Whamcloud - gitweb
Branch HEAD
[fs/lustre-release.git] / lustre / llite / lloop.c
1 /* -*- mode: c; c-basic-offset: 8; indent-tabs-mode: nil; -*-
2  * vim:expandtab:shiftwidth=8:tabstop=8:
3  *
4  * Lustre virtual block device emulator.
5  *
6  *  Copyright (c) 2001-2003 Cluster File Systems, Inc.
7  *
8  *   This file is part of Lustre, http://www.lustre.org.
9  *
10  *   Lustre is free software; you can redistribute it and/or
11  *   modify it under the terms of version 2 of the GNU General Public
12  *   License as published by the Free Software Foundation.
13  *
14  *   Lustre is distributed in the hope that it will be useful,
15  *   but WITHOUT ANY WARRANTY; without even the implied warranty of
16  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17  *   GNU General Public License for more details.
18  *
19  *   You should have received a copy of the GNU General Public License
20  *   along with Lustre; if not, write to the Free Software
21  *   Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
22  */
23
24 /*
25  *  linux/drivers/block/loop.c
26  *
27  *  Written by Theodore Ts'o, 3/29/93
28  *
29  * Copyright 1993 by Theodore Ts'o.  Redistribution of this file is
30  * permitted under the GNU General Public License.
31  *
32  * DES encryption plus some minor changes by Werner Almesberger, 30-MAY-1993
33  * more DES encryption plus IDEA encryption by Nicholas J. Leon, June 20, 1996
34  *
35  * Modularized and updated for 1.1.16 kernel - Mitch Dsouza 28th May 1994
36  * Adapted for 1.3.59 kernel - Andries Brouwer, 1 Feb 1996
37  *
38  * Fixed do_loop_request() re-entrancy - Vincent.Renardias@waw.com Mar 20, 1997
39  *
40  * Added devfs support - Richard Gooch <rgooch@atnf.csiro.au> 16-Jan-1998
41  *
42  * Handle sparse backing files correctly - Kenn Humborg, Jun 28, 1998
43  *
44  * Loadable modules and other fixes by AK, 1998
45  *
46  * Make real block number available to downstream transfer functions, enables
47  * CBC (and relatives) mode encryption requiring unique IVs per data block.
48  * Reed H. Petty, rhp@draper.net
49  *
50  * Maximum number of loop devices now dynamic via max_loop module parameter.
51  * Russell Kroll <rkroll@exploits.org> 19990701
52  *
53  * Maximum number of loop devices when compiled-in now selectable by passing
54  * max_loop=<1-255> to the kernel on boot.
55  * Erik I. Bols?, <eriki@himolde.no>, Oct 31, 1999
56  *
57  * Completely rewrite request handling to be make_request_fn style and
58  * non blocking, pushing work to a helper thread. Lots of fixes from
59  * Al Viro too.
60  * Jens Axboe <axboe@suse.de>, Nov 2000
61  *
62  * Support up to 256 loop devices
63  * Heinz Mauelshagen <mge@sistina.com>, Feb 2002
64  *
65  * Support for falling back on the write file operation when the address space
66  * operations prepare_write and/or commit_write are not available on the
67  * backing filesystem.
68  * Anton Altaparmakov, 16 Feb 2005
69  *
70  * Still To Fix:
71  * - Advisory locking is ignored here.
72  * - Should use an own CAP_* category instead of CAP_SYS_ADMIN
73  *
74  */
75
76 #ifndef AUTOCONF_INCLUDED
77 #include <linux/config.h>
78 #endif
79 #include <linux/module.h>
80
81 #include <linux/sched.h>
82 #include <linux/fs.h>
83 #include <linux/file.h>
84 #include <linux/stat.h>
85 #include <linux/errno.h>
86 #include <linux/major.h>
87 #include <linux/wait.h>
88 #include <linux/blkdev.h>
89 #include <linux/blkpg.h>
90 #include <linux/init.h>
91 #include <linux/smp_lock.h>
92 #include <linux/swap.h>
93 #include <linux/slab.h>
94 #include <linux/suspend.h>
95 #include <linux/writeback.h>
96 #include <linux/buffer_head.h>                /* for invalidate_bdev() */
97 #include <linux/completion.h>
98 #include <linux/highmem.h>
99 #include <linux/gfp.h>
100 #include <linux/swap.h>
101 #include <linux/pagevec.h>
102
103 #include <asm/uaccess.h>
104
105 #include <lustre_lib.h>
106 #include <lustre_lite.h>
107 #include "llite_internal.h"
108
109 #define LLOOP_MAX_SEGMENTS        PTLRPC_MAX_BRW_PAGES
110
111 /* Possible states of device */
112 enum {
113         LLOOP_UNBOUND,
114         LLOOP_BOUND,
115         LLOOP_RUNDOWN,
116 };
117
118 struct lloop_device {
119         int                lo_number;
120         int                lo_refcnt;
121         loff_t             lo_offset;
122         loff_t             lo_sizelimit;
123         int                lo_flags;
124         int                (*ioctl)(struct lloop_device *, int cmd, 
125                                  unsigned long arg); 
126
127         struct file *      lo_backing_file;
128         struct block_device *lo_device;
129         unsigned           lo_blocksize;
130
131         int                old_gfp_mask;
132
133         spinlock_t         lo_lock;
134         struct bio         *lo_bio;
135         struct bio         *lo_biotail;
136         int                lo_state;
137         struct semaphore   lo_sem;
138         struct semaphore   lo_ctl_mutex;
139         struct semaphore   lo_bh_mutex;
140         atomic_t           lo_pending;
141
142         request_queue_t    *lo_queue;
143
144         /* data to handle bio for lustre. */
145         struct lo_request_data {
146                 struct brw_page    lrd_pages[LLOOP_MAX_SEGMENTS];
147                 struct obdo        lrd_oa;
148         } lo_requests[1];
149
150 };
151
152 /*
153  * Loop flags
154  */
155 enum {
156         LO_FLAGS_READ_ONLY       = 1,
157 };
158
159 static int lloop_major;
160 static int max_loop = 8;
161 static struct lloop_device *loop_dev;
162 static struct gendisk **disks;
163 static struct semaphore lloop_mutex;
164 static void *ll_iocontrol_magic = NULL;
165
166 static loff_t get_loop_size(struct lloop_device *lo, struct file *file)
167 {
168         loff_t size, offset, loopsize;
169
170         /* Compute loopsize in bytes */
171         size = i_size_read(file->f_mapping->host);
172         offset = lo->lo_offset;
173         loopsize = size - offset;
174         if (lo->lo_sizelimit > 0 && lo->lo_sizelimit < loopsize)
175                 loopsize = lo->lo_sizelimit;
176
177         /*
178          * Unfortunately, if we want to do I/O on the device,
179          * the number of 512-byte sectors has to fit into a sector_t.
180          */
181         return loopsize >> 9;
182 }
183
184 static int do_bio_filebacked(struct lloop_device *lo, struct bio *bio)
185 {
186         struct inode *inode = lo->lo_backing_file->f_dentry->d_inode;
187         struct ll_inode_info *lli = ll_i2info(inode);
188         struct lov_stripe_md *lsm = lli->lli_smd;
189         struct obd_info oinfo = {{{ 0 }}};
190         struct brw_page *pg = lo->lo_requests[0].lrd_pages;
191         struct obdo *oa = &lo->lo_requests[0].lrd_oa;
192         pgoff_t offset;
193         int ret, cmd, i, opc;
194         struct bio_vec *bvec;
195
196         BUG_ON(bio->bi_hw_segments > LLOOP_MAX_SEGMENTS);
197
198         offset = (pgoff_t)(bio->bi_sector << 9) + lo->lo_offset;
199         bio_for_each_segment(bvec, bio, i) {
200                 BUG_ON(bvec->bv_offset != 0);
201                 BUG_ON(bvec->bv_len != CFS_PAGE_SIZE);
202
203                 pg->pg = bvec->bv_page;
204                 pg->off = offset;
205                 pg->count = bvec->bv_len;
206                 pg->flag = OBD_BRW_SRVLOCK;
207
208                 pg++;
209                 offset += bvec->bv_len;
210         }
211
212         oa->o_mode = inode->i_mode;
213         oa->o_id = lsm->lsm_object_id;
214         oa->o_gr = lsm->lsm_object_gr;
215         oa->o_valid = OBD_MD_FLID | OBD_MD_FLMODE |
216                       OBD_MD_FLTYPE |OBD_MD_FLGROUP;
217         obdo_from_inode(oa, inode, OBD_MD_FLFID | OBD_MD_FLGENER);
218
219         cmd = OBD_BRW_READ;
220         if (bio_rw(bio) == WRITE)
221                 cmd = OBD_BRW_WRITE;
222
223         if (cmd == OBD_BRW_WRITE)
224                 ll_stats_ops_tally(ll_i2sbi(inode), LPROC_LL_BRW_WRITE, bio->bi_size);
225         else
226                 ll_stats_ops_tally(ll_i2sbi(inode), LPROC_LL_BRW_READ, bio->bi_size);
227         oinfo.oi_oa = oa;
228         oinfo.oi_md = lsm;
229         opc = cmd & OBD_BRW_WRITE ? CAPA_OPC_OSS_WRITE : CAPA_OPC_OSS_RW;
230         oinfo.oi_capa = ll_osscapa_get(inode, opc);
231         ret = obd_brw(cmd, ll_i2dtexp(inode), &oinfo, 
232                       (obd_count)(i - bio->bi_idx), 
233                       lo->lo_requests[0].lrd_pages, NULL);
234         capa_put(oinfo.oi_capa);
235         if (ret == 0)
236                 obdo_to_inode(inode, oa, OBD_MD_FLBLOCKS);
237         return ret;
238 }
239
240
241 /*
242  * Add bio to back of pending list
243  */
244 static void loop_add_bio(struct lloop_device *lo, struct bio *bio)
245 {
246         unsigned long flags;
247
248         spin_lock_irqsave(&lo->lo_lock, flags);
249         if (lo->lo_biotail) {
250                 lo->lo_biotail->bi_next = bio;
251                 lo->lo_biotail = bio;
252         } else
253                 lo->lo_bio = lo->lo_biotail = bio;
254         spin_unlock_irqrestore(&lo->lo_lock, flags);
255
256         up(&lo->lo_bh_mutex);
257 }
258
259 /*
260  * Grab first pending buffer
261  */
262 static struct bio *loop_get_bio(struct lloop_device *lo)
263 {
264         struct bio *bio;
265
266         spin_lock_irq(&lo->lo_lock);
267         if ((bio = lo->lo_bio)) {
268                 if (bio == lo->lo_biotail)
269                         lo->lo_biotail = NULL;
270                 lo->lo_bio = bio->bi_next;
271                 bio->bi_next = NULL;
272         }
273         spin_unlock_irq(&lo->lo_lock);
274
275         return bio;
276 }
277
278 static int loop_make_request(request_queue_t *q, struct bio *old_bio)
279 {
280         struct lloop_device *lo = q->queuedata;
281         int rw = bio_rw(old_bio);
282
283         if (!lo)
284                 goto out;
285
286         spin_lock_irq(&lo->lo_lock);
287         if (lo->lo_state != LLOOP_BOUND)
288                 goto inactive;
289         atomic_inc(&lo->lo_pending);
290         spin_unlock_irq(&lo->lo_lock);
291
292         if (rw == WRITE) {
293                 if (lo->lo_flags & LO_FLAGS_READ_ONLY)
294                         goto err;
295         } else if (rw == READA) {
296                 rw = READ;
297         } else if (rw != READ) {
298                 CERROR("lloop: unknown command (%x)\n", rw);
299                 goto err;
300         }
301         loop_add_bio(lo, old_bio);
302         return 0;
303 err:
304         if (atomic_dec_and_test(&lo->lo_pending))
305                 up(&lo->lo_bh_mutex);
306 out:
307         bio_io_error(old_bio, old_bio->bi_size);
308         return 0;
309 inactive:
310         spin_unlock_irq(&lo->lo_lock);
311         goto out;
312 }
313
314 /*
315  * kick off io on the underlying address space
316  */
317 static void loop_unplug(request_queue_t *q)
318 {
319         struct lloop_device *lo = q->queuedata;
320
321         clear_bit(QUEUE_FLAG_PLUGGED, &q->queue_flags);
322         blk_run_address_space(lo->lo_backing_file->f_mapping);
323 }
324
325 static inline void loop_handle_bio(struct lloop_device *lo, struct bio *bio)
326 {
327         int ret;
328         ret = do_bio_filebacked(lo, bio);
329         bio_endio(bio, bio->bi_size, ret);
330 }
331
332 /*
333  * worker thread that handles reads/writes to file backed loop devices,
334  * to avoid blocking in our make_request_fn. it also does loop decrypting
335  * on reads for block backed loop, as that is too heavy to do from
336  * b_end_io context where irqs may be disabled.
337  */
338 static int loop_thread(void *data)
339 {
340         struct lloop_device *lo = data;
341         struct bio *bio;
342
343         daemonize("lloop%d", lo->lo_number);
344
345         set_user_nice(current, -20);
346
347         lo->lo_state = LLOOP_BOUND;
348         atomic_inc(&lo->lo_pending);
349
350         /*
351          * up sem, we are running
352          */
353         up(&lo->lo_sem);
354
355         for (;;) {
356                 down_interruptible(&lo->lo_bh_mutex);
357                 /*
358                  * could be upped because of tear-down, not because of
359                  * pending work
360                  */
361                 if (!atomic_read(&lo->lo_pending))
362                         break;
363
364                 bio = loop_get_bio(lo);
365                 if (!bio) {
366                         CWARN("lloop(minor: %d): missing bio\n", lo->lo_number);
367                         continue;
368                 }
369                 loop_handle_bio(lo, bio);
370
371                 /*
372                  * upped both for pending work and tear-down, lo_pending
373                  * will hit zero then
374                  */
375                 if (atomic_dec_and_test(&lo->lo_pending))
376                         break;
377         }
378
379         up(&lo->lo_sem);
380         return 0;
381 }
382
383 static int loop_set_fd(struct lloop_device *lo, struct file *unused,
384                        struct block_device *bdev, struct file *file)
385 {
386         struct inode        *inode;
387         struct address_space *mapping;
388         int                lo_flags = 0;
389         int                error;
390         loff_t                size;
391
392         if (!try_module_get(THIS_MODULE))
393                 return -ENODEV;
394
395         error = -EBUSY;
396         if (lo->lo_state != LLOOP_UNBOUND)
397                 goto out;
398
399         mapping = file->f_mapping;
400         inode = mapping->host;
401
402         error = -EINVAL;
403         if (!S_ISREG(inode->i_mode) || inode->i_sb->s_magic != LL_SUPER_MAGIC)
404                 goto out;
405
406         if (!(file->f_mode & FMODE_WRITE))
407                 lo_flags |= LO_FLAGS_READ_ONLY;
408
409         size = get_loop_size(lo, file);
410
411         if ((loff_t)(sector_t)size != size) {
412                 error = -EFBIG;
413                 goto out;
414         }
415
416         /* remove all pages in cache so as dirty pages not to be existent. */
417         truncate_inode_pages(mapping, 0);
418
419         set_device_ro(bdev, (lo_flags & LO_FLAGS_READ_ONLY) != 0);
420
421         lo->lo_blocksize = CFS_PAGE_SIZE;
422         lo->lo_device = bdev;
423         lo->lo_flags = lo_flags;
424         lo->lo_backing_file = file;
425         lo->ioctl = NULL;
426         lo->lo_sizelimit = 0;
427         lo->old_gfp_mask = mapping_gfp_mask(mapping);
428         mapping_set_gfp_mask(mapping, lo->old_gfp_mask & ~(__GFP_IO|__GFP_FS));
429
430         lo->lo_bio = lo->lo_biotail = NULL;
431
432         /*
433          * set queue make_request_fn, and add limits based on lower level
434          * device
435          */
436         blk_queue_make_request(lo->lo_queue, loop_make_request);
437         lo->lo_queue->queuedata = lo;
438         lo->lo_queue->unplug_fn = loop_unplug;
439
440         /* queue parameters */
441         blk_queue_hardsect_size(lo->lo_queue, CFS_PAGE_SIZE);
442         blk_queue_max_sectors(lo->lo_queue, LLOOP_MAX_SEGMENTS);
443         blk_queue_max_phys_segments(lo->lo_queue, LLOOP_MAX_SEGMENTS);
444
445         set_capacity(disks[lo->lo_number], size);
446         bd_set_size(bdev, size << 9);
447
448         set_blocksize(bdev, lo->lo_blocksize);
449
450         kernel_thread(loop_thread, lo, CLONE_KERNEL);
451         down(&lo->lo_sem);
452         return 0;
453
454  out:
455         /* This is safe: open() is still holding a reference. */
456         module_put(THIS_MODULE);
457         return error;
458 }
459
460 static int loop_clr_fd(struct lloop_device *lo, struct block_device *bdev, 
461                        int count)
462 {
463         struct file *filp = lo->lo_backing_file;
464         int gfp = lo->old_gfp_mask;
465
466         if (lo->lo_state != LLOOP_BOUND)
467                 return -ENXIO;
468
469         if (lo->lo_refcnt > count)        /* we needed one fd for the ioctl */
470                 return -EBUSY;
471
472         if (filp == NULL)
473                 return -EINVAL;
474
475         spin_lock_irq(&lo->lo_lock);
476         lo->lo_state = LLOOP_RUNDOWN;
477         if (atomic_dec_and_test(&lo->lo_pending))
478                 up(&lo->lo_bh_mutex);
479         spin_unlock_irq(&lo->lo_lock);
480
481         down(&lo->lo_sem);
482         lo->lo_backing_file = NULL;
483         lo->ioctl = NULL;
484         lo->lo_device = NULL;
485         lo->lo_offset = 0;
486         lo->lo_sizelimit = 0;
487         lo->lo_flags = 0;
488         ll_invalidate_bdev(bdev, 0);
489         set_capacity(disks[lo->lo_number], 0);
490         bd_set_size(bdev, 0);
491         mapping_set_gfp_mask(filp->f_mapping, gfp);
492         lo->lo_state = LLOOP_UNBOUND;
493         fput(filp);
494         /* This is safe: open() is still holding a reference. */
495         module_put(THIS_MODULE);
496         return 0;
497 }
498
499 static int lo_open(struct inode *inode, struct file *file)
500 {
501         struct lloop_device *lo = inode->i_bdev->bd_disk->private_data;
502
503         down(&lo->lo_ctl_mutex);
504         lo->lo_refcnt++;
505         up(&lo->lo_ctl_mutex);
506
507         return 0;
508 }
509
510 static int lo_release(struct inode *inode, struct file *file)
511 {
512         struct lloop_device *lo = inode->i_bdev->bd_disk->private_data;
513
514         down(&lo->lo_ctl_mutex);
515         --lo->lo_refcnt;
516         up(&lo->lo_ctl_mutex);
517
518         return 0;
519 }
520
521 /* lloop device node's ioctl function. */
522 static int lo_ioctl(struct inode *inode, struct file *unused, 
523         unsigned int cmd, unsigned long arg)
524 {
525         struct lloop_device *lo = inode->i_bdev->bd_disk->private_data;
526         struct block_device *bdev = inode->i_bdev;
527         int err = 0;
528
529         down(&lloop_mutex);
530         switch (cmd) {
531         case LL_IOC_LLOOP_DETACH: {
532                 err = loop_clr_fd(lo, bdev, 2);
533                 if (err == 0)
534                         blkdev_put(bdev); /* grabbed in LLOOP_ATTACH */
535                 break;
536         }
537
538         case LL_IOC_LLOOP_INFO: {
539                 __u64 ino = 0;
540
541                 if (lo->lo_state == LLOOP_BOUND)
542                         ino = lo->lo_backing_file->f_dentry->d_inode->i_ino;
543
544                 if (put_user(ino, (__u64 *)arg))
545                         err = -EFAULT;
546                 break; 
547         }
548
549         default:
550                 err = -EINVAL;
551                 break;
552         }
553         up(&lloop_mutex);
554
555         return err;
556 }
557
558 static struct block_device_operations lo_fops = {
559         .owner =        THIS_MODULE,
560         .open =         lo_open,
561         .release =      lo_release,
562         .ioctl =        lo_ioctl,
563 };
564
565 /* dynamic iocontrol callback. 
566  * This callback is registered in lloop_init and will be called by 
567  * ll_iocontrol_call. 
568  * This is a llite regular file ioctl function. It takes the responsibility 
569  * of attaching a file, and detaching a file by a lloop's device numner. 
570  */
571 static enum llioc_iter lloop_ioctl(struct inode *unused, struct file *file, 
572                 unsigned int cmd, unsigned long arg,
573                 void *magic, int *rcp)
574 {
575         struct lloop_device *lo = NULL;
576         struct block_device *bdev = NULL;
577         int err = 0;
578         dev_t dev;
579
580         if (magic != ll_iocontrol_magic)
581                 return LLIOC_CONT;
582
583         if (disks == NULL)
584                 GOTO(out1, err = -ENODEV);
585
586         CWARN("Enter llop_ioctl\n");
587
588         down(&lloop_mutex);
589         switch (cmd) {
590         case LL_IOC_LLOOP_ATTACH: {
591                 struct lloop_device *lo_free = NULL;
592                 int i;
593
594                 for (i = 0; i < max_loop; i++, lo = NULL) {
595                         lo = &loop_dev[i];
596                         if (lo->lo_state == LLOOP_UNBOUND) {
597                                 if (!lo_free)
598                                         lo_free = lo;
599                                 continue;
600                         }
601                         if (lo->lo_backing_file->f_dentry->d_inode == 
602                             file->f_dentry->d_inode)
603                                 break;
604                 }
605                 if (lo || !lo_free)
606                         GOTO(out, err = -EBUSY);
607
608                 lo = lo_free;
609                 dev = MKDEV(lloop_major, lo->lo_number);
610
611                 /* quit if the used pointer is writable */
612                 if (put_user((long)old_encode_dev(dev), (long*)arg))
613                         GOTO(out, err = -EFAULT);
614
615                 bdev = open_by_devnum(dev, file->f_mode);
616                 if (IS_ERR(bdev))
617                         GOTO(out, err = PTR_ERR(bdev));
618
619                 get_file(file);
620                 err = loop_set_fd(lo, NULL, bdev, file);
621                 if (err) {
622                         fput(file);
623                         blkdev_put(bdev);
624                 }
625
626                 break;
627         }
628
629         case LL_IOC_LLOOP_DETACH_BYDEV: {
630                 int minor;
631                 
632                 dev = old_decode_dev(arg);
633                 if (MAJOR(dev) != lloop_major)
634                         GOTO(out, err = -EINVAL);
635
636                 minor = MINOR(dev);
637                 if (minor > max_loop - 1)
638                         GOTO(out, err = -EINVAL);
639
640                 lo = &loop_dev[minor];
641                 if (lo->lo_state != LLOOP_BOUND)
642                         GOTO(out, err = -EINVAL);
643
644                 bdev = lo->lo_device;
645                 err = loop_clr_fd(lo, bdev, 1);
646                 if (err == 0)
647                         blkdev_put(bdev); /* grabbed in LLOOP_ATTACH */
648
649                 break;
650         }
651
652         default:
653                 err = -EINVAL;
654                 break;
655         }
656
657 out:
658         up(&lloop_mutex);
659 out1:
660         if (rcp)
661                 *rcp = err;
662         return LLIOC_STOP;
663 }
664
665 static int __init lloop_init(void)
666 {
667         int        i;
668         unsigned int cmdlist[] = {
669                 LL_IOC_LLOOP_ATTACH,
670                 LL_IOC_LLOOP_DETACH_BYDEV,
671         };
672
673         if (max_loop < 1 || max_loop > 256) {
674                 CWARN("lloop: invalid max_loop (must be between"
675                       " 1 and 256), using default (8)\n");
676                 max_loop = 8;
677         }
678
679         lloop_major = register_blkdev(0, "lloop");
680         if (lloop_major < 0)
681                 return -EIO;
682
683         ll_iocontrol_magic = ll_iocontrol_register(lloop_ioctl, 2, cmdlist);
684         if (ll_iocontrol_magic == NULL)
685                 goto out_mem1;
686
687         loop_dev = kmalloc(max_loop * sizeof(struct lloop_device), GFP_KERNEL);
688         if (!loop_dev)
689                 goto out_mem1;
690         memset(loop_dev, 0, max_loop * sizeof(struct lloop_device));
691
692         disks = kmalloc(max_loop * sizeof(struct gendisk *), GFP_KERNEL);
693         if (!disks)
694                 goto out_mem2;
695
696         for (i = 0; i < max_loop; i++) {
697                 disks[i] = alloc_disk(1);
698                 if (!disks[i])
699                         goto out_mem3;
700         }
701
702         init_MUTEX(&lloop_mutex);
703
704         for (i = 0; i < max_loop; i++) {
705                 struct lloop_device *lo = &loop_dev[i];
706                 struct gendisk *disk = disks[i];
707
708                 memset(lo, 0, sizeof(*lo));
709                 lo->lo_queue = blk_alloc_queue(GFP_KERNEL);
710                 if (!lo->lo_queue)
711                         goto out_mem4;
712
713                 init_MUTEX(&lo->lo_ctl_mutex);
714                 init_MUTEX_LOCKED(&lo->lo_sem);
715                 init_MUTEX_LOCKED(&lo->lo_bh_mutex);
716                 lo->lo_number = i;
717                 spin_lock_init(&lo->lo_lock);
718                 disk->major = lloop_major;
719                 disk->first_minor = i;
720                 disk->fops = &lo_fops;
721                 sprintf(disk->disk_name, "lloop%d", i);
722                 disk->private_data = lo;
723                 disk->queue = lo->lo_queue;
724         }
725
726         /* We cannot fail after we call this, so another loop!*/
727         for (i = 0; i < max_loop; i++)
728                 add_disk(disks[i]);
729         return 0;
730
731 out_mem4:
732         while (i--)
733                 blk_put_queue(loop_dev[i].lo_queue);
734         i = max_loop;
735 out_mem3:
736         while (i--)
737                 put_disk(disks[i]);
738         kfree(disks);
739 out_mem2:
740         kfree(loop_dev);
741 out_mem1:
742         unregister_blkdev(lloop_major, "lloop");
743         ll_iocontrol_unregister(ll_iocontrol_magic);
744         CERROR("lloop: ran out of memory\n");
745         return -ENOMEM;
746 }
747
748 static void lloop_exit(void)
749 {
750         int i;
751
752         ll_iocontrol_unregister(ll_iocontrol_magic);
753         for (i = 0; i < max_loop; i++) {
754                 del_gendisk(disks[i]);
755                 blk_put_queue(loop_dev[i].lo_queue);
756                 put_disk(disks[i]);
757         }
758         if (ll_unregister_blkdev(lloop_major, "lloop"))
759                 CWARN("lloop: cannot unregister blkdev\n");
760
761         kfree(disks);
762         kfree(loop_dev);
763 }
764
765 module_init(lloop_init);
766 module_exit(lloop_exit);
767
768 CFS_MODULE_PARM(max_loop, "i", int, 0444, "maximum of lloop_device");
769 MODULE_AUTHOR("Cluster File Systems, Inc. <info@clusterfs.com>");
770 MODULE_DESCRIPTION("Lustre virtual block device");
771 MODULE_LICENSE("GPL");