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> 79d9a08a9eSAdam Manzanares #include <linux/ioprio.h> 80db6638d7SDennis Zhou #include <linux/blk-cgroup.h> 81d9a08a9eSAdam Manzanares 8283a87611SAl Viro #include "loop.h" 831da177e4SLinus Torvalds 847c0f6ba6SLinus Torvalds #include <linux/uaccess.h> 851da177e4SLinus Torvalds 8634dd82afSKay Sievers static DEFINE_IDR(loop_index_idr); 87310ca162STetsuo Handa static DEFINE_MUTEX(loop_ctl_mutex); 881da177e4SLinus Torvalds 89476a4813SLaurent Vivier static int max_part; 90476a4813SLaurent Vivier static int part_shift; 91476a4813SLaurent Vivier 921da177e4SLinus Torvalds static int transfer_xor(struct loop_device *lo, int cmd, 931da177e4SLinus Torvalds struct page *raw_page, unsigned raw_off, 941da177e4SLinus Torvalds struct page *loop_page, unsigned loop_off, 951da177e4SLinus Torvalds int size, sector_t real_block) 961da177e4SLinus Torvalds { 97cfd8005cSCong Wang char *raw_buf = kmap_atomic(raw_page) + raw_off; 98cfd8005cSCong Wang char *loop_buf = kmap_atomic(loop_page) + loop_off; 991da177e4SLinus Torvalds char *in, *out, *key; 1001da177e4SLinus Torvalds int i, keysize; 1011da177e4SLinus Torvalds 1021da177e4SLinus Torvalds if (cmd == READ) { 1031da177e4SLinus Torvalds in = raw_buf; 1041da177e4SLinus Torvalds out = loop_buf; 1051da177e4SLinus Torvalds } else { 1061da177e4SLinus Torvalds in = loop_buf; 1071da177e4SLinus Torvalds out = raw_buf; 1081da177e4SLinus Torvalds } 1091da177e4SLinus Torvalds 1101da177e4SLinus Torvalds key = lo->lo_encrypt_key; 1111da177e4SLinus Torvalds keysize = lo->lo_encrypt_key_size; 1121da177e4SLinus Torvalds for (i = 0; i < size; i++) 1131da177e4SLinus Torvalds *out++ = *in++ ^ key[(i & 511) % keysize]; 1141da177e4SLinus Torvalds 115cfd8005cSCong Wang kunmap_atomic(loop_buf); 116cfd8005cSCong Wang kunmap_atomic(raw_buf); 1171da177e4SLinus Torvalds cond_resched(); 1181da177e4SLinus Torvalds return 0; 1191da177e4SLinus Torvalds } 1201da177e4SLinus Torvalds 1211da177e4SLinus Torvalds static int xor_init(struct loop_device *lo, const struct loop_info64 *info) 1221da177e4SLinus Torvalds { 1231da177e4SLinus Torvalds if (unlikely(info->lo_encrypt_key_size <= 0)) 1241da177e4SLinus Torvalds return -EINVAL; 1251da177e4SLinus Torvalds return 0; 1261da177e4SLinus Torvalds } 1271da177e4SLinus Torvalds 1281da177e4SLinus Torvalds static struct loop_func_table none_funcs = { 1291da177e4SLinus Torvalds .number = LO_CRYPT_NONE, 1301da177e4SLinus Torvalds }; 1311da177e4SLinus Torvalds 1321da177e4SLinus Torvalds static struct loop_func_table xor_funcs = { 1331da177e4SLinus Torvalds .number = LO_CRYPT_XOR, 1341da177e4SLinus Torvalds .transfer = transfer_xor, 1351da177e4SLinus Torvalds .init = xor_init 1361da177e4SLinus Torvalds }; 1371da177e4SLinus Torvalds 1381da177e4SLinus Torvalds /* xfer_funcs[0] is special - its release function is never called */ 1391da177e4SLinus Torvalds static struct loop_func_table *xfer_funcs[MAX_LO_CRYPT] = { 1401da177e4SLinus Torvalds &none_funcs, 1411da177e4SLinus Torvalds &xor_funcs 1421da177e4SLinus Torvalds }; 1431da177e4SLinus Torvalds 1447035b5dfSDmitry Monakhov static loff_t get_size(loff_t offset, loff_t sizelimit, struct file *file) 1451da177e4SLinus Torvalds { 146b7a1da69SGuo Chao loff_t loopsize; 1471da177e4SLinus Torvalds 1481da177e4SLinus Torvalds /* Compute loopsize in bytes */ 149b7a1da69SGuo Chao loopsize = i_size_read(file->f_mapping->host); 150b7a1da69SGuo Chao if (offset > 0) 151b7a1da69SGuo Chao loopsize -= offset; 152b7a1da69SGuo Chao /* offset is beyond i_size, weird but possible */ 1537035b5dfSDmitry Monakhov if (loopsize < 0) 1547035b5dfSDmitry Monakhov return 0; 1551da177e4SLinus Torvalds 1567035b5dfSDmitry Monakhov if (sizelimit > 0 && sizelimit < loopsize) 1577035b5dfSDmitry Monakhov loopsize = sizelimit; 1581da177e4SLinus Torvalds /* 1591da177e4SLinus Torvalds * Unfortunately, if we want to do I/O on the device, 1601da177e4SLinus Torvalds * the number of 512-byte sectors has to fit into a sector_t. 1611da177e4SLinus Torvalds */ 1621da177e4SLinus Torvalds return loopsize >> 9; 1631da177e4SLinus Torvalds } 1641da177e4SLinus Torvalds 1657035b5dfSDmitry Monakhov static loff_t get_loop_size(struct loop_device *lo, struct file *file) 1661da177e4SLinus Torvalds { 1677035b5dfSDmitry Monakhov return get_size(lo->lo_offset, lo->lo_sizelimit, file); 1687035b5dfSDmitry Monakhov } 1697035b5dfSDmitry Monakhov 1702e5ab5f3SMing Lei static void __loop_update_dio(struct loop_device *lo, bool dio) 1712e5ab5f3SMing Lei { 1722e5ab5f3SMing Lei struct file *file = lo->lo_backing_file; 1732e5ab5f3SMing Lei struct address_space *mapping = file->f_mapping; 1742e5ab5f3SMing Lei struct inode *inode = mapping->host; 1752e5ab5f3SMing Lei unsigned short sb_bsize = 0; 1762e5ab5f3SMing Lei unsigned dio_align = 0; 1772e5ab5f3SMing Lei bool use_dio; 1782e5ab5f3SMing Lei 1792e5ab5f3SMing Lei if (inode->i_sb->s_bdev) { 1802e5ab5f3SMing Lei sb_bsize = bdev_logical_block_size(inode->i_sb->s_bdev); 1812e5ab5f3SMing Lei dio_align = sb_bsize - 1; 1822e5ab5f3SMing Lei } 1832e5ab5f3SMing Lei 1842e5ab5f3SMing Lei /* 1852e5ab5f3SMing Lei * We support direct I/O only if lo_offset is aligned with the 1862e5ab5f3SMing Lei * logical I/O size of backing device, and the logical block 1872e5ab5f3SMing Lei * size of loop is bigger than the backing device's and the loop 1882e5ab5f3SMing Lei * needn't transform transfer. 1892e5ab5f3SMing Lei * 1902e5ab5f3SMing Lei * TODO: the above condition may be loosed in the future, and 1912e5ab5f3SMing Lei * direct I/O may be switched runtime at that time because most 19289d790abSMasahiro Yamada * of requests in sane applications should be PAGE_SIZE aligned 1932e5ab5f3SMing Lei */ 1942e5ab5f3SMing Lei if (dio) { 1952e5ab5f3SMing Lei if (queue_logical_block_size(lo->lo_queue) >= sb_bsize && 1962e5ab5f3SMing Lei !(lo->lo_offset & dio_align) && 1972e5ab5f3SMing Lei mapping->a_ops->direct_IO && 1982e5ab5f3SMing Lei !lo->transfer) 1992e5ab5f3SMing Lei use_dio = true; 2002e5ab5f3SMing Lei else 2012e5ab5f3SMing Lei use_dio = false; 2022e5ab5f3SMing Lei } else { 2032e5ab5f3SMing Lei use_dio = false; 2042e5ab5f3SMing Lei } 2052e5ab5f3SMing Lei 2062e5ab5f3SMing Lei if (lo->use_dio == use_dio) 2072e5ab5f3SMing Lei return; 2082e5ab5f3SMing Lei 2092e5ab5f3SMing Lei /* flush dirty pages before changing direct IO */ 2102e5ab5f3SMing Lei vfs_fsync(file, 0); 2112e5ab5f3SMing Lei 2122e5ab5f3SMing Lei /* 2132e5ab5f3SMing Lei * The flag of LO_FLAGS_DIRECT_IO is handled similarly with 2142e5ab5f3SMing Lei * LO_FLAGS_READ_ONLY, both are set from kernel, and losetup 2152e5ab5f3SMing Lei * will get updated by ioctl(LOOP_GET_STATUS) 2162e5ab5f3SMing Lei */ 2170fbcf579SMartijn Coenen if (lo->lo_state == Lo_bound) 2182e5ab5f3SMing Lei blk_mq_freeze_queue(lo->lo_queue); 2192e5ab5f3SMing Lei lo->use_dio = use_dio; 22040326d8aSShaohua Li if (use_dio) { 2218b904b5bSBart Van Assche blk_queue_flag_clear(QUEUE_FLAG_NOMERGES, lo->lo_queue); 2222e5ab5f3SMing Lei lo->lo_flags |= LO_FLAGS_DIRECT_IO; 22340326d8aSShaohua Li } else { 2248b904b5bSBart Van Assche blk_queue_flag_set(QUEUE_FLAG_NOMERGES, lo->lo_queue); 2252e5ab5f3SMing Lei lo->lo_flags &= ~LO_FLAGS_DIRECT_IO; 22640326d8aSShaohua Li } 2270fbcf579SMartijn Coenen if (lo->lo_state == Lo_bound) 2282e5ab5f3SMing Lei blk_mq_unfreeze_queue(lo->lo_queue); 2292e5ab5f3SMing Lei } 2302e5ab5f3SMing Lei 2317035b5dfSDmitry Monakhov static int 2321e6ec9eaSOmar Sandoval figure_loop_size(struct loop_device *lo, loff_t offset, loff_t sizelimit) 2337035b5dfSDmitry Monakhov { 2347035b5dfSDmitry Monakhov loff_t size = get_size(offset, sizelimit, lo->lo_backing_file); 2351da177e4SLinus Torvalds sector_t x = (sector_t)size; 2367b0576a3SGuo Chao struct block_device *bdev = lo->lo_device; 2371da177e4SLinus Torvalds 2381da177e4SLinus Torvalds if (unlikely((loff_t)x != size)) 2391da177e4SLinus Torvalds return -EFBIG; 2407035b5dfSDmitry Monakhov if (lo->lo_offset != offset) 2417035b5dfSDmitry Monakhov lo->lo_offset = offset; 2427035b5dfSDmitry Monakhov if (lo->lo_sizelimit != sizelimit) 2437035b5dfSDmitry Monakhov lo->lo_sizelimit = sizelimit; 24473285082SKen Chen set_capacity(lo->lo_disk, x); 2457b0576a3SGuo Chao bd_set_size(bdev, (loff_t)get_capacity(bdev->bd_disk) << 9); 2467b0576a3SGuo Chao /* let user-space know about the new size */ 2477b0576a3SGuo Chao kobject_uevent(&disk_to_dev(bdev->bd_disk)->kobj, KOBJ_CHANGE); 2481da177e4SLinus Torvalds return 0; 2491da177e4SLinus Torvalds } 2501da177e4SLinus Torvalds 2511da177e4SLinus Torvalds static inline int 2521da177e4SLinus Torvalds lo_do_transfer(struct loop_device *lo, int cmd, 2531da177e4SLinus Torvalds struct page *rpage, unsigned roffs, 2541da177e4SLinus Torvalds struct page *lpage, unsigned loffs, 2551da177e4SLinus Torvalds int size, sector_t rblock) 2561da177e4SLinus Torvalds { 257aa4d8616SChristoph Hellwig int ret; 258aa4d8616SChristoph Hellwig 259aa4d8616SChristoph Hellwig ret = lo->transfer(lo, cmd, rpage, roffs, lpage, loffs, size, rblock); 260aa4d8616SChristoph Hellwig if (likely(!ret)) 2611da177e4SLinus Torvalds return 0; 2621da177e4SLinus Torvalds 263aa4d8616SChristoph Hellwig printk_ratelimited(KERN_ERR 264aa4d8616SChristoph Hellwig "loop: Transfer error at byte offset %llu, length %i.\n", 265aa4d8616SChristoph Hellwig (unsigned long long)rblock << 9, size); 266aa4d8616SChristoph Hellwig return ret; 2671da177e4SLinus Torvalds } 2681da177e4SLinus Torvalds 269aa4d8616SChristoph Hellwig static int lo_write_bvec(struct file *file, struct bio_vec *bvec, loff_t *ppos) 2701da177e4SLinus Torvalds { 271aa4d8616SChristoph Hellwig struct iov_iter i; 2721da177e4SLinus Torvalds ssize_t bw; 273283e7e5dSAl Viro 274b6207430SChristoph Hellwig iov_iter_bvec(&i, WRITE, bvec, 1, bvec->bv_len); 2751da177e4SLinus Torvalds 27603d95eb2SAl Viro file_start_write(file); 277abbb6589SChristoph Hellwig bw = vfs_iter_write(file, &i, ppos, 0); 27803d95eb2SAl Viro file_end_write(file); 279aa4d8616SChristoph Hellwig 280aa4d8616SChristoph Hellwig if (likely(bw == bvec->bv_len)) 2811da177e4SLinus Torvalds return 0; 282aa4d8616SChristoph Hellwig 283aa4d8616SChristoph Hellwig printk_ratelimited(KERN_ERR 284aa4d8616SChristoph Hellwig "loop: Write error at byte offset %llu, length %i.\n", 285aa4d8616SChristoph Hellwig (unsigned long long)*ppos, bvec->bv_len); 2861da177e4SLinus Torvalds if (bw >= 0) 2871da177e4SLinus Torvalds bw = -EIO; 2881da177e4SLinus Torvalds return bw; 2891da177e4SLinus Torvalds } 2901da177e4SLinus Torvalds 291aa4d8616SChristoph Hellwig static int lo_write_simple(struct loop_device *lo, struct request *rq, 292aa4d8616SChristoph Hellwig loff_t pos) 2931da177e4SLinus Torvalds { 294aa4d8616SChristoph Hellwig struct bio_vec bvec; 295aa4d8616SChristoph Hellwig struct req_iterator iter; 296aa4d8616SChristoph Hellwig int ret = 0; 297aa4d8616SChristoph Hellwig 298aa4d8616SChristoph Hellwig rq_for_each_segment(bvec, rq, iter) { 299aa4d8616SChristoph Hellwig ret = lo_write_bvec(lo->lo_backing_file, &bvec, &pos); 300aa4d8616SChristoph Hellwig if (ret < 0) 301aa4d8616SChristoph Hellwig break; 3021da177e4SLinus Torvalds cond_resched(); 3031da177e4SLinus Torvalds } 3041da177e4SLinus Torvalds 305aa4d8616SChristoph Hellwig return ret; 306aa4d8616SChristoph Hellwig } 307aa4d8616SChristoph Hellwig 308aa4d8616SChristoph Hellwig /* 309456be148SChristoph Hellwig * This is the slow, transforming version that needs to double buffer the 310456be148SChristoph Hellwig * data as it cannot do the transformations in place without having direct 311456be148SChristoph Hellwig * access to the destination pages of the backing file. 3121da177e4SLinus Torvalds */ 313aa4d8616SChristoph Hellwig static int lo_write_transfer(struct loop_device *lo, struct request *rq, 314aa4d8616SChristoph Hellwig loff_t pos) 3151da177e4SLinus Torvalds { 316aa4d8616SChristoph Hellwig struct bio_vec bvec, b; 31730112013SMing Lei struct req_iterator iter; 318aa4d8616SChristoph Hellwig struct page *page; 3197988613bSKent Overstreet int ret = 0; 3201da177e4SLinus Torvalds 321aa4d8616SChristoph Hellwig page = alloc_page(GFP_NOIO); 3221da177e4SLinus Torvalds if (unlikely(!page)) 323aa4d8616SChristoph Hellwig return -ENOMEM; 324456be148SChristoph Hellwig 32530112013SMing Lei rq_for_each_segment(bvec, rq, iter) { 326aa4d8616SChristoph Hellwig ret = lo_do_transfer(lo, WRITE, page, 0, bvec.bv_page, 327aa4d8616SChristoph Hellwig bvec.bv_offset, bvec.bv_len, pos >> 9); 328aa4d8616SChristoph Hellwig if (unlikely(ret)) 329aa4d8616SChristoph Hellwig break; 330aa4d8616SChristoph Hellwig 331aa4d8616SChristoph Hellwig b.bv_page = page; 332aa4d8616SChristoph Hellwig b.bv_offset = 0; 333aa4d8616SChristoph Hellwig b.bv_len = bvec.bv_len; 334aa4d8616SChristoph Hellwig ret = lo_write_bvec(lo->lo_backing_file, &b, &pos); 3351da177e4SLinus Torvalds if (ret < 0) 3361da177e4SLinus Torvalds break; 3371da177e4SLinus Torvalds } 338aa4d8616SChristoph Hellwig 3391da177e4SLinus Torvalds __free_page(page); 3401da177e4SLinus Torvalds return ret; 3411da177e4SLinus Torvalds } 3421da177e4SLinus Torvalds 343aa4d8616SChristoph Hellwig static int lo_read_simple(struct loop_device *lo, struct request *rq, 344aa4d8616SChristoph Hellwig loff_t pos) 3451da177e4SLinus Torvalds { 3467988613bSKent Overstreet struct bio_vec bvec; 34730112013SMing Lei struct req_iterator iter; 348aa4d8616SChristoph Hellwig struct iov_iter i; 349aa4d8616SChristoph Hellwig ssize_t len; 3501da177e4SLinus Torvalds 35130112013SMing Lei rq_for_each_segment(bvec, rq, iter) { 352b6207430SChristoph Hellwig iov_iter_bvec(&i, READ, &bvec, 1, bvec.bv_len); 35318e9710eSChristoph Hellwig len = vfs_iter_read(lo->lo_backing_file, &i, &pos, 0); 354aa4d8616SChristoph Hellwig if (len < 0) 355aa4d8616SChristoph Hellwig return len; 356306df071SDave Young 357aa4d8616SChristoph Hellwig flush_dcache_page(bvec.bv_page); 358aa4d8616SChristoph Hellwig 359aa4d8616SChristoph Hellwig if (len != bvec.bv_len) { 36030112013SMing Lei struct bio *bio; 36130112013SMing Lei 36230112013SMing Lei __rq_for_each_bio(bio, rq) 363306df071SDave Young zero_fill_bio(bio); 3641da177e4SLinus Torvalds break; 365306df071SDave Young } 366aa4d8616SChristoph Hellwig cond_resched(); 3671da177e4SLinus Torvalds } 368aa4d8616SChristoph Hellwig 369306df071SDave Young return 0; 3701da177e4SLinus Torvalds } 3711da177e4SLinus Torvalds 372aa4d8616SChristoph Hellwig static int lo_read_transfer(struct loop_device *lo, struct request *rq, 373aa4d8616SChristoph Hellwig loff_t pos) 374aa4d8616SChristoph Hellwig { 375aa4d8616SChristoph Hellwig struct bio_vec bvec, b; 376aa4d8616SChristoph Hellwig struct req_iterator iter; 377aa4d8616SChristoph Hellwig struct iov_iter i; 378aa4d8616SChristoph Hellwig struct page *page; 379aa4d8616SChristoph Hellwig ssize_t len; 380aa4d8616SChristoph Hellwig int ret = 0; 381aa4d8616SChristoph Hellwig 382aa4d8616SChristoph Hellwig page = alloc_page(GFP_NOIO); 383aa4d8616SChristoph Hellwig if (unlikely(!page)) 384aa4d8616SChristoph Hellwig return -ENOMEM; 385aa4d8616SChristoph Hellwig 386aa4d8616SChristoph Hellwig rq_for_each_segment(bvec, rq, iter) { 387aa4d8616SChristoph Hellwig loff_t offset = pos; 388aa4d8616SChristoph Hellwig 389aa4d8616SChristoph Hellwig b.bv_page = page; 390aa4d8616SChristoph Hellwig b.bv_offset = 0; 391aa4d8616SChristoph Hellwig b.bv_len = bvec.bv_len; 392aa4d8616SChristoph Hellwig 393b6207430SChristoph Hellwig iov_iter_bvec(&i, READ, &b, 1, b.bv_len); 39418e9710eSChristoph Hellwig len = vfs_iter_read(lo->lo_backing_file, &i, &pos, 0); 395aa4d8616SChristoph Hellwig if (len < 0) { 396aa4d8616SChristoph Hellwig ret = len; 397aa4d8616SChristoph Hellwig goto out_free_page; 398aa4d8616SChristoph Hellwig } 399aa4d8616SChristoph Hellwig 400aa4d8616SChristoph Hellwig ret = lo_do_transfer(lo, READ, page, 0, bvec.bv_page, 401aa4d8616SChristoph Hellwig bvec.bv_offset, len, offset >> 9); 402aa4d8616SChristoph Hellwig if (ret) 403aa4d8616SChristoph Hellwig goto out_free_page; 404aa4d8616SChristoph Hellwig 405aa4d8616SChristoph Hellwig flush_dcache_page(bvec.bv_page); 406aa4d8616SChristoph Hellwig 407aa4d8616SChristoph Hellwig if (len != bvec.bv_len) { 408aa4d8616SChristoph Hellwig struct bio *bio; 409aa4d8616SChristoph Hellwig 410aa4d8616SChristoph Hellwig __rq_for_each_bio(bio, rq) 411aa4d8616SChristoph Hellwig zero_fill_bio(bio); 412aa4d8616SChristoph Hellwig break; 413aa4d8616SChristoph Hellwig } 414aa4d8616SChristoph Hellwig } 415aa4d8616SChristoph Hellwig 416aa4d8616SChristoph Hellwig ret = 0; 417aa4d8616SChristoph Hellwig out_free_page: 418aa4d8616SChristoph Hellwig __free_page(page); 419aa4d8616SChristoph Hellwig return ret; 420aa4d8616SChristoph Hellwig } 421aa4d8616SChristoph Hellwig 422efcfec57SDarrick J. Wong static int lo_fallocate(struct loop_device *lo, struct request *rq, loff_t pos, 423efcfec57SDarrick J. Wong int mode) 424cf655d95SMing Lei { 425cf655d95SMing Lei /* 426efcfec57SDarrick J. Wong * We use fallocate to manipulate the space mappings used by the image 427efcfec57SDarrick J. Wong * a.k.a. discard/zerorange. However we do not support this if 428efcfec57SDarrick J. Wong * encryption is enabled, because it may give an attacker useful 429efcfec57SDarrick J. Wong * information. 430cf655d95SMing Lei */ 431cf655d95SMing Lei struct file *file = lo->lo_backing_file; 432c52abf56SEvan Green struct request_queue *q = lo->lo_queue; 433cf655d95SMing Lei int ret; 434cf655d95SMing Lei 435efcfec57SDarrick J. Wong mode |= FALLOC_FL_KEEP_SIZE; 436efcfec57SDarrick J. Wong 437c52abf56SEvan Green if (!blk_queue_discard(q)) { 438cf655d95SMing Lei ret = -EOPNOTSUPP; 439cf655d95SMing Lei goto out; 440cf655d95SMing Lei } 441cf655d95SMing Lei 442cf655d95SMing Lei ret = file->f_op->fallocate(file, mode, pos, blk_rq_bytes(rq)); 443cf655d95SMing Lei if (unlikely(ret && ret != -EINVAL && ret != -EOPNOTSUPP)) 444cf655d95SMing Lei ret = -EIO; 445cf655d95SMing Lei out: 446cf655d95SMing Lei return ret; 447cf655d95SMing Lei } 448cf655d95SMing Lei 449cf655d95SMing Lei static int lo_req_flush(struct loop_device *lo, struct request *rq) 450cf655d95SMing Lei { 451cf655d95SMing Lei struct file *file = lo->lo_backing_file; 452cf655d95SMing Lei int ret = vfs_fsync(file, 0); 453cf655d95SMing Lei if (unlikely(ret && ret != -EINVAL)) 454cf655d95SMing Lei ret = -EIO; 455cf655d95SMing Lei 456cf655d95SMing Lei return ret; 457cf655d95SMing Lei } 458cf655d95SMing Lei 459fe2cb290SChristoph Hellwig static void lo_complete_rq(struct request *rq) 460bc07c10aSMing Lei { 461fe2cb290SChristoph Hellwig struct loop_cmd *cmd = blk_mq_rq_to_pdu(rq); 462f9de14bcSJens Axboe blk_status_t ret = BLK_STS_OK; 463bc07c10aSMing Lei 464f9de14bcSJens Axboe if (!cmd->use_aio || cmd->ret < 0 || cmd->ret == blk_rq_bytes(rq) || 465f9de14bcSJens Axboe req_op(rq) != REQ_OP_READ) { 466f9de14bcSJens Axboe if (cmd->ret < 0) 4678cd55087SEvan Green ret = errno_to_blk_status(cmd->ret); 468f9de14bcSJens Axboe goto end_io; 469bc07c10aSMing Lei } 470fe2cb290SChristoph Hellwig 471f9de14bcSJens Axboe /* 472f9de14bcSJens Axboe * Short READ - if we got some data, advance our request and 473f9de14bcSJens Axboe * retry it. If we got no data, end the rest with EIO. 474f9de14bcSJens Axboe */ 475f9de14bcSJens Axboe if (cmd->ret) { 476f9de14bcSJens Axboe blk_update_request(rq, BLK_STS_OK, cmd->ret); 477f9de14bcSJens Axboe cmd->ret = 0; 478f9de14bcSJens Axboe blk_mq_requeue_request(rq, true); 479f9de14bcSJens Axboe } else { 480f9de14bcSJens Axboe if (cmd->use_aio) { 481f9de14bcSJens Axboe struct bio *bio = rq->bio; 482f9de14bcSJens Axboe 483f9de14bcSJens Axboe while (bio) { 484f9de14bcSJens Axboe zero_fill_bio(bio); 485f9de14bcSJens Axboe bio = bio->bi_next; 486f9de14bcSJens Axboe } 487f9de14bcSJens Axboe } 488f9de14bcSJens Axboe ret = BLK_STS_IOERR; 489f9de14bcSJens Axboe end_io: 490f9de14bcSJens Axboe blk_mq_end_request(rq, ret); 491f9de14bcSJens Axboe } 492bc07c10aSMing Lei } 493bc07c10aSMing Lei 49492d77332SShaohua Li static void lo_rw_aio_do_completion(struct loop_cmd *cmd) 49592d77332SShaohua Li { 4961894e916SJens Axboe struct request *rq = blk_mq_rq_from_pdu(cmd); 4971894e916SJens Axboe 49892d77332SShaohua Li if (!atomic_dec_and_test(&cmd->ref)) 49992d77332SShaohua Li return; 50092d77332SShaohua Li kfree(cmd->bvec); 50192d77332SShaohua Li cmd->bvec = NULL; 5021894e916SJens Axboe blk_mq_complete_request(rq); 50392d77332SShaohua Li } 50492d77332SShaohua Li 505bc07c10aSMing Lei static void lo_rw_aio_complete(struct kiocb *iocb, long ret, long ret2) 506bc07c10aSMing Lei { 507bc07c10aSMing Lei struct loop_cmd *cmd = container_of(iocb, struct loop_cmd, iocb); 508bc07c10aSMing Lei 509d4478e92SShaohua Li if (cmd->css) 510d4478e92SShaohua Li css_put(cmd->css); 511fe2cb290SChristoph Hellwig cmd->ret = ret; 51292d77332SShaohua Li lo_rw_aio_do_completion(cmd); 513bc07c10aSMing Lei } 514bc07c10aSMing Lei 515bc07c10aSMing Lei static int lo_rw_aio(struct loop_device *lo, struct loop_cmd *cmd, 516bc07c10aSMing Lei loff_t pos, bool rw) 517bc07c10aSMing Lei { 518bc07c10aSMing Lei struct iov_iter iter; 51986af5952SMing Lei struct req_iterator rq_iter; 520bc07c10aSMing Lei struct bio_vec *bvec; 5211894e916SJens Axboe struct request *rq = blk_mq_rq_from_pdu(cmd); 52240326d8aSShaohua Li struct bio *bio = rq->bio; 523bc07c10aSMing Lei struct file *file = lo->lo_backing_file; 52486af5952SMing Lei struct bio_vec tmp; 52540326d8aSShaohua Li unsigned int offset; 52686af5952SMing Lei int nr_bvec = 0; 527bc07c10aSMing Lei int ret; 528bc07c10aSMing Lei 52986af5952SMing Lei rq_for_each_bvec(tmp, rq, rq_iter) 53086af5952SMing Lei nr_bvec++; 531bc07c10aSMing Lei 53286af5952SMing Lei if (rq->bio != rq->biotail) { 53386af5952SMing Lei 53486af5952SMing Lei bvec = kmalloc_array(nr_bvec, sizeof(struct bio_vec), 5356da2ec56SKees Cook GFP_NOIO); 53640326d8aSShaohua Li if (!bvec) 53740326d8aSShaohua Li return -EIO; 53840326d8aSShaohua Li cmd->bvec = bvec; 53940326d8aSShaohua Li 540a7297a6aSMing Lei /* 54140326d8aSShaohua Li * The bios of the request may be started from the middle of 54240326d8aSShaohua Li * the 'bvec' because of bio splitting, so we can't directly 54386af5952SMing Lei * copy bio->bi_iov_vec to new bvec. The rq_for_each_bvec 54440326d8aSShaohua Li * API will take care of all details for us. 545a7297a6aSMing Lei */ 54686af5952SMing Lei rq_for_each_bvec(tmp, rq, rq_iter) { 54740326d8aSShaohua Li *bvec = tmp; 54840326d8aSShaohua Li bvec++; 54940326d8aSShaohua Li } 55040326d8aSShaohua Li bvec = cmd->bvec; 55140326d8aSShaohua Li offset = 0; 55240326d8aSShaohua Li } else { 55340326d8aSShaohua Li /* 55440326d8aSShaohua Li * Same here, this bio may be started from the middle of the 55540326d8aSShaohua Li * 'bvec' because of bio splitting, so offset from the bvec 55640326d8aSShaohua Li * must be passed to iov iterator 55740326d8aSShaohua Li */ 55840326d8aSShaohua Li offset = bio->bi_iter.bi_bvec_done; 55940326d8aSShaohua Li bvec = __bvec_iter_bvec(bio->bi_io_vec, bio->bi_iter); 56040326d8aSShaohua Li } 56192d77332SShaohua Li atomic_set(&cmd->ref, 2); 56240326d8aSShaohua Li 563b6207430SChristoph Hellwig iov_iter_bvec(&iter, rw, bvec, nr_bvec, blk_rq_bytes(rq)); 56440326d8aSShaohua Li iter.iov_offset = offset; 565bc07c10aSMing Lei 566bc07c10aSMing Lei cmd->iocb.ki_pos = pos; 567bc07c10aSMing Lei cmd->iocb.ki_filp = file; 568bc07c10aSMing Lei cmd->iocb.ki_complete = lo_rw_aio_complete; 569bc07c10aSMing Lei cmd->iocb.ki_flags = IOCB_DIRECT; 570d9a08a9eSAdam Manzanares cmd->iocb.ki_ioprio = IOPRIO_PRIO_VALUE(IOPRIO_CLASS_NONE, 0); 571d4478e92SShaohua Li if (cmd->css) 572d4478e92SShaohua Li kthread_associate_blkcg(cmd->css); 573bc07c10aSMing Lei 574bc07c10aSMing Lei if (rw == WRITE) 575bb7462b6SMiklos Szeredi ret = call_write_iter(file, &cmd->iocb, &iter); 576bc07c10aSMing Lei else 577bb7462b6SMiklos Szeredi ret = call_read_iter(file, &cmd->iocb, &iter); 578bc07c10aSMing Lei 57992d77332SShaohua Li lo_rw_aio_do_completion(cmd); 580d4478e92SShaohua Li kthread_associate_blkcg(NULL); 58192d77332SShaohua Li 582bc07c10aSMing Lei if (ret != -EIOCBQUEUED) 583bc07c10aSMing Lei cmd->iocb.ki_complete(&cmd->iocb, ret, 0); 584bc07c10aSMing Lei return 0; 585bc07c10aSMing Lei } 586bc07c10aSMing Lei 587c1c87c2bSChristoph Hellwig static int do_req_filebacked(struct loop_device *lo, struct request *rq) 588bc07c10aSMing Lei { 589bc07c10aSMing Lei struct loop_cmd *cmd = blk_mq_rq_to_pdu(rq); 590c1c87c2bSChristoph Hellwig loff_t pos = ((loff_t) blk_rq_pos(rq) << 9) + lo->lo_offset; 591bc07c10aSMing Lei 592bc07c10aSMing Lei /* 593bc07c10aSMing Lei * lo_write_simple and lo_read_simple should have been covered 594bc07c10aSMing Lei * by io submit style function like lo_rw_aio(), one blocker 595bc07c10aSMing Lei * is that lo_read_simple() need to call flush_dcache_page after 596bc07c10aSMing Lei * the page is written from kernel, and it isn't easy to handle 597bc07c10aSMing Lei * this in io submit style function which submits all segments 598bc07c10aSMing Lei * of the req at one time. And direct read IO doesn't need to 599bc07c10aSMing Lei * run flush_dcache_page(). 600bc07c10aSMing Lei */ 601c1c87c2bSChristoph Hellwig switch (req_op(rq)) { 602c1c87c2bSChristoph Hellwig case REQ_OP_FLUSH: 603c1c87c2bSChristoph Hellwig return lo_req_flush(lo, rq); 60419372e27SChristoph Hellwig case REQ_OP_WRITE_ZEROES: 605efcfec57SDarrick J. Wong /* 606efcfec57SDarrick J. Wong * If the caller doesn't want deallocation, call zeroout to 607efcfec57SDarrick J. Wong * write zeroes the range. Otherwise, punch them out. 608efcfec57SDarrick J. Wong */ 609efcfec57SDarrick J. Wong return lo_fallocate(lo, rq, pos, 610efcfec57SDarrick J. Wong (rq->cmd_flags & REQ_NOUNMAP) ? 611efcfec57SDarrick J. Wong FALLOC_FL_ZERO_RANGE : 612efcfec57SDarrick J. Wong FALLOC_FL_PUNCH_HOLE); 613efcfec57SDarrick J. Wong case REQ_OP_DISCARD: 614efcfec57SDarrick J. Wong return lo_fallocate(lo, rq, pos, FALLOC_FL_PUNCH_HOLE); 615c1c87c2bSChristoph Hellwig case REQ_OP_WRITE: 616c1c87c2bSChristoph Hellwig if (lo->transfer) 617c1c87c2bSChristoph Hellwig return lo_write_transfer(lo, rq, pos); 618c1c87c2bSChristoph Hellwig else if (cmd->use_aio) 619c1c87c2bSChristoph Hellwig return lo_rw_aio(lo, cmd, pos, WRITE); 620c1c87c2bSChristoph Hellwig else 621bc07c10aSMing Lei return lo_write_simple(lo, rq, pos); 622c1c87c2bSChristoph Hellwig case REQ_OP_READ: 623c1c87c2bSChristoph Hellwig if (lo->transfer) 624c1c87c2bSChristoph Hellwig return lo_read_transfer(lo, rq, pos); 625c1c87c2bSChristoph Hellwig else if (cmd->use_aio) 626c1c87c2bSChristoph Hellwig return lo_rw_aio(lo, cmd, pos, READ); 627bc07c10aSMing Lei else 628bc07c10aSMing Lei return lo_read_simple(lo, rq, pos); 629c1c87c2bSChristoph Hellwig default: 630c1c87c2bSChristoph Hellwig WARN_ON_ONCE(1); 631c1c87c2bSChristoph Hellwig return -EIO; 632bc07c10aSMing Lei } 6331da177e4SLinus Torvalds } 6341da177e4SLinus Torvalds 6352e5ab5f3SMing Lei static inline void loop_update_dio(struct loop_device *lo) 6362e5ab5f3SMing Lei { 6372e5ab5f3SMing Lei __loop_update_dio(lo, io_is_direct(lo->lo_backing_file) | 6382e5ab5f3SMing Lei lo->use_dio); 6392e5ab5f3SMing Lei } 6402e5ab5f3SMing Lei 64106f0e9e6SMing Lei static void loop_reread_partitions(struct loop_device *lo, 64206f0e9e6SMing Lei struct block_device *bdev) 64306f0e9e6SMing Lei { 64406f0e9e6SMing Lei int rc; 64506f0e9e6SMing Lei 646f0b870dfSChristoph Hellwig mutex_lock(&bdev->bd_mutex); 647f0b870dfSChristoph Hellwig rc = bdev_disk_changed(bdev, false); 648f0b870dfSChristoph Hellwig mutex_unlock(&bdev->bd_mutex); 64906f0e9e6SMing Lei if (rc) 65006f0e9e6SMing Lei pr_warn("%s: partition scan of loop%d (%s) failed (rc=%d)\n", 65106f0e9e6SMing Lei __func__, lo->lo_number, lo->lo_file_name, rc); 65206f0e9e6SMing Lei } 65306f0e9e6SMing Lei 654d2ac838eSTheodore Ts'o static inline int is_loop_device(struct file *file) 655d2ac838eSTheodore Ts'o { 656d2ac838eSTheodore Ts'o struct inode *i = file->f_mapping->host; 657d2ac838eSTheodore Ts'o 658d2ac838eSTheodore Ts'o return i && S_ISBLK(i->i_mode) && MAJOR(i->i_rdev) == LOOP_MAJOR; 659d2ac838eSTheodore Ts'o } 660d2ac838eSTheodore Ts'o 661d2ac838eSTheodore Ts'o static int loop_validate_file(struct file *file, struct block_device *bdev) 662d2ac838eSTheodore Ts'o { 663d2ac838eSTheodore Ts'o struct inode *inode = file->f_mapping->host; 664d2ac838eSTheodore Ts'o struct file *f = file; 665d2ac838eSTheodore Ts'o 666d2ac838eSTheodore Ts'o /* Avoid recursion */ 667d2ac838eSTheodore Ts'o while (is_loop_device(f)) { 668d2ac838eSTheodore Ts'o struct loop_device *l; 669d2ac838eSTheodore Ts'o 670d2ac838eSTheodore Ts'o if (f->f_mapping->host->i_bdev == bdev) 671d2ac838eSTheodore Ts'o return -EBADF; 672d2ac838eSTheodore Ts'o 673d2ac838eSTheodore Ts'o l = f->f_mapping->host->i_bdev->bd_disk->private_data; 674f7c8a412SDongli Zhang if (l->lo_state != Lo_bound) { 675d2ac838eSTheodore Ts'o return -EINVAL; 676d2ac838eSTheodore Ts'o } 677d2ac838eSTheodore Ts'o f = l->lo_backing_file; 678d2ac838eSTheodore Ts'o } 679d2ac838eSTheodore Ts'o if (!S_ISREG(inode->i_mode) && !S_ISBLK(inode->i_mode)) 680d2ac838eSTheodore Ts'o return -EINVAL; 681d2ac838eSTheodore Ts'o return 0; 682d2ac838eSTheodore Ts'o } 683d2ac838eSTheodore Ts'o 6841da177e4SLinus Torvalds /* 6851da177e4SLinus Torvalds * loop_change_fd switched the backing store of a loopback device to 6861da177e4SLinus Torvalds * a new file. This is useful for operating system installers to free up 6871da177e4SLinus Torvalds * the original file and in High Availability environments to switch to 6881da177e4SLinus Torvalds * an alternative location for the content in case of server meltdown. 6891da177e4SLinus Torvalds * This can only work if the loop device is used read-only, and if the 6901da177e4SLinus Torvalds * new backing store is the same size and type as the old backing store. 6911da177e4SLinus Torvalds */ 692bb214884SAl Viro static int loop_change_fd(struct loop_device *lo, struct block_device *bdev, 693bb214884SAl Viro unsigned int arg) 6941da177e4SLinus Torvalds { 6951dded9acSJan Kara struct file *file = NULL, *old_file; 6961da177e4SLinus Torvalds int error; 69785b0a54aSJan Kara bool partscan; 6981da177e4SLinus Torvalds 699c28445faSJan Kara error = mutex_lock_killable(&loop_ctl_mutex); 700c3710770SJan Kara if (error) 701c3710770SJan Kara return error; 7021da177e4SLinus Torvalds error = -ENXIO; 7031da177e4SLinus Torvalds if (lo->lo_state != Lo_bound) 7041dded9acSJan Kara goto out_err; 7051da177e4SLinus Torvalds 7061da177e4SLinus Torvalds /* the loop device has to be read-only */ 7071da177e4SLinus Torvalds error = -EINVAL; 7081da177e4SLinus Torvalds if (!(lo->lo_flags & LO_FLAGS_READ_ONLY)) 7091dded9acSJan Kara goto out_err; 7101da177e4SLinus Torvalds 7111da177e4SLinus Torvalds error = -EBADF; 7121da177e4SLinus Torvalds file = fget(arg); 7131da177e4SLinus Torvalds if (!file) 7141dded9acSJan Kara goto out_err; 7151da177e4SLinus Torvalds 716d2ac838eSTheodore Ts'o error = loop_validate_file(file, bdev); 717d2ac838eSTheodore Ts'o if (error) 7181dded9acSJan Kara goto out_err; 719d2ac838eSTheodore Ts'o 7201da177e4SLinus Torvalds old_file = lo->lo_backing_file; 7211da177e4SLinus Torvalds 7221da177e4SLinus Torvalds error = -EINVAL; 7231da177e4SLinus Torvalds 7241da177e4SLinus Torvalds /* size of the new backing store needs to be the same */ 7251da177e4SLinus Torvalds if (get_loop_size(lo, file) != get_loop_size(lo, old_file)) 7261dded9acSJan Kara goto out_err; 7271da177e4SLinus Torvalds 7281da177e4SLinus Torvalds /* and ... switch */ 72943cade80SOmar Sandoval blk_mq_freeze_queue(lo->lo_queue); 73043cade80SOmar Sandoval mapping_set_gfp_mask(old_file->f_mapping, lo->old_gfp_mask); 73143cade80SOmar Sandoval lo->lo_backing_file = file; 73243cade80SOmar Sandoval lo->old_gfp_mask = mapping_gfp_mask(file->f_mapping); 73343cade80SOmar Sandoval mapping_set_gfp_mask(file->f_mapping, 73443cade80SOmar Sandoval lo->old_gfp_mask & ~(__GFP_IO|__GFP_FS)); 73543cade80SOmar Sandoval loop_update_dio(lo); 73643cade80SOmar Sandoval blk_mq_unfreeze_queue(lo->lo_queue); 73785b0a54aSJan Kara partscan = lo->lo_flags & LO_FLAGS_PARTSCAN; 738c3710770SJan Kara mutex_unlock(&loop_ctl_mutex); 7391dded9acSJan Kara /* 7401dded9acSJan Kara * We must drop file reference outside of loop_ctl_mutex as dropping 7411dded9acSJan Kara * the file ref can take bd_mutex which creates circular locking 7421dded9acSJan Kara * dependency. 7431dded9acSJan Kara */ 7441dded9acSJan Kara fput(old_file); 74585b0a54aSJan Kara if (partscan) 74685b0a54aSJan Kara loop_reread_partitions(lo, bdev); 7471da177e4SLinus Torvalds return 0; 7481da177e4SLinus Torvalds 7491dded9acSJan Kara out_err: 750c3710770SJan Kara mutex_unlock(&loop_ctl_mutex); 7511dded9acSJan Kara if (file) 7521dded9acSJan Kara fput(file); 7531da177e4SLinus Torvalds return error; 7541da177e4SLinus Torvalds } 7551da177e4SLinus Torvalds 756ee862730SMilan Broz /* loop sysfs attributes */ 757ee862730SMilan Broz 758ee862730SMilan Broz static ssize_t loop_attr_show(struct device *dev, char *page, 759ee862730SMilan Broz ssize_t (*callback)(struct loop_device *, char *)) 760ee862730SMilan Broz { 76134dd82afSKay Sievers struct gendisk *disk = dev_to_disk(dev); 76234dd82afSKay Sievers struct loop_device *lo = disk->private_data; 763ee862730SMilan Broz 76434dd82afSKay Sievers return callback(lo, page); 765ee862730SMilan Broz } 766ee862730SMilan Broz 767ee862730SMilan Broz #define LOOP_ATTR_RO(_name) \ 768ee862730SMilan Broz static ssize_t loop_attr_##_name##_show(struct loop_device *, char *); \ 769ee862730SMilan Broz static ssize_t loop_attr_do_show_##_name(struct device *d, \ 770ee862730SMilan Broz struct device_attribute *attr, char *b) \ 771ee862730SMilan Broz { \ 772ee862730SMilan Broz return loop_attr_show(d, b, loop_attr_##_name##_show); \ 773ee862730SMilan Broz } \ 774ee862730SMilan Broz static struct device_attribute loop_attr_##_name = \ 7755657a819SJoe Perches __ATTR(_name, 0444, loop_attr_do_show_##_name, NULL); 776ee862730SMilan Broz 777ee862730SMilan Broz static ssize_t loop_attr_backing_file_show(struct loop_device *lo, char *buf) 778ee862730SMilan Broz { 779ee862730SMilan Broz ssize_t ret; 780ee862730SMilan Broz char *p = NULL; 781ee862730SMilan Broz 78205eb0f25SKay Sievers spin_lock_irq(&lo->lo_lock); 783ee862730SMilan Broz if (lo->lo_backing_file) 7849bf39ab2SMiklos Szeredi p = file_path(lo->lo_backing_file, buf, PAGE_SIZE - 1); 78505eb0f25SKay Sievers spin_unlock_irq(&lo->lo_lock); 786ee862730SMilan Broz 787ee862730SMilan Broz if (IS_ERR_OR_NULL(p)) 788ee862730SMilan Broz ret = PTR_ERR(p); 789ee862730SMilan Broz else { 790ee862730SMilan Broz ret = strlen(p); 791ee862730SMilan Broz memmove(buf, p, ret); 792ee862730SMilan Broz buf[ret++] = '\n'; 793ee862730SMilan Broz buf[ret] = 0; 794ee862730SMilan Broz } 795ee862730SMilan Broz 796ee862730SMilan Broz return ret; 797ee862730SMilan Broz } 798ee862730SMilan Broz 799ee862730SMilan Broz static ssize_t loop_attr_offset_show(struct loop_device *lo, char *buf) 800ee862730SMilan Broz { 801ee862730SMilan Broz return sprintf(buf, "%llu\n", (unsigned long long)lo->lo_offset); 802ee862730SMilan Broz } 803ee862730SMilan Broz 804ee862730SMilan Broz static ssize_t loop_attr_sizelimit_show(struct loop_device *lo, char *buf) 805ee862730SMilan Broz { 806ee862730SMilan Broz return sprintf(buf, "%llu\n", (unsigned long long)lo->lo_sizelimit); 807ee862730SMilan Broz } 808ee862730SMilan Broz 809ee862730SMilan Broz static ssize_t loop_attr_autoclear_show(struct loop_device *lo, char *buf) 810ee862730SMilan Broz { 811ee862730SMilan Broz int autoclear = (lo->lo_flags & LO_FLAGS_AUTOCLEAR); 812ee862730SMilan Broz 813ee862730SMilan Broz return sprintf(buf, "%s\n", autoclear ? "1" : "0"); 814ee862730SMilan Broz } 815ee862730SMilan Broz 816e03c8dd1SKay Sievers static ssize_t loop_attr_partscan_show(struct loop_device *lo, char *buf) 817e03c8dd1SKay Sievers { 818e03c8dd1SKay Sievers int partscan = (lo->lo_flags & LO_FLAGS_PARTSCAN); 819e03c8dd1SKay Sievers 820e03c8dd1SKay Sievers return sprintf(buf, "%s\n", partscan ? "1" : "0"); 821e03c8dd1SKay Sievers } 822e03c8dd1SKay Sievers 8232e5ab5f3SMing Lei static ssize_t loop_attr_dio_show(struct loop_device *lo, char *buf) 8242e5ab5f3SMing Lei { 8252e5ab5f3SMing Lei int dio = (lo->lo_flags & LO_FLAGS_DIRECT_IO); 8262e5ab5f3SMing Lei 8272e5ab5f3SMing Lei return sprintf(buf, "%s\n", dio ? "1" : "0"); 8282e5ab5f3SMing Lei } 8292e5ab5f3SMing Lei 830ee862730SMilan Broz LOOP_ATTR_RO(backing_file); 831ee862730SMilan Broz LOOP_ATTR_RO(offset); 832ee862730SMilan Broz LOOP_ATTR_RO(sizelimit); 833ee862730SMilan Broz LOOP_ATTR_RO(autoclear); 834e03c8dd1SKay Sievers LOOP_ATTR_RO(partscan); 8352e5ab5f3SMing Lei LOOP_ATTR_RO(dio); 836ee862730SMilan Broz 837ee862730SMilan Broz static struct attribute *loop_attrs[] = { 838ee862730SMilan Broz &loop_attr_backing_file.attr, 839ee862730SMilan Broz &loop_attr_offset.attr, 840ee862730SMilan Broz &loop_attr_sizelimit.attr, 841ee862730SMilan Broz &loop_attr_autoclear.attr, 842e03c8dd1SKay Sievers &loop_attr_partscan.attr, 8432e5ab5f3SMing Lei &loop_attr_dio.attr, 844ee862730SMilan Broz NULL, 845ee862730SMilan Broz }; 846ee862730SMilan Broz 847ee862730SMilan Broz static struct attribute_group loop_attribute_group = { 848ee862730SMilan Broz .name = "loop", 849ee862730SMilan Broz .attrs= loop_attrs, 850ee862730SMilan Broz }; 851ee862730SMilan Broz 852d3349b6bSTetsuo Handa static void loop_sysfs_init(struct loop_device *lo) 853ee862730SMilan Broz { 854d3349b6bSTetsuo Handa lo->sysfs_inited = !sysfs_create_group(&disk_to_dev(lo->lo_disk)->kobj, 855ee862730SMilan Broz &loop_attribute_group); 856ee862730SMilan Broz } 857ee862730SMilan Broz 858ee862730SMilan Broz static void loop_sysfs_exit(struct loop_device *lo) 859ee862730SMilan Broz { 860d3349b6bSTetsuo Handa if (lo->sysfs_inited) 861ee862730SMilan Broz sysfs_remove_group(&disk_to_dev(lo->lo_disk)->kobj, 862ee862730SMilan Broz &loop_attribute_group); 863ee862730SMilan Broz } 864ee862730SMilan Broz 865dfaa2ef6SLukas Czerner static void loop_config_discard(struct loop_device *lo) 866dfaa2ef6SLukas Czerner { 867dfaa2ef6SLukas Czerner struct file *file = lo->lo_backing_file; 868dfaa2ef6SLukas Czerner struct inode *inode = file->f_mapping->host; 869dfaa2ef6SLukas Czerner struct request_queue *q = lo->lo_queue; 870dfaa2ef6SLukas Czerner 871dfaa2ef6SLukas Czerner /* 872c52abf56SEvan Green * If the backing device is a block device, mirror its zeroing 873c52abf56SEvan Green * capability. Set the discard sectors to the block device's zeroing 874c52abf56SEvan Green * capabilities because loop discards result in blkdev_issue_zeroout(), 875c52abf56SEvan Green * not blkdev_issue_discard(). This maintains consistent behavior with 876c52abf56SEvan Green * file-backed loop devices: discarded regions read back as zero. 877c52abf56SEvan Green */ 878c52abf56SEvan Green if (S_ISBLK(inode->i_mode) && !lo->lo_encrypt_key_size) { 879c52abf56SEvan Green struct request_queue *backingq; 880c52abf56SEvan Green 881c52abf56SEvan Green backingq = bdev_get_queue(inode->i_bdev); 882c52abf56SEvan Green blk_queue_max_discard_sectors(q, 883c52abf56SEvan Green backingq->limits.max_write_zeroes_sectors); 884c52abf56SEvan Green 885c52abf56SEvan Green blk_queue_max_write_zeroes_sectors(q, 886c52abf56SEvan Green backingq->limits.max_write_zeroes_sectors); 887c52abf56SEvan Green 888c52abf56SEvan Green /* 889dfaa2ef6SLukas Czerner * We use punch hole to reclaim the free space used by the 89012a64d2fSOlaf Hering * image a.k.a. discard. However we do not support discard if 891dfaa2ef6SLukas Czerner * encryption is enabled, because it may give an attacker 892dfaa2ef6SLukas Czerner * useful information. 893dfaa2ef6SLukas Czerner */ 894c52abf56SEvan Green } else if (!file->f_op->fallocate || lo->lo_encrypt_key_size) { 895dfaa2ef6SLukas Czerner q->limits.discard_granularity = 0; 896dfaa2ef6SLukas Czerner q->limits.discard_alignment = 0; 8972bb4cd5cSJens Axboe blk_queue_max_discard_sectors(q, 0); 89819372e27SChristoph Hellwig blk_queue_max_write_zeroes_sectors(q, 0); 899dfaa2ef6SLukas Czerner 900c52abf56SEvan Green } else { 901dfaa2ef6SLukas Czerner q->limits.discard_granularity = inode->i_sb->s_blocksize; 902dfaf3c03SLukas Czerner q->limits.discard_alignment = 0; 903f2c6df7dSHannes Reinecke 9041e6ec9eaSOmar Sandoval blk_queue_max_discard_sectors(q, UINT_MAX >> 9); 9051e6ec9eaSOmar Sandoval blk_queue_max_write_zeroes_sectors(q, UINT_MAX >> 9); 906c52abf56SEvan Green } 907c52abf56SEvan Green 908c52abf56SEvan Green if (q->limits.max_write_zeroes_sectors) 9098b904b5bSBart Van Assche blk_queue_flag_set(QUEUE_FLAG_DISCARD, q); 910c52abf56SEvan Green else 911c52abf56SEvan Green blk_queue_flag_clear(QUEUE_FLAG_DISCARD, q); 912dfaa2ef6SLukas Czerner } 913dfaa2ef6SLukas Czerner 914e03a3d7aSMing Lei static void loop_unprepare_queue(struct loop_device *lo) 915e03a3d7aSMing Lei { 9163989144fSPetr Mladek kthread_flush_worker(&lo->worker); 917e03a3d7aSMing Lei kthread_stop(lo->worker_task); 918e03a3d7aSMing Lei } 919e03a3d7aSMing Lei 920b2ee7d46SNeilBrown static int loop_kthread_worker_fn(void *worker_ptr) 921b2ee7d46SNeilBrown { 922*a37b0715SNeilBrown current->flags |= PF_LOCAL_THROTTLE | PF_MEMALLOC_NOIO; 923b2ee7d46SNeilBrown return kthread_worker_fn(worker_ptr); 924b2ee7d46SNeilBrown } 925b2ee7d46SNeilBrown 926e03a3d7aSMing Lei static int loop_prepare_queue(struct loop_device *lo) 927e03a3d7aSMing Lei { 9283989144fSPetr Mladek kthread_init_worker(&lo->worker); 929b2ee7d46SNeilBrown lo->worker_task = kthread_run(loop_kthread_worker_fn, 930e03a3d7aSMing Lei &lo->worker, "loop%d", lo->lo_number); 931e03a3d7aSMing Lei if (IS_ERR(lo->worker_task)) 932e03a3d7aSMing Lei return -ENOMEM; 933e03a3d7aSMing Lei set_user_nice(lo->worker_task, MIN_NICE); 934e03a3d7aSMing Lei return 0; 935e03a3d7aSMing Lei } 936e03a3d7aSMing Lei 93756a85fd8SHolger Hoffstätte static void loop_update_rotational(struct loop_device *lo) 93856a85fd8SHolger Hoffstätte { 93956a85fd8SHolger Hoffstätte struct file *file = lo->lo_backing_file; 94056a85fd8SHolger Hoffstätte struct inode *file_inode = file->f_mapping->host; 94156a85fd8SHolger Hoffstätte struct block_device *file_bdev = file_inode->i_sb->s_bdev; 94256a85fd8SHolger Hoffstätte struct request_queue *q = lo->lo_queue; 94356a85fd8SHolger Hoffstätte bool nonrot = true; 94456a85fd8SHolger Hoffstätte 94556a85fd8SHolger Hoffstätte /* not all filesystems (e.g. tmpfs) have a sb->s_bdev */ 94656a85fd8SHolger Hoffstätte if (file_bdev) 94756a85fd8SHolger Hoffstätte nonrot = blk_queue_nonrot(bdev_get_queue(file_bdev)); 94856a85fd8SHolger Hoffstätte 94956a85fd8SHolger Hoffstätte if (nonrot) 95056a85fd8SHolger Hoffstätte blk_queue_flag_set(QUEUE_FLAG_NONROT, q); 95156a85fd8SHolger Hoffstätte else 95256a85fd8SHolger Hoffstätte blk_queue_flag_clear(QUEUE_FLAG_NONROT, q); 95356a85fd8SHolger Hoffstätte } 95456a85fd8SHolger Hoffstätte 955bb214884SAl Viro static int loop_set_fd(struct loop_device *lo, fmode_t mode, 9561da177e4SLinus Torvalds struct block_device *bdev, unsigned int arg) 9571da177e4SLinus Torvalds { 958d2ac838eSTheodore Ts'o struct file *file; 9591da177e4SLinus Torvalds struct inode *inode; 9601da177e4SLinus Torvalds struct address_space *mapping; 96189e524c0SJan Kara struct block_device *claimed_bdev = NULL; 9621da177e4SLinus Torvalds int lo_flags = 0; 9631da177e4SLinus Torvalds int error; 9641da177e4SLinus Torvalds loff_t size; 96585b0a54aSJan Kara bool partscan; 9661da177e4SLinus Torvalds 9671da177e4SLinus Torvalds /* This is safe, since we have a reference from open(). */ 9681da177e4SLinus Torvalds __module_get(THIS_MODULE); 9691da177e4SLinus Torvalds 9701da177e4SLinus Torvalds error = -EBADF; 9711da177e4SLinus Torvalds file = fget(arg); 9721da177e4SLinus Torvalds if (!file) 9731da177e4SLinus Torvalds goto out; 9741da177e4SLinus Torvalds 97533ec3e53SJan Kara /* 97633ec3e53SJan Kara * If we don't hold exclusive handle for the device, upgrade to it 97733ec3e53SJan Kara * here to avoid changing device under exclusive owner. 97833ec3e53SJan Kara */ 97933ec3e53SJan Kara if (!(mode & FMODE_EXCL)) { 98089e524c0SJan Kara claimed_bdev = bd_start_claiming(bdev, loop_set_fd); 98189e524c0SJan Kara if (IS_ERR(claimed_bdev)) { 98289e524c0SJan Kara error = PTR_ERR(claimed_bdev); 983757ecf40SJan Kara goto out_putf; 98433ec3e53SJan Kara } 98589e524c0SJan Kara } 98633ec3e53SJan Kara 98733ec3e53SJan Kara error = mutex_lock_killable(&loop_ctl_mutex); 98833ec3e53SJan Kara if (error) 98933ec3e53SJan Kara goto out_bdev; 990757ecf40SJan Kara 9911da177e4SLinus Torvalds error = -EBUSY; 9921da177e4SLinus Torvalds if (lo->lo_state != Lo_unbound) 993757ecf40SJan Kara goto out_unlock; 9941da177e4SLinus Torvalds 995d2ac838eSTheodore Ts'o error = loop_validate_file(file, bdev); 996d2ac838eSTheodore Ts'o if (error) 997757ecf40SJan Kara goto out_unlock; 9981da177e4SLinus Torvalds 9991da177e4SLinus Torvalds mapping = file->f_mapping; 10001da177e4SLinus Torvalds inode = mapping->host; 10011da177e4SLinus Torvalds 1002456be148SChristoph Hellwig if (!(file->f_mode & FMODE_WRITE) || !(mode & FMODE_WRITE) || 1003283e7e5dSAl Viro !file->f_op->write_iter) 10041da177e4SLinus Torvalds lo_flags |= LO_FLAGS_READ_ONLY; 10051da177e4SLinus Torvalds 10061da177e4SLinus Torvalds error = -EFBIG; 1007456be148SChristoph Hellwig size = get_loop_size(lo, file); 1008456be148SChristoph Hellwig if ((loff_t)(sector_t)size != size) 1009757ecf40SJan Kara goto out_unlock; 1010e03a3d7aSMing Lei error = loop_prepare_queue(lo); 1011e03a3d7aSMing Lei if (error) 1012757ecf40SJan Kara goto out_unlock; 10131da177e4SLinus Torvalds 1014456be148SChristoph Hellwig error = 0; 10151da177e4SLinus Torvalds 10161da177e4SLinus Torvalds set_device_ro(bdev, (lo_flags & LO_FLAGS_READ_ONLY) != 0); 10171da177e4SLinus Torvalds 10182e5ab5f3SMing Lei lo->use_dio = false; 10191da177e4SLinus Torvalds lo->lo_device = bdev; 10201da177e4SLinus Torvalds lo->lo_flags = lo_flags; 10211da177e4SLinus Torvalds lo->lo_backing_file = file; 1022aa4d8616SChristoph Hellwig lo->transfer = NULL; 10231da177e4SLinus Torvalds lo->ioctl = NULL; 10241da177e4SLinus Torvalds lo->lo_sizelimit = 0; 10251da177e4SLinus Torvalds lo->old_gfp_mask = mapping_gfp_mask(mapping); 10261da177e4SLinus Torvalds mapping_set_gfp_mask(mapping, lo->old_gfp_mask & ~(__GFP_IO|__GFP_FS)); 10271da177e4SLinus Torvalds 102868db1961SNikanth Karthikesan if (!(lo_flags & LO_FLAGS_READ_ONLY) && file->f_op->fsync) 102921d0727fSJens Axboe blk_queue_write_cache(lo->lo_queue, true, false); 103068db1961SNikanth Karthikesan 103185560117SMartijn Coenen if (io_is_direct(lo->lo_backing_file) && inode->i_sb->s_bdev) { 103285560117SMartijn Coenen /* In case of direct I/O, match underlying block size */ 103385560117SMartijn Coenen unsigned short bsize = bdev_logical_block_size( 103485560117SMartijn Coenen inode->i_sb->s_bdev); 103585560117SMartijn Coenen 103685560117SMartijn Coenen blk_queue_logical_block_size(lo->lo_queue, bsize); 103785560117SMartijn Coenen blk_queue_physical_block_size(lo->lo_queue, bsize); 103885560117SMartijn Coenen blk_queue_io_min(lo->lo_queue, bsize); 103985560117SMartijn Coenen } 104085560117SMartijn Coenen 104156a85fd8SHolger Hoffstätte loop_update_rotational(lo); 10422e5ab5f3SMing Lei loop_update_dio(lo); 104373285082SKen Chen set_capacity(lo->lo_disk, size); 10441da177e4SLinus Torvalds bd_set_size(bdev, size << 9); 1045ee862730SMilan Broz loop_sysfs_init(lo); 1046c3473c63SDavid Zeuthen /* let user-space know about the new size */ 1047c3473c63SDavid Zeuthen kobject_uevent(&disk_to_dev(bdev->bd_disk)->kobj, KOBJ_CHANGE); 10481da177e4SLinus Torvalds 10498a0740c4SOmar Sandoval set_blocksize(bdev, S_ISBLK(inode->i_mode) ? 10508a0740c4SOmar Sandoval block_size(inode->i_bdev) : PAGE_SIZE); 10511da177e4SLinus Torvalds 10526c997918SSerge E. Hallyn lo->lo_state = Lo_bound; 1053e03c8dd1SKay Sievers if (part_shift) 1054e03c8dd1SKay Sievers lo->lo_flags |= LO_FLAGS_PARTSCAN; 105585b0a54aSJan Kara partscan = lo->lo_flags & LO_FLAGS_PARTSCAN; 1056c1681bf8SAnatol Pomozov 1057c1681bf8SAnatol Pomozov /* Grab the block_device to prevent its destruction after we 1058a2505b79SJan Kara * put /dev/loopXX inode. Later in __loop_clr_fd() we bdput(bdev). 1059c1681bf8SAnatol Pomozov */ 1060c1681bf8SAnatol Pomozov bdgrab(bdev); 1061757ecf40SJan Kara mutex_unlock(&loop_ctl_mutex); 106285b0a54aSJan Kara if (partscan) 106385b0a54aSJan Kara loop_reread_partitions(lo, bdev); 106489e524c0SJan Kara if (claimed_bdev) 106589e524c0SJan Kara bd_abort_claiming(bdev, claimed_bdev, loop_set_fd); 10661da177e4SLinus Torvalds return 0; 10671da177e4SLinus Torvalds 1068757ecf40SJan Kara out_unlock: 1069757ecf40SJan Kara mutex_unlock(&loop_ctl_mutex); 107033ec3e53SJan Kara out_bdev: 107189e524c0SJan Kara if (claimed_bdev) 107289e524c0SJan Kara bd_abort_claiming(bdev, claimed_bdev, loop_set_fd); 10731da177e4SLinus Torvalds out_putf: 10741da177e4SLinus Torvalds fput(file); 10751da177e4SLinus Torvalds out: 10761da177e4SLinus Torvalds /* This is safe: open() is still holding a reference. */ 10771da177e4SLinus Torvalds module_put(THIS_MODULE); 10781da177e4SLinus Torvalds return error; 10791da177e4SLinus Torvalds } 10801da177e4SLinus Torvalds 10811da177e4SLinus Torvalds static int 10821da177e4SLinus Torvalds loop_release_xfer(struct loop_device *lo) 10831da177e4SLinus Torvalds { 10841da177e4SLinus Torvalds int err = 0; 10851da177e4SLinus Torvalds struct loop_func_table *xfer = lo->lo_encryption; 10861da177e4SLinus Torvalds 10871da177e4SLinus Torvalds if (xfer) { 10881da177e4SLinus Torvalds if (xfer->release) 10891da177e4SLinus Torvalds err = xfer->release(lo); 10901da177e4SLinus Torvalds lo->transfer = NULL; 10911da177e4SLinus Torvalds lo->lo_encryption = NULL; 10921da177e4SLinus Torvalds module_put(xfer->owner); 10931da177e4SLinus Torvalds } 10941da177e4SLinus Torvalds return err; 10951da177e4SLinus Torvalds } 10961da177e4SLinus Torvalds 10971da177e4SLinus Torvalds static int 10981da177e4SLinus Torvalds loop_init_xfer(struct loop_device *lo, struct loop_func_table *xfer, 10991da177e4SLinus Torvalds const struct loop_info64 *i) 11001da177e4SLinus Torvalds { 11011da177e4SLinus Torvalds int err = 0; 11021da177e4SLinus Torvalds 11031da177e4SLinus Torvalds if (xfer) { 11041da177e4SLinus Torvalds struct module *owner = xfer->owner; 11051da177e4SLinus Torvalds 11061da177e4SLinus Torvalds if (!try_module_get(owner)) 11071da177e4SLinus Torvalds return -EINVAL; 11081da177e4SLinus Torvalds if (xfer->init) 11091da177e4SLinus Torvalds err = xfer->init(lo, i); 11101da177e4SLinus Torvalds if (err) 11111da177e4SLinus Torvalds module_put(owner); 11121da177e4SLinus Torvalds else 11131da177e4SLinus Torvalds lo->lo_encryption = xfer; 11141da177e4SLinus Torvalds } 11151da177e4SLinus Torvalds return err; 11161da177e4SLinus Torvalds } 11171da177e4SLinus Torvalds 11180da03cabSJan Kara static int __loop_clr_fd(struct loop_device *lo, bool release) 11191da177e4SLinus Torvalds { 11207ccd0791SJan Kara struct file *filp = NULL; 1121b4e3ca1aSAl Viro gfp_t gfp = lo->old_gfp_mask; 11224c823cc3SAyan George struct block_device *bdev = lo->lo_device; 11237ccd0791SJan Kara int err = 0; 11240da03cabSJan Kara bool partscan = false; 11250da03cabSJan Kara int lo_number; 11261da177e4SLinus Torvalds 11277ccd0791SJan Kara mutex_lock(&loop_ctl_mutex); 11287ccd0791SJan Kara if (WARN_ON_ONCE(lo->lo_state != Lo_rundown)) { 11297ccd0791SJan Kara err = -ENXIO; 11307ccd0791SJan Kara goto out_unlock; 11317ccd0791SJan Kara } 11321da177e4SLinus Torvalds 11337ccd0791SJan Kara filp = lo->lo_backing_file; 11347ccd0791SJan Kara if (filp == NULL) { 11357ccd0791SJan Kara err = -EINVAL; 11367ccd0791SJan Kara goto out_unlock; 11377ccd0791SJan Kara } 11381da177e4SLinus Torvalds 1139f8933667SMing Lei /* freeze request queue during the transition */ 1140f8933667SMing Lei blk_mq_freeze_queue(lo->lo_queue); 1141f8933667SMing Lei 11421da177e4SLinus Torvalds spin_lock_irq(&lo->lo_lock); 11431da177e4SLinus Torvalds lo->lo_backing_file = NULL; 114405eb0f25SKay Sievers spin_unlock_irq(&lo->lo_lock); 11451da177e4SLinus Torvalds 11461da177e4SLinus Torvalds loop_release_xfer(lo); 11471da177e4SLinus Torvalds lo->transfer = NULL; 11481da177e4SLinus Torvalds lo->ioctl = NULL; 11491da177e4SLinus Torvalds lo->lo_device = NULL; 11501da177e4SLinus Torvalds lo->lo_encryption = NULL; 11511da177e4SLinus Torvalds lo->lo_offset = 0; 11521da177e4SLinus Torvalds lo->lo_sizelimit = 0; 11531da177e4SLinus Torvalds lo->lo_encrypt_key_size = 0; 11541da177e4SLinus Torvalds memset(lo->lo_encrypt_key, 0, LO_KEY_SIZE); 11551da177e4SLinus Torvalds memset(lo->lo_crypt_name, 0, LO_NAME_SIZE); 11561da177e4SLinus Torvalds memset(lo->lo_file_name, 0, LO_NAME_SIZE); 115789e4fdecSOmar Sandoval blk_queue_logical_block_size(lo->lo_queue, 512); 1158bf093753SOmar Sandoval blk_queue_physical_block_size(lo->lo_queue, 512); 1159bf093753SOmar Sandoval blk_queue_io_min(lo->lo_queue, 512); 1160c1681bf8SAnatol Pomozov if (bdev) { 1161c1681bf8SAnatol Pomozov bdput(bdev); 1162f98393a6SPeter Zijlstra invalidate_bdev(bdev); 1163eedffa28SJeff Layton bdev->bd_inode->i_mapping->wb_err = 0; 1164c1681bf8SAnatol Pomozov } 116573285082SKen Chen set_capacity(lo->lo_disk, 0); 116651a0bb0cSMilan Broz loop_sysfs_exit(lo); 1167c3473c63SDavid Zeuthen if (bdev) { 11681da177e4SLinus Torvalds bd_set_size(bdev, 0); 1169c3473c63SDavid Zeuthen /* let user-space know about this change */ 1170c3473c63SDavid Zeuthen kobject_uevent(&disk_to_dev(bdev->bd_disk)->kobj, KOBJ_CHANGE); 1171c3473c63SDavid Zeuthen } 11721da177e4SLinus Torvalds mapping_set_gfp_mask(filp->f_mapping, gfp); 11731da177e4SLinus Torvalds /* This is safe: open() is still holding a reference. */ 11741da177e4SLinus Torvalds module_put(THIS_MODULE); 1175f8933667SMing Lei blk_mq_unfreeze_queue(lo->lo_queue); 1176f8933667SMing Lei 11770da03cabSJan Kara partscan = lo->lo_flags & LO_FLAGS_PARTSCAN && bdev; 11780da03cabSJan Kara lo_number = lo->lo_number; 11790da03cabSJan Kara loop_unprepare_queue(lo); 11800da03cabSJan Kara out_unlock: 11810da03cabSJan Kara mutex_unlock(&loop_ctl_mutex); 11820da03cabSJan Kara if (partscan) { 1183d57f3374SJan Kara /* 1184d57f3374SJan Kara * bd_mutex has been held already in release path, so don't 1185d57f3374SJan Kara * acquire it if this function is called in such case. 1186d57f3374SJan Kara * 1187d57f3374SJan Kara * If the reread partition isn't from release path, lo_refcnt 1188d57f3374SJan Kara * must be at least one and it can only become zero when the 1189d57f3374SJan Kara * current holder is released. 1190d57f3374SJan Kara */ 1191f0b870dfSChristoph Hellwig if (!release) 1192f0b870dfSChristoph Hellwig mutex_lock(&bdev->bd_mutex); 1193f0b870dfSChristoph Hellwig err = bdev_disk_changed(bdev, false); 1194f0b870dfSChristoph Hellwig if (!release) 1195f0b870dfSChristoph Hellwig mutex_unlock(&bdev->bd_mutex); 119640853d6fSDongli Zhang if (err) 1197d57f3374SJan Kara pr_warn("%s: partition scan of loop%d failed (rc=%d)\n", 11980da03cabSJan Kara __func__, lo_number, err); 1199d57f3374SJan Kara /* Device is gone, no point in returning error */ 1200d57f3374SJan Kara err = 0; 1201d57f3374SJan Kara } 1202758a58d0SDongli Zhang 1203758a58d0SDongli Zhang /* 1204758a58d0SDongli Zhang * lo->lo_state is set to Lo_unbound here after above partscan has 1205758a58d0SDongli Zhang * finished. 1206758a58d0SDongli Zhang * 1207758a58d0SDongli Zhang * There cannot be anybody else entering __loop_clr_fd() as 1208758a58d0SDongli Zhang * lo->lo_backing_file is already cleared and Lo_rundown state 1209758a58d0SDongli Zhang * protects us from all the other places trying to change the 'lo' 1210758a58d0SDongli Zhang * device. 1211758a58d0SDongli Zhang */ 1212758a58d0SDongli Zhang mutex_lock(&loop_ctl_mutex); 1213758a58d0SDongli Zhang lo->lo_flags = 0; 1214758a58d0SDongli Zhang if (!part_shift) 1215758a58d0SDongli Zhang lo->lo_disk->flags |= GENHD_FL_NO_PART_SCAN; 1216758a58d0SDongli Zhang lo->lo_state = Lo_unbound; 1217758a58d0SDongli Zhang mutex_unlock(&loop_ctl_mutex); 1218758a58d0SDongli Zhang 1219f028f3b2SNikanth Karthikesan /* 1220310ca162STetsuo Handa * Need not hold loop_ctl_mutex to fput backing file. 1221310ca162STetsuo Handa * Calling fput holding loop_ctl_mutex triggers a circular 1222f028f3b2SNikanth Karthikesan * lock dependency possibility warning as fput can take 1223310ca162STetsuo Handa * bd_mutex which is usually taken before loop_ctl_mutex. 1224f028f3b2SNikanth Karthikesan */ 12257ccd0791SJan Kara if (filp) 1226f028f3b2SNikanth Karthikesan fput(filp); 12277ccd0791SJan Kara return err; 12281da177e4SLinus Torvalds } 12291da177e4SLinus Torvalds 1230a2505b79SJan Kara static int loop_clr_fd(struct loop_device *lo) 1231a2505b79SJan Kara { 12327ccd0791SJan Kara int err; 12337ccd0791SJan Kara 1234c28445faSJan Kara err = mutex_lock_killable(&loop_ctl_mutex); 12357ccd0791SJan Kara if (err) 12367ccd0791SJan Kara return err; 12377ccd0791SJan Kara if (lo->lo_state != Lo_bound) { 12387ccd0791SJan Kara mutex_unlock(&loop_ctl_mutex); 1239a2505b79SJan Kara return -ENXIO; 12407ccd0791SJan Kara } 1241a2505b79SJan Kara /* 1242a2505b79SJan Kara * If we've explicitly asked to tear down the loop device, 1243a2505b79SJan Kara * and it has an elevated reference count, set it for auto-teardown when 1244a2505b79SJan Kara * the last reference goes away. This stops $!~#$@ udev from 1245a2505b79SJan Kara * preventing teardown because it decided that it needs to run blkid on 1246a2505b79SJan Kara * the loopback device whenever they appear. xfstests is notorious for 1247a2505b79SJan Kara * failing tests because blkid via udev races with a losetup 1248a2505b79SJan Kara * <dev>/do something like mkfs/losetup -d <dev> causing the losetup -d 1249a2505b79SJan Kara * command to fail with EBUSY. 1250a2505b79SJan Kara */ 1251a2505b79SJan Kara if (atomic_read(&lo->lo_refcnt) > 1) { 1252a2505b79SJan Kara lo->lo_flags |= LO_FLAGS_AUTOCLEAR; 1253a2505b79SJan Kara mutex_unlock(&loop_ctl_mutex); 1254a2505b79SJan Kara return 0; 1255a2505b79SJan Kara } 1256a2505b79SJan Kara lo->lo_state = Lo_rundown; 12577ccd0791SJan Kara mutex_unlock(&loop_ctl_mutex); 1258a2505b79SJan Kara 12590da03cabSJan Kara return __loop_clr_fd(lo, false); 1260a2505b79SJan Kara } 1261a2505b79SJan Kara 12621da177e4SLinus Torvalds static int 12631da177e4SLinus Torvalds loop_set_status(struct loop_device *lo, const struct loop_info64 *info) 12641da177e4SLinus Torvalds { 12651da177e4SLinus Torvalds int err; 12661da177e4SLinus Torvalds struct loop_func_table *xfer; 1267e4849737SEric W. Biederman kuid_t uid = current_uid(); 126885b0a54aSJan Kara struct block_device *bdev; 126985b0a54aSJan Kara bool partscan = false; 12701da177e4SLinus Torvalds 1271c28445faSJan Kara err = mutex_lock_killable(&loop_ctl_mutex); 1272550df5fdSJan Kara if (err) 1273550df5fdSJan Kara return err; 1274b0fafa81SDavid Howells if (lo->lo_encrypt_key_size && 1275e4849737SEric W. Biederman !uid_eq(lo->lo_key_owner, uid) && 1276550df5fdSJan Kara !capable(CAP_SYS_ADMIN)) { 1277550df5fdSJan Kara err = -EPERM; 1278550df5fdSJan Kara goto out_unlock; 1279550df5fdSJan Kara } 1280550df5fdSJan Kara if (lo->lo_state != Lo_bound) { 1281550df5fdSJan Kara err = -ENXIO; 1282550df5fdSJan Kara goto out_unlock; 1283550df5fdSJan Kara } 1284550df5fdSJan Kara if ((unsigned int) info->lo_encrypt_key_size > LO_KEY_SIZE) { 1285550df5fdSJan Kara err = -EINVAL; 1286550df5fdSJan Kara goto out_unlock; 1287550df5fdSJan Kara } 12881da177e4SLinus Torvalds 12895db470e2SJaegeuk Kim if (lo->lo_offset != info->lo_offset || 12905db470e2SJaegeuk Kim lo->lo_sizelimit != info->lo_sizelimit) { 12915db470e2SJaegeuk Kim sync_blockdev(lo->lo_device); 12925db470e2SJaegeuk Kim kill_bdev(lo->lo_device); 12935db470e2SJaegeuk Kim } 12945db470e2SJaegeuk Kim 1295ecdd0959SMing Lei /* I/O need to be drained during transfer transition */ 1296ecdd0959SMing Lei blk_mq_freeze_queue(lo->lo_queue); 1297ecdd0959SMing Lei 12981da177e4SLinus Torvalds err = loop_release_xfer(lo); 12991da177e4SLinus Torvalds if (err) 1300550df5fdSJan Kara goto out_unfreeze; 13011da177e4SLinus Torvalds 13021da177e4SLinus Torvalds if (info->lo_encrypt_type) { 13031da177e4SLinus Torvalds unsigned int type = info->lo_encrypt_type; 13041da177e4SLinus Torvalds 13051e047eaaSTetsuo Handa if (type >= MAX_LO_CRYPT) { 13061e047eaaSTetsuo Handa err = -EINVAL; 1307550df5fdSJan Kara goto out_unfreeze; 13081e047eaaSTetsuo Handa } 13091da177e4SLinus Torvalds xfer = xfer_funcs[type]; 13101e047eaaSTetsuo Handa if (xfer == NULL) { 13111e047eaaSTetsuo Handa err = -EINVAL; 1312550df5fdSJan Kara goto out_unfreeze; 13131e047eaaSTetsuo Handa } 13141da177e4SLinus Torvalds } else 13151da177e4SLinus Torvalds xfer = NULL; 13161da177e4SLinus Torvalds 13171da177e4SLinus Torvalds err = loop_init_xfer(lo, xfer, info); 13181da177e4SLinus Torvalds if (err) 1319550df5fdSJan Kara goto out_unfreeze; 13201da177e4SLinus Torvalds 13211da177e4SLinus Torvalds if (lo->lo_offset != info->lo_offset || 13221e6ec9eaSOmar Sandoval lo->lo_sizelimit != info->lo_sizelimit) { 13235db470e2SJaegeuk Kim /* kill_bdev should have truncated all the pages */ 13245db470e2SJaegeuk Kim if (lo->lo_device->bd_inode->i_mapping->nrpages) { 13255db470e2SJaegeuk Kim err = -EAGAIN; 13265db470e2SJaegeuk Kim pr_warn("%s: loop%d (%s) has still dirty pages (nrpages=%lu)\n", 13275db470e2SJaegeuk Kim __func__, lo->lo_number, lo->lo_file_name, 13285db470e2SJaegeuk Kim lo->lo_device->bd_inode->i_mapping->nrpages); 13295db470e2SJaegeuk Kim goto out_unfreeze; 13305db470e2SJaegeuk Kim } 13311e6ec9eaSOmar Sandoval if (figure_loop_size(lo, info->lo_offset, info->lo_sizelimit)) { 1332ecdd0959SMing Lei err = -EFBIG; 1333550df5fdSJan Kara goto out_unfreeze; 1334ecdd0959SMing Lei } 1335b040ad9cSArnd Bergmann } 1336541c742aSGuo Chao 1337dfaa2ef6SLukas Czerner loop_config_discard(lo); 13381da177e4SLinus Torvalds 13391da177e4SLinus Torvalds memcpy(lo->lo_file_name, info->lo_file_name, LO_NAME_SIZE); 13401da177e4SLinus Torvalds memcpy(lo->lo_crypt_name, info->lo_crypt_name, LO_NAME_SIZE); 13411da177e4SLinus Torvalds lo->lo_file_name[LO_NAME_SIZE-1] = 0; 13421da177e4SLinus Torvalds lo->lo_crypt_name[LO_NAME_SIZE-1] = 0; 13431da177e4SLinus Torvalds 13441da177e4SLinus Torvalds if (!xfer) 13451da177e4SLinus Torvalds xfer = &none_funcs; 13461da177e4SLinus Torvalds lo->transfer = xfer->transfer; 13471da177e4SLinus Torvalds lo->ioctl = xfer->ioctl; 13481da177e4SLinus Torvalds 134996c58655SDavid Woodhouse if ((lo->lo_flags & LO_FLAGS_AUTOCLEAR) != 135096c58655SDavid Woodhouse (info->lo_flags & LO_FLAGS_AUTOCLEAR)) 135196c58655SDavid Woodhouse lo->lo_flags ^= LO_FLAGS_AUTOCLEAR; 135296c58655SDavid Woodhouse 13531da177e4SLinus Torvalds lo->lo_encrypt_key_size = info->lo_encrypt_key_size; 13541da177e4SLinus Torvalds lo->lo_init[0] = info->lo_init[0]; 13551da177e4SLinus Torvalds lo->lo_init[1] = info->lo_init[1]; 13561da177e4SLinus Torvalds if (info->lo_encrypt_key_size) { 13571da177e4SLinus Torvalds memcpy(lo->lo_encrypt_key, info->lo_encrypt_key, 13581da177e4SLinus Torvalds info->lo_encrypt_key_size); 1359b0fafa81SDavid Howells lo->lo_key_owner = uid; 13601da177e4SLinus Torvalds } 13611da177e4SLinus Torvalds 13622e5ab5f3SMing Lei /* update dio if lo_offset or transfer is changed */ 13632e5ab5f3SMing Lei __loop_update_dio(lo, lo->use_dio); 13642e5ab5f3SMing Lei 1365550df5fdSJan Kara out_unfreeze: 1366ecdd0959SMing Lei blk_mq_unfreeze_queue(lo->lo_queue); 1367e02898b4SOmar Sandoval 1368e02898b4SOmar Sandoval if (!err && (info->lo_flags & LO_FLAGS_PARTSCAN) && 1369e02898b4SOmar Sandoval !(lo->lo_flags & LO_FLAGS_PARTSCAN)) { 1370e02898b4SOmar Sandoval lo->lo_flags |= LO_FLAGS_PARTSCAN; 1371e02898b4SOmar Sandoval lo->lo_disk->flags &= ~GENHD_FL_NO_PART_SCAN; 137285b0a54aSJan Kara bdev = lo->lo_device; 137385b0a54aSJan Kara partscan = true; 1374e02898b4SOmar Sandoval } 1375550df5fdSJan Kara out_unlock: 1376550df5fdSJan Kara mutex_unlock(&loop_ctl_mutex); 137785b0a54aSJan Kara if (partscan) 137885b0a54aSJan Kara loop_reread_partitions(lo, bdev); 1379e02898b4SOmar Sandoval 1380ecdd0959SMing Lei return err; 13811da177e4SLinus Torvalds } 13821da177e4SLinus Torvalds 13831da177e4SLinus Torvalds static int 13841da177e4SLinus Torvalds loop_get_status(struct loop_device *lo, struct loop_info64 *info) 13851da177e4SLinus Torvalds { 1386b1ab5fa3STetsuo Handa struct path path; 13871da177e4SLinus Torvalds struct kstat stat; 13882d1d4c1eSOmar Sandoval int ret; 13891da177e4SLinus Torvalds 1390c28445faSJan Kara ret = mutex_lock_killable(&loop_ctl_mutex); 13914a5ce9baSJan Kara if (ret) 13924a5ce9baSJan Kara return ret; 13932d1d4c1eSOmar Sandoval if (lo->lo_state != Lo_bound) { 1394310ca162STetsuo Handa mutex_unlock(&loop_ctl_mutex); 13951da177e4SLinus Torvalds return -ENXIO; 13962d1d4c1eSOmar Sandoval } 13972d1d4c1eSOmar Sandoval 13981da177e4SLinus Torvalds memset(info, 0, sizeof(*info)); 13991da177e4SLinus Torvalds info->lo_number = lo->lo_number; 14001da177e4SLinus Torvalds info->lo_offset = lo->lo_offset; 14011da177e4SLinus Torvalds info->lo_sizelimit = lo->lo_sizelimit; 14021da177e4SLinus Torvalds info->lo_flags = lo->lo_flags; 14031da177e4SLinus Torvalds memcpy(info->lo_file_name, lo->lo_file_name, LO_NAME_SIZE); 14041da177e4SLinus Torvalds memcpy(info->lo_crypt_name, lo->lo_crypt_name, LO_NAME_SIZE); 14051da177e4SLinus Torvalds info->lo_encrypt_type = 14061da177e4SLinus Torvalds lo->lo_encryption ? lo->lo_encryption->number : 0; 14071da177e4SLinus Torvalds if (lo->lo_encrypt_key_size && capable(CAP_SYS_ADMIN)) { 14081da177e4SLinus Torvalds info->lo_encrypt_key_size = lo->lo_encrypt_key_size; 14091da177e4SLinus Torvalds memcpy(info->lo_encrypt_key, lo->lo_encrypt_key, 14101da177e4SLinus Torvalds lo->lo_encrypt_key_size); 14111da177e4SLinus Torvalds } 14122d1d4c1eSOmar Sandoval 1413310ca162STetsuo Handa /* Drop loop_ctl_mutex while we call into the filesystem. */ 1414b1ab5fa3STetsuo Handa path = lo->lo_backing_file->f_path; 1415b1ab5fa3STetsuo Handa path_get(&path); 1416310ca162STetsuo Handa mutex_unlock(&loop_ctl_mutex); 1417b1ab5fa3STetsuo Handa ret = vfs_getattr(&path, &stat, STATX_INO, AT_STATX_SYNC_AS_STAT); 14182d1d4c1eSOmar Sandoval if (!ret) { 14192d1d4c1eSOmar Sandoval info->lo_device = huge_encode_dev(stat.dev); 14202d1d4c1eSOmar Sandoval info->lo_inode = stat.ino; 14212d1d4c1eSOmar Sandoval info->lo_rdevice = huge_encode_dev(stat.rdev); 14222d1d4c1eSOmar Sandoval } 1423b1ab5fa3STetsuo Handa path_put(&path); 14242d1d4c1eSOmar Sandoval return ret; 14251da177e4SLinus Torvalds } 14261da177e4SLinus Torvalds 14271da177e4SLinus Torvalds static void 14281da177e4SLinus Torvalds loop_info64_from_old(const struct loop_info *info, struct loop_info64 *info64) 14291da177e4SLinus Torvalds { 14301da177e4SLinus Torvalds memset(info64, 0, sizeof(*info64)); 14311da177e4SLinus Torvalds info64->lo_number = info->lo_number; 14321da177e4SLinus Torvalds info64->lo_device = info->lo_device; 14331da177e4SLinus Torvalds info64->lo_inode = info->lo_inode; 14341da177e4SLinus Torvalds info64->lo_rdevice = info->lo_rdevice; 14351da177e4SLinus Torvalds info64->lo_offset = info->lo_offset; 14361da177e4SLinus Torvalds info64->lo_sizelimit = 0; 14371da177e4SLinus Torvalds info64->lo_encrypt_type = info->lo_encrypt_type; 14381da177e4SLinus Torvalds info64->lo_encrypt_key_size = info->lo_encrypt_key_size; 14391da177e4SLinus Torvalds info64->lo_flags = info->lo_flags; 14401da177e4SLinus Torvalds info64->lo_init[0] = info->lo_init[0]; 14411da177e4SLinus Torvalds info64->lo_init[1] = info->lo_init[1]; 14421da177e4SLinus Torvalds if (info->lo_encrypt_type == LO_CRYPT_CRYPTOAPI) 14431da177e4SLinus Torvalds memcpy(info64->lo_crypt_name, info->lo_name, LO_NAME_SIZE); 14441da177e4SLinus Torvalds else 14451da177e4SLinus Torvalds memcpy(info64->lo_file_name, info->lo_name, LO_NAME_SIZE); 14461da177e4SLinus Torvalds memcpy(info64->lo_encrypt_key, info->lo_encrypt_key, LO_KEY_SIZE); 14471da177e4SLinus Torvalds } 14481da177e4SLinus Torvalds 14491da177e4SLinus Torvalds static int 14501da177e4SLinus Torvalds loop_info64_to_old(const struct loop_info64 *info64, struct loop_info *info) 14511da177e4SLinus Torvalds { 14521da177e4SLinus Torvalds memset(info, 0, sizeof(*info)); 14531da177e4SLinus Torvalds info->lo_number = info64->lo_number; 14541da177e4SLinus Torvalds info->lo_device = info64->lo_device; 14551da177e4SLinus Torvalds info->lo_inode = info64->lo_inode; 14561da177e4SLinus Torvalds info->lo_rdevice = info64->lo_rdevice; 14571da177e4SLinus Torvalds info->lo_offset = info64->lo_offset; 14581da177e4SLinus Torvalds info->lo_encrypt_type = info64->lo_encrypt_type; 14591da177e4SLinus Torvalds info->lo_encrypt_key_size = info64->lo_encrypt_key_size; 14601da177e4SLinus Torvalds info->lo_flags = info64->lo_flags; 14611da177e4SLinus Torvalds info->lo_init[0] = info64->lo_init[0]; 14621da177e4SLinus Torvalds info->lo_init[1] = info64->lo_init[1]; 14631da177e4SLinus Torvalds if (info->lo_encrypt_type == LO_CRYPT_CRYPTOAPI) 14641da177e4SLinus Torvalds memcpy(info->lo_name, info64->lo_crypt_name, LO_NAME_SIZE); 14651da177e4SLinus Torvalds else 14661da177e4SLinus Torvalds memcpy(info->lo_name, info64->lo_file_name, LO_NAME_SIZE); 14671da177e4SLinus Torvalds memcpy(info->lo_encrypt_key, info64->lo_encrypt_key, LO_KEY_SIZE); 14681da177e4SLinus Torvalds 14691da177e4SLinus Torvalds /* error in case values were truncated */ 14701da177e4SLinus Torvalds if (info->lo_device != info64->lo_device || 14711da177e4SLinus Torvalds info->lo_rdevice != info64->lo_rdevice || 14721da177e4SLinus Torvalds info->lo_inode != info64->lo_inode || 14731da177e4SLinus Torvalds info->lo_offset != info64->lo_offset) 14741da177e4SLinus Torvalds return -EOVERFLOW; 14751da177e4SLinus Torvalds 14761da177e4SLinus Torvalds return 0; 14771da177e4SLinus Torvalds } 14781da177e4SLinus Torvalds 14791da177e4SLinus Torvalds static int 14801da177e4SLinus Torvalds loop_set_status_old(struct loop_device *lo, const struct loop_info __user *arg) 14811da177e4SLinus Torvalds { 14821da177e4SLinus Torvalds struct loop_info info; 14831da177e4SLinus Torvalds struct loop_info64 info64; 14841da177e4SLinus Torvalds 14851da177e4SLinus Torvalds if (copy_from_user(&info, arg, sizeof (struct loop_info))) 14861da177e4SLinus Torvalds return -EFAULT; 14871da177e4SLinus Torvalds loop_info64_from_old(&info, &info64); 14881da177e4SLinus Torvalds return loop_set_status(lo, &info64); 14891da177e4SLinus Torvalds } 14901da177e4SLinus Torvalds 14911da177e4SLinus Torvalds static int 14921da177e4SLinus Torvalds loop_set_status64(struct loop_device *lo, const struct loop_info64 __user *arg) 14931da177e4SLinus Torvalds { 14941da177e4SLinus Torvalds struct loop_info64 info64; 14951da177e4SLinus Torvalds 14961da177e4SLinus Torvalds if (copy_from_user(&info64, arg, sizeof (struct loop_info64))) 14971da177e4SLinus Torvalds return -EFAULT; 14981da177e4SLinus Torvalds return loop_set_status(lo, &info64); 14991da177e4SLinus Torvalds } 15001da177e4SLinus Torvalds 15011da177e4SLinus Torvalds static int 15021da177e4SLinus Torvalds loop_get_status_old(struct loop_device *lo, struct loop_info __user *arg) { 15031da177e4SLinus Torvalds struct loop_info info; 15041da177e4SLinus Torvalds struct loop_info64 info64; 1505bdac616dSOmar Sandoval int err; 15061da177e4SLinus Torvalds 15074a5ce9baSJan Kara if (!arg) 1508bdac616dSOmar Sandoval return -EINVAL; 15091da177e4SLinus Torvalds err = loop_get_status(lo, &info64); 15101da177e4SLinus Torvalds if (!err) 15111da177e4SLinus Torvalds err = loop_info64_to_old(&info64, &info); 15121da177e4SLinus Torvalds if (!err && copy_to_user(arg, &info, sizeof(info))) 15131da177e4SLinus Torvalds err = -EFAULT; 15141da177e4SLinus Torvalds 15151da177e4SLinus Torvalds return err; 15161da177e4SLinus Torvalds } 15171da177e4SLinus Torvalds 15181da177e4SLinus Torvalds static int 15191da177e4SLinus Torvalds loop_get_status64(struct loop_device *lo, struct loop_info64 __user *arg) { 15201da177e4SLinus Torvalds struct loop_info64 info64; 1521bdac616dSOmar Sandoval int err; 15221da177e4SLinus Torvalds 15234a5ce9baSJan Kara if (!arg) 1524bdac616dSOmar Sandoval return -EINVAL; 15251da177e4SLinus Torvalds err = loop_get_status(lo, &info64); 15261da177e4SLinus Torvalds if (!err && copy_to_user(arg, &info64, sizeof(info64))) 15271da177e4SLinus Torvalds err = -EFAULT; 15281da177e4SLinus Torvalds 15291da177e4SLinus Torvalds return err; 15301da177e4SLinus Torvalds } 15311da177e4SLinus Torvalds 153251001b7dSHannes Reinecke static int loop_set_capacity(struct loop_device *lo) 153353d66608SJ. R. Okajima { 153453d66608SJ. R. Okajima if (unlikely(lo->lo_state != Lo_bound)) 15357b0576a3SGuo Chao return -ENXIO; 153653d66608SJ. R. Okajima 15371e6ec9eaSOmar Sandoval return figure_loop_size(lo, lo->lo_offset, lo->lo_sizelimit); 153853d66608SJ. R. Okajima } 153953d66608SJ. R. Okajima 1540ab1cb278SMing Lei static int loop_set_dio(struct loop_device *lo, unsigned long arg) 1541ab1cb278SMing Lei { 1542ab1cb278SMing Lei int error = -ENXIO; 1543ab1cb278SMing Lei if (lo->lo_state != Lo_bound) 1544ab1cb278SMing Lei goto out; 1545ab1cb278SMing Lei 1546ab1cb278SMing Lei __loop_update_dio(lo, !!arg); 1547ab1cb278SMing Lei if (lo->use_dio == !!arg) 1548ab1cb278SMing Lei return 0; 1549ab1cb278SMing Lei error = -EINVAL; 1550ab1cb278SMing Lei out: 1551ab1cb278SMing Lei return error; 1552ab1cb278SMing Lei } 1553ab1cb278SMing Lei 155489e4fdecSOmar Sandoval static int loop_set_block_size(struct loop_device *lo, unsigned long arg) 155589e4fdecSOmar Sandoval { 15565db470e2SJaegeuk Kim int err = 0; 15575db470e2SJaegeuk Kim 155889e4fdecSOmar Sandoval if (lo->lo_state != Lo_bound) 155989e4fdecSOmar Sandoval return -ENXIO; 156089e4fdecSOmar Sandoval 156189e4fdecSOmar Sandoval if (arg < 512 || arg > PAGE_SIZE || !is_power_of_2(arg)) 156289e4fdecSOmar Sandoval return -EINVAL; 156389e4fdecSOmar Sandoval 15647e81f99aSMartijn Coenen if (lo->lo_queue->limits.logical_block_size == arg) 15657e81f99aSMartijn Coenen return 0; 15667e81f99aSMartijn Coenen 15675db470e2SJaegeuk Kim sync_blockdev(lo->lo_device); 15685db470e2SJaegeuk Kim kill_bdev(lo->lo_device); 15695db470e2SJaegeuk Kim 157089e4fdecSOmar Sandoval blk_mq_freeze_queue(lo->lo_queue); 157189e4fdecSOmar Sandoval 15725db470e2SJaegeuk Kim /* kill_bdev should have truncated all the pages */ 15737e81f99aSMartijn Coenen if (lo->lo_device->bd_inode->i_mapping->nrpages) { 15745db470e2SJaegeuk Kim err = -EAGAIN; 15755db470e2SJaegeuk Kim pr_warn("%s: loop%d (%s) has still dirty pages (nrpages=%lu)\n", 15765db470e2SJaegeuk Kim __func__, lo->lo_number, lo->lo_file_name, 15775db470e2SJaegeuk Kim lo->lo_device->bd_inode->i_mapping->nrpages); 15785db470e2SJaegeuk Kim goto out_unfreeze; 15795db470e2SJaegeuk Kim } 15805db470e2SJaegeuk Kim 158189e4fdecSOmar Sandoval blk_queue_logical_block_size(lo->lo_queue, arg); 1582bf093753SOmar Sandoval blk_queue_physical_block_size(lo->lo_queue, arg); 1583bf093753SOmar Sandoval blk_queue_io_min(lo->lo_queue, arg); 158489e4fdecSOmar Sandoval loop_update_dio(lo); 15855db470e2SJaegeuk Kim out_unfreeze: 158689e4fdecSOmar Sandoval blk_mq_unfreeze_queue(lo->lo_queue); 158789e4fdecSOmar Sandoval 15885db470e2SJaegeuk Kim return err; 158989e4fdecSOmar Sandoval } 159089e4fdecSOmar Sandoval 1591a1316544SJan Kara static int lo_simple_ioctl(struct loop_device *lo, unsigned int cmd, 1592a1316544SJan Kara unsigned long arg) 15931da177e4SLinus Torvalds { 15941da177e4SLinus Torvalds int err; 15951da177e4SLinus Torvalds 1596c28445faSJan Kara err = mutex_lock_killable(&loop_ctl_mutex); 15973148ffbdSOmar Sandoval if (err) 1598a1316544SJan Kara return err; 15991da177e4SLinus Torvalds switch (cmd) { 160053d66608SJ. R. Okajima case LOOP_SET_CAPACITY: 160151001b7dSHannes Reinecke err = loop_set_capacity(lo); 160253d66608SJ. R. Okajima break; 1603ab1cb278SMing Lei case LOOP_SET_DIRECT_IO: 1604ab1cb278SMing Lei err = loop_set_dio(lo, arg); 1605ab1cb278SMing Lei break; 160689e4fdecSOmar Sandoval case LOOP_SET_BLOCK_SIZE: 160789e4fdecSOmar Sandoval err = loop_set_block_size(lo, arg); 160889e4fdecSOmar Sandoval break; 16091da177e4SLinus Torvalds default: 16101da177e4SLinus Torvalds err = lo->ioctl ? lo->ioctl(lo, cmd, arg) : -EINVAL; 16111da177e4SLinus Torvalds } 1612310ca162STetsuo Handa mutex_unlock(&loop_ctl_mutex); 1613a1316544SJan Kara return err; 1614a1316544SJan Kara } 1615f028f3b2SNikanth Karthikesan 1616a1316544SJan Kara static int lo_ioctl(struct block_device *bdev, fmode_t mode, 1617a1316544SJan Kara unsigned int cmd, unsigned long arg) 1618a1316544SJan Kara { 1619a1316544SJan Kara struct loop_device *lo = bdev->bd_disk->private_data; 1620a1316544SJan Kara int err; 1621a1316544SJan Kara 1622a1316544SJan Kara switch (cmd) { 1623a1316544SJan Kara case LOOP_SET_FD: 1624757ecf40SJan Kara return loop_set_fd(lo, mode, bdev, arg); 1625a1316544SJan Kara case LOOP_CHANGE_FD: 1626c3710770SJan Kara return loop_change_fd(lo, bdev, arg); 1627a1316544SJan Kara case LOOP_CLR_FD: 16287ccd0791SJan Kara return loop_clr_fd(lo); 1629a1316544SJan Kara case LOOP_SET_STATUS: 1630a1316544SJan Kara err = -EPERM; 1631a1316544SJan Kara if ((mode & FMODE_WRITE) || capable(CAP_SYS_ADMIN)) { 1632a1316544SJan Kara err = loop_set_status_old(lo, 1633a1316544SJan Kara (struct loop_info __user *)arg); 1634a1316544SJan Kara } 1635a1316544SJan Kara break; 1636a1316544SJan Kara case LOOP_GET_STATUS: 16374a5ce9baSJan Kara return loop_get_status_old(lo, (struct loop_info __user *) arg); 1638a1316544SJan Kara case LOOP_SET_STATUS64: 1639a1316544SJan Kara err = -EPERM; 1640a1316544SJan Kara if ((mode & FMODE_WRITE) || capable(CAP_SYS_ADMIN)) { 1641a1316544SJan Kara err = loop_set_status64(lo, 1642a1316544SJan Kara (struct loop_info64 __user *) arg); 1643a1316544SJan Kara } 1644a1316544SJan Kara break; 1645a1316544SJan Kara case LOOP_GET_STATUS64: 16464a5ce9baSJan Kara return loop_get_status64(lo, (struct loop_info64 __user *) arg); 1647a1316544SJan Kara case LOOP_SET_CAPACITY: 1648a1316544SJan Kara case LOOP_SET_DIRECT_IO: 1649a1316544SJan Kara case LOOP_SET_BLOCK_SIZE: 1650a1316544SJan Kara if (!(mode & FMODE_WRITE) && !capable(CAP_SYS_ADMIN)) 1651a1316544SJan Kara return -EPERM; 1652a1316544SJan Kara /* Fall through */ 1653a1316544SJan Kara default: 1654a1316544SJan Kara err = lo_simple_ioctl(lo, cmd, arg); 1655a1316544SJan Kara break; 1656a1316544SJan Kara } 1657a1316544SJan Kara 16581da177e4SLinus Torvalds return err; 16591da177e4SLinus Torvalds } 16601da177e4SLinus Torvalds 1661863d5b82SDavid Howells #ifdef CONFIG_COMPAT 1662863d5b82SDavid Howells struct compat_loop_info { 1663863d5b82SDavid Howells compat_int_t lo_number; /* ioctl r/o */ 1664863d5b82SDavid Howells compat_dev_t lo_device; /* ioctl r/o */ 1665863d5b82SDavid Howells compat_ulong_t lo_inode; /* ioctl r/o */ 1666863d5b82SDavid Howells compat_dev_t lo_rdevice; /* ioctl r/o */ 1667863d5b82SDavid Howells compat_int_t lo_offset; 1668863d5b82SDavid Howells compat_int_t lo_encrypt_type; 1669863d5b82SDavid Howells compat_int_t lo_encrypt_key_size; /* ioctl w/o */ 1670863d5b82SDavid Howells compat_int_t lo_flags; /* ioctl r/o */ 1671863d5b82SDavid Howells char lo_name[LO_NAME_SIZE]; 1672863d5b82SDavid Howells unsigned char lo_encrypt_key[LO_KEY_SIZE]; /* ioctl w/o */ 1673863d5b82SDavid Howells compat_ulong_t lo_init[2]; 1674863d5b82SDavid Howells char reserved[4]; 1675863d5b82SDavid Howells }; 1676863d5b82SDavid Howells 1677863d5b82SDavid Howells /* 1678863d5b82SDavid Howells * Transfer 32-bit compatibility structure in userspace to 64-bit loop info 1679863d5b82SDavid Howells * - noinlined to reduce stack space usage in main part of driver 1680863d5b82SDavid Howells */ 1681863d5b82SDavid Howells static noinline int 1682ba674cfcSAl Viro loop_info64_from_compat(const struct compat_loop_info __user *arg, 1683863d5b82SDavid Howells struct loop_info64 *info64) 1684863d5b82SDavid Howells { 1685863d5b82SDavid Howells struct compat_loop_info info; 1686863d5b82SDavid Howells 1687863d5b82SDavid Howells if (copy_from_user(&info, arg, sizeof(info))) 1688863d5b82SDavid Howells return -EFAULT; 1689863d5b82SDavid Howells 1690863d5b82SDavid Howells memset(info64, 0, sizeof(*info64)); 1691863d5b82SDavid Howells info64->lo_number = info.lo_number; 1692863d5b82SDavid Howells info64->lo_device = info.lo_device; 1693863d5b82SDavid Howells info64->lo_inode = info.lo_inode; 1694863d5b82SDavid Howells info64->lo_rdevice = info.lo_rdevice; 1695863d5b82SDavid Howells info64->lo_offset = info.lo_offset; 1696863d5b82SDavid Howells info64->lo_sizelimit = 0; 1697863d5b82SDavid Howells info64->lo_encrypt_type = info.lo_encrypt_type; 1698863d5b82SDavid Howells info64->lo_encrypt_key_size = info.lo_encrypt_key_size; 1699863d5b82SDavid Howells info64->lo_flags = info.lo_flags; 1700863d5b82SDavid Howells info64->lo_init[0] = info.lo_init[0]; 1701863d5b82SDavid Howells info64->lo_init[1] = info.lo_init[1]; 1702863d5b82SDavid Howells if (info.lo_encrypt_type == LO_CRYPT_CRYPTOAPI) 1703863d5b82SDavid Howells memcpy(info64->lo_crypt_name, info.lo_name, LO_NAME_SIZE); 1704863d5b82SDavid Howells else 1705863d5b82SDavid Howells memcpy(info64->lo_file_name, info.lo_name, LO_NAME_SIZE); 1706863d5b82SDavid Howells memcpy(info64->lo_encrypt_key, info.lo_encrypt_key, LO_KEY_SIZE); 1707863d5b82SDavid Howells return 0; 1708863d5b82SDavid Howells } 1709863d5b82SDavid Howells 1710863d5b82SDavid Howells /* 1711863d5b82SDavid Howells * Transfer 64-bit loop info to 32-bit compatibility structure in userspace 1712863d5b82SDavid Howells * - noinlined to reduce stack space usage in main part of driver 1713863d5b82SDavid Howells */ 1714863d5b82SDavid Howells static noinline int 1715863d5b82SDavid Howells loop_info64_to_compat(const struct loop_info64 *info64, 1716863d5b82SDavid Howells struct compat_loop_info __user *arg) 1717863d5b82SDavid Howells { 1718863d5b82SDavid Howells struct compat_loop_info info; 1719863d5b82SDavid Howells 1720863d5b82SDavid Howells memset(&info, 0, sizeof(info)); 1721863d5b82SDavid Howells info.lo_number = info64->lo_number; 1722863d5b82SDavid Howells info.lo_device = info64->lo_device; 1723863d5b82SDavid Howells info.lo_inode = info64->lo_inode; 1724863d5b82SDavid Howells info.lo_rdevice = info64->lo_rdevice; 1725863d5b82SDavid Howells info.lo_offset = info64->lo_offset; 1726863d5b82SDavid Howells info.lo_encrypt_type = info64->lo_encrypt_type; 1727863d5b82SDavid Howells info.lo_encrypt_key_size = info64->lo_encrypt_key_size; 1728863d5b82SDavid Howells info.lo_flags = info64->lo_flags; 1729863d5b82SDavid Howells info.lo_init[0] = info64->lo_init[0]; 1730863d5b82SDavid Howells info.lo_init[1] = info64->lo_init[1]; 1731863d5b82SDavid Howells if (info.lo_encrypt_type == LO_CRYPT_CRYPTOAPI) 1732863d5b82SDavid Howells memcpy(info.lo_name, info64->lo_crypt_name, LO_NAME_SIZE); 1733863d5b82SDavid Howells else 1734863d5b82SDavid Howells memcpy(info.lo_name, info64->lo_file_name, LO_NAME_SIZE); 1735863d5b82SDavid Howells memcpy(info.lo_encrypt_key, info64->lo_encrypt_key, LO_KEY_SIZE); 1736863d5b82SDavid Howells 1737863d5b82SDavid Howells /* error in case values were truncated */ 1738863d5b82SDavid Howells if (info.lo_device != info64->lo_device || 1739863d5b82SDavid Howells info.lo_rdevice != info64->lo_rdevice || 1740863d5b82SDavid Howells info.lo_inode != info64->lo_inode || 1741863d5b82SDavid Howells info.lo_offset != info64->lo_offset || 1742863d5b82SDavid Howells info.lo_init[0] != info64->lo_init[0] || 1743863d5b82SDavid Howells info.lo_init[1] != info64->lo_init[1]) 1744863d5b82SDavid Howells return -EOVERFLOW; 1745863d5b82SDavid Howells 1746863d5b82SDavid Howells if (copy_to_user(arg, &info, sizeof(info))) 1747863d5b82SDavid Howells return -EFAULT; 1748863d5b82SDavid Howells return 0; 1749863d5b82SDavid Howells } 1750863d5b82SDavid Howells 1751863d5b82SDavid Howells static int 1752863d5b82SDavid Howells loop_set_status_compat(struct loop_device *lo, 1753863d5b82SDavid Howells const struct compat_loop_info __user *arg) 1754863d5b82SDavid Howells { 1755863d5b82SDavid Howells struct loop_info64 info64; 1756863d5b82SDavid Howells int ret; 1757863d5b82SDavid Howells 1758863d5b82SDavid Howells ret = loop_info64_from_compat(arg, &info64); 1759863d5b82SDavid Howells if (ret < 0) 1760863d5b82SDavid Howells return ret; 1761863d5b82SDavid Howells return loop_set_status(lo, &info64); 1762863d5b82SDavid Howells } 1763863d5b82SDavid Howells 1764863d5b82SDavid Howells static int 1765863d5b82SDavid Howells loop_get_status_compat(struct loop_device *lo, 1766863d5b82SDavid Howells struct compat_loop_info __user *arg) 1767863d5b82SDavid Howells { 1768863d5b82SDavid Howells struct loop_info64 info64; 1769bdac616dSOmar Sandoval int err; 1770863d5b82SDavid Howells 17714a5ce9baSJan Kara if (!arg) 1772bdac616dSOmar Sandoval return -EINVAL; 1773863d5b82SDavid Howells err = loop_get_status(lo, &info64); 1774863d5b82SDavid Howells if (!err) 1775863d5b82SDavid Howells err = loop_info64_to_compat(&info64, arg); 1776863d5b82SDavid Howells return err; 1777863d5b82SDavid Howells } 1778863d5b82SDavid Howells 1779bb214884SAl Viro static int lo_compat_ioctl(struct block_device *bdev, fmode_t mode, 1780bb214884SAl Viro unsigned int cmd, unsigned long arg) 1781863d5b82SDavid Howells { 1782bb214884SAl Viro struct loop_device *lo = bdev->bd_disk->private_data; 1783863d5b82SDavid Howells int err; 1784863d5b82SDavid Howells 1785863d5b82SDavid Howells switch(cmd) { 1786863d5b82SDavid Howells case LOOP_SET_STATUS: 17873148ffbdSOmar Sandoval err = loop_set_status_compat(lo, 17883148ffbdSOmar Sandoval (const struct compat_loop_info __user *)arg); 1789863d5b82SDavid Howells break; 1790863d5b82SDavid Howells case LOOP_GET_STATUS: 17913148ffbdSOmar Sandoval err = loop_get_status_compat(lo, 17923148ffbdSOmar Sandoval (struct compat_loop_info __user *)arg); 1793863d5b82SDavid Howells break; 179453d66608SJ. R. Okajima case LOOP_SET_CAPACITY: 1795863d5b82SDavid Howells case LOOP_CLR_FD: 1796863d5b82SDavid Howells case LOOP_GET_STATUS64: 1797863d5b82SDavid Howells case LOOP_SET_STATUS64: 1798863d5b82SDavid Howells arg = (unsigned long) compat_ptr(arg); 1799d893ff86SGustavo A. R. Silva /* fall through */ 1800863d5b82SDavid Howells case LOOP_SET_FD: 1801863d5b82SDavid Howells case LOOP_CHANGE_FD: 18029fea4b39SEvan Green case LOOP_SET_BLOCK_SIZE: 1803fdbe4eeeSAlessio Balsini case LOOP_SET_DIRECT_IO: 1804bb214884SAl Viro err = lo_ioctl(bdev, mode, cmd, arg); 1805863d5b82SDavid Howells break; 1806863d5b82SDavid Howells default: 1807863d5b82SDavid Howells err = -ENOIOCTLCMD; 1808863d5b82SDavid Howells break; 1809863d5b82SDavid Howells } 1810863d5b82SDavid Howells return err; 1811863d5b82SDavid Howells } 1812863d5b82SDavid Howells #endif 1813863d5b82SDavid Howells 1814bb214884SAl Viro static int lo_open(struct block_device *bdev, fmode_t mode) 18151da177e4SLinus Torvalds { 1816770fe30aSKay Sievers struct loop_device *lo; 18170a42e99bSJan Kara int err; 1818770fe30aSKay Sievers 18190a42e99bSJan Kara err = mutex_lock_killable(&loop_ctl_mutex); 18200a42e99bSJan Kara if (err) 18210a42e99bSJan Kara return err; 1822770fe30aSKay Sievers lo = bdev->bd_disk->private_data; 1823770fe30aSKay Sievers if (!lo) { 1824770fe30aSKay Sievers err = -ENXIO; 1825770fe30aSKay Sievers goto out; 1826770fe30aSKay Sievers } 18271da177e4SLinus Torvalds 1828f8933667SMing Lei atomic_inc(&lo->lo_refcnt); 1829770fe30aSKay Sievers out: 18300a42e99bSJan Kara mutex_unlock(&loop_ctl_mutex); 1831770fe30aSKay Sievers return err; 18321da177e4SLinus Torvalds } 18331da177e4SLinus Torvalds 1834967d1dc1SJan Kara static void lo_release(struct gendisk *disk, fmode_t mode) 18351da177e4SLinus Torvalds { 1836967d1dc1SJan Kara struct loop_device *lo; 18371da177e4SLinus Torvalds 18380a42e99bSJan Kara mutex_lock(&loop_ctl_mutex); 1839967d1dc1SJan Kara lo = disk->private_data; 1840f8933667SMing Lei if (atomic_dec_return(&lo->lo_refcnt)) 18410a42e99bSJan Kara goto out_unlock; 1842f8933667SMing Lei 184314f27939SMilan Broz if (lo->lo_flags & LO_FLAGS_AUTOCLEAR) { 1844a2505b79SJan Kara if (lo->lo_state != Lo_bound) 1845a2505b79SJan Kara goto out_unlock; 1846a2505b79SJan Kara lo->lo_state = Lo_rundown; 18477ccd0791SJan Kara mutex_unlock(&loop_ctl_mutex); 184814f27939SMilan Broz /* 184914f27939SMilan Broz * In autoclear mode, stop the loop thread 185014f27939SMilan Broz * and remove configuration after last close. 185114f27939SMilan Broz */ 18520da03cabSJan Kara __loop_clr_fd(lo, true); 18530a42e99bSJan Kara return; 185443cade80SOmar Sandoval } else if (lo->lo_state == Lo_bound) { 185514f27939SMilan Broz /* 185614f27939SMilan Broz * Otherwise keep thread (if running) and config, 185714f27939SMilan Broz * but flush possible ongoing bios in thread. 185814f27939SMilan Broz */ 185943cade80SOmar Sandoval blk_mq_freeze_queue(lo->lo_queue); 186043cade80SOmar Sandoval blk_mq_unfreeze_queue(lo->lo_queue); 186114f27939SMilan Broz } 186296c58655SDavid Woodhouse 18630a42e99bSJan Kara out_unlock: 1864310ca162STetsuo Handa mutex_unlock(&loop_ctl_mutex); 1865ae665016SLinus Torvalds } 1866ae665016SLinus Torvalds 186783d5cde4SAlexey Dobriyan static const struct block_device_operations lo_fops = { 18681da177e4SLinus Torvalds .owner = THIS_MODULE, 1869bb214884SAl Viro .open = lo_open, 1870bb214884SAl Viro .release = lo_release, 1871bb214884SAl Viro .ioctl = lo_ioctl, 1872863d5b82SDavid Howells #ifdef CONFIG_COMPAT 1873bb214884SAl Viro .compat_ioctl = lo_compat_ioctl, 1874863d5b82SDavid Howells #endif 18751da177e4SLinus Torvalds }; 18761da177e4SLinus Torvalds 18771da177e4SLinus Torvalds /* 18781da177e4SLinus Torvalds * And now the modules code and kernel interface. 18791da177e4SLinus Torvalds */ 188073285082SKen Chen static int max_loop; 18815657a819SJoe Perches module_param(max_loop, int, 0444); 1882a47653fcSKen Chen MODULE_PARM_DESC(max_loop, "Maximum number of loop devices"); 18835657a819SJoe Perches module_param(max_part, int, 0444); 1884476a4813SLaurent Vivier MODULE_PARM_DESC(max_part, "Maximum number of partitions per loop device"); 18851da177e4SLinus Torvalds MODULE_LICENSE("GPL"); 18861da177e4SLinus Torvalds MODULE_ALIAS_BLOCKDEV_MAJOR(LOOP_MAJOR); 18871da177e4SLinus Torvalds 18881da177e4SLinus Torvalds int loop_register_transfer(struct loop_func_table *funcs) 18891da177e4SLinus Torvalds { 18901da177e4SLinus Torvalds unsigned int n = funcs->number; 18911da177e4SLinus Torvalds 18921da177e4SLinus Torvalds if (n >= MAX_LO_CRYPT || xfer_funcs[n]) 18931da177e4SLinus Torvalds return -EINVAL; 18941da177e4SLinus Torvalds xfer_funcs[n] = funcs; 18951da177e4SLinus Torvalds return 0; 18961da177e4SLinus Torvalds } 18971da177e4SLinus Torvalds 189834dd82afSKay Sievers static int unregister_transfer_cb(int id, void *ptr, void *data) 189934dd82afSKay Sievers { 190034dd82afSKay Sievers struct loop_device *lo = ptr; 190134dd82afSKay Sievers struct loop_func_table *xfer = data; 190234dd82afSKay Sievers 1903310ca162STetsuo Handa mutex_lock(&loop_ctl_mutex); 190434dd82afSKay Sievers if (lo->lo_encryption == xfer) 190534dd82afSKay Sievers loop_release_xfer(lo); 1906310ca162STetsuo Handa mutex_unlock(&loop_ctl_mutex); 190734dd82afSKay Sievers return 0; 190834dd82afSKay Sievers } 190934dd82afSKay Sievers 19101da177e4SLinus Torvalds int loop_unregister_transfer(int number) 19111da177e4SLinus Torvalds { 19121da177e4SLinus Torvalds unsigned int n = number; 19131da177e4SLinus Torvalds struct loop_func_table *xfer; 19141da177e4SLinus Torvalds 19151da177e4SLinus Torvalds if (n == 0 || n >= MAX_LO_CRYPT || (xfer = xfer_funcs[n]) == NULL) 19161da177e4SLinus Torvalds return -EINVAL; 19171da177e4SLinus Torvalds 19181da177e4SLinus Torvalds xfer_funcs[n] = NULL; 191934dd82afSKay Sievers idr_for_each(&loop_index_idr, &unregister_transfer_cb, xfer); 19201da177e4SLinus Torvalds return 0; 19211da177e4SLinus Torvalds } 19221da177e4SLinus Torvalds 19231da177e4SLinus Torvalds EXPORT_SYMBOL(loop_register_transfer); 19241da177e4SLinus Torvalds EXPORT_SYMBOL(loop_unregister_transfer); 19251da177e4SLinus Torvalds 1926fc17b653SChristoph Hellwig static blk_status_t loop_queue_rq(struct blk_mq_hw_ctx *hctx, 1927b5dd2f60SMing Lei const struct blk_mq_queue_data *bd) 1928b5dd2f60SMing Lei { 19291894e916SJens Axboe struct request *rq = bd->rq; 19301894e916SJens Axboe struct loop_cmd *cmd = blk_mq_rq_to_pdu(rq); 19311894e916SJens Axboe struct loop_device *lo = rq->q->queuedata; 1932b5dd2f60SMing Lei 19331894e916SJens Axboe blk_mq_start_request(rq); 1934b5dd2f60SMing Lei 1935f4aa4c7bSMing Lei if (lo->lo_state != Lo_bound) 1936fc17b653SChristoph Hellwig return BLK_STS_IOERR; 1937f4aa4c7bSMing Lei 19381894e916SJens Axboe switch (req_op(rq)) { 1939f0225cacSChristoph Hellwig case REQ_OP_FLUSH: 1940f0225cacSChristoph Hellwig case REQ_OP_DISCARD: 194119372e27SChristoph Hellwig case REQ_OP_WRITE_ZEROES: 1942bc07c10aSMing Lei cmd->use_aio = false; 1943f0225cacSChristoph Hellwig break; 1944f0225cacSChristoph Hellwig default: 1945f0225cacSChristoph Hellwig cmd->use_aio = lo->use_dio; 1946f0225cacSChristoph Hellwig break; 1947f0225cacSChristoph Hellwig } 1948bc07c10aSMing Lei 1949d4478e92SShaohua Li /* always use the first bio's css */ 19500b508bc9SShaohua Li #ifdef CONFIG_BLK_CGROUP 1951db6638d7SDennis Zhou if (cmd->use_aio && rq->bio && rq->bio->bi_blkg) { 1952db6638d7SDennis Zhou cmd->css = &bio_blkcg(rq->bio)->css; 1953d4478e92SShaohua Li css_get(cmd->css); 1954d4478e92SShaohua Li } else 1955d4478e92SShaohua Li #endif 1956d4478e92SShaohua Li cmd->css = NULL; 19573989144fSPetr Mladek kthread_queue_work(&lo->worker, &cmd->work); 1958b5dd2f60SMing Lei 1959fc17b653SChristoph Hellwig return BLK_STS_OK; 1960b5dd2f60SMing Lei } 1961b5dd2f60SMing Lei 1962b5dd2f60SMing Lei static void loop_handle_cmd(struct loop_cmd *cmd) 1963b5dd2f60SMing Lei { 19641894e916SJens Axboe struct request *rq = blk_mq_rq_from_pdu(cmd); 19651894e916SJens Axboe const bool write = op_is_write(req_op(rq)); 19661894e916SJens Axboe struct loop_device *lo = rq->q->queuedata; 1967f4829a9bSChristoph Hellwig int ret = 0; 1968b5dd2f60SMing Lei 1969f4829a9bSChristoph Hellwig if (write && (lo->lo_flags & LO_FLAGS_READ_ONLY)) { 1970f4829a9bSChristoph Hellwig ret = -EIO; 1971b5dd2f60SMing Lei goto failed; 1972f4829a9bSChristoph Hellwig } 1973b5dd2f60SMing Lei 19741894e916SJens Axboe ret = do_req_filebacked(lo, rq); 1975b5dd2f60SMing Lei failed: 1976bc07c10aSMing Lei /* complete non-aio request */ 1977fe2cb290SChristoph Hellwig if (!cmd->use_aio || ret) { 19788cd55087SEvan Green if (ret == -EOPNOTSUPP) 19798cd55087SEvan Green cmd->ret = ret; 19808cd55087SEvan Green else 1981fe2cb290SChristoph Hellwig cmd->ret = ret ? -EIO : 0; 19821894e916SJens Axboe blk_mq_complete_request(rq); 1983fe2cb290SChristoph Hellwig } 1984b5dd2f60SMing Lei } 1985b5dd2f60SMing Lei 1986e03a3d7aSMing Lei static void loop_queue_work(struct kthread_work *work) 1987b5dd2f60SMing Lei { 1988b5dd2f60SMing Lei struct loop_cmd *cmd = 1989e03a3d7aSMing Lei container_of(work, struct loop_cmd, work); 1990b5dd2f60SMing Lei 1991b5dd2f60SMing Lei loop_handle_cmd(cmd); 1992b5dd2f60SMing Lei } 1993b5dd2f60SMing Lei 1994d6296d39SChristoph Hellwig static int loop_init_request(struct blk_mq_tag_set *set, struct request *rq, 1995d6296d39SChristoph Hellwig unsigned int hctx_idx, unsigned int numa_node) 1996b5dd2f60SMing Lei { 1997b5dd2f60SMing Lei struct loop_cmd *cmd = blk_mq_rq_to_pdu(rq); 1998b5dd2f60SMing Lei 19993989144fSPetr Mladek kthread_init_work(&cmd->work, loop_queue_work); 2000b5dd2f60SMing Lei return 0; 2001b5dd2f60SMing Lei } 2002b5dd2f60SMing Lei 2003f363b089SEric Biggers static const struct blk_mq_ops loop_mq_ops = { 2004b5dd2f60SMing Lei .queue_rq = loop_queue_rq, 2005b5dd2f60SMing Lei .init_request = loop_init_request, 2006fe2cb290SChristoph Hellwig .complete = lo_complete_rq, 2007b5dd2f60SMing Lei }; 2008b5dd2f60SMing Lei 200934dd82afSKay Sievers static int loop_add(struct loop_device **l, int i) 20101da177e4SLinus Torvalds { 201173285082SKen Chen struct loop_device *lo; 201273285082SKen Chen struct gendisk *disk; 201334dd82afSKay Sievers int err; 20141da177e4SLinus Torvalds 201534dd82afSKay Sievers err = -ENOMEM; 201668d740d7SSilva Paulo lo = kzalloc(sizeof(*lo), GFP_KERNEL); 201768d740d7SSilva Paulo if (!lo) 201873285082SKen Chen goto out; 201934dd82afSKay Sievers 2020ef7e7c82SMikulas Patocka lo->lo_state = Lo_unbound; 2021ef7e7c82SMikulas Patocka 2022c718aa65STejun Heo /* allocate id, if @id >= 0, we're requesting that specific id */ 202334dd82afSKay Sievers if (i >= 0) { 2024c718aa65STejun Heo err = idr_alloc(&loop_index_idr, lo, i, i + 1, GFP_KERNEL); 2025c718aa65STejun Heo if (err == -ENOSPC) 202634dd82afSKay Sievers err = -EEXIST; 202734dd82afSKay Sievers } else { 2028c718aa65STejun Heo err = idr_alloc(&loop_index_idr, lo, 0, 0, GFP_KERNEL); 202934dd82afSKay Sievers } 203034dd82afSKay Sievers if (err < 0) 203134dd82afSKay Sievers goto out_free_dev; 2032c718aa65STejun Heo i = err; 20331da177e4SLinus Torvalds 2034183cfb57SWei Yongjun err = -ENOMEM; 2035b5dd2f60SMing Lei lo->tag_set.ops = &loop_mq_ops; 2036b5dd2f60SMing Lei lo->tag_set.nr_hw_queues = 1; 2037b5dd2f60SMing Lei lo->tag_set.queue_depth = 128; 2038b5dd2f60SMing Lei lo->tag_set.numa_node = NUMA_NO_NODE; 2039b5dd2f60SMing Lei lo->tag_set.cmd_size = sizeof(struct loop_cmd); 204056d18f62SMing Lei lo->tag_set.flags = BLK_MQ_F_SHOULD_MERGE; 2041b5dd2f60SMing Lei lo->tag_set.driver_data = lo; 2042b5dd2f60SMing Lei 2043b5dd2f60SMing Lei err = blk_mq_alloc_tag_set(&lo->tag_set); 2044b5dd2f60SMing Lei if (err) 20453ec981e3SMikulas Patocka goto out_free_idr; 204673285082SKen Chen 2047b5dd2f60SMing Lei lo->lo_queue = blk_mq_init_queue(&lo->tag_set); 204838a3499fSChengguang Xu if (IS_ERR(lo->lo_queue)) { 2049b5dd2f60SMing Lei err = PTR_ERR(lo->lo_queue); 2050b5dd2f60SMing Lei goto out_cleanup_tags; 2051b5dd2f60SMing Lei } 2052ef7e7c82SMikulas Patocka lo->lo_queue->queuedata = lo; 2053ef7e7c82SMikulas Patocka 205454bb0adeSShaohua Li blk_queue_max_hw_sectors(lo->lo_queue, BLK_DEF_MAX_SECTORS); 205540326d8aSShaohua Li 20565b5e20f4SMing Lei /* 205740326d8aSShaohua Li * By default, we do buffer IO, so it doesn't make sense to enable 205840326d8aSShaohua Li * merge because the I/O submitted to backing file is handled page by 205940326d8aSShaohua Li * page. For directio mode, merge does help to dispatch bigger request 206040326d8aSShaohua Li * to underlayer disk. We will enable merge once directio is enabled. 20615b5e20f4SMing Lei */ 20628b904b5bSBart Van Assche blk_queue_flag_set(QUEUE_FLAG_NOMERGES, lo->lo_queue); 20635b5e20f4SMing Lei 20647a649737SMinfei Huang err = -ENOMEM; 2065476a4813SLaurent Vivier disk = lo->lo_disk = alloc_disk(1 << part_shift); 206673285082SKen Chen if (!disk) 206773285082SKen Chen goto out_free_queue; 206873285082SKen Chen 2069e03c8dd1SKay Sievers /* 2070e03c8dd1SKay Sievers * Disable partition scanning by default. The in-kernel partition 2071e03c8dd1SKay Sievers * scanning can be requested individually per-device during its 2072e03c8dd1SKay Sievers * setup. Userspace can always add and remove partitions from all 2073e03c8dd1SKay Sievers * devices. The needed partition minors are allocated from the 2074e03c8dd1SKay Sievers * extended minor space, the main loop device numbers will continue 2075e03c8dd1SKay Sievers * to match the loop minors, regardless of the number of partitions 2076e03c8dd1SKay Sievers * used. 2077e03c8dd1SKay Sievers * 2078e03c8dd1SKay Sievers * If max_part is given, partition scanning is globally enabled for 2079e03c8dd1SKay Sievers * all loop devices. The minors for the main loop devices will be 2080e03c8dd1SKay Sievers * multiples of max_part. 2081e03c8dd1SKay Sievers * 2082e03c8dd1SKay Sievers * Note: Global-for-all-devices, set-only-at-init, read-only module 2083e03c8dd1SKay Sievers * parameteters like 'max_loop' and 'max_part' make things needlessly 2084e03c8dd1SKay Sievers * complicated, are too static, inflexible and may surprise 2085e03c8dd1SKay Sievers * userspace tools. Parameters like this in general should be avoided. 2086e03c8dd1SKay Sievers */ 2087e03c8dd1SKay Sievers if (!part_shift) 2088e03c8dd1SKay Sievers disk->flags |= GENHD_FL_NO_PART_SCAN; 2089e03c8dd1SKay Sievers disk->flags |= GENHD_FL_EXT_DEVT; 2090f8933667SMing Lei atomic_set(&lo->lo_refcnt, 0); 20911da177e4SLinus Torvalds lo->lo_number = i; 20921da177e4SLinus Torvalds spin_lock_init(&lo->lo_lock); 20931da177e4SLinus Torvalds disk->major = LOOP_MAJOR; 2094476a4813SLaurent Vivier disk->first_minor = i << part_shift; 20951da177e4SLinus Torvalds disk->fops = &lo_fops; 20961da177e4SLinus Torvalds disk->private_data = lo; 20971da177e4SLinus Torvalds disk->queue = lo->lo_queue; 209873285082SKen Chen sprintf(disk->disk_name, "loop%d", i); 209934dd82afSKay Sievers add_disk(disk); 210034dd82afSKay Sievers *l = lo; 210134dd82afSKay Sievers return lo->lo_number; 210273285082SKen Chen 210373285082SKen Chen out_free_queue: 210473285082SKen Chen blk_cleanup_queue(lo->lo_queue); 2105b5dd2f60SMing Lei out_cleanup_tags: 2106b5dd2f60SMing Lei blk_mq_free_tag_set(&lo->tag_set); 21073ec981e3SMikulas Patocka out_free_idr: 21083ec981e3SMikulas Patocka idr_remove(&loop_index_idr, i); 210973285082SKen Chen out_free_dev: 211073285082SKen Chen kfree(lo); 211173285082SKen Chen out: 211234dd82afSKay Sievers return err; 21131da177e4SLinus Torvalds } 21141da177e4SLinus Torvalds 211534dd82afSKay Sievers static void loop_remove(struct loop_device *lo) 211673285082SKen Chen { 21176cd18e71SNeilBrown del_gendisk(lo->lo_disk); 21180fa8ebddSBart Van Assche blk_cleanup_queue(lo->lo_queue); 2119b5dd2f60SMing Lei blk_mq_free_tag_set(&lo->tag_set); 212073285082SKen Chen put_disk(lo->lo_disk); 212173285082SKen Chen kfree(lo); 212273285082SKen Chen } 212373285082SKen Chen 2124770fe30aSKay Sievers static int find_free_cb(int id, void *ptr, void *data) 2125770fe30aSKay Sievers { 2126770fe30aSKay Sievers struct loop_device *lo = ptr; 2127770fe30aSKay Sievers struct loop_device **l = data; 2128770fe30aSKay Sievers 2129770fe30aSKay Sievers if (lo->lo_state == Lo_unbound) { 2130770fe30aSKay Sievers *l = lo; 2131770fe30aSKay Sievers return 1; 2132770fe30aSKay Sievers } 2133770fe30aSKay Sievers return 0; 2134770fe30aSKay Sievers } 2135770fe30aSKay Sievers 213634dd82afSKay Sievers static int loop_lookup(struct loop_device **l, int i) 2137a47653fcSKen Chen { 2138a47653fcSKen Chen struct loop_device *lo; 213934dd82afSKay Sievers int ret = -ENODEV; 2140a47653fcSKen Chen 2141770fe30aSKay Sievers if (i < 0) { 2142770fe30aSKay Sievers int err; 2143770fe30aSKay Sievers 2144770fe30aSKay Sievers err = idr_for_each(&loop_index_idr, &find_free_cb, &lo); 2145770fe30aSKay Sievers if (err == 1) { 2146770fe30aSKay Sievers *l = lo; 2147770fe30aSKay Sievers ret = lo->lo_number; 2148770fe30aSKay Sievers } 2149770fe30aSKay Sievers goto out; 2150a47653fcSKen Chen } 2151a47653fcSKen Chen 2152770fe30aSKay Sievers /* lookup and return a specific i */ 215334dd82afSKay Sievers lo = idr_find(&loop_index_idr, i); 2154a47653fcSKen Chen if (lo) { 215534dd82afSKay Sievers *l = lo; 215634dd82afSKay Sievers ret = lo->lo_number; 2157a47653fcSKen Chen } 2158770fe30aSKay Sievers out: 215934dd82afSKay Sievers return ret; 2160a47653fcSKen Chen } 2161a47653fcSKen Chen 216273285082SKen Chen static struct kobject *loop_probe(dev_t dev, int *part, void *data) 216373285082SKen Chen { 2164705962ccSAl Viro struct loop_device *lo; 216507002e99SAl Viro struct kobject *kobj; 216634dd82afSKay Sievers int err; 216773285082SKen Chen 21680a42e99bSJan Kara mutex_lock(&loop_ctl_mutex); 216934dd82afSKay Sievers err = loop_lookup(&lo, MINOR(dev) >> part_shift); 217034dd82afSKay Sievers if (err < 0) 217134dd82afSKay Sievers err = loop_add(&lo, MINOR(dev) >> part_shift); 217234dd82afSKay Sievers if (err < 0) 2173a207f593SMikulas Patocka kobj = NULL; 217434dd82afSKay Sievers else 21753079c22eSJan Kara kobj = get_disk_and_module(lo->lo_disk); 21760a42e99bSJan Kara mutex_unlock(&loop_ctl_mutex); 217773285082SKen Chen 217873285082SKen Chen *part = 0; 217907002e99SAl Viro return kobj; 218073285082SKen Chen } 218173285082SKen Chen 2182770fe30aSKay Sievers static long loop_control_ioctl(struct file *file, unsigned int cmd, 2183770fe30aSKay Sievers unsigned long parm) 2184770fe30aSKay Sievers { 2185770fe30aSKay Sievers struct loop_device *lo; 21860a42e99bSJan Kara int ret; 2187770fe30aSKay Sievers 21880a42e99bSJan Kara ret = mutex_lock_killable(&loop_ctl_mutex); 21890a42e99bSJan Kara if (ret) 21900a42e99bSJan Kara return ret; 21910a42e99bSJan Kara 21920a42e99bSJan Kara ret = -ENOSYS; 2193770fe30aSKay Sievers switch (cmd) { 2194770fe30aSKay Sievers case LOOP_CTL_ADD: 2195770fe30aSKay Sievers ret = loop_lookup(&lo, parm); 2196770fe30aSKay Sievers if (ret >= 0) { 2197770fe30aSKay Sievers ret = -EEXIST; 2198770fe30aSKay Sievers break; 2199770fe30aSKay Sievers } 2200770fe30aSKay Sievers ret = loop_add(&lo, parm); 2201770fe30aSKay Sievers break; 2202770fe30aSKay Sievers case LOOP_CTL_REMOVE: 2203770fe30aSKay Sievers ret = loop_lookup(&lo, parm); 2204770fe30aSKay Sievers if (ret < 0) 2205770fe30aSKay Sievers break; 2206770fe30aSKay Sievers if (lo->lo_state != Lo_unbound) { 2207770fe30aSKay Sievers ret = -EBUSY; 2208770fe30aSKay Sievers break; 2209770fe30aSKay Sievers } 2210f8933667SMing Lei if (atomic_read(&lo->lo_refcnt) > 0) { 2211770fe30aSKay Sievers ret = -EBUSY; 2212770fe30aSKay Sievers break; 2213770fe30aSKay Sievers } 2214770fe30aSKay Sievers lo->lo_disk->private_data = NULL; 2215770fe30aSKay Sievers idr_remove(&loop_index_idr, lo->lo_number); 2216770fe30aSKay Sievers loop_remove(lo); 2217770fe30aSKay Sievers break; 2218770fe30aSKay Sievers case LOOP_CTL_GET_FREE: 2219770fe30aSKay Sievers ret = loop_lookup(&lo, -1); 2220770fe30aSKay Sievers if (ret >= 0) 2221770fe30aSKay Sievers break; 2222770fe30aSKay Sievers ret = loop_add(&lo, -1); 2223770fe30aSKay Sievers } 22240a42e99bSJan Kara mutex_unlock(&loop_ctl_mutex); 2225770fe30aSKay Sievers 2226770fe30aSKay Sievers return ret; 2227770fe30aSKay Sievers } 2228770fe30aSKay Sievers 2229770fe30aSKay Sievers static const struct file_operations loop_ctl_fops = { 2230770fe30aSKay Sievers .open = nonseekable_open, 2231770fe30aSKay Sievers .unlocked_ioctl = loop_control_ioctl, 2232770fe30aSKay Sievers .compat_ioctl = loop_control_ioctl, 2233770fe30aSKay Sievers .owner = THIS_MODULE, 2234770fe30aSKay Sievers .llseek = noop_llseek, 2235770fe30aSKay Sievers }; 2236770fe30aSKay Sievers 2237770fe30aSKay Sievers static struct miscdevice loop_misc = { 2238770fe30aSKay Sievers .minor = LOOP_CTRL_MINOR, 2239770fe30aSKay Sievers .name = "loop-control", 2240770fe30aSKay Sievers .fops = &loop_ctl_fops, 2241770fe30aSKay Sievers }; 2242770fe30aSKay Sievers 2243770fe30aSKay Sievers MODULE_ALIAS_MISCDEV(LOOP_CTRL_MINOR); 2244770fe30aSKay Sievers MODULE_ALIAS("devname:loop-control"); 2245770fe30aSKay Sievers 224673285082SKen Chen static int __init loop_init(void) 224773285082SKen Chen { 2248a47653fcSKen Chen int i, nr; 2249a47653fcSKen Chen unsigned long range; 225034dd82afSKay Sievers struct loop_device *lo; 2251770fe30aSKay Sievers int err; 2252a47653fcSKen Chen 2253476a4813SLaurent Vivier part_shift = 0; 2254ac04fee0SNamhyung Kim if (max_part > 0) { 2255476a4813SLaurent Vivier part_shift = fls(max_part); 2256476a4813SLaurent Vivier 2257ac04fee0SNamhyung Kim /* 2258ac04fee0SNamhyung Kim * Adjust max_part according to part_shift as it is exported 2259ac04fee0SNamhyung Kim * to user space so that user can decide correct minor number 2260ac04fee0SNamhyung Kim * if [s]he want to create more devices. 2261ac04fee0SNamhyung Kim * 2262ac04fee0SNamhyung Kim * Note that -1 is required because partition 0 is reserved 2263ac04fee0SNamhyung Kim * for the whole disk. 2264ac04fee0SNamhyung Kim */ 2265ac04fee0SNamhyung Kim max_part = (1UL << part_shift) - 1; 2266ac04fee0SNamhyung Kim } 2267ac04fee0SNamhyung Kim 2268b1a66504SGuo Chao if ((1UL << part_shift) > DISK_MAX_PARTS) { 2269b1a66504SGuo Chao err = -EINVAL; 2270a8c1d064SAnton Volkov goto err_out; 2271b1a66504SGuo Chao } 227278f4bb36SNamhyung Kim 2273b1a66504SGuo Chao if (max_loop > 1UL << (MINORBITS - part_shift)) { 2274b1a66504SGuo Chao err = -EINVAL; 2275a8c1d064SAnton Volkov goto err_out; 2276b1a66504SGuo Chao } 227773285082SKen Chen 2278d134b00bSKay Sievers /* 2279d134b00bSKay Sievers * If max_loop is specified, create that many devices upfront. 2280d134b00bSKay Sievers * This also becomes a hard limit. If max_loop is not specified, 2281d134b00bSKay Sievers * create CONFIG_BLK_DEV_LOOP_MIN_COUNT loop devices at module 2282d134b00bSKay Sievers * init time. Loop devices can be requested on-demand with the 2283d134b00bSKay Sievers * /dev/loop-control interface, or be instantiated by accessing 2284d134b00bSKay Sievers * a 'dead' device node. 2285d134b00bSKay Sievers */ 228673285082SKen Chen if (max_loop) { 2287a47653fcSKen Chen nr = max_loop; 2288a1c15c59SNamhyung Kim range = max_loop << part_shift; 2289a47653fcSKen Chen } else { 2290d134b00bSKay Sievers nr = CONFIG_BLK_DEV_LOOP_MIN_COUNT; 2291a1c15c59SNamhyung Kim range = 1UL << MINORBITS; 229273285082SKen Chen } 2293a47653fcSKen Chen 2294a8c1d064SAnton Volkov err = misc_register(&loop_misc); 2295a8c1d064SAnton Volkov if (err < 0) 2296a8c1d064SAnton Volkov goto err_out; 2297a8c1d064SAnton Volkov 2298a8c1d064SAnton Volkov 2299b1a66504SGuo Chao if (register_blkdev(LOOP_MAJOR, "loop")) { 2300b1a66504SGuo Chao err = -EIO; 2301b1a66504SGuo Chao goto misc_out; 2302b1a66504SGuo Chao } 2303a47653fcSKen Chen 2304a47653fcSKen Chen blk_register_region(MKDEV(LOOP_MAJOR, 0), range, 2305a47653fcSKen Chen THIS_MODULE, loop_probe, NULL, NULL); 2306a47653fcSKen Chen 2307d134b00bSKay Sievers /* pre-create number of devices given by config or max_loop */ 23080a42e99bSJan Kara mutex_lock(&loop_ctl_mutex); 230934dd82afSKay Sievers for (i = 0; i < nr; i++) 231034dd82afSKay Sievers loop_add(&lo, i); 23110a42e99bSJan Kara mutex_unlock(&loop_ctl_mutex); 231234dd82afSKay Sievers 231373285082SKen Chen printk(KERN_INFO "loop: module loaded\n"); 23141da177e4SLinus Torvalds return 0; 2315b1a66504SGuo Chao 2316b1a66504SGuo Chao misc_out: 2317b1a66504SGuo Chao misc_deregister(&loop_misc); 2318a8c1d064SAnton Volkov err_out: 2319b1a66504SGuo Chao return err; 232034dd82afSKay Sievers } 2321a47653fcSKen Chen 232234dd82afSKay Sievers static int loop_exit_cb(int id, void *ptr, void *data) 232334dd82afSKay Sievers { 232434dd82afSKay Sievers struct loop_device *lo = ptr; 2325a47653fcSKen Chen 232634dd82afSKay Sievers loop_remove(lo); 232734dd82afSKay Sievers return 0; 23281da177e4SLinus Torvalds } 23291da177e4SLinus Torvalds 233073285082SKen Chen static void __exit loop_exit(void) 23311da177e4SLinus Torvalds { 2332a47653fcSKen Chen unsigned long range; 23331da177e4SLinus Torvalds 2334a1c15c59SNamhyung Kim range = max_loop ? max_loop << part_shift : 1UL << MINORBITS; 2335a47653fcSKen Chen 233634dd82afSKay Sievers idr_for_each(&loop_index_idr, &loop_exit_cb, NULL); 233734dd82afSKay Sievers idr_destroy(&loop_index_idr); 233873285082SKen Chen 2339a47653fcSKen Chen blk_unregister_region(MKDEV(LOOP_MAJOR, 0), range); 234000d59405SAkinobu Mita unregister_blkdev(LOOP_MAJOR, "loop"); 2341770fe30aSKay Sievers 2342770fe30aSKay Sievers misc_deregister(&loop_misc); 23431da177e4SLinus Torvalds } 23441da177e4SLinus Torvalds 23451da177e4SLinus Torvalds module_init(loop_init); 23461da177e4SLinus Torvalds module_exit(loop_exit); 23471da177e4SLinus Torvalds 23481da177e4SLinus Torvalds #ifndef MODULE 23491da177e4SLinus Torvalds static int __init max_loop_setup(char *str) 23501da177e4SLinus Torvalds { 23511da177e4SLinus Torvalds max_loop = simple_strtol(str, NULL, 0); 23521da177e4SLinus Torvalds return 1; 23531da177e4SLinus Torvalds } 23541da177e4SLinus Torvalds 23551da177e4SLinus Torvalds __setup("max_loop=", max_loop_setup); 23561da177e4SLinus Torvalds #endif 2357