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 const struct lu_env *lo_env;
152 struct ll_dio_pages lo_pvec;
154 /* data to handle bio for lustre. */
155 struct lo_request_data {
156 struct page *lrd_pages[LLOOP_MAX_SEGMENTS];
157 loff_t lrd_offsets[LLOOP_MAX_SEGMENTS];
165 LO_FLAGS_READ_ONLY = 1,
168 static int lloop_major;
169 #define MAX_LOOP_DEFAULT 16
170 static int max_loop = MAX_LOOP_DEFAULT;
171 static struct lloop_device *loop_dev;
172 static struct gendisk **disks;
173 static struct semaphore lloop_mutex;
174 static void *ll_iocontrol_magic = NULL;
176 static loff_t get_loop_size(struct lloop_device *lo, struct file *file)
178 loff_t size, offset, loopsize;
180 /* Compute loopsize in bytes */
181 size = i_size_read(file->f_mapping->host);
182 offset = lo->lo_offset;
183 loopsize = size - offset;
184 if (lo->lo_sizelimit > 0 && lo->lo_sizelimit < loopsize)
185 loopsize = lo->lo_sizelimit;
188 * Unfortunately, if we want to do I/O on the device,
189 * the number of 512-byte sectors has to fit into a sector_t.
191 return loopsize >> 9;
194 static int do_bio_lustrebacked(struct lloop_device *lo, struct bio *head)
196 const struct lu_env *env = lo->lo_env;
197 struct cl_io *io = &lo->lo_io;
198 struct inode *inode = lo->lo_backing_file->f_dentry->d_inode;
199 struct cl_object *obj = ll_i2info(inode)->lli_clob;
204 obd_count page_count = 0;
205 struct bio_vec *bvec;
209 struct ll_dio_pages *pvec = &lo->lo_pvec;
210 struct page **pages = pvec->ldp_pages;
211 loff_t *offsets = pvec->ldp_offsets;
213 truncate_inode_pages(inode->i_mapping, 0);
215 /* initialize the IO */
216 memset(io, 0, sizeof(*io));
218 ret = cl_io_init(env, io, CIT_MISC, obj);
220 return io->ci_result;
221 io->ci_lockreq = CILR_NEVER;
223 LASSERT(head != NULL);
225 for (bio = head; bio != NULL; bio = bio->bi_next) {
226 LASSERT(rw == bio->bi_rw);
228 offset = (pgoff_t)(bio->bi_sector << 9) + lo->lo_offset;
229 bio_for_each_segment(bvec, bio, i) {
230 BUG_ON(bvec->bv_offset != 0);
231 BUG_ON(bvec->bv_len != CFS_PAGE_SIZE);
233 pages[page_count] = bvec->bv_page;
234 offsets[page_count] = offset;
236 offset += bvec->bv_len;
238 LASSERT(page_count <= LLOOP_MAX_SEGMENTS);
241 ll_stats_ops_tally(ll_i2sbi(inode),
242 (rw == WRITE) ? LPROC_LL_BRW_WRITE : LPROC_LL_BRW_READ,
243 page_count << PAGE_CACHE_SHIFT);
245 pvec->ldp_size = page_count << PAGE_CACHE_SHIFT;
246 pvec->ldp_nr = page_count;
248 /* FIXME: in ll_direct_rw_pages, it has to allocate many cl_page{}s to
249 * write those pages into OST. Even worse case is that more pages
250 * would be asked to write out to swap space, and then finally get here
252 * Unfortunately this is NOT easy to fix.
253 * Thoughts on solution:
254 * 0. Define a reserved pool for cl_pages, which could be a list of
255 * pre-allocated cl_pages from cl_page_kmem;
256 * 1. Define a new operation in cl_object_operations{}, says clo_depth,
257 * which measures how many layers for this lustre object. Generally
258 * speaking, the depth would be 2, one for llite, and one for lovsub.
259 * However, for SNS, there will be more since we need additional page
261 * 2. Reserve the # of (page_count * depth) cl_pages from the reserved
262 * pool. Afterwards, the clio would allocate the pages from reserved
263 * pool, this guarantees we neeedn't allocate the cl_pages from
264 * generic cl_page slab cache.
265 * Of course, if there is NOT enough pages in the pool, we might
266 * be asked to write less pages once, this purely depends on
267 * implementation. Anyway, we should be careful to avoid deadlocking.
269 LOCK_INODE_MUTEX(inode);
270 bytes = ll_direct_rw_pages(env, io, rw, inode, pvec);
271 UNLOCK_INODE_MUTEX(inode);
273 return (bytes == pvec->ldp_size) ? 0 : (int)bytes;
277 * Add bio to back of pending list
279 static void loop_add_bio(struct lloop_device *lo, struct bio *bio)
283 spin_lock_irqsave(&lo->lo_lock, flags);
284 if (lo->lo_biotail) {
285 lo->lo_biotail->bi_next = bio;
286 lo->lo_biotail = bio;
288 lo->lo_bio = lo->lo_biotail = bio;
289 spin_unlock_irqrestore(&lo->lo_lock, flags);
291 atomic_inc(&lo->lo_pending);
292 if (waitqueue_active(&lo->lo_bh_wait))
293 wake_up(&lo->lo_bh_wait);
297 * Grab first pending buffer
299 static unsigned int loop_get_bio(struct lloop_device *lo, struct bio **req)
303 unsigned int count = 0;
304 unsigned int page_count = 0;
307 spin_lock_irq(&lo->lo_lock);
309 if (unlikely(first == NULL)) {
310 spin_unlock_irq(&lo->lo_lock);
314 /* TODO: need to split the bio, too bad. */
315 LASSERT(first->bi_vcnt <= LLOOP_MAX_SEGMENTS);
319 while (*bio && (*bio)->bi_rw == rw) {
320 CDEBUG(D_INFO, "bio sector %llu size %u count %u vcnt%u \n",
321 (unsigned long long)(*bio)->bi_sector, (*bio)->bi_size,
322 page_count, (*bio)->bi_vcnt);
323 if (page_count + (*bio)->bi_vcnt > LLOOP_MAX_SEGMENTS)
327 page_count += (*bio)->bi_vcnt;
329 bio = &(*bio)->bi_next;
332 /* Some of bios can't be mergable. */
336 /* Hit the end of queue */
337 lo->lo_biotail = NULL;
341 spin_unlock_irq(&lo->lo_lock);
345 static int loop_make_request(struct request_queue *q, struct bio *old_bio)
347 struct lloop_device *lo = q->queuedata;
348 int rw = bio_rw(old_bio);
354 CDEBUG(D_INFO, "submit bio sector %llu size %u\n",
355 (unsigned long long)old_bio->bi_sector, old_bio->bi_size);
357 spin_lock_irq(&lo->lo_lock);
358 inactive = (lo->lo_state != LLOOP_BOUND);
359 spin_unlock_irq(&lo->lo_lock);
364 if (lo->lo_flags & LO_FLAGS_READ_ONLY)
366 } else if (rw == READA) {
368 } else if (rw != READ) {
369 CERROR("lloop: unknown command (%x)\n", rw);
372 loop_add_bio(lo, old_bio);
375 cfs_bio_io_error(old_bio, old_bio->bi_size);
380 * kick off io on the underlying address space
382 static void loop_unplug(struct request_queue *q)
384 struct lloop_device *lo = q->queuedata;
386 clear_bit(QUEUE_FLAG_PLUGGED, &q->queue_flags);
387 blk_run_address_space(lo->lo_backing_file->f_mapping);
390 static inline void loop_handle_bio(struct lloop_device *lo, struct bio *bio)
393 ret = do_bio_lustrebacked(lo, bio);
395 struct bio *tmp = bio->bi_next;
397 cfs_bio_endio(bio, bio->bi_size, ret);
402 static inline int loop_active(struct lloop_device *lo)
404 return atomic_read(&lo->lo_pending) || (lo->lo_state == LLOOP_RUNDOWN);
408 * worker thread that handles reads/writes to file backed loop devices,
409 * to avoid blocking in our make_request_fn.
411 static int loop_thread(void *data)
413 struct lloop_device *lo = data;
416 unsigned long times = 0;
417 unsigned long total_count = 0;
423 daemonize("lloop%d", lo->lo_number);
425 set_user_nice(current, -20);
427 lo->lo_state = LLOOP_BOUND;
429 env = cl_env_get(&refcheck);
431 GOTO(out, ret = PTR_ERR(env));
434 memset(&lo->lo_pvec, 0, sizeof(lo->lo_pvec));
435 lo->lo_pvec.ldp_pages = lo->lo_requests[0].lrd_pages;
436 lo->lo_pvec.ldp_offsets = lo->lo_requests[0].lrd_offsets;
439 * up sem, we are running
444 wait_event(lo->lo_bh_wait, loop_active(lo));
445 if (!atomic_read(&lo->lo_pending)) {
447 spin_lock_irq(&lo->lo_lock);
448 exiting = (lo->lo_state == LLOOP_RUNDOWN);
449 spin_unlock_irq(&lo->lo_lock);
455 count = loop_get_bio(lo, &bio);
457 CWARN("lloop(minor: %d): missing bio\n", lo->lo_number);
461 total_count += count;
462 if (total_count < count) { /* overflow */
468 if ((times & 127) == 0) {
469 CDEBUG(D_INFO, "total: %lu, count: %lu, avg: %lu\n",
470 total_count, times, total_count / times);
473 LASSERT(bio != NULL);
474 LASSERT(count <= atomic_read(&lo->lo_pending));
475 loop_handle_bio(lo, bio);
476 atomic_sub(count, &lo->lo_pending);
478 cl_env_put(env, &refcheck);
485 static int loop_set_fd(struct lloop_device *lo, struct file *unused,
486 struct block_device *bdev, struct file *file)
489 struct address_space *mapping;
494 if (!try_module_get(THIS_MODULE))
498 if (lo->lo_state != LLOOP_UNBOUND)
501 mapping = file->f_mapping;
502 inode = mapping->host;
505 if (!S_ISREG(inode->i_mode) || inode->i_sb->s_magic != LL_SUPER_MAGIC)
508 if (!(file->f_mode & FMODE_WRITE))
509 lo_flags |= LO_FLAGS_READ_ONLY;
511 size = get_loop_size(lo, file);
513 if ((loff_t)(sector_t)size != size) {
518 /* remove all pages in cache so as dirty pages not to be existent. */
519 truncate_inode_pages(mapping, 0);
521 set_device_ro(bdev, (lo_flags & LO_FLAGS_READ_ONLY) != 0);
523 lo->lo_blocksize = CFS_PAGE_SIZE;
524 lo->lo_device = bdev;
525 lo->lo_flags = lo_flags;
526 lo->lo_backing_file = file;
528 lo->lo_sizelimit = 0;
529 lo->old_gfp_mask = mapping_gfp_mask(mapping);
530 mapping_set_gfp_mask(mapping, lo->old_gfp_mask & ~(__GFP_IO|__GFP_FS));
532 lo->lo_bio = lo->lo_biotail = NULL;
535 * set queue make_request_fn, and add limits based on lower level
538 blk_queue_make_request(lo->lo_queue, loop_make_request);
539 lo->lo_queue->queuedata = lo;
540 lo->lo_queue->unplug_fn = loop_unplug;
542 /* queue parameters */
543 blk_queue_hardsect_size(lo->lo_queue, CFS_PAGE_SIZE);
544 blk_queue_max_sectors(lo->lo_queue,
545 LLOOP_MAX_SEGMENTS << (CFS_PAGE_SHIFT - 9));
546 blk_queue_max_phys_segments(lo->lo_queue, LLOOP_MAX_SEGMENTS);
547 blk_queue_max_hw_segments(lo->lo_queue, LLOOP_MAX_SEGMENTS);
549 set_capacity(disks[lo->lo_number], size);
550 bd_set_size(bdev, size << 9);
552 set_blocksize(bdev, lo->lo_blocksize);
554 kernel_thread(loop_thread, lo, CLONE_KERNEL);
559 /* This is safe: open() is still holding a reference. */
560 module_put(THIS_MODULE);
564 static int loop_clr_fd(struct lloop_device *lo, struct block_device *bdev,
567 struct file *filp = lo->lo_backing_file;
568 int gfp = lo->old_gfp_mask;
570 if (lo->lo_state != LLOOP_BOUND)
573 if (lo->lo_refcnt > count) /* we needed one fd for the ioctl */
579 spin_lock_irq(&lo->lo_lock);
580 lo->lo_state = LLOOP_RUNDOWN;
581 spin_unlock_irq(&lo->lo_lock);
582 wake_up(&lo->lo_bh_wait);
585 lo->lo_backing_file = NULL;
587 lo->lo_device = NULL;
589 lo->lo_sizelimit = 0;
591 ll_invalidate_bdev(bdev, 0);
592 set_capacity(disks[lo->lo_number], 0);
593 bd_set_size(bdev, 0);
594 mapping_set_gfp_mask(filp->f_mapping, gfp);
595 lo->lo_state = LLOOP_UNBOUND;
597 /* This is safe: open() is still holding a reference. */
598 module_put(THIS_MODULE);
602 static int lo_open(struct inode *inode, struct file *file)
604 struct lloop_device *lo = inode->i_bdev->bd_disk->private_data;
606 down(&lo->lo_ctl_mutex);
608 up(&lo->lo_ctl_mutex);
613 static int lo_release(struct inode *inode, struct file *file)
615 struct lloop_device *lo = inode->i_bdev->bd_disk->private_data;
617 down(&lo->lo_ctl_mutex);
619 up(&lo->lo_ctl_mutex);
624 /* lloop device node's ioctl function. */
625 static int lo_ioctl(struct inode *inode, struct file *unused,
626 unsigned int cmd, unsigned long arg)
628 struct lloop_device *lo = inode->i_bdev->bd_disk->private_data;
629 struct block_device *bdev = inode->i_bdev;
634 case LL_IOC_LLOOP_DETACH: {
635 err = loop_clr_fd(lo, bdev, 2);
637 blkdev_put(bdev); /* grabbed in LLOOP_ATTACH */
641 case LL_IOC_LLOOP_INFO: {
644 if (lo->lo_state == LLOOP_BOUND)
645 ino = lo->lo_backing_file->f_dentry->d_inode->i_ino;
647 if (put_user(ino, (__u64 *)arg))
661 static struct block_device_operations lo_fops = {
662 .owner = THIS_MODULE,
664 .release = lo_release,
668 /* dynamic iocontrol callback.
669 * This callback is registered in lloop_init and will be called by
672 * This is a llite regular file ioctl function. It takes the responsibility
673 * of attaching or detaching a file by a lloop's device numner.
675 static enum llioc_iter lloop_ioctl(struct inode *unused, struct file *file,
676 unsigned int cmd, unsigned long arg,
677 void *magic, int *rcp)
679 struct lloop_device *lo = NULL;
680 struct block_device *bdev = NULL;
684 if (magic != ll_iocontrol_magic)
688 GOTO(out1, err = -ENODEV);
690 CWARN("Enter llop_ioctl\n");
694 case LL_IOC_LLOOP_ATTACH: {
695 struct lloop_device *lo_free = NULL;
698 for (i = 0; i < max_loop; i++, lo = NULL) {
700 if (lo->lo_state == LLOOP_UNBOUND) {
705 if (lo->lo_backing_file->f_dentry->d_inode ==
706 file->f_dentry->d_inode)
710 GOTO(out, err = -EBUSY);
713 dev = MKDEV(lloop_major, lo->lo_number);
715 /* quit if the used pointer is writable */
716 if (put_user((long)old_encode_dev(dev), (long*)arg))
717 GOTO(out, err = -EFAULT);
719 bdev = open_by_devnum(dev, file->f_mode);
721 GOTO(out, err = PTR_ERR(bdev));
724 err = loop_set_fd(lo, NULL, bdev, file);
733 case LL_IOC_LLOOP_DETACH_BYDEV: {
736 dev = old_decode_dev(arg);
737 if (MAJOR(dev) != lloop_major)
738 GOTO(out, err = -EINVAL);
741 if (minor > max_loop - 1)
742 GOTO(out, err = -EINVAL);
744 lo = &loop_dev[minor];
745 if (lo->lo_state != LLOOP_BOUND)
746 GOTO(out, err = -EINVAL);
748 bdev = lo->lo_device;
749 err = loop_clr_fd(lo, bdev, 1);
751 blkdev_put(bdev); /* grabbed in LLOOP_ATTACH */
769 static int __init lloop_init(void)
772 unsigned int cmdlist[] = {
774 LL_IOC_LLOOP_DETACH_BYDEV,
777 if (max_loop < 1 || max_loop > 256) {
778 max_loop = MAX_LOOP_DEFAULT;
779 CWARN("lloop: invalid max_loop (must be between"
780 " 1 and 256), using default (%u)\n", max_loop);
783 lloop_major = register_blkdev(0, "lloop");
787 CDEBUG(D_CONFIG, "registered lloop major %d with %u minors\n",
788 lloop_major, max_loop);
790 ll_iocontrol_magic = ll_iocontrol_register(lloop_ioctl, 2, cmdlist);
791 if (ll_iocontrol_magic == NULL)
794 OBD_ALLOC_WAIT(loop_dev, max_loop * sizeof(*loop_dev));
798 OBD_ALLOC_WAIT(disks, max_loop * sizeof(*disks));
802 for (i = 0; i < max_loop; i++) {
803 disks[i] = alloc_disk(1);
808 init_MUTEX(&lloop_mutex);
810 for (i = 0; i < max_loop; i++) {
811 struct lloop_device *lo = &loop_dev[i];
812 struct gendisk *disk = disks[i];
814 lo->lo_queue = blk_alloc_queue(GFP_KERNEL);
818 init_MUTEX(&lo->lo_ctl_mutex);
819 init_MUTEX_LOCKED(&lo->lo_sem);
820 init_waitqueue_head(&lo->lo_bh_wait);
822 spin_lock_init(&lo->lo_lock);
823 disk->major = lloop_major;
824 disk->first_minor = i;
825 disk->fops = &lo_fops;
826 sprintf(disk->disk_name, "lloop%d", i);
827 disk->private_data = lo;
828 disk->queue = lo->lo_queue;
831 /* We cannot fail after we call this, so another loop!*/
832 for (i = 0; i < max_loop; i++)
838 blk_cleanup_queue(loop_dev[i].lo_queue);
843 OBD_FREE(disks, max_loop * sizeof(*disks));
845 OBD_FREE(loop_dev, max_loop * sizeof(*loop_dev));
847 unregister_blkdev(lloop_major, "lloop");
848 ll_iocontrol_unregister(ll_iocontrol_magic);
849 CERROR("lloop: ran out of memory\n");
853 static void lloop_exit(void)
857 ll_iocontrol_unregister(ll_iocontrol_magic);
858 for (i = 0; i < max_loop; i++) {
859 del_gendisk(disks[i]);
860 blk_cleanup_queue(loop_dev[i].lo_queue);
863 if (ll_unregister_blkdev(lloop_major, "lloop"))
864 CWARN("lloop: cannot unregister blkdev\n");
866 CDEBUG(D_CONFIG, "unregistered lloop major %d\n", lloop_major);
868 OBD_FREE(disks, max_loop * sizeof(*disks));
869 OBD_FREE(loop_dev, max_loop * sizeof(*loop_dev));
872 module_init(lloop_init);
873 module_exit(lloop_exit);
875 CFS_MODULE_PARM(max_loop, "i", int, 0444, "maximum of lloop_device");
876 MODULE_AUTHOR("Sun Microsystems, Inc. <http://www.lustre.org/>");
877 MODULE_DESCRIPTION("Lustre virtual block device");
878 MODULE_LICENSE("GPL");