Whamcloud - gitweb
LU-812 kernel: remove smp_lock.h
[fs/lustre-release.git] / lustre / llite / lloop.c
1 /*
2  * GPL HEADER START
3  *
4  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License version 2 only,
8  * as published by the Free Software Foundation.
9  *
10  * This program is distributed in the hope that it will be useful, but
11  * WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13  * General Public License version 2 for more details (a copy is included
14  * in the LICENSE file that accompanied this code).
15  *
16  * You should have received a copy of the GNU General Public License
17  * version 2 along with this program; If not, see
18  * http://www.sun.com/software/products/lustre/docs/GPLv2.pdf
19  *
20  * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
21  * CA 95054 USA or visit www.sun.com if you need additional information or
22  * have any questions.
23  *
24  * GPL HEADER END
25  */
26 /*
27  * Copyright (c) 2007, 2010, Oracle and/or its affiliates. All rights reserved.
28  * Use is subject to license terms.
29  *
30  * Copyright (c) 2011, Whamcloud, Inc.
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/swap.h>
98 #include <linux/slab.h>
99 #include <linux/suspend.h>
100 #include <linux/writeback.h>
101 #include <linux/buffer_head.h>                /* for invalidate_bdev() */
102 #include <linux/completion.h>
103 #include <linux/highmem.h>
104 #include <linux/gfp.h>
105 #include <linux/swap.h>
106 #include <linux/pagevec.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         cfs_spinlock_t       lo_lock;
139         struct bio          *lo_bio;
140         struct bio          *lo_biotail;
141         int                  lo_state;
142         cfs_semaphore_t      lo_sem;
143         cfs_mutex_t          lo_ctl_mutex;
144         cfs_atomic_t         lo_pending;
145         cfs_waitq_t          lo_bh_wait;
146
147         struct request_queue *lo_queue;
148
149         const struct lu_env *lo_env;
150         struct cl_io         lo_io;
151         struct ll_dio_pages  lo_pvec;
152
153         /* data to handle bio for lustre. */
154         struct lo_request_data {
155                 struct page *lrd_pages[LLOOP_MAX_SEGMENTS];
156                 loff_t       lrd_offsets[LLOOP_MAX_SEGMENTS];
157         } lo_requests[1];
158 };
159
160 /*
161  * Loop flags
162  */
163 enum {
164         LO_FLAGS_READ_ONLY       = 1,
165 };
166
167 static int lloop_major;
168 #define MAX_LOOP_DEFAULT  16
169 static int max_loop = MAX_LOOP_DEFAULT;
170 static struct lloop_device *loop_dev;
171 static struct gendisk **disks;
172 static cfs_mutex_t lloop_mutex;
173 static void *ll_iocontrol_magic = NULL;
174
175 static loff_t get_loop_size(struct lloop_device *lo, struct file *file)
176 {
177         loff_t size, offset, loopsize;
178
179         /* Compute loopsize in bytes */
180         size = i_size_read(file->f_mapping->host);
181         offset = lo->lo_offset;
182         loopsize = size - offset;
183         if (lo->lo_sizelimit > 0 && lo->lo_sizelimit < loopsize)
184                 loopsize = lo->lo_sizelimit;
185
186         /*
187          * Unfortunately, if we want to do I/O on the device,
188          * the number of 512-byte sectors has to fit into a sector_t.
189          */
190         return loopsize >> 9;
191 }
192
193 static int do_bio_lustrebacked(struct lloop_device *lo, struct bio *head)
194 {
195         const struct lu_env  *env   = lo->lo_env;
196         struct cl_io         *io    = &lo->lo_io;
197         struct inode         *inode = lo->lo_backing_file->f_dentry->d_inode;
198         struct cl_object     *obj = ll_i2info(inode)->lli_clob;
199         pgoff_t               offset;
200         int                   ret;
201         int                   i;
202         int                   rw;
203         obd_count             page_count = 0;
204         struct bio_vec       *bvec;
205         struct bio           *bio;
206         ssize_t               bytes;
207
208         struct ll_dio_pages  *pvec = &lo->lo_pvec;
209         struct page         **pages = pvec->ldp_pages;
210         loff_t               *offsets = pvec->ldp_offsets;
211
212         truncate_inode_pages(inode->i_mapping, 0);
213
214         /* initialize the IO */
215         memset(io, 0, sizeof(*io));
216         io->ci_obj = obj;
217         ret = cl_io_init(env, io, CIT_MISC, obj);
218         if (ret)
219                 return io->ci_result;
220         io->ci_lockreq = CILR_NEVER;
221
222         LASSERT(head != NULL);
223         rw = head->bi_rw;
224         for (bio = head; bio != NULL; bio = bio->bi_next) {
225                 LASSERT(rw == bio->bi_rw);
226
227                 offset = (pgoff_t)(bio->bi_sector << 9) + lo->lo_offset;
228                 bio_for_each_segment(bvec, bio, i) {
229                         BUG_ON(bvec->bv_offset != 0);
230                         BUG_ON(bvec->bv_len != CFS_PAGE_SIZE);
231
232                         pages[page_count] = bvec->bv_page;
233                         offsets[page_count] = offset;
234                         page_count++;
235                         offset += bvec->bv_len;
236                 }
237                 LASSERT(page_count <= LLOOP_MAX_SEGMENTS);
238         }
239
240         ll_stats_ops_tally(ll_i2sbi(inode),
241                         (rw == WRITE) ? LPROC_LL_BRW_WRITE : LPROC_LL_BRW_READ,
242                         page_count);
243
244         pvec->ldp_size = page_count << PAGE_CACHE_SHIFT;
245         pvec->ldp_nr = page_count;
246
247         /* FIXME: in ll_direct_rw_pages, it has to allocate many cl_page{}s to
248          * write those pages into OST. Even worse case is that more pages
249          * would be asked to write out to swap space, and then finally get here
250          * again.
251          * Unfortunately this is NOT easy to fix.
252          * Thoughts on solution:
253          * 0. Define a reserved pool for cl_pages, which could be a list of
254          *    pre-allocated cl_pages from cl_page_kmem;
255          * 1. Define a new operation in cl_object_operations{}, says clo_depth,
256          *    which measures how many layers for this lustre object. Generally
257          *    speaking, the depth would be 2, one for llite, and one for lovsub.
258          *    However, for SNS, there will be more since we need additional page
259          *    to store parity;
260          * 2. Reserve the # of (page_count * depth) cl_pages from the reserved
261          *    pool. Afterwards, the clio would allocate the pages from reserved 
262          *    pool, this guarantees we neeedn't allocate the cl_pages from
263          *    generic cl_page slab cache.
264          *    Of course, if there is NOT enough pages in the pool, we might
265          *    be asked to write less pages once, this purely depends on
266          *    implementation. Anyway, we should be careful to avoid deadlocking.
267          */
268         LOCK_INODE_MUTEX(inode);
269         bytes = ll_direct_rw_pages(env, io, rw, inode, pvec);
270         UNLOCK_INODE_MUTEX(inode);
271         cl_io_fini(env, io);
272         return (bytes == pvec->ldp_size) ? 0 : (int)bytes;
273 }
274
275 /*
276  * Add bio to back of pending list
277  */
278 static void loop_add_bio(struct lloop_device *lo, struct bio *bio)
279 {
280         unsigned long flags;
281
282         cfs_spin_lock_irqsave(&lo->lo_lock, flags);
283         if (lo->lo_biotail) {
284                 lo->lo_biotail->bi_next = bio;
285                 lo->lo_biotail = bio;
286         } else
287                 lo->lo_bio = lo->lo_biotail = bio;
288         cfs_spin_unlock_irqrestore(&lo->lo_lock, flags);
289
290         cfs_atomic_inc(&lo->lo_pending);
291         if (cfs_waitq_active(&lo->lo_bh_wait))
292                 cfs_waitq_signal(&lo->lo_bh_wait);
293 }
294
295 /*
296  * Grab first pending buffer
297  */
298 static unsigned int loop_get_bio(struct lloop_device *lo, struct bio **req)
299 {
300         struct bio *first;
301         struct bio **bio;
302         unsigned int count = 0;
303         unsigned int page_count = 0;
304         int rw;
305
306         cfs_spin_lock_irq(&lo->lo_lock);
307         first = lo->lo_bio;
308         if (unlikely(first == NULL)) {
309                 cfs_spin_unlock_irq(&lo->lo_lock);
310                 return 0;
311         }
312
313         /* TODO: need to split the bio, too bad. */
314         LASSERT(first->bi_vcnt <= LLOOP_MAX_SEGMENTS);
315
316         rw = first->bi_rw;
317         bio = &lo->lo_bio;
318         while (*bio && (*bio)->bi_rw == rw) {
319                 CDEBUG(D_INFO, "bio sector %llu size %u count %u vcnt%u \n",
320                        (unsigned long long)(*bio)->bi_sector, (*bio)->bi_size,
321                        page_count, (*bio)->bi_vcnt);
322                 if (page_count + (*bio)->bi_vcnt > LLOOP_MAX_SEGMENTS)
323                         break;
324
325
326                 page_count += (*bio)->bi_vcnt;
327                 count++;
328                 bio = &(*bio)->bi_next;
329         }
330         if (*bio) {
331                 /* Some of bios can't be mergable. */
332                 lo->lo_bio = *bio;
333                 *bio = NULL;
334         } else {
335                 /* Hit the end of queue */
336                 lo->lo_biotail = NULL;
337                 lo->lo_bio = NULL;
338         }
339         *req = first;
340         cfs_spin_unlock_irq(&lo->lo_lock);
341         return count;
342 }
343
344 static int loop_make_request(struct request_queue *q, struct bio *old_bio)
345 {
346         struct lloop_device *lo = q->queuedata;
347         int rw = bio_rw(old_bio);
348         int inactive;
349
350         if (!lo)
351                 goto err;
352
353         CDEBUG(D_INFO, "submit bio sector %llu size %u\n",
354                (unsigned long long)old_bio->bi_sector, old_bio->bi_size);
355
356         cfs_spin_lock_irq(&lo->lo_lock);
357         inactive = (lo->lo_state != LLOOP_BOUND);
358         cfs_spin_unlock_irq(&lo->lo_lock);
359         if (inactive)
360                 goto err;
361
362         if (rw == WRITE) {
363                 if (lo->lo_flags & LO_FLAGS_READ_ONLY)
364                         goto err;
365         } else if (rw == READA) {
366                 rw = READ;
367         } else if (rw != READ) {
368                 CERROR("lloop: unknown command (%x)\n", rw);
369                 goto err;
370         }
371         loop_add_bio(lo, old_bio);
372         return 0;
373 err:
374         cfs_bio_io_error(old_bio, old_bio->bi_size);
375         return 0;
376 }
377
378 #ifdef HAVE_REQUEST_QUEUE_UNPLUG_FN
379 /*
380  * kick off io on the underlying address space
381  */
382 static void loop_unplug(struct request_queue *q)
383 {
384         struct lloop_device *lo = q->queuedata;
385
386         clear_bit(QUEUE_FLAG_PLUGGED, &q->queue_flags);
387         blk_run_address_space(lo->lo_backing_file->f_mapping);
388 }
389 #endif
390
391 static inline void loop_handle_bio(struct lloop_device *lo, struct bio *bio)
392 {
393         int ret;
394         ret = do_bio_lustrebacked(lo, bio);
395         while (bio) {
396                 struct bio *tmp = bio->bi_next;
397                 bio->bi_next = NULL;
398                 cfs_bio_endio(bio, bio->bi_size, ret);
399                 bio = tmp;
400         }
401 }
402
403 static inline int loop_active(struct lloop_device *lo)
404 {
405         return cfs_atomic_read(&lo->lo_pending) ||
406                 (lo->lo_state == LLOOP_RUNDOWN);
407 }
408
409 /*
410  * worker thread that handles reads/writes to file backed loop devices,
411  * to avoid blocking in our make_request_fn.
412  */
413 static int loop_thread(void *data)
414 {
415         struct lloop_device *lo = data;
416         struct bio *bio;
417         unsigned int count;
418         unsigned long times = 0;
419         unsigned long total_count = 0;
420
421         struct lu_env *env;
422         int refcheck;
423         int ret = 0;
424
425         daemonize("lloop%d", lo->lo_number);
426
427         set_user_nice(current, -20);
428
429         lo->lo_state = LLOOP_BOUND;
430
431         env = cl_env_get(&refcheck);
432         if (IS_ERR(env))
433                 GOTO(out, ret = PTR_ERR(env));
434
435         lo->lo_env = env;
436         memset(&lo->lo_pvec, 0, sizeof(lo->lo_pvec));
437         lo->lo_pvec.ldp_pages   = lo->lo_requests[0].lrd_pages;
438         lo->lo_pvec.ldp_offsets = lo->lo_requests[0].lrd_offsets;
439
440         /*
441          * up sem, we are running
442          */
443         cfs_up(&lo->lo_sem);
444
445         for (;;) {
446                 cfs_wait_event(lo->lo_bh_wait, loop_active(lo));
447                 if (!cfs_atomic_read(&lo->lo_pending)) {
448                         int exiting = 0;
449                         cfs_spin_lock_irq(&lo->lo_lock);
450                         exiting = (lo->lo_state == LLOOP_RUNDOWN);
451                         cfs_spin_unlock_irq(&lo->lo_lock);
452                         if (exiting)
453                                 break;
454                 }
455
456                 bio = NULL;
457                 count = loop_get_bio(lo, &bio);
458                 if (!count) {
459                         CWARN("lloop(minor: %d): missing bio\n", lo->lo_number);
460                         continue;
461                 }
462
463                 total_count += count;
464                 if (total_count < count) {     /* overflow */
465                         total_count = count;
466                         times = 1;
467                 } else {
468                         times++;
469                 }
470                 if ((times & 127) == 0) {
471                         CDEBUG(D_INFO, "total: %lu, count: %lu, avg: %lu\n",
472                                total_count, times, total_count / times);
473                 }
474
475                 LASSERT(bio != NULL);
476                 LASSERT(count <= cfs_atomic_read(&lo->lo_pending));
477                 loop_handle_bio(lo, bio);
478                 cfs_atomic_sub(count, &lo->lo_pending);
479         }
480         cl_env_put(env, &refcheck);
481
482 out:
483         cfs_up(&lo->lo_sem);
484         return ret;
485 }
486
487 static int loop_set_fd(struct lloop_device *lo, struct file *unused,
488                        struct block_device *bdev, struct file *file)
489 {
490         struct inode         *inode;
491         struct address_space *mapping;
492         int                   lo_flags = 0;
493         int                   error;
494         loff_t                size;
495
496         if (!cfs_try_module_get(THIS_MODULE))
497                 return -ENODEV;
498
499         error = -EBUSY;
500         if (lo->lo_state != LLOOP_UNBOUND)
501                 goto out;
502
503         mapping = file->f_mapping;
504         inode = mapping->host;
505
506         error = -EINVAL;
507         if (!S_ISREG(inode->i_mode) || inode->i_sb->s_magic != LL_SUPER_MAGIC)
508                 goto out;
509
510         if (!(file->f_mode & FMODE_WRITE))
511                 lo_flags |= LO_FLAGS_READ_ONLY;
512
513         size = get_loop_size(lo, file);
514
515         if ((loff_t)(sector_t)size != size) {
516                 error = -EFBIG;
517                 goto out;
518         }
519
520         /* remove all pages in cache so as dirty pages not to be existent. */
521         truncate_inode_pages(mapping, 0);
522
523         set_device_ro(bdev, (lo_flags & LO_FLAGS_READ_ONLY) != 0);
524
525         lo->lo_blocksize = CFS_PAGE_SIZE;
526         lo->lo_device = bdev;
527         lo->lo_flags = lo_flags;
528         lo->lo_backing_file = file;
529         lo->ioctl = NULL;
530         lo->lo_sizelimit = 0;
531         lo->old_gfp_mask = mapping_gfp_mask(mapping);
532         mapping_set_gfp_mask(mapping, lo->old_gfp_mask & ~(__GFP_IO|__GFP_FS));
533
534         lo->lo_bio = lo->lo_biotail = NULL;
535
536         /*
537          * set queue make_request_fn, and add limits based on lower level
538          * device
539          */
540         blk_queue_make_request(lo->lo_queue, loop_make_request);
541         lo->lo_queue->queuedata = lo;
542 #ifdef HAVE_REQUEST_QUEUE_UNPLUG_FN
543         lo->lo_queue->unplug_fn = loop_unplug;
544 #endif
545
546         /* queue parameters */
547         CLASSERT(CFS_PAGE_SIZE < (1 << (sizeof(unsigned short) * 8)));
548         blk_queue_logical_block_size(lo->lo_queue,
549                                      (unsigned short)CFS_PAGE_SIZE);
550         blk_queue_max_hw_sectors(lo->lo_queue,
551                                  LLOOP_MAX_SEGMENTS << (CFS_PAGE_SHIFT - 9));
552         blk_queue_max_segments(lo->lo_queue, LLOOP_MAX_SEGMENTS);
553
554         set_capacity(disks[lo->lo_number], size);
555         bd_set_size(bdev, size << 9);
556
557         set_blocksize(bdev, lo->lo_blocksize);
558
559         cfs_create_thread(loop_thread, lo, CLONE_KERNEL);
560         cfs_down(&lo->lo_sem);
561         return 0;
562
563  out:
564         /* This is safe: open() is still holding a reference. */
565         cfs_module_put(THIS_MODULE);
566         return error;
567 }
568
569 static int loop_clr_fd(struct lloop_device *lo, struct block_device *bdev,
570                        int count)
571 {
572         struct file *filp = lo->lo_backing_file;
573         int gfp = lo->old_gfp_mask;
574
575         if (lo->lo_state != LLOOP_BOUND)
576                 return -ENXIO;
577
578         if (lo->lo_refcnt > count)        /* we needed one fd for the ioctl */
579                 return -EBUSY;
580
581         if (filp == NULL)
582                 return -EINVAL;
583
584         cfs_spin_lock_irq(&lo->lo_lock);
585         lo->lo_state = LLOOP_RUNDOWN;
586         cfs_spin_unlock_irq(&lo->lo_lock);
587         cfs_waitq_signal(&lo->lo_bh_wait);
588
589         cfs_down(&lo->lo_sem);
590         lo->lo_backing_file = NULL;
591         lo->ioctl = NULL;
592         lo->lo_device = NULL;
593         lo->lo_offset = 0;
594         lo->lo_sizelimit = 0;
595         lo->lo_flags = 0;
596         ll_invalidate_bdev(bdev, 0);
597         set_capacity(disks[lo->lo_number], 0);
598         bd_set_size(bdev, 0);
599         mapping_set_gfp_mask(filp->f_mapping, gfp);
600         lo->lo_state = LLOOP_UNBOUND;
601         fput(filp);
602         /* This is safe: open() is still holding a reference. */
603         cfs_module_put(THIS_MODULE);
604         return 0;
605 }
606
607 #ifdef HAVE_BLKDEV_PUT_2ARGS
608 static int lo_open(struct block_device *bdev, fmode_t mode)
609 {
610         struct lloop_device *lo = bdev->bd_disk->private_data;
611 #else
612 static int lo_open(struct inode *inode, struct file *file)
613 {
614         struct lloop_device *lo = inode->i_bdev->bd_disk->private_data;
615 #endif
616
617         cfs_mutex_lock(&lo->lo_ctl_mutex);
618         lo->lo_refcnt++;
619         cfs_mutex_unlock(&lo->lo_ctl_mutex);
620
621         return 0;
622 }
623
624 #ifdef HAVE_BLKDEV_PUT_2ARGS
625 static int lo_release(struct gendisk *disk, fmode_t mode)
626 {
627         struct lloop_device *lo = disk->private_data;
628 #else
629 static int lo_release(struct inode *inode, struct file *file)
630 {
631         struct lloop_device *lo = inode->i_bdev->bd_disk->private_data;
632 #endif
633
634         cfs_mutex_lock(&lo->lo_ctl_mutex);
635         --lo->lo_refcnt;
636         cfs_mutex_unlock(&lo->lo_ctl_mutex);
637
638         return 0;
639 }
640
641 /* lloop device node's ioctl function. */
642 #ifdef HAVE_BLKDEV_PUT_2ARGS
643 static int lo_ioctl(struct block_device *bdev, fmode_t mode,
644                     unsigned int cmd, unsigned long arg)
645 {
646         struct lloop_device *lo = bdev->bd_disk->private_data;
647         struct inode *inode = NULL;
648         int err = 0;
649 #else
650 static int lo_ioctl(struct inode *inode, struct file *unused,
651                     unsigned int cmd, unsigned long arg)
652 {
653         struct lloop_device *lo = inode->i_bdev->bd_disk->private_data;
654         struct block_device *bdev = inode->i_bdev;
655         int err = 0;
656 #endif
657
658         cfs_mutex_lock(&lloop_mutex);
659         switch (cmd) {
660         case LL_IOC_LLOOP_DETACH: {
661                 err = loop_clr_fd(lo, bdev, 2);
662                 if (err == 0)
663                         ll_blkdev_put(bdev, 0); /* grabbed in LLOOP_ATTACH */
664                 break;
665         }
666
667         case LL_IOC_LLOOP_INFO: {
668                 struct lu_fid fid;
669
670                 LASSERT(lo->lo_backing_file != NULL);
671                 if (inode == NULL)
672                         inode = lo->lo_backing_file->f_dentry->d_inode;
673                 if (lo->lo_state == LLOOP_BOUND)
674                         fid = ll_i2info(inode)->lli_fid;
675                 else
676                         fid_zero(&fid);
677
678                 if (copy_to_user((struct lu_fid *)arg, &fid, sizeof(fid)))
679                         err = -EFAULT;
680                 break;
681         }
682
683         default:
684                 err = -EINVAL;
685                 break;
686         }
687         cfs_mutex_unlock(&lloop_mutex);
688
689         return err;
690 }
691
692 static struct block_device_operations lo_fops = {
693         .owner =        THIS_MODULE,
694         .open =         lo_open,
695         .release =      lo_release,
696         .ioctl =        lo_ioctl,
697 };
698
699 /* dynamic iocontrol callback.
700  * This callback is registered in lloop_init and will be called by
701  * ll_iocontrol_call.
702  *
703  * This is a llite regular file ioctl function. It takes the responsibility
704  * of attaching or detaching a file by a lloop's device numner.
705  */
706 static enum llioc_iter lloop_ioctl(struct inode *unused, struct file *file,
707                                    unsigned int cmd, unsigned long arg,
708                                    void *magic, int *rcp)
709 {
710         struct lloop_device *lo = NULL;
711         struct block_device *bdev = NULL;
712         int err = 0;
713         dev_t dev;
714
715         if (magic != ll_iocontrol_magic)
716                 return LLIOC_CONT;
717
718         if (disks == NULL)
719                 GOTO(out1, err = -ENODEV);
720
721         CWARN("Enter llop_ioctl\n");
722
723         cfs_mutex_lock(&lloop_mutex);
724         switch (cmd) {
725         case LL_IOC_LLOOP_ATTACH: {
726                 struct lloop_device *lo_free = NULL;
727                 int i;
728
729                 for (i = 0; i < max_loop; i++, lo = NULL) {
730                         lo = &loop_dev[i];
731                         if (lo->lo_state == LLOOP_UNBOUND) {
732                                 if (!lo_free)
733                                         lo_free = lo;
734                                 continue;
735                         }
736                         if (lo->lo_backing_file->f_dentry->d_inode ==
737                             file->f_dentry->d_inode)
738                                 break;
739                 }
740                 if (lo || !lo_free)
741                         GOTO(out, err = -EBUSY);
742
743                 lo = lo_free;
744                 dev = MKDEV(lloop_major, lo->lo_number);
745
746                 /* quit if the used pointer is writable */
747                 if (put_user((long)old_encode_dev(dev), (long*)arg))
748                         GOTO(out, err = -EFAULT);
749
750                 bdev = blkdev_get_by_dev(dev, file->f_mode, NULL);
751                 if (IS_ERR(bdev))
752                         GOTO(out, err = PTR_ERR(bdev));
753
754                 get_file(file);
755                 err = loop_set_fd(lo, NULL, bdev, file);
756                 if (err) {
757                         fput(file);
758                         ll_blkdev_put(bdev, 0);
759                 }
760
761                 break;
762         }
763
764         case LL_IOC_LLOOP_DETACH_BYDEV: {
765                 int minor;
766
767                 dev = old_decode_dev(arg);
768                 if (MAJOR(dev) != lloop_major)
769                         GOTO(out, err = -EINVAL);
770
771                 minor = MINOR(dev);
772                 if (minor > max_loop - 1)
773                         GOTO(out, err = -EINVAL);
774
775                 lo = &loop_dev[minor];
776                 if (lo->lo_state != LLOOP_BOUND)
777                         GOTO(out, err = -EINVAL);
778
779                 bdev = lo->lo_device;
780                 err = loop_clr_fd(lo, bdev, 1);
781                 if (err == 0)
782                         ll_blkdev_put(bdev, 0); /* grabbed in LLOOP_ATTACH */
783
784                 break;
785         }
786
787         default:
788                 err = -EINVAL;
789                 break;
790         }
791
792 out:
793         cfs_mutex_unlock(&lloop_mutex);
794 out1:
795         if (rcp)
796                 *rcp = err;
797         return LLIOC_STOP;
798 }
799
800 static int __init lloop_init(void)
801 {
802         int        i;
803         unsigned int cmdlist[] = {
804                 LL_IOC_LLOOP_ATTACH,
805                 LL_IOC_LLOOP_DETACH_BYDEV,
806         };
807
808         if (max_loop < 1 || max_loop > 256) {
809                 max_loop = MAX_LOOP_DEFAULT;
810                 CWARN("lloop: invalid max_loop (must be between"
811                       " 1 and 256), using default (%u)\n", max_loop);
812         }
813
814         lloop_major = register_blkdev(0, "lloop");
815         if (lloop_major < 0)
816                 return -EIO;
817
818         CDEBUG(D_CONFIG, "registered lloop major %d with %u minors\n",
819                lloop_major, max_loop);
820
821         ll_iocontrol_magic = ll_iocontrol_register(lloop_ioctl, 2, cmdlist);
822         if (ll_iocontrol_magic == NULL)
823                 goto out_mem1;
824
825         OBD_ALLOC_WAIT(loop_dev, max_loop * sizeof(*loop_dev));
826         if (!loop_dev)
827                 goto out_mem1;
828
829         OBD_ALLOC_WAIT(disks, max_loop * sizeof(*disks));
830         if (!disks)
831                 goto out_mem2;
832
833         for (i = 0; i < max_loop; i++) {
834                 disks[i] = alloc_disk(1);
835                 if (!disks[i])
836                         goto out_mem3;
837         }
838
839         cfs_mutex_init(&lloop_mutex);
840
841         for (i = 0; i < max_loop; i++) {
842                 struct lloop_device *lo = &loop_dev[i];
843                 struct gendisk *disk = disks[i];
844
845                 lo->lo_queue = blk_alloc_queue(GFP_KERNEL);
846                 if (!lo->lo_queue)
847                         goto out_mem4;
848
849                 cfs_mutex_init(&lo->lo_ctl_mutex);
850                 cfs_sema_init(&lo->lo_sem, 0);
851                 cfs_waitq_init(&lo->lo_bh_wait);
852                 lo->lo_number = i;
853                 cfs_spin_lock_init(&lo->lo_lock);
854                 disk->major = lloop_major;
855                 disk->first_minor = i;
856                 disk->fops = &lo_fops;
857                 sprintf(disk->disk_name, "lloop%d", i);
858                 disk->private_data = lo;
859                 disk->queue = lo->lo_queue;
860         }
861
862         /* We cannot fail after we call this, so another loop!*/
863         for (i = 0; i < max_loop; i++)
864                 add_disk(disks[i]);
865         return 0;
866
867 out_mem4:
868         while (i--)
869                 blk_cleanup_queue(loop_dev[i].lo_queue);
870         i = max_loop;
871 out_mem3:
872         while (i--)
873                 put_disk(disks[i]);
874         OBD_FREE(disks, max_loop * sizeof(*disks));
875 out_mem2:
876         OBD_FREE(loop_dev, max_loop * sizeof(*loop_dev));
877 out_mem1:
878         unregister_blkdev(lloop_major, "lloop");
879         ll_iocontrol_unregister(ll_iocontrol_magic);
880         CERROR("lloop: ran out of memory\n");
881         return -ENOMEM;
882 }
883
884 static void lloop_exit(void)
885 {
886         int i;
887
888         ll_iocontrol_unregister(ll_iocontrol_magic);
889         for (i = 0; i < max_loop; i++) {
890                 del_gendisk(disks[i]);
891                 blk_cleanup_queue(loop_dev[i].lo_queue);
892                 put_disk(disks[i]);
893         }
894         if (ll_unregister_blkdev(lloop_major, "lloop"))
895                 CWARN("lloop: cannot unregister blkdev\n");
896         else
897                 CDEBUG(D_CONFIG, "unregistered lloop major %d\n", lloop_major);
898
899         OBD_FREE(disks, max_loop * sizeof(*disks));
900         OBD_FREE(loop_dev, max_loop * sizeof(*loop_dev));
901 }
902
903 module_init(lloop_init);
904 module_exit(lloop_exit);
905
906 CFS_MODULE_PARM(max_loop, "i", int, 0444, "maximum of lloop_device");
907 MODULE_AUTHOR("Sun Microsystems, Inc. <http://www.lustre.org/>");
908 MODULE_DESCRIPTION("Lustre virtual block device");
909 MODULE_LICENSE("GPL");