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