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> 66863d5b82SDavid Howells #include <linux/compat.h> 671da177e4SLinus Torvalds #include <linux/suspend.h> 6883144186SRafael J. Wysocki #include <linux/freezer.h> 692a48fc0aSArnd Bergmann #include <linux/mutex.h> 701da177e4SLinus Torvalds #include <linux/writeback.h> 711da177e4SLinus Torvalds #include <linux/completion.h> 721da177e4SLinus Torvalds #include <linux/highmem.h> 736c997918SSerge E. Hallyn #include <linux/kthread.h> 74d6b29d7cSJens Axboe #include <linux/splice.h> 75ee862730SMilan Broz #include <linux/sysfs.h> 76770fe30aSKay Sievers #include <linux/miscdevice.h> 77dfaa2ef6SLukas Czerner #include <linux/falloc.h> 78283e7e5dSAl Viro #include <linux/uio.h> 7983a87611SAl Viro #include "loop.h" 801da177e4SLinus Torvalds 817c0f6ba6SLinus Torvalds #include <linux/uaccess.h> 821da177e4SLinus Torvalds 8334dd82afSKay Sievers static DEFINE_IDR(loop_index_idr); 8434dd82afSKay Sievers static DEFINE_MUTEX(loop_index_mutex); 851da177e4SLinus Torvalds 86476a4813SLaurent Vivier static int max_part; 87476a4813SLaurent Vivier static int part_shift; 88476a4813SLaurent Vivier 891da177e4SLinus Torvalds static int transfer_xor(struct loop_device *lo, int cmd, 901da177e4SLinus Torvalds struct page *raw_page, unsigned raw_off, 911da177e4SLinus Torvalds struct page *loop_page, unsigned loop_off, 921da177e4SLinus Torvalds int size, sector_t real_block) 931da177e4SLinus Torvalds { 94cfd8005cSCong Wang char *raw_buf = kmap_atomic(raw_page) + raw_off; 95cfd8005cSCong Wang char *loop_buf = kmap_atomic(loop_page) + loop_off; 961da177e4SLinus Torvalds char *in, *out, *key; 971da177e4SLinus Torvalds int i, keysize; 981da177e4SLinus Torvalds 991da177e4SLinus Torvalds if (cmd == READ) { 1001da177e4SLinus Torvalds in = raw_buf; 1011da177e4SLinus Torvalds out = loop_buf; 1021da177e4SLinus Torvalds } else { 1031da177e4SLinus Torvalds in = loop_buf; 1041da177e4SLinus Torvalds out = raw_buf; 1051da177e4SLinus Torvalds } 1061da177e4SLinus Torvalds 1071da177e4SLinus Torvalds key = lo->lo_encrypt_key; 1081da177e4SLinus Torvalds keysize = lo->lo_encrypt_key_size; 1091da177e4SLinus Torvalds for (i = 0; i < size; i++) 1101da177e4SLinus Torvalds *out++ = *in++ ^ key[(i & 511) % keysize]; 1111da177e4SLinus Torvalds 112cfd8005cSCong Wang kunmap_atomic(loop_buf); 113cfd8005cSCong Wang kunmap_atomic(raw_buf); 1141da177e4SLinus Torvalds cond_resched(); 1151da177e4SLinus Torvalds return 0; 1161da177e4SLinus Torvalds } 1171da177e4SLinus Torvalds 1181da177e4SLinus Torvalds static int xor_init(struct loop_device *lo, const struct loop_info64 *info) 1191da177e4SLinus Torvalds { 1201da177e4SLinus Torvalds if (unlikely(info->lo_encrypt_key_size <= 0)) 1211da177e4SLinus Torvalds return -EINVAL; 1221da177e4SLinus Torvalds return 0; 1231da177e4SLinus Torvalds } 1241da177e4SLinus Torvalds 1251da177e4SLinus Torvalds static struct loop_func_table none_funcs = { 1261da177e4SLinus Torvalds .number = LO_CRYPT_NONE, 1271da177e4SLinus Torvalds }; 1281da177e4SLinus Torvalds 1291da177e4SLinus Torvalds static struct loop_func_table xor_funcs = { 1301da177e4SLinus Torvalds .number = LO_CRYPT_XOR, 1311da177e4SLinus Torvalds .transfer = transfer_xor, 1321da177e4SLinus Torvalds .init = xor_init 1331da177e4SLinus Torvalds }; 1341da177e4SLinus Torvalds 1351da177e4SLinus Torvalds /* xfer_funcs[0] is special - its release function is never called */ 1361da177e4SLinus Torvalds static struct loop_func_table *xfer_funcs[MAX_LO_CRYPT] = { 1371da177e4SLinus Torvalds &none_funcs, 1381da177e4SLinus Torvalds &xor_funcs 1391da177e4SLinus Torvalds }; 1401da177e4SLinus Torvalds 1417035b5dfSDmitry Monakhov static loff_t get_size(loff_t offset, loff_t sizelimit, struct file *file) 1421da177e4SLinus Torvalds { 143b7a1da69SGuo Chao loff_t loopsize; 1441da177e4SLinus Torvalds 1451da177e4SLinus Torvalds /* Compute loopsize in bytes */ 146b7a1da69SGuo Chao loopsize = i_size_read(file->f_mapping->host); 147b7a1da69SGuo Chao if (offset > 0) 148b7a1da69SGuo Chao loopsize -= offset; 149b7a1da69SGuo Chao /* offset is beyond i_size, weird but possible */ 1507035b5dfSDmitry Monakhov if (loopsize < 0) 1517035b5dfSDmitry Monakhov return 0; 1521da177e4SLinus Torvalds 1537035b5dfSDmitry Monakhov if (sizelimit > 0 && sizelimit < loopsize) 1547035b5dfSDmitry Monakhov loopsize = sizelimit; 1551da177e4SLinus Torvalds /* 1561da177e4SLinus Torvalds * Unfortunately, if we want to do I/O on the device, 1571da177e4SLinus Torvalds * the number of 512-byte sectors has to fit into a sector_t. 1581da177e4SLinus Torvalds */ 1591da177e4SLinus Torvalds return loopsize >> 9; 1601da177e4SLinus Torvalds } 1611da177e4SLinus Torvalds 1627035b5dfSDmitry Monakhov static loff_t get_loop_size(struct loop_device *lo, struct file *file) 1631da177e4SLinus Torvalds { 1647035b5dfSDmitry Monakhov return get_size(lo->lo_offset, lo->lo_sizelimit, file); 1657035b5dfSDmitry Monakhov } 1667035b5dfSDmitry Monakhov 1672e5ab5f3SMing Lei static void __loop_update_dio(struct loop_device *lo, bool dio) 1682e5ab5f3SMing Lei { 1692e5ab5f3SMing Lei struct file *file = lo->lo_backing_file; 1702e5ab5f3SMing Lei struct address_space *mapping = file->f_mapping; 1712e5ab5f3SMing Lei struct inode *inode = mapping->host; 1722e5ab5f3SMing Lei unsigned short sb_bsize = 0; 1732e5ab5f3SMing Lei unsigned dio_align = 0; 1742e5ab5f3SMing Lei bool use_dio; 1752e5ab5f3SMing Lei 1762e5ab5f3SMing Lei if (inode->i_sb->s_bdev) { 1772e5ab5f3SMing Lei sb_bsize = bdev_logical_block_size(inode->i_sb->s_bdev); 1782e5ab5f3SMing Lei dio_align = sb_bsize - 1; 1792e5ab5f3SMing Lei } 1802e5ab5f3SMing Lei 1812e5ab5f3SMing Lei /* 1822e5ab5f3SMing Lei * We support direct I/O only if lo_offset is aligned with the 1832e5ab5f3SMing Lei * logical I/O size of backing device, and the logical block 1842e5ab5f3SMing Lei * size of loop is bigger than the backing device's and the loop 1852e5ab5f3SMing Lei * needn't transform transfer. 1862e5ab5f3SMing Lei * 1872e5ab5f3SMing Lei * TODO: the above condition may be loosed in the future, and 1882e5ab5f3SMing Lei * direct I/O may be switched runtime at that time because most 18989d790abSMasahiro Yamada * of requests in sane applications should be PAGE_SIZE aligned 1902e5ab5f3SMing Lei */ 1912e5ab5f3SMing Lei if (dio) { 1922e5ab5f3SMing Lei if (queue_logical_block_size(lo->lo_queue) >= sb_bsize && 1932e5ab5f3SMing Lei !(lo->lo_offset & dio_align) && 1942e5ab5f3SMing Lei mapping->a_ops->direct_IO && 1952e5ab5f3SMing Lei !lo->transfer) 1962e5ab5f3SMing Lei use_dio = true; 1972e5ab5f3SMing Lei else 1982e5ab5f3SMing Lei use_dio = false; 1992e5ab5f3SMing Lei } else { 2002e5ab5f3SMing Lei use_dio = false; 2012e5ab5f3SMing Lei } 2022e5ab5f3SMing Lei 2032e5ab5f3SMing Lei if (lo->use_dio == use_dio) 2042e5ab5f3SMing Lei return; 2052e5ab5f3SMing Lei 2062e5ab5f3SMing Lei /* flush dirty pages before changing direct IO */ 2072e5ab5f3SMing Lei vfs_fsync(file, 0); 2082e5ab5f3SMing Lei 2092e5ab5f3SMing Lei /* 2102e5ab5f3SMing Lei * The flag of LO_FLAGS_DIRECT_IO is handled similarly with 2112e5ab5f3SMing Lei * LO_FLAGS_READ_ONLY, both are set from kernel, and losetup 2122e5ab5f3SMing Lei * will get updated by ioctl(LOOP_GET_STATUS) 2132e5ab5f3SMing Lei */ 2142e5ab5f3SMing Lei blk_mq_freeze_queue(lo->lo_queue); 2152e5ab5f3SMing Lei lo->use_dio = use_dio; 2162e5ab5f3SMing Lei if (use_dio) 2172e5ab5f3SMing Lei lo->lo_flags |= LO_FLAGS_DIRECT_IO; 2182e5ab5f3SMing Lei else 2192e5ab5f3SMing Lei lo->lo_flags &= ~LO_FLAGS_DIRECT_IO; 2202e5ab5f3SMing Lei blk_mq_unfreeze_queue(lo->lo_queue); 2212e5ab5f3SMing Lei } 2222e5ab5f3SMing Lei 2237035b5dfSDmitry Monakhov static int 2247035b5dfSDmitry Monakhov figure_loop_size(struct loop_device *lo, loff_t offset, loff_t sizelimit) 2257035b5dfSDmitry Monakhov { 2267035b5dfSDmitry Monakhov loff_t size = get_size(offset, sizelimit, lo->lo_backing_file); 2271da177e4SLinus Torvalds sector_t x = (sector_t)size; 2287b0576a3SGuo Chao struct block_device *bdev = lo->lo_device; 2291da177e4SLinus Torvalds 2301da177e4SLinus Torvalds if (unlikely((loff_t)x != size)) 2311da177e4SLinus Torvalds return -EFBIG; 2327035b5dfSDmitry Monakhov if (lo->lo_offset != offset) 2337035b5dfSDmitry Monakhov lo->lo_offset = offset; 2347035b5dfSDmitry Monakhov if (lo->lo_sizelimit != sizelimit) 2357035b5dfSDmitry Monakhov lo->lo_sizelimit = sizelimit; 23673285082SKen Chen set_capacity(lo->lo_disk, x); 2377b0576a3SGuo Chao bd_set_size(bdev, (loff_t)get_capacity(bdev->bd_disk) << 9); 2387b0576a3SGuo Chao /* let user-space know about the new size */ 2397b0576a3SGuo Chao kobject_uevent(&disk_to_dev(bdev->bd_disk)->kobj, KOBJ_CHANGE); 2401da177e4SLinus Torvalds return 0; 2411da177e4SLinus Torvalds } 2421da177e4SLinus Torvalds 2431da177e4SLinus Torvalds static inline int 2441da177e4SLinus Torvalds lo_do_transfer(struct loop_device *lo, int cmd, 2451da177e4SLinus Torvalds struct page *rpage, unsigned roffs, 2461da177e4SLinus Torvalds struct page *lpage, unsigned loffs, 2471da177e4SLinus Torvalds int size, sector_t rblock) 2481da177e4SLinus Torvalds { 249aa4d8616SChristoph Hellwig int ret; 250aa4d8616SChristoph Hellwig 251aa4d8616SChristoph Hellwig ret = lo->transfer(lo, cmd, rpage, roffs, lpage, loffs, size, rblock); 252aa4d8616SChristoph Hellwig if (likely(!ret)) 2531da177e4SLinus Torvalds return 0; 2541da177e4SLinus Torvalds 255aa4d8616SChristoph Hellwig printk_ratelimited(KERN_ERR 256aa4d8616SChristoph Hellwig "loop: Transfer error at byte offset %llu, length %i.\n", 257aa4d8616SChristoph Hellwig (unsigned long long)rblock << 9, size); 258aa4d8616SChristoph Hellwig return ret; 2591da177e4SLinus Torvalds } 2601da177e4SLinus Torvalds 261aa4d8616SChristoph Hellwig static int lo_write_bvec(struct file *file, struct bio_vec *bvec, loff_t *ppos) 2621da177e4SLinus Torvalds { 263aa4d8616SChristoph Hellwig struct iov_iter i; 2641da177e4SLinus Torvalds ssize_t bw; 265283e7e5dSAl Viro 266aa4d8616SChristoph Hellwig iov_iter_bvec(&i, ITER_BVEC, bvec, 1, bvec->bv_len); 2671da177e4SLinus Torvalds 26803d95eb2SAl Viro file_start_write(file); 269*abbb6589SChristoph Hellwig bw = vfs_iter_write(file, &i, ppos, 0); 27003d95eb2SAl Viro file_end_write(file); 271aa4d8616SChristoph Hellwig 272aa4d8616SChristoph Hellwig if (likely(bw == bvec->bv_len)) 2731da177e4SLinus Torvalds return 0; 274aa4d8616SChristoph Hellwig 275aa4d8616SChristoph Hellwig printk_ratelimited(KERN_ERR 276aa4d8616SChristoph Hellwig "loop: Write error at byte offset %llu, length %i.\n", 277aa4d8616SChristoph Hellwig (unsigned long long)*ppos, bvec->bv_len); 2781da177e4SLinus Torvalds if (bw >= 0) 2791da177e4SLinus Torvalds bw = -EIO; 2801da177e4SLinus Torvalds return bw; 2811da177e4SLinus Torvalds } 2821da177e4SLinus Torvalds 283aa4d8616SChristoph Hellwig static int lo_write_simple(struct loop_device *lo, struct request *rq, 284aa4d8616SChristoph Hellwig loff_t pos) 2851da177e4SLinus Torvalds { 286aa4d8616SChristoph Hellwig struct bio_vec bvec; 287aa4d8616SChristoph Hellwig struct req_iterator iter; 288aa4d8616SChristoph Hellwig int ret = 0; 289aa4d8616SChristoph Hellwig 290aa4d8616SChristoph Hellwig rq_for_each_segment(bvec, rq, iter) { 291aa4d8616SChristoph Hellwig ret = lo_write_bvec(lo->lo_backing_file, &bvec, &pos); 292aa4d8616SChristoph Hellwig if (ret < 0) 293aa4d8616SChristoph Hellwig break; 2941da177e4SLinus Torvalds cond_resched(); 2951da177e4SLinus Torvalds } 2961da177e4SLinus Torvalds 297aa4d8616SChristoph Hellwig return ret; 298aa4d8616SChristoph Hellwig } 299aa4d8616SChristoph Hellwig 300aa4d8616SChristoph Hellwig /* 301456be148SChristoph Hellwig * This is the slow, transforming version that needs to double buffer the 302456be148SChristoph Hellwig * data as it cannot do the transformations in place without having direct 303456be148SChristoph Hellwig * access to the destination pages of the backing file. 3041da177e4SLinus Torvalds */ 305aa4d8616SChristoph Hellwig static int lo_write_transfer(struct loop_device *lo, struct request *rq, 306aa4d8616SChristoph Hellwig loff_t pos) 3071da177e4SLinus Torvalds { 308aa4d8616SChristoph Hellwig struct bio_vec bvec, b; 30930112013SMing Lei struct req_iterator iter; 310aa4d8616SChristoph Hellwig struct page *page; 3117988613bSKent Overstreet int ret = 0; 3121da177e4SLinus Torvalds 313aa4d8616SChristoph Hellwig page = alloc_page(GFP_NOIO); 3141da177e4SLinus Torvalds if (unlikely(!page)) 315aa4d8616SChristoph Hellwig return -ENOMEM; 316456be148SChristoph Hellwig 31730112013SMing Lei rq_for_each_segment(bvec, rq, iter) { 318aa4d8616SChristoph Hellwig ret = lo_do_transfer(lo, WRITE, page, 0, bvec.bv_page, 319aa4d8616SChristoph Hellwig bvec.bv_offset, bvec.bv_len, pos >> 9); 320aa4d8616SChristoph Hellwig if (unlikely(ret)) 321aa4d8616SChristoph Hellwig break; 322aa4d8616SChristoph Hellwig 323aa4d8616SChristoph Hellwig b.bv_page = page; 324aa4d8616SChristoph Hellwig b.bv_offset = 0; 325aa4d8616SChristoph Hellwig b.bv_len = bvec.bv_len; 326aa4d8616SChristoph Hellwig ret = lo_write_bvec(lo->lo_backing_file, &b, &pos); 3271da177e4SLinus Torvalds if (ret < 0) 3281da177e4SLinus Torvalds break; 3291da177e4SLinus Torvalds } 330aa4d8616SChristoph Hellwig 3311da177e4SLinus Torvalds __free_page(page); 3321da177e4SLinus Torvalds return ret; 3331da177e4SLinus Torvalds } 3341da177e4SLinus Torvalds 335aa4d8616SChristoph Hellwig static int lo_read_simple(struct loop_device *lo, struct request *rq, 336aa4d8616SChristoph Hellwig loff_t pos) 3371da177e4SLinus Torvalds { 3387988613bSKent Overstreet struct bio_vec bvec; 33930112013SMing Lei struct req_iterator iter; 340aa4d8616SChristoph Hellwig struct iov_iter i; 341aa4d8616SChristoph Hellwig ssize_t len; 3421da177e4SLinus Torvalds 34330112013SMing Lei rq_for_each_segment(bvec, rq, iter) { 344aa4d8616SChristoph Hellwig iov_iter_bvec(&i, ITER_BVEC, &bvec, 1, bvec.bv_len); 34518e9710eSChristoph Hellwig len = vfs_iter_read(lo->lo_backing_file, &i, &pos, 0); 346aa4d8616SChristoph Hellwig if (len < 0) 347aa4d8616SChristoph Hellwig return len; 348306df071SDave Young 349aa4d8616SChristoph Hellwig flush_dcache_page(bvec.bv_page); 350aa4d8616SChristoph Hellwig 351aa4d8616SChristoph Hellwig if (len != bvec.bv_len) { 35230112013SMing Lei struct bio *bio; 35330112013SMing Lei 35430112013SMing Lei __rq_for_each_bio(bio, rq) 355306df071SDave Young zero_fill_bio(bio); 3561da177e4SLinus Torvalds break; 357306df071SDave Young } 358aa4d8616SChristoph Hellwig cond_resched(); 3591da177e4SLinus Torvalds } 360aa4d8616SChristoph Hellwig 361306df071SDave Young return 0; 3621da177e4SLinus Torvalds } 3631da177e4SLinus Torvalds 364aa4d8616SChristoph Hellwig static int lo_read_transfer(struct loop_device *lo, struct request *rq, 365aa4d8616SChristoph Hellwig loff_t pos) 366aa4d8616SChristoph Hellwig { 367aa4d8616SChristoph Hellwig struct bio_vec bvec, b; 368aa4d8616SChristoph Hellwig struct req_iterator iter; 369aa4d8616SChristoph Hellwig struct iov_iter i; 370aa4d8616SChristoph Hellwig struct page *page; 371aa4d8616SChristoph Hellwig ssize_t len; 372aa4d8616SChristoph Hellwig int ret = 0; 373aa4d8616SChristoph Hellwig 374aa4d8616SChristoph Hellwig page = alloc_page(GFP_NOIO); 375aa4d8616SChristoph Hellwig if (unlikely(!page)) 376aa4d8616SChristoph Hellwig return -ENOMEM; 377aa4d8616SChristoph Hellwig 378aa4d8616SChristoph Hellwig rq_for_each_segment(bvec, rq, iter) { 379aa4d8616SChristoph Hellwig loff_t offset = pos; 380aa4d8616SChristoph Hellwig 381aa4d8616SChristoph Hellwig b.bv_page = page; 382aa4d8616SChristoph Hellwig b.bv_offset = 0; 383aa4d8616SChristoph Hellwig b.bv_len = bvec.bv_len; 384aa4d8616SChristoph Hellwig 385aa4d8616SChristoph Hellwig iov_iter_bvec(&i, ITER_BVEC, &b, 1, b.bv_len); 38618e9710eSChristoph Hellwig len = vfs_iter_read(lo->lo_backing_file, &i, &pos, 0); 387aa4d8616SChristoph Hellwig if (len < 0) { 388aa4d8616SChristoph Hellwig ret = len; 389aa4d8616SChristoph Hellwig goto out_free_page; 390aa4d8616SChristoph Hellwig } 391aa4d8616SChristoph Hellwig 392aa4d8616SChristoph Hellwig ret = lo_do_transfer(lo, READ, page, 0, bvec.bv_page, 393aa4d8616SChristoph Hellwig bvec.bv_offset, len, offset >> 9); 394aa4d8616SChristoph Hellwig if (ret) 395aa4d8616SChristoph Hellwig goto out_free_page; 396aa4d8616SChristoph Hellwig 397aa4d8616SChristoph Hellwig flush_dcache_page(bvec.bv_page); 398aa4d8616SChristoph Hellwig 399aa4d8616SChristoph Hellwig if (len != bvec.bv_len) { 400aa4d8616SChristoph Hellwig struct bio *bio; 401aa4d8616SChristoph Hellwig 402aa4d8616SChristoph Hellwig __rq_for_each_bio(bio, rq) 403aa4d8616SChristoph Hellwig zero_fill_bio(bio); 404aa4d8616SChristoph Hellwig break; 405aa4d8616SChristoph Hellwig } 406aa4d8616SChristoph Hellwig } 407aa4d8616SChristoph Hellwig 408aa4d8616SChristoph Hellwig ret = 0; 409aa4d8616SChristoph Hellwig out_free_page: 410aa4d8616SChristoph Hellwig __free_page(page); 411aa4d8616SChristoph Hellwig return ret; 412aa4d8616SChristoph Hellwig } 413aa4d8616SChristoph Hellwig 414cf655d95SMing Lei static int lo_discard(struct loop_device *lo, struct request *rq, loff_t pos) 415cf655d95SMing Lei { 416cf655d95SMing Lei /* 417cf655d95SMing Lei * We use punch hole to reclaim the free space used by the 418cf655d95SMing Lei * image a.k.a. discard. However we do not support discard if 419cf655d95SMing Lei * encryption is enabled, because it may give an attacker 420cf655d95SMing Lei * useful information. 421cf655d95SMing Lei */ 422cf655d95SMing Lei struct file *file = lo->lo_backing_file; 423cf655d95SMing Lei int mode = FALLOC_FL_PUNCH_HOLE | FALLOC_FL_KEEP_SIZE; 424cf655d95SMing Lei int ret; 425cf655d95SMing Lei 426cf655d95SMing Lei if ((!file->f_op->fallocate) || lo->lo_encrypt_key_size) { 427cf655d95SMing Lei ret = -EOPNOTSUPP; 428cf655d95SMing Lei goto out; 429cf655d95SMing Lei } 430cf655d95SMing Lei 431cf655d95SMing Lei ret = file->f_op->fallocate(file, mode, pos, blk_rq_bytes(rq)); 432cf655d95SMing Lei if (unlikely(ret && ret != -EINVAL && ret != -EOPNOTSUPP)) 433cf655d95SMing Lei ret = -EIO; 434cf655d95SMing Lei out: 435cf655d95SMing Lei return ret; 436cf655d95SMing Lei } 437cf655d95SMing Lei 438cf655d95SMing Lei static int lo_req_flush(struct loop_device *lo, struct request *rq) 439cf655d95SMing Lei { 440cf655d95SMing Lei struct file *file = lo->lo_backing_file; 441cf655d95SMing Lei int ret = vfs_fsync(file, 0); 442cf655d95SMing Lei if (unlikely(ret && ret != -EINVAL)) 443cf655d95SMing Lei ret = -EIO; 444cf655d95SMing Lei 445cf655d95SMing Lei return ret; 446cf655d95SMing Lei } 447cf655d95SMing Lei 448fe2cb290SChristoph Hellwig static void lo_complete_rq(struct request *rq) 449bc07c10aSMing Lei { 450fe2cb290SChristoph Hellwig struct loop_cmd *cmd = blk_mq_rq_to_pdu(rq); 451bc07c10aSMing Lei 452fe2cb290SChristoph Hellwig if (unlikely(req_op(cmd->rq) == REQ_OP_READ && cmd->use_aio && 453fe2cb290SChristoph Hellwig cmd->ret >= 0 && cmd->ret < blk_rq_bytes(cmd->rq))) { 454bc07c10aSMing Lei struct bio *bio = cmd->rq->bio; 455bc07c10aSMing Lei 456fe2cb290SChristoph Hellwig bio_advance(bio, cmd->ret); 457bc07c10aSMing Lei zero_fill_bio(bio); 458bc07c10aSMing Lei } 459fe2cb290SChristoph Hellwig 460fe2cb290SChristoph Hellwig blk_mq_end_request(rq, cmd->ret < 0 ? -EIO : 0); 461bc07c10aSMing Lei } 462bc07c10aSMing Lei 463bc07c10aSMing Lei static void lo_rw_aio_complete(struct kiocb *iocb, long ret, long ret2) 464bc07c10aSMing Lei { 465bc07c10aSMing Lei struct loop_cmd *cmd = container_of(iocb, struct loop_cmd, iocb); 466bc07c10aSMing Lei 467fe2cb290SChristoph Hellwig cmd->ret = ret; 46808e0029aSChristoph Hellwig blk_mq_complete_request(cmd->rq); 469bc07c10aSMing Lei } 470bc07c10aSMing Lei 471bc07c10aSMing Lei static int lo_rw_aio(struct loop_device *lo, struct loop_cmd *cmd, 472bc07c10aSMing Lei loff_t pos, bool rw) 473bc07c10aSMing Lei { 474bc07c10aSMing Lei struct iov_iter iter; 475bc07c10aSMing Lei struct bio_vec *bvec; 476bc07c10aSMing Lei struct bio *bio = cmd->rq->bio; 477bc07c10aSMing Lei struct file *file = lo->lo_backing_file; 478bc07c10aSMing Lei int ret; 479bc07c10aSMing Lei 480bc07c10aSMing Lei /* nomerge for loop request queue */ 481bc07c10aSMing Lei WARN_ON(cmd->rq->bio != cmd->rq->biotail); 482bc07c10aSMing Lei 483bc07c10aSMing Lei bvec = __bvec_iter_bvec(bio->bi_io_vec, bio->bi_iter); 484bc07c10aSMing Lei iov_iter_bvec(&iter, ITER_BVEC | rw, bvec, 485bc07c10aSMing Lei bio_segments(bio), blk_rq_bytes(cmd->rq)); 486a7297a6aSMing Lei /* 487a7297a6aSMing Lei * This bio may be started from the middle of the 'bvec' 488a7297a6aSMing Lei * because of bio splitting, so offset from the bvec must 489a7297a6aSMing Lei * be passed to iov iterator 490a7297a6aSMing Lei */ 491a7297a6aSMing Lei iter.iov_offset = bio->bi_iter.bi_bvec_done; 492bc07c10aSMing Lei 493bc07c10aSMing Lei cmd->iocb.ki_pos = pos; 494bc07c10aSMing Lei cmd->iocb.ki_filp = file; 495bc07c10aSMing Lei cmd->iocb.ki_complete = lo_rw_aio_complete; 496bc07c10aSMing Lei cmd->iocb.ki_flags = IOCB_DIRECT; 497bc07c10aSMing Lei 498bc07c10aSMing Lei if (rw == WRITE) 499bb7462b6SMiklos Szeredi ret = call_write_iter(file, &cmd->iocb, &iter); 500bc07c10aSMing Lei else 501bb7462b6SMiklos Szeredi ret = call_read_iter(file, &cmd->iocb, &iter); 502bc07c10aSMing Lei 503bc07c10aSMing Lei if (ret != -EIOCBQUEUED) 504bc07c10aSMing Lei cmd->iocb.ki_complete(&cmd->iocb, ret, 0); 505bc07c10aSMing Lei return 0; 506bc07c10aSMing Lei } 507bc07c10aSMing Lei 508c1c87c2bSChristoph Hellwig static int do_req_filebacked(struct loop_device *lo, struct request *rq) 509bc07c10aSMing Lei { 510bc07c10aSMing Lei struct loop_cmd *cmd = blk_mq_rq_to_pdu(rq); 511c1c87c2bSChristoph Hellwig loff_t pos = ((loff_t) blk_rq_pos(rq) << 9) + lo->lo_offset; 512bc07c10aSMing Lei 513bc07c10aSMing Lei /* 514bc07c10aSMing Lei * lo_write_simple and lo_read_simple should have been covered 515bc07c10aSMing Lei * by io submit style function like lo_rw_aio(), one blocker 516bc07c10aSMing Lei * is that lo_read_simple() need to call flush_dcache_page after 517bc07c10aSMing Lei * the page is written from kernel, and it isn't easy to handle 518bc07c10aSMing Lei * this in io submit style function which submits all segments 519bc07c10aSMing Lei * of the req at one time. And direct read IO doesn't need to 520bc07c10aSMing Lei * run flush_dcache_page(). 521bc07c10aSMing Lei */ 522c1c87c2bSChristoph Hellwig switch (req_op(rq)) { 523c1c87c2bSChristoph Hellwig case REQ_OP_FLUSH: 524c1c87c2bSChristoph Hellwig return lo_req_flush(lo, rq); 525c1c87c2bSChristoph Hellwig case REQ_OP_DISCARD: 52619372e27SChristoph Hellwig case REQ_OP_WRITE_ZEROES: 527c1c87c2bSChristoph Hellwig return lo_discard(lo, rq, pos); 528c1c87c2bSChristoph Hellwig case REQ_OP_WRITE: 529c1c87c2bSChristoph Hellwig if (lo->transfer) 530c1c87c2bSChristoph Hellwig return lo_write_transfer(lo, rq, pos); 531c1c87c2bSChristoph Hellwig else if (cmd->use_aio) 532c1c87c2bSChristoph Hellwig return lo_rw_aio(lo, cmd, pos, WRITE); 533c1c87c2bSChristoph Hellwig else 534bc07c10aSMing Lei return lo_write_simple(lo, rq, pos); 535c1c87c2bSChristoph Hellwig case REQ_OP_READ: 536c1c87c2bSChristoph Hellwig if (lo->transfer) 537c1c87c2bSChristoph Hellwig return lo_read_transfer(lo, rq, pos); 538c1c87c2bSChristoph Hellwig else if (cmd->use_aio) 539c1c87c2bSChristoph Hellwig return lo_rw_aio(lo, cmd, pos, READ); 540bc07c10aSMing Lei else 541bc07c10aSMing Lei return lo_read_simple(lo, rq, pos); 542c1c87c2bSChristoph Hellwig default: 543c1c87c2bSChristoph Hellwig WARN_ON_ONCE(1); 544c1c87c2bSChristoph Hellwig return -EIO; 545c1c87c2bSChristoph Hellwig break; 546bc07c10aSMing Lei } 5471da177e4SLinus Torvalds } 5481da177e4SLinus Torvalds 5491da177e4SLinus Torvalds struct switch_request { 5501da177e4SLinus Torvalds struct file *file; 5511da177e4SLinus Torvalds struct completion wait; 5521da177e4SLinus Torvalds }; 5531da177e4SLinus Torvalds 5542e5ab5f3SMing Lei static inline void loop_update_dio(struct loop_device *lo) 5552e5ab5f3SMing Lei { 5562e5ab5f3SMing Lei __loop_update_dio(lo, io_is_direct(lo->lo_backing_file) | 5572e5ab5f3SMing Lei lo->use_dio); 5582e5ab5f3SMing Lei } 5592e5ab5f3SMing Lei 56014f27939SMilan Broz /* 5611da177e4SLinus Torvalds * Do the actual switch; called from the BIO completion routine 5621da177e4SLinus Torvalds */ 5631da177e4SLinus Torvalds static void do_loop_switch(struct loop_device *lo, struct switch_request *p) 5641da177e4SLinus Torvalds { 5651da177e4SLinus Torvalds struct file *file = p->file; 5661da177e4SLinus Torvalds struct file *old_file = lo->lo_backing_file; 56714f27939SMilan Broz struct address_space *mapping; 5681da177e4SLinus Torvalds 56914f27939SMilan Broz /* if no new file, only flush of queued bios requested */ 57014f27939SMilan Broz if (!file) 571b5dd2f60SMing Lei return; 57214f27939SMilan Broz 57314f27939SMilan Broz mapping = file->f_mapping; 5741da177e4SLinus Torvalds mapping_set_gfp_mask(old_file->f_mapping, lo->old_gfp_mask); 5751da177e4SLinus Torvalds lo->lo_backing_file = file; 576ba52de12STheodore Ts'o lo->lo_blocksize = S_ISBLK(mapping->host->i_mode) ? 577ba52de12STheodore Ts'o mapping->host->i_bdev->bd_block_size : PAGE_SIZE; 5781da177e4SLinus Torvalds lo->old_gfp_mask = mapping_gfp_mask(mapping); 5791da177e4SLinus Torvalds mapping_set_gfp_mask(mapping, lo->old_gfp_mask & ~(__GFP_IO|__GFP_FS)); 5802e5ab5f3SMing Lei loop_update_dio(lo); 5811da177e4SLinus Torvalds } 5821da177e4SLinus Torvalds 583b5dd2f60SMing Lei /* 584b5dd2f60SMing Lei * loop_switch performs the hard work of switching a backing store. 585b5dd2f60SMing Lei * First it needs to flush existing IO, it does this by sending a magic 586b5dd2f60SMing Lei * BIO down the pipe. The completion of this BIO does the actual switch. 587b5dd2f60SMing Lei */ 588b5dd2f60SMing Lei static int loop_switch(struct loop_device *lo, struct file *file) 589b5dd2f60SMing Lei { 590b5dd2f60SMing Lei struct switch_request w; 591b5dd2f60SMing Lei 592b5dd2f60SMing Lei w.file = file; 593b5dd2f60SMing Lei 594b5dd2f60SMing Lei /* freeze queue and wait for completion of scheduled requests */ 595b5dd2f60SMing Lei blk_mq_freeze_queue(lo->lo_queue); 596b5dd2f60SMing Lei 597b5dd2f60SMing Lei /* do the switch action */ 598b5dd2f60SMing Lei do_loop_switch(lo, &w); 599b5dd2f60SMing Lei 600b5dd2f60SMing Lei /* unfreeze */ 601b5dd2f60SMing Lei blk_mq_unfreeze_queue(lo->lo_queue); 602b5dd2f60SMing Lei 603b5dd2f60SMing Lei return 0; 604b5dd2f60SMing Lei } 605b5dd2f60SMing Lei 606b5dd2f60SMing Lei /* 607b5dd2f60SMing Lei * Helper to flush the IOs in loop, but keeping loop thread running 608b5dd2f60SMing Lei */ 609b5dd2f60SMing Lei static int loop_flush(struct loop_device *lo) 610b5dd2f60SMing Lei { 61164604957SJames Wang /* loop not yet configured, no running thread, nothing to flush */ 61264604957SJames Wang if (lo->lo_state != Lo_bound) 61364604957SJames Wang return 0; 614b5dd2f60SMing Lei return loop_switch(lo, NULL); 615b5dd2f60SMing Lei } 6161da177e4SLinus Torvalds 61706f0e9e6SMing Lei static void loop_reread_partitions(struct loop_device *lo, 61806f0e9e6SMing Lei struct block_device *bdev) 61906f0e9e6SMing Lei { 62006f0e9e6SMing Lei int rc; 62106f0e9e6SMing Lei 62206f0e9e6SMing Lei /* 62306f0e9e6SMing Lei * bd_mutex has been held already in release path, so don't 62406f0e9e6SMing Lei * acquire it if this function is called in such case. 62506f0e9e6SMing Lei * 62606f0e9e6SMing Lei * If the reread partition isn't from release path, lo_refcnt 62706f0e9e6SMing Lei * must be at least one and it can only become zero when the 62806f0e9e6SMing Lei * current holder is released. 62906f0e9e6SMing Lei */ 63006f0e9e6SMing Lei if (!atomic_read(&lo->lo_refcnt)) 63106f0e9e6SMing Lei rc = __blkdev_reread_part(bdev); 63206f0e9e6SMing Lei else 63306f0e9e6SMing Lei rc = blkdev_reread_part(bdev); 63406f0e9e6SMing Lei if (rc) 63506f0e9e6SMing Lei pr_warn("%s: partition scan of loop%d (%s) failed (rc=%d)\n", 63606f0e9e6SMing Lei __func__, lo->lo_number, lo->lo_file_name, rc); 63706f0e9e6SMing Lei } 63806f0e9e6SMing Lei 6391da177e4SLinus Torvalds /* 6401da177e4SLinus Torvalds * loop_change_fd switched the backing store of a loopback device to 6411da177e4SLinus Torvalds * a new file. This is useful for operating system installers to free up 6421da177e4SLinus Torvalds * the original file and in High Availability environments to switch to 6431da177e4SLinus Torvalds * an alternative location for the content in case of server meltdown. 6441da177e4SLinus Torvalds * This can only work if the loop device is used read-only, and if the 6451da177e4SLinus Torvalds * new backing store is the same size and type as the old backing store. 6461da177e4SLinus Torvalds */ 647bb214884SAl Viro static int loop_change_fd(struct loop_device *lo, struct block_device *bdev, 648bb214884SAl Viro unsigned int arg) 6491da177e4SLinus Torvalds { 6501da177e4SLinus Torvalds struct file *file, *old_file; 6511da177e4SLinus Torvalds struct inode *inode; 6521da177e4SLinus Torvalds int error; 6531da177e4SLinus Torvalds 6541da177e4SLinus Torvalds error = -ENXIO; 6551da177e4SLinus Torvalds if (lo->lo_state != Lo_bound) 6561da177e4SLinus Torvalds goto out; 6571da177e4SLinus Torvalds 6581da177e4SLinus Torvalds /* the loop device has to be read-only */ 6591da177e4SLinus Torvalds error = -EINVAL; 6601da177e4SLinus Torvalds if (!(lo->lo_flags & LO_FLAGS_READ_ONLY)) 6611da177e4SLinus Torvalds goto out; 6621da177e4SLinus Torvalds 6631da177e4SLinus Torvalds error = -EBADF; 6641da177e4SLinus Torvalds file = fget(arg); 6651da177e4SLinus Torvalds if (!file) 6661da177e4SLinus Torvalds goto out; 6671da177e4SLinus Torvalds 6681da177e4SLinus Torvalds inode = file->f_mapping->host; 6691da177e4SLinus Torvalds old_file = lo->lo_backing_file; 6701da177e4SLinus Torvalds 6711da177e4SLinus Torvalds error = -EINVAL; 6721da177e4SLinus Torvalds 6731da177e4SLinus Torvalds if (!S_ISREG(inode->i_mode) && !S_ISBLK(inode->i_mode)) 6741da177e4SLinus Torvalds goto out_putf; 6751da177e4SLinus Torvalds 6761da177e4SLinus Torvalds /* size of the new backing store needs to be the same */ 6771da177e4SLinus Torvalds if (get_loop_size(lo, file) != get_loop_size(lo, old_file)) 6781da177e4SLinus Torvalds goto out_putf; 6791da177e4SLinus Torvalds 6801da177e4SLinus Torvalds /* and ... switch */ 6811da177e4SLinus Torvalds error = loop_switch(lo, file); 6821da177e4SLinus Torvalds if (error) 6831da177e4SLinus Torvalds goto out_putf; 6841da177e4SLinus Torvalds 6851da177e4SLinus Torvalds fput(old_file); 686e03c8dd1SKay Sievers if (lo->lo_flags & LO_FLAGS_PARTSCAN) 68706f0e9e6SMing Lei loop_reread_partitions(lo, bdev); 6881da177e4SLinus Torvalds return 0; 6891da177e4SLinus Torvalds 6901da177e4SLinus Torvalds out_putf: 6911da177e4SLinus Torvalds fput(file); 6921da177e4SLinus Torvalds out: 6931da177e4SLinus Torvalds return error; 6941da177e4SLinus Torvalds } 6951da177e4SLinus Torvalds 6961da177e4SLinus Torvalds static inline int is_loop_device(struct file *file) 6971da177e4SLinus Torvalds { 6981da177e4SLinus Torvalds struct inode *i = file->f_mapping->host; 6991da177e4SLinus Torvalds 7001da177e4SLinus Torvalds return i && S_ISBLK(i->i_mode) && MAJOR(i->i_rdev) == LOOP_MAJOR; 7011da177e4SLinus Torvalds } 7021da177e4SLinus Torvalds 703ee862730SMilan Broz /* loop sysfs attributes */ 704ee862730SMilan Broz 705ee862730SMilan Broz static ssize_t loop_attr_show(struct device *dev, char *page, 706ee862730SMilan Broz ssize_t (*callback)(struct loop_device *, char *)) 707ee862730SMilan Broz { 70834dd82afSKay Sievers struct gendisk *disk = dev_to_disk(dev); 70934dd82afSKay Sievers struct loop_device *lo = disk->private_data; 710ee862730SMilan Broz 71134dd82afSKay Sievers return callback(lo, page); 712ee862730SMilan Broz } 713ee862730SMilan Broz 714ee862730SMilan Broz #define LOOP_ATTR_RO(_name) \ 715ee862730SMilan Broz static ssize_t loop_attr_##_name##_show(struct loop_device *, char *); \ 716ee862730SMilan Broz static ssize_t loop_attr_do_show_##_name(struct device *d, \ 717ee862730SMilan Broz struct device_attribute *attr, char *b) \ 718ee862730SMilan Broz { \ 719ee862730SMilan Broz return loop_attr_show(d, b, loop_attr_##_name##_show); \ 720ee862730SMilan Broz } \ 721ee862730SMilan Broz static struct device_attribute loop_attr_##_name = \ 722ee862730SMilan Broz __ATTR(_name, S_IRUGO, loop_attr_do_show_##_name, NULL); 723ee862730SMilan Broz 724ee862730SMilan Broz static ssize_t loop_attr_backing_file_show(struct loop_device *lo, char *buf) 725ee862730SMilan Broz { 726ee862730SMilan Broz ssize_t ret; 727ee862730SMilan Broz char *p = NULL; 728ee862730SMilan Broz 72905eb0f25SKay Sievers spin_lock_irq(&lo->lo_lock); 730ee862730SMilan Broz if (lo->lo_backing_file) 7319bf39ab2SMiklos Szeredi p = file_path(lo->lo_backing_file, buf, PAGE_SIZE - 1); 73205eb0f25SKay Sievers spin_unlock_irq(&lo->lo_lock); 733ee862730SMilan Broz 734ee862730SMilan Broz if (IS_ERR_OR_NULL(p)) 735ee862730SMilan Broz ret = PTR_ERR(p); 736ee862730SMilan Broz else { 737ee862730SMilan Broz ret = strlen(p); 738ee862730SMilan Broz memmove(buf, p, ret); 739ee862730SMilan Broz buf[ret++] = '\n'; 740ee862730SMilan Broz buf[ret] = 0; 741ee862730SMilan Broz } 742ee862730SMilan Broz 743ee862730SMilan Broz return ret; 744ee862730SMilan Broz } 745ee862730SMilan Broz 746ee862730SMilan Broz static ssize_t loop_attr_offset_show(struct loop_device *lo, char *buf) 747ee862730SMilan Broz { 748ee862730SMilan Broz return sprintf(buf, "%llu\n", (unsigned long long)lo->lo_offset); 749ee862730SMilan Broz } 750ee862730SMilan Broz 751ee862730SMilan Broz static ssize_t loop_attr_sizelimit_show(struct loop_device *lo, char *buf) 752ee862730SMilan Broz { 753ee862730SMilan Broz return sprintf(buf, "%llu\n", (unsigned long long)lo->lo_sizelimit); 754ee862730SMilan Broz } 755ee862730SMilan Broz 756ee862730SMilan Broz static ssize_t loop_attr_autoclear_show(struct loop_device *lo, char *buf) 757ee862730SMilan Broz { 758ee862730SMilan Broz int autoclear = (lo->lo_flags & LO_FLAGS_AUTOCLEAR); 759ee862730SMilan Broz 760ee862730SMilan Broz return sprintf(buf, "%s\n", autoclear ? "1" : "0"); 761ee862730SMilan Broz } 762ee862730SMilan Broz 763e03c8dd1SKay Sievers static ssize_t loop_attr_partscan_show(struct loop_device *lo, char *buf) 764e03c8dd1SKay Sievers { 765e03c8dd1SKay Sievers int partscan = (lo->lo_flags & LO_FLAGS_PARTSCAN); 766e03c8dd1SKay Sievers 767e03c8dd1SKay Sievers return sprintf(buf, "%s\n", partscan ? "1" : "0"); 768e03c8dd1SKay Sievers } 769e03c8dd1SKay Sievers 7702e5ab5f3SMing Lei static ssize_t loop_attr_dio_show(struct loop_device *lo, char *buf) 7712e5ab5f3SMing Lei { 7722e5ab5f3SMing Lei int dio = (lo->lo_flags & LO_FLAGS_DIRECT_IO); 7732e5ab5f3SMing Lei 7742e5ab5f3SMing Lei return sprintf(buf, "%s\n", dio ? "1" : "0"); 7752e5ab5f3SMing Lei } 7762e5ab5f3SMing Lei 777ee862730SMilan Broz LOOP_ATTR_RO(backing_file); 778ee862730SMilan Broz LOOP_ATTR_RO(offset); 779ee862730SMilan Broz LOOP_ATTR_RO(sizelimit); 780ee862730SMilan Broz LOOP_ATTR_RO(autoclear); 781e03c8dd1SKay Sievers LOOP_ATTR_RO(partscan); 7822e5ab5f3SMing Lei LOOP_ATTR_RO(dio); 783ee862730SMilan Broz 784ee862730SMilan Broz static struct attribute *loop_attrs[] = { 785ee862730SMilan Broz &loop_attr_backing_file.attr, 786ee862730SMilan Broz &loop_attr_offset.attr, 787ee862730SMilan Broz &loop_attr_sizelimit.attr, 788ee862730SMilan Broz &loop_attr_autoclear.attr, 789e03c8dd1SKay Sievers &loop_attr_partscan.attr, 7902e5ab5f3SMing Lei &loop_attr_dio.attr, 791ee862730SMilan Broz NULL, 792ee862730SMilan Broz }; 793ee862730SMilan Broz 794ee862730SMilan Broz static struct attribute_group loop_attribute_group = { 795ee862730SMilan Broz .name = "loop", 796ee862730SMilan Broz .attrs= loop_attrs, 797ee862730SMilan Broz }; 798ee862730SMilan Broz 799ee862730SMilan Broz static int loop_sysfs_init(struct loop_device *lo) 800ee862730SMilan Broz { 801ee862730SMilan Broz return sysfs_create_group(&disk_to_dev(lo->lo_disk)->kobj, 802ee862730SMilan Broz &loop_attribute_group); 803ee862730SMilan Broz } 804ee862730SMilan Broz 805ee862730SMilan Broz static void loop_sysfs_exit(struct loop_device *lo) 806ee862730SMilan Broz { 807ee862730SMilan Broz sysfs_remove_group(&disk_to_dev(lo->lo_disk)->kobj, 808ee862730SMilan Broz &loop_attribute_group); 809ee862730SMilan Broz } 810ee862730SMilan Broz 811dfaa2ef6SLukas Czerner static void loop_config_discard(struct loop_device *lo) 812dfaa2ef6SLukas Czerner { 813dfaa2ef6SLukas Czerner struct file *file = lo->lo_backing_file; 814dfaa2ef6SLukas Czerner struct inode *inode = file->f_mapping->host; 815dfaa2ef6SLukas Czerner struct request_queue *q = lo->lo_queue; 816dfaa2ef6SLukas Czerner 817dfaa2ef6SLukas Czerner /* 818dfaa2ef6SLukas Czerner * We use punch hole to reclaim the free space used by the 81912a64d2fSOlaf Hering * image a.k.a. discard. However we do not support discard if 820dfaa2ef6SLukas Czerner * encryption is enabled, because it may give an attacker 821dfaa2ef6SLukas Czerner * useful information. 822dfaa2ef6SLukas Czerner */ 823dfaa2ef6SLukas Czerner if ((!file->f_op->fallocate) || 824dfaa2ef6SLukas Czerner lo->lo_encrypt_key_size) { 825dfaa2ef6SLukas Czerner q->limits.discard_granularity = 0; 826dfaa2ef6SLukas Czerner q->limits.discard_alignment = 0; 8272bb4cd5cSJens Axboe blk_queue_max_discard_sectors(q, 0); 82819372e27SChristoph Hellwig blk_queue_max_write_zeroes_sectors(q, 0); 829dfaa2ef6SLukas Czerner queue_flag_clear_unlocked(QUEUE_FLAG_DISCARD, q); 830dfaa2ef6SLukas Czerner return; 831dfaa2ef6SLukas Czerner } 832dfaa2ef6SLukas Czerner 833dfaa2ef6SLukas Czerner q->limits.discard_granularity = inode->i_sb->s_blocksize; 834dfaf3c03SLukas Czerner q->limits.discard_alignment = 0; 8352bb4cd5cSJens Axboe blk_queue_max_discard_sectors(q, UINT_MAX >> 9); 83619372e27SChristoph Hellwig blk_queue_max_write_zeroes_sectors(q, UINT_MAX >> 9); 837dfaa2ef6SLukas Czerner queue_flag_set_unlocked(QUEUE_FLAG_DISCARD, q); 838dfaa2ef6SLukas Czerner } 839dfaa2ef6SLukas Czerner 840e03a3d7aSMing Lei static void loop_unprepare_queue(struct loop_device *lo) 841e03a3d7aSMing Lei { 8423989144fSPetr Mladek kthread_flush_worker(&lo->worker); 843e03a3d7aSMing Lei kthread_stop(lo->worker_task); 844e03a3d7aSMing Lei } 845e03a3d7aSMing Lei 846e03a3d7aSMing Lei static int loop_prepare_queue(struct loop_device *lo) 847e03a3d7aSMing Lei { 8483989144fSPetr Mladek kthread_init_worker(&lo->worker); 849e03a3d7aSMing Lei lo->worker_task = kthread_run(kthread_worker_fn, 850e03a3d7aSMing Lei &lo->worker, "loop%d", lo->lo_number); 851e03a3d7aSMing Lei if (IS_ERR(lo->worker_task)) 852e03a3d7aSMing Lei return -ENOMEM; 853e03a3d7aSMing Lei set_user_nice(lo->worker_task, MIN_NICE); 854e03a3d7aSMing Lei return 0; 855e03a3d7aSMing Lei } 856e03a3d7aSMing Lei 857bb214884SAl Viro static int loop_set_fd(struct loop_device *lo, fmode_t mode, 8581da177e4SLinus Torvalds struct block_device *bdev, unsigned int arg) 8591da177e4SLinus Torvalds { 8601da177e4SLinus Torvalds struct file *file, *f; 8611da177e4SLinus Torvalds struct inode *inode; 8621da177e4SLinus Torvalds struct address_space *mapping; 8631da177e4SLinus Torvalds unsigned lo_blocksize; 8641da177e4SLinus Torvalds int lo_flags = 0; 8651da177e4SLinus Torvalds int error; 8661da177e4SLinus Torvalds loff_t size; 8671da177e4SLinus Torvalds 8681da177e4SLinus Torvalds /* This is safe, since we have a reference from open(). */ 8691da177e4SLinus Torvalds __module_get(THIS_MODULE); 8701da177e4SLinus Torvalds 8711da177e4SLinus Torvalds error = -EBADF; 8721da177e4SLinus Torvalds file = fget(arg); 8731da177e4SLinus Torvalds if (!file) 8741da177e4SLinus Torvalds goto out; 8751da177e4SLinus Torvalds 8761da177e4SLinus Torvalds error = -EBUSY; 8771da177e4SLinus Torvalds if (lo->lo_state != Lo_unbound) 8781da177e4SLinus Torvalds goto out_putf; 8791da177e4SLinus Torvalds 8801da177e4SLinus Torvalds /* Avoid recursion */ 8811da177e4SLinus Torvalds f = file; 8821da177e4SLinus Torvalds while (is_loop_device(f)) { 8831da177e4SLinus Torvalds struct loop_device *l; 8841da177e4SLinus Torvalds 885bb214884SAl Viro if (f->f_mapping->host->i_bdev == bdev) 8861da177e4SLinus Torvalds goto out_putf; 8871da177e4SLinus Torvalds 8881da177e4SLinus Torvalds l = f->f_mapping->host->i_bdev->bd_disk->private_data; 8891da177e4SLinus Torvalds if (l->lo_state == Lo_unbound) { 8901da177e4SLinus Torvalds error = -EINVAL; 8911da177e4SLinus Torvalds goto out_putf; 8921da177e4SLinus Torvalds } 8931da177e4SLinus Torvalds f = l->lo_backing_file; 8941da177e4SLinus Torvalds } 8951da177e4SLinus Torvalds 8961da177e4SLinus Torvalds mapping = file->f_mapping; 8971da177e4SLinus Torvalds inode = mapping->host; 8981da177e4SLinus Torvalds 8991da177e4SLinus Torvalds error = -EINVAL; 900456be148SChristoph Hellwig if (!S_ISREG(inode->i_mode) && !S_ISBLK(inode->i_mode)) 901456be148SChristoph Hellwig goto out_putf; 9026818173bSMiklos Szeredi 903456be148SChristoph Hellwig if (!(file->f_mode & FMODE_WRITE) || !(mode & FMODE_WRITE) || 904283e7e5dSAl Viro !file->f_op->write_iter) 9051da177e4SLinus Torvalds lo_flags |= LO_FLAGS_READ_ONLY; 9061da177e4SLinus Torvalds 907ba52de12STheodore Ts'o lo_blocksize = S_ISBLK(inode->i_mode) ? 908ba52de12STheodore Ts'o inode->i_bdev->bd_block_size : PAGE_SIZE; 909ba52de12STheodore Ts'o 9101da177e4SLinus Torvalds error = -EFBIG; 911456be148SChristoph Hellwig size = get_loop_size(lo, file); 912456be148SChristoph Hellwig if ((loff_t)(sector_t)size != size) 9131da177e4SLinus Torvalds goto out_putf; 914e03a3d7aSMing Lei error = loop_prepare_queue(lo); 915e03a3d7aSMing Lei if (error) 916f4aa4c7bSMing Lei goto out_putf; 9171da177e4SLinus Torvalds 918456be148SChristoph Hellwig error = 0; 9191da177e4SLinus Torvalds 9201da177e4SLinus Torvalds set_device_ro(bdev, (lo_flags & LO_FLAGS_READ_ONLY) != 0); 9211da177e4SLinus Torvalds 9222e5ab5f3SMing Lei lo->use_dio = false; 9231da177e4SLinus Torvalds lo->lo_blocksize = lo_blocksize; 9241da177e4SLinus Torvalds lo->lo_device = bdev; 9251da177e4SLinus Torvalds lo->lo_flags = lo_flags; 9261da177e4SLinus Torvalds lo->lo_backing_file = file; 927aa4d8616SChristoph Hellwig lo->transfer = NULL; 9281da177e4SLinus Torvalds lo->ioctl = NULL; 9291da177e4SLinus Torvalds lo->lo_sizelimit = 0; 9301da177e4SLinus Torvalds lo->old_gfp_mask = mapping_gfp_mask(mapping); 9311da177e4SLinus Torvalds mapping_set_gfp_mask(mapping, lo->old_gfp_mask & ~(__GFP_IO|__GFP_FS)); 9321da177e4SLinus Torvalds 93368db1961SNikanth Karthikesan if (!(lo_flags & LO_FLAGS_READ_ONLY) && file->f_op->fsync) 93421d0727fSJens Axboe blk_queue_write_cache(lo->lo_queue, true, false); 93568db1961SNikanth Karthikesan 9362e5ab5f3SMing Lei loop_update_dio(lo); 93773285082SKen Chen set_capacity(lo->lo_disk, size); 9381da177e4SLinus Torvalds bd_set_size(bdev, size << 9); 939ee862730SMilan Broz loop_sysfs_init(lo); 940c3473c63SDavid Zeuthen /* let user-space know about the new size */ 941c3473c63SDavid Zeuthen kobject_uevent(&disk_to_dev(bdev->bd_disk)->kobj, KOBJ_CHANGE); 9421da177e4SLinus Torvalds 9431da177e4SLinus Torvalds set_blocksize(bdev, lo_blocksize); 9441da177e4SLinus Torvalds 9456c997918SSerge E. Hallyn lo->lo_state = Lo_bound; 946e03c8dd1SKay Sievers if (part_shift) 947e03c8dd1SKay Sievers lo->lo_flags |= LO_FLAGS_PARTSCAN; 948e03c8dd1SKay Sievers if (lo->lo_flags & LO_FLAGS_PARTSCAN) 94906f0e9e6SMing Lei loop_reread_partitions(lo, bdev); 950c1681bf8SAnatol Pomozov 951c1681bf8SAnatol Pomozov /* Grab the block_device to prevent its destruction after we 952c1681bf8SAnatol Pomozov * put /dev/loopXX inode. Later in loop_clr_fd() we bdput(bdev). 953c1681bf8SAnatol Pomozov */ 954c1681bf8SAnatol Pomozov bdgrab(bdev); 9551da177e4SLinus Torvalds return 0; 9561da177e4SLinus Torvalds 9571da177e4SLinus Torvalds out_putf: 9581da177e4SLinus Torvalds fput(file); 9591da177e4SLinus Torvalds out: 9601da177e4SLinus Torvalds /* This is safe: open() is still holding a reference. */ 9611da177e4SLinus Torvalds module_put(THIS_MODULE); 9621da177e4SLinus Torvalds return error; 9631da177e4SLinus Torvalds } 9641da177e4SLinus Torvalds 9651da177e4SLinus Torvalds static int 9661da177e4SLinus Torvalds loop_release_xfer(struct loop_device *lo) 9671da177e4SLinus Torvalds { 9681da177e4SLinus Torvalds int err = 0; 9691da177e4SLinus Torvalds struct loop_func_table *xfer = lo->lo_encryption; 9701da177e4SLinus Torvalds 9711da177e4SLinus Torvalds if (xfer) { 9721da177e4SLinus Torvalds if (xfer->release) 9731da177e4SLinus Torvalds err = xfer->release(lo); 9741da177e4SLinus Torvalds lo->transfer = NULL; 9751da177e4SLinus Torvalds lo->lo_encryption = NULL; 9761da177e4SLinus Torvalds module_put(xfer->owner); 9771da177e4SLinus Torvalds } 9781da177e4SLinus Torvalds return err; 9791da177e4SLinus Torvalds } 9801da177e4SLinus Torvalds 9811da177e4SLinus Torvalds static int 9821da177e4SLinus Torvalds loop_init_xfer(struct loop_device *lo, struct loop_func_table *xfer, 9831da177e4SLinus Torvalds const struct loop_info64 *i) 9841da177e4SLinus Torvalds { 9851da177e4SLinus Torvalds int err = 0; 9861da177e4SLinus Torvalds 9871da177e4SLinus Torvalds if (xfer) { 9881da177e4SLinus Torvalds struct module *owner = xfer->owner; 9891da177e4SLinus Torvalds 9901da177e4SLinus Torvalds if (!try_module_get(owner)) 9911da177e4SLinus Torvalds return -EINVAL; 9921da177e4SLinus Torvalds if (xfer->init) 9931da177e4SLinus Torvalds err = xfer->init(lo, i); 9941da177e4SLinus Torvalds if (err) 9951da177e4SLinus Torvalds module_put(owner); 9961da177e4SLinus Torvalds else 9971da177e4SLinus Torvalds lo->lo_encryption = xfer; 9981da177e4SLinus Torvalds } 9991da177e4SLinus Torvalds return err; 10001da177e4SLinus Torvalds } 10011da177e4SLinus Torvalds 10024c823cc3SAyan George static int loop_clr_fd(struct loop_device *lo) 10031da177e4SLinus Torvalds { 10041da177e4SLinus Torvalds struct file *filp = lo->lo_backing_file; 1005b4e3ca1aSAl Viro gfp_t gfp = lo->old_gfp_mask; 10064c823cc3SAyan George struct block_device *bdev = lo->lo_device; 10071da177e4SLinus Torvalds 10081da177e4SLinus Torvalds if (lo->lo_state != Lo_bound) 10091da177e4SLinus Torvalds return -ENXIO; 10101da177e4SLinus Torvalds 1011a1ecac3bSDave Chinner /* 1012a1ecac3bSDave Chinner * If we've explicitly asked to tear down the loop device, 1013a1ecac3bSDave Chinner * and it has an elevated reference count, set it for auto-teardown when 1014a1ecac3bSDave Chinner * the last reference goes away. This stops $!~#$@ udev from 1015a1ecac3bSDave Chinner * preventing teardown because it decided that it needs to run blkid on 1016a1ecac3bSDave Chinner * the loopback device whenever they appear. xfstests is notorious for 1017a1ecac3bSDave Chinner * failing tests because blkid via udev races with a losetup 1018a1ecac3bSDave Chinner * <dev>/do something like mkfs/losetup -d <dev> causing the losetup -d 1019a1ecac3bSDave Chinner * command to fail with EBUSY. 1020a1ecac3bSDave Chinner */ 1021f8933667SMing Lei if (atomic_read(&lo->lo_refcnt) > 1) { 1022a1ecac3bSDave Chinner lo->lo_flags |= LO_FLAGS_AUTOCLEAR; 1023a1ecac3bSDave Chinner mutex_unlock(&lo->lo_ctl_mutex); 1024a1ecac3bSDave Chinner return 0; 1025a1ecac3bSDave Chinner } 10261da177e4SLinus Torvalds 10271da177e4SLinus Torvalds if (filp == NULL) 10281da177e4SLinus Torvalds return -EINVAL; 10291da177e4SLinus Torvalds 1030f8933667SMing Lei /* freeze request queue during the transition */ 1031f8933667SMing Lei blk_mq_freeze_queue(lo->lo_queue); 1032f8933667SMing Lei 10331da177e4SLinus Torvalds spin_lock_irq(&lo->lo_lock); 10341da177e4SLinus Torvalds lo->lo_state = Lo_rundown; 10351da177e4SLinus Torvalds lo->lo_backing_file = NULL; 103605eb0f25SKay Sievers spin_unlock_irq(&lo->lo_lock); 10371da177e4SLinus Torvalds 10381da177e4SLinus Torvalds loop_release_xfer(lo); 10391da177e4SLinus Torvalds lo->transfer = NULL; 10401da177e4SLinus Torvalds lo->ioctl = NULL; 10411da177e4SLinus Torvalds lo->lo_device = NULL; 10421da177e4SLinus Torvalds lo->lo_encryption = NULL; 10431da177e4SLinus Torvalds lo->lo_offset = 0; 10441da177e4SLinus Torvalds lo->lo_sizelimit = 0; 10451da177e4SLinus Torvalds lo->lo_encrypt_key_size = 0; 10461da177e4SLinus Torvalds memset(lo->lo_encrypt_key, 0, LO_KEY_SIZE); 10471da177e4SLinus Torvalds memset(lo->lo_crypt_name, 0, LO_NAME_SIZE); 10481da177e4SLinus Torvalds memset(lo->lo_file_name, 0, LO_NAME_SIZE); 1049c1681bf8SAnatol Pomozov if (bdev) { 1050c1681bf8SAnatol Pomozov bdput(bdev); 1051f98393a6SPeter Zijlstra invalidate_bdev(bdev); 1052c1681bf8SAnatol Pomozov } 105373285082SKen Chen set_capacity(lo->lo_disk, 0); 105451a0bb0cSMilan Broz loop_sysfs_exit(lo); 1055c3473c63SDavid Zeuthen if (bdev) { 10561da177e4SLinus Torvalds bd_set_size(bdev, 0); 1057c3473c63SDavid Zeuthen /* let user-space know about this change */ 1058c3473c63SDavid Zeuthen kobject_uevent(&disk_to_dev(bdev->bd_disk)->kobj, KOBJ_CHANGE); 1059c3473c63SDavid Zeuthen } 10601da177e4SLinus Torvalds mapping_set_gfp_mask(filp->f_mapping, gfp); 10611da177e4SLinus Torvalds lo->lo_state = Lo_unbound; 10621da177e4SLinus Torvalds /* This is safe: open() is still holding a reference. */ 10631da177e4SLinus Torvalds module_put(THIS_MODULE); 1064f8933667SMing Lei blk_mq_unfreeze_queue(lo->lo_queue); 1065f8933667SMing Lei 1066c2fccc1cSJens Axboe if (lo->lo_flags & LO_FLAGS_PARTSCAN && bdev) 106706f0e9e6SMing Lei loop_reread_partitions(lo, bdev); 1068e03c8dd1SKay Sievers lo->lo_flags = 0; 1069e03c8dd1SKay Sievers if (!part_shift) 1070e03c8dd1SKay Sievers lo->lo_disk->flags |= GENHD_FL_NO_PART_SCAN; 1071e03a3d7aSMing Lei loop_unprepare_queue(lo); 1072f028f3b2SNikanth Karthikesan mutex_unlock(&lo->lo_ctl_mutex); 1073f028f3b2SNikanth Karthikesan /* 1074f028f3b2SNikanth Karthikesan * Need not hold lo_ctl_mutex to fput backing file. 1075f028f3b2SNikanth Karthikesan * Calling fput holding lo_ctl_mutex triggers a circular 1076f028f3b2SNikanth Karthikesan * lock dependency possibility warning as fput can take 1077f028f3b2SNikanth Karthikesan * bd_mutex which is usually taken before lo_ctl_mutex. 1078f028f3b2SNikanth Karthikesan */ 1079f028f3b2SNikanth Karthikesan fput(filp); 10801da177e4SLinus Torvalds return 0; 10811da177e4SLinus Torvalds } 10821da177e4SLinus Torvalds 10831da177e4SLinus Torvalds static int 10841da177e4SLinus Torvalds loop_set_status(struct loop_device *lo, const struct loop_info64 *info) 10851da177e4SLinus Torvalds { 10861da177e4SLinus Torvalds int err; 10871da177e4SLinus Torvalds struct loop_func_table *xfer; 1088e4849737SEric W. Biederman kuid_t uid = current_uid(); 10891da177e4SLinus Torvalds 1090b0fafa81SDavid Howells if (lo->lo_encrypt_key_size && 1091e4849737SEric W. Biederman !uid_eq(lo->lo_key_owner, uid) && 10921da177e4SLinus Torvalds !capable(CAP_SYS_ADMIN)) 10931da177e4SLinus Torvalds return -EPERM; 10941da177e4SLinus Torvalds if (lo->lo_state != Lo_bound) 10951da177e4SLinus Torvalds return -ENXIO; 10961da177e4SLinus Torvalds if ((unsigned int) info->lo_encrypt_key_size > LO_KEY_SIZE) 10971da177e4SLinus Torvalds return -EINVAL; 10981da177e4SLinus Torvalds 1099ecdd0959SMing Lei /* I/O need to be drained during transfer transition */ 1100ecdd0959SMing Lei blk_mq_freeze_queue(lo->lo_queue); 1101ecdd0959SMing Lei 11021da177e4SLinus Torvalds err = loop_release_xfer(lo); 11031da177e4SLinus Torvalds if (err) 1104ecdd0959SMing Lei goto exit; 11051da177e4SLinus Torvalds 11061da177e4SLinus Torvalds if (info->lo_encrypt_type) { 11071da177e4SLinus Torvalds unsigned int type = info->lo_encrypt_type; 11081da177e4SLinus Torvalds 11091da177e4SLinus Torvalds if (type >= MAX_LO_CRYPT) 11101da177e4SLinus Torvalds return -EINVAL; 11111da177e4SLinus Torvalds xfer = xfer_funcs[type]; 11121da177e4SLinus Torvalds if (xfer == NULL) 11131da177e4SLinus Torvalds return -EINVAL; 11141da177e4SLinus Torvalds } else 11151da177e4SLinus Torvalds xfer = NULL; 11161da177e4SLinus Torvalds 11171da177e4SLinus Torvalds err = loop_init_xfer(lo, xfer, info); 11181da177e4SLinus Torvalds if (err) 1119ecdd0959SMing Lei goto exit; 11201da177e4SLinus Torvalds 11211da177e4SLinus Torvalds if (lo->lo_offset != info->lo_offset || 11227b0576a3SGuo Chao lo->lo_sizelimit != info->lo_sizelimit) 1123ecdd0959SMing Lei if (figure_loop_size(lo, info->lo_offset, info->lo_sizelimit)) { 1124ecdd0959SMing Lei err = -EFBIG; 1125ecdd0959SMing Lei goto exit; 1126ecdd0959SMing Lei } 1127541c742aSGuo Chao 1128dfaa2ef6SLukas Czerner loop_config_discard(lo); 11291da177e4SLinus Torvalds 11301da177e4SLinus Torvalds memcpy(lo->lo_file_name, info->lo_file_name, LO_NAME_SIZE); 11311da177e4SLinus Torvalds memcpy(lo->lo_crypt_name, info->lo_crypt_name, LO_NAME_SIZE); 11321da177e4SLinus Torvalds lo->lo_file_name[LO_NAME_SIZE-1] = 0; 11331da177e4SLinus Torvalds lo->lo_crypt_name[LO_NAME_SIZE-1] = 0; 11341da177e4SLinus Torvalds 11351da177e4SLinus Torvalds if (!xfer) 11361da177e4SLinus Torvalds xfer = &none_funcs; 11371da177e4SLinus Torvalds lo->transfer = xfer->transfer; 11381da177e4SLinus Torvalds lo->ioctl = xfer->ioctl; 11391da177e4SLinus Torvalds 114096c58655SDavid Woodhouse if ((lo->lo_flags & LO_FLAGS_AUTOCLEAR) != 114196c58655SDavid Woodhouse (info->lo_flags & LO_FLAGS_AUTOCLEAR)) 114296c58655SDavid Woodhouse lo->lo_flags ^= LO_FLAGS_AUTOCLEAR; 114396c58655SDavid Woodhouse 11441da177e4SLinus Torvalds lo->lo_encrypt_key_size = info->lo_encrypt_key_size; 11451da177e4SLinus Torvalds lo->lo_init[0] = info->lo_init[0]; 11461da177e4SLinus Torvalds lo->lo_init[1] = info->lo_init[1]; 11471da177e4SLinus Torvalds if (info->lo_encrypt_key_size) { 11481da177e4SLinus Torvalds memcpy(lo->lo_encrypt_key, info->lo_encrypt_key, 11491da177e4SLinus Torvalds info->lo_encrypt_key_size); 1150b0fafa81SDavid Howells lo->lo_key_owner = uid; 11511da177e4SLinus Torvalds } 11521da177e4SLinus Torvalds 11532e5ab5f3SMing Lei /* update dio if lo_offset or transfer is changed */ 11542e5ab5f3SMing Lei __loop_update_dio(lo, lo->use_dio); 11552e5ab5f3SMing Lei 1156ecdd0959SMing Lei exit: 1157ecdd0959SMing Lei blk_mq_unfreeze_queue(lo->lo_queue); 1158e02898b4SOmar Sandoval 1159e02898b4SOmar Sandoval if (!err && (info->lo_flags & LO_FLAGS_PARTSCAN) && 1160e02898b4SOmar Sandoval !(lo->lo_flags & LO_FLAGS_PARTSCAN)) { 1161e02898b4SOmar Sandoval lo->lo_flags |= LO_FLAGS_PARTSCAN; 1162e02898b4SOmar Sandoval lo->lo_disk->flags &= ~GENHD_FL_NO_PART_SCAN; 1163e02898b4SOmar Sandoval loop_reread_partitions(lo, lo->lo_device); 1164e02898b4SOmar Sandoval } 1165e02898b4SOmar Sandoval 1166ecdd0959SMing Lei return err; 11671da177e4SLinus Torvalds } 11681da177e4SLinus Torvalds 11691da177e4SLinus Torvalds static int 11701da177e4SLinus Torvalds loop_get_status(struct loop_device *lo, struct loop_info64 *info) 11711da177e4SLinus Torvalds { 11721da177e4SLinus Torvalds struct file *file = lo->lo_backing_file; 11731da177e4SLinus Torvalds struct kstat stat; 11741da177e4SLinus Torvalds int error; 11751da177e4SLinus Torvalds 11761da177e4SLinus Torvalds if (lo->lo_state != Lo_bound) 11771da177e4SLinus Torvalds return -ENXIO; 1178a528d35eSDavid Howells error = vfs_getattr(&file->f_path, &stat, 1179a528d35eSDavid Howells STATX_INO, AT_STATX_SYNC_AS_STAT); 11801da177e4SLinus Torvalds if (error) 11811da177e4SLinus Torvalds return error; 11821da177e4SLinus Torvalds memset(info, 0, sizeof(*info)); 11831da177e4SLinus Torvalds info->lo_number = lo->lo_number; 11841da177e4SLinus Torvalds info->lo_device = huge_encode_dev(stat.dev); 11851da177e4SLinus Torvalds info->lo_inode = stat.ino; 11861da177e4SLinus Torvalds info->lo_rdevice = huge_encode_dev(lo->lo_device ? stat.rdev : stat.dev); 11871da177e4SLinus Torvalds info->lo_offset = lo->lo_offset; 11881da177e4SLinus Torvalds info->lo_sizelimit = lo->lo_sizelimit; 11891da177e4SLinus Torvalds info->lo_flags = lo->lo_flags; 11901da177e4SLinus Torvalds memcpy(info->lo_file_name, lo->lo_file_name, LO_NAME_SIZE); 11911da177e4SLinus Torvalds memcpy(info->lo_crypt_name, lo->lo_crypt_name, LO_NAME_SIZE); 11921da177e4SLinus Torvalds info->lo_encrypt_type = 11931da177e4SLinus Torvalds lo->lo_encryption ? lo->lo_encryption->number : 0; 11941da177e4SLinus Torvalds if (lo->lo_encrypt_key_size && capable(CAP_SYS_ADMIN)) { 11951da177e4SLinus Torvalds info->lo_encrypt_key_size = lo->lo_encrypt_key_size; 11961da177e4SLinus Torvalds memcpy(info->lo_encrypt_key, lo->lo_encrypt_key, 11971da177e4SLinus Torvalds lo->lo_encrypt_key_size); 11981da177e4SLinus Torvalds } 11991da177e4SLinus Torvalds return 0; 12001da177e4SLinus Torvalds } 12011da177e4SLinus Torvalds 12021da177e4SLinus Torvalds static void 12031da177e4SLinus Torvalds loop_info64_from_old(const struct loop_info *info, struct loop_info64 *info64) 12041da177e4SLinus Torvalds { 12051da177e4SLinus Torvalds memset(info64, 0, sizeof(*info64)); 12061da177e4SLinus Torvalds info64->lo_number = info->lo_number; 12071da177e4SLinus Torvalds info64->lo_device = info->lo_device; 12081da177e4SLinus Torvalds info64->lo_inode = info->lo_inode; 12091da177e4SLinus Torvalds info64->lo_rdevice = info->lo_rdevice; 12101da177e4SLinus Torvalds info64->lo_offset = info->lo_offset; 12111da177e4SLinus Torvalds info64->lo_sizelimit = 0; 12121da177e4SLinus Torvalds info64->lo_encrypt_type = info->lo_encrypt_type; 12131da177e4SLinus Torvalds info64->lo_encrypt_key_size = info->lo_encrypt_key_size; 12141da177e4SLinus Torvalds info64->lo_flags = info->lo_flags; 12151da177e4SLinus Torvalds info64->lo_init[0] = info->lo_init[0]; 12161da177e4SLinus Torvalds info64->lo_init[1] = info->lo_init[1]; 12171da177e4SLinus Torvalds if (info->lo_encrypt_type == LO_CRYPT_CRYPTOAPI) 12181da177e4SLinus Torvalds memcpy(info64->lo_crypt_name, info->lo_name, LO_NAME_SIZE); 12191da177e4SLinus Torvalds else 12201da177e4SLinus Torvalds memcpy(info64->lo_file_name, info->lo_name, LO_NAME_SIZE); 12211da177e4SLinus Torvalds memcpy(info64->lo_encrypt_key, info->lo_encrypt_key, LO_KEY_SIZE); 12221da177e4SLinus Torvalds } 12231da177e4SLinus Torvalds 12241da177e4SLinus Torvalds static int 12251da177e4SLinus Torvalds loop_info64_to_old(const struct loop_info64 *info64, struct loop_info *info) 12261da177e4SLinus Torvalds { 12271da177e4SLinus Torvalds memset(info, 0, sizeof(*info)); 12281da177e4SLinus Torvalds info->lo_number = info64->lo_number; 12291da177e4SLinus Torvalds info->lo_device = info64->lo_device; 12301da177e4SLinus Torvalds info->lo_inode = info64->lo_inode; 12311da177e4SLinus Torvalds info->lo_rdevice = info64->lo_rdevice; 12321da177e4SLinus Torvalds info->lo_offset = info64->lo_offset; 12331da177e4SLinus Torvalds info->lo_encrypt_type = info64->lo_encrypt_type; 12341da177e4SLinus Torvalds info->lo_encrypt_key_size = info64->lo_encrypt_key_size; 12351da177e4SLinus Torvalds info->lo_flags = info64->lo_flags; 12361da177e4SLinus Torvalds info->lo_init[0] = info64->lo_init[0]; 12371da177e4SLinus Torvalds info->lo_init[1] = info64->lo_init[1]; 12381da177e4SLinus Torvalds if (info->lo_encrypt_type == LO_CRYPT_CRYPTOAPI) 12391da177e4SLinus Torvalds memcpy(info->lo_name, info64->lo_crypt_name, LO_NAME_SIZE); 12401da177e4SLinus Torvalds else 12411da177e4SLinus Torvalds memcpy(info->lo_name, info64->lo_file_name, LO_NAME_SIZE); 12421da177e4SLinus Torvalds memcpy(info->lo_encrypt_key, info64->lo_encrypt_key, LO_KEY_SIZE); 12431da177e4SLinus Torvalds 12441da177e4SLinus Torvalds /* error in case values were truncated */ 12451da177e4SLinus Torvalds if (info->lo_device != info64->lo_device || 12461da177e4SLinus Torvalds info->lo_rdevice != info64->lo_rdevice || 12471da177e4SLinus Torvalds info->lo_inode != info64->lo_inode || 12481da177e4SLinus Torvalds info->lo_offset != info64->lo_offset) 12491da177e4SLinus Torvalds return -EOVERFLOW; 12501da177e4SLinus Torvalds 12511da177e4SLinus Torvalds return 0; 12521da177e4SLinus Torvalds } 12531da177e4SLinus Torvalds 12541da177e4SLinus Torvalds static int 12551da177e4SLinus Torvalds loop_set_status_old(struct loop_device *lo, const struct loop_info __user *arg) 12561da177e4SLinus Torvalds { 12571da177e4SLinus Torvalds struct loop_info info; 12581da177e4SLinus Torvalds struct loop_info64 info64; 12591da177e4SLinus Torvalds 12601da177e4SLinus Torvalds if (copy_from_user(&info, arg, sizeof (struct loop_info))) 12611da177e4SLinus Torvalds return -EFAULT; 12621da177e4SLinus Torvalds loop_info64_from_old(&info, &info64); 12631da177e4SLinus Torvalds return loop_set_status(lo, &info64); 12641da177e4SLinus Torvalds } 12651da177e4SLinus Torvalds 12661da177e4SLinus Torvalds static int 12671da177e4SLinus Torvalds loop_set_status64(struct loop_device *lo, const struct loop_info64 __user *arg) 12681da177e4SLinus Torvalds { 12691da177e4SLinus Torvalds struct loop_info64 info64; 12701da177e4SLinus Torvalds 12711da177e4SLinus Torvalds if (copy_from_user(&info64, arg, sizeof (struct loop_info64))) 12721da177e4SLinus Torvalds return -EFAULT; 12731da177e4SLinus Torvalds return loop_set_status(lo, &info64); 12741da177e4SLinus Torvalds } 12751da177e4SLinus Torvalds 12761da177e4SLinus Torvalds static int 12771da177e4SLinus Torvalds loop_get_status_old(struct loop_device *lo, struct loop_info __user *arg) { 12781da177e4SLinus Torvalds struct loop_info info; 12791da177e4SLinus Torvalds struct loop_info64 info64; 12801da177e4SLinus Torvalds int err = 0; 12811da177e4SLinus Torvalds 12821da177e4SLinus Torvalds if (!arg) 12831da177e4SLinus Torvalds err = -EINVAL; 12841da177e4SLinus Torvalds if (!err) 12851da177e4SLinus Torvalds err = loop_get_status(lo, &info64); 12861da177e4SLinus Torvalds if (!err) 12871da177e4SLinus Torvalds err = loop_info64_to_old(&info64, &info); 12881da177e4SLinus Torvalds if (!err && copy_to_user(arg, &info, sizeof(info))) 12891da177e4SLinus Torvalds err = -EFAULT; 12901da177e4SLinus Torvalds 12911da177e4SLinus Torvalds return err; 12921da177e4SLinus Torvalds } 12931da177e4SLinus Torvalds 12941da177e4SLinus Torvalds static int 12951da177e4SLinus Torvalds loop_get_status64(struct loop_device *lo, struct loop_info64 __user *arg) { 12961da177e4SLinus Torvalds struct loop_info64 info64; 12971da177e4SLinus Torvalds int err = 0; 12981da177e4SLinus Torvalds 12991da177e4SLinus Torvalds if (!arg) 13001da177e4SLinus Torvalds err = -EINVAL; 13011da177e4SLinus Torvalds if (!err) 13021da177e4SLinus Torvalds err = loop_get_status(lo, &info64); 13031da177e4SLinus Torvalds if (!err && copy_to_user(arg, &info64, sizeof(info64))) 13041da177e4SLinus Torvalds err = -EFAULT; 13051da177e4SLinus Torvalds 13061da177e4SLinus Torvalds return err; 13071da177e4SLinus Torvalds } 13081da177e4SLinus Torvalds 130953d66608SJ. R. Okajima static int loop_set_capacity(struct loop_device *lo, struct block_device *bdev) 131053d66608SJ. R. Okajima { 131153d66608SJ. R. Okajima if (unlikely(lo->lo_state != Lo_bound)) 13127b0576a3SGuo Chao return -ENXIO; 131353d66608SJ. R. Okajima 13147b0576a3SGuo Chao return figure_loop_size(lo, lo->lo_offset, lo->lo_sizelimit); 131553d66608SJ. R. Okajima } 131653d66608SJ. R. Okajima 1317ab1cb278SMing Lei static int loop_set_dio(struct loop_device *lo, unsigned long arg) 1318ab1cb278SMing Lei { 1319ab1cb278SMing Lei int error = -ENXIO; 1320ab1cb278SMing Lei if (lo->lo_state != Lo_bound) 1321ab1cb278SMing Lei goto out; 1322ab1cb278SMing Lei 1323ab1cb278SMing Lei __loop_update_dio(lo, !!arg); 1324ab1cb278SMing Lei if (lo->use_dio == !!arg) 1325ab1cb278SMing Lei return 0; 1326ab1cb278SMing Lei error = -EINVAL; 1327ab1cb278SMing Lei out: 1328ab1cb278SMing Lei return error; 1329ab1cb278SMing Lei } 1330ab1cb278SMing Lei 1331bb214884SAl Viro static int lo_ioctl(struct block_device *bdev, fmode_t mode, 13321da177e4SLinus Torvalds unsigned int cmd, unsigned long arg) 13331da177e4SLinus Torvalds { 1334bb214884SAl Viro struct loop_device *lo = bdev->bd_disk->private_data; 13351da177e4SLinus Torvalds int err; 13361da177e4SLinus Torvalds 1337f028f3b2SNikanth Karthikesan mutex_lock_nested(&lo->lo_ctl_mutex, 1); 13381da177e4SLinus Torvalds switch (cmd) { 13391da177e4SLinus Torvalds case LOOP_SET_FD: 1340bb214884SAl Viro err = loop_set_fd(lo, mode, bdev, arg); 13411da177e4SLinus Torvalds break; 13421da177e4SLinus Torvalds case LOOP_CHANGE_FD: 1343bb214884SAl Viro err = loop_change_fd(lo, bdev, arg); 13441da177e4SLinus Torvalds break; 13451da177e4SLinus Torvalds case LOOP_CLR_FD: 1346f028f3b2SNikanth Karthikesan /* loop_clr_fd would have unlocked lo_ctl_mutex on success */ 13474c823cc3SAyan George err = loop_clr_fd(lo); 1348f028f3b2SNikanth Karthikesan if (!err) 1349f028f3b2SNikanth Karthikesan goto out_unlocked; 13501da177e4SLinus Torvalds break; 13511da177e4SLinus Torvalds case LOOP_SET_STATUS: 13527035b5dfSDmitry Monakhov err = -EPERM; 13537035b5dfSDmitry Monakhov if ((mode & FMODE_WRITE) || capable(CAP_SYS_ADMIN)) 13547035b5dfSDmitry Monakhov err = loop_set_status_old(lo, 13557035b5dfSDmitry Monakhov (struct loop_info __user *)arg); 13561da177e4SLinus Torvalds break; 13571da177e4SLinus Torvalds case LOOP_GET_STATUS: 13581da177e4SLinus Torvalds err = loop_get_status_old(lo, (struct loop_info __user *) arg); 13591da177e4SLinus Torvalds break; 13601da177e4SLinus Torvalds case LOOP_SET_STATUS64: 13617035b5dfSDmitry Monakhov err = -EPERM; 13627035b5dfSDmitry Monakhov if ((mode & FMODE_WRITE) || capable(CAP_SYS_ADMIN)) 13637035b5dfSDmitry Monakhov err = loop_set_status64(lo, 13647035b5dfSDmitry Monakhov (struct loop_info64 __user *) arg); 13651da177e4SLinus Torvalds break; 13661da177e4SLinus Torvalds case LOOP_GET_STATUS64: 13671da177e4SLinus Torvalds err = loop_get_status64(lo, (struct loop_info64 __user *) arg); 13681da177e4SLinus Torvalds break; 136953d66608SJ. R. Okajima case LOOP_SET_CAPACITY: 137053d66608SJ. R. Okajima err = -EPERM; 137153d66608SJ. R. Okajima if ((mode & FMODE_WRITE) || capable(CAP_SYS_ADMIN)) 137253d66608SJ. R. Okajima err = loop_set_capacity(lo, bdev); 137353d66608SJ. R. Okajima break; 1374ab1cb278SMing Lei case LOOP_SET_DIRECT_IO: 1375ab1cb278SMing Lei err = -EPERM; 1376ab1cb278SMing Lei if ((mode & FMODE_WRITE) || capable(CAP_SYS_ADMIN)) 1377ab1cb278SMing Lei err = loop_set_dio(lo, arg); 1378ab1cb278SMing Lei break; 13791da177e4SLinus Torvalds default: 13801da177e4SLinus Torvalds err = lo->ioctl ? lo->ioctl(lo, cmd, arg) : -EINVAL; 13811da177e4SLinus Torvalds } 1382f85221ddSIngo Molnar mutex_unlock(&lo->lo_ctl_mutex); 1383f028f3b2SNikanth Karthikesan 1384f028f3b2SNikanth Karthikesan out_unlocked: 13851da177e4SLinus Torvalds return err; 13861da177e4SLinus Torvalds } 13871da177e4SLinus Torvalds 1388863d5b82SDavid Howells #ifdef CONFIG_COMPAT 1389863d5b82SDavid Howells struct compat_loop_info { 1390863d5b82SDavid Howells compat_int_t lo_number; /* ioctl r/o */ 1391863d5b82SDavid Howells compat_dev_t lo_device; /* ioctl r/o */ 1392863d5b82SDavid Howells compat_ulong_t lo_inode; /* ioctl r/o */ 1393863d5b82SDavid Howells compat_dev_t lo_rdevice; /* ioctl r/o */ 1394863d5b82SDavid Howells compat_int_t lo_offset; 1395863d5b82SDavid Howells compat_int_t lo_encrypt_type; 1396863d5b82SDavid Howells compat_int_t lo_encrypt_key_size; /* ioctl w/o */ 1397863d5b82SDavid Howells compat_int_t lo_flags; /* ioctl r/o */ 1398863d5b82SDavid Howells char lo_name[LO_NAME_SIZE]; 1399863d5b82SDavid Howells unsigned char lo_encrypt_key[LO_KEY_SIZE]; /* ioctl w/o */ 1400863d5b82SDavid Howells compat_ulong_t lo_init[2]; 1401863d5b82SDavid Howells char reserved[4]; 1402863d5b82SDavid Howells }; 1403863d5b82SDavid Howells 1404863d5b82SDavid Howells /* 1405863d5b82SDavid Howells * Transfer 32-bit compatibility structure in userspace to 64-bit loop info 1406863d5b82SDavid Howells * - noinlined to reduce stack space usage in main part of driver 1407863d5b82SDavid Howells */ 1408863d5b82SDavid Howells static noinline int 1409ba674cfcSAl Viro loop_info64_from_compat(const struct compat_loop_info __user *arg, 1410863d5b82SDavid Howells struct loop_info64 *info64) 1411863d5b82SDavid Howells { 1412863d5b82SDavid Howells struct compat_loop_info info; 1413863d5b82SDavid Howells 1414863d5b82SDavid Howells if (copy_from_user(&info, arg, sizeof(info))) 1415863d5b82SDavid Howells return -EFAULT; 1416863d5b82SDavid Howells 1417863d5b82SDavid Howells memset(info64, 0, sizeof(*info64)); 1418863d5b82SDavid Howells info64->lo_number = info.lo_number; 1419863d5b82SDavid Howells info64->lo_device = info.lo_device; 1420863d5b82SDavid Howells info64->lo_inode = info.lo_inode; 1421863d5b82SDavid Howells info64->lo_rdevice = info.lo_rdevice; 1422863d5b82SDavid Howells info64->lo_offset = info.lo_offset; 1423863d5b82SDavid Howells info64->lo_sizelimit = 0; 1424863d5b82SDavid Howells info64->lo_encrypt_type = info.lo_encrypt_type; 1425863d5b82SDavid Howells info64->lo_encrypt_key_size = info.lo_encrypt_key_size; 1426863d5b82SDavid Howells info64->lo_flags = info.lo_flags; 1427863d5b82SDavid Howells info64->lo_init[0] = info.lo_init[0]; 1428863d5b82SDavid Howells info64->lo_init[1] = info.lo_init[1]; 1429863d5b82SDavid Howells if (info.lo_encrypt_type == LO_CRYPT_CRYPTOAPI) 1430863d5b82SDavid Howells memcpy(info64->lo_crypt_name, info.lo_name, LO_NAME_SIZE); 1431863d5b82SDavid Howells else 1432863d5b82SDavid Howells memcpy(info64->lo_file_name, info.lo_name, LO_NAME_SIZE); 1433863d5b82SDavid Howells memcpy(info64->lo_encrypt_key, info.lo_encrypt_key, LO_KEY_SIZE); 1434863d5b82SDavid Howells return 0; 1435863d5b82SDavid Howells } 1436863d5b82SDavid Howells 1437863d5b82SDavid Howells /* 1438863d5b82SDavid Howells * Transfer 64-bit loop info to 32-bit compatibility structure in userspace 1439863d5b82SDavid Howells * - noinlined to reduce stack space usage in main part of driver 1440863d5b82SDavid Howells */ 1441863d5b82SDavid Howells static noinline int 1442863d5b82SDavid Howells loop_info64_to_compat(const struct loop_info64 *info64, 1443863d5b82SDavid Howells struct compat_loop_info __user *arg) 1444863d5b82SDavid Howells { 1445863d5b82SDavid Howells struct compat_loop_info info; 1446863d5b82SDavid Howells 1447863d5b82SDavid Howells memset(&info, 0, sizeof(info)); 1448863d5b82SDavid Howells info.lo_number = info64->lo_number; 1449863d5b82SDavid Howells info.lo_device = info64->lo_device; 1450863d5b82SDavid Howells info.lo_inode = info64->lo_inode; 1451863d5b82SDavid Howells info.lo_rdevice = info64->lo_rdevice; 1452863d5b82SDavid Howells info.lo_offset = info64->lo_offset; 1453863d5b82SDavid Howells info.lo_encrypt_type = info64->lo_encrypt_type; 1454863d5b82SDavid Howells info.lo_encrypt_key_size = info64->lo_encrypt_key_size; 1455863d5b82SDavid Howells info.lo_flags = info64->lo_flags; 1456863d5b82SDavid Howells info.lo_init[0] = info64->lo_init[0]; 1457863d5b82SDavid Howells info.lo_init[1] = info64->lo_init[1]; 1458863d5b82SDavid Howells if (info.lo_encrypt_type == LO_CRYPT_CRYPTOAPI) 1459863d5b82SDavid Howells memcpy(info.lo_name, info64->lo_crypt_name, LO_NAME_SIZE); 1460863d5b82SDavid Howells else 1461863d5b82SDavid Howells memcpy(info.lo_name, info64->lo_file_name, LO_NAME_SIZE); 1462863d5b82SDavid Howells memcpy(info.lo_encrypt_key, info64->lo_encrypt_key, LO_KEY_SIZE); 1463863d5b82SDavid Howells 1464863d5b82SDavid Howells /* error in case values were truncated */ 1465863d5b82SDavid Howells if (info.lo_device != info64->lo_device || 1466863d5b82SDavid Howells info.lo_rdevice != info64->lo_rdevice || 1467863d5b82SDavid Howells info.lo_inode != info64->lo_inode || 1468863d5b82SDavid Howells info.lo_offset != info64->lo_offset || 1469863d5b82SDavid Howells info.lo_init[0] != info64->lo_init[0] || 1470863d5b82SDavid Howells info.lo_init[1] != info64->lo_init[1]) 1471863d5b82SDavid Howells return -EOVERFLOW; 1472863d5b82SDavid Howells 1473863d5b82SDavid Howells if (copy_to_user(arg, &info, sizeof(info))) 1474863d5b82SDavid Howells return -EFAULT; 1475863d5b82SDavid Howells return 0; 1476863d5b82SDavid Howells } 1477863d5b82SDavid Howells 1478863d5b82SDavid Howells static int 1479863d5b82SDavid Howells loop_set_status_compat(struct loop_device *lo, 1480863d5b82SDavid Howells const struct compat_loop_info __user *arg) 1481863d5b82SDavid Howells { 1482863d5b82SDavid Howells struct loop_info64 info64; 1483863d5b82SDavid Howells int ret; 1484863d5b82SDavid Howells 1485863d5b82SDavid Howells ret = loop_info64_from_compat(arg, &info64); 1486863d5b82SDavid Howells if (ret < 0) 1487863d5b82SDavid Howells return ret; 1488863d5b82SDavid Howells return loop_set_status(lo, &info64); 1489863d5b82SDavid Howells } 1490863d5b82SDavid Howells 1491863d5b82SDavid Howells static int 1492863d5b82SDavid Howells loop_get_status_compat(struct loop_device *lo, 1493863d5b82SDavid Howells struct compat_loop_info __user *arg) 1494863d5b82SDavid Howells { 1495863d5b82SDavid Howells struct loop_info64 info64; 1496863d5b82SDavid Howells int err = 0; 1497863d5b82SDavid Howells 1498863d5b82SDavid Howells if (!arg) 1499863d5b82SDavid Howells err = -EINVAL; 1500863d5b82SDavid Howells if (!err) 1501863d5b82SDavid Howells err = loop_get_status(lo, &info64); 1502863d5b82SDavid Howells if (!err) 1503863d5b82SDavid Howells err = loop_info64_to_compat(&info64, arg); 1504863d5b82SDavid Howells return err; 1505863d5b82SDavid Howells } 1506863d5b82SDavid Howells 1507bb214884SAl Viro static int lo_compat_ioctl(struct block_device *bdev, fmode_t mode, 1508bb214884SAl Viro unsigned int cmd, unsigned long arg) 1509863d5b82SDavid Howells { 1510bb214884SAl Viro struct loop_device *lo = bdev->bd_disk->private_data; 1511863d5b82SDavid Howells int err; 1512863d5b82SDavid Howells 1513863d5b82SDavid Howells switch(cmd) { 1514863d5b82SDavid Howells case LOOP_SET_STATUS: 1515863d5b82SDavid Howells mutex_lock(&lo->lo_ctl_mutex); 1516863d5b82SDavid Howells err = loop_set_status_compat( 1517863d5b82SDavid Howells lo, (const struct compat_loop_info __user *) arg); 1518863d5b82SDavid Howells mutex_unlock(&lo->lo_ctl_mutex); 1519863d5b82SDavid Howells break; 1520863d5b82SDavid Howells case LOOP_GET_STATUS: 1521863d5b82SDavid Howells mutex_lock(&lo->lo_ctl_mutex); 1522863d5b82SDavid Howells err = loop_get_status_compat( 1523863d5b82SDavid Howells lo, (struct compat_loop_info __user *) arg); 1524863d5b82SDavid Howells mutex_unlock(&lo->lo_ctl_mutex); 1525863d5b82SDavid Howells break; 152653d66608SJ. R. Okajima case LOOP_SET_CAPACITY: 1527863d5b82SDavid Howells case LOOP_CLR_FD: 1528863d5b82SDavid Howells case LOOP_GET_STATUS64: 1529863d5b82SDavid Howells case LOOP_SET_STATUS64: 1530863d5b82SDavid Howells arg = (unsigned long) compat_ptr(arg); 1531863d5b82SDavid Howells case LOOP_SET_FD: 1532863d5b82SDavid Howells case LOOP_CHANGE_FD: 1533bb214884SAl Viro err = lo_ioctl(bdev, mode, cmd, arg); 1534863d5b82SDavid Howells break; 1535863d5b82SDavid Howells default: 1536863d5b82SDavid Howells err = -ENOIOCTLCMD; 1537863d5b82SDavid Howells break; 1538863d5b82SDavid Howells } 1539863d5b82SDavid Howells return err; 1540863d5b82SDavid Howells } 1541863d5b82SDavid Howells #endif 1542863d5b82SDavid Howells 1543bb214884SAl Viro static int lo_open(struct block_device *bdev, fmode_t mode) 15441da177e4SLinus Torvalds { 1545770fe30aSKay Sievers struct loop_device *lo; 1546770fe30aSKay Sievers int err = 0; 1547770fe30aSKay Sievers 1548770fe30aSKay Sievers mutex_lock(&loop_index_mutex); 1549770fe30aSKay Sievers lo = bdev->bd_disk->private_data; 1550770fe30aSKay Sievers if (!lo) { 1551770fe30aSKay Sievers err = -ENXIO; 1552770fe30aSKay Sievers goto out; 1553770fe30aSKay Sievers } 15541da177e4SLinus Torvalds 1555f8933667SMing Lei atomic_inc(&lo->lo_refcnt); 1556770fe30aSKay Sievers out: 1557770fe30aSKay Sievers mutex_unlock(&loop_index_mutex); 1558770fe30aSKay Sievers return err; 15591da177e4SLinus Torvalds } 15601da177e4SLinus Torvalds 1561db2a144bSAl Viro static void lo_release(struct gendisk *disk, fmode_t mode) 15621da177e4SLinus Torvalds { 1563bb214884SAl Viro struct loop_device *lo = disk->private_data; 1564ffcd7dcaSAlexander Beregalov int err; 15651da177e4SLinus Torvalds 1566f8933667SMing Lei if (atomic_dec_return(&lo->lo_refcnt)) 1567f8933667SMing Lei return; 1568f8933667SMing Lei 1569f85221ddSIngo Molnar mutex_lock(&lo->lo_ctl_mutex); 157014f27939SMilan Broz if (lo->lo_flags & LO_FLAGS_AUTOCLEAR) { 157114f27939SMilan Broz /* 157214f27939SMilan Broz * In autoclear mode, stop the loop thread 157314f27939SMilan Broz * and remove configuration after last close. 157414f27939SMilan Broz */ 15754c823cc3SAyan George err = loop_clr_fd(lo); 1576ffcd7dcaSAlexander Beregalov if (!err) 1577db2a144bSAl Viro return; 157814f27939SMilan Broz } else { 157914f27939SMilan Broz /* 158014f27939SMilan Broz * Otherwise keep thread (if running) and config, 158114f27939SMilan Broz * but flush possible ongoing bios in thread. 158214f27939SMilan Broz */ 158314f27939SMilan Broz loop_flush(lo); 158414f27939SMilan Broz } 158596c58655SDavid Woodhouse 1586f85221ddSIngo Molnar mutex_unlock(&lo->lo_ctl_mutex); 15871da177e4SLinus Torvalds } 15881da177e4SLinus Torvalds 158983d5cde4SAlexey Dobriyan static const struct block_device_operations lo_fops = { 15901da177e4SLinus Torvalds .owner = THIS_MODULE, 1591bb214884SAl Viro .open = lo_open, 1592bb214884SAl Viro .release = lo_release, 1593bb214884SAl Viro .ioctl = lo_ioctl, 1594863d5b82SDavid Howells #ifdef CONFIG_COMPAT 1595bb214884SAl Viro .compat_ioctl = lo_compat_ioctl, 1596863d5b82SDavid Howells #endif 15971da177e4SLinus Torvalds }; 15981da177e4SLinus Torvalds 15991da177e4SLinus Torvalds /* 16001da177e4SLinus Torvalds * And now the modules code and kernel interface. 16011da177e4SLinus Torvalds */ 160273285082SKen Chen static int max_loop; 1603ac04fee0SNamhyung Kim module_param(max_loop, int, S_IRUGO); 1604a47653fcSKen Chen MODULE_PARM_DESC(max_loop, "Maximum number of loop devices"); 1605ac04fee0SNamhyung Kim module_param(max_part, int, S_IRUGO); 1606476a4813SLaurent Vivier MODULE_PARM_DESC(max_part, "Maximum number of partitions per loop device"); 16071da177e4SLinus Torvalds MODULE_LICENSE("GPL"); 16081da177e4SLinus Torvalds MODULE_ALIAS_BLOCKDEV_MAJOR(LOOP_MAJOR); 16091da177e4SLinus Torvalds 16101da177e4SLinus Torvalds int loop_register_transfer(struct loop_func_table *funcs) 16111da177e4SLinus Torvalds { 16121da177e4SLinus Torvalds unsigned int n = funcs->number; 16131da177e4SLinus Torvalds 16141da177e4SLinus Torvalds if (n >= MAX_LO_CRYPT || xfer_funcs[n]) 16151da177e4SLinus Torvalds return -EINVAL; 16161da177e4SLinus Torvalds xfer_funcs[n] = funcs; 16171da177e4SLinus Torvalds return 0; 16181da177e4SLinus Torvalds } 16191da177e4SLinus Torvalds 162034dd82afSKay Sievers static int unregister_transfer_cb(int id, void *ptr, void *data) 162134dd82afSKay Sievers { 162234dd82afSKay Sievers struct loop_device *lo = ptr; 162334dd82afSKay Sievers struct loop_func_table *xfer = data; 162434dd82afSKay Sievers 162534dd82afSKay Sievers mutex_lock(&lo->lo_ctl_mutex); 162634dd82afSKay Sievers if (lo->lo_encryption == xfer) 162734dd82afSKay Sievers loop_release_xfer(lo); 162834dd82afSKay Sievers mutex_unlock(&lo->lo_ctl_mutex); 162934dd82afSKay Sievers return 0; 163034dd82afSKay Sievers } 163134dd82afSKay Sievers 16321da177e4SLinus Torvalds int loop_unregister_transfer(int number) 16331da177e4SLinus Torvalds { 16341da177e4SLinus Torvalds unsigned int n = number; 16351da177e4SLinus Torvalds struct loop_func_table *xfer; 16361da177e4SLinus Torvalds 16371da177e4SLinus Torvalds if (n == 0 || n >= MAX_LO_CRYPT || (xfer = xfer_funcs[n]) == NULL) 16381da177e4SLinus Torvalds return -EINVAL; 16391da177e4SLinus Torvalds 16401da177e4SLinus Torvalds xfer_funcs[n] = NULL; 164134dd82afSKay Sievers idr_for_each(&loop_index_idr, &unregister_transfer_cb, xfer); 16421da177e4SLinus Torvalds return 0; 16431da177e4SLinus Torvalds } 16441da177e4SLinus Torvalds 16451da177e4SLinus Torvalds EXPORT_SYMBOL(loop_register_transfer); 16461da177e4SLinus Torvalds EXPORT_SYMBOL(loop_unregister_transfer); 16471da177e4SLinus Torvalds 1648b5dd2f60SMing Lei static int loop_queue_rq(struct blk_mq_hw_ctx *hctx, 1649b5dd2f60SMing Lei const struct blk_mq_queue_data *bd) 1650b5dd2f60SMing Lei { 1651b5dd2f60SMing Lei struct loop_cmd *cmd = blk_mq_rq_to_pdu(bd->rq); 1652f4aa4c7bSMing Lei struct loop_device *lo = cmd->rq->q->queuedata; 1653b5dd2f60SMing Lei 1654b5dd2f60SMing Lei blk_mq_start_request(bd->rq); 1655b5dd2f60SMing Lei 1656f4aa4c7bSMing Lei if (lo->lo_state != Lo_bound) 1657b4a567e8SOmar Sandoval return BLK_MQ_RQ_QUEUE_ERROR; 1658f4aa4c7bSMing Lei 1659f0225cacSChristoph Hellwig switch (req_op(cmd->rq)) { 1660f0225cacSChristoph Hellwig case REQ_OP_FLUSH: 1661f0225cacSChristoph Hellwig case REQ_OP_DISCARD: 166219372e27SChristoph Hellwig case REQ_OP_WRITE_ZEROES: 1663bc07c10aSMing Lei cmd->use_aio = false; 1664f0225cacSChristoph Hellwig break; 1665f0225cacSChristoph Hellwig default: 1666f0225cacSChristoph Hellwig cmd->use_aio = lo->use_dio; 1667f0225cacSChristoph Hellwig break; 1668f0225cacSChristoph Hellwig } 1669bc07c10aSMing Lei 16703989144fSPetr Mladek kthread_queue_work(&lo->worker, &cmd->work); 1671b5dd2f60SMing Lei 1672b5dd2f60SMing Lei return BLK_MQ_RQ_QUEUE_OK; 1673b5dd2f60SMing Lei } 1674b5dd2f60SMing Lei 1675b5dd2f60SMing Lei static void loop_handle_cmd(struct loop_cmd *cmd) 1676b5dd2f60SMing Lei { 1677a8ebb056SMike Christie const bool write = op_is_write(req_op(cmd->rq)); 1678b5dd2f60SMing Lei struct loop_device *lo = cmd->rq->q->queuedata; 1679f4829a9bSChristoph Hellwig int ret = 0; 1680b5dd2f60SMing Lei 1681f4829a9bSChristoph Hellwig if (write && (lo->lo_flags & LO_FLAGS_READ_ONLY)) { 1682f4829a9bSChristoph Hellwig ret = -EIO; 1683b5dd2f60SMing Lei goto failed; 1684f4829a9bSChristoph Hellwig } 1685b5dd2f60SMing Lei 168630112013SMing Lei ret = do_req_filebacked(lo, cmd->rq); 1687b5dd2f60SMing Lei failed: 1688bc07c10aSMing Lei /* complete non-aio request */ 1689fe2cb290SChristoph Hellwig if (!cmd->use_aio || ret) { 1690fe2cb290SChristoph Hellwig cmd->ret = ret ? -EIO : 0; 169108e0029aSChristoph Hellwig blk_mq_complete_request(cmd->rq); 1692fe2cb290SChristoph Hellwig } 1693b5dd2f60SMing Lei } 1694b5dd2f60SMing Lei 1695e03a3d7aSMing Lei static void loop_queue_work(struct kthread_work *work) 1696b5dd2f60SMing Lei { 1697b5dd2f60SMing Lei struct loop_cmd *cmd = 1698e03a3d7aSMing Lei container_of(work, struct loop_cmd, work); 1699b5dd2f60SMing Lei 1700b5dd2f60SMing Lei loop_handle_cmd(cmd); 1701b5dd2f60SMing Lei } 1702b5dd2f60SMing Lei 1703d6296d39SChristoph Hellwig static int loop_init_request(struct blk_mq_tag_set *set, struct request *rq, 1704d6296d39SChristoph Hellwig unsigned int hctx_idx, unsigned int numa_node) 1705b5dd2f60SMing Lei { 1706b5dd2f60SMing Lei struct loop_cmd *cmd = blk_mq_rq_to_pdu(rq); 1707b5dd2f60SMing Lei 1708b5dd2f60SMing Lei cmd->rq = rq; 17093989144fSPetr Mladek kthread_init_work(&cmd->work, loop_queue_work); 1710b5dd2f60SMing Lei 1711b5dd2f60SMing Lei return 0; 1712b5dd2f60SMing Lei } 1713b5dd2f60SMing Lei 1714f363b089SEric Biggers static const struct blk_mq_ops loop_mq_ops = { 1715b5dd2f60SMing Lei .queue_rq = loop_queue_rq, 1716b5dd2f60SMing Lei .init_request = loop_init_request, 1717fe2cb290SChristoph Hellwig .complete = lo_complete_rq, 1718b5dd2f60SMing Lei }; 1719b5dd2f60SMing Lei 172034dd82afSKay Sievers static int loop_add(struct loop_device **l, int i) 17211da177e4SLinus Torvalds { 172273285082SKen Chen struct loop_device *lo; 172373285082SKen Chen struct gendisk *disk; 172434dd82afSKay Sievers int err; 17251da177e4SLinus Torvalds 172634dd82afSKay Sievers err = -ENOMEM; 172768d740d7SSilva Paulo lo = kzalloc(sizeof(*lo), GFP_KERNEL); 172868d740d7SSilva Paulo if (!lo) 172973285082SKen Chen goto out; 173034dd82afSKay Sievers 1731ef7e7c82SMikulas Patocka lo->lo_state = Lo_unbound; 1732ef7e7c82SMikulas Patocka 1733c718aa65STejun Heo /* allocate id, if @id >= 0, we're requesting that specific id */ 173434dd82afSKay Sievers if (i >= 0) { 1735c718aa65STejun Heo err = idr_alloc(&loop_index_idr, lo, i, i + 1, GFP_KERNEL); 1736c718aa65STejun Heo if (err == -ENOSPC) 173734dd82afSKay Sievers err = -EEXIST; 173834dd82afSKay Sievers } else { 1739c718aa65STejun Heo err = idr_alloc(&loop_index_idr, lo, 0, 0, GFP_KERNEL); 174034dd82afSKay Sievers } 174134dd82afSKay Sievers if (err < 0) 174234dd82afSKay Sievers goto out_free_dev; 1743c718aa65STejun Heo i = err; 17441da177e4SLinus Torvalds 1745183cfb57SWei Yongjun err = -ENOMEM; 1746b5dd2f60SMing Lei lo->tag_set.ops = &loop_mq_ops; 1747b5dd2f60SMing Lei lo->tag_set.nr_hw_queues = 1; 1748b5dd2f60SMing Lei lo->tag_set.queue_depth = 128; 1749b5dd2f60SMing Lei lo->tag_set.numa_node = NUMA_NO_NODE; 1750b5dd2f60SMing Lei lo->tag_set.cmd_size = sizeof(struct loop_cmd); 1751b5dd2f60SMing Lei lo->tag_set.flags = BLK_MQ_F_SHOULD_MERGE | BLK_MQ_F_SG_MERGE; 1752b5dd2f60SMing Lei lo->tag_set.driver_data = lo; 1753b5dd2f60SMing Lei 1754b5dd2f60SMing Lei err = blk_mq_alloc_tag_set(&lo->tag_set); 1755b5dd2f60SMing Lei if (err) 17563ec981e3SMikulas Patocka goto out_free_idr; 175773285082SKen Chen 1758b5dd2f60SMing Lei lo->lo_queue = blk_mq_init_queue(&lo->tag_set); 1759b5dd2f60SMing Lei if (IS_ERR_OR_NULL(lo->lo_queue)) { 1760b5dd2f60SMing Lei err = PTR_ERR(lo->lo_queue); 1761b5dd2f60SMing Lei goto out_cleanup_tags; 1762b5dd2f60SMing Lei } 1763ef7e7c82SMikulas Patocka lo->lo_queue->queuedata = lo; 1764ef7e7c82SMikulas Patocka 17655b5e20f4SMing Lei /* 17665b5e20f4SMing Lei * It doesn't make sense to enable merge because the I/O 17675b5e20f4SMing Lei * submitted to backing file is handled page by page. 17685b5e20f4SMing Lei */ 17695b5e20f4SMing Lei queue_flag_set_unlocked(QUEUE_FLAG_NOMERGES, lo->lo_queue); 17705b5e20f4SMing Lei 17717a649737SMinfei Huang err = -ENOMEM; 1772476a4813SLaurent Vivier disk = lo->lo_disk = alloc_disk(1 << part_shift); 177373285082SKen Chen if (!disk) 177473285082SKen Chen goto out_free_queue; 177573285082SKen Chen 1776e03c8dd1SKay Sievers /* 1777e03c8dd1SKay Sievers * Disable partition scanning by default. The in-kernel partition 1778e03c8dd1SKay Sievers * scanning can be requested individually per-device during its 1779e03c8dd1SKay Sievers * setup. Userspace can always add and remove partitions from all 1780e03c8dd1SKay Sievers * devices. The needed partition minors are allocated from the 1781e03c8dd1SKay Sievers * extended minor space, the main loop device numbers will continue 1782e03c8dd1SKay Sievers * to match the loop minors, regardless of the number of partitions 1783e03c8dd1SKay Sievers * used. 1784e03c8dd1SKay Sievers * 1785e03c8dd1SKay Sievers * If max_part is given, partition scanning is globally enabled for 1786e03c8dd1SKay Sievers * all loop devices. The minors for the main loop devices will be 1787e03c8dd1SKay Sievers * multiples of max_part. 1788e03c8dd1SKay Sievers * 1789e03c8dd1SKay Sievers * Note: Global-for-all-devices, set-only-at-init, read-only module 1790e03c8dd1SKay Sievers * parameteters like 'max_loop' and 'max_part' make things needlessly 1791e03c8dd1SKay Sievers * complicated, are too static, inflexible and may surprise 1792e03c8dd1SKay Sievers * userspace tools. Parameters like this in general should be avoided. 1793e03c8dd1SKay Sievers */ 1794e03c8dd1SKay Sievers if (!part_shift) 1795e03c8dd1SKay Sievers disk->flags |= GENHD_FL_NO_PART_SCAN; 1796e03c8dd1SKay Sievers disk->flags |= GENHD_FL_EXT_DEVT; 1797f85221ddSIngo Molnar mutex_init(&lo->lo_ctl_mutex); 1798f8933667SMing Lei atomic_set(&lo->lo_refcnt, 0); 17991da177e4SLinus Torvalds lo->lo_number = i; 18001da177e4SLinus Torvalds spin_lock_init(&lo->lo_lock); 18011da177e4SLinus Torvalds disk->major = LOOP_MAJOR; 1802476a4813SLaurent Vivier disk->first_minor = i << part_shift; 18031da177e4SLinus Torvalds disk->fops = &lo_fops; 18041da177e4SLinus Torvalds disk->private_data = lo; 18051da177e4SLinus Torvalds disk->queue = lo->lo_queue; 180673285082SKen Chen sprintf(disk->disk_name, "loop%d", i); 180734dd82afSKay Sievers add_disk(disk); 180834dd82afSKay Sievers *l = lo; 180934dd82afSKay Sievers return lo->lo_number; 181073285082SKen Chen 181173285082SKen Chen out_free_queue: 181273285082SKen Chen blk_cleanup_queue(lo->lo_queue); 1813b5dd2f60SMing Lei out_cleanup_tags: 1814b5dd2f60SMing Lei blk_mq_free_tag_set(&lo->tag_set); 18153ec981e3SMikulas Patocka out_free_idr: 18163ec981e3SMikulas Patocka idr_remove(&loop_index_idr, i); 181773285082SKen Chen out_free_dev: 181873285082SKen Chen kfree(lo); 181973285082SKen Chen out: 182034dd82afSKay Sievers return err; 18211da177e4SLinus Torvalds } 18221da177e4SLinus Torvalds 182334dd82afSKay Sievers static void loop_remove(struct loop_device *lo) 182473285082SKen Chen { 182573285082SKen Chen blk_cleanup_queue(lo->lo_queue); 18266cd18e71SNeilBrown del_gendisk(lo->lo_disk); 1827b5dd2f60SMing Lei blk_mq_free_tag_set(&lo->tag_set); 182873285082SKen Chen put_disk(lo->lo_disk); 182973285082SKen Chen kfree(lo); 183073285082SKen Chen } 183173285082SKen Chen 1832770fe30aSKay Sievers static int find_free_cb(int id, void *ptr, void *data) 1833770fe30aSKay Sievers { 1834770fe30aSKay Sievers struct loop_device *lo = ptr; 1835770fe30aSKay Sievers struct loop_device **l = data; 1836770fe30aSKay Sievers 1837770fe30aSKay Sievers if (lo->lo_state == Lo_unbound) { 1838770fe30aSKay Sievers *l = lo; 1839770fe30aSKay Sievers return 1; 1840770fe30aSKay Sievers } 1841770fe30aSKay Sievers return 0; 1842770fe30aSKay Sievers } 1843770fe30aSKay Sievers 184434dd82afSKay Sievers static int loop_lookup(struct loop_device **l, int i) 1845a47653fcSKen Chen { 1846a47653fcSKen Chen struct loop_device *lo; 184734dd82afSKay Sievers int ret = -ENODEV; 1848a47653fcSKen Chen 1849770fe30aSKay Sievers if (i < 0) { 1850770fe30aSKay Sievers int err; 1851770fe30aSKay Sievers 1852770fe30aSKay Sievers err = idr_for_each(&loop_index_idr, &find_free_cb, &lo); 1853770fe30aSKay Sievers if (err == 1) { 1854770fe30aSKay Sievers *l = lo; 1855770fe30aSKay Sievers ret = lo->lo_number; 1856770fe30aSKay Sievers } 1857770fe30aSKay Sievers goto out; 1858a47653fcSKen Chen } 1859a47653fcSKen Chen 1860770fe30aSKay Sievers /* lookup and return a specific i */ 186134dd82afSKay Sievers lo = idr_find(&loop_index_idr, i); 1862a47653fcSKen Chen if (lo) { 186334dd82afSKay Sievers *l = lo; 186434dd82afSKay Sievers ret = lo->lo_number; 1865a47653fcSKen Chen } 1866770fe30aSKay Sievers out: 186734dd82afSKay Sievers return ret; 1868a47653fcSKen Chen } 1869a47653fcSKen Chen 187073285082SKen Chen static struct kobject *loop_probe(dev_t dev, int *part, void *data) 187173285082SKen Chen { 1872705962ccSAl Viro struct loop_device *lo; 187307002e99SAl Viro struct kobject *kobj; 187434dd82afSKay Sievers int err; 187573285082SKen Chen 187634dd82afSKay Sievers mutex_lock(&loop_index_mutex); 187734dd82afSKay Sievers err = loop_lookup(&lo, MINOR(dev) >> part_shift); 187834dd82afSKay Sievers if (err < 0) 187934dd82afSKay Sievers err = loop_add(&lo, MINOR(dev) >> part_shift); 188034dd82afSKay Sievers if (err < 0) 1881a207f593SMikulas Patocka kobj = NULL; 188234dd82afSKay Sievers else 188334dd82afSKay Sievers kobj = get_disk(lo->lo_disk); 188434dd82afSKay Sievers mutex_unlock(&loop_index_mutex); 188573285082SKen Chen 188673285082SKen Chen *part = 0; 188707002e99SAl Viro return kobj; 188873285082SKen Chen } 188973285082SKen Chen 1890770fe30aSKay Sievers static long loop_control_ioctl(struct file *file, unsigned int cmd, 1891770fe30aSKay Sievers unsigned long parm) 1892770fe30aSKay Sievers { 1893770fe30aSKay Sievers struct loop_device *lo; 1894770fe30aSKay Sievers int ret = -ENOSYS; 1895770fe30aSKay Sievers 1896770fe30aSKay Sievers mutex_lock(&loop_index_mutex); 1897770fe30aSKay Sievers switch (cmd) { 1898770fe30aSKay Sievers case LOOP_CTL_ADD: 1899770fe30aSKay Sievers ret = loop_lookup(&lo, parm); 1900770fe30aSKay Sievers if (ret >= 0) { 1901770fe30aSKay Sievers ret = -EEXIST; 1902770fe30aSKay Sievers break; 1903770fe30aSKay Sievers } 1904770fe30aSKay Sievers ret = loop_add(&lo, parm); 1905770fe30aSKay Sievers break; 1906770fe30aSKay Sievers case LOOP_CTL_REMOVE: 1907770fe30aSKay Sievers ret = loop_lookup(&lo, parm); 1908770fe30aSKay Sievers if (ret < 0) 1909770fe30aSKay Sievers break; 1910770fe30aSKay Sievers mutex_lock(&lo->lo_ctl_mutex); 1911770fe30aSKay Sievers if (lo->lo_state != Lo_unbound) { 1912770fe30aSKay Sievers ret = -EBUSY; 1913770fe30aSKay Sievers mutex_unlock(&lo->lo_ctl_mutex); 1914770fe30aSKay Sievers break; 1915770fe30aSKay Sievers } 1916f8933667SMing Lei if (atomic_read(&lo->lo_refcnt) > 0) { 1917770fe30aSKay Sievers ret = -EBUSY; 1918770fe30aSKay Sievers mutex_unlock(&lo->lo_ctl_mutex); 1919770fe30aSKay Sievers break; 1920770fe30aSKay Sievers } 1921770fe30aSKay Sievers lo->lo_disk->private_data = NULL; 1922770fe30aSKay Sievers mutex_unlock(&lo->lo_ctl_mutex); 1923770fe30aSKay Sievers idr_remove(&loop_index_idr, lo->lo_number); 1924770fe30aSKay Sievers loop_remove(lo); 1925770fe30aSKay Sievers break; 1926770fe30aSKay Sievers case LOOP_CTL_GET_FREE: 1927770fe30aSKay Sievers ret = loop_lookup(&lo, -1); 1928770fe30aSKay Sievers if (ret >= 0) 1929770fe30aSKay Sievers break; 1930770fe30aSKay Sievers ret = loop_add(&lo, -1); 1931770fe30aSKay Sievers } 1932770fe30aSKay Sievers mutex_unlock(&loop_index_mutex); 1933770fe30aSKay Sievers 1934770fe30aSKay Sievers return ret; 1935770fe30aSKay Sievers } 1936770fe30aSKay Sievers 1937770fe30aSKay Sievers static const struct file_operations loop_ctl_fops = { 1938770fe30aSKay Sievers .open = nonseekable_open, 1939770fe30aSKay Sievers .unlocked_ioctl = loop_control_ioctl, 1940770fe30aSKay Sievers .compat_ioctl = loop_control_ioctl, 1941770fe30aSKay Sievers .owner = THIS_MODULE, 1942770fe30aSKay Sievers .llseek = noop_llseek, 1943770fe30aSKay Sievers }; 1944770fe30aSKay Sievers 1945770fe30aSKay Sievers static struct miscdevice loop_misc = { 1946770fe30aSKay Sievers .minor = LOOP_CTRL_MINOR, 1947770fe30aSKay Sievers .name = "loop-control", 1948770fe30aSKay Sievers .fops = &loop_ctl_fops, 1949770fe30aSKay Sievers }; 1950770fe30aSKay Sievers 1951770fe30aSKay Sievers MODULE_ALIAS_MISCDEV(LOOP_CTRL_MINOR); 1952770fe30aSKay Sievers MODULE_ALIAS("devname:loop-control"); 1953770fe30aSKay Sievers 195473285082SKen Chen static int __init loop_init(void) 195573285082SKen Chen { 1956a47653fcSKen Chen int i, nr; 1957a47653fcSKen Chen unsigned long range; 195834dd82afSKay Sievers struct loop_device *lo; 1959770fe30aSKay Sievers int err; 1960a47653fcSKen Chen 1961770fe30aSKay Sievers err = misc_register(&loop_misc); 1962770fe30aSKay Sievers if (err < 0) 1963770fe30aSKay Sievers return err; 1964476a4813SLaurent Vivier 1965476a4813SLaurent Vivier part_shift = 0; 1966ac04fee0SNamhyung Kim if (max_part > 0) { 1967476a4813SLaurent Vivier part_shift = fls(max_part); 1968476a4813SLaurent Vivier 1969ac04fee0SNamhyung Kim /* 1970ac04fee0SNamhyung Kim * Adjust max_part according to part_shift as it is exported 1971ac04fee0SNamhyung Kim * to user space so that user can decide correct minor number 1972ac04fee0SNamhyung Kim * if [s]he want to create more devices. 1973ac04fee0SNamhyung Kim * 1974ac04fee0SNamhyung Kim * Note that -1 is required because partition 0 is reserved 1975ac04fee0SNamhyung Kim * for the whole disk. 1976ac04fee0SNamhyung Kim */ 1977ac04fee0SNamhyung Kim max_part = (1UL << part_shift) - 1; 1978ac04fee0SNamhyung Kim } 1979ac04fee0SNamhyung Kim 1980b1a66504SGuo Chao if ((1UL << part_shift) > DISK_MAX_PARTS) { 1981b1a66504SGuo Chao err = -EINVAL; 1982b1a66504SGuo Chao goto misc_out; 1983b1a66504SGuo Chao } 198478f4bb36SNamhyung Kim 1985b1a66504SGuo Chao if (max_loop > 1UL << (MINORBITS - part_shift)) { 1986b1a66504SGuo Chao err = -EINVAL; 1987b1a66504SGuo Chao goto misc_out; 1988b1a66504SGuo Chao } 198973285082SKen Chen 1990d134b00bSKay Sievers /* 1991d134b00bSKay Sievers * If max_loop is specified, create that many devices upfront. 1992d134b00bSKay Sievers * This also becomes a hard limit. If max_loop is not specified, 1993d134b00bSKay Sievers * create CONFIG_BLK_DEV_LOOP_MIN_COUNT loop devices at module 1994d134b00bSKay Sievers * init time. Loop devices can be requested on-demand with the 1995d134b00bSKay Sievers * /dev/loop-control interface, or be instantiated by accessing 1996d134b00bSKay Sievers * a 'dead' device node. 1997d134b00bSKay Sievers */ 199873285082SKen Chen if (max_loop) { 1999a47653fcSKen Chen nr = max_loop; 2000a1c15c59SNamhyung Kim range = max_loop << part_shift; 2001a47653fcSKen Chen } else { 2002d134b00bSKay Sievers nr = CONFIG_BLK_DEV_LOOP_MIN_COUNT; 2003a1c15c59SNamhyung Kim range = 1UL << MINORBITS; 200473285082SKen Chen } 2005a47653fcSKen Chen 2006b1a66504SGuo Chao if (register_blkdev(LOOP_MAJOR, "loop")) { 2007b1a66504SGuo Chao err = -EIO; 2008b1a66504SGuo Chao goto misc_out; 2009b1a66504SGuo Chao } 2010a47653fcSKen Chen 2011a47653fcSKen Chen blk_register_region(MKDEV(LOOP_MAJOR, 0), range, 2012a47653fcSKen Chen THIS_MODULE, loop_probe, NULL, NULL); 2013a47653fcSKen Chen 2014d134b00bSKay Sievers /* pre-create number of devices given by config or max_loop */ 201534dd82afSKay Sievers mutex_lock(&loop_index_mutex); 201634dd82afSKay Sievers for (i = 0; i < nr; i++) 201734dd82afSKay Sievers loop_add(&lo, i); 201834dd82afSKay Sievers mutex_unlock(&loop_index_mutex); 201934dd82afSKay Sievers 202073285082SKen Chen printk(KERN_INFO "loop: module loaded\n"); 20211da177e4SLinus Torvalds return 0; 2022b1a66504SGuo Chao 2023b1a66504SGuo Chao misc_out: 2024b1a66504SGuo Chao misc_deregister(&loop_misc); 2025b1a66504SGuo Chao return err; 202634dd82afSKay Sievers } 2027a47653fcSKen Chen 202834dd82afSKay Sievers static int loop_exit_cb(int id, void *ptr, void *data) 202934dd82afSKay Sievers { 203034dd82afSKay Sievers struct loop_device *lo = ptr; 2031a47653fcSKen Chen 203234dd82afSKay Sievers loop_remove(lo); 203334dd82afSKay Sievers return 0; 20341da177e4SLinus Torvalds } 20351da177e4SLinus Torvalds 203673285082SKen Chen static void __exit loop_exit(void) 20371da177e4SLinus Torvalds { 2038a47653fcSKen Chen unsigned long range; 20391da177e4SLinus Torvalds 2040a1c15c59SNamhyung Kim range = max_loop ? max_loop << part_shift : 1UL << MINORBITS; 2041a47653fcSKen Chen 204234dd82afSKay Sievers idr_for_each(&loop_index_idr, &loop_exit_cb, NULL); 204334dd82afSKay Sievers idr_destroy(&loop_index_idr); 204473285082SKen Chen 2045a47653fcSKen Chen blk_unregister_region(MKDEV(LOOP_MAJOR, 0), range); 204600d59405SAkinobu Mita unregister_blkdev(LOOP_MAJOR, "loop"); 2047770fe30aSKay Sievers 2048770fe30aSKay Sievers misc_deregister(&loop_misc); 20491da177e4SLinus Torvalds } 20501da177e4SLinus Torvalds 20511da177e4SLinus Torvalds module_init(loop_init); 20521da177e4SLinus Torvalds module_exit(loop_exit); 20531da177e4SLinus Torvalds 20541da177e4SLinus Torvalds #ifndef MODULE 20551da177e4SLinus Torvalds static int __init max_loop_setup(char *str) 20561da177e4SLinus Torvalds { 20571da177e4SLinus Torvalds max_loop = simple_strtol(str, NULL, 0); 20581da177e4SLinus Torvalds return 1; 20591da177e4SLinus Torvalds } 20601da177e4SLinus Torvalds 20611da177e4SLinus Torvalds __setup("max_loop=", max_loop_setup); 20621da177e4SLinus Torvalds #endif 2063