CTC++ Coverage Report - Execution Profile    #310/1532

Files Summary | Functions Summary | Execution Profile | Index | No Index
First | Previous | Next | Last


File: drivers/block/nbd.c
Instrumentation mode: function-decision-multicondition
TER: 3 % ( 14/417)

Start/ End/    
True False - Line Source

  1 /*
  2  * Network block device - make block devices work over TCP
  3  *
  4  * Note that you can not swap over this thing, yet. Seems to work but
  5  * deadlocks sometimes - you can not swap over TCP in general.
  6  * 
  7  * Copyright 1997-2000 Pavel Machek <pavel@ucw.cz>
  8  * Parts copyright 2001 Steven Whitehouse <steve@chygwyn.com>
  9  *
  10  * (part of code stolen from loop.c)
  11  *
  12  * 97-3-25 compiled 0-th version, not yet tested it 
  13  *   (it did not work, BTW) (later that day) HEY! it works!
  14  *   (bit later) hmm, not that much... 2:00am next day:
  15  *   yes, it works, but it gives something like 50kB/sec
  16  * 97-4-01 complete rewrite to make it possible for many requests at 
  17  *   once to be processed
  18  * 97-4-11 Making protocol independent of endianity etc.
  19  * 97-9-13 Cosmetic changes
  20  * 98-5-13 Attempt to make 64-bit-clean on 64-bit machines
  21  * 99-1-11 Attempt to make 64-bit-clean on 32-bit machines <ankry@mif.pg.gda.pl>
  22  * 01-2-27 Fix to store proper blockcount for kernel (calculated using
  23  *   BLOCK_SIZE_BITS, not device blocksize) <aga@permonline.ru>
  24  * 01-3-11 Make nbd work with new Linux block layer code. It now supports
  25  *   plugging like all the other block devices. Also added in MSG_MORE to
  26  *   reduce number of partial TCP segments sent. <steve@chygwyn.com>
  27  * 01-12-6 Fix deadlock condition by making queue locks independent of
  28  *   the transmit lock. <steve@chygwyn.com>
  29  * 02-10-11 Allow hung xmit to be aborted via SIGKILL & various fixes.
  30  *   <Paul.Clements@SteelEye.com> <James.Bottomley@SteelEye.com>
  31  * 03-06-22 Make nbd work with new linux 2.5 block layer design. This fixes
  32  *   memory corruption from module removal and possible memory corruption
  33  *   from sending/receiving disk data. <ldl@aros.net>
  34  * 03-06-23 Cosmetic changes. <ldl@aros.net>
  35  * 03-06-23 Enhance diagnostics support. <ldl@aros.net>
  36  * 03-06-24 Remove unneeded blksize_bits field from nbd_device struct.
  37  *   <ldl@aros.net>
  38  * 03-06-24 Cleanup PARANOIA usage & code. <ldl@aros.net>
  39  * 04-02-19 Remove PARANOIA, plus various cleanups (Paul Clements)
  40  * possible FIXME: make set_sock / set_blksize / set_size / do_it one syscall
  41  * why not: would need access_ok and friends, would share yet another
  42  *          structure with userland
  43  */
  44 
  45 #include <linux/major.h>
  46 
  47 #include <linux/blkdev.h>
  48 #include <linux/module.h>
  49 #include <linux/init.h>
  50 #include <linux/sched.h>
  51 #include <linux/fs.h>
  52 #include <linux/bio.h>
  53 #include <linux/stat.h>
  54 #include <linux/errno.h>
  55 #include <linux/file.h>
  56 #include <linux/ioctl.h>
  57 #include <linux/compiler.h>
  58 #include <linux/err.h>
  59 #include <linux/kernel.h>
  60 #include <net/sock.h>
  61 
  62 #include <linux/devfs_fs_kernel.h>
  63 
  64 #include <asm/uaccess.h>
  65 #include <asm/system.h>
  66 #include <asm/types.h>
  67 
  68 #include <linux/nbd.h>
  69 
  70 #define LO_MAGIC 0x68797548
  71 
  72 #ifdef NDEBUG
  73 #define dprintk(flags, fmt...)
  74 #else /* NDEBUG */
  75 #define dprintk(flags, fmt...) do { \
  76    if (debugflags & (flags)) printk(KERN_DEBUG fmt); \
  77 } while (0)
  78 #define DBG_IOCTL       0x0004
  79 #define DBG_INIT        0x0010
  80 #define DBG_EXIT        0x0020
  81 #define DBG_BLKDEV      0x0100
  82 #define DBG_RX          0x0200
  83 #define DBG_TX          0x0400
  84 static unsigned int debugflags;
  85 static unsigned int nbds_max = 16;
  86 #endif /* NDEBUG */
  87 
  88 static struct nbd_device nbd_dev[MAX_NBD];
  89 
  90 /*
  91  * Use just one lock (or at most 1 per NIC). Two arguments for this:
  92  * 1. Each NIC is essentially a synchronization point for all servers
  93  *    accessed through that NIC so there's no need to have more locks
  94  *    than NICs anyway.
  95  * 2. More locks lead to more "Dirty cache line bouncing" which will slow
  96  *    down each lock to the point where they're actually slower than just
  97  *    a single lock.
  98  * Thanks go to Jens Axboe and Al Viro for their LKML emails explaining this!
  99  */
  100 static DEFINE_SPINLOCK(nbd_lock);
  101 
  102 #ifndef NDEBUG
 
