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> 7983a87611SAl Viro #include "loop.h" 801da177e4SLinus Torvalds 817c0f6ba6SLinus Torvalds #include <linux/uaccess.h> 821da177e4SLinus Torvalds 8334dd82afSKay Sievers static DEFINE_IDR(loop_index_idr); 8434dd82afSKay Sievers static DEFINE_MUTEX(loop_index_mutex); 851da177e4SLinus Torvalds 86476a4813SLaurent Vivier static int max_part; 87476a4813SLaurent Vivier static int part_shift; 88476a4813SLaurent Vivier 891da177e4SLinus Torvalds static int transfer_xor(struct loop_device *lo, int cmd, 901da177e4SLinus Torvalds struct page *raw_page, unsigned raw_off, 911da177e4SLinus Torvalds struct page *loop_page, unsigned loop_off, 921da177e4SLinus Torvalds int size, sector_t real_block) 931da177e4SLinus Torvalds { 94cfd8005cSCong Wang char *raw_buf = kmap_atomic(raw_page) + raw_off; 95cfd8005cSCong Wang char *loop_buf = kmap_atomic(loop_page) + loop_off; 961da177e4SLinus Torvalds char *in, *out, *key; 971da177e4SLinus Torvalds int i, keysize; 981da177e4SLinus Torvalds 991da177e4SLinus Torvalds if (cmd == READ) { 1001da177e4SLinus Torvalds in = raw_buf; 1011da177e4SLinus Torvalds out = loop_buf; 1021da177e4SLinus Torvalds } else { 1031da177e4SLinus Torvalds in = loop_buf; 1041da177e4SLinus Torvalds out = raw_buf; 1051da177e4SLinus Torvalds } 1061da177e4SLinus Torvalds 1071da177e4SLinus Torvalds key = lo->lo_encrypt_key; 1081da177e4SLinus Torvalds keysize = lo->lo_encrypt_key_size; 1091da177e4SLinus Torvalds for (i = 0; i < size; i++) 1101da177e4SLinus Torvalds *out++ = *in++ ^ key[(i & 511) % keysize]; 1111da177e4SLinus Torvalds 112cfd8005cSCong Wang kunmap_atomic(loop_buf); 113cfd8005cSCong Wang kunmap_atomic(raw_buf); 1141da177e4SLinus Torvalds cond_resched(); 1151da177e4SLinus Torvalds return 0; 1161da177e4SLinus Torvalds } 1171da177e4SLinus Torvalds 1181da177e4SLinus Torvalds static int xor_init(struct loop_device *lo, const struct loop_info64 *info) 1191da177e4SLinus Torvalds { 1201da177e4SLinus Torvalds if (unlikely(info->lo_encrypt_key_size <= 0)) 1211da177e4SLinus Torvalds return -EINVAL; 1221da177e4SLinus Torvalds return 0; 1231da177e4SLinus Torvalds } 1241da177e4SLinus Torvalds 1251da177e4SLinus Torvalds static struct loop_func_table none_funcs = { 1261da177e4SLinus Torvalds .number = LO_CRYPT_NONE, 1271da177e4SLinus Torvalds }; 1281da177e4SLinus Torvalds 1291da177e4SLinus Torvalds static struct loop_func_table xor_funcs = { 1301da177e4SLinus Torvalds .number = LO_CRYPT_XOR, 1311da177e4SLinus Torvalds .transfer = transfer_xor, 1321da177e4SLinus Torvalds .init = xor_init 1331da177e4SLinus Torvalds }; 1341da177e4SLinus Torvalds 1351da177e4SLinus Torvalds /* xfer_funcs[0] is special - its release function is never called */ 1361da177e4SLinus Torvalds static struct loop_func_table *xfer_funcs[MAX_LO_CRYPT] = { 1371da177e4SLinus Torvalds &none_funcs, 1381da177e4SLinus Torvalds &xor_funcs 1391da177e4SLinus Torvalds }; 1401da177e4SLinus Torvalds 1417035b5dfSDmitry Monakhov static loff_t get_size(loff_t offset, loff_t sizelimit, struct file *file) 1421da177e4SLinus Torvalds { 143b7a1da69SGuo Chao loff_t loopsize; 1441da177e4SLinus Torvalds 1451da177e4SLinus Torvalds /* Compute loopsize in bytes */ 146b7a1da69SGuo Chao loopsize = i_size_read(file->f_mapping->host); 147b7a1da69SGuo Chao if (offset > 0) 148b7a1da69SGuo Chao loopsize -= offset; 149b7a1da69SGuo Chao /* offset is beyond i_size, weird but possible */ 1507035b5dfSDmitry Monakhov if (loopsize < 0) 1517035b5dfSDmitry Monakhov return 0; 1521da177e4SLinus Torvalds 1537035b5dfSDmitry Monakhov if (sizelimit > 0 && sizelimit < loopsize) 1547035b5dfSDmitry Monakhov loopsize = sizelimit; 1551da177e4SLinus Torvalds /* 1561da177e4SLinus Torvalds * Unfortunately, if we want to do I/O on the device, 1571da177e4SLinus Torvalds * the number of 512-byte sectors has to fit into a sector_t. 1581da177e4SLinus Torvalds */ 1591da177e4SLinus Torvalds return loopsize >> 9; 1601da177e4SLinus Torvalds } 1611da177e4SLinus Torvalds 1627035b5dfSDmitry Monakhov static loff_t get_loop_size(struct loop_device *lo, struct file *file) 1631da177e4SLinus Torvalds { 1647035b5dfSDmitry Monakhov return get_size(lo->lo_offset, lo->lo_sizelimit, file); 1657035b5dfSDmitry Monakhov } 1667035b5dfSDmitry Monakhov 1672e5ab5f3SMing Lei static void __loop_update_dio(struct loop_device *lo, bool dio) 1682e5ab5f3SMing Lei { 1692e5ab5f3SMing Lei struct file *file = lo->lo_backing_file; 1702e5ab5f3SMing Lei struct address_space *mapping = file->f_mapping; 1712e5ab5f3SMing Lei struct inode *inode = mapping->host; 1722e5ab5f3SMing Lei unsigned short sb_bsize = 0; 1732e5ab5f3SMing Lei unsigned dio_align = 0; 1742e5ab5f3SMing Lei bool use_dio; 1752e5ab5f3SMing Lei 1762e5ab5f3SMing Lei if (inode->i_sb->s_bdev) { 1772e5ab5f3SMing Lei sb_bsize = bdev_logical_block_size(inode->i_sb->s_bdev); 1782e5ab5f3SMing Lei dio_align = sb_bsize - 1; 1792e5ab5f3SMing Lei } 1802e5ab5f3SMing Lei 1812e5ab5f3SMing Lei /* 1822e5ab5f3SMing Lei * We support direct I/O only if lo_offset is aligned with the 1832e5ab5f3SMing Lei * logical I/O size of backing device, and the logical block 1842e5ab5f3SMing Lei * size of loop is bigger than the backing device's and the loop 1852e5ab5f3SMing Lei * needn't transform transfer. 1862e5ab5f3SMing Lei * 1872e5ab5f3SMing Lei * TODO: the above condition may be loosed in the future, and 1882e5ab5f3SMing Lei * direct I/O may be switched runtime at that time because most 18989d790abSMasahiro Yamada * of requests in sane applications should be PAGE_SIZE aligned 1902e5ab5f3SMing Lei */ 1912e5ab5f3SMing Lei if (dio) { 1922e5ab5f3SMing Lei if (queue_logical_block_size(lo->lo_queue) >= sb_bsize && 1932e5ab5f3SMing Lei !(lo->lo_offset & dio_align) && 1942e5ab5f3SMing Lei mapping->a_ops->direct_IO && 1952e5ab5f3SMing Lei !lo->transfer) 1962e5ab5f3SMing Lei use_dio = true; 1972e5ab5f3SMing Lei else 1982e5ab5f3SMing Lei use_dio = false; 1992e5ab5f3SMing Lei } else { 2002e5ab5f3SMing Lei use_dio = false; 2012e5ab5f3SMing Lei } 2022e5ab5f3SMing Lei 2032e5ab5f3SMing Lei if (lo->use_dio == use_dio) 2042e5ab5f3SMing Lei return; 2052e5ab5f3SMing Lei 2062e5ab5f3SMing Lei /* flush dirty pages before changing direct IO */ 2072e5ab5f3SMing Lei vfs_fsync(file, 0); 2082e5ab5f3SMing Lei 2092e5ab5f3SMing Lei /* 2102e5ab5f3SMing Lei * The flag of LO_FLAGS_DIRECT_IO is handled similarly with 2112e5ab5f3SMing Lei * LO_FLAGS_READ_ONLY, both are set from kernel, and losetup 2122e5ab5f3SMing Lei * will get updated by ioctl(LOOP_GET_STATUS) 2132e5ab5f3SMing Lei */ 2142e5ab5f3SMing Lei blk_mq_freeze_queue(lo->lo_queue); 2152e5ab5f3SMing Lei lo->use_dio = use_dio; 21640326d8aSShaohua Li if (use_dio) { 2178b904b5bSBart Van Assche blk_queue_flag_clear(QUEUE_FLAG_NOMERGES, lo->lo_queue); 2182e5ab5f3SMing Lei lo->lo_flags |= LO_FLAGS_DIRECT_IO; 21940326d8aSShaohua Li } else { 2208b904b5bSBart Van Assche blk_queue_flag_set(QUEUE_FLAG_NOMERGES, lo->lo_queue); 2212e5ab5f3SMing Lei lo->lo_flags &= ~LO_FLAGS_DIRECT_IO; 22240326d8aSShaohua Li } 2232e5ab5f3SMing Lei blk_mq_unfreeze_queue(lo->lo_queue); 2242e5ab5f3SMing Lei } 2252e5ab5f3SMing Lei 2267035b5dfSDmitry Monakhov static int 2271e6ec9eaSOmar Sandoval figure_loop_size(struct loop_device *lo, loff_t offset, loff_t sizelimit) 2287035b5dfSDmitry Monakhov { 2297035b5dfSDmitry Monakhov loff_t size = get_size(offset, sizelimit, lo->lo_backing_file); 2301da177e4SLinus Torvalds sector_t x = (sector_t)size; 2317b0576a3SGuo Chao struct block_device *bdev = lo->lo_device; 2321da177e4SLinus Torvalds 2331da177e4SLinus Torvalds if (unlikely((loff_t)x != size)) 2341da177e4SLinus Torvalds return -EFBIG; 2357035b5dfSDmitry Monakhov if (lo->lo_offset != offset) 2367035b5dfSDmitry Monakhov lo->lo_offset = offset; 2377035b5dfSDmitry Monakhov if (lo->lo_sizelimit != sizelimit) 2387035b5dfSDmitry Monakhov lo->lo_sizelimit = sizelimit; 23973285082SKen Chen set_capacity(lo->lo_disk, x); 2407b0576a3SGuo Chao bd_set_size(bdev, (loff_t)get_capacity(bdev->bd_disk) << 9); 2417b0576a3SGuo Chao /* let user-space know about the new size */ 2427b0576a3SGuo Chao kobject_uevent(&disk_to_dev(bdev->bd_disk)->kobj, KOBJ_CHANGE); 2431da177e4SLinus Torvalds return 0; 2441da177e4SLinus Torvalds } 2451da177e4SLinus Torvalds 2461da177e4SLinus Torvalds static inline int 2471da177e4SLinus Torvalds lo_do_transfer(struct loop_device *lo, int cmd, 2481da177e4SLinus Torvalds struct page *rpage, unsigned roffs, 2491da177e4SLinus Torvalds struct page *lpage, unsigned loffs, 2501da177e4SLinus Torvalds int size, sector_t rblock) 2511da177e4SLinus Torvalds { 252aa4d8616SChristoph Hellwig int ret; 253aa4d8616SChristoph Hellwig 254aa4d8616SChristoph Hellwig ret = lo->transfer(lo, cmd, rpage, roffs, lpage, loffs, size, rblock); 255aa4d8616SChristoph Hellwig if (likely(!ret)) 2561da177e4SLinus Torvalds return 0; 2571da177e4SLinus Torvalds 258aa4d8616SChristoph Hellwig printk_ratelimited(KERN_ERR 259aa4d8616SChristoph Hellwig "loop: Transfer error at byte offset %llu, length %i.\n", 260aa4d8616SChristoph Hellwig (unsigned long long)rblock << 9, size); 261aa4d8616SChristoph Hellwig return ret; 2621da177e4SLinus Torvalds } 2631da177e4SLinus Torvalds 264aa4d8616SChristoph Hellwig static int lo_write_bvec(struct file *file, struct bio_vec *bvec, loff_t *ppos) 2651da177e4SLinus Torvalds { 266aa4d8616SChristoph Hellwig struct iov_iter i; 2671da177e4SLinus Torvalds ssize_t bw; 268283e7e5dSAl Viro 2691d037577SRoss Zwisler iov_iter_bvec(&i, ITER_BVEC | WRITE, bvec, 1, bvec->bv_len); 2701da177e4SLinus Torvalds 27103d95eb2SAl Viro file_start_write(file); 272abbb6589SChristoph Hellwig bw = vfs_iter_write(file, &i, ppos, 0); 27303d95eb2SAl Viro file_end_write(file); 274aa4d8616SChristoph Hellwig 275aa4d8616SChristoph Hellwig if (likely(bw == bvec->bv_len)) 2761da177e4SLinus Torvalds return 0; 277aa4d8616SChristoph Hellwig 278aa4d8616SChristoph Hellwig printk_ratelimited(KERN_ERR 279aa4d8616SChristoph Hellwig "loop: Write error at byte offset %llu, length %i.\n", 280aa4d8616SChristoph Hellwig (unsigned long long)*ppos, bvec->bv_len); 2811da177e4SLinus Torvalds if (bw >= 0) 2821da177e4SLinus Torvalds bw = -EIO; 2831da177e4SLinus Torvalds return bw; 2841da177e4SLinus Torvalds } 2851da177e4SLinus Torvalds 286aa4d8616SChristoph Hellwig static int lo_write_simple(struct loop_device *lo, struct request *rq, 287aa4d8616SChristoph Hellwig loff_t pos) 2881da177e4SLinus Torvalds { 289aa4d8616SChristoph Hellwig struct bio_vec bvec; 290aa4d8616SChristoph Hellwig struct req_iterator iter; 291aa4d8616SChristoph Hellwig int ret = 0; 292aa4d8616SChristoph Hellwig 293aa4d8616SChristoph Hellwig rq_for_each_segment(bvec, rq, iter) { 294aa4d8616SChristoph Hellwig ret = lo_write_bvec(lo->lo_backing_file, &bvec, &pos); 295aa4d8616SChristoph Hellwig if (ret < 0) 296aa4d8616SChristoph Hellwig break; 2971da177e4SLinus Torvalds cond_resched(); 2981da177e4SLinus Torvalds } 2991da177e4SLinus Torvalds 300aa4d8616SChristoph Hellwig return ret; 301aa4d8616SChristoph Hellwig } 302aa4d8616SChristoph Hellwig 303aa4d8616SChristoph Hellwig /* 304456be148SChristoph Hellwig * This is the slow, transforming version that needs to double buffer the 305456be148SChristoph Hellwig * data as it cannot do the transformations in place without having direct 306456be148SChristoph Hellwig * access to the destination pages of the backing file. 3071da177e4SLinus Torvalds */ 308aa4d8616SChristoph Hellwig static int lo_write_transfer(struct loop_device *lo, struct request *rq, 309aa4d8616SChristoph Hellwig loff_t pos) 3101da177e4SLinus Torvalds { 311aa4d8616SChristoph Hellwig struct bio_vec bvec, b; 31230112013SMing Lei struct req_iterator iter; 313aa4d8616SChristoph Hellwig struct page *page; 3147988613bSKent Overstreet int ret = 0; 3151da177e4SLinus Torvalds 316aa4d8616SChristoph Hellwig page = alloc_page(GFP_NOIO); 3171da177e4SLinus Torvalds if (unlikely(!page)) 318aa4d8616SChristoph Hellwig return -ENOMEM; 319456be148SChristoph Hellwig 32030112013SMing Lei rq_for_each_segment(bvec, rq, iter) { 321aa4d8616SChristoph Hellwig ret = lo_do_transfer(lo, WRITE, page, 0, bvec.bv_page, 322aa4d8616SChristoph Hellwig bvec.bv_offset, bvec.bv_len, pos >> 9); 323aa4d8616SChristoph Hellwig if (unlikely(ret)) 324aa4d8616SChristoph Hellwig break; 325aa4d8616SChristoph Hellwig 326aa4d8616SChristoph Hellwig b.bv_page = page; 327aa4d8616SChristoph Hellwig b.bv_offset = 0; 328aa4d8616SChristoph Hellwig b.bv_len = bvec.bv_len; 329aa4d8616SChristoph Hellwig ret = lo_write_bvec(lo->lo_backing_file, &b, &pos); 3301da177e4SLinus Torvalds if (ret < 0) 3311da177e4SLinus Torvalds break; 3321da177e4SLinus Torvalds } 333aa4d8616SChristoph Hellwig 3341da177e4SLinus Torvalds __free_page(page); 3351da177e4SLinus Torvalds return ret; 3361da177e4SLinus Torvalds } 3371da177e4SLinus Torvalds 338aa4d8616SChristoph Hellwig static int lo_read_simple(struct loop_device *lo, struct request *rq, 339aa4d8616SChristoph Hellwig loff_t pos) 3401da177e4SLinus Torvalds { 3417988613bSKent Overstreet struct bio_vec bvec; 34230112013SMing Lei struct req_iterator iter; 343aa4d8616SChristoph Hellwig struct iov_iter i; 344aa4d8616SChristoph Hellwig ssize_t len; 3451da177e4SLinus Torvalds 34630112013SMing Lei rq_for_each_segment(bvec, rq, iter) { 347aa4d8616SChristoph Hellwig iov_iter_bvec(&i, ITER_BVEC, &bvec, 1, bvec.bv_len); 34818e9710eSChristoph Hellwig len = vfs_iter_read(lo->lo_backing_file, &i, &pos, 0); 349aa4d8616SChristoph Hellwig if (len < 0) 350aa4d8616SChristoph Hellwig return len; 351306df071SDave Young 352aa4d8616SChristoph Hellwig flush_dcache_page(bvec.bv_page); 353aa4d8616SChristoph Hellwig 354aa4d8616SChristoph Hellwig if (len != bvec.bv_len) { 35530112013SMing Lei struct bio *bio; 35630112013SMing Lei 35730112013SMing Lei __rq_for_each_bio(bio, rq) 358306df071SDave Young zero_fill_bio(bio); 3591da177e4SLinus Torvalds break; 360306df071SDave Young } 361aa4d8616SChristoph Hellwig cond_resched(); 3621da177e4SLinus Torvalds } 363aa4d8616SChristoph Hellwig 364306df071SDave Young return 0; 3651da177e4SLinus Torvalds } 3661da177e4SLinus Torvalds 367aa4d8616SChristoph Hellwig static int lo_read_transfer(struct loop_device *lo, struct request *rq, 368aa4d8616SChristoph Hellwig loff_t pos) 369aa4d8616SChristoph Hellwig { 370aa4d8616SChristoph Hellwig struct bio_vec bvec, b; 371aa4d8616SChristoph Hellwig struct req_iterator iter; 372aa4d8616SChristoph Hellwig struct iov_iter i; 373aa4d8616SChristoph Hellwig struct page *page; 374aa4d8616SChristoph Hellwig ssize_t len; 375aa4d8616SChristoph Hellwig int ret = 0; 376aa4d8616SChristoph Hellwig 377aa4d8616SChristoph Hellwig page = alloc_page(GFP_NOIO); 378aa4d8616SChristoph Hellwig if (unlikely(!page)) 379aa4d8616SChristoph Hellwig return -ENOMEM; 380aa4d8616SChristoph Hellwig 381aa4d8616SChristoph Hellwig rq_for_each_segment(bvec, rq, iter) { 382aa4d8616SChristoph Hellwig loff_t offset = pos; 383aa4d8616SChristoph Hellwig 384aa4d8616SChristoph Hellwig b.bv_page = page; 385aa4d8616SChristoph Hellwig b.bv_offset = 0; 386aa4d8616SChristoph Hellwig b.bv_len = bvec.bv_len; 387aa4d8616SChristoph Hellwig 388aa4d8616SChristoph Hellwig iov_iter_bvec(&i, ITER_BVEC, &b, 1, b.bv_len); 38918e9710eSChristoph Hellwig len = vfs_iter_read(lo->lo_backing_file, &i, &pos, 0); 390aa4d8616SChristoph Hellwig if (len < 0) { 391aa4d8616SChristoph Hellwig ret = len; 392aa4d8616SChristoph Hellwig goto out_free_page; 393aa4d8616SChristoph Hellwig } 394aa4d8616SChristoph Hellwig 395aa4d8616SChristoph Hellwig ret = lo_do_transfer(lo, READ, page, 0, bvec.bv_page, 396aa4d8616SChristoph Hellwig bvec.bv_offset, len, offset >> 9); 397aa4d8616SChristoph Hellwig if (ret) 398aa4d8616SChristoph Hellwig goto out_free_page; 399aa4d8616SChristoph Hellwig 400aa4d8616SChristoph Hellwig flush_dcache_page(bvec.bv_page); 401aa4d8616SChristoph Hellwig 402aa4d8616SChristoph Hellwig if (len != bvec.bv_len) { 403aa4d8616SChristoph Hellwig struct bio *bio; 404aa4d8616SChristoph Hellwig 405aa4d8616SChristoph Hellwig __rq_for_each_bio(bio, rq) 406aa4d8616SChristoph Hellwig zero_fill_bio(bio); 407aa4d8616SChristoph Hellwig break; 408aa4d8616SChristoph Hellwig } 409aa4d8616SChristoph Hellwig } 410aa4d8616SChristoph Hellwig 411aa4d8616SChristoph Hellwig ret = 0; 412aa4d8616SChristoph Hellwig out_free_page: 413aa4d8616SChristoph Hellwig __free_page(page); 414aa4d8616SChristoph Hellwig return ret; 415aa4d8616SChristoph Hellwig } 416aa4d8616SChristoph Hellwig 417cf655d95SMing Lei static int lo_discard(struct loop_device *lo, struct request *rq, loff_t pos) 418cf655d95SMing Lei { 419cf655d95SMing Lei /* 420cf655d95SMing Lei * We use punch hole to reclaim the free space used by the 421cf655d95SMing Lei * image a.k.a. discard. However we do not support discard if 422cf655d95SMing Lei * encryption is enabled, because it may give an attacker 423cf655d95SMing Lei * useful information. 424cf655d95SMing Lei */ 425cf655d95SMing Lei struct file *file = lo->lo_backing_file; 426cf655d95SMing Lei int mode = FALLOC_FL_PUNCH_HOLE | FALLOC_FL_KEEP_SIZE; 427cf655d95SMing Lei int ret; 428cf655d95SMing Lei 429cf655d95SMing Lei if ((!file->f_op->fallocate) || lo->lo_encrypt_key_size) { 430cf655d95SMing Lei ret = -EOPNOTSUPP; 431cf655d95SMing Lei goto out; 432cf655d95SMing Lei } 433cf655d95SMing Lei 434cf655d95SMing Lei ret = file->f_op->fallocate(file, mode, pos, blk_rq_bytes(rq)); 435cf655d95SMing Lei if (unlikely(ret && ret != -EINVAL && ret != -EOPNOTSUPP)) 436cf655d95SMing Lei ret = -EIO; 437cf655d95SMing Lei out: 438cf655d95SMing Lei return ret; 439cf655d95SMing Lei } 440cf655d95SMing Lei 441cf655d95SMing Lei static int lo_req_flush(struct loop_device *lo, struct request *rq) 442cf655d95SMing Lei { 443cf655d95SMing Lei struct file *file = lo->lo_backing_file; 444cf655d95SMing Lei int ret = vfs_fsync(file, 0); 445cf655d95SMing Lei if (unlikely(ret && ret != -EINVAL)) 446cf655d95SMing Lei ret = -EIO; 447cf655d95SMing Lei 448cf655d95SMing Lei return ret; 449cf655d95SMing Lei } 450cf655d95SMing Lei 451fe2cb290SChristoph Hellwig static void lo_complete_rq(struct request *rq) 452bc07c10aSMing Lei { 453fe2cb290SChristoph Hellwig struct loop_cmd *cmd = blk_mq_rq_to_pdu(rq); 454f9de14bcSJens Axboe blk_status_t ret = BLK_STS_OK; 455bc07c10aSMing Lei 456f9de14bcSJens Axboe if (!cmd->use_aio || cmd->ret < 0 || cmd->ret == blk_rq_bytes(rq) || 457f9de14bcSJens Axboe req_op(rq) != REQ_OP_READ) { 458f9de14bcSJens Axboe if (cmd->ret < 0) 459f9de14bcSJens Axboe ret = BLK_STS_IOERR; 460f9de14bcSJens Axboe goto end_io; 461bc07c10aSMing Lei } 462fe2cb290SChristoph Hellwig 463f9de14bcSJens Axboe /* 464f9de14bcSJens Axboe * Short READ - if we got some data, advance our request and 465f9de14bcSJens Axboe * retry it. If we got no data, end the rest with EIO. 466f9de14bcSJens Axboe */ 467f9de14bcSJens Axboe if (cmd->ret) { 468f9de14bcSJens Axboe blk_update_request(rq, BLK_STS_OK, cmd->ret); 469f9de14bcSJens Axboe cmd->ret = 0; 470f9de14bcSJens Axboe blk_mq_requeue_request(rq, true); 471f9de14bcSJens Axboe } else { 472f9de14bcSJens Axboe if (cmd->use_aio) { 473f9de14bcSJens Axboe struct bio *bio = rq->bio; 474f9de14bcSJens Axboe 475f9de14bcSJens Axboe while (bio) { 476f9de14bcSJens Axboe zero_fill_bio(bio); 477f9de14bcSJens Axboe bio = bio->bi_next; 478f9de14bcSJens Axboe } 479f9de14bcSJens Axboe } 480f9de14bcSJens Axboe ret = BLK_STS_IOERR; 481f9de14bcSJens Axboe end_io: 482f9de14bcSJens Axboe blk_mq_end_request(rq, ret); 483f9de14bcSJens Axboe } 484bc07c10aSMing Lei } 485bc07c10aSMing Lei 48692d77332SShaohua Li static void lo_rw_aio_do_completion(struct loop_cmd *cmd) 48792d77332SShaohua Li { 4881894e916SJens Axboe struct request *rq = blk_mq_rq_from_pdu(cmd); 4891894e916SJens Axboe 49092d77332SShaohua Li if (!atomic_dec_and_test(&cmd->ref)) 49192d77332SShaohua Li return; 49292d77332SShaohua Li kfree(cmd->bvec); 49392d77332SShaohua Li cmd->bvec = NULL; 4941894e916SJens Axboe blk_mq_complete_request(rq); 49592d77332SShaohua Li } 49692d77332SShaohua Li 497bc07c10aSMing Lei static void lo_rw_aio_complete(struct kiocb *iocb, long ret, long ret2) 498bc07c10aSMing Lei { 499bc07c10aSMing Lei struct loop_cmd *cmd = container_of(iocb, struct loop_cmd, iocb); 500bc07c10aSMing Lei 501d4478e92SShaohua Li if (cmd->css) 502d4478e92SShaohua Li css_put(cmd->css); 503fe2cb290SChristoph Hellwig cmd->ret = ret; 50492d77332SShaohua Li lo_rw_aio_do_completion(cmd); 505bc07c10aSMing Lei } 506bc07c10aSMing Lei 507bc07c10aSMing Lei static int lo_rw_aio(struct loop_device *lo, struct loop_cmd *cmd, 508bc07c10aSMing Lei loff_t pos, bool rw) 509bc07c10aSMing Lei { 510bc07c10aSMing Lei struct iov_iter iter; 511bc07c10aSMing Lei struct bio_vec *bvec; 5121894e916SJens Axboe struct request *rq = blk_mq_rq_from_pdu(cmd); 51340326d8aSShaohua Li struct bio *bio = rq->bio; 514bc07c10aSMing Lei struct file *file = lo->lo_backing_file; 51540326d8aSShaohua Li unsigned int offset; 51640326d8aSShaohua Li int segments = 0; 517bc07c10aSMing Lei int ret; 518bc07c10aSMing Lei 51940326d8aSShaohua Li if (rq->bio != rq->biotail) { 52040326d8aSShaohua Li struct req_iterator iter; 52140326d8aSShaohua Li struct bio_vec tmp; 522bc07c10aSMing Lei 52340326d8aSShaohua Li __rq_for_each_bio(bio, rq) 52440326d8aSShaohua Li segments += bio_segments(bio); 52540326d8aSShaohua Li bvec = kmalloc(sizeof(struct bio_vec) * segments, GFP_NOIO); 52640326d8aSShaohua Li if (!bvec) 52740326d8aSShaohua Li return -EIO; 52840326d8aSShaohua Li cmd->bvec = bvec; 52940326d8aSShaohua Li 530a7297a6aSMing Lei /* 53140326d8aSShaohua Li * The bios of the request may be started from the middle of 53240326d8aSShaohua Li * the 'bvec' because of bio splitting, so we can't directly 53340326d8aSShaohua Li * copy bio->bi_iov_vec to new bvec. The rq_for_each_segment 53440326d8aSShaohua Li * API will take care of all details for us. 535a7297a6aSMing Lei */ 53640326d8aSShaohua Li rq_for_each_segment(tmp, rq, iter) { 53740326d8aSShaohua Li *bvec = tmp; 53840326d8aSShaohua Li bvec++; 53940326d8aSShaohua Li } 54040326d8aSShaohua Li bvec = cmd->bvec; 54140326d8aSShaohua Li offset = 0; 54240326d8aSShaohua Li } else { 54340326d8aSShaohua Li /* 54440326d8aSShaohua Li * Same here, this bio may be started from the middle of the 54540326d8aSShaohua Li * 'bvec' because of bio splitting, so offset from the bvec 54640326d8aSShaohua Li * must be passed to iov iterator 54740326d8aSShaohua Li */ 54840326d8aSShaohua Li offset = bio->bi_iter.bi_bvec_done; 54940326d8aSShaohua Li bvec = __bvec_iter_bvec(bio->bi_io_vec, bio->bi_iter); 55040326d8aSShaohua Li segments = bio_segments(bio); 55140326d8aSShaohua Li } 55292d77332SShaohua Li atomic_set(&cmd->ref, 2); 55340326d8aSShaohua Li 55440326d8aSShaohua Li iov_iter_bvec(&iter, ITER_BVEC | rw, bvec, 55540326d8aSShaohua Li segments, blk_rq_bytes(rq)); 55640326d8aSShaohua Li iter.iov_offset = offset; 557bc07c10aSMing Lei 558bc07c10aSMing Lei cmd->iocb.ki_pos = pos; 559bc07c10aSMing Lei cmd->iocb.ki_filp = file; 560bc07c10aSMing Lei cmd->iocb.ki_complete = lo_rw_aio_complete; 561bc07c10aSMing Lei cmd->iocb.ki_flags = IOCB_DIRECT; 562d4478e92SShaohua Li if (cmd->css) 563d4478e92SShaohua Li kthread_associate_blkcg(cmd->css); 564bc07c10aSMing Lei 565bc07c10aSMing Lei if (rw == WRITE) 566bb7462b6SMiklos Szeredi ret = call_write_iter(file, &cmd->iocb, &iter); 567bc07c10aSMing Lei else 568bb7462b6SMiklos Szeredi ret = call_read_iter(file, &cmd->iocb, &iter); 569bc07c10aSMing Lei 57092d77332SShaohua Li lo_rw_aio_do_completion(cmd); 571d4478e92SShaohua Li kthread_associate_blkcg(NULL); 57292d77332SShaohua Li 573bc07c10aSMing Lei if (ret != -EIOCBQUEUED) 574bc07c10aSMing Lei cmd->iocb.ki_complete(&cmd->iocb, ret, 0); 575bc07c10aSMing Lei return 0; 576bc07c10aSMing Lei } 577bc07c10aSMing Lei 578c1c87c2bSChristoph Hellwig static int do_req_filebacked(struct loop_device *lo, struct request *rq) 579bc07c10aSMing Lei { 580bc07c10aSMing Lei struct loop_cmd *cmd = blk_mq_rq_to_pdu(rq); 581c1c87c2bSChristoph Hellwig loff_t pos = ((loff_t) blk_rq_pos(rq) << 9) + lo->lo_offset; 582bc07c10aSMing Lei 583bc07c10aSMing Lei /* 584bc07c10aSMing Lei * lo_write_simple and lo_read_simple should have been covered 585bc07c10aSMing Lei * by io submit style function like lo_rw_aio(), one blocker 586bc07c10aSMing Lei * is that lo_read_simple() need to call flush_dcache_page after 587bc07c10aSMing Lei * the page is written from kernel, and it isn't easy to handle 588bc07c10aSMing Lei * this in io submit style function which submits all segments 589bc07c10aSMing Lei * of the req at one time. And direct read IO doesn't need to 590bc07c10aSMing Lei * run flush_dcache_page(). 591bc07c10aSMing Lei */ 592c1c87c2bSChristoph Hellwig switch (req_op(rq)) { 593c1c87c2bSChristoph Hellwig case REQ_OP_FLUSH: 594c1c87c2bSChristoph Hellwig return lo_req_flush(lo, rq); 595c1c87c2bSChristoph Hellwig case REQ_OP_DISCARD: 59619372e27SChristoph Hellwig case REQ_OP_WRITE_ZEROES: 597c1c87c2bSChristoph Hellwig return lo_discard(lo, rq, pos); 598c1c87c2bSChristoph Hellwig case REQ_OP_WRITE: 599c1c87c2bSChristoph Hellwig if (lo->transfer) 600c1c87c2bSChristoph Hellwig return lo_write_transfer(lo, rq, pos); 601c1c87c2bSChristoph Hellwig else if (cmd->use_aio) 602c1c87c2bSChristoph Hellwig return lo_rw_aio(lo, cmd, pos, WRITE); 603c1c87c2bSChristoph Hellwig else 604bc07c10aSMing Lei return lo_write_simple(lo, rq, pos); 605c1c87c2bSChristoph Hellwig case REQ_OP_READ: 606c1c87c2bSChristoph Hellwig if (lo->transfer) 607c1c87c2bSChristoph Hellwig return lo_read_transfer(lo, rq, pos); 608c1c87c2bSChristoph Hellwig else if (cmd->use_aio) 609c1c87c2bSChristoph Hellwig return lo_rw_aio(lo, cmd, pos, READ); 610bc07c10aSMing Lei else 611bc07c10aSMing Lei return lo_read_simple(lo, rq, pos); 612c1c87c2bSChristoph Hellwig default: 613c1c87c2bSChristoph Hellwig WARN_ON_ONCE(1); 614c1c87c2bSChristoph Hellwig return -EIO; 615c1c87c2bSChristoph Hellwig break; 616bc07c10aSMing Lei } 6171da177e4SLinus Torvalds } 6181da177e4SLinus Torvalds 6192e5ab5f3SMing Lei static inline void loop_update_dio(struct loop_device *lo) 6202e5ab5f3SMing Lei { 6212e5ab5f3SMing Lei __loop_update_dio(lo, io_is_direct(lo->lo_backing_file) | 6222e5ab5f3SMing Lei lo->use_dio); 6232e5ab5f3SMing Lei } 6242e5ab5f3SMing Lei 62506f0e9e6SMing Lei static void loop_reread_partitions(struct loop_device *lo, 62606f0e9e6SMing Lei struct block_device *bdev) 62706f0e9e6SMing Lei { 62806f0e9e6SMing Lei int rc; 62906f0e9e6SMing Lei 63006f0e9e6SMing Lei /* 63106f0e9e6SMing Lei * bd_mutex has been held already in release path, so don't 63206f0e9e6SMing Lei * acquire it if this function is called in such case. 63306f0e9e6SMing Lei * 63406f0e9e6SMing Lei * If the reread partition isn't from release path, lo_refcnt 63506f0e9e6SMing Lei * must be at least one and it can only become zero when the 63606f0e9e6SMing Lei * current holder is released. 63706f0e9e6SMing Lei */ 63806f0e9e6SMing Lei if (!atomic_read(&lo->lo_refcnt)) 63906f0e9e6SMing Lei rc = __blkdev_reread_part(bdev); 64006f0e9e6SMing Lei else 64106f0e9e6SMing Lei rc = blkdev_reread_part(bdev); 64206f0e9e6SMing Lei if (rc) 64306f0e9e6SMing Lei pr_warn("%s: partition scan of loop%d (%s) failed (rc=%d)\n", 64406f0e9e6SMing Lei __func__, lo->lo_number, lo->lo_file_name, rc); 64506f0e9e6SMing Lei } 64606f0e9e6SMing Lei 6471da177e4SLinus Torvalds /* 6481da177e4SLinus Torvalds * loop_change_fd switched the backing store of a loopback device to 6491da177e4SLinus Torvalds * a new file. This is useful for operating system installers to free up 6501da177e4SLinus Torvalds * the original file and in High Availability environments to switch to 6511da177e4SLinus Torvalds * an alternative location for the content in case of server meltdown. 6521da177e4SLinus Torvalds * This can only work if the loop device is used read-only, and if the 6531da177e4SLinus Torvalds * new backing store is the same size and type as the old backing store. 6541da177e4SLinus Torvalds */ 655bb214884SAl Viro static int loop_change_fd(struct loop_device *lo, struct block_device *bdev, 656bb214884SAl Viro unsigned int arg) 6571da177e4SLinus Torvalds { 6581da177e4SLinus Torvalds struct file *file, *old_file; 6591da177e4SLinus Torvalds struct inode *inode; 6601da177e4SLinus Torvalds int error; 6611da177e4SLinus Torvalds 6621da177e4SLinus Torvalds error = -ENXIO; 6631da177e4SLinus Torvalds if (lo->lo_state != Lo_bound) 6641da177e4SLinus Torvalds goto out; 6651da177e4SLinus Torvalds 6661da177e4SLinus Torvalds /* the loop device has to be read-only */ 6671da177e4SLinus Torvalds error = -EINVAL; 6681da177e4SLinus Torvalds if (!(lo->lo_flags & LO_FLAGS_READ_ONLY)) 6691da177e4SLinus Torvalds goto out; 6701da177e4SLinus Torvalds 6711da177e4SLinus Torvalds error = -EBADF; 6721da177e4SLinus Torvalds file = fget(arg); 6731da177e4SLinus Torvalds if (!file) 6741da177e4SLinus Torvalds goto out; 6751da177e4SLinus Torvalds 6761da177e4SLinus Torvalds inode = file->f_mapping->host; 6771da177e4SLinus Torvalds old_file = lo->lo_backing_file; 6781da177e4SLinus Torvalds 6791da177e4SLinus Torvalds error = -EINVAL; 6801da177e4SLinus Torvalds 6811da177e4SLinus Torvalds if (!S_ISREG(inode->i_mode) && !S_ISBLK(inode->i_mode)) 6821da177e4SLinus Torvalds goto out_putf; 6831da177e4SLinus Torvalds 6841da177e4SLinus Torvalds /* size of the new backing store needs to be the same */ 6851da177e4SLinus Torvalds if (get_loop_size(lo, file) != get_loop_size(lo, old_file)) 6861da177e4SLinus Torvalds goto out_putf; 6871da177e4SLinus Torvalds 6881da177e4SLinus Torvalds /* and ... switch */ 68943cade80SOmar Sandoval blk_mq_freeze_queue(lo->lo_queue); 69043cade80SOmar Sandoval mapping_set_gfp_mask(old_file->f_mapping, lo->old_gfp_mask); 69143cade80SOmar Sandoval lo->lo_backing_file = file; 69243cade80SOmar Sandoval lo->old_gfp_mask = mapping_gfp_mask(file->f_mapping); 69343cade80SOmar Sandoval mapping_set_gfp_mask(file->f_mapping, 69443cade80SOmar Sandoval lo->old_gfp_mask & ~(__GFP_IO|__GFP_FS)); 69543cade80SOmar Sandoval loop_update_dio(lo); 69643cade80SOmar Sandoval blk_mq_unfreeze_queue(lo->lo_queue); 6971da177e4SLinus Torvalds 6981da177e4SLinus Torvalds fput(old_file); 699e03c8dd1SKay Sievers if (lo->lo_flags & LO_FLAGS_PARTSCAN) 70006f0e9e6SMing Lei loop_reread_partitions(lo, bdev); 7011da177e4SLinus Torvalds return 0; 7021da177e4SLinus Torvalds 7031da177e4SLinus Torvalds out_putf: 7041da177e4SLinus Torvalds fput(file); 7051da177e4SLinus Torvalds out: 7061da177e4SLinus Torvalds return error; 7071da177e4SLinus Torvalds } 7081da177e4SLinus Torvalds 7091da177e4SLinus Torvalds static inline int is_loop_device(struct file *file) 7101da177e4SLinus Torvalds { 7111da177e4SLinus Torvalds struct inode *i = file->f_mapping->host; 7121da177e4SLinus Torvalds 7131da177e4SLinus Torvalds return i && S_ISBLK(i->i_mode) && MAJOR(i->i_rdev) == LOOP_MAJOR; 7141da177e4SLinus Torvalds } 7151da177e4SLinus Torvalds 716ee862730SMilan Broz /* loop sysfs attributes */ 717ee862730SMilan Broz 718ee862730SMilan Broz static ssize_t loop_attr_show(struct device *dev, char *page, 719ee862730SMilan Broz ssize_t (*callback)(struct loop_device *, char *)) 720ee862730SMilan Broz { 72134dd82afSKay Sievers struct gendisk *disk = dev_to_disk(dev); 72234dd82afSKay Sievers struct loop_device *lo = disk->private_data; 723ee862730SMilan Broz 72434dd82afSKay Sievers return callback(lo, page); 725ee862730SMilan Broz } 726ee862730SMilan Broz 727ee862730SMilan Broz #define LOOP_ATTR_RO(_name) \ 728ee862730SMilan Broz static ssize_t loop_attr_##_name##_show(struct loop_device *, char *); \ 729ee862730SMilan Broz static ssize_t loop_attr_do_show_##_name(struct device *d, \ 730ee862730SMilan Broz struct device_attribute *attr, char *b) \ 731ee862730SMilan Broz { \ 732ee862730SMilan Broz return loop_attr_show(d, b, loop_attr_##_name##_show); \ 733ee862730SMilan Broz } \ 734ee862730SMilan Broz static struct device_attribute loop_attr_##_name = \ 735ee862730SMilan Broz __ATTR(_name, S_IRUGO, loop_attr_do_show_##_name, NULL); 736ee862730SMilan Broz 737ee862730SMilan Broz static ssize_t loop_attr_backing_file_show(struct loop_device *lo, char *buf) 738ee862730SMilan Broz { 739ee862730SMilan Broz ssize_t ret; 740ee862730SMilan Broz char *p = NULL; 741ee862730SMilan Broz 74205eb0f25SKay Sievers spin_lock_irq(&lo->lo_lock); 743ee862730SMilan Broz if (lo->lo_backing_file) 7449bf39ab2SMiklos Szeredi p = file_path(lo->lo_backing_file, buf, PAGE_SIZE - 1); 74505eb0f25SKay Sievers spin_unlock_irq(&lo->lo_lock); 746ee862730SMilan Broz 747ee862730SMilan Broz if (IS_ERR_OR_NULL(p)) 748ee862730SMilan Broz ret = PTR_ERR(p); 749ee862730SMilan Broz else { 750ee862730SMilan Broz ret = strlen(p); 751ee862730SMilan Broz memmove(buf, p, ret); 752ee862730SMilan Broz buf[ret++] = '\n'; 753ee862730SMilan Broz buf[ret] = 0; 754ee862730SMilan Broz } 755ee862730SMilan Broz 756ee862730SMilan Broz return ret; 757ee862730SMilan Broz } 758ee862730SMilan Broz 759ee862730SMilan Broz static ssize_t loop_attr_offset_show(struct loop_device *lo, char *buf) 760ee862730SMilan Broz { 761ee862730SMilan Broz return sprintf(buf, "%llu\n", (unsigned long long)lo->lo_offset); 762ee862730SMilan Broz } 763ee862730SMilan Broz 764ee862730SMilan Broz static ssize_t loop_attr_sizelimit_show(struct loop_device *lo, char *buf) 765ee862730SMilan Broz { 766ee862730SMilan Broz return sprintf(buf, "%llu\n", (unsigned long long)lo->lo_sizelimit); 767ee862730SMilan Broz } 768ee862730SMilan Broz 769ee862730SMilan Broz static ssize_t loop_attr_autoclear_show(struct loop_device *lo, char *buf) 770ee862730SMilan Broz { 771ee862730SMilan Broz int autoclear = (lo->lo_flags & LO_FLAGS_AUTOCLEAR); 772ee862730SMilan Broz 773ee862730SMilan Broz return sprintf(buf, "%s\n", autoclear ? "1" : "0"); 774ee862730SMilan Broz } 775ee862730SMilan Broz 776e03c8dd1SKay Sievers static ssize_t loop_attr_partscan_show(struct loop_device *lo, char *buf) 777e03c8dd1SKay Sievers { 778e03c8dd1SKay Sievers int partscan = (lo->lo_flags & LO_FLAGS_PARTSCAN); 779e03c8dd1SKay Sievers 780e03c8dd1SKay Sievers return sprintf(buf, "%s\n", partscan ? "1" : "0"); 781e03c8dd1SKay Sievers } 782e03c8dd1SKay Sievers 7832e5ab5f3SMing Lei static ssize_t loop_attr_dio_show(struct loop_device *lo, char *buf) 7842e5ab5f3SMing Lei { 7852e5ab5f3SMing Lei int dio = (lo->lo_flags & LO_FLAGS_DIRECT_IO); 7862e5ab5f3SMing Lei 7872e5ab5f3SMing Lei return sprintf(buf, "%s\n", dio ? "1" : "0"); 7882e5ab5f3SMing Lei } 7892e5ab5f3SMing Lei 790ee862730SMilan Broz LOOP_ATTR_RO(backing_file); 791ee862730SMilan Broz LOOP_ATTR_RO(offset); 792ee862730SMilan Broz LOOP_ATTR_RO(sizelimit); 793ee862730SMilan Broz LOOP_ATTR_RO(autoclear); 794e03c8dd1SKay Sievers LOOP_ATTR_RO(partscan); 7952e5ab5f3SMing Lei LOOP_ATTR_RO(dio); 796ee862730SMilan Broz 797ee862730SMilan Broz static struct attribute *loop_attrs[] = { 798ee862730SMilan Broz &loop_attr_backing_file.attr, 799ee862730SMilan Broz &loop_attr_offset.attr, 800ee862730SMilan Broz &loop_attr_sizelimit.attr, 801ee862730SMilan Broz &loop_attr_autoclear.attr, 802e03c8dd1SKay Sievers &loop_attr_partscan.attr, 8032e5ab5f3SMing Lei &loop_attr_dio.attr, 804ee862730SMilan Broz NULL, 805ee862730SMilan Broz }; 806ee862730SMilan Broz 807ee862730SMilan Broz static struct attribute_group loop_attribute_group = { 808ee862730SMilan Broz .name = "loop", 809ee862730SMilan Broz .attrs= loop_attrs, 810ee862730SMilan Broz }; 811ee862730SMilan Broz 812ee862730SMilan Broz static int loop_sysfs_init(struct loop_device *lo) 813ee862730SMilan Broz { 814ee862730SMilan Broz return sysfs_create_group(&disk_to_dev(lo->lo_disk)->kobj, 815ee862730SMilan Broz &loop_attribute_group); 816ee862730SMilan Broz } 817ee862730SMilan Broz 818ee862730SMilan Broz static void loop_sysfs_exit(struct loop_device *lo) 819ee862730SMilan Broz { 820ee862730SMilan Broz sysfs_remove_group(&disk_to_dev(lo->lo_disk)->kobj, 821ee862730SMilan Broz &loop_attribute_group); 822ee862730SMilan Broz } 823ee862730SMilan Broz 824dfaa2ef6SLukas Czerner static void loop_config_discard(struct loop_device *lo) 825dfaa2ef6SLukas Czerner { 826dfaa2ef6SLukas Czerner struct file *file = lo->lo_backing_file; 827dfaa2ef6SLukas Czerner struct inode *inode = file->f_mapping->host; 828dfaa2ef6SLukas Czerner struct request_queue *q = lo->lo_queue; 829dfaa2ef6SLukas Czerner 830dfaa2ef6SLukas Czerner /* 831dfaa2ef6SLukas Czerner * We use punch hole to reclaim the free space used by the 83212a64d2fSOlaf Hering * image a.k.a. discard. However we do not support discard if 833dfaa2ef6SLukas Czerner * encryption is enabled, because it may give an attacker 834dfaa2ef6SLukas Czerner * useful information. 835dfaa2ef6SLukas Czerner */ 836dfaa2ef6SLukas Czerner if ((!file->f_op->fallocate) || 837dfaa2ef6SLukas Czerner lo->lo_encrypt_key_size) { 838dfaa2ef6SLukas Czerner q->limits.discard_granularity = 0; 839dfaa2ef6SLukas Czerner q->limits.discard_alignment = 0; 8402bb4cd5cSJens Axboe blk_queue_max_discard_sectors(q, 0); 84119372e27SChristoph Hellwig blk_queue_max_write_zeroes_sectors(q, 0); 8428b904b5bSBart Van Assche blk_queue_flag_clear(QUEUE_FLAG_DISCARD, q); 843dfaa2ef6SLukas Czerner return; 844dfaa2ef6SLukas Czerner } 845dfaa2ef6SLukas Czerner 846dfaa2ef6SLukas Czerner q->limits.discard_granularity = inode->i_sb->s_blocksize; 847dfaf3c03SLukas Czerner q->limits.discard_alignment = 0; 848f2c6df7dSHannes Reinecke 8491e6ec9eaSOmar Sandoval blk_queue_max_discard_sectors(q, UINT_MAX >> 9); 8501e6ec9eaSOmar Sandoval blk_queue_max_write_zeroes_sectors(q, UINT_MAX >> 9); 8518b904b5bSBart Van Assche blk_queue_flag_set(QUEUE_FLAG_DISCARD, q); 852dfaa2ef6SLukas Czerner } 853dfaa2ef6SLukas Czerner 854e03a3d7aSMing Lei static void loop_unprepare_queue(struct loop_device *lo) 855e03a3d7aSMing Lei { 8563989144fSPetr Mladek kthread_flush_worker(&lo->worker); 857e03a3d7aSMing Lei kthread_stop(lo->worker_task); 858e03a3d7aSMing Lei } 859e03a3d7aSMing Lei 860b2ee7d46SNeilBrown static int loop_kthread_worker_fn(void *worker_ptr) 861b2ee7d46SNeilBrown { 862b2ee7d46SNeilBrown current->flags |= PF_LESS_THROTTLE; 863b2ee7d46SNeilBrown return kthread_worker_fn(worker_ptr); 864b2ee7d46SNeilBrown } 865b2ee7d46SNeilBrown 866e03a3d7aSMing Lei static int loop_prepare_queue(struct loop_device *lo) 867e03a3d7aSMing Lei { 8683989144fSPetr Mladek kthread_init_worker(&lo->worker); 869b2ee7d46SNeilBrown lo->worker_task = kthread_run(loop_kthread_worker_fn, 870e03a3d7aSMing Lei &lo->worker, "loop%d", lo->lo_number); 871e03a3d7aSMing Lei if (IS_ERR(lo->worker_task)) 872e03a3d7aSMing Lei return -ENOMEM; 873e03a3d7aSMing Lei set_user_nice(lo->worker_task, MIN_NICE); 874e03a3d7aSMing Lei return 0; 875e03a3d7aSMing Lei } 876e03a3d7aSMing Lei 877bb214884SAl Viro static int loop_set_fd(struct loop_device *lo, fmode_t mode, 8781da177e4SLinus Torvalds struct block_device *bdev, unsigned int arg) 8791da177e4SLinus Torvalds { 8801da177e4SLinus Torvalds struct file *file, *f; 8811da177e4SLinus Torvalds struct inode *inode; 8821da177e4SLinus Torvalds struct address_space *mapping; 8831da177e4SLinus Torvalds int lo_flags = 0; 8841da177e4SLinus Torvalds int error; 8851da177e4SLinus Torvalds loff_t size; 8861da177e4SLinus Torvalds 8871da177e4SLinus Torvalds /* This is safe, since we have a reference from open(). */ 8881da177e4SLinus Torvalds __module_get(THIS_MODULE); 8891da177e4SLinus Torvalds 8901da177e4SLinus Torvalds error = -EBADF; 8911da177e4SLinus Torvalds file = fget(arg); 8921da177e4SLinus Torvalds if (!file) 8931da177e4SLinus Torvalds goto out; 8941da177e4SLinus Torvalds 8951da177e4SLinus Torvalds error = -EBUSY; 8961da177e4SLinus Torvalds if (lo->lo_state != Lo_unbound) 8971da177e4SLinus Torvalds goto out_putf; 8981da177e4SLinus Torvalds 8991da177e4SLinus Torvalds /* Avoid recursion */ 9001da177e4SLinus Torvalds f = file; 9011da177e4SLinus Torvalds while (is_loop_device(f)) { 9021da177e4SLinus Torvalds struct loop_device *l; 9031da177e4SLinus Torvalds 904bb214884SAl Viro if (f->f_mapping->host->i_bdev == bdev) 9051da177e4SLinus Torvalds goto out_putf; 9061da177e4SLinus Torvalds 9071da177e4SLinus Torvalds l = f->f_mapping->host->i_bdev->bd_disk->private_data; 9081da177e4SLinus Torvalds if (l->lo_state == Lo_unbound) { 9091da177e4SLinus Torvalds error = -EINVAL; 9101da177e4SLinus Torvalds goto out_putf; 9111da177e4SLinus Torvalds } 9121da177e4SLinus Torvalds f = l->lo_backing_file; 9131da177e4SLinus Torvalds } 9141da177e4SLinus Torvalds 9151da177e4SLinus Torvalds mapping = file->f_mapping; 9161da177e4SLinus Torvalds inode = mapping->host; 9171da177e4SLinus Torvalds 9181da177e4SLinus Torvalds error = -EINVAL; 919456be148SChristoph Hellwig if (!S_ISREG(inode->i_mode) && !S_ISBLK(inode->i_mode)) 920456be148SChristoph Hellwig goto out_putf; 9216818173bSMiklos Szeredi 922456be148SChristoph Hellwig if (!(file->f_mode & FMODE_WRITE) || !(mode & FMODE_WRITE) || 923283e7e5dSAl Viro !file->f_op->write_iter) 9241da177e4SLinus Torvalds lo_flags |= LO_FLAGS_READ_ONLY; 9251da177e4SLinus Torvalds 9261da177e4SLinus Torvalds error = -EFBIG; 927456be148SChristoph Hellwig size = get_loop_size(lo, file); 928456be148SChristoph Hellwig if ((loff_t)(sector_t)size != size) 9291da177e4SLinus Torvalds goto out_putf; 930e03a3d7aSMing Lei error = loop_prepare_queue(lo); 931e03a3d7aSMing Lei if (error) 932f4aa4c7bSMing Lei goto out_putf; 9331da177e4SLinus Torvalds 934456be148SChristoph Hellwig error = 0; 9351da177e4SLinus Torvalds 9361da177e4SLinus Torvalds set_device_ro(bdev, (lo_flags & LO_FLAGS_READ_ONLY) != 0); 9371da177e4SLinus Torvalds 9382e5ab5f3SMing Lei lo->use_dio = false; 9391da177e4SLinus Torvalds lo->lo_device = bdev; 9401da177e4SLinus Torvalds lo->lo_flags = lo_flags; 9411da177e4SLinus Torvalds lo->lo_backing_file = file; 942aa4d8616SChristoph Hellwig lo->transfer = NULL; 9431da177e4SLinus Torvalds lo->ioctl = NULL; 9441da177e4SLinus Torvalds lo->lo_sizelimit = 0; 9451da177e4SLinus Torvalds lo->old_gfp_mask = mapping_gfp_mask(mapping); 9461da177e4SLinus Torvalds mapping_set_gfp_mask(mapping, lo->old_gfp_mask & ~(__GFP_IO|__GFP_FS)); 9471da177e4SLinus Torvalds 94868db1961SNikanth Karthikesan if (!(lo_flags & LO_FLAGS_READ_ONLY) && file->f_op->fsync) 94921d0727fSJens Axboe blk_queue_write_cache(lo->lo_queue, true, false); 95068db1961SNikanth Karthikesan 9512e5ab5f3SMing Lei loop_update_dio(lo); 95273285082SKen Chen set_capacity(lo->lo_disk, size); 9531da177e4SLinus Torvalds bd_set_size(bdev, size << 9); 954ee862730SMilan Broz loop_sysfs_init(lo); 955c3473c63SDavid Zeuthen /* let user-space know about the new size */ 956c3473c63SDavid Zeuthen kobject_uevent(&disk_to_dev(bdev->bd_disk)->kobj, KOBJ_CHANGE); 9571da177e4SLinus Torvalds 9588a0740c4SOmar Sandoval set_blocksize(bdev, S_ISBLK(inode->i_mode) ? 9598a0740c4SOmar Sandoval block_size(inode->i_bdev) : PAGE_SIZE); 9601da177e4SLinus Torvalds 9616c997918SSerge E. Hallyn lo->lo_state = Lo_bound; 962e03c8dd1SKay Sievers if (part_shift) 963e03c8dd1SKay Sievers lo->lo_flags |= LO_FLAGS_PARTSCAN; 964e03c8dd1SKay Sievers if (lo->lo_flags & LO_FLAGS_PARTSCAN) 96506f0e9e6SMing Lei loop_reread_partitions(lo, bdev); 966c1681bf8SAnatol Pomozov 967c1681bf8SAnatol Pomozov /* Grab the block_device to prevent its destruction after we 968c1681bf8SAnatol Pomozov * put /dev/loopXX inode. Later in loop_clr_fd() we bdput(bdev). 969c1681bf8SAnatol Pomozov */ 970c1681bf8SAnatol Pomozov bdgrab(bdev); 9711da177e4SLinus Torvalds return 0; 9721da177e4SLinus Torvalds 9731da177e4SLinus Torvalds out_putf: 9741da177e4SLinus Torvalds fput(file); 9751da177e4SLinus Torvalds out: 9761da177e4SLinus Torvalds /* This is safe: open() is still holding a reference. */ 9771da177e4SLinus Torvalds module_put(THIS_MODULE); 9781da177e4SLinus Torvalds return error; 9791da177e4SLinus Torvalds } 9801da177e4SLinus Torvalds 9811da177e4SLinus Torvalds static int 9821da177e4SLinus Torvalds loop_release_xfer(struct loop_device *lo) 9831da177e4SLinus Torvalds { 9841da177e4SLinus Torvalds int err = 0; 9851da177e4SLinus Torvalds struct loop_func_table *xfer = lo->lo_encryption; 9861da177e4SLinus Torvalds 9871da177e4SLinus Torvalds if (xfer) { 9881da177e4SLinus Torvalds if (xfer->release) 9891da177e4SLinus Torvalds err = xfer->release(lo); 9901da177e4SLinus Torvalds lo->transfer = NULL; 9911da177e4SLinus Torvalds lo->lo_encryption = NULL; 9921da177e4SLinus Torvalds module_put(xfer->owner); 9931da177e4SLinus Torvalds } 9941da177e4SLinus Torvalds return err; 9951da177e4SLinus Torvalds } 9961da177e4SLinus Torvalds 9971da177e4SLinus Torvalds static int 9981da177e4SLinus Torvalds loop_init_xfer(struct loop_device *lo, struct loop_func_table *xfer, 9991da177e4SLinus Torvalds const struct loop_info64 *i) 10001da177e4SLinus Torvalds { 10011da177e4SLinus Torvalds int err = 0; 10021da177e4SLinus Torvalds 10031da177e4SLinus Torvalds if (xfer) { 10041da177e4SLinus Torvalds struct module *owner = xfer->owner; 10051da177e4SLinus Torvalds 10061da177e4SLinus Torvalds if (!try_module_get(owner)) 10071da177e4SLinus Torvalds return -EINVAL; 10081da177e4SLinus Torvalds if (xfer->init) 10091da177e4SLinus Torvalds err = xfer->init(lo, i); 10101da177e4SLinus Torvalds if (err) 10111da177e4SLinus Torvalds module_put(owner); 10121da177e4SLinus Torvalds else 10131da177e4SLinus Torvalds lo->lo_encryption = xfer; 10141da177e4SLinus Torvalds } 10151da177e4SLinus Torvalds return err; 10161da177e4SLinus Torvalds } 10171da177e4SLinus Torvalds 10184c823cc3SAyan George static int loop_clr_fd(struct loop_device *lo) 10191da177e4SLinus Torvalds { 10201da177e4SLinus Torvalds struct file *filp = lo->lo_backing_file; 1021b4e3ca1aSAl Viro gfp_t gfp = lo->old_gfp_mask; 10224c823cc3SAyan George struct block_device *bdev = lo->lo_device; 10231da177e4SLinus Torvalds 10241da177e4SLinus Torvalds if (lo->lo_state != Lo_bound) 10251da177e4SLinus Torvalds return -ENXIO; 10261da177e4SLinus Torvalds 1027a1ecac3bSDave Chinner /* 1028a1ecac3bSDave Chinner * If we've explicitly asked to tear down the loop device, 1029a1ecac3bSDave Chinner * and it has an elevated reference count, set it for auto-teardown when 1030a1ecac3bSDave Chinner * the last reference goes away. This stops $!~#$@ udev from 1031a1ecac3bSDave Chinner * preventing teardown because it decided that it needs to run blkid on 1032a1ecac3bSDave Chinner * the loopback device whenever they appear. xfstests is notorious for 1033a1ecac3bSDave Chinner * failing tests because blkid via udev races with a losetup 1034a1ecac3bSDave Chinner * <dev>/do something like mkfs/losetup -d <dev> causing the losetup -d 1035a1ecac3bSDave Chinner * command to fail with EBUSY. 1036a1ecac3bSDave Chinner */ 1037f8933667SMing Lei if (atomic_read(&lo->lo_refcnt) > 1) { 1038a1ecac3bSDave Chinner lo->lo_flags |= LO_FLAGS_AUTOCLEAR; 1039a1ecac3bSDave Chinner mutex_unlock(&lo->lo_ctl_mutex); 1040a1ecac3bSDave Chinner return 0; 1041a1ecac3bSDave Chinner } 10421da177e4SLinus Torvalds 10431da177e4SLinus Torvalds if (filp == NULL) 10441da177e4SLinus Torvalds return -EINVAL; 10451da177e4SLinus Torvalds 1046f8933667SMing Lei /* freeze request queue during the transition */ 1047f8933667SMing Lei blk_mq_freeze_queue(lo->lo_queue); 1048f8933667SMing Lei 10491da177e4SLinus Torvalds spin_lock_irq(&lo->lo_lock); 10501da177e4SLinus Torvalds lo->lo_state = Lo_rundown; 10511da177e4SLinus Torvalds lo->lo_backing_file = NULL; 105205eb0f25SKay Sievers spin_unlock_irq(&lo->lo_lock); 10531da177e4SLinus Torvalds 10541da177e4SLinus Torvalds loop_release_xfer(lo); 10551da177e4SLinus Torvalds lo->transfer = NULL; 10561da177e4SLinus Torvalds lo->ioctl = NULL; 10571da177e4SLinus Torvalds lo->lo_device = NULL; 10581da177e4SLinus Torvalds lo->lo_encryption = NULL; 10591da177e4SLinus Torvalds lo->lo_offset = 0; 10601da177e4SLinus Torvalds lo->lo_sizelimit = 0; 10611da177e4SLinus Torvalds lo->lo_encrypt_key_size = 0; 10621da177e4SLinus Torvalds memset(lo->lo_encrypt_key, 0, LO_KEY_SIZE); 10631da177e4SLinus Torvalds memset(lo->lo_crypt_name, 0, LO_NAME_SIZE); 10641da177e4SLinus Torvalds memset(lo->lo_file_name, 0, LO_NAME_SIZE); 106589e4fdecSOmar Sandoval blk_queue_logical_block_size(lo->lo_queue, 512); 1066bf093753SOmar Sandoval blk_queue_physical_block_size(lo->lo_queue, 512); 1067bf093753SOmar Sandoval blk_queue_io_min(lo->lo_queue, 512); 1068c1681bf8SAnatol Pomozov if (bdev) { 1069c1681bf8SAnatol Pomozov bdput(bdev); 1070f98393a6SPeter Zijlstra invalidate_bdev(bdev); 1071*eedffa28SJeff Layton bdev->bd_inode->i_mapping->wb_err = 0; 1072c1681bf8SAnatol Pomozov } 107373285082SKen Chen set_capacity(lo->lo_disk, 0); 107451a0bb0cSMilan Broz loop_sysfs_exit(lo); 1075c3473c63SDavid Zeuthen if (bdev) { 10761da177e4SLinus Torvalds bd_set_size(bdev, 0); 1077c3473c63SDavid Zeuthen /* let user-space know about this change */ 1078c3473c63SDavid Zeuthen kobject_uevent(&disk_to_dev(bdev->bd_disk)->kobj, KOBJ_CHANGE); 1079c3473c63SDavid Zeuthen } 10801da177e4SLinus Torvalds mapping_set_gfp_mask(filp->f_mapping, gfp); 10811da177e4SLinus Torvalds lo->lo_state = Lo_unbound; 10821da177e4SLinus Torvalds /* This is safe: open() is still holding a reference. */ 10831da177e4SLinus Torvalds module_put(THIS_MODULE); 1084f8933667SMing Lei blk_mq_unfreeze_queue(lo->lo_queue); 1085f8933667SMing Lei 1086c2fccc1cSJens Axboe if (lo->lo_flags & LO_FLAGS_PARTSCAN && bdev) 108706f0e9e6SMing Lei loop_reread_partitions(lo, bdev); 1088e03c8dd1SKay Sievers lo->lo_flags = 0; 1089e03c8dd1SKay Sievers if (!part_shift) 1090e03c8dd1SKay Sievers lo->lo_disk->flags |= GENHD_FL_NO_PART_SCAN; 1091e03a3d7aSMing Lei loop_unprepare_queue(lo); 1092f028f3b2SNikanth Karthikesan mutex_unlock(&lo->lo_ctl_mutex); 1093f028f3b2SNikanth Karthikesan /* 1094f028f3b2SNikanth Karthikesan * Need not hold lo_ctl_mutex to fput backing file. 1095f028f3b2SNikanth Karthikesan * Calling fput holding lo_ctl_mutex triggers a circular 1096f028f3b2SNikanth Karthikesan * lock dependency possibility warning as fput can take 1097f028f3b2SNikanth Karthikesan * bd_mutex which is usually taken before lo_ctl_mutex. 1098f028f3b2SNikanth Karthikesan */ 1099f028f3b2SNikanth Karthikesan fput(filp); 11001da177e4SLinus Torvalds return 0; 11011da177e4SLinus Torvalds } 11021da177e4SLinus Torvalds 11031da177e4SLinus Torvalds static int 11041da177e4SLinus Torvalds loop_set_status(struct loop_device *lo, const struct loop_info64 *info) 11051da177e4SLinus Torvalds { 11061da177e4SLinus Torvalds int err; 11071da177e4SLinus Torvalds struct loop_func_table *xfer; 1108e4849737SEric W. Biederman kuid_t uid = current_uid(); 11091da177e4SLinus Torvalds 1110b0fafa81SDavid Howells if (lo->lo_encrypt_key_size && 1111e4849737SEric W. Biederman !uid_eq(lo->lo_key_owner, uid) && 11121da177e4SLinus Torvalds !capable(CAP_SYS_ADMIN)) 11131da177e4SLinus Torvalds return -EPERM; 11141da177e4SLinus Torvalds if (lo->lo_state != Lo_bound) 11151da177e4SLinus Torvalds return -ENXIO; 11161da177e4SLinus Torvalds if ((unsigned int) info->lo_encrypt_key_size > LO_KEY_SIZE) 11171da177e4SLinus Torvalds return -EINVAL; 11181da177e4SLinus Torvalds 1119ecdd0959SMing Lei /* I/O need to be drained during transfer transition */ 1120ecdd0959SMing Lei blk_mq_freeze_queue(lo->lo_queue); 1121ecdd0959SMing Lei 11221da177e4SLinus Torvalds err = loop_release_xfer(lo); 11231da177e4SLinus Torvalds if (err) 1124ecdd0959SMing Lei goto exit; 11251da177e4SLinus Torvalds 11261da177e4SLinus Torvalds if (info->lo_encrypt_type) { 11271da177e4SLinus Torvalds unsigned int type = info->lo_encrypt_type; 11281da177e4SLinus Torvalds 11291e047eaaSTetsuo Handa if (type >= MAX_LO_CRYPT) { 11301e047eaaSTetsuo Handa err = -EINVAL; 11311e047eaaSTetsuo Handa goto exit; 11321e047eaaSTetsuo Handa } 11331da177e4SLinus Torvalds xfer = xfer_funcs[type]; 11341e047eaaSTetsuo Handa if (xfer == NULL) { 11351e047eaaSTetsuo Handa err = -EINVAL; 11361e047eaaSTetsuo Handa goto exit; 11371e047eaaSTetsuo Handa } 11381da177e4SLinus Torvalds } else 11391da177e4SLinus Torvalds xfer = NULL; 11401da177e4SLinus Torvalds 11411da177e4SLinus Torvalds err = loop_init_xfer(lo, xfer, info); 11421da177e4SLinus Torvalds if (err) 1143ecdd0959SMing Lei goto exit; 11441da177e4SLinus Torvalds 11451da177e4SLinus Torvalds if (lo->lo_offset != info->lo_offset || 11461e6ec9eaSOmar Sandoval lo->lo_sizelimit != info->lo_sizelimit) { 11471e6ec9eaSOmar Sandoval if (figure_loop_size(lo, info->lo_offset, info->lo_sizelimit)) { 1148ecdd0959SMing Lei err = -EFBIG; 1149ecdd0959SMing Lei goto exit; 1150ecdd0959SMing Lei } 1151b040ad9cSArnd Bergmann } 1152541c742aSGuo Chao 1153dfaa2ef6SLukas Czerner loop_config_discard(lo); 11541da177e4SLinus Torvalds 11551da177e4SLinus Torvalds memcpy(lo->lo_file_name, info->lo_file_name, LO_NAME_SIZE); 11561da177e4SLinus Torvalds memcpy(lo->lo_crypt_name, info->lo_crypt_name, LO_NAME_SIZE); 11571da177e4SLinus Torvalds lo->lo_file_name[LO_NAME_SIZE-1] = 0; 11581da177e4SLinus Torvalds lo->lo_crypt_name[LO_NAME_SIZE-1] = 0; 11591da177e4SLinus Torvalds 11601da177e4SLinus Torvalds if (!xfer) 11611da177e4SLinus Torvalds xfer = &none_funcs; 11621da177e4SLinus Torvalds lo->transfer = xfer->transfer; 11631da177e4SLinus Torvalds lo->ioctl = xfer->ioctl; 11641da177e4SLinus Torvalds 116596c58655SDavid Woodhouse if ((lo->lo_flags & LO_FLAGS_AUTOCLEAR) != 116696c58655SDavid Woodhouse (info->lo_flags & LO_FLAGS_AUTOCLEAR)) 116796c58655SDavid Woodhouse lo->lo_flags ^= LO_FLAGS_AUTOCLEAR; 116896c58655SDavid Woodhouse 11691da177e4SLinus Torvalds lo->lo_encrypt_key_size = info->lo_encrypt_key_size; 11701da177e4SLinus Torvalds lo->lo_init[0] = info->lo_init[0]; 11711da177e4SLinus Torvalds lo->lo_init[1] = info->lo_init[1]; 11721da177e4SLinus Torvalds if (info->lo_encrypt_key_size) { 11731da177e4SLinus Torvalds memcpy(lo->lo_encrypt_key, info->lo_encrypt_key, 11741da177e4SLinus Torvalds info->lo_encrypt_key_size); 1175b0fafa81SDavid Howells lo->lo_key_owner = uid; 11761da177e4SLinus Torvalds } 11771da177e4SLinus Torvalds 11782e5ab5f3SMing Lei /* update dio if lo_offset or transfer is changed */ 11792e5ab5f3SMing Lei __loop_update_dio(lo, lo->use_dio); 11802e5ab5f3SMing Lei 1181ecdd0959SMing Lei exit: 1182ecdd0959SMing Lei blk_mq_unfreeze_queue(lo->lo_queue); 1183e02898b4SOmar Sandoval 1184e02898b4SOmar Sandoval if (!err && (info->lo_flags & LO_FLAGS_PARTSCAN) && 1185e02898b4SOmar Sandoval !(lo->lo_flags & LO_FLAGS_PARTSCAN)) { 1186e02898b4SOmar Sandoval lo->lo_flags |= LO_FLAGS_PARTSCAN; 1187e02898b4SOmar Sandoval lo->lo_disk->flags &= ~GENHD_FL_NO_PART_SCAN; 1188e02898b4SOmar Sandoval loop_reread_partitions(lo, lo->lo_device); 1189e02898b4SOmar Sandoval } 1190e02898b4SOmar Sandoval 1191ecdd0959SMing Lei return err; 11921da177e4SLinus Torvalds } 11931da177e4SLinus Torvalds 11941da177e4SLinus Torvalds static int 11951da177e4SLinus Torvalds loop_get_status(struct loop_device *lo, struct loop_info64 *info) 11961da177e4SLinus Torvalds { 11972d1d4c1eSOmar Sandoval struct file *file; 11981da177e4SLinus Torvalds struct kstat stat; 11992d1d4c1eSOmar Sandoval int ret; 12001da177e4SLinus Torvalds 12012d1d4c1eSOmar Sandoval if (lo->lo_state != Lo_bound) { 12022d1d4c1eSOmar Sandoval mutex_unlock(&lo->lo_ctl_mutex); 12031da177e4SLinus Torvalds return -ENXIO; 12042d1d4c1eSOmar Sandoval } 12052d1d4c1eSOmar Sandoval 12061da177e4SLinus Torvalds memset(info, 0, sizeof(*info)); 12071da177e4SLinus Torvalds info->lo_number = lo->lo_number; 12081da177e4SLinus Torvalds info->lo_offset = lo->lo_offset; 12091da177e4SLinus Torvalds info->lo_sizelimit = lo->lo_sizelimit; 12101da177e4SLinus Torvalds info->lo_flags = lo->lo_flags; 12111da177e4SLinus Torvalds memcpy(info->lo_file_name, lo->lo_file_name, LO_NAME_SIZE); 12121da177e4SLinus Torvalds memcpy(info->lo_crypt_name, lo->lo_crypt_name, LO_NAME_SIZE); 12131da177e4SLinus Torvalds info->lo_encrypt_type = 12141da177e4SLinus Torvalds lo->lo_encryption ? lo->lo_encryption->number : 0; 12151da177e4SLinus Torvalds if (lo->lo_encrypt_key_size && capable(CAP_SYS_ADMIN)) { 12161da177e4SLinus Torvalds info->lo_encrypt_key_size = lo->lo_encrypt_key_size; 12171da177e4SLinus Torvalds memcpy(info->lo_encrypt_key, lo->lo_encrypt_key, 12181da177e4SLinus Torvalds lo->lo_encrypt_key_size); 12191da177e4SLinus Torvalds } 12202d1d4c1eSOmar Sandoval 12212d1d4c1eSOmar Sandoval /* Drop lo_ctl_mutex while we call into the filesystem. */ 12222d1d4c1eSOmar Sandoval file = get_file(lo->lo_backing_file); 12232d1d4c1eSOmar Sandoval mutex_unlock(&lo->lo_ctl_mutex); 12242d1d4c1eSOmar Sandoval ret = vfs_getattr(&file->f_path, &stat, STATX_INO, 12252d1d4c1eSOmar Sandoval AT_STATX_SYNC_AS_STAT); 12262d1d4c1eSOmar Sandoval if (!ret) { 12272d1d4c1eSOmar Sandoval info->lo_device = huge_encode_dev(stat.dev); 12282d1d4c1eSOmar Sandoval info->lo_inode = stat.ino; 12292d1d4c1eSOmar Sandoval info->lo_rdevice = huge_encode_dev(stat.rdev); 12302d1d4c1eSOmar Sandoval } 12312d1d4c1eSOmar Sandoval fput(file); 12322d1d4c1eSOmar Sandoval return ret; 12331da177e4SLinus Torvalds } 12341da177e4SLinus Torvalds 12351da177e4SLinus Torvalds static void 12361da177e4SLinus Torvalds loop_info64_from_old(const struct loop_info *info, struct loop_info64 *info64) 12371da177e4SLinus Torvalds { 12381da177e4SLinus Torvalds memset(info64, 0, sizeof(*info64)); 12391da177e4SLinus Torvalds info64->lo_number = info->lo_number; 12401da177e4SLinus Torvalds info64->lo_device = info->lo_device; 12411da177e4SLinus Torvalds info64->lo_inode = info->lo_inode; 12421da177e4SLinus Torvalds info64->lo_rdevice = info->lo_rdevice; 12431da177e4SLinus Torvalds info64->lo_offset = info->lo_offset; 12441da177e4SLinus Torvalds info64->lo_sizelimit = 0; 12451da177e4SLinus Torvalds info64->lo_encrypt_type = info->lo_encrypt_type; 12461da177e4SLinus Torvalds info64->lo_encrypt_key_size = info->lo_encrypt_key_size; 12471da177e4SLinus Torvalds info64->lo_flags = info->lo_flags; 12481da177e4SLinus Torvalds info64->lo_init[0] = info->lo_init[0]; 12491da177e4SLinus Torvalds info64->lo_init[1] = info->lo_init[1]; 12501da177e4SLinus Torvalds if (info->lo_encrypt_type == LO_CRYPT_CRYPTOAPI) 12511da177e4SLinus Torvalds memcpy(info64->lo_crypt_name, info->lo_name, LO_NAME_SIZE); 12521da177e4SLinus Torvalds else 12531da177e4SLinus Torvalds memcpy(info64->lo_file_name, info->lo_name, LO_NAME_SIZE); 12541da177e4SLinus Torvalds memcpy(info64->lo_encrypt_key, info->lo_encrypt_key, LO_KEY_SIZE); 12551da177e4SLinus Torvalds } 12561da177e4SLinus Torvalds 12571da177e4SLinus Torvalds static int 12581da177e4SLinus Torvalds loop_info64_to_old(const struct loop_info64 *info64, struct loop_info *info) 12591da177e4SLinus Torvalds { 12601da177e4SLinus Torvalds memset(info, 0, sizeof(*info)); 12611da177e4SLinus Torvalds info->lo_number = info64->lo_number; 12621da177e4SLinus Torvalds info->lo_device = info64->lo_device; 12631da177e4SLinus Torvalds info->lo_inode = info64->lo_inode; 12641da177e4SLinus Torvalds info->lo_rdevice = info64->lo_rdevice; 12651da177e4SLinus Torvalds info->lo_offset = info64->lo_offset; 12661da177e4SLinus Torvalds info->lo_encrypt_type = info64->lo_encrypt_type; 12671da177e4SLinus Torvalds info->lo_encrypt_key_size = info64->lo_encrypt_key_size; 12681da177e4SLinus Torvalds info->lo_flags = info64->lo_flags; 12691da177e4SLinus Torvalds info->lo_init[0] = info64->lo_init[0]; 12701da177e4SLinus Torvalds info->lo_init[1] = info64->lo_init[1]; 12711da177e4SLinus Torvalds if (info->lo_encrypt_type == LO_CRYPT_CRYPTOAPI) 12721da177e4SLinus Torvalds memcpy(info->lo_name, info64->lo_crypt_name, LO_NAME_SIZE); 12731da177e4SLinus Torvalds else 12741da177e4SLinus Torvalds memcpy(info->lo_name, info64->lo_file_name, LO_NAME_SIZE); 12751da177e4SLinus Torvalds memcpy(info->lo_encrypt_key, info64->lo_encrypt_key, LO_KEY_SIZE); 12761da177e4SLinus Torvalds 12771da177e4SLinus Torvalds /* error in case values were truncated */ 12781da177e4SLinus Torvalds if (info->lo_device != info64->lo_device || 12791da177e4SLinus Torvalds info->lo_rdevice != info64->lo_rdevice || 12801da177e4SLinus Torvalds info->lo_inode != info64->lo_inode || 12811da177e4SLinus Torvalds info->lo_offset != info64->lo_offset) 12821da177e4SLinus Torvalds return -EOVERFLOW; 12831da177e4SLinus Torvalds 12841da177e4SLinus Torvalds return 0; 12851da177e4SLinus Torvalds } 12861da177e4SLinus Torvalds 12871da177e4SLinus Torvalds static int 12881da177e4SLinus Torvalds loop_set_status_old(struct loop_device *lo, const struct loop_info __user *arg) 12891da177e4SLinus Torvalds { 12901da177e4SLinus Torvalds struct loop_info info; 12911da177e4SLinus Torvalds struct loop_info64 info64; 12921da177e4SLinus Torvalds 12931da177e4SLinus Torvalds if (copy_from_user(&info, arg, sizeof (struct loop_info))) 12941da177e4SLinus Torvalds return -EFAULT; 12951da177e4SLinus Torvalds loop_info64_from_old(&info, &info64); 12961da177e4SLinus Torvalds return loop_set_status(lo, &info64); 12971da177e4SLinus Torvalds } 12981da177e4SLinus Torvalds 12991da177e4SLinus Torvalds static int 13001da177e4SLinus Torvalds loop_set_status64(struct loop_device *lo, const struct loop_info64 __user *arg) 13011da177e4SLinus Torvalds { 13021da177e4SLinus Torvalds struct loop_info64 info64; 13031da177e4SLinus Torvalds 13041da177e4SLinus Torvalds if (copy_from_user(&info64, arg, sizeof (struct loop_info64))) 13051da177e4SLinus Torvalds return -EFAULT; 13061da177e4SLinus Torvalds return loop_set_status(lo, &info64); 13071da177e4SLinus Torvalds } 13081da177e4SLinus Torvalds 13091da177e4SLinus Torvalds static int 13101da177e4SLinus Torvalds loop_get_status_old(struct loop_device *lo, struct loop_info __user *arg) { 13111da177e4SLinus Torvalds struct loop_info info; 13121da177e4SLinus Torvalds struct loop_info64 info64; 1313bdac616dSOmar Sandoval int err; 13141da177e4SLinus Torvalds 1315bdac616dSOmar Sandoval if (!arg) { 1316bdac616dSOmar Sandoval mutex_unlock(&lo->lo_ctl_mutex); 1317bdac616dSOmar Sandoval return -EINVAL; 1318bdac616dSOmar Sandoval } 13191da177e4SLinus Torvalds err = loop_get_status(lo, &info64); 13201da177e4SLinus Torvalds if (!err) 13211da177e4SLinus Torvalds err = loop_info64_to_old(&info64, &info); 13221da177e4SLinus Torvalds if (!err && copy_to_user(arg, &info, sizeof(info))) 13231da177e4SLinus Torvalds err = -EFAULT; 13241da177e4SLinus Torvalds 13251da177e4SLinus Torvalds return err; 13261da177e4SLinus Torvalds } 13271da177e4SLinus Torvalds 13281da177e4SLinus Torvalds static int 13291da177e4SLinus Torvalds loop_get_status64(struct loop_device *lo, struct loop_info64 __user *arg) { 13301da177e4SLinus Torvalds struct loop_info64 info64; 1331bdac616dSOmar Sandoval int err; 13321da177e4SLinus Torvalds 1333bdac616dSOmar Sandoval if (!arg) { 1334bdac616dSOmar Sandoval mutex_unlock(&lo->lo_ctl_mutex); 1335bdac616dSOmar Sandoval return -EINVAL; 1336bdac616dSOmar Sandoval } 13371da177e4SLinus Torvalds err = loop_get_status(lo, &info64); 13381da177e4SLinus Torvalds if (!err && copy_to_user(arg, &info64, sizeof(info64))) 13391da177e4SLinus Torvalds err = -EFAULT; 13401da177e4SLinus Torvalds 13411da177e4SLinus Torvalds return err; 13421da177e4SLinus Torvalds } 13431da177e4SLinus Torvalds 134451001b7dSHannes Reinecke static int loop_set_capacity(struct loop_device *lo) 134553d66608SJ. R. Okajima { 134653d66608SJ. R. Okajima if (unlikely(lo->lo_state != Lo_bound)) 13477b0576a3SGuo Chao return -ENXIO; 134853d66608SJ. R. Okajima 13491e6ec9eaSOmar Sandoval return figure_loop_size(lo, lo->lo_offset, lo->lo_sizelimit); 135053d66608SJ. R. Okajima } 135153d66608SJ. R. Okajima 1352ab1cb278SMing Lei static int loop_set_dio(struct loop_device *lo, unsigned long arg) 1353ab1cb278SMing Lei { 1354ab1cb278SMing Lei int error = -ENXIO; 1355ab1cb278SMing Lei if (lo->lo_state != Lo_bound) 1356ab1cb278SMing Lei goto out; 1357ab1cb278SMing Lei 1358ab1cb278SMing Lei __loop_update_dio(lo, !!arg); 1359ab1cb278SMing Lei if (lo->use_dio == !!arg) 1360ab1cb278SMing Lei return 0; 1361ab1cb278SMing Lei error = -EINVAL; 1362ab1cb278SMing Lei out: 1363ab1cb278SMing Lei return error; 1364ab1cb278SMing Lei } 1365ab1cb278SMing Lei 136689e4fdecSOmar Sandoval static int loop_set_block_size(struct loop_device *lo, unsigned long arg) 136789e4fdecSOmar Sandoval { 136889e4fdecSOmar Sandoval if (lo->lo_state != Lo_bound) 136989e4fdecSOmar Sandoval return -ENXIO; 137089e4fdecSOmar Sandoval 137189e4fdecSOmar Sandoval if (arg < 512 || arg > PAGE_SIZE || !is_power_of_2(arg)) 137289e4fdecSOmar Sandoval return -EINVAL; 137389e4fdecSOmar Sandoval 137489e4fdecSOmar Sandoval blk_mq_freeze_queue(lo->lo_queue); 137589e4fdecSOmar Sandoval 137689e4fdecSOmar Sandoval blk_queue_logical_block_size(lo->lo_queue, arg); 1377bf093753SOmar Sandoval blk_queue_physical_block_size(lo->lo_queue, arg); 1378bf093753SOmar Sandoval blk_queue_io_min(lo->lo_queue, arg); 137989e4fdecSOmar Sandoval loop_update_dio(lo); 138089e4fdecSOmar Sandoval 138189e4fdecSOmar Sandoval blk_mq_unfreeze_queue(lo->lo_queue); 138289e4fdecSOmar Sandoval 138389e4fdecSOmar Sandoval return 0; 138489e4fdecSOmar Sandoval } 138589e4fdecSOmar Sandoval 1386bb214884SAl Viro static int lo_ioctl(struct block_device *bdev, fmode_t mode, 13871da177e4SLinus Torvalds unsigned int cmd, unsigned long arg) 13881da177e4SLinus Torvalds { 1389bb214884SAl Viro struct loop_device *lo = bdev->bd_disk->private_data; 13901da177e4SLinus Torvalds int err; 13911da177e4SLinus Torvalds 13923148ffbdSOmar Sandoval err = mutex_lock_killable_nested(&lo->lo_ctl_mutex, 1); 13933148ffbdSOmar Sandoval if (err) 13943148ffbdSOmar Sandoval goto out_unlocked; 13953148ffbdSOmar Sandoval 13961da177e4SLinus Torvalds switch (cmd) { 13971da177e4SLinus Torvalds case LOOP_SET_FD: 1398bb214884SAl Viro err = loop_set_fd(lo, mode, bdev, arg); 13991da177e4SLinus Torvalds break; 14001da177e4SLinus Torvalds case LOOP_CHANGE_FD: 1401bb214884SAl Viro err = loop_change_fd(lo, bdev, arg); 14021da177e4SLinus Torvalds break; 14031da177e4SLinus Torvalds case LOOP_CLR_FD: 1404f028f3b2SNikanth Karthikesan /* loop_clr_fd would have unlocked lo_ctl_mutex on success */ 14054c823cc3SAyan George err = loop_clr_fd(lo); 1406f028f3b2SNikanth Karthikesan if (!err) 1407f028f3b2SNikanth Karthikesan goto out_unlocked; 14081da177e4SLinus Torvalds break; 14091da177e4SLinus Torvalds case LOOP_SET_STATUS: 14107035b5dfSDmitry Monakhov err = -EPERM; 14117035b5dfSDmitry Monakhov if ((mode & FMODE_WRITE) || capable(CAP_SYS_ADMIN)) 14127035b5dfSDmitry Monakhov err = loop_set_status_old(lo, 14137035b5dfSDmitry Monakhov (struct loop_info __user *)arg); 14141da177e4SLinus Torvalds break; 14151da177e4SLinus Torvalds case LOOP_GET_STATUS: 14161da177e4SLinus Torvalds err = loop_get_status_old(lo, (struct loop_info __user *) arg); 14172d1d4c1eSOmar Sandoval /* loop_get_status() unlocks lo_ctl_mutex */ 14182d1d4c1eSOmar Sandoval goto out_unlocked; 14191da177e4SLinus Torvalds case LOOP_SET_STATUS64: 14207035b5dfSDmitry Monakhov err = -EPERM; 14217035b5dfSDmitry Monakhov if ((mode & FMODE_WRITE) || capable(CAP_SYS_ADMIN)) 14227035b5dfSDmitry Monakhov err = loop_set_status64(lo, 14237035b5dfSDmitry Monakhov (struct loop_info64 __user *) arg); 14241da177e4SLinus Torvalds break; 14251da177e4SLinus Torvalds case LOOP_GET_STATUS64: 14261da177e4SLinus Torvalds err = loop_get_status64(lo, (struct loop_info64 __user *) arg); 14272d1d4c1eSOmar Sandoval /* loop_get_status() unlocks lo_ctl_mutex */ 14282d1d4c1eSOmar Sandoval goto out_unlocked; 142953d66608SJ. R. Okajima case LOOP_SET_CAPACITY: 143053d66608SJ. R. Okajima err = -EPERM; 143153d66608SJ. R. Okajima if ((mode & FMODE_WRITE) || capable(CAP_SYS_ADMIN)) 143251001b7dSHannes Reinecke err = loop_set_capacity(lo); 143353d66608SJ. R. Okajima break; 1434ab1cb278SMing Lei case LOOP_SET_DIRECT_IO: 1435ab1cb278SMing Lei err = -EPERM; 1436ab1cb278SMing Lei if ((mode & FMODE_WRITE) || capable(CAP_SYS_ADMIN)) 1437ab1cb278SMing Lei err = loop_set_dio(lo, arg); 1438ab1cb278SMing Lei break; 143989e4fdecSOmar Sandoval case LOOP_SET_BLOCK_SIZE: 144089e4fdecSOmar Sandoval err = -EPERM; 144189e4fdecSOmar Sandoval if ((mode & FMODE_WRITE) || capable(CAP_SYS_ADMIN)) 144289e4fdecSOmar Sandoval err = loop_set_block_size(lo, arg); 144389e4fdecSOmar Sandoval break; 14441da177e4SLinus Torvalds default: 14451da177e4SLinus Torvalds err = lo->ioctl ? lo->ioctl(lo, cmd, arg) : -EINVAL; 14461da177e4SLinus Torvalds } 1447f85221ddSIngo Molnar mutex_unlock(&lo->lo_ctl_mutex); 1448f028f3b2SNikanth Karthikesan 1449f028f3b2SNikanth Karthikesan out_unlocked: 14501da177e4SLinus Torvalds return err; 14511da177e4SLinus Torvalds } 14521da177e4SLinus Torvalds 1453863d5b82SDavid Howells #ifdef CONFIG_COMPAT 1454863d5b82SDavid Howells struct compat_loop_info { 1455863d5b82SDavid Howells compat_int_t lo_number; /* ioctl r/o */ 1456863d5b82SDavid Howells compat_dev_t lo_device; /* ioctl r/o */ 1457863d5b82SDavid Howells compat_ulong_t lo_inode; /* ioctl r/o */ 1458863d5b82SDavid Howells compat_dev_t lo_rdevice; /* ioctl r/o */ 1459863d5b82SDavid Howells compat_int_t lo_offset; 1460863d5b82SDavid Howells compat_int_t lo_encrypt_type; 1461863d5b82SDavid Howells compat_int_t lo_encrypt_key_size; /* ioctl w/o */ 1462863d5b82SDavid Howells compat_int_t lo_flags; /* ioctl r/o */ 1463863d5b82SDavid Howells char lo_name[LO_NAME_SIZE]; 1464863d5b82SDavid Howells unsigned char lo_encrypt_key[LO_KEY_SIZE]; /* ioctl w/o */ 1465863d5b82SDavid Howells compat_ulong_t lo_init[2]; 1466863d5b82SDavid Howells char reserved[4]; 1467863d5b82SDavid Howells }; 1468863d5b82SDavid Howells 1469863d5b82SDavid Howells /* 1470863d5b82SDavid Howells * Transfer 32-bit compatibility structure in userspace to 64-bit loop info 1471863d5b82SDavid Howells * - noinlined to reduce stack space usage in main part of driver 1472863d5b82SDavid Howells */ 1473863d5b82SDavid Howells static noinline int 1474ba674cfcSAl Viro loop_info64_from_compat(const struct compat_loop_info __user *arg, 1475863d5b82SDavid Howells struct loop_info64 *info64) 1476863d5b82SDavid Howells { 1477863d5b82SDavid Howells struct compat_loop_info info; 1478863d5b82SDavid Howells 1479863d5b82SDavid Howells if (copy_from_user(&info, arg, sizeof(info))) 1480863d5b82SDavid Howells return -EFAULT; 1481863d5b82SDavid Howells 1482863d5b82SDavid Howells memset(info64, 0, sizeof(*info64)); 1483863d5b82SDavid Howells info64->lo_number = info.lo_number; 1484863d5b82SDavid Howells info64->lo_device = info.lo_device; 1485863d5b82SDavid Howells info64->lo_inode = info.lo_inode; 1486863d5b82SDavid Howells info64->lo_rdevice = info.lo_rdevice; 1487863d5b82SDavid Howells info64->lo_offset = info.lo_offset; 1488863d5b82SDavid Howells info64->lo_sizelimit = 0; 1489863d5b82SDavid Howells info64->lo_encrypt_type = info.lo_encrypt_type; 1490863d5b82SDavid Howells info64->lo_encrypt_key_size = info.lo_encrypt_key_size; 1491863d5b82SDavid Howells info64->lo_flags = info.lo_flags; 1492863d5b82SDavid Howells info64->lo_init[0] = info.lo_init[0]; 1493863d5b82SDavid Howells info64->lo_init[1] = info.lo_init[1]; 1494863d5b82SDavid Howells if (info.lo_encrypt_type == LO_CRYPT_CRYPTOAPI) 1495863d5b82SDavid Howells memcpy(info64->lo_crypt_name, info.lo_name, LO_NAME_SIZE); 1496863d5b82SDavid Howells else 1497863d5b82SDavid Howells memcpy(info64->lo_file_name, info.lo_name, LO_NAME_SIZE); 1498863d5b82SDavid Howells memcpy(info64->lo_encrypt_key, info.lo_encrypt_key, LO_KEY_SIZE); 1499863d5b82SDavid Howells return 0; 1500863d5b82SDavid Howells } 1501863d5b82SDavid Howells 1502863d5b82SDavid Howells /* 1503863d5b82SDavid Howells * Transfer 64-bit loop info to 32-bit compatibility structure in userspace 1504863d5b82SDavid Howells * - noinlined to reduce stack space usage in main part of driver 1505863d5b82SDavid Howells */ 1506863d5b82SDavid Howells static noinline int 1507863d5b82SDavid Howells loop_info64_to_compat(const struct loop_info64 *info64, 1508863d5b82SDavid Howells struct compat_loop_info __user *arg) 1509863d5b82SDavid Howells { 1510863d5b82SDavid Howells struct compat_loop_info info; 1511863d5b82SDavid Howells 1512863d5b82SDavid Howells memset(&info, 0, sizeof(info)); 1513863d5b82SDavid Howells info.lo_number = info64->lo_number; 1514863d5b82SDavid Howells info.lo_device = info64->lo_device; 1515863d5b82SDavid Howells info.lo_inode = info64->lo_inode; 1516863d5b82SDavid Howells info.lo_rdevice = info64->lo_rdevice; 1517863d5b82SDavid Howells info.lo_offset = info64->lo_offset; 1518863d5b82SDavid Howells info.lo_encrypt_type = info64->lo_encrypt_type; 1519863d5b82SDavid Howells info.lo_encrypt_key_size = info64->lo_encrypt_key_size; 1520863d5b82SDavid Howells info.lo_flags = info64->lo_flags; 1521863d5b82SDavid Howells info.lo_init[0] = info64->lo_init[0]; 1522863d5b82SDavid Howells info.lo_init[1] = info64->lo_init[1]; 1523863d5b82SDavid Howells if (info.lo_encrypt_type == LO_CRYPT_CRYPTOAPI) 1524863d5b82SDavid Howells memcpy(info.lo_name, info64->lo_crypt_name, LO_NAME_SIZE); 1525863d5b82SDavid Howells else 1526863d5b82SDavid Howells memcpy(info.lo_name, info64->lo_file_name, LO_NAME_SIZE); 1527863d5b82SDavid Howells memcpy(info.lo_encrypt_key, info64->lo_encrypt_key, LO_KEY_SIZE); 1528863d5b82SDavid Howells 1529863d5b82SDavid Howells /* error in case values were truncated */ 1530863d5b82SDavid Howells if (info.lo_device != info64->lo_device || 1531863d5b82SDavid Howells info.lo_rdevice != info64->lo_rdevice || 1532863d5b82SDavid Howells info.lo_inode != info64->lo_inode || 1533863d5b82SDavid Howells info.lo_offset != info64->lo_offset || 1534863d5b82SDavid Howells info.lo_init[0] != info64->lo_init[0] || 1535863d5b82SDavid Howells info.lo_init[1] != info64->lo_init[1]) 1536863d5b82SDavid Howells return -EOVERFLOW; 1537863d5b82SDavid Howells 1538863d5b82SDavid Howells if (copy_to_user(arg, &info, sizeof(info))) 1539863d5b82SDavid Howells return -EFAULT; 1540863d5b82SDavid Howells return 0; 1541863d5b82SDavid Howells } 1542863d5b82SDavid Howells 1543863d5b82SDavid Howells static int 1544863d5b82SDavid Howells loop_set_status_compat(struct loop_device *lo, 1545863d5b82SDavid Howells const struct compat_loop_info __user *arg) 1546863d5b82SDavid Howells { 1547863d5b82SDavid Howells struct loop_info64 info64; 1548863d5b82SDavid Howells int ret; 1549863d5b82SDavid Howells 1550863d5b82SDavid Howells ret = loop_info64_from_compat(arg, &info64); 1551863d5b82SDavid Howells if (ret < 0) 1552863d5b82SDavid Howells return ret; 1553863d5b82SDavid Howells return loop_set_status(lo, &info64); 1554863d5b82SDavid Howells } 1555863d5b82SDavid Howells 1556863d5b82SDavid Howells static int 1557863d5b82SDavid Howells loop_get_status_compat(struct loop_device *lo, 1558863d5b82SDavid Howells struct compat_loop_info __user *arg) 1559863d5b82SDavid Howells { 1560863d5b82SDavid Howells struct loop_info64 info64; 1561bdac616dSOmar Sandoval int err; 1562863d5b82SDavid Howells 1563bdac616dSOmar Sandoval if (!arg) { 1564bdac616dSOmar Sandoval mutex_unlock(&lo->lo_ctl_mutex); 1565bdac616dSOmar Sandoval return -EINVAL; 1566bdac616dSOmar Sandoval } 1567863d5b82SDavid Howells err = loop_get_status(lo, &info64); 1568863d5b82SDavid Howells if (!err) 1569863d5b82SDavid Howells err = loop_info64_to_compat(&info64, arg); 1570863d5b82SDavid Howells return err; 1571863d5b82SDavid Howells } 1572863d5b82SDavid Howells 1573bb214884SAl Viro static int lo_compat_ioctl(struct block_device *bdev, fmode_t mode, 1574bb214884SAl Viro unsigned int cmd, unsigned long arg) 1575863d5b82SDavid Howells { 1576bb214884SAl Viro struct loop_device *lo = bdev->bd_disk->private_data; 1577863d5b82SDavid Howells int err; 1578863d5b82SDavid Howells 1579863d5b82SDavid Howells switch(cmd) { 1580863d5b82SDavid Howells case LOOP_SET_STATUS: 15813148ffbdSOmar Sandoval err = mutex_lock_killable(&lo->lo_ctl_mutex); 15823148ffbdSOmar Sandoval if (!err) { 15833148ffbdSOmar Sandoval err = loop_set_status_compat(lo, 15843148ffbdSOmar Sandoval (const struct compat_loop_info __user *)arg); 1585863d5b82SDavid Howells mutex_unlock(&lo->lo_ctl_mutex); 15863148ffbdSOmar Sandoval } 1587863d5b82SDavid Howells break; 1588863d5b82SDavid Howells case LOOP_GET_STATUS: 15893148ffbdSOmar Sandoval err = mutex_lock_killable(&lo->lo_ctl_mutex); 15903148ffbdSOmar Sandoval if (!err) { 15913148ffbdSOmar Sandoval err = loop_get_status_compat(lo, 15923148ffbdSOmar Sandoval (struct compat_loop_info __user *)arg); 15932d1d4c1eSOmar Sandoval /* loop_get_status() unlocks lo_ctl_mutex */ 15943148ffbdSOmar Sandoval } 1595863d5b82SDavid Howells break; 159653d66608SJ. R. Okajima case LOOP_SET_CAPACITY: 1597863d5b82SDavid Howells case LOOP_CLR_FD: 1598863d5b82SDavid Howells case LOOP_GET_STATUS64: 1599863d5b82SDavid Howells case LOOP_SET_STATUS64: 1600863d5b82SDavid Howells arg = (unsigned long) compat_ptr(arg); 1601863d5b82SDavid Howells case LOOP_SET_FD: 1602863d5b82SDavid Howells case LOOP_CHANGE_FD: 1603bb214884SAl Viro err = lo_ioctl(bdev, mode, cmd, arg); 1604863d5b82SDavid Howells break; 1605863d5b82SDavid Howells default: 1606863d5b82SDavid Howells err = -ENOIOCTLCMD; 1607863d5b82SDavid Howells break; 1608863d5b82SDavid Howells } 1609863d5b82SDavid Howells return err; 1610863d5b82SDavid Howells } 1611863d5b82SDavid Howells #endif 1612863d5b82SDavid Howells 1613bb214884SAl Viro static int lo_open(struct block_device *bdev, fmode_t mode) 16141da177e4SLinus Torvalds { 1615770fe30aSKay Sievers struct loop_device *lo; 1616770fe30aSKay Sievers int err = 0; 1617770fe30aSKay Sievers 1618770fe30aSKay Sievers mutex_lock(&loop_index_mutex); 1619770fe30aSKay Sievers lo = bdev->bd_disk->private_data; 1620770fe30aSKay Sievers if (!lo) { 1621770fe30aSKay Sievers err = -ENXIO; 1622770fe30aSKay Sievers goto out; 1623770fe30aSKay Sievers } 16241da177e4SLinus Torvalds 1625f8933667SMing Lei atomic_inc(&lo->lo_refcnt); 1626770fe30aSKay Sievers out: 1627770fe30aSKay Sievers mutex_unlock(&loop_index_mutex); 1628770fe30aSKay Sievers return err; 16291da177e4SLinus Torvalds } 16301da177e4SLinus Torvalds 1631ae665016SLinus Torvalds static void __lo_release(struct loop_device *lo) 16321da177e4SLinus Torvalds { 1633ffcd7dcaSAlexander Beregalov int err; 16341da177e4SLinus Torvalds 1635f8933667SMing Lei if (atomic_dec_return(&lo->lo_refcnt)) 1636f8933667SMing Lei return; 1637f8933667SMing Lei 1638f85221ddSIngo Molnar mutex_lock(&lo->lo_ctl_mutex); 163914f27939SMilan Broz if (lo->lo_flags & LO_FLAGS_AUTOCLEAR) { 164014f27939SMilan Broz /* 164114f27939SMilan Broz * In autoclear mode, stop the loop thread 164214f27939SMilan Broz * and remove configuration after last close. 164314f27939SMilan Broz */ 16444c823cc3SAyan George err = loop_clr_fd(lo); 1645ffcd7dcaSAlexander Beregalov if (!err) 1646db2a144bSAl Viro return; 164743cade80SOmar Sandoval } else if (lo->lo_state == Lo_bound) { 164814f27939SMilan Broz /* 164914f27939SMilan Broz * Otherwise keep thread (if running) and config, 165014f27939SMilan Broz * but flush possible ongoing bios in thread. 165114f27939SMilan Broz */ 165243cade80SOmar Sandoval blk_mq_freeze_queue(lo->lo_queue); 165343cade80SOmar Sandoval blk_mq_unfreeze_queue(lo->lo_queue); 165414f27939SMilan Broz } 165596c58655SDavid Woodhouse 1656f85221ddSIngo Molnar mutex_unlock(&lo->lo_ctl_mutex); 16571da177e4SLinus Torvalds } 16581da177e4SLinus Torvalds 1659ae665016SLinus Torvalds static void lo_release(struct gendisk *disk, fmode_t mode) 1660ae665016SLinus Torvalds { 1661ae665016SLinus Torvalds mutex_lock(&loop_index_mutex); 1662ae665016SLinus Torvalds __lo_release(disk->private_data); 1663ae665016SLinus Torvalds mutex_unlock(&loop_index_mutex); 1664ae665016SLinus Torvalds } 1665ae665016SLinus Torvalds 166683d5cde4SAlexey Dobriyan static const struct block_device_operations lo_fops = { 16671da177e4SLinus Torvalds .owner = THIS_MODULE, 1668bb214884SAl Viro .open = lo_open, 1669bb214884SAl Viro .release = lo_release, 1670bb214884SAl Viro .ioctl = lo_ioctl, 1671863d5b82SDavid Howells #ifdef CONFIG_COMPAT 1672bb214884SAl Viro .compat_ioctl = lo_compat_ioctl, 1673863d5b82SDavid Howells #endif 16741da177e4SLinus Torvalds }; 16751da177e4SLinus Torvalds 16761da177e4SLinus Torvalds /* 16771da177e4SLinus Torvalds * And now the modules code and kernel interface. 16781da177e4SLinus Torvalds */ 167973285082SKen Chen static int max_loop; 1680ac04fee0SNamhyung Kim module_param(max_loop, int, S_IRUGO); 1681a47653fcSKen Chen MODULE_PARM_DESC(max_loop, "Maximum number of loop devices"); 1682ac04fee0SNamhyung Kim module_param(max_part, int, S_IRUGO); 1683476a4813SLaurent Vivier MODULE_PARM_DESC(max_part, "Maximum number of partitions per loop device"); 16841da177e4SLinus Torvalds MODULE_LICENSE("GPL"); 16851da177e4SLinus Torvalds MODULE_ALIAS_BLOCKDEV_MAJOR(LOOP_MAJOR); 16861da177e4SLinus Torvalds 16871da177e4SLinus Torvalds int loop_register_transfer(struct loop_func_table *funcs) 16881da177e4SLinus Torvalds { 16891da177e4SLinus Torvalds unsigned int n = funcs->number; 16901da177e4SLinus Torvalds 16911da177e4SLinus Torvalds if (n >= MAX_LO_CRYPT || xfer_funcs[n]) 16921da177e4SLinus Torvalds return -EINVAL; 16931da177e4SLinus Torvalds xfer_funcs[n] = funcs; 16941da177e4SLinus Torvalds return 0; 16951da177e4SLinus Torvalds } 16961da177e4SLinus Torvalds 169734dd82afSKay Sievers static int unregister_transfer_cb(int id, void *ptr, void *data) 169834dd82afSKay Sievers { 169934dd82afSKay Sievers struct loop_device *lo = ptr; 170034dd82afSKay Sievers struct loop_func_table *xfer = data; 170134dd82afSKay Sievers 170234dd82afSKay Sievers mutex_lock(&lo->lo_ctl_mutex); 170334dd82afSKay Sievers if (lo->lo_encryption == xfer) 170434dd82afSKay Sievers loop_release_xfer(lo); 170534dd82afSKay Sievers mutex_unlock(&lo->lo_ctl_mutex); 170634dd82afSKay Sievers return 0; 170734dd82afSKay Sievers } 170834dd82afSKay Sievers 17091da177e4SLinus Torvalds int loop_unregister_transfer(int number) 17101da177e4SLinus Torvalds { 17111da177e4SLinus Torvalds unsigned int n = number; 17121da177e4SLinus Torvalds struct loop_func_table *xfer; 17131da177e4SLinus Torvalds 17141da177e4SLinus Torvalds if (n == 0 || n >= MAX_LO_CRYPT || (xfer = xfer_funcs[n]) == NULL) 17151da177e4SLinus Torvalds return -EINVAL; 17161da177e4SLinus Torvalds 17171da177e4SLinus Torvalds xfer_funcs[n] = NULL; 171834dd82afSKay Sievers idr_for_each(&loop_index_idr, &unregister_transfer_cb, xfer); 17191da177e4SLinus Torvalds return 0; 17201da177e4SLinus Torvalds } 17211da177e4SLinus Torvalds 17221da177e4SLinus Torvalds EXPORT_SYMBOL(loop_register_transfer); 17231da177e4SLinus Torvalds EXPORT_SYMBOL(loop_unregister_transfer); 17241da177e4SLinus Torvalds 1725fc17b653SChristoph Hellwig static blk_status_t loop_queue_rq(struct blk_mq_hw_ctx *hctx, 1726b5dd2f60SMing Lei const struct blk_mq_queue_data *bd) 1727b5dd2f60SMing Lei { 17281894e916SJens Axboe struct request *rq = bd->rq; 17291894e916SJens Axboe struct loop_cmd *cmd = blk_mq_rq_to_pdu(rq); 17301894e916SJens Axboe struct loop_device *lo = rq->q->queuedata; 1731b5dd2f60SMing Lei 17321894e916SJens Axboe blk_mq_start_request(rq); 1733b5dd2f60SMing Lei 1734f4aa4c7bSMing Lei if (lo->lo_state != Lo_bound) 1735fc17b653SChristoph Hellwig return BLK_STS_IOERR; 1736f4aa4c7bSMing Lei 17371894e916SJens Axboe switch (req_op(rq)) { 1738f0225cacSChristoph Hellwig case REQ_OP_FLUSH: 1739f0225cacSChristoph Hellwig case REQ_OP_DISCARD: 174019372e27SChristoph Hellwig case REQ_OP_WRITE_ZEROES: 1741bc07c10aSMing Lei cmd->use_aio = false; 1742f0225cacSChristoph Hellwig break; 1743f0225cacSChristoph Hellwig default: 1744f0225cacSChristoph Hellwig cmd->use_aio = lo->use_dio; 1745f0225cacSChristoph Hellwig break; 1746f0225cacSChristoph Hellwig } 1747bc07c10aSMing Lei 1748d4478e92SShaohua Li /* always use the first bio's css */ 17490b508bc9SShaohua Li #ifdef CONFIG_BLK_CGROUP 17501894e916SJens Axboe if (cmd->use_aio && rq->bio && rq->bio->bi_css) { 17511894e916SJens Axboe cmd->css = rq->bio->bi_css; 1752d4478e92SShaohua Li css_get(cmd->css); 1753d4478e92SShaohua Li } else 1754d4478e92SShaohua Li #endif 1755d4478e92SShaohua Li cmd->css = NULL; 17563989144fSPetr Mladek kthread_queue_work(&lo->worker, &cmd->work); 1757b5dd2f60SMing Lei 1758fc17b653SChristoph Hellwig return BLK_STS_OK; 1759b5dd2f60SMing Lei } 1760b5dd2f60SMing Lei 1761b5dd2f60SMing Lei static void loop_handle_cmd(struct loop_cmd *cmd) 1762b5dd2f60SMing Lei { 17631894e916SJens Axboe struct request *rq = blk_mq_rq_from_pdu(cmd); 17641894e916SJens Axboe const bool write = op_is_write(req_op(rq)); 17651894e916SJens Axboe struct loop_device *lo = rq->q->queuedata; 1766f4829a9bSChristoph Hellwig int ret = 0; 1767b5dd2f60SMing Lei 1768f4829a9bSChristoph Hellwig if (write && (lo->lo_flags & LO_FLAGS_READ_ONLY)) { 1769f4829a9bSChristoph Hellwig ret = -EIO; 1770b5dd2f60SMing Lei goto failed; 1771f4829a9bSChristoph Hellwig } 1772b5dd2f60SMing Lei 17731894e916SJens Axboe ret = do_req_filebacked(lo, rq); 1774b5dd2f60SMing Lei failed: 1775bc07c10aSMing Lei /* complete non-aio request */ 1776fe2cb290SChristoph Hellwig if (!cmd->use_aio || ret) { 1777fe2cb290SChristoph Hellwig cmd->ret = ret ? -EIO : 0; 17781894e916SJens Axboe blk_mq_complete_request(rq); 1779fe2cb290SChristoph Hellwig } 1780b5dd2f60SMing Lei } 1781b5dd2f60SMing Lei 1782e03a3d7aSMing Lei static void loop_queue_work(struct kthread_work *work) 1783b5dd2f60SMing Lei { 1784b5dd2f60SMing Lei struct loop_cmd *cmd = 1785e03a3d7aSMing Lei container_of(work, struct loop_cmd, work); 1786b5dd2f60SMing Lei 1787b5dd2f60SMing Lei loop_handle_cmd(cmd); 1788b5dd2f60SMing Lei } 1789b5dd2f60SMing Lei 1790d6296d39SChristoph Hellwig static int loop_init_request(struct blk_mq_tag_set *set, struct request *rq, 1791d6296d39SChristoph Hellwig unsigned int hctx_idx, unsigned int numa_node) 1792b5dd2f60SMing Lei { 1793b5dd2f60SMing Lei struct loop_cmd *cmd = blk_mq_rq_to_pdu(rq); 1794b5dd2f60SMing Lei 17953989144fSPetr Mladek kthread_init_work(&cmd->work, loop_queue_work); 1796b5dd2f60SMing Lei return 0; 1797b5dd2f60SMing Lei } 1798b5dd2f60SMing Lei 1799f363b089SEric Biggers static const struct blk_mq_ops loop_mq_ops = { 1800b5dd2f60SMing Lei .queue_rq = loop_queue_rq, 1801b5dd2f60SMing Lei .init_request = loop_init_request, 1802fe2cb290SChristoph Hellwig .complete = lo_complete_rq, 1803b5dd2f60SMing Lei }; 1804b5dd2f60SMing Lei 180534dd82afSKay Sievers static int loop_add(struct loop_device **l, int i) 18061da177e4SLinus Torvalds { 180773285082SKen Chen struct loop_device *lo; 180873285082SKen Chen struct gendisk *disk; 180934dd82afSKay Sievers int err; 18101da177e4SLinus Torvalds 181134dd82afSKay Sievers err = -ENOMEM; 181268d740d7SSilva Paulo lo = kzalloc(sizeof(*lo), GFP_KERNEL); 181368d740d7SSilva Paulo if (!lo) 181473285082SKen Chen goto out; 181534dd82afSKay Sievers 1816ef7e7c82SMikulas Patocka lo->lo_state = Lo_unbound; 1817ef7e7c82SMikulas Patocka 1818c718aa65STejun Heo /* allocate id, if @id >= 0, we're requesting that specific id */ 181934dd82afSKay Sievers if (i >= 0) { 1820c718aa65STejun Heo err = idr_alloc(&loop_index_idr, lo, i, i + 1, GFP_KERNEL); 1821c718aa65STejun Heo if (err == -ENOSPC) 182234dd82afSKay Sievers err = -EEXIST; 182334dd82afSKay Sievers } else { 1824c718aa65STejun Heo err = idr_alloc(&loop_index_idr, lo, 0, 0, GFP_KERNEL); 182534dd82afSKay Sievers } 182634dd82afSKay Sievers if (err < 0) 182734dd82afSKay Sievers goto out_free_dev; 1828c718aa65STejun Heo i = err; 18291da177e4SLinus Torvalds 1830183cfb57SWei Yongjun err = -ENOMEM; 1831b5dd2f60SMing Lei lo->tag_set.ops = &loop_mq_ops; 1832b5dd2f60SMing Lei lo->tag_set.nr_hw_queues = 1; 1833b5dd2f60SMing Lei lo->tag_set.queue_depth = 128; 1834b5dd2f60SMing Lei lo->tag_set.numa_node = NUMA_NO_NODE; 1835b5dd2f60SMing Lei lo->tag_set.cmd_size = sizeof(struct loop_cmd); 1836b5dd2f60SMing Lei lo->tag_set.flags = BLK_MQ_F_SHOULD_MERGE | BLK_MQ_F_SG_MERGE; 1837b5dd2f60SMing Lei lo->tag_set.driver_data = lo; 1838b5dd2f60SMing Lei 1839b5dd2f60SMing Lei err = blk_mq_alloc_tag_set(&lo->tag_set); 1840b5dd2f60SMing Lei if (err) 18413ec981e3SMikulas Patocka goto out_free_idr; 184273285082SKen Chen 1843b5dd2f60SMing Lei lo->lo_queue = blk_mq_init_queue(&lo->tag_set); 1844b5dd2f60SMing Lei if (IS_ERR_OR_NULL(lo->lo_queue)) { 1845b5dd2f60SMing Lei err = PTR_ERR(lo->lo_queue); 1846b5dd2f60SMing Lei goto out_cleanup_tags; 1847b5dd2f60SMing Lei } 1848ef7e7c82SMikulas Patocka lo->lo_queue->queuedata = lo; 1849ef7e7c82SMikulas Patocka 185054bb0adeSShaohua Li blk_queue_max_hw_sectors(lo->lo_queue, BLK_DEF_MAX_SECTORS); 185140326d8aSShaohua Li 18525b5e20f4SMing Lei /* 185340326d8aSShaohua Li * By default, we do buffer IO, so it doesn't make sense to enable 185440326d8aSShaohua Li * merge because the I/O submitted to backing file is handled page by 185540326d8aSShaohua Li * page. For directio mode, merge does help to dispatch bigger request 185640326d8aSShaohua Li * to underlayer disk. We will enable merge once directio is enabled. 18575b5e20f4SMing Lei */ 18588b904b5bSBart Van Assche blk_queue_flag_set(QUEUE_FLAG_NOMERGES, lo->lo_queue); 18595b5e20f4SMing Lei 18607a649737SMinfei Huang err = -ENOMEM; 1861476a4813SLaurent Vivier disk = lo->lo_disk = alloc_disk(1 << part_shift); 186273285082SKen Chen if (!disk) 186373285082SKen Chen goto out_free_queue; 186473285082SKen Chen 1865e03c8dd1SKay Sievers /* 1866e03c8dd1SKay Sievers * Disable partition scanning by default. The in-kernel partition 1867e03c8dd1SKay Sievers * scanning can be requested individually per-device during its 1868e03c8dd1SKay Sievers * setup. Userspace can always add and remove partitions from all 1869e03c8dd1SKay Sievers * devices. The needed partition minors are allocated from the 1870e03c8dd1SKay Sievers * extended minor space, the main loop device numbers will continue 1871e03c8dd1SKay Sievers * to match the loop minors, regardless of the number of partitions 1872e03c8dd1SKay Sievers * used. 1873e03c8dd1SKay Sievers * 1874e03c8dd1SKay Sievers * If max_part is given, partition scanning is globally enabled for 1875e03c8dd1SKay Sievers * all loop devices. The minors for the main loop devices will be 1876e03c8dd1SKay Sievers * multiples of max_part. 1877e03c8dd1SKay Sievers * 1878e03c8dd1SKay Sievers * Note: Global-for-all-devices, set-only-at-init, read-only module 1879e03c8dd1SKay Sievers * parameteters like 'max_loop' and 'max_part' make things needlessly 1880e03c8dd1SKay Sievers * complicated, are too static, inflexible and may surprise 1881e03c8dd1SKay Sievers * userspace tools. Parameters like this in general should be avoided. 1882e03c8dd1SKay Sievers */ 1883e03c8dd1SKay Sievers if (!part_shift) 1884e03c8dd1SKay Sievers disk->flags |= GENHD_FL_NO_PART_SCAN; 1885e03c8dd1SKay Sievers disk->flags |= GENHD_FL_EXT_DEVT; 1886f85221ddSIngo Molnar mutex_init(&lo->lo_ctl_mutex); 1887f8933667SMing Lei atomic_set(&lo->lo_refcnt, 0); 18881da177e4SLinus Torvalds lo->lo_number = i; 18891da177e4SLinus Torvalds spin_lock_init(&lo->lo_lock); 18901da177e4SLinus Torvalds disk->major = LOOP_MAJOR; 1891476a4813SLaurent Vivier disk->first_minor = i << part_shift; 18921da177e4SLinus Torvalds disk->fops = &lo_fops; 18931da177e4SLinus Torvalds disk->private_data = lo; 18941da177e4SLinus Torvalds disk->queue = lo->lo_queue; 189573285082SKen Chen sprintf(disk->disk_name, "loop%d", i); 189634dd82afSKay Sievers add_disk(disk); 189734dd82afSKay Sievers *l = lo; 189834dd82afSKay Sievers return lo->lo_number; 189973285082SKen Chen 190073285082SKen Chen out_free_queue: 190173285082SKen Chen blk_cleanup_queue(lo->lo_queue); 1902b5dd2f60SMing Lei out_cleanup_tags: 1903b5dd2f60SMing Lei blk_mq_free_tag_set(&lo->tag_set); 19043ec981e3SMikulas Patocka out_free_idr: 19053ec981e3SMikulas Patocka idr_remove(&loop_index_idr, i); 190673285082SKen Chen out_free_dev: 190773285082SKen Chen kfree(lo); 190873285082SKen Chen out: 190934dd82afSKay Sievers return err; 19101da177e4SLinus Torvalds } 19111da177e4SLinus Torvalds 191234dd82afSKay Sievers static void loop_remove(struct loop_device *lo) 191373285082SKen Chen { 19146cd18e71SNeilBrown del_gendisk(lo->lo_disk); 19150fa8ebddSBart Van Assche blk_cleanup_queue(lo->lo_queue); 1916b5dd2f60SMing Lei blk_mq_free_tag_set(&lo->tag_set); 191773285082SKen Chen put_disk(lo->lo_disk); 191873285082SKen Chen kfree(lo); 191973285082SKen Chen } 192073285082SKen Chen 1921770fe30aSKay Sievers static int find_free_cb(int id, void *ptr, void *data) 1922770fe30aSKay Sievers { 1923770fe30aSKay Sievers struct loop_device *lo = ptr; 1924770fe30aSKay Sievers struct loop_device **l = data; 1925770fe30aSKay Sievers 1926770fe30aSKay Sievers if (lo->lo_state == Lo_unbound) { 1927770fe30aSKay Sievers *l = lo; 1928770fe30aSKay Sievers return 1; 1929770fe30aSKay Sievers } 1930770fe30aSKay Sievers return 0; 1931770fe30aSKay Sievers } 1932770fe30aSKay Sievers 193334dd82afSKay Sievers static int loop_lookup(struct loop_device **l, int i) 1934a47653fcSKen Chen { 1935a47653fcSKen Chen struct loop_device *lo; 193634dd82afSKay Sievers int ret = -ENODEV; 1937a47653fcSKen Chen 1938770fe30aSKay Sievers if (i < 0) { 1939770fe30aSKay Sievers int err; 1940770fe30aSKay Sievers 1941770fe30aSKay Sievers err = idr_for_each(&loop_index_idr, &find_free_cb, &lo); 1942770fe30aSKay Sievers if (err == 1) { 1943770fe30aSKay Sievers *l = lo; 1944770fe30aSKay Sievers ret = lo->lo_number; 1945770fe30aSKay Sievers } 1946770fe30aSKay Sievers goto out; 1947a47653fcSKen Chen } 1948a47653fcSKen Chen 1949770fe30aSKay Sievers /* lookup and return a specific i */ 195034dd82afSKay Sievers lo = idr_find(&loop_index_idr, i); 1951a47653fcSKen Chen if (lo) { 195234dd82afSKay Sievers *l = lo; 195334dd82afSKay Sievers ret = lo->lo_number; 1954a47653fcSKen Chen } 1955770fe30aSKay Sievers out: 195634dd82afSKay Sievers return ret; 1957a47653fcSKen Chen } 1958a47653fcSKen Chen 195973285082SKen Chen static struct kobject *loop_probe(dev_t dev, int *part, void *data) 196073285082SKen Chen { 1961705962ccSAl Viro struct loop_device *lo; 196207002e99SAl Viro struct kobject *kobj; 196334dd82afSKay Sievers int err; 196473285082SKen Chen 196534dd82afSKay Sievers mutex_lock(&loop_index_mutex); 196634dd82afSKay Sievers err = loop_lookup(&lo, MINOR(dev) >> part_shift); 196734dd82afSKay Sievers if (err < 0) 196834dd82afSKay Sievers err = loop_add(&lo, MINOR(dev) >> part_shift); 196934dd82afSKay Sievers if (err < 0) 1970a207f593SMikulas Patocka kobj = NULL; 197134dd82afSKay Sievers else 19723079c22eSJan Kara kobj = get_disk_and_module(lo->lo_disk); 197334dd82afSKay Sievers mutex_unlock(&loop_index_mutex); 197473285082SKen Chen 197573285082SKen Chen *part = 0; 197607002e99SAl Viro return kobj; 197773285082SKen Chen } 197873285082SKen Chen 1979770fe30aSKay Sievers static long loop_control_ioctl(struct file *file, unsigned int cmd, 1980770fe30aSKay Sievers unsigned long parm) 1981770fe30aSKay Sievers { 1982770fe30aSKay Sievers struct loop_device *lo; 1983770fe30aSKay Sievers int ret = -ENOSYS; 1984770fe30aSKay Sievers 1985770fe30aSKay Sievers mutex_lock(&loop_index_mutex); 1986770fe30aSKay Sievers switch (cmd) { 1987770fe30aSKay Sievers case LOOP_CTL_ADD: 1988770fe30aSKay Sievers ret = loop_lookup(&lo, parm); 1989770fe30aSKay Sievers if (ret >= 0) { 1990770fe30aSKay Sievers ret = -EEXIST; 1991770fe30aSKay Sievers break; 1992770fe30aSKay Sievers } 1993770fe30aSKay Sievers ret = loop_add(&lo, parm); 1994770fe30aSKay Sievers break; 1995770fe30aSKay Sievers case LOOP_CTL_REMOVE: 1996770fe30aSKay Sievers ret = loop_lookup(&lo, parm); 1997770fe30aSKay Sievers if (ret < 0) 1998770fe30aSKay Sievers break; 19993148ffbdSOmar Sandoval ret = mutex_lock_killable(&lo->lo_ctl_mutex); 20003148ffbdSOmar Sandoval if (ret) 20013148ffbdSOmar Sandoval break; 2002770fe30aSKay Sievers if (lo->lo_state != Lo_unbound) { 2003770fe30aSKay Sievers ret = -EBUSY; 2004770fe30aSKay Sievers mutex_unlock(&lo->lo_ctl_mutex); 2005770fe30aSKay Sievers break; 2006770fe30aSKay Sievers } 2007f8933667SMing Lei if (atomic_read(&lo->lo_refcnt) > 0) { 2008770fe30aSKay Sievers ret = -EBUSY; 2009770fe30aSKay Sievers mutex_unlock(&lo->lo_ctl_mutex); 2010770fe30aSKay Sievers break; 2011770fe30aSKay Sievers } 2012770fe30aSKay Sievers lo->lo_disk->private_data = NULL; 2013770fe30aSKay Sievers mutex_unlock(&lo->lo_ctl_mutex); 2014770fe30aSKay Sievers idr_remove(&loop_index_idr, lo->lo_number); 2015770fe30aSKay Sievers loop_remove(lo); 2016770fe30aSKay Sievers break; 2017770fe30aSKay Sievers case LOOP_CTL_GET_FREE: 2018770fe30aSKay Sievers ret = loop_lookup(&lo, -1); 2019770fe30aSKay Sievers if (ret >= 0) 2020770fe30aSKay Sievers break; 2021770fe30aSKay Sievers ret = loop_add(&lo, -1); 2022770fe30aSKay Sievers } 2023770fe30aSKay Sievers mutex_unlock(&loop_index_mutex); 2024770fe30aSKay Sievers 2025770fe30aSKay Sievers return ret; 2026770fe30aSKay Sievers } 2027770fe30aSKay Sievers 2028770fe30aSKay Sievers static const struct file_operations loop_ctl_fops = { 2029770fe30aSKay Sievers .open = nonseekable_open, 2030770fe30aSKay Sievers .unlocked_ioctl = loop_control_ioctl, 2031770fe30aSKay Sievers .compat_ioctl = loop_control_ioctl, 2032770fe30aSKay Sievers .owner = THIS_MODULE, 2033770fe30aSKay Sievers .llseek = noop_llseek, 2034770fe30aSKay Sievers }; 2035770fe30aSKay Sievers 2036770fe30aSKay Sievers static struct miscdevice loop_misc = { 2037770fe30aSKay Sievers .minor = LOOP_CTRL_MINOR, 2038770fe30aSKay Sievers .name = "loop-control", 2039770fe30aSKay Sievers .fops = &loop_ctl_fops, 2040770fe30aSKay Sievers }; 2041770fe30aSKay Sievers 2042770fe30aSKay Sievers MODULE_ALIAS_MISCDEV(LOOP_CTRL_MINOR); 2043770fe30aSKay Sievers MODULE_ALIAS("devname:loop-control"); 2044770fe30aSKay Sievers 204573285082SKen Chen static int __init loop_init(void) 204673285082SKen Chen { 2047a47653fcSKen Chen int i, nr; 2048a47653fcSKen Chen unsigned long range; 204934dd82afSKay Sievers struct loop_device *lo; 2050770fe30aSKay Sievers int err; 2051a47653fcSKen Chen 2052476a4813SLaurent Vivier part_shift = 0; 2053ac04fee0SNamhyung Kim if (max_part > 0) { 2054476a4813SLaurent Vivier part_shift = fls(max_part); 2055476a4813SLaurent Vivier 2056ac04fee0SNamhyung Kim /* 2057ac04fee0SNamhyung Kim * Adjust max_part according to part_shift as it is exported 2058ac04fee0SNamhyung Kim * to user space so that user can decide correct minor number 2059ac04fee0SNamhyung Kim * if [s]he want to create more devices. 2060ac04fee0SNamhyung Kim * 2061ac04fee0SNamhyung Kim * Note that -1 is required because partition 0 is reserved 2062ac04fee0SNamhyung Kim * for the whole disk. 2063ac04fee0SNamhyung Kim */ 2064ac04fee0SNamhyung Kim max_part = (1UL << part_shift) - 1; 2065ac04fee0SNamhyung Kim } 2066ac04fee0SNamhyung Kim 2067b1a66504SGuo Chao if ((1UL << part_shift) > DISK_MAX_PARTS) { 2068b1a66504SGuo Chao err = -EINVAL; 2069a8c1d064SAnton Volkov goto err_out; 2070b1a66504SGuo Chao } 207178f4bb36SNamhyung Kim 2072b1a66504SGuo Chao if (max_loop > 1UL << (MINORBITS - part_shift)) { 2073b1a66504SGuo Chao err = -EINVAL; 2074a8c1d064SAnton Volkov goto err_out; 2075b1a66504SGuo Chao } 207673285082SKen Chen 2077d134b00bSKay Sievers /* 2078d134b00bSKay Sievers * If max_loop is specified, create that many devices upfront. 2079d134b00bSKay Sievers * This also becomes a hard limit. If max_loop is not specified, 2080d134b00bSKay Sievers * create CONFIG_BLK_DEV_LOOP_MIN_COUNT loop devices at module 2081d134b00bSKay Sievers * init time. Loop devices can be requested on-demand with the 2082d134b00bSKay Sievers * /dev/loop-control interface, or be instantiated by accessing 2083d134b00bSKay Sievers * a 'dead' device node. 2084d134b00bSKay Sievers */ 208573285082SKen Chen if (max_loop) { 2086a47653fcSKen Chen nr = max_loop; 2087a1c15c59SNamhyung Kim range = max_loop << part_shift; 2088a47653fcSKen Chen } else { 2089d134b00bSKay Sievers nr = CONFIG_BLK_DEV_LOOP_MIN_COUNT; 2090a1c15c59SNamhyung Kim range = 1UL << MINORBITS; 209173285082SKen Chen } 2092a47653fcSKen Chen 2093a8c1d064SAnton Volkov err = misc_register(&loop_misc); 2094a8c1d064SAnton Volkov if (err < 0) 2095a8c1d064SAnton Volkov goto err_out; 2096a8c1d064SAnton Volkov 2097a8c1d064SAnton Volkov 2098b1a66504SGuo Chao if (register_blkdev(LOOP_MAJOR, "loop")) { 2099b1a66504SGuo Chao err = -EIO; 2100b1a66504SGuo Chao goto misc_out; 2101b1a66504SGuo Chao } 2102a47653fcSKen Chen 2103a47653fcSKen Chen blk_register_region(MKDEV(LOOP_MAJOR, 0), range, 2104a47653fcSKen Chen THIS_MODULE, loop_probe, NULL, NULL); 2105a47653fcSKen Chen 2106d134b00bSKay Sievers /* pre-create number of devices given by config or max_loop */ 210734dd82afSKay Sievers mutex_lock(&loop_index_mutex); 210834dd82afSKay Sievers for (i = 0; i < nr; i++) 210934dd82afSKay Sievers loop_add(&lo, i); 211034dd82afSKay Sievers mutex_unlock(&loop_index_mutex); 211134dd82afSKay Sievers 211273285082SKen Chen printk(KERN_INFO "loop: module loaded\n"); 21131da177e4SLinus Torvalds return 0; 2114b1a66504SGuo Chao 2115b1a66504SGuo Chao misc_out: 2116b1a66504SGuo Chao misc_deregister(&loop_misc); 2117a8c1d064SAnton Volkov err_out: 2118b1a66504SGuo Chao return err; 211934dd82afSKay Sievers } 2120a47653fcSKen Chen 212134dd82afSKay Sievers static int loop_exit_cb(int id, void *ptr, void *data) 212234dd82afSKay Sievers { 212334dd82afSKay Sievers struct loop_device *lo = ptr; 2124a47653fcSKen Chen 212534dd82afSKay Sievers loop_remove(lo); 212634dd82afSKay Sievers return 0; 21271da177e4SLinus Torvalds } 21281da177e4SLinus Torvalds 212973285082SKen Chen static void __exit loop_exit(void) 21301da177e4SLinus Torvalds { 2131a47653fcSKen Chen unsigned long range; 21321da177e4SLinus Torvalds 2133a1c15c59SNamhyung Kim range = max_loop ? max_loop << part_shift : 1UL << MINORBITS; 2134a47653fcSKen Chen 213534dd82afSKay Sievers idr_for_each(&loop_index_idr, &loop_exit_cb, NULL); 213634dd82afSKay Sievers idr_destroy(&loop_index_idr); 213773285082SKen Chen 2138a47653fcSKen Chen blk_unregister_region(MKDEV(LOOP_MAJOR, 0), range); 213900d59405SAkinobu Mita unregister_blkdev(LOOP_MAJOR, "loop"); 2140770fe30aSKay Sievers 2141770fe30aSKay Sievers misc_deregister(&loop_misc); 21421da177e4SLinus Torvalds } 21431da177e4SLinus Torvalds 21441da177e4SLinus Torvalds module_init(loop_init); 21451da177e4SLinus Torvalds module_exit(loop_exit); 21461da177e4SLinus Torvalds 21471da177e4SLinus Torvalds #ifndef MODULE 21481da177e4SLinus Torvalds static int __init max_loop_setup(char *str) 21491da177e4SLinus Torvalds { 21501da177e4SLinus Torvalds max_loop = simple_strtol(str, NULL, 0); 21511da177e4SLinus Torvalds return 1; 21521da177e4SLinus Torvalds } 21531da177e4SLinus Torvalds 21541da177e4SLinus Torvalds __setup("max_loop=", max_loop_setup); 21551da177e4SLinus Torvalds #endif 2156