1 /* -*- mode: c; c-basic-offset: 8; indent-tabs-mode: nil; -*-
2 * vim:expandtab:shiftwidth=8:tabstop=8:
6 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
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.
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).
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
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
29 * Copyright 2008 Sun Microsystems, Inc. All rights reserved
30 * Use is subject to license terms.
33 * This file is part of Lustre, http://www.lustre.org/
34 * Lustre is a trademark of Sun Microsystems, Inc.
38 * linux/drivers/block/loop.c
40 * Written by Theodore Ts'o, 3/29/93
42 * Copyright 1993 by Theodore Ts'o. Redistribution of this file is
43 * permitted under the GNU General Public License.
45 * DES encryption plus some minor changes by Werner Almesberger, 30-MAY-1993
46 * more DES encryption plus IDEA encryption by Nicholas J. Leon, June 20, 1996
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
51 * Fixed do_loop_request() re-entrancy - Vincent.Renardias@waw.com Mar 20, 1997
53 * Added devfs support - Richard Gooch <rgooch@atnf.csiro.au> 16-Jan-1998
55 * Handle sparse backing files correctly - Kenn Humborg, Jun 28, 1998
57 * Loadable modules and other fixes by AK, 1998
59 * Make real block number available to downstream transfer functions, enables
60 * CBC (and relatives) mode encryption requiring unique IVs per data block.
61 * Reed H. Petty, rhp@draper.net
63 * Maximum number of loop devices now dynamic via max_loop module parameter.
64 * Russell Kroll <rkroll@exploits.org> 19990701
66 * Maximum number of loop devices when compiled-in now selectable by passing
67 * max_loop=<1-255> to the kernel on boot.
68 * Erik I. Bols?, <eriki@himolde.no>, Oct 31, 1999
70 * Completely rewrite request handling to be make_request_fn style and
71 * non blocking, pushing work to a helper thread. Lots of fixes from
73 * Jens Axboe <axboe@suse.de>, Nov 2000
75 * Support up to 256 loop devices
76 * Heinz Mauelshagen <mge@sistina.com>, Feb 2002
78 * Support for falling back on the write file operation when the address space
79 * operations prepare_write and/or commit_write are not available on the
81 * Anton Altaparmakov, 16 Feb 2005
84 * - Advisory locking is ignored here.
85 * - Should use an own CAP_* category instead of CAP_SYS_ADMIN
89 #ifndef AUTOCONF_INCLUDED
90 #include <linux/config.h>
92 #include <linux/module.h>
94 #include <linux/sched.h>
96 #include <linux/file.h>
97 #include <linux/stat.h>
98 #include <linux/errno.h>
99 #include <linux/major.h>
100 #include <linux/wait.h>
101 #include <linux/blkdev.h>
102 #include <linux/blkpg.h>
103 #include <linux/init.h>
104 #include <linux/smp_lock.h>
105 #include <linux/swap.h>
106 #include <linux/slab.h>
107 #include <linux/suspend.h>
108 #include <linux/writeback.h>
109 #include <linux/buffer_head.h> /* for invalidate_bdev() */
110 #include <linux/completion.h>
111 #include <linux/highmem.h>
112 #include <linux/gfp.h>
113 #include <linux/swap.h>
114 #include <linux/pagevec.h>
116 #include <asm/uaccess.h>
118 #include <lustre_lib.h>
119 #include <lustre_lite.h>
120 #include "llite_internal.h"
122 #define LLOOP_MAX_SEGMENTS PTLRPC_MAX_BRW_PAGES
124 /* Possible states of device */
131 struct lloop_device {
137 int (*ioctl)(struct lloop_device *, int cmd,
140 struct file * lo_backing_file;
141 struct block_device *lo_device;
142 unsigned lo_blocksize;
148 struct bio *lo_biotail;
150 struct semaphore lo_sem;
151 struct semaphore lo_ctl_mutex;
152 struct semaphore lo_bh_mutex;
155 request_queue_t *lo_queue;
157 /* data to handle bio for lustre. */
158 struct lo_request_data {
159 struct brw_page lrd_pages[LLOOP_MAX_SEGMENTS];
169 LO_FLAGS_READ_ONLY = 1,
172 static int lloop_major;
173 static int max_loop = 8;
174 static struct lloop_device *loop_dev;
175 static struct gendisk **disks;
176 static struct semaphore lloop_mutex;
177 static void *ll_iocontrol_magic = NULL;
179 static loff_t get_loop_size(struct lloop_device *lo, struct file *file)
181 loff_t size, offset, loopsize;
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;
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.
194 return loopsize >> 9;
197 static int do_bio_filebacked(struct lloop_device *lo, struct bio *bio)
199 struct inode *inode = lo->lo_backing_file->f_dentry->d_inode;
200 struct ll_inode_info *lli = ll_i2info(inode);
201 struct lov_stripe_md *lsm = lli->lli_smd;
202 struct obd_info oinfo = {{{ 0 }}};
203 struct brw_page *pg = lo->lo_requests[0].lrd_pages;
204 struct obdo *oa = &lo->lo_requests[0].lrd_oa;
206 int ret, cmd, i, opc;
207 struct bio_vec *bvec;
209 BUG_ON(bio->bi_hw_segments > LLOOP_MAX_SEGMENTS);
211 offset = (pgoff_t)(bio->bi_sector << 9) + lo->lo_offset;
212 bio_for_each_segment(bvec, bio, i) {
213 BUG_ON(bvec->bv_offset != 0);
214 BUG_ON(bvec->bv_len != CFS_PAGE_SIZE);
216 pg->pg = bvec->bv_page;
218 pg->count = bvec->bv_len;
219 pg->flag = OBD_BRW_SRVLOCK;
222 offset += bvec->bv_len;
225 oa->o_mode = inode->i_mode;
226 oa->o_id = lsm->lsm_object_id;
227 oa->o_gr = lsm->lsm_object_gr;
228 oa->o_valid = OBD_MD_FLID | OBD_MD_FLMODE |
229 OBD_MD_FLTYPE |OBD_MD_FLGROUP;
230 obdo_from_inode(oa, inode, OBD_MD_FLFID | OBD_MD_FLGENER);
233 if (bio_rw(bio) == WRITE)
236 if (cmd == OBD_BRW_WRITE)
237 ll_stats_ops_tally(ll_i2sbi(inode), LPROC_LL_BRW_WRITE, bio->bi_size);
239 ll_stats_ops_tally(ll_i2sbi(inode), LPROC_LL_BRW_READ, bio->bi_size);
242 opc = cmd & OBD_BRW_WRITE ? CAPA_OPC_OSS_WRITE : CAPA_OPC_OSS_RW;
243 oinfo.oi_capa = ll_osscapa_get(inode, opc);
244 ret = obd_brw(cmd, ll_i2dtexp(inode), &oinfo,
245 (obd_count)(i - bio->bi_idx),
246 lo->lo_requests[0].lrd_pages, NULL);
247 capa_put(oinfo.oi_capa);
249 obdo_to_inode(inode, oa, OBD_MD_FLBLOCKS);
255 * Add bio to back of pending list
257 static void loop_add_bio(struct lloop_device *lo, struct bio *bio)
261 spin_lock_irqsave(&lo->lo_lock, flags);
262 if (lo->lo_biotail) {
263 lo->lo_biotail->bi_next = bio;
264 lo->lo_biotail = bio;
266 lo->lo_bio = lo->lo_biotail = bio;
267 spin_unlock_irqrestore(&lo->lo_lock, flags);
269 up(&lo->lo_bh_mutex);
273 * Grab first pending buffer
275 static struct bio *loop_get_bio(struct lloop_device *lo)
279 spin_lock_irq(&lo->lo_lock);
280 if ((bio = lo->lo_bio)) {
281 if (bio == lo->lo_biotail)
282 lo->lo_biotail = NULL;
283 lo->lo_bio = bio->bi_next;
286 spin_unlock_irq(&lo->lo_lock);
291 static int loop_make_request(request_queue_t *q, struct bio *old_bio)
293 struct lloop_device *lo = q->queuedata;
294 int rw = bio_rw(old_bio);
299 spin_lock_irq(&lo->lo_lock);
300 if (lo->lo_state != LLOOP_BOUND)
302 atomic_inc(&lo->lo_pending);
303 spin_unlock_irq(&lo->lo_lock);
306 if (lo->lo_flags & LO_FLAGS_READ_ONLY)
308 } else if (rw == READA) {
310 } else if (rw != READ) {
311 CERROR("lloop: unknown command (%x)\n", rw);
314 loop_add_bio(lo, old_bio);
317 if (atomic_dec_and_test(&lo->lo_pending))
318 up(&lo->lo_bh_mutex);
320 bio_io_error(old_bio, old_bio->bi_size);
323 spin_unlock_irq(&lo->lo_lock);
328 * kick off io on the underlying address space
330 static void loop_unplug(request_queue_t *q)
332 struct lloop_device *lo = q->queuedata;
334 clear_bit(QUEUE_FLAG_PLUGGED, &q->queue_flags);
335 blk_run_address_space(lo->lo_backing_file->f_mapping);
338 static inline void loop_handle_bio(struct lloop_device *lo, struct bio *bio)
341 ret = do_bio_filebacked(lo, bio);
342 bio_endio(bio, bio->bi_size, ret);
346 * worker thread that handles reads/writes to file backed loop devices,
347 * to avoid blocking in our make_request_fn. it also does loop decrypting
348 * on reads for block backed loop, as that is too heavy to do from
349 * b_end_io context where irqs may be disabled.
351 static int loop_thread(void *data)
353 struct lloop_device *lo = data;
356 daemonize("lloop%d", lo->lo_number);
358 set_user_nice(current, -20);
360 lo->lo_state = LLOOP_BOUND;
361 atomic_inc(&lo->lo_pending);
364 * up sem, we are running
369 down_interruptible(&lo->lo_bh_mutex);
371 * could be upped because of tear-down, not because of
374 if (!atomic_read(&lo->lo_pending))
377 bio = loop_get_bio(lo);
379 CWARN("lloop(minor: %d): missing bio\n", lo->lo_number);
382 loop_handle_bio(lo, bio);
385 * upped both for pending work and tear-down, lo_pending
388 if (atomic_dec_and_test(&lo->lo_pending))
396 static int loop_set_fd(struct lloop_device *lo, struct file *unused,
397 struct block_device *bdev, struct file *file)
400 struct address_space *mapping;
405 if (!try_module_get(THIS_MODULE))
409 if (lo->lo_state != LLOOP_UNBOUND)
412 mapping = file->f_mapping;
413 inode = mapping->host;
416 if (!S_ISREG(inode->i_mode) || inode->i_sb->s_magic != LL_SUPER_MAGIC)
419 if (!(file->f_mode & FMODE_WRITE))
420 lo_flags |= LO_FLAGS_READ_ONLY;
422 size = get_loop_size(lo, file);
424 if ((loff_t)(sector_t)size != size) {
429 /* remove all pages in cache so as dirty pages not to be existent. */
430 truncate_inode_pages(mapping, 0);
432 set_device_ro(bdev, (lo_flags & LO_FLAGS_READ_ONLY) != 0);
434 lo->lo_blocksize = CFS_PAGE_SIZE;
435 lo->lo_device = bdev;
436 lo->lo_flags = lo_flags;
437 lo->lo_backing_file = file;
439 lo->lo_sizelimit = 0;
440 lo->old_gfp_mask = mapping_gfp_mask(mapping);
441 mapping_set_gfp_mask(mapping, lo->old_gfp_mask & ~(__GFP_IO|__GFP_FS));
443 lo->lo_bio = lo->lo_biotail = NULL;
446 * set queue make_request_fn, and add limits based on lower level
449 blk_queue_make_request(lo->lo_queue, loop_make_request);
450 lo->lo_queue->queuedata = lo;
451 lo->lo_queue->unplug_fn = loop_unplug;
453 /* queue parameters */
454 blk_queue_hardsect_size(lo->lo_queue, CFS_PAGE_SIZE);
455 blk_queue_max_sectors(lo->lo_queue, LLOOP_MAX_SEGMENTS);
456 blk_queue_max_phys_segments(lo->lo_queue, LLOOP_MAX_SEGMENTS);
458 set_capacity(disks[lo->lo_number], size);
459 bd_set_size(bdev, size << 9);
461 set_blocksize(bdev, lo->lo_blocksize);
463 kernel_thread(loop_thread, lo, CLONE_KERNEL);
468 /* This is safe: open() is still holding a reference. */
469 module_put(THIS_MODULE);
473 static int loop_clr_fd(struct lloop_device *lo, struct block_device *bdev,
476 struct file *filp = lo->lo_backing_file;
477 int gfp = lo->old_gfp_mask;
479 if (lo->lo_state != LLOOP_BOUND)
482 if (lo->lo_refcnt > count) /* we needed one fd for the ioctl */
488 spin_lock_irq(&lo->lo_lock);
489 lo->lo_state = LLOOP_RUNDOWN;
490 if (atomic_dec_and_test(&lo->lo_pending))
491 up(&lo->lo_bh_mutex);
492 spin_unlock_irq(&lo->lo_lock);
495 lo->lo_backing_file = NULL;
497 lo->lo_device = NULL;
499 lo->lo_sizelimit = 0;
501 ll_invalidate_bdev(bdev, 0);
502 set_capacity(disks[lo->lo_number], 0);
503 bd_set_size(bdev, 0);
504 mapping_set_gfp_mask(filp->f_mapping, gfp);
505 lo->lo_state = LLOOP_UNBOUND;
507 /* This is safe: open() is still holding a reference. */
508 module_put(THIS_MODULE);
512 static int lo_open(struct inode *inode, struct file *file)
514 struct lloop_device *lo = inode->i_bdev->bd_disk->private_data;
516 down(&lo->lo_ctl_mutex);
518 up(&lo->lo_ctl_mutex);
523 static int lo_release(struct inode *inode, struct file *file)
525 struct lloop_device *lo = inode->i_bdev->bd_disk->private_data;
527 down(&lo->lo_ctl_mutex);
529 up(&lo->lo_ctl_mutex);
534 /* lloop device node's ioctl function. */
535 static int lo_ioctl(struct inode *inode, struct file *unused,
536 unsigned int cmd, unsigned long arg)
538 struct lloop_device *lo = inode->i_bdev->bd_disk->private_data;
539 struct block_device *bdev = inode->i_bdev;
544 case LL_IOC_LLOOP_DETACH: {
545 err = loop_clr_fd(lo, bdev, 2);
547 blkdev_put(bdev); /* grabbed in LLOOP_ATTACH */
551 case LL_IOC_LLOOP_INFO: {
554 if (lo->lo_state == LLOOP_BOUND)
555 ino = lo->lo_backing_file->f_dentry->d_inode->i_ino;
557 if (put_user(ino, (__u64 *)arg))
571 static struct block_device_operations lo_fops = {
572 .owner = THIS_MODULE,
574 .release = lo_release,
578 /* dynamic iocontrol callback.
579 * This callback is registered in lloop_init and will be called by
581 * This is a llite regular file ioctl function. It takes the responsibility
582 * of attaching a file, and detaching a file by a lloop's device numner.
584 static enum llioc_iter lloop_ioctl(struct inode *unused, struct file *file,
585 unsigned int cmd, unsigned long arg,
586 void *magic, int *rcp)
588 struct lloop_device *lo = NULL;
589 struct block_device *bdev = NULL;
593 if (magic != ll_iocontrol_magic)
597 GOTO(out1, err = -ENODEV);
599 CWARN("Enter llop_ioctl\n");
603 case LL_IOC_LLOOP_ATTACH: {
604 struct lloop_device *lo_free = NULL;
607 for (i = 0; i < max_loop; i++, lo = NULL) {
609 if (lo->lo_state == LLOOP_UNBOUND) {
614 if (lo->lo_backing_file->f_dentry->d_inode ==
615 file->f_dentry->d_inode)
619 GOTO(out, err = -EBUSY);
622 dev = MKDEV(lloop_major, lo->lo_number);
624 /* quit if the used pointer is writable */
625 if (put_user((long)old_encode_dev(dev), (long*)arg))
626 GOTO(out, err = -EFAULT);
628 bdev = open_by_devnum(dev, file->f_mode);
630 GOTO(out, err = PTR_ERR(bdev));
633 err = loop_set_fd(lo, NULL, bdev, file);
642 case LL_IOC_LLOOP_DETACH_BYDEV: {
645 dev = old_decode_dev(arg);
646 if (MAJOR(dev) != lloop_major)
647 GOTO(out, err = -EINVAL);
650 if (minor > max_loop - 1)
651 GOTO(out, err = -EINVAL);
653 lo = &loop_dev[minor];
654 if (lo->lo_state != LLOOP_BOUND)
655 GOTO(out, err = -EINVAL);
657 bdev = lo->lo_device;
658 err = loop_clr_fd(lo, bdev, 1);
660 blkdev_put(bdev); /* grabbed in LLOOP_ATTACH */
678 static int __init lloop_init(void)
681 unsigned int cmdlist[] = {
683 LL_IOC_LLOOP_DETACH_BYDEV,
686 if (max_loop < 1 || max_loop > 256) {
687 CWARN("lloop: invalid max_loop (must be between"
688 " 1 and 256), using default (8)\n");
692 lloop_major = register_blkdev(0, "lloop");
696 ll_iocontrol_magic = ll_iocontrol_register(lloop_ioctl, 2, cmdlist);
697 if (ll_iocontrol_magic == NULL)
700 loop_dev = kmalloc(max_loop * sizeof(struct lloop_device), GFP_KERNEL);
703 memset(loop_dev, 0, max_loop * sizeof(struct lloop_device));
705 disks = kmalloc(max_loop * sizeof(struct gendisk *), GFP_KERNEL);
709 for (i = 0; i < max_loop; i++) {
710 disks[i] = alloc_disk(1);
715 init_MUTEX(&lloop_mutex);
717 for (i = 0; i < max_loop; i++) {
718 struct lloop_device *lo = &loop_dev[i];
719 struct gendisk *disk = disks[i];
721 memset(lo, 0, sizeof(*lo));
722 lo->lo_queue = blk_alloc_queue(GFP_KERNEL);
726 init_MUTEX(&lo->lo_ctl_mutex);
727 init_MUTEX_LOCKED(&lo->lo_sem);
728 init_MUTEX_LOCKED(&lo->lo_bh_mutex);
730 spin_lock_init(&lo->lo_lock);
731 disk->major = lloop_major;
732 disk->first_minor = i;
733 disk->fops = &lo_fops;
734 sprintf(disk->disk_name, "lloop%d", i);
735 disk->private_data = lo;
736 disk->queue = lo->lo_queue;
739 /* We cannot fail after we call this, so another loop!*/
740 for (i = 0; i < max_loop; i++)
746 blk_put_queue(loop_dev[i].lo_queue);
755 unregister_blkdev(lloop_major, "lloop");
756 ll_iocontrol_unregister(ll_iocontrol_magic);
757 CERROR("lloop: ran out of memory\n");
761 static void lloop_exit(void)
765 ll_iocontrol_unregister(ll_iocontrol_magic);
766 for (i = 0; i < max_loop; i++) {
767 del_gendisk(disks[i]);
768 blk_put_queue(loop_dev[i].lo_queue);
771 if (ll_unregister_blkdev(lloop_major, "lloop"))
772 CWARN("lloop: cannot unregister blkdev\n");
778 module_init(lloop_init);
779 module_exit(lloop_exit);
781 CFS_MODULE_PARM(max_loop, "i", int, 0444, "maximum of lloop_device");
782 MODULE_AUTHOR("Sun Microsystems, Inc. <http://www.lustre.org/>");
783 MODULE_DESCRIPTION("Lustre virtual block device");
784 MODULE_LICENSE("GPL");