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>
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 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 #include <linux/nbd.h>
66 #define LO_MAGIC 0x68797548
69 #define dprintk(flags, fmt...)
71 #define dprintk(flags, fmt...) do { \
72 if (debugflags & (flags)) printk(KERN_DEBUG fmt); \
74 #define DBG_IOCTL 0x0004
75 #define DBG_INIT 0x0010
76 #define DBG_EXIT 0x0020
77 #define DBG_BLKDEV 0x0100
80 static unsigned int debugflags;
83 static struct nbd_device nbd_dev[MAX_NBD];
86 * Use just one lock (or at most 1 per NIC). Two arguments for this:
87 * 1. Each NIC is essentially a synchronization point for all servers
88 * accessed through that NIC so there's no need to have more locks
90 * 2. More locks lead to more "Dirty cache line bouncing" which will slow
91 * down each lock to the point where they're actually slower than just
93 * Thanks go to Jens Axboe and Al Viro for their LKML emails explaining this!
95 static spinlock_t nbd_lock = SPIN_LOCK_UNLOCKED;
98 static const char *ioctl_cmd_to_ascii(int cmd)
101 case NBD_SET_SOCK: return "set-sock";
102 case NBD_SET_BLKSIZE: return "set-blksize";
103 case NBD_SET_SIZE: return "set-size";
104 case NBD_DO_IT: return "do-it";
105 case NBD_CLEAR_SOCK: return "clear-sock";
106 case NBD_CLEAR_QUE: return "clear-que";
107 case NBD_PRINT_DEBUG: return "print-debug";
108 case NBD_SET_SIZE_BLOCKS: return "set-size-blocks";
109 case NBD_DISCONNECT: return "disconnect";
110 case BLKROSET: return "set-read-only";
111 case BLKFLSBUF: return "flush-buffer-cache";
116 static const char *nbdcmd_to_ascii(int cmd)
119 case NBD_CMD_READ: return "read";
120 case NBD_CMD_WRITE: return "write";
121 case NBD_CMD_DISC: return "disconnect";
127 static void nbd_end_request(struct request *req)
129 int uptodate = (req->errors == 0) ? 1 : 0;
130 request_queue_t *q = req->q;
131 struct nbd_device *lo = req->rq_disk->private_data;
134 dprintk(DBG_BLKDEV, "%s: request %p: %s\n", req->rq_disk->disk_name,
135 req, uptodate? "done": "failed");
137 spin_lock(&lo->queue_lock);
138 while (req->ref_count > 1) { /* still in send */
139 spin_unlock(&lo->queue_lock);
140 printk(KERN_DEBUG "%s: request %p still in use (%d), waiting\n",
141 lo->disk->disk_name, req, req->ref_count);
142 set_current_state(TASK_UNINTERRUPTIBLE);
143 schedule_timeout(HZ); /* wait a second */
144 spin_lock(&lo->queue_lock);
146 spin_unlock(&lo->queue_lock);
148 spin_lock_irqsave(q->queue_lock, flags);
149 if (!end_that_request_first(req, uptodate, req->nr_sectors)) {
150 end_that_request_last(req);
152 spin_unlock_irqrestore(q->queue_lock, flags);
156 * Send or receive packet.
158 static int sock_xmit(struct socket *sock, int send, void *buf, int size,
170 /* Allow interception of SIGKILL only
171 * Don't allow other signals to interrupt the transmission */
172 spin_lock_irqsave(¤t->sighand->siglock, flags);
173 oldset = current->blocked;
174 sigfillset(¤t->blocked);
175 sigdelsetmask(¤t->blocked, sigmask(SIGKILL));
177 spin_unlock_irqrestore(¤t->sighand->siglock, flags);
180 sock->sk->sk_allocation = GFP_NOIO;
187 msg.msg_control = NULL;
188 msg.msg_controllen = 0;
190 msg.msg_flags = msg_flags | MSG_NOSIGNAL;
193 result = sock_sendmsg(sock, &msg, size);
195 result = sock_recvmsg(sock, &msg, size, 0);
197 if (signal_pending(current)) {
199 spin_lock_irqsave(¤t->sighand->siglock, flags);
200 printk(KERN_WARNING "nbd (pid %d: %s) got signal %d\n",
201 current->pid, current->comm,
202 dequeue_signal(current, ¤t->blocked, &info));
203 spin_unlock_irqrestore(¤t->sighand->siglock, flags);
210 result = -EPIPE; /* short read */
217 spin_lock_irqsave(¤t->sighand->siglock, flags);
218 current->blocked = oldset;
220 spin_unlock_irqrestore(¤t->sighand->siglock, flags);
226 static inline int sock_send_bvec(struct socket *sock, struct bio_vec *bvec,
230 void *kaddr = kmap(bvec->bv_page);
231 result = sock_xmit(sock, 1, kaddr + bvec->bv_offset, bvec->bv_len,
233 kunmap(bvec->bv_page);
237 void nbd_send_req(struct nbd_device *lo, struct request *req)
239 int result, i, flags;
240 struct nbd_request request;
241 unsigned long size = req->nr_sectors << 9;
242 struct socket *sock = lo->sock;
244 request.magic = htonl(NBD_REQUEST_MAGIC);
245 request.type = htonl(nbd_cmd(req));
246 request.from = cpu_to_be64((u64) req->sector << 9);
247 request.len = htonl(size);
248 memcpy(request.handle, &req, sizeof(req));
252 if (!sock || !lo->sock) {
253 printk(KERN_ERR "%s: Attempted send on closed socket\n",
254 lo->disk->disk_name);
258 dprintk(DBG_TX, "%s: request %p: sending control (%s@%llu,%luB)\n",
259 lo->disk->disk_name, req,
260 nbdcmd_to_ascii(nbd_cmd(req)),
261 (unsigned long long)req->sector << 9,
262 req->nr_sectors << 9);
263 result = sock_xmit(sock, 1, &request, sizeof(request),
264 (nbd_cmd(req) == NBD_CMD_WRITE)? MSG_MORE: 0);
266 printk(KERN_ERR "%s: Send control failed (result %d)\n",
267 lo->disk->disk_name, result);
271 if (nbd_cmd(req) == NBD_CMD_WRITE) {
274 * we are really probing at internals to determine
275 * whether to set MSG_MORE or not...
277 rq_for_each_bio(bio, req) {
278 struct bio_vec *bvec;
279 bio_for_each_segment(bvec, bio, i) {
281 if ((i < (bio->bi_vcnt - 1)) || bio->bi_next)
283 dprintk(DBG_TX, "%s: request %p: sending %d bytes data\n",
284 lo->disk->disk_name, req,
286 result = sock_send_bvec(sock, bvec, flags);
288 printk(KERN_ERR "%s: Send data failed (result %d)\n",
304 static struct request *nbd_find_request(struct nbd_device *lo, char *handle)
307 struct list_head *tmp;
308 struct request *xreq;
310 memcpy(&xreq, handle, sizeof(xreq));
312 spin_lock(&lo->queue_lock);
313 list_for_each(tmp, &lo->queue_head) {
314 req = list_entry(tmp, struct request, queuelist);
317 list_del_init(&req->queuelist);
318 spin_unlock(&lo->queue_lock);
321 spin_unlock(&lo->queue_lock);
325 static inline int sock_recv_bvec(struct socket *sock, struct bio_vec *bvec)
328 void *kaddr = kmap(bvec->bv_page);
329 result = sock_xmit(sock, 0, kaddr + bvec->bv_offset, bvec->bv_len,
331 kunmap(bvec->bv_page);
335 /* NULL returned = something went wrong, inform userspace */
336 struct request *nbd_read_stat(struct nbd_device *lo)
339 struct nbd_reply reply;
341 struct socket *sock = lo->sock;
344 result = sock_xmit(sock, 0, &reply, sizeof(reply), MSG_WAITALL);
346 printk(KERN_ERR "%s: Receive control failed (result %d)\n",
347 lo->disk->disk_name, result);
350 req = nbd_find_request(lo, reply.handle);
352 printk(KERN_ERR "%s: Unexpected reply (%p)\n",
353 lo->disk->disk_name, reply.handle);
358 if (ntohl(reply.magic) != NBD_REPLY_MAGIC) {
359 printk(KERN_ERR "%s: Wrong magic (0x%lx)\n",
361 (unsigned long)ntohl(reply.magic));
365 if (ntohl(reply.error)) {
366 printk(KERN_ERR "%s: Other side returned error (%d)\n",
367 lo->disk->disk_name, ntohl(reply.error));
372 dprintk(DBG_RX, "%s: request %p: got reply\n",
373 lo->disk->disk_name, req);
374 if (nbd_cmd(req) == NBD_CMD_READ) {
377 rq_for_each_bio(bio, req) {
378 struct bio_vec *bvec;
379 bio_for_each_segment(bvec, bio, i) {
380 result = sock_recv_bvec(sock, bvec);
382 printk(KERN_ERR "%s: Receive data failed (result %d)\n",
387 dprintk(DBG_RX, "%s: request %p: got %d bytes data\n",
388 lo->disk->disk_name, req, bvec->bv_len);
394 lo->harderror = result;
398 void nbd_do_it(struct nbd_device *lo)
402 BUG_ON(lo->magic != LO_MAGIC);
404 while ((req = nbd_read_stat(lo)) != NULL)
405 nbd_end_request(req);
409 void nbd_clear_que(struct nbd_device *lo)
413 BUG_ON(lo->magic != LO_MAGIC);
417 spin_lock(&lo->queue_lock);
418 if (!list_empty(&lo->queue_head)) {
419 req = list_entry(lo->queue_head.next, struct request, queuelist);
420 list_del_init(&req->queuelist);
422 spin_unlock(&lo->queue_lock);
425 nbd_end_request(req);
431 * We always wait for result of write, for now. It would be nice to make it optional
433 * if ((req->cmd == WRITE) && (lo->flags & NBD_WRITE_NOCHK))
434 * { printk( "Warning: Ignoring result!\n"); nbd_end_request( req ); }
437 static void do_nbd_request(request_queue_t * q)
441 while ((req = elv_next_request(q)) != NULL) {
442 struct nbd_device *lo;
444 blkdev_dequeue_request(req);
445 dprintk(DBG_BLKDEV, "%s: request %p: dequeued (flags=%lx)\n",
446 req->rq_disk->disk_name, req, req->flags);
448 if (!(req->flags & REQ_CMD))
451 lo = req->rq_disk->private_data;
453 BUG_ON(lo->magic != LO_MAGIC);
456 printk(KERN_ERR "%s: Request when not-ready\n",
457 lo->disk->disk_name);
460 nbd_cmd(req) = NBD_CMD_READ;
461 if (rq_data_dir(req) == WRITE) {
462 nbd_cmd(req) = NBD_CMD_WRITE;
463 if (lo->flags & NBD_READ_ONLY) {
464 printk(KERN_ERR "%s: Write on read-only\n",
465 lo->disk->disk_name);
471 spin_unlock_irq(q->queue_lock);
473 spin_lock(&lo->queue_lock);
476 spin_unlock(&lo->queue_lock);
477 printk(KERN_ERR "%s: failed between accept and semaphore, file lost\n",
478 lo->disk->disk_name);
480 nbd_end_request(req);
481 spin_lock_irq(q->queue_lock);
485 list_add(&req->queuelist, &lo->queue_head);
486 req->ref_count++; /* make sure req does not get freed */
487 spin_unlock(&lo->queue_lock);
489 nbd_send_req(lo, req);
492 printk(KERN_ERR "%s: Request send failed\n",
493 lo->disk->disk_name);
494 spin_lock(&lo->queue_lock);
495 list_del_init(&req->queuelist);
497 spin_unlock(&lo->queue_lock);
498 nbd_end_request(req);
499 spin_lock_irq(q->queue_lock);
503 spin_lock(&lo->queue_lock);
505 spin_unlock(&lo->queue_lock);
506 spin_lock_irq(q->queue_lock);
511 spin_unlock(q->queue_lock);
512 nbd_end_request(req);
513 spin_lock(q->queue_lock);
518 static int nbd_ioctl(struct inode *inode, struct file *file,
519 unsigned int cmd, unsigned long arg)
521 struct nbd_device *lo = inode->i_bdev->bd_disk->private_data;
523 struct request sreq ;
525 if (!capable(CAP_SYS_ADMIN))
528 BUG_ON(lo->magic != LO_MAGIC);
530 /* Anyone capable of this syscall can do *real bad* things */
531 dprintk(DBG_IOCTL, "%s: nbd_ioctl cmd=%s(0x%x) arg=%lu\n",
532 lo->disk->disk_name, ioctl_cmd_to_ascii(cmd), cmd, arg);
536 printk(KERN_INFO "%s: NBD_DISCONNECT\n", lo->disk->disk_name);
537 sreq.flags = REQ_SPECIAL;
538 nbd_cmd(&sreq) = NBD_CMD_DISC;
540 * Set these to sane values in case server implementation
541 * fails to check the request type first and also to keep
542 * debugging output cleaner.
548 nbd_send_req(lo, &sreq);
556 spin_lock(&lo->queue_lock);
559 spin_unlock(&lo->queue_lock);
561 spin_lock(&lo->queue_lock);
562 if (!list_empty(&lo->queue_head)) {
563 printk(KERN_ERR "nbd: disconnect: some requests are in progress -> please try again.\n");
566 spin_unlock(&lo->queue_lock);
576 inode = file->f_dentry->d_inode;
579 lo->sock = SOCKET_I(inode);
586 case NBD_SET_BLKSIZE:
588 lo->bytesize &= ~(lo->blksize-1);
589 inode->i_bdev->bd_inode->i_size = lo->bytesize;
590 set_blocksize(inode->i_bdev, lo->blksize);
591 set_capacity(lo->disk, lo->bytesize >> 9);
594 lo->bytesize = arg & ~(lo->blksize-1);
595 inode->i_bdev->bd_inode->i_size = lo->bytesize;
596 set_blocksize(inode->i_bdev, lo->blksize);
597 set_capacity(lo->disk, lo->bytesize >> 9);
599 case NBD_SET_SIZE_BLOCKS:
600 lo->bytesize = ((u64) arg) * lo->blksize;
601 inode->i_bdev->bd_inode->i_size = lo->bytesize;
602 set_blocksize(inode->i_bdev, lo->blksize);
603 set_capacity(lo->disk, lo->bytesize >> 9);
609 /* on return tidy up in case we have a signal */
610 /* Forcibly shutdown the socket causing all listeners
613 * FIXME: This code is duplicated from sys_shutdown, but
614 * there should be a more generic interface rather than
615 * calling socket ops directly here */
618 printk(KERN_WARNING "%s: shutting down socket\n",
619 lo->disk->disk_name);
620 lo->sock->ops->shutdown(lo->sock,
621 SEND_SHUTDOWN|RCV_SHUTDOWN);
625 spin_lock(&lo->queue_lock);
628 spin_unlock(&lo->queue_lock);
630 printk(KERN_WARNING "%s: queue cleared\n", lo->disk->disk_name);
633 return lo->harderror;
638 return 0; /* probably should be error, but that would
639 * break "nbd-client -d", so just return 0 */
644 case NBD_PRINT_DEBUG:
645 printk(KERN_INFO "%s: next = %p, prev = %p, head = %p\n",
646 inode->i_bdev->bd_disk->disk_name,
647 lo->queue_head.next, lo->queue_head.prev,
654 static struct block_device_operations nbd_fops =
656 .owner = THIS_MODULE,
661 * And here should be modules and kernel interface
662 * (Just smiley confuses emacs :-)
665 static int __init nbd_init(void)
670 if (sizeof(struct nbd_request) != 28) {
671 printk(KERN_CRIT "nbd: sizeof nbd_request needs to be 28 in order to work!\n" );
675 for (i = 0; i < MAX_NBD; i++) {
676 struct gendisk *disk = alloc_disk(1);
679 nbd_dev[i].disk = disk;
681 * The new linux 2.5 block layer implementation requires
682 * every gendisk to have its very own request_queue struct.
683 * These structs are big so we dynamically allocate them.
685 disk->queue = blk_init_queue(do_nbd_request, &nbd_lock);
692 if (register_blkdev(NBD_MAJOR, "nbd")) {
697 printk(KERN_INFO "nbd: registered device at major %d\n", NBD_MAJOR);
698 dprintk(DBG_INIT, "nbd: debugflags=0x%x\n", debugflags);
701 for (i = 0; i < MAX_NBD; i++) {
702 struct gendisk *disk = nbd_dev[i].disk;
703 nbd_dev[i].file = NULL;
704 nbd_dev[i].magic = LO_MAGIC;
705 nbd_dev[i].flags = 0;
706 spin_lock_init(&nbd_dev[i].queue_lock);
707 INIT_LIST_HEAD(&nbd_dev[i].queue_head);
708 init_MUTEX(&nbd_dev[i].tx_lock);
709 nbd_dev[i].blksize = 1024;
710 nbd_dev[i].bytesize = 0x7ffffc00ULL << 10; /* 2TB */
711 disk->major = NBD_MAJOR;
712 disk->first_minor = i;
713 disk->fops = &nbd_fops;
714 disk->private_data = &nbd_dev[i];
715 disk->flags |= GENHD_FL_SUPPRESS_PARTITION_INFO;
716 sprintf(disk->disk_name, "nbd%d", i);
717 sprintf(disk->devfs_name, "nbd/%d", i);
718 set_capacity(disk, 0x7ffffc00ULL << 1); /* 2 TB */
725 blk_cleanup_queue(nbd_dev[i].disk->queue);
726 put_disk(nbd_dev[i].disk);
731 static void __exit nbd_cleanup(void)
734 for (i = 0; i < MAX_NBD; i++) {
735 struct gendisk *disk = nbd_dev[i].disk;
738 blk_cleanup_queue(disk->queue);
743 unregister_blkdev(NBD_MAJOR, "nbd");
744 printk(KERN_INFO "nbd: unregistered device at major %d\n", NBD_MAJOR);
747 module_init(nbd_init);
748 module_exit(nbd_cleanup);
750 MODULE_DESCRIPTION("Network Block Device");
751 MODULE_LICENSE("GPL");
754 MODULE_PARM(debugflags, "i");
755 MODULE_PARM_DESC(debugflags, "flags for controlling debug output");