1 /* -*- mode: c; c-basic-offset: 8; indent-tabs-mode: nil; -*-
2 * vim:expandtab:shiftwidth=8:tabstop=8:
4 * Lustre virtual block device emulator.
6 * Copyright (c) 2001-2003 Cluster File Systems, Inc.
8 * This file is part of Lustre, http://www.lustre.org.
10 * Lustre is free software; you can redistribute it and/or
11 * modify it under the terms of version 2 of the GNU General Public
12 * License as published by the Free Software Foundation.
14 * Lustre is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
19 * You should have received a copy of the GNU General Public License
20 * along with Lustre; if not, write to the Free Software
21 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
25 * linux/drivers/block/loop.c
27 * Written by Theodore Ts'o, 3/29/93
29 * Copyright 1993 by Theodore Ts'o. Redistribution of this file is
30 * permitted under the GNU General Public License.
32 * DES encryption plus some minor changes by Werner Almesberger, 30-MAY-1993
33 * more DES encryption plus IDEA encryption by Nicholas J. Leon, June 20, 1996
35 * Modularized and updated for 1.1.16 kernel - Mitch Dsouza 28th May 1994
36 * Adapted for 1.3.59 kernel - Andries Brouwer, 1 Feb 1996
38 * Fixed do_loop_request() re-entrancy - Vincent.Renardias@waw.com Mar 20, 1997
40 * Added devfs support - Richard Gooch <rgooch@atnf.csiro.au> 16-Jan-1998
42 * Handle sparse backing files correctly - Kenn Humborg, Jun 28, 1998
44 * Loadable modules and other fixes by AK, 1998
46 * Make real block number available to downstream transfer functions, enables
47 * CBC (and relatives) mode encryption requiring unique IVs per data block.
48 * Reed H. Petty, rhp@draper.net
50 * Maximum number of loop devices now dynamic via max_loop module parameter.
51 * Russell Kroll <rkroll@exploits.org> 19990701
53 * Maximum number of loop devices when compiled-in now selectable by passing
54 * max_loop=<1-255> to the kernel on boot.
55 * Erik I. Bols?, <eriki@himolde.no>, Oct 31, 1999
57 * Completely rewrite request handling to be make_request_fn style and
58 * non blocking, pushing work to a helper thread. Lots of fixes from
60 * Jens Axboe <axboe@suse.de>, Nov 2000
62 * Support up to 256 loop devices
63 * Heinz Mauelshagen <mge@sistina.com>, Feb 2002
65 * Support for falling back on the write file operation when the address space
66 * operations prepare_write and/or commit_write are not available on the
68 * Anton Altaparmakov, 16 Feb 2005
71 * - Advisory locking is ignored here.
72 * - Should use an own CAP_* category instead of CAP_SYS_ADMIN
76 #include <linux/config.h>
77 #include <linux/module.h>
79 #include <linux/sched.h>
81 #include <linux/file.h>
82 #include <linux/stat.h>
83 #include <linux/errno.h>
84 #include <linux/major.h>
85 #include <linux/wait.h>
86 #include <linux/blkdev.h>
87 #include <linux/blkpg.h>
88 #include <linux/init.h>
89 #include <linux/smp_lock.h>
90 #include <linux/swap.h>
91 #include <linux/slab.h>
92 #include <linux/suspend.h>
93 #include <linux/writeback.h>
94 #include <linux/buffer_head.h> /* for invalidate_bdev() */
95 #include <linux/completion.h>
96 #include <linux/highmem.h>
97 #include <linux/gfp.h>
98 #include <linux/swap.h>
99 #include <linux/pagevec.h>
101 #include <asm/uaccess.h>
103 #include <lustre_lib.h>
104 #include <lustre_lite.h>
105 #include "llite_internal.h"
107 #define LLOOP_MAX_SEGMENTS PTLRPC_MAX_BRW_PAGES
109 /* Possible states of device */
116 struct lloop_device {
122 int (*ioctl)(struct lloop_device *, int cmd,
125 struct file * lo_backing_file;
126 struct block_device *lo_device;
127 unsigned lo_blocksize;
133 struct bio *lo_biotail;
135 struct semaphore lo_sem;
136 struct semaphore lo_ctl_mutex;
137 struct semaphore lo_bh_mutex;
140 request_queue_t *lo_queue;
142 /* data to handle bio for lustre. */
143 struct lo_request_data {
144 struct brw_page lrd_pages[LLOOP_MAX_SEGMENTS];
154 LO_FLAGS_READ_ONLY = 1,
157 static int lloop_major;
158 static int max_loop = 8;
159 static struct lloop_device *loop_dev;
160 static struct gendisk **disks;
161 static struct semaphore lloop_mutex;
162 static void *ll_iocontrol_magic = NULL;
164 static loff_t get_loop_size(struct lloop_device *lo, struct file *file)
166 loff_t size, offset, loopsize;
168 /* Compute loopsize in bytes */
169 size = i_size_read(file->f_mapping->host);
170 offset = lo->lo_offset;
171 loopsize = size - offset;
172 if (lo->lo_sizelimit > 0 && lo->lo_sizelimit < loopsize)
173 loopsize = lo->lo_sizelimit;
176 * Unfortunately, if we want to do I/O on the device,
177 * the number of 512-byte sectors has to fit into a sector_t.
179 return loopsize >> 9;
182 static int do_bio_filebacked(struct lloop_device *lo, struct bio *bio)
184 struct inode *inode = lo->lo_backing_file->f_dentry->d_inode;
185 struct ll_inode_info *lli = ll_i2info(inode);
186 struct lov_stripe_md *lsm = lli->lli_smd;
187 struct obd_info oinfo = {{{ 0 }}};
188 struct brw_page *pg = lo->lo_requests[0].lrd_pages;
189 struct obdo *oa = &lo->lo_requests[0].lrd_oa;
191 int ret, cmd, i, opc;
192 struct bio_vec *bvec;
194 BUG_ON(bio->bi_hw_segments > LLOOP_MAX_SEGMENTS);
196 offset = (pgoff_t)(bio->bi_sector << 9) + lo->lo_offset;
197 bio_for_each_segment(bvec, bio, i) {
198 BUG_ON(bvec->bv_offset != 0);
199 BUG_ON(bvec->bv_len != CFS_PAGE_SIZE);
201 pg->pg = bvec->bv_page;
203 pg->count = bvec->bv_len;
204 pg->flag = OBD_BRW_SRVLOCK;
207 offset += bvec->bv_len;
210 oa->o_mode = inode->i_mode;
211 oa->o_id = lsm->lsm_object_id;
212 oa->o_gr = lsm->lsm_object_gr;
213 oa->o_valid = OBD_MD_FLID | OBD_MD_FLMODE |
214 OBD_MD_FLTYPE |OBD_MD_FLGROUP;
215 obdo_from_inode(oa, inode, OBD_MD_FLFID | OBD_MD_FLGENER);
218 if (bio_rw(bio) == WRITE)
221 if (cmd == OBD_BRW_WRITE)
222 ll_stats_ops_tally(ll_i2sbi(inode), LPROC_LL_BRW_WRITE, bio->bi_size);
224 ll_stats_ops_tally(ll_i2sbi(inode), LPROC_LL_BRW_READ, bio->bi_size);
227 opc = cmd & OBD_BRW_WRITE ? CAPA_OPC_OSS_WRITE : CAPA_OPC_OSS_RW;
228 oinfo.oi_capa = ll_osscapa_get(inode, opc);
229 ret = obd_brw(cmd, ll_i2dtexp(inode), &oinfo,
230 (obd_count)(i - bio->bi_idx),
231 lo->lo_requests[0].lrd_pages, NULL);
232 capa_put(oinfo.oi_capa);
234 obdo_to_inode(inode, oa, OBD_MD_FLBLOCKS);
240 * Add bio to back of pending list
242 static void loop_add_bio(struct lloop_device *lo, struct bio *bio)
246 spin_lock_irqsave(&lo->lo_lock, flags);
247 if (lo->lo_biotail) {
248 lo->lo_biotail->bi_next = bio;
249 lo->lo_biotail = bio;
251 lo->lo_bio = lo->lo_biotail = bio;
252 spin_unlock_irqrestore(&lo->lo_lock, flags);
254 up(&lo->lo_bh_mutex);
258 * Grab first pending buffer
260 static struct bio *loop_get_bio(struct lloop_device *lo)
264 spin_lock_irq(&lo->lo_lock);
265 if ((bio = lo->lo_bio)) {
266 if (bio == lo->lo_biotail)
267 lo->lo_biotail = NULL;
268 lo->lo_bio = bio->bi_next;
271 spin_unlock_irq(&lo->lo_lock);
276 static int loop_make_request(request_queue_t *q, struct bio *old_bio)
278 struct lloop_device *lo = q->queuedata;
279 int rw = bio_rw(old_bio);
284 spin_lock_irq(&lo->lo_lock);
285 if (lo->lo_state != LLOOP_BOUND)
287 atomic_inc(&lo->lo_pending);
288 spin_unlock_irq(&lo->lo_lock);
291 if (lo->lo_flags & LO_FLAGS_READ_ONLY)
293 } else if (rw == READA) {
295 } else if (rw != READ) {
296 CERROR("lloop: unknown command (%x)\n", rw);
299 loop_add_bio(lo, old_bio);
302 if (atomic_dec_and_test(&lo->lo_pending))
303 up(&lo->lo_bh_mutex);
305 bio_io_error(old_bio, old_bio->bi_size);
308 spin_unlock_irq(&lo->lo_lock);
313 * kick off io on the underlying address space
315 static void loop_unplug(request_queue_t *q)
317 struct lloop_device *lo = q->queuedata;
319 clear_bit(QUEUE_FLAG_PLUGGED, &q->queue_flags);
320 blk_run_address_space(lo->lo_backing_file->f_mapping);
323 static inline void loop_handle_bio(struct lloop_device *lo, struct bio *bio)
326 ret = do_bio_filebacked(lo, bio);
327 bio_endio(bio, bio->bi_size, ret);
331 * worker thread that handles reads/writes to file backed loop devices,
332 * to avoid blocking in our make_request_fn. it also does loop decrypting
333 * on reads for block backed loop, as that is too heavy to do from
334 * b_end_io context where irqs may be disabled.
336 static int loop_thread(void *data)
338 struct lloop_device *lo = data;
341 daemonize("lloop%d", lo->lo_number);
343 set_user_nice(current, -20);
345 lo->lo_state = LLOOP_BOUND;
346 atomic_inc(&lo->lo_pending);
349 * up sem, we are running
354 down_interruptible(&lo->lo_bh_mutex);
356 * could be upped because of tear-down, not because of
359 if (!atomic_read(&lo->lo_pending))
362 bio = loop_get_bio(lo);
364 CWARN("lloop(minor: %d): missing bio\n", lo->lo_number);
367 loop_handle_bio(lo, bio);
370 * upped both for pending work and tear-down, lo_pending
373 if (atomic_dec_and_test(&lo->lo_pending))
381 static int loop_set_fd(struct lloop_device *lo, struct file *unused,
382 struct block_device *bdev, struct file *file)
385 struct address_space *mapping;
390 if (!try_module_get(THIS_MODULE))
394 if (lo->lo_state != LLOOP_UNBOUND)
397 mapping = file->f_mapping;
398 inode = mapping->host;
401 if (!S_ISREG(inode->i_mode) || inode->i_sb->s_magic != LL_SUPER_MAGIC)
404 if (!(file->f_mode & FMODE_WRITE))
405 lo_flags |= LO_FLAGS_READ_ONLY;
407 size = get_loop_size(lo, file);
409 if ((loff_t)(sector_t)size != size) {
414 /* remove all pages in cache so as dirty pages not to be existent. */
415 truncate_inode_pages(mapping, 0);
417 set_device_ro(bdev, (lo_flags & LO_FLAGS_READ_ONLY) != 0);
419 lo->lo_blocksize = CFS_PAGE_SIZE;
420 lo->lo_device = bdev;
421 lo->lo_flags = lo_flags;
422 lo->lo_backing_file = file;
424 lo->lo_sizelimit = 0;
425 lo->old_gfp_mask = mapping_gfp_mask(mapping);
426 mapping_set_gfp_mask(mapping, lo->old_gfp_mask & ~(__GFP_IO|__GFP_FS));
428 lo->lo_bio = lo->lo_biotail = NULL;
431 * set queue make_request_fn, and add limits based on lower level
434 blk_queue_make_request(lo->lo_queue, loop_make_request);
435 lo->lo_queue->queuedata = lo;
436 lo->lo_queue->unplug_fn = loop_unplug;
438 /* queue parameters */
439 blk_queue_hardsect_size(lo->lo_queue, CFS_PAGE_SIZE);
440 blk_queue_max_sectors(lo->lo_queue, LLOOP_MAX_SEGMENTS);
441 blk_queue_max_phys_segments(lo->lo_queue, LLOOP_MAX_SEGMENTS);
443 set_capacity(disks[lo->lo_number], size);
444 bd_set_size(bdev, size << 9);
446 set_blocksize(bdev, lo->lo_blocksize);
448 kernel_thread(loop_thread, lo, CLONE_KERNEL);
453 /* This is safe: open() is still holding a reference. */
454 module_put(THIS_MODULE);
458 static int loop_clr_fd(struct lloop_device *lo, struct block_device *bdev,
461 struct file *filp = lo->lo_backing_file;
462 int gfp = lo->old_gfp_mask;
464 if (lo->lo_state != LLOOP_BOUND)
467 if (lo->lo_refcnt > count) /* we needed one fd for the ioctl */
473 spin_lock_irq(&lo->lo_lock);
474 lo->lo_state = LLOOP_RUNDOWN;
475 if (atomic_dec_and_test(&lo->lo_pending))
476 up(&lo->lo_bh_mutex);
477 spin_unlock_irq(&lo->lo_lock);
480 lo->lo_backing_file = NULL;
482 lo->lo_device = NULL;
484 lo->lo_sizelimit = 0;
486 invalidate_bdev(bdev, 0);
487 set_capacity(disks[lo->lo_number], 0);
488 bd_set_size(bdev, 0);
489 mapping_set_gfp_mask(filp->f_mapping, gfp);
490 lo->lo_state = LLOOP_UNBOUND;
492 /* This is safe: open() is still holding a reference. */
493 module_put(THIS_MODULE);
497 static int lo_open(struct inode *inode, struct file *file)
499 struct lloop_device *lo = inode->i_bdev->bd_disk->private_data;
501 down(&lo->lo_ctl_mutex);
503 up(&lo->lo_ctl_mutex);
508 static int lo_release(struct inode *inode, struct file *file)
510 struct lloop_device *lo = inode->i_bdev->bd_disk->private_data;
512 down(&lo->lo_ctl_mutex);
514 up(&lo->lo_ctl_mutex);
519 /* lloop device node's ioctl function. */
520 static int lo_ioctl(struct inode *inode, struct file *unused,
521 unsigned int cmd, unsigned long arg)
523 struct lloop_device *lo = inode->i_bdev->bd_disk->private_data;
524 struct block_device *bdev = inode->i_bdev;
529 case LL_IOC_LLOOP_DETACH: {
530 err = loop_clr_fd(lo, bdev, 2);
532 blkdev_put(bdev); /* grabbed in LLOOP_ATTACH */
536 case LL_IOC_LLOOP_INFO: {
539 if (lo->lo_state == LLOOP_BOUND)
540 ino = lo->lo_backing_file->f_dentry->d_inode->i_ino;
542 if (put_user(ino, (__u64 *)arg))
556 static struct block_device_operations lo_fops = {
557 .owner = THIS_MODULE,
559 .release = lo_release,
563 /* dynamic iocontrol callback.
564 * This callback is registered in lloop_init and will be called by
566 * This is a llite regular file ioctl function. It takes the responsibility
567 * of attaching a file, and detaching a file by a lloop's device numner.
569 static enum llioc_iter lloop_ioctl(struct inode *unused, struct file *file,
570 unsigned int cmd, unsigned long arg,
571 void *magic, int *rcp)
573 struct lloop_device *lo = NULL;
574 struct block_device *bdev = NULL;
578 if (magic != ll_iocontrol_magic)
582 GOTO(out1, err = -ENODEV);
584 CWARN("Enter llop_ioctl\n");
588 case LL_IOC_LLOOP_ATTACH: {
589 struct lloop_device *lo_free = NULL;
592 for (i = 0; i < max_loop; i++, lo = NULL) {
594 if (lo->lo_state == LLOOP_UNBOUND) {
599 if (lo->lo_backing_file->f_dentry->d_inode ==
600 file->f_dentry->d_inode)
604 GOTO(out, err = -EBUSY);
607 dev = MKDEV(lloop_major, lo->lo_number);
609 /* quit if the used pointer is writable */
610 if (put_user((long)old_encode_dev(dev), (long*)arg))
611 GOTO(out, err = -EFAULT);
613 bdev = open_by_devnum(dev, file->f_mode);
615 GOTO(out, err = PTR_ERR(bdev));
618 err = loop_set_fd(lo, NULL, bdev, file);
627 case LL_IOC_LLOOP_DETACH_BYDEV: {
630 dev = old_decode_dev(arg);
631 if (MAJOR(dev) != lloop_major)
632 GOTO(out, err = -EINVAL);
635 if (minor > max_loop - 1)
636 GOTO(out, err = -EINVAL);
638 lo = &loop_dev[minor];
639 if (lo->lo_state != LLOOP_BOUND)
640 GOTO(out, err = -EINVAL);
642 bdev = lo->lo_device;
643 err = loop_clr_fd(lo, bdev, 1);
645 blkdev_put(bdev); /* grabbed in LLOOP_ATTACH */
663 static int __init lloop_init(void)
666 unsigned int cmdlist[] = {
668 LL_IOC_LLOOP_DETACH_BYDEV,
671 if (max_loop < 1 || max_loop > 256) {
672 CWARN("lloop: invalid max_loop (must be between"
673 " 1 and 256), using default (8)\n");
677 lloop_major = register_blkdev(0, "lloop");
681 ll_iocontrol_magic = ll_iocontrol_register(lloop_ioctl, 2, cmdlist);
682 if (ll_iocontrol_magic == NULL)
685 loop_dev = kmalloc(max_loop * sizeof(struct lloop_device), GFP_KERNEL);
688 memset(loop_dev, 0, max_loop * sizeof(struct lloop_device));
690 disks = kmalloc(max_loop * sizeof(struct gendisk *), GFP_KERNEL);
694 for (i = 0; i < max_loop; i++) {
695 disks[i] = alloc_disk(1);
700 init_MUTEX(&lloop_mutex);
702 for (i = 0; i < max_loop; i++) {
703 struct lloop_device *lo = &loop_dev[i];
704 struct gendisk *disk = disks[i];
706 memset(lo, 0, sizeof(*lo));
707 lo->lo_queue = blk_alloc_queue(GFP_KERNEL);
711 init_MUTEX(&lo->lo_ctl_mutex);
712 init_MUTEX_LOCKED(&lo->lo_sem);
713 init_MUTEX_LOCKED(&lo->lo_bh_mutex);
715 spin_lock_init(&lo->lo_lock);
716 disk->major = lloop_major;
717 disk->first_minor = i;
718 disk->fops = &lo_fops;
719 sprintf(disk->disk_name, "lloop%d", i);
720 disk->private_data = lo;
721 disk->queue = lo->lo_queue;
724 /* We cannot fail after we call this, so another loop!*/
725 for (i = 0; i < max_loop; i++)
731 blk_put_queue(loop_dev[i].lo_queue);
740 unregister_blkdev(lloop_major, "lloop");
741 ll_iocontrol_unregister(ll_iocontrol_magic);
742 CERROR("lloop: ran out of memory\n");
746 static void lloop_exit(void)
750 ll_iocontrol_unregister(ll_iocontrol_magic);
751 for (i = 0; i < max_loop; i++) {
752 del_gendisk(disks[i]);
753 blk_put_queue(loop_dev[i].lo_queue);
756 if (unregister_blkdev(lloop_major, "lloop"))
757 CWARN("lloop: cannot unregister blkdev\n");
763 module_init(lloop_init);
764 module_exit(lloop_exit);
766 CFS_MODULE_PARM(max_loop, "i", int, 0444, "maximum of lloop_device");