2 * Network block device - make block devices work over TCP
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.
7 * Copyright 1997-2000 Pavel Machek <pavel@ucw.cz>
8 * Parts copyright 2001 Steven Whitehouse <steve@chygwyn.com>
10 * (part of code stolen from loop.c)
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.
38 * 03-06-24 Cleanup PARANOIA usage & code. <ldl@aros.net>
40 * possible FIXME: make set_sock / set_blksize / set_size / do_it one syscall
41 * why not: would need verify_area and friends, would share yet another
42 * structure with userland
45 #include <linux/major.h>
47 #include <linux/blkdev.h>
48 #include <linux/module.h>
49 #include <linux/init.h>
50 #include <linux/sched.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>
59 #include <linux/devfs_fs_kernel.h>
61 #include <asm/uaccess.h>
62 #include <asm/types.h>
64 /* Define PARANOIA in linux/nbd.h to turn on extra sanity checking */
65 #include <linux/nbd.h>
68 #define LO_MAGIC 0x68797548
72 #define dprintk(flags, fmt...)
74 #define dprintk(flags, fmt...) do { \
75 if (debugflags & (flags)) printk(KERN_DEBUG fmt); \
77 #define DBG_IOCTL 0x0004
78 #define DBG_INIT 0x0010
79 #define DBG_EXIT 0x0020
80 #define DBG_BLKDEV 0x0100
83 static unsigned int debugflags;
86 static struct nbd_device nbd_dev[MAX_NBD];
89 * Use just one lock (or at most 1 per NIC). Two arguments for this:
90 * 1. Each NIC is essentially a synchronization point for all servers
91 * accessed through that NIC so there's no need to have more locks
93 * 2. More locks lead to more "Dirty cache line bouncing" which will slow
94 * down each lock to the point where they're actually slower than just
96 * Thanks go to Jens Axboe and Al Viro for their LKML emails explaining this!
98 static spinlock_t nbd_lock = SPIN_LOCK_UNLOCKED;
101 static int requests_in;
102 static int requests_out;
106 static const char *ioctl_cmd_to_ascii(int cmd)
109 case NBD_SET_SOCK: return "set-sock";
110 case NBD_SET_BLKSIZE: return "set-blksize";
111 case NBD_SET_SIZE: return "set-size";
112 case NBD_DO_IT: return "do-it";
113 case NBD_CLEAR_SOCK: return "clear-sock";
114 case NBD_CLEAR_QUE: return "clear-que";
115 case NBD_PRINT_DEBUG: return "print-debug";
116 case NBD_SET_SIZE_BLOCKS: return "set-size-blocks";
117 case NBD_DISCONNECT: return "disconnect";
118 case BLKROSET: return "set-read-only";
119 case BLKFLSBUF: return "flush-buffer-cache";
124 static const char *nbdcmd_to_ascii(int cmd)
127 case NBD_CMD_READ: return "read";
128 case NBD_CMD_WRITE: return "write";
129 case NBD_CMD_DISC: return "disconnect";
135 static void nbd_end_request(struct request *req)
137 int uptodate = (req->errors == 0) ? 1 : 0;
138 request_queue_t *q = req->q;
139 struct nbd_device *lo = req->rq_disk->private_data;
142 dprintk(DBG_BLKDEV, "%s: request %p: %s\n", req->rq_disk->disk_name,
143 req, uptodate? "done": "failed");
145 spin_lock(&lo->queue_lock);
146 while (req->ref_count > 1) { /* still in send */
147 spin_unlock(&lo->queue_lock);
148 printk(KERN_DEBUG "%s: request %p still in use (%d), waiting\n",
149 lo->disk->disk_name, req, req->ref_count);
150 set_current_state(TASK_UNINTERRUPTIBLE);
151 schedule_timeout(HZ); /* wait a second */
152 spin_lock(&lo->queue_lock);
154 spin_unlock(&lo->queue_lock);
159 spin_lock_irqsave(q->queue_lock, flags);
160 if (!end_that_request_first(req, uptodate, req->nr_sectors)) {
161 end_that_request_last(req);
163 spin_unlock_irqrestore(q->queue_lock, flags);
167 * Send or receive packet.
169 static int sock_xmit(struct socket *sock, int send, void *buf, int size,
181 /* Allow interception of SIGKILL only
182 * Don't allow other signals to interrupt the transmission */
183 spin_lock_irqsave(¤t->sighand->siglock, flags);
184 oldset = current->blocked;
185 sigfillset(¤t->blocked);
186 sigdelsetmask(¤t->blocked, sigmask(SIGKILL));
188 spin_unlock_irqrestore(¤t->sighand->siglock, flags);
191 sock->sk->sk_allocation = GFP_NOIO;
198 msg.msg_control = NULL;
199 msg.msg_controllen = 0;
201 msg.msg_flags = msg_flags | MSG_NOSIGNAL;
204 result = sock_sendmsg(sock, &msg, size);
206 result = sock_recvmsg(sock, &msg, size, 0);
208 if (signal_pending(current)) {
210 spin_lock_irqsave(¤t->sighand->siglock, flags);
211 printk(KERN_WARNING "nbd (pid %d: %s) got signal %d\n",
212 current->pid, current->comm,
213 dequeue_signal(current, ¤t->blocked, &info));
214 spin_unlock_irqrestore(¤t->sighand->siglock, flags);
221 printk(KERN_ERR "nbd: %s - sock=%p at buf=%p, size=%d returned %d.\n",
222 send? "send": "receive", sock, buf, size, result);
230 spin_lock_irqsave(¤t->sighand->siglock, flags);
231 current->blocked = oldset;
233 spin_unlock_irqrestore(¤t->sighand->siglock, flags);
239 static inline int sock_send_bvec(struct socket *sock, struct bio_vec *bvec,
243 void *kaddr = kmap(bvec->bv_page);
244 result = sock_xmit(sock, 1, kaddr + bvec->bv_offset, bvec->bv_len,
246 kunmap(bvec->bv_page);
250 void nbd_send_req(struct nbd_device *lo, struct request *req)
252 int result, i, flags;
253 struct nbd_request request;
254 unsigned long size = req->nr_sectors << 9;
255 struct socket *sock = lo->sock;
257 request.magic = htonl(NBD_REQUEST_MAGIC);
258 request.type = htonl(nbd_cmd(req));
259 request.from = cpu_to_be64((u64) req->sector << 9);
260 request.len = htonl(size);
261 memcpy(request.handle, &req, sizeof(req));
265 if (!sock || !lo->sock) {
266 printk(KERN_ERR "%s: Attempted send on closed socket\n",
267 lo->disk->disk_name);
271 dprintk(DBG_TX, "%s: request %p: sending control (%s@%llu,%luB)\n",
272 lo->disk->disk_name, req,
273 nbdcmd_to_ascii(nbd_cmd(req)),
274 (unsigned long long)req->sector << 9,
275 req->nr_sectors << 9);
276 result = sock_xmit(sock, 1, &request, sizeof(request),
277 (nbd_cmd(req) == NBD_CMD_WRITE)? MSG_MORE: 0);
279 printk(KERN_ERR "%s: Send control failed (result %d)\n",
280 lo->disk->disk_name, result);
284 if (nbd_cmd(req) == NBD_CMD_WRITE) {
287 * we are really probing at internals to determine
288 * whether to set MSG_MORE or not...
290 rq_for_each_bio(bio, req) {
291 struct bio_vec *bvec;
292 bio_for_each_segment(bvec, bio, i) {
294 if ((i < (bio->bi_vcnt - 1)) || bio->bi_next)
296 dprintk(DBG_TX, "%s: request %p: sending %d bytes data\n",
297 lo->disk->disk_name, req,
299 result = sock_send_bvec(sock, bvec, flags);
301 printk(KERN_ERR "%s: Send data failed (result %d)\n",
317 static struct request *nbd_find_request(struct nbd_device *lo, char *handle)
320 struct list_head *tmp;
321 struct request *xreq;
323 memcpy(&xreq, handle, sizeof(xreq));
325 spin_lock(&lo->queue_lock);
326 list_for_each(tmp, &lo->queue_head) {
327 req = list_entry(tmp, struct request, queuelist);
330 list_del_init(&req->queuelist);
331 spin_unlock(&lo->queue_lock);
334 spin_unlock(&lo->queue_lock);
338 static inline int sock_recv_bvec(struct socket *sock, struct bio_vec *bvec)
341 void *kaddr = kmap(bvec->bv_page);
342 result = sock_xmit(sock, 0, kaddr + bvec->bv_offset, bvec->bv_len,
344 kunmap(bvec->bv_page);
348 /* NULL returned = something went wrong, inform userspace */
349 struct request *nbd_read_stat(struct nbd_device *lo)
352 struct nbd_reply reply;
354 struct socket *sock = lo->sock;
357 result = sock_xmit(sock, 0, &reply, sizeof(reply), MSG_WAITALL);
359 printk(KERN_ERR "%s: Receive control failed (result %d)\n",
360 lo->disk->disk_name, result);
361 lo->harderror = result;
364 req = nbd_find_request(lo, reply.handle);
366 printk(KERN_ERR "%s: Unexpected reply (%p)\n",
367 lo->disk->disk_name, reply.handle);
368 lo->harderror = result;
372 if (ntohl(reply.magic) != NBD_REPLY_MAGIC) {
373 printk(KERN_ERR "%s: Wrong magic (0x%lx)\n",
375 (unsigned long)ntohl(reply.magic));
376 lo->harderror = result;
379 if (ntohl(reply.error)) {
380 printk(KERN_ERR "%s: Other side returned error (%d)\n",
381 lo->disk->disk_name, ntohl(reply.error));
386 dprintk(DBG_RX, "%s: request %p: got reply\n",
387 lo->disk->disk_name, req);
388 if (nbd_cmd(req) == NBD_CMD_READ) {
391 rq_for_each_bio(bio, req) {
392 struct bio_vec *bvec;
393 bio_for_each_segment(bvec, bio, i) {
394 result = sock_recv_bvec(sock, bvec);
396 printk(KERN_ERR "%s: Receive data failed (result %d)\n",
399 lo->harderror = result;
402 dprintk(DBG_RX, "%s: request %p: got %d bytes data\n",
403 lo->disk->disk_name, req, bvec->bv_len);
410 void nbd_do_it(struct nbd_device *lo)
415 BUG_ON(lo->magic != LO_MAGIC);
417 while ((req = nbd_read_stat(lo)) != NULL)
418 nbd_end_request(req);
419 printk(KERN_NOTICE "%s: req should never be null\n",
420 lo->disk->disk_name);
424 void nbd_clear_que(struct nbd_device *lo)
429 BUG_ON(lo->magic != LO_MAGIC);
434 spin_lock(&lo->queue_lock);
435 if (!list_empty(&lo->queue_head)) {
436 req = list_entry(lo->queue_head.next, struct request, queuelist);
437 list_del_init(&req->queuelist);
439 spin_unlock(&lo->queue_lock);
442 nbd_end_request(req);
448 * We always wait for result of write, for now. It would be nice to make it optional
450 * if ((req->cmd == WRITE) && (lo->flags & NBD_WRITE_NOCHK))
451 * { printk( "Warning: Ignoring result!\n"); nbd_end_request( req ); }
454 static void do_nbd_request(request_queue_t * q)
458 while ((req = elv_next_request(q)) != NULL) {
459 struct nbd_device *lo;
461 blkdev_dequeue_request(req);
462 dprintk(DBG_BLKDEV, "%s: request %p: dequeued (flags=%lx)\n",
463 req->rq_disk->disk_name, req, req->flags);
465 if (!(req->flags & REQ_CMD))
468 lo = req->rq_disk->private_data;
470 BUG_ON(lo->magic != LO_MAGIC);
473 printk(KERN_ERR "%s: Request when not-ready\n",
474 lo->disk->disk_name);
477 nbd_cmd(req) = NBD_CMD_READ;
478 if (rq_data_dir(req) == WRITE) {
479 nbd_cmd(req) = NBD_CMD_WRITE;
480 if (lo->flags & NBD_READ_ONLY) {
481 printk(KERN_ERR "%s: Write on read-only\n",
482 lo->disk->disk_name);
491 spin_unlock_irq(q->queue_lock);
493 spin_lock(&lo->queue_lock);
496 spin_unlock(&lo->queue_lock);
497 printk(KERN_ERR "%s: failed between accept and semaphore, file lost\n",
498 lo->disk->disk_name);
500 nbd_end_request(req);
501 spin_lock_irq(q->queue_lock);
505 list_add(&req->queuelist, &lo->queue_head);
506 req->ref_count++; /* make sure req does not get freed */
507 spin_unlock(&lo->queue_lock);
509 nbd_send_req(lo, req);
512 printk(KERN_ERR "%s: Request send failed\n",
513 lo->disk->disk_name);
514 spin_lock(&lo->queue_lock);
515 list_del_init(&req->queuelist);
517 spin_unlock(&lo->queue_lock);
518 nbd_end_request(req);
519 spin_lock_irq(q->queue_lock);
523 spin_lock(&lo->queue_lock);
525 spin_unlock(&lo->queue_lock);
526 spin_lock_irq(q->queue_lock);
531 spin_unlock(q->queue_lock);
532 nbd_end_request(req);
533 spin_lock(q->queue_lock);
538 static int nbd_ioctl(struct inode *inode, struct file *file,
539 unsigned int cmd, unsigned long arg)
541 struct nbd_device *lo = inode->i_bdev->bd_disk->private_data;
543 struct request sreq ;
545 if (!capable(CAP_SYS_ADMIN))
548 BUG_ON(lo->magic != LO_MAGIC);
550 /* Anyone capable of this syscall can do *real bad* things */
551 dprintk(DBG_IOCTL, "%s: nbd_ioctl cmd=%s(0x%x) arg=%lu\n",
552 lo->disk->disk_name, ioctl_cmd_to_ascii(cmd), cmd, arg);
556 printk(KERN_INFO "%s: NBD_DISCONNECT\n", lo->disk->disk_name);
557 sreq.flags = REQ_SPECIAL;
558 nbd_cmd(&sreq) = NBD_CMD_DISC;
560 * Set these to sane values in case server implementation
561 * fails to check the request type first and also to keep
562 * debugging output cleaner.
568 nbd_send_req(lo, &sreq);
576 spin_lock(&lo->queue_lock);
579 spin_unlock(&lo->queue_lock);
581 spin_lock(&lo->queue_lock);
582 if (!list_empty(&lo->queue_head)) {
583 printk(KERN_ERR "nbd: disconnect: some requests are in progress -> please try again.\n");
586 spin_unlock(&lo->queue_lock);
596 inode = file->f_dentry->d_inode;
599 lo->sock = SOCKET_I(inode);
606 case NBD_SET_BLKSIZE:
608 lo->bytesize &= ~(lo->blksize-1);
609 inode->i_bdev->bd_inode->i_size = lo->bytesize;
610 set_blocksize(inode->i_bdev, lo->blksize);
611 set_capacity(lo->disk, lo->bytesize >> 9);
614 lo->bytesize = arg & ~(lo->blksize-1);
615 inode->i_bdev->bd_inode->i_size = lo->bytesize;
616 set_blocksize(inode->i_bdev, lo->blksize);
617 set_capacity(lo->disk, lo->bytesize >> 9);
619 case NBD_SET_SIZE_BLOCKS:
620 lo->bytesize = ((u64) arg) * lo->blksize;
621 inode->i_bdev->bd_inode->i_size = lo->bytesize;
622 set_blocksize(inode->i_bdev, lo->blksize);
623 set_capacity(lo->disk, lo->bytesize >> 9);
629 /* on return tidy up in case we have a signal */
630 /* Forcibly shutdown the socket causing all listeners
633 * FIXME: This code is duplicated from sys_shutdown, but
634 * there should be a more generic interface rather than
635 * calling socket ops directly here */
638 printk(KERN_WARNING "%s: shutting down socket\n",
639 lo->disk->disk_name);
640 lo->sock->ops->shutdown(lo->sock,
641 SEND_SHUTDOWN|RCV_SHUTDOWN);
645 spin_lock(&lo->queue_lock);
648 spin_unlock(&lo->queue_lock);
650 printk(KERN_WARNING "%s: queue cleared\n", lo->disk->disk_name);
653 return lo->harderror;
658 return 0; /* probably should be error, but that would
659 * break "nbd-client -d", so just return 0 */
664 case NBD_PRINT_DEBUG:
666 printk(KERN_INFO "%s: next = %p, prev = %p. Global: in %d, out %d\n",
667 inode->i_bdev->bd_disk->disk_name, lo->queue_head.next,
668 lo->queue_head.prev, requests_in, requests_out);
670 printk(KERN_INFO "%s: next = %p, prev = %p\n",
671 inode->i_bdev->bd_disk->disk_name,
672 lo->queue_head.next, lo->queue_head.prev);
679 static struct block_device_operations nbd_fops =
681 .owner = THIS_MODULE,
686 * And here should be modules and kernel interface
687 * (Just smiley confuses emacs :-)
690 static int __init nbd_init(void)
696 if (sizeof(struct nbd_request) != 28) {
697 printk(KERN_CRIT "nbd: Sizeof nbd_request needs to be 28 in order to work!\n" );
702 for (i = 0; i < MAX_NBD; i++) {
703 struct gendisk *disk = alloc_disk(1);
706 nbd_dev[i].disk = disk;
708 * The new linux 2.5 block layer implementation requires
709 * every gendisk to have its very own request_queue struct.
710 * These structs are big so we dynamically allocate them.
712 disk->queue = blk_init_queue(do_nbd_request, &nbd_lock);
719 if (register_blkdev(NBD_MAJOR, "nbd")) {
724 printk(KERN_INFO "nbd: registered device at major %d\n", NBD_MAJOR);
725 dprintk(DBG_INIT, "nbd: debugflags=0x%x\n", debugflags);
728 for (i = 0; i < MAX_NBD; i++) {
729 struct gendisk *disk = nbd_dev[i].disk;
730 nbd_dev[i].file = NULL;
732 nbd_dev[i].magic = LO_MAGIC;
734 nbd_dev[i].flags = 0;
735 spin_lock_init(&nbd_dev[i].queue_lock);
736 INIT_LIST_HEAD(&nbd_dev[i].queue_head);
737 init_MUTEX(&nbd_dev[i].tx_lock);
738 nbd_dev[i].blksize = 1024;
739 nbd_dev[i].bytesize = ((u64)0x7ffffc00) << 10; /* 2TB */
740 disk->major = NBD_MAJOR;
741 disk->first_minor = i;
742 disk->fops = &nbd_fops;
743 disk->private_data = &nbd_dev[i];
744 disk->flags |= GENHD_FL_SUPPRESS_PARTITION_INFO;
745 sprintf(disk->disk_name, "nbd%d", i);
746 sprintf(disk->devfs_name, "nbd/%d", i);
747 set_capacity(disk, 0x3ffffe);
754 if (nbd_dev[i].disk->queue)
755 blk_cleanup_queue(nbd_dev[i].disk->queue);
756 put_disk(nbd_dev[i].disk);
761 static void __exit nbd_cleanup(void)
764 for (i = 0; i < MAX_NBD; i++) {
765 struct gendisk *disk = nbd_dev[i].disk;
768 blk_cleanup_queue(disk->queue);
774 unregister_blkdev(NBD_MAJOR, "nbd");
775 printk(KERN_INFO "nbd: unregistered device at major %d\n", NBD_MAJOR);
778 module_init(nbd_init);
779 module_exit(nbd_cleanup);
781 MODULE_DESCRIPTION("Network Block Device");
782 MODULE_LICENSE("GPL");
785 MODULE_PARM(debugflags, "i");
786 MODULE_PARM_DESC(debugflags, "flags for controlling debug output");