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 { 254449f4ec9SChristoph Hellwig if (!set_capacity_and_notify(lo->lo_disk, size)) 2553b4f85d0SChristoph Hellwig kobject_uevent(&disk_to_dev(lo->lo_disk)->kobj, KOBJ_CHANGE); 2561da177e4SLinus Torvalds } 2571da177e4SLinus Torvalds 2581da177e4SLinus Torvalds static inline int 2591da177e4SLinus Torvalds lo_do_transfer(struct loop_device *lo, int cmd, 2601da177e4SLinus Torvalds struct page *rpage, unsigned roffs, 2611da177e4SLinus Torvalds struct page *lpage, unsigned loffs, 2621da177e4SLinus Torvalds int size, sector_t rblock) 2631da177e4SLinus Torvalds { 264aa4d8616SChristoph Hellwig int ret; 265aa4d8616SChristoph Hellwig 266aa4d8616SChristoph Hellwig ret = lo->transfer(lo, cmd, rpage, roffs, lpage, loffs, size, rblock); 267aa4d8616SChristoph Hellwig if (likely(!ret)) 2681da177e4SLinus Torvalds return 0; 2691da177e4SLinus Torvalds 270aa4d8616SChristoph Hellwig printk_ratelimited(KERN_ERR 271aa4d8616SChristoph Hellwig "loop: Transfer error at byte offset %llu, length %i.\n", 272aa4d8616SChristoph Hellwig (unsigned long long)rblock << 9, size); 273aa4d8616SChristoph Hellwig return ret; 2741da177e4SLinus Torvalds } 2751da177e4SLinus Torvalds 276aa4d8616SChristoph Hellwig static int lo_write_bvec(struct file *file, struct bio_vec *bvec, loff_t *ppos) 2771da177e4SLinus Torvalds { 278aa4d8616SChristoph Hellwig struct iov_iter i; 2791da177e4SLinus Torvalds ssize_t bw; 280283e7e5dSAl Viro 281b6207430SChristoph Hellwig iov_iter_bvec(&i, WRITE, bvec, 1, bvec->bv_len); 2821da177e4SLinus Torvalds 28303d95eb2SAl Viro file_start_write(file); 284abbb6589SChristoph Hellwig bw = vfs_iter_write(file, &i, ppos, 0); 28503d95eb2SAl Viro file_end_write(file); 286aa4d8616SChristoph Hellwig 287aa4d8616SChristoph Hellwig if (likely(bw == bvec->bv_len)) 2881da177e4SLinus Torvalds return 0; 289aa4d8616SChristoph Hellwig 290aa4d8616SChristoph Hellwig printk_ratelimited(KERN_ERR 291aa4d8616SChristoph Hellwig "loop: Write error at byte offset %llu, length %i.\n", 292aa4d8616SChristoph Hellwig (unsigned long long)*ppos, bvec->bv_len); 2931da177e4SLinus Torvalds if (bw >= 0) 2941da177e4SLinus Torvalds bw = -EIO; 2951da177e4SLinus Torvalds return bw; 2961da177e4SLinus Torvalds } 2971da177e4SLinus Torvalds 298aa4d8616SChristoph Hellwig static int lo_write_simple(struct loop_device *lo, struct request *rq, 299aa4d8616SChristoph Hellwig loff_t pos) 3001da177e4SLinus Torvalds { 301aa4d8616SChristoph Hellwig struct bio_vec bvec; 302aa4d8616SChristoph Hellwig struct req_iterator iter; 303aa4d8616SChristoph Hellwig int ret = 0; 304aa4d8616SChristoph Hellwig 305aa4d8616SChristoph Hellwig rq_for_each_segment(bvec, rq, iter) { 306aa4d8616SChristoph Hellwig ret = lo_write_bvec(lo->lo_backing_file, &bvec, &pos); 307aa4d8616SChristoph Hellwig if (ret < 0) 308aa4d8616SChristoph Hellwig break; 3091da177e4SLinus Torvalds cond_resched(); 3101da177e4SLinus Torvalds } 3111da177e4SLinus Torvalds 312aa4d8616SChristoph Hellwig return ret; 313aa4d8616SChristoph Hellwig } 314aa4d8616SChristoph Hellwig 315aa4d8616SChristoph Hellwig /* 316456be148SChristoph Hellwig * This is the slow, transforming version that needs to double buffer the 317456be148SChristoph Hellwig * data as it cannot do the transformations in place without having direct 318456be148SChristoph Hellwig * access to the destination pages of the backing file. 3191da177e4SLinus Torvalds */ 320aa4d8616SChristoph Hellwig static int lo_write_transfer(struct loop_device *lo, struct request *rq, 321aa4d8616SChristoph Hellwig loff_t pos) 3221da177e4SLinus Torvalds { 323aa4d8616SChristoph Hellwig struct bio_vec bvec, b; 32430112013SMing Lei struct req_iterator iter; 325aa4d8616SChristoph Hellwig struct page *page; 3267988613bSKent Overstreet int ret = 0; 3271da177e4SLinus Torvalds 328aa4d8616SChristoph Hellwig page = alloc_page(GFP_NOIO); 3291da177e4SLinus Torvalds if (unlikely(!page)) 330aa4d8616SChristoph Hellwig return -ENOMEM; 331456be148SChristoph Hellwig 33230112013SMing Lei rq_for_each_segment(bvec, rq, iter) { 333aa4d8616SChristoph Hellwig ret = lo_do_transfer(lo, WRITE, page, 0, bvec.bv_page, 334aa4d8616SChristoph Hellwig bvec.bv_offset, bvec.bv_len, pos >> 9); 335aa4d8616SChristoph Hellwig if (unlikely(ret)) 336aa4d8616SChristoph Hellwig break; 337aa4d8616SChristoph Hellwig 338aa4d8616SChristoph Hellwig b.bv_page = page; 339aa4d8616SChristoph Hellwig b.bv_offset = 0; 340aa4d8616SChristoph Hellwig b.bv_len = bvec.bv_len; 341aa4d8616SChristoph Hellwig ret = lo_write_bvec(lo->lo_backing_file, &b, &pos); 3421da177e4SLinus Torvalds if (ret < 0) 3431da177e4SLinus Torvalds break; 3441da177e4SLinus Torvalds } 345aa4d8616SChristoph Hellwig 3461da177e4SLinus Torvalds __free_page(page); 3471da177e4SLinus Torvalds return ret; 3481da177e4SLinus Torvalds } 3491da177e4SLinus Torvalds 350aa4d8616SChristoph Hellwig static int lo_read_simple(struct loop_device *lo, struct request *rq, 351aa4d8616SChristoph Hellwig loff_t pos) 3521da177e4SLinus Torvalds { 3537988613bSKent Overstreet struct bio_vec bvec; 35430112013SMing Lei struct req_iterator iter; 355aa4d8616SChristoph Hellwig struct iov_iter i; 356aa4d8616SChristoph Hellwig ssize_t len; 3571da177e4SLinus Torvalds 35830112013SMing Lei rq_for_each_segment(bvec, rq, iter) { 359b6207430SChristoph Hellwig iov_iter_bvec(&i, READ, &bvec, 1, bvec.bv_len); 36018e9710eSChristoph Hellwig len = vfs_iter_read(lo->lo_backing_file, &i, &pos, 0); 361aa4d8616SChristoph Hellwig if (len < 0) 362aa4d8616SChristoph Hellwig return len; 363306df071SDave Young 364aa4d8616SChristoph Hellwig flush_dcache_page(bvec.bv_page); 365aa4d8616SChristoph Hellwig 366aa4d8616SChristoph Hellwig if (len != bvec.bv_len) { 36730112013SMing Lei struct bio *bio; 36830112013SMing Lei 36930112013SMing Lei __rq_for_each_bio(bio, rq) 370306df071SDave Young zero_fill_bio(bio); 3711da177e4SLinus Torvalds break; 372306df071SDave Young } 373aa4d8616SChristoph Hellwig cond_resched(); 3741da177e4SLinus Torvalds } 375aa4d8616SChristoph Hellwig 376306df071SDave Young return 0; 3771da177e4SLinus Torvalds } 3781da177e4SLinus Torvalds 379aa4d8616SChristoph Hellwig static int lo_read_transfer(struct loop_device *lo, struct request *rq, 380aa4d8616SChristoph Hellwig loff_t pos) 381aa4d8616SChristoph Hellwig { 382aa4d8616SChristoph Hellwig struct bio_vec bvec, b; 383aa4d8616SChristoph Hellwig struct req_iterator iter; 384aa4d8616SChristoph Hellwig struct iov_iter i; 385aa4d8616SChristoph Hellwig struct page *page; 386aa4d8616SChristoph Hellwig ssize_t len; 387aa4d8616SChristoph Hellwig int ret = 0; 388aa4d8616SChristoph Hellwig 389aa4d8616SChristoph Hellwig page = alloc_page(GFP_NOIO); 390aa4d8616SChristoph Hellwig if (unlikely(!page)) 391aa4d8616SChristoph Hellwig return -ENOMEM; 392aa4d8616SChristoph Hellwig 393aa4d8616SChristoph Hellwig rq_for_each_segment(bvec, rq, iter) { 394aa4d8616SChristoph Hellwig loff_t offset = pos; 395aa4d8616SChristoph Hellwig 396aa4d8616SChristoph Hellwig b.bv_page = page; 397aa4d8616SChristoph Hellwig b.bv_offset = 0; 398aa4d8616SChristoph Hellwig b.bv_len = bvec.bv_len; 399aa4d8616SChristoph Hellwig 400b6207430SChristoph Hellwig iov_iter_bvec(&i, READ, &b, 1, b.bv_len); 40118e9710eSChristoph Hellwig len = vfs_iter_read(lo->lo_backing_file, &i, &pos, 0); 402aa4d8616SChristoph Hellwig if (len < 0) { 403aa4d8616SChristoph Hellwig ret = len; 404aa4d8616SChristoph Hellwig goto out_free_page; 405aa4d8616SChristoph Hellwig } 406aa4d8616SChristoph Hellwig 407aa4d8616SChristoph Hellwig ret = lo_do_transfer(lo, READ, page, 0, bvec.bv_page, 408aa4d8616SChristoph Hellwig bvec.bv_offset, len, offset >> 9); 409aa4d8616SChristoph Hellwig if (ret) 410aa4d8616SChristoph Hellwig goto out_free_page; 411aa4d8616SChristoph Hellwig 412aa4d8616SChristoph Hellwig flush_dcache_page(bvec.bv_page); 413aa4d8616SChristoph Hellwig 414aa4d8616SChristoph Hellwig if (len != bvec.bv_len) { 415aa4d8616SChristoph Hellwig struct bio *bio; 416aa4d8616SChristoph Hellwig 417aa4d8616SChristoph Hellwig __rq_for_each_bio(bio, rq) 418aa4d8616SChristoph Hellwig zero_fill_bio(bio); 419aa4d8616SChristoph Hellwig break; 420aa4d8616SChristoph Hellwig } 421aa4d8616SChristoph Hellwig } 422aa4d8616SChristoph Hellwig 423aa4d8616SChristoph Hellwig ret = 0; 424aa4d8616SChristoph Hellwig out_free_page: 425aa4d8616SChristoph Hellwig __free_page(page); 426aa4d8616SChristoph Hellwig return ret; 427aa4d8616SChristoph Hellwig } 428aa4d8616SChristoph Hellwig 429efcfec57SDarrick J. Wong static int lo_fallocate(struct loop_device *lo, struct request *rq, loff_t pos, 430efcfec57SDarrick J. Wong int mode) 431cf655d95SMing Lei { 432cf655d95SMing Lei /* 433efcfec57SDarrick J. Wong * We use fallocate to manipulate the space mappings used by the image 434efcfec57SDarrick J. Wong * a.k.a. discard/zerorange. However we do not support this if 435efcfec57SDarrick J. Wong * encryption is enabled, because it may give an attacker useful 436efcfec57SDarrick J. Wong * information. 437cf655d95SMing Lei */ 438cf655d95SMing Lei struct file *file = lo->lo_backing_file; 439c52abf56SEvan Green struct request_queue *q = lo->lo_queue; 440cf655d95SMing Lei int ret; 441cf655d95SMing Lei 442efcfec57SDarrick J. Wong mode |= FALLOC_FL_KEEP_SIZE; 443efcfec57SDarrick J. Wong 444c52abf56SEvan Green if (!blk_queue_discard(q)) { 445cf655d95SMing Lei ret = -EOPNOTSUPP; 446cf655d95SMing Lei goto out; 447cf655d95SMing Lei } 448cf655d95SMing Lei 449cf655d95SMing Lei ret = file->f_op->fallocate(file, mode, pos, blk_rq_bytes(rq)); 450cf655d95SMing Lei if (unlikely(ret && ret != -EINVAL && ret != -EOPNOTSUPP)) 451cf655d95SMing Lei ret = -EIO; 452cf655d95SMing Lei out: 453cf655d95SMing Lei return ret; 454cf655d95SMing Lei } 455cf655d95SMing Lei 456cf655d95SMing Lei static int lo_req_flush(struct loop_device *lo, struct request *rq) 457cf655d95SMing Lei { 458cf655d95SMing Lei struct file *file = lo->lo_backing_file; 459cf655d95SMing Lei int ret = vfs_fsync(file, 0); 460cf655d95SMing Lei if (unlikely(ret && ret != -EINVAL)) 461cf655d95SMing Lei ret = -EIO; 462cf655d95SMing Lei 463cf655d95SMing Lei return ret; 464cf655d95SMing Lei } 465cf655d95SMing Lei 466fe2cb290SChristoph Hellwig static void lo_complete_rq(struct request *rq) 467bc07c10aSMing Lei { 468fe2cb290SChristoph Hellwig struct loop_cmd *cmd = blk_mq_rq_to_pdu(rq); 469f9de14bcSJens Axboe blk_status_t ret = BLK_STS_OK; 470bc07c10aSMing Lei 471f9de14bcSJens Axboe if (!cmd->use_aio || cmd->ret < 0 || cmd->ret == blk_rq_bytes(rq) || 472f9de14bcSJens Axboe req_op(rq) != REQ_OP_READ) { 473f9de14bcSJens Axboe if (cmd->ret < 0) 4748cd55087SEvan Green ret = errno_to_blk_status(cmd->ret); 475f9de14bcSJens Axboe goto end_io; 476bc07c10aSMing Lei } 477fe2cb290SChristoph Hellwig 478f9de14bcSJens Axboe /* 479f9de14bcSJens Axboe * Short READ - if we got some data, advance our request and 480f9de14bcSJens Axboe * retry it. If we got no data, end the rest with EIO. 481f9de14bcSJens Axboe */ 482f9de14bcSJens Axboe if (cmd->ret) { 483f9de14bcSJens Axboe blk_update_request(rq, BLK_STS_OK, cmd->ret); 484f9de14bcSJens Axboe cmd->ret = 0; 485f9de14bcSJens Axboe blk_mq_requeue_request(rq, true); 486f9de14bcSJens Axboe } else { 487f9de14bcSJens Axboe if (cmd->use_aio) { 488f9de14bcSJens Axboe struct bio *bio = rq->bio; 489f9de14bcSJens Axboe 490f9de14bcSJens Axboe while (bio) { 491f9de14bcSJens Axboe zero_fill_bio(bio); 492f9de14bcSJens Axboe bio = bio->bi_next; 493f9de14bcSJens Axboe } 494f9de14bcSJens Axboe } 495f9de14bcSJens Axboe ret = BLK_STS_IOERR; 496f9de14bcSJens Axboe end_io: 497f9de14bcSJens Axboe blk_mq_end_request(rq, ret); 498f9de14bcSJens Axboe } 499bc07c10aSMing Lei } 500bc07c10aSMing Lei 50192d77332SShaohua Li static void lo_rw_aio_do_completion(struct loop_cmd *cmd) 50292d77332SShaohua Li { 5031894e916SJens Axboe struct request *rq = blk_mq_rq_from_pdu(cmd); 5041894e916SJens Axboe 50592d77332SShaohua Li if (!atomic_dec_and_test(&cmd->ref)) 50692d77332SShaohua Li return; 50792d77332SShaohua Li kfree(cmd->bvec); 50892d77332SShaohua Li cmd->bvec = NULL; 50915f73f5bSChristoph Hellwig if (likely(!blk_should_fake_timeout(rq->q))) 5101894e916SJens Axboe blk_mq_complete_request(rq); 51192d77332SShaohua Li } 51292d77332SShaohua Li 513bc07c10aSMing Lei static void lo_rw_aio_complete(struct kiocb *iocb, long ret, long ret2) 514bc07c10aSMing Lei { 515bc07c10aSMing Lei struct loop_cmd *cmd = container_of(iocb, struct loop_cmd, iocb); 516bc07c10aSMing Lei 517d4478e92SShaohua Li if (cmd->css) 518d4478e92SShaohua Li css_put(cmd->css); 519fe2cb290SChristoph Hellwig cmd->ret = ret; 52092d77332SShaohua Li lo_rw_aio_do_completion(cmd); 521bc07c10aSMing Lei } 522bc07c10aSMing Lei 523bc07c10aSMing Lei static int lo_rw_aio(struct loop_device *lo, struct loop_cmd *cmd, 524bc07c10aSMing Lei loff_t pos, bool rw) 525bc07c10aSMing Lei { 526bc07c10aSMing Lei struct iov_iter iter; 52786af5952SMing Lei struct req_iterator rq_iter; 528bc07c10aSMing Lei struct bio_vec *bvec; 5291894e916SJens Axboe struct request *rq = blk_mq_rq_from_pdu(cmd); 53040326d8aSShaohua Li struct bio *bio = rq->bio; 531bc07c10aSMing Lei struct file *file = lo->lo_backing_file; 53286af5952SMing Lei struct bio_vec tmp; 53340326d8aSShaohua Li unsigned int offset; 53486af5952SMing Lei int nr_bvec = 0; 535bc07c10aSMing Lei int ret; 536bc07c10aSMing Lei 53786af5952SMing Lei rq_for_each_bvec(tmp, rq, rq_iter) 53886af5952SMing Lei nr_bvec++; 539bc07c10aSMing Lei 54086af5952SMing Lei if (rq->bio != rq->biotail) { 54186af5952SMing Lei 54286af5952SMing Lei bvec = kmalloc_array(nr_bvec, sizeof(struct bio_vec), 5436da2ec56SKees Cook GFP_NOIO); 54440326d8aSShaohua Li if (!bvec) 54540326d8aSShaohua Li return -EIO; 54640326d8aSShaohua Li cmd->bvec = bvec; 54740326d8aSShaohua Li 548a7297a6aSMing Lei /* 54940326d8aSShaohua Li * The bios of the request may be started from the middle of 55040326d8aSShaohua Li * the 'bvec' because of bio splitting, so we can't directly 55186af5952SMing Lei * copy bio->bi_iov_vec to new bvec. The rq_for_each_bvec 55240326d8aSShaohua Li * API will take care of all details for us. 553a7297a6aSMing Lei */ 55486af5952SMing Lei rq_for_each_bvec(tmp, rq, rq_iter) { 55540326d8aSShaohua Li *bvec = tmp; 55640326d8aSShaohua Li bvec++; 55740326d8aSShaohua Li } 55840326d8aSShaohua Li bvec = cmd->bvec; 55940326d8aSShaohua Li offset = 0; 56040326d8aSShaohua Li } else { 56140326d8aSShaohua Li /* 56240326d8aSShaohua Li * Same here, this bio may be started from the middle of the 56340326d8aSShaohua Li * 'bvec' because of bio splitting, so offset from the bvec 56440326d8aSShaohua Li * must be passed to iov iterator 56540326d8aSShaohua Li */ 56640326d8aSShaohua Li offset = bio->bi_iter.bi_bvec_done; 56740326d8aSShaohua Li bvec = __bvec_iter_bvec(bio->bi_io_vec, bio->bi_iter); 56840326d8aSShaohua Li } 56992d77332SShaohua Li atomic_set(&cmd->ref, 2); 57040326d8aSShaohua Li 571b6207430SChristoph Hellwig iov_iter_bvec(&iter, rw, bvec, nr_bvec, blk_rq_bytes(rq)); 57240326d8aSShaohua Li iter.iov_offset = offset; 573bc07c10aSMing Lei 574bc07c10aSMing Lei cmd->iocb.ki_pos = pos; 575bc07c10aSMing Lei cmd->iocb.ki_filp = file; 576bc07c10aSMing Lei cmd->iocb.ki_complete = lo_rw_aio_complete; 577bc07c10aSMing Lei cmd->iocb.ki_flags = IOCB_DIRECT; 578d9a08a9eSAdam Manzanares cmd->iocb.ki_ioprio = IOPRIO_PRIO_VALUE(IOPRIO_CLASS_NONE, 0); 579d4478e92SShaohua Li if (cmd->css) 580d4478e92SShaohua Li kthread_associate_blkcg(cmd->css); 581bc07c10aSMing Lei 582bc07c10aSMing Lei if (rw == WRITE) 583bb7462b6SMiklos Szeredi ret = call_write_iter(file, &cmd->iocb, &iter); 584bc07c10aSMing Lei else 585bb7462b6SMiklos Szeredi ret = call_read_iter(file, &cmd->iocb, &iter); 586bc07c10aSMing Lei 58792d77332SShaohua Li lo_rw_aio_do_completion(cmd); 588d4478e92SShaohua Li kthread_associate_blkcg(NULL); 58992d77332SShaohua Li 590bc07c10aSMing Lei if (ret != -EIOCBQUEUED) 591bc07c10aSMing Lei cmd->iocb.ki_complete(&cmd->iocb, ret, 0); 592bc07c10aSMing Lei return 0; 593bc07c10aSMing Lei } 594bc07c10aSMing Lei 595c1c87c2bSChristoph Hellwig static int do_req_filebacked(struct loop_device *lo, struct request *rq) 596bc07c10aSMing Lei { 597bc07c10aSMing Lei struct loop_cmd *cmd = blk_mq_rq_to_pdu(rq); 598c1c87c2bSChristoph Hellwig loff_t pos = ((loff_t) blk_rq_pos(rq) << 9) + lo->lo_offset; 599bc07c10aSMing Lei 600bc07c10aSMing Lei /* 601bc07c10aSMing Lei * lo_write_simple and lo_read_simple should have been covered 602bc07c10aSMing Lei * by io submit style function like lo_rw_aio(), one blocker 603bc07c10aSMing Lei * is that lo_read_simple() need to call flush_dcache_page after 604bc07c10aSMing Lei * the page is written from kernel, and it isn't easy to handle 605bc07c10aSMing Lei * this in io submit style function which submits all segments 606bc07c10aSMing Lei * of the req at one time. And direct read IO doesn't need to 607bc07c10aSMing Lei * run flush_dcache_page(). 608bc07c10aSMing Lei */ 609c1c87c2bSChristoph Hellwig switch (req_op(rq)) { 610c1c87c2bSChristoph Hellwig case REQ_OP_FLUSH: 611c1c87c2bSChristoph Hellwig return lo_req_flush(lo, rq); 61219372e27SChristoph Hellwig case REQ_OP_WRITE_ZEROES: 613efcfec57SDarrick J. Wong /* 614efcfec57SDarrick J. Wong * If the caller doesn't want deallocation, call zeroout to 615efcfec57SDarrick J. Wong * write zeroes the range. Otherwise, punch them out. 616efcfec57SDarrick J. Wong */ 617efcfec57SDarrick J. Wong return lo_fallocate(lo, rq, pos, 618efcfec57SDarrick J. Wong (rq->cmd_flags & REQ_NOUNMAP) ? 619efcfec57SDarrick J. Wong FALLOC_FL_ZERO_RANGE : 620efcfec57SDarrick J. Wong FALLOC_FL_PUNCH_HOLE); 621efcfec57SDarrick J. Wong case REQ_OP_DISCARD: 622efcfec57SDarrick J. Wong return lo_fallocate(lo, rq, pos, FALLOC_FL_PUNCH_HOLE); 623c1c87c2bSChristoph Hellwig case REQ_OP_WRITE: 624c1c87c2bSChristoph Hellwig if (lo->transfer) 625c1c87c2bSChristoph Hellwig return lo_write_transfer(lo, rq, pos); 626c1c87c2bSChristoph Hellwig else if (cmd->use_aio) 627c1c87c2bSChristoph Hellwig return lo_rw_aio(lo, cmd, pos, WRITE); 628c1c87c2bSChristoph Hellwig else 629bc07c10aSMing Lei return lo_write_simple(lo, rq, pos); 630c1c87c2bSChristoph Hellwig case REQ_OP_READ: 631c1c87c2bSChristoph Hellwig if (lo->transfer) 632c1c87c2bSChristoph Hellwig return lo_read_transfer(lo, rq, pos); 633c1c87c2bSChristoph Hellwig else if (cmd->use_aio) 634c1c87c2bSChristoph Hellwig return lo_rw_aio(lo, cmd, pos, READ); 635bc07c10aSMing Lei else 636bc07c10aSMing Lei return lo_read_simple(lo, rq, pos); 637c1c87c2bSChristoph Hellwig default: 638c1c87c2bSChristoph Hellwig WARN_ON_ONCE(1); 639c1c87c2bSChristoph Hellwig return -EIO; 640bc07c10aSMing Lei } 6411da177e4SLinus Torvalds } 6421da177e4SLinus Torvalds 6432e5ab5f3SMing Lei static inline void loop_update_dio(struct loop_device *lo) 6442e5ab5f3SMing Lei { 645efbe3c24SIra Weiny __loop_update_dio(lo, (lo->lo_backing_file->f_flags & O_DIRECT) | 6462e5ab5f3SMing Lei lo->use_dio); 6472e5ab5f3SMing Lei } 6482e5ab5f3SMing Lei 64906f0e9e6SMing Lei static void loop_reread_partitions(struct loop_device *lo, 65006f0e9e6SMing Lei struct block_device *bdev) 65106f0e9e6SMing Lei { 65206f0e9e6SMing Lei int rc; 65306f0e9e6SMing Lei 654f0b870dfSChristoph Hellwig mutex_lock(&bdev->bd_mutex); 655f0b870dfSChristoph Hellwig rc = bdev_disk_changed(bdev, false); 656f0b870dfSChristoph Hellwig mutex_unlock(&bdev->bd_mutex); 65706f0e9e6SMing Lei if (rc) 65806f0e9e6SMing Lei pr_warn("%s: partition scan of loop%d (%s) failed (rc=%d)\n", 65906f0e9e6SMing Lei __func__, lo->lo_number, lo->lo_file_name, rc); 66006f0e9e6SMing Lei } 66106f0e9e6SMing Lei 662d2ac838eSTheodore Ts'o static inline int is_loop_device(struct file *file) 663d2ac838eSTheodore Ts'o { 664d2ac838eSTheodore Ts'o struct inode *i = file->f_mapping->host; 665d2ac838eSTheodore Ts'o 666d2ac838eSTheodore Ts'o return i && S_ISBLK(i->i_mode) && MAJOR(i->i_rdev) == LOOP_MAJOR; 667d2ac838eSTheodore Ts'o } 668d2ac838eSTheodore Ts'o 669d2ac838eSTheodore Ts'o static int loop_validate_file(struct file *file, struct block_device *bdev) 670d2ac838eSTheodore Ts'o { 671d2ac838eSTheodore Ts'o struct inode *inode = file->f_mapping->host; 672d2ac838eSTheodore Ts'o struct file *f = file; 673d2ac838eSTheodore Ts'o 674d2ac838eSTheodore Ts'o /* Avoid recursion */ 675d2ac838eSTheodore Ts'o while (is_loop_device(f)) { 676d2ac838eSTheodore Ts'o struct loop_device *l; 677d2ac838eSTheodore Ts'o 6784e7b5671SChristoph Hellwig if (f->f_mapping->host->i_rdev == bdev->bd_dev) 679d2ac838eSTheodore Ts'o return -EBADF; 680d2ac838eSTheodore Ts'o 6814e7b5671SChristoph Hellwig l = I_BDEV(f->f_mapping->host)->bd_disk->private_data; 682f7c8a412SDongli Zhang if (l->lo_state != Lo_bound) { 683d2ac838eSTheodore Ts'o return -EINVAL; 684d2ac838eSTheodore Ts'o } 685d2ac838eSTheodore Ts'o f = l->lo_backing_file; 686d2ac838eSTheodore Ts'o } 687d2ac838eSTheodore Ts'o if (!S_ISREG(inode->i_mode) && !S_ISBLK(inode->i_mode)) 688d2ac838eSTheodore Ts'o return -EINVAL; 689d2ac838eSTheodore Ts'o return 0; 690d2ac838eSTheodore Ts'o } 691d2ac838eSTheodore Ts'o 6921da177e4SLinus Torvalds /* 6931da177e4SLinus Torvalds * loop_change_fd switched the backing store of a loopback device to 6941da177e4SLinus Torvalds * a new file. This is useful for operating system installers to free up 6951da177e4SLinus Torvalds * the original file and in High Availability environments to switch to 6961da177e4SLinus Torvalds * an alternative location for the content in case of server meltdown. 6971da177e4SLinus Torvalds * This can only work if the loop device is used read-only, and if the 6981da177e4SLinus Torvalds * new backing store is the same size and type as the old backing store. 6991da177e4SLinus Torvalds */ 700bb214884SAl Viro static int loop_change_fd(struct loop_device *lo, struct block_device *bdev, 701bb214884SAl Viro unsigned int arg) 7021da177e4SLinus Torvalds { 7031dded9acSJan Kara struct file *file = NULL, *old_file; 7041da177e4SLinus Torvalds int error; 70585b0a54aSJan Kara bool partscan; 7061da177e4SLinus Torvalds 7076cc8e743SPavel Tatashin error = mutex_lock_killable(&lo->lo_mutex); 708c3710770SJan Kara if (error) 709c3710770SJan Kara return error; 7101da177e4SLinus Torvalds error = -ENXIO; 7111da177e4SLinus Torvalds if (lo->lo_state != Lo_bound) 7121dded9acSJan Kara goto out_err; 7131da177e4SLinus Torvalds 7141da177e4SLinus Torvalds /* the loop device has to be read-only */ 7151da177e4SLinus Torvalds error = -EINVAL; 7161da177e4SLinus Torvalds if (!(lo->lo_flags & LO_FLAGS_READ_ONLY)) 7171dded9acSJan Kara goto out_err; 7181da177e4SLinus Torvalds 7191da177e4SLinus Torvalds error = -EBADF; 7201da177e4SLinus Torvalds file = fget(arg); 7211da177e4SLinus Torvalds if (!file) 7221dded9acSJan Kara goto out_err; 7231da177e4SLinus Torvalds 724d2ac838eSTheodore Ts'o error = loop_validate_file(file, bdev); 725d2ac838eSTheodore Ts'o if (error) 7261dded9acSJan Kara goto out_err; 727d2ac838eSTheodore Ts'o 7281da177e4SLinus Torvalds old_file = lo->lo_backing_file; 7291da177e4SLinus Torvalds 7301da177e4SLinus Torvalds error = -EINVAL; 7311da177e4SLinus Torvalds 7321da177e4SLinus Torvalds /* size of the new backing store needs to be the same */ 7331da177e4SLinus Torvalds if (get_loop_size(lo, file) != get_loop_size(lo, old_file)) 7341dded9acSJan Kara goto out_err; 7351da177e4SLinus Torvalds 7361da177e4SLinus Torvalds /* and ... switch */ 73743cade80SOmar Sandoval blk_mq_freeze_queue(lo->lo_queue); 73843cade80SOmar Sandoval mapping_set_gfp_mask(old_file->f_mapping, lo->old_gfp_mask); 73943cade80SOmar Sandoval lo->lo_backing_file = file; 74043cade80SOmar Sandoval lo->old_gfp_mask = mapping_gfp_mask(file->f_mapping); 74143cade80SOmar Sandoval mapping_set_gfp_mask(file->f_mapping, 74243cade80SOmar Sandoval lo->old_gfp_mask & ~(__GFP_IO|__GFP_FS)); 74343cade80SOmar Sandoval loop_update_dio(lo); 74443cade80SOmar Sandoval blk_mq_unfreeze_queue(lo->lo_queue); 74585b0a54aSJan Kara partscan = lo->lo_flags & LO_FLAGS_PARTSCAN; 7466cc8e743SPavel Tatashin mutex_unlock(&lo->lo_mutex); 7471dded9acSJan Kara /* 7486cc8e743SPavel Tatashin * We must drop file reference outside of lo_mutex as dropping 7491dded9acSJan Kara * the file ref can take bd_mutex which creates circular locking 7501dded9acSJan Kara * dependency. 7511dded9acSJan Kara */ 7521dded9acSJan Kara fput(old_file); 75385b0a54aSJan Kara if (partscan) 75485b0a54aSJan Kara loop_reread_partitions(lo, bdev); 7551da177e4SLinus Torvalds return 0; 7561da177e4SLinus Torvalds 7571dded9acSJan Kara out_err: 7586cc8e743SPavel Tatashin mutex_unlock(&lo->lo_mutex); 7591dded9acSJan Kara if (file) 7601dded9acSJan Kara fput(file); 7611da177e4SLinus Torvalds return error; 7621da177e4SLinus Torvalds } 7631da177e4SLinus Torvalds 764ee862730SMilan Broz /* loop sysfs attributes */ 765ee862730SMilan Broz 766ee862730SMilan Broz static ssize_t loop_attr_show(struct device *dev, char *page, 767ee862730SMilan Broz ssize_t (*callback)(struct loop_device *, char *)) 768ee862730SMilan Broz { 76934dd82afSKay Sievers struct gendisk *disk = dev_to_disk(dev); 77034dd82afSKay Sievers struct loop_device *lo = disk->private_data; 771ee862730SMilan Broz 77234dd82afSKay Sievers return callback(lo, page); 773ee862730SMilan Broz } 774ee862730SMilan Broz 775ee862730SMilan Broz #define LOOP_ATTR_RO(_name) \ 776ee862730SMilan Broz static ssize_t loop_attr_##_name##_show(struct loop_device *, char *); \ 777ee862730SMilan Broz static ssize_t loop_attr_do_show_##_name(struct device *d, \ 778ee862730SMilan Broz struct device_attribute *attr, char *b) \ 779ee862730SMilan Broz { \ 780ee862730SMilan Broz return loop_attr_show(d, b, loop_attr_##_name##_show); \ 781ee862730SMilan Broz } \ 782ee862730SMilan Broz static struct device_attribute loop_attr_##_name = \ 7835657a819SJoe Perches __ATTR(_name, 0444, loop_attr_do_show_##_name, NULL); 784ee862730SMilan Broz 785ee862730SMilan Broz static ssize_t loop_attr_backing_file_show(struct loop_device *lo, char *buf) 786ee862730SMilan Broz { 787ee862730SMilan Broz ssize_t ret; 788ee862730SMilan Broz char *p = NULL; 789ee862730SMilan Broz 79005eb0f25SKay Sievers spin_lock_irq(&lo->lo_lock); 791ee862730SMilan Broz if (lo->lo_backing_file) 7929bf39ab2SMiklos Szeredi p = file_path(lo->lo_backing_file, buf, PAGE_SIZE - 1); 79305eb0f25SKay Sievers spin_unlock_irq(&lo->lo_lock); 794ee862730SMilan Broz 795ee862730SMilan Broz if (IS_ERR_OR_NULL(p)) 796ee862730SMilan Broz ret = PTR_ERR(p); 797ee862730SMilan Broz else { 798ee862730SMilan Broz ret = strlen(p); 799ee862730SMilan Broz memmove(buf, p, ret); 800ee862730SMilan Broz buf[ret++] = '\n'; 801ee862730SMilan Broz buf[ret] = 0; 802ee862730SMilan Broz } 803ee862730SMilan Broz 804ee862730SMilan Broz return ret; 805ee862730SMilan Broz } 806ee862730SMilan Broz 807ee862730SMilan Broz static ssize_t loop_attr_offset_show(struct loop_device *lo, char *buf) 808ee862730SMilan Broz { 809ee862730SMilan Broz return sprintf(buf, "%llu\n", (unsigned long long)lo->lo_offset); 810ee862730SMilan Broz } 811ee862730SMilan Broz 812ee862730SMilan Broz static ssize_t loop_attr_sizelimit_show(struct loop_device *lo, char *buf) 813ee862730SMilan Broz { 814ee862730SMilan Broz return sprintf(buf, "%llu\n", (unsigned long long)lo->lo_sizelimit); 815ee862730SMilan Broz } 816ee862730SMilan Broz 817ee862730SMilan Broz static ssize_t loop_attr_autoclear_show(struct loop_device *lo, char *buf) 818ee862730SMilan Broz { 819ee862730SMilan Broz int autoclear = (lo->lo_flags & LO_FLAGS_AUTOCLEAR); 820ee862730SMilan Broz 821ee862730SMilan Broz return sprintf(buf, "%s\n", autoclear ? "1" : "0"); 822ee862730SMilan Broz } 823ee862730SMilan Broz 824e03c8dd1SKay Sievers static ssize_t loop_attr_partscan_show(struct loop_device *lo, char *buf) 825e03c8dd1SKay Sievers { 826e03c8dd1SKay Sievers int partscan = (lo->lo_flags & LO_FLAGS_PARTSCAN); 827e03c8dd1SKay Sievers 828e03c8dd1SKay Sievers return sprintf(buf, "%s\n", partscan ? "1" : "0"); 829e03c8dd1SKay Sievers } 830e03c8dd1SKay Sievers 8312e5ab5f3SMing Lei static ssize_t loop_attr_dio_show(struct loop_device *lo, char *buf) 8322e5ab5f3SMing Lei { 8332e5ab5f3SMing Lei int dio = (lo->lo_flags & LO_FLAGS_DIRECT_IO); 8342e5ab5f3SMing Lei 8352e5ab5f3SMing Lei return sprintf(buf, "%s\n", dio ? "1" : "0"); 8362e5ab5f3SMing Lei } 8372e5ab5f3SMing Lei 838ee862730SMilan Broz LOOP_ATTR_RO(backing_file); 839ee862730SMilan Broz LOOP_ATTR_RO(offset); 840ee862730SMilan Broz LOOP_ATTR_RO(sizelimit); 841ee862730SMilan Broz LOOP_ATTR_RO(autoclear); 842e03c8dd1SKay Sievers LOOP_ATTR_RO(partscan); 8432e5ab5f3SMing Lei LOOP_ATTR_RO(dio); 844ee862730SMilan Broz 845ee862730SMilan Broz static struct attribute *loop_attrs[] = { 846ee862730SMilan Broz &loop_attr_backing_file.attr, 847ee862730SMilan Broz &loop_attr_offset.attr, 848ee862730SMilan Broz &loop_attr_sizelimit.attr, 849ee862730SMilan Broz &loop_attr_autoclear.attr, 850e03c8dd1SKay Sievers &loop_attr_partscan.attr, 8512e5ab5f3SMing Lei &loop_attr_dio.attr, 852ee862730SMilan Broz NULL, 853ee862730SMilan Broz }; 854ee862730SMilan Broz 855ee862730SMilan Broz static struct attribute_group loop_attribute_group = { 856ee862730SMilan Broz .name = "loop", 857ee862730SMilan Broz .attrs= loop_attrs, 858ee862730SMilan Broz }; 859ee862730SMilan Broz 860d3349b6bSTetsuo Handa static void loop_sysfs_init(struct loop_device *lo) 861ee862730SMilan Broz { 862d3349b6bSTetsuo Handa lo->sysfs_inited = !sysfs_create_group(&disk_to_dev(lo->lo_disk)->kobj, 863ee862730SMilan Broz &loop_attribute_group); 864ee862730SMilan Broz } 865ee862730SMilan Broz 866ee862730SMilan Broz static void loop_sysfs_exit(struct loop_device *lo) 867ee862730SMilan Broz { 868d3349b6bSTetsuo Handa if (lo->sysfs_inited) 869ee862730SMilan Broz sysfs_remove_group(&disk_to_dev(lo->lo_disk)->kobj, 870ee862730SMilan Broz &loop_attribute_group); 871ee862730SMilan Broz } 872ee862730SMilan Broz 873dfaa2ef6SLukas Czerner static void loop_config_discard(struct loop_device *lo) 874dfaa2ef6SLukas Czerner { 875dfaa2ef6SLukas Czerner struct file *file = lo->lo_backing_file; 876dfaa2ef6SLukas Czerner struct inode *inode = file->f_mapping->host; 877dfaa2ef6SLukas Czerner struct request_queue *q = lo->lo_queue; 878bcb21c8cSMing Lei u32 granularity, max_discard_sectors; 879dfaa2ef6SLukas Czerner 880dfaa2ef6SLukas Czerner /* 881c52abf56SEvan Green * If the backing device is a block device, mirror its zeroing 882c52abf56SEvan Green * capability. Set the discard sectors to the block device's zeroing 883c52abf56SEvan Green * capabilities because loop discards result in blkdev_issue_zeroout(), 884c52abf56SEvan Green * not blkdev_issue_discard(). This maintains consistent behavior with 885c52abf56SEvan Green * file-backed loop devices: discarded regions read back as zero. 886c52abf56SEvan Green */ 887c52abf56SEvan Green if (S_ISBLK(inode->i_mode) && !lo->lo_encrypt_key_size) { 8884e7b5671SChristoph Hellwig struct request_queue *backingq = bdev_get_queue(I_BDEV(inode)); 889c52abf56SEvan Green 890bcb21c8cSMing Lei max_discard_sectors = backingq->limits.max_write_zeroes_sectors; 891bcb21c8cSMing Lei granularity = backingq->limits.discard_granularity ?: 892bcb21c8cSMing Lei queue_physical_block_size(backingq); 893c52abf56SEvan Green 894c52abf56SEvan Green /* 895dfaa2ef6SLukas Czerner * We use punch hole to reclaim the free space used by the 89612a64d2fSOlaf Hering * image a.k.a. discard. However we do not support discard if 897dfaa2ef6SLukas Czerner * encryption is enabled, because it may give an attacker 898dfaa2ef6SLukas Czerner * useful information. 899dfaa2ef6SLukas Czerner */ 900c52abf56SEvan Green } else if (!file->f_op->fallocate || lo->lo_encrypt_key_size) { 901bcb21c8cSMing Lei max_discard_sectors = 0; 902bcb21c8cSMing Lei granularity = 0; 903dfaa2ef6SLukas Czerner 904c52abf56SEvan Green } else { 905bcb21c8cSMing Lei max_discard_sectors = UINT_MAX >> 9; 906bcb21c8cSMing Lei granularity = inode->i_sb->s_blocksize; 907c52abf56SEvan Green } 908c52abf56SEvan Green 909bcb21c8cSMing Lei if (max_discard_sectors) { 910bcb21c8cSMing Lei q->limits.discard_granularity = granularity; 911bcb21c8cSMing Lei blk_queue_max_discard_sectors(q, max_discard_sectors); 912bcb21c8cSMing Lei blk_queue_max_write_zeroes_sectors(q, max_discard_sectors); 9138b904b5bSBart Van Assche blk_queue_flag_set(QUEUE_FLAG_DISCARD, q); 914bcb21c8cSMing Lei } else { 915bcb21c8cSMing Lei q->limits.discard_granularity = 0; 916bcb21c8cSMing Lei blk_queue_max_discard_sectors(q, 0); 917bcb21c8cSMing Lei blk_queue_max_write_zeroes_sectors(q, 0); 918c52abf56SEvan Green blk_queue_flag_clear(QUEUE_FLAG_DISCARD, q); 919dfaa2ef6SLukas Czerner } 920bcb21c8cSMing Lei q->limits.discard_alignment = 0; 921bcb21c8cSMing Lei } 922dfaa2ef6SLukas Czerner 923e03a3d7aSMing Lei static void loop_unprepare_queue(struct loop_device *lo) 924e03a3d7aSMing Lei { 9253989144fSPetr Mladek kthread_flush_worker(&lo->worker); 926e03a3d7aSMing Lei kthread_stop(lo->worker_task); 927e03a3d7aSMing Lei } 928e03a3d7aSMing Lei 929b2ee7d46SNeilBrown static int loop_kthread_worker_fn(void *worker_ptr) 930b2ee7d46SNeilBrown { 931a37b0715SNeilBrown current->flags |= PF_LOCAL_THROTTLE | PF_MEMALLOC_NOIO; 932b2ee7d46SNeilBrown return kthread_worker_fn(worker_ptr); 933b2ee7d46SNeilBrown } 934b2ee7d46SNeilBrown 935e03a3d7aSMing Lei static int loop_prepare_queue(struct loop_device *lo) 936e03a3d7aSMing Lei { 9373989144fSPetr Mladek kthread_init_worker(&lo->worker); 938b2ee7d46SNeilBrown lo->worker_task = kthread_run(loop_kthread_worker_fn, 939e03a3d7aSMing Lei &lo->worker, "loop%d", lo->lo_number); 940e03a3d7aSMing Lei if (IS_ERR(lo->worker_task)) 941e03a3d7aSMing Lei return -ENOMEM; 942e03a3d7aSMing Lei set_user_nice(lo->worker_task, MIN_NICE); 943e03a3d7aSMing Lei return 0; 944e03a3d7aSMing Lei } 945e03a3d7aSMing Lei 94656a85fd8SHolger Hoffstätte static void loop_update_rotational(struct loop_device *lo) 94756a85fd8SHolger Hoffstätte { 94856a85fd8SHolger Hoffstätte struct file *file = lo->lo_backing_file; 94956a85fd8SHolger Hoffstätte struct inode *file_inode = file->f_mapping->host; 95056a85fd8SHolger Hoffstätte struct block_device *file_bdev = file_inode->i_sb->s_bdev; 95156a85fd8SHolger Hoffstätte struct request_queue *q = lo->lo_queue; 95256a85fd8SHolger Hoffstätte bool nonrot = true; 95356a85fd8SHolger Hoffstätte 95456a85fd8SHolger Hoffstätte /* not all filesystems (e.g. tmpfs) have a sb->s_bdev */ 95556a85fd8SHolger Hoffstätte if (file_bdev) 95656a85fd8SHolger Hoffstätte nonrot = blk_queue_nonrot(bdev_get_queue(file_bdev)); 95756a85fd8SHolger Hoffstätte 95856a85fd8SHolger Hoffstätte if (nonrot) 95956a85fd8SHolger Hoffstätte blk_queue_flag_set(QUEUE_FLAG_NONROT, q); 96056a85fd8SHolger Hoffstätte else 96156a85fd8SHolger Hoffstätte blk_queue_flag_clear(QUEUE_FLAG_NONROT, q); 96256a85fd8SHolger Hoffstätte } 96356a85fd8SHolger Hoffstätte 9641da177e4SLinus Torvalds static int 9651da177e4SLinus Torvalds loop_release_xfer(struct loop_device *lo) 9661da177e4SLinus Torvalds { 9671da177e4SLinus Torvalds int err = 0; 9681da177e4SLinus Torvalds struct loop_func_table *xfer = lo->lo_encryption; 9691da177e4SLinus Torvalds 9701da177e4SLinus Torvalds if (xfer) { 9711da177e4SLinus Torvalds if (xfer->release) 9721da177e4SLinus Torvalds err = xfer->release(lo); 9731da177e4SLinus Torvalds lo->transfer = NULL; 9741da177e4SLinus Torvalds lo->lo_encryption = NULL; 9751da177e4SLinus Torvalds module_put(xfer->owner); 9761da177e4SLinus Torvalds } 9771da177e4SLinus Torvalds return err; 9781da177e4SLinus Torvalds } 9791da177e4SLinus Torvalds 9801da177e4SLinus Torvalds static int 9811da177e4SLinus Torvalds loop_init_xfer(struct loop_device *lo, struct loop_func_table *xfer, 9821da177e4SLinus Torvalds const struct loop_info64 *i) 9831da177e4SLinus Torvalds { 9841da177e4SLinus Torvalds int err = 0; 9851da177e4SLinus Torvalds 9861da177e4SLinus Torvalds if (xfer) { 9871da177e4SLinus Torvalds struct module *owner = xfer->owner; 9881da177e4SLinus Torvalds 9891da177e4SLinus Torvalds if (!try_module_get(owner)) 9901da177e4SLinus Torvalds return -EINVAL; 9911da177e4SLinus Torvalds if (xfer->init) 9921da177e4SLinus Torvalds err = xfer->init(lo, i); 9931da177e4SLinus Torvalds if (err) 9941da177e4SLinus Torvalds module_put(owner); 9951da177e4SLinus Torvalds else 9961da177e4SLinus Torvalds lo->lo_encryption = xfer; 9971da177e4SLinus Torvalds } 9981da177e4SLinus Torvalds return err; 9991da177e4SLinus Torvalds } 10001da177e4SLinus Torvalds 100162ab466cSMartijn Coenen /** 100262ab466cSMartijn Coenen * loop_set_status_from_info - configure device from loop_info 100362ab466cSMartijn Coenen * @lo: struct loop_device to configure 100462ab466cSMartijn Coenen * @info: struct loop_info64 to configure the device with 100562ab466cSMartijn Coenen * 100662ab466cSMartijn Coenen * Configures the loop device parameters according to the passed 100762ab466cSMartijn Coenen * in loop_info64 configuration. 100862ab466cSMartijn Coenen */ 100962ab466cSMartijn Coenen static int 101062ab466cSMartijn Coenen loop_set_status_from_info(struct loop_device *lo, 101162ab466cSMartijn Coenen const struct loop_info64 *info) 101262ab466cSMartijn Coenen { 101362ab466cSMartijn Coenen int err; 101462ab466cSMartijn Coenen struct loop_func_table *xfer; 101562ab466cSMartijn Coenen kuid_t uid = current_uid(); 101662ab466cSMartijn Coenen 101762ab466cSMartijn Coenen if ((unsigned int) info->lo_encrypt_key_size > LO_KEY_SIZE) 101862ab466cSMartijn Coenen return -EINVAL; 101962ab466cSMartijn Coenen 102062ab466cSMartijn Coenen err = loop_release_xfer(lo); 102162ab466cSMartijn Coenen if (err) 102262ab466cSMartijn Coenen return err; 102362ab466cSMartijn Coenen 102462ab466cSMartijn Coenen if (info->lo_encrypt_type) { 102562ab466cSMartijn Coenen unsigned int type = info->lo_encrypt_type; 102662ab466cSMartijn Coenen 102762ab466cSMartijn Coenen if (type >= MAX_LO_CRYPT) 102862ab466cSMartijn Coenen return -EINVAL; 102962ab466cSMartijn Coenen xfer = xfer_funcs[type]; 103062ab466cSMartijn Coenen if (xfer == NULL) 103162ab466cSMartijn Coenen return -EINVAL; 103262ab466cSMartijn Coenen } else 103362ab466cSMartijn Coenen xfer = NULL; 103462ab466cSMartijn Coenen 103562ab466cSMartijn Coenen err = loop_init_xfer(lo, xfer, info); 103662ab466cSMartijn Coenen if (err) 103762ab466cSMartijn Coenen return err; 103862ab466cSMartijn Coenen 103962ab466cSMartijn Coenen lo->lo_offset = info->lo_offset; 104062ab466cSMartijn Coenen lo->lo_sizelimit = info->lo_sizelimit; 104162ab466cSMartijn Coenen memcpy(lo->lo_file_name, info->lo_file_name, LO_NAME_SIZE); 104262ab466cSMartijn Coenen memcpy(lo->lo_crypt_name, info->lo_crypt_name, LO_NAME_SIZE); 104362ab466cSMartijn Coenen lo->lo_file_name[LO_NAME_SIZE-1] = 0; 104462ab466cSMartijn Coenen lo->lo_crypt_name[LO_NAME_SIZE-1] = 0; 104562ab466cSMartijn Coenen 104662ab466cSMartijn Coenen if (!xfer) 104762ab466cSMartijn Coenen xfer = &none_funcs; 104862ab466cSMartijn Coenen lo->transfer = xfer->transfer; 104962ab466cSMartijn Coenen lo->ioctl = xfer->ioctl; 105062ab466cSMartijn Coenen 1051faf1d254SMartijn Coenen lo->lo_flags = info->lo_flags; 105262ab466cSMartijn Coenen 105362ab466cSMartijn Coenen lo->lo_encrypt_key_size = info->lo_encrypt_key_size; 105462ab466cSMartijn Coenen lo->lo_init[0] = info->lo_init[0]; 105562ab466cSMartijn Coenen lo->lo_init[1] = info->lo_init[1]; 105662ab466cSMartijn Coenen if (info->lo_encrypt_key_size) { 105762ab466cSMartijn Coenen memcpy(lo->lo_encrypt_key, info->lo_encrypt_key, 105862ab466cSMartijn Coenen info->lo_encrypt_key_size); 105962ab466cSMartijn Coenen lo->lo_key_owner = uid; 106062ab466cSMartijn Coenen } 106162ab466cSMartijn Coenen 106262ab466cSMartijn Coenen return 0; 106362ab466cSMartijn Coenen } 106462ab466cSMartijn Coenen 10653448914eSMartijn Coenen static int loop_configure(struct loop_device *lo, fmode_t mode, 10663448914eSMartijn Coenen struct block_device *bdev, 10673448914eSMartijn Coenen const struct loop_config *config) 10681da177e4SLinus Torvalds { 10691da177e4SLinus Torvalds struct file *file; 10701da177e4SLinus Torvalds struct inode *inode; 10711da177e4SLinus Torvalds struct address_space *mapping; 10721da177e4SLinus Torvalds int error; 10731da177e4SLinus Torvalds loff_t size; 10741da177e4SLinus Torvalds bool partscan; 10753448914eSMartijn Coenen unsigned short bsize; 10761da177e4SLinus Torvalds 10771da177e4SLinus Torvalds /* This is safe, since we have a reference from open(). */ 10781da177e4SLinus Torvalds __module_get(THIS_MODULE); 10791da177e4SLinus Torvalds 10801da177e4SLinus Torvalds error = -EBADF; 10813448914eSMartijn Coenen file = fget(config->fd); 10821da177e4SLinus Torvalds if (!file) 10831da177e4SLinus Torvalds goto out; 10841da177e4SLinus Torvalds 10851da177e4SLinus Torvalds /* 10861da177e4SLinus Torvalds * If we don't hold exclusive handle for the device, upgrade to it 10871da177e4SLinus Torvalds * here to avoid changing device under exclusive owner. 10881da177e4SLinus Torvalds */ 10891da177e4SLinus Torvalds if (!(mode & FMODE_EXCL)) { 109037c3fc9aSChristoph Hellwig error = bd_prepare_to_claim(bdev, loop_configure); 1091ecbe6bc0SChristoph Hellwig if (error) 10921da177e4SLinus Torvalds goto out_putf; 10931da177e4SLinus Torvalds } 10941da177e4SLinus Torvalds 10956cc8e743SPavel Tatashin error = mutex_lock_killable(&lo->lo_mutex); 10961da177e4SLinus Torvalds if (error) 10971da177e4SLinus Torvalds goto out_bdev; 10981da177e4SLinus Torvalds 10991da177e4SLinus Torvalds error = -EBUSY; 11001da177e4SLinus Torvalds if (lo->lo_state != Lo_unbound) 11011da177e4SLinus Torvalds goto out_unlock; 11021da177e4SLinus Torvalds 11031da177e4SLinus Torvalds error = loop_validate_file(file, bdev); 11041da177e4SLinus Torvalds if (error) 11051da177e4SLinus Torvalds goto out_unlock; 11061da177e4SLinus Torvalds 11071da177e4SLinus Torvalds mapping = file->f_mapping; 11081da177e4SLinus Torvalds inode = mapping->host; 11091da177e4SLinus Torvalds 11103448914eSMartijn Coenen if ((config->info.lo_flags & ~LOOP_CONFIGURE_SETTABLE_FLAGS) != 0) { 11113448914eSMartijn Coenen error = -EINVAL; 11123448914eSMartijn Coenen goto out_unlock; 11133448914eSMartijn Coenen } 11143448914eSMartijn Coenen 11153448914eSMartijn Coenen if (config->block_size) { 11163448914eSMartijn Coenen error = loop_validate_block_size(config->block_size); 11173448914eSMartijn Coenen if (error) 11183448914eSMartijn Coenen goto out_unlock; 11193448914eSMartijn Coenen } 11203448914eSMartijn Coenen 11213448914eSMartijn Coenen error = loop_set_status_from_info(lo, &config->info); 11223448914eSMartijn Coenen if (error) 11233448914eSMartijn Coenen goto out_unlock; 11243448914eSMartijn Coenen 11251da177e4SLinus Torvalds if (!(file->f_mode & FMODE_WRITE) || !(mode & FMODE_WRITE) || 11261da177e4SLinus Torvalds !file->f_op->write_iter) 11273448914eSMartijn Coenen lo->lo_flags |= LO_FLAGS_READ_ONLY; 1128083a6a50SMartijn Coenen 11291da177e4SLinus Torvalds error = loop_prepare_queue(lo); 11301da177e4SLinus Torvalds if (error) 11311da177e4SLinus Torvalds goto out_unlock; 11321da177e4SLinus Torvalds 11337a2f0ce1SChristoph Hellwig set_disk_ro(lo->lo_disk, (lo->lo_flags & LO_FLAGS_READ_ONLY) != 0); 11341da177e4SLinus Torvalds 11353448914eSMartijn Coenen lo->use_dio = lo->lo_flags & LO_FLAGS_DIRECT_IO; 11361da177e4SLinus Torvalds lo->lo_device = bdev; 11371da177e4SLinus Torvalds lo->lo_backing_file = file; 11381da177e4SLinus Torvalds lo->old_gfp_mask = mapping_gfp_mask(mapping); 11391da177e4SLinus Torvalds mapping_set_gfp_mask(mapping, lo->old_gfp_mask & ~(__GFP_IO|__GFP_FS)); 11401da177e4SLinus Torvalds 11413448914eSMartijn Coenen if (!(lo->lo_flags & LO_FLAGS_READ_ONLY) && file->f_op->fsync) 11421da177e4SLinus Torvalds blk_queue_write_cache(lo->lo_queue, true, false); 11431da177e4SLinus Torvalds 11443448914eSMartijn Coenen if (config->block_size) 11453448914eSMartijn Coenen bsize = config->block_size; 114696ed320dSLinus Torvalds else if ((lo->lo_backing_file->f_flags & O_DIRECT) && inode->i_sb->s_bdev) 11471da177e4SLinus Torvalds /* In case of direct I/O, match underlying block size */ 11483448914eSMartijn Coenen bsize = bdev_logical_block_size(inode->i_sb->s_bdev); 11493448914eSMartijn Coenen else 11503448914eSMartijn Coenen bsize = 512; 11511da177e4SLinus Torvalds 11521da177e4SLinus Torvalds blk_queue_logical_block_size(lo->lo_queue, bsize); 11531da177e4SLinus Torvalds blk_queue_physical_block_size(lo->lo_queue, bsize); 11541da177e4SLinus Torvalds blk_queue_io_min(lo->lo_queue, bsize); 11551da177e4SLinus Torvalds 11561da177e4SLinus Torvalds loop_update_rotational(lo); 11571da177e4SLinus Torvalds loop_update_dio(lo); 11581da177e4SLinus Torvalds loop_sysfs_init(lo); 115979e5dc59SMartijn Coenen 116079e5dc59SMartijn Coenen size = get_loop_size(lo, file); 11615795b6f5SMartijn Coenen loop_set_size(lo, size); 11621da177e4SLinus Torvalds 11631da177e4SLinus Torvalds lo->lo_state = Lo_bound; 11641da177e4SLinus Torvalds if (part_shift) 11651da177e4SLinus Torvalds lo->lo_flags |= LO_FLAGS_PARTSCAN; 11661da177e4SLinus Torvalds partscan = lo->lo_flags & LO_FLAGS_PARTSCAN; 1167fe6a8fc5SLennart Poettering if (partscan) 1168fe6a8fc5SLennart Poettering lo->lo_disk->flags &= ~GENHD_FL_NO_PART_SCAN; 11691da177e4SLinus Torvalds 11701da177e4SLinus Torvalds /* Grab the block_device to prevent its destruction after we 11711da177e4SLinus Torvalds * put /dev/loopXX inode. Later in __loop_clr_fd() we bdput(bdev). 11721da177e4SLinus Torvalds */ 11731da177e4SLinus Torvalds bdgrab(bdev); 11746cc8e743SPavel Tatashin mutex_unlock(&lo->lo_mutex); 11751da177e4SLinus Torvalds if (partscan) 11761da177e4SLinus Torvalds loop_reread_partitions(lo, bdev); 117737c3fc9aSChristoph Hellwig if (!(mode & FMODE_EXCL)) 117837c3fc9aSChristoph Hellwig bd_abort_claiming(bdev, loop_configure); 11791da177e4SLinus Torvalds return 0; 11801da177e4SLinus Torvalds 11811da177e4SLinus Torvalds out_unlock: 11826cc8e743SPavel Tatashin mutex_unlock(&lo->lo_mutex); 11831da177e4SLinus Torvalds out_bdev: 118437c3fc9aSChristoph Hellwig if (!(mode & FMODE_EXCL)) 118537c3fc9aSChristoph Hellwig bd_abort_claiming(bdev, loop_configure); 11861da177e4SLinus Torvalds out_putf: 11871da177e4SLinus Torvalds fput(file); 11881da177e4SLinus Torvalds out: 11891da177e4SLinus Torvalds /* This is safe: open() is still holding a reference. */ 11901da177e4SLinus Torvalds module_put(THIS_MODULE); 11911da177e4SLinus Torvalds return error; 11921da177e4SLinus Torvalds } 11931da177e4SLinus Torvalds 11940da03cabSJan Kara static int __loop_clr_fd(struct loop_device *lo, bool release) 11951da177e4SLinus Torvalds { 11967ccd0791SJan Kara struct file *filp = NULL; 1197b4e3ca1aSAl Viro gfp_t gfp = lo->old_gfp_mask; 11984c823cc3SAyan George struct block_device *bdev = lo->lo_device; 11997ccd0791SJan Kara int err = 0; 12000da03cabSJan Kara bool partscan = false; 12010da03cabSJan Kara int lo_number; 12021da177e4SLinus Torvalds 12036cc8e743SPavel Tatashin mutex_lock(&lo->lo_mutex); 12047ccd0791SJan Kara if (WARN_ON_ONCE(lo->lo_state != Lo_rundown)) { 12057ccd0791SJan Kara err = -ENXIO; 12067ccd0791SJan Kara goto out_unlock; 12077ccd0791SJan Kara } 12081da177e4SLinus Torvalds 12097ccd0791SJan Kara filp = lo->lo_backing_file; 12107ccd0791SJan Kara if (filp == NULL) { 12117ccd0791SJan Kara err = -EINVAL; 12127ccd0791SJan Kara goto out_unlock; 12137ccd0791SJan Kara } 12141da177e4SLinus Torvalds 1215*4ceddce5SMauricio Faria de Oliveira if (test_bit(QUEUE_FLAG_WC, &lo->lo_queue->queue_flags)) 1216*4ceddce5SMauricio Faria de Oliveira blk_queue_write_cache(lo->lo_queue, false, false); 1217*4ceddce5SMauricio Faria de Oliveira 1218f8933667SMing Lei /* freeze request queue during the transition */ 1219f8933667SMing Lei blk_mq_freeze_queue(lo->lo_queue); 1220f8933667SMing Lei 12211da177e4SLinus Torvalds spin_lock_irq(&lo->lo_lock); 12221da177e4SLinus Torvalds lo->lo_backing_file = NULL; 122305eb0f25SKay Sievers spin_unlock_irq(&lo->lo_lock); 12241da177e4SLinus Torvalds 12251da177e4SLinus Torvalds loop_release_xfer(lo); 12261da177e4SLinus Torvalds lo->transfer = NULL; 12271da177e4SLinus Torvalds lo->ioctl = NULL; 12281da177e4SLinus Torvalds lo->lo_device = NULL; 12291da177e4SLinus Torvalds lo->lo_encryption = NULL; 12301da177e4SLinus Torvalds lo->lo_offset = 0; 12311da177e4SLinus Torvalds lo->lo_sizelimit = 0; 12321da177e4SLinus Torvalds lo->lo_encrypt_key_size = 0; 12331da177e4SLinus Torvalds memset(lo->lo_encrypt_key, 0, LO_KEY_SIZE); 12341da177e4SLinus Torvalds memset(lo->lo_crypt_name, 0, LO_NAME_SIZE); 12351da177e4SLinus Torvalds memset(lo->lo_file_name, 0, LO_NAME_SIZE); 123689e4fdecSOmar Sandoval blk_queue_logical_block_size(lo->lo_queue, 512); 1237bf093753SOmar Sandoval blk_queue_physical_block_size(lo->lo_queue, 512); 1238bf093753SOmar Sandoval blk_queue_io_min(lo->lo_queue, 512); 1239c1681bf8SAnatol Pomozov if (bdev) { 1240c1681bf8SAnatol Pomozov bdput(bdev); 1241f98393a6SPeter Zijlstra invalidate_bdev(bdev); 1242eedffa28SJeff Layton bdev->bd_inode->i_mapping->wb_err = 0; 1243c1681bf8SAnatol Pomozov } 124473285082SKen Chen set_capacity(lo->lo_disk, 0); 124551a0bb0cSMilan Broz loop_sysfs_exit(lo); 1246c3473c63SDavid Zeuthen if (bdev) { 1247c3473c63SDavid Zeuthen /* let user-space know about this change */ 1248c3473c63SDavid Zeuthen kobject_uevent(&disk_to_dev(bdev->bd_disk)->kobj, KOBJ_CHANGE); 1249c3473c63SDavid Zeuthen } 12501da177e4SLinus Torvalds mapping_set_gfp_mask(filp->f_mapping, gfp); 12511da177e4SLinus Torvalds /* This is safe: open() is still holding a reference. */ 12521da177e4SLinus Torvalds module_put(THIS_MODULE); 1253f8933667SMing Lei blk_mq_unfreeze_queue(lo->lo_queue); 1254f8933667SMing Lei 12550da03cabSJan Kara partscan = lo->lo_flags & LO_FLAGS_PARTSCAN && bdev; 12560da03cabSJan Kara lo_number = lo->lo_number; 12570da03cabSJan Kara loop_unprepare_queue(lo); 12580da03cabSJan Kara out_unlock: 12596cc8e743SPavel Tatashin mutex_unlock(&lo->lo_mutex); 12600da03cabSJan Kara if (partscan) { 1261d57f3374SJan Kara /* 1262d57f3374SJan Kara * bd_mutex has been held already in release path, so don't 1263d57f3374SJan Kara * acquire it if this function is called in such case. 1264d57f3374SJan Kara * 1265d57f3374SJan Kara * If the reread partition isn't from release path, lo_refcnt 1266d57f3374SJan Kara * must be at least one and it can only become zero when the 1267d57f3374SJan Kara * current holder is released. 1268d57f3374SJan Kara */ 1269f0b870dfSChristoph Hellwig if (!release) 1270f0b870dfSChristoph Hellwig mutex_lock(&bdev->bd_mutex); 1271f0b870dfSChristoph Hellwig err = bdev_disk_changed(bdev, false); 1272f0b870dfSChristoph Hellwig if (!release) 1273f0b870dfSChristoph Hellwig mutex_unlock(&bdev->bd_mutex); 127440853d6fSDongli Zhang if (err) 1275d57f3374SJan Kara pr_warn("%s: partition scan of loop%d failed (rc=%d)\n", 12760da03cabSJan Kara __func__, lo_number, err); 1277d57f3374SJan Kara /* Device is gone, no point in returning error */ 1278d57f3374SJan Kara err = 0; 1279d57f3374SJan Kara } 1280758a58d0SDongli Zhang 1281758a58d0SDongli Zhang /* 1282758a58d0SDongli Zhang * lo->lo_state is set to Lo_unbound here after above partscan has 1283758a58d0SDongli Zhang * finished. 1284758a58d0SDongli Zhang * 1285758a58d0SDongli Zhang * There cannot be anybody else entering __loop_clr_fd() as 1286758a58d0SDongli Zhang * lo->lo_backing_file is already cleared and Lo_rundown state 1287758a58d0SDongli Zhang * protects us from all the other places trying to change the 'lo' 1288758a58d0SDongli Zhang * device. 1289758a58d0SDongli Zhang */ 12906cc8e743SPavel Tatashin mutex_lock(&lo->lo_mutex); 1291758a58d0SDongli Zhang lo->lo_flags = 0; 1292758a58d0SDongli Zhang if (!part_shift) 1293758a58d0SDongli Zhang lo->lo_disk->flags |= GENHD_FL_NO_PART_SCAN; 1294758a58d0SDongli Zhang lo->lo_state = Lo_unbound; 12956cc8e743SPavel Tatashin mutex_unlock(&lo->lo_mutex); 1296758a58d0SDongli Zhang 1297f028f3b2SNikanth Karthikesan /* 12986cc8e743SPavel Tatashin * Need not hold lo_mutex to fput backing file. Calling fput holding 12996cc8e743SPavel Tatashin * lo_mutex triggers a circular lock dependency possibility warning as 13006cc8e743SPavel Tatashin * fput can take bd_mutex which is usually taken before lo_mutex. 1301f028f3b2SNikanth Karthikesan */ 13027ccd0791SJan Kara if (filp) 1303f028f3b2SNikanth Karthikesan fput(filp); 13047ccd0791SJan Kara return err; 13051da177e4SLinus Torvalds } 13061da177e4SLinus Torvalds 1307a2505b79SJan Kara static int loop_clr_fd(struct loop_device *lo) 1308a2505b79SJan Kara { 13097ccd0791SJan Kara int err; 13107ccd0791SJan Kara 13116cc8e743SPavel Tatashin err = mutex_lock_killable(&lo->lo_mutex); 13127ccd0791SJan Kara if (err) 13137ccd0791SJan Kara return err; 13147ccd0791SJan Kara if (lo->lo_state != Lo_bound) { 13156cc8e743SPavel Tatashin mutex_unlock(&lo->lo_mutex); 1316a2505b79SJan Kara return -ENXIO; 13177ccd0791SJan Kara } 1318a2505b79SJan Kara /* 1319a2505b79SJan Kara * If we've explicitly asked to tear down the loop device, 1320a2505b79SJan Kara * and it has an elevated reference count, set it for auto-teardown when 1321a2505b79SJan Kara * the last reference goes away. This stops $!~#$@ udev from 1322a2505b79SJan Kara * preventing teardown because it decided that it needs to run blkid on 1323a2505b79SJan Kara * the loopback device whenever they appear. xfstests is notorious for 1324a2505b79SJan Kara * failing tests because blkid via udev races with a losetup 1325a2505b79SJan Kara * <dev>/do something like mkfs/losetup -d <dev> causing the losetup -d 1326a2505b79SJan Kara * command to fail with EBUSY. 1327a2505b79SJan Kara */ 1328a2505b79SJan Kara if (atomic_read(&lo->lo_refcnt) > 1) { 1329a2505b79SJan Kara lo->lo_flags |= LO_FLAGS_AUTOCLEAR; 13306cc8e743SPavel Tatashin mutex_unlock(&lo->lo_mutex); 1331a2505b79SJan Kara return 0; 1332a2505b79SJan Kara } 1333a2505b79SJan Kara lo->lo_state = Lo_rundown; 13346cc8e743SPavel Tatashin mutex_unlock(&lo->lo_mutex); 1335a2505b79SJan Kara 13360da03cabSJan Kara return __loop_clr_fd(lo, false); 1337a2505b79SJan Kara } 1338a2505b79SJan Kara 13391da177e4SLinus Torvalds static int 13401da177e4SLinus Torvalds loop_set_status(struct loop_device *lo, const struct loop_info64 *info) 13411da177e4SLinus Torvalds { 13421da177e4SLinus Torvalds int err; 134385b0a54aSJan Kara struct block_device *bdev; 13440c3796c2SMartijn Coenen kuid_t uid = current_uid(); 1345faf1d254SMartijn Coenen int prev_lo_flags; 134685b0a54aSJan Kara bool partscan = false; 13470c3796c2SMartijn Coenen bool size_changed = false; 13481da177e4SLinus Torvalds 13496cc8e743SPavel Tatashin err = mutex_lock_killable(&lo->lo_mutex); 1350550df5fdSJan Kara if (err) 1351550df5fdSJan Kara return err; 1352b0fafa81SDavid Howells if (lo->lo_encrypt_key_size && 1353e4849737SEric W. Biederman !uid_eq(lo->lo_key_owner, uid) && 1354550df5fdSJan Kara !capable(CAP_SYS_ADMIN)) { 1355550df5fdSJan Kara err = -EPERM; 1356550df5fdSJan Kara goto out_unlock; 1357550df5fdSJan Kara } 1358550df5fdSJan Kara if (lo->lo_state != Lo_bound) { 1359550df5fdSJan Kara err = -ENXIO; 1360550df5fdSJan Kara goto out_unlock; 1361550df5fdSJan Kara } 13621da177e4SLinus Torvalds 13635db470e2SJaegeuk Kim if (lo->lo_offset != info->lo_offset || 13645db470e2SJaegeuk Kim lo->lo_sizelimit != info->lo_sizelimit) { 13650c3796c2SMartijn Coenen size_changed = true; 13665db470e2SJaegeuk Kim sync_blockdev(lo->lo_device); 1367f4bd34b1SZheng Bin invalidate_bdev(lo->lo_device); 13685db470e2SJaegeuk Kim } 13695db470e2SJaegeuk Kim 1370ecdd0959SMing Lei /* I/O need to be drained during transfer transition */ 1371ecdd0959SMing Lei blk_mq_freeze_queue(lo->lo_queue); 1372ecdd0959SMing Lei 13730c3796c2SMartijn Coenen if (size_changed && lo->lo_device->bd_inode->i_mapping->nrpages) { 1374f4bd34b1SZheng Bin /* If any pages were dirtied after invalidate_bdev(), try again */ 13755db470e2SJaegeuk Kim err = -EAGAIN; 13765db470e2SJaegeuk Kim pr_warn("%s: loop%d (%s) has still dirty pages (nrpages=%lu)\n", 13775db470e2SJaegeuk Kim __func__, lo->lo_number, lo->lo_file_name, 13785db470e2SJaegeuk Kim lo->lo_device->bd_inode->i_mapping->nrpages); 13795db470e2SJaegeuk Kim goto out_unfreeze; 13805db470e2SJaegeuk Kim } 13810c3796c2SMartijn Coenen 1382faf1d254SMartijn Coenen prev_lo_flags = lo->lo_flags; 1383faf1d254SMartijn Coenen 13840c3796c2SMartijn Coenen err = loop_set_status_from_info(lo, info); 13850c3796c2SMartijn Coenen if (err) 1386550df5fdSJan Kara goto out_unfreeze; 13870c3796c2SMartijn Coenen 1388faf1d254SMartijn Coenen /* Mask out flags that can't be set using LOOP_SET_STATUS. */ 13896ac92fb5SMartijn Coenen lo->lo_flags &= LOOP_SET_STATUS_SETTABLE_FLAGS; 1390faf1d254SMartijn Coenen /* For those flags, use the previous values instead */ 1391faf1d254SMartijn Coenen lo->lo_flags |= prev_lo_flags & ~LOOP_SET_STATUS_SETTABLE_FLAGS; 1392faf1d254SMartijn Coenen /* For flags that can't be cleared, use previous values too */ 1393faf1d254SMartijn Coenen lo->lo_flags |= prev_lo_flags & ~LOOP_SET_STATUS_CLEARABLE_FLAGS; 1394faf1d254SMartijn Coenen 1395b0bd158dSMartijn Coenen if (size_changed) { 1396b0bd158dSMartijn Coenen loff_t new_size = get_size(lo->lo_offset, lo->lo_sizelimit, 1397b0bd158dSMartijn Coenen lo->lo_backing_file); 1398b0bd158dSMartijn Coenen loop_set_size(lo, new_size); 1399b040ad9cSArnd Bergmann } 1400541c742aSGuo Chao 1401dfaa2ef6SLukas Czerner loop_config_discard(lo); 14021da177e4SLinus Torvalds 14032e5ab5f3SMing Lei /* update dio if lo_offset or transfer is changed */ 14042e5ab5f3SMing Lei __loop_update_dio(lo, lo->use_dio); 14052e5ab5f3SMing Lei 1406550df5fdSJan Kara out_unfreeze: 1407ecdd0959SMing Lei blk_mq_unfreeze_queue(lo->lo_queue); 1408e02898b4SOmar Sandoval 1409faf1d254SMartijn Coenen if (!err && (lo->lo_flags & LO_FLAGS_PARTSCAN) && 1410faf1d254SMartijn Coenen !(prev_lo_flags & LO_FLAGS_PARTSCAN)) { 1411e02898b4SOmar Sandoval lo->lo_disk->flags &= ~GENHD_FL_NO_PART_SCAN; 141285b0a54aSJan Kara bdev = lo->lo_device; 141385b0a54aSJan Kara partscan = true; 1414e02898b4SOmar Sandoval } 1415550df5fdSJan Kara out_unlock: 14166cc8e743SPavel Tatashin mutex_unlock(&lo->lo_mutex); 141785b0a54aSJan Kara if (partscan) 141885b0a54aSJan Kara loop_reread_partitions(lo, bdev); 1419e02898b4SOmar Sandoval 1420ecdd0959SMing Lei return err; 14211da177e4SLinus Torvalds } 14221da177e4SLinus Torvalds 14231da177e4SLinus Torvalds static int 14241da177e4SLinus Torvalds loop_get_status(struct loop_device *lo, struct loop_info64 *info) 14251da177e4SLinus Torvalds { 1426b1ab5fa3STetsuo Handa struct path path; 14271da177e4SLinus Torvalds struct kstat stat; 14282d1d4c1eSOmar Sandoval int ret; 14291da177e4SLinus Torvalds 14306cc8e743SPavel Tatashin ret = mutex_lock_killable(&lo->lo_mutex); 14314a5ce9baSJan Kara if (ret) 14324a5ce9baSJan Kara return ret; 14332d1d4c1eSOmar Sandoval if (lo->lo_state != Lo_bound) { 14346cc8e743SPavel Tatashin mutex_unlock(&lo->lo_mutex); 14351da177e4SLinus Torvalds return -ENXIO; 14362d1d4c1eSOmar Sandoval } 14372d1d4c1eSOmar Sandoval 14381da177e4SLinus Torvalds memset(info, 0, sizeof(*info)); 14391da177e4SLinus Torvalds info->lo_number = lo->lo_number; 14401da177e4SLinus Torvalds info->lo_offset = lo->lo_offset; 14411da177e4SLinus Torvalds info->lo_sizelimit = lo->lo_sizelimit; 14421da177e4SLinus Torvalds info->lo_flags = lo->lo_flags; 14431da177e4SLinus Torvalds memcpy(info->lo_file_name, lo->lo_file_name, LO_NAME_SIZE); 14441da177e4SLinus Torvalds memcpy(info->lo_crypt_name, lo->lo_crypt_name, LO_NAME_SIZE); 14451da177e4SLinus Torvalds info->lo_encrypt_type = 14461da177e4SLinus Torvalds lo->lo_encryption ? lo->lo_encryption->number : 0; 14471da177e4SLinus Torvalds if (lo->lo_encrypt_key_size && capable(CAP_SYS_ADMIN)) { 14481da177e4SLinus Torvalds info->lo_encrypt_key_size = lo->lo_encrypt_key_size; 14491da177e4SLinus Torvalds memcpy(info->lo_encrypt_key, lo->lo_encrypt_key, 14501da177e4SLinus Torvalds lo->lo_encrypt_key_size); 14511da177e4SLinus Torvalds } 14522d1d4c1eSOmar Sandoval 14536cc8e743SPavel Tatashin /* Drop lo_mutex while we call into the filesystem. */ 1454b1ab5fa3STetsuo Handa path = lo->lo_backing_file->f_path; 1455b1ab5fa3STetsuo Handa path_get(&path); 14566cc8e743SPavel Tatashin mutex_unlock(&lo->lo_mutex); 1457b1ab5fa3STetsuo Handa ret = vfs_getattr(&path, &stat, STATX_INO, AT_STATX_SYNC_AS_STAT); 14582d1d4c1eSOmar Sandoval if (!ret) { 14592d1d4c1eSOmar Sandoval info->lo_device = huge_encode_dev(stat.dev); 14602d1d4c1eSOmar Sandoval info->lo_inode = stat.ino; 14612d1d4c1eSOmar Sandoval info->lo_rdevice = huge_encode_dev(stat.rdev); 14622d1d4c1eSOmar Sandoval } 1463b1ab5fa3STetsuo Handa path_put(&path); 14642d1d4c1eSOmar Sandoval return ret; 14651da177e4SLinus Torvalds } 14661da177e4SLinus Torvalds 14671da177e4SLinus Torvalds static void 14681da177e4SLinus Torvalds loop_info64_from_old(const struct loop_info *info, struct loop_info64 *info64) 14691da177e4SLinus Torvalds { 14701da177e4SLinus Torvalds memset(info64, 0, sizeof(*info64)); 14711da177e4SLinus Torvalds info64->lo_number = info->lo_number; 14721da177e4SLinus Torvalds info64->lo_device = info->lo_device; 14731da177e4SLinus Torvalds info64->lo_inode = info->lo_inode; 14741da177e4SLinus Torvalds info64->lo_rdevice = info->lo_rdevice; 14751da177e4SLinus Torvalds info64->lo_offset = info->lo_offset; 14761da177e4SLinus Torvalds info64->lo_sizelimit = 0; 14771da177e4SLinus Torvalds info64->lo_encrypt_type = info->lo_encrypt_type; 14781da177e4SLinus Torvalds info64->lo_encrypt_key_size = info->lo_encrypt_key_size; 14791da177e4SLinus Torvalds info64->lo_flags = info->lo_flags; 14801da177e4SLinus Torvalds info64->lo_init[0] = info->lo_init[0]; 14811da177e4SLinus Torvalds info64->lo_init[1] = info->lo_init[1]; 14821da177e4SLinus Torvalds if (info->lo_encrypt_type == LO_CRYPT_CRYPTOAPI) 14831da177e4SLinus Torvalds memcpy(info64->lo_crypt_name, info->lo_name, LO_NAME_SIZE); 14841da177e4SLinus Torvalds else 14851da177e4SLinus Torvalds memcpy(info64->lo_file_name, info->lo_name, LO_NAME_SIZE); 14861da177e4SLinus Torvalds memcpy(info64->lo_encrypt_key, info->lo_encrypt_key, LO_KEY_SIZE); 14871da177e4SLinus Torvalds } 14881da177e4SLinus Torvalds 14891da177e4SLinus Torvalds static int 14901da177e4SLinus Torvalds loop_info64_to_old(const struct loop_info64 *info64, struct loop_info *info) 14911da177e4SLinus Torvalds { 14921da177e4SLinus Torvalds memset(info, 0, sizeof(*info)); 14931da177e4SLinus Torvalds info->lo_number = info64->lo_number; 14941da177e4SLinus Torvalds info->lo_device = info64->lo_device; 14951da177e4SLinus Torvalds info->lo_inode = info64->lo_inode; 14961da177e4SLinus Torvalds info->lo_rdevice = info64->lo_rdevice; 14971da177e4SLinus Torvalds info->lo_offset = info64->lo_offset; 14981da177e4SLinus Torvalds info->lo_encrypt_type = info64->lo_encrypt_type; 14991da177e4SLinus Torvalds info->lo_encrypt_key_size = info64->lo_encrypt_key_size; 15001da177e4SLinus Torvalds info->lo_flags = info64->lo_flags; 15011da177e4SLinus Torvalds info->lo_init[0] = info64->lo_init[0]; 15021da177e4SLinus Torvalds info->lo_init[1] = info64->lo_init[1]; 15031da177e4SLinus Torvalds if (info->lo_encrypt_type == LO_CRYPT_CRYPTOAPI) 15041da177e4SLinus Torvalds memcpy(info->lo_name, info64->lo_crypt_name, LO_NAME_SIZE); 15051da177e4SLinus Torvalds else 15061da177e4SLinus Torvalds memcpy(info->lo_name, info64->lo_file_name, LO_NAME_SIZE); 15071da177e4SLinus Torvalds memcpy(info->lo_encrypt_key, info64->lo_encrypt_key, LO_KEY_SIZE); 15081da177e4SLinus Torvalds 15091da177e4SLinus Torvalds /* error in case values were truncated */ 15101da177e4SLinus Torvalds if (info->lo_device != info64->lo_device || 15111da177e4SLinus Torvalds info->lo_rdevice != info64->lo_rdevice || 15121da177e4SLinus Torvalds info->lo_inode != info64->lo_inode || 15131da177e4SLinus Torvalds info->lo_offset != info64->lo_offset) 15141da177e4SLinus Torvalds return -EOVERFLOW; 15151da177e4SLinus Torvalds 15161da177e4SLinus Torvalds return 0; 15171da177e4SLinus Torvalds } 15181da177e4SLinus Torvalds 15191da177e4SLinus Torvalds static int 15201da177e4SLinus Torvalds loop_set_status_old(struct loop_device *lo, const struct loop_info __user *arg) 15211da177e4SLinus Torvalds { 15221da177e4SLinus Torvalds struct loop_info info; 15231da177e4SLinus Torvalds struct loop_info64 info64; 15241da177e4SLinus Torvalds 15251da177e4SLinus Torvalds if (copy_from_user(&info, arg, sizeof (struct loop_info))) 15261da177e4SLinus Torvalds return -EFAULT; 15271da177e4SLinus Torvalds loop_info64_from_old(&info, &info64); 15281da177e4SLinus Torvalds return loop_set_status(lo, &info64); 15291da177e4SLinus Torvalds } 15301da177e4SLinus Torvalds 15311da177e4SLinus Torvalds static int 15321da177e4SLinus Torvalds loop_set_status64(struct loop_device *lo, const struct loop_info64 __user *arg) 15331da177e4SLinus Torvalds { 15341da177e4SLinus Torvalds struct loop_info64 info64; 15351da177e4SLinus Torvalds 15361da177e4SLinus Torvalds if (copy_from_user(&info64, arg, sizeof (struct loop_info64))) 15371da177e4SLinus Torvalds return -EFAULT; 15381da177e4SLinus Torvalds return loop_set_status(lo, &info64); 15391da177e4SLinus Torvalds } 15401da177e4SLinus Torvalds 15411da177e4SLinus Torvalds static int 15421da177e4SLinus Torvalds loop_get_status_old(struct loop_device *lo, struct loop_info __user *arg) { 15431da177e4SLinus Torvalds struct loop_info info; 15441da177e4SLinus Torvalds struct loop_info64 info64; 1545bdac616dSOmar Sandoval int err; 15461da177e4SLinus Torvalds 15474a5ce9baSJan Kara if (!arg) 1548bdac616dSOmar Sandoval return -EINVAL; 15491da177e4SLinus Torvalds err = loop_get_status(lo, &info64); 15501da177e4SLinus Torvalds if (!err) 15511da177e4SLinus Torvalds err = loop_info64_to_old(&info64, &info); 15521da177e4SLinus Torvalds if (!err && copy_to_user(arg, &info, sizeof(info))) 15531da177e4SLinus Torvalds err = -EFAULT; 15541da177e4SLinus Torvalds 15551da177e4SLinus Torvalds return err; 15561da177e4SLinus Torvalds } 15571da177e4SLinus Torvalds 15581da177e4SLinus Torvalds static int 15591da177e4SLinus Torvalds loop_get_status64(struct loop_device *lo, struct loop_info64 __user *arg) { 15601da177e4SLinus Torvalds struct loop_info64 info64; 1561bdac616dSOmar Sandoval int err; 15621da177e4SLinus Torvalds 15634a5ce9baSJan Kara if (!arg) 1564bdac616dSOmar Sandoval return -EINVAL; 15651da177e4SLinus Torvalds err = loop_get_status(lo, &info64); 15661da177e4SLinus Torvalds if (!err && copy_to_user(arg, &info64, sizeof(info64))) 15671da177e4SLinus Torvalds err = -EFAULT; 15681da177e4SLinus Torvalds 15691da177e4SLinus Torvalds return err; 15701da177e4SLinus Torvalds } 15711da177e4SLinus Torvalds 157251001b7dSHannes Reinecke static int loop_set_capacity(struct loop_device *lo) 157353d66608SJ. R. Okajima { 15740a6ed1b5SMartijn Coenen loff_t size; 15750a6ed1b5SMartijn Coenen 157653d66608SJ. R. Okajima if (unlikely(lo->lo_state != Lo_bound)) 15777b0576a3SGuo Chao return -ENXIO; 157853d66608SJ. R. Okajima 15790a6ed1b5SMartijn Coenen size = get_loop_size(lo, lo->lo_backing_file); 15800a6ed1b5SMartijn Coenen loop_set_size(lo, size); 1581083a6a50SMartijn Coenen 1582083a6a50SMartijn Coenen return 0; 158353d66608SJ. R. Okajima } 158453d66608SJ. R. Okajima 1585ab1cb278SMing Lei static int loop_set_dio(struct loop_device *lo, unsigned long arg) 1586ab1cb278SMing Lei { 1587ab1cb278SMing Lei int error = -ENXIO; 1588ab1cb278SMing Lei if (lo->lo_state != Lo_bound) 1589ab1cb278SMing Lei goto out; 1590ab1cb278SMing Lei 1591ab1cb278SMing Lei __loop_update_dio(lo, !!arg); 1592ab1cb278SMing Lei if (lo->use_dio == !!arg) 1593ab1cb278SMing Lei return 0; 1594ab1cb278SMing Lei error = -EINVAL; 1595ab1cb278SMing Lei out: 1596ab1cb278SMing Lei return error; 1597ab1cb278SMing Lei } 1598ab1cb278SMing Lei 159989e4fdecSOmar Sandoval static int loop_set_block_size(struct loop_device *lo, unsigned long arg) 160089e4fdecSOmar Sandoval { 16015db470e2SJaegeuk Kim int err = 0; 16025db470e2SJaegeuk Kim 160389e4fdecSOmar Sandoval if (lo->lo_state != Lo_bound) 160489e4fdecSOmar Sandoval return -ENXIO; 160589e4fdecSOmar Sandoval 16063448914eSMartijn Coenen err = loop_validate_block_size(arg); 16073448914eSMartijn Coenen if (err) 16083448914eSMartijn Coenen return err; 160989e4fdecSOmar Sandoval 16107e81f99aSMartijn Coenen if (lo->lo_queue->limits.logical_block_size == arg) 16117e81f99aSMartijn Coenen return 0; 16127e81f99aSMartijn Coenen 16135db470e2SJaegeuk Kim sync_blockdev(lo->lo_device); 1614f4bd34b1SZheng Bin invalidate_bdev(lo->lo_device); 16155db470e2SJaegeuk Kim 161689e4fdecSOmar Sandoval blk_mq_freeze_queue(lo->lo_queue); 161789e4fdecSOmar Sandoval 1618f4bd34b1SZheng Bin /* invalidate_bdev should have truncated all the pages */ 16197e81f99aSMartijn Coenen if (lo->lo_device->bd_inode->i_mapping->nrpages) { 16205db470e2SJaegeuk Kim err = -EAGAIN; 16215db470e2SJaegeuk Kim pr_warn("%s: loop%d (%s) has still dirty pages (nrpages=%lu)\n", 16225db470e2SJaegeuk Kim __func__, lo->lo_number, lo->lo_file_name, 16235db470e2SJaegeuk Kim lo->lo_device->bd_inode->i_mapping->nrpages); 16245db470e2SJaegeuk Kim goto out_unfreeze; 16255db470e2SJaegeuk Kim } 16265db470e2SJaegeuk Kim 162789e4fdecSOmar Sandoval blk_queue_logical_block_size(lo->lo_queue, arg); 1628bf093753SOmar Sandoval blk_queue_physical_block_size(lo->lo_queue, arg); 1629bf093753SOmar Sandoval blk_queue_io_min(lo->lo_queue, arg); 163089e4fdecSOmar Sandoval loop_update_dio(lo); 16315db470e2SJaegeuk Kim out_unfreeze: 163289e4fdecSOmar Sandoval blk_mq_unfreeze_queue(lo->lo_queue); 163389e4fdecSOmar Sandoval 16345db470e2SJaegeuk Kim return err; 163589e4fdecSOmar Sandoval } 163689e4fdecSOmar Sandoval 1637a1316544SJan Kara static int lo_simple_ioctl(struct loop_device *lo, unsigned int cmd, 1638a1316544SJan Kara unsigned long arg) 16391da177e4SLinus Torvalds { 16401da177e4SLinus Torvalds int err; 16411da177e4SLinus Torvalds 16426cc8e743SPavel Tatashin err = mutex_lock_killable(&lo->lo_mutex); 16433148ffbdSOmar Sandoval if (err) 1644a1316544SJan Kara return err; 16451da177e4SLinus Torvalds switch (cmd) { 164653d66608SJ. R. Okajima case LOOP_SET_CAPACITY: 164751001b7dSHannes Reinecke err = loop_set_capacity(lo); 164853d66608SJ. R. Okajima break; 1649ab1cb278SMing Lei case LOOP_SET_DIRECT_IO: 1650ab1cb278SMing Lei err = loop_set_dio(lo, arg); 1651ab1cb278SMing Lei break; 165289e4fdecSOmar Sandoval case LOOP_SET_BLOCK_SIZE: 165389e4fdecSOmar Sandoval err = loop_set_block_size(lo, arg); 165489e4fdecSOmar Sandoval break; 16551da177e4SLinus Torvalds default: 16561da177e4SLinus Torvalds err = lo->ioctl ? lo->ioctl(lo, cmd, arg) : -EINVAL; 16571da177e4SLinus Torvalds } 16586cc8e743SPavel Tatashin mutex_unlock(&lo->lo_mutex); 1659a1316544SJan Kara return err; 1660a1316544SJan Kara } 1661f028f3b2SNikanth Karthikesan 1662a1316544SJan Kara static int lo_ioctl(struct block_device *bdev, fmode_t mode, 1663a1316544SJan Kara unsigned int cmd, unsigned long arg) 1664a1316544SJan Kara { 1665a1316544SJan Kara struct loop_device *lo = bdev->bd_disk->private_data; 1666571fae6eSMartijn Coenen void __user *argp = (void __user *) arg; 1667a1316544SJan Kara int err; 1668a1316544SJan Kara 1669a1316544SJan Kara switch (cmd) { 16703448914eSMartijn Coenen case LOOP_SET_FD: { 16713448914eSMartijn Coenen /* 16723448914eSMartijn Coenen * Legacy case - pass in a zeroed out struct loop_config with 16733448914eSMartijn Coenen * only the file descriptor set , which corresponds with the 16743448914eSMartijn Coenen * default parameters we'd have used otherwise. 16753448914eSMartijn Coenen */ 16763448914eSMartijn Coenen struct loop_config config; 16773448914eSMartijn Coenen 16783448914eSMartijn Coenen memset(&config, 0, sizeof(config)); 16793448914eSMartijn Coenen config.fd = arg; 16803448914eSMartijn Coenen 16813448914eSMartijn Coenen return loop_configure(lo, mode, bdev, &config); 16823448914eSMartijn Coenen } 16833448914eSMartijn Coenen case LOOP_CONFIGURE: { 16843448914eSMartijn Coenen struct loop_config config; 16853448914eSMartijn Coenen 16863448914eSMartijn Coenen if (copy_from_user(&config, argp, sizeof(config))) 16873448914eSMartijn Coenen return -EFAULT; 16883448914eSMartijn Coenen 16893448914eSMartijn Coenen return loop_configure(lo, mode, bdev, &config); 16903448914eSMartijn Coenen } 1691a1316544SJan Kara case LOOP_CHANGE_FD: 1692c3710770SJan Kara return loop_change_fd(lo, bdev, arg); 1693a1316544SJan Kara case LOOP_CLR_FD: 16947ccd0791SJan Kara return loop_clr_fd(lo); 1695a1316544SJan Kara case LOOP_SET_STATUS: 1696a1316544SJan Kara err = -EPERM; 1697a1316544SJan Kara if ((mode & FMODE_WRITE) || capable(CAP_SYS_ADMIN)) { 1698571fae6eSMartijn Coenen err = loop_set_status_old(lo, argp); 1699a1316544SJan Kara } 1700a1316544SJan Kara break; 1701a1316544SJan Kara case LOOP_GET_STATUS: 1702571fae6eSMartijn Coenen return loop_get_status_old(lo, argp); 1703a1316544SJan Kara case LOOP_SET_STATUS64: 1704a1316544SJan Kara err = -EPERM; 1705a1316544SJan Kara if ((mode & FMODE_WRITE) || capable(CAP_SYS_ADMIN)) { 1706571fae6eSMartijn Coenen err = loop_set_status64(lo, argp); 1707a1316544SJan Kara } 1708a1316544SJan Kara break; 1709a1316544SJan Kara case LOOP_GET_STATUS64: 1710571fae6eSMartijn Coenen return loop_get_status64(lo, argp); 1711a1316544SJan Kara case LOOP_SET_CAPACITY: 1712a1316544SJan Kara case LOOP_SET_DIRECT_IO: 1713a1316544SJan Kara case LOOP_SET_BLOCK_SIZE: 1714a1316544SJan Kara if (!(mode & FMODE_WRITE) && !capable(CAP_SYS_ADMIN)) 1715a1316544SJan Kara return -EPERM; 1716df561f66SGustavo A. R. Silva fallthrough; 1717a1316544SJan Kara default: 1718a1316544SJan Kara err = lo_simple_ioctl(lo, cmd, arg); 1719a1316544SJan Kara break; 1720a1316544SJan Kara } 1721a1316544SJan Kara 17221da177e4SLinus Torvalds return err; 17231da177e4SLinus Torvalds } 17241da177e4SLinus Torvalds 1725863d5b82SDavid Howells #ifdef CONFIG_COMPAT 1726863d5b82SDavid Howells struct compat_loop_info { 1727863d5b82SDavid Howells compat_int_t lo_number; /* ioctl r/o */ 1728863d5b82SDavid Howells compat_dev_t lo_device; /* ioctl r/o */ 1729863d5b82SDavid Howells compat_ulong_t lo_inode; /* ioctl r/o */ 1730863d5b82SDavid Howells compat_dev_t lo_rdevice; /* ioctl r/o */ 1731863d5b82SDavid Howells compat_int_t lo_offset; 1732863d5b82SDavid Howells compat_int_t lo_encrypt_type; 1733863d5b82SDavid Howells compat_int_t lo_encrypt_key_size; /* ioctl w/o */ 1734863d5b82SDavid Howells compat_int_t lo_flags; /* ioctl r/o */ 1735863d5b82SDavid Howells char lo_name[LO_NAME_SIZE]; 1736863d5b82SDavid Howells unsigned char lo_encrypt_key[LO_KEY_SIZE]; /* ioctl w/o */ 1737863d5b82SDavid Howells compat_ulong_t lo_init[2]; 1738863d5b82SDavid Howells char reserved[4]; 1739863d5b82SDavid Howells }; 1740863d5b82SDavid Howells 1741863d5b82SDavid Howells /* 1742863d5b82SDavid Howells * Transfer 32-bit compatibility structure in userspace to 64-bit loop info 1743863d5b82SDavid Howells * - noinlined to reduce stack space usage in main part of driver 1744863d5b82SDavid Howells */ 1745863d5b82SDavid Howells static noinline int 1746ba674cfcSAl Viro loop_info64_from_compat(const struct compat_loop_info __user *arg, 1747863d5b82SDavid Howells struct loop_info64 *info64) 1748863d5b82SDavid Howells { 1749863d5b82SDavid Howells struct compat_loop_info info; 1750863d5b82SDavid Howells 1751863d5b82SDavid Howells if (copy_from_user(&info, arg, sizeof(info))) 1752863d5b82SDavid Howells return -EFAULT; 1753863d5b82SDavid Howells 1754863d5b82SDavid Howells memset(info64, 0, sizeof(*info64)); 1755863d5b82SDavid Howells info64->lo_number = info.lo_number; 1756863d5b82SDavid Howells info64->lo_device = info.lo_device; 1757863d5b82SDavid Howells info64->lo_inode = info.lo_inode; 1758863d5b82SDavid Howells info64->lo_rdevice = info.lo_rdevice; 1759863d5b82SDavid Howells info64->lo_offset = info.lo_offset; 1760863d5b82SDavid Howells info64->lo_sizelimit = 0; 1761863d5b82SDavid Howells info64->lo_encrypt_type = info.lo_encrypt_type; 1762863d5b82SDavid Howells info64->lo_encrypt_key_size = info.lo_encrypt_key_size; 1763863d5b82SDavid Howells info64->lo_flags = info.lo_flags; 1764863d5b82SDavid Howells info64->lo_init[0] = info.lo_init[0]; 1765863d5b82SDavid Howells info64->lo_init[1] = info.lo_init[1]; 1766863d5b82SDavid Howells if (info.lo_encrypt_type == LO_CRYPT_CRYPTOAPI) 1767863d5b82SDavid Howells memcpy(info64->lo_crypt_name, info.lo_name, LO_NAME_SIZE); 1768863d5b82SDavid Howells else 1769863d5b82SDavid Howells memcpy(info64->lo_file_name, info.lo_name, LO_NAME_SIZE); 1770863d5b82SDavid Howells memcpy(info64->lo_encrypt_key, info.lo_encrypt_key, LO_KEY_SIZE); 1771863d5b82SDavid Howells return 0; 1772863d5b82SDavid Howells } 1773863d5b82SDavid Howells 1774863d5b82SDavid Howells /* 1775863d5b82SDavid Howells * Transfer 64-bit loop info to 32-bit compatibility structure in userspace 1776863d5b82SDavid Howells * - noinlined to reduce stack space usage in main part of driver 1777863d5b82SDavid Howells */ 1778863d5b82SDavid Howells static noinline int 1779863d5b82SDavid Howells loop_info64_to_compat(const struct loop_info64 *info64, 1780863d5b82SDavid Howells struct compat_loop_info __user *arg) 1781863d5b82SDavid Howells { 1782863d5b82SDavid Howells struct compat_loop_info info; 1783863d5b82SDavid Howells 1784863d5b82SDavid Howells memset(&info, 0, sizeof(info)); 1785863d5b82SDavid Howells info.lo_number = info64->lo_number; 1786863d5b82SDavid Howells info.lo_device = info64->lo_device; 1787863d5b82SDavid Howells info.lo_inode = info64->lo_inode; 1788863d5b82SDavid Howells info.lo_rdevice = info64->lo_rdevice; 1789863d5b82SDavid Howells info.lo_offset = info64->lo_offset; 1790863d5b82SDavid Howells info.lo_encrypt_type = info64->lo_encrypt_type; 1791863d5b82SDavid Howells info.lo_encrypt_key_size = info64->lo_encrypt_key_size; 1792863d5b82SDavid Howells info.lo_flags = info64->lo_flags; 1793863d5b82SDavid Howells info.lo_init[0] = info64->lo_init[0]; 1794863d5b82SDavid Howells info.lo_init[1] = info64->lo_init[1]; 1795863d5b82SDavid Howells if (info.lo_encrypt_type == LO_CRYPT_CRYPTOAPI) 1796863d5b82SDavid Howells memcpy(info.lo_name, info64->lo_crypt_name, LO_NAME_SIZE); 1797863d5b82SDavid Howells else 1798863d5b82SDavid Howells memcpy(info.lo_name, info64->lo_file_name, LO_NAME_SIZE); 1799863d5b82SDavid Howells memcpy(info.lo_encrypt_key, info64->lo_encrypt_key, LO_KEY_SIZE); 1800863d5b82SDavid Howells 1801863d5b82SDavid Howells /* error in case values were truncated */ 1802863d5b82SDavid Howells if (info.lo_device != info64->lo_device || 1803863d5b82SDavid Howells info.lo_rdevice != info64->lo_rdevice || 1804863d5b82SDavid Howells info.lo_inode != info64->lo_inode || 1805863d5b82SDavid Howells info.lo_offset != info64->lo_offset || 1806863d5b82SDavid Howells info.lo_init[0] != info64->lo_init[0] || 1807863d5b82SDavid Howells info.lo_init[1] != info64->lo_init[1]) 1808863d5b82SDavid Howells return -EOVERFLOW; 1809863d5b82SDavid Howells 1810863d5b82SDavid Howells if (copy_to_user(arg, &info, sizeof(info))) 1811863d5b82SDavid Howells return -EFAULT; 1812863d5b82SDavid Howells return 0; 1813863d5b82SDavid Howells } 1814863d5b82SDavid Howells 1815863d5b82SDavid Howells static int 1816863d5b82SDavid Howells loop_set_status_compat(struct loop_device *lo, 1817863d5b82SDavid Howells const struct compat_loop_info __user *arg) 1818863d5b82SDavid Howells { 1819863d5b82SDavid Howells struct loop_info64 info64; 1820863d5b82SDavid Howells int ret; 1821863d5b82SDavid Howells 1822863d5b82SDavid Howells ret = loop_info64_from_compat(arg, &info64); 1823863d5b82SDavid Howells if (ret < 0) 1824863d5b82SDavid Howells return ret; 1825863d5b82SDavid Howells return loop_set_status(lo, &info64); 1826863d5b82SDavid Howells } 1827863d5b82SDavid Howells 1828863d5b82SDavid Howells static int 1829863d5b82SDavid Howells loop_get_status_compat(struct loop_device *lo, 1830863d5b82SDavid Howells struct compat_loop_info __user *arg) 1831863d5b82SDavid Howells { 1832863d5b82SDavid Howells struct loop_info64 info64; 1833bdac616dSOmar Sandoval int err; 1834863d5b82SDavid Howells 18354a5ce9baSJan Kara if (!arg) 1836bdac616dSOmar Sandoval return -EINVAL; 1837863d5b82SDavid Howells err = loop_get_status(lo, &info64); 1838863d5b82SDavid Howells if (!err) 1839863d5b82SDavid Howells err = loop_info64_to_compat(&info64, arg); 1840863d5b82SDavid Howells return err; 1841863d5b82SDavid Howells } 1842863d5b82SDavid Howells 1843bb214884SAl Viro static int lo_compat_ioctl(struct block_device *bdev, fmode_t mode, 1844bb214884SAl Viro unsigned int cmd, unsigned long arg) 1845863d5b82SDavid Howells { 1846bb214884SAl Viro struct loop_device *lo = bdev->bd_disk->private_data; 1847863d5b82SDavid Howells int err; 1848863d5b82SDavid Howells 1849863d5b82SDavid Howells switch(cmd) { 1850863d5b82SDavid Howells case LOOP_SET_STATUS: 18513148ffbdSOmar Sandoval err = loop_set_status_compat(lo, 18523148ffbdSOmar Sandoval (const struct compat_loop_info __user *)arg); 1853863d5b82SDavid Howells break; 1854863d5b82SDavid Howells case LOOP_GET_STATUS: 18553148ffbdSOmar Sandoval err = loop_get_status_compat(lo, 18563148ffbdSOmar Sandoval (struct compat_loop_info __user *)arg); 1857863d5b82SDavid Howells break; 185853d66608SJ. R. Okajima case LOOP_SET_CAPACITY: 1859863d5b82SDavid Howells case LOOP_CLR_FD: 1860863d5b82SDavid Howells case LOOP_GET_STATUS64: 1861863d5b82SDavid Howells case LOOP_SET_STATUS64: 18623448914eSMartijn Coenen case LOOP_CONFIGURE: 1863863d5b82SDavid Howells arg = (unsigned long) compat_ptr(arg); 1864df561f66SGustavo A. R. Silva fallthrough; 1865863d5b82SDavid Howells case LOOP_SET_FD: 1866863d5b82SDavid Howells case LOOP_CHANGE_FD: 18679fea4b39SEvan Green case LOOP_SET_BLOCK_SIZE: 1868fdbe4eeeSAlessio Balsini case LOOP_SET_DIRECT_IO: 1869bb214884SAl Viro err = lo_ioctl(bdev, mode, cmd, arg); 1870863d5b82SDavid Howells break; 1871863d5b82SDavid Howells default: 1872863d5b82SDavid Howells err = -ENOIOCTLCMD; 1873863d5b82SDavid Howells break; 1874863d5b82SDavid Howells } 1875863d5b82SDavid Howells return err; 1876863d5b82SDavid Howells } 1877863d5b82SDavid Howells #endif 1878863d5b82SDavid Howells 1879bb214884SAl Viro static int lo_open(struct block_device *bdev, fmode_t mode) 18801da177e4SLinus Torvalds { 1881770fe30aSKay Sievers struct loop_device *lo; 18820a42e99bSJan Kara int err; 1883770fe30aSKay Sievers 18846cc8e743SPavel Tatashin /* 18856cc8e743SPavel Tatashin * take loop_ctl_mutex to protect lo pointer from race with 18866cc8e743SPavel Tatashin * loop_control_ioctl(LOOP_CTL_REMOVE), however, to reduce contention 18876cc8e743SPavel Tatashin * release it prior to updating lo->lo_refcnt. 18886cc8e743SPavel Tatashin */ 18890a42e99bSJan Kara err = mutex_lock_killable(&loop_ctl_mutex); 18900a42e99bSJan Kara if (err) 18910a42e99bSJan Kara return err; 1892770fe30aSKay Sievers lo = bdev->bd_disk->private_data; 1893770fe30aSKay Sievers if (!lo) { 18940a42e99bSJan Kara mutex_unlock(&loop_ctl_mutex); 18956cc8e743SPavel Tatashin return -ENXIO; 18966cc8e743SPavel Tatashin } 18976cc8e743SPavel Tatashin err = mutex_lock_killable(&lo->lo_mutex); 18986cc8e743SPavel Tatashin mutex_unlock(&loop_ctl_mutex); 18996cc8e743SPavel Tatashin if (err) 1900770fe30aSKay Sievers return err; 19016cc8e743SPavel Tatashin atomic_inc(&lo->lo_refcnt); 19026cc8e743SPavel Tatashin mutex_unlock(&lo->lo_mutex); 19036cc8e743SPavel Tatashin return 0; 19041da177e4SLinus Torvalds } 19051da177e4SLinus Torvalds 1906967d1dc1SJan Kara static void lo_release(struct gendisk *disk, fmode_t mode) 19071da177e4SLinus Torvalds { 19086cc8e743SPavel Tatashin struct loop_device *lo = disk->private_data; 19091da177e4SLinus Torvalds 19106cc8e743SPavel Tatashin mutex_lock(&lo->lo_mutex); 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; 19186cc8e743SPavel Tatashin mutex_unlock(&lo->lo_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: 19356cc8e743SPavel Tatashin mutex_unlock(&lo->lo_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 19746cc8e743SPavel Tatashin mutex_lock(&lo->lo_mutex); 197534dd82afSKay Sievers if (lo->lo_encryption == xfer) 197634dd82afSKay Sievers loop_release_xfer(lo); 19776cc8e743SPavel Tatashin mutex_unlock(&lo->lo_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); 21636cc8e743SPavel Tatashin mutex_init(&lo->lo_mutex); 21641da177e4SLinus Torvalds lo->lo_number = i; 21651da177e4SLinus Torvalds spin_lock_init(&lo->lo_lock); 21661da177e4SLinus Torvalds disk->major = LOOP_MAJOR; 2167476a4813SLaurent Vivier disk->first_minor = i << part_shift; 21681da177e4SLinus Torvalds disk->fops = &lo_fops; 21691da177e4SLinus Torvalds disk->private_data = lo; 21701da177e4SLinus Torvalds disk->queue = lo->lo_queue; 217173285082SKen Chen sprintf(disk->disk_name, "loop%d", i); 217234dd82afSKay Sievers add_disk(disk); 217334dd82afSKay Sievers *l = lo; 217434dd82afSKay Sievers return lo->lo_number; 217573285082SKen Chen 217673285082SKen Chen out_free_queue: 217773285082SKen Chen blk_cleanup_queue(lo->lo_queue); 2178b5dd2f60SMing Lei out_cleanup_tags: 2179b5dd2f60SMing Lei blk_mq_free_tag_set(&lo->tag_set); 21803ec981e3SMikulas Patocka out_free_idr: 21813ec981e3SMikulas Patocka idr_remove(&loop_index_idr, i); 218273285082SKen Chen out_free_dev: 218373285082SKen Chen kfree(lo); 218473285082SKen Chen out: 218534dd82afSKay Sievers return err; 21861da177e4SLinus Torvalds } 21871da177e4SLinus Torvalds 218834dd82afSKay Sievers static void loop_remove(struct loop_device *lo) 218973285082SKen Chen { 21906cd18e71SNeilBrown del_gendisk(lo->lo_disk); 21910fa8ebddSBart Van Assche blk_cleanup_queue(lo->lo_queue); 2192b5dd2f60SMing Lei blk_mq_free_tag_set(&lo->tag_set); 219373285082SKen Chen put_disk(lo->lo_disk); 21946cc8e743SPavel Tatashin mutex_destroy(&lo->lo_mutex); 219573285082SKen Chen kfree(lo); 219673285082SKen Chen } 219773285082SKen Chen 2198770fe30aSKay Sievers static int find_free_cb(int id, void *ptr, void *data) 2199770fe30aSKay Sievers { 2200770fe30aSKay Sievers struct loop_device *lo = ptr; 2201770fe30aSKay Sievers struct loop_device **l = data; 2202770fe30aSKay Sievers 2203770fe30aSKay Sievers if (lo->lo_state == Lo_unbound) { 2204770fe30aSKay Sievers *l = lo; 2205770fe30aSKay Sievers return 1; 2206770fe30aSKay Sievers } 2207770fe30aSKay Sievers return 0; 2208770fe30aSKay Sievers } 2209770fe30aSKay Sievers 221034dd82afSKay Sievers static int loop_lookup(struct loop_device **l, int i) 2211a47653fcSKen Chen { 2212a47653fcSKen Chen struct loop_device *lo; 221334dd82afSKay Sievers int ret = -ENODEV; 2214a47653fcSKen Chen 2215770fe30aSKay Sievers if (i < 0) { 2216770fe30aSKay Sievers int err; 2217770fe30aSKay Sievers 2218770fe30aSKay Sievers err = idr_for_each(&loop_index_idr, &find_free_cb, &lo); 2219770fe30aSKay Sievers if (err == 1) { 2220770fe30aSKay Sievers *l = lo; 2221770fe30aSKay Sievers ret = lo->lo_number; 2222770fe30aSKay Sievers } 2223770fe30aSKay Sievers goto out; 2224a47653fcSKen Chen } 2225a47653fcSKen Chen 2226770fe30aSKay Sievers /* lookup and return a specific i */ 222734dd82afSKay Sievers lo = idr_find(&loop_index_idr, i); 2228a47653fcSKen Chen if (lo) { 222934dd82afSKay Sievers *l = lo; 223034dd82afSKay Sievers ret = lo->lo_number; 2231a47653fcSKen Chen } 2232770fe30aSKay Sievers out: 223334dd82afSKay Sievers return ret; 2234a47653fcSKen Chen } 2235a47653fcSKen Chen 22368410d38cSChristoph Hellwig static void loop_probe(dev_t dev) 223773285082SKen Chen { 22388410d38cSChristoph Hellwig int idx = MINOR(dev) >> part_shift; 2239705962ccSAl Viro struct loop_device *lo; 22408410d38cSChristoph Hellwig 22418410d38cSChristoph Hellwig if (max_loop && idx >= max_loop) 22428410d38cSChristoph Hellwig return; 224373285082SKen Chen 22440a42e99bSJan Kara mutex_lock(&loop_ctl_mutex); 22458410d38cSChristoph Hellwig if (loop_lookup(&lo, idx) < 0) 22468410d38cSChristoph Hellwig loop_add(&lo, idx); 22470a42e99bSJan Kara mutex_unlock(&loop_ctl_mutex); 224873285082SKen Chen } 224973285082SKen Chen 2250770fe30aSKay Sievers static long loop_control_ioctl(struct file *file, unsigned int cmd, 2251770fe30aSKay Sievers unsigned long parm) 2252770fe30aSKay Sievers { 2253770fe30aSKay Sievers struct loop_device *lo; 22540a42e99bSJan Kara int ret; 2255770fe30aSKay Sievers 22560a42e99bSJan Kara ret = mutex_lock_killable(&loop_ctl_mutex); 22570a42e99bSJan Kara if (ret) 22580a42e99bSJan Kara return ret; 22590a42e99bSJan Kara 22600a42e99bSJan Kara ret = -ENOSYS; 2261770fe30aSKay Sievers switch (cmd) { 2262770fe30aSKay Sievers case LOOP_CTL_ADD: 2263770fe30aSKay Sievers ret = loop_lookup(&lo, parm); 2264770fe30aSKay Sievers if (ret >= 0) { 2265770fe30aSKay Sievers ret = -EEXIST; 2266770fe30aSKay Sievers break; 2267770fe30aSKay Sievers } 2268770fe30aSKay Sievers ret = loop_add(&lo, parm); 2269770fe30aSKay Sievers break; 2270770fe30aSKay Sievers case LOOP_CTL_REMOVE: 2271770fe30aSKay Sievers ret = loop_lookup(&lo, parm); 2272770fe30aSKay Sievers if (ret < 0) 2273770fe30aSKay Sievers break; 22746cc8e743SPavel Tatashin ret = mutex_lock_killable(&lo->lo_mutex); 22756cc8e743SPavel Tatashin if (ret) 22766cc8e743SPavel Tatashin break; 2277770fe30aSKay Sievers if (lo->lo_state != Lo_unbound) { 2278770fe30aSKay Sievers ret = -EBUSY; 22796cc8e743SPavel Tatashin mutex_unlock(&lo->lo_mutex); 2280770fe30aSKay Sievers break; 2281770fe30aSKay Sievers } 2282f8933667SMing Lei if (atomic_read(&lo->lo_refcnt) > 0) { 2283770fe30aSKay Sievers ret = -EBUSY; 22846cc8e743SPavel Tatashin mutex_unlock(&lo->lo_mutex); 2285770fe30aSKay Sievers break; 2286770fe30aSKay Sievers } 2287770fe30aSKay Sievers lo->lo_disk->private_data = NULL; 22886cc8e743SPavel Tatashin mutex_unlock(&lo->lo_mutex); 2289770fe30aSKay Sievers idr_remove(&loop_index_idr, lo->lo_number); 2290770fe30aSKay Sievers loop_remove(lo); 2291770fe30aSKay Sievers break; 2292770fe30aSKay Sievers case LOOP_CTL_GET_FREE: 2293770fe30aSKay Sievers ret = loop_lookup(&lo, -1); 2294770fe30aSKay Sievers if (ret >= 0) 2295770fe30aSKay Sievers break; 2296770fe30aSKay Sievers ret = loop_add(&lo, -1); 2297770fe30aSKay Sievers } 22980a42e99bSJan Kara mutex_unlock(&loop_ctl_mutex); 2299770fe30aSKay Sievers 2300770fe30aSKay Sievers return ret; 2301770fe30aSKay Sievers } 2302770fe30aSKay Sievers 2303770fe30aSKay Sievers static const struct file_operations loop_ctl_fops = { 2304770fe30aSKay Sievers .open = nonseekable_open, 2305770fe30aSKay Sievers .unlocked_ioctl = loop_control_ioctl, 2306770fe30aSKay Sievers .compat_ioctl = loop_control_ioctl, 2307770fe30aSKay Sievers .owner = THIS_MODULE, 2308770fe30aSKay Sievers .llseek = noop_llseek, 2309770fe30aSKay Sievers }; 2310770fe30aSKay Sievers 2311770fe30aSKay Sievers static struct miscdevice loop_misc = { 2312770fe30aSKay Sievers .minor = LOOP_CTRL_MINOR, 2313770fe30aSKay Sievers .name = "loop-control", 2314770fe30aSKay Sievers .fops = &loop_ctl_fops, 2315770fe30aSKay Sievers }; 2316770fe30aSKay Sievers 2317770fe30aSKay Sievers MODULE_ALIAS_MISCDEV(LOOP_CTRL_MINOR); 2318770fe30aSKay Sievers MODULE_ALIAS("devname:loop-control"); 2319770fe30aSKay Sievers 232073285082SKen Chen static int __init loop_init(void) 232173285082SKen Chen { 2322a47653fcSKen Chen int i, nr; 232334dd82afSKay Sievers struct loop_device *lo; 2324770fe30aSKay Sievers int err; 2325a47653fcSKen Chen 2326476a4813SLaurent Vivier part_shift = 0; 2327ac04fee0SNamhyung Kim if (max_part > 0) { 2328476a4813SLaurent Vivier part_shift = fls(max_part); 2329476a4813SLaurent Vivier 2330ac04fee0SNamhyung Kim /* 2331ac04fee0SNamhyung Kim * Adjust max_part according to part_shift as it is exported 2332ac04fee0SNamhyung Kim * to user space so that user can decide correct minor number 2333ac04fee0SNamhyung Kim * if [s]he want to create more devices. 2334ac04fee0SNamhyung Kim * 2335ac04fee0SNamhyung Kim * Note that -1 is required because partition 0 is reserved 2336ac04fee0SNamhyung Kim * for the whole disk. 2337ac04fee0SNamhyung Kim */ 2338ac04fee0SNamhyung Kim max_part = (1UL << part_shift) - 1; 2339ac04fee0SNamhyung Kim } 2340ac04fee0SNamhyung Kim 2341b1a66504SGuo Chao if ((1UL << part_shift) > DISK_MAX_PARTS) { 2342b1a66504SGuo Chao err = -EINVAL; 2343a8c1d064SAnton Volkov goto err_out; 2344b1a66504SGuo Chao } 234578f4bb36SNamhyung Kim 2346b1a66504SGuo Chao if (max_loop > 1UL << (MINORBITS - part_shift)) { 2347b1a66504SGuo Chao err = -EINVAL; 2348a8c1d064SAnton Volkov goto err_out; 2349b1a66504SGuo Chao } 235073285082SKen Chen 2351d134b00bSKay Sievers /* 2352d134b00bSKay Sievers * If max_loop is specified, create that many devices upfront. 2353d134b00bSKay Sievers * This also becomes a hard limit. If max_loop is not specified, 2354d134b00bSKay Sievers * create CONFIG_BLK_DEV_LOOP_MIN_COUNT loop devices at module 2355d134b00bSKay Sievers * init time. Loop devices can be requested on-demand with the 2356d134b00bSKay Sievers * /dev/loop-control interface, or be instantiated by accessing 2357d134b00bSKay Sievers * a 'dead' device node. 2358d134b00bSKay Sievers */ 2359aeb2b0b1SLukas Bulwahn if (max_loop) 2360a47653fcSKen Chen nr = max_loop; 2361aeb2b0b1SLukas Bulwahn else 2362d134b00bSKay Sievers nr = CONFIG_BLK_DEV_LOOP_MIN_COUNT; 2363a47653fcSKen Chen 2364a8c1d064SAnton Volkov err = misc_register(&loop_misc); 2365a8c1d064SAnton Volkov if (err < 0) 2366a8c1d064SAnton Volkov goto err_out; 2367a8c1d064SAnton Volkov 2368a8c1d064SAnton Volkov 23698410d38cSChristoph Hellwig if (__register_blkdev(LOOP_MAJOR, "loop", loop_probe)) { 2370b1a66504SGuo Chao err = -EIO; 2371b1a66504SGuo Chao goto misc_out; 2372b1a66504SGuo Chao } 2373a47653fcSKen Chen 2374d134b00bSKay Sievers /* pre-create number of devices given by config or max_loop */ 23750a42e99bSJan Kara mutex_lock(&loop_ctl_mutex); 237634dd82afSKay Sievers for (i = 0; i < nr; i++) 237734dd82afSKay Sievers loop_add(&lo, i); 23780a42e99bSJan Kara mutex_unlock(&loop_ctl_mutex); 237934dd82afSKay Sievers 238073285082SKen Chen printk(KERN_INFO "loop: module loaded\n"); 23811da177e4SLinus Torvalds return 0; 2382b1a66504SGuo Chao 2383b1a66504SGuo Chao misc_out: 2384b1a66504SGuo Chao misc_deregister(&loop_misc); 2385a8c1d064SAnton Volkov err_out: 2386b1a66504SGuo Chao return err; 238734dd82afSKay Sievers } 2388a47653fcSKen Chen 238934dd82afSKay Sievers static int loop_exit_cb(int id, void *ptr, void *data) 239034dd82afSKay Sievers { 239134dd82afSKay Sievers struct loop_device *lo = ptr; 2392a47653fcSKen Chen 239334dd82afSKay Sievers loop_remove(lo); 239434dd82afSKay Sievers return 0; 23951da177e4SLinus Torvalds } 23961da177e4SLinus Torvalds 239773285082SKen Chen static void __exit loop_exit(void) 23981da177e4SLinus Torvalds { 2399200f9337SLuis Chamberlain mutex_lock(&loop_ctl_mutex); 2400200f9337SLuis Chamberlain 240134dd82afSKay Sievers idr_for_each(&loop_index_idr, &loop_exit_cb, NULL); 240234dd82afSKay Sievers idr_destroy(&loop_index_idr); 240373285082SKen Chen 240400d59405SAkinobu Mita unregister_blkdev(LOOP_MAJOR, "loop"); 2405770fe30aSKay Sievers 2406770fe30aSKay Sievers misc_deregister(&loop_misc); 2407200f9337SLuis Chamberlain 2408200f9337SLuis Chamberlain mutex_unlock(&loop_ctl_mutex); 24091da177e4SLinus Torvalds } 24101da177e4SLinus Torvalds 24111da177e4SLinus Torvalds module_init(loop_init); 24121da177e4SLinus Torvalds module_exit(loop_exit); 24131da177e4SLinus Torvalds 24141da177e4SLinus Torvalds #ifndef MODULE 24151da177e4SLinus Torvalds static int __init max_loop_setup(char *str) 24161da177e4SLinus Torvalds { 24171da177e4SLinus Torvalds max_loop = simple_strtol(str, NULL, 0); 24181da177e4SLinus Torvalds return 1; 24191da177e4SLinus Torvalds } 24201da177e4SLinus Torvalds 24211da177e4SLinus Torvalds __setup("max_loop=", max_loop_setup); 24221da177e4SLinus Torvalds #endif 2423