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