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 Pavel Machek <pavel@atrey.karlin.mff.cuni.cz>
9 * (part of code stolen from loop.c)
11 * 97-3-25 compiled 0-th version, not yet tested it
12 * (it did not work, BTW) (later that day) HEY! it works!
13 * (bit later) hmm, not that much... 2:00am next day:
14 * yes, it works, but it gives something like 50kB/sec
15 * 97-4-01 complete rewrite to make it possible for many requests at
16 * once to be processed
17 * 97-4-11 Making protocol independent of endianity etc.
18 * 97-9-13 Cosmetic changes
19 * 98-5-13 Attempt to make 64-bit-clean on 64-bit machines
20 * 99-1-11 Attempt to make 64-bit-clean on 32-bit machines <ankry@mif.pg.gda.pl>
22 * possible FIXME: make set_sock / set_blksize / set_size / do_it one syscall
23 * why not: would need verify_area and friends, would share yet another
24 * structure with userland
28 #include <linux/major.h>
30 #include <linux/module.h>
32 #include <linux/sched.h>
34 #include <linux/stat.h>
35 #include <linux/errno.h>
36 #include <linux/file.h>
37 #include <linux/ioctl.h>
40 #include <asm/segment.h>
41 #include <asm/uaccess.h>
42 #include <asm/types.h>
44 #define MAJOR_NR NBD_MAJOR
45 #include <linux/nbd.h>
47 #define LO_MAGIC 0x68797548
49 static int nbd_blksizes
[MAX_NBD
];
50 static int nbd_blksize_bits
[MAX_NBD
];
51 static int nbd_sizes
[MAX_NBD
];
52 static u64 nbd_bytesizes
[MAX_NBD
];
54 static struct nbd_device nbd_dev
[MAX_NBD
];
57 /* #define DEBUG( s ) printk( s )
61 static int requests_in
;
62 static int requests_out
;
65 static int nbd_open(struct inode
*inode
, struct file
*file
)
68 struct nbd_device
*nbdev
;
72 dev
= MINOR(inode
->i_rdev
);
76 nbdev
= &nbd_dev
[dev
];
77 nbd_dev
[dev
].refcnt
++;
78 if (!(nbdev
->flags
& NBD_INITIALISED
)) {
79 init_MUTEX(&nbdev
->queue_lock
);
80 INIT_LIST_HEAD(&nbdev
->queue_head
);
81 nbdev
->flags
|= NBD_INITIALISED
;
88 * Send or receive packet.
90 static int nbd_xmit(int send
, struct socket
*sock
, char *buf
, int size
)
102 spin_lock_irqsave(¤t
->sigmask_lock
, flags
);
103 oldset
= current
->blocked
;
104 sigfillset(¤t
->blocked
);
105 recalc_sigpending(current
);
106 spin_unlock_irqrestore(¤t
->sigmask_lock
, flags
);
110 sock
->sk
->allocation
= GFP_ATOMIC
;
117 msg
.msg_control
= NULL
;
118 msg
.msg_controllen
= 0;
123 result
= sock_sendmsg(sock
, &msg
, size
);
125 result
= sock_recvmsg(sock
, &msg
, size
, 0);
129 printk(KERN_ERR
"NBD: %s - sock=%ld at buf=%ld, size=%d returned %d.\n",
130 send
? "send" : "receive", (long) sock
, (long) buf
, size
, result
);
138 spin_lock_irqsave(¤t
->sigmask_lock
, flags
);
139 current
->blocked
= oldset
;
140 recalc_sigpending(current
);
141 spin_unlock_irqrestore(¤t
->sigmask_lock
, flags
);
147 #define FAIL( s ) { printk( KERN_ERR "NBD: " s "(result %d)\n", result ); goto error_out; }
149 void nbd_send_req(struct socket
*sock
, struct request
*req
)
152 struct nbd_request request
;
154 DEBUG("NBD: sending control, ");
155 request
.magic
= htonl(NBD_REQUEST_MAGIC
);
156 request
.type
= htonl(req
->cmd
);
157 request
.from
= cpu_to_be64( (u64
) req
->sector
<< 9);
158 request
.len
= htonl(req
->current_nr_sectors
<< 9);
159 memcpy(request
.handle
, &req
, sizeof(req
));
161 result
= nbd_xmit(1, sock
, (char *) &request
, sizeof(request
));
163 FAIL("Sendmsg failed for control.");
165 if (req
->cmd
== WRITE
) {
167 result
= nbd_xmit(1, sock
, req
->buffer
, req
->current_nr_sectors
<< 9);
169 FAIL("Send data failed.");
177 #define HARDFAIL( s ) { printk( KERN_ERR "NBD: " s "(result %d)\n", result ); lo->harderror = result; return NULL; }
178 struct request
*nbd_read_stat(struct nbd_device
*lo
)
179 /* NULL returned = something went wrong, inform userspace */
182 struct nbd_reply reply
;
183 struct request
*xreq
, *req
;
185 DEBUG("reading control, ");
187 result
= nbd_xmit(0, lo
->sock
, (char *) &reply
, sizeof(reply
));
189 HARDFAIL("Recv control failed.");
190 memcpy(&xreq
, reply
.handle
, sizeof(xreq
));
191 req
= blkdev_entry_prev_request(&lo
->queue_head
);
194 FAIL("Unexpected handle received.\n");
197 if (ntohl(reply
.magic
) != NBD_REPLY_MAGIC
)
198 HARDFAIL("Not enough magic.");
199 if (ntohl(reply
.error
))
200 FAIL("Other side returned error.");
201 if (req
->cmd
== READ
) {
203 result
= nbd_xmit(0, lo
->sock
, req
->buffer
, req
->current_nr_sectors
<< 9);
205 HARDFAIL("Recv data failed.");
210 /* Can we get here? Yes, if other side returns error */
216 void nbd_do_it(struct nbd_device
*lo
)
220 down (&lo
->queue_lock
);
221 while (!list_empty(&lo
->queue_head
)) {
222 req
= nbd_read_stat(lo
);
226 if (req
!= blkdev_entry_prev_request(&lo
->queue_head
)) {
227 printk(KERN_ALERT
"NBD: I have problem...\n");
229 if (lo
!= &nbd_dev
[MINOR(req
->rq_dev
)]) {
230 printk(KERN_ALERT
"NBD: request corrupted!\n");
233 if (lo
->magic
!= LO_MAGIC
) {
234 printk(KERN_ALERT
"NBD: nbd_dev[] corrupted: Not enough magic\n");
238 list_del(&req
->queue
);
239 up (&lo
->queue_lock
);
241 nbd_end_request(req
);
243 down (&lo
->queue_lock
);
246 up (&lo
->queue_lock
);
249 void nbd_clear_que(struct nbd_device
*lo
)
253 while (!list_empty(&lo
->queue_head
)) {
254 req
= blkdev_entry_prev_request(&lo
->queue_head
);
256 if (lo
!= &nbd_dev
[MINOR(req
->rq_dev
)]) {
257 printk(KERN_ALERT
"NBD: request corrupted when clearing!\n");
260 if (lo
->magic
!= LO_MAGIC
) {
261 printk(KERN_ERR
"NBD: nbd_dev[] corrupted: Not enough magic when clearing!\n");
266 list_del(&req
->queue
);
269 nbd_end_request(req
);
271 down(&lo
->queue_lock
);
276 * We always wait for result of write, for now. It would be nice to make it optional
278 * if ((req->cmd == WRITE) && (lo->flags & NBD_WRITE_NOCHK))
279 * { printk( "Warning: Ignoring result!\n"); nbd_end_request( req ); }
283 #define FAIL( s ) { printk( KERN_ERR "NBD, minor %d: " s "\n", dev ); goto error_out; }
285 static void do_nbd_request(request_queue_t
* q
)
289 struct nbd_device
*lo
;
291 while (!QUEUE_EMPTY
) {
293 dev
= MINOR(req
->rq_dev
);
296 FAIL("Minor too big."); /* Probably can not happen */
300 FAIL("Request when not-ready.");
301 if ((req
->cmd
== WRITE
) && (lo
->flags
& NBD_READ_ONLY
))
302 FAIL("Write on read-only");
304 if (lo
->magic
!= LO_MAGIC
)
305 FAIL("nbd[] is not magical!");
309 blkdev_dequeue_request(req
);
310 spin_unlock_irq(&io_request_lock
);
312 down (&lo
->queue_lock
);
313 list_add(&req
->queue
, &lo
->queue_head
);
314 nbd_send_req(lo
->sock
, req
); /* Why does this block? */
315 up (&lo
->queue_lock
);
317 spin_lock_irq(&io_request_lock
);
322 blkdev_dequeue_request(req
);
323 spin_unlock(&io_request_lock
);
324 nbd_end_request(req
);
325 spin_lock(&io_request_lock
);
330 static int nbd_ioctl(struct inode
*inode
, struct file
*file
,
331 unsigned int cmd
, unsigned long arg
)
333 struct nbd_device
*lo
;
334 int dev
, error
, temp
;
336 /* Anyone capable of this syscall can do *real bad* things */
338 if (!capable(CAP_SYS_ADMIN
))
342 dev
= MINOR(inode
->i_rdev
);
349 down(&lo
->queue_lock
);
351 if (!list_empty(&lo
->queue_head
)) {
353 printk(KERN_ERR
"nbd: Some requests are in progress -> can not turn off.\n");
370 inode
= file
->f_dentry
->d_inode
;
371 /* N.B. Should verify that it's a socket */
373 lo
->sock
= &inode
->u
.socket_i
;
377 case NBD_SET_BLKSIZE
:
378 if ((arg
& (arg
-1)) || (arg
< 512) || (arg
> PAGE_SIZE
))
380 nbd_blksizes
[dev
] = arg
;
382 nbd_blksize_bits
[dev
] = 9;
384 nbd_blksize_bits
[dev
]++;
387 nbd_sizes
[dev
] = nbd_bytesizes
[dev
] >> nbd_blksize_bits
[dev
];
388 nbd_bytesizes
[dev
] = nbd_sizes
[dev
] << nbd_blksize_bits
[dev
];
391 nbd_sizes
[dev
] = arg
>> nbd_blksize_bits
[dev
];
392 nbd_bytesizes
[dev
] = nbd_sizes
[dev
] << nbd_blksize_bits
[dev
];
394 case NBD_SET_SIZE_BLOCKS
:
395 nbd_sizes
[dev
] = arg
;
396 nbd_bytesizes
[dev
] = ((u64
) arg
) << nbd_blksize_bits
[dev
];
402 return lo
->harderror
;
407 case NBD_PRINT_DEBUG
:
408 printk(KERN_INFO
"NBD device %d: next = %p, prev = %p. Global: in %d, out %d\n",
409 dev
, lo
->queue_head
.next
, lo
->queue_head
.prev
, requests_in
, requests_out
);
413 return put_user(nbd_bytesizes
[dev
] >> 9, (long *) arg
);
418 static int nbd_release(struct inode
*inode
, struct file
*file
)
420 struct nbd_device
*lo
;
425 dev
= MINOR(inode
->i_rdev
);
430 printk(KERN_ALERT
"nbd_release: refcount(%d) <= 0\n", lo
->refcnt
);
432 /* N.B. Doesn't lo->file need an fput?? */
437 static struct block_device_operations nbd_fops
=
440 release
: nbd_release
,
445 * And here should be modules and kernel interface
446 * (Just smiley confuses emacs :-)
450 #define nbd_init init_module
457 if (sizeof(struct nbd_request
) != 28) {
458 printk(KERN_CRIT
"Sizeof nbd_request needs to be 28 in order to work!\n" );
462 if (register_blkdev(MAJOR_NR
, "nbd", &nbd_fops
)) {
463 printk("Unable to get major number %d for NBD\n",
468 printk("nbd: registered device at major %d\n", MAJOR_NR
);
470 blksize_size
[MAJOR_NR
] = nbd_blksizes
;
471 blk_size
[MAJOR_NR
] = nbd_sizes
;
472 blk_init_queue(BLK_DEFAULT_QUEUE(MAJOR_NR
), do_nbd_request
);
473 blk_queue_headactive(BLK_DEFAULT_QUEUE(MAJOR_NR
), 0);
474 for (i
= 0; i
< MAX_NBD
; i
++) {
475 nbd_dev
[i
].refcnt
= 0;
476 nbd_dev
[i
].file
= NULL
;
477 nbd_dev
[i
].magic
= LO_MAGIC
;
478 nbd_dev
[i
].flags
= 0;
479 nbd_blksizes
[i
] = 1024;
480 nbd_blksize_bits
[i
] = 10;
481 nbd_bytesizes
[i
] = 0x7ffffc00; /* 2GB */
482 nbd_sizes
[i
] = nbd_bytesizes
[i
] >> nbd_blksize_bits
[i
];
483 register_disk(NULL
, MKDEV(MAJOR_NR
,i
), 1, &nbd_fops
,
484 nbd_bytesizes
[i
]>>9);
490 void cleanup_module(void)
492 if (unregister_blkdev(MAJOR_NR
, "nbd") != 0)
493 printk("nbd: cleanup_module failed\n");
495 printk("nbd: module cleaned up.\n");