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 * 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
48 * Fixed do_loop_request() re-entrancy - Vincent.Renardias@waw.com Mar 20, 1997
50 * Added devfs support - Richard Gooch <rgooch@atnf.csiro.au> 16-Jan-1998
52 * Handle sparse backing files correctly - Kenn Humborg, Jun 28, 1998
54 * Loadable modules and other fixes by AK, 1998
56 * Maximum number of loop devices now dynamic via max_loop module parameter.
57 * Russell Kroll <rkroll@exploits.org> 19990701
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
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
66 * Jens Axboe <axboe@suse.de>, Nov 2000
68 * Support up to 256 loop devices
69 * Heinz Mauelshagen <mge@sistina.com>, Feb 2002
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
74 * Anton Altaparmakov, 16 Feb 2005
77 * - Advisory locking is ignored here.
78 * - Should use an own CAP_* category instead of CAP_SYS_ADMIN
82 #ifndef AUTOCONF_INCLUDED
83 #include <linux/config.h>
85 #include <linux/module.h>
87 #include <linux/sched.h>
89 #include <linux/file.h>
90 #include <linux/stat.h>
91 #include <linux/errno.h>
92 #include <linux/major.h>
93 #include <linux/wait.h>
94 #include <linux/blkdev.h>
95 #include <linux/blkpg.h>
96 #include <linux/init.h>
97 #include <linux/smp_lock.h>
98 #include <linux/swap.h>
99 #include <linux/slab.h>
100 #include <linux/suspend.h>
101 #include <linux/writeback.h>
102 #include <linux/buffer_head.h> /* for invalidate_bdev() */
103 #include <linux/completion.h>
104 #include <linux/highmem.h>
105 #include <linux/gfp.h>
106 #include <linux/swap.h>
107 #include <linux/pagevec.h>
109 #include <asm/uaccess.h>
111 #include <lustre_lib.h>
112 #include <lustre_lite.h>
113 #include "llite_internal.h"
115 #define LLOOP_MAX_SEGMENTS PTLRPC_MAX_BRW_PAGES
117 /* Possible states of device */
124 struct lloop_device {
130 int (*ioctl)(struct lloop_device *, int cmd,
133 struct file * lo_backing_file;
134 struct block_device *lo_device;
135 unsigned lo_blocksize;
141 struct bio *lo_biotail;
143 struct semaphore lo_sem;
144 struct semaphore lo_ctl_mutex;
146 wait_queue_head_t lo_bh_wait;
148 struct request_queue *lo_queue;
150 /* data to handle bio for lustre. */
151 struct lo_request_data {
152 struct brw_page lrd_pages[LLOOP_MAX_SEGMENTS];
161 LO_FLAGS_READ_ONLY = 1,
164 #define MAX_LOOP_DEFAULT 16
165 static int lloop_major;
166 static int max_loop = MAX_LOOP_DEFAULT;
167 static struct lloop_device *loop_dev;
168 static struct gendisk **disks;
169 static struct semaphore lloop_mutex;
170 static void *ll_iocontrol_magic = NULL;
172 static loff_t get_loop_size(struct lloop_device *lo, struct file *file)
174 loff_t size, offset, loopsize;
176 /* Compute loopsize in bytes */
177 size = i_size_read(file->f_mapping->host);
178 offset = lo->lo_offset;
179 loopsize = size - offset;
180 if (lo->lo_sizelimit > 0 && lo->lo_sizelimit < loopsize)
181 loopsize = lo->lo_sizelimit;
184 * Unfortunately, if we want to do I/O on the device,
185 * the number of 512-byte sectors has to fit into a sector_t.
187 return loopsize >> 9;
190 static int do_bio_lustrebacked(struct lloop_device *lo, struct bio *head)
192 struct inode *inode = lo->lo_backing_file->f_dentry->d_inode;
193 struct ll_inode_info *lli = ll_i2info(inode);
194 struct lov_stripe_md *lsm = lli->lli_smd;
195 struct obd_info oinfo = {{{0}}};
196 struct brw_page *pg = lo->lo_requests[0].lrd_pages;
197 struct obdo *oa = &lo->lo_requests[0].lrd_oa;
200 obd_count page_count = 0;
201 struct bio_vec *bvec;
204 LASSERT(head != NULL);
207 for (bio = head; bio != NULL; bio = bio->bi_next) {
208 LASSERT(rw == bio->bi_rw);
210 offset = (pgoff_t)(bio->bi_sector << 9) + lo->lo_offset;
211 bio_for_each_segment(bvec, bio, i) {
212 BUG_ON(bvec->bv_offset != 0);
213 BUG_ON(bvec->bv_len != CFS_PAGE_SIZE);
215 pg->pg = bvec->bv_page;
217 pg->count = bvec->bv_len;
218 pg->flag = OBD_BRW_SRVLOCK;
220 CDEBUG(D_INFO, "index %lu offset "LPU64", count %u\n",
221 pg->pg->index, pg->off, pg->count);
224 offset += bvec->bv_len;
226 LASSERT(page_count <= LLOOP_MAX_SEGMENTS);
229 ll_stats_ops_tally(ll_i2sbi(inode),
230 (rw == WRITE) ? LPROC_LL_BRW_WRITE : LPROC_LL_BRW_READ,
231 page_count << PAGE_CACHE_SHIFT);
233 oa->o_mode = inode->i_mode;
234 oa->o_id = lsm->lsm_object_id;
235 oa->o_gr = lsm->lsm_object_gr;
236 oa->o_valid = OBD_MD_FLID | OBD_MD_FLGROUP |
237 OBD_MD_FLMODE | OBD_MD_FLTYPE;
238 obdo_from_inode(oa, inode, OBD_MD_FLFID | OBD_MD_FLGENER);
242 ret = obd_brw((rw == WRITE) ? OBD_BRW_WRITE : OBD_BRW_READ,
243 ll_i2obdexp(inode), &oinfo, (obd_count)page_count,
244 lo->lo_requests[0].lrd_pages, NULL);
246 obdo_to_inode(inode, oa, OBD_MD_FLBLOCKS);
252 * Add bio to back of pending list
254 static void loop_add_bio(struct lloop_device *lo, struct bio *bio)
258 spin_lock_irqsave(&lo->lo_lock, flags);
259 if (lo->lo_biotail) {
260 lo->lo_biotail->bi_next = bio;
261 lo->lo_biotail = bio;
263 lo->lo_bio = lo->lo_biotail = bio;
264 spin_unlock_irqrestore(&lo->lo_lock, flags);
266 atomic_inc(&lo->lo_pending);
267 if (waitqueue_active(&lo->lo_bh_wait))
268 wake_up(&lo->lo_bh_wait);
272 * Grab first pending buffer
274 static unsigned int loop_get_bio(struct lloop_device *lo, struct bio **req)
278 unsigned int count = 0;
279 unsigned int page_count = 0;
282 spin_lock_irq(&lo->lo_lock);
284 if (unlikely(first == NULL)) {
285 spin_unlock_irq(&lo->lo_lock);
289 /* TODO: need to split the bio, too bad. */
290 LASSERT(first->bi_vcnt <= LLOOP_MAX_SEGMENTS);
294 while (*bio && (*bio)->bi_rw == rw) {
295 CDEBUG(D_INFO, "bio sector %llu size %u count %u vcnt%u \n",
296 (unsigned long long)(*bio)->bi_sector, (*bio)->bi_size,
297 page_count, (*bio)->bi_vcnt);
298 if (page_count + (*bio)->bi_vcnt > LLOOP_MAX_SEGMENTS)
302 page_count += (*bio)->bi_vcnt;
304 bio = &(*bio)->bi_next;
307 /* Some of bios can't be mergable. */
311 /* Hit the end of queue */
312 lo->lo_biotail = NULL;
316 spin_unlock_irq(&lo->lo_lock);
320 static int loop_make_request(struct request_queue *q, struct bio *old_bio)
322 struct lloop_device *lo = q->queuedata;
323 int rw = bio_rw(old_bio);
329 CDEBUG(D_INFO, "submit bio sector %llu size %u\n",
330 (unsigned long long)old_bio->bi_sector, old_bio->bi_size);
332 spin_lock_irq(&lo->lo_lock);
333 inactive = (lo->lo_state != LLOOP_BOUND);
334 spin_unlock_irq(&lo->lo_lock);
339 if (lo->lo_flags & LO_FLAGS_READ_ONLY)
341 } else if (rw == READA) {
343 } else if (rw != READ) {
344 CERROR("lloop: unknown command (%x)\n", rw);
347 loop_add_bio(lo, old_bio);
350 cfs_bio_io_error(old_bio, old_bio->bi_size);
355 * kick off io on the underlying address space
357 static void loop_unplug(struct request_queue *q)
359 struct lloop_device *lo = q->queuedata;
361 clear_bit(QUEUE_FLAG_PLUGGED, &q->queue_flags);
362 blk_run_address_space(lo->lo_backing_file->f_mapping);
365 static inline void loop_handle_bio(struct lloop_device *lo, struct bio *bio)
368 ret = do_bio_lustrebacked(lo, bio);
370 struct bio *tmp = bio->bi_next;
372 cfs_bio_endio(bio, bio->bi_size, ret);
377 static inline int loop_active(struct lloop_device *lo)
379 return atomic_read(&lo->lo_pending) || (lo->lo_state == LLOOP_RUNDOWN);
383 * worker thread that handles reads/writes to file backed loop devices,
384 * to avoid blocking in our make_request_fn.
386 static int loop_thread(void *data)
388 struct lloop_device *lo = data;
391 unsigned long times = 0;
392 unsigned long total_count = 0;
394 daemonize("lloop%d", lo->lo_number);
396 set_user_nice(current, -20);
398 lo->lo_state = LLOOP_BOUND;
401 * up sem, we are running
406 wait_event(lo->lo_bh_wait, loop_active(lo));
407 if (!atomic_read(&lo->lo_pending)) {
409 spin_lock_irq(&lo->lo_lock);
410 exiting = (lo->lo_state == LLOOP_RUNDOWN);
411 spin_unlock_irq(&lo->lo_lock);
417 count = loop_get_bio(lo, &bio);
419 CWARN("lloop(minor: %d): missing bio\n", lo->lo_number);
423 total_count += count;
424 if (total_count < count) { /* overflow */
430 if ((times & 127) == 0) {
431 CDEBUG(D_INFO, "total: %lu, count: %lu, avg: %lu\n",
432 total_count, times, total_count / times);
435 LASSERT(bio != NULL);
436 LASSERT(count <= atomic_read(&lo->lo_pending));
437 loop_handle_bio(lo, bio);
438 atomic_sub(count, &lo->lo_pending);
445 static int loop_set_fd(struct lloop_device *lo, struct file *unused,
446 struct block_device *bdev, struct file *file)
449 struct address_space *mapping;
454 if (!try_module_get(THIS_MODULE))
458 if (lo->lo_state != LLOOP_UNBOUND)
461 mapping = file->f_mapping;
462 inode = mapping->host;
465 if (!S_ISREG(inode->i_mode) || inode->i_sb->s_magic != LL_SUPER_MAGIC)
468 if (!(file->f_mode & FMODE_WRITE))
469 lo_flags |= LO_FLAGS_READ_ONLY;
471 size = get_loop_size(lo, file);
473 if ((loff_t)(sector_t)size != size) {
478 /* remove all pages in cache so as dirty pages not to be existent. */
479 truncate_inode_pages(mapping, 0);
481 set_device_ro(bdev, (lo_flags & LO_FLAGS_READ_ONLY) != 0);
483 lo->lo_blocksize = CFS_PAGE_SIZE;
484 lo->lo_device = bdev;
485 lo->lo_flags = lo_flags;
486 lo->lo_backing_file = file;
488 lo->lo_sizelimit = 0;
489 lo->old_gfp_mask = mapping_gfp_mask(mapping);
490 mapping_set_gfp_mask(mapping, lo->old_gfp_mask & ~(__GFP_IO|__GFP_FS));
492 lo->lo_bio = lo->lo_biotail = NULL;
495 * set queue make_request_fn, and add limits based on lower level
498 blk_queue_make_request(lo->lo_queue, loop_make_request);
499 lo->lo_queue->queuedata = lo;
500 lo->lo_queue->unplug_fn = loop_unplug;
502 /* queue parameters */
503 blk_queue_hardsect_size(lo->lo_queue, CFS_PAGE_SIZE);
504 blk_queue_max_sectors(lo->lo_queue,
505 LLOOP_MAX_SEGMENTS << (CFS_PAGE_SHIFT - 9));
506 blk_queue_max_phys_segments(lo->lo_queue, LLOOP_MAX_SEGMENTS);
507 blk_queue_max_hw_segments(lo->lo_queue, LLOOP_MAX_SEGMENTS);
509 set_capacity(disks[lo->lo_number], size);
510 bd_set_size(bdev, size << 9);
512 set_blocksize(bdev, lo->lo_blocksize);
514 kernel_thread(loop_thread, lo, CLONE_KERNEL);
519 /* This is safe: open() is still holding a reference. */
520 module_put(THIS_MODULE);
524 static int loop_clr_fd(struct lloop_device *lo, struct block_device *bdev,
527 struct file *filp = lo->lo_backing_file;
528 int gfp = lo->old_gfp_mask;
530 if (lo->lo_state != LLOOP_BOUND)
533 if (lo->lo_refcnt > count) /* we needed one fd for the ioctl */
539 spin_lock_irq(&lo->lo_lock);
540 lo->lo_state = LLOOP_RUNDOWN;
541 spin_unlock_irq(&lo->lo_lock);
542 wake_up(&lo->lo_bh_wait);
545 lo->lo_backing_file = NULL;
547 lo->lo_device = NULL;
549 lo->lo_sizelimit = 0;
551 ll_invalidate_bdev(bdev, 0);
552 set_capacity(disks[lo->lo_number], 0);
553 bd_set_size(bdev, 0);
554 mapping_set_gfp_mask(filp->f_mapping, gfp);
555 lo->lo_state = LLOOP_UNBOUND;
557 /* This is safe: open() is still holding a reference. */
558 module_put(THIS_MODULE);
562 static int lo_open(struct inode *inode, struct file *file)
564 struct lloop_device *lo = inode->i_bdev->bd_disk->private_data;
566 down(&lo->lo_ctl_mutex);
568 up(&lo->lo_ctl_mutex);
573 static int lo_release(struct inode *inode, struct file *file)
575 struct lloop_device *lo = inode->i_bdev->bd_disk->private_data;
577 down(&lo->lo_ctl_mutex);
579 up(&lo->lo_ctl_mutex);
584 /* lloop device node's ioctl function. */
585 static int lo_ioctl(struct inode *inode, struct file *unused,
586 unsigned int cmd, unsigned long arg)
588 struct lloop_device *lo = inode->i_bdev->bd_disk->private_data;
589 struct block_device *bdev = inode->i_bdev;
594 case LL_IOC_LLOOP_DETACH: {
595 err = loop_clr_fd(lo, bdev, 2);
597 blkdev_put(bdev); /* grabbed in LLOOP_ATTACH */
601 case LL_IOC_LLOOP_INFO: {
604 if (lo->lo_state == LLOOP_BOUND)
605 ino = lo->lo_backing_file->f_dentry->d_inode->i_ino;
607 if (put_user(ino, (__u64 *)arg))
621 static struct block_device_operations lo_fops = {
622 .owner = THIS_MODULE,
624 .release = lo_release,
628 /* dynamic iocontrol callback.
629 * This callback is registered in lloop_init and will be called by
632 * This is a llite regular file ioctl function. It takes the responsibility
633 * of attaching a file, and detaching a file by a lloop's device numner.
635 static enum llioc_iter lloop_ioctl(struct inode *unused, struct file *file,
636 unsigned int cmd, unsigned long arg,
637 void *magic, int *rcp)
639 struct lloop_device *lo = NULL;
640 struct block_device *bdev = NULL;
644 if (magic != ll_iocontrol_magic)
648 GOTO(out1, err = -ENODEV);
652 case LL_IOC_LLOOP_ATTACH: {
653 struct lloop_device *lo_free = NULL;
656 for (i = 0; i < max_loop; i++, lo = NULL) {
658 if (lo->lo_state == LLOOP_UNBOUND) {
663 if (lo->lo_backing_file->f_dentry->d_inode ==
664 file->f_dentry->d_inode)
668 GOTO(out, err = -EBUSY);
671 dev = MKDEV(lloop_major, lo->lo_number);
673 /* quit if the used pointer is writable */
674 if (put_user((long)old_encode_dev(dev), (long*)arg))
675 GOTO(out, err = -EFAULT);
677 bdev = open_by_devnum(dev, file->f_mode);
679 GOTO(out, err = PTR_ERR(bdev));
682 err = loop_set_fd(lo, NULL, bdev, file);
691 case LL_IOC_LLOOP_DETACH_BYDEV: {
694 dev = old_decode_dev(arg);
695 if (MAJOR(dev) != lloop_major)
696 GOTO(out, err = -EINVAL);
699 if (minor > max_loop - 1)
700 GOTO(out, err = -EINVAL);
702 lo = &loop_dev[minor];
703 if (lo->lo_state != LLOOP_BOUND)
704 GOTO(out, err = -EINVAL);
706 bdev = lo->lo_device;
707 err = loop_clr_fd(lo, bdev, 1);
709 blkdev_put(bdev); /* grabbed in LLOOP_ATTACH */
727 static int __init lloop_init(void)
730 unsigned int cmdlist[] = {
732 LL_IOC_LLOOP_DETACH_BYDEV,
735 if (max_loop < 1 || max_loop > 256) {
736 max_loop = MAX_LOOP_DEFAULT;
737 CWARN("lloop: invalid max_loop (must be between"
738 " 1 and 256), using default (%u)\n", max_loop);
741 lloop_major = register_blkdev(0, "lloop");
745 CDEBUG(D_CONFIG, "registered lloop major %d with %u minors\n",
746 lloop_major, max_loop);
748 ll_iocontrol_magic = ll_iocontrol_register(lloop_ioctl, 2, cmdlist);
749 if (ll_iocontrol_magic == NULL)
752 OBD_ALLOC_WAIT(loop_dev, max_loop * sizeof(*loop_dev));
756 OBD_ALLOC_WAIT(disks, max_loop * sizeof(*disks));
760 for (i = 0; i < max_loop; i++) {
761 disks[i] = alloc_disk(1);
766 init_MUTEX(&lloop_mutex);
768 for (i = 0; i < max_loop; i++) {
769 struct lloop_device *lo = &loop_dev[i];
770 struct gendisk *disk = disks[i];
772 lo->lo_queue = blk_alloc_queue(GFP_KERNEL);
776 init_MUTEX(&lo->lo_ctl_mutex);
777 init_MUTEX_LOCKED(&lo->lo_sem);
778 init_waitqueue_head(&lo->lo_bh_wait);
780 spin_lock_init(&lo->lo_lock);
781 disk->major = lloop_major;
782 disk->first_minor = i;
783 disk->fops = &lo_fops;
784 sprintf(disk->disk_name, "lloop%d", i);
785 disk->private_data = lo;
786 disk->queue = lo->lo_queue;
789 /* We cannot fail after we call this, so another loop!*/
790 for (i = 0; i < max_loop; i++)
796 blk_cleanup_queue(loop_dev[i].lo_queue);
801 OBD_FREE(disks, max_loop * sizeof(*disks));
803 OBD_FREE(loop_dev, max_loop * sizeof(*loop_dev));
805 unregister_blkdev(lloop_major, "lloop");
806 ll_iocontrol_unregister(ll_iocontrol_magic);
807 CERROR("lloop: ran out of memory\n");
811 static void lloop_exit(void)
815 ll_iocontrol_unregister(ll_iocontrol_magic);
816 for (i = 0; i < max_loop; i++) {
817 del_gendisk(disks[i]);
818 blk_cleanup_queue(loop_dev[i].lo_queue);
821 if (ll_unregister_blkdev(lloop_major, "lloop"))
822 CWARN("lloop: cannot unregister blkdev\n");
824 CDEBUG(D_CONFIG, "unregistered lloop major %d\n", lloop_major);
826 OBD_FREE(disks, max_loop * sizeof(*disks));
827 OBD_FREE(loop_dev, max_loop * sizeof(*loop_dev));
830 module_init(lloop_init);
831 module_exit(lloop_exit);
833 CFS_MODULE_PARM(max_loop, "i", int, 0444, "maximum of lloop_device");
834 MODULE_AUTHOR("Sun Microsystems, Inc. <http://www.lustre.org/>");
835 MODULE_DESCRIPTION("Lustre virtual block device");
836 MODULE_LICENSE("GPL");