ftp://ftp.kernel.org/pub/linux/kernel/v2.6/linux-2.6.6.tar.bz2
[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 verify_area 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 #endif /* NDEBUG */
82
83 static struct nbd_device nbd_dev[MAX_NBD];
84
85 /*
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
89  *    than NICs anyway.
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
92  *    a single lock.
93  * Thanks go to Jens Axboe and Al Viro for their LKML emails explaining this!
94  */
95 static spinlock_t nbd_lock = SPIN_LOCK_UNLOCKED;
96
97 #ifndef NDEBUG
98 static const char *ioctl_cmd_to_ascii(int cmd)
99 {
100         switch (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";
112         }
113         return "unknown";
114 }
115
116 static const char *nbdcmd_to_ascii(int cmd)
117 {
118         switch (cmd) {
119         case  NBD_CMD_READ: return "read";
120         case NBD_CMD_WRITE: return "write";
121         case  NBD_CMD_DISC: return "disconnect";
122         }
123         return "invalid";
124 }
125 #endif /* NDEBUG */
126
127 static void nbd_end_request(struct request *req)
128 {
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;
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(&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);
145         }
146         spin_unlock(&lo->queue_lock);
147
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);
151         }
152         spin_unlock_irqrestore(q->queue_lock, flags);
153 }
154
155 /*
156  *  Send or receive packet.
157  */
158 static int sock_xmit(struct socket *sock, int send, void *buf, int size,
159                 int msg_flags)
160 {
161         mm_segment_t oldfs;
162         int result;
163         struct msghdr msg;
164         struct iovec iov;
165         unsigned long flags;
166         sigset_t oldset;
167
168         oldfs = get_fs();
169         set_fs(get_ds());
170         /* Allow interception of SIGKILL only
171          * Don't allow other signals to interrupt the transmission */
172         spin_lock_irqsave(&current->sighand->siglock, flags);
173         oldset = current->blocked;
174         sigfillset(&current->blocked);
175         sigdelsetmask(&current->blocked, sigmask(SIGKILL));
176         recalc_sigpending();
177         spin_unlock_irqrestore(&current->sighand->siglock, flags);
178
179         do {
180                 sock->sk->sk_allocation = GFP_NOIO;
181                 iov.iov_base = buf;
182                 iov.iov_len = size;
183                 msg.msg_name = NULL;
184                 msg.msg_namelen = 0;
185                 msg.msg_iov = &iov;
186                 msg.msg_iovlen = 1;
187                 msg.msg_control = NULL;
188                 msg.msg_controllen = 0;
189                 msg.msg_namelen = 0;
190                 msg.msg_flags = msg_flags | MSG_NOSIGNAL;
191
192                 if (send)
193                         result = sock_sendmsg(sock, &msg, size);
194                 else
195                         result = sock_recvmsg(sock, &msg, size, 0);
196
197                 if (signal_pending(current)) {
198                         siginfo_t info;
199                         spin_lock_irqsave(&current->sighand->siglock, flags);
200                         printk(KERN_WARNING "nbd (pid %d: %s) got signal %d\n",
201                                 current->pid, current->comm, 
202                                 dequeue_signal(current, &current->blocked, &info));
203                         spin_unlock_irqrestore(&current->sighand->siglock, flags);
204                         result = -EINTR;
205                         break;
206                 }
207
208                 if (result <= 0) {
209                         if (result == 0)
210                                 result = -EPIPE; /* short read */
211                         break;
212                 }
213                 size -= result;
214                 buf += result;
215         } while (size > 0);
216
217         spin_lock_irqsave(&current->sighand->siglock, flags);
218         current->blocked = oldset;
219         recalc_sigpending();
220         spin_unlock_irqrestore(&current->sighand->siglock, flags);
221
222         set_fs(oldfs);
223         return result;
224 }
225
226 static inline int sock_send_bvec(struct socket *sock, struct bio_vec *bvec,
227                 int flags)
228 {
229         int result;
230         void *kaddr = kmap(bvec->bv_page);
231         result = sock_xmit(sock, 1, kaddr + bvec->bv_offset, bvec->bv_len,
232                         flags);
233         kunmap(bvec->bv_page);
234         return result;
235 }
236
237 void nbd_send_req(struct nbd_device *lo, struct request *req)
238 {
239         int result, i, flags;
240         struct nbd_request request;
241         unsigned long size = req->nr_sectors << 9;
242         struct socket *sock = lo->sock;
243
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));
249
250         down(&lo->tx_lock);
251
252         if (!sock || !lo->sock) {
253                 printk(KERN_ERR "%s: Attempted send on closed socket\n",
254                                 lo->disk->disk_name);
255                 goto error_out;
256         }
257
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);
265         if (result <= 0) {
266                 printk(KERN_ERR "%s: Send control failed (result %d)\n",
267                                 lo->disk->disk_name, result);
268                 goto error_out;
269         }
270
271         if (nbd_cmd(req) == NBD_CMD_WRITE) {
272                 struct bio *bio;
273                 /*
274                  * we are really probing at internals to determine
275                  * whether to set MSG_MORE or not...
276                  */
277                 rq_for_each_bio(bio, req) {
278                         struct bio_vec *bvec;
279                         bio_for_each_segment(bvec, bio, i) {
280                                 flags = 0;
281                                 if ((i < (bio->bi_vcnt - 1)) || bio->bi_next)
282                                         flags = MSG_MORE;
283                                 dprintk(DBG_TX, "%s: request %p: sending %d bytes data\n",
284                                                 lo->disk->disk_name, req,
285                                                 bvec->bv_len);
286                                 result = sock_send_bvec(sock, bvec, flags);
287                                 if (result <= 0) {
288                                         printk(KERN_ERR "%s: Send data failed (result %d)\n",
289                                                         lo->disk->disk_name,
290                                                         result);
291                                         goto error_out;
292                                 }
293                         }
294                 }
295         }
296         up(&lo->tx_lock);
297         return;
298
299 error_out:
300         up(&lo->tx_lock);
301         req->errors++;
302 }
303
304 static struct request *nbd_find_request(struct nbd_device *lo, char *handle)
305 {
306         struct request *req;
307         struct list_head *tmp;
308         struct request *xreq;
309
310         memcpy(&xreq, handle, sizeof(xreq));
311
312         spin_lock(&lo->queue_lock);
313         list_for_each(tmp, &lo->queue_head) {
314                 req = list_entry(tmp, struct request, queuelist);
315                 if (req != xreq)
316                         continue;
317                 list_del_init(&req->queuelist);
318                 spin_unlock(&lo->queue_lock);
319                 return req;
320         }
321         spin_unlock(&lo->queue_lock);
322         return NULL;
323 }
324
325 static inline int sock_recv_bvec(struct socket *sock, struct bio_vec *bvec)
326 {
327         int result;
328         void *kaddr = kmap(bvec->bv_page);
329         result = sock_xmit(sock, 0, kaddr + bvec->bv_offset, bvec->bv_len,
330                         MSG_WAITALL);
331         kunmap(bvec->bv_page);
332         return result;
333 }
334
335 /* NULL returned = something went wrong, inform userspace */
336 struct request *nbd_read_stat(struct nbd_device *lo)
337 {
338         int result;
339         struct nbd_reply reply;
340         struct request *req;
341         struct socket *sock = lo->sock;
342
343         reply.magic = 0;
344         result = sock_xmit(sock, 0, &reply, sizeof(reply), MSG_WAITALL);
345         if (result <= 0) {
346                 printk(KERN_ERR "%s: Receive control failed (result %d)\n",
347                                 lo->disk->disk_name, result);
348                 goto harderror;
349         }
350         req = nbd_find_request(lo, reply.handle);
351         if (req == NULL) {
352                 printk(KERN_ERR "%s: Unexpected reply (%p)\n",
353                                 lo->disk->disk_name, reply.handle);
354                 result = -EBADR;
355                 goto harderror;
356         }
357
358         if (ntohl(reply.magic) != NBD_REPLY_MAGIC) {
359                 printk(KERN_ERR "%s: Wrong magic (0x%lx)\n",
360                                 lo->disk->disk_name,
361                                 (unsigned long)ntohl(reply.magic));
362                 result = -EPROTO;
363                 goto harderror;
364         }
365         if (ntohl(reply.error)) {
366                 printk(KERN_ERR "%s: Other side returned error (%d)\n",
367                                 lo->disk->disk_name, ntohl(reply.error));
368                 req->errors++;
369                 return req;
370         }
371
372         dprintk(DBG_RX, "%s: request %p: got reply\n",
373                         lo->disk->disk_name, req);
374         if (nbd_cmd(req) == NBD_CMD_READ) {
375                 int i;
376                 struct bio *bio;
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);
381                                 if (result <= 0) {
382                                         printk(KERN_ERR "%s: Receive data failed (result %d)\n",
383                                                         lo->disk->disk_name,
384                                                         result);
385                                         goto harderror;
386                                 }
387                                 dprintk(DBG_RX, "%s: request %p: got %d bytes data\n",
388                                         lo->disk->disk_name, req, bvec->bv_len);
389                         }
390                 }
391         }
392         return req;
393 harderror:
394         lo->harderror = result;
395         return NULL;
396 }
397
398 void nbd_do_it(struct nbd_device *lo)
399 {
400         struct request *req;
401
402         BUG_ON(lo->magic != LO_MAGIC);
403
404         while ((req = nbd_read_stat(lo)) != NULL)
405                 nbd_end_request(req);
406         return;
407 }
408
409 void nbd_clear_que(struct nbd_device *lo)
410 {
411         struct request *req;
412
413         BUG_ON(lo->magic != LO_MAGIC);
414
415         do {
416                 req = NULL;
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);
421                 }
422                 spin_unlock(&lo->queue_lock);
423                 if (req) {
424                         req->errors++;
425                         nbd_end_request(req);
426                 }
427         } while (req);
428 }
429
430 /*
431  * We always wait for result of write, for now. It would be nice to make it optional
432  * in future
433  * if ((req->cmd == WRITE) && (lo->flags & NBD_WRITE_NOCHK)) 
434  *   { printk( "Warning: Ignoring result!\n"); nbd_end_request( req ); }
435  */
436
437 static void do_nbd_request(request_queue_t * q)
438 {
439         struct request *req;
440         
441         while ((req = elv_next_request(q)) != NULL) {
442                 struct nbd_device *lo;
443
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);
447
448                 if (!(req->flags & REQ_CMD))
449                         goto error_out;
450
451                 lo = req->rq_disk->private_data;
452
453                 BUG_ON(lo->magic != LO_MAGIC);
454
455                 if (!lo->file) {
456                         printk(KERN_ERR "%s: Request when not-ready\n",
457                                         lo->disk->disk_name);
458                         goto error_out;
459                 }
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);
466                                 goto error_out;
467                         }
468                 }
469
470                 req->errors = 0;
471                 spin_unlock_irq(q->queue_lock);
472
473                 spin_lock(&lo->queue_lock);
474
475                 if (!lo->file) {
476                         spin_unlock(&lo->queue_lock);
477                         printk(KERN_ERR "%s: failed between accept and semaphore, file lost\n",
478                                         lo->disk->disk_name);
479                         req->errors++;
480                         nbd_end_request(req);
481                         spin_lock_irq(q->queue_lock);
482                         continue;
483                 }
484
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);
488
489                 nbd_send_req(lo, req);
490
491                 if (req->errors) {
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);
496                         req->ref_count--;
497                         spin_unlock(&lo->queue_lock);
498                         nbd_end_request(req);
499                         spin_lock_irq(q->queue_lock);
500                         continue;
501                 }
502
503                 spin_lock(&lo->queue_lock);
504                 req->ref_count--;
505                 spin_unlock(&lo->queue_lock);
506                 spin_lock_irq(q->queue_lock);
507                 continue;
508
509 error_out:
510                 req->errors++;
511                 spin_unlock(q->queue_lock);
512                 nbd_end_request(req);
513                 spin_lock(q->queue_lock);
514         }
515         return;
516 }
517
518 static int nbd_ioctl(struct inode *inode, struct file *file,
519                      unsigned int cmd, unsigned long arg)
520 {
521         struct nbd_device *lo = inode->i_bdev->bd_disk->private_data;
522         int error;
523         struct request sreq ;
524
525         if (!capable(CAP_SYS_ADMIN))
526                 return -EPERM;
527
528         BUG_ON(lo->magic != LO_MAGIC);
529
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);
533
534         switch (cmd) {
535         case NBD_DISCONNECT:
536                 printk(KERN_INFO "%s: NBD_DISCONNECT\n", lo->disk->disk_name);
537                 sreq.flags = REQ_SPECIAL;
538                 nbd_cmd(&sreq) = NBD_CMD_DISC;
539                 /*
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.
543                  */
544                 sreq.sector = 0;
545                 sreq.nr_sectors = 0;
546                 if (!lo->sock)
547                         return -EINVAL;
548                 nbd_send_req(lo, &sreq);
549                 return 0;
550  
551         case NBD_CLEAR_SOCK:
552                 error = 0;
553                 down(&lo->tx_lock);
554                 lo->sock = NULL;
555                 up(&lo->tx_lock);
556                 spin_lock(&lo->queue_lock);
557                 file = lo->file;
558                 lo->file = NULL;
559                 spin_unlock(&lo->queue_lock);
560                 nbd_clear_que(lo);
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");
564                         error = -EBUSY;
565                 }
566                 spin_unlock(&lo->queue_lock);
567                 if (file)
568                         fput(file);
569                 return error;
570         case NBD_SET_SOCK:
571                 if (lo->file)
572                         return -EBUSY;
573                 error = -EINVAL;
574                 file = fget(arg);
575                 if (file) {
576                         inode = file->f_dentry->d_inode;
577                         if (inode->i_sock) {
578                                 lo->file = file;
579                                 lo->sock = SOCKET_I(inode);
580                                 error = 0;
581                         } else {
582                                 fput(file);
583                         }
584                 }
585                 return error;
586         case NBD_SET_BLKSIZE:
587                 lo->blksize = arg;
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);
592                 return 0;
593         case NBD_SET_SIZE:
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);
598                 return 0;
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);
604                 return 0;
605         case NBD_DO_IT:
606                 if (!lo->file)
607                         return -EINVAL;
608                 nbd_do_it(lo);
609                 /* on return tidy up in case we have a signal */
610                 /* Forcibly shutdown the socket causing all listeners
611                  * to error
612                  *
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 */
616                 down(&lo->tx_lock);
617                 if (lo->sock) {
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);
622                         lo->sock = NULL;
623                 }
624                 up(&lo->tx_lock);
625                 spin_lock(&lo->queue_lock);
626                 file = lo->file;
627                 lo->file = NULL;
628                 spin_unlock(&lo->queue_lock);
629                 nbd_clear_que(lo);
630                 printk(KERN_WARNING "%s: queue cleared\n", lo->disk->disk_name);
631                 if (file)
632                         fput(file);
633                 return lo->harderror;
634         case NBD_CLEAR_QUE:
635                 down(&lo->tx_lock);
636                 if (lo->sock) {
637                         up(&lo->tx_lock);
638                         return 0; /* probably should be error, but that would
639                                    * break "nbd-client -d", so just return 0 */
640                 }
641                 up(&lo->tx_lock);
642                 nbd_clear_que(lo);
643                 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,
648                         &lo->queue_head);
649                 return 0;
650         }
651         return -EINVAL;
652 }
653
654 static struct block_device_operations nbd_fops =
655 {
656         .owner =        THIS_MODULE,
657         .ioctl =        nbd_ioctl,
658 };
659
660 /*
661  * And here should be modules and kernel interface 
662  *  (Just smiley confuses emacs :-)
663  */
664
665 static int __init nbd_init(void)
666 {
667         int err = -ENOMEM;
668         int i;
669
670         if (sizeof(struct nbd_request) != 28) {
671                 printk(KERN_CRIT "nbd: sizeof nbd_request needs to be 28 in order to work!\n" );
672                 return -EIO;
673         }
674
675         for (i = 0; i < MAX_NBD; i++) {
676                 struct gendisk *disk = alloc_disk(1);
677                 if (!disk)
678                         goto out;
679                 nbd_dev[i].disk = disk;
680                 /*
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.
684                  */
685                 disk->queue = blk_init_queue(do_nbd_request, &nbd_lock);
686                 if (!disk->queue) {
687                         put_disk(disk);
688                         goto out;
689                 }
690         }
691
692         if (register_blkdev(NBD_MAJOR, "nbd")) {
693                 err = -EIO;
694                 goto out;
695         }
696
697         printk(KERN_INFO "nbd: registered device at major %d\n", NBD_MAJOR);
698         dprintk(DBG_INIT, "nbd: debugflags=0x%x\n", debugflags);
699
700         devfs_mk_dir("nbd");
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 */
719                 add_disk(disk);
720         }
721
722         return 0;
723 out:
724         while (i--) {
725                 blk_cleanup_queue(nbd_dev[i].disk->queue);
726                 put_disk(nbd_dev[i].disk);
727         }
728         return err;
729 }
730
731 static void __exit nbd_cleanup(void)
732 {
733         int i;
734         for (i = 0; i < MAX_NBD; i++) {
735                 struct gendisk *disk = nbd_dev[i].disk;
736                 if (disk) {
737                         del_gendisk(disk);
738                         blk_cleanup_queue(disk->queue);
739                         put_disk(disk);
740                 }
741         }
742         devfs_remove("nbd");
743         unregister_blkdev(NBD_MAJOR, "nbd");
744         printk(KERN_INFO "nbd: unregistered device at major %d\n", NBD_MAJOR);
745 }
746
747 module_init(nbd_init);
748 module_exit(nbd_cleanup);
749
750 MODULE_DESCRIPTION("Network Block Device");
751 MODULE_LICENSE("GPL");
752
753 #ifndef NDEBUG
754 MODULE_PARM(debugflags, "i");
755 MODULE_PARM_DESC(debugflags, "flags for controlling debug output");
756 #endif