Whamcloud - gitweb
b=23781 fix obdo leak issue
[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
20  * http://www.sun.com/software/products/lustre/docs/GPLv2.pdf
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 (c) 2007, 2010, Oracle and/or its affiliates. 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  * Modularized and updated for 1.1.16 kernel - Mitch Dsouza 28th May 1994
46  * Adapted for 1.3.59 kernel - Andries Brouwer, 1 Feb 1996
47  *
48  * Fixed do_loop_request() re-entrancy - Vincent.Renardias@waw.com Mar 20, 1997
49  *
50  * Added devfs support - Richard Gooch <rgooch@atnf.csiro.au> 16-Jan-1998
51  *
52  * Handle sparse backing files correctly - Kenn Humborg, Jun 28, 1998
53  *
54  * Loadable modules and other fixes by AK, 1998
55  *
56  * Maximum number of loop devices now dynamic via max_loop module parameter.
57  * Russell Kroll <rkroll@exploits.org> 19990701
58  *
59  * Maximum number of loop devices when compiled-in now selectable by passing
60  * max_loop=<1-255> to the kernel on boot.
61  * Erik I. Bols?, <eriki@himolde.no>, Oct 31, 1999
62  *
63  * Completely rewrite request handling to be make_request_fn style and
64  * non blocking, pushing work to a helper thread. Lots of fixes from
65  * Al Viro too.
66  * Jens Axboe <axboe@suse.de>, Nov 2000
67  *
68  * Support up to 256 loop devices
69  * Heinz Mauelshagen <mge@sistina.com>, Feb 2002
70  *
71  * Support for falling back on the write file operation when the address space
72  * operations prepare_write and/or commit_write are not available on the
73  * backing filesystem.
74  * Anton Altaparmakov, 16 Feb 2005
75  *
76  * Still To Fix:
77  * - Advisory locking is ignored here.
78  * - Should use an own CAP_* category instead of CAP_SYS_ADMIN
79  *
80  */
81
82 #ifndef AUTOCONF_INCLUDED
83 #include <linux/config.h>
84 #endif
85 #include <linux/module.h>
86
87 #include <linux/sched.h>
88 #include <linux/fs.h>
89 #include <linux/file.h>
90 #include <linux/stat.h>
91 #include <linux/errno.h>
92 #include <linux/major.h>
93 #include <linux/wait.h>
94 #include <linux/blkdev.h>
95 #include <linux/blkpg.h>
96 #include <linux/init.h>
97 #include <linux/smp_lock.h>
98 #include <linux/swap.h>
99 #include <linux/slab.h>
100 #include <linux/suspend.h>
101 #include <linux/writeback.h>
102 #include <linux/buffer_head.h>                /* for invalidate_bdev() */
103 #include <linux/completion.h>
104 #include <linux/highmem.h>
105 #include <linux/gfp.h>
106 #include <linux/swap.h>
107
108 #include <asm/uaccess.h>
109
110 #include <lustre_lib.h>
111 #include <lustre_lite.h>
112 #include "llite_internal.h"
113
114 #define LLOOP_MAX_SEGMENTS    PTLRPC_MAX_BRW_PAGES
115
116 /* Possible states of device */
117 enum {
118         LLOOP_UNBOUND,
119         LLOOP_BOUND,
120         LLOOP_RUNDOWN,
121 };
122
123 struct lloop_device {
124         int                lo_number;
125         int                lo_refcnt;
126         loff_t             lo_offset;
127         loff_t             lo_sizelimit;
128         int                lo_flags;
129         int                (*ioctl)(struct lloop_device *, int cmd,
130                                     unsigned long arg);
131
132         struct file *      lo_backing_file;
133         struct block_device *lo_device;
134         unsigned           lo_blocksize;
135
136         int                old_gfp_mask;
137
138         spinlock_t         lo_lock;
139         struct bio         *lo_bio;
140         struct bio         *lo_biotail;
141         int                lo_state;
142         struct semaphore   lo_sem;
143         struct semaphore   lo_ctl_mutex;
144         atomic_t           lo_pending;
145         wait_queue_head_t  lo_bh_wait;
146
147         struct request_queue  *lo_queue;
148
149         /* data to handle bio for lustre. */
150         struct lo_request_data {
151                 struct brw_page    lrd_pages[LLOOP_MAX_SEGMENTS];
152                 struct obdo        lrd_oa;
153         } lo_requests[1];
154 };
155
156 /*
157  * Loop flags
158  */
159 enum {
160         LO_FLAGS_READ_ONLY       = 1,
161 };
162
163 #define MAX_LOOP_DEFAULT  16
164 static int lloop_major;
165 static int max_loop = MAX_LOOP_DEFAULT;
166 static struct lloop_device *loop_dev;
167 static struct gendisk **disks;
168 static struct semaphore lloop_mutex;
169 static void *ll_iocontrol_magic = NULL;
170
171 static loff_t get_loop_size(struct lloop_device *lo, struct file *file)
172 {
173         loff_t size, offset, loopsize;
174
175         /* Compute loopsize in bytes */
176         size = i_size_read(file->f_mapping->host);
177         offset = lo->lo_offset;
178         loopsize = size - offset;
179         if (lo->lo_sizelimit > 0 && lo->lo_sizelimit < loopsize)
180                 loopsize = lo->lo_sizelimit;
181
182         /*
183          * Unfortunately, if we want to do I/O on the device,
184          * the number of 512-byte sectors has to fit into a sector_t.
185          */
186         return loopsize >> 9;
187 }
188
189 static int do_bio_lustrebacked(struct lloop_device *lo, struct bio *head)
190 {
191         struct inode *inode = lo->lo_backing_file->f_dentry->d_inode;
192         struct ll_inode_info *lli = ll_i2info(inode);
193         struct lov_stripe_md *lsm = lli->lli_smd;
194         struct obd_info oinfo = {{{0}}};
195         struct brw_page *pg = lo->lo_requests[0].lrd_pages;
196         struct obdo *oa = &lo->lo_requests[0].lrd_oa;
197         pgoff_t offset;
198         int ret, i, rw;
199         obd_count page_count = 0;
200         struct bio_vec *bvec;
201         struct bio *bio;
202
203         LASSERT(head != NULL);
204
205         rw = head->bi_rw;
206         for (bio = head; bio != NULL; bio = bio->bi_next) {
207                 LASSERT(rw == bio->bi_rw);
208
209                 offset = (pgoff_t)(bio->bi_sector << 9) + lo->lo_offset;
210                 bio_for_each_segment(bvec, bio, i) {
211                         BUG_ON(bvec->bv_offset != 0);
212                         BUG_ON(bvec->bv_len != CFS_PAGE_SIZE);
213
214                         pg->pg = bvec->bv_page;
215                         pg->off = offset;
216                         pg->count = bvec->bv_len;
217                         pg->flag = OBD_BRW_SRVLOCK;
218
219                         CDEBUG(D_INFO, "index %lu offset "LPU64", count %u\n",
220                                pg->pg->index, pg->off, pg->count);
221                         pg++;
222                         page_count++;
223                         offset += bvec->bv_len;
224                 }
225                 LASSERT(page_count <= LLOOP_MAX_SEGMENTS);
226         }
227
228         ll_stats_ops_tally(ll_i2sbi(inode),
229                         (rw == WRITE) ? LPROC_LL_BRW_WRITE : LPROC_LL_BRW_READ,
230                         page_count << PAGE_CACHE_SHIFT);
231
232         oa->o_mode = inode->i_mode;
233         oa->o_id = lsm->lsm_object_id;
234         oa->o_gr = lsm->lsm_object_gr;
235         oa->o_valid = OBD_MD_FLID   | OBD_MD_FLGROUP |
236                       OBD_MD_FLMODE | OBD_MD_FLTYPE;
237         obdo_from_inode(oa, inode, OBD_MD_FLFID | OBD_MD_FLGENER);
238
239         oinfo.oi_oa = oa;
240         oinfo.oi_md = lsm;
241         ret = obd_brw((rw == WRITE) ? OBD_BRW_WRITE : OBD_BRW_READ,
242                       ll_i2obdexp(inode), &oinfo, (obd_count)page_count,
243                       lo->lo_requests[0].lrd_pages, NULL);
244         if (ret == 0)
245                 obdo_to_inode(inode, oa, OBD_MD_FLBLOCKS);
246         return ret;
247 }
248
249
250 /*
251  * Add bio to back of pending list
252  */
253 static void loop_add_bio(struct lloop_device *lo, struct bio *bio)
254 {
255         unsigned long flags;
256
257         spin_lock_irqsave(&lo->lo_lock, flags);
258         if (lo->lo_biotail) {
259                 lo->lo_biotail->bi_next = bio;
260                 lo->lo_biotail = bio;
261         } else
262                 lo->lo_bio = lo->lo_biotail = bio;
263         spin_unlock_irqrestore(&lo->lo_lock, flags);
264
265         atomic_inc(&lo->lo_pending);
266         if (waitqueue_active(&lo->lo_bh_wait))
267                 wake_up(&lo->lo_bh_wait);
268 }
269
270 /*
271  * Grab first pending buffer
272  */
273 static unsigned int loop_get_bio(struct lloop_device *lo, struct bio **req)
274 {
275         struct bio *first;
276         struct bio **bio;
277         unsigned int count = 0;
278         unsigned int page_count = 0;
279         int rw;
280
281         spin_lock_irq(&lo->lo_lock);
282         first = lo->lo_bio;
283         if (unlikely(first == NULL)) {
284                 spin_unlock_irq(&lo->lo_lock);
285                 return 0;
286         }
287
288         /* TODO: need to split the bio, too bad. */
289         LASSERT(first->bi_vcnt <= LLOOP_MAX_SEGMENTS);
290
291         rw = first->bi_rw;
292         bio = &lo->lo_bio;
293         while (*bio && (*bio)->bi_rw == rw) {
294                 CDEBUG(D_INFO, "bio sector %llu size %u count %u vcnt%u \n",
295                        (unsigned long long)(*bio)->bi_sector, (*bio)->bi_size,
296                        page_count, (*bio)->bi_vcnt);
297                 if (page_count + (*bio)->bi_vcnt > LLOOP_MAX_SEGMENTS)
298                         break;
299
300
301                 page_count += (*bio)->bi_vcnt;
302                 count++;
303                 bio = &(*bio)->bi_next;
304         }
305         if (*bio) {
306                 /* Some of bios can't be mergable. */
307                 lo->lo_bio = *bio;
308                 *bio = NULL;
309         } else {
310                 /* Hit the end of queue */
311                 lo->lo_biotail = NULL;
312                 lo->lo_bio = NULL;
313         }
314         *req = first;
315         spin_unlock_irq(&lo->lo_lock);
316         return count;
317 }
318
319 static int loop_make_request(struct request_queue *q, struct bio *old_bio)
320 {
321         struct lloop_device *lo = q->queuedata;
322         int rw = bio_rw(old_bio);
323         int inactive;
324
325         if (!lo)
326                 goto err;
327
328         CDEBUG(D_INFO, "submit bio sector %llu size %u\n",
329                (unsigned long long)old_bio->bi_sector, old_bio->bi_size);
330
331         spin_lock_irq(&lo->lo_lock);
332         inactive = (lo->lo_state != LLOOP_BOUND);
333         spin_unlock_irq(&lo->lo_lock);
334         if (inactive)
335                 goto err;
336
337         if (rw == WRITE) {
338                 if (lo->lo_flags & LO_FLAGS_READ_ONLY)
339                         goto err;
340         } else if (rw == READA) {
341                 rw = READ;
342         } else if (rw != READ) {
343                 CERROR("lloop: unknown command (%x)\n", rw);
344                 goto err;
345         }
346         loop_add_bio(lo, old_bio);
347         return 0;
348 err:
349         cfs_bio_io_error(old_bio, old_bio->bi_size);
350         return 0;
351 }
352
353 /*
354  * kick off io on the underlying address space
355  */
356 static void loop_unplug(struct request_queue *q)
357 {
358         struct lloop_device *lo = q->queuedata;
359
360         clear_bit(QUEUE_FLAG_PLUGGED, &q->queue_flags);
361         blk_run_address_space(lo->lo_backing_file->f_mapping);
362 }
363
364 static inline void loop_handle_bio(struct lloop_device *lo, struct bio *bio)
365 {
366         int ret;
367         ret = do_bio_lustrebacked(lo, bio);
368         while (bio) {
369                 struct bio *tmp = bio->bi_next;
370                 bio->bi_next = NULL;
371                 cfs_bio_endio(bio, bio->bi_size, ret);
372                 bio = tmp;
373         }
374 }
375
376 static inline int loop_active(struct lloop_device *lo)
377 {
378         return atomic_read(&lo->lo_pending) || (lo->lo_state == LLOOP_RUNDOWN);
379 }
380
381 /*
382  * worker thread that handles reads/writes to file backed loop devices,
383  * to avoid blocking in our make_request_fn.
384  */
385 static int loop_thread(void *data)
386 {
387         struct lloop_device *lo = data;
388         struct bio *bio;
389         unsigned int count;
390         unsigned long times = 0;
391         unsigned long total_count = 0;
392
393         daemonize("lloop%d", lo->lo_number);
394
395         set_user_nice(current, -20);
396
397         lo->lo_state = LLOOP_BOUND;
398
399         /*
400          * up sem, we are running
401          */
402         up(&lo->lo_sem);
403
404         for (;;) {
405                 wait_event(lo->lo_bh_wait, loop_active(lo));
406                 if (!atomic_read(&lo->lo_pending)) {
407                         int exiting = 0;
408                         spin_lock_irq(&lo->lo_lock);
409                         exiting = (lo->lo_state == LLOOP_RUNDOWN);
410                         spin_unlock_irq(&lo->lo_lock);
411                         if (exiting)
412                                 break;
413                 }
414
415                 bio = NULL;
416                 count = loop_get_bio(lo, &bio);
417                 if (!count) {
418                         CWARN("lloop(minor: %d): missing bio\n", lo->lo_number);
419                         continue;
420                 }
421
422                 total_count += count;
423                 if (total_count < count) {      /* overflow */
424                         total_count = count;
425                         times = 1;
426                 } else {
427                         times++;
428                 }
429                 if ((times & 127) == 0) {
430                         CDEBUG(D_INFO, "total: %lu, count: %lu, avg: %lu\n",
431                                total_count, times, total_count / times);
432                 }
433
434                 LASSERT(bio != NULL);
435                 LASSERT(count <= atomic_read(&lo->lo_pending));
436                 loop_handle_bio(lo, bio);
437                 atomic_sub(count, &lo->lo_pending);
438         }
439
440         up(&lo->lo_sem);
441         return 0;
442 }
443
444 static int loop_set_fd(struct lloop_device *lo, struct file *unused,
445                        struct block_device *bdev, struct file *file)
446 {
447         struct inode         *inode;
448         struct address_space *mapping;
449         int                   lo_flags = 0;
450         int                   error;
451         loff_t                size;
452
453         if (!try_module_get(THIS_MODULE))
454                 return -ENODEV;
455
456         error = -EBUSY;
457         if (lo->lo_state != LLOOP_UNBOUND)
458                 goto out;
459
460         mapping = file->f_mapping;
461         inode = mapping->host;
462
463         error = -EINVAL;
464         if (!S_ISREG(inode->i_mode) || inode->i_sb->s_magic != LL_SUPER_MAGIC)
465                 goto out;
466
467         if (!(file->f_mode & FMODE_WRITE))
468                 lo_flags |= LO_FLAGS_READ_ONLY;
469
470         size = get_loop_size(lo, file);
471
472         if ((loff_t)(sector_t)size != size) {
473                 error = -EFBIG;
474                 goto out;
475         }
476
477         /* remove all pages in cache so as dirty pages not to be existent. */
478         truncate_inode_pages(mapping, 0);
479
480         set_device_ro(bdev, (lo_flags & LO_FLAGS_READ_ONLY) != 0);
481
482         lo->lo_blocksize = CFS_PAGE_SIZE;
483         lo->lo_device = bdev;
484         lo->lo_flags = lo_flags;
485         lo->lo_backing_file = file;
486         lo->ioctl = NULL;
487         lo->lo_sizelimit = 0;
488         lo->old_gfp_mask = mapping_gfp_mask(mapping);
489         mapping_set_gfp_mask(mapping, lo->old_gfp_mask & ~(__GFP_IO|__GFP_FS));
490
491         lo->lo_bio = lo->lo_biotail = NULL;
492
493         /*
494          * set queue make_request_fn, and add limits based on lower level
495          * device
496          */
497         blk_queue_make_request(lo->lo_queue, loop_make_request);
498         lo->lo_queue->queuedata = lo;
499         lo->lo_queue->unplug_fn = loop_unplug;
500
501         /* queue parameters */
502         /*
503          * using unsigned type cast instead of unsigned short cast in order
504          * to avoid truncate of CFS_PAGE_SIZE (= 2**16) value
505          */
506         blk_queue_logical_block_size(lo->lo_queue,
507                                      min_t(unsigned, CFS_PAGE_SIZE, 16384));
508         blk_queue_max_sectors(lo->lo_queue,
509                               LLOOP_MAX_SEGMENTS << (CFS_PAGE_SHIFT - 9));
510         blk_queue_max_phys_segments(lo->lo_queue, LLOOP_MAX_SEGMENTS);
511         blk_queue_max_hw_segments(lo->lo_queue, LLOOP_MAX_SEGMENTS);
512
513         set_capacity(disks[lo->lo_number], size);
514         bd_set_size(bdev, size << 9);
515
516         set_blocksize(bdev, lo->lo_blocksize);
517
518         kernel_thread(loop_thread, lo, CLONE_KERNEL);
519         down(&lo->lo_sem);
520         return 0;
521
522  out:
523         /* This is safe: open() is still holding a reference. */
524         module_put(THIS_MODULE);
525         return error;
526 }
527
528 static int loop_clr_fd(struct lloop_device *lo, struct block_device *bdev,
529                        int count)
530 {
531         struct file *filp = lo->lo_backing_file;
532         int gfp = lo->old_gfp_mask;
533
534         if (lo->lo_state != LLOOP_BOUND)
535                 return -ENXIO;
536
537         if (lo->lo_refcnt > count)        /* we needed one fd for the ioctl */
538                 return -EBUSY;
539
540         if (filp == NULL)
541                 return -EINVAL;
542
543         spin_lock_irq(&lo->lo_lock);
544         lo->lo_state = LLOOP_RUNDOWN;
545         spin_unlock_irq(&lo->lo_lock);
546         wake_up(&lo->lo_bh_wait);
547
548         down(&lo->lo_sem);
549         lo->lo_backing_file = NULL;
550         lo->ioctl = NULL;
551         lo->lo_device = NULL;
552         lo->lo_offset = 0;
553         lo->lo_sizelimit = 0;
554         lo->lo_flags = 0;
555         ll_invalidate_bdev(bdev, 0);
556         set_capacity(disks[lo->lo_number], 0);
557         bd_set_size(bdev, 0);
558         mapping_set_gfp_mask(filp->f_mapping, gfp);
559         lo->lo_state = LLOOP_UNBOUND;
560         fput(filp);
561         /* This is safe: open() is still holding a reference. */
562         module_put(THIS_MODULE);
563         return 0;
564 }
565
566 #ifdef HAVE_BLKDEV_PUT_2ARGS
567 static int lo_open(struct block_device *bdev, fmode_t mode)
568 {
569         struct lloop_device *lo = bdev->bd_disk->private_data;
570 #else
571 static int lo_open(struct inode *inode, struct file *file)
572 {
573         struct lloop_device *lo = inode->i_bdev->bd_disk->private_data;
574 #endif
575
576         down(&lo->lo_ctl_mutex);
577         lo->lo_refcnt++;
578         up(&lo->lo_ctl_mutex);
579
580         return 0;
581 }
582
583 #ifdef HAVE_BLKDEV_PUT_2ARGS
584 static int lo_release(struct gendisk *disk, fmode_t mode)
585 {
586         struct lloop_device *lo = disk->private_data;
587 #else
588 static int lo_release(struct inode *inode, struct file *file)
589 {
590         struct lloop_device *lo = inode->i_bdev->bd_disk->private_data;
591 #endif
592
593         down(&lo->lo_ctl_mutex);
594         --lo->lo_refcnt;
595         up(&lo->lo_ctl_mutex);
596
597         return 0;
598 }
599
600 /* lloop device node's ioctl function. */
601 #ifdef HAVE_BLKDEV_PUT_2ARGS
602 static int lo_ioctl(struct block_device *bdev, fmode_t mode,
603                     unsigned int cmd, unsigned long arg)
604 {
605         struct lloop_device *lo = bdev->bd_disk->private_data;
606 #else
607 static int lo_ioctl(struct inode *inode, struct file *unused,
608                     unsigned int cmd, unsigned long arg)
609 {
610         struct lloop_device *lo = inode->i_bdev->bd_disk->private_data;
611         struct block_device *bdev = inode->i_bdev;
612 #endif
613         int err = 0;
614
615         down(&lloop_mutex);
616         switch (cmd) {
617         case LL_IOC_LLOOP_DETACH: {
618                 err = loop_clr_fd(lo, bdev, 2);
619                 if (err == 0)
620                         ll_blkdev_put(bdev, 0); /* grabbed in LLOOP_ATTACH */
621                 break;
622         }
623
624         case LL_IOC_LLOOP_INFO: {
625                 __u64 ino = 0;
626
627                 if (lo->lo_state == LLOOP_BOUND)
628                         ino = lo->lo_backing_file->f_dentry->d_inode->i_ino;
629
630                 if (put_user(ino, (__u64 *)arg))
631                         err = -EFAULT;
632                 break;
633         }
634
635         default:
636                 err = -EINVAL;
637                 break;
638         }
639         up(&lloop_mutex);
640
641         return err;
642 }
643
644 static struct block_device_operations lo_fops = {
645         .owner =        THIS_MODULE,
646         .open =         lo_open,
647         .release =      lo_release,
648         .ioctl =        lo_ioctl,
649 };
650
651 /* dynamic iocontrol callback.
652  * This callback is registered in lloop_init and will be called by
653  * ll_iocontrol_call.
654  *
655  * This is a llite regular file ioctl function. It takes the responsibility
656  * of attaching a file, and detaching a file by a lloop's device numner.
657  */
658 static enum llioc_iter lloop_ioctl(struct inode *unused, struct file *file,
659                                    unsigned int cmd, unsigned long arg,
660                                    void *magic, int *rcp)
661 {
662         struct lloop_device *lo = NULL;
663         struct block_device *bdev = NULL;
664         int err = 0;
665         dev_t dev;
666
667         if (magic != ll_iocontrol_magic)
668                 return LLIOC_CONT;
669
670         if (disks == NULL)
671                 GOTO(out1, err = -ENODEV);
672
673         down(&lloop_mutex);
674         switch (cmd) {
675         case LL_IOC_LLOOP_ATTACH: {
676                 struct lloop_device *lo_free = NULL;
677                 int i;
678
679                 for (i = 0; i < max_loop; i++, lo = NULL) {
680                         lo = &loop_dev[i];
681                         if (lo->lo_state == LLOOP_UNBOUND) {
682                                 if (!lo_free)
683                                         lo_free = lo;
684                                 continue;
685                         }
686                         if (lo->lo_backing_file->f_dentry->d_inode ==
687                             file->f_dentry->d_inode)
688                                 break;
689                 }
690                 if (lo || !lo_free)
691                         GOTO(out, err = -EBUSY);
692
693                 lo = lo_free;
694                 dev = MKDEV(lloop_major, lo->lo_number);
695
696                 /* quit if the used pointer is writable */
697                 if (put_user((long)old_encode_dev(dev), (long*)arg))
698                         GOTO(out, err = -EFAULT);
699
700                 bdev = open_by_devnum(dev, file->f_mode);
701                 if (IS_ERR(bdev))
702                         GOTO(out, err = PTR_ERR(bdev));
703
704                 get_file(file);
705                 err = loop_set_fd(lo, NULL, bdev, file);
706                 if (err) {
707                         fput(file);
708                         ll_blkdev_put(bdev, 0);
709                 }
710
711                 break;
712         }
713
714         case LL_IOC_LLOOP_DETACH_BYDEV: {
715                 int minor;
716
717                 dev = old_decode_dev(arg);
718                 if (MAJOR(dev) != lloop_major)
719                         GOTO(out, err = -EINVAL);
720
721                 minor = MINOR(dev);
722                 if (minor > max_loop - 1)
723                         GOTO(out, err = -EINVAL);
724
725                 lo = &loop_dev[minor];
726                 if (lo->lo_state != LLOOP_BOUND)
727                         GOTO(out, err = -EINVAL);
728
729                 bdev = lo->lo_device;
730                 err = loop_clr_fd(lo, bdev, 1);
731                 if (err == 0)
732                         ll_blkdev_put(bdev, 0); /* grabbed in LLOOP_ATTACH */
733
734                 break;
735         }
736
737         default:
738                 err = -EINVAL;
739                 break;
740         }
741
742 out:
743         up(&lloop_mutex);
744 out1:
745         if (rcp)
746                 *rcp = err;
747         return LLIOC_STOP;
748 }
749
750 static int __init lloop_init(void)
751 {
752         int        i;
753         unsigned int cmdlist[] = {
754                 LL_IOC_LLOOP_ATTACH,
755                 LL_IOC_LLOOP_DETACH_BYDEV,
756         };
757
758         if (max_loop < 1 || max_loop > 256) {
759                 max_loop = MAX_LOOP_DEFAULT;
760                 CWARN("lloop: invalid max_loop (must be between"
761                       " 1 and 256), using default (%u)\n", max_loop);
762         }
763
764         lloop_major = register_blkdev(0, "lloop");
765         if (lloop_major < 0)
766                 return -EIO;
767
768         CDEBUG(D_CONFIG, "registered lloop major %d with %u minors\n",
769                lloop_major, max_loop);
770
771         ll_iocontrol_magic = ll_iocontrol_register(lloop_ioctl, 2, cmdlist);
772         if (ll_iocontrol_magic == NULL)
773                 goto out_mem1;
774
775         OBD_ALLOC_WAIT(loop_dev, max_loop * sizeof(*loop_dev));
776         if (!loop_dev)
777                 goto out_mem1;
778
779         OBD_ALLOC_WAIT(disks, max_loop * sizeof(*disks));
780         if (!disks)
781                 goto out_mem2;
782
783         for (i = 0; i < max_loop; i++) {
784                 disks[i] = alloc_disk(1);
785                 if (!disks[i])
786                         goto out_mem3;
787         }
788
789         init_MUTEX(&lloop_mutex);
790
791         for (i = 0; i < max_loop; i++) {
792                 struct lloop_device *lo = &loop_dev[i];
793                 struct gendisk *disk = disks[i];
794
795                 lo->lo_queue = blk_alloc_queue(GFP_KERNEL);
796                 if (!lo->lo_queue)
797                         goto out_mem4;
798
799                 init_MUTEX(&lo->lo_ctl_mutex);
800                 init_MUTEX_LOCKED(&lo->lo_sem);
801                 init_waitqueue_head(&lo->lo_bh_wait);
802                 lo->lo_number = i;
803                 spin_lock_init(&lo->lo_lock);
804                 disk->major = lloop_major;
805                 disk->first_minor = i;
806                 disk->fops = &lo_fops;
807                 sprintf(disk->disk_name, "lloop%d", i);
808                 disk->private_data = lo;
809                 disk->queue = lo->lo_queue;
810         }
811
812         /* We cannot fail after we call this, so another loop!*/
813         for (i = 0; i < max_loop; i++)
814                 add_disk(disks[i]);
815         return 0;
816
817 out_mem4:
818         while (i--)
819                 blk_cleanup_queue(loop_dev[i].lo_queue);
820         i = max_loop;
821 out_mem3:
822         while (i--)
823                 put_disk(disks[i]);
824         OBD_FREE(disks, max_loop * sizeof(*disks));
825 out_mem2:
826         OBD_FREE(loop_dev, max_loop * sizeof(*loop_dev));
827 out_mem1:
828         unregister_blkdev(lloop_major, "lloop");
829         ll_iocontrol_unregister(ll_iocontrol_magic);
830         CERROR("lloop: ran out of memory\n");
831         return -ENOMEM;
832 }
833
834 static void lloop_exit(void)
835 {
836         int i;
837
838         ll_iocontrol_unregister(ll_iocontrol_magic);
839         for (i = 0; i < max_loop; i++) {
840                 del_gendisk(disks[i]);
841                 blk_cleanup_queue(loop_dev[i].lo_queue);
842                 put_disk(disks[i]);
843         }
844         if (ll_unregister_blkdev(lloop_major, "lloop"))
845                 CWARN("lloop: cannot unregister blkdev\n");
846         else
847                 CDEBUG(D_CONFIG, "unregistered lloop major %d\n", lloop_major);
848
849         OBD_FREE(disks, max_loop * sizeof(*disks));
850         OBD_FREE(loop_dev, max_loop * sizeof(*loop_dev));
851 }
852
853 module_init(lloop_init);
854 module_exit(lloop_exit);
855
856 CFS_MODULE_PARM(max_loop, "i", int, 0444, "maximum of lloop_device");
857 MODULE_AUTHOR("Sun Microsystems, Inc. <http://www.lustre.org/>");
858 MODULE_DESCRIPTION("Lustre virtual block device");
859 MODULE_LICENSE("GPL");