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>
35 * possible FIXME: make set_sock / set_blksize / set_size / do_it one syscall
36 * why not: would need verify_area and friends, would share yet another
37 * structure with userland
41 #include <linux/major.h>
43 #include <linux/blk.h>
44 #include <linux/blkdev.h>
45 #include <linux/module.h>
46 #include <linux/init.h>
47 #include <linux/sched.h>
49 #include <linux/bio.h>
50 #include <linux/stat.h>
51 #include <linux/errno.h>
52 #include <linux/file.h>
53 #include <linux/ioctl.h>
54 #include <linux/blkdev.h>
55 #include <linux/blk.h>
58 #include <linux/devfs_fs_kernel.h>
60 #include <asm/uaccess.h>
61 #include <asm/types.h>
63 #include <linux/nbd.h>
65 #define LO_MAGIC 0x68797548
67 static struct nbd_device nbd_dev
[MAX_NBD
];
70 * Use just one lock (or at most 1 per NIC). Two arguments for this:
71 * 1. Each NIC is essentially a synchronization point for all servers
72 * accessed through that NIC so there's no need to have more locks
74 * 2. More locks lead to more "Dirty cache line bouncing" which will slow
75 * down each lock to the point where they're actually slower than just
77 * Thanks go to Jens Axboe and Al Viro for their LKML emails explaining this!
79 static spinlock_t nbd_lock
= SPIN_LOCK_UNLOCKED
;
82 /* #define DEBUG( s ) printk( s )
85 static int requests_in
;
86 static int requests_out
;
88 static void nbd_end_request(struct request
*req
)
90 int uptodate
= (req
->errors
== 0) ? 1 : 0;
91 request_queue_t
*q
= req
->q
;
97 spin_lock_irqsave(q
->queue_lock
, flags
);
98 if (!end_that_request_first(req
, uptodate
, req
->nr_sectors
)) {
99 end_that_request_last(req
);
101 spin_unlock_irqrestore(q
->queue_lock
, flags
);
104 static int nbd_open(struct inode
*inode
, struct file
*file
)
106 struct nbd_device
*lo
= inode
->i_bdev
->bd_disk
->private_data
;
112 * Send or receive packet.
114 static int nbd_xmit(int send
, struct socket
*sock
, char *buf
, int size
, int msg_flags
)
125 /* Allow interception of SIGKILL only
126 * Don't allow other signals to interrupt the transmission */
127 spin_lock_irqsave(¤t
->sighand
->siglock
, flags
);
128 oldset
= current
->blocked
;
129 sigfillset(¤t
->blocked
);
130 sigdelsetmask(¤t
->blocked
, sigmask(SIGKILL
));
132 spin_unlock_irqrestore(¤t
->sighand
->siglock
, flags
);
136 sock
->sk
->sk_allocation
= GFP_NOIO
;
143 msg
.msg_control
= NULL
;
144 msg
.msg_controllen
= 0;
146 msg
.msg_flags
= msg_flags
| MSG_NOSIGNAL
;
149 result
= sock_sendmsg(sock
, &msg
, size
);
151 result
= sock_recvmsg(sock
, &msg
, size
, 0);
153 if (signal_pending(current
)) {
155 spin_lock_irqsave(¤t
->sighand
->siglock
, flags
);
156 printk(KERN_WARNING
"NBD (pid %d: %s) got signal %d\n",
157 current
->pid
, current
->comm
,
158 dequeue_signal(current
, ¤t
->blocked
, &info
));
159 spin_unlock_irqrestore(¤t
->sighand
->siglock
, flags
);
166 printk(KERN_ERR
"NBD: %s - sock=%ld at buf=%ld, size=%d returned %d.\n",
167 send
? "send" : "receive", (long) sock
, (long) buf
, size
, result
);
175 spin_lock_irqsave(¤t
->sighand
->siglock
, flags
);
176 current
->blocked
= oldset
;
178 spin_unlock_irqrestore(¤t
->sighand
->siglock
, flags
);
184 static inline int sock_send_bvec(struct socket
*sock
, struct bio_vec
*bvec
,
188 void *kaddr
= kmap(bvec
->bv_page
);
189 result
= nbd_xmit(1, sock
, kaddr
+ bvec
->bv_offset
, bvec
->bv_len
,
191 kunmap(bvec
->bv_page
);
195 #define FAIL( s ) { printk( KERN_ERR "NBD: " s "(result %d)\n", result ); goto error_out; }
197 void nbd_send_req(struct nbd_device
*lo
, struct request
*req
)
199 int result
, i
, flags
;
200 struct nbd_request request
;
201 unsigned long size
= req
->nr_sectors
<< 9;
202 struct socket
*sock
= lo
->sock
;
204 DEBUG("NBD: sending control, ");
206 request
.magic
= htonl(NBD_REQUEST_MAGIC
);
207 request
.type
= htonl(nbd_cmd(req
));
208 request
.from
= cpu_to_be64( (u64
) req
->sector
<< 9);
209 request
.len
= htonl(size
);
210 memcpy(request
.handle
, &req
, sizeof(req
));
214 if (!sock
|| !lo
->sock
) {
215 printk(KERN_ERR
"NBD: Attempted sendmsg to closed socket\n");
219 result
= nbd_xmit(1, sock
, (char *) &request
, sizeof(request
), nbd_cmd(req
) == NBD_CMD_WRITE
? MSG_MORE
: 0);
221 FAIL("Sendmsg failed for control.");
223 if (nbd_cmd(req
) == NBD_CMD_WRITE
) {
226 * we are really probing at internals to determine
227 * whether to set MSG_MORE or not...
229 rq_for_each_bio(bio
, req
) {
230 struct bio_vec
*bvec
;
231 bio_for_each_segment(bvec
, bio
, i
) {
233 if ((i
< (bio
->bi_vcnt
- 1)) || bio
->bi_next
)
236 result
= sock_send_bvec(sock
, bvec
, flags
);
238 FAIL("Send data failed.");
250 static struct request
*nbd_find_request(struct nbd_device
*lo
, char *handle
)
253 struct list_head
*tmp
;
254 struct request
*xreq
;
256 memcpy(&xreq
, handle
, sizeof(xreq
));
258 spin_lock(&lo
->queue_lock
);
259 list_for_each(tmp
, &lo
->queue_head
) {
260 req
= list_entry(tmp
, struct request
, queuelist
);
263 list_del_init(&req
->queuelist
);
264 spin_unlock(&lo
->queue_lock
);
267 spin_unlock(&lo
->queue_lock
);
271 static inline int sock_recv_bvec(struct socket
*sock
, struct bio_vec
*bvec
)
274 void *kaddr
= kmap(bvec
->bv_page
);
275 result
= nbd_xmit(0, sock
, kaddr
+ bvec
->bv_offset
, bvec
->bv_len
,
277 kunmap(bvec
->bv_page
);
281 #define HARDFAIL( s ) { printk( KERN_ERR "NBD: " s "(result %d)\n", result ); lo->harderror = result; return NULL; }
282 struct request
*nbd_read_stat(struct nbd_device
*lo
)
283 /* NULL returned = something went wrong, inform userspace */
286 struct nbd_reply reply
;
288 struct socket
*sock
= lo
->sock
;
290 DEBUG("reading control, ");
292 result
= nbd_xmit(0, sock
, (char *) &reply
, sizeof(reply
), MSG_WAITALL
);
294 HARDFAIL("Recv control failed.");
295 req
= nbd_find_request(lo
, reply
.handle
);
297 HARDFAIL("Unexpected reply");
300 if (ntohl(reply
.magic
) != NBD_REPLY_MAGIC
)
301 HARDFAIL("Not enough magic.");
302 if (ntohl(reply
.error
))
303 FAIL("Other side returned error.");
305 if (nbd_cmd(req
) == NBD_CMD_READ
) {
309 rq_for_each_bio(bio
, req
) {
310 struct bio_vec
*bvec
;
311 bio_for_each_segment(bvec
, bio
, i
) {
312 result
= sock_recv_bvec(sock
, bvec
);
314 HARDFAIL("Recv data failed.");
321 /* Can we get here? Yes, if other side returns error */
327 void nbd_do_it(struct nbd_device
*lo
)
332 req
= nbd_read_stat(lo
);
335 printk(KERN_ALERT
"req should never be null\n" );
338 BUG_ON(lo
->magic
!= LO_MAGIC
);
339 nbd_end_request(req
);
345 void nbd_clear_que(struct nbd_device
*lo
)
349 BUG_ON(lo
->magic
!= LO_MAGIC
);
353 spin_lock(&lo
->queue_lock
);
354 if (!list_empty(&lo
->queue_head
)) {
355 req
= list_entry(lo
->queue_head
.next
, struct request
, queuelist
);
356 list_del_init(&req
->queuelist
);
358 spin_unlock(&lo
->queue_lock
);
361 nbd_end_request(req
);
367 * We always wait for result of write, for now. It would be nice to make it optional
369 * if ((req->cmd == WRITE) && (lo->flags & NBD_WRITE_NOCHK))
370 * { printk( "Warning: Ignoring result!\n"); nbd_end_request( req ); }
374 #define FAIL( s ) { printk( KERN_ERR "%s: " s "\n", req->rq_disk->disk_name ); goto error_out; }
376 static void do_nbd_request(request_queue_t
* q
)
380 while ((req
= elv_next_request(q
)) != NULL
) {
381 struct nbd_device
*lo
;
383 if (!(req
->flags
& REQ_CMD
))
386 lo
= req
->rq_disk
->private_data
;
388 FAIL("Request when not-ready.");
389 nbd_cmd(req
) = NBD_CMD_READ
;
390 if (rq_data_dir(req
) == WRITE
) {
391 nbd_cmd(req
) = NBD_CMD_WRITE
;
392 if (lo
->flags
& NBD_READ_ONLY
)
393 FAIL("Write on read-only");
395 BUG_ON(lo
->magic
!= LO_MAGIC
);
399 blkdev_dequeue_request(req
);
400 spin_unlock_irq(q
->queue_lock
);
402 spin_lock(&lo
->queue_lock
);
405 spin_unlock(&lo
->queue_lock
);
406 printk(KERN_ERR
"nbd: failed between accept and semaphore, file lost\n");
408 nbd_end_request(req
);
409 spin_lock_irq(q
->queue_lock
);
413 list_add(&req
->queuelist
, &lo
->queue_head
);
414 spin_unlock(&lo
->queue_lock
);
416 nbd_send_req(lo
, req
);
419 printk(KERN_ERR
"nbd: nbd_send_req failed\n");
420 spin_lock(&lo
->queue_lock
);
421 list_del_init(&req
->queuelist
);
422 spin_unlock(&lo
->queue_lock
);
423 nbd_end_request(req
);
424 spin_lock_irq(q
->queue_lock
);
428 spin_lock_irq(q
->queue_lock
);
433 blkdev_dequeue_request(req
);
434 spin_unlock(q
->queue_lock
);
435 nbd_end_request(req
);
436 spin_lock(q
->queue_lock
);
441 static int nbd_ioctl(struct inode
*inode
, struct file
*file
,
442 unsigned int cmd
, unsigned long arg
)
444 struct nbd_device
*lo
= inode
->i_bdev
->bd_disk
->private_data
;
446 struct request sreq
;
448 /* Anyone capable of this syscall can do *real bad* things */
450 if (!capable(CAP_SYS_ADMIN
))
454 printk(KERN_INFO
"NBD_DISCONNECT\n");
455 sreq
.flags
= REQ_SPECIAL
;
456 nbd_cmd(&sreq
) = NBD_CMD_DISC
;
459 nbd_send_req(lo
, &sreq
);
464 spin_lock(&lo
->queue_lock
);
465 if (!list_empty(&lo
->queue_head
)) {
466 spin_unlock(&lo
->queue_lock
);
467 printk(KERN_ERR
"nbd: Some requests are in progress -> can not turn off.\n");
472 spin_unlock(&lo
->queue_lock
);
477 spin_unlock(&lo
->queue_lock
);
486 inode
= file
->f_dentry
->d_inode
;
489 lo
->sock
= SOCKET_I(inode
);
496 case NBD_SET_BLKSIZE
:
497 if ((arg
& (arg
-1)) || (arg
< 512) || (arg
> PAGE_SIZE
))
501 lo
->blksize_bits
= 9;
506 lo
->bytesize
&= ~(lo
->blksize
-1);
507 set_capacity(lo
->disk
, lo
->bytesize
>> 9);
510 lo
->bytesize
= arg
& ~(lo
->blksize
-1);
511 set_capacity(lo
->disk
, lo
->bytesize
>> 9);
513 case NBD_SET_SIZE_BLOCKS
:
514 lo
->bytesize
= ((u64
) arg
) << lo
->blksize_bits
;
515 set_capacity(lo
->disk
, lo
->bytesize
>> 9);
521 /* on return tidy up in case we have a signal */
522 /* Forcibly shutdown the socket causing all listeners
525 * FIXME: This code is duplicated from sys_shutdown, but
526 * there should be a more generic interface rather than
527 * calling socket ops directly here */
529 printk(KERN_WARNING
"nbd: shutting down socket\n");
530 lo
->sock
->ops
->shutdown(lo
->sock
, SEND_SHUTDOWN
|RCV_SHUTDOWN
);
533 spin_lock(&lo
->queue_lock
);
536 spin_unlock(&lo
->queue_lock
);
538 printk(KERN_WARNING
"nbd: queue cleared\n");
541 return lo
->harderror
;
546 case NBD_PRINT_DEBUG
:
547 printk(KERN_INFO
"%s: next = %p, prev = %p. Global: in %d, out %d\n",
548 inode
->i_bdev
->bd_disk
->disk_name
, lo
->queue_head
.next
,
549 lo
->queue_head
.prev
, requests_in
, requests_out
);
556 static int nbd_release(struct inode
*inode
, struct file
*file
)
558 struct nbd_device
*lo
= inode
->i_bdev
->bd_disk
->private_data
;
560 printk(KERN_ALERT
"nbd_release: refcount(%d) <= 0\n", lo
->refcnt
);
562 /* N.B. Doesn't lo->file need an fput?? */
566 static struct block_device_operations nbd_fops
=
568 .owner
= THIS_MODULE
,
570 .release
= nbd_release
,
575 * And here should be modules and kernel interface
576 * (Just smiley confuses emacs :-)
579 static int __init
nbd_init(void)
584 if (sizeof(struct nbd_request
) != 28) {
585 printk(KERN_CRIT
"Sizeof nbd_request needs to be 28 in order to work!\n" );
589 for (i
= 0; i
< MAX_NBD
; i
++) {
590 struct gendisk
*disk
= alloc_disk(1);
593 nbd_dev
[i
].disk
= disk
;
595 * The new linux 2.5 block layer implementation requires
596 * every gendisk to have its very own request_queue struct.
597 * These structs are big so we dynamically allocate them.
599 disk
->queue
= kmalloc(sizeof(struct request_queue
), GFP_KERNEL
);
604 blk_init_queue(disk
->queue
, do_nbd_request
, &nbd_lock
);
607 if (register_blkdev(NBD_MAJOR
, "nbd")) {
612 printk("nbd: registered device at major %d\n", NBD_MAJOR
);
615 for (i
= 0; i
< MAX_NBD
; i
++) {
616 struct gendisk
*disk
= nbd_dev
[i
].disk
;
617 nbd_dev
[i
].refcnt
= 0;
618 nbd_dev
[i
].file
= NULL
;
619 nbd_dev
[i
].magic
= LO_MAGIC
;
620 nbd_dev
[i
].flags
= 0;
621 spin_lock_init(&nbd_dev
[i
].queue_lock
);
622 INIT_LIST_HEAD(&nbd_dev
[i
].queue_head
);
623 init_MUTEX(&nbd_dev
[i
].tx_lock
);
624 nbd_dev
[i
].blksize
= 1024;
625 nbd_dev
[i
].blksize_bits
= 10;
626 nbd_dev
[i
].bytesize
= ((u64
)0x7ffffc00) << 10; /* 2TB */
627 disk
->major
= NBD_MAJOR
;
628 disk
->first_minor
= i
;
629 disk
->fops
= &nbd_fops
;
630 disk
->private_data
= &nbd_dev
[i
];
631 sprintf(disk
->disk_name
, "nbd%d", i
);
632 sprintf(disk
->devfs_name
, "nbd/%d", i
);
633 set_capacity(disk
, 0x3ffffe);
640 kfree(nbd_dev
[i
].disk
->queue
);
641 put_disk(nbd_dev
[i
].disk
);
646 static void __exit
nbd_cleanup(void)
649 for (i
= 0; i
< MAX_NBD
; i
++) {
650 struct gendisk
*disk
= nbd_dev
[i
].disk
;
653 blk_cleanup_queue(disk
->queue
);
662 unregister_blkdev(NBD_MAJOR
, "nbd");
664 printk("nbd: unregistered device at major %d\n", NBD_MAJOR
);
668 module_init(nbd_init
);
669 module_exit(nbd_cleanup
);
671 MODULE_DESCRIPTION("Network Block Device");
672 MODULE_LICENSE("GPL");