Whamcloud - gitweb
Fix off-by-one error in file length calculation.
[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 #include <linux/config.h>
77 #include <linux/module.h>
78
79 #include <linux/sched.h>
80 #include <linux/fs.h>
81 #include <linux/file.h>
82 #include <linux/stat.h>
83 #include <linux/errno.h>
84 #include <linux/major.h>
85 #include <linux/wait.h>
86 #include <linux/blkdev.h>
87 #include <linux/blkpg.h>
88 #include <linux/init.h>
89 #include <linux/smp_lock.h>
90 #include <linux/swap.h>
91 #include <linux/slab.h>
92 #include <linux/suspend.h>
93 #include <linux/writeback.h>
94 #include <linux/buffer_head.h>                /* for invalidate_bdev() */
95 #include <linux/completion.h>
96 #include <linux/highmem.h>
97 #include <linux/gfp.h>
98 #include <linux/swap.h>
99 #include <linux/pagevec.h>
100
101 #include <asm/uaccess.h>
102
103 #include <lustre_lib.h>
104 #include <lustre_lite.h>
105 #include "llite_internal.h"
106
107 #define LLOOP_MAX_SEGMENTS        PTLRPC_MAX_BRW_PAGES
108
109 /* Possible states of device */
110 enum {
111         LLOOP_UNBOUND,
112         LLOOP_BOUND,
113         LLOOP_RUNDOWN,
114 };
115
116 struct lloop_device {
117         int                lo_number;
118         int                lo_refcnt;
119         loff_t             lo_offset;
120         loff_t             lo_sizelimit;
121         int                lo_flags;
122         int                (*ioctl)(struct lloop_device *, int cmd, 
123                                  unsigned long arg); 
124
125         struct file *      lo_backing_file;
126         struct block_device *lo_device;
127         unsigned           lo_blocksize;
128
129         int                old_gfp_mask;
130
131         spinlock_t         lo_lock;
132         struct bio         *lo_bio;
133         struct bio         *lo_biotail;
134         int                lo_state;
135         struct semaphore   lo_sem;
136         struct semaphore   lo_ctl_mutex;
137         struct semaphore   lo_bh_mutex;
138         atomic_t           lo_pending;
139
140         request_queue_t    *lo_queue;
141
142         /* data to handle bio for lustre. */
143         struct lo_request_data {
144                 struct brw_page    lrd_pages[LLOOP_MAX_SEGMENTS];
145                 struct obdo        lrd_oa;
146         } lo_requests[1];
147
148 };
149
150 /*
151  * Loop flags
152  */
153 enum {
154         LO_FLAGS_READ_ONLY       = 1,
155 };
156
157 static int lloop_major;
158 static int max_loop = 8;
159 static struct lloop_device *loop_dev;
160 static struct gendisk **disks;
161 static struct semaphore lloop_mutex;
162 static void *ll_iocontrol_magic = NULL;
163
164 static loff_t get_loop_size(struct lloop_device *lo, struct file *file)
165 {
166         loff_t size, offset, loopsize;
167
168         /* Compute loopsize in bytes */
169         size = i_size_read(file->f_mapping->host);
170         offset = lo->lo_offset;
171         loopsize = size - offset;
172         if (lo->lo_sizelimit > 0 && lo->lo_sizelimit < loopsize)
173                 loopsize = lo->lo_sizelimit;
174
175         /*
176          * Unfortunately, if we want to do I/O on the device,
177          * the number of 512-byte sectors has to fit into a sector_t.
178          */
179         return loopsize >> 9;
180 }
181
182 static int do_bio_filebacked(struct lloop_device *lo, struct bio *bio)
183 {
184         struct inode *inode = lo->lo_backing_file->f_dentry->d_inode;
185         struct ll_inode_info *lli = ll_i2info(inode);
186         struct lov_stripe_md *lsm = lli->lli_smd;
187         struct obd_info oinfo = {{{ 0 }}};
188         struct brw_page *pg = lo->lo_requests[0].lrd_pages;
189         struct obdo *oa = &lo->lo_requests[0].lrd_oa;
190         pgoff_t offset;
191         int ret, cmd, i, opc;
192         struct bio_vec *bvec;
193
194         BUG_ON(bio->bi_hw_segments > LLOOP_MAX_SEGMENTS);
195
196         offset = (pgoff_t)(bio->bi_sector << 9) + lo->lo_offset;
197         bio_for_each_segment(bvec, bio, i) {
198                 BUG_ON(bvec->bv_offset != 0);
199                 BUG_ON(bvec->bv_len != CFS_PAGE_SIZE);
200
201                 pg->pg = bvec->bv_page;
202                 pg->off = offset;
203                 pg->count = bvec->bv_len;
204                 pg->flag = OBD_BRW_SRVLOCK;
205
206                 pg++;
207                 offset += bvec->bv_len;
208         }
209
210         oa->o_mode = inode->i_mode;
211         oa->o_id = lsm->lsm_object_id;
212         oa->o_gr = lsm->lsm_object_gr;
213         oa->o_valid = OBD_MD_FLID | OBD_MD_FLMODE |
214                       OBD_MD_FLTYPE |OBD_MD_FLGROUP;
215         obdo_from_inode(oa, inode, OBD_MD_FLFID | OBD_MD_FLGENER);
216
217         cmd = OBD_BRW_READ;
218         if (bio_rw(bio) == WRITE)
219                 cmd = OBD_BRW_WRITE;
220
221         if (cmd == OBD_BRW_WRITE)
222                 ll_stats_ops_tally(ll_i2sbi(inode), LPROC_LL_BRW_WRITE, bio->bi_size);
223         else
224                 ll_stats_ops_tally(ll_i2sbi(inode), LPROC_LL_BRW_READ, bio->bi_size);
225         oinfo.oi_oa = oa;
226         oinfo.oi_md = lsm;
227         opc = cmd & OBD_BRW_WRITE ? CAPA_OPC_OSS_WRITE : CAPA_OPC_OSS_RW;
228         oinfo.oi_capa = ll_osscapa_get(inode, opc);
229         ret = obd_brw(cmd, ll_i2dtexp(inode), &oinfo, 
230                       (obd_count)(i - bio->bi_idx), 
231                       lo->lo_requests[0].lrd_pages, NULL);
232         capa_put(oinfo.oi_capa);
233         if (ret == 0)
234                 obdo_to_inode(inode, oa, OBD_MD_FLBLOCKS);
235         return ret;
236 }
237
238
239 /*
240  * Add bio to back of pending list
241  */
242 static void loop_add_bio(struct lloop_device *lo, struct bio *bio)
243 {
244         unsigned long flags;
245
246         spin_lock_irqsave(&lo->lo_lock, flags);
247         if (lo->lo_biotail) {
248                 lo->lo_biotail->bi_next = bio;
249                 lo->lo_biotail = bio;
250         } else
251                 lo->lo_bio = lo->lo_biotail = bio;
252         spin_unlock_irqrestore(&lo->lo_lock, flags);
253
254         up(&lo->lo_bh_mutex);
255 }
256
257 /*
258  * Grab first pending buffer
259  */
260 static struct bio *loop_get_bio(struct lloop_device *lo)
261 {
262         struct bio *bio;
263
264         spin_lock_irq(&lo->lo_lock);
265         if ((bio = lo->lo_bio)) {
266                 if (bio == lo->lo_biotail)
267                         lo->lo_biotail = NULL;
268                 lo->lo_bio = bio->bi_next;
269                 bio->bi_next = NULL;
270         }
271         spin_unlock_irq(&lo->lo_lock);
272
273         return bio;
274 }
275
276 static int loop_make_request(request_queue_t *q, struct bio *old_bio)
277 {
278         struct lloop_device *lo = q->queuedata;
279         int rw = bio_rw(old_bio);
280
281         if (!lo)
282                 goto out;
283
284         spin_lock_irq(&lo->lo_lock);
285         if (lo->lo_state != LLOOP_BOUND)
286                 goto inactive;
287         atomic_inc(&lo->lo_pending);
288         spin_unlock_irq(&lo->lo_lock);
289
290         if (rw == WRITE) {
291                 if (lo->lo_flags & LO_FLAGS_READ_ONLY)
292                         goto err;
293         } else if (rw == READA) {
294                 rw = READ;
295         } else if (rw != READ) {
296                 CERROR("lloop: unknown command (%x)\n", rw);
297                 goto err;
298         }
299         loop_add_bio(lo, old_bio);
300         return 0;
301 err:
302         if (atomic_dec_and_test(&lo->lo_pending))
303                 up(&lo->lo_bh_mutex);
304 out:
305         bio_io_error(old_bio, old_bio->bi_size);
306         return 0;
307 inactive:
308         spin_unlock_irq(&lo->lo_lock);
309         goto out;
310 }
311
312 /*
313  * kick off io on the underlying address space
314  */
315 static void loop_unplug(request_queue_t *q)
316 {
317         struct lloop_device *lo = q->queuedata;
318
319         clear_bit(QUEUE_FLAG_PLUGGED, &q->queue_flags);
320         blk_run_address_space(lo->lo_backing_file->f_mapping);
321 }
322
323 static inline void loop_handle_bio(struct lloop_device *lo, struct bio *bio)
324 {
325         int ret;
326         ret = do_bio_filebacked(lo, bio);
327         bio_endio(bio, bio->bi_size, ret);
328 }
329
330 /*
331  * worker thread that handles reads/writes to file backed loop devices,
332  * to avoid blocking in our make_request_fn. it also does loop decrypting
333  * on reads for block backed loop, as that is too heavy to do from
334  * b_end_io context where irqs may be disabled.
335  */
336 static int loop_thread(void *data)
337 {
338         struct lloop_device *lo = data;
339         struct bio *bio;
340
341         daemonize("lloop%d", lo->lo_number);
342
343         set_user_nice(current, -20);
344
345         lo->lo_state = LLOOP_BOUND;
346         atomic_inc(&lo->lo_pending);
347
348         /*
349          * up sem, we are running
350          */
351         up(&lo->lo_sem);
352
353         for (;;) {
354                 down_interruptible(&lo->lo_bh_mutex);
355                 /*
356                  * could be upped because of tear-down, not because of
357                  * pending work
358                  */
359                 if (!atomic_read(&lo->lo_pending))
360                         break;
361
362                 bio = loop_get_bio(lo);
363                 if (!bio) {
364                         CWARN("lloop(minor: %d): missing bio\n", lo->lo_number);
365                         continue;
366                 }
367                 loop_handle_bio(lo, bio);
368
369                 /*
370                  * upped both for pending work and tear-down, lo_pending
371                  * will hit zero then
372                  */
373                 if (atomic_dec_and_test(&lo->lo_pending))
374                         break;
375         }
376
377         up(&lo->lo_sem);
378         return 0;
379 }
380
381 static int loop_set_fd(struct lloop_device *lo, struct file *unused,
382                        struct block_device *bdev, struct file *file)
383 {
384         struct inode        *inode;
385         struct address_space *mapping;
386         int                lo_flags = 0;
387         int                error;
388         loff_t                size;
389
390         if (!try_module_get(THIS_MODULE))
391                 return -ENODEV;
392
393         error = -EBUSY;
394         if (lo->lo_state != LLOOP_UNBOUND)
395                 goto out;
396
397         mapping = file->f_mapping;
398         inode = mapping->host;
399
400         error = -EINVAL;
401         if (!S_ISREG(inode->i_mode) || inode->i_sb->s_magic != LL_SUPER_MAGIC)
402                 goto out;
403
404         if (!(file->f_mode & FMODE_WRITE))
405                 lo_flags |= LO_FLAGS_READ_ONLY;
406
407         size = get_loop_size(lo, file);
408
409         if ((loff_t)(sector_t)size != size) {
410                 error = -EFBIG;
411                 goto out;
412         }
413
414         /* remove all pages in cache so as dirty pages not to be existent. */
415         truncate_inode_pages(mapping, 0);
416
417         set_device_ro(bdev, (lo_flags & LO_FLAGS_READ_ONLY) != 0);
418
419         lo->lo_blocksize = CFS_PAGE_SIZE;
420         lo->lo_device = bdev;
421         lo->lo_flags = lo_flags;
422         lo->lo_backing_file = file;
423         lo->ioctl = NULL;
424         lo->lo_sizelimit = 0;
425         lo->old_gfp_mask = mapping_gfp_mask(mapping);
426         mapping_set_gfp_mask(mapping, lo->old_gfp_mask & ~(__GFP_IO|__GFP_FS));
427
428         lo->lo_bio = lo->lo_biotail = NULL;
429
430         /*
431          * set queue make_request_fn, and add limits based on lower level
432          * device
433          */
434         blk_queue_make_request(lo->lo_queue, loop_make_request);
435         lo->lo_queue->queuedata = lo;
436         lo->lo_queue->unplug_fn = loop_unplug;
437
438         /* queue parameters */
439         blk_queue_hardsect_size(lo->lo_queue, CFS_PAGE_SIZE);
440         blk_queue_max_sectors(lo->lo_queue, LLOOP_MAX_SEGMENTS);
441         blk_queue_max_phys_segments(lo->lo_queue, LLOOP_MAX_SEGMENTS);
442
443         set_capacity(disks[lo->lo_number], size);
444         bd_set_size(bdev, size << 9);
445
446         set_blocksize(bdev, lo->lo_blocksize);
447
448         kernel_thread(loop_thread, lo, CLONE_KERNEL);
449         down(&lo->lo_sem);
450         return 0;
451
452  out:
453         /* This is safe: open() is still holding a reference. */
454         module_put(THIS_MODULE);
455         return error;
456 }
457
458 static int loop_clr_fd(struct lloop_device *lo, struct block_device *bdev, 
459                        int count)
460 {
461         struct file *filp = lo->lo_backing_file;
462         int gfp = lo->old_gfp_mask;
463
464         if (lo->lo_state != LLOOP_BOUND)
465                 return -ENXIO;
466
467         if (lo->lo_refcnt > count)        /* we needed one fd for the ioctl */
468                 return -EBUSY;
469
470         if (filp == NULL)
471                 return -EINVAL;
472
473         spin_lock_irq(&lo->lo_lock);
474         lo->lo_state = LLOOP_RUNDOWN;
475         if (atomic_dec_and_test(&lo->lo_pending))
476                 up(&lo->lo_bh_mutex);
477         spin_unlock_irq(&lo->lo_lock);
478
479         down(&lo->lo_sem);
480         lo->lo_backing_file = NULL;
481         lo->ioctl = NULL;
482         lo->lo_device = NULL;
483         lo->lo_offset = 0;
484         lo->lo_sizelimit = 0;
485         lo->lo_flags = 0;
486         invalidate_bdev(bdev, 0);
487         set_capacity(disks[lo->lo_number], 0);
488         bd_set_size(bdev, 0);
489         mapping_set_gfp_mask(filp->f_mapping, gfp);
490         lo->lo_state = LLOOP_UNBOUND;
491         fput(filp);
492         /* This is safe: open() is still holding a reference. */
493         module_put(THIS_MODULE);
494         return 0;
495 }
496
497 static int lo_open(struct inode *inode, struct file *file)
498 {
499         struct lloop_device *lo = inode->i_bdev->bd_disk->private_data;
500
501         down(&lo->lo_ctl_mutex);
502         lo->lo_refcnt++;
503         up(&lo->lo_ctl_mutex);
504
505         return 0;
506 }
507
508 static int lo_release(struct inode *inode, struct file *file)
509 {
510         struct lloop_device *lo = inode->i_bdev->bd_disk->private_data;
511
512         down(&lo->lo_ctl_mutex);
513         --lo->lo_refcnt;
514         up(&lo->lo_ctl_mutex);
515
516         return 0;
517 }
518
519 /* lloop device node's ioctl function. */
520 static int lo_ioctl(struct inode *inode, struct file *unused, 
521         unsigned int cmd, unsigned long arg)
522 {
523         struct lloop_device *lo = inode->i_bdev->bd_disk->private_data;
524         struct block_device *bdev = inode->i_bdev;
525         int err = 0;
526
527         down(&lloop_mutex);
528         switch (cmd) {
529         case LL_IOC_LLOOP_DETACH: {
530                 err = loop_clr_fd(lo, bdev, 2);
531                 if (err == 0)
532                         blkdev_put(bdev); /* grabbed in LLOOP_ATTACH */
533                 break;
534         }
535
536         case LL_IOC_LLOOP_INFO: {
537                 __u64 ino = 0;
538
539                 if (lo->lo_state == LLOOP_BOUND)
540                         ino = lo->lo_backing_file->f_dentry->d_inode->i_ino;
541
542                 if (put_user(ino, (__u64 *)arg))
543                         err = -EFAULT;
544                 break; 
545         }
546
547         default:
548                 err = -EINVAL;
549                 break;
550         }
551         up(&lloop_mutex);
552
553         return err;
554 }
555
556 static struct block_device_operations lo_fops = {
557         .owner =        THIS_MODULE,
558         .open =         lo_open,
559         .release =      lo_release,
560         .ioctl =        lo_ioctl,
561 };
562
563 /* dynamic iocontrol callback. 
564  * This callback is registered in lloop_init and will be called by 
565  * ll_iocontrol_call. 
566  * This is a llite regular file ioctl function. It takes the responsibility 
567  * of attaching a file, and detaching a file by a lloop's device numner. 
568  */
569 static enum llioc_iter lloop_ioctl(struct inode *unused, struct file *file, 
570                 unsigned int cmd, unsigned long arg,
571                 void *magic, int *rcp)
572 {
573         struct lloop_device *lo = NULL;
574         struct block_device *bdev = NULL;
575         int err = 0;
576         dev_t dev;
577
578         if (magic != ll_iocontrol_magic)
579                 return LLIOC_CONT;
580
581         if (disks == NULL)
582                 GOTO(out1, err = -ENODEV);
583
584         CWARN("Enter llop_ioctl\n");
585
586         down(&lloop_mutex);
587         switch (cmd) {
588         case LL_IOC_LLOOP_ATTACH: {
589                 struct lloop_device *lo_free = NULL;
590                 int i;
591
592                 for (i = 0; i < max_loop; i++, lo = NULL) {
593                         lo = &loop_dev[i];
594                         if (lo->lo_state == LLOOP_UNBOUND) {
595                                 if (!lo_free)
596                                         lo_free = lo;
597                                 continue;
598                         }
599                         if (lo->lo_backing_file->f_dentry->d_inode == 
600                             file->f_dentry->d_inode)
601                                 break;
602                 }
603                 if (lo || !lo_free)
604                         GOTO(out, err = -EBUSY);
605
606                 lo = lo_free;
607                 dev = MKDEV(lloop_major, lo->lo_number);
608
609                 /* quit if the used pointer is writable */
610                 if (put_user((long)old_encode_dev(dev), (long*)arg))
611                         GOTO(out, err = -EFAULT);
612
613                 bdev = open_by_devnum(dev, file->f_mode);
614                 if (IS_ERR(bdev))
615                         GOTO(out, err = PTR_ERR(bdev));
616
617                 get_file(file);
618                 err = loop_set_fd(lo, NULL, bdev, file);
619                 if (err) {
620                         fput(file);
621                         blkdev_put(bdev);
622                 }
623
624                 break;
625         }
626
627         case LL_IOC_LLOOP_DETACH_BYDEV: {
628                 int minor;
629                 
630                 dev = old_decode_dev(arg);
631                 if (MAJOR(dev) != lloop_major)
632                         GOTO(out, err = -EINVAL);
633
634                 minor = MINOR(dev);
635                 if (minor > max_loop - 1)
636                         GOTO(out, err = -EINVAL);
637
638                 lo = &loop_dev[minor];
639                 if (lo->lo_state != LLOOP_BOUND)
640                         GOTO(out, err = -EINVAL);
641
642                 bdev = lo->lo_device;
643                 err = loop_clr_fd(lo, bdev, 1);
644                 if (err == 0)
645                         blkdev_put(bdev); /* grabbed in LLOOP_ATTACH */
646
647                 break;
648         }
649
650         default:
651                 err = -EINVAL;
652                 break;
653         }
654
655 out:
656         up(&lloop_mutex);
657 out1:
658         if (rcp)
659                 *rcp = err;
660         return LLIOC_STOP;
661 }
662
663 static int __init lloop_init(void)
664 {
665         int        i;
666         unsigned int cmdlist[] = {
667                 LL_IOC_LLOOP_ATTACH,
668                 LL_IOC_LLOOP_DETACH_BYDEV,
669         };
670
671         if (max_loop < 1 || max_loop > 256) {
672                 CWARN("lloop: invalid max_loop (must be between"
673                       " 1 and 256), using default (8)\n");
674                 max_loop = 8;
675         }
676
677         lloop_major = register_blkdev(0, "lloop");
678         if (lloop_major < 0)
679                 return -EIO;
680
681         ll_iocontrol_magic = ll_iocontrol_register(lloop_ioctl, 2, cmdlist);
682         if (ll_iocontrol_magic == NULL)
683                 goto out_mem1;
684
685         loop_dev = kmalloc(max_loop * sizeof(struct lloop_device), GFP_KERNEL);
686         if (!loop_dev)
687                 goto out_mem1;
688         memset(loop_dev, 0, max_loop * sizeof(struct lloop_device));
689
690         disks = kmalloc(max_loop * sizeof(struct gendisk *), GFP_KERNEL);
691         if (!disks)
692                 goto out_mem2;
693
694         for (i = 0; i < max_loop; i++) {
695                 disks[i] = alloc_disk(1);
696                 if (!disks[i])
697                         goto out_mem3;
698         }
699
700         init_MUTEX(&lloop_mutex);
701
702         for (i = 0; i < max_loop; i++) {
703                 struct lloop_device *lo = &loop_dev[i];
704                 struct gendisk *disk = disks[i];
705
706                 memset(lo, 0, sizeof(*lo));
707                 lo->lo_queue = blk_alloc_queue(GFP_KERNEL);
708                 if (!lo->lo_queue)
709                         goto out_mem4;
710
711                 init_MUTEX(&lo->lo_ctl_mutex);
712                 init_MUTEX_LOCKED(&lo->lo_sem);
713                 init_MUTEX_LOCKED(&lo->lo_bh_mutex);
714                 lo->lo_number = i;
715                 spin_lock_init(&lo->lo_lock);
716                 disk->major = lloop_major;
717                 disk->first_minor = i;
718                 disk->fops = &lo_fops;
719                 sprintf(disk->disk_name, "lloop%d", i);
720                 disk->private_data = lo;
721                 disk->queue = lo->lo_queue;
722         }
723
724         /* We cannot fail after we call this, so another loop!*/
725         for (i = 0; i < max_loop; i++)
726                 add_disk(disks[i]);
727         return 0;
728
729 out_mem4:
730         while (i--)
731                 blk_put_queue(loop_dev[i].lo_queue);
732         i = max_loop;
733 out_mem3:
734         while (i--)
735                 put_disk(disks[i]);
736         kfree(disks);
737 out_mem2:
738         kfree(loop_dev);
739 out_mem1:
740         unregister_blkdev(lloop_major, "lloop");
741         ll_iocontrol_unregister(ll_iocontrol_magic);
742         CERROR("lloop: ran out of memory\n");
743         return -ENOMEM;
744 }
745
746 static void lloop_exit(void)
747 {
748         int i;
749
750         ll_iocontrol_unregister(ll_iocontrol_magic);
751         for (i = 0; i < max_loop; i++) {
752                 del_gendisk(disks[i]);
753                 blk_put_queue(loop_dev[i].lo_queue);
754                 put_disk(disks[i]);
755         }
756         if (unregister_blkdev(lloop_major, "lloop"))
757                 CWARN("lloop: cannot unregister blkdev\n");
758
759         kfree(disks);
760         kfree(loop_dev);
761 }
762
763 module_init(lloop_init);
764 module_exit(lloop_exit);
765
766 CFS_MODULE_PARM(max_loop, "i", int, 0444, "maximum of lloop_device");