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) { 21740326d8aSShaohua Li queue_flag_clear_unlocked(QUEUE_FLAG_NOMERGES, lo->lo_queue); 2182e5ab5f3SMing Lei lo->lo_flags |= LO_FLAGS_DIRECT_IO; 21940326d8aSShaohua Li } else { 22040326d8aSShaohua Li queue_flag_set_unlocked(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 269aa4d8616SChristoph Hellwig iov_iter_bvec(&i, ITER_BVEC, 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); 454bc07c10aSMing Lei 455fe2cb290SChristoph Hellwig if (unlikely(req_op(cmd->rq) == REQ_OP_READ && cmd->use_aio && 456fe2cb290SChristoph Hellwig cmd->ret >= 0 && cmd->ret < blk_rq_bytes(cmd->rq))) { 457bc07c10aSMing Lei struct bio *bio = cmd->rq->bio; 458bc07c10aSMing Lei 459fe2cb290SChristoph Hellwig bio_advance(bio, cmd->ret); 460bc07c10aSMing Lei zero_fill_bio(bio); 461bc07c10aSMing Lei } 462fe2cb290SChristoph Hellwig 4632a842acaSChristoph Hellwig blk_mq_end_request(rq, cmd->ret < 0 ? BLK_STS_IOERR : BLK_STS_OK); 464bc07c10aSMing Lei } 465bc07c10aSMing Lei 46692d77332SShaohua Li static void lo_rw_aio_do_completion(struct loop_cmd *cmd) 46792d77332SShaohua Li { 46892d77332SShaohua Li if (!atomic_dec_and_test(&cmd->ref)) 46992d77332SShaohua Li return; 47092d77332SShaohua Li kfree(cmd->bvec); 47192d77332SShaohua Li cmd->bvec = NULL; 47292d77332SShaohua Li blk_mq_complete_request(cmd->rq); 47392d77332SShaohua Li } 47492d77332SShaohua Li 475bc07c10aSMing Lei static void lo_rw_aio_complete(struct kiocb *iocb, long ret, long ret2) 476bc07c10aSMing Lei { 477bc07c10aSMing Lei struct loop_cmd *cmd = container_of(iocb, struct loop_cmd, iocb); 478bc07c10aSMing Lei 479d4478e92SShaohua Li if (cmd->css) 480d4478e92SShaohua Li css_put(cmd->css); 481fe2cb290SChristoph Hellwig cmd->ret = ret; 48292d77332SShaohua Li lo_rw_aio_do_completion(cmd); 483bc07c10aSMing Lei } 484bc07c10aSMing Lei 485bc07c10aSMing Lei static int lo_rw_aio(struct loop_device *lo, struct loop_cmd *cmd, 486bc07c10aSMing Lei loff_t pos, bool rw) 487bc07c10aSMing Lei { 488bc07c10aSMing Lei struct iov_iter iter; 489bc07c10aSMing Lei struct bio_vec *bvec; 49040326d8aSShaohua Li struct request *rq = cmd->rq; 49140326d8aSShaohua Li struct bio *bio = rq->bio; 492bc07c10aSMing Lei struct file *file = lo->lo_backing_file; 49340326d8aSShaohua Li unsigned int offset; 49440326d8aSShaohua Li int segments = 0; 495bc07c10aSMing Lei int ret; 496bc07c10aSMing Lei 49740326d8aSShaohua Li if (rq->bio != rq->biotail) { 49840326d8aSShaohua Li struct req_iterator iter; 49940326d8aSShaohua Li struct bio_vec tmp; 500bc07c10aSMing Lei 50140326d8aSShaohua Li __rq_for_each_bio(bio, rq) 50240326d8aSShaohua Li segments += bio_segments(bio); 50340326d8aSShaohua Li bvec = kmalloc(sizeof(struct bio_vec) * segments, GFP_NOIO); 50440326d8aSShaohua Li if (!bvec) 50540326d8aSShaohua Li return -EIO; 50640326d8aSShaohua Li cmd->bvec = bvec; 50740326d8aSShaohua Li 508a7297a6aSMing Lei /* 50940326d8aSShaohua Li * The bios of the request may be started from the middle of 51040326d8aSShaohua Li * the 'bvec' because of bio splitting, so we can't directly 51140326d8aSShaohua Li * copy bio->bi_iov_vec to new bvec. The rq_for_each_segment 51240326d8aSShaohua Li * API will take care of all details for us. 513a7297a6aSMing Lei */ 51440326d8aSShaohua Li rq_for_each_segment(tmp, rq, iter) { 51540326d8aSShaohua Li *bvec = tmp; 51640326d8aSShaohua Li bvec++; 51740326d8aSShaohua Li } 51840326d8aSShaohua Li bvec = cmd->bvec; 51940326d8aSShaohua Li offset = 0; 52040326d8aSShaohua Li } else { 52140326d8aSShaohua Li /* 52240326d8aSShaohua Li * Same here, this bio may be started from the middle of the 52340326d8aSShaohua Li * 'bvec' because of bio splitting, so offset from the bvec 52440326d8aSShaohua Li * must be passed to iov iterator 52540326d8aSShaohua Li */ 52640326d8aSShaohua Li offset = bio->bi_iter.bi_bvec_done; 52740326d8aSShaohua Li bvec = __bvec_iter_bvec(bio->bi_io_vec, bio->bi_iter); 52840326d8aSShaohua Li segments = bio_segments(bio); 52940326d8aSShaohua Li } 53092d77332SShaohua Li atomic_set(&cmd->ref, 2); 53140326d8aSShaohua Li 53240326d8aSShaohua Li iov_iter_bvec(&iter, ITER_BVEC | rw, bvec, 53340326d8aSShaohua Li segments, blk_rq_bytes(rq)); 53440326d8aSShaohua Li iter.iov_offset = offset; 535bc07c10aSMing Lei 536bc07c10aSMing Lei cmd->iocb.ki_pos = pos; 537bc07c10aSMing Lei cmd->iocb.ki_filp = file; 538bc07c10aSMing Lei cmd->iocb.ki_complete = lo_rw_aio_complete; 539bc07c10aSMing Lei cmd->iocb.ki_flags = IOCB_DIRECT; 540d4478e92SShaohua Li if (cmd->css) 541d4478e92SShaohua Li kthread_associate_blkcg(cmd->css); 542bc07c10aSMing Lei 543bc07c10aSMing Lei if (rw == WRITE) 544bb7462b6SMiklos Szeredi ret = call_write_iter(file, &cmd->iocb, &iter); 545bc07c10aSMing Lei else 546bb7462b6SMiklos Szeredi ret = call_read_iter(file, &cmd->iocb, &iter); 547bc07c10aSMing Lei 54892d77332SShaohua Li lo_rw_aio_do_completion(cmd); 549d4478e92SShaohua Li kthread_associate_blkcg(NULL); 55092d77332SShaohua Li 551bc07c10aSMing Lei if (ret != -EIOCBQUEUED) 552bc07c10aSMing Lei cmd->iocb.ki_complete(&cmd->iocb, ret, 0); 553bc07c10aSMing Lei return 0; 554bc07c10aSMing Lei } 555bc07c10aSMing Lei 556c1c87c2bSChristoph Hellwig static int do_req_filebacked(struct loop_device *lo, struct request *rq) 557bc07c10aSMing Lei { 558bc07c10aSMing Lei struct loop_cmd *cmd = blk_mq_rq_to_pdu(rq); 559c1c87c2bSChristoph Hellwig loff_t pos = ((loff_t) blk_rq_pos(rq) << 9) + lo->lo_offset; 560bc07c10aSMing Lei 561bc07c10aSMing Lei /* 562bc07c10aSMing Lei * lo_write_simple and lo_read_simple should have been covered 563bc07c10aSMing Lei * by io submit style function like lo_rw_aio(), one blocker 564bc07c10aSMing Lei * is that lo_read_simple() need to call flush_dcache_page after 565bc07c10aSMing Lei * the page is written from kernel, and it isn't easy to handle 566bc07c10aSMing Lei * this in io submit style function which submits all segments 567bc07c10aSMing Lei * of the req at one time. And direct read IO doesn't need to 568bc07c10aSMing Lei * run flush_dcache_page(). 569bc07c10aSMing Lei */ 570c1c87c2bSChristoph Hellwig switch (req_op(rq)) { 571c1c87c2bSChristoph Hellwig case REQ_OP_FLUSH: 572c1c87c2bSChristoph Hellwig return lo_req_flush(lo, rq); 573c1c87c2bSChristoph Hellwig case REQ_OP_DISCARD: 57419372e27SChristoph Hellwig case REQ_OP_WRITE_ZEROES: 575c1c87c2bSChristoph Hellwig return lo_discard(lo, rq, pos); 576c1c87c2bSChristoph Hellwig case REQ_OP_WRITE: 577c1c87c2bSChristoph Hellwig if (lo->transfer) 578c1c87c2bSChristoph Hellwig return lo_write_transfer(lo, rq, pos); 579c1c87c2bSChristoph Hellwig else if (cmd->use_aio) 580c1c87c2bSChristoph Hellwig return lo_rw_aio(lo, cmd, pos, WRITE); 581c1c87c2bSChristoph Hellwig else 582bc07c10aSMing Lei return lo_write_simple(lo, rq, pos); 583c1c87c2bSChristoph Hellwig case REQ_OP_READ: 584c1c87c2bSChristoph Hellwig if (lo->transfer) 585c1c87c2bSChristoph Hellwig return lo_read_transfer(lo, rq, pos); 586c1c87c2bSChristoph Hellwig else if (cmd->use_aio) 587c1c87c2bSChristoph Hellwig return lo_rw_aio(lo, cmd, pos, READ); 588bc07c10aSMing Lei else 589bc07c10aSMing Lei return lo_read_simple(lo, rq, pos); 590c1c87c2bSChristoph Hellwig default: 591c1c87c2bSChristoph Hellwig WARN_ON_ONCE(1); 592c1c87c2bSChristoph Hellwig return -EIO; 593c1c87c2bSChristoph Hellwig break; 594bc07c10aSMing Lei } 5951da177e4SLinus Torvalds } 5961da177e4SLinus Torvalds 5972e5ab5f3SMing Lei static inline void loop_update_dio(struct loop_device *lo) 5982e5ab5f3SMing Lei { 5992e5ab5f3SMing Lei __loop_update_dio(lo, io_is_direct(lo->lo_backing_file) | 6002e5ab5f3SMing Lei lo->use_dio); 6012e5ab5f3SMing Lei } 6022e5ab5f3SMing Lei 60306f0e9e6SMing Lei static void loop_reread_partitions(struct loop_device *lo, 60406f0e9e6SMing Lei struct block_device *bdev) 60506f0e9e6SMing Lei { 60606f0e9e6SMing Lei int rc; 60706f0e9e6SMing Lei 60806f0e9e6SMing Lei /* 60906f0e9e6SMing Lei * bd_mutex has been held already in release path, so don't 61006f0e9e6SMing Lei * acquire it if this function is called in such case. 61106f0e9e6SMing Lei * 61206f0e9e6SMing Lei * If the reread partition isn't from release path, lo_refcnt 61306f0e9e6SMing Lei * must be at least one and it can only become zero when the 61406f0e9e6SMing Lei * current holder is released. 61506f0e9e6SMing Lei */ 61606f0e9e6SMing Lei if (!atomic_read(&lo->lo_refcnt)) 61706f0e9e6SMing Lei rc = __blkdev_reread_part(bdev); 61806f0e9e6SMing Lei else 61906f0e9e6SMing Lei rc = blkdev_reread_part(bdev); 62006f0e9e6SMing Lei if (rc) 62106f0e9e6SMing Lei pr_warn("%s: partition scan of loop%d (%s) failed (rc=%d)\n", 62206f0e9e6SMing Lei __func__, lo->lo_number, lo->lo_file_name, rc); 62306f0e9e6SMing Lei } 62406f0e9e6SMing Lei 6251da177e4SLinus Torvalds /* 6261da177e4SLinus Torvalds * loop_change_fd switched the backing store of a loopback device to 6271da177e4SLinus Torvalds * a new file. This is useful for operating system installers to free up 6281da177e4SLinus Torvalds * the original file and in High Availability environments to switch to 6291da177e4SLinus Torvalds * an alternative location for the content in case of server meltdown. 6301da177e4SLinus Torvalds * This can only work if the loop device is used read-only, and if the 6311da177e4SLinus Torvalds * new backing store is the same size and type as the old backing store. 6321da177e4SLinus Torvalds */ 633bb214884SAl Viro static int loop_change_fd(struct loop_device *lo, struct block_device *bdev, 634bb214884SAl Viro unsigned int arg) 6351da177e4SLinus Torvalds { 6361da177e4SLinus Torvalds struct file *file, *old_file; 6371da177e4SLinus Torvalds struct inode *inode; 6381da177e4SLinus Torvalds int error; 6391da177e4SLinus Torvalds 6401da177e4SLinus Torvalds error = -ENXIO; 6411da177e4SLinus Torvalds if (lo->lo_state != Lo_bound) 6421da177e4SLinus Torvalds goto out; 6431da177e4SLinus Torvalds 6441da177e4SLinus Torvalds /* the loop device has to be read-only */ 6451da177e4SLinus Torvalds error = -EINVAL; 6461da177e4SLinus Torvalds if (!(lo->lo_flags & LO_FLAGS_READ_ONLY)) 6471da177e4SLinus Torvalds goto out; 6481da177e4SLinus Torvalds 6491da177e4SLinus Torvalds error = -EBADF; 6501da177e4SLinus Torvalds file = fget(arg); 6511da177e4SLinus Torvalds if (!file) 6521da177e4SLinus Torvalds goto out; 6531da177e4SLinus Torvalds 6541da177e4SLinus Torvalds inode = file->f_mapping->host; 6551da177e4SLinus Torvalds old_file = lo->lo_backing_file; 6561da177e4SLinus Torvalds 6571da177e4SLinus Torvalds error = -EINVAL; 6581da177e4SLinus Torvalds 6591da177e4SLinus Torvalds if (!S_ISREG(inode->i_mode) && !S_ISBLK(inode->i_mode)) 6601da177e4SLinus Torvalds goto out_putf; 6611da177e4SLinus Torvalds 6621da177e4SLinus Torvalds /* size of the new backing store needs to be the same */ 6631da177e4SLinus Torvalds if (get_loop_size(lo, file) != get_loop_size(lo, old_file)) 6641da177e4SLinus Torvalds goto out_putf; 6651da177e4SLinus Torvalds 6661da177e4SLinus Torvalds /* and ... switch */ 66743cade80SOmar Sandoval blk_mq_freeze_queue(lo->lo_queue); 66843cade80SOmar Sandoval mapping_set_gfp_mask(old_file->f_mapping, lo->old_gfp_mask); 66943cade80SOmar Sandoval lo->lo_backing_file = file; 67043cade80SOmar Sandoval lo->old_gfp_mask = mapping_gfp_mask(file->f_mapping); 67143cade80SOmar Sandoval mapping_set_gfp_mask(file->f_mapping, 67243cade80SOmar Sandoval lo->old_gfp_mask & ~(__GFP_IO|__GFP_FS)); 67343cade80SOmar Sandoval loop_update_dio(lo); 67443cade80SOmar Sandoval blk_mq_unfreeze_queue(lo->lo_queue); 6751da177e4SLinus Torvalds 6761da177e4SLinus Torvalds fput(old_file); 677e03c8dd1SKay Sievers if (lo->lo_flags & LO_FLAGS_PARTSCAN) 67806f0e9e6SMing Lei loop_reread_partitions(lo, bdev); 6791da177e4SLinus Torvalds return 0; 6801da177e4SLinus Torvalds 6811da177e4SLinus Torvalds out_putf: 6821da177e4SLinus Torvalds fput(file); 6831da177e4SLinus Torvalds out: 6841da177e4SLinus Torvalds return error; 6851da177e4SLinus Torvalds } 6861da177e4SLinus Torvalds 6871da177e4SLinus Torvalds static inline int is_loop_device(struct file *file) 6881da177e4SLinus Torvalds { 6891da177e4SLinus Torvalds struct inode *i = file->f_mapping->host; 6901da177e4SLinus Torvalds 6911da177e4SLinus Torvalds return i && S_ISBLK(i->i_mode) && MAJOR(i->i_rdev) == LOOP_MAJOR; 6921da177e4SLinus Torvalds } 6931da177e4SLinus Torvalds 694ee862730SMilan Broz /* loop sysfs attributes */ 695ee862730SMilan Broz 696ee862730SMilan Broz static ssize_t loop_attr_show(struct device *dev, char *page, 697ee862730SMilan Broz ssize_t (*callback)(struct loop_device *, char *)) 698ee862730SMilan Broz { 69934dd82afSKay Sievers struct gendisk *disk = dev_to_disk(dev); 70034dd82afSKay Sievers struct loop_device *lo = disk->private_data; 701ee862730SMilan Broz 70234dd82afSKay Sievers return callback(lo, page); 703ee862730SMilan Broz } 704ee862730SMilan Broz 705ee862730SMilan Broz #define LOOP_ATTR_RO(_name) \ 706ee862730SMilan Broz static ssize_t loop_attr_##_name##_show(struct loop_device *, char *); \ 707ee862730SMilan Broz static ssize_t loop_attr_do_show_##_name(struct device *d, \ 708ee862730SMilan Broz struct device_attribute *attr, char *b) \ 709ee862730SMilan Broz { \ 710ee862730SMilan Broz return loop_attr_show(d, b, loop_attr_##_name##_show); \ 711ee862730SMilan Broz } \ 712ee862730SMilan Broz static struct device_attribute loop_attr_##_name = \ 713ee862730SMilan Broz __ATTR(_name, S_IRUGO, loop_attr_do_show_##_name, NULL); 714ee862730SMilan Broz 715ee862730SMilan Broz static ssize_t loop_attr_backing_file_show(struct loop_device *lo, char *buf) 716ee862730SMilan Broz { 717ee862730SMilan Broz ssize_t ret; 718ee862730SMilan Broz char *p = NULL; 719ee862730SMilan Broz 72005eb0f25SKay Sievers spin_lock_irq(&lo->lo_lock); 721ee862730SMilan Broz if (lo->lo_backing_file) 7229bf39ab2SMiklos Szeredi p = file_path(lo->lo_backing_file, buf, PAGE_SIZE - 1); 72305eb0f25SKay Sievers spin_unlock_irq(&lo->lo_lock); 724ee862730SMilan Broz 725ee862730SMilan Broz if (IS_ERR_OR_NULL(p)) 726ee862730SMilan Broz ret = PTR_ERR(p); 727ee862730SMilan Broz else { 728ee862730SMilan Broz ret = strlen(p); 729ee862730SMilan Broz memmove(buf, p, ret); 730ee862730SMilan Broz buf[ret++] = '\n'; 731ee862730SMilan Broz buf[ret] = 0; 732ee862730SMilan Broz } 733ee862730SMilan Broz 734ee862730SMilan Broz return ret; 735ee862730SMilan Broz } 736ee862730SMilan Broz 737ee862730SMilan Broz static ssize_t loop_attr_offset_show(struct loop_device *lo, char *buf) 738ee862730SMilan Broz { 739ee862730SMilan Broz return sprintf(buf, "%llu\n", (unsigned long long)lo->lo_offset); 740ee862730SMilan Broz } 741ee862730SMilan Broz 742ee862730SMilan Broz static ssize_t loop_attr_sizelimit_show(struct loop_device *lo, char *buf) 743ee862730SMilan Broz { 744ee862730SMilan Broz return sprintf(buf, "%llu\n", (unsigned long long)lo->lo_sizelimit); 745ee862730SMilan Broz } 746ee862730SMilan Broz 747ee862730SMilan Broz static ssize_t loop_attr_autoclear_show(struct loop_device *lo, char *buf) 748ee862730SMilan Broz { 749ee862730SMilan Broz int autoclear = (lo->lo_flags & LO_FLAGS_AUTOCLEAR); 750ee862730SMilan Broz 751ee862730SMilan Broz return sprintf(buf, "%s\n", autoclear ? "1" : "0"); 752ee862730SMilan Broz } 753ee862730SMilan Broz 754e03c8dd1SKay Sievers static ssize_t loop_attr_partscan_show(struct loop_device *lo, char *buf) 755e03c8dd1SKay Sievers { 756e03c8dd1SKay Sievers int partscan = (lo->lo_flags & LO_FLAGS_PARTSCAN); 757e03c8dd1SKay Sievers 758e03c8dd1SKay Sievers return sprintf(buf, "%s\n", partscan ? "1" : "0"); 759e03c8dd1SKay Sievers } 760e03c8dd1SKay Sievers 7612e5ab5f3SMing Lei static ssize_t loop_attr_dio_show(struct loop_device *lo, char *buf) 7622e5ab5f3SMing Lei { 7632e5ab5f3SMing Lei int dio = (lo->lo_flags & LO_FLAGS_DIRECT_IO); 7642e5ab5f3SMing Lei 7652e5ab5f3SMing Lei return sprintf(buf, "%s\n", dio ? "1" : "0"); 7662e5ab5f3SMing Lei } 7672e5ab5f3SMing Lei 768ee862730SMilan Broz LOOP_ATTR_RO(backing_file); 769ee862730SMilan Broz LOOP_ATTR_RO(offset); 770ee862730SMilan Broz LOOP_ATTR_RO(sizelimit); 771ee862730SMilan Broz LOOP_ATTR_RO(autoclear); 772e03c8dd1SKay Sievers LOOP_ATTR_RO(partscan); 7732e5ab5f3SMing Lei LOOP_ATTR_RO(dio); 774ee862730SMilan Broz 775ee862730SMilan Broz static struct attribute *loop_attrs[] = { 776ee862730SMilan Broz &loop_attr_backing_file.attr, 777ee862730SMilan Broz &loop_attr_offset.attr, 778ee862730SMilan Broz &loop_attr_sizelimit.attr, 779ee862730SMilan Broz &loop_attr_autoclear.attr, 780e03c8dd1SKay Sievers &loop_attr_partscan.attr, 7812e5ab5f3SMing Lei &loop_attr_dio.attr, 782ee862730SMilan Broz NULL, 783ee862730SMilan Broz }; 784ee862730SMilan Broz 785ee862730SMilan Broz static struct attribute_group loop_attribute_group = { 786ee862730SMilan Broz .name = "loop", 787ee862730SMilan Broz .attrs= loop_attrs, 788ee862730SMilan Broz }; 789ee862730SMilan Broz 790ee862730SMilan Broz static int loop_sysfs_init(struct loop_device *lo) 791ee862730SMilan Broz { 792ee862730SMilan Broz return sysfs_create_group(&disk_to_dev(lo->lo_disk)->kobj, 793ee862730SMilan Broz &loop_attribute_group); 794ee862730SMilan Broz } 795ee862730SMilan Broz 796ee862730SMilan Broz static void loop_sysfs_exit(struct loop_device *lo) 797ee862730SMilan Broz { 798ee862730SMilan Broz sysfs_remove_group(&disk_to_dev(lo->lo_disk)->kobj, 799ee862730SMilan Broz &loop_attribute_group); 800ee862730SMilan Broz } 801ee862730SMilan Broz 802dfaa2ef6SLukas Czerner static void loop_config_discard(struct loop_device *lo) 803dfaa2ef6SLukas Czerner { 804dfaa2ef6SLukas Czerner struct file *file = lo->lo_backing_file; 805dfaa2ef6SLukas Czerner struct inode *inode = file->f_mapping->host; 806dfaa2ef6SLukas Czerner struct request_queue *q = lo->lo_queue; 807dfaa2ef6SLukas Czerner 808dfaa2ef6SLukas Czerner /* 809dfaa2ef6SLukas Czerner * We use punch hole to reclaim the free space used by the 81012a64d2fSOlaf Hering * image a.k.a. discard. However we do not support discard if 811dfaa2ef6SLukas Czerner * encryption is enabled, because it may give an attacker 812dfaa2ef6SLukas Czerner * useful information. 813dfaa2ef6SLukas Czerner */ 814dfaa2ef6SLukas Czerner if ((!file->f_op->fallocate) || 815dfaa2ef6SLukas Czerner lo->lo_encrypt_key_size) { 816dfaa2ef6SLukas Czerner q->limits.discard_granularity = 0; 817dfaa2ef6SLukas Czerner q->limits.discard_alignment = 0; 8182bb4cd5cSJens Axboe blk_queue_max_discard_sectors(q, 0); 81919372e27SChristoph Hellwig blk_queue_max_write_zeroes_sectors(q, 0); 820dfaa2ef6SLukas Czerner queue_flag_clear_unlocked(QUEUE_FLAG_DISCARD, q); 821dfaa2ef6SLukas Czerner return; 822dfaa2ef6SLukas Czerner } 823dfaa2ef6SLukas Czerner 824dfaa2ef6SLukas Czerner q->limits.discard_granularity = inode->i_sb->s_blocksize; 825dfaf3c03SLukas Czerner q->limits.discard_alignment = 0; 826f2c6df7dSHannes Reinecke 8271e6ec9eaSOmar Sandoval blk_queue_max_discard_sectors(q, UINT_MAX >> 9); 8281e6ec9eaSOmar Sandoval blk_queue_max_write_zeroes_sectors(q, UINT_MAX >> 9); 829dfaa2ef6SLukas Czerner queue_flag_set_unlocked(QUEUE_FLAG_DISCARD, q); 830dfaa2ef6SLukas Czerner } 831dfaa2ef6SLukas Czerner 832e03a3d7aSMing Lei static void loop_unprepare_queue(struct loop_device *lo) 833e03a3d7aSMing Lei { 8343989144fSPetr Mladek kthread_flush_worker(&lo->worker); 835e03a3d7aSMing Lei kthread_stop(lo->worker_task); 836e03a3d7aSMing Lei } 837e03a3d7aSMing Lei 838b2ee7d46SNeilBrown static int loop_kthread_worker_fn(void *worker_ptr) 839b2ee7d46SNeilBrown { 840b2ee7d46SNeilBrown current->flags |= PF_LESS_THROTTLE; 841b2ee7d46SNeilBrown return kthread_worker_fn(worker_ptr); 842b2ee7d46SNeilBrown } 843b2ee7d46SNeilBrown 844e03a3d7aSMing Lei static int loop_prepare_queue(struct loop_device *lo) 845e03a3d7aSMing Lei { 8463989144fSPetr Mladek kthread_init_worker(&lo->worker); 847b2ee7d46SNeilBrown lo->worker_task = kthread_run(loop_kthread_worker_fn, 848e03a3d7aSMing Lei &lo->worker, "loop%d", lo->lo_number); 849e03a3d7aSMing Lei if (IS_ERR(lo->worker_task)) 850e03a3d7aSMing Lei return -ENOMEM; 851e03a3d7aSMing Lei set_user_nice(lo->worker_task, MIN_NICE); 852e03a3d7aSMing Lei return 0; 853e03a3d7aSMing Lei } 854e03a3d7aSMing Lei 855bb214884SAl Viro static int loop_set_fd(struct loop_device *lo, fmode_t mode, 8561da177e4SLinus Torvalds struct block_device *bdev, unsigned int arg) 8571da177e4SLinus Torvalds { 8581da177e4SLinus Torvalds struct file *file, *f; 8591da177e4SLinus Torvalds struct inode *inode; 8601da177e4SLinus Torvalds struct address_space *mapping; 8611da177e4SLinus Torvalds int lo_flags = 0; 8621da177e4SLinus Torvalds int error; 8631da177e4SLinus Torvalds loff_t size; 8641da177e4SLinus Torvalds 8651da177e4SLinus Torvalds /* This is safe, since we have a reference from open(). */ 8661da177e4SLinus Torvalds __module_get(THIS_MODULE); 8671da177e4SLinus Torvalds 8681da177e4SLinus Torvalds error = -EBADF; 8691da177e4SLinus Torvalds file = fget(arg); 8701da177e4SLinus Torvalds if (!file) 8711da177e4SLinus Torvalds goto out; 8721da177e4SLinus Torvalds 8731da177e4SLinus Torvalds error = -EBUSY; 8741da177e4SLinus Torvalds if (lo->lo_state != Lo_unbound) 8751da177e4SLinus Torvalds goto out_putf; 8761da177e4SLinus Torvalds 8771da177e4SLinus Torvalds /* Avoid recursion */ 8781da177e4SLinus Torvalds f = file; 8791da177e4SLinus Torvalds while (is_loop_device(f)) { 8801da177e4SLinus Torvalds struct loop_device *l; 8811da177e4SLinus Torvalds 882bb214884SAl Viro if (f->f_mapping->host->i_bdev == bdev) 8831da177e4SLinus Torvalds goto out_putf; 8841da177e4SLinus Torvalds 8851da177e4SLinus Torvalds l = f->f_mapping->host->i_bdev->bd_disk->private_data; 8861da177e4SLinus Torvalds if (l->lo_state == Lo_unbound) { 8871da177e4SLinus Torvalds error = -EINVAL; 8881da177e4SLinus Torvalds goto out_putf; 8891da177e4SLinus Torvalds } 8901da177e4SLinus Torvalds f = l->lo_backing_file; 8911da177e4SLinus Torvalds } 8921da177e4SLinus Torvalds 8931da177e4SLinus Torvalds mapping = file->f_mapping; 8941da177e4SLinus Torvalds inode = mapping->host; 8951da177e4SLinus Torvalds 8961da177e4SLinus Torvalds error = -EINVAL; 897456be148SChristoph Hellwig if (!S_ISREG(inode->i_mode) && !S_ISBLK(inode->i_mode)) 898456be148SChristoph Hellwig goto out_putf; 8996818173bSMiklos Szeredi 900456be148SChristoph Hellwig if (!(file->f_mode & FMODE_WRITE) || !(mode & FMODE_WRITE) || 901283e7e5dSAl Viro !file->f_op->write_iter) 9021da177e4SLinus Torvalds lo_flags |= LO_FLAGS_READ_ONLY; 9031da177e4SLinus Torvalds 9041da177e4SLinus Torvalds error = -EFBIG; 905456be148SChristoph Hellwig size = get_loop_size(lo, file); 906456be148SChristoph Hellwig if ((loff_t)(sector_t)size != size) 9071da177e4SLinus Torvalds goto out_putf; 908e03a3d7aSMing Lei error = loop_prepare_queue(lo); 909e03a3d7aSMing Lei if (error) 910f4aa4c7bSMing Lei goto out_putf; 9111da177e4SLinus Torvalds 912456be148SChristoph Hellwig error = 0; 9131da177e4SLinus Torvalds 9141da177e4SLinus Torvalds set_device_ro(bdev, (lo_flags & LO_FLAGS_READ_ONLY) != 0); 9151da177e4SLinus Torvalds 9162e5ab5f3SMing Lei lo->use_dio = false; 9171da177e4SLinus Torvalds lo->lo_device = bdev; 9181da177e4SLinus Torvalds lo->lo_flags = lo_flags; 9191da177e4SLinus Torvalds lo->lo_backing_file = file; 920aa4d8616SChristoph Hellwig lo->transfer = NULL; 9211da177e4SLinus Torvalds lo->ioctl = NULL; 9221da177e4SLinus Torvalds lo->lo_sizelimit = 0; 9231da177e4SLinus Torvalds lo->old_gfp_mask = mapping_gfp_mask(mapping); 9241da177e4SLinus Torvalds mapping_set_gfp_mask(mapping, lo->old_gfp_mask & ~(__GFP_IO|__GFP_FS)); 9251da177e4SLinus Torvalds 92668db1961SNikanth Karthikesan if (!(lo_flags & LO_FLAGS_READ_ONLY) && file->f_op->fsync) 92721d0727fSJens Axboe blk_queue_write_cache(lo->lo_queue, true, false); 92868db1961SNikanth Karthikesan 9292e5ab5f3SMing Lei loop_update_dio(lo); 93073285082SKen Chen set_capacity(lo->lo_disk, size); 9311da177e4SLinus Torvalds bd_set_size(bdev, size << 9); 932ee862730SMilan Broz loop_sysfs_init(lo); 933c3473c63SDavid Zeuthen /* let user-space know about the new size */ 934c3473c63SDavid Zeuthen kobject_uevent(&disk_to_dev(bdev->bd_disk)->kobj, KOBJ_CHANGE); 9351da177e4SLinus Torvalds 9368a0740c4SOmar Sandoval set_blocksize(bdev, S_ISBLK(inode->i_mode) ? 9378a0740c4SOmar Sandoval block_size(inode->i_bdev) : PAGE_SIZE); 9381da177e4SLinus Torvalds 9396c997918SSerge E. Hallyn lo->lo_state = Lo_bound; 940e03c8dd1SKay Sievers if (part_shift) 941e03c8dd1SKay Sievers lo->lo_flags |= LO_FLAGS_PARTSCAN; 942e03c8dd1SKay Sievers if (lo->lo_flags & LO_FLAGS_PARTSCAN) 94306f0e9e6SMing Lei loop_reread_partitions(lo, bdev); 944c1681bf8SAnatol Pomozov 945c1681bf8SAnatol Pomozov /* Grab the block_device to prevent its destruction after we 946c1681bf8SAnatol Pomozov * put /dev/loopXX inode. Later in loop_clr_fd() we bdput(bdev). 947c1681bf8SAnatol Pomozov */ 948c1681bf8SAnatol Pomozov bdgrab(bdev); 9491da177e4SLinus Torvalds return 0; 9501da177e4SLinus Torvalds 9511da177e4SLinus Torvalds out_putf: 9521da177e4SLinus Torvalds fput(file); 9531da177e4SLinus Torvalds out: 9541da177e4SLinus Torvalds /* This is safe: open() is still holding a reference. */ 9551da177e4SLinus Torvalds module_put(THIS_MODULE); 9561da177e4SLinus Torvalds return error; 9571da177e4SLinus Torvalds } 9581da177e4SLinus Torvalds 9591da177e4SLinus Torvalds static int 9601da177e4SLinus Torvalds loop_release_xfer(struct loop_device *lo) 9611da177e4SLinus Torvalds { 9621da177e4SLinus Torvalds int err = 0; 9631da177e4SLinus Torvalds struct loop_func_table *xfer = lo->lo_encryption; 9641da177e4SLinus Torvalds 9651da177e4SLinus Torvalds if (xfer) { 9661da177e4SLinus Torvalds if (xfer->release) 9671da177e4SLinus Torvalds err = xfer->release(lo); 9681da177e4SLinus Torvalds lo->transfer = NULL; 9691da177e4SLinus Torvalds lo->lo_encryption = NULL; 9701da177e4SLinus Torvalds module_put(xfer->owner); 9711da177e4SLinus Torvalds } 9721da177e4SLinus Torvalds return err; 9731da177e4SLinus Torvalds } 9741da177e4SLinus Torvalds 9751da177e4SLinus Torvalds static int 9761da177e4SLinus Torvalds loop_init_xfer(struct loop_device *lo, struct loop_func_table *xfer, 9771da177e4SLinus Torvalds const struct loop_info64 *i) 9781da177e4SLinus Torvalds { 9791da177e4SLinus Torvalds int err = 0; 9801da177e4SLinus Torvalds 9811da177e4SLinus Torvalds if (xfer) { 9821da177e4SLinus Torvalds struct module *owner = xfer->owner; 9831da177e4SLinus Torvalds 9841da177e4SLinus Torvalds if (!try_module_get(owner)) 9851da177e4SLinus Torvalds return -EINVAL; 9861da177e4SLinus Torvalds if (xfer->init) 9871da177e4SLinus Torvalds err = xfer->init(lo, i); 9881da177e4SLinus Torvalds if (err) 9891da177e4SLinus Torvalds module_put(owner); 9901da177e4SLinus Torvalds else 9911da177e4SLinus Torvalds lo->lo_encryption = xfer; 9921da177e4SLinus Torvalds } 9931da177e4SLinus Torvalds return err; 9941da177e4SLinus Torvalds } 9951da177e4SLinus Torvalds 9964c823cc3SAyan George static int loop_clr_fd(struct loop_device *lo) 9971da177e4SLinus Torvalds { 9981da177e4SLinus Torvalds struct file *filp = lo->lo_backing_file; 999b4e3ca1aSAl Viro gfp_t gfp = lo->old_gfp_mask; 10004c823cc3SAyan George struct block_device *bdev = lo->lo_device; 10011da177e4SLinus Torvalds 10021da177e4SLinus Torvalds if (lo->lo_state != Lo_bound) 10031da177e4SLinus Torvalds return -ENXIO; 10041da177e4SLinus Torvalds 1005a1ecac3bSDave Chinner /* 1006a1ecac3bSDave Chinner * If we've explicitly asked to tear down the loop device, 1007a1ecac3bSDave Chinner * and it has an elevated reference count, set it for auto-teardown when 1008a1ecac3bSDave Chinner * the last reference goes away. This stops $!~#$@ udev from 1009a1ecac3bSDave Chinner * preventing teardown because it decided that it needs to run blkid on 1010a1ecac3bSDave Chinner * the loopback device whenever they appear. xfstests is notorious for 1011a1ecac3bSDave Chinner * failing tests because blkid via udev races with a losetup 1012a1ecac3bSDave Chinner * <dev>/do something like mkfs/losetup -d <dev> causing the losetup -d 1013a1ecac3bSDave Chinner * command to fail with EBUSY. 1014a1ecac3bSDave Chinner */ 1015f8933667SMing Lei if (atomic_read(&lo->lo_refcnt) > 1) { 1016a1ecac3bSDave Chinner lo->lo_flags |= LO_FLAGS_AUTOCLEAR; 1017a1ecac3bSDave Chinner mutex_unlock(&lo->lo_ctl_mutex); 1018a1ecac3bSDave Chinner return 0; 1019a1ecac3bSDave Chinner } 10201da177e4SLinus Torvalds 10211da177e4SLinus Torvalds if (filp == NULL) 10221da177e4SLinus Torvalds return -EINVAL; 10231da177e4SLinus Torvalds 1024f8933667SMing Lei /* freeze request queue during the transition */ 1025f8933667SMing Lei blk_mq_freeze_queue(lo->lo_queue); 1026f8933667SMing Lei 10271da177e4SLinus Torvalds spin_lock_irq(&lo->lo_lock); 10281da177e4SLinus Torvalds lo->lo_state = Lo_rundown; 10291da177e4SLinus Torvalds lo->lo_backing_file = NULL; 103005eb0f25SKay Sievers spin_unlock_irq(&lo->lo_lock); 10311da177e4SLinus Torvalds 10321da177e4SLinus Torvalds loop_release_xfer(lo); 10331da177e4SLinus Torvalds lo->transfer = NULL; 10341da177e4SLinus Torvalds lo->ioctl = NULL; 10351da177e4SLinus Torvalds lo->lo_device = NULL; 10361da177e4SLinus Torvalds lo->lo_encryption = NULL; 10371da177e4SLinus Torvalds lo->lo_offset = 0; 10381da177e4SLinus Torvalds lo->lo_sizelimit = 0; 10391da177e4SLinus Torvalds lo->lo_encrypt_key_size = 0; 10401da177e4SLinus Torvalds memset(lo->lo_encrypt_key, 0, LO_KEY_SIZE); 10411da177e4SLinus Torvalds memset(lo->lo_crypt_name, 0, LO_NAME_SIZE); 10421da177e4SLinus Torvalds memset(lo->lo_file_name, 0, LO_NAME_SIZE); 104389e4fdecSOmar Sandoval blk_queue_logical_block_size(lo->lo_queue, 512); 1044bf093753SOmar Sandoval blk_queue_physical_block_size(lo->lo_queue, 512); 1045bf093753SOmar Sandoval blk_queue_io_min(lo->lo_queue, 512); 1046c1681bf8SAnatol Pomozov if (bdev) { 1047c1681bf8SAnatol Pomozov bdput(bdev); 1048f98393a6SPeter Zijlstra invalidate_bdev(bdev); 1049c1681bf8SAnatol Pomozov } 105073285082SKen Chen set_capacity(lo->lo_disk, 0); 105151a0bb0cSMilan Broz loop_sysfs_exit(lo); 1052c3473c63SDavid Zeuthen if (bdev) { 10531da177e4SLinus Torvalds bd_set_size(bdev, 0); 1054c3473c63SDavid Zeuthen /* let user-space know about this change */ 1055c3473c63SDavid Zeuthen kobject_uevent(&disk_to_dev(bdev->bd_disk)->kobj, KOBJ_CHANGE); 1056c3473c63SDavid Zeuthen } 10571da177e4SLinus Torvalds mapping_set_gfp_mask(filp->f_mapping, gfp); 10581da177e4SLinus Torvalds lo->lo_state = Lo_unbound; 10591da177e4SLinus Torvalds /* This is safe: open() is still holding a reference. */ 10601da177e4SLinus Torvalds module_put(THIS_MODULE); 1061f8933667SMing Lei blk_mq_unfreeze_queue(lo->lo_queue); 1062f8933667SMing Lei 1063c2fccc1cSJens Axboe if (lo->lo_flags & LO_FLAGS_PARTSCAN && bdev) 106406f0e9e6SMing Lei loop_reread_partitions(lo, bdev); 1065e03c8dd1SKay Sievers lo->lo_flags = 0; 1066e03c8dd1SKay Sievers if (!part_shift) 1067e03c8dd1SKay Sievers lo->lo_disk->flags |= GENHD_FL_NO_PART_SCAN; 1068e03a3d7aSMing Lei loop_unprepare_queue(lo); 1069f028f3b2SNikanth Karthikesan mutex_unlock(&lo->lo_ctl_mutex); 1070f028f3b2SNikanth Karthikesan /* 1071f028f3b2SNikanth Karthikesan * Need not hold lo_ctl_mutex to fput backing file. 1072f028f3b2SNikanth Karthikesan * Calling fput holding lo_ctl_mutex triggers a circular 1073f028f3b2SNikanth Karthikesan * lock dependency possibility warning as fput can take 1074f028f3b2SNikanth Karthikesan * bd_mutex which is usually taken before lo_ctl_mutex. 1075f028f3b2SNikanth Karthikesan */ 1076f028f3b2SNikanth Karthikesan fput(filp); 10771da177e4SLinus Torvalds return 0; 10781da177e4SLinus Torvalds } 10791da177e4SLinus Torvalds 10801da177e4SLinus Torvalds static int 10811da177e4SLinus Torvalds loop_set_status(struct loop_device *lo, const struct loop_info64 *info) 10821da177e4SLinus Torvalds { 10831da177e4SLinus Torvalds int err; 10841da177e4SLinus Torvalds struct loop_func_table *xfer; 1085e4849737SEric W. Biederman kuid_t uid = current_uid(); 10861da177e4SLinus Torvalds 1087b0fafa81SDavid Howells if (lo->lo_encrypt_key_size && 1088e4849737SEric W. Biederman !uid_eq(lo->lo_key_owner, uid) && 10891da177e4SLinus Torvalds !capable(CAP_SYS_ADMIN)) 10901da177e4SLinus Torvalds return -EPERM; 10911da177e4SLinus Torvalds if (lo->lo_state != Lo_bound) 10921da177e4SLinus Torvalds return -ENXIO; 10931da177e4SLinus Torvalds if ((unsigned int) info->lo_encrypt_key_size > LO_KEY_SIZE) 10941da177e4SLinus Torvalds return -EINVAL; 10951da177e4SLinus Torvalds 1096ecdd0959SMing Lei /* I/O need to be drained during transfer transition */ 1097ecdd0959SMing Lei blk_mq_freeze_queue(lo->lo_queue); 1098ecdd0959SMing Lei 10991da177e4SLinus Torvalds err = loop_release_xfer(lo); 11001da177e4SLinus Torvalds if (err) 1101ecdd0959SMing Lei goto exit; 11021da177e4SLinus Torvalds 11031da177e4SLinus Torvalds if (info->lo_encrypt_type) { 11041da177e4SLinus Torvalds unsigned int type = info->lo_encrypt_type; 11051da177e4SLinus Torvalds 11061da177e4SLinus Torvalds if (type >= MAX_LO_CRYPT) 11071da177e4SLinus Torvalds return -EINVAL; 11081da177e4SLinus Torvalds xfer = xfer_funcs[type]; 11091da177e4SLinus Torvalds if (xfer == NULL) 11101da177e4SLinus Torvalds return -EINVAL; 11111da177e4SLinus Torvalds } else 11121da177e4SLinus Torvalds xfer = NULL; 11131da177e4SLinus Torvalds 11141da177e4SLinus Torvalds err = loop_init_xfer(lo, xfer, info); 11151da177e4SLinus Torvalds if (err) 1116ecdd0959SMing Lei goto exit; 11171da177e4SLinus Torvalds 11181da177e4SLinus Torvalds if (lo->lo_offset != info->lo_offset || 11191e6ec9eaSOmar Sandoval lo->lo_sizelimit != info->lo_sizelimit) { 11201e6ec9eaSOmar Sandoval if (figure_loop_size(lo, info->lo_offset, info->lo_sizelimit)) { 1121ecdd0959SMing Lei err = -EFBIG; 1122ecdd0959SMing Lei goto exit; 1123ecdd0959SMing Lei } 1124b040ad9cSArnd Bergmann } 1125541c742aSGuo Chao 1126dfaa2ef6SLukas Czerner loop_config_discard(lo); 11271da177e4SLinus Torvalds 11281da177e4SLinus Torvalds memcpy(lo->lo_file_name, info->lo_file_name, LO_NAME_SIZE); 11291da177e4SLinus Torvalds memcpy(lo->lo_crypt_name, info->lo_crypt_name, LO_NAME_SIZE); 11301da177e4SLinus Torvalds lo->lo_file_name[LO_NAME_SIZE-1] = 0; 11311da177e4SLinus Torvalds lo->lo_crypt_name[LO_NAME_SIZE-1] = 0; 11321da177e4SLinus Torvalds 11331da177e4SLinus Torvalds if (!xfer) 11341da177e4SLinus Torvalds xfer = &none_funcs; 11351da177e4SLinus Torvalds lo->transfer = xfer->transfer; 11361da177e4SLinus Torvalds lo->ioctl = xfer->ioctl; 11371da177e4SLinus Torvalds 113896c58655SDavid Woodhouse if ((lo->lo_flags & LO_FLAGS_AUTOCLEAR) != 113996c58655SDavid Woodhouse (info->lo_flags & LO_FLAGS_AUTOCLEAR)) 114096c58655SDavid Woodhouse lo->lo_flags ^= LO_FLAGS_AUTOCLEAR; 114196c58655SDavid Woodhouse 11421da177e4SLinus Torvalds lo->lo_encrypt_key_size = info->lo_encrypt_key_size; 11431da177e4SLinus Torvalds lo->lo_init[0] = info->lo_init[0]; 11441da177e4SLinus Torvalds lo->lo_init[1] = info->lo_init[1]; 11451da177e4SLinus Torvalds if (info->lo_encrypt_key_size) { 11461da177e4SLinus Torvalds memcpy(lo->lo_encrypt_key, info->lo_encrypt_key, 11471da177e4SLinus Torvalds info->lo_encrypt_key_size); 1148b0fafa81SDavid Howells lo->lo_key_owner = uid; 11491da177e4SLinus Torvalds } 11501da177e4SLinus Torvalds 11512e5ab5f3SMing Lei /* update dio if lo_offset or transfer is changed */ 11522e5ab5f3SMing Lei __loop_update_dio(lo, lo->use_dio); 11532e5ab5f3SMing Lei 1154ecdd0959SMing Lei exit: 1155ecdd0959SMing Lei blk_mq_unfreeze_queue(lo->lo_queue); 1156e02898b4SOmar Sandoval 1157e02898b4SOmar Sandoval if (!err && (info->lo_flags & LO_FLAGS_PARTSCAN) && 1158e02898b4SOmar Sandoval !(lo->lo_flags & LO_FLAGS_PARTSCAN)) { 1159e02898b4SOmar Sandoval lo->lo_flags |= LO_FLAGS_PARTSCAN; 1160e02898b4SOmar Sandoval lo->lo_disk->flags &= ~GENHD_FL_NO_PART_SCAN; 1161e02898b4SOmar Sandoval loop_reread_partitions(lo, lo->lo_device); 1162e02898b4SOmar Sandoval } 1163e02898b4SOmar Sandoval 1164ecdd0959SMing Lei return err; 11651da177e4SLinus Torvalds } 11661da177e4SLinus Torvalds 11671da177e4SLinus Torvalds static int 11681da177e4SLinus Torvalds loop_get_status(struct loop_device *lo, struct loop_info64 *info) 11691da177e4SLinus Torvalds { 11701da177e4SLinus Torvalds struct file *file = lo->lo_backing_file; 11711da177e4SLinus Torvalds struct kstat stat; 11721da177e4SLinus Torvalds int error; 11731da177e4SLinus Torvalds 11741da177e4SLinus Torvalds if (lo->lo_state != Lo_bound) 11751da177e4SLinus Torvalds return -ENXIO; 1176a528d35eSDavid Howells error = vfs_getattr(&file->f_path, &stat, 1177a528d35eSDavid Howells STATX_INO, AT_STATX_SYNC_AS_STAT); 11781da177e4SLinus Torvalds if (error) 11791da177e4SLinus Torvalds return error; 11801da177e4SLinus Torvalds memset(info, 0, sizeof(*info)); 11811da177e4SLinus Torvalds info->lo_number = lo->lo_number; 11821da177e4SLinus Torvalds info->lo_device = huge_encode_dev(stat.dev); 11831da177e4SLinus Torvalds info->lo_inode = stat.ino; 11841da177e4SLinus Torvalds info->lo_rdevice = huge_encode_dev(lo->lo_device ? stat.rdev : stat.dev); 11851da177e4SLinus Torvalds info->lo_offset = lo->lo_offset; 11861da177e4SLinus Torvalds info->lo_sizelimit = lo->lo_sizelimit; 11871da177e4SLinus Torvalds info->lo_flags = lo->lo_flags; 11881da177e4SLinus Torvalds memcpy(info->lo_file_name, lo->lo_file_name, LO_NAME_SIZE); 11891da177e4SLinus Torvalds memcpy(info->lo_crypt_name, lo->lo_crypt_name, LO_NAME_SIZE); 11901da177e4SLinus Torvalds info->lo_encrypt_type = 11911da177e4SLinus Torvalds lo->lo_encryption ? lo->lo_encryption->number : 0; 11921da177e4SLinus Torvalds if (lo->lo_encrypt_key_size && capable(CAP_SYS_ADMIN)) { 11931da177e4SLinus Torvalds info->lo_encrypt_key_size = lo->lo_encrypt_key_size; 11941da177e4SLinus Torvalds memcpy(info->lo_encrypt_key, lo->lo_encrypt_key, 11951da177e4SLinus Torvalds lo->lo_encrypt_key_size); 11961da177e4SLinus Torvalds } 11971da177e4SLinus Torvalds return 0; 11981da177e4SLinus Torvalds } 11991da177e4SLinus Torvalds 12001da177e4SLinus Torvalds static void 12011da177e4SLinus Torvalds loop_info64_from_old(const struct loop_info *info, struct loop_info64 *info64) 12021da177e4SLinus Torvalds { 12031da177e4SLinus Torvalds memset(info64, 0, sizeof(*info64)); 12041da177e4SLinus Torvalds info64->lo_number = info->lo_number; 12051da177e4SLinus Torvalds info64->lo_device = info->lo_device; 12061da177e4SLinus Torvalds info64->lo_inode = info->lo_inode; 12071da177e4SLinus Torvalds info64->lo_rdevice = info->lo_rdevice; 12081da177e4SLinus Torvalds info64->lo_offset = info->lo_offset; 12091da177e4SLinus Torvalds info64->lo_sizelimit = 0; 12101da177e4SLinus Torvalds info64->lo_encrypt_type = info->lo_encrypt_type; 12111da177e4SLinus Torvalds info64->lo_encrypt_key_size = info->lo_encrypt_key_size; 12121da177e4SLinus Torvalds info64->lo_flags = info->lo_flags; 12131da177e4SLinus Torvalds info64->lo_init[0] = info->lo_init[0]; 12141da177e4SLinus Torvalds info64->lo_init[1] = info->lo_init[1]; 12151da177e4SLinus Torvalds if (info->lo_encrypt_type == LO_CRYPT_CRYPTOAPI) 12161da177e4SLinus Torvalds memcpy(info64->lo_crypt_name, info->lo_name, LO_NAME_SIZE); 12171da177e4SLinus Torvalds else 12181da177e4SLinus Torvalds memcpy(info64->lo_file_name, info->lo_name, LO_NAME_SIZE); 12191da177e4SLinus Torvalds memcpy(info64->lo_encrypt_key, info->lo_encrypt_key, LO_KEY_SIZE); 12201da177e4SLinus Torvalds } 12211da177e4SLinus Torvalds 12221da177e4SLinus Torvalds static int 12231da177e4SLinus Torvalds loop_info64_to_old(const struct loop_info64 *info64, struct loop_info *info) 12241da177e4SLinus Torvalds { 12251da177e4SLinus Torvalds memset(info, 0, sizeof(*info)); 12261da177e4SLinus Torvalds info->lo_number = info64->lo_number; 12271da177e4SLinus Torvalds info->lo_device = info64->lo_device; 12281da177e4SLinus Torvalds info->lo_inode = info64->lo_inode; 12291da177e4SLinus Torvalds info->lo_rdevice = info64->lo_rdevice; 12301da177e4SLinus Torvalds info->lo_offset = info64->lo_offset; 12311da177e4SLinus Torvalds info->lo_encrypt_type = info64->lo_encrypt_type; 12321da177e4SLinus Torvalds info->lo_encrypt_key_size = info64->lo_encrypt_key_size; 12331da177e4SLinus Torvalds info->lo_flags = info64->lo_flags; 12341da177e4SLinus Torvalds info->lo_init[0] = info64->lo_init[0]; 12351da177e4SLinus Torvalds info->lo_init[1] = info64->lo_init[1]; 12361da177e4SLinus Torvalds if (info->lo_encrypt_type == LO_CRYPT_CRYPTOAPI) 12371da177e4SLinus Torvalds memcpy(info->lo_name, info64->lo_crypt_name, LO_NAME_SIZE); 12381da177e4SLinus Torvalds else 12391da177e4SLinus Torvalds memcpy(info->lo_name, info64->lo_file_name, LO_NAME_SIZE); 12401da177e4SLinus Torvalds memcpy(info->lo_encrypt_key, info64->lo_encrypt_key, LO_KEY_SIZE); 12411da177e4SLinus Torvalds 12421da177e4SLinus Torvalds /* error in case values were truncated */ 12431da177e4SLinus Torvalds if (info->lo_device != info64->lo_device || 12441da177e4SLinus Torvalds info->lo_rdevice != info64->lo_rdevice || 12451da177e4SLinus Torvalds info->lo_inode != info64->lo_inode || 12461da177e4SLinus Torvalds info->lo_offset != info64->lo_offset) 12471da177e4SLinus Torvalds return -EOVERFLOW; 12481da177e4SLinus Torvalds 12491da177e4SLinus Torvalds return 0; 12501da177e4SLinus Torvalds } 12511da177e4SLinus Torvalds 12521da177e4SLinus Torvalds static int 12531da177e4SLinus Torvalds loop_set_status_old(struct loop_device *lo, const struct loop_info __user *arg) 12541da177e4SLinus Torvalds { 12551da177e4SLinus Torvalds struct loop_info info; 12561da177e4SLinus Torvalds struct loop_info64 info64; 12571da177e4SLinus Torvalds 12581da177e4SLinus Torvalds if (copy_from_user(&info, arg, sizeof (struct loop_info))) 12591da177e4SLinus Torvalds return -EFAULT; 12601da177e4SLinus Torvalds loop_info64_from_old(&info, &info64); 12611da177e4SLinus Torvalds return loop_set_status(lo, &info64); 12621da177e4SLinus Torvalds } 12631da177e4SLinus Torvalds 12641da177e4SLinus Torvalds static int 12651da177e4SLinus Torvalds loop_set_status64(struct loop_device *lo, const struct loop_info64 __user *arg) 12661da177e4SLinus Torvalds { 12671da177e4SLinus Torvalds struct loop_info64 info64; 12681da177e4SLinus Torvalds 12691da177e4SLinus Torvalds if (copy_from_user(&info64, arg, sizeof (struct loop_info64))) 12701da177e4SLinus Torvalds return -EFAULT; 12711da177e4SLinus Torvalds return loop_set_status(lo, &info64); 12721da177e4SLinus Torvalds } 12731da177e4SLinus Torvalds 12741da177e4SLinus Torvalds static int 12751da177e4SLinus Torvalds loop_get_status_old(struct loop_device *lo, struct loop_info __user *arg) { 12761da177e4SLinus Torvalds struct loop_info info; 12771da177e4SLinus Torvalds struct loop_info64 info64; 12781da177e4SLinus Torvalds int err = 0; 12791da177e4SLinus Torvalds 12801da177e4SLinus Torvalds if (!arg) 12811da177e4SLinus Torvalds err = -EINVAL; 12821da177e4SLinus Torvalds if (!err) 12831da177e4SLinus Torvalds err = loop_get_status(lo, &info64); 12841da177e4SLinus Torvalds if (!err) 12851da177e4SLinus Torvalds err = loop_info64_to_old(&info64, &info); 12861da177e4SLinus Torvalds if (!err && copy_to_user(arg, &info, sizeof(info))) 12871da177e4SLinus Torvalds err = -EFAULT; 12881da177e4SLinus Torvalds 12891da177e4SLinus Torvalds return err; 12901da177e4SLinus Torvalds } 12911da177e4SLinus Torvalds 12921da177e4SLinus Torvalds static int 12931da177e4SLinus Torvalds loop_get_status64(struct loop_device *lo, struct loop_info64 __user *arg) { 12941da177e4SLinus Torvalds struct loop_info64 info64; 12951da177e4SLinus Torvalds int err = 0; 12961da177e4SLinus Torvalds 12971da177e4SLinus Torvalds if (!arg) 12981da177e4SLinus Torvalds err = -EINVAL; 12991da177e4SLinus Torvalds if (!err) 13001da177e4SLinus Torvalds err = loop_get_status(lo, &info64); 13011da177e4SLinus Torvalds if (!err && copy_to_user(arg, &info64, sizeof(info64))) 13021da177e4SLinus Torvalds err = -EFAULT; 13031da177e4SLinus Torvalds 13041da177e4SLinus Torvalds return err; 13051da177e4SLinus Torvalds } 13061da177e4SLinus Torvalds 130751001b7dSHannes Reinecke static int loop_set_capacity(struct loop_device *lo) 130853d66608SJ. R. Okajima { 130953d66608SJ. R. Okajima if (unlikely(lo->lo_state != Lo_bound)) 13107b0576a3SGuo Chao return -ENXIO; 131153d66608SJ. R. Okajima 13121e6ec9eaSOmar Sandoval return figure_loop_size(lo, lo->lo_offset, lo->lo_sizelimit); 131353d66608SJ. R. Okajima } 131453d66608SJ. R. Okajima 1315ab1cb278SMing Lei static int loop_set_dio(struct loop_device *lo, unsigned long arg) 1316ab1cb278SMing Lei { 1317ab1cb278SMing Lei int error = -ENXIO; 1318ab1cb278SMing Lei if (lo->lo_state != Lo_bound) 1319ab1cb278SMing Lei goto out; 1320ab1cb278SMing Lei 1321ab1cb278SMing Lei __loop_update_dio(lo, !!arg); 1322ab1cb278SMing Lei if (lo->use_dio == !!arg) 1323ab1cb278SMing Lei return 0; 1324ab1cb278SMing Lei error = -EINVAL; 1325ab1cb278SMing Lei out: 1326ab1cb278SMing Lei return error; 1327ab1cb278SMing Lei } 1328ab1cb278SMing Lei 132989e4fdecSOmar Sandoval static int loop_set_block_size(struct loop_device *lo, unsigned long arg) 133089e4fdecSOmar Sandoval { 133189e4fdecSOmar Sandoval if (lo->lo_state != Lo_bound) 133289e4fdecSOmar Sandoval return -ENXIO; 133389e4fdecSOmar Sandoval 133489e4fdecSOmar Sandoval if (arg < 512 || arg > PAGE_SIZE || !is_power_of_2(arg)) 133589e4fdecSOmar Sandoval return -EINVAL; 133689e4fdecSOmar Sandoval 133789e4fdecSOmar Sandoval blk_mq_freeze_queue(lo->lo_queue); 133889e4fdecSOmar Sandoval 133989e4fdecSOmar Sandoval blk_queue_logical_block_size(lo->lo_queue, arg); 1340bf093753SOmar Sandoval blk_queue_physical_block_size(lo->lo_queue, arg); 1341bf093753SOmar Sandoval blk_queue_io_min(lo->lo_queue, arg); 134289e4fdecSOmar Sandoval loop_update_dio(lo); 134389e4fdecSOmar Sandoval 134489e4fdecSOmar Sandoval blk_mq_unfreeze_queue(lo->lo_queue); 134589e4fdecSOmar Sandoval 134689e4fdecSOmar Sandoval return 0; 134789e4fdecSOmar Sandoval } 134889e4fdecSOmar Sandoval 1349bb214884SAl Viro static int lo_ioctl(struct block_device *bdev, fmode_t mode, 13501da177e4SLinus Torvalds unsigned int cmd, unsigned long arg) 13511da177e4SLinus Torvalds { 1352bb214884SAl Viro struct loop_device *lo = bdev->bd_disk->private_data; 13531da177e4SLinus Torvalds int err; 13541da177e4SLinus Torvalds 1355f028f3b2SNikanth Karthikesan mutex_lock_nested(&lo->lo_ctl_mutex, 1); 13561da177e4SLinus Torvalds switch (cmd) { 13571da177e4SLinus Torvalds case LOOP_SET_FD: 1358bb214884SAl Viro err = loop_set_fd(lo, mode, bdev, arg); 13591da177e4SLinus Torvalds break; 13601da177e4SLinus Torvalds case LOOP_CHANGE_FD: 1361bb214884SAl Viro err = loop_change_fd(lo, bdev, arg); 13621da177e4SLinus Torvalds break; 13631da177e4SLinus Torvalds case LOOP_CLR_FD: 1364f028f3b2SNikanth Karthikesan /* loop_clr_fd would have unlocked lo_ctl_mutex on success */ 13654c823cc3SAyan George err = loop_clr_fd(lo); 1366f028f3b2SNikanth Karthikesan if (!err) 1367f028f3b2SNikanth Karthikesan goto out_unlocked; 13681da177e4SLinus Torvalds break; 13691da177e4SLinus Torvalds case LOOP_SET_STATUS: 13707035b5dfSDmitry Monakhov err = -EPERM; 13717035b5dfSDmitry Monakhov if ((mode & FMODE_WRITE) || capable(CAP_SYS_ADMIN)) 13727035b5dfSDmitry Monakhov err = loop_set_status_old(lo, 13737035b5dfSDmitry Monakhov (struct loop_info __user *)arg); 13741da177e4SLinus Torvalds break; 13751da177e4SLinus Torvalds case LOOP_GET_STATUS: 13761da177e4SLinus Torvalds err = loop_get_status_old(lo, (struct loop_info __user *) arg); 13771da177e4SLinus Torvalds break; 13781da177e4SLinus Torvalds case LOOP_SET_STATUS64: 13797035b5dfSDmitry Monakhov err = -EPERM; 13807035b5dfSDmitry Monakhov if ((mode & FMODE_WRITE) || capable(CAP_SYS_ADMIN)) 13817035b5dfSDmitry Monakhov err = loop_set_status64(lo, 13827035b5dfSDmitry Monakhov (struct loop_info64 __user *) arg); 13831da177e4SLinus Torvalds break; 13841da177e4SLinus Torvalds case LOOP_GET_STATUS64: 13851da177e4SLinus Torvalds err = loop_get_status64(lo, (struct loop_info64 __user *) arg); 13861da177e4SLinus Torvalds break; 138753d66608SJ. R. Okajima case LOOP_SET_CAPACITY: 138853d66608SJ. R. Okajima err = -EPERM; 138953d66608SJ. R. Okajima if ((mode & FMODE_WRITE) || capable(CAP_SYS_ADMIN)) 139051001b7dSHannes Reinecke err = loop_set_capacity(lo); 139153d66608SJ. R. Okajima break; 1392ab1cb278SMing Lei case LOOP_SET_DIRECT_IO: 1393ab1cb278SMing Lei err = -EPERM; 1394ab1cb278SMing Lei if ((mode & FMODE_WRITE) || capable(CAP_SYS_ADMIN)) 1395ab1cb278SMing Lei err = loop_set_dio(lo, arg); 1396ab1cb278SMing Lei break; 139789e4fdecSOmar Sandoval case LOOP_SET_BLOCK_SIZE: 139889e4fdecSOmar Sandoval err = -EPERM; 139989e4fdecSOmar Sandoval if ((mode & FMODE_WRITE) || capable(CAP_SYS_ADMIN)) 140089e4fdecSOmar Sandoval err = loop_set_block_size(lo, arg); 140189e4fdecSOmar Sandoval break; 14021da177e4SLinus Torvalds default: 14031da177e4SLinus Torvalds err = lo->ioctl ? lo->ioctl(lo, cmd, arg) : -EINVAL; 14041da177e4SLinus Torvalds } 1405f85221ddSIngo Molnar mutex_unlock(&lo->lo_ctl_mutex); 1406f028f3b2SNikanth Karthikesan 1407f028f3b2SNikanth Karthikesan out_unlocked: 14081da177e4SLinus Torvalds return err; 14091da177e4SLinus Torvalds } 14101da177e4SLinus Torvalds 1411863d5b82SDavid Howells #ifdef CONFIG_COMPAT 1412863d5b82SDavid Howells struct compat_loop_info { 1413863d5b82SDavid Howells compat_int_t lo_number; /* ioctl r/o */ 1414863d5b82SDavid Howells compat_dev_t lo_device; /* ioctl r/o */ 1415863d5b82SDavid Howells compat_ulong_t lo_inode; /* ioctl r/o */ 1416863d5b82SDavid Howells compat_dev_t lo_rdevice; /* ioctl r/o */ 1417863d5b82SDavid Howells compat_int_t lo_offset; 1418863d5b82SDavid Howells compat_int_t lo_encrypt_type; 1419863d5b82SDavid Howells compat_int_t lo_encrypt_key_size; /* ioctl w/o */ 1420863d5b82SDavid Howells compat_int_t lo_flags; /* ioctl r/o */ 1421863d5b82SDavid Howells char lo_name[LO_NAME_SIZE]; 1422863d5b82SDavid Howells unsigned char lo_encrypt_key[LO_KEY_SIZE]; /* ioctl w/o */ 1423863d5b82SDavid Howells compat_ulong_t lo_init[2]; 1424863d5b82SDavid Howells char reserved[4]; 1425863d5b82SDavid Howells }; 1426863d5b82SDavid Howells 1427863d5b82SDavid Howells /* 1428863d5b82SDavid Howells * Transfer 32-bit compatibility structure in userspace to 64-bit loop info 1429863d5b82SDavid Howells * - noinlined to reduce stack space usage in main part of driver 1430863d5b82SDavid Howells */ 1431863d5b82SDavid Howells static noinline int 1432ba674cfcSAl Viro loop_info64_from_compat(const struct compat_loop_info __user *arg, 1433863d5b82SDavid Howells struct loop_info64 *info64) 1434863d5b82SDavid Howells { 1435863d5b82SDavid Howells struct compat_loop_info info; 1436863d5b82SDavid Howells 1437863d5b82SDavid Howells if (copy_from_user(&info, arg, sizeof(info))) 1438863d5b82SDavid Howells return -EFAULT; 1439863d5b82SDavid Howells 1440863d5b82SDavid Howells memset(info64, 0, sizeof(*info64)); 1441863d5b82SDavid Howells info64->lo_number = info.lo_number; 1442863d5b82SDavid Howells info64->lo_device = info.lo_device; 1443863d5b82SDavid Howells info64->lo_inode = info.lo_inode; 1444863d5b82SDavid Howells info64->lo_rdevice = info.lo_rdevice; 1445863d5b82SDavid Howells info64->lo_offset = info.lo_offset; 1446863d5b82SDavid Howells info64->lo_sizelimit = 0; 1447863d5b82SDavid Howells info64->lo_encrypt_type = info.lo_encrypt_type; 1448863d5b82SDavid Howells info64->lo_encrypt_key_size = info.lo_encrypt_key_size; 1449863d5b82SDavid Howells info64->lo_flags = info.lo_flags; 1450863d5b82SDavid Howells info64->lo_init[0] = info.lo_init[0]; 1451863d5b82SDavid Howells info64->lo_init[1] = info.lo_init[1]; 1452863d5b82SDavid Howells if (info.lo_encrypt_type == LO_CRYPT_CRYPTOAPI) 1453863d5b82SDavid Howells memcpy(info64->lo_crypt_name, info.lo_name, LO_NAME_SIZE); 1454863d5b82SDavid Howells else 1455863d5b82SDavid Howells memcpy(info64->lo_file_name, info.lo_name, LO_NAME_SIZE); 1456863d5b82SDavid Howells memcpy(info64->lo_encrypt_key, info.lo_encrypt_key, LO_KEY_SIZE); 1457863d5b82SDavid Howells return 0; 1458863d5b82SDavid Howells } 1459863d5b82SDavid Howells 1460863d5b82SDavid Howells /* 1461863d5b82SDavid Howells * Transfer 64-bit loop info to 32-bit compatibility structure in userspace 1462863d5b82SDavid Howells * - noinlined to reduce stack space usage in main part of driver 1463863d5b82SDavid Howells */ 1464863d5b82SDavid Howells static noinline int 1465863d5b82SDavid Howells loop_info64_to_compat(const struct loop_info64 *info64, 1466863d5b82SDavid Howells struct compat_loop_info __user *arg) 1467863d5b82SDavid Howells { 1468863d5b82SDavid Howells struct compat_loop_info info; 1469863d5b82SDavid Howells 1470863d5b82SDavid Howells memset(&info, 0, sizeof(info)); 1471863d5b82SDavid Howells info.lo_number = info64->lo_number; 1472863d5b82SDavid Howells info.lo_device = info64->lo_device; 1473863d5b82SDavid Howells info.lo_inode = info64->lo_inode; 1474863d5b82SDavid Howells info.lo_rdevice = info64->lo_rdevice; 1475863d5b82SDavid Howells info.lo_offset = info64->lo_offset; 1476863d5b82SDavid Howells info.lo_encrypt_type = info64->lo_encrypt_type; 1477863d5b82SDavid Howells info.lo_encrypt_key_size = info64->lo_encrypt_key_size; 1478863d5b82SDavid Howells info.lo_flags = info64->lo_flags; 1479863d5b82SDavid Howells info.lo_init[0] = info64->lo_init[0]; 1480863d5b82SDavid Howells info.lo_init[1] = info64->lo_init[1]; 1481863d5b82SDavid Howells if (info.lo_encrypt_type == LO_CRYPT_CRYPTOAPI) 1482863d5b82SDavid Howells memcpy(info.lo_name, info64->lo_crypt_name, LO_NAME_SIZE); 1483863d5b82SDavid Howells else 1484863d5b82SDavid Howells memcpy(info.lo_name, info64->lo_file_name, LO_NAME_SIZE); 1485863d5b82SDavid Howells memcpy(info.lo_encrypt_key, info64->lo_encrypt_key, LO_KEY_SIZE); 1486863d5b82SDavid Howells 1487863d5b82SDavid Howells /* error in case values were truncated */ 1488863d5b82SDavid Howells if (info.lo_device != info64->lo_device || 1489863d5b82SDavid Howells info.lo_rdevice != info64->lo_rdevice || 1490863d5b82SDavid Howells info.lo_inode != info64->lo_inode || 1491863d5b82SDavid Howells info.lo_offset != info64->lo_offset || 1492863d5b82SDavid Howells info.lo_init[0] != info64->lo_init[0] || 1493863d5b82SDavid Howells info.lo_init[1] != info64->lo_init[1]) 1494863d5b82SDavid Howells return -EOVERFLOW; 1495863d5b82SDavid Howells 1496863d5b82SDavid Howells if (copy_to_user(arg, &info, sizeof(info))) 1497863d5b82SDavid Howells return -EFAULT; 1498863d5b82SDavid Howells return 0; 1499863d5b82SDavid Howells } 1500863d5b82SDavid Howells 1501863d5b82SDavid Howells static int 1502863d5b82SDavid Howells loop_set_status_compat(struct loop_device *lo, 1503863d5b82SDavid Howells const struct compat_loop_info __user *arg) 1504863d5b82SDavid Howells { 1505863d5b82SDavid Howells struct loop_info64 info64; 1506863d5b82SDavid Howells int ret; 1507863d5b82SDavid Howells 1508863d5b82SDavid Howells ret = loop_info64_from_compat(arg, &info64); 1509863d5b82SDavid Howells if (ret < 0) 1510863d5b82SDavid Howells return ret; 1511863d5b82SDavid Howells return loop_set_status(lo, &info64); 1512863d5b82SDavid Howells } 1513863d5b82SDavid Howells 1514863d5b82SDavid Howells static int 1515863d5b82SDavid Howells loop_get_status_compat(struct loop_device *lo, 1516863d5b82SDavid Howells struct compat_loop_info __user *arg) 1517863d5b82SDavid Howells { 1518863d5b82SDavid Howells struct loop_info64 info64; 1519863d5b82SDavid Howells int err = 0; 1520863d5b82SDavid Howells 1521863d5b82SDavid Howells if (!arg) 1522863d5b82SDavid Howells err = -EINVAL; 1523863d5b82SDavid Howells if (!err) 1524863d5b82SDavid Howells err = loop_get_status(lo, &info64); 1525863d5b82SDavid Howells if (!err) 1526863d5b82SDavid Howells err = loop_info64_to_compat(&info64, arg); 1527863d5b82SDavid Howells return err; 1528863d5b82SDavid Howells } 1529863d5b82SDavid Howells 1530bb214884SAl Viro static int lo_compat_ioctl(struct block_device *bdev, fmode_t mode, 1531bb214884SAl Viro unsigned int cmd, unsigned long arg) 1532863d5b82SDavid Howells { 1533bb214884SAl Viro struct loop_device *lo = bdev->bd_disk->private_data; 1534863d5b82SDavid Howells int err; 1535863d5b82SDavid Howells 1536863d5b82SDavid Howells switch(cmd) { 1537863d5b82SDavid Howells case LOOP_SET_STATUS: 1538863d5b82SDavid Howells mutex_lock(&lo->lo_ctl_mutex); 1539863d5b82SDavid Howells err = loop_set_status_compat( 1540863d5b82SDavid Howells lo, (const struct compat_loop_info __user *) arg); 1541863d5b82SDavid Howells mutex_unlock(&lo->lo_ctl_mutex); 1542863d5b82SDavid Howells break; 1543863d5b82SDavid Howells case LOOP_GET_STATUS: 1544863d5b82SDavid Howells mutex_lock(&lo->lo_ctl_mutex); 1545863d5b82SDavid Howells err = loop_get_status_compat( 1546863d5b82SDavid Howells lo, (struct compat_loop_info __user *) arg); 1547863d5b82SDavid Howells mutex_unlock(&lo->lo_ctl_mutex); 1548863d5b82SDavid Howells break; 154953d66608SJ. R. Okajima case LOOP_SET_CAPACITY: 1550863d5b82SDavid Howells case LOOP_CLR_FD: 1551863d5b82SDavid Howells case LOOP_GET_STATUS64: 1552863d5b82SDavid Howells case LOOP_SET_STATUS64: 1553863d5b82SDavid Howells arg = (unsigned long) compat_ptr(arg); 1554863d5b82SDavid Howells case LOOP_SET_FD: 1555863d5b82SDavid Howells case LOOP_CHANGE_FD: 1556bb214884SAl Viro err = lo_ioctl(bdev, mode, cmd, arg); 1557863d5b82SDavid Howells break; 1558863d5b82SDavid Howells default: 1559863d5b82SDavid Howells err = -ENOIOCTLCMD; 1560863d5b82SDavid Howells break; 1561863d5b82SDavid Howells } 1562863d5b82SDavid Howells return err; 1563863d5b82SDavid Howells } 1564863d5b82SDavid Howells #endif 1565863d5b82SDavid Howells 1566bb214884SAl Viro static int lo_open(struct block_device *bdev, fmode_t mode) 15671da177e4SLinus Torvalds { 1568770fe30aSKay Sievers struct loop_device *lo; 1569770fe30aSKay Sievers int err = 0; 1570770fe30aSKay Sievers 1571770fe30aSKay Sievers mutex_lock(&loop_index_mutex); 1572770fe30aSKay Sievers lo = bdev->bd_disk->private_data; 1573770fe30aSKay Sievers if (!lo) { 1574770fe30aSKay Sievers err = -ENXIO; 1575770fe30aSKay Sievers goto out; 1576770fe30aSKay Sievers } 15771da177e4SLinus Torvalds 1578f8933667SMing Lei atomic_inc(&lo->lo_refcnt); 1579770fe30aSKay Sievers out: 1580770fe30aSKay Sievers mutex_unlock(&loop_index_mutex); 1581770fe30aSKay Sievers return err; 15821da177e4SLinus Torvalds } 15831da177e4SLinus Torvalds 1584*ae665016SLinus Torvalds static void __lo_release(struct loop_device *lo) 15851da177e4SLinus Torvalds { 1586ffcd7dcaSAlexander Beregalov int err; 15871da177e4SLinus Torvalds 1588f8933667SMing Lei if (atomic_dec_return(&lo->lo_refcnt)) 1589f8933667SMing Lei return; 1590f8933667SMing Lei 1591f85221ddSIngo Molnar mutex_lock(&lo->lo_ctl_mutex); 159214f27939SMilan Broz if (lo->lo_flags & LO_FLAGS_AUTOCLEAR) { 159314f27939SMilan Broz /* 159414f27939SMilan Broz * In autoclear mode, stop the loop thread 159514f27939SMilan Broz * and remove configuration after last close. 159614f27939SMilan Broz */ 15974c823cc3SAyan George err = loop_clr_fd(lo); 1598ffcd7dcaSAlexander Beregalov if (!err) 1599db2a144bSAl Viro return; 160043cade80SOmar Sandoval } else if (lo->lo_state == Lo_bound) { 160114f27939SMilan Broz /* 160214f27939SMilan Broz * Otherwise keep thread (if running) and config, 160314f27939SMilan Broz * but flush possible ongoing bios in thread. 160414f27939SMilan Broz */ 160543cade80SOmar Sandoval blk_mq_freeze_queue(lo->lo_queue); 160643cade80SOmar Sandoval blk_mq_unfreeze_queue(lo->lo_queue); 160714f27939SMilan Broz } 160896c58655SDavid Woodhouse 1609f85221ddSIngo Molnar mutex_unlock(&lo->lo_ctl_mutex); 16101da177e4SLinus Torvalds } 16111da177e4SLinus Torvalds 1612*ae665016SLinus Torvalds static void lo_release(struct gendisk *disk, fmode_t mode) 1613*ae665016SLinus Torvalds { 1614*ae665016SLinus Torvalds mutex_lock(&loop_index_mutex); 1615*ae665016SLinus Torvalds __lo_release(disk->private_data); 1616*ae665016SLinus Torvalds mutex_unlock(&loop_index_mutex); 1617*ae665016SLinus Torvalds } 1618*ae665016SLinus Torvalds 161983d5cde4SAlexey Dobriyan static const struct block_device_operations lo_fops = { 16201da177e4SLinus Torvalds .owner = THIS_MODULE, 1621bb214884SAl Viro .open = lo_open, 1622bb214884SAl Viro .release = lo_release, 1623bb214884SAl Viro .ioctl = lo_ioctl, 1624863d5b82SDavid Howells #ifdef CONFIG_COMPAT 1625bb214884SAl Viro .compat_ioctl = lo_compat_ioctl, 1626863d5b82SDavid Howells #endif 16271da177e4SLinus Torvalds }; 16281da177e4SLinus Torvalds 16291da177e4SLinus Torvalds /* 16301da177e4SLinus Torvalds * And now the modules code and kernel interface. 16311da177e4SLinus Torvalds */ 163273285082SKen Chen static int max_loop; 1633ac04fee0SNamhyung Kim module_param(max_loop, int, S_IRUGO); 1634a47653fcSKen Chen MODULE_PARM_DESC(max_loop, "Maximum number of loop devices"); 1635ac04fee0SNamhyung Kim module_param(max_part, int, S_IRUGO); 1636476a4813SLaurent Vivier MODULE_PARM_DESC(max_part, "Maximum number of partitions per loop device"); 16371da177e4SLinus Torvalds MODULE_LICENSE("GPL"); 16381da177e4SLinus Torvalds MODULE_ALIAS_BLOCKDEV_MAJOR(LOOP_MAJOR); 16391da177e4SLinus Torvalds 16401da177e4SLinus Torvalds int loop_register_transfer(struct loop_func_table *funcs) 16411da177e4SLinus Torvalds { 16421da177e4SLinus Torvalds unsigned int n = funcs->number; 16431da177e4SLinus Torvalds 16441da177e4SLinus Torvalds if (n >= MAX_LO_CRYPT || xfer_funcs[n]) 16451da177e4SLinus Torvalds return -EINVAL; 16461da177e4SLinus Torvalds xfer_funcs[n] = funcs; 16471da177e4SLinus Torvalds return 0; 16481da177e4SLinus Torvalds } 16491da177e4SLinus Torvalds 165034dd82afSKay Sievers static int unregister_transfer_cb(int id, void *ptr, void *data) 165134dd82afSKay Sievers { 165234dd82afSKay Sievers struct loop_device *lo = ptr; 165334dd82afSKay Sievers struct loop_func_table *xfer = data; 165434dd82afSKay Sievers 165534dd82afSKay Sievers mutex_lock(&lo->lo_ctl_mutex); 165634dd82afSKay Sievers if (lo->lo_encryption == xfer) 165734dd82afSKay Sievers loop_release_xfer(lo); 165834dd82afSKay Sievers mutex_unlock(&lo->lo_ctl_mutex); 165934dd82afSKay Sievers return 0; 166034dd82afSKay Sievers } 166134dd82afSKay Sievers 16621da177e4SLinus Torvalds int loop_unregister_transfer(int number) 16631da177e4SLinus Torvalds { 16641da177e4SLinus Torvalds unsigned int n = number; 16651da177e4SLinus Torvalds struct loop_func_table *xfer; 16661da177e4SLinus Torvalds 16671da177e4SLinus Torvalds if (n == 0 || n >= MAX_LO_CRYPT || (xfer = xfer_funcs[n]) == NULL) 16681da177e4SLinus Torvalds return -EINVAL; 16691da177e4SLinus Torvalds 16701da177e4SLinus Torvalds xfer_funcs[n] = NULL; 167134dd82afSKay Sievers idr_for_each(&loop_index_idr, &unregister_transfer_cb, xfer); 16721da177e4SLinus Torvalds return 0; 16731da177e4SLinus Torvalds } 16741da177e4SLinus Torvalds 16751da177e4SLinus Torvalds EXPORT_SYMBOL(loop_register_transfer); 16761da177e4SLinus Torvalds EXPORT_SYMBOL(loop_unregister_transfer); 16771da177e4SLinus Torvalds 1678fc17b653SChristoph Hellwig static blk_status_t loop_queue_rq(struct blk_mq_hw_ctx *hctx, 1679b5dd2f60SMing Lei const struct blk_mq_queue_data *bd) 1680b5dd2f60SMing Lei { 1681b5dd2f60SMing Lei struct loop_cmd *cmd = blk_mq_rq_to_pdu(bd->rq); 1682f4aa4c7bSMing Lei struct loop_device *lo = cmd->rq->q->queuedata; 1683b5dd2f60SMing Lei 1684b5dd2f60SMing Lei blk_mq_start_request(bd->rq); 1685b5dd2f60SMing Lei 1686f4aa4c7bSMing Lei if (lo->lo_state != Lo_bound) 1687fc17b653SChristoph Hellwig return BLK_STS_IOERR; 1688f4aa4c7bSMing Lei 1689f0225cacSChristoph Hellwig switch (req_op(cmd->rq)) { 1690f0225cacSChristoph Hellwig case REQ_OP_FLUSH: 1691f0225cacSChristoph Hellwig case REQ_OP_DISCARD: 169219372e27SChristoph Hellwig case REQ_OP_WRITE_ZEROES: 1693bc07c10aSMing Lei cmd->use_aio = false; 1694f0225cacSChristoph Hellwig break; 1695f0225cacSChristoph Hellwig default: 1696f0225cacSChristoph Hellwig cmd->use_aio = lo->use_dio; 1697f0225cacSChristoph Hellwig break; 1698f0225cacSChristoph Hellwig } 1699bc07c10aSMing Lei 1700d4478e92SShaohua Li /* always use the first bio's css */ 17010b508bc9SShaohua Li #ifdef CONFIG_BLK_CGROUP 1702d4478e92SShaohua Li if (cmd->use_aio && cmd->rq->bio && cmd->rq->bio->bi_css) { 1703d4478e92SShaohua Li cmd->css = cmd->rq->bio->bi_css; 1704d4478e92SShaohua Li css_get(cmd->css); 1705d4478e92SShaohua Li } else 1706d4478e92SShaohua Li #endif 1707d4478e92SShaohua Li cmd->css = NULL; 17083989144fSPetr Mladek kthread_queue_work(&lo->worker, &cmd->work); 1709b5dd2f60SMing Lei 1710fc17b653SChristoph Hellwig return BLK_STS_OK; 1711b5dd2f60SMing Lei } 1712b5dd2f60SMing Lei 1713b5dd2f60SMing Lei static void loop_handle_cmd(struct loop_cmd *cmd) 1714b5dd2f60SMing Lei { 1715a8ebb056SMike Christie const bool write = op_is_write(req_op(cmd->rq)); 1716b5dd2f60SMing Lei struct loop_device *lo = cmd->rq->q->queuedata; 1717f4829a9bSChristoph Hellwig int ret = 0; 1718b5dd2f60SMing Lei 1719f4829a9bSChristoph Hellwig if (write && (lo->lo_flags & LO_FLAGS_READ_ONLY)) { 1720f4829a9bSChristoph Hellwig ret = -EIO; 1721b5dd2f60SMing Lei goto failed; 1722f4829a9bSChristoph Hellwig } 1723b5dd2f60SMing Lei 172430112013SMing Lei ret = do_req_filebacked(lo, cmd->rq); 1725b5dd2f60SMing Lei failed: 1726bc07c10aSMing Lei /* complete non-aio request */ 1727fe2cb290SChristoph Hellwig if (!cmd->use_aio || ret) { 1728fe2cb290SChristoph Hellwig cmd->ret = ret ? -EIO : 0; 172908e0029aSChristoph Hellwig blk_mq_complete_request(cmd->rq); 1730fe2cb290SChristoph Hellwig } 1731b5dd2f60SMing Lei } 1732b5dd2f60SMing Lei 1733e03a3d7aSMing Lei static void loop_queue_work(struct kthread_work *work) 1734b5dd2f60SMing Lei { 1735b5dd2f60SMing Lei struct loop_cmd *cmd = 1736e03a3d7aSMing Lei container_of(work, struct loop_cmd, work); 1737b5dd2f60SMing Lei 1738b5dd2f60SMing Lei loop_handle_cmd(cmd); 1739b5dd2f60SMing Lei } 1740b5dd2f60SMing Lei 1741d6296d39SChristoph Hellwig static int loop_init_request(struct blk_mq_tag_set *set, struct request *rq, 1742d6296d39SChristoph Hellwig unsigned int hctx_idx, unsigned int numa_node) 1743b5dd2f60SMing Lei { 1744b5dd2f60SMing Lei struct loop_cmd *cmd = blk_mq_rq_to_pdu(rq); 1745b5dd2f60SMing Lei 1746b5dd2f60SMing Lei cmd->rq = rq; 17473989144fSPetr Mladek kthread_init_work(&cmd->work, loop_queue_work); 1748b5dd2f60SMing Lei 1749b5dd2f60SMing Lei return 0; 1750b5dd2f60SMing Lei } 1751b5dd2f60SMing Lei 1752f363b089SEric Biggers static const struct blk_mq_ops loop_mq_ops = { 1753b5dd2f60SMing Lei .queue_rq = loop_queue_rq, 1754b5dd2f60SMing Lei .init_request = loop_init_request, 1755fe2cb290SChristoph Hellwig .complete = lo_complete_rq, 1756b5dd2f60SMing Lei }; 1757b5dd2f60SMing Lei 175834dd82afSKay Sievers static int loop_add(struct loop_device **l, int i) 17591da177e4SLinus Torvalds { 176073285082SKen Chen struct loop_device *lo; 176173285082SKen Chen struct gendisk *disk; 176234dd82afSKay Sievers int err; 17631da177e4SLinus Torvalds 176434dd82afSKay Sievers err = -ENOMEM; 176568d740d7SSilva Paulo lo = kzalloc(sizeof(*lo), GFP_KERNEL); 176668d740d7SSilva Paulo if (!lo) 176773285082SKen Chen goto out; 176834dd82afSKay Sievers 1769ef7e7c82SMikulas Patocka lo->lo_state = Lo_unbound; 1770ef7e7c82SMikulas Patocka 1771c718aa65STejun Heo /* allocate id, if @id >= 0, we're requesting that specific id */ 177234dd82afSKay Sievers if (i >= 0) { 1773c718aa65STejun Heo err = idr_alloc(&loop_index_idr, lo, i, i + 1, GFP_KERNEL); 1774c718aa65STejun Heo if (err == -ENOSPC) 177534dd82afSKay Sievers err = -EEXIST; 177634dd82afSKay Sievers } else { 1777c718aa65STejun Heo err = idr_alloc(&loop_index_idr, lo, 0, 0, GFP_KERNEL); 177834dd82afSKay Sievers } 177934dd82afSKay Sievers if (err < 0) 178034dd82afSKay Sievers goto out_free_dev; 1781c718aa65STejun Heo i = err; 17821da177e4SLinus Torvalds 1783183cfb57SWei Yongjun err = -ENOMEM; 1784b5dd2f60SMing Lei lo->tag_set.ops = &loop_mq_ops; 1785b5dd2f60SMing Lei lo->tag_set.nr_hw_queues = 1; 1786b5dd2f60SMing Lei lo->tag_set.queue_depth = 128; 1787b5dd2f60SMing Lei lo->tag_set.numa_node = NUMA_NO_NODE; 1788b5dd2f60SMing Lei lo->tag_set.cmd_size = sizeof(struct loop_cmd); 1789b5dd2f60SMing Lei lo->tag_set.flags = BLK_MQ_F_SHOULD_MERGE | BLK_MQ_F_SG_MERGE; 1790b5dd2f60SMing Lei lo->tag_set.driver_data = lo; 1791b5dd2f60SMing Lei 1792b5dd2f60SMing Lei err = blk_mq_alloc_tag_set(&lo->tag_set); 1793b5dd2f60SMing Lei if (err) 17943ec981e3SMikulas Patocka goto out_free_idr; 179573285082SKen Chen 1796b5dd2f60SMing Lei lo->lo_queue = blk_mq_init_queue(&lo->tag_set); 1797b5dd2f60SMing Lei if (IS_ERR_OR_NULL(lo->lo_queue)) { 1798b5dd2f60SMing Lei err = PTR_ERR(lo->lo_queue); 1799b5dd2f60SMing Lei goto out_cleanup_tags; 1800b5dd2f60SMing Lei } 1801ef7e7c82SMikulas Patocka lo->lo_queue->queuedata = lo; 1802ef7e7c82SMikulas Patocka 180354bb0adeSShaohua Li blk_queue_max_hw_sectors(lo->lo_queue, BLK_DEF_MAX_SECTORS); 180440326d8aSShaohua Li 18055b5e20f4SMing Lei /* 180640326d8aSShaohua Li * By default, we do buffer IO, so it doesn't make sense to enable 180740326d8aSShaohua Li * merge because the I/O submitted to backing file is handled page by 180840326d8aSShaohua Li * page. For directio mode, merge does help to dispatch bigger request 180940326d8aSShaohua Li * to underlayer disk. We will enable merge once directio is enabled. 18105b5e20f4SMing Lei */ 18115b5e20f4SMing Lei queue_flag_set_unlocked(QUEUE_FLAG_NOMERGES, lo->lo_queue); 18125b5e20f4SMing Lei 18137a649737SMinfei Huang err = -ENOMEM; 1814476a4813SLaurent Vivier disk = lo->lo_disk = alloc_disk(1 << part_shift); 181573285082SKen Chen if (!disk) 181673285082SKen Chen goto out_free_queue; 181773285082SKen Chen 1818e03c8dd1SKay Sievers /* 1819e03c8dd1SKay Sievers * Disable partition scanning by default. The in-kernel partition 1820e03c8dd1SKay Sievers * scanning can be requested individually per-device during its 1821e03c8dd1SKay Sievers * setup. Userspace can always add and remove partitions from all 1822e03c8dd1SKay Sievers * devices. The needed partition minors are allocated from the 1823e03c8dd1SKay Sievers * extended minor space, the main loop device numbers will continue 1824e03c8dd1SKay Sievers * to match the loop minors, regardless of the number of partitions 1825e03c8dd1SKay Sievers * used. 1826e03c8dd1SKay Sievers * 1827e03c8dd1SKay Sievers * If max_part is given, partition scanning is globally enabled for 1828e03c8dd1SKay Sievers * all loop devices. The minors for the main loop devices will be 1829e03c8dd1SKay Sievers * multiples of max_part. 1830e03c8dd1SKay Sievers * 1831e03c8dd1SKay Sievers * Note: Global-for-all-devices, set-only-at-init, read-only module 1832e03c8dd1SKay Sievers * parameteters like 'max_loop' and 'max_part' make things needlessly 1833e03c8dd1SKay Sievers * complicated, are too static, inflexible and may surprise 1834e03c8dd1SKay Sievers * userspace tools. Parameters like this in general should be avoided. 1835e03c8dd1SKay Sievers */ 1836e03c8dd1SKay Sievers if (!part_shift) 1837e03c8dd1SKay Sievers disk->flags |= GENHD_FL_NO_PART_SCAN; 1838e03c8dd1SKay Sievers disk->flags |= GENHD_FL_EXT_DEVT; 1839f85221ddSIngo Molnar mutex_init(&lo->lo_ctl_mutex); 1840f8933667SMing Lei atomic_set(&lo->lo_refcnt, 0); 18411da177e4SLinus Torvalds lo->lo_number = i; 18421da177e4SLinus Torvalds spin_lock_init(&lo->lo_lock); 18431da177e4SLinus Torvalds disk->major = LOOP_MAJOR; 1844476a4813SLaurent Vivier disk->first_minor = i << part_shift; 18451da177e4SLinus Torvalds disk->fops = &lo_fops; 18461da177e4SLinus Torvalds disk->private_data = lo; 18471da177e4SLinus Torvalds disk->queue = lo->lo_queue; 184873285082SKen Chen sprintf(disk->disk_name, "loop%d", i); 184934dd82afSKay Sievers add_disk(disk); 185034dd82afSKay Sievers *l = lo; 185134dd82afSKay Sievers return lo->lo_number; 185273285082SKen Chen 185373285082SKen Chen out_free_queue: 185473285082SKen Chen blk_cleanup_queue(lo->lo_queue); 1855b5dd2f60SMing Lei out_cleanup_tags: 1856b5dd2f60SMing Lei blk_mq_free_tag_set(&lo->tag_set); 18573ec981e3SMikulas Patocka out_free_idr: 18583ec981e3SMikulas Patocka idr_remove(&loop_index_idr, i); 185973285082SKen Chen out_free_dev: 186073285082SKen Chen kfree(lo); 186173285082SKen Chen out: 186234dd82afSKay Sievers return err; 18631da177e4SLinus Torvalds } 18641da177e4SLinus Torvalds 186534dd82afSKay Sievers static void loop_remove(struct loop_device *lo) 186673285082SKen Chen { 186773285082SKen Chen blk_cleanup_queue(lo->lo_queue); 18686cd18e71SNeilBrown del_gendisk(lo->lo_disk); 1869b5dd2f60SMing Lei blk_mq_free_tag_set(&lo->tag_set); 187073285082SKen Chen put_disk(lo->lo_disk); 187173285082SKen Chen kfree(lo); 187273285082SKen Chen } 187373285082SKen Chen 1874770fe30aSKay Sievers static int find_free_cb(int id, void *ptr, void *data) 1875770fe30aSKay Sievers { 1876770fe30aSKay Sievers struct loop_device *lo = ptr; 1877770fe30aSKay Sievers struct loop_device **l = data; 1878770fe30aSKay Sievers 1879770fe30aSKay Sievers if (lo->lo_state == Lo_unbound) { 1880770fe30aSKay Sievers *l = lo; 1881770fe30aSKay Sievers return 1; 1882770fe30aSKay Sievers } 1883770fe30aSKay Sievers return 0; 1884770fe30aSKay Sievers } 1885770fe30aSKay Sievers 188634dd82afSKay Sievers static int loop_lookup(struct loop_device **l, int i) 1887a47653fcSKen Chen { 1888a47653fcSKen Chen struct loop_device *lo; 188934dd82afSKay Sievers int ret = -ENODEV; 1890a47653fcSKen Chen 1891770fe30aSKay Sievers if (i < 0) { 1892770fe30aSKay Sievers int err; 1893770fe30aSKay Sievers 1894770fe30aSKay Sievers err = idr_for_each(&loop_index_idr, &find_free_cb, &lo); 1895770fe30aSKay Sievers if (err == 1) { 1896770fe30aSKay Sievers *l = lo; 1897770fe30aSKay Sievers ret = lo->lo_number; 1898770fe30aSKay Sievers } 1899770fe30aSKay Sievers goto out; 1900a47653fcSKen Chen } 1901a47653fcSKen Chen 1902770fe30aSKay Sievers /* lookup and return a specific i */ 190334dd82afSKay Sievers lo = idr_find(&loop_index_idr, i); 1904a47653fcSKen Chen if (lo) { 190534dd82afSKay Sievers *l = lo; 190634dd82afSKay Sievers ret = lo->lo_number; 1907a47653fcSKen Chen } 1908770fe30aSKay Sievers out: 190934dd82afSKay Sievers return ret; 1910a47653fcSKen Chen } 1911a47653fcSKen Chen 191273285082SKen Chen static struct kobject *loop_probe(dev_t dev, int *part, void *data) 191373285082SKen Chen { 1914705962ccSAl Viro struct loop_device *lo; 191507002e99SAl Viro struct kobject *kobj; 191634dd82afSKay Sievers int err; 191773285082SKen Chen 191834dd82afSKay Sievers mutex_lock(&loop_index_mutex); 191934dd82afSKay Sievers err = loop_lookup(&lo, MINOR(dev) >> part_shift); 192034dd82afSKay Sievers if (err < 0) 192134dd82afSKay Sievers err = loop_add(&lo, MINOR(dev) >> part_shift); 192234dd82afSKay Sievers if (err < 0) 1923a207f593SMikulas Patocka kobj = NULL; 192434dd82afSKay Sievers else 192534dd82afSKay Sievers kobj = get_disk(lo->lo_disk); 192634dd82afSKay Sievers mutex_unlock(&loop_index_mutex); 192773285082SKen Chen 192873285082SKen Chen *part = 0; 192907002e99SAl Viro return kobj; 193073285082SKen Chen } 193173285082SKen Chen 1932770fe30aSKay Sievers static long loop_control_ioctl(struct file *file, unsigned int cmd, 1933770fe30aSKay Sievers unsigned long parm) 1934770fe30aSKay Sievers { 1935770fe30aSKay Sievers struct loop_device *lo; 1936770fe30aSKay Sievers int ret = -ENOSYS; 1937770fe30aSKay Sievers 1938770fe30aSKay Sievers mutex_lock(&loop_index_mutex); 1939770fe30aSKay Sievers switch (cmd) { 1940770fe30aSKay Sievers case LOOP_CTL_ADD: 1941770fe30aSKay Sievers ret = loop_lookup(&lo, parm); 1942770fe30aSKay Sievers if (ret >= 0) { 1943770fe30aSKay Sievers ret = -EEXIST; 1944770fe30aSKay Sievers break; 1945770fe30aSKay Sievers } 1946770fe30aSKay Sievers ret = loop_add(&lo, parm); 1947770fe30aSKay Sievers break; 1948770fe30aSKay Sievers case LOOP_CTL_REMOVE: 1949770fe30aSKay Sievers ret = loop_lookup(&lo, parm); 1950770fe30aSKay Sievers if (ret < 0) 1951770fe30aSKay Sievers break; 1952770fe30aSKay Sievers mutex_lock(&lo->lo_ctl_mutex); 1953770fe30aSKay Sievers if (lo->lo_state != Lo_unbound) { 1954770fe30aSKay Sievers ret = -EBUSY; 1955770fe30aSKay Sievers mutex_unlock(&lo->lo_ctl_mutex); 1956770fe30aSKay Sievers break; 1957770fe30aSKay Sievers } 1958f8933667SMing Lei if (atomic_read(&lo->lo_refcnt) > 0) { 1959770fe30aSKay Sievers ret = -EBUSY; 1960770fe30aSKay Sievers mutex_unlock(&lo->lo_ctl_mutex); 1961770fe30aSKay Sievers break; 1962770fe30aSKay Sievers } 1963770fe30aSKay Sievers lo->lo_disk->private_data = NULL; 1964770fe30aSKay Sievers mutex_unlock(&lo->lo_ctl_mutex); 1965770fe30aSKay Sievers idr_remove(&loop_index_idr, lo->lo_number); 1966770fe30aSKay Sievers loop_remove(lo); 1967770fe30aSKay Sievers break; 1968770fe30aSKay Sievers case LOOP_CTL_GET_FREE: 1969770fe30aSKay Sievers ret = loop_lookup(&lo, -1); 1970770fe30aSKay Sievers if (ret >= 0) 1971770fe30aSKay Sievers break; 1972770fe30aSKay Sievers ret = loop_add(&lo, -1); 1973770fe30aSKay Sievers } 1974770fe30aSKay Sievers mutex_unlock(&loop_index_mutex); 1975770fe30aSKay Sievers 1976770fe30aSKay Sievers return ret; 1977770fe30aSKay Sievers } 1978770fe30aSKay Sievers 1979770fe30aSKay Sievers static const struct file_operations loop_ctl_fops = { 1980770fe30aSKay Sievers .open = nonseekable_open, 1981770fe30aSKay Sievers .unlocked_ioctl = loop_control_ioctl, 1982770fe30aSKay Sievers .compat_ioctl = loop_control_ioctl, 1983770fe30aSKay Sievers .owner = THIS_MODULE, 1984770fe30aSKay Sievers .llseek = noop_llseek, 1985770fe30aSKay Sievers }; 1986770fe30aSKay Sievers 1987770fe30aSKay Sievers static struct miscdevice loop_misc = { 1988770fe30aSKay Sievers .minor = LOOP_CTRL_MINOR, 1989770fe30aSKay Sievers .name = "loop-control", 1990770fe30aSKay Sievers .fops = &loop_ctl_fops, 1991770fe30aSKay Sievers }; 1992770fe30aSKay Sievers 1993770fe30aSKay Sievers MODULE_ALIAS_MISCDEV(LOOP_CTRL_MINOR); 1994770fe30aSKay Sievers MODULE_ALIAS("devname:loop-control"); 1995770fe30aSKay Sievers 199673285082SKen Chen static int __init loop_init(void) 199773285082SKen Chen { 1998a47653fcSKen Chen int i, nr; 1999a47653fcSKen Chen unsigned long range; 200034dd82afSKay Sievers struct loop_device *lo; 2001770fe30aSKay Sievers int err; 2002a47653fcSKen Chen 2003476a4813SLaurent Vivier part_shift = 0; 2004ac04fee0SNamhyung Kim if (max_part > 0) { 2005476a4813SLaurent Vivier part_shift = fls(max_part); 2006476a4813SLaurent Vivier 2007ac04fee0SNamhyung Kim /* 2008ac04fee0SNamhyung Kim * Adjust max_part according to part_shift as it is exported 2009ac04fee0SNamhyung Kim * to user space so that user can decide correct minor number 2010ac04fee0SNamhyung Kim * if [s]he want to create more devices. 2011ac04fee0SNamhyung Kim * 2012ac04fee0SNamhyung Kim * Note that -1 is required because partition 0 is reserved 2013ac04fee0SNamhyung Kim * for the whole disk. 2014ac04fee0SNamhyung Kim */ 2015ac04fee0SNamhyung Kim max_part = (1UL << part_shift) - 1; 2016ac04fee0SNamhyung Kim } 2017ac04fee0SNamhyung Kim 2018b1a66504SGuo Chao if ((1UL << part_shift) > DISK_MAX_PARTS) { 2019b1a66504SGuo Chao err = -EINVAL; 2020a8c1d064SAnton Volkov goto err_out; 2021b1a66504SGuo Chao } 202278f4bb36SNamhyung Kim 2023b1a66504SGuo Chao if (max_loop > 1UL << (MINORBITS - part_shift)) { 2024b1a66504SGuo Chao err = -EINVAL; 2025a8c1d064SAnton Volkov goto err_out; 2026b1a66504SGuo Chao } 202773285082SKen Chen 2028d134b00bSKay Sievers /* 2029d134b00bSKay Sievers * If max_loop is specified, create that many devices upfront. 2030d134b00bSKay Sievers * This also becomes a hard limit. If max_loop is not specified, 2031d134b00bSKay Sievers * create CONFIG_BLK_DEV_LOOP_MIN_COUNT loop devices at module 2032d134b00bSKay Sievers * init time. Loop devices can be requested on-demand with the 2033d134b00bSKay Sievers * /dev/loop-control interface, or be instantiated by accessing 2034d134b00bSKay Sievers * a 'dead' device node. 2035d134b00bSKay Sievers */ 203673285082SKen Chen if (max_loop) { 2037a47653fcSKen Chen nr = max_loop; 2038a1c15c59SNamhyung Kim range = max_loop << part_shift; 2039a47653fcSKen Chen } else { 2040d134b00bSKay Sievers nr = CONFIG_BLK_DEV_LOOP_MIN_COUNT; 2041a1c15c59SNamhyung Kim range = 1UL << MINORBITS; 204273285082SKen Chen } 2043a47653fcSKen Chen 2044a8c1d064SAnton Volkov err = misc_register(&loop_misc); 2045a8c1d064SAnton Volkov if (err < 0) 2046a8c1d064SAnton Volkov goto err_out; 2047a8c1d064SAnton Volkov 2048a8c1d064SAnton Volkov 2049b1a66504SGuo Chao if (register_blkdev(LOOP_MAJOR, "loop")) { 2050b1a66504SGuo Chao err = -EIO; 2051b1a66504SGuo Chao goto misc_out; 2052b1a66504SGuo Chao } 2053a47653fcSKen Chen 2054a47653fcSKen Chen blk_register_region(MKDEV(LOOP_MAJOR, 0), range, 2055a47653fcSKen Chen THIS_MODULE, loop_probe, NULL, NULL); 2056a47653fcSKen Chen 2057d134b00bSKay Sievers /* pre-create number of devices given by config or max_loop */ 205834dd82afSKay Sievers mutex_lock(&loop_index_mutex); 205934dd82afSKay Sievers for (i = 0; i < nr; i++) 206034dd82afSKay Sievers loop_add(&lo, i); 206134dd82afSKay Sievers mutex_unlock(&loop_index_mutex); 206234dd82afSKay Sievers 206373285082SKen Chen printk(KERN_INFO "loop: module loaded\n"); 20641da177e4SLinus Torvalds return 0; 2065b1a66504SGuo Chao 2066b1a66504SGuo Chao misc_out: 2067b1a66504SGuo Chao misc_deregister(&loop_misc); 2068a8c1d064SAnton Volkov err_out: 2069b1a66504SGuo Chao return err; 207034dd82afSKay Sievers } 2071a47653fcSKen Chen 207234dd82afSKay Sievers static int loop_exit_cb(int id, void *ptr, void *data) 207334dd82afSKay Sievers { 207434dd82afSKay Sievers struct loop_device *lo = ptr; 2075a47653fcSKen Chen 207634dd82afSKay Sievers loop_remove(lo); 207734dd82afSKay Sievers return 0; 20781da177e4SLinus Torvalds } 20791da177e4SLinus Torvalds 208073285082SKen Chen static void __exit loop_exit(void) 20811da177e4SLinus Torvalds { 2082a47653fcSKen Chen unsigned long range; 20831da177e4SLinus Torvalds 2084a1c15c59SNamhyung Kim range = max_loop ? max_loop << part_shift : 1UL << MINORBITS; 2085a47653fcSKen Chen 208634dd82afSKay Sievers idr_for_each(&loop_index_idr, &loop_exit_cb, NULL); 208734dd82afSKay Sievers idr_destroy(&loop_index_idr); 208873285082SKen Chen 2089a47653fcSKen Chen blk_unregister_region(MKDEV(LOOP_MAJOR, 0), range); 209000d59405SAkinobu Mita unregister_blkdev(LOOP_MAJOR, "loop"); 2091770fe30aSKay Sievers 2092770fe30aSKay Sievers misc_deregister(&loop_misc); 20931da177e4SLinus Torvalds } 20941da177e4SLinus Torvalds 20951da177e4SLinus Torvalds module_init(loop_init); 20961da177e4SLinus Torvalds module_exit(loop_exit); 20971da177e4SLinus Torvalds 20981da177e4SLinus Torvalds #ifndef MODULE 20991da177e4SLinus Torvalds static int __init max_loop_setup(char *str) 21001da177e4SLinus Torvalds { 21011da177e4SLinus Torvalds max_loop = simple_strtol(str, NULL, 0); 21021da177e4SLinus Torvalds return 1; 21031da177e4SLinus Torvalds } 21041da177e4SLinus Torvalds 21051da177e4SLinus Torvalds __setup("max_loop=", max_loop_setup); 21061da177e4SLinus Torvalds #endif 2107