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 */ 2172e5ab5f3SMing Lei blk_mq_freeze_queue(lo->lo_queue); 2182e5ab5f3SMing Lei lo->use_dio = use_dio; 21940326d8aSShaohua Li if (use_dio) { 2208b904b5bSBart Van Assche blk_queue_flag_clear(QUEUE_FLAG_NOMERGES, lo->lo_queue); 2212e5ab5f3SMing Lei lo->lo_flags |= LO_FLAGS_DIRECT_IO; 22240326d8aSShaohua Li } else { 2238b904b5bSBart Van Assche blk_queue_flag_set(QUEUE_FLAG_NOMERGES, lo->lo_queue); 2242e5ab5f3SMing Lei lo->lo_flags &= ~LO_FLAGS_DIRECT_IO; 22540326d8aSShaohua Li } 2262e5ab5f3SMing Lei blk_mq_unfreeze_queue(lo->lo_queue); 2272e5ab5f3SMing Lei } 2282e5ab5f3SMing Lei 2297035b5dfSDmitry Monakhov static int 2301e6ec9eaSOmar Sandoval figure_loop_size(struct loop_device *lo, loff_t offset, loff_t sizelimit) 2317035b5dfSDmitry Monakhov { 2327035b5dfSDmitry Monakhov loff_t size = get_size(offset, sizelimit, lo->lo_backing_file); 2331da177e4SLinus Torvalds sector_t x = (sector_t)size; 2347b0576a3SGuo Chao struct block_device *bdev = lo->lo_device; 2351da177e4SLinus Torvalds 2361da177e4SLinus Torvalds if (unlikely((loff_t)x != size)) 2371da177e4SLinus Torvalds return -EFBIG; 2387035b5dfSDmitry Monakhov if (lo->lo_offset != offset) 2397035b5dfSDmitry Monakhov lo->lo_offset = offset; 2407035b5dfSDmitry Monakhov if (lo->lo_sizelimit != sizelimit) 2417035b5dfSDmitry Monakhov lo->lo_sizelimit = sizelimit; 24273285082SKen Chen set_capacity(lo->lo_disk, x); 2437b0576a3SGuo Chao bd_set_size(bdev, (loff_t)get_capacity(bdev->bd_disk) << 9); 2447b0576a3SGuo Chao /* let user-space know about the new size */ 2457b0576a3SGuo Chao kobject_uevent(&disk_to_dev(bdev->bd_disk)->kobj, KOBJ_CHANGE); 2461da177e4SLinus Torvalds return 0; 2471da177e4SLinus Torvalds } 2481da177e4SLinus Torvalds 2491da177e4SLinus Torvalds static inline int 2501da177e4SLinus Torvalds lo_do_transfer(struct loop_device *lo, int cmd, 2511da177e4SLinus Torvalds struct page *rpage, unsigned roffs, 2521da177e4SLinus Torvalds struct page *lpage, unsigned loffs, 2531da177e4SLinus Torvalds int size, sector_t rblock) 2541da177e4SLinus Torvalds { 255aa4d8616SChristoph Hellwig int ret; 256aa4d8616SChristoph Hellwig 257aa4d8616SChristoph Hellwig ret = lo->transfer(lo, cmd, rpage, roffs, lpage, loffs, size, rblock); 258aa4d8616SChristoph Hellwig if (likely(!ret)) 2591da177e4SLinus Torvalds return 0; 2601da177e4SLinus Torvalds 261aa4d8616SChristoph Hellwig printk_ratelimited(KERN_ERR 262aa4d8616SChristoph Hellwig "loop: Transfer error at byte offset %llu, length %i.\n", 263aa4d8616SChristoph Hellwig (unsigned long long)rblock << 9, size); 264aa4d8616SChristoph Hellwig return ret; 2651da177e4SLinus Torvalds } 2661da177e4SLinus Torvalds 267aa4d8616SChristoph Hellwig static int lo_write_bvec(struct file *file, struct bio_vec *bvec, loff_t *ppos) 2681da177e4SLinus Torvalds { 269aa4d8616SChristoph Hellwig struct iov_iter i; 2701da177e4SLinus Torvalds ssize_t bw; 271283e7e5dSAl Viro 272aa563d7bSDavid Howells iov_iter_bvec(&i, WRITE, bvec, 1, bvec->bv_len); 2731da177e4SLinus Torvalds 27403d95eb2SAl Viro file_start_write(file); 275abbb6589SChristoph Hellwig bw = vfs_iter_write(file, &i, ppos, 0); 27603d95eb2SAl Viro file_end_write(file); 277aa4d8616SChristoph Hellwig 278aa4d8616SChristoph Hellwig if (likely(bw == bvec->bv_len)) 2791da177e4SLinus Torvalds return 0; 280aa4d8616SChristoph Hellwig 281aa4d8616SChristoph Hellwig printk_ratelimited(KERN_ERR 282aa4d8616SChristoph Hellwig "loop: Write error at byte offset %llu, length %i.\n", 283aa4d8616SChristoph Hellwig (unsigned long long)*ppos, bvec->bv_len); 2841da177e4SLinus Torvalds if (bw >= 0) 2851da177e4SLinus Torvalds bw = -EIO; 2861da177e4SLinus Torvalds return bw; 2871da177e4SLinus Torvalds } 2881da177e4SLinus Torvalds 289aa4d8616SChristoph Hellwig static int lo_write_simple(struct loop_device *lo, struct request *rq, 290aa4d8616SChristoph Hellwig loff_t pos) 2911da177e4SLinus Torvalds { 292aa4d8616SChristoph Hellwig struct bio_vec bvec; 293aa4d8616SChristoph Hellwig struct req_iterator iter; 294aa4d8616SChristoph Hellwig int ret = 0; 295aa4d8616SChristoph Hellwig 296aa4d8616SChristoph Hellwig rq_for_each_segment(bvec, rq, iter) { 297aa4d8616SChristoph Hellwig ret = lo_write_bvec(lo->lo_backing_file, &bvec, &pos); 298aa4d8616SChristoph Hellwig if (ret < 0) 299aa4d8616SChristoph Hellwig break; 3001da177e4SLinus Torvalds cond_resched(); 3011da177e4SLinus Torvalds } 3021da177e4SLinus Torvalds 303aa4d8616SChristoph Hellwig return ret; 304aa4d8616SChristoph Hellwig } 305aa4d8616SChristoph Hellwig 306aa4d8616SChristoph Hellwig /* 307456be148SChristoph Hellwig * This is the slow, transforming version that needs to double buffer the 308456be148SChristoph Hellwig * data as it cannot do the transformations in place without having direct 309456be148SChristoph Hellwig * access to the destination pages of the backing file. 3101da177e4SLinus Torvalds */ 311aa4d8616SChristoph Hellwig static int lo_write_transfer(struct loop_device *lo, struct request *rq, 312aa4d8616SChristoph Hellwig loff_t pos) 3131da177e4SLinus Torvalds { 314aa4d8616SChristoph Hellwig struct bio_vec bvec, b; 31530112013SMing Lei struct req_iterator iter; 316aa4d8616SChristoph Hellwig struct page *page; 3177988613bSKent Overstreet int ret = 0; 3181da177e4SLinus Torvalds 319aa4d8616SChristoph Hellwig page = alloc_page(GFP_NOIO); 3201da177e4SLinus Torvalds if (unlikely(!page)) 321aa4d8616SChristoph Hellwig return -ENOMEM; 322456be148SChristoph Hellwig 32330112013SMing Lei rq_for_each_segment(bvec, rq, iter) { 324aa4d8616SChristoph Hellwig ret = lo_do_transfer(lo, WRITE, page, 0, bvec.bv_page, 325aa4d8616SChristoph Hellwig bvec.bv_offset, bvec.bv_len, pos >> 9); 326aa4d8616SChristoph Hellwig if (unlikely(ret)) 327aa4d8616SChristoph Hellwig break; 328aa4d8616SChristoph Hellwig 329aa4d8616SChristoph Hellwig b.bv_page = page; 330aa4d8616SChristoph Hellwig b.bv_offset = 0; 331aa4d8616SChristoph Hellwig b.bv_len = bvec.bv_len; 332aa4d8616SChristoph Hellwig ret = lo_write_bvec(lo->lo_backing_file, &b, &pos); 3331da177e4SLinus Torvalds if (ret < 0) 3341da177e4SLinus Torvalds break; 3351da177e4SLinus Torvalds } 336aa4d8616SChristoph Hellwig 3371da177e4SLinus Torvalds __free_page(page); 3381da177e4SLinus Torvalds return ret; 3391da177e4SLinus Torvalds } 3401da177e4SLinus Torvalds 341aa4d8616SChristoph Hellwig static int lo_read_simple(struct loop_device *lo, struct request *rq, 342aa4d8616SChristoph Hellwig loff_t pos) 3431da177e4SLinus Torvalds { 3447988613bSKent Overstreet struct bio_vec bvec; 34530112013SMing Lei struct req_iterator iter; 346aa4d8616SChristoph Hellwig struct iov_iter i; 347aa4d8616SChristoph Hellwig ssize_t len; 3481da177e4SLinus Torvalds 34930112013SMing Lei rq_for_each_segment(bvec, rq, iter) { 350aa563d7bSDavid Howells iov_iter_bvec(&i, READ, &bvec, 1, bvec.bv_len); 35118e9710eSChristoph Hellwig len = vfs_iter_read(lo->lo_backing_file, &i, &pos, 0); 352aa4d8616SChristoph Hellwig if (len < 0) 353aa4d8616SChristoph Hellwig return len; 354306df071SDave Young 355aa4d8616SChristoph Hellwig flush_dcache_page(bvec.bv_page); 356aa4d8616SChristoph Hellwig 357aa4d8616SChristoph Hellwig if (len != bvec.bv_len) { 35830112013SMing Lei struct bio *bio; 35930112013SMing Lei 36030112013SMing Lei __rq_for_each_bio(bio, rq) 361306df071SDave Young zero_fill_bio(bio); 3621da177e4SLinus Torvalds break; 363306df071SDave Young } 364aa4d8616SChristoph Hellwig cond_resched(); 3651da177e4SLinus Torvalds } 366aa4d8616SChristoph Hellwig 367306df071SDave Young return 0; 3681da177e4SLinus Torvalds } 3691da177e4SLinus Torvalds 370aa4d8616SChristoph Hellwig static int lo_read_transfer(struct loop_device *lo, struct request *rq, 371aa4d8616SChristoph Hellwig loff_t pos) 372aa4d8616SChristoph Hellwig { 373aa4d8616SChristoph Hellwig struct bio_vec bvec, b; 374aa4d8616SChristoph Hellwig struct req_iterator iter; 375aa4d8616SChristoph Hellwig struct iov_iter i; 376aa4d8616SChristoph Hellwig struct page *page; 377aa4d8616SChristoph Hellwig ssize_t len; 378aa4d8616SChristoph Hellwig int ret = 0; 379aa4d8616SChristoph Hellwig 380aa4d8616SChristoph Hellwig page = alloc_page(GFP_NOIO); 381aa4d8616SChristoph Hellwig if (unlikely(!page)) 382aa4d8616SChristoph Hellwig return -ENOMEM; 383aa4d8616SChristoph Hellwig 384aa4d8616SChristoph Hellwig rq_for_each_segment(bvec, rq, iter) { 385aa4d8616SChristoph Hellwig loff_t offset = pos; 386aa4d8616SChristoph Hellwig 387aa4d8616SChristoph Hellwig b.bv_page = page; 388aa4d8616SChristoph Hellwig b.bv_offset = 0; 389aa4d8616SChristoph Hellwig b.bv_len = bvec.bv_len; 390aa4d8616SChristoph Hellwig 391aa563d7bSDavid Howells iov_iter_bvec(&i, READ, &b, 1, b.bv_len); 39218e9710eSChristoph Hellwig len = vfs_iter_read(lo->lo_backing_file, &i, &pos, 0); 393aa4d8616SChristoph Hellwig if (len < 0) { 394aa4d8616SChristoph Hellwig ret = len; 395aa4d8616SChristoph Hellwig goto out_free_page; 396aa4d8616SChristoph Hellwig } 397aa4d8616SChristoph Hellwig 398aa4d8616SChristoph Hellwig ret = lo_do_transfer(lo, READ, page, 0, bvec.bv_page, 399aa4d8616SChristoph Hellwig bvec.bv_offset, len, offset >> 9); 400aa4d8616SChristoph Hellwig if (ret) 401aa4d8616SChristoph Hellwig goto out_free_page; 402aa4d8616SChristoph Hellwig 403aa4d8616SChristoph Hellwig flush_dcache_page(bvec.bv_page); 404aa4d8616SChristoph Hellwig 405aa4d8616SChristoph Hellwig if (len != bvec.bv_len) { 406aa4d8616SChristoph Hellwig struct bio *bio; 407aa4d8616SChristoph Hellwig 408aa4d8616SChristoph Hellwig __rq_for_each_bio(bio, rq) 409aa4d8616SChristoph Hellwig zero_fill_bio(bio); 410aa4d8616SChristoph Hellwig break; 411aa4d8616SChristoph Hellwig } 412aa4d8616SChristoph Hellwig } 413aa4d8616SChristoph Hellwig 414aa4d8616SChristoph Hellwig ret = 0; 415aa4d8616SChristoph Hellwig out_free_page: 416aa4d8616SChristoph Hellwig __free_page(page); 417aa4d8616SChristoph Hellwig return ret; 418aa4d8616SChristoph Hellwig } 419aa4d8616SChristoph Hellwig 420cf655d95SMing Lei static int lo_discard(struct loop_device *lo, struct request *rq, loff_t pos) 421cf655d95SMing Lei { 422cf655d95SMing Lei /* 423cf655d95SMing Lei * We use punch hole to reclaim the free space used by the 424cf655d95SMing Lei * image a.k.a. discard. However we do not support discard if 425cf655d95SMing Lei * encryption is enabled, because it may give an attacker 426cf655d95SMing Lei * useful information. 427cf655d95SMing Lei */ 428cf655d95SMing Lei struct file *file = lo->lo_backing_file; 429cf655d95SMing Lei int mode = FALLOC_FL_PUNCH_HOLE | FALLOC_FL_KEEP_SIZE; 430cf655d95SMing Lei int ret; 431cf655d95SMing Lei 432cf655d95SMing Lei if ((!file->f_op->fallocate) || lo->lo_encrypt_key_size) { 433cf655d95SMing Lei ret = -EOPNOTSUPP; 434cf655d95SMing Lei goto out; 435cf655d95SMing Lei } 436cf655d95SMing Lei 437cf655d95SMing Lei ret = file->f_op->fallocate(file, mode, pos, blk_rq_bytes(rq)); 438cf655d95SMing Lei if (unlikely(ret && ret != -EINVAL && ret != -EOPNOTSUPP)) 439cf655d95SMing Lei ret = -EIO; 440cf655d95SMing Lei out: 441cf655d95SMing Lei return ret; 442cf655d95SMing Lei } 443cf655d95SMing Lei 444cf655d95SMing Lei static int lo_req_flush(struct loop_device *lo, struct request *rq) 445cf655d95SMing Lei { 446cf655d95SMing Lei struct file *file = lo->lo_backing_file; 447cf655d95SMing Lei int ret = vfs_fsync(file, 0); 448cf655d95SMing Lei if (unlikely(ret && ret != -EINVAL)) 449cf655d95SMing Lei ret = -EIO; 450cf655d95SMing Lei 451cf655d95SMing Lei return ret; 452cf655d95SMing Lei } 453cf655d95SMing Lei 454fe2cb290SChristoph Hellwig static void lo_complete_rq(struct request *rq) 455bc07c10aSMing Lei { 456fe2cb290SChristoph Hellwig struct loop_cmd *cmd = blk_mq_rq_to_pdu(rq); 457f9de14bcSJens Axboe blk_status_t ret = BLK_STS_OK; 458bc07c10aSMing Lei 459f9de14bcSJens Axboe if (!cmd->use_aio || cmd->ret < 0 || cmd->ret == blk_rq_bytes(rq) || 460f9de14bcSJens Axboe req_op(rq) != REQ_OP_READ) { 461f9de14bcSJens Axboe if (cmd->ret < 0) 462f9de14bcSJens Axboe ret = BLK_STS_IOERR; 463f9de14bcSJens Axboe goto end_io; 464bc07c10aSMing Lei } 465fe2cb290SChristoph Hellwig 466f9de14bcSJens Axboe /* 467f9de14bcSJens Axboe * Short READ - if we got some data, advance our request and 468f9de14bcSJens Axboe * retry it. If we got no data, end the rest with EIO. 469f9de14bcSJens Axboe */ 470f9de14bcSJens Axboe if (cmd->ret) { 471f9de14bcSJens Axboe blk_update_request(rq, BLK_STS_OK, cmd->ret); 472f9de14bcSJens Axboe cmd->ret = 0; 473f9de14bcSJens Axboe blk_mq_requeue_request(rq, true); 474f9de14bcSJens Axboe } else { 475f9de14bcSJens Axboe if (cmd->use_aio) { 476f9de14bcSJens Axboe struct bio *bio = rq->bio; 477f9de14bcSJens Axboe 478f9de14bcSJens Axboe while (bio) { 479f9de14bcSJens Axboe zero_fill_bio(bio); 480f9de14bcSJens Axboe bio = bio->bi_next; 481f9de14bcSJens Axboe } 482f9de14bcSJens Axboe } 483f9de14bcSJens Axboe ret = BLK_STS_IOERR; 484f9de14bcSJens Axboe end_io: 485f9de14bcSJens Axboe blk_mq_end_request(rq, ret); 486f9de14bcSJens Axboe } 487bc07c10aSMing Lei } 488bc07c10aSMing Lei 48992d77332SShaohua Li static void lo_rw_aio_do_completion(struct loop_cmd *cmd) 49092d77332SShaohua Li { 4911894e916SJens Axboe struct request *rq = blk_mq_rq_from_pdu(cmd); 4921894e916SJens Axboe 49392d77332SShaohua Li if (!atomic_dec_and_test(&cmd->ref)) 49492d77332SShaohua Li return; 49592d77332SShaohua Li kfree(cmd->bvec); 49692d77332SShaohua Li cmd->bvec = NULL; 4971894e916SJens Axboe blk_mq_complete_request(rq); 49892d77332SShaohua Li } 49992d77332SShaohua Li 500bc07c10aSMing Lei static void lo_rw_aio_complete(struct kiocb *iocb, long ret, long ret2) 501bc07c10aSMing Lei { 502bc07c10aSMing Lei struct loop_cmd *cmd = container_of(iocb, struct loop_cmd, iocb); 503bc07c10aSMing Lei 504d4478e92SShaohua Li if (cmd->css) 505d4478e92SShaohua Li css_put(cmd->css); 506fe2cb290SChristoph Hellwig cmd->ret = ret; 50792d77332SShaohua Li lo_rw_aio_do_completion(cmd); 508bc07c10aSMing Lei } 509bc07c10aSMing Lei 510bc07c10aSMing Lei static int lo_rw_aio(struct loop_device *lo, struct loop_cmd *cmd, 511bc07c10aSMing Lei loff_t pos, bool rw) 512bc07c10aSMing Lei { 513bc07c10aSMing Lei struct iov_iter iter; 51486af5952SMing Lei struct req_iterator rq_iter; 515bc07c10aSMing Lei struct bio_vec *bvec; 5161894e916SJens Axboe struct request *rq = blk_mq_rq_from_pdu(cmd); 51740326d8aSShaohua Li struct bio *bio = rq->bio; 518bc07c10aSMing Lei struct file *file = lo->lo_backing_file; 51986af5952SMing Lei struct bio_vec tmp; 52040326d8aSShaohua Li unsigned int offset; 52186af5952SMing Lei int nr_bvec = 0; 522bc07c10aSMing Lei int ret; 523bc07c10aSMing Lei 52486af5952SMing Lei rq_for_each_bvec(tmp, rq, rq_iter) 52586af5952SMing Lei nr_bvec++; 526bc07c10aSMing Lei 52786af5952SMing Lei if (rq->bio != rq->biotail) { 52886af5952SMing Lei 52986af5952SMing Lei bvec = kmalloc_array(nr_bvec, sizeof(struct bio_vec), 5306da2ec56SKees Cook GFP_NOIO); 53140326d8aSShaohua Li if (!bvec) 53240326d8aSShaohua Li return -EIO; 53340326d8aSShaohua Li cmd->bvec = bvec; 53440326d8aSShaohua Li 535a7297a6aSMing Lei /* 53640326d8aSShaohua Li * The bios of the request may be started from the middle of 53740326d8aSShaohua Li * the 'bvec' because of bio splitting, so we can't directly 53886af5952SMing Lei * copy bio->bi_iov_vec to new bvec. The rq_for_each_bvec 53940326d8aSShaohua Li * API will take care of all details for us. 540a7297a6aSMing Lei */ 54186af5952SMing Lei rq_for_each_bvec(tmp, rq, rq_iter) { 54240326d8aSShaohua Li *bvec = tmp; 54340326d8aSShaohua Li bvec++; 54440326d8aSShaohua Li } 54540326d8aSShaohua Li bvec = cmd->bvec; 54640326d8aSShaohua Li offset = 0; 54740326d8aSShaohua Li } else { 54840326d8aSShaohua Li /* 54940326d8aSShaohua Li * Same here, this bio may be started from the middle of the 55040326d8aSShaohua Li * 'bvec' because of bio splitting, so offset from the bvec 55140326d8aSShaohua Li * must be passed to iov iterator 55240326d8aSShaohua Li */ 55340326d8aSShaohua Li offset = bio->bi_iter.bi_bvec_done; 55440326d8aSShaohua Li bvec = __bvec_iter_bvec(bio->bi_io_vec, bio->bi_iter); 55540326d8aSShaohua Li } 55692d77332SShaohua Li atomic_set(&cmd->ref, 2); 55740326d8aSShaohua Li 55886af5952SMing Lei iov_iter_bvec(&iter, rw, bvec, nr_bvec, blk_rq_bytes(rq)); 55940326d8aSShaohua Li iter.iov_offset = offset; 560bc07c10aSMing Lei 561bc07c10aSMing Lei cmd->iocb.ki_pos = pos; 562bc07c10aSMing Lei cmd->iocb.ki_filp = file; 563bc07c10aSMing Lei cmd->iocb.ki_complete = lo_rw_aio_complete; 564bc07c10aSMing Lei cmd->iocb.ki_flags = IOCB_DIRECT; 565d9a08a9eSAdam Manzanares cmd->iocb.ki_ioprio = IOPRIO_PRIO_VALUE(IOPRIO_CLASS_NONE, 0); 566d4478e92SShaohua Li if (cmd->css) 567d4478e92SShaohua Li kthread_associate_blkcg(cmd->css); 568bc07c10aSMing Lei 569bc07c10aSMing Lei if (rw == WRITE) 570bb7462b6SMiklos Szeredi ret = call_write_iter(file, &cmd->iocb, &iter); 571bc07c10aSMing Lei else 572bb7462b6SMiklos Szeredi ret = call_read_iter(file, &cmd->iocb, &iter); 573bc07c10aSMing Lei 57492d77332SShaohua Li lo_rw_aio_do_completion(cmd); 575d4478e92SShaohua Li kthread_associate_blkcg(NULL); 57692d77332SShaohua Li 577bc07c10aSMing Lei if (ret != -EIOCBQUEUED) 578bc07c10aSMing Lei cmd->iocb.ki_complete(&cmd->iocb, ret, 0); 579bc07c10aSMing Lei return 0; 580bc07c10aSMing Lei } 581bc07c10aSMing Lei 582c1c87c2bSChristoph Hellwig static int do_req_filebacked(struct loop_device *lo, struct request *rq) 583bc07c10aSMing Lei { 584bc07c10aSMing Lei struct loop_cmd *cmd = blk_mq_rq_to_pdu(rq); 585c1c87c2bSChristoph Hellwig loff_t pos = ((loff_t) blk_rq_pos(rq) << 9) + lo->lo_offset; 586bc07c10aSMing Lei 587bc07c10aSMing Lei /* 588bc07c10aSMing Lei * lo_write_simple and lo_read_simple should have been covered 589bc07c10aSMing Lei * by io submit style function like lo_rw_aio(), one blocker 590bc07c10aSMing Lei * is that lo_read_simple() need to call flush_dcache_page after 591bc07c10aSMing Lei * the page is written from kernel, and it isn't easy to handle 592bc07c10aSMing Lei * this in io submit style function which submits all segments 593bc07c10aSMing Lei * of the req at one time. And direct read IO doesn't need to 594bc07c10aSMing Lei * run flush_dcache_page(). 595bc07c10aSMing Lei */ 596c1c87c2bSChristoph Hellwig switch (req_op(rq)) { 597c1c87c2bSChristoph Hellwig case REQ_OP_FLUSH: 598c1c87c2bSChristoph Hellwig return lo_req_flush(lo, rq); 599c1c87c2bSChristoph Hellwig case REQ_OP_DISCARD: 60019372e27SChristoph Hellwig case REQ_OP_WRITE_ZEROES: 601c1c87c2bSChristoph Hellwig return lo_discard(lo, rq, pos); 602c1c87c2bSChristoph Hellwig case REQ_OP_WRITE: 603c1c87c2bSChristoph Hellwig if (lo->transfer) 604c1c87c2bSChristoph Hellwig return lo_write_transfer(lo, rq, pos); 605c1c87c2bSChristoph Hellwig else if (cmd->use_aio) 606c1c87c2bSChristoph Hellwig return lo_rw_aio(lo, cmd, pos, WRITE); 607c1c87c2bSChristoph Hellwig else 608bc07c10aSMing Lei return lo_write_simple(lo, rq, pos); 609c1c87c2bSChristoph Hellwig case REQ_OP_READ: 610c1c87c2bSChristoph Hellwig if (lo->transfer) 611c1c87c2bSChristoph Hellwig return lo_read_transfer(lo, rq, pos); 612c1c87c2bSChristoph Hellwig else if (cmd->use_aio) 613c1c87c2bSChristoph Hellwig return lo_rw_aio(lo, cmd, pos, READ); 614bc07c10aSMing Lei else 615bc07c10aSMing Lei return lo_read_simple(lo, rq, pos); 616c1c87c2bSChristoph Hellwig default: 617c1c87c2bSChristoph Hellwig WARN_ON_ONCE(1); 618c1c87c2bSChristoph Hellwig return -EIO; 619bc07c10aSMing Lei } 6201da177e4SLinus Torvalds } 6211da177e4SLinus Torvalds 6222e5ab5f3SMing Lei static inline void loop_update_dio(struct loop_device *lo) 6232e5ab5f3SMing Lei { 6242e5ab5f3SMing Lei __loop_update_dio(lo, io_is_direct(lo->lo_backing_file) | 6252e5ab5f3SMing Lei lo->use_dio); 6262e5ab5f3SMing Lei } 6272e5ab5f3SMing Lei 62806f0e9e6SMing Lei static void loop_reread_partitions(struct loop_device *lo, 62906f0e9e6SMing Lei struct block_device *bdev) 63006f0e9e6SMing Lei { 63106f0e9e6SMing Lei int rc; 63206f0e9e6SMing Lei 63306f0e9e6SMing Lei rc = blkdev_reread_part(bdev); 63406f0e9e6SMing Lei if (rc) 63506f0e9e6SMing Lei pr_warn("%s: partition scan of loop%d (%s) failed (rc=%d)\n", 63606f0e9e6SMing Lei __func__, lo->lo_number, lo->lo_file_name, rc); 63706f0e9e6SMing Lei } 63806f0e9e6SMing Lei 639d2ac838eSTheodore Ts'o static inline int is_loop_device(struct file *file) 640d2ac838eSTheodore Ts'o { 641d2ac838eSTheodore Ts'o struct inode *i = file->f_mapping->host; 642d2ac838eSTheodore Ts'o 643d2ac838eSTheodore Ts'o return i && S_ISBLK(i->i_mode) && MAJOR(i->i_rdev) == LOOP_MAJOR; 644d2ac838eSTheodore Ts'o } 645d2ac838eSTheodore Ts'o 646d2ac838eSTheodore Ts'o static int loop_validate_file(struct file *file, struct block_device *bdev) 647d2ac838eSTheodore Ts'o { 648d2ac838eSTheodore Ts'o struct inode *inode = file->f_mapping->host; 649d2ac838eSTheodore Ts'o struct file *f = file; 650d2ac838eSTheodore Ts'o 651d2ac838eSTheodore Ts'o /* Avoid recursion */ 652d2ac838eSTheodore Ts'o while (is_loop_device(f)) { 653d2ac838eSTheodore Ts'o struct loop_device *l; 654d2ac838eSTheodore Ts'o 655d2ac838eSTheodore Ts'o if (f->f_mapping->host->i_bdev == bdev) 656d2ac838eSTheodore Ts'o return -EBADF; 657d2ac838eSTheodore Ts'o 658d2ac838eSTheodore Ts'o l = f->f_mapping->host->i_bdev->bd_disk->private_data; 659d2ac838eSTheodore Ts'o if (l->lo_state == Lo_unbound) { 660d2ac838eSTheodore Ts'o return -EINVAL; 661d2ac838eSTheodore Ts'o } 662d2ac838eSTheodore Ts'o f = l->lo_backing_file; 663d2ac838eSTheodore Ts'o } 664d2ac838eSTheodore Ts'o if (!S_ISREG(inode->i_mode) && !S_ISBLK(inode->i_mode)) 665d2ac838eSTheodore Ts'o return -EINVAL; 666d2ac838eSTheodore Ts'o return 0; 667d2ac838eSTheodore Ts'o } 668d2ac838eSTheodore Ts'o 6691da177e4SLinus Torvalds /* 6701da177e4SLinus Torvalds * loop_change_fd switched the backing store of a loopback device to 6711da177e4SLinus Torvalds * a new file. This is useful for operating system installers to free up 6721da177e4SLinus Torvalds * the original file and in High Availability environments to switch to 6731da177e4SLinus Torvalds * an alternative location for the content in case of server meltdown. 6741da177e4SLinus Torvalds * This can only work if the loop device is used read-only, and if the 6751da177e4SLinus Torvalds * new backing store is the same size and type as the old backing store. 6761da177e4SLinus Torvalds */ 677bb214884SAl Viro static int loop_change_fd(struct loop_device *lo, struct block_device *bdev, 678bb214884SAl Viro unsigned int arg) 6791da177e4SLinus Torvalds { 6801dded9acSJan Kara struct file *file = NULL, *old_file; 6811da177e4SLinus Torvalds int error; 68285b0a54aSJan Kara bool partscan; 6831da177e4SLinus Torvalds 684c28445faSJan Kara error = mutex_lock_killable(&loop_ctl_mutex); 685c3710770SJan Kara if (error) 686c3710770SJan Kara return error; 6871da177e4SLinus Torvalds error = -ENXIO; 6881da177e4SLinus Torvalds if (lo->lo_state != Lo_bound) 6891dded9acSJan Kara goto out_err; 6901da177e4SLinus Torvalds 6911da177e4SLinus Torvalds /* the loop device has to be read-only */ 6921da177e4SLinus Torvalds error = -EINVAL; 6931da177e4SLinus Torvalds if (!(lo->lo_flags & LO_FLAGS_READ_ONLY)) 6941dded9acSJan Kara goto out_err; 6951da177e4SLinus Torvalds 6961da177e4SLinus Torvalds error = -EBADF; 6971da177e4SLinus Torvalds file = fget(arg); 6981da177e4SLinus Torvalds if (!file) 6991dded9acSJan Kara goto out_err; 7001da177e4SLinus Torvalds 701d2ac838eSTheodore Ts'o error = loop_validate_file(file, bdev); 702d2ac838eSTheodore Ts'o if (error) 7031dded9acSJan Kara goto out_err; 704d2ac838eSTheodore Ts'o 7051da177e4SLinus Torvalds old_file = lo->lo_backing_file; 7061da177e4SLinus Torvalds 7071da177e4SLinus Torvalds error = -EINVAL; 7081da177e4SLinus Torvalds 7091da177e4SLinus Torvalds /* size of the new backing store needs to be the same */ 7101da177e4SLinus Torvalds if (get_loop_size(lo, file) != get_loop_size(lo, old_file)) 7111dded9acSJan Kara goto out_err; 7121da177e4SLinus Torvalds 7131da177e4SLinus Torvalds /* and ... switch */ 71443cade80SOmar Sandoval blk_mq_freeze_queue(lo->lo_queue); 71543cade80SOmar Sandoval mapping_set_gfp_mask(old_file->f_mapping, lo->old_gfp_mask); 71643cade80SOmar Sandoval lo->lo_backing_file = file; 71743cade80SOmar Sandoval lo->old_gfp_mask = mapping_gfp_mask(file->f_mapping); 71843cade80SOmar Sandoval mapping_set_gfp_mask(file->f_mapping, 71943cade80SOmar Sandoval lo->old_gfp_mask & ~(__GFP_IO|__GFP_FS)); 72043cade80SOmar Sandoval loop_update_dio(lo); 72143cade80SOmar Sandoval blk_mq_unfreeze_queue(lo->lo_queue); 72285b0a54aSJan Kara partscan = lo->lo_flags & LO_FLAGS_PARTSCAN; 723c3710770SJan Kara mutex_unlock(&loop_ctl_mutex); 7241dded9acSJan Kara /* 7251dded9acSJan Kara * We must drop file reference outside of loop_ctl_mutex as dropping 7261dded9acSJan Kara * the file ref can take bd_mutex which creates circular locking 7271dded9acSJan Kara * dependency. 7281dded9acSJan Kara */ 7291dded9acSJan Kara fput(old_file); 73085b0a54aSJan Kara if (partscan) 73185b0a54aSJan Kara loop_reread_partitions(lo, bdev); 7321da177e4SLinus Torvalds return 0; 7331da177e4SLinus Torvalds 7341dded9acSJan Kara out_err: 735c3710770SJan Kara mutex_unlock(&loop_ctl_mutex); 7361dded9acSJan Kara if (file) 7371dded9acSJan Kara fput(file); 7381da177e4SLinus Torvalds return error; 7391da177e4SLinus Torvalds } 7401da177e4SLinus Torvalds 741ee862730SMilan Broz /* loop sysfs attributes */ 742ee862730SMilan Broz 743ee862730SMilan Broz static ssize_t loop_attr_show(struct device *dev, char *page, 744ee862730SMilan Broz ssize_t (*callback)(struct loop_device *, char *)) 745ee862730SMilan Broz { 74634dd82afSKay Sievers struct gendisk *disk = dev_to_disk(dev); 74734dd82afSKay Sievers struct loop_device *lo = disk->private_data; 748ee862730SMilan Broz 74934dd82afSKay Sievers return callback(lo, page); 750ee862730SMilan Broz } 751ee862730SMilan Broz 752ee862730SMilan Broz #define LOOP_ATTR_RO(_name) \ 753ee862730SMilan Broz static ssize_t loop_attr_##_name##_show(struct loop_device *, char *); \ 754ee862730SMilan Broz static ssize_t loop_attr_do_show_##_name(struct device *d, \ 755ee862730SMilan Broz struct device_attribute *attr, char *b) \ 756ee862730SMilan Broz { \ 757ee862730SMilan Broz return loop_attr_show(d, b, loop_attr_##_name##_show); \ 758ee862730SMilan Broz } \ 759ee862730SMilan Broz static struct device_attribute loop_attr_##_name = \ 7605657a819SJoe Perches __ATTR(_name, 0444, loop_attr_do_show_##_name, NULL); 761ee862730SMilan Broz 762ee862730SMilan Broz static ssize_t loop_attr_backing_file_show(struct loop_device *lo, char *buf) 763ee862730SMilan Broz { 764ee862730SMilan Broz ssize_t ret; 765ee862730SMilan Broz char *p = NULL; 766ee862730SMilan Broz 76705eb0f25SKay Sievers spin_lock_irq(&lo->lo_lock); 768ee862730SMilan Broz if (lo->lo_backing_file) 7699bf39ab2SMiklos Szeredi p = file_path(lo->lo_backing_file, buf, PAGE_SIZE - 1); 77005eb0f25SKay Sievers spin_unlock_irq(&lo->lo_lock); 771ee862730SMilan Broz 772ee862730SMilan Broz if (IS_ERR_OR_NULL(p)) 773ee862730SMilan Broz ret = PTR_ERR(p); 774ee862730SMilan Broz else { 775ee862730SMilan Broz ret = strlen(p); 776ee862730SMilan Broz memmove(buf, p, ret); 777ee862730SMilan Broz buf[ret++] = '\n'; 778ee862730SMilan Broz buf[ret] = 0; 779ee862730SMilan Broz } 780ee862730SMilan Broz 781ee862730SMilan Broz return ret; 782ee862730SMilan Broz } 783ee862730SMilan Broz 784ee862730SMilan Broz static ssize_t loop_attr_offset_show(struct loop_device *lo, char *buf) 785ee862730SMilan Broz { 786ee862730SMilan Broz return sprintf(buf, "%llu\n", (unsigned long long)lo->lo_offset); 787ee862730SMilan Broz } 788ee862730SMilan Broz 789ee862730SMilan Broz static ssize_t loop_attr_sizelimit_show(struct loop_device *lo, char *buf) 790ee862730SMilan Broz { 791ee862730SMilan Broz return sprintf(buf, "%llu\n", (unsigned long long)lo->lo_sizelimit); 792ee862730SMilan Broz } 793ee862730SMilan Broz 794ee862730SMilan Broz static ssize_t loop_attr_autoclear_show(struct loop_device *lo, char *buf) 795ee862730SMilan Broz { 796ee862730SMilan Broz int autoclear = (lo->lo_flags & LO_FLAGS_AUTOCLEAR); 797ee862730SMilan Broz 798ee862730SMilan Broz return sprintf(buf, "%s\n", autoclear ? "1" : "0"); 799ee862730SMilan Broz } 800ee862730SMilan Broz 801e03c8dd1SKay Sievers static ssize_t loop_attr_partscan_show(struct loop_device *lo, char *buf) 802e03c8dd1SKay Sievers { 803e03c8dd1SKay Sievers int partscan = (lo->lo_flags & LO_FLAGS_PARTSCAN); 804e03c8dd1SKay Sievers 805e03c8dd1SKay Sievers return sprintf(buf, "%s\n", partscan ? "1" : "0"); 806e03c8dd1SKay Sievers } 807e03c8dd1SKay Sievers 8082e5ab5f3SMing Lei static ssize_t loop_attr_dio_show(struct loop_device *lo, char *buf) 8092e5ab5f3SMing Lei { 8102e5ab5f3SMing Lei int dio = (lo->lo_flags & LO_FLAGS_DIRECT_IO); 8112e5ab5f3SMing Lei 8122e5ab5f3SMing Lei return sprintf(buf, "%s\n", dio ? "1" : "0"); 8132e5ab5f3SMing Lei } 8142e5ab5f3SMing Lei 815ee862730SMilan Broz LOOP_ATTR_RO(backing_file); 816ee862730SMilan Broz LOOP_ATTR_RO(offset); 817ee862730SMilan Broz LOOP_ATTR_RO(sizelimit); 818ee862730SMilan Broz LOOP_ATTR_RO(autoclear); 819e03c8dd1SKay Sievers LOOP_ATTR_RO(partscan); 8202e5ab5f3SMing Lei LOOP_ATTR_RO(dio); 821ee862730SMilan Broz 822ee862730SMilan Broz static struct attribute *loop_attrs[] = { 823ee862730SMilan Broz &loop_attr_backing_file.attr, 824ee862730SMilan Broz &loop_attr_offset.attr, 825ee862730SMilan Broz &loop_attr_sizelimit.attr, 826ee862730SMilan Broz &loop_attr_autoclear.attr, 827e03c8dd1SKay Sievers &loop_attr_partscan.attr, 8282e5ab5f3SMing Lei &loop_attr_dio.attr, 829ee862730SMilan Broz NULL, 830ee862730SMilan Broz }; 831ee862730SMilan Broz 832ee862730SMilan Broz static struct attribute_group loop_attribute_group = { 833ee862730SMilan Broz .name = "loop", 834ee862730SMilan Broz .attrs= loop_attrs, 835ee862730SMilan Broz }; 836ee862730SMilan Broz 837d3349b6bSTetsuo Handa static void loop_sysfs_init(struct loop_device *lo) 838ee862730SMilan Broz { 839d3349b6bSTetsuo Handa lo->sysfs_inited = !sysfs_create_group(&disk_to_dev(lo->lo_disk)->kobj, 840ee862730SMilan Broz &loop_attribute_group); 841ee862730SMilan Broz } 842ee862730SMilan Broz 843ee862730SMilan Broz static void loop_sysfs_exit(struct loop_device *lo) 844ee862730SMilan Broz { 845d3349b6bSTetsuo Handa if (lo->sysfs_inited) 846ee862730SMilan Broz sysfs_remove_group(&disk_to_dev(lo->lo_disk)->kobj, 847ee862730SMilan Broz &loop_attribute_group); 848ee862730SMilan Broz } 849ee862730SMilan Broz 850dfaa2ef6SLukas Czerner static void loop_config_discard(struct loop_device *lo) 851dfaa2ef6SLukas Czerner { 852dfaa2ef6SLukas Czerner struct file *file = lo->lo_backing_file; 853dfaa2ef6SLukas Czerner struct inode *inode = file->f_mapping->host; 854dfaa2ef6SLukas Czerner struct request_queue *q = lo->lo_queue; 855dfaa2ef6SLukas Czerner 856dfaa2ef6SLukas Czerner /* 857dfaa2ef6SLukas Czerner * We use punch hole to reclaim the free space used by the 85812a64d2fSOlaf Hering * image a.k.a. discard. However we do not support discard if 859dfaa2ef6SLukas Czerner * encryption is enabled, because it may give an attacker 860dfaa2ef6SLukas Czerner * useful information. 861dfaa2ef6SLukas Czerner */ 862dfaa2ef6SLukas Czerner if ((!file->f_op->fallocate) || 863dfaa2ef6SLukas Czerner lo->lo_encrypt_key_size) { 864dfaa2ef6SLukas Czerner q->limits.discard_granularity = 0; 865dfaa2ef6SLukas Czerner q->limits.discard_alignment = 0; 8662bb4cd5cSJens Axboe blk_queue_max_discard_sectors(q, 0); 86719372e27SChristoph Hellwig blk_queue_max_write_zeroes_sectors(q, 0); 8688b904b5bSBart Van Assche blk_queue_flag_clear(QUEUE_FLAG_DISCARD, q); 869dfaa2ef6SLukas Czerner return; 870dfaa2ef6SLukas Czerner } 871dfaa2ef6SLukas Czerner 872dfaa2ef6SLukas Czerner q->limits.discard_granularity = inode->i_sb->s_blocksize; 873dfaf3c03SLukas Czerner q->limits.discard_alignment = 0; 874f2c6df7dSHannes Reinecke 8751e6ec9eaSOmar Sandoval blk_queue_max_discard_sectors(q, UINT_MAX >> 9); 8761e6ec9eaSOmar Sandoval blk_queue_max_write_zeroes_sectors(q, UINT_MAX >> 9); 8778b904b5bSBart Van Assche blk_queue_flag_set(QUEUE_FLAG_DISCARD, q); 878dfaa2ef6SLukas Czerner } 879dfaa2ef6SLukas Czerner 880e03a3d7aSMing Lei static void loop_unprepare_queue(struct loop_device *lo) 881e03a3d7aSMing Lei { 8823989144fSPetr Mladek kthread_flush_worker(&lo->worker); 883e03a3d7aSMing Lei kthread_stop(lo->worker_task); 884e03a3d7aSMing Lei } 885e03a3d7aSMing Lei 886b2ee7d46SNeilBrown static int loop_kthread_worker_fn(void *worker_ptr) 887b2ee7d46SNeilBrown { 888b2ee7d46SNeilBrown current->flags |= PF_LESS_THROTTLE; 889b2ee7d46SNeilBrown return kthread_worker_fn(worker_ptr); 890b2ee7d46SNeilBrown } 891b2ee7d46SNeilBrown 892e03a3d7aSMing Lei static int loop_prepare_queue(struct loop_device *lo) 893e03a3d7aSMing Lei { 8943989144fSPetr Mladek kthread_init_worker(&lo->worker); 895b2ee7d46SNeilBrown lo->worker_task = kthread_run(loop_kthread_worker_fn, 896e03a3d7aSMing Lei &lo->worker, "loop%d", lo->lo_number); 897e03a3d7aSMing Lei if (IS_ERR(lo->worker_task)) 898e03a3d7aSMing Lei return -ENOMEM; 899e03a3d7aSMing Lei set_user_nice(lo->worker_task, MIN_NICE); 900e03a3d7aSMing Lei return 0; 901e03a3d7aSMing Lei } 902e03a3d7aSMing Lei 903bb214884SAl Viro static int loop_set_fd(struct loop_device *lo, fmode_t mode, 9041da177e4SLinus Torvalds struct block_device *bdev, unsigned int arg) 9051da177e4SLinus Torvalds { 906d2ac838eSTheodore Ts'o struct file *file; 9071da177e4SLinus Torvalds struct inode *inode; 9081da177e4SLinus Torvalds struct address_space *mapping; 9091da177e4SLinus Torvalds int lo_flags = 0; 9101da177e4SLinus Torvalds int error; 9111da177e4SLinus Torvalds loff_t size; 91285b0a54aSJan Kara bool partscan; 9131da177e4SLinus Torvalds 9141da177e4SLinus Torvalds /* This is safe, since we have a reference from open(). */ 9151da177e4SLinus Torvalds __module_get(THIS_MODULE); 9161da177e4SLinus Torvalds 9171da177e4SLinus Torvalds error = -EBADF; 9181da177e4SLinus Torvalds file = fget(arg); 9191da177e4SLinus Torvalds if (!file) 9201da177e4SLinus Torvalds goto out; 9211da177e4SLinus Torvalds 922c28445faSJan Kara error = mutex_lock_killable(&loop_ctl_mutex); 923757ecf40SJan Kara if (error) 924757ecf40SJan Kara goto out_putf; 925757ecf40SJan Kara 9261da177e4SLinus Torvalds error = -EBUSY; 9271da177e4SLinus Torvalds if (lo->lo_state != Lo_unbound) 928757ecf40SJan Kara goto out_unlock; 9291da177e4SLinus Torvalds 930d2ac838eSTheodore Ts'o error = loop_validate_file(file, bdev); 931d2ac838eSTheodore Ts'o if (error) 932757ecf40SJan Kara goto out_unlock; 9331da177e4SLinus Torvalds 9341da177e4SLinus Torvalds mapping = file->f_mapping; 9351da177e4SLinus Torvalds inode = mapping->host; 9361da177e4SLinus Torvalds 937456be148SChristoph Hellwig if (!(file->f_mode & FMODE_WRITE) || !(mode & FMODE_WRITE) || 938283e7e5dSAl Viro !file->f_op->write_iter) 9391da177e4SLinus Torvalds lo_flags |= LO_FLAGS_READ_ONLY; 9401da177e4SLinus Torvalds 9411da177e4SLinus Torvalds error = -EFBIG; 942456be148SChristoph Hellwig size = get_loop_size(lo, file); 943456be148SChristoph Hellwig if ((loff_t)(sector_t)size != size) 944757ecf40SJan Kara goto out_unlock; 945e03a3d7aSMing Lei error = loop_prepare_queue(lo); 946e03a3d7aSMing Lei if (error) 947757ecf40SJan Kara goto out_unlock; 9481da177e4SLinus Torvalds 949456be148SChristoph Hellwig error = 0; 9501da177e4SLinus Torvalds 9511da177e4SLinus Torvalds set_device_ro(bdev, (lo_flags & LO_FLAGS_READ_ONLY) != 0); 9521da177e4SLinus Torvalds 9532e5ab5f3SMing Lei lo->use_dio = false; 9541da177e4SLinus Torvalds lo->lo_device = bdev; 9551da177e4SLinus Torvalds lo->lo_flags = lo_flags; 9561da177e4SLinus Torvalds lo->lo_backing_file = file; 957aa4d8616SChristoph Hellwig lo->transfer = NULL; 9581da177e4SLinus Torvalds lo->ioctl = NULL; 9591da177e4SLinus Torvalds lo->lo_sizelimit = 0; 9601da177e4SLinus Torvalds lo->old_gfp_mask = mapping_gfp_mask(mapping); 9611da177e4SLinus Torvalds mapping_set_gfp_mask(mapping, lo->old_gfp_mask & ~(__GFP_IO|__GFP_FS)); 9621da177e4SLinus Torvalds 96368db1961SNikanth Karthikesan if (!(lo_flags & LO_FLAGS_READ_ONLY) && file->f_op->fsync) 96421d0727fSJens Axboe blk_queue_write_cache(lo->lo_queue, true, false); 96568db1961SNikanth Karthikesan 9662e5ab5f3SMing Lei loop_update_dio(lo); 96773285082SKen Chen set_capacity(lo->lo_disk, size); 9681da177e4SLinus Torvalds bd_set_size(bdev, size << 9); 969ee862730SMilan Broz loop_sysfs_init(lo); 970c3473c63SDavid Zeuthen /* let user-space know about the new size */ 971c3473c63SDavid Zeuthen kobject_uevent(&disk_to_dev(bdev->bd_disk)->kobj, KOBJ_CHANGE); 9721da177e4SLinus Torvalds 9738a0740c4SOmar Sandoval set_blocksize(bdev, S_ISBLK(inode->i_mode) ? 9748a0740c4SOmar Sandoval block_size(inode->i_bdev) : PAGE_SIZE); 9751da177e4SLinus Torvalds 9766c997918SSerge E. Hallyn lo->lo_state = Lo_bound; 977e03c8dd1SKay Sievers if (part_shift) 978e03c8dd1SKay Sievers lo->lo_flags |= LO_FLAGS_PARTSCAN; 97985b0a54aSJan Kara partscan = lo->lo_flags & LO_FLAGS_PARTSCAN; 980c1681bf8SAnatol Pomozov 981c1681bf8SAnatol Pomozov /* Grab the block_device to prevent its destruction after we 982a2505b79SJan Kara * put /dev/loopXX inode. Later in __loop_clr_fd() we bdput(bdev). 983c1681bf8SAnatol Pomozov */ 984c1681bf8SAnatol Pomozov bdgrab(bdev); 985757ecf40SJan Kara mutex_unlock(&loop_ctl_mutex); 98685b0a54aSJan Kara if (partscan) 98785b0a54aSJan Kara loop_reread_partitions(lo, bdev); 9881da177e4SLinus Torvalds return 0; 9891da177e4SLinus Torvalds 990757ecf40SJan Kara out_unlock: 991757ecf40SJan Kara mutex_unlock(&loop_ctl_mutex); 9921da177e4SLinus Torvalds out_putf: 9931da177e4SLinus Torvalds fput(file); 9941da177e4SLinus Torvalds out: 9951da177e4SLinus Torvalds /* This is safe: open() is still holding a reference. */ 9961da177e4SLinus Torvalds module_put(THIS_MODULE); 9971da177e4SLinus Torvalds return error; 9981da177e4SLinus Torvalds } 9991da177e4SLinus Torvalds 10001da177e4SLinus Torvalds static int 10011da177e4SLinus Torvalds loop_release_xfer(struct loop_device *lo) 10021da177e4SLinus Torvalds { 10031da177e4SLinus Torvalds int err = 0; 10041da177e4SLinus Torvalds struct loop_func_table *xfer = lo->lo_encryption; 10051da177e4SLinus Torvalds 10061da177e4SLinus Torvalds if (xfer) { 10071da177e4SLinus Torvalds if (xfer->release) 10081da177e4SLinus Torvalds err = xfer->release(lo); 10091da177e4SLinus Torvalds lo->transfer = NULL; 10101da177e4SLinus Torvalds lo->lo_encryption = NULL; 10111da177e4SLinus Torvalds module_put(xfer->owner); 10121da177e4SLinus Torvalds } 10131da177e4SLinus Torvalds return err; 10141da177e4SLinus Torvalds } 10151da177e4SLinus Torvalds 10161da177e4SLinus Torvalds static int 10171da177e4SLinus Torvalds loop_init_xfer(struct loop_device *lo, struct loop_func_table *xfer, 10181da177e4SLinus Torvalds const struct loop_info64 *i) 10191da177e4SLinus Torvalds { 10201da177e4SLinus Torvalds int err = 0; 10211da177e4SLinus Torvalds 10221da177e4SLinus Torvalds if (xfer) { 10231da177e4SLinus Torvalds struct module *owner = xfer->owner; 10241da177e4SLinus Torvalds 10251da177e4SLinus Torvalds if (!try_module_get(owner)) 10261da177e4SLinus Torvalds return -EINVAL; 10271da177e4SLinus Torvalds if (xfer->init) 10281da177e4SLinus Torvalds err = xfer->init(lo, i); 10291da177e4SLinus Torvalds if (err) 10301da177e4SLinus Torvalds module_put(owner); 10311da177e4SLinus Torvalds else 10321da177e4SLinus Torvalds lo->lo_encryption = xfer; 10331da177e4SLinus Torvalds } 10341da177e4SLinus Torvalds return err; 10351da177e4SLinus Torvalds } 10361da177e4SLinus Torvalds 10370da03cabSJan Kara static int __loop_clr_fd(struct loop_device *lo, bool release) 10381da177e4SLinus Torvalds { 10397ccd0791SJan Kara struct file *filp = NULL; 1040b4e3ca1aSAl Viro gfp_t gfp = lo->old_gfp_mask; 10414c823cc3SAyan George struct block_device *bdev = lo->lo_device; 10427ccd0791SJan Kara int err = 0; 10430da03cabSJan Kara bool partscan = false; 10440da03cabSJan Kara int lo_number; 10451da177e4SLinus Torvalds 10467ccd0791SJan Kara mutex_lock(&loop_ctl_mutex); 10477ccd0791SJan Kara if (WARN_ON_ONCE(lo->lo_state != Lo_rundown)) { 10487ccd0791SJan Kara err = -ENXIO; 10497ccd0791SJan Kara goto out_unlock; 10507ccd0791SJan Kara } 10511da177e4SLinus Torvalds 10527ccd0791SJan Kara filp = lo->lo_backing_file; 10537ccd0791SJan Kara if (filp == NULL) { 10547ccd0791SJan Kara err = -EINVAL; 10557ccd0791SJan Kara goto out_unlock; 10567ccd0791SJan Kara } 10571da177e4SLinus Torvalds 1058f8933667SMing Lei /* freeze request queue during the transition */ 1059f8933667SMing Lei blk_mq_freeze_queue(lo->lo_queue); 1060f8933667SMing Lei 10611da177e4SLinus Torvalds spin_lock_irq(&lo->lo_lock); 10621da177e4SLinus Torvalds lo->lo_backing_file = NULL; 106305eb0f25SKay Sievers spin_unlock_irq(&lo->lo_lock); 10641da177e4SLinus Torvalds 10651da177e4SLinus Torvalds loop_release_xfer(lo); 10661da177e4SLinus Torvalds lo->transfer = NULL; 10671da177e4SLinus Torvalds lo->ioctl = NULL; 10681da177e4SLinus Torvalds lo->lo_device = NULL; 10691da177e4SLinus Torvalds lo->lo_encryption = NULL; 10701da177e4SLinus Torvalds lo->lo_offset = 0; 10711da177e4SLinus Torvalds lo->lo_sizelimit = 0; 10721da177e4SLinus Torvalds lo->lo_encrypt_key_size = 0; 10731da177e4SLinus Torvalds memset(lo->lo_encrypt_key, 0, LO_KEY_SIZE); 10741da177e4SLinus Torvalds memset(lo->lo_crypt_name, 0, LO_NAME_SIZE); 10751da177e4SLinus Torvalds memset(lo->lo_file_name, 0, LO_NAME_SIZE); 107689e4fdecSOmar Sandoval blk_queue_logical_block_size(lo->lo_queue, 512); 1077bf093753SOmar Sandoval blk_queue_physical_block_size(lo->lo_queue, 512); 1078bf093753SOmar Sandoval blk_queue_io_min(lo->lo_queue, 512); 1079c1681bf8SAnatol Pomozov if (bdev) { 1080c1681bf8SAnatol Pomozov bdput(bdev); 1081f98393a6SPeter Zijlstra invalidate_bdev(bdev); 1082eedffa28SJeff Layton bdev->bd_inode->i_mapping->wb_err = 0; 1083c1681bf8SAnatol Pomozov } 108473285082SKen Chen set_capacity(lo->lo_disk, 0); 108551a0bb0cSMilan Broz loop_sysfs_exit(lo); 1086c3473c63SDavid Zeuthen if (bdev) { 10871da177e4SLinus Torvalds bd_set_size(bdev, 0); 1088c3473c63SDavid Zeuthen /* let user-space know about this change */ 1089c3473c63SDavid Zeuthen kobject_uevent(&disk_to_dev(bdev->bd_disk)->kobj, KOBJ_CHANGE); 1090c3473c63SDavid Zeuthen } 10911da177e4SLinus Torvalds mapping_set_gfp_mask(filp->f_mapping, gfp); 10921da177e4SLinus Torvalds lo->lo_state = Lo_unbound; 10931da177e4SLinus Torvalds /* This is safe: open() is still holding a reference. */ 10941da177e4SLinus Torvalds module_put(THIS_MODULE); 1095f8933667SMing Lei blk_mq_unfreeze_queue(lo->lo_queue); 1096f8933667SMing Lei 10970da03cabSJan Kara partscan = lo->lo_flags & LO_FLAGS_PARTSCAN && bdev; 10980da03cabSJan Kara lo_number = lo->lo_number; 10990da03cabSJan Kara lo->lo_flags = 0; 11000da03cabSJan Kara if (!part_shift) 11010da03cabSJan Kara lo->lo_disk->flags |= GENHD_FL_NO_PART_SCAN; 11020da03cabSJan Kara loop_unprepare_queue(lo); 11030da03cabSJan Kara out_unlock: 11040da03cabSJan Kara mutex_unlock(&loop_ctl_mutex); 11050da03cabSJan Kara if (partscan) { 1106d57f3374SJan Kara /* 1107d57f3374SJan Kara * bd_mutex has been held already in release path, so don't 1108d57f3374SJan Kara * acquire it if this function is called in such case. 1109d57f3374SJan Kara * 1110d57f3374SJan Kara * If the reread partition isn't from release path, lo_refcnt 1111d57f3374SJan Kara * must be at least one and it can only become zero when the 1112d57f3374SJan Kara * current holder is released. 1113d57f3374SJan Kara */ 11140da03cabSJan Kara if (release) 1115d57f3374SJan Kara err = __blkdev_reread_part(bdev); 1116d57f3374SJan Kara else 1117d57f3374SJan Kara err = blkdev_reread_part(bdev); 1118*40853d6fSDongli Zhang if (err) 1119d57f3374SJan Kara pr_warn("%s: partition scan of loop%d failed (rc=%d)\n", 11200da03cabSJan Kara __func__, lo_number, err); 1121d57f3374SJan Kara /* Device is gone, no point in returning error */ 1122d57f3374SJan Kara err = 0; 1123d57f3374SJan Kara } 1124f028f3b2SNikanth Karthikesan /* 1125310ca162STetsuo Handa * Need not hold loop_ctl_mutex to fput backing file. 1126310ca162STetsuo Handa * Calling fput holding loop_ctl_mutex triggers a circular 1127f028f3b2SNikanth Karthikesan * lock dependency possibility warning as fput can take 1128310ca162STetsuo Handa * bd_mutex which is usually taken before loop_ctl_mutex. 1129f028f3b2SNikanth Karthikesan */ 11307ccd0791SJan Kara if (filp) 1131f028f3b2SNikanth Karthikesan fput(filp); 11327ccd0791SJan Kara return err; 11331da177e4SLinus Torvalds } 11341da177e4SLinus Torvalds 1135a2505b79SJan Kara static int loop_clr_fd(struct loop_device *lo) 1136a2505b79SJan Kara { 11377ccd0791SJan Kara int err; 11387ccd0791SJan Kara 1139c28445faSJan Kara err = mutex_lock_killable(&loop_ctl_mutex); 11407ccd0791SJan Kara if (err) 11417ccd0791SJan Kara return err; 11427ccd0791SJan Kara if (lo->lo_state != Lo_bound) { 11437ccd0791SJan Kara mutex_unlock(&loop_ctl_mutex); 1144a2505b79SJan Kara return -ENXIO; 11457ccd0791SJan Kara } 1146a2505b79SJan Kara /* 1147a2505b79SJan Kara * If we've explicitly asked to tear down the loop device, 1148a2505b79SJan Kara * and it has an elevated reference count, set it for auto-teardown when 1149a2505b79SJan Kara * the last reference goes away. This stops $!~#$@ udev from 1150a2505b79SJan Kara * preventing teardown because it decided that it needs to run blkid on 1151a2505b79SJan Kara * the loopback device whenever they appear. xfstests is notorious for 1152a2505b79SJan Kara * failing tests because blkid via udev races with a losetup 1153a2505b79SJan Kara * <dev>/do something like mkfs/losetup -d <dev> causing the losetup -d 1154a2505b79SJan Kara * command to fail with EBUSY. 1155a2505b79SJan Kara */ 1156a2505b79SJan Kara if (atomic_read(&lo->lo_refcnt) > 1) { 1157a2505b79SJan Kara lo->lo_flags |= LO_FLAGS_AUTOCLEAR; 1158a2505b79SJan Kara mutex_unlock(&loop_ctl_mutex); 1159a2505b79SJan Kara return 0; 1160a2505b79SJan Kara } 1161a2505b79SJan Kara lo->lo_state = Lo_rundown; 11627ccd0791SJan Kara mutex_unlock(&loop_ctl_mutex); 1163a2505b79SJan Kara 11640da03cabSJan Kara return __loop_clr_fd(lo, false); 1165a2505b79SJan Kara } 1166a2505b79SJan Kara 11671da177e4SLinus Torvalds static int 11681da177e4SLinus Torvalds loop_set_status(struct loop_device *lo, const struct loop_info64 *info) 11691da177e4SLinus Torvalds { 11701da177e4SLinus Torvalds int err; 11711da177e4SLinus Torvalds struct loop_func_table *xfer; 1172e4849737SEric W. Biederman kuid_t uid = current_uid(); 117385b0a54aSJan Kara struct block_device *bdev; 117485b0a54aSJan Kara bool partscan = false; 11751da177e4SLinus Torvalds 1176c28445faSJan Kara err = mutex_lock_killable(&loop_ctl_mutex); 1177550df5fdSJan Kara if (err) 1178550df5fdSJan Kara return err; 1179b0fafa81SDavid Howells if (lo->lo_encrypt_key_size && 1180e4849737SEric W. Biederman !uid_eq(lo->lo_key_owner, uid) && 1181550df5fdSJan Kara !capable(CAP_SYS_ADMIN)) { 1182550df5fdSJan Kara err = -EPERM; 1183550df5fdSJan Kara goto out_unlock; 1184550df5fdSJan Kara } 1185550df5fdSJan Kara if (lo->lo_state != Lo_bound) { 1186550df5fdSJan Kara err = -ENXIO; 1187550df5fdSJan Kara goto out_unlock; 1188550df5fdSJan Kara } 1189550df5fdSJan Kara if ((unsigned int) info->lo_encrypt_key_size > LO_KEY_SIZE) { 1190550df5fdSJan Kara err = -EINVAL; 1191550df5fdSJan Kara goto out_unlock; 1192550df5fdSJan Kara } 11931da177e4SLinus Torvalds 11945db470e2SJaegeuk Kim if (lo->lo_offset != info->lo_offset || 11955db470e2SJaegeuk Kim lo->lo_sizelimit != info->lo_sizelimit) { 11965db470e2SJaegeuk Kim sync_blockdev(lo->lo_device); 11975db470e2SJaegeuk Kim kill_bdev(lo->lo_device); 11985db470e2SJaegeuk Kim } 11995db470e2SJaegeuk Kim 1200ecdd0959SMing Lei /* I/O need to be drained during transfer transition */ 1201ecdd0959SMing Lei blk_mq_freeze_queue(lo->lo_queue); 1202ecdd0959SMing Lei 12031da177e4SLinus Torvalds err = loop_release_xfer(lo); 12041da177e4SLinus Torvalds if (err) 1205550df5fdSJan Kara goto out_unfreeze; 12061da177e4SLinus Torvalds 12071da177e4SLinus Torvalds if (info->lo_encrypt_type) { 12081da177e4SLinus Torvalds unsigned int type = info->lo_encrypt_type; 12091da177e4SLinus Torvalds 12101e047eaaSTetsuo Handa if (type >= MAX_LO_CRYPT) { 12111e047eaaSTetsuo Handa err = -EINVAL; 1212550df5fdSJan Kara goto out_unfreeze; 12131e047eaaSTetsuo Handa } 12141da177e4SLinus Torvalds xfer = xfer_funcs[type]; 12151e047eaaSTetsuo Handa if (xfer == NULL) { 12161e047eaaSTetsuo Handa err = -EINVAL; 1217550df5fdSJan Kara goto out_unfreeze; 12181e047eaaSTetsuo Handa } 12191da177e4SLinus Torvalds } else 12201da177e4SLinus Torvalds xfer = NULL; 12211da177e4SLinus Torvalds 12221da177e4SLinus Torvalds err = loop_init_xfer(lo, xfer, info); 12231da177e4SLinus Torvalds if (err) 1224550df5fdSJan Kara goto out_unfreeze; 12251da177e4SLinus Torvalds 12261da177e4SLinus Torvalds if (lo->lo_offset != info->lo_offset || 12271e6ec9eaSOmar Sandoval lo->lo_sizelimit != info->lo_sizelimit) { 12285db470e2SJaegeuk Kim /* kill_bdev should have truncated all the pages */ 12295db470e2SJaegeuk Kim if (lo->lo_device->bd_inode->i_mapping->nrpages) { 12305db470e2SJaegeuk Kim err = -EAGAIN; 12315db470e2SJaegeuk Kim pr_warn("%s: loop%d (%s) has still dirty pages (nrpages=%lu)\n", 12325db470e2SJaegeuk Kim __func__, lo->lo_number, lo->lo_file_name, 12335db470e2SJaegeuk Kim lo->lo_device->bd_inode->i_mapping->nrpages); 12345db470e2SJaegeuk Kim goto out_unfreeze; 12355db470e2SJaegeuk Kim } 12361e6ec9eaSOmar Sandoval if (figure_loop_size(lo, info->lo_offset, info->lo_sizelimit)) { 1237ecdd0959SMing Lei err = -EFBIG; 1238550df5fdSJan Kara goto out_unfreeze; 1239ecdd0959SMing Lei } 1240b040ad9cSArnd Bergmann } 1241541c742aSGuo Chao 1242dfaa2ef6SLukas Czerner loop_config_discard(lo); 12431da177e4SLinus Torvalds 12441da177e4SLinus Torvalds memcpy(lo->lo_file_name, info->lo_file_name, LO_NAME_SIZE); 12451da177e4SLinus Torvalds memcpy(lo->lo_crypt_name, info->lo_crypt_name, LO_NAME_SIZE); 12461da177e4SLinus Torvalds lo->lo_file_name[LO_NAME_SIZE-1] = 0; 12471da177e4SLinus Torvalds lo->lo_crypt_name[LO_NAME_SIZE-1] = 0; 12481da177e4SLinus Torvalds 12491da177e4SLinus Torvalds if (!xfer) 12501da177e4SLinus Torvalds xfer = &none_funcs; 12511da177e4SLinus Torvalds lo->transfer = xfer->transfer; 12521da177e4SLinus Torvalds lo->ioctl = xfer->ioctl; 12531da177e4SLinus Torvalds 125496c58655SDavid Woodhouse if ((lo->lo_flags & LO_FLAGS_AUTOCLEAR) != 125596c58655SDavid Woodhouse (info->lo_flags & LO_FLAGS_AUTOCLEAR)) 125696c58655SDavid Woodhouse lo->lo_flags ^= LO_FLAGS_AUTOCLEAR; 125796c58655SDavid Woodhouse 12581da177e4SLinus Torvalds lo->lo_encrypt_key_size = info->lo_encrypt_key_size; 12591da177e4SLinus Torvalds lo->lo_init[0] = info->lo_init[0]; 12601da177e4SLinus Torvalds lo->lo_init[1] = info->lo_init[1]; 12611da177e4SLinus Torvalds if (info->lo_encrypt_key_size) { 12621da177e4SLinus Torvalds memcpy(lo->lo_encrypt_key, info->lo_encrypt_key, 12631da177e4SLinus Torvalds info->lo_encrypt_key_size); 1264b0fafa81SDavid Howells lo->lo_key_owner = uid; 12651da177e4SLinus Torvalds } 12661da177e4SLinus Torvalds 12672e5ab5f3SMing Lei /* update dio if lo_offset or transfer is changed */ 12682e5ab5f3SMing Lei __loop_update_dio(lo, lo->use_dio); 12692e5ab5f3SMing Lei 1270550df5fdSJan Kara out_unfreeze: 1271ecdd0959SMing Lei blk_mq_unfreeze_queue(lo->lo_queue); 1272e02898b4SOmar Sandoval 1273e02898b4SOmar Sandoval if (!err && (info->lo_flags & LO_FLAGS_PARTSCAN) && 1274e02898b4SOmar Sandoval !(lo->lo_flags & LO_FLAGS_PARTSCAN)) { 1275e02898b4SOmar Sandoval lo->lo_flags |= LO_FLAGS_PARTSCAN; 1276e02898b4SOmar Sandoval lo->lo_disk->flags &= ~GENHD_FL_NO_PART_SCAN; 127785b0a54aSJan Kara bdev = lo->lo_device; 127885b0a54aSJan Kara partscan = true; 1279e02898b4SOmar Sandoval } 1280550df5fdSJan Kara out_unlock: 1281550df5fdSJan Kara mutex_unlock(&loop_ctl_mutex); 128285b0a54aSJan Kara if (partscan) 128385b0a54aSJan Kara loop_reread_partitions(lo, bdev); 1284e02898b4SOmar Sandoval 1285ecdd0959SMing Lei return err; 12861da177e4SLinus Torvalds } 12871da177e4SLinus Torvalds 12881da177e4SLinus Torvalds static int 12891da177e4SLinus Torvalds loop_get_status(struct loop_device *lo, struct loop_info64 *info) 12901da177e4SLinus Torvalds { 1291b1ab5fa3STetsuo Handa struct path path; 12921da177e4SLinus Torvalds struct kstat stat; 12932d1d4c1eSOmar Sandoval int ret; 12941da177e4SLinus Torvalds 1295c28445faSJan Kara ret = mutex_lock_killable(&loop_ctl_mutex); 12964a5ce9baSJan Kara if (ret) 12974a5ce9baSJan Kara return ret; 12982d1d4c1eSOmar Sandoval if (lo->lo_state != Lo_bound) { 1299310ca162STetsuo Handa mutex_unlock(&loop_ctl_mutex); 13001da177e4SLinus Torvalds return -ENXIO; 13012d1d4c1eSOmar Sandoval } 13022d1d4c1eSOmar Sandoval 13031da177e4SLinus Torvalds memset(info, 0, sizeof(*info)); 13041da177e4SLinus Torvalds info->lo_number = lo->lo_number; 13051da177e4SLinus Torvalds info->lo_offset = lo->lo_offset; 13061da177e4SLinus Torvalds info->lo_sizelimit = lo->lo_sizelimit; 13071da177e4SLinus Torvalds info->lo_flags = lo->lo_flags; 13081da177e4SLinus Torvalds memcpy(info->lo_file_name, lo->lo_file_name, LO_NAME_SIZE); 13091da177e4SLinus Torvalds memcpy(info->lo_crypt_name, lo->lo_crypt_name, LO_NAME_SIZE); 13101da177e4SLinus Torvalds info->lo_encrypt_type = 13111da177e4SLinus Torvalds lo->lo_encryption ? lo->lo_encryption->number : 0; 13121da177e4SLinus Torvalds if (lo->lo_encrypt_key_size && capable(CAP_SYS_ADMIN)) { 13131da177e4SLinus Torvalds info->lo_encrypt_key_size = lo->lo_encrypt_key_size; 13141da177e4SLinus Torvalds memcpy(info->lo_encrypt_key, lo->lo_encrypt_key, 13151da177e4SLinus Torvalds lo->lo_encrypt_key_size); 13161da177e4SLinus Torvalds } 13172d1d4c1eSOmar Sandoval 1318310ca162STetsuo Handa /* Drop loop_ctl_mutex while we call into the filesystem. */ 1319b1ab5fa3STetsuo Handa path = lo->lo_backing_file->f_path; 1320b1ab5fa3STetsuo Handa path_get(&path); 1321310ca162STetsuo Handa mutex_unlock(&loop_ctl_mutex); 1322b1ab5fa3STetsuo Handa ret = vfs_getattr(&path, &stat, STATX_INO, AT_STATX_SYNC_AS_STAT); 13232d1d4c1eSOmar Sandoval if (!ret) { 13242d1d4c1eSOmar Sandoval info->lo_device = huge_encode_dev(stat.dev); 13252d1d4c1eSOmar Sandoval info->lo_inode = stat.ino; 13262d1d4c1eSOmar Sandoval info->lo_rdevice = huge_encode_dev(stat.rdev); 13272d1d4c1eSOmar Sandoval } 1328b1ab5fa3STetsuo Handa path_put(&path); 13292d1d4c1eSOmar Sandoval return ret; 13301da177e4SLinus Torvalds } 13311da177e4SLinus Torvalds 13321da177e4SLinus Torvalds static void 13331da177e4SLinus Torvalds loop_info64_from_old(const struct loop_info *info, struct loop_info64 *info64) 13341da177e4SLinus Torvalds { 13351da177e4SLinus Torvalds memset(info64, 0, sizeof(*info64)); 13361da177e4SLinus Torvalds info64->lo_number = info->lo_number; 13371da177e4SLinus Torvalds info64->lo_device = info->lo_device; 13381da177e4SLinus Torvalds info64->lo_inode = info->lo_inode; 13391da177e4SLinus Torvalds info64->lo_rdevice = info->lo_rdevice; 13401da177e4SLinus Torvalds info64->lo_offset = info->lo_offset; 13411da177e4SLinus Torvalds info64->lo_sizelimit = 0; 13421da177e4SLinus Torvalds info64->lo_encrypt_type = info->lo_encrypt_type; 13431da177e4SLinus Torvalds info64->lo_encrypt_key_size = info->lo_encrypt_key_size; 13441da177e4SLinus Torvalds info64->lo_flags = info->lo_flags; 13451da177e4SLinus Torvalds info64->lo_init[0] = info->lo_init[0]; 13461da177e4SLinus Torvalds info64->lo_init[1] = info->lo_init[1]; 13471da177e4SLinus Torvalds if (info->lo_encrypt_type == LO_CRYPT_CRYPTOAPI) 13481da177e4SLinus Torvalds memcpy(info64->lo_crypt_name, info->lo_name, LO_NAME_SIZE); 13491da177e4SLinus Torvalds else 13501da177e4SLinus Torvalds memcpy(info64->lo_file_name, info->lo_name, LO_NAME_SIZE); 13511da177e4SLinus Torvalds memcpy(info64->lo_encrypt_key, info->lo_encrypt_key, LO_KEY_SIZE); 13521da177e4SLinus Torvalds } 13531da177e4SLinus Torvalds 13541da177e4SLinus Torvalds static int 13551da177e4SLinus Torvalds loop_info64_to_old(const struct loop_info64 *info64, struct loop_info *info) 13561da177e4SLinus Torvalds { 13571da177e4SLinus Torvalds memset(info, 0, sizeof(*info)); 13581da177e4SLinus Torvalds info->lo_number = info64->lo_number; 13591da177e4SLinus Torvalds info->lo_device = info64->lo_device; 13601da177e4SLinus Torvalds info->lo_inode = info64->lo_inode; 13611da177e4SLinus Torvalds info->lo_rdevice = info64->lo_rdevice; 13621da177e4SLinus Torvalds info->lo_offset = info64->lo_offset; 13631da177e4SLinus Torvalds info->lo_encrypt_type = info64->lo_encrypt_type; 13641da177e4SLinus Torvalds info->lo_encrypt_key_size = info64->lo_encrypt_key_size; 13651da177e4SLinus Torvalds info->lo_flags = info64->lo_flags; 13661da177e4SLinus Torvalds info->lo_init[0] = info64->lo_init[0]; 13671da177e4SLinus Torvalds info->lo_init[1] = info64->lo_init[1]; 13681da177e4SLinus Torvalds if (info->lo_encrypt_type == LO_CRYPT_CRYPTOAPI) 13691da177e4SLinus Torvalds memcpy(info->lo_name, info64->lo_crypt_name, LO_NAME_SIZE); 13701da177e4SLinus Torvalds else 13711da177e4SLinus Torvalds memcpy(info->lo_name, info64->lo_file_name, LO_NAME_SIZE); 13721da177e4SLinus Torvalds memcpy(info->lo_encrypt_key, info64->lo_encrypt_key, LO_KEY_SIZE); 13731da177e4SLinus Torvalds 13741da177e4SLinus Torvalds /* error in case values were truncated */ 13751da177e4SLinus Torvalds if (info->lo_device != info64->lo_device || 13761da177e4SLinus Torvalds info->lo_rdevice != info64->lo_rdevice || 13771da177e4SLinus Torvalds info->lo_inode != info64->lo_inode || 13781da177e4SLinus Torvalds info->lo_offset != info64->lo_offset) 13791da177e4SLinus Torvalds return -EOVERFLOW; 13801da177e4SLinus Torvalds 13811da177e4SLinus Torvalds return 0; 13821da177e4SLinus Torvalds } 13831da177e4SLinus Torvalds 13841da177e4SLinus Torvalds static int 13851da177e4SLinus Torvalds loop_set_status_old(struct loop_device *lo, const struct loop_info __user *arg) 13861da177e4SLinus Torvalds { 13871da177e4SLinus Torvalds struct loop_info info; 13881da177e4SLinus Torvalds struct loop_info64 info64; 13891da177e4SLinus Torvalds 13901da177e4SLinus Torvalds if (copy_from_user(&info, arg, sizeof (struct loop_info))) 13911da177e4SLinus Torvalds return -EFAULT; 13921da177e4SLinus Torvalds loop_info64_from_old(&info, &info64); 13931da177e4SLinus Torvalds return loop_set_status(lo, &info64); 13941da177e4SLinus Torvalds } 13951da177e4SLinus Torvalds 13961da177e4SLinus Torvalds static int 13971da177e4SLinus Torvalds loop_set_status64(struct loop_device *lo, const struct loop_info64 __user *arg) 13981da177e4SLinus Torvalds { 13991da177e4SLinus Torvalds struct loop_info64 info64; 14001da177e4SLinus Torvalds 14011da177e4SLinus Torvalds if (copy_from_user(&info64, arg, sizeof (struct loop_info64))) 14021da177e4SLinus Torvalds return -EFAULT; 14031da177e4SLinus Torvalds return loop_set_status(lo, &info64); 14041da177e4SLinus Torvalds } 14051da177e4SLinus Torvalds 14061da177e4SLinus Torvalds static int 14071da177e4SLinus Torvalds loop_get_status_old(struct loop_device *lo, struct loop_info __user *arg) { 14081da177e4SLinus Torvalds struct loop_info info; 14091da177e4SLinus Torvalds struct loop_info64 info64; 1410bdac616dSOmar Sandoval int err; 14111da177e4SLinus Torvalds 14124a5ce9baSJan Kara if (!arg) 1413bdac616dSOmar Sandoval return -EINVAL; 14141da177e4SLinus Torvalds err = loop_get_status(lo, &info64); 14151da177e4SLinus Torvalds if (!err) 14161da177e4SLinus Torvalds err = loop_info64_to_old(&info64, &info); 14171da177e4SLinus Torvalds if (!err && copy_to_user(arg, &info, sizeof(info))) 14181da177e4SLinus Torvalds err = -EFAULT; 14191da177e4SLinus Torvalds 14201da177e4SLinus Torvalds return err; 14211da177e4SLinus Torvalds } 14221da177e4SLinus Torvalds 14231da177e4SLinus Torvalds static int 14241da177e4SLinus Torvalds loop_get_status64(struct loop_device *lo, struct loop_info64 __user *arg) { 14251da177e4SLinus Torvalds struct loop_info64 info64; 1426bdac616dSOmar Sandoval int err; 14271da177e4SLinus Torvalds 14284a5ce9baSJan Kara if (!arg) 1429bdac616dSOmar Sandoval return -EINVAL; 14301da177e4SLinus Torvalds err = loop_get_status(lo, &info64); 14311da177e4SLinus Torvalds if (!err && copy_to_user(arg, &info64, sizeof(info64))) 14321da177e4SLinus Torvalds err = -EFAULT; 14331da177e4SLinus Torvalds 14341da177e4SLinus Torvalds return err; 14351da177e4SLinus Torvalds } 14361da177e4SLinus Torvalds 143751001b7dSHannes Reinecke static int loop_set_capacity(struct loop_device *lo) 143853d66608SJ. R. Okajima { 143953d66608SJ. R. Okajima if (unlikely(lo->lo_state != Lo_bound)) 14407b0576a3SGuo Chao return -ENXIO; 144153d66608SJ. R. Okajima 14421e6ec9eaSOmar Sandoval return figure_loop_size(lo, lo->lo_offset, lo->lo_sizelimit); 144353d66608SJ. R. Okajima } 144453d66608SJ. R. Okajima 1445ab1cb278SMing Lei static int loop_set_dio(struct loop_device *lo, unsigned long arg) 1446ab1cb278SMing Lei { 1447ab1cb278SMing Lei int error = -ENXIO; 1448ab1cb278SMing Lei if (lo->lo_state != Lo_bound) 1449ab1cb278SMing Lei goto out; 1450ab1cb278SMing Lei 1451ab1cb278SMing Lei __loop_update_dio(lo, !!arg); 1452ab1cb278SMing Lei if (lo->use_dio == !!arg) 1453ab1cb278SMing Lei return 0; 1454ab1cb278SMing Lei error = -EINVAL; 1455ab1cb278SMing Lei out: 1456ab1cb278SMing Lei return error; 1457ab1cb278SMing Lei } 1458ab1cb278SMing Lei 145989e4fdecSOmar Sandoval static int loop_set_block_size(struct loop_device *lo, unsigned long arg) 146089e4fdecSOmar Sandoval { 14615db470e2SJaegeuk Kim int err = 0; 14625db470e2SJaegeuk Kim 146389e4fdecSOmar Sandoval if (lo->lo_state != Lo_bound) 146489e4fdecSOmar Sandoval return -ENXIO; 146589e4fdecSOmar Sandoval 146689e4fdecSOmar Sandoval if (arg < 512 || arg > PAGE_SIZE || !is_power_of_2(arg)) 146789e4fdecSOmar Sandoval return -EINVAL; 146889e4fdecSOmar Sandoval 14695db470e2SJaegeuk Kim if (lo->lo_queue->limits.logical_block_size != arg) { 14705db470e2SJaegeuk Kim sync_blockdev(lo->lo_device); 14715db470e2SJaegeuk Kim kill_bdev(lo->lo_device); 14725db470e2SJaegeuk Kim } 14735db470e2SJaegeuk Kim 147489e4fdecSOmar Sandoval blk_mq_freeze_queue(lo->lo_queue); 147589e4fdecSOmar Sandoval 14765db470e2SJaegeuk Kim /* kill_bdev should have truncated all the pages */ 14775db470e2SJaegeuk Kim if (lo->lo_queue->limits.logical_block_size != arg && 14785db470e2SJaegeuk Kim lo->lo_device->bd_inode->i_mapping->nrpages) { 14795db470e2SJaegeuk Kim err = -EAGAIN; 14805db470e2SJaegeuk Kim pr_warn("%s: loop%d (%s) has still dirty pages (nrpages=%lu)\n", 14815db470e2SJaegeuk Kim __func__, lo->lo_number, lo->lo_file_name, 14825db470e2SJaegeuk Kim lo->lo_device->bd_inode->i_mapping->nrpages); 14835db470e2SJaegeuk Kim goto out_unfreeze; 14845db470e2SJaegeuk Kim } 14855db470e2SJaegeuk Kim 148689e4fdecSOmar Sandoval blk_queue_logical_block_size(lo->lo_queue, arg); 1487bf093753SOmar Sandoval blk_queue_physical_block_size(lo->lo_queue, arg); 1488bf093753SOmar Sandoval blk_queue_io_min(lo->lo_queue, arg); 148989e4fdecSOmar Sandoval loop_update_dio(lo); 14905db470e2SJaegeuk Kim out_unfreeze: 149189e4fdecSOmar Sandoval blk_mq_unfreeze_queue(lo->lo_queue); 149289e4fdecSOmar Sandoval 14935db470e2SJaegeuk Kim return err; 149489e4fdecSOmar Sandoval } 149589e4fdecSOmar Sandoval 1496a1316544SJan Kara static int lo_simple_ioctl(struct loop_device *lo, unsigned int cmd, 1497a1316544SJan Kara unsigned long arg) 14981da177e4SLinus Torvalds { 14991da177e4SLinus Torvalds int err; 15001da177e4SLinus Torvalds 1501c28445faSJan Kara err = mutex_lock_killable(&loop_ctl_mutex); 15023148ffbdSOmar Sandoval if (err) 1503a1316544SJan Kara return err; 15041da177e4SLinus Torvalds switch (cmd) { 150553d66608SJ. R. Okajima case LOOP_SET_CAPACITY: 150651001b7dSHannes Reinecke err = loop_set_capacity(lo); 150753d66608SJ. R. Okajima break; 1508ab1cb278SMing Lei case LOOP_SET_DIRECT_IO: 1509ab1cb278SMing Lei err = loop_set_dio(lo, arg); 1510ab1cb278SMing Lei break; 151189e4fdecSOmar Sandoval case LOOP_SET_BLOCK_SIZE: 151289e4fdecSOmar Sandoval err = loop_set_block_size(lo, arg); 151389e4fdecSOmar Sandoval break; 15141da177e4SLinus Torvalds default: 15151da177e4SLinus Torvalds err = lo->ioctl ? lo->ioctl(lo, cmd, arg) : -EINVAL; 15161da177e4SLinus Torvalds } 1517310ca162STetsuo Handa mutex_unlock(&loop_ctl_mutex); 1518a1316544SJan Kara return err; 1519a1316544SJan Kara } 1520f028f3b2SNikanth Karthikesan 1521a1316544SJan Kara static int lo_ioctl(struct block_device *bdev, fmode_t mode, 1522a1316544SJan Kara unsigned int cmd, unsigned long arg) 1523a1316544SJan Kara { 1524a1316544SJan Kara struct loop_device *lo = bdev->bd_disk->private_data; 1525a1316544SJan Kara int err; 1526a1316544SJan Kara 1527a1316544SJan Kara switch (cmd) { 1528a1316544SJan Kara case LOOP_SET_FD: 1529757ecf40SJan Kara return loop_set_fd(lo, mode, bdev, arg); 1530a1316544SJan Kara case LOOP_CHANGE_FD: 1531c3710770SJan Kara return loop_change_fd(lo, bdev, arg); 1532a1316544SJan Kara case LOOP_CLR_FD: 15337ccd0791SJan Kara return loop_clr_fd(lo); 1534a1316544SJan Kara case LOOP_SET_STATUS: 1535a1316544SJan Kara err = -EPERM; 1536a1316544SJan Kara if ((mode & FMODE_WRITE) || capable(CAP_SYS_ADMIN)) { 1537a1316544SJan Kara err = loop_set_status_old(lo, 1538a1316544SJan Kara (struct loop_info __user *)arg); 1539a1316544SJan Kara } 1540a1316544SJan Kara break; 1541a1316544SJan Kara case LOOP_GET_STATUS: 15424a5ce9baSJan Kara return loop_get_status_old(lo, (struct loop_info __user *) arg); 1543a1316544SJan Kara case LOOP_SET_STATUS64: 1544a1316544SJan Kara err = -EPERM; 1545a1316544SJan Kara if ((mode & FMODE_WRITE) || capable(CAP_SYS_ADMIN)) { 1546a1316544SJan Kara err = loop_set_status64(lo, 1547a1316544SJan Kara (struct loop_info64 __user *) arg); 1548a1316544SJan Kara } 1549a1316544SJan Kara break; 1550a1316544SJan Kara case LOOP_GET_STATUS64: 15514a5ce9baSJan Kara return loop_get_status64(lo, (struct loop_info64 __user *) arg); 1552a1316544SJan Kara case LOOP_SET_CAPACITY: 1553a1316544SJan Kara case LOOP_SET_DIRECT_IO: 1554a1316544SJan Kara case LOOP_SET_BLOCK_SIZE: 1555a1316544SJan Kara if (!(mode & FMODE_WRITE) && !capable(CAP_SYS_ADMIN)) 1556a1316544SJan Kara return -EPERM; 1557a1316544SJan Kara /* Fall through */ 1558a1316544SJan Kara default: 1559a1316544SJan Kara err = lo_simple_ioctl(lo, cmd, arg); 1560a1316544SJan Kara break; 1561a1316544SJan Kara } 1562a1316544SJan Kara 15631da177e4SLinus Torvalds return err; 15641da177e4SLinus Torvalds } 15651da177e4SLinus Torvalds 1566863d5b82SDavid Howells #ifdef CONFIG_COMPAT 1567863d5b82SDavid Howells struct compat_loop_info { 1568863d5b82SDavid Howells compat_int_t lo_number; /* ioctl r/o */ 1569863d5b82SDavid Howells compat_dev_t lo_device; /* ioctl r/o */ 1570863d5b82SDavid Howells compat_ulong_t lo_inode; /* ioctl r/o */ 1571863d5b82SDavid Howells compat_dev_t lo_rdevice; /* ioctl r/o */ 1572863d5b82SDavid Howells compat_int_t lo_offset; 1573863d5b82SDavid Howells compat_int_t lo_encrypt_type; 1574863d5b82SDavid Howells compat_int_t lo_encrypt_key_size; /* ioctl w/o */ 1575863d5b82SDavid Howells compat_int_t lo_flags; /* ioctl r/o */ 1576863d5b82SDavid Howells char lo_name[LO_NAME_SIZE]; 1577863d5b82SDavid Howells unsigned char lo_encrypt_key[LO_KEY_SIZE]; /* ioctl w/o */ 1578863d5b82SDavid Howells compat_ulong_t lo_init[2]; 1579863d5b82SDavid Howells char reserved[4]; 1580863d5b82SDavid Howells }; 1581863d5b82SDavid Howells 1582863d5b82SDavid Howells /* 1583863d5b82SDavid Howells * Transfer 32-bit compatibility structure in userspace to 64-bit loop info 1584863d5b82SDavid Howells * - noinlined to reduce stack space usage in main part of driver 1585863d5b82SDavid Howells */ 1586863d5b82SDavid Howells static noinline int 1587ba674cfcSAl Viro loop_info64_from_compat(const struct compat_loop_info __user *arg, 1588863d5b82SDavid Howells struct loop_info64 *info64) 1589863d5b82SDavid Howells { 1590863d5b82SDavid Howells struct compat_loop_info info; 1591863d5b82SDavid Howells 1592863d5b82SDavid Howells if (copy_from_user(&info, arg, sizeof(info))) 1593863d5b82SDavid Howells return -EFAULT; 1594863d5b82SDavid Howells 1595863d5b82SDavid Howells memset(info64, 0, sizeof(*info64)); 1596863d5b82SDavid Howells info64->lo_number = info.lo_number; 1597863d5b82SDavid Howells info64->lo_device = info.lo_device; 1598863d5b82SDavid Howells info64->lo_inode = info.lo_inode; 1599863d5b82SDavid Howells info64->lo_rdevice = info.lo_rdevice; 1600863d5b82SDavid Howells info64->lo_offset = info.lo_offset; 1601863d5b82SDavid Howells info64->lo_sizelimit = 0; 1602863d5b82SDavid Howells info64->lo_encrypt_type = info.lo_encrypt_type; 1603863d5b82SDavid Howells info64->lo_encrypt_key_size = info.lo_encrypt_key_size; 1604863d5b82SDavid Howells info64->lo_flags = info.lo_flags; 1605863d5b82SDavid Howells info64->lo_init[0] = info.lo_init[0]; 1606863d5b82SDavid Howells info64->lo_init[1] = info.lo_init[1]; 1607863d5b82SDavid Howells if (info.lo_encrypt_type == LO_CRYPT_CRYPTOAPI) 1608863d5b82SDavid Howells memcpy(info64->lo_crypt_name, info.lo_name, LO_NAME_SIZE); 1609863d5b82SDavid Howells else 1610863d5b82SDavid Howells memcpy(info64->lo_file_name, info.lo_name, LO_NAME_SIZE); 1611863d5b82SDavid Howells memcpy(info64->lo_encrypt_key, info.lo_encrypt_key, LO_KEY_SIZE); 1612863d5b82SDavid Howells return 0; 1613863d5b82SDavid Howells } 1614863d5b82SDavid Howells 1615863d5b82SDavid Howells /* 1616863d5b82SDavid Howells * Transfer 64-bit loop info to 32-bit compatibility structure in userspace 1617863d5b82SDavid Howells * - noinlined to reduce stack space usage in main part of driver 1618863d5b82SDavid Howells */ 1619863d5b82SDavid Howells static noinline int 1620863d5b82SDavid Howells loop_info64_to_compat(const struct loop_info64 *info64, 1621863d5b82SDavid Howells struct compat_loop_info __user *arg) 1622863d5b82SDavid Howells { 1623863d5b82SDavid Howells struct compat_loop_info info; 1624863d5b82SDavid Howells 1625863d5b82SDavid Howells memset(&info, 0, sizeof(info)); 1626863d5b82SDavid Howells info.lo_number = info64->lo_number; 1627863d5b82SDavid Howells info.lo_device = info64->lo_device; 1628863d5b82SDavid Howells info.lo_inode = info64->lo_inode; 1629863d5b82SDavid Howells info.lo_rdevice = info64->lo_rdevice; 1630863d5b82SDavid Howells info.lo_offset = info64->lo_offset; 1631863d5b82SDavid Howells info.lo_encrypt_type = info64->lo_encrypt_type; 1632863d5b82SDavid Howells info.lo_encrypt_key_size = info64->lo_encrypt_key_size; 1633863d5b82SDavid Howells info.lo_flags = info64->lo_flags; 1634863d5b82SDavid Howells info.lo_init[0] = info64->lo_init[0]; 1635863d5b82SDavid Howells info.lo_init[1] = info64->lo_init[1]; 1636863d5b82SDavid Howells if (info.lo_encrypt_type == LO_CRYPT_CRYPTOAPI) 1637863d5b82SDavid Howells memcpy(info.lo_name, info64->lo_crypt_name, LO_NAME_SIZE); 1638863d5b82SDavid Howells else 1639863d5b82SDavid Howells memcpy(info.lo_name, info64->lo_file_name, LO_NAME_SIZE); 1640863d5b82SDavid Howells memcpy(info.lo_encrypt_key, info64->lo_encrypt_key, LO_KEY_SIZE); 1641863d5b82SDavid Howells 1642863d5b82SDavid Howells /* error in case values were truncated */ 1643863d5b82SDavid Howells if (info.lo_device != info64->lo_device || 1644863d5b82SDavid Howells info.lo_rdevice != info64->lo_rdevice || 1645863d5b82SDavid Howells info.lo_inode != info64->lo_inode || 1646863d5b82SDavid Howells info.lo_offset != info64->lo_offset || 1647863d5b82SDavid Howells info.lo_init[0] != info64->lo_init[0] || 1648863d5b82SDavid Howells info.lo_init[1] != info64->lo_init[1]) 1649863d5b82SDavid Howells return -EOVERFLOW; 1650863d5b82SDavid Howells 1651863d5b82SDavid Howells if (copy_to_user(arg, &info, sizeof(info))) 1652863d5b82SDavid Howells return -EFAULT; 1653863d5b82SDavid Howells return 0; 1654863d5b82SDavid Howells } 1655863d5b82SDavid Howells 1656863d5b82SDavid Howells static int 1657863d5b82SDavid Howells loop_set_status_compat(struct loop_device *lo, 1658863d5b82SDavid Howells const struct compat_loop_info __user *arg) 1659863d5b82SDavid Howells { 1660863d5b82SDavid Howells struct loop_info64 info64; 1661863d5b82SDavid Howells int ret; 1662863d5b82SDavid Howells 1663863d5b82SDavid Howells ret = loop_info64_from_compat(arg, &info64); 1664863d5b82SDavid Howells if (ret < 0) 1665863d5b82SDavid Howells return ret; 1666863d5b82SDavid Howells return loop_set_status(lo, &info64); 1667863d5b82SDavid Howells } 1668863d5b82SDavid Howells 1669863d5b82SDavid Howells static int 1670863d5b82SDavid Howells loop_get_status_compat(struct loop_device *lo, 1671863d5b82SDavid Howells struct compat_loop_info __user *arg) 1672863d5b82SDavid Howells { 1673863d5b82SDavid Howells struct loop_info64 info64; 1674bdac616dSOmar Sandoval int err; 1675863d5b82SDavid Howells 16764a5ce9baSJan Kara if (!arg) 1677bdac616dSOmar Sandoval return -EINVAL; 1678863d5b82SDavid Howells err = loop_get_status(lo, &info64); 1679863d5b82SDavid Howells if (!err) 1680863d5b82SDavid Howells err = loop_info64_to_compat(&info64, arg); 1681863d5b82SDavid Howells return err; 1682863d5b82SDavid Howells } 1683863d5b82SDavid Howells 1684bb214884SAl Viro static int lo_compat_ioctl(struct block_device *bdev, fmode_t mode, 1685bb214884SAl Viro unsigned int cmd, unsigned long arg) 1686863d5b82SDavid Howells { 1687bb214884SAl Viro struct loop_device *lo = bdev->bd_disk->private_data; 1688863d5b82SDavid Howells int err; 1689863d5b82SDavid Howells 1690863d5b82SDavid Howells switch(cmd) { 1691863d5b82SDavid Howells case LOOP_SET_STATUS: 16923148ffbdSOmar Sandoval err = loop_set_status_compat(lo, 16933148ffbdSOmar Sandoval (const struct compat_loop_info __user *)arg); 1694863d5b82SDavid Howells break; 1695863d5b82SDavid Howells case LOOP_GET_STATUS: 16963148ffbdSOmar Sandoval err = loop_get_status_compat(lo, 16973148ffbdSOmar Sandoval (struct compat_loop_info __user *)arg); 1698863d5b82SDavid Howells break; 169953d66608SJ. R. Okajima case LOOP_SET_CAPACITY: 1700863d5b82SDavid Howells case LOOP_CLR_FD: 1701863d5b82SDavid Howells case LOOP_GET_STATUS64: 1702863d5b82SDavid Howells case LOOP_SET_STATUS64: 1703863d5b82SDavid Howells arg = (unsigned long) compat_ptr(arg); 1704d893ff86SGustavo A. R. Silva /* fall through */ 1705863d5b82SDavid Howells case LOOP_SET_FD: 1706863d5b82SDavid Howells case LOOP_CHANGE_FD: 17079fea4b39SEvan Green case LOOP_SET_BLOCK_SIZE: 1708bb214884SAl Viro err = lo_ioctl(bdev, mode, cmd, arg); 1709863d5b82SDavid Howells break; 1710863d5b82SDavid Howells default: 1711863d5b82SDavid Howells err = -ENOIOCTLCMD; 1712863d5b82SDavid Howells break; 1713863d5b82SDavid Howells } 1714863d5b82SDavid Howells return err; 1715863d5b82SDavid Howells } 1716863d5b82SDavid Howells #endif 1717863d5b82SDavid Howells 1718bb214884SAl Viro static int lo_open(struct block_device *bdev, fmode_t mode) 17191da177e4SLinus Torvalds { 1720770fe30aSKay Sievers struct loop_device *lo; 17210a42e99bSJan Kara int err; 1722770fe30aSKay Sievers 17230a42e99bSJan Kara err = mutex_lock_killable(&loop_ctl_mutex); 17240a42e99bSJan Kara if (err) 17250a42e99bSJan Kara return err; 1726770fe30aSKay Sievers lo = bdev->bd_disk->private_data; 1727770fe30aSKay Sievers if (!lo) { 1728770fe30aSKay Sievers err = -ENXIO; 1729770fe30aSKay Sievers goto out; 1730770fe30aSKay Sievers } 17311da177e4SLinus Torvalds 1732f8933667SMing Lei atomic_inc(&lo->lo_refcnt); 1733770fe30aSKay Sievers out: 17340a42e99bSJan Kara mutex_unlock(&loop_ctl_mutex); 1735770fe30aSKay Sievers return err; 17361da177e4SLinus Torvalds } 17371da177e4SLinus Torvalds 1738967d1dc1SJan Kara static void lo_release(struct gendisk *disk, fmode_t mode) 17391da177e4SLinus Torvalds { 1740967d1dc1SJan Kara struct loop_device *lo; 17411da177e4SLinus Torvalds 17420a42e99bSJan Kara mutex_lock(&loop_ctl_mutex); 1743967d1dc1SJan Kara lo = disk->private_data; 1744f8933667SMing Lei if (atomic_dec_return(&lo->lo_refcnt)) 17450a42e99bSJan Kara goto out_unlock; 1746f8933667SMing Lei 174714f27939SMilan Broz if (lo->lo_flags & LO_FLAGS_AUTOCLEAR) { 1748a2505b79SJan Kara if (lo->lo_state != Lo_bound) 1749a2505b79SJan Kara goto out_unlock; 1750a2505b79SJan Kara lo->lo_state = Lo_rundown; 17517ccd0791SJan Kara mutex_unlock(&loop_ctl_mutex); 175214f27939SMilan Broz /* 175314f27939SMilan Broz * In autoclear mode, stop the loop thread 175414f27939SMilan Broz * and remove configuration after last close. 175514f27939SMilan Broz */ 17560da03cabSJan Kara __loop_clr_fd(lo, true); 17570a42e99bSJan Kara return; 175843cade80SOmar Sandoval } else if (lo->lo_state == Lo_bound) { 175914f27939SMilan Broz /* 176014f27939SMilan Broz * Otherwise keep thread (if running) and config, 176114f27939SMilan Broz * but flush possible ongoing bios in thread. 176214f27939SMilan Broz */ 176343cade80SOmar Sandoval blk_mq_freeze_queue(lo->lo_queue); 176443cade80SOmar Sandoval blk_mq_unfreeze_queue(lo->lo_queue); 176514f27939SMilan Broz } 176696c58655SDavid Woodhouse 17670a42e99bSJan Kara out_unlock: 1768310ca162STetsuo Handa mutex_unlock(&loop_ctl_mutex); 1769ae665016SLinus Torvalds } 1770ae665016SLinus Torvalds 177183d5cde4SAlexey Dobriyan static const struct block_device_operations lo_fops = { 17721da177e4SLinus Torvalds .owner = THIS_MODULE, 1773bb214884SAl Viro .open = lo_open, 1774bb214884SAl Viro .release = lo_release, 1775bb214884SAl Viro .ioctl = lo_ioctl, 1776863d5b82SDavid Howells #ifdef CONFIG_COMPAT 1777bb214884SAl Viro .compat_ioctl = lo_compat_ioctl, 1778863d5b82SDavid Howells #endif 17791da177e4SLinus Torvalds }; 17801da177e4SLinus Torvalds 17811da177e4SLinus Torvalds /* 17821da177e4SLinus Torvalds * And now the modules code and kernel interface. 17831da177e4SLinus Torvalds */ 178473285082SKen Chen static int max_loop; 17855657a819SJoe Perches module_param(max_loop, int, 0444); 1786a47653fcSKen Chen MODULE_PARM_DESC(max_loop, "Maximum number of loop devices"); 17875657a819SJoe Perches module_param(max_part, int, 0444); 1788476a4813SLaurent Vivier MODULE_PARM_DESC(max_part, "Maximum number of partitions per loop device"); 17891da177e4SLinus Torvalds MODULE_LICENSE("GPL"); 17901da177e4SLinus Torvalds MODULE_ALIAS_BLOCKDEV_MAJOR(LOOP_MAJOR); 17911da177e4SLinus Torvalds 17921da177e4SLinus Torvalds int loop_register_transfer(struct loop_func_table *funcs) 17931da177e4SLinus Torvalds { 17941da177e4SLinus Torvalds unsigned int n = funcs->number; 17951da177e4SLinus Torvalds 17961da177e4SLinus Torvalds if (n >= MAX_LO_CRYPT || xfer_funcs[n]) 17971da177e4SLinus Torvalds return -EINVAL; 17981da177e4SLinus Torvalds xfer_funcs[n] = funcs; 17991da177e4SLinus Torvalds return 0; 18001da177e4SLinus Torvalds } 18011da177e4SLinus Torvalds 180234dd82afSKay Sievers static int unregister_transfer_cb(int id, void *ptr, void *data) 180334dd82afSKay Sievers { 180434dd82afSKay Sievers struct loop_device *lo = ptr; 180534dd82afSKay Sievers struct loop_func_table *xfer = data; 180634dd82afSKay Sievers 1807310ca162STetsuo Handa mutex_lock(&loop_ctl_mutex); 180834dd82afSKay Sievers if (lo->lo_encryption == xfer) 180934dd82afSKay Sievers loop_release_xfer(lo); 1810310ca162STetsuo Handa mutex_unlock(&loop_ctl_mutex); 181134dd82afSKay Sievers return 0; 181234dd82afSKay Sievers } 181334dd82afSKay Sievers 18141da177e4SLinus Torvalds int loop_unregister_transfer(int number) 18151da177e4SLinus Torvalds { 18161da177e4SLinus Torvalds unsigned int n = number; 18171da177e4SLinus Torvalds struct loop_func_table *xfer; 18181da177e4SLinus Torvalds 18191da177e4SLinus Torvalds if (n == 0 || n >= MAX_LO_CRYPT || (xfer = xfer_funcs[n]) == NULL) 18201da177e4SLinus Torvalds return -EINVAL; 18211da177e4SLinus Torvalds 18221da177e4SLinus Torvalds xfer_funcs[n] = NULL; 182334dd82afSKay Sievers idr_for_each(&loop_index_idr, &unregister_transfer_cb, xfer); 18241da177e4SLinus Torvalds return 0; 18251da177e4SLinus Torvalds } 18261da177e4SLinus Torvalds 18271da177e4SLinus Torvalds EXPORT_SYMBOL(loop_register_transfer); 18281da177e4SLinus Torvalds EXPORT_SYMBOL(loop_unregister_transfer); 18291da177e4SLinus Torvalds 1830fc17b653SChristoph Hellwig static blk_status_t loop_queue_rq(struct blk_mq_hw_ctx *hctx, 1831b5dd2f60SMing Lei const struct blk_mq_queue_data *bd) 1832b5dd2f60SMing Lei { 18331894e916SJens Axboe struct request *rq = bd->rq; 18341894e916SJens Axboe struct loop_cmd *cmd = blk_mq_rq_to_pdu(rq); 18351894e916SJens Axboe struct loop_device *lo = rq->q->queuedata; 1836b5dd2f60SMing Lei 18371894e916SJens Axboe blk_mq_start_request(rq); 1838b5dd2f60SMing Lei 1839f4aa4c7bSMing Lei if (lo->lo_state != Lo_bound) 1840fc17b653SChristoph Hellwig return BLK_STS_IOERR; 1841f4aa4c7bSMing Lei 18421894e916SJens Axboe switch (req_op(rq)) { 1843f0225cacSChristoph Hellwig case REQ_OP_FLUSH: 1844f0225cacSChristoph Hellwig case REQ_OP_DISCARD: 184519372e27SChristoph Hellwig case REQ_OP_WRITE_ZEROES: 1846bc07c10aSMing Lei cmd->use_aio = false; 1847f0225cacSChristoph Hellwig break; 1848f0225cacSChristoph Hellwig default: 1849f0225cacSChristoph Hellwig cmd->use_aio = lo->use_dio; 1850f0225cacSChristoph Hellwig break; 1851f0225cacSChristoph Hellwig } 1852bc07c10aSMing Lei 1853d4478e92SShaohua Li /* always use the first bio's css */ 18540b508bc9SShaohua Li #ifdef CONFIG_BLK_CGROUP 1855db6638d7SDennis Zhou if (cmd->use_aio && rq->bio && rq->bio->bi_blkg) { 1856db6638d7SDennis Zhou cmd->css = &bio_blkcg(rq->bio)->css; 1857d4478e92SShaohua Li css_get(cmd->css); 1858d4478e92SShaohua Li } else 1859d4478e92SShaohua Li #endif 1860d4478e92SShaohua Li cmd->css = NULL; 18613989144fSPetr Mladek kthread_queue_work(&lo->worker, &cmd->work); 1862b5dd2f60SMing Lei 1863fc17b653SChristoph Hellwig return BLK_STS_OK; 1864b5dd2f60SMing Lei } 1865b5dd2f60SMing Lei 1866b5dd2f60SMing Lei static void loop_handle_cmd(struct loop_cmd *cmd) 1867b5dd2f60SMing Lei { 18681894e916SJens Axboe struct request *rq = blk_mq_rq_from_pdu(cmd); 18691894e916SJens Axboe const bool write = op_is_write(req_op(rq)); 18701894e916SJens Axboe struct loop_device *lo = rq->q->queuedata; 1871f4829a9bSChristoph Hellwig int ret = 0; 1872b5dd2f60SMing Lei 1873f4829a9bSChristoph Hellwig if (write && (lo->lo_flags & LO_FLAGS_READ_ONLY)) { 1874f4829a9bSChristoph Hellwig ret = -EIO; 1875b5dd2f60SMing Lei goto failed; 1876f4829a9bSChristoph Hellwig } 1877b5dd2f60SMing Lei 18781894e916SJens Axboe ret = do_req_filebacked(lo, rq); 1879b5dd2f60SMing Lei failed: 1880bc07c10aSMing Lei /* complete non-aio request */ 1881fe2cb290SChristoph Hellwig if (!cmd->use_aio || ret) { 1882fe2cb290SChristoph Hellwig cmd->ret = ret ? -EIO : 0; 18831894e916SJens Axboe blk_mq_complete_request(rq); 1884fe2cb290SChristoph Hellwig } 1885b5dd2f60SMing Lei } 1886b5dd2f60SMing Lei 1887e03a3d7aSMing Lei static void loop_queue_work(struct kthread_work *work) 1888b5dd2f60SMing Lei { 1889b5dd2f60SMing Lei struct loop_cmd *cmd = 1890e03a3d7aSMing Lei container_of(work, struct loop_cmd, work); 1891b5dd2f60SMing Lei 1892b5dd2f60SMing Lei loop_handle_cmd(cmd); 1893b5dd2f60SMing Lei } 1894b5dd2f60SMing Lei 1895d6296d39SChristoph Hellwig static int loop_init_request(struct blk_mq_tag_set *set, struct request *rq, 1896d6296d39SChristoph Hellwig unsigned int hctx_idx, unsigned int numa_node) 1897b5dd2f60SMing Lei { 1898b5dd2f60SMing Lei struct loop_cmd *cmd = blk_mq_rq_to_pdu(rq); 1899b5dd2f60SMing Lei 19003989144fSPetr Mladek kthread_init_work(&cmd->work, loop_queue_work); 1901b5dd2f60SMing Lei return 0; 1902b5dd2f60SMing Lei } 1903b5dd2f60SMing Lei 1904f363b089SEric Biggers static const struct blk_mq_ops loop_mq_ops = { 1905b5dd2f60SMing Lei .queue_rq = loop_queue_rq, 1906b5dd2f60SMing Lei .init_request = loop_init_request, 1907fe2cb290SChristoph Hellwig .complete = lo_complete_rq, 1908b5dd2f60SMing Lei }; 1909b5dd2f60SMing Lei 191034dd82afSKay Sievers static int loop_add(struct loop_device **l, int i) 19111da177e4SLinus Torvalds { 191273285082SKen Chen struct loop_device *lo; 191373285082SKen Chen struct gendisk *disk; 191434dd82afSKay Sievers int err; 19151da177e4SLinus Torvalds 191634dd82afSKay Sievers err = -ENOMEM; 191768d740d7SSilva Paulo lo = kzalloc(sizeof(*lo), GFP_KERNEL); 191868d740d7SSilva Paulo if (!lo) 191973285082SKen Chen goto out; 192034dd82afSKay Sievers 1921ef7e7c82SMikulas Patocka lo->lo_state = Lo_unbound; 1922ef7e7c82SMikulas Patocka 1923c718aa65STejun Heo /* allocate id, if @id >= 0, we're requesting that specific id */ 192434dd82afSKay Sievers if (i >= 0) { 1925c718aa65STejun Heo err = idr_alloc(&loop_index_idr, lo, i, i + 1, GFP_KERNEL); 1926c718aa65STejun Heo if (err == -ENOSPC) 192734dd82afSKay Sievers err = -EEXIST; 192834dd82afSKay Sievers } else { 1929c718aa65STejun Heo err = idr_alloc(&loop_index_idr, lo, 0, 0, GFP_KERNEL); 193034dd82afSKay Sievers } 193134dd82afSKay Sievers if (err < 0) 193234dd82afSKay Sievers goto out_free_dev; 1933c718aa65STejun Heo i = err; 19341da177e4SLinus Torvalds 1935183cfb57SWei Yongjun err = -ENOMEM; 1936b5dd2f60SMing Lei lo->tag_set.ops = &loop_mq_ops; 1937b5dd2f60SMing Lei lo->tag_set.nr_hw_queues = 1; 1938b5dd2f60SMing Lei lo->tag_set.queue_depth = 128; 1939b5dd2f60SMing Lei lo->tag_set.numa_node = NUMA_NO_NODE; 1940b5dd2f60SMing Lei lo->tag_set.cmd_size = sizeof(struct loop_cmd); 194156d18f62SMing Lei lo->tag_set.flags = BLK_MQ_F_SHOULD_MERGE; 1942b5dd2f60SMing Lei lo->tag_set.driver_data = lo; 1943b5dd2f60SMing Lei 1944b5dd2f60SMing Lei err = blk_mq_alloc_tag_set(&lo->tag_set); 1945b5dd2f60SMing Lei if (err) 19463ec981e3SMikulas Patocka goto out_free_idr; 194773285082SKen Chen 1948b5dd2f60SMing Lei lo->lo_queue = blk_mq_init_queue(&lo->tag_set); 194938a3499fSChengguang Xu if (IS_ERR(lo->lo_queue)) { 1950b5dd2f60SMing Lei err = PTR_ERR(lo->lo_queue); 1951b5dd2f60SMing Lei goto out_cleanup_tags; 1952b5dd2f60SMing Lei } 1953ef7e7c82SMikulas Patocka lo->lo_queue->queuedata = lo; 1954ef7e7c82SMikulas Patocka 195554bb0adeSShaohua Li blk_queue_max_hw_sectors(lo->lo_queue, BLK_DEF_MAX_SECTORS); 195640326d8aSShaohua Li 19575b5e20f4SMing Lei /* 195840326d8aSShaohua Li * By default, we do buffer IO, so it doesn't make sense to enable 195940326d8aSShaohua Li * merge because the I/O submitted to backing file is handled page by 196040326d8aSShaohua Li * page. For directio mode, merge does help to dispatch bigger request 196140326d8aSShaohua Li * to underlayer disk. We will enable merge once directio is enabled. 19625b5e20f4SMing Lei */ 19638b904b5bSBart Van Assche blk_queue_flag_set(QUEUE_FLAG_NOMERGES, lo->lo_queue); 19645b5e20f4SMing Lei 19657a649737SMinfei Huang err = -ENOMEM; 1966476a4813SLaurent Vivier disk = lo->lo_disk = alloc_disk(1 << part_shift); 196773285082SKen Chen if (!disk) 196873285082SKen Chen goto out_free_queue; 196973285082SKen Chen 1970e03c8dd1SKay Sievers /* 1971e03c8dd1SKay Sievers * Disable partition scanning by default. The in-kernel partition 1972e03c8dd1SKay Sievers * scanning can be requested individually per-device during its 1973e03c8dd1SKay Sievers * setup. Userspace can always add and remove partitions from all 1974e03c8dd1SKay Sievers * devices. The needed partition minors are allocated from the 1975e03c8dd1SKay Sievers * extended minor space, the main loop device numbers will continue 1976e03c8dd1SKay Sievers * to match the loop minors, regardless of the number of partitions 1977e03c8dd1SKay Sievers * used. 1978e03c8dd1SKay Sievers * 1979e03c8dd1SKay Sievers * If max_part is given, partition scanning is globally enabled for 1980e03c8dd1SKay Sievers * all loop devices. The minors for the main loop devices will be 1981e03c8dd1SKay Sievers * multiples of max_part. 1982e03c8dd1SKay Sievers * 1983e03c8dd1SKay Sievers * Note: Global-for-all-devices, set-only-at-init, read-only module 1984e03c8dd1SKay Sievers * parameteters like 'max_loop' and 'max_part' make things needlessly 1985e03c8dd1SKay Sievers * complicated, are too static, inflexible and may surprise 1986e03c8dd1SKay Sievers * userspace tools. Parameters like this in general should be avoided. 1987e03c8dd1SKay Sievers */ 1988e03c8dd1SKay Sievers if (!part_shift) 1989e03c8dd1SKay Sievers disk->flags |= GENHD_FL_NO_PART_SCAN; 1990e03c8dd1SKay Sievers disk->flags |= GENHD_FL_EXT_DEVT; 1991f8933667SMing Lei atomic_set(&lo->lo_refcnt, 0); 19921da177e4SLinus Torvalds lo->lo_number = i; 19931da177e4SLinus Torvalds spin_lock_init(&lo->lo_lock); 19941da177e4SLinus Torvalds disk->major = LOOP_MAJOR; 1995476a4813SLaurent Vivier disk->first_minor = i << part_shift; 19961da177e4SLinus Torvalds disk->fops = &lo_fops; 19971da177e4SLinus Torvalds disk->private_data = lo; 19981da177e4SLinus Torvalds disk->queue = lo->lo_queue; 199973285082SKen Chen sprintf(disk->disk_name, "loop%d", i); 200034dd82afSKay Sievers add_disk(disk); 200134dd82afSKay Sievers *l = lo; 200234dd82afSKay Sievers return lo->lo_number; 200373285082SKen Chen 200473285082SKen Chen out_free_queue: 200573285082SKen Chen blk_cleanup_queue(lo->lo_queue); 2006b5dd2f60SMing Lei out_cleanup_tags: 2007b5dd2f60SMing Lei blk_mq_free_tag_set(&lo->tag_set); 20083ec981e3SMikulas Patocka out_free_idr: 20093ec981e3SMikulas Patocka idr_remove(&loop_index_idr, i); 201073285082SKen Chen out_free_dev: 201173285082SKen Chen kfree(lo); 201273285082SKen Chen out: 201334dd82afSKay Sievers return err; 20141da177e4SLinus Torvalds } 20151da177e4SLinus Torvalds 201634dd82afSKay Sievers static void loop_remove(struct loop_device *lo) 201773285082SKen Chen { 20186cd18e71SNeilBrown del_gendisk(lo->lo_disk); 20190fa8ebddSBart Van Assche blk_cleanup_queue(lo->lo_queue); 2020b5dd2f60SMing Lei blk_mq_free_tag_set(&lo->tag_set); 202173285082SKen Chen put_disk(lo->lo_disk); 202273285082SKen Chen kfree(lo); 202373285082SKen Chen } 202473285082SKen Chen 2025770fe30aSKay Sievers static int find_free_cb(int id, void *ptr, void *data) 2026770fe30aSKay Sievers { 2027770fe30aSKay Sievers struct loop_device *lo = ptr; 2028770fe30aSKay Sievers struct loop_device **l = data; 2029770fe30aSKay Sievers 2030770fe30aSKay Sievers if (lo->lo_state == Lo_unbound) { 2031770fe30aSKay Sievers *l = lo; 2032770fe30aSKay Sievers return 1; 2033770fe30aSKay Sievers } 2034770fe30aSKay Sievers return 0; 2035770fe30aSKay Sievers } 2036770fe30aSKay Sievers 203734dd82afSKay Sievers static int loop_lookup(struct loop_device **l, int i) 2038a47653fcSKen Chen { 2039a47653fcSKen Chen struct loop_device *lo; 204034dd82afSKay Sievers int ret = -ENODEV; 2041a47653fcSKen Chen 2042770fe30aSKay Sievers if (i < 0) { 2043770fe30aSKay Sievers int err; 2044770fe30aSKay Sievers 2045770fe30aSKay Sievers err = idr_for_each(&loop_index_idr, &find_free_cb, &lo); 2046770fe30aSKay Sievers if (err == 1) { 2047770fe30aSKay Sievers *l = lo; 2048770fe30aSKay Sievers ret = lo->lo_number; 2049770fe30aSKay Sievers } 2050770fe30aSKay Sievers goto out; 2051a47653fcSKen Chen } 2052a47653fcSKen Chen 2053770fe30aSKay Sievers /* lookup and return a specific i */ 205434dd82afSKay Sievers lo = idr_find(&loop_index_idr, i); 2055a47653fcSKen Chen if (lo) { 205634dd82afSKay Sievers *l = lo; 205734dd82afSKay Sievers ret = lo->lo_number; 2058a47653fcSKen Chen } 2059770fe30aSKay Sievers out: 206034dd82afSKay Sievers return ret; 2061a47653fcSKen Chen } 2062a47653fcSKen Chen 206373285082SKen Chen static struct kobject *loop_probe(dev_t dev, int *part, void *data) 206473285082SKen Chen { 2065705962ccSAl Viro struct loop_device *lo; 206607002e99SAl Viro struct kobject *kobj; 206734dd82afSKay Sievers int err; 206873285082SKen Chen 20690a42e99bSJan Kara mutex_lock(&loop_ctl_mutex); 207034dd82afSKay Sievers err = loop_lookup(&lo, MINOR(dev) >> part_shift); 207134dd82afSKay Sievers if (err < 0) 207234dd82afSKay Sievers err = loop_add(&lo, MINOR(dev) >> part_shift); 207334dd82afSKay Sievers if (err < 0) 2074a207f593SMikulas Patocka kobj = NULL; 207534dd82afSKay Sievers else 20763079c22eSJan Kara kobj = get_disk_and_module(lo->lo_disk); 20770a42e99bSJan Kara mutex_unlock(&loop_ctl_mutex); 207873285082SKen Chen 207973285082SKen Chen *part = 0; 208007002e99SAl Viro return kobj; 208173285082SKen Chen } 208273285082SKen Chen 2083770fe30aSKay Sievers static long loop_control_ioctl(struct file *file, unsigned int cmd, 2084770fe30aSKay Sievers unsigned long parm) 2085770fe30aSKay Sievers { 2086770fe30aSKay Sievers struct loop_device *lo; 20870a42e99bSJan Kara int ret; 2088770fe30aSKay Sievers 20890a42e99bSJan Kara ret = mutex_lock_killable(&loop_ctl_mutex); 20900a42e99bSJan Kara if (ret) 20910a42e99bSJan Kara return ret; 20920a42e99bSJan Kara 20930a42e99bSJan Kara ret = -ENOSYS; 2094770fe30aSKay Sievers switch (cmd) { 2095770fe30aSKay Sievers case LOOP_CTL_ADD: 2096770fe30aSKay Sievers ret = loop_lookup(&lo, parm); 2097770fe30aSKay Sievers if (ret >= 0) { 2098770fe30aSKay Sievers ret = -EEXIST; 2099770fe30aSKay Sievers break; 2100770fe30aSKay Sievers } 2101770fe30aSKay Sievers ret = loop_add(&lo, parm); 2102770fe30aSKay Sievers break; 2103770fe30aSKay Sievers case LOOP_CTL_REMOVE: 2104770fe30aSKay Sievers ret = loop_lookup(&lo, parm); 2105770fe30aSKay Sievers if (ret < 0) 2106770fe30aSKay Sievers break; 2107770fe30aSKay Sievers if (lo->lo_state != Lo_unbound) { 2108770fe30aSKay Sievers ret = -EBUSY; 2109770fe30aSKay Sievers break; 2110770fe30aSKay Sievers } 2111f8933667SMing Lei if (atomic_read(&lo->lo_refcnt) > 0) { 2112770fe30aSKay Sievers ret = -EBUSY; 2113770fe30aSKay Sievers break; 2114770fe30aSKay Sievers } 2115770fe30aSKay Sievers lo->lo_disk->private_data = NULL; 2116770fe30aSKay Sievers idr_remove(&loop_index_idr, lo->lo_number); 2117770fe30aSKay Sievers loop_remove(lo); 2118770fe30aSKay Sievers break; 2119770fe30aSKay Sievers case LOOP_CTL_GET_FREE: 2120770fe30aSKay Sievers ret = loop_lookup(&lo, -1); 2121770fe30aSKay Sievers if (ret >= 0) 2122770fe30aSKay Sievers break; 2123770fe30aSKay Sievers ret = loop_add(&lo, -1); 2124770fe30aSKay Sievers } 21250a42e99bSJan Kara mutex_unlock(&loop_ctl_mutex); 2126770fe30aSKay Sievers 2127770fe30aSKay Sievers return ret; 2128770fe30aSKay Sievers } 2129770fe30aSKay Sievers 2130770fe30aSKay Sievers static const struct file_operations loop_ctl_fops = { 2131770fe30aSKay Sievers .open = nonseekable_open, 2132770fe30aSKay Sievers .unlocked_ioctl = loop_control_ioctl, 2133770fe30aSKay Sievers .compat_ioctl = loop_control_ioctl, 2134770fe30aSKay Sievers .owner = THIS_MODULE, 2135770fe30aSKay Sievers .llseek = noop_llseek, 2136770fe30aSKay Sievers }; 2137770fe30aSKay Sievers 2138770fe30aSKay Sievers static struct miscdevice loop_misc = { 2139770fe30aSKay Sievers .minor = LOOP_CTRL_MINOR, 2140770fe30aSKay Sievers .name = "loop-control", 2141770fe30aSKay Sievers .fops = &loop_ctl_fops, 2142770fe30aSKay Sievers }; 2143770fe30aSKay Sievers 2144770fe30aSKay Sievers MODULE_ALIAS_MISCDEV(LOOP_CTRL_MINOR); 2145770fe30aSKay Sievers MODULE_ALIAS("devname:loop-control"); 2146770fe30aSKay Sievers 214773285082SKen Chen static int __init loop_init(void) 214873285082SKen Chen { 2149a47653fcSKen Chen int i, nr; 2150a47653fcSKen Chen unsigned long range; 215134dd82afSKay Sievers struct loop_device *lo; 2152770fe30aSKay Sievers int err; 2153a47653fcSKen Chen 2154476a4813SLaurent Vivier part_shift = 0; 2155ac04fee0SNamhyung Kim if (max_part > 0) { 2156476a4813SLaurent Vivier part_shift = fls(max_part); 2157476a4813SLaurent Vivier 2158ac04fee0SNamhyung Kim /* 2159ac04fee0SNamhyung Kim * Adjust max_part according to part_shift as it is exported 2160ac04fee0SNamhyung Kim * to user space so that user can decide correct minor number 2161ac04fee0SNamhyung Kim * if [s]he want to create more devices. 2162ac04fee0SNamhyung Kim * 2163ac04fee0SNamhyung Kim * Note that -1 is required because partition 0 is reserved 2164ac04fee0SNamhyung Kim * for the whole disk. 2165ac04fee0SNamhyung Kim */ 2166ac04fee0SNamhyung Kim max_part = (1UL << part_shift) - 1; 2167ac04fee0SNamhyung Kim } 2168ac04fee0SNamhyung Kim 2169b1a66504SGuo Chao if ((1UL << part_shift) > DISK_MAX_PARTS) { 2170b1a66504SGuo Chao err = -EINVAL; 2171a8c1d064SAnton Volkov goto err_out; 2172b1a66504SGuo Chao } 217378f4bb36SNamhyung Kim 2174b1a66504SGuo Chao if (max_loop > 1UL << (MINORBITS - part_shift)) { 2175b1a66504SGuo Chao err = -EINVAL; 2176a8c1d064SAnton Volkov goto err_out; 2177b1a66504SGuo Chao } 217873285082SKen Chen 2179d134b00bSKay Sievers /* 2180d134b00bSKay Sievers * If max_loop is specified, create that many devices upfront. 2181d134b00bSKay Sievers * This also becomes a hard limit. If max_loop is not specified, 2182d134b00bSKay Sievers * create CONFIG_BLK_DEV_LOOP_MIN_COUNT loop devices at module 2183d134b00bSKay Sievers * init time. Loop devices can be requested on-demand with the 2184d134b00bSKay Sievers * /dev/loop-control interface, or be instantiated by accessing 2185d134b00bSKay Sievers * a 'dead' device node. 2186d134b00bSKay Sievers */ 218773285082SKen Chen if (max_loop) { 2188a47653fcSKen Chen nr = max_loop; 2189a1c15c59SNamhyung Kim range = max_loop << part_shift; 2190a47653fcSKen Chen } else { 2191d134b00bSKay Sievers nr = CONFIG_BLK_DEV_LOOP_MIN_COUNT; 2192a1c15c59SNamhyung Kim range = 1UL << MINORBITS; 219373285082SKen Chen } 2194a47653fcSKen Chen 2195a8c1d064SAnton Volkov err = misc_register(&loop_misc); 2196a8c1d064SAnton Volkov if (err < 0) 2197a8c1d064SAnton Volkov goto err_out; 2198a8c1d064SAnton Volkov 2199a8c1d064SAnton Volkov 2200b1a66504SGuo Chao if (register_blkdev(LOOP_MAJOR, "loop")) { 2201b1a66504SGuo Chao err = -EIO; 2202b1a66504SGuo Chao goto misc_out; 2203b1a66504SGuo Chao } 2204a47653fcSKen Chen 2205a47653fcSKen Chen blk_register_region(MKDEV(LOOP_MAJOR, 0), range, 2206a47653fcSKen Chen THIS_MODULE, loop_probe, NULL, NULL); 2207a47653fcSKen Chen 2208d134b00bSKay Sievers /* pre-create number of devices given by config or max_loop */ 22090a42e99bSJan Kara mutex_lock(&loop_ctl_mutex); 221034dd82afSKay Sievers for (i = 0; i < nr; i++) 221134dd82afSKay Sievers loop_add(&lo, i); 22120a42e99bSJan Kara mutex_unlock(&loop_ctl_mutex); 221334dd82afSKay Sievers 221473285082SKen Chen printk(KERN_INFO "loop: module loaded\n"); 22151da177e4SLinus Torvalds return 0; 2216b1a66504SGuo Chao 2217b1a66504SGuo Chao misc_out: 2218b1a66504SGuo Chao misc_deregister(&loop_misc); 2219a8c1d064SAnton Volkov err_out: 2220b1a66504SGuo Chao return err; 222134dd82afSKay Sievers } 2222a47653fcSKen Chen 222334dd82afSKay Sievers static int loop_exit_cb(int id, void *ptr, void *data) 222434dd82afSKay Sievers { 222534dd82afSKay Sievers struct loop_device *lo = ptr; 2226a47653fcSKen Chen 222734dd82afSKay Sievers loop_remove(lo); 222834dd82afSKay Sievers return 0; 22291da177e4SLinus Torvalds } 22301da177e4SLinus Torvalds 223173285082SKen Chen static void __exit loop_exit(void) 22321da177e4SLinus Torvalds { 2233a47653fcSKen Chen unsigned long range; 22341da177e4SLinus Torvalds 2235a1c15c59SNamhyung Kim range = max_loop ? max_loop << part_shift : 1UL << MINORBITS; 2236a47653fcSKen Chen 223734dd82afSKay Sievers idr_for_each(&loop_index_idr, &loop_exit_cb, NULL); 223834dd82afSKay Sievers idr_destroy(&loop_index_idr); 223973285082SKen Chen 2240a47653fcSKen Chen blk_unregister_region(MKDEV(LOOP_MAJOR, 0), range); 224100d59405SAkinobu Mita unregister_blkdev(LOOP_MAJOR, "loop"); 2242770fe30aSKay Sievers 2243770fe30aSKay Sievers misc_deregister(&loop_misc); 22441da177e4SLinus Torvalds } 22451da177e4SLinus Torvalds 22461da177e4SLinus Torvalds module_init(loop_init); 22471da177e4SLinus Torvalds module_exit(loop_exit); 22481da177e4SLinus Torvalds 22491da177e4SLinus Torvalds #ifndef MODULE 22501da177e4SLinus Torvalds static int __init max_loop_setup(char *str) 22511da177e4SLinus Torvalds { 22521da177e4SLinus Torvalds max_loop = simple_strtol(str, NULL, 0); 22531da177e4SLinus Torvalds return 1; 22541da177e4SLinus Torvalds } 22551da177e4SLinus Torvalds 22561da177e4SLinus Torvalds __setup("max_loop=", max_loop_setup); 22571da177e4SLinus Torvalds #endif 2258