[BLOCK] add @uptodate to end_that_request_last() and @error to rq_end_io_fn()
[linux-2.6.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 <net/sock.h>
58
59 #include <linux/devfs_fs_kernel.h>
60
61 #include <asm/uaccess.h>
62 #include <asm/types.h>
63
64 #include <linux/nbd.h>
65
66 #define LO_MAGIC 0x68797548
67
68 #ifdef NDEBUG
69 #define dprintk(flags, fmt...)
70 #else /* NDEBUG */
71 #define dprintk(flags, fmt...) do { \
72         if (debugflags & (flags)) printk(KERN_DEBUG fmt); \
73 } while (0)
74 #define DBG_IOCTL       0x0004
75 #define DBG_INIT        0x0010
76 #define DBG_EXIT        0x0020
77 #define DBG_BLKDEV      0x0100
78 #define DBG_RX          0x0200
79 #define DBG_TX          0x0400
80 static unsigned int debugflags;
81 static unsigned int nbds_max = 16;
82 #endif /* NDEBUG */
83
84 static struct nbd_device nbd_dev[MAX_NBD];
85
86 /*
87  * Use just one lock (or at most 1 per NIC). Two arguments for this:
88  * 1. Each NIC is essentially a synchronization point for all servers
89  *    accessed through that NIC so there's no need to have more locks
90  *    than NICs anyway.
91  * 2. More locks lead to more "Dirty cache line bouncing" which will slow
92  *    down each lock to the point where they're actually slower than just
93  *    a single lock.
94  * Thanks go to Jens Axboe and Al Viro for their LKML emails explaining this!
95  */
96 static DEFINE_SPINLOCK(nbd_lock);
97
98 #ifndef NDEBUG
99 static const char *ioctl_cmd_to_ascii(int cmd)
100 {
101         switch (cmd) {
102         case NBD_SET_SOCK: return "set-sock";
103         case NBD_SET_BLKSIZE: return "set-blksize";
104         case NBD_SET_SIZE: return "set-size";
105         case NBD_DO_IT: return "do-it";
106         case NBD_CLEAR_SOCK: return "clear-sock";
107         case NBD_CLEAR_QUE: return "clear-que";
108         case NBD_PRINT_DEBUG: return "print-debug";
109         case NBD_SET_SIZE_BLOCKS: return "set-size-blocks";
110         case NBD_DISCONNECT: return "disconnect";
111         case BLKROSET: return "set-read-only";
112         case BLKFLSBUF: return "flush-buffer-cache";
113         }
114         return "unknown";
115 }
116
117 static const char *nbdcmd_to_ascii(int cmd)
118 {
119         switch (cmd) {
120         case  NBD_CMD_READ: return "read";
121         case NBD_CMD_WRITE: return "write";
122         case  NBD_CMD_DISC: return "disconnect";
123         }
124         return "invalid";
125 }
126 #endif /* NDEBUG */
127
128 static void nbd_end_request(struct request *req)
129 {
130         int uptodate = (req->errors == 0) ? 1 : 0;
131         request_queue_t *q = req->q;
132         unsigned long flags;
133
134         dprintk(DBG_BLKDEV, "%s: request %p: %s\n", req->rq_disk->disk_name,
135                         req, uptodate? "done": "failed");
136
137         spin_lock_irqsave(q->queue_lock, flags);
138         if (!end_that_request_first(req, uptodate, req->nr_sectors)) {
139                 end_that_request_last(req, uptodate);
140         }
141         spin_unlock_irqrestore(q->queue_lock, flags);
142 }
143
144 /*
145  *  Send or receive packet.
146  */
147 static int sock_xmit(struct socket *sock, int send, void *buf, int size,
148                 int msg_flags)
149 {
150         int result;
151         struct msghdr msg;
152         struct kvec iov;
153         unsigned long flags;
154         sigset_t oldset;
155
156         /* Allow interception of SIGKILL only
157          * Don't allow other signals to interrupt the transmission */
158         spin_lock_irqsave(&current->sighand->siglock, flags);
159         oldset = current->blocked;
160         sigfillset(&current->blocked);
161         sigdelsetmask(&current->blocked, sigmask(SIGKILL));
162         recalc_sigpending();
163         spin_unlock_irqrestore(&current->sighand->siglock, flags);
164
165         do {
166                 sock->sk->sk_allocation = GFP_NOIO;
167                 iov.iov_base = buf;
168                 iov.iov_len = size;
169                 msg.msg_name = NULL;
170                 msg.msg_namelen = 0;
171                 msg.msg_control = NULL;
172                 msg.msg_controllen = 0;
173                 msg.msg_namelen = 0;
174                 msg.msg_flags = msg_flags | MSG_NOSIGNAL;
175
176                 if (send)
177                         result = kernel_sendmsg(sock, &msg, &iov, 1, size);
178                 else
179                         result = kernel_recvmsg(sock, &msg, &iov, 1, size, 0);
180
181                 if (signal_pending(current)) {
182                         siginfo_t info;
183                         spin_lock_irqsave(&current->sighand->siglock, flags);
184                         printk(KERN_WARNING "nbd (pid %d: %s) got signal %d\n",
185                                 current->pid, current->comm, 
186                                 dequeue_signal(current, &current->blocked, &info));
187                         spin_unlock_irqrestore(&current->sighand->siglock, flags);
188                         result = -EINTR;
189                         break;
190                 }
191
192                 if (result <= 0) {
193                         if (result == 0)
194                                 result = -EPIPE; /* short read */
195                         break;
196                 }
197                 size -= result;
198                 buf += result;
199         } while (size > 0);
200
201         spin_lock_irqsave(&current->sighand->siglock, flags);
202         current->blocked = oldset;
203         recalc_sigpending();
204         spin_unlock_irqrestore(&current->sighand->siglock, flags);
205
206         return result;
207 }
208
209 static inline int sock_send_bvec(struct socket *sock, struct bio_vec *bvec,
210                 int flags)
211 {
212         int result;
213         void *kaddr = kmap(bvec->bv_page);
214         result = sock_xmit(sock, 1, kaddr + bvec->bv_offset, bvec->bv_len,
215                         flags);
216         kunmap(bvec->bv_page);
217         return result;
218 }
219
220 static int nbd_send_req(struct nbd_device *lo, struct request *req)
221 {
222         int result, i, flags;
223         struct nbd_request request;
224         unsigned long size = req->nr_sectors << 9;
225         struct socket *sock = lo->sock;
226
227         request.magic = htonl(NBD_REQUEST_MAGIC);
228         request.type = htonl(nbd_cmd(req));
229         request.from = cpu_to_be64((u64) req->sector << 9);
230         request.len = htonl(size);
231         memcpy(request.handle, &req, sizeof(req));
232
233         down(&lo->tx_lock);
234
235         if (!sock || !lo->sock) {
236                 printk(KERN_ERR "%s: Attempted send on closed socket\n",
237                                 lo->disk->disk_name);
238                 goto error_out;
239         }
240
241         dprintk(DBG_TX, "%s: request %p: sending control (%s@%llu,%luB)\n",
242                         lo->disk->disk_name, req,
243                         nbdcmd_to_ascii(nbd_cmd(req)),
244                         (unsigned long long)req->sector << 9,
245                         req->nr_sectors << 9);
246         result = sock_xmit(sock, 1, &request, sizeof(request),
247                         (nbd_cmd(req) == NBD_CMD_WRITE)? MSG_MORE: 0);
248         if (result <= 0) {
249                 printk(KERN_ERR "%s: Send control failed (result %d)\n",
250                                 lo->disk->disk_name, result);
251                 goto error_out;
252         }
253
254         if (nbd_cmd(req) == NBD_CMD_WRITE) {
255                 struct bio *bio;
256                 /*
257                  * we are really probing at internals to determine
258                  * whether to set MSG_MORE or not...
259                  */
260                 rq_for_each_bio(bio, req) {
261                         struct bio_vec *bvec;
262                         bio_for_each_segment(bvec, bio, i) {
263                                 flags = 0;
264                                 if ((i < (bio->bi_vcnt - 1)) || bio->bi_next)
265                                         flags = MSG_MORE;
266                                 dprintk(DBG_TX, "%s: request %p: sending %d bytes data\n",
267                                                 lo->disk->disk_name, req,
268                                                 bvec->bv_len);
269                                 result = sock_send_bvec(sock, bvec, flags);
270                                 if (result <= 0) {
271                                         printk(KERN_ERR "%s: Send data failed (result %d)\n",
272                                                         lo->disk->disk_name,
273                                                         result);
274                                         goto error_out;
275                                 }
276                         }
277                 }
278         }
279         up(&lo->tx_lock);
280         return 0;
281
282 error_out:
283         up(&lo->tx_lock);
284         return 1;
285 }
286
287 static struct request *nbd_find_request(struct nbd_device *lo, char *handle)
288 {
289         struct request *req;
290         struct list_head *tmp;
291         struct request *xreq;
292
293         memcpy(&xreq, handle, sizeof(xreq));
294
295         spin_lock(&lo->queue_lock);
296         list_for_each(tmp, &lo->queue_head) {
297                 req = list_entry(tmp, struct request, queuelist);
298                 if (req != xreq)
299                         continue;
300                 list_del_init(&req->queuelist);
301                 spin_unlock(&lo->queue_lock);
302                 return req;
303         }
304         spin_unlock(&lo->queue_lock);
305         return NULL;
306 }
307
308 static inline int sock_recv_bvec(struct socket *sock, struct bio_vec *bvec)
309 {
310         int result;
311         void *kaddr = kmap(bvec->bv_page);
312         result = sock_xmit(sock, 0, kaddr + bvec->bv_offset, bvec->bv_len,
313                         MSG_WAITALL);
314         kunmap(bvec->bv_page);
315         return result;
316 }
317
318 /* NULL returned = something went wrong, inform userspace */
319 static struct request *nbd_read_stat(struct nbd_device *lo)
320 {
321         int result;
322         struct nbd_reply reply;
323         struct request *req;
324         struct socket *sock = lo->sock;
325
326         reply.magic = 0;
327         result = sock_xmit(sock, 0, &reply, sizeof(reply), MSG_WAITALL);
328         if (result <= 0) {
329                 printk(KERN_ERR "%s: Receive control failed (result %d)\n",
330                                 lo->disk->disk_name, result);
331                 goto harderror;
332         }
333         req = nbd_find_request(lo, reply.handle);
334         if (req == NULL) {
335                 printk(KERN_ERR "%s: Unexpected reply (%p)\n",
336                                 lo->disk->disk_name, reply.handle);
337                 result = -EBADR;
338                 goto harderror;
339         }
340
341         if (ntohl(reply.magic) != NBD_REPLY_MAGIC) {
342                 printk(KERN_ERR "%s: Wrong magic (0x%lx)\n",
343                                 lo->disk->disk_name,
344                                 (unsigned long)ntohl(reply.magic));
345                 result = -EPROTO;
346                 goto harderror;
347         }
348         if (ntohl(reply.error)) {
349                 printk(KERN_ERR "%s: Other side returned error (%d)\n",
350                                 lo->disk->disk_name, ntohl(reply.error));
351                 req->errors++;
352                 return req;
353         }
354
355         dprintk(DBG_RX, "%s: request %p: got reply\n",
356                         lo->disk->disk_name, req);
357         if (nbd_cmd(req) == NBD_CMD_READ) {
358                 int i;
359                 struct bio *bio;
360                 rq_for_each_bio(bio, req) {
361                         struct bio_vec *bvec;
362                         bio_for_each_segment(bvec, bio, i) {
363                                 result = sock_recv_bvec(sock, bvec);
364                                 if (result <= 0) {
365                                         printk(KERN_ERR "%s: Receive data failed (result %d)\n",
366                                                         lo->disk->disk_name,
367                                                         result);
368                                         goto harderror;
369                                 }
370                                 dprintk(DBG_RX, "%s: request %p: got %d bytes data\n",
371                                         lo->disk->disk_name, req, bvec->bv_len);
372                         }
373                 }
374         }
375         return req;
376 harderror:
377         lo->harderror = result;
378         return NULL;
379 }
380
381 static void nbd_do_it(struct nbd_device *lo)
382 {
383         struct request *req;
384
385         BUG_ON(lo->magic != LO_MAGIC);
386
387         while ((req = nbd_read_stat(lo)) != NULL)
388                 nbd_end_request(req);
389         return;
390 }
391
392 static void nbd_clear_que(struct nbd_device *lo)
393 {
394         struct request *req;
395
396         BUG_ON(lo->magic != LO_MAGIC);
397
398         do {
399                 req = NULL;
400                 spin_lock(&lo->queue_lock);
401                 if (!list_empty(&lo->queue_head)) {
402                         req = list_entry(lo->queue_head.next, struct request, queuelist);
403                         list_del_init(&req->queuelist);
404                 }
405                 spin_unlock(&lo->queue_lock);
406                 if (req) {
407                         req->errors++;
408                         nbd_end_request(req);
409                 }
410         } while (req);
411 }
412
413 /*
414  * We always wait for result of write, for now. It would be nice to make it optional
415  * in future
416  * if ((req->cmd == WRITE) && (lo->flags & NBD_WRITE_NOCHK)) 
417  *   { printk( "Warning: Ignoring result!\n"); nbd_end_request( req ); }
418  */
419
420 static void do_nbd_request(request_queue_t * q)
421 {
422         struct request *req;
423         
424         while ((req = elv_next_request(q)) != NULL) {
425                 struct nbd_device *lo;
426
427                 blkdev_dequeue_request(req);
428                 dprintk(DBG_BLKDEV, "%s: request %p: dequeued (flags=%lx)\n",
429                                 req->rq_disk->disk_name, req, req->flags);
430
431                 if (!(req->flags & REQ_CMD))
432                         goto error_out;
433
434                 lo = req->rq_disk->private_data;
435
436                 BUG_ON(lo->magic != LO_MAGIC);
437
438                 if (!lo->file) {
439                         printk(KERN_ERR "%s: Request when not-ready\n",
440                                         lo->disk->disk_name);
441                         goto error_out;
442                 }
443                 nbd_cmd(req) = NBD_CMD_READ;
444                 if (rq_data_dir(req) == WRITE) {
445                         nbd_cmd(req) = NBD_CMD_WRITE;
446                         if (lo->flags & NBD_READ_ONLY) {
447                                 printk(KERN_ERR "%s: Write on read-only\n",
448                                                 lo->disk->disk_name);
449                                 goto error_out;
450                         }
451                 }
452
453                 req->errors = 0;
454                 spin_unlock_irq(q->queue_lock);
455
456                 spin_lock(&lo->queue_lock);
457
458                 if (!lo->file) {
459                         spin_unlock(&lo->queue_lock);
460                         printk(KERN_ERR "%s: failed between accept and semaphore, file lost\n",
461                                         lo->disk->disk_name);
462                         req->errors++;
463                         nbd_end_request(req);
464                         spin_lock_irq(q->queue_lock);
465                         continue;
466                 }
467
468                 list_add(&req->queuelist, &lo->queue_head);
469                 spin_unlock(&lo->queue_lock);
470
471                 if (nbd_send_req(lo, req) != 0) {
472                         printk(KERN_ERR "%s: Request send failed\n",
473                                         lo->disk->disk_name);
474                         if (nbd_find_request(lo, (char *)&req) != NULL) {
475                                 /* we still own req */
476                                 req->errors++;
477                                 nbd_end_request(req);
478                         } else /* we're racing with nbd_clear_que */
479                                 printk(KERN_DEBUG "nbd: can't find req\n");
480                 }
481
482                 spin_lock_irq(q->queue_lock);
483                 continue;
484
485 error_out:
486                 req->errors++;
487                 spin_unlock(q->queue_lock);
488                 nbd_end_request(req);
489                 spin_lock(q->queue_lock);
490         }
491         return;
492 }
493
494 static int nbd_ioctl(struct inode *inode, struct file *file,
495                      unsigned int cmd, unsigned long arg)
496 {
497         struct nbd_device *lo = inode->i_bdev->bd_disk->private_data;
498         int error;
499         struct request sreq ;
500
501         if (!capable(CAP_SYS_ADMIN))
502                 return -EPERM;
503
504         BUG_ON(lo->magic != LO_MAGIC);
505
506         /* Anyone capable of this syscall can do *real bad* things */
507         dprintk(DBG_IOCTL, "%s: nbd_ioctl cmd=%s(0x%x) arg=%lu\n",
508                         lo->disk->disk_name, ioctl_cmd_to_ascii(cmd), cmd, arg);
509
510         switch (cmd) {
511         case NBD_DISCONNECT:
512                 printk(KERN_INFO "%s: NBD_DISCONNECT\n", lo->disk->disk_name);
513                 sreq.flags = REQ_SPECIAL;
514                 nbd_cmd(&sreq) = NBD_CMD_DISC;
515                 /*
516                  * Set these to sane values in case server implementation
517                  * fails to check the request type first and also to keep
518                  * debugging output cleaner.
519                  */
520                 sreq.sector = 0;
521                 sreq.nr_sectors = 0;
522                 if (!lo->sock)
523                         return -EINVAL;
524                 nbd_send_req(lo, &sreq);
525                 return 0;
526  
527         case NBD_CLEAR_SOCK:
528                 error = 0;
529                 down(&lo->tx_lock);
530                 lo->sock = NULL;
531                 up(&lo->tx_lock);
532                 spin_lock(&lo->queue_lock);
533                 file = lo->file;
534                 lo->file = NULL;
535                 spin_unlock(&lo->queue_lock);
536                 nbd_clear_que(lo);
537                 spin_lock(&lo->queue_lock);
538                 if (!list_empty(&lo->queue_head)) {
539                         printk(KERN_ERR "nbd: disconnect: some requests are in progress -> please try again.\n");
540                         error = -EBUSY;
541                 }
542                 spin_unlock(&lo->queue_lock);
543                 if (file)
544                         fput(file);
545                 return error;
546         case NBD_SET_SOCK:
547                 if (lo->file)
548                         return -EBUSY;
549                 error = -EINVAL;
550                 file = fget(arg);
551                 if (file) {
552                         inode = file->f_dentry->d_inode;
553                         if (S_ISSOCK(inode->i_mode)) {
554                                 lo->file = file;
555                                 lo->sock = SOCKET_I(inode);
556                                 error = 0;
557                         } else {
558                                 fput(file);
559                         }
560                 }
561                 return error;
562         case NBD_SET_BLKSIZE:
563                 lo->blksize = arg;
564                 lo->bytesize &= ~(lo->blksize-1);
565                 inode->i_bdev->bd_inode->i_size = lo->bytesize;
566                 set_blocksize(inode->i_bdev, lo->blksize);
567                 set_capacity(lo->disk, lo->bytesize >> 9);
568                 return 0;
569         case NBD_SET_SIZE:
570                 lo->bytesize = arg & ~(lo->blksize-1);
571                 inode->i_bdev->bd_inode->i_size = lo->bytesize;
572                 set_blocksize(inode->i_bdev, lo->blksize);
573                 set_capacity(lo->disk, lo->bytesize >> 9);
574                 return 0;
575         case NBD_SET_SIZE_BLOCKS:
576                 lo->bytesize = ((u64) arg) * lo->blksize;
577                 inode->i_bdev->bd_inode->i_size = lo->bytesize;
578                 set_blocksize(inode->i_bdev, lo->blksize);
579                 set_capacity(lo->disk, lo->bytesize >> 9);
580                 return 0;
581         case NBD_DO_IT:
582                 if (!lo->file)
583                         return -EINVAL;
584                 nbd_do_it(lo);
585                 /* on return tidy up in case we have a signal */
586                 /* Forcibly shutdown the socket causing all listeners
587                  * to error
588                  *
589                  * FIXME: This code is duplicated from sys_shutdown, but
590                  * there should be a more generic interface rather than
591                  * calling socket ops directly here */
592                 down(&lo->tx_lock);
593                 if (lo->sock) {
594                         printk(KERN_WARNING "%s: shutting down socket\n",
595                                 lo->disk->disk_name);
596                         lo->sock->ops->shutdown(lo->sock,
597                                 SEND_SHUTDOWN|RCV_SHUTDOWN);
598                         lo->sock = NULL;
599                 }
600                 up(&lo->tx_lock);
601                 spin_lock(&lo->queue_lock);
602                 file = lo->file;
603                 lo->file = NULL;
604                 spin_unlock(&lo->queue_lock);
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                 down(&lo->tx_lock);
612                 if (lo->sock) {
613                         up(&lo->tx_lock);
614                         return 0; /* probably should be error, but that would
615                                    * break "nbd-client -d", so just return 0 */
616                 }
617                 up(&lo->tx_lock);
618                 nbd_clear_que(lo);
619                 return 0;
620         case NBD_PRINT_DEBUG:
621                 printk(KERN_INFO "%s: next = %p, prev = %p, head = %p\n",
622                         inode->i_bdev->bd_disk->disk_name,
623                         lo->queue_head.next, lo->queue_head.prev,
624                         &lo->queue_head);
625                 return 0;
626         }
627         return -EINVAL;
628 }
629
630 static struct block_device_operations nbd_fops =
631 {
632         .owner =        THIS_MODULE,
633         .ioctl =        nbd_ioctl,
634 };
635
636 /*
637  * And here should be modules and kernel interface 
638  *  (Just smiley confuses emacs :-)
639  */
640
641 static int __init nbd_init(void)
642 {
643         int err = -ENOMEM;
644         int i;
645
646         if (sizeof(struct nbd_request) != 28) {
647                 printk(KERN_CRIT "nbd: sizeof nbd_request needs to be 28 in order to work!\n" );
648                 return -EIO;
649         }
650
651         if (nbds_max > MAX_NBD) {
652                 printk(KERN_CRIT "nbd: cannot allocate more than %u nbds; %u requested.\n", MAX_NBD,
653                                 nbds_max);
654                 return -EINVAL;
655         }
656
657         for (i = 0; i < nbds_max; i++) {
658                 struct gendisk *disk = alloc_disk(1);
659                 if (!disk)
660                         goto out;
661                 nbd_dev[i].disk = disk;
662                 /*
663                  * The new linux 2.5 block layer implementation requires
664                  * every gendisk to have its very own request_queue struct.
665                  * These structs are big so we dynamically allocate them.
666                  */
667                 disk->queue = blk_init_queue(do_nbd_request, &nbd_lock);
668                 if (!disk->queue) {
669                         put_disk(disk);
670                         goto out;
671                 }
672         }
673
674         if (register_blkdev(NBD_MAJOR, "nbd")) {
675                 err = -EIO;
676                 goto out;
677         }
678
679         printk(KERN_INFO "nbd: registered device at major %d\n", NBD_MAJOR);
680         dprintk(DBG_INIT, "nbd: debugflags=0x%x\n", debugflags);
681
682         devfs_mk_dir("nbd");
683         for (i = 0; i < nbds_max; i++) {
684                 struct gendisk *disk = nbd_dev[i].disk;
685                 nbd_dev[i].file = NULL;
686                 nbd_dev[i].magic = LO_MAGIC;
687                 nbd_dev[i].flags = 0;
688                 spin_lock_init(&nbd_dev[i].queue_lock);
689                 INIT_LIST_HEAD(&nbd_dev[i].queue_head);
690                 init_MUTEX(&nbd_dev[i].tx_lock);
691                 nbd_dev[i].blksize = 1024;
692                 nbd_dev[i].bytesize = 0x7ffffc00ULL << 10; /* 2TB */
693                 disk->major = NBD_MAJOR;
694                 disk->first_minor = i;
695                 disk->fops = &nbd_fops;
696                 disk->private_data = &nbd_dev[i];
697                 disk->flags |= GENHD_FL_SUPPRESS_PARTITION_INFO;
698                 sprintf(disk->disk_name, "nbd%d", i);
699                 sprintf(disk->devfs_name, "nbd/%d", i);
700                 set_capacity(disk, 0x7ffffc00ULL << 1); /* 2 TB */
701                 add_disk(disk);
702         }
703
704         return 0;
705 out:
706         while (i--) {
707                 blk_cleanup_queue(nbd_dev[i].disk->queue);
708                 put_disk(nbd_dev[i].disk);
709         }
710         return err;
711 }
712
713 static void __exit nbd_cleanup(void)
714 {
715         int i;
716         for (i = 0; i < nbds_max; i++) {
717                 struct gendisk *disk = nbd_dev[i].disk;
718                 nbd_dev[i].magic = 0;
719                 if (disk) {
720                         del_gendisk(disk);
721                         blk_cleanup_queue(disk->queue);
722                         put_disk(disk);
723                 }
724         }
725         devfs_remove("nbd");
726         unregister_blkdev(NBD_MAJOR, "nbd");
727         printk(KERN_INFO "nbd: unregistered device at major %d\n", NBD_MAJOR);
728 }
729
730 module_init(nbd_init);
731 module_exit(nbd_cleanup);
732
733 MODULE_DESCRIPTION("Network Block Device");
734 MODULE_LICENSE("GPL");
735
736 module_param(nbds_max, int, 0444);
737 MODULE_PARM_DESC(nbds_max, "How many network block devices to initialize.");
738 #ifndef NDEBUG
739 module_param(debugflags, int, 0644);
740 MODULE_PARM_DESC(debugflags, "flags for controlling debug output");
741 #endif