11da177e4SLinus Torvalds /* 21da177e4SLinus Torvalds * linux/drivers/block/loop.c 31da177e4SLinus Torvalds * 41da177e4SLinus Torvalds * Written by Theodore Ts'o, 3/29/93 51da177e4SLinus Torvalds * 61da177e4SLinus Torvalds * Copyright 1993 by Theodore Ts'o. Redistribution of this file is 71da177e4SLinus Torvalds * permitted under the GNU General Public License. 81da177e4SLinus Torvalds * 91da177e4SLinus Torvalds * DES encryption plus some minor changes by Werner Almesberger, 30-MAY-1993 101da177e4SLinus Torvalds * more DES encryption plus IDEA encryption by Nicholas J. Leon, June 20, 1996 111da177e4SLinus Torvalds * 121da177e4SLinus Torvalds * Modularized and updated for 1.1.16 kernel - Mitch Dsouza 28th May 1994 131da177e4SLinus Torvalds * Adapted for 1.3.59 kernel - Andries Brouwer, 1 Feb 1996 141da177e4SLinus Torvalds * 151da177e4SLinus Torvalds * Fixed do_loop_request() re-entrancy - Vincent.Renardias@waw.com Mar 20, 1997 161da177e4SLinus Torvalds * 171da177e4SLinus Torvalds * Added devfs support - Richard Gooch <rgooch@atnf.csiro.au> 16-Jan-1998 181da177e4SLinus Torvalds * 191da177e4SLinus Torvalds * Handle sparse backing files correctly - Kenn Humborg, Jun 28, 1998 201da177e4SLinus Torvalds * 211da177e4SLinus Torvalds * Loadable modules and other fixes by AK, 1998 221da177e4SLinus Torvalds * 231da177e4SLinus Torvalds * Make real block number available to downstream transfer functions, enables 241da177e4SLinus Torvalds * CBC (and relatives) mode encryption requiring unique IVs per data block. 251da177e4SLinus Torvalds * Reed H. Petty, rhp@draper.net 261da177e4SLinus Torvalds * 271da177e4SLinus Torvalds * Maximum number of loop devices now dynamic via max_loop module parameter. 281da177e4SLinus Torvalds * Russell Kroll <rkroll@exploits.org> 19990701 291da177e4SLinus Torvalds * 301da177e4SLinus Torvalds * Maximum number of loop devices when compiled-in now selectable by passing 311da177e4SLinus Torvalds * max_loop=<1-255> to the kernel on boot. 3296de0e25SJan Engelhardt * Erik I. Bolsø, <eriki@himolde.no>, Oct 31, 1999 331da177e4SLinus Torvalds * 341da177e4SLinus Torvalds * Completely rewrite request handling to be make_request_fn style and 351da177e4SLinus Torvalds * non blocking, pushing work to a helper thread. Lots of fixes from 361da177e4SLinus Torvalds * Al Viro too. 371da177e4SLinus Torvalds * Jens Axboe <axboe@suse.de>, Nov 2000 381da177e4SLinus Torvalds * 391da177e4SLinus Torvalds * Support up to 256 loop devices 401da177e4SLinus Torvalds * Heinz Mauelshagen <mge@sistina.com>, Feb 2002 411da177e4SLinus Torvalds * 421da177e4SLinus Torvalds * Support for falling back on the write file operation when the address space 434e02ed4bSNick Piggin * operations write_begin is not available on the backing filesystem. 441da177e4SLinus Torvalds * Anton Altaparmakov, 16 Feb 2005 451da177e4SLinus Torvalds * 461da177e4SLinus Torvalds * Still To Fix: 471da177e4SLinus Torvalds * - Advisory locking is ignored here. 481da177e4SLinus Torvalds * - Should use an own CAP_* category instead of CAP_SYS_ADMIN 491da177e4SLinus Torvalds * 501da177e4SLinus Torvalds */ 511da177e4SLinus Torvalds 521da177e4SLinus Torvalds #include <linux/module.h> 531da177e4SLinus Torvalds #include <linux/moduleparam.h> 541da177e4SLinus Torvalds #include <linux/sched.h> 551da177e4SLinus Torvalds #include <linux/fs.h> 561da177e4SLinus Torvalds #include <linux/file.h> 571da177e4SLinus Torvalds #include <linux/stat.h> 581da177e4SLinus Torvalds #include <linux/errno.h> 591da177e4SLinus Torvalds #include <linux/major.h> 601da177e4SLinus Torvalds #include <linux/wait.h> 611da177e4SLinus Torvalds #include <linux/blkdev.h> 621da177e4SLinus Torvalds #include <linux/blkpg.h> 631da177e4SLinus Torvalds #include <linux/init.h> 641da177e4SLinus Torvalds #include <linux/swap.h> 651da177e4SLinus Torvalds #include <linux/slab.h> 66863d5b82SDavid Howells #include <linux/compat.h> 671da177e4SLinus Torvalds #include <linux/suspend.h> 6883144186SRafael J. Wysocki #include <linux/freezer.h> 692a48fc0aSArnd Bergmann #include <linux/mutex.h> 701da177e4SLinus Torvalds #include <linux/writeback.h> 711da177e4SLinus Torvalds #include <linux/completion.h> 721da177e4SLinus Torvalds #include <linux/highmem.h> 736c997918SSerge E. Hallyn #include <linux/kthread.h> 74d6b29d7cSJens Axboe #include <linux/splice.h> 75ee862730SMilan Broz #include <linux/sysfs.h> 76770fe30aSKay Sievers #include <linux/miscdevice.h> 77dfaa2ef6SLukas Czerner #include <linux/falloc.h> 78283e7e5dSAl Viro #include <linux/uio.h> 79d9a08a9eSAdam Manzanares #include <linux/ioprio.h> 80db6638d7SDennis Zhou #include <linux/blk-cgroup.h> 81d9a08a9eSAdam Manzanares 8283a87611SAl Viro #include "loop.h" 831da177e4SLinus Torvalds 847c0f6ba6SLinus Torvalds #include <linux/uaccess.h> 851da177e4SLinus Torvalds 8634dd82afSKay Sievers static DEFINE_IDR(loop_index_idr); 87310ca162STetsuo Handa static DEFINE_MUTEX(loop_ctl_mutex); 881da177e4SLinus Torvalds 89476a4813SLaurent Vivier static int max_part; 90476a4813SLaurent Vivier static int part_shift; 91476a4813SLaurent Vivier 921da177e4SLinus Torvalds static int transfer_xor(struct loop_device *lo, int cmd, 931da177e4SLinus Torvalds struct page *raw_page, unsigned raw_off, 941da177e4SLinus Torvalds struct page *loop_page, unsigned loop_off, 951da177e4SLinus Torvalds int size, sector_t real_block) 961da177e4SLinus Torvalds { 97cfd8005cSCong Wang char *raw_buf = kmap_atomic(raw_page) + raw_off; 98cfd8005cSCong Wang char *loop_buf = kmap_atomic(loop_page) + loop_off; 991da177e4SLinus Torvalds char *in, *out, *key; 1001da177e4SLinus Torvalds int i, keysize; 1011da177e4SLinus Torvalds 1021da177e4SLinus Torvalds if (cmd == READ) { 1031da177e4SLinus Torvalds in = raw_buf; 1041da177e4SLinus Torvalds out = loop_buf; 1051da177e4SLinus Torvalds } else { 1061da177e4SLinus Torvalds in = loop_buf; 1071da177e4SLinus Torvalds out = raw_buf; 1081da177e4SLinus Torvalds } 1091da177e4SLinus Torvalds 1101da177e4SLinus Torvalds key = lo->lo_encrypt_key; 1111da177e4SLinus Torvalds keysize = lo->lo_encrypt_key_size; 1121da177e4SLinus Torvalds for (i = 0; i < size; i++) 1131da177e4SLinus Torvalds *out++ = *in++ ^ key[(i & 511) % keysize]; 1141da177e4SLinus Torvalds 115cfd8005cSCong Wang kunmap_atomic(loop_buf); 116cfd8005cSCong Wang kunmap_atomic(raw_buf); 1171da177e4SLinus Torvalds cond_resched(); 1181da177e4SLinus Torvalds return 0; 1191da177e4SLinus Torvalds } 1201da177e4SLinus Torvalds 1211da177e4SLinus Torvalds static int xor_init(struct loop_device *lo, const struct loop_info64 *info) 1221da177e4SLinus Torvalds { 1231da177e4SLinus Torvalds if (unlikely(info->lo_encrypt_key_size <= 0)) 1241da177e4SLinus Torvalds return -EINVAL; 1251da177e4SLinus Torvalds return 0; 1261da177e4SLinus Torvalds } 1271da177e4SLinus Torvalds 1281da177e4SLinus Torvalds static struct loop_func_table none_funcs = { 1291da177e4SLinus Torvalds .number = LO_CRYPT_NONE, 1301da177e4SLinus Torvalds }; 1311da177e4SLinus Torvalds 1321da177e4SLinus Torvalds static struct loop_func_table xor_funcs = { 1331da177e4SLinus Torvalds .number = LO_CRYPT_XOR, 1341da177e4SLinus Torvalds .transfer = transfer_xor, 1351da177e4SLinus Torvalds .init = xor_init 1361da177e4SLinus Torvalds }; 1371da177e4SLinus Torvalds 1381da177e4SLinus Torvalds /* xfer_funcs[0] is special - its release function is never called */ 1391da177e4SLinus Torvalds static struct loop_func_table *xfer_funcs[MAX_LO_CRYPT] = { 1401da177e4SLinus Torvalds &none_funcs, 1411da177e4SLinus Torvalds &xor_funcs 1421da177e4SLinus Torvalds }; 1431da177e4SLinus Torvalds 1447035b5dfSDmitry Monakhov static loff_t get_size(loff_t offset, loff_t sizelimit, struct file *file) 1451da177e4SLinus Torvalds { 146b7a1da69SGuo Chao loff_t loopsize; 1471da177e4SLinus Torvalds 1481da177e4SLinus Torvalds /* Compute loopsize in bytes */ 149b7a1da69SGuo Chao loopsize = i_size_read(file->f_mapping->host); 150b7a1da69SGuo Chao if (offset > 0) 151b7a1da69SGuo Chao loopsize -= offset; 152b7a1da69SGuo Chao /* offset is beyond i_size, weird but possible */ 1537035b5dfSDmitry Monakhov if (loopsize < 0) 1547035b5dfSDmitry Monakhov return 0; 1551da177e4SLinus Torvalds 1567035b5dfSDmitry Monakhov if (sizelimit > 0 && sizelimit < loopsize) 1577035b5dfSDmitry Monakhov loopsize = sizelimit; 1581da177e4SLinus Torvalds /* 1591da177e4SLinus Torvalds * Unfortunately, if we want to do I/O on the device, 1601da177e4SLinus Torvalds * the number of 512-byte sectors has to fit into a sector_t. 1611da177e4SLinus Torvalds */ 1621da177e4SLinus Torvalds return loopsize >> 9; 1631da177e4SLinus Torvalds } 1641da177e4SLinus Torvalds 1657035b5dfSDmitry Monakhov static loff_t get_loop_size(struct loop_device *lo, struct file *file) 1661da177e4SLinus Torvalds { 1677035b5dfSDmitry Monakhov return get_size(lo->lo_offset, lo->lo_sizelimit, file); 1687035b5dfSDmitry Monakhov } 1697035b5dfSDmitry Monakhov 1702e5ab5f3SMing Lei static void __loop_update_dio(struct loop_device *lo, bool dio) 1712e5ab5f3SMing Lei { 1722e5ab5f3SMing Lei struct file *file = lo->lo_backing_file; 1732e5ab5f3SMing Lei struct address_space *mapping = file->f_mapping; 1742e5ab5f3SMing Lei struct inode *inode = mapping->host; 1752e5ab5f3SMing Lei unsigned short sb_bsize = 0; 1762e5ab5f3SMing Lei unsigned dio_align = 0; 1772e5ab5f3SMing Lei bool use_dio; 1782e5ab5f3SMing Lei 1792e5ab5f3SMing Lei if (inode->i_sb->s_bdev) { 1802e5ab5f3SMing Lei sb_bsize = bdev_logical_block_size(inode->i_sb->s_bdev); 1812e5ab5f3SMing Lei dio_align = sb_bsize - 1; 1822e5ab5f3SMing Lei } 1832e5ab5f3SMing Lei 1842e5ab5f3SMing Lei /* 1852e5ab5f3SMing Lei * We support direct I/O only if lo_offset is aligned with the 1862e5ab5f3SMing Lei * logical I/O size of backing device, and the logical block 1872e5ab5f3SMing Lei * size of loop is bigger than the backing device's and the loop 1882e5ab5f3SMing Lei * needn't transform transfer. 1892e5ab5f3SMing Lei * 1902e5ab5f3SMing Lei * TODO: the above condition may be loosed in the future, and 1912e5ab5f3SMing Lei * direct I/O may be switched runtime at that time because most 19289d790abSMasahiro Yamada * of requests in sane applications should be PAGE_SIZE aligned 1932e5ab5f3SMing Lei */ 1942e5ab5f3SMing Lei if (dio) { 1952e5ab5f3SMing Lei if (queue_logical_block_size(lo->lo_queue) >= sb_bsize && 1962e5ab5f3SMing Lei !(lo->lo_offset & dio_align) && 1972e5ab5f3SMing Lei mapping->a_ops->direct_IO && 1982e5ab5f3SMing Lei !lo->transfer) 1992e5ab5f3SMing Lei use_dio = true; 2002e5ab5f3SMing Lei else 2012e5ab5f3SMing Lei use_dio = false; 2022e5ab5f3SMing Lei } else { 2032e5ab5f3SMing Lei use_dio = false; 2042e5ab5f3SMing Lei } 2052e5ab5f3SMing Lei 2062e5ab5f3SMing Lei if (lo->use_dio == use_dio) 2072e5ab5f3SMing Lei return; 2082e5ab5f3SMing Lei 2092e5ab5f3SMing Lei /* flush dirty pages before changing direct IO */ 2102e5ab5f3SMing Lei vfs_fsync(file, 0); 2112e5ab5f3SMing Lei 2122e5ab5f3SMing Lei /* 2132e5ab5f3SMing Lei * The flag of LO_FLAGS_DIRECT_IO is handled similarly with 2142e5ab5f3SMing Lei * LO_FLAGS_READ_ONLY, both are set from kernel, and losetup 2152e5ab5f3SMing Lei * will get updated by ioctl(LOOP_GET_STATUS) 2162e5ab5f3SMing Lei */ 2170fbcf579SMartijn Coenen if (lo->lo_state == Lo_bound) 2182e5ab5f3SMing Lei blk_mq_freeze_queue(lo->lo_queue); 2192e5ab5f3SMing Lei lo->use_dio = use_dio; 22040326d8aSShaohua Li if (use_dio) { 2218b904b5bSBart Van Assche blk_queue_flag_clear(QUEUE_FLAG_NOMERGES, lo->lo_queue); 2222e5ab5f3SMing Lei lo->lo_flags |= LO_FLAGS_DIRECT_IO; 22340326d8aSShaohua Li } else { 2248b904b5bSBart Van Assche blk_queue_flag_set(QUEUE_FLAG_NOMERGES, lo->lo_queue); 2252e5ab5f3SMing Lei lo->lo_flags &= ~LO_FLAGS_DIRECT_IO; 22640326d8aSShaohua Li } 2270fbcf579SMartijn Coenen if (lo->lo_state == Lo_bound) 2282e5ab5f3SMing Lei blk_mq_unfreeze_queue(lo->lo_queue); 2292e5ab5f3SMing Lei } 2302e5ab5f3SMing Lei 2315795b6f5SMartijn Coenen /** 2323448914eSMartijn Coenen * loop_validate_block_size() - validates the passed in block size 2333448914eSMartijn Coenen * @bsize: size to validate 2343448914eSMartijn Coenen */ 2357035b5dfSDmitry Monakhov static int 2363448914eSMartijn Coenen loop_validate_block_size(unsigned short bsize) 2377035b5dfSDmitry Monakhov { 2383448914eSMartijn Coenen if (bsize < 512 || bsize > PAGE_SIZE || !is_power_of_2(bsize)) 2393448914eSMartijn Coenen return -EINVAL; 2403448914eSMartijn Coenen 2413448914eSMartijn Coenen return 0; 2423448914eSMartijn Coenen } 2433448914eSMartijn Coenen 2443448914eSMartijn Coenen /** 2455795b6f5SMartijn Coenen * loop_set_size() - sets device size and notifies userspace 2465795b6f5SMartijn Coenen * @lo: struct loop_device to set the size for 2475795b6f5SMartijn Coenen * @size: new size of the loop device 2485795b6f5SMartijn Coenen * 2495795b6f5SMartijn Coenen * Callers must validate that the size passed into this function fits into 2505795b6f5SMartijn Coenen * a sector_t, eg using loop_validate_size() 2515795b6f5SMartijn Coenen */ 2525795b6f5SMartijn Coenen static void loop_set_size(struct loop_device *lo, loff_t size) 2535795b6f5SMartijn Coenen { 2547b0576a3SGuo Chao struct block_device *bdev = lo->lo_device; 2551da177e4SLinus Torvalds 2565795b6f5SMartijn Coenen bd_set_size(bdev, size << SECTOR_SHIFT); 257716ad098SMartijn Coenen 258716ad098SMartijn Coenen set_capacity_revalidate_and_notify(lo->lo_disk, size, false); 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 520d4478e92SShaohua Li if (cmd->css) 521d4478e92SShaohua Li css_put(cmd->css); 522fe2cb290SChristoph Hellwig cmd->ret = ret; 52392d77332SShaohua Li lo_rw_aio_do_completion(cmd); 524bc07c10aSMing Lei } 525bc07c10aSMing Lei 526bc07c10aSMing Lei static int lo_rw_aio(struct loop_device *lo, struct loop_cmd *cmd, 527bc07c10aSMing Lei loff_t pos, bool rw) 528bc07c10aSMing Lei { 529bc07c10aSMing Lei struct iov_iter iter; 53086af5952SMing Lei struct req_iterator rq_iter; 531bc07c10aSMing Lei struct bio_vec *bvec; 5321894e916SJens Axboe struct request *rq = blk_mq_rq_from_pdu(cmd); 53340326d8aSShaohua Li struct bio *bio = rq->bio; 534bc07c10aSMing Lei struct file *file = lo->lo_backing_file; 53586af5952SMing Lei struct bio_vec tmp; 53640326d8aSShaohua Li unsigned int offset; 53786af5952SMing Lei int nr_bvec = 0; 538bc07c10aSMing Lei int ret; 539bc07c10aSMing Lei 54086af5952SMing Lei rq_for_each_bvec(tmp, rq, rq_iter) 54186af5952SMing Lei nr_bvec++; 542bc07c10aSMing Lei 54386af5952SMing Lei if (rq->bio != rq->biotail) { 54486af5952SMing Lei 54586af5952SMing Lei bvec = kmalloc_array(nr_bvec, sizeof(struct bio_vec), 5466da2ec56SKees Cook GFP_NOIO); 54740326d8aSShaohua Li if (!bvec) 54840326d8aSShaohua Li return -EIO; 54940326d8aSShaohua Li cmd->bvec = bvec; 55040326d8aSShaohua Li 551a7297a6aSMing Lei /* 55240326d8aSShaohua Li * The bios of the request may be started from the middle of 55340326d8aSShaohua Li * the 'bvec' because of bio splitting, so we can't directly 55486af5952SMing Lei * copy bio->bi_iov_vec to new bvec. The rq_for_each_bvec 55540326d8aSShaohua Li * API will take care of all details for us. 556a7297a6aSMing Lei */ 55786af5952SMing Lei rq_for_each_bvec(tmp, rq, rq_iter) { 55840326d8aSShaohua Li *bvec = tmp; 55940326d8aSShaohua Li bvec++; 56040326d8aSShaohua Li } 56140326d8aSShaohua Li bvec = cmd->bvec; 56240326d8aSShaohua Li offset = 0; 56340326d8aSShaohua Li } else { 56440326d8aSShaohua Li /* 56540326d8aSShaohua Li * Same here, this bio may be started from the middle of the 56640326d8aSShaohua Li * 'bvec' because of bio splitting, so offset from the bvec 56740326d8aSShaohua Li * must be passed to iov iterator 56840326d8aSShaohua Li */ 56940326d8aSShaohua Li offset = bio->bi_iter.bi_bvec_done; 57040326d8aSShaohua Li bvec = __bvec_iter_bvec(bio->bi_io_vec, bio->bi_iter); 57140326d8aSShaohua Li } 57292d77332SShaohua Li atomic_set(&cmd->ref, 2); 57340326d8aSShaohua Li 574b6207430SChristoph Hellwig iov_iter_bvec(&iter, rw, bvec, nr_bvec, blk_rq_bytes(rq)); 57540326d8aSShaohua Li iter.iov_offset = offset; 576bc07c10aSMing Lei 577bc07c10aSMing Lei cmd->iocb.ki_pos = pos; 578bc07c10aSMing Lei cmd->iocb.ki_filp = file; 579bc07c10aSMing Lei cmd->iocb.ki_complete = lo_rw_aio_complete; 580bc07c10aSMing Lei cmd->iocb.ki_flags = IOCB_DIRECT; 581d9a08a9eSAdam Manzanares cmd->iocb.ki_ioprio = IOPRIO_PRIO_VALUE(IOPRIO_CLASS_NONE, 0); 582d4478e92SShaohua Li if (cmd->css) 583d4478e92SShaohua Li kthread_associate_blkcg(cmd->css); 584bc07c10aSMing Lei 585bc07c10aSMing Lei if (rw == WRITE) 586bb7462b6SMiklos Szeredi ret = call_write_iter(file, &cmd->iocb, &iter); 587bc07c10aSMing Lei else 588bb7462b6SMiklos Szeredi ret = call_read_iter(file, &cmd->iocb, &iter); 589bc07c10aSMing Lei 59092d77332SShaohua Li lo_rw_aio_do_completion(cmd); 591d4478e92SShaohua Li kthread_associate_blkcg(NULL); 59292d77332SShaohua Li 593bc07c10aSMing Lei if (ret != -EIOCBQUEUED) 594bc07c10aSMing Lei cmd->iocb.ki_complete(&cmd->iocb, ret, 0); 595bc07c10aSMing Lei return 0; 596bc07c10aSMing Lei } 597bc07c10aSMing Lei 598c1c87c2bSChristoph Hellwig static int do_req_filebacked(struct loop_device *lo, struct request *rq) 599bc07c10aSMing Lei { 600bc07c10aSMing Lei struct loop_cmd *cmd = blk_mq_rq_to_pdu(rq); 601c1c87c2bSChristoph Hellwig loff_t pos = ((loff_t) blk_rq_pos(rq) << 9) + lo->lo_offset; 602bc07c10aSMing Lei 603bc07c10aSMing Lei /* 604bc07c10aSMing Lei * lo_write_simple and lo_read_simple should have been covered 605bc07c10aSMing Lei * by io submit style function like lo_rw_aio(), one blocker 606bc07c10aSMing Lei * is that lo_read_simple() need to call flush_dcache_page after 607bc07c10aSMing Lei * the page is written from kernel, and it isn't easy to handle 608bc07c10aSMing Lei * this in io submit style function which submits all segments 609bc07c10aSMing Lei * of the req at one time. And direct read IO doesn't need to 610bc07c10aSMing Lei * run flush_dcache_page(). 611bc07c10aSMing Lei */ 612c1c87c2bSChristoph Hellwig switch (req_op(rq)) { 613c1c87c2bSChristoph Hellwig case REQ_OP_FLUSH: 614c1c87c2bSChristoph Hellwig return lo_req_flush(lo, rq); 61519372e27SChristoph Hellwig case REQ_OP_WRITE_ZEROES: 616efcfec57SDarrick J. Wong /* 617efcfec57SDarrick J. Wong * If the caller doesn't want deallocation, call zeroout to 618efcfec57SDarrick J. Wong * write zeroes the range. Otherwise, punch them out. 619efcfec57SDarrick J. Wong */ 620efcfec57SDarrick J. Wong return lo_fallocate(lo, rq, pos, 621efcfec57SDarrick J. Wong (rq->cmd_flags & REQ_NOUNMAP) ? 622efcfec57SDarrick J. Wong FALLOC_FL_ZERO_RANGE : 623efcfec57SDarrick J. Wong FALLOC_FL_PUNCH_HOLE); 624efcfec57SDarrick J. Wong case REQ_OP_DISCARD: 625efcfec57SDarrick J. Wong return lo_fallocate(lo, rq, pos, FALLOC_FL_PUNCH_HOLE); 626c1c87c2bSChristoph Hellwig case REQ_OP_WRITE: 627c1c87c2bSChristoph Hellwig if (lo->transfer) 628c1c87c2bSChristoph Hellwig return lo_write_transfer(lo, rq, pos); 629c1c87c2bSChristoph Hellwig else if (cmd->use_aio) 630c1c87c2bSChristoph Hellwig return lo_rw_aio(lo, cmd, pos, WRITE); 631c1c87c2bSChristoph Hellwig else 632bc07c10aSMing Lei return lo_write_simple(lo, rq, pos); 633c1c87c2bSChristoph Hellwig case REQ_OP_READ: 634c1c87c2bSChristoph Hellwig if (lo->transfer) 635c1c87c2bSChristoph Hellwig return lo_read_transfer(lo, rq, pos); 636c1c87c2bSChristoph Hellwig else if (cmd->use_aio) 637c1c87c2bSChristoph Hellwig return lo_rw_aio(lo, cmd, pos, READ); 638bc07c10aSMing Lei else 639bc07c10aSMing Lei return lo_read_simple(lo, rq, pos); 640c1c87c2bSChristoph Hellwig default: 641c1c87c2bSChristoph Hellwig WARN_ON_ONCE(1); 642c1c87c2bSChristoph Hellwig return -EIO; 643bc07c10aSMing Lei } 6441da177e4SLinus Torvalds } 6451da177e4SLinus Torvalds 6462e5ab5f3SMing Lei static inline void loop_update_dio(struct loop_device *lo) 6472e5ab5f3SMing Lei { 648efbe3c24SIra Weiny __loop_update_dio(lo, (lo->lo_backing_file->f_flags & O_DIRECT) | 6492e5ab5f3SMing Lei lo->use_dio); 6502e5ab5f3SMing Lei } 6512e5ab5f3SMing Lei 65206f0e9e6SMing Lei static void loop_reread_partitions(struct loop_device *lo, 65306f0e9e6SMing Lei struct block_device *bdev) 65406f0e9e6SMing Lei { 65506f0e9e6SMing Lei int rc; 65606f0e9e6SMing Lei 657f0b870dfSChristoph Hellwig mutex_lock(&bdev->bd_mutex); 658f0b870dfSChristoph Hellwig rc = bdev_disk_changed(bdev, false); 659f0b870dfSChristoph Hellwig mutex_unlock(&bdev->bd_mutex); 66006f0e9e6SMing Lei if (rc) 66106f0e9e6SMing Lei pr_warn("%s: partition scan of loop%d (%s) failed (rc=%d)\n", 66206f0e9e6SMing Lei __func__, lo->lo_number, lo->lo_file_name, rc); 66306f0e9e6SMing Lei } 66406f0e9e6SMing Lei 665d2ac838eSTheodore Ts'o static inline int is_loop_device(struct file *file) 666d2ac838eSTheodore Ts'o { 667d2ac838eSTheodore Ts'o struct inode *i = file->f_mapping->host; 668d2ac838eSTheodore Ts'o 669d2ac838eSTheodore Ts'o return i && S_ISBLK(i->i_mode) && MAJOR(i->i_rdev) == LOOP_MAJOR; 670d2ac838eSTheodore Ts'o } 671d2ac838eSTheodore Ts'o 672d2ac838eSTheodore Ts'o static int loop_validate_file(struct file *file, struct block_device *bdev) 673d2ac838eSTheodore Ts'o { 674d2ac838eSTheodore Ts'o struct inode *inode = file->f_mapping->host; 675d2ac838eSTheodore Ts'o struct file *f = file; 676d2ac838eSTheodore Ts'o 677d2ac838eSTheodore Ts'o /* Avoid recursion */ 678d2ac838eSTheodore Ts'o while (is_loop_device(f)) { 679d2ac838eSTheodore Ts'o struct loop_device *l; 680d2ac838eSTheodore Ts'o 681d2ac838eSTheodore Ts'o if (f->f_mapping->host->i_bdev == bdev) 682d2ac838eSTheodore Ts'o return -EBADF; 683d2ac838eSTheodore Ts'o 684d2ac838eSTheodore Ts'o l = f->f_mapping->host->i_bdev->bd_disk->private_data; 685f7c8a412SDongli Zhang if (l->lo_state != Lo_bound) { 686d2ac838eSTheodore Ts'o return -EINVAL; 687d2ac838eSTheodore Ts'o } 688d2ac838eSTheodore Ts'o f = l->lo_backing_file; 689d2ac838eSTheodore Ts'o } 690d2ac838eSTheodore Ts'o if (!S_ISREG(inode->i_mode) && !S_ISBLK(inode->i_mode)) 691d2ac838eSTheodore Ts'o return -EINVAL; 692d2ac838eSTheodore Ts'o return 0; 693d2ac838eSTheodore Ts'o } 694d2ac838eSTheodore Ts'o 6951da177e4SLinus Torvalds /* 6961da177e4SLinus Torvalds * loop_change_fd switched the backing store of a loopback device to 6971da177e4SLinus Torvalds * a new file. This is useful for operating system installers to free up 6981da177e4SLinus Torvalds * the original file and in High Availability environments to switch to 6991da177e4SLinus Torvalds * an alternative location for the content in case of server meltdown. 7001da177e4SLinus Torvalds * This can only work if the loop device is used read-only, and if the 7011da177e4SLinus Torvalds * new backing store is the same size and type as the old backing store. 7021da177e4SLinus Torvalds */ 703bb214884SAl Viro static int loop_change_fd(struct loop_device *lo, struct block_device *bdev, 704bb214884SAl Viro unsigned int arg) 7051da177e4SLinus Torvalds { 7061dded9acSJan Kara struct file *file = NULL, *old_file; 7071da177e4SLinus Torvalds int error; 70885b0a54aSJan Kara bool partscan; 7091da177e4SLinus Torvalds 710c28445faSJan Kara error = mutex_lock_killable(&loop_ctl_mutex); 711c3710770SJan Kara if (error) 712c3710770SJan Kara return error; 7131da177e4SLinus Torvalds error = -ENXIO; 7141da177e4SLinus Torvalds if (lo->lo_state != Lo_bound) 7151dded9acSJan Kara goto out_err; 7161da177e4SLinus Torvalds 7171da177e4SLinus Torvalds /* the loop device has to be read-only */ 7181da177e4SLinus Torvalds error = -EINVAL; 7191da177e4SLinus Torvalds if (!(lo->lo_flags & LO_FLAGS_READ_ONLY)) 7201dded9acSJan Kara goto out_err; 7211da177e4SLinus Torvalds 7221da177e4SLinus Torvalds error = -EBADF; 7231da177e4SLinus Torvalds file = fget(arg); 7241da177e4SLinus Torvalds if (!file) 7251dded9acSJan Kara goto out_err; 7261da177e4SLinus Torvalds 727d2ac838eSTheodore Ts'o error = loop_validate_file(file, bdev); 728d2ac838eSTheodore Ts'o if (error) 7291dded9acSJan Kara goto out_err; 730d2ac838eSTheodore Ts'o 7311da177e4SLinus Torvalds old_file = lo->lo_backing_file; 7321da177e4SLinus Torvalds 7331da177e4SLinus Torvalds error = -EINVAL; 7341da177e4SLinus Torvalds 7351da177e4SLinus Torvalds /* size of the new backing store needs to be the same */ 7361da177e4SLinus Torvalds if (get_loop_size(lo, file) != get_loop_size(lo, old_file)) 7371dded9acSJan Kara goto out_err; 7381da177e4SLinus Torvalds 7391da177e4SLinus Torvalds /* and ... switch */ 74043cade80SOmar Sandoval blk_mq_freeze_queue(lo->lo_queue); 74143cade80SOmar Sandoval mapping_set_gfp_mask(old_file->f_mapping, lo->old_gfp_mask); 74243cade80SOmar Sandoval lo->lo_backing_file = file; 74343cade80SOmar Sandoval lo->old_gfp_mask = mapping_gfp_mask(file->f_mapping); 74443cade80SOmar Sandoval mapping_set_gfp_mask(file->f_mapping, 74543cade80SOmar Sandoval lo->old_gfp_mask & ~(__GFP_IO|__GFP_FS)); 74643cade80SOmar Sandoval loop_update_dio(lo); 74743cade80SOmar Sandoval blk_mq_unfreeze_queue(lo->lo_queue); 74885b0a54aSJan Kara partscan = lo->lo_flags & LO_FLAGS_PARTSCAN; 749c3710770SJan Kara mutex_unlock(&loop_ctl_mutex); 7501dded9acSJan Kara /* 7511dded9acSJan Kara * We must drop file reference outside of loop_ctl_mutex as dropping 7521dded9acSJan Kara * the file ref can take bd_mutex which creates circular locking 7531dded9acSJan Kara * dependency. 7541dded9acSJan Kara */ 7551dded9acSJan Kara fput(old_file); 75685b0a54aSJan Kara if (partscan) 75785b0a54aSJan Kara loop_reread_partitions(lo, bdev); 7581da177e4SLinus Torvalds return 0; 7591da177e4SLinus Torvalds 7601dded9acSJan Kara out_err: 761c3710770SJan Kara mutex_unlock(&loop_ctl_mutex); 7621dded9acSJan Kara if (file) 7631dded9acSJan Kara fput(file); 7641da177e4SLinus Torvalds return error; 7651da177e4SLinus Torvalds } 7661da177e4SLinus Torvalds 767ee862730SMilan Broz /* loop sysfs attributes */ 768ee862730SMilan Broz 769ee862730SMilan Broz static ssize_t loop_attr_show(struct device *dev, char *page, 770ee862730SMilan Broz ssize_t (*callback)(struct loop_device *, char *)) 771ee862730SMilan Broz { 77234dd82afSKay Sievers struct gendisk *disk = dev_to_disk(dev); 77334dd82afSKay Sievers struct loop_device *lo = disk->private_data; 774ee862730SMilan Broz 77534dd82afSKay Sievers return callback(lo, page); 776ee862730SMilan Broz } 777ee862730SMilan Broz 778ee862730SMilan Broz #define LOOP_ATTR_RO(_name) \ 779ee862730SMilan Broz static ssize_t loop_attr_##_name##_show(struct loop_device *, char *); \ 780ee862730SMilan Broz static ssize_t loop_attr_do_show_##_name(struct device *d, \ 781ee862730SMilan Broz struct device_attribute *attr, char *b) \ 782ee862730SMilan Broz { \ 783ee862730SMilan Broz return loop_attr_show(d, b, loop_attr_##_name##_show); \ 784ee862730SMilan Broz } \ 785ee862730SMilan Broz static struct device_attribute loop_attr_##_name = \ 7865657a819SJoe Perches __ATTR(_name, 0444, loop_attr_do_show_##_name, NULL); 787ee862730SMilan Broz 788ee862730SMilan Broz static ssize_t loop_attr_backing_file_show(struct loop_device *lo, char *buf) 789ee862730SMilan Broz { 790ee862730SMilan Broz ssize_t ret; 791ee862730SMilan Broz char *p = NULL; 792ee862730SMilan Broz 79305eb0f25SKay Sievers spin_lock_irq(&lo->lo_lock); 794ee862730SMilan Broz if (lo->lo_backing_file) 7959bf39ab2SMiklos Szeredi p = file_path(lo->lo_backing_file, buf, PAGE_SIZE - 1); 79605eb0f25SKay Sievers spin_unlock_irq(&lo->lo_lock); 797ee862730SMilan Broz 798ee862730SMilan Broz if (IS_ERR_OR_NULL(p)) 799ee862730SMilan Broz ret = PTR_ERR(p); 800ee862730SMilan Broz else { 801ee862730SMilan Broz ret = strlen(p); 802ee862730SMilan Broz memmove(buf, p, ret); 803ee862730SMilan Broz buf[ret++] = '\n'; 804ee862730SMilan Broz buf[ret] = 0; 805ee862730SMilan Broz } 806ee862730SMilan Broz 807ee862730SMilan Broz return ret; 808ee862730SMilan Broz } 809ee862730SMilan Broz 810ee862730SMilan Broz static ssize_t loop_attr_offset_show(struct loop_device *lo, char *buf) 811ee862730SMilan Broz { 812ee862730SMilan Broz return sprintf(buf, "%llu\n", (unsigned long long)lo->lo_offset); 813ee862730SMilan Broz } 814ee862730SMilan Broz 815ee862730SMilan Broz static ssize_t loop_attr_sizelimit_show(struct loop_device *lo, char *buf) 816ee862730SMilan Broz { 817ee862730SMilan Broz return sprintf(buf, "%llu\n", (unsigned long long)lo->lo_sizelimit); 818ee862730SMilan Broz } 819ee862730SMilan Broz 820ee862730SMilan Broz static ssize_t loop_attr_autoclear_show(struct loop_device *lo, char *buf) 821ee862730SMilan Broz { 822ee862730SMilan Broz int autoclear = (lo->lo_flags & LO_FLAGS_AUTOCLEAR); 823ee862730SMilan Broz 824ee862730SMilan Broz return sprintf(buf, "%s\n", autoclear ? "1" : "0"); 825ee862730SMilan Broz } 826ee862730SMilan Broz 827e03c8dd1SKay Sievers static ssize_t loop_attr_partscan_show(struct loop_device *lo, char *buf) 828e03c8dd1SKay Sievers { 829e03c8dd1SKay Sievers int partscan = (lo->lo_flags & LO_FLAGS_PARTSCAN); 830e03c8dd1SKay Sievers 831e03c8dd1SKay Sievers return sprintf(buf, "%s\n", partscan ? "1" : "0"); 832e03c8dd1SKay Sievers } 833e03c8dd1SKay Sievers 8342e5ab5f3SMing Lei static ssize_t loop_attr_dio_show(struct loop_device *lo, char *buf) 8352e5ab5f3SMing Lei { 8362e5ab5f3SMing Lei int dio = (lo->lo_flags & LO_FLAGS_DIRECT_IO); 8372e5ab5f3SMing Lei 8382e5ab5f3SMing Lei return sprintf(buf, "%s\n", dio ? "1" : "0"); 8392e5ab5f3SMing Lei } 8402e5ab5f3SMing Lei 841ee862730SMilan Broz LOOP_ATTR_RO(backing_file); 842ee862730SMilan Broz LOOP_ATTR_RO(offset); 843ee862730SMilan Broz LOOP_ATTR_RO(sizelimit); 844ee862730SMilan Broz LOOP_ATTR_RO(autoclear); 845e03c8dd1SKay Sievers LOOP_ATTR_RO(partscan); 8462e5ab5f3SMing Lei LOOP_ATTR_RO(dio); 847ee862730SMilan Broz 848ee862730SMilan Broz static struct attribute *loop_attrs[] = { 849ee862730SMilan Broz &loop_attr_backing_file.attr, 850ee862730SMilan Broz &loop_attr_offset.attr, 851ee862730SMilan Broz &loop_attr_sizelimit.attr, 852ee862730SMilan Broz &loop_attr_autoclear.attr, 853e03c8dd1SKay Sievers &loop_attr_partscan.attr, 8542e5ab5f3SMing Lei &loop_attr_dio.attr, 855ee862730SMilan Broz NULL, 856ee862730SMilan Broz }; 857ee862730SMilan Broz 858ee862730SMilan Broz static struct attribute_group loop_attribute_group = { 859ee862730SMilan Broz .name = "loop", 860ee862730SMilan Broz .attrs= loop_attrs, 861ee862730SMilan Broz }; 862ee862730SMilan Broz 863d3349b6bSTetsuo Handa static void loop_sysfs_init(struct loop_device *lo) 864ee862730SMilan Broz { 865d3349b6bSTetsuo Handa lo->sysfs_inited = !sysfs_create_group(&disk_to_dev(lo->lo_disk)->kobj, 866ee862730SMilan Broz &loop_attribute_group); 867ee862730SMilan Broz } 868ee862730SMilan Broz 869ee862730SMilan Broz static void loop_sysfs_exit(struct loop_device *lo) 870ee862730SMilan Broz { 871d3349b6bSTetsuo Handa if (lo->sysfs_inited) 872ee862730SMilan Broz sysfs_remove_group(&disk_to_dev(lo->lo_disk)->kobj, 873ee862730SMilan Broz &loop_attribute_group); 874ee862730SMilan Broz } 875ee862730SMilan Broz 876dfaa2ef6SLukas Czerner static void loop_config_discard(struct loop_device *lo) 877dfaa2ef6SLukas Czerner { 878dfaa2ef6SLukas Czerner struct file *file = lo->lo_backing_file; 879dfaa2ef6SLukas Czerner struct inode *inode = file->f_mapping->host; 880dfaa2ef6SLukas Czerner struct request_queue *q = lo->lo_queue; 881dfaa2ef6SLukas Czerner 882dfaa2ef6SLukas Czerner /* 883c52abf56SEvan Green * If the backing device is a block device, mirror its zeroing 884c52abf56SEvan Green * capability. Set the discard sectors to the block device's zeroing 885c52abf56SEvan Green * capabilities because loop discards result in blkdev_issue_zeroout(), 886c52abf56SEvan Green * not blkdev_issue_discard(). This maintains consistent behavior with 887c52abf56SEvan Green * file-backed loop devices: discarded regions read back as zero. 888c52abf56SEvan Green */ 889c52abf56SEvan Green if (S_ISBLK(inode->i_mode) && !lo->lo_encrypt_key_size) { 890c52abf56SEvan Green struct request_queue *backingq; 891c52abf56SEvan Green 892c52abf56SEvan Green backingq = bdev_get_queue(inode->i_bdev); 893c52abf56SEvan Green blk_queue_max_discard_sectors(q, 894c52abf56SEvan Green backingq->limits.max_write_zeroes_sectors); 895c52abf56SEvan Green 896c52abf56SEvan Green blk_queue_max_write_zeroes_sectors(q, 897c52abf56SEvan Green backingq->limits.max_write_zeroes_sectors); 898c52abf56SEvan Green 899c52abf56SEvan Green /* 900dfaa2ef6SLukas Czerner * We use punch hole to reclaim the free space used by the 90112a64d2fSOlaf Hering * image a.k.a. discard. However we do not support discard if 902dfaa2ef6SLukas Czerner * encryption is enabled, because it may give an attacker 903dfaa2ef6SLukas Czerner * useful information. 904dfaa2ef6SLukas Czerner */ 905c52abf56SEvan Green } else if (!file->f_op->fallocate || lo->lo_encrypt_key_size) { 906dfaa2ef6SLukas Czerner q->limits.discard_granularity = 0; 907dfaa2ef6SLukas Czerner q->limits.discard_alignment = 0; 9082bb4cd5cSJens Axboe blk_queue_max_discard_sectors(q, 0); 90919372e27SChristoph Hellwig blk_queue_max_write_zeroes_sectors(q, 0); 910dfaa2ef6SLukas Czerner 911c52abf56SEvan Green } else { 912dfaa2ef6SLukas Czerner q->limits.discard_granularity = inode->i_sb->s_blocksize; 913dfaf3c03SLukas Czerner q->limits.discard_alignment = 0; 914f2c6df7dSHannes Reinecke 9151e6ec9eaSOmar Sandoval blk_queue_max_discard_sectors(q, UINT_MAX >> 9); 9161e6ec9eaSOmar Sandoval blk_queue_max_write_zeroes_sectors(q, UINT_MAX >> 9); 917c52abf56SEvan Green } 918c52abf56SEvan Green 919c52abf56SEvan Green if (q->limits.max_write_zeroes_sectors) 9208b904b5bSBart Van Assche blk_queue_flag_set(QUEUE_FLAG_DISCARD, q); 921c52abf56SEvan Green else 922c52abf56SEvan Green blk_queue_flag_clear(QUEUE_FLAG_DISCARD, q); 923dfaa2ef6SLukas Czerner } 924dfaa2ef6SLukas Czerner 925e03a3d7aSMing Lei static void loop_unprepare_queue(struct loop_device *lo) 926e03a3d7aSMing Lei { 9273989144fSPetr Mladek kthread_flush_worker(&lo->worker); 928e03a3d7aSMing Lei kthread_stop(lo->worker_task); 929e03a3d7aSMing Lei } 930e03a3d7aSMing Lei 931b2ee7d46SNeilBrown static int loop_kthread_worker_fn(void *worker_ptr) 932b2ee7d46SNeilBrown { 933a37b0715SNeilBrown current->flags |= PF_LOCAL_THROTTLE | PF_MEMALLOC_NOIO; 934b2ee7d46SNeilBrown return kthread_worker_fn(worker_ptr); 935b2ee7d46SNeilBrown } 936b2ee7d46SNeilBrown 937e03a3d7aSMing Lei static int loop_prepare_queue(struct loop_device *lo) 938e03a3d7aSMing Lei { 9393989144fSPetr Mladek kthread_init_worker(&lo->worker); 940b2ee7d46SNeilBrown lo->worker_task = kthread_run(loop_kthread_worker_fn, 941e03a3d7aSMing Lei &lo->worker, "loop%d", lo->lo_number); 942e03a3d7aSMing Lei if (IS_ERR(lo->worker_task)) 943e03a3d7aSMing Lei return -ENOMEM; 944e03a3d7aSMing Lei set_user_nice(lo->worker_task, MIN_NICE); 945e03a3d7aSMing Lei return 0; 946e03a3d7aSMing Lei } 947e03a3d7aSMing Lei 94856a85fd8SHolger Hoffstätte static void loop_update_rotational(struct loop_device *lo) 94956a85fd8SHolger Hoffstätte { 95056a85fd8SHolger Hoffstätte struct file *file = lo->lo_backing_file; 95156a85fd8SHolger Hoffstätte struct inode *file_inode = file->f_mapping->host; 95256a85fd8SHolger Hoffstätte struct block_device *file_bdev = file_inode->i_sb->s_bdev; 95356a85fd8SHolger Hoffstätte struct request_queue *q = lo->lo_queue; 95456a85fd8SHolger Hoffstätte bool nonrot = true; 95556a85fd8SHolger Hoffstätte 95656a85fd8SHolger Hoffstätte /* not all filesystems (e.g. tmpfs) have a sb->s_bdev */ 95756a85fd8SHolger Hoffstätte if (file_bdev) 95856a85fd8SHolger Hoffstätte nonrot = blk_queue_nonrot(bdev_get_queue(file_bdev)); 95956a85fd8SHolger Hoffstätte 96056a85fd8SHolger Hoffstätte if (nonrot) 96156a85fd8SHolger Hoffstätte blk_queue_flag_set(QUEUE_FLAG_NONROT, q); 96256a85fd8SHolger Hoffstätte else 96356a85fd8SHolger Hoffstätte blk_queue_flag_clear(QUEUE_FLAG_NONROT, q); 96456a85fd8SHolger Hoffstätte } 96556a85fd8SHolger Hoffstätte 9661da177e4SLinus Torvalds static int 9671da177e4SLinus Torvalds loop_release_xfer(struct loop_device *lo) 9681da177e4SLinus Torvalds { 9691da177e4SLinus Torvalds int err = 0; 9701da177e4SLinus Torvalds struct loop_func_table *xfer = lo->lo_encryption; 9711da177e4SLinus Torvalds 9721da177e4SLinus Torvalds if (xfer) { 9731da177e4SLinus Torvalds if (xfer->release) 9741da177e4SLinus Torvalds err = xfer->release(lo); 9751da177e4SLinus Torvalds lo->transfer = NULL; 9761da177e4SLinus Torvalds lo->lo_encryption = NULL; 9771da177e4SLinus Torvalds module_put(xfer->owner); 9781da177e4SLinus Torvalds } 9791da177e4SLinus Torvalds return err; 9801da177e4SLinus Torvalds } 9811da177e4SLinus Torvalds 9821da177e4SLinus Torvalds static int 9831da177e4SLinus Torvalds loop_init_xfer(struct loop_device *lo, struct loop_func_table *xfer, 9841da177e4SLinus Torvalds const struct loop_info64 *i) 9851da177e4SLinus Torvalds { 9861da177e4SLinus Torvalds int err = 0; 9871da177e4SLinus Torvalds 9881da177e4SLinus Torvalds if (xfer) { 9891da177e4SLinus Torvalds struct module *owner = xfer->owner; 9901da177e4SLinus Torvalds 9911da177e4SLinus Torvalds if (!try_module_get(owner)) 9921da177e4SLinus Torvalds return -EINVAL; 9931da177e4SLinus Torvalds if (xfer->init) 9941da177e4SLinus Torvalds err = xfer->init(lo, i); 9951da177e4SLinus Torvalds if (err) 9961da177e4SLinus Torvalds module_put(owner); 9971da177e4SLinus Torvalds else 9981da177e4SLinus Torvalds lo->lo_encryption = xfer; 9991da177e4SLinus Torvalds } 10001da177e4SLinus Torvalds return err; 10011da177e4SLinus Torvalds } 10021da177e4SLinus Torvalds 100362ab466cSMartijn Coenen /** 100462ab466cSMartijn Coenen * loop_set_status_from_info - configure device from loop_info 100562ab466cSMartijn Coenen * @lo: struct loop_device to configure 100662ab466cSMartijn Coenen * @info: struct loop_info64 to configure the device with 100762ab466cSMartijn Coenen * 100862ab466cSMartijn Coenen * Configures the loop device parameters according to the passed 100962ab466cSMartijn Coenen * in loop_info64 configuration. 101062ab466cSMartijn Coenen */ 101162ab466cSMartijn Coenen static int 101262ab466cSMartijn Coenen loop_set_status_from_info(struct loop_device *lo, 101362ab466cSMartijn Coenen const struct loop_info64 *info) 101462ab466cSMartijn Coenen { 101562ab466cSMartijn Coenen int err; 101662ab466cSMartijn Coenen struct loop_func_table *xfer; 101762ab466cSMartijn Coenen kuid_t uid = current_uid(); 101862ab466cSMartijn Coenen 101962ab466cSMartijn Coenen if ((unsigned int) info->lo_encrypt_key_size > LO_KEY_SIZE) 102062ab466cSMartijn Coenen return -EINVAL; 102162ab466cSMartijn Coenen 102262ab466cSMartijn Coenen err = loop_release_xfer(lo); 102362ab466cSMartijn Coenen if (err) 102462ab466cSMartijn Coenen return err; 102562ab466cSMartijn Coenen 102662ab466cSMartijn Coenen if (info->lo_encrypt_type) { 102762ab466cSMartijn Coenen unsigned int type = info->lo_encrypt_type; 102862ab466cSMartijn Coenen 102962ab466cSMartijn Coenen if (type >= MAX_LO_CRYPT) 103062ab466cSMartijn Coenen return -EINVAL; 103162ab466cSMartijn Coenen xfer = xfer_funcs[type]; 103262ab466cSMartijn Coenen if (xfer == NULL) 103362ab466cSMartijn Coenen return -EINVAL; 103462ab466cSMartijn Coenen } else 103562ab466cSMartijn Coenen xfer = NULL; 103662ab466cSMartijn Coenen 103762ab466cSMartijn Coenen err = loop_init_xfer(lo, xfer, info); 103862ab466cSMartijn Coenen if (err) 103962ab466cSMartijn Coenen return err; 104062ab466cSMartijn Coenen 104162ab466cSMartijn Coenen lo->lo_offset = info->lo_offset; 104262ab466cSMartijn Coenen lo->lo_sizelimit = info->lo_sizelimit; 104362ab466cSMartijn Coenen memcpy(lo->lo_file_name, info->lo_file_name, LO_NAME_SIZE); 104462ab466cSMartijn Coenen memcpy(lo->lo_crypt_name, info->lo_crypt_name, LO_NAME_SIZE); 104562ab466cSMartijn Coenen lo->lo_file_name[LO_NAME_SIZE-1] = 0; 104662ab466cSMartijn Coenen lo->lo_crypt_name[LO_NAME_SIZE-1] = 0; 104762ab466cSMartijn Coenen 104862ab466cSMartijn Coenen if (!xfer) 104962ab466cSMartijn Coenen xfer = &none_funcs; 105062ab466cSMartijn Coenen lo->transfer = xfer->transfer; 105162ab466cSMartijn Coenen lo->ioctl = xfer->ioctl; 105262ab466cSMartijn Coenen 1053faf1d254SMartijn Coenen lo->lo_flags = info->lo_flags; 105462ab466cSMartijn Coenen 105562ab466cSMartijn Coenen lo->lo_encrypt_key_size = info->lo_encrypt_key_size; 105662ab466cSMartijn Coenen lo->lo_init[0] = info->lo_init[0]; 105762ab466cSMartijn Coenen lo->lo_init[1] = info->lo_init[1]; 105862ab466cSMartijn Coenen if (info->lo_encrypt_key_size) { 105962ab466cSMartijn Coenen memcpy(lo->lo_encrypt_key, info->lo_encrypt_key, 106062ab466cSMartijn Coenen info->lo_encrypt_key_size); 106162ab466cSMartijn Coenen lo->lo_key_owner = uid; 106262ab466cSMartijn Coenen } 106362ab466cSMartijn Coenen 106462ab466cSMartijn Coenen return 0; 106562ab466cSMartijn Coenen } 106662ab466cSMartijn Coenen 10673448914eSMartijn Coenen static int loop_configure(struct loop_device *lo, fmode_t mode, 10683448914eSMartijn Coenen struct block_device *bdev, 10693448914eSMartijn Coenen const struct loop_config *config) 10701da177e4SLinus Torvalds { 10711da177e4SLinus Torvalds struct file *file; 10721da177e4SLinus Torvalds struct inode *inode; 10731da177e4SLinus Torvalds struct address_space *mapping; 10741da177e4SLinus Torvalds struct block_device *claimed_bdev = NULL; 10751da177e4SLinus Torvalds int error; 10761da177e4SLinus Torvalds loff_t size; 10771da177e4SLinus Torvalds bool partscan; 10783448914eSMartijn Coenen unsigned short bsize; 10791da177e4SLinus Torvalds 10801da177e4SLinus Torvalds /* This is safe, since we have a reference from open(). */ 10811da177e4SLinus Torvalds __module_get(THIS_MODULE); 10821da177e4SLinus Torvalds 10831da177e4SLinus Torvalds error = -EBADF; 10843448914eSMartijn Coenen file = fget(config->fd); 10851da177e4SLinus Torvalds if (!file) 10861da177e4SLinus Torvalds goto out; 10871da177e4SLinus Torvalds 10881da177e4SLinus Torvalds /* 10891da177e4SLinus Torvalds * If we don't hold exclusive handle for the device, upgrade to it 10901da177e4SLinus Torvalds * here to avoid changing device under exclusive owner. 10911da177e4SLinus Torvalds */ 10921da177e4SLinus Torvalds if (!(mode & FMODE_EXCL)) { 1093ecbe6bc0SChristoph Hellwig claimed_bdev = bdev->bd_contains; 1094ecbe6bc0SChristoph Hellwig error = bd_prepare_to_claim(bdev, claimed_bdev, loop_configure); 1095ecbe6bc0SChristoph Hellwig if (error) 10961da177e4SLinus Torvalds goto out_putf; 10971da177e4SLinus Torvalds } 10981da177e4SLinus Torvalds 10991da177e4SLinus Torvalds error = mutex_lock_killable(&loop_ctl_mutex); 11001da177e4SLinus Torvalds if (error) 11011da177e4SLinus Torvalds goto out_bdev; 11021da177e4SLinus Torvalds 11031da177e4SLinus Torvalds error = -EBUSY; 11041da177e4SLinus Torvalds if (lo->lo_state != Lo_unbound) 11051da177e4SLinus Torvalds goto out_unlock; 11061da177e4SLinus Torvalds 11071da177e4SLinus Torvalds error = loop_validate_file(file, bdev); 11081da177e4SLinus Torvalds if (error) 11091da177e4SLinus Torvalds goto out_unlock; 11101da177e4SLinus Torvalds 11111da177e4SLinus Torvalds mapping = file->f_mapping; 11121da177e4SLinus Torvalds inode = mapping->host; 11131da177e4SLinus Torvalds 11143448914eSMartijn Coenen size = get_loop_size(lo, file); 11153448914eSMartijn Coenen 11163448914eSMartijn Coenen if ((config->info.lo_flags & ~LOOP_CONFIGURE_SETTABLE_FLAGS) != 0) { 11173448914eSMartijn Coenen error = -EINVAL; 11183448914eSMartijn Coenen goto out_unlock; 11193448914eSMartijn Coenen } 11203448914eSMartijn Coenen 11213448914eSMartijn Coenen if (config->block_size) { 11223448914eSMartijn Coenen error = loop_validate_block_size(config->block_size); 11233448914eSMartijn Coenen if (error) 11243448914eSMartijn Coenen goto out_unlock; 11253448914eSMartijn Coenen } 11263448914eSMartijn Coenen 11273448914eSMartijn Coenen error = loop_set_status_from_info(lo, &config->info); 11283448914eSMartijn Coenen if (error) 11293448914eSMartijn Coenen goto out_unlock; 11303448914eSMartijn Coenen 11311da177e4SLinus Torvalds if (!(file->f_mode & FMODE_WRITE) || !(mode & FMODE_WRITE) || 11321da177e4SLinus Torvalds !file->f_op->write_iter) 11333448914eSMartijn Coenen lo->lo_flags |= LO_FLAGS_READ_ONLY; 1134083a6a50SMartijn Coenen 11351da177e4SLinus Torvalds error = loop_prepare_queue(lo); 11361da177e4SLinus Torvalds if (error) 11371da177e4SLinus Torvalds goto out_unlock; 11381da177e4SLinus Torvalds 11393448914eSMartijn Coenen set_device_ro(bdev, (lo->lo_flags & LO_FLAGS_READ_ONLY) != 0); 11401da177e4SLinus Torvalds 11413448914eSMartijn Coenen lo->use_dio = lo->lo_flags & LO_FLAGS_DIRECT_IO; 11421da177e4SLinus Torvalds lo->lo_device = bdev; 11431da177e4SLinus Torvalds lo->lo_backing_file = file; 11441da177e4SLinus Torvalds lo->old_gfp_mask = mapping_gfp_mask(mapping); 11451da177e4SLinus Torvalds mapping_set_gfp_mask(mapping, lo->old_gfp_mask & ~(__GFP_IO|__GFP_FS)); 11461da177e4SLinus Torvalds 11473448914eSMartijn Coenen if (!(lo->lo_flags & LO_FLAGS_READ_ONLY) && file->f_op->fsync) 11481da177e4SLinus Torvalds blk_queue_write_cache(lo->lo_queue, true, false); 11491da177e4SLinus Torvalds 11503448914eSMartijn Coenen if (config->block_size) 11513448914eSMartijn Coenen bsize = config->block_size; 115296ed320dSLinus Torvalds else if ((lo->lo_backing_file->f_flags & O_DIRECT) && inode->i_sb->s_bdev) 11531da177e4SLinus Torvalds /* In case of direct I/O, match underlying block size */ 11543448914eSMartijn Coenen bsize = bdev_logical_block_size(inode->i_sb->s_bdev); 11553448914eSMartijn Coenen else 11563448914eSMartijn Coenen bsize = 512; 11571da177e4SLinus Torvalds 11581da177e4SLinus Torvalds blk_queue_logical_block_size(lo->lo_queue, bsize); 11591da177e4SLinus Torvalds blk_queue_physical_block_size(lo->lo_queue, bsize); 11601da177e4SLinus Torvalds blk_queue_io_min(lo->lo_queue, bsize); 11611da177e4SLinus Torvalds 11621da177e4SLinus Torvalds loop_update_rotational(lo); 11631da177e4SLinus Torvalds loop_update_dio(lo); 11641da177e4SLinus Torvalds loop_sysfs_init(lo); 11655795b6f5SMartijn Coenen loop_set_size(lo, size); 11661da177e4SLinus Torvalds 11671da177e4SLinus Torvalds set_blocksize(bdev, S_ISBLK(inode->i_mode) ? 11681da177e4SLinus Torvalds block_size(inode->i_bdev) : PAGE_SIZE); 11691da177e4SLinus Torvalds 11701da177e4SLinus Torvalds lo->lo_state = Lo_bound; 11711da177e4SLinus Torvalds if (part_shift) 11721da177e4SLinus Torvalds lo->lo_flags |= LO_FLAGS_PARTSCAN; 11731da177e4SLinus Torvalds partscan = lo->lo_flags & LO_FLAGS_PARTSCAN; 1174*fe6a8fc5SLennart Poettering if (partscan) 1175*fe6a8fc5SLennart Poettering lo->lo_disk->flags &= ~GENHD_FL_NO_PART_SCAN; 11761da177e4SLinus Torvalds 11771da177e4SLinus Torvalds /* Grab the block_device to prevent its destruction after we 11781da177e4SLinus Torvalds * put /dev/loopXX inode. Later in __loop_clr_fd() we bdput(bdev). 11791da177e4SLinus Torvalds */ 11801da177e4SLinus Torvalds bdgrab(bdev); 11811da177e4SLinus Torvalds mutex_unlock(&loop_ctl_mutex); 11821da177e4SLinus Torvalds if (partscan) 11831da177e4SLinus Torvalds loop_reread_partitions(lo, bdev); 11841da177e4SLinus Torvalds if (claimed_bdev) 11853448914eSMartijn Coenen bd_abort_claiming(bdev, claimed_bdev, loop_configure); 11861da177e4SLinus Torvalds return 0; 11871da177e4SLinus Torvalds 11881da177e4SLinus Torvalds out_unlock: 11891da177e4SLinus Torvalds mutex_unlock(&loop_ctl_mutex); 11901da177e4SLinus Torvalds out_bdev: 11911da177e4SLinus Torvalds if (claimed_bdev) 11923448914eSMartijn Coenen bd_abort_claiming(bdev, claimed_bdev, loop_configure); 11931da177e4SLinus Torvalds out_putf: 11941da177e4SLinus Torvalds fput(file); 11951da177e4SLinus Torvalds out: 11961da177e4SLinus Torvalds /* This is safe: open() is still holding a reference. */ 11971da177e4SLinus Torvalds module_put(THIS_MODULE); 11981da177e4SLinus Torvalds return error; 11991da177e4SLinus Torvalds } 12001da177e4SLinus Torvalds 12010da03cabSJan Kara static int __loop_clr_fd(struct loop_device *lo, bool release) 12021da177e4SLinus Torvalds { 12037ccd0791SJan Kara struct file *filp = NULL; 1204b4e3ca1aSAl Viro gfp_t gfp = lo->old_gfp_mask; 12054c823cc3SAyan George struct block_device *bdev = lo->lo_device; 12067ccd0791SJan Kara int err = 0; 12070da03cabSJan Kara bool partscan = false; 12080da03cabSJan Kara int lo_number; 12091da177e4SLinus Torvalds 12107ccd0791SJan Kara mutex_lock(&loop_ctl_mutex); 12117ccd0791SJan Kara if (WARN_ON_ONCE(lo->lo_state != Lo_rundown)) { 12127ccd0791SJan Kara err = -ENXIO; 12137ccd0791SJan Kara goto out_unlock; 12147ccd0791SJan Kara } 12151da177e4SLinus Torvalds 12167ccd0791SJan Kara filp = lo->lo_backing_file; 12177ccd0791SJan Kara if (filp == NULL) { 12187ccd0791SJan Kara err = -EINVAL; 12197ccd0791SJan Kara goto out_unlock; 12207ccd0791SJan Kara } 12211da177e4SLinus Torvalds 1222f8933667SMing Lei /* freeze request queue during the transition */ 1223f8933667SMing Lei blk_mq_freeze_queue(lo->lo_queue); 1224f8933667SMing Lei 12251da177e4SLinus Torvalds spin_lock_irq(&lo->lo_lock); 12261da177e4SLinus Torvalds lo->lo_backing_file = NULL; 122705eb0f25SKay Sievers spin_unlock_irq(&lo->lo_lock); 12281da177e4SLinus Torvalds 12291da177e4SLinus Torvalds loop_release_xfer(lo); 12301da177e4SLinus Torvalds lo->transfer = NULL; 12311da177e4SLinus Torvalds lo->ioctl = NULL; 12321da177e4SLinus Torvalds lo->lo_device = NULL; 12331da177e4SLinus Torvalds lo->lo_encryption = NULL; 12341da177e4SLinus Torvalds lo->lo_offset = 0; 12351da177e4SLinus Torvalds lo->lo_sizelimit = 0; 12361da177e4SLinus Torvalds lo->lo_encrypt_key_size = 0; 12371da177e4SLinus Torvalds memset(lo->lo_encrypt_key, 0, LO_KEY_SIZE); 12381da177e4SLinus Torvalds memset(lo->lo_crypt_name, 0, LO_NAME_SIZE); 12391da177e4SLinus Torvalds memset(lo->lo_file_name, 0, LO_NAME_SIZE); 124089e4fdecSOmar Sandoval blk_queue_logical_block_size(lo->lo_queue, 512); 1241bf093753SOmar Sandoval blk_queue_physical_block_size(lo->lo_queue, 512); 1242bf093753SOmar Sandoval blk_queue_io_min(lo->lo_queue, 512); 1243c1681bf8SAnatol Pomozov if (bdev) { 1244c1681bf8SAnatol Pomozov bdput(bdev); 1245f98393a6SPeter Zijlstra invalidate_bdev(bdev); 1246eedffa28SJeff Layton bdev->bd_inode->i_mapping->wb_err = 0; 1247c1681bf8SAnatol Pomozov } 124873285082SKen Chen set_capacity(lo->lo_disk, 0); 124951a0bb0cSMilan Broz loop_sysfs_exit(lo); 1250c3473c63SDavid Zeuthen if (bdev) { 12511da177e4SLinus Torvalds bd_set_size(bdev, 0); 1252c3473c63SDavid Zeuthen /* let user-space know about this change */ 1253c3473c63SDavid Zeuthen kobject_uevent(&disk_to_dev(bdev->bd_disk)->kobj, KOBJ_CHANGE); 1254c3473c63SDavid Zeuthen } 12551da177e4SLinus Torvalds mapping_set_gfp_mask(filp->f_mapping, gfp); 12561da177e4SLinus Torvalds /* This is safe: open() is still holding a reference. */ 12571da177e4SLinus Torvalds module_put(THIS_MODULE); 1258f8933667SMing Lei blk_mq_unfreeze_queue(lo->lo_queue); 1259f8933667SMing Lei 12600da03cabSJan Kara partscan = lo->lo_flags & LO_FLAGS_PARTSCAN && bdev; 12610da03cabSJan Kara lo_number = lo->lo_number; 12620da03cabSJan Kara loop_unprepare_queue(lo); 12630da03cabSJan Kara out_unlock: 12640da03cabSJan Kara mutex_unlock(&loop_ctl_mutex); 12650da03cabSJan Kara if (partscan) { 1266d57f3374SJan Kara /* 1267d57f3374SJan Kara * bd_mutex has been held already in release path, so don't 1268d57f3374SJan Kara * acquire it if this function is called in such case. 1269d57f3374SJan Kara * 1270d57f3374SJan Kara * If the reread partition isn't from release path, lo_refcnt 1271d57f3374SJan Kara * must be at least one and it can only become zero when the 1272d57f3374SJan Kara * current holder is released. 1273d57f3374SJan Kara */ 1274f0b870dfSChristoph Hellwig if (!release) 1275f0b870dfSChristoph Hellwig mutex_lock(&bdev->bd_mutex); 1276f0b870dfSChristoph Hellwig err = bdev_disk_changed(bdev, false); 1277f0b870dfSChristoph Hellwig if (!release) 1278f0b870dfSChristoph Hellwig mutex_unlock(&bdev->bd_mutex); 127940853d6fSDongli Zhang if (err) 1280d57f3374SJan Kara pr_warn("%s: partition scan of loop%d failed (rc=%d)\n", 12810da03cabSJan Kara __func__, lo_number, err); 1282d57f3374SJan Kara /* Device is gone, no point in returning error */ 1283d57f3374SJan Kara err = 0; 1284d57f3374SJan Kara } 1285758a58d0SDongli Zhang 1286758a58d0SDongli Zhang /* 1287758a58d0SDongli Zhang * lo->lo_state is set to Lo_unbound here after above partscan has 1288758a58d0SDongli Zhang * finished. 1289758a58d0SDongli Zhang * 1290758a58d0SDongli Zhang * There cannot be anybody else entering __loop_clr_fd() as 1291758a58d0SDongli Zhang * lo->lo_backing_file is already cleared and Lo_rundown state 1292758a58d0SDongli Zhang * protects us from all the other places trying to change the 'lo' 1293758a58d0SDongli Zhang * device. 1294758a58d0SDongli Zhang */ 1295758a58d0SDongli Zhang mutex_lock(&loop_ctl_mutex); 1296758a58d0SDongli Zhang lo->lo_flags = 0; 1297758a58d0SDongli Zhang if (!part_shift) 1298758a58d0SDongli Zhang lo->lo_disk->flags |= GENHD_FL_NO_PART_SCAN; 1299758a58d0SDongli Zhang lo->lo_state = Lo_unbound; 1300758a58d0SDongli Zhang mutex_unlock(&loop_ctl_mutex); 1301758a58d0SDongli Zhang 1302f028f3b2SNikanth Karthikesan /* 1303310ca162STetsuo Handa * Need not hold loop_ctl_mutex to fput backing file. 1304310ca162STetsuo Handa * Calling fput holding loop_ctl_mutex triggers a circular 1305f028f3b2SNikanth Karthikesan * lock dependency possibility warning as fput can take 1306310ca162STetsuo Handa * bd_mutex which is usually taken before loop_ctl_mutex. 1307f028f3b2SNikanth Karthikesan */ 13087ccd0791SJan Kara if (filp) 1309f028f3b2SNikanth Karthikesan fput(filp); 13107ccd0791SJan Kara return err; 13111da177e4SLinus Torvalds } 13121da177e4SLinus Torvalds 1313a2505b79SJan Kara static int loop_clr_fd(struct loop_device *lo) 1314a2505b79SJan Kara { 13157ccd0791SJan Kara int err; 13167ccd0791SJan Kara 1317c28445faSJan Kara err = mutex_lock_killable(&loop_ctl_mutex); 13187ccd0791SJan Kara if (err) 13197ccd0791SJan Kara return err; 13207ccd0791SJan Kara if (lo->lo_state != Lo_bound) { 13217ccd0791SJan Kara mutex_unlock(&loop_ctl_mutex); 1322a2505b79SJan Kara return -ENXIO; 13237ccd0791SJan Kara } 1324a2505b79SJan Kara /* 1325a2505b79SJan Kara * If we've explicitly asked to tear down the loop device, 1326a2505b79SJan Kara * and it has an elevated reference count, set it for auto-teardown when 1327a2505b79SJan Kara * the last reference goes away. This stops $!~#$@ udev from 1328a2505b79SJan Kara * preventing teardown because it decided that it needs to run blkid on 1329a2505b79SJan Kara * the loopback device whenever they appear. xfstests is notorious for 1330a2505b79SJan Kara * failing tests because blkid via udev races with a losetup 1331a2505b79SJan Kara * <dev>/do something like mkfs/losetup -d <dev> causing the losetup -d 1332a2505b79SJan Kara * command to fail with EBUSY. 1333a2505b79SJan Kara */ 1334a2505b79SJan Kara if (atomic_read(&lo->lo_refcnt) > 1) { 1335a2505b79SJan Kara lo->lo_flags |= LO_FLAGS_AUTOCLEAR; 1336a2505b79SJan Kara mutex_unlock(&loop_ctl_mutex); 1337a2505b79SJan Kara return 0; 1338a2505b79SJan Kara } 1339a2505b79SJan Kara lo->lo_state = Lo_rundown; 13407ccd0791SJan Kara mutex_unlock(&loop_ctl_mutex); 1341a2505b79SJan Kara 13420da03cabSJan Kara return __loop_clr_fd(lo, false); 1343a2505b79SJan Kara } 1344a2505b79SJan Kara 13451da177e4SLinus Torvalds static int 13461da177e4SLinus Torvalds loop_set_status(struct loop_device *lo, const struct loop_info64 *info) 13471da177e4SLinus Torvalds { 13481da177e4SLinus Torvalds int err; 134985b0a54aSJan Kara struct block_device *bdev; 13500c3796c2SMartijn Coenen kuid_t uid = current_uid(); 1351faf1d254SMartijn Coenen int prev_lo_flags; 135285b0a54aSJan Kara bool partscan = false; 13530c3796c2SMartijn Coenen bool size_changed = false; 13541da177e4SLinus Torvalds 1355c28445faSJan Kara err = mutex_lock_killable(&loop_ctl_mutex); 1356550df5fdSJan Kara if (err) 1357550df5fdSJan Kara return err; 1358b0fafa81SDavid Howells if (lo->lo_encrypt_key_size && 1359e4849737SEric W. Biederman !uid_eq(lo->lo_key_owner, uid) && 1360550df5fdSJan Kara !capable(CAP_SYS_ADMIN)) { 1361550df5fdSJan Kara err = -EPERM; 1362550df5fdSJan Kara goto out_unlock; 1363550df5fdSJan Kara } 1364550df5fdSJan Kara if (lo->lo_state != Lo_bound) { 1365550df5fdSJan Kara err = -ENXIO; 1366550df5fdSJan Kara goto out_unlock; 1367550df5fdSJan Kara } 13681da177e4SLinus Torvalds 13695db470e2SJaegeuk Kim if (lo->lo_offset != info->lo_offset || 13705db470e2SJaegeuk Kim lo->lo_sizelimit != info->lo_sizelimit) { 13710c3796c2SMartijn Coenen size_changed = true; 13725db470e2SJaegeuk Kim sync_blockdev(lo->lo_device); 1373f4bd34b1SZheng Bin invalidate_bdev(lo->lo_device); 13745db470e2SJaegeuk Kim } 13755db470e2SJaegeuk Kim 1376ecdd0959SMing Lei /* I/O need to be drained during transfer transition */ 1377ecdd0959SMing Lei blk_mq_freeze_queue(lo->lo_queue); 1378ecdd0959SMing Lei 13790c3796c2SMartijn Coenen if (size_changed && lo->lo_device->bd_inode->i_mapping->nrpages) { 1380f4bd34b1SZheng Bin /* If any pages were dirtied after invalidate_bdev(), try again */ 13815db470e2SJaegeuk Kim err = -EAGAIN; 13825db470e2SJaegeuk Kim pr_warn("%s: loop%d (%s) has still dirty pages (nrpages=%lu)\n", 13835db470e2SJaegeuk Kim __func__, lo->lo_number, lo->lo_file_name, 13845db470e2SJaegeuk Kim lo->lo_device->bd_inode->i_mapping->nrpages); 13855db470e2SJaegeuk Kim goto out_unfreeze; 13865db470e2SJaegeuk Kim } 13870c3796c2SMartijn Coenen 1388faf1d254SMartijn Coenen prev_lo_flags = lo->lo_flags; 1389faf1d254SMartijn Coenen 13900c3796c2SMartijn Coenen err = loop_set_status_from_info(lo, info); 13910c3796c2SMartijn Coenen if (err) 1392550df5fdSJan Kara goto out_unfreeze; 13930c3796c2SMartijn Coenen 1394faf1d254SMartijn Coenen /* Mask out flags that can't be set using LOOP_SET_STATUS. */ 13956ac92fb5SMartijn Coenen lo->lo_flags &= LOOP_SET_STATUS_SETTABLE_FLAGS; 1396faf1d254SMartijn Coenen /* For those flags, use the previous values instead */ 1397faf1d254SMartijn Coenen lo->lo_flags |= prev_lo_flags & ~LOOP_SET_STATUS_SETTABLE_FLAGS; 1398faf1d254SMartijn Coenen /* For flags that can't be cleared, use previous values too */ 1399faf1d254SMartijn Coenen lo->lo_flags |= prev_lo_flags & ~LOOP_SET_STATUS_CLEARABLE_FLAGS; 1400faf1d254SMartijn Coenen 1401b0bd158dSMartijn Coenen if (size_changed) { 1402b0bd158dSMartijn Coenen loff_t new_size = get_size(lo->lo_offset, lo->lo_sizelimit, 1403b0bd158dSMartijn Coenen lo->lo_backing_file); 1404b0bd158dSMartijn Coenen loop_set_size(lo, new_size); 1405b040ad9cSArnd Bergmann } 1406541c742aSGuo Chao 1407dfaa2ef6SLukas Czerner loop_config_discard(lo); 14081da177e4SLinus Torvalds 14092e5ab5f3SMing Lei /* update dio if lo_offset or transfer is changed */ 14102e5ab5f3SMing Lei __loop_update_dio(lo, lo->use_dio); 14112e5ab5f3SMing Lei 1412550df5fdSJan Kara out_unfreeze: 1413ecdd0959SMing Lei blk_mq_unfreeze_queue(lo->lo_queue); 1414e02898b4SOmar Sandoval 1415faf1d254SMartijn Coenen if (!err && (lo->lo_flags & LO_FLAGS_PARTSCAN) && 1416faf1d254SMartijn Coenen !(prev_lo_flags & LO_FLAGS_PARTSCAN)) { 1417e02898b4SOmar Sandoval lo->lo_disk->flags &= ~GENHD_FL_NO_PART_SCAN; 141885b0a54aSJan Kara bdev = lo->lo_device; 141985b0a54aSJan Kara partscan = true; 1420e02898b4SOmar Sandoval } 1421550df5fdSJan Kara out_unlock: 1422550df5fdSJan Kara mutex_unlock(&loop_ctl_mutex); 142385b0a54aSJan Kara if (partscan) 142485b0a54aSJan Kara loop_reread_partitions(lo, bdev); 1425e02898b4SOmar Sandoval 1426ecdd0959SMing Lei return err; 14271da177e4SLinus Torvalds } 14281da177e4SLinus Torvalds 14291da177e4SLinus Torvalds static int 14301da177e4SLinus Torvalds loop_get_status(struct loop_device *lo, struct loop_info64 *info) 14311da177e4SLinus Torvalds { 1432b1ab5fa3STetsuo Handa struct path path; 14331da177e4SLinus Torvalds struct kstat stat; 14342d1d4c1eSOmar Sandoval int ret; 14351da177e4SLinus Torvalds 1436c28445faSJan Kara ret = mutex_lock_killable(&loop_ctl_mutex); 14374a5ce9baSJan Kara if (ret) 14384a5ce9baSJan Kara return ret; 14392d1d4c1eSOmar Sandoval if (lo->lo_state != Lo_bound) { 1440310ca162STetsuo Handa mutex_unlock(&loop_ctl_mutex); 14411da177e4SLinus Torvalds return -ENXIO; 14422d1d4c1eSOmar Sandoval } 14432d1d4c1eSOmar Sandoval 14441da177e4SLinus Torvalds memset(info, 0, sizeof(*info)); 14451da177e4SLinus Torvalds info->lo_number = lo->lo_number; 14461da177e4SLinus Torvalds info->lo_offset = lo->lo_offset; 14471da177e4SLinus Torvalds info->lo_sizelimit = lo->lo_sizelimit; 14481da177e4SLinus Torvalds info->lo_flags = lo->lo_flags; 14491da177e4SLinus Torvalds memcpy(info->lo_file_name, lo->lo_file_name, LO_NAME_SIZE); 14501da177e4SLinus Torvalds memcpy(info->lo_crypt_name, lo->lo_crypt_name, LO_NAME_SIZE); 14511da177e4SLinus Torvalds info->lo_encrypt_type = 14521da177e4SLinus Torvalds lo->lo_encryption ? lo->lo_encryption->number : 0; 14531da177e4SLinus Torvalds if (lo->lo_encrypt_key_size && capable(CAP_SYS_ADMIN)) { 14541da177e4SLinus Torvalds info->lo_encrypt_key_size = lo->lo_encrypt_key_size; 14551da177e4SLinus Torvalds memcpy(info->lo_encrypt_key, lo->lo_encrypt_key, 14561da177e4SLinus Torvalds lo->lo_encrypt_key_size); 14571da177e4SLinus Torvalds } 14582d1d4c1eSOmar Sandoval 1459310ca162STetsuo Handa /* Drop loop_ctl_mutex while we call into the filesystem. */ 1460b1ab5fa3STetsuo Handa path = lo->lo_backing_file->f_path; 1461b1ab5fa3STetsuo Handa path_get(&path); 1462310ca162STetsuo Handa mutex_unlock(&loop_ctl_mutex); 1463b1ab5fa3STetsuo Handa ret = vfs_getattr(&path, &stat, STATX_INO, AT_STATX_SYNC_AS_STAT); 14642d1d4c1eSOmar Sandoval if (!ret) { 14652d1d4c1eSOmar Sandoval info->lo_device = huge_encode_dev(stat.dev); 14662d1d4c1eSOmar Sandoval info->lo_inode = stat.ino; 14672d1d4c1eSOmar Sandoval info->lo_rdevice = huge_encode_dev(stat.rdev); 14682d1d4c1eSOmar Sandoval } 1469b1ab5fa3STetsuo Handa path_put(&path); 14702d1d4c1eSOmar Sandoval return ret; 14711da177e4SLinus Torvalds } 14721da177e4SLinus Torvalds 14731da177e4SLinus Torvalds static void 14741da177e4SLinus Torvalds loop_info64_from_old(const struct loop_info *info, struct loop_info64 *info64) 14751da177e4SLinus Torvalds { 14761da177e4SLinus Torvalds memset(info64, 0, sizeof(*info64)); 14771da177e4SLinus Torvalds info64->lo_number = info->lo_number; 14781da177e4SLinus Torvalds info64->lo_device = info->lo_device; 14791da177e4SLinus Torvalds info64->lo_inode = info->lo_inode; 14801da177e4SLinus Torvalds info64->lo_rdevice = info->lo_rdevice; 14811da177e4SLinus Torvalds info64->lo_offset = info->lo_offset; 14821da177e4SLinus Torvalds info64->lo_sizelimit = 0; 14831da177e4SLinus Torvalds info64->lo_encrypt_type = info->lo_encrypt_type; 14841da177e4SLinus Torvalds info64->lo_encrypt_key_size = info->lo_encrypt_key_size; 14851da177e4SLinus Torvalds info64->lo_flags = info->lo_flags; 14861da177e4SLinus Torvalds info64->lo_init[0] = info->lo_init[0]; 14871da177e4SLinus Torvalds info64->lo_init[1] = info->lo_init[1]; 14881da177e4SLinus Torvalds if (info->lo_encrypt_type == LO_CRYPT_CRYPTOAPI) 14891da177e4SLinus Torvalds memcpy(info64->lo_crypt_name, info->lo_name, LO_NAME_SIZE); 14901da177e4SLinus Torvalds else 14911da177e4SLinus Torvalds memcpy(info64->lo_file_name, info->lo_name, LO_NAME_SIZE); 14921da177e4SLinus Torvalds memcpy(info64->lo_encrypt_key, info->lo_encrypt_key, LO_KEY_SIZE); 14931da177e4SLinus Torvalds } 14941da177e4SLinus Torvalds 14951da177e4SLinus Torvalds static int 14961da177e4SLinus Torvalds loop_info64_to_old(const struct loop_info64 *info64, struct loop_info *info) 14971da177e4SLinus Torvalds { 14981da177e4SLinus Torvalds memset(info, 0, sizeof(*info)); 14991da177e4SLinus Torvalds info->lo_number = info64->lo_number; 15001da177e4SLinus Torvalds info->lo_device = info64->lo_device; 15011da177e4SLinus Torvalds info->lo_inode = info64->lo_inode; 15021da177e4SLinus Torvalds info->lo_rdevice = info64->lo_rdevice; 15031da177e4SLinus Torvalds info->lo_offset = info64->lo_offset; 15041da177e4SLinus Torvalds info->lo_encrypt_type = info64->lo_encrypt_type; 15051da177e4SLinus Torvalds info->lo_encrypt_key_size = info64->lo_encrypt_key_size; 15061da177e4SLinus Torvalds info->lo_flags = info64->lo_flags; 15071da177e4SLinus Torvalds info->lo_init[0] = info64->lo_init[0]; 15081da177e4SLinus Torvalds info->lo_init[1] = info64->lo_init[1]; 15091da177e4SLinus Torvalds if (info->lo_encrypt_type == LO_CRYPT_CRYPTOAPI) 15101da177e4SLinus Torvalds memcpy(info->lo_name, info64->lo_crypt_name, LO_NAME_SIZE); 15111da177e4SLinus Torvalds else 15121da177e4SLinus Torvalds memcpy(info->lo_name, info64->lo_file_name, LO_NAME_SIZE); 15131da177e4SLinus Torvalds memcpy(info->lo_encrypt_key, info64->lo_encrypt_key, LO_KEY_SIZE); 15141da177e4SLinus Torvalds 15151da177e4SLinus Torvalds /* error in case values were truncated */ 15161da177e4SLinus Torvalds if (info->lo_device != info64->lo_device || 15171da177e4SLinus Torvalds info->lo_rdevice != info64->lo_rdevice || 15181da177e4SLinus Torvalds info->lo_inode != info64->lo_inode || 15191da177e4SLinus Torvalds info->lo_offset != info64->lo_offset) 15201da177e4SLinus Torvalds return -EOVERFLOW; 15211da177e4SLinus Torvalds 15221da177e4SLinus Torvalds return 0; 15231da177e4SLinus Torvalds } 15241da177e4SLinus Torvalds 15251da177e4SLinus Torvalds static int 15261da177e4SLinus Torvalds loop_set_status_old(struct loop_device *lo, const struct loop_info __user *arg) 15271da177e4SLinus Torvalds { 15281da177e4SLinus Torvalds struct loop_info info; 15291da177e4SLinus Torvalds struct loop_info64 info64; 15301da177e4SLinus Torvalds 15311da177e4SLinus Torvalds if (copy_from_user(&info, arg, sizeof (struct loop_info))) 15321da177e4SLinus Torvalds return -EFAULT; 15331da177e4SLinus Torvalds loop_info64_from_old(&info, &info64); 15341da177e4SLinus Torvalds return loop_set_status(lo, &info64); 15351da177e4SLinus Torvalds } 15361da177e4SLinus Torvalds 15371da177e4SLinus Torvalds static int 15381da177e4SLinus Torvalds loop_set_status64(struct loop_device *lo, const struct loop_info64 __user *arg) 15391da177e4SLinus Torvalds { 15401da177e4SLinus Torvalds struct loop_info64 info64; 15411da177e4SLinus Torvalds 15421da177e4SLinus Torvalds if (copy_from_user(&info64, arg, sizeof (struct loop_info64))) 15431da177e4SLinus Torvalds return -EFAULT; 15441da177e4SLinus Torvalds return loop_set_status(lo, &info64); 15451da177e4SLinus Torvalds } 15461da177e4SLinus Torvalds 15471da177e4SLinus Torvalds static int 15481da177e4SLinus Torvalds loop_get_status_old(struct loop_device *lo, struct loop_info __user *arg) { 15491da177e4SLinus Torvalds struct loop_info info; 15501da177e4SLinus Torvalds struct loop_info64 info64; 1551bdac616dSOmar Sandoval int err; 15521da177e4SLinus Torvalds 15534a5ce9baSJan Kara if (!arg) 1554bdac616dSOmar Sandoval return -EINVAL; 15551da177e4SLinus Torvalds err = loop_get_status(lo, &info64); 15561da177e4SLinus Torvalds if (!err) 15571da177e4SLinus Torvalds err = loop_info64_to_old(&info64, &info); 15581da177e4SLinus Torvalds if (!err && copy_to_user(arg, &info, sizeof(info))) 15591da177e4SLinus Torvalds err = -EFAULT; 15601da177e4SLinus Torvalds 15611da177e4SLinus Torvalds return err; 15621da177e4SLinus Torvalds } 15631da177e4SLinus Torvalds 15641da177e4SLinus Torvalds static int 15651da177e4SLinus Torvalds loop_get_status64(struct loop_device *lo, struct loop_info64 __user *arg) { 15661da177e4SLinus Torvalds struct loop_info64 info64; 1567bdac616dSOmar Sandoval int err; 15681da177e4SLinus Torvalds 15694a5ce9baSJan Kara if (!arg) 1570bdac616dSOmar Sandoval return -EINVAL; 15711da177e4SLinus Torvalds err = loop_get_status(lo, &info64); 15721da177e4SLinus Torvalds if (!err && copy_to_user(arg, &info64, sizeof(info64))) 15731da177e4SLinus Torvalds err = -EFAULT; 15741da177e4SLinus Torvalds 15751da177e4SLinus Torvalds return err; 15761da177e4SLinus Torvalds } 15771da177e4SLinus Torvalds 157851001b7dSHannes Reinecke static int loop_set_capacity(struct loop_device *lo) 157953d66608SJ. R. Okajima { 15800a6ed1b5SMartijn Coenen loff_t size; 15810a6ed1b5SMartijn Coenen 158253d66608SJ. R. Okajima if (unlikely(lo->lo_state != Lo_bound)) 15837b0576a3SGuo Chao return -ENXIO; 158453d66608SJ. R. Okajima 15850a6ed1b5SMartijn Coenen size = get_loop_size(lo, lo->lo_backing_file); 15860a6ed1b5SMartijn Coenen loop_set_size(lo, size); 1587083a6a50SMartijn Coenen 1588083a6a50SMartijn Coenen return 0; 158953d66608SJ. R. Okajima } 159053d66608SJ. R. Okajima 1591ab1cb278SMing Lei static int loop_set_dio(struct loop_device *lo, unsigned long arg) 1592ab1cb278SMing Lei { 1593ab1cb278SMing Lei int error = -ENXIO; 1594ab1cb278SMing Lei if (lo->lo_state != Lo_bound) 1595ab1cb278SMing Lei goto out; 1596ab1cb278SMing Lei 1597ab1cb278SMing Lei __loop_update_dio(lo, !!arg); 1598ab1cb278SMing Lei if (lo->use_dio == !!arg) 1599ab1cb278SMing Lei return 0; 1600ab1cb278SMing Lei error = -EINVAL; 1601ab1cb278SMing Lei out: 1602ab1cb278SMing Lei return error; 1603ab1cb278SMing Lei } 1604ab1cb278SMing Lei 160589e4fdecSOmar Sandoval static int loop_set_block_size(struct loop_device *lo, unsigned long arg) 160689e4fdecSOmar Sandoval { 16075db470e2SJaegeuk Kim int err = 0; 16085db470e2SJaegeuk Kim 160989e4fdecSOmar Sandoval if (lo->lo_state != Lo_bound) 161089e4fdecSOmar Sandoval return -ENXIO; 161189e4fdecSOmar Sandoval 16123448914eSMartijn Coenen err = loop_validate_block_size(arg); 16133448914eSMartijn Coenen if (err) 16143448914eSMartijn Coenen return err; 161589e4fdecSOmar Sandoval 16167e81f99aSMartijn Coenen if (lo->lo_queue->limits.logical_block_size == arg) 16177e81f99aSMartijn Coenen return 0; 16187e81f99aSMartijn Coenen 16195db470e2SJaegeuk Kim sync_blockdev(lo->lo_device); 1620f4bd34b1SZheng Bin invalidate_bdev(lo->lo_device); 16215db470e2SJaegeuk Kim 162289e4fdecSOmar Sandoval blk_mq_freeze_queue(lo->lo_queue); 162389e4fdecSOmar Sandoval 1624f4bd34b1SZheng Bin /* invalidate_bdev should have truncated all the pages */ 16257e81f99aSMartijn Coenen if (lo->lo_device->bd_inode->i_mapping->nrpages) { 16265db470e2SJaegeuk Kim err = -EAGAIN; 16275db470e2SJaegeuk Kim pr_warn("%s: loop%d (%s) has still dirty pages (nrpages=%lu)\n", 16285db470e2SJaegeuk Kim __func__, lo->lo_number, lo->lo_file_name, 16295db470e2SJaegeuk Kim lo->lo_device->bd_inode->i_mapping->nrpages); 16305db470e2SJaegeuk Kim goto out_unfreeze; 16315db470e2SJaegeuk Kim } 16325db470e2SJaegeuk Kim 163389e4fdecSOmar Sandoval blk_queue_logical_block_size(lo->lo_queue, arg); 1634bf093753SOmar Sandoval blk_queue_physical_block_size(lo->lo_queue, arg); 1635bf093753SOmar Sandoval blk_queue_io_min(lo->lo_queue, arg); 163689e4fdecSOmar Sandoval loop_update_dio(lo); 16375db470e2SJaegeuk Kim out_unfreeze: 163889e4fdecSOmar Sandoval blk_mq_unfreeze_queue(lo->lo_queue); 163989e4fdecSOmar Sandoval 16405db470e2SJaegeuk Kim return err; 164189e4fdecSOmar Sandoval } 164289e4fdecSOmar Sandoval 1643a1316544SJan Kara static int lo_simple_ioctl(struct loop_device *lo, unsigned int cmd, 1644a1316544SJan Kara unsigned long arg) 16451da177e4SLinus Torvalds { 16461da177e4SLinus Torvalds int err; 16471da177e4SLinus Torvalds 1648c28445faSJan Kara err = mutex_lock_killable(&loop_ctl_mutex); 16493148ffbdSOmar Sandoval if (err) 1650a1316544SJan Kara return err; 16511da177e4SLinus Torvalds switch (cmd) { 165253d66608SJ. R. Okajima case LOOP_SET_CAPACITY: 165351001b7dSHannes Reinecke err = loop_set_capacity(lo); 165453d66608SJ. R. Okajima break; 1655ab1cb278SMing Lei case LOOP_SET_DIRECT_IO: 1656ab1cb278SMing Lei err = loop_set_dio(lo, arg); 1657ab1cb278SMing Lei break; 165889e4fdecSOmar Sandoval case LOOP_SET_BLOCK_SIZE: 165989e4fdecSOmar Sandoval err = loop_set_block_size(lo, arg); 166089e4fdecSOmar Sandoval break; 16611da177e4SLinus Torvalds default: 16621da177e4SLinus Torvalds err = lo->ioctl ? lo->ioctl(lo, cmd, arg) : -EINVAL; 16631da177e4SLinus Torvalds } 1664310ca162STetsuo Handa mutex_unlock(&loop_ctl_mutex); 1665a1316544SJan Kara return err; 1666a1316544SJan Kara } 1667f028f3b2SNikanth Karthikesan 1668a1316544SJan Kara static int lo_ioctl(struct block_device *bdev, fmode_t mode, 1669a1316544SJan Kara unsigned int cmd, unsigned long arg) 1670a1316544SJan Kara { 1671a1316544SJan Kara struct loop_device *lo = bdev->bd_disk->private_data; 1672571fae6eSMartijn Coenen void __user *argp = (void __user *) arg; 1673a1316544SJan Kara int err; 1674a1316544SJan Kara 1675a1316544SJan Kara switch (cmd) { 16763448914eSMartijn Coenen case LOOP_SET_FD: { 16773448914eSMartijn Coenen /* 16783448914eSMartijn Coenen * Legacy case - pass in a zeroed out struct loop_config with 16793448914eSMartijn Coenen * only the file descriptor set , which corresponds with the 16803448914eSMartijn Coenen * default parameters we'd have used otherwise. 16813448914eSMartijn Coenen */ 16823448914eSMartijn Coenen struct loop_config config; 16833448914eSMartijn Coenen 16843448914eSMartijn Coenen memset(&config, 0, sizeof(config)); 16853448914eSMartijn Coenen config.fd = arg; 16863448914eSMartijn Coenen 16873448914eSMartijn Coenen return loop_configure(lo, mode, bdev, &config); 16883448914eSMartijn Coenen } 16893448914eSMartijn Coenen case LOOP_CONFIGURE: { 16903448914eSMartijn Coenen struct loop_config config; 16913448914eSMartijn Coenen 16923448914eSMartijn Coenen if (copy_from_user(&config, argp, sizeof(config))) 16933448914eSMartijn Coenen return -EFAULT; 16943448914eSMartijn Coenen 16953448914eSMartijn Coenen return loop_configure(lo, mode, bdev, &config); 16963448914eSMartijn Coenen } 1697a1316544SJan Kara case LOOP_CHANGE_FD: 1698c3710770SJan Kara return loop_change_fd(lo, bdev, arg); 1699a1316544SJan Kara case LOOP_CLR_FD: 17007ccd0791SJan Kara return loop_clr_fd(lo); 1701a1316544SJan Kara case LOOP_SET_STATUS: 1702a1316544SJan Kara err = -EPERM; 1703a1316544SJan Kara if ((mode & FMODE_WRITE) || capable(CAP_SYS_ADMIN)) { 1704571fae6eSMartijn Coenen err = loop_set_status_old(lo, argp); 1705a1316544SJan Kara } 1706a1316544SJan Kara break; 1707a1316544SJan Kara case LOOP_GET_STATUS: 1708571fae6eSMartijn Coenen return loop_get_status_old(lo, argp); 1709a1316544SJan Kara case LOOP_SET_STATUS64: 1710a1316544SJan Kara err = -EPERM; 1711a1316544SJan Kara if ((mode & FMODE_WRITE) || capable(CAP_SYS_ADMIN)) { 1712571fae6eSMartijn Coenen err = loop_set_status64(lo, argp); 1713a1316544SJan Kara } 1714a1316544SJan Kara break; 1715a1316544SJan Kara case LOOP_GET_STATUS64: 1716571fae6eSMartijn Coenen return loop_get_status64(lo, argp); 1717a1316544SJan Kara case LOOP_SET_CAPACITY: 1718a1316544SJan Kara case LOOP_SET_DIRECT_IO: 1719a1316544SJan Kara case LOOP_SET_BLOCK_SIZE: 1720a1316544SJan Kara if (!(mode & FMODE_WRITE) && !capable(CAP_SYS_ADMIN)) 1721a1316544SJan Kara return -EPERM; 1722a1316544SJan Kara /* Fall through */ 1723a1316544SJan Kara default: 1724a1316544SJan Kara err = lo_simple_ioctl(lo, cmd, arg); 1725a1316544SJan Kara break; 1726a1316544SJan Kara } 1727a1316544SJan Kara 17281da177e4SLinus Torvalds return err; 17291da177e4SLinus Torvalds } 17301da177e4SLinus Torvalds 1731863d5b82SDavid Howells #ifdef CONFIG_COMPAT 1732863d5b82SDavid Howells struct compat_loop_info { 1733863d5b82SDavid Howells compat_int_t lo_number; /* ioctl r/o */ 1734863d5b82SDavid Howells compat_dev_t lo_device; /* ioctl r/o */ 1735863d5b82SDavid Howells compat_ulong_t lo_inode; /* ioctl r/o */ 1736863d5b82SDavid Howells compat_dev_t lo_rdevice; /* ioctl r/o */ 1737863d5b82SDavid Howells compat_int_t lo_offset; 1738863d5b82SDavid Howells compat_int_t lo_encrypt_type; 1739863d5b82SDavid Howells compat_int_t lo_encrypt_key_size; /* ioctl w/o */ 1740863d5b82SDavid Howells compat_int_t lo_flags; /* ioctl r/o */ 1741863d5b82SDavid Howells char lo_name[LO_NAME_SIZE]; 1742863d5b82SDavid Howells unsigned char lo_encrypt_key[LO_KEY_SIZE]; /* ioctl w/o */ 1743863d5b82SDavid Howells compat_ulong_t lo_init[2]; 1744863d5b82SDavid Howells char reserved[4]; 1745863d5b82SDavid Howells }; 1746863d5b82SDavid Howells 1747863d5b82SDavid Howells /* 1748863d5b82SDavid Howells * Transfer 32-bit compatibility structure in userspace to 64-bit loop info 1749863d5b82SDavid Howells * - noinlined to reduce stack space usage in main part of driver 1750863d5b82SDavid Howells */ 1751863d5b82SDavid Howells static noinline int 1752ba674cfcSAl Viro loop_info64_from_compat(const struct compat_loop_info __user *arg, 1753863d5b82SDavid Howells struct loop_info64 *info64) 1754863d5b82SDavid Howells { 1755863d5b82SDavid Howells struct compat_loop_info info; 1756863d5b82SDavid Howells 1757863d5b82SDavid Howells if (copy_from_user(&info, arg, sizeof(info))) 1758863d5b82SDavid Howells return -EFAULT; 1759863d5b82SDavid Howells 1760863d5b82SDavid Howells memset(info64, 0, sizeof(*info64)); 1761863d5b82SDavid Howells info64->lo_number = info.lo_number; 1762863d5b82SDavid Howells info64->lo_device = info.lo_device; 1763863d5b82SDavid Howells info64->lo_inode = info.lo_inode; 1764863d5b82SDavid Howells info64->lo_rdevice = info.lo_rdevice; 1765863d5b82SDavid Howells info64->lo_offset = info.lo_offset; 1766863d5b82SDavid Howells info64->lo_sizelimit = 0; 1767863d5b82SDavid Howells info64->lo_encrypt_type = info.lo_encrypt_type; 1768863d5b82SDavid Howells info64->lo_encrypt_key_size = info.lo_encrypt_key_size; 1769863d5b82SDavid Howells info64->lo_flags = info.lo_flags; 1770863d5b82SDavid Howells info64->lo_init[0] = info.lo_init[0]; 1771863d5b82SDavid Howells info64->lo_init[1] = info.lo_init[1]; 1772863d5b82SDavid Howells if (info.lo_encrypt_type == LO_CRYPT_CRYPTOAPI) 1773863d5b82SDavid Howells memcpy(info64->lo_crypt_name, info.lo_name, LO_NAME_SIZE); 1774863d5b82SDavid Howells else 1775863d5b82SDavid Howells memcpy(info64->lo_file_name, info.lo_name, LO_NAME_SIZE); 1776863d5b82SDavid Howells memcpy(info64->lo_encrypt_key, info.lo_encrypt_key, LO_KEY_SIZE); 1777863d5b82SDavid Howells return 0; 1778863d5b82SDavid Howells } 1779863d5b82SDavid Howells 1780863d5b82SDavid Howells /* 1781863d5b82SDavid Howells * Transfer 64-bit loop info to 32-bit compatibility structure in userspace 1782863d5b82SDavid Howells * - noinlined to reduce stack space usage in main part of driver 1783863d5b82SDavid Howells */ 1784863d5b82SDavid Howells static noinline int 1785863d5b82SDavid Howells loop_info64_to_compat(const struct loop_info64 *info64, 1786863d5b82SDavid Howells struct compat_loop_info __user *arg) 1787863d5b82SDavid Howells { 1788863d5b82SDavid Howells struct compat_loop_info info; 1789863d5b82SDavid Howells 1790863d5b82SDavid Howells memset(&info, 0, sizeof(info)); 1791863d5b82SDavid Howells info.lo_number = info64->lo_number; 1792863d5b82SDavid Howells info.lo_device = info64->lo_device; 1793863d5b82SDavid Howells info.lo_inode = info64->lo_inode; 1794863d5b82SDavid Howells info.lo_rdevice = info64->lo_rdevice; 1795863d5b82SDavid Howells info.lo_offset = info64->lo_offset; 1796863d5b82SDavid Howells info.lo_encrypt_type = info64->lo_encrypt_type; 1797863d5b82SDavid Howells info.lo_encrypt_key_size = info64->lo_encrypt_key_size; 1798863d5b82SDavid Howells info.lo_flags = info64->lo_flags; 1799863d5b82SDavid Howells info.lo_init[0] = info64->lo_init[0]; 1800863d5b82SDavid Howells info.lo_init[1] = info64->lo_init[1]; 1801863d5b82SDavid Howells if (info.lo_encrypt_type == LO_CRYPT_CRYPTOAPI) 1802863d5b82SDavid Howells memcpy(info.lo_name, info64->lo_crypt_name, LO_NAME_SIZE); 1803863d5b82SDavid Howells else 1804863d5b82SDavid Howells memcpy(info.lo_name, info64->lo_file_name, LO_NAME_SIZE); 1805863d5b82SDavid Howells memcpy(info.lo_encrypt_key, info64->lo_encrypt_key, LO_KEY_SIZE); 1806863d5b82SDavid Howells 1807863d5b82SDavid Howells /* error in case values were truncated */ 1808863d5b82SDavid Howells if (info.lo_device != info64->lo_device || 1809863d5b82SDavid Howells info.lo_rdevice != info64->lo_rdevice || 1810863d5b82SDavid Howells info.lo_inode != info64->lo_inode || 1811863d5b82SDavid Howells info.lo_offset != info64->lo_offset || 1812863d5b82SDavid Howells info.lo_init[0] != info64->lo_init[0] || 1813863d5b82SDavid Howells info.lo_init[1] != info64->lo_init[1]) 1814863d5b82SDavid Howells return -EOVERFLOW; 1815863d5b82SDavid Howells 1816863d5b82SDavid Howells if (copy_to_user(arg, &info, sizeof(info))) 1817863d5b82SDavid Howells return -EFAULT; 1818863d5b82SDavid Howells return 0; 1819863d5b82SDavid Howells } 1820863d5b82SDavid Howells 1821863d5b82SDavid Howells static int 1822863d5b82SDavid Howells loop_set_status_compat(struct loop_device *lo, 1823863d5b82SDavid Howells const struct compat_loop_info __user *arg) 1824863d5b82SDavid Howells { 1825863d5b82SDavid Howells struct loop_info64 info64; 1826863d5b82SDavid Howells int ret; 1827863d5b82SDavid Howells 1828863d5b82SDavid Howells ret = loop_info64_from_compat(arg, &info64); 1829863d5b82SDavid Howells if (ret < 0) 1830863d5b82SDavid Howells return ret; 1831863d5b82SDavid Howells return loop_set_status(lo, &info64); 1832863d5b82SDavid Howells } 1833863d5b82SDavid Howells 1834863d5b82SDavid Howells static int 1835863d5b82SDavid Howells loop_get_status_compat(struct loop_device *lo, 1836863d5b82SDavid Howells struct compat_loop_info __user *arg) 1837863d5b82SDavid Howells { 1838863d5b82SDavid Howells struct loop_info64 info64; 1839bdac616dSOmar Sandoval int err; 1840863d5b82SDavid Howells 18414a5ce9baSJan Kara if (!arg) 1842bdac616dSOmar Sandoval return -EINVAL; 1843863d5b82SDavid Howells err = loop_get_status(lo, &info64); 1844863d5b82SDavid Howells if (!err) 1845863d5b82SDavid Howells err = loop_info64_to_compat(&info64, arg); 1846863d5b82SDavid Howells return err; 1847863d5b82SDavid Howells } 1848863d5b82SDavid Howells 1849bb214884SAl Viro static int lo_compat_ioctl(struct block_device *bdev, fmode_t mode, 1850bb214884SAl Viro unsigned int cmd, unsigned long arg) 1851863d5b82SDavid Howells { 1852bb214884SAl Viro struct loop_device *lo = bdev->bd_disk->private_data; 1853863d5b82SDavid Howells int err; 1854863d5b82SDavid Howells 1855863d5b82SDavid Howells switch(cmd) { 1856863d5b82SDavid Howells case LOOP_SET_STATUS: 18573148ffbdSOmar Sandoval err = loop_set_status_compat(lo, 18583148ffbdSOmar Sandoval (const struct compat_loop_info __user *)arg); 1859863d5b82SDavid Howells break; 1860863d5b82SDavid Howells case LOOP_GET_STATUS: 18613148ffbdSOmar Sandoval err = loop_get_status_compat(lo, 18623148ffbdSOmar Sandoval (struct compat_loop_info __user *)arg); 1863863d5b82SDavid Howells break; 186453d66608SJ. R. Okajima case LOOP_SET_CAPACITY: 1865863d5b82SDavid Howells case LOOP_CLR_FD: 1866863d5b82SDavid Howells case LOOP_GET_STATUS64: 1867863d5b82SDavid Howells case LOOP_SET_STATUS64: 18683448914eSMartijn Coenen case LOOP_CONFIGURE: 1869863d5b82SDavid Howells arg = (unsigned long) compat_ptr(arg); 1870d893ff86SGustavo A. R. Silva /* fall through */ 1871863d5b82SDavid Howells case LOOP_SET_FD: 1872863d5b82SDavid Howells case LOOP_CHANGE_FD: 18739fea4b39SEvan Green case LOOP_SET_BLOCK_SIZE: 1874fdbe4eeeSAlessio Balsini case LOOP_SET_DIRECT_IO: 1875bb214884SAl Viro err = lo_ioctl(bdev, mode, cmd, arg); 1876863d5b82SDavid Howells break; 1877863d5b82SDavid Howells default: 1878863d5b82SDavid Howells err = -ENOIOCTLCMD; 1879863d5b82SDavid Howells break; 1880863d5b82SDavid Howells } 1881863d5b82SDavid Howells return err; 1882863d5b82SDavid Howells } 1883863d5b82SDavid Howells #endif 1884863d5b82SDavid Howells 1885bb214884SAl Viro static int lo_open(struct block_device *bdev, fmode_t mode) 18861da177e4SLinus Torvalds { 1887770fe30aSKay Sievers struct loop_device *lo; 18880a42e99bSJan Kara int err; 1889770fe30aSKay Sievers 18900a42e99bSJan Kara err = mutex_lock_killable(&loop_ctl_mutex); 18910a42e99bSJan Kara if (err) 18920a42e99bSJan Kara return err; 1893770fe30aSKay Sievers lo = bdev->bd_disk->private_data; 1894770fe30aSKay Sievers if (!lo) { 1895770fe30aSKay Sievers err = -ENXIO; 1896770fe30aSKay Sievers goto out; 1897770fe30aSKay Sievers } 18981da177e4SLinus Torvalds 1899f8933667SMing Lei atomic_inc(&lo->lo_refcnt); 1900770fe30aSKay Sievers out: 19010a42e99bSJan Kara mutex_unlock(&loop_ctl_mutex); 1902770fe30aSKay Sievers return err; 19031da177e4SLinus Torvalds } 19041da177e4SLinus Torvalds 1905967d1dc1SJan Kara static void lo_release(struct gendisk *disk, fmode_t mode) 19061da177e4SLinus Torvalds { 1907967d1dc1SJan Kara struct loop_device *lo; 19081da177e4SLinus Torvalds 19090a42e99bSJan Kara mutex_lock(&loop_ctl_mutex); 1910967d1dc1SJan Kara lo = disk->private_data; 1911f8933667SMing Lei if (atomic_dec_return(&lo->lo_refcnt)) 19120a42e99bSJan Kara goto out_unlock; 1913f8933667SMing Lei 191414f27939SMilan Broz if (lo->lo_flags & LO_FLAGS_AUTOCLEAR) { 1915a2505b79SJan Kara if (lo->lo_state != Lo_bound) 1916a2505b79SJan Kara goto out_unlock; 1917a2505b79SJan Kara lo->lo_state = Lo_rundown; 19187ccd0791SJan Kara mutex_unlock(&loop_ctl_mutex); 191914f27939SMilan Broz /* 192014f27939SMilan Broz * In autoclear mode, stop the loop thread 192114f27939SMilan Broz * and remove configuration after last close. 192214f27939SMilan Broz */ 19230da03cabSJan Kara __loop_clr_fd(lo, true); 19240a42e99bSJan Kara return; 192543cade80SOmar Sandoval } else if (lo->lo_state == Lo_bound) { 192614f27939SMilan Broz /* 192714f27939SMilan Broz * Otherwise keep thread (if running) and config, 192814f27939SMilan Broz * but flush possible ongoing bios in thread. 192914f27939SMilan Broz */ 193043cade80SOmar Sandoval blk_mq_freeze_queue(lo->lo_queue); 193143cade80SOmar Sandoval blk_mq_unfreeze_queue(lo->lo_queue); 193214f27939SMilan Broz } 193396c58655SDavid Woodhouse 19340a42e99bSJan Kara out_unlock: 1935310ca162STetsuo Handa mutex_unlock(&loop_ctl_mutex); 1936ae665016SLinus Torvalds } 1937ae665016SLinus Torvalds 193883d5cde4SAlexey Dobriyan static const struct block_device_operations lo_fops = { 19391da177e4SLinus Torvalds .owner = THIS_MODULE, 1940bb214884SAl Viro .open = lo_open, 1941bb214884SAl Viro .release = lo_release, 1942bb214884SAl Viro .ioctl = lo_ioctl, 1943863d5b82SDavid Howells #ifdef CONFIG_COMPAT 1944bb214884SAl Viro .compat_ioctl = lo_compat_ioctl, 1945863d5b82SDavid Howells #endif 19461da177e4SLinus Torvalds }; 19471da177e4SLinus Torvalds 19481da177e4SLinus Torvalds /* 19491da177e4SLinus Torvalds * And now the modules code and kernel interface. 19501da177e4SLinus Torvalds */ 195173285082SKen Chen static int max_loop; 19525657a819SJoe Perches module_param(max_loop, int, 0444); 1953a47653fcSKen Chen MODULE_PARM_DESC(max_loop, "Maximum number of loop devices"); 19545657a819SJoe Perches module_param(max_part, int, 0444); 1955476a4813SLaurent Vivier MODULE_PARM_DESC(max_part, "Maximum number of partitions per loop device"); 19561da177e4SLinus Torvalds MODULE_LICENSE("GPL"); 19571da177e4SLinus Torvalds MODULE_ALIAS_BLOCKDEV_MAJOR(LOOP_MAJOR); 19581da177e4SLinus Torvalds 19591da177e4SLinus Torvalds int loop_register_transfer(struct loop_func_table *funcs) 19601da177e4SLinus Torvalds { 19611da177e4SLinus Torvalds unsigned int n = funcs->number; 19621da177e4SLinus Torvalds 19631da177e4SLinus Torvalds if (n >= MAX_LO_CRYPT || xfer_funcs[n]) 19641da177e4SLinus Torvalds return -EINVAL; 19651da177e4SLinus Torvalds xfer_funcs[n] = funcs; 19661da177e4SLinus Torvalds return 0; 19671da177e4SLinus Torvalds } 19681da177e4SLinus Torvalds 196934dd82afSKay Sievers static int unregister_transfer_cb(int id, void *ptr, void *data) 197034dd82afSKay Sievers { 197134dd82afSKay Sievers struct loop_device *lo = ptr; 197234dd82afSKay Sievers struct loop_func_table *xfer = data; 197334dd82afSKay Sievers 1974310ca162STetsuo Handa mutex_lock(&loop_ctl_mutex); 197534dd82afSKay Sievers if (lo->lo_encryption == xfer) 197634dd82afSKay Sievers loop_release_xfer(lo); 1977310ca162STetsuo Handa mutex_unlock(&loop_ctl_mutex); 197834dd82afSKay Sievers return 0; 197934dd82afSKay Sievers } 198034dd82afSKay Sievers 19811da177e4SLinus Torvalds int loop_unregister_transfer(int number) 19821da177e4SLinus Torvalds { 19831da177e4SLinus Torvalds unsigned int n = number; 19841da177e4SLinus Torvalds struct loop_func_table *xfer; 19851da177e4SLinus Torvalds 19861da177e4SLinus Torvalds if (n == 0 || n >= MAX_LO_CRYPT || (xfer = xfer_funcs[n]) == NULL) 19871da177e4SLinus Torvalds return -EINVAL; 19881da177e4SLinus Torvalds 19891da177e4SLinus Torvalds xfer_funcs[n] = NULL; 199034dd82afSKay Sievers idr_for_each(&loop_index_idr, &unregister_transfer_cb, xfer); 19911da177e4SLinus Torvalds return 0; 19921da177e4SLinus Torvalds } 19931da177e4SLinus Torvalds 19941da177e4SLinus Torvalds EXPORT_SYMBOL(loop_register_transfer); 19951da177e4SLinus Torvalds EXPORT_SYMBOL(loop_unregister_transfer); 19961da177e4SLinus Torvalds 1997fc17b653SChristoph Hellwig static blk_status_t loop_queue_rq(struct blk_mq_hw_ctx *hctx, 1998b5dd2f60SMing Lei const struct blk_mq_queue_data *bd) 1999b5dd2f60SMing Lei { 20001894e916SJens Axboe struct request *rq = bd->rq; 20011894e916SJens Axboe struct loop_cmd *cmd = blk_mq_rq_to_pdu(rq); 20021894e916SJens Axboe struct loop_device *lo = rq->q->queuedata; 2003b5dd2f60SMing Lei 20041894e916SJens Axboe blk_mq_start_request(rq); 2005b5dd2f60SMing Lei 2006f4aa4c7bSMing Lei if (lo->lo_state != Lo_bound) 2007fc17b653SChristoph Hellwig return BLK_STS_IOERR; 2008f4aa4c7bSMing Lei 20091894e916SJens Axboe switch (req_op(rq)) { 2010f0225cacSChristoph Hellwig case REQ_OP_FLUSH: 2011f0225cacSChristoph Hellwig case REQ_OP_DISCARD: 201219372e27SChristoph Hellwig case REQ_OP_WRITE_ZEROES: 2013bc07c10aSMing Lei cmd->use_aio = false; 2014f0225cacSChristoph Hellwig break; 2015f0225cacSChristoph Hellwig default: 2016f0225cacSChristoph Hellwig cmd->use_aio = lo->use_dio; 2017f0225cacSChristoph Hellwig break; 2018f0225cacSChristoph Hellwig } 2019bc07c10aSMing Lei 2020d4478e92SShaohua Li /* always use the first bio's css */ 20210b508bc9SShaohua Li #ifdef CONFIG_BLK_CGROUP 2022db6638d7SDennis Zhou if (cmd->use_aio && rq->bio && rq->bio->bi_blkg) { 2023db6638d7SDennis Zhou cmd->css = &bio_blkcg(rq->bio)->css; 2024d4478e92SShaohua Li css_get(cmd->css); 2025d4478e92SShaohua Li } else 2026d4478e92SShaohua Li #endif 2027d4478e92SShaohua Li cmd->css = NULL; 20283989144fSPetr Mladek kthread_queue_work(&lo->worker, &cmd->work); 2029b5dd2f60SMing Lei 2030fc17b653SChristoph Hellwig return BLK_STS_OK; 2031b5dd2f60SMing Lei } 2032b5dd2f60SMing Lei 2033b5dd2f60SMing Lei static void loop_handle_cmd(struct loop_cmd *cmd) 2034b5dd2f60SMing Lei { 20351894e916SJens Axboe struct request *rq = blk_mq_rq_from_pdu(cmd); 20361894e916SJens Axboe const bool write = op_is_write(req_op(rq)); 20371894e916SJens Axboe struct loop_device *lo = rq->q->queuedata; 2038f4829a9bSChristoph Hellwig int ret = 0; 2039b5dd2f60SMing Lei 2040f4829a9bSChristoph Hellwig if (write && (lo->lo_flags & LO_FLAGS_READ_ONLY)) { 2041f4829a9bSChristoph Hellwig ret = -EIO; 2042b5dd2f60SMing Lei goto failed; 2043f4829a9bSChristoph Hellwig } 2044b5dd2f60SMing Lei 20451894e916SJens Axboe ret = do_req_filebacked(lo, rq); 2046b5dd2f60SMing Lei failed: 2047bc07c10aSMing Lei /* complete non-aio request */ 2048fe2cb290SChristoph Hellwig if (!cmd->use_aio || ret) { 20498cd55087SEvan Green if (ret == -EOPNOTSUPP) 20508cd55087SEvan Green cmd->ret = ret; 20518cd55087SEvan Green else 2052fe2cb290SChristoph Hellwig cmd->ret = ret ? -EIO : 0; 205315f73f5bSChristoph Hellwig if (likely(!blk_should_fake_timeout(rq->q))) 20541894e916SJens Axboe blk_mq_complete_request(rq); 2055fe2cb290SChristoph Hellwig } 2056b5dd2f60SMing Lei } 2057b5dd2f60SMing Lei 2058e03a3d7aSMing Lei static void loop_queue_work(struct kthread_work *work) 2059b5dd2f60SMing Lei { 2060b5dd2f60SMing Lei struct loop_cmd *cmd = 2061e03a3d7aSMing Lei container_of(work, struct loop_cmd, work); 2062b5dd2f60SMing Lei 2063b5dd2f60SMing Lei loop_handle_cmd(cmd); 2064b5dd2f60SMing Lei } 2065b5dd2f60SMing Lei 2066d6296d39SChristoph Hellwig static int loop_init_request(struct blk_mq_tag_set *set, struct request *rq, 2067d6296d39SChristoph Hellwig unsigned int hctx_idx, unsigned int numa_node) 2068b5dd2f60SMing Lei { 2069b5dd2f60SMing Lei struct loop_cmd *cmd = blk_mq_rq_to_pdu(rq); 2070b5dd2f60SMing Lei 20713989144fSPetr Mladek kthread_init_work(&cmd->work, loop_queue_work); 2072b5dd2f60SMing Lei return 0; 2073b5dd2f60SMing Lei } 2074b5dd2f60SMing Lei 2075f363b089SEric Biggers static const struct blk_mq_ops loop_mq_ops = { 2076b5dd2f60SMing Lei .queue_rq = loop_queue_rq, 2077b5dd2f60SMing Lei .init_request = loop_init_request, 2078fe2cb290SChristoph Hellwig .complete = lo_complete_rq, 2079b5dd2f60SMing Lei }; 2080b5dd2f60SMing Lei 208134dd82afSKay Sievers static int loop_add(struct loop_device **l, int i) 20821da177e4SLinus Torvalds { 208373285082SKen Chen struct loop_device *lo; 208473285082SKen Chen struct gendisk *disk; 208534dd82afSKay Sievers int err; 20861da177e4SLinus Torvalds 208734dd82afSKay Sievers err = -ENOMEM; 208868d740d7SSilva Paulo lo = kzalloc(sizeof(*lo), GFP_KERNEL); 208968d740d7SSilva Paulo if (!lo) 209073285082SKen Chen goto out; 209134dd82afSKay Sievers 2092ef7e7c82SMikulas Patocka lo->lo_state = Lo_unbound; 2093ef7e7c82SMikulas Patocka 2094c718aa65STejun Heo /* allocate id, if @id >= 0, we're requesting that specific id */ 209534dd82afSKay Sievers if (i >= 0) { 2096c718aa65STejun Heo err = idr_alloc(&loop_index_idr, lo, i, i + 1, GFP_KERNEL); 2097c718aa65STejun Heo if (err == -ENOSPC) 209834dd82afSKay Sievers err = -EEXIST; 209934dd82afSKay Sievers } else { 2100c718aa65STejun Heo err = idr_alloc(&loop_index_idr, lo, 0, 0, GFP_KERNEL); 210134dd82afSKay Sievers } 210234dd82afSKay Sievers if (err < 0) 210334dd82afSKay Sievers goto out_free_dev; 2104c718aa65STejun Heo i = err; 21051da177e4SLinus Torvalds 2106183cfb57SWei Yongjun err = -ENOMEM; 2107b5dd2f60SMing Lei lo->tag_set.ops = &loop_mq_ops; 2108b5dd2f60SMing Lei lo->tag_set.nr_hw_queues = 1; 2109b5dd2f60SMing Lei lo->tag_set.queue_depth = 128; 2110b5dd2f60SMing Lei lo->tag_set.numa_node = NUMA_NO_NODE; 2111b5dd2f60SMing Lei lo->tag_set.cmd_size = sizeof(struct loop_cmd); 2112bf0beec0SMing Lei lo->tag_set.flags = BLK_MQ_F_SHOULD_MERGE | BLK_MQ_F_STACKING; 2113b5dd2f60SMing Lei lo->tag_set.driver_data = lo; 2114b5dd2f60SMing Lei 2115b5dd2f60SMing Lei err = blk_mq_alloc_tag_set(&lo->tag_set); 2116b5dd2f60SMing Lei if (err) 21173ec981e3SMikulas Patocka goto out_free_idr; 211873285082SKen Chen 2119b5dd2f60SMing Lei lo->lo_queue = blk_mq_init_queue(&lo->tag_set); 212038a3499fSChengguang Xu if (IS_ERR(lo->lo_queue)) { 2121b5dd2f60SMing Lei err = PTR_ERR(lo->lo_queue); 2122b5dd2f60SMing Lei goto out_cleanup_tags; 2123b5dd2f60SMing Lei } 2124ef7e7c82SMikulas Patocka lo->lo_queue->queuedata = lo; 2125ef7e7c82SMikulas Patocka 212654bb0adeSShaohua Li blk_queue_max_hw_sectors(lo->lo_queue, BLK_DEF_MAX_SECTORS); 212740326d8aSShaohua Li 21285b5e20f4SMing Lei /* 212940326d8aSShaohua Li * By default, we do buffer IO, so it doesn't make sense to enable 213040326d8aSShaohua Li * merge because the I/O submitted to backing file is handled page by 213140326d8aSShaohua Li * page. For directio mode, merge does help to dispatch bigger request 213240326d8aSShaohua Li * to underlayer disk. We will enable merge once directio is enabled. 21335b5e20f4SMing Lei */ 21348b904b5bSBart Van Assche blk_queue_flag_set(QUEUE_FLAG_NOMERGES, lo->lo_queue); 21355b5e20f4SMing Lei 21367a649737SMinfei Huang err = -ENOMEM; 2137476a4813SLaurent Vivier disk = lo->lo_disk = alloc_disk(1 << part_shift); 213873285082SKen Chen if (!disk) 213973285082SKen Chen goto out_free_queue; 214073285082SKen Chen 2141e03c8dd1SKay Sievers /* 2142e03c8dd1SKay Sievers * Disable partition scanning by default. The in-kernel partition 2143e03c8dd1SKay Sievers * scanning can be requested individually per-device during its 2144e03c8dd1SKay Sievers * setup. Userspace can always add and remove partitions from all 2145e03c8dd1SKay Sievers * devices. The needed partition minors are allocated from the 2146e03c8dd1SKay Sievers * extended minor space, the main loop device numbers will continue 2147e03c8dd1SKay Sievers * to match the loop minors, regardless of the number of partitions 2148e03c8dd1SKay Sievers * used. 2149e03c8dd1SKay Sievers * 2150e03c8dd1SKay Sievers * If max_part is given, partition scanning is globally enabled for 2151e03c8dd1SKay Sievers * all loop devices. The minors for the main loop devices will be 2152e03c8dd1SKay Sievers * multiples of max_part. 2153e03c8dd1SKay Sievers * 2154e03c8dd1SKay Sievers * Note: Global-for-all-devices, set-only-at-init, read-only module 2155e03c8dd1SKay Sievers * parameteters like 'max_loop' and 'max_part' make things needlessly 2156e03c8dd1SKay Sievers * complicated, are too static, inflexible and may surprise 2157e03c8dd1SKay Sievers * userspace tools. Parameters like this in general should be avoided. 2158e03c8dd1SKay Sievers */ 2159e03c8dd1SKay Sievers if (!part_shift) 2160e03c8dd1SKay Sievers disk->flags |= GENHD_FL_NO_PART_SCAN; 2161e03c8dd1SKay Sievers disk->flags |= GENHD_FL_EXT_DEVT; 2162f8933667SMing Lei atomic_set(&lo->lo_refcnt, 0); 21631da177e4SLinus Torvalds lo->lo_number = i; 21641da177e4SLinus Torvalds spin_lock_init(&lo->lo_lock); 21651da177e4SLinus Torvalds disk->major = LOOP_MAJOR; 2166476a4813SLaurent Vivier disk->first_minor = i << part_shift; 21671da177e4SLinus Torvalds disk->fops = &lo_fops; 21681da177e4SLinus Torvalds disk->private_data = lo; 21691da177e4SLinus Torvalds disk->queue = lo->lo_queue; 217073285082SKen Chen sprintf(disk->disk_name, "loop%d", i); 217134dd82afSKay Sievers add_disk(disk); 217234dd82afSKay Sievers *l = lo; 217334dd82afSKay Sievers return lo->lo_number; 217473285082SKen Chen 217573285082SKen Chen out_free_queue: 217673285082SKen Chen blk_cleanup_queue(lo->lo_queue); 2177b5dd2f60SMing Lei out_cleanup_tags: 2178b5dd2f60SMing Lei blk_mq_free_tag_set(&lo->tag_set); 21793ec981e3SMikulas Patocka out_free_idr: 21803ec981e3SMikulas Patocka idr_remove(&loop_index_idr, i); 218173285082SKen Chen out_free_dev: 218273285082SKen Chen kfree(lo); 218373285082SKen Chen out: 218434dd82afSKay Sievers return err; 21851da177e4SLinus Torvalds } 21861da177e4SLinus Torvalds 218734dd82afSKay Sievers static void loop_remove(struct loop_device *lo) 218873285082SKen Chen { 21896cd18e71SNeilBrown del_gendisk(lo->lo_disk); 21900fa8ebddSBart Van Assche blk_cleanup_queue(lo->lo_queue); 2191b5dd2f60SMing Lei blk_mq_free_tag_set(&lo->tag_set); 219273285082SKen Chen put_disk(lo->lo_disk); 219373285082SKen Chen kfree(lo); 219473285082SKen Chen } 219573285082SKen Chen 2196770fe30aSKay Sievers static int find_free_cb(int id, void *ptr, void *data) 2197770fe30aSKay Sievers { 2198770fe30aSKay Sievers struct loop_device *lo = ptr; 2199770fe30aSKay Sievers struct loop_device **l = data; 2200770fe30aSKay Sievers 2201770fe30aSKay Sievers if (lo->lo_state == Lo_unbound) { 2202770fe30aSKay Sievers *l = lo; 2203770fe30aSKay Sievers return 1; 2204770fe30aSKay Sievers } 2205770fe30aSKay Sievers return 0; 2206770fe30aSKay Sievers } 2207770fe30aSKay Sievers 220834dd82afSKay Sievers static int loop_lookup(struct loop_device **l, int i) 2209a47653fcSKen Chen { 2210a47653fcSKen Chen struct loop_device *lo; 221134dd82afSKay Sievers int ret = -ENODEV; 2212a47653fcSKen Chen 2213770fe30aSKay Sievers if (i < 0) { 2214770fe30aSKay Sievers int err; 2215770fe30aSKay Sievers 2216770fe30aSKay Sievers err = idr_for_each(&loop_index_idr, &find_free_cb, &lo); 2217770fe30aSKay Sievers if (err == 1) { 2218770fe30aSKay Sievers *l = lo; 2219770fe30aSKay Sievers ret = lo->lo_number; 2220770fe30aSKay Sievers } 2221770fe30aSKay Sievers goto out; 2222a47653fcSKen Chen } 2223a47653fcSKen Chen 2224770fe30aSKay Sievers /* lookup and return a specific i */ 222534dd82afSKay Sievers lo = idr_find(&loop_index_idr, i); 2226a47653fcSKen Chen if (lo) { 222734dd82afSKay Sievers *l = lo; 222834dd82afSKay Sievers ret = lo->lo_number; 2229a47653fcSKen Chen } 2230770fe30aSKay Sievers out: 223134dd82afSKay Sievers return ret; 2232a47653fcSKen Chen } 2233a47653fcSKen Chen 223473285082SKen Chen static struct kobject *loop_probe(dev_t dev, int *part, void *data) 223573285082SKen Chen { 2236705962ccSAl Viro struct loop_device *lo; 223707002e99SAl Viro struct kobject *kobj; 223834dd82afSKay Sievers int err; 223973285082SKen Chen 22400a42e99bSJan Kara mutex_lock(&loop_ctl_mutex); 224134dd82afSKay Sievers err = loop_lookup(&lo, MINOR(dev) >> part_shift); 224234dd82afSKay Sievers if (err < 0) 224334dd82afSKay Sievers err = loop_add(&lo, MINOR(dev) >> part_shift); 224434dd82afSKay Sievers if (err < 0) 2245a207f593SMikulas Patocka kobj = NULL; 224634dd82afSKay Sievers else 22473079c22eSJan Kara kobj = get_disk_and_module(lo->lo_disk); 22480a42e99bSJan Kara mutex_unlock(&loop_ctl_mutex); 224973285082SKen Chen 225073285082SKen Chen *part = 0; 225107002e99SAl Viro return kobj; 225273285082SKen Chen } 225373285082SKen Chen 2254770fe30aSKay Sievers static long loop_control_ioctl(struct file *file, unsigned int cmd, 2255770fe30aSKay Sievers unsigned long parm) 2256770fe30aSKay Sievers { 2257770fe30aSKay Sievers struct loop_device *lo; 22580a42e99bSJan Kara int ret; 2259770fe30aSKay Sievers 22600a42e99bSJan Kara ret = mutex_lock_killable(&loop_ctl_mutex); 22610a42e99bSJan Kara if (ret) 22620a42e99bSJan Kara return ret; 22630a42e99bSJan Kara 22640a42e99bSJan Kara ret = -ENOSYS; 2265770fe30aSKay Sievers switch (cmd) { 2266770fe30aSKay Sievers case LOOP_CTL_ADD: 2267770fe30aSKay Sievers ret = loop_lookup(&lo, parm); 2268770fe30aSKay Sievers if (ret >= 0) { 2269770fe30aSKay Sievers ret = -EEXIST; 2270770fe30aSKay Sievers break; 2271770fe30aSKay Sievers } 2272770fe30aSKay Sievers ret = loop_add(&lo, parm); 2273770fe30aSKay Sievers break; 2274770fe30aSKay Sievers case LOOP_CTL_REMOVE: 2275770fe30aSKay Sievers ret = loop_lookup(&lo, parm); 2276770fe30aSKay Sievers if (ret < 0) 2277770fe30aSKay Sievers break; 2278770fe30aSKay Sievers if (lo->lo_state != Lo_unbound) { 2279770fe30aSKay Sievers ret = -EBUSY; 2280770fe30aSKay Sievers break; 2281770fe30aSKay Sievers } 2282f8933667SMing Lei if (atomic_read(&lo->lo_refcnt) > 0) { 2283770fe30aSKay Sievers ret = -EBUSY; 2284770fe30aSKay Sievers break; 2285770fe30aSKay Sievers } 2286770fe30aSKay Sievers lo->lo_disk->private_data = NULL; 2287770fe30aSKay Sievers idr_remove(&loop_index_idr, lo->lo_number); 2288770fe30aSKay Sievers loop_remove(lo); 2289770fe30aSKay Sievers break; 2290770fe30aSKay Sievers case LOOP_CTL_GET_FREE: 2291770fe30aSKay Sievers ret = loop_lookup(&lo, -1); 2292770fe30aSKay Sievers if (ret >= 0) 2293770fe30aSKay Sievers break; 2294770fe30aSKay Sievers ret = loop_add(&lo, -1); 2295770fe30aSKay Sievers } 22960a42e99bSJan Kara mutex_unlock(&loop_ctl_mutex); 2297770fe30aSKay Sievers 2298770fe30aSKay Sievers return ret; 2299770fe30aSKay Sievers } 2300770fe30aSKay Sievers 2301770fe30aSKay Sievers static const struct file_operations loop_ctl_fops = { 2302770fe30aSKay Sievers .open = nonseekable_open, 2303770fe30aSKay Sievers .unlocked_ioctl = loop_control_ioctl, 2304770fe30aSKay Sievers .compat_ioctl = loop_control_ioctl, 2305770fe30aSKay Sievers .owner = THIS_MODULE, 2306770fe30aSKay Sievers .llseek = noop_llseek, 2307770fe30aSKay Sievers }; 2308770fe30aSKay Sievers 2309770fe30aSKay Sievers static struct miscdevice loop_misc = { 2310770fe30aSKay Sievers .minor = LOOP_CTRL_MINOR, 2311770fe30aSKay Sievers .name = "loop-control", 2312770fe30aSKay Sievers .fops = &loop_ctl_fops, 2313770fe30aSKay Sievers }; 2314770fe30aSKay Sievers 2315770fe30aSKay Sievers MODULE_ALIAS_MISCDEV(LOOP_CTRL_MINOR); 2316770fe30aSKay Sievers MODULE_ALIAS("devname:loop-control"); 2317770fe30aSKay Sievers 231873285082SKen Chen static int __init loop_init(void) 231973285082SKen Chen { 2320a47653fcSKen Chen int i, nr; 2321a47653fcSKen Chen unsigned long range; 232234dd82afSKay Sievers struct loop_device *lo; 2323770fe30aSKay Sievers int err; 2324a47653fcSKen Chen 2325476a4813SLaurent Vivier part_shift = 0; 2326ac04fee0SNamhyung Kim if (max_part > 0) { 2327476a4813SLaurent Vivier part_shift = fls(max_part); 2328476a4813SLaurent Vivier 2329ac04fee0SNamhyung Kim /* 2330ac04fee0SNamhyung Kim * Adjust max_part according to part_shift as it is exported 2331ac04fee0SNamhyung Kim * to user space so that user can decide correct minor number 2332ac04fee0SNamhyung Kim * if [s]he want to create more devices. 2333ac04fee0SNamhyung Kim * 2334ac04fee0SNamhyung Kim * Note that -1 is required because partition 0 is reserved 2335ac04fee0SNamhyung Kim * for the whole disk. 2336ac04fee0SNamhyung Kim */ 2337ac04fee0SNamhyung Kim max_part = (1UL << part_shift) - 1; 2338ac04fee0SNamhyung Kim } 2339ac04fee0SNamhyung Kim 2340b1a66504SGuo Chao if ((1UL << part_shift) > DISK_MAX_PARTS) { 2341b1a66504SGuo Chao err = -EINVAL; 2342a8c1d064SAnton Volkov goto err_out; 2343b1a66504SGuo Chao } 234478f4bb36SNamhyung Kim 2345b1a66504SGuo Chao if (max_loop > 1UL << (MINORBITS - part_shift)) { 2346b1a66504SGuo Chao err = -EINVAL; 2347a8c1d064SAnton Volkov goto err_out; 2348b1a66504SGuo Chao } 234973285082SKen Chen 2350d134b00bSKay Sievers /* 2351d134b00bSKay Sievers * If max_loop is specified, create that many devices upfront. 2352d134b00bSKay Sievers * This also becomes a hard limit. If max_loop is not specified, 2353d134b00bSKay Sievers * create CONFIG_BLK_DEV_LOOP_MIN_COUNT loop devices at module 2354d134b00bSKay Sievers * init time. Loop devices can be requested on-demand with the 2355d134b00bSKay Sievers * /dev/loop-control interface, or be instantiated by accessing 2356d134b00bSKay Sievers * a 'dead' device node. 2357d134b00bSKay Sievers */ 235873285082SKen Chen if (max_loop) { 2359a47653fcSKen Chen nr = max_loop; 2360a1c15c59SNamhyung Kim range = max_loop << part_shift; 2361a47653fcSKen Chen } else { 2362d134b00bSKay Sievers nr = CONFIG_BLK_DEV_LOOP_MIN_COUNT; 2363a1c15c59SNamhyung Kim range = 1UL << MINORBITS; 236473285082SKen Chen } 2365a47653fcSKen Chen 2366a8c1d064SAnton Volkov err = misc_register(&loop_misc); 2367a8c1d064SAnton Volkov if (err < 0) 2368a8c1d064SAnton Volkov goto err_out; 2369a8c1d064SAnton Volkov 2370a8c1d064SAnton Volkov 2371b1a66504SGuo Chao if (register_blkdev(LOOP_MAJOR, "loop")) { 2372b1a66504SGuo Chao err = -EIO; 2373b1a66504SGuo Chao goto misc_out; 2374b1a66504SGuo Chao } 2375a47653fcSKen Chen 2376a47653fcSKen Chen blk_register_region(MKDEV(LOOP_MAJOR, 0), range, 2377a47653fcSKen Chen THIS_MODULE, loop_probe, NULL, NULL); 2378a47653fcSKen Chen 2379d134b00bSKay Sievers /* pre-create number of devices given by config or max_loop */ 23800a42e99bSJan Kara mutex_lock(&loop_ctl_mutex); 238134dd82afSKay Sievers for (i = 0; i < nr; i++) 238234dd82afSKay Sievers loop_add(&lo, i); 23830a42e99bSJan Kara mutex_unlock(&loop_ctl_mutex); 238434dd82afSKay Sievers 238573285082SKen Chen printk(KERN_INFO "loop: module loaded\n"); 23861da177e4SLinus Torvalds return 0; 2387b1a66504SGuo Chao 2388b1a66504SGuo Chao misc_out: 2389b1a66504SGuo Chao misc_deregister(&loop_misc); 2390a8c1d064SAnton Volkov err_out: 2391b1a66504SGuo Chao return err; 239234dd82afSKay Sievers } 2393a47653fcSKen Chen 239434dd82afSKay Sievers static int loop_exit_cb(int id, void *ptr, void *data) 239534dd82afSKay Sievers { 239634dd82afSKay Sievers struct loop_device *lo = ptr; 2397a47653fcSKen Chen 239834dd82afSKay Sievers loop_remove(lo); 239934dd82afSKay Sievers return 0; 24001da177e4SLinus Torvalds } 24011da177e4SLinus Torvalds 240273285082SKen Chen static void __exit loop_exit(void) 24031da177e4SLinus Torvalds { 2404a47653fcSKen Chen unsigned long range; 24051da177e4SLinus Torvalds 2406a1c15c59SNamhyung Kim range = max_loop ? max_loop << part_shift : 1UL << MINORBITS; 2407a47653fcSKen Chen 2408200f9337SLuis Chamberlain mutex_lock(&loop_ctl_mutex); 2409200f9337SLuis Chamberlain 241034dd82afSKay Sievers idr_for_each(&loop_index_idr, &loop_exit_cb, NULL); 241134dd82afSKay Sievers idr_destroy(&loop_index_idr); 241273285082SKen Chen 2413a47653fcSKen Chen blk_unregister_region(MKDEV(LOOP_MAJOR, 0), range); 241400d59405SAkinobu Mita unregister_blkdev(LOOP_MAJOR, "loop"); 2415770fe30aSKay Sievers 2416770fe30aSKay Sievers misc_deregister(&loop_misc); 2417200f9337SLuis Chamberlain 2418200f9337SLuis Chamberlain mutex_unlock(&loop_ctl_mutex); 24191da177e4SLinus Torvalds } 24201da177e4SLinus Torvalds 24211da177e4SLinus Torvalds module_init(loop_init); 24221da177e4SLinus Torvalds module_exit(loop_exit); 24231da177e4SLinus Torvalds 24241da177e4SLinus Torvalds #ifndef MODULE 24251da177e4SLinus Torvalds static int __init max_loop_setup(char *str) 24261da177e4SLinus Torvalds { 24271da177e4SLinus Torvalds max_loop = simple_strtol(str, NULL, 0); 24281da177e4SLinus Torvalds return 1; 24291da177e4SLinus Torvalds } 24301da177e4SLinus Torvalds 24311da177e4SLinus Torvalds __setup("max_loop=", max_loop_setup); 24321da177e4SLinus Torvalds #endif 2433