- 103 static const char *ioctl_cmd_to_ascii(int cmd)
  104 {
    105    switch (cmd) {
 - 106    case NBD_SET_SOCK: return "set-sock";
 - 106   return "set-sock"
 - 107    case NBD_SET_BLKSIZE: return "set-blksize";
 - 107   return "set-blksize"
 - 108    case NBD_SET_SIZE: return "set-size";
 - 108   return "set-size"
 - 109    case NBD_DO_IT: return "do-it";
 - 109   return "do-it"
 - 110    case NBD_CLEAR_SOCK: return "clear-sock";
 - 110   return "clear-sock"
 - 111    case NBD_CLEAR_QUE: return "clear-que";
 - 111   return "clear-que"
 - 112    case NBD_PRINT_DEBUG: return "print-debug";
 - 112   return "print-debug"
 - 113    case NBD_SET_SIZE_BLOCKS: return "set-size-blocks";
 - 113   return "set-size-blocks"
 - 114    case NBD_DISCONNECT: return "disconnect";
 - 114   return "disconnect"
 - 115    case BLKROSET: return "set-read-only";
 - 115   return "set-read-only"
 - 116    case BLKFLSBUF: return "flush-buffer-cache";
 - 116   return "flush-buffer-cache"
  117    }
 - 118    return "unknown";
  119 }
  120 
 
- 121 static const char *nbdcmd_to_ascii(int cmd)
  122 {
    123    switch (cmd) {
 - 124    case  NBD_CMD_READ: return "read";
 - 124   return "read"
 - 125    case NBD_CMD_WRITE: return "write";
 - 125   return "write"
 - 126    case  NBD_CMD_DISC: return "disconnect";
 - 126   return "disconnect"
  127    }
 - 128    return "invalid";
  129 }
  130 #endif /* NDEBUG */
  131 
 
- 132 static void nbd_end_request(struct request *req)
  133 {
    134    int uptodate = (req->errors == 0) ? 1 : 0;
- 134 ternary-?: ( req -> errors == 0 )
  135    request_queue_t *q = req->q;
  136    unsigned long flags;
  137 
    138    dprintk(DBG_BLKDEV, "%s: request %p: %s\n", req->rq_disk->disk_name,
- 138   if (debugflags & ( 0x0100 ))
- 138     ternary-?: uptodate
- 138 do-while (0)
  139          req, uptodate? "done": "failed");
  140 
    141    spin_lock_irqsave(q->queue_lock, flags);
    141   do
    141     do
- 141     do-while (0)
- 141   do-while (0)
    141   do
    141     do
- 141     do-while (0)
- 141   do-while (0)
- 141 do-while (0)
- 142    if (!end_that_request_first(req, uptodate, req->nr_sectors)) {
  143       end_that_request_last(req, uptodate);
  144    }
    145    spin_unlock_irqrestore(q->queue_lock, flags);
    145   do
    145     do
- 145     do-while (0)
- 145   do-while (0)
- 145 do-while (0)
  146 }
  147 
  148 /*
  149  *  Send or receive packet.
  150  */
 
- 151 static int sock_xmit(struct socket *sock, int send, void *buf, int size,
  152       int msg_flags)
  153 {
  154    int result;
  155    struct msghdr msg;
  156    struct kvec iov;
  157    unsigned long flags;
  158    sigset_t oldset;
  159 
  160    /* Allow interception of SIGKILL only
  161     * Don't allow other signals to interrupt the transmission */
    162    spin_lock_irqsave(&current->sighand->siglock, flags);
    162   do
    162     do
- 162     do-while (0)
- 162   do-while (0)
    162   do
    162     do
- 162     do-while (0)
- 162   do-while (0)
- 162 do-while (0)
  163    oldset = current->blocked;
  164    sigfillset(&current->blocked);
  165    sigdelsetmask(&current->blocked, sigmask(SIGKILL));
  166    recalc_sigpending();
    167    spin_unlock_irqrestore(&current->sighand->siglock, flags);
    167   do
    167     do
- 167     do-while (0)
- 167   do-while (0)
- 167 do-while (0)
  168 
    169    do {
  170       sock->sk->sk_allocation = GFP_NOIO;
  171       iov.iov_base = buf;
  172       iov.iov_len = size;
  173       msg.msg_name = NULL;
  174       msg.msg_namelen = 0;
  175       msg.msg_control = NULL;
  176       msg.msg_controllen = 0;
  177       msg.msg_flags = msg_flags | MSG_NOSIGNAL;
  178 
- 179       if (send)
  180          result = kernel_sendmsg(sock, &msg, &iov, 1, size);
    181       else
  182          result = kernel_recvmsg(sock, &msg, &iov, 1, size, 0);
  183 
- 184       if (signal_pending(current)) {
  185          siginfo_t info;
    186          spin_lock_irqsave(&current->sighand->siglock, flags);
    186       do
    186         do
- 186         do-while (0)
- 186       do-while (0)
    186       do
    186         do
- 186         do-while (0)
- 186       do-while (0)
- 186     do-while (0)
  187          printk(KERN_WARNING "nbd (pid %d: %s) got signal %d\n",
  188             current->pid, current->comm, 
  189             dequeue_signal(current, &current->blocked, &info));
    190          spin_unlock_irqrestore(&current->sighand->siglock, flags);
    190       do
    190         do
- 190         do-while (0)
- 190       do-while (0)
- 190     do-while (0)
  191          result = -EINTR;
 - 192          break;
  193       }
  194 
- 195       if (result <= 0) {
- 196          if (result == 0)
  197             result = -EPIPE; /* short read */
 - 198          break;
  199       }
  200       size -= result;
  201       buf += result;
- 202    } while (size > 0);
  203 
    204    spin_lock_irqsave(&current->sighand->siglock, flags);
    204   do
    204     do
- 204     do-while (0)
- 204   do-while (0)
    204   do
    204     do
- 204     do-while (0)
- 204   do-while (0)
- 204 do-while (0)
  205    current->blocked = oldset;
  206    recalc_sigpending();
    207    spin_unlock_irqrestore(&current->sighand->siglock, flags);
    207   do
    207     do
- 207     do-while (0)
- 207   do-while (0)
- 207 do-while (0)
  208 
 - 209    return result;
  210 }
  211 
 
- 212 static inline int sock_send_bvec(struct socket *sock, struct bio_vec *bvec,
  213       int flags)
  214 {
  215    int result;
  216    void *kaddr = kmap(bvec->bv_page);
  217    result = sock_xmit(sock, 1, kaddr + bvec->bv_offset, bvec->bv_len,
  218          flags);
    219    kunmap(bvec->bv_page);
- 219 do-while (0)
 - 220    return result;
  221 }
  222 
 
- 223 static int nbd_send_req(struct nbd_device *lo, struct request *req)
  224 {
  225    int result, i, flags;
  226    struct nbd_request request;
  227    unsigned long size = req->nr_sectors << 9;
  228    struct socket *sock = lo->sock;
  229 
    230    request.magic = htonl(NBD_REQUEST_MAGIC);
- 230 ternary-?: __builtin_constant_p ( ( __u32 ) ( ..
    231    request.type = htonl(nbd_cmd(req));
- 231 ternary-?: __builtin_constant_p ( ( __u32 ) ( ..
    232    request.from = cpu_to_be64((u64) req->sector << 9);
- 232 ternary-?: __builtin_constant_p ( ( __u64 ) ( ..
    233    request.len = htonl(size);
- 233 ternary-?: __builtin_constant_p ( ( __u32 ) ( ..
  234    memcpy(request.handle, &req, sizeof(req));
  235 
    236    dprintk(DBG_TX, "%s: request %p: sending control (%s@%llu,%luB)\n",
- 236   if (debugflags & ( 0x0400 ))
- 236 do-while (0)
  237          lo->disk->disk_name, req,
  238          nbdcmd_to_ascii(nbd_cmd(req)),
  239          (unsigned long long)req->sector << 9,
  240          req->nr_sectors << 9);
  241    result = sock_xmit(sock, 1, &request, sizeof(request),
    242          (nbd_cmd(req) == NBD_CMD_WRITE)? MSG_MORE: 0);
- 242 ternary-?: ( ( ( req ) -> cmd [ 0 ] ) == NBD_C..
- 243    if (result <= 0) {
  244       printk(KERN_ERR "%s: Send control failed (result %d)\n",
  245             lo->disk->disk_name, result);
 - 246       goto error_out;
  247    }
  248 
- 249    if (nbd_cmd(req) == NBD_CMD_WRITE) {
  250       struct bio *bio;
  251       /*
  252        * we are really probing at internals to determine
  253        * whether to set MSG_MORE or not...
  254        */
- 255       rq_for_each_bio(bio, req) {
- 255     for (;bio;)
  256          struct bio_vec *bvec;
- 257          bio_for_each_segment(bvec, bio, i) {
  258             flags = 0;
- 259             if ((i < (bio->bi_vcnt - 1)) || bio->bi_next)
 - 259           (T) || _
 - 259           (F) || T
 - 259           (F) || F
  260                flags = MSG_MORE;
    261             dprintk(DBG_TX, "%s: request %p: sending %d bytes data\n",
- 261           if (debugflags & ( 0x0400 ))
- 261         do-while (0)
  262                   lo->disk->disk_name, req,
  263                   bvec->bv_len);
  264             result = sock_send_bvec(sock, bvec, flags);
- 265             if (result <= 0) {
  266                printk(KERN_ERR "%s: Send data failed (result %d)\n",
  267                      lo->disk->disk_name,
  268                      result);
 - 269                goto error_out;
  270             }
  271          }
  272       }
  273    }
 - 274    return 0;
  275 
  276 error_out:
 - 277    return 1;
  278 }
  279 
 
- 280 static struct request *nbd_find_request(struct nbd_device *lo, char *handle)
  281 {
  282    struct request *req;
  283    struct list_head *tmp;
  284    struct request *xreq;
  285    int err;
  286 
  287    memcpy(&xreq, handle, sizeof(xreq));
  288 
  289    err = wait_event_interruptible(lo->active_wq, lo->active_req != xreq);
- 290    if (unlikely(err))
 - 291       goto out;
  292 
    293    spin_lock(&lo->queue_lock);
    293   do
- 293   do-while (0)
- 293 do-while (0)
- 294    list_for_each(tmp, &lo->queue_head) {
  295       req = list_entry(tmp, struct request, queuelist);
- 296       if (req != xreq)
 - 297          continue;
  298       list_del_init(&req->queuelist);
    299       spin_unlock(&lo->queue_lock);
    299     do
- 299     do-while (0)
- 299   do-while (0)
 - 300       return req;
  301    }
    302    spin_unlock(&lo->queue_lock);
    302   do
- 302   do-while (0)
- 302 do-while (0)
  303 
  304    err = -ENOENT;
  305 
  306 out:
 - 307    return ERR_PTR(err);
  308 }
  309 
 
- 310 static inline int sock_recv_bvec(struct socket *sock, struct bio_vec *bvec)
  311 {
  312    int result;
  313    void *kaddr = kmap(bvec->bv_page);
  314    result = sock_xmit(sock, 0, kaddr + bvec->bv_offset, bvec->bv_len,
  315          MSG_WAITALL);
    316    kunmap(bvec->bv_page);
- 316 do-while (0)
 - 317    return result;
  318 }
  319 
  320 /* NULL returned = something went wrong, inform userspace */
 
- 321 static struct request *nbd_read_stat(struct nbd_device *lo)
  322 {
  323    int result;
  324    struct nbd_reply reply;
  325    struct request *req;
  326    struct socket *sock = lo->sock;
  327 
  328    reply.magic = 0;
  329    result = sock_xmit(sock, 0, &reply, sizeof(reply), MSG_WAITALL);
- 330    if (result <= 0) {
  331       printk(KERN_ERR "%s: Receive control failed (result %d)\n",
  332             lo->disk->disk_name, result);
 - 333       goto harderror;
  334    }
  335    req = nbd_find_request(lo, reply.handle);
- 336    if (unlikely(IS_ERR(req))) {
  337       result = PTR_ERR(req);
- 338       if (result != -ENOENT)
 - 339          goto harderror;
  340 
  341       printk(KERN_ERR "%s: Unexpected reply (%p)\n",
  342             lo->disk->disk_name, reply.handle);
  343       result = -EBADR;
 - 344       goto harderror;
  345    }
  346 
- 347    if (ntohl(reply.magic) != NBD_REPLY_MAGIC) {
- 347 ternary-?: __builtin_constant_p ( ( __u32 ) ( ..
  348       printk(KERN_ERR "%s: Wrong magic (0x%lx)\n",
  349             lo->disk->disk_name,
    350             (unsigned long)ntohl(reply.magic));
- 350   ternary-?: __builtin_constant_p ( ( __u32 ) ..
  351       result = -EPROTO;
 - 352       goto harderror;
  353    }
- 354    if (ntohl(reply.error)) {
- 354 ternary-?: __builtin_constant_p ( ( __u32 ) ( ..
  355       printk(KERN_ERR "%s: Other side returned error (%d)\n",
    356             lo->disk->disk_name, ntohl(reply.error));
- 356   ternary-?: __builtin_constant_p ( ( __u32 ) ..
  357       req->errors++;
 - 358       return req;
  359    }
  360 
    361    dprintk(DBG_RX, "%s: request %p: got reply\n",
- 361   if (debugflags & ( 0x0200 ))
- 361 do-while (0)
  362          lo->disk->disk_name, req);
- 363    if (nbd_cmd(req) == NBD_CMD_READ) {
  364       int i;
  365       struct bio *bio;
- 366       rq_for_each_bio(bio, req) {
- 366     for (;bio;)
  367          struct bio_vec *bvec;
- 368          bio_for_each_segment(bvec, bio, i) {
  369             result = sock_recv_bvec(sock, bvec);
- 370             if (result <= 0) {
  371                printk(KERN_ERR "%s: Receive data failed (result %d)\n",
  372                      lo->disk->disk_name,
  373                      result);
 - 374                goto harderror;
  375             }
    376             dprintk(DBG_RX, "%s: request %p: got %d bytes data\n",
- 376           if (debugflags & ( 0x0200 ))
- 376         do-while (0)
  377                lo->disk->disk_name, req, bvec->bv_len);
  378          }
  379       }
  380    }
 - 381    return req;
  382 harderror:
  383    lo->harderror = result;