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 #ifndef AUTOCONF_INCLUDED
77 #include <linux/config.h>
79 #include <linux/module.h>
81 #include <linux/sched.h>
83 #include <linux/file.h>
84 #include <linux/stat.h>
85 #include <linux/errno.h>
86 #include <linux/major.h>
87 #include <linux/wait.h>
88 #include <linux/blkdev.h>
89 #include <linux/blkpg.h>
90 #include <linux/init.h>
91 #include <linux/smp_lock.h>
92 #include <linux/swap.h>
93 #include <linux/slab.h>
94 #include <linux/suspend.h>
95 #include <linux/writeback.h>
96 #include <linux/buffer_head.h> /* for invalidate_bdev() */
97 #include <linux/completion.h>
98 #include <linux/highmem.h>
99 #include <linux/gfp.h>
100 #include <linux/swap.h>
101 #include <linux/pagevec.h>
103 #include <asm/uaccess.h>
105 #include <lustre_lib.h>
106 #include <lustre_lite.h>
107 #include "llite_internal.h"
109 #define LLOOP_MAX_SEGMENTS PTLRPC_MAX_BRW_PAGES
111 /* Possible states of device */
118 struct lloop_device {
124 int (*ioctl)(struct lloop_device *, int cmd,
127 struct file * lo_backing_file;
128 struct block_device *lo_device;
129 unsigned lo_blocksize;
135 struct bio *lo_biotail;
137 struct semaphore lo_sem;
138 struct semaphore lo_ctl_mutex;
139 struct semaphore lo_bh_mutex;
142 request_queue_t *lo_queue;
144 /* data to handle bio for lustre. */
145 struct lo_request_data {
146 struct brw_page lrd_pages[LLOOP_MAX_SEGMENTS];
156 LO_FLAGS_READ_ONLY = 1,
159 static int lloop_major;
160 static int max_loop = 8;
161 static struct lloop_device *loop_dev;
162 static struct gendisk **disks;
163 static struct semaphore lloop_mutex;
164 static void *ll_iocontrol_magic = NULL;
166 static loff_t get_loop_size(struct lloop_device *lo, struct file *file)
168 loff_t size, offset, loopsize;
170 /* Compute loopsize in bytes */
171 size = i_size_read(file->f_mapping->host);
172 offset = lo->lo_offset;
173 loopsize = size - offset;
174 if (lo->lo_sizelimit > 0 && lo->lo_sizelimit < loopsize)
175 loopsize = lo->lo_sizelimit;
178 * Unfortunately, if we want to do I/O on the device,
179 * the number of 512-byte sectors has to fit into a sector_t.
181 return loopsize >> 9;
184 static int do_bio_filebacked(struct lloop_device *lo, struct bio *bio)
186 struct inode *inode = lo->lo_backing_file->f_dentry->d_inode;
187 struct ll_inode_info *lli = ll_i2info(inode);
188 struct lov_stripe_md *lsm = lli->lli_smd;
189 struct obd_info oinfo = {{{ 0 }}};
190 struct brw_page *pg = lo->lo_requests[0].lrd_pages;
191 struct obdo *oa = &lo->lo_requests[0].lrd_oa;
193 int ret, cmd, i, opc;
194 struct bio_vec *bvec;
196 BUG_ON(bio->bi_hw_segments > LLOOP_MAX_SEGMENTS);
198 offset = (pgoff_t)(bio->bi_sector << 9) + lo->lo_offset;
199 bio_for_each_segment(bvec, bio, i) {
200 BUG_ON(bvec->bv_offset != 0);
201 BUG_ON(bvec->bv_len != CFS_PAGE_SIZE);
203 pg->pg = bvec->bv_page;
205 pg->count = bvec->bv_len;
206 pg->flag = OBD_BRW_SRVLOCK;
209 offset += bvec->bv_len;
212 oa->o_mode = inode->i_mode;
213 oa->o_id = lsm->lsm_object_id;
214 oa->o_gr = lsm->lsm_object_gr;
215 oa->o_valid = OBD_MD_FLID | OBD_MD_FLMODE |
216 OBD_MD_FLTYPE |OBD_MD_FLGROUP;
217 obdo_from_inode(oa, inode, OBD_MD_FLFID | OBD_MD_FLGENER);
220 if (bio_rw(bio) == WRITE)
223 if (cmd == OBD_BRW_WRITE)
224 ll_stats_ops_tally(ll_i2sbi(inode), LPROC_LL_BRW_WRITE, bio->bi_size);
226 ll_stats_ops_tally(ll_i2sbi(inode), LPROC_LL_BRW_READ, bio->bi_size);
229 opc = cmd & OBD_BRW_WRITE ? CAPA_OPC_OSS_WRITE : CAPA_OPC_OSS_RW;
230 oinfo.oi_capa = ll_osscapa_get(inode, opc);
231 ret = obd_brw(cmd, ll_i2dtexp(inode), &oinfo,
232 (obd_count)(i - bio->bi_idx),
233 lo->lo_requests[0].lrd_pages, NULL);
234 capa_put(oinfo.oi_capa);
236 obdo_to_inode(inode, oa, OBD_MD_FLBLOCKS);
242 * Add bio to back of pending list
244 static void loop_add_bio(struct lloop_device *lo, struct bio *bio)
248 spin_lock_irqsave(&lo->lo_lock, flags);
249 if (lo->lo_biotail) {
250 lo->lo_biotail->bi_next = bio;
251 lo->lo_biotail = bio;
253 lo->lo_bio = lo->lo_biotail = bio;
254 spin_unlock_irqrestore(&lo->lo_lock, flags);
256 up(&lo->lo_bh_mutex);
260 * Grab first pending buffer
262 static struct bio *loop_get_bio(struct lloop_device *lo)
266 spin_lock_irq(&lo->lo_lock);
267 if ((bio = lo->lo_bio)) {
268 if (bio == lo->lo_biotail)
269 lo->lo_biotail = NULL;
270 lo->lo_bio = bio->bi_next;
273 spin_unlock_irq(&lo->lo_lock);
278 static int loop_make_request(request_queue_t *q, struct bio *old_bio)
280 struct lloop_device *lo = q->queuedata;
281 int rw = bio_rw(old_bio);
286 spin_lock_irq(&lo->lo_lock);
287 if (lo->lo_state != LLOOP_BOUND)
289 atomic_inc(&lo->lo_pending);
290 spin_unlock_irq(&lo->lo_lock);
293 if (lo->lo_flags & LO_FLAGS_READ_ONLY)
295 } else if (rw == READA) {
297 } else if (rw != READ) {
298 CERROR("lloop: unknown command (%x)\n", rw);
301 loop_add_bio(lo, old_bio);
304 if (atomic_dec_and_test(&lo->lo_pending))
305 up(&lo->lo_bh_mutex);
307 bio_io_error(old_bio, old_bio->bi_size);
310 spin_unlock_irq(&lo->lo_lock);
315 * kick off io on the underlying address space
317 static void loop_unplug(request_queue_t *q)
319 struct lloop_device *lo = q->queuedata;
321 clear_bit(QUEUE_FLAG_PLUGGED, &q->queue_flags);
322 blk_run_address_space(lo->lo_backing_file->f_mapping);
325 static inline void loop_handle_bio(struct lloop_device *lo, struct bio *bio)
328 ret = do_bio_filebacked(lo, bio);
329 bio_endio(bio, bio->bi_size, ret);
333 * worker thread that handles reads/writes to file backed loop devices,
334 * to avoid blocking in our make_request_fn. it also does loop decrypting
335 * on reads for block backed loop, as that is too heavy to do from
336 * b_end_io context where irqs may be disabled.
338 static int loop_thread(void *data)
340 struct lloop_device *lo = data;
343 daemonize("lloop%d", lo->lo_number);
345 set_user_nice(current, -20);
347 lo->lo_state = LLOOP_BOUND;
348 atomic_inc(&lo->lo_pending);
351 * up sem, we are running
356 down_interruptible(&lo->lo_bh_mutex);
358 * could be upped because of tear-down, not because of
361 if (!atomic_read(&lo->lo_pending))
364 bio = loop_get_bio(lo);
366 CWARN("lloop(minor: %d): missing bio\n", lo->lo_number);
369 loop_handle_bio(lo, bio);
372 * upped both for pending work and tear-down, lo_pending
375 if (atomic_dec_and_test(&lo->lo_pending))
383 static int loop_set_fd(struct lloop_device *lo, struct file *unused,
384 struct block_device *bdev, struct file *file)
387 struct address_space *mapping;
392 if (!try_module_get(THIS_MODULE))
396 if (lo->lo_state != LLOOP_UNBOUND)
399 mapping = file->f_mapping;
400 inode = mapping->host;
403 if (!S_ISREG(inode->i_mode) || inode->i_sb->s_magic != LL_SUPER_MAGIC)
406 if (!(file->f_mode & FMODE_WRITE))
407 lo_flags |= LO_FLAGS_READ_ONLY;
409 size = get_loop_size(lo, file);
411 if ((loff_t)(sector_t)size != size) {
416 /* remove all pages in cache so as dirty pages not to be existent. */
417 truncate_inode_pages(mapping, 0);
419 set_device_ro(bdev, (lo_flags & LO_FLAGS_READ_ONLY) != 0);
421 lo->lo_blocksize = CFS_PAGE_SIZE;
422 lo->lo_device = bdev;
423 lo->lo_flags = lo_flags;
424 lo->lo_backing_file = file;
426 lo->lo_sizelimit = 0;
427 lo->old_gfp_mask = mapping_gfp_mask(mapping);
428 mapping_set_gfp_mask(mapping, lo->old_gfp_mask & ~(__GFP_IO|__GFP_FS));
430 lo->lo_bio = lo->lo_biotail = NULL;
433 * set queue make_request_fn, and add limits based on lower level
436 blk_queue_make_request(lo->lo_queue, loop_make_request);
437 lo->lo_queue->queuedata = lo;
438 lo->lo_queue->unplug_fn = loop_unplug;
440 /* queue parameters */
441 blk_queue_hardsect_size(lo->lo_queue, CFS_PAGE_SIZE);
442 blk_queue_max_sectors(lo->lo_queue, LLOOP_MAX_SEGMENTS);
443 blk_queue_max_phys_segments(lo->lo_queue, LLOOP_MAX_SEGMENTS);
445 set_capacity(disks[lo->lo_number], size);
446 bd_set_size(bdev, size << 9);
448 set_blocksize(bdev, lo->lo_blocksize);
450 kernel_thread(loop_thread, lo, CLONE_KERNEL);
455 /* This is safe: open() is still holding a reference. */
456 module_put(THIS_MODULE);
460 static int loop_clr_fd(struct lloop_device *lo, struct block_device *bdev,
463 struct file *filp = lo->lo_backing_file;
464 int gfp = lo->old_gfp_mask;
466 if (lo->lo_state != LLOOP_BOUND)
469 if (lo->lo_refcnt > count) /* we needed one fd for the ioctl */
475 spin_lock_irq(&lo->lo_lock);
476 lo->lo_state = LLOOP_RUNDOWN;
477 if (atomic_dec_and_test(&lo->lo_pending))
478 up(&lo->lo_bh_mutex);
479 spin_unlock_irq(&lo->lo_lock);
482 lo->lo_backing_file = NULL;
484 lo->lo_device = NULL;
486 lo->lo_sizelimit = 0;
488 ll_invalidate_bdev(bdev, 0);
489 set_capacity(disks[lo->lo_number], 0);
490 bd_set_size(bdev, 0);
491 mapping_set_gfp_mask(filp->f_mapping, gfp);
492 lo->lo_state = LLOOP_UNBOUND;
494 /* This is safe: open() is still holding a reference. */
495 module_put(THIS_MODULE);
499 static int lo_open(struct inode *inode, struct file *file)
501 struct lloop_device *lo = inode->i_bdev->bd_disk->private_data;
503 down(&lo->lo_ctl_mutex);
505 up(&lo->lo_ctl_mutex);
510 static int lo_release(struct inode *inode, struct file *file)
512 struct lloop_device *lo = inode->i_bdev->bd_disk->private_data;
514 down(&lo->lo_ctl_mutex);
516 up(&lo->lo_ctl_mutex);
521 /* lloop device node's ioctl function. */
522 static int lo_ioctl(struct inode *inode, struct file *unused,
523 unsigned int cmd, unsigned long arg)
525 struct lloop_device *lo = inode->i_bdev->bd_disk->private_data;
526 struct block_device *bdev = inode->i_bdev;
531 case LL_IOC_LLOOP_DETACH: {
532 err = loop_clr_fd(lo, bdev, 2);
534 blkdev_put(bdev); /* grabbed in LLOOP_ATTACH */
538 case LL_IOC_LLOOP_INFO: {
541 if (lo->lo_state == LLOOP_BOUND)
542 ino = lo->lo_backing_file->f_dentry->d_inode->i_ino;
544 if (put_user(ino, (__u64 *)arg))
558 static struct block_device_operations lo_fops = {
559 .owner = THIS_MODULE,
561 .release = lo_release,
565 /* dynamic iocontrol callback.
566 * This callback is registered in lloop_init and will be called by
568 * This is a llite regular file ioctl function. It takes the responsibility
569 * of attaching a file, and detaching a file by a lloop's device numner.
571 static enum llioc_iter lloop_ioctl(struct inode *unused, struct file *file,
572 unsigned int cmd, unsigned long arg,
573 void *magic, int *rcp)
575 struct lloop_device *lo = NULL;
576 struct block_device *bdev = NULL;
580 if (magic != ll_iocontrol_magic)
584 GOTO(out1, err = -ENODEV);
586 CWARN("Enter llop_ioctl\n");
590 case LL_IOC_LLOOP_ATTACH: {
591 struct lloop_device *lo_free = NULL;
594 for (i = 0; i < max_loop; i++, lo = NULL) {
596 if (lo->lo_state == LLOOP_UNBOUND) {
601 if (lo->lo_backing_file->f_dentry->d_inode ==
602 file->f_dentry->d_inode)
606 GOTO(out, err = -EBUSY);
609 dev = MKDEV(lloop_major, lo->lo_number);
611 /* quit if the used pointer is writable */
612 if (put_user((long)old_encode_dev(dev), (long*)arg))
613 GOTO(out, err = -EFAULT);
615 bdev = open_by_devnum(dev, file->f_mode);
617 GOTO(out, err = PTR_ERR(bdev));
620 err = loop_set_fd(lo, NULL, bdev, file);
629 case LL_IOC_LLOOP_DETACH_BYDEV: {
632 dev = old_decode_dev(arg);
633 if (MAJOR(dev) != lloop_major)
634 GOTO(out, err = -EINVAL);
637 if (minor > max_loop - 1)
638 GOTO(out, err = -EINVAL);
640 lo = &loop_dev[minor];
641 if (lo->lo_state != LLOOP_BOUND)
642 GOTO(out, err = -EINVAL);
644 bdev = lo->lo_device;
645 err = loop_clr_fd(lo, bdev, 1);
647 blkdev_put(bdev); /* grabbed in LLOOP_ATTACH */
665 static int __init lloop_init(void)
668 unsigned int cmdlist[] = {
670 LL_IOC_LLOOP_DETACH_BYDEV,
673 if (max_loop < 1 || max_loop > 256) {
674 CWARN("lloop: invalid max_loop (must be between"
675 " 1 and 256), using default (8)\n");
679 lloop_major = register_blkdev(0, "lloop");
683 ll_iocontrol_magic = ll_iocontrol_register(lloop_ioctl, 2, cmdlist);
684 if (ll_iocontrol_magic == NULL)
687 loop_dev = kmalloc(max_loop * sizeof(struct lloop_device), GFP_KERNEL);
690 memset(loop_dev, 0, max_loop * sizeof(struct lloop_device));
692 disks = kmalloc(max_loop * sizeof(struct gendisk *), GFP_KERNEL);
696 for (i = 0; i < max_loop; i++) {
697 disks[i] = alloc_disk(1);
702 init_MUTEX(&lloop_mutex);
704 for (i = 0; i < max_loop; i++) {
705 struct lloop_device *lo = &loop_dev[i];
706 struct gendisk *disk = disks[i];
708 memset(lo, 0, sizeof(*lo));
709 lo->lo_queue = blk_alloc_queue(GFP_KERNEL);
713 init_MUTEX(&lo->lo_ctl_mutex);
714 init_MUTEX_LOCKED(&lo->lo_sem);
715 init_MUTEX_LOCKED(&lo->lo_bh_mutex);
717 spin_lock_init(&lo->lo_lock);
718 disk->major = lloop_major;
719 disk->first_minor = i;
720 disk->fops = &lo_fops;
721 sprintf(disk->disk_name, "lloop%d", i);
722 disk->private_data = lo;
723 disk->queue = lo->lo_queue;
726 /* We cannot fail after we call this, so another loop!*/
727 for (i = 0; i < max_loop; i++)
733 blk_put_queue(loop_dev[i].lo_queue);
742 unregister_blkdev(lloop_major, "lloop");
743 ll_iocontrol_unregister(ll_iocontrol_magic);
744 CERROR("lloop: ran out of memory\n");
748 static void lloop_exit(void)
752 ll_iocontrol_unregister(ll_iocontrol_magic);
753 for (i = 0; i < max_loop; i++) {
754 del_gendisk(disks[i]);
755 blk_put_queue(loop_dev[i].lo_queue);
758 if (ll_unregister_blkdev(lloop_major, "lloop"))
759 CWARN("lloop: cannot unregister blkdev\n");
765 module_init(lloop_init);
766 module_exit(lloop_exit);
768 CFS_MODULE_PARM(max_loop, "i", int, 0444, "maximum of lloop_device");
769 MODULE_AUTHOR("Cluster File Systems, Inc. <info@clusterfs.com>");
770 MODULE_DESCRIPTION("Lustre virtual block device");
771 MODULE_LICENSE("GPL");