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