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