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/config.h> 541da177e4SLinus Torvalds #include <linux/module.h> 551da177e4SLinus Torvalds #include <linux/moduleparam.h> 561da177e4SLinus Torvalds #include <linux/sched.h> 571da177e4SLinus Torvalds #include <linux/fs.h> 581da177e4SLinus Torvalds #include <linux/file.h> 591da177e4SLinus Torvalds #include <linux/stat.h> 601da177e4SLinus Torvalds #include <linux/errno.h> 611da177e4SLinus Torvalds #include <linux/major.h> 621da177e4SLinus Torvalds #include <linux/wait.h> 631da177e4SLinus Torvalds #include <linux/blkdev.h> 641da177e4SLinus Torvalds #include <linux/blkpg.h> 651da177e4SLinus Torvalds #include <linux/init.h> 661da177e4SLinus Torvalds #include <linux/devfs_fs_kernel.h> 671da177e4SLinus Torvalds #include <linux/smp_lock.h> 681da177e4SLinus Torvalds #include <linux/swap.h> 691da177e4SLinus Torvalds #include <linux/slab.h> 701da177e4SLinus Torvalds #include <linux/loop.h> 711da177e4SLinus Torvalds #include <linux/suspend.h> 721da177e4SLinus Torvalds #include <linux/writeback.h> 731da177e4SLinus Torvalds #include <linux/buffer_head.h> /* for invalidate_bdev() */ 741da177e4SLinus Torvalds #include <linux/completion.h> 751da177e4SLinus Torvalds #include <linux/highmem.h> 761da177e4SLinus Torvalds #include <linux/gfp.h> 771da177e4SLinus Torvalds 781da177e4SLinus Torvalds #include <asm/uaccess.h> 791da177e4SLinus Torvalds 801da177e4SLinus Torvalds static int max_loop = 8; 811da177e4SLinus Torvalds static struct loop_device *loop_dev; 821da177e4SLinus Torvalds static struct gendisk **disks; 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 1861da177e4SLinus Torvalds set_capacity(disks[lo->lo_number], 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; 213*f5e54d6eSChristoph 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 char *kaddr; 2481da177e4SLinus Torvalds 2491da177e4SLinus Torvalds /* 2501da177e4SLinus Torvalds * The transfer failed, but we still write the data to 2511da177e4SLinus Torvalds * keep prepare/commit calls balanced. 2521da177e4SLinus Torvalds */ 2531da177e4SLinus Torvalds printk(KERN_ERR "loop: transfer error block %llu\n", 2541da177e4SLinus Torvalds (unsigned long long)index); 2551da177e4SLinus Torvalds kaddr = kmap_atomic(page, KM_USER0); 2561da177e4SLinus Torvalds memset(kaddr + offset, 0, size); 2571da177e4SLinus Torvalds kunmap_atomic(kaddr, KM_USER0); 2581da177e4SLinus Torvalds } 2591da177e4SLinus Torvalds flush_dcache_page(page); 260994fc28cSZach Brown ret = aops->commit_write(file, page, offset, 261994fc28cSZach Brown offset + size); 262994fc28cSZach Brown if (unlikely(ret)) { 263994fc28cSZach Brown if (ret == AOP_TRUNCATED_PAGE) { 264994fc28cSZach Brown page_cache_release(page); 265994fc28cSZach Brown continue; 266994fc28cSZach Brown } 2671da177e4SLinus Torvalds goto unlock; 268994fc28cSZach Brown } 2691da177e4SLinus Torvalds if (unlikely(transfer_result)) 2701da177e4SLinus Torvalds goto unlock; 2711da177e4SLinus Torvalds bv_offs += size; 2721da177e4SLinus Torvalds len -= size; 2731da177e4SLinus Torvalds offset = 0; 2741da177e4SLinus Torvalds index++; 2751da177e4SLinus Torvalds pos += size; 2761da177e4SLinus Torvalds unlock_page(page); 2771da177e4SLinus Torvalds page_cache_release(page); 2781da177e4SLinus Torvalds } 279994fc28cSZach Brown ret = 0; 2801da177e4SLinus Torvalds out: 2811b1dcc1bSJes Sorensen mutex_unlock(&mapping->host->i_mutex); 2821da177e4SLinus Torvalds return ret; 2831da177e4SLinus Torvalds unlock: 2841da177e4SLinus Torvalds unlock_page(page); 2851da177e4SLinus Torvalds page_cache_release(page); 2861da177e4SLinus Torvalds fail: 2871da177e4SLinus Torvalds ret = -1; 2881da177e4SLinus Torvalds goto out; 2891da177e4SLinus Torvalds } 2901da177e4SLinus Torvalds 2911da177e4SLinus Torvalds /** 2921da177e4SLinus Torvalds * __do_lo_send_write - helper for writing data to a loop device 2931da177e4SLinus Torvalds * 2941da177e4SLinus Torvalds * This helper just factors out common code between do_lo_send_direct_write() 2951da177e4SLinus Torvalds * and do_lo_send_write(). 2961da177e4SLinus Torvalds */ 297858119e1SArjan van de Ven static int __do_lo_send_write(struct file *file, 2981da177e4SLinus Torvalds u8 __user *buf, const int len, loff_t pos) 2991da177e4SLinus Torvalds { 3001da177e4SLinus Torvalds ssize_t bw; 3011da177e4SLinus Torvalds mm_segment_t old_fs = get_fs(); 3021da177e4SLinus Torvalds 3031da177e4SLinus Torvalds set_fs(get_ds()); 3041da177e4SLinus Torvalds bw = file->f_op->write(file, buf, len, &pos); 3051da177e4SLinus Torvalds set_fs(old_fs); 3061da177e4SLinus Torvalds if (likely(bw == len)) 3071da177e4SLinus Torvalds return 0; 3081da177e4SLinus Torvalds printk(KERN_ERR "loop: Write error at byte offset %llu, length %i.\n", 3091da177e4SLinus Torvalds (unsigned long long)pos, len); 3101da177e4SLinus Torvalds if (bw >= 0) 3111da177e4SLinus Torvalds bw = -EIO; 3121da177e4SLinus Torvalds return bw; 3131da177e4SLinus Torvalds } 3141da177e4SLinus Torvalds 3151da177e4SLinus Torvalds /** 3161da177e4SLinus Torvalds * do_lo_send_direct_write - helper for writing data to a loop device 3171da177e4SLinus Torvalds * 3181da177e4SLinus Torvalds * This is the fast, non-transforming version for backing filesystems which do 3191da177e4SLinus Torvalds * not implement the address space operations prepare_write and commit_write. 3201da177e4SLinus Torvalds * It uses the write file operation which should be present on all writeable 3211da177e4SLinus Torvalds * filesystems. 3221da177e4SLinus Torvalds */ 3231da177e4SLinus Torvalds static int do_lo_send_direct_write(struct loop_device *lo, 3241da177e4SLinus Torvalds struct bio_vec *bvec, int bsize, loff_t pos, struct page *page) 3251da177e4SLinus Torvalds { 3261da177e4SLinus Torvalds ssize_t bw = __do_lo_send_write(lo->lo_backing_file, 3271da177e4SLinus Torvalds (u8 __user *)kmap(bvec->bv_page) + bvec->bv_offset, 3281da177e4SLinus Torvalds bvec->bv_len, pos); 3291da177e4SLinus Torvalds kunmap(bvec->bv_page); 3301da177e4SLinus Torvalds cond_resched(); 3311da177e4SLinus Torvalds return bw; 3321da177e4SLinus Torvalds } 3331da177e4SLinus Torvalds 3341da177e4SLinus Torvalds /** 3351da177e4SLinus Torvalds * do_lo_send_write - helper for writing data to a loop device 3361da177e4SLinus Torvalds * 3371da177e4SLinus Torvalds * This is the slow, transforming version for filesystems which do not 3381da177e4SLinus Torvalds * implement the address space operations prepare_write and commit_write. It 3391da177e4SLinus Torvalds * uses the write file operation which should be present on all writeable 3401da177e4SLinus Torvalds * filesystems. 3411da177e4SLinus Torvalds * 3421da177e4SLinus Torvalds * Using fops->write is slower than using aops->{prepare,commit}_write in the 3431da177e4SLinus Torvalds * transforming case because we need to double buffer the data as we cannot do 3441da177e4SLinus Torvalds * the transformations in place as we do not have direct access to the 3451da177e4SLinus Torvalds * destination pages of the backing file. 3461da177e4SLinus Torvalds */ 3471da177e4SLinus Torvalds static int do_lo_send_write(struct loop_device *lo, struct bio_vec *bvec, 3481da177e4SLinus Torvalds int bsize, loff_t pos, struct page *page) 3491da177e4SLinus Torvalds { 3501da177e4SLinus Torvalds int ret = lo_do_transfer(lo, WRITE, page, 0, bvec->bv_page, 3511da177e4SLinus Torvalds bvec->bv_offset, bvec->bv_len, pos >> 9); 3521da177e4SLinus Torvalds if (likely(!ret)) 3531da177e4SLinus Torvalds return __do_lo_send_write(lo->lo_backing_file, 3541da177e4SLinus Torvalds (u8 __user *)page_address(page), bvec->bv_len, 3551da177e4SLinus Torvalds pos); 3561da177e4SLinus Torvalds printk(KERN_ERR "loop: Transfer error at byte offset %llu, " 3571da177e4SLinus Torvalds "length %i.\n", (unsigned long long)pos, bvec->bv_len); 3581da177e4SLinus Torvalds if (ret > 0) 3591da177e4SLinus Torvalds ret = -EIO; 3601da177e4SLinus Torvalds return ret; 3611da177e4SLinus Torvalds } 3621da177e4SLinus Torvalds 3631da177e4SLinus Torvalds static int lo_send(struct loop_device *lo, struct bio *bio, int bsize, 3641da177e4SLinus Torvalds loff_t pos) 3651da177e4SLinus Torvalds { 3661da177e4SLinus Torvalds int (*do_lo_send)(struct loop_device *, struct bio_vec *, int, loff_t, 3671da177e4SLinus Torvalds struct page *page); 3681da177e4SLinus Torvalds struct bio_vec *bvec; 3691da177e4SLinus Torvalds struct page *page = NULL; 3701da177e4SLinus Torvalds int i, ret = 0; 3711da177e4SLinus Torvalds 3721da177e4SLinus Torvalds do_lo_send = do_lo_send_aops; 3731da177e4SLinus Torvalds if (!(lo->lo_flags & LO_FLAGS_USE_AOPS)) { 3741da177e4SLinus Torvalds do_lo_send = do_lo_send_direct_write; 3751da177e4SLinus Torvalds if (lo->transfer != transfer_none) { 3761da177e4SLinus Torvalds page = alloc_page(GFP_NOIO | __GFP_HIGHMEM); 3771da177e4SLinus Torvalds if (unlikely(!page)) 3781da177e4SLinus Torvalds goto fail; 3791da177e4SLinus Torvalds kmap(page); 3801da177e4SLinus Torvalds do_lo_send = do_lo_send_write; 3811da177e4SLinus Torvalds } 3821da177e4SLinus Torvalds } 3831da177e4SLinus Torvalds bio_for_each_segment(bvec, bio, i) { 3841da177e4SLinus Torvalds ret = do_lo_send(lo, bvec, bsize, pos, page); 3851da177e4SLinus Torvalds if (ret < 0) 3861da177e4SLinus Torvalds break; 3871da177e4SLinus Torvalds pos += bvec->bv_len; 3881da177e4SLinus Torvalds } 3891da177e4SLinus Torvalds if (page) { 3901da177e4SLinus Torvalds kunmap(page); 3911da177e4SLinus Torvalds __free_page(page); 3921da177e4SLinus Torvalds } 3931da177e4SLinus Torvalds out: 3941da177e4SLinus Torvalds return ret; 3951da177e4SLinus Torvalds fail: 3961da177e4SLinus Torvalds printk(KERN_ERR "loop: Failed to allocate temporary page for write.\n"); 3971da177e4SLinus Torvalds ret = -ENOMEM; 3981da177e4SLinus Torvalds goto out; 3991da177e4SLinus Torvalds } 4001da177e4SLinus Torvalds 4011da177e4SLinus Torvalds struct lo_read_data { 4021da177e4SLinus Torvalds struct loop_device *lo; 4031da177e4SLinus Torvalds struct page *page; 4041da177e4SLinus Torvalds unsigned offset; 4051da177e4SLinus Torvalds int bsize; 4061da177e4SLinus Torvalds }; 4071da177e4SLinus Torvalds 4081da177e4SLinus Torvalds static int 4091da177e4SLinus Torvalds lo_read_actor(read_descriptor_t *desc, struct page *page, 4101da177e4SLinus Torvalds unsigned long offset, unsigned long size) 4111da177e4SLinus Torvalds { 4121da177e4SLinus Torvalds unsigned long count = desc->count; 4131da177e4SLinus Torvalds struct lo_read_data *p = desc->arg.data; 4141da177e4SLinus Torvalds struct loop_device *lo = p->lo; 4151da177e4SLinus Torvalds sector_t IV; 4161da177e4SLinus Torvalds 4171da177e4SLinus Torvalds IV = ((sector_t) page->index << (PAGE_CACHE_SHIFT - 9))+(offset >> 9); 4181da177e4SLinus Torvalds 4191da177e4SLinus Torvalds if (size > count) 4201da177e4SLinus Torvalds size = count; 4211da177e4SLinus Torvalds 4221da177e4SLinus Torvalds if (lo_do_transfer(lo, READ, page, offset, p->page, p->offset, size, IV)) { 4231da177e4SLinus Torvalds size = 0; 4241da177e4SLinus Torvalds printk(KERN_ERR "loop: transfer error block %ld\n", 4251da177e4SLinus Torvalds page->index); 4261da177e4SLinus Torvalds desc->error = -EINVAL; 4271da177e4SLinus Torvalds } 4281da177e4SLinus Torvalds 4291da177e4SLinus Torvalds flush_dcache_page(p->page); 4301da177e4SLinus Torvalds 4311da177e4SLinus Torvalds desc->count = count - size; 4321da177e4SLinus Torvalds desc->written += size; 4331da177e4SLinus Torvalds p->offset += size; 4341da177e4SLinus Torvalds return size; 4351da177e4SLinus Torvalds } 4361da177e4SLinus Torvalds 4371da177e4SLinus Torvalds static int 4381da177e4SLinus Torvalds do_lo_receive(struct loop_device *lo, 4391da177e4SLinus Torvalds struct bio_vec *bvec, int bsize, loff_t pos) 4401da177e4SLinus Torvalds { 4411da177e4SLinus Torvalds struct lo_read_data cookie; 4421da177e4SLinus Torvalds struct file *file; 4431da177e4SLinus Torvalds int retval; 4441da177e4SLinus Torvalds 4451da177e4SLinus Torvalds cookie.lo = lo; 4461da177e4SLinus Torvalds cookie.page = bvec->bv_page; 4471da177e4SLinus Torvalds cookie.offset = bvec->bv_offset; 4481da177e4SLinus Torvalds cookie.bsize = bsize; 4491da177e4SLinus Torvalds file = lo->lo_backing_file; 4501da177e4SLinus Torvalds retval = file->f_op->sendfile(file, &pos, bvec->bv_len, 4511da177e4SLinus Torvalds lo_read_actor, &cookie); 4521da177e4SLinus Torvalds return (retval < 0)? retval: 0; 4531da177e4SLinus Torvalds } 4541da177e4SLinus Torvalds 4551da177e4SLinus Torvalds static int 4561da177e4SLinus Torvalds lo_receive(struct loop_device *lo, struct bio *bio, int bsize, loff_t pos) 4571da177e4SLinus Torvalds { 4581da177e4SLinus Torvalds struct bio_vec *bvec; 4591da177e4SLinus Torvalds int i, ret = 0; 4601da177e4SLinus Torvalds 4611da177e4SLinus Torvalds bio_for_each_segment(bvec, bio, i) { 4621da177e4SLinus Torvalds ret = do_lo_receive(lo, bvec, bsize, pos); 4631da177e4SLinus Torvalds if (ret < 0) 4641da177e4SLinus Torvalds break; 4651da177e4SLinus Torvalds pos += bvec->bv_len; 4661da177e4SLinus Torvalds } 4671da177e4SLinus Torvalds return ret; 4681da177e4SLinus Torvalds } 4691da177e4SLinus Torvalds 4701da177e4SLinus Torvalds static int do_bio_filebacked(struct loop_device *lo, struct bio *bio) 4711da177e4SLinus Torvalds { 4721da177e4SLinus Torvalds loff_t pos; 4731da177e4SLinus Torvalds int ret; 4741da177e4SLinus Torvalds 4751da177e4SLinus Torvalds pos = ((loff_t) bio->bi_sector << 9) + lo->lo_offset; 4761da177e4SLinus Torvalds if (bio_rw(bio) == WRITE) 4771da177e4SLinus Torvalds ret = lo_send(lo, bio, lo->lo_blocksize, pos); 4781da177e4SLinus Torvalds else 4791da177e4SLinus Torvalds ret = lo_receive(lo, bio, lo->lo_blocksize, pos); 4801da177e4SLinus Torvalds return ret; 4811da177e4SLinus Torvalds } 4821da177e4SLinus Torvalds 4831da177e4SLinus Torvalds /* 4841da177e4SLinus Torvalds * Add bio to back of pending list 4851da177e4SLinus Torvalds */ 4861da177e4SLinus Torvalds static void loop_add_bio(struct loop_device *lo, struct bio *bio) 4871da177e4SLinus Torvalds { 4881da177e4SLinus Torvalds if (lo->lo_biotail) { 4891da177e4SLinus Torvalds lo->lo_biotail->bi_next = bio; 4901da177e4SLinus Torvalds lo->lo_biotail = bio; 4911da177e4SLinus Torvalds } else 4921da177e4SLinus Torvalds lo->lo_bio = lo->lo_biotail = bio; 4931da177e4SLinus Torvalds } 4941da177e4SLinus Torvalds 4951da177e4SLinus Torvalds /* 4961da177e4SLinus Torvalds * Grab first pending buffer 4971da177e4SLinus Torvalds */ 4981da177e4SLinus Torvalds static struct bio *loop_get_bio(struct loop_device *lo) 4991da177e4SLinus Torvalds { 5001da177e4SLinus Torvalds struct bio *bio; 5011da177e4SLinus Torvalds 5021da177e4SLinus Torvalds if ((bio = lo->lo_bio)) { 5031da177e4SLinus Torvalds if (bio == lo->lo_biotail) 5041da177e4SLinus Torvalds lo->lo_biotail = NULL; 5051da177e4SLinus Torvalds lo->lo_bio = bio->bi_next; 5061da177e4SLinus Torvalds bio->bi_next = NULL; 5071da177e4SLinus Torvalds } 5081da177e4SLinus Torvalds 5091da177e4SLinus Torvalds return bio; 5101da177e4SLinus Torvalds } 5111da177e4SLinus Torvalds 5121da177e4SLinus Torvalds static int loop_make_request(request_queue_t *q, struct bio *old_bio) 5131da177e4SLinus Torvalds { 5141da177e4SLinus Torvalds struct loop_device *lo = q->queuedata; 5151da177e4SLinus Torvalds int rw = bio_rw(old_bio); 5161da177e4SLinus Torvalds 51735a82d1aSNick Piggin if (rw == READA) 51835a82d1aSNick Piggin rw = READ; 51935a82d1aSNick Piggin 52035a82d1aSNick Piggin BUG_ON(!lo || (rw != READ && rw != WRITE)); 5211da177e4SLinus Torvalds 5221da177e4SLinus Torvalds spin_lock_irq(&lo->lo_lock); 5231da177e4SLinus Torvalds if (lo->lo_state != Lo_bound) 52435a82d1aSNick Piggin goto out; 52535a82d1aSNick Piggin if (unlikely(rw == WRITE && (lo->lo_flags & LO_FLAGS_READ_ONLY))) 52635a82d1aSNick Piggin goto out; 52735a82d1aSNick Piggin lo->lo_pending++; 5281da177e4SLinus Torvalds loop_add_bio(lo, old_bio); 52935a82d1aSNick Piggin spin_unlock_irq(&lo->lo_lock); 53011b751aeSIngo Molnar complete(&lo->lo_bh_done); 53135a82d1aSNick Piggin return 0; 53235a82d1aSNick Piggin 5331da177e4SLinus Torvalds out: 53435a82d1aSNick Piggin if (lo->lo_pending == 0) 53511b751aeSIngo Molnar complete(&lo->lo_bh_done); 53635a82d1aSNick Piggin spin_unlock_irq(&lo->lo_lock); 5371da177e4SLinus Torvalds bio_io_error(old_bio, old_bio->bi_size); 5381da177e4SLinus Torvalds return 0; 5391da177e4SLinus Torvalds } 5401da177e4SLinus Torvalds 5411da177e4SLinus Torvalds /* 5421da177e4SLinus Torvalds * kick off io on the underlying address space 5431da177e4SLinus Torvalds */ 5441da177e4SLinus Torvalds static void loop_unplug(request_queue_t *q) 5451da177e4SLinus Torvalds { 5461da177e4SLinus Torvalds struct loop_device *lo = q->queuedata; 5471da177e4SLinus Torvalds 5481da177e4SLinus Torvalds clear_bit(QUEUE_FLAG_PLUGGED, &q->queue_flags); 5491da177e4SLinus Torvalds blk_run_address_space(lo->lo_backing_file->f_mapping); 5501da177e4SLinus Torvalds } 5511da177e4SLinus Torvalds 5521da177e4SLinus Torvalds struct switch_request { 5531da177e4SLinus Torvalds struct file *file; 5541da177e4SLinus Torvalds struct completion wait; 5551da177e4SLinus Torvalds }; 5561da177e4SLinus Torvalds 5571da177e4SLinus Torvalds static void do_loop_switch(struct loop_device *, struct switch_request *); 5581da177e4SLinus Torvalds 5591da177e4SLinus Torvalds static inline void loop_handle_bio(struct loop_device *lo, struct bio *bio) 5601da177e4SLinus Torvalds { 5611da177e4SLinus Torvalds if (unlikely(!bio->bi_bdev)) { 5621da177e4SLinus Torvalds do_loop_switch(lo, bio->bi_private); 5631da177e4SLinus Torvalds bio_put(bio); 5641da177e4SLinus Torvalds } else { 56535a82d1aSNick Piggin int ret = do_bio_filebacked(lo, bio); 5661da177e4SLinus Torvalds bio_endio(bio, bio->bi_size, ret); 5671da177e4SLinus Torvalds } 5681da177e4SLinus Torvalds } 5691da177e4SLinus Torvalds 5701da177e4SLinus Torvalds /* 5711da177e4SLinus Torvalds * worker thread that handles reads/writes to file backed loop devices, 5721da177e4SLinus Torvalds * to avoid blocking in our make_request_fn. it also does loop decrypting 5731da177e4SLinus Torvalds * on reads for block backed loop, as that is too heavy to do from 5741da177e4SLinus Torvalds * b_end_io context where irqs may be disabled. 5751da177e4SLinus Torvalds */ 5761da177e4SLinus Torvalds static int loop_thread(void *data) 5771da177e4SLinus Torvalds { 5781da177e4SLinus Torvalds struct loop_device *lo = data; 5791da177e4SLinus Torvalds struct bio *bio; 5801da177e4SLinus Torvalds 58109c0dc68SLinus Torvalds daemonize("loop%d", lo->lo_number); 58209c0dc68SLinus Torvalds 5831da177e4SLinus Torvalds /* 5841da177e4SLinus Torvalds * loop can be used in an encrypted device, 5851da177e4SLinus Torvalds * hence, it mustn't be stopped at all 5861da177e4SLinus Torvalds * because it could be indirectly used during suspension 5871da177e4SLinus Torvalds */ 5881da177e4SLinus Torvalds current->flags |= PF_NOFREEZE; 5891da177e4SLinus Torvalds 5901da177e4SLinus Torvalds set_user_nice(current, -20); 5911da177e4SLinus Torvalds 5921da177e4SLinus Torvalds lo->lo_state = Lo_bound; 59335a82d1aSNick Piggin lo->lo_pending = 1; 5941da177e4SLinus Torvalds 59509c0dc68SLinus Torvalds /* 59609c0dc68SLinus Torvalds * complete it, we are running 59709c0dc68SLinus Torvalds */ 59809c0dc68SLinus Torvalds complete(&lo->lo_done); 59909c0dc68SLinus Torvalds 6001da177e4SLinus Torvalds for (;;) { 60135a82d1aSNick Piggin int pending; 60235a82d1aSNick Piggin 60311b751aeSIngo Molnar if (wait_for_completion_interruptible(&lo->lo_bh_done)) 60435a82d1aSNick Piggin continue; 60535a82d1aSNick Piggin 60635a82d1aSNick Piggin spin_lock_irq(&lo->lo_lock); 60735a82d1aSNick Piggin 60835a82d1aSNick Piggin /* 60911b751aeSIngo Molnar * could be completed because of tear-down, not pending work 61035a82d1aSNick Piggin */ 61135a82d1aSNick Piggin if (unlikely(!lo->lo_pending)) { 61235a82d1aSNick Piggin spin_unlock_irq(&lo->lo_lock); 6131da177e4SLinus Torvalds break; 61435a82d1aSNick Piggin } 6151da177e4SLinus Torvalds 6161da177e4SLinus Torvalds bio = loop_get_bio(lo); 61735a82d1aSNick Piggin lo->lo_pending--; 61835a82d1aSNick Piggin pending = lo->lo_pending; 61935a82d1aSNick Piggin spin_unlock_irq(&lo->lo_lock); 62035a82d1aSNick Piggin 62135a82d1aSNick Piggin BUG_ON(!bio); 6221da177e4SLinus Torvalds loop_handle_bio(lo, bio); 6231da177e4SLinus Torvalds 6241da177e4SLinus Torvalds /* 6251da177e4SLinus Torvalds * upped both for pending work and tear-down, lo_pending 6261da177e4SLinus Torvalds * will hit zero then 6271da177e4SLinus Torvalds */ 62835a82d1aSNick Piggin if (unlikely(!pending)) 6291da177e4SLinus Torvalds break; 6301da177e4SLinus Torvalds } 6311da177e4SLinus Torvalds 63209c0dc68SLinus Torvalds complete(&lo->lo_done); 6331da177e4SLinus Torvalds return 0; 6341da177e4SLinus Torvalds } 6351da177e4SLinus Torvalds 6361da177e4SLinus Torvalds /* 6371da177e4SLinus Torvalds * loop_switch performs the hard work of switching a backing store. 6381da177e4SLinus Torvalds * First it needs to flush existing IO, it does this by sending a magic 6391da177e4SLinus Torvalds * BIO down the pipe. The completion of this BIO does the actual switch. 6401da177e4SLinus Torvalds */ 6411da177e4SLinus Torvalds static int loop_switch(struct loop_device *lo, struct file *file) 6421da177e4SLinus Torvalds { 6431da177e4SLinus Torvalds struct switch_request w; 6441da177e4SLinus Torvalds struct bio *bio = bio_alloc(GFP_KERNEL, 1); 6451da177e4SLinus Torvalds if (!bio) 6461da177e4SLinus Torvalds return -ENOMEM; 6471da177e4SLinus Torvalds init_completion(&w.wait); 6481da177e4SLinus Torvalds w.file = file; 6491da177e4SLinus Torvalds bio->bi_private = &w; 6501da177e4SLinus Torvalds bio->bi_bdev = NULL; 6511da177e4SLinus Torvalds loop_make_request(lo->lo_queue, bio); 6521da177e4SLinus Torvalds wait_for_completion(&w.wait); 6531da177e4SLinus Torvalds return 0; 6541da177e4SLinus Torvalds } 6551da177e4SLinus Torvalds 6561da177e4SLinus Torvalds /* 6571da177e4SLinus Torvalds * Do the actual switch; called from the BIO completion routine 6581da177e4SLinus Torvalds */ 6591da177e4SLinus Torvalds static void do_loop_switch(struct loop_device *lo, struct switch_request *p) 6601da177e4SLinus Torvalds { 6611da177e4SLinus Torvalds struct file *file = p->file; 6621da177e4SLinus Torvalds struct file *old_file = lo->lo_backing_file; 6631da177e4SLinus Torvalds struct address_space *mapping = file->f_mapping; 6641da177e4SLinus Torvalds 6651da177e4SLinus Torvalds mapping_set_gfp_mask(old_file->f_mapping, lo->old_gfp_mask); 6661da177e4SLinus Torvalds lo->lo_backing_file = file; 6671da177e4SLinus Torvalds lo->lo_blocksize = mapping->host->i_blksize; 6681da177e4SLinus Torvalds lo->old_gfp_mask = mapping_gfp_mask(mapping); 6691da177e4SLinus Torvalds mapping_set_gfp_mask(mapping, lo->old_gfp_mask & ~(__GFP_IO|__GFP_FS)); 6701da177e4SLinus Torvalds complete(&p->wait); 6711da177e4SLinus Torvalds } 6721da177e4SLinus Torvalds 6731da177e4SLinus Torvalds 6741da177e4SLinus Torvalds /* 6751da177e4SLinus Torvalds * loop_change_fd switched the backing store of a loopback device to 6761da177e4SLinus Torvalds * a new file. This is useful for operating system installers to free up 6771da177e4SLinus Torvalds * the original file and in High Availability environments to switch to 6781da177e4SLinus Torvalds * an alternative location for the content in case of server meltdown. 6791da177e4SLinus Torvalds * This can only work if the loop device is used read-only, and if the 6801da177e4SLinus Torvalds * new backing store is the same size and type as the old backing store. 6811da177e4SLinus Torvalds */ 6821da177e4SLinus Torvalds static int loop_change_fd(struct loop_device *lo, struct file *lo_file, 6831da177e4SLinus Torvalds struct block_device *bdev, unsigned int arg) 6841da177e4SLinus Torvalds { 6851da177e4SLinus Torvalds struct file *file, *old_file; 6861da177e4SLinus Torvalds struct inode *inode; 6871da177e4SLinus Torvalds int error; 6881da177e4SLinus Torvalds 6891da177e4SLinus Torvalds error = -ENXIO; 6901da177e4SLinus Torvalds if (lo->lo_state != Lo_bound) 6911da177e4SLinus Torvalds goto out; 6921da177e4SLinus Torvalds 6931da177e4SLinus Torvalds /* the loop device has to be read-only */ 6941da177e4SLinus Torvalds error = -EINVAL; 6951da177e4SLinus Torvalds if (!(lo->lo_flags & LO_FLAGS_READ_ONLY)) 6961da177e4SLinus Torvalds goto out; 6971da177e4SLinus Torvalds 6981da177e4SLinus Torvalds error = -EBADF; 6991da177e4SLinus Torvalds file = fget(arg); 7001da177e4SLinus Torvalds if (!file) 7011da177e4SLinus Torvalds goto out; 7021da177e4SLinus Torvalds 7031da177e4SLinus Torvalds inode = file->f_mapping->host; 7041da177e4SLinus Torvalds old_file = lo->lo_backing_file; 7051da177e4SLinus Torvalds 7061da177e4SLinus Torvalds error = -EINVAL; 7071da177e4SLinus Torvalds 7081da177e4SLinus Torvalds if (!S_ISREG(inode->i_mode) && !S_ISBLK(inode->i_mode)) 7091da177e4SLinus Torvalds goto out_putf; 7101da177e4SLinus Torvalds 7111da177e4SLinus Torvalds /* new backing store needs to support loop (eg sendfile) */ 7121da177e4SLinus Torvalds if (!inode->i_fop->sendfile) 7131da177e4SLinus Torvalds goto out_putf; 7141da177e4SLinus Torvalds 7151da177e4SLinus Torvalds /* size of the new backing store needs to be the same */ 7161da177e4SLinus Torvalds if (get_loop_size(lo, file) != get_loop_size(lo, old_file)) 7171da177e4SLinus Torvalds goto out_putf; 7181da177e4SLinus Torvalds 7191da177e4SLinus Torvalds /* and ... switch */ 7201da177e4SLinus Torvalds error = loop_switch(lo, file); 7211da177e4SLinus Torvalds if (error) 7221da177e4SLinus Torvalds goto out_putf; 7231da177e4SLinus Torvalds 7241da177e4SLinus Torvalds fput(old_file); 7251da177e4SLinus Torvalds return 0; 7261da177e4SLinus Torvalds 7271da177e4SLinus Torvalds out_putf: 7281da177e4SLinus Torvalds fput(file); 7291da177e4SLinus Torvalds out: 7301da177e4SLinus Torvalds return error; 7311da177e4SLinus Torvalds } 7321da177e4SLinus Torvalds 7331da177e4SLinus Torvalds static inline int is_loop_device(struct file *file) 7341da177e4SLinus Torvalds { 7351da177e4SLinus Torvalds struct inode *i = file->f_mapping->host; 7361da177e4SLinus Torvalds 7371da177e4SLinus Torvalds return i && S_ISBLK(i->i_mode) && MAJOR(i->i_rdev) == LOOP_MAJOR; 7381da177e4SLinus Torvalds } 7391da177e4SLinus Torvalds 7401da177e4SLinus Torvalds static int loop_set_fd(struct loop_device *lo, struct file *lo_file, 7411da177e4SLinus Torvalds struct block_device *bdev, unsigned int arg) 7421da177e4SLinus Torvalds { 7431da177e4SLinus Torvalds struct file *file, *f; 7441da177e4SLinus Torvalds struct inode *inode; 7451da177e4SLinus Torvalds struct address_space *mapping; 7461da177e4SLinus Torvalds unsigned lo_blocksize; 7471da177e4SLinus Torvalds int lo_flags = 0; 7481da177e4SLinus Torvalds int error; 7491da177e4SLinus Torvalds loff_t size; 7501da177e4SLinus Torvalds 7511da177e4SLinus Torvalds /* This is safe, since we have a reference from open(). */ 7521da177e4SLinus Torvalds __module_get(THIS_MODULE); 7531da177e4SLinus Torvalds 7541da177e4SLinus Torvalds error = -EBADF; 7551da177e4SLinus Torvalds file = fget(arg); 7561da177e4SLinus Torvalds if (!file) 7571da177e4SLinus Torvalds goto out; 7581da177e4SLinus Torvalds 7591da177e4SLinus Torvalds error = -EBUSY; 7601da177e4SLinus Torvalds if (lo->lo_state != Lo_unbound) 7611da177e4SLinus Torvalds goto out_putf; 7621da177e4SLinus Torvalds 7631da177e4SLinus Torvalds /* Avoid recursion */ 7641da177e4SLinus Torvalds f = file; 7651da177e4SLinus Torvalds while (is_loop_device(f)) { 7661da177e4SLinus Torvalds struct loop_device *l; 7671da177e4SLinus Torvalds 7681da177e4SLinus Torvalds if (f->f_mapping->host->i_rdev == lo_file->f_mapping->host->i_rdev) 7691da177e4SLinus Torvalds goto out_putf; 7701da177e4SLinus Torvalds 7711da177e4SLinus Torvalds l = f->f_mapping->host->i_bdev->bd_disk->private_data; 7721da177e4SLinus Torvalds if (l->lo_state == Lo_unbound) { 7731da177e4SLinus Torvalds error = -EINVAL; 7741da177e4SLinus Torvalds goto out_putf; 7751da177e4SLinus Torvalds } 7761da177e4SLinus Torvalds f = l->lo_backing_file; 7771da177e4SLinus Torvalds } 7781da177e4SLinus Torvalds 7791da177e4SLinus Torvalds mapping = file->f_mapping; 7801da177e4SLinus Torvalds inode = mapping->host; 7811da177e4SLinus Torvalds 7821da177e4SLinus Torvalds if (!(file->f_mode & FMODE_WRITE)) 7831da177e4SLinus Torvalds lo_flags |= LO_FLAGS_READ_ONLY; 7841da177e4SLinus Torvalds 7851da177e4SLinus Torvalds error = -EINVAL; 7861da177e4SLinus Torvalds if (S_ISREG(inode->i_mode) || S_ISBLK(inode->i_mode)) { 787*f5e54d6eSChristoph Hellwig const struct address_space_operations *aops = mapping->a_ops; 7881da177e4SLinus Torvalds /* 7891da177e4SLinus Torvalds * If we can't read - sorry. If we only can't write - well, 7901da177e4SLinus Torvalds * it's going to be read-only. 7911da177e4SLinus Torvalds */ 7921da177e4SLinus Torvalds if (!file->f_op->sendfile) 7931da177e4SLinus Torvalds goto out_putf; 7941da177e4SLinus Torvalds if (aops->prepare_write && aops->commit_write) 7951da177e4SLinus Torvalds lo_flags |= LO_FLAGS_USE_AOPS; 7961da177e4SLinus Torvalds if (!(lo_flags & LO_FLAGS_USE_AOPS) && !file->f_op->write) 7971da177e4SLinus Torvalds lo_flags |= LO_FLAGS_READ_ONLY; 7981da177e4SLinus Torvalds 7991da177e4SLinus Torvalds lo_blocksize = inode->i_blksize; 8001da177e4SLinus Torvalds error = 0; 8011da177e4SLinus Torvalds } else { 8021da177e4SLinus Torvalds goto out_putf; 8031da177e4SLinus Torvalds } 8041da177e4SLinus Torvalds 8051da177e4SLinus Torvalds size = get_loop_size(lo, file); 8061da177e4SLinus Torvalds 8071da177e4SLinus Torvalds if ((loff_t)(sector_t)size != size) { 8081da177e4SLinus Torvalds error = -EFBIG; 8091da177e4SLinus Torvalds goto out_putf; 8101da177e4SLinus Torvalds } 8111da177e4SLinus Torvalds 8121da177e4SLinus Torvalds if (!(lo_file->f_mode & FMODE_WRITE)) 8131da177e4SLinus Torvalds lo_flags |= LO_FLAGS_READ_ONLY; 8141da177e4SLinus Torvalds 8151da177e4SLinus Torvalds set_device_ro(bdev, (lo_flags & LO_FLAGS_READ_ONLY) != 0); 8161da177e4SLinus Torvalds 8171da177e4SLinus Torvalds lo->lo_blocksize = lo_blocksize; 8181da177e4SLinus Torvalds lo->lo_device = bdev; 8191da177e4SLinus Torvalds lo->lo_flags = lo_flags; 8201da177e4SLinus Torvalds lo->lo_backing_file = file; 821eefe85eeSConstantine Sapuntzakis lo->transfer = transfer_none; 8221da177e4SLinus Torvalds lo->ioctl = NULL; 8231da177e4SLinus Torvalds lo->lo_sizelimit = 0; 8241da177e4SLinus Torvalds lo->old_gfp_mask = mapping_gfp_mask(mapping); 8251da177e4SLinus Torvalds mapping_set_gfp_mask(mapping, lo->old_gfp_mask & ~(__GFP_IO|__GFP_FS)); 8261da177e4SLinus Torvalds 8271da177e4SLinus Torvalds lo->lo_bio = lo->lo_biotail = NULL; 8281da177e4SLinus Torvalds 8291da177e4SLinus Torvalds /* 8301da177e4SLinus Torvalds * set queue make_request_fn, and add limits based on lower level 8311da177e4SLinus Torvalds * device 8321da177e4SLinus Torvalds */ 8331da177e4SLinus Torvalds blk_queue_make_request(lo->lo_queue, loop_make_request); 8341da177e4SLinus Torvalds lo->lo_queue->queuedata = lo; 8351da177e4SLinus Torvalds lo->lo_queue->unplug_fn = loop_unplug; 8361da177e4SLinus Torvalds 8371da177e4SLinus Torvalds set_capacity(disks[lo->lo_number], size); 8381da177e4SLinus Torvalds bd_set_size(bdev, size << 9); 8391da177e4SLinus Torvalds 8401da177e4SLinus Torvalds set_blocksize(bdev, lo_blocksize); 8411da177e4SLinus Torvalds 84209c0dc68SLinus Torvalds error = kernel_thread(loop_thread, lo, CLONE_KERNEL); 84309c0dc68SLinus Torvalds if (error < 0) 8443e88c17dSHerbert Poetzl goto out_putf; 84509c0dc68SLinus Torvalds wait_for_completion(&lo->lo_done); 8461da177e4SLinus Torvalds return 0; 8471da177e4SLinus Torvalds 8481da177e4SLinus Torvalds out_putf: 8491da177e4SLinus Torvalds fput(file); 8501da177e4SLinus Torvalds out: 8511da177e4SLinus Torvalds /* This is safe: open() is still holding a reference. */ 8521da177e4SLinus Torvalds module_put(THIS_MODULE); 8531da177e4SLinus Torvalds return error; 8541da177e4SLinus Torvalds } 8551da177e4SLinus Torvalds 8561da177e4SLinus Torvalds static int 8571da177e4SLinus Torvalds loop_release_xfer(struct loop_device *lo) 8581da177e4SLinus Torvalds { 8591da177e4SLinus Torvalds int err = 0; 8601da177e4SLinus Torvalds struct loop_func_table *xfer = lo->lo_encryption; 8611da177e4SLinus Torvalds 8621da177e4SLinus Torvalds if (xfer) { 8631da177e4SLinus Torvalds if (xfer->release) 8641da177e4SLinus Torvalds err = xfer->release(lo); 8651da177e4SLinus Torvalds lo->transfer = NULL; 8661da177e4SLinus Torvalds lo->lo_encryption = NULL; 8671da177e4SLinus Torvalds module_put(xfer->owner); 8681da177e4SLinus Torvalds } 8691da177e4SLinus Torvalds return err; 8701da177e4SLinus Torvalds } 8711da177e4SLinus Torvalds 8721da177e4SLinus Torvalds static int 8731da177e4SLinus Torvalds loop_init_xfer(struct loop_device *lo, struct loop_func_table *xfer, 8741da177e4SLinus Torvalds const struct loop_info64 *i) 8751da177e4SLinus Torvalds { 8761da177e4SLinus Torvalds int err = 0; 8771da177e4SLinus Torvalds 8781da177e4SLinus Torvalds if (xfer) { 8791da177e4SLinus Torvalds struct module *owner = xfer->owner; 8801da177e4SLinus Torvalds 8811da177e4SLinus Torvalds if (!try_module_get(owner)) 8821da177e4SLinus Torvalds return -EINVAL; 8831da177e4SLinus Torvalds if (xfer->init) 8841da177e4SLinus Torvalds err = xfer->init(lo, i); 8851da177e4SLinus Torvalds if (err) 8861da177e4SLinus Torvalds module_put(owner); 8871da177e4SLinus Torvalds else 8881da177e4SLinus Torvalds lo->lo_encryption = xfer; 8891da177e4SLinus Torvalds } 8901da177e4SLinus Torvalds return err; 8911da177e4SLinus Torvalds } 8921da177e4SLinus Torvalds 8931da177e4SLinus Torvalds static int loop_clr_fd(struct loop_device *lo, struct block_device *bdev) 8941da177e4SLinus Torvalds { 8951da177e4SLinus Torvalds struct file *filp = lo->lo_backing_file; 896b4e3ca1aSAl Viro gfp_t gfp = lo->old_gfp_mask; 8971da177e4SLinus Torvalds 8981da177e4SLinus Torvalds if (lo->lo_state != Lo_bound) 8991da177e4SLinus Torvalds return -ENXIO; 9001da177e4SLinus Torvalds 9011da177e4SLinus Torvalds if (lo->lo_refcnt > 1) /* we needed one fd for the ioctl */ 9021da177e4SLinus Torvalds return -EBUSY; 9031da177e4SLinus Torvalds 9041da177e4SLinus Torvalds if (filp == NULL) 9051da177e4SLinus Torvalds return -EINVAL; 9061da177e4SLinus Torvalds 9071da177e4SLinus Torvalds spin_lock_irq(&lo->lo_lock); 9081da177e4SLinus Torvalds lo->lo_state = Lo_rundown; 90935a82d1aSNick Piggin lo->lo_pending--; 91035a82d1aSNick Piggin if (!lo->lo_pending) 91111b751aeSIngo Molnar complete(&lo->lo_bh_done); 9121da177e4SLinus Torvalds spin_unlock_irq(&lo->lo_lock); 9131da177e4SLinus Torvalds 91409c0dc68SLinus Torvalds wait_for_completion(&lo->lo_done); 9151da177e4SLinus Torvalds 9161da177e4SLinus Torvalds lo->lo_backing_file = NULL; 9171da177e4SLinus Torvalds 9181da177e4SLinus Torvalds loop_release_xfer(lo); 9191da177e4SLinus Torvalds lo->transfer = NULL; 9201da177e4SLinus Torvalds lo->ioctl = NULL; 9211da177e4SLinus Torvalds lo->lo_device = NULL; 9221da177e4SLinus Torvalds lo->lo_encryption = NULL; 9231da177e4SLinus Torvalds lo->lo_offset = 0; 9241da177e4SLinus Torvalds lo->lo_sizelimit = 0; 9251da177e4SLinus Torvalds lo->lo_encrypt_key_size = 0; 9261da177e4SLinus Torvalds lo->lo_flags = 0; 9271da177e4SLinus Torvalds memset(lo->lo_encrypt_key, 0, LO_KEY_SIZE); 9281da177e4SLinus Torvalds memset(lo->lo_crypt_name, 0, LO_NAME_SIZE); 9291da177e4SLinus Torvalds memset(lo->lo_file_name, 0, LO_NAME_SIZE); 9301da177e4SLinus Torvalds invalidate_bdev(bdev, 0); 9311da177e4SLinus Torvalds set_capacity(disks[lo->lo_number], 0); 9321da177e4SLinus Torvalds bd_set_size(bdev, 0); 9331da177e4SLinus Torvalds mapping_set_gfp_mask(filp->f_mapping, gfp); 9341da177e4SLinus Torvalds lo->lo_state = Lo_unbound; 9351da177e4SLinus Torvalds fput(filp); 9361da177e4SLinus Torvalds /* This is safe: open() is still holding a reference. */ 9371da177e4SLinus Torvalds module_put(THIS_MODULE); 9381da177e4SLinus Torvalds return 0; 9391da177e4SLinus Torvalds } 9401da177e4SLinus Torvalds 9411da177e4SLinus Torvalds static int 9421da177e4SLinus Torvalds loop_set_status(struct loop_device *lo, const struct loop_info64 *info) 9431da177e4SLinus Torvalds { 9441da177e4SLinus Torvalds int err; 9451da177e4SLinus Torvalds struct loop_func_table *xfer; 9461da177e4SLinus Torvalds 9471da177e4SLinus Torvalds if (lo->lo_encrypt_key_size && lo->lo_key_owner != current->uid && 9481da177e4SLinus Torvalds !capable(CAP_SYS_ADMIN)) 9491da177e4SLinus Torvalds return -EPERM; 9501da177e4SLinus Torvalds if (lo->lo_state != Lo_bound) 9511da177e4SLinus Torvalds return -ENXIO; 9521da177e4SLinus Torvalds if ((unsigned int) info->lo_encrypt_key_size > LO_KEY_SIZE) 9531da177e4SLinus Torvalds return -EINVAL; 9541da177e4SLinus Torvalds 9551da177e4SLinus Torvalds err = loop_release_xfer(lo); 9561da177e4SLinus Torvalds if (err) 9571da177e4SLinus Torvalds return err; 9581da177e4SLinus Torvalds 9591da177e4SLinus Torvalds if (info->lo_encrypt_type) { 9601da177e4SLinus Torvalds unsigned int type = info->lo_encrypt_type; 9611da177e4SLinus Torvalds 9621da177e4SLinus Torvalds if (type >= MAX_LO_CRYPT) 9631da177e4SLinus Torvalds return -EINVAL; 9641da177e4SLinus Torvalds xfer = xfer_funcs[type]; 9651da177e4SLinus Torvalds if (xfer == NULL) 9661da177e4SLinus Torvalds return -EINVAL; 9671da177e4SLinus Torvalds } else 9681da177e4SLinus Torvalds xfer = NULL; 9691da177e4SLinus Torvalds 9701da177e4SLinus Torvalds err = loop_init_xfer(lo, xfer, info); 9711da177e4SLinus Torvalds if (err) 9721da177e4SLinus Torvalds return err; 9731da177e4SLinus Torvalds 9741da177e4SLinus Torvalds if (lo->lo_offset != info->lo_offset || 9751da177e4SLinus Torvalds lo->lo_sizelimit != info->lo_sizelimit) { 9761da177e4SLinus Torvalds lo->lo_offset = info->lo_offset; 9771da177e4SLinus Torvalds lo->lo_sizelimit = info->lo_sizelimit; 9781da177e4SLinus Torvalds if (figure_loop_size(lo)) 9791da177e4SLinus Torvalds return -EFBIG; 9801da177e4SLinus Torvalds } 9811da177e4SLinus Torvalds 9821da177e4SLinus Torvalds memcpy(lo->lo_file_name, info->lo_file_name, LO_NAME_SIZE); 9831da177e4SLinus Torvalds memcpy(lo->lo_crypt_name, info->lo_crypt_name, LO_NAME_SIZE); 9841da177e4SLinus Torvalds lo->lo_file_name[LO_NAME_SIZE-1] = 0; 9851da177e4SLinus Torvalds lo->lo_crypt_name[LO_NAME_SIZE-1] = 0; 9861da177e4SLinus Torvalds 9871da177e4SLinus Torvalds if (!xfer) 9881da177e4SLinus Torvalds xfer = &none_funcs; 9891da177e4SLinus Torvalds lo->transfer = xfer->transfer; 9901da177e4SLinus Torvalds lo->ioctl = xfer->ioctl; 9911da177e4SLinus Torvalds 9921da177e4SLinus Torvalds lo->lo_encrypt_key_size = info->lo_encrypt_key_size; 9931da177e4SLinus Torvalds lo->lo_init[0] = info->lo_init[0]; 9941da177e4SLinus Torvalds lo->lo_init[1] = info->lo_init[1]; 9951da177e4SLinus Torvalds if (info->lo_encrypt_key_size) { 9961da177e4SLinus Torvalds memcpy(lo->lo_encrypt_key, info->lo_encrypt_key, 9971da177e4SLinus Torvalds info->lo_encrypt_key_size); 9981da177e4SLinus Torvalds lo->lo_key_owner = current->uid; 9991da177e4SLinus Torvalds } 10001da177e4SLinus Torvalds 10011da177e4SLinus Torvalds return 0; 10021da177e4SLinus Torvalds } 10031da177e4SLinus Torvalds 10041da177e4SLinus Torvalds static int 10051da177e4SLinus Torvalds loop_get_status(struct loop_device *lo, struct loop_info64 *info) 10061da177e4SLinus Torvalds { 10071da177e4SLinus Torvalds struct file *file = lo->lo_backing_file; 10081da177e4SLinus Torvalds struct kstat stat; 10091da177e4SLinus Torvalds int error; 10101da177e4SLinus Torvalds 10111da177e4SLinus Torvalds if (lo->lo_state != Lo_bound) 10121da177e4SLinus Torvalds return -ENXIO; 10131da177e4SLinus Torvalds error = vfs_getattr(file->f_vfsmnt, file->f_dentry, &stat); 10141da177e4SLinus Torvalds if (error) 10151da177e4SLinus Torvalds return error; 10161da177e4SLinus Torvalds memset(info, 0, sizeof(*info)); 10171da177e4SLinus Torvalds info->lo_number = lo->lo_number; 10181da177e4SLinus Torvalds info->lo_device = huge_encode_dev(stat.dev); 10191da177e4SLinus Torvalds info->lo_inode = stat.ino; 10201da177e4SLinus Torvalds info->lo_rdevice = huge_encode_dev(lo->lo_device ? stat.rdev : stat.dev); 10211da177e4SLinus Torvalds info->lo_offset = lo->lo_offset; 10221da177e4SLinus Torvalds info->lo_sizelimit = lo->lo_sizelimit; 10231da177e4SLinus Torvalds info->lo_flags = lo->lo_flags; 10241da177e4SLinus Torvalds memcpy(info->lo_file_name, lo->lo_file_name, LO_NAME_SIZE); 10251da177e4SLinus Torvalds memcpy(info->lo_crypt_name, lo->lo_crypt_name, LO_NAME_SIZE); 10261da177e4SLinus Torvalds info->lo_encrypt_type = 10271da177e4SLinus Torvalds lo->lo_encryption ? lo->lo_encryption->number : 0; 10281da177e4SLinus Torvalds if (lo->lo_encrypt_key_size && capable(CAP_SYS_ADMIN)) { 10291da177e4SLinus Torvalds info->lo_encrypt_key_size = lo->lo_encrypt_key_size; 10301da177e4SLinus Torvalds memcpy(info->lo_encrypt_key, lo->lo_encrypt_key, 10311da177e4SLinus Torvalds lo->lo_encrypt_key_size); 10321da177e4SLinus Torvalds } 10331da177e4SLinus Torvalds return 0; 10341da177e4SLinus Torvalds } 10351da177e4SLinus Torvalds 10361da177e4SLinus Torvalds static void 10371da177e4SLinus Torvalds loop_info64_from_old(const struct loop_info *info, struct loop_info64 *info64) 10381da177e4SLinus Torvalds { 10391da177e4SLinus Torvalds memset(info64, 0, sizeof(*info64)); 10401da177e4SLinus Torvalds info64->lo_number = info->lo_number; 10411da177e4SLinus Torvalds info64->lo_device = info->lo_device; 10421da177e4SLinus Torvalds info64->lo_inode = info->lo_inode; 10431da177e4SLinus Torvalds info64->lo_rdevice = info->lo_rdevice; 10441da177e4SLinus Torvalds info64->lo_offset = info->lo_offset; 10451da177e4SLinus Torvalds info64->lo_sizelimit = 0; 10461da177e4SLinus Torvalds info64->lo_encrypt_type = info->lo_encrypt_type; 10471da177e4SLinus Torvalds info64->lo_encrypt_key_size = info->lo_encrypt_key_size; 10481da177e4SLinus Torvalds info64->lo_flags = info->lo_flags; 10491da177e4SLinus Torvalds info64->lo_init[0] = info->lo_init[0]; 10501da177e4SLinus Torvalds info64->lo_init[1] = info->lo_init[1]; 10511da177e4SLinus Torvalds if (info->lo_encrypt_type == LO_CRYPT_CRYPTOAPI) 10521da177e4SLinus Torvalds memcpy(info64->lo_crypt_name, info->lo_name, LO_NAME_SIZE); 10531da177e4SLinus Torvalds else 10541da177e4SLinus Torvalds memcpy(info64->lo_file_name, info->lo_name, LO_NAME_SIZE); 10551da177e4SLinus Torvalds memcpy(info64->lo_encrypt_key, info->lo_encrypt_key, LO_KEY_SIZE); 10561da177e4SLinus Torvalds } 10571da177e4SLinus Torvalds 10581da177e4SLinus Torvalds static int 10591da177e4SLinus Torvalds loop_info64_to_old(const struct loop_info64 *info64, struct loop_info *info) 10601da177e4SLinus Torvalds { 10611da177e4SLinus Torvalds memset(info, 0, sizeof(*info)); 10621da177e4SLinus Torvalds info->lo_number = info64->lo_number; 10631da177e4SLinus Torvalds info->lo_device = info64->lo_device; 10641da177e4SLinus Torvalds info->lo_inode = info64->lo_inode; 10651da177e4SLinus Torvalds info->lo_rdevice = info64->lo_rdevice; 10661da177e4SLinus Torvalds info->lo_offset = info64->lo_offset; 10671da177e4SLinus Torvalds info->lo_encrypt_type = info64->lo_encrypt_type; 10681da177e4SLinus Torvalds info->lo_encrypt_key_size = info64->lo_encrypt_key_size; 10691da177e4SLinus Torvalds info->lo_flags = info64->lo_flags; 10701da177e4SLinus Torvalds info->lo_init[0] = info64->lo_init[0]; 10711da177e4SLinus Torvalds info->lo_init[1] = info64->lo_init[1]; 10721da177e4SLinus Torvalds if (info->lo_encrypt_type == LO_CRYPT_CRYPTOAPI) 10731da177e4SLinus Torvalds memcpy(info->lo_name, info64->lo_crypt_name, LO_NAME_SIZE); 10741da177e4SLinus Torvalds else 10751da177e4SLinus Torvalds memcpy(info->lo_name, info64->lo_file_name, LO_NAME_SIZE); 10761da177e4SLinus Torvalds memcpy(info->lo_encrypt_key, info64->lo_encrypt_key, LO_KEY_SIZE); 10771da177e4SLinus Torvalds 10781da177e4SLinus Torvalds /* error in case values were truncated */ 10791da177e4SLinus Torvalds if (info->lo_device != info64->lo_device || 10801da177e4SLinus Torvalds info->lo_rdevice != info64->lo_rdevice || 10811da177e4SLinus Torvalds info->lo_inode != info64->lo_inode || 10821da177e4SLinus Torvalds info->lo_offset != info64->lo_offset) 10831da177e4SLinus Torvalds return -EOVERFLOW; 10841da177e4SLinus Torvalds 10851da177e4SLinus Torvalds return 0; 10861da177e4SLinus Torvalds } 10871da177e4SLinus Torvalds 10881da177e4SLinus Torvalds static int 10891da177e4SLinus Torvalds loop_set_status_old(struct loop_device *lo, const struct loop_info __user *arg) 10901da177e4SLinus Torvalds { 10911da177e4SLinus Torvalds struct loop_info info; 10921da177e4SLinus Torvalds struct loop_info64 info64; 10931da177e4SLinus Torvalds 10941da177e4SLinus Torvalds if (copy_from_user(&info, arg, sizeof (struct loop_info))) 10951da177e4SLinus Torvalds return -EFAULT; 10961da177e4SLinus Torvalds loop_info64_from_old(&info, &info64); 10971da177e4SLinus Torvalds return loop_set_status(lo, &info64); 10981da177e4SLinus Torvalds } 10991da177e4SLinus Torvalds 11001da177e4SLinus Torvalds static int 11011da177e4SLinus Torvalds loop_set_status64(struct loop_device *lo, const struct loop_info64 __user *arg) 11021da177e4SLinus Torvalds { 11031da177e4SLinus Torvalds struct loop_info64 info64; 11041da177e4SLinus Torvalds 11051da177e4SLinus Torvalds if (copy_from_user(&info64, arg, sizeof (struct loop_info64))) 11061da177e4SLinus Torvalds return -EFAULT; 11071da177e4SLinus Torvalds return loop_set_status(lo, &info64); 11081da177e4SLinus Torvalds } 11091da177e4SLinus Torvalds 11101da177e4SLinus Torvalds static int 11111da177e4SLinus Torvalds loop_get_status_old(struct loop_device *lo, struct loop_info __user *arg) { 11121da177e4SLinus Torvalds struct loop_info info; 11131da177e4SLinus Torvalds struct loop_info64 info64; 11141da177e4SLinus Torvalds int err = 0; 11151da177e4SLinus Torvalds 11161da177e4SLinus Torvalds if (!arg) 11171da177e4SLinus Torvalds err = -EINVAL; 11181da177e4SLinus Torvalds if (!err) 11191da177e4SLinus Torvalds err = loop_get_status(lo, &info64); 11201da177e4SLinus Torvalds if (!err) 11211da177e4SLinus Torvalds err = loop_info64_to_old(&info64, &info); 11221da177e4SLinus Torvalds if (!err && copy_to_user(arg, &info, sizeof(info))) 11231da177e4SLinus Torvalds err = -EFAULT; 11241da177e4SLinus Torvalds 11251da177e4SLinus Torvalds return err; 11261da177e4SLinus Torvalds } 11271da177e4SLinus Torvalds 11281da177e4SLinus Torvalds static int 11291da177e4SLinus Torvalds loop_get_status64(struct loop_device *lo, struct loop_info64 __user *arg) { 11301da177e4SLinus Torvalds struct loop_info64 info64; 11311da177e4SLinus Torvalds int err = 0; 11321da177e4SLinus Torvalds 11331da177e4SLinus Torvalds if (!arg) 11341da177e4SLinus Torvalds err = -EINVAL; 11351da177e4SLinus Torvalds if (!err) 11361da177e4SLinus Torvalds err = loop_get_status(lo, &info64); 11371da177e4SLinus Torvalds if (!err && copy_to_user(arg, &info64, sizeof(info64))) 11381da177e4SLinus Torvalds err = -EFAULT; 11391da177e4SLinus Torvalds 11401da177e4SLinus Torvalds return err; 11411da177e4SLinus Torvalds } 11421da177e4SLinus Torvalds 11431da177e4SLinus Torvalds static int lo_ioctl(struct inode * inode, struct file * file, 11441da177e4SLinus Torvalds unsigned int cmd, unsigned long arg) 11451da177e4SLinus Torvalds { 11461da177e4SLinus Torvalds struct loop_device *lo = inode->i_bdev->bd_disk->private_data; 11471da177e4SLinus Torvalds int err; 11481da177e4SLinus Torvalds 1149f85221ddSIngo Molnar mutex_lock(&lo->lo_ctl_mutex); 11501da177e4SLinus Torvalds switch (cmd) { 11511da177e4SLinus Torvalds case LOOP_SET_FD: 11521da177e4SLinus Torvalds err = loop_set_fd(lo, file, inode->i_bdev, arg); 11531da177e4SLinus Torvalds break; 11541da177e4SLinus Torvalds case LOOP_CHANGE_FD: 11551da177e4SLinus Torvalds err = loop_change_fd(lo, file, inode->i_bdev, arg); 11561da177e4SLinus Torvalds break; 11571da177e4SLinus Torvalds case LOOP_CLR_FD: 11581da177e4SLinus Torvalds err = loop_clr_fd(lo, inode->i_bdev); 11591da177e4SLinus Torvalds break; 11601da177e4SLinus Torvalds case LOOP_SET_STATUS: 11611da177e4SLinus Torvalds err = loop_set_status_old(lo, (struct loop_info __user *) arg); 11621da177e4SLinus Torvalds break; 11631da177e4SLinus Torvalds case LOOP_GET_STATUS: 11641da177e4SLinus Torvalds err = loop_get_status_old(lo, (struct loop_info __user *) arg); 11651da177e4SLinus Torvalds break; 11661da177e4SLinus Torvalds case LOOP_SET_STATUS64: 11671da177e4SLinus Torvalds err = loop_set_status64(lo, (struct loop_info64 __user *) arg); 11681da177e4SLinus Torvalds break; 11691da177e4SLinus Torvalds case LOOP_GET_STATUS64: 11701da177e4SLinus Torvalds err = loop_get_status64(lo, (struct loop_info64 __user *) arg); 11711da177e4SLinus Torvalds break; 11721da177e4SLinus Torvalds default: 11731da177e4SLinus Torvalds err = lo->ioctl ? lo->ioctl(lo, cmd, arg) : -EINVAL; 11741da177e4SLinus Torvalds } 1175f85221ddSIngo Molnar mutex_unlock(&lo->lo_ctl_mutex); 11761da177e4SLinus Torvalds return err; 11771da177e4SLinus Torvalds } 11781da177e4SLinus Torvalds 11791da177e4SLinus Torvalds static int lo_open(struct inode *inode, struct file *file) 11801da177e4SLinus Torvalds { 11811da177e4SLinus Torvalds struct loop_device *lo = inode->i_bdev->bd_disk->private_data; 11821da177e4SLinus Torvalds 1183f85221ddSIngo Molnar mutex_lock(&lo->lo_ctl_mutex); 11841da177e4SLinus Torvalds lo->lo_refcnt++; 1185f85221ddSIngo Molnar mutex_unlock(&lo->lo_ctl_mutex); 11861da177e4SLinus Torvalds 11871da177e4SLinus Torvalds return 0; 11881da177e4SLinus Torvalds } 11891da177e4SLinus Torvalds 11901da177e4SLinus Torvalds static int lo_release(struct inode *inode, struct file *file) 11911da177e4SLinus Torvalds { 11921da177e4SLinus Torvalds struct loop_device *lo = inode->i_bdev->bd_disk->private_data; 11931da177e4SLinus Torvalds 1194f85221ddSIngo Molnar mutex_lock(&lo->lo_ctl_mutex); 11951da177e4SLinus Torvalds --lo->lo_refcnt; 1196f85221ddSIngo Molnar mutex_unlock(&lo->lo_ctl_mutex); 11971da177e4SLinus Torvalds 11981da177e4SLinus Torvalds return 0; 11991da177e4SLinus Torvalds } 12001da177e4SLinus Torvalds 12011da177e4SLinus Torvalds static struct block_device_operations lo_fops = { 12021da177e4SLinus Torvalds .owner = THIS_MODULE, 12031da177e4SLinus Torvalds .open = lo_open, 12041da177e4SLinus Torvalds .release = lo_release, 12051da177e4SLinus Torvalds .ioctl = lo_ioctl, 12061da177e4SLinus Torvalds }; 12071da177e4SLinus Torvalds 12081da177e4SLinus Torvalds /* 12091da177e4SLinus Torvalds * And now the modules code and kernel interface. 12101da177e4SLinus Torvalds */ 12111da177e4SLinus Torvalds module_param(max_loop, int, 0); 12121da177e4SLinus Torvalds MODULE_PARM_DESC(max_loop, "Maximum number of loop devices (1-256)"); 12131da177e4SLinus Torvalds MODULE_LICENSE("GPL"); 12141da177e4SLinus Torvalds MODULE_ALIAS_BLOCKDEV_MAJOR(LOOP_MAJOR); 12151da177e4SLinus Torvalds 12161da177e4SLinus Torvalds int loop_register_transfer(struct loop_func_table *funcs) 12171da177e4SLinus Torvalds { 12181da177e4SLinus Torvalds unsigned int n = funcs->number; 12191da177e4SLinus Torvalds 12201da177e4SLinus Torvalds if (n >= MAX_LO_CRYPT || xfer_funcs[n]) 12211da177e4SLinus Torvalds return -EINVAL; 12221da177e4SLinus Torvalds xfer_funcs[n] = funcs; 12231da177e4SLinus Torvalds return 0; 12241da177e4SLinus Torvalds } 12251da177e4SLinus Torvalds 12261da177e4SLinus Torvalds int loop_unregister_transfer(int number) 12271da177e4SLinus Torvalds { 12281da177e4SLinus Torvalds unsigned int n = number; 12291da177e4SLinus Torvalds struct loop_device *lo; 12301da177e4SLinus Torvalds struct loop_func_table *xfer; 12311da177e4SLinus Torvalds 12321da177e4SLinus Torvalds if (n == 0 || n >= MAX_LO_CRYPT || (xfer = xfer_funcs[n]) == NULL) 12331da177e4SLinus Torvalds return -EINVAL; 12341da177e4SLinus Torvalds 12351da177e4SLinus Torvalds xfer_funcs[n] = NULL; 12361da177e4SLinus Torvalds 12371da177e4SLinus Torvalds for (lo = &loop_dev[0]; lo < &loop_dev[max_loop]; lo++) { 1238f85221ddSIngo Molnar mutex_lock(&lo->lo_ctl_mutex); 12391da177e4SLinus Torvalds 12401da177e4SLinus Torvalds if (lo->lo_encryption == xfer) 12411da177e4SLinus Torvalds loop_release_xfer(lo); 12421da177e4SLinus Torvalds 1243f85221ddSIngo Molnar mutex_unlock(&lo->lo_ctl_mutex); 12441da177e4SLinus Torvalds } 12451da177e4SLinus Torvalds 12461da177e4SLinus Torvalds return 0; 12471da177e4SLinus Torvalds } 12481da177e4SLinus Torvalds 12491da177e4SLinus Torvalds EXPORT_SYMBOL(loop_register_transfer); 12501da177e4SLinus Torvalds EXPORT_SYMBOL(loop_unregister_transfer); 12511da177e4SLinus Torvalds 12521da177e4SLinus Torvalds static int __init loop_init(void) 12531da177e4SLinus Torvalds { 12541da177e4SLinus Torvalds int i; 12551da177e4SLinus Torvalds 12561da177e4SLinus Torvalds if (max_loop < 1 || max_loop > 256) { 12571da177e4SLinus Torvalds printk(KERN_WARNING "loop: invalid max_loop (must be between" 12581da177e4SLinus Torvalds " 1 and 256), using default (8)\n"); 12591da177e4SLinus Torvalds max_loop = 8; 12601da177e4SLinus Torvalds } 12611da177e4SLinus Torvalds 12621da177e4SLinus Torvalds if (register_blkdev(LOOP_MAJOR, "loop")) 12631da177e4SLinus Torvalds return -EIO; 12641da177e4SLinus Torvalds 12651da177e4SLinus Torvalds loop_dev = kmalloc(max_loop * sizeof(struct loop_device), GFP_KERNEL); 12661da177e4SLinus Torvalds if (!loop_dev) 12671da177e4SLinus Torvalds goto out_mem1; 12681da177e4SLinus Torvalds memset(loop_dev, 0, max_loop * sizeof(struct loop_device)); 12691da177e4SLinus Torvalds 12701da177e4SLinus Torvalds disks = kmalloc(max_loop * sizeof(struct gendisk *), GFP_KERNEL); 12711da177e4SLinus Torvalds if (!disks) 12721da177e4SLinus Torvalds goto out_mem2; 12731da177e4SLinus Torvalds 12741da177e4SLinus Torvalds for (i = 0; i < max_loop; i++) { 12751da177e4SLinus Torvalds disks[i] = alloc_disk(1); 12761da177e4SLinus Torvalds if (!disks[i]) 12771da177e4SLinus Torvalds goto out_mem3; 12781da177e4SLinus Torvalds } 12791da177e4SLinus Torvalds 12801da177e4SLinus Torvalds devfs_mk_dir("loop"); 12811da177e4SLinus Torvalds 12821da177e4SLinus Torvalds for (i = 0; i < max_loop; i++) { 12831da177e4SLinus Torvalds struct loop_device *lo = &loop_dev[i]; 12841da177e4SLinus Torvalds struct gendisk *disk = disks[i]; 12851da177e4SLinus Torvalds 12861da177e4SLinus Torvalds memset(lo, 0, sizeof(*lo)); 12871da177e4SLinus Torvalds lo->lo_queue = blk_alloc_queue(GFP_KERNEL); 12881da177e4SLinus Torvalds if (!lo->lo_queue) 12891da177e4SLinus Torvalds goto out_mem4; 1290f85221ddSIngo Molnar mutex_init(&lo->lo_ctl_mutex); 129109c0dc68SLinus Torvalds init_completion(&lo->lo_done); 129211b751aeSIngo Molnar init_completion(&lo->lo_bh_done); 12931da177e4SLinus Torvalds lo->lo_number = i; 12941da177e4SLinus Torvalds spin_lock_init(&lo->lo_lock); 12951da177e4SLinus Torvalds disk->major = LOOP_MAJOR; 12961da177e4SLinus Torvalds disk->first_minor = i; 12971da177e4SLinus Torvalds disk->fops = &lo_fops; 12981da177e4SLinus Torvalds sprintf(disk->disk_name, "loop%d", i); 12991da177e4SLinus Torvalds sprintf(disk->devfs_name, "loop/%d", i); 13001da177e4SLinus Torvalds disk->private_data = lo; 13011da177e4SLinus Torvalds disk->queue = lo->lo_queue; 13021da177e4SLinus Torvalds } 13031da177e4SLinus Torvalds 13041da177e4SLinus Torvalds /* We cannot fail after we call this, so another loop!*/ 13051da177e4SLinus Torvalds for (i = 0; i < max_loop; i++) 13061da177e4SLinus Torvalds add_disk(disks[i]); 13071da177e4SLinus Torvalds printk(KERN_INFO "loop: loaded (max %d devices)\n", max_loop); 13081da177e4SLinus Torvalds return 0; 13091da177e4SLinus Torvalds 13101da177e4SLinus Torvalds out_mem4: 13111da177e4SLinus Torvalds while (i--) 13121312f40eSAl Viro blk_cleanup_queue(loop_dev[i].lo_queue); 13131da177e4SLinus Torvalds devfs_remove("loop"); 13141da177e4SLinus Torvalds i = max_loop; 13151da177e4SLinus Torvalds out_mem3: 13161da177e4SLinus Torvalds while (i--) 13171da177e4SLinus Torvalds put_disk(disks[i]); 13181da177e4SLinus Torvalds kfree(disks); 13191da177e4SLinus Torvalds out_mem2: 13201da177e4SLinus Torvalds kfree(loop_dev); 13211da177e4SLinus Torvalds out_mem1: 13221da177e4SLinus Torvalds unregister_blkdev(LOOP_MAJOR, "loop"); 13231da177e4SLinus Torvalds printk(KERN_ERR "loop: ran out of memory\n"); 13241da177e4SLinus Torvalds return -ENOMEM; 13251da177e4SLinus Torvalds } 13261da177e4SLinus Torvalds 13271da177e4SLinus Torvalds static void loop_exit(void) 13281da177e4SLinus Torvalds { 13291da177e4SLinus Torvalds int i; 13301da177e4SLinus Torvalds 13311da177e4SLinus Torvalds for (i = 0; i < max_loop; i++) { 13321da177e4SLinus Torvalds del_gendisk(disks[i]); 13331312f40eSAl Viro blk_cleanup_queue(loop_dev[i].lo_queue); 13341da177e4SLinus Torvalds put_disk(disks[i]); 13351da177e4SLinus Torvalds } 13361da177e4SLinus Torvalds devfs_remove("loop"); 13371da177e4SLinus Torvalds if (unregister_blkdev(LOOP_MAJOR, "loop")) 13381da177e4SLinus Torvalds printk(KERN_WARNING "loop: cannot unregister blkdev\n"); 13391da177e4SLinus Torvalds 13401da177e4SLinus Torvalds kfree(disks); 13411da177e4SLinus Torvalds kfree(loop_dev); 13421da177e4SLinus Torvalds } 13431da177e4SLinus Torvalds 13441da177e4SLinus Torvalds module_init(loop_init); 13451da177e4SLinus Torvalds module_exit(loop_exit); 13461da177e4SLinus Torvalds 13471da177e4SLinus Torvalds #ifndef MODULE 13481da177e4SLinus Torvalds static int __init max_loop_setup(char *str) 13491da177e4SLinus Torvalds { 13501da177e4SLinus Torvalds max_loop = simple_strtol(str, NULL, 0); 13511da177e4SLinus Torvalds return 1; 13521da177e4SLinus Torvalds } 13531da177e4SLinus Torvalds 13541da177e4SLinus Torvalds __setup("max_loop=", max_loop_setup); 13551da177e4SLinus Torvalds #endif 1356