11da177e4SLinus Torvalds /* 21da177e4SLinus Torvalds * linux/drivers/block/loop.c 31da177e4SLinus Torvalds * 41da177e4SLinus Torvalds * Written by Theodore Ts'o, 3/29/93 51da177e4SLinus Torvalds * 61da177e4SLinus Torvalds * Copyright 1993 by Theodore Ts'o. Redistribution of this file is 71da177e4SLinus Torvalds * permitted under the GNU General Public License. 81da177e4SLinus Torvalds * 91da177e4SLinus Torvalds * DES encryption plus some minor changes by Werner Almesberger, 30-MAY-1993 101da177e4SLinus Torvalds * more DES encryption plus IDEA encryption by Nicholas J. Leon, June 20, 1996 111da177e4SLinus Torvalds * 121da177e4SLinus Torvalds * Modularized and updated for 1.1.16 kernel - Mitch Dsouza 28th May 1994 131da177e4SLinus Torvalds * Adapted for 1.3.59 kernel - Andries Brouwer, 1 Feb 1996 141da177e4SLinus Torvalds * 151da177e4SLinus Torvalds * Fixed do_loop_request() re-entrancy - Vincent.Renardias@waw.com Mar 20, 1997 161da177e4SLinus Torvalds * 171da177e4SLinus Torvalds * Added devfs support - Richard Gooch <rgooch@atnf.csiro.au> 16-Jan-1998 181da177e4SLinus Torvalds * 191da177e4SLinus Torvalds * Handle sparse backing files correctly - Kenn Humborg, Jun 28, 1998 201da177e4SLinus Torvalds * 211da177e4SLinus Torvalds * Loadable modules and other fixes by AK, 1998 221da177e4SLinus Torvalds * 231da177e4SLinus Torvalds * Make real block number available to downstream transfer functions, enables 241da177e4SLinus Torvalds * CBC (and relatives) mode encryption requiring unique IVs per data block. 251da177e4SLinus Torvalds * Reed H. Petty, rhp@draper.net 261da177e4SLinus Torvalds * 271da177e4SLinus Torvalds * Maximum number of loop devices now dynamic via max_loop module parameter. 281da177e4SLinus Torvalds * Russell Kroll <rkroll@exploits.org> 19990701 291da177e4SLinus Torvalds * 301da177e4SLinus Torvalds * Maximum number of loop devices when compiled-in now selectable by passing 311da177e4SLinus Torvalds * max_loop=<1-255> to the kernel on boot. 3296de0e25SJan Engelhardt * Erik I. Bolsø, <eriki@himolde.no>, Oct 31, 1999 331da177e4SLinus Torvalds * 341da177e4SLinus Torvalds * Completely rewrite request handling to be make_request_fn style and 351da177e4SLinus Torvalds * non blocking, pushing work to a helper thread. Lots of fixes from 361da177e4SLinus Torvalds * Al Viro too. 371da177e4SLinus Torvalds * Jens Axboe <axboe@suse.de>, Nov 2000 381da177e4SLinus Torvalds * 391da177e4SLinus Torvalds * Support up to 256 loop devices 401da177e4SLinus Torvalds * Heinz Mauelshagen <mge@sistina.com>, Feb 2002 411da177e4SLinus Torvalds * 421da177e4SLinus Torvalds * Support for falling back on the write file operation when the address space 434e02ed4bSNick Piggin * operations write_begin is not available on the backing filesystem. 441da177e4SLinus Torvalds * Anton Altaparmakov, 16 Feb 2005 451da177e4SLinus Torvalds * 461da177e4SLinus Torvalds * Still To Fix: 471da177e4SLinus Torvalds * - Advisory locking is ignored here. 481da177e4SLinus Torvalds * - Should use an own CAP_* category instead of CAP_SYS_ADMIN 491da177e4SLinus Torvalds * 501da177e4SLinus Torvalds */ 511da177e4SLinus Torvalds 521da177e4SLinus Torvalds #include <linux/module.h> 531da177e4SLinus Torvalds #include <linux/moduleparam.h> 541da177e4SLinus Torvalds #include <linux/sched.h> 551da177e4SLinus Torvalds #include <linux/fs.h> 564ee60ec1SMatthew Wilcox (Oracle) #include <linux/pagemap.h> 571da177e4SLinus Torvalds #include <linux/file.h> 581da177e4SLinus Torvalds #include <linux/stat.h> 591da177e4SLinus Torvalds #include <linux/errno.h> 601da177e4SLinus Torvalds #include <linux/major.h> 611da177e4SLinus Torvalds #include <linux/wait.h> 621da177e4SLinus Torvalds #include <linux/blkdev.h> 631da177e4SLinus Torvalds #include <linux/blkpg.h> 641da177e4SLinus Torvalds #include <linux/init.h> 651da177e4SLinus Torvalds #include <linux/swap.h> 661da177e4SLinus Torvalds #include <linux/slab.h> 67863d5b82SDavid Howells #include <linux/compat.h> 681da177e4SLinus Torvalds #include <linux/suspend.h> 6983144186SRafael J. Wysocki #include <linux/freezer.h> 702a48fc0aSArnd Bergmann #include <linux/mutex.h> 711da177e4SLinus Torvalds #include <linux/writeback.h> 721da177e4SLinus Torvalds #include <linux/completion.h> 731da177e4SLinus Torvalds #include <linux/highmem.h> 746c997918SSerge E. Hallyn #include <linux/kthread.h> 75d6b29d7cSJens Axboe #include <linux/splice.h> 76ee862730SMilan Broz #include <linux/sysfs.h> 77770fe30aSKay Sievers #include <linux/miscdevice.h> 78dfaa2ef6SLukas Czerner #include <linux/falloc.h> 79283e7e5dSAl Viro #include <linux/uio.h> 80d9a08a9eSAdam Manzanares #include <linux/ioprio.h> 81db6638d7SDennis Zhou #include <linux/blk-cgroup.h> 82d9a08a9eSAdam Manzanares 8383a87611SAl Viro #include "loop.h" 841da177e4SLinus Torvalds 857c0f6ba6SLinus Torvalds #include <linux/uaccess.h> 861da177e4SLinus Torvalds 8734dd82afSKay Sievers static DEFINE_IDR(loop_index_idr); 88310ca162STetsuo Handa static DEFINE_MUTEX(loop_ctl_mutex); 891da177e4SLinus Torvalds 90476a4813SLaurent Vivier static int max_part; 91476a4813SLaurent Vivier static int part_shift; 92476a4813SLaurent Vivier 931da177e4SLinus Torvalds static int transfer_xor(struct loop_device *lo, int cmd, 941da177e4SLinus Torvalds struct page *raw_page, unsigned raw_off, 951da177e4SLinus Torvalds struct page *loop_page, unsigned loop_off, 961da177e4SLinus Torvalds int size, sector_t real_block) 971da177e4SLinus Torvalds { 98cfd8005cSCong Wang char *raw_buf = kmap_atomic(raw_page) + raw_off; 99cfd8005cSCong Wang char *loop_buf = kmap_atomic(loop_page) + loop_off; 1001da177e4SLinus Torvalds char *in, *out, *key; 1011da177e4SLinus Torvalds int i, keysize; 1021da177e4SLinus Torvalds 1031da177e4SLinus Torvalds if (cmd == READ) { 1041da177e4SLinus Torvalds in = raw_buf; 1051da177e4SLinus Torvalds out = loop_buf; 1061da177e4SLinus Torvalds } else { 1071da177e4SLinus Torvalds in = loop_buf; 1081da177e4SLinus Torvalds out = raw_buf; 1091da177e4SLinus Torvalds } 1101da177e4SLinus Torvalds 1111da177e4SLinus Torvalds key = lo->lo_encrypt_key; 1121da177e4SLinus Torvalds keysize = lo->lo_encrypt_key_size; 1131da177e4SLinus Torvalds for (i = 0; i < size; i++) 1141da177e4SLinus Torvalds *out++ = *in++ ^ key[(i & 511) % keysize]; 1151da177e4SLinus Torvalds 116cfd8005cSCong Wang kunmap_atomic(loop_buf); 117cfd8005cSCong Wang kunmap_atomic(raw_buf); 1181da177e4SLinus Torvalds cond_resched(); 1191da177e4SLinus Torvalds return 0; 1201da177e4SLinus Torvalds } 1211da177e4SLinus Torvalds 1221da177e4SLinus Torvalds static int xor_init(struct loop_device *lo, const struct loop_info64 *info) 1231da177e4SLinus Torvalds { 1241da177e4SLinus Torvalds if (unlikely(info->lo_encrypt_key_size <= 0)) 1251da177e4SLinus Torvalds return -EINVAL; 1261da177e4SLinus Torvalds return 0; 1271da177e4SLinus Torvalds } 1281da177e4SLinus Torvalds 1291da177e4SLinus Torvalds static struct loop_func_table none_funcs = { 1301da177e4SLinus Torvalds .number = LO_CRYPT_NONE, 1311da177e4SLinus Torvalds }; 1321da177e4SLinus Torvalds 1331da177e4SLinus Torvalds static struct loop_func_table xor_funcs = { 1341da177e4SLinus Torvalds .number = LO_CRYPT_XOR, 1351da177e4SLinus Torvalds .transfer = transfer_xor, 1361da177e4SLinus Torvalds .init = xor_init 1371da177e4SLinus Torvalds }; 1381da177e4SLinus Torvalds 1391da177e4SLinus Torvalds /* xfer_funcs[0] is special - its release function is never called */ 1401da177e4SLinus Torvalds static struct loop_func_table *xfer_funcs[MAX_LO_CRYPT] = { 1411da177e4SLinus Torvalds &none_funcs, 1421da177e4SLinus Torvalds &xor_funcs 1431da177e4SLinus Torvalds }; 1441da177e4SLinus Torvalds 1457035b5dfSDmitry Monakhov static loff_t get_size(loff_t offset, loff_t sizelimit, struct file *file) 1461da177e4SLinus Torvalds { 147b7a1da69SGuo Chao loff_t loopsize; 1481da177e4SLinus Torvalds 1491da177e4SLinus Torvalds /* Compute loopsize in bytes */ 150b7a1da69SGuo Chao loopsize = i_size_read(file->f_mapping->host); 151b7a1da69SGuo Chao if (offset > 0) 152b7a1da69SGuo Chao loopsize -= offset; 153b7a1da69SGuo Chao /* offset is beyond i_size, weird but possible */ 1547035b5dfSDmitry Monakhov if (loopsize < 0) 1557035b5dfSDmitry Monakhov return 0; 1561da177e4SLinus Torvalds 1577035b5dfSDmitry Monakhov if (sizelimit > 0 && sizelimit < loopsize) 1587035b5dfSDmitry Monakhov loopsize = sizelimit; 1591da177e4SLinus Torvalds /* 1601da177e4SLinus Torvalds * Unfortunately, if we want to do I/O on the device, 1611da177e4SLinus Torvalds * the number of 512-byte sectors has to fit into a sector_t. 1621da177e4SLinus Torvalds */ 1631da177e4SLinus Torvalds return loopsize >> 9; 1641da177e4SLinus Torvalds } 1651da177e4SLinus Torvalds 1667035b5dfSDmitry Monakhov static loff_t get_loop_size(struct loop_device *lo, struct file *file) 1671da177e4SLinus Torvalds { 1687035b5dfSDmitry Monakhov return get_size(lo->lo_offset, lo->lo_sizelimit, file); 1697035b5dfSDmitry Monakhov } 1707035b5dfSDmitry Monakhov 1712e5ab5f3SMing Lei static void __loop_update_dio(struct loop_device *lo, bool dio) 1722e5ab5f3SMing Lei { 1732e5ab5f3SMing Lei struct file *file = lo->lo_backing_file; 1742e5ab5f3SMing Lei struct address_space *mapping = file->f_mapping; 1752e5ab5f3SMing Lei struct inode *inode = mapping->host; 1762e5ab5f3SMing Lei unsigned short sb_bsize = 0; 1772e5ab5f3SMing Lei unsigned dio_align = 0; 1782e5ab5f3SMing Lei bool use_dio; 1792e5ab5f3SMing Lei 1802e5ab5f3SMing Lei if (inode->i_sb->s_bdev) { 1812e5ab5f3SMing Lei sb_bsize = bdev_logical_block_size(inode->i_sb->s_bdev); 1822e5ab5f3SMing Lei dio_align = sb_bsize - 1; 1832e5ab5f3SMing Lei } 1842e5ab5f3SMing Lei 1852e5ab5f3SMing Lei /* 1862e5ab5f3SMing Lei * We support direct I/O only if lo_offset is aligned with the 1872e5ab5f3SMing Lei * logical I/O size of backing device, and the logical block 1882e5ab5f3SMing Lei * size of loop is bigger than the backing device's and the loop 1892e5ab5f3SMing Lei * needn't transform transfer. 1902e5ab5f3SMing Lei * 1912e5ab5f3SMing Lei * TODO: the above condition may be loosed in the future, and 1922e5ab5f3SMing Lei * direct I/O may be switched runtime at that time because most 19389d790abSMasahiro Yamada * of requests in sane applications should be PAGE_SIZE aligned 1942e5ab5f3SMing Lei */ 1952e5ab5f3SMing Lei if (dio) { 1962e5ab5f3SMing Lei if (queue_logical_block_size(lo->lo_queue) >= sb_bsize && 1972e5ab5f3SMing Lei !(lo->lo_offset & dio_align) && 1982e5ab5f3SMing Lei mapping->a_ops->direct_IO && 1992e5ab5f3SMing Lei !lo->transfer) 2002e5ab5f3SMing Lei use_dio = true; 2012e5ab5f3SMing Lei else 2022e5ab5f3SMing Lei use_dio = false; 2032e5ab5f3SMing Lei } else { 2042e5ab5f3SMing Lei use_dio = false; 2052e5ab5f3SMing Lei } 2062e5ab5f3SMing Lei 2072e5ab5f3SMing Lei if (lo->use_dio == use_dio) 2082e5ab5f3SMing Lei return; 2092e5ab5f3SMing Lei 2102e5ab5f3SMing Lei /* flush dirty pages before changing direct IO */ 2112e5ab5f3SMing Lei vfs_fsync(file, 0); 2122e5ab5f3SMing Lei 2132e5ab5f3SMing Lei /* 2142e5ab5f3SMing Lei * The flag of LO_FLAGS_DIRECT_IO is handled similarly with 2152e5ab5f3SMing Lei * LO_FLAGS_READ_ONLY, both are set from kernel, and losetup 2162e5ab5f3SMing Lei * will get updated by ioctl(LOOP_GET_STATUS) 2172e5ab5f3SMing Lei */ 2180fbcf579SMartijn Coenen if (lo->lo_state == Lo_bound) 2192e5ab5f3SMing Lei blk_mq_freeze_queue(lo->lo_queue); 2202e5ab5f3SMing Lei lo->use_dio = use_dio; 22140326d8aSShaohua Li if (use_dio) { 2228b904b5bSBart Van Assche blk_queue_flag_clear(QUEUE_FLAG_NOMERGES, lo->lo_queue); 2232e5ab5f3SMing Lei lo->lo_flags |= LO_FLAGS_DIRECT_IO; 22440326d8aSShaohua Li } else { 2258b904b5bSBart Van Assche blk_queue_flag_set(QUEUE_FLAG_NOMERGES, lo->lo_queue); 2262e5ab5f3SMing Lei lo->lo_flags &= ~LO_FLAGS_DIRECT_IO; 22740326d8aSShaohua Li } 2280fbcf579SMartijn Coenen if (lo->lo_state == Lo_bound) 2292e5ab5f3SMing Lei blk_mq_unfreeze_queue(lo->lo_queue); 2302e5ab5f3SMing Lei } 2312e5ab5f3SMing Lei 2325795b6f5SMartijn Coenen /** 2333448914eSMartijn Coenen * loop_validate_block_size() - validates the passed in block size 2343448914eSMartijn Coenen * @bsize: size to validate 2353448914eSMartijn Coenen */ 2367035b5dfSDmitry Monakhov static int 2373448914eSMartijn Coenen loop_validate_block_size(unsigned short bsize) 2387035b5dfSDmitry Monakhov { 2393448914eSMartijn Coenen if (bsize < 512 || bsize > PAGE_SIZE || !is_power_of_2(bsize)) 2403448914eSMartijn Coenen return -EINVAL; 2413448914eSMartijn Coenen 2423448914eSMartijn Coenen return 0; 2433448914eSMartijn Coenen } 2443448914eSMartijn Coenen 2453448914eSMartijn Coenen /** 2465795b6f5SMartijn Coenen * loop_set_size() - sets device size and notifies userspace 2475795b6f5SMartijn Coenen * @lo: struct loop_device to set the size for 2485795b6f5SMartijn Coenen * @size: new size of the loop device 2495795b6f5SMartijn Coenen * 2505795b6f5SMartijn Coenen * Callers must validate that the size passed into this function fits into 2515795b6f5SMartijn Coenen * a sector_t, eg using loop_validate_size() 2525795b6f5SMartijn Coenen */ 2535795b6f5SMartijn Coenen static void loop_set_size(struct loop_device *lo, loff_t size) 2545795b6f5SMartijn Coenen { 255449f4ec9SChristoph Hellwig if (!set_capacity_and_notify(lo->lo_disk, size)) 2563b4f85d0SChristoph Hellwig kobject_uevent(&disk_to_dev(lo->lo_disk)->kobj, KOBJ_CHANGE); 2571da177e4SLinus Torvalds } 2581da177e4SLinus Torvalds 2591da177e4SLinus Torvalds static inline int 2601da177e4SLinus Torvalds lo_do_transfer(struct loop_device *lo, int cmd, 2611da177e4SLinus Torvalds struct page *rpage, unsigned roffs, 2621da177e4SLinus Torvalds struct page *lpage, unsigned loffs, 2631da177e4SLinus Torvalds int size, sector_t rblock) 2641da177e4SLinus Torvalds { 265aa4d8616SChristoph Hellwig int ret; 266aa4d8616SChristoph Hellwig 267aa4d8616SChristoph Hellwig ret = lo->transfer(lo, cmd, rpage, roffs, lpage, loffs, size, rblock); 268aa4d8616SChristoph Hellwig if (likely(!ret)) 2691da177e4SLinus Torvalds return 0; 2701da177e4SLinus Torvalds 271aa4d8616SChristoph Hellwig printk_ratelimited(KERN_ERR 272aa4d8616SChristoph Hellwig "loop: Transfer error at byte offset %llu, length %i.\n", 273aa4d8616SChristoph Hellwig (unsigned long long)rblock << 9, size); 274aa4d8616SChristoph Hellwig return ret; 2751da177e4SLinus Torvalds } 2761da177e4SLinus Torvalds 277aa4d8616SChristoph Hellwig static int lo_write_bvec(struct file *file, struct bio_vec *bvec, loff_t *ppos) 2781da177e4SLinus Torvalds { 279aa4d8616SChristoph Hellwig struct iov_iter i; 2801da177e4SLinus Torvalds ssize_t bw; 281283e7e5dSAl Viro 282b6207430SChristoph Hellwig iov_iter_bvec(&i, WRITE, bvec, 1, bvec->bv_len); 2831da177e4SLinus Torvalds 28403d95eb2SAl Viro file_start_write(file); 285abbb6589SChristoph Hellwig bw = vfs_iter_write(file, &i, ppos, 0); 28603d95eb2SAl Viro file_end_write(file); 287aa4d8616SChristoph Hellwig 288aa4d8616SChristoph Hellwig if (likely(bw == bvec->bv_len)) 2891da177e4SLinus Torvalds return 0; 290aa4d8616SChristoph Hellwig 291aa4d8616SChristoph Hellwig printk_ratelimited(KERN_ERR 292aa4d8616SChristoph Hellwig "loop: Write error at byte offset %llu, length %i.\n", 293aa4d8616SChristoph Hellwig (unsigned long long)*ppos, bvec->bv_len); 2941da177e4SLinus Torvalds if (bw >= 0) 2951da177e4SLinus Torvalds bw = -EIO; 2961da177e4SLinus Torvalds return bw; 2971da177e4SLinus Torvalds } 2981da177e4SLinus Torvalds 299aa4d8616SChristoph Hellwig static int lo_write_simple(struct loop_device *lo, struct request *rq, 300aa4d8616SChristoph Hellwig loff_t pos) 3011da177e4SLinus Torvalds { 302aa4d8616SChristoph Hellwig struct bio_vec bvec; 303aa4d8616SChristoph Hellwig struct req_iterator iter; 304aa4d8616SChristoph Hellwig int ret = 0; 305aa4d8616SChristoph Hellwig 306aa4d8616SChristoph Hellwig rq_for_each_segment(bvec, rq, iter) { 307aa4d8616SChristoph Hellwig ret = lo_write_bvec(lo->lo_backing_file, &bvec, &pos); 308aa4d8616SChristoph Hellwig if (ret < 0) 309aa4d8616SChristoph Hellwig break; 3101da177e4SLinus Torvalds cond_resched(); 3111da177e4SLinus Torvalds } 3121da177e4SLinus Torvalds 313aa4d8616SChristoph Hellwig return ret; 314aa4d8616SChristoph Hellwig } 315aa4d8616SChristoph Hellwig 316aa4d8616SChristoph Hellwig /* 317456be148SChristoph Hellwig * This is the slow, transforming version that needs to double buffer the 318456be148SChristoph Hellwig * data as it cannot do the transformations in place without having direct 319456be148SChristoph Hellwig * access to the destination pages of the backing file. 3201da177e4SLinus Torvalds */ 321aa4d8616SChristoph Hellwig static int lo_write_transfer(struct loop_device *lo, struct request *rq, 322aa4d8616SChristoph Hellwig loff_t pos) 3231da177e4SLinus Torvalds { 324aa4d8616SChristoph Hellwig struct bio_vec bvec, b; 32530112013SMing Lei struct req_iterator iter; 326aa4d8616SChristoph Hellwig struct page *page; 3277988613bSKent Overstreet int ret = 0; 3281da177e4SLinus Torvalds 329aa4d8616SChristoph Hellwig page = alloc_page(GFP_NOIO); 3301da177e4SLinus Torvalds if (unlikely(!page)) 331aa4d8616SChristoph Hellwig return -ENOMEM; 332456be148SChristoph Hellwig 33330112013SMing Lei rq_for_each_segment(bvec, rq, iter) { 334aa4d8616SChristoph Hellwig ret = lo_do_transfer(lo, WRITE, page, 0, bvec.bv_page, 335aa4d8616SChristoph Hellwig bvec.bv_offset, bvec.bv_len, pos >> 9); 336aa4d8616SChristoph Hellwig if (unlikely(ret)) 337aa4d8616SChristoph Hellwig break; 338aa4d8616SChristoph Hellwig 339aa4d8616SChristoph Hellwig b.bv_page = page; 340aa4d8616SChristoph Hellwig b.bv_offset = 0; 341aa4d8616SChristoph Hellwig b.bv_len = bvec.bv_len; 342aa4d8616SChristoph Hellwig ret = lo_write_bvec(lo->lo_backing_file, &b, &pos); 3431da177e4SLinus Torvalds if (ret < 0) 3441da177e4SLinus Torvalds break; 3451da177e4SLinus Torvalds } 346aa4d8616SChristoph Hellwig 3471da177e4SLinus Torvalds __free_page(page); 3481da177e4SLinus Torvalds return ret; 3491da177e4SLinus Torvalds } 3501da177e4SLinus Torvalds 351aa4d8616SChristoph Hellwig static int lo_read_simple(struct loop_device *lo, struct request *rq, 352aa4d8616SChristoph Hellwig loff_t pos) 3531da177e4SLinus Torvalds { 3547988613bSKent Overstreet struct bio_vec bvec; 35530112013SMing Lei struct req_iterator iter; 356aa4d8616SChristoph Hellwig struct iov_iter i; 357aa4d8616SChristoph Hellwig ssize_t len; 3581da177e4SLinus Torvalds 35930112013SMing Lei rq_for_each_segment(bvec, rq, iter) { 360b6207430SChristoph Hellwig iov_iter_bvec(&i, READ, &bvec, 1, bvec.bv_len); 36118e9710eSChristoph Hellwig len = vfs_iter_read(lo->lo_backing_file, &i, &pos, 0); 362aa4d8616SChristoph Hellwig if (len < 0) 363aa4d8616SChristoph Hellwig return len; 364306df071SDave Young 365aa4d8616SChristoph Hellwig flush_dcache_page(bvec.bv_page); 366aa4d8616SChristoph Hellwig 367aa4d8616SChristoph Hellwig if (len != bvec.bv_len) { 36830112013SMing Lei struct bio *bio; 36930112013SMing Lei 37030112013SMing Lei __rq_for_each_bio(bio, rq) 371306df071SDave Young zero_fill_bio(bio); 3721da177e4SLinus Torvalds break; 373306df071SDave Young } 374aa4d8616SChristoph Hellwig cond_resched(); 3751da177e4SLinus Torvalds } 376aa4d8616SChristoph Hellwig 377306df071SDave Young return 0; 3781da177e4SLinus Torvalds } 3791da177e4SLinus Torvalds 380aa4d8616SChristoph Hellwig static int lo_read_transfer(struct loop_device *lo, struct request *rq, 381aa4d8616SChristoph Hellwig loff_t pos) 382aa4d8616SChristoph Hellwig { 383aa4d8616SChristoph Hellwig struct bio_vec bvec, b; 384aa4d8616SChristoph Hellwig struct req_iterator iter; 385aa4d8616SChristoph Hellwig struct iov_iter i; 386aa4d8616SChristoph Hellwig struct page *page; 387aa4d8616SChristoph Hellwig ssize_t len; 388aa4d8616SChristoph Hellwig int ret = 0; 389aa4d8616SChristoph Hellwig 390aa4d8616SChristoph Hellwig page = alloc_page(GFP_NOIO); 391aa4d8616SChristoph Hellwig if (unlikely(!page)) 392aa4d8616SChristoph Hellwig return -ENOMEM; 393aa4d8616SChristoph Hellwig 394aa4d8616SChristoph Hellwig rq_for_each_segment(bvec, rq, iter) { 395aa4d8616SChristoph Hellwig loff_t offset = pos; 396aa4d8616SChristoph Hellwig 397aa4d8616SChristoph Hellwig b.bv_page = page; 398aa4d8616SChristoph Hellwig b.bv_offset = 0; 399aa4d8616SChristoph Hellwig b.bv_len = bvec.bv_len; 400aa4d8616SChristoph Hellwig 401b6207430SChristoph Hellwig iov_iter_bvec(&i, READ, &b, 1, b.bv_len); 40218e9710eSChristoph Hellwig len = vfs_iter_read(lo->lo_backing_file, &i, &pos, 0); 403aa4d8616SChristoph Hellwig if (len < 0) { 404aa4d8616SChristoph Hellwig ret = len; 405aa4d8616SChristoph Hellwig goto out_free_page; 406aa4d8616SChristoph Hellwig } 407aa4d8616SChristoph Hellwig 408aa4d8616SChristoph Hellwig ret = lo_do_transfer(lo, READ, page, 0, bvec.bv_page, 409aa4d8616SChristoph Hellwig bvec.bv_offset, len, offset >> 9); 410aa4d8616SChristoph Hellwig if (ret) 411aa4d8616SChristoph Hellwig goto out_free_page; 412aa4d8616SChristoph Hellwig 413aa4d8616SChristoph Hellwig flush_dcache_page(bvec.bv_page); 414aa4d8616SChristoph Hellwig 415aa4d8616SChristoph Hellwig if (len != bvec.bv_len) { 416aa4d8616SChristoph Hellwig struct bio *bio; 417aa4d8616SChristoph Hellwig 418aa4d8616SChristoph Hellwig __rq_for_each_bio(bio, rq) 419aa4d8616SChristoph Hellwig zero_fill_bio(bio); 420aa4d8616SChristoph Hellwig break; 421aa4d8616SChristoph Hellwig } 422aa4d8616SChristoph Hellwig } 423aa4d8616SChristoph Hellwig 424aa4d8616SChristoph Hellwig ret = 0; 425aa4d8616SChristoph Hellwig out_free_page: 426aa4d8616SChristoph Hellwig __free_page(page); 427aa4d8616SChristoph Hellwig return ret; 428aa4d8616SChristoph Hellwig } 429aa4d8616SChristoph Hellwig 430efcfec57SDarrick J. Wong static int lo_fallocate(struct loop_device *lo, struct request *rq, loff_t pos, 431efcfec57SDarrick J. Wong int mode) 432cf655d95SMing Lei { 433cf655d95SMing Lei /* 434efcfec57SDarrick J. Wong * We use fallocate to manipulate the space mappings used by the image 435efcfec57SDarrick J. Wong * a.k.a. discard/zerorange. However we do not support this if 436efcfec57SDarrick J. Wong * encryption is enabled, because it may give an attacker useful 437efcfec57SDarrick J. Wong * information. 438cf655d95SMing Lei */ 439cf655d95SMing Lei struct file *file = lo->lo_backing_file; 440c52abf56SEvan Green struct request_queue *q = lo->lo_queue; 441cf655d95SMing Lei int ret; 442cf655d95SMing Lei 443efcfec57SDarrick J. Wong mode |= FALLOC_FL_KEEP_SIZE; 444efcfec57SDarrick J. Wong 445c52abf56SEvan Green if (!blk_queue_discard(q)) { 446cf655d95SMing Lei ret = -EOPNOTSUPP; 447cf655d95SMing Lei goto out; 448cf655d95SMing Lei } 449cf655d95SMing Lei 450cf655d95SMing Lei ret = file->f_op->fallocate(file, mode, pos, blk_rq_bytes(rq)); 451cf655d95SMing Lei if (unlikely(ret && ret != -EINVAL && ret != -EOPNOTSUPP)) 452cf655d95SMing Lei ret = -EIO; 453cf655d95SMing Lei out: 454cf655d95SMing Lei return ret; 455cf655d95SMing Lei } 456cf655d95SMing Lei 457cf655d95SMing Lei static int lo_req_flush(struct loop_device *lo, struct request *rq) 458cf655d95SMing Lei { 459cf655d95SMing Lei struct file *file = lo->lo_backing_file; 460cf655d95SMing Lei int ret = vfs_fsync(file, 0); 461cf655d95SMing Lei if (unlikely(ret && ret != -EINVAL)) 462cf655d95SMing Lei ret = -EIO; 463cf655d95SMing Lei 464cf655d95SMing Lei return ret; 465cf655d95SMing Lei } 466cf655d95SMing Lei 467fe2cb290SChristoph Hellwig static void lo_complete_rq(struct request *rq) 468bc07c10aSMing Lei { 469fe2cb290SChristoph Hellwig struct loop_cmd *cmd = blk_mq_rq_to_pdu(rq); 470f9de14bcSJens Axboe blk_status_t ret = BLK_STS_OK; 471bc07c10aSMing Lei 472f9de14bcSJens Axboe if (!cmd->use_aio || cmd->ret < 0 || cmd->ret == blk_rq_bytes(rq) || 473f9de14bcSJens Axboe req_op(rq) != REQ_OP_READ) { 474f9de14bcSJens Axboe if (cmd->ret < 0) 4758cd55087SEvan Green ret = errno_to_blk_status(cmd->ret); 476f9de14bcSJens Axboe goto end_io; 477bc07c10aSMing Lei } 478fe2cb290SChristoph Hellwig 479f9de14bcSJens Axboe /* 480f9de14bcSJens Axboe * Short READ - if we got some data, advance our request and 481f9de14bcSJens Axboe * retry it. If we got no data, end the rest with EIO. 482f9de14bcSJens Axboe */ 483f9de14bcSJens Axboe if (cmd->ret) { 484f9de14bcSJens Axboe blk_update_request(rq, BLK_STS_OK, cmd->ret); 485f9de14bcSJens Axboe cmd->ret = 0; 486f9de14bcSJens Axboe blk_mq_requeue_request(rq, true); 487f9de14bcSJens Axboe } else { 488f9de14bcSJens Axboe if (cmd->use_aio) { 489f9de14bcSJens Axboe struct bio *bio = rq->bio; 490f9de14bcSJens Axboe 491f9de14bcSJens Axboe while (bio) { 492f9de14bcSJens Axboe zero_fill_bio(bio); 493f9de14bcSJens Axboe bio = bio->bi_next; 494f9de14bcSJens Axboe } 495f9de14bcSJens Axboe } 496f9de14bcSJens Axboe ret = BLK_STS_IOERR; 497f9de14bcSJens Axboe end_io: 498f9de14bcSJens Axboe blk_mq_end_request(rq, ret); 499f9de14bcSJens Axboe } 500bc07c10aSMing Lei } 501bc07c10aSMing Lei 50292d77332SShaohua Li static void lo_rw_aio_do_completion(struct loop_cmd *cmd) 50392d77332SShaohua Li { 5041894e916SJens Axboe struct request *rq = blk_mq_rq_from_pdu(cmd); 5051894e916SJens Axboe 50692d77332SShaohua Li if (!atomic_dec_and_test(&cmd->ref)) 50792d77332SShaohua Li return; 50892d77332SShaohua Li kfree(cmd->bvec); 50992d77332SShaohua Li cmd->bvec = NULL; 51015f73f5bSChristoph Hellwig if (likely(!blk_should_fake_timeout(rq->q))) 5111894e916SJens Axboe blk_mq_complete_request(rq); 51292d77332SShaohua Li } 51392d77332SShaohua Li 514bc07c10aSMing Lei static void lo_rw_aio_complete(struct kiocb *iocb, long ret, long ret2) 515bc07c10aSMing Lei { 516bc07c10aSMing Lei struct loop_cmd *cmd = container_of(iocb, struct loop_cmd, iocb); 517bc07c10aSMing Lei 518d4478e92SShaohua Li if (cmd->css) 519d4478e92SShaohua Li css_put(cmd->css); 520fe2cb290SChristoph Hellwig cmd->ret = ret; 52192d77332SShaohua Li lo_rw_aio_do_completion(cmd); 522bc07c10aSMing Lei } 523bc07c10aSMing Lei 524bc07c10aSMing Lei static int lo_rw_aio(struct loop_device *lo, struct loop_cmd *cmd, 525bc07c10aSMing Lei loff_t pos, bool rw) 526bc07c10aSMing Lei { 527bc07c10aSMing Lei struct iov_iter iter; 52886af5952SMing Lei struct req_iterator rq_iter; 529bc07c10aSMing Lei struct bio_vec *bvec; 5301894e916SJens Axboe struct request *rq = blk_mq_rq_from_pdu(cmd); 53140326d8aSShaohua Li struct bio *bio = rq->bio; 532bc07c10aSMing Lei struct file *file = lo->lo_backing_file; 53386af5952SMing Lei struct bio_vec tmp; 53440326d8aSShaohua Li unsigned int offset; 53586af5952SMing Lei int nr_bvec = 0; 536bc07c10aSMing Lei int ret; 537bc07c10aSMing Lei 53886af5952SMing Lei rq_for_each_bvec(tmp, rq, rq_iter) 53986af5952SMing Lei nr_bvec++; 540bc07c10aSMing Lei 54186af5952SMing Lei if (rq->bio != rq->biotail) { 54286af5952SMing Lei 54386af5952SMing Lei bvec = kmalloc_array(nr_bvec, sizeof(struct bio_vec), 5446da2ec56SKees Cook GFP_NOIO); 54540326d8aSShaohua Li if (!bvec) 54640326d8aSShaohua Li return -EIO; 54740326d8aSShaohua Li cmd->bvec = bvec; 54840326d8aSShaohua Li 549a7297a6aSMing Lei /* 55040326d8aSShaohua Li * The bios of the request may be started from the middle of 55140326d8aSShaohua Li * the 'bvec' because of bio splitting, so we can't directly 55286af5952SMing Lei * copy bio->bi_iov_vec to new bvec. The rq_for_each_bvec 55340326d8aSShaohua Li * API will take care of all details for us. 554a7297a6aSMing Lei */ 55586af5952SMing Lei rq_for_each_bvec(tmp, rq, rq_iter) { 55640326d8aSShaohua Li *bvec = tmp; 55740326d8aSShaohua Li bvec++; 55840326d8aSShaohua Li } 55940326d8aSShaohua Li bvec = cmd->bvec; 56040326d8aSShaohua Li offset = 0; 56140326d8aSShaohua Li } else { 56240326d8aSShaohua Li /* 56340326d8aSShaohua Li * Same here, this bio may be started from the middle of the 56440326d8aSShaohua Li * 'bvec' because of bio splitting, so offset from the bvec 56540326d8aSShaohua Li * must be passed to iov iterator 56640326d8aSShaohua Li */ 56740326d8aSShaohua Li offset = bio->bi_iter.bi_bvec_done; 56840326d8aSShaohua Li bvec = __bvec_iter_bvec(bio->bi_io_vec, bio->bi_iter); 56940326d8aSShaohua Li } 57092d77332SShaohua Li atomic_set(&cmd->ref, 2); 57140326d8aSShaohua Li 572b6207430SChristoph Hellwig iov_iter_bvec(&iter, rw, bvec, nr_bvec, blk_rq_bytes(rq)); 57340326d8aSShaohua Li iter.iov_offset = offset; 574bc07c10aSMing Lei 575bc07c10aSMing Lei cmd->iocb.ki_pos = pos; 576bc07c10aSMing Lei cmd->iocb.ki_filp = file; 577bc07c10aSMing Lei cmd->iocb.ki_complete = lo_rw_aio_complete; 578bc07c10aSMing Lei cmd->iocb.ki_flags = IOCB_DIRECT; 579d9a08a9eSAdam Manzanares cmd->iocb.ki_ioprio = IOPRIO_PRIO_VALUE(IOPRIO_CLASS_NONE, 0); 580d4478e92SShaohua Li if (cmd->css) 581d4478e92SShaohua Li kthread_associate_blkcg(cmd->css); 582bc07c10aSMing Lei 583bc07c10aSMing Lei if (rw == WRITE) 584bb7462b6SMiklos Szeredi ret = call_write_iter(file, &cmd->iocb, &iter); 585bc07c10aSMing Lei else 586bb7462b6SMiklos Szeredi ret = call_read_iter(file, &cmd->iocb, &iter); 587bc07c10aSMing Lei 58892d77332SShaohua Li lo_rw_aio_do_completion(cmd); 589d4478e92SShaohua Li kthread_associate_blkcg(NULL); 59092d77332SShaohua Li 591bc07c10aSMing Lei if (ret != -EIOCBQUEUED) 592bc07c10aSMing Lei cmd->iocb.ki_complete(&cmd->iocb, ret, 0); 593bc07c10aSMing Lei return 0; 594bc07c10aSMing Lei } 595bc07c10aSMing Lei 596c1c87c2bSChristoph Hellwig static int do_req_filebacked(struct loop_device *lo, struct request *rq) 597bc07c10aSMing Lei { 598bc07c10aSMing Lei struct loop_cmd *cmd = blk_mq_rq_to_pdu(rq); 599c1c87c2bSChristoph Hellwig loff_t pos = ((loff_t) blk_rq_pos(rq) << 9) + lo->lo_offset; 600bc07c10aSMing Lei 601bc07c10aSMing Lei /* 602bc07c10aSMing Lei * lo_write_simple and lo_read_simple should have been covered 603bc07c10aSMing Lei * by io submit style function like lo_rw_aio(), one blocker 604bc07c10aSMing Lei * is that lo_read_simple() need to call flush_dcache_page after 605bc07c10aSMing Lei * the page is written from kernel, and it isn't easy to handle 606bc07c10aSMing Lei * this in io submit style function which submits all segments 607bc07c10aSMing Lei * of the req at one time. And direct read IO doesn't need to 608bc07c10aSMing Lei * run flush_dcache_page(). 609bc07c10aSMing Lei */ 610c1c87c2bSChristoph Hellwig switch (req_op(rq)) { 611c1c87c2bSChristoph Hellwig case REQ_OP_FLUSH: 612c1c87c2bSChristoph Hellwig return lo_req_flush(lo, rq); 61319372e27SChristoph Hellwig case REQ_OP_WRITE_ZEROES: 614efcfec57SDarrick J. Wong /* 615efcfec57SDarrick J. Wong * If the caller doesn't want deallocation, call zeroout to 616efcfec57SDarrick J. Wong * write zeroes the range. Otherwise, punch them out. 617efcfec57SDarrick J. Wong */ 618efcfec57SDarrick J. Wong return lo_fallocate(lo, rq, pos, 619efcfec57SDarrick J. Wong (rq->cmd_flags & REQ_NOUNMAP) ? 620efcfec57SDarrick J. Wong FALLOC_FL_ZERO_RANGE : 621efcfec57SDarrick J. Wong FALLOC_FL_PUNCH_HOLE); 622efcfec57SDarrick J. Wong case REQ_OP_DISCARD: 623efcfec57SDarrick J. Wong return lo_fallocate(lo, rq, pos, FALLOC_FL_PUNCH_HOLE); 624c1c87c2bSChristoph Hellwig case REQ_OP_WRITE: 625c1c87c2bSChristoph Hellwig if (lo->transfer) 626c1c87c2bSChristoph Hellwig return lo_write_transfer(lo, rq, pos); 627c1c87c2bSChristoph Hellwig else if (cmd->use_aio) 628c1c87c2bSChristoph Hellwig return lo_rw_aio(lo, cmd, pos, WRITE); 629c1c87c2bSChristoph Hellwig else 630bc07c10aSMing Lei return lo_write_simple(lo, rq, pos); 631c1c87c2bSChristoph Hellwig case REQ_OP_READ: 632c1c87c2bSChristoph Hellwig if (lo->transfer) 633c1c87c2bSChristoph Hellwig return lo_read_transfer(lo, rq, pos); 634c1c87c2bSChristoph Hellwig else if (cmd->use_aio) 635c1c87c2bSChristoph Hellwig return lo_rw_aio(lo, cmd, pos, READ); 636bc07c10aSMing Lei else 637bc07c10aSMing Lei return lo_read_simple(lo, rq, pos); 638c1c87c2bSChristoph Hellwig default: 639c1c87c2bSChristoph Hellwig WARN_ON_ONCE(1); 640c1c87c2bSChristoph Hellwig return -EIO; 641bc07c10aSMing Lei } 6421da177e4SLinus Torvalds } 6431da177e4SLinus Torvalds 6442e5ab5f3SMing Lei static inline void loop_update_dio(struct loop_device *lo) 6452e5ab5f3SMing Lei { 646efbe3c24SIra Weiny __loop_update_dio(lo, (lo->lo_backing_file->f_flags & O_DIRECT) | 6472e5ab5f3SMing Lei lo->use_dio); 6482e5ab5f3SMing Lei } 6492e5ab5f3SMing Lei 65006f0e9e6SMing Lei static void loop_reread_partitions(struct loop_device *lo, 65106f0e9e6SMing Lei struct block_device *bdev) 65206f0e9e6SMing Lei { 65306f0e9e6SMing Lei int rc; 65406f0e9e6SMing Lei 655a8698707SChristoph Hellwig mutex_lock(&bdev->bd_disk->open_mutex); 656f0b870dfSChristoph Hellwig rc = bdev_disk_changed(bdev, false); 657a8698707SChristoph Hellwig mutex_unlock(&bdev->bd_disk->open_mutex); 65806f0e9e6SMing Lei if (rc) 65906f0e9e6SMing Lei pr_warn("%s: partition scan of loop%d (%s) failed (rc=%d)\n", 66006f0e9e6SMing Lei __func__, lo->lo_number, lo->lo_file_name, rc); 66106f0e9e6SMing Lei } 66206f0e9e6SMing Lei 663d2ac838eSTheodore Ts'o static inline int is_loop_device(struct file *file) 664d2ac838eSTheodore Ts'o { 665d2ac838eSTheodore Ts'o struct inode *i = file->f_mapping->host; 666d2ac838eSTheodore Ts'o 6676f24784fSAl Viro return i && S_ISBLK(i->i_mode) && imajor(i) == LOOP_MAJOR; 668d2ac838eSTheodore Ts'o } 669d2ac838eSTheodore Ts'o 670d2ac838eSTheodore Ts'o static int loop_validate_file(struct file *file, struct block_device *bdev) 671d2ac838eSTheodore Ts'o { 672d2ac838eSTheodore Ts'o struct inode *inode = file->f_mapping->host; 673d2ac838eSTheodore Ts'o struct file *f = file; 674d2ac838eSTheodore Ts'o 675d2ac838eSTheodore Ts'o /* Avoid recursion */ 676d2ac838eSTheodore Ts'o while (is_loop_device(f)) { 677d2ac838eSTheodore Ts'o struct loop_device *l; 678d2ac838eSTheodore Ts'o 6794e7b5671SChristoph Hellwig if (f->f_mapping->host->i_rdev == bdev->bd_dev) 680d2ac838eSTheodore Ts'o return -EBADF; 681d2ac838eSTheodore Ts'o 6824e7b5671SChristoph Hellwig l = I_BDEV(f->f_mapping->host)->bd_disk->private_data; 683f7c8a412SDongli Zhang if (l->lo_state != Lo_bound) { 684d2ac838eSTheodore Ts'o return -EINVAL; 685d2ac838eSTheodore Ts'o } 686d2ac838eSTheodore Ts'o f = l->lo_backing_file; 687d2ac838eSTheodore Ts'o } 688d2ac838eSTheodore Ts'o if (!S_ISREG(inode->i_mode) && !S_ISBLK(inode->i_mode)) 689d2ac838eSTheodore Ts'o return -EINVAL; 690d2ac838eSTheodore Ts'o return 0; 691d2ac838eSTheodore Ts'o } 692d2ac838eSTheodore Ts'o 6931da177e4SLinus Torvalds /* 6941da177e4SLinus Torvalds * loop_change_fd switched the backing store of a loopback device to 6951da177e4SLinus Torvalds * a new file. This is useful for operating system installers to free up 6961da177e4SLinus Torvalds * the original file and in High Availability environments to switch to 6971da177e4SLinus Torvalds * an alternative location for the content in case of server meltdown. 6981da177e4SLinus Torvalds * This can only work if the loop device is used read-only, and if the 6991da177e4SLinus Torvalds * new backing store is the same size and type as the old backing store. 7001da177e4SLinus Torvalds */ 701bb214884SAl Viro static int loop_change_fd(struct loop_device *lo, struct block_device *bdev, 702bb214884SAl Viro unsigned int arg) 7031da177e4SLinus Torvalds { 7041dded9acSJan Kara struct file *file = NULL, *old_file; 7051da177e4SLinus Torvalds int error; 70685b0a54aSJan Kara bool partscan; 7071da177e4SLinus Torvalds 7086cc8e743SPavel Tatashin error = mutex_lock_killable(&lo->lo_mutex); 709c3710770SJan Kara if (error) 710c3710770SJan Kara return error; 7111da177e4SLinus Torvalds error = -ENXIO; 7121da177e4SLinus Torvalds if (lo->lo_state != Lo_bound) 7131dded9acSJan Kara goto out_err; 7141da177e4SLinus Torvalds 7151da177e4SLinus Torvalds /* the loop device has to be read-only */ 7161da177e4SLinus Torvalds error = -EINVAL; 7171da177e4SLinus Torvalds if (!(lo->lo_flags & LO_FLAGS_READ_ONLY)) 7181dded9acSJan Kara goto out_err; 7191da177e4SLinus Torvalds 7201da177e4SLinus Torvalds error = -EBADF; 7211da177e4SLinus Torvalds file = fget(arg); 7221da177e4SLinus Torvalds if (!file) 7231dded9acSJan Kara goto out_err; 7241da177e4SLinus Torvalds 725d2ac838eSTheodore Ts'o error = loop_validate_file(file, bdev); 726d2ac838eSTheodore Ts'o if (error) 7271dded9acSJan Kara goto out_err; 728d2ac838eSTheodore Ts'o 7291da177e4SLinus Torvalds old_file = lo->lo_backing_file; 7301da177e4SLinus Torvalds 7311da177e4SLinus Torvalds error = -EINVAL; 7321da177e4SLinus Torvalds 7331da177e4SLinus Torvalds /* size of the new backing store needs to be the same */ 7341da177e4SLinus Torvalds if (get_loop_size(lo, file) != get_loop_size(lo, old_file)) 7351dded9acSJan Kara goto out_err; 7361da177e4SLinus Torvalds 7371da177e4SLinus Torvalds /* and ... switch */ 73843cade80SOmar Sandoval blk_mq_freeze_queue(lo->lo_queue); 73943cade80SOmar Sandoval mapping_set_gfp_mask(old_file->f_mapping, lo->old_gfp_mask); 74043cade80SOmar Sandoval lo->lo_backing_file = file; 74143cade80SOmar Sandoval lo->old_gfp_mask = mapping_gfp_mask(file->f_mapping); 74243cade80SOmar Sandoval mapping_set_gfp_mask(file->f_mapping, 74343cade80SOmar Sandoval lo->old_gfp_mask & ~(__GFP_IO|__GFP_FS)); 74443cade80SOmar Sandoval loop_update_dio(lo); 74543cade80SOmar Sandoval blk_mq_unfreeze_queue(lo->lo_queue); 74685b0a54aSJan Kara partscan = lo->lo_flags & LO_FLAGS_PARTSCAN; 7476cc8e743SPavel Tatashin mutex_unlock(&lo->lo_mutex); 7481dded9acSJan Kara /* 7496cc8e743SPavel Tatashin * We must drop file reference outside of lo_mutex as dropping 750a8698707SChristoph Hellwig * the file ref can take open_mutex which creates circular locking 7511dded9acSJan Kara * dependency. 7521dded9acSJan Kara */ 7531dded9acSJan Kara fput(old_file); 75485b0a54aSJan Kara if (partscan) 75585b0a54aSJan Kara loop_reread_partitions(lo, bdev); 7561da177e4SLinus Torvalds return 0; 7571da177e4SLinus Torvalds 7581dded9acSJan Kara out_err: 7596cc8e743SPavel Tatashin mutex_unlock(&lo->lo_mutex); 7601dded9acSJan Kara if (file) 7611dded9acSJan Kara fput(file); 7621da177e4SLinus Torvalds return error; 7631da177e4SLinus Torvalds } 7641da177e4SLinus Torvalds 765ee862730SMilan Broz /* loop sysfs attributes */ 766ee862730SMilan Broz 767ee862730SMilan Broz static ssize_t loop_attr_show(struct device *dev, char *page, 768ee862730SMilan Broz ssize_t (*callback)(struct loop_device *, char *)) 769ee862730SMilan Broz { 77034dd82afSKay Sievers struct gendisk *disk = dev_to_disk(dev); 77134dd82afSKay Sievers struct loop_device *lo = disk->private_data; 772ee862730SMilan Broz 77334dd82afSKay Sievers return callback(lo, page); 774ee862730SMilan Broz } 775ee862730SMilan Broz 776ee862730SMilan Broz #define LOOP_ATTR_RO(_name) \ 777ee862730SMilan Broz static ssize_t loop_attr_##_name##_show(struct loop_device *, char *); \ 778ee862730SMilan Broz static ssize_t loop_attr_do_show_##_name(struct device *d, \ 779ee862730SMilan Broz struct device_attribute *attr, char *b) \ 780ee862730SMilan Broz { \ 781ee862730SMilan Broz return loop_attr_show(d, b, loop_attr_##_name##_show); \ 782ee862730SMilan Broz } \ 783ee862730SMilan Broz static struct device_attribute loop_attr_##_name = \ 7845657a819SJoe Perches __ATTR(_name, 0444, loop_attr_do_show_##_name, NULL); 785ee862730SMilan Broz 786ee862730SMilan Broz static ssize_t loop_attr_backing_file_show(struct loop_device *lo, char *buf) 787ee862730SMilan Broz { 788ee862730SMilan Broz ssize_t ret; 789ee862730SMilan Broz char *p = NULL; 790ee862730SMilan Broz 79105eb0f25SKay Sievers spin_lock_irq(&lo->lo_lock); 792ee862730SMilan Broz if (lo->lo_backing_file) 7939bf39ab2SMiklos Szeredi p = file_path(lo->lo_backing_file, buf, PAGE_SIZE - 1); 79405eb0f25SKay Sievers spin_unlock_irq(&lo->lo_lock); 795ee862730SMilan Broz 796ee862730SMilan Broz if (IS_ERR_OR_NULL(p)) 797ee862730SMilan Broz ret = PTR_ERR(p); 798ee862730SMilan Broz else { 799ee862730SMilan Broz ret = strlen(p); 800ee862730SMilan Broz memmove(buf, p, ret); 801ee862730SMilan Broz buf[ret++] = '\n'; 802ee862730SMilan Broz buf[ret] = 0; 803ee862730SMilan Broz } 804ee862730SMilan Broz 805ee862730SMilan Broz return ret; 806ee862730SMilan Broz } 807ee862730SMilan Broz 808ee862730SMilan Broz static ssize_t loop_attr_offset_show(struct loop_device *lo, char *buf) 809ee862730SMilan Broz { 810ee862730SMilan Broz return sprintf(buf, "%llu\n", (unsigned long long)lo->lo_offset); 811ee862730SMilan Broz } 812ee862730SMilan Broz 813ee862730SMilan Broz static ssize_t loop_attr_sizelimit_show(struct loop_device *lo, char *buf) 814ee862730SMilan Broz { 815ee862730SMilan Broz return sprintf(buf, "%llu\n", (unsigned long long)lo->lo_sizelimit); 816ee862730SMilan Broz } 817ee862730SMilan Broz 818ee862730SMilan Broz static ssize_t loop_attr_autoclear_show(struct loop_device *lo, char *buf) 819ee862730SMilan Broz { 820ee862730SMilan Broz int autoclear = (lo->lo_flags & LO_FLAGS_AUTOCLEAR); 821ee862730SMilan Broz 822ee862730SMilan Broz return sprintf(buf, "%s\n", autoclear ? "1" : "0"); 823ee862730SMilan Broz } 824ee862730SMilan Broz 825e03c8dd1SKay Sievers static ssize_t loop_attr_partscan_show(struct loop_device *lo, char *buf) 826e03c8dd1SKay Sievers { 827e03c8dd1SKay Sievers int partscan = (lo->lo_flags & LO_FLAGS_PARTSCAN); 828e03c8dd1SKay Sievers 829e03c8dd1SKay Sievers return sprintf(buf, "%s\n", partscan ? "1" : "0"); 830e03c8dd1SKay Sievers } 831e03c8dd1SKay Sievers 8322e5ab5f3SMing Lei static ssize_t loop_attr_dio_show(struct loop_device *lo, char *buf) 8332e5ab5f3SMing Lei { 8342e5ab5f3SMing Lei int dio = (lo->lo_flags & LO_FLAGS_DIRECT_IO); 8352e5ab5f3SMing Lei 8362e5ab5f3SMing Lei return sprintf(buf, "%s\n", dio ? "1" : "0"); 8372e5ab5f3SMing Lei } 8382e5ab5f3SMing Lei 839ee862730SMilan Broz LOOP_ATTR_RO(backing_file); 840ee862730SMilan Broz LOOP_ATTR_RO(offset); 841ee862730SMilan Broz LOOP_ATTR_RO(sizelimit); 842ee862730SMilan Broz LOOP_ATTR_RO(autoclear); 843e03c8dd1SKay Sievers LOOP_ATTR_RO(partscan); 8442e5ab5f3SMing Lei LOOP_ATTR_RO(dio); 845ee862730SMilan Broz 846ee862730SMilan Broz static struct attribute *loop_attrs[] = { 847ee862730SMilan Broz &loop_attr_backing_file.attr, 848ee862730SMilan Broz &loop_attr_offset.attr, 849ee862730SMilan Broz &loop_attr_sizelimit.attr, 850ee862730SMilan Broz &loop_attr_autoclear.attr, 851e03c8dd1SKay Sievers &loop_attr_partscan.attr, 8522e5ab5f3SMing Lei &loop_attr_dio.attr, 853ee862730SMilan Broz NULL, 854ee862730SMilan Broz }; 855ee862730SMilan Broz 856ee862730SMilan Broz static struct attribute_group loop_attribute_group = { 857ee862730SMilan Broz .name = "loop", 858ee862730SMilan Broz .attrs= loop_attrs, 859ee862730SMilan Broz }; 860ee862730SMilan Broz 861d3349b6bSTetsuo Handa static void loop_sysfs_init(struct loop_device *lo) 862ee862730SMilan Broz { 863d3349b6bSTetsuo Handa lo->sysfs_inited = !sysfs_create_group(&disk_to_dev(lo->lo_disk)->kobj, 864ee862730SMilan Broz &loop_attribute_group); 865ee862730SMilan Broz } 866ee862730SMilan Broz 867ee862730SMilan Broz static void loop_sysfs_exit(struct loop_device *lo) 868ee862730SMilan Broz { 869d3349b6bSTetsuo Handa if (lo->sysfs_inited) 870ee862730SMilan Broz sysfs_remove_group(&disk_to_dev(lo->lo_disk)->kobj, 871ee862730SMilan Broz &loop_attribute_group); 872ee862730SMilan Broz } 873ee862730SMilan Broz 874dfaa2ef6SLukas Czerner static void loop_config_discard(struct loop_device *lo) 875dfaa2ef6SLukas Czerner { 876dfaa2ef6SLukas Czerner struct file *file = lo->lo_backing_file; 877dfaa2ef6SLukas Czerner struct inode *inode = file->f_mapping->host; 878dfaa2ef6SLukas Czerner struct request_queue *q = lo->lo_queue; 879bcb21c8cSMing Lei u32 granularity, max_discard_sectors; 880dfaa2ef6SLukas Czerner 881dfaa2ef6SLukas Czerner /* 882c52abf56SEvan Green * If the backing device is a block device, mirror its zeroing 883c52abf56SEvan Green * capability. Set the discard sectors to the block device's zeroing 884c52abf56SEvan Green * capabilities because loop discards result in blkdev_issue_zeroout(), 885c52abf56SEvan Green * not blkdev_issue_discard(). This maintains consistent behavior with 886c52abf56SEvan Green * file-backed loop devices: discarded regions read back as zero. 887c52abf56SEvan Green */ 888c52abf56SEvan Green if (S_ISBLK(inode->i_mode) && !lo->lo_encrypt_key_size) { 8894e7b5671SChristoph Hellwig struct request_queue *backingq = bdev_get_queue(I_BDEV(inode)); 890c52abf56SEvan Green 891bcb21c8cSMing Lei max_discard_sectors = backingq->limits.max_write_zeroes_sectors; 892bcb21c8cSMing Lei granularity = backingq->limits.discard_granularity ?: 893bcb21c8cSMing Lei queue_physical_block_size(backingq); 894c52abf56SEvan Green 895c52abf56SEvan Green /* 896dfaa2ef6SLukas Czerner * We use punch hole to reclaim the free space used by the 89712a64d2fSOlaf Hering * image a.k.a. discard. However we do not support discard if 898dfaa2ef6SLukas Czerner * encryption is enabled, because it may give an attacker 899dfaa2ef6SLukas Czerner * useful information. 900dfaa2ef6SLukas Czerner */ 901c52abf56SEvan Green } else if (!file->f_op->fallocate || lo->lo_encrypt_key_size) { 902bcb21c8cSMing Lei max_discard_sectors = 0; 903bcb21c8cSMing Lei granularity = 0; 904dfaa2ef6SLukas Czerner 905c52abf56SEvan Green } else { 906bcb21c8cSMing Lei max_discard_sectors = UINT_MAX >> 9; 907bcb21c8cSMing Lei granularity = inode->i_sb->s_blocksize; 908c52abf56SEvan Green } 909c52abf56SEvan Green 910bcb21c8cSMing Lei if (max_discard_sectors) { 911bcb21c8cSMing Lei q->limits.discard_granularity = granularity; 912bcb21c8cSMing Lei blk_queue_max_discard_sectors(q, max_discard_sectors); 913bcb21c8cSMing Lei blk_queue_max_write_zeroes_sectors(q, max_discard_sectors); 9148b904b5bSBart Van Assche blk_queue_flag_set(QUEUE_FLAG_DISCARD, q); 915bcb21c8cSMing Lei } else { 916bcb21c8cSMing Lei q->limits.discard_granularity = 0; 917bcb21c8cSMing Lei blk_queue_max_discard_sectors(q, 0); 918bcb21c8cSMing Lei blk_queue_max_write_zeroes_sectors(q, 0); 919c52abf56SEvan Green blk_queue_flag_clear(QUEUE_FLAG_DISCARD, q); 920dfaa2ef6SLukas Czerner } 921bcb21c8cSMing Lei q->limits.discard_alignment = 0; 922bcb21c8cSMing Lei } 923dfaa2ef6SLukas Czerner 924e03a3d7aSMing Lei static void loop_unprepare_queue(struct loop_device *lo) 925e03a3d7aSMing Lei { 9263989144fSPetr Mladek kthread_flush_worker(&lo->worker); 927e03a3d7aSMing Lei kthread_stop(lo->worker_task); 928e03a3d7aSMing Lei } 929e03a3d7aSMing Lei 930b2ee7d46SNeilBrown static int loop_kthread_worker_fn(void *worker_ptr) 931b2ee7d46SNeilBrown { 932a37b0715SNeilBrown current->flags |= PF_LOCAL_THROTTLE | PF_MEMALLOC_NOIO; 933b2ee7d46SNeilBrown return kthread_worker_fn(worker_ptr); 934b2ee7d46SNeilBrown } 935b2ee7d46SNeilBrown 936e03a3d7aSMing Lei static int loop_prepare_queue(struct loop_device *lo) 937e03a3d7aSMing Lei { 9383989144fSPetr Mladek kthread_init_worker(&lo->worker); 939b2ee7d46SNeilBrown lo->worker_task = kthread_run(loop_kthread_worker_fn, 940e03a3d7aSMing Lei &lo->worker, "loop%d", lo->lo_number); 941e03a3d7aSMing Lei if (IS_ERR(lo->worker_task)) 942e03a3d7aSMing Lei return -ENOMEM; 943e03a3d7aSMing Lei set_user_nice(lo->worker_task, MIN_NICE); 944e03a3d7aSMing Lei return 0; 945e03a3d7aSMing Lei } 946e03a3d7aSMing Lei 94756a85fd8SHolger Hoffstätte static void loop_update_rotational(struct loop_device *lo) 94856a85fd8SHolger Hoffstätte { 94956a85fd8SHolger Hoffstätte struct file *file = lo->lo_backing_file; 95056a85fd8SHolger Hoffstätte struct inode *file_inode = file->f_mapping->host; 95156a85fd8SHolger Hoffstätte struct block_device *file_bdev = file_inode->i_sb->s_bdev; 95256a85fd8SHolger Hoffstätte struct request_queue *q = lo->lo_queue; 95356a85fd8SHolger Hoffstätte bool nonrot = true; 95456a85fd8SHolger Hoffstätte 95556a85fd8SHolger Hoffstätte /* not all filesystems (e.g. tmpfs) have a sb->s_bdev */ 95656a85fd8SHolger Hoffstätte if (file_bdev) 95756a85fd8SHolger Hoffstätte nonrot = blk_queue_nonrot(bdev_get_queue(file_bdev)); 95856a85fd8SHolger Hoffstätte 95956a85fd8SHolger Hoffstätte if (nonrot) 96056a85fd8SHolger Hoffstätte blk_queue_flag_set(QUEUE_FLAG_NONROT, q); 96156a85fd8SHolger Hoffstätte else 96256a85fd8SHolger Hoffstätte blk_queue_flag_clear(QUEUE_FLAG_NONROT, q); 96356a85fd8SHolger Hoffstätte } 96456a85fd8SHolger Hoffstätte 9651da177e4SLinus Torvalds static int 9661da177e4SLinus Torvalds loop_release_xfer(struct loop_device *lo) 9671da177e4SLinus Torvalds { 9681da177e4SLinus Torvalds int err = 0; 9691da177e4SLinus Torvalds struct loop_func_table *xfer = lo->lo_encryption; 9701da177e4SLinus Torvalds 9711da177e4SLinus Torvalds if (xfer) { 9721da177e4SLinus Torvalds if (xfer->release) 9731da177e4SLinus Torvalds err = xfer->release(lo); 9741da177e4SLinus Torvalds lo->transfer = NULL; 9751da177e4SLinus Torvalds lo->lo_encryption = NULL; 9761da177e4SLinus Torvalds module_put(xfer->owner); 9771da177e4SLinus Torvalds } 9781da177e4SLinus Torvalds return err; 9791da177e4SLinus Torvalds } 9801da177e4SLinus Torvalds 9811da177e4SLinus Torvalds static int 9821da177e4SLinus Torvalds loop_init_xfer(struct loop_device *lo, struct loop_func_table *xfer, 9831da177e4SLinus Torvalds const struct loop_info64 *i) 9841da177e4SLinus Torvalds { 9851da177e4SLinus Torvalds int err = 0; 9861da177e4SLinus Torvalds 9871da177e4SLinus Torvalds if (xfer) { 9881da177e4SLinus Torvalds struct module *owner = xfer->owner; 9891da177e4SLinus Torvalds 9901da177e4SLinus Torvalds if (!try_module_get(owner)) 9911da177e4SLinus Torvalds return -EINVAL; 9921da177e4SLinus Torvalds if (xfer->init) 9931da177e4SLinus Torvalds err = xfer->init(lo, i); 9941da177e4SLinus Torvalds if (err) 9951da177e4SLinus Torvalds module_put(owner); 9961da177e4SLinus Torvalds else 9971da177e4SLinus Torvalds lo->lo_encryption = xfer; 9981da177e4SLinus Torvalds } 9991da177e4SLinus Torvalds return err; 10001da177e4SLinus Torvalds } 10011da177e4SLinus Torvalds 100262ab466cSMartijn Coenen /** 100362ab466cSMartijn Coenen * loop_set_status_from_info - configure device from loop_info 100462ab466cSMartijn Coenen * @lo: struct loop_device to configure 100562ab466cSMartijn Coenen * @info: struct loop_info64 to configure the device with 100662ab466cSMartijn Coenen * 100762ab466cSMartijn Coenen * Configures the loop device parameters according to the passed 100862ab466cSMartijn Coenen * in loop_info64 configuration. 100962ab466cSMartijn Coenen */ 101062ab466cSMartijn Coenen static int 101162ab466cSMartijn Coenen loop_set_status_from_info(struct loop_device *lo, 101262ab466cSMartijn Coenen const struct loop_info64 *info) 101362ab466cSMartijn Coenen { 101462ab466cSMartijn Coenen int err; 101562ab466cSMartijn Coenen struct loop_func_table *xfer; 101662ab466cSMartijn Coenen kuid_t uid = current_uid(); 101762ab466cSMartijn Coenen 101862ab466cSMartijn Coenen if ((unsigned int) info->lo_encrypt_key_size > LO_KEY_SIZE) 101962ab466cSMartijn Coenen return -EINVAL; 102062ab466cSMartijn Coenen 102162ab466cSMartijn Coenen err = loop_release_xfer(lo); 102262ab466cSMartijn Coenen if (err) 102362ab466cSMartijn Coenen return err; 102462ab466cSMartijn Coenen 102562ab466cSMartijn Coenen if (info->lo_encrypt_type) { 102662ab466cSMartijn Coenen unsigned int type = info->lo_encrypt_type; 102762ab466cSMartijn Coenen 102862ab466cSMartijn Coenen if (type >= MAX_LO_CRYPT) 102962ab466cSMartijn Coenen return -EINVAL; 103062ab466cSMartijn Coenen xfer = xfer_funcs[type]; 103162ab466cSMartijn Coenen if (xfer == NULL) 103262ab466cSMartijn Coenen return -EINVAL; 103362ab466cSMartijn Coenen } else 103462ab466cSMartijn Coenen xfer = NULL; 103562ab466cSMartijn Coenen 103662ab466cSMartijn Coenen err = loop_init_xfer(lo, xfer, info); 103762ab466cSMartijn Coenen if (err) 103862ab466cSMartijn Coenen return err; 103962ab466cSMartijn Coenen 104062ab466cSMartijn Coenen lo->lo_offset = info->lo_offset; 104162ab466cSMartijn Coenen lo->lo_sizelimit = info->lo_sizelimit; 104262ab466cSMartijn Coenen memcpy(lo->lo_file_name, info->lo_file_name, LO_NAME_SIZE); 104362ab466cSMartijn Coenen memcpy(lo->lo_crypt_name, info->lo_crypt_name, LO_NAME_SIZE); 104462ab466cSMartijn Coenen lo->lo_file_name[LO_NAME_SIZE-1] = 0; 104562ab466cSMartijn Coenen lo->lo_crypt_name[LO_NAME_SIZE-1] = 0; 104662ab466cSMartijn Coenen 104762ab466cSMartijn Coenen if (!xfer) 104862ab466cSMartijn Coenen xfer = &none_funcs; 104962ab466cSMartijn Coenen lo->transfer = xfer->transfer; 105062ab466cSMartijn Coenen lo->ioctl = xfer->ioctl; 105162ab466cSMartijn Coenen 1052faf1d254SMartijn Coenen lo->lo_flags = info->lo_flags; 105362ab466cSMartijn Coenen 105462ab466cSMartijn Coenen lo->lo_encrypt_key_size = info->lo_encrypt_key_size; 105562ab466cSMartijn Coenen lo->lo_init[0] = info->lo_init[0]; 105662ab466cSMartijn Coenen lo->lo_init[1] = info->lo_init[1]; 105762ab466cSMartijn Coenen if (info->lo_encrypt_key_size) { 105862ab466cSMartijn Coenen memcpy(lo->lo_encrypt_key, info->lo_encrypt_key, 105962ab466cSMartijn Coenen info->lo_encrypt_key_size); 106062ab466cSMartijn Coenen lo->lo_key_owner = uid; 106162ab466cSMartijn Coenen } 106262ab466cSMartijn Coenen 106362ab466cSMartijn Coenen return 0; 106462ab466cSMartijn Coenen } 106562ab466cSMartijn Coenen 10663448914eSMartijn Coenen static int loop_configure(struct loop_device *lo, fmode_t mode, 10673448914eSMartijn Coenen struct block_device *bdev, 10683448914eSMartijn Coenen const struct loop_config *config) 10691da177e4SLinus Torvalds { 10701da177e4SLinus Torvalds struct file *file; 10711da177e4SLinus Torvalds struct inode *inode; 10721da177e4SLinus Torvalds struct address_space *mapping; 10731da177e4SLinus Torvalds int error; 10741da177e4SLinus Torvalds loff_t size; 10751da177e4SLinus Torvalds bool partscan; 10763448914eSMartijn Coenen unsigned short bsize; 10771da177e4SLinus Torvalds 10781da177e4SLinus Torvalds /* This is safe, since we have a reference from open(). */ 10791da177e4SLinus Torvalds __module_get(THIS_MODULE); 10801da177e4SLinus Torvalds 10811da177e4SLinus Torvalds error = -EBADF; 10823448914eSMartijn Coenen file = fget(config->fd); 10831da177e4SLinus Torvalds if (!file) 10841da177e4SLinus Torvalds goto out; 10851da177e4SLinus Torvalds 10861da177e4SLinus Torvalds /* 10871da177e4SLinus Torvalds * If we don't hold exclusive handle for the device, upgrade to it 10881da177e4SLinus Torvalds * here to avoid changing device under exclusive owner. 10891da177e4SLinus Torvalds */ 10901da177e4SLinus Torvalds if (!(mode & FMODE_EXCL)) { 109137c3fc9aSChristoph Hellwig error = bd_prepare_to_claim(bdev, loop_configure); 1092ecbe6bc0SChristoph Hellwig if (error) 10931da177e4SLinus Torvalds goto out_putf; 10941da177e4SLinus Torvalds } 10951da177e4SLinus Torvalds 10966cc8e743SPavel Tatashin error = mutex_lock_killable(&lo->lo_mutex); 10971da177e4SLinus Torvalds if (error) 10981da177e4SLinus Torvalds goto out_bdev; 10991da177e4SLinus Torvalds 11001da177e4SLinus Torvalds error = -EBUSY; 11011da177e4SLinus Torvalds if (lo->lo_state != Lo_unbound) 11021da177e4SLinus Torvalds goto out_unlock; 11031da177e4SLinus Torvalds 11041da177e4SLinus Torvalds error = loop_validate_file(file, bdev); 11051da177e4SLinus Torvalds if (error) 11061da177e4SLinus Torvalds goto out_unlock; 11071da177e4SLinus Torvalds 11081da177e4SLinus Torvalds mapping = file->f_mapping; 11091da177e4SLinus Torvalds inode = mapping->host; 11101da177e4SLinus Torvalds 11113448914eSMartijn Coenen if ((config->info.lo_flags & ~LOOP_CONFIGURE_SETTABLE_FLAGS) != 0) { 11123448914eSMartijn Coenen error = -EINVAL; 11133448914eSMartijn Coenen goto out_unlock; 11143448914eSMartijn Coenen } 11153448914eSMartijn Coenen 11163448914eSMartijn Coenen if (config->block_size) { 11173448914eSMartijn Coenen error = loop_validate_block_size(config->block_size); 11183448914eSMartijn Coenen if (error) 11193448914eSMartijn Coenen goto out_unlock; 11203448914eSMartijn Coenen } 11213448914eSMartijn Coenen 11223448914eSMartijn Coenen error = loop_set_status_from_info(lo, &config->info); 11233448914eSMartijn Coenen if (error) 11243448914eSMartijn Coenen goto out_unlock; 11253448914eSMartijn Coenen 11261da177e4SLinus Torvalds if (!(file->f_mode & FMODE_WRITE) || !(mode & FMODE_WRITE) || 11271da177e4SLinus Torvalds !file->f_op->write_iter) 11283448914eSMartijn Coenen lo->lo_flags |= LO_FLAGS_READ_ONLY; 1129083a6a50SMartijn Coenen 11301da177e4SLinus Torvalds error = loop_prepare_queue(lo); 11311da177e4SLinus Torvalds if (error) 11321da177e4SLinus Torvalds goto out_unlock; 11331da177e4SLinus Torvalds 11347a2f0ce1SChristoph Hellwig set_disk_ro(lo->lo_disk, (lo->lo_flags & LO_FLAGS_READ_ONLY) != 0); 11351da177e4SLinus Torvalds 11363448914eSMartijn Coenen lo->use_dio = lo->lo_flags & LO_FLAGS_DIRECT_IO; 11371da177e4SLinus Torvalds lo->lo_device = bdev; 11381da177e4SLinus Torvalds lo->lo_backing_file = file; 11391da177e4SLinus Torvalds lo->old_gfp_mask = mapping_gfp_mask(mapping); 11401da177e4SLinus Torvalds mapping_set_gfp_mask(mapping, lo->old_gfp_mask & ~(__GFP_IO|__GFP_FS)); 11411da177e4SLinus Torvalds 11423448914eSMartijn Coenen if (!(lo->lo_flags & LO_FLAGS_READ_ONLY) && file->f_op->fsync) 11431da177e4SLinus Torvalds blk_queue_write_cache(lo->lo_queue, true, false); 11441da177e4SLinus Torvalds 11453448914eSMartijn Coenen if (config->block_size) 11463448914eSMartijn Coenen bsize = config->block_size; 114796ed320dSLinus Torvalds else if ((lo->lo_backing_file->f_flags & O_DIRECT) && inode->i_sb->s_bdev) 11481da177e4SLinus Torvalds /* In case of direct I/O, match underlying block size */ 11493448914eSMartijn Coenen bsize = bdev_logical_block_size(inode->i_sb->s_bdev); 11503448914eSMartijn Coenen else 11513448914eSMartijn Coenen bsize = 512; 11521da177e4SLinus Torvalds 11531da177e4SLinus Torvalds blk_queue_logical_block_size(lo->lo_queue, bsize); 11541da177e4SLinus Torvalds blk_queue_physical_block_size(lo->lo_queue, bsize); 11551da177e4SLinus Torvalds blk_queue_io_min(lo->lo_queue, bsize); 11561da177e4SLinus Torvalds 11571da177e4SLinus Torvalds loop_update_rotational(lo); 11581da177e4SLinus Torvalds loop_update_dio(lo); 11591da177e4SLinus Torvalds loop_sysfs_init(lo); 116079e5dc59SMartijn Coenen 116179e5dc59SMartijn Coenen size = get_loop_size(lo, file); 11625795b6f5SMartijn Coenen loop_set_size(lo, size); 11631da177e4SLinus Torvalds 11641da177e4SLinus Torvalds lo->lo_state = Lo_bound; 11651da177e4SLinus Torvalds if (part_shift) 11661da177e4SLinus Torvalds lo->lo_flags |= LO_FLAGS_PARTSCAN; 11671da177e4SLinus Torvalds partscan = lo->lo_flags & LO_FLAGS_PARTSCAN; 1168fe6a8fc5SLennart Poettering if (partscan) 1169fe6a8fc5SLennart Poettering lo->lo_disk->flags &= ~GENHD_FL_NO_PART_SCAN; 11701da177e4SLinus Torvalds 11711da177e4SLinus Torvalds /* Grab the block_device to prevent its destruction after we 11721da177e4SLinus Torvalds * put /dev/loopXX inode. Later in __loop_clr_fd() we bdput(bdev). 11731da177e4SLinus Torvalds */ 11741da177e4SLinus Torvalds bdgrab(bdev); 11756cc8e743SPavel Tatashin mutex_unlock(&lo->lo_mutex); 11761da177e4SLinus Torvalds if (partscan) 11771da177e4SLinus Torvalds loop_reread_partitions(lo, bdev); 117837c3fc9aSChristoph Hellwig if (!(mode & FMODE_EXCL)) 117937c3fc9aSChristoph Hellwig bd_abort_claiming(bdev, loop_configure); 11801da177e4SLinus Torvalds return 0; 11811da177e4SLinus Torvalds 11821da177e4SLinus Torvalds out_unlock: 11836cc8e743SPavel Tatashin mutex_unlock(&lo->lo_mutex); 11841da177e4SLinus Torvalds out_bdev: 118537c3fc9aSChristoph Hellwig if (!(mode & FMODE_EXCL)) 118637c3fc9aSChristoph Hellwig bd_abort_claiming(bdev, loop_configure); 11871da177e4SLinus Torvalds out_putf: 11881da177e4SLinus Torvalds fput(file); 11891da177e4SLinus Torvalds out: 11901da177e4SLinus Torvalds /* This is safe: open() is still holding a reference. */ 11911da177e4SLinus Torvalds module_put(THIS_MODULE); 11921da177e4SLinus Torvalds return error; 11931da177e4SLinus Torvalds } 11941da177e4SLinus Torvalds 11950da03cabSJan Kara static int __loop_clr_fd(struct loop_device *lo, bool release) 11961da177e4SLinus Torvalds { 11977ccd0791SJan Kara struct file *filp = NULL; 1198b4e3ca1aSAl Viro gfp_t gfp = lo->old_gfp_mask; 11994c823cc3SAyan George struct block_device *bdev = lo->lo_device; 12007ccd0791SJan Kara int err = 0; 12010da03cabSJan Kara bool partscan = false; 12020da03cabSJan Kara int lo_number; 12031da177e4SLinus Torvalds 12046cc8e743SPavel Tatashin mutex_lock(&lo->lo_mutex); 12057ccd0791SJan Kara if (WARN_ON_ONCE(lo->lo_state != Lo_rundown)) { 12067ccd0791SJan Kara err = -ENXIO; 12077ccd0791SJan Kara goto out_unlock; 12087ccd0791SJan Kara } 12091da177e4SLinus Torvalds 12107ccd0791SJan Kara filp = lo->lo_backing_file; 12117ccd0791SJan Kara if (filp == NULL) { 12127ccd0791SJan Kara err = -EINVAL; 12137ccd0791SJan Kara goto out_unlock; 12147ccd0791SJan Kara } 12151da177e4SLinus Torvalds 12164ceddce5SMauricio Faria de Oliveira if (test_bit(QUEUE_FLAG_WC, &lo->lo_queue->queue_flags)) 12174ceddce5SMauricio Faria de Oliveira blk_queue_write_cache(lo->lo_queue, false, false); 12184ceddce5SMauricio Faria de Oliveira 1219f8933667SMing Lei /* freeze request queue during the transition */ 1220f8933667SMing Lei blk_mq_freeze_queue(lo->lo_queue); 1221f8933667SMing Lei 12221da177e4SLinus Torvalds spin_lock_irq(&lo->lo_lock); 12231da177e4SLinus Torvalds lo->lo_backing_file = NULL; 122405eb0f25SKay Sievers spin_unlock_irq(&lo->lo_lock); 12251da177e4SLinus Torvalds 12261da177e4SLinus Torvalds loop_release_xfer(lo); 12271da177e4SLinus Torvalds lo->transfer = NULL; 12281da177e4SLinus Torvalds lo->ioctl = NULL; 12291da177e4SLinus Torvalds lo->lo_device = NULL; 12301da177e4SLinus Torvalds lo->lo_encryption = NULL; 12311da177e4SLinus Torvalds lo->lo_offset = 0; 12321da177e4SLinus Torvalds lo->lo_sizelimit = 0; 12331da177e4SLinus Torvalds lo->lo_encrypt_key_size = 0; 12341da177e4SLinus Torvalds memset(lo->lo_encrypt_key, 0, LO_KEY_SIZE); 12351da177e4SLinus Torvalds memset(lo->lo_crypt_name, 0, LO_NAME_SIZE); 12361da177e4SLinus Torvalds memset(lo->lo_file_name, 0, LO_NAME_SIZE); 123789e4fdecSOmar Sandoval blk_queue_logical_block_size(lo->lo_queue, 512); 1238bf093753SOmar Sandoval blk_queue_physical_block_size(lo->lo_queue, 512); 1239bf093753SOmar Sandoval blk_queue_io_min(lo->lo_queue, 512); 1240c1681bf8SAnatol Pomozov if (bdev) { 1241c1681bf8SAnatol Pomozov bdput(bdev); 1242f98393a6SPeter Zijlstra invalidate_bdev(bdev); 1243eedffa28SJeff Layton bdev->bd_inode->i_mapping->wb_err = 0; 1244c1681bf8SAnatol Pomozov } 124573285082SKen Chen set_capacity(lo->lo_disk, 0); 124651a0bb0cSMilan Broz loop_sysfs_exit(lo); 1247c3473c63SDavid Zeuthen if (bdev) { 1248c3473c63SDavid Zeuthen /* let user-space know about this change */ 1249c3473c63SDavid Zeuthen kobject_uevent(&disk_to_dev(bdev->bd_disk)->kobj, KOBJ_CHANGE); 1250c3473c63SDavid Zeuthen } 12511da177e4SLinus Torvalds mapping_set_gfp_mask(filp->f_mapping, gfp); 12521da177e4SLinus Torvalds /* This is safe: open() is still holding a reference. */ 12531da177e4SLinus Torvalds module_put(THIS_MODULE); 1254f8933667SMing Lei blk_mq_unfreeze_queue(lo->lo_queue); 1255f8933667SMing Lei 12560da03cabSJan Kara partscan = lo->lo_flags & LO_FLAGS_PARTSCAN && bdev; 12570da03cabSJan Kara lo_number = lo->lo_number; 12580da03cabSJan Kara loop_unprepare_queue(lo); 12590da03cabSJan Kara out_unlock: 12606cc8e743SPavel Tatashin mutex_unlock(&lo->lo_mutex); 12610da03cabSJan Kara if (partscan) { 1262d57f3374SJan Kara /* 1263a8698707SChristoph Hellwig * open_mutex has been held already in release path, so don't 1264d57f3374SJan Kara * acquire it if this function is called in such case. 1265d57f3374SJan Kara * 1266d57f3374SJan Kara * If the reread partition isn't from release path, lo_refcnt 1267d57f3374SJan Kara * must be at least one and it can only become zero when the 1268d57f3374SJan Kara * current holder is released. 1269d57f3374SJan Kara */ 1270f0b870dfSChristoph Hellwig if (!release) 1271a8698707SChristoph Hellwig mutex_lock(&bdev->bd_disk->open_mutex); 1272f0b870dfSChristoph Hellwig err = bdev_disk_changed(bdev, false); 1273f0b870dfSChristoph Hellwig if (!release) 1274a8698707SChristoph Hellwig mutex_unlock(&bdev->bd_disk->open_mutex); 127540853d6fSDongli Zhang if (err) 1276d57f3374SJan Kara pr_warn("%s: partition scan of loop%d failed (rc=%d)\n", 12770da03cabSJan Kara __func__, lo_number, err); 1278d57f3374SJan Kara /* Device is gone, no point in returning error */ 1279d57f3374SJan Kara err = 0; 1280d57f3374SJan Kara } 1281758a58d0SDongli Zhang 1282758a58d0SDongli Zhang /* 1283758a58d0SDongli Zhang * lo->lo_state is set to Lo_unbound here after above partscan has 1284758a58d0SDongli Zhang * finished. 1285758a58d0SDongli Zhang * 1286758a58d0SDongli Zhang * There cannot be anybody else entering __loop_clr_fd() as 1287758a58d0SDongli Zhang * lo->lo_backing_file is already cleared and Lo_rundown state 1288758a58d0SDongli Zhang * protects us from all the other places trying to change the 'lo' 1289758a58d0SDongli Zhang * device. 1290758a58d0SDongli Zhang */ 12916cc8e743SPavel Tatashin mutex_lock(&lo->lo_mutex); 1292758a58d0SDongli Zhang lo->lo_flags = 0; 1293758a58d0SDongli Zhang if (!part_shift) 1294758a58d0SDongli Zhang lo->lo_disk->flags |= GENHD_FL_NO_PART_SCAN; 1295758a58d0SDongli Zhang lo->lo_state = Lo_unbound; 12966cc8e743SPavel Tatashin mutex_unlock(&lo->lo_mutex); 1297758a58d0SDongli Zhang 1298f028f3b2SNikanth Karthikesan /* 12996cc8e743SPavel Tatashin * Need not hold lo_mutex to fput backing file. Calling fput holding 13006cc8e743SPavel Tatashin * lo_mutex triggers a circular lock dependency possibility warning as 1301a8698707SChristoph Hellwig * fput can take open_mutex which is usually taken before lo_mutex. 1302f028f3b2SNikanth Karthikesan */ 13037ccd0791SJan Kara if (filp) 1304f028f3b2SNikanth Karthikesan fput(filp); 13057ccd0791SJan Kara return err; 13061da177e4SLinus Torvalds } 13071da177e4SLinus Torvalds 1308a2505b79SJan Kara static int loop_clr_fd(struct loop_device *lo) 1309a2505b79SJan Kara { 13107ccd0791SJan Kara int err; 13117ccd0791SJan Kara 13126cc8e743SPavel Tatashin err = mutex_lock_killable(&lo->lo_mutex); 13137ccd0791SJan Kara if (err) 13147ccd0791SJan Kara return err; 13157ccd0791SJan Kara if (lo->lo_state != Lo_bound) { 13166cc8e743SPavel Tatashin mutex_unlock(&lo->lo_mutex); 1317a2505b79SJan Kara return -ENXIO; 13187ccd0791SJan Kara } 1319a2505b79SJan Kara /* 1320a2505b79SJan Kara * If we've explicitly asked to tear down the loop device, 1321a2505b79SJan Kara * and it has an elevated reference count, set it for auto-teardown when 1322a2505b79SJan Kara * the last reference goes away. This stops $!~#$@ udev from 1323a2505b79SJan Kara * preventing teardown because it decided that it needs to run blkid on 1324a2505b79SJan Kara * the loopback device whenever they appear. xfstests is notorious for 1325a2505b79SJan Kara * failing tests because blkid via udev races with a losetup 1326a2505b79SJan Kara * <dev>/do something like mkfs/losetup -d <dev> causing the losetup -d 1327a2505b79SJan Kara * command to fail with EBUSY. 1328a2505b79SJan Kara */ 1329a2505b79SJan Kara if (atomic_read(&lo->lo_refcnt) > 1) { 1330a2505b79SJan Kara lo->lo_flags |= LO_FLAGS_AUTOCLEAR; 13316cc8e743SPavel Tatashin mutex_unlock(&lo->lo_mutex); 1332a2505b79SJan Kara return 0; 1333a2505b79SJan Kara } 1334a2505b79SJan Kara lo->lo_state = Lo_rundown; 13356cc8e743SPavel Tatashin mutex_unlock(&lo->lo_mutex); 1336a2505b79SJan Kara 13370da03cabSJan Kara return __loop_clr_fd(lo, false); 1338a2505b79SJan Kara } 1339a2505b79SJan Kara 13401da177e4SLinus Torvalds static int 13411da177e4SLinus Torvalds loop_set_status(struct loop_device *lo, const struct loop_info64 *info) 13421da177e4SLinus Torvalds { 13431da177e4SLinus Torvalds int err; 134485b0a54aSJan Kara struct block_device *bdev; 13450c3796c2SMartijn Coenen kuid_t uid = current_uid(); 1346faf1d254SMartijn Coenen int prev_lo_flags; 134785b0a54aSJan Kara bool partscan = false; 13480c3796c2SMartijn Coenen bool size_changed = false; 13491da177e4SLinus Torvalds 13506cc8e743SPavel Tatashin err = mutex_lock_killable(&lo->lo_mutex); 1351550df5fdSJan Kara if (err) 1352550df5fdSJan Kara return err; 1353b0fafa81SDavid Howells if (lo->lo_encrypt_key_size && 1354e4849737SEric W. Biederman !uid_eq(lo->lo_key_owner, uid) && 1355550df5fdSJan Kara !capable(CAP_SYS_ADMIN)) { 1356550df5fdSJan Kara err = -EPERM; 1357550df5fdSJan Kara goto out_unlock; 1358550df5fdSJan Kara } 1359550df5fdSJan Kara if (lo->lo_state != Lo_bound) { 1360550df5fdSJan Kara err = -ENXIO; 1361550df5fdSJan Kara goto out_unlock; 1362550df5fdSJan Kara } 13631da177e4SLinus Torvalds 13645db470e2SJaegeuk Kim if (lo->lo_offset != info->lo_offset || 13655db470e2SJaegeuk Kim lo->lo_sizelimit != info->lo_sizelimit) { 13660c3796c2SMartijn Coenen size_changed = true; 13675db470e2SJaegeuk Kim sync_blockdev(lo->lo_device); 1368f4bd34b1SZheng Bin invalidate_bdev(lo->lo_device); 13695db470e2SJaegeuk Kim } 13705db470e2SJaegeuk Kim 1371ecdd0959SMing Lei /* I/O need to be drained during transfer transition */ 1372ecdd0959SMing Lei blk_mq_freeze_queue(lo->lo_queue); 1373ecdd0959SMing Lei 13740c3796c2SMartijn Coenen if (size_changed && lo->lo_device->bd_inode->i_mapping->nrpages) { 1375f4bd34b1SZheng Bin /* If any pages were dirtied after invalidate_bdev(), try again */ 13765db470e2SJaegeuk Kim err = -EAGAIN; 13775db470e2SJaegeuk Kim pr_warn("%s: loop%d (%s) has still dirty pages (nrpages=%lu)\n", 13785db470e2SJaegeuk Kim __func__, lo->lo_number, lo->lo_file_name, 13795db470e2SJaegeuk Kim lo->lo_device->bd_inode->i_mapping->nrpages); 13805db470e2SJaegeuk Kim goto out_unfreeze; 13815db470e2SJaegeuk Kim } 13820c3796c2SMartijn Coenen 1383faf1d254SMartijn Coenen prev_lo_flags = lo->lo_flags; 1384faf1d254SMartijn Coenen 13850c3796c2SMartijn Coenen err = loop_set_status_from_info(lo, info); 13860c3796c2SMartijn Coenen if (err) 1387550df5fdSJan Kara goto out_unfreeze; 13880c3796c2SMartijn Coenen 1389faf1d254SMartijn Coenen /* Mask out flags that can't be set using LOOP_SET_STATUS. */ 13906ac92fb5SMartijn Coenen lo->lo_flags &= LOOP_SET_STATUS_SETTABLE_FLAGS; 1391faf1d254SMartijn Coenen /* For those flags, use the previous values instead */ 1392faf1d254SMartijn Coenen lo->lo_flags |= prev_lo_flags & ~LOOP_SET_STATUS_SETTABLE_FLAGS; 1393faf1d254SMartijn Coenen /* For flags that can't be cleared, use previous values too */ 1394faf1d254SMartijn Coenen lo->lo_flags |= prev_lo_flags & ~LOOP_SET_STATUS_CLEARABLE_FLAGS; 1395faf1d254SMartijn Coenen 1396b0bd158dSMartijn Coenen if (size_changed) { 1397b0bd158dSMartijn Coenen loff_t new_size = get_size(lo->lo_offset, lo->lo_sizelimit, 1398b0bd158dSMartijn Coenen lo->lo_backing_file); 1399b0bd158dSMartijn Coenen loop_set_size(lo, new_size); 1400b040ad9cSArnd Bergmann } 1401541c742aSGuo Chao 1402dfaa2ef6SLukas Czerner loop_config_discard(lo); 14031da177e4SLinus Torvalds 14042e5ab5f3SMing Lei /* update dio if lo_offset or transfer is changed */ 14052e5ab5f3SMing Lei __loop_update_dio(lo, lo->use_dio); 14062e5ab5f3SMing Lei 1407550df5fdSJan Kara out_unfreeze: 1408ecdd0959SMing Lei blk_mq_unfreeze_queue(lo->lo_queue); 1409e02898b4SOmar Sandoval 1410faf1d254SMartijn Coenen if (!err && (lo->lo_flags & LO_FLAGS_PARTSCAN) && 1411faf1d254SMartijn Coenen !(prev_lo_flags & LO_FLAGS_PARTSCAN)) { 1412e02898b4SOmar Sandoval lo->lo_disk->flags &= ~GENHD_FL_NO_PART_SCAN; 141385b0a54aSJan Kara bdev = lo->lo_device; 141485b0a54aSJan Kara partscan = true; 1415e02898b4SOmar Sandoval } 1416550df5fdSJan Kara out_unlock: 14176cc8e743SPavel Tatashin mutex_unlock(&lo->lo_mutex); 141885b0a54aSJan Kara if (partscan) 141985b0a54aSJan Kara loop_reread_partitions(lo, bdev); 1420e02898b4SOmar Sandoval 1421ecdd0959SMing Lei return err; 14221da177e4SLinus Torvalds } 14231da177e4SLinus Torvalds 14241da177e4SLinus Torvalds static int 14251da177e4SLinus Torvalds loop_get_status(struct loop_device *lo, struct loop_info64 *info) 14261da177e4SLinus Torvalds { 1427b1ab5fa3STetsuo Handa struct path path; 14281da177e4SLinus Torvalds struct kstat stat; 14292d1d4c1eSOmar Sandoval int ret; 14301da177e4SLinus Torvalds 14316cc8e743SPavel Tatashin ret = mutex_lock_killable(&lo->lo_mutex); 14324a5ce9baSJan Kara if (ret) 14334a5ce9baSJan Kara return ret; 14342d1d4c1eSOmar Sandoval if (lo->lo_state != Lo_bound) { 14356cc8e743SPavel Tatashin mutex_unlock(&lo->lo_mutex); 14361da177e4SLinus Torvalds return -ENXIO; 14372d1d4c1eSOmar Sandoval } 14382d1d4c1eSOmar Sandoval 14391da177e4SLinus Torvalds memset(info, 0, sizeof(*info)); 14401da177e4SLinus Torvalds info->lo_number = lo->lo_number; 14411da177e4SLinus Torvalds info->lo_offset = lo->lo_offset; 14421da177e4SLinus Torvalds info->lo_sizelimit = lo->lo_sizelimit; 14431da177e4SLinus Torvalds info->lo_flags = lo->lo_flags; 14441da177e4SLinus Torvalds memcpy(info->lo_file_name, lo->lo_file_name, LO_NAME_SIZE); 14451da177e4SLinus Torvalds memcpy(info->lo_crypt_name, lo->lo_crypt_name, LO_NAME_SIZE); 14461da177e4SLinus Torvalds info->lo_encrypt_type = 14471da177e4SLinus Torvalds lo->lo_encryption ? lo->lo_encryption->number : 0; 14481da177e4SLinus Torvalds if (lo->lo_encrypt_key_size && capable(CAP_SYS_ADMIN)) { 14491da177e4SLinus Torvalds info->lo_encrypt_key_size = lo->lo_encrypt_key_size; 14501da177e4SLinus Torvalds memcpy(info->lo_encrypt_key, lo->lo_encrypt_key, 14511da177e4SLinus Torvalds lo->lo_encrypt_key_size); 14521da177e4SLinus Torvalds } 14532d1d4c1eSOmar Sandoval 14546cc8e743SPavel Tatashin /* Drop lo_mutex while we call into the filesystem. */ 1455b1ab5fa3STetsuo Handa path = lo->lo_backing_file->f_path; 1456b1ab5fa3STetsuo Handa path_get(&path); 14576cc8e743SPavel Tatashin mutex_unlock(&lo->lo_mutex); 1458b1ab5fa3STetsuo Handa ret = vfs_getattr(&path, &stat, STATX_INO, AT_STATX_SYNC_AS_STAT); 14592d1d4c1eSOmar Sandoval if (!ret) { 14602d1d4c1eSOmar Sandoval info->lo_device = huge_encode_dev(stat.dev); 14612d1d4c1eSOmar Sandoval info->lo_inode = stat.ino; 14622d1d4c1eSOmar Sandoval info->lo_rdevice = huge_encode_dev(stat.rdev); 14632d1d4c1eSOmar Sandoval } 1464b1ab5fa3STetsuo Handa path_put(&path); 14652d1d4c1eSOmar Sandoval return ret; 14661da177e4SLinus Torvalds } 14671da177e4SLinus Torvalds 14681da177e4SLinus Torvalds static void 14691da177e4SLinus Torvalds loop_info64_from_old(const struct loop_info *info, struct loop_info64 *info64) 14701da177e4SLinus Torvalds { 14711da177e4SLinus Torvalds memset(info64, 0, sizeof(*info64)); 14721da177e4SLinus Torvalds info64->lo_number = info->lo_number; 14731da177e4SLinus Torvalds info64->lo_device = info->lo_device; 14741da177e4SLinus Torvalds info64->lo_inode = info->lo_inode; 14751da177e4SLinus Torvalds info64->lo_rdevice = info->lo_rdevice; 14761da177e4SLinus Torvalds info64->lo_offset = info->lo_offset; 14771da177e4SLinus Torvalds info64->lo_sizelimit = 0; 14781da177e4SLinus Torvalds info64->lo_encrypt_type = info->lo_encrypt_type; 14791da177e4SLinus Torvalds info64->lo_encrypt_key_size = info->lo_encrypt_key_size; 14801da177e4SLinus Torvalds info64->lo_flags = info->lo_flags; 14811da177e4SLinus Torvalds info64->lo_init[0] = info->lo_init[0]; 14821da177e4SLinus Torvalds info64->lo_init[1] = info->lo_init[1]; 14831da177e4SLinus Torvalds if (info->lo_encrypt_type == LO_CRYPT_CRYPTOAPI) 14841da177e4SLinus Torvalds memcpy(info64->lo_crypt_name, info->lo_name, LO_NAME_SIZE); 14851da177e4SLinus Torvalds else 14861da177e4SLinus Torvalds memcpy(info64->lo_file_name, info->lo_name, LO_NAME_SIZE); 14871da177e4SLinus Torvalds memcpy(info64->lo_encrypt_key, info->lo_encrypt_key, LO_KEY_SIZE); 14881da177e4SLinus Torvalds } 14891da177e4SLinus Torvalds 14901da177e4SLinus Torvalds static int 14911da177e4SLinus Torvalds loop_info64_to_old(const struct loop_info64 *info64, struct loop_info *info) 14921da177e4SLinus Torvalds { 14931da177e4SLinus Torvalds memset(info, 0, sizeof(*info)); 14941da177e4SLinus Torvalds info->lo_number = info64->lo_number; 14951da177e4SLinus Torvalds info->lo_device = info64->lo_device; 14961da177e4SLinus Torvalds info->lo_inode = info64->lo_inode; 14971da177e4SLinus Torvalds info->lo_rdevice = info64->lo_rdevice; 14981da177e4SLinus Torvalds info->lo_offset = info64->lo_offset; 14991da177e4SLinus Torvalds info->lo_encrypt_type = info64->lo_encrypt_type; 15001da177e4SLinus Torvalds info->lo_encrypt_key_size = info64->lo_encrypt_key_size; 15011da177e4SLinus Torvalds info->lo_flags = info64->lo_flags; 15021da177e4SLinus Torvalds info->lo_init[0] = info64->lo_init[0]; 15031da177e4SLinus Torvalds info->lo_init[1] = info64->lo_init[1]; 15041da177e4SLinus Torvalds if (info->lo_encrypt_type == LO_CRYPT_CRYPTOAPI) 15051da177e4SLinus Torvalds memcpy(info->lo_name, info64->lo_crypt_name, LO_NAME_SIZE); 15061da177e4SLinus Torvalds else 15071da177e4SLinus Torvalds memcpy(info->lo_name, info64->lo_file_name, LO_NAME_SIZE); 15081da177e4SLinus Torvalds memcpy(info->lo_encrypt_key, info64->lo_encrypt_key, LO_KEY_SIZE); 15091da177e4SLinus Torvalds 15101da177e4SLinus Torvalds /* error in case values were truncated */ 15111da177e4SLinus Torvalds if (info->lo_device != info64->lo_device || 15121da177e4SLinus Torvalds info->lo_rdevice != info64->lo_rdevice || 15131da177e4SLinus Torvalds info->lo_inode != info64->lo_inode || 15141da177e4SLinus Torvalds info->lo_offset != info64->lo_offset) 15151da177e4SLinus Torvalds return -EOVERFLOW; 15161da177e4SLinus Torvalds 15171da177e4SLinus Torvalds return 0; 15181da177e4SLinus Torvalds } 15191da177e4SLinus Torvalds 15201da177e4SLinus Torvalds static int 15211da177e4SLinus Torvalds loop_set_status_old(struct loop_device *lo, const struct loop_info __user *arg) 15221da177e4SLinus Torvalds { 15231da177e4SLinus Torvalds struct loop_info info; 15241da177e4SLinus Torvalds struct loop_info64 info64; 15251da177e4SLinus Torvalds 15261da177e4SLinus Torvalds if (copy_from_user(&info, arg, sizeof (struct loop_info))) 15271da177e4SLinus Torvalds return -EFAULT; 15281da177e4SLinus Torvalds loop_info64_from_old(&info, &info64); 15291da177e4SLinus Torvalds return loop_set_status(lo, &info64); 15301da177e4SLinus Torvalds } 15311da177e4SLinus Torvalds 15321da177e4SLinus Torvalds static int 15331da177e4SLinus Torvalds loop_set_status64(struct loop_device *lo, const struct loop_info64 __user *arg) 15341da177e4SLinus Torvalds { 15351da177e4SLinus Torvalds struct loop_info64 info64; 15361da177e4SLinus Torvalds 15371da177e4SLinus Torvalds if (copy_from_user(&info64, arg, sizeof (struct loop_info64))) 15381da177e4SLinus Torvalds return -EFAULT; 15391da177e4SLinus Torvalds return loop_set_status(lo, &info64); 15401da177e4SLinus Torvalds } 15411da177e4SLinus Torvalds 15421da177e4SLinus Torvalds static int 15431da177e4SLinus Torvalds loop_get_status_old(struct loop_device *lo, struct loop_info __user *arg) { 15441da177e4SLinus Torvalds struct loop_info info; 15451da177e4SLinus Torvalds struct loop_info64 info64; 1546bdac616dSOmar Sandoval int err; 15471da177e4SLinus Torvalds 15484a5ce9baSJan Kara if (!arg) 1549bdac616dSOmar Sandoval return -EINVAL; 15501da177e4SLinus Torvalds err = loop_get_status(lo, &info64); 15511da177e4SLinus Torvalds if (!err) 15521da177e4SLinus Torvalds err = loop_info64_to_old(&info64, &info); 15531da177e4SLinus Torvalds if (!err && copy_to_user(arg, &info, sizeof(info))) 15541da177e4SLinus Torvalds err = -EFAULT; 15551da177e4SLinus Torvalds 15561da177e4SLinus Torvalds return err; 15571da177e4SLinus Torvalds } 15581da177e4SLinus Torvalds 15591da177e4SLinus Torvalds static int 15601da177e4SLinus Torvalds loop_get_status64(struct loop_device *lo, struct loop_info64 __user *arg) { 15611da177e4SLinus Torvalds struct loop_info64 info64; 1562bdac616dSOmar Sandoval int err; 15631da177e4SLinus Torvalds 15644a5ce9baSJan Kara if (!arg) 1565bdac616dSOmar Sandoval return -EINVAL; 15661da177e4SLinus Torvalds err = loop_get_status(lo, &info64); 15671da177e4SLinus Torvalds if (!err && copy_to_user(arg, &info64, sizeof(info64))) 15681da177e4SLinus Torvalds err = -EFAULT; 15691da177e4SLinus Torvalds 15701da177e4SLinus Torvalds return err; 15711da177e4SLinus Torvalds } 15721da177e4SLinus Torvalds 157351001b7dSHannes Reinecke static int loop_set_capacity(struct loop_device *lo) 157453d66608SJ. R. Okajima { 15750a6ed1b5SMartijn Coenen loff_t size; 15760a6ed1b5SMartijn Coenen 157753d66608SJ. R. Okajima if (unlikely(lo->lo_state != Lo_bound)) 15787b0576a3SGuo Chao return -ENXIO; 157953d66608SJ. R. Okajima 15800a6ed1b5SMartijn Coenen size = get_loop_size(lo, lo->lo_backing_file); 15810a6ed1b5SMartijn Coenen loop_set_size(lo, size); 1582083a6a50SMartijn Coenen 1583083a6a50SMartijn Coenen return 0; 158453d66608SJ. R. Okajima } 158553d66608SJ. R. Okajima 1586ab1cb278SMing Lei static int loop_set_dio(struct loop_device *lo, unsigned long arg) 1587ab1cb278SMing Lei { 1588ab1cb278SMing Lei int error = -ENXIO; 1589ab1cb278SMing Lei if (lo->lo_state != Lo_bound) 1590ab1cb278SMing Lei goto out; 1591ab1cb278SMing Lei 1592ab1cb278SMing Lei __loop_update_dio(lo, !!arg); 1593ab1cb278SMing Lei if (lo->use_dio == !!arg) 1594ab1cb278SMing Lei return 0; 1595ab1cb278SMing Lei error = -EINVAL; 1596ab1cb278SMing Lei out: 1597ab1cb278SMing Lei return error; 1598ab1cb278SMing Lei } 1599ab1cb278SMing Lei 160089e4fdecSOmar Sandoval static int loop_set_block_size(struct loop_device *lo, unsigned long arg) 160189e4fdecSOmar Sandoval { 16025db470e2SJaegeuk Kim int err = 0; 16035db470e2SJaegeuk Kim 160489e4fdecSOmar Sandoval if (lo->lo_state != Lo_bound) 160589e4fdecSOmar Sandoval return -ENXIO; 160689e4fdecSOmar Sandoval 16073448914eSMartijn Coenen err = loop_validate_block_size(arg); 16083448914eSMartijn Coenen if (err) 16093448914eSMartijn Coenen return err; 161089e4fdecSOmar Sandoval 16117e81f99aSMartijn Coenen if (lo->lo_queue->limits.logical_block_size == arg) 16127e81f99aSMartijn Coenen return 0; 16137e81f99aSMartijn Coenen 16145db470e2SJaegeuk Kim sync_blockdev(lo->lo_device); 1615f4bd34b1SZheng Bin invalidate_bdev(lo->lo_device); 16165db470e2SJaegeuk Kim 161789e4fdecSOmar Sandoval blk_mq_freeze_queue(lo->lo_queue); 161889e4fdecSOmar Sandoval 1619f4bd34b1SZheng Bin /* invalidate_bdev should have truncated all the pages */ 16207e81f99aSMartijn Coenen if (lo->lo_device->bd_inode->i_mapping->nrpages) { 16215db470e2SJaegeuk Kim err = -EAGAIN; 16225db470e2SJaegeuk Kim pr_warn("%s: loop%d (%s) has still dirty pages (nrpages=%lu)\n", 16235db470e2SJaegeuk Kim __func__, lo->lo_number, lo->lo_file_name, 16245db470e2SJaegeuk Kim lo->lo_device->bd_inode->i_mapping->nrpages); 16255db470e2SJaegeuk Kim goto out_unfreeze; 16265db470e2SJaegeuk Kim } 16275db470e2SJaegeuk Kim 162889e4fdecSOmar Sandoval blk_queue_logical_block_size(lo->lo_queue, arg); 1629bf093753SOmar Sandoval blk_queue_physical_block_size(lo->lo_queue, arg); 1630bf093753SOmar Sandoval blk_queue_io_min(lo->lo_queue, arg); 163189e4fdecSOmar Sandoval loop_update_dio(lo); 16325db470e2SJaegeuk Kim out_unfreeze: 163389e4fdecSOmar Sandoval blk_mq_unfreeze_queue(lo->lo_queue); 163489e4fdecSOmar Sandoval 16355db470e2SJaegeuk Kim return err; 163689e4fdecSOmar Sandoval } 163789e4fdecSOmar Sandoval 1638a1316544SJan Kara static int lo_simple_ioctl(struct loop_device *lo, unsigned int cmd, 1639a1316544SJan Kara unsigned long arg) 16401da177e4SLinus Torvalds { 16411da177e4SLinus Torvalds int err; 16421da177e4SLinus Torvalds 16436cc8e743SPavel Tatashin err = mutex_lock_killable(&lo->lo_mutex); 16443148ffbdSOmar Sandoval if (err) 1645a1316544SJan Kara return err; 16461da177e4SLinus Torvalds switch (cmd) { 164753d66608SJ. R. Okajima case LOOP_SET_CAPACITY: 164851001b7dSHannes Reinecke err = loop_set_capacity(lo); 164953d66608SJ. R. Okajima break; 1650ab1cb278SMing Lei case LOOP_SET_DIRECT_IO: 1651ab1cb278SMing Lei err = loop_set_dio(lo, arg); 1652ab1cb278SMing Lei break; 165389e4fdecSOmar Sandoval case LOOP_SET_BLOCK_SIZE: 165489e4fdecSOmar Sandoval err = loop_set_block_size(lo, arg); 165589e4fdecSOmar Sandoval break; 16561da177e4SLinus Torvalds default: 16571da177e4SLinus Torvalds err = lo->ioctl ? lo->ioctl(lo, cmd, arg) : -EINVAL; 16581da177e4SLinus Torvalds } 16596cc8e743SPavel Tatashin mutex_unlock(&lo->lo_mutex); 1660a1316544SJan Kara return err; 1661a1316544SJan Kara } 1662f028f3b2SNikanth Karthikesan 1663a1316544SJan Kara static int lo_ioctl(struct block_device *bdev, fmode_t mode, 1664a1316544SJan Kara unsigned int cmd, unsigned long arg) 1665a1316544SJan Kara { 1666a1316544SJan Kara struct loop_device *lo = bdev->bd_disk->private_data; 1667571fae6eSMartijn Coenen void __user *argp = (void __user *) arg; 1668a1316544SJan Kara int err; 1669a1316544SJan Kara 1670a1316544SJan Kara switch (cmd) { 16713448914eSMartijn Coenen case LOOP_SET_FD: { 16723448914eSMartijn Coenen /* 16733448914eSMartijn Coenen * Legacy case - pass in a zeroed out struct loop_config with 16743448914eSMartijn Coenen * only the file descriptor set , which corresponds with the 16753448914eSMartijn Coenen * default parameters we'd have used otherwise. 16763448914eSMartijn Coenen */ 16773448914eSMartijn Coenen struct loop_config config; 16783448914eSMartijn Coenen 16793448914eSMartijn Coenen memset(&config, 0, sizeof(config)); 16803448914eSMartijn Coenen config.fd = arg; 16813448914eSMartijn Coenen 16823448914eSMartijn Coenen return loop_configure(lo, mode, bdev, &config); 16833448914eSMartijn Coenen } 16843448914eSMartijn Coenen case LOOP_CONFIGURE: { 16853448914eSMartijn Coenen struct loop_config config; 16863448914eSMartijn Coenen 16873448914eSMartijn Coenen if (copy_from_user(&config, argp, sizeof(config))) 16883448914eSMartijn Coenen return -EFAULT; 16893448914eSMartijn Coenen 16903448914eSMartijn Coenen return loop_configure(lo, mode, bdev, &config); 16913448914eSMartijn Coenen } 1692a1316544SJan Kara case LOOP_CHANGE_FD: 1693c3710770SJan Kara return loop_change_fd(lo, bdev, arg); 1694a1316544SJan Kara case LOOP_CLR_FD: 16957ccd0791SJan Kara return loop_clr_fd(lo); 1696a1316544SJan Kara case LOOP_SET_STATUS: 1697a1316544SJan Kara err = -EPERM; 1698a1316544SJan Kara if ((mode & FMODE_WRITE) || capable(CAP_SYS_ADMIN)) { 1699571fae6eSMartijn Coenen err = loop_set_status_old(lo, argp); 1700a1316544SJan Kara } 1701a1316544SJan Kara break; 1702a1316544SJan Kara case LOOP_GET_STATUS: 1703571fae6eSMartijn Coenen return loop_get_status_old(lo, argp); 1704a1316544SJan Kara case LOOP_SET_STATUS64: 1705a1316544SJan Kara err = -EPERM; 1706a1316544SJan Kara if ((mode & FMODE_WRITE) || capable(CAP_SYS_ADMIN)) { 1707571fae6eSMartijn Coenen err = loop_set_status64(lo, argp); 1708a1316544SJan Kara } 1709a1316544SJan Kara break; 1710a1316544SJan Kara case LOOP_GET_STATUS64: 1711571fae6eSMartijn Coenen return loop_get_status64(lo, argp); 1712a1316544SJan Kara case LOOP_SET_CAPACITY: 1713a1316544SJan Kara case LOOP_SET_DIRECT_IO: 1714a1316544SJan Kara case LOOP_SET_BLOCK_SIZE: 1715a1316544SJan Kara if (!(mode & FMODE_WRITE) && !capable(CAP_SYS_ADMIN)) 1716a1316544SJan Kara return -EPERM; 1717df561f66SGustavo A. R. Silva fallthrough; 1718a1316544SJan Kara default: 1719a1316544SJan Kara err = lo_simple_ioctl(lo, cmd, arg); 1720a1316544SJan Kara break; 1721a1316544SJan Kara } 1722a1316544SJan Kara 17231da177e4SLinus Torvalds return err; 17241da177e4SLinus Torvalds } 17251da177e4SLinus Torvalds 1726863d5b82SDavid Howells #ifdef CONFIG_COMPAT 1727863d5b82SDavid Howells struct compat_loop_info { 1728863d5b82SDavid Howells compat_int_t lo_number; /* ioctl r/o */ 1729863d5b82SDavid Howells compat_dev_t lo_device; /* ioctl r/o */ 1730863d5b82SDavid Howells compat_ulong_t lo_inode; /* ioctl r/o */ 1731863d5b82SDavid Howells compat_dev_t lo_rdevice; /* ioctl r/o */ 1732863d5b82SDavid Howells compat_int_t lo_offset; 1733863d5b82SDavid Howells compat_int_t lo_encrypt_type; 1734863d5b82SDavid Howells compat_int_t lo_encrypt_key_size; /* ioctl w/o */ 1735863d5b82SDavid Howells compat_int_t lo_flags; /* ioctl r/o */ 1736863d5b82SDavid Howells char lo_name[LO_NAME_SIZE]; 1737863d5b82SDavid Howells unsigned char lo_encrypt_key[LO_KEY_SIZE]; /* ioctl w/o */ 1738863d5b82SDavid Howells compat_ulong_t lo_init[2]; 1739863d5b82SDavid Howells char reserved[4]; 1740863d5b82SDavid Howells }; 1741863d5b82SDavid Howells 1742863d5b82SDavid Howells /* 1743863d5b82SDavid Howells * Transfer 32-bit compatibility structure in userspace to 64-bit loop info 1744863d5b82SDavid Howells * - noinlined to reduce stack space usage in main part of driver 1745863d5b82SDavid Howells */ 1746863d5b82SDavid Howells static noinline int 1747ba674cfcSAl Viro loop_info64_from_compat(const struct compat_loop_info __user *arg, 1748863d5b82SDavid Howells struct loop_info64 *info64) 1749863d5b82SDavid Howells { 1750863d5b82SDavid Howells struct compat_loop_info info; 1751863d5b82SDavid Howells 1752863d5b82SDavid Howells if (copy_from_user(&info, arg, sizeof(info))) 1753863d5b82SDavid Howells return -EFAULT; 1754863d5b82SDavid Howells 1755863d5b82SDavid Howells memset(info64, 0, sizeof(*info64)); 1756863d5b82SDavid Howells info64->lo_number = info.lo_number; 1757863d5b82SDavid Howells info64->lo_device = info.lo_device; 1758863d5b82SDavid Howells info64->lo_inode = info.lo_inode; 1759863d5b82SDavid Howells info64->lo_rdevice = info.lo_rdevice; 1760863d5b82SDavid Howells info64->lo_offset = info.lo_offset; 1761863d5b82SDavid Howells info64->lo_sizelimit = 0; 1762863d5b82SDavid Howells info64->lo_encrypt_type = info.lo_encrypt_type; 1763863d5b82SDavid Howells info64->lo_encrypt_key_size = info.lo_encrypt_key_size; 1764863d5b82SDavid Howells info64->lo_flags = info.lo_flags; 1765863d5b82SDavid Howells info64->lo_init[0] = info.lo_init[0]; 1766863d5b82SDavid Howells info64->lo_init[1] = info.lo_init[1]; 1767863d5b82SDavid Howells if (info.lo_encrypt_type == LO_CRYPT_CRYPTOAPI) 1768863d5b82SDavid Howells memcpy(info64->lo_crypt_name, info.lo_name, LO_NAME_SIZE); 1769863d5b82SDavid Howells else 1770863d5b82SDavid Howells memcpy(info64->lo_file_name, info.lo_name, LO_NAME_SIZE); 1771863d5b82SDavid Howells memcpy(info64->lo_encrypt_key, info.lo_encrypt_key, LO_KEY_SIZE); 1772863d5b82SDavid Howells return 0; 1773863d5b82SDavid Howells } 1774863d5b82SDavid Howells 1775863d5b82SDavid Howells /* 1776863d5b82SDavid Howells * Transfer 64-bit loop info to 32-bit compatibility structure in userspace 1777863d5b82SDavid Howells * - noinlined to reduce stack space usage in main part of driver 1778863d5b82SDavid Howells */ 1779863d5b82SDavid Howells static noinline int 1780863d5b82SDavid Howells loop_info64_to_compat(const struct loop_info64 *info64, 1781863d5b82SDavid Howells struct compat_loop_info __user *arg) 1782863d5b82SDavid Howells { 1783863d5b82SDavid Howells struct compat_loop_info info; 1784863d5b82SDavid Howells 1785863d5b82SDavid Howells memset(&info, 0, sizeof(info)); 1786863d5b82SDavid Howells info.lo_number = info64->lo_number; 1787863d5b82SDavid Howells info.lo_device = info64->lo_device; 1788863d5b82SDavid Howells info.lo_inode = info64->lo_inode; 1789863d5b82SDavid Howells info.lo_rdevice = info64->lo_rdevice; 1790863d5b82SDavid Howells info.lo_offset = info64->lo_offset; 1791863d5b82SDavid Howells info.lo_encrypt_type = info64->lo_encrypt_type; 1792863d5b82SDavid Howells info.lo_encrypt_key_size = info64->lo_encrypt_key_size; 1793863d5b82SDavid Howells info.lo_flags = info64->lo_flags; 1794863d5b82SDavid Howells info.lo_init[0] = info64->lo_init[0]; 1795863d5b82SDavid Howells info.lo_init[1] = info64->lo_init[1]; 1796863d5b82SDavid Howells if (info.lo_encrypt_type == LO_CRYPT_CRYPTOAPI) 1797863d5b82SDavid Howells memcpy(info.lo_name, info64->lo_crypt_name, LO_NAME_SIZE); 1798863d5b82SDavid Howells else 1799863d5b82SDavid Howells memcpy(info.lo_name, info64->lo_file_name, LO_NAME_SIZE); 1800863d5b82SDavid Howells memcpy(info.lo_encrypt_key, info64->lo_encrypt_key, LO_KEY_SIZE); 1801863d5b82SDavid Howells 1802863d5b82SDavid Howells /* error in case values were truncated */ 1803863d5b82SDavid Howells if (info.lo_device != info64->lo_device || 1804863d5b82SDavid Howells info.lo_rdevice != info64->lo_rdevice || 1805863d5b82SDavid Howells info.lo_inode != info64->lo_inode || 1806863d5b82SDavid Howells info.lo_offset != info64->lo_offset || 1807863d5b82SDavid Howells info.lo_init[0] != info64->lo_init[0] || 1808863d5b82SDavid Howells info.lo_init[1] != info64->lo_init[1]) 1809863d5b82SDavid Howells return -EOVERFLOW; 1810863d5b82SDavid Howells 1811863d5b82SDavid Howells if (copy_to_user(arg, &info, sizeof(info))) 1812863d5b82SDavid Howells return -EFAULT; 1813863d5b82SDavid Howells return 0; 1814863d5b82SDavid Howells } 1815863d5b82SDavid Howells 1816863d5b82SDavid Howells static int 1817863d5b82SDavid Howells loop_set_status_compat(struct loop_device *lo, 1818863d5b82SDavid Howells const struct compat_loop_info __user *arg) 1819863d5b82SDavid Howells { 1820863d5b82SDavid Howells struct loop_info64 info64; 1821863d5b82SDavid Howells int ret; 1822863d5b82SDavid Howells 1823863d5b82SDavid Howells ret = loop_info64_from_compat(arg, &info64); 1824863d5b82SDavid Howells if (ret < 0) 1825863d5b82SDavid Howells return ret; 1826863d5b82SDavid Howells return loop_set_status(lo, &info64); 1827863d5b82SDavid Howells } 1828863d5b82SDavid Howells 1829863d5b82SDavid Howells static int 1830863d5b82SDavid Howells loop_get_status_compat(struct loop_device *lo, 1831863d5b82SDavid Howells struct compat_loop_info __user *arg) 1832863d5b82SDavid Howells { 1833863d5b82SDavid Howells struct loop_info64 info64; 1834bdac616dSOmar Sandoval int err; 1835863d5b82SDavid Howells 18364a5ce9baSJan Kara if (!arg) 1837bdac616dSOmar Sandoval return -EINVAL; 1838863d5b82SDavid Howells err = loop_get_status(lo, &info64); 1839863d5b82SDavid Howells if (!err) 1840863d5b82SDavid Howells err = loop_info64_to_compat(&info64, arg); 1841863d5b82SDavid Howells return err; 1842863d5b82SDavid Howells } 1843863d5b82SDavid Howells 1844bb214884SAl Viro static int lo_compat_ioctl(struct block_device *bdev, fmode_t mode, 1845bb214884SAl Viro unsigned int cmd, unsigned long arg) 1846863d5b82SDavid Howells { 1847bb214884SAl Viro struct loop_device *lo = bdev->bd_disk->private_data; 1848863d5b82SDavid Howells int err; 1849863d5b82SDavid Howells 1850863d5b82SDavid Howells switch(cmd) { 1851863d5b82SDavid Howells case LOOP_SET_STATUS: 18523148ffbdSOmar Sandoval err = loop_set_status_compat(lo, 18533148ffbdSOmar Sandoval (const struct compat_loop_info __user *)arg); 1854863d5b82SDavid Howells break; 1855863d5b82SDavid Howells case LOOP_GET_STATUS: 18563148ffbdSOmar Sandoval err = loop_get_status_compat(lo, 18573148ffbdSOmar Sandoval (struct compat_loop_info __user *)arg); 1858863d5b82SDavid Howells break; 185953d66608SJ. R. Okajima case LOOP_SET_CAPACITY: 1860863d5b82SDavid Howells case LOOP_CLR_FD: 1861863d5b82SDavid Howells case LOOP_GET_STATUS64: 1862863d5b82SDavid Howells case LOOP_SET_STATUS64: 18633448914eSMartijn Coenen case LOOP_CONFIGURE: 1864863d5b82SDavid Howells arg = (unsigned long) compat_ptr(arg); 1865df561f66SGustavo A. R. Silva fallthrough; 1866863d5b82SDavid Howells case LOOP_SET_FD: 1867863d5b82SDavid Howells case LOOP_CHANGE_FD: 18689fea4b39SEvan Green case LOOP_SET_BLOCK_SIZE: 1869fdbe4eeeSAlessio Balsini case LOOP_SET_DIRECT_IO: 1870bb214884SAl Viro err = lo_ioctl(bdev, mode, cmd, arg); 1871863d5b82SDavid Howells break; 1872863d5b82SDavid Howells default: 1873863d5b82SDavid Howells err = -ENOIOCTLCMD; 1874863d5b82SDavid Howells break; 1875863d5b82SDavid Howells } 1876863d5b82SDavid Howells return err; 1877863d5b82SDavid Howells } 1878863d5b82SDavid Howells #endif 1879863d5b82SDavid Howells 1880bb214884SAl Viro static int lo_open(struct block_device *bdev, fmode_t mode) 18811da177e4SLinus Torvalds { 1882770fe30aSKay Sievers struct loop_device *lo; 18830a42e99bSJan Kara int err; 1884770fe30aSKay Sievers 18856cc8e743SPavel Tatashin /* 18866cc8e743SPavel Tatashin * take loop_ctl_mutex to protect lo pointer from race with 18876cc8e743SPavel Tatashin * loop_control_ioctl(LOOP_CTL_REMOVE), however, to reduce contention 18886cc8e743SPavel Tatashin * release it prior to updating lo->lo_refcnt. 18896cc8e743SPavel Tatashin */ 18900a42e99bSJan Kara err = mutex_lock_killable(&loop_ctl_mutex); 18910a42e99bSJan Kara if (err) 18920a42e99bSJan Kara return err; 1893770fe30aSKay Sievers lo = bdev->bd_disk->private_data; 1894770fe30aSKay Sievers if (!lo) { 18950a42e99bSJan Kara mutex_unlock(&loop_ctl_mutex); 18966cc8e743SPavel Tatashin return -ENXIO; 18976cc8e743SPavel Tatashin } 18986cc8e743SPavel Tatashin err = mutex_lock_killable(&lo->lo_mutex); 18996cc8e743SPavel Tatashin mutex_unlock(&loop_ctl_mutex); 19006cc8e743SPavel Tatashin if (err) 1901770fe30aSKay Sievers return err; 19026cc8e743SPavel Tatashin atomic_inc(&lo->lo_refcnt); 19036cc8e743SPavel Tatashin mutex_unlock(&lo->lo_mutex); 19046cc8e743SPavel Tatashin return 0; 19051da177e4SLinus Torvalds } 19061da177e4SLinus Torvalds 1907967d1dc1SJan Kara static void lo_release(struct gendisk *disk, fmode_t mode) 19081da177e4SLinus Torvalds { 19096cc8e743SPavel Tatashin struct loop_device *lo = disk->private_data; 19101da177e4SLinus Torvalds 19116cc8e743SPavel Tatashin mutex_lock(&lo->lo_mutex); 1912f8933667SMing Lei if (atomic_dec_return(&lo->lo_refcnt)) 19130a42e99bSJan Kara goto out_unlock; 1914f8933667SMing Lei 191514f27939SMilan Broz if (lo->lo_flags & LO_FLAGS_AUTOCLEAR) { 1916a2505b79SJan Kara if (lo->lo_state != Lo_bound) 1917a2505b79SJan Kara goto out_unlock; 1918a2505b79SJan Kara lo->lo_state = Lo_rundown; 19196cc8e743SPavel Tatashin mutex_unlock(&lo->lo_mutex); 192014f27939SMilan Broz /* 192114f27939SMilan Broz * In autoclear mode, stop the loop thread 192214f27939SMilan Broz * and remove configuration after last close. 192314f27939SMilan Broz */ 19240da03cabSJan Kara __loop_clr_fd(lo, true); 19250a42e99bSJan Kara return; 192643cade80SOmar Sandoval } else if (lo->lo_state == Lo_bound) { 192714f27939SMilan Broz /* 192814f27939SMilan Broz * Otherwise keep thread (if running) and config, 192914f27939SMilan Broz * but flush possible ongoing bios in thread. 193014f27939SMilan Broz */ 193143cade80SOmar Sandoval blk_mq_freeze_queue(lo->lo_queue); 193243cade80SOmar Sandoval blk_mq_unfreeze_queue(lo->lo_queue); 193314f27939SMilan Broz } 193496c58655SDavid Woodhouse 19350a42e99bSJan Kara out_unlock: 19366cc8e743SPavel Tatashin mutex_unlock(&lo->lo_mutex); 1937ae665016SLinus Torvalds } 1938ae665016SLinus Torvalds 193983d5cde4SAlexey Dobriyan static const struct block_device_operations lo_fops = { 19401da177e4SLinus Torvalds .owner = THIS_MODULE, 1941bb214884SAl Viro .open = lo_open, 1942bb214884SAl Viro .release = lo_release, 1943bb214884SAl Viro .ioctl = lo_ioctl, 1944863d5b82SDavid Howells #ifdef CONFIG_COMPAT 1945bb214884SAl Viro .compat_ioctl = lo_compat_ioctl, 1946863d5b82SDavid Howells #endif 19471da177e4SLinus Torvalds }; 19481da177e4SLinus Torvalds 19491da177e4SLinus Torvalds /* 19501da177e4SLinus Torvalds * And now the modules code and kernel interface. 19511da177e4SLinus Torvalds */ 195273285082SKen Chen static int max_loop; 19535657a819SJoe Perches module_param(max_loop, int, 0444); 1954a47653fcSKen Chen MODULE_PARM_DESC(max_loop, "Maximum number of loop devices"); 19555657a819SJoe Perches module_param(max_part, int, 0444); 1956476a4813SLaurent Vivier MODULE_PARM_DESC(max_part, "Maximum number of partitions per loop device"); 19571da177e4SLinus Torvalds MODULE_LICENSE("GPL"); 19581da177e4SLinus Torvalds MODULE_ALIAS_BLOCKDEV_MAJOR(LOOP_MAJOR); 19591da177e4SLinus Torvalds 19601da177e4SLinus Torvalds int loop_register_transfer(struct loop_func_table *funcs) 19611da177e4SLinus Torvalds { 19621da177e4SLinus Torvalds unsigned int n = funcs->number; 19631da177e4SLinus Torvalds 19641da177e4SLinus Torvalds if (n >= MAX_LO_CRYPT || xfer_funcs[n]) 19651da177e4SLinus Torvalds return -EINVAL; 19661da177e4SLinus Torvalds xfer_funcs[n] = funcs; 19671da177e4SLinus Torvalds return 0; 19681da177e4SLinus Torvalds } 19691da177e4SLinus Torvalds 197034dd82afSKay Sievers static int unregister_transfer_cb(int id, void *ptr, void *data) 197134dd82afSKay Sievers { 197234dd82afSKay Sievers struct loop_device *lo = ptr; 197334dd82afSKay Sievers struct loop_func_table *xfer = data; 197434dd82afSKay Sievers 19756cc8e743SPavel Tatashin mutex_lock(&lo->lo_mutex); 197634dd82afSKay Sievers if (lo->lo_encryption == xfer) 197734dd82afSKay Sievers loop_release_xfer(lo); 19786cc8e743SPavel Tatashin mutex_unlock(&lo->lo_mutex); 197934dd82afSKay Sievers return 0; 198034dd82afSKay Sievers } 198134dd82afSKay Sievers 19821da177e4SLinus Torvalds int loop_unregister_transfer(int number) 19831da177e4SLinus Torvalds { 19841da177e4SLinus Torvalds unsigned int n = number; 19851da177e4SLinus Torvalds struct loop_func_table *xfer; 19861da177e4SLinus Torvalds 19871da177e4SLinus Torvalds if (n == 0 || n >= MAX_LO_CRYPT || (xfer = xfer_funcs[n]) == NULL) 19881da177e4SLinus Torvalds return -EINVAL; 19891da177e4SLinus Torvalds 19901da177e4SLinus Torvalds xfer_funcs[n] = NULL; 199134dd82afSKay Sievers idr_for_each(&loop_index_idr, &unregister_transfer_cb, xfer); 19921da177e4SLinus Torvalds return 0; 19931da177e4SLinus Torvalds } 19941da177e4SLinus Torvalds 19951da177e4SLinus Torvalds EXPORT_SYMBOL(loop_register_transfer); 19961da177e4SLinus Torvalds EXPORT_SYMBOL(loop_unregister_transfer); 19971da177e4SLinus Torvalds 1998fc17b653SChristoph Hellwig static blk_status_t loop_queue_rq(struct blk_mq_hw_ctx *hctx, 1999b5dd2f60SMing Lei const struct blk_mq_queue_data *bd) 2000b5dd2f60SMing Lei { 20011894e916SJens Axboe struct request *rq = bd->rq; 20021894e916SJens Axboe struct loop_cmd *cmd = blk_mq_rq_to_pdu(rq); 20031894e916SJens Axboe struct loop_device *lo = rq->q->queuedata; 2004b5dd2f60SMing Lei 20051894e916SJens Axboe blk_mq_start_request(rq); 2006b5dd2f60SMing Lei 2007f4aa4c7bSMing Lei if (lo->lo_state != Lo_bound) 2008fc17b653SChristoph Hellwig return BLK_STS_IOERR; 2009f4aa4c7bSMing Lei 20101894e916SJens Axboe switch (req_op(rq)) { 2011f0225cacSChristoph Hellwig case REQ_OP_FLUSH: 2012f0225cacSChristoph Hellwig case REQ_OP_DISCARD: 201319372e27SChristoph Hellwig case REQ_OP_WRITE_ZEROES: 2014bc07c10aSMing Lei cmd->use_aio = false; 2015f0225cacSChristoph Hellwig break; 2016f0225cacSChristoph Hellwig default: 2017f0225cacSChristoph Hellwig cmd->use_aio = lo->use_dio; 2018f0225cacSChristoph Hellwig break; 2019f0225cacSChristoph Hellwig } 2020bc07c10aSMing Lei 2021d4478e92SShaohua Li /* always use the first bio's css */ 20220b508bc9SShaohua Li #ifdef CONFIG_BLK_CGROUP 2023db6638d7SDennis Zhou if (cmd->use_aio && rq->bio && rq->bio->bi_blkg) { 2024db6638d7SDennis Zhou cmd->css = &bio_blkcg(rq->bio)->css; 2025d4478e92SShaohua Li css_get(cmd->css); 2026d4478e92SShaohua Li } else 2027d4478e92SShaohua Li #endif 2028d4478e92SShaohua Li cmd->css = NULL; 20293989144fSPetr Mladek kthread_queue_work(&lo->worker, &cmd->work); 2030b5dd2f60SMing Lei 2031fc17b653SChristoph Hellwig return BLK_STS_OK; 2032b5dd2f60SMing Lei } 2033b5dd2f60SMing Lei 2034b5dd2f60SMing Lei static void loop_handle_cmd(struct loop_cmd *cmd) 2035b5dd2f60SMing Lei { 20361894e916SJens Axboe struct request *rq = blk_mq_rq_from_pdu(cmd); 20371894e916SJens Axboe const bool write = op_is_write(req_op(rq)); 20381894e916SJens Axboe struct loop_device *lo = rq->q->queuedata; 2039f4829a9bSChristoph Hellwig int ret = 0; 2040b5dd2f60SMing Lei 2041f4829a9bSChristoph Hellwig if (write && (lo->lo_flags & LO_FLAGS_READ_ONLY)) { 2042f4829a9bSChristoph Hellwig ret = -EIO; 2043b5dd2f60SMing Lei goto failed; 2044f4829a9bSChristoph Hellwig } 2045b5dd2f60SMing Lei 20461894e916SJens Axboe ret = do_req_filebacked(lo, rq); 2047b5dd2f60SMing Lei failed: 2048bc07c10aSMing Lei /* complete non-aio request */ 2049fe2cb290SChristoph Hellwig if (!cmd->use_aio || ret) { 20508cd55087SEvan Green if (ret == -EOPNOTSUPP) 20518cd55087SEvan Green cmd->ret = ret; 20528cd55087SEvan Green else 2053fe2cb290SChristoph Hellwig cmd->ret = ret ? -EIO : 0; 205415f73f5bSChristoph Hellwig if (likely(!blk_should_fake_timeout(rq->q))) 20551894e916SJens Axboe blk_mq_complete_request(rq); 2056fe2cb290SChristoph Hellwig } 2057b5dd2f60SMing Lei } 2058b5dd2f60SMing Lei 2059e03a3d7aSMing Lei static void loop_queue_work(struct kthread_work *work) 2060b5dd2f60SMing Lei { 2061b5dd2f60SMing Lei struct loop_cmd *cmd = 2062e03a3d7aSMing Lei container_of(work, struct loop_cmd, work); 2063b5dd2f60SMing Lei 2064b5dd2f60SMing Lei loop_handle_cmd(cmd); 2065b5dd2f60SMing Lei } 2066b5dd2f60SMing Lei 2067d6296d39SChristoph Hellwig static int loop_init_request(struct blk_mq_tag_set *set, struct request *rq, 2068d6296d39SChristoph Hellwig unsigned int hctx_idx, unsigned int numa_node) 2069b5dd2f60SMing Lei { 2070b5dd2f60SMing Lei struct loop_cmd *cmd = blk_mq_rq_to_pdu(rq); 2071b5dd2f60SMing Lei 20723989144fSPetr Mladek kthread_init_work(&cmd->work, loop_queue_work); 2073b5dd2f60SMing Lei return 0; 2074b5dd2f60SMing Lei } 2075b5dd2f60SMing Lei 2076f363b089SEric Biggers static const struct blk_mq_ops loop_mq_ops = { 2077b5dd2f60SMing Lei .queue_rq = loop_queue_rq, 2078b5dd2f60SMing Lei .init_request = loop_init_request, 2079fe2cb290SChristoph Hellwig .complete = lo_complete_rq, 2080b5dd2f60SMing Lei }; 2081b5dd2f60SMing Lei 208234dd82afSKay Sievers static int loop_add(struct loop_device **l, int i) 20831da177e4SLinus Torvalds { 208473285082SKen Chen struct loop_device *lo; 208573285082SKen Chen struct gendisk *disk; 208634dd82afSKay Sievers int err; 20871da177e4SLinus Torvalds 208834dd82afSKay Sievers err = -ENOMEM; 208968d740d7SSilva Paulo lo = kzalloc(sizeof(*lo), GFP_KERNEL); 209068d740d7SSilva Paulo if (!lo) 209173285082SKen Chen goto out; 209234dd82afSKay Sievers 2093ef7e7c82SMikulas Patocka lo->lo_state = Lo_unbound; 2094ef7e7c82SMikulas Patocka 2095c718aa65STejun Heo /* allocate id, if @id >= 0, we're requesting that specific id */ 209634dd82afSKay Sievers if (i >= 0) { 2097c718aa65STejun Heo err = idr_alloc(&loop_index_idr, lo, i, i + 1, GFP_KERNEL); 2098c718aa65STejun Heo if (err == -ENOSPC) 209934dd82afSKay Sievers err = -EEXIST; 210034dd82afSKay Sievers } else { 2101c718aa65STejun Heo err = idr_alloc(&loop_index_idr, lo, 0, 0, GFP_KERNEL); 210234dd82afSKay Sievers } 210334dd82afSKay Sievers if (err < 0) 210434dd82afSKay Sievers goto out_free_dev; 2105c718aa65STejun Heo i = err; 21061da177e4SLinus Torvalds 2107183cfb57SWei Yongjun err = -ENOMEM; 2108b5dd2f60SMing Lei lo->tag_set.ops = &loop_mq_ops; 2109b5dd2f60SMing Lei lo->tag_set.nr_hw_queues = 1; 2110b5dd2f60SMing Lei lo->tag_set.queue_depth = 128; 2111b5dd2f60SMing Lei lo->tag_set.numa_node = NUMA_NO_NODE; 2112b5dd2f60SMing Lei lo->tag_set.cmd_size = sizeof(struct loop_cmd); 2113bf0beec0SMing Lei lo->tag_set.flags = BLK_MQ_F_SHOULD_MERGE | BLK_MQ_F_STACKING; 2114b5dd2f60SMing Lei lo->tag_set.driver_data = lo; 2115b5dd2f60SMing Lei 2116b5dd2f60SMing Lei err = blk_mq_alloc_tag_set(&lo->tag_set); 2117b5dd2f60SMing Lei if (err) 21183ec981e3SMikulas Patocka goto out_free_idr; 211973285082SKen Chen 2120*1c99502fSChristoph Hellwig disk = lo->lo_disk = blk_mq_alloc_disk(&lo->tag_set, lo); 2121*1c99502fSChristoph Hellwig if (IS_ERR(disk)) { 2122*1c99502fSChristoph Hellwig err = PTR_ERR(disk); 2123b5dd2f60SMing Lei goto out_cleanup_tags; 2124b5dd2f60SMing Lei } 2125*1c99502fSChristoph Hellwig lo->lo_queue = lo->lo_disk->queue; 2126ef7e7c82SMikulas Patocka 212754bb0adeSShaohua Li blk_queue_max_hw_sectors(lo->lo_queue, BLK_DEF_MAX_SECTORS); 212840326d8aSShaohua Li 21295b5e20f4SMing Lei /* 213040326d8aSShaohua Li * By default, we do buffer IO, so it doesn't make sense to enable 213140326d8aSShaohua Li * merge because the I/O submitted to backing file is handled page by 213240326d8aSShaohua Li * page. For directio mode, merge does help to dispatch bigger request 213340326d8aSShaohua Li * to underlayer disk. We will enable merge once directio is enabled. 21345b5e20f4SMing Lei */ 21358b904b5bSBart Van Assche blk_queue_flag_set(QUEUE_FLAG_NOMERGES, lo->lo_queue); 21365b5e20f4SMing Lei 2137e03c8dd1SKay Sievers /* 2138e03c8dd1SKay Sievers * Disable partition scanning by default. The in-kernel partition 2139e03c8dd1SKay Sievers * scanning can be requested individually per-device during its 2140e03c8dd1SKay Sievers * setup. Userspace can always add and remove partitions from all 2141e03c8dd1SKay Sievers * devices. The needed partition minors are allocated from the 2142e03c8dd1SKay Sievers * extended minor space, the main loop device numbers will continue 2143e03c8dd1SKay Sievers * to match the loop minors, regardless of the number of partitions 2144e03c8dd1SKay Sievers * used. 2145e03c8dd1SKay Sievers * 2146e03c8dd1SKay Sievers * If max_part is given, partition scanning is globally enabled for 2147e03c8dd1SKay Sievers * all loop devices. The minors for the main loop devices will be 2148e03c8dd1SKay Sievers * multiples of max_part. 2149e03c8dd1SKay Sievers * 2150e03c8dd1SKay Sievers * Note: Global-for-all-devices, set-only-at-init, read-only module 2151e03c8dd1SKay Sievers * parameteters like 'max_loop' and 'max_part' make things needlessly 2152e03c8dd1SKay Sievers * complicated, are too static, inflexible and may surprise 2153e03c8dd1SKay Sievers * userspace tools. Parameters like this in general should be avoided. 2154e03c8dd1SKay Sievers */ 2155e03c8dd1SKay Sievers if (!part_shift) 2156e03c8dd1SKay Sievers disk->flags |= GENHD_FL_NO_PART_SCAN; 2157e03c8dd1SKay Sievers disk->flags |= GENHD_FL_EXT_DEVT; 2158f8933667SMing Lei atomic_set(&lo->lo_refcnt, 0); 21596cc8e743SPavel Tatashin mutex_init(&lo->lo_mutex); 21601da177e4SLinus Torvalds lo->lo_number = i; 21611da177e4SLinus Torvalds spin_lock_init(&lo->lo_lock); 21621da177e4SLinus Torvalds disk->major = LOOP_MAJOR; 2163476a4813SLaurent Vivier disk->first_minor = i << part_shift; 2164*1c99502fSChristoph Hellwig disk->minors = 1 << part_shift; 21651da177e4SLinus Torvalds disk->fops = &lo_fops; 21661da177e4SLinus Torvalds disk->private_data = lo; 21671da177e4SLinus Torvalds disk->queue = lo->lo_queue; 216873285082SKen Chen sprintf(disk->disk_name, "loop%d", i); 216934dd82afSKay Sievers add_disk(disk); 217034dd82afSKay Sievers *l = lo; 217134dd82afSKay Sievers return lo->lo_number; 217273285082SKen Chen 2173b5dd2f60SMing Lei out_cleanup_tags: 2174b5dd2f60SMing Lei blk_mq_free_tag_set(&lo->tag_set); 21753ec981e3SMikulas Patocka out_free_idr: 21763ec981e3SMikulas Patocka idr_remove(&loop_index_idr, i); 217773285082SKen Chen out_free_dev: 217873285082SKen Chen kfree(lo); 217973285082SKen Chen out: 218034dd82afSKay Sievers return err; 21811da177e4SLinus Torvalds } 21821da177e4SLinus Torvalds 218334dd82afSKay Sievers static void loop_remove(struct loop_device *lo) 218473285082SKen Chen { 21856cd18e71SNeilBrown del_gendisk(lo->lo_disk); 2186b5dd2f60SMing Lei blk_mq_free_tag_set(&lo->tag_set); 2187*1c99502fSChristoph Hellwig blk_cleanup_disk(lo->lo_disk); 21886cc8e743SPavel Tatashin mutex_destroy(&lo->lo_mutex); 218973285082SKen Chen kfree(lo); 219073285082SKen Chen } 219173285082SKen Chen 2192770fe30aSKay Sievers static int find_free_cb(int id, void *ptr, void *data) 2193770fe30aSKay Sievers { 2194770fe30aSKay Sievers struct loop_device *lo = ptr; 2195770fe30aSKay Sievers struct loop_device **l = data; 2196770fe30aSKay Sievers 2197770fe30aSKay Sievers if (lo->lo_state == Lo_unbound) { 2198770fe30aSKay Sievers *l = lo; 2199770fe30aSKay Sievers return 1; 2200770fe30aSKay Sievers } 2201770fe30aSKay Sievers return 0; 2202770fe30aSKay Sievers } 2203770fe30aSKay Sievers 220434dd82afSKay Sievers static int loop_lookup(struct loop_device **l, int i) 2205a47653fcSKen Chen { 2206a47653fcSKen Chen struct loop_device *lo; 220734dd82afSKay Sievers int ret = -ENODEV; 2208a47653fcSKen Chen 2209770fe30aSKay Sievers if (i < 0) { 2210770fe30aSKay Sievers int err; 2211770fe30aSKay Sievers 2212770fe30aSKay Sievers err = idr_for_each(&loop_index_idr, &find_free_cb, &lo); 2213770fe30aSKay Sievers if (err == 1) { 2214770fe30aSKay Sievers *l = lo; 2215770fe30aSKay Sievers ret = lo->lo_number; 2216770fe30aSKay Sievers } 2217770fe30aSKay Sievers goto out; 2218a47653fcSKen Chen } 2219a47653fcSKen Chen 2220770fe30aSKay Sievers /* lookup and return a specific i */ 222134dd82afSKay Sievers lo = idr_find(&loop_index_idr, i); 2222a47653fcSKen Chen if (lo) { 222334dd82afSKay Sievers *l = lo; 222434dd82afSKay Sievers ret = lo->lo_number; 2225a47653fcSKen Chen } 2226770fe30aSKay Sievers out: 222734dd82afSKay Sievers return ret; 2228a47653fcSKen Chen } 2229a47653fcSKen Chen 22308410d38cSChristoph Hellwig static void loop_probe(dev_t dev) 223173285082SKen Chen { 22328410d38cSChristoph Hellwig int idx = MINOR(dev) >> part_shift; 2233705962ccSAl Viro struct loop_device *lo; 22348410d38cSChristoph Hellwig 22358410d38cSChristoph Hellwig if (max_loop && idx >= max_loop) 22368410d38cSChristoph Hellwig return; 223773285082SKen Chen 22380a42e99bSJan Kara mutex_lock(&loop_ctl_mutex); 22398410d38cSChristoph Hellwig if (loop_lookup(&lo, idx) < 0) 22408410d38cSChristoph Hellwig loop_add(&lo, idx); 22410a42e99bSJan Kara mutex_unlock(&loop_ctl_mutex); 224273285082SKen Chen } 224373285082SKen Chen 2244770fe30aSKay Sievers static long loop_control_ioctl(struct file *file, unsigned int cmd, 2245770fe30aSKay Sievers unsigned long parm) 2246770fe30aSKay Sievers { 2247770fe30aSKay Sievers struct loop_device *lo; 22480a42e99bSJan Kara int ret; 2249770fe30aSKay Sievers 22500a42e99bSJan Kara ret = mutex_lock_killable(&loop_ctl_mutex); 22510a42e99bSJan Kara if (ret) 22520a42e99bSJan Kara return ret; 22530a42e99bSJan Kara 22540a42e99bSJan Kara ret = -ENOSYS; 2255770fe30aSKay Sievers switch (cmd) { 2256770fe30aSKay Sievers case LOOP_CTL_ADD: 2257770fe30aSKay Sievers ret = loop_lookup(&lo, parm); 2258770fe30aSKay Sievers if (ret >= 0) { 2259770fe30aSKay Sievers ret = -EEXIST; 2260770fe30aSKay Sievers break; 2261770fe30aSKay Sievers } 2262770fe30aSKay Sievers ret = loop_add(&lo, parm); 2263770fe30aSKay Sievers break; 2264770fe30aSKay Sievers case LOOP_CTL_REMOVE: 2265770fe30aSKay Sievers ret = loop_lookup(&lo, parm); 2266770fe30aSKay Sievers if (ret < 0) 2267770fe30aSKay Sievers break; 22686cc8e743SPavel Tatashin ret = mutex_lock_killable(&lo->lo_mutex); 22696cc8e743SPavel Tatashin if (ret) 22706cc8e743SPavel Tatashin break; 2271770fe30aSKay Sievers if (lo->lo_state != Lo_unbound) { 2272770fe30aSKay Sievers ret = -EBUSY; 22736cc8e743SPavel Tatashin mutex_unlock(&lo->lo_mutex); 2274770fe30aSKay Sievers break; 2275770fe30aSKay Sievers } 2276f8933667SMing Lei if (atomic_read(&lo->lo_refcnt) > 0) { 2277770fe30aSKay Sievers ret = -EBUSY; 22786cc8e743SPavel Tatashin mutex_unlock(&lo->lo_mutex); 2279770fe30aSKay Sievers break; 2280770fe30aSKay Sievers } 2281770fe30aSKay Sievers lo->lo_disk->private_data = NULL; 22826cc8e743SPavel Tatashin mutex_unlock(&lo->lo_mutex); 2283770fe30aSKay Sievers idr_remove(&loop_index_idr, lo->lo_number); 2284770fe30aSKay Sievers loop_remove(lo); 2285770fe30aSKay Sievers break; 2286770fe30aSKay Sievers case LOOP_CTL_GET_FREE: 2287770fe30aSKay Sievers ret = loop_lookup(&lo, -1); 2288770fe30aSKay Sievers if (ret >= 0) 2289770fe30aSKay Sievers break; 2290770fe30aSKay Sievers ret = loop_add(&lo, -1); 2291770fe30aSKay Sievers } 22920a42e99bSJan Kara mutex_unlock(&loop_ctl_mutex); 2293770fe30aSKay Sievers 2294770fe30aSKay Sievers return ret; 2295770fe30aSKay Sievers } 2296770fe30aSKay Sievers 2297770fe30aSKay Sievers static const struct file_operations loop_ctl_fops = { 2298770fe30aSKay Sievers .open = nonseekable_open, 2299770fe30aSKay Sievers .unlocked_ioctl = loop_control_ioctl, 2300770fe30aSKay Sievers .compat_ioctl = loop_control_ioctl, 2301770fe30aSKay Sievers .owner = THIS_MODULE, 2302770fe30aSKay Sievers .llseek = noop_llseek, 2303770fe30aSKay Sievers }; 2304770fe30aSKay Sievers 2305770fe30aSKay Sievers static struct miscdevice loop_misc = { 2306770fe30aSKay Sievers .minor = LOOP_CTRL_MINOR, 2307770fe30aSKay Sievers .name = "loop-control", 2308770fe30aSKay Sievers .fops = &loop_ctl_fops, 2309770fe30aSKay Sievers }; 2310770fe30aSKay Sievers 2311770fe30aSKay Sievers MODULE_ALIAS_MISCDEV(LOOP_CTRL_MINOR); 2312770fe30aSKay Sievers MODULE_ALIAS("devname:loop-control"); 2313770fe30aSKay Sievers 231473285082SKen Chen static int __init loop_init(void) 231573285082SKen Chen { 2316a47653fcSKen Chen int i, nr; 231734dd82afSKay Sievers struct loop_device *lo; 2318770fe30aSKay Sievers int err; 2319a47653fcSKen Chen 2320476a4813SLaurent Vivier part_shift = 0; 2321ac04fee0SNamhyung Kim if (max_part > 0) { 2322476a4813SLaurent Vivier part_shift = fls(max_part); 2323476a4813SLaurent Vivier 2324ac04fee0SNamhyung Kim /* 2325ac04fee0SNamhyung Kim * Adjust max_part according to part_shift as it is exported 2326ac04fee0SNamhyung Kim * to user space so that user can decide correct minor number 2327ac04fee0SNamhyung Kim * if [s]he want to create more devices. 2328ac04fee0SNamhyung Kim * 2329ac04fee0SNamhyung Kim * Note that -1 is required because partition 0 is reserved 2330ac04fee0SNamhyung Kim * for the whole disk. 2331ac04fee0SNamhyung Kim */ 2332ac04fee0SNamhyung Kim max_part = (1UL << part_shift) - 1; 2333ac04fee0SNamhyung Kim } 2334ac04fee0SNamhyung Kim 2335b1a66504SGuo Chao if ((1UL << part_shift) > DISK_MAX_PARTS) { 2336b1a66504SGuo Chao err = -EINVAL; 2337a8c1d064SAnton Volkov goto err_out; 2338b1a66504SGuo Chao } 233978f4bb36SNamhyung Kim 2340b1a66504SGuo Chao if (max_loop > 1UL << (MINORBITS - part_shift)) { 2341b1a66504SGuo Chao err = -EINVAL; 2342a8c1d064SAnton Volkov goto err_out; 2343b1a66504SGuo Chao } 234473285082SKen Chen 2345d134b00bSKay Sievers /* 2346d134b00bSKay Sievers * If max_loop is specified, create that many devices upfront. 2347d134b00bSKay Sievers * This also becomes a hard limit. If max_loop is not specified, 2348d134b00bSKay Sievers * create CONFIG_BLK_DEV_LOOP_MIN_COUNT loop devices at module 2349d134b00bSKay Sievers * init time. Loop devices can be requested on-demand with the 2350d134b00bSKay Sievers * /dev/loop-control interface, or be instantiated by accessing 2351d134b00bSKay Sievers * a 'dead' device node. 2352d134b00bSKay Sievers */ 2353aeb2b0b1SLukas Bulwahn if (max_loop) 2354a47653fcSKen Chen nr = max_loop; 2355aeb2b0b1SLukas Bulwahn else 2356d134b00bSKay Sievers nr = CONFIG_BLK_DEV_LOOP_MIN_COUNT; 2357a47653fcSKen Chen 2358a8c1d064SAnton Volkov err = misc_register(&loop_misc); 2359a8c1d064SAnton Volkov if (err < 0) 2360a8c1d064SAnton Volkov goto err_out; 2361a8c1d064SAnton Volkov 2362a8c1d064SAnton Volkov 23638410d38cSChristoph Hellwig if (__register_blkdev(LOOP_MAJOR, "loop", loop_probe)) { 2364b1a66504SGuo Chao err = -EIO; 2365b1a66504SGuo Chao goto misc_out; 2366b1a66504SGuo Chao } 2367a47653fcSKen Chen 2368d134b00bSKay Sievers /* pre-create number of devices given by config or max_loop */ 23690a42e99bSJan Kara mutex_lock(&loop_ctl_mutex); 237034dd82afSKay Sievers for (i = 0; i < nr; i++) 237134dd82afSKay Sievers loop_add(&lo, i); 23720a42e99bSJan Kara mutex_unlock(&loop_ctl_mutex); 237334dd82afSKay Sievers 237473285082SKen Chen printk(KERN_INFO "loop: module loaded\n"); 23751da177e4SLinus Torvalds return 0; 2376b1a66504SGuo Chao 2377b1a66504SGuo Chao misc_out: 2378b1a66504SGuo Chao misc_deregister(&loop_misc); 2379a8c1d064SAnton Volkov err_out: 2380b1a66504SGuo Chao return err; 238134dd82afSKay Sievers } 2382a47653fcSKen Chen 238334dd82afSKay Sievers static int loop_exit_cb(int id, void *ptr, void *data) 238434dd82afSKay Sievers { 238534dd82afSKay Sievers struct loop_device *lo = ptr; 2386a47653fcSKen Chen 238734dd82afSKay Sievers loop_remove(lo); 238834dd82afSKay Sievers return 0; 23891da177e4SLinus Torvalds } 23901da177e4SLinus Torvalds 239173285082SKen Chen static void __exit loop_exit(void) 23921da177e4SLinus Torvalds { 2393200f9337SLuis Chamberlain mutex_lock(&loop_ctl_mutex); 2394200f9337SLuis Chamberlain 239534dd82afSKay Sievers idr_for_each(&loop_index_idr, &loop_exit_cb, NULL); 239634dd82afSKay Sievers idr_destroy(&loop_index_idr); 239773285082SKen Chen 239800d59405SAkinobu Mita unregister_blkdev(LOOP_MAJOR, "loop"); 2399770fe30aSKay Sievers 2400770fe30aSKay Sievers misc_deregister(&loop_misc); 2401200f9337SLuis Chamberlain 2402200f9337SLuis Chamberlain mutex_unlock(&loop_ctl_mutex); 24031da177e4SLinus Torvalds } 24041da177e4SLinus Torvalds 24051da177e4SLinus Torvalds module_init(loop_init); 24061da177e4SLinus Torvalds module_exit(loop_exit); 24071da177e4SLinus Torvalds 24081da177e4SLinus Torvalds #ifndef MODULE 24091da177e4SLinus Torvalds static int __init max_loop_setup(char *str) 24101da177e4SLinus Torvalds { 24111da177e4SLinus Torvalds max_loop = simple_strtol(str, NULL, 0); 24121da177e4SLinus Torvalds return 1; 24131da177e4SLinus Torvalds } 24141da177e4SLinus Torvalds 24151da177e4SLinus Torvalds __setup("max_loop=", max_loop_setup); 24161da177e4SLinus Torvalds #endif 2417