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> 564ee60ec1SMatthew Wilcox (Oracle) #include <linux/pagemap.h> 571da177e4SLinus Torvalds #include <linux/file.h> 581da177e4SLinus Torvalds #include <linux/stat.h> 591da177e4SLinus Torvalds #include <linux/errno.h> 601da177e4SLinus Torvalds #include <linux/major.h> 611da177e4SLinus Torvalds #include <linux/wait.h> 621da177e4SLinus Torvalds #include <linux/blkdev.h> 631da177e4SLinus Torvalds #include <linux/blkpg.h> 641da177e4SLinus Torvalds #include <linux/init.h> 651da177e4SLinus Torvalds #include <linux/swap.h> 661da177e4SLinus Torvalds #include <linux/slab.h> 67863d5b82SDavid Howells #include <linux/compat.h> 681da177e4SLinus Torvalds #include <linux/suspend.h> 6983144186SRafael J. Wysocki #include <linux/freezer.h> 702a48fc0aSArnd Bergmann #include <linux/mutex.h> 711da177e4SLinus Torvalds #include <linux/writeback.h> 721da177e4SLinus Torvalds #include <linux/completion.h> 731da177e4SLinus Torvalds #include <linux/highmem.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> 81*c74d40e8SDan Schatzberg #include <linux/sched/mm.h> 82d9a08a9eSAdam Manzanares 8383a87611SAl Viro #include "loop.h" 841da177e4SLinus Torvalds 857c0f6ba6SLinus Torvalds #include <linux/uaccess.h> 861da177e4SLinus Torvalds 8787579e9bSDan Schatzberg #define LOOP_IDLE_WORKER_TIMEOUT (60 * HZ) 8887579e9bSDan Schatzberg 8934dd82afSKay Sievers static DEFINE_IDR(loop_index_idr); 90310ca162STetsuo Handa static DEFINE_MUTEX(loop_ctl_mutex); 911da177e4SLinus Torvalds 92476a4813SLaurent Vivier static int max_part; 93476a4813SLaurent Vivier static int part_shift; 94476a4813SLaurent Vivier 951da177e4SLinus Torvalds static int transfer_xor(struct loop_device *lo, int cmd, 961da177e4SLinus Torvalds struct page *raw_page, unsigned raw_off, 971da177e4SLinus Torvalds struct page *loop_page, unsigned loop_off, 981da177e4SLinus Torvalds int size, sector_t real_block) 991da177e4SLinus Torvalds { 100cfd8005cSCong Wang char *raw_buf = kmap_atomic(raw_page) + raw_off; 101cfd8005cSCong Wang char *loop_buf = kmap_atomic(loop_page) + loop_off; 1021da177e4SLinus Torvalds char *in, *out, *key; 1031da177e4SLinus Torvalds int i, keysize; 1041da177e4SLinus Torvalds 1051da177e4SLinus Torvalds if (cmd == READ) { 1061da177e4SLinus Torvalds in = raw_buf; 1071da177e4SLinus Torvalds out = loop_buf; 1081da177e4SLinus Torvalds } else { 1091da177e4SLinus Torvalds in = loop_buf; 1101da177e4SLinus Torvalds out = raw_buf; 1111da177e4SLinus Torvalds } 1121da177e4SLinus Torvalds 1131da177e4SLinus Torvalds key = lo->lo_encrypt_key; 1141da177e4SLinus Torvalds keysize = lo->lo_encrypt_key_size; 1151da177e4SLinus Torvalds for (i = 0; i < size; i++) 1161da177e4SLinus Torvalds *out++ = *in++ ^ key[(i & 511) % keysize]; 1171da177e4SLinus Torvalds 118cfd8005cSCong Wang kunmap_atomic(loop_buf); 119cfd8005cSCong Wang kunmap_atomic(raw_buf); 1201da177e4SLinus Torvalds cond_resched(); 1211da177e4SLinus Torvalds return 0; 1221da177e4SLinus Torvalds } 1231da177e4SLinus Torvalds 1241da177e4SLinus Torvalds static int xor_init(struct loop_device *lo, const struct loop_info64 *info) 1251da177e4SLinus Torvalds { 1261da177e4SLinus Torvalds if (unlikely(info->lo_encrypt_key_size <= 0)) 1271da177e4SLinus Torvalds return -EINVAL; 1281da177e4SLinus Torvalds return 0; 1291da177e4SLinus Torvalds } 1301da177e4SLinus Torvalds 1311da177e4SLinus Torvalds static struct loop_func_table none_funcs = { 1321da177e4SLinus Torvalds .number = LO_CRYPT_NONE, 1331da177e4SLinus Torvalds }; 1341da177e4SLinus Torvalds 1351da177e4SLinus Torvalds static struct loop_func_table xor_funcs = { 1361da177e4SLinus Torvalds .number = LO_CRYPT_XOR, 1371da177e4SLinus Torvalds .transfer = transfer_xor, 1381da177e4SLinus Torvalds .init = xor_init 1391da177e4SLinus Torvalds }; 1401da177e4SLinus Torvalds 1411da177e4SLinus Torvalds /* xfer_funcs[0] is special - its release function is never called */ 1421da177e4SLinus Torvalds static struct loop_func_table *xfer_funcs[MAX_LO_CRYPT] = { 1431da177e4SLinus Torvalds &none_funcs, 1441da177e4SLinus Torvalds &xor_funcs 1451da177e4SLinus Torvalds }; 1461da177e4SLinus Torvalds 1477035b5dfSDmitry Monakhov static loff_t get_size(loff_t offset, loff_t sizelimit, struct file *file) 1481da177e4SLinus Torvalds { 149b7a1da69SGuo Chao loff_t loopsize; 1501da177e4SLinus Torvalds 1511da177e4SLinus Torvalds /* Compute loopsize in bytes */ 152b7a1da69SGuo Chao loopsize = i_size_read(file->f_mapping->host); 153b7a1da69SGuo Chao if (offset > 0) 154b7a1da69SGuo Chao loopsize -= offset; 155b7a1da69SGuo Chao /* offset is beyond i_size, weird but possible */ 1567035b5dfSDmitry Monakhov if (loopsize < 0) 1577035b5dfSDmitry Monakhov return 0; 1581da177e4SLinus Torvalds 1597035b5dfSDmitry Monakhov if (sizelimit > 0 && sizelimit < loopsize) 1607035b5dfSDmitry Monakhov loopsize = sizelimit; 1611da177e4SLinus Torvalds /* 1621da177e4SLinus Torvalds * Unfortunately, if we want to do I/O on the device, 1631da177e4SLinus Torvalds * the number of 512-byte sectors has to fit into a sector_t. 1641da177e4SLinus Torvalds */ 1651da177e4SLinus Torvalds return loopsize >> 9; 1661da177e4SLinus Torvalds } 1671da177e4SLinus Torvalds 1687035b5dfSDmitry Monakhov static loff_t get_loop_size(struct loop_device *lo, struct file *file) 1691da177e4SLinus Torvalds { 1707035b5dfSDmitry Monakhov return get_size(lo->lo_offset, lo->lo_sizelimit, file); 1717035b5dfSDmitry Monakhov } 1727035b5dfSDmitry Monakhov 1732e5ab5f3SMing Lei static void __loop_update_dio(struct loop_device *lo, bool dio) 1742e5ab5f3SMing Lei { 1752e5ab5f3SMing Lei struct file *file = lo->lo_backing_file; 1762e5ab5f3SMing Lei struct address_space *mapping = file->f_mapping; 1772e5ab5f3SMing Lei struct inode *inode = mapping->host; 1782e5ab5f3SMing Lei unsigned short sb_bsize = 0; 1792e5ab5f3SMing Lei unsigned dio_align = 0; 1802e5ab5f3SMing Lei bool use_dio; 1812e5ab5f3SMing Lei 1822e5ab5f3SMing Lei if (inode->i_sb->s_bdev) { 1832e5ab5f3SMing Lei sb_bsize = bdev_logical_block_size(inode->i_sb->s_bdev); 1842e5ab5f3SMing Lei dio_align = sb_bsize - 1; 1852e5ab5f3SMing Lei } 1862e5ab5f3SMing Lei 1872e5ab5f3SMing Lei /* 1882e5ab5f3SMing Lei * We support direct I/O only if lo_offset is aligned with the 1892e5ab5f3SMing Lei * logical I/O size of backing device, and the logical block 1902e5ab5f3SMing Lei * size of loop is bigger than the backing device's and the loop 1912e5ab5f3SMing Lei * needn't transform transfer. 1922e5ab5f3SMing Lei * 1932e5ab5f3SMing Lei * TODO: the above condition may be loosed in the future, and 1942e5ab5f3SMing Lei * direct I/O may be switched runtime at that time because most 19589d790abSMasahiro Yamada * of requests in sane applications should be PAGE_SIZE aligned 1962e5ab5f3SMing Lei */ 1972e5ab5f3SMing Lei if (dio) { 1982e5ab5f3SMing Lei if (queue_logical_block_size(lo->lo_queue) >= sb_bsize && 1992e5ab5f3SMing Lei !(lo->lo_offset & dio_align) && 2002e5ab5f3SMing Lei mapping->a_ops->direct_IO && 2012e5ab5f3SMing Lei !lo->transfer) 2022e5ab5f3SMing Lei use_dio = true; 2032e5ab5f3SMing Lei else 2042e5ab5f3SMing Lei use_dio = false; 2052e5ab5f3SMing Lei } else { 2062e5ab5f3SMing Lei use_dio = false; 2072e5ab5f3SMing Lei } 2082e5ab5f3SMing Lei 2092e5ab5f3SMing Lei if (lo->use_dio == use_dio) 2102e5ab5f3SMing Lei return; 2112e5ab5f3SMing Lei 2122e5ab5f3SMing Lei /* flush dirty pages before changing direct IO */ 2132e5ab5f3SMing Lei vfs_fsync(file, 0); 2142e5ab5f3SMing Lei 2152e5ab5f3SMing Lei /* 2162e5ab5f3SMing Lei * The flag of LO_FLAGS_DIRECT_IO is handled similarly with 2172e5ab5f3SMing Lei * LO_FLAGS_READ_ONLY, both are set from kernel, and losetup 2182e5ab5f3SMing Lei * will get updated by ioctl(LOOP_GET_STATUS) 2192e5ab5f3SMing Lei */ 2200fbcf579SMartijn Coenen if (lo->lo_state == Lo_bound) 2212e5ab5f3SMing Lei blk_mq_freeze_queue(lo->lo_queue); 2222e5ab5f3SMing Lei lo->use_dio = use_dio; 22340326d8aSShaohua Li if (use_dio) { 2248b904b5bSBart Van Assche blk_queue_flag_clear(QUEUE_FLAG_NOMERGES, lo->lo_queue); 2252e5ab5f3SMing Lei lo->lo_flags |= LO_FLAGS_DIRECT_IO; 22640326d8aSShaohua Li } else { 2278b904b5bSBart Van Assche blk_queue_flag_set(QUEUE_FLAG_NOMERGES, lo->lo_queue); 2282e5ab5f3SMing Lei lo->lo_flags &= ~LO_FLAGS_DIRECT_IO; 22940326d8aSShaohua Li } 2300fbcf579SMartijn Coenen if (lo->lo_state == Lo_bound) 2312e5ab5f3SMing Lei blk_mq_unfreeze_queue(lo->lo_queue); 2322e5ab5f3SMing Lei } 2332e5ab5f3SMing Lei 2345795b6f5SMartijn Coenen /** 2353448914eSMartijn Coenen * loop_validate_block_size() - validates the passed in block size 2363448914eSMartijn Coenen * @bsize: size to validate 2373448914eSMartijn Coenen */ 2387035b5dfSDmitry Monakhov static int 2393448914eSMartijn Coenen loop_validate_block_size(unsigned short bsize) 2407035b5dfSDmitry Monakhov { 2413448914eSMartijn Coenen if (bsize < 512 || bsize > PAGE_SIZE || !is_power_of_2(bsize)) 2423448914eSMartijn Coenen return -EINVAL; 2433448914eSMartijn Coenen 2443448914eSMartijn Coenen return 0; 2453448914eSMartijn Coenen } 2463448914eSMartijn Coenen 2473448914eSMartijn Coenen /** 2485795b6f5SMartijn Coenen * loop_set_size() - sets device size and notifies userspace 2495795b6f5SMartijn Coenen * @lo: struct loop_device to set the size for 2505795b6f5SMartijn Coenen * @size: new size of the loop device 2515795b6f5SMartijn Coenen * 2525795b6f5SMartijn Coenen * Callers must validate that the size passed into this function fits into 2535795b6f5SMartijn Coenen * a sector_t, eg using loop_validate_size() 2545795b6f5SMartijn Coenen */ 2555795b6f5SMartijn Coenen static void loop_set_size(struct loop_device *lo, loff_t size) 2565795b6f5SMartijn Coenen { 257449f4ec9SChristoph Hellwig if (!set_capacity_and_notify(lo->lo_disk, size)) 2583b4f85d0SChristoph Hellwig kobject_uevent(&disk_to_dev(lo->lo_disk)->kobj, KOBJ_CHANGE); 2591da177e4SLinus Torvalds } 2601da177e4SLinus Torvalds 2611da177e4SLinus Torvalds static inline int 2621da177e4SLinus Torvalds lo_do_transfer(struct loop_device *lo, int cmd, 2631da177e4SLinus Torvalds struct page *rpage, unsigned roffs, 2641da177e4SLinus Torvalds struct page *lpage, unsigned loffs, 2651da177e4SLinus Torvalds int size, sector_t rblock) 2661da177e4SLinus Torvalds { 267aa4d8616SChristoph Hellwig int ret; 268aa4d8616SChristoph Hellwig 269aa4d8616SChristoph Hellwig ret = lo->transfer(lo, cmd, rpage, roffs, lpage, loffs, size, rblock); 270aa4d8616SChristoph Hellwig if (likely(!ret)) 2711da177e4SLinus Torvalds return 0; 2721da177e4SLinus Torvalds 273aa4d8616SChristoph Hellwig printk_ratelimited(KERN_ERR 274aa4d8616SChristoph Hellwig "loop: Transfer error at byte offset %llu, length %i.\n", 275aa4d8616SChristoph Hellwig (unsigned long long)rblock << 9, size); 276aa4d8616SChristoph Hellwig return ret; 2771da177e4SLinus Torvalds } 2781da177e4SLinus Torvalds 279aa4d8616SChristoph Hellwig static int lo_write_bvec(struct file *file, struct bio_vec *bvec, loff_t *ppos) 2801da177e4SLinus Torvalds { 281aa4d8616SChristoph Hellwig struct iov_iter i; 2821da177e4SLinus Torvalds ssize_t bw; 283283e7e5dSAl Viro 284b6207430SChristoph Hellwig iov_iter_bvec(&i, WRITE, bvec, 1, bvec->bv_len); 2851da177e4SLinus Torvalds 28603d95eb2SAl Viro file_start_write(file); 287abbb6589SChristoph Hellwig bw = vfs_iter_write(file, &i, ppos, 0); 28803d95eb2SAl Viro file_end_write(file); 289aa4d8616SChristoph Hellwig 290aa4d8616SChristoph Hellwig if (likely(bw == bvec->bv_len)) 2911da177e4SLinus Torvalds return 0; 292aa4d8616SChristoph Hellwig 293aa4d8616SChristoph Hellwig printk_ratelimited(KERN_ERR 294aa4d8616SChristoph Hellwig "loop: Write error at byte offset %llu, length %i.\n", 295aa4d8616SChristoph Hellwig (unsigned long long)*ppos, bvec->bv_len); 2961da177e4SLinus Torvalds if (bw >= 0) 2971da177e4SLinus Torvalds bw = -EIO; 2981da177e4SLinus Torvalds return bw; 2991da177e4SLinus Torvalds } 3001da177e4SLinus Torvalds 301aa4d8616SChristoph Hellwig static int lo_write_simple(struct loop_device *lo, struct request *rq, 302aa4d8616SChristoph Hellwig loff_t pos) 3031da177e4SLinus Torvalds { 304aa4d8616SChristoph Hellwig struct bio_vec bvec; 305aa4d8616SChristoph Hellwig struct req_iterator iter; 306aa4d8616SChristoph Hellwig int ret = 0; 307aa4d8616SChristoph Hellwig 308aa4d8616SChristoph Hellwig rq_for_each_segment(bvec, rq, iter) { 309aa4d8616SChristoph Hellwig ret = lo_write_bvec(lo->lo_backing_file, &bvec, &pos); 310aa4d8616SChristoph Hellwig if (ret < 0) 311aa4d8616SChristoph Hellwig break; 3121da177e4SLinus Torvalds cond_resched(); 3131da177e4SLinus Torvalds } 3141da177e4SLinus Torvalds 315aa4d8616SChristoph Hellwig return ret; 316aa4d8616SChristoph Hellwig } 317aa4d8616SChristoph Hellwig 318aa4d8616SChristoph Hellwig /* 319456be148SChristoph Hellwig * This is the slow, transforming version that needs to double buffer the 320456be148SChristoph Hellwig * data as it cannot do the transformations in place without having direct 321456be148SChristoph Hellwig * access to the destination pages of the backing file. 3221da177e4SLinus Torvalds */ 323aa4d8616SChristoph Hellwig static int lo_write_transfer(struct loop_device *lo, struct request *rq, 324aa4d8616SChristoph Hellwig loff_t pos) 3251da177e4SLinus Torvalds { 326aa4d8616SChristoph Hellwig struct bio_vec bvec, b; 32730112013SMing Lei struct req_iterator iter; 328aa4d8616SChristoph Hellwig struct page *page; 3297988613bSKent Overstreet int ret = 0; 3301da177e4SLinus Torvalds 331aa4d8616SChristoph Hellwig page = alloc_page(GFP_NOIO); 3321da177e4SLinus Torvalds if (unlikely(!page)) 333aa4d8616SChristoph Hellwig return -ENOMEM; 334456be148SChristoph Hellwig 33530112013SMing Lei rq_for_each_segment(bvec, rq, iter) { 336aa4d8616SChristoph Hellwig ret = lo_do_transfer(lo, WRITE, page, 0, bvec.bv_page, 337aa4d8616SChristoph Hellwig bvec.bv_offset, bvec.bv_len, pos >> 9); 338aa4d8616SChristoph Hellwig if (unlikely(ret)) 339aa4d8616SChristoph Hellwig break; 340aa4d8616SChristoph Hellwig 341aa4d8616SChristoph Hellwig b.bv_page = page; 342aa4d8616SChristoph Hellwig b.bv_offset = 0; 343aa4d8616SChristoph Hellwig b.bv_len = bvec.bv_len; 344aa4d8616SChristoph Hellwig ret = lo_write_bvec(lo->lo_backing_file, &b, &pos); 3451da177e4SLinus Torvalds if (ret < 0) 3461da177e4SLinus Torvalds break; 3471da177e4SLinus Torvalds } 348aa4d8616SChristoph Hellwig 3491da177e4SLinus Torvalds __free_page(page); 3501da177e4SLinus Torvalds return ret; 3511da177e4SLinus Torvalds } 3521da177e4SLinus Torvalds 353aa4d8616SChristoph Hellwig static int lo_read_simple(struct loop_device *lo, struct request *rq, 354aa4d8616SChristoph Hellwig loff_t pos) 3551da177e4SLinus Torvalds { 3567988613bSKent Overstreet struct bio_vec bvec; 35730112013SMing Lei struct req_iterator iter; 358aa4d8616SChristoph Hellwig struct iov_iter i; 359aa4d8616SChristoph Hellwig ssize_t len; 3601da177e4SLinus Torvalds 36130112013SMing Lei rq_for_each_segment(bvec, rq, iter) { 362b6207430SChristoph Hellwig iov_iter_bvec(&i, READ, &bvec, 1, bvec.bv_len); 36318e9710eSChristoph Hellwig len = vfs_iter_read(lo->lo_backing_file, &i, &pos, 0); 364aa4d8616SChristoph Hellwig if (len < 0) 365aa4d8616SChristoph Hellwig return len; 366306df071SDave Young 367aa4d8616SChristoph Hellwig flush_dcache_page(bvec.bv_page); 368aa4d8616SChristoph Hellwig 369aa4d8616SChristoph Hellwig if (len != bvec.bv_len) { 37030112013SMing Lei struct bio *bio; 37130112013SMing Lei 37230112013SMing Lei __rq_for_each_bio(bio, rq) 373306df071SDave Young zero_fill_bio(bio); 3741da177e4SLinus Torvalds break; 375306df071SDave Young } 376aa4d8616SChristoph Hellwig cond_resched(); 3771da177e4SLinus Torvalds } 378aa4d8616SChristoph Hellwig 379306df071SDave Young return 0; 3801da177e4SLinus Torvalds } 3811da177e4SLinus Torvalds 382aa4d8616SChristoph Hellwig static int lo_read_transfer(struct loop_device *lo, struct request *rq, 383aa4d8616SChristoph Hellwig loff_t pos) 384aa4d8616SChristoph Hellwig { 385aa4d8616SChristoph Hellwig struct bio_vec bvec, b; 386aa4d8616SChristoph Hellwig struct req_iterator iter; 387aa4d8616SChristoph Hellwig struct iov_iter i; 388aa4d8616SChristoph Hellwig struct page *page; 389aa4d8616SChristoph Hellwig ssize_t len; 390aa4d8616SChristoph Hellwig int ret = 0; 391aa4d8616SChristoph Hellwig 392aa4d8616SChristoph Hellwig page = alloc_page(GFP_NOIO); 393aa4d8616SChristoph Hellwig if (unlikely(!page)) 394aa4d8616SChristoph Hellwig return -ENOMEM; 395aa4d8616SChristoph Hellwig 396aa4d8616SChristoph Hellwig rq_for_each_segment(bvec, rq, iter) { 397aa4d8616SChristoph Hellwig loff_t offset = pos; 398aa4d8616SChristoph Hellwig 399aa4d8616SChristoph Hellwig b.bv_page = page; 400aa4d8616SChristoph Hellwig b.bv_offset = 0; 401aa4d8616SChristoph Hellwig b.bv_len = bvec.bv_len; 402aa4d8616SChristoph Hellwig 403b6207430SChristoph Hellwig iov_iter_bvec(&i, READ, &b, 1, b.bv_len); 40418e9710eSChristoph Hellwig len = vfs_iter_read(lo->lo_backing_file, &i, &pos, 0); 405aa4d8616SChristoph Hellwig if (len < 0) { 406aa4d8616SChristoph Hellwig ret = len; 407aa4d8616SChristoph Hellwig goto out_free_page; 408aa4d8616SChristoph Hellwig } 409aa4d8616SChristoph Hellwig 410aa4d8616SChristoph Hellwig ret = lo_do_transfer(lo, READ, page, 0, bvec.bv_page, 411aa4d8616SChristoph Hellwig bvec.bv_offset, len, offset >> 9); 412aa4d8616SChristoph Hellwig if (ret) 413aa4d8616SChristoph Hellwig goto out_free_page; 414aa4d8616SChristoph Hellwig 415aa4d8616SChristoph Hellwig flush_dcache_page(bvec.bv_page); 416aa4d8616SChristoph Hellwig 417aa4d8616SChristoph Hellwig if (len != bvec.bv_len) { 418aa4d8616SChristoph Hellwig struct bio *bio; 419aa4d8616SChristoph Hellwig 420aa4d8616SChristoph Hellwig __rq_for_each_bio(bio, rq) 421aa4d8616SChristoph Hellwig zero_fill_bio(bio); 422aa4d8616SChristoph Hellwig break; 423aa4d8616SChristoph Hellwig } 424aa4d8616SChristoph Hellwig } 425aa4d8616SChristoph Hellwig 426aa4d8616SChristoph Hellwig ret = 0; 427aa4d8616SChristoph Hellwig out_free_page: 428aa4d8616SChristoph Hellwig __free_page(page); 429aa4d8616SChristoph Hellwig return ret; 430aa4d8616SChristoph Hellwig } 431aa4d8616SChristoph Hellwig 432efcfec57SDarrick J. Wong static int lo_fallocate(struct loop_device *lo, struct request *rq, loff_t pos, 433efcfec57SDarrick J. Wong int mode) 434cf655d95SMing Lei { 435cf655d95SMing Lei /* 436efcfec57SDarrick J. Wong * We use fallocate to manipulate the space mappings used by the image 437efcfec57SDarrick J. Wong * a.k.a. discard/zerorange. However we do not support this if 438efcfec57SDarrick J. Wong * encryption is enabled, because it may give an attacker useful 439efcfec57SDarrick J. Wong * information. 440cf655d95SMing Lei */ 441cf655d95SMing Lei struct file *file = lo->lo_backing_file; 442c52abf56SEvan Green struct request_queue *q = lo->lo_queue; 443cf655d95SMing Lei int ret; 444cf655d95SMing Lei 445efcfec57SDarrick J. Wong mode |= FALLOC_FL_KEEP_SIZE; 446efcfec57SDarrick J. Wong 447c52abf56SEvan Green if (!blk_queue_discard(q)) { 448cf655d95SMing Lei ret = -EOPNOTSUPP; 449cf655d95SMing Lei goto out; 450cf655d95SMing Lei } 451cf655d95SMing Lei 452cf655d95SMing Lei ret = file->f_op->fallocate(file, mode, pos, blk_rq_bytes(rq)); 453cf655d95SMing Lei if (unlikely(ret && ret != -EINVAL && ret != -EOPNOTSUPP)) 454cf655d95SMing Lei ret = -EIO; 455cf655d95SMing Lei out: 456cf655d95SMing Lei return ret; 457cf655d95SMing Lei } 458cf655d95SMing Lei 459cf655d95SMing Lei static int lo_req_flush(struct loop_device *lo, struct request *rq) 460cf655d95SMing Lei { 461cf655d95SMing Lei struct file *file = lo->lo_backing_file; 462cf655d95SMing Lei int ret = vfs_fsync(file, 0); 463cf655d95SMing Lei if (unlikely(ret && ret != -EINVAL)) 464cf655d95SMing Lei ret = -EIO; 465cf655d95SMing Lei 466cf655d95SMing Lei return ret; 467cf655d95SMing Lei } 468cf655d95SMing Lei 469fe2cb290SChristoph Hellwig static void lo_complete_rq(struct request *rq) 470bc07c10aSMing Lei { 471fe2cb290SChristoph Hellwig struct loop_cmd *cmd = blk_mq_rq_to_pdu(rq); 472f9de14bcSJens Axboe blk_status_t ret = BLK_STS_OK; 473bc07c10aSMing Lei 474f9de14bcSJens Axboe if (!cmd->use_aio || cmd->ret < 0 || cmd->ret == blk_rq_bytes(rq) || 475f9de14bcSJens Axboe req_op(rq) != REQ_OP_READ) { 476f9de14bcSJens Axboe if (cmd->ret < 0) 4778cd55087SEvan Green ret = errno_to_blk_status(cmd->ret); 478f9de14bcSJens Axboe goto end_io; 479bc07c10aSMing Lei } 480fe2cb290SChristoph Hellwig 481f9de14bcSJens Axboe /* 482f9de14bcSJens Axboe * Short READ - if we got some data, advance our request and 483f9de14bcSJens Axboe * retry it. If we got no data, end the rest with EIO. 484f9de14bcSJens Axboe */ 485f9de14bcSJens Axboe if (cmd->ret) { 486f9de14bcSJens Axboe blk_update_request(rq, BLK_STS_OK, cmd->ret); 487f9de14bcSJens Axboe cmd->ret = 0; 488f9de14bcSJens Axboe blk_mq_requeue_request(rq, true); 489f9de14bcSJens Axboe } else { 490f9de14bcSJens Axboe if (cmd->use_aio) { 491f9de14bcSJens Axboe struct bio *bio = rq->bio; 492f9de14bcSJens Axboe 493f9de14bcSJens Axboe while (bio) { 494f9de14bcSJens Axboe zero_fill_bio(bio); 495f9de14bcSJens Axboe bio = bio->bi_next; 496f9de14bcSJens Axboe } 497f9de14bcSJens Axboe } 498f9de14bcSJens Axboe ret = BLK_STS_IOERR; 499f9de14bcSJens Axboe end_io: 500f9de14bcSJens Axboe blk_mq_end_request(rq, ret); 501f9de14bcSJens Axboe } 502bc07c10aSMing Lei } 503bc07c10aSMing Lei 50492d77332SShaohua Li static void lo_rw_aio_do_completion(struct loop_cmd *cmd) 50592d77332SShaohua Li { 5061894e916SJens Axboe struct request *rq = blk_mq_rq_from_pdu(cmd); 5071894e916SJens Axboe 50892d77332SShaohua Li if (!atomic_dec_and_test(&cmd->ref)) 50992d77332SShaohua Li return; 51092d77332SShaohua Li kfree(cmd->bvec); 51192d77332SShaohua Li cmd->bvec = NULL; 51215f73f5bSChristoph Hellwig if (likely(!blk_should_fake_timeout(rq->q))) 5131894e916SJens Axboe blk_mq_complete_request(rq); 51492d77332SShaohua Li } 51592d77332SShaohua Li 516bc07c10aSMing Lei static void lo_rw_aio_complete(struct kiocb *iocb, long ret, long ret2) 517bc07c10aSMing Lei { 518bc07c10aSMing Lei struct loop_cmd *cmd = container_of(iocb, struct loop_cmd, iocb); 519bc07c10aSMing Lei 520fe2cb290SChristoph Hellwig cmd->ret = ret; 52192d77332SShaohua Li lo_rw_aio_do_completion(cmd); 522bc07c10aSMing Lei } 523bc07c10aSMing Lei 524bc07c10aSMing Lei static int lo_rw_aio(struct loop_device *lo, struct loop_cmd *cmd, 525bc07c10aSMing Lei loff_t pos, bool rw) 526bc07c10aSMing Lei { 527bc07c10aSMing Lei struct iov_iter iter; 52886af5952SMing Lei struct req_iterator rq_iter; 529bc07c10aSMing Lei struct bio_vec *bvec; 5301894e916SJens Axboe struct request *rq = blk_mq_rq_from_pdu(cmd); 53140326d8aSShaohua Li struct bio *bio = rq->bio; 532bc07c10aSMing Lei struct file *file = lo->lo_backing_file; 53386af5952SMing Lei struct bio_vec tmp; 53440326d8aSShaohua Li unsigned int offset; 53586af5952SMing Lei int nr_bvec = 0; 536bc07c10aSMing Lei int ret; 537bc07c10aSMing Lei 53886af5952SMing Lei rq_for_each_bvec(tmp, rq, rq_iter) 53986af5952SMing Lei nr_bvec++; 540bc07c10aSMing Lei 54186af5952SMing Lei if (rq->bio != rq->biotail) { 54286af5952SMing Lei 54386af5952SMing Lei bvec = kmalloc_array(nr_bvec, sizeof(struct bio_vec), 5446da2ec56SKees Cook GFP_NOIO); 54540326d8aSShaohua Li if (!bvec) 54640326d8aSShaohua Li return -EIO; 54740326d8aSShaohua Li cmd->bvec = bvec; 54840326d8aSShaohua Li 549a7297a6aSMing Lei /* 55040326d8aSShaohua Li * The bios of the request may be started from the middle of 55140326d8aSShaohua Li * the 'bvec' because of bio splitting, so we can't directly 55286af5952SMing Lei * copy bio->bi_iov_vec to new bvec. The rq_for_each_bvec 55340326d8aSShaohua Li * API will take care of all details for us. 554a7297a6aSMing Lei */ 55586af5952SMing Lei rq_for_each_bvec(tmp, rq, rq_iter) { 55640326d8aSShaohua Li *bvec = tmp; 55740326d8aSShaohua Li bvec++; 55840326d8aSShaohua Li } 55940326d8aSShaohua Li bvec = cmd->bvec; 56040326d8aSShaohua Li offset = 0; 56140326d8aSShaohua Li } else { 56240326d8aSShaohua Li /* 56340326d8aSShaohua Li * Same here, this bio may be started from the middle of the 56440326d8aSShaohua Li * 'bvec' because of bio splitting, so offset from the bvec 56540326d8aSShaohua Li * must be passed to iov iterator 56640326d8aSShaohua Li */ 56740326d8aSShaohua Li offset = bio->bi_iter.bi_bvec_done; 56840326d8aSShaohua Li bvec = __bvec_iter_bvec(bio->bi_io_vec, bio->bi_iter); 56940326d8aSShaohua Li } 57092d77332SShaohua Li atomic_set(&cmd->ref, 2); 57140326d8aSShaohua Li 572b6207430SChristoph Hellwig iov_iter_bvec(&iter, rw, bvec, nr_bvec, blk_rq_bytes(rq)); 57340326d8aSShaohua Li iter.iov_offset = offset; 574bc07c10aSMing Lei 575bc07c10aSMing Lei cmd->iocb.ki_pos = pos; 576bc07c10aSMing Lei cmd->iocb.ki_filp = file; 577bc07c10aSMing Lei cmd->iocb.ki_complete = lo_rw_aio_complete; 578bc07c10aSMing Lei cmd->iocb.ki_flags = IOCB_DIRECT; 579d9a08a9eSAdam Manzanares cmd->iocb.ki_ioprio = IOPRIO_PRIO_VALUE(IOPRIO_CLASS_NONE, 0); 580bc07c10aSMing Lei 581bc07c10aSMing Lei if (rw == WRITE) 582bb7462b6SMiklos Szeredi ret = call_write_iter(file, &cmd->iocb, &iter); 583bc07c10aSMing Lei else 584bb7462b6SMiklos Szeredi ret = call_read_iter(file, &cmd->iocb, &iter); 585bc07c10aSMing Lei 58692d77332SShaohua Li lo_rw_aio_do_completion(cmd); 58792d77332SShaohua Li 588bc07c10aSMing Lei if (ret != -EIOCBQUEUED) 589bc07c10aSMing Lei cmd->iocb.ki_complete(&cmd->iocb, ret, 0); 590bc07c10aSMing Lei return 0; 591bc07c10aSMing Lei } 592bc07c10aSMing Lei 593c1c87c2bSChristoph Hellwig static int do_req_filebacked(struct loop_device *lo, struct request *rq) 594bc07c10aSMing Lei { 595bc07c10aSMing Lei struct loop_cmd *cmd = blk_mq_rq_to_pdu(rq); 596c1c87c2bSChristoph Hellwig loff_t pos = ((loff_t) blk_rq_pos(rq) << 9) + lo->lo_offset; 597bc07c10aSMing Lei 598bc07c10aSMing Lei /* 599bc07c10aSMing Lei * lo_write_simple and lo_read_simple should have been covered 600bc07c10aSMing Lei * by io submit style function like lo_rw_aio(), one blocker 601bc07c10aSMing Lei * is that lo_read_simple() need to call flush_dcache_page after 602bc07c10aSMing Lei * the page is written from kernel, and it isn't easy to handle 603bc07c10aSMing Lei * this in io submit style function which submits all segments 604bc07c10aSMing Lei * of the req at one time. And direct read IO doesn't need to 605bc07c10aSMing Lei * run flush_dcache_page(). 606bc07c10aSMing Lei */ 607c1c87c2bSChristoph Hellwig switch (req_op(rq)) { 608c1c87c2bSChristoph Hellwig case REQ_OP_FLUSH: 609c1c87c2bSChristoph Hellwig return lo_req_flush(lo, rq); 61019372e27SChristoph Hellwig case REQ_OP_WRITE_ZEROES: 611efcfec57SDarrick J. Wong /* 612efcfec57SDarrick J. Wong * If the caller doesn't want deallocation, call zeroout to 613efcfec57SDarrick J. Wong * write zeroes the range. Otherwise, punch them out. 614efcfec57SDarrick J. Wong */ 615efcfec57SDarrick J. Wong return lo_fallocate(lo, rq, pos, 616efcfec57SDarrick J. Wong (rq->cmd_flags & REQ_NOUNMAP) ? 617efcfec57SDarrick J. Wong FALLOC_FL_ZERO_RANGE : 618efcfec57SDarrick J. Wong FALLOC_FL_PUNCH_HOLE); 619efcfec57SDarrick J. Wong case REQ_OP_DISCARD: 620efcfec57SDarrick J. Wong return lo_fallocate(lo, rq, pos, FALLOC_FL_PUNCH_HOLE); 621c1c87c2bSChristoph Hellwig case REQ_OP_WRITE: 622c1c87c2bSChristoph Hellwig if (lo->transfer) 623c1c87c2bSChristoph Hellwig return lo_write_transfer(lo, rq, pos); 624c1c87c2bSChristoph Hellwig else if (cmd->use_aio) 625c1c87c2bSChristoph Hellwig return lo_rw_aio(lo, cmd, pos, WRITE); 626c1c87c2bSChristoph Hellwig else 627bc07c10aSMing Lei return lo_write_simple(lo, rq, pos); 628c1c87c2bSChristoph Hellwig case REQ_OP_READ: 629c1c87c2bSChristoph Hellwig if (lo->transfer) 630c1c87c2bSChristoph Hellwig return lo_read_transfer(lo, rq, pos); 631c1c87c2bSChristoph Hellwig else if (cmd->use_aio) 632c1c87c2bSChristoph Hellwig return lo_rw_aio(lo, cmd, pos, READ); 633bc07c10aSMing Lei else 634bc07c10aSMing Lei return lo_read_simple(lo, rq, pos); 635c1c87c2bSChristoph Hellwig default: 636c1c87c2bSChristoph Hellwig WARN_ON_ONCE(1); 637c1c87c2bSChristoph Hellwig return -EIO; 638bc07c10aSMing Lei } 6391da177e4SLinus Torvalds } 6401da177e4SLinus Torvalds 6412e5ab5f3SMing Lei static inline void loop_update_dio(struct loop_device *lo) 6422e5ab5f3SMing Lei { 643efbe3c24SIra Weiny __loop_update_dio(lo, (lo->lo_backing_file->f_flags & O_DIRECT) | 6442e5ab5f3SMing Lei lo->use_dio); 6452e5ab5f3SMing Lei } 6462e5ab5f3SMing Lei 64706f0e9e6SMing Lei static void loop_reread_partitions(struct loop_device *lo, 64806f0e9e6SMing Lei struct block_device *bdev) 64906f0e9e6SMing Lei { 65006f0e9e6SMing Lei int rc; 65106f0e9e6SMing Lei 652f0b870dfSChristoph Hellwig mutex_lock(&bdev->bd_mutex); 653f0b870dfSChristoph Hellwig rc = bdev_disk_changed(bdev, false); 654f0b870dfSChristoph Hellwig mutex_unlock(&bdev->bd_mutex); 65506f0e9e6SMing Lei if (rc) 65606f0e9e6SMing Lei pr_warn("%s: partition scan of loop%d (%s) failed (rc=%d)\n", 65706f0e9e6SMing Lei __func__, lo->lo_number, lo->lo_file_name, rc); 65806f0e9e6SMing Lei } 65906f0e9e6SMing Lei 660d2ac838eSTheodore Ts'o static inline int is_loop_device(struct file *file) 661d2ac838eSTheodore Ts'o { 662d2ac838eSTheodore Ts'o struct inode *i = file->f_mapping->host; 663d2ac838eSTheodore Ts'o 6646f24784fSAl Viro return i && S_ISBLK(i->i_mode) && imajor(i) == LOOP_MAJOR; 665d2ac838eSTheodore Ts'o } 666d2ac838eSTheodore Ts'o 667d2ac838eSTheodore Ts'o static int loop_validate_file(struct file *file, struct block_device *bdev) 668d2ac838eSTheodore Ts'o { 669d2ac838eSTheodore Ts'o struct inode *inode = file->f_mapping->host; 670d2ac838eSTheodore Ts'o struct file *f = file; 671d2ac838eSTheodore Ts'o 672d2ac838eSTheodore Ts'o /* Avoid recursion */ 673d2ac838eSTheodore Ts'o while (is_loop_device(f)) { 674d2ac838eSTheodore Ts'o struct loop_device *l; 675d2ac838eSTheodore Ts'o 6764e7b5671SChristoph Hellwig if (f->f_mapping->host->i_rdev == bdev->bd_dev) 677d2ac838eSTheodore Ts'o return -EBADF; 678d2ac838eSTheodore Ts'o 6794e7b5671SChristoph Hellwig l = I_BDEV(f->f_mapping->host)->bd_disk->private_data; 680f7c8a412SDongli Zhang if (l->lo_state != Lo_bound) { 681d2ac838eSTheodore Ts'o return -EINVAL; 682d2ac838eSTheodore Ts'o } 683d2ac838eSTheodore Ts'o f = l->lo_backing_file; 684d2ac838eSTheodore Ts'o } 685d2ac838eSTheodore Ts'o if (!S_ISREG(inode->i_mode) && !S_ISBLK(inode->i_mode)) 686d2ac838eSTheodore Ts'o return -EINVAL; 687d2ac838eSTheodore Ts'o return 0; 688d2ac838eSTheodore Ts'o } 689d2ac838eSTheodore Ts'o 6901da177e4SLinus Torvalds /* 6911da177e4SLinus Torvalds * loop_change_fd switched the backing store of a loopback device to 6921da177e4SLinus Torvalds * a new file. This is useful for operating system installers to free up 6931da177e4SLinus Torvalds * the original file and in High Availability environments to switch to 6941da177e4SLinus Torvalds * an alternative location for the content in case of server meltdown. 6951da177e4SLinus Torvalds * This can only work if the loop device is used read-only, and if the 6961da177e4SLinus Torvalds * new backing store is the same size and type as the old backing store. 6971da177e4SLinus Torvalds */ 698bb214884SAl Viro static int loop_change_fd(struct loop_device *lo, struct block_device *bdev, 699bb214884SAl Viro unsigned int arg) 7001da177e4SLinus Torvalds { 7011dded9acSJan Kara struct file *file = NULL, *old_file; 7021da177e4SLinus Torvalds int error; 70385b0a54aSJan Kara bool partscan; 7041da177e4SLinus Torvalds 7056cc8e743SPavel Tatashin error = mutex_lock_killable(&lo->lo_mutex); 706c3710770SJan Kara if (error) 707c3710770SJan Kara return error; 7081da177e4SLinus Torvalds error = -ENXIO; 7091da177e4SLinus Torvalds if (lo->lo_state != Lo_bound) 7101dded9acSJan Kara goto out_err; 7111da177e4SLinus Torvalds 7121da177e4SLinus Torvalds /* the loop device has to be read-only */ 7131da177e4SLinus Torvalds error = -EINVAL; 7141da177e4SLinus Torvalds if (!(lo->lo_flags & LO_FLAGS_READ_ONLY)) 7151dded9acSJan Kara goto out_err; 7161da177e4SLinus Torvalds 7171da177e4SLinus Torvalds error = -EBADF; 7181da177e4SLinus Torvalds file = fget(arg); 7191da177e4SLinus Torvalds if (!file) 7201dded9acSJan Kara goto out_err; 7211da177e4SLinus Torvalds 722d2ac838eSTheodore Ts'o error = loop_validate_file(file, bdev); 723d2ac838eSTheodore Ts'o if (error) 7241dded9acSJan Kara goto out_err; 725d2ac838eSTheodore Ts'o 7261da177e4SLinus Torvalds old_file = lo->lo_backing_file; 7271da177e4SLinus Torvalds 7281da177e4SLinus Torvalds error = -EINVAL; 7291da177e4SLinus Torvalds 7301da177e4SLinus Torvalds /* size of the new backing store needs to be the same */ 7311da177e4SLinus Torvalds if (get_loop_size(lo, file) != get_loop_size(lo, old_file)) 7321dded9acSJan Kara goto out_err; 7331da177e4SLinus Torvalds 7341da177e4SLinus Torvalds /* and ... switch */ 73543cade80SOmar Sandoval blk_mq_freeze_queue(lo->lo_queue); 73643cade80SOmar Sandoval mapping_set_gfp_mask(old_file->f_mapping, lo->old_gfp_mask); 73743cade80SOmar Sandoval lo->lo_backing_file = file; 73843cade80SOmar Sandoval lo->old_gfp_mask = mapping_gfp_mask(file->f_mapping); 73943cade80SOmar Sandoval mapping_set_gfp_mask(file->f_mapping, 74043cade80SOmar Sandoval lo->old_gfp_mask & ~(__GFP_IO|__GFP_FS)); 74143cade80SOmar Sandoval loop_update_dio(lo); 74243cade80SOmar Sandoval blk_mq_unfreeze_queue(lo->lo_queue); 74385b0a54aSJan Kara partscan = lo->lo_flags & LO_FLAGS_PARTSCAN; 7446cc8e743SPavel Tatashin mutex_unlock(&lo->lo_mutex); 7451dded9acSJan Kara /* 7466cc8e743SPavel Tatashin * We must drop file reference outside of lo_mutex as dropping 7471dded9acSJan Kara * the file ref can take bd_mutex which creates circular locking 7481dded9acSJan Kara * dependency. 7491dded9acSJan Kara */ 7501dded9acSJan Kara fput(old_file); 75185b0a54aSJan Kara if (partscan) 75285b0a54aSJan Kara loop_reread_partitions(lo, bdev); 7531da177e4SLinus Torvalds return 0; 7541da177e4SLinus Torvalds 7551dded9acSJan Kara out_err: 7566cc8e743SPavel Tatashin mutex_unlock(&lo->lo_mutex); 7571dded9acSJan Kara if (file) 7581dded9acSJan Kara fput(file); 7591da177e4SLinus Torvalds return error; 7601da177e4SLinus Torvalds } 7611da177e4SLinus Torvalds 762ee862730SMilan Broz /* loop sysfs attributes */ 763ee862730SMilan Broz 764ee862730SMilan Broz static ssize_t loop_attr_show(struct device *dev, char *page, 765ee862730SMilan Broz ssize_t (*callback)(struct loop_device *, char *)) 766ee862730SMilan Broz { 76734dd82afSKay Sievers struct gendisk *disk = dev_to_disk(dev); 76834dd82afSKay Sievers struct loop_device *lo = disk->private_data; 769ee862730SMilan Broz 77034dd82afSKay Sievers return callback(lo, page); 771ee862730SMilan Broz } 772ee862730SMilan Broz 773ee862730SMilan Broz #define LOOP_ATTR_RO(_name) \ 774ee862730SMilan Broz static ssize_t loop_attr_##_name##_show(struct loop_device *, char *); \ 775ee862730SMilan Broz static ssize_t loop_attr_do_show_##_name(struct device *d, \ 776ee862730SMilan Broz struct device_attribute *attr, char *b) \ 777ee862730SMilan Broz { \ 778ee862730SMilan Broz return loop_attr_show(d, b, loop_attr_##_name##_show); \ 779ee862730SMilan Broz } \ 780ee862730SMilan Broz static struct device_attribute loop_attr_##_name = \ 7815657a819SJoe Perches __ATTR(_name, 0444, loop_attr_do_show_##_name, NULL); 782ee862730SMilan Broz 783ee862730SMilan Broz static ssize_t loop_attr_backing_file_show(struct loop_device *lo, char *buf) 784ee862730SMilan Broz { 785ee862730SMilan Broz ssize_t ret; 786ee862730SMilan Broz char *p = NULL; 787ee862730SMilan Broz 78805eb0f25SKay Sievers spin_lock_irq(&lo->lo_lock); 789ee862730SMilan Broz if (lo->lo_backing_file) 7909bf39ab2SMiklos Szeredi p = file_path(lo->lo_backing_file, buf, PAGE_SIZE - 1); 79105eb0f25SKay Sievers spin_unlock_irq(&lo->lo_lock); 792ee862730SMilan Broz 793ee862730SMilan Broz if (IS_ERR_OR_NULL(p)) 794ee862730SMilan Broz ret = PTR_ERR(p); 795ee862730SMilan Broz else { 796ee862730SMilan Broz ret = strlen(p); 797ee862730SMilan Broz memmove(buf, p, ret); 798ee862730SMilan Broz buf[ret++] = '\n'; 799ee862730SMilan Broz buf[ret] = 0; 800ee862730SMilan Broz } 801ee862730SMilan Broz 802ee862730SMilan Broz return ret; 803ee862730SMilan Broz } 804ee862730SMilan Broz 805ee862730SMilan Broz static ssize_t loop_attr_offset_show(struct loop_device *lo, char *buf) 806ee862730SMilan Broz { 807ee862730SMilan Broz return sprintf(buf, "%llu\n", (unsigned long long)lo->lo_offset); 808ee862730SMilan Broz } 809ee862730SMilan Broz 810ee862730SMilan Broz static ssize_t loop_attr_sizelimit_show(struct loop_device *lo, char *buf) 811ee862730SMilan Broz { 812ee862730SMilan Broz return sprintf(buf, "%llu\n", (unsigned long long)lo->lo_sizelimit); 813ee862730SMilan Broz } 814ee862730SMilan Broz 815ee862730SMilan Broz static ssize_t loop_attr_autoclear_show(struct loop_device *lo, char *buf) 816ee862730SMilan Broz { 817ee862730SMilan Broz int autoclear = (lo->lo_flags & LO_FLAGS_AUTOCLEAR); 818ee862730SMilan Broz 819ee862730SMilan Broz return sprintf(buf, "%s\n", autoclear ? "1" : "0"); 820ee862730SMilan Broz } 821ee862730SMilan Broz 822e03c8dd1SKay Sievers static ssize_t loop_attr_partscan_show(struct loop_device *lo, char *buf) 823e03c8dd1SKay Sievers { 824e03c8dd1SKay Sievers int partscan = (lo->lo_flags & LO_FLAGS_PARTSCAN); 825e03c8dd1SKay Sievers 826e03c8dd1SKay Sievers return sprintf(buf, "%s\n", partscan ? "1" : "0"); 827e03c8dd1SKay Sievers } 828e03c8dd1SKay Sievers 8292e5ab5f3SMing Lei static ssize_t loop_attr_dio_show(struct loop_device *lo, char *buf) 8302e5ab5f3SMing Lei { 8312e5ab5f3SMing Lei int dio = (lo->lo_flags & LO_FLAGS_DIRECT_IO); 8322e5ab5f3SMing Lei 8332e5ab5f3SMing Lei return sprintf(buf, "%s\n", dio ? "1" : "0"); 8342e5ab5f3SMing Lei } 8352e5ab5f3SMing Lei 836ee862730SMilan Broz LOOP_ATTR_RO(backing_file); 837ee862730SMilan Broz LOOP_ATTR_RO(offset); 838ee862730SMilan Broz LOOP_ATTR_RO(sizelimit); 839ee862730SMilan Broz LOOP_ATTR_RO(autoclear); 840e03c8dd1SKay Sievers LOOP_ATTR_RO(partscan); 8412e5ab5f3SMing Lei LOOP_ATTR_RO(dio); 842ee862730SMilan Broz 843ee862730SMilan Broz static struct attribute *loop_attrs[] = { 844ee862730SMilan Broz &loop_attr_backing_file.attr, 845ee862730SMilan Broz &loop_attr_offset.attr, 846ee862730SMilan Broz &loop_attr_sizelimit.attr, 847ee862730SMilan Broz &loop_attr_autoclear.attr, 848e03c8dd1SKay Sievers &loop_attr_partscan.attr, 8492e5ab5f3SMing Lei &loop_attr_dio.attr, 850ee862730SMilan Broz NULL, 851ee862730SMilan Broz }; 852ee862730SMilan Broz 853ee862730SMilan Broz static struct attribute_group loop_attribute_group = { 854ee862730SMilan Broz .name = "loop", 855ee862730SMilan Broz .attrs= loop_attrs, 856ee862730SMilan Broz }; 857ee862730SMilan Broz 858d3349b6bSTetsuo Handa static void loop_sysfs_init(struct loop_device *lo) 859ee862730SMilan Broz { 860d3349b6bSTetsuo Handa lo->sysfs_inited = !sysfs_create_group(&disk_to_dev(lo->lo_disk)->kobj, 861ee862730SMilan Broz &loop_attribute_group); 862ee862730SMilan Broz } 863ee862730SMilan Broz 864ee862730SMilan Broz static void loop_sysfs_exit(struct loop_device *lo) 865ee862730SMilan Broz { 866d3349b6bSTetsuo Handa if (lo->sysfs_inited) 867ee862730SMilan Broz sysfs_remove_group(&disk_to_dev(lo->lo_disk)->kobj, 868ee862730SMilan Broz &loop_attribute_group); 869ee862730SMilan Broz } 870ee862730SMilan Broz 871dfaa2ef6SLukas Czerner static void loop_config_discard(struct loop_device *lo) 872dfaa2ef6SLukas Czerner { 873dfaa2ef6SLukas Czerner struct file *file = lo->lo_backing_file; 874dfaa2ef6SLukas Czerner struct inode *inode = file->f_mapping->host; 875dfaa2ef6SLukas Czerner struct request_queue *q = lo->lo_queue; 876bcb21c8cSMing Lei u32 granularity, max_discard_sectors; 877dfaa2ef6SLukas Czerner 878dfaa2ef6SLukas Czerner /* 879c52abf56SEvan Green * If the backing device is a block device, mirror its zeroing 880c52abf56SEvan Green * capability. Set the discard sectors to the block device's zeroing 881c52abf56SEvan Green * capabilities because loop discards result in blkdev_issue_zeroout(), 882c52abf56SEvan Green * not blkdev_issue_discard(). This maintains consistent behavior with 883c52abf56SEvan Green * file-backed loop devices: discarded regions read back as zero. 884c52abf56SEvan Green */ 885c52abf56SEvan Green if (S_ISBLK(inode->i_mode) && !lo->lo_encrypt_key_size) { 8864e7b5671SChristoph Hellwig struct request_queue *backingq = bdev_get_queue(I_BDEV(inode)); 887c52abf56SEvan Green 888bcb21c8cSMing Lei max_discard_sectors = backingq->limits.max_write_zeroes_sectors; 889bcb21c8cSMing Lei granularity = backingq->limits.discard_granularity ?: 890bcb21c8cSMing Lei queue_physical_block_size(backingq); 891c52abf56SEvan Green 892c52abf56SEvan Green /* 893dfaa2ef6SLukas Czerner * We use punch hole to reclaim the free space used by the 89412a64d2fSOlaf Hering * image a.k.a. discard. However we do not support discard if 895dfaa2ef6SLukas Czerner * encryption is enabled, because it may give an attacker 896dfaa2ef6SLukas Czerner * useful information. 897dfaa2ef6SLukas Czerner */ 898c52abf56SEvan Green } else if (!file->f_op->fallocate || lo->lo_encrypt_key_size) { 899bcb21c8cSMing Lei max_discard_sectors = 0; 900bcb21c8cSMing Lei granularity = 0; 901dfaa2ef6SLukas Czerner 902c52abf56SEvan Green } else { 903bcb21c8cSMing Lei max_discard_sectors = UINT_MAX >> 9; 904bcb21c8cSMing Lei granularity = inode->i_sb->s_blocksize; 905c52abf56SEvan Green } 906c52abf56SEvan Green 907bcb21c8cSMing Lei if (max_discard_sectors) { 908bcb21c8cSMing Lei q->limits.discard_granularity = granularity; 909bcb21c8cSMing Lei blk_queue_max_discard_sectors(q, max_discard_sectors); 910bcb21c8cSMing Lei blk_queue_max_write_zeroes_sectors(q, max_discard_sectors); 9118b904b5bSBart Van Assche blk_queue_flag_set(QUEUE_FLAG_DISCARD, q); 912bcb21c8cSMing Lei } else { 913bcb21c8cSMing Lei q->limits.discard_granularity = 0; 914bcb21c8cSMing Lei blk_queue_max_discard_sectors(q, 0); 915bcb21c8cSMing Lei blk_queue_max_write_zeroes_sectors(q, 0); 916c52abf56SEvan Green blk_queue_flag_clear(QUEUE_FLAG_DISCARD, q); 917dfaa2ef6SLukas Czerner } 918bcb21c8cSMing Lei q->limits.discard_alignment = 0; 919bcb21c8cSMing Lei } 920dfaa2ef6SLukas Czerner 92187579e9bSDan Schatzberg struct loop_worker { 92287579e9bSDan Schatzberg struct rb_node rb_node; 92387579e9bSDan Schatzberg struct work_struct work; 92487579e9bSDan Schatzberg struct list_head cmd_list; 92587579e9bSDan Schatzberg struct list_head idle_list; 92687579e9bSDan Schatzberg struct loop_device *lo; 927*c74d40e8SDan Schatzberg struct cgroup_subsys_state *blkcg_css; 92887579e9bSDan Schatzberg unsigned long last_ran_at; 92987579e9bSDan Schatzberg }; 930e03a3d7aSMing Lei 93187579e9bSDan Schatzberg static void loop_workfn(struct work_struct *work); 93287579e9bSDan Schatzberg static void loop_rootcg_workfn(struct work_struct *work); 93387579e9bSDan Schatzberg static void loop_free_idle_workers(struct timer_list *timer); 934b2ee7d46SNeilBrown 93587579e9bSDan Schatzberg #ifdef CONFIG_BLK_CGROUP 93687579e9bSDan Schatzberg static inline int queue_on_root_worker(struct cgroup_subsys_state *css) 937e03a3d7aSMing Lei { 93887579e9bSDan Schatzberg return !css || css == blkcg_root_css; 93987579e9bSDan Schatzberg } 94087579e9bSDan Schatzberg #else 94187579e9bSDan Schatzberg static inline int queue_on_root_worker(struct cgroup_subsys_state *css) 94287579e9bSDan Schatzberg { 94387579e9bSDan Schatzberg return !css; 94487579e9bSDan Schatzberg } 94587579e9bSDan Schatzberg #endif 94687579e9bSDan Schatzberg 94787579e9bSDan Schatzberg static void loop_queue_work(struct loop_device *lo, struct loop_cmd *cmd) 94887579e9bSDan Schatzberg { 94987579e9bSDan Schatzberg struct rb_node **node = &(lo->worker_tree.rb_node), *parent = NULL; 95087579e9bSDan Schatzberg struct loop_worker *cur_worker, *worker = NULL; 95187579e9bSDan Schatzberg struct work_struct *work; 95287579e9bSDan Schatzberg struct list_head *cmd_list; 95387579e9bSDan Schatzberg 95487579e9bSDan Schatzberg spin_lock_irq(&lo->lo_work_lock); 95587579e9bSDan Schatzberg 956*c74d40e8SDan Schatzberg if (queue_on_root_worker(cmd->blkcg_css)) 95787579e9bSDan Schatzberg goto queue_work; 95887579e9bSDan Schatzberg 95987579e9bSDan Schatzberg node = &lo->worker_tree.rb_node; 96087579e9bSDan Schatzberg 96187579e9bSDan Schatzberg while (*node) { 96287579e9bSDan Schatzberg parent = *node; 96387579e9bSDan Schatzberg cur_worker = container_of(*node, struct loop_worker, rb_node); 964*c74d40e8SDan Schatzberg if (cur_worker->blkcg_css == cmd->blkcg_css) { 96587579e9bSDan Schatzberg worker = cur_worker; 96687579e9bSDan Schatzberg break; 967*c74d40e8SDan Schatzberg } else if ((long)cur_worker->blkcg_css < (long)cmd->blkcg_css) { 96887579e9bSDan Schatzberg node = &(*node)->rb_left; 96987579e9bSDan Schatzberg } else { 97087579e9bSDan Schatzberg node = &(*node)->rb_right; 97187579e9bSDan Schatzberg } 97287579e9bSDan Schatzberg } 97387579e9bSDan Schatzberg if (worker) 97487579e9bSDan Schatzberg goto queue_work; 97587579e9bSDan Schatzberg 97687579e9bSDan Schatzberg worker = kzalloc(sizeof(struct loop_worker), GFP_NOWAIT | __GFP_NOWARN); 97787579e9bSDan Schatzberg /* 97887579e9bSDan Schatzberg * In the event we cannot allocate a worker, just queue on the 979*c74d40e8SDan Schatzberg * rootcg worker and issue the I/O as the rootcg 98087579e9bSDan Schatzberg */ 981*c74d40e8SDan Schatzberg if (!worker) { 982*c74d40e8SDan Schatzberg cmd->blkcg_css = NULL; 983*c74d40e8SDan Schatzberg if (cmd->memcg_css) 984*c74d40e8SDan Schatzberg css_put(cmd->memcg_css); 985*c74d40e8SDan Schatzberg cmd->memcg_css = NULL; 98687579e9bSDan Schatzberg goto queue_work; 987*c74d40e8SDan Schatzberg } 98887579e9bSDan Schatzberg 989*c74d40e8SDan Schatzberg worker->blkcg_css = cmd->blkcg_css; 990*c74d40e8SDan Schatzberg css_get(worker->blkcg_css); 99187579e9bSDan Schatzberg INIT_WORK(&worker->work, loop_workfn); 99287579e9bSDan Schatzberg INIT_LIST_HEAD(&worker->cmd_list); 99387579e9bSDan Schatzberg INIT_LIST_HEAD(&worker->idle_list); 99487579e9bSDan Schatzberg worker->lo = lo; 99587579e9bSDan Schatzberg rb_link_node(&worker->rb_node, parent, node); 99687579e9bSDan Schatzberg rb_insert_color(&worker->rb_node, &lo->worker_tree); 99787579e9bSDan Schatzberg queue_work: 99887579e9bSDan Schatzberg if (worker) { 99987579e9bSDan Schatzberg /* 100087579e9bSDan Schatzberg * We need to remove from the idle list here while 100187579e9bSDan Schatzberg * holding the lock so that the idle timer doesn't 100287579e9bSDan Schatzberg * free the worker 100387579e9bSDan Schatzberg */ 100487579e9bSDan Schatzberg if (!list_empty(&worker->idle_list)) 100587579e9bSDan Schatzberg list_del_init(&worker->idle_list); 100687579e9bSDan Schatzberg work = &worker->work; 100787579e9bSDan Schatzberg cmd_list = &worker->cmd_list; 100887579e9bSDan Schatzberg } else { 100987579e9bSDan Schatzberg work = &lo->rootcg_work; 101087579e9bSDan Schatzberg cmd_list = &lo->rootcg_cmd_list; 101187579e9bSDan Schatzberg } 101287579e9bSDan Schatzberg list_add_tail(&cmd->list_entry, cmd_list); 101387579e9bSDan Schatzberg queue_work(lo->workqueue, work); 101487579e9bSDan Schatzberg spin_unlock_irq(&lo->lo_work_lock); 1015e03a3d7aSMing Lei } 1016e03a3d7aSMing Lei 101756a85fd8SHolger Hoffstätte static void loop_update_rotational(struct loop_device *lo) 101856a85fd8SHolger Hoffstätte { 101956a85fd8SHolger Hoffstätte struct file *file = lo->lo_backing_file; 102056a85fd8SHolger Hoffstätte struct inode *file_inode = file->f_mapping->host; 102156a85fd8SHolger Hoffstätte struct block_device *file_bdev = file_inode->i_sb->s_bdev; 102256a85fd8SHolger Hoffstätte struct request_queue *q = lo->lo_queue; 102356a85fd8SHolger Hoffstätte bool nonrot = true; 102456a85fd8SHolger Hoffstätte 102556a85fd8SHolger Hoffstätte /* not all filesystems (e.g. tmpfs) have a sb->s_bdev */ 102656a85fd8SHolger Hoffstätte if (file_bdev) 102756a85fd8SHolger Hoffstätte nonrot = blk_queue_nonrot(bdev_get_queue(file_bdev)); 102856a85fd8SHolger Hoffstätte 102956a85fd8SHolger Hoffstätte if (nonrot) 103056a85fd8SHolger Hoffstätte blk_queue_flag_set(QUEUE_FLAG_NONROT, q); 103156a85fd8SHolger Hoffstätte else 103256a85fd8SHolger Hoffstätte blk_queue_flag_clear(QUEUE_FLAG_NONROT, q); 103356a85fd8SHolger Hoffstätte } 103456a85fd8SHolger Hoffstätte 10351da177e4SLinus Torvalds static int 10361da177e4SLinus Torvalds loop_release_xfer(struct loop_device *lo) 10371da177e4SLinus Torvalds { 10381da177e4SLinus Torvalds int err = 0; 10391da177e4SLinus Torvalds struct loop_func_table *xfer = lo->lo_encryption; 10401da177e4SLinus Torvalds 10411da177e4SLinus Torvalds if (xfer) { 10421da177e4SLinus Torvalds if (xfer->release) 10431da177e4SLinus Torvalds err = xfer->release(lo); 10441da177e4SLinus Torvalds lo->transfer = NULL; 10451da177e4SLinus Torvalds lo->lo_encryption = NULL; 10461da177e4SLinus Torvalds module_put(xfer->owner); 10471da177e4SLinus Torvalds } 10481da177e4SLinus Torvalds return err; 10491da177e4SLinus Torvalds } 10501da177e4SLinus Torvalds 10511da177e4SLinus Torvalds static int 10521da177e4SLinus Torvalds loop_init_xfer(struct loop_device *lo, struct loop_func_table *xfer, 10531da177e4SLinus Torvalds const struct loop_info64 *i) 10541da177e4SLinus Torvalds { 10551da177e4SLinus Torvalds int err = 0; 10561da177e4SLinus Torvalds 10571da177e4SLinus Torvalds if (xfer) { 10581da177e4SLinus Torvalds struct module *owner = xfer->owner; 10591da177e4SLinus Torvalds 10601da177e4SLinus Torvalds if (!try_module_get(owner)) 10611da177e4SLinus Torvalds return -EINVAL; 10621da177e4SLinus Torvalds if (xfer->init) 10631da177e4SLinus Torvalds err = xfer->init(lo, i); 10641da177e4SLinus Torvalds if (err) 10651da177e4SLinus Torvalds module_put(owner); 10661da177e4SLinus Torvalds else 10671da177e4SLinus Torvalds lo->lo_encryption = xfer; 10681da177e4SLinus Torvalds } 10691da177e4SLinus Torvalds return err; 10701da177e4SLinus Torvalds } 10711da177e4SLinus Torvalds 107262ab466cSMartijn Coenen /** 107362ab466cSMartijn Coenen * loop_set_status_from_info - configure device from loop_info 107462ab466cSMartijn Coenen * @lo: struct loop_device to configure 107562ab466cSMartijn Coenen * @info: struct loop_info64 to configure the device with 107662ab466cSMartijn Coenen * 107762ab466cSMartijn Coenen * Configures the loop device parameters according to the passed 107862ab466cSMartijn Coenen * in loop_info64 configuration. 107962ab466cSMartijn Coenen */ 108062ab466cSMartijn Coenen static int 108162ab466cSMartijn Coenen loop_set_status_from_info(struct loop_device *lo, 108262ab466cSMartijn Coenen const struct loop_info64 *info) 108362ab466cSMartijn Coenen { 108462ab466cSMartijn Coenen int err; 108562ab466cSMartijn Coenen struct loop_func_table *xfer; 108662ab466cSMartijn Coenen kuid_t uid = current_uid(); 108762ab466cSMartijn Coenen 108862ab466cSMartijn Coenen if ((unsigned int) info->lo_encrypt_key_size > LO_KEY_SIZE) 108962ab466cSMartijn Coenen return -EINVAL; 109062ab466cSMartijn Coenen 109162ab466cSMartijn Coenen err = loop_release_xfer(lo); 109262ab466cSMartijn Coenen if (err) 109362ab466cSMartijn Coenen return err; 109462ab466cSMartijn Coenen 109562ab466cSMartijn Coenen if (info->lo_encrypt_type) { 109662ab466cSMartijn Coenen unsigned int type = info->lo_encrypt_type; 109762ab466cSMartijn Coenen 109862ab466cSMartijn Coenen if (type >= MAX_LO_CRYPT) 109962ab466cSMartijn Coenen return -EINVAL; 110062ab466cSMartijn Coenen xfer = xfer_funcs[type]; 110162ab466cSMartijn Coenen if (xfer == NULL) 110262ab466cSMartijn Coenen return -EINVAL; 110362ab466cSMartijn Coenen } else 110462ab466cSMartijn Coenen xfer = NULL; 110562ab466cSMartijn Coenen 110662ab466cSMartijn Coenen err = loop_init_xfer(lo, xfer, info); 110762ab466cSMartijn Coenen if (err) 110862ab466cSMartijn Coenen return err; 110962ab466cSMartijn Coenen 111062ab466cSMartijn Coenen lo->lo_offset = info->lo_offset; 111162ab466cSMartijn Coenen lo->lo_sizelimit = info->lo_sizelimit; 111262ab466cSMartijn Coenen memcpy(lo->lo_file_name, info->lo_file_name, LO_NAME_SIZE); 111362ab466cSMartijn Coenen memcpy(lo->lo_crypt_name, info->lo_crypt_name, LO_NAME_SIZE); 111462ab466cSMartijn Coenen lo->lo_file_name[LO_NAME_SIZE-1] = 0; 111562ab466cSMartijn Coenen lo->lo_crypt_name[LO_NAME_SIZE-1] = 0; 111662ab466cSMartijn Coenen 111762ab466cSMartijn Coenen if (!xfer) 111862ab466cSMartijn Coenen xfer = &none_funcs; 111962ab466cSMartijn Coenen lo->transfer = xfer->transfer; 112062ab466cSMartijn Coenen lo->ioctl = xfer->ioctl; 112162ab466cSMartijn Coenen 1122faf1d254SMartijn Coenen lo->lo_flags = info->lo_flags; 112362ab466cSMartijn Coenen 112462ab466cSMartijn Coenen lo->lo_encrypt_key_size = info->lo_encrypt_key_size; 112562ab466cSMartijn Coenen lo->lo_init[0] = info->lo_init[0]; 112662ab466cSMartijn Coenen lo->lo_init[1] = info->lo_init[1]; 112762ab466cSMartijn Coenen if (info->lo_encrypt_key_size) { 112862ab466cSMartijn Coenen memcpy(lo->lo_encrypt_key, info->lo_encrypt_key, 112962ab466cSMartijn Coenen info->lo_encrypt_key_size); 113062ab466cSMartijn Coenen lo->lo_key_owner = uid; 113162ab466cSMartijn Coenen } 113262ab466cSMartijn Coenen 113362ab466cSMartijn Coenen return 0; 113462ab466cSMartijn Coenen } 113562ab466cSMartijn Coenen 11363448914eSMartijn Coenen static int loop_configure(struct loop_device *lo, fmode_t mode, 11373448914eSMartijn Coenen struct block_device *bdev, 11383448914eSMartijn Coenen const struct loop_config *config) 11391da177e4SLinus Torvalds { 11401da177e4SLinus Torvalds struct file *file; 11411da177e4SLinus Torvalds struct inode *inode; 11421da177e4SLinus Torvalds struct address_space *mapping; 11431da177e4SLinus Torvalds int error; 11441da177e4SLinus Torvalds loff_t size; 11451da177e4SLinus Torvalds bool partscan; 11463448914eSMartijn Coenen unsigned short bsize; 11471da177e4SLinus Torvalds 11481da177e4SLinus Torvalds /* This is safe, since we have a reference from open(). */ 11491da177e4SLinus Torvalds __module_get(THIS_MODULE); 11501da177e4SLinus Torvalds 11511da177e4SLinus Torvalds error = -EBADF; 11523448914eSMartijn Coenen file = fget(config->fd); 11531da177e4SLinus Torvalds if (!file) 11541da177e4SLinus Torvalds goto out; 11551da177e4SLinus Torvalds 11561da177e4SLinus Torvalds /* 11571da177e4SLinus Torvalds * If we don't hold exclusive handle for the device, upgrade to it 11581da177e4SLinus Torvalds * here to avoid changing device under exclusive owner. 11591da177e4SLinus Torvalds */ 11601da177e4SLinus Torvalds if (!(mode & FMODE_EXCL)) { 116137c3fc9aSChristoph Hellwig error = bd_prepare_to_claim(bdev, loop_configure); 1162ecbe6bc0SChristoph Hellwig if (error) 11631da177e4SLinus Torvalds goto out_putf; 11641da177e4SLinus Torvalds } 11651da177e4SLinus Torvalds 11666cc8e743SPavel Tatashin error = mutex_lock_killable(&lo->lo_mutex); 11671da177e4SLinus Torvalds if (error) 11681da177e4SLinus Torvalds goto out_bdev; 11691da177e4SLinus Torvalds 11701da177e4SLinus Torvalds error = -EBUSY; 11711da177e4SLinus Torvalds if (lo->lo_state != Lo_unbound) 11721da177e4SLinus Torvalds goto out_unlock; 11731da177e4SLinus Torvalds 11741da177e4SLinus Torvalds error = loop_validate_file(file, bdev); 11751da177e4SLinus Torvalds if (error) 11761da177e4SLinus Torvalds goto out_unlock; 11771da177e4SLinus Torvalds 11781da177e4SLinus Torvalds mapping = file->f_mapping; 11791da177e4SLinus Torvalds inode = mapping->host; 11801da177e4SLinus Torvalds 11813448914eSMartijn Coenen if ((config->info.lo_flags & ~LOOP_CONFIGURE_SETTABLE_FLAGS) != 0) { 11823448914eSMartijn Coenen error = -EINVAL; 11833448914eSMartijn Coenen goto out_unlock; 11843448914eSMartijn Coenen } 11853448914eSMartijn Coenen 11863448914eSMartijn Coenen if (config->block_size) { 11873448914eSMartijn Coenen error = loop_validate_block_size(config->block_size); 11883448914eSMartijn Coenen if (error) 11893448914eSMartijn Coenen goto out_unlock; 11903448914eSMartijn Coenen } 11913448914eSMartijn Coenen 11923448914eSMartijn Coenen error = loop_set_status_from_info(lo, &config->info); 11933448914eSMartijn Coenen if (error) 11943448914eSMartijn Coenen goto out_unlock; 11953448914eSMartijn Coenen 11961da177e4SLinus Torvalds if (!(file->f_mode & FMODE_WRITE) || !(mode & FMODE_WRITE) || 11971da177e4SLinus Torvalds !file->f_op->write_iter) 11983448914eSMartijn Coenen lo->lo_flags |= LO_FLAGS_READ_ONLY; 1199083a6a50SMartijn Coenen 120087579e9bSDan Schatzberg lo->workqueue = alloc_workqueue("loop%d", 120187579e9bSDan Schatzberg WQ_UNBOUND | WQ_FREEZABLE, 120287579e9bSDan Schatzberg 0, 120387579e9bSDan Schatzberg lo->lo_number); 120487579e9bSDan Schatzberg if (!lo->workqueue) { 120587579e9bSDan Schatzberg error = -ENOMEM; 12061da177e4SLinus Torvalds goto out_unlock; 120787579e9bSDan Schatzberg } 12081da177e4SLinus Torvalds 12097a2f0ce1SChristoph Hellwig set_disk_ro(lo->lo_disk, (lo->lo_flags & LO_FLAGS_READ_ONLY) != 0); 12101da177e4SLinus Torvalds 121187579e9bSDan Schatzberg INIT_WORK(&lo->rootcg_work, loop_rootcg_workfn); 121287579e9bSDan Schatzberg INIT_LIST_HEAD(&lo->rootcg_cmd_list); 121387579e9bSDan Schatzberg INIT_LIST_HEAD(&lo->idle_worker_list); 121487579e9bSDan Schatzberg lo->worker_tree = RB_ROOT; 121587579e9bSDan Schatzberg timer_setup(&lo->timer, loop_free_idle_workers, 121687579e9bSDan Schatzberg TIMER_DEFERRABLE); 12173448914eSMartijn Coenen lo->use_dio = lo->lo_flags & LO_FLAGS_DIRECT_IO; 12181da177e4SLinus Torvalds lo->lo_device = bdev; 12191da177e4SLinus Torvalds lo->lo_backing_file = file; 12201da177e4SLinus Torvalds lo->old_gfp_mask = mapping_gfp_mask(mapping); 12211da177e4SLinus Torvalds mapping_set_gfp_mask(mapping, lo->old_gfp_mask & ~(__GFP_IO|__GFP_FS)); 12221da177e4SLinus Torvalds 12233448914eSMartijn Coenen if (!(lo->lo_flags & LO_FLAGS_READ_ONLY) && file->f_op->fsync) 12241da177e4SLinus Torvalds blk_queue_write_cache(lo->lo_queue, true, false); 12251da177e4SLinus Torvalds 12263448914eSMartijn Coenen if (config->block_size) 12273448914eSMartijn Coenen bsize = config->block_size; 122896ed320dSLinus Torvalds else if ((lo->lo_backing_file->f_flags & O_DIRECT) && inode->i_sb->s_bdev) 12291da177e4SLinus Torvalds /* In case of direct I/O, match underlying block size */ 12303448914eSMartijn Coenen bsize = bdev_logical_block_size(inode->i_sb->s_bdev); 12313448914eSMartijn Coenen else 12323448914eSMartijn Coenen bsize = 512; 12331da177e4SLinus Torvalds 12341da177e4SLinus Torvalds blk_queue_logical_block_size(lo->lo_queue, bsize); 12351da177e4SLinus Torvalds blk_queue_physical_block_size(lo->lo_queue, bsize); 12361da177e4SLinus Torvalds blk_queue_io_min(lo->lo_queue, bsize); 12371da177e4SLinus Torvalds 12381da177e4SLinus Torvalds loop_update_rotational(lo); 12391da177e4SLinus Torvalds loop_update_dio(lo); 12401da177e4SLinus Torvalds loop_sysfs_init(lo); 124179e5dc59SMartijn Coenen 124279e5dc59SMartijn Coenen size = get_loop_size(lo, file); 12435795b6f5SMartijn Coenen loop_set_size(lo, size); 12441da177e4SLinus Torvalds 12451da177e4SLinus Torvalds lo->lo_state = Lo_bound; 12461da177e4SLinus Torvalds if (part_shift) 12471da177e4SLinus Torvalds lo->lo_flags |= LO_FLAGS_PARTSCAN; 12481da177e4SLinus Torvalds partscan = lo->lo_flags & LO_FLAGS_PARTSCAN; 1249fe6a8fc5SLennart Poettering if (partscan) 1250fe6a8fc5SLennart Poettering lo->lo_disk->flags &= ~GENHD_FL_NO_PART_SCAN; 12511da177e4SLinus Torvalds 12521da177e4SLinus Torvalds /* Grab the block_device to prevent its destruction after we 12531da177e4SLinus Torvalds * put /dev/loopXX inode. Later in __loop_clr_fd() we bdput(bdev). 12541da177e4SLinus Torvalds */ 12551da177e4SLinus Torvalds bdgrab(bdev); 12566cc8e743SPavel Tatashin mutex_unlock(&lo->lo_mutex); 12571da177e4SLinus Torvalds if (partscan) 12581da177e4SLinus Torvalds loop_reread_partitions(lo, bdev); 125937c3fc9aSChristoph Hellwig if (!(mode & FMODE_EXCL)) 126037c3fc9aSChristoph Hellwig bd_abort_claiming(bdev, loop_configure); 12611da177e4SLinus Torvalds return 0; 12621da177e4SLinus Torvalds 12631da177e4SLinus Torvalds out_unlock: 12646cc8e743SPavel Tatashin mutex_unlock(&lo->lo_mutex); 12651da177e4SLinus Torvalds out_bdev: 126637c3fc9aSChristoph Hellwig if (!(mode & FMODE_EXCL)) 126737c3fc9aSChristoph Hellwig bd_abort_claiming(bdev, loop_configure); 12681da177e4SLinus Torvalds out_putf: 12691da177e4SLinus Torvalds fput(file); 12701da177e4SLinus Torvalds out: 12711da177e4SLinus Torvalds /* This is safe: open() is still holding a reference. */ 12721da177e4SLinus Torvalds module_put(THIS_MODULE); 12731da177e4SLinus Torvalds return error; 12741da177e4SLinus Torvalds } 12751da177e4SLinus Torvalds 12760da03cabSJan Kara static int __loop_clr_fd(struct loop_device *lo, bool release) 12771da177e4SLinus Torvalds { 12787ccd0791SJan Kara struct file *filp = NULL; 1279b4e3ca1aSAl Viro gfp_t gfp = lo->old_gfp_mask; 12804c823cc3SAyan George struct block_device *bdev = lo->lo_device; 12817ccd0791SJan Kara int err = 0; 12820da03cabSJan Kara bool partscan = false; 12830da03cabSJan Kara int lo_number; 128487579e9bSDan Schatzberg struct loop_worker *pos, *worker; 12851da177e4SLinus Torvalds 12866cc8e743SPavel Tatashin mutex_lock(&lo->lo_mutex); 12877ccd0791SJan Kara if (WARN_ON_ONCE(lo->lo_state != Lo_rundown)) { 12887ccd0791SJan Kara err = -ENXIO; 12897ccd0791SJan Kara goto out_unlock; 12907ccd0791SJan Kara } 12911da177e4SLinus Torvalds 12927ccd0791SJan Kara filp = lo->lo_backing_file; 12937ccd0791SJan Kara if (filp == NULL) { 12947ccd0791SJan Kara err = -EINVAL; 12957ccd0791SJan Kara goto out_unlock; 12967ccd0791SJan Kara } 12971da177e4SLinus Torvalds 12984ceddce5SMauricio Faria de Oliveira if (test_bit(QUEUE_FLAG_WC, &lo->lo_queue->queue_flags)) 12994ceddce5SMauricio Faria de Oliveira blk_queue_write_cache(lo->lo_queue, false, false); 13004ceddce5SMauricio Faria de Oliveira 1301f8933667SMing Lei /* freeze request queue during the transition */ 1302f8933667SMing Lei blk_mq_freeze_queue(lo->lo_queue); 1303f8933667SMing Lei 130487579e9bSDan Schatzberg destroy_workqueue(lo->workqueue); 130587579e9bSDan Schatzberg spin_lock_irq(&lo->lo_work_lock); 130687579e9bSDan Schatzberg list_for_each_entry_safe(worker, pos, &lo->idle_worker_list, 130787579e9bSDan Schatzberg idle_list) { 130887579e9bSDan Schatzberg list_del(&worker->idle_list); 130987579e9bSDan Schatzberg rb_erase(&worker->rb_node, &lo->worker_tree); 1310*c74d40e8SDan Schatzberg css_put(worker->blkcg_css); 131187579e9bSDan Schatzberg kfree(worker); 131287579e9bSDan Schatzberg } 131387579e9bSDan Schatzberg spin_unlock_irq(&lo->lo_work_lock); 131487579e9bSDan Schatzberg del_timer_sync(&lo->timer); 131587579e9bSDan Schatzberg 13161da177e4SLinus Torvalds spin_lock_irq(&lo->lo_lock); 13171da177e4SLinus Torvalds lo->lo_backing_file = NULL; 131805eb0f25SKay Sievers spin_unlock_irq(&lo->lo_lock); 13191da177e4SLinus Torvalds 13201da177e4SLinus Torvalds loop_release_xfer(lo); 13211da177e4SLinus Torvalds lo->transfer = NULL; 13221da177e4SLinus Torvalds lo->ioctl = NULL; 13231da177e4SLinus Torvalds lo->lo_device = NULL; 13241da177e4SLinus Torvalds lo->lo_encryption = NULL; 13251da177e4SLinus Torvalds lo->lo_offset = 0; 13261da177e4SLinus Torvalds lo->lo_sizelimit = 0; 13271da177e4SLinus Torvalds lo->lo_encrypt_key_size = 0; 13281da177e4SLinus Torvalds memset(lo->lo_encrypt_key, 0, LO_KEY_SIZE); 13291da177e4SLinus Torvalds memset(lo->lo_crypt_name, 0, LO_NAME_SIZE); 13301da177e4SLinus Torvalds memset(lo->lo_file_name, 0, LO_NAME_SIZE); 133189e4fdecSOmar Sandoval blk_queue_logical_block_size(lo->lo_queue, 512); 1332bf093753SOmar Sandoval blk_queue_physical_block_size(lo->lo_queue, 512); 1333bf093753SOmar Sandoval blk_queue_io_min(lo->lo_queue, 512); 1334c1681bf8SAnatol Pomozov if (bdev) { 1335c1681bf8SAnatol Pomozov bdput(bdev); 1336f98393a6SPeter Zijlstra invalidate_bdev(bdev); 1337eedffa28SJeff Layton bdev->bd_inode->i_mapping->wb_err = 0; 1338c1681bf8SAnatol Pomozov } 133973285082SKen Chen set_capacity(lo->lo_disk, 0); 134051a0bb0cSMilan Broz loop_sysfs_exit(lo); 1341c3473c63SDavid Zeuthen if (bdev) { 1342c3473c63SDavid Zeuthen /* let user-space know about this change */ 1343c3473c63SDavid Zeuthen kobject_uevent(&disk_to_dev(bdev->bd_disk)->kobj, KOBJ_CHANGE); 1344c3473c63SDavid Zeuthen } 13451da177e4SLinus Torvalds mapping_set_gfp_mask(filp->f_mapping, gfp); 13461da177e4SLinus Torvalds /* This is safe: open() is still holding a reference. */ 13471da177e4SLinus Torvalds module_put(THIS_MODULE); 1348f8933667SMing Lei blk_mq_unfreeze_queue(lo->lo_queue); 1349f8933667SMing Lei 13500da03cabSJan Kara partscan = lo->lo_flags & LO_FLAGS_PARTSCAN && bdev; 13510da03cabSJan Kara lo_number = lo->lo_number; 13520da03cabSJan Kara out_unlock: 13536cc8e743SPavel Tatashin mutex_unlock(&lo->lo_mutex); 13540da03cabSJan Kara if (partscan) { 1355d57f3374SJan Kara /* 1356d57f3374SJan Kara * bd_mutex has been held already in release path, so don't 1357d57f3374SJan Kara * acquire it if this function is called in such case. 1358d57f3374SJan Kara * 1359d57f3374SJan Kara * If the reread partition isn't from release path, lo_refcnt 1360d57f3374SJan Kara * must be at least one and it can only become zero when the 1361d57f3374SJan Kara * current holder is released. 1362d57f3374SJan Kara */ 1363f0b870dfSChristoph Hellwig if (!release) 1364f0b870dfSChristoph Hellwig mutex_lock(&bdev->bd_mutex); 1365f0b870dfSChristoph Hellwig err = bdev_disk_changed(bdev, false); 1366f0b870dfSChristoph Hellwig if (!release) 1367f0b870dfSChristoph Hellwig mutex_unlock(&bdev->bd_mutex); 136840853d6fSDongli Zhang if (err) 1369d57f3374SJan Kara pr_warn("%s: partition scan of loop%d failed (rc=%d)\n", 13700da03cabSJan Kara __func__, lo_number, err); 1371d57f3374SJan Kara /* Device is gone, no point in returning error */ 1372d57f3374SJan Kara err = 0; 1373d57f3374SJan Kara } 1374758a58d0SDongli Zhang 1375758a58d0SDongli Zhang /* 1376758a58d0SDongli Zhang * lo->lo_state is set to Lo_unbound here after above partscan has 1377758a58d0SDongli Zhang * finished. 1378758a58d0SDongli Zhang * 1379758a58d0SDongli Zhang * There cannot be anybody else entering __loop_clr_fd() as 1380758a58d0SDongli Zhang * lo->lo_backing_file is already cleared and Lo_rundown state 1381758a58d0SDongli Zhang * protects us from all the other places trying to change the 'lo' 1382758a58d0SDongli Zhang * device. 1383758a58d0SDongli Zhang */ 13846cc8e743SPavel Tatashin mutex_lock(&lo->lo_mutex); 1385758a58d0SDongli Zhang lo->lo_flags = 0; 1386758a58d0SDongli Zhang if (!part_shift) 1387758a58d0SDongli Zhang lo->lo_disk->flags |= GENHD_FL_NO_PART_SCAN; 1388758a58d0SDongli Zhang lo->lo_state = Lo_unbound; 13896cc8e743SPavel Tatashin mutex_unlock(&lo->lo_mutex); 1390758a58d0SDongli Zhang 1391f028f3b2SNikanth Karthikesan /* 13926cc8e743SPavel Tatashin * Need not hold lo_mutex to fput backing file. Calling fput holding 13936cc8e743SPavel Tatashin * lo_mutex triggers a circular lock dependency possibility warning as 13946cc8e743SPavel Tatashin * fput can take bd_mutex which is usually taken before lo_mutex. 1395f028f3b2SNikanth Karthikesan */ 13967ccd0791SJan Kara if (filp) 1397f028f3b2SNikanth Karthikesan fput(filp); 13987ccd0791SJan Kara return err; 13991da177e4SLinus Torvalds } 14001da177e4SLinus Torvalds 1401a2505b79SJan Kara static int loop_clr_fd(struct loop_device *lo) 1402a2505b79SJan Kara { 14037ccd0791SJan Kara int err; 14047ccd0791SJan Kara 14056cc8e743SPavel Tatashin err = mutex_lock_killable(&lo->lo_mutex); 14067ccd0791SJan Kara if (err) 14077ccd0791SJan Kara return err; 14087ccd0791SJan Kara if (lo->lo_state != Lo_bound) { 14096cc8e743SPavel Tatashin mutex_unlock(&lo->lo_mutex); 1410a2505b79SJan Kara return -ENXIO; 14117ccd0791SJan Kara } 1412a2505b79SJan Kara /* 1413a2505b79SJan Kara * If we've explicitly asked to tear down the loop device, 1414a2505b79SJan Kara * and it has an elevated reference count, set it for auto-teardown when 1415a2505b79SJan Kara * the last reference goes away. This stops $!~#$@ udev from 1416a2505b79SJan Kara * preventing teardown because it decided that it needs to run blkid on 1417a2505b79SJan Kara * the loopback device whenever they appear. xfstests is notorious for 1418a2505b79SJan Kara * failing tests because blkid via udev races with a losetup 1419a2505b79SJan Kara * <dev>/do something like mkfs/losetup -d <dev> causing the losetup -d 1420a2505b79SJan Kara * command to fail with EBUSY. 1421a2505b79SJan Kara */ 1422a2505b79SJan Kara if (atomic_read(&lo->lo_refcnt) > 1) { 1423a2505b79SJan Kara lo->lo_flags |= LO_FLAGS_AUTOCLEAR; 14246cc8e743SPavel Tatashin mutex_unlock(&lo->lo_mutex); 1425a2505b79SJan Kara return 0; 1426a2505b79SJan Kara } 1427a2505b79SJan Kara lo->lo_state = Lo_rundown; 14286cc8e743SPavel Tatashin mutex_unlock(&lo->lo_mutex); 1429a2505b79SJan Kara 14300da03cabSJan Kara return __loop_clr_fd(lo, false); 1431a2505b79SJan Kara } 1432a2505b79SJan Kara 14331da177e4SLinus Torvalds static int 14341da177e4SLinus Torvalds loop_set_status(struct loop_device *lo, const struct loop_info64 *info) 14351da177e4SLinus Torvalds { 14361da177e4SLinus Torvalds int err; 143785b0a54aSJan Kara struct block_device *bdev; 14380c3796c2SMartijn Coenen kuid_t uid = current_uid(); 1439faf1d254SMartijn Coenen int prev_lo_flags; 144085b0a54aSJan Kara bool partscan = false; 14410c3796c2SMartijn Coenen bool size_changed = false; 14421da177e4SLinus Torvalds 14436cc8e743SPavel Tatashin err = mutex_lock_killable(&lo->lo_mutex); 1444550df5fdSJan Kara if (err) 1445550df5fdSJan Kara return err; 1446b0fafa81SDavid Howells if (lo->lo_encrypt_key_size && 1447e4849737SEric W. Biederman !uid_eq(lo->lo_key_owner, uid) && 1448550df5fdSJan Kara !capable(CAP_SYS_ADMIN)) { 1449550df5fdSJan Kara err = -EPERM; 1450550df5fdSJan Kara goto out_unlock; 1451550df5fdSJan Kara } 1452550df5fdSJan Kara if (lo->lo_state != Lo_bound) { 1453550df5fdSJan Kara err = -ENXIO; 1454550df5fdSJan Kara goto out_unlock; 1455550df5fdSJan Kara } 14561da177e4SLinus Torvalds 14575db470e2SJaegeuk Kim if (lo->lo_offset != info->lo_offset || 14585db470e2SJaegeuk Kim lo->lo_sizelimit != info->lo_sizelimit) { 14590c3796c2SMartijn Coenen size_changed = true; 14605db470e2SJaegeuk Kim sync_blockdev(lo->lo_device); 1461f4bd34b1SZheng Bin invalidate_bdev(lo->lo_device); 14625db470e2SJaegeuk Kim } 14635db470e2SJaegeuk Kim 1464ecdd0959SMing Lei /* I/O need to be drained during transfer transition */ 1465ecdd0959SMing Lei blk_mq_freeze_queue(lo->lo_queue); 1466ecdd0959SMing Lei 14670c3796c2SMartijn Coenen if (size_changed && lo->lo_device->bd_inode->i_mapping->nrpages) { 1468f4bd34b1SZheng Bin /* If any pages were dirtied after invalidate_bdev(), try again */ 14695db470e2SJaegeuk Kim err = -EAGAIN; 14705db470e2SJaegeuk Kim pr_warn("%s: loop%d (%s) has still dirty pages (nrpages=%lu)\n", 14715db470e2SJaegeuk Kim __func__, lo->lo_number, lo->lo_file_name, 14725db470e2SJaegeuk Kim lo->lo_device->bd_inode->i_mapping->nrpages); 14735db470e2SJaegeuk Kim goto out_unfreeze; 14745db470e2SJaegeuk Kim } 14750c3796c2SMartijn Coenen 1476faf1d254SMartijn Coenen prev_lo_flags = lo->lo_flags; 1477faf1d254SMartijn Coenen 14780c3796c2SMartijn Coenen err = loop_set_status_from_info(lo, info); 14790c3796c2SMartijn Coenen if (err) 1480550df5fdSJan Kara goto out_unfreeze; 14810c3796c2SMartijn Coenen 1482faf1d254SMartijn Coenen /* Mask out flags that can't be set using LOOP_SET_STATUS. */ 14836ac92fb5SMartijn Coenen lo->lo_flags &= LOOP_SET_STATUS_SETTABLE_FLAGS; 1484faf1d254SMartijn Coenen /* For those flags, use the previous values instead */ 1485faf1d254SMartijn Coenen lo->lo_flags |= prev_lo_flags & ~LOOP_SET_STATUS_SETTABLE_FLAGS; 1486faf1d254SMartijn Coenen /* For flags that can't be cleared, use previous values too */ 1487faf1d254SMartijn Coenen lo->lo_flags |= prev_lo_flags & ~LOOP_SET_STATUS_CLEARABLE_FLAGS; 1488faf1d254SMartijn Coenen 1489b0bd158dSMartijn Coenen if (size_changed) { 1490b0bd158dSMartijn Coenen loff_t new_size = get_size(lo->lo_offset, lo->lo_sizelimit, 1491b0bd158dSMartijn Coenen lo->lo_backing_file); 1492b0bd158dSMartijn Coenen loop_set_size(lo, new_size); 1493b040ad9cSArnd Bergmann } 1494541c742aSGuo Chao 1495dfaa2ef6SLukas Czerner loop_config_discard(lo); 14961da177e4SLinus Torvalds 14972e5ab5f3SMing Lei /* update dio if lo_offset or transfer is changed */ 14982e5ab5f3SMing Lei __loop_update_dio(lo, lo->use_dio); 14992e5ab5f3SMing Lei 1500550df5fdSJan Kara out_unfreeze: 1501ecdd0959SMing Lei blk_mq_unfreeze_queue(lo->lo_queue); 1502e02898b4SOmar Sandoval 1503faf1d254SMartijn Coenen if (!err && (lo->lo_flags & LO_FLAGS_PARTSCAN) && 1504faf1d254SMartijn Coenen !(prev_lo_flags & LO_FLAGS_PARTSCAN)) { 1505e02898b4SOmar Sandoval lo->lo_disk->flags &= ~GENHD_FL_NO_PART_SCAN; 150685b0a54aSJan Kara bdev = lo->lo_device; 150785b0a54aSJan Kara partscan = true; 1508e02898b4SOmar Sandoval } 1509550df5fdSJan Kara out_unlock: 15106cc8e743SPavel Tatashin mutex_unlock(&lo->lo_mutex); 151185b0a54aSJan Kara if (partscan) 151285b0a54aSJan Kara loop_reread_partitions(lo, bdev); 1513e02898b4SOmar Sandoval 1514ecdd0959SMing Lei return err; 15151da177e4SLinus Torvalds } 15161da177e4SLinus Torvalds 15171da177e4SLinus Torvalds static int 15181da177e4SLinus Torvalds loop_get_status(struct loop_device *lo, struct loop_info64 *info) 15191da177e4SLinus Torvalds { 1520b1ab5fa3STetsuo Handa struct path path; 15211da177e4SLinus Torvalds struct kstat stat; 15222d1d4c1eSOmar Sandoval int ret; 15231da177e4SLinus Torvalds 15246cc8e743SPavel Tatashin ret = mutex_lock_killable(&lo->lo_mutex); 15254a5ce9baSJan Kara if (ret) 15264a5ce9baSJan Kara return ret; 15272d1d4c1eSOmar Sandoval if (lo->lo_state != Lo_bound) { 15286cc8e743SPavel Tatashin mutex_unlock(&lo->lo_mutex); 15291da177e4SLinus Torvalds return -ENXIO; 15302d1d4c1eSOmar Sandoval } 15312d1d4c1eSOmar Sandoval 15321da177e4SLinus Torvalds memset(info, 0, sizeof(*info)); 15331da177e4SLinus Torvalds info->lo_number = lo->lo_number; 15341da177e4SLinus Torvalds info->lo_offset = lo->lo_offset; 15351da177e4SLinus Torvalds info->lo_sizelimit = lo->lo_sizelimit; 15361da177e4SLinus Torvalds info->lo_flags = lo->lo_flags; 15371da177e4SLinus Torvalds memcpy(info->lo_file_name, lo->lo_file_name, LO_NAME_SIZE); 15381da177e4SLinus Torvalds memcpy(info->lo_crypt_name, lo->lo_crypt_name, LO_NAME_SIZE); 15391da177e4SLinus Torvalds info->lo_encrypt_type = 15401da177e4SLinus Torvalds lo->lo_encryption ? lo->lo_encryption->number : 0; 15411da177e4SLinus Torvalds if (lo->lo_encrypt_key_size && capable(CAP_SYS_ADMIN)) { 15421da177e4SLinus Torvalds info->lo_encrypt_key_size = lo->lo_encrypt_key_size; 15431da177e4SLinus Torvalds memcpy(info->lo_encrypt_key, lo->lo_encrypt_key, 15441da177e4SLinus Torvalds lo->lo_encrypt_key_size); 15451da177e4SLinus Torvalds } 15462d1d4c1eSOmar Sandoval 15476cc8e743SPavel Tatashin /* Drop lo_mutex while we call into the filesystem. */ 1548b1ab5fa3STetsuo Handa path = lo->lo_backing_file->f_path; 1549b1ab5fa3STetsuo Handa path_get(&path); 15506cc8e743SPavel Tatashin mutex_unlock(&lo->lo_mutex); 1551b1ab5fa3STetsuo Handa ret = vfs_getattr(&path, &stat, STATX_INO, AT_STATX_SYNC_AS_STAT); 15522d1d4c1eSOmar Sandoval if (!ret) { 15532d1d4c1eSOmar Sandoval info->lo_device = huge_encode_dev(stat.dev); 15542d1d4c1eSOmar Sandoval info->lo_inode = stat.ino; 15552d1d4c1eSOmar Sandoval info->lo_rdevice = huge_encode_dev(stat.rdev); 15562d1d4c1eSOmar Sandoval } 1557b1ab5fa3STetsuo Handa path_put(&path); 15582d1d4c1eSOmar Sandoval return ret; 15591da177e4SLinus Torvalds } 15601da177e4SLinus Torvalds 15611da177e4SLinus Torvalds static void 15621da177e4SLinus Torvalds loop_info64_from_old(const struct loop_info *info, struct loop_info64 *info64) 15631da177e4SLinus Torvalds { 15641da177e4SLinus Torvalds memset(info64, 0, sizeof(*info64)); 15651da177e4SLinus Torvalds info64->lo_number = info->lo_number; 15661da177e4SLinus Torvalds info64->lo_device = info->lo_device; 15671da177e4SLinus Torvalds info64->lo_inode = info->lo_inode; 15681da177e4SLinus Torvalds info64->lo_rdevice = info->lo_rdevice; 15691da177e4SLinus Torvalds info64->lo_offset = info->lo_offset; 15701da177e4SLinus Torvalds info64->lo_sizelimit = 0; 15711da177e4SLinus Torvalds info64->lo_encrypt_type = info->lo_encrypt_type; 15721da177e4SLinus Torvalds info64->lo_encrypt_key_size = info->lo_encrypt_key_size; 15731da177e4SLinus Torvalds info64->lo_flags = info->lo_flags; 15741da177e4SLinus Torvalds info64->lo_init[0] = info->lo_init[0]; 15751da177e4SLinus Torvalds info64->lo_init[1] = info->lo_init[1]; 15761da177e4SLinus Torvalds if (info->lo_encrypt_type == LO_CRYPT_CRYPTOAPI) 15771da177e4SLinus Torvalds memcpy(info64->lo_crypt_name, info->lo_name, LO_NAME_SIZE); 15781da177e4SLinus Torvalds else 15791da177e4SLinus Torvalds memcpy(info64->lo_file_name, info->lo_name, LO_NAME_SIZE); 15801da177e4SLinus Torvalds memcpy(info64->lo_encrypt_key, info->lo_encrypt_key, LO_KEY_SIZE); 15811da177e4SLinus Torvalds } 15821da177e4SLinus Torvalds 15831da177e4SLinus Torvalds static int 15841da177e4SLinus Torvalds loop_info64_to_old(const struct loop_info64 *info64, struct loop_info *info) 15851da177e4SLinus Torvalds { 15861da177e4SLinus Torvalds memset(info, 0, sizeof(*info)); 15871da177e4SLinus Torvalds info->lo_number = info64->lo_number; 15881da177e4SLinus Torvalds info->lo_device = info64->lo_device; 15891da177e4SLinus Torvalds info->lo_inode = info64->lo_inode; 15901da177e4SLinus Torvalds info->lo_rdevice = info64->lo_rdevice; 15911da177e4SLinus Torvalds info->lo_offset = info64->lo_offset; 15921da177e4SLinus Torvalds info->lo_encrypt_type = info64->lo_encrypt_type; 15931da177e4SLinus Torvalds info->lo_encrypt_key_size = info64->lo_encrypt_key_size; 15941da177e4SLinus Torvalds info->lo_flags = info64->lo_flags; 15951da177e4SLinus Torvalds info->lo_init[0] = info64->lo_init[0]; 15961da177e4SLinus Torvalds info->lo_init[1] = info64->lo_init[1]; 15971da177e4SLinus Torvalds if (info->lo_encrypt_type == LO_CRYPT_CRYPTOAPI) 15981da177e4SLinus Torvalds memcpy(info->lo_name, info64->lo_crypt_name, LO_NAME_SIZE); 15991da177e4SLinus Torvalds else 16001da177e4SLinus Torvalds memcpy(info->lo_name, info64->lo_file_name, LO_NAME_SIZE); 16011da177e4SLinus Torvalds memcpy(info->lo_encrypt_key, info64->lo_encrypt_key, LO_KEY_SIZE); 16021da177e4SLinus Torvalds 16031da177e4SLinus Torvalds /* error in case values were truncated */ 16041da177e4SLinus Torvalds if (info->lo_device != info64->lo_device || 16051da177e4SLinus Torvalds info->lo_rdevice != info64->lo_rdevice || 16061da177e4SLinus Torvalds info->lo_inode != info64->lo_inode || 16071da177e4SLinus Torvalds info->lo_offset != info64->lo_offset) 16081da177e4SLinus Torvalds return -EOVERFLOW; 16091da177e4SLinus Torvalds 16101da177e4SLinus Torvalds return 0; 16111da177e4SLinus Torvalds } 16121da177e4SLinus Torvalds 16131da177e4SLinus Torvalds static int 16141da177e4SLinus Torvalds loop_set_status_old(struct loop_device *lo, const struct loop_info __user *arg) 16151da177e4SLinus Torvalds { 16161da177e4SLinus Torvalds struct loop_info info; 16171da177e4SLinus Torvalds struct loop_info64 info64; 16181da177e4SLinus Torvalds 16191da177e4SLinus Torvalds if (copy_from_user(&info, arg, sizeof (struct loop_info))) 16201da177e4SLinus Torvalds return -EFAULT; 16211da177e4SLinus Torvalds loop_info64_from_old(&info, &info64); 16221da177e4SLinus Torvalds return loop_set_status(lo, &info64); 16231da177e4SLinus Torvalds } 16241da177e4SLinus Torvalds 16251da177e4SLinus Torvalds static int 16261da177e4SLinus Torvalds loop_set_status64(struct loop_device *lo, const struct loop_info64 __user *arg) 16271da177e4SLinus Torvalds { 16281da177e4SLinus Torvalds struct loop_info64 info64; 16291da177e4SLinus Torvalds 16301da177e4SLinus Torvalds if (copy_from_user(&info64, arg, sizeof (struct loop_info64))) 16311da177e4SLinus Torvalds return -EFAULT; 16321da177e4SLinus Torvalds return loop_set_status(lo, &info64); 16331da177e4SLinus Torvalds } 16341da177e4SLinus Torvalds 16351da177e4SLinus Torvalds static int 16361da177e4SLinus Torvalds loop_get_status_old(struct loop_device *lo, struct loop_info __user *arg) { 16371da177e4SLinus Torvalds struct loop_info info; 16381da177e4SLinus Torvalds struct loop_info64 info64; 1639bdac616dSOmar Sandoval int err; 16401da177e4SLinus Torvalds 16414a5ce9baSJan Kara if (!arg) 1642bdac616dSOmar Sandoval return -EINVAL; 16431da177e4SLinus Torvalds err = loop_get_status(lo, &info64); 16441da177e4SLinus Torvalds if (!err) 16451da177e4SLinus Torvalds err = loop_info64_to_old(&info64, &info); 16461da177e4SLinus Torvalds if (!err && copy_to_user(arg, &info, sizeof(info))) 16471da177e4SLinus Torvalds err = -EFAULT; 16481da177e4SLinus Torvalds 16491da177e4SLinus Torvalds return err; 16501da177e4SLinus Torvalds } 16511da177e4SLinus Torvalds 16521da177e4SLinus Torvalds static int 16531da177e4SLinus Torvalds loop_get_status64(struct loop_device *lo, struct loop_info64 __user *arg) { 16541da177e4SLinus Torvalds struct loop_info64 info64; 1655bdac616dSOmar Sandoval int err; 16561da177e4SLinus Torvalds 16574a5ce9baSJan Kara if (!arg) 1658bdac616dSOmar Sandoval return -EINVAL; 16591da177e4SLinus Torvalds err = loop_get_status(lo, &info64); 16601da177e4SLinus Torvalds if (!err && copy_to_user(arg, &info64, sizeof(info64))) 16611da177e4SLinus Torvalds err = -EFAULT; 16621da177e4SLinus Torvalds 16631da177e4SLinus Torvalds return err; 16641da177e4SLinus Torvalds } 16651da177e4SLinus Torvalds 166651001b7dSHannes Reinecke static int loop_set_capacity(struct loop_device *lo) 166753d66608SJ. R. Okajima { 16680a6ed1b5SMartijn Coenen loff_t size; 16690a6ed1b5SMartijn Coenen 167053d66608SJ. R. Okajima if (unlikely(lo->lo_state != Lo_bound)) 16717b0576a3SGuo Chao return -ENXIO; 167253d66608SJ. R. Okajima 16730a6ed1b5SMartijn Coenen size = get_loop_size(lo, lo->lo_backing_file); 16740a6ed1b5SMartijn Coenen loop_set_size(lo, size); 1675083a6a50SMartijn Coenen 1676083a6a50SMartijn Coenen return 0; 167753d66608SJ. R. Okajima } 167853d66608SJ. R. Okajima 1679ab1cb278SMing Lei static int loop_set_dio(struct loop_device *lo, unsigned long arg) 1680ab1cb278SMing Lei { 1681ab1cb278SMing Lei int error = -ENXIO; 1682ab1cb278SMing Lei if (lo->lo_state != Lo_bound) 1683ab1cb278SMing Lei goto out; 1684ab1cb278SMing Lei 1685ab1cb278SMing Lei __loop_update_dio(lo, !!arg); 1686ab1cb278SMing Lei if (lo->use_dio == !!arg) 1687ab1cb278SMing Lei return 0; 1688ab1cb278SMing Lei error = -EINVAL; 1689ab1cb278SMing Lei out: 1690ab1cb278SMing Lei return error; 1691ab1cb278SMing Lei } 1692ab1cb278SMing Lei 169389e4fdecSOmar Sandoval static int loop_set_block_size(struct loop_device *lo, unsigned long arg) 169489e4fdecSOmar Sandoval { 16955db470e2SJaegeuk Kim int err = 0; 16965db470e2SJaegeuk Kim 169789e4fdecSOmar Sandoval if (lo->lo_state != Lo_bound) 169889e4fdecSOmar Sandoval return -ENXIO; 169989e4fdecSOmar Sandoval 17003448914eSMartijn Coenen err = loop_validate_block_size(arg); 17013448914eSMartijn Coenen if (err) 17023448914eSMartijn Coenen return err; 170389e4fdecSOmar Sandoval 17047e81f99aSMartijn Coenen if (lo->lo_queue->limits.logical_block_size == arg) 17057e81f99aSMartijn Coenen return 0; 17067e81f99aSMartijn Coenen 17075db470e2SJaegeuk Kim sync_blockdev(lo->lo_device); 1708f4bd34b1SZheng Bin invalidate_bdev(lo->lo_device); 17095db470e2SJaegeuk Kim 171089e4fdecSOmar Sandoval blk_mq_freeze_queue(lo->lo_queue); 171189e4fdecSOmar Sandoval 1712f4bd34b1SZheng Bin /* invalidate_bdev should have truncated all the pages */ 17137e81f99aSMartijn Coenen if (lo->lo_device->bd_inode->i_mapping->nrpages) { 17145db470e2SJaegeuk Kim err = -EAGAIN; 17155db470e2SJaegeuk Kim pr_warn("%s: loop%d (%s) has still dirty pages (nrpages=%lu)\n", 17165db470e2SJaegeuk Kim __func__, lo->lo_number, lo->lo_file_name, 17175db470e2SJaegeuk Kim lo->lo_device->bd_inode->i_mapping->nrpages); 17185db470e2SJaegeuk Kim goto out_unfreeze; 17195db470e2SJaegeuk Kim } 17205db470e2SJaegeuk Kim 172189e4fdecSOmar Sandoval blk_queue_logical_block_size(lo->lo_queue, arg); 1722bf093753SOmar Sandoval blk_queue_physical_block_size(lo->lo_queue, arg); 1723bf093753SOmar Sandoval blk_queue_io_min(lo->lo_queue, arg); 172489e4fdecSOmar Sandoval loop_update_dio(lo); 17255db470e2SJaegeuk Kim out_unfreeze: 172689e4fdecSOmar Sandoval blk_mq_unfreeze_queue(lo->lo_queue); 172789e4fdecSOmar Sandoval 17285db470e2SJaegeuk Kim return err; 172989e4fdecSOmar Sandoval } 173089e4fdecSOmar Sandoval 1731a1316544SJan Kara static int lo_simple_ioctl(struct loop_device *lo, unsigned int cmd, 1732a1316544SJan Kara unsigned long arg) 17331da177e4SLinus Torvalds { 17341da177e4SLinus Torvalds int err; 17351da177e4SLinus Torvalds 17366cc8e743SPavel Tatashin err = mutex_lock_killable(&lo->lo_mutex); 17373148ffbdSOmar Sandoval if (err) 1738a1316544SJan Kara return err; 17391da177e4SLinus Torvalds switch (cmd) { 174053d66608SJ. R. Okajima case LOOP_SET_CAPACITY: 174151001b7dSHannes Reinecke err = loop_set_capacity(lo); 174253d66608SJ. R. Okajima break; 1743ab1cb278SMing Lei case LOOP_SET_DIRECT_IO: 1744ab1cb278SMing Lei err = loop_set_dio(lo, arg); 1745ab1cb278SMing Lei break; 174689e4fdecSOmar Sandoval case LOOP_SET_BLOCK_SIZE: 174789e4fdecSOmar Sandoval err = loop_set_block_size(lo, arg); 174889e4fdecSOmar Sandoval break; 17491da177e4SLinus Torvalds default: 17501da177e4SLinus Torvalds err = lo->ioctl ? lo->ioctl(lo, cmd, arg) : -EINVAL; 17511da177e4SLinus Torvalds } 17526cc8e743SPavel Tatashin mutex_unlock(&lo->lo_mutex); 1753a1316544SJan Kara return err; 1754a1316544SJan Kara } 1755f028f3b2SNikanth Karthikesan 1756a1316544SJan Kara static int lo_ioctl(struct block_device *bdev, fmode_t mode, 1757a1316544SJan Kara unsigned int cmd, unsigned long arg) 1758a1316544SJan Kara { 1759a1316544SJan Kara struct loop_device *lo = bdev->bd_disk->private_data; 1760571fae6eSMartijn Coenen void __user *argp = (void __user *) arg; 1761a1316544SJan Kara int err; 1762a1316544SJan Kara 1763a1316544SJan Kara switch (cmd) { 17643448914eSMartijn Coenen case LOOP_SET_FD: { 17653448914eSMartijn Coenen /* 17663448914eSMartijn Coenen * Legacy case - pass in a zeroed out struct loop_config with 17673448914eSMartijn Coenen * only the file descriptor set , which corresponds with the 17683448914eSMartijn Coenen * default parameters we'd have used otherwise. 17693448914eSMartijn Coenen */ 17703448914eSMartijn Coenen struct loop_config config; 17713448914eSMartijn Coenen 17723448914eSMartijn Coenen memset(&config, 0, sizeof(config)); 17733448914eSMartijn Coenen config.fd = arg; 17743448914eSMartijn Coenen 17753448914eSMartijn Coenen return loop_configure(lo, mode, bdev, &config); 17763448914eSMartijn Coenen } 17773448914eSMartijn Coenen case LOOP_CONFIGURE: { 17783448914eSMartijn Coenen struct loop_config config; 17793448914eSMartijn Coenen 17803448914eSMartijn Coenen if (copy_from_user(&config, argp, sizeof(config))) 17813448914eSMartijn Coenen return -EFAULT; 17823448914eSMartijn Coenen 17833448914eSMartijn Coenen return loop_configure(lo, mode, bdev, &config); 17843448914eSMartijn Coenen } 1785a1316544SJan Kara case LOOP_CHANGE_FD: 1786c3710770SJan Kara return loop_change_fd(lo, bdev, arg); 1787a1316544SJan Kara case LOOP_CLR_FD: 17887ccd0791SJan Kara return loop_clr_fd(lo); 1789a1316544SJan Kara case LOOP_SET_STATUS: 1790a1316544SJan Kara err = -EPERM; 1791a1316544SJan Kara if ((mode & FMODE_WRITE) || capable(CAP_SYS_ADMIN)) { 1792571fae6eSMartijn Coenen err = loop_set_status_old(lo, argp); 1793a1316544SJan Kara } 1794a1316544SJan Kara break; 1795a1316544SJan Kara case LOOP_GET_STATUS: 1796571fae6eSMartijn Coenen return loop_get_status_old(lo, argp); 1797a1316544SJan Kara case LOOP_SET_STATUS64: 1798a1316544SJan Kara err = -EPERM; 1799a1316544SJan Kara if ((mode & FMODE_WRITE) || capable(CAP_SYS_ADMIN)) { 1800571fae6eSMartijn Coenen err = loop_set_status64(lo, argp); 1801a1316544SJan Kara } 1802a1316544SJan Kara break; 1803a1316544SJan Kara case LOOP_GET_STATUS64: 1804571fae6eSMartijn Coenen return loop_get_status64(lo, argp); 1805a1316544SJan Kara case LOOP_SET_CAPACITY: 1806a1316544SJan Kara case LOOP_SET_DIRECT_IO: 1807a1316544SJan Kara case LOOP_SET_BLOCK_SIZE: 1808a1316544SJan Kara if (!(mode & FMODE_WRITE) && !capable(CAP_SYS_ADMIN)) 1809a1316544SJan Kara return -EPERM; 1810df561f66SGustavo A. R. Silva fallthrough; 1811a1316544SJan Kara default: 1812a1316544SJan Kara err = lo_simple_ioctl(lo, cmd, arg); 1813a1316544SJan Kara break; 1814a1316544SJan Kara } 1815a1316544SJan Kara 18161da177e4SLinus Torvalds return err; 18171da177e4SLinus Torvalds } 18181da177e4SLinus Torvalds 1819863d5b82SDavid Howells #ifdef CONFIG_COMPAT 1820863d5b82SDavid Howells struct compat_loop_info { 1821863d5b82SDavid Howells compat_int_t lo_number; /* ioctl r/o */ 1822863d5b82SDavid Howells compat_dev_t lo_device; /* ioctl r/o */ 1823863d5b82SDavid Howells compat_ulong_t lo_inode; /* ioctl r/o */ 1824863d5b82SDavid Howells compat_dev_t lo_rdevice; /* ioctl r/o */ 1825863d5b82SDavid Howells compat_int_t lo_offset; 1826863d5b82SDavid Howells compat_int_t lo_encrypt_type; 1827863d5b82SDavid Howells compat_int_t lo_encrypt_key_size; /* ioctl w/o */ 1828863d5b82SDavid Howells compat_int_t lo_flags; /* ioctl r/o */ 1829863d5b82SDavid Howells char lo_name[LO_NAME_SIZE]; 1830863d5b82SDavid Howells unsigned char lo_encrypt_key[LO_KEY_SIZE]; /* ioctl w/o */ 1831863d5b82SDavid Howells compat_ulong_t lo_init[2]; 1832863d5b82SDavid Howells char reserved[4]; 1833863d5b82SDavid Howells }; 1834863d5b82SDavid Howells 1835863d5b82SDavid Howells /* 1836863d5b82SDavid Howells * Transfer 32-bit compatibility structure in userspace to 64-bit loop info 1837863d5b82SDavid Howells * - noinlined to reduce stack space usage in main part of driver 1838863d5b82SDavid Howells */ 1839863d5b82SDavid Howells static noinline int 1840ba674cfcSAl Viro loop_info64_from_compat(const struct compat_loop_info __user *arg, 1841863d5b82SDavid Howells struct loop_info64 *info64) 1842863d5b82SDavid Howells { 1843863d5b82SDavid Howells struct compat_loop_info info; 1844863d5b82SDavid Howells 1845863d5b82SDavid Howells if (copy_from_user(&info, arg, sizeof(info))) 1846863d5b82SDavid Howells return -EFAULT; 1847863d5b82SDavid Howells 1848863d5b82SDavid Howells memset(info64, 0, sizeof(*info64)); 1849863d5b82SDavid Howells info64->lo_number = info.lo_number; 1850863d5b82SDavid Howells info64->lo_device = info.lo_device; 1851863d5b82SDavid Howells info64->lo_inode = info.lo_inode; 1852863d5b82SDavid Howells info64->lo_rdevice = info.lo_rdevice; 1853863d5b82SDavid Howells info64->lo_offset = info.lo_offset; 1854863d5b82SDavid Howells info64->lo_sizelimit = 0; 1855863d5b82SDavid Howells info64->lo_encrypt_type = info.lo_encrypt_type; 1856863d5b82SDavid Howells info64->lo_encrypt_key_size = info.lo_encrypt_key_size; 1857863d5b82SDavid Howells info64->lo_flags = info.lo_flags; 1858863d5b82SDavid Howells info64->lo_init[0] = info.lo_init[0]; 1859863d5b82SDavid Howells info64->lo_init[1] = info.lo_init[1]; 1860863d5b82SDavid Howells if (info.lo_encrypt_type == LO_CRYPT_CRYPTOAPI) 1861863d5b82SDavid Howells memcpy(info64->lo_crypt_name, info.lo_name, LO_NAME_SIZE); 1862863d5b82SDavid Howells else 1863863d5b82SDavid Howells memcpy(info64->lo_file_name, info.lo_name, LO_NAME_SIZE); 1864863d5b82SDavid Howells memcpy(info64->lo_encrypt_key, info.lo_encrypt_key, LO_KEY_SIZE); 1865863d5b82SDavid Howells return 0; 1866863d5b82SDavid Howells } 1867863d5b82SDavid Howells 1868863d5b82SDavid Howells /* 1869863d5b82SDavid Howells * Transfer 64-bit loop info to 32-bit compatibility structure in userspace 1870863d5b82SDavid Howells * - noinlined to reduce stack space usage in main part of driver 1871863d5b82SDavid Howells */ 1872863d5b82SDavid Howells static noinline int 1873863d5b82SDavid Howells loop_info64_to_compat(const struct loop_info64 *info64, 1874863d5b82SDavid Howells struct compat_loop_info __user *arg) 1875863d5b82SDavid Howells { 1876863d5b82SDavid Howells struct compat_loop_info info; 1877863d5b82SDavid Howells 1878863d5b82SDavid Howells memset(&info, 0, sizeof(info)); 1879863d5b82SDavid Howells info.lo_number = info64->lo_number; 1880863d5b82SDavid Howells info.lo_device = info64->lo_device; 1881863d5b82SDavid Howells info.lo_inode = info64->lo_inode; 1882863d5b82SDavid Howells info.lo_rdevice = info64->lo_rdevice; 1883863d5b82SDavid Howells info.lo_offset = info64->lo_offset; 1884863d5b82SDavid Howells info.lo_encrypt_type = info64->lo_encrypt_type; 1885863d5b82SDavid Howells info.lo_encrypt_key_size = info64->lo_encrypt_key_size; 1886863d5b82SDavid Howells info.lo_flags = info64->lo_flags; 1887863d5b82SDavid Howells info.lo_init[0] = info64->lo_init[0]; 1888863d5b82SDavid Howells info.lo_init[1] = info64->lo_init[1]; 1889863d5b82SDavid Howells if (info.lo_encrypt_type == LO_CRYPT_CRYPTOAPI) 1890863d5b82SDavid Howells memcpy(info.lo_name, info64->lo_crypt_name, LO_NAME_SIZE); 1891863d5b82SDavid Howells else 1892863d5b82SDavid Howells memcpy(info.lo_name, info64->lo_file_name, LO_NAME_SIZE); 1893863d5b82SDavid Howells memcpy(info.lo_encrypt_key, info64->lo_encrypt_key, LO_KEY_SIZE); 1894863d5b82SDavid Howells 1895863d5b82SDavid Howells /* error in case values were truncated */ 1896863d5b82SDavid Howells if (info.lo_device != info64->lo_device || 1897863d5b82SDavid Howells info.lo_rdevice != info64->lo_rdevice || 1898863d5b82SDavid Howells info.lo_inode != info64->lo_inode || 1899863d5b82SDavid Howells info.lo_offset != info64->lo_offset || 1900863d5b82SDavid Howells info.lo_init[0] != info64->lo_init[0] || 1901863d5b82SDavid Howells info.lo_init[1] != info64->lo_init[1]) 1902863d5b82SDavid Howells return -EOVERFLOW; 1903863d5b82SDavid Howells 1904863d5b82SDavid Howells if (copy_to_user(arg, &info, sizeof(info))) 1905863d5b82SDavid Howells return -EFAULT; 1906863d5b82SDavid Howells return 0; 1907863d5b82SDavid Howells } 1908863d5b82SDavid Howells 1909863d5b82SDavid Howells static int 1910863d5b82SDavid Howells loop_set_status_compat(struct loop_device *lo, 1911863d5b82SDavid Howells const struct compat_loop_info __user *arg) 1912863d5b82SDavid Howells { 1913863d5b82SDavid Howells struct loop_info64 info64; 1914863d5b82SDavid Howells int ret; 1915863d5b82SDavid Howells 1916863d5b82SDavid Howells ret = loop_info64_from_compat(arg, &info64); 1917863d5b82SDavid Howells if (ret < 0) 1918863d5b82SDavid Howells return ret; 1919863d5b82SDavid Howells return loop_set_status(lo, &info64); 1920863d5b82SDavid Howells } 1921863d5b82SDavid Howells 1922863d5b82SDavid Howells static int 1923863d5b82SDavid Howells loop_get_status_compat(struct loop_device *lo, 1924863d5b82SDavid Howells struct compat_loop_info __user *arg) 1925863d5b82SDavid Howells { 1926863d5b82SDavid Howells struct loop_info64 info64; 1927bdac616dSOmar Sandoval int err; 1928863d5b82SDavid Howells 19294a5ce9baSJan Kara if (!arg) 1930bdac616dSOmar Sandoval return -EINVAL; 1931863d5b82SDavid Howells err = loop_get_status(lo, &info64); 1932863d5b82SDavid Howells if (!err) 1933863d5b82SDavid Howells err = loop_info64_to_compat(&info64, arg); 1934863d5b82SDavid Howells return err; 1935863d5b82SDavid Howells } 1936863d5b82SDavid Howells 1937bb214884SAl Viro static int lo_compat_ioctl(struct block_device *bdev, fmode_t mode, 1938bb214884SAl Viro unsigned int cmd, unsigned long arg) 1939863d5b82SDavid Howells { 1940bb214884SAl Viro struct loop_device *lo = bdev->bd_disk->private_data; 1941863d5b82SDavid Howells int err; 1942863d5b82SDavid Howells 1943863d5b82SDavid Howells switch(cmd) { 1944863d5b82SDavid Howells case LOOP_SET_STATUS: 19453148ffbdSOmar Sandoval err = loop_set_status_compat(lo, 19463148ffbdSOmar Sandoval (const struct compat_loop_info __user *)arg); 1947863d5b82SDavid Howells break; 1948863d5b82SDavid Howells case LOOP_GET_STATUS: 19493148ffbdSOmar Sandoval err = loop_get_status_compat(lo, 19503148ffbdSOmar Sandoval (struct compat_loop_info __user *)arg); 1951863d5b82SDavid Howells break; 195253d66608SJ. R. Okajima case LOOP_SET_CAPACITY: 1953863d5b82SDavid Howells case LOOP_CLR_FD: 1954863d5b82SDavid Howells case LOOP_GET_STATUS64: 1955863d5b82SDavid Howells case LOOP_SET_STATUS64: 19563448914eSMartijn Coenen case LOOP_CONFIGURE: 1957863d5b82SDavid Howells arg = (unsigned long) compat_ptr(arg); 1958df561f66SGustavo A. R. Silva fallthrough; 1959863d5b82SDavid Howells case LOOP_SET_FD: 1960863d5b82SDavid Howells case LOOP_CHANGE_FD: 19619fea4b39SEvan Green case LOOP_SET_BLOCK_SIZE: 1962fdbe4eeeSAlessio Balsini case LOOP_SET_DIRECT_IO: 1963bb214884SAl Viro err = lo_ioctl(bdev, mode, cmd, arg); 1964863d5b82SDavid Howells break; 1965863d5b82SDavid Howells default: 1966863d5b82SDavid Howells err = -ENOIOCTLCMD; 1967863d5b82SDavid Howells break; 1968863d5b82SDavid Howells } 1969863d5b82SDavid Howells return err; 1970863d5b82SDavid Howells } 1971863d5b82SDavid Howells #endif 1972863d5b82SDavid Howells 1973bb214884SAl Viro static int lo_open(struct block_device *bdev, fmode_t mode) 19741da177e4SLinus Torvalds { 1975990e7811SChristoph Hellwig struct loop_device *lo = bdev->bd_disk->private_data; 19760a42e99bSJan Kara int err; 1977770fe30aSKay Sievers 19786cc8e743SPavel Tatashin err = mutex_lock_killable(&lo->lo_mutex); 19796cc8e743SPavel Tatashin if (err) 1980770fe30aSKay Sievers return err; 1981990e7811SChristoph Hellwig if (lo->lo_state == Lo_deleting) 1982990e7811SChristoph Hellwig err = -ENXIO; 1983990e7811SChristoph Hellwig else 19846cc8e743SPavel Tatashin atomic_inc(&lo->lo_refcnt); 19856cc8e743SPavel Tatashin mutex_unlock(&lo->lo_mutex); 1986990e7811SChristoph Hellwig return err; 19871da177e4SLinus Torvalds } 19881da177e4SLinus Torvalds 1989967d1dc1SJan Kara static void lo_release(struct gendisk *disk, fmode_t mode) 19901da177e4SLinus Torvalds { 19916cc8e743SPavel Tatashin struct loop_device *lo = disk->private_data; 19921da177e4SLinus Torvalds 19936cc8e743SPavel Tatashin mutex_lock(&lo->lo_mutex); 1994f8933667SMing Lei if (atomic_dec_return(&lo->lo_refcnt)) 19950a42e99bSJan Kara goto out_unlock; 1996f8933667SMing Lei 199714f27939SMilan Broz if (lo->lo_flags & LO_FLAGS_AUTOCLEAR) { 1998a2505b79SJan Kara if (lo->lo_state != Lo_bound) 1999a2505b79SJan Kara goto out_unlock; 2000a2505b79SJan Kara lo->lo_state = Lo_rundown; 20016cc8e743SPavel Tatashin mutex_unlock(&lo->lo_mutex); 200214f27939SMilan Broz /* 200314f27939SMilan Broz * In autoclear mode, stop the loop thread 200414f27939SMilan Broz * and remove configuration after last close. 200514f27939SMilan Broz */ 20060da03cabSJan Kara __loop_clr_fd(lo, true); 20070a42e99bSJan Kara return; 200843cade80SOmar Sandoval } else if (lo->lo_state == Lo_bound) { 200914f27939SMilan Broz /* 201014f27939SMilan Broz * Otherwise keep thread (if running) and config, 201114f27939SMilan Broz * but flush possible ongoing bios in thread. 201214f27939SMilan Broz */ 201343cade80SOmar Sandoval blk_mq_freeze_queue(lo->lo_queue); 201443cade80SOmar Sandoval blk_mq_unfreeze_queue(lo->lo_queue); 201514f27939SMilan Broz } 201696c58655SDavid Woodhouse 20170a42e99bSJan Kara out_unlock: 20186cc8e743SPavel Tatashin mutex_unlock(&lo->lo_mutex); 2019ae665016SLinus Torvalds } 2020ae665016SLinus Torvalds 202183d5cde4SAlexey Dobriyan static const struct block_device_operations lo_fops = { 20221da177e4SLinus Torvalds .owner = THIS_MODULE, 2023bb214884SAl Viro .open = lo_open, 2024bb214884SAl Viro .release = lo_release, 2025bb214884SAl Viro .ioctl = lo_ioctl, 2026863d5b82SDavid Howells #ifdef CONFIG_COMPAT 2027bb214884SAl Viro .compat_ioctl = lo_compat_ioctl, 2028863d5b82SDavid Howells #endif 20291da177e4SLinus Torvalds }; 20301da177e4SLinus Torvalds 20311da177e4SLinus Torvalds /* 20321da177e4SLinus Torvalds * And now the modules code and kernel interface. 20331da177e4SLinus Torvalds */ 203473285082SKen Chen static int max_loop; 20355657a819SJoe Perches module_param(max_loop, int, 0444); 2036a47653fcSKen Chen MODULE_PARM_DESC(max_loop, "Maximum number of loop devices"); 20375657a819SJoe Perches module_param(max_part, int, 0444); 2038476a4813SLaurent Vivier MODULE_PARM_DESC(max_part, "Maximum number of partitions per loop device"); 20391da177e4SLinus Torvalds MODULE_LICENSE("GPL"); 20401da177e4SLinus Torvalds MODULE_ALIAS_BLOCKDEV_MAJOR(LOOP_MAJOR); 20411da177e4SLinus Torvalds 20421da177e4SLinus Torvalds int loop_register_transfer(struct loop_func_table *funcs) 20431da177e4SLinus Torvalds { 20441da177e4SLinus Torvalds unsigned int n = funcs->number; 20451da177e4SLinus Torvalds 20461da177e4SLinus Torvalds if (n >= MAX_LO_CRYPT || xfer_funcs[n]) 20471da177e4SLinus Torvalds return -EINVAL; 20481da177e4SLinus Torvalds xfer_funcs[n] = funcs; 20491da177e4SLinus Torvalds return 0; 20501da177e4SLinus Torvalds } 20511da177e4SLinus Torvalds 205234dd82afSKay Sievers static int unregister_transfer_cb(int id, void *ptr, void *data) 205334dd82afSKay Sievers { 205434dd82afSKay Sievers struct loop_device *lo = ptr; 205534dd82afSKay Sievers struct loop_func_table *xfer = data; 205634dd82afSKay Sievers 20576cc8e743SPavel Tatashin mutex_lock(&lo->lo_mutex); 205834dd82afSKay Sievers if (lo->lo_encryption == xfer) 205934dd82afSKay Sievers loop_release_xfer(lo); 20606cc8e743SPavel Tatashin mutex_unlock(&lo->lo_mutex); 206134dd82afSKay Sievers return 0; 206234dd82afSKay Sievers } 206334dd82afSKay Sievers 20641da177e4SLinus Torvalds int loop_unregister_transfer(int number) 20651da177e4SLinus Torvalds { 20661da177e4SLinus Torvalds unsigned int n = number; 20671da177e4SLinus Torvalds struct loop_func_table *xfer; 20681da177e4SLinus Torvalds 20691da177e4SLinus Torvalds if (n == 0 || n >= MAX_LO_CRYPT || (xfer = xfer_funcs[n]) == NULL) 20701da177e4SLinus Torvalds return -EINVAL; 20711da177e4SLinus Torvalds 20721da177e4SLinus Torvalds xfer_funcs[n] = NULL; 207334dd82afSKay Sievers idr_for_each(&loop_index_idr, &unregister_transfer_cb, xfer); 20741da177e4SLinus Torvalds return 0; 20751da177e4SLinus Torvalds } 20761da177e4SLinus Torvalds 20771da177e4SLinus Torvalds EXPORT_SYMBOL(loop_register_transfer); 20781da177e4SLinus Torvalds EXPORT_SYMBOL(loop_unregister_transfer); 20791da177e4SLinus Torvalds 2080fc17b653SChristoph Hellwig static blk_status_t loop_queue_rq(struct blk_mq_hw_ctx *hctx, 2081b5dd2f60SMing Lei const struct blk_mq_queue_data *bd) 2082b5dd2f60SMing Lei { 20831894e916SJens Axboe struct request *rq = bd->rq; 20841894e916SJens Axboe struct loop_cmd *cmd = blk_mq_rq_to_pdu(rq); 20851894e916SJens Axboe struct loop_device *lo = rq->q->queuedata; 2086b5dd2f60SMing Lei 20871894e916SJens Axboe blk_mq_start_request(rq); 2088b5dd2f60SMing Lei 2089f4aa4c7bSMing Lei if (lo->lo_state != Lo_bound) 2090fc17b653SChristoph Hellwig return BLK_STS_IOERR; 2091f4aa4c7bSMing Lei 20921894e916SJens Axboe switch (req_op(rq)) { 2093f0225cacSChristoph Hellwig case REQ_OP_FLUSH: 2094f0225cacSChristoph Hellwig case REQ_OP_DISCARD: 209519372e27SChristoph Hellwig case REQ_OP_WRITE_ZEROES: 2096bc07c10aSMing Lei cmd->use_aio = false; 2097f0225cacSChristoph Hellwig break; 2098f0225cacSChristoph Hellwig default: 2099f0225cacSChristoph Hellwig cmd->use_aio = lo->use_dio; 2100f0225cacSChristoph Hellwig break; 2101f0225cacSChristoph Hellwig } 2102bc07c10aSMing Lei 2103d4478e92SShaohua Li /* always use the first bio's css */ 2104*c74d40e8SDan Schatzberg cmd->blkcg_css = NULL; 2105*c74d40e8SDan Schatzberg cmd->memcg_css = NULL; 21060b508bc9SShaohua Li #ifdef CONFIG_BLK_CGROUP 2107*c74d40e8SDan Schatzberg if (rq->bio && rq->bio->bi_blkg) { 2108*c74d40e8SDan Schatzberg cmd->blkcg_css = &bio_blkcg(rq->bio)->css; 2109*c74d40e8SDan Schatzberg #ifdef CONFIG_MEMCG 2110*c74d40e8SDan Schatzberg cmd->memcg_css = 2111*c74d40e8SDan Schatzberg cgroup_get_e_css(cmd->blkcg_css->cgroup, 2112*c74d40e8SDan Schatzberg &memory_cgrp_subsys); 2113d4478e92SShaohua Li #endif 2114*c74d40e8SDan Schatzberg } 2115*c74d40e8SDan Schatzberg #endif 211687579e9bSDan Schatzberg loop_queue_work(lo, cmd); 2117b5dd2f60SMing Lei 2118fc17b653SChristoph Hellwig return BLK_STS_OK; 2119b5dd2f60SMing Lei } 2120b5dd2f60SMing Lei 2121b5dd2f60SMing Lei static void loop_handle_cmd(struct loop_cmd *cmd) 2122b5dd2f60SMing Lei { 21231894e916SJens Axboe struct request *rq = blk_mq_rq_from_pdu(cmd); 21241894e916SJens Axboe const bool write = op_is_write(req_op(rq)); 21251894e916SJens Axboe struct loop_device *lo = rq->q->queuedata; 2126f4829a9bSChristoph Hellwig int ret = 0; 2127*c74d40e8SDan Schatzberg struct mem_cgroup *old_memcg = NULL; 2128b5dd2f60SMing Lei 2129f4829a9bSChristoph Hellwig if (write && (lo->lo_flags & LO_FLAGS_READ_ONLY)) { 2130f4829a9bSChristoph Hellwig ret = -EIO; 2131b5dd2f60SMing Lei goto failed; 2132f4829a9bSChristoph Hellwig } 2133b5dd2f60SMing Lei 2134*c74d40e8SDan Schatzberg if (cmd->blkcg_css) 2135*c74d40e8SDan Schatzberg kthread_associate_blkcg(cmd->blkcg_css); 2136*c74d40e8SDan Schatzberg if (cmd->memcg_css) 2137*c74d40e8SDan Schatzberg old_memcg = set_active_memcg( 2138*c74d40e8SDan Schatzberg mem_cgroup_from_css(cmd->memcg_css)); 2139*c74d40e8SDan Schatzberg 21401894e916SJens Axboe ret = do_req_filebacked(lo, rq); 2141*c74d40e8SDan Schatzberg 2142*c74d40e8SDan Schatzberg if (cmd->blkcg_css) 2143*c74d40e8SDan Schatzberg kthread_associate_blkcg(NULL); 2144*c74d40e8SDan Schatzberg 2145*c74d40e8SDan Schatzberg if (cmd->memcg_css) { 2146*c74d40e8SDan Schatzberg set_active_memcg(old_memcg); 2147*c74d40e8SDan Schatzberg css_put(cmd->memcg_css); 2148*c74d40e8SDan Schatzberg } 2149b5dd2f60SMing Lei failed: 2150bc07c10aSMing Lei /* complete non-aio request */ 2151fe2cb290SChristoph Hellwig if (!cmd->use_aio || ret) { 21528cd55087SEvan Green if (ret == -EOPNOTSUPP) 21538cd55087SEvan Green cmd->ret = ret; 21548cd55087SEvan Green else 2155fe2cb290SChristoph Hellwig cmd->ret = ret ? -EIO : 0; 215615f73f5bSChristoph Hellwig if (likely(!blk_should_fake_timeout(rq->q))) 21571894e916SJens Axboe blk_mq_complete_request(rq); 2158fe2cb290SChristoph Hellwig } 2159b5dd2f60SMing Lei } 2160b5dd2f60SMing Lei 216187579e9bSDan Schatzberg static void loop_set_timer(struct loop_device *lo) 2162b5dd2f60SMing Lei { 216387579e9bSDan Schatzberg timer_reduce(&lo->timer, jiffies + LOOP_IDLE_WORKER_TIMEOUT); 2164b5dd2f60SMing Lei } 2165b5dd2f60SMing Lei 216687579e9bSDan Schatzberg static void loop_process_work(struct loop_worker *worker, 216787579e9bSDan Schatzberg struct list_head *cmd_list, struct loop_device *lo) 2168b5dd2f60SMing Lei { 216987579e9bSDan Schatzberg int orig_flags = current->flags; 217087579e9bSDan Schatzberg struct loop_cmd *cmd; 2171b5dd2f60SMing Lei 217287579e9bSDan Schatzberg current->flags |= PF_LOCAL_THROTTLE | PF_MEMALLOC_NOIO; 217387579e9bSDan Schatzberg spin_lock_irq(&lo->lo_work_lock); 217487579e9bSDan Schatzberg while (!list_empty(cmd_list)) { 217587579e9bSDan Schatzberg cmd = container_of( 217687579e9bSDan Schatzberg cmd_list->next, struct loop_cmd, list_entry); 217787579e9bSDan Schatzberg list_del(cmd_list->next); 217887579e9bSDan Schatzberg spin_unlock_irq(&lo->lo_work_lock); 217987579e9bSDan Schatzberg 218087579e9bSDan Schatzberg loop_handle_cmd(cmd); 218187579e9bSDan Schatzberg cond_resched(); 218287579e9bSDan Schatzberg 218387579e9bSDan Schatzberg spin_lock_irq(&lo->lo_work_lock); 218487579e9bSDan Schatzberg } 218587579e9bSDan Schatzberg 218687579e9bSDan Schatzberg /* 218787579e9bSDan Schatzberg * We only add to the idle list if there are no pending cmds 218887579e9bSDan Schatzberg * *and* the worker will not run again which ensures that it 218987579e9bSDan Schatzberg * is safe to free any worker on the idle list 219087579e9bSDan Schatzberg */ 219187579e9bSDan Schatzberg if (worker && !work_pending(&worker->work)) { 219287579e9bSDan Schatzberg worker->last_ran_at = jiffies; 219387579e9bSDan Schatzberg list_add_tail(&worker->idle_list, &lo->idle_worker_list); 219487579e9bSDan Schatzberg loop_set_timer(lo); 219587579e9bSDan Schatzberg } 219687579e9bSDan Schatzberg spin_unlock_irq(&lo->lo_work_lock); 219787579e9bSDan Schatzberg current->flags = orig_flags; 219887579e9bSDan Schatzberg } 219987579e9bSDan Schatzberg 220087579e9bSDan Schatzberg static void loop_workfn(struct work_struct *work) 220187579e9bSDan Schatzberg { 220287579e9bSDan Schatzberg struct loop_worker *worker = 220387579e9bSDan Schatzberg container_of(work, struct loop_worker, work); 220487579e9bSDan Schatzberg loop_process_work(worker, &worker->cmd_list, worker->lo); 220587579e9bSDan Schatzberg } 220687579e9bSDan Schatzberg 220787579e9bSDan Schatzberg static void loop_rootcg_workfn(struct work_struct *work) 220887579e9bSDan Schatzberg { 220987579e9bSDan Schatzberg struct loop_device *lo = 221087579e9bSDan Schatzberg container_of(work, struct loop_device, rootcg_work); 221187579e9bSDan Schatzberg loop_process_work(NULL, &lo->rootcg_cmd_list, lo); 221287579e9bSDan Schatzberg } 221387579e9bSDan Schatzberg 221487579e9bSDan Schatzberg static void loop_free_idle_workers(struct timer_list *timer) 221587579e9bSDan Schatzberg { 221687579e9bSDan Schatzberg struct loop_device *lo = container_of(timer, struct loop_device, timer); 221787579e9bSDan Schatzberg struct loop_worker *pos, *worker; 221887579e9bSDan Schatzberg 221987579e9bSDan Schatzberg spin_lock_irq(&lo->lo_work_lock); 222087579e9bSDan Schatzberg list_for_each_entry_safe(worker, pos, &lo->idle_worker_list, 222187579e9bSDan Schatzberg idle_list) { 222287579e9bSDan Schatzberg if (time_is_after_jiffies(worker->last_ran_at + 222387579e9bSDan Schatzberg LOOP_IDLE_WORKER_TIMEOUT)) 222487579e9bSDan Schatzberg break; 222587579e9bSDan Schatzberg list_del(&worker->idle_list); 222687579e9bSDan Schatzberg rb_erase(&worker->rb_node, &lo->worker_tree); 2227*c74d40e8SDan Schatzberg css_put(worker->blkcg_css); 222887579e9bSDan Schatzberg kfree(worker); 222987579e9bSDan Schatzberg } 223087579e9bSDan Schatzberg if (!list_empty(&lo->idle_worker_list)) 223187579e9bSDan Schatzberg loop_set_timer(lo); 223287579e9bSDan Schatzberg spin_unlock_irq(&lo->lo_work_lock); 2233b5dd2f60SMing Lei } 2234b5dd2f60SMing Lei 2235f363b089SEric Biggers static const struct blk_mq_ops loop_mq_ops = { 2236b5dd2f60SMing Lei .queue_rq = loop_queue_rq, 2237fe2cb290SChristoph Hellwig .complete = lo_complete_rq, 2238b5dd2f60SMing Lei }; 2239b5dd2f60SMing Lei 224034dd82afSKay Sievers static int loop_add(struct loop_device **l, int i) 22411da177e4SLinus Torvalds { 224273285082SKen Chen struct loop_device *lo; 224373285082SKen Chen struct gendisk *disk; 224434dd82afSKay Sievers int err; 22451da177e4SLinus Torvalds 224634dd82afSKay Sievers err = -ENOMEM; 224768d740d7SSilva Paulo lo = kzalloc(sizeof(*lo), GFP_KERNEL); 224868d740d7SSilva Paulo if (!lo) 224973285082SKen Chen goto out; 225034dd82afSKay Sievers 2251ef7e7c82SMikulas Patocka lo->lo_state = Lo_unbound; 2252ef7e7c82SMikulas Patocka 2253c718aa65STejun Heo /* allocate id, if @id >= 0, we're requesting that specific id */ 225434dd82afSKay Sievers if (i >= 0) { 2255c718aa65STejun Heo err = idr_alloc(&loop_index_idr, lo, i, i + 1, GFP_KERNEL); 2256c718aa65STejun Heo if (err == -ENOSPC) 225734dd82afSKay Sievers err = -EEXIST; 225834dd82afSKay Sievers } else { 2259c718aa65STejun Heo err = idr_alloc(&loop_index_idr, lo, 0, 0, GFP_KERNEL); 226034dd82afSKay Sievers } 226134dd82afSKay Sievers if (err < 0) 226234dd82afSKay Sievers goto out_free_dev; 2263c718aa65STejun Heo i = err; 22641da177e4SLinus Torvalds 2265183cfb57SWei Yongjun err = -ENOMEM; 2266b5dd2f60SMing Lei lo->tag_set.ops = &loop_mq_ops; 2267b5dd2f60SMing Lei lo->tag_set.nr_hw_queues = 1; 2268b5dd2f60SMing Lei lo->tag_set.queue_depth = 128; 2269b5dd2f60SMing Lei lo->tag_set.numa_node = NUMA_NO_NODE; 2270b5dd2f60SMing Lei lo->tag_set.cmd_size = sizeof(struct loop_cmd); 2271bf0beec0SMing Lei lo->tag_set.flags = BLK_MQ_F_SHOULD_MERGE | BLK_MQ_F_STACKING; 2272b5dd2f60SMing Lei lo->tag_set.driver_data = lo; 2273b5dd2f60SMing Lei 2274b5dd2f60SMing Lei err = blk_mq_alloc_tag_set(&lo->tag_set); 2275b5dd2f60SMing Lei if (err) 22763ec981e3SMikulas Patocka goto out_free_idr; 227773285082SKen Chen 2278b5dd2f60SMing Lei lo->lo_queue = blk_mq_init_queue(&lo->tag_set); 227938a3499fSChengguang Xu if (IS_ERR(lo->lo_queue)) { 2280b5dd2f60SMing Lei err = PTR_ERR(lo->lo_queue); 2281b5dd2f60SMing Lei goto out_cleanup_tags; 2282b5dd2f60SMing Lei } 2283ef7e7c82SMikulas Patocka lo->lo_queue->queuedata = lo; 2284ef7e7c82SMikulas Patocka 228554bb0adeSShaohua Li blk_queue_max_hw_sectors(lo->lo_queue, BLK_DEF_MAX_SECTORS); 228640326d8aSShaohua Li 22875b5e20f4SMing Lei /* 228840326d8aSShaohua Li * By default, we do buffer IO, so it doesn't make sense to enable 228940326d8aSShaohua Li * merge because the I/O submitted to backing file is handled page by 229040326d8aSShaohua Li * page. For directio mode, merge does help to dispatch bigger request 229140326d8aSShaohua Li * to underlayer disk. We will enable merge once directio is enabled. 22925b5e20f4SMing Lei */ 22938b904b5bSBart Van Assche blk_queue_flag_set(QUEUE_FLAG_NOMERGES, lo->lo_queue); 22945b5e20f4SMing Lei 22957a649737SMinfei Huang err = -ENOMEM; 2296476a4813SLaurent Vivier disk = lo->lo_disk = alloc_disk(1 << part_shift); 229773285082SKen Chen if (!disk) 229873285082SKen Chen goto out_free_queue; 229973285082SKen Chen 2300e03c8dd1SKay Sievers /* 2301e03c8dd1SKay Sievers * Disable partition scanning by default. The in-kernel partition 2302e03c8dd1SKay Sievers * scanning can be requested individually per-device during its 2303e03c8dd1SKay Sievers * setup. Userspace can always add and remove partitions from all 2304e03c8dd1SKay Sievers * devices. The needed partition minors are allocated from the 2305e03c8dd1SKay Sievers * extended minor space, the main loop device numbers will continue 2306e03c8dd1SKay Sievers * to match the loop minors, regardless of the number of partitions 2307e03c8dd1SKay Sievers * used. 2308e03c8dd1SKay Sievers * 2309e03c8dd1SKay Sievers * If max_part is given, partition scanning is globally enabled for 2310e03c8dd1SKay Sievers * all loop devices. The minors for the main loop devices will be 2311e03c8dd1SKay Sievers * multiples of max_part. 2312e03c8dd1SKay Sievers * 2313e03c8dd1SKay Sievers * Note: Global-for-all-devices, set-only-at-init, read-only module 2314e03c8dd1SKay Sievers * parameteters like 'max_loop' and 'max_part' make things needlessly 2315e03c8dd1SKay Sievers * complicated, are too static, inflexible and may surprise 2316e03c8dd1SKay Sievers * userspace tools. Parameters like this in general should be avoided. 2317e03c8dd1SKay Sievers */ 2318e03c8dd1SKay Sievers if (!part_shift) 2319e03c8dd1SKay Sievers disk->flags |= GENHD_FL_NO_PART_SCAN; 2320e03c8dd1SKay Sievers disk->flags |= GENHD_FL_EXT_DEVT; 2321f8933667SMing Lei atomic_set(&lo->lo_refcnt, 0); 23226cc8e743SPavel Tatashin mutex_init(&lo->lo_mutex); 23231da177e4SLinus Torvalds lo->lo_number = i; 23241da177e4SLinus Torvalds spin_lock_init(&lo->lo_lock); 232587579e9bSDan Schatzberg spin_lock_init(&lo->lo_work_lock); 23261da177e4SLinus Torvalds disk->major = LOOP_MAJOR; 2327476a4813SLaurent Vivier disk->first_minor = i << part_shift; 23281da177e4SLinus Torvalds disk->fops = &lo_fops; 23291da177e4SLinus Torvalds disk->private_data = lo; 23301da177e4SLinus Torvalds disk->queue = lo->lo_queue; 233173285082SKen Chen sprintf(disk->disk_name, "loop%d", i); 233234dd82afSKay Sievers add_disk(disk); 233334dd82afSKay Sievers *l = lo; 233434dd82afSKay Sievers return lo->lo_number; 233573285082SKen Chen 233673285082SKen Chen out_free_queue: 233773285082SKen Chen blk_cleanup_queue(lo->lo_queue); 2338b5dd2f60SMing Lei out_cleanup_tags: 2339b5dd2f60SMing Lei blk_mq_free_tag_set(&lo->tag_set); 23403ec981e3SMikulas Patocka out_free_idr: 23413ec981e3SMikulas Patocka idr_remove(&loop_index_idr, i); 234273285082SKen Chen out_free_dev: 234373285082SKen Chen kfree(lo); 234473285082SKen Chen out: 234534dd82afSKay Sievers return err; 23461da177e4SLinus Torvalds } 23471da177e4SLinus Torvalds 234834dd82afSKay Sievers static void loop_remove(struct loop_device *lo) 234973285082SKen Chen { 23506cd18e71SNeilBrown del_gendisk(lo->lo_disk); 23510fa8ebddSBart Van Assche blk_cleanup_queue(lo->lo_queue); 2352b5dd2f60SMing Lei blk_mq_free_tag_set(&lo->tag_set); 235373285082SKen Chen put_disk(lo->lo_disk); 23546cc8e743SPavel Tatashin mutex_destroy(&lo->lo_mutex); 235573285082SKen Chen kfree(lo); 235673285082SKen Chen } 235773285082SKen Chen 2358770fe30aSKay Sievers static int find_free_cb(int id, void *ptr, void *data) 2359770fe30aSKay Sievers { 2360770fe30aSKay Sievers struct loop_device *lo = ptr; 2361770fe30aSKay Sievers struct loop_device **l = data; 2362770fe30aSKay Sievers 2363770fe30aSKay Sievers if (lo->lo_state == Lo_unbound) { 2364770fe30aSKay Sievers *l = lo; 2365770fe30aSKay Sievers return 1; 2366770fe30aSKay Sievers } 2367770fe30aSKay Sievers return 0; 2368770fe30aSKay Sievers } 2369770fe30aSKay Sievers 237034dd82afSKay Sievers static int loop_lookup(struct loop_device **l, int i) 2371a47653fcSKen Chen { 2372a47653fcSKen Chen struct loop_device *lo; 237334dd82afSKay Sievers int ret = -ENODEV; 2374a47653fcSKen Chen 2375770fe30aSKay Sievers if (i < 0) { 2376770fe30aSKay Sievers int err; 2377770fe30aSKay Sievers 2378770fe30aSKay Sievers err = idr_for_each(&loop_index_idr, &find_free_cb, &lo); 2379770fe30aSKay Sievers if (err == 1) { 2380770fe30aSKay Sievers *l = lo; 2381770fe30aSKay Sievers ret = lo->lo_number; 2382770fe30aSKay Sievers } 2383770fe30aSKay Sievers goto out; 2384a47653fcSKen Chen } 2385a47653fcSKen Chen 2386770fe30aSKay Sievers /* lookup and return a specific i */ 238734dd82afSKay Sievers lo = idr_find(&loop_index_idr, i); 2388a47653fcSKen Chen if (lo) { 238934dd82afSKay Sievers *l = lo; 239034dd82afSKay Sievers ret = lo->lo_number; 2391a47653fcSKen Chen } 2392770fe30aSKay Sievers out: 239334dd82afSKay Sievers return ret; 2394a47653fcSKen Chen } 2395a47653fcSKen Chen 23968410d38cSChristoph Hellwig static void loop_probe(dev_t dev) 239773285082SKen Chen { 23988410d38cSChristoph Hellwig int idx = MINOR(dev) >> part_shift; 2399705962ccSAl Viro struct loop_device *lo; 24008410d38cSChristoph Hellwig 24018410d38cSChristoph Hellwig if (max_loop && idx >= max_loop) 24028410d38cSChristoph Hellwig return; 240373285082SKen Chen 24040a42e99bSJan Kara mutex_lock(&loop_ctl_mutex); 24058410d38cSChristoph Hellwig if (loop_lookup(&lo, idx) < 0) 24068410d38cSChristoph Hellwig loop_add(&lo, idx); 24070a42e99bSJan Kara mutex_unlock(&loop_ctl_mutex); 240873285082SKen Chen } 240973285082SKen Chen 2410770fe30aSKay Sievers static long loop_control_ioctl(struct file *file, unsigned int cmd, 2411770fe30aSKay Sievers unsigned long parm) 2412770fe30aSKay Sievers { 2413770fe30aSKay Sievers struct loop_device *lo; 24140a42e99bSJan Kara int ret; 2415770fe30aSKay Sievers 24160a42e99bSJan Kara ret = mutex_lock_killable(&loop_ctl_mutex); 24170a42e99bSJan Kara if (ret) 24180a42e99bSJan Kara return ret; 24190a42e99bSJan Kara 24200a42e99bSJan Kara ret = -ENOSYS; 2421770fe30aSKay Sievers switch (cmd) { 2422770fe30aSKay Sievers case LOOP_CTL_ADD: 2423770fe30aSKay Sievers ret = loop_lookup(&lo, parm); 2424770fe30aSKay Sievers if (ret >= 0) { 2425770fe30aSKay Sievers ret = -EEXIST; 2426770fe30aSKay Sievers break; 2427770fe30aSKay Sievers } 2428770fe30aSKay Sievers ret = loop_add(&lo, parm); 2429770fe30aSKay Sievers break; 2430770fe30aSKay Sievers case LOOP_CTL_REMOVE: 2431770fe30aSKay Sievers ret = loop_lookup(&lo, parm); 2432770fe30aSKay Sievers if (ret < 0) 2433770fe30aSKay Sievers break; 24346cc8e743SPavel Tatashin ret = mutex_lock_killable(&lo->lo_mutex); 24356cc8e743SPavel Tatashin if (ret) 24366cc8e743SPavel Tatashin break; 2437770fe30aSKay Sievers if (lo->lo_state != Lo_unbound) { 2438770fe30aSKay Sievers ret = -EBUSY; 24396cc8e743SPavel Tatashin mutex_unlock(&lo->lo_mutex); 2440770fe30aSKay Sievers break; 2441770fe30aSKay Sievers } 2442f8933667SMing Lei if (atomic_read(&lo->lo_refcnt) > 0) { 2443770fe30aSKay Sievers ret = -EBUSY; 24446cc8e743SPavel Tatashin mutex_unlock(&lo->lo_mutex); 2445770fe30aSKay Sievers break; 2446770fe30aSKay Sievers } 2447990e7811SChristoph Hellwig lo->lo_state = Lo_deleting; 24486cc8e743SPavel Tatashin mutex_unlock(&lo->lo_mutex); 2449770fe30aSKay Sievers idr_remove(&loop_index_idr, lo->lo_number); 2450770fe30aSKay Sievers loop_remove(lo); 2451770fe30aSKay Sievers break; 2452770fe30aSKay Sievers case LOOP_CTL_GET_FREE: 2453770fe30aSKay Sievers ret = loop_lookup(&lo, -1); 2454770fe30aSKay Sievers if (ret >= 0) 2455770fe30aSKay Sievers break; 2456770fe30aSKay Sievers ret = loop_add(&lo, -1); 2457770fe30aSKay Sievers } 24580a42e99bSJan Kara mutex_unlock(&loop_ctl_mutex); 2459770fe30aSKay Sievers 2460770fe30aSKay Sievers return ret; 2461770fe30aSKay Sievers } 2462770fe30aSKay Sievers 2463770fe30aSKay Sievers static const struct file_operations loop_ctl_fops = { 2464770fe30aSKay Sievers .open = nonseekable_open, 2465770fe30aSKay Sievers .unlocked_ioctl = loop_control_ioctl, 2466770fe30aSKay Sievers .compat_ioctl = loop_control_ioctl, 2467770fe30aSKay Sievers .owner = THIS_MODULE, 2468770fe30aSKay Sievers .llseek = noop_llseek, 2469770fe30aSKay Sievers }; 2470770fe30aSKay Sievers 2471770fe30aSKay Sievers static struct miscdevice loop_misc = { 2472770fe30aSKay Sievers .minor = LOOP_CTRL_MINOR, 2473770fe30aSKay Sievers .name = "loop-control", 2474770fe30aSKay Sievers .fops = &loop_ctl_fops, 2475770fe30aSKay Sievers }; 2476770fe30aSKay Sievers 2477770fe30aSKay Sievers MODULE_ALIAS_MISCDEV(LOOP_CTRL_MINOR); 2478770fe30aSKay Sievers MODULE_ALIAS("devname:loop-control"); 2479770fe30aSKay Sievers 248073285082SKen Chen static int __init loop_init(void) 248173285082SKen Chen { 2482a47653fcSKen Chen int i, nr; 248334dd82afSKay Sievers struct loop_device *lo; 2484770fe30aSKay Sievers int err; 2485a47653fcSKen Chen 2486476a4813SLaurent Vivier part_shift = 0; 2487ac04fee0SNamhyung Kim if (max_part > 0) { 2488476a4813SLaurent Vivier part_shift = fls(max_part); 2489476a4813SLaurent Vivier 2490ac04fee0SNamhyung Kim /* 2491ac04fee0SNamhyung Kim * Adjust max_part according to part_shift as it is exported 2492ac04fee0SNamhyung Kim * to user space so that user can decide correct minor number 2493ac04fee0SNamhyung Kim * if [s]he want to create more devices. 2494ac04fee0SNamhyung Kim * 2495ac04fee0SNamhyung Kim * Note that -1 is required because partition 0 is reserved 2496ac04fee0SNamhyung Kim * for the whole disk. 2497ac04fee0SNamhyung Kim */ 2498ac04fee0SNamhyung Kim max_part = (1UL << part_shift) - 1; 2499ac04fee0SNamhyung Kim } 2500ac04fee0SNamhyung Kim 2501b1a66504SGuo Chao if ((1UL << part_shift) > DISK_MAX_PARTS) { 2502b1a66504SGuo Chao err = -EINVAL; 2503a8c1d064SAnton Volkov goto err_out; 2504b1a66504SGuo Chao } 250578f4bb36SNamhyung Kim 2506b1a66504SGuo Chao if (max_loop > 1UL << (MINORBITS - part_shift)) { 2507b1a66504SGuo Chao err = -EINVAL; 2508a8c1d064SAnton Volkov goto err_out; 2509b1a66504SGuo Chao } 251073285082SKen Chen 2511d134b00bSKay Sievers /* 2512d134b00bSKay Sievers * If max_loop is specified, create that many devices upfront. 2513d134b00bSKay Sievers * This also becomes a hard limit. If max_loop is not specified, 2514d134b00bSKay Sievers * create CONFIG_BLK_DEV_LOOP_MIN_COUNT loop devices at module 2515d134b00bSKay Sievers * init time. Loop devices can be requested on-demand with the 2516d134b00bSKay Sievers * /dev/loop-control interface, or be instantiated by accessing 2517d134b00bSKay Sievers * a 'dead' device node. 2518d134b00bSKay Sievers */ 2519aeb2b0b1SLukas Bulwahn if (max_loop) 2520a47653fcSKen Chen nr = max_loop; 2521aeb2b0b1SLukas Bulwahn else 2522d134b00bSKay Sievers nr = CONFIG_BLK_DEV_LOOP_MIN_COUNT; 2523a47653fcSKen Chen 2524a8c1d064SAnton Volkov err = misc_register(&loop_misc); 2525a8c1d064SAnton Volkov if (err < 0) 2526a8c1d064SAnton Volkov goto err_out; 2527a8c1d064SAnton Volkov 2528a8c1d064SAnton Volkov 25298410d38cSChristoph Hellwig if (__register_blkdev(LOOP_MAJOR, "loop", loop_probe)) { 2530b1a66504SGuo Chao err = -EIO; 2531b1a66504SGuo Chao goto misc_out; 2532b1a66504SGuo Chao } 2533a47653fcSKen Chen 2534d134b00bSKay Sievers /* pre-create number of devices given by config or max_loop */ 25350a42e99bSJan Kara mutex_lock(&loop_ctl_mutex); 253634dd82afSKay Sievers for (i = 0; i < nr; i++) 253734dd82afSKay Sievers loop_add(&lo, i); 25380a42e99bSJan Kara mutex_unlock(&loop_ctl_mutex); 253934dd82afSKay Sievers 254073285082SKen Chen printk(KERN_INFO "loop: module loaded\n"); 25411da177e4SLinus Torvalds return 0; 2542b1a66504SGuo Chao 2543b1a66504SGuo Chao misc_out: 2544b1a66504SGuo Chao misc_deregister(&loop_misc); 2545a8c1d064SAnton Volkov err_out: 2546b1a66504SGuo Chao return err; 254734dd82afSKay Sievers } 2548a47653fcSKen Chen 254934dd82afSKay Sievers static int loop_exit_cb(int id, void *ptr, void *data) 255034dd82afSKay Sievers { 255134dd82afSKay Sievers struct loop_device *lo = ptr; 2552a47653fcSKen Chen 255334dd82afSKay Sievers loop_remove(lo); 255434dd82afSKay Sievers return 0; 25551da177e4SLinus Torvalds } 25561da177e4SLinus Torvalds 255773285082SKen Chen static void __exit loop_exit(void) 25581da177e4SLinus Torvalds { 2559200f9337SLuis Chamberlain mutex_lock(&loop_ctl_mutex); 2560200f9337SLuis Chamberlain 256134dd82afSKay Sievers idr_for_each(&loop_index_idr, &loop_exit_cb, NULL); 256234dd82afSKay Sievers idr_destroy(&loop_index_idr); 256373285082SKen Chen 256400d59405SAkinobu Mita unregister_blkdev(LOOP_MAJOR, "loop"); 2565770fe30aSKay Sievers 2566770fe30aSKay Sievers misc_deregister(&loop_misc); 2567200f9337SLuis Chamberlain 2568200f9337SLuis Chamberlain mutex_unlock(&loop_ctl_mutex); 25691da177e4SLinus Torvalds } 25701da177e4SLinus Torvalds 25711da177e4SLinus Torvalds module_init(loop_init); 25721da177e4SLinus Torvalds module_exit(loop_exit); 25731da177e4SLinus Torvalds 25741da177e4SLinus Torvalds #ifndef MODULE 25751da177e4SLinus Torvalds static int __init max_loop_setup(char *str) 25761da177e4SLinus Torvalds { 25771da177e4SLinus Torvalds max_loop = simple_strtol(str, NULL, 0); 25781da177e4SLinus Torvalds return 1; 25791da177e4SLinus Torvalds } 25801da177e4SLinus Torvalds 25811da177e4SLinus Torvalds __setup("max_loop=", max_loop_setup); 25821da177e4SLinus Torvalds #endif 2583