Whamcloud - gitweb
LU-819 utils: Fix lfs getstripe -M
[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  * Copyright (c) 2011 Whamcloud, Inc.
33  *
34  */
35 /*
36  * This file is part of Lustre, http://www.lustre.org/
37  * Lustre is a trademark of Sun Microsystems, Inc.
38  */
39
40 /*
41  *  linux/drivers/block/loop.c
42  *
43  *  Written by Theodore Ts'o, 3/29/93
44  *
45  * Copyright 1993 by Theodore Ts'o.  Redistribution of this file is
46  * permitted under the GNU General Public License.
47  *
48  * Modularized and updated for 1.1.16 kernel - Mitch Dsouza 28th May 1994
49  * Adapted for 1.3.59 kernel - Andries Brouwer, 1 Feb 1996
50  *
51  * Fixed do_loop_request() re-entrancy - Vincent.Renardias@waw.com Mar 20, 1997
52  *
53  * Added devfs support - Richard Gooch <rgooch@atnf.csiro.au> 16-Jan-1998
54  *
55  * Handle sparse backing files correctly - Kenn Humborg, Jun 28, 1998
56  *
57  * Loadable modules and other fixes by AK, 1998
58  *
59  * Maximum number of loop devices now dynamic via max_loop module parameter.
60  * Russell Kroll <rkroll@exploits.org> 19990701
61  *
62  * Maximum number of loop devices when compiled-in now selectable by passing
63  * max_loop=<1-255> to the kernel on boot.
64  * Erik I. Bols?, <eriki@himolde.no>, Oct 31, 1999
65  *
66  * Completely rewrite request handling to be make_request_fn style and
67  * non blocking, pushing work to a helper thread. Lots of fixes from
68  * Al Viro too.
69  * Jens Axboe <axboe@suse.de>, Nov 2000
70  *
71  * Support up to 256 loop devices
72  * Heinz Mauelshagen <mge@sistina.com>, Feb 2002
73  *
74  * Support for falling back on the write file operation when the address space
75  * operations prepare_write and/or commit_write are not available on the
76  * backing filesystem.
77  * Anton Altaparmakov, 16 Feb 2005
78  *
79  * Still To Fix:
80  * - Advisory locking is ignored here.
81  * - Should use an own CAP_* category instead of CAP_SYS_ADMIN
82  *
83  */
84
85 #ifndef AUTOCONF_INCLUDED
86 #include <linux/config.h>
87 #endif
88 #include <linux/module.h>
89
90 #include <linux/sched.h>
91 #include <linux/fs.h>
92 #include <linux/file.h>
93 #include <linux/stat.h>
94 #include <linux/errno.h>
95 #include <linux/major.h>
96 #include <linux/wait.h>
97 #include <linux/blkdev.h>
98 #include <linux/blkpg.h>
99 #include <linux/init.h>
100 #include <linux/smp_lock.h>
101 #include <linux/swap.h>
102 #include <linux/slab.h>
103 #include <linux/suspend.h>
104 #include <linux/writeback.h>
105 #include <linux/buffer_head.h>                /* for invalidate_bdev() */
106 #include <linux/completion.h>
107 #include <linux/highmem.h>
108 #include <linux/gfp.h>
109 #include <linux/swap.h>
110 #include <linux/pagevec.h>
111
112 #include <asm/uaccess.h>
113
114 #include <lustre_lib.h>
115 #include <lustre_lite.h>
116 #include "llite_internal.h"
117
118 #define LLOOP_MAX_SEGMENTS        PTLRPC_MAX_BRW_PAGES
119
120 /* Possible states of device */
121 enum {
122         LLOOP_UNBOUND,
123         LLOOP_BOUND,
124         LLOOP_RUNDOWN,
125 };
126
127 struct lloop_device {
128         int                  lo_number;
129         int                  lo_refcnt;
130         loff_t               lo_offset;
131         loff_t               lo_sizelimit;
132         int                  lo_flags;
133         int                (*ioctl)(struct lloop_device *, int cmd,
134                                     unsigned long arg);
135
136         struct file         *lo_backing_file;
137         struct block_device *lo_device;
138         unsigned             lo_blocksize;
139
140         int                  old_gfp_mask;
141
142         cfs_spinlock_t       lo_lock;
143         struct bio          *lo_bio;
144         struct bio          *lo_biotail;
145         int                  lo_state;
146         cfs_semaphore_t      lo_sem;
147         cfs_semaphore_t      lo_ctl_mutex;
148         cfs_atomic_t         lo_pending;
149         cfs_waitq_t          lo_bh_wait;
150
151         struct request_queue *lo_queue;
152
153         const struct lu_env *lo_env;
154         struct cl_io         lo_io;
155         struct ll_dio_pages  lo_pvec;
156
157         /* data to handle bio for lustre. */
158         struct lo_request_data {
159                 struct page *lrd_pages[LLOOP_MAX_SEGMENTS];
160                 loff_t       lrd_offsets[LLOOP_MAX_SEGMENTS];
161         } lo_requests[1];
162 };
163
164 /*
165  * Loop flags
166  */
167 enum {
168         LO_FLAGS_READ_ONLY       = 1,
169 };
170
171 static int lloop_major;
172 #define MAX_LOOP_DEFAULT  16
173 static int max_loop = MAX_LOOP_DEFAULT;
174 static struct lloop_device *loop_dev;
175 static struct gendisk **disks;
176 static cfs_semaphore_t lloop_mutex;
177 static void *ll_iocontrol_magic = NULL;
178
179 static loff_t get_loop_size(struct lloop_device *lo, struct file *file)
180 {
181         loff_t size, offset, loopsize;
182
183         /* Compute loopsize in bytes */
184         size = i_size_read(file->f_mapping->host);
185         offset = lo->lo_offset;
186         loopsize = size - offset;
187         if (lo->lo_sizelimit > 0 && lo->lo_sizelimit < loopsize)
188                 loopsize = lo->lo_sizelimit;
189
190         /*
191          * Unfortunately, if we want to do I/O on the device,
192          * the number of 512-byte sectors has to fit into a sector_t.
193          */
194         return loopsize >> 9;
195 }
196
197 static int do_bio_lustrebacked(struct lloop_device *lo, struct bio *head)
198 {
199         const struct lu_env  *env   = lo->lo_env;
200         struct cl_io         *io    = &lo->lo_io;
201         struct inode         *inode = lo->lo_backing_file->f_dentry->d_inode;
202         struct cl_object     *obj = ll_i2info(inode)->lli_clob;
203         pgoff_t               offset;
204         int                   ret;
205         int                   i;
206         int                   rw;
207         obd_count             page_count = 0;
208         struct bio_vec       *bvec;
209         struct bio           *bio;
210         ssize_t               bytes;
211
212         struct ll_dio_pages  *pvec = &lo->lo_pvec;
213         struct page         **pages = pvec->ldp_pages;
214         loff_t               *offsets = pvec->ldp_offsets;
215
216         truncate_inode_pages(inode->i_mapping, 0);
217
218         /* initialize the IO */
219         memset(io, 0, sizeof(*io));
220         io->ci_obj = obj;
221         ret = cl_io_init(env, io, CIT_MISC, obj);
222         if (ret)
223                 return io->ci_result;
224         io->ci_lockreq = CILR_NEVER;
225
226         LASSERT(head != NULL);
227         rw = head->bi_rw;
228         for (bio = head; bio != NULL; bio = bio->bi_next) {
229                 LASSERT(rw == bio->bi_rw);
230
231                 offset = (pgoff_t)(bio->bi_sector << 9) + lo->lo_offset;
232                 bio_for_each_segment(bvec, bio, i) {
233                         BUG_ON(bvec->bv_offset != 0);
234                         BUG_ON(bvec->bv_len != CFS_PAGE_SIZE);
235
236                         pages[page_count] = bvec->bv_page;
237                         offsets[page_count] = offset;
238                         page_count++;
239                         offset += bvec->bv_len;
240                 }
241                 LASSERT(page_count <= LLOOP_MAX_SEGMENTS);
242         }
243
244         ll_stats_ops_tally(ll_i2sbi(inode),
245                         (rw == WRITE) ? LPROC_LL_BRW_WRITE : LPROC_LL_BRW_READ,
246                         page_count);
247
248         pvec->ldp_size = page_count << PAGE_CACHE_SHIFT;
249         pvec->ldp_nr = page_count;
250
251         /* FIXME: in ll_direct_rw_pages, it has to allocate many cl_page{}s to
252          * write those pages into OST. Even worse case is that more pages
253          * would be asked to write out to swap space, and then finally get here
254          * again.
255          * Unfortunately this is NOT easy to fix.
256          * Thoughts on solution:
257          * 0. Define a reserved pool for cl_pages, which could be a list of
258          *    pre-allocated cl_pages from cl_page_kmem;
259          * 1. Define a new operation in cl_object_operations{}, says clo_depth,
260          *    which measures how many layers for this lustre object. Generally
261          *    speaking, the depth would be 2, one for llite, and one for lovsub.
262          *    However, for SNS, there will be more since we need additional page
263          *    to store parity;
264          * 2. Reserve the # of (page_count * depth) cl_pages from the reserved
265          *    pool. Afterwards, the clio would allocate the pages from reserved 
266          *    pool, this guarantees we neeedn't allocate the cl_pages from
267          *    generic cl_page slab cache.
268          *    Of course, if there is NOT enough pages in the pool, we might
269          *    be asked to write less pages once, this purely depends on
270          *    implementation. Anyway, we should be careful to avoid deadlocking.
271          */
272         LOCK_INODE_MUTEX(inode);
273         bytes = ll_direct_rw_pages(env, io, rw, inode, pvec);
274         UNLOCK_INODE_MUTEX(inode);
275         cl_io_fini(env, io);
276         return (bytes == pvec->ldp_size) ? 0 : (int)bytes;
277 }
278
279 /*
280  * Add bio to back of pending list
281  */
282 static void loop_add_bio(struct lloop_device *lo, struct bio *bio)
283 {
284         unsigned long flags;
285
286         cfs_spin_lock_irqsave(&lo->lo_lock, flags);
287         if (lo->lo_biotail) {
288                 lo->lo_biotail->bi_next = bio;
289                 lo->lo_biotail = bio;
290         } else
291                 lo->lo_bio = lo->lo_biotail = bio;
292         cfs_spin_unlock_irqrestore(&lo->lo_lock, flags);
293
294         cfs_atomic_inc(&lo->lo_pending);
295         if (cfs_waitq_active(&lo->lo_bh_wait))
296                 cfs_waitq_signal(&lo->lo_bh_wait);
297 }
298
299 /*
300  * Grab first pending buffer
301  */
302 static unsigned int loop_get_bio(struct lloop_device *lo, struct bio **req)
303 {
304         struct bio *first;
305         struct bio **bio;
306         unsigned int count = 0;
307         unsigned int page_count = 0;
308         int rw;
309
310         cfs_spin_lock_irq(&lo->lo_lock);
311         first = lo->lo_bio;
312         if (unlikely(first == NULL)) {
313                 cfs_spin_unlock_irq(&lo->lo_lock);
314                 return 0;
315         }
316
317         /* TODO: need to split the bio, too bad. */
318         LASSERT(first->bi_vcnt <= LLOOP_MAX_SEGMENTS);
319
320         rw = first->bi_rw;
321         bio = &lo->lo_bio;
322         while (*bio && (*bio)->bi_rw == rw) {
323                 CDEBUG(D_INFO, "bio sector %llu size %u count %u vcnt%u \n",
324                        (unsigned long long)(*bio)->bi_sector, (*bio)->bi_size,
325                        page_count, (*bio)->bi_vcnt);
326                 if (page_count + (*bio)->bi_vcnt > LLOOP_MAX_SEGMENTS)
327                         break;
328
329
330                 page_count += (*bio)->bi_vcnt;
331                 count++;
332                 bio = &(*bio)->bi_next;
333         }
334         if (*bio) {
335                 /* Some of bios can't be mergable. */
336                 lo->lo_bio = *bio;
337                 *bio = NULL;
338         } else {
339                 /* Hit the end of queue */
340                 lo->lo_biotail = NULL;
341                 lo->lo_bio = NULL;
342         }
343         *req = first;
344         cfs_spin_unlock_irq(&lo->lo_lock);
345         return count;
346 }
347
348 static int loop_make_request(struct request_queue *q, struct bio *old_bio)
349 {
350         struct lloop_device *lo = q->queuedata;
351         int rw = bio_rw(old_bio);
352         int inactive;
353
354         if (!lo)
355                 goto err;
356
357         CDEBUG(D_INFO, "submit bio sector %llu size %u\n",
358                (unsigned long long)old_bio->bi_sector, old_bio->bi_size);
359
360         cfs_spin_lock_irq(&lo->lo_lock);
361         inactive = (lo->lo_state != LLOOP_BOUND);
362         cfs_spin_unlock_irq(&lo->lo_lock);
363         if (inactive)
364                 goto err;
365
366         if (rw == WRITE) {
367                 if (lo->lo_flags & LO_FLAGS_READ_ONLY)
368                         goto err;
369         } else if (rw == READA) {
370                 rw = READ;
371         } else if (rw != READ) {
372                 CERROR("lloop: unknown command (%x)\n", rw);
373                 goto err;
374         }
375         loop_add_bio(lo, old_bio);
376         return 0;
377 err:
378         cfs_bio_io_error(old_bio, old_bio->bi_size);
379         return 0;
380 }
381
382 #ifdef HAVE_REQUEST_QUEUE_UNPLUG_FN
383 /*
384  * kick off io on the underlying address space
385  */
386 static void loop_unplug(struct request_queue *q)
387 {
388         struct lloop_device *lo = q->queuedata;
389
390         clear_bit(QUEUE_FLAG_PLUGGED, &q->queue_flags);
391         blk_run_address_space(lo->lo_backing_file->f_mapping);
392 }
393 #endif
394
395 static inline void loop_handle_bio(struct lloop_device *lo, struct bio *bio)
396 {
397         int ret;
398         ret = do_bio_lustrebacked(lo, bio);
399         while (bio) {
400                 struct bio *tmp = bio->bi_next;
401                 bio->bi_next = NULL;
402                 cfs_bio_endio(bio, bio->bi_size, ret);
403                 bio = tmp;
404         }
405 }
406
407 static inline int loop_active(struct lloop_device *lo)
408 {
409         return cfs_atomic_read(&lo->lo_pending) ||
410                 (lo->lo_state == LLOOP_RUNDOWN);
411 }
412
413 /*
414  * worker thread that handles reads/writes to file backed loop devices,
415  * to avoid blocking in our make_request_fn.
416  */
417 static int loop_thread(void *data)
418 {
419         struct lloop_device *lo = data;
420         struct bio *bio;
421         unsigned int count;
422         unsigned long times = 0;
423         unsigned long total_count = 0;
424
425         struct lu_env *env;
426         int refcheck;
427         int ret = 0;
428
429         daemonize("lloop%d", lo->lo_number);
430
431         set_user_nice(current, -20);
432
433         lo->lo_state = LLOOP_BOUND;
434
435         env = cl_env_get(&refcheck);
436         if (IS_ERR(env))
437                 GOTO(out, ret = PTR_ERR(env));
438
439         lo->lo_env = env;
440         memset(&lo->lo_pvec, 0, sizeof(lo->lo_pvec));
441         lo->lo_pvec.ldp_pages   = lo->lo_requests[0].lrd_pages;
442         lo->lo_pvec.ldp_offsets = lo->lo_requests[0].lrd_offsets;
443
444         /*
445          * up sem, we are running
446          */
447         cfs_up(&lo->lo_sem);
448
449         for (;;) {
450                 cfs_wait_event(lo->lo_bh_wait, loop_active(lo));
451                 if (!cfs_atomic_read(&lo->lo_pending)) {
452                         int exiting = 0;
453                         cfs_spin_lock_irq(&lo->lo_lock);
454                         exiting = (lo->lo_state == LLOOP_RUNDOWN);
455                         cfs_spin_unlock_irq(&lo->lo_lock);
456                         if (exiting)
457                                 break;
458                 }
459
460                 bio = NULL;
461                 count = loop_get_bio(lo, &bio);
462                 if (!count) {
463                         CWARN("lloop(minor: %d): missing bio\n", lo->lo_number);
464                         continue;
465                 }
466
467                 total_count += count;
468                 if (total_count < count) {     /* overflow */
469                         total_count = count;
470                         times = 1;
471                 } else {
472                         times++;
473                 }
474                 if ((times & 127) == 0) {
475                         CDEBUG(D_INFO, "total: %lu, count: %lu, avg: %lu\n",
476                                total_count, times, total_count / times);
477                 }
478
479                 LASSERT(bio != NULL);
480                 LASSERT(count <= cfs_atomic_read(&lo->lo_pending));
481                 loop_handle_bio(lo, bio);
482                 cfs_atomic_sub(count, &lo->lo_pending);
483         }
484         cl_env_put(env, &refcheck);
485
486 out:
487         cfs_up(&lo->lo_sem);
488         return ret;
489 }
490
491 static int loop_set_fd(struct lloop_device *lo, struct file *unused,
492                        struct block_device *bdev, struct file *file)
493 {
494         struct inode         *inode;
495         struct address_space *mapping;
496         int                   lo_flags = 0;
497         int                   error;
498         loff_t                size;
499
500         if (!cfs_try_module_get(THIS_MODULE))
501                 return -ENODEV;
502
503         error = -EBUSY;
504         if (lo->lo_state != LLOOP_UNBOUND)
505                 goto out;
506
507         mapping = file->f_mapping;
508         inode = mapping->host;
509
510         error = -EINVAL;
511         if (!S_ISREG(inode->i_mode) || inode->i_sb->s_magic != LL_SUPER_MAGIC)
512                 goto out;
513
514         if (!(file->f_mode & FMODE_WRITE))
515                 lo_flags |= LO_FLAGS_READ_ONLY;
516
517         size = get_loop_size(lo, file);
518
519         if ((loff_t)(sector_t)size != size) {
520                 error = -EFBIG;
521                 goto out;
522         }
523
524         /* remove all pages in cache so as dirty pages not to be existent. */
525         truncate_inode_pages(mapping, 0);
526
527         set_device_ro(bdev, (lo_flags & LO_FLAGS_READ_ONLY) != 0);
528
529         lo->lo_blocksize = CFS_PAGE_SIZE;
530         lo->lo_device = bdev;
531         lo->lo_flags = lo_flags;
532         lo->lo_backing_file = file;
533         lo->ioctl = NULL;
534         lo->lo_sizelimit = 0;
535         lo->old_gfp_mask = mapping_gfp_mask(mapping);
536         mapping_set_gfp_mask(mapping, lo->old_gfp_mask & ~(__GFP_IO|__GFP_FS));
537
538         lo->lo_bio = lo->lo_biotail = NULL;
539
540         /*
541          * set queue make_request_fn, and add limits based on lower level
542          * device
543          */
544         blk_queue_make_request(lo->lo_queue, loop_make_request);
545         lo->lo_queue->queuedata = lo;
546 #ifdef HAVE_REQUEST_QUEUE_UNPLUG_FN
547         lo->lo_queue->unplug_fn = loop_unplug;
548 #endif
549
550         /* queue parameters */
551         CLASSERT(CFS_PAGE_SIZE < (1 << (sizeof(unsigned short) * 8)));
552         blk_queue_logical_block_size(lo->lo_queue,
553                                      (unsigned short)CFS_PAGE_SIZE);
554         blk_queue_max_hw_sectors(lo->lo_queue,
555                                  LLOOP_MAX_SEGMENTS << (CFS_PAGE_SHIFT - 9));
556         blk_queue_max_segments(lo->lo_queue, LLOOP_MAX_SEGMENTS);
557
558         set_capacity(disks[lo->lo_number], size);
559         bd_set_size(bdev, size << 9);
560
561         set_blocksize(bdev, lo->lo_blocksize);
562
563         cfs_create_thread(loop_thread, lo, CLONE_KERNEL);
564         cfs_down(&lo->lo_sem);
565         return 0;
566
567  out:
568         /* This is safe: open() is still holding a reference. */
569         cfs_module_put(THIS_MODULE);
570         return error;
571 }
572
573 static int loop_clr_fd(struct lloop_device *lo, struct block_device *bdev,
574                        int count)
575 {
576         struct file *filp = lo->lo_backing_file;
577         int gfp = lo->old_gfp_mask;
578
579         if (lo->lo_state != LLOOP_BOUND)
580                 return -ENXIO;
581
582         if (lo->lo_refcnt > count)        /* we needed one fd for the ioctl */
583                 return -EBUSY;
584
585         if (filp == NULL)
586                 return -EINVAL;
587
588         cfs_spin_lock_irq(&lo->lo_lock);
589         lo->lo_state = LLOOP_RUNDOWN;
590         cfs_spin_unlock_irq(&lo->lo_lock);
591         cfs_waitq_signal(&lo->lo_bh_wait);
592
593         cfs_down(&lo->lo_sem);
594         lo->lo_backing_file = NULL;
595         lo->ioctl = NULL;
596         lo->lo_device = NULL;
597         lo->lo_offset = 0;
598         lo->lo_sizelimit = 0;
599         lo->lo_flags = 0;
600         ll_invalidate_bdev(bdev, 0);
601         set_capacity(disks[lo->lo_number], 0);
602         bd_set_size(bdev, 0);
603         mapping_set_gfp_mask(filp->f_mapping, gfp);
604         lo->lo_state = LLOOP_UNBOUND;
605         fput(filp);
606         /* This is safe: open() is still holding a reference. */
607         cfs_module_put(THIS_MODULE);
608         return 0;
609 }
610
611 #ifdef HAVE_BLKDEV_PUT_2ARGS
612 static int lo_open(struct block_device *bdev, fmode_t mode)
613 {
614         struct lloop_device *lo = bdev->bd_disk->private_data;
615 #else
616 static int lo_open(struct inode *inode, struct file *file)
617 {
618         struct lloop_device *lo = inode->i_bdev->bd_disk->private_data;
619 #endif
620
621         cfs_down(&lo->lo_ctl_mutex);
622         lo->lo_refcnt++;
623         cfs_up(&lo->lo_ctl_mutex);
624
625         return 0;
626 }
627
628 #ifdef HAVE_BLKDEV_PUT_2ARGS
629 static int lo_release(struct gendisk *disk, fmode_t mode)
630 {
631         struct lloop_device *lo = disk->private_data;
632 #else
633 static int lo_release(struct inode *inode, struct file *file)
634 {
635         struct lloop_device *lo = inode->i_bdev->bd_disk->private_data;
636 #endif
637
638         cfs_down(&lo->lo_ctl_mutex);
639         --lo->lo_refcnt;
640         cfs_up(&lo->lo_ctl_mutex);
641
642         return 0;
643 }
644
645 /* lloop device node's ioctl function. */
646 #ifdef HAVE_BLKDEV_PUT_2ARGS
647 static int lo_ioctl(struct block_device *bdev, fmode_t mode,
648                     unsigned int cmd, unsigned long arg)
649 {
650         struct lloop_device *lo = bdev->bd_disk->private_data;
651         struct inode *inode = lo->lo_backing_file->f_dentry->d_inode;
652 #else
653 static int lo_ioctl(struct inode *inode, struct file *unused,
654                     unsigned int cmd, unsigned long arg)
655 {
656         struct lloop_device *lo = inode->i_bdev->bd_disk->private_data;
657         struct block_device *bdev = inode->i_bdev;
658 #endif
659         int err = 0;
660
661         cfs_down(&lloop_mutex);
662         switch (cmd) {
663         case LL_IOC_LLOOP_DETACH: {
664                 err = loop_clr_fd(lo, bdev, 2);
665                 if (err == 0)
666                         ll_blkdev_put(bdev, 0); /* grabbed in LLOOP_ATTACH */
667                 break;
668         }
669
670         case LL_IOC_LLOOP_INFO: {
671                 struct lu_fid fid;
672
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_up(&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_down(&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_up(&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_init_mutex(&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_init_mutex(&lo->lo_ctl_mutex);
850                 cfs_init_mutex_locked(&lo->lo_sem);
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");