11da177e4SLinus Torvalds /* 21da177e4SLinus Torvalds * linux/drivers/block/loop.c 31da177e4SLinus Torvalds * 41da177e4SLinus Torvalds * Written by Theodore Ts'o, 3/29/93 51da177e4SLinus Torvalds * 61da177e4SLinus Torvalds * Copyright 1993 by Theodore Ts'o. Redistribution of this file is 71da177e4SLinus Torvalds * permitted under the GNU General Public License. 81da177e4SLinus Torvalds * 91da177e4SLinus Torvalds * DES encryption plus some minor changes by Werner Almesberger, 30-MAY-1993 101da177e4SLinus Torvalds * more DES encryption plus IDEA encryption by Nicholas J. Leon, June 20, 1996 111da177e4SLinus Torvalds * 121da177e4SLinus Torvalds * Modularized and updated for 1.1.16 kernel - Mitch Dsouza 28th May 1994 131da177e4SLinus Torvalds * Adapted for 1.3.59 kernel - Andries Brouwer, 1 Feb 1996 141da177e4SLinus Torvalds * 151da177e4SLinus Torvalds * Fixed do_loop_request() re-entrancy - Vincent.Renardias@waw.com Mar 20, 1997 161da177e4SLinus Torvalds * 171da177e4SLinus Torvalds * Added devfs support - Richard Gooch <rgooch@atnf.csiro.au> 16-Jan-1998 181da177e4SLinus Torvalds * 191da177e4SLinus Torvalds * Handle sparse backing files correctly - Kenn Humborg, Jun 28, 1998 201da177e4SLinus Torvalds * 211da177e4SLinus Torvalds * Loadable modules and other fixes by AK, 1998 221da177e4SLinus Torvalds * 231da177e4SLinus Torvalds * Make real block number available to downstream transfer functions, enables 241da177e4SLinus Torvalds * CBC (and relatives) mode encryption requiring unique IVs per data block. 251da177e4SLinus Torvalds * Reed H. Petty, rhp@draper.net 261da177e4SLinus Torvalds * 271da177e4SLinus Torvalds * Maximum number of loop devices now dynamic via max_loop module parameter. 281da177e4SLinus Torvalds * Russell Kroll <rkroll@exploits.org> 19990701 291da177e4SLinus Torvalds * 301da177e4SLinus Torvalds * Maximum number of loop devices when compiled-in now selectable by passing 311da177e4SLinus Torvalds * max_loop=<1-255> to the kernel on boot. 321da177e4SLinus Torvalds * Erik I. Bols�, <eriki@himolde.no>, Oct 31, 1999 331da177e4SLinus Torvalds * 341da177e4SLinus Torvalds * Completely rewrite request handling to be make_request_fn style and 351da177e4SLinus Torvalds * non blocking, pushing work to a helper thread. Lots of fixes from 361da177e4SLinus Torvalds * Al Viro too. 371da177e4SLinus Torvalds * Jens Axboe <axboe@suse.de>, Nov 2000 381da177e4SLinus Torvalds * 391da177e4SLinus Torvalds * Support up to 256 loop devices 401da177e4SLinus Torvalds * Heinz Mauelshagen <mge@sistina.com>, Feb 2002 411da177e4SLinus Torvalds * 421da177e4SLinus Torvalds * Support for falling back on the write file operation when the address space 431da177e4SLinus Torvalds * operations prepare_write and/or commit_write are not available on the 441da177e4SLinus Torvalds * backing filesystem. 451da177e4SLinus Torvalds * Anton Altaparmakov, 16 Feb 2005 461da177e4SLinus Torvalds * 471da177e4SLinus Torvalds * Still To Fix: 481da177e4SLinus Torvalds * - Advisory locking is ignored here. 491da177e4SLinus Torvalds * - Should use an own CAP_* category instead of CAP_SYS_ADMIN 501da177e4SLinus Torvalds * 511da177e4SLinus Torvalds */ 521da177e4SLinus Torvalds 531da177e4SLinus Torvalds #include <linux/module.h> 541da177e4SLinus Torvalds #include <linux/moduleparam.h> 551da177e4SLinus Torvalds #include <linux/sched.h> 561da177e4SLinus Torvalds #include <linux/fs.h> 571da177e4SLinus Torvalds #include <linux/file.h> 581da177e4SLinus Torvalds #include <linux/stat.h> 591da177e4SLinus Torvalds #include <linux/errno.h> 601da177e4SLinus Torvalds #include <linux/major.h> 611da177e4SLinus Torvalds #include <linux/wait.h> 621da177e4SLinus Torvalds #include <linux/blkdev.h> 631da177e4SLinus Torvalds #include <linux/blkpg.h> 641da177e4SLinus Torvalds #include <linux/init.h> 651da177e4SLinus Torvalds #include <linux/smp_lock.h> 661da177e4SLinus Torvalds #include <linux/swap.h> 671da177e4SLinus Torvalds #include <linux/slab.h> 681da177e4SLinus Torvalds #include <linux/loop.h> 69863d5b82SDavid Howells #include <linux/compat.h> 701da177e4SLinus Torvalds #include <linux/suspend.h> 711da177e4SLinus Torvalds #include <linux/writeback.h> 721da177e4SLinus Torvalds #include <linux/buffer_head.h> /* for invalidate_bdev() */ 731da177e4SLinus Torvalds #include <linux/completion.h> 741da177e4SLinus Torvalds #include <linux/highmem.h> 751da177e4SLinus Torvalds #include <linux/gfp.h> 766c997918SSerge E. Hallyn #include <linux/kthread.h> 77*fd582140SJens Axboe #include <linux/pipe_fs_i.h> 781da177e4SLinus Torvalds 791da177e4SLinus Torvalds #include <asm/uaccess.h> 801da177e4SLinus Torvalds 8173285082SKen Chen static LIST_HEAD(loop_devices); 8273285082SKen Chen static DEFINE_MUTEX(loop_devices_mutex); 831da177e4SLinus Torvalds 841da177e4SLinus Torvalds /* 851da177e4SLinus Torvalds * Transfer functions 861da177e4SLinus Torvalds */ 871da177e4SLinus Torvalds static int transfer_none(struct loop_device *lo, int cmd, 881da177e4SLinus Torvalds struct page *raw_page, unsigned raw_off, 891da177e4SLinus Torvalds struct page *loop_page, unsigned loop_off, 901da177e4SLinus Torvalds int size, sector_t real_block) 911da177e4SLinus Torvalds { 921da177e4SLinus Torvalds char *raw_buf = kmap_atomic(raw_page, KM_USER0) + raw_off; 931da177e4SLinus Torvalds char *loop_buf = kmap_atomic(loop_page, KM_USER1) + loop_off; 941da177e4SLinus Torvalds 951da177e4SLinus Torvalds if (cmd == READ) 961da177e4SLinus Torvalds memcpy(loop_buf, raw_buf, size); 971da177e4SLinus Torvalds else 981da177e4SLinus Torvalds memcpy(raw_buf, loop_buf, size); 991da177e4SLinus Torvalds 1001da177e4SLinus Torvalds kunmap_atomic(raw_buf, KM_USER0); 1011da177e4SLinus Torvalds kunmap_atomic(loop_buf, KM_USER1); 1021da177e4SLinus Torvalds cond_resched(); 1031da177e4SLinus Torvalds return 0; 1041da177e4SLinus Torvalds } 1051da177e4SLinus Torvalds 1061da177e4SLinus Torvalds static int transfer_xor(struct loop_device *lo, int cmd, 1071da177e4SLinus Torvalds struct page *raw_page, unsigned raw_off, 1081da177e4SLinus Torvalds struct page *loop_page, unsigned loop_off, 1091da177e4SLinus Torvalds int size, sector_t real_block) 1101da177e4SLinus Torvalds { 1111da177e4SLinus Torvalds char *raw_buf = kmap_atomic(raw_page, KM_USER0) + raw_off; 1121da177e4SLinus Torvalds char *loop_buf = kmap_atomic(loop_page, KM_USER1) + loop_off; 1131da177e4SLinus Torvalds char *in, *out, *key; 1141da177e4SLinus Torvalds int i, keysize; 1151da177e4SLinus Torvalds 1161da177e4SLinus Torvalds if (cmd == READ) { 1171da177e4SLinus Torvalds in = raw_buf; 1181da177e4SLinus Torvalds out = loop_buf; 1191da177e4SLinus Torvalds } else { 1201da177e4SLinus Torvalds in = loop_buf; 1211da177e4SLinus Torvalds out = raw_buf; 1221da177e4SLinus Torvalds } 1231da177e4SLinus Torvalds 1241da177e4SLinus Torvalds key = lo->lo_encrypt_key; 1251da177e4SLinus Torvalds keysize = lo->lo_encrypt_key_size; 1261da177e4SLinus Torvalds for (i = 0; i < size; i++) 1271da177e4SLinus Torvalds *out++ = *in++ ^ key[(i & 511) % keysize]; 1281da177e4SLinus Torvalds 1291da177e4SLinus Torvalds kunmap_atomic(raw_buf, KM_USER0); 1301da177e4SLinus Torvalds kunmap_atomic(loop_buf, KM_USER1); 1311da177e4SLinus Torvalds cond_resched(); 1321da177e4SLinus Torvalds return 0; 1331da177e4SLinus Torvalds } 1341da177e4SLinus Torvalds 1351da177e4SLinus Torvalds static int xor_init(struct loop_device *lo, const struct loop_info64 *info) 1361da177e4SLinus Torvalds { 1371da177e4SLinus Torvalds if (unlikely(info->lo_encrypt_key_size <= 0)) 1381da177e4SLinus Torvalds return -EINVAL; 1391da177e4SLinus Torvalds return 0; 1401da177e4SLinus Torvalds } 1411da177e4SLinus Torvalds 1421da177e4SLinus Torvalds static struct loop_func_table none_funcs = { 1431da177e4SLinus Torvalds .number = LO_CRYPT_NONE, 1441da177e4SLinus Torvalds .transfer = transfer_none, 1451da177e4SLinus Torvalds }; 1461da177e4SLinus Torvalds 1471da177e4SLinus Torvalds static struct loop_func_table xor_funcs = { 1481da177e4SLinus Torvalds .number = LO_CRYPT_XOR, 1491da177e4SLinus Torvalds .transfer = transfer_xor, 1501da177e4SLinus Torvalds .init = xor_init 1511da177e4SLinus Torvalds }; 1521da177e4SLinus Torvalds 1531da177e4SLinus Torvalds /* xfer_funcs[0] is special - its release function is never called */ 1541da177e4SLinus Torvalds static struct loop_func_table *xfer_funcs[MAX_LO_CRYPT] = { 1551da177e4SLinus Torvalds &none_funcs, 1561da177e4SLinus Torvalds &xor_funcs 1571da177e4SLinus Torvalds }; 1581da177e4SLinus Torvalds 1591da177e4SLinus Torvalds static loff_t get_loop_size(struct loop_device *lo, struct file *file) 1601da177e4SLinus Torvalds { 1611da177e4SLinus Torvalds loff_t size, offset, loopsize; 1621da177e4SLinus Torvalds 1631da177e4SLinus Torvalds /* Compute loopsize in bytes */ 1641da177e4SLinus Torvalds size = i_size_read(file->f_mapping->host); 1651da177e4SLinus Torvalds offset = lo->lo_offset; 1661da177e4SLinus Torvalds loopsize = size - offset; 1671da177e4SLinus Torvalds if (lo->lo_sizelimit > 0 && lo->lo_sizelimit < loopsize) 1681da177e4SLinus Torvalds loopsize = lo->lo_sizelimit; 1691da177e4SLinus Torvalds 1701da177e4SLinus Torvalds /* 1711da177e4SLinus Torvalds * Unfortunately, if we want to do I/O on the device, 1721da177e4SLinus Torvalds * the number of 512-byte sectors has to fit into a sector_t. 1731da177e4SLinus Torvalds */ 1741da177e4SLinus Torvalds return loopsize >> 9; 1751da177e4SLinus Torvalds } 1761da177e4SLinus Torvalds 1771da177e4SLinus Torvalds static int 1781da177e4SLinus Torvalds figure_loop_size(struct loop_device *lo) 1791da177e4SLinus Torvalds { 1801da177e4SLinus Torvalds loff_t size = get_loop_size(lo, lo->lo_backing_file); 1811da177e4SLinus Torvalds sector_t x = (sector_t)size; 1821da177e4SLinus Torvalds 1831da177e4SLinus Torvalds if (unlikely((loff_t)x != size)) 1841da177e4SLinus Torvalds return -EFBIG; 1851da177e4SLinus Torvalds 18673285082SKen Chen set_capacity(lo->lo_disk, x); 1871da177e4SLinus Torvalds return 0; 1881da177e4SLinus Torvalds } 1891da177e4SLinus Torvalds 1901da177e4SLinus Torvalds static inline int 1911da177e4SLinus Torvalds lo_do_transfer(struct loop_device *lo, int cmd, 1921da177e4SLinus Torvalds struct page *rpage, unsigned roffs, 1931da177e4SLinus Torvalds struct page *lpage, unsigned loffs, 1941da177e4SLinus Torvalds int size, sector_t rblock) 1951da177e4SLinus Torvalds { 1961da177e4SLinus Torvalds if (unlikely(!lo->transfer)) 1971da177e4SLinus Torvalds return 0; 1981da177e4SLinus Torvalds 1991da177e4SLinus Torvalds return lo->transfer(lo, cmd, rpage, roffs, lpage, loffs, size, rblock); 2001da177e4SLinus Torvalds } 2011da177e4SLinus Torvalds 2021da177e4SLinus Torvalds /** 2031da177e4SLinus Torvalds * do_lo_send_aops - helper for writing data to a loop device 2041da177e4SLinus Torvalds * 2051da177e4SLinus Torvalds * This is the fast version for backing filesystems which implement the address 2061da177e4SLinus Torvalds * space operations prepare_write and commit_write. 2071da177e4SLinus Torvalds */ 2081da177e4SLinus Torvalds static int do_lo_send_aops(struct loop_device *lo, struct bio_vec *bvec, 2091da177e4SLinus Torvalds int bsize, loff_t pos, struct page *page) 2101da177e4SLinus Torvalds { 2111da177e4SLinus Torvalds struct file *file = lo->lo_backing_file; /* kudos to NFsckingS */ 2121da177e4SLinus Torvalds struct address_space *mapping = file->f_mapping; 213f5e54d6eSChristoph Hellwig const struct address_space_operations *aops = mapping->a_ops; 2141da177e4SLinus Torvalds pgoff_t index; 2151da177e4SLinus Torvalds unsigned offset, bv_offs; 216994fc28cSZach Brown int len, ret; 2171da177e4SLinus Torvalds 2181b1dcc1bSJes Sorensen mutex_lock(&mapping->host->i_mutex); 2191da177e4SLinus Torvalds index = pos >> PAGE_CACHE_SHIFT; 2201da177e4SLinus Torvalds offset = pos & ((pgoff_t)PAGE_CACHE_SIZE - 1); 2211da177e4SLinus Torvalds bv_offs = bvec->bv_offset; 2221da177e4SLinus Torvalds len = bvec->bv_len; 2231da177e4SLinus Torvalds while (len > 0) { 2241da177e4SLinus Torvalds sector_t IV; 2251da177e4SLinus Torvalds unsigned size; 2261da177e4SLinus Torvalds int transfer_result; 2271da177e4SLinus Torvalds 2281da177e4SLinus Torvalds IV = ((sector_t)index << (PAGE_CACHE_SHIFT - 9))+(offset >> 9); 2291da177e4SLinus Torvalds size = PAGE_CACHE_SIZE - offset; 2301da177e4SLinus Torvalds if (size > len) 2311da177e4SLinus Torvalds size = len; 2321da177e4SLinus Torvalds page = grab_cache_page(mapping, index); 2331da177e4SLinus Torvalds if (unlikely(!page)) 2341da177e4SLinus Torvalds goto fail; 235994fc28cSZach Brown ret = aops->prepare_write(file, page, offset, 236994fc28cSZach Brown offset + size); 237994fc28cSZach Brown if (unlikely(ret)) { 238994fc28cSZach Brown if (ret == AOP_TRUNCATED_PAGE) { 239994fc28cSZach Brown page_cache_release(page); 240994fc28cSZach Brown continue; 241994fc28cSZach Brown } 2421da177e4SLinus Torvalds goto unlock; 243994fc28cSZach Brown } 2441da177e4SLinus Torvalds transfer_result = lo_do_transfer(lo, WRITE, page, offset, 2451da177e4SLinus Torvalds bvec->bv_page, bv_offs, size, IV); 2461da177e4SLinus Torvalds if (unlikely(transfer_result)) { 2471da177e4SLinus Torvalds /* 2481da177e4SLinus Torvalds * The transfer failed, but we still write the data to 2491da177e4SLinus Torvalds * keep prepare/commit calls balanced. 2501da177e4SLinus Torvalds */ 2511da177e4SLinus Torvalds printk(KERN_ERR "loop: transfer error block %llu\n", 2521da177e4SLinus Torvalds (unsigned long long)index); 25301f2705dSNate Diller zero_user_page(page, offset, size, KM_USER0); 2541da177e4SLinus Torvalds } 2551da177e4SLinus Torvalds flush_dcache_page(page); 256994fc28cSZach Brown ret = aops->commit_write(file, page, offset, 257994fc28cSZach Brown offset + size); 258994fc28cSZach Brown if (unlikely(ret)) { 259994fc28cSZach Brown if (ret == AOP_TRUNCATED_PAGE) { 260994fc28cSZach Brown page_cache_release(page); 261994fc28cSZach Brown continue; 262994fc28cSZach Brown } 2631da177e4SLinus Torvalds goto unlock; 264994fc28cSZach Brown } 2651da177e4SLinus Torvalds if (unlikely(transfer_result)) 2661da177e4SLinus Torvalds goto unlock; 2671da177e4SLinus Torvalds bv_offs += size; 2681da177e4SLinus Torvalds len -= size; 2691da177e4SLinus Torvalds offset = 0; 2701da177e4SLinus Torvalds index++; 2711da177e4SLinus Torvalds pos += size; 2721da177e4SLinus Torvalds unlock_page(page); 2731da177e4SLinus Torvalds page_cache_release(page); 2741da177e4SLinus Torvalds } 275994fc28cSZach Brown ret = 0; 2761da177e4SLinus Torvalds out: 2771b1dcc1bSJes Sorensen mutex_unlock(&mapping->host->i_mutex); 2781da177e4SLinus Torvalds return ret; 2791da177e4SLinus Torvalds unlock: 2801da177e4SLinus Torvalds unlock_page(page); 2811da177e4SLinus Torvalds page_cache_release(page); 2821da177e4SLinus Torvalds fail: 2831da177e4SLinus Torvalds ret = -1; 2841da177e4SLinus Torvalds goto out; 2851da177e4SLinus Torvalds } 2861da177e4SLinus Torvalds 2871da177e4SLinus Torvalds /** 2881da177e4SLinus Torvalds * __do_lo_send_write - helper for writing data to a loop device 2891da177e4SLinus Torvalds * 2901da177e4SLinus Torvalds * This helper just factors out common code between do_lo_send_direct_write() 2911da177e4SLinus Torvalds * and do_lo_send_write(). 2921da177e4SLinus Torvalds */ 293858119e1SArjan van de Ven static int __do_lo_send_write(struct file *file, 29498ae6ccdSAl Viro u8 *buf, const int len, loff_t pos) 2951da177e4SLinus Torvalds { 2961da177e4SLinus Torvalds ssize_t bw; 2971da177e4SLinus Torvalds mm_segment_t old_fs = get_fs(); 2981da177e4SLinus Torvalds 2991da177e4SLinus Torvalds set_fs(get_ds()); 3001da177e4SLinus Torvalds bw = file->f_op->write(file, buf, len, &pos); 3011da177e4SLinus Torvalds set_fs(old_fs); 3021da177e4SLinus Torvalds if (likely(bw == len)) 3031da177e4SLinus Torvalds return 0; 3041da177e4SLinus Torvalds printk(KERN_ERR "loop: Write error at byte offset %llu, length %i.\n", 3051da177e4SLinus Torvalds (unsigned long long)pos, len); 3061da177e4SLinus Torvalds if (bw >= 0) 3071da177e4SLinus Torvalds bw = -EIO; 3081da177e4SLinus Torvalds return bw; 3091da177e4SLinus Torvalds } 3101da177e4SLinus Torvalds 3111da177e4SLinus Torvalds /** 3121da177e4SLinus Torvalds * do_lo_send_direct_write - helper for writing data to a loop device 3131da177e4SLinus Torvalds * 3141da177e4SLinus Torvalds * This is the fast, non-transforming version for backing filesystems which do 3151da177e4SLinus Torvalds * not implement the address space operations prepare_write and commit_write. 3161da177e4SLinus Torvalds * It uses the write file operation which should be present on all writeable 3171da177e4SLinus Torvalds * filesystems. 3181da177e4SLinus Torvalds */ 3191da177e4SLinus Torvalds static int do_lo_send_direct_write(struct loop_device *lo, 3201da177e4SLinus Torvalds struct bio_vec *bvec, int bsize, loff_t pos, struct page *page) 3211da177e4SLinus Torvalds { 3221da177e4SLinus Torvalds ssize_t bw = __do_lo_send_write(lo->lo_backing_file, 32398ae6ccdSAl Viro kmap(bvec->bv_page) + bvec->bv_offset, 3241da177e4SLinus Torvalds bvec->bv_len, pos); 3251da177e4SLinus Torvalds kunmap(bvec->bv_page); 3261da177e4SLinus Torvalds cond_resched(); 3271da177e4SLinus Torvalds return bw; 3281da177e4SLinus Torvalds } 3291da177e4SLinus Torvalds 3301da177e4SLinus Torvalds /** 3311da177e4SLinus Torvalds * do_lo_send_write - helper for writing data to a loop device 3321da177e4SLinus Torvalds * 3331da177e4SLinus Torvalds * This is the slow, transforming version for filesystems which do not 3341da177e4SLinus Torvalds * implement the address space operations prepare_write and commit_write. It 3351da177e4SLinus Torvalds * uses the write file operation which should be present on all writeable 3361da177e4SLinus Torvalds * filesystems. 3371da177e4SLinus Torvalds * 3381da177e4SLinus Torvalds * Using fops->write is slower than using aops->{prepare,commit}_write in the 3391da177e4SLinus Torvalds * transforming case because we need to double buffer the data as we cannot do 3401da177e4SLinus Torvalds * the transformations in place as we do not have direct access to the 3411da177e4SLinus Torvalds * destination pages of the backing file. 3421da177e4SLinus Torvalds */ 3431da177e4SLinus Torvalds static int do_lo_send_write(struct loop_device *lo, struct bio_vec *bvec, 3441da177e4SLinus Torvalds int bsize, loff_t pos, struct page *page) 3451da177e4SLinus Torvalds { 3461da177e4SLinus Torvalds int ret = lo_do_transfer(lo, WRITE, page, 0, bvec->bv_page, 3471da177e4SLinus Torvalds bvec->bv_offset, bvec->bv_len, pos >> 9); 3481da177e4SLinus Torvalds if (likely(!ret)) 3491da177e4SLinus Torvalds return __do_lo_send_write(lo->lo_backing_file, 35098ae6ccdSAl Viro page_address(page), bvec->bv_len, 3511da177e4SLinus Torvalds pos); 3521da177e4SLinus Torvalds printk(KERN_ERR "loop: Transfer error at byte offset %llu, " 3531da177e4SLinus Torvalds "length %i.\n", (unsigned long long)pos, bvec->bv_len); 3541da177e4SLinus Torvalds if (ret > 0) 3551da177e4SLinus Torvalds ret = -EIO; 3561da177e4SLinus Torvalds return ret; 3571da177e4SLinus Torvalds } 3581da177e4SLinus Torvalds 3591da177e4SLinus Torvalds static int lo_send(struct loop_device *lo, struct bio *bio, int bsize, 3601da177e4SLinus Torvalds loff_t pos) 3611da177e4SLinus Torvalds { 3621da177e4SLinus Torvalds int (*do_lo_send)(struct loop_device *, struct bio_vec *, int, loff_t, 3631da177e4SLinus Torvalds struct page *page); 3641da177e4SLinus Torvalds struct bio_vec *bvec; 3651da177e4SLinus Torvalds struct page *page = NULL; 3661da177e4SLinus Torvalds int i, ret = 0; 3671da177e4SLinus Torvalds 3681da177e4SLinus Torvalds do_lo_send = do_lo_send_aops; 3691da177e4SLinus Torvalds if (!(lo->lo_flags & LO_FLAGS_USE_AOPS)) { 3701da177e4SLinus Torvalds do_lo_send = do_lo_send_direct_write; 3711da177e4SLinus Torvalds if (lo->transfer != transfer_none) { 3721da177e4SLinus Torvalds page = alloc_page(GFP_NOIO | __GFP_HIGHMEM); 3731da177e4SLinus Torvalds if (unlikely(!page)) 3741da177e4SLinus Torvalds goto fail; 3751da177e4SLinus Torvalds kmap(page); 3761da177e4SLinus Torvalds do_lo_send = do_lo_send_write; 3771da177e4SLinus Torvalds } 3781da177e4SLinus Torvalds } 3791da177e4SLinus Torvalds bio_for_each_segment(bvec, bio, i) { 3801da177e4SLinus Torvalds ret = do_lo_send(lo, bvec, bsize, pos, page); 3811da177e4SLinus Torvalds if (ret < 0) 3821da177e4SLinus Torvalds break; 3831da177e4SLinus Torvalds pos += bvec->bv_len; 3841da177e4SLinus Torvalds } 3851da177e4SLinus Torvalds if (page) { 3861da177e4SLinus Torvalds kunmap(page); 3871da177e4SLinus Torvalds __free_page(page); 3881da177e4SLinus Torvalds } 3891da177e4SLinus Torvalds out: 3901da177e4SLinus Torvalds return ret; 3911da177e4SLinus Torvalds fail: 3921da177e4SLinus Torvalds printk(KERN_ERR "loop: Failed to allocate temporary page for write.\n"); 3931da177e4SLinus Torvalds ret = -ENOMEM; 3941da177e4SLinus Torvalds goto out; 3951da177e4SLinus Torvalds } 3961da177e4SLinus Torvalds 3971da177e4SLinus Torvalds struct lo_read_data { 3981da177e4SLinus Torvalds struct loop_device *lo; 3991da177e4SLinus Torvalds struct page *page; 4001da177e4SLinus Torvalds unsigned offset; 4011da177e4SLinus Torvalds int bsize; 4021da177e4SLinus Torvalds }; 4031da177e4SLinus Torvalds 4041da177e4SLinus Torvalds static int 405*fd582140SJens Axboe lo_splice_actor(struct pipe_inode_info *pipe, struct pipe_buffer *buf, 406*fd582140SJens Axboe struct splice_desc *sd) 4071da177e4SLinus Torvalds { 408*fd582140SJens Axboe struct lo_read_data *p = sd->u.data; 4091da177e4SLinus Torvalds struct loop_device *lo = p->lo; 410*fd582140SJens Axboe struct page *page = buf->page; 4111da177e4SLinus Torvalds sector_t IV; 412*fd582140SJens Axboe size_t size; 413*fd582140SJens Axboe int ret; 4141da177e4SLinus Torvalds 415*fd582140SJens Axboe ret = buf->ops->pin(pipe, buf); 416*fd582140SJens Axboe if (unlikely(ret)) 417*fd582140SJens Axboe return ret; 4181da177e4SLinus Torvalds 419*fd582140SJens Axboe IV = ((sector_t) page->index << (PAGE_CACHE_SHIFT - 9)) + 420*fd582140SJens Axboe (buf->offset >> 9); 421*fd582140SJens Axboe size = sd->len; 422*fd582140SJens Axboe if (size > p->bsize) 423*fd582140SJens Axboe size = p->bsize; 4241da177e4SLinus Torvalds 425*fd582140SJens Axboe if (lo_do_transfer(lo, READ, page, buf->offset, p->page, p->offset, size, IV)) { 4261da177e4SLinus Torvalds printk(KERN_ERR "loop: transfer error block %ld\n", 4271da177e4SLinus Torvalds page->index); 428*fd582140SJens Axboe size = -EINVAL; 4291da177e4SLinus Torvalds } 4301da177e4SLinus Torvalds 4311da177e4SLinus Torvalds flush_dcache_page(p->page); 4321da177e4SLinus Torvalds 433*fd582140SJens Axboe if (size > 0) 4341da177e4SLinus Torvalds p->offset += size; 435*fd582140SJens Axboe 4361da177e4SLinus Torvalds return size; 4371da177e4SLinus Torvalds } 4381da177e4SLinus Torvalds 4391da177e4SLinus Torvalds static int 440*fd582140SJens Axboe lo_direct_splice_actor(struct pipe_inode_info *pipe, struct splice_desc *sd) 441*fd582140SJens Axboe { 442*fd582140SJens Axboe return __splice_from_pipe(pipe, sd, lo_splice_actor); 443*fd582140SJens Axboe } 444*fd582140SJens Axboe 445*fd582140SJens Axboe static int 4461da177e4SLinus Torvalds do_lo_receive(struct loop_device *lo, 4471da177e4SLinus Torvalds struct bio_vec *bvec, int bsize, loff_t pos) 4481da177e4SLinus Torvalds { 4491da177e4SLinus Torvalds struct lo_read_data cookie; 450*fd582140SJens Axboe struct splice_desc sd; 4511da177e4SLinus Torvalds struct file *file; 452*fd582140SJens Axboe long retval; 4531da177e4SLinus Torvalds 4541da177e4SLinus Torvalds cookie.lo = lo; 4551da177e4SLinus Torvalds cookie.page = bvec->bv_page; 4561da177e4SLinus Torvalds cookie.offset = bvec->bv_offset; 4571da177e4SLinus Torvalds cookie.bsize = bsize; 458*fd582140SJens Axboe 459*fd582140SJens Axboe sd.len = 0; 460*fd582140SJens Axboe sd.total_len = bvec->bv_len; 461*fd582140SJens Axboe sd.flags = 0; 462*fd582140SJens Axboe sd.pos = pos; 463*fd582140SJens Axboe sd.u.data = &cookie; 464*fd582140SJens Axboe 4651da177e4SLinus Torvalds file = lo->lo_backing_file; 466*fd582140SJens Axboe retval = splice_direct_to_actor(file, &sd, lo_direct_splice_actor); 467*fd582140SJens Axboe 468*fd582140SJens Axboe if (retval < 0) 469*fd582140SJens Axboe return retval; 470*fd582140SJens Axboe 471*fd582140SJens Axboe return 0; 4721da177e4SLinus Torvalds } 4731da177e4SLinus Torvalds 4741da177e4SLinus Torvalds static int 4751da177e4SLinus Torvalds lo_receive(struct loop_device *lo, struct bio *bio, int bsize, loff_t pos) 4761da177e4SLinus Torvalds { 4771da177e4SLinus Torvalds struct bio_vec *bvec; 4781da177e4SLinus Torvalds int i, ret = 0; 4791da177e4SLinus Torvalds 4801da177e4SLinus Torvalds bio_for_each_segment(bvec, bio, i) { 4811da177e4SLinus Torvalds ret = do_lo_receive(lo, bvec, bsize, pos); 4821da177e4SLinus Torvalds if (ret < 0) 4831da177e4SLinus Torvalds break; 4841da177e4SLinus Torvalds pos += bvec->bv_len; 4851da177e4SLinus Torvalds } 4861da177e4SLinus Torvalds return ret; 4871da177e4SLinus Torvalds } 4881da177e4SLinus Torvalds 4891da177e4SLinus Torvalds static int do_bio_filebacked(struct loop_device *lo, struct bio *bio) 4901da177e4SLinus Torvalds { 4911da177e4SLinus Torvalds loff_t pos; 4921da177e4SLinus Torvalds int ret; 4931da177e4SLinus Torvalds 4941da177e4SLinus Torvalds pos = ((loff_t) bio->bi_sector << 9) + lo->lo_offset; 4951da177e4SLinus Torvalds if (bio_rw(bio) == WRITE) 4961da177e4SLinus Torvalds ret = lo_send(lo, bio, lo->lo_blocksize, pos); 4971da177e4SLinus Torvalds else 4981da177e4SLinus Torvalds ret = lo_receive(lo, bio, lo->lo_blocksize, pos); 4991da177e4SLinus Torvalds return ret; 5001da177e4SLinus Torvalds } 5011da177e4SLinus Torvalds 5021da177e4SLinus Torvalds /* 5031da177e4SLinus Torvalds * Add bio to back of pending list 5041da177e4SLinus Torvalds */ 5051da177e4SLinus Torvalds static void loop_add_bio(struct loop_device *lo, struct bio *bio) 5061da177e4SLinus Torvalds { 5071da177e4SLinus Torvalds if (lo->lo_biotail) { 5081da177e4SLinus Torvalds lo->lo_biotail->bi_next = bio; 5091da177e4SLinus Torvalds lo->lo_biotail = bio; 5101da177e4SLinus Torvalds } else 5111da177e4SLinus Torvalds lo->lo_bio = lo->lo_biotail = bio; 5121da177e4SLinus Torvalds } 5131da177e4SLinus Torvalds 5141da177e4SLinus Torvalds /* 5151da177e4SLinus Torvalds * Grab first pending buffer 5161da177e4SLinus Torvalds */ 5171da177e4SLinus Torvalds static struct bio *loop_get_bio(struct loop_device *lo) 5181da177e4SLinus Torvalds { 5191da177e4SLinus Torvalds struct bio *bio; 5201da177e4SLinus Torvalds 5211da177e4SLinus Torvalds if ((bio = lo->lo_bio)) { 5221da177e4SLinus Torvalds if (bio == lo->lo_biotail) 5231da177e4SLinus Torvalds lo->lo_biotail = NULL; 5241da177e4SLinus Torvalds lo->lo_bio = bio->bi_next; 5251da177e4SLinus Torvalds bio->bi_next = NULL; 5261da177e4SLinus Torvalds } 5271da177e4SLinus Torvalds 5281da177e4SLinus Torvalds return bio; 5291da177e4SLinus Torvalds } 5301da177e4SLinus Torvalds 5311da177e4SLinus Torvalds static int loop_make_request(request_queue_t *q, struct bio *old_bio) 5321da177e4SLinus Torvalds { 5331da177e4SLinus Torvalds struct loop_device *lo = q->queuedata; 5341da177e4SLinus Torvalds int rw = bio_rw(old_bio); 5351da177e4SLinus Torvalds 53635a82d1aSNick Piggin if (rw == READA) 53735a82d1aSNick Piggin rw = READ; 53835a82d1aSNick Piggin 53935a82d1aSNick Piggin BUG_ON(!lo || (rw != READ && rw != WRITE)); 5401da177e4SLinus Torvalds 5411da177e4SLinus Torvalds spin_lock_irq(&lo->lo_lock); 5421da177e4SLinus Torvalds if (lo->lo_state != Lo_bound) 54335a82d1aSNick Piggin goto out; 54435a82d1aSNick Piggin if (unlikely(rw == WRITE && (lo->lo_flags & LO_FLAGS_READ_ONLY))) 54535a82d1aSNick Piggin goto out; 5461da177e4SLinus Torvalds loop_add_bio(lo, old_bio); 5476c997918SSerge E. Hallyn wake_up(&lo->lo_event); 54835a82d1aSNick Piggin spin_unlock_irq(&lo->lo_lock); 54935a82d1aSNick Piggin return 0; 55035a82d1aSNick Piggin 5511da177e4SLinus Torvalds out: 55235a82d1aSNick Piggin spin_unlock_irq(&lo->lo_lock); 5531da177e4SLinus Torvalds bio_io_error(old_bio, old_bio->bi_size); 5541da177e4SLinus Torvalds return 0; 5551da177e4SLinus Torvalds } 5561da177e4SLinus Torvalds 5571da177e4SLinus Torvalds /* 5581da177e4SLinus Torvalds * kick off io on the underlying address space 5591da177e4SLinus Torvalds */ 5601da177e4SLinus Torvalds static void loop_unplug(request_queue_t *q) 5611da177e4SLinus Torvalds { 5621da177e4SLinus Torvalds struct loop_device *lo = q->queuedata; 5631da177e4SLinus Torvalds 5641da177e4SLinus Torvalds clear_bit(QUEUE_FLAG_PLUGGED, &q->queue_flags); 5651da177e4SLinus Torvalds blk_run_address_space(lo->lo_backing_file->f_mapping); 5661da177e4SLinus Torvalds } 5671da177e4SLinus Torvalds 5681da177e4SLinus Torvalds struct switch_request { 5691da177e4SLinus Torvalds struct file *file; 5701da177e4SLinus Torvalds struct completion wait; 5711da177e4SLinus Torvalds }; 5721da177e4SLinus Torvalds 5731da177e4SLinus Torvalds static void do_loop_switch(struct loop_device *, struct switch_request *); 5741da177e4SLinus Torvalds 5751da177e4SLinus Torvalds static inline void loop_handle_bio(struct loop_device *lo, struct bio *bio) 5761da177e4SLinus Torvalds { 5771da177e4SLinus Torvalds if (unlikely(!bio->bi_bdev)) { 5781da177e4SLinus Torvalds do_loop_switch(lo, bio->bi_private); 5791da177e4SLinus Torvalds bio_put(bio); 5801da177e4SLinus Torvalds } else { 58135a82d1aSNick Piggin int ret = do_bio_filebacked(lo, bio); 5821da177e4SLinus Torvalds bio_endio(bio, bio->bi_size, ret); 5831da177e4SLinus Torvalds } 5841da177e4SLinus Torvalds } 5851da177e4SLinus Torvalds 5861da177e4SLinus Torvalds /* 5871da177e4SLinus Torvalds * worker thread that handles reads/writes to file backed loop devices, 5881da177e4SLinus Torvalds * to avoid blocking in our make_request_fn. it also does loop decrypting 5891da177e4SLinus Torvalds * on reads for block backed loop, as that is too heavy to do from 5901da177e4SLinus Torvalds * b_end_io context where irqs may be disabled. 5916c997918SSerge E. Hallyn * 5926c997918SSerge E. Hallyn * Loop explanation: loop_clr_fd() sets lo_state to Lo_rundown before 5936c997918SSerge E. Hallyn * calling kthread_stop(). Therefore once kthread_should_stop() is 5946c997918SSerge E. Hallyn * true, make_request will not place any more requests. Therefore 5956c997918SSerge E. Hallyn * once kthread_should_stop() is true and lo_bio is NULL, we are 5966c997918SSerge E. Hallyn * done with the loop. 5971da177e4SLinus Torvalds */ 5981da177e4SLinus Torvalds static int loop_thread(void *data) 5991da177e4SLinus Torvalds { 6001da177e4SLinus Torvalds struct loop_device *lo = data; 6011da177e4SLinus Torvalds struct bio *bio; 6021da177e4SLinus Torvalds 6031da177e4SLinus Torvalds /* 6041da177e4SLinus Torvalds * loop can be used in an encrypted device, 6051da177e4SLinus Torvalds * hence, it mustn't be stopped at all 6061da177e4SLinus Torvalds * because it could be indirectly used during suspension 6071da177e4SLinus Torvalds */ 6081da177e4SLinus Torvalds current->flags |= PF_NOFREEZE; 6091da177e4SLinus Torvalds 6101da177e4SLinus Torvalds set_user_nice(current, -20); 6111da177e4SLinus Torvalds 6126c997918SSerge E. Hallyn while (!kthread_should_stop() || lo->lo_bio) { 6131da177e4SLinus Torvalds 6146c997918SSerge E. Hallyn wait_event_interruptible(lo->lo_event, 6156c997918SSerge E. Hallyn lo->lo_bio || kthread_should_stop()); 61609c0dc68SLinus Torvalds 6176c997918SSerge E. Hallyn if (!lo->lo_bio) 61835a82d1aSNick Piggin continue; 61935a82d1aSNick Piggin spin_lock_irq(&lo->lo_lock); 6201da177e4SLinus Torvalds bio = loop_get_bio(lo); 62135a82d1aSNick Piggin spin_unlock_irq(&lo->lo_lock); 62235a82d1aSNick Piggin 62335a82d1aSNick Piggin BUG_ON(!bio); 6241da177e4SLinus Torvalds loop_handle_bio(lo, bio); 6251da177e4SLinus Torvalds } 6261da177e4SLinus Torvalds 6271da177e4SLinus Torvalds return 0; 6281da177e4SLinus Torvalds } 6291da177e4SLinus Torvalds 6301da177e4SLinus Torvalds /* 6311da177e4SLinus Torvalds * loop_switch performs the hard work of switching a backing store. 6321da177e4SLinus Torvalds * First it needs to flush existing IO, it does this by sending a magic 6331da177e4SLinus Torvalds * BIO down the pipe. The completion of this BIO does the actual switch. 6341da177e4SLinus Torvalds */ 6351da177e4SLinus Torvalds static int loop_switch(struct loop_device *lo, struct file *file) 6361da177e4SLinus Torvalds { 6371da177e4SLinus Torvalds struct switch_request w; 6381da177e4SLinus Torvalds struct bio *bio = bio_alloc(GFP_KERNEL, 1); 6391da177e4SLinus Torvalds if (!bio) 6401da177e4SLinus Torvalds return -ENOMEM; 6411da177e4SLinus Torvalds init_completion(&w.wait); 6421da177e4SLinus Torvalds w.file = file; 6431da177e4SLinus Torvalds bio->bi_private = &w; 6441da177e4SLinus Torvalds bio->bi_bdev = NULL; 6451da177e4SLinus Torvalds loop_make_request(lo->lo_queue, bio); 6461da177e4SLinus Torvalds wait_for_completion(&w.wait); 6471da177e4SLinus Torvalds return 0; 6481da177e4SLinus Torvalds } 6491da177e4SLinus Torvalds 6501da177e4SLinus Torvalds /* 6511da177e4SLinus Torvalds * Do the actual switch; called from the BIO completion routine 6521da177e4SLinus Torvalds */ 6531da177e4SLinus Torvalds static void do_loop_switch(struct loop_device *lo, struct switch_request *p) 6541da177e4SLinus Torvalds { 6551da177e4SLinus Torvalds struct file *file = p->file; 6561da177e4SLinus Torvalds struct file *old_file = lo->lo_backing_file; 6571da177e4SLinus Torvalds struct address_space *mapping = file->f_mapping; 6581da177e4SLinus Torvalds 6591da177e4SLinus Torvalds mapping_set_gfp_mask(old_file->f_mapping, lo->old_gfp_mask); 6601da177e4SLinus Torvalds lo->lo_backing_file = file; 661ba52de12STheodore Ts'o lo->lo_blocksize = S_ISBLK(mapping->host->i_mode) ? 662ba52de12STheodore Ts'o mapping->host->i_bdev->bd_block_size : PAGE_SIZE; 6631da177e4SLinus Torvalds lo->old_gfp_mask = mapping_gfp_mask(mapping); 6641da177e4SLinus Torvalds mapping_set_gfp_mask(mapping, lo->old_gfp_mask & ~(__GFP_IO|__GFP_FS)); 6651da177e4SLinus Torvalds complete(&p->wait); 6661da177e4SLinus Torvalds } 6671da177e4SLinus Torvalds 6681da177e4SLinus Torvalds 6691da177e4SLinus Torvalds /* 6701da177e4SLinus Torvalds * loop_change_fd switched the backing store of a loopback device to 6711da177e4SLinus Torvalds * a new file. This is useful for operating system installers to free up 6721da177e4SLinus Torvalds * the original file and in High Availability environments to switch to 6731da177e4SLinus Torvalds * an alternative location for the content in case of server meltdown. 6741da177e4SLinus Torvalds * This can only work if the loop device is used read-only, and if the 6751da177e4SLinus Torvalds * new backing store is the same size and type as the old backing store. 6761da177e4SLinus Torvalds */ 6771da177e4SLinus Torvalds static int loop_change_fd(struct loop_device *lo, struct file *lo_file, 6781da177e4SLinus Torvalds struct block_device *bdev, unsigned int arg) 6791da177e4SLinus Torvalds { 6801da177e4SLinus Torvalds struct file *file, *old_file; 6811da177e4SLinus Torvalds struct inode *inode; 6821da177e4SLinus Torvalds int error; 6831da177e4SLinus Torvalds 6841da177e4SLinus Torvalds error = -ENXIO; 6851da177e4SLinus Torvalds if (lo->lo_state != Lo_bound) 6861da177e4SLinus Torvalds goto out; 6871da177e4SLinus Torvalds 6881da177e4SLinus Torvalds /* the loop device has to be read-only */ 6891da177e4SLinus Torvalds error = -EINVAL; 6901da177e4SLinus Torvalds if (!(lo->lo_flags & LO_FLAGS_READ_ONLY)) 6911da177e4SLinus Torvalds goto out; 6921da177e4SLinus Torvalds 6931da177e4SLinus Torvalds error = -EBADF; 6941da177e4SLinus Torvalds file = fget(arg); 6951da177e4SLinus Torvalds if (!file) 6961da177e4SLinus Torvalds goto out; 6971da177e4SLinus Torvalds 6981da177e4SLinus Torvalds inode = file->f_mapping->host; 6991da177e4SLinus Torvalds old_file = lo->lo_backing_file; 7001da177e4SLinus Torvalds 7011da177e4SLinus Torvalds error = -EINVAL; 7021da177e4SLinus Torvalds 7031da177e4SLinus Torvalds if (!S_ISREG(inode->i_mode) && !S_ISBLK(inode->i_mode)) 7041da177e4SLinus Torvalds goto out_putf; 7051da177e4SLinus Torvalds 706*fd582140SJens Axboe /* new backing store needs to support loop (eg splice_read) */ 707*fd582140SJens Axboe if (!inode->i_fop->splice_read) 7081da177e4SLinus Torvalds goto out_putf; 7091da177e4SLinus Torvalds 7101da177e4SLinus Torvalds /* size of the new backing store needs to be the same */ 7111da177e4SLinus Torvalds if (get_loop_size(lo, file) != get_loop_size(lo, old_file)) 7121da177e4SLinus Torvalds goto out_putf; 7131da177e4SLinus Torvalds 7141da177e4SLinus Torvalds /* and ... switch */ 7151da177e4SLinus Torvalds error = loop_switch(lo, file); 7161da177e4SLinus Torvalds if (error) 7171da177e4SLinus Torvalds goto out_putf; 7181da177e4SLinus Torvalds 7191da177e4SLinus Torvalds fput(old_file); 7201da177e4SLinus Torvalds return 0; 7211da177e4SLinus Torvalds 7221da177e4SLinus Torvalds out_putf: 7231da177e4SLinus Torvalds fput(file); 7241da177e4SLinus Torvalds out: 7251da177e4SLinus Torvalds return error; 7261da177e4SLinus Torvalds } 7271da177e4SLinus Torvalds 7281da177e4SLinus Torvalds static inline int is_loop_device(struct file *file) 7291da177e4SLinus Torvalds { 7301da177e4SLinus Torvalds struct inode *i = file->f_mapping->host; 7311da177e4SLinus Torvalds 7321da177e4SLinus Torvalds return i && S_ISBLK(i->i_mode) && MAJOR(i->i_rdev) == LOOP_MAJOR; 7331da177e4SLinus Torvalds } 7341da177e4SLinus Torvalds 7351da177e4SLinus Torvalds static int loop_set_fd(struct loop_device *lo, struct file *lo_file, 7361da177e4SLinus Torvalds struct block_device *bdev, unsigned int arg) 7371da177e4SLinus Torvalds { 7381da177e4SLinus Torvalds struct file *file, *f; 7391da177e4SLinus Torvalds struct inode *inode; 7401da177e4SLinus Torvalds struct address_space *mapping; 7411da177e4SLinus Torvalds unsigned lo_blocksize; 7421da177e4SLinus Torvalds int lo_flags = 0; 7431da177e4SLinus Torvalds int error; 7441da177e4SLinus Torvalds loff_t size; 7451da177e4SLinus Torvalds 7461da177e4SLinus Torvalds /* This is safe, since we have a reference from open(). */ 7471da177e4SLinus Torvalds __module_get(THIS_MODULE); 7481da177e4SLinus Torvalds 7491da177e4SLinus Torvalds error = -EBADF; 7501da177e4SLinus Torvalds file = fget(arg); 7511da177e4SLinus Torvalds if (!file) 7521da177e4SLinus Torvalds goto out; 7531da177e4SLinus Torvalds 7541da177e4SLinus Torvalds error = -EBUSY; 7551da177e4SLinus Torvalds if (lo->lo_state != Lo_unbound) 7561da177e4SLinus Torvalds goto out_putf; 7571da177e4SLinus Torvalds 7581da177e4SLinus Torvalds /* Avoid recursion */ 7591da177e4SLinus Torvalds f = file; 7601da177e4SLinus Torvalds while (is_loop_device(f)) { 7611da177e4SLinus Torvalds struct loop_device *l; 7621da177e4SLinus Torvalds 7631da177e4SLinus Torvalds if (f->f_mapping->host->i_rdev == lo_file->f_mapping->host->i_rdev) 7641da177e4SLinus Torvalds goto out_putf; 7651da177e4SLinus Torvalds 7661da177e4SLinus Torvalds l = f->f_mapping->host->i_bdev->bd_disk->private_data; 7671da177e4SLinus Torvalds if (l->lo_state == Lo_unbound) { 7681da177e4SLinus Torvalds error = -EINVAL; 7691da177e4SLinus Torvalds goto out_putf; 7701da177e4SLinus Torvalds } 7711da177e4SLinus Torvalds f = l->lo_backing_file; 7721da177e4SLinus Torvalds } 7731da177e4SLinus Torvalds 7741da177e4SLinus Torvalds mapping = file->f_mapping; 7751da177e4SLinus Torvalds inode = mapping->host; 7761da177e4SLinus Torvalds 7771da177e4SLinus Torvalds if (!(file->f_mode & FMODE_WRITE)) 7781da177e4SLinus Torvalds lo_flags |= LO_FLAGS_READ_ONLY; 7791da177e4SLinus Torvalds 7801da177e4SLinus Torvalds error = -EINVAL; 7811da177e4SLinus Torvalds if (S_ISREG(inode->i_mode) || S_ISBLK(inode->i_mode)) { 782f5e54d6eSChristoph Hellwig const struct address_space_operations *aops = mapping->a_ops; 7831da177e4SLinus Torvalds /* 7841da177e4SLinus Torvalds * If we can't read - sorry. If we only can't write - well, 7851da177e4SLinus Torvalds * it's going to be read-only. 7861da177e4SLinus Torvalds */ 787*fd582140SJens Axboe if (!file->f_op->splice_read) 7881da177e4SLinus Torvalds goto out_putf; 7891da177e4SLinus Torvalds if (aops->prepare_write && aops->commit_write) 7901da177e4SLinus Torvalds lo_flags |= LO_FLAGS_USE_AOPS; 7911da177e4SLinus Torvalds if (!(lo_flags & LO_FLAGS_USE_AOPS) && !file->f_op->write) 7921da177e4SLinus Torvalds lo_flags |= LO_FLAGS_READ_ONLY; 7931da177e4SLinus Torvalds 794ba52de12STheodore Ts'o lo_blocksize = S_ISBLK(inode->i_mode) ? 795ba52de12STheodore Ts'o inode->i_bdev->bd_block_size : PAGE_SIZE; 796ba52de12STheodore Ts'o 7971da177e4SLinus Torvalds error = 0; 7981da177e4SLinus Torvalds } else { 7991da177e4SLinus Torvalds goto out_putf; 8001da177e4SLinus Torvalds } 8011da177e4SLinus Torvalds 8021da177e4SLinus Torvalds size = get_loop_size(lo, file); 8031da177e4SLinus Torvalds 8041da177e4SLinus Torvalds if ((loff_t)(sector_t)size != size) { 8051da177e4SLinus Torvalds error = -EFBIG; 8061da177e4SLinus Torvalds goto out_putf; 8071da177e4SLinus Torvalds } 8081da177e4SLinus Torvalds 8091da177e4SLinus Torvalds if (!(lo_file->f_mode & FMODE_WRITE)) 8101da177e4SLinus Torvalds lo_flags |= LO_FLAGS_READ_ONLY; 8111da177e4SLinus Torvalds 8121da177e4SLinus Torvalds set_device_ro(bdev, (lo_flags & LO_FLAGS_READ_ONLY) != 0); 8131da177e4SLinus Torvalds 8141da177e4SLinus Torvalds lo->lo_blocksize = lo_blocksize; 8151da177e4SLinus Torvalds lo->lo_device = bdev; 8161da177e4SLinus Torvalds lo->lo_flags = lo_flags; 8171da177e4SLinus Torvalds lo->lo_backing_file = file; 818eefe85eeSConstantine Sapuntzakis lo->transfer = transfer_none; 8191da177e4SLinus Torvalds lo->ioctl = NULL; 8201da177e4SLinus Torvalds lo->lo_sizelimit = 0; 8211da177e4SLinus Torvalds lo->old_gfp_mask = mapping_gfp_mask(mapping); 8221da177e4SLinus Torvalds mapping_set_gfp_mask(mapping, lo->old_gfp_mask & ~(__GFP_IO|__GFP_FS)); 8231da177e4SLinus Torvalds 8241da177e4SLinus Torvalds lo->lo_bio = lo->lo_biotail = NULL; 8251da177e4SLinus Torvalds 8261da177e4SLinus Torvalds /* 8271da177e4SLinus Torvalds * set queue make_request_fn, and add limits based on lower level 8281da177e4SLinus Torvalds * device 8291da177e4SLinus Torvalds */ 8301da177e4SLinus Torvalds blk_queue_make_request(lo->lo_queue, loop_make_request); 8311da177e4SLinus Torvalds lo->lo_queue->queuedata = lo; 8321da177e4SLinus Torvalds lo->lo_queue->unplug_fn = loop_unplug; 8331da177e4SLinus Torvalds 83473285082SKen Chen set_capacity(lo->lo_disk, size); 8351da177e4SLinus Torvalds bd_set_size(bdev, size << 9); 8361da177e4SLinus Torvalds 8371da177e4SLinus Torvalds set_blocksize(bdev, lo_blocksize); 8381da177e4SLinus Torvalds 8396c997918SSerge E. Hallyn lo->lo_thread = kthread_create(loop_thread, lo, "loop%d", 8406c997918SSerge E. Hallyn lo->lo_number); 8416c997918SSerge E. Hallyn if (IS_ERR(lo->lo_thread)) { 8426c997918SSerge E. Hallyn error = PTR_ERR(lo->lo_thread); 843a7422bf8SSerge E. Hallyn goto out_clr; 8446c997918SSerge E. Hallyn } 8456c997918SSerge E. Hallyn lo->lo_state = Lo_bound; 8466c997918SSerge E. Hallyn wake_up_process(lo->lo_thread); 8471da177e4SLinus Torvalds return 0; 8481da177e4SLinus Torvalds 849a7422bf8SSerge E. Hallyn out_clr: 850a7422bf8SSerge E. Hallyn lo->lo_thread = NULL; 851a7422bf8SSerge E. Hallyn lo->lo_device = NULL; 852a7422bf8SSerge E. Hallyn lo->lo_backing_file = NULL; 853a7422bf8SSerge E. Hallyn lo->lo_flags = 0; 85473285082SKen Chen set_capacity(lo->lo_disk, 0); 855f98393a6SPeter Zijlstra invalidate_bdev(bdev); 856a7422bf8SSerge E. Hallyn bd_set_size(bdev, 0); 857a7422bf8SSerge E. Hallyn mapping_set_gfp_mask(mapping, lo->old_gfp_mask); 858a7422bf8SSerge E. Hallyn lo->lo_state = Lo_unbound; 8591da177e4SLinus Torvalds out_putf: 8601da177e4SLinus Torvalds fput(file); 8611da177e4SLinus Torvalds out: 8621da177e4SLinus Torvalds /* This is safe: open() is still holding a reference. */ 8631da177e4SLinus Torvalds module_put(THIS_MODULE); 8641da177e4SLinus Torvalds return error; 8651da177e4SLinus Torvalds } 8661da177e4SLinus Torvalds 8671da177e4SLinus Torvalds static int 8681da177e4SLinus Torvalds loop_release_xfer(struct loop_device *lo) 8691da177e4SLinus Torvalds { 8701da177e4SLinus Torvalds int err = 0; 8711da177e4SLinus Torvalds struct loop_func_table *xfer = lo->lo_encryption; 8721da177e4SLinus Torvalds 8731da177e4SLinus Torvalds if (xfer) { 8741da177e4SLinus Torvalds if (xfer->release) 8751da177e4SLinus Torvalds err = xfer->release(lo); 8761da177e4SLinus Torvalds lo->transfer = NULL; 8771da177e4SLinus Torvalds lo->lo_encryption = NULL; 8781da177e4SLinus Torvalds module_put(xfer->owner); 8791da177e4SLinus Torvalds } 8801da177e4SLinus Torvalds return err; 8811da177e4SLinus Torvalds } 8821da177e4SLinus Torvalds 8831da177e4SLinus Torvalds static int 8841da177e4SLinus Torvalds loop_init_xfer(struct loop_device *lo, struct loop_func_table *xfer, 8851da177e4SLinus Torvalds const struct loop_info64 *i) 8861da177e4SLinus Torvalds { 8871da177e4SLinus Torvalds int err = 0; 8881da177e4SLinus Torvalds 8891da177e4SLinus Torvalds if (xfer) { 8901da177e4SLinus Torvalds struct module *owner = xfer->owner; 8911da177e4SLinus Torvalds 8921da177e4SLinus Torvalds if (!try_module_get(owner)) 8931da177e4SLinus Torvalds return -EINVAL; 8941da177e4SLinus Torvalds if (xfer->init) 8951da177e4SLinus Torvalds err = xfer->init(lo, i); 8961da177e4SLinus Torvalds if (err) 8971da177e4SLinus Torvalds module_put(owner); 8981da177e4SLinus Torvalds else 8991da177e4SLinus Torvalds lo->lo_encryption = xfer; 9001da177e4SLinus Torvalds } 9011da177e4SLinus Torvalds return err; 9021da177e4SLinus Torvalds } 9031da177e4SLinus Torvalds 9041da177e4SLinus Torvalds static int loop_clr_fd(struct loop_device *lo, struct block_device *bdev) 9051da177e4SLinus Torvalds { 9061da177e4SLinus Torvalds struct file *filp = lo->lo_backing_file; 907b4e3ca1aSAl Viro gfp_t gfp = lo->old_gfp_mask; 9081da177e4SLinus Torvalds 9091da177e4SLinus Torvalds if (lo->lo_state != Lo_bound) 9101da177e4SLinus Torvalds return -ENXIO; 9111da177e4SLinus Torvalds 9121da177e4SLinus Torvalds if (lo->lo_refcnt > 1) /* we needed one fd for the ioctl */ 9131da177e4SLinus Torvalds return -EBUSY; 9141da177e4SLinus Torvalds 9151da177e4SLinus Torvalds if (filp == NULL) 9161da177e4SLinus Torvalds return -EINVAL; 9171da177e4SLinus Torvalds 9181da177e4SLinus Torvalds spin_lock_irq(&lo->lo_lock); 9191da177e4SLinus Torvalds lo->lo_state = Lo_rundown; 9201da177e4SLinus Torvalds spin_unlock_irq(&lo->lo_lock); 9211da177e4SLinus Torvalds 9226c997918SSerge E. Hallyn kthread_stop(lo->lo_thread); 9231da177e4SLinus Torvalds 9241da177e4SLinus Torvalds lo->lo_backing_file = NULL; 9251da177e4SLinus Torvalds 9261da177e4SLinus Torvalds loop_release_xfer(lo); 9271da177e4SLinus Torvalds lo->transfer = NULL; 9281da177e4SLinus Torvalds lo->ioctl = NULL; 9291da177e4SLinus Torvalds lo->lo_device = NULL; 9301da177e4SLinus Torvalds lo->lo_encryption = NULL; 9311da177e4SLinus Torvalds lo->lo_offset = 0; 9321da177e4SLinus Torvalds lo->lo_sizelimit = 0; 9331da177e4SLinus Torvalds lo->lo_encrypt_key_size = 0; 9341da177e4SLinus Torvalds lo->lo_flags = 0; 9356c997918SSerge E. Hallyn lo->lo_thread = NULL; 9361da177e4SLinus Torvalds memset(lo->lo_encrypt_key, 0, LO_KEY_SIZE); 9371da177e4SLinus Torvalds memset(lo->lo_crypt_name, 0, LO_NAME_SIZE); 9381da177e4SLinus Torvalds memset(lo->lo_file_name, 0, LO_NAME_SIZE); 939f98393a6SPeter Zijlstra invalidate_bdev(bdev); 94073285082SKen Chen set_capacity(lo->lo_disk, 0); 9411da177e4SLinus Torvalds bd_set_size(bdev, 0); 9421da177e4SLinus Torvalds mapping_set_gfp_mask(filp->f_mapping, gfp); 9431da177e4SLinus Torvalds lo->lo_state = Lo_unbound; 9441da177e4SLinus Torvalds fput(filp); 9451da177e4SLinus Torvalds /* This is safe: open() is still holding a reference. */ 9461da177e4SLinus Torvalds module_put(THIS_MODULE); 9471da177e4SLinus Torvalds return 0; 9481da177e4SLinus Torvalds } 9491da177e4SLinus Torvalds 9501da177e4SLinus Torvalds static int 9511da177e4SLinus Torvalds loop_set_status(struct loop_device *lo, const struct loop_info64 *info) 9521da177e4SLinus Torvalds { 9531da177e4SLinus Torvalds int err; 9541da177e4SLinus Torvalds struct loop_func_table *xfer; 9551da177e4SLinus Torvalds 9561da177e4SLinus Torvalds if (lo->lo_encrypt_key_size && lo->lo_key_owner != current->uid && 9571da177e4SLinus Torvalds !capable(CAP_SYS_ADMIN)) 9581da177e4SLinus Torvalds return -EPERM; 9591da177e4SLinus Torvalds if (lo->lo_state != Lo_bound) 9601da177e4SLinus Torvalds return -ENXIO; 9611da177e4SLinus Torvalds if ((unsigned int) info->lo_encrypt_key_size > LO_KEY_SIZE) 9621da177e4SLinus Torvalds return -EINVAL; 9631da177e4SLinus Torvalds 9641da177e4SLinus Torvalds err = loop_release_xfer(lo); 9651da177e4SLinus Torvalds if (err) 9661da177e4SLinus Torvalds return err; 9671da177e4SLinus Torvalds 9681da177e4SLinus Torvalds if (info->lo_encrypt_type) { 9691da177e4SLinus Torvalds unsigned int type = info->lo_encrypt_type; 9701da177e4SLinus Torvalds 9711da177e4SLinus Torvalds if (type >= MAX_LO_CRYPT) 9721da177e4SLinus Torvalds return -EINVAL; 9731da177e4SLinus Torvalds xfer = xfer_funcs[type]; 9741da177e4SLinus Torvalds if (xfer == NULL) 9751da177e4SLinus Torvalds return -EINVAL; 9761da177e4SLinus Torvalds } else 9771da177e4SLinus Torvalds xfer = NULL; 9781da177e4SLinus Torvalds 9791da177e4SLinus Torvalds err = loop_init_xfer(lo, xfer, info); 9801da177e4SLinus Torvalds if (err) 9811da177e4SLinus Torvalds return err; 9821da177e4SLinus Torvalds 9831da177e4SLinus Torvalds if (lo->lo_offset != info->lo_offset || 9841da177e4SLinus Torvalds lo->lo_sizelimit != info->lo_sizelimit) { 9851da177e4SLinus Torvalds lo->lo_offset = info->lo_offset; 9861da177e4SLinus Torvalds lo->lo_sizelimit = info->lo_sizelimit; 9871da177e4SLinus Torvalds if (figure_loop_size(lo)) 9881da177e4SLinus Torvalds return -EFBIG; 9891da177e4SLinus Torvalds } 9901da177e4SLinus Torvalds 9911da177e4SLinus Torvalds memcpy(lo->lo_file_name, info->lo_file_name, LO_NAME_SIZE); 9921da177e4SLinus Torvalds memcpy(lo->lo_crypt_name, info->lo_crypt_name, LO_NAME_SIZE); 9931da177e4SLinus Torvalds lo->lo_file_name[LO_NAME_SIZE-1] = 0; 9941da177e4SLinus Torvalds lo->lo_crypt_name[LO_NAME_SIZE-1] = 0; 9951da177e4SLinus Torvalds 9961da177e4SLinus Torvalds if (!xfer) 9971da177e4SLinus Torvalds xfer = &none_funcs; 9981da177e4SLinus Torvalds lo->transfer = xfer->transfer; 9991da177e4SLinus Torvalds lo->ioctl = xfer->ioctl; 10001da177e4SLinus Torvalds 10011da177e4SLinus Torvalds lo->lo_encrypt_key_size = info->lo_encrypt_key_size; 10021da177e4SLinus Torvalds lo->lo_init[0] = info->lo_init[0]; 10031da177e4SLinus Torvalds lo->lo_init[1] = info->lo_init[1]; 10041da177e4SLinus Torvalds if (info->lo_encrypt_key_size) { 10051da177e4SLinus Torvalds memcpy(lo->lo_encrypt_key, info->lo_encrypt_key, 10061da177e4SLinus Torvalds info->lo_encrypt_key_size); 10071da177e4SLinus Torvalds lo->lo_key_owner = current->uid; 10081da177e4SLinus Torvalds } 10091da177e4SLinus Torvalds 10101da177e4SLinus Torvalds return 0; 10111da177e4SLinus Torvalds } 10121da177e4SLinus Torvalds 10131da177e4SLinus Torvalds static int 10141da177e4SLinus Torvalds loop_get_status(struct loop_device *lo, struct loop_info64 *info) 10151da177e4SLinus Torvalds { 10161da177e4SLinus Torvalds struct file *file = lo->lo_backing_file; 10171da177e4SLinus Torvalds struct kstat stat; 10181da177e4SLinus Torvalds int error; 10191da177e4SLinus Torvalds 10201da177e4SLinus Torvalds if (lo->lo_state != Lo_bound) 10211da177e4SLinus Torvalds return -ENXIO; 10226c648be6SJosef Sipek error = vfs_getattr(file->f_path.mnt, file->f_path.dentry, &stat); 10231da177e4SLinus Torvalds if (error) 10241da177e4SLinus Torvalds return error; 10251da177e4SLinus Torvalds memset(info, 0, sizeof(*info)); 10261da177e4SLinus Torvalds info->lo_number = lo->lo_number; 10271da177e4SLinus Torvalds info->lo_device = huge_encode_dev(stat.dev); 10281da177e4SLinus Torvalds info->lo_inode = stat.ino; 10291da177e4SLinus Torvalds info->lo_rdevice = huge_encode_dev(lo->lo_device ? stat.rdev : stat.dev); 10301da177e4SLinus Torvalds info->lo_offset = lo->lo_offset; 10311da177e4SLinus Torvalds info->lo_sizelimit = lo->lo_sizelimit; 10321da177e4SLinus Torvalds info->lo_flags = lo->lo_flags; 10331da177e4SLinus Torvalds memcpy(info->lo_file_name, lo->lo_file_name, LO_NAME_SIZE); 10341da177e4SLinus Torvalds memcpy(info->lo_crypt_name, lo->lo_crypt_name, LO_NAME_SIZE); 10351da177e4SLinus Torvalds info->lo_encrypt_type = 10361da177e4SLinus Torvalds lo->lo_encryption ? lo->lo_encryption->number : 0; 10371da177e4SLinus Torvalds if (lo->lo_encrypt_key_size && capable(CAP_SYS_ADMIN)) { 10381da177e4SLinus Torvalds info->lo_encrypt_key_size = lo->lo_encrypt_key_size; 10391da177e4SLinus Torvalds memcpy(info->lo_encrypt_key, lo->lo_encrypt_key, 10401da177e4SLinus Torvalds lo->lo_encrypt_key_size); 10411da177e4SLinus Torvalds } 10421da177e4SLinus Torvalds return 0; 10431da177e4SLinus Torvalds } 10441da177e4SLinus Torvalds 10451da177e4SLinus Torvalds static void 10461da177e4SLinus Torvalds loop_info64_from_old(const struct loop_info *info, struct loop_info64 *info64) 10471da177e4SLinus Torvalds { 10481da177e4SLinus Torvalds memset(info64, 0, sizeof(*info64)); 10491da177e4SLinus Torvalds info64->lo_number = info->lo_number; 10501da177e4SLinus Torvalds info64->lo_device = info->lo_device; 10511da177e4SLinus Torvalds info64->lo_inode = info->lo_inode; 10521da177e4SLinus Torvalds info64->lo_rdevice = info->lo_rdevice; 10531da177e4SLinus Torvalds info64->lo_offset = info->lo_offset; 10541da177e4SLinus Torvalds info64->lo_sizelimit = 0; 10551da177e4SLinus Torvalds info64->lo_encrypt_type = info->lo_encrypt_type; 10561da177e4SLinus Torvalds info64->lo_encrypt_key_size = info->lo_encrypt_key_size; 10571da177e4SLinus Torvalds info64->lo_flags = info->lo_flags; 10581da177e4SLinus Torvalds info64->lo_init[0] = info->lo_init[0]; 10591da177e4SLinus Torvalds info64->lo_init[1] = info->lo_init[1]; 10601da177e4SLinus Torvalds if (info->lo_encrypt_type == LO_CRYPT_CRYPTOAPI) 10611da177e4SLinus Torvalds memcpy(info64->lo_crypt_name, info->lo_name, LO_NAME_SIZE); 10621da177e4SLinus Torvalds else 10631da177e4SLinus Torvalds memcpy(info64->lo_file_name, info->lo_name, LO_NAME_SIZE); 10641da177e4SLinus Torvalds memcpy(info64->lo_encrypt_key, info->lo_encrypt_key, LO_KEY_SIZE); 10651da177e4SLinus Torvalds } 10661da177e4SLinus Torvalds 10671da177e4SLinus Torvalds static int 10681da177e4SLinus Torvalds loop_info64_to_old(const struct loop_info64 *info64, struct loop_info *info) 10691da177e4SLinus Torvalds { 10701da177e4SLinus Torvalds memset(info, 0, sizeof(*info)); 10711da177e4SLinus Torvalds info->lo_number = info64->lo_number; 10721da177e4SLinus Torvalds info->lo_device = info64->lo_device; 10731da177e4SLinus Torvalds info->lo_inode = info64->lo_inode; 10741da177e4SLinus Torvalds info->lo_rdevice = info64->lo_rdevice; 10751da177e4SLinus Torvalds info->lo_offset = info64->lo_offset; 10761da177e4SLinus Torvalds info->lo_encrypt_type = info64->lo_encrypt_type; 10771da177e4SLinus Torvalds info->lo_encrypt_key_size = info64->lo_encrypt_key_size; 10781da177e4SLinus Torvalds info->lo_flags = info64->lo_flags; 10791da177e4SLinus Torvalds info->lo_init[0] = info64->lo_init[0]; 10801da177e4SLinus Torvalds info->lo_init[1] = info64->lo_init[1]; 10811da177e4SLinus Torvalds if (info->lo_encrypt_type == LO_CRYPT_CRYPTOAPI) 10821da177e4SLinus Torvalds memcpy(info->lo_name, info64->lo_crypt_name, LO_NAME_SIZE); 10831da177e4SLinus Torvalds else 10841da177e4SLinus Torvalds memcpy(info->lo_name, info64->lo_file_name, LO_NAME_SIZE); 10851da177e4SLinus Torvalds memcpy(info->lo_encrypt_key, info64->lo_encrypt_key, LO_KEY_SIZE); 10861da177e4SLinus Torvalds 10871da177e4SLinus Torvalds /* error in case values were truncated */ 10881da177e4SLinus Torvalds if (info->lo_device != info64->lo_device || 10891da177e4SLinus Torvalds info->lo_rdevice != info64->lo_rdevice || 10901da177e4SLinus Torvalds info->lo_inode != info64->lo_inode || 10911da177e4SLinus Torvalds info->lo_offset != info64->lo_offset) 10921da177e4SLinus Torvalds return -EOVERFLOW; 10931da177e4SLinus Torvalds 10941da177e4SLinus Torvalds return 0; 10951da177e4SLinus Torvalds } 10961da177e4SLinus Torvalds 10971da177e4SLinus Torvalds static int 10981da177e4SLinus Torvalds loop_set_status_old(struct loop_device *lo, const struct loop_info __user *arg) 10991da177e4SLinus Torvalds { 11001da177e4SLinus Torvalds struct loop_info info; 11011da177e4SLinus Torvalds struct loop_info64 info64; 11021da177e4SLinus Torvalds 11031da177e4SLinus Torvalds if (copy_from_user(&info, arg, sizeof (struct loop_info))) 11041da177e4SLinus Torvalds return -EFAULT; 11051da177e4SLinus Torvalds loop_info64_from_old(&info, &info64); 11061da177e4SLinus Torvalds return loop_set_status(lo, &info64); 11071da177e4SLinus Torvalds } 11081da177e4SLinus Torvalds 11091da177e4SLinus Torvalds static int 11101da177e4SLinus Torvalds loop_set_status64(struct loop_device *lo, const struct loop_info64 __user *arg) 11111da177e4SLinus Torvalds { 11121da177e4SLinus Torvalds struct loop_info64 info64; 11131da177e4SLinus Torvalds 11141da177e4SLinus Torvalds if (copy_from_user(&info64, arg, sizeof (struct loop_info64))) 11151da177e4SLinus Torvalds return -EFAULT; 11161da177e4SLinus Torvalds return loop_set_status(lo, &info64); 11171da177e4SLinus Torvalds } 11181da177e4SLinus Torvalds 11191da177e4SLinus Torvalds static int 11201da177e4SLinus Torvalds loop_get_status_old(struct loop_device *lo, struct loop_info __user *arg) { 11211da177e4SLinus Torvalds struct loop_info info; 11221da177e4SLinus Torvalds struct loop_info64 info64; 11231da177e4SLinus Torvalds int err = 0; 11241da177e4SLinus Torvalds 11251da177e4SLinus Torvalds if (!arg) 11261da177e4SLinus Torvalds err = -EINVAL; 11271da177e4SLinus Torvalds if (!err) 11281da177e4SLinus Torvalds err = loop_get_status(lo, &info64); 11291da177e4SLinus Torvalds if (!err) 11301da177e4SLinus Torvalds err = loop_info64_to_old(&info64, &info); 11311da177e4SLinus Torvalds if (!err && copy_to_user(arg, &info, sizeof(info))) 11321da177e4SLinus Torvalds err = -EFAULT; 11331da177e4SLinus Torvalds 11341da177e4SLinus Torvalds return err; 11351da177e4SLinus Torvalds } 11361da177e4SLinus Torvalds 11371da177e4SLinus Torvalds static int 11381da177e4SLinus Torvalds loop_get_status64(struct loop_device *lo, struct loop_info64 __user *arg) { 11391da177e4SLinus Torvalds struct loop_info64 info64; 11401da177e4SLinus Torvalds int err = 0; 11411da177e4SLinus Torvalds 11421da177e4SLinus Torvalds if (!arg) 11431da177e4SLinus Torvalds err = -EINVAL; 11441da177e4SLinus Torvalds if (!err) 11451da177e4SLinus Torvalds err = loop_get_status(lo, &info64); 11461da177e4SLinus Torvalds if (!err && copy_to_user(arg, &info64, sizeof(info64))) 11471da177e4SLinus Torvalds err = -EFAULT; 11481da177e4SLinus Torvalds 11491da177e4SLinus Torvalds return err; 11501da177e4SLinus Torvalds } 11511da177e4SLinus Torvalds 11521da177e4SLinus Torvalds static int lo_ioctl(struct inode * inode, struct file * file, 11531da177e4SLinus Torvalds unsigned int cmd, unsigned long arg) 11541da177e4SLinus Torvalds { 11551da177e4SLinus Torvalds struct loop_device *lo = inode->i_bdev->bd_disk->private_data; 11561da177e4SLinus Torvalds int err; 11571da177e4SLinus Torvalds 1158f85221ddSIngo Molnar mutex_lock(&lo->lo_ctl_mutex); 11591da177e4SLinus Torvalds switch (cmd) { 11601da177e4SLinus Torvalds case LOOP_SET_FD: 11611da177e4SLinus Torvalds err = loop_set_fd(lo, file, inode->i_bdev, arg); 11621da177e4SLinus Torvalds break; 11631da177e4SLinus Torvalds case LOOP_CHANGE_FD: 11641da177e4SLinus Torvalds err = loop_change_fd(lo, file, inode->i_bdev, arg); 11651da177e4SLinus Torvalds break; 11661da177e4SLinus Torvalds case LOOP_CLR_FD: 11671da177e4SLinus Torvalds err = loop_clr_fd(lo, inode->i_bdev); 11681da177e4SLinus Torvalds break; 11691da177e4SLinus Torvalds case LOOP_SET_STATUS: 11701da177e4SLinus Torvalds err = loop_set_status_old(lo, (struct loop_info __user *) arg); 11711da177e4SLinus Torvalds break; 11721da177e4SLinus Torvalds case LOOP_GET_STATUS: 11731da177e4SLinus Torvalds err = loop_get_status_old(lo, (struct loop_info __user *) arg); 11741da177e4SLinus Torvalds break; 11751da177e4SLinus Torvalds case LOOP_SET_STATUS64: 11761da177e4SLinus Torvalds err = loop_set_status64(lo, (struct loop_info64 __user *) arg); 11771da177e4SLinus Torvalds break; 11781da177e4SLinus Torvalds case LOOP_GET_STATUS64: 11791da177e4SLinus Torvalds err = loop_get_status64(lo, (struct loop_info64 __user *) arg); 11801da177e4SLinus Torvalds break; 11811da177e4SLinus Torvalds default: 11821da177e4SLinus Torvalds err = lo->ioctl ? lo->ioctl(lo, cmd, arg) : -EINVAL; 11831da177e4SLinus Torvalds } 1184f85221ddSIngo Molnar mutex_unlock(&lo->lo_ctl_mutex); 11851da177e4SLinus Torvalds return err; 11861da177e4SLinus Torvalds } 11871da177e4SLinus Torvalds 1188863d5b82SDavid Howells #ifdef CONFIG_COMPAT 1189863d5b82SDavid Howells struct compat_loop_info { 1190863d5b82SDavid Howells compat_int_t lo_number; /* ioctl r/o */ 1191863d5b82SDavid Howells compat_dev_t lo_device; /* ioctl r/o */ 1192863d5b82SDavid Howells compat_ulong_t lo_inode; /* ioctl r/o */ 1193863d5b82SDavid Howells compat_dev_t lo_rdevice; /* ioctl r/o */ 1194863d5b82SDavid Howells compat_int_t lo_offset; 1195863d5b82SDavid Howells compat_int_t lo_encrypt_type; 1196863d5b82SDavid Howells compat_int_t lo_encrypt_key_size; /* ioctl w/o */ 1197863d5b82SDavid Howells compat_int_t lo_flags; /* ioctl r/o */ 1198863d5b82SDavid Howells char lo_name[LO_NAME_SIZE]; 1199863d5b82SDavid Howells unsigned char lo_encrypt_key[LO_KEY_SIZE]; /* ioctl w/o */ 1200863d5b82SDavid Howells compat_ulong_t lo_init[2]; 1201863d5b82SDavid Howells char reserved[4]; 1202863d5b82SDavid Howells }; 1203863d5b82SDavid Howells 1204863d5b82SDavid Howells /* 1205863d5b82SDavid Howells * Transfer 32-bit compatibility structure in userspace to 64-bit loop info 1206863d5b82SDavid Howells * - noinlined to reduce stack space usage in main part of driver 1207863d5b82SDavid Howells */ 1208863d5b82SDavid Howells static noinline int 1209ba674cfcSAl Viro loop_info64_from_compat(const struct compat_loop_info __user *arg, 1210863d5b82SDavid Howells struct loop_info64 *info64) 1211863d5b82SDavid Howells { 1212863d5b82SDavid Howells struct compat_loop_info info; 1213863d5b82SDavid Howells 1214863d5b82SDavid Howells if (copy_from_user(&info, arg, sizeof(info))) 1215863d5b82SDavid Howells return -EFAULT; 1216863d5b82SDavid Howells 1217863d5b82SDavid Howells memset(info64, 0, sizeof(*info64)); 1218863d5b82SDavid Howells info64->lo_number = info.lo_number; 1219863d5b82SDavid Howells info64->lo_device = info.lo_device; 1220863d5b82SDavid Howells info64->lo_inode = info.lo_inode; 1221863d5b82SDavid Howells info64->lo_rdevice = info.lo_rdevice; 1222863d5b82SDavid Howells info64->lo_offset = info.lo_offset; 1223863d5b82SDavid Howells info64->lo_sizelimit = 0; 1224863d5b82SDavid Howells info64->lo_encrypt_type = info.lo_encrypt_type; 1225863d5b82SDavid Howells info64->lo_encrypt_key_size = info.lo_encrypt_key_size; 1226863d5b82SDavid Howells info64->lo_flags = info.lo_flags; 1227863d5b82SDavid Howells info64->lo_init[0] = info.lo_init[0]; 1228863d5b82SDavid Howells info64->lo_init[1] = info.lo_init[1]; 1229863d5b82SDavid Howells if (info.lo_encrypt_type == LO_CRYPT_CRYPTOAPI) 1230863d5b82SDavid Howells memcpy(info64->lo_crypt_name, info.lo_name, LO_NAME_SIZE); 1231863d5b82SDavid Howells else 1232863d5b82SDavid Howells memcpy(info64->lo_file_name, info.lo_name, LO_NAME_SIZE); 1233863d5b82SDavid Howells memcpy(info64->lo_encrypt_key, info.lo_encrypt_key, LO_KEY_SIZE); 1234863d5b82SDavid Howells return 0; 1235863d5b82SDavid Howells } 1236863d5b82SDavid Howells 1237863d5b82SDavid Howells /* 1238863d5b82SDavid Howells * Transfer 64-bit loop info to 32-bit compatibility structure in userspace 1239863d5b82SDavid Howells * - noinlined to reduce stack space usage in main part of driver 1240863d5b82SDavid Howells */ 1241863d5b82SDavid Howells static noinline int 1242863d5b82SDavid Howells loop_info64_to_compat(const struct loop_info64 *info64, 1243863d5b82SDavid Howells struct compat_loop_info __user *arg) 1244863d5b82SDavid Howells { 1245863d5b82SDavid Howells struct compat_loop_info info; 1246863d5b82SDavid Howells 1247863d5b82SDavid Howells memset(&info, 0, sizeof(info)); 1248863d5b82SDavid Howells info.lo_number = info64->lo_number; 1249863d5b82SDavid Howells info.lo_device = info64->lo_device; 1250863d5b82SDavid Howells info.lo_inode = info64->lo_inode; 1251863d5b82SDavid Howells info.lo_rdevice = info64->lo_rdevice; 1252863d5b82SDavid Howells info.lo_offset = info64->lo_offset; 1253863d5b82SDavid Howells info.lo_encrypt_type = info64->lo_encrypt_type; 1254863d5b82SDavid Howells info.lo_encrypt_key_size = info64->lo_encrypt_key_size; 1255863d5b82SDavid Howells info.lo_flags = info64->lo_flags; 1256863d5b82SDavid Howells info.lo_init[0] = info64->lo_init[0]; 1257863d5b82SDavid Howells info.lo_init[1] = info64->lo_init[1]; 1258863d5b82SDavid Howells if (info.lo_encrypt_type == LO_CRYPT_CRYPTOAPI) 1259863d5b82SDavid Howells memcpy(info.lo_name, info64->lo_crypt_name, LO_NAME_SIZE); 1260863d5b82SDavid Howells else 1261863d5b82SDavid Howells memcpy(info.lo_name, info64->lo_file_name, LO_NAME_SIZE); 1262863d5b82SDavid Howells memcpy(info.lo_encrypt_key, info64->lo_encrypt_key, LO_KEY_SIZE); 1263863d5b82SDavid Howells 1264863d5b82SDavid Howells /* error in case values were truncated */ 1265863d5b82SDavid Howells if (info.lo_device != info64->lo_device || 1266863d5b82SDavid Howells info.lo_rdevice != info64->lo_rdevice || 1267863d5b82SDavid Howells info.lo_inode != info64->lo_inode || 1268863d5b82SDavid Howells info.lo_offset != info64->lo_offset || 1269863d5b82SDavid Howells info.lo_init[0] != info64->lo_init[0] || 1270863d5b82SDavid Howells info.lo_init[1] != info64->lo_init[1]) 1271863d5b82SDavid Howells return -EOVERFLOW; 1272863d5b82SDavid Howells 1273863d5b82SDavid Howells if (copy_to_user(arg, &info, sizeof(info))) 1274863d5b82SDavid Howells return -EFAULT; 1275863d5b82SDavid Howells return 0; 1276863d5b82SDavid Howells } 1277863d5b82SDavid Howells 1278863d5b82SDavid Howells static int 1279863d5b82SDavid Howells loop_set_status_compat(struct loop_device *lo, 1280863d5b82SDavid Howells const struct compat_loop_info __user *arg) 1281863d5b82SDavid Howells { 1282863d5b82SDavid Howells struct loop_info64 info64; 1283863d5b82SDavid Howells int ret; 1284863d5b82SDavid Howells 1285863d5b82SDavid Howells ret = loop_info64_from_compat(arg, &info64); 1286863d5b82SDavid Howells if (ret < 0) 1287863d5b82SDavid Howells return ret; 1288863d5b82SDavid Howells return loop_set_status(lo, &info64); 1289863d5b82SDavid Howells } 1290863d5b82SDavid Howells 1291863d5b82SDavid Howells static int 1292863d5b82SDavid Howells loop_get_status_compat(struct loop_device *lo, 1293863d5b82SDavid Howells struct compat_loop_info __user *arg) 1294863d5b82SDavid Howells { 1295863d5b82SDavid Howells struct loop_info64 info64; 1296863d5b82SDavid Howells int err = 0; 1297863d5b82SDavid Howells 1298863d5b82SDavid Howells if (!arg) 1299863d5b82SDavid Howells err = -EINVAL; 1300863d5b82SDavid Howells if (!err) 1301863d5b82SDavid Howells err = loop_get_status(lo, &info64); 1302863d5b82SDavid Howells if (!err) 1303863d5b82SDavid Howells err = loop_info64_to_compat(&info64, arg); 1304863d5b82SDavid Howells return err; 1305863d5b82SDavid Howells } 1306863d5b82SDavid Howells 1307863d5b82SDavid Howells static long lo_compat_ioctl(struct file *file, unsigned int cmd, unsigned long arg) 1308863d5b82SDavid Howells { 13096c648be6SJosef Sipek struct inode *inode = file->f_path.dentry->d_inode; 1310863d5b82SDavid Howells struct loop_device *lo = inode->i_bdev->bd_disk->private_data; 1311863d5b82SDavid Howells int err; 1312863d5b82SDavid Howells 1313863d5b82SDavid Howells lock_kernel(); 1314863d5b82SDavid Howells switch(cmd) { 1315863d5b82SDavid Howells case LOOP_SET_STATUS: 1316863d5b82SDavid Howells mutex_lock(&lo->lo_ctl_mutex); 1317863d5b82SDavid Howells err = loop_set_status_compat( 1318863d5b82SDavid Howells lo, (const struct compat_loop_info __user *) arg); 1319863d5b82SDavid Howells mutex_unlock(&lo->lo_ctl_mutex); 1320863d5b82SDavid Howells break; 1321863d5b82SDavid Howells case LOOP_GET_STATUS: 1322863d5b82SDavid Howells mutex_lock(&lo->lo_ctl_mutex); 1323863d5b82SDavid Howells err = loop_get_status_compat( 1324863d5b82SDavid Howells lo, (struct compat_loop_info __user *) arg); 1325863d5b82SDavid Howells mutex_unlock(&lo->lo_ctl_mutex); 1326863d5b82SDavid Howells break; 1327863d5b82SDavid Howells case LOOP_CLR_FD: 1328863d5b82SDavid Howells case LOOP_GET_STATUS64: 1329863d5b82SDavid Howells case LOOP_SET_STATUS64: 1330863d5b82SDavid Howells arg = (unsigned long) compat_ptr(arg); 1331863d5b82SDavid Howells case LOOP_SET_FD: 1332863d5b82SDavid Howells case LOOP_CHANGE_FD: 1333863d5b82SDavid Howells err = lo_ioctl(inode, file, cmd, arg); 1334863d5b82SDavid Howells break; 1335863d5b82SDavid Howells default: 1336863d5b82SDavid Howells err = -ENOIOCTLCMD; 1337863d5b82SDavid Howells break; 1338863d5b82SDavid Howells } 1339863d5b82SDavid Howells unlock_kernel(); 1340863d5b82SDavid Howells return err; 1341863d5b82SDavid Howells } 1342863d5b82SDavid Howells #endif 1343863d5b82SDavid Howells 13441da177e4SLinus Torvalds static int lo_open(struct inode *inode, struct file *file) 13451da177e4SLinus Torvalds { 13461da177e4SLinus Torvalds struct loop_device *lo = inode->i_bdev->bd_disk->private_data; 13471da177e4SLinus Torvalds 1348f85221ddSIngo Molnar mutex_lock(&lo->lo_ctl_mutex); 13491da177e4SLinus Torvalds lo->lo_refcnt++; 1350f85221ddSIngo Molnar mutex_unlock(&lo->lo_ctl_mutex); 13511da177e4SLinus Torvalds 13521da177e4SLinus Torvalds return 0; 13531da177e4SLinus Torvalds } 13541da177e4SLinus Torvalds 13551da177e4SLinus Torvalds static int lo_release(struct inode *inode, struct file *file) 13561da177e4SLinus Torvalds { 13571da177e4SLinus Torvalds struct loop_device *lo = inode->i_bdev->bd_disk->private_data; 13581da177e4SLinus Torvalds 1359f85221ddSIngo Molnar mutex_lock(&lo->lo_ctl_mutex); 13601da177e4SLinus Torvalds --lo->lo_refcnt; 1361f85221ddSIngo Molnar mutex_unlock(&lo->lo_ctl_mutex); 13621da177e4SLinus Torvalds 13631da177e4SLinus Torvalds return 0; 13641da177e4SLinus Torvalds } 13651da177e4SLinus Torvalds 13661da177e4SLinus Torvalds static struct block_device_operations lo_fops = { 13671da177e4SLinus Torvalds .owner = THIS_MODULE, 13681da177e4SLinus Torvalds .open = lo_open, 13691da177e4SLinus Torvalds .release = lo_release, 13701da177e4SLinus Torvalds .ioctl = lo_ioctl, 1371863d5b82SDavid Howells #ifdef CONFIG_COMPAT 1372863d5b82SDavid Howells .compat_ioctl = lo_compat_ioctl, 1373863d5b82SDavid Howells #endif 13741da177e4SLinus Torvalds }; 13751da177e4SLinus Torvalds 13761da177e4SLinus Torvalds /* 13771da177e4SLinus Torvalds * And now the modules code and kernel interface. 13781da177e4SLinus Torvalds */ 137973285082SKen Chen static int max_loop; 13801da177e4SLinus Torvalds module_param(max_loop, int, 0); 1381a47653fcSKen Chen MODULE_PARM_DESC(max_loop, "Maximum number of loop devices"); 13821da177e4SLinus Torvalds MODULE_LICENSE("GPL"); 13831da177e4SLinus Torvalds MODULE_ALIAS_BLOCKDEV_MAJOR(LOOP_MAJOR); 13841da177e4SLinus Torvalds 13851da177e4SLinus Torvalds int loop_register_transfer(struct loop_func_table *funcs) 13861da177e4SLinus Torvalds { 13871da177e4SLinus Torvalds unsigned int n = funcs->number; 13881da177e4SLinus Torvalds 13891da177e4SLinus Torvalds if (n >= MAX_LO_CRYPT || xfer_funcs[n]) 13901da177e4SLinus Torvalds return -EINVAL; 13911da177e4SLinus Torvalds xfer_funcs[n] = funcs; 13921da177e4SLinus Torvalds return 0; 13931da177e4SLinus Torvalds } 13941da177e4SLinus Torvalds 13951da177e4SLinus Torvalds int loop_unregister_transfer(int number) 13961da177e4SLinus Torvalds { 13971da177e4SLinus Torvalds unsigned int n = number; 13981da177e4SLinus Torvalds struct loop_device *lo; 13991da177e4SLinus Torvalds struct loop_func_table *xfer; 14001da177e4SLinus Torvalds 14011da177e4SLinus Torvalds if (n == 0 || n >= MAX_LO_CRYPT || (xfer = xfer_funcs[n]) == NULL) 14021da177e4SLinus Torvalds return -EINVAL; 14031da177e4SLinus Torvalds 14041da177e4SLinus Torvalds xfer_funcs[n] = NULL; 14051da177e4SLinus Torvalds 140673285082SKen Chen list_for_each_entry(lo, &loop_devices, lo_list) { 1407f85221ddSIngo Molnar mutex_lock(&lo->lo_ctl_mutex); 14081da177e4SLinus Torvalds 14091da177e4SLinus Torvalds if (lo->lo_encryption == xfer) 14101da177e4SLinus Torvalds loop_release_xfer(lo); 14111da177e4SLinus Torvalds 1412f85221ddSIngo Molnar mutex_unlock(&lo->lo_ctl_mutex); 14131da177e4SLinus Torvalds } 14141da177e4SLinus Torvalds 14151da177e4SLinus Torvalds return 0; 14161da177e4SLinus Torvalds } 14171da177e4SLinus Torvalds 14181da177e4SLinus Torvalds EXPORT_SYMBOL(loop_register_transfer); 14191da177e4SLinus Torvalds EXPORT_SYMBOL(loop_unregister_transfer); 14201da177e4SLinus Torvalds 1421a47653fcSKen Chen static struct loop_device *loop_alloc(int i) 14221da177e4SLinus Torvalds { 142373285082SKen Chen struct loop_device *lo; 142473285082SKen Chen struct gendisk *disk; 14251da177e4SLinus Torvalds 142673285082SKen Chen lo = kzalloc(sizeof(*lo), GFP_KERNEL); 142773285082SKen Chen if (!lo) 142873285082SKen Chen goto out; 14291da177e4SLinus Torvalds 14301da177e4SLinus Torvalds lo->lo_queue = blk_alloc_queue(GFP_KERNEL); 14311da177e4SLinus Torvalds if (!lo->lo_queue) 143273285082SKen Chen goto out_free_dev; 143373285082SKen Chen 143473285082SKen Chen disk = lo->lo_disk = alloc_disk(1); 143573285082SKen Chen if (!disk) 143673285082SKen Chen goto out_free_queue; 143773285082SKen Chen 1438f85221ddSIngo Molnar mutex_init(&lo->lo_ctl_mutex); 14391da177e4SLinus Torvalds lo->lo_number = i; 14406c997918SSerge E. Hallyn lo->lo_thread = NULL; 14416c997918SSerge E. Hallyn init_waitqueue_head(&lo->lo_event); 14421da177e4SLinus Torvalds spin_lock_init(&lo->lo_lock); 14431da177e4SLinus Torvalds disk->major = LOOP_MAJOR; 14441da177e4SLinus Torvalds disk->first_minor = i; 14451da177e4SLinus Torvalds disk->fops = &lo_fops; 14461da177e4SLinus Torvalds disk->private_data = lo; 14471da177e4SLinus Torvalds disk->queue = lo->lo_queue; 144873285082SKen Chen sprintf(disk->disk_name, "loop%d", i); 144973285082SKen Chen return lo; 145073285082SKen Chen 145173285082SKen Chen out_free_queue: 145273285082SKen Chen blk_cleanup_queue(lo->lo_queue); 145373285082SKen Chen out_free_dev: 145473285082SKen Chen kfree(lo); 145573285082SKen Chen out: 145607002e99SAl Viro return NULL; 14571da177e4SLinus Torvalds } 14581da177e4SLinus Torvalds 1459a47653fcSKen Chen static void loop_free(struct loop_device *lo) 146073285082SKen Chen { 146173285082SKen Chen blk_cleanup_queue(lo->lo_queue); 146273285082SKen Chen put_disk(lo->lo_disk); 146373285082SKen Chen list_del(&lo->lo_list); 146473285082SKen Chen kfree(lo); 146573285082SKen Chen } 146673285082SKen Chen 1467a47653fcSKen Chen static struct loop_device *loop_init_one(int i) 1468a47653fcSKen Chen { 1469a47653fcSKen Chen struct loop_device *lo; 1470a47653fcSKen Chen 1471a47653fcSKen Chen list_for_each_entry(lo, &loop_devices, lo_list) { 1472a47653fcSKen Chen if (lo->lo_number == i) 1473a47653fcSKen Chen return lo; 1474a47653fcSKen Chen } 1475a47653fcSKen Chen 1476a47653fcSKen Chen lo = loop_alloc(i); 1477a47653fcSKen Chen if (lo) { 1478a47653fcSKen Chen add_disk(lo->lo_disk); 1479a47653fcSKen Chen list_add_tail(&lo->lo_list, &loop_devices); 1480a47653fcSKen Chen } 1481a47653fcSKen Chen return lo; 1482a47653fcSKen Chen } 1483a47653fcSKen Chen 1484a47653fcSKen Chen static void loop_del_one(struct loop_device *lo) 1485a47653fcSKen Chen { 1486a47653fcSKen Chen del_gendisk(lo->lo_disk); 1487a47653fcSKen Chen loop_free(lo); 1488a47653fcSKen Chen } 1489a47653fcSKen Chen 149073285082SKen Chen static struct kobject *loop_probe(dev_t dev, int *part, void *data) 149173285082SKen Chen { 1492705962ccSAl Viro struct loop_device *lo; 149307002e99SAl Viro struct kobject *kobj; 149473285082SKen Chen 1495705962ccSAl Viro mutex_lock(&loop_devices_mutex); 1496705962ccSAl Viro lo = loop_init_one(dev & MINORMASK); 149707002e99SAl Viro kobj = lo ? get_disk(lo->lo_disk) : ERR_PTR(-ENOMEM); 149873285082SKen Chen mutex_unlock(&loop_devices_mutex); 149973285082SKen Chen 150073285082SKen Chen *part = 0; 150107002e99SAl Viro return kobj; 150273285082SKen Chen } 150373285082SKen Chen 150473285082SKen Chen static int __init loop_init(void) 150573285082SKen Chen { 1506a47653fcSKen Chen int i, nr; 1507a47653fcSKen Chen unsigned long range; 1508a47653fcSKen Chen struct loop_device *lo, *next; 1509a47653fcSKen Chen 1510a47653fcSKen Chen /* 1511a47653fcSKen Chen * loop module now has a feature to instantiate underlying device 1512a47653fcSKen Chen * structure on-demand, provided that there is an access dev node. 1513a47653fcSKen Chen * However, this will not work well with user space tool that doesn't 1514a47653fcSKen Chen * know about such "feature". In order to not break any existing 1515a47653fcSKen Chen * tool, we do the following: 1516a47653fcSKen Chen * 1517a47653fcSKen Chen * (1) if max_loop is specified, create that many upfront, and this 1518a47653fcSKen Chen * also becomes a hard limit. 1519a47653fcSKen Chen * (2) if max_loop is not specified, create 8 loop device on module 1520a47653fcSKen Chen * load, user can further extend loop device by create dev node 1521a47653fcSKen Chen * themselves and have kernel automatically instantiate actual 1522a47653fcSKen Chen * device on-demand. 1523a47653fcSKen Chen */ 1524a47653fcSKen Chen if (max_loop > 1UL << MINORBITS) 1525a47653fcSKen Chen return -EINVAL; 152673285082SKen Chen 152773285082SKen Chen if (max_loop) { 1528a47653fcSKen Chen nr = max_loop; 1529a47653fcSKen Chen range = max_loop; 1530a47653fcSKen Chen } else { 1531a47653fcSKen Chen nr = 8; 1532a47653fcSKen Chen range = 1UL << MINORBITS; 153373285082SKen Chen } 1534a47653fcSKen Chen 1535a47653fcSKen Chen if (register_blkdev(LOOP_MAJOR, "loop")) 1536a47653fcSKen Chen return -EIO; 1537a47653fcSKen Chen 1538a47653fcSKen Chen for (i = 0; i < nr; i++) { 1539a47653fcSKen Chen lo = loop_alloc(i); 1540a47653fcSKen Chen if (!lo) 1541a47653fcSKen Chen goto Enomem; 1542a47653fcSKen Chen list_add_tail(&lo->lo_list, &loop_devices); 1543a47653fcSKen Chen } 1544a47653fcSKen Chen 1545a47653fcSKen Chen /* point of no return */ 1546a47653fcSKen Chen 1547a47653fcSKen Chen list_for_each_entry(lo, &loop_devices, lo_list) 1548a47653fcSKen Chen add_disk(lo->lo_disk); 1549a47653fcSKen Chen 1550a47653fcSKen Chen blk_register_region(MKDEV(LOOP_MAJOR, 0), range, 1551a47653fcSKen Chen THIS_MODULE, loop_probe, NULL, NULL); 1552a47653fcSKen Chen 155373285082SKen Chen printk(KERN_INFO "loop: module loaded\n"); 15541da177e4SLinus Torvalds return 0; 1555a47653fcSKen Chen 1556a47653fcSKen Chen Enomem: 1557a47653fcSKen Chen printk(KERN_INFO "loop: out of memory\n"); 1558a47653fcSKen Chen 1559a47653fcSKen Chen list_for_each_entry_safe(lo, next, &loop_devices, lo_list) 1560a47653fcSKen Chen loop_free(lo); 1561a47653fcSKen Chen 1562a47653fcSKen Chen unregister_blkdev(LOOP_MAJOR, "loop"); 1563a47653fcSKen Chen return -ENOMEM; 15641da177e4SLinus Torvalds } 15651da177e4SLinus Torvalds 156673285082SKen Chen static void __exit loop_exit(void) 15671da177e4SLinus Torvalds { 1568a47653fcSKen Chen unsigned long range; 156973285082SKen Chen struct loop_device *lo, *next; 15701da177e4SLinus Torvalds 1571a47653fcSKen Chen range = max_loop ? max_loop : 1UL << MINORBITS; 1572a47653fcSKen Chen 157373285082SKen Chen list_for_each_entry_safe(lo, next, &loop_devices, lo_list) 157473285082SKen Chen loop_del_one(lo); 157573285082SKen Chen 1576a47653fcSKen Chen blk_unregister_region(MKDEV(LOOP_MAJOR, 0), range); 15771da177e4SLinus Torvalds if (unregister_blkdev(LOOP_MAJOR, "loop")) 15781da177e4SLinus Torvalds printk(KERN_WARNING "loop: cannot unregister blkdev\n"); 15791da177e4SLinus Torvalds } 15801da177e4SLinus Torvalds 15811da177e4SLinus Torvalds module_init(loop_init); 15821da177e4SLinus Torvalds module_exit(loop_exit); 15831da177e4SLinus Torvalds 15841da177e4SLinus Torvalds #ifndef MODULE 15851da177e4SLinus Torvalds static int __init max_loop_setup(char *str) 15861da177e4SLinus Torvalds { 15871da177e4SLinus Torvalds max_loop = simple_strtol(str, NULL, 0); 15881da177e4SLinus Torvalds return 1; 15891da177e4SLinus Torvalds } 15901da177e4SLinus Torvalds 15911da177e4SLinus Torvalds __setup("max_loop=", max_loop_setup); 15921da177e4SLinus Torvalds #endif 1593