11da177e4SLinus Torvalds /* 21da177e4SLinus Torvalds * linux/drivers/block/loop.c 31da177e4SLinus Torvalds * 41da177e4SLinus Torvalds * Written by Theodore Ts'o, 3/29/93 51da177e4SLinus Torvalds * 61da177e4SLinus Torvalds * Copyright 1993 by Theodore Ts'o. Redistribution of this file is 71da177e4SLinus Torvalds * permitted under the GNU General Public License. 81da177e4SLinus Torvalds * 91da177e4SLinus Torvalds * DES encryption plus some minor changes by Werner Almesberger, 30-MAY-1993 101da177e4SLinus Torvalds * more DES encryption plus IDEA encryption by Nicholas J. Leon, June 20, 1996 111da177e4SLinus Torvalds * 121da177e4SLinus Torvalds * Modularized and updated for 1.1.16 kernel - Mitch Dsouza 28th May 1994 131da177e4SLinus Torvalds * Adapted for 1.3.59 kernel - Andries Brouwer, 1 Feb 1996 141da177e4SLinus Torvalds * 151da177e4SLinus Torvalds * Fixed do_loop_request() re-entrancy - Vincent.Renardias@waw.com Mar 20, 1997 161da177e4SLinus Torvalds * 171da177e4SLinus Torvalds * Added devfs support - Richard Gooch <rgooch@atnf.csiro.au> 16-Jan-1998 181da177e4SLinus Torvalds * 191da177e4SLinus Torvalds * Handle sparse backing files correctly - Kenn Humborg, Jun 28, 1998 201da177e4SLinus Torvalds * 211da177e4SLinus Torvalds * Loadable modules and other fixes by AK, 1998 221da177e4SLinus Torvalds * 231da177e4SLinus Torvalds * Make real block number available to downstream transfer functions, enables 241da177e4SLinus Torvalds * CBC (and relatives) mode encryption requiring unique IVs per data block. 251da177e4SLinus Torvalds * Reed H. Petty, rhp@draper.net 261da177e4SLinus Torvalds * 271da177e4SLinus Torvalds * Maximum number of loop devices now dynamic via max_loop module parameter. 281da177e4SLinus Torvalds * Russell Kroll <rkroll@exploits.org> 19990701 291da177e4SLinus Torvalds * 301da177e4SLinus Torvalds * Maximum number of loop devices when compiled-in now selectable by passing 311da177e4SLinus Torvalds * max_loop=<1-255> to the kernel on boot. 3296de0e25SJan Engelhardt * Erik I. Bolsø, <eriki@himolde.no>, Oct 31, 1999 331da177e4SLinus Torvalds * 341da177e4SLinus Torvalds * Completely rewrite request handling to be make_request_fn style and 351da177e4SLinus Torvalds * non blocking, pushing work to a helper thread. Lots of fixes from 361da177e4SLinus Torvalds * Al Viro too. 371da177e4SLinus Torvalds * Jens Axboe <axboe@suse.de>, Nov 2000 381da177e4SLinus Torvalds * 391da177e4SLinus Torvalds * Support up to 256 loop devices 401da177e4SLinus Torvalds * Heinz Mauelshagen <mge@sistina.com>, Feb 2002 411da177e4SLinus Torvalds * 421da177e4SLinus Torvalds * Support for falling back on the write file operation when the address space 434e02ed4bSNick Piggin * operations write_begin is not available on the backing filesystem. 441da177e4SLinus Torvalds * Anton Altaparmakov, 16 Feb 2005 451da177e4SLinus Torvalds * 461da177e4SLinus Torvalds * Still To Fix: 471da177e4SLinus Torvalds * - Advisory locking is ignored here. 481da177e4SLinus Torvalds * - Should use an own CAP_* category instead of CAP_SYS_ADMIN 491da177e4SLinus Torvalds * 501da177e4SLinus Torvalds */ 511da177e4SLinus Torvalds 521da177e4SLinus Torvalds #include <linux/module.h> 531da177e4SLinus Torvalds #include <linux/moduleparam.h> 541da177e4SLinus Torvalds #include <linux/sched.h> 551da177e4SLinus Torvalds #include <linux/fs.h> 561da177e4SLinus Torvalds #include <linux/file.h> 571da177e4SLinus Torvalds #include <linux/stat.h> 581da177e4SLinus Torvalds #include <linux/errno.h> 591da177e4SLinus Torvalds #include <linux/major.h> 601da177e4SLinus Torvalds #include <linux/wait.h> 611da177e4SLinus Torvalds #include <linux/blkdev.h> 621da177e4SLinus Torvalds #include <linux/blkpg.h> 631da177e4SLinus Torvalds #include <linux/init.h> 641da177e4SLinus Torvalds #include <linux/swap.h> 651da177e4SLinus Torvalds #include <linux/slab.h> 66863d5b82SDavid Howells #include <linux/compat.h> 671da177e4SLinus Torvalds #include <linux/suspend.h> 6883144186SRafael J. Wysocki #include <linux/freezer.h> 692a48fc0aSArnd Bergmann #include <linux/mutex.h> 701da177e4SLinus Torvalds #include <linux/writeback.h> 711da177e4SLinus Torvalds #include <linux/completion.h> 721da177e4SLinus Torvalds #include <linux/highmem.h> 736c997918SSerge E. Hallyn #include <linux/kthread.h> 74d6b29d7cSJens Axboe #include <linux/splice.h> 75ee862730SMilan Broz #include <linux/sysfs.h> 76770fe30aSKay Sievers #include <linux/miscdevice.h> 77dfaa2ef6SLukas Czerner #include <linux/falloc.h> 78283e7e5dSAl Viro #include <linux/uio.h> 79d9a08a9eSAdam Manzanares #include <linux/ioprio.h> 80db6638d7SDennis Zhou #include <linux/blk-cgroup.h> 81d9a08a9eSAdam Manzanares 8283a87611SAl Viro #include "loop.h" 831da177e4SLinus Torvalds 847c0f6ba6SLinus Torvalds #include <linux/uaccess.h> 851da177e4SLinus Torvalds 8634dd82afSKay Sievers static DEFINE_IDR(loop_index_idr); 87310ca162STetsuo Handa static DEFINE_MUTEX(loop_ctl_mutex); 881da177e4SLinus Torvalds 89476a4813SLaurent Vivier static int max_part; 90476a4813SLaurent Vivier static int part_shift; 91476a4813SLaurent Vivier 921da177e4SLinus Torvalds static int transfer_xor(struct loop_device *lo, int cmd, 931da177e4SLinus Torvalds struct page *raw_page, unsigned raw_off, 941da177e4SLinus Torvalds struct page *loop_page, unsigned loop_off, 951da177e4SLinus Torvalds int size, sector_t real_block) 961da177e4SLinus Torvalds { 97cfd8005cSCong Wang char *raw_buf = kmap_atomic(raw_page) + raw_off; 98cfd8005cSCong Wang char *loop_buf = kmap_atomic(loop_page) + loop_off; 991da177e4SLinus Torvalds char *in, *out, *key; 1001da177e4SLinus Torvalds int i, keysize; 1011da177e4SLinus Torvalds 1021da177e4SLinus Torvalds if (cmd == READ) { 1031da177e4SLinus Torvalds in = raw_buf; 1041da177e4SLinus Torvalds out = loop_buf; 1051da177e4SLinus Torvalds } else { 1061da177e4SLinus Torvalds in = loop_buf; 1071da177e4SLinus Torvalds out = raw_buf; 1081da177e4SLinus Torvalds } 1091da177e4SLinus Torvalds 1101da177e4SLinus Torvalds key = lo->lo_encrypt_key; 1111da177e4SLinus Torvalds keysize = lo->lo_encrypt_key_size; 1121da177e4SLinus Torvalds for (i = 0; i < size; i++) 1131da177e4SLinus Torvalds *out++ = *in++ ^ key[(i & 511) % keysize]; 1141da177e4SLinus Torvalds 115cfd8005cSCong Wang kunmap_atomic(loop_buf); 116cfd8005cSCong Wang kunmap_atomic(raw_buf); 1171da177e4SLinus Torvalds cond_resched(); 1181da177e4SLinus Torvalds return 0; 1191da177e4SLinus Torvalds } 1201da177e4SLinus Torvalds 1211da177e4SLinus Torvalds static int xor_init(struct loop_device *lo, const struct loop_info64 *info) 1221da177e4SLinus Torvalds { 1231da177e4SLinus Torvalds if (unlikely(info->lo_encrypt_key_size <= 0)) 1241da177e4SLinus Torvalds return -EINVAL; 1251da177e4SLinus Torvalds return 0; 1261da177e4SLinus Torvalds } 1271da177e4SLinus Torvalds 1281da177e4SLinus Torvalds static struct loop_func_table none_funcs = { 1291da177e4SLinus Torvalds .number = LO_CRYPT_NONE, 1301da177e4SLinus Torvalds }; 1311da177e4SLinus Torvalds 1321da177e4SLinus Torvalds static struct loop_func_table xor_funcs = { 1331da177e4SLinus Torvalds .number = LO_CRYPT_XOR, 1341da177e4SLinus Torvalds .transfer = transfer_xor, 1351da177e4SLinus Torvalds .init = xor_init 1361da177e4SLinus Torvalds }; 1371da177e4SLinus Torvalds 1381da177e4SLinus Torvalds /* xfer_funcs[0] is special - its release function is never called */ 1391da177e4SLinus Torvalds static struct loop_func_table *xfer_funcs[MAX_LO_CRYPT] = { 1401da177e4SLinus Torvalds &none_funcs, 1411da177e4SLinus Torvalds &xor_funcs 1421da177e4SLinus Torvalds }; 1431da177e4SLinus Torvalds 1447035b5dfSDmitry Monakhov static loff_t get_size(loff_t offset, loff_t sizelimit, struct file *file) 1451da177e4SLinus Torvalds { 146b7a1da69SGuo Chao loff_t loopsize; 1471da177e4SLinus Torvalds 1481da177e4SLinus Torvalds /* Compute loopsize in bytes */ 149b7a1da69SGuo Chao loopsize = i_size_read(file->f_mapping->host); 150b7a1da69SGuo Chao if (offset > 0) 151b7a1da69SGuo Chao loopsize -= offset; 152b7a1da69SGuo Chao /* offset is beyond i_size, weird but possible */ 1537035b5dfSDmitry Monakhov if (loopsize < 0) 1547035b5dfSDmitry Monakhov return 0; 1551da177e4SLinus Torvalds 1567035b5dfSDmitry Monakhov if (sizelimit > 0 && sizelimit < loopsize) 1577035b5dfSDmitry Monakhov loopsize = sizelimit; 1581da177e4SLinus Torvalds /* 1591da177e4SLinus Torvalds * Unfortunately, if we want to do I/O on the device, 1601da177e4SLinus Torvalds * the number of 512-byte sectors has to fit into a sector_t. 1611da177e4SLinus Torvalds */ 1621da177e4SLinus Torvalds return loopsize >> 9; 1631da177e4SLinus Torvalds } 1641da177e4SLinus Torvalds 1657035b5dfSDmitry Monakhov static loff_t get_loop_size(struct loop_device *lo, struct file *file) 1661da177e4SLinus Torvalds { 1677035b5dfSDmitry Monakhov return get_size(lo->lo_offset, lo->lo_sizelimit, file); 1687035b5dfSDmitry Monakhov } 1697035b5dfSDmitry Monakhov 1702e5ab5f3SMing Lei static void __loop_update_dio(struct loop_device *lo, bool dio) 1712e5ab5f3SMing Lei { 1722e5ab5f3SMing Lei struct file *file = lo->lo_backing_file; 1732e5ab5f3SMing Lei struct address_space *mapping = file->f_mapping; 1742e5ab5f3SMing Lei struct inode *inode = mapping->host; 1752e5ab5f3SMing Lei unsigned short sb_bsize = 0; 1762e5ab5f3SMing Lei unsigned dio_align = 0; 1772e5ab5f3SMing Lei bool use_dio; 1782e5ab5f3SMing Lei 1792e5ab5f3SMing Lei if (inode->i_sb->s_bdev) { 1802e5ab5f3SMing Lei sb_bsize = bdev_logical_block_size(inode->i_sb->s_bdev); 1812e5ab5f3SMing Lei dio_align = sb_bsize - 1; 1822e5ab5f3SMing Lei } 1832e5ab5f3SMing Lei 1842e5ab5f3SMing Lei /* 1852e5ab5f3SMing Lei * We support direct I/O only if lo_offset is aligned with the 1862e5ab5f3SMing Lei * logical I/O size of backing device, and the logical block 1872e5ab5f3SMing Lei * size of loop is bigger than the backing device's and the loop 1882e5ab5f3SMing Lei * needn't transform transfer. 1892e5ab5f3SMing Lei * 1902e5ab5f3SMing Lei * TODO: the above condition may be loosed in the future, and 1912e5ab5f3SMing Lei * direct I/O may be switched runtime at that time because most 19289d790abSMasahiro Yamada * of requests in sane applications should be PAGE_SIZE aligned 1932e5ab5f3SMing Lei */ 1942e5ab5f3SMing Lei if (dio) { 1952e5ab5f3SMing Lei if (queue_logical_block_size(lo->lo_queue) >= sb_bsize && 1962e5ab5f3SMing Lei !(lo->lo_offset & dio_align) && 1972e5ab5f3SMing Lei mapping->a_ops->direct_IO && 1982e5ab5f3SMing Lei !lo->transfer) 1992e5ab5f3SMing Lei use_dio = true; 2002e5ab5f3SMing Lei else 2012e5ab5f3SMing Lei use_dio = false; 2022e5ab5f3SMing Lei } else { 2032e5ab5f3SMing Lei use_dio = false; 2042e5ab5f3SMing Lei } 2052e5ab5f3SMing Lei 2062e5ab5f3SMing Lei if (lo->use_dio == use_dio) 2072e5ab5f3SMing Lei return; 2082e5ab5f3SMing Lei 2092e5ab5f3SMing Lei /* flush dirty pages before changing direct IO */ 2102e5ab5f3SMing Lei vfs_fsync(file, 0); 2112e5ab5f3SMing Lei 2122e5ab5f3SMing Lei /* 2132e5ab5f3SMing Lei * The flag of LO_FLAGS_DIRECT_IO is handled similarly with 2142e5ab5f3SMing Lei * LO_FLAGS_READ_ONLY, both are set from kernel, and losetup 2152e5ab5f3SMing Lei * will get updated by ioctl(LOOP_GET_STATUS) 2162e5ab5f3SMing Lei */ 2170fbcf579SMartijn Coenen if (lo->lo_state == Lo_bound) 2182e5ab5f3SMing Lei blk_mq_freeze_queue(lo->lo_queue); 2192e5ab5f3SMing Lei lo->use_dio = use_dio; 22040326d8aSShaohua Li if (use_dio) { 2218b904b5bSBart Van Assche blk_queue_flag_clear(QUEUE_FLAG_NOMERGES, lo->lo_queue); 2222e5ab5f3SMing Lei lo->lo_flags |= LO_FLAGS_DIRECT_IO; 22340326d8aSShaohua Li } else { 2248b904b5bSBart Van Assche blk_queue_flag_set(QUEUE_FLAG_NOMERGES, lo->lo_queue); 2252e5ab5f3SMing Lei lo->lo_flags &= ~LO_FLAGS_DIRECT_IO; 22640326d8aSShaohua Li } 2270fbcf579SMartijn Coenen if (lo->lo_state == Lo_bound) 2282e5ab5f3SMing Lei blk_mq_unfreeze_queue(lo->lo_queue); 2292e5ab5f3SMing Lei } 2302e5ab5f3SMing Lei 2315795b6f5SMartijn Coenen /** 2325795b6f5SMartijn Coenen * loop_set_size() - sets device size and notifies userspace 2335795b6f5SMartijn Coenen * @lo: struct loop_device to set the size for 2345795b6f5SMartijn Coenen * @size: new size of the loop device 2355795b6f5SMartijn Coenen * 2365795b6f5SMartijn Coenen * Callers must validate that the size passed into this function fits into 2375795b6f5SMartijn Coenen * a sector_t, eg using loop_validate_size() 2385795b6f5SMartijn Coenen */ 2395795b6f5SMartijn Coenen static void loop_set_size(struct loop_device *lo, loff_t size) 2405795b6f5SMartijn Coenen { 2415795b6f5SMartijn Coenen struct block_device *bdev = lo->lo_device; 2425795b6f5SMartijn Coenen 2435795b6f5SMartijn Coenen bd_set_size(bdev, size << SECTOR_SHIFT); 244716ad098SMartijn Coenen 245716ad098SMartijn Coenen set_capacity_revalidate_and_notify(lo->lo_disk, size, false); 2465795b6f5SMartijn Coenen } 2475795b6f5SMartijn Coenen 2481da177e4SLinus Torvalds static inline int 2491da177e4SLinus Torvalds lo_do_transfer(struct loop_device *lo, int cmd, 2501da177e4SLinus Torvalds struct page *rpage, unsigned roffs, 2511da177e4SLinus Torvalds struct page *lpage, unsigned loffs, 2521da177e4SLinus Torvalds int size, sector_t rblock) 2531da177e4SLinus Torvalds { 254aa4d8616SChristoph Hellwig int ret; 255aa4d8616SChristoph Hellwig 256aa4d8616SChristoph Hellwig ret = lo->transfer(lo, cmd, rpage, roffs, lpage, loffs, size, rblock); 257aa4d8616SChristoph Hellwig if (likely(!ret)) 2581da177e4SLinus Torvalds return 0; 2591da177e4SLinus Torvalds 260aa4d8616SChristoph Hellwig printk_ratelimited(KERN_ERR 261aa4d8616SChristoph Hellwig "loop: Transfer error at byte offset %llu, length %i.\n", 262aa4d8616SChristoph Hellwig (unsigned long long)rblock << 9, size); 263aa4d8616SChristoph Hellwig return ret; 2641da177e4SLinus Torvalds } 2651da177e4SLinus Torvalds 266aa4d8616SChristoph Hellwig static int lo_write_bvec(struct file *file, struct bio_vec *bvec, loff_t *ppos) 2671da177e4SLinus Torvalds { 268aa4d8616SChristoph Hellwig struct iov_iter i; 2691da177e4SLinus Torvalds ssize_t bw; 270283e7e5dSAl Viro 271b6207430SChristoph Hellwig iov_iter_bvec(&i, WRITE, bvec, 1, bvec->bv_len); 2721da177e4SLinus Torvalds 27303d95eb2SAl Viro file_start_write(file); 274abbb6589SChristoph Hellwig bw = vfs_iter_write(file, &i, ppos, 0); 27503d95eb2SAl Viro file_end_write(file); 276aa4d8616SChristoph Hellwig 277aa4d8616SChristoph Hellwig if (likely(bw == bvec->bv_len)) 2781da177e4SLinus Torvalds return 0; 279aa4d8616SChristoph Hellwig 280aa4d8616SChristoph Hellwig printk_ratelimited(KERN_ERR 281aa4d8616SChristoph Hellwig "loop: Write error at byte offset %llu, length %i.\n", 282aa4d8616SChristoph Hellwig (unsigned long long)*ppos, bvec->bv_len); 2831da177e4SLinus Torvalds if (bw >= 0) 2841da177e4SLinus Torvalds bw = -EIO; 2851da177e4SLinus Torvalds return bw; 2861da177e4SLinus Torvalds } 2871da177e4SLinus Torvalds 288aa4d8616SChristoph Hellwig static int lo_write_simple(struct loop_device *lo, struct request *rq, 289aa4d8616SChristoph Hellwig loff_t pos) 2901da177e4SLinus Torvalds { 291aa4d8616SChristoph Hellwig struct bio_vec bvec; 292aa4d8616SChristoph Hellwig struct req_iterator iter; 293aa4d8616SChristoph Hellwig int ret = 0; 294aa4d8616SChristoph Hellwig 295aa4d8616SChristoph Hellwig rq_for_each_segment(bvec, rq, iter) { 296aa4d8616SChristoph Hellwig ret = lo_write_bvec(lo->lo_backing_file, &bvec, &pos); 297aa4d8616SChristoph Hellwig if (ret < 0) 298aa4d8616SChristoph Hellwig break; 2991da177e4SLinus Torvalds cond_resched(); 3001da177e4SLinus Torvalds } 3011da177e4SLinus Torvalds 302aa4d8616SChristoph Hellwig return ret; 303aa4d8616SChristoph Hellwig } 304aa4d8616SChristoph Hellwig 305aa4d8616SChristoph Hellwig /* 306456be148SChristoph Hellwig * This is the slow, transforming version that needs to double buffer the 307456be148SChristoph Hellwig * data as it cannot do the transformations in place without having direct 308456be148SChristoph Hellwig * access to the destination pages of the backing file. 3091da177e4SLinus Torvalds */ 310aa4d8616SChristoph Hellwig static int lo_write_transfer(struct loop_device *lo, struct request *rq, 311aa4d8616SChristoph Hellwig loff_t pos) 3121da177e4SLinus Torvalds { 313aa4d8616SChristoph Hellwig struct bio_vec bvec, b; 31430112013SMing Lei struct req_iterator iter; 315aa4d8616SChristoph Hellwig struct page *page; 3167988613bSKent Overstreet int ret = 0; 3171da177e4SLinus Torvalds 318aa4d8616SChristoph Hellwig page = alloc_page(GFP_NOIO); 3191da177e4SLinus Torvalds if (unlikely(!page)) 320aa4d8616SChristoph Hellwig return -ENOMEM; 321456be148SChristoph Hellwig 32230112013SMing Lei rq_for_each_segment(bvec, rq, iter) { 323aa4d8616SChristoph Hellwig ret = lo_do_transfer(lo, WRITE, page, 0, bvec.bv_page, 324aa4d8616SChristoph Hellwig bvec.bv_offset, bvec.bv_len, pos >> 9); 325aa4d8616SChristoph Hellwig if (unlikely(ret)) 326aa4d8616SChristoph Hellwig break; 327aa4d8616SChristoph Hellwig 328aa4d8616SChristoph Hellwig b.bv_page = page; 329aa4d8616SChristoph Hellwig b.bv_offset = 0; 330aa4d8616SChristoph Hellwig b.bv_len = bvec.bv_len; 331aa4d8616SChristoph Hellwig ret = lo_write_bvec(lo->lo_backing_file, &b, &pos); 3321da177e4SLinus Torvalds if (ret < 0) 3331da177e4SLinus Torvalds break; 3341da177e4SLinus Torvalds } 335aa4d8616SChristoph Hellwig 3361da177e4SLinus Torvalds __free_page(page); 3371da177e4SLinus Torvalds return ret; 3381da177e4SLinus Torvalds } 3391da177e4SLinus Torvalds 340aa4d8616SChristoph Hellwig static int lo_read_simple(struct loop_device *lo, struct request *rq, 341aa4d8616SChristoph Hellwig loff_t pos) 3421da177e4SLinus Torvalds { 3437988613bSKent Overstreet struct bio_vec bvec; 34430112013SMing Lei struct req_iterator iter; 345aa4d8616SChristoph Hellwig struct iov_iter i; 346aa4d8616SChristoph Hellwig ssize_t len; 3471da177e4SLinus Torvalds 34830112013SMing Lei rq_for_each_segment(bvec, rq, iter) { 349b6207430SChristoph Hellwig iov_iter_bvec(&i, READ, &bvec, 1, bvec.bv_len); 35018e9710eSChristoph Hellwig len = vfs_iter_read(lo->lo_backing_file, &i, &pos, 0); 351aa4d8616SChristoph Hellwig if (len < 0) 352aa4d8616SChristoph Hellwig return len; 353306df071SDave Young 354aa4d8616SChristoph Hellwig flush_dcache_page(bvec.bv_page); 355aa4d8616SChristoph Hellwig 356aa4d8616SChristoph Hellwig if (len != bvec.bv_len) { 35730112013SMing Lei struct bio *bio; 35830112013SMing Lei 35930112013SMing Lei __rq_for_each_bio(bio, rq) 360306df071SDave Young zero_fill_bio(bio); 3611da177e4SLinus Torvalds break; 362306df071SDave Young } 363aa4d8616SChristoph Hellwig cond_resched(); 3641da177e4SLinus Torvalds } 365aa4d8616SChristoph Hellwig 366306df071SDave Young return 0; 3671da177e4SLinus Torvalds } 3681da177e4SLinus Torvalds 369aa4d8616SChristoph Hellwig static int lo_read_transfer(struct loop_device *lo, struct request *rq, 370aa4d8616SChristoph Hellwig loff_t pos) 371aa4d8616SChristoph Hellwig { 372aa4d8616SChristoph Hellwig struct bio_vec bvec, b; 373aa4d8616SChristoph Hellwig struct req_iterator iter; 374aa4d8616SChristoph Hellwig struct iov_iter i; 375aa4d8616SChristoph Hellwig struct page *page; 376aa4d8616SChristoph Hellwig ssize_t len; 377aa4d8616SChristoph Hellwig int ret = 0; 378aa4d8616SChristoph Hellwig 379aa4d8616SChristoph Hellwig page = alloc_page(GFP_NOIO); 380aa4d8616SChristoph Hellwig if (unlikely(!page)) 381aa4d8616SChristoph Hellwig return -ENOMEM; 382aa4d8616SChristoph Hellwig 383aa4d8616SChristoph Hellwig rq_for_each_segment(bvec, rq, iter) { 384aa4d8616SChristoph Hellwig loff_t offset = pos; 385aa4d8616SChristoph Hellwig 386aa4d8616SChristoph Hellwig b.bv_page = page; 387aa4d8616SChristoph Hellwig b.bv_offset = 0; 388aa4d8616SChristoph Hellwig b.bv_len = bvec.bv_len; 389aa4d8616SChristoph Hellwig 390b6207430SChristoph Hellwig iov_iter_bvec(&i, READ, &b, 1, b.bv_len); 39118e9710eSChristoph Hellwig len = vfs_iter_read(lo->lo_backing_file, &i, &pos, 0); 392aa4d8616SChristoph Hellwig if (len < 0) { 393aa4d8616SChristoph Hellwig ret = len; 394aa4d8616SChristoph Hellwig goto out_free_page; 395aa4d8616SChristoph Hellwig } 396aa4d8616SChristoph Hellwig 397aa4d8616SChristoph Hellwig ret = lo_do_transfer(lo, READ, page, 0, bvec.bv_page, 398aa4d8616SChristoph Hellwig bvec.bv_offset, len, offset >> 9); 399aa4d8616SChristoph Hellwig if (ret) 400aa4d8616SChristoph Hellwig goto out_free_page; 401aa4d8616SChristoph Hellwig 402aa4d8616SChristoph Hellwig flush_dcache_page(bvec.bv_page); 403aa4d8616SChristoph Hellwig 404aa4d8616SChristoph Hellwig if (len != bvec.bv_len) { 405aa4d8616SChristoph Hellwig struct bio *bio; 406aa4d8616SChristoph Hellwig 407aa4d8616SChristoph Hellwig __rq_for_each_bio(bio, rq) 408aa4d8616SChristoph Hellwig zero_fill_bio(bio); 409aa4d8616SChristoph Hellwig break; 410aa4d8616SChristoph Hellwig } 411aa4d8616SChristoph Hellwig } 412aa4d8616SChristoph Hellwig 413aa4d8616SChristoph Hellwig ret = 0; 414aa4d8616SChristoph Hellwig out_free_page: 415aa4d8616SChristoph Hellwig __free_page(page); 416aa4d8616SChristoph Hellwig return ret; 417aa4d8616SChristoph Hellwig } 418aa4d8616SChristoph Hellwig 419efcfec57SDarrick J. Wong static int lo_fallocate(struct loop_device *lo, struct request *rq, loff_t pos, 420efcfec57SDarrick J. Wong int mode) 421cf655d95SMing Lei { 422cf655d95SMing Lei /* 423efcfec57SDarrick J. Wong * We use fallocate to manipulate the space mappings used by the image 424efcfec57SDarrick J. Wong * a.k.a. discard/zerorange. However we do not support this if 425efcfec57SDarrick J. Wong * encryption is enabled, because it may give an attacker useful 426efcfec57SDarrick J. Wong * information. 427cf655d95SMing Lei */ 428cf655d95SMing Lei struct file *file = lo->lo_backing_file; 429c52abf56SEvan Green struct request_queue *q = lo->lo_queue; 430cf655d95SMing Lei int ret; 431cf655d95SMing Lei 432efcfec57SDarrick J. Wong mode |= FALLOC_FL_KEEP_SIZE; 433efcfec57SDarrick J. Wong 434c52abf56SEvan Green if (!blk_queue_discard(q)) { 435cf655d95SMing Lei ret = -EOPNOTSUPP; 436cf655d95SMing Lei goto out; 437cf655d95SMing Lei } 438cf655d95SMing Lei 439cf655d95SMing Lei ret = file->f_op->fallocate(file, mode, pos, blk_rq_bytes(rq)); 440cf655d95SMing Lei if (unlikely(ret && ret != -EINVAL && ret != -EOPNOTSUPP)) 441cf655d95SMing Lei ret = -EIO; 442cf655d95SMing Lei out: 443cf655d95SMing Lei return ret; 444cf655d95SMing Lei } 445cf655d95SMing Lei 446cf655d95SMing Lei static int lo_req_flush(struct loop_device *lo, struct request *rq) 447cf655d95SMing Lei { 448cf655d95SMing Lei struct file *file = lo->lo_backing_file; 449cf655d95SMing Lei int ret = vfs_fsync(file, 0); 450cf655d95SMing Lei if (unlikely(ret && ret != -EINVAL)) 451cf655d95SMing Lei ret = -EIO; 452cf655d95SMing Lei 453cf655d95SMing Lei return ret; 454cf655d95SMing Lei } 455cf655d95SMing Lei 456fe2cb290SChristoph Hellwig static void lo_complete_rq(struct request *rq) 457bc07c10aSMing Lei { 458fe2cb290SChristoph Hellwig struct loop_cmd *cmd = blk_mq_rq_to_pdu(rq); 459f9de14bcSJens Axboe blk_status_t ret = BLK_STS_OK; 460bc07c10aSMing Lei 461f9de14bcSJens Axboe if (!cmd->use_aio || cmd->ret < 0 || cmd->ret == blk_rq_bytes(rq) || 462f9de14bcSJens Axboe req_op(rq) != REQ_OP_READ) { 463f9de14bcSJens Axboe if (cmd->ret < 0) 4648cd55087SEvan Green ret = errno_to_blk_status(cmd->ret); 465f9de14bcSJens Axboe goto end_io; 466bc07c10aSMing Lei } 467fe2cb290SChristoph Hellwig 468f9de14bcSJens Axboe /* 469f9de14bcSJens Axboe * Short READ - if we got some data, advance our request and 470f9de14bcSJens Axboe * retry it. If we got no data, end the rest with EIO. 471f9de14bcSJens Axboe */ 472f9de14bcSJens Axboe if (cmd->ret) { 473f9de14bcSJens Axboe blk_update_request(rq, BLK_STS_OK, cmd->ret); 474f9de14bcSJens Axboe cmd->ret = 0; 475f9de14bcSJens Axboe blk_mq_requeue_request(rq, true); 476f9de14bcSJens Axboe } else { 477f9de14bcSJens Axboe if (cmd->use_aio) { 478f9de14bcSJens Axboe struct bio *bio = rq->bio; 479f9de14bcSJens Axboe 480f9de14bcSJens Axboe while (bio) { 481f9de14bcSJens Axboe zero_fill_bio(bio); 482f9de14bcSJens Axboe bio = bio->bi_next; 483f9de14bcSJens Axboe } 484f9de14bcSJens Axboe } 485f9de14bcSJens Axboe ret = BLK_STS_IOERR; 486f9de14bcSJens Axboe end_io: 487f9de14bcSJens Axboe blk_mq_end_request(rq, ret); 488f9de14bcSJens Axboe } 489bc07c10aSMing Lei } 490bc07c10aSMing Lei 49192d77332SShaohua Li static void lo_rw_aio_do_completion(struct loop_cmd *cmd) 49292d77332SShaohua Li { 4931894e916SJens Axboe struct request *rq = blk_mq_rq_from_pdu(cmd); 4941894e916SJens Axboe 49592d77332SShaohua Li if (!atomic_dec_and_test(&cmd->ref)) 49692d77332SShaohua Li return; 49792d77332SShaohua Li kfree(cmd->bvec); 49892d77332SShaohua Li cmd->bvec = NULL; 4991894e916SJens Axboe blk_mq_complete_request(rq); 50092d77332SShaohua Li } 50192d77332SShaohua Li 502bc07c10aSMing Lei static void lo_rw_aio_complete(struct kiocb *iocb, long ret, long ret2) 503bc07c10aSMing Lei { 504bc07c10aSMing Lei struct loop_cmd *cmd = container_of(iocb, struct loop_cmd, iocb); 505bc07c10aSMing Lei 506d4478e92SShaohua Li if (cmd->css) 507d4478e92SShaohua Li css_put(cmd->css); 508fe2cb290SChristoph Hellwig cmd->ret = ret; 50992d77332SShaohua Li lo_rw_aio_do_completion(cmd); 510bc07c10aSMing Lei } 511bc07c10aSMing Lei 512bc07c10aSMing Lei static int lo_rw_aio(struct loop_device *lo, struct loop_cmd *cmd, 513bc07c10aSMing Lei loff_t pos, bool rw) 514bc07c10aSMing Lei { 515bc07c10aSMing Lei struct iov_iter iter; 51686af5952SMing Lei struct req_iterator rq_iter; 517bc07c10aSMing Lei struct bio_vec *bvec; 5181894e916SJens Axboe struct request *rq = blk_mq_rq_from_pdu(cmd); 51940326d8aSShaohua Li struct bio *bio = rq->bio; 520bc07c10aSMing Lei struct file *file = lo->lo_backing_file; 52186af5952SMing Lei struct bio_vec tmp; 52240326d8aSShaohua Li unsigned int offset; 52386af5952SMing Lei int nr_bvec = 0; 524bc07c10aSMing Lei int ret; 525bc07c10aSMing Lei 52686af5952SMing Lei rq_for_each_bvec(tmp, rq, rq_iter) 52786af5952SMing Lei nr_bvec++; 528bc07c10aSMing Lei 52986af5952SMing Lei if (rq->bio != rq->biotail) { 53086af5952SMing Lei 53186af5952SMing Lei bvec = kmalloc_array(nr_bvec, sizeof(struct bio_vec), 5326da2ec56SKees Cook GFP_NOIO); 53340326d8aSShaohua Li if (!bvec) 53440326d8aSShaohua Li return -EIO; 53540326d8aSShaohua Li cmd->bvec = bvec; 53640326d8aSShaohua Li 537a7297a6aSMing Lei /* 53840326d8aSShaohua Li * The bios of the request may be started from the middle of 53940326d8aSShaohua Li * the 'bvec' because of bio splitting, so we can't directly 54086af5952SMing Lei * copy bio->bi_iov_vec to new bvec. The rq_for_each_bvec 54140326d8aSShaohua Li * API will take care of all details for us. 542a7297a6aSMing Lei */ 54386af5952SMing Lei rq_for_each_bvec(tmp, rq, rq_iter) { 54440326d8aSShaohua Li *bvec = tmp; 54540326d8aSShaohua Li bvec++; 54640326d8aSShaohua Li } 54740326d8aSShaohua Li bvec = cmd->bvec; 54840326d8aSShaohua Li offset = 0; 54940326d8aSShaohua Li } else { 55040326d8aSShaohua Li /* 55140326d8aSShaohua Li * Same here, this bio may be started from the middle of the 55240326d8aSShaohua Li * 'bvec' because of bio splitting, so offset from the bvec 55340326d8aSShaohua Li * must be passed to iov iterator 55440326d8aSShaohua Li */ 55540326d8aSShaohua Li offset = bio->bi_iter.bi_bvec_done; 55640326d8aSShaohua Li bvec = __bvec_iter_bvec(bio->bi_io_vec, bio->bi_iter); 55740326d8aSShaohua Li } 55892d77332SShaohua Li atomic_set(&cmd->ref, 2); 55940326d8aSShaohua Li 560b6207430SChristoph Hellwig iov_iter_bvec(&iter, rw, bvec, nr_bvec, blk_rq_bytes(rq)); 56140326d8aSShaohua Li iter.iov_offset = offset; 562bc07c10aSMing Lei 563bc07c10aSMing Lei cmd->iocb.ki_pos = pos; 564bc07c10aSMing Lei cmd->iocb.ki_filp = file; 565bc07c10aSMing Lei cmd->iocb.ki_complete = lo_rw_aio_complete; 566bc07c10aSMing Lei cmd->iocb.ki_flags = IOCB_DIRECT; 567d9a08a9eSAdam Manzanares cmd->iocb.ki_ioprio = IOPRIO_PRIO_VALUE(IOPRIO_CLASS_NONE, 0); 568d4478e92SShaohua Li if (cmd->css) 569d4478e92SShaohua Li kthread_associate_blkcg(cmd->css); 570bc07c10aSMing Lei 571bc07c10aSMing Lei if (rw == WRITE) 572bb7462b6SMiklos Szeredi ret = call_write_iter(file, &cmd->iocb, &iter); 573bc07c10aSMing Lei else 574bb7462b6SMiklos Szeredi ret = call_read_iter(file, &cmd->iocb, &iter); 575bc07c10aSMing Lei 57692d77332SShaohua Li lo_rw_aio_do_completion(cmd); 577d4478e92SShaohua Li kthread_associate_blkcg(NULL); 57892d77332SShaohua Li 579bc07c10aSMing Lei if (ret != -EIOCBQUEUED) 580bc07c10aSMing Lei cmd->iocb.ki_complete(&cmd->iocb, ret, 0); 581bc07c10aSMing Lei return 0; 582bc07c10aSMing Lei } 583bc07c10aSMing Lei 584c1c87c2bSChristoph Hellwig static int do_req_filebacked(struct loop_device *lo, struct request *rq) 585bc07c10aSMing Lei { 586bc07c10aSMing Lei struct loop_cmd *cmd = blk_mq_rq_to_pdu(rq); 587c1c87c2bSChristoph Hellwig loff_t pos = ((loff_t) blk_rq_pos(rq) << 9) + lo->lo_offset; 588bc07c10aSMing Lei 589bc07c10aSMing Lei /* 590bc07c10aSMing Lei * lo_write_simple and lo_read_simple should have been covered 591bc07c10aSMing Lei * by io submit style function like lo_rw_aio(), one blocker 592bc07c10aSMing Lei * is that lo_read_simple() need to call flush_dcache_page after 593bc07c10aSMing Lei * the page is written from kernel, and it isn't easy to handle 594bc07c10aSMing Lei * this in io submit style function which submits all segments 595bc07c10aSMing Lei * of the req at one time. And direct read IO doesn't need to 596bc07c10aSMing Lei * run flush_dcache_page(). 597bc07c10aSMing Lei */ 598c1c87c2bSChristoph Hellwig switch (req_op(rq)) { 599c1c87c2bSChristoph Hellwig case REQ_OP_FLUSH: 600c1c87c2bSChristoph Hellwig return lo_req_flush(lo, rq); 60119372e27SChristoph Hellwig case REQ_OP_WRITE_ZEROES: 602efcfec57SDarrick J. Wong /* 603efcfec57SDarrick J. Wong * If the caller doesn't want deallocation, call zeroout to 604efcfec57SDarrick J. Wong * write zeroes the range. Otherwise, punch them out. 605efcfec57SDarrick J. Wong */ 606efcfec57SDarrick J. Wong return lo_fallocate(lo, rq, pos, 607efcfec57SDarrick J. Wong (rq->cmd_flags & REQ_NOUNMAP) ? 608efcfec57SDarrick J. Wong FALLOC_FL_ZERO_RANGE : 609efcfec57SDarrick J. Wong FALLOC_FL_PUNCH_HOLE); 610efcfec57SDarrick J. Wong case REQ_OP_DISCARD: 611efcfec57SDarrick J. Wong return lo_fallocate(lo, rq, pos, FALLOC_FL_PUNCH_HOLE); 612c1c87c2bSChristoph Hellwig case REQ_OP_WRITE: 613c1c87c2bSChristoph Hellwig if (lo->transfer) 614c1c87c2bSChristoph Hellwig return lo_write_transfer(lo, rq, pos); 615c1c87c2bSChristoph Hellwig else if (cmd->use_aio) 616c1c87c2bSChristoph Hellwig return lo_rw_aio(lo, cmd, pos, WRITE); 617c1c87c2bSChristoph Hellwig else 618bc07c10aSMing Lei return lo_write_simple(lo, rq, pos); 619c1c87c2bSChristoph Hellwig case REQ_OP_READ: 620c1c87c2bSChristoph Hellwig if (lo->transfer) 621c1c87c2bSChristoph Hellwig return lo_read_transfer(lo, rq, pos); 622c1c87c2bSChristoph Hellwig else if (cmd->use_aio) 623c1c87c2bSChristoph Hellwig return lo_rw_aio(lo, cmd, pos, READ); 624bc07c10aSMing Lei else 625bc07c10aSMing Lei return lo_read_simple(lo, rq, pos); 626c1c87c2bSChristoph Hellwig default: 627c1c87c2bSChristoph Hellwig WARN_ON_ONCE(1); 628c1c87c2bSChristoph Hellwig return -EIO; 629bc07c10aSMing Lei } 6301da177e4SLinus Torvalds } 6311da177e4SLinus Torvalds 6322e5ab5f3SMing Lei static inline void loop_update_dio(struct loop_device *lo) 6332e5ab5f3SMing Lei { 6342e5ab5f3SMing Lei __loop_update_dio(lo, io_is_direct(lo->lo_backing_file) | 6352e5ab5f3SMing Lei lo->use_dio); 6362e5ab5f3SMing Lei } 6372e5ab5f3SMing Lei 63806f0e9e6SMing Lei static void loop_reread_partitions(struct loop_device *lo, 63906f0e9e6SMing Lei struct block_device *bdev) 64006f0e9e6SMing Lei { 64106f0e9e6SMing Lei int rc; 64206f0e9e6SMing Lei 643f0b870dfSChristoph Hellwig mutex_lock(&bdev->bd_mutex); 644f0b870dfSChristoph Hellwig rc = bdev_disk_changed(bdev, false); 645f0b870dfSChristoph Hellwig mutex_unlock(&bdev->bd_mutex); 64606f0e9e6SMing Lei if (rc) 64706f0e9e6SMing Lei pr_warn("%s: partition scan of loop%d (%s) failed (rc=%d)\n", 64806f0e9e6SMing Lei __func__, lo->lo_number, lo->lo_file_name, rc); 64906f0e9e6SMing Lei } 65006f0e9e6SMing Lei 651d2ac838eSTheodore Ts'o static inline int is_loop_device(struct file *file) 652d2ac838eSTheodore Ts'o { 653d2ac838eSTheodore Ts'o struct inode *i = file->f_mapping->host; 654d2ac838eSTheodore Ts'o 655d2ac838eSTheodore Ts'o return i && S_ISBLK(i->i_mode) && MAJOR(i->i_rdev) == LOOP_MAJOR; 656d2ac838eSTheodore Ts'o } 657d2ac838eSTheodore Ts'o 658d2ac838eSTheodore Ts'o static int loop_validate_file(struct file *file, struct block_device *bdev) 659d2ac838eSTheodore Ts'o { 660d2ac838eSTheodore Ts'o struct inode *inode = file->f_mapping->host; 661d2ac838eSTheodore Ts'o struct file *f = file; 662d2ac838eSTheodore Ts'o 663d2ac838eSTheodore Ts'o /* Avoid recursion */ 664d2ac838eSTheodore Ts'o while (is_loop_device(f)) { 665d2ac838eSTheodore Ts'o struct loop_device *l; 666d2ac838eSTheodore Ts'o 667d2ac838eSTheodore Ts'o if (f->f_mapping->host->i_bdev == bdev) 668d2ac838eSTheodore Ts'o return -EBADF; 669d2ac838eSTheodore Ts'o 670d2ac838eSTheodore Ts'o l = f->f_mapping->host->i_bdev->bd_disk->private_data; 671f7c8a412SDongli Zhang if (l->lo_state != Lo_bound) { 672d2ac838eSTheodore Ts'o return -EINVAL; 673d2ac838eSTheodore Ts'o } 674d2ac838eSTheodore Ts'o f = l->lo_backing_file; 675d2ac838eSTheodore Ts'o } 676d2ac838eSTheodore Ts'o if (!S_ISREG(inode->i_mode) && !S_ISBLK(inode->i_mode)) 677d2ac838eSTheodore Ts'o return -EINVAL; 678d2ac838eSTheodore Ts'o return 0; 679d2ac838eSTheodore Ts'o } 680d2ac838eSTheodore Ts'o 6811da177e4SLinus Torvalds /* 6821da177e4SLinus Torvalds * loop_change_fd switched the backing store of a loopback device to 6831da177e4SLinus Torvalds * a new file. This is useful for operating system installers to free up 6841da177e4SLinus Torvalds * the original file and in High Availability environments to switch to 6851da177e4SLinus Torvalds * an alternative location for the content in case of server meltdown. 6861da177e4SLinus Torvalds * This can only work if the loop device is used read-only, and if the 6871da177e4SLinus Torvalds * new backing store is the same size and type as the old backing store. 6881da177e4SLinus Torvalds */ 689bb214884SAl Viro static int loop_change_fd(struct loop_device *lo, struct block_device *bdev, 690bb214884SAl Viro unsigned int arg) 6911da177e4SLinus Torvalds { 6921dded9acSJan Kara struct file *file = NULL, *old_file; 6931da177e4SLinus Torvalds int error; 69485b0a54aSJan Kara bool partscan; 6951da177e4SLinus Torvalds 696c28445faSJan Kara error = mutex_lock_killable(&loop_ctl_mutex); 697c3710770SJan Kara if (error) 698c3710770SJan Kara return error; 6991da177e4SLinus Torvalds error = -ENXIO; 7001da177e4SLinus Torvalds if (lo->lo_state != Lo_bound) 7011dded9acSJan Kara goto out_err; 7021da177e4SLinus Torvalds 7031da177e4SLinus Torvalds /* the loop device has to be read-only */ 7041da177e4SLinus Torvalds error = -EINVAL; 7051da177e4SLinus Torvalds if (!(lo->lo_flags & LO_FLAGS_READ_ONLY)) 7061dded9acSJan Kara goto out_err; 7071da177e4SLinus Torvalds 7081da177e4SLinus Torvalds error = -EBADF; 7091da177e4SLinus Torvalds file = fget(arg); 7101da177e4SLinus Torvalds if (!file) 7111dded9acSJan Kara goto out_err; 7121da177e4SLinus Torvalds 713d2ac838eSTheodore Ts'o error = loop_validate_file(file, bdev); 714d2ac838eSTheodore Ts'o if (error) 7151dded9acSJan Kara goto out_err; 716d2ac838eSTheodore Ts'o 7171da177e4SLinus Torvalds old_file = lo->lo_backing_file; 7181da177e4SLinus Torvalds 7191da177e4SLinus Torvalds error = -EINVAL; 7201da177e4SLinus Torvalds 7211da177e4SLinus Torvalds /* size of the new backing store needs to be the same */ 7221da177e4SLinus Torvalds if (get_loop_size(lo, file) != get_loop_size(lo, old_file)) 7231dded9acSJan Kara goto out_err; 7241da177e4SLinus Torvalds 7251da177e4SLinus Torvalds /* and ... switch */ 72643cade80SOmar Sandoval blk_mq_freeze_queue(lo->lo_queue); 72743cade80SOmar Sandoval mapping_set_gfp_mask(old_file->f_mapping, lo->old_gfp_mask); 72843cade80SOmar Sandoval lo->lo_backing_file = file; 72943cade80SOmar Sandoval lo->old_gfp_mask = mapping_gfp_mask(file->f_mapping); 73043cade80SOmar Sandoval mapping_set_gfp_mask(file->f_mapping, 73143cade80SOmar Sandoval lo->old_gfp_mask & ~(__GFP_IO|__GFP_FS)); 73243cade80SOmar Sandoval loop_update_dio(lo); 73343cade80SOmar Sandoval blk_mq_unfreeze_queue(lo->lo_queue); 73485b0a54aSJan Kara partscan = lo->lo_flags & LO_FLAGS_PARTSCAN; 735c3710770SJan Kara mutex_unlock(&loop_ctl_mutex); 7361dded9acSJan Kara /* 7371dded9acSJan Kara * We must drop file reference outside of loop_ctl_mutex as dropping 7381dded9acSJan Kara * the file ref can take bd_mutex which creates circular locking 7391dded9acSJan Kara * dependency. 7401dded9acSJan Kara */ 7411dded9acSJan Kara fput(old_file); 74285b0a54aSJan Kara if (partscan) 74385b0a54aSJan Kara loop_reread_partitions(lo, bdev); 7441da177e4SLinus Torvalds return 0; 7451da177e4SLinus Torvalds 7461dded9acSJan Kara out_err: 747c3710770SJan Kara mutex_unlock(&loop_ctl_mutex); 7481dded9acSJan Kara if (file) 7491dded9acSJan Kara fput(file); 7501da177e4SLinus Torvalds return error; 7511da177e4SLinus Torvalds } 7521da177e4SLinus Torvalds 753ee862730SMilan Broz /* loop sysfs attributes */ 754ee862730SMilan Broz 755ee862730SMilan Broz static ssize_t loop_attr_show(struct device *dev, char *page, 756ee862730SMilan Broz ssize_t (*callback)(struct loop_device *, char *)) 757ee862730SMilan Broz { 75834dd82afSKay Sievers struct gendisk *disk = dev_to_disk(dev); 75934dd82afSKay Sievers struct loop_device *lo = disk->private_data; 760ee862730SMilan Broz 76134dd82afSKay Sievers return callback(lo, page); 762ee862730SMilan Broz } 763ee862730SMilan Broz 764ee862730SMilan Broz #define LOOP_ATTR_RO(_name) \ 765ee862730SMilan Broz static ssize_t loop_attr_##_name##_show(struct loop_device *, char *); \ 766ee862730SMilan Broz static ssize_t loop_attr_do_show_##_name(struct device *d, \ 767ee862730SMilan Broz struct device_attribute *attr, char *b) \ 768ee862730SMilan Broz { \ 769ee862730SMilan Broz return loop_attr_show(d, b, loop_attr_##_name##_show); \ 770ee862730SMilan Broz } \ 771ee862730SMilan Broz static struct device_attribute loop_attr_##_name = \ 7725657a819SJoe Perches __ATTR(_name, 0444, loop_attr_do_show_##_name, NULL); 773ee862730SMilan Broz 774ee862730SMilan Broz static ssize_t loop_attr_backing_file_show(struct loop_device *lo, char *buf) 775ee862730SMilan Broz { 776ee862730SMilan Broz ssize_t ret; 777ee862730SMilan Broz char *p = NULL; 778ee862730SMilan Broz 77905eb0f25SKay Sievers spin_lock_irq(&lo->lo_lock); 780ee862730SMilan Broz if (lo->lo_backing_file) 7819bf39ab2SMiklos Szeredi p = file_path(lo->lo_backing_file, buf, PAGE_SIZE - 1); 78205eb0f25SKay Sievers spin_unlock_irq(&lo->lo_lock); 783ee862730SMilan Broz 784ee862730SMilan Broz if (IS_ERR_OR_NULL(p)) 785ee862730SMilan Broz ret = PTR_ERR(p); 786ee862730SMilan Broz else { 787ee862730SMilan Broz ret = strlen(p); 788ee862730SMilan Broz memmove(buf, p, ret); 789ee862730SMilan Broz buf[ret++] = '\n'; 790ee862730SMilan Broz buf[ret] = 0; 791ee862730SMilan Broz } 792ee862730SMilan Broz 793ee862730SMilan Broz return ret; 794ee862730SMilan Broz } 795ee862730SMilan Broz 796ee862730SMilan Broz static ssize_t loop_attr_offset_show(struct loop_device *lo, char *buf) 797ee862730SMilan Broz { 798ee862730SMilan Broz return sprintf(buf, "%llu\n", (unsigned long long)lo->lo_offset); 799ee862730SMilan Broz } 800ee862730SMilan Broz 801ee862730SMilan Broz static ssize_t loop_attr_sizelimit_show(struct loop_device *lo, char *buf) 802ee862730SMilan Broz { 803ee862730SMilan Broz return sprintf(buf, "%llu\n", (unsigned long long)lo->lo_sizelimit); 804ee862730SMilan Broz } 805ee862730SMilan Broz 806ee862730SMilan Broz static ssize_t loop_attr_autoclear_show(struct loop_device *lo, char *buf) 807ee862730SMilan Broz { 808ee862730SMilan Broz int autoclear = (lo->lo_flags & LO_FLAGS_AUTOCLEAR); 809ee862730SMilan Broz 810ee862730SMilan Broz return sprintf(buf, "%s\n", autoclear ? "1" : "0"); 811ee862730SMilan Broz } 812ee862730SMilan Broz 813e03c8dd1SKay Sievers static ssize_t loop_attr_partscan_show(struct loop_device *lo, char *buf) 814e03c8dd1SKay Sievers { 815e03c8dd1SKay Sievers int partscan = (lo->lo_flags & LO_FLAGS_PARTSCAN); 816e03c8dd1SKay Sievers 817e03c8dd1SKay Sievers return sprintf(buf, "%s\n", partscan ? "1" : "0"); 818e03c8dd1SKay Sievers } 819e03c8dd1SKay Sievers 8202e5ab5f3SMing Lei static ssize_t loop_attr_dio_show(struct loop_device *lo, char *buf) 8212e5ab5f3SMing Lei { 8222e5ab5f3SMing Lei int dio = (lo->lo_flags & LO_FLAGS_DIRECT_IO); 8232e5ab5f3SMing Lei 8242e5ab5f3SMing Lei return sprintf(buf, "%s\n", dio ? "1" : "0"); 8252e5ab5f3SMing Lei } 8262e5ab5f3SMing Lei 827ee862730SMilan Broz LOOP_ATTR_RO(backing_file); 828ee862730SMilan Broz LOOP_ATTR_RO(offset); 829ee862730SMilan Broz LOOP_ATTR_RO(sizelimit); 830ee862730SMilan Broz LOOP_ATTR_RO(autoclear); 831e03c8dd1SKay Sievers LOOP_ATTR_RO(partscan); 8322e5ab5f3SMing Lei LOOP_ATTR_RO(dio); 833ee862730SMilan Broz 834ee862730SMilan Broz static struct attribute *loop_attrs[] = { 835ee862730SMilan Broz &loop_attr_backing_file.attr, 836ee862730SMilan Broz &loop_attr_offset.attr, 837ee862730SMilan Broz &loop_attr_sizelimit.attr, 838ee862730SMilan Broz &loop_attr_autoclear.attr, 839e03c8dd1SKay Sievers &loop_attr_partscan.attr, 8402e5ab5f3SMing Lei &loop_attr_dio.attr, 841ee862730SMilan Broz NULL, 842ee862730SMilan Broz }; 843ee862730SMilan Broz 844ee862730SMilan Broz static struct attribute_group loop_attribute_group = { 845ee862730SMilan Broz .name = "loop", 846ee862730SMilan Broz .attrs= loop_attrs, 847ee862730SMilan Broz }; 848ee862730SMilan Broz 849d3349b6bSTetsuo Handa static void loop_sysfs_init(struct loop_device *lo) 850ee862730SMilan Broz { 851d3349b6bSTetsuo Handa lo->sysfs_inited = !sysfs_create_group(&disk_to_dev(lo->lo_disk)->kobj, 852ee862730SMilan Broz &loop_attribute_group); 853ee862730SMilan Broz } 854ee862730SMilan Broz 855ee862730SMilan Broz static void loop_sysfs_exit(struct loop_device *lo) 856ee862730SMilan Broz { 857d3349b6bSTetsuo Handa if (lo->sysfs_inited) 858ee862730SMilan Broz sysfs_remove_group(&disk_to_dev(lo->lo_disk)->kobj, 859ee862730SMilan Broz &loop_attribute_group); 860ee862730SMilan Broz } 861ee862730SMilan Broz 862dfaa2ef6SLukas Czerner static void loop_config_discard(struct loop_device *lo) 863dfaa2ef6SLukas Czerner { 864dfaa2ef6SLukas Czerner struct file *file = lo->lo_backing_file; 865dfaa2ef6SLukas Czerner struct inode *inode = file->f_mapping->host; 866dfaa2ef6SLukas Czerner struct request_queue *q = lo->lo_queue; 867dfaa2ef6SLukas Czerner 868dfaa2ef6SLukas Czerner /* 869c52abf56SEvan Green * If the backing device is a block device, mirror its zeroing 870c52abf56SEvan Green * capability. Set the discard sectors to the block device's zeroing 871c52abf56SEvan Green * capabilities because loop discards result in blkdev_issue_zeroout(), 872c52abf56SEvan Green * not blkdev_issue_discard(). This maintains consistent behavior with 873c52abf56SEvan Green * file-backed loop devices: discarded regions read back as zero. 874c52abf56SEvan Green */ 875c52abf56SEvan Green if (S_ISBLK(inode->i_mode) && !lo->lo_encrypt_key_size) { 876c52abf56SEvan Green struct request_queue *backingq; 877c52abf56SEvan Green 878c52abf56SEvan Green backingq = bdev_get_queue(inode->i_bdev); 879c52abf56SEvan Green blk_queue_max_discard_sectors(q, 880c52abf56SEvan Green backingq->limits.max_write_zeroes_sectors); 881c52abf56SEvan Green 882c52abf56SEvan Green blk_queue_max_write_zeroes_sectors(q, 883c52abf56SEvan Green backingq->limits.max_write_zeroes_sectors); 884c52abf56SEvan Green 885c52abf56SEvan Green /* 886dfaa2ef6SLukas Czerner * We use punch hole to reclaim the free space used by the 88712a64d2fSOlaf Hering * image a.k.a. discard. However we do not support discard if 888dfaa2ef6SLukas Czerner * encryption is enabled, because it may give an attacker 889dfaa2ef6SLukas Czerner * useful information. 890dfaa2ef6SLukas Czerner */ 891c52abf56SEvan Green } else if (!file->f_op->fallocate || lo->lo_encrypt_key_size) { 892dfaa2ef6SLukas Czerner q->limits.discard_granularity = 0; 893dfaa2ef6SLukas Czerner q->limits.discard_alignment = 0; 8942bb4cd5cSJens Axboe blk_queue_max_discard_sectors(q, 0); 89519372e27SChristoph Hellwig blk_queue_max_write_zeroes_sectors(q, 0); 896dfaa2ef6SLukas Czerner 897c52abf56SEvan Green } else { 898dfaa2ef6SLukas Czerner q->limits.discard_granularity = inode->i_sb->s_blocksize; 899dfaf3c03SLukas Czerner q->limits.discard_alignment = 0; 900f2c6df7dSHannes Reinecke 9011e6ec9eaSOmar Sandoval blk_queue_max_discard_sectors(q, UINT_MAX >> 9); 9021e6ec9eaSOmar Sandoval blk_queue_max_write_zeroes_sectors(q, UINT_MAX >> 9); 903c52abf56SEvan Green } 904c52abf56SEvan Green 905c52abf56SEvan Green if (q->limits.max_write_zeroes_sectors) 9068b904b5bSBart Van Assche blk_queue_flag_set(QUEUE_FLAG_DISCARD, q); 907c52abf56SEvan Green else 908c52abf56SEvan Green blk_queue_flag_clear(QUEUE_FLAG_DISCARD, q); 909dfaa2ef6SLukas Czerner } 910dfaa2ef6SLukas Czerner 911e03a3d7aSMing Lei static void loop_unprepare_queue(struct loop_device *lo) 912e03a3d7aSMing Lei { 9133989144fSPetr Mladek kthread_flush_worker(&lo->worker); 914e03a3d7aSMing Lei kthread_stop(lo->worker_task); 915e03a3d7aSMing Lei } 916e03a3d7aSMing Lei 917b2ee7d46SNeilBrown static int loop_kthread_worker_fn(void *worker_ptr) 918b2ee7d46SNeilBrown { 919d0a255e7SMikulas Patocka current->flags |= PF_LESS_THROTTLE | PF_MEMALLOC_NOIO; 920b2ee7d46SNeilBrown return kthread_worker_fn(worker_ptr); 921b2ee7d46SNeilBrown } 922b2ee7d46SNeilBrown 923e03a3d7aSMing Lei static int loop_prepare_queue(struct loop_device *lo) 924e03a3d7aSMing Lei { 9253989144fSPetr Mladek kthread_init_worker(&lo->worker); 926b2ee7d46SNeilBrown lo->worker_task = kthread_run(loop_kthread_worker_fn, 927e03a3d7aSMing Lei &lo->worker, "loop%d", lo->lo_number); 928e03a3d7aSMing Lei if (IS_ERR(lo->worker_task)) 929e03a3d7aSMing Lei return -ENOMEM; 930e03a3d7aSMing Lei set_user_nice(lo->worker_task, MIN_NICE); 931e03a3d7aSMing Lei return 0; 932e03a3d7aSMing Lei } 933e03a3d7aSMing Lei 93456a85fd8SHolger Hoffstätte static void loop_update_rotational(struct loop_device *lo) 93556a85fd8SHolger Hoffstätte { 93656a85fd8SHolger Hoffstätte struct file *file = lo->lo_backing_file; 93756a85fd8SHolger Hoffstätte struct inode *file_inode = file->f_mapping->host; 93856a85fd8SHolger Hoffstätte struct block_device *file_bdev = file_inode->i_sb->s_bdev; 93956a85fd8SHolger Hoffstätte struct request_queue *q = lo->lo_queue; 94056a85fd8SHolger Hoffstätte bool nonrot = true; 94156a85fd8SHolger Hoffstätte 94256a85fd8SHolger Hoffstätte /* not all filesystems (e.g. tmpfs) have a sb->s_bdev */ 94356a85fd8SHolger Hoffstätte if (file_bdev) 94456a85fd8SHolger Hoffstätte nonrot = blk_queue_nonrot(bdev_get_queue(file_bdev)); 94556a85fd8SHolger Hoffstätte 94656a85fd8SHolger Hoffstätte if (nonrot) 94756a85fd8SHolger Hoffstätte blk_queue_flag_set(QUEUE_FLAG_NONROT, q); 94856a85fd8SHolger Hoffstätte else 94956a85fd8SHolger Hoffstätte blk_queue_flag_clear(QUEUE_FLAG_NONROT, q); 95056a85fd8SHolger Hoffstätte } 95156a85fd8SHolger Hoffstätte 952*62ab466cSMartijn Coenen static int 953*62ab466cSMartijn Coenen loop_release_xfer(struct loop_device *lo) 954*62ab466cSMartijn Coenen { 955*62ab466cSMartijn Coenen int err = 0; 956*62ab466cSMartijn Coenen struct loop_func_table *xfer = lo->lo_encryption; 957*62ab466cSMartijn Coenen 958*62ab466cSMartijn Coenen if (xfer) { 959*62ab466cSMartijn Coenen if (xfer->release) 960*62ab466cSMartijn Coenen err = xfer->release(lo); 961*62ab466cSMartijn Coenen lo->transfer = NULL; 962*62ab466cSMartijn Coenen lo->lo_encryption = NULL; 963*62ab466cSMartijn Coenen module_put(xfer->owner); 964*62ab466cSMartijn Coenen } 965*62ab466cSMartijn Coenen return err; 966*62ab466cSMartijn Coenen } 967*62ab466cSMartijn Coenen 968*62ab466cSMartijn Coenen static int 969*62ab466cSMartijn Coenen loop_init_xfer(struct loop_device *lo, struct loop_func_table *xfer, 970*62ab466cSMartijn Coenen const struct loop_info64 *i) 971*62ab466cSMartijn Coenen { 972*62ab466cSMartijn Coenen int err = 0; 973*62ab466cSMartijn Coenen 974*62ab466cSMartijn Coenen if (xfer) { 975*62ab466cSMartijn Coenen struct module *owner = xfer->owner; 976*62ab466cSMartijn Coenen 977*62ab466cSMartijn Coenen if (!try_module_get(owner)) 978*62ab466cSMartijn Coenen return -EINVAL; 979*62ab466cSMartijn Coenen if (xfer->init) 980*62ab466cSMartijn Coenen err = xfer->init(lo, i); 981*62ab466cSMartijn Coenen if (err) 982*62ab466cSMartijn Coenen module_put(owner); 983*62ab466cSMartijn Coenen else 984*62ab466cSMartijn Coenen lo->lo_encryption = xfer; 985*62ab466cSMartijn Coenen } 986*62ab466cSMartijn Coenen return err; 987*62ab466cSMartijn Coenen } 988*62ab466cSMartijn Coenen 989*62ab466cSMartijn Coenen /** 990*62ab466cSMartijn Coenen * loop_set_status_from_info - configure device from loop_info 991*62ab466cSMartijn Coenen * @lo: struct loop_device to configure 992*62ab466cSMartijn Coenen * @info: struct loop_info64 to configure the device with 993*62ab466cSMartijn Coenen * 994*62ab466cSMartijn Coenen * Configures the loop device parameters according to the passed 995*62ab466cSMartijn Coenen * in loop_info64 configuration. 996*62ab466cSMartijn Coenen */ 997*62ab466cSMartijn Coenen static int 998*62ab466cSMartijn Coenen loop_set_status_from_info(struct loop_device *lo, 999*62ab466cSMartijn Coenen const struct loop_info64 *info) 1000*62ab466cSMartijn Coenen { 1001*62ab466cSMartijn Coenen int err; 1002*62ab466cSMartijn Coenen struct loop_func_table *xfer; 1003*62ab466cSMartijn Coenen kuid_t uid = current_uid(); 1004*62ab466cSMartijn Coenen 1005*62ab466cSMartijn Coenen if ((unsigned int) info->lo_encrypt_key_size > LO_KEY_SIZE) 1006*62ab466cSMartijn Coenen return -EINVAL; 1007*62ab466cSMartijn Coenen 1008*62ab466cSMartijn Coenen err = loop_release_xfer(lo); 1009*62ab466cSMartijn Coenen if (err) 1010*62ab466cSMartijn Coenen return err; 1011*62ab466cSMartijn Coenen 1012*62ab466cSMartijn Coenen if (info->lo_encrypt_type) { 1013*62ab466cSMartijn Coenen unsigned int type = info->lo_encrypt_type; 1014*62ab466cSMartijn Coenen 1015*62ab466cSMartijn Coenen if (type >= MAX_LO_CRYPT) 1016*62ab466cSMartijn Coenen return -EINVAL; 1017*62ab466cSMartijn Coenen xfer = xfer_funcs[type]; 1018*62ab466cSMartijn Coenen if (xfer == NULL) 1019*62ab466cSMartijn Coenen return -EINVAL; 1020*62ab466cSMartijn Coenen } else 1021*62ab466cSMartijn Coenen xfer = NULL; 1022*62ab466cSMartijn Coenen 1023*62ab466cSMartijn Coenen err = loop_init_xfer(lo, xfer, info); 1024*62ab466cSMartijn Coenen if (err) 1025*62ab466cSMartijn Coenen return err; 1026*62ab466cSMartijn Coenen 1027*62ab466cSMartijn Coenen lo->lo_offset = info->lo_offset; 1028*62ab466cSMartijn Coenen lo->lo_sizelimit = info->lo_sizelimit; 1029*62ab466cSMartijn Coenen memcpy(lo->lo_file_name, info->lo_file_name, LO_NAME_SIZE); 1030*62ab466cSMartijn Coenen memcpy(lo->lo_crypt_name, info->lo_crypt_name, LO_NAME_SIZE); 1031*62ab466cSMartijn Coenen lo->lo_file_name[LO_NAME_SIZE-1] = 0; 1032*62ab466cSMartijn Coenen lo->lo_crypt_name[LO_NAME_SIZE-1] = 0; 1033*62ab466cSMartijn Coenen 1034*62ab466cSMartijn Coenen if (!xfer) 1035*62ab466cSMartijn Coenen xfer = &none_funcs; 1036*62ab466cSMartijn Coenen lo->transfer = xfer->transfer; 1037*62ab466cSMartijn Coenen lo->ioctl = xfer->ioctl; 1038*62ab466cSMartijn Coenen 1039*62ab466cSMartijn Coenen if ((lo->lo_flags & LO_FLAGS_AUTOCLEAR) != 1040*62ab466cSMartijn Coenen (info->lo_flags & LO_FLAGS_AUTOCLEAR)) 1041*62ab466cSMartijn Coenen lo->lo_flags ^= LO_FLAGS_AUTOCLEAR; 1042*62ab466cSMartijn Coenen 1043*62ab466cSMartijn Coenen lo->lo_encrypt_key_size = info->lo_encrypt_key_size; 1044*62ab466cSMartijn Coenen lo->lo_init[0] = info->lo_init[0]; 1045*62ab466cSMartijn Coenen lo->lo_init[1] = info->lo_init[1]; 1046*62ab466cSMartijn Coenen if (info->lo_encrypt_key_size) { 1047*62ab466cSMartijn Coenen memcpy(lo->lo_encrypt_key, info->lo_encrypt_key, 1048*62ab466cSMartijn Coenen info->lo_encrypt_key_size); 1049*62ab466cSMartijn Coenen lo->lo_key_owner = uid; 1050*62ab466cSMartijn Coenen } 1051*62ab466cSMartijn Coenen 1052*62ab466cSMartijn Coenen return 0; 1053*62ab466cSMartijn Coenen } 1054*62ab466cSMartijn Coenen 1055bb214884SAl Viro static int loop_set_fd(struct loop_device *lo, fmode_t mode, 10561da177e4SLinus Torvalds struct block_device *bdev, unsigned int arg) 10571da177e4SLinus Torvalds { 1058d2ac838eSTheodore Ts'o struct file *file; 10591da177e4SLinus Torvalds struct inode *inode; 10601da177e4SLinus Torvalds struct address_space *mapping; 106189e524c0SJan Kara struct block_device *claimed_bdev = NULL; 10621da177e4SLinus Torvalds int lo_flags = 0; 10631da177e4SLinus Torvalds int error; 10641da177e4SLinus Torvalds loff_t size; 106585b0a54aSJan Kara bool partscan; 10661da177e4SLinus Torvalds 10671da177e4SLinus Torvalds /* This is safe, since we have a reference from open(). */ 10681da177e4SLinus Torvalds __module_get(THIS_MODULE); 10691da177e4SLinus Torvalds 10701da177e4SLinus Torvalds error = -EBADF; 10711da177e4SLinus Torvalds file = fget(arg); 10721da177e4SLinus Torvalds if (!file) 10731da177e4SLinus Torvalds goto out; 10741da177e4SLinus Torvalds 107533ec3e53SJan Kara /* 107633ec3e53SJan Kara * If we don't hold exclusive handle for the device, upgrade to it 107733ec3e53SJan Kara * here to avoid changing device under exclusive owner. 107833ec3e53SJan Kara */ 107933ec3e53SJan Kara if (!(mode & FMODE_EXCL)) { 108089e524c0SJan Kara claimed_bdev = bd_start_claiming(bdev, loop_set_fd); 108189e524c0SJan Kara if (IS_ERR(claimed_bdev)) { 108289e524c0SJan Kara error = PTR_ERR(claimed_bdev); 1083757ecf40SJan Kara goto out_putf; 108433ec3e53SJan Kara } 108589e524c0SJan Kara } 108633ec3e53SJan Kara 108733ec3e53SJan Kara error = mutex_lock_killable(&loop_ctl_mutex); 108833ec3e53SJan Kara if (error) 108933ec3e53SJan Kara goto out_bdev; 1090757ecf40SJan Kara 10911da177e4SLinus Torvalds error = -EBUSY; 10921da177e4SLinus Torvalds if (lo->lo_state != Lo_unbound) 1093757ecf40SJan Kara goto out_unlock; 10941da177e4SLinus Torvalds 1095d2ac838eSTheodore Ts'o error = loop_validate_file(file, bdev); 1096d2ac838eSTheodore Ts'o if (error) 1097757ecf40SJan Kara goto out_unlock; 10981da177e4SLinus Torvalds 10991da177e4SLinus Torvalds mapping = file->f_mapping; 11001da177e4SLinus Torvalds inode = mapping->host; 11011da177e4SLinus Torvalds 1102456be148SChristoph Hellwig if (!(file->f_mode & FMODE_WRITE) || !(mode & FMODE_WRITE) || 1103283e7e5dSAl Viro !file->f_op->write_iter) 11041da177e4SLinus Torvalds lo_flags |= LO_FLAGS_READ_ONLY; 11051da177e4SLinus Torvalds 1106456be148SChristoph Hellwig size = get_loop_size(lo, file); 1107083a6a50SMartijn Coenen 1108e03a3d7aSMing Lei error = loop_prepare_queue(lo); 1109e03a3d7aSMing Lei if (error) 1110757ecf40SJan Kara goto out_unlock; 11111da177e4SLinus Torvalds 1112456be148SChristoph Hellwig error = 0; 11131da177e4SLinus Torvalds 11141da177e4SLinus Torvalds set_device_ro(bdev, (lo_flags & LO_FLAGS_READ_ONLY) != 0); 11151da177e4SLinus Torvalds 11162e5ab5f3SMing Lei lo->use_dio = false; 11171da177e4SLinus Torvalds lo->lo_device = bdev; 11181da177e4SLinus Torvalds lo->lo_flags = lo_flags; 11191da177e4SLinus Torvalds lo->lo_backing_file = file; 1120aa4d8616SChristoph Hellwig lo->transfer = NULL; 11211da177e4SLinus Torvalds lo->ioctl = NULL; 11221da177e4SLinus Torvalds lo->lo_sizelimit = 0; 11231da177e4SLinus Torvalds lo->old_gfp_mask = mapping_gfp_mask(mapping); 11241da177e4SLinus Torvalds mapping_set_gfp_mask(mapping, lo->old_gfp_mask & ~(__GFP_IO|__GFP_FS)); 11251da177e4SLinus Torvalds 112668db1961SNikanth Karthikesan if (!(lo_flags & LO_FLAGS_READ_ONLY) && file->f_op->fsync) 112721d0727fSJens Axboe blk_queue_write_cache(lo->lo_queue, true, false); 112868db1961SNikanth Karthikesan 112985560117SMartijn Coenen if (io_is_direct(lo->lo_backing_file) && inode->i_sb->s_bdev) { 113085560117SMartijn Coenen /* In case of direct I/O, match underlying block size */ 113185560117SMartijn Coenen unsigned short bsize = bdev_logical_block_size( 113285560117SMartijn Coenen inode->i_sb->s_bdev); 113385560117SMartijn Coenen 113485560117SMartijn Coenen blk_queue_logical_block_size(lo->lo_queue, bsize); 113585560117SMartijn Coenen blk_queue_physical_block_size(lo->lo_queue, bsize); 113685560117SMartijn Coenen blk_queue_io_min(lo->lo_queue, bsize); 113785560117SMartijn Coenen } 113885560117SMartijn Coenen 113956a85fd8SHolger Hoffstätte loop_update_rotational(lo); 11402e5ab5f3SMing Lei loop_update_dio(lo); 1141ee862730SMilan Broz loop_sysfs_init(lo); 11425795b6f5SMartijn Coenen loop_set_size(lo, size); 11431da177e4SLinus Torvalds 11448a0740c4SOmar Sandoval set_blocksize(bdev, S_ISBLK(inode->i_mode) ? 11458a0740c4SOmar Sandoval block_size(inode->i_bdev) : PAGE_SIZE); 11461da177e4SLinus Torvalds 11476c997918SSerge E. Hallyn lo->lo_state = Lo_bound; 1148e03c8dd1SKay Sievers if (part_shift) 1149e03c8dd1SKay Sievers lo->lo_flags |= LO_FLAGS_PARTSCAN; 115085b0a54aSJan Kara partscan = lo->lo_flags & LO_FLAGS_PARTSCAN; 1151c1681bf8SAnatol Pomozov 1152c1681bf8SAnatol Pomozov /* Grab the block_device to prevent its destruction after we 1153a2505b79SJan Kara * put /dev/loopXX inode. Later in __loop_clr_fd() we bdput(bdev). 1154c1681bf8SAnatol Pomozov */ 1155c1681bf8SAnatol Pomozov bdgrab(bdev); 1156757ecf40SJan Kara mutex_unlock(&loop_ctl_mutex); 115785b0a54aSJan Kara if (partscan) 115885b0a54aSJan Kara loop_reread_partitions(lo, bdev); 115989e524c0SJan Kara if (claimed_bdev) 116089e524c0SJan Kara bd_abort_claiming(bdev, claimed_bdev, loop_set_fd); 11611da177e4SLinus Torvalds return 0; 11621da177e4SLinus Torvalds 1163757ecf40SJan Kara out_unlock: 1164757ecf40SJan Kara mutex_unlock(&loop_ctl_mutex); 116533ec3e53SJan Kara out_bdev: 116689e524c0SJan Kara if (claimed_bdev) 116789e524c0SJan Kara bd_abort_claiming(bdev, claimed_bdev, loop_set_fd); 11681da177e4SLinus Torvalds out_putf: 11691da177e4SLinus Torvalds fput(file); 11701da177e4SLinus Torvalds out: 11711da177e4SLinus Torvalds /* This is safe: open() is still holding a reference. */ 11721da177e4SLinus Torvalds module_put(THIS_MODULE); 11731da177e4SLinus Torvalds return error; 11741da177e4SLinus Torvalds } 11751da177e4SLinus Torvalds 11760da03cabSJan Kara static int __loop_clr_fd(struct loop_device *lo, bool release) 11771da177e4SLinus Torvalds { 11787ccd0791SJan Kara struct file *filp = NULL; 1179b4e3ca1aSAl Viro gfp_t gfp = lo->old_gfp_mask; 11804c823cc3SAyan George struct block_device *bdev = lo->lo_device; 11817ccd0791SJan Kara int err = 0; 11820da03cabSJan Kara bool partscan = false; 11830da03cabSJan Kara int lo_number; 11841da177e4SLinus Torvalds 11857ccd0791SJan Kara mutex_lock(&loop_ctl_mutex); 11867ccd0791SJan Kara if (WARN_ON_ONCE(lo->lo_state != Lo_rundown)) { 11877ccd0791SJan Kara err = -ENXIO; 11887ccd0791SJan Kara goto out_unlock; 11897ccd0791SJan Kara } 11901da177e4SLinus Torvalds 11917ccd0791SJan Kara filp = lo->lo_backing_file; 11927ccd0791SJan Kara if (filp == NULL) { 11937ccd0791SJan Kara err = -EINVAL; 11947ccd0791SJan Kara goto out_unlock; 11957ccd0791SJan Kara } 11961da177e4SLinus Torvalds 1197f8933667SMing Lei /* freeze request queue during the transition */ 1198f8933667SMing Lei blk_mq_freeze_queue(lo->lo_queue); 1199f8933667SMing Lei 12001da177e4SLinus Torvalds spin_lock_irq(&lo->lo_lock); 12011da177e4SLinus Torvalds lo->lo_backing_file = NULL; 120205eb0f25SKay Sievers spin_unlock_irq(&lo->lo_lock); 12031da177e4SLinus Torvalds 12041da177e4SLinus Torvalds loop_release_xfer(lo); 12051da177e4SLinus Torvalds lo->transfer = NULL; 12061da177e4SLinus Torvalds lo->ioctl = NULL; 12071da177e4SLinus Torvalds lo->lo_device = NULL; 12081da177e4SLinus Torvalds lo->lo_encryption = NULL; 12091da177e4SLinus Torvalds lo->lo_offset = 0; 12101da177e4SLinus Torvalds lo->lo_sizelimit = 0; 12111da177e4SLinus Torvalds lo->lo_encrypt_key_size = 0; 12121da177e4SLinus Torvalds memset(lo->lo_encrypt_key, 0, LO_KEY_SIZE); 12131da177e4SLinus Torvalds memset(lo->lo_crypt_name, 0, LO_NAME_SIZE); 12141da177e4SLinus Torvalds memset(lo->lo_file_name, 0, LO_NAME_SIZE); 121589e4fdecSOmar Sandoval blk_queue_logical_block_size(lo->lo_queue, 512); 1216bf093753SOmar Sandoval blk_queue_physical_block_size(lo->lo_queue, 512); 1217bf093753SOmar Sandoval blk_queue_io_min(lo->lo_queue, 512); 1218c1681bf8SAnatol Pomozov if (bdev) { 1219c1681bf8SAnatol Pomozov bdput(bdev); 1220f98393a6SPeter Zijlstra invalidate_bdev(bdev); 1221eedffa28SJeff Layton bdev->bd_inode->i_mapping->wb_err = 0; 1222c1681bf8SAnatol Pomozov } 122373285082SKen Chen set_capacity(lo->lo_disk, 0); 122451a0bb0cSMilan Broz loop_sysfs_exit(lo); 1225c3473c63SDavid Zeuthen if (bdev) { 12261da177e4SLinus Torvalds bd_set_size(bdev, 0); 1227c3473c63SDavid Zeuthen /* let user-space know about this change */ 1228c3473c63SDavid Zeuthen kobject_uevent(&disk_to_dev(bdev->bd_disk)->kobj, KOBJ_CHANGE); 1229c3473c63SDavid Zeuthen } 12301da177e4SLinus Torvalds mapping_set_gfp_mask(filp->f_mapping, gfp); 12311da177e4SLinus Torvalds /* This is safe: open() is still holding a reference. */ 12321da177e4SLinus Torvalds module_put(THIS_MODULE); 1233f8933667SMing Lei blk_mq_unfreeze_queue(lo->lo_queue); 1234f8933667SMing Lei 12350da03cabSJan Kara partscan = lo->lo_flags & LO_FLAGS_PARTSCAN && bdev; 12360da03cabSJan Kara lo_number = lo->lo_number; 12370da03cabSJan Kara loop_unprepare_queue(lo); 12380da03cabSJan Kara out_unlock: 12390da03cabSJan Kara mutex_unlock(&loop_ctl_mutex); 12400da03cabSJan Kara if (partscan) { 1241d57f3374SJan Kara /* 1242d57f3374SJan Kara * bd_mutex has been held already in release path, so don't 1243d57f3374SJan Kara * acquire it if this function is called in such case. 1244d57f3374SJan Kara * 1245d57f3374SJan Kara * If the reread partition isn't from release path, lo_refcnt 1246d57f3374SJan Kara * must be at least one and it can only become zero when the 1247d57f3374SJan Kara * current holder is released. 1248d57f3374SJan Kara */ 1249f0b870dfSChristoph Hellwig if (!release) 1250f0b870dfSChristoph Hellwig mutex_lock(&bdev->bd_mutex); 1251f0b870dfSChristoph Hellwig err = bdev_disk_changed(bdev, false); 1252f0b870dfSChristoph Hellwig if (!release) 1253f0b870dfSChristoph Hellwig mutex_unlock(&bdev->bd_mutex); 125440853d6fSDongli Zhang if (err) 1255d57f3374SJan Kara pr_warn("%s: partition scan of loop%d failed (rc=%d)\n", 12560da03cabSJan Kara __func__, lo_number, err); 1257d57f3374SJan Kara /* Device is gone, no point in returning error */ 1258d57f3374SJan Kara err = 0; 1259d57f3374SJan Kara } 1260758a58d0SDongli Zhang 1261758a58d0SDongli Zhang /* 1262758a58d0SDongli Zhang * lo->lo_state is set to Lo_unbound here after above partscan has 1263758a58d0SDongli Zhang * finished. 1264758a58d0SDongli Zhang * 1265758a58d0SDongli Zhang * There cannot be anybody else entering __loop_clr_fd() as 1266758a58d0SDongli Zhang * lo->lo_backing_file is already cleared and Lo_rundown state 1267758a58d0SDongli Zhang * protects us from all the other places trying to change the 'lo' 1268758a58d0SDongli Zhang * device. 1269758a58d0SDongli Zhang */ 1270758a58d0SDongli Zhang mutex_lock(&loop_ctl_mutex); 1271758a58d0SDongli Zhang lo->lo_flags = 0; 1272758a58d0SDongli Zhang if (!part_shift) 1273758a58d0SDongli Zhang lo->lo_disk->flags |= GENHD_FL_NO_PART_SCAN; 1274758a58d0SDongli Zhang lo->lo_state = Lo_unbound; 1275758a58d0SDongli Zhang mutex_unlock(&loop_ctl_mutex); 1276758a58d0SDongli Zhang 1277f028f3b2SNikanth Karthikesan /* 1278310ca162STetsuo Handa * Need not hold loop_ctl_mutex to fput backing file. 1279310ca162STetsuo Handa * Calling fput holding loop_ctl_mutex triggers a circular 1280f028f3b2SNikanth Karthikesan * lock dependency possibility warning as fput can take 1281310ca162STetsuo Handa * bd_mutex which is usually taken before loop_ctl_mutex. 1282f028f3b2SNikanth Karthikesan */ 12837ccd0791SJan Kara if (filp) 1284f028f3b2SNikanth Karthikesan fput(filp); 12857ccd0791SJan Kara return err; 12861da177e4SLinus Torvalds } 12871da177e4SLinus Torvalds 1288a2505b79SJan Kara static int loop_clr_fd(struct loop_device *lo) 1289a2505b79SJan Kara { 12907ccd0791SJan Kara int err; 12917ccd0791SJan Kara 1292c28445faSJan Kara err = mutex_lock_killable(&loop_ctl_mutex); 12937ccd0791SJan Kara if (err) 12947ccd0791SJan Kara return err; 12957ccd0791SJan Kara if (lo->lo_state != Lo_bound) { 12967ccd0791SJan Kara mutex_unlock(&loop_ctl_mutex); 1297a2505b79SJan Kara return -ENXIO; 12987ccd0791SJan Kara } 1299a2505b79SJan Kara /* 1300a2505b79SJan Kara * If we've explicitly asked to tear down the loop device, 1301a2505b79SJan Kara * and it has an elevated reference count, set it for auto-teardown when 1302a2505b79SJan Kara * the last reference goes away. This stops $!~#$@ udev from 1303a2505b79SJan Kara * preventing teardown because it decided that it needs to run blkid on 1304a2505b79SJan Kara * the loopback device whenever they appear. xfstests is notorious for 1305a2505b79SJan Kara * failing tests because blkid via udev races with a losetup 1306a2505b79SJan Kara * <dev>/do something like mkfs/losetup -d <dev> causing the losetup -d 1307a2505b79SJan Kara * command to fail with EBUSY. 1308a2505b79SJan Kara */ 1309a2505b79SJan Kara if (atomic_read(&lo->lo_refcnt) > 1) { 1310a2505b79SJan Kara lo->lo_flags |= LO_FLAGS_AUTOCLEAR; 1311a2505b79SJan Kara mutex_unlock(&loop_ctl_mutex); 1312a2505b79SJan Kara return 0; 1313a2505b79SJan Kara } 1314a2505b79SJan Kara lo->lo_state = Lo_rundown; 13157ccd0791SJan Kara mutex_unlock(&loop_ctl_mutex); 1316a2505b79SJan Kara 13170da03cabSJan Kara return __loop_clr_fd(lo, false); 1318a2505b79SJan Kara } 1319a2505b79SJan Kara 13200c3796c2SMartijn Coenen static int 13210c3796c2SMartijn Coenen loop_set_status(struct loop_device *lo, const struct loop_info64 *info) 13220c3796c2SMartijn Coenen { 13230c3796c2SMartijn Coenen int err; 13240c3796c2SMartijn Coenen struct block_device *bdev; 13250c3796c2SMartijn Coenen kuid_t uid = current_uid(); 13260c3796c2SMartijn Coenen bool partscan = false; 13270c3796c2SMartijn Coenen bool size_changed = false; 13280c3796c2SMartijn Coenen 13290c3796c2SMartijn Coenen err = mutex_lock_killable(&loop_ctl_mutex); 13300c3796c2SMartijn Coenen if (err) 13310c3796c2SMartijn Coenen return err; 13320c3796c2SMartijn Coenen if (lo->lo_encrypt_key_size && 13330c3796c2SMartijn Coenen !uid_eq(lo->lo_key_owner, uid) && 13340c3796c2SMartijn Coenen !capable(CAP_SYS_ADMIN)) { 13350c3796c2SMartijn Coenen err = -EPERM; 13360c3796c2SMartijn Coenen goto out_unlock; 13370c3796c2SMartijn Coenen } 13380c3796c2SMartijn Coenen if (lo->lo_state != Lo_bound) { 13390c3796c2SMartijn Coenen err = -ENXIO; 13400c3796c2SMartijn Coenen goto out_unlock; 13410c3796c2SMartijn Coenen } 13420c3796c2SMartijn Coenen 13430c3796c2SMartijn Coenen if (lo->lo_offset != info->lo_offset || 13440c3796c2SMartijn Coenen lo->lo_sizelimit != info->lo_sizelimit) { 13450c3796c2SMartijn Coenen size_changed = true; 13460c3796c2SMartijn Coenen sync_blockdev(lo->lo_device); 13470c3796c2SMartijn Coenen kill_bdev(lo->lo_device); 13480c3796c2SMartijn Coenen } 13490c3796c2SMartijn Coenen 13500c3796c2SMartijn Coenen /* I/O need to be drained during transfer transition */ 13510c3796c2SMartijn Coenen blk_mq_freeze_queue(lo->lo_queue); 13520c3796c2SMartijn Coenen 13530c3796c2SMartijn Coenen if (size_changed && lo->lo_device->bd_inode->i_mapping->nrpages) { 13540c3796c2SMartijn Coenen /* If any pages were dirtied after kill_bdev(), try again */ 13550c3796c2SMartijn Coenen err = -EAGAIN; 13560c3796c2SMartijn Coenen pr_warn("%s: loop%d (%s) has still dirty pages (nrpages=%lu)\n", 13570c3796c2SMartijn Coenen __func__, lo->lo_number, lo->lo_file_name, 13580c3796c2SMartijn Coenen lo->lo_device->bd_inode->i_mapping->nrpages); 13590c3796c2SMartijn Coenen goto out_unfreeze; 13600c3796c2SMartijn Coenen } 13610c3796c2SMartijn Coenen 13620c3796c2SMartijn Coenen err = loop_set_status_from_info(lo, info); 13630c3796c2SMartijn Coenen if (err) 13640c3796c2SMartijn Coenen goto out_unfreeze; 13650c3796c2SMartijn Coenen 1366b0bd158dSMartijn Coenen if (size_changed) { 1367b0bd158dSMartijn Coenen loff_t new_size = get_size(lo->lo_offset, lo->lo_sizelimit, 1368b0bd158dSMartijn Coenen lo->lo_backing_file); 1369b0bd158dSMartijn Coenen loop_set_size(lo, new_size); 1370b0bd158dSMartijn Coenen } 1371b0bd158dSMartijn Coenen 13727c5014b0SMartijn Coenen loop_config_discard(lo); 13737c5014b0SMartijn Coenen 13742e5ab5f3SMing Lei /* update dio if lo_offset or transfer is changed */ 13752e5ab5f3SMing Lei __loop_update_dio(lo, lo->use_dio); 13762e5ab5f3SMing Lei 1377550df5fdSJan Kara out_unfreeze: 1378ecdd0959SMing Lei blk_mq_unfreeze_queue(lo->lo_queue); 1379e02898b4SOmar Sandoval 1380e02898b4SOmar Sandoval if (!err && (info->lo_flags & LO_FLAGS_PARTSCAN) && 1381e02898b4SOmar Sandoval !(lo->lo_flags & LO_FLAGS_PARTSCAN)) { 1382e02898b4SOmar Sandoval lo->lo_flags |= LO_FLAGS_PARTSCAN; 1383e02898b4SOmar Sandoval lo->lo_disk->flags &= ~GENHD_FL_NO_PART_SCAN; 138485b0a54aSJan Kara bdev = lo->lo_device; 138585b0a54aSJan Kara partscan = true; 1386e02898b4SOmar Sandoval } 1387550df5fdSJan Kara out_unlock: 1388550df5fdSJan Kara mutex_unlock(&loop_ctl_mutex); 138985b0a54aSJan Kara if (partscan) 139085b0a54aSJan Kara loop_reread_partitions(lo, bdev); 1391e02898b4SOmar Sandoval 1392ecdd0959SMing Lei return err; 13931da177e4SLinus Torvalds } 13941da177e4SLinus Torvalds 13951da177e4SLinus Torvalds static int 13961da177e4SLinus Torvalds loop_get_status(struct loop_device *lo, struct loop_info64 *info) 13971da177e4SLinus Torvalds { 1398b1ab5fa3STetsuo Handa struct path path; 13991da177e4SLinus Torvalds struct kstat stat; 14002d1d4c1eSOmar Sandoval int ret; 14011da177e4SLinus Torvalds 1402c28445faSJan Kara ret = mutex_lock_killable(&loop_ctl_mutex); 14034a5ce9baSJan Kara if (ret) 14044a5ce9baSJan Kara return ret; 14052d1d4c1eSOmar Sandoval if (lo->lo_state != Lo_bound) { 1406310ca162STetsuo Handa mutex_unlock(&loop_ctl_mutex); 14071da177e4SLinus Torvalds return -ENXIO; 14082d1d4c1eSOmar Sandoval } 14092d1d4c1eSOmar Sandoval 14101da177e4SLinus Torvalds memset(info, 0, sizeof(*info)); 14111da177e4SLinus Torvalds info->lo_number = lo->lo_number; 14121da177e4SLinus Torvalds info->lo_offset = lo->lo_offset; 14131da177e4SLinus Torvalds info->lo_sizelimit = lo->lo_sizelimit; 14141da177e4SLinus Torvalds info->lo_flags = lo->lo_flags; 14151da177e4SLinus Torvalds memcpy(info->lo_file_name, lo->lo_file_name, LO_NAME_SIZE); 14161da177e4SLinus Torvalds memcpy(info->lo_crypt_name, lo->lo_crypt_name, LO_NAME_SIZE); 14171da177e4SLinus Torvalds info->lo_encrypt_type = 14181da177e4SLinus Torvalds lo->lo_encryption ? lo->lo_encryption->number : 0; 14191da177e4SLinus Torvalds if (lo->lo_encrypt_key_size && capable(CAP_SYS_ADMIN)) { 14201da177e4SLinus Torvalds info->lo_encrypt_key_size = lo->lo_encrypt_key_size; 14211da177e4SLinus Torvalds memcpy(info->lo_encrypt_key, lo->lo_encrypt_key, 14221da177e4SLinus Torvalds lo->lo_encrypt_key_size); 14231da177e4SLinus Torvalds } 14242d1d4c1eSOmar Sandoval 1425310ca162STetsuo Handa /* Drop loop_ctl_mutex while we call into the filesystem. */ 1426b1ab5fa3STetsuo Handa path = lo->lo_backing_file->f_path; 1427b1ab5fa3STetsuo Handa path_get(&path); 1428310ca162STetsuo Handa mutex_unlock(&loop_ctl_mutex); 1429b1ab5fa3STetsuo Handa ret = vfs_getattr(&path, &stat, STATX_INO, AT_STATX_SYNC_AS_STAT); 14302d1d4c1eSOmar Sandoval if (!ret) { 14312d1d4c1eSOmar Sandoval info->lo_device = huge_encode_dev(stat.dev); 14322d1d4c1eSOmar Sandoval info->lo_inode = stat.ino; 14332d1d4c1eSOmar Sandoval info->lo_rdevice = huge_encode_dev(stat.rdev); 14342d1d4c1eSOmar Sandoval } 1435b1ab5fa3STetsuo Handa path_put(&path); 14362d1d4c1eSOmar Sandoval return ret; 14371da177e4SLinus Torvalds } 14381da177e4SLinus Torvalds 14391da177e4SLinus Torvalds static void 14401da177e4SLinus Torvalds loop_info64_from_old(const struct loop_info *info, struct loop_info64 *info64) 14411da177e4SLinus Torvalds { 14421da177e4SLinus Torvalds memset(info64, 0, sizeof(*info64)); 14431da177e4SLinus Torvalds info64->lo_number = info->lo_number; 14441da177e4SLinus Torvalds info64->lo_device = info->lo_device; 14451da177e4SLinus Torvalds info64->lo_inode = info->lo_inode; 14461da177e4SLinus Torvalds info64->lo_rdevice = info->lo_rdevice; 14471da177e4SLinus Torvalds info64->lo_offset = info->lo_offset; 14481da177e4SLinus Torvalds info64->lo_sizelimit = 0; 14491da177e4SLinus Torvalds info64->lo_encrypt_type = info->lo_encrypt_type; 14501da177e4SLinus Torvalds info64->lo_encrypt_key_size = info->lo_encrypt_key_size; 14511da177e4SLinus Torvalds info64->lo_flags = info->lo_flags; 14521da177e4SLinus Torvalds info64->lo_init[0] = info->lo_init[0]; 14531da177e4SLinus Torvalds info64->lo_init[1] = info->lo_init[1]; 14541da177e4SLinus Torvalds if (info->lo_encrypt_type == LO_CRYPT_CRYPTOAPI) 14551da177e4SLinus Torvalds memcpy(info64->lo_crypt_name, info->lo_name, LO_NAME_SIZE); 14561da177e4SLinus Torvalds else 14571da177e4SLinus Torvalds memcpy(info64->lo_file_name, info->lo_name, LO_NAME_SIZE); 14581da177e4SLinus Torvalds memcpy(info64->lo_encrypt_key, info->lo_encrypt_key, LO_KEY_SIZE); 14591da177e4SLinus Torvalds } 14601da177e4SLinus Torvalds 14611da177e4SLinus Torvalds static int 14621da177e4SLinus Torvalds loop_info64_to_old(const struct loop_info64 *info64, struct loop_info *info) 14631da177e4SLinus Torvalds { 14641da177e4SLinus Torvalds memset(info, 0, sizeof(*info)); 14651da177e4SLinus Torvalds info->lo_number = info64->lo_number; 14661da177e4SLinus Torvalds info->lo_device = info64->lo_device; 14671da177e4SLinus Torvalds info->lo_inode = info64->lo_inode; 14681da177e4SLinus Torvalds info->lo_rdevice = info64->lo_rdevice; 14691da177e4SLinus Torvalds info->lo_offset = info64->lo_offset; 14701da177e4SLinus Torvalds info->lo_encrypt_type = info64->lo_encrypt_type; 14711da177e4SLinus Torvalds info->lo_encrypt_key_size = info64->lo_encrypt_key_size; 14721da177e4SLinus Torvalds info->lo_flags = info64->lo_flags; 14731da177e4SLinus Torvalds info->lo_init[0] = info64->lo_init[0]; 14741da177e4SLinus Torvalds info->lo_init[1] = info64->lo_init[1]; 14751da177e4SLinus Torvalds if (info->lo_encrypt_type == LO_CRYPT_CRYPTOAPI) 14761da177e4SLinus Torvalds memcpy(info->lo_name, info64->lo_crypt_name, LO_NAME_SIZE); 14771da177e4SLinus Torvalds else 14781da177e4SLinus Torvalds memcpy(info->lo_name, info64->lo_file_name, LO_NAME_SIZE); 14791da177e4SLinus Torvalds memcpy(info->lo_encrypt_key, info64->lo_encrypt_key, LO_KEY_SIZE); 14801da177e4SLinus Torvalds 14811da177e4SLinus Torvalds /* error in case values were truncated */ 14821da177e4SLinus Torvalds if (info->lo_device != info64->lo_device || 14831da177e4SLinus Torvalds info->lo_rdevice != info64->lo_rdevice || 14841da177e4SLinus Torvalds info->lo_inode != info64->lo_inode || 14851da177e4SLinus Torvalds info->lo_offset != info64->lo_offset) 14861da177e4SLinus Torvalds return -EOVERFLOW; 14871da177e4SLinus Torvalds 14881da177e4SLinus Torvalds return 0; 14891da177e4SLinus Torvalds } 14901da177e4SLinus Torvalds 14911da177e4SLinus Torvalds static int 14921da177e4SLinus Torvalds loop_set_status_old(struct loop_device *lo, const struct loop_info __user *arg) 14931da177e4SLinus Torvalds { 14941da177e4SLinus Torvalds struct loop_info info; 14951da177e4SLinus Torvalds struct loop_info64 info64; 14961da177e4SLinus Torvalds 14971da177e4SLinus Torvalds if (copy_from_user(&info, arg, sizeof (struct loop_info))) 14981da177e4SLinus Torvalds return -EFAULT; 14991da177e4SLinus Torvalds loop_info64_from_old(&info, &info64); 15001da177e4SLinus Torvalds return loop_set_status(lo, &info64); 15011da177e4SLinus Torvalds } 15021da177e4SLinus Torvalds 15031da177e4SLinus Torvalds static int 15041da177e4SLinus Torvalds loop_set_status64(struct loop_device *lo, const struct loop_info64 __user *arg) 15051da177e4SLinus Torvalds { 15061da177e4SLinus Torvalds struct loop_info64 info64; 15071da177e4SLinus Torvalds 15081da177e4SLinus Torvalds if (copy_from_user(&info64, arg, sizeof (struct loop_info64))) 15091da177e4SLinus Torvalds return -EFAULT; 15101da177e4SLinus Torvalds return loop_set_status(lo, &info64); 15111da177e4SLinus Torvalds } 15121da177e4SLinus Torvalds 15131da177e4SLinus Torvalds static int 15141da177e4SLinus Torvalds loop_get_status_old(struct loop_device *lo, struct loop_info __user *arg) { 15151da177e4SLinus Torvalds struct loop_info info; 15161da177e4SLinus Torvalds struct loop_info64 info64; 1517bdac616dSOmar Sandoval int err; 15181da177e4SLinus Torvalds 15194a5ce9baSJan Kara if (!arg) 1520bdac616dSOmar Sandoval return -EINVAL; 15211da177e4SLinus Torvalds err = loop_get_status(lo, &info64); 15221da177e4SLinus Torvalds if (!err) 15231da177e4SLinus Torvalds err = loop_info64_to_old(&info64, &info); 15241da177e4SLinus Torvalds if (!err && copy_to_user(arg, &info, sizeof(info))) 15251da177e4SLinus Torvalds err = -EFAULT; 15261da177e4SLinus Torvalds 15271da177e4SLinus Torvalds return err; 15281da177e4SLinus Torvalds } 15291da177e4SLinus Torvalds 15301da177e4SLinus Torvalds static int 15311da177e4SLinus Torvalds loop_get_status64(struct loop_device *lo, struct loop_info64 __user *arg) { 15321da177e4SLinus Torvalds struct loop_info64 info64; 1533bdac616dSOmar Sandoval int err; 15341da177e4SLinus Torvalds 15354a5ce9baSJan Kara if (!arg) 1536bdac616dSOmar Sandoval return -EINVAL; 15371da177e4SLinus Torvalds err = loop_get_status(lo, &info64); 15381da177e4SLinus Torvalds if (!err && copy_to_user(arg, &info64, sizeof(info64))) 15391da177e4SLinus Torvalds err = -EFAULT; 15401da177e4SLinus Torvalds 15411da177e4SLinus Torvalds return err; 15421da177e4SLinus Torvalds } 15431da177e4SLinus Torvalds 154451001b7dSHannes Reinecke static int loop_set_capacity(struct loop_device *lo) 154553d66608SJ. R. Okajima { 15460a6ed1b5SMartijn Coenen loff_t size; 15470a6ed1b5SMartijn Coenen 154853d66608SJ. R. Okajima if (unlikely(lo->lo_state != Lo_bound)) 15497b0576a3SGuo Chao return -ENXIO; 155053d66608SJ. R. Okajima 15510a6ed1b5SMartijn Coenen size = get_loop_size(lo, lo->lo_backing_file); 15520a6ed1b5SMartijn Coenen loop_set_size(lo, size); 1553083a6a50SMartijn Coenen 1554083a6a50SMartijn Coenen return 0; 155553d66608SJ. R. Okajima } 155653d66608SJ. R. Okajima 1557ab1cb278SMing Lei static int loop_set_dio(struct loop_device *lo, unsigned long arg) 1558ab1cb278SMing Lei { 1559ab1cb278SMing Lei int error = -ENXIO; 1560ab1cb278SMing Lei if (lo->lo_state != Lo_bound) 1561ab1cb278SMing Lei goto out; 1562ab1cb278SMing Lei 1563ab1cb278SMing Lei __loop_update_dio(lo, !!arg); 1564ab1cb278SMing Lei if (lo->use_dio == !!arg) 1565ab1cb278SMing Lei return 0; 1566ab1cb278SMing Lei error = -EINVAL; 1567ab1cb278SMing Lei out: 1568ab1cb278SMing Lei return error; 1569ab1cb278SMing Lei } 1570ab1cb278SMing Lei 157189e4fdecSOmar Sandoval static int loop_set_block_size(struct loop_device *lo, unsigned long arg) 157289e4fdecSOmar Sandoval { 15735db470e2SJaegeuk Kim int err = 0; 15745db470e2SJaegeuk Kim 157589e4fdecSOmar Sandoval if (lo->lo_state != Lo_bound) 157689e4fdecSOmar Sandoval return -ENXIO; 157789e4fdecSOmar Sandoval 157889e4fdecSOmar Sandoval if (arg < 512 || arg > PAGE_SIZE || !is_power_of_2(arg)) 157989e4fdecSOmar Sandoval return -EINVAL; 158089e4fdecSOmar Sandoval 15817e81f99aSMartijn Coenen if (lo->lo_queue->limits.logical_block_size == arg) 15827e81f99aSMartijn Coenen return 0; 15837e81f99aSMartijn Coenen 15845db470e2SJaegeuk Kim sync_blockdev(lo->lo_device); 15855db470e2SJaegeuk Kim kill_bdev(lo->lo_device); 15865db470e2SJaegeuk Kim 158789e4fdecSOmar Sandoval blk_mq_freeze_queue(lo->lo_queue); 158889e4fdecSOmar Sandoval 15895db470e2SJaegeuk Kim /* kill_bdev should have truncated all the pages */ 15907e81f99aSMartijn Coenen if (lo->lo_device->bd_inode->i_mapping->nrpages) { 15915db470e2SJaegeuk Kim err = -EAGAIN; 15925db470e2SJaegeuk Kim pr_warn("%s: loop%d (%s) has still dirty pages (nrpages=%lu)\n", 15935db470e2SJaegeuk Kim __func__, lo->lo_number, lo->lo_file_name, 15945db470e2SJaegeuk Kim lo->lo_device->bd_inode->i_mapping->nrpages); 15955db470e2SJaegeuk Kim goto out_unfreeze; 15965db470e2SJaegeuk Kim } 15975db470e2SJaegeuk Kim 159889e4fdecSOmar Sandoval blk_queue_logical_block_size(lo->lo_queue, arg); 1599bf093753SOmar Sandoval blk_queue_physical_block_size(lo->lo_queue, arg); 1600bf093753SOmar Sandoval blk_queue_io_min(lo->lo_queue, arg); 160189e4fdecSOmar Sandoval loop_update_dio(lo); 16025db470e2SJaegeuk Kim out_unfreeze: 160389e4fdecSOmar Sandoval blk_mq_unfreeze_queue(lo->lo_queue); 160489e4fdecSOmar Sandoval 16055db470e2SJaegeuk Kim return err; 160689e4fdecSOmar Sandoval } 160789e4fdecSOmar Sandoval 1608a1316544SJan Kara static int lo_simple_ioctl(struct loop_device *lo, unsigned int cmd, 1609a1316544SJan Kara unsigned long arg) 16101da177e4SLinus Torvalds { 16111da177e4SLinus Torvalds int err; 16121da177e4SLinus Torvalds 1613c28445faSJan Kara err = mutex_lock_killable(&loop_ctl_mutex); 16143148ffbdSOmar Sandoval if (err) 1615a1316544SJan Kara return err; 16161da177e4SLinus Torvalds switch (cmd) { 161753d66608SJ. R. Okajima case LOOP_SET_CAPACITY: 161851001b7dSHannes Reinecke err = loop_set_capacity(lo); 161953d66608SJ. R. Okajima break; 1620ab1cb278SMing Lei case LOOP_SET_DIRECT_IO: 1621ab1cb278SMing Lei err = loop_set_dio(lo, arg); 1622ab1cb278SMing Lei break; 162389e4fdecSOmar Sandoval case LOOP_SET_BLOCK_SIZE: 162489e4fdecSOmar Sandoval err = loop_set_block_size(lo, arg); 162589e4fdecSOmar Sandoval break; 16261da177e4SLinus Torvalds default: 16271da177e4SLinus Torvalds err = lo->ioctl ? lo->ioctl(lo, cmd, arg) : -EINVAL; 16281da177e4SLinus Torvalds } 1629310ca162STetsuo Handa mutex_unlock(&loop_ctl_mutex); 1630a1316544SJan Kara return err; 1631a1316544SJan Kara } 1632f028f3b2SNikanth Karthikesan 1633a1316544SJan Kara static int lo_ioctl(struct block_device *bdev, fmode_t mode, 1634a1316544SJan Kara unsigned int cmd, unsigned long arg) 1635a1316544SJan Kara { 1636a1316544SJan Kara struct loop_device *lo = bdev->bd_disk->private_data; 1637a1316544SJan Kara int err; 1638a1316544SJan Kara 1639a1316544SJan Kara switch (cmd) { 1640a1316544SJan Kara case LOOP_SET_FD: 1641757ecf40SJan Kara return loop_set_fd(lo, mode, bdev, arg); 1642a1316544SJan Kara case LOOP_CHANGE_FD: 1643c3710770SJan Kara return loop_change_fd(lo, bdev, arg); 1644a1316544SJan Kara case LOOP_CLR_FD: 16457ccd0791SJan Kara return loop_clr_fd(lo); 1646a1316544SJan Kara case LOOP_SET_STATUS: 1647a1316544SJan Kara err = -EPERM; 1648a1316544SJan Kara if ((mode & FMODE_WRITE) || capable(CAP_SYS_ADMIN)) { 1649a1316544SJan Kara err = loop_set_status_old(lo, 1650a1316544SJan Kara (struct loop_info __user *)arg); 1651a1316544SJan Kara } 1652a1316544SJan Kara break; 1653a1316544SJan Kara case LOOP_GET_STATUS: 16544a5ce9baSJan Kara return loop_get_status_old(lo, (struct loop_info __user *) arg); 1655a1316544SJan Kara case LOOP_SET_STATUS64: 1656a1316544SJan Kara err = -EPERM; 1657a1316544SJan Kara if ((mode & FMODE_WRITE) || capable(CAP_SYS_ADMIN)) { 1658a1316544SJan Kara err = loop_set_status64(lo, 1659a1316544SJan Kara (struct loop_info64 __user *) arg); 1660a1316544SJan Kara } 1661a1316544SJan Kara break; 1662a1316544SJan Kara case LOOP_GET_STATUS64: 16634a5ce9baSJan Kara return loop_get_status64(lo, (struct loop_info64 __user *) arg); 1664a1316544SJan Kara case LOOP_SET_CAPACITY: 1665a1316544SJan Kara case LOOP_SET_DIRECT_IO: 1666a1316544SJan Kara case LOOP_SET_BLOCK_SIZE: 1667a1316544SJan Kara if (!(mode & FMODE_WRITE) && !capable(CAP_SYS_ADMIN)) 1668a1316544SJan Kara return -EPERM; 1669a1316544SJan Kara /* Fall through */ 1670a1316544SJan Kara default: 1671a1316544SJan Kara err = lo_simple_ioctl(lo, cmd, arg); 1672a1316544SJan Kara break; 1673a1316544SJan Kara } 1674a1316544SJan Kara 16751da177e4SLinus Torvalds return err; 16761da177e4SLinus Torvalds } 16771da177e4SLinus Torvalds 1678863d5b82SDavid Howells #ifdef CONFIG_COMPAT 1679863d5b82SDavid Howells struct compat_loop_info { 1680863d5b82SDavid Howells compat_int_t lo_number; /* ioctl r/o */ 1681863d5b82SDavid Howells compat_dev_t lo_device; /* ioctl r/o */ 1682863d5b82SDavid Howells compat_ulong_t lo_inode; /* ioctl r/o */ 1683863d5b82SDavid Howells compat_dev_t lo_rdevice; /* ioctl r/o */ 1684863d5b82SDavid Howells compat_int_t lo_offset; 1685863d5b82SDavid Howells compat_int_t lo_encrypt_type; 1686863d5b82SDavid Howells compat_int_t lo_encrypt_key_size; /* ioctl w/o */ 1687863d5b82SDavid Howells compat_int_t lo_flags; /* ioctl r/o */ 1688863d5b82SDavid Howells char lo_name[LO_NAME_SIZE]; 1689863d5b82SDavid Howells unsigned char lo_encrypt_key[LO_KEY_SIZE]; /* ioctl w/o */ 1690863d5b82SDavid Howells compat_ulong_t lo_init[2]; 1691863d5b82SDavid Howells char reserved[4]; 1692863d5b82SDavid Howells }; 1693863d5b82SDavid Howells 1694863d5b82SDavid Howells /* 1695863d5b82SDavid Howells * Transfer 32-bit compatibility structure in userspace to 64-bit loop info 1696863d5b82SDavid Howells * - noinlined to reduce stack space usage in main part of driver 1697863d5b82SDavid Howells */ 1698863d5b82SDavid Howells static noinline int 1699ba674cfcSAl Viro loop_info64_from_compat(const struct compat_loop_info __user *arg, 1700863d5b82SDavid Howells struct loop_info64 *info64) 1701863d5b82SDavid Howells { 1702863d5b82SDavid Howells struct compat_loop_info info; 1703863d5b82SDavid Howells 1704863d5b82SDavid Howells if (copy_from_user(&info, arg, sizeof(info))) 1705863d5b82SDavid Howells return -EFAULT; 1706863d5b82SDavid Howells 1707863d5b82SDavid Howells memset(info64, 0, sizeof(*info64)); 1708863d5b82SDavid Howells info64->lo_number = info.lo_number; 1709863d5b82SDavid Howells info64->lo_device = info.lo_device; 1710863d5b82SDavid Howells info64->lo_inode = info.lo_inode; 1711863d5b82SDavid Howells info64->lo_rdevice = info.lo_rdevice; 1712863d5b82SDavid Howells info64->lo_offset = info.lo_offset; 1713863d5b82SDavid Howells info64->lo_sizelimit = 0; 1714863d5b82SDavid Howells info64->lo_encrypt_type = info.lo_encrypt_type; 1715863d5b82SDavid Howells info64->lo_encrypt_key_size = info.lo_encrypt_key_size; 1716863d5b82SDavid Howells info64->lo_flags = info.lo_flags; 1717863d5b82SDavid Howells info64->lo_init[0] = info.lo_init[0]; 1718863d5b82SDavid Howells info64->lo_init[1] = info.lo_init[1]; 1719863d5b82SDavid Howells if (info.lo_encrypt_type == LO_CRYPT_CRYPTOAPI) 1720863d5b82SDavid Howells memcpy(info64->lo_crypt_name, info.lo_name, LO_NAME_SIZE); 1721863d5b82SDavid Howells else 1722863d5b82SDavid Howells memcpy(info64->lo_file_name, info.lo_name, LO_NAME_SIZE); 1723863d5b82SDavid Howells memcpy(info64->lo_encrypt_key, info.lo_encrypt_key, LO_KEY_SIZE); 1724863d5b82SDavid Howells return 0; 1725863d5b82SDavid Howells } 1726863d5b82SDavid Howells 1727863d5b82SDavid Howells /* 1728863d5b82SDavid Howells * Transfer 64-bit loop info to 32-bit compatibility structure in userspace 1729863d5b82SDavid Howells * - noinlined to reduce stack space usage in main part of driver 1730863d5b82SDavid Howells */ 1731863d5b82SDavid Howells static noinline int 1732863d5b82SDavid Howells loop_info64_to_compat(const struct loop_info64 *info64, 1733863d5b82SDavid Howells struct compat_loop_info __user *arg) 1734863d5b82SDavid Howells { 1735863d5b82SDavid Howells struct compat_loop_info info; 1736863d5b82SDavid Howells 1737863d5b82SDavid Howells memset(&info, 0, sizeof(info)); 1738863d5b82SDavid Howells info.lo_number = info64->lo_number; 1739863d5b82SDavid Howells info.lo_device = info64->lo_device; 1740863d5b82SDavid Howells info.lo_inode = info64->lo_inode; 1741863d5b82SDavid Howells info.lo_rdevice = info64->lo_rdevice; 1742863d5b82SDavid Howells info.lo_offset = info64->lo_offset; 1743863d5b82SDavid Howells info.lo_encrypt_type = info64->lo_encrypt_type; 1744863d5b82SDavid Howells info.lo_encrypt_key_size = info64->lo_encrypt_key_size; 1745863d5b82SDavid Howells info.lo_flags = info64->lo_flags; 1746863d5b82SDavid Howells info.lo_init[0] = info64->lo_init[0]; 1747863d5b82SDavid Howells info.lo_init[1] = info64->lo_init[1]; 1748863d5b82SDavid Howells if (info.lo_encrypt_type == LO_CRYPT_CRYPTOAPI) 1749863d5b82SDavid Howells memcpy(info.lo_name, info64->lo_crypt_name, LO_NAME_SIZE); 1750863d5b82SDavid Howells else 1751863d5b82SDavid Howells memcpy(info.lo_name, info64->lo_file_name, LO_NAME_SIZE); 1752863d5b82SDavid Howells memcpy(info.lo_encrypt_key, info64->lo_encrypt_key, LO_KEY_SIZE); 1753863d5b82SDavid Howells 1754863d5b82SDavid Howells /* error in case values were truncated */ 1755863d5b82SDavid Howells if (info.lo_device != info64->lo_device || 1756863d5b82SDavid Howells info.lo_rdevice != info64->lo_rdevice || 1757863d5b82SDavid Howells info.lo_inode != info64->lo_inode || 1758863d5b82SDavid Howells info.lo_offset != info64->lo_offset || 1759863d5b82SDavid Howells info.lo_init[0] != info64->lo_init[0] || 1760863d5b82SDavid Howells info.lo_init[1] != info64->lo_init[1]) 1761863d5b82SDavid Howells return -EOVERFLOW; 1762863d5b82SDavid Howells 1763863d5b82SDavid Howells if (copy_to_user(arg, &info, sizeof(info))) 1764863d5b82SDavid Howells return -EFAULT; 1765863d5b82SDavid Howells return 0; 1766863d5b82SDavid Howells } 1767863d5b82SDavid Howells 1768863d5b82SDavid Howells static int 1769863d5b82SDavid Howells loop_set_status_compat(struct loop_device *lo, 1770863d5b82SDavid Howells const struct compat_loop_info __user *arg) 1771863d5b82SDavid Howells { 1772863d5b82SDavid Howells struct loop_info64 info64; 1773863d5b82SDavid Howells int ret; 1774863d5b82SDavid Howells 1775863d5b82SDavid Howells ret = loop_info64_from_compat(arg, &info64); 1776863d5b82SDavid Howells if (ret < 0) 1777863d5b82SDavid Howells return ret; 1778863d5b82SDavid Howells return loop_set_status(lo, &info64); 1779863d5b82SDavid Howells } 1780863d5b82SDavid Howells 1781863d5b82SDavid Howells static int 1782863d5b82SDavid Howells loop_get_status_compat(struct loop_device *lo, 1783863d5b82SDavid Howells struct compat_loop_info __user *arg) 1784863d5b82SDavid Howells { 1785863d5b82SDavid Howells struct loop_info64 info64; 1786bdac616dSOmar Sandoval int err; 1787863d5b82SDavid Howells 17884a5ce9baSJan Kara if (!arg) 1789bdac616dSOmar Sandoval return -EINVAL; 1790863d5b82SDavid Howells err = loop_get_status(lo, &info64); 1791863d5b82SDavid Howells if (!err) 1792863d5b82SDavid Howells err = loop_info64_to_compat(&info64, arg); 1793863d5b82SDavid Howells return err; 1794863d5b82SDavid Howells } 1795863d5b82SDavid Howells 1796bb214884SAl Viro static int lo_compat_ioctl(struct block_device *bdev, fmode_t mode, 1797bb214884SAl Viro unsigned int cmd, unsigned long arg) 1798863d5b82SDavid Howells { 1799bb214884SAl Viro struct loop_device *lo = bdev->bd_disk->private_data; 1800863d5b82SDavid Howells int err; 1801863d5b82SDavid Howells 1802863d5b82SDavid Howells switch(cmd) { 1803863d5b82SDavid Howells case LOOP_SET_STATUS: 18043148ffbdSOmar Sandoval err = loop_set_status_compat(lo, 18053148ffbdSOmar Sandoval (const struct compat_loop_info __user *)arg); 1806863d5b82SDavid Howells break; 1807863d5b82SDavid Howells case LOOP_GET_STATUS: 18083148ffbdSOmar Sandoval err = loop_get_status_compat(lo, 18093148ffbdSOmar Sandoval (struct compat_loop_info __user *)arg); 1810863d5b82SDavid Howells break; 181153d66608SJ. R. Okajima case LOOP_SET_CAPACITY: 1812863d5b82SDavid Howells case LOOP_CLR_FD: 1813863d5b82SDavid Howells case LOOP_GET_STATUS64: 1814863d5b82SDavid Howells case LOOP_SET_STATUS64: 1815863d5b82SDavid Howells arg = (unsigned long) compat_ptr(arg); 1816d893ff86SGustavo A. R. Silva /* fall through */ 1817863d5b82SDavid Howells case LOOP_SET_FD: 1818863d5b82SDavid Howells case LOOP_CHANGE_FD: 18199fea4b39SEvan Green case LOOP_SET_BLOCK_SIZE: 1820fdbe4eeeSAlessio Balsini case LOOP_SET_DIRECT_IO: 1821bb214884SAl Viro err = lo_ioctl(bdev, mode, cmd, arg); 1822863d5b82SDavid Howells break; 1823863d5b82SDavid Howells default: 1824863d5b82SDavid Howells err = -ENOIOCTLCMD; 1825863d5b82SDavid Howells break; 1826863d5b82SDavid Howells } 1827863d5b82SDavid Howells return err; 1828863d5b82SDavid Howells } 1829863d5b82SDavid Howells #endif 1830863d5b82SDavid Howells 1831bb214884SAl Viro static int lo_open(struct block_device *bdev, fmode_t mode) 18321da177e4SLinus Torvalds { 1833770fe30aSKay Sievers struct loop_device *lo; 18340a42e99bSJan Kara int err; 1835770fe30aSKay Sievers 18360a42e99bSJan Kara err = mutex_lock_killable(&loop_ctl_mutex); 18370a42e99bSJan Kara if (err) 18380a42e99bSJan Kara return err; 1839770fe30aSKay Sievers lo = bdev->bd_disk->private_data; 1840770fe30aSKay Sievers if (!lo) { 1841770fe30aSKay Sievers err = -ENXIO; 1842770fe30aSKay Sievers goto out; 1843770fe30aSKay Sievers } 18441da177e4SLinus Torvalds 1845f8933667SMing Lei atomic_inc(&lo->lo_refcnt); 1846770fe30aSKay Sievers out: 18470a42e99bSJan Kara mutex_unlock(&loop_ctl_mutex); 1848770fe30aSKay Sievers return err; 18491da177e4SLinus Torvalds } 18501da177e4SLinus Torvalds 1851967d1dc1SJan Kara static void lo_release(struct gendisk *disk, fmode_t mode) 18521da177e4SLinus Torvalds { 1853967d1dc1SJan Kara struct loop_device *lo; 18541da177e4SLinus Torvalds 18550a42e99bSJan Kara mutex_lock(&loop_ctl_mutex); 1856967d1dc1SJan Kara lo = disk->private_data; 1857f8933667SMing Lei if (atomic_dec_return(&lo->lo_refcnt)) 18580a42e99bSJan Kara goto out_unlock; 1859f8933667SMing Lei 186014f27939SMilan Broz if (lo->lo_flags & LO_FLAGS_AUTOCLEAR) { 1861a2505b79SJan Kara if (lo->lo_state != Lo_bound) 1862a2505b79SJan Kara goto out_unlock; 1863a2505b79SJan Kara lo->lo_state = Lo_rundown; 18647ccd0791SJan Kara mutex_unlock(&loop_ctl_mutex); 186514f27939SMilan Broz /* 186614f27939SMilan Broz * In autoclear mode, stop the loop thread 186714f27939SMilan Broz * and remove configuration after last close. 186814f27939SMilan Broz */ 18690da03cabSJan Kara __loop_clr_fd(lo, true); 18700a42e99bSJan Kara return; 187143cade80SOmar Sandoval } else if (lo->lo_state == Lo_bound) { 187214f27939SMilan Broz /* 187314f27939SMilan Broz * Otherwise keep thread (if running) and config, 187414f27939SMilan Broz * but flush possible ongoing bios in thread. 187514f27939SMilan Broz */ 187643cade80SOmar Sandoval blk_mq_freeze_queue(lo->lo_queue); 187743cade80SOmar Sandoval blk_mq_unfreeze_queue(lo->lo_queue); 187814f27939SMilan Broz } 187996c58655SDavid Woodhouse 18800a42e99bSJan Kara out_unlock: 1881310ca162STetsuo Handa mutex_unlock(&loop_ctl_mutex); 1882ae665016SLinus Torvalds } 1883ae665016SLinus Torvalds 188483d5cde4SAlexey Dobriyan static const struct block_device_operations lo_fops = { 18851da177e4SLinus Torvalds .owner = THIS_MODULE, 1886bb214884SAl Viro .open = lo_open, 1887bb214884SAl Viro .release = lo_release, 1888bb214884SAl Viro .ioctl = lo_ioctl, 1889863d5b82SDavid Howells #ifdef CONFIG_COMPAT 1890bb214884SAl Viro .compat_ioctl = lo_compat_ioctl, 1891863d5b82SDavid Howells #endif 18921da177e4SLinus Torvalds }; 18931da177e4SLinus Torvalds 18941da177e4SLinus Torvalds /* 18951da177e4SLinus Torvalds * And now the modules code and kernel interface. 18961da177e4SLinus Torvalds */ 189773285082SKen Chen static int max_loop; 18985657a819SJoe Perches module_param(max_loop, int, 0444); 1899a47653fcSKen Chen MODULE_PARM_DESC(max_loop, "Maximum number of loop devices"); 19005657a819SJoe Perches module_param(max_part, int, 0444); 1901476a4813SLaurent Vivier MODULE_PARM_DESC(max_part, "Maximum number of partitions per loop device"); 19021da177e4SLinus Torvalds MODULE_LICENSE("GPL"); 19031da177e4SLinus Torvalds MODULE_ALIAS_BLOCKDEV_MAJOR(LOOP_MAJOR); 19041da177e4SLinus Torvalds 19051da177e4SLinus Torvalds int loop_register_transfer(struct loop_func_table *funcs) 19061da177e4SLinus Torvalds { 19071da177e4SLinus Torvalds unsigned int n = funcs->number; 19081da177e4SLinus Torvalds 19091da177e4SLinus Torvalds if (n >= MAX_LO_CRYPT || xfer_funcs[n]) 19101da177e4SLinus Torvalds return -EINVAL; 19111da177e4SLinus Torvalds xfer_funcs[n] = funcs; 19121da177e4SLinus Torvalds return 0; 19131da177e4SLinus Torvalds } 19141da177e4SLinus Torvalds 191534dd82afSKay Sievers static int unregister_transfer_cb(int id, void *ptr, void *data) 191634dd82afSKay Sievers { 191734dd82afSKay Sievers struct loop_device *lo = ptr; 191834dd82afSKay Sievers struct loop_func_table *xfer = data; 191934dd82afSKay Sievers 1920310ca162STetsuo Handa mutex_lock(&loop_ctl_mutex); 192134dd82afSKay Sievers if (lo->lo_encryption == xfer) 192234dd82afSKay Sievers loop_release_xfer(lo); 1923310ca162STetsuo Handa mutex_unlock(&loop_ctl_mutex); 192434dd82afSKay Sievers return 0; 192534dd82afSKay Sievers } 192634dd82afSKay Sievers 19271da177e4SLinus Torvalds int loop_unregister_transfer(int number) 19281da177e4SLinus Torvalds { 19291da177e4SLinus Torvalds unsigned int n = number; 19301da177e4SLinus Torvalds struct loop_func_table *xfer; 19311da177e4SLinus Torvalds 19321da177e4SLinus Torvalds if (n == 0 || n >= MAX_LO_CRYPT || (xfer = xfer_funcs[n]) == NULL) 19331da177e4SLinus Torvalds return -EINVAL; 19341da177e4SLinus Torvalds 19351da177e4SLinus Torvalds xfer_funcs[n] = NULL; 193634dd82afSKay Sievers idr_for_each(&loop_index_idr, &unregister_transfer_cb, xfer); 19371da177e4SLinus Torvalds return 0; 19381da177e4SLinus Torvalds } 19391da177e4SLinus Torvalds 19401da177e4SLinus Torvalds EXPORT_SYMBOL(loop_register_transfer); 19411da177e4SLinus Torvalds EXPORT_SYMBOL(loop_unregister_transfer); 19421da177e4SLinus Torvalds 1943fc17b653SChristoph Hellwig static blk_status_t loop_queue_rq(struct blk_mq_hw_ctx *hctx, 1944b5dd2f60SMing Lei const struct blk_mq_queue_data *bd) 1945b5dd2f60SMing Lei { 19461894e916SJens Axboe struct request *rq = bd->rq; 19471894e916SJens Axboe struct loop_cmd *cmd = blk_mq_rq_to_pdu(rq); 19481894e916SJens Axboe struct loop_device *lo = rq->q->queuedata; 1949b5dd2f60SMing Lei 19501894e916SJens Axboe blk_mq_start_request(rq); 1951b5dd2f60SMing Lei 1952f4aa4c7bSMing Lei if (lo->lo_state != Lo_bound) 1953fc17b653SChristoph Hellwig return BLK_STS_IOERR; 1954f4aa4c7bSMing Lei 19551894e916SJens Axboe switch (req_op(rq)) { 1956f0225cacSChristoph Hellwig case REQ_OP_FLUSH: 1957f0225cacSChristoph Hellwig case REQ_OP_DISCARD: 195819372e27SChristoph Hellwig case REQ_OP_WRITE_ZEROES: 1959bc07c10aSMing Lei cmd->use_aio = false; 1960f0225cacSChristoph Hellwig break; 1961f0225cacSChristoph Hellwig default: 1962f0225cacSChristoph Hellwig cmd->use_aio = lo->use_dio; 1963f0225cacSChristoph Hellwig break; 1964f0225cacSChristoph Hellwig } 1965bc07c10aSMing Lei 1966d4478e92SShaohua Li /* always use the first bio's css */ 19670b508bc9SShaohua Li #ifdef CONFIG_BLK_CGROUP 1968db6638d7SDennis Zhou if (cmd->use_aio && rq->bio && rq->bio->bi_blkg) { 1969db6638d7SDennis Zhou cmd->css = &bio_blkcg(rq->bio)->css; 1970d4478e92SShaohua Li css_get(cmd->css); 1971d4478e92SShaohua Li } else 1972d4478e92SShaohua Li #endif 1973d4478e92SShaohua Li cmd->css = NULL; 19743989144fSPetr Mladek kthread_queue_work(&lo->worker, &cmd->work); 1975b5dd2f60SMing Lei 1976fc17b653SChristoph Hellwig return BLK_STS_OK; 1977b5dd2f60SMing Lei } 1978b5dd2f60SMing Lei 1979b5dd2f60SMing Lei static void loop_handle_cmd(struct loop_cmd *cmd) 1980b5dd2f60SMing Lei { 19811894e916SJens Axboe struct request *rq = blk_mq_rq_from_pdu(cmd); 19821894e916SJens Axboe const bool write = op_is_write(req_op(rq)); 19831894e916SJens Axboe struct loop_device *lo = rq->q->queuedata; 1984f4829a9bSChristoph Hellwig int ret = 0; 1985b5dd2f60SMing Lei 1986f4829a9bSChristoph Hellwig if (write && (lo->lo_flags & LO_FLAGS_READ_ONLY)) { 1987f4829a9bSChristoph Hellwig ret = -EIO; 1988b5dd2f60SMing Lei goto failed; 1989f4829a9bSChristoph Hellwig } 1990b5dd2f60SMing Lei 19911894e916SJens Axboe ret = do_req_filebacked(lo, rq); 1992b5dd2f60SMing Lei failed: 1993bc07c10aSMing Lei /* complete non-aio request */ 1994fe2cb290SChristoph Hellwig if (!cmd->use_aio || ret) { 19958cd55087SEvan Green if (ret == -EOPNOTSUPP) 19968cd55087SEvan Green cmd->ret = ret; 19978cd55087SEvan Green else 1998fe2cb290SChristoph Hellwig cmd->ret = ret ? -EIO : 0; 19991894e916SJens Axboe blk_mq_complete_request(rq); 2000fe2cb290SChristoph Hellwig } 2001b5dd2f60SMing Lei } 2002b5dd2f60SMing Lei 2003e03a3d7aSMing Lei static void loop_queue_work(struct kthread_work *work) 2004b5dd2f60SMing Lei { 2005b5dd2f60SMing Lei struct loop_cmd *cmd = 2006e03a3d7aSMing Lei container_of(work, struct loop_cmd, work); 2007b5dd2f60SMing Lei 2008b5dd2f60SMing Lei loop_handle_cmd(cmd); 2009b5dd2f60SMing Lei } 2010b5dd2f60SMing Lei 2011d6296d39SChristoph Hellwig static int loop_init_request(struct blk_mq_tag_set *set, struct request *rq, 2012d6296d39SChristoph Hellwig unsigned int hctx_idx, unsigned int numa_node) 2013b5dd2f60SMing Lei { 2014b5dd2f60SMing Lei struct loop_cmd *cmd = blk_mq_rq_to_pdu(rq); 2015b5dd2f60SMing Lei 20163989144fSPetr Mladek kthread_init_work(&cmd->work, loop_queue_work); 2017b5dd2f60SMing Lei return 0; 2018b5dd2f60SMing Lei } 2019b5dd2f60SMing Lei 2020f363b089SEric Biggers static const struct blk_mq_ops loop_mq_ops = { 2021b5dd2f60SMing Lei .queue_rq = loop_queue_rq, 2022b5dd2f60SMing Lei .init_request = loop_init_request, 2023fe2cb290SChristoph Hellwig .complete = lo_complete_rq, 2024b5dd2f60SMing Lei }; 2025b5dd2f60SMing Lei 202634dd82afSKay Sievers static int loop_add(struct loop_device **l, int i) 20271da177e4SLinus Torvalds { 202873285082SKen Chen struct loop_device *lo; 202973285082SKen Chen struct gendisk *disk; 203034dd82afSKay Sievers int err; 20311da177e4SLinus Torvalds 203234dd82afSKay Sievers err = -ENOMEM; 203368d740d7SSilva Paulo lo = kzalloc(sizeof(*lo), GFP_KERNEL); 203468d740d7SSilva Paulo if (!lo) 203573285082SKen Chen goto out; 203634dd82afSKay Sievers 2037ef7e7c82SMikulas Patocka lo->lo_state = Lo_unbound; 2038ef7e7c82SMikulas Patocka 2039c718aa65STejun Heo /* allocate id, if @id >= 0, we're requesting that specific id */ 204034dd82afSKay Sievers if (i >= 0) { 2041c718aa65STejun Heo err = idr_alloc(&loop_index_idr, lo, i, i + 1, GFP_KERNEL); 2042c718aa65STejun Heo if (err == -ENOSPC) 204334dd82afSKay Sievers err = -EEXIST; 204434dd82afSKay Sievers } else { 2045c718aa65STejun Heo err = idr_alloc(&loop_index_idr, lo, 0, 0, GFP_KERNEL); 204634dd82afSKay Sievers } 204734dd82afSKay Sievers if (err < 0) 204834dd82afSKay Sievers goto out_free_dev; 2049c718aa65STejun Heo i = err; 20501da177e4SLinus Torvalds 2051183cfb57SWei Yongjun err = -ENOMEM; 2052b5dd2f60SMing Lei lo->tag_set.ops = &loop_mq_ops; 2053b5dd2f60SMing Lei lo->tag_set.nr_hw_queues = 1; 2054b5dd2f60SMing Lei lo->tag_set.queue_depth = 128; 2055b5dd2f60SMing Lei lo->tag_set.numa_node = NUMA_NO_NODE; 2056b5dd2f60SMing Lei lo->tag_set.cmd_size = sizeof(struct loop_cmd); 205756d18f62SMing Lei lo->tag_set.flags = BLK_MQ_F_SHOULD_MERGE; 2058b5dd2f60SMing Lei lo->tag_set.driver_data = lo; 2059b5dd2f60SMing Lei 2060b5dd2f60SMing Lei err = blk_mq_alloc_tag_set(&lo->tag_set); 2061b5dd2f60SMing Lei if (err) 20623ec981e3SMikulas Patocka goto out_free_idr; 206373285082SKen Chen 2064b5dd2f60SMing Lei lo->lo_queue = blk_mq_init_queue(&lo->tag_set); 206538a3499fSChengguang Xu if (IS_ERR(lo->lo_queue)) { 2066b5dd2f60SMing Lei err = PTR_ERR(lo->lo_queue); 2067b5dd2f60SMing Lei goto out_cleanup_tags; 2068b5dd2f60SMing Lei } 2069ef7e7c82SMikulas Patocka lo->lo_queue->queuedata = lo; 2070ef7e7c82SMikulas Patocka 207154bb0adeSShaohua Li blk_queue_max_hw_sectors(lo->lo_queue, BLK_DEF_MAX_SECTORS); 207240326d8aSShaohua Li 20735b5e20f4SMing Lei /* 207440326d8aSShaohua Li * By default, we do buffer IO, so it doesn't make sense to enable 207540326d8aSShaohua Li * merge because the I/O submitted to backing file is handled page by 207640326d8aSShaohua Li * page. For directio mode, merge does help to dispatch bigger request 207740326d8aSShaohua Li * to underlayer disk. We will enable merge once directio is enabled. 20785b5e20f4SMing Lei */ 20798b904b5bSBart Van Assche blk_queue_flag_set(QUEUE_FLAG_NOMERGES, lo->lo_queue); 20805b5e20f4SMing Lei 20817a649737SMinfei Huang err = -ENOMEM; 2082476a4813SLaurent Vivier disk = lo->lo_disk = alloc_disk(1 << part_shift); 208373285082SKen Chen if (!disk) 208473285082SKen Chen goto out_free_queue; 208573285082SKen Chen 2086e03c8dd1SKay Sievers /* 2087e03c8dd1SKay Sievers * Disable partition scanning by default. The in-kernel partition 2088e03c8dd1SKay Sievers * scanning can be requested individually per-device during its 2089e03c8dd1SKay Sievers * setup. Userspace can always add and remove partitions from all 2090e03c8dd1SKay Sievers * devices. The needed partition minors are allocated from the 2091e03c8dd1SKay Sievers * extended minor space, the main loop device numbers will continue 2092e03c8dd1SKay Sievers * to match the loop minors, regardless of the number of partitions 2093e03c8dd1SKay Sievers * used. 2094e03c8dd1SKay Sievers * 2095e03c8dd1SKay Sievers * If max_part is given, partition scanning is globally enabled for 2096e03c8dd1SKay Sievers * all loop devices. The minors for the main loop devices will be 2097e03c8dd1SKay Sievers * multiples of max_part. 2098e03c8dd1SKay Sievers * 2099e03c8dd1SKay Sievers * Note: Global-for-all-devices, set-only-at-init, read-only module 2100e03c8dd1SKay Sievers * parameteters like 'max_loop' and 'max_part' make things needlessly 2101e03c8dd1SKay Sievers * complicated, are too static, inflexible and may surprise 2102e03c8dd1SKay Sievers * userspace tools. Parameters like this in general should be avoided. 2103e03c8dd1SKay Sievers */ 2104e03c8dd1SKay Sievers if (!part_shift) 2105e03c8dd1SKay Sievers disk->flags |= GENHD_FL_NO_PART_SCAN; 2106e03c8dd1SKay Sievers disk->flags |= GENHD_FL_EXT_DEVT; 2107f8933667SMing Lei atomic_set(&lo->lo_refcnt, 0); 21081da177e4SLinus Torvalds lo->lo_number = i; 21091da177e4SLinus Torvalds spin_lock_init(&lo->lo_lock); 21101da177e4SLinus Torvalds disk->major = LOOP_MAJOR; 2111476a4813SLaurent Vivier disk->first_minor = i << part_shift; 21121da177e4SLinus Torvalds disk->fops = &lo_fops; 21131da177e4SLinus Torvalds disk->private_data = lo; 21141da177e4SLinus Torvalds disk->queue = lo->lo_queue; 211573285082SKen Chen sprintf(disk->disk_name, "loop%d", i); 211634dd82afSKay Sievers add_disk(disk); 211734dd82afSKay Sievers *l = lo; 211834dd82afSKay Sievers return lo->lo_number; 211973285082SKen Chen 212073285082SKen Chen out_free_queue: 212173285082SKen Chen blk_cleanup_queue(lo->lo_queue); 2122b5dd2f60SMing Lei out_cleanup_tags: 2123b5dd2f60SMing Lei blk_mq_free_tag_set(&lo->tag_set); 21243ec981e3SMikulas Patocka out_free_idr: 21253ec981e3SMikulas Patocka idr_remove(&loop_index_idr, i); 212673285082SKen Chen out_free_dev: 212773285082SKen Chen kfree(lo); 212873285082SKen Chen out: 212934dd82afSKay Sievers return err; 21301da177e4SLinus Torvalds } 21311da177e4SLinus Torvalds 213234dd82afSKay Sievers static void loop_remove(struct loop_device *lo) 213373285082SKen Chen { 21346cd18e71SNeilBrown del_gendisk(lo->lo_disk); 21350fa8ebddSBart Van Assche blk_cleanup_queue(lo->lo_queue); 2136b5dd2f60SMing Lei blk_mq_free_tag_set(&lo->tag_set); 213773285082SKen Chen put_disk(lo->lo_disk); 213873285082SKen Chen kfree(lo); 213973285082SKen Chen } 214073285082SKen Chen 2141770fe30aSKay Sievers static int find_free_cb(int id, void *ptr, void *data) 2142770fe30aSKay Sievers { 2143770fe30aSKay Sievers struct loop_device *lo = ptr; 2144770fe30aSKay Sievers struct loop_device **l = data; 2145770fe30aSKay Sievers 2146770fe30aSKay Sievers if (lo->lo_state == Lo_unbound) { 2147770fe30aSKay Sievers *l = lo; 2148770fe30aSKay Sievers return 1; 2149770fe30aSKay Sievers } 2150770fe30aSKay Sievers return 0; 2151770fe30aSKay Sievers } 2152770fe30aSKay Sievers 215334dd82afSKay Sievers static int loop_lookup(struct loop_device **l, int i) 2154a47653fcSKen Chen { 2155a47653fcSKen Chen struct loop_device *lo; 215634dd82afSKay Sievers int ret = -ENODEV; 2157a47653fcSKen Chen 2158770fe30aSKay Sievers if (i < 0) { 2159770fe30aSKay Sievers int err; 2160770fe30aSKay Sievers 2161770fe30aSKay Sievers err = idr_for_each(&loop_index_idr, &find_free_cb, &lo); 2162770fe30aSKay Sievers if (err == 1) { 2163770fe30aSKay Sievers *l = lo; 2164770fe30aSKay Sievers ret = lo->lo_number; 2165770fe30aSKay Sievers } 2166770fe30aSKay Sievers goto out; 2167a47653fcSKen Chen } 2168a47653fcSKen Chen 2169770fe30aSKay Sievers /* lookup and return a specific i */ 217034dd82afSKay Sievers lo = idr_find(&loop_index_idr, i); 2171a47653fcSKen Chen if (lo) { 217234dd82afSKay Sievers *l = lo; 217334dd82afSKay Sievers ret = lo->lo_number; 2174a47653fcSKen Chen } 2175770fe30aSKay Sievers out: 217634dd82afSKay Sievers return ret; 2177a47653fcSKen Chen } 2178a47653fcSKen Chen 217973285082SKen Chen static struct kobject *loop_probe(dev_t dev, int *part, void *data) 218073285082SKen Chen { 2181705962ccSAl Viro struct loop_device *lo; 218207002e99SAl Viro struct kobject *kobj; 218334dd82afSKay Sievers int err; 218473285082SKen Chen 21850a42e99bSJan Kara mutex_lock(&loop_ctl_mutex); 218634dd82afSKay Sievers err = loop_lookup(&lo, MINOR(dev) >> part_shift); 218734dd82afSKay Sievers if (err < 0) 218834dd82afSKay Sievers err = loop_add(&lo, MINOR(dev) >> part_shift); 218934dd82afSKay Sievers if (err < 0) 2190a207f593SMikulas Patocka kobj = NULL; 219134dd82afSKay Sievers else 21923079c22eSJan Kara kobj = get_disk_and_module(lo->lo_disk); 21930a42e99bSJan Kara mutex_unlock(&loop_ctl_mutex); 219473285082SKen Chen 219573285082SKen Chen *part = 0; 219607002e99SAl Viro return kobj; 219773285082SKen Chen } 219873285082SKen Chen 2199770fe30aSKay Sievers static long loop_control_ioctl(struct file *file, unsigned int cmd, 2200770fe30aSKay Sievers unsigned long parm) 2201770fe30aSKay Sievers { 2202770fe30aSKay Sievers struct loop_device *lo; 22030a42e99bSJan Kara int ret; 2204770fe30aSKay Sievers 22050a42e99bSJan Kara ret = mutex_lock_killable(&loop_ctl_mutex); 22060a42e99bSJan Kara if (ret) 22070a42e99bSJan Kara return ret; 22080a42e99bSJan Kara 22090a42e99bSJan Kara ret = -ENOSYS; 2210770fe30aSKay Sievers switch (cmd) { 2211770fe30aSKay Sievers case LOOP_CTL_ADD: 2212770fe30aSKay Sievers ret = loop_lookup(&lo, parm); 2213770fe30aSKay Sievers if (ret >= 0) { 2214770fe30aSKay Sievers ret = -EEXIST; 2215770fe30aSKay Sievers break; 2216770fe30aSKay Sievers } 2217770fe30aSKay Sievers ret = loop_add(&lo, parm); 2218770fe30aSKay Sievers break; 2219770fe30aSKay Sievers case LOOP_CTL_REMOVE: 2220770fe30aSKay Sievers ret = loop_lookup(&lo, parm); 2221770fe30aSKay Sievers if (ret < 0) 2222770fe30aSKay Sievers break; 2223770fe30aSKay Sievers if (lo->lo_state != Lo_unbound) { 2224770fe30aSKay Sievers ret = -EBUSY; 2225770fe30aSKay Sievers break; 2226770fe30aSKay Sievers } 2227f8933667SMing Lei if (atomic_read(&lo->lo_refcnt) > 0) { 2228770fe30aSKay Sievers ret = -EBUSY; 2229770fe30aSKay Sievers break; 2230770fe30aSKay Sievers } 2231770fe30aSKay Sievers lo->lo_disk->private_data = NULL; 2232770fe30aSKay Sievers idr_remove(&loop_index_idr, lo->lo_number); 2233770fe30aSKay Sievers loop_remove(lo); 2234770fe30aSKay Sievers break; 2235770fe30aSKay Sievers case LOOP_CTL_GET_FREE: 2236770fe30aSKay Sievers ret = loop_lookup(&lo, -1); 2237770fe30aSKay Sievers if (ret >= 0) 2238770fe30aSKay Sievers break; 2239770fe30aSKay Sievers ret = loop_add(&lo, -1); 2240770fe30aSKay Sievers } 22410a42e99bSJan Kara mutex_unlock(&loop_ctl_mutex); 2242770fe30aSKay Sievers 2243770fe30aSKay Sievers return ret; 2244770fe30aSKay Sievers } 2245770fe30aSKay Sievers 2246770fe30aSKay Sievers static const struct file_operations loop_ctl_fops = { 2247770fe30aSKay Sievers .open = nonseekable_open, 2248770fe30aSKay Sievers .unlocked_ioctl = loop_control_ioctl, 2249770fe30aSKay Sievers .compat_ioctl = loop_control_ioctl, 2250770fe30aSKay Sievers .owner = THIS_MODULE, 2251770fe30aSKay Sievers .llseek = noop_llseek, 2252770fe30aSKay Sievers }; 2253770fe30aSKay Sievers 2254770fe30aSKay Sievers static struct miscdevice loop_misc = { 2255770fe30aSKay Sievers .minor = LOOP_CTRL_MINOR, 2256770fe30aSKay Sievers .name = "loop-control", 2257770fe30aSKay Sievers .fops = &loop_ctl_fops, 2258770fe30aSKay Sievers }; 2259770fe30aSKay Sievers 2260770fe30aSKay Sievers MODULE_ALIAS_MISCDEV(LOOP_CTRL_MINOR); 2261770fe30aSKay Sievers MODULE_ALIAS("devname:loop-control"); 2262770fe30aSKay Sievers 226373285082SKen Chen static int __init loop_init(void) 226473285082SKen Chen { 2265a47653fcSKen Chen int i, nr; 2266a47653fcSKen Chen unsigned long range; 226734dd82afSKay Sievers struct loop_device *lo; 2268770fe30aSKay Sievers int err; 2269a47653fcSKen Chen 2270476a4813SLaurent Vivier part_shift = 0; 2271ac04fee0SNamhyung Kim if (max_part > 0) { 2272476a4813SLaurent Vivier part_shift = fls(max_part); 2273476a4813SLaurent Vivier 2274ac04fee0SNamhyung Kim /* 2275ac04fee0SNamhyung Kim * Adjust max_part according to part_shift as it is exported 2276ac04fee0SNamhyung Kim * to user space so that user can decide correct minor number 2277ac04fee0SNamhyung Kim * if [s]he want to create more devices. 2278ac04fee0SNamhyung Kim * 2279ac04fee0SNamhyung Kim * Note that -1 is required because partition 0 is reserved 2280ac04fee0SNamhyung Kim * for the whole disk. 2281ac04fee0SNamhyung Kim */ 2282ac04fee0SNamhyung Kim max_part = (1UL << part_shift) - 1; 2283ac04fee0SNamhyung Kim } 2284ac04fee0SNamhyung Kim 2285b1a66504SGuo Chao if ((1UL << part_shift) > DISK_MAX_PARTS) { 2286b1a66504SGuo Chao err = -EINVAL; 2287a8c1d064SAnton Volkov goto err_out; 2288b1a66504SGuo Chao } 228978f4bb36SNamhyung Kim 2290b1a66504SGuo Chao if (max_loop > 1UL << (MINORBITS - part_shift)) { 2291b1a66504SGuo Chao err = -EINVAL; 2292a8c1d064SAnton Volkov goto err_out; 2293b1a66504SGuo Chao } 229473285082SKen Chen 2295d134b00bSKay Sievers /* 2296d134b00bSKay Sievers * If max_loop is specified, create that many devices upfront. 2297d134b00bSKay Sievers * This also becomes a hard limit. If max_loop is not specified, 2298d134b00bSKay Sievers * create CONFIG_BLK_DEV_LOOP_MIN_COUNT loop devices at module 2299d134b00bSKay Sievers * init time. Loop devices can be requested on-demand with the 2300d134b00bSKay Sievers * /dev/loop-control interface, or be instantiated by accessing 2301d134b00bSKay Sievers * a 'dead' device node. 2302d134b00bSKay Sievers */ 230373285082SKen Chen if (max_loop) { 2304a47653fcSKen Chen nr = max_loop; 2305a1c15c59SNamhyung Kim range = max_loop << part_shift; 2306a47653fcSKen Chen } else { 2307d134b00bSKay Sievers nr = CONFIG_BLK_DEV_LOOP_MIN_COUNT; 2308a1c15c59SNamhyung Kim range = 1UL << MINORBITS; 230973285082SKen Chen } 2310a47653fcSKen Chen 2311a8c1d064SAnton Volkov err = misc_register(&loop_misc); 2312a8c1d064SAnton Volkov if (err < 0) 2313a8c1d064SAnton Volkov goto err_out; 2314a8c1d064SAnton Volkov 2315a8c1d064SAnton Volkov 2316b1a66504SGuo Chao if (register_blkdev(LOOP_MAJOR, "loop")) { 2317b1a66504SGuo Chao err = -EIO; 2318b1a66504SGuo Chao goto misc_out; 2319b1a66504SGuo Chao } 2320a47653fcSKen Chen 2321a47653fcSKen Chen blk_register_region(MKDEV(LOOP_MAJOR, 0), range, 2322a47653fcSKen Chen THIS_MODULE, loop_probe, NULL, NULL); 2323a47653fcSKen Chen 2324d134b00bSKay Sievers /* pre-create number of devices given by config or max_loop */ 23250a42e99bSJan Kara mutex_lock(&loop_ctl_mutex); 232634dd82afSKay Sievers for (i = 0; i < nr; i++) 232734dd82afSKay Sievers loop_add(&lo, i); 23280a42e99bSJan Kara mutex_unlock(&loop_ctl_mutex); 232934dd82afSKay Sievers 233073285082SKen Chen printk(KERN_INFO "loop: module loaded\n"); 23311da177e4SLinus Torvalds return 0; 2332b1a66504SGuo Chao 2333b1a66504SGuo Chao misc_out: 2334b1a66504SGuo Chao misc_deregister(&loop_misc); 2335a8c1d064SAnton Volkov err_out: 2336b1a66504SGuo Chao return err; 233734dd82afSKay Sievers } 2338a47653fcSKen Chen 233934dd82afSKay Sievers static int loop_exit_cb(int id, void *ptr, void *data) 234034dd82afSKay Sievers { 234134dd82afSKay Sievers struct loop_device *lo = ptr; 2342a47653fcSKen Chen 234334dd82afSKay Sievers loop_remove(lo); 234434dd82afSKay Sievers return 0; 23451da177e4SLinus Torvalds } 23461da177e4SLinus Torvalds 234773285082SKen Chen static void __exit loop_exit(void) 23481da177e4SLinus Torvalds { 2349a47653fcSKen Chen unsigned long range; 23501da177e4SLinus Torvalds 2351a1c15c59SNamhyung Kim range = max_loop ? max_loop << part_shift : 1UL << MINORBITS; 2352a47653fcSKen Chen 235334dd82afSKay Sievers idr_for_each(&loop_index_idr, &loop_exit_cb, NULL); 235434dd82afSKay Sievers idr_destroy(&loop_index_idr); 235573285082SKen Chen 2356a47653fcSKen Chen blk_unregister_region(MKDEV(LOOP_MAJOR, 0), range); 235700d59405SAkinobu Mita unregister_blkdev(LOOP_MAJOR, "loop"); 2358770fe30aSKay Sievers 2359770fe30aSKay Sievers misc_deregister(&loop_misc); 23601da177e4SLinus Torvalds } 23611da177e4SLinus Torvalds 23621da177e4SLinus Torvalds module_init(loop_init); 23631da177e4SLinus Torvalds module_exit(loop_exit); 23641da177e4SLinus Torvalds 23651da177e4SLinus Torvalds #ifndef MODULE 23661da177e4SLinus Torvalds static int __init max_loop_setup(char *str) 23671da177e4SLinus Torvalds { 23681da177e4SLinus Torvalds max_loop = simple_strtol(str, NULL, 0); 23691da177e4SLinus Torvalds return 1; 23701da177e4SLinus Torvalds } 23711da177e4SLinus Torvalds 23721da177e4SLinus Torvalds __setup("max_loop=", max_loop_setup); 23731da177e4SLinus Torvalds #endif 2374