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/blkpg.h> 631da177e4SLinus Torvalds #include <linux/init.h> 641da177e4SLinus Torvalds #include <linux/swap.h> 651da177e4SLinus Torvalds #include <linux/slab.h> 66863d5b82SDavid Howells #include <linux/compat.h> 671da177e4SLinus Torvalds #include <linux/suspend.h> 6883144186SRafael J. Wysocki #include <linux/freezer.h> 692a48fc0aSArnd Bergmann #include <linux/mutex.h> 701da177e4SLinus Torvalds #include <linux/writeback.h> 711da177e4SLinus Torvalds #include <linux/completion.h> 721da177e4SLinus Torvalds #include <linux/highmem.h> 73d6b29d7cSJens Axboe #include <linux/splice.h> 74ee862730SMilan Broz #include <linux/sysfs.h> 75770fe30aSKay Sievers #include <linux/miscdevice.h> 76dfaa2ef6SLukas Czerner #include <linux/falloc.h> 77283e7e5dSAl Viro #include <linux/uio.h> 78d9a08a9eSAdam Manzanares #include <linux/ioprio.h> 79db6638d7SDennis Zhou #include <linux/blk-cgroup.h> 80c74d40e8SDan Schatzberg #include <linux/sched/mm.h> 8106582bc8SMing Lei #include <linux/statfs.h> 827c0f6ba6SLinus Torvalds #include <linux/uaccess.h> 83*754d9679SChristoph Hellwig #include <linux/blk-mq.h> 84*754d9679SChristoph Hellwig #include <linux/spinlock.h> 85*754d9679SChristoph Hellwig #include <uapi/linux/loop.h> 86*754d9679SChristoph Hellwig 87*754d9679SChristoph Hellwig /* Possible states of device */ 88*754d9679SChristoph Hellwig enum { 89*754d9679SChristoph Hellwig Lo_unbound, 90*754d9679SChristoph Hellwig Lo_bound, 91*754d9679SChristoph Hellwig Lo_rundown, 92*754d9679SChristoph Hellwig Lo_deleting, 93*754d9679SChristoph Hellwig }; 94*754d9679SChristoph Hellwig 95*754d9679SChristoph Hellwig struct loop_func_table; 96*754d9679SChristoph Hellwig 97*754d9679SChristoph Hellwig struct loop_device { 98*754d9679SChristoph Hellwig int lo_number; 99*754d9679SChristoph Hellwig loff_t lo_offset; 100*754d9679SChristoph Hellwig loff_t lo_sizelimit; 101*754d9679SChristoph Hellwig int lo_flags; 102*754d9679SChristoph Hellwig char lo_file_name[LO_NAME_SIZE]; 103*754d9679SChristoph Hellwig 104*754d9679SChristoph Hellwig struct file * lo_backing_file; 105*754d9679SChristoph Hellwig struct block_device *lo_device; 106*754d9679SChristoph Hellwig 107*754d9679SChristoph Hellwig gfp_t old_gfp_mask; 108*754d9679SChristoph Hellwig 109*754d9679SChristoph Hellwig spinlock_t lo_lock; 110*754d9679SChristoph Hellwig int lo_state; 111*754d9679SChristoph Hellwig spinlock_t lo_work_lock; 112*754d9679SChristoph Hellwig struct workqueue_struct *workqueue; 113*754d9679SChristoph Hellwig struct work_struct rootcg_work; 114*754d9679SChristoph Hellwig struct list_head rootcg_cmd_list; 115*754d9679SChristoph Hellwig struct list_head idle_worker_list; 116*754d9679SChristoph Hellwig struct rb_root worker_tree; 117*754d9679SChristoph Hellwig struct timer_list timer; 118*754d9679SChristoph Hellwig bool use_dio; 119*754d9679SChristoph Hellwig bool sysfs_inited; 120*754d9679SChristoph Hellwig 121*754d9679SChristoph Hellwig struct request_queue *lo_queue; 122*754d9679SChristoph Hellwig struct blk_mq_tag_set tag_set; 123*754d9679SChristoph Hellwig struct gendisk *lo_disk; 124*754d9679SChristoph Hellwig struct mutex lo_mutex; 125*754d9679SChristoph Hellwig bool idr_visible; 126*754d9679SChristoph Hellwig }; 127*754d9679SChristoph Hellwig 128*754d9679SChristoph Hellwig struct loop_cmd { 129*754d9679SChristoph Hellwig struct list_head list_entry; 130*754d9679SChristoph Hellwig bool use_aio; /* use AIO interface to handle I/O */ 131*754d9679SChristoph Hellwig atomic_t ref; /* only for aio */ 132*754d9679SChristoph Hellwig long ret; 133*754d9679SChristoph Hellwig struct kiocb iocb; 134*754d9679SChristoph Hellwig struct bio_vec *bvec; 135*754d9679SChristoph Hellwig struct cgroup_subsys_state *blkcg_css; 136*754d9679SChristoph Hellwig struct cgroup_subsys_state *memcg_css; 137*754d9679SChristoph Hellwig }; 1381da177e4SLinus Torvalds 13987579e9bSDan Schatzberg #define LOOP_IDLE_WORKER_TIMEOUT (60 * HZ) 140ef44c508SChaitanya Kulkarni #define LOOP_DEFAULT_HW_Q_DEPTH (128) 14187579e9bSDan Schatzberg 14234dd82afSKay Sievers static DEFINE_IDR(loop_index_idr); 143310ca162STetsuo Handa static DEFINE_MUTEX(loop_ctl_mutex); 1443ce6e1f6STetsuo Handa static DEFINE_MUTEX(loop_validate_mutex); 1453ce6e1f6STetsuo Handa 1463ce6e1f6STetsuo Handa /** 1473ce6e1f6STetsuo Handa * loop_global_lock_killable() - take locks for safe loop_validate_file() test 1483ce6e1f6STetsuo Handa * 1493ce6e1f6STetsuo Handa * @lo: struct loop_device 1503ce6e1f6STetsuo Handa * @global: true if @lo is about to bind another "struct loop_device", false otherwise 1513ce6e1f6STetsuo Handa * 1523ce6e1f6STetsuo Handa * Returns 0 on success, -EINTR otherwise. 1533ce6e1f6STetsuo Handa * 1543ce6e1f6STetsuo Handa * Since loop_validate_file() traverses on other "struct loop_device" if 1553ce6e1f6STetsuo Handa * is_loop_device() is true, we need a global lock for serializing concurrent 1563ce6e1f6STetsuo Handa * loop_configure()/loop_change_fd()/__loop_clr_fd() calls. 1573ce6e1f6STetsuo Handa */ 1583ce6e1f6STetsuo Handa static int loop_global_lock_killable(struct loop_device *lo, bool global) 1593ce6e1f6STetsuo Handa { 1603ce6e1f6STetsuo Handa int err; 1613ce6e1f6STetsuo Handa 1623ce6e1f6STetsuo Handa if (global) { 1633ce6e1f6STetsuo Handa err = mutex_lock_killable(&loop_validate_mutex); 1643ce6e1f6STetsuo Handa if (err) 1653ce6e1f6STetsuo Handa return err; 1663ce6e1f6STetsuo Handa } 1673ce6e1f6STetsuo Handa err = mutex_lock_killable(&lo->lo_mutex); 1683ce6e1f6STetsuo Handa if (err && global) 1693ce6e1f6STetsuo Handa mutex_unlock(&loop_validate_mutex); 1703ce6e1f6STetsuo Handa return err; 1713ce6e1f6STetsuo Handa } 1723ce6e1f6STetsuo Handa 1733ce6e1f6STetsuo Handa /** 1743ce6e1f6STetsuo Handa * loop_global_unlock() - release locks taken by loop_global_lock_killable() 1753ce6e1f6STetsuo Handa * 1763ce6e1f6STetsuo Handa * @lo: struct loop_device 1773ce6e1f6STetsuo Handa * @global: true if @lo was about to bind another "struct loop_device", false otherwise 1783ce6e1f6STetsuo Handa */ 1793ce6e1f6STetsuo Handa static void loop_global_unlock(struct loop_device *lo, bool global) 1803ce6e1f6STetsuo Handa { 1813ce6e1f6STetsuo Handa mutex_unlock(&lo->lo_mutex); 1823ce6e1f6STetsuo Handa if (global) 1833ce6e1f6STetsuo Handa mutex_unlock(&loop_validate_mutex); 1843ce6e1f6STetsuo Handa } 1851da177e4SLinus Torvalds 186476a4813SLaurent Vivier static int max_part; 187476a4813SLaurent Vivier static int part_shift; 188476a4813SLaurent Vivier 1897035b5dfSDmitry Monakhov static loff_t get_size(loff_t offset, loff_t sizelimit, struct file *file) 1901da177e4SLinus Torvalds { 191b7a1da69SGuo Chao loff_t loopsize; 1921da177e4SLinus Torvalds 1931da177e4SLinus Torvalds /* Compute loopsize in bytes */ 194b7a1da69SGuo Chao loopsize = i_size_read(file->f_mapping->host); 195b7a1da69SGuo Chao if (offset > 0) 196b7a1da69SGuo Chao loopsize -= offset; 197b7a1da69SGuo Chao /* offset is beyond i_size, weird but possible */ 1987035b5dfSDmitry Monakhov if (loopsize < 0) 1997035b5dfSDmitry Monakhov return 0; 2001da177e4SLinus Torvalds 2017035b5dfSDmitry Monakhov if (sizelimit > 0 && sizelimit < loopsize) 2027035b5dfSDmitry Monakhov loopsize = sizelimit; 2031da177e4SLinus Torvalds /* 2041da177e4SLinus Torvalds * Unfortunately, if we want to do I/O on the device, 2051da177e4SLinus Torvalds * the number of 512-byte sectors has to fit into a sector_t. 2061da177e4SLinus Torvalds */ 2071da177e4SLinus Torvalds return loopsize >> 9; 2081da177e4SLinus Torvalds } 2091da177e4SLinus Torvalds 2107035b5dfSDmitry Monakhov static loff_t get_loop_size(struct loop_device *lo, struct file *file) 2111da177e4SLinus Torvalds { 2127035b5dfSDmitry Monakhov return get_size(lo->lo_offset, lo->lo_sizelimit, file); 2137035b5dfSDmitry Monakhov } 2147035b5dfSDmitry Monakhov 2152e5ab5f3SMing Lei static void __loop_update_dio(struct loop_device *lo, bool dio) 2162e5ab5f3SMing Lei { 2172e5ab5f3SMing Lei struct file *file = lo->lo_backing_file; 2182e5ab5f3SMing Lei struct address_space *mapping = file->f_mapping; 2192e5ab5f3SMing Lei struct inode *inode = mapping->host; 2202e5ab5f3SMing Lei unsigned short sb_bsize = 0; 2212e5ab5f3SMing Lei unsigned dio_align = 0; 2222e5ab5f3SMing Lei bool use_dio; 2232e5ab5f3SMing Lei 2242e5ab5f3SMing Lei if (inode->i_sb->s_bdev) { 2252e5ab5f3SMing Lei sb_bsize = bdev_logical_block_size(inode->i_sb->s_bdev); 2262e5ab5f3SMing Lei dio_align = sb_bsize - 1; 2272e5ab5f3SMing Lei } 2282e5ab5f3SMing Lei 2292e5ab5f3SMing Lei /* 2302e5ab5f3SMing Lei * We support direct I/O only if lo_offset is aligned with the 2312e5ab5f3SMing Lei * logical I/O size of backing device, and the logical block 23247e96246SChristoph Hellwig * size of loop is bigger than the backing device's. 2332e5ab5f3SMing Lei * 2342e5ab5f3SMing Lei * TODO: the above condition may be loosed in the future, and 2352e5ab5f3SMing Lei * direct I/O may be switched runtime at that time because most 23689d790abSMasahiro Yamada * of requests in sane applications should be PAGE_SIZE aligned 2372e5ab5f3SMing Lei */ 2382e5ab5f3SMing Lei if (dio) { 2392e5ab5f3SMing Lei if (queue_logical_block_size(lo->lo_queue) >= sb_bsize && 2402e5ab5f3SMing Lei !(lo->lo_offset & dio_align) && 24147e96246SChristoph Hellwig mapping->a_ops->direct_IO) 2422e5ab5f3SMing Lei use_dio = true; 2432e5ab5f3SMing Lei else 2442e5ab5f3SMing Lei use_dio = false; 2452e5ab5f3SMing Lei } else { 2462e5ab5f3SMing Lei use_dio = false; 2472e5ab5f3SMing Lei } 2482e5ab5f3SMing Lei 2492e5ab5f3SMing Lei if (lo->use_dio == use_dio) 2502e5ab5f3SMing Lei return; 2512e5ab5f3SMing Lei 2522e5ab5f3SMing Lei /* flush dirty pages before changing direct IO */ 2532e5ab5f3SMing Lei vfs_fsync(file, 0); 2542e5ab5f3SMing Lei 2552e5ab5f3SMing Lei /* 2562e5ab5f3SMing Lei * The flag of LO_FLAGS_DIRECT_IO is handled similarly with 2572e5ab5f3SMing Lei * LO_FLAGS_READ_ONLY, both are set from kernel, and losetup 2582e5ab5f3SMing Lei * will get updated by ioctl(LOOP_GET_STATUS) 2592e5ab5f3SMing Lei */ 2600fbcf579SMartijn Coenen if (lo->lo_state == Lo_bound) 2612e5ab5f3SMing Lei blk_mq_freeze_queue(lo->lo_queue); 2622e5ab5f3SMing Lei lo->use_dio = use_dio; 26340326d8aSShaohua Li if (use_dio) { 2648b904b5bSBart Van Assche blk_queue_flag_clear(QUEUE_FLAG_NOMERGES, lo->lo_queue); 2652e5ab5f3SMing Lei lo->lo_flags |= LO_FLAGS_DIRECT_IO; 26640326d8aSShaohua Li } else { 2678b904b5bSBart Van Assche blk_queue_flag_set(QUEUE_FLAG_NOMERGES, lo->lo_queue); 2682e5ab5f3SMing Lei lo->lo_flags &= ~LO_FLAGS_DIRECT_IO; 26940326d8aSShaohua Li } 2700fbcf579SMartijn Coenen if (lo->lo_state == Lo_bound) 2712e5ab5f3SMing Lei blk_mq_unfreeze_queue(lo->lo_queue); 2722e5ab5f3SMing Lei } 2732e5ab5f3SMing Lei 2745795b6f5SMartijn Coenen /** 2755795b6f5SMartijn Coenen * loop_set_size() - sets device size and notifies userspace 2765795b6f5SMartijn Coenen * @lo: struct loop_device to set the size for 2775795b6f5SMartijn Coenen * @size: new size of the loop device 2785795b6f5SMartijn Coenen * 2795795b6f5SMartijn Coenen * Callers must validate that the size passed into this function fits into 2805795b6f5SMartijn Coenen * a sector_t, eg using loop_validate_size() 2815795b6f5SMartijn Coenen */ 2825795b6f5SMartijn Coenen static void loop_set_size(struct loop_device *lo, loff_t size) 2835795b6f5SMartijn Coenen { 284449f4ec9SChristoph Hellwig if (!set_capacity_and_notify(lo->lo_disk, size)) 2853b4f85d0SChristoph Hellwig kobject_uevent(&disk_to_dev(lo->lo_disk)->kobj, KOBJ_CHANGE); 2861da177e4SLinus Torvalds } 2871da177e4SLinus Torvalds 288aa4d8616SChristoph Hellwig static int lo_write_bvec(struct file *file, struct bio_vec *bvec, loff_t *ppos) 2891da177e4SLinus Torvalds { 290aa4d8616SChristoph Hellwig struct iov_iter i; 2911da177e4SLinus Torvalds ssize_t bw; 292283e7e5dSAl Viro 293b6207430SChristoph Hellwig iov_iter_bvec(&i, WRITE, bvec, 1, bvec->bv_len); 2941da177e4SLinus Torvalds 29503d95eb2SAl Viro file_start_write(file); 296abbb6589SChristoph Hellwig bw = vfs_iter_write(file, &i, ppos, 0); 29703d95eb2SAl Viro file_end_write(file); 298aa4d8616SChristoph Hellwig 299aa4d8616SChristoph Hellwig if (likely(bw == bvec->bv_len)) 3001da177e4SLinus Torvalds return 0; 301aa4d8616SChristoph Hellwig 302aa4d8616SChristoph Hellwig printk_ratelimited(KERN_ERR 303aa4d8616SChristoph Hellwig "loop: Write error at byte offset %llu, length %i.\n", 304aa4d8616SChristoph Hellwig (unsigned long long)*ppos, bvec->bv_len); 3051da177e4SLinus Torvalds if (bw >= 0) 3061da177e4SLinus Torvalds bw = -EIO; 3071da177e4SLinus Torvalds return bw; 3081da177e4SLinus Torvalds } 3091da177e4SLinus Torvalds 310aa4d8616SChristoph Hellwig static int lo_write_simple(struct loop_device *lo, struct request *rq, 311aa4d8616SChristoph Hellwig loff_t pos) 3121da177e4SLinus Torvalds { 313aa4d8616SChristoph Hellwig struct bio_vec bvec; 314aa4d8616SChristoph Hellwig struct req_iterator iter; 315aa4d8616SChristoph Hellwig int ret = 0; 316aa4d8616SChristoph Hellwig 317aa4d8616SChristoph Hellwig rq_for_each_segment(bvec, rq, iter) { 318aa4d8616SChristoph Hellwig ret = lo_write_bvec(lo->lo_backing_file, &bvec, &pos); 319aa4d8616SChristoph Hellwig if (ret < 0) 320aa4d8616SChristoph Hellwig break; 3211da177e4SLinus Torvalds cond_resched(); 3221da177e4SLinus Torvalds } 3231da177e4SLinus Torvalds 324aa4d8616SChristoph Hellwig return ret; 325aa4d8616SChristoph Hellwig } 326aa4d8616SChristoph Hellwig 327aa4d8616SChristoph Hellwig static int lo_read_simple(struct loop_device *lo, struct request *rq, 328aa4d8616SChristoph Hellwig loff_t pos) 3291da177e4SLinus Torvalds { 3307988613bSKent Overstreet struct bio_vec bvec; 33130112013SMing Lei struct req_iterator iter; 332aa4d8616SChristoph Hellwig struct iov_iter i; 333aa4d8616SChristoph Hellwig ssize_t len; 3341da177e4SLinus Torvalds 33530112013SMing Lei rq_for_each_segment(bvec, rq, iter) { 336b6207430SChristoph Hellwig iov_iter_bvec(&i, READ, &bvec, 1, bvec.bv_len); 33718e9710eSChristoph Hellwig len = vfs_iter_read(lo->lo_backing_file, &i, &pos, 0); 338aa4d8616SChristoph Hellwig if (len < 0) 339aa4d8616SChristoph Hellwig return len; 340306df071SDave Young 341aa4d8616SChristoph Hellwig flush_dcache_page(bvec.bv_page); 342aa4d8616SChristoph Hellwig 343aa4d8616SChristoph Hellwig if (len != bvec.bv_len) { 34430112013SMing Lei struct bio *bio; 34530112013SMing Lei 34630112013SMing Lei __rq_for_each_bio(bio, rq) 347306df071SDave Young zero_fill_bio(bio); 3481da177e4SLinus Torvalds break; 349306df071SDave Young } 350aa4d8616SChristoph Hellwig cond_resched(); 3511da177e4SLinus Torvalds } 352aa4d8616SChristoph Hellwig 353306df071SDave Young return 0; 3541da177e4SLinus Torvalds } 3551da177e4SLinus Torvalds 356efcfec57SDarrick J. Wong static int lo_fallocate(struct loop_device *lo, struct request *rq, loff_t pos, 357efcfec57SDarrick J. Wong int mode) 358cf655d95SMing Lei { 359cf655d95SMing Lei /* 360efcfec57SDarrick J. Wong * We use fallocate to manipulate the space mappings used by the image 36147e96246SChristoph Hellwig * a.k.a. discard/zerorange. 362cf655d95SMing Lei */ 363cf655d95SMing Lei struct file *file = lo->lo_backing_file; 364cf655d95SMing Lei int ret; 365cf655d95SMing Lei 366efcfec57SDarrick J. Wong mode |= FALLOC_FL_KEEP_SIZE; 367efcfec57SDarrick J. Wong 36870200574SChristoph Hellwig if (!bdev_max_discard_sectors(lo->lo_device)) 36970200574SChristoph Hellwig return -EOPNOTSUPP; 370cf655d95SMing Lei 371cf655d95SMing Lei ret = file->f_op->fallocate(file, mode, pos, blk_rq_bytes(rq)); 372cf655d95SMing Lei if (unlikely(ret && ret != -EINVAL && ret != -EOPNOTSUPP)) 37370200574SChristoph Hellwig return -EIO; 374cf655d95SMing Lei return ret; 375cf655d95SMing Lei } 376cf655d95SMing Lei 377cf655d95SMing Lei static int lo_req_flush(struct loop_device *lo, struct request *rq) 378cf655d95SMing Lei { 3799c64e38cSChaitanya Kulkarni int ret = vfs_fsync(lo->lo_backing_file, 0); 380cf655d95SMing Lei if (unlikely(ret && ret != -EINVAL)) 381cf655d95SMing Lei ret = -EIO; 382cf655d95SMing Lei 383cf655d95SMing Lei return ret; 384cf655d95SMing Lei } 385cf655d95SMing Lei 386fe2cb290SChristoph Hellwig static void lo_complete_rq(struct request *rq) 387bc07c10aSMing Lei { 388fe2cb290SChristoph Hellwig struct loop_cmd *cmd = blk_mq_rq_to_pdu(rq); 389f9de14bcSJens Axboe blk_status_t ret = BLK_STS_OK; 390bc07c10aSMing Lei 391f9de14bcSJens Axboe if (!cmd->use_aio || cmd->ret < 0 || cmd->ret == blk_rq_bytes(rq) || 392f9de14bcSJens Axboe req_op(rq) != REQ_OP_READ) { 393f9de14bcSJens Axboe if (cmd->ret < 0) 3948cd55087SEvan Green ret = errno_to_blk_status(cmd->ret); 395f9de14bcSJens Axboe goto end_io; 396bc07c10aSMing Lei } 397fe2cb290SChristoph Hellwig 398f9de14bcSJens Axboe /* 399f9de14bcSJens Axboe * Short READ - if we got some data, advance our request and 400f9de14bcSJens Axboe * retry it. If we got no data, end the rest with EIO. 401f9de14bcSJens Axboe */ 402f9de14bcSJens Axboe if (cmd->ret) { 403f9de14bcSJens Axboe blk_update_request(rq, BLK_STS_OK, cmd->ret); 404f9de14bcSJens Axboe cmd->ret = 0; 405f9de14bcSJens Axboe blk_mq_requeue_request(rq, true); 406f9de14bcSJens Axboe } else { 407f9de14bcSJens Axboe if (cmd->use_aio) { 408f9de14bcSJens Axboe struct bio *bio = rq->bio; 409f9de14bcSJens Axboe 410f9de14bcSJens Axboe while (bio) { 411f9de14bcSJens Axboe zero_fill_bio(bio); 412f9de14bcSJens Axboe bio = bio->bi_next; 413f9de14bcSJens Axboe } 414f9de14bcSJens Axboe } 415f9de14bcSJens Axboe ret = BLK_STS_IOERR; 416f9de14bcSJens Axboe end_io: 417f9de14bcSJens Axboe blk_mq_end_request(rq, ret); 418f9de14bcSJens Axboe } 419bc07c10aSMing Lei } 420bc07c10aSMing Lei 42192d77332SShaohua Li static void lo_rw_aio_do_completion(struct loop_cmd *cmd) 42292d77332SShaohua Li { 4231894e916SJens Axboe struct request *rq = blk_mq_rq_from_pdu(cmd); 4241894e916SJens Axboe 42592d77332SShaohua Li if (!atomic_dec_and_test(&cmd->ref)) 42692d77332SShaohua Li return; 42792d77332SShaohua Li kfree(cmd->bvec); 42892d77332SShaohua Li cmd->bvec = NULL; 42915f73f5bSChristoph Hellwig if (likely(!blk_should_fake_timeout(rq->q))) 4301894e916SJens Axboe blk_mq_complete_request(rq); 43192d77332SShaohua Li } 43292d77332SShaohua Li 4336b19b766SJens Axboe static void lo_rw_aio_complete(struct kiocb *iocb, long ret) 434bc07c10aSMing Lei { 435bc07c10aSMing Lei struct loop_cmd *cmd = container_of(iocb, struct loop_cmd, iocb); 436bc07c10aSMing Lei 437fe2cb290SChristoph Hellwig cmd->ret = ret; 43892d77332SShaohua Li lo_rw_aio_do_completion(cmd); 439bc07c10aSMing Lei } 440bc07c10aSMing Lei 441bc07c10aSMing Lei static int lo_rw_aio(struct loop_device *lo, struct loop_cmd *cmd, 442bc07c10aSMing Lei loff_t pos, bool rw) 443bc07c10aSMing Lei { 444bc07c10aSMing Lei struct iov_iter iter; 44586af5952SMing Lei struct req_iterator rq_iter; 446bc07c10aSMing Lei struct bio_vec *bvec; 4471894e916SJens Axboe struct request *rq = blk_mq_rq_from_pdu(cmd); 44840326d8aSShaohua Li struct bio *bio = rq->bio; 449bc07c10aSMing Lei struct file *file = lo->lo_backing_file; 45086af5952SMing Lei struct bio_vec tmp; 45140326d8aSShaohua Li unsigned int offset; 45286af5952SMing Lei int nr_bvec = 0; 453bc07c10aSMing Lei int ret; 454bc07c10aSMing Lei 45586af5952SMing Lei rq_for_each_bvec(tmp, rq, rq_iter) 45686af5952SMing Lei nr_bvec++; 457bc07c10aSMing Lei 45886af5952SMing Lei if (rq->bio != rq->biotail) { 45986af5952SMing Lei 46086af5952SMing Lei bvec = kmalloc_array(nr_bvec, sizeof(struct bio_vec), 4616da2ec56SKees Cook GFP_NOIO); 46240326d8aSShaohua Li if (!bvec) 46340326d8aSShaohua Li return -EIO; 46440326d8aSShaohua Li cmd->bvec = bvec; 46540326d8aSShaohua Li 466a7297a6aSMing Lei /* 46740326d8aSShaohua Li * The bios of the request may be started from the middle of 46840326d8aSShaohua Li * the 'bvec' because of bio splitting, so we can't directly 46986af5952SMing Lei * copy bio->bi_iov_vec to new bvec. The rq_for_each_bvec 47040326d8aSShaohua Li * API will take care of all details for us. 471a7297a6aSMing Lei */ 47286af5952SMing Lei rq_for_each_bvec(tmp, rq, rq_iter) { 47340326d8aSShaohua Li *bvec = tmp; 47440326d8aSShaohua Li bvec++; 47540326d8aSShaohua Li } 47640326d8aSShaohua Li bvec = cmd->bvec; 47740326d8aSShaohua Li offset = 0; 47840326d8aSShaohua Li } else { 47940326d8aSShaohua Li /* 48040326d8aSShaohua Li * Same here, this bio may be started from the middle of the 48140326d8aSShaohua Li * 'bvec' because of bio splitting, so offset from the bvec 48240326d8aSShaohua Li * must be passed to iov iterator 48340326d8aSShaohua Li */ 48440326d8aSShaohua Li offset = bio->bi_iter.bi_bvec_done; 48540326d8aSShaohua Li bvec = __bvec_iter_bvec(bio->bi_io_vec, bio->bi_iter); 48640326d8aSShaohua Li } 48792d77332SShaohua Li atomic_set(&cmd->ref, 2); 48840326d8aSShaohua Li 489b6207430SChristoph Hellwig iov_iter_bvec(&iter, rw, bvec, nr_bvec, blk_rq_bytes(rq)); 49040326d8aSShaohua Li iter.iov_offset = offset; 491bc07c10aSMing Lei 492bc07c10aSMing Lei cmd->iocb.ki_pos = pos; 493bc07c10aSMing Lei cmd->iocb.ki_filp = file; 494bc07c10aSMing Lei cmd->iocb.ki_complete = lo_rw_aio_complete; 495bc07c10aSMing Lei cmd->iocb.ki_flags = IOCB_DIRECT; 496d9a08a9eSAdam Manzanares cmd->iocb.ki_ioprio = IOPRIO_PRIO_VALUE(IOPRIO_CLASS_NONE, 0); 497bc07c10aSMing Lei 498bc07c10aSMing Lei if (rw == WRITE) 499bb7462b6SMiklos Szeredi ret = call_write_iter(file, &cmd->iocb, &iter); 500bc07c10aSMing Lei else 501bb7462b6SMiklos Szeredi ret = call_read_iter(file, &cmd->iocb, &iter); 502bc07c10aSMing Lei 50392d77332SShaohua Li lo_rw_aio_do_completion(cmd); 50492d77332SShaohua Li 505bc07c10aSMing Lei if (ret != -EIOCBQUEUED) 5066b19b766SJens Axboe lo_rw_aio_complete(&cmd->iocb, ret); 507bc07c10aSMing Lei return 0; 508bc07c10aSMing Lei } 509bc07c10aSMing Lei 510c1c87c2bSChristoph Hellwig static int do_req_filebacked(struct loop_device *lo, struct request *rq) 511bc07c10aSMing Lei { 512bc07c10aSMing Lei struct loop_cmd *cmd = blk_mq_rq_to_pdu(rq); 513c1c87c2bSChristoph Hellwig loff_t pos = ((loff_t) blk_rq_pos(rq) << 9) + lo->lo_offset; 514bc07c10aSMing Lei 515bc07c10aSMing Lei /* 516bc07c10aSMing Lei * lo_write_simple and lo_read_simple should have been covered 517bc07c10aSMing Lei * by io submit style function like lo_rw_aio(), one blocker 518bc07c10aSMing Lei * is that lo_read_simple() need to call flush_dcache_page after 519bc07c10aSMing Lei * the page is written from kernel, and it isn't easy to handle 520bc07c10aSMing Lei * this in io submit style function which submits all segments 521bc07c10aSMing Lei * of the req at one time. And direct read IO doesn't need to 522bc07c10aSMing Lei * run flush_dcache_page(). 523bc07c10aSMing Lei */ 524c1c87c2bSChristoph Hellwig switch (req_op(rq)) { 525c1c87c2bSChristoph Hellwig case REQ_OP_FLUSH: 526c1c87c2bSChristoph Hellwig return lo_req_flush(lo, rq); 52719372e27SChristoph Hellwig case REQ_OP_WRITE_ZEROES: 528efcfec57SDarrick J. Wong /* 529efcfec57SDarrick J. Wong * If the caller doesn't want deallocation, call zeroout to 530efcfec57SDarrick J. Wong * write zeroes the range. Otherwise, punch them out. 531efcfec57SDarrick J. Wong */ 532efcfec57SDarrick J. Wong return lo_fallocate(lo, rq, pos, 533efcfec57SDarrick J. Wong (rq->cmd_flags & REQ_NOUNMAP) ? 534efcfec57SDarrick J. Wong FALLOC_FL_ZERO_RANGE : 535efcfec57SDarrick J. Wong FALLOC_FL_PUNCH_HOLE); 536efcfec57SDarrick J. Wong case REQ_OP_DISCARD: 537efcfec57SDarrick J. Wong return lo_fallocate(lo, rq, pos, FALLOC_FL_PUNCH_HOLE); 538c1c87c2bSChristoph Hellwig case REQ_OP_WRITE: 53947e96246SChristoph Hellwig if (cmd->use_aio) 540c1c87c2bSChristoph Hellwig return lo_rw_aio(lo, cmd, pos, WRITE); 541c1c87c2bSChristoph Hellwig else 542bc07c10aSMing Lei return lo_write_simple(lo, rq, pos); 543c1c87c2bSChristoph Hellwig case REQ_OP_READ: 54447e96246SChristoph Hellwig if (cmd->use_aio) 545c1c87c2bSChristoph Hellwig return lo_rw_aio(lo, cmd, pos, READ); 546bc07c10aSMing Lei else 547bc07c10aSMing Lei return lo_read_simple(lo, rq, pos); 548c1c87c2bSChristoph Hellwig default: 549c1c87c2bSChristoph Hellwig WARN_ON_ONCE(1); 550c1c87c2bSChristoph Hellwig return -EIO; 551bc07c10aSMing Lei } 5521da177e4SLinus Torvalds } 5531da177e4SLinus Torvalds 5542e5ab5f3SMing Lei static inline void loop_update_dio(struct loop_device *lo) 5552e5ab5f3SMing Lei { 556efbe3c24SIra Weiny __loop_update_dio(lo, (lo->lo_backing_file->f_flags & O_DIRECT) | 5572e5ab5f3SMing Lei lo->use_dio); 5582e5ab5f3SMing Lei } 5592e5ab5f3SMing Lei 5600384264eSChristoph Hellwig static void loop_reread_partitions(struct loop_device *lo) 56106f0e9e6SMing Lei { 56206f0e9e6SMing Lei int rc; 56306f0e9e6SMing Lei 5640384264eSChristoph Hellwig mutex_lock(&lo->lo_disk->open_mutex); 5650384264eSChristoph Hellwig rc = bdev_disk_changed(lo->lo_disk, false); 5660384264eSChristoph Hellwig mutex_unlock(&lo->lo_disk->open_mutex); 56706f0e9e6SMing Lei if (rc) 56806f0e9e6SMing Lei pr_warn("%s: partition scan of loop%d (%s) failed (rc=%d)\n", 56906f0e9e6SMing Lei __func__, lo->lo_number, lo->lo_file_name, rc); 57006f0e9e6SMing Lei } 57106f0e9e6SMing Lei 572d2ac838eSTheodore Ts'o static inline int is_loop_device(struct file *file) 573d2ac838eSTheodore Ts'o { 574d2ac838eSTheodore Ts'o struct inode *i = file->f_mapping->host; 575d2ac838eSTheodore Ts'o 5766f24784fSAl Viro return i && S_ISBLK(i->i_mode) && imajor(i) == LOOP_MAJOR; 577d2ac838eSTheodore Ts'o } 578d2ac838eSTheodore Ts'o 579d2ac838eSTheodore Ts'o static int loop_validate_file(struct file *file, struct block_device *bdev) 580d2ac838eSTheodore Ts'o { 581d2ac838eSTheodore Ts'o struct inode *inode = file->f_mapping->host; 582d2ac838eSTheodore Ts'o struct file *f = file; 583d2ac838eSTheodore Ts'o 584d2ac838eSTheodore Ts'o /* Avoid recursion */ 585d2ac838eSTheodore Ts'o while (is_loop_device(f)) { 586d2ac838eSTheodore Ts'o struct loop_device *l; 587d2ac838eSTheodore Ts'o 5883ce6e1f6STetsuo Handa lockdep_assert_held(&loop_validate_mutex); 5894e7b5671SChristoph Hellwig if (f->f_mapping->host->i_rdev == bdev->bd_dev) 590d2ac838eSTheodore Ts'o return -EBADF; 591d2ac838eSTheodore Ts'o 5924e7b5671SChristoph Hellwig l = I_BDEV(f->f_mapping->host)->bd_disk->private_data; 5933ce6e1f6STetsuo Handa if (l->lo_state != Lo_bound) 594d2ac838eSTheodore Ts'o return -EINVAL; 5953ce6e1f6STetsuo Handa /* Order wrt setting lo->lo_backing_file in loop_configure(). */ 5963ce6e1f6STetsuo Handa rmb(); 597d2ac838eSTheodore Ts'o f = l->lo_backing_file; 598d2ac838eSTheodore Ts'o } 599d2ac838eSTheodore Ts'o if (!S_ISREG(inode->i_mode) && !S_ISBLK(inode->i_mode)) 600d2ac838eSTheodore Ts'o return -EINVAL; 601d2ac838eSTheodore Ts'o return 0; 602d2ac838eSTheodore Ts'o } 603d2ac838eSTheodore Ts'o 6041da177e4SLinus Torvalds /* 6051da177e4SLinus Torvalds * loop_change_fd switched the backing store of a loopback device to 6061da177e4SLinus Torvalds * a new file. This is useful for operating system installers to free up 6071da177e4SLinus Torvalds * the original file and in High Availability environments to switch to 6081da177e4SLinus Torvalds * an alternative location for the content in case of server meltdown. 6091da177e4SLinus Torvalds * This can only work if the loop device is used read-only, and if the 6101da177e4SLinus Torvalds * new backing store is the same size and type as the old backing store. 6111da177e4SLinus Torvalds */ 612bb214884SAl Viro static int loop_change_fd(struct loop_device *lo, struct block_device *bdev, 613bb214884SAl Viro unsigned int arg) 6141da177e4SLinus Torvalds { 6153ce6e1f6STetsuo Handa struct file *file = fget(arg); 6163ce6e1f6STetsuo Handa struct file *old_file; 6171da177e4SLinus Torvalds int error; 61885b0a54aSJan Kara bool partscan; 6193ce6e1f6STetsuo Handa bool is_loop; 6201da177e4SLinus Torvalds 6213ce6e1f6STetsuo Handa if (!file) 6223ce6e1f6STetsuo Handa return -EBADF; 623498ef5c7SChristoph Hellwig 624498ef5c7SChristoph Hellwig /* suppress uevents while reconfiguring the device */ 625498ef5c7SChristoph Hellwig dev_set_uevent_suppress(disk_to_dev(lo->lo_disk), 1); 626498ef5c7SChristoph Hellwig 6273ce6e1f6STetsuo Handa is_loop = is_loop_device(file); 6283ce6e1f6STetsuo Handa error = loop_global_lock_killable(lo, is_loop); 629c3710770SJan Kara if (error) 6303ce6e1f6STetsuo Handa goto out_putf; 6311da177e4SLinus Torvalds error = -ENXIO; 6321da177e4SLinus Torvalds if (lo->lo_state != Lo_bound) 6331dded9acSJan Kara goto out_err; 6341da177e4SLinus Torvalds 6351da177e4SLinus Torvalds /* the loop device has to be read-only */ 6361da177e4SLinus Torvalds error = -EINVAL; 6371da177e4SLinus Torvalds if (!(lo->lo_flags & LO_FLAGS_READ_ONLY)) 6381dded9acSJan Kara goto out_err; 6391da177e4SLinus Torvalds 640d2ac838eSTheodore Ts'o error = loop_validate_file(file, bdev); 641d2ac838eSTheodore Ts'o if (error) 6421dded9acSJan Kara goto out_err; 643d2ac838eSTheodore Ts'o 6441da177e4SLinus Torvalds old_file = lo->lo_backing_file; 6451da177e4SLinus Torvalds 6461da177e4SLinus Torvalds error = -EINVAL; 6471da177e4SLinus Torvalds 6481da177e4SLinus Torvalds /* size of the new backing store needs to be the same */ 6491da177e4SLinus Torvalds if (get_loop_size(lo, file) != get_loop_size(lo, old_file)) 6501dded9acSJan Kara goto out_err; 6511da177e4SLinus Torvalds 6521da177e4SLinus Torvalds /* and ... switch */ 6539f65c489SMatteo Croce disk_force_media_change(lo->lo_disk, DISK_EVENT_MEDIA_CHANGE); 65443cade80SOmar Sandoval blk_mq_freeze_queue(lo->lo_queue); 65543cade80SOmar Sandoval mapping_set_gfp_mask(old_file->f_mapping, lo->old_gfp_mask); 65643cade80SOmar Sandoval lo->lo_backing_file = file; 65743cade80SOmar Sandoval lo->old_gfp_mask = mapping_gfp_mask(file->f_mapping); 65843cade80SOmar Sandoval mapping_set_gfp_mask(file->f_mapping, 65943cade80SOmar Sandoval lo->old_gfp_mask & ~(__GFP_IO|__GFP_FS)); 66043cade80SOmar Sandoval loop_update_dio(lo); 66143cade80SOmar Sandoval blk_mq_unfreeze_queue(lo->lo_queue); 66285b0a54aSJan Kara partscan = lo->lo_flags & LO_FLAGS_PARTSCAN; 6633ce6e1f6STetsuo Handa loop_global_unlock(lo, is_loop); 6643ce6e1f6STetsuo Handa 6653ce6e1f6STetsuo Handa /* 6663ce6e1f6STetsuo Handa * Flush loop_validate_file() before fput(), for l->lo_backing_file 6673ce6e1f6STetsuo Handa * might be pointing at old_file which might be the last reference. 6683ce6e1f6STetsuo Handa */ 6693ce6e1f6STetsuo Handa if (!is_loop) { 6703ce6e1f6STetsuo Handa mutex_lock(&loop_validate_mutex); 6713ce6e1f6STetsuo Handa mutex_unlock(&loop_validate_mutex); 6723ce6e1f6STetsuo Handa } 6731dded9acSJan Kara /* 6746cc8e743SPavel Tatashin * We must drop file reference outside of lo_mutex as dropping 675a8698707SChristoph Hellwig * the file ref can take open_mutex which creates circular locking 6761dded9acSJan Kara * dependency. 6771dded9acSJan Kara */ 6781dded9acSJan Kara fput(old_file); 67985b0a54aSJan Kara if (partscan) 6800384264eSChristoph Hellwig loop_reread_partitions(lo); 681498ef5c7SChristoph Hellwig 682498ef5c7SChristoph Hellwig error = 0; 683498ef5c7SChristoph Hellwig done: 684498ef5c7SChristoph Hellwig /* enable and uncork uevent now that we are done */ 685498ef5c7SChristoph Hellwig dev_set_uevent_suppress(disk_to_dev(lo->lo_disk), 0); 686498ef5c7SChristoph Hellwig return error; 6871da177e4SLinus Torvalds 6881dded9acSJan Kara out_err: 6893ce6e1f6STetsuo Handa loop_global_unlock(lo, is_loop); 6903ce6e1f6STetsuo Handa out_putf: 6911dded9acSJan Kara fput(file); 692498ef5c7SChristoph Hellwig goto done; 6931da177e4SLinus Torvalds } 6941da177e4SLinus Torvalds 695ee862730SMilan Broz /* loop sysfs attributes */ 696ee862730SMilan Broz 697ee862730SMilan Broz static ssize_t loop_attr_show(struct device *dev, char *page, 698ee862730SMilan Broz ssize_t (*callback)(struct loop_device *, char *)) 699ee862730SMilan Broz { 70034dd82afSKay Sievers struct gendisk *disk = dev_to_disk(dev); 70134dd82afSKay Sievers struct loop_device *lo = disk->private_data; 702ee862730SMilan Broz 70334dd82afSKay Sievers return callback(lo, page); 704ee862730SMilan Broz } 705ee862730SMilan Broz 706ee862730SMilan Broz #define LOOP_ATTR_RO(_name) \ 707ee862730SMilan Broz static ssize_t loop_attr_##_name##_show(struct loop_device *, char *); \ 708ee862730SMilan Broz static ssize_t loop_attr_do_show_##_name(struct device *d, \ 709ee862730SMilan Broz struct device_attribute *attr, char *b) \ 710ee862730SMilan Broz { \ 711ee862730SMilan Broz return loop_attr_show(d, b, loop_attr_##_name##_show); \ 712ee862730SMilan Broz } \ 713ee862730SMilan Broz static struct device_attribute loop_attr_##_name = \ 7145657a819SJoe Perches __ATTR(_name, 0444, loop_attr_do_show_##_name, NULL); 715ee862730SMilan Broz 716ee862730SMilan Broz static ssize_t loop_attr_backing_file_show(struct loop_device *lo, char *buf) 717ee862730SMilan Broz { 718ee862730SMilan Broz ssize_t ret; 719ee862730SMilan Broz char *p = NULL; 720ee862730SMilan Broz 72105eb0f25SKay Sievers spin_lock_irq(&lo->lo_lock); 722ee862730SMilan Broz if (lo->lo_backing_file) 7239bf39ab2SMiklos Szeredi p = file_path(lo->lo_backing_file, buf, PAGE_SIZE - 1); 72405eb0f25SKay Sievers spin_unlock_irq(&lo->lo_lock); 725ee862730SMilan Broz 726ee862730SMilan Broz if (IS_ERR_OR_NULL(p)) 727ee862730SMilan Broz ret = PTR_ERR(p); 728ee862730SMilan Broz else { 729ee862730SMilan Broz ret = strlen(p); 730ee862730SMilan Broz memmove(buf, p, ret); 731ee862730SMilan Broz buf[ret++] = '\n'; 732ee862730SMilan Broz buf[ret] = 0; 733ee862730SMilan Broz } 734ee862730SMilan Broz 735ee862730SMilan Broz return ret; 736ee862730SMilan Broz } 737ee862730SMilan Broz 738ee862730SMilan Broz static ssize_t loop_attr_offset_show(struct loop_device *lo, char *buf) 739ee862730SMilan Broz { 740b27824d3SChaitanya Kulkarni return sysfs_emit(buf, "%llu\n", (unsigned long long)lo->lo_offset); 741ee862730SMilan Broz } 742ee862730SMilan Broz 743ee862730SMilan Broz static ssize_t loop_attr_sizelimit_show(struct loop_device *lo, char *buf) 744ee862730SMilan Broz { 745b27824d3SChaitanya Kulkarni return sysfs_emit(buf, "%llu\n", (unsigned long long)lo->lo_sizelimit); 746ee862730SMilan Broz } 747ee862730SMilan Broz 748ee862730SMilan Broz static ssize_t loop_attr_autoclear_show(struct loop_device *lo, char *buf) 749ee862730SMilan Broz { 750ee862730SMilan Broz int autoclear = (lo->lo_flags & LO_FLAGS_AUTOCLEAR); 751ee862730SMilan Broz 752b27824d3SChaitanya Kulkarni return sysfs_emit(buf, "%s\n", autoclear ? "1" : "0"); 753ee862730SMilan Broz } 754ee862730SMilan Broz 755e03c8dd1SKay Sievers static ssize_t loop_attr_partscan_show(struct loop_device *lo, char *buf) 756e03c8dd1SKay Sievers { 757e03c8dd1SKay Sievers int partscan = (lo->lo_flags & LO_FLAGS_PARTSCAN); 758e03c8dd1SKay Sievers 759b27824d3SChaitanya Kulkarni return sysfs_emit(buf, "%s\n", partscan ? "1" : "0"); 760e03c8dd1SKay Sievers } 761e03c8dd1SKay Sievers 7622e5ab5f3SMing Lei static ssize_t loop_attr_dio_show(struct loop_device *lo, char *buf) 7632e5ab5f3SMing Lei { 7642e5ab5f3SMing Lei int dio = (lo->lo_flags & LO_FLAGS_DIRECT_IO); 7652e5ab5f3SMing Lei 766b27824d3SChaitanya Kulkarni return sysfs_emit(buf, "%s\n", dio ? "1" : "0"); 7672e5ab5f3SMing Lei } 7682e5ab5f3SMing Lei 769ee862730SMilan Broz LOOP_ATTR_RO(backing_file); 770ee862730SMilan Broz LOOP_ATTR_RO(offset); 771ee862730SMilan Broz LOOP_ATTR_RO(sizelimit); 772ee862730SMilan Broz LOOP_ATTR_RO(autoclear); 773e03c8dd1SKay Sievers LOOP_ATTR_RO(partscan); 7742e5ab5f3SMing Lei LOOP_ATTR_RO(dio); 775ee862730SMilan Broz 776ee862730SMilan Broz static struct attribute *loop_attrs[] = { 777ee862730SMilan Broz &loop_attr_backing_file.attr, 778ee862730SMilan Broz &loop_attr_offset.attr, 779ee862730SMilan Broz &loop_attr_sizelimit.attr, 780ee862730SMilan Broz &loop_attr_autoclear.attr, 781e03c8dd1SKay Sievers &loop_attr_partscan.attr, 7822e5ab5f3SMing Lei &loop_attr_dio.attr, 783ee862730SMilan Broz NULL, 784ee862730SMilan Broz }; 785ee862730SMilan Broz 786ee862730SMilan Broz static struct attribute_group loop_attribute_group = { 787ee862730SMilan Broz .name = "loop", 788ee862730SMilan Broz .attrs= loop_attrs, 789ee862730SMilan Broz }; 790ee862730SMilan Broz 791d3349b6bSTetsuo Handa static void loop_sysfs_init(struct loop_device *lo) 792ee862730SMilan Broz { 793d3349b6bSTetsuo Handa lo->sysfs_inited = !sysfs_create_group(&disk_to_dev(lo->lo_disk)->kobj, 794ee862730SMilan Broz &loop_attribute_group); 795ee862730SMilan Broz } 796ee862730SMilan Broz 797ee862730SMilan Broz static void loop_sysfs_exit(struct loop_device *lo) 798ee862730SMilan Broz { 799d3349b6bSTetsuo Handa if (lo->sysfs_inited) 800ee862730SMilan Broz sysfs_remove_group(&disk_to_dev(lo->lo_disk)->kobj, 801ee862730SMilan Broz &loop_attribute_group); 802ee862730SMilan Broz } 803ee862730SMilan Broz 804dfaa2ef6SLukas Czerner static void loop_config_discard(struct loop_device *lo) 805dfaa2ef6SLukas Czerner { 806dfaa2ef6SLukas Czerner struct file *file = lo->lo_backing_file; 807dfaa2ef6SLukas Czerner struct inode *inode = file->f_mapping->host; 808dfaa2ef6SLukas Czerner struct request_queue *q = lo->lo_queue; 809bcb21c8cSMing Lei u32 granularity, max_discard_sectors; 810dfaa2ef6SLukas Czerner 811dfaa2ef6SLukas Czerner /* 812c52abf56SEvan Green * If the backing device is a block device, mirror its zeroing 813c52abf56SEvan Green * capability. Set the discard sectors to the block device's zeroing 814c52abf56SEvan Green * capabilities because loop discards result in blkdev_issue_zeroout(), 815c52abf56SEvan Green * not blkdev_issue_discard(). This maintains consistent behavior with 816c52abf56SEvan Green * file-backed loop devices: discarded regions read back as zero. 817c52abf56SEvan Green */ 81847e96246SChristoph Hellwig if (S_ISBLK(inode->i_mode)) { 8194e7b5671SChristoph Hellwig struct request_queue *backingq = bdev_get_queue(I_BDEV(inode)); 820c52abf56SEvan Green 821bcb21c8cSMing Lei max_discard_sectors = backingq->limits.max_write_zeroes_sectors; 8227b47ef52SChristoph Hellwig granularity = bdev_discard_granularity(I_BDEV(inode)) ?: 823bcb21c8cSMing Lei queue_physical_block_size(backingq); 824c52abf56SEvan Green 825c52abf56SEvan Green /* 826dfaa2ef6SLukas Czerner * We use punch hole to reclaim the free space used by the 82747e96246SChristoph Hellwig * image a.k.a. discard. 828dfaa2ef6SLukas Czerner */ 82947e96246SChristoph Hellwig } else if (!file->f_op->fallocate) { 830bcb21c8cSMing Lei max_discard_sectors = 0; 831bcb21c8cSMing Lei granularity = 0; 832dfaa2ef6SLukas Czerner 833c52abf56SEvan Green } else { 83406582bc8SMing Lei struct kstatfs sbuf; 83506582bc8SMing Lei 836bcb21c8cSMing Lei max_discard_sectors = UINT_MAX >> 9; 83706582bc8SMing Lei if (!vfs_statfs(&file->f_path, &sbuf)) 83806582bc8SMing Lei granularity = sbuf.f_bsize; 83906582bc8SMing Lei else 84006582bc8SMing Lei max_discard_sectors = 0; 841c52abf56SEvan Green } 842c52abf56SEvan Green 843bcb21c8cSMing Lei if (max_discard_sectors) { 844bcb21c8cSMing Lei q->limits.discard_granularity = granularity; 845bcb21c8cSMing Lei blk_queue_max_discard_sectors(q, max_discard_sectors); 846bcb21c8cSMing Lei blk_queue_max_write_zeroes_sectors(q, max_discard_sectors); 847bcb21c8cSMing Lei } else { 848bcb21c8cSMing Lei q->limits.discard_granularity = 0; 849bcb21c8cSMing Lei blk_queue_max_discard_sectors(q, 0); 850bcb21c8cSMing Lei blk_queue_max_write_zeroes_sectors(q, 0); 851dfaa2ef6SLukas Czerner } 852bcb21c8cSMing Lei } 853dfaa2ef6SLukas Czerner 85487579e9bSDan Schatzberg struct loop_worker { 85587579e9bSDan Schatzberg struct rb_node rb_node; 85687579e9bSDan Schatzberg struct work_struct work; 85787579e9bSDan Schatzberg struct list_head cmd_list; 85887579e9bSDan Schatzberg struct list_head idle_list; 85987579e9bSDan Schatzberg struct loop_device *lo; 860c74d40e8SDan Schatzberg struct cgroup_subsys_state *blkcg_css; 86187579e9bSDan Schatzberg unsigned long last_ran_at; 86287579e9bSDan Schatzberg }; 863e03a3d7aSMing Lei 86487579e9bSDan Schatzberg static void loop_workfn(struct work_struct *work); 865b2ee7d46SNeilBrown 86687579e9bSDan Schatzberg #ifdef CONFIG_BLK_CGROUP 86787579e9bSDan Schatzberg static inline int queue_on_root_worker(struct cgroup_subsys_state *css) 868e03a3d7aSMing Lei { 86987579e9bSDan Schatzberg return !css || css == blkcg_root_css; 87087579e9bSDan Schatzberg } 87187579e9bSDan Schatzberg #else 87287579e9bSDan Schatzberg static inline int queue_on_root_worker(struct cgroup_subsys_state *css) 87387579e9bSDan Schatzberg { 87487579e9bSDan Schatzberg return !css; 87587579e9bSDan Schatzberg } 87687579e9bSDan Schatzberg #endif 87787579e9bSDan Schatzberg 87887579e9bSDan Schatzberg static void loop_queue_work(struct loop_device *lo, struct loop_cmd *cmd) 87987579e9bSDan Schatzberg { 880413ec805SColin Ian King struct rb_node **node, *parent = NULL; 88187579e9bSDan Schatzberg struct loop_worker *cur_worker, *worker = NULL; 88287579e9bSDan Schatzberg struct work_struct *work; 88387579e9bSDan Schatzberg struct list_head *cmd_list; 88487579e9bSDan Schatzberg 88587579e9bSDan Schatzberg spin_lock_irq(&lo->lo_work_lock); 88687579e9bSDan Schatzberg 887c74d40e8SDan Schatzberg if (queue_on_root_worker(cmd->blkcg_css)) 88887579e9bSDan Schatzberg goto queue_work; 88987579e9bSDan Schatzberg 89087579e9bSDan Schatzberg node = &lo->worker_tree.rb_node; 89187579e9bSDan Schatzberg 89287579e9bSDan Schatzberg while (*node) { 89387579e9bSDan Schatzberg parent = *node; 89487579e9bSDan Schatzberg cur_worker = container_of(*node, struct loop_worker, rb_node); 895c74d40e8SDan Schatzberg if (cur_worker->blkcg_css == cmd->blkcg_css) { 89687579e9bSDan Schatzberg worker = cur_worker; 89787579e9bSDan Schatzberg break; 898c74d40e8SDan Schatzberg } else if ((long)cur_worker->blkcg_css < (long)cmd->blkcg_css) { 89987579e9bSDan Schatzberg node = &(*node)->rb_left; 90087579e9bSDan Schatzberg } else { 90187579e9bSDan Schatzberg node = &(*node)->rb_right; 90287579e9bSDan Schatzberg } 90387579e9bSDan Schatzberg } 90487579e9bSDan Schatzberg if (worker) 90587579e9bSDan Schatzberg goto queue_work; 90687579e9bSDan Schatzberg 90787579e9bSDan Schatzberg worker = kzalloc(sizeof(struct loop_worker), GFP_NOWAIT | __GFP_NOWARN); 90887579e9bSDan Schatzberg /* 90987579e9bSDan Schatzberg * In the event we cannot allocate a worker, just queue on the 910c74d40e8SDan Schatzberg * rootcg worker and issue the I/O as the rootcg 91187579e9bSDan Schatzberg */ 912c74d40e8SDan Schatzberg if (!worker) { 913c74d40e8SDan Schatzberg cmd->blkcg_css = NULL; 914c74d40e8SDan Schatzberg if (cmd->memcg_css) 915c74d40e8SDan Schatzberg css_put(cmd->memcg_css); 916c74d40e8SDan Schatzberg cmd->memcg_css = NULL; 91787579e9bSDan Schatzberg goto queue_work; 918c74d40e8SDan Schatzberg } 91987579e9bSDan Schatzberg 920c74d40e8SDan Schatzberg worker->blkcg_css = cmd->blkcg_css; 921c74d40e8SDan Schatzberg css_get(worker->blkcg_css); 92287579e9bSDan Schatzberg INIT_WORK(&worker->work, loop_workfn); 92387579e9bSDan Schatzberg INIT_LIST_HEAD(&worker->cmd_list); 92487579e9bSDan Schatzberg INIT_LIST_HEAD(&worker->idle_list); 92587579e9bSDan Schatzberg worker->lo = lo; 92687579e9bSDan Schatzberg rb_link_node(&worker->rb_node, parent, node); 92787579e9bSDan Schatzberg rb_insert_color(&worker->rb_node, &lo->worker_tree); 92887579e9bSDan Schatzberg queue_work: 92987579e9bSDan Schatzberg if (worker) { 93087579e9bSDan Schatzberg /* 93187579e9bSDan Schatzberg * We need to remove from the idle list here while 93287579e9bSDan Schatzberg * holding the lock so that the idle timer doesn't 93387579e9bSDan Schatzberg * free the worker 93487579e9bSDan Schatzberg */ 93587579e9bSDan Schatzberg if (!list_empty(&worker->idle_list)) 93687579e9bSDan Schatzberg list_del_init(&worker->idle_list); 93787579e9bSDan Schatzberg work = &worker->work; 93887579e9bSDan Schatzberg cmd_list = &worker->cmd_list; 93987579e9bSDan Schatzberg } else { 94087579e9bSDan Schatzberg work = &lo->rootcg_work; 94187579e9bSDan Schatzberg cmd_list = &lo->rootcg_cmd_list; 94287579e9bSDan Schatzberg } 94387579e9bSDan Schatzberg list_add_tail(&cmd->list_entry, cmd_list); 94487579e9bSDan Schatzberg queue_work(lo->workqueue, work); 94587579e9bSDan Schatzberg spin_unlock_irq(&lo->lo_work_lock); 946e03a3d7aSMing Lei } 947e03a3d7aSMing Lei 9482cf429b5SChristoph Hellwig static void loop_set_timer(struct loop_device *lo) 9492cf429b5SChristoph Hellwig { 9502cf429b5SChristoph Hellwig timer_reduce(&lo->timer, jiffies + LOOP_IDLE_WORKER_TIMEOUT); 9512cf429b5SChristoph Hellwig } 9522cf429b5SChristoph Hellwig 9532cf429b5SChristoph Hellwig static void loop_free_idle_workers(struct loop_device *lo, bool delete_all) 9542cf429b5SChristoph Hellwig { 9552cf429b5SChristoph Hellwig struct loop_worker *pos, *worker; 9562cf429b5SChristoph Hellwig 9572cf429b5SChristoph Hellwig spin_lock_irq(&lo->lo_work_lock); 9582cf429b5SChristoph Hellwig list_for_each_entry_safe(worker, pos, &lo->idle_worker_list, 9592cf429b5SChristoph Hellwig idle_list) { 9602cf429b5SChristoph Hellwig if (!delete_all && 9612cf429b5SChristoph Hellwig time_is_after_jiffies(worker->last_ran_at + 9622cf429b5SChristoph Hellwig LOOP_IDLE_WORKER_TIMEOUT)) 9632cf429b5SChristoph Hellwig break; 9642cf429b5SChristoph Hellwig list_del(&worker->idle_list); 9652cf429b5SChristoph Hellwig rb_erase(&worker->rb_node, &lo->worker_tree); 9662cf429b5SChristoph Hellwig css_put(worker->blkcg_css); 9672cf429b5SChristoph Hellwig kfree(worker); 9682cf429b5SChristoph Hellwig } 9692cf429b5SChristoph Hellwig if (!list_empty(&lo->idle_worker_list)) 9702cf429b5SChristoph Hellwig loop_set_timer(lo); 9712cf429b5SChristoph Hellwig spin_unlock_irq(&lo->lo_work_lock); 9722cf429b5SChristoph Hellwig } 9732cf429b5SChristoph Hellwig 9742cf429b5SChristoph Hellwig static void loop_free_idle_workers_timer(struct timer_list *timer) 9752cf429b5SChristoph Hellwig { 9762cf429b5SChristoph Hellwig struct loop_device *lo = container_of(timer, struct loop_device, timer); 9772cf429b5SChristoph Hellwig 9782cf429b5SChristoph Hellwig return loop_free_idle_workers(lo, false); 9792cf429b5SChristoph Hellwig } 9802cf429b5SChristoph Hellwig 98156a85fd8SHolger Hoffstätte static void loop_update_rotational(struct loop_device *lo) 98256a85fd8SHolger Hoffstätte { 98356a85fd8SHolger Hoffstätte struct file *file = lo->lo_backing_file; 98456a85fd8SHolger Hoffstätte struct inode *file_inode = file->f_mapping->host; 98556a85fd8SHolger Hoffstätte struct block_device *file_bdev = file_inode->i_sb->s_bdev; 98656a85fd8SHolger Hoffstätte struct request_queue *q = lo->lo_queue; 98756a85fd8SHolger Hoffstätte bool nonrot = true; 98856a85fd8SHolger Hoffstätte 98956a85fd8SHolger Hoffstätte /* not all filesystems (e.g. tmpfs) have a sb->s_bdev */ 99056a85fd8SHolger Hoffstätte if (file_bdev) 99110f0d2a5SChristoph Hellwig nonrot = bdev_nonrot(file_bdev); 99256a85fd8SHolger Hoffstätte 99356a85fd8SHolger Hoffstätte if (nonrot) 99456a85fd8SHolger Hoffstätte blk_queue_flag_set(QUEUE_FLAG_NONROT, q); 99556a85fd8SHolger Hoffstätte else 99656a85fd8SHolger Hoffstätte blk_queue_flag_clear(QUEUE_FLAG_NONROT, q); 99756a85fd8SHolger Hoffstätte } 99856a85fd8SHolger Hoffstätte 99962ab466cSMartijn Coenen /** 100062ab466cSMartijn Coenen * loop_set_status_from_info - configure device from loop_info 100162ab466cSMartijn Coenen * @lo: struct loop_device to configure 100262ab466cSMartijn Coenen * @info: struct loop_info64 to configure the device with 100362ab466cSMartijn Coenen * 100462ab466cSMartijn Coenen * Configures the loop device parameters according to the passed 100562ab466cSMartijn Coenen * in loop_info64 configuration. 100662ab466cSMartijn Coenen */ 100762ab466cSMartijn Coenen static int 100862ab466cSMartijn Coenen loop_set_status_from_info(struct loop_device *lo, 100962ab466cSMartijn Coenen const struct loop_info64 *info) 101062ab466cSMartijn Coenen { 101162ab466cSMartijn Coenen if ((unsigned int) info->lo_encrypt_key_size > LO_KEY_SIZE) 101262ab466cSMartijn Coenen return -EINVAL; 101362ab466cSMartijn Coenen 101447e96246SChristoph Hellwig switch (info->lo_encrypt_type) { 101547e96246SChristoph Hellwig case LO_CRYPT_NONE: 101647e96246SChristoph Hellwig break; 101747e96246SChristoph Hellwig case LO_CRYPT_XOR: 101847e96246SChristoph Hellwig pr_warn("support for the xor transformation has been removed.\n"); 101962ab466cSMartijn Coenen return -EINVAL; 102047e96246SChristoph Hellwig case LO_CRYPT_CRYPTOAPI: 102147e96246SChristoph Hellwig pr_warn("support for cryptoloop has been removed. Use dm-crypt instead.\n"); 102262ab466cSMartijn Coenen return -EINVAL; 102347e96246SChristoph Hellwig default: 102447e96246SChristoph Hellwig return -EINVAL; 102547e96246SChristoph Hellwig } 102662ab466cSMartijn Coenen 102762ab466cSMartijn Coenen lo->lo_offset = info->lo_offset; 102862ab466cSMartijn Coenen lo->lo_sizelimit = info->lo_sizelimit; 102962ab466cSMartijn Coenen memcpy(lo->lo_file_name, info->lo_file_name, LO_NAME_SIZE); 103062ab466cSMartijn Coenen lo->lo_file_name[LO_NAME_SIZE-1] = 0; 1031faf1d254SMartijn Coenen lo->lo_flags = info->lo_flags; 103262ab466cSMartijn Coenen return 0; 103362ab466cSMartijn Coenen } 103462ab466cSMartijn Coenen 10353448914eSMartijn Coenen static int loop_configure(struct loop_device *lo, fmode_t mode, 10363448914eSMartijn Coenen struct block_device *bdev, 10373448914eSMartijn Coenen const struct loop_config *config) 10381da177e4SLinus Torvalds { 10393ce6e1f6STetsuo Handa struct file *file = fget(config->fd); 10401da177e4SLinus Torvalds struct inode *inode; 10411da177e4SLinus Torvalds struct address_space *mapping; 10421da177e4SLinus Torvalds int error; 10431da177e4SLinus Torvalds loff_t size; 104485b0a54aSJan Kara bool partscan; 10453448914eSMartijn Coenen unsigned short bsize; 10463ce6e1f6STetsuo Handa bool is_loop; 10473ce6e1f6STetsuo Handa 10483ce6e1f6STetsuo Handa if (!file) 10493ce6e1f6STetsuo Handa return -EBADF; 10503ce6e1f6STetsuo Handa is_loop = is_loop_device(file); 10511da177e4SLinus Torvalds 10521da177e4SLinus Torvalds /* This is safe, since we have a reference from open(). */ 10531da177e4SLinus Torvalds __module_get(THIS_MODULE); 10541da177e4SLinus Torvalds 1055498ef5c7SChristoph Hellwig /* suppress uevents while reconfiguring the device */ 1056498ef5c7SChristoph Hellwig dev_set_uevent_suppress(disk_to_dev(lo->lo_disk), 1); 1057498ef5c7SChristoph Hellwig 105833ec3e53SJan Kara /* 105933ec3e53SJan Kara * If we don't hold exclusive handle for the device, upgrade to it 106033ec3e53SJan Kara * here to avoid changing device under exclusive owner. 106133ec3e53SJan Kara */ 106233ec3e53SJan Kara if (!(mode & FMODE_EXCL)) { 106337c3fc9aSChristoph Hellwig error = bd_prepare_to_claim(bdev, loop_configure); 1064ecbe6bc0SChristoph Hellwig if (error) 1065757ecf40SJan Kara goto out_putf; 106633ec3e53SJan Kara } 106733ec3e53SJan Kara 10683ce6e1f6STetsuo Handa error = loop_global_lock_killable(lo, is_loop); 106933ec3e53SJan Kara if (error) 107033ec3e53SJan Kara goto out_bdev; 1071757ecf40SJan Kara 10721da177e4SLinus Torvalds error = -EBUSY; 10731da177e4SLinus Torvalds if (lo->lo_state != Lo_unbound) 1074757ecf40SJan Kara goto out_unlock; 10751da177e4SLinus Torvalds 1076d2ac838eSTheodore Ts'o error = loop_validate_file(file, bdev); 1077d2ac838eSTheodore Ts'o if (error) 1078757ecf40SJan Kara goto out_unlock; 10791da177e4SLinus Torvalds 10801da177e4SLinus Torvalds mapping = file->f_mapping; 10811da177e4SLinus Torvalds inode = mapping->host; 10821da177e4SLinus Torvalds 10833448914eSMartijn Coenen if ((config->info.lo_flags & ~LOOP_CONFIGURE_SETTABLE_FLAGS) != 0) { 10843448914eSMartijn Coenen error = -EINVAL; 10853448914eSMartijn Coenen goto out_unlock; 10863448914eSMartijn Coenen } 10873448914eSMartijn Coenen 10883448914eSMartijn Coenen if (config->block_size) { 1089af3c570fSXie Yongji error = blk_validate_block_size(config->block_size); 10903448914eSMartijn Coenen if (error) 10913448914eSMartijn Coenen goto out_unlock; 10923448914eSMartijn Coenen } 10933448914eSMartijn Coenen 10943448914eSMartijn Coenen error = loop_set_status_from_info(lo, &config->info); 10953448914eSMartijn Coenen if (error) 10963448914eSMartijn Coenen goto out_unlock; 10973448914eSMartijn Coenen 1098456be148SChristoph Hellwig if (!(file->f_mode & FMODE_WRITE) || !(mode & FMODE_WRITE) || 1099283e7e5dSAl Viro !file->f_op->write_iter) 11003448914eSMartijn Coenen lo->lo_flags |= LO_FLAGS_READ_ONLY; 1101083a6a50SMartijn Coenen 1102d292dc80SChristoph Hellwig if (!lo->workqueue) { 110387579e9bSDan Schatzberg lo->workqueue = alloc_workqueue("loop%d", 110487579e9bSDan Schatzberg WQ_UNBOUND | WQ_FREEZABLE, 1105d292dc80SChristoph Hellwig 0, lo->lo_number); 110687579e9bSDan Schatzberg if (!lo->workqueue) { 110787579e9bSDan Schatzberg error = -ENOMEM; 1108757ecf40SJan Kara goto out_unlock; 110987579e9bSDan Schatzberg } 1110d292dc80SChristoph Hellwig } 11111da177e4SLinus Torvalds 11129f65c489SMatteo Croce disk_force_media_change(lo->lo_disk, DISK_EVENT_MEDIA_CHANGE); 11137a2f0ce1SChristoph Hellwig set_disk_ro(lo->lo_disk, (lo->lo_flags & LO_FLAGS_READ_ONLY) != 0); 11141da177e4SLinus Torvalds 11153448914eSMartijn Coenen lo->use_dio = lo->lo_flags & LO_FLAGS_DIRECT_IO; 11161da177e4SLinus Torvalds lo->lo_device = bdev; 11171da177e4SLinus Torvalds lo->lo_backing_file = file; 11181da177e4SLinus Torvalds lo->old_gfp_mask = mapping_gfp_mask(mapping); 11191da177e4SLinus Torvalds mapping_set_gfp_mask(mapping, lo->old_gfp_mask & ~(__GFP_IO|__GFP_FS)); 11201da177e4SLinus Torvalds 11213448914eSMartijn Coenen if (!(lo->lo_flags & LO_FLAGS_READ_ONLY) && file->f_op->fsync) 112221d0727fSJens Axboe blk_queue_write_cache(lo->lo_queue, true, false); 112368db1961SNikanth Karthikesan 11243448914eSMartijn Coenen if (config->block_size) 11253448914eSMartijn Coenen bsize = config->block_size; 112696ed320dSLinus Torvalds else if ((lo->lo_backing_file->f_flags & O_DIRECT) && inode->i_sb->s_bdev) 112785560117SMartijn Coenen /* In case of direct I/O, match underlying block size */ 11283448914eSMartijn Coenen bsize = bdev_logical_block_size(inode->i_sb->s_bdev); 11293448914eSMartijn Coenen else 11303448914eSMartijn Coenen bsize = 512; 113185560117SMartijn Coenen 113285560117SMartijn Coenen blk_queue_logical_block_size(lo->lo_queue, bsize); 113385560117SMartijn Coenen blk_queue_physical_block_size(lo->lo_queue, bsize); 113485560117SMartijn Coenen blk_queue_io_min(lo->lo_queue, bsize); 113585560117SMartijn Coenen 11362b9ac22bSKristian Klausen loop_config_discard(lo); 113756a85fd8SHolger Hoffstätte loop_update_rotational(lo); 11382e5ab5f3SMing Lei loop_update_dio(lo); 1139ee862730SMilan Broz loop_sysfs_init(lo); 114079e5dc59SMartijn Coenen 114179e5dc59SMartijn Coenen size = get_loop_size(lo, file); 11425795b6f5SMartijn Coenen loop_set_size(lo, size); 11431da177e4SLinus Torvalds 11443ce6e1f6STetsuo Handa /* Order wrt reading lo_state in loop_validate_file(). */ 11453ce6e1f6STetsuo Handa wmb(); 11463ce6e1f6STetsuo Handa 11476c997918SSerge E. Hallyn lo->lo_state = Lo_bound; 1148e03c8dd1SKay Sievers if (part_shift) 1149e03c8dd1SKay Sievers lo->lo_flags |= LO_FLAGS_PARTSCAN; 115085b0a54aSJan Kara partscan = lo->lo_flags & LO_FLAGS_PARTSCAN; 1151fe6a8fc5SLennart Poettering if (partscan) 115246e7eac6SChristoph Hellwig lo->lo_disk->flags &= ~GENHD_FL_NO_PART; 1153c1681bf8SAnatol Pomozov 11543ce6e1f6STetsuo Handa loop_global_unlock(lo, is_loop); 115585b0a54aSJan Kara if (partscan) 11560384264eSChristoph Hellwig loop_reread_partitions(lo); 115737c3fc9aSChristoph Hellwig if (!(mode & FMODE_EXCL)) 115837c3fc9aSChristoph Hellwig bd_abort_claiming(bdev, loop_configure); 1159498ef5c7SChristoph Hellwig 1160498ef5c7SChristoph Hellwig error = 0; 1161498ef5c7SChristoph Hellwig done: 1162498ef5c7SChristoph Hellwig /* enable and uncork uevent now that we are done */ 1163498ef5c7SChristoph Hellwig dev_set_uevent_suppress(disk_to_dev(lo->lo_disk), 0); 1164498ef5c7SChristoph Hellwig return error; 11651da177e4SLinus Torvalds 1166757ecf40SJan Kara out_unlock: 11673ce6e1f6STetsuo Handa loop_global_unlock(lo, is_loop); 116833ec3e53SJan Kara out_bdev: 116937c3fc9aSChristoph Hellwig if (!(mode & FMODE_EXCL)) 117037c3fc9aSChristoph Hellwig bd_abort_claiming(bdev, loop_configure); 11711da177e4SLinus Torvalds out_putf: 11721da177e4SLinus Torvalds fput(file); 11731da177e4SLinus Torvalds /* This is safe: open() is still holding a reference. */ 11741da177e4SLinus Torvalds module_put(THIS_MODULE); 1175498ef5c7SChristoph Hellwig goto done; 11761da177e4SLinus Torvalds } 11771da177e4SLinus Torvalds 1178bf23747eSTetsuo Handa static void __loop_clr_fd(struct loop_device *lo, bool release) 11791da177e4SLinus Torvalds { 11806050fa4cSTetsuo Handa struct file *filp; 1181b4e3ca1aSAl Viro gfp_t gfp = lo->old_gfp_mask; 11821da177e4SLinus Torvalds 11834ceddce5SMauricio Faria de Oliveira if (test_bit(QUEUE_FLAG_WC, &lo->lo_queue->queue_flags)) 11844ceddce5SMauricio Faria de Oliveira blk_queue_write_cache(lo->lo_queue, false, false); 11854ceddce5SMauricio Faria de Oliveira 11861fe0b1acSChristoph Hellwig /* 11871fe0b1acSChristoph Hellwig * Freeze the request queue when unbinding on a live file descriptor and 11881fe0b1acSChristoph Hellwig * thus an open device. When called from ->release we are guaranteed 11891fe0b1acSChristoph Hellwig * that there is no I/O in progress already. 11901fe0b1acSChristoph Hellwig */ 11911fe0b1acSChristoph Hellwig if (!release) 1192f8933667SMing Lei blk_mq_freeze_queue(lo->lo_queue); 1193f8933667SMing Lei 11941da177e4SLinus Torvalds spin_lock_irq(&lo->lo_lock); 11956050fa4cSTetsuo Handa filp = lo->lo_backing_file; 11961da177e4SLinus Torvalds lo->lo_backing_file = NULL; 119705eb0f25SKay Sievers spin_unlock_irq(&lo->lo_lock); 11981da177e4SLinus Torvalds 11991da177e4SLinus Torvalds lo->lo_device = NULL; 12001da177e4SLinus Torvalds lo->lo_offset = 0; 12011da177e4SLinus Torvalds lo->lo_sizelimit = 0; 12021da177e4SLinus Torvalds memset(lo->lo_file_name, 0, LO_NAME_SIZE); 120389e4fdecSOmar Sandoval blk_queue_logical_block_size(lo->lo_queue, 512); 1204bf093753SOmar Sandoval blk_queue_physical_block_size(lo->lo_queue, 512); 1205bf093753SOmar Sandoval blk_queue_io_min(lo->lo_queue, 512); 1206e515be8fSXie Yongji invalidate_disk(lo->lo_disk); 120751a0bb0cSMilan Broz loop_sysfs_exit(lo); 1208c3473c63SDavid Zeuthen /* let user-space know about this change */ 120919f553dbSXie Yongji kobject_uevent(&disk_to_dev(lo->lo_disk)->kobj, KOBJ_CHANGE); 12101da177e4SLinus Torvalds mapping_set_gfp_mask(filp->f_mapping, gfp); 1211bf23747eSTetsuo Handa /* This is safe: open() is still holding a reference. */ 1212bf23747eSTetsuo Handa module_put(THIS_MODULE); 12131fe0b1acSChristoph Hellwig if (!release) 1214f8933667SMing Lei blk_mq_unfreeze_queue(lo->lo_queue); 1215f8933667SMing Lei 12169f65c489SMatteo Croce disk_force_media_change(lo->lo_disk, DISK_EVENT_MEDIA_CHANGE); 12176050fa4cSTetsuo Handa 12186050fa4cSTetsuo Handa if (lo->lo_flags & LO_FLAGS_PARTSCAN) { 12196050fa4cSTetsuo Handa int err; 12206050fa4cSTetsuo Handa 1221bf23747eSTetsuo Handa /* 1222bf23747eSTetsuo Handa * open_mutex has been held already in release path, so don't 1223bf23747eSTetsuo Handa * acquire it if this function is called in such case. 1224bf23747eSTetsuo Handa * 1225bf23747eSTetsuo Handa * If the reread partition isn't from release path, lo_refcnt 1226bf23747eSTetsuo Handa * must be at least one and it can only become zero when the 1227bf23747eSTetsuo Handa * current holder is released. 1228bf23747eSTetsuo Handa */ 1229bf23747eSTetsuo Handa if (!release) 12300384264eSChristoph Hellwig mutex_lock(&lo->lo_disk->open_mutex); 12310384264eSChristoph Hellwig err = bdev_disk_changed(lo->lo_disk, false); 1232bf23747eSTetsuo Handa if (!release) 12330384264eSChristoph Hellwig mutex_unlock(&lo->lo_disk->open_mutex); 123440853d6fSDongli Zhang if (err) 1235d57f3374SJan Kara pr_warn("%s: partition scan of loop%d failed (rc=%d)\n", 12366050fa4cSTetsuo Handa __func__, lo->lo_number, err); 1237d57f3374SJan Kara /* Device is gone, no point in returning error */ 1238d57f3374SJan Kara } 1239758a58d0SDongli Zhang 1240bf23747eSTetsuo Handa /* 1241bf23747eSTetsuo Handa * lo->lo_state is set to Lo_unbound here after above partscan has 1242bf23747eSTetsuo Handa * finished. There cannot be anybody else entering __loop_clr_fd() as 1243bf23747eSTetsuo Handa * Lo_rundown state protects us from all the other places trying to 1244bf23747eSTetsuo Handa * change the 'lo' device. 1245bf23747eSTetsuo Handa */ 1246758a58d0SDongli Zhang lo->lo_flags = 0; 1247758a58d0SDongli Zhang if (!part_shift) 124846e7eac6SChristoph Hellwig lo->lo_disk->flags |= GENHD_FL_NO_PART; 12496050fa4cSTetsuo Handa mutex_lock(&lo->lo_mutex); 1250758a58d0SDongli Zhang lo->lo_state = Lo_unbound; 12516cc8e743SPavel Tatashin mutex_unlock(&lo->lo_mutex); 1252758a58d0SDongli Zhang 1253bf23747eSTetsuo Handa /* 1254bf23747eSTetsuo Handa * Need not hold lo_mutex to fput backing file. Calling fput holding 1255bf23747eSTetsuo Handa * lo_mutex triggers a circular lock dependency possibility warning as 1256bf23747eSTetsuo Handa * fput can take open_mutex which is usually taken before lo_mutex. 1257bf23747eSTetsuo Handa */ 1258bf23747eSTetsuo Handa fput(filp); 12591da177e4SLinus Torvalds } 12601da177e4SLinus Torvalds 1261a2505b79SJan Kara static int loop_clr_fd(struct loop_device *lo) 1262a2505b79SJan Kara { 12637ccd0791SJan Kara int err; 12647ccd0791SJan Kara 1265158eaebaSTetsuo Handa /* 1266158eaebaSTetsuo Handa * Since lo_ioctl() is called without locks held, it is possible that 1267158eaebaSTetsuo Handa * loop_configure()/loop_change_fd() and loop_clr_fd() run in parallel. 1268158eaebaSTetsuo Handa * 1269158eaebaSTetsuo Handa * Therefore, use global lock when setting Lo_rundown state in order to 1270158eaebaSTetsuo Handa * make sure that loop_validate_file() will fail if the "struct file" 1271158eaebaSTetsuo Handa * which loop_configure()/loop_change_fd() found via fget() was this 1272158eaebaSTetsuo Handa * loop device. 1273158eaebaSTetsuo Handa */ 1274158eaebaSTetsuo Handa err = loop_global_lock_killable(lo, true); 12757ccd0791SJan Kara if (err) 12767ccd0791SJan Kara return err; 12777ccd0791SJan Kara if (lo->lo_state != Lo_bound) { 1278158eaebaSTetsuo Handa loop_global_unlock(lo, true); 1279a2505b79SJan Kara return -ENXIO; 12807ccd0791SJan Kara } 1281a2505b79SJan Kara /* 1282a2505b79SJan Kara * If we've explicitly asked to tear down the loop device, 1283a2505b79SJan Kara * and it has an elevated reference count, set it for auto-teardown when 1284a2505b79SJan Kara * the last reference goes away. This stops $!~#$@ udev from 1285a2505b79SJan Kara * preventing teardown because it decided that it needs to run blkid on 1286a2505b79SJan Kara * the loopback device whenever they appear. xfstests is notorious for 1287a2505b79SJan Kara * failing tests because blkid via udev races with a losetup 1288a2505b79SJan Kara * <dev>/do something like mkfs/losetup -d <dev> causing the losetup -d 1289a2505b79SJan Kara * command to fail with EBUSY. 1290a2505b79SJan Kara */ 1291a0e286b6SChristoph Hellwig if (disk_openers(lo->lo_disk) > 1) { 1292a2505b79SJan Kara lo->lo_flags |= LO_FLAGS_AUTOCLEAR; 1293158eaebaSTetsuo Handa loop_global_unlock(lo, true); 1294a2505b79SJan Kara return 0; 1295a2505b79SJan Kara } 1296a2505b79SJan Kara lo->lo_state = Lo_rundown; 1297158eaebaSTetsuo Handa loop_global_unlock(lo, true); 1298a2505b79SJan Kara 1299bf23747eSTetsuo Handa __loop_clr_fd(lo, false); 13006050fa4cSTetsuo Handa return 0; 1301a2505b79SJan Kara } 1302a2505b79SJan Kara 13031da177e4SLinus Torvalds static int 13041da177e4SLinus Torvalds loop_set_status(struct loop_device *lo, const struct loop_info64 *info) 13051da177e4SLinus Torvalds { 13061da177e4SLinus Torvalds int err; 1307faf1d254SMartijn Coenen int prev_lo_flags; 130885b0a54aSJan Kara bool partscan = false; 13090c3796c2SMartijn Coenen bool size_changed = false; 13101da177e4SLinus Torvalds 13116cc8e743SPavel Tatashin err = mutex_lock_killable(&lo->lo_mutex); 1312550df5fdSJan Kara if (err) 1313550df5fdSJan Kara return err; 1314550df5fdSJan Kara if (lo->lo_state != Lo_bound) { 1315550df5fdSJan Kara err = -ENXIO; 1316550df5fdSJan Kara goto out_unlock; 1317550df5fdSJan Kara } 13181da177e4SLinus Torvalds 13195db470e2SJaegeuk Kim if (lo->lo_offset != info->lo_offset || 13205db470e2SJaegeuk Kim lo->lo_sizelimit != info->lo_sizelimit) { 13210c3796c2SMartijn Coenen size_changed = true; 13225db470e2SJaegeuk Kim sync_blockdev(lo->lo_device); 1323f4bd34b1SZheng Bin invalidate_bdev(lo->lo_device); 13245db470e2SJaegeuk Kim } 13255db470e2SJaegeuk Kim 1326ecdd0959SMing Lei /* I/O need to be drained during transfer transition */ 1327ecdd0959SMing Lei blk_mq_freeze_queue(lo->lo_queue); 1328ecdd0959SMing Lei 1329faf1d254SMartijn Coenen prev_lo_flags = lo->lo_flags; 1330faf1d254SMartijn Coenen 13310c3796c2SMartijn Coenen err = loop_set_status_from_info(lo, info); 13320c3796c2SMartijn Coenen if (err) 1333550df5fdSJan Kara goto out_unfreeze; 13340c3796c2SMartijn Coenen 1335faf1d254SMartijn Coenen /* Mask out flags that can't be set using LOOP_SET_STATUS. */ 13366ac92fb5SMartijn Coenen lo->lo_flags &= LOOP_SET_STATUS_SETTABLE_FLAGS; 1337faf1d254SMartijn Coenen /* For those flags, use the previous values instead */ 1338faf1d254SMartijn Coenen lo->lo_flags |= prev_lo_flags & ~LOOP_SET_STATUS_SETTABLE_FLAGS; 1339faf1d254SMartijn Coenen /* For flags that can't be cleared, use previous values too */ 1340faf1d254SMartijn Coenen lo->lo_flags |= prev_lo_flags & ~LOOP_SET_STATUS_CLEARABLE_FLAGS; 1341faf1d254SMartijn Coenen 1342b0bd158dSMartijn Coenen if (size_changed) { 1343b0bd158dSMartijn Coenen loff_t new_size = get_size(lo->lo_offset, lo->lo_sizelimit, 1344b0bd158dSMartijn Coenen lo->lo_backing_file); 1345b0bd158dSMartijn Coenen loop_set_size(lo, new_size); 1346b040ad9cSArnd Bergmann } 1347541c742aSGuo Chao 1348dfaa2ef6SLukas Czerner loop_config_discard(lo); 13491da177e4SLinus Torvalds 13502e5ab5f3SMing Lei /* update dio if lo_offset or transfer is changed */ 13512e5ab5f3SMing Lei __loop_update_dio(lo, lo->use_dio); 13522e5ab5f3SMing Lei 1353550df5fdSJan Kara out_unfreeze: 1354ecdd0959SMing Lei blk_mq_unfreeze_queue(lo->lo_queue); 1355e02898b4SOmar Sandoval 1356faf1d254SMartijn Coenen if (!err && (lo->lo_flags & LO_FLAGS_PARTSCAN) && 1357faf1d254SMartijn Coenen !(prev_lo_flags & LO_FLAGS_PARTSCAN)) { 135846e7eac6SChristoph Hellwig lo->lo_disk->flags &= ~GENHD_FL_NO_PART; 135985b0a54aSJan Kara partscan = true; 1360e02898b4SOmar Sandoval } 1361550df5fdSJan Kara out_unlock: 13626cc8e743SPavel Tatashin mutex_unlock(&lo->lo_mutex); 136385b0a54aSJan Kara if (partscan) 13640384264eSChristoph Hellwig loop_reread_partitions(lo); 1365e02898b4SOmar Sandoval 1366ecdd0959SMing Lei return err; 13671da177e4SLinus Torvalds } 13681da177e4SLinus Torvalds 13691da177e4SLinus Torvalds static int 13701da177e4SLinus Torvalds loop_get_status(struct loop_device *lo, struct loop_info64 *info) 13711da177e4SLinus Torvalds { 1372b1ab5fa3STetsuo Handa struct path path; 13731da177e4SLinus Torvalds struct kstat stat; 13742d1d4c1eSOmar Sandoval int ret; 13751da177e4SLinus Torvalds 13766cc8e743SPavel Tatashin ret = mutex_lock_killable(&lo->lo_mutex); 13774a5ce9baSJan Kara if (ret) 13784a5ce9baSJan Kara return ret; 13792d1d4c1eSOmar Sandoval if (lo->lo_state != Lo_bound) { 13806cc8e743SPavel Tatashin mutex_unlock(&lo->lo_mutex); 13811da177e4SLinus Torvalds return -ENXIO; 13822d1d4c1eSOmar Sandoval } 13832d1d4c1eSOmar Sandoval 13841da177e4SLinus Torvalds memset(info, 0, sizeof(*info)); 13851da177e4SLinus Torvalds info->lo_number = lo->lo_number; 13861da177e4SLinus Torvalds info->lo_offset = lo->lo_offset; 13871da177e4SLinus Torvalds info->lo_sizelimit = lo->lo_sizelimit; 13881da177e4SLinus Torvalds info->lo_flags = lo->lo_flags; 13891da177e4SLinus Torvalds memcpy(info->lo_file_name, lo->lo_file_name, LO_NAME_SIZE); 13902d1d4c1eSOmar Sandoval 13916cc8e743SPavel Tatashin /* Drop lo_mutex while we call into the filesystem. */ 1392b1ab5fa3STetsuo Handa path = lo->lo_backing_file->f_path; 1393b1ab5fa3STetsuo Handa path_get(&path); 13946cc8e743SPavel Tatashin mutex_unlock(&lo->lo_mutex); 1395b1ab5fa3STetsuo Handa ret = vfs_getattr(&path, &stat, STATX_INO, AT_STATX_SYNC_AS_STAT); 13962d1d4c1eSOmar Sandoval if (!ret) { 13972d1d4c1eSOmar Sandoval info->lo_device = huge_encode_dev(stat.dev); 13982d1d4c1eSOmar Sandoval info->lo_inode = stat.ino; 13992d1d4c1eSOmar Sandoval info->lo_rdevice = huge_encode_dev(stat.rdev); 14002d1d4c1eSOmar Sandoval } 1401b1ab5fa3STetsuo Handa path_put(&path); 14022d1d4c1eSOmar Sandoval return ret; 14031da177e4SLinus Torvalds } 14041da177e4SLinus Torvalds 14051da177e4SLinus Torvalds static void 14061da177e4SLinus Torvalds loop_info64_from_old(const struct loop_info *info, struct loop_info64 *info64) 14071da177e4SLinus Torvalds { 14081da177e4SLinus Torvalds memset(info64, 0, sizeof(*info64)); 14091da177e4SLinus Torvalds info64->lo_number = info->lo_number; 14101da177e4SLinus Torvalds info64->lo_device = info->lo_device; 14111da177e4SLinus Torvalds info64->lo_inode = info->lo_inode; 14121da177e4SLinus Torvalds info64->lo_rdevice = info->lo_rdevice; 14131da177e4SLinus Torvalds info64->lo_offset = info->lo_offset; 14141da177e4SLinus Torvalds info64->lo_sizelimit = 0; 14151da177e4SLinus Torvalds info64->lo_flags = info->lo_flags; 14161da177e4SLinus Torvalds memcpy(info64->lo_file_name, info->lo_name, LO_NAME_SIZE); 14171da177e4SLinus Torvalds } 14181da177e4SLinus Torvalds 14191da177e4SLinus Torvalds static int 14201da177e4SLinus Torvalds loop_info64_to_old(const struct loop_info64 *info64, struct loop_info *info) 14211da177e4SLinus Torvalds { 14221da177e4SLinus Torvalds memset(info, 0, sizeof(*info)); 14231da177e4SLinus Torvalds info->lo_number = info64->lo_number; 14241da177e4SLinus Torvalds info->lo_device = info64->lo_device; 14251da177e4SLinus Torvalds info->lo_inode = info64->lo_inode; 14261da177e4SLinus Torvalds info->lo_rdevice = info64->lo_rdevice; 14271da177e4SLinus Torvalds info->lo_offset = info64->lo_offset; 14281da177e4SLinus Torvalds info->lo_flags = info64->lo_flags; 14291da177e4SLinus Torvalds memcpy(info->lo_name, info64->lo_file_name, LO_NAME_SIZE); 14301da177e4SLinus Torvalds 14311da177e4SLinus Torvalds /* error in case values were truncated */ 14321da177e4SLinus Torvalds if (info->lo_device != info64->lo_device || 14331da177e4SLinus Torvalds info->lo_rdevice != info64->lo_rdevice || 14341da177e4SLinus Torvalds info->lo_inode != info64->lo_inode || 14351da177e4SLinus Torvalds info->lo_offset != info64->lo_offset) 14361da177e4SLinus Torvalds return -EOVERFLOW; 14371da177e4SLinus Torvalds 14381da177e4SLinus Torvalds return 0; 14391da177e4SLinus Torvalds } 14401da177e4SLinus Torvalds 14411da177e4SLinus Torvalds static int 14421da177e4SLinus Torvalds loop_set_status_old(struct loop_device *lo, const struct loop_info __user *arg) 14431da177e4SLinus Torvalds { 14441da177e4SLinus Torvalds struct loop_info info; 14451da177e4SLinus Torvalds struct loop_info64 info64; 14461da177e4SLinus Torvalds 14471da177e4SLinus Torvalds if (copy_from_user(&info, arg, sizeof (struct loop_info))) 14481da177e4SLinus Torvalds return -EFAULT; 14491da177e4SLinus Torvalds loop_info64_from_old(&info, &info64); 14501da177e4SLinus Torvalds return loop_set_status(lo, &info64); 14511da177e4SLinus Torvalds } 14521da177e4SLinus Torvalds 14531da177e4SLinus Torvalds static int 14541da177e4SLinus Torvalds loop_set_status64(struct loop_device *lo, const struct loop_info64 __user *arg) 14551da177e4SLinus Torvalds { 14561da177e4SLinus Torvalds struct loop_info64 info64; 14571da177e4SLinus Torvalds 14581da177e4SLinus Torvalds if (copy_from_user(&info64, arg, sizeof (struct loop_info64))) 14591da177e4SLinus Torvalds return -EFAULT; 14601da177e4SLinus Torvalds return loop_set_status(lo, &info64); 14611da177e4SLinus Torvalds } 14621da177e4SLinus Torvalds 14631da177e4SLinus Torvalds static int 14641da177e4SLinus Torvalds loop_get_status_old(struct loop_device *lo, struct loop_info __user *arg) { 14651da177e4SLinus Torvalds struct loop_info info; 14661da177e4SLinus Torvalds struct loop_info64 info64; 1467bdac616dSOmar Sandoval int err; 14681da177e4SLinus Torvalds 14694a5ce9baSJan Kara if (!arg) 1470bdac616dSOmar Sandoval return -EINVAL; 14711da177e4SLinus Torvalds err = loop_get_status(lo, &info64); 14721da177e4SLinus Torvalds if (!err) 14731da177e4SLinus Torvalds err = loop_info64_to_old(&info64, &info); 14741da177e4SLinus Torvalds if (!err && copy_to_user(arg, &info, sizeof(info))) 14751da177e4SLinus Torvalds err = -EFAULT; 14761da177e4SLinus Torvalds 14771da177e4SLinus Torvalds return err; 14781da177e4SLinus Torvalds } 14791da177e4SLinus Torvalds 14801da177e4SLinus Torvalds static int 14811da177e4SLinus Torvalds loop_get_status64(struct loop_device *lo, struct loop_info64 __user *arg) { 14821da177e4SLinus Torvalds struct loop_info64 info64; 1483bdac616dSOmar Sandoval int err; 14841da177e4SLinus Torvalds 14854a5ce9baSJan Kara if (!arg) 1486bdac616dSOmar Sandoval return -EINVAL; 14871da177e4SLinus Torvalds err = loop_get_status(lo, &info64); 14881da177e4SLinus Torvalds if (!err && copy_to_user(arg, &info64, sizeof(info64))) 14891da177e4SLinus Torvalds err = -EFAULT; 14901da177e4SLinus Torvalds 14911da177e4SLinus Torvalds return err; 14921da177e4SLinus Torvalds } 14931da177e4SLinus Torvalds 149451001b7dSHannes Reinecke static int loop_set_capacity(struct loop_device *lo) 149553d66608SJ. R. Okajima { 14960a6ed1b5SMartijn Coenen loff_t size; 14970a6ed1b5SMartijn Coenen 149853d66608SJ. R. Okajima if (unlikely(lo->lo_state != Lo_bound)) 14997b0576a3SGuo Chao return -ENXIO; 150053d66608SJ. R. Okajima 15010a6ed1b5SMartijn Coenen size = get_loop_size(lo, lo->lo_backing_file); 15020a6ed1b5SMartijn Coenen loop_set_size(lo, size); 1503083a6a50SMartijn Coenen 1504083a6a50SMartijn Coenen return 0; 150553d66608SJ. R. Okajima } 150653d66608SJ. R. Okajima 1507ab1cb278SMing Lei static int loop_set_dio(struct loop_device *lo, unsigned long arg) 1508ab1cb278SMing Lei { 1509ab1cb278SMing Lei int error = -ENXIO; 1510ab1cb278SMing Lei if (lo->lo_state != Lo_bound) 1511ab1cb278SMing Lei goto out; 1512ab1cb278SMing Lei 1513ab1cb278SMing Lei __loop_update_dio(lo, !!arg); 1514ab1cb278SMing Lei if (lo->use_dio == !!arg) 1515ab1cb278SMing Lei return 0; 1516ab1cb278SMing Lei error = -EINVAL; 1517ab1cb278SMing Lei out: 1518ab1cb278SMing Lei return error; 1519ab1cb278SMing Lei } 1520ab1cb278SMing Lei 152189e4fdecSOmar Sandoval static int loop_set_block_size(struct loop_device *lo, unsigned long arg) 152289e4fdecSOmar Sandoval { 15235db470e2SJaegeuk Kim int err = 0; 15245db470e2SJaegeuk Kim 152589e4fdecSOmar Sandoval if (lo->lo_state != Lo_bound) 152689e4fdecSOmar Sandoval return -ENXIO; 152789e4fdecSOmar Sandoval 1528af3c570fSXie Yongji err = blk_validate_block_size(arg); 15293448914eSMartijn Coenen if (err) 15303448914eSMartijn Coenen return err; 153189e4fdecSOmar Sandoval 15327e81f99aSMartijn Coenen if (lo->lo_queue->limits.logical_block_size == arg) 15337e81f99aSMartijn Coenen return 0; 15347e81f99aSMartijn Coenen 15355db470e2SJaegeuk Kim sync_blockdev(lo->lo_device); 1536f4bd34b1SZheng Bin invalidate_bdev(lo->lo_device); 15375db470e2SJaegeuk Kim 153889e4fdecSOmar Sandoval blk_mq_freeze_queue(lo->lo_queue); 153989e4fdecSOmar Sandoval blk_queue_logical_block_size(lo->lo_queue, arg); 1540bf093753SOmar Sandoval blk_queue_physical_block_size(lo->lo_queue, arg); 1541bf093753SOmar Sandoval blk_queue_io_min(lo->lo_queue, arg); 154289e4fdecSOmar Sandoval loop_update_dio(lo); 154389e4fdecSOmar Sandoval blk_mq_unfreeze_queue(lo->lo_queue); 154489e4fdecSOmar Sandoval 15455db470e2SJaegeuk Kim return err; 154689e4fdecSOmar Sandoval } 154789e4fdecSOmar Sandoval 1548a1316544SJan Kara static int lo_simple_ioctl(struct loop_device *lo, unsigned int cmd, 1549a1316544SJan Kara unsigned long arg) 15501da177e4SLinus Torvalds { 15511da177e4SLinus Torvalds int err; 15521da177e4SLinus Torvalds 15536cc8e743SPavel Tatashin err = mutex_lock_killable(&lo->lo_mutex); 15543148ffbdSOmar Sandoval if (err) 1555a1316544SJan Kara return err; 15561da177e4SLinus Torvalds switch (cmd) { 155753d66608SJ. R. Okajima case LOOP_SET_CAPACITY: 155851001b7dSHannes Reinecke err = loop_set_capacity(lo); 155953d66608SJ. R. Okajima break; 1560ab1cb278SMing Lei case LOOP_SET_DIRECT_IO: 1561ab1cb278SMing Lei err = loop_set_dio(lo, arg); 1562ab1cb278SMing Lei break; 156389e4fdecSOmar Sandoval case LOOP_SET_BLOCK_SIZE: 156489e4fdecSOmar Sandoval err = loop_set_block_size(lo, arg); 156589e4fdecSOmar Sandoval break; 15661da177e4SLinus Torvalds default: 156747e96246SChristoph Hellwig err = -EINVAL; 15681da177e4SLinus Torvalds } 15696cc8e743SPavel Tatashin mutex_unlock(&lo->lo_mutex); 1570a1316544SJan Kara return err; 1571a1316544SJan Kara } 1572f028f3b2SNikanth Karthikesan 1573a1316544SJan Kara static int lo_ioctl(struct block_device *bdev, fmode_t mode, 1574a1316544SJan Kara unsigned int cmd, unsigned long arg) 1575a1316544SJan Kara { 1576a1316544SJan Kara struct loop_device *lo = bdev->bd_disk->private_data; 1577571fae6eSMartijn Coenen void __user *argp = (void __user *) arg; 1578a1316544SJan Kara int err; 1579a1316544SJan Kara 1580a1316544SJan Kara switch (cmd) { 15813448914eSMartijn Coenen case LOOP_SET_FD: { 15823448914eSMartijn Coenen /* 15833448914eSMartijn Coenen * Legacy case - pass in a zeroed out struct loop_config with 15843448914eSMartijn Coenen * only the file descriptor set , which corresponds with the 15853448914eSMartijn Coenen * default parameters we'd have used otherwise. 15863448914eSMartijn Coenen */ 15873448914eSMartijn Coenen struct loop_config config; 15883448914eSMartijn Coenen 15893448914eSMartijn Coenen memset(&config, 0, sizeof(config)); 15903448914eSMartijn Coenen config.fd = arg; 15913448914eSMartijn Coenen 15923448914eSMartijn Coenen return loop_configure(lo, mode, bdev, &config); 15933448914eSMartijn Coenen } 15943448914eSMartijn Coenen case LOOP_CONFIGURE: { 15953448914eSMartijn Coenen struct loop_config config; 15963448914eSMartijn Coenen 15973448914eSMartijn Coenen if (copy_from_user(&config, argp, sizeof(config))) 15983448914eSMartijn Coenen return -EFAULT; 15993448914eSMartijn Coenen 16003448914eSMartijn Coenen return loop_configure(lo, mode, bdev, &config); 16013448914eSMartijn Coenen } 1602a1316544SJan Kara case LOOP_CHANGE_FD: 1603c3710770SJan Kara return loop_change_fd(lo, bdev, arg); 1604a1316544SJan Kara case LOOP_CLR_FD: 16057ccd0791SJan Kara return loop_clr_fd(lo); 1606a1316544SJan Kara case LOOP_SET_STATUS: 1607a1316544SJan Kara err = -EPERM; 1608a1316544SJan Kara if ((mode & FMODE_WRITE) || capable(CAP_SYS_ADMIN)) { 1609571fae6eSMartijn Coenen err = loop_set_status_old(lo, argp); 1610a1316544SJan Kara } 1611a1316544SJan Kara break; 1612a1316544SJan Kara case LOOP_GET_STATUS: 1613571fae6eSMartijn Coenen return loop_get_status_old(lo, argp); 1614a1316544SJan Kara case LOOP_SET_STATUS64: 1615a1316544SJan Kara err = -EPERM; 1616a1316544SJan Kara if ((mode & FMODE_WRITE) || capable(CAP_SYS_ADMIN)) { 1617571fae6eSMartijn Coenen err = loop_set_status64(lo, argp); 1618a1316544SJan Kara } 1619a1316544SJan Kara break; 1620a1316544SJan Kara case LOOP_GET_STATUS64: 1621571fae6eSMartijn Coenen return loop_get_status64(lo, argp); 1622a1316544SJan Kara case LOOP_SET_CAPACITY: 1623a1316544SJan Kara case LOOP_SET_DIRECT_IO: 1624a1316544SJan Kara case LOOP_SET_BLOCK_SIZE: 1625a1316544SJan Kara if (!(mode & FMODE_WRITE) && !capable(CAP_SYS_ADMIN)) 1626a1316544SJan Kara return -EPERM; 1627df561f66SGustavo A. R. Silva fallthrough; 1628a1316544SJan Kara default: 1629a1316544SJan Kara err = lo_simple_ioctl(lo, cmd, arg); 1630a1316544SJan Kara break; 1631a1316544SJan Kara } 1632a1316544SJan Kara 16331da177e4SLinus Torvalds return err; 16341da177e4SLinus Torvalds } 16351da177e4SLinus Torvalds 1636863d5b82SDavid Howells #ifdef CONFIG_COMPAT 1637863d5b82SDavid Howells struct compat_loop_info { 1638863d5b82SDavid Howells compat_int_t lo_number; /* ioctl r/o */ 1639863d5b82SDavid Howells compat_dev_t lo_device; /* ioctl r/o */ 1640863d5b82SDavid Howells compat_ulong_t lo_inode; /* ioctl r/o */ 1641863d5b82SDavid Howells compat_dev_t lo_rdevice; /* ioctl r/o */ 1642863d5b82SDavid Howells compat_int_t lo_offset; 1643f941c51eSCarlos Llamas compat_int_t lo_encrypt_type; /* obsolete, ignored */ 1644863d5b82SDavid Howells compat_int_t lo_encrypt_key_size; /* ioctl w/o */ 1645863d5b82SDavid Howells compat_int_t lo_flags; /* ioctl r/o */ 1646863d5b82SDavid Howells char lo_name[LO_NAME_SIZE]; 1647863d5b82SDavid Howells unsigned char lo_encrypt_key[LO_KEY_SIZE]; /* ioctl w/o */ 1648863d5b82SDavid Howells compat_ulong_t lo_init[2]; 1649863d5b82SDavid Howells char reserved[4]; 1650863d5b82SDavid Howells }; 1651863d5b82SDavid Howells 1652863d5b82SDavid Howells /* 1653863d5b82SDavid Howells * Transfer 32-bit compatibility structure in userspace to 64-bit loop info 1654863d5b82SDavid Howells * - noinlined to reduce stack space usage in main part of driver 1655863d5b82SDavid Howells */ 1656863d5b82SDavid Howells static noinline int 1657ba674cfcSAl Viro loop_info64_from_compat(const struct compat_loop_info __user *arg, 1658863d5b82SDavid Howells struct loop_info64 *info64) 1659863d5b82SDavid Howells { 1660863d5b82SDavid Howells struct compat_loop_info info; 1661863d5b82SDavid Howells 1662863d5b82SDavid Howells if (copy_from_user(&info, arg, sizeof(info))) 1663863d5b82SDavid Howells return -EFAULT; 1664863d5b82SDavid Howells 1665863d5b82SDavid Howells memset(info64, 0, sizeof(*info64)); 1666863d5b82SDavid Howells info64->lo_number = info.lo_number; 1667863d5b82SDavid Howells info64->lo_device = info.lo_device; 1668863d5b82SDavid Howells info64->lo_inode = info.lo_inode; 1669863d5b82SDavid Howells info64->lo_rdevice = info.lo_rdevice; 1670863d5b82SDavid Howells info64->lo_offset = info.lo_offset; 1671863d5b82SDavid Howells info64->lo_sizelimit = 0; 1672863d5b82SDavid Howells info64->lo_flags = info.lo_flags; 1673863d5b82SDavid Howells memcpy(info64->lo_file_name, info.lo_name, LO_NAME_SIZE); 1674863d5b82SDavid Howells return 0; 1675863d5b82SDavid Howells } 1676863d5b82SDavid Howells 1677863d5b82SDavid Howells /* 1678863d5b82SDavid Howells * Transfer 64-bit loop info to 32-bit compatibility structure in userspace 1679863d5b82SDavid Howells * - noinlined to reduce stack space usage in main part of driver 1680863d5b82SDavid Howells */ 1681863d5b82SDavid Howells static noinline int 1682863d5b82SDavid Howells loop_info64_to_compat(const struct loop_info64 *info64, 1683863d5b82SDavid Howells struct compat_loop_info __user *arg) 1684863d5b82SDavid Howells { 1685863d5b82SDavid Howells struct compat_loop_info info; 1686863d5b82SDavid Howells 1687863d5b82SDavid Howells memset(&info, 0, sizeof(info)); 1688863d5b82SDavid Howells info.lo_number = info64->lo_number; 1689863d5b82SDavid Howells info.lo_device = info64->lo_device; 1690863d5b82SDavid Howells info.lo_inode = info64->lo_inode; 1691863d5b82SDavid Howells info.lo_rdevice = info64->lo_rdevice; 1692863d5b82SDavid Howells info.lo_offset = info64->lo_offset; 1693863d5b82SDavid Howells info.lo_flags = info64->lo_flags; 1694863d5b82SDavid Howells memcpy(info.lo_name, info64->lo_file_name, LO_NAME_SIZE); 1695863d5b82SDavid Howells 1696863d5b82SDavid Howells /* error in case values were truncated */ 1697863d5b82SDavid Howells if (info.lo_device != info64->lo_device || 1698863d5b82SDavid Howells info.lo_rdevice != info64->lo_rdevice || 1699863d5b82SDavid Howells info.lo_inode != info64->lo_inode || 170047e96246SChristoph Hellwig info.lo_offset != info64->lo_offset) 1701863d5b82SDavid Howells return -EOVERFLOW; 1702863d5b82SDavid Howells 1703863d5b82SDavid Howells if (copy_to_user(arg, &info, sizeof(info))) 1704863d5b82SDavid Howells return -EFAULT; 1705863d5b82SDavid Howells return 0; 1706863d5b82SDavid Howells } 1707863d5b82SDavid Howells 1708863d5b82SDavid Howells static int 1709863d5b82SDavid Howells loop_set_status_compat(struct loop_device *lo, 1710863d5b82SDavid Howells const struct compat_loop_info __user *arg) 1711863d5b82SDavid Howells { 1712863d5b82SDavid Howells struct loop_info64 info64; 1713863d5b82SDavid Howells int ret; 1714863d5b82SDavid Howells 1715863d5b82SDavid Howells ret = loop_info64_from_compat(arg, &info64); 1716863d5b82SDavid Howells if (ret < 0) 1717863d5b82SDavid Howells return ret; 1718863d5b82SDavid Howells return loop_set_status(lo, &info64); 1719863d5b82SDavid Howells } 1720863d5b82SDavid Howells 1721863d5b82SDavid Howells static int 1722863d5b82SDavid Howells loop_get_status_compat(struct loop_device *lo, 1723863d5b82SDavid Howells struct compat_loop_info __user *arg) 1724863d5b82SDavid Howells { 1725863d5b82SDavid Howells struct loop_info64 info64; 1726bdac616dSOmar Sandoval int err; 1727863d5b82SDavid Howells 17284a5ce9baSJan Kara if (!arg) 1729bdac616dSOmar Sandoval return -EINVAL; 1730863d5b82SDavid Howells err = loop_get_status(lo, &info64); 1731863d5b82SDavid Howells if (!err) 1732863d5b82SDavid Howells err = loop_info64_to_compat(&info64, arg); 1733863d5b82SDavid Howells return err; 1734863d5b82SDavid Howells } 1735863d5b82SDavid Howells 1736bb214884SAl Viro static int lo_compat_ioctl(struct block_device *bdev, fmode_t mode, 1737bb214884SAl Viro unsigned int cmd, unsigned long arg) 1738863d5b82SDavid Howells { 1739bb214884SAl Viro struct loop_device *lo = bdev->bd_disk->private_data; 1740863d5b82SDavid Howells int err; 1741863d5b82SDavid Howells 1742863d5b82SDavid Howells switch(cmd) { 1743863d5b82SDavid Howells case LOOP_SET_STATUS: 17443148ffbdSOmar Sandoval err = loop_set_status_compat(lo, 17453148ffbdSOmar Sandoval (const struct compat_loop_info __user *)arg); 1746863d5b82SDavid Howells break; 1747863d5b82SDavid Howells case LOOP_GET_STATUS: 17483148ffbdSOmar Sandoval err = loop_get_status_compat(lo, 17493148ffbdSOmar Sandoval (struct compat_loop_info __user *)arg); 1750863d5b82SDavid Howells break; 175153d66608SJ. R. Okajima case LOOP_SET_CAPACITY: 1752863d5b82SDavid Howells case LOOP_CLR_FD: 1753863d5b82SDavid Howells case LOOP_GET_STATUS64: 1754863d5b82SDavid Howells case LOOP_SET_STATUS64: 17553448914eSMartijn Coenen case LOOP_CONFIGURE: 1756863d5b82SDavid Howells arg = (unsigned long) compat_ptr(arg); 1757df561f66SGustavo A. R. Silva fallthrough; 1758863d5b82SDavid Howells case LOOP_SET_FD: 1759863d5b82SDavid Howells case LOOP_CHANGE_FD: 17609fea4b39SEvan Green case LOOP_SET_BLOCK_SIZE: 1761fdbe4eeeSAlessio Balsini case LOOP_SET_DIRECT_IO: 1762bb214884SAl Viro err = lo_ioctl(bdev, mode, cmd, arg); 1763863d5b82SDavid Howells break; 1764863d5b82SDavid Howells default: 1765863d5b82SDavid Howells err = -ENOIOCTLCMD; 1766863d5b82SDavid Howells break; 1767863d5b82SDavid Howells } 1768863d5b82SDavid Howells return err; 1769863d5b82SDavid Howells } 1770863d5b82SDavid Howells #endif 1771863d5b82SDavid Howells 1772967d1dc1SJan Kara static void lo_release(struct gendisk *disk, fmode_t mode) 17731da177e4SLinus Torvalds { 17746cc8e743SPavel Tatashin struct loop_device *lo = disk->private_data; 17751da177e4SLinus Torvalds 1776a0e286b6SChristoph Hellwig if (disk_openers(disk) > 0) 1777a0e286b6SChristoph Hellwig return; 1778f8933667SMing Lei 1779a0e286b6SChristoph Hellwig mutex_lock(&lo->lo_mutex); 1780a0e286b6SChristoph Hellwig if (lo->lo_state == Lo_bound && (lo->lo_flags & LO_FLAGS_AUTOCLEAR)) { 1781a2505b79SJan Kara lo->lo_state = Lo_rundown; 17826cc8e743SPavel Tatashin mutex_unlock(&lo->lo_mutex); 178314f27939SMilan Broz /* 178414f27939SMilan Broz * In autoclear mode, stop the loop thread 178514f27939SMilan Broz * and remove configuration after last close. 178614f27939SMilan Broz */ 1787bf23747eSTetsuo Handa __loop_clr_fd(lo, true); 17880a42e99bSJan Kara return; 178914f27939SMilan Broz } 17906cc8e743SPavel Tatashin mutex_unlock(&lo->lo_mutex); 1791ae665016SLinus Torvalds } 1792ae665016SLinus Torvalds 1793d2c7f56fSChristoph Hellwig static void lo_free_disk(struct gendisk *disk) 1794d2c7f56fSChristoph Hellwig { 1795d2c7f56fSChristoph Hellwig struct loop_device *lo = disk->private_data; 1796d2c7f56fSChristoph Hellwig 1797d292dc80SChristoph Hellwig if (lo->workqueue) 1798d292dc80SChristoph Hellwig destroy_workqueue(lo->workqueue); 1799d292dc80SChristoph Hellwig loop_free_idle_workers(lo, true); 1800d292dc80SChristoph Hellwig del_timer_sync(&lo->timer); 1801d2c7f56fSChristoph Hellwig mutex_destroy(&lo->lo_mutex); 1802d2c7f56fSChristoph Hellwig kfree(lo); 1803d2c7f56fSChristoph Hellwig } 1804d2c7f56fSChristoph Hellwig 180583d5cde4SAlexey Dobriyan static const struct block_device_operations lo_fops = { 18061da177e4SLinus Torvalds .owner = THIS_MODULE, 1807bb214884SAl Viro .release = lo_release, 1808bb214884SAl Viro .ioctl = lo_ioctl, 1809863d5b82SDavid Howells #ifdef CONFIG_COMPAT 1810bb214884SAl Viro .compat_ioctl = lo_compat_ioctl, 1811863d5b82SDavid Howells #endif 1812d2c7f56fSChristoph Hellwig .free_disk = lo_free_disk, 18131da177e4SLinus Torvalds }; 18141da177e4SLinus Torvalds 18151da177e4SLinus Torvalds /* 18161da177e4SLinus Torvalds * And now the modules code and kernel interface. 18171da177e4SLinus Torvalds */ 181873285082SKen Chen static int max_loop; 18195657a819SJoe Perches module_param(max_loop, int, 0444); 1820a47653fcSKen Chen MODULE_PARM_DESC(max_loop, "Maximum number of loop devices"); 18215657a819SJoe Perches module_param(max_part, int, 0444); 1822476a4813SLaurent Vivier MODULE_PARM_DESC(max_part, "Maximum number of partitions per loop device"); 1823ef44c508SChaitanya Kulkarni 1824ef44c508SChaitanya Kulkarni static int hw_queue_depth = LOOP_DEFAULT_HW_Q_DEPTH; 1825ef44c508SChaitanya Kulkarni 1826ef44c508SChaitanya Kulkarni static int loop_set_hw_queue_depth(const char *s, const struct kernel_param *p) 1827ef44c508SChaitanya Kulkarni { 1828ef44c508SChaitanya Kulkarni int ret = kstrtoint(s, 10, &hw_queue_depth); 1829ef44c508SChaitanya Kulkarni 1830ef44c508SChaitanya Kulkarni return (ret || (hw_queue_depth < 1)) ? -EINVAL : 0; 1831ef44c508SChaitanya Kulkarni } 1832ef44c508SChaitanya Kulkarni 1833ef44c508SChaitanya Kulkarni static const struct kernel_param_ops loop_hw_qdepth_param_ops = { 1834ef44c508SChaitanya Kulkarni .set = loop_set_hw_queue_depth, 1835ef44c508SChaitanya Kulkarni .get = param_get_int, 1836ef44c508SChaitanya Kulkarni }; 1837ef44c508SChaitanya Kulkarni 1838ef44c508SChaitanya Kulkarni device_param_cb(hw_queue_depth, &loop_hw_qdepth_param_ops, &hw_queue_depth, 0444); 1839ef44c508SChaitanya Kulkarni MODULE_PARM_DESC(hw_queue_depth, "Queue depth for each hardware queue. Default: 128"); 1840ef44c508SChaitanya Kulkarni 18411da177e4SLinus Torvalds MODULE_LICENSE("GPL"); 18421da177e4SLinus Torvalds MODULE_ALIAS_BLOCKDEV_MAJOR(LOOP_MAJOR); 18431da177e4SLinus Torvalds 1844fc17b653SChristoph Hellwig static blk_status_t loop_queue_rq(struct blk_mq_hw_ctx *hctx, 1845b5dd2f60SMing Lei const struct blk_mq_queue_data *bd) 1846b5dd2f60SMing Lei { 18471894e916SJens Axboe struct request *rq = bd->rq; 18481894e916SJens Axboe struct loop_cmd *cmd = blk_mq_rq_to_pdu(rq); 18491894e916SJens Axboe struct loop_device *lo = rq->q->queuedata; 1850b5dd2f60SMing Lei 18511894e916SJens Axboe blk_mq_start_request(rq); 1852b5dd2f60SMing Lei 1853f4aa4c7bSMing Lei if (lo->lo_state != Lo_bound) 1854fc17b653SChristoph Hellwig return BLK_STS_IOERR; 1855f4aa4c7bSMing Lei 18561894e916SJens Axboe switch (req_op(rq)) { 1857f0225cacSChristoph Hellwig case REQ_OP_FLUSH: 1858f0225cacSChristoph Hellwig case REQ_OP_DISCARD: 185919372e27SChristoph Hellwig case REQ_OP_WRITE_ZEROES: 1860bc07c10aSMing Lei cmd->use_aio = false; 1861f0225cacSChristoph Hellwig break; 1862f0225cacSChristoph Hellwig default: 1863f0225cacSChristoph Hellwig cmd->use_aio = lo->use_dio; 1864f0225cacSChristoph Hellwig break; 1865f0225cacSChristoph Hellwig } 1866bc07c10aSMing Lei 1867d4478e92SShaohua Li /* always use the first bio's css */ 1868c74d40e8SDan Schatzberg cmd->blkcg_css = NULL; 1869c74d40e8SDan Schatzberg cmd->memcg_css = NULL; 18700b508bc9SShaohua Li #ifdef CONFIG_BLK_CGROUP 1871c74d40e8SDan Schatzberg if (rq->bio && rq->bio->bi_blkg) { 1872c74d40e8SDan Schatzberg cmd->blkcg_css = &bio_blkcg(rq->bio)->css; 1873c74d40e8SDan Schatzberg #ifdef CONFIG_MEMCG 1874c74d40e8SDan Schatzberg cmd->memcg_css = 1875c74d40e8SDan Schatzberg cgroup_get_e_css(cmd->blkcg_css->cgroup, 1876c74d40e8SDan Schatzberg &memory_cgrp_subsys); 1877d4478e92SShaohua Li #endif 1878c74d40e8SDan Schatzberg } 1879c74d40e8SDan Schatzberg #endif 188087579e9bSDan Schatzberg loop_queue_work(lo, cmd); 1881b5dd2f60SMing Lei 1882fc17b653SChristoph Hellwig return BLK_STS_OK; 1883b5dd2f60SMing Lei } 1884b5dd2f60SMing Lei 1885b5dd2f60SMing Lei static void loop_handle_cmd(struct loop_cmd *cmd) 1886b5dd2f60SMing Lei { 18871894e916SJens Axboe struct request *rq = blk_mq_rq_from_pdu(cmd); 18881894e916SJens Axboe const bool write = op_is_write(req_op(rq)); 18891894e916SJens Axboe struct loop_device *lo = rq->q->queuedata; 1890f4829a9bSChristoph Hellwig int ret = 0; 1891c74d40e8SDan Schatzberg struct mem_cgroup *old_memcg = NULL; 1892b5dd2f60SMing Lei 1893f4829a9bSChristoph Hellwig if (write && (lo->lo_flags & LO_FLAGS_READ_ONLY)) { 1894f4829a9bSChristoph Hellwig ret = -EIO; 1895b5dd2f60SMing Lei goto failed; 1896f4829a9bSChristoph Hellwig } 1897b5dd2f60SMing Lei 1898c74d40e8SDan Schatzberg if (cmd->blkcg_css) 1899c74d40e8SDan Schatzberg kthread_associate_blkcg(cmd->blkcg_css); 1900c74d40e8SDan Schatzberg if (cmd->memcg_css) 1901c74d40e8SDan Schatzberg old_memcg = set_active_memcg( 1902c74d40e8SDan Schatzberg mem_cgroup_from_css(cmd->memcg_css)); 1903c74d40e8SDan Schatzberg 19041894e916SJens Axboe ret = do_req_filebacked(lo, rq); 1905c74d40e8SDan Schatzberg 1906c74d40e8SDan Schatzberg if (cmd->blkcg_css) 1907c74d40e8SDan Schatzberg kthread_associate_blkcg(NULL); 1908c74d40e8SDan Schatzberg 1909c74d40e8SDan Schatzberg if (cmd->memcg_css) { 1910c74d40e8SDan Schatzberg set_active_memcg(old_memcg); 1911c74d40e8SDan Schatzberg css_put(cmd->memcg_css); 1912c74d40e8SDan Schatzberg } 1913b5dd2f60SMing Lei failed: 1914bc07c10aSMing Lei /* complete non-aio request */ 1915fe2cb290SChristoph Hellwig if (!cmd->use_aio || ret) { 19168cd55087SEvan Green if (ret == -EOPNOTSUPP) 19178cd55087SEvan Green cmd->ret = ret; 19188cd55087SEvan Green else 1919fe2cb290SChristoph Hellwig cmd->ret = ret ? -EIO : 0; 192015f73f5bSChristoph Hellwig if (likely(!blk_should_fake_timeout(rq->q))) 19211894e916SJens Axboe blk_mq_complete_request(rq); 1922fe2cb290SChristoph Hellwig } 1923b5dd2f60SMing Lei } 1924b5dd2f60SMing Lei 192587579e9bSDan Schatzberg static void loop_process_work(struct loop_worker *worker, 192687579e9bSDan Schatzberg struct list_head *cmd_list, struct loop_device *lo) 1927b5dd2f60SMing Lei { 192887579e9bSDan Schatzberg int orig_flags = current->flags; 192987579e9bSDan Schatzberg struct loop_cmd *cmd; 1930b5dd2f60SMing Lei 193187579e9bSDan Schatzberg current->flags |= PF_LOCAL_THROTTLE | PF_MEMALLOC_NOIO; 193287579e9bSDan Schatzberg spin_lock_irq(&lo->lo_work_lock); 193387579e9bSDan Schatzberg while (!list_empty(cmd_list)) { 193487579e9bSDan Schatzberg cmd = container_of( 193587579e9bSDan Schatzberg cmd_list->next, struct loop_cmd, list_entry); 193687579e9bSDan Schatzberg list_del(cmd_list->next); 193787579e9bSDan Schatzberg spin_unlock_irq(&lo->lo_work_lock); 193887579e9bSDan Schatzberg 193987579e9bSDan Schatzberg loop_handle_cmd(cmd); 194087579e9bSDan Schatzberg cond_resched(); 194187579e9bSDan Schatzberg 194287579e9bSDan Schatzberg spin_lock_irq(&lo->lo_work_lock); 194387579e9bSDan Schatzberg } 194487579e9bSDan Schatzberg 194587579e9bSDan Schatzberg /* 194687579e9bSDan Schatzberg * We only add to the idle list if there are no pending cmds 194787579e9bSDan Schatzberg * *and* the worker will not run again which ensures that it 194887579e9bSDan Schatzberg * is safe to free any worker on the idle list 194987579e9bSDan Schatzberg */ 195087579e9bSDan Schatzberg if (worker && !work_pending(&worker->work)) { 195187579e9bSDan Schatzberg worker->last_ran_at = jiffies; 195287579e9bSDan Schatzberg list_add_tail(&worker->idle_list, &lo->idle_worker_list); 195387579e9bSDan Schatzberg loop_set_timer(lo); 195487579e9bSDan Schatzberg } 195587579e9bSDan Schatzberg spin_unlock_irq(&lo->lo_work_lock); 195687579e9bSDan Schatzberg current->flags = orig_flags; 195787579e9bSDan Schatzberg } 195887579e9bSDan Schatzberg 195987579e9bSDan Schatzberg static void loop_workfn(struct work_struct *work) 196087579e9bSDan Schatzberg { 196187579e9bSDan Schatzberg struct loop_worker *worker = 196287579e9bSDan Schatzberg container_of(work, struct loop_worker, work); 196387579e9bSDan Schatzberg loop_process_work(worker, &worker->cmd_list, worker->lo); 196487579e9bSDan Schatzberg } 196587579e9bSDan Schatzberg 196687579e9bSDan Schatzberg static void loop_rootcg_workfn(struct work_struct *work) 196787579e9bSDan Schatzberg { 196887579e9bSDan Schatzberg struct loop_device *lo = 196987579e9bSDan Schatzberg container_of(work, struct loop_device, rootcg_work); 197087579e9bSDan Schatzberg loop_process_work(NULL, &lo->rootcg_cmd_list, lo); 197187579e9bSDan Schatzberg } 197287579e9bSDan Schatzberg 1973f363b089SEric Biggers static const struct blk_mq_ops loop_mq_ops = { 1974b5dd2f60SMing Lei .queue_rq = loop_queue_rq, 1975fe2cb290SChristoph Hellwig .complete = lo_complete_rq, 1976b5dd2f60SMing Lei }; 1977b5dd2f60SMing Lei 1978d6da83d0SChristoph Hellwig static int loop_add(int i) 19791da177e4SLinus Torvalds { 198073285082SKen Chen struct loop_device *lo; 198173285082SKen Chen struct gendisk *disk; 198234dd82afSKay Sievers int err; 19831da177e4SLinus Torvalds 198434dd82afSKay Sievers err = -ENOMEM; 198568d740d7SSilva Paulo lo = kzalloc(sizeof(*lo), GFP_KERNEL); 198668d740d7SSilva Paulo if (!lo) 198773285082SKen Chen goto out; 1988b15ed546SChristoph Hellwig lo->worker_tree = RB_ROOT; 1989b15ed546SChristoph Hellwig INIT_LIST_HEAD(&lo->idle_worker_list); 1990b15ed546SChristoph Hellwig timer_setup(&lo->timer, loop_free_idle_workers_timer, TIMER_DEFERRABLE); 1991ef7e7c82SMikulas Patocka lo->lo_state = Lo_unbound; 1992ef7e7c82SMikulas Patocka 199318d1f200SChristoph Hellwig err = mutex_lock_killable(&loop_ctl_mutex); 199418d1f200SChristoph Hellwig if (err) 199518d1f200SChristoph Hellwig goto out_free_dev; 199618d1f200SChristoph Hellwig 1997c718aa65STejun Heo /* allocate id, if @id >= 0, we're requesting that specific id */ 199834dd82afSKay Sievers if (i >= 0) { 1999c718aa65STejun Heo err = idr_alloc(&loop_index_idr, lo, i, i + 1, GFP_KERNEL); 2000c718aa65STejun Heo if (err == -ENOSPC) 200134dd82afSKay Sievers err = -EEXIST; 200234dd82afSKay Sievers } else { 2003c718aa65STejun Heo err = idr_alloc(&loop_index_idr, lo, 0, 0, GFP_KERNEL); 200434dd82afSKay Sievers } 20051c500ad7STetsuo Handa mutex_unlock(&loop_ctl_mutex); 200634dd82afSKay Sievers if (err < 0) 20071c500ad7STetsuo Handa goto out_free_dev; 2008c718aa65STejun Heo i = err; 20091da177e4SLinus Torvalds 2010b5dd2f60SMing Lei lo->tag_set.ops = &loop_mq_ops; 2011b5dd2f60SMing Lei lo->tag_set.nr_hw_queues = 1; 2012ef44c508SChaitanya Kulkarni lo->tag_set.queue_depth = hw_queue_depth; 2013b5dd2f60SMing Lei lo->tag_set.numa_node = NUMA_NO_NODE; 2014b5dd2f60SMing Lei lo->tag_set.cmd_size = sizeof(struct loop_cmd); 20152112f5c1SBart Van Assche lo->tag_set.flags = BLK_MQ_F_SHOULD_MERGE | BLK_MQ_F_STACKING | 20162112f5c1SBart Van Assche BLK_MQ_F_NO_SCHED_BY_DEFAULT; 2017b5dd2f60SMing Lei lo->tag_set.driver_data = lo; 2018b5dd2f60SMing Lei 2019b5dd2f60SMing Lei err = blk_mq_alloc_tag_set(&lo->tag_set); 2020b5dd2f60SMing Lei if (err) 20213ec981e3SMikulas Patocka goto out_free_idr; 202273285082SKen Chen 20231c99502fSChristoph Hellwig disk = lo->lo_disk = blk_mq_alloc_disk(&lo->tag_set, lo); 20241c99502fSChristoph Hellwig if (IS_ERR(disk)) { 20251c99502fSChristoph Hellwig err = PTR_ERR(disk); 2026b5dd2f60SMing Lei goto out_cleanup_tags; 2027b5dd2f60SMing Lei } 20281c99502fSChristoph Hellwig lo->lo_queue = lo->lo_disk->queue; 2029ef7e7c82SMikulas Patocka 203054bb0adeSShaohua Li blk_queue_max_hw_sectors(lo->lo_queue, BLK_DEF_MAX_SECTORS); 203140326d8aSShaohua Li 20325b5e20f4SMing Lei /* 203340326d8aSShaohua Li * By default, we do buffer IO, so it doesn't make sense to enable 203440326d8aSShaohua Li * merge because the I/O submitted to backing file is handled page by 203540326d8aSShaohua Li * page. For directio mode, merge does help to dispatch bigger request 203640326d8aSShaohua Li * to underlayer disk. We will enable merge once directio is enabled. 20375b5e20f4SMing Lei */ 20388b904b5bSBart Van Assche blk_queue_flag_set(QUEUE_FLAG_NOMERGES, lo->lo_queue); 20395b5e20f4SMing Lei 2040e03c8dd1SKay Sievers /* 2041e03c8dd1SKay Sievers * Disable partition scanning by default. The in-kernel partition 2042e03c8dd1SKay Sievers * scanning can be requested individually per-device during its 2043e03c8dd1SKay Sievers * setup. Userspace can always add and remove partitions from all 2044e03c8dd1SKay Sievers * devices. The needed partition minors are allocated from the 2045e03c8dd1SKay Sievers * extended minor space, the main loop device numbers will continue 2046e03c8dd1SKay Sievers * to match the loop minors, regardless of the number of partitions 2047e03c8dd1SKay Sievers * used. 2048e03c8dd1SKay Sievers * 2049e03c8dd1SKay Sievers * If max_part is given, partition scanning is globally enabled for 2050e03c8dd1SKay Sievers * all loop devices. The minors for the main loop devices will be 2051e03c8dd1SKay Sievers * multiples of max_part. 2052e03c8dd1SKay Sievers * 2053e03c8dd1SKay Sievers * Note: Global-for-all-devices, set-only-at-init, read-only module 2054e03c8dd1SKay Sievers * parameteters like 'max_loop' and 'max_part' make things needlessly 2055e03c8dd1SKay Sievers * complicated, are too static, inflexible and may surprise 2056e03c8dd1SKay Sievers * userspace tools. Parameters like this in general should be avoided. 2057e03c8dd1SKay Sievers */ 2058e03c8dd1SKay Sievers if (!part_shift) 205946e7eac6SChristoph Hellwig disk->flags |= GENHD_FL_NO_PART; 20606cc8e743SPavel Tatashin mutex_init(&lo->lo_mutex); 20611da177e4SLinus Torvalds lo->lo_number = i; 20621da177e4SLinus Torvalds spin_lock_init(&lo->lo_lock); 206387579e9bSDan Schatzberg spin_lock_init(&lo->lo_work_lock); 2064d292dc80SChristoph Hellwig INIT_WORK(&lo->rootcg_work, loop_rootcg_workfn); 2065d292dc80SChristoph Hellwig INIT_LIST_HEAD(&lo->rootcg_cmd_list); 20661da177e4SLinus Torvalds disk->major = LOOP_MAJOR; 2067476a4813SLaurent Vivier disk->first_minor = i << part_shift; 20681c99502fSChristoph Hellwig disk->minors = 1 << part_shift; 20691da177e4SLinus Torvalds disk->fops = &lo_fops; 20701da177e4SLinus Torvalds disk->private_data = lo; 20711da177e4SLinus Torvalds disk->queue = lo->lo_queue; 20729f65c489SMatteo Croce disk->events = DISK_EVENT_MEDIA_CHANGE; 20739f65c489SMatteo Croce disk->event_flags = DISK_EVENT_FLAG_UEVENT; 207473285082SKen Chen sprintf(disk->disk_name, "loop%d", i); 20751c500ad7STetsuo Handa /* Make this loop device reachable from pathname. */ 2076905705f0SLuis Chamberlain err = add_disk(disk); 2077905705f0SLuis Chamberlain if (err) 2078905705f0SLuis Chamberlain goto out_cleanup_disk; 2079905705f0SLuis Chamberlain 20801c500ad7STetsuo Handa /* Show this loop device. */ 20811c500ad7STetsuo Handa mutex_lock(&loop_ctl_mutex); 20821c500ad7STetsuo Handa lo->idr_visible = true; 208318d1f200SChristoph Hellwig mutex_unlock(&loop_ctl_mutex); 2084905705f0SLuis Chamberlain 208518d1f200SChristoph Hellwig return i; 208673285082SKen Chen 2087905705f0SLuis Chamberlain out_cleanup_disk: 2088905705f0SLuis Chamberlain blk_cleanup_disk(disk); 2089b5dd2f60SMing Lei out_cleanup_tags: 2090b5dd2f60SMing Lei blk_mq_free_tag_set(&lo->tag_set); 20913ec981e3SMikulas Patocka out_free_idr: 20921c500ad7STetsuo Handa mutex_lock(&loop_ctl_mutex); 20933ec981e3SMikulas Patocka idr_remove(&loop_index_idr, i); 209418d1f200SChristoph Hellwig mutex_unlock(&loop_ctl_mutex); 209573285082SKen Chen out_free_dev: 209673285082SKen Chen kfree(lo); 209773285082SKen Chen out: 209834dd82afSKay Sievers return err; 20991da177e4SLinus Torvalds } 21001da177e4SLinus Torvalds 210134dd82afSKay Sievers static void loop_remove(struct loop_device *lo) 210273285082SKen Chen { 21031c500ad7STetsuo Handa /* Make this loop device unreachable from pathname. */ 21046cd18e71SNeilBrown del_gendisk(lo->lo_disk); 2105d2c7f56fSChristoph Hellwig blk_cleanup_queue(lo->lo_disk->queue); 2106b5dd2f60SMing Lei blk_mq_free_tag_set(&lo->tag_set); 2107ce8d7861SChristoph Hellwig 21081c500ad7STetsuo Handa mutex_lock(&loop_ctl_mutex); 21091c500ad7STetsuo Handa idr_remove(&loop_index_idr, lo->lo_number); 21101c500ad7STetsuo Handa mutex_unlock(&loop_ctl_mutex); 2111d2c7f56fSChristoph Hellwig 2112d2c7f56fSChristoph Hellwig put_disk(lo->lo_disk); 211373285082SKen Chen } 211473285082SKen Chen 21158410d38cSChristoph Hellwig static void loop_probe(dev_t dev) 211673285082SKen Chen { 21178410d38cSChristoph Hellwig int idx = MINOR(dev) >> part_shift; 21188410d38cSChristoph Hellwig 21198410d38cSChristoph Hellwig if (max_loop && idx >= max_loop) 21208410d38cSChristoph Hellwig return; 2121d6da83d0SChristoph Hellwig loop_add(idx); 2122f9d10764SChristoph Hellwig } 2123f9d10764SChristoph Hellwig 2124f9d10764SChristoph Hellwig static int loop_control_remove(int idx) 2125770fe30aSKay Sievers { 2126770fe30aSKay Sievers struct loop_device *lo; 21270a42e99bSJan Kara int ret; 2128e5d66a10SChristoph Hellwig 2129e5d66a10SChristoph Hellwig if (idx < 0) { 2130e3f9387aSTetsuo Handa pr_warn_once("deleting an unspecified loop device is not supported.\n"); 2131e5d66a10SChristoph Hellwig return -EINVAL; 2132e5d66a10SChristoph Hellwig } 2133770fe30aSKay Sievers 21341c500ad7STetsuo Handa /* Hide this loop device for serialization. */ 21350a42e99bSJan Kara ret = mutex_lock_killable(&loop_ctl_mutex); 21360a42e99bSJan Kara if (ret) 21370a42e99bSJan Kara return ret; 2138b9848081SChristoph Hellwig lo = idr_find(&loop_index_idr, idx); 21391c500ad7STetsuo Handa if (!lo || !lo->idr_visible) 2140b9848081SChristoph Hellwig ret = -ENODEV; 21411c500ad7STetsuo Handa else 21421c500ad7STetsuo Handa lo->idr_visible = false; 21431c500ad7STetsuo Handa mutex_unlock(&loop_ctl_mutex); 21441c500ad7STetsuo Handa if (ret) 21451c500ad7STetsuo Handa return ret; 2146f9d10764SChristoph Hellwig 21471c500ad7STetsuo Handa /* Check whether this loop device can be removed. */ 21486cc8e743SPavel Tatashin ret = mutex_lock_killable(&lo->lo_mutex); 21496cc8e743SPavel Tatashin if (ret) 21501c500ad7STetsuo Handa goto mark_visible; 2151a0e286b6SChristoph Hellwig if (lo->lo_state != Lo_unbound || disk_openers(lo->lo_disk) > 0) { 21526cc8e743SPavel Tatashin mutex_unlock(&lo->lo_mutex); 2153770fe30aSKay Sievers ret = -EBUSY; 21541c500ad7STetsuo Handa goto mark_visible; 2155770fe30aSKay Sievers } 2156a0e286b6SChristoph Hellwig /* Mark this loop device as no more bound, but not quite unbound yet */ 2157990e7811SChristoph Hellwig lo->lo_state = Lo_deleting; 21586cc8e743SPavel Tatashin mutex_unlock(&lo->lo_mutex); 2159f9d10764SChristoph Hellwig 2160770fe30aSKay Sievers loop_remove(lo); 21611c500ad7STetsuo Handa return 0; 21621c500ad7STetsuo Handa 21631c500ad7STetsuo Handa mark_visible: 21641c500ad7STetsuo Handa /* Show this loop device again. */ 21651c500ad7STetsuo Handa mutex_lock(&loop_ctl_mutex); 21661c500ad7STetsuo Handa lo->idr_visible = true; 2167f9d10764SChristoph Hellwig mutex_unlock(&loop_ctl_mutex); 2168f9d10764SChristoph Hellwig return ret; 2169770fe30aSKay Sievers } 2170f9d10764SChristoph Hellwig 2171f9d10764SChristoph Hellwig static int loop_control_get_free(int idx) 2172f9d10764SChristoph Hellwig { 2173f9d10764SChristoph Hellwig struct loop_device *lo; 2174b9848081SChristoph Hellwig int id, ret; 2175f9d10764SChristoph Hellwig 2176f9d10764SChristoph Hellwig ret = mutex_lock_killable(&loop_ctl_mutex); 2177f9d10764SChristoph Hellwig if (ret) 2178f9d10764SChristoph Hellwig return ret; 2179b9848081SChristoph Hellwig idr_for_each_entry(&loop_index_idr, lo, id) { 21801c500ad7STetsuo Handa /* Hitting a race results in creating a new loop device which is harmless. */ 21811c500ad7STetsuo Handa if (lo->idr_visible && data_race(lo->lo_state) == Lo_unbound) 2182b9848081SChristoph Hellwig goto found; 2183b9848081SChristoph Hellwig } 21840a42e99bSJan Kara mutex_unlock(&loop_ctl_mutex); 218518d1f200SChristoph Hellwig return loop_add(-1); 2186b9848081SChristoph Hellwig found: 2187b9848081SChristoph Hellwig mutex_unlock(&loop_ctl_mutex); 2188b9848081SChristoph Hellwig return id; 2189770fe30aSKay Sievers } 2190770fe30aSKay Sievers 2191f9d10764SChristoph Hellwig static long loop_control_ioctl(struct file *file, unsigned int cmd, 2192f9d10764SChristoph Hellwig unsigned long parm) 2193f9d10764SChristoph Hellwig { 2194f9d10764SChristoph Hellwig switch (cmd) { 2195f9d10764SChristoph Hellwig case LOOP_CTL_ADD: 219618d1f200SChristoph Hellwig return loop_add(parm); 2197f9d10764SChristoph Hellwig case LOOP_CTL_REMOVE: 2198f9d10764SChristoph Hellwig return loop_control_remove(parm); 2199f9d10764SChristoph Hellwig case LOOP_CTL_GET_FREE: 2200f9d10764SChristoph Hellwig return loop_control_get_free(parm); 2201f9d10764SChristoph Hellwig default: 2202f9d10764SChristoph Hellwig return -ENOSYS; 2203f9d10764SChristoph Hellwig } 2204f9d10764SChristoph Hellwig } 2205f9d10764SChristoph Hellwig 2206770fe30aSKay Sievers static const struct file_operations loop_ctl_fops = { 2207770fe30aSKay Sievers .open = nonseekable_open, 2208770fe30aSKay Sievers .unlocked_ioctl = loop_control_ioctl, 2209770fe30aSKay Sievers .compat_ioctl = loop_control_ioctl, 2210770fe30aSKay Sievers .owner = THIS_MODULE, 2211770fe30aSKay Sievers .llseek = noop_llseek, 2212770fe30aSKay Sievers }; 2213770fe30aSKay Sievers 2214770fe30aSKay Sievers static struct miscdevice loop_misc = { 2215770fe30aSKay Sievers .minor = LOOP_CTRL_MINOR, 2216770fe30aSKay Sievers .name = "loop-control", 2217770fe30aSKay Sievers .fops = &loop_ctl_fops, 2218770fe30aSKay Sievers }; 2219770fe30aSKay Sievers 2220770fe30aSKay Sievers MODULE_ALIAS_MISCDEV(LOOP_CTRL_MINOR); 2221770fe30aSKay Sievers MODULE_ALIAS("devname:loop-control"); 2222770fe30aSKay Sievers 222373285082SKen Chen static int __init loop_init(void) 222473285082SKen Chen { 2225a47653fcSKen Chen int i, nr; 2226770fe30aSKay Sievers int err; 2227a47653fcSKen Chen 2228476a4813SLaurent Vivier part_shift = 0; 2229ac04fee0SNamhyung Kim if (max_part > 0) { 2230476a4813SLaurent Vivier part_shift = fls(max_part); 2231476a4813SLaurent Vivier 2232ac04fee0SNamhyung Kim /* 2233ac04fee0SNamhyung Kim * Adjust max_part according to part_shift as it is exported 2234ac04fee0SNamhyung Kim * to user space so that user can decide correct minor number 2235ac04fee0SNamhyung Kim * if [s]he want to create more devices. 2236ac04fee0SNamhyung Kim * 2237ac04fee0SNamhyung Kim * Note that -1 is required because partition 0 is reserved 2238ac04fee0SNamhyung Kim * for the whole disk. 2239ac04fee0SNamhyung Kim */ 2240ac04fee0SNamhyung Kim max_part = (1UL << part_shift) - 1; 2241ac04fee0SNamhyung Kim } 2242ac04fee0SNamhyung Kim 2243b1a66504SGuo Chao if ((1UL << part_shift) > DISK_MAX_PARTS) { 2244b1a66504SGuo Chao err = -EINVAL; 2245a8c1d064SAnton Volkov goto err_out; 2246b1a66504SGuo Chao } 224778f4bb36SNamhyung Kim 2248b1a66504SGuo Chao if (max_loop > 1UL << (MINORBITS - part_shift)) { 2249b1a66504SGuo Chao err = -EINVAL; 2250a8c1d064SAnton Volkov goto err_out; 2251b1a66504SGuo Chao } 225273285082SKen Chen 2253d134b00bSKay Sievers /* 2254d134b00bSKay Sievers * If max_loop is specified, create that many devices upfront. 2255d134b00bSKay Sievers * This also becomes a hard limit. If max_loop is not specified, 2256d134b00bSKay Sievers * create CONFIG_BLK_DEV_LOOP_MIN_COUNT loop devices at module 2257d134b00bSKay Sievers * init time. Loop devices can be requested on-demand with the 2258d134b00bSKay Sievers * /dev/loop-control interface, or be instantiated by accessing 2259d134b00bSKay Sievers * a 'dead' device node. 2260d134b00bSKay Sievers */ 2261aeb2b0b1SLukas Bulwahn if (max_loop) 2262a47653fcSKen Chen nr = max_loop; 2263aeb2b0b1SLukas Bulwahn else 2264d134b00bSKay Sievers nr = CONFIG_BLK_DEV_LOOP_MIN_COUNT; 2265a47653fcSKen Chen 2266a8c1d064SAnton Volkov err = misc_register(&loop_misc); 2267a8c1d064SAnton Volkov if (err < 0) 2268a8c1d064SAnton Volkov goto err_out; 2269a8c1d064SAnton Volkov 2270a8c1d064SAnton Volkov 22718410d38cSChristoph Hellwig if (__register_blkdev(LOOP_MAJOR, "loop", loop_probe)) { 2272b1a66504SGuo Chao err = -EIO; 2273b1a66504SGuo Chao goto misc_out; 2274b1a66504SGuo Chao } 2275a47653fcSKen Chen 2276d134b00bSKay Sievers /* pre-create number of devices given by config or max_loop */ 227734dd82afSKay Sievers for (i = 0; i < nr; i++) 2278d6da83d0SChristoph Hellwig loop_add(i); 227934dd82afSKay Sievers 228073285082SKen Chen printk(KERN_INFO "loop: module loaded\n"); 22811da177e4SLinus Torvalds return 0; 2282b1a66504SGuo Chao 2283b1a66504SGuo Chao misc_out: 2284b1a66504SGuo Chao misc_deregister(&loop_misc); 2285a8c1d064SAnton Volkov err_out: 2286b1a66504SGuo Chao return err; 228734dd82afSKay Sievers } 2288a47653fcSKen Chen 228973285082SKen Chen static void __exit loop_exit(void) 22901da177e4SLinus Torvalds { 22918e60947dSChristoph Hellwig struct loop_device *lo; 22928e60947dSChristoph Hellwig int id; 22938e60947dSChristoph Hellwig 22948b52d8beSChristoph Hellwig unregister_blkdev(LOOP_MAJOR, "loop"); 22958b52d8beSChristoph Hellwig misc_deregister(&loop_misc); 2296200f9337SLuis Chamberlain 22971c500ad7STetsuo Handa /* 22981c500ad7STetsuo Handa * There is no need to use loop_ctl_mutex here, for nobody else can 22991c500ad7STetsuo Handa * access loop_index_idr when this module is unloading (unless forced 23001c500ad7STetsuo Handa * module unloading is requested). If this is not a clean unloading, 23011c500ad7STetsuo Handa * we have no means to avoid kernel crash. 23021c500ad7STetsuo Handa */ 23038e60947dSChristoph Hellwig idr_for_each_entry(&loop_index_idr, lo, id) 23048e60947dSChristoph Hellwig loop_remove(lo); 2305bd5c39edSChristoph Hellwig 2306bd5c39edSChristoph Hellwig idr_destroy(&loop_index_idr); 23071da177e4SLinus Torvalds } 23081da177e4SLinus Torvalds 23091da177e4SLinus Torvalds module_init(loop_init); 23101da177e4SLinus Torvalds module_exit(loop_exit); 23111da177e4SLinus Torvalds 23121da177e4SLinus Torvalds #ifndef MODULE 23131da177e4SLinus Torvalds static int __init max_loop_setup(char *str) 23141da177e4SLinus Torvalds { 23151da177e4SLinus Torvalds max_loop = simple_strtol(str, NULL, 0); 23161da177e4SLinus Torvalds return 1; 23171da177e4SLinus Torvalds } 23181da177e4SLinus Torvalds 23191da177e4SLinus Torvalds __setup("max_loop=", max_loop_setup); 23201da177e4SLinus Torvalds #endif 2321