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 811da177e4SLinus Torvalds #include <asm/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 1677035b5dfSDmitry Monakhov static int 1687035b5dfSDmitry Monakhov figure_loop_size(struct loop_device *lo, loff_t offset, loff_t sizelimit) 1697035b5dfSDmitry Monakhov { 1707035b5dfSDmitry Monakhov loff_t size = get_size(offset, sizelimit, lo->lo_backing_file); 1711da177e4SLinus Torvalds sector_t x = (sector_t)size; 1727b0576a3SGuo Chao struct block_device *bdev = lo->lo_device; 1731da177e4SLinus Torvalds 1741da177e4SLinus Torvalds if (unlikely((loff_t)x != size)) 1751da177e4SLinus Torvalds return -EFBIG; 1767035b5dfSDmitry Monakhov if (lo->lo_offset != offset) 1777035b5dfSDmitry Monakhov lo->lo_offset = offset; 1787035b5dfSDmitry Monakhov if (lo->lo_sizelimit != sizelimit) 1797035b5dfSDmitry Monakhov lo->lo_sizelimit = sizelimit; 18073285082SKen Chen set_capacity(lo->lo_disk, x); 1817b0576a3SGuo Chao bd_set_size(bdev, (loff_t)get_capacity(bdev->bd_disk) << 9); 1827b0576a3SGuo Chao /* let user-space know about the new size */ 1837b0576a3SGuo Chao kobject_uevent(&disk_to_dev(bdev->bd_disk)->kobj, KOBJ_CHANGE); 1841da177e4SLinus Torvalds return 0; 1851da177e4SLinus Torvalds } 1861da177e4SLinus Torvalds 1871da177e4SLinus Torvalds static inline int 1881da177e4SLinus Torvalds lo_do_transfer(struct loop_device *lo, int cmd, 1891da177e4SLinus Torvalds struct page *rpage, unsigned roffs, 1901da177e4SLinus Torvalds struct page *lpage, unsigned loffs, 1911da177e4SLinus Torvalds int size, sector_t rblock) 1921da177e4SLinus Torvalds { 193aa4d8616SChristoph Hellwig int ret; 194aa4d8616SChristoph Hellwig 195aa4d8616SChristoph Hellwig ret = lo->transfer(lo, cmd, rpage, roffs, lpage, loffs, size, rblock); 196aa4d8616SChristoph Hellwig if (likely(!ret)) 1971da177e4SLinus Torvalds return 0; 1981da177e4SLinus Torvalds 199aa4d8616SChristoph Hellwig printk_ratelimited(KERN_ERR 200aa4d8616SChristoph Hellwig "loop: Transfer error at byte offset %llu, length %i.\n", 201aa4d8616SChristoph Hellwig (unsigned long long)rblock << 9, size); 202aa4d8616SChristoph Hellwig return ret; 2031da177e4SLinus Torvalds } 2041da177e4SLinus Torvalds 205aa4d8616SChristoph Hellwig static int lo_write_bvec(struct file *file, struct bio_vec *bvec, loff_t *ppos) 2061da177e4SLinus Torvalds { 207aa4d8616SChristoph Hellwig struct iov_iter i; 2081da177e4SLinus Torvalds ssize_t bw; 209283e7e5dSAl Viro 210aa4d8616SChristoph Hellwig iov_iter_bvec(&i, ITER_BVEC, bvec, 1, bvec->bv_len); 2111da177e4SLinus Torvalds 21203d95eb2SAl Viro file_start_write(file); 213aa4d8616SChristoph Hellwig bw = vfs_iter_write(file, &i, ppos); 21403d95eb2SAl Viro file_end_write(file); 215aa4d8616SChristoph Hellwig 216aa4d8616SChristoph Hellwig if (likely(bw == bvec->bv_len)) 2171da177e4SLinus Torvalds return 0; 218aa4d8616SChristoph Hellwig 219aa4d8616SChristoph Hellwig printk_ratelimited(KERN_ERR 220aa4d8616SChristoph Hellwig "loop: Write error at byte offset %llu, length %i.\n", 221aa4d8616SChristoph Hellwig (unsigned long long)*ppos, bvec->bv_len); 2221da177e4SLinus Torvalds if (bw >= 0) 2231da177e4SLinus Torvalds bw = -EIO; 2241da177e4SLinus Torvalds return bw; 2251da177e4SLinus Torvalds } 2261da177e4SLinus Torvalds 227aa4d8616SChristoph Hellwig static int lo_write_simple(struct loop_device *lo, struct request *rq, 228aa4d8616SChristoph Hellwig loff_t pos) 2291da177e4SLinus Torvalds { 230aa4d8616SChristoph Hellwig struct bio_vec bvec; 231aa4d8616SChristoph Hellwig struct req_iterator iter; 232aa4d8616SChristoph Hellwig int ret = 0; 233aa4d8616SChristoph Hellwig 234aa4d8616SChristoph Hellwig rq_for_each_segment(bvec, rq, iter) { 235aa4d8616SChristoph Hellwig ret = lo_write_bvec(lo->lo_backing_file, &bvec, &pos); 236aa4d8616SChristoph Hellwig if (ret < 0) 237aa4d8616SChristoph Hellwig break; 2381da177e4SLinus Torvalds cond_resched(); 2391da177e4SLinus Torvalds } 2401da177e4SLinus Torvalds 241aa4d8616SChristoph Hellwig return ret; 242aa4d8616SChristoph Hellwig } 243aa4d8616SChristoph Hellwig 244aa4d8616SChristoph Hellwig /* 245456be148SChristoph Hellwig * This is the slow, transforming version that needs to double buffer the 246456be148SChristoph Hellwig * data as it cannot do the transformations in place without having direct 247456be148SChristoph Hellwig * access to the destination pages of the backing file. 2481da177e4SLinus Torvalds */ 249aa4d8616SChristoph Hellwig static int lo_write_transfer(struct loop_device *lo, struct request *rq, 250aa4d8616SChristoph Hellwig loff_t pos) 2511da177e4SLinus Torvalds { 252aa4d8616SChristoph Hellwig struct bio_vec bvec, b; 25330112013SMing Lei struct req_iterator iter; 254aa4d8616SChristoph Hellwig struct page *page; 2557988613bSKent Overstreet int ret = 0; 2561da177e4SLinus Torvalds 257aa4d8616SChristoph Hellwig page = alloc_page(GFP_NOIO); 2581da177e4SLinus Torvalds if (unlikely(!page)) 259aa4d8616SChristoph Hellwig return -ENOMEM; 260456be148SChristoph Hellwig 26130112013SMing Lei rq_for_each_segment(bvec, rq, iter) { 262aa4d8616SChristoph Hellwig ret = lo_do_transfer(lo, WRITE, page, 0, bvec.bv_page, 263aa4d8616SChristoph Hellwig bvec.bv_offset, bvec.bv_len, pos >> 9); 264aa4d8616SChristoph Hellwig if (unlikely(ret)) 265aa4d8616SChristoph Hellwig break; 266aa4d8616SChristoph Hellwig 267aa4d8616SChristoph Hellwig b.bv_page = page; 268aa4d8616SChristoph Hellwig b.bv_offset = 0; 269aa4d8616SChristoph Hellwig b.bv_len = bvec.bv_len; 270aa4d8616SChristoph Hellwig ret = lo_write_bvec(lo->lo_backing_file, &b, &pos); 2711da177e4SLinus Torvalds if (ret < 0) 2721da177e4SLinus Torvalds break; 2731da177e4SLinus Torvalds } 274aa4d8616SChristoph Hellwig 2751da177e4SLinus Torvalds __free_page(page); 2761da177e4SLinus Torvalds return ret; 2771da177e4SLinus Torvalds } 2781da177e4SLinus Torvalds 279aa4d8616SChristoph Hellwig static int lo_read_simple(struct loop_device *lo, struct request *rq, 280aa4d8616SChristoph Hellwig loff_t pos) 2811da177e4SLinus Torvalds { 2827988613bSKent Overstreet struct bio_vec bvec; 28330112013SMing Lei struct req_iterator iter; 284aa4d8616SChristoph Hellwig struct iov_iter i; 285aa4d8616SChristoph Hellwig ssize_t len; 2861da177e4SLinus Torvalds 28730112013SMing Lei rq_for_each_segment(bvec, rq, iter) { 288aa4d8616SChristoph Hellwig iov_iter_bvec(&i, ITER_BVEC, &bvec, 1, bvec.bv_len); 289aa4d8616SChristoph Hellwig len = vfs_iter_read(lo->lo_backing_file, &i, &pos); 290aa4d8616SChristoph Hellwig if (len < 0) 291aa4d8616SChristoph Hellwig return len; 292306df071SDave Young 293aa4d8616SChristoph Hellwig flush_dcache_page(bvec.bv_page); 294aa4d8616SChristoph Hellwig 295aa4d8616SChristoph Hellwig if (len != bvec.bv_len) { 29630112013SMing Lei struct bio *bio; 29730112013SMing Lei 29830112013SMing Lei __rq_for_each_bio(bio, rq) 299306df071SDave Young zero_fill_bio(bio); 3001da177e4SLinus Torvalds break; 301306df071SDave Young } 302aa4d8616SChristoph Hellwig cond_resched(); 3031da177e4SLinus Torvalds } 304aa4d8616SChristoph Hellwig 305306df071SDave Young return 0; 3061da177e4SLinus Torvalds } 3071da177e4SLinus Torvalds 308aa4d8616SChristoph Hellwig static int lo_read_transfer(struct loop_device *lo, struct request *rq, 309aa4d8616SChristoph Hellwig loff_t pos) 310aa4d8616SChristoph Hellwig { 311aa4d8616SChristoph Hellwig struct bio_vec bvec, b; 312aa4d8616SChristoph Hellwig struct req_iterator iter; 313aa4d8616SChristoph Hellwig struct iov_iter i; 314aa4d8616SChristoph Hellwig struct page *page; 315aa4d8616SChristoph Hellwig ssize_t len; 316aa4d8616SChristoph Hellwig int ret = 0; 317aa4d8616SChristoph Hellwig 318aa4d8616SChristoph Hellwig page = alloc_page(GFP_NOIO); 319aa4d8616SChristoph Hellwig if (unlikely(!page)) 320aa4d8616SChristoph Hellwig return -ENOMEM; 321aa4d8616SChristoph Hellwig 322aa4d8616SChristoph Hellwig rq_for_each_segment(bvec, rq, iter) { 323aa4d8616SChristoph Hellwig loff_t offset = pos; 324aa4d8616SChristoph Hellwig 325aa4d8616SChristoph Hellwig b.bv_page = page; 326aa4d8616SChristoph Hellwig b.bv_offset = 0; 327aa4d8616SChristoph Hellwig b.bv_len = bvec.bv_len; 328aa4d8616SChristoph Hellwig 329aa4d8616SChristoph Hellwig iov_iter_bvec(&i, ITER_BVEC, &b, 1, b.bv_len); 330aa4d8616SChristoph Hellwig len = vfs_iter_read(lo->lo_backing_file, &i, &pos); 331aa4d8616SChristoph Hellwig if (len < 0) { 332aa4d8616SChristoph Hellwig ret = len; 333aa4d8616SChristoph Hellwig goto out_free_page; 334aa4d8616SChristoph Hellwig } 335aa4d8616SChristoph Hellwig 336aa4d8616SChristoph Hellwig ret = lo_do_transfer(lo, READ, page, 0, bvec.bv_page, 337aa4d8616SChristoph Hellwig bvec.bv_offset, len, offset >> 9); 338aa4d8616SChristoph Hellwig if (ret) 339aa4d8616SChristoph Hellwig goto out_free_page; 340aa4d8616SChristoph Hellwig 341aa4d8616SChristoph Hellwig flush_dcache_page(bvec.bv_page); 342aa4d8616SChristoph Hellwig 343aa4d8616SChristoph Hellwig if (len != bvec.bv_len) { 344aa4d8616SChristoph Hellwig struct bio *bio; 345aa4d8616SChristoph Hellwig 346aa4d8616SChristoph Hellwig __rq_for_each_bio(bio, rq) 347aa4d8616SChristoph Hellwig zero_fill_bio(bio); 348aa4d8616SChristoph Hellwig break; 349aa4d8616SChristoph Hellwig } 350aa4d8616SChristoph Hellwig } 351aa4d8616SChristoph Hellwig 352aa4d8616SChristoph Hellwig ret = 0; 353aa4d8616SChristoph Hellwig out_free_page: 354aa4d8616SChristoph Hellwig __free_page(page); 355aa4d8616SChristoph Hellwig return ret; 356aa4d8616SChristoph Hellwig } 357aa4d8616SChristoph Hellwig 358cf655d95SMing Lei static int lo_discard(struct loop_device *lo, struct request *rq, loff_t pos) 359cf655d95SMing Lei { 360cf655d95SMing Lei /* 361cf655d95SMing Lei * We use punch hole to reclaim the free space used by the 362cf655d95SMing Lei * image a.k.a. discard. However we do not support discard if 363cf655d95SMing Lei * encryption is enabled, because it may give an attacker 364cf655d95SMing Lei * useful information. 365cf655d95SMing Lei */ 366cf655d95SMing Lei struct file *file = lo->lo_backing_file; 367cf655d95SMing Lei int mode = FALLOC_FL_PUNCH_HOLE | FALLOC_FL_KEEP_SIZE; 368cf655d95SMing Lei int ret; 369cf655d95SMing Lei 370cf655d95SMing Lei if ((!file->f_op->fallocate) || lo->lo_encrypt_key_size) { 371cf655d95SMing Lei ret = -EOPNOTSUPP; 372cf655d95SMing Lei goto out; 373cf655d95SMing Lei } 374cf655d95SMing Lei 375cf655d95SMing Lei ret = file->f_op->fallocate(file, mode, pos, blk_rq_bytes(rq)); 376cf655d95SMing Lei if (unlikely(ret && ret != -EINVAL && ret != -EOPNOTSUPP)) 377cf655d95SMing Lei ret = -EIO; 378cf655d95SMing Lei out: 379cf655d95SMing Lei return ret; 380cf655d95SMing Lei } 381cf655d95SMing Lei 382cf655d95SMing Lei static int lo_req_flush(struct loop_device *lo, struct request *rq) 383cf655d95SMing Lei { 384cf655d95SMing Lei struct file *file = lo->lo_backing_file; 385cf655d95SMing Lei int ret = vfs_fsync(file, 0); 386cf655d95SMing Lei if (unlikely(ret && ret != -EINVAL)) 387cf655d95SMing Lei ret = -EIO; 388cf655d95SMing Lei 389cf655d95SMing Lei return ret; 390cf655d95SMing Lei } 391cf655d95SMing Lei 39230112013SMing Lei static int do_req_filebacked(struct loop_device *lo, struct request *rq) 3931da177e4SLinus Torvalds { 3941da177e4SLinus Torvalds loff_t pos; 3951da177e4SLinus Torvalds int ret; 3961da177e4SLinus Torvalds 39730112013SMing Lei pos = ((loff_t) blk_rq_pos(rq) << 9) + lo->lo_offset; 39868db1961SNikanth Karthikesan 39930112013SMing Lei if (rq->cmd_flags & REQ_WRITE) { 400cf655d95SMing Lei if (rq->cmd_flags & REQ_FLUSH) 401cf655d95SMing Lei ret = lo_req_flush(lo, rq); 402af65aa8eSMing Lei else if (rq->cmd_flags & REQ_DISCARD) 403cf655d95SMing Lei ret = lo_discard(lo, rq, pos); 404aa4d8616SChristoph Hellwig else if (lo->transfer) 405aa4d8616SChristoph Hellwig ret = lo_write_transfer(lo, rq, pos); 406af65aa8eSMing Lei else 407aa4d8616SChristoph Hellwig ret = lo_write_simple(lo, rq, pos); 408aa4d8616SChristoph Hellwig 409aa4d8616SChristoph Hellwig } else { 410aa4d8616SChristoph Hellwig if (lo->transfer) 411aa4d8616SChristoph Hellwig ret = lo_read_transfer(lo, rq, pos); 412aa4d8616SChristoph Hellwig else 413aa4d8616SChristoph Hellwig ret = lo_read_simple(lo, rq, pos); 414aa4d8616SChristoph Hellwig } 41568db1961SNikanth Karthikesan 4161da177e4SLinus Torvalds return ret; 4171da177e4SLinus Torvalds } 4181da177e4SLinus Torvalds 4191da177e4SLinus Torvalds struct switch_request { 4201da177e4SLinus Torvalds struct file *file; 4211da177e4SLinus Torvalds struct completion wait; 4221da177e4SLinus Torvalds }; 4231da177e4SLinus Torvalds 42414f27939SMilan Broz /* 4251da177e4SLinus Torvalds * Do the actual switch; called from the BIO completion routine 4261da177e4SLinus Torvalds */ 4271da177e4SLinus Torvalds static void do_loop_switch(struct loop_device *lo, struct switch_request *p) 4281da177e4SLinus Torvalds { 4291da177e4SLinus Torvalds struct file *file = p->file; 4301da177e4SLinus Torvalds struct file *old_file = lo->lo_backing_file; 43114f27939SMilan Broz struct address_space *mapping; 4321da177e4SLinus Torvalds 43314f27939SMilan Broz /* if no new file, only flush of queued bios requested */ 43414f27939SMilan Broz if (!file) 435b5dd2f60SMing Lei return; 43614f27939SMilan Broz 43714f27939SMilan Broz mapping = file->f_mapping; 4381da177e4SLinus Torvalds mapping_set_gfp_mask(old_file->f_mapping, lo->old_gfp_mask); 4391da177e4SLinus Torvalds lo->lo_backing_file = file; 440ba52de12STheodore Ts'o lo->lo_blocksize = S_ISBLK(mapping->host->i_mode) ? 441ba52de12STheodore Ts'o mapping->host->i_bdev->bd_block_size : PAGE_SIZE; 4421da177e4SLinus Torvalds lo->old_gfp_mask = mapping_gfp_mask(mapping); 4431da177e4SLinus Torvalds mapping_set_gfp_mask(mapping, lo->old_gfp_mask & ~(__GFP_IO|__GFP_FS)); 4441da177e4SLinus Torvalds } 4451da177e4SLinus Torvalds 446b5dd2f60SMing Lei /* 447b5dd2f60SMing Lei * loop_switch performs the hard work of switching a backing store. 448b5dd2f60SMing Lei * First it needs to flush existing IO, it does this by sending a magic 449b5dd2f60SMing Lei * BIO down the pipe. The completion of this BIO does the actual switch. 450b5dd2f60SMing Lei */ 451b5dd2f60SMing Lei static int loop_switch(struct loop_device *lo, struct file *file) 452b5dd2f60SMing Lei { 453b5dd2f60SMing Lei struct switch_request w; 454b5dd2f60SMing Lei 455b5dd2f60SMing Lei w.file = file; 456b5dd2f60SMing Lei 457b5dd2f60SMing Lei /* freeze queue and wait for completion of scheduled requests */ 458b5dd2f60SMing Lei blk_mq_freeze_queue(lo->lo_queue); 459b5dd2f60SMing Lei 460b5dd2f60SMing Lei /* do the switch action */ 461b5dd2f60SMing Lei do_loop_switch(lo, &w); 462b5dd2f60SMing Lei 463b5dd2f60SMing Lei /* unfreeze */ 464b5dd2f60SMing Lei blk_mq_unfreeze_queue(lo->lo_queue); 465b5dd2f60SMing Lei 466b5dd2f60SMing Lei return 0; 467b5dd2f60SMing Lei } 468b5dd2f60SMing Lei 469b5dd2f60SMing Lei /* 470b5dd2f60SMing Lei * Helper to flush the IOs in loop, but keeping loop thread running 471b5dd2f60SMing Lei */ 472b5dd2f60SMing Lei static int loop_flush(struct loop_device *lo) 473b5dd2f60SMing Lei { 474b5dd2f60SMing Lei return loop_switch(lo, NULL); 475b5dd2f60SMing Lei } 4761da177e4SLinus Torvalds 47706f0e9e6SMing Lei static void loop_reread_partitions(struct loop_device *lo, 47806f0e9e6SMing Lei struct block_device *bdev) 47906f0e9e6SMing Lei { 48006f0e9e6SMing Lei int rc; 48106f0e9e6SMing Lei 48206f0e9e6SMing Lei /* 48306f0e9e6SMing Lei * bd_mutex has been held already in release path, so don't 48406f0e9e6SMing Lei * acquire it if this function is called in such case. 48506f0e9e6SMing Lei * 48606f0e9e6SMing Lei * If the reread partition isn't from release path, lo_refcnt 48706f0e9e6SMing Lei * must be at least one and it can only become zero when the 48806f0e9e6SMing Lei * current holder is released. 48906f0e9e6SMing Lei */ 49006f0e9e6SMing Lei if (!atomic_read(&lo->lo_refcnt)) 49106f0e9e6SMing Lei rc = __blkdev_reread_part(bdev); 49206f0e9e6SMing Lei else 49306f0e9e6SMing Lei rc = blkdev_reread_part(bdev); 49406f0e9e6SMing Lei if (rc) 49506f0e9e6SMing Lei pr_warn("%s: partition scan of loop%d (%s) failed (rc=%d)\n", 49606f0e9e6SMing Lei __func__, lo->lo_number, lo->lo_file_name, rc); 49706f0e9e6SMing Lei } 49806f0e9e6SMing Lei 4991da177e4SLinus Torvalds /* 5001da177e4SLinus Torvalds * loop_change_fd switched the backing store of a loopback device to 5011da177e4SLinus Torvalds * a new file. This is useful for operating system installers to free up 5021da177e4SLinus Torvalds * the original file and in High Availability environments to switch to 5031da177e4SLinus Torvalds * an alternative location for the content in case of server meltdown. 5041da177e4SLinus Torvalds * This can only work if the loop device is used read-only, and if the 5051da177e4SLinus Torvalds * new backing store is the same size and type as the old backing store. 5061da177e4SLinus Torvalds */ 507bb214884SAl Viro static int loop_change_fd(struct loop_device *lo, struct block_device *bdev, 508bb214884SAl Viro unsigned int arg) 5091da177e4SLinus Torvalds { 5101da177e4SLinus Torvalds struct file *file, *old_file; 5111da177e4SLinus Torvalds struct inode *inode; 5121da177e4SLinus Torvalds int error; 5131da177e4SLinus Torvalds 5141da177e4SLinus Torvalds error = -ENXIO; 5151da177e4SLinus Torvalds if (lo->lo_state != Lo_bound) 5161da177e4SLinus Torvalds goto out; 5171da177e4SLinus Torvalds 5181da177e4SLinus Torvalds /* the loop device has to be read-only */ 5191da177e4SLinus Torvalds error = -EINVAL; 5201da177e4SLinus Torvalds if (!(lo->lo_flags & LO_FLAGS_READ_ONLY)) 5211da177e4SLinus Torvalds goto out; 5221da177e4SLinus Torvalds 5231da177e4SLinus Torvalds error = -EBADF; 5241da177e4SLinus Torvalds file = fget(arg); 5251da177e4SLinus Torvalds if (!file) 5261da177e4SLinus Torvalds goto out; 5271da177e4SLinus Torvalds 5281da177e4SLinus Torvalds inode = file->f_mapping->host; 5291da177e4SLinus Torvalds old_file = lo->lo_backing_file; 5301da177e4SLinus Torvalds 5311da177e4SLinus Torvalds error = -EINVAL; 5321da177e4SLinus Torvalds 5331da177e4SLinus Torvalds if (!S_ISREG(inode->i_mode) && !S_ISBLK(inode->i_mode)) 5341da177e4SLinus Torvalds goto out_putf; 5351da177e4SLinus Torvalds 5361da177e4SLinus Torvalds /* size of the new backing store needs to be the same */ 5371da177e4SLinus Torvalds if (get_loop_size(lo, file) != get_loop_size(lo, old_file)) 5381da177e4SLinus Torvalds goto out_putf; 5391da177e4SLinus Torvalds 5401da177e4SLinus Torvalds /* and ... switch */ 5411da177e4SLinus Torvalds error = loop_switch(lo, file); 5421da177e4SLinus Torvalds if (error) 5431da177e4SLinus Torvalds goto out_putf; 5441da177e4SLinus Torvalds 5451da177e4SLinus Torvalds fput(old_file); 546e03c8dd1SKay Sievers if (lo->lo_flags & LO_FLAGS_PARTSCAN) 54706f0e9e6SMing Lei loop_reread_partitions(lo, bdev); 5481da177e4SLinus Torvalds return 0; 5491da177e4SLinus Torvalds 5501da177e4SLinus Torvalds out_putf: 5511da177e4SLinus Torvalds fput(file); 5521da177e4SLinus Torvalds out: 5531da177e4SLinus Torvalds return error; 5541da177e4SLinus Torvalds } 5551da177e4SLinus Torvalds 5561da177e4SLinus Torvalds static inline int is_loop_device(struct file *file) 5571da177e4SLinus Torvalds { 5581da177e4SLinus Torvalds struct inode *i = file->f_mapping->host; 5591da177e4SLinus Torvalds 5601da177e4SLinus Torvalds return i && S_ISBLK(i->i_mode) && MAJOR(i->i_rdev) == LOOP_MAJOR; 5611da177e4SLinus Torvalds } 5621da177e4SLinus Torvalds 563ee862730SMilan Broz /* loop sysfs attributes */ 564ee862730SMilan Broz 565ee862730SMilan Broz static ssize_t loop_attr_show(struct device *dev, char *page, 566ee862730SMilan Broz ssize_t (*callback)(struct loop_device *, char *)) 567ee862730SMilan Broz { 56834dd82afSKay Sievers struct gendisk *disk = dev_to_disk(dev); 56934dd82afSKay Sievers struct loop_device *lo = disk->private_data; 570ee862730SMilan Broz 57134dd82afSKay Sievers return callback(lo, page); 572ee862730SMilan Broz } 573ee862730SMilan Broz 574ee862730SMilan Broz #define LOOP_ATTR_RO(_name) \ 575ee862730SMilan Broz static ssize_t loop_attr_##_name##_show(struct loop_device *, char *); \ 576ee862730SMilan Broz static ssize_t loop_attr_do_show_##_name(struct device *d, \ 577ee862730SMilan Broz struct device_attribute *attr, char *b) \ 578ee862730SMilan Broz { \ 579ee862730SMilan Broz return loop_attr_show(d, b, loop_attr_##_name##_show); \ 580ee862730SMilan Broz } \ 581ee862730SMilan Broz static struct device_attribute loop_attr_##_name = \ 582ee862730SMilan Broz __ATTR(_name, S_IRUGO, loop_attr_do_show_##_name, NULL); 583ee862730SMilan Broz 584ee862730SMilan Broz static ssize_t loop_attr_backing_file_show(struct loop_device *lo, char *buf) 585ee862730SMilan Broz { 586ee862730SMilan Broz ssize_t ret; 587ee862730SMilan Broz char *p = NULL; 588ee862730SMilan Broz 58905eb0f25SKay Sievers spin_lock_irq(&lo->lo_lock); 590ee862730SMilan Broz if (lo->lo_backing_file) 5919bf39ab2SMiklos Szeredi p = file_path(lo->lo_backing_file, buf, PAGE_SIZE - 1); 59205eb0f25SKay Sievers spin_unlock_irq(&lo->lo_lock); 593ee862730SMilan Broz 594ee862730SMilan Broz if (IS_ERR_OR_NULL(p)) 595ee862730SMilan Broz ret = PTR_ERR(p); 596ee862730SMilan Broz else { 597ee862730SMilan Broz ret = strlen(p); 598ee862730SMilan Broz memmove(buf, p, ret); 599ee862730SMilan Broz buf[ret++] = '\n'; 600ee862730SMilan Broz buf[ret] = 0; 601ee862730SMilan Broz } 602ee862730SMilan Broz 603ee862730SMilan Broz return ret; 604ee862730SMilan Broz } 605ee862730SMilan Broz 606ee862730SMilan Broz static ssize_t loop_attr_offset_show(struct loop_device *lo, char *buf) 607ee862730SMilan Broz { 608ee862730SMilan Broz return sprintf(buf, "%llu\n", (unsigned long long)lo->lo_offset); 609ee862730SMilan Broz } 610ee862730SMilan Broz 611ee862730SMilan Broz static ssize_t loop_attr_sizelimit_show(struct loop_device *lo, char *buf) 612ee862730SMilan Broz { 613ee862730SMilan Broz return sprintf(buf, "%llu\n", (unsigned long long)lo->lo_sizelimit); 614ee862730SMilan Broz } 615ee862730SMilan Broz 616ee862730SMilan Broz static ssize_t loop_attr_autoclear_show(struct loop_device *lo, char *buf) 617ee862730SMilan Broz { 618ee862730SMilan Broz int autoclear = (lo->lo_flags & LO_FLAGS_AUTOCLEAR); 619ee862730SMilan Broz 620ee862730SMilan Broz return sprintf(buf, "%s\n", autoclear ? "1" : "0"); 621ee862730SMilan Broz } 622ee862730SMilan Broz 623e03c8dd1SKay Sievers static ssize_t loop_attr_partscan_show(struct loop_device *lo, char *buf) 624e03c8dd1SKay Sievers { 625e03c8dd1SKay Sievers int partscan = (lo->lo_flags & LO_FLAGS_PARTSCAN); 626e03c8dd1SKay Sievers 627e03c8dd1SKay Sievers return sprintf(buf, "%s\n", partscan ? "1" : "0"); 628e03c8dd1SKay Sievers } 629e03c8dd1SKay Sievers 630ee862730SMilan Broz LOOP_ATTR_RO(backing_file); 631ee862730SMilan Broz LOOP_ATTR_RO(offset); 632ee862730SMilan Broz LOOP_ATTR_RO(sizelimit); 633ee862730SMilan Broz LOOP_ATTR_RO(autoclear); 634e03c8dd1SKay Sievers LOOP_ATTR_RO(partscan); 635ee862730SMilan Broz 636ee862730SMilan Broz static struct attribute *loop_attrs[] = { 637ee862730SMilan Broz &loop_attr_backing_file.attr, 638ee862730SMilan Broz &loop_attr_offset.attr, 639ee862730SMilan Broz &loop_attr_sizelimit.attr, 640ee862730SMilan Broz &loop_attr_autoclear.attr, 641e03c8dd1SKay Sievers &loop_attr_partscan.attr, 642ee862730SMilan Broz NULL, 643ee862730SMilan Broz }; 644ee862730SMilan Broz 645ee862730SMilan Broz static struct attribute_group loop_attribute_group = { 646ee862730SMilan Broz .name = "loop", 647ee862730SMilan Broz .attrs= loop_attrs, 648ee862730SMilan Broz }; 649ee862730SMilan Broz 650ee862730SMilan Broz static int loop_sysfs_init(struct loop_device *lo) 651ee862730SMilan Broz { 652ee862730SMilan Broz return sysfs_create_group(&disk_to_dev(lo->lo_disk)->kobj, 653ee862730SMilan Broz &loop_attribute_group); 654ee862730SMilan Broz } 655ee862730SMilan Broz 656ee862730SMilan Broz static void loop_sysfs_exit(struct loop_device *lo) 657ee862730SMilan Broz { 658ee862730SMilan Broz sysfs_remove_group(&disk_to_dev(lo->lo_disk)->kobj, 659ee862730SMilan Broz &loop_attribute_group); 660ee862730SMilan Broz } 661ee862730SMilan Broz 662dfaa2ef6SLukas Czerner static void loop_config_discard(struct loop_device *lo) 663dfaa2ef6SLukas Czerner { 664dfaa2ef6SLukas Czerner struct file *file = lo->lo_backing_file; 665dfaa2ef6SLukas Czerner struct inode *inode = file->f_mapping->host; 666dfaa2ef6SLukas Czerner struct request_queue *q = lo->lo_queue; 667dfaa2ef6SLukas Czerner 668dfaa2ef6SLukas Czerner /* 669dfaa2ef6SLukas Czerner * We use punch hole to reclaim the free space used by the 67012a64d2fSOlaf Hering * image a.k.a. discard. However we do not support discard if 671dfaa2ef6SLukas Czerner * encryption is enabled, because it may give an attacker 672dfaa2ef6SLukas Czerner * useful information. 673dfaa2ef6SLukas Czerner */ 674dfaa2ef6SLukas Czerner if ((!file->f_op->fallocate) || 675dfaa2ef6SLukas Czerner lo->lo_encrypt_key_size) { 676dfaa2ef6SLukas Czerner q->limits.discard_granularity = 0; 677dfaa2ef6SLukas Czerner q->limits.discard_alignment = 0; 6782bb4cd5cSJens Axboe blk_queue_max_discard_sectors(q, 0); 679dfaa2ef6SLukas Czerner q->limits.discard_zeroes_data = 0; 680dfaa2ef6SLukas Czerner queue_flag_clear_unlocked(QUEUE_FLAG_DISCARD, q); 681dfaa2ef6SLukas Czerner return; 682dfaa2ef6SLukas Czerner } 683dfaa2ef6SLukas Czerner 684dfaa2ef6SLukas Czerner q->limits.discard_granularity = inode->i_sb->s_blocksize; 685dfaf3c03SLukas Czerner q->limits.discard_alignment = 0; 6862bb4cd5cSJens Axboe blk_queue_max_discard_sectors(q, UINT_MAX >> 9); 687dfaa2ef6SLukas Czerner q->limits.discard_zeroes_data = 1; 688dfaa2ef6SLukas Czerner queue_flag_set_unlocked(QUEUE_FLAG_DISCARD, q); 689dfaa2ef6SLukas Czerner } 690dfaa2ef6SLukas Czerner 691bb214884SAl Viro static int loop_set_fd(struct loop_device *lo, fmode_t mode, 6921da177e4SLinus Torvalds struct block_device *bdev, unsigned int arg) 6931da177e4SLinus Torvalds { 6941da177e4SLinus Torvalds struct file *file, *f; 6951da177e4SLinus Torvalds struct inode *inode; 6961da177e4SLinus Torvalds struct address_space *mapping; 6971da177e4SLinus Torvalds unsigned lo_blocksize; 6981da177e4SLinus Torvalds int lo_flags = 0; 6991da177e4SLinus Torvalds int error; 7001da177e4SLinus Torvalds loff_t size; 7011da177e4SLinus Torvalds 7021da177e4SLinus Torvalds /* This is safe, since we have a reference from open(). */ 7031da177e4SLinus Torvalds __module_get(THIS_MODULE); 7041da177e4SLinus Torvalds 7051da177e4SLinus Torvalds error = -EBADF; 7061da177e4SLinus Torvalds file = fget(arg); 7071da177e4SLinus Torvalds if (!file) 7081da177e4SLinus Torvalds goto out; 7091da177e4SLinus Torvalds 7101da177e4SLinus Torvalds error = -EBUSY; 7111da177e4SLinus Torvalds if (lo->lo_state != Lo_unbound) 7121da177e4SLinus Torvalds goto out_putf; 7131da177e4SLinus Torvalds 7141da177e4SLinus Torvalds /* Avoid recursion */ 7151da177e4SLinus Torvalds f = file; 7161da177e4SLinus Torvalds while (is_loop_device(f)) { 7171da177e4SLinus Torvalds struct loop_device *l; 7181da177e4SLinus Torvalds 719bb214884SAl Viro if (f->f_mapping->host->i_bdev == bdev) 7201da177e4SLinus Torvalds goto out_putf; 7211da177e4SLinus Torvalds 7221da177e4SLinus Torvalds l = f->f_mapping->host->i_bdev->bd_disk->private_data; 7231da177e4SLinus Torvalds if (l->lo_state == Lo_unbound) { 7241da177e4SLinus Torvalds error = -EINVAL; 7251da177e4SLinus Torvalds goto out_putf; 7261da177e4SLinus Torvalds } 7271da177e4SLinus Torvalds f = l->lo_backing_file; 7281da177e4SLinus Torvalds } 7291da177e4SLinus Torvalds 7301da177e4SLinus Torvalds mapping = file->f_mapping; 7311da177e4SLinus Torvalds inode = mapping->host; 7321da177e4SLinus Torvalds 7331da177e4SLinus Torvalds error = -EINVAL; 734456be148SChristoph Hellwig if (!S_ISREG(inode->i_mode) && !S_ISBLK(inode->i_mode)) 735456be148SChristoph Hellwig goto out_putf; 7366818173bSMiklos Szeredi 737456be148SChristoph Hellwig if (!(file->f_mode & FMODE_WRITE) || !(mode & FMODE_WRITE) || 738283e7e5dSAl Viro !file->f_op->write_iter) 7391da177e4SLinus Torvalds lo_flags |= LO_FLAGS_READ_ONLY; 7401da177e4SLinus Torvalds 741ba52de12STheodore Ts'o lo_blocksize = S_ISBLK(inode->i_mode) ? 742ba52de12STheodore Ts'o inode->i_bdev->bd_block_size : PAGE_SIZE; 743ba52de12STheodore Ts'o 7441da177e4SLinus Torvalds error = -EFBIG; 745456be148SChristoph Hellwig size = get_loop_size(lo, file); 746456be148SChristoph Hellwig if ((loff_t)(sector_t)size != size) 7471da177e4SLinus Torvalds goto out_putf; 748f4aa4c7bSMing Lei error = -ENOMEM; 749f4aa4c7bSMing Lei lo->wq = alloc_workqueue("kloopd%d", 7504d4e41aeSMing Lei WQ_MEM_RECLAIM | WQ_HIGHPRI | WQ_UNBOUND, 16, 751f4aa4c7bSMing Lei lo->lo_number); 752f4aa4c7bSMing Lei if (!lo->wq) 753f4aa4c7bSMing Lei goto out_putf; 7541da177e4SLinus Torvalds 755456be148SChristoph Hellwig error = 0; 7561da177e4SLinus Torvalds 7571da177e4SLinus Torvalds set_device_ro(bdev, (lo_flags & LO_FLAGS_READ_ONLY) != 0); 7581da177e4SLinus Torvalds 7591da177e4SLinus Torvalds lo->lo_blocksize = lo_blocksize; 7601da177e4SLinus Torvalds lo->lo_device = bdev; 7611da177e4SLinus Torvalds lo->lo_flags = lo_flags; 7621da177e4SLinus Torvalds lo->lo_backing_file = file; 763aa4d8616SChristoph Hellwig lo->transfer = NULL; 7641da177e4SLinus Torvalds lo->ioctl = NULL; 7651da177e4SLinus Torvalds lo->lo_sizelimit = 0; 7661da177e4SLinus Torvalds lo->old_gfp_mask = mapping_gfp_mask(mapping); 7671da177e4SLinus Torvalds mapping_set_gfp_mask(mapping, lo->old_gfp_mask & ~(__GFP_IO|__GFP_FS)); 7681da177e4SLinus Torvalds 76968db1961SNikanth Karthikesan if (!(lo_flags & LO_FLAGS_READ_ONLY) && file->f_op->fsync) 7704913efe4STejun Heo blk_queue_flush(lo->lo_queue, REQ_FLUSH); 77168db1961SNikanth Karthikesan 77273285082SKen Chen set_capacity(lo->lo_disk, size); 7731da177e4SLinus Torvalds bd_set_size(bdev, size << 9); 774ee862730SMilan Broz loop_sysfs_init(lo); 775c3473c63SDavid Zeuthen /* let user-space know about the new size */ 776c3473c63SDavid Zeuthen kobject_uevent(&disk_to_dev(bdev->bd_disk)->kobj, KOBJ_CHANGE); 7771da177e4SLinus Torvalds 7781da177e4SLinus Torvalds set_blocksize(bdev, lo_blocksize); 7791da177e4SLinus Torvalds 7806c997918SSerge E. Hallyn lo->lo_state = Lo_bound; 781e03c8dd1SKay Sievers if (part_shift) 782e03c8dd1SKay Sievers lo->lo_flags |= LO_FLAGS_PARTSCAN; 783e03c8dd1SKay Sievers if (lo->lo_flags & LO_FLAGS_PARTSCAN) 78406f0e9e6SMing Lei loop_reread_partitions(lo, bdev); 785c1681bf8SAnatol Pomozov 786c1681bf8SAnatol Pomozov /* Grab the block_device to prevent its destruction after we 787c1681bf8SAnatol Pomozov * put /dev/loopXX inode. Later in loop_clr_fd() we bdput(bdev). 788c1681bf8SAnatol Pomozov */ 789c1681bf8SAnatol Pomozov bdgrab(bdev); 7901da177e4SLinus Torvalds return 0; 7911da177e4SLinus Torvalds 7921da177e4SLinus Torvalds out_putf: 7931da177e4SLinus Torvalds fput(file); 7941da177e4SLinus Torvalds out: 7951da177e4SLinus Torvalds /* This is safe: open() is still holding a reference. */ 7961da177e4SLinus Torvalds module_put(THIS_MODULE); 7971da177e4SLinus Torvalds return error; 7981da177e4SLinus Torvalds } 7991da177e4SLinus Torvalds 8001da177e4SLinus Torvalds static int 8011da177e4SLinus Torvalds loop_release_xfer(struct loop_device *lo) 8021da177e4SLinus Torvalds { 8031da177e4SLinus Torvalds int err = 0; 8041da177e4SLinus Torvalds struct loop_func_table *xfer = lo->lo_encryption; 8051da177e4SLinus Torvalds 8061da177e4SLinus Torvalds if (xfer) { 8071da177e4SLinus Torvalds if (xfer->release) 8081da177e4SLinus Torvalds err = xfer->release(lo); 8091da177e4SLinus Torvalds lo->transfer = NULL; 8101da177e4SLinus Torvalds lo->lo_encryption = NULL; 8111da177e4SLinus Torvalds module_put(xfer->owner); 8121da177e4SLinus Torvalds } 8131da177e4SLinus Torvalds return err; 8141da177e4SLinus Torvalds } 8151da177e4SLinus Torvalds 8161da177e4SLinus Torvalds static int 8171da177e4SLinus Torvalds loop_init_xfer(struct loop_device *lo, struct loop_func_table *xfer, 8181da177e4SLinus Torvalds const struct loop_info64 *i) 8191da177e4SLinus Torvalds { 8201da177e4SLinus Torvalds int err = 0; 8211da177e4SLinus Torvalds 8221da177e4SLinus Torvalds if (xfer) { 8231da177e4SLinus Torvalds struct module *owner = xfer->owner; 8241da177e4SLinus Torvalds 8251da177e4SLinus Torvalds if (!try_module_get(owner)) 8261da177e4SLinus Torvalds return -EINVAL; 8271da177e4SLinus Torvalds if (xfer->init) 8281da177e4SLinus Torvalds err = xfer->init(lo, i); 8291da177e4SLinus Torvalds if (err) 8301da177e4SLinus Torvalds module_put(owner); 8311da177e4SLinus Torvalds else 8321da177e4SLinus Torvalds lo->lo_encryption = xfer; 8331da177e4SLinus Torvalds } 8341da177e4SLinus Torvalds return err; 8351da177e4SLinus Torvalds } 8361da177e4SLinus Torvalds 8374c823cc3SAyan George static int loop_clr_fd(struct loop_device *lo) 8381da177e4SLinus Torvalds { 8391da177e4SLinus Torvalds struct file *filp = lo->lo_backing_file; 840b4e3ca1aSAl Viro gfp_t gfp = lo->old_gfp_mask; 8414c823cc3SAyan George struct block_device *bdev = lo->lo_device; 8421da177e4SLinus Torvalds 8431da177e4SLinus Torvalds if (lo->lo_state != Lo_bound) 8441da177e4SLinus Torvalds return -ENXIO; 8451da177e4SLinus Torvalds 846a1ecac3bSDave Chinner /* 847a1ecac3bSDave Chinner * If we've explicitly asked to tear down the loop device, 848a1ecac3bSDave Chinner * and it has an elevated reference count, set it for auto-teardown when 849a1ecac3bSDave Chinner * the last reference goes away. This stops $!~#$@ udev from 850a1ecac3bSDave Chinner * preventing teardown because it decided that it needs to run blkid on 851a1ecac3bSDave Chinner * the loopback device whenever they appear. xfstests is notorious for 852a1ecac3bSDave Chinner * failing tests because blkid via udev races with a losetup 853a1ecac3bSDave Chinner * <dev>/do something like mkfs/losetup -d <dev> causing the losetup -d 854a1ecac3bSDave Chinner * command to fail with EBUSY. 855a1ecac3bSDave Chinner */ 856f8933667SMing Lei if (atomic_read(&lo->lo_refcnt) > 1) { 857a1ecac3bSDave Chinner lo->lo_flags |= LO_FLAGS_AUTOCLEAR; 858a1ecac3bSDave Chinner mutex_unlock(&lo->lo_ctl_mutex); 859a1ecac3bSDave Chinner return 0; 860a1ecac3bSDave Chinner } 8611da177e4SLinus Torvalds 8621da177e4SLinus Torvalds if (filp == NULL) 8631da177e4SLinus Torvalds return -EINVAL; 8641da177e4SLinus Torvalds 865f8933667SMing Lei /* freeze request queue during the transition */ 866f8933667SMing Lei blk_mq_freeze_queue(lo->lo_queue); 867f8933667SMing Lei 8681da177e4SLinus Torvalds spin_lock_irq(&lo->lo_lock); 8691da177e4SLinus Torvalds lo->lo_state = Lo_rundown; 8701da177e4SLinus Torvalds lo->lo_backing_file = NULL; 87105eb0f25SKay Sievers spin_unlock_irq(&lo->lo_lock); 8721da177e4SLinus Torvalds 8731da177e4SLinus Torvalds loop_release_xfer(lo); 8741da177e4SLinus Torvalds lo->transfer = NULL; 8751da177e4SLinus Torvalds lo->ioctl = NULL; 8761da177e4SLinus Torvalds lo->lo_device = NULL; 8771da177e4SLinus Torvalds lo->lo_encryption = NULL; 8781da177e4SLinus Torvalds lo->lo_offset = 0; 8791da177e4SLinus Torvalds lo->lo_sizelimit = 0; 8801da177e4SLinus Torvalds lo->lo_encrypt_key_size = 0; 8811da177e4SLinus Torvalds memset(lo->lo_encrypt_key, 0, LO_KEY_SIZE); 8821da177e4SLinus Torvalds memset(lo->lo_crypt_name, 0, LO_NAME_SIZE); 8831da177e4SLinus Torvalds memset(lo->lo_file_name, 0, LO_NAME_SIZE); 884c1681bf8SAnatol Pomozov if (bdev) { 885c1681bf8SAnatol Pomozov bdput(bdev); 886f98393a6SPeter Zijlstra invalidate_bdev(bdev); 887c1681bf8SAnatol Pomozov } 88873285082SKen Chen set_capacity(lo->lo_disk, 0); 88951a0bb0cSMilan Broz loop_sysfs_exit(lo); 890c3473c63SDavid Zeuthen if (bdev) { 8911da177e4SLinus Torvalds bd_set_size(bdev, 0); 892c3473c63SDavid Zeuthen /* let user-space know about this change */ 893c3473c63SDavid Zeuthen kobject_uevent(&disk_to_dev(bdev->bd_disk)->kobj, KOBJ_CHANGE); 894c3473c63SDavid Zeuthen } 8951da177e4SLinus Torvalds mapping_set_gfp_mask(filp->f_mapping, gfp); 8961da177e4SLinus Torvalds lo->lo_state = Lo_unbound; 8971da177e4SLinus Torvalds /* This is safe: open() is still holding a reference. */ 8981da177e4SLinus Torvalds module_put(THIS_MODULE); 899f8933667SMing Lei blk_mq_unfreeze_queue(lo->lo_queue); 900f8933667SMing Lei 901c2fccc1cSJens Axboe if (lo->lo_flags & LO_FLAGS_PARTSCAN && bdev) 90206f0e9e6SMing Lei loop_reread_partitions(lo, bdev); 903e03c8dd1SKay Sievers lo->lo_flags = 0; 904e03c8dd1SKay Sievers if (!part_shift) 905e03c8dd1SKay Sievers lo->lo_disk->flags |= GENHD_FL_NO_PART_SCAN; 906f4aa4c7bSMing Lei destroy_workqueue(lo->wq); 907f4aa4c7bSMing Lei lo->wq = NULL; 908f028f3b2SNikanth Karthikesan mutex_unlock(&lo->lo_ctl_mutex); 909f028f3b2SNikanth Karthikesan /* 910f028f3b2SNikanth Karthikesan * Need not hold lo_ctl_mutex to fput backing file. 911f028f3b2SNikanth Karthikesan * Calling fput holding lo_ctl_mutex triggers a circular 912f028f3b2SNikanth Karthikesan * lock dependency possibility warning as fput can take 913f028f3b2SNikanth Karthikesan * bd_mutex which is usually taken before lo_ctl_mutex. 914f028f3b2SNikanth Karthikesan */ 915f028f3b2SNikanth Karthikesan fput(filp); 9161da177e4SLinus Torvalds return 0; 9171da177e4SLinus Torvalds } 9181da177e4SLinus Torvalds 9191da177e4SLinus Torvalds static int 9201da177e4SLinus Torvalds loop_set_status(struct loop_device *lo, const struct loop_info64 *info) 9211da177e4SLinus Torvalds { 9221da177e4SLinus Torvalds int err; 9231da177e4SLinus Torvalds struct loop_func_table *xfer; 924e4849737SEric W. Biederman kuid_t uid = current_uid(); 9251da177e4SLinus Torvalds 926b0fafa81SDavid Howells if (lo->lo_encrypt_key_size && 927e4849737SEric W. Biederman !uid_eq(lo->lo_key_owner, uid) && 9281da177e4SLinus Torvalds !capable(CAP_SYS_ADMIN)) 9291da177e4SLinus Torvalds return -EPERM; 9301da177e4SLinus Torvalds if (lo->lo_state != Lo_bound) 9311da177e4SLinus Torvalds return -ENXIO; 9321da177e4SLinus Torvalds if ((unsigned int) info->lo_encrypt_key_size > LO_KEY_SIZE) 9331da177e4SLinus Torvalds return -EINVAL; 9341da177e4SLinus Torvalds 9351da177e4SLinus Torvalds err = loop_release_xfer(lo); 9361da177e4SLinus Torvalds if (err) 9371da177e4SLinus Torvalds return err; 9381da177e4SLinus Torvalds 9391da177e4SLinus Torvalds if (info->lo_encrypt_type) { 9401da177e4SLinus Torvalds unsigned int type = info->lo_encrypt_type; 9411da177e4SLinus Torvalds 9421da177e4SLinus Torvalds if (type >= MAX_LO_CRYPT) 9431da177e4SLinus Torvalds return -EINVAL; 9441da177e4SLinus Torvalds xfer = xfer_funcs[type]; 9451da177e4SLinus Torvalds if (xfer == NULL) 9461da177e4SLinus Torvalds return -EINVAL; 9471da177e4SLinus Torvalds } else 9481da177e4SLinus Torvalds xfer = NULL; 9491da177e4SLinus Torvalds 9501da177e4SLinus Torvalds err = loop_init_xfer(lo, xfer, info); 9511da177e4SLinus Torvalds if (err) 9521da177e4SLinus Torvalds return err; 9531da177e4SLinus Torvalds 9541da177e4SLinus Torvalds if (lo->lo_offset != info->lo_offset || 9557b0576a3SGuo Chao lo->lo_sizelimit != info->lo_sizelimit) 9567035b5dfSDmitry Monakhov if (figure_loop_size(lo, info->lo_offset, info->lo_sizelimit)) 9571da177e4SLinus Torvalds return -EFBIG; 958541c742aSGuo Chao 959dfaa2ef6SLukas Czerner loop_config_discard(lo); 9601da177e4SLinus Torvalds 9611da177e4SLinus Torvalds memcpy(lo->lo_file_name, info->lo_file_name, LO_NAME_SIZE); 9621da177e4SLinus Torvalds memcpy(lo->lo_crypt_name, info->lo_crypt_name, LO_NAME_SIZE); 9631da177e4SLinus Torvalds lo->lo_file_name[LO_NAME_SIZE-1] = 0; 9641da177e4SLinus Torvalds lo->lo_crypt_name[LO_NAME_SIZE-1] = 0; 9651da177e4SLinus Torvalds 9661da177e4SLinus Torvalds if (!xfer) 9671da177e4SLinus Torvalds xfer = &none_funcs; 9681da177e4SLinus Torvalds lo->transfer = xfer->transfer; 9691da177e4SLinus Torvalds lo->ioctl = xfer->ioctl; 9701da177e4SLinus Torvalds 97196c58655SDavid Woodhouse if ((lo->lo_flags & LO_FLAGS_AUTOCLEAR) != 97296c58655SDavid Woodhouse (info->lo_flags & LO_FLAGS_AUTOCLEAR)) 97396c58655SDavid Woodhouse lo->lo_flags ^= LO_FLAGS_AUTOCLEAR; 97496c58655SDavid Woodhouse 975e03c8dd1SKay Sievers if ((info->lo_flags & LO_FLAGS_PARTSCAN) && 976e03c8dd1SKay Sievers !(lo->lo_flags & LO_FLAGS_PARTSCAN)) { 977e03c8dd1SKay Sievers lo->lo_flags |= LO_FLAGS_PARTSCAN; 978e03c8dd1SKay Sievers lo->lo_disk->flags &= ~GENHD_FL_NO_PART_SCAN; 97906f0e9e6SMing Lei loop_reread_partitions(lo, lo->lo_device); 980e03c8dd1SKay Sievers } 981e03c8dd1SKay Sievers 9821da177e4SLinus Torvalds lo->lo_encrypt_key_size = info->lo_encrypt_key_size; 9831da177e4SLinus Torvalds lo->lo_init[0] = info->lo_init[0]; 9841da177e4SLinus Torvalds lo->lo_init[1] = info->lo_init[1]; 9851da177e4SLinus Torvalds if (info->lo_encrypt_key_size) { 9861da177e4SLinus Torvalds memcpy(lo->lo_encrypt_key, info->lo_encrypt_key, 9871da177e4SLinus Torvalds info->lo_encrypt_key_size); 988b0fafa81SDavid Howells lo->lo_key_owner = uid; 9891da177e4SLinus Torvalds } 9901da177e4SLinus Torvalds 9911da177e4SLinus Torvalds return 0; 9921da177e4SLinus Torvalds } 9931da177e4SLinus Torvalds 9941da177e4SLinus Torvalds static int 9951da177e4SLinus Torvalds loop_get_status(struct loop_device *lo, struct loop_info64 *info) 9961da177e4SLinus Torvalds { 9971da177e4SLinus Torvalds struct file *file = lo->lo_backing_file; 9981da177e4SLinus Torvalds struct kstat stat; 9991da177e4SLinus Torvalds int error; 10001da177e4SLinus Torvalds 10011da177e4SLinus Torvalds if (lo->lo_state != Lo_bound) 10021da177e4SLinus Torvalds return -ENXIO; 10033dadecceSAl Viro error = vfs_getattr(&file->f_path, &stat); 10041da177e4SLinus Torvalds if (error) 10051da177e4SLinus Torvalds return error; 10061da177e4SLinus Torvalds memset(info, 0, sizeof(*info)); 10071da177e4SLinus Torvalds info->lo_number = lo->lo_number; 10081da177e4SLinus Torvalds info->lo_device = huge_encode_dev(stat.dev); 10091da177e4SLinus Torvalds info->lo_inode = stat.ino; 10101da177e4SLinus Torvalds info->lo_rdevice = huge_encode_dev(lo->lo_device ? stat.rdev : stat.dev); 10111da177e4SLinus Torvalds info->lo_offset = lo->lo_offset; 10121da177e4SLinus Torvalds info->lo_sizelimit = lo->lo_sizelimit; 10131da177e4SLinus Torvalds info->lo_flags = lo->lo_flags; 10141da177e4SLinus Torvalds memcpy(info->lo_file_name, lo->lo_file_name, LO_NAME_SIZE); 10151da177e4SLinus Torvalds memcpy(info->lo_crypt_name, lo->lo_crypt_name, LO_NAME_SIZE); 10161da177e4SLinus Torvalds info->lo_encrypt_type = 10171da177e4SLinus Torvalds lo->lo_encryption ? lo->lo_encryption->number : 0; 10181da177e4SLinus Torvalds if (lo->lo_encrypt_key_size && capable(CAP_SYS_ADMIN)) { 10191da177e4SLinus Torvalds info->lo_encrypt_key_size = lo->lo_encrypt_key_size; 10201da177e4SLinus Torvalds memcpy(info->lo_encrypt_key, lo->lo_encrypt_key, 10211da177e4SLinus Torvalds lo->lo_encrypt_key_size); 10221da177e4SLinus Torvalds } 10231da177e4SLinus Torvalds return 0; 10241da177e4SLinus Torvalds } 10251da177e4SLinus Torvalds 10261da177e4SLinus Torvalds static void 10271da177e4SLinus Torvalds loop_info64_from_old(const struct loop_info *info, struct loop_info64 *info64) 10281da177e4SLinus Torvalds { 10291da177e4SLinus Torvalds memset(info64, 0, sizeof(*info64)); 10301da177e4SLinus Torvalds info64->lo_number = info->lo_number; 10311da177e4SLinus Torvalds info64->lo_device = info->lo_device; 10321da177e4SLinus Torvalds info64->lo_inode = info->lo_inode; 10331da177e4SLinus Torvalds info64->lo_rdevice = info->lo_rdevice; 10341da177e4SLinus Torvalds info64->lo_offset = info->lo_offset; 10351da177e4SLinus Torvalds info64->lo_sizelimit = 0; 10361da177e4SLinus Torvalds info64->lo_encrypt_type = info->lo_encrypt_type; 10371da177e4SLinus Torvalds info64->lo_encrypt_key_size = info->lo_encrypt_key_size; 10381da177e4SLinus Torvalds info64->lo_flags = info->lo_flags; 10391da177e4SLinus Torvalds info64->lo_init[0] = info->lo_init[0]; 10401da177e4SLinus Torvalds info64->lo_init[1] = info->lo_init[1]; 10411da177e4SLinus Torvalds if (info->lo_encrypt_type == LO_CRYPT_CRYPTOAPI) 10421da177e4SLinus Torvalds memcpy(info64->lo_crypt_name, info->lo_name, LO_NAME_SIZE); 10431da177e4SLinus Torvalds else 10441da177e4SLinus Torvalds memcpy(info64->lo_file_name, info->lo_name, LO_NAME_SIZE); 10451da177e4SLinus Torvalds memcpy(info64->lo_encrypt_key, info->lo_encrypt_key, LO_KEY_SIZE); 10461da177e4SLinus Torvalds } 10471da177e4SLinus Torvalds 10481da177e4SLinus Torvalds static int 10491da177e4SLinus Torvalds loop_info64_to_old(const struct loop_info64 *info64, struct loop_info *info) 10501da177e4SLinus Torvalds { 10511da177e4SLinus Torvalds memset(info, 0, sizeof(*info)); 10521da177e4SLinus Torvalds info->lo_number = info64->lo_number; 10531da177e4SLinus Torvalds info->lo_device = info64->lo_device; 10541da177e4SLinus Torvalds info->lo_inode = info64->lo_inode; 10551da177e4SLinus Torvalds info->lo_rdevice = info64->lo_rdevice; 10561da177e4SLinus Torvalds info->lo_offset = info64->lo_offset; 10571da177e4SLinus Torvalds info->lo_encrypt_type = info64->lo_encrypt_type; 10581da177e4SLinus Torvalds info->lo_encrypt_key_size = info64->lo_encrypt_key_size; 10591da177e4SLinus Torvalds info->lo_flags = info64->lo_flags; 10601da177e4SLinus Torvalds info->lo_init[0] = info64->lo_init[0]; 10611da177e4SLinus Torvalds info->lo_init[1] = info64->lo_init[1]; 10621da177e4SLinus Torvalds if (info->lo_encrypt_type == LO_CRYPT_CRYPTOAPI) 10631da177e4SLinus Torvalds memcpy(info->lo_name, info64->lo_crypt_name, LO_NAME_SIZE); 10641da177e4SLinus Torvalds else 10651da177e4SLinus Torvalds memcpy(info->lo_name, info64->lo_file_name, LO_NAME_SIZE); 10661da177e4SLinus Torvalds memcpy(info->lo_encrypt_key, info64->lo_encrypt_key, LO_KEY_SIZE); 10671da177e4SLinus Torvalds 10681da177e4SLinus Torvalds /* error in case values were truncated */ 10691da177e4SLinus Torvalds if (info->lo_device != info64->lo_device || 10701da177e4SLinus Torvalds info->lo_rdevice != info64->lo_rdevice || 10711da177e4SLinus Torvalds info->lo_inode != info64->lo_inode || 10721da177e4SLinus Torvalds info->lo_offset != info64->lo_offset) 10731da177e4SLinus Torvalds return -EOVERFLOW; 10741da177e4SLinus Torvalds 10751da177e4SLinus Torvalds return 0; 10761da177e4SLinus Torvalds } 10771da177e4SLinus Torvalds 10781da177e4SLinus Torvalds static int 10791da177e4SLinus Torvalds loop_set_status_old(struct loop_device *lo, const struct loop_info __user *arg) 10801da177e4SLinus Torvalds { 10811da177e4SLinus Torvalds struct loop_info info; 10821da177e4SLinus Torvalds struct loop_info64 info64; 10831da177e4SLinus Torvalds 10841da177e4SLinus Torvalds if (copy_from_user(&info, arg, sizeof (struct loop_info))) 10851da177e4SLinus Torvalds return -EFAULT; 10861da177e4SLinus Torvalds loop_info64_from_old(&info, &info64); 10871da177e4SLinus Torvalds return loop_set_status(lo, &info64); 10881da177e4SLinus Torvalds } 10891da177e4SLinus Torvalds 10901da177e4SLinus Torvalds static int 10911da177e4SLinus Torvalds loop_set_status64(struct loop_device *lo, const struct loop_info64 __user *arg) 10921da177e4SLinus Torvalds { 10931da177e4SLinus Torvalds struct loop_info64 info64; 10941da177e4SLinus Torvalds 10951da177e4SLinus Torvalds if (copy_from_user(&info64, arg, sizeof (struct loop_info64))) 10961da177e4SLinus Torvalds return -EFAULT; 10971da177e4SLinus Torvalds return loop_set_status(lo, &info64); 10981da177e4SLinus Torvalds } 10991da177e4SLinus Torvalds 11001da177e4SLinus Torvalds static int 11011da177e4SLinus Torvalds loop_get_status_old(struct loop_device *lo, struct loop_info __user *arg) { 11021da177e4SLinus Torvalds struct loop_info info; 11031da177e4SLinus Torvalds struct loop_info64 info64; 11041da177e4SLinus Torvalds int err = 0; 11051da177e4SLinus Torvalds 11061da177e4SLinus Torvalds if (!arg) 11071da177e4SLinus Torvalds err = -EINVAL; 11081da177e4SLinus Torvalds if (!err) 11091da177e4SLinus Torvalds err = loop_get_status(lo, &info64); 11101da177e4SLinus Torvalds if (!err) 11111da177e4SLinus Torvalds err = loop_info64_to_old(&info64, &info); 11121da177e4SLinus Torvalds if (!err && copy_to_user(arg, &info, sizeof(info))) 11131da177e4SLinus Torvalds err = -EFAULT; 11141da177e4SLinus Torvalds 11151da177e4SLinus Torvalds return err; 11161da177e4SLinus Torvalds } 11171da177e4SLinus Torvalds 11181da177e4SLinus Torvalds static int 11191da177e4SLinus Torvalds loop_get_status64(struct loop_device *lo, struct loop_info64 __user *arg) { 11201da177e4SLinus Torvalds struct loop_info64 info64; 11211da177e4SLinus Torvalds int err = 0; 11221da177e4SLinus Torvalds 11231da177e4SLinus Torvalds if (!arg) 11241da177e4SLinus Torvalds err = -EINVAL; 11251da177e4SLinus Torvalds if (!err) 11261da177e4SLinus Torvalds err = loop_get_status(lo, &info64); 11271da177e4SLinus Torvalds if (!err && copy_to_user(arg, &info64, sizeof(info64))) 11281da177e4SLinus Torvalds err = -EFAULT; 11291da177e4SLinus Torvalds 11301da177e4SLinus Torvalds return err; 11311da177e4SLinus Torvalds } 11321da177e4SLinus Torvalds 113353d66608SJ. R. Okajima static int loop_set_capacity(struct loop_device *lo, struct block_device *bdev) 113453d66608SJ. R. Okajima { 113553d66608SJ. R. Okajima if (unlikely(lo->lo_state != Lo_bound)) 11367b0576a3SGuo Chao return -ENXIO; 113753d66608SJ. R. Okajima 11387b0576a3SGuo Chao return figure_loop_size(lo, lo->lo_offset, lo->lo_sizelimit); 113953d66608SJ. R. Okajima } 114053d66608SJ. R. Okajima 1141bb214884SAl Viro static int lo_ioctl(struct block_device *bdev, fmode_t mode, 11421da177e4SLinus Torvalds unsigned int cmd, unsigned long arg) 11431da177e4SLinus Torvalds { 1144bb214884SAl Viro struct loop_device *lo = bdev->bd_disk->private_data; 11451da177e4SLinus Torvalds int err; 11461da177e4SLinus Torvalds 1147f028f3b2SNikanth Karthikesan mutex_lock_nested(&lo->lo_ctl_mutex, 1); 11481da177e4SLinus Torvalds switch (cmd) { 11491da177e4SLinus Torvalds case LOOP_SET_FD: 1150bb214884SAl Viro err = loop_set_fd(lo, mode, bdev, arg); 11511da177e4SLinus Torvalds break; 11521da177e4SLinus Torvalds case LOOP_CHANGE_FD: 1153bb214884SAl Viro err = loop_change_fd(lo, bdev, arg); 11541da177e4SLinus Torvalds break; 11551da177e4SLinus Torvalds case LOOP_CLR_FD: 1156f028f3b2SNikanth Karthikesan /* loop_clr_fd would have unlocked lo_ctl_mutex on success */ 11574c823cc3SAyan George err = loop_clr_fd(lo); 1158f028f3b2SNikanth Karthikesan if (!err) 1159f028f3b2SNikanth Karthikesan goto out_unlocked; 11601da177e4SLinus Torvalds break; 11611da177e4SLinus Torvalds case LOOP_SET_STATUS: 11627035b5dfSDmitry Monakhov err = -EPERM; 11637035b5dfSDmitry Monakhov if ((mode & FMODE_WRITE) || capable(CAP_SYS_ADMIN)) 11647035b5dfSDmitry Monakhov err = loop_set_status_old(lo, 11657035b5dfSDmitry Monakhov (struct loop_info __user *)arg); 11661da177e4SLinus Torvalds break; 11671da177e4SLinus Torvalds case LOOP_GET_STATUS: 11681da177e4SLinus Torvalds err = loop_get_status_old(lo, (struct loop_info __user *) arg); 11691da177e4SLinus Torvalds break; 11701da177e4SLinus Torvalds case LOOP_SET_STATUS64: 11717035b5dfSDmitry Monakhov err = -EPERM; 11727035b5dfSDmitry Monakhov if ((mode & FMODE_WRITE) || capable(CAP_SYS_ADMIN)) 11737035b5dfSDmitry Monakhov err = loop_set_status64(lo, 11747035b5dfSDmitry Monakhov (struct loop_info64 __user *) arg); 11751da177e4SLinus Torvalds break; 11761da177e4SLinus Torvalds case LOOP_GET_STATUS64: 11771da177e4SLinus Torvalds err = loop_get_status64(lo, (struct loop_info64 __user *) arg); 11781da177e4SLinus Torvalds break; 117953d66608SJ. R. Okajima case LOOP_SET_CAPACITY: 118053d66608SJ. R. Okajima err = -EPERM; 118153d66608SJ. R. Okajima if ((mode & FMODE_WRITE) || capable(CAP_SYS_ADMIN)) 118253d66608SJ. R. Okajima err = loop_set_capacity(lo, bdev); 118353d66608SJ. R. Okajima break; 11841da177e4SLinus Torvalds default: 11851da177e4SLinus Torvalds err = lo->ioctl ? lo->ioctl(lo, cmd, arg) : -EINVAL; 11861da177e4SLinus Torvalds } 1187f85221ddSIngo Molnar mutex_unlock(&lo->lo_ctl_mutex); 1188f028f3b2SNikanth Karthikesan 1189f028f3b2SNikanth Karthikesan out_unlocked: 11901da177e4SLinus Torvalds return err; 11911da177e4SLinus Torvalds } 11921da177e4SLinus Torvalds 1193863d5b82SDavid Howells #ifdef CONFIG_COMPAT 1194863d5b82SDavid Howells struct compat_loop_info { 1195863d5b82SDavid Howells compat_int_t lo_number; /* ioctl r/o */ 1196863d5b82SDavid Howells compat_dev_t lo_device; /* ioctl r/o */ 1197863d5b82SDavid Howells compat_ulong_t lo_inode; /* ioctl r/o */ 1198863d5b82SDavid Howells compat_dev_t lo_rdevice; /* ioctl r/o */ 1199863d5b82SDavid Howells compat_int_t lo_offset; 1200863d5b82SDavid Howells compat_int_t lo_encrypt_type; 1201863d5b82SDavid Howells compat_int_t lo_encrypt_key_size; /* ioctl w/o */ 1202863d5b82SDavid Howells compat_int_t lo_flags; /* ioctl r/o */ 1203863d5b82SDavid Howells char lo_name[LO_NAME_SIZE]; 1204863d5b82SDavid Howells unsigned char lo_encrypt_key[LO_KEY_SIZE]; /* ioctl w/o */ 1205863d5b82SDavid Howells compat_ulong_t lo_init[2]; 1206863d5b82SDavid Howells char reserved[4]; 1207863d5b82SDavid Howells }; 1208863d5b82SDavid Howells 1209863d5b82SDavid Howells /* 1210863d5b82SDavid Howells * Transfer 32-bit compatibility structure in userspace to 64-bit loop info 1211863d5b82SDavid Howells * - noinlined to reduce stack space usage in main part of driver 1212863d5b82SDavid Howells */ 1213863d5b82SDavid Howells static noinline int 1214ba674cfcSAl Viro loop_info64_from_compat(const struct compat_loop_info __user *arg, 1215863d5b82SDavid Howells struct loop_info64 *info64) 1216863d5b82SDavid Howells { 1217863d5b82SDavid Howells struct compat_loop_info info; 1218863d5b82SDavid Howells 1219863d5b82SDavid Howells if (copy_from_user(&info, arg, sizeof(info))) 1220863d5b82SDavid Howells return -EFAULT; 1221863d5b82SDavid Howells 1222863d5b82SDavid Howells memset(info64, 0, sizeof(*info64)); 1223863d5b82SDavid Howells info64->lo_number = info.lo_number; 1224863d5b82SDavid Howells info64->lo_device = info.lo_device; 1225863d5b82SDavid Howells info64->lo_inode = info.lo_inode; 1226863d5b82SDavid Howells info64->lo_rdevice = info.lo_rdevice; 1227863d5b82SDavid Howells info64->lo_offset = info.lo_offset; 1228863d5b82SDavid Howells info64->lo_sizelimit = 0; 1229863d5b82SDavid Howells info64->lo_encrypt_type = info.lo_encrypt_type; 1230863d5b82SDavid Howells info64->lo_encrypt_key_size = info.lo_encrypt_key_size; 1231863d5b82SDavid Howells info64->lo_flags = info.lo_flags; 1232863d5b82SDavid Howells info64->lo_init[0] = info.lo_init[0]; 1233863d5b82SDavid Howells info64->lo_init[1] = info.lo_init[1]; 1234863d5b82SDavid Howells if (info.lo_encrypt_type == LO_CRYPT_CRYPTOAPI) 1235863d5b82SDavid Howells memcpy(info64->lo_crypt_name, info.lo_name, LO_NAME_SIZE); 1236863d5b82SDavid Howells else 1237863d5b82SDavid Howells memcpy(info64->lo_file_name, info.lo_name, LO_NAME_SIZE); 1238863d5b82SDavid Howells memcpy(info64->lo_encrypt_key, info.lo_encrypt_key, LO_KEY_SIZE); 1239863d5b82SDavid Howells return 0; 1240863d5b82SDavid Howells } 1241863d5b82SDavid Howells 1242863d5b82SDavid Howells /* 1243863d5b82SDavid Howells * Transfer 64-bit loop info to 32-bit compatibility structure in userspace 1244863d5b82SDavid Howells * - noinlined to reduce stack space usage in main part of driver 1245863d5b82SDavid Howells */ 1246863d5b82SDavid Howells static noinline int 1247863d5b82SDavid Howells loop_info64_to_compat(const struct loop_info64 *info64, 1248863d5b82SDavid Howells struct compat_loop_info __user *arg) 1249863d5b82SDavid Howells { 1250863d5b82SDavid Howells struct compat_loop_info info; 1251863d5b82SDavid Howells 1252863d5b82SDavid Howells memset(&info, 0, sizeof(info)); 1253863d5b82SDavid Howells info.lo_number = info64->lo_number; 1254863d5b82SDavid Howells info.lo_device = info64->lo_device; 1255863d5b82SDavid Howells info.lo_inode = info64->lo_inode; 1256863d5b82SDavid Howells info.lo_rdevice = info64->lo_rdevice; 1257863d5b82SDavid Howells info.lo_offset = info64->lo_offset; 1258863d5b82SDavid Howells info.lo_encrypt_type = info64->lo_encrypt_type; 1259863d5b82SDavid Howells info.lo_encrypt_key_size = info64->lo_encrypt_key_size; 1260863d5b82SDavid Howells info.lo_flags = info64->lo_flags; 1261863d5b82SDavid Howells info.lo_init[0] = info64->lo_init[0]; 1262863d5b82SDavid Howells info.lo_init[1] = info64->lo_init[1]; 1263863d5b82SDavid Howells if (info.lo_encrypt_type == LO_CRYPT_CRYPTOAPI) 1264863d5b82SDavid Howells memcpy(info.lo_name, info64->lo_crypt_name, LO_NAME_SIZE); 1265863d5b82SDavid Howells else 1266863d5b82SDavid Howells memcpy(info.lo_name, info64->lo_file_name, LO_NAME_SIZE); 1267863d5b82SDavid Howells memcpy(info.lo_encrypt_key, info64->lo_encrypt_key, LO_KEY_SIZE); 1268863d5b82SDavid Howells 1269863d5b82SDavid Howells /* error in case values were truncated */ 1270863d5b82SDavid Howells if (info.lo_device != info64->lo_device || 1271863d5b82SDavid Howells info.lo_rdevice != info64->lo_rdevice || 1272863d5b82SDavid Howells info.lo_inode != info64->lo_inode || 1273863d5b82SDavid Howells info.lo_offset != info64->lo_offset || 1274863d5b82SDavid Howells info.lo_init[0] != info64->lo_init[0] || 1275863d5b82SDavid Howells info.lo_init[1] != info64->lo_init[1]) 1276863d5b82SDavid Howells return -EOVERFLOW; 1277863d5b82SDavid Howells 1278863d5b82SDavid Howells if (copy_to_user(arg, &info, sizeof(info))) 1279863d5b82SDavid Howells return -EFAULT; 1280863d5b82SDavid Howells return 0; 1281863d5b82SDavid Howells } 1282863d5b82SDavid Howells 1283863d5b82SDavid Howells static int 1284863d5b82SDavid Howells loop_set_status_compat(struct loop_device *lo, 1285863d5b82SDavid Howells const struct compat_loop_info __user *arg) 1286863d5b82SDavid Howells { 1287863d5b82SDavid Howells struct loop_info64 info64; 1288863d5b82SDavid Howells int ret; 1289863d5b82SDavid Howells 1290863d5b82SDavid Howells ret = loop_info64_from_compat(arg, &info64); 1291863d5b82SDavid Howells if (ret < 0) 1292863d5b82SDavid Howells return ret; 1293863d5b82SDavid Howells return loop_set_status(lo, &info64); 1294863d5b82SDavid Howells } 1295863d5b82SDavid Howells 1296863d5b82SDavid Howells static int 1297863d5b82SDavid Howells loop_get_status_compat(struct loop_device *lo, 1298863d5b82SDavid Howells struct compat_loop_info __user *arg) 1299863d5b82SDavid Howells { 1300863d5b82SDavid Howells struct loop_info64 info64; 1301863d5b82SDavid Howells int err = 0; 1302863d5b82SDavid Howells 1303863d5b82SDavid Howells if (!arg) 1304863d5b82SDavid Howells err = -EINVAL; 1305863d5b82SDavid Howells if (!err) 1306863d5b82SDavid Howells err = loop_get_status(lo, &info64); 1307863d5b82SDavid Howells if (!err) 1308863d5b82SDavid Howells err = loop_info64_to_compat(&info64, arg); 1309863d5b82SDavid Howells return err; 1310863d5b82SDavid Howells } 1311863d5b82SDavid Howells 1312bb214884SAl Viro static int lo_compat_ioctl(struct block_device *bdev, fmode_t mode, 1313bb214884SAl Viro unsigned int cmd, unsigned long arg) 1314863d5b82SDavid Howells { 1315bb214884SAl Viro struct loop_device *lo = bdev->bd_disk->private_data; 1316863d5b82SDavid Howells int err; 1317863d5b82SDavid Howells 1318863d5b82SDavid Howells switch(cmd) { 1319863d5b82SDavid Howells case LOOP_SET_STATUS: 1320863d5b82SDavid Howells mutex_lock(&lo->lo_ctl_mutex); 1321863d5b82SDavid Howells err = loop_set_status_compat( 1322863d5b82SDavid Howells lo, (const struct compat_loop_info __user *) arg); 1323863d5b82SDavid Howells mutex_unlock(&lo->lo_ctl_mutex); 1324863d5b82SDavid Howells break; 1325863d5b82SDavid Howells case LOOP_GET_STATUS: 1326863d5b82SDavid Howells mutex_lock(&lo->lo_ctl_mutex); 1327863d5b82SDavid Howells err = loop_get_status_compat( 1328863d5b82SDavid Howells lo, (struct compat_loop_info __user *) arg); 1329863d5b82SDavid Howells mutex_unlock(&lo->lo_ctl_mutex); 1330863d5b82SDavid Howells break; 133153d66608SJ. R. Okajima case LOOP_SET_CAPACITY: 1332863d5b82SDavid Howells case LOOP_CLR_FD: 1333863d5b82SDavid Howells case LOOP_GET_STATUS64: 1334863d5b82SDavid Howells case LOOP_SET_STATUS64: 1335863d5b82SDavid Howells arg = (unsigned long) compat_ptr(arg); 1336863d5b82SDavid Howells case LOOP_SET_FD: 1337863d5b82SDavid Howells case LOOP_CHANGE_FD: 1338bb214884SAl Viro err = lo_ioctl(bdev, mode, cmd, arg); 1339863d5b82SDavid Howells break; 1340863d5b82SDavid Howells default: 1341863d5b82SDavid Howells err = -ENOIOCTLCMD; 1342863d5b82SDavid Howells break; 1343863d5b82SDavid Howells } 1344863d5b82SDavid Howells return err; 1345863d5b82SDavid Howells } 1346863d5b82SDavid Howells #endif 1347863d5b82SDavid Howells 1348bb214884SAl Viro static int lo_open(struct block_device *bdev, fmode_t mode) 13491da177e4SLinus Torvalds { 1350770fe30aSKay Sievers struct loop_device *lo; 1351770fe30aSKay Sievers int err = 0; 1352770fe30aSKay Sievers 1353770fe30aSKay Sievers mutex_lock(&loop_index_mutex); 1354770fe30aSKay Sievers lo = bdev->bd_disk->private_data; 1355770fe30aSKay Sievers if (!lo) { 1356770fe30aSKay Sievers err = -ENXIO; 1357770fe30aSKay Sievers goto out; 1358770fe30aSKay Sievers } 13591da177e4SLinus Torvalds 1360f8933667SMing Lei atomic_inc(&lo->lo_refcnt); 1361770fe30aSKay Sievers out: 1362770fe30aSKay Sievers mutex_unlock(&loop_index_mutex); 1363770fe30aSKay Sievers return err; 13641da177e4SLinus Torvalds } 13651da177e4SLinus Torvalds 1366db2a144bSAl Viro static void lo_release(struct gendisk *disk, fmode_t mode) 13671da177e4SLinus Torvalds { 1368bb214884SAl Viro struct loop_device *lo = disk->private_data; 1369ffcd7dcaSAlexander Beregalov int err; 13701da177e4SLinus Torvalds 1371f8933667SMing Lei if (atomic_dec_return(&lo->lo_refcnt)) 1372f8933667SMing Lei return; 1373f8933667SMing Lei 1374f85221ddSIngo Molnar mutex_lock(&lo->lo_ctl_mutex); 137514f27939SMilan Broz if (lo->lo_flags & LO_FLAGS_AUTOCLEAR) { 137614f27939SMilan Broz /* 137714f27939SMilan Broz * In autoclear mode, stop the loop thread 137814f27939SMilan Broz * and remove configuration after last close. 137914f27939SMilan Broz */ 13804c823cc3SAyan George err = loop_clr_fd(lo); 1381ffcd7dcaSAlexander Beregalov if (!err) 1382db2a144bSAl Viro return; 138314f27939SMilan Broz } else { 138414f27939SMilan Broz /* 138514f27939SMilan Broz * Otherwise keep thread (if running) and config, 138614f27939SMilan Broz * but flush possible ongoing bios in thread. 138714f27939SMilan Broz */ 138814f27939SMilan Broz loop_flush(lo); 138914f27939SMilan Broz } 139096c58655SDavid Woodhouse 1391f85221ddSIngo Molnar mutex_unlock(&lo->lo_ctl_mutex); 13921da177e4SLinus Torvalds } 13931da177e4SLinus Torvalds 139483d5cde4SAlexey Dobriyan static const struct block_device_operations lo_fops = { 13951da177e4SLinus Torvalds .owner = THIS_MODULE, 1396bb214884SAl Viro .open = lo_open, 1397bb214884SAl Viro .release = lo_release, 1398bb214884SAl Viro .ioctl = lo_ioctl, 1399863d5b82SDavid Howells #ifdef CONFIG_COMPAT 1400bb214884SAl Viro .compat_ioctl = lo_compat_ioctl, 1401863d5b82SDavid Howells #endif 14021da177e4SLinus Torvalds }; 14031da177e4SLinus Torvalds 14041da177e4SLinus Torvalds /* 14051da177e4SLinus Torvalds * And now the modules code and kernel interface. 14061da177e4SLinus Torvalds */ 140773285082SKen Chen static int max_loop; 1408ac04fee0SNamhyung Kim module_param(max_loop, int, S_IRUGO); 1409a47653fcSKen Chen MODULE_PARM_DESC(max_loop, "Maximum number of loop devices"); 1410ac04fee0SNamhyung Kim module_param(max_part, int, S_IRUGO); 1411476a4813SLaurent Vivier MODULE_PARM_DESC(max_part, "Maximum number of partitions per loop device"); 14121da177e4SLinus Torvalds MODULE_LICENSE("GPL"); 14131da177e4SLinus Torvalds MODULE_ALIAS_BLOCKDEV_MAJOR(LOOP_MAJOR); 14141da177e4SLinus Torvalds 14151da177e4SLinus Torvalds int loop_register_transfer(struct loop_func_table *funcs) 14161da177e4SLinus Torvalds { 14171da177e4SLinus Torvalds unsigned int n = funcs->number; 14181da177e4SLinus Torvalds 14191da177e4SLinus Torvalds if (n >= MAX_LO_CRYPT || xfer_funcs[n]) 14201da177e4SLinus Torvalds return -EINVAL; 14211da177e4SLinus Torvalds xfer_funcs[n] = funcs; 14221da177e4SLinus Torvalds return 0; 14231da177e4SLinus Torvalds } 14241da177e4SLinus Torvalds 142534dd82afSKay Sievers static int unregister_transfer_cb(int id, void *ptr, void *data) 142634dd82afSKay Sievers { 142734dd82afSKay Sievers struct loop_device *lo = ptr; 142834dd82afSKay Sievers struct loop_func_table *xfer = data; 142934dd82afSKay Sievers 143034dd82afSKay Sievers mutex_lock(&lo->lo_ctl_mutex); 143134dd82afSKay Sievers if (lo->lo_encryption == xfer) 143234dd82afSKay Sievers loop_release_xfer(lo); 143334dd82afSKay Sievers mutex_unlock(&lo->lo_ctl_mutex); 143434dd82afSKay Sievers return 0; 143534dd82afSKay Sievers } 143634dd82afSKay Sievers 14371da177e4SLinus Torvalds int loop_unregister_transfer(int number) 14381da177e4SLinus Torvalds { 14391da177e4SLinus Torvalds unsigned int n = number; 14401da177e4SLinus Torvalds struct loop_func_table *xfer; 14411da177e4SLinus Torvalds 14421da177e4SLinus Torvalds if (n == 0 || n >= MAX_LO_CRYPT || (xfer = xfer_funcs[n]) == NULL) 14431da177e4SLinus Torvalds return -EINVAL; 14441da177e4SLinus Torvalds 14451da177e4SLinus Torvalds xfer_funcs[n] = NULL; 144634dd82afSKay Sievers idr_for_each(&loop_index_idr, &unregister_transfer_cb, xfer); 14471da177e4SLinus Torvalds return 0; 14481da177e4SLinus Torvalds } 14491da177e4SLinus Torvalds 14501da177e4SLinus Torvalds EXPORT_SYMBOL(loop_register_transfer); 14511da177e4SLinus Torvalds EXPORT_SYMBOL(loop_unregister_transfer); 14521da177e4SLinus Torvalds 1453b5dd2f60SMing Lei static int loop_queue_rq(struct blk_mq_hw_ctx *hctx, 1454b5dd2f60SMing Lei const struct blk_mq_queue_data *bd) 1455b5dd2f60SMing Lei { 1456b5dd2f60SMing Lei struct loop_cmd *cmd = blk_mq_rq_to_pdu(bd->rq); 1457f4aa4c7bSMing Lei struct loop_device *lo = cmd->rq->q->queuedata; 1458b5dd2f60SMing Lei 1459b5dd2f60SMing Lei blk_mq_start_request(bd->rq); 1460b5dd2f60SMing Lei 1461f4aa4c7bSMing Lei if (lo->lo_state != Lo_bound) 1462f4aa4c7bSMing Lei return -EIO; 1463f4aa4c7bSMing Lei 1464b5dd2f60SMing Lei if (cmd->rq->cmd_flags & REQ_WRITE) { 1465b5dd2f60SMing Lei struct loop_device *lo = cmd->rq->q->queuedata; 1466b5dd2f60SMing Lei bool need_sched = true; 1467b5dd2f60SMing Lei 1468b5dd2f60SMing Lei spin_lock_irq(&lo->lo_lock); 1469b5dd2f60SMing Lei if (lo->write_started) 1470b5dd2f60SMing Lei need_sched = false; 1471b5dd2f60SMing Lei else 1472b5dd2f60SMing Lei lo->write_started = true; 1473b5dd2f60SMing Lei list_add_tail(&cmd->list, &lo->write_cmd_head); 1474b5dd2f60SMing Lei spin_unlock_irq(&lo->lo_lock); 1475b5dd2f60SMing Lei 1476b5dd2f60SMing Lei if (need_sched) 1477f4aa4c7bSMing Lei queue_work(lo->wq, &lo->write_work); 1478b5dd2f60SMing Lei } else { 1479f4aa4c7bSMing Lei queue_work(lo->wq, &cmd->read_work); 1480b5dd2f60SMing Lei } 1481b5dd2f60SMing Lei 1482b5dd2f60SMing Lei return BLK_MQ_RQ_QUEUE_OK; 1483b5dd2f60SMing Lei } 1484b5dd2f60SMing Lei 1485b5dd2f60SMing Lei static void loop_handle_cmd(struct loop_cmd *cmd) 1486b5dd2f60SMing Lei { 1487b5dd2f60SMing Lei const bool write = cmd->rq->cmd_flags & REQ_WRITE; 1488b5dd2f60SMing Lei struct loop_device *lo = cmd->rq->q->queuedata; 1489b5dd2f60SMing Lei int ret = -EIO; 1490b5dd2f60SMing Lei 1491b5dd2f60SMing Lei if (write && (lo->lo_flags & LO_FLAGS_READ_ONLY)) 1492b5dd2f60SMing Lei goto failed; 1493b5dd2f60SMing Lei 149430112013SMing Lei ret = do_req_filebacked(lo, cmd->rq); 1495b5dd2f60SMing Lei 1496b5dd2f60SMing Lei failed: 1497b5dd2f60SMing Lei if (ret) 1498b5dd2f60SMing Lei cmd->rq->errors = -EIO; 1499b5dd2f60SMing Lei blk_mq_complete_request(cmd->rq); 1500b5dd2f60SMing Lei } 1501b5dd2f60SMing Lei 1502b5dd2f60SMing Lei static void loop_queue_write_work(struct work_struct *work) 1503b5dd2f60SMing Lei { 1504b5dd2f60SMing Lei struct loop_device *lo = 1505b5dd2f60SMing Lei container_of(work, struct loop_device, write_work); 1506b5dd2f60SMing Lei LIST_HEAD(cmd_list); 1507b5dd2f60SMing Lei 1508b5dd2f60SMing Lei spin_lock_irq(&lo->lo_lock); 1509b5dd2f60SMing Lei repeat: 1510b5dd2f60SMing Lei list_splice_init(&lo->write_cmd_head, &cmd_list); 1511b5dd2f60SMing Lei spin_unlock_irq(&lo->lo_lock); 1512b5dd2f60SMing Lei 1513b5dd2f60SMing Lei while (!list_empty(&cmd_list)) { 1514b5dd2f60SMing Lei struct loop_cmd *cmd = list_first_entry(&cmd_list, 1515b5dd2f60SMing Lei struct loop_cmd, list); 1516b5dd2f60SMing Lei list_del_init(&cmd->list); 1517b5dd2f60SMing Lei loop_handle_cmd(cmd); 1518b5dd2f60SMing Lei } 1519b5dd2f60SMing Lei 1520b5dd2f60SMing Lei spin_lock_irq(&lo->lo_lock); 1521b5dd2f60SMing Lei if (!list_empty(&lo->write_cmd_head)) 1522b5dd2f60SMing Lei goto repeat; 1523b5dd2f60SMing Lei lo->write_started = false; 1524b5dd2f60SMing Lei spin_unlock_irq(&lo->lo_lock); 1525b5dd2f60SMing Lei } 1526b5dd2f60SMing Lei 1527b5dd2f60SMing Lei static void loop_queue_read_work(struct work_struct *work) 1528b5dd2f60SMing Lei { 1529b5dd2f60SMing Lei struct loop_cmd *cmd = 1530b5dd2f60SMing Lei container_of(work, struct loop_cmd, read_work); 1531b5dd2f60SMing Lei 1532b5dd2f60SMing Lei loop_handle_cmd(cmd); 1533b5dd2f60SMing Lei } 1534b5dd2f60SMing Lei 1535b5dd2f60SMing Lei static int loop_init_request(void *data, struct request *rq, 1536b5dd2f60SMing Lei unsigned int hctx_idx, unsigned int request_idx, 1537b5dd2f60SMing Lei unsigned int numa_node) 1538b5dd2f60SMing Lei { 1539b5dd2f60SMing Lei struct loop_cmd *cmd = blk_mq_rq_to_pdu(rq); 1540b5dd2f60SMing Lei 1541b5dd2f60SMing Lei cmd->rq = rq; 1542b5dd2f60SMing Lei INIT_WORK(&cmd->read_work, loop_queue_read_work); 1543b5dd2f60SMing Lei 1544b5dd2f60SMing Lei return 0; 1545b5dd2f60SMing Lei } 1546b5dd2f60SMing Lei 1547b5dd2f60SMing Lei static struct blk_mq_ops loop_mq_ops = { 1548b5dd2f60SMing Lei .queue_rq = loop_queue_rq, 1549b5dd2f60SMing Lei .map_queue = blk_mq_map_queue, 1550b5dd2f60SMing Lei .init_request = loop_init_request, 1551b5dd2f60SMing Lei }; 1552b5dd2f60SMing Lei 155334dd82afSKay Sievers static int loop_add(struct loop_device **l, int i) 15541da177e4SLinus Torvalds { 155573285082SKen Chen struct loop_device *lo; 155673285082SKen Chen struct gendisk *disk; 155734dd82afSKay Sievers int err; 15581da177e4SLinus Torvalds 155934dd82afSKay Sievers err = -ENOMEM; 156068d740d7SSilva Paulo lo = kzalloc(sizeof(*lo), GFP_KERNEL); 156168d740d7SSilva Paulo if (!lo) 156273285082SKen Chen goto out; 156334dd82afSKay Sievers 1564ef7e7c82SMikulas Patocka lo->lo_state = Lo_unbound; 1565ef7e7c82SMikulas Patocka 1566c718aa65STejun Heo /* allocate id, if @id >= 0, we're requesting that specific id */ 156734dd82afSKay Sievers if (i >= 0) { 1568c718aa65STejun Heo err = idr_alloc(&loop_index_idr, lo, i, i + 1, GFP_KERNEL); 1569c718aa65STejun Heo if (err == -ENOSPC) 157034dd82afSKay Sievers err = -EEXIST; 157134dd82afSKay Sievers } else { 1572c718aa65STejun Heo err = idr_alloc(&loop_index_idr, lo, 0, 0, GFP_KERNEL); 157334dd82afSKay Sievers } 157434dd82afSKay Sievers if (err < 0) 157534dd82afSKay Sievers goto out_free_dev; 1576c718aa65STejun Heo i = err; 15771da177e4SLinus Torvalds 1578183cfb57SWei Yongjun err = -ENOMEM; 1579b5dd2f60SMing Lei lo->tag_set.ops = &loop_mq_ops; 1580b5dd2f60SMing Lei lo->tag_set.nr_hw_queues = 1; 1581b5dd2f60SMing Lei lo->tag_set.queue_depth = 128; 1582b5dd2f60SMing Lei lo->tag_set.numa_node = NUMA_NO_NODE; 1583b5dd2f60SMing Lei lo->tag_set.cmd_size = sizeof(struct loop_cmd); 1584b5dd2f60SMing Lei lo->tag_set.flags = BLK_MQ_F_SHOULD_MERGE | BLK_MQ_F_SG_MERGE; 1585b5dd2f60SMing Lei lo->tag_set.driver_data = lo; 1586b5dd2f60SMing Lei 1587b5dd2f60SMing Lei err = blk_mq_alloc_tag_set(&lo->tag_set); 1588b5dd2f60SMing Lei if (err) 15893ec981e3SMikulas Patocka goto out_free_idr; 159073285082SKen Chen 1591b5dd2f60SMing Lei lo->lo_queue = blk_mq_init_queue(&lo->tag_set); 1592b5dd2f60SMing Lei if (IS_ERR_OR_NULL(lo->lo_queue)) { 1593b5dd2f60SMing Lei err = PTR_ERR(lo->lo_queue); 1594b5dd2f60SMing Lei goto out_cleanup_tags; 1595b5dd2f60SMing Lei } 1596ef7e7c82SMikulas Patocka lo->lo_queue->queuedata = lo; 1597ef7e7c82SMikulas Patocka 1598*5b5e20f4SMing Lei /* 1599*5b5e20f4SMing Lei * It doesn't make sense to enable merge because the I/O 1600*5b5e20f4SMing Lei * submitted to backing file is handled page by page. 1601*5b5e20f4SMing Lei */ 1602*5b5e20f4SMing Lei queue_flag_set_unlocked(QUEUE_FLAG_NOMERGES, lo->lo_queue); 1603*5b5e20f4SMing Lei 1604b5dd2f60SMing Lei INIT_LIST_HEAD(&lo->write_cmd_head); 1605b5dd2f60SMing Lei INIT_WORK(&lo->write_work, loop_queue_write_work); 1606b5dd2f60SMing Lei 1607476a4813SLaurent Vivier disk = lo->lo_disk = alloc_disk(1 << part_shift); 160873285082SKen Chen if (!disk) 160973285082SKen Chen goto out_free_queue; 161073285082SKen Chen 1611e03c8dd1SKay Sievers /* 1612e03c8dd1SKay Sievers * Disable partition scanning by default. The in-kernel partition 1613e03c8dd1SKay Sievers * scanning can be requested individually per-device during its 1614e03c8dd1SKay Sievers * setup. Userspace can always add and remove partitions from all 1615e03c8dd1SKay Sievers * devices. The needed partition minors are allocated from the 1616e03c8dd1SKay Sievers * extended minor space, the main loop device numbers will continue 1617e03c8dd1SKay Sievers * to match the loop minors, regardless of the number of partitions 1618e03c8dd1SKay Sievers * used. 1619e03c8dd1SKay Sievers * 1620e03c8dd1SKay Sievers * If max_part is given, partition scanning is globally enabled for 1621e03c8dd1SKay Sievers * all loop devices. The minors for the main loop devices will be 1622e03c8dd1SKay Sievers * multiples of max_part. 1623e03c8dd1SKay Sievers * 1624e03c8dd1SKay Sievers * Note: Global-for-all-devices, set-only-at-init, read-only module 1625e03c8dd1SKay Sievers * parameteters like 'max_loop' and 'max_part' make things needlessly 1626e03c8dd1SKay Sievers * complicated, are too static, inflexible and may surprise 1627e03c8dd1SKay Sievers * userspace tools. Parameters like this in general should be avoided. 1628e03c8dd1SKay Sievers */ 1629e03c8dd1SKay Sievers if (!part_shift) 1630e03c8dd1SKay Sievers disk->flags |= GENHD_FL_NO_PART_SCAN; 1631e03c8dd1SKay Sievers disk->flags |= GENHD_FL_EXT_DEVT; 1632f85221ddSIngo Molnar mutex_init(&lo->lo_ctl_mutex); 1633f8933667SMing Lei atomic_set(&lo->lo_refcnt, 0); 16341da177e4SLinus Torvalds lo->lo_number = i; 16351da177e4SLinus Torvalds spin_lock_init(&lo->lo_lock); 16361da177e4SLinus Torvalds disk->major = LOOP_MAJOR; 1637476a4813SLaurent Vivier disk->first_minor = i << part_shift; 16381da177e4SLinus Torvalds disk->fops = &lo_fops; 16391da177e4SLinus Torvalds disk->private_data = lo; 16401da177e4SLinus Torvalds disk->queue = lo->lo_queue; 164173285082SKen Chen sprintf(disk->disk_name, "loop%d", i); 164234dd82afSKay Sievers add_disk(disk); 164334dd82afSKay Sievers *l = lo; 164434dd82afSKay Sievers return lo->lo_number; 164573285082SKen Chen 164673285082SKen Chen out_free_queue: 164773285082SKen Chen blk_cleanup_queue(lo->lo_queue); 1648b5dd2f60SMing Lei out_cleanup_tags: 1649b5dd2f60SMing Lei blk_mq_free_tag_set(&lo->tag_set); 16503ec981e3SMikulas Patocka out_free_idr: 16513ec981e3SMikulas Patocka idr_remove(&loop_index_idr, i); 165273285082SKen Chen out_free_dev: 165373285082SKen Chen kfree(lo); 165473285082SKen Chen out: 165534dd82afSKay Sievers return err; 16561da177e4SLinus Torvalds } 16571da177e4SLinus Torvalds 165834dd82afSKay Sievers static void loop_remove(struct loop_device *lo) 165973285082SKen Chen { 166073285082SKen Chen blk_cleanup_queue(lo->lo_queue); 16616cd18e71SNeilBrown del_gendisk(lo->lo_disk); 1662b5dd2f60SMing Lei blk_mq_free_tag_set(&lo->tag_set); 166373285082SKen Chen put_disk(lo->lo_disk); 166473285082SKen Chen kfree(lo); 166573285082SKen Chen } 166673285082SKen Chen 1667770fe30aSKay Sievers static int find_free_cb(int id, void *ptr, void *data) 1668770fe30aSKay Sievers { 1669770fe30aSKay Sievers struct loop_device *lo = ptr; 1670770fe30aSKay Sievers struct loop_device **l = data; 1671770fe30aSKay Sievers 1672770fe30aSKay Sievers if (lo->lo_state == Lo_unbound) { 1673770fe30aSKay Sievers *l = lo; 1674770fe30aSKay Sievers return 1; 1675770fe30aSKay Sievers } 1676770fe30aSKay Sievers return 0; 1677770fe30aSKay Sievers } 1678770fe30aSKay Sievers 167934dd82afSKay Sievers static int loop_lookup(struct loop_device **l, int i) 1680a47653fcSKen Chen { 1681a47653fcSKen Chen struct loop_device *lo; 168234dd82afSKay Sievers int ret = -ENODEV; 1683a47653fcSKen Chen 1684770fe30aSKay Sievers if (i < 0) { 1685770fe30aSKay Sievers int err; 1686770fe30aSKay Sievers 1687770fe30aSKay Sievers err = idr_for_each(&loop_index_idr, &find_free_cb, &lo); 1688770fe30aSKay Sievers if (err == 1) { 1689770fe30aSKay Sievers *l = lo; 1690770fe30aSKay Sievers ret = lo->lo_number; 1691770fe30aSKay Sievers } 1692770fe30aSKay Sievers goto out; 1693a47653fcSKen Chen } 1694a47653fcSKen Chen 1695770fe30aSKay Sievers /* lookup and return a specific i */ 169634dd82afSKay Sievers lo = idr_find(&loop_index_idr, i); 1697a47653fcSKen Chen if (lo) { 169834dd82afSKay Sievers *l = lo; 169934dd82afSKay Sievers ret = lo->lo_number; 1700a47653fcSKen Chen } 1701770fe30aSKay Sievers out: 170234dd82afSKay Sievers return ret; 1703a47653fcSKen Chen } 1704a47653fcSKen Chen 170573285082SKen Chen static struct kobject *loop_probe(dev_t dev, int *part, void *data) 170673285082SKen Chen { 1707705962ccSAl Viro struct loop_device *lo; 170807002e99SAl Viro struct kobject *kobj; 170934dd82afSKay Sievers int err; 171073285082SKen Chen 171134dd82afSKay Sievers mutex_lock(&loop_index_mutex); 171234dd82afSKay Sievers err = loop_lookup(&lo, MINOR(dev) >> part_shift); 171334dd82afSKay Sievers if (err < 0) 171434dd82afSKay Sievers err = loop_add(&lo, MINOR(dev) >> part_shift); 171534dd82afSKay Sievers if (err < 0) 1716a207f593SMikulas Patocka kobj = NULL; 171734dd82afSKay Sievers else 171834dd82afSKay Sievers kobj = get_disk(lo->lo_disk); 171934dd82afSKay Sievers mutex_unlock(&loop_index_mutex); 172073285082SKen Chen 172173285082SKen Chen *part = 0; 172207002e99SAl Viro return kobj; 172373285082SKen Chen } 172473285082SKen Chen 1725770fe30aSKay Sievers static long loop_control_ioctl(struct file *file, unsigned int cmd, 1726770fe30aSKay Sievers unsigned long parm) 1727770fe30aSKay Sievers { 1728770fe30aSKay Sievers struct loop_device *lo; 1729770fe30aSKay Sievers int ret = -ENOSYS; 1730770fe30aSKay Sievers 1731770fe30aSKay Sievers mutex_lock(&loop_index_mutex); 1732770fe30aSKay Sievers switch (cmd) { 1733770fe30aSKay Sievers case LOOP_CTL_ADD: 1734770fe30aSKay Sievers ret = loop_lookup(&lo, parm); 1735770fe30aSKay Sievers if (ret >= 0) { 1736770fe30aSKay Sievers ret = -EEXIST; 1737770fe30aSKay Sievers break; 1738770fe30aSKay Sievers } 1739770fe30aSKay Sievers ret = loop_add(&lo, parm); 1740770fe30aSKay Sievers break; 1741770fe30aSKay Sievers case LOOP_CTL_REMOVE: 1742770fe30aSKay Sievers ret = loop_lookup(&lo, parm); 1743770fe30aSKay Sievers if (ret < 0) 1744770fe30aSKay Sievers break; 1745770fe30aSKay Sievers mutex_lock(&lo->lo_ctl_mutex); 1746770fe30aSKay Sievers if (lo->lo_state != Lo_unbound) { 1747770fe30aSKay Sievers ret = -EBUSY; 1748770fe30aSKay Sievers mutex_unlock(&lo->lo_ctl_mutex); 1749770fe30aSKay Sievers break; 1750770fe30aSKay Sievers } 1751f8933667SMing Lei if (atomic_read(&lo->lo_refcnt) > 0) { 1752770fe30aSKay Sievers ret = -EBUSY; 1753770fe30aSKay Sievers mutex_unlock(&lo->lo_ctl_mutex); 1754770fe30aSKay Sievers break; 1755770fe30aSKay Sievers } 1756770fe30aSKay Sievers lo->lo_disk->private_data = NULL; 1757770fe30aSKay Sievers mutex_unlock(&lo->lo_ctl_mutex); 1758770fe30aSKay Sievers idr_remove(&loop_index_idr, lo->lo_number); 1759770fe30aSKay Sievers loop_remove(lo); 1760770fe30aSKay Sievers break; 1761770fe30aSKay Sievers case LOOP_CTL_GET_FREE: 1762770fe30aSKay Sievers ret = loop_lookup(&lo, -1); 1763770fe30aSKay Sievers if (ret >= 0) 1764770fe30aSKay Sievers break; 1765770fe30aSKay Sievers ret = loop_add(&lo, -1); 1766770fe30aSKay Sievers } 1767770fe30aSKay Sievers mutex_unlock(&loop_index_mutex); 1768770fe30aSKay Sievers 1769770fe30aSKay Sievers return ret; 1770770fe30aSKay Sievers } 1771770fe30aSKay Sievers 1772770fe30aSKay Sievers static const struct file_operations loop_ctl_fops = { 1773770fe30aSKay Sievers .open = nonseekable_open, 1774770fe30aSKay Sievers .unlocked_ioctl = loop_control_ioctl, 1775770fe30aSKay Sievers .compat_ioctl = loop_control_ioctl, 1776770fe30aSKay Sievers .owner = THIS_MODULE, 1777770fe30aSKay Sievers .llseek = noop_llseek, 1778770fe30aSKay Sievers }; 1779770fe30aSKay Sievers 1780770fe30aSKay Sievers static struct miscdevice loop_misc = { 1781770fe30aSKay Sievers .minor = LOOP_CTRL_MINOR, 1782770fe30aSKay Sievers .name = "loop-control", 1783770fe30aSKay Sievers .fops = &loop_ctl_fops, 1784770fe30aSKay Sievers }; 1785770fe30aSKay Sievers 1786770fe30aSKay Sievers MODULE_ALIAS_MISCDEV(LOOP_CTRL_MINOR); 1787770fe30aSKay Sievers MODULE_ALIAS("devname:loop-control"); 1788770fe30aSKay Sievers 178973285082SKen Chen static int __init loop_init(void) 179073285082SKen Chen { 1791a47653fcSKen Chen int i, nr; 1792a47653fcSKen Chen unsigned long range; 179334dd82afSKay Sievers struct loop_device *lo; 1794770fe30aSKay Sievers int err; 1795a47653fcSKen Chen 1796770fe30aSKay Sievers err = misc_register(&loop_misc); 1797770fe30aSKay Sievers if (err < 0) 1798770fe30aSKay Sievers return err; 1799476a4813SLaurent Vivier 1800476a4813SLaurent Vivier part_shift = 0; 1801ac04fee0SNamhyung Kim if (max_part > 0) { 1802476a4813SLaurent Vivier part_shift = fls(max_part); 1803476a4813SLaurent Vivier 1804ac04fee0SNamhyung Kim /* 1805ac04fee0SNamhyung Kim * Adjust max_part according to part_shift as it is exported 1806ac04fee0SNamhyung Kim * to user space so that user can decide correct minor number 1807ac04fee0SNamhyung Kim * if [s]he want to create more devices. 1808ac04fee0SNamhyung Kim * 1809ac04fee0SNamhyung Kim * Note that -1 is required because partition 0 is reserved 1810ac04fee0SNamhyung Kim * for the whole disk. 1811ac04fee0SNamhyung Kim */ 1812ac04fee0SNamhyung Kim max_part = (1UL << part_shift) - 1; 1813ac04fee0SNamhyung Kim } 1814ac04fee0SNamhyung Kim 1815b1a66504SGuo Chao if ((1UL << part_shift) > DISK_MAX_PARTS) { 1816b1a66504SGuo Chao err = -EINVAL; 1817b1a66504SGuo Chao goto misc_out; 1818b1a66504SGuo Chao } 181978f4bb36SNamhyung Kim 1820b1a66504SGuo Chao if (max_loop > 1UL << (MINORBITS - part_shift)) { 1821b1a66504SGuo Chao err = -EINVAL; 1822b1a66504SGuo Chao goto misc_out; 1823b1a66504SGuo Chao } 182473285082SKen Chen 1825d134b00bSKay Sievers /* 1826d134b00bSKay Sievers * If max_loop is specified, create that many devices upfront. 1827d134b00bSKay Sievers * This also becomes a hard limit. If max_loop is not specified, 1828d134b00bSKay Sievers * create CONFIG_BLK_DEV_LOOP_MIN_COUNT loop devices at module 1829d134b00bSKay Sievers * init time. Loop devices can be requested on-demand with the 1830d134b00bSKay Sievers * /dev/loop-control interface, or be instantiated by accessing 1831d134b00bSKay Sievers * a 'dead' device node. 1832d134b00bSKay Sievers */ 183373285082SKen Chen if (max_loop) { 1834a47653fcSKen Chen nr = max_loop; 1835a1c15c59SNamhyung Kim range = max_loop << part_shift; 1836a47653fcSKen Chen } else { 1837d134b00bSKay Sievers nr = CONFIG_BLK_DEV_LOOP_MIN_COUNT; 1838a1c15c59SNamhyung Kim range = 1UL << MINORBITS; 183973285082SKen Chen } 1840a47653fcSKen Chen 1841b1a66504SGuo Chao if (register_blkdev(LOOP_MAJOR, "loop")) { 1842b1a66504SGuo Chao err = -EIO; 1843b1a66504SGuo Chao goto misc_out; 1844b1a66504SGuo Chao } 1845a47653fcSKen Chen 1846a47653fcSKen Chen blk_register_region(MKDEV(LOOP_MAJOR, 0), range, 1847a47653fcSKen Chen THIS_MODULE, loop_probe, NULL, NULL); 1848a47653fcSKen Chen 1849d134b00bSKay Sievers /* pre-create number of devices given by config or max_loop */ 185034dd82afSKay Sievers mutex_lock(&loop_index_mutex); 185134dd82afSKay Sievers for (i = 0; i < nr; i++) 185234dd82afSKay Sievers loop_add(&lo, i); 185334dd82afSKay Sievers mutex_unlock(&loop_index_mutex); 185434dd82afSKay Sievers 185573285082SKen Chen printk(KERN_INFO "loop: module loaded\n"); 18561da177e4SLinus Torvalds return 0; 1857b1a66504SGuo Chao 1858b1a66504SGuo Chao misc_out: 1859b1a66504SGuo Chao misc_deregister(&loop_misc); 1860b1a66504SGuo Chao return err; 186134dd82afSKay Sievers } 1862a47653fcSKen Chen 186334dd82afSKay Sievers static int loop_exit_cb(int id, void *ptr, void *data) 186434dd82afSKay Sievers { 186534dd82afSKay Sievers struct loop_device *lo = ptr; 1866a47653fcSKen Chen 186734dd82afSKay Sievers loop_remove(lo); 186834dd82afSKay Sievers return 0; 18691da177e4SLinus Torvalds } 18701da177e4SLinus Torvalds 187173285082SKen Chen static void __exit loop_exit(void) 18721da177e4SLinus Torvalds { 1873a47653fcSKen Chen unsigned long range; 18741da177e4SLinus Torvalds 1875a1c15c59SNamhyung Kim range = max_loop ? max_loop << part_shift : 1UL << MINORBITS; 1876a47653fcSKen Chen 187734dd82afSKay Sievers idr_for_each(&loop_index_idr, &loop_exit_cb, NULL); 187834dd82afSKay Sievers idr_destroy(&loop_index_idr); 187973285082SKen Chen 1880a47653fcSKen Chen blk_unregister_region(MKDEV(LOOP_MAJOR, 0), range); 188100d59405SAkinobu Mita unregister_blkdev(LOOP_MAJOR, "loop"); 1882770fe30aSKay Sievers 1883770fe30aSKay Sievers misc_deregister(&loop_misc); 18841da177e4SLinus Torvalds } 18851da177e4SLinus Torvalds 18861da177e4SLinus Torvalds module_init(loop_init); 18871da177e4SLinus Torvalds module_exit(loop_exit); 18881da177e4SLinus Torvalds 18891da177e4SLinus Torvalds #ifndef MODULE 18901da177e4SLinus Torvalds static int __init max_loop_setup(char *str) 18911da177e4SLinus Torvalds { 18921da177e4SLinus Torvalds max_loop = simple_strtol(str, NULL, 0); 18931da177e4SLinus Torvalds return 1; 18941da177e4SLinus Torvalds } 18951da177e4SLinus Torvalds 18961da177e4SLinus Torvalds __setup("max_loop=", max_loop_setup); 18971da177e4SLinus Torvalds #endif 1898