11da177e4SLinus Torvalds /* 21da177e4SLinus Torvalds * linux/drivers/block/loop.c 31da177e4SLinus Torvalds * 41da177e4SLinus Torvalds * Written by Theodore Ts'o, 3/29/93 51da177e4SLinus Torvalds * 61da177e4SLinus Torvalds * Copyright 1993 by Theodore Ts'o. Redistribution of this file is 71da177e4SLinus Torvalds * permitted under the GNU General Public License. 81da177e4SLinus Torvalds * 91da177e4SLinus Torvalds * DES encryption plus some minor changes by Werner Almesberger, 30-MAY-1993 101da177e4SLinus Torvalds * more DES encryption plus IDEA encryption by Nicholas J. Leon, June 20, 1996 111da177e4SLinus Torvalds * 121da177e4SLinus Torvalds * Modularized and updated for 1.1.16 kernel - Mitch Dsouza 28th May 1994 131da177e4SLinus Torvalds * Adapted for 1.3.59 kernel - Andries Brouwer, 1 Feb 1996 141da177e4SLinus Torvalds * 151da177e4SLinus Torvalds * Fixed do_loop_request() re-entrancy - Vincent.Renardias@waw.com Mar 20, 1997 161da177e4SLinus Torvalds * 171da177e4SLinus Torvalds * Added devfs support - Richard Gooch <rgooch@atnf.csiro.au> 16-Jan-1998 181da177e4SLinus Torvalds * 191da177e4SLinus Torvalds * Handle sparse backing files correctly - Kenn Humborg, Jun 28, 1998 201da177e4SLinus Torvalds * 211da177e4SLinus Torvalds * Loadable modules and other fixes by AK, 1998 221da177e4SLinus Torvalds * 231da177e4SLinus Torvalds * Make real block number available to downstream transfer functions, enables 241da177e4SLinus Torvalds * CBC (and relatives) mode encryption requiring unique IVs per data block. 251da177e4SLinus Torvalds * Reed H. Petty, rhp@draper.net 261da177e4SLinus Torvalds * 271da177e4SLinus Torvalds * Maximum number of loop devices now dynamic via max_loop module parameter. 281da177e4SLinus Torvalds * Russell Kroll <rkroll@exploits.org> 19990701 291da177e4SLinus Torvalds * 301da177e4SLinus Torvalds * Maximum number of loop devices when compiled-in now selectable by passing 311da177e4SLinus Torvalds * max_loop=<1-255> to the kernel on boot. 3296de0e25SJan Engelhardt * Erik I. Bolsø, <eriki@himolde.no>, Oct 31, 1999 331da177e4SLinus Torvalds * 341da177e4SLinus Torvalds * Completely rewrite request handling to be make_request_fn style and 351da177e4SLinus Torvalds * non blocking, pushing work to a helper thread. Lots of fixes from 361da177e4SLinus Torvalds * Al Viro too. 371da177e4SLinus Torvalds * Jens Axboe <axboe@suse.de>, Nov 2000 381da177e4SLinus Torvalds * 391da177e4SLinus Torvalds * Support up to 256 loop devices 401da177e4SLinus Torvalds * Heinz Mauelshagen <mge@sistina.com>, Feb 2002 411da177e4SLinus Torvalds * 421da177e4SLinus Torvalds * Support for falling back on the write file operation when the address space 434e02ed4bSNick Piggin * operations write_begin is not available on the backing filesystem. 441da177e4SLinus Torvalds * Anton Altaparmakov, 16 Feb 2005 451da177e4SLinus Torvalds * 461da177e4SLinus Torvalds * Still To Fix: 471da177e4SLinus Torvalds * - Advisory locking is ignored here. 481da177e4SLinus Torvalds * - Should use an own CAP_* category instead of CAP_SYS_ADMIN 491da177e4SLinus Torvalds * 501da177e4SLinus Torvalds */ 511da177e4SLinus Torvalds 521da177e4SLinus Torvalds #include <linux/module.h> 531da177e4SLinus Torvalds #include <linux/moduleparam.h> 541da177e4SLinus Torvalds #include <linux/sched.h> 551da177e4SLinus Torvalds #include <linux/fs.h> 564ee60ec1SMatthew Wilcox (Oracle) #include <linux/pagemap.h> 571da177e4SLinus Torvalds #include <linux/file.h> 581da177e4SLinus Torvalds #include <linux/stat.h> 591da177e4SLinus Torvalds #include <linux/errno.h> 601da177e4SLinus Torvalds #include <linux/major.h> 611da177e4SLinus Torvalds #include <linux/wait.h> 621da177e4SLinus Torvalds #include <linux/blkdev.h> 631da177e4SLinus Torvalds #include <linux/blkpg.h> 641da177e4SLinus Torvalds #include <linux/init.h> 651da177e4SLinus Torvalds #include <linux/swap.h> 661da177e4SLinus Torvalds #include <linux/slab.h> 67863d5b82SDavid Howells #include <linux/compat.h> 681da177e4SLinus Torvalds #include <linux/suspend.h> 6983144186SRafael J. Wysocki #include <linux/freezer.h> 702a48fc0aSArnd Bergmann #include <linux/mutex.h> 711da177e4SLinus Torvalds #include <linux/writeback.h> 721da177e4SLinus Torvalds #include <linux/completion.h> 731da177e4SLinus Torvalds #include <linux/highmem.h> 74d6b29d7cSJens Axboe #include <linux/splice.h> 75ee862730SMilan Broz #include <linux/sysfs.h> 76770fe30aSKay Sievers #include <linux/miscdevice.h> 77dfaa2ef6SLukas Czerner #include <linux/falloc.h> 78283e7e5dSAl Viro #include <linux/uio.h> 79d9a08a9eSAdam Manzanares #include <linux/ioprio.h> 80db6638d7SDennis Zhou #include <linux/blk-cgroup.h> 81c74d40e8SDan Schatzberg #include <linux/sched/mm.h> 82d9a08a9eSAdam Manzanares 8383a87611SAl Viro #include "loop.h" 841da177e4SLinus Torvalds 857c0f6ba6SLinus Torvalds #include <linux/uaccess.h> 861da177e4SLinus Torvalds 8787579e9bSDan Schatzberg #define LOOP_IDLE_WORKER_TIMEOUT (60 * HZ) 88ef44c508SChaitanya Kulkarni #define LOOP_DEFAULT_HW_Q_DEPTH (128) 8987579e9bSDan Schatzberg 9034dd82afSKay Sievers static DEFINE_IDR(loop_index_idr); 91310ca162STetsuo Handa static DEFINE_MUTEX(loop_ctl_mutex); 923ce6e1f6STetsuo Handa static DEFINE_MUTEX(loop_validate_mutex); 933ce6e1f6STetsuo Handa 943ce6e1f6STetsuo Handa /** 953ce6e1f6STetsuo Handa * loop_global_lock_killable() - take locks for safe loop_validate_file() test 963ce6e1f6STetsuo Handa * 973ce6e1f6STetsuo Handa * @lo: struct loop_device 983ce6e1f6STetsuo Handa * @global: true if @lo is about to bind another "struct loop_device", false otherwise 993ce6e1f6STetsuo Handa * 1003ce6e1f6STetsuo Handa * Returns 0 on success, -EINTR otherwise. 1013ce6e1f6STetsuo Handa * 1023ce6e1f6STetsuo Handa * Since loop_validate_file() traverses on other "struct loop_device" if 1033ce6e1f6STetsuo Handa * is_loop_device() is true, we need a global lock for serializing concurrent 1043ce6e1f6STetsuo Handa * loop_configure()/loop_change_fd()/__loop_clr_fd() calls. 1053ce6e1f6STetsuo Handa */ 1063ce6e1f6STetsuo Handa static int loop_global_lock_killable(struct loop_device *lo, bool global) 1073ce6e1f6STetsuo Handa { 1083ce6e1f6STetsuo Handa int err; 1093ce6e1f6STetsuo Handa 1103ce6e1f6STetsuo Handa if (global) { 1113ce6e1f6STetsuo Handa err = mutex_lock_killable(&loop_validate_mutex); 1123ce6e1f6STetsuo Handa if (err) 1133ce6e1f6STetsuo Handa return err; 1143ce6e1f6STetsuo Handa } 1153ce6e1f6STetsuo Handa err = mutex_lock_killable(&lo->lo_mutex); 1163ce6e1f6STetsuo Handa if (err && global) 1173ce6e1f6STetsuo Handa mutex_unlock(&loop_validate_mutex); 1183ce6e1f6STetsuo Handa return err; 1193ce6e1f6STetsuo Handa } 1203ce6e1f6STetsuo Handa 1213ce6e1f6STetsuo Handa /** 1223ce6e1f6STetsuo Handa * loop_global_unlock() - release locks taken by loop_global_lock_killable() 1233ce6e1f6STetsuo Handa * 1243ce6e1f6STetsuo Handa * @lo: struct loop_device 1253ce6e1f6STetsuo Handa * @global: true if @lo was about to bind another "struct loop_device", false otherwise 1263ce6e1f6STetsuo Handa */ 1273ce6e1f6STetsuo Handa static void loop_global_unlock(struct loop_device *lo, bool global) 1283ce6e1f6STetsuo Handa { 1293ce6e1f6STetsuo Handa mutex_unlock(&lo->lo_mutex); 1303ce6e1f6STetsuo Handa if (global) 1313ce6e1f6STetsuo Handa mutex_unlock(&loop_validate_mutex); 1323ce6e1f6STetsuo Handa } 1331da177e4SLinus Torvalds 134476a4813SLaurent Vivier static int max_part; 135476a4813SLaurent Vivier static int part_shift; 136476a4813SLaurent Vivier 1377035b5dfSDmitry Monakhov static loff_t get_size(loff_t offset, loff_t sizelimit, struct file *file) 1381da177e4SLinus Torvalds { 139b7a1da69SGuo Chao loff_t loopsize; 1401da177e4SLinus Torvalds 1411da177e4SLinus Torvalds /* Compute loopsize in bytes */ 142b7a1da69SGuo Chao loopsize = i_size_read(file->f_mapping->host); 143b7a1da69SGuo Chao if (offset > 0) 144b7a1da69SGuo Chao loopsize -= offset; 145b7a1da69SGuo Chao /* offset is beyond i_size, weird but possible */ 1467035b5dfSDmitry Monakhov if (loopsize < 0) 1477035b5dfSDmitry Monakhov return 0; 1481da177e4SLinus Torvalds 1497035b5dfSDmitry Monakhov if (sizelimit > 0 && sizelimit < loopsize) 1507035b5dfSDmitry Monakhov loopsize = sizelimit; 1511da177e4SLinus Torvalds /* 1521da177e4SLinus Torvalds * Unfortunately, if we want to do I/O on the device, 1531da177e4SLinus Torvalds * the number of 512-byte sectors has to fit into a sector_t. 1541da177e4SLinus Torvalds */ 1551da177e4SLinus Torvalds return loopsize >> 9; 1561da177e4SLinus Torvalds } 1571da177e4SLinus Torvalds 1587035b5dfSDmitry Monakhov static loff_t get_loop_size(struct loop_device *lo, struct file *file) 1591da177e4SLinus Torvalds { 1607035b5dfSDmitry Monakhov return get_size(lo->lo_offset, lo->lo_sizelimit, file); 1617035b5dfSDmitry Monakhov } 1627035b5dfSDmitry Monakhov 1632e5ab5f3SMing Lei static void __loop_update_dio(struct loop_device *lo, bool dio) 1642e5ab5f3SMing Lei { 1652e5ab5f3SMing Lei struct file *file = lo->lo_backing_file; 1662e5ab5f3SMing Lei struct address_space *mapping = file->f_mapping; 1672e5ab5f3SMing Lei struct inode *inode = mapping->host; 1682e5ab5f3SMing Lei unsigned short sb_bsize = 0; 1692e5ab5f3SMing Lei unsigned dio_align = 0; 1702e5ab5f3SMing Lei bool use_dio; 1712e5ab5f3SMing Lei 1722e5ab5f3SMing Lei if (inode->i_sb->s_bdev) { 1732e5ab5f3SMing Lei sb_bsize = bdev_logical_block_size(inode->i_sb->s_bdev); 1742e5ab5f3SMing Lei dio_align = sb_bsize - 1; 1752e5ab5f3SMing Lei } 1762e5ab5f3SMing Lei 1772e5ab5f3SMing Lei /* 1782e5ab5f3SMing Lei * We support direct I/O only if lo_offset is aligned with the 1792e5ab5f3SMing Lei * logical I/O size of backing device, and the logical block 18047e96246SChristoph Hellwig * size of loop is bigger than the backing device's. 1812e5ab5f3SMing Lei * 1822e5ab5f3SMing Lei * TODO: the above condition may be loosed in the future, and 1832e5ab5f3SMing Lei * direct I/O may be switched runtime at that time because most 18489d790abSMasahiro Yamada * of requests in sane applications should be PAGE_SIZE aligned 1852e5ab5f3SMing Lei */ 1862e5ab5f3SMing Lei if (dio) { 1872e5ab5f3SMing Lei if (queue_logical_block_size(lo->lo_queue) >= sb_bsize && 1882e5ab5f3SMing Lei !(lo->lo_offset & dio_align) && 18947e96246SChristoph Hellwig mapping->a_ops->direct_IO) 1902e5ab5f3SMing Lei use_dio = true; 1912e5ab5f3SMing Lei else 1922e5ab5f3SMing Lei use_dio = false; 1932e5ab5f3SMing Lei } else { 1942e5ab5f3SMing Lei use_dio = false; 1952e5ab5f3SMing Lei } 1962e5ab5f3SMing Lei 1972e5ab5f3SMing Lei if (lo->use_dio == use_dio) 1982e5ab5f3SMing Lei return; 1992e5ab5f3SMing Lei 2002e5ab5f3SMing Lei /* flush dirty pages before changing direct IO */ 2012e5ab5f3SMing Lei vfs_fsync(file, 0); 2022e5ab5f3SMing Lei 2032e5ab5f3SMing Lei /* 2042e5ab5f3SMing Lei * The flag of LO_FLAGS_DIRECT_IO is handled similarly with 2052e5ab5f3SMing Lei * LO_FLAGS_READ_ONLY, both are set from kernel, and losetup 2062e5ab5f3SMing Lei * will get updated by ioctl(LOOP_GET_STATUS) 2072e5ab5f3SMing Lei */ 2080fbcf579SMartijn Coenen if (lo->lo_state == Lo_bound) 2092e5ab5f3SMing Lei blk_mq_freeze_queue(lo->lo_queue); 2102e5ab5f3SMing Lei lo->use_dio = use_dio; 21140326d8aSShaohua Li if (use_dio) { 2128b904b5bSBart Van Assche blk_queue_flag_clear(QUEUE_FLAG_NOMERGES, lo->lo_queue); 2132e5ab5f3SMing Lei lo->lo_flags |= LO_FLAGS_DIRECT_IO; 21440326d8aSShaohua Li } else { 2158b904b5bSBart Van Assche blk_queue_flag_set(QUEUE_FLAG_NOMERGES, lo->lo_queue); 2162e5ab5f3SMing Lei lo->lo_flags &= ~LO_FLAGS_DIRECT_IO; 21740326d8aSShaohua Li } 2180fbcf579SMartijn Coenen if (lo->lo_state == Lo_bound) 2192e5ab5f3SMing Lei blk_mq_unfreeze_queue(lo->lo_queue); 2202e5ab5f3SMing Lei } 2212e5ab5f3SMing Lei 2225795b6f5SMartijn Coenen /** 2235795b6f5SMartijn Coenen * loop_set_size() - sets device size and notifies userspace 2245795b6f5SMartijn Coenen * @lo: struct loop_device to set the size for 2255795b6f5SMartijn Coenen * @size: new size of the loop device 2265795b6f5SMartijn Coenen * 2275795b6f5SMartijn Coenen * Callers must validate that the size passed into this function fits into 2285795b6f5SMartijn Coenen * a sector_t, eg using loop_validate_size() 2295795b6f5SMartijn Coenen */ 2305795b6f5SMartijn Coenen static void loop_set_size(struct loop_device *lo, loff_t size) 2315795b6f5SMartijn Coenen { 232449f4ec9SChristoph Hellwig if (!set_capacity_and_notify(lo->lo_disk, size)) 2333b4f85d0SChristoph Hellwig kobject_uevent(&disk_to_dev(lo->lo_disk)->kobj, KOBJ_CHANGE); 2341da177e4SLinus Torvalds } 2351da177e4SLinus Torvalds 236aa4d8616SChristoph Hellwig static int lo_write_bvec(struct file *file, struct bio_vec *bvec, loff_t *ppos) 2371da177e4SLinus Torvalds { 238aa4d8616SChristoph Hellwig struct iov_iter i; 2391da177e4SLinus Torvalds ssize_t bw; 240283e7e5dSAl Viro 241b6207430SChristoph Hellwig iov_iter_bvec(&i, WRITE, bvec, 1, bvec->bv_len); 2421da177e4SLinus Torvalds 24303d95eb2SAl Viro file_start_write(file); 244abbb6589SChristoph Hellwig bw = vfs_iter_write(file, &i, ppos, 0); 24503d95eb2SAl Viro file_end_write(file); 246aa4d8616SChristoph Hellwig 247aa4d8616SChristoph Hellwig if (likely(bw == bvec->bv_len)) 2481da177e4SLinus Torvalds return 0; 249aa4d8616SChristoph Hellwig 250aa4d8616SChristoph Hellwig printk_ratelimited(KERN_ERR 251aa4d8616SChristoph Hellwig "loop: Write error at byte offset %llu, length %i.\n", 252aa4d8616SChristoph Hellwig (unsigned long long)*ppos, bvec->bv_len); 2531da177e4SLinus Torvalds if (bw >= 0) 2541da177e4SLinus Torvalds bw = -EIO; 2551da177e4SLinus Torvalds return bw; 2561da177e4SLinus Torvalds } 2571da177e4SLinus Torvalds 258aa4d8616SChristoph Hellwig static int lo_write_simple(struct loop_device *lo, struct request *rq, 259aa4d8616SChristoph Hellwig loff_t pos) 2601da177e4SLinus Torvalds { 261aa4d8616SChristoph Hellwig struct bio_vec bvec; 262aa4d8616SChristoph Hellwig struct req_iterator iter; 263aa4d8616SChristoph Hellwig int ret = 0; 264aa4d8616SChristoph Hellwig 265aa4d8616SChristoph Hellwig rq_for_each_segment(bvec, rq, iter) { 266aa4d8616SChristoph Hellwig ret = lo_write_bvec(lo->lo_backing_file, &bvec, &pos); 267aa4d8616SChristoph Hellwig if (ret < 0) 268aa4d8616SChristoph Hellwig break; 2691da177e4SLinus Torvalds cond_resched(); 2701da177e4SLinus Torvalds } 2711da177e4SLinus Torvalds 272aa4d8616SChristoph Hellwig return ret; 273aa4d8616SChristoph Hellwig } 274aa4d8616SChristoph Hellwig 275aa4d8616SChristoph Hellwig static int lo_read_simple(struct loop_device *lo, struct request *rq, 276aa4d8616SChristoph Hellwig loff_t pos) 2771da177e4SLinus Torvalds { 2787988613bSKent Overstreet struct bio_vec bvec; 27930112013SMing Lei struct req_iterator iter; 280aa4d8616SChristoph Hellwig struct iov_iter i; 281aa4d8616SChristoph Hellwig ssize_t len; 2821da177e4SLinus Torvalds 28330112013SMing Lei rq_for_each_segment(bvec, rq, iter) { 284b6207430SChristoph Hellwig iov_iter_bvec(&i, READ, &bvec, 1, bvec.bv_len); 28518e9710eSChristoph Hellwig len = vfs_iter_read(lo->lo_backing_file, &i, &pos, 0); 286aa4d8616SChristoph Hellwig if (len < 0) 287aa4d8616SChristoph Hellwig return len; 288306df071SDave Young 289aa4d8616SChristoph Hellwig flush_dcache_page(bvec.bv_page); 290aa4d8616SChristoph Hellwig 291aa4d8616SChristoph Hellwig if (len != bvec.bv_len) { 29230112013SMing Lei struct bio *bio; 29330112013SMing Lei 29430112013SMing Lei __rq_for_each_bio(bio, rq) 295306df071SDave Young zero_fill_bio(bio); 2961da177e4SLinus Torvalds break; 297306df071SDave Young } 298aa4d8616SChristoph Hellwig cond_resched(); 2991da177e4SLinus Torvalds } 300aa4d8616SChristoph Hellwig 301306df071SDave Young return 0; 3021da177e4SLinus Torvalds } 3031da177e4SLinus Torvalds 304efcfec57SDarrick J. Wong static int lo_fallocate(struct loop_device *lo, struct request *rq, loff_t pos, 305efcfec57SDarrick J. Wong int mode) 306cf655d95SMing Lei { 307cf655d95SMing Lei /* 308efcfec57SDarrick J. Wong * We use fallocate to manipulate the space mappings used by the image 30947e96246SChristoph Hellwig * a.k.a. discard/zerorange. 310cf655d95SMing Lei */ 311cf655d95SMing Lei struct file *file = lo->lo_backing_file; 312cf655d95SMing Lei int ret; 313cf655d95SMing Lei 314efcfec57SDarrick J. Wong mode |= FALLOC_FL_KEEP_SIZE; 315efcfec57SDarrick J. Wong 3160aab29b8SChaitanya Kulkarni if (!blk_queue_discard(lo->lo_queue)) { 317cf655d95SMing Lei ret = -EOPNOTSUPP; 318cf655d95SMing Lei goto out; 319cf655d95SMing Lei } 320cf655d95SMing Lei 321cf655d95SMing Lei ret = file->f_op->fallocate(file, mode, pos, blk_rq_bytes(rq)); 322cf655d95SMing Lei if (unlikely(ret && ret != -EINVAL && ret != -EOPNOTSUPP)) 323cf655d95SMing Lei ret = -EIO; 324cf655d95SMing Lei out: 325cf655d95SMing Lei return ret; 326cf655d95SMing Lei } 327cf655d95SMing Lei 328cf655d95SMing Lei static int lo_req_flush(struct loop_device *lo, struct request *rq) 329cf655d95SMing Lei { 3309c64e38cSChaitanya Kulkarni int ret = vfs_fsync(lo->lo_backing_file, 0); 331cf655d95SMing Lei if (unlikely(ret && ret != -EINVAL)) 332cf655d95SMing Lei ret = -EIO; 333cf655d95SMing Lei 334cf655d95SMing Lei return ret; 335cf655d95SMing Lei } 336cf655d95SMing Lei 337fe2cb290SChristoph Hellwig static void lo_complete_rq(struct request *rq) 338bc07c10aSMing Lei { 339fe2cb290SChristoph Hellwig struct loop_cmd *cmd = blk_mq_rq_to_pdu(rq); 340f9de14bcSJens Axboe blk_status_t ret = BLK_STS_OK; 341bc07c10aSMing Lei 342f9de14bcSJens Axboe if (!cmd->use_aio || cmd->ret < 0 || cmd->ret == blk_rq_bytes(rq) || 343f9de14bcSJens Axboe req_op(rq) != REQ_OP_READ) { 344f9de14bcSJens Axboe if (cmd->ret < 0) 3458cd55087SEvan Green ret = errno_to_blk_status(cmd->ret); 346f9de14bcSJens Axboe goto end_io; 347bc07c10aSMing Lei } 348fe2cb290SChristoph Hellwig 349f9de14bcSJens Axboe /* 350f9de14bcSJens Axboe * Short READ - if we got some data, advance our request and 351f9de14bcSJens Axboe * retry it. If we got no data, end the rest with EIO. 352f9de14bcSJens Axboe */ 353f9de14bcSJens Axboe if (cmd->ret) { 354f9de14bcSJens Axboe blk_update_request(rq, BLK_STS_OK, cmd->ret); 355f9de14bcSJens Axboe cmd->ret = 0; 356f9de14bcSJens Axboe blk_mq_requeue_request(rq, true); 357f9de14bcSJens Axboe } else { 358f9de14bcSJens Axboe if (cmd->use_aio) { 359f9de14bcSJens Axboe struct bio *bio = rq->bio; 360f9de14bcSJens Axboe 361f9de14bcSJens Axboe while (bio) { 362f9de14bcSJens Axboe zero_fill_bio(bio); 363f9de14bcSJens Axboe bio = bio->bi_next; 364f9de14bcSJens Axboe } 365f9de14bcSJens Axboe } 366f9de14bcSJens Axboe ret = BLK_STS_IOERR; 367f9de14bcSJens Axboe end_io: 368f9de14bcSJens Axboe blk_mq_end_request(rq, ret); 369f9de14bcSJens Axboe } 370bc07c10aSMing Lei } 371bc07c10aSMing Lei 37292d77332SShaohua Li static void lo_rw_aio_do_completion(struct loop_cmd *cmd) 37392d77332SShaohua Li { 3741894e916SJens Axboe struct request *rq = blk_mq_rq_from_pdu(cmd); 3751894e916SJens Axboe 37692d77332SShaohua Li if (!atomic_dec_and_test(&cmd->ref)) 37792d77332SShaohua Li return; 37892d77332SShaohua Li kfree(cmd->bvec); 37992d77332SShaohua Li cmd->bvec = NULL; 38015f73f5bSChristoph Hellwig if (likely(!blk_should_fake_timeout(rq->q))) 3811894e916SJens Axboe blk_mq_complete_request(rq); 38292d77332SShaohua Li } 38392d77332SShaohua Li 3846b19b766SJens Axboe static void lo_rw_aio_complete(struct kiocb *iocb, long ret) 385bc07c10aSMing Lei { 386bc07c10aSMing Lei struct loop_cmd *cmd = container_of(iocb, struct loop_cmd, iocb); 387bc07c10aSMing Lei 388fe2cb290SChristoph Hellwig cmd->ret = ret; 38992d77332SShaohua Li lo_rw_aio_do_completion(cmd); 390bc07c10aSMing Lei } 391bc07c10aSMing Lei 392bc07c10aSMing Lei static int lo_rw_aio(struct loop_device *lo, struct loop_cmd *cmd, 393bc07c10aSMing Lei loff_t pos, bool rw) 394bc07c10aSMing Lei { 395bc07c10aSMing Lei struct iov_iter iter; 39686af5952SMing Lei struct req_iterator rq_iter; 397bc07c10aSMing Lei struct bio_vec *bvec; 3981894e916SJens Axboe struct request *rq = blk_mq_rq_from_pdu(cmd); 39940326d8aSShaohua Li struct bio *bio = rq->bio; 400bc07c10aSMing Lei struct file *file = lo->lo_backing_file; 40186af5952SMing Lei struct bio_vec tmp; 40240326d8aSShaohua Li unsigned int offset; 40386af5952SMing Lei int nr_bvec = 0; 404bc07c10aSMing Lei int ret; 405bc07c10aSMing Lei 40686af5952SMing Lei rq_for_each_bvec(tmp, rq, rq_iter) 40786af5952SMing Lei nr_bvec++; 408bc07c10aSMing Lei 40986af5952SMing Lei if (rq->bio != rq->biotail) { 41086af5952SMing Lei 41186af5952SMing Lei bvec = kmalloc_array(nr_bvec, sizeof(struct bio_vec), 4126da2ec56SKees Cook GFP_NOIO); 41340326d8aSShaohua Li if (!bvec) 41440326d8aSShaohua Li return -EIO; 41540326d8aSShaohua Li cmd->bvec = bvec; 41640326d8aSShaohua Li 417a7297a6aSMing Lei /* 41840326d8aSShaohua Li * The bios of the request may be started from the middle of 41940326d8aSShaohua Li * the 'bvec' because of bio splitting, so we can't directly 42086af5952SMing Lei * copy bio->bi_iov_vec to new bvec. The rq_for_each_bvec 42140326d8aSShaohua Li * API will take care of all details for us. 422a7297a6aSMing Lei */ 42386af5952SMing Lei rq_for_each_bvec(tmp, rq, rq_iter) { 42440326d8aSShaohua Li *bvec = tmp; 42540326d8aSShaohua Li bvec++; 42640326d8aSShaohua Li } 42740326d8aSShaohua Li bvec = cmd->bvec; 42840326d8aSShaohua Li offset = 0; 42940326d8aSShaohua Li } else { 43040326d8aSShaohua Li /* 43140326d8aSShaohua Li * Same here, this bio may be started from the middle of the 43240326d8aSShaohua Li * 'bvec' because of bio splitting, so offset from the bvec 43340326d8aSShaohua Li * must be passed to iov iterator 43440326d8aSShaohua Li */ 43540326d8aSShaohua Li offset = bio->bi_iter.bi_bvec_done; 43640326d8aSShaohua Li bvec = __bvec_iter_bvec(bio->bi_io_vec, bio->bi_iter); 43740326d8aSShaohua Li } 43892d77332SShaohua Li atomic_set(&cmd->ref, 2); 43940326d8aSShaohua Li 440b6207430SChristoph Hellwig iov_iter_bvec(&iter, rw, bvec, nr_bvec, blk_rq_bytes(rq)); 44140326d8aSShaohua Li iter.iov_offset = offset; 442bc07c10aSMing Lei 443bc07c10aSMing Lei cmd->iocb.ki_pos = pos; 444bc07c10aSMing Lei cmd->iocb.ki_filp = file; 445bc07c10aSMing Lei cmd->iocb.ki_complete = lo_rw_aio_complete; 446bc07c10aSMing Lei cmd->iocb.ki_flags = IOCB_DIRECT; 447d9a08a9eSAdam Manzanares cmd->iocb.ki_ioprio = IOPRIO_PRIO_VALUE(IOPRIO_CLASS_NONE, 0); 448bc07c10aSMing Lei 449bc07c10aSMing Lei if (rw == WRITE) 450bb7462b6SMiklos Szeredi ret = call_write_iter(file, &cmd->iocb, &iter); 451bc07c10aSMing Lei else 452bb7462b6SMiklos Szeredi ret = call_read_iter(file, &cmd->iocb, &iter); 453bc07c10aSMing Lei 45492d77332SShaohua Li lo_rw_aio_do_completion(cmd); 45592d77332SShaohua Li 456bc07c10aSMing Lei if (ret != -EIOCBQUEUED) 4576b19b766SJens Axboe lo_rw_aio_complete(&cmd->iocb, ret); 458bc07c10aSMing Lei return 0; 459bc07c10aSMing Lei } 460bc07c10aSMing Lei 461c1c87c2bSChristoph Hellwig static int do_req_filebacked(struct loop_device *lo, struct request *rq) 462bc07c10aSMing Lei { 463bc07c10aSMing Lei struct loop_cmd *cmd = blk_mq_rq_to_pdu(rq); 464c1c87c2bSChristoph Hellwig loff_t pos = ((loff_t) blk_rq_pos(rq) << 9) + lo->lo_offset; 465bc07c10aSMing Lei 466bc07c10aSMing Lei /* 467bc07c10aSMing Lei * lo_write_simple and lo_read_simple should have been covered 468bc07c10aSMing Lei * by io submit style function like lo_rw_aio(), one blocker 469bc07c10aSMing Lei * is that lo_read_simple() need to call flush_dcache_page after 470bc07c10aSMing Lei * the page is written from kernel, and it isn't easy to handle 471bc07c10aSMing Lei * this in io submit style function which submits all segments 472bc07c10aSMing Lei * of the req at one time. And direct read IO doesn't need to 473bc07c10aSMing Lei * run flush_dcache_page(). 474bc07c10aSMing Lei */ 475c1c87c2bSChristoph Hellwig switch (req_op(rq)) { 476c1c87c2bSChristoph Hellwig case REQ_OP_FLUSH: 477c1c87c2bSChristoph Hellwig return lo_req_flush(lo, rq); 47819372e27SChristoph Hellwig case REQ_OP_WRITE_ZEROES: 479efcfec57SDarrick J. Wong /* 480efcfec57SDarrick J. Wong * If the caller doesn't want deallocation, call zeroout to 481efcfec57SDarrick J. Wong * write zeroes the range. Otherwise, punch them out. 482efcfec57SDarrick J. Wong */ 483efcfec57SDarrick J. Wong return lo_fallocate(lo, rq, pos, 484efcfec57SDarrick J. Wong (rq->cmd_flags & REQ_NOUNMAP) ? 485efcfec57SDarrick J. Wong FALLOC_FL_ZERO_RANGE : 486efcfec57SDarrick J. Wong FALLOC_FL_PUNCH_HOLE); 487efcfec57SDarrick J. Wong case REQ_OP_DISCARD: 488efcfec57SDarrick J. Wong return lo_fallocate(lo, rq, pos, FALLOC_FL_PUNCH_HOLE); 489c1c87c2bSChristoph Hellwig case REQ_OP_WRITE: 49047e96246SChristoph Hellwig if (cmd->use_aio) 491c1c87c2bSChristoph Hellwig return lo_rw_aio(lo, cmd, pos, WRITE); 492c1c87c2bSChristoph Hellwig else 493bc07c10aSMing Lei return lo_write_simple(lo, rq, pos); 494c1c87c2bSChristoph Hellwig case REQ_OP_READ: 49547e96246SChristoph Hellwig if (cmd->use_aio) 496c1c87c2bSChristoph Hellwig return lo_rw_aio(lo, cmd, pos, READ); 497bc07c10aSMing Lei else 498bc07c10aSMing Lei return lo_read_simple(lo, rq, pos); 499c1c87c2bSChristoph Hellwig default: 500c1c87c2bSChristoph Hellwig WARN_ON_ONCE(1); 501c1c87c2bSChristoph Hellwig return -EIO; 502bc07c10aSMing Lei } 5031da177e4SLinus Torvalds } 5041da177e4SLinus Torvalds 5052e5ab5f3SMing Lei static inline void loop_update_dio(struct loop_device *lo) 5062e5ab5f3SMing Lei { 507efbe3c24SIra Weiny __loop_update_dio(lo, (lo->lo_backing_file->f_flags & O_DIRECT) | 5082e5ab5f3SMing Lei lo->use_dio); 5092e5ab5f3SMing Lei } 5102e5ab5f3SMing Lei 5110384264eSChristoph Hellwig static void loop_reread_partitions(struct loop_device *lo) 51206f0e9e6SMing Lei { 51306f0e9e6SMing Lei int rc; 51406f0e9e6SMing Lei 5150384264eSChristoph Hellwig mutex_lock(&lo->lo_disk->open_mutex); 5160384264eSChristoph Hellwig rc = bdev_disk_changed(lo->lo_disk, false); 5170384264eSChristoph Hellwig mutex_unlock(&lo->lo_disk->open_mutex); 51806f0e9e6SMing Lei if (rc) 51906f0e9e6SMing Lei pr_warn("%s: partition scan of loop%d (%s) failed (rc=%d)\n", 52006f0e9e6SMing Lei __func__, lo->lo_number, lo->lo_file_name, rc); 52106f0e9e6SMing Lei } 52206f0e9e6SMing Lei 523d2ac838eSTheodore Ts'o static inline int is_loop_device(struct file *file) 524d2ac838eSTheodore Ts'o { 525d2ac838eSTheodore Ts'o struct inode *i = file->f_mapping->host; 526d2ac838eSTheodore Ts'o 5276f24784fSAl Viro return i && S_ISBLK(i->i_mode) && imajor(i) == LOOP_MAJOR; 528d2ac838eSTheodore Ts'o } 529d2ac838eSTheodore Ts'o 530d2ac838eSTheodore Ts'o static int loop_validate_file(struct file *file, struct block_device *bdev) 531d2ac838eSTheodore Ts'o { 532d2ac838eSTheodore Ts'o struct inode *inode = file->f_mapping->host; 533d2ac838eSTheodore Ts'o struct file *f = file; 534d2ac838eSTheodore Ts'o 535d2ac838eSTheodore Ts'o /* Avoid recursion */ 536d2ac838eSTheodore Ts'o while (is_loop_device(f)) { 537d2ac838eSTheodore Ts'o struct loop_device *l; 538d2ac838eSTheodore Ts'o 5393ce6e1f6STetsuo Handa lockdep_assert_held(&loop_validate_mutex); 5404e7b5671SChristoph Hellwig if (f->f_mapping->host->i_rdev == bdev->bd_dev) 541d2ac838eSTheodore Ts'o return -EBADF; 542d2ac838eSTheodore Ts'o 5434e7b5671SChristoph Hellwig l = I_BDEV(f->f_mapping->host)->bd_disk->private_data; 5443ce6e1f6STetsuo Handa if (l->lo_state != Lo_bound) 545d2ac838eSTheodore Ts'o return -EINVAL; 5463ce6e1f6STetsuo Handa /* Order wrt setting lo->lo_backing_file in loop_configure(). */ 5473ce6e1f6STetsuo Handa rmb(); 548d2ac838eSTheodore Ts'o f = l->lo_backing_file; 549d2ac838eSTheodore Ts'o } 550d2ac838eSTheodore Ts'o if (!S_ISREG(inode->i_mode) && !S_ISBLK(inode->i_mode)) 551d2ac838eSTheodore Ts'o return -EINVAL; 552d2ac838eSTheodore Ts'o return 0; 553d2ac838eSTheodore Ts'o } 554d2ac838eSTheodore Ts'o 5551da177e4SLinus Torvalds /* 5561da177e4SLinus Torvalds * loop_change_fd switched the backing store of a loopback device to 5571da177e4SLinus Torvalds * a new file. This is useful for operating system installers to free up 5581da177e4SLinus Torvalds * the original file and in High Availability environments to switch to 5591da177e4SLinus Torvalds * an alternative location for the content in case of server meltdown. 5601da177e4SLinus Torvalds * This can only work if the loop device is used read-only, and if the 5611da177e4SLinus Torvalds * new backing store is the same size and type as the old backing store. 5621da177e4SLinus Torvalds */ 563bb214884SAl Viro static int loop_change_fd(struct loop_device *lo, struct block_device *bdev, 564bb214884SAl Viro unsigned int arg) 5651da177e4SLinus Torvalds { 5663ce6e1f6STetsuo Handa struct file *file = fget(arg); 5673ce6e1f6STetsuo Handa struct file *old_file; 5681da177e4SLinus Torvalds int error; 56985b0a54aSJan Kara bool partscan; 5703ce6e1f6STetsuo Handa bool is_loop; 5711da177e4SLinus Torvalds 5723ce6e1f6STetsuo Handa if (!file) 5733ce6e1f6STetsuo Handa return -EBADF; 5743ce6e1f6STetsuo Handa is_loop = is_loop_device(file); 5753ce6e1f6STetsuo Handa error = loop_global_lock_killable(lo, is_loop); 576c3710770SJan Kara if (error) 5773ce6e1f6STetsuo Handa goto out_putf; 5781da177e4SLinus Torvalds error = -ENXIO; 5791da177e4SLinus Torvalds if (lo->lo_state != Lo_bound) 5801dded9acSJan Kara goto out_err; 5811da177e4SLinus Torvalds 5821da177e4SLinus Torvalds /* the loop device has to be read-only */ 5831da177e4SLinus Torvalds error = -EINVAL; 5841da177e4SLinus Torvalds if (!(lo->lo_flags & LO_FLAGS_READ_ONLY)) 5851dded9acSJan Kara goto out_err; 5861da177e4SLinus Torvalds 587d2ac838eSTheodore Ts'o error = loop_validate_file(file, bdev); 588d2ac838eSTheodore Ts'o if (error) 5891dded9acSJan Kara goto out_err; 590d2ac838eSTheodore Ts'o 5911da177e4SLinus Torvalds old_file = lo->lo_backing_file; 5921da177e4SLinus Torvalds 5931da177e4SLinus Torvalds error = -EINVAL; 5941da177e4SLinus Torvalds 5951da177e4SLinus Torvalds /* size of the new backing store needs to be the same */ 5961da177e4SLinus Torvalds if (get_loop_size(lo, file) != get_loop_size(lo, old_file)) 5971dded9acSJan Kara goto out_err; 5981da177e4SLinus Torvalds 5991da177e4SLinus Torvalds /* and ... switch */ 6009f65c489SMatteo Croce disk_force_media_change(lo->lo_disk, DISK_EVENT_MEDIA_CHANGE); 60143cade80SOmar Sandoval blk_mq_freeze_queue(lo->lo_queue); 60243cade80SOmar Sandoval mapping_set_gfp_mask(old_file->f_mapping, lo->old_gfp_mask); 60343cade80SOmar Sandoval lo->lo_backing_file = file; 60443cade80SOmar Sandoval lo->old_gfp_mask = mapping_gfp_mask(file->f_mapping); 60543cade80SOmar Sandoval mapping_set_gfp_mask(file->f_mapping, 60643cade80SOmar Sandoval lo->old_gfp_mask & ~(__GFP_IO|__GFP_FS)); 60743cade80SOmar Sandoval loop_update_dio(lo); 60843cade80SOmar Sandoval blk_mq_unfreeze_queue(lo->lo_queue); 60985b0a54aSJan Kara partscan = lo->lo_flags & LO_FLAGS_PARTSCAN; 6103ce6e1f6STetsuo Handa loop_global_unlock(lo, is_loop); 6113ce6e1f6STetsuo Handa 6123ce6e1f6STetsuo Handa /* 6133ce6e1f6STetsuo Handa * Flush loop_validate_file() before fput(), for l->lo_backing_file 6143ce6e1f6STetsuo Handa * might be pointing at old_file which might be the last reference. 6153ce6e1f6STetsuo Handa */ 6163ce6e1f6STetsuo Handa if (!is_loop) { 6173ce6e1f6STetsuo Handa mutex_lock(&loop_validate_mutex); 6183ce6e1f6STetsuo Handa mutex_unlock(&loop_validate_mutex); 6193ce6e1f6STetsuo Handa } 6201dded9acSJan Kara /* 6216cc8e743SPavel Tatashin * We must drop file reference outside of lo_mutex as dropping 622a8698707SChristoph Hellwig * the file ref can take open_mutex which creates circular locking 6231dded9acSJan Kara * dependency. 6241dded9acSJan Kara */ 6251dded9acSJan Kara fput(old_file); 62685b0a54aSJan Kara if (partscan) 6270384264eSChristoph Hellwig loop_reread_partitions(lo); 6281da177e4SLinus Torvalds return 0; 6291da177e4SLinus Torvalds 6301dded9acSJan Kara out_err: 6313ce6e1f6STetsuo Handa loop_global_unlock(lo, is_loop); 6323ce6e1f6STetsuo Handa out_putf: 6331dded9acSJan Kara fput(file); 6341da177e4SLinus Torvalds return error; 6351da177e4SLinus Torvalds } 6361da177e4SLinus Torvalds 637ee862730SMilan Broz /* loop sysfs attributes */ 638ee862730SMilan Broz 639ee862730SMilan Broz static ssize_t loop_attr_show(struct device *dev, char *page, 640ee862730SMilan Broz ssize_t (*callback)(struct loop_device *, char *)) 641ee862730SMilan Broz { 64234dd82afSKay Sievers struct gendisk *disk = dev_to_disk(dev); 64334dd82afSKay Sievers struct loop_device *lo = disk->private_data; 644ee862730SMilan Broz 64534dd82afSKay Sievers return callback(lo, page); 646ee862730SMilan Broz } 647ee862730SMilan Broz 648ee862730SMilan Broz #define LOOP_ATTR_RO(_name) \ 649ee862730SMilan Broz static ssize_t loop_attr_##_name##_show(struct loop_device *, char *); \ 650ee862730SMilan Broz static ssize_t loop_attr_do_show_##_name(struct device *d, \ 651ee862730SMilan Broz struct device_attribute *attr, char *b) \ 652ee862730SMilan Broz { \ 653ee862730SMilan Broz return loop_attr_show(d, b, loop_attr_##_name##_show); \ 654ee862730SMilan Broz } \ 655ee862730SMilan Broz static struct device_attribute loop_attr_##_name = \ 6565657a819SJoe Perches __ATTR(_name, 0444, loop_attr_do_show_##_name, NULL); 657ee862730SMilan Broz 658ee862730SMilan Broz static ssize_t loop_attr_backing_file_show(struct loop_device *lo, char *buf) 659ee862730SMilan Broz { 660ee862730SMilan Broz ssize_t ret; 661ee862730SMilan Broz char *p = NULL; 662ee862730SMilan Broz 66305eb0f25SKay Sievers spin_lock_irq(&lo->lo_lock); 664ee862730SMilan Broz if (lo->lo_backing_file) 6659bf39ab2SMiklos Szeredi p = file_path(lo->lo_backing_file, buf, PAGE_SIZE - 1); 66605eb0f25SKay Sievers spin_unlock_irq(&lo->lo_lock); 667ee862730SMilan Broz 668ee862730SMilan Broz if (IS_ERR_OR_NULL(p)) 669ee862730SMilan Broz ret = PTR_ERR(p); 670ee862730SMilan Broz else { 671ee862730SMilan Broz ret = strlen(p); 672ee862730SMilan Broz memmove(buf, p, ret); 673ee862730SMilan Broz buf[ret++] = '\n'; 674ee862730SMilan Broz buf[ret] = 0; 675ee862730SMilan Broz } 676ee862730SMilan Broz 677ee862730SMilan Broz return ret; 678ee862730SMilan Broz } 679ee862730SMilan Broz 680ee862730SMilan Broz static ssize_t loop_attr_offset_show(struct loop_device *lo, char *buf) 681ee862730SMilan Broz { 682b27824d3SChaitanya Kulkarni return sysfs_emit(buf, "%llu\n", (unsigned long long)lo->lo_offset); 683ee862730SMilan Broz } 684ee862730SMilan Broz 685ee862730SMilan Broz static ssize_t loop_attr_sizelimit_show(struct loop_device *lo, char *buf) 686ee862730SMilan Broz { 687b27824d3SChaitanya Kulkarni return sysfs_emit(buf, "%llu\n", (unsigned long long)lo->lo_sizelimit); 688ee862730SMilan Broz } 689ee862730SMilan Broz 690ee862730SMilan Broz static ssize_t loop_attr_autoclear_show(struct loop_device *lo, char *buf) 691ee862730SMilan Broz { 692ee862730SMilan Broz int autoclear = (lo->lo_flags & LO_FLAGS_AUTOCLEAR); 693ee862730SMilan Broz 694b27824d3SChaitanya Kulkarni return sysfs_emit(buf, "%s\n", autoclear ? "1" : "0"); 695ee862730SMilan Broz } 696ee862730SMilan Broz 697e03c8dd1SKay Sievers static ssize_t loop_attr_partscan_show(struct loop_device *lo, char *buf) 698e03c8dd1SKay Sievers { 699e03c8dd1SKay Sievers int partscan = (lo->lo_flags & LO_FLAGS_PARTSCAN); 700e03c8dd1SKay Sievers 701b27824d3SChaitanya Kulkarni return sysfs_emit(buf, "%s\n", partscan ? "1" : "0"); 702e03c8dd1SKay Sievers } 703e03c8dd1SKay Sievers 7042e5ab5f3SMing Lei static ssize_t loop_attr_dio_show(struct loop_device *lo, char *buf) 7052e5ab5f3SMing Lei { 7062e5ab5f3SMing Lei int dio = (lo->lo_flags & LO_FLAGS_DIRECT_IO); 7072e5ab5f3SMing Lei 708b27824d3SChaitanya Kulkarni return sysfs_emit(buf, "%s\n", dio ? "1" : "0"); 7092e5ab5f3SMing Lei } 7102e5ab5f3SMing Lei 711ee862730SMilan Broz LOOP_ATTR_RO(backing_file); 712ee862730SMilan Broz LOOP_ATTR_RO(offset); 713ee862730SMilan Broz LOOP_ATTR_RO(sizelimit); 714ee862730SMilan Broz LOOP_ATTR_RO(autoclear); 715e03c8dd1SKay Sievers LOOP_ATTR_RO(partscan); 7162e5ab5f3SMing Lei LOOP_ATTR_RO(dio); 717ee862730SMilan Broz 718ee862730SMilan Broz static struct attribute *loop_attrs[] = { 719ee862730SMilan Broz &loop_attr_backing_file.attr, 720ee862730SMilan Broz &loop_attr_offset.attr, 721ee862730SMilan Broz &loop_attr_sizelimit.attr, 722ee862730SMilan Broz &loop_attr_autoclear.attr, 723e03c8dd1SKay Sievers &loop_attr_partscan.attr, 7242e5ab5f3SMing Lei &loop_attr_dio.attr, 725ee862730SMilan Broz NULL, 726ee862730SMilan Broz }; 727ee862730SMilan Broz 728ee862730SMilan Broz static struct attribute_group loop_attribute_group = { 729ee862730SMilan Broz .name = "loop", 730ee862730SMilan Broz .attrs= loop_attrs, 731ee862730SMilan Broz }; 732ee862730SMilan Broz 733d3349b6bSTetsuo Handa static void loop_sysfs_init(struct loop_device *lo) 734ee862730SMilan Broz { 735d3349b6bSTetsuo Handa lo->sysfs_inited = !sysfs_create_group(&disk_to_dev(lo->lo_disk)->kobj, 736ee862730SMilan Broz &loop_attribute_group); 737ee862730SMilan Broz } 738ee862730SMilan Broz 739ee862730SMilan Broz static void loop_sysfs_exit(struct loop_device *lo) 740ee862730SMilan Broz { 741d3349b6bSTetsuo Handa if (lo->sysfs_inited) 742ee862730SMilan Broz sysfs_remove_group(&disk_to_dev(lo->lo_disk)->kobj, 743ee862730SMilan Broz &loop_attribute_group); 744ee862730SMilan Broz } 745ee862730SMilan Broz 746dfaa2ef6SLukas Czerner static void loop_config_discard(struct loop_device *lo) 747dfaa2ef6SLukas Czerner { 748dfaa2ef6SLukas Czerner struct file *file = lo->lo_backing_file; 749dfaa2ef6SLukas Czerner struct inode *inode = file->f_mapping->host; 750dfaa2ef6SLukas Czerner struct request_queue *q = lo->lo_queue; 751bcb21c8cSMing Lei u32 granularity, max_discard_sectors; 752dfaa2ef6SLukas Czerner 753dfaa2ef6SLukas Czerner /* 754c52abf56SEvan Green * If the backing device is a block device, mirror its zeroing 755c52abf56SEvan Green * capability. Set the discard sectors to the block device's zeroing 756c52abf56SEvan Green * capabilities because loop discards result in blkdev_issue_zeroout(), 757c52abf56SEvan Green * not blkdev_issue_discard(). This maintains consistent behavior with 758c52abf56SEvan Green * file-backed loop devices: discarded regions read back as zero. 759c52abf56SEvan Green */ 76047e96246SChristoph Hellwig if (S_ISBLK(inode->i_mode)) { 7614e7b5671SChristoph Hellwig struct request_queue *backingq = bdev_get_queue(I_BDEV(inode)); 762c52abf56SEvan Green 763bcb21c8cSMing Lei max_discard_sectors = backingq->limits.max_write_zeroes_sectors; 764bcb21c8cSMing Lei granularity = backingq->limits.discard_granularity ?: 765bcb21c8cSMing Lei queue_physical_block_size(backingq); 766c52abf56SEvan Green 767c52abf56SEvan Green /* 768dfaa2ef6SLukas Czerner * We use punch hole to reclaim the free space used by the 76947e96246SChristoph Hellwig * image a.k.a. discard. 770dfaa2ef6SLukas Czerner */ 77147e96246SChristoph Hellwig } else if (!file->f_op->fallocate) { 772bcb21c8cSMing Lei max_discard_sectors = 0; 773bcb21c8cSMing Lei granularity = 0; 774dfaa2ef6SLukas Czerner 775c52abf56SEvan Green } else { 776bcb21c8cSMing Lei max_discard_sectors = UINT_MAX >> 9; 777bcb21c8cSMing Lei granularity = inode->i_sb->s_blocksize; 778c52abf56SEvan Green } 779c52abf56SEvan Green 780bcb21c8cSMing Lei if (max_discard_sectors) { 781bcb21c8cSMing Lei q->limits.discard_granularity = granularity; 782bcb21c8cSMing Lei blk_queue_max_discard_sectors(q, max_discard_sectors); 783bcb21c8cSMing Lei blk_queue_max_write_zeroes_sectors(q, max_discard_sectors); 7848b904b5bSBart Van Assche blk_queue_flag_set(QUEUE_FLAG_DISCARD, q); 785bcb21c8cSMing Lei } else { 786bcb21c8cSMing Lei q->limits.discard_granularity = 0; 787bcb21c8cSMing Lei blk_queue_max_discard_sectors(q, 0); 788bcb21c8cSMing Lei blk_queue_max_write_zeroes_sectors(q, 0); 789c52abf56SEvan Green blk_queue_flag_clear(QUEUE_FLAG_DISCARD, q); 790dfaa2ef6SLukas Czerner } 791bcb21c8cSMing Lei q->limits.discard_alignment = 0; 792bcb21c8cSMing Lei } 793dfaa2ef6SLukas Czerner 79487579e9bSDan Schatzberg struct loop_worker { 79587579e9bSDan Schatzberg struct rb_node rb_node; 79687579e9bSDan Schatzberg struct work_struct work; 79787579e9bSDan Schatzberg struct list_head cmd_list; 79887579e9bSDan Schatzberg struct list_head idle_list; 79987579e9bSDan Schatzberg struct loop_device *lo; 800c74d40e8SDan Schatzberg struct cgroup_subsys_state *blkcg_css; 80187579e9bSDan Schatzberg unsigned long last_ran_at; 80287579e9bSDan Schatzberg }; 803e03a3d7aSMing Lei 80487579e9bSDan Schatzberg static void loop_workfn(struct work_struct *work); 80587579e9bSDan Schatzberg static void loop_rootcg_workfn(struct work_struct *work); 80687579e9bSDan Schatzberg static void loop_free_idle_workers(struct timer_list *timer); 807b2ee7d46SNeilBrown 80887579e9bSDan Schatzberg #ifdef CONFIG_BLK_CGROUP 80987579e9bSDan Schatzberg static inline int queue_on_root_worker(struct cgroup_subsys_state *css) 810e03a3d7aSMing Lei { 81187579e9bSDan Schatzberg return !css || css == blkcg_root_css; 81287579e9bSDan Schatzberg } 81387579e9bSDan Schatzberg #else 81487579e9bSDan Schatzberg static inline int queue_on_root_worker(struct cgroup_subsys_state *css) 81587579e9bSDan Schatzberg { 81687579e9bSDan Schatzberg return !css; 81787579e9bSDan Schatzberg } 81887579e9bSDan Schatzberg #endif 81987579e9bSDan Schatzberg 82087579e9bSDan Schatzberg static void loop_queue_work(struct loop_device *lo, struct loop_cmd *cmd) 82187579e9bSDan Schatzberg { 822413ec805SColin Ian King struct rb_node **node, *parent = NULL; 82387579e9bSDan Schatzberg struct loop_worker *cur_worker, *worker = NULL; 82487579e9bSDan Schatzberg struct work_struct *work; 82587579e9bSDan Schatzberg struct list_head *cmd_list; 82687579e9bSDan Schatzberg 82787579e9bSDan Schatzberg spin_lock_irq(&lo->lo_work_lock); 82887579e9bSDan Schatzberg 829c74d40e8SDan Schatzberg if (queue_on_root_worker(cmd->blkcg_css)) 83087579e9bSDan Schatzberg goto queue_work; 83187579e9bSDan Schatzberg 83287579e9bSDan Schatzberg node = &lo->worker_tree.rb_node; 83387579e9bSDan Schatzberg 83487579e9bSDan Schatzberg while (*node) { 83587579e9bSDan Schatzberg parent = *node; 83687579e9bSDan Schatzberg cur_worker = container_of(*node, struct loop_worker, rb_node); 837c74d40e8SDan Schatzberg if (cur_worker->blkcg_css == cmd->blkcg_css) { 83887579e9bSDan Schatzberg worker = cur_worker; 83987579e9bSDan Schatzberg break; 840c74d40e8SDan Schatzberg } else if ((long)cur_worker->blkcg_css < (long)cmd->blkcg_css) { 84187579e9bSDan Schatzberg node = &(*node)->rb_left; 84287579e9bSDan Schatzberg } else { 84387579e9bSDan Schatzberg node = &(*node)->rb_right; 84487579e9bSDan Schatzberg } 84587579e9bSDan Schatzberg } 84687579e9bSDan Schatzberg if (worker) 84787579e9bSDan Schatzberg goto queue_work; 84887579e9bSDan Schatzberg 84987579e9bSDan Schatzberg worker = kzalloc(sizeof(struct loop_worker), GFP_NOWAIT | __GFP_NOWARN); 85087579e9bSDan Schatzberg /* 85187579e9bSDan Schatzberg * In the event we cannot allocate a worker, just queue on the 852c74d40e8SDan Schatzberg * rootcg worker and issue the I/O as the rootcg 85387579e9bSDan Schatzberg */ 854c74d40e8SDan Schatzberg if (!worker) { 855c74d40e8SDan Schatzberg cmd->blkcg_css = NULL; 856c74d40e8SDan Schatzberg if (cmd->memcg_css) 857c74d40e8SDan Schatzberg css_put(cmd->memcg_css); 858c74d40e8SDan Schatzberg cmd->memcg_css = NULL; 85987579e9bSDan Schatzberg goto queue_work; 860c74d40e8SDan Schatzberg } 86187579e9bSDan Schatzberg 862c74d40e8SDan Schatzberg worker->blkcg_css = cmd->blkcg_css; 863c74d40e8SDan Schatzberg css_get(worker->blkcg_css); 86487579e9bSDan Schatzberg INIT_WORK(&worker->work, loop_workfn); 86587579e9bSDan Schatzberg INIT_LIST_HEAD(&worker->cmd_list); 86687579e9bSDan Schatzberg INIT_LIST_HEAD(&worker->idle_list); 86787579e9bSDan Schatzberg worker->lo = lo; 86887579e9bSDan Schatzberg rb_link_node(&worker->rb_node, parent, node); 86987579e9bSDan Schatzberg rb_insert_color(&worker->rb_node, &lo->worker_tree); 87087579e9bSDan Schatzberg queue_work: 87187579e9bSDan Schatzberg if (worker) { 87287579e9bSDan Schatzberg /* 87387579e9bSDan Schatzberg * We need to remove from the idle list here while 87487579e9bSDan Schatzberg * holding the lock so that the idle timer doesn't 87587579e9bSDan Schatzberg * free the worker 87687579e9bSDan Schatzberg */ 87787579e9bSDan Schatzberg if (!list_empty(&worker->idle_list)) 87887579e9bSDan Schatzberg list_del_init(&worker->idle_list); 87987579e9bSDan Schatzberg work = &worker->work; 88087579e9bSDan Schatzberg cmd_list = &worker->cmd_list; 88187579e9bSDan Schatzberg } else { 88287579e9bSDan Schatzberg work = &lo->rootcg_work; 88387579e9bSDan Schatzberg cmd_list = &lo->rootcg_cmd_list; 88487579e9bSDan Schatzberg } 88587579e9bSDan Schatzberg list_add_tail(&cmd->list_entry, cmd_list); 88687579e9bSDan Schatzberg queue_work(lo->workqueue, work); 88787579e9bSDan Schatzberg spin_unlock_irq(&lo->lo_work_lock); 888e03a3d7aSMing Lei } 889e03a3d7aSMing Lei 89056a85fd8SHolger Hoffstätte static void loop_update_rotational(struct loop_device *lo) 89156a85fd8SHolger Hoffstätte { 89256a85fd8SHolger Hoffstätte struct file *file = lo->lo_backing_file; 89356a85fd8SHolger Hoffstätte struct inode *file_inode = file->f_mapping->host; 89456a85fd8SHolger Hoffstätte struct block_device *file_bdev = file_inode->i_sb->s_bdev; 89556a85fd8SHolger Hoffstätte struct request_queue *q = lo->lo_queue; 89656a85fd8SHolger Hoffstätte bool nonrot = true; 89756a85fd8SHolger Hoffstätte 89856a85fd8SHolger Hoffstätte /* not all filesystems (e.g. tmpfs) have a sb->s_bdev */ 89956a85fd8SHolger Hoffstätte if (file_bdev) 90056a85fd8SHolger Hoffstätte nonrot = blk_queue_nonrot(bdev_get_queue(file_bdev)); 90156a85fd8SHolger Hoffstätte 90256a85fd8SHolger Hoffstätte if (nonrot) 90356a85fd8SHolger Hoffstätte blk_queue_flag_set(QUEUE_FLAG_NONROT, q); 90456a85fd8SHolger Hoffstätte else 90556a85fd8SHolger Hoffstätte blk_queue_flag_clear(QUEUE_FLAG_NONROT, q); 90656a85fd8SHolger Hoffstätte } 90756a85fd8SHolger Hoffstätte 90862ab466cSMartijn Coenen /** 90962ab466cSMartijn Coenen * loop_set_status_from_info - configure device from loop_info 91062ab466cSMartijn Coenen * @lo: struct loop_device to configure 91162ab466cSMartijn Coenen * @info: struct loop_info64 to configure the device with 91262ab466cSMartijn Coenen * 91362ab466cSMartijn Coenen * Configures the loop device parameters according to the passed 91462ab466cSMartijn Coenen * in loop_info64 configuration. 91562ab466cSMartijn Coenen */ 91662ab466cSMartijn Coenen static int 91762ab466cSMartijn Coenen loop_set_status_from_info(struct loop_device *lo, 91862ab466cSMartijn Coenen const struct loop_info64 *info) 91962ab466cSMartijn Coenen { 92062ab466cSMartijn Coenen if ((unsigned int) info->lo_encrypt_key_size > LO_KEY_SIZE) 92162ab466cSMartijn Coenen return -EINVAL; 92262ab466cSMartijn Coenen 92347e96246SChristoph Hellwig switch (info->lo_encrypt_type) { 92447e96246SChristoph Hellwig case LO_CRYPT_NONE: 92547e96246SChristoph Hellwig break; 92647e96246SChristoph Hellwig case LO_CRYPT_XOR: 92747e96246SChristoph Hellwig pr_warn("support for the xor transformation has been removed.\n"); 92862ab466cSMartijn Coenen return -EINVAL; 92947e96246SChristoph Hellwig case LO_CRYPT_CRYPTOAPI: 93047e96246SChristoph Hellwig pr_warn("support for cryptoloop has been removed. Use dm-crypt instead.\n"); 93162ab466cSMartijn Coenen return -EINVAL; 93247e96246SChristoph Hellwig default: 93347e96246SChristoph Hellwig return -EINVAL; 93447e96246SChristoph Hellwig } 93562ab466cSMartijn Coenen 93662ab466cSMartijn Coenen lo->lo_offset = info->lo_offset; 93762ab466cSMartijn Coenen lo->lo_sizelimit = info->lo_sizelimit; 93862ab466cSMartijn Coenen memcpy(lo->lo_file_name, info->lo_file_name, LO_NAME_SIZE); 93962ab466cSMartijn Coenen lo->lo_file_name[LO_NAME_SIZE-1] = 0; 940faf1d254SMartijn Coenen lo->lo_flags = info->lo_flags; 94162ab466cSMartijn Coenen return 0; 94262ab466cSMartijn Coenen } 94362ab466cSMartijn Coenen 9443448914eSMartijn Coenen static int loop_configure(struct loop_device *lo, fmode_t mode, 9453448914eSMartijn Coenen struct block_device *bdev, 9463448914eSMartijn Coenen const struct loop_config *config) 9471da177e4SLinus Torvalds { 9483ce6e1f6STetsuo Handa struct file *file = fget(config->fd); 9491da177e4SLinus Torvalds struct inode *inode; 9501da177e4SLinus Torvalds struct address_space *mapping; 9511da177e4SLinus Torvalds int error; 9521da177e4SLinus Torvalds loff_t size; 95385b0a54aSJan Kara bool partscan; 9543448914eSMartijn Coenen unsigned short bsize; 9553ce6e1f6STetsuo Handa bool is_loop; 9563ce6e1f6STetsuo Handa 9573ce6e1f6STetsuo Handa if (!file) 9583ce6e1f6STetsuo Handa return -EBADF; 9593ce6e1f6STetsuo Handa is_loop = is_loop_device(file); 9601da177e4SLinus Torvalds 9611da177e4SLinus Torvalds /* This is safe, since we have a reference from open(). */ 9621da177e4SLinus Torvalds __module_get(THIS_MODULE); 9631da177e4SLinus Torvalds 96433ec3e53SJan Kara /* 96533ec3e53SJan Kara * If we don't hold exclusive handle for the device, upgrade to it 96633ec3e53SJan Kara * here to avoid changing device under exclusive owner. 96733ec3e53SJan Kara */ 96833ec3e53SJan Kara if (!(mode & FMODE_EXCL)) { 96937c3fc9aSChristoph Hellwig error = bd_prepare_to_claim(bdev, loop_configure); 970ecbe6bc0SChristoph Hellwig if (error) 971757ecf40SJan Kara goto out_putf; 97233ec3e53SJan Kara } 97333ec3e53SJan Kara 9743ce6e1f6STetsuo Handa error = loop_global_lock_killable(lo, is_loop); 97533ec3e53SJan Kara if (error) 97633ec3e53SJan Kara goto out_bdev; 977757ecf40SJan Kara 9781da177e4SLinus Torvalds error = -EBUSY; 9791da177e4SLinus Torvalds if (lo->lo_state != Lo_unbound) 980757ecf40SJan Kara goto out_unlock; 9811da177e4SLinus Torvalds 982d2ac838eSTheodore Ts'o error = loop_validate_file(file, bdev); 983d2ac838eSTheodore Ts'o if (error) 984757ecf40SJan Kara goto out_unlock; 9851da177e4SLinus Torvalds 9861da177e4SLinus Torvalds mapping = file->f_mapping; 9871da177e4SLinus Torvalds inode = mapping->host; 9881da177e4SLinus Torvalds 9893448914eSMartijn Coenen if ((config->info.lo_flags & ~LOOP_CONFIGURE_SETTABLE_FLAGS) != 0) { 9903448914eSMartijn Coenen error = -EINVAL; 9913448914eSMartijn Coenen goto out_unlock; 9923448914eSMartijn Coenen } 9933448914eSMartijn Coenen 9943448914eSMartijn Coenen if (config->block_size) { 995af3c570fSXie Yongji error = blk_validate_block_size(config->block_size); 9963448914eSMartijn Coenen if (error) 9973448914eSMartijn Coenen goto out_unlock; 9983448914eSMartijn Coenen } 9993448914eSMartijn Coenen 10003448914eSMartijn Coenen error = loop_set_status_from_info(lo, &config->info); 10013448914eSMartijn Coenen if (error) 10023448914eSMartijn Coenen goto out_unlock; 10033448914eSMartijn Coenen 1004456be148SChristoph Hellwig if (!(file->f_mode & FMODE_WRITE) || !(mode & FMODE_WRITE) || 1005283e7e5dSAl Viro !file->f_op->write_iter) 10063448914eSMartijn Coenen lo->lo_flags |= LO_FLAGS_READ_ONLY; 1007083a6a50SMartijn Coenen 100887579e9bSDan Schatzberg lo->workqueue = alloc_workqueue("loop%d", 100987579e9bSDan Schatzberg WQ_UNBOUND | WQ_FREEZABLE, 101087579e9bSDan Schatzberg 0, 101187579e9bSDan Schatzberg lo->lo_number); 101287579e9bSDan Schatzberg if (!lo->workqueue) { 101387579e9bSDan Schatzberg error = -ENOMEM; 1014757ecf40SJan Kara goto out_unlock; 101587579e9bSDan Schatzberg } 10161da177e4SLinus Torvalds 10179f65c489SMatteo Croce disk_force_media_change(lo->lo_disk, DISK_EVENT_MEDIA_CHANGE); 10187a2f0ce1SChristoph Hellwig set_disk_ro(lo->lo_disk, (lo->lo_flags & LO_FLAGS_READ_ONLY) != 0); 10191da177e4SLinus Torvalds 102087579e9bSDan Schatzberg INIT_WORK(&lo->rootcg_work, loop_rootcg_workfn); 102187579e9bSDan Schatzberg INIT_LIST_HEAD(&lo->rootcg_cmd_list); 102287579e9bSDan Schatzberg INIT_LIST_HEAD(&lo->idle_worker_list); 102387579e9bSDan Schatzberg lo->worker_tree = RB_ROOT; 102487579e9bSDan Schatzberg timer_setup(&lo->timer, loop_free_idle_workers, 102587579e9bSDan Schatzberg TIMER_DEFERRABLE); 10263448914eSMartijn Coenen lo->use_dio = lo->lo_flags & LO_FLAGS_DIRECT_IO; 10271da177e4SLinus Torvalds lo->lo_device = bdev; 10281da177e4SLinus Torvalds lo->lo_backing_file = file; 10291da177e4SLinus Torvalds lo->old_gfp_mask = mapping_gfp_mask(mapping); 10301da177e4SLinus Torvalds mapping_set_gfp_mask(mapping, lo->old_gfp_mask & ~(__GFP_IO|__GFP_FS)); 10311da177e4SLinus Torvalds 10323448914eSMartijn Coenen if (!(lo->lo_flags & LO_FLAGS_READ_ONLY) && file->f_op->fsync) 103321d0727fSJens Axboe blk_queue_write_cache(lo->lo_queue, true, false); 103468db1961SNikanth Karthikesan 10353448914eSMartijn Coenen if (config->block_size) 10363448914eSMartijn Coenen bsize = config->block_size; 103796ed320dSLinus Torvalds else if ((lo->lo_backing_file->f_flags & O_DIRECT) && inode->i_sb->s_bdev) 103885560117SMartijn Coenen /* In case of direct I/O, match underlying block size */ 10393448914eSMartijn Coenen bsize = bdev_logical_block_size(inode->i_sb->s_bdev); 10403448914eSMartijn Coenen else 10413448914eSMartijn Coenen bsize = 512; 104285560117SMartijn Coenen 104385560117SMartijn Coenen blk_queue_logical_block_size(lo->lo_queue, bsize); 104485560117SMartijn Coenen blk_queue_physical_block_size(lo->lo_queue, bsize); 104585560117SMartijn Coenen blk_queue_io_min(lo->lo_queue, bsize); 104685560117SMartijn Coenen 10472b9ac22bSKristian Klausen loop_config_discard(lo); 104856a85fd8SHolger Hoffstätte loop_update_rotational(lo); 10492e5ab5f3SMing Lei loop_update_dio(lo); 1050ee862730SMilan Broz loop_sysfs_init(lo); 105179e5dc59SMartijn Coenen 105279e5dc59SMartijn Coenen size = get_loop_size(lo, file); 10535795b6f5SMartijn Coenen loop_set_size(lo, size); 10541da177e4SLinus Torvalds 10553ce6e1f6STetsuo Handa /* Order wrt reading lo_state in loop_validate_file(). */ 10563ce6e1f6STetsuo Handa wmb(); 10573ce6e1f6STetsuo Handa 10586c997918SSerge E. Hallyn lo->lo_state = Lo_bound; 1059e03c8dd1SKay Sievers if (part_shift) 1060e03c8dd1SKay Sievers lo->lo_flags |= LO_FLAGS_PARTSCAN; 106185b0a54aSJan Kara partscan = lo->lo_flags & LO_FLAGS_PARTSCAN; 1062fe6a8fc5SLennart Poettering if (partscan) 106346e7eac6SChristoph Hellwig lo->lo_disk->flags &= ~GENHD_FL_NO_PART; 1064c1681bf8SAnatol Pomozov 10653ce6e1f6STetsuo Handa loop_global_unlock(lo, is_loop); 106685b0a54aSJan Kara if (partscan) 10670384264eSChristoph Hellwig loop_reread_partitions(lo); 106837c3fc9aSChristoph Hellwig if (!(mode & FMODE_EXCL)) 106937c3fc9aSChristoph Hellwig bd_abort_claiming(bdev, loop_configure); 10701da177e4SLinus Torvalds return 0; 10711da177e4SLinus Torvalds 1072757ecf40SJan Kara out_unlock: 10733ce6e1f6STetsuo Handa loop_global_unlock(lo, is_loop); 107433ec3e53SJan Kara out_bdev: 107537c3fc9aSChristoph Hellwig if (!(mode & FMODE_EXCL)) 107637c3fc9aSChristoph Hellwig bd_abort_claiming(bdev, loop_configure); 10771da177e4SLinus Torvalds out_putf: 10781da177e4SLinus Torvalds fput(file); 10791da177e4SLinus Torvalds /* This is safe: open() is still holding a reference. */ 10801da177e4SLinus Torvalds module_put(THIS_MODULE); 10811da177e4SLinus Torvalds return error; 10821da177e4SLinus Torvalds } 10831da177e4SLinus Torvalds 1084322c4293STetsuo Handa static void __loop_clr_fd(struct loop_device *lo) 10851da177e4SLinus Torvalds { 10866050fa4cSTetsuo Handa struct file *filp; 1087b4e3ca1aSAl Viro gfp_t gfp = lo->old_gfp_mask; 108887579e9bSDan Schatzberg struct loop_worker *pos, *worker; 10891da177e4SLinus Torvalds 10903ce6e1f6STetsuo Handa /* 10913ce6e1f6STetsuo Handa * Flush loop_configure() and loop_change_fd(). It is acceptable for 10923ce6e1f6STetsuo Handa * loop_validate_file() to succeed, for actual clear operation has not 10933ce6e1f6STetsuo Handa * started yet. 10943ce6e1f6STetsuo Handa */ 10953ce6e1f6STetsuo Handa mutex_lock(&loop_validate_mutex); 10963ce6e1f6STetsuo Handa mutex_unlock(&loop_validate_mutex); 10973ce6e1f6STetsuo Handa /* 10983ce6e1f6STetsuo Handa * loop_validate_file() now fails because l->lo_state != Lo_bound 10993ce6e1f6STetsuo Handa * became visible. 11003ce6e1f6STetsuo Handa */ 11013ce6e1f6STetsuo Handa 11026050fa4cSTetsuo Handa /* 11036050fa4cSTetsuo Handa * Since this function is called upon "ioctl(LOOP_CLR_FD)" xor "close() 11046050fa4cSTetsuo Handa * after ioctl(LOOP_CLR_FD)", it is a sign of something going wrong if 11056050fa4cSTetsuo Handa * lo->lo_state has changed while waiting for lo->lo_mutex. 11066050fa4cSTetsuo Handa */ 11076cc8e743SPavel Tatashin mutex_lock(&lo->lo_mutex); 11086050fa4cSTetsuo Handa BUG_ON(lo->lo_state != Lo_rundown); 11096050fa4cSTetsuo Handa mutex_unlock(&lo->lo_mutex); 11101da177e4SLinus Torvalds 11114ceddce5SMauricio Faria de Oliveira if (test_bit(QUEUE_FLAG_WC, &lo->lo_queue->queue_flags)) 11124ceddce5SMauricio Faria de Oliveira blk_queue_write_cache(lo->lo_queue, false, false); 11134ceddce5SMauricio Faria de Oliveira 1114f8933667SMing Lei /* freeze request queue during the transition */ 1115f8933667SMing Lei blk_mq_freeze_queue(lo->lo_queue); 1116f8933667SMing Lei 111787579e9bSDan Schatzberg destroy_workqueue(lo->workqueue); 111887579e9bSDan Schatzberg spin_lock_irq(&lo->lo_work_lock); 111987579e9bSDan Schatzberg list_for_each_entry_safe(worker, pos, &lo->idle_worker_list, 112087579e9bSDan Schatzberg idle_list) { 112187579e9bSDan Schatzberg list_del(&worker->idle_list); 112287579e9bSDan Schatzberg rb_erase(&worker->rb_node, &lo->worker_tree); 1123c74d40e8SDan Schatzberg css_put(worker->blkcg_css); 112487579e9bSDan Schatzberg kfree(worker); 112587579e9bSDan Schatzberg } 112687579e9bSDan Schatzberg spin_unlock_irq(&lo->lo_work_lock); 112787579e9bSDan Schatzberg del_timer_sync(&lo->timer); 112887579e9bSDan Schatzberg 11291da177e4SLinus Torvalds spin_lock_irq(&lo->lo_lock); 11306050fa4cSTetsuo Handa filp = lo->lo_backing_file; 11311da177e4SLinus Torvalds lo->lo_backing_file = NULL; 113205eb0f25SKay Sievers spin_unlock_irq(&lo->lo_lock); 11331da177e4SLinus Torvalds 11341da177e4SLinus Torvalds lo->lo_device = NULL; 11351da177e4SLinus Torvalds lo->lo_offset = 0; 11361da177e4SLinus Torvalds lo->lo_sizelimit = 0; 11371da177e4SLinus Torvalds memset(lo->lo_file_name, 0, LO_NAME_SIZE); 113889e4fdecSOmar Sandoval blk_queue_logical_block_size(lo->lo_queue, 512); 1139bf093753SOmar Sandoval blk_queue_physical_block_size(lo->lo_queue, 512); 1140bf093753SOmar Sandoval blk_queue_io_min(lo->lo_queue, 512); 1141e515be8fSXie Yongji invalidate_disk(lo->lo_disk); 114251a0bb0cSMilan Broz loop_sysfs_exit(lo); 1143c3473c63SDavid Zeuthen /* let user-space know about this change */ 114419f553dbSXie Yongji kobject_uevent(&disk_to_dev(lo->lo_disk)->kobj, KOBJ_CHANGE); 11451da177e4SLinus Torvalds mapping_set_gfp_mask(filp->f_mapping, gfp); 1146f8933667SMing Lei blk_mq_unfreeze_queue(lo->lo_queue); 1147f8933667SMing Lei 11489f65c489SMatteo Croce disk_force_media_change(lo->lo_disk, DISK_EVENT_MEDIA_CHANGE); 11496050fa4cSTetsuo Handa 11506050fa4cSTetsuo Handa if (lo->lo_flags & LO_FLAGS_PARTSCAN) { 11516050fa4cSTetsuo Handa int err; 11526050fa4cSTetsuo Handa 11530384264eSChristoph Hellwig mutex_lock(&lo->lo_disk->open_mutex); 11540384264eSChristoph Hellwig err = bdev_disk_changed(lo->lo_disk, false); 11550384264eSChristoph Hellwig mutex_unlock(&lo->lo_disk->open_mutex); 115640853d6fSDongli Zhang if (err) 1157d57f3374SJan Kara pr_warn("%s: partition scan of loop%d failed (rc=%d)\n", 11586050fa4cSTetsuo Handa __func__, lo->lo_number, err); 1159d57f3374SJan Kara /* Device is gone, no point in returning error */ 1160d57f3374SJan Kara } 1161758a58d0SDongli Zhang 1162758a58d0SDongli Zhang lo->lo_flags = 0; 1163758a58d0SDongli Zhang if (!part_shift) 116446e7eac6SChristoph Hellwig lo->lo_disk->flags |= GENHD_FL_NO_PART; 1165322c4293STetsuo Handa 1166322c4293STetsuo Handa fput(filp); 1167322c4293STetsuo Handa } 1168322c4293STetsuo Handa 1169322c4293STetsuo Handa static void loop_rundown_completed(struct loop_device *lo) 1170322c4293STetsuo Handa { 11716050fa4cSTetsuo Handa mutex_lock(&lo->lo_mutex); 1172758a58d0SDongli Zhang lo->lo_state = Lo_unbound; 11736cc8e743SPavel Tatashin mutex_unlock(&lo->lo_mutex); 1174322c4293STetsuo Handa module_put(THIS_MODULE); 1175322c4293STetsuo Handa } 1176758a58d0SDongli Zhang 1177322c4293STetsuo Handa static void loop_rundown_workfn(struct work_struct *work) 1178322c4293STetsuo Handa { 1179322c4293STetsuo Handa struct loop_device *lo = container_of(work, struct loop_device, 1180322c4293STetsuo Handa rundown_work); 1181322c4293STetsuo Handa struct block_device *bdev = lo->lo_device; 1182322c4293STetsuo Handa struct gendisk *disk = lo->lo_disk; 1183322c4293STetsuo Handa 1184322c4293STetsuo Handa __loop_clr_fd(lo); 1185322c4293STetsuo Handa kobject_put(&bdev->bd_device.kobj); 1186322c4293STetsuo Handa module_put(disk->fops->owner); 1187322c4293STetsuo Handa loop_rundown_completed(lo); 1188322c4293STetsuo Handa } 1189322c4293STetsuo Handa 1190322c4293STetsuo Handa static void loop_schedule_rundown(struct loop_device *lo) 1191322c4293STetsuo Handa { 1192322c4293STetsuo Handa struct block_device *bdev = lo->lo_device; 1193322c4293STetsuo Handa struct gendisk *disk = lo->lo_disk; 1194322c4293STetsuo Handa 1195322c4293STetsuo Handa __module_get(disk->fops->owner); 1196322c4293STetsuo Handa kobject_get(&bdev->bd_device.kobj); 1197322c4293STetsuo Handa INIT_WORK(&lo->rundown_work, loop_rundown_workfn); 1198322c4293STetsuo Handa queue_work(system_long_wq, &lo->rundown_work); 11991da177e4SLinus Torvalds } 12001da177e4SLinus Torvalds 1201a2505b79SJan Kara static int loop_clr_fd(struct loop_device *lo) 1202a2505b79SJan Kara { 12037ccd0791SJan Kara int err; 12047ccd0791SJan Kara 12056cc8e743SPavel Tatashin err = mutex_lock_killable(&lo->lo_mutex); 12067ccd0791SJan Kara if (err) 12077ccd0791SJan Kara return err; 12087ccd0791SJan Kara if (lo->lo_state != Lo_bound) { 12096cc8e743SPavel Tatashin mutex_unlock(&lo->lo_mutex); 1210a2505b79SJan Kara return -ENXIO; 12117ccd0791SJan Kara } 1212a2505b79SJan Kara /* 1213a2505b79SJan Kara * If we've explicitly asked to tear down the loop device, 1214a2505b79SJan Kara * and it has an elevated reference count, set it for auto-teardown when 1215a2505b79SJan Kara * the last reference goes away. This stops $!~#$@ udev from 1216a2505b79SJan Kara * preventing teardown because it decided that it needs to run blkid on 1217a2505b79SJan Kara * the loopback device whenever they appear. xfstests is notorious for 1218a2505b79SJan Kara * failing tests because blkid via udev races with a losetup 1219a2505b79SJan Kara * <dev>/do something like mkfs/losetup -d <dev> causing the losetup -d 1220a2505b79SJan Kara * command to fail with EBUSY. 1221a2505b79SJan Kara */ 1222a2505b79SJan Kara if (atomic_read(&lo->lo_refcnt) > 1) { 1223a2505b79SJan Kara lo->lo_flags |= LO_FLAGS_AUTOCLEAR; 12246cc8e743SPavel Tatashin mutex_unlock(&lo->lo_mutex); 1225a2505b79SJan Kara return 0; 1226a2505b79SJan Kara } 1227a2505b79SJan Kara lo->lo_state = Lo_rundown; 12286cc8e743SPavel Tatashin mutex_unlock(&lo->lo_mutex); 1229a2505b79SJan Kara 1230322c4293STetsuo Handa __loop_clr_fd(lo); 1231322c4293STetsuo Handa loop_rundown_completed(lo); 12326050fa4cSTetsuo Handa return 0; 1233a2505b79SJan Kara } 1234a2505b79SJan Kara 12351da177e4SLinus Torvalds static int 12361da177e4SLinus Torvalds loop_set_status(struct loop_device *lo, const struct loop_info64 *info) 12371da177e4SLinus Torvalds { 12381da177e4SLinus Torvalds int err; 1239faf1d254SMartijn Coenen int prev_lo_flags; 124085b0a54aSJan Kara bool partscan = false; 12410c3796c2SMartijn Coenen bool size_changed = false; 12421da177e4SLinus Torvalds 12436cc8e743SPavel Tatashin err = mutex_lock_killable(&lo->lo_mutex); 1244550df5fdSJan Kara if (err) 1245550df5fdSJan Kara return err; 1246550df5fdSJan Kara if (lo->lo_state != Lo_bound) { 1247550df5fdSJan Kara err = -ENXIO; 1248550df5fdSJan Kara goto out_unlock; 1249550df5fdSJan Kara } 12501da177e4SLinus Torvalds 12515db470e2SJaegeuk Kim if (lo->lo_offset != info->lo_offset || 12525db470e2SJaegeuk Kim lo->lo_sizelimit != info->lo_sizelimit) { 12530c3796c2SMartijn Coenen size_changed = true; 12545db470e2SJaegeuk Kim sync_blockdev(lo->lo_device); 1255f4bd34b1SZheng Bin invalidate_bdev(lo->lo_device); 12565db470e2SJaegeuk Kim } 12575db470e2SJaegeuk Kim 1258ecdd0959SMing Lei /* I/O need to be drained during transfer transition */ 1259ecdd0959SMing Lei blk_mq_freeze_queue(lo->lo_queue); 1260ecdd0959SMing Lei 12610c3796c2SMartijn Coenen if (size_changed && lo->lo_device->bd_inode->i_mapping->nrpages) { 1262f4bd34b1SZheng Bin /* If any pages were dirtied after invalidate_bdev(), try again */ 12635db470e2SJaegeuk Kim err = -EAGAIN; 1264d9a74051SColin Ian King pr_warn("%s: loop%d (%s) still has dirty pages (nrpages=%lu)\n", 12655db470e2SJaegeuk Kim __func__, lo->lo_number, lo->lo_file_name, 12665db470e2SJaegeuk Kim lo->lo_device->bd_inode->i_mapping->nrpages); 12675db470e2SJaegeuk Kim goto out_unfreeze; 12685db470e2SJaegeuk Kim } 12690c3796c2SMartijn Coenen 1270faf1d254SMartijn Coenen prev_lo_flags = lo->lo_flags; 1271faf1d254SMartijn Coenen 12720c3796c2SMartijn Coenen err = loop_set_status_from_info(lo, info); 12730c3796c2SMartijn Coenen if (err) 1274550df5fdSJan Kara goto out_unfreeze; 12750c3796c2SMartijn Coenen 1276faf1d254SMartijn Coenen /* Mask out flags that can't be set using LOOP_SET_STATUS. */ 12776ac92fb5SMartijn Coenen lo->lo_flags &= LOOP_SET_STATUS_SETTABLE_FLAGS; 1278faf1d254SMartijn Coenen /* For those flags, use the previous values instead */ 1279faf1d254SMartijn Coenen lo->lo_flags |= prev_lo_flags & ~LOOP_SET_STATUS_SETTABLE_FLAGS; 1280faf1d254SMartijn Coenen /* For flags that can't be cleared, use previous values too */ 1281faf1d254SMartijn Coenen lo->lo_flags |= prev_lo_flags & ~LOOP_SET_STATUS_CLEARABLE_FLAGS; 1282faf1d254SMartijn Coenen 1283b0bd158dSMartijn Coenen if (size_changed) { 1284b0bd158dSMartijn Coenen loff_t new_size = get_size(lo->lo_offset, lo->lo_sizelimit, 1285b0bd158dSMartijn Coenen lo->lo_backing_file); 1286b0bd158dSMartijn Coenen loop_set_size(lo, new_size); 1287b040ad9cSArnd Bergmann } 1288541c742aSGuo Chao 1289dfaa2ef6SLukas Czerner loop_config_discard(lo); 12901da177e4SLinus Torvalds 12912e5ab5f3SMing Lei /* update dio if lo_offset or transfer is changed */ 12922e5ab5f3SMing Lei __loop_update_dio(lo, lo->use_dio); 12932e5ab5f3SMing Lei 1294550df5fdSJan Kara out_unfreeze: 1295ecdd0959SMing Lei blk_mq_unfreeze_queue(lo->lo_queue); 1296e02898b4SOmar Sandoval 1297faf1d254SMartijn Coenen if (!err && (lo->lo_flags & LO_FLAGS_PARTSCAN) && 1298faf1d254SMartijn Coenen !(prev_lo_flags & LO_FLAGS_PARTSCAN)) { 129946e7eac6SChristoph Hellwig lo->lo_disk->flags &= ~GENHD_FL_NO_PART; 130085b0a54aSJan Kara partscan = true; 1301e02898b4SOmar Sandoval } 1302550df5fdSJan Kara out_unlock: 13036cc8e743SPavel Tatashin mutex_unlock(&lo->lo_mutex); 130485b0a54aSJan Kara if (partscan) 13050384264eSChristoph Hellwig loop_reread_partitions(lo); 1306e02898b4SOmar Sandoval 1307ecdd0959SMing Lei return err; 13081da177e4SLinus Torvalds } 13091da177e4SLinus Torvalds 13101da177e4SLinus Torvalds static int 13111da177e4SLinus Torvalds loop_get_status(struct loop_device *lo, struct loop_info64 *info) 13121da177e4SLinus Torvalds { 1313b1ab5fa3STetsuo Handa struct path path; 13141da177e4SLinus Torvalds struct kstat stat; 13152d1d4c1eSOmar Sandoval int ret; 13161da177e4SLinus Torvalds 13176cc8e743SPavel Tatashin ret = mutex_lock_killable(&lo->lo_mutex); 13184a5ce9baSJan Kara if (ret) 13194a5ce9baSJan Kara return ret; 13202d1d4c1eSOmar Sandoval if (lo->lo_state != Lo_bound) { 13216cc8e743SPavel Tatashin mutex_unlock(&lo->lo_mutex); 13221da177e4SLinus Torvalds return -ENXIO; 13232d1d4c1eSOmar Sandoval } 13242d1d4c1eSOmar Sandoval 13251da177e4SLinus Torvalds memset(info, 0, sizeof(*info)); 13261da177e4SLinus Torvalds info->lo_number = lo->lo_number; 13271da177e4SLinus Torvalds info->lo_offset = lo->lo_offset; 13281da177e4SLinus Torvalds info->lo_sizelimit = lo->lo_sizelimit; 13291da177e4SLinus Torvalds info->lo_flags = lo->lo_flags; 13301da177e4SLinus Torvalds memcpy(info->lo_file_name, lo->lo_file_name, LO_NAME_SIZE); 13312d1d4c1eSOmar Sandoval 13326cc8e743SPavel Tatashin /* Drop lo_mutex while we call into the filesystem. */ 1333b1ab5fa3STetsuo Handa path = lo->lo_backing_file->f_path; 1334b1ab5fa3STetsuo Handa path_get(&path); 13356cc8e743SPavel Tatashin mutex_unlock(&lo->lo_mutex); 1336b1ab5fa3STetsuo Handa ret = vfs_getattr(&path, &stat, STATX_INO, AT_STATX_SYNC_AS_STAT); 13372d1d4c1eSOmar Sandoval if (!ret) { 13382d1d4c1eSOmar Sandoval info->lo_device = huge_encode_dev(stat.dev); 13392d1d4c1eSOmar Sandoval info->lo_inode = stat.ino; 13402d1d4c1eSOmar Sandoval info->lo_rdevice = huge_encode_dev(stat.rdev); 13412d1d4c1eSOmar Sandoval } 1342b1ab5fa3STetsuo Handa path_put(&path); 13432d1d4c1eSOmar Sandoval return ret; 13441da177e4SLinus Torvalds } 13451da177e4SLinus Torvalds 13461da177e4SLinus Torvalds static void 13471da177e4SLinus Torvalds loop_info64_from_old(const struct loop_info *info, struct loop_info64 *info64) 13481da177e4SLinus Torvalds { 13491da177e4SLinus Torvalds memset(info64, 0, sizeof(*info64)); 13501da177e4SLinus Torvalds info64->lo_number = info->lo_number; 13511da177e4SLinus Torvalds info64->lo_device = info->lo_device; 13521da177e4SLinus Torvalds info64->lo_inode = info->lo_inode; 13531da177e4SLinus Torvalds info64->lo_rdevice = info->lo_rdevice; 13541da177e4SLinus Torvalds info64->lo_offset = info->lo_offset; 13551da177e4SLinus Torvalds info64->lo_sizelimit = 0; 13561da177e4SLinus Torvalds info64->lo_flags = info->lo_flags; 13571da177e4SLinus Torvalds memcpy(info64->lo_file_name, info->lo_name, LO_NAME_SIZE); 13581da177e4SLinus Torvalds } 13591da177e4SLinus Torvalds 13601da177e4SLinus Torvalds static int 13611da177e4SLinus Torvalds loop_info64_to_old(const struct loop_info64 *info64, struct loop_info *info) 13621da177e4SLinus Torvalds { 13631da177e4SLinus Torvalds memset(info, 0, sizeof(*info)); 13641da177e4SLinus Torvalds info->lo_number = info64->lo_number; 13651da177e4SLinus Torvalds info->lo_device = info64->lo_device; 13661da177e4SLinus Torvalds info->lo_inode = info64->lo_inode; 13671da177e4SLinus Torvalds info->lo_rdevice = info64->lo_rdevice; 13681da177e4SLinus Torvalds info->lo_offset = info64->lo_offset; 13691da177e4SLinus Torvalds info->lo_flags = info64->lo_flags; 13701da177e4SLinus Torvalds memcpy(info->lo_name, info64->lo_file_name, LO_NAME_SIZE); 13711da177e4SLinus Torvalds 13721da177e4SLinus Torvalds /* error in case values were truncated */ 13731da177e4SLinus Torvalds if (info->lo_device != info64->lo_device || 13741da177e4SLinus Torvalds info->lo_rdevice != info64->lo_rdevice || 13751da177e4SLinus Torvalds info->lo_inode != info64->lo_inode || 13761da177e4SLinus Torvalds info->lo_offset != info64->lo_offset) 13771da177e4SLinus Torvalds return -EOVERFLOW; 13781da177e4SLinus Torvalds 13791da177e4SLinus Torvalds return 0; 13801da177e4SLinus Torvalds } 13811da177e4SLinus Torvalds 13821da177e4SLinus Torvalds static int 13831da177e4SLinus Torvalds loop_set_status_old(struct loop_device *lo, const struct loop_info __user *arg) 13841da177e4SLinus Torvalds { 13851da177e4SLinus Torvalds struct loop_info info; 13861da177e4SLinus Torvalds struct loop_info64 info64; 13871da177e4SLinus Torvalds 13881da177e4SLinus Torvalds if (copy_from_user(&info, arg, sizeof (struct loop_info))) 13891da177e4SLinus Torvalds return -EFAULT; 13901da177e4SLinus Torvalds loop_info64_from_old(&info, &info64); 13911da177e4SLinus Torvalds return loop_set_status(lo, &info64); 13921da177e4SLinus Torvalds } 13931da177e4SLinus Torvalds 13941da177e4SLinus Torvalds static int 13951da177e4SLinus Torvalds loop_set_status64(struct loop_device *lo, const struct loop_info64 __user *arg) 13961da177e4SLinus Torvalds { 13971da177e4SLinus Torvalds struct loop_info64 info64; 13981da177e4SLinus Torvalds 13991da177e4SLinus Torvalds if (copy_from_user(&info64, arg, sizeof (struct loop_info64))) 14001da177e4SLinus Torvalds return -EFAULT; 14011da177e4SLinus Torvalds return loop_set_status(lo, &info64); 14021da177e4SLinus Torvalds } 14031da177e4SLinus Torvalds 14041da177e4SLinus Torvalds static int 14051da177e4SLinus Torvalds loop_get_status_old(struct loop_device *lo, struct loop_info __user *arg) { 14061da177e4SLinus Torvalds struct loop_info info; 14071da177e4SLinus Torvalds struct loop_info64 info64; 1408bdac616dSOmar Sandoval int err; 14091da177e4SLinus Torvalds 14104a5ce9baSJan Kara if (!arg) 1411bdac616dSOmar Sandoval return -EINVAL; 14121da177e4SLinus Torvalds err = loop_get_status(lo, &info64); 14131da177e4SLinus Torvalds if (!err) 14141da177e4SLinus Torvalds err = loop_info64_to_old(&info64, &info); 14151da177e4SLinus Torvalds if (!err && copy_to_user(arg, &info, sizeof(info))) 14161da177e4SLinus Torvalds err = -EFAULT; 14171da177e4SLinus Torvalds 14181da177e4SLinus Torvalds return err; 14191da177e4SLinus Torvalds } 14201da177e4SLinus Torvalds 14211da177e4SLinus Torvalds static int 14221da177e4SLinus Torvalds loop_get_status64(struct loop_device *lo, struct loop_info64 __user *arg) { 14231da177e4SLinus Torvalds struct loop_info64 info64; 1424bdac616dSOmar Sandoval int err; 14251da177e4SLinus Torvalds 14264a5ce9baSJan Kara if (!arg) 1427bdac616dSOmar Sandoval return -EINVAL; 14281da177e4SLinus Torvalds err = loop_get_status(lo, &info64); 14291da177e4SLinus Torvalds if (!err && copy_to_user(arg, &info64, sizeof(info64))) 14301da177e4SLinus Torvalds err = -EFAULT; 14311da177e4SLinus Torvalds 14321da177e4SLinus Torvalds return err; 14331da177e4SLinus Torvalds } 14341da177e4SLinus Torvalds 143551001b7dSHannes Reinecke static int loop_set_capacity(struct loop_device *lo) 143653d66608SJ. R. Okajima { 14370a6ed1b5SMartijn Coenen loff_t size; 14380a6ed1b5SMartijn Coenen 143953d66608SJ. R. Okajima if (unlikely(lo->lo_state != Lo_bound)) 14407b0576a3SGuo Chao return -ENXIO; 144153d66608SJ. R. Okajima 14420a6ed1b5SMartijn Coenen size = get_loop_size(lo, lo->lo_backing_file); 14430a6ed1b5SMartijn Coenen loop_set_size(lo, size); 1444083a6a50SMartijn Coenen 1445083a6a50SMartijn Coenen return 0; 144653d66608SJ. R. Okajima } 144753d66608SJ. R. Okajima 1448ab1cb278SMing Lei static int loop_set_dio(struct loop_device *lo, unsigned long arg) 1449ab1cb278SMing Lei { 1450ab1cb278SMing Lei int error = -ENXIO; 1451ab1cb278SMing Lei if (lo->lo_state != Lo_bound) 1452ab1cb278SMing Lei goto out; 1453ab1cb278SMing Lei 1454ab1cb278SMing Lei __loop_update_dio(lo, !!arg); 1455ab1cb278SMing Lei if (lo->use_dio == !!arg) 1456ab1cb278SMing Lei return 0; 1457ab1cb278SMing Lei error = -EINVAL; 1458ab1cb278SMing Lei out: 1459ab1cb278SMing Lei return error; 1460ab1cb278SMing Lei } 1461ab1cb278SMing Lei 146289e4fdecSOmar Sandoval static int loop_set_block_size(struct loop_device *lo, unsigned long arg) 146389e4fdecSOmar Sandoval { 14645db470e2SJaegeuk Kim int err = 0; 14655db470e2SJaegeuk Kim 146689e4fdecSOmar Sandoval if (lo->lo_state != Lo_bound) 146789e4fdecSOmar Sandoval return -ENXIO; 146889e4fdecSOmar Sandoval 1469af3c570fSXie Yongji err = blk_validate_block_size(arg); 14703448914eSMartijn Coenen if (err) 14713448914eSMartijn Coenen return err; 147289e4fdecSOmar Sandoval 14737e81f99aSMartijn Coenen if (lo->lo_queue->limits.logical_block_size == arg) 14747e81f99aSMartijn Coenen return 0; 14757e81f99aSMartijn Coenen 14765db470e2SJaegeuk Kim sync_blockdev(lo->lo_device); 1477f4bd34b1SZheng Bin invalidate_bdev(lo->lo_device); 14785db470e2SJaegeuk Kim 147989e4fdecSOmar Sandoval blk_mq_freeze_queue(lo->lo_queue); 148089e4fdecSOmar Sandoval 1481f4bd34b1SZheng Bin /* invalidate_bdev should have truncated all the pages */ 14827e81f99aSMartijn Coenen if (lo->lo_device->bd_inode->i_mapping->nrpages) { 14835db470e2SJaegeuk Kim err = -EAGAIN; 1484d9a74051SColin Ian King pr_warn("%s: loop%d (%s) still has dirty pages (nrpages=%lu)\n", 14855db470e2SJaegeuk Kim __func__, lo->lo_number, lo->lo_file_name, 14865db470e2SJaegeuk Kim lo->lo_device->bd_inode->i_mapping->nrpages); 14875db470e2SJaegeuk Kim goto out_unfreeze; 14885db470e2SJaegeuk Kim } 14895db470e2SJaegeuk Kim 149089e4fdecSOmar Sandoval blk_queue_logical_block_size(lo->lo_queue, arg); 1491bf093753SOmar Sandoval blk_queue_physical_block_size(lo->lo_queue, arg); 1492bf093753SOmar Sandoval blk_queue_io_min(lo->lo_queue, arg); 149389e4fdecSOmar Sandoval loop_update_dio(lo); 14945db470e2SJaegeuk Kim out_unfreeze: 149589e4fdecSOmar Sandoval blk_mq_unfreeze_queue(lo->lo_queue); 149689e4fdecSOmar Sandoval 14975db470e2SJaegeuk Kim return err; 149889e4fdecSOmar Sandoval } 149989e4fdecSOmar Sandoval 1500a1316544SJan Kara static int lo_simple_ioctl(struct loop_device *lo, unsigned int cmd, 1501a1316544SJan Kara unsigned long arg) 15021da177e4SLinus Torvalds { 15031da177e4SLinus Torvalds int err; 15041da177e4SLinus Torvalds 15056cc8e743SPavel Tatashin err = mutex_lock_killable(&lo->lo_mutex); 15063148ffbdSOmar Sandoval if (err) 1507a1316544SJan Kara return err; 15081da177e4SLinus Torvalds switch (cmd) { 150953d66608SJ. R. Okajima case LOOP_SET_CAPACITY: 151051001b7dSHannes Reinecke err = loop_set_capacity(lo); 151153d66608SJ. R. Okajima break; 1512ab1cb278SMing Lei case LOOP_SET_DIRECT_IO: 1513ab1cb278SMing Lei err = loop_set_dio(lo, arg); 1514ab1cb278SMing Lei break; 151589e4fdecSOmar Sandoval case LOOP_SET_BLOCK_SIZE: 151689e4fdecSOmar Sandoval err = loop_set_block_size(lo, arg); 151789e4fdecSOmar Sandoval break; 15181da177e4SLinus Torvalds default: 151947e96246SChristoph Hellwig err = -EINVAL; 15201da177e4SLinus Torvalds } 15216cc8e743SPavel Tatashin mutex_unlock(&lo->lo_mutex); 1522a1316544SJan Kara return err; 1523a1316544SJan Kara } 1524f028f3b2SNikanth Karthikesan 1525a1316544SJan Kara static int lo_ioctl(struct block_device *bdev, fmode_t mode, 1526a1316544SJan Kara unsigned int cmd, unsigned long arg) 1527a1316544SJan Kara { 1528a1316544SJan Kara struct loop_device *lo = bdev->bd_disk->private_data; 1529571fae6eSMartijn Coenen void __user *argp = (void __user *) arg; 1530a1316544SJan Kara int err; 1531a1316544SJan Kara 1532a1316544SJan Kara switch (cmd) { 15333448914eSMartijn Coenen case LOOP_SET_FD: { 15343448914eSMartijn Coenen /* 15353448914eSMartijn Coenen * Legacy case - pass in a zeroed out struct loop_config with 15363448914eSMartijn Coenen * only the file descriptor set , which corresponds with the 15373448914eSMartijn Coenen * default parameters we'd have used otherwise. 15383448914eSMartijn Coenen */ 15393448914eSMartijn Coenen struct loop_config config; 15403448914eSMartijn Coenen 15413448914eSMartijn Coenen memset(&config, 0, sizeof(config)); 15423448914eSMartijn Coenen config.fd = arg; 15433448914eSMartijn Coenen 15443448914eSMartijn Coenen return loop_configure(lo, mode, bdev, &config); 15453448914eSMartijn Coenen } 15463448914eSMartijn Coenen case LOOP_CONFIGURE: { 15473448914eSMartijn Coenen struct loop_config config; 15483448914eSMartijn Coenen 15493448914eSMartijn Coenen if (copy_from_user(&config, argp, sizeof(config))) 15503448914eSMartijn Coenen return -EFAULT; 15513448914eSMartijn Coenen 15523448914eSMartijn Coenen return loop_configure(lo, mode, bdev, &config); 15533448914eSMartijn Coenen } 1554a1316544SJan Kara case LOOP_CHANGE_FD: 1555c3710770SJan Kara return loop_change_fd(lo, bdev, arg); 1556a1316544SJan Kara case LOOP_CLR_FD: 15577ccd0791SJan Kara return loop_clr_fd(lo); 1558a1316544SJan Kara case LOOP_SET_STATUS: 1559a1316544SJan Kara err = -EPERM; 1560a1316544SJan Kara if ((mode & FMODE_WRITE) || capable(CAP_SYS_ADMIN)) { 1561571fae6eSMartijn Coenen err = loop_set_status_old(lo, argp); 1562a1316544SJan Kara } 1563a1316544SJan Kara break; 1564a1316544SJan Kara case LOOP_GET_STATUS: 1565571fae6eSMartijn Coenen return loop_get_status_old(lo, argp); 1566a1316544SJan Kara case LOOP_SET_STATUS64: 1567a1316544SJan Kara err = -EPERM; 1568a1316544SJan Kara if ((mode & FMODE_WRITE) || capable(CAP_SYS_ADMIN)) { 1569571fae6eSMartijn Coenen err = loop_set_status64(lo, argp); 1570a1316544SJan Kara } 1571a1316544SJan Kara break; 1572a1316544SJan Kara case LOOP_GET_STATUS64: 1573571fae6eSMartijn Coenen return loop_get_status64(lo, argp); 1574a1316544SJan Kara case LOOP_SET_CAPACITY: 1575a1316544SJan Kara case LOOP_SET_DIRECT_IO: 1576a1316544SJan Kara case LOOP_SET_BLOCK_SIZE: 1577a1316544SJan Kara if (!(mode & FMODE_WRITE) && !capable(CAP_SYS_ADMIN)) 1578a1316544SJan Kara return -EPERM; 1579df561f66SGustavo A. R. Silva fallthrough; 1580a1316544SJan Kara default: 1581a1316544SJan Kara err = lo_simple_ioctl(lo, cmd, arg); 1582a1316544SJan Kara break; 1583a1316544SJan Kara } 1584a1316544SJan Kara 15851da177e4SLinus Torvalds return err; 15861da177e4SLinus Torvalds } 15871da177e4SLinus Torvalds 1588863d5b82SDavid Howells #ifdef CONFIG_COMPAT 1589863d5b82SDavid Howells struct compat_loop_info { 1590863d5b82SDavid Howells compat_int_t lo_number; /* ioctl r/o */ 1591863d5b82SDavid Howells compat_dev_t lo_device; /* ioctl r/o */ 1592863d5b82SDavid Howells compat_ulong_t lo_inode; /* ioctl r/o */ 1593863d5b82SDavid Howells compat_dev_t lo_rdevice; /* ioctl r/o */ 1594863d5b82SDavid Howells compat_int_t lo_offset; 1595863d5b82SDavid Howells compat_int_t lo_encrypt_key_size; /* ioctl w/o */ 1596863d5b82SDavid Howells compat_int_t lo_flags; /* ioctl r/o */ 1597863d5b82SDavid Howells char lo_name[LO_NAME_SIZE]; 1598863d5b82SDavid Howells unsigned char lo_encrypt_key[LO_KEY_SIZE]; /* ioctl w/o */ 1599863d5b82SDavid Howells compat_ulong_t lo_init[2]; 1600863d5b82SDavid Howells char reserved[4]; 1601863d5b82SDavid Howells }; 1602863d5b82SDavid Howells 1603863d5b82SDavid Howells /* 1604863d5b82SDavid Howells * Transfer 32-bit compatibility structure in userspace to 64-bit loop info 1605863d5b82SDavid Howells * - noinlined to reduce stack space usage in main part of driver 1606863d5b82SDavid Howells */ 1607863d5b82SDavid Howells static noinline int 1608ba674cfcSAl Viro loop_info64_from_compat(const struct compat_loop_info __user *arg, 1609863d5b82SDavid Howells struct loop_info64 *info64) 1610863d5b82SDavid Howells { 1611863d5b82SDavid Howells struct compat_loop_info info; 1612863d5b82SDavid Howells 1613863d5b82SDavid Howells if (copy_from_user(&info, arg, sizeof(info))) 1614863d5b82SDavid Howells return -EFAULT; 1615863d5b82SDavid Howells 1616863d5b82SDavid Howells memset(info64, 0, sizeof(*info64)); 1617863d5b82SDavid Howells info64->lo_number = info.lo_number; 1618863d5b82SDavid Howells info64->lo_device = info.lo_device; 1619863d5b82SDavid Howells info64->lo_inode = info.lo_inode; 1620863d5b82SDavid Howells info64->lo_rdevice = info.lo_rdevice; 1621863d5b82SDavid Howells info64->lo_offset = info.lo_offset; 1622863d5b82SDavid Howells info64->lo_sizelimit = 0; 1623863d5b82SDavid Howells info64->lo_flags = info.lo_flags; 1624863d5b82SDavid Howells memcpy(info64->lo_file_name, info.lo_name, LO_NAME_SIZE); 1625863d5b82SDavid Howells return 0; 1626863d5b82SDavid Howells } 1627863d5b82SDavid Howells 1628863d5b82SDavid Howells /* 1629863d5b82SDavid Howells * Transfer 64-bit loop info to 32-bit compatibility structure in userspace 1630863d5b82SDavid Howells * - noinlined to reduce stack space usage in main part of driver 1631863d5b82SDavid Howells */ 1632863d5b82SDavid Howells static noinline int 1633863d5b82SDavid Howells loop_info64_to_compat(const struct loop_info64 *info64, 1634863d5b82SDavid Howells struct compat_loop_info __user *arg) 1635863d5b82SDavid Howells { 1636863d5b82SDavid Howells struct compat_loop_info info; 1637863d5b82SDavid Howells 1638863d5b82SDavid Howells memset(&info, 0, sizeof(info)); 1639863d5b82SDavid Howells info.lo_number = info64->lo_number; 1640863d5b82SDavid Howells info.lo_device = info64->lo_device; 1641863d5b82SDavid Howells info.lo_inode = info64->lo_inode; 1642863d5b82SDavid Howells info.lo_rdevice = info64->lo_rdevice; 1643863d5b82SDavid Howells info.lo_offset = info64->lo_offset; 1644863d5b82SDavid Howells info.lo_flags = info64->lo_flags; 1645863d5b82SDavid Howells memcpy(info.lo_name, info64->lo_file_name, LO_NAME_SIZE); 1646863d5b82SDavid Howells 1647863d5b82SDavid Howells /* error in case values were truncated */ 1648863d5b82SDavid Howells if (info.lo_device != info64->lo_device || 1649863d5b82SDavid Howells info.lo_rdevice != info64->lo_rdevice || 1650863d5b82SDavid Howells info.lo_inode != info64->lo_inode || 165147e96246SChristoph Hellwig info.lo_offset != info64->lo_offset) 1652863d5b82SDavid Howells return -EOVERFLOW; 1653863d5b82SDavid Howells 1654863d5b82SDavid Howells if (copy_to_user(arg, &info, sizeof(info))) 1655863d5b82SDavid Howells return -EFAULT; 1656863d5b82SDavid Howells return 0; 1657863d5b82SDavid Howells } 1658863d5b82SDavid Howells 1659863d5b82SDavid Howells static int 1660863d5b82SDavid Howells loop_set_status_compat(struct loop_device *lo, 1661863d5b82SDavid Howells const struct compat_loop_info __user *arg) 1662863d5b82SDavid Howells { 1663863d5b82SDavid Howells struct loop_info64 info64; 1664863d5b82SDavid Howells int ret; 1665863d5b82SDavid Howells 1666863d5b82SDavid Howells ret = loop_info64_from_compat(arg, &info64); 1667863d5b82SDavid Howells if (ret < 0) 1668863d5b82SDavid Howells return ret; 1669863d5b82SDavid Howells return loop_set_status(lo, &info64); 1670863d5b82SDavid Howells } 1671863d5b82SDavid Howells 1672863d5b82SDavid Howells static int 1673863d5b82SDavid Howells loop_get_status_compat(struct loop_device *lo, 1674863d5b82SDavid Howells struct compat_loop_info __user *arg) 1675863d5b82SDavid Howells { 1676863d5b82SDavid Howells struct loop_info64 info64; 1677bdac616dSOmar Sandoval int err; 1678863d5b82SDavid Howells 16794a5ce9baSJan Kara if (!arg) 1680bdac616dSOmar Sandoval return -EINVAL; 1681863d5b82SDavid Howells err = loop_get_status(lo, &info64); 1682863d5b82SDavid Howells if (!err) 1683863d5b82SDavid Howells err = loop_info64_to_compat(&info64, arg); 1684863d5b82SDavid Howells return err; 1685863d5b82SDavid Howells } 1686863d5b82SDavid Howells 1687bb214884SAl Viro static int lo_compat_ioctl(struct block_device *bdev, fmode_t mode, 1688bb214884SAl Viro unsigned int cmd, unsigned long arg) 1689863d5b82SDavid Howells { 1690bb214884SAl Viro struct loop_device *lo = bdev->bd_disk->private_data; 1691863d5b82SDavid Howells int err; 1692863d5b82SDavid Howells 1693863d5b82SDavid Howells switch(cmd) { 1694863d5b82SDavid Howells case LOOP_SET_STATUS: 16953148ffbdSOmar Sandoval err = loop_set_status_compat(lo, 16963148ffbdSOmar Sandoval (const struct compat_loop_info __user *)arg); 1697863d5b82SDavid Howells break; 1698863d5b82SDavid Howells case LOOP_GET_STATUS: 16993148ffbdSOmar Sandoval err = loop_get_status_compat(lo, 17003148ffbdSOmar Sandoval (struct compat_loop_info __user *)arg); 1701863d5b82SDavid Howells break; 170253d66608SJ. R. Okajima case LOOP_SET_CAPACITY: 1703863d5b82SDavid Howells case LOOP_CLR_FD: 1704863d5b82SDavid Howells case LOOP_GET_STATUS64: 1705863d5b82SDavid Howells case LOOP_SET_STATUS64: 17063448914eSMartijn Coenen case LOOP_CONFIGURE: 1707863d5b82SDavid Howells arg = (unsigned long) compat_ptr(arg); 1708df561f66SGustavo A. R. Silva fallthrough; 1709863d5b82SDavid Howells case LOOP_SET_FD: 1710863d5b82SDavid Howells case LOOP_CHANGE_FD: 17119fea4b39SEvan Green case LOOP_SET_BLOCK_SIZE: 1712fdbe4eeeSAlessio Balsini case LOOP_SET_DIRECT_IO: 1713bb214884SAl Viro err = lo_ioctl(bdev, mode, cmd, arg); 1714863d5b82SDavid Howells break; 1715863d5b82SDavid Howells default: 1716863d5b82SDavid Howells err = -ENOIOCTLCMD; 1717863d5b82SDavid Howells break; 1718863d5b82SDavid Howells } 1719863d5b82SDavid Howells return err; 1720863d5b82SDavid Howells } 1721863d5b82SDavid Howells #endif 1722863d5b82SDavid Howells 1723bb214884SAl Viro static int lo_open(struct block_device *bdev, fmode_t mode) 17241da177e4SLinus Torvalds { 1725990e7811SChristoph Hellwig struct loop_device *lo = bdev->bd_disk->private_data; 17260a42e99bSJan Kara int err; 1727770fe30aSKay Sievers 17286cc8e743SPavel Tatashin err = mutex_lock_killable(&lo->lo_mutex); 17296cc8e743SPavel Tatashin if (err) 1730770fe30aSKay Sievers return err; 1731990e7811SChristoph Hellwig if (lo->lo_state == Lo_deleting) 1732990e7811SChristoph Hellwig err = -ENXIO; 1733990e7811SChristoph Hellwig else 17346cc8e743SPavel Tatashin atomic_inc(&lo->lo_refcnt); 17356cc8e743SPavel Tatashin mutex_unlock(&lo->lo_mutex); 1736990e7811SChristoph Hellwig return err; 17371da177e4SLinus Torvalds } 17381da177e4SLinus Torvalds 1739967d1dc1SJan Kara static void lo_release(struct gendisk *disk, fmode_t mode) 17401da177e4SLinus Torvalds { 17416cc8e743SPavel Tatashin struct loop_device *lo = disk->private_data; 17421da177e4SLinus Torvalds 17436cc8e743SPavel Tatashin mutex_lock(&lo->lo_mutex); 1744f8933667SMing Lei if (atomic_dec_return(&lo->lo_refcnt)) 17450a42e99bSJan Kara goto out_unlock; 1746f8933667SMing Lei 174714f27939SMilan Broz if (lo->lo_flags & LO_FLAGS_AUTOCLEAR) { 1748a2505b79SJan Kara if (lo->lo_state != Lo_bound) 1749a2505b79SJan Kara goto out_unlock; 1750a2505b79SJan Kara lo->lo_state = Lo_rundown; 17516cc8e743SPavel Tatashin mutex_unlock(&lo->lo_mutex); 175214f27939SMilan Broz /* 175314f27939SMilan Broz * In autoclear mode, stop the loop thread 175414f27939SMilan Broz * and remove configuration after last close. 175514f27939SMilan Broz */ 1756322c4293STetsuo Handa loop_schedule_rundown(lo); 17570a42e99bSJan Kara return; 175843cade80SOmar Sandoval } else if (lo->lo_state == Lo_bound) { 175914f27939SMilan Broz /* 176014f27939SMilan Broz * Otherwise keep thread (if running) and config, 176114f27939SMilan Broz * but flush possible ongoing bios in thread. 176214f27939SMilan Broz */ 176343cade80SOmar Sandoval blk_mq_freeze_queue(lo->lo_queue); 176443cade80SOmar Sandoval blk_mq_unfreeze_queue(lo->lo_queue); 176514f27939SMilan Broz } 176696c58655SDavid Woodhouse 17670a42e99bSJan Kara out_unlock: 17686cc8e743SPavel Tatashin mutex_unlock(&lo->lo_mutex); 1769ae665016SLinus Torvalds } 1770ae665016SLinus Torvalds 177183d5cde4SAlexey Dobriyan static const struct block_device_operations lo_fops = { 17721da177e4SLinus Torvalds .owner = THIS_MODULE, 1773bb214884SAl Viro .open = lo_open, 1774bb214884SAl Viro .release = lo_release, 1775bb214884SAl Viro .ioctl = lo_ioctl, 1776863d5b82SDavid Howells #ifdef CONFIG_COMPAT 1777bb214884SAl Viro .compat_ioctl = lo_compat_ioctl, 1778863d5b82SDavid Howells #endif 17791da177e4SLinus Torvalds }; 17801da177e4SLinus Torvalds 17811da177e4SLinus Torvalds /* 17821da177e4SLinus Torvalds * And now the modules code and kernel interface. 17831da177e4SLinus Torvalds */ 178473285082SKen Chen static int max_loop; 17855657a819SJoe Perches module_param(max_loop, int, 0444); 1786a47653fcSKen Chen MODULE_PARM_DESC(max_loop, "Maximum number of loop devices"); 17875657a819SJoe Perches module_param(max_part, int, 0444); 1788476a4813SLaurent Vivier MODULE_PARM_DESC(max_part, "Maximum number of partitions per loop device"); 1789ef44c508SChaitanya Kulkarni 1790ef44c508SChaitanya Kulkarni static int hw_queue_depth = LOOP_DEFAULT_HW_Q_DEPTH; 1791ef44c508SChaitanya Kulkarni 1792ef44c508SChaitanya Kulkarni static int loop_set_hw_queue_depth(const char *s, const struct kernel_param *p) 1793ef44c508SChaitanya Kulkarni { 1794ef44c508SChaitanya Kulkarni int ret = kstrtoint(s, 10, &hw_queue_depth); 1795ef44c508SChaitanya Kulkarni 1796ef44c508SChaitanya Kulkarni return (ret || (hw_queue_depth < 1)) ? -EINVAL : 0; 1797ef44c508SChaitanya Kulkarni } 1798ef44c508SChaitanya Kulkarni 1799ef44c508SChaitanya Kulkarni static const struct kernel_param_ops loop_hw_qdepth_param_ops = { 1800ef44c508SChaitanya Kulkarni .set = loop_set_hw_queue_depth, 1801ef44c508SChaitanya Kulkarni .get = param_get_int, 1802ef44c508SChaitanya Kulkarni }; 1803ef44c508SChaitanya Kulkarni 1804ef44c508SChaitanya Kulkarni device_param_cb(hw_queue_depth, &loop_hw_qdepth_param_ops, &hw_queue_depth, 0444); 1805ef44c508SChaitanya Kulkarni MODULE_PARM_DESC(hw_queue_depth, "Queue depth for each hardware queue. Default: 128"); 1806ef44c508SChaitanya Kulkarni 18071da177e4SLinus Torvalds MODULE_LICENSE("GPL"); 18081da177e4SLinus Torvalds MODULE_ALIAS_BLOCKDEV_MAJOR(LOOP_MAJOR); 18091da177e4SLinus Torvalds 1810fc17b653SChristoph Hellwig static blk_status_t loop_queue_rq(struct blk_mq_hw_ctx *hctx, 1811b5dd2f60SMing Lei const struct blk_mq_queue_data *bd) 1812b5dd2f60SMing Lei { 18131894e916SJens Axboe struct request *rq = bd->rq; 18141894e916SJens Axboe struct loop_cmd *cmd = blk_mq_rq_to_pdu(rq); 18151894e916SJens Axboe struct loop_device *lo = rq->q->queuedata; 1816b5dd2f60SMing Lei 18171894e916SJens Axboe blk_mq_start_request(rq); 1818b5dd2f60SMing Lei 1819f4aa4c7bSMing Lei if (lo->lo_state != Lo_bound) 1820fc17b653SChristoph Hellwig return BLK_STS_IOERR; 1821f4aa4c7bSMing Lei 18221894e916SJens Axboe switch (req_op(rq)) { 1823f0225cacSChristoph Hellwig case REQ_OP_FLUSH: 1824f0225cacSChristoph Hellwig case REQ_OP_DISCARD: 182519372e27SChristoph Hellwig case REQ_OP_WRITE_ZEROES: 1826bc07c10aSMing Lei cmd->use_aio = false; 1827f0225cacSChristoph Hellwig break; 1828f0225cacSChristoph Hellwig default: 1829f0225cacSChristoph Hellwig cmd->use_aio = lo->use_dio; 1830f0225cacSChristoph Hellwig break; 1831f0225cacSChristoph Hellwig } 1832bc07c10aSMing Lei 1833d4478e92SShaohua Li /* always use the first bio's css */ 1834c74d40e8SDan Schatzberg cmd->blkcg_css = NULL; 1835c74d40e8SDan Schatzberg cmd->memcg_css = NULL; 18360b508bc9SShaohua Li #ifdef CONFIG_BLK_CGROUP 1837c74d40e8SDan Schatzberg if (rq->bio && rq->bio->bi_blkg) { 1838c74d40e8SDan Schatzberg cmd->blkcg_css = &bio_blkcg(rq->bio)->css; 1839c74d40e8SDan Schatzberg #ifdef CONFIG_MEMCG 1840c74d40e8SDan Schatzberg cmd->memcg_css = 1841c74d40e8SDan Schatzberg cgroup_get_e_css(cmd->blkcg_css->cgroup, 1842c74d40e8SDan Schatzberg &memory_cgrp_subsys); 1843d4478e92SShaohua Li #endif 1844c74d40e8SDan Schatzberg } 1845c74d40e8SDan Schatzberg #endif 184687579e9bSDan Schatzberg loop_queue_work(lo, cmd); 1847b5dd2f60SMing Lei 1848fc17b653SChristoph Hellwig return BLK_STS_OK; 1849b5dd2f60SMing Lei } 1850b5dd2f60SMing Lei 1851b5dd2f60SMing Lei static void loop_handle_cmd(struct loop_cmd *cmd) 1852b5dd2f60SMing Lei { 18531894e916SJens Axboe struct request *rq = blk_mq_rq_from_pdu(cmd); 18541894e916SJens Axboe const bool write = op_is_write(req_op(rq)); 18551894e916SJens Axboe struct loop_device *lo = rq->q->queuedata; 1856f4829a9bSChristoph Hellwig int ret = 0; 1857c74d40e8SDan Schatzberg struct mem_cgroup *old_memcg = NULL; 1858b5dd2f60SMing Lei 1859f4829a9bSChristoph Hellwig if (write && (lo->lo_flags & LO_FLAGS_READ_ONLY)) { 1860f4829a9bSChristoph Hellwig ret = -EIO; 1861b5dd2f60SMing Lei goto failed; 1862f4829a9bSChristoph Hellwig } 1863b5dd2f60SMing Lei 1864c74d40e8SDan Schatzberg if (cmd->blkcg_css) 1865c74d40e8SDan Schatzberg kthread_associate_blkcg(cmd->blkcg_css); 1866c74d40e8SDan Schatzberg if (cmd->memcg_css) 1867c74d40e8SDan Schatzberg old_memcg = set_active_memcg( 1868c74d40e8SDan Schatzberg mem_cgroup_from_css(cmd->memcg_css)); 1869c74d40e8SDan Schatzberg 18701894e916SJens Axboe ret = do_req_filebacked(lo, rq); 1871c74d40e8SDan Schatzberg 1872c74d40e8SDan Schatzberg if (cmd->blkcg_css) 1873c74d40e8SDan Schatzberg kthread_associate_blkcg(NULL); 1874c74d40e8SDan Schatzberg 1875c74d40e8SDan Schatzberg if (cmd->memcg_css) { 1876c74d40e8SDan Schatzberg set_active_memcg(old_memcg); 1877c74d40e8SDan Schatzberg css_put(cmd->memcg_css); 1878c74d40e8SDan Schatzberg } 1879b5dd2f60SMing Lei failed: 1880bc07c10aSMing Lei /* complete non-aio request */ 1881fe2cb290SChristoph Hellwig if (!cmd->use_aio || ret) { 18828cd55087SEvan Green if (ret == -EOPNOTSUPP) 18838cd55087SEvan Green cmd->ret = ret; 18848cd55087SEvan Green else 1885fe2cb290SChristoph Hellwig cmd->ret = ret ? -EIO : 0; 188615f73f5bSChristoph Hellwig if (likely(!blk_should_fake_timeout(rq->q))) 18871894e916SJens Axboe blk_mq_complete_request(rq); 1888fe2cb290SChristoph Hellwig } 1889b5dd2f60SMing Lei } 1890b5dd2f60SMing Lei 189187579e9bSDan Schatzberg static void loop_set_timer(struct loop_device *lo) 1892b5dd2f60SMing Lei { 189387579e9bSDan Schatzberg timer_reduce(&lo->timer, jiffies + LOOP_IDLE_WORKER_TIMEOUT); 1894b5dd2f60SMing Lei } 1895b5dd2f60SMing Lei 189687579e9bSDan Schatzberg static void loop_process_work(struct loop_worker *worker, 189787579e9bSDan Schatzberg struct list_head *cmd_list, struct loop_device *lo) 1898b5dd2f60SMing Lei { 189987579e9bSDan Schatzberg int orig_flags = current->flags; 190087579e9bSDan Schatzberg struct loop_cmd *cmd; 1901b5dd2f60SMing Lei 190287579e9bSDan Schatzberg current->flags |= PF_LOCAL_THROTTLE | PF_MEMALLOC_NOIO; 190387579e9bSDan Schatzberg spin_lock_irq(&lo->lo_work_lock); 190487579e9bSDan Schatzberg while (!list_empty(cmd_list)) { 190587579e9bSDan Schatzberg cmd = container_of( 190687579e9bSDan Schatzberg cmd_list->next, struct loop_cmd, list_entry); 190787579e9bSDan Schatzberg list_del(cmd_list->next); 190887579e9bSDan Schatzberg spin_unlock_irq(&lo->lo_work_lock); 190987579e9bSDan Schatzberg 191087579e9bSDan Schatzberg loop_handle_cmd(cmd); 191187579e9bSDan Schatzberg cond_resched(); 191287579e9bSDan Schatzberg 191387579e9bSDan Schatzberg spin_lock_irq(&lo->lo_work_lock); 191487579e9bSDan Schatzberg } 191587579e9bSDan Schatzberg 191687579e9bSDan Schatzberg /* 191787579e9bSDan Schatzberg * We only add to the idle list if there are no pending cmds 191887579e9bSDan Schatzberg * *and* the worker will not run again which ensures that it 191987579e9bSDan Schatzberg * is safe to free any worker on the idle list 192087579e9bSDan Schatzberg */ 192187579e9bSDan Schatzberg if (worker && !work_pending(&worker->work)) { 192287579e9bSDan Schatzberg worker->last_ran_at = jiffies; 192387579e9bSDan Schatzberg list_add_tail(&worker->idle_list, &lo->idle_worker_list); 192487579e9bSDan Schatzberg loop_set_timer(lo); 192587579e9bSDan Schatzberg } 192687579e9bSDan Schatzberg spin_unlock_irq(&lo->lo_work_lock); 192787579e9bSDan Schatzberg current->flags = orig_flags; 192887579e9bSDan Schatzberg } 192987579e9bSDan Schatzberg 193087579e9bSDan Schatzberg static void loop_workfn(struct work_struct *work) 193187579e9bSDan Schatzberg { 193287579e9bSDan Schatzberg struct loop_worker *worker = 193387579e9bSDan Schatzberg container_of(work, struct loop_worker, work); 193487579e9bSDan Schatzberg loop_process_work(worker, &worker->cmd_list, worker->lo); 193587579e9bSDan Schatzberg } 193687579e9bSDan Schatzberg 193787579e9bSDan Schatzberg static void loop_rootcg_workfn(struct work_struct *work) 193887579e9bSDan Schatzberg { 193987579e9bSDan Schatzberg struct loop_device *lo = 194087579e9bSDan Schatzberg container_of(work, struct loop_device, rootcg_work); 194187579e9bSDan Schatzberg loop_process_work(NULL, &lo->rootcg_cmd_list, lo); 194287579e9bSDan Schatzberg } 194387579e9bSDan Schatzberg 194487579e9bSDan Schatzberg static void loop_free_idle_workers(struct timer_list *timer) 194587579e9bSDan Schatzberg { 194687579e9bSDan Schatzberg struct loop_device *lo = container_of(timer, struct loop_device, timer); 194787579e9bSDan Schatzberg struct loop_worker *pos, *worker; 194887579e9bSDan Schatzberg 194987579e9bSDan Schatzberg spin_lock_irq(&lo->lo_work_lock); 195087579e9bSDan Schatzberg list_for_each_entry_safe(worker, pos, &lo->idle_worker_list, 195187579e9bSDan Schatzberg idle_list) { 195287579e9bSDan Schatzberg if (time_is_after_jiffies(worker->last_ran_at + 195387579e9bSDan Schatzberg LOOP_IDLE_WORKER_TIMEOUT)) 195487579e9bSDan Schatzberg break; 195587579e9bSDan Schatzberg list_del(&worker->idle_list); 195687579e9bSDan Schatzberg rb_erase(&worker->rb_node, &lo->worker_tree); 1957c74d40e8SDan Schatzberg css_put(worker->blkcg_css); 195887579e9bSDan Schatzberg kfree(worker); 195987579e9bSDan Schatzberg } 196087579e9bSDan Schatzberg if (!list_empty(&lo->idle_worker_list)) 196187579e9bSDan Schatzberg loop_set_timer(lo); 196287579e9bSDan Schatzberg spin_unlock_irq(&lo->lo_work_lock); 1963b5dd2f60SMing Lei } 1964b5dd2f60SMing Lei 1965f363b089SEric Biggers static const struct blk_mq_ops loop_mq_ops = { 1966b5dd2f60SMing Lei .queue_rq = loop_queue_rq, 1967fe2cb290SChristoph Hellwig .complete = lo_complete_rq, 1968b5dd2f60SMing Lei }; 1969b5dd2f60SMing Lei 1970d6da83d0SChristoph Hellwig static int loop_add(int i) 19711da177e4SLinus Torvalds { 197273285082SKen Chen struct loop_device *lo; 197373285082SKen Chen struct gendisk *disk; 197434dd82afSKay Sievers int err; 19751da177e4SLinus Torvalds 197634dd82afSKay Sievers err = -ENOMEM; 197768d740d7SSilva Paulo lo = kzalloc(sizeof(*lo), GFP_KERNEL); 197868d740d7SSilva Paulo if (!lo) 197973285082SKen Chen goto out; 1980ef7e7c82SMikulas Patocka lo->lo_state = Lo_unbound; 1981ef7e7c82SMikulas Patocka 198218d1f200SChristoph Hellwig err = mutex_lock_killable(&loop_ctl_mutex); 198318d1f200SChristoph Hellwig if (err) 198418d1f200SChristoph Hellwig goto out_free_dev; 198518d1f200SChristoph Hellwig 1986c718aa65STejun Heo /* allocate id, if @id >= 0, we're requesting that specific id */ 198734dd82afSKay Sievers if (i >= 0) { 1988c718aa65STejun Heo err = idr_alloc(&loop_index_idr, lo, i, i + 1, GFP_KERNEL); 1989c718aa65STejun Heo if (err == -ENOSPC) 199034dd82afSKay Sievers err = -EEXIST; 199134dd82afSKay Sievers } else { 1992c718aa65STejun Heo err = idr_alloc(&loop_index_idr, lo, 0, 0, GFP_KERNEL); 199334dd82afSKay Sievers } 19941c500ad7STetsuo Handa mutex_unlock(&loop_ctl_mutex); 199534dd82afSKay Sievers if (err < 0) 19961c500ad7STetsuo Handa goto out_free_dev; 1997c718aa65STejun Heo i = err; 19981da177e4SLinus Torvalds 1999b5dd2f60SMing Lei lo->tag_set.ops = &loop_mq_ops; 2000b5dd2f60SMing Lei lo->tag_set.nr_hw_queues = 1; 2001ef44c508SChaitanya Kulkarni lo->tag_set.queue_depth = hw_queue_depth; 2002b5dd2f60SMing Lei lo->tag_set.numa_node = NUMA_NO_NODE; 2003b5dd2f60SMing Lei lo->tag_set.cmd_size = sizeof(struct loop_cmd); 20042112f5c1SBart Van Assche lo->tag_set.flags = BLK_MQ_F_SHOULD_MERGE | BLK_MQ_F_STACKING | 20052112f5c1SBart Van Assche BLK_MQ_F_NO_SCHED_BY_DEFAULT; 2006b5dd2f60SMing Lei lo->tag_set.driver_data = lo; 2007b5dd2f60SMing Lei 2008b5dd2f60SMing Lei err = blk_mq_alloc_tag_set(&lo->tag_set); 2009b5dd2f60SMing Lei if (err) 20103ec981e3SMikulas Patocka goto out_free_idr; 201173285082SKen Chen 20121c99502fSChristoph Hellwig disk = lo->lo_disk = blk_mq_alloc_disk(&lo->tag_set, lo); 20131c99502fSChristoph Hellwig if (IS_ERR(disk)) { 20141c99502fSChristoph Hellwig err = PTR_ERR(disk); 2015b5dd2f60SMing Lei goto out_cleanup_tags; 2016b5dd2f60SMing Lei } 20171c99502fSChristoph Hellwig lo->lo_queue = lo->lo_disk->queue; 2018ef7e7c82SMikulas Patocka 201954bb0adeSShaohua Li blk_queue_max_hw_sectors(lo->lo_queue, BLK_DEF_MAX_SECTORS); 202040326d8aSShaohua Li 20215b5e20f4SMing Lei /* 202240326d8aSShaohua Li * By default, we do buffer IO, so it doesn't make sense to enable 202340326d8aSShaohua Li * merge because the I/O submitted to backing file is handled page by 202440326d8aSShaohua Li * page. For directio mode, merge does help to dispatch bigger request 202540326d8aSShaohua Li * to underlayer disk. We will enable merge once directio is enabled. 20265b5e20f4SMing Lei */ 20278b904b5bSBart Van Assche blk_queue_flag_set(QUEUE_FLAG_NOMERGES, lo->lo_queue); 20285b5e20f4SMing Lei 2029e03c8dd1SKay Sievers /* 2030e03c8dd1SKay Sievers * Disable partition scanning by default. The in-kernel partition 2031e03c8dd1SKay Sievers * scanning can be requested individually per-device during its 2032e03c8dd1SKay Sievers * setup. Userspace can always add and remove partitions from all 2033e03c8dd1SKay Sievers * devices. The needed partition minors are allocated from the 2034e03c8dd1SKay Sievers * extended minor space, the main loop device numbers will continue 2035e03c8dd1SKay Sievers * to match the loop minors, regardless of the number of partitions 2036e03c8dd1SKay Sievers * used. 2037e03c8dd1SKay Sievers * 2038e03c8dd1SKay Sievers * If max_part is given, partition scanning is globally enabled for 2039e03c8dd1SKay Sievers * all loop devices. The minors for the main loop devices will be 2040e03c8dd1SKay Sievers * multiples of max_part. 2041e03c8dd1SKay Sievers * 2042e03c8dd1SKay Sievers * Note: Global-for-all-devices, set-only-at-init, read-only module 2043e03c8dd1SKay Sievers * parameteters like 'max_loop' and 'max_part' make things needlessly 2044e03c8dd1SKay Sievers * complicated, are too static, inflexible and may surprise 2045e03c8dd1SKay Sievers * userspace tools. Parameters like this in general should be avoided. 2046e03c8dd1SKay Sievers */ 2047e03c8dd1SKay Sievers if (!part_shift) 204846e7eac6SChristoph Hellwig disk->flags |= GENHD_FL_NO_PART; 2049f8933667SMing Lei atomic_set(&lo->lo_refcnt, 0); 20506cc8e743SPavel Tatashin mutex_init(&lo->lo_mutex); 20511da177e4SLinus Torvalds lo->lo_number = i; 20521da177e4SLinus Torvalds spin_lock_init(&lo->lo_lock); 205387579e9bSDan Schatzberg spin_lock_init(&lo->lo_work_lock); 20541da177e4SLinus Torvalds disk->major = LOOP_MAJOR; 2055476a4813SLaurent Vivier disk->first_minor = i << part_shift; 20561c99502fSChristoph Hellwig disk->minors = 1 << part_shift; 20571da177e4SLinus Torvalds disk->fops = &lo_fops; 20581da177e4SLinus Torvalds disk->private_data = lo; 20591da177e4SLinus Torvalds disk->queue = lo->lo_queue; 20609f65c489SMatteo Croce disk->events = DISK_EVENT_MEDIA_CHANGE; 20619f65c489SMatteo Croce disk->event_flags = DISK_EVENT_FLAG_UEVENT; 206273285082SKen Chen sprintf(disk->disk_name, "loop%d", i); 20631c500ad7STetsuo Handa /* Make this loop device reachable from pathname. */ 2064905705f0SLuis Chamberlain err = add_disk(disk); 2065905705f0SLuis Chamberlain if (err) 2066905705f0SLuis Chamberlain goto out_cleanup_disk; 2067905705f0SLuis Chamberlain 20681c500ad7STetsuo Handa /* Show this loop device. */ 20691c500ad7STetsuo Handa mutex_lock(&loop_ctl_mutex); 20701c500ad7STetsuo Handa lo->idr_visible = true; 207118d1f200SChristoph Hellwig mutex_unlock(&loop_ctl_mutex); 2072905705f0SLuis Chamberlain 207318d1f200SChristoph Hellwig return i; 207473285082SKen Chen 2075905705f0SLuis Chamberlain out_cleanup_disk: 2076905705f0SLuis Chamberlain blk_cleanup_disk(disk); 2077b5dd2f60SMing Lei out_cleanup_tags: 2078b5dd2f60SMing Lei blk_mq_free_tag_set(&lo->tag_set); 20793ec981e3SMikulas Patocka out_free_idr: 20801c500ad7STetsuo Handa mutex_lock(&loop_ctl_mutex); 20813ec981e3SMikulas Patocka idr_remove(&loop_index_idr, i); 208218d1f200SChristoph Hellwig mutex_unlock(&loop_ctl_mutex); 208373285082SKen Chen out_free_dev: 208473285082SKen Chen kfree(lo); 208573285082SKen Chen out: 208634dd82afSKay Sievers return err; 20871da177e4SLinus Torvalds } 20881da177e4SLinus Torvalds 208934dd82afSKay Sievers static void loop_remove(struct loop_device *lo) 209073285082SKen Chen { 20911c500ad7STetsuo Handa /* Make this loop device unreachable from pathname. */ 20926cd18e71SNeilBrown del_gendisk(lo->lo_disk); 20931c99502fSChristoph Hellwig blk_cleanup_disk(lo->lo_disk); 2094b5dd2f60SMing Lei blk_mq_free_tag_set(&lo->tag_set); 2095*ce8d7861SChristoph Hellwig 20961c500ad7STetsuo Handa mutex_lock(&loop_ctl_mutex); 20971c500ad7STetsuo Handa idr_remove(&loop_index_idr, lo->lo_number); 20981c500ad7STetsuo Handa mutex_unlock(&loop_ctl_mutex); 20991c500ad7STetsuo Handa /* There is no route which can find this loop device. */ 21006cc8e743SPavel Tatashin mutex_destroy(&lo->lo_mutex); 210173285082SKen Chen kfree(lo); 210273285082SKen Chen } 210373285082SKen Chen 21048410d38cSChristoph Hellwig static void loop_probe(dev_t dev) 210573285082SKen Chen { 21068410d38cSChristoph Hellwig int idx = MINOR(dev) >> part_shift; 21078410d38cSChristoph Hellwig 21088410d38cSChristoph Hellwig if (max_loop && idx >= max_loop) 21098410d38cSChristoph Hellwig return; 2110d6da83d0SChristoph Hellwig loop_add(idx); 2111f9d10764SChristoph Hellwig } 2112f9d10764SChristoph Hellwig 2113f9d10764SChristoph Hellwig static int loop_control_remove(int idx) 2114770fe30aSKay Sievers { 2115770fe30aSKay Sievers struct loop_device *lo; 21160a42e99bSJan Kara int ret; 2117e5d66a10SChristoph Hellwig 2118e5d66a10SChristoph Hellwig if (idx < 0) { 2119e3f9387aSTetsuo Handa pr_warn_once("deleting an unspecified loop device is not supported.\n"); 2120e5d66a10SChristoph Hellwig return -EINVAL; 2121e5d66a10SChristoph Hellwig } 2122770fe30aSKay Sievers 21231c500ad7STetsuo Handa /* Hide this loop device for serialization. */ 21240a42e99bSJan Kara ret = mutex_lock_killable(&loop_ctl_mutex); 21250a42e99bSJan Kara if (ret) 21260a42e99bSJan Kara return ret; 2127b9848081SChristoph Hellwig lo = idr_find(&loop_index_idr, idx); 21281c500ad7STetsuo Handa if (!lo || !lo->idr_visible) 2129b9848081SChristoph Hellwig ret = -ENODEV; 21301c500ad7STetsuo Handa else 21311c500ad7STetsuo Handa lo->idr_visible = false; 21321c500ad7STetsuo Handa mutex_unlock(&loop_ctl_mutex); 21331c500ad7STetsuo Handa if (ret) 21341c500ad7STetsuo Handa return ret; 2135f9d10764SChristoph Hellwig 21361c500ad7STetsuo Handa /* Check whether this loop device can be removed. */ 21376cc8e743SPavel Tatashin ret = mutex_lock_killable(&lo->lo_mutex); 21386cc8e743SPavel Tatashin if (ret) 21391c500ad7STetsuo Handa goto mark_visible; 2140f9d10764SChristoph Hellwig if (lo->lo_state != Lo_unbound || 2141f9d10764SChristoph Hellwig atomic_read(&lo->lo_refcnt) > 0) { 21426cc8e743SPavel Tatashin mutex_unlock(&lo->lo_mutex); 2143770fe30aSKay Sievers ret = -EBUSY; 21441c500ad7STetsuo Handa goto mark_visible; 2145770fe30aSKay Sievers } 21461c500ad7STetsuo Handa /* Mark this loop device no longer open()-able. */ 2147990e7811SChristoph Hellwig lo->lo_state = Lo_deleting; 21486cc8e743SPavel Tatashin mutex_unlock(&lo->lo_mutex); 2149f9d10764SChristoph Hellwig 2150770fe30aSKay Sievers loop_remove(lo); 21511c500ad7STetsuo Handa return 0; 21521c500ad7STetsuo Handa 21531c500ad7STetsuo Handa mark_visible: 21541c500ad7STetsuo Handa /* Show this loop device again. */ 21551c500ad7STetsuo Handa mutex_lock(&loop_ctl_mutex); 21561c500ad7STetsuo Handa lo->idr_visible = true; 2157f9d10764SChristoph Hellwig mutex_unlock(&loop_ctl_mutex); 2158f9d10764SChristoph Hellwig return ret; 2159770fe30aSKay Sievers } 2160f9d10764SChristoph Hellwig 2161f9d10764SChristoph Hellwig static int loop_control_get_free(int idx) 2162f9d10764SChristoph Hellwig { 2163f9d10764SChristoph Hellwig struct loop_device *lo; 2164b9848081SChristoph Hellwig int id, ret; 2165f9d10764SChristoph Hellwig 2166f9d10764SChristoph Hellwig ret = mutex_lock_killable(&loop_ctl_mutex); 2167f9d10764SChristoph Hellwig if (ret) 2168f9d10764SChristoph Hellwig return ret; 2169b9848081SChristoph Hellwig idr_for_each_entry(&loop_index_idr, lo, id) { 21701c500ad7STetsuo Handa /* Hitting a race results in creating a new loop device which is harmless. */ 21711c500ad7STetsuo Handa if (lo->idr_visible && data_race(lo->lo_state) == Lo_unbound) 2172b9848081SChristoph Hellwig goto found; 2173b9848081SChristoph Hellwig } 21740a42e99bSJan Kara mutex_unlock(&loop_ctl_mutex); 217518d1f200SChristoph Hellwig return loop_add(-1); 2176b9848081SChristoph Hellwig found: 2177b9848081SChristoph Hellwig mutex_unlock(&loop_ctl_mutex); 2178b9848081SChristoph Hellwig return id; 2179770fe30aSKay Sievers } 2180770fe30aSKay Sievers 2181f9d10764SChristoph Hellwig static long loop_control_ioctl(struct file *file, unsigned int cmd, 2182f9d10764SChristoph Hellwig unsigned long parm) 2183f9d10764SChristoph Hellwig { 2184f9d10764SChristoph Hellwig switch (cmd) { 2185f9d10764SChristoph Hellwig case LOOP_CTL_ADD: 218618d1f200SChristoph Hellwig return loop_add(parm); 2187f9d10764SChristoph Hellwig case LOOP_CTL_REMOVE: 2188f9d10764SChristoph Hellwig return loop_control_remove(parm); 2189f9d10764SChristoph Hellwig case LOOP_CTL_GET_FREE: 2190f9d10764SChristoph Hellwig return loop_control_get_free(parm); 2191f9d10764SChristoph Hellwig default: 2192f9d10764SChristoph Hellwig return -ENOSYS; 2193f9d10764SChristoph Hellwig } 2194f9d10764SChristoph Hellwig } 2195f9d10764SChristoph Hellwig 2196770fe30aSKay Sievers static const struct file_operations loop_ctl_fops = { 2197770fe30aSKay Sievers .open = nonseekable_open, 2198770fe30aSKay Sievers .unlocked_ioctl = loop_control_ioctl, 2199770fe30aSKay Sievers .compat_ioctl = loop_control_ioctl, 2200770fe30aSKay Sievers .owner = THIS_MODULE, 2201770fe30aSKay Sievers .llseek = noop_llseek, 2202770fe30aSKay Sievers }; 2203770fe30aSKay Sievers 2204770fe30aSKay Sievers static struct miscdevice loop_misc = { 2205770fe30aSKay Sievers .minor = LOOP_CTRL_MINOR, 2206770fe30aSKay Sievers .name = "loop-control", 2207770fe30aSKay Sievers .fops = &loop_ctl_fops, 2208770fe30aSKay Sievers }; 2209770fe30aSKay Sievers 2210770fe30aSKay Sievers MODULE_ALIAS_MISCDEV(LOOP_CTRL_MINOR); 2211770fe30aSKay Sievers MODULE_ALIAS("devname:loop-control"); 2212770fe30aSKay Sievers 221373285082SKen Chen static int __init loop_init(void) 221473285082SKen Chen { 2215a47653fcSKen Chen int i, nr; 2216770fe30aSKay Sievers int err; 2217a47653fcSKen Chen 2218476a4813SLaurent Vivier part_shift = 0; 2219ac04fee0SNamhyung Kim if (max_part > 0) { 2220476a4813SLaurent Vivier part_shift = fls(max_part); 2221476a4813SLaurent Vivier 2222ac04fee0SNamhyung Kim /* 2223ac04fee0SNamhyung Kim * Adjust max_part according to part_shift as it is exported 2224ac04fee0SNamhyung Kim * to user space so that user can decide correct minor number 2225ac04fee0SNamhyung Kim * if [s]he want to create more devices. 2226ac04fee0SNamhyung Kim * 2227ac04fee0SNamhyung Kim * Note that -1 is required because partition 0 is reserved 2228ac04fee0SNamhyung Kim * for the whole disk. 2229ac04fee0SNamhyung Kim */ 2230ac04fee0SNamhyung Kim max_part = (1UL << part_shift) - 1; 2231ac04fee0SNamhyung Kim } 2232ac04fee0SNamhyung Kim 2233b1a66504SGuo Chao if ((1UL << part_shift) > DISK_MAX_PARTS) { 2234b1a66504SGuo Chao err = -EINVAL; 2235a8c1d064SAnton Volkov goto err_out; 2236b1a66504SGuo Chao } 223778f4bb36SNamhyung Kim 2238b1a66504SGuo Chao if (max_loop > 1UL << (MINORBITS - part_shift)) { 2239b1a66504SGuo Chao err = -EINVAL; 2240a8c1d064SAnton Volkov goto err_out; 2241b1a66504SGuo Chao } 224273285082SKen Chen 2243d134b00bSKay Sievers /* 2244d134b00bSKay Sievers * If max_loop is specified, create that many devices upfront. 2245d134b00bSKay Sievers * This also becomes a hard limit. If max_loop is not specified, 2246d134b00bSKay Sievers * create CONFIG_BLK_DEV_LOOP_MIN_COUNT loop devices at module 2247d134b00bSKay Sievers * init time. Loop devices can be requested on-demand with the 2248d134b00bSKay Sievers * /dev/loop-control interface, or be instantiated by accessing 2249d134b00bSKay Sievers * a 'dead' device node. 2250d134b00bSKay Sievers */ 2251aeb2b0b1SLukas Bulwahn if (max_loop) 2252a47653fcSKen Chen nr = max_loop; 2253aeb2b0b1SLukas Bulwahn else 2254d134b00bSKay Sievers nr = CONFIG_BLK_DEV_LOOP_MIN_COUNT; 2255a47653fcSKen Chen 2256a8c1d064SAnton Volkov err = misc_register(&loop_misc); 2257a8c1d064SAnton Volkov if (err < 0) 2258a8c1d064SAnton Volkov goto err_out; 2259a8c1d064SAnton Volkov 2260a8c1d064SAnton Volkov 22618410d38cSChristoph Hellwig if (__register_blkdev(LOOP_MAJOR, "loop", loop_probe)) { 2262b1a66504SGuo Chao err = -EIO; 2263b1a66504SGuo Chao goto misc_out; 2264b1a66504SGuo Chao } 2265a47653fcSKen Chen 2266d134b00bSKay Sievers /* pre-create number of devices given by config or max_loop */ 226734dd82afSKay Sievers for (i = 0; i < nr; i++) 2268d6da83d0SChristoph Hellwig loop_add(i); 226934dd82afSKay Sievers 227073285082SKen Chen printk(KERN_INFO "loop: module loaded\n"); 22711da177e4SLinus Torvalds return 0; 2272b1a66504SGuo Chao 2273b1a66504SGuo Chao misc_out: 2274b1a66504SGuo Chao misc_deregister(&loop_misc); 2275a8c1d064SAnton Volkov err_out: 2276b1a66504SGuo Chao return err; 227734dd82afSKay Sievers } 2278a47653fcSKen Chen 227973285082SKen Chen static void __exit loop_exit(void) 22801da177e4SLinus Torvalds { 22818e60947dSChristoph Hellwig struct loop_device *lo; 22828e60947dSChristoph Hellwig int id; 22838e60947dSChristoph Hellwig 22848b52d8beSChristoph Hellwig unregister_blkdev(LOOP_MAJOR, "loop"); 22858b52d8beSChristoph Hellwig misc_deregister(&loop_misc); 2286200f9337SLuis Chamberlain 22871c500ad7STetsuo Handa /* 22881c500ad7STetsuo Handa * There is no need to use loop_ctl_mutex here, for nobody else can 22891c500ad7STetsuo Handa * access loop_index_idr when this module is unloading (unless forced 22901c500ad7STetsuo Handa * module unloading is requested). If this is not a clean unloading, 22911c500ad7STetsuo Handa * we have no means to avoid kernel crash. 22921c500ad7STetsuo Handa */ 22938e60947dSChristoph Hellwig idr_for_each_entry(&loop_index_idr, lo, id) 22948e60947dSChristoph Hellwig loop_remove(lo); 2295bd5c39edSChristoph Hellwig 2296bd5c39edSChristoph Hellwig idr_destroy(&loop_index_idr); 22971da177e4SLinus Torvalds } 22981da177e4SLinus Torvalds 22991da177e4SLinus Torvalds module_init(loop_init); 23001da177e4SLinus Torvalds module_exit(loop_exit); 23011da177e4SLinus Torvalds 23021da177e4SLinus Torvalds #ifndef MODULE 23031da177e4SLinus Torvalds static int __init max_loop_setup(char *str) 23041da177e4SLinus Torvalds { 23051da177e4SLinus Torvalds max_loop = simple_strtol(str, NULL, 0); 23061da177e4SLinus Torvalds return 1; 23071da177e4SLinus Torvalds } 23081da177e4SLinus Torvalds 23091da177e4SLinus Torvalds __setup("max_loop=", max_loop_setup); 23101da177e4SLinus Torvalds #endif 2311