Merge master.kernel.org:/home/rmk/linux-2.6-serial
[pandora-kernel.git] / drivers / block / nbd.c
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";
107         case NBD_SET_BLKSIZE: return "set-blksize";
108         case NBD_SET_SIZE: return "set-size";
109         case NBD_DO_IT: return "do-it";
110         case NBD_CLEAR_SOCK: return "clear-sock";
111         case NBD_CLEAR_QUE: return "clear-que";
112         case NBD_PRINT_DEBUG: return "print-debug";
113         case NBD_SET_SIZE_BLOCKS: return "set-size-blocks";
114         case NBD_DISCONNECT: return "disconnect";
115         case BLKROSET: return "set-read-only";
116         case BLKFLSBUF: 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";
125         case NBD_CMD_WRITE: return "write";
126         case  NBD_CMD_DISC: 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;
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,
139                         req, uptodate? "done": "failed");
140
141         spin_lock_irqsave(q->queue_lock, flags);
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);
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);
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);
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_namelen = 0;
178                 msg.msg_flags = msg_flags | MSG_NOSIGNAL;
179
180                 if (send)
181                         result = kernel_sendmsg(sock, &msg, &iov, 1, size);
182                 else
183                         result = kernel_recvmsg(sock, &msg, &iov, 1, size, 0);
184
185                 if (signal_pending(current)) {
186                         siginfo_t info;
187                         spin_lock_irqsave(&current->sighand->siglock, flags);
188                         printk(KERN_WARNING "nbd (pid %d: %s) got signal %d\n",
189                                 current->pid, current->comm, 
190                                 dequeue_signal(current, &current->blocked, &info));
191                         spin_unlock_irqrestore(&current->sighand->siglock, flags);
192                         result = -EINTR;
193                         break;
194                 }
195
196                 if (result <= 0) {
197                         if (result == 0)
198                                 result = -EPIPE; /* short read */
199                         break;
200                 }
201                 size -= result;
202                 buf += result;
203         } while (size > 0);
204
205         spin_lock_irqsave(&current->sighand->siglock, flags);
206         current->blocked = oldset;
207         recalc_sigpending();
208         spin_unlock_irqrestore(&current->sighand->siglock, flags);
209
210         return result;
211 }
212
213 static inline int sock_send_bvec(struct socket *sock, struct bio_vec *bvec,
214                 int flags)
215 {
216         int result;
217         void *kaddr = kmap(bvec->bv_page);
218         result = sock_xmit(sock, 1, kaddr + bvec->bv_offset, bvec->bv_len,
219                         flags);
220         kunmap(bvec->bv_page);
221         return result;
222 }
223
224 static int nbd_send_req(struct nbd_device *lo, struct request *req)
225 {
226         int result, i, flags;
227         struct nbd_request request;
228         unsigned long size = req->nr_sectors << 9;
229         struct socket *sock = lo->sock;
230
231         request.magic = htonl(NBD_REQUEST_MAGIC);
232         request.type = htonl(nbd_cmd(req));
233         request.from = cpu_to_be64((u64) req->sector << 9);
234         request.len = htonl(size);
235         memcpy(request.handle, &req, sizeof(req));
236
237         dprintk(DBG_TX, "%s: request %p: sending control (%s@%llu,%luB)\n",
238                         lo->disk->disk_name, req,
239                         nbdcmd_to_ascii(nbd_cmd(req)),
240                         (unsigned long long)req->sector << 9,
241                         req->nr_sectors << 9);
242         result = sock_xmit(sock, 1, &request, sizeof(request),
243                         (nbd_cmd(req) == NBD_CMD_WRITE)? MSG_MORE: 0);
244         if (result <= 0) {
245                 printk(KERN_ERR "%s: Send control failed (result %d)\n",
246                                 lo->disk->disk_name, result);
247                 goto error_out;
248         }
249
250         if (nbd_cmd(req) == NBD_CMD_WRITE) {
251                 struct bio *bio;
252                 /*
253                  * we are really probing at internals to determine
254                  * whether to set MSG_MORE or not...
255                  */
256                 rq_for_each_bio(bio, req) {
257                         struct bio_vec *bvec;
258                         bio_for_each_segment(bvec, bio, i) {
259                                 flags = 0;
260                                 if ((i < (bio->bi_vcnt - 1)) || bio->bi_next)
261                                         flags = MSG_MORE;
262                                 dprintk(DBG_TX, "%s: request %p: sending %d bytes data\n",
263                                                 lo->disk->disk_name, req,
264                                                 bvec->bv_len);
265                                 result = sock_send_bvec(sock, bvec, flags);
266                                 if (result <= 0) {
267                                         printk(KERN_ERR "%s: Send data failed (result %d)\n",
268                                                         lo->disk->disk_name,
269                                                         result);
270                                         goto error_out;
271                                 }
272                         }
273                 }
274         }
275         return 0;
276
277 error_out:
278         return 1;
279 }
280
281 static struct request *nbd_find_request(struct nbd_device *lo, char *handle)
282 {
283         struct request *req;
284         struct list_head *tmp;
285         struct request *xreq;
286         int err;
287
288         memcpy(&xreq, handle, sizeof(xreq));
289
290         err = wait_event_interruptible(lo->active_wq, lo->active_req != xreq);
291         if (unlikely(err))
292                 goto out;
293
294         spin_lock(&lo->queue_lock);
295         list_for_each(tmp, &lo->queue_head) {
296                 req = list_entry(tmp, struct request, queuelist);
297                 if (req != xreq)
298                         continue;
299                 list_del_init(&req->queuelist);
300                 spin_unlock(&lo->queue_lock);
301                 return req;
302         }
303         spin_unlock(&lo->queue_lock);
304
305         err = -ENOENT;
306
307 out:
308         return ERR_PTR(err);
309 }
310
311 static inline int sock_recv_bvec(struct socket *sock, struct bio_vec *bvec)
312 {
313         int result;
314         void *kaddr = kmap(bvec->bv_page);
315         result = sock_xmit(sock, 0, kaddr + bvec->bv_offset, bvec->bv_len,
316                         MSG_WAITALL);
317         kunmap(bvec->bv_page);
318         return result;
319 }
320
321 /* NULL returned = something went wrong, inform userspace */
322 static struct request *nbd_read_stat(struct nbd_device *lo)
323 {
324         int result;
325         struct nbd_reply reply;
326         struct request *req;
327         struct socket *sock = lo->sock;
328
329         reply.magic = 0;
330         result = sock_xmit(sock, 0, &reply, sizeof(reply), MSG_WAITALL);
331         if (result <= 0) {
332                 printk(KERN_ERR "%s: Receive control failed (result %d)\n",
333                                 lo->disk->disk_name, result);
334                 goto harderror;
335         }
336         req = nbd_find_request(lo, reply.handle);
337         if (unlikely(IS_ERR(req))) {
338                 result = PTR_ERR(req);
339                 if (result != -ENOENT)
340                         goto harderror;
341
342                 printk(KERN_ERR "%s: Unexpected reply (%p)\n",
343                                 lo->disk->disk_name, reply.handle);
344                 result = -EBADR;
345                 goto harderror;
346         }
347
348         if (ntohl(reply.magic) != NBD_REPLY_MAGIC) {
349                 printk(KERN_ERR "%s: Wrong magic (0x%lx)\n",
350                                 lo->disk->disk_name,
351                                 (unsigned long)ntohl(reply.magic));
352                 result = -EPROTO;
353                 goto harderror;
354         }
355         if (ntohl(reply.error)) {
356                 printk(KERN_ERR "%s: Other side returned error (%d)\n",
357                                 lo->disk->disk_name, ntohl(reply.error));
358                 req->errors++;
359                 return req;
360         }
361
362         dprintk(DBG_RX, "%s: request %p: got reply\n",
363                         lo->disk->disk_name, req);
364         if (nbd_cmd(req) == NBD_CMD_READ) {
365                 int i;
366                 struct bio *bio;
367                 rq_for_each_bio(bio, req) {
368                         struct bio_vec *bvec;
369                         bio_for_each_segment(bvec, bio, i) {
370                                 result = sock_recv_bvec(sock, bvec);
371                                 if (result <= 0) {
372                                         printk(KERN_ERR "%s: Receive data failed (result %d)\n",
373                                                         lo->disk->disk_name,
374                                                         result);
375                                         goto harderror;
376                                 }
377                                 dprintk(DBG_RX, "%s: request %p: got %d bytes data\n",
378                                         lo->disk->disk_name, req, bvec->bv_len);
379                         }
380                 }
381         }
382         return req;
383 harderror:
384         lo->harderror = result;
385         return NULL;
386 }
387
388 static void nbd_do_it(struct nbd_device *lo)
389 {
390         struct request *req;
391
392         BUG_ON(lo->magic != LO_MAGIC);
393
394         while ((req = nbd_read_stat(lo)) != NULL)
395                 nbd_end_request(req);
396         return;
397 }
398
399 static void nbd_clear_que(struct nbd_device *lo)
400 {
401         struct request *req;
402
403         BUG_ON(lo->magic != LO_MAGIC);
404
405         /*
406          * Because we have set lo->sock to NULL under the tx_lock, all
407          * modifications to the list must have completed by now.  For
408          * the same reason, the active_req must be NULL.
409          *
410          * As a consequence, we don't need to take the spin lock while
411          * purging the list here.
412          */
413         BUG_ON(lo->sock);
414         BUG_ON(lo->active_req);
415
416         while (!list_empty(&lo->queue_head)) {
417                 req = list_entry(lo->queue_head.next, struct request,
418                                  queuelist);
419                 list_del_init(&req->queuelist);
420                 req->errors++;
421                 nbd_end_request(req);
422         }
423 }
424
425 /*
426  * We always wait for result of write, for now. It would be nice to make it optional
427  * in future
428  * if ((req->cmd == WRITE) && (lo->flags & NBD_WRITE_NOCHK)) 
429  *   { printk( "Warning: Ignoring result!\n"); nbd_end_request( req ); }
430  */
431
432 static void do_nbd_request(request_queue_t * q)
433 {
434         struct request *req;
435         
436         while ((req = elv_next_request(q)) != NULL) {
437                 struct nbd_device *lo;
438
439                 blkdev_dequeue_request(req);
440                 dprintk(DBG_BLKDEV, "%s: request %p: dequeued (flags=%lx)\n",
441                                 req->rq_disk->disk_name, req, req->flags);
442
443                 if (!(req->flags & REQ_CMD))
444                         goto error_out;
445
446                 lo = req->rq_disk->private_data;
447
448                 BUG_ON(lo->magic != LO_MAGIC);
449
450                 nbd_cmd(req) = NBD_CMD_READ;
451                 if (rq_data_dir(req) == WRITE) {
452                         nbd_cmd(req) = NBD_CMD_WRITE;
453                         if (lo->flags & NBD_READ_ONLY) {
454                                 printk(KERN_ERR "%s: Write on read-only\n",
455                                                 lo->disk->disk_name);
456                                 goto error_out;
457                         }
458                 }
459
460                 req->errors = 0;
461                 spin_unlock_irq(q->queue_lock);
462
463                 down(&lo->tx_lock);
464                 if (unlikely(!lo->sock)) {
465                         up(&lo->tx_lock);
466                         printk(KERN_ERR "%s: Attempted send on closed socket\n",
467                                lo->disk->disk_name);
468                         req->errors++;
469                         nbd_end_request(req);
470                         spin_lock_irq(q->queue_lock);
471                         continue;
472                 }
473
474                 lo->active_req = req;
475
476                 if (nbd_send_req(lo, req) != 0) {
477                         printk(KERN_ERR "%s: Request send failed\n",
478                                         lo->disk->disk_name);
479                         req->errors++;
480                         nbd_end_request(req);
481                 } else {
482                         spin_lock(&lo->queue_lock);
483                         list_add(&req->queuelist, &lo->queue_head);
484                         spin_unlock(&lo->queue_lock);
485                 }
486
487                 lo->active_req = NULL;
488                 up(&lo->tx_lock);
489                 wake_up_all(&lo->active_wq);
490
491                 spin_lock_irq(q->queue_lock);
492                 continue;
493
494 error_out:
495                 req->errors++;
496                 spin_unlock(q->queue_lock);
497                 nbd_end_request(req);
498                 spin_lock(q->queue_lock);
499         }
500         return;
501 }
502
503 static int nbd_ioctl(struct inode *inode, struct file *file,
504                      unsigned int cmd, unsigned long arg)
505 {
506         struct nbd_device *lo = inode->i_bdev->bd_disk->private_data;
507         int error;
508         struct request sreq ;
509
510         if (!capable(CAP_SYS_ADMIN))
511                 return -EPERM;
512
513         BUG_ON(lo->magic != LO_MAGIC);
514
515         /* Anyone capable of this syscall can do *real bad* things */
516         dprintk(DBG_IOCTL, "%s: nbd_ioctl cmd=%s(0x%x) arg=%lu\n",
517                         lo->disk->disk_name, ioctl_cmd_to_ascii(cmd), cmd, arg);
518
519         switch (cmd) {
520         case NBD_DISCONNECT:
521                 printk(KERN_INFO "%s: NBD_DISCONNECT\n", lo->disk->disk_name);
522                 sreq.flags = REQ_SPECIAL;
523                 nbd_cmd(&sreq) = NBD_CMD_DISC;
524                 /*
525                  * Set these to sane values in case server implementation
526                  * fails to check the request type first and also to keep
527                  * debugging output cleaner.
528                  */
529                 sreq.sector = 0;
530                 sreq.nr_sectors = 0;
531                 if (!lo->sock)
532                         return -EINVAL;
533                 nbd_send_req(lo, &sreq);
534                 return 0;
535  
536         case NBD_CLEAR_SOCK:
537                 error = 0;
538                 down(&lo->tx_lock);
539                 lo->sock = NULL;
540                 up(&lo->tx_lock);
541                 file = lo->file;
542                 lo->file = NULL;
543                 nbd_clear_que(lo);
544                 BUG_ON(!list_empty(&lo->queue_head));
545                 if (file)
546                         fput(file);
547                 return error;
548         case NBD_SET_SOCK:
549                 if (lo->file)
550                         return -EBUSY;
551                 error = -EINVAL;
552                 file = fget(arg);
553                 if (file) {
554                         inode = file->f_dentry->d_inode;
555                         if (S_ISSOCK(inode->i_mode)) {
556                                 lo->file = file;
557                                 lo->sock = SOCKET_I(inode);
558                                 error = 0;
559                         } else {
560                                 fput(file);
561                         }
562                 }
563                 return error;
564         case NBD_SET_BLKSIZE:
565                 lo->blksize = arg;
566                 lo->bytesize &= ~(lo->blksize-1);
567                 inode->i_bdev->bd_inode->i_size = lo->bytesize;
568                 set_blocksize(inode->i_bdev, lo->blksize);
569                 set_capacity(lo->disk, lo->bytesize >> 9);
570                 return 0;
571         case NBD_SET_SIZE:
572                 lo->bytesize = arg & ~(lo->blksize-1);
573                 inode->i_bdev->bd_inode->i_size = lo->bytesize;
574                 set_blocksize(inode->i_bdev, lo->blksize);
575                 set_capacity(lo->disk, lo->bytesize >> 9);
576                 return 0;
577         case NBD_SET_SIZE_BLOCKS:
578                 lo->bytesize = ((u64) arg) * lo->blksize;
579                 inode->i_bdev->bd_inode->i_size = lo->bytesize;
580                 set_blocksize(inode->i_bdev, lo->blksize);
581                 set_capacity(lo->disk, lo->bytesize >> 9);
582                 return 0;
583         case NBD_DO_IT:
584                 if (!lo->file)
585                         return -EINVAL;
586                 nbd_do_it(lo);
587                 /* on return tidy up in case we have a signal */
588                 /* Forcibly shutdown the socket causing all listeners
589                  * to error
590                  *
591                  * FIXME: This code is duplicated from sys_shutdown, but
592                  * there should be a more generic interface rather than
593                  * calling socket ops directly here */
594                 down(&lo->tx_lock);
595                 if (lo->sock) {
596                         printk(KERN_WARNING "%s: shutting down socket\n",
597                                 lo->disk->disk_name);
598                         lo->sock->ops->shutdown(lo->sock,
599                                 SEND_SHUTDOWN|RCV_SHUTDOWN);
600                         lo->sock = NULL;
601                 }
602                 up(&lo->tx_lock);
603                 file = lo->file;
604                 lo->file = NULL;
605                 nbd_clear_que(lo);
606                 printk(KERN_WARNING "%s: queue cleared\n", lo->disk->disk_name);
607                 if (file)
608                         fput(file);
609                 return lo->harderror;
610         case NBD_CLEAR_QUE:
611                 /*
612                  * This is for compatibility only.  The queue is always cleared
613                  * by NBD_DO_IT or NBD_CLEAR_SOCK.
614                  */
615                 BUG_ON(!lo->sock && !list_empty(&lo->queue_head));
616                 return 0;
617         case NBD_PRINT_DEBUG:
618                 printk(KERN_INFO "%s: next = %p, prev = %p, head = %p\n",
619                         inode->i_bdev->bd_disk->disk_name,
620                         lo->queue_head.next, lo->queue_head.prev,
621                         &lo->queue_head);
622                 return 0;
623         }
624         return -EINVAL;
625 }
626
627 static struct block_device_operations nbd_fops =
628 {
629         .owner =        THIS_MODULE,
630         .ioctl =        nbd_ioctl,
631 };
632
633 /*
634  * And here should be modules and kernel interface 
635  *  (Just smiley confuses emacs :-)
636  */
637
638 static int __init nbd_init(void)
639 {
640         int err = -ENOMEM;
641         int i;
642
643         if (sizeof(struct nbd_request) != 28) {
644                 printk(KERN_CRIT "nbd: sizeof nbd_request needs to be 28 in order to work!\n" );
645                 return -EIO;
646         }
647
648         if (nbds_max > MAX_NBD) {
649                 printk(KERN_CRIT "nbd: cannot allocate more than %u nbds; %u requested.\n", MAX_NBD,
650                                 nbds_max);
651                 return -EINVAL;
652         }
653
654         for (i = 0; i < nbds_max; i++) {
655                 struct gendisk *disk = alloc_disk(1);
656                 if (!disk)
657                         goto out;
658                 nbd_dev[i].disk = disk;
659                 /*
660                  * The new linux 2.5 block layer implementation requires
661                  * every gendisk to have its very own request_queue struct.
662                  * These structs are big so we dynamically allocate them.
663                  */
664                 disk->queue = blk_init_queue(do_nbd_request, &nbd_lock);
665                 if (!disk->queue) {
666                         put_disk(disk);
667                         goto out;
668                 }
669         }
670
671         if (register_blkdev(NBD_MAJOR, "nbd")) {
672                 err = -EIO;
673                 goto out;
674         }
675
676         printk(KERN_INFO "nbd: registered device at major %d\n", NBD_MAJOR);
677         dprintk(DBG_INIT, "nbd: debugflags=0x%x\n", debugflags);
678
679         devfs_mk_dir("nbd");
680         for (i = 0; i < nbds_max; i++) {
681                 struct gendisk *disk = nbd_dev[i].disk;
682                 nbd_dev[i].file = NULL;
683                 nbd_dev[i].magic = LO_MAGIC;
684                 nbd_dev[i].flags = 0;
685                 spin_lock_init(&nbd_dev[i].queue_lock);
686                 INIT_LIST_HEAD(&nbd_dev[i].queue_head);
687                 init_MUTEX(&nbd_dev[i].tx_lock);
688                 init_waitqueue_head(&nbd_dev[i].active_wq);
689                 nbd_dev[i].blksize = 1024;
690                 nbd_dev[i].bytesize = 0x7ffffc00ULL << 10; /* 2TB */
691                 disk->major = NBD_MAJOR;
692                 disk->first_minor = i;
693                 disk->fops = &nbd_fops;
694                 disk->private_data = &nbd_dev[i];
695                 disk->flags |= GENHD_FL_SUPPRESS_PARTITION_INFO;
696                 sprintf(disk->disk_name, "nbd%d", i);
697                 sprintf(disk->devfs_name, "nbd/%d", i);
698                 set_capacity(disk, 0x7ffffc00ULL << 1); /* 2 TB */
699                 add_disk(disk);
700         }
701
702         return 0;
703 out:
704         while (i--) {
705                 blk_cleanup_queue(nbd_dev[i].disk->queue);
706                 put_disk(nbd_dev[i].disk);
707         }
708         return err;
709 }
710
711 static void __exit nbd_cleanup(void)
712 {
713         int i;
714         for (i = 0; i < nbds_max; i++) {
715                 struct gendisk *disk = nbd_dev[i].disk;
716                 nbd_dev[i].magic = 0;
717                 if (disk) {
718                         del_gendisk(disk);
719                         blk_cleanup_queue(disk->queue);
720                         put_disk(disk);
721                 }
722         }
723         devfs_remove("nbd");
724         unregister_blkdev(NBD_MAJOR, "nbd");
725         printk(KERN_INFO "nbd: unregistered device at major %d\n", NBD_MAJOR);
726 }
727
728 module_init(nbd_init);
729 module_exit(nbd_cleanup);
730
731 MODULE_DESCRIPTION("Network Block Device");
732 MODULE_LICENSE("GPL");
733
734 module_param(nbds_max, int, 0444);
735 MODULE_PARM_DESC(nbds_max, "How many network block devices to initialize.");
736 #ifndef NDEBUG
737 module_param(debugflags, int, 0644);
738 MODULE_PARM_DESC(debugflags, "flags for controlling debug output");
739 #endif