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