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. 3296de0e25SJan Engelhardt * 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 434e02ed4bSNick Piggin * operations write_begin is not available on the backing filesystem. 441da177e4SLinus Torvalds * Anton Altaparmakov, 16 Feb 2005 451da177e4SLinus Torvalds * 461da177e4SLinus Torvalds * Still To Fix: 471da177e4SLinus Torvalds * - Advisory locking is ignored here. 481da177e4SLinus Torvalds * - Should use an own CAP_* category instead of CAP_SYS_ADMIN 491da177e4SLinus Torvalds * 501da177e4SLinus Torvalds */ 511da177e4SLinus Torvalds 521da177e4SLinus Torvalds #include <linux/module.h> 531da177e4SLinus Torvalds #include <linux/moduleparam.h> 541da177e4SLinus Torvalds #include <linux/sched.h> 551da177e4SLinus Torvalds #include <linux/fs.h> 561da177e4SLinus Torvalds #include <linux/file.h> 571da177e4SLinus Torvalds #include <linux/stat.h> 581da177e4SLinus Torvalds #include <linux/errno.h> 591da177e4SLinus Torvalds #include <linux/major.h> 601da177e4SLinus Torvalds #include <linux/wait.h> 611da177e4SLinus Torvalds #include <linux/blkdev.h> 621da177e4SLinus Torvalds #include <linux/blkpg.h> 631da177e4SLinus Torvalds #include <linux/init.h> 641da177e4SLinus Torvalds #include <linux/swap.h> 651da177e4SLinus Torvalds #include <linux/slab.h> 661da177e4SLinus Torvalds #include <linux/loop.h> 67863d5b82SDavid Howells #include <linux/compat.h> 681da177e4SLinus Torvalds #include <linux/suspend.h> 6983144186SRafael J. Wysocki #include <linux/freezer.h> 702a48fc0aSArnd Bergmann #include <linux/mutex.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> 756c997918SSerge E. Hallyn #include <linux/kthread.h> 76d6b29d7cSJens Axboe #include <linux/splice.h> 77ee862730SMilan Broz #include <linux/sysfs.h> 78*dfaa2ef6SLukas Czerner #include <linux/falloc.h> 791da177e4SLinus Torvalds 801da177e4SLinus Torvalds #include <asm/uaccess.h> 811da177e4SLinus Torvalds 8273285082SKen Chen static LIST_HEAD(loop_devices); 8373285082SKen Chen static DEFINE_MUTEX(loop_devices_mutex); 841da177e4SLinus Torvalds 85476a4813SLaurent Vivier static int max_part; 86476a4813SLaurent Vivier static int part_shift; 87476a4813SLaurent Vivier 881da177e4SLinus Torvalds /* 891da177e4SLinus Torvalds * Transfer functions 901da177e4SLinus Torvalds */ 911da177e4SLinus Torvalds static int transfer_none(struct loop_device *lo, int cmd, 921da177e4SLinus Torvalds struct page *raw_page, unsigned raw_off, 931da177e4SLinus Torvalds struct page *loop_page, unsigned loop_off, 941da177e4SLinus Torvalds int size, sector_t real_block) 951da177e4SLinus Torvalds { 961da177e4SLinus Torvalds char *raw_buf = kmap_atomic(raw_page, KM_USER0) + raw_off; 971da177e4SLinus Torvalds char *loop_buf = kmap_atomic(loop_page, KM_USER1) + loop_off; 981da177e4SLinus Torvalds 991da177e4SLinus Torvalds if (cmd == READ) 1001da177e4SLinus Torvalds memcpy(loop_buf, raw_buf, size); 1011da177e4SLinus Torvalds else 1021da177e4SLinus Torvalds memcpy(raw_buf, loop_buf, size); 1031da177e4SLinus Torvalds 1041da177e4SLinus Torvalds kunmap_atomic(loop_buf, KM_USER1); 10561ecdb80SPeter Zijlstra kunmap_atomic(raw_buf, KM_USER0); 1061da177e4SLinus Torvalds cond_resched(); 1071da177e4SLinus Torvalds return 0; 1081da177e4SLinus Torvalds } 1091da177e4SLinus Torvalds 1101da177e4SLinus Torvalds static int transfer_xor(struct loop_device *lo, int cmd, 1111da177e4SLinus Torvalds struct page *raw_page, unsigned raw_off, 1121da177e4SLinus Torvalds struct page *loop_page, unsigned loop_off, 1131da177e4SLinus Torvalds int size, sector_t real_block) 1141da177e4SLinus Torvalds { 1151da177e4SLinus Torvalds char *raw_buf = kmap_atomic(raw_page, KM_USER0) + raw_off; 1161da177e4SLinus Torvalds char *loop_buf = kmap_atomic(loop_page, KM_USER1) + loop_off; 1171da177e4SLinus Torvalds char *in, *out, *key; 1181da177e4SLinus Torvalds int i, keysize; 1191da177e4SLinus Torvalds 1201da177e4SLinus Torvalds if (cmd == READ) { 1211da177e4SLinus Torvalds in = raw_buf; 1221da177e4SLinus Torvalds out = loop_buf; 1231da177e4SLinus Torvalds } else { 1241da177e4SLinus Torvalds in = loop_buf; 1251da177e4SLinus Torvalds out = raw_buf; 1261da177e4SLinus Torvalds } 1271da177e4SLinus Torvalds 1281da177e4SLinus Torvalds key = lo->lo_encrypt_key; 1291da177e4SLinus Torvalds keysize = lo->lo_encrypt_key_size; 1301da177e4SLinus Torvalds for (i = 0; i < size; i++) 1311da177e4SLinus Torvalds *out++ = *in++ ^ key[(i & 511) % keysize]; 1321da177e4SLinus Torvalds 1331da177e4SLinus Torvalds kunmap_atomic(loop_buf, KM_USER1); 13461ecdb80SPeter Zijlstra kunmap_atomic(raw_buf, KM_USER0); 1351da177e4SLinus Torvalds cond_resched(); 1361da177e4SLinus Torvalds return 0; 1371da177e4SLinus Torvalds } 1381da177e4SLinus Torvalds 1391da177e4SLinus Torvalds static int xor_init(struct loop_device *lo, const struct loop_info64 *info) 1401da177e4SLinus Torvalds { 1411da177e4SLinus Torvalds if (unlikely(info->lo_encrypt_key_size <= 0)) 1421da177e4SLinus Torvalds return -EINVAL; 1431da177e4SLinus Torvalds return 0; 1441da177e4SLinus Torvalds } 1451da177e4SLinus Torvalds 1461da177e4SLinus Torvalds static struct loop_func_table none_funcs = { 1471da177e4SLinus Torvalds .number = LO_CRYPT_NONE, 1481da177e4SLinus Torvalds .transfer = transfer_none, 1491da177e4SLinus Torvalds }; 1501da177e4SLinus Torvalds 1511da177e4SLinus Torvalds static struct loop_func_table xor_funcs = { 1521da177e4SLinus Torvalds .number = LO_CRYPT_XOR, 1531da177e4SLinus Torvalds .transfer = transfer_xor, 1541da177e4SLinus Torvalds .init = xor_init 1551da177e4SLinus Torvalds }; 1561da177e4SLinus Torvalds 1571da177e4SLinus Torvalds /* xfer_funcs[0] is special - its release function is never called */ 1581da177e4SLinus Torvalds static struct loop_func_table *xfer_funcs[MAX_LO_CRYPT] = { 1591da177e4SLinus Torvalds &none_funcs, 1601da177e4SLinus Torvalds &xor_funcs 1611da177e4SLinus Torvalds }; 1621da177e4SLinus Torvalds 1631da177e4SLinus Torvalds static loff_t get_loop_size(struct loop_device *lo, struct file *file) 1641da177e4SLinus Torvalds { 1651da177e4SLinus Torvalds loff_t size, offset, loopsize; 1661da177e4SLinus Torvalds 1671da177e4SLinus Torvalds /* Compute loopsize in bytes */ 1681da177e4SLinus Torvalds size = i_size_read(file->f_mapping->host); 1691da177e4SLinus Torvalds offset = lo->lo_offset; 1701da177e4SLinus Torvalds loopsize = size - offset; 1711da177e4SLinus Torvalds if (lo->lo_sizelimit > 0 && lo->lo_sizelimit < loopsize) 1721da177e4SLinus Torvalds loopsize = lo->lo_sizelimit; 1731da177e4SLinus Torvalds 1741da177e4SLinus Torvalds /* 1751da177e4SLinus Torvalds * Unfortunately, if we want to do I/O on the device, 1761da177e4SLinus Torvalds * the number of 512-byte sectors has to fit into a sector_t. 1771da177e4SLinus Torvalds */ 1781da177e4SLinus Torvalds return loopsize >> 9; 1791da177e4SLinus Torvalds } 1801da177e4SLinus Torvalds 1811da177e4SLinus Torvalds static int 1821da177e4SLinus Torvalds figure_loop_size(struct loop_device *lo) 1831da177e4SLinus Torvalds { 1841da177e4SLinus Torvalds loff_t size = get_loop_size(lo, lo->lo_backing_file); 1851da177e4SLinus Torvalds sector_t x = (sector_t)size; 1861da177e4SLinus Torvalds 1871da177e4SLinus Torvalds if (unlikely((loff_t)x != size)) 1881da177e4SLinus Torvalds return -EFBIG; 1891da177e4SLinus Torvalds 19073285082SKen Chen set_capacity(lo->lo_disk, x); 1911da177e4SLinus Torvalds return 0; 1921da177e4SLinus Torvalds } 1931da177e4SLinus Torvalds 1941da177e4SLinus Torvalds static inline int 1951da177e4SLinus Torvalds lo_do_transfer(struct loop_device *lo, int cmd, 1961da177e4SLinus Torvalds struct page *rpage, unsigned roffs, 1971da177e4SLinus Torvalds struct page *lpage, unsigned loffs, 1981da177e4SLinus Torvalds int size, sector_t rblock) 1991da177e4SLinus Torvalds { 2001da177e4SLinus Torvalds if (unlikely(!lo->transfer)) 2011da177e4SLinus Torvalds return 0; 2021da177e4SLinus Torvalds 2031da177e4SLinus Torvalds return lo->transfer(lo, cmd, rpage, roffs, lpage, loffs, size, rblock); 2041da177e4SLinus Torvalds } 2051da177e4SLinus Torvalds 2061da177e4SLinus Torvalds /** 2071da177e4SLinus Torvalds * do_lo_send_aops - helper for writing data to a loop device 2081da177e4SLinus Torvalds * 2091da177e4SLinus Torvalds * This is the fast version for backing filesystems which implement the address 210afddba49SNick Piggin * space operations write_begin and write_end. 2111da177e4SLinus Torvalds */ 2121da177e4SLinus Torvalds static int do_lo_send_aops(struct loop_device *lo, struct bio_vec *bvec, 213511de73fSAl Viro loff_t pos, struct page *unused) 2141da177e4SLinus Torvalds { 2151da177e4SLinus Torvalds struct file *file = lo->lo_backing_file; /* kudos to NFsckingS */ 2161da177e4SLinus Torvalds struct address_space *mapping = file->f_mapping; 2171da177e4SLinus Torvalds pgoff_t index; 2181da177e4SLinus Torvalds unsigned offset, bv_offs; 219994fc28cSZach Brown int len, ret; 2201da177e4SLinus Torvalds 2211b1dcc1bSJes Sorensen mutex_lock(&mapping->host->i_mutex); 2221da177e4SLinus Torvalds index = pos >> PAGE_CACHE_SHIFT; 2231da177e4SLinus Torvalds offset = pos & ((pgoff_t)PAGE_CACHE_SIZE - 1); 2241da177e4SLinus Torvalds bv_offs = bvec->bv_offset; 2251da177e4SLinus Torvalds len = bvec->bv_len; 2261da177e4SLinus Torvalds while (len > 0) { 2271da177e4SLinus Torvalds sector_t IV; 228afddba49SNick Piggin unsigned size, copied; 2291da177e4SLinus Torvalds int transfer_result; 230afddba49SNick Piggin struct page *page; 231afddba49SNick Piggin void *fsdata; 2321da177e4SLinus Torvalds 2331da177e4SLinus Torvalds IV = ((sector_t)index << (PAGE_CACHE_SHIFT - 9))+(offset >> 9); 2341da177e4SLinus Torvalds size = PAGE_CACHE_SIZE - offset; 2351da177e4SLinus Torvalds if (size > len) 2361da177e4SLinus Torvalds size = len; 237afddba49SNick Piggin 238afddba49SNick Piggin ret = pagecache_write_begin(file, mapping, pos, size, 0, 239afddba49SNick Piggin &page, &fsdata); 240afddba49SNick Piggin if (ret) 2411da177e4SLinus Torvalds goto fail; 242afddba49SNick Piggin 24302246c41SNikanth Karthikesan file_update_time(file); 24402246c41SNikanth Karthikesan 2451da177e4SLinus Torvalds transfer_result = lo_do_transfer(lo, WRITE, page, offset, 2461da177e4SLinus Torvalds bvec->bv_page, bv_offs, size, IV); 247afddba49SNick Piggin copied = size; 2481da177e4SLinus Torvalds if (unlikely(transfer_result)) 249afddba49SNick Piggin copied = 0; 250afddba49SNick Piggin 251afddba49SNick Piggin ret = pagecache_write_end(file, mapping, pos, size, copied, 252afddba49SNick Piggin page, fsdata); 2538268f5a7SDmitry Monakhov if (ret < 0 || ret != copied) 254afddba49SNick Piggin goto fail; 255afddba49SNick Piggin 256afddba49SNick Piggin if (unlikely(transfer_result)) 257afddba49SNick Piggin goto fail; 258afddba49SNick Piggin 259afddba49SNick Piggin bv_offs += copied; 260afddba49SNick Piggin len -= copied; 2611da177e4SLinus Torvalds offset = 0; 2621da177e4SLinus Torvalds index++; 263afddba49SNick Piggin pos += copied; 2641da177e4SLinus Torvalds } 265994fc28cSZach Brown ret = 0; 2661da177e4SLinus Torvalds out: 2671b1dcc1bSJes Sorensen mutex_unlock(&mapping->host->i_mutex); 2681da177e4SLinus Torvalds return ret; 2691da177e4SLinus Torvalds fail: 2701da177e4SLinus Torvalds ret = -1; 2711da177e4SLinus Torvalds goto out; 2721da177e4SLinus Torvalds } 2731da177e4SLinus Torvalds 2741da177e4SLinus Torvalds /** 2751da177e4SLinus Torvalds * __do_lo_send_write - helper for writing data to a loop device 2761da177e4SLinus Torvalds * 2771da177e4SLinus Torvalds * This helper just factors out common code between do_lo_send_direct_write() 2781da177e4SLinus Torvalds * and do_lo_send_write(). 2791da177e4SLinus Torvalds */ 280858119e1SArjan van de Ven static int __do_lo_send_write(struct file *file, 28198ae6ccdSAl Viro u8 *buf, const int len, loff_t pos) 2821da177e4SLinus Torvalds { 2831da177e4SLinus Torvalds ssize_t bw; 2841da177e4SLinus Torvalds mm_segment_t old_fs = get_fs(); 2851da177e4SLinus Torvalds 2861da177e4SLinus Torvalds set_fs(get_ds()); 2871da177e4SLinus Torvalds bw = file->f_op->write(file, buf, len, &pos); 2881da177e4SLinus Torvalds set_fs(old_fs); 2891da177e4SLinus Torvalds if (likely(bw == len)) 2901da177e4SLinus Torvalds return 0; 2911da177e4SLinus Torvalds printk(KERN_ERR "loop: Write error at byte offset %llu, length %i.\n", 2921da177e4SLinus Torvalds (unsigned long long)pos, len); 2931da177e4SLinus Torvalds if (bw >= 0) 2941da177e4SLinus Torvalds bw = -EIO; 2951da177e4SLinus Torvalds return bw; 2961da177e4SLinus Torvalds } 2971da177e4SLinus Torvalds 2981da177e4SLinus Torvalds /** 2991da177e4SLinus Torvalds * do_lo_send_direct_write - helper for writing data to a loop device 3001da177e4SLinus Torvalds * 3011da177e4SLinus Torvalds * This is the fast, non-transforming version for backing filesystems which do 302afddba49SNick Piggin * not implement the address space operations write_begin and write_end. 3031da177e4SLinus Torvalds * It uses the write file operation which should be present on all writeable 3041da177e4SLinus Torvalds * filesystems. 3051da177e4SLinus Torvalds */ 3061da177e4SLinus Torvalds static int do_lo_send_direct_write(struct loop_device *lo, 307511de73fSAl Viro struct bio_vec *bvec, loff_t pos, struct page *page) 3081da177e4SLinus Torvalds { 3091da177e4SLinus Torvalds ssize_t bw = __do_lo_send_write(lo->lo_backing_file, 31098ae6ccdSAl Viro kmap(bvec->bv_page) + bvec->bv_offset, 3111da177e4SLinus Torvalds bvec->bv_len, pos); 3121da177e4SLinus Torvalds kunmap(bvec->bv_page); 3131da177e4SLinus Torvalds cond_resched(); 3141da177e4SLinus Torvalds return bw; 3151da177e4SLinus Torvalds } 3161da177e4SLinus Torvalds 3171da177e4SLinus Torvalds /** 3181da177e4SLinus Torvalds * do_lo_send_write - helper for writing data to a loop device 3191da177e4SLinus Torvalds * 3201da177e4SLinus Torvalds * This is the slow, transforming version for filesystems which do not 321afddba49SNick Piggin * implement the address space operations write_begin and write_end. It 3221da177e4SLinus Torvalds * uses the write file operation which should be present on all writeable 3231da177e4SLinus Torvalds * filesystems. 3241da177e4SLinus Torvalds * 3251da177e4SLinus Torvalds * Using fops->write is slower than using aops->{prepare,commit}_write in the 3261da177e4SLinus Torvalds * transforming case because we need to double buffer the data as we cannot do 3271da177e4SLinus Torvalds * the transformations in place as we do not have direct access to the 3281da177e4SLinus Torvalds * destination pages of the backing file. 3291da177e4SLinus Torvalds */ 3301da177e4SLinus Torvalds static int do_lo_send_write(struct loop_device *lo, struct bio_vec *bvec, 331511de73fSAl Viro loff_t pos, struct page *page) 3321da177e4SLinus Torvalds { 3331da177e4SLinus Torvalds int ret = lo_do_transfer(lo, WRITE, page, 0, bvec->bv_page, 3341da177e4SLinus Torvalds bvec->bv_offset, bvec->bv_len, pos >> 9); 3351da177e4SLinus Torvalds if (likely(!ret)) 3361da177e4SLinus Torvalds return __do_lo_send_write(lo->lo_backing_file, 33798ae6ccdSAl Viro page_address(page), bvec->bv_len, 3381da177e4SLinus Torvalds pos); 3391da177e4SLinus Torvalds printk(KERN_ERR "loop: Transfer error at byte offset %llu, " 3401da177e4SLinus Torvalds "length %i.\n", (unsigned long long)pos, bvec->bv_len); 3411da177e4SLinus Torvalds if (ret > 0) 3421da177e4SLinus Torvalds ret = -EIO; 3431da177e4SLinus Torvalds return ret; 3441da177e4SLinus Torvalds } 3451da177e4SLinus Torvalds 346511de73fSAl Viro static int lo_send(struct loop_device *lo, struct bio *bio, loff_t pos) 3471da177e4SLinus Torvalds { 348511de73fSAl Viro int (*do_lo_send)(struct loop_device *, struct bio_vec *, loff_t, 3491da177e4SLinus Torvalds struct page *page); 3501da177e4SLinus Torvalds struct bio_vec *bvec; 3511da177e4SLinus Torvalds struct page *page = NULL; 3521da177e4SLinus Torvalds int i, ret = 0; 3531da177e4SLinus Torvalds 3541da177e4SLinus Torvalds do_lo_send = do_lo_send_aops; 3551da177e4SLinus Torvalds if (!(lo->lo_flags & LO_FLAGS_USE_AOPS)) { 3561da177e4SLinus Torvalds do_lo_send = do_lo_send_direct_write; 3571da177e4SLinus Torvalds if (lo->transfer != transfer_none) { 3581da177e4SLinus Torvalds page = alloc_page(GFP_NOIO | __GFP_HIGHMEM); 3591da177e4SLinus Torvalds if (unlikely(!page)) 3601da177e4SLinus Torvalds goto fail; 3611da177e4SLinus Torvalds kmap(page); 3621da177e4SLinus Torvalds do_lo_send = do_lo_send_write; 3631da177e4SLinus Torvalds } 3641da177e4SLinus Torvalds } 3651da177e4SLinus Torvalds bio_for_each_segment(bvec, bio, i) { 366511de73fSAl Viro ret = do_lo_send(lo, bvec, pos, page); 3671da177e4SLinus Torvalds if (ret < 0) 3681da177e4SLinus Torvalds break; 3691da177e4SLinus Torvalds pos += bvec->bv_len; 3701da177e4SLinus Torvalds } 3711da177e4SLinus Torvalds if (page) { 3721da177e4SLinus Torvalds kunmap(page); 3731da177e4SLinus Torvalds __free_page(page); 3741da177e4SLinus Torvalds } 3751da177e4SLinus Torvalds out: 3761da177e4SLinus Torvalds return ret; 3771da177e4SLinus Torvalds fail: 3781da177e4SLinus Torvalds printk(KERN_ERR "loop: Failed to allocate temporary page for write.\n"); 3791da177e4SLinus Torvalds ret = -ENOMEM; 3801da177e4SLinus Torvalds goto out; 3811da177e4SLinus Torvalds } 3821da177e4SLinus Torvalds 3831da177e4SLinus Torvalds struct lo_read_data { 3841da177e4SLinus Torvalds struct loop_device *lo; 3851da177e4SLinus Torvalds struct page *page; 3861da177e4SLinus Torvalds unsigned offset; 3871da177e4SLinus Torvalds int bsize; 3881da177e4SLinus Torvalds }; 3891da177e4SLinus Torvalds 3901da177e4SLinus Torvalds static int 391fd582140SJens Axboe lo_splice_actor(struct pipe_inode_info *pipe, struct pipe_buffer *buf, 392fd582140SJens Axboe struct splice_desc *sd) 3931da177e4SLinus Torvalds { 394fd582140SJens Axboe struct lo_read_data *p = sd->u.data; 3951da177e4SLinus Torvalds struct loop_device *lo = p->lo; 396fd582140SJens Axboe struct page *page = buf->page; 3971da177e4SLinus Torvalds sector_t IV; 3983603b8eaSJens Axboe int size; 3991da177e4SLinus Torvalds 400fd582140SJens Axboe IV = ((sector_t) page->index << (PAGE_CACHE_SHIFT - 9)) + 401fd582140SJens Axboe (buf->offset >> 9); 402fd582140SJens Axboe size = sd->len; 403fd582140SJens Axboe if (size > p->bsize) 404fd582140SJens Axboe size = p->bsize; 4051da177e4SLinus Torvalds 406fd582140SJens Axboe if (lo_do_transfer(lo, READ, page, buf->offset, p->page, p->offset, size, IV)) { 4071da177e4SLinus Torvalds printk(KERN_ERR "loop: transfer error block %ld\n", 4081da177e4SLinus Torvalds page->index); 409fd582140SJens Axboe size = -EINVAL; 4101da177e4SLinus Torvalds } 4111da177e4SLinus Torvalds 4121da177e4SLinus Torvalds flush_dcache_page(p->page); 4131da177e4SLinus Torvalds 414fd582140SJens Axboe if (size > 0) 4151da177e4SLinus Torvalds p->offset += size; 416fd582140SJens Axboe 4171da177e4SLinus Torvalds return size; 4181da177e4SLinus Torvalds } 4191da177e4SLinus Torvalds 4201da177e4SLinus Torvalds static int 421fd582140SJens Axboe lo_direct_splice_actor(struct pipe_inode_info *pipe, struct splice_desc *sd) 422fd582140SJens Axboe { 423fd582140SJens Axboe return __splice_from_pipe(pipe, sd, lo_splice_actor); 424fd582140SJens Axboe } 425fd582140SJens Axboe 426fd582140SJens Axboe static int 4271da177e4SLinus Torvalds do_lo_receive(struct loop_device *lo, 4281da177e4SLinus Torvalds struct bio_vec *bvec, int bsize, loff_t pos) 4291da177e4SLinus Torvalds { 4301da177e4SLinus Torvalds struct lo_read_data cookie; 431fd582140SJens Axboe struct splice_desc sd; 4321da177e4SLinus Torvalds struct file *file; 433fd582140SJens Axboe long retval; 4341da177e4SLinus Torvalds 4351da177e4SLinus Torvalds cookie.lo = lo; 4361da177e4SLinus Torvalds cookie.page = bvec->bv_page; 4371da177e4SLinus Torvalds cookie.offset = bvec->bv_offset; 4381da177e4SLinus Torvalds cookie.bsize = bsize; 439fd582140SJens Axboe 440fd582140SJens Axboe sd.len = 0; 441fd582140SJens Axboe sd.total_len = bvec->bv_len; 442fd582140SJens Axboe sd.flags = 0; 443fd582140SJens Axboe sd.pos = pos; 444fd582140SJens Axboe sd.u.data = &cookie; 445fd582140SJens Axboe 4461da177e4SLinus Torvalds file = lo->lo_backing_file; 447fd582140SJens Axboe retval = splice_direct_to_actor(file, &sd, lo_direct_splice_actor); 448fd582140SJens Axboe 449fd582140SJens Axboe if (retval < 0) 450fd582140SJens Axboe return retval; 451fd582140SJens Axboe 452fd582140SJens Axboe return 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; 47668db1961SNikanth Karthikesan 47768db1961SNikanth Karthikesan if (bio_rw(bio) == WRITE) { 47868db1961SNikanth Karthikesan struct file *file = lo->lo_backing_file; 47968db1961SNikanth Karthikesan 4806259f284STejun Heo if (bio->bi_rw & REQ_FLUSH) { 4818018ab05SChristoph Hellwig ret = vfs_fsync(file, 0); 4826259f284STejun Heo if (unlikely(ret && ret != -EINVAL)) { 48368db1961SNikanth Karthikesan ret = -EIO; 48468db1961SNikanth Karthikesan goto out; 48568db1961SNikanth Karthikesan } 48668db1961SNikanth Karthikesan } 48768db1961SNikanth Karthikesan 488*dfaa2ef6SLukas Czerner /* 489*dfaa2ef6SLukas Czerner * We use punch hole to reclaim the free space used by the 490*dfaa2ef6SLukas Czerner * image a.k.a. discard. However we do support discard if 491*dfaa2ef6SLukas Czerner * encryption is enabled, because it may give an attacker 492*dfaa2ef6SLukas Czerner * useful information. 493*dfaa2ef6SLukas Czerner */ 494*dfaa2ef6SLukas Czerner if (bio->bi_rw & REQ_DISCARD) { 495*dfaa2ef6SLukas Czerner struct file *file = lo->lo_backing_file; 496*dfaa2ef6SLukas Czerner int mode = FALLOC_FL_PUNCH_HOLE | FALLOC_FL_KEEP_SIZE; 497*dfaa2ef6SLukas Czerner 498*dfaa2ef6SLukas Czerner if ((!file->f_op->fallocate) || 499*dfaa2ef6SLukas Czerner lo->lo_encrypt_key_size) { 500*dfaa2ef6SLukas Czerner ret = -EOPNOTSUPP; 501*dfaa2ef6SLukas Czerner goto out; 502*dfaa2ef6SLukas Czerner } 503*dfaa2ef6SLukas Czerner ret = file->f_op->fallocate(file, mode, pos, 504*dfaa2ef6SLukas Czerner bio->bi_size); 505*dfaa2ef6SLukas Czerner if (unlikely(ret && ret != -EINVAL && 506*dfaa2ef6SLukas Czerner ret != -EOPNOTSUPP)) 507*dfaa2ef6SLukas Czerner ret = -EIO; 508*dfaa2ef6SLukas Czerner goto out; 509*dfaa2ef6SLukas Czerner } 510*dfaa2ef6SLukas Czerner 511511de73fSAl Viro ret = lo_send(lo, bio, pos); 51268db1961SNikanth Karthikesan 5136259f284STejun Heo if ((bio->bi_rw & REQ_FUA) && !ret) { 5148018ab05SChristoph Hellwig ret = vfs_fsync(file, 0); 5156259f284STejun Heo if (unlikely(ret && ret != -EINVAL)) 51668db1961SNikanth Karthikesan ret = -EIO; 51768db1961SNikanth Karthikesan } 51868db1961SNikanth Karthikesan } else 5191da177e4SLinus Torvalds ret = lo_receive(lo, bio, lo->lo_blocksize, pos); 52068db1961SNikanth Karthikesan 52168db1961SNikanth Karthikesan out: 5221da177e4SLinus Torvalds return ret; 5231da177e4SLinus Torvalds } 5241da177e4SLinus Torvalds 5251da177e4SLinus Torvalds /* 5261da177e4SLinus Torvalds * Add bio to back of pending list 5271da177e4SLinus Torvalds */ 5281da177e4SLinus Torvalds static void loop_add_bio(struct loop_device *lo, struct bio *bio) 5291da177e4SLinus Torvalds { 530e686307fSAkinobu Mita bio_list_add(&lo->lo_bio_list, bio); 5311da177e4SLinus Torvalds } 5321da177e4SLinus Torvalds 5331da177e4SLinus Torvalds /* 5341da177e4SLinus Torvalds * Grab first pending buffer 5351da177e4SLinus Torvalds */ 5361da177e4SLinus Torvalds static struct bio *loop_get_bio(struct loop_device *lo) 5371da177e4SLinus Torvalds { 538e686307fSAkinobu Mita return bio_list_pop(&lo->lo_bio_list); 5391da177e4SLinus Torvalds } 5401da177e4SLinus Torvalds 541165125e1SJens Axboe static int loop_make_request(struct request_queue *q, struct bio *old_bio) 5421da177e4SLinus Torvalds { 5431da177e4SLinus Torvalds struct loop_device *lo = q->queuedata; 5441da177e4SLinus Torvalds int rw = bio_rw(old_bio); 5451da177e4SLinus Torvalds 54635a82d1aSNick Piggin if (rw == READA) 54735a82d1aSNick Piggin rw = READ; 54835a82d1aSNick Piggin 54935a82d1aSNick Piggin BUG_ON(!lo || (rw != READ && rw != WRITE)); 5501da177e4SLinus Torvalds 5511da177e4SLinus Torvalds spin_lock_irq(&lo->lo_lock); 5521da177e4SLinus Torvalds if (lo->lo_state != Lo_bound) 55335a82d1aSNick Piggin goto out; 55435a82d1aSNick Piggin if (unlikely(rw == WRITE && (lo->lo_flags & LO_FLAGS_READ_ONLY))) 55535a82d1aSNick Piggin goto out; 5561da177e4SLinus Torvalds loop_add_bio(lo, old_bio); 5576c997918SSerge E. Hallyn wake_up(&lo->lo_event); 55835a82d1aSNick Piggin spin_unlock_irq(&lo->lo_lock); 55935a82d1aSNick Piggin return 0; 56035a82d1aSNick Piggin 5611da177e4SLinus Torvalds out: 56235a82d1aSNick Piggin spin_unlock_irq(&lo->lo_lock); 5636712ecf8SNeilBrown bio_io_error(old_bio); 5641da177e4SLinus Torvalds return 0; 5651da177e4SLinus Torvalds } 5661da177e4SLinus Torvalds 5671da177e4SLinus Torvalds struct switch_request { 5681da177e4SLinus Torvalds struct file *file; 5691da177e4SLinus Torvalds struct completion wait; 5701da177e4SLinus Torvalds }; 5711da177e4SLinus Torvalds 5721da177e4SLinus Torvalds static void do_loop_switch(struct loop_device *, struct switch_request *); 5731da177e4SLinus Torvalds 5741da177e4SLinus Torvalds static inline void loop_handle_bio(struct loop_device *lo, struct bio *bio) 5751da177e4SLinus Torvalds { 5761da177e4SLinus Torvalds if (unlikely(!bio->bi_bdev)) { 5771da177e4SLinus Torvalds do_loop_switch(lo, bio->bi_private); 5781da177e4SLinus Torvalds bio_put(bio); 5791da177e4SLinus Torvalds } else { 58035a82d1aSNick Piggin int ret = do_bio_filebacked(lo, bio); 5816712ecf8SNeilBrown bio_endio(bio, ret); 5821da177e4SLinus Torvalds } 5831da177e4SLinus Torvalds } 5841da177e4SLinus Torvalds 5851da177e4SLinus Torvalds /* 5861da177e4SLinus Torvalds * worker thread that handles reads/writes to file backed loop devices, 5871da177e4SLinus Torvalds * to avoid blocking in our make_request_fn. it also does loop decrypting 5881da177e4SLinus Torvalds * on reads for block backed loop, as that is too heavy to do from 5891da177e4SLinus Torvalds * b_end_io context where irqs may be disabled. 5906c997918SSerge E. Hallyn * 5916c997918SSerge E. Hallyn * Loop explanation: loop_clr_fd() sets lo_state to Lo_rundown before 5926c997918SSerge E. Hallyn * calling kthread_stop(). Therefore once kthread_should_stop() is 5936c997918SSerge E. Hallyn * true, make_request will not place any more requests. Therefore 5946c997918SSerge E. Hallyn * once kthread_should_stop() is true and lo_bio is NULL, we are 5956c997918SSerge E. Hallyn * done with the loop. 5961da177e4SLinus Torvalds */ 5971da177e4SLinus Torvalds static int loop_thread(void *data) 5981da177e4SLinus Torvalds { 5991da177e4SLinus Torvalds struct loop_device *lo = data; 6001da177e4SLinus Torvalds struct bio *bio; 6011da177e4SLinus Torvalds 6021da177e4SLinus Torvalds set_user_nice(current, -20); 6031da177e4SLinus Torvalds 604e686307fSAkinobu Mita while (!kthread_should_stop() || !bio_list_empty(&lo->lo_bio_list)) { 6051da177e4SLinus Torvalds 6066c997918SSerge E. Hallyn wait_event_interruptible(lo->lo_event, 607e686307fSAkinobu Mita !bio_list_empty(&lo->lo_bio_list) || 608e686307fSAkinobu Mita kthread_should_stop()); 60909c0dc68SLinus Torvalds 610e686307fSAkinobu Mita if (bio_list_empty(&lo->lo_bio_list)) 61135a82d1aSNick Piggin continue; 61235a82d1aSNick Piggin spin_lock_irq(&lo->lo_lock); 6131da177e4SLinus Torvalds bio = loop_get_bio(lo); 61435a82d1aSNick Piggin spin_unlock_irq(&lo->lo_lock); 61535a82d1aSNick Piggin 61635a82d1aSNick Piggin BUG_ON(!bio); 6171da177e4SLinus Torvalds loop_handle_bio(lo, bio); 6181da177e4SLinus Torvalds } 6191da177e4SLinus Torvalds 6201da177e4SLinus Torvalds return 0; 6211da177e4SLinus Torvalds } 6221da177e4SLinus Torvalds 6231da177e4SLinus Torvalds /* 6241da177e4SLinus Torvalds * loop_switch performs the hard work of switching a backing store. 6251da177e4SLinus Torvalds * First it needs to flush existing IO, it does this by sending a magic 6261da177e4SLinus Torvalds * BIO down the pipe. The completion of this BIO does the actual switch. 6271da177e4SLinus Torvalds */ 6281da177e4SLinus Torvalds static int loop_switch(struct loop_device *lo, struct file *file) 6291da177e4SLinus Torvalds { 6301da177e4SLinus Torvalds struct switch_request w; 631a24eab1eSJens Axboe struct bio *bio = bio_alloc(GFP_KERNEL, 0); 6321da177e4SLinus Torvalds if (!bio) 6331da177e4SLinus Torvalds return -ENOMEM; 6341da177e4SLinus Torvalds init_completion(&w.wait); 6351da177e4SLinus Torvalds w.file = file; 6361da177e4SLinus Torvalds bio->bi_private = &w; 6371da177e4SLinus Torvalds bio->bi_bdev = NULL; 6381da177e4SLinus Torvalds loop_make_request(lo->lo_queue, bio); 6391da177e4SLinus Torvalds wait_for_completion(&w.wait); 6401da177e4SLinus Torvalds return 0; 6411da177e4SLinus Torvalds } 6421da177e4SLinus Torvalds 6431da177e4SLinus Torvalds /* 64414f27939SMilan Broz * Helper to flush the IOs in loop, but keeping loop thread running 64514f27939SMilan Broz */ 64614f27939SMilan Broz static int loop_flush(struct loop_device *lo) 64714f27939SMilan Broz { 64814f27939SMilan Broz /* loop not yet configured, no running thread, nothing to flush */ 64914f27939SMilan Broz if (!lo->lo_thread) 65014f27939SMilan Broz return 0; 65114f27939SMilan Broz 65214f27939SMilan Broz return loop_switch(lo, NULL); 65314f27939SMilan Broz } 65414f27939SMilan Broz 65514f27939SMilan Broz /* 6561da177e4SLinus Torvalds * Do the actual switch; called from the BIO completion routine 6571da177e4SLinus Torvalds */ 6581da177e4SLinus Torvalds static void do_loop_switch(struct loop_device *lo, struct switch_request *p) 6591da177e4SLinus Torvalds { 6601da177e4SLinus Torvalds struct file *file = p->file; 6611da177e4SLinus Torvalds struct file *old_file = lo->lo_backing_file; 66214f27939SMilan Broz struct address_space *mapping; 6631da177e4SLinus Torvalds 66414f27939SMilan Broz /* if no new file, only flush of queued bios requested */ 66514f27939SMilan Broz if (!file) 66614f27939SMilan Broz goto out; 66714f27939SMilan Broz 66814f27939SMilan Broz mapping = file->f_mapping; 6691da177e4SLinus Torvalds mapping_set_gfp_mask(old_file->f_mapping, lo->old_gfp_mask); 6701da177e4SLinus Torvalds lo->lo_backing_file = file; 671ba52de12STheodore Ts'o lo->lo_blocksize = S_ISBLK(mapping->host->i_mode) ? 672ba52de12STheodore Ts'o mapping->host->i_bdev->bd_block_size : PAGE_SIZE; 6731da177e4SLinus Torvalds lo->old_gfp_mask = mapping_gfp_mask(mapping); 6741da177e4SLinus Torvalds mapping_set_gfp_mask(mapping, lo->old_gfp_mask & ~(__GFP_IO|__GFP_FS)); 67514f27939SMilan Broz out: 6761da177e4SLinus Torvalds complete(&p->wait); 6771da177e4SLinus Torvalds } 6781da177e4SLinus Torvalds 6791da177e4SLinus Torvalds 6801da177e4SLinus Torvalds /* 6811da177e4SLinus Torvalds * loop_change_fd switched the backing store of a loopback device to 6821da177e4SLinus Torvalds * a new file. This is useful for operating system installers to free up 6831da177e4SLinus Torvalds * the original file and in High Availability environments to switch to 6841da177e4SLinus Torvalds * an alternative location for the content in case of server meltdown. 6851da177e4SLinus Torvalds * This can only work if the loop device is used read-only, and if the 6861da177e4SLinus Torvalds * new backing store is the same size and type as the old backing store. 6871da177e4SLinus Torvalds */ 688bb214884SAl Viro static int loop_change_fd(struct loop_device *lo, struct block_device *bdev, 689bb214884SAl Viro unsigned int arg) 6901da177e4SLinus Torvalds { 6911da177e4SLinus Torvalds struct file *file, *old_file; 6921da177e4SLinus Torvalds struct inode *inode; 6931da177e4SLinus Torvalds int error; 6941da177e4SLinus Torvalds 6951da177e4SLinus Torvalds error = -ENXIO; 6961da177e4SLinus Torvalds if (lo->lo_state != Lo_bound) 6971da177e4SLinus Torvalds goto out; 6981da177e4SLinus Torvalds 6991da177e4SLinus Torvalds /* the loop device has to be read-only */ 7001da177e4SLinus Torvalds error = -EINVAL; 7011da177e4SLinus Torvalds if (!(lo->lo_flags & LO_FLAGS_READ_ONLY)) 7021da177e4SLinus Torvalds goto out; 7031da177e4SLinus Torvalds 7041da177e4SLinus Torvalds error = -EBADF; 7051da177e4SLinus Torvalds file = fget(arg); 7061da177e4SLinus Torvalds if (!file) 7071da177e4SLinus Torvalds goto out; 7081da177e4SLinus Torvalds 7091da177e4SLinus Torvalds inode = file->f_mapping->host; 7101da177e4SLinus Torvalds old_file = lo->lo_backing_file; 7111da177e4SLinus Torvalds 7121da177e4SLinus Torvalds error = -EINVAL; 7131da177e4SLinus Torvalds 7141da177e4SLinus Torvalds if (!S_ISREG(inode->i_mode) && !S_ISBLK(inode->i_mode)) 7151da177e4SLinus Torvalds goto out_putf; 7161da177e4SLinus Torvalds 7171da177e4SLinus Torvalds /* size of the new backing store needs to be the same */ 7181da177e4SLinus Torvalds if (get_loop_size(lo, file) != get_loop_size(lo, old_file)) 7191da177e4SLinus Torvalds goto out_putf; 7201da177e4SLinus Torvalds 7211da177e4SLinus Torvalds /* and ... switch */ 7221da177e4SLinus Torvalds error = loop_switch(lo, file); 7231da177e4SLinus Torvalds if (error) 7241da177e4SLinus Torvalds goto out_putf; 7251da177e4SLinus Torvalds 7261da177e4SLinus Torvalds fput(old_file); 727476a4813SLaurent Vivier if (max_part > 0) 728476a4813SLaurent Vivier ioctl_by_bdev(bdev, BLKRRPART, 0); 7291da177e4SLinus Torvalds return 0; 7301da177e4SLinus Torvalds 7311da177e4SLinus Torvalds out_putf: 7321da177e4SLinus Torvalds fput(file); 7331da177e4SLinus Torvalds out: 7341da177e4SLinus Torvalds return error; 7351da177e4SLinus Torvalds } 7361da177e4SLinus Torvalds 7371da177e4SLinus Torvalds static inline int is_loop_device(struct file *file) 7381da177e4SLinus Torvalds { 7391da177e4SLinus Torvalds struct inode *i = file->f_mapping->host; 7401da177e4SLinus Torvalds 7411da177e4SLinus Torvalds return i && S_ISBLK(i->i_mode) && MAJOR(i->i_rdev) == LOOP_MAJOR; 7421da177e4SLinus Torvalds } 7431da177e4SLinus Torvalds 744ee862730SMilan Broz /* loop sysfs attributes */ 745ee862730SMilan Broz 746ee862730SMilan Broz static ssize_t loop_attr_show(struct device *dev, char *page, 747ee862730SMilan Broz ssize_t (*callback)(struct loop_device *, char *)) 748ee862730SMilan Broz { 749ee862730SMilan Broz struct loop_device *l, *lo = NULL; 750ee862730SMilan Broz 751ee862730SMilan Broz mutex_lock(&loop_devices_mutex); 752ee862730SMilan Broz list_for_each_entry(l, &loop_devices, lo_list) 753ee862730SMilan Broz if (disk_to_dev(l->lo_disk) == dev) { 754ee862730SMilan Broz lo = l; 755ee862730SMilan Broz break; 756ee862730SMilan Broz } 757ee862730SMilan Broz mutex_unlock(&loop_devices_mutex); 758ee862730SMilan Broz 759ee862730SMilan Broz return lo ? callback(lo, page) : -EIO; 760ee862730SMilan Broz } 761ee862730SMilan Broz 762ee862730SMilan Broz #define LOOP_ATTR_RO(_name) \ 763ee862730SMilan Broz static ssize_t loop_attr_##_name##_show(struct loop_device *, char *); \ 764ee862730SMilan Broz static ssize_t loop_attr_do_show_##_name(struct device *d, \ 765ee862730SMilan Broz struct device_attribute *attr, char *b) \ 766ee862730SMilan Broz { \ 767ee862730SMilan Broz return loop_attr_show(d, b, loop_attr_##_name##_show); \ 768ee862730SMilan Broz } \ 769ee862730SMilan Broz static struct device_attribute loop_attr_##_name = \ 770ee862730SMilan Broz __ATTR(_name, S_IRUGO, loop_attr_do_show_##_name, NULL); 771ee862730SMilan Broz 772ee862730SMilan Broz static ssize_t loop_attr_backing_file_show(struct loop_device *lo, char *buf) 773ee862730SMilan Broz { 774ee862730SMilan Broz ssize_t ret; 775ee862730SMilan Broz char *p = NULL; 776ee862730SMilan Broz 777ee862730SMilan Broz mutex_lock(&lo->lo_ctl_mutex); 778ee862730SMilan Broz if (lo->lo_backing_file) 779ee862730SMilan Broz p = d_path(&lo->lo_backing_file->f_path, buf, PAGE_SIZE - 1); 780ee862730SMilan Broz mutex_unlock(&lo->lo_ctl_mutex); 781ee862730SMilan Broz 782ee862730SMilan Broz if (IS_ERR_OR_NULL(p)) 783ee862730SMilan Broz ret = PTR_ERR(p); 784ee862730SMilan Broz else { 785ee862730SMilan Broz ret = strlen(p); 786ee862730SMilan Broz memmove(buf, p, ret); 787ee862730SMilan Broz buf[ret++] = '\n'; 788ee862730SMilan Broz buf[ret] = 0; 789ee862730SMilan Broz } 790ee862730SMilan Broz 791ee862730SMilan Broz return ret; 792ee862730SMilan Broz } 793ee862730SMilan Broz 794ee862730SMilan Broz static ssize_t loop_attr_offset_show(struct loop_device *lo, char *buf) 795ee862730SMilan Broz { 796ee862730SMilan Broz return sprintf(buf, "%llu\n", (unsigned long long)lo->lo_offset); 797ee862730SMilan Broz } 798ee862730SMilan Broz 799ee862730SMilan Broz static ssize_t loop_attr_sizelimit_show(struct loop_device *lo, char *buf) 800ee862730SMilan Broz { 801ee862730SMilan Broz return sprintf(buf, "%llu\n", (unsigned long long)lo->lo_sizelimit); 802ee862730SMilan Broz } 803ee862730SMilan Broz 804ee862730SMilan Broz static ssize_t loop_attr_autoclear_show(struct loop_device *lo, char *buf) 805ee862730SMilan Broz { 806ee862730SMilan Broz int autoclear = (lo->lo_flags & LO_FLAGS_AUTOCLEAR); 807ee862730SMilan Broz 808ee862730SMilan Broz return sprintf(buf, "%s\n", autoclear ? "1" : "0"); 809ee862730SMilan Broz } 810ee862730SMilan Broz 811ee862730SMilan Broz LOOP_ATTR_RO(backing_file); 812ee862730SMilan Broz LOOP_ATTR_RO(offset); 813ee862730SMilan Broz LOOP_ATTR_RO(sizelimit); 814ee862730SMilan Broz LOOP_ATTR_RO(autoclear); 815ee862730SMilan Broz 816ee862730SMilan Broz static struct attribute *loop_attrs[] = { 817ee862730SMilan Broz &loop_attr_backing_file.attr, 818ee862730SMilan Broz &loop_attr_offset.attr, 819ee862730SMilan Broz &loop_attr_sizelimit.attr, 820ee862730SMilan Broz &loop_attr_autoclear.attr, 821ee862730SMilan Broz NULL, 822ee862730SMilan Broz }; 823ee862730SMilan Broz 824ee862730SMilan Broz static struct attribute_group loop_attribute_group = { 825ee862730SMilan Broz .name = "loop", 826ee862730SMilan Broz .attrs= loop_attrs, 827ee862730SMilan Broz }; 828ee862730SMilan Broz 829ee862730SMilan Broz static int loop_sysfs_init(struct loop_device *lo) 830ee862730SMilan Broz { 831ee862730SMilan Broz return sysfs_create_group(&disk_to_dev(lo->lo_disk)->kobj, 832ee862730SMilan Broz &loop_attribute_group); 833ee862730SMilan Broz } 834ee862730SMilan Broz 835ee862730SMilan Broz static void loop_sysfs_exit(struct loop_device *lo) 836ee862730SMilan Broz { 837ee862730SMilan Broz sysfs_remove_group(&disk_to_dev(lo->lo_disk)->kobj, 838ee862730SMilan Broz &loop_attribute_group); 839ee862730SMilan Broz } 840ee862730SMilan Broz 841*dfaa2ef6SLukas Czerner static void loop_config_discard(struct loop_device *lo) 842*dfaa2ef6SLukas Czerner { 843*dfaa2ef6SLukas Czerner struct file *file = lo->lo_backing_file; 844*dfaa2ef6SLukas Czerner struct inode *inode = file->f_mapping->host; 845*dfaa2ef6SLukas Czerner struct request_queue *q = lo->lo_queue; 846*dfaa2ef6SLukas Czerner 847*dfaa2ef6SLukas Czerner /* 848*dfaa2ef6SLukas Czerner * We use punch hole to reclaim the free space used by the 849*dfaa2ef6SLukas Czerner * image a.k.a. discard. However we do support discard if 850*dfaa2ef6SLukas Czerner * encryption is enabled, because it may give an attacker 851*dfaa2ef6SLukas Czerner * useful information. 852*dfaa2ef6SLukas Czerner */ 853*dfaa2ef6SLukas Czerner if ((!file->f_op->fallocate) || 854*dfaa2ef6SLukas Czerner lo->lo_encrypt_key_size) { 855*dfaa2ef6SLukas Czerner q->limits.discard_granularity = 0; 856*dfaa2ef6SLukas Czerner q->limits.discard_alignment = 0; 857*dfaa2ef6SLukas Czerner q->limits.max_discard_sectors = 0; 858*dfaa2ef6SLukas Czerner q->limits.discard_zeroes_data = 0; 859*dfaa2ef6SLukas Czerner queue_flag_clear_unlocked(QUEUE_FLAG_DISCARD, q); 860*dfaa2ef6SLukas Czerner return; 861*dfaa2ef6SLukas Czerner } 862*dfaa2ef6SLukas Czerner 863*dfaa2ef6SLukas Czerner q->limits.discard_granularity = inode->i_sb->s_blocksize; 864*dfaa2ef6SLukas Czerner q->limits.discard_alignment = inode->i_sb->s_blocksize; 865*dfaa2ef6SLukas Czerner q->limits.max_discard_sectors = UINT_MAX >> 9; 866*dfaa2ef6SLukas Czerner q->limits.discard_zeroes_data = 1; 867*dfaa2ef6SLukas Czerner queue_flag_set_unlocked(QUEUE_FLAG_DISCARD, q); 868*dfaa2ef6SLukas Czerner } 869*dfaa2ef6SLukas Czerner 870bb214884SAl Viro static int loop_set_fd(struct loop_device *lo, fmode_t mode, 8711da177e4SLinus Torvalds struct block_device *bdev, unsigned int arg) 8721da177e4SLinus Torvalds { 8731da177e4SLinus Torvalds struct file *file, *f; 8741da177e4SLinus Torvalds struct inode *inode; 8751da177e4SLinus Torvalds struct address_space *mapping; 8761da177e4SLinus Torvalds unsigned lo_blocksize; 8771da177e4SLinus Torvalds int lo_flags = 0; 8781da177e4SLinus Torvalds int error; 8791da177e4SLinus Torvalds loff_t size; 8801da177e4SLinus Torvalds 8811da177e4SLinus Torvalds /* This is safe, since we have a reference from open(). */ 8821da177e4SLinus Torvalds __module_get(THIS_MODULE); 8831da177e4SLinus Torvalds 8841da177e4SLinus Torvalds error = -EBADF; 8851da177e4SLinus Torvalds file = fget(arg); 8861da177e4SLinus Torvalds if (!file) 8871da177e4SLinus Torvalds goto out; 8881da177e4SLinus Torvalds 8891da177e4SLinus Torvalds error = -EBUSY; 8901da177e4SLinus Torvalds if (lo->lo_state != Lo_unbound) 8911da177e4SLinus Torvalds goto out_putf; 8921da177e4SLinus Torvalds 8931da177e4SLinus Torvalds /* Avoid recursion */ 8941da177e4SLinus Torvalds f = file; 8951da177e4SLinus Torvalds while (is_loop_device(f)) { 8961da177e4SLinus Torvalds struct loop_device *l; 8971da177e4SLinus Torvalds 898bb214884SAl Viro if (f->f_mapping->host->i_bdev == bdev) 8991da177e4SLinus Torvalds goto out_putf; 9001da177e4SLinus Torvalds 9011da177e4SLinus Torvalds l = f->f_mapping->host->i_bdev->bd_disk->private_data; 9021da177e4SLinus Torvalds if (l->lo_state == Lo_unbound) { 9031da177e4SLinus Torvalds error = -EINVAL; 9041da177e4SLinus Torvalds goto out_putf; 9051da177e4SLinus Torvalds } 9061da177e4SLinus Torvalds f = l->lo_backing_file; 9071da177e4SLinus Torvalds } 9081da177e4SLinus Torvalds 9091da177e4SLinus Torvalds mapping = file->f_mapping; 9101da177e4SLinus Torvalds inode = mapping->host; 9111da177e4SLinus Torvalds 9121da177e4SLinus Torvalds if (!(file->f_mode & FMODE_WRITE)) 9131da177e4SLinus Torvalds lo_flags |= LO_FLAGS_READ_ONLY; 9141da177e4SLinus Torvalds 9151da177e4SLinus Torvalds error = -EINVAL; 9161da177e4SLinus Torvalds if (S_ISREG(inode->i_mode) || S_ISBLK(inode->i_mode)) { 917f5e54d6eSChristoph Hellwig const struct address_space_operations *aops = mapping->a_ops; 9186818173bSMiklos Szeredi 9194e02ed4bSNick Piggin if (aops->write_begin) 9201da177e4SLinus Torvalds lo_flags |= LO_FLAGS_USE_AOPS; 9211da177e4SLinus Torvalds if (!(lo_flags & LO_FLAGS_USE_AOPS) && !file->f_op->write) 9221da177e4SLinus Torvalds lo_flags |= LO_FLAGS_READ_ONLY; 9231da177e4SLinus Torvalds 924ba52de12STheodore Ts'o lo_blocksize = S_ISBLK(inode->i_mode) ? 925ba52de12STheodore Ts'o inode->i_bdev->bd_block_size : PAGE_SIZE; 926ba52de12STheodore Ts'o 9271da177e4SLinus Torvalds error = 0; 9281da177e4SLinus Torvalds } else { 9291da177e4SLinus Torvalds goto out_putf; 9301da177e4SLinus Torvalds } 9311da177e4SLinus Torvalds 9321da177e4SLinus Torvalds size = get_loop_size(lo, file); 9331da177e4SLinus Torvalds 9341da177e4SLinus Torvalds if ((loff_t)(sector_t)size != size) { 9351da177e4SLinus Torvalds error = -EFBIG; 9361da177e4SLinus Torvalds goto out_putf; 9371da177e4SLinus Torvalds } 9381da177e4SLinus Torvalds 939bb214884SAl Viro if (!(mode & FMODE_WRITE)) 9401da177e4SLinus Torvalds lo_flags |= LO_FLAGS_READ_ONLY; 9411da177e4SLinus Torvalds 9421da177e4SLinus Torvalds set_device_ro(bdev, (lo_flags & LO_FLAGS_READ_ONLY) != 0); 9431da177e4SLinus Torvalds 9441da177e4SLinus Torvalds lo->lo_blocksize = lo_blocksize; 9451da177e4SLinus Torvalds lo->lo_device = bdev; 9461da177e4SLinus Torvalds lo->lo_flags = lo_flags; 9471da177e4SLinus Torvalds lo->lo_backing_file = file; 948eefe85eeSConstantine Sapuntzakis lo->transfer = transfer_none; 9491da177e4SLinus Torvalds lo->ioctl = NULL; 9501da177e4SLinus Torvalds lo->lo_sizelimit = 0; 9511da177e4SLinus Torvalds lo->old_gfp_mask = mapping_gfp_mask(mapping); 9521da177e4SLinus Torvalds mapping_set_gfp_mask(mapping, lo->old_gfp_mask & ~(__GFP_IO|__GFP_FS)); 9531da177e4SLinus Torvalds 954e686307fSAkinobu Mita bio_list_init(&lo->lo_bio_list); 9551da177e4SLinus Torvalds 9561da177e4SLinus Torvalds /* 9571da177e4SLinus Torvalds * set queue make_request_fn, and add limits based on lower level 9581da177e4SLinus Torvalds * device 9591da177e4SLinus Torvalds */ 9601da177e4SLinus Torvalds blk_queue_make_request(lo->lo_queue, loop_make_request); 9611da177e4SLinus Torvalds lo->lo_queue->queuedata = lo; 9621da177e4SLinus Torvalds 96368db1961SNikanth Karthikesan if (!(lo_flags & LO_FLAGS_READ_ONLY) && file->f_op->fsync) 9644913efe4STejun Heo blk_queue_flush(lo->lo_queue, REQ_FLUSH); 96568db1961SNikanth Karthikesan 96673285082SKen Chen set_capacity(lo->lo_disk, size); 9671da177e4SLinus Torvalds bd_set_size(bdev, size << 9); 968ee862730SMilan Broz loop_sysfs_init(lo); 969c3473c63SDavid Zeuthen /* let user-space know about the new size */ 970c3473c63SDavid Zeuthen kobject_uevent(&disk_to_dev(bdev->bd_disk)->kobj, KOBJ_CHANGE); 9711da177e4SLinus Torvalds 9721da177e4SLinus Torvalds set_blocksize(bdev, lo_blocksize); 9731da177e4SLinus Torvalds 9746c997918SSerge E. Hallyn lo->lo_thread = kthread_create(loop_thread, lo, "loop%d", 9756c997918SSerge E. Hallyn lo->lo_number); 9766c997918SSerge E. Hallyn if (IS_ERR(lo->lo_thread)) { 9776c997918SSerge E. Hallyn error = PTR_ERR(lo->lo_thread); 978a7422bf8SSerge E. Hallyn goto out_clr; 9796c997918SSerge E. Hallyn } 9806c997918SSerge E. Hallyn lo->lo_state = Lo_bound; 9816c997918SSerge E. Hallyn wake_up_process(lo->lo_thread); 982476a4813SLaurent Vivier if (max_part > 0) 983476a4813SLaurent Vivier ioctl_by_bdev(bdev, BLKRRPART, 0); 9841da177e4SLinus Torvalds return 0; 9851da177e4SLinus Torvalds 986a7422bf8SSerge E. Hallyn out_clr: 987ee862730SMilan Broz loop_sysfs_exit(lo); 988a7422bf8SSerge E. Hallyn lo->lo_thread = NULL; 989a7422bf8SSerge E. Hallyn lo->lo_device = NULL; 990a7422bf8SSerge E. Hallyn lo->lo_backing_file = NULL; 991a7422bf8SSerge E. Hallyn lo->lo_flags = 0; 99273285082SKen Chen set_capacity(lo->lo_disk, 0); 993f98393a6SPeter Zijlstra invalidate_bdev(bdev); 994a7422bf8SSerge E. Hallyn bd_set_size(bdev, 0); 995c3473c63SDavid Zeuthen kobject_uevent(&disk_to_dev(bdev->bd_disk)->kobj, KOBJ_CHANGE); 996a7422bf8SSerge E. Hallyn mapping_set_gfp_mask(mapping, lo->old_gfp_mask); 997a7422bf8SSerge E. Hallyn lo->lo_state = Lo_unbound; 9981da177e4SLinus Torvalds out_putf: 9991da177e4SLinus Torvalds fput(file); 10001da177e4SLinus Torvalds out: 10011da177e4SLinus Torvalds /* This is safe: open() is still holding a reference. */ 10021da177e4SLinus Torvalds module_put(THIS_MODULE); 10031da177e4SLinus Torvalds return error; 10041da177e4SLinus Torvalds } 10051da177e4SLinus Torvalds 10061da177e4SLinus Torvalds static int 10071da177e4SLinus Torvalds loop_release_xfer(struct loop_device *lo) 10081da177e4SLinus Torvalds { 10091da177e4SLinus Torvalds int err = 0; 10101da177e4SLinus Torvalds struct loop_func_table *xfer = lo->lo_encryption; 10111da177e4SLinus Torvalds 10121da177e4SLinus Torvalds if (xfer) { 10131da177e4SLinus Torvalds if (xfer->release) 10141da177e4SLinus Torvalds err = xfer->release(lo); 10151da177e4SLinus Torvalds lo->transfer = NULL; 10161da177e4SLinus Torvalds lo->lo_encryption = NULL; 10171da177e4SLinus Torvalds module_put(xfer->owner); 10181da177e4SLinus Torvalds } 10191da177e4SLinus Torvalds return err; 10201da177e4SLinus Torvalds } 10211da177e4SLinus Torvalds 10221da177e4SLinus Torvalds static int 10231da177e4SLinus Torvalds loop_init_xfer(struct loop_device *lo, struct loop_func_table *xfer, 10241da177e4SLinus Torvalds const struct loop_info64 *i) 10251da177e4SLinus Torvalds { 10261da177e4SLinus Torvalds int err = 0; 10271da177e4SLinus Torvalds 10281da177e4SLinus Torvalds if (xfer) { 10291da177e4SLinus Torvalds struct module *owner = xfer->owner; 10301da177e4SLinus Torvalds 10311da177e4SLinus Torvalds if (!try_module_get(owner)) 10321da177e4SLinus Torvalds return -EINVAL; 10331da177e4SLinus Torvalds if (xfer->init) 10341da177e4SLinus Torvalds err = xfer->init(lo, i); 10351da177e4SLinus Torvalds if (err) 10361da177e4SLinus Torvalds module_put(owner); 10371da177e4SLinus Torvalds else 10381da177e4SLinus Torvalds lo->lo_encryption = xfer; 10391da177e4SLinus Torvalds } 10401da177e4SLinus Torvalds return err; 10411da177e4SLinus Torvalds } 10421da177e4SLinus Torvalds 10431da177e4SLinus Torvalds static int loop_clr_fd(struct loop_device *lo, struct block_device *bdev) 10441da177e4SLinus Torvalds { 10451da177e4SLinus Torvalds struct file *filp = lo->lo_backing_file; 1046b4e3ca1aSAl Viro gfp_t gfp = lo->old_gfp_mask; 10471da177e4SLinus Torvalds 10481da177e4SLinus Torvalds if (lo->lo_state != Lo_bound) 10491da177e4SLinus Torvalds return -ENXIO; 10501da177e4SLinus Torvalds 10511da177e4SLinus Torvalds if (lo->lo_refcnt > 1) /* we needed one fd for the ioctl */ 10521da177e4SLinus Torvalds return -EBUSY; 10531da177e4SLinus Torvalds 10541da177e4SLinus Torvalds if (filp == NULL) 10551da177e4SLinus Torvalds return -EINVAL; 10561da177e4SLinus Torvalds 10571da177e4SLinus Torvalds spin_lock_irq(&lo->lo_lock); 10581da177e4SLinus Torvalds lo->lo_state = Lo_rundown; 10591da177e4SLinus Torvalds spin_unlock_irq(&lo->lo_lock); 10601da177e4SLinus Torvalds 10616c997918SSerge E. Hallyn kthread_stop(lo->lo_thread); 10621da177e4SLinus Torvalds 10631da177e4SLinus Torvalds lo->lo_backing_file = NULL; 10641da177e4SLinus Torvalds 10651da177e4SLinus Torvalds loop_release_xfer(lo); 10661da177e4SLinus Torvalds lo->transfer = NULL; 10671da177e4SLinus Torvalds lo->ioctl = NULL; 10681da177e4SLinus Torvalds lo->lo_device = NULL; 10691da177e4SLinus Torvalds lo->lo_encryption = NULL; 10701da177e4SLinus Torvalds lo->lo_offset = 0; 10711da177e4SLinus Torvalds lo->lo_sizelimit = 0; 10721da177e4SLinus Torvalds lo->lo_encrypt_key_size = 0; 10731da177e4SLinus Torvalds lo->lo_flags = 0; 10746c997918SSerge E. Hallyn lo->lo_thread = NULL; 10751da177e4SLinus Torvalds memset(lo->lo_encrypt_key, 0, LO_KEY_SIZE); 10761da177e4SLinus Torvalds memset(lo->lo_crypt_name, 0, LO_NAME_SIZE); 10771da177e4SLinus Torvalds memset(lo->lo_file_name, 0, LO_NAME_SIZE); 1078bb214884SAl Viro if (bdev) 1079f98393a6SPeter Zijlstra invalidate_bdev(bdev); 108073285082SKen Chen set_capacity(lo->lo_disk, 0); 108151a0bb0cSMilan Broz loop_sysfs_exit(lo); 1082c3473c63SDavid Zeuthen if (bdev) { 10831da177e4SLinus Torvalds bd_set_size(bdev, 0); 1084c3473c63SDavid Zeuthen /* let user-space know about this change */ 1085c3473c63SDavid Zeuthen kobject_uevent(&disk_to_dev(bdev->bd_disk)->kobj, KOBJ_CHANGE); 1086c3473c63SDavid Zeuthen } 10871da177e4SLinus Torvalds mapping_set_gfp_mask(filp->f_mapping, gfp); 10881da177e4SLinus Torvalds lo->lo_state = Lo_unbound; 10891da177e4SLinus Torvalds /* This is safe: open() is still holding a reference. */ 10901da177e4SLinus Torvalds module_put(THIS_MODULE); 1091cf6e6932SAlexey Dobriyan if (max_part > 0 && bdev) 1092476a4813SLaurent Vivier ioctl_by_bdev(bdev, BLKRRPART, 0); 1093f028f3b2SNikanth Karthikesan mutex_unlock(&lo->lo_ctl_mutex); 1094f028f3b2SNikanth Karthikesan /* 1095f028f3b2SNikanth Karthikesan * Need not hold lo_ctl_mutex to fput backing file. 1096f028f3b2SNikanth Karthikesan * Calling fput holding lo_ctl_mutex triggers a circular 1097f028f3b2SNikanth Karthikesan * lock dependency possibility warning as fput can take 1098f028f3b2SNikanth Karthikesan * bd_mutex which is usually taken before lo_ctl_mutex. 1099f028f3b2SNikanth Karthikesan */ 1100f028f3b2SNikanth Karthikesan fput(filp); 11011da177e4SLinus Torvalds return 0; 11021da177e4SLinus Torvalds } 11031da177e4SLinus Torvalds 11041da177e4SLinus Torvalds static int 11051da177e4SLinus Torvalds loop_set_status(struct loop_device *lo, const struct loop_info64 *info) 11061da177e4SLinus Torvalds { 11071da177e4SLinus Torvalds int err; 11081da177e4SLinus Torvalds struct loop_func_table *xfer; 1109b0fafa81SDavid Howells uid_t uid = current_uid(); 11101da177e4SLinus Torvalds 1111b0fafa81SDavid Howells if (lo->lo_encrypt_key_size && 1112b0fafa81SDavid Howells lo->lo_key_owner != uid && 11131da177e4SLinus Torvalds !capable(CAP_SYS_ADMIN)) 11141da177e4SLinus Torvalds return -EPERM; 11151da177e4SLinus Torvalds if (lo->lo_state != Lo_bound) 11161da177e4SLinus Torvalds return -ENXIO; 11171da177e4SLinus Torvalds if ((unsigned int) info->lo_encrypt_key_size > LO_KEY_SIZE) 11181da177e4SLinus Torvalds return -EINVAL; 11191da177e4SLinus Torvalds 11201da177e4SLinus Torvalds err = loop_release_xfer(lo); 11211da177e4SLinus Torvalds if (err) 11221da177e4SLinus Torvalds return err; 11231da177e4SLinus Torvalds 11241da177e4SLinus Torvalds if (info->lo_encrypt_type) { 11251da177e4SLinus Torvalds unsigned int type = info->lo_encrypt_type; 11261da177e4SLinus Torvalds 11271da177e4SLinus Torvalds if (type >= MAX_LO_CRYPT) 11281da177e4SLinus Torvalds return -EINVAL; 11291da177e4SLinus Torvalds xfer = xfer_funcs[type]; 11301da177e4SLinus Torvalds if (xfer == NULL) 11311da177e4SLinus Torvalds return -EINVAL; 11321da177e4SLinus Torvalds } else 11331da177e4SLinus Torvalds xfer = NULL; 11341da177e4SLinus Torvalds 11351da177e4SLinus Torvalds err = loop_init_xfer(lo, xfer, info); 11361da177e4SLinus Torvalds if (err) 11371da177e4SLinus Torvalds return err; 11381da177e4SLinus Torvalds 11391da177e4SLinus Torvalds if (lo->lo_offset != info->lo_offset || 11401da177e4SLinus Torvalds lo->lo_sizelimit != info->lo_sizelimit) { 11411da177e4SLinus Torvalds lo->lo_offset = info->lo_offset; 11421da177e4SLinus Torvalds lo->lo_sizelimit = info->lo_sizelimit; 11431da177e4SLinus Torvalds if (figure_loop_size(lo)) 11441da177e4SLinus Torvalds return -EFBIG; 11451da177e4SLinus Torvalds } 1146*dfaa2ef6SLukas Czerner loop_config_discard(lo); 11471da177e4SLinus Torvalds 11481da177e4SLinus Torvalds memcpy(lo->lo_file_name, info->lo_file_name, LO_NAME_SIZE); 11491da177e4SLinus Torvalds memcpy(lo->lo_crypt_name, info->lo_crypt_name, LO_NAME_SIZE); 11501da177e4SLinus Torvalds lo->lo_file_name[LO_NAME_SIZE-1] = 0; 11511da177e4SLinus Torvalds lo->lo_crypt_name[LO_NAME_SIZE-1] = 0; 11521da177e4SLinus Torvalds 11531da177e4SLinus Torvalds if (!xfer) 11541da177e4SLinus Torvalds xfer = &none_funcs; 11551da177e4SLinus Torvalds lo->transfer = xfer->transfer; 11561da177e4SLinus Torvalds lo->ioctl = xfer->ioctl; 11571da177e4SLinus Torvalds 115896c58655SDavid Woodhouse if ((lo->lo_flags & LO_FLAGS_AUTOCLEAR) != 115996c58655SDavid Woodhouse (info->lo_flags & LO_FLAGS_AUTOCLEAR)) 116096c58655SDavid Woodhouse lo->lo_flags ^= LO_FLAGS_AUTOCLEAR; 116196c58655SDavid Woodhouse 11621da177e4SLinus Torvalds lo->lo_encrypt_key_size = info->lo_encrypt_key_size; 11631da177e4SLinus Torvalds lo->lo_init[0] = info->lo_init[0]; 11641da177e4SLinus Torvalds lo->lo_init[1] = info->lo_init[1]; 11651da177e4SLinus Torvalds if (info->lo_encrypt_key_size) { 11661da177e4SLinus Torvalds memcpy(lo->lo_encrypt_key, info->lo_encrypt_key, 11671da177e4SLinus Torvalds info->lo_encrypt_key_size); 1168b0fafa81SDavid Howells lo->lo_key_owner = uid; 11691da177e4SLinus Torvalds } 11701da177e4SLinus Torvalds 11711da177e4SLinus Torvalds return 0; 11721da177e4SLinus Torvalds } 11731da177e4SLinus Torvalds 11741da177e4SLinus Torvalds static int 11751da177e4SLinus Torvalds loop_get_status(struct loop_device *lo, struct loop_info64 *info) 11761da177e4SLinus Torvalds { 11771da177e4SLinus Torvalds struct file *file = lo->lo_backing_file; 11781da177e4SLinus Torvalds struct kstat stat; 11791da177e4SLinus Torvalds int error; 11801da177e4SLinus Torvalds 11811da177e4SLinus Torvalds if (lo->lo_state != Lo_bound) 11821da177e4SLinus Torvalds return -ENXIO; 11836c648be6SJosef Sipek error = vfs_getattr(file->f_path.mnt, file->f_path.dentry, &stat); 11841da177e4SLinus Torvalds if (error) 11851da177e4SLinus Torvalds return error; 11861da177e4SLinus Torvalds memset(info, 0, sizeof(*info)); 11871da177e4SLinus Torvalds info->lo_number = lo->lo_number; 11881da177e4SLinus Torvalds info->lo_device = huge_encode_dev(stat.dev); 11891da177e4SLinus Torvalds info->lo_inode = stat.ino; 11901da177e4SLinus Torvalds info->lo_rdevice = huge_encode_dev(lo->lo_device ? stat.rdev : stat.dev); 11911da177e4SLinus Torvalds info->lo_offset = lo->lo_offset; 11921da177e4SLinus Torvalds info->lo_sizelimit = lo->lo_sizelimit; 11931da177e4SLinus Torvalds info->lo_flags = lo->lo_flags; 11941da177e4SLinus Torvalds memcpy(info->lo_file_name, lo->lo_file_name, LO_NAME_SIZE); 11951da177e4SLinus Torvalds memcpy(info->lo_crypt_name, lo->lo_crypt_name, LO_NAME_SIZE); 11961da177e4SLinus Torvalds info->lo_encrypt_type = 11971da177e4SLinus Torvalds lo->lo_encryption ? lo->lo_encryption->number : 0; 11981da177e4SLinus Torvalds if (lo->lo_encrypt_key_size && capable(CAP_SYS_ADMIN)) { 11991da177e4SLinus Torvalds info->lo_encrypt_key_size = lo->lo_encrypt_key_size; 12001da177e4SLinus Torvalds memcpy(info->lo_encrypt_key, lo->lo_encrypt_key, 12011da177e4SLinus Torvalds lo->lo_encrypt_key_size); 12021da177e4SLinus Torvalds } 12031da177e4SLinus Torvalds return 0; 12041da177e4SLinus Torvalds } 12051da177e4SLinus Torvalds 12061da177e4SLinus Torvalds static void 12071da177e4SLinus Torvalds loop_info64_from_old(const struct loop_info *info, struct loop_info64 *info64) 12081da177e4SLinus Torvalds { 12091da177e4SLinus Torvalds memset(info64, 0, sizeof(*info64)); 12101da177e4SLinus Torvalds info64->lo_number = info->lo_number; 12111da177e4SLinus Torvalds info64->lo_device = info->lo_device; 12121da177e4SLinus Torvalds info64->lo_inode = info->lo_inode; 12131da177e4SLinus Torvalds info64->lo_rdevice = info->lo_rdevice; 12141da177e4SLinus Torvalds info64->lo_offset = info->lo_offset; 12151da177e4SLinus Torvalds info64->lo_sizelimit = 0; 12161da177e4SLinus Torvalds info64->lo_encrypt_type = info->lo_encrypt_type; 12171da177e4SLinus Torvalds info64->lo_encrypt_key_size = info->lo_encrypt_key_size; 12181da177e4SLinus Torvalds info64->lo_flags = info->lo_flags; 12191da177e4SLinus Torvalds info64->lo_init[0] = info->lo_init[0]; 12201da177e4SLinus Torvalds info64->lo_init[1] = info->lo_init[1]; 12211da177e4SLinus Torvalds if (info->lo_encrypt_type == LO_CRYPT_CRYPTOAPI) 12221da177e4SLinus Torvalds memcpy(info64->lo_crypt_name, info->lo_name, LO_NAME_SIZE); 12231da177e4SLinus Torvalds else 12241da177e4SLinus Torvalds memcpy(info64->lo_file_name, info->lo_name, LO_NAME_SIZE); 12251da177e4SLinus Torvalds memcpy(info64->lo_encrypt_key, info->lo_encrypt_key, LO_KEY_SIZE); 12261da177e4SLinus Torvalds } 12271da177e4SLinus Torvalds 12281da177e4SLinus Torvalds static int 12291da177e4SLinus Torvalds loop_info64_to_old(const struct loop_info64 *info64, struct loop_info *info) 12301da177e4SLinus Torvalds { 12311da177e4SLinus Torvalds memset(info, 0, sizeof(*info)); 12321da177e4SLinus Torvalds info->lo_number = info64->lo_number; 12331da177e4SLinus Torvalds info->lo_device = info64->lo_device; 12341da177e4SLinus Torvalds info->lo_inode = info64->lo_inode; 12351da177e4SLinus Torvalds info->lo_rdevice = info64->lo_rdevice; 12361da177e4SLinus Torvalds info->lo_offset = info64->lo_offset; 12371da177e4SLinus Torvalds info->lo_encrypt_type = info64->lo_encrypt_type; 12381da177e4SLinus Torvalds info->lo_encrypt_key_size = info64->lo_encrypt_key_size; 12391da177e4SLinus Torvalds info->lo_flags = info64->lo_flags; 12401da177e4SLinus Torvalds info->lo_init[0] = info64->lo_init[0]; 12411da177e4SLinus Torvalds info->lo_init[1] = info64->lo_init[1]; 12421da177e4SLinus Torvalds if (info->lo_encrypt_type == LO_CRYPT_CRYPTOAPI) 12431da177e4SLinus Torvalds memcpy(info->lo_name, info64->lo_crypt_name, LO_NAME_SIZE); 12441da177e4SLinus Torvalds else 12451da177e4SLinus Torvalds memcpy(info->lo_name, info64->lo_file_name, LO_NAME_SIZE); 12461da177e4SLinus Torvalds memcpy(info->lo_encrypt_key, info64->lo_encrypt_key, LO_KEY_SIZE); 12471da177e4SLinus Torvalds 12481da177e4SLinus Torvalds /* error in case values were truncated */ 12491da177e4SLinus Torvalds if (info->lo_device != info64->lo_device || 12501da177e4SLinus Torvalds info->lo_rdevice != info64->lo_rdevice || 12511da177e4SLinus Torvalds info->lo_inode != info64->lo_inode || 12521da177e4SLinus Torvalds info->lo_offset != info64->lo_offset) 12531da177e4SLinus Torvalds return -EOVERFLOW; 12541da177e4SLinus Torvalds 12551da177e4SLinus Torvalds return 0; 12561da177e4SLinus Torvalds } 12571da177e4SLinus Torvalds 12581da177e4SLinus Torvalds static int 12591da177e4SLinus Torvalds loop_set_status_old(struct loop_device *lo, const struct loop_info __user *arg) 12601da177e4SLinus Torvalds { 12611da177e4SLinus Torvalds struct loop_info info; 12621da177e4SLinus Torvalds struct loop_info64 info64; 12631da177e4SLinus Torvalds 12641da177e4SLinus Torvalds if (copy_from_user(&info, arg, sizeof (struct loop_info))) 12651da177e4SLinus Torvalds return -EFAULT; 12661da177e4SLinus Torvalds loop_info64_from_old(&info, &info64); 12671da177e4SLinus Torvalds return loop_set_status(lo, &info64); 12681da177e4SLinus Torvalds } 12691da177e4SLinus Torvalds 12701da177e4SLinus Torvalds static int 12711da177e4SLinus Torvalds loop_set_status64(struct loop_device *lo, const struct loop_info64 __user *arg) 12721da177e4SLinus Torvalds { 12731da177e4SLinus Torvalds struct loop_info64 info64; 12741da177e4SLinus Torvalds 12751da177e4SLinus Torvalds if (copy_from_user(&info64, arg, sizeof (struct loop_info64))) 12761da177e4SLinus Torvalds return -EFAULT; 12771da177e4SLinus Torvalds return loop_set_status(lo, &info64); 12781da177e4SLinus Torvalds } 12791da177e4SLinus Torvalds 12801da177e4SLinus Torvalds static int 12811da177e4SLinus Torvalds loop_get_status_old(struct loop_device *lo, struct loop_info __user *arg) { 12821da177e4SLinus Torvalds struct loop_info info; 12831da177e4SLinus Torvalds struct loop_info64 info64; 12841da177e4SLinus Torvalds int err = 0; 12851da177e4SLinus Torvalds 12861da177e4SLinus Torvalds if (!arg) 12871da177e4SLinus Torvalds err = -EINVAL; 12881da177e4SLinus Torvalds if (!err) 12891da177e4SLinus Torvalds err = loop_get_status(lo, &info64); 12901da177e4SLinus Torvalds if (!err) 12911da177e4SLinus Torvalds err = loop_info64_to_old(&info64, &info); 12921da177e4SLinus Torvalds if (!err && copy_to_user(arg, &info, sizeof(info))) 12931da177e4SLinus Torvalds err = -EFAULT; 12941da177e4SLinus Torvalds 12951da177e4SLinus Torvalds return err; 12961da177e4SLinus Torvalds } 12971da177e4SLinus Torvalds 12981da177e4SLinus Torvalds static int 12991da177e4SLinus Torvalds loop_get_status64(struct loop_device *lo, struct loop_info64 __user *arg) { 13001da177e4SLinus Torvalds struct loop_info64 info64; 13011da177e4SLinus Torvalds int err = 0; 13021da177e4SLinus Torvalds 13031da177e4SLinus Torvalds if (!arg) 13041da177e4SLinus Torvalds err = -EINVAL; 13051da177e4SLinus Torvalds if (!err) 13061da177e4SLinus Torvalds err = loop_get_status(lo, &info64); 13071da177e4SLinus Torvalds if (!err && copy_to_user(arg, &info64, sizeof(info64))) 13081da177e4SLinus Torvalds err = -EFAULT; 13091da177e4SLinus Torvalds 13101da177e4SLinus Torvalds return err; 13111da177e4SLinus Torvalds } 13121da177e4SLinus Torvalds 131353d66608SJ. R. Okajima static int loop_set_capacity(struct loop_device *lo, struct block_device *bdev) 131453d66608SJ. R. Okajima { 131553d66608SJ. R. Okajima int err; 131653d66608SJ. R. Okajima sector_t sec; 131753d66608SJ. R. Okajima loff_t sz; 131853d66608SJ. R. Okajima 131953d66608SJ. R. Okajima err = -ENXIO; 132053d66608SJ. R. Okajima if (unlikely(lo->lo_state != Lo_bound)) 132153d66608SJ. R. Okajima goto out; 132253d66608SJ. R. Okajima err = figure_loop_size(lo); 132353d66608SJ. R. Okajima if (unlikely(err)) 132453d66608SJ. R. Okajima goto out; 132553d66608SJ. R. Okajima sec = get_capacity(lo->lo_disk); 132653d66608SJ. R. Okajima /* the width of sector_t may be narrow for bit-shift */ 132753d66608SJ. R. Okajima sz = sec; 132853d66608SJ. R. Okajima sz <<= 9; 132953d66608SJ. R. Okajima mutex_lock(&bdev->bd_mutex); 133053d66608SJ. R. Okajima bd_set_size(bdev, sz); 1331c3473c63SDavid Zeuthen /* let user-space know about the new size */ 1332c3473c63SDavid Zeuthen kobject_uevent(&disk_to_dev(bdev->bd_disk)->kobj, KOBJ_CHANGE); 133353d66608SJ. R. Okajima mutex_unlock(&bdev->bd_mutex); 133453d66608SJ. R. Okajima 133553d66608SJ. R. Okajima out: 133653d66608SJ. R. Okajima return err; 133753d66608SJ. R. Okajima } 133853d66608SJ. R. Okajima 1339bb214884SAl Viro static int lo_ioctl(struct block_device *bdev, fmode_t mode, 13401da177e4SLinus Torvalds unsigned int cmd, unsigned long arg) 13411da177e4SLinus Torvalds { 1342bb214884SAl Viro struct loop_device *lo = bdev->bd_disk->private_data; 13431da177e4SLinus Torvalds int err; 13441da177e4SLinus Torvalds 1345f028f3b2SNikanth Karthikesan mutex_lock_nested(&lo->lo_ctl_mutex, 1); 13461da177e4SLinus Torvalds switch (cmd) { 13471da177e4SLinus Torvalds case LOOP_SET_FD: 1348bb214884SAl Viro err = loop_set_fd(lo, mode, bdev, arg); 13491da177e4SLinus Torvalds break; 13501da177e4SLinus Torvalds case LOOP_CHANGE_FD: 1351bb214884SAl Viro err = loop_change_fd(lo, bdev, arg); 13521da177e4SLinus Torvalds break; 13531da177e4SLinus Torvalds case LOOP_CLR_FD: 1354f028f3b2SNikanth Karthikesan /* loop_clr_fd would have unlocked lo_ctl_mutex on success */ 1355bb214884SAl Viro err = loop_clr_fd(lo, bdev); 1356f028f3b2SNikanth Karthikesan if (!err) 1357f028f3b2SNikanth Karthikesan goto out_unlocked; 13581da177e4SLinus Torvalds break; 13591da177e4SLinus Torvalds case LOOP_SET_STATUS: 13601da177e4SLinus Torvalds err = loop_set_status_old(lo, (struct loop_info __user *) arg); 13611da177e4SLinus Torvalds break; 13621da177e4SLinus Torvalds case LOOP_GET_STATUS: 13631da177e4SLinus Torvalds err = loop_get_status_old(lo, (struct loop_info __user *) arg); 13641da177e4SLinus Torvalds break; 13651da177e4SLinus Torvalds case LOOP_SET_STATUS64: 13661da177e4SLinus Torvalds err = loop_set_status64(lo, (struct loop_info64 __user *) arg); 13671da177e4SLinus Torvalds break; 13681da177e4SLinus Torvalds case LOOP_GET_STATUS64: 13691da177e4SLinus Torvalds err = loop_get_status64(lo, (struct loop_info64 __user *) arg); 13701da177e4SLinus Torvalds break; 137153d66608SJ. R. Okajima case LOOP_SET_CAPACITY: 137253d66608SJ. R. Okajima err = -EPERM; 137353d66608SJ. R. Okajima if ((mode & FMODE_WRITE) || capable(CAP_SYS_ADMIN)) 137453d66608SJ. R. Okajima err = loop_set_capacity(lo, bdev); 137553d66608SJ. R. Okajima break; 13761da177e4SLinus Torvalds default: 13771da177e4SLinus Torvalds err = lo->ioctl ? lo->ioctl(lo, cmd, arg) : -EINVAL; 13781da177e4SLinus Torvalds } 1379f85221ddSIngo Molnar mutex_unlock(&lo->lo_ctl_mutex); 1380f028f3b2SNikanth Karthikesan 1381f028f3b2SNikanth Karthikesan out_unlocked: 13821da177e4SLinus Torvalds return err; 13831da177e4SLinus Torvalds } 13841da177e4SLinus Torvalds 1385863d5b82SDavid Howells #ifdef CONFIG_COMPAT 1386863d5b82SDavid Howells struct compat_loop_info { 1387863d5b82SDavid Howells compat_int_t lo_number; /* ioctl r/o */ 1388863d5b82SDavid Howells compat_dev_t lo_device; /* ioctl r/o */ 1389863d5b82SDavid Howells compat_ulong_t lo_inode; /* ioctl r/o */ 1390863d5b82SDavid Howells compat_dev_t lo_rdevice; /* ioctl r/o */ 1391863d5b82SDavid Howells compat_int_t lo_offset; 1392863d5b82SDavid Howells compat_int_t lo_encrypt_type; 1393863d5b82SDavid Howells compat_int_t lo_encrypt_key_size; /* ioctl w/o */ 1394863d5b82SDavid Howells compat_int_t lo_flags; /* ioctl r/o */ 1395863d5b82SDavid Howells char lo_name[LO_NAME_SIZE]; 1396863d5b82SDavid Howells unsigned char lo_encrypt_key[LO_KEY_SIZE]; /* ioctl w/o */ 1397863d5b82SDavid Howells compat_ulong_t lo_init[2]; 1398863d5b82SDavid Howells char reserved[4]; 1399863d5b82SDavid Howells }; 1400863d5b82SDavid Howells 1401863d5b82SDavid Howells /* 1402863d5b82SDavid Howells * Transfer 32-bit compatibility structure in userspace to 64-bit loop info 1403863d5b82SDavid Howells * - noinlined to reduce stack space usage in main part of driver 1404863d5b82SDavid Howells */ 1405863d5b82SDavid Howells static noinline int 1406ba674cfcSAl Viro loop_info64_from_compat(const struct compat_loop_info __user *arg, 1407863d5b82SDavid Howells struct loop_info64 *info64) 1408863d5b82SDavid Howells { 1409863d5b82SDavid Howells struct compat_loop_info info; 1410863d5b82SDavid Howells 1411863d5b82SDavid Howells if (copy_from_user(&info, arg, sizeof(info))) 1412863d5b82SDavid Howells return -EFAULT; 1413863d5b82SDavid Howells 1414863d5b82SDavid Howells memset(info64, 0, sizeof(*info64)); 1415863d5b82SDavid Howells info64->lo_number = info.lo_number; 1416863d5b82SDavid Howells info64->lo_device = info.lo_device; 1417863d5b82SDavid Howells info64->lo_inode = info.lo_inode; 1418863d5b82SDavid Howells info64->lo_rdevice = info.lo_rdevice; 1419863d5b82SDavid Howells info64->lo_offset = info.lo_offset; 1420863d5b82SDavid Howells info64->lo_sizelimit = 0; 1421863d5b82SDavid Howells info64->lo_encrypt_type = info.lo_encrypt_type; 1422863d5b82SDavid Howells info64->lo_encrypt_key_size = info.lo_encrypt_key_size; 1423863d5b82SDavid Howells info64->lo_flags = info.lo_flags; 1424863d5b82SDavid Howells info64->lo_init[0] = info.lo_init[0]; 1425863d5b82SDavid Howells info64->lo_init[1] = info.lo_init[1]; 1426863d5b82SDavid Howells if (info.lo_encrypt_type == LO_CRYPT_CRYPTOAPI) 1427863d5b82SDavid Howells memcpy(info64->lo_crypt_name, info.lo_name, LO_NAME_SIZE); 1428863d5b82SDavid Howells else 1429863d5b82SDavid Howells memcpy(info64->lo_file_name, info.lo_name, LO_NAME_SIZE); 1430863d5b82SDavid Howells memcpy(info64->lo_encrypt_key, info.lo_encrypt_key, LO_KEY_SIZE); 1431863d5b82SDavid Howells return 0; 1432863d5b82SDavid Howells } 1433863d5b82SDavid Howells 1434863d5b82SDavid Howells /* 1435863d5b82SDavid Howells * Transfer 64-bit loop info to 32-bit compatibility structure in userspace 1436863d5b82SDavid Howells * - noinlined to reduce stack space usage in main part of driver 1437863d5b82SDavid Howells */ 1438863d5b82SDavid Howells static noinline int 1439863d5b82SDavid Howells loop_info64_to_compat(const struct loop_info64 *info64, 1440863d5b82SDavid Howells struct compat_loop_info __user *arg) 1441863d5b82SDavid Howells { 1442863d5b82SDavid Howells struct compat_loop_info info; 1443863d5b82SDavid Howells 1444863d5b82SDavid Howells memset(&info, 0, sizeof(info)); 1445863d5b82SDavid Howells info.lo_number = info64->lo_number; 1446863d5b82SDavid Howells info.lo_device = info64->lo_device; 1447863d5b82SDavid Howells info.lo_inode = info64->lo_inode; 1448863d5b82SDavid Howells info.lo_rdevice = info64->lo_rdevice; 1449863d5b82SDavid Howells info.lo_offset = info64->lo_offset; 1450863d5b82SDavid Howells info.lo_encrypt_type = info64->lo_encrypt_type; 1451863d5b82SDavid Howells info.lo_encrypt_key_size = info64->lo_encrypt_key_size; 1452863d5b82SDavid Howells info.lo_flags = info64->lo_flags; 1453863d5b82SDavid Howells info.lo_init[0] = info64->lo_init[0]; 1454863d5b82SDavid Howells info.lo_init[1] = info64->lo_init[1]; 1455863d5b82SDavid Howells if (info.lo_encrypt_type == LO_CRYPT_CRYPTOAPI) 1456863d5b82SDavid Howells memcpy(info.lo_name, info64->lo_crypt_name, LO_NAME_SIZE); 1457863d5b82SDavid Howells else 1458863d5b82SDavid Howells memcpy(info.lo_name, info64->lo_file_name, LO_NAME_SIZE); 1459863d5b82SDavid Howells memcpy(info.lo_encrypt_key, info64->lo_encrypt_key, LO_KEY_SIZE); 1460863d5b82SDavid Howells 1461863d5b82SDavid Howells /* error in case values were truncated */ 1462863d5b82SDavid Howells if (info.lo_device != info64->lo_device || 1463863d5b82SDavid Howells info.lo_rdevice != info64->lo_rdevice || 1464863d5b82SDavid Howells info.lo_inode != info64->lo_inode || 1465863d5b82SDavid Howells info.lo_offset != info64->lo_offset || 1466863d5b82SDavid Howells info.lo_init[0] != info64->lo_init[0] || 1467863d5b82SDavid Howells info.lo_init[1] != info64->lo_init[1]) 1468863d5b82SDavid Howells return -EOVERFLOW; 1469863d5b82SDavid Howells 1470863d5b82SDavid Howells if (copy_to_user(arg, &info, sizeof(info))) 1471863d5b82SDavid Howells return -EFAULT; 1472863d5b82SDavid Howells return 0; 1473863d5b82SDavid Howells } 1474863d5b82SDavid Howells 1475863d5b82SDavid Howells static int 1476863d5b82SDavid Howells loop_set_status_compat(struct loop_device *lo, 1477863d5b82SDavid Howells const struct compat_loop_info __user *arg) 1478863d5b82SDavid Howells { 1479863d5b82SDavid Howells struct loop_info64 info64; 1480863d5b82SDavid Howells int ret; 1481863d5b82SDavid Howells 1482863d5b82SDavid Howells ret = loop_info64_from_compat(arg, &info64); 1483863d5b82SDavid Howells if (ret < 0) 1484863d5b82SDavid Howells return ret; 1485863d5b82SDavid Howells return loop_set_status(lo, &info64); 1486863d5b82SDavid Howells } 1487863d5b82SDavid Howells 1488863d5b82SDavid Howells static int 1489863d5b82SDavid Howells loop_get_status_compat(struct loop_device *lo, 1490863d5b82SDavid Howells struct compat_loop_info __user *arg) 1491863d5b82SDavid Howells { 1492863d5b82SDavid Howells struct loop_info64 info64; 1493863d5b82SDavid Howells int err = 0; 1494863d5b82SDavid Howells 1495863d5b82SDavid Howells if (!arg) 1496863d5b82SDavid Howells err = -EINVAL; 1497863d5b82SDavid Howells if (!err) 1498863d5b82SDavid Howells err = loop_get_status(lo, &info64); 1499863d5b82SDavid Howells if (!err) 1500863d5b82SDavid Howells err = loop_info64_to_compat(&info64, arg); 1501863d5b82SDavid Howells return err; 1502863d5b82SDavid Howells } 1503863d5b82SDavid Howells 1504bb214884SAl Viro static int lo_compat_ioctl(struct block_device *bdev, fmode_t mode, 1505bb214884SAl Viro unsigned int cmd, unsigned long arg) 1506863d5b82SDavid Howells { 1507bb214884SAl Viro struct loop_device *lo = bdev->bd_disk->private_data; 1508863d5b82SDavid Howells int err; 1509863d5b82SDavid Howells 1510863d5b82SDavid Howells switch(cmd) { 1511863d5b82SDavid Howells case LOOP_SET_STATUS: 1512863d5b82SDavid Howells mutex_lock(&lo->lo_ctl_mutex); 1513863d5b82SDavid Howells err = loop_set_status_compat( 1514863d5b82SDavid Howells lo, (const struct compat_loop_info __user *) arg); 1515863d5b82SDavid Howells mutex_unlock(&lo->lo_ctl_mutex); 1516863d5b82SDavid Howells break; 1517863d5b82SDavid Howells case LOOP_GET_STATUS: 1518863d5b82SDavid Howells mutex_lock(&lo->lo_ctl_mutex); 1519863d5b82SDavid Howells err = loop_get_status_compat( 1520863d5b82SDavid Howells lo, (struct compat_loop_info __user *) arg); 1521863d5b82SDavid Howells mutex_unlock(&lo->lo_ctl_mutex); 1522863d5b82SDavid Howells break; 152353d66608SJ. R. Okajima case LOOP_SET_CAPACITY: 1524863d5b82SDavid Howells case LOOP_CLR_FD: 1525863d5b82SDavid Howells case LOOP_GET_STATUS64: 1526863d5b82SDavid Howells case LOOP_SET_STATUS64: 1527863d5b82SDavid Howells arg = (unsigned long) compat_ptr(arg); 1528863d5b82SDavid Howells case LOOP_SET_FD: 1529863d5b82SDavid Howells case LOOP_CHANGE_FD: 1530bb214884SAl Viro err = lo_ioctl(bdev, mode, cmd, arg); 1531863d5b82SDavid Howells break; 1532863d5b82SDavid Howells default: 1533863d5b82SDavid Howells err = -ENOIOCTLCMD; 1534863d5b82SDavid Howells break; 1535863d5b82SDavid Howells } 1536863d5b82SDavid Howells return err; 1537863d5b82SDavid Howells } 1538863d5b82SDavid Howells #endif 1539863d5b82SDavid Howells 1540bb214884SAl Viro static int lo_open(struct block_device *bdev, fmode_t mode) 15411da177e4SLinus Torvalds { 1542bb214884SAl Viro struct loop_device *lo = bdev->bd_disk->private_data; 15431da177e4SLinus Torvalds 1544f85221ddSIngo Molnar mutex_lock(&lo->lo_ctl_mutex); 15451da177e4SLinus Torvalds lo->lo_refcnt++; 1546f85221ddSIngo Molnar mutex_unlock(&lo->lo_ctl_mutex); 15471da177e4SLinus Torvalds 15481da177e4SLinus Torvalds return 0; 15491da177e4SLinus Torvalds } 15501da177e4SLinus Torvalds 1551bb214884SAl Viro static int lo_release(struct gendisk *disk, fmode_t mode) 15521da177e4SLinus Torvalds { 1553bb214884SAl Viro struct loop_device *lo = disk->private_data; 1554ffcd7dcaSAlexander Beregalov int err; 15551da177e4SLinus Torvalds 1556f85221ddSIngo Molnar mutex_lock(&lo->lo_ctl_mutex); 155796c58655SDavid Woodhouse 155814f27939SMilan Broz if (--lo->lo_refcnt) 155914f27939SMilan Broz goto out; 156014f27939SMilan Broz 156114f27939SMilan Broz if (lo->lo_flags & LO_FLAGS_AUTOCLEAR) { 156214f27939SMilan Broz /* 156314f27939SMilan Broz * In autoclear mode, stop the loop thread 156414f27939SMilan Broz * and remove configuration after last close. 156514f27939SMilan Broz */ 1566ffcd7dcaSAlexander Beregalov err = loop_clr_fd(lo, NULL); 1567ffcd7dcaSAlexander Beregalov if (!err) 1568ffcd7dcaSAlexander Beregalov goto out_unlocked; 156914f27939SMilan Broz } else { 157014f27939SMilan Broz /* 157114f27939SMilan Broz * Otherwise keep thread (if running) and config, 157214f27939SMilan Broz * but flush possible ongoing bios in thread. 157314f27939SMilan Broz */ 157414f27939SMilan Broz loop_flush(lo); 157514f27939SMilan Broz } 157696c58655SDavid Woodhouse 157714f27939SMilan Broz out: 1578f85221ddSIngo Molnar mutex_unlock(&lo->lo_ctl_mutex); 1579ffcd7dcaSAlexander Beregalov out_unlocked: 15801da177e4SLinus Torvalds return 0; 15811da177e4SLinus Torvalds } 15821da177e4SLinus Torvalds 158383d5cde4SAlexey Dobriyan static const struct block_device_operations lo_fops = { 15841da177e4SLinus Torvalds .owner = THIS_MODULE, 1585bb214884SAl Viro .open = lo_open, 1586bb214884SAl Viro .release = lo_release, 1587bb214884SAl Viro .ioctl = lo_ioctl, 1588863d5b82SDavid Howells #ifdef CONFIG_COMPAT 1589bb214884SAl Viro .compat_ioctl = lo_compat_ioctl, 1590863d5b82SDavid Howells #endif 15911da177e4SLinus Torvalds }; 15921da177e4SLinus Torvalds 15931da177e4SLinus Torvalds /* 15941da177e4SLinus Torvalds * And now the modules code and kernel interface. 15951da177e4SLinus Torvalds */ 159673285082SKen Chen static int max_loop; 1597ac04fee0SNamhyung Kim module_param(max_loop, int, S_IRUGO); 1598a47653fcSKen Chen MODULE_PARM_DESC(max_loop, "Maximum number of loop devices"); 1599ac04fee0SNamhyung Kim module_param(max_part, int, S_IRUGO); 1600476a4813SLaurent Vivier MODULE_PARM_DESC(max_part, "Maximum number of partitions per loop device"); 16011da177e4SLinus Torvalds MODULE_LICENSE("GPL"); 16021da177e4SLinus Torvalds MODULE_ALIAS_BLOCKDEV_MAJOR(LOOP_MAJOR); 16031da177e4SLinus Torvalds 16041da177e4SLinus Torvalds int loop_register_transfer(struct loop_func_table *funcs) 16051da177e4SLinus Torvalds { 16061da177e4SLinus Torvalds unsigned int n = funcs->number; 16071da177e4SLinus Torvalds 16081da177e4SLinus Torvalds if (n >= MAX_LO_CRYPT || xfer_funcs[n]) 16091da177e4SLinus Torvalds return -EINVAL; 16101da177e4SLinus Torvalds xfer_funcs[n] = funcs; 16111da177e4SLinus Torvalds return 0; 16121da177e4SLinus Torvalds } 16131da177e4SLinus Torvalds 16141da177e4SLinus Torvalds int loop_unregister_transfer(int number) 16151da177e4SLinus Torvalds { 16161da177e4SLinus Torvalds unsigned int n = number; 16171da177e4SLinus Torvalds struct loop_device *lo; 16181da177e4SLinus Torvalds struct loop_func_table *xfer; 16191da177e4SLinus Torvalds 16201da177e4SLinus Torvalds if (n == 0 || n >= MAX_LO_CRYPT || (xfer = xfer_funcs[n]) == NULL) 16211da177e4SLinus Torvalds return -EINVAL; 16221da177e4SLinus Torvalds 16231da177e4SLinus Torvalds xfer_funcs[n] = NULL; 16241da177e4SLinus Torvalds 162573285082SKen Chen list_for_each_entry(lo, &loop_devices, lo_list) { 1626f85221ddSIngo Molnar mutex_lock(&lo->lo_ctl_mutex); 16271da177e4SLinus Torvalds 16281da177e4SLinus Torvalds if (lo->lo_encryption == xfer) 16291da177e4SLinus Torvalds loop_release_xfer(lo); 16301da177e4SLinus Torvalds 1631f85221ddSIngo Molnar mutex_unlock(&lo->lo_ctl_mutex); 16321da177e4SLinus Torvalds } 16331da177e4SLinus Torvalds 16341da177e4SLinus Torvalds return 0; 16351da177e4SLinus Torvalds } 16361da177e4SLinus Torvalds 16371da177e4SLinus Torvalds EXPORT_SYMBOL(loop_register_transfer); 16381da177e4SLinus Torvalds EXPORT_SYMBOL(loop_unregister_transfer); 16391da177e4SLinus Torvalds 1640a47653fcSKen Chen static struct loop_device *loop_alloc(int i) 16411da177e4SLinus Torvalds { 164273285082SKen Chen struct loop_device *lo; 164373285082SKen Chen struct gendisk *disk; 16441da177e4SLinus Torvalds 164573285082SKen Chen lo = kzalloc(sizeof(*lo), GFP_KERNEL); 164673285082SKen Chen if (!lo) 164773285082SKen Chen goto out; 16481da177e4SLinus Torvalds 16491da177e4SLinus Torvalds lo->lo_queue = blk_alloc_queue(GFP_KERNEL); 16501da177e4SLinus Torvalds if (!lo->lo_queue) 165173285082SKen Chen goto out_free_dev; 165273285082SKen Chen 1653476a4813SLaurent Vivier disk = lo->lo_disk = alloc_disk(1 << part_shift); 165473285082SKen Chen if (!disk) 165573285082SKen Chen goto out_free_queue; 165673285082SKen Chen 1657f85221ddSIngo Molnar mutex_init(&lo->lo_ctl_mutex); 16581da177e4SLinus Torvalds lo->lo_number = i; 16596c997918SSerge E. Hallyn lo->lo_thread = NULL; 16606c997918SSerge E. Hallyn init_waitqueue_head(&lo->lo_event); 16611da177e4SLinus Torvalds spin_lock_init(&lo->lo_lock); 16621da177e4SLinus Torvalds disk->major = LOOP_MAJOR; 1663476a4813SLaurent Vivier disk->first_minor = i << part_shift; 16641da177e4SLinus Torvalds disk->fops = &lo_fops; 16651da177e4SLinus Torvalds disk->private_data = lo; 16661da177e4SLinus Torvalds disk->queue = lo->lo_queue; 166773285082SKen Chen sprintf(disk->disk_name, "loop%d", i); 166873285082SKen Chen return lo; 166973285082SKen Chen 167073285082SKen Chen out_free_queue: 167173285082SKen Chen blk_cleanup_queue(lo->lo_queue); 167273285082SKen Chen out_free_dev: 167373285082SKen Chen kfree(lo); 167473285082SKen Chen out: 167507002e99SAl Viro return NULL; 16761da177e4SLinus Torvalds } 16771da177e4SLinus Torvalds 1678a47653fcSKen Chen static void loop_free(struct loop_device *lo) 167973285082SKen Chen { 168073285082SKen Chen blk_cleanup_queue(lo->lo_queue); 168173285082SKen Chen put_disk(lo->lo_disk); 168273285082SKen Chen list_del(&lo->lo_list); 168373285082SKen Chen kfree(lo); 168473285082SKen Chen } 168573285082SKen Chen 1686a47653fcSKen Chen static struct loop_device *loop_init_one(int i) 1687a47653fcSKen Chen { 1688a47653fcSKen Chen struct loop_device *lo; 1689a47653fcSKen Chen 1690a47653fcSKen Chen list_for_each_entry(lo, &loop_devices, lo_list) { 1691a47653fcSKen Chen if (lo->lo_number == i) 1692a47653fcSKen Chen return lo; 1693a47653fcSKen Chen } 1694a47653fcSKen Chen 1695a47653fcSKen Chen lo = loop_alloc(i); 1696a47653fcSKen Chen if (lo) { 1697a47653fcSKen Chen add_disk(lo->lo_disk); 1698a47653fcSKen Chen list_add_tail(&lo->lo_list, &loop_devices); 1699a47653fcSKen Chen } 1700a47653fcSKen Chen return lo; 1701a47653fcSKen Chen } 1702a47653fcSKen Chen 1703a47653fcSKen Chen static void loop_del_one(struct loop_device *lo) 1704a47653fcSKen Chen { 1705a47653fcSKen Chen del_gendisk(lo->lo_disk); 1706a47653fcSKen Chen loop_free(lo); 1707a47653fcSKen Chen } 1708a47653fcSKen Chen 170973285082SKen Chen static struct kobject *loop_probe(dev_t dev, int *part, void *data) 171073285082SKen Chen { 1711705962ccSAl Viro struct loop_device *lo; 171207002e99SAl Viro struct kobject *kobj; 171373285082SKen Chen 1714705962ccSAl Viro mutex_lock(&loop_devices_mutex); 1715a1c15c59SNamhyung Kim lo = loop_init_one(MINOR(dev) >> part_shift); 171607002e99SAl Viro kobj = lo ? get_disk(lo->lo_disk) : ERR_PTR(-ENOMEM); 171773285082SKen Chen mutex_unlock(&loop_devices_mutex); 171873285082SKen Chen 171973285082SKen Chen *part = 0; 172007002e99SAl Viro return kobj; 172173285082SKen Chen } 172273285082SKen Chen 172373285082SKen Chen static int __init loop_init(void) 172473285082SKen Chen { 1725a47653fcSKen Chen int i, nr; 1726a47653fcSKen Chen unsigned long range; 1727a47653fcSKen Chen struct loop_device *lo, *next; 1728a47653fcSKen Chen 1729a47653fcSKen Chen /* 1730a47653fcSKen Chen * loop module now has a feature to instantiate underlying device 1731a47653fcSKen Chen * structure on-demand, provided that there is an access dev node. 1732a47653fcSKen Chen * However, this will not work well with user space tool that doesn't 1733a47653fcSKen Chen * know about such "feature". In order to not break any existing 1734a47653fcSKen Chen * tool, we do the following: 1735a47653fcSKen Chen * 1736a47653fcSKen Chen * (1) if max_loop is specified, create that many upfront, and this 1737a47653fcSKen Chen * also becomes a hard limit. 1738a47653fcSKen Chen * (2) if max_loop is not specified, create 8 loop device on module 1739a47653fcSKen Chen * load, user can further extend loop device by create dev node 1740a47653fcSKen Chen * themselves and have kernel automatically instantiate actual 1741a47653fcSKen Chen * device on-demand. 1742a47653fcSKen Chen */ 1743476a4813SLaurent Vivier 1744476a4813SLaurent Vivier part_shift = 0; 1745ac04fee0SNamhyung Kim if (max_part > 0) { 1746476a4813SLaurent Vivier part_shift = fls(max_part); 1747476a4813SLaurent Vivier 1748ac04fee0SNamhyung Kim /* 1749ac04fee0SNamhyung Kim * Adjust max_part according to part_shift as it is exported 1750ac04fee0SNamhyung Kim * to user space so that user can decide correct minor number 1751ac04fee0SNamhyung Kim * if [s]he want to create more devices. 1752ac04fee0SNamhyung Kim * 1753ac04fee0SNamhyung Kim * Note that -1 is required because partition 0 is reserved 1754ac04fee0SNamhyung Kim * for the whole disk. 1755ac04fee0SNamhyung Kim */ 1756ac04fee0SNamhyung Kim max_part = (1UL << part_shift) - 1; 1757ac04fee0SNamhyung Kim } 1758ac04fee0SNamhyung Kim 175978f4bb36SNamhyung Kim if ((1UL << part_shift) > DISK_MAX_PARTS) 176078f4bb36SNamhyung Kim return -EINVAL; 176178f4bb36SNamhyung Kim 1762476a4813SLaurent Vivier if (max_loop > 1UL << (MINORBITS - part_shift)) 1763a47653fcSKen Chen return -EINVAL; 176473285082SKen Chen 176573285082SKen Chen if (max_loop) { 1766a47653fcSKen Chen nr = max_loop; 1767a1c15c59SNamhyung Kim range = max_loop << part_shift; 1768a47653fcSKen Chen } else { 1769a47653fcSKen Chen nr = 8; 1770a1c15c59SNamhyung Kim range = 1UL << MINORBITS; 177173285082SKen Chen } 1772a47653fcSKen Chen 1773a47653fcSKen Chen if (register_blkdev(LOOP_MAJOR, "loop")) 1774a47653fcSKen Chen return -EIO; 1775a47653fcSKen Chen 1776a47653fcSKen Chen for (i = 0; i < nr; i++) { 1777a47653fcSKen Chen lo = loop_alloc(i); 1778a47653fcSKen Chen if (!lo) 1779a47653fcSKen Chen goto Enomem; 1780a47653fcSKen Chen list_add_tail(&lo->lo_list, &loop_devices); 1781a47653fcSKen Chen } 1782a47653fcSKen Chen 1783a47653fcSKen Chen /* point of no return */ 1784a47653fcSKen Chen 1785a47653fcSKen Chen list_for_each_entry(lo, &loop_devices, lo_list) 1786a47653fcSKen Chen add_disk(lo->lo_disk); 1787a47653fcSKen Chen 1788a47653fcSKen Chen blk_register_region(MKDEV(LOOP_MAJOR, 0), range, 1789a47653fcSKen Chen THIS_MODULE, loop_probe, NULL, NULL); 1790a47653fcSKen Chen 179173285082SKen Chen printk(KERN_INFO "loop: module loaded\n"); 17921da177e4SLinus Torvalds return 0; 1793a47653fcSKen Chen 1794a47653fcSKen Chen Enomem: 1795a47653fcSKen Chen printk(KERN_INFO "loop: out of memory\n"); 1796a47653fcSKen Chen 1797a47653fcSKen Chen list_for_each_entry_safe(lo, next, &loop_devices, lo_list) 1798a47653fcSKen Chen loop_free(lo); 1799a47653fcSKen Chen 1800a47653fcSKen Chen unregister_blkdev(LOOP_MAJOR, "loop"); 1801a47653fcSKen Chen return -ENOMEM; 18021da177e4SLinus Torvalds } 18031da177e4SLinus Torvalds 180473285082SKen Chen static void __exit loop_exit(void) 18051da177e4SLinus Torvalds { 1806a47653fcSKen Chen unsigned long range; 180773285082SKen Chen struct loop_device *lo, *next; 18081da177e4SLinus Torvalds 1809a1c15c59SNamhyung Kim range = max_loop ? max_loop << part_shift : 1UL << MINORBITS; 1810a47653fcSKen Chen 181173285082SKen Chen list_for_each_entry_safe(lo, next, &loop_devices, lo_list) 181273285082SKen Chen loop_del_one(lo); 181373285082SKen Chen 1814a47653fcSKen Chen blk_unregister_region(MKDEV(LOOP_MAJOR, 0), range); 181500d59405SAkinobu Mita unregister_blkdev(LOOP_MAJOR, "loop"); 18161da177e4SLinus Torvalds } 18171da177e4SLinus Torvalds 18181da177e4SLinus Torvalds module_init(loop_init); 18191da177e4SLinus Torvalds module_exit(loop_exit); 18201da177e4SLinus Torvalds 18211da177e4SLinus Torvalds #ifndef MODULE 18221da177e4SLinus Torvalds static int __init max_loop_setup(char *str) 18231da177e4SLinus Torvalds { 18241da177e4SLinus Torvalds max_loop = simple_strtol(str, NULL, 0); 18251da177e4SLinus Torvalds return 1; 18261da177e4SLinus Torvalds } 18271da177e4SLinus Torvalds 18281da177e4SLinus Torvalds __setup("max_loop=", max_loop_setup); 18291da177e4SLinus Torvalds #endif 1830