Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* |
| 2 | * linux/drivers/block/loop.c |
| 3 | * |
| 4 | * Written by Theodore Ts'o, 3/29/93 |
| 5 | * |
| 6 | * Copyright 1993 by Theodore Ts'o. Redistribution of this file is |
| 7 | * permitted under the GNU General Public License. |
| 8 | * |
| 9 | * DES encryption plus some minor changes by Werner Almesberger, 30-MAY-1993 |
| 10 | * more DES encryption plus IDEA encryption by Nicholas J. Leon, June 20, 1996 |
| 11 | * |
| 12 | * Modularized and updated for 1.1.16 kernel - Mitch Dsouza 28th May 1994 |
| 13 | * Adapted for 1.3.59 kernel - Andries Brouwer, 1 Feb 1996 |
| 14 | * |
| 15 | * Fixed do_loop_request() re-entrancy - Vincent.Renardias@waw.com Mar 20, 1997 |
| 16 | * |
| 17 | * Added devfs support - Richard Gooch <rgooch@atnf.csiro.au> 16-Jan-1998 |
| 18 | * |
| 19 | * Handle sparse backing files correctly - Kenn Humborg, Jun 28, 1998 |
| 20 | * |
| 21 | * Loadable modules and other fixes by AK, 1998 |
| 22 | * |
| 23 | * Make real block number available to downstream transfer functions, enables |
| 24 | * CBC (and relatives) mode encryption requiring unique IVs per data block. |
| 25 | * Reed H. Petty, rhp@draper.net |
| 26 | * |
| 27 | * Maximum number of loop devices now dynamic via max_loop module parameter. |
| 28 | * Russell Kroll <rkroll@exploits.org> 19990701 |
| 29 | * |
| 30 | * Maximum number of loop devices when compiled-in now selectable by passing |
| 31 | * max_loop=<1-255> to the kernel on boot. |
Jan Engelhardt | 96de0e2 | 2007-10-19 23:21:04 +0200 | [diff] [blame] | 32 | * Erik I. Bolsø, <eriki@himolde.no>, Oct 31, 1999 |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 33 | * |
| 34 | * Completely rewrite request handling to be make_request_fn style and |
| 35 | * non blocking, pushing work to a helper thread. Lots of fixes from |
| 36 | * Al Viro too. |
| 37 | * Jens Axboe <axboe@suse.de>, Nov 2000 |
| 38 | * |
| 39 | * Support up to 256 loop devices |
| 40 | * Heinz Mauelshagen <mge@sistina.com>, Feb 2002 |
| 41 | * |
| 42 | * Support for falling back on the write file operation when the address space |
Nick Piggin | 4e02ed4 | 2008-10-29 14:00:55 -0700 | [diff] [blame] | 43 | * operations write_begin is not available on the backing filesystem. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 44 | * Anton Altaparmakov, 16 Feb 2005 |
| 45 | * |
| 46 | * Still To Fix: |
| 47 | * - Advisory locking is ignored here. |
| 48 | * - Should use an own CAP_* category instead of CAP_SYS_ADMIN |
| 49 | * |
| 50 | */ |
| 51 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 52 | #include <linux/module.h> |
| 53 | #include <linux/moduleparam.h> |
| 54 | #include <linux/sched.h> |
| 55 | #include <linux/fs.h> |
Matthew Wilcox (Oracle) | 4ee60ec | 2021-05-06 18:02:27 -0700 | [diff] [blame] | 56 | #include <linux/pagemap.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 57 | #include <linux/file.h> |
| 58 | #include <linux/stat.h> |
| 59 | #include <linux/errno.h> |
| 60 | #include <linux/major.h> |
| 61 | #include <linux/wait.h> |
| 62 | #include <linux/blkdev.h> |
| 63 | #include <linux/blkpg.h> |
| 64 | #include <linux/init.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 65 | #include <linux/swap.h> |
| 66 | #include <linux/slab.h> |
David Howells | 863d5b82 | 2006-08-29 19:06:14 +0100 | [diff] [blame] | 67 | #include <linux/compat.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 68 | #include <linux/suspend.h> |
Rafael J. Wysocki | 8314418 | 2007-07-17 04:03:35 -0700 | [diff] [blame] | 69 | #include <linux/freezer.h> |
Arnd Bergmann | 2a48fc0 | 2010-06-02 14:28:52 +0200 | [diff] [blame] | 70 | #include <linux/mutex.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 71 | #include <linux/writeback.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 72 | #include <linux/completion.h> |
| 73 | #include <linux/highmem.h> |
Jens Axboe | d6b29d7 | 2007-06-04 09:59:47 +0200 | [diff] [blame] | 74 | #include <linux/splice.h> |
Milan Broz | ee86273 | 2010-08-23 15:16:00 +0200 | [diff] [blame] | 75 | #include <linux/sysfs.h> |
Kay Sievers | 770fe30 | 2011-07-31 22:08:04 +0200 | [diff] [blame] | 76 | #include <linux/miscdevice.h> |
Lukas Czerner | dfaa2ef | 2011-08-19 14:50:46 +0200 | [diff] [blame] | 77 | #include <linux/falloc.h> |
Al Viro | 283e7e5 | 2015-04-03 15:21:59 -0400 | [diff] [blame] | 78 | #include <linux/uio.h> |
Adam Manzanares | d9a08a9 | 2018-05-22 10:52:19 -0700 | [diff] [blame] | 79 | #include <linux/ioprio.h> |
Dennis Zhou | db6638d | 2018-12-05 12:10:35 -0500 | [diff] [blame] | 80 | #include <linux/blk-cgroup.h> |
Dan Schatzberg | c74d40e | 2021-06-28 19:38:21 -0700 | [diff] [blame] | 81 | #include <linux/sched/mm.h> |
Adam Manzanares | d9a08a9 | 2018-05-22 10:52:19 -0700 | [diff] [blame] | 82 | |
Al Viro | 83a8761 | 2013-05-12 10:14:07 -0400 | [diff] [blame] | 83 | #include "loop.h" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 84 | |
Linus Torvalds | 7c0f6ba | 2016-12-24 11:46:01 -0800 | [diff] [blame] | 85 | #include <linux/uaccess.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 86 | |
Dan Schatzberg | 87579e9 | 2021-06-28 19:38:15 -0700 | [diff] [blame] | 87 | #define LOOP_IDLE_WORKER_TIMEOUT (60 * HZ) |
| 88 | |
Kay Sievers | 34dd82a | 2011-07-31 22:08:04 +0200 | [diff] [blame] | 89 | static DEFINE_IDR(loop_index_idr); |
Tetsuo Handa | 310ca16 | 2018-11-08 14:01:02 +0100 | [diff] [blame] | 90 | static DEFINE_MUTEX(loop_ctl_mutex); |
Tetsuo Handa | 3ce6e1f | 2021-07-06 23:40:34 +0900 | [diff] [blame] | 91 | static DEFINE_MUTEX(loop_validate_mutex); |
| 92 | |
| 93 | /** |
| 94 | * loop_global_lock_killable() - take locks for safe loop_validate_file() test |
| 95 | * |
| 96 | * @lo: struct loop_device |
| 97 | * @global: true if @lo is about to bind another "struct loop_device", false otherwise |
| 98 | * |
| 99 | * Returns 0 on success, -EINTR otherwise. |
| 100 | * |
| 101 | * Since loop_validate_file() traverses on other "struct loop_device" if |
| 102 | * is_loop_device() is true, we need a global lock for serializing concurrent |
| 103 | * loop_configure()/loop_change_fd()/__loop_clr_fd() calls. |
| 104 | */ |
| 105 | static int loop_global_lock_killable(struct loop_device *lo, bool global) |
| 106 | { |
| 107 | int err; |
| 108 | |
| 109 | if (global) { |
| 110 | err = mutex_lock_killable(&loop_validate_mutex); |
| 111 | if (err) |
| 112 | return err; |
| 113 | } |
| 114 | err = mutex_lock_killable(&lo->lo_mutex); |
| 115 | if (err && global) |
| 116 | mutex_unlock(&loop_validate_mutex); |
| 117 | return err; |
| 118 | } |
| 119 | |
| 120 | /** |
| 121 | * loop_global_unlock() - release locks taken by loop_global_lock_killable() |
| 122 | * |
| 123 | * @lo: struct loop_device |
| 124 | * @global: true if @lo was about to bind another "struct loop_device", false otherwise |
| 125 | */ |
| 126 | static void loop_global_unlock(struct loop_device *lo, bool global) |
| 127 | { |
| 128 | mutex_unlock(&lo->lo_mutex); |
| 129 | if (global) |
| 130 | mutex_unlock(&loop_validate_mutex); |
| 131 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 132 | |
Laurent Vivier | 476a481 | 2008-03-26 12:11:53 +0100 | [diff] [blame] | 133 | static int max_part; |
| 134 | static int part_shift; |
| 135 | |
Dmitry Monakhov | 7035b5d | 2011-11-16 09:21:49 +0100 | [diff] [blame] | 136 | static loff_t get_size(loff_t offset, loff_t sizelimit, struct file *file) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 137 | { |
Guo Chao | b7a1da6 | 2013-02-21 15:16:50 -0800 | [diff] [blame] | 138 | loff_t loopsize; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 139 | |
| 140 | /* Compute loopsize in bytes */ |
Guo Chao | b7a1da6 | 2013-02-21 15:16:50 -0800 | [diff] [blame] | 141 | loopsize = i_size_read(file->f_mapping->host); |
| 142 | if (offset > 0) |
| 143 | loopsize -= offset; |
| 144 | /* offset is beyond i_size, weird but possible */ |
Dmitry Monakhov | 7035b5d | 2011-11-16 09:21:49 +0100 | [diff] [blame] | 145 | if (loopsize < 0) |
| 146 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 147 | |
Dmitry Monakhov | 7035b5d | 2011-11-16 09:21:49 +0100 | [diff] [blame] | 148 | if (sizelimit > 0 && sizelimit < loopsize) |
| 149 | loopsize = sizelimit; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 150 | /* |
| 151 | * Unfortunately, if we want to do I/O on the device, |
| 152 | * the number of 512-byte sectors has to fit into a sector_t. |
| 153 | */ |
| 154 | return loopsize >> 9; |
| 155 | } |
| 156 | |
Dmitry Monakhov | 7035b5d | 2011-11-16 09:21:49 +0100 | [diff] [blame] | 157 | static loff_t get_loop_size(struct loop_device *lo, struct file *file) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 158 | { |
Dmitry Monakhov | 7035b5d | 2011-11-16 09:21:49 +0100 | [diff] [blame] | 159 | return get_size(lo->lo_offset, lo->lo_sizelimit, file); |
| 160 | } |
| 161 | |
Ming Lei | 2e5ab5f | 2015-08-17 10:31:49 +0800 | [diff] [blame] | 162 | static void __loop_update_dio(struct loop_device *lo, bool dio) |
| 163 | { |
| 164 | struct file *file = lo->lo_backing_file; |
| 165 | struct address_space *mapping = file->f_mapping; |
| 166 | struct inode *inode = mapping->host; |
| 167 | unsigned short sb_bsize = 0; |
| 168 | unsigned dio_align = 0; |
| 169 | bool use_dio; |
| 170 | |
| 171 | if (inode->i_sb->s_bdev) { |
| 172 | sb_bsize = bdev_logical_block_size(inode->i_sb->s_bdev); |
| 173 | dio_align = sb_bsize - 1; |
| 174 | } |
| 175 | |
| 176 | /* |
| 177 | * We support direct I/O only if lo_offset is aligned with the |
| 178 | * logical I/O size of backing device, and the logical block |
Christoph Hellwig | 47e9624 | 2021-10-19 09:56:39 +0200 | [diff] [blame] | 179 | * size of loop is bigger than the backing device's. |
Ming Lei | 2e5ab5f | 2015-08-17 10:31:49 +0800 | [diff] [blame] | 180 | * |
| 181 | * TODO: the above condition may be loosed in the future, and |
| 182 | * direct I/O may be switched runtime at that time because most |
Masahiro Yamada | 89d790a | 2017-02-27 14:29:01 -0800 | [diff] [blame] | 183 | * of requests in sane applications should be PAGE_SIZE aligned |
Ming Lei | 2e5ab5f | 2015-08-17 10:31:49 +0800 | [diff] [blame] | 184 | */ |
| 185 | if (dio) { |
| 186 | if (queue_logical_block_size(lo->lo_queue) >= sb_bsize && |
| 187 | !(lo->lo_offset & dio_align) && |
Christoph Hellwig | 47e9624 | 2021-10-19 09:56:39 +0200 | [diff] [blame] | 188 | mapping->a_ops->direct_IO) |
Ming Lei | 2e5ab5f | 2015-08-17 10:31:49 +0800 | [diff] [blame] | 189 | use_dio = true; |
| 190 | else |
| 191 | use_dio = false; |
| 192 | } else { |
| 193 | use_dio = false; |
| 194 | } |
| 195 | |
| 196 | if (lo->use_dio == use_dio) |
| 197 | return; |
| 198 | |
| 199 | /* flush dirty pages before changing direct IO */ |
| 200 | vfs_fsync(file, 0); |
| 201 | |
| 202 | /* |
| 203 | * The flag of LO_FLAGS_DIRECT_IO is handled similarly with |
| 204 | * LO_FLAGS_READ_ONLY, both are set from kernel, and losetup |
| 205 | * will get updated by ioctl(LOOP_GET_STATUS) |
| 206 | */ |
Martijn Coenen | 0fbcf57 | 2020-03-10 14:06:54 +0100 | [diff] [blame] | 207 | if (lo->lo_state == Lo_bound) |
| 208 | blk_mq_freeze_queue(lo->lo_queue); |
Ming Lei | 2e5ab5f | 2015-08-17 10:31:49 +0800 | [diff] [blame] | 209 | lo->use_dio = use_dio; |
Shaohua Li | 40326d8 | 2017-08-31 22:09:46 -0700 | [diff] [blame] | 210 | if (use_dio) { |
Bart Van Assche | 8b904b5 | 2018-03-07 17:10:10 -0800 | [diff] [blame] | 211 | blk_queue_flag_clear(QUEUE_FLAG_NOMERGES, lo->lo_queue); |
Ming Lei | 2e5ab5f | 2015-08-17 10:31:49 +0800 | [diff] [blame] | 212 | lo->lo_flags |= LO_FLAGS_DIRECT_IO; |
Shaohua Li | 40326d8 | 2017-08-31 22:09:46 -0700 | [diff] [blame] | 213 | } else { |
Bart Van Assche | 8b904b5 | 2018-03-07 17:10:10 -0800 | [diff] [blame] | 214 | blk_queue_flag_set(QUEUE_FLAG_NOMERGES, lo->lo_queue); |
Ming Lei | 2e5ab5f | 2015-08-17 10:31:49 +0800 | [diff] [blame] | 215 | lo->lo_flags &= ~LO_FLAGS_DIRECT_IO; |
Shaohua Li | 40326d8 | 2017-08-31 22:09:46 -0700 | [diff] [blame] | 216 | } |
Martijn Coenen | 0fbcf57 | 2020-03-10 14:06:54 +0100 | [diff] [blame] | 217 | if (lo->lo_state == Lo_bound) |
| 218 | blk_mq_unfreeze_queue(lo->lo_queue); |
Ming Lei | 2e5ab5f | 2015-08-17 10:31:49 +0800 | [diff] [blame] | 219 | } |
| 220 | |
Martijn Coenen | 5795b6f | 2020-05-13 15:38:37 +0200 | [diff] [blame] | 221 | /** |
| 222 | * loop_set_size() - sets device size and notifies userspace |
| 223 | * @lo: struct loop_device to set the size for |
| 224 | * @size: new size of the loop device |
| 225 | * |
| 226 | * Callers must validate that the size passed into this function fits into |
| 227 | * a sector_t, eg using loop_validate_size() |
| 228 | */ |
| 229 | static void loop_set_size(struct loop_device *lo, loff_t size) |
| 230 | { |
Christoph Hellwig | 449f4ec | 2020-11-16 15:56:56 +0100 | [diff] [blame] | 231 | if (!set_capacity_and_notify(lo->lo_disk, size)) |
Christoph Hellwig | 3b4f85d | 2020-11-16 15:56:53 +0100 | [diff] [blame] | 232 | kobject_uevent(&disk_to_dev(lo->lo_disk)->kobj, KOBJ_CHANGE); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 233 | } |
| 234 | |
Christoph Hellwig | aa4d861 | 2015-04-07 18:23:29 +0200 | [diff] [blame] | 235 | static int lo_write_bvec(struct file *file, struct bio_vec *bvec, loff_t *ppos) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 236 | { |
Christoph Hellwig | aa4d861 | 2015-04-07 18:23:29 +0200 | [diff] [blame] | 237 | struct iov_iter i; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 238 | ssize_t bw; |
Al Viro | 283e7e5 | 2015-04-03 15:21:59 -0400 | [diff] [blame] | 239 | |
Christoph Hellwig | b620743 | 2019-06-26 15:49:28 +0200 | [diff] [blame] | 240 | iov_iter_bvec(&i, WRITE, bvec, 1, bvec->bv_len); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 241 | |
Al Viro | 03d95eb | 2013-03-20 13:04:20 -0400 | [diff] [blame] | 242 | file_start_write(file); |
Christoph Hellwig | abbb6589 | 2017-05-27 11:16:52 +0300 | [diff] [blame] | 243 | bw = vfs_iter_write(file, &i, ppos, 0); |
Al Viro | 03d95eb | 2013-03-20 13:04:20 -0400 | [diff] [blame] | 244 | file_end_write(file); |
Christoph Hellwig | aa4d861 | 2015-04-07 18:23:29 +0200 | [diff] [blame] | 245 | |
| 246 | if (likely(bw == bvec->bv_len)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 247 | return 0; |
Christoph Hellwig | aa4d861 | 2015-04-07 18:23:29 +0200 | [diff] [blame] | 248 | |
| 249 | printk_ratelimited(KERN_ERR |
| 250 | "loop: Write error at byte offset %llu, length %i.\n", |
| 251 | (unsigned long long)*ppos, bvec->bv_len); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 252 | if (bw >= 0) |
| 253 | bw = -EIO; |
| 254 | return bw; |
| 255 | } |
| 256 | |
Christoph Hellwig | aa4d861 | 2015-04-07 18:23:29 +0200 | [diff] [blame] | 257 | static int lo_write_simple(struct loop_device *lo, struct request *rq, |
| 258 | loff_t pos) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 259 | { |
Christoph Hellwig | aa4d861 | 2015-04-07 18:23:29 +0200 | [diff] [blame] | 260 | struct bio_vec bvec; |
| 261 | struct req_iterator iter; |
| 262 | int ret = 0; |
| 263 | |
| 264 | rq_for_each_segment(bvec, rq, iter) { |
| 265 | ret = lo_write_bvec(lo->lo_backing_file, &bvec, &pos); |
| 266 | if (ret < 0) |
| 267 | break; |
| 268 | cond_resched(); |
| 269 | } |
| 270 | |
| 271 | return ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 272 | } |
| 273 | |
Christoph Hellwig | aa4d861 | 2015-04-07 18:23:29 +0200 | [diff] [blame] | 274 | static int lo_read_simple(struct loop_device *lo, struct request *rq, |
| 275 | loff_t pos) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 276 | { |
Kent Overstreet | 7988613 | 2013-11-23 17:19:00 -0800 | [diff] [blame] | 277 | struct bio_vec bvec; |
Ming Lei | 3011201 | 2014-12-31 13:22:58 +0000 | [diff] [blame] | 278 | struct req_iterator iter; |
Christoph Hellwig | aa4d861 | 2015-04-07 18:23:29 +0200 | [diff] [blame] | 279 | struct iov_iter i; |
| 280 | ssize_t len; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 281 | |
Ming Lei | 3011201 | 2014-12-31 13:22:58 +0000 | [diff] [blame] | 282 | rq_for_each_segment(bvec, rq, iter) { |
Christoph Hellwig | b620743 | 2019-06-26 15:49:28 +0200 | [diff] [blame] | 283 | iov_iter_bvec(&i, READ, &bvec, 1, bvec.bv_len); |
Christoph Hellwig | 18e9710 | 2017-05-27 11:16:51 +0300 | [diff] [blame] | 284 | len = vfs_iter_read(lo->lo_backing_file, &i, &pos, 0); |
Christoph Hellwig | aa4d861 | 2015-04-07 18:23:29 +0200 | [diff] [blame] | 285 | if (len < 0) |
| 286 | return len; |
Dave Young | 306df07 | 2012-02-08 22:07:19 +0100 | [diff] [blame] | 287 | |
Christoph Hellwig | aa4d861 | 2015-04-07 18:23:29 +0200 | [diff] [blame] | 288 | flush_dcache_page(bvec.bv_page); |
| 289 | |
| 290 | if (len != bvec.bv_len) { |
Ming Lei | 3011201 | 2014-12-31 13:22:58 +0000 | [diff] [blame] | 291 | struct bio *bio; |
| 292 | |
| 293 | __rq_for_each_bio(bio, rq) |
| 294 | zero_fill_bio(bio); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 295 | break; |
Dave Young | 306df07 | 2012-02-08 22:07:19 +0100 | [diff] [blame] | 296 | } |
Christoph Hellwig | aa4d861 | 2015-04-07 18:23:29 +0200 | [diff] [blame] | 297 | cond_resched(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 298 | } |
Christoph Hellwig | aa4d861 | 2015-04-07 18:23:29 +0200 | [diff] [blame] | 299 | |
Dave Young | 306df07 | 2012-02-08 22:07:19 +0100 | [diff] [blame] | 300 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 301 | } |
| 302 | |
Darrick J. Wong | efcfec57 | 2019-10-30 20:29:48 -0700 | [diff] [blame] | 303 | static int lo_fallocate(struct loop_device *lo, struct request *rq, loff_t pos, |
| 304 | int mode) |
Ming Lei | cf655d9 | 2014-12-31 13:22:59 +0000 | [diff] [blame] | 305 | { |
| 306 | /* |
Darrick J. Wong | efcfec57 | 2019-10-30 20:29:48 -0700 | [diff] [blame] | 307 | * We use fallocate to manipulate the space mappings used by the image |
Christoph Hellwig | 47e9624 | 2021-10-19 09:56:39 +0200 | [diff] [blame] | 308 | * a.k.a. discard/zerorange. |
Ming Lei | cf655d9 | 2014-12-31 13:22:59 +0000 | [diff] [blame] | 309 | */ |
| 310 | struct file *file = lo->lo_backing_file; |
Evan Green | c52abf5 | 2020-04-03 16:43:04 +0200 | [diff] [blame] | 311 | struct request_queue *q = lo->lo_queue; |
Ming Lei | cf655d9 | 2014-12-31 13:22:59 +0000 | [diff] [blame] | 312 | int ret; |
| 313 | |
Darrick J. Wong | efcfec57 | 2019-10-30 20:29:48 -0700 | [diff] [blame] | 314 | mode |= FALLOC_FL_KEEP_SIZE; |
| 315 | |
Evan Green | c52abf5 | 2020-04-03 16:43:04 +0200 | [diff] [blame] | 316 | if (!blk_queue_discard(q)) { |
Ming Lei | cf655d9 | 2014-12-31 13:22:59 +0000 | [diff] [blame] | 317 | ret = -EOPNOTSUPP; |
| 318 | goto out; |
| 319 | } |
| 320 | |
| 321 | ret = file->f_op->fallocate(file, mode, pos, blk_rq_bytes(rq)); |
| 322 | if (unlikely(ret && ret != -EINVAL && ret != -EOPNOTSUPP)) |
| 323 | ret = -EIO; |
| 324 | out: |
| 325 | return ret; |
| 326 | } |
| 327 | |
| 328 | static int lo_req_flush(struct loop_device *lo, struct request *rq) |
| 329 | { |
| 330 | struct file *file = lo->lo_backing_file; |
| 331 | int ret = vfs_fsync(file, 0); |
| 332 | if (unlikely(ret && ret != -EINVAL)) |
| 333 | ret = -EIO; |
| 334 | |
| 335 | return ret; |
| 336 | } |
| 337 | |
Christoph Hellwig | fe2cb29 | 2017-04-20 16:03:02 +0200 | [diff] [blame] | 338 | static void lo_complete_rq(struct request *rq) |
Ming Lei | bc07c10 | 2015-08-17 10:31:51 +0800 | [diff] [blame] | 339 | { |
Christoph Hellwig | fe2cb29 | 2017-04-20 16:03:02 +0200 | [diff] [blame] | 340 | struct loop_cmd *cmd = blk_mq_rq_to_pdu(rq); |
Jens Axboe | f9de14b | 2018-04-13 16:25:57 -0600 | [diff] [blame] | 341 | blk_status_t ret = BLK_STS_OK; |
Ming Lei | bc07c10 | 2015-08-17 10:31:51 +0800 | [diff] [blame] | 342 | |
Jens Axboe | f9de14b | 2018-04-13 16:25:57 -0600 | [diff] [blame] | 343 | if (!cmd->use_aio || cmd->ret < 0 || cmd->ret == blk_rq_bytes(rq) || |
| 344 | req_op(rq) != REQ_OP_READ) { |
| 345 | if (cmd->ret < 0) |
Evan Green | 8cd5508 | 2020-04-03 16:43:03 +0200 | [diff] [blame] | 346 | ret = errno_to_blk_status(cmd->ret); |
Jens Axboe | f9de14b | 2018-04-13 16:25:57 -0600 | [diff] [blame] | 347 | goto end_io; |
Ming Lei | bc07c10 | 2015-08-17 10:31:51 +0800 | [diff] [blame] | 348 | } |
Christoph Hellwig | fe2cb29 | 2017-04-20 16:03:02 +0200 | [diff] [blame] | 349 | |
Jens Axboe | f9de14b | 2018-04-13 16:25:57 -0600 | [diff] [blame] | 350 | /* |
| 351 | * Short READ - if we got some data, advance our request and |
| 352 | * retry it. If we got no data, end the rest with EIO. |
| 353 | */ |
| 354 | if (cmd->ret) { |
| 355 | blk_update_request(rq, BLK_STS_OK, cmd->ret); |
| 356 | cmd->ret = 0; |
| 357 | blk_mq_requeue_request(rq, true); |
| 358 | } else { |
| 359 | if (cmd->use_aio) { |
| 360 | struct bio *bio = rq->bio; |
| 361 | |
| 362 | while (bio) { |
| 363 | zero_fill_bio(bio); |
| 364 | bio = bio->bi_next; |
| 365 | } |
| 366 | } |
| 367 | ret = BLK_STS_IOERR; |
| 368 | end_io: |
| 369 | blk_mq_end_request(rq, ret); |
| 370 | } |
Ming Lei | bc07c10 | 2015-08-17 10:31:51 +0800 | [diff] [blame] | 371 | } |
| 372 | |
Shaohua Li | 92d7733 | 2017-09-01 11:15:17 -0700 | [diff] [blame] | 373 | static void lo_rw_aio_do_completion(struct loop_cmd *cmd) |
| 374 | { |
Jens Axboe | 1894e91 | 2018-04-13 16:24:29 -0600 | [diff] [blame] | 375 | struct request *rq = blk_mq_rq_from_pdu(cmd); |
| 376 | |
Shaohua Li | 92d7733 | 2017-09-01 11:15:17 -0700 | [diff] [blame] | 377 | if (!atomic_dec_and_test(&cmd->ref)) |
| 378 | return; |
| 379 | kfree(cmd->bvec); |
| 380 | cmd->bvec = NULL; |
Christoph Hellwig | 15f73f5 | 2020-06-11 08:44:47 +0200 | [diff] [blame] | 381 | if (likely(!blk_should_fake_timeout(rq->q))) |
| 382 | blk_mq_complete_request(rq); |
Shaohua Li | 92d7733 | 2017-09-01 11:15:17 -0700 | [diff] [blame] | 383 | } |
| 384 | |
Jens Axboe | 6b19b76 | 2021-10-21 09:22:35 -0600 | [diff] [blame] | 385 | static void lo_rw_aio_complete(struct kiocb *iocb, long ret) |
Ming Lei | bc07c10 | 2015-08-17 10:31:51 +0800 | [diff] [blame] | 386 | { |
| 387 | struct loop_cmd *cmd = container_of(iocb, struct loop_cmd, iocb); |
Ming Lei | bc07c10 | 2015-08-17 10:31:51 +0800 | [diff] [blame] | 388 | |
Christoph Hellwig | fe2cb29 | 2017-04-20 16:03:02 +0200 | [diff] [blame] | 389 | cmd->ret = ret; |
Shaohua Li | 92d7733 | 2017-09-01 11:15:17 -0700 | [diff] [blame] | 390 | lo_rw_aio_do_completion(cmd); |
Ming Lei | bc07c10 | 2015-08-17 10:31:51 +0800 | [diff] [blame] | 391 | } |
| 392 | |
| 393 | static int lo_rw_aio(struct loop_device *lo, struct loop_cmd *cmd, |
| 394 | loff_t pos, bool rw) |
| 395 | { |
| 396 | struct iov_iter iter; |
Ming Lei | 86af595 | 2019-02-15 19:13:17 +0800 | [diff] [blame] | 397 | struct req_iterator rq_iter; |
Ming Lei | bc07c10 | 2015-08-17 10:31:51 +0800 | [diff] [blame] | 398 | struct bio_vec *bvec; |
Jens Axboe | 1894e91 | 2018-04-13 16:24:29 -0600 | [diff] [blame] | 399 | struct request *rq = blk_mq_rq_from_pdu(cmd); |
Shaohua Li | 40326d8 | 2017-08-31 22:09:46 -0700 | [diff] [blame] | 400 | struct bio *bio = rq->bio; |
Ming Lei | bc07c10 | 2015-08-17 10:31:51 +0800 | [diff] [blame] | 401 | struct file *file = lo->lo_backing_file; |
Ming Lei | 86af595 | 2019-02-15 19:13:17 +0800 | [diff] [blame] | 402 | struct bio_vec tmp; |
Shaohua Li | 40326d8 | 2017-08-31 22:09:46 -0700 | [diff] [blame] | 403 | unsigned int offset; |
Ming Lei | 86af595 | 2019-02-15 19:13:17 +0800 | [diff] [blame] | 404 | int nr_bvec = 0; |
Ming Lei | bc07c10 | 2015-08-17 10:31:51 +0800 | [diff] [blame] | 405 | int ret; |
| 406 | |
Ming Lei | 86af595 | 2019-02-15 19:13:17 +0800 | [diff] [blame] | 407 | rq_for_each_bvec(tmp, rq, rq_iter) |
| 408 | nr_bvec++; |
Ming Lei | bc07c10 | 2015-08-17 10:31:51 +0800 | [diff] [blame] | 409 | |
Ming Lei | 86af595 | 2019-02-15 19:13:17 +0800 | [diff] [blame] | 410 | if (rq->bio != rq->biotail) { |
| 411 | |
| 412 | bvec = kmalloc_array(nr_bvec, sizeof(struct bio_vec), |
Kees Cook | 6da2ec5 | 2018-06-12 13:55:00 -0700 | [diff] [blame] | 413 | GFP_NOIO); |
Shaohua Li | 40326d8 | 2017-08-31 22:09:46 -0700 | [diff] [blame] | 414 | if (!bvec) |
| 415 | return -EIO; |
| 416 | cmd->bvec = bvec; |
| 417 | |
| 418 | /* |
| 419 | * The bios of the request may be started from the middle of |
| 420 | * the 'bvec' because of bio splitting, so we can't directly |
Ming Lei | 86af595 | 2019-02-15 19:13:17 +0800 | [diff] [blame] | 421 | * copy bio->bi_iov_vec to new bvec. The rq_for_each_bvec |
Shaohua Li | 40326d8 | 2017-08-31 22:09:46 -0700 | [diff] [blame] | 422 | * API will take care of all details for us. |
| 423 | */ |
Ming Lei | 86af595 | 2019-02-15 19:13:17 +0800 | [diff] [blame] | 424 | rq_for_each_bvec(tmp, rq, rq_iter) { |
Shaohua Li | 40326d8 | 2017-08-31 22:09:46 -0700 | [diff] [blame] | 425 | *bvec = tmp; |
| 426 | bvec++; |
| 427 | } |
| 428 | bvec = cmd->bvec; |
| 429 | offset = 0; |
| 430 | } else { |
| 431 | /* |
| 432 | * Same here, this bio may be started from the middle of the |
| 433 | * 'bvec' because of bio splitting, so offset from the bvec |
| 434 | * must be passed to iov iterator |
| 435 | */ |
| 436 | offset = bio->bi_iter.bi_bvec_done; |
| 437 | bvec = __bvec_iter_bvec(bio->bi_io_vec, bio->bi_iter); |
Shaohua Li | 40326d8 | 2017-08-31 22:09:46 -0700 | [diff] [blame] | 438 | } |
Shaohua Li | 92d7733 | 2017-09-01 11:15:17 -0700 | [diff] [blame] | 439 | atomic_set(&cmd->ref, 2); |
Shaohua Li | 40326d8 | 2017-08-31 22:09:46 -0700 | [diff] [blame] | 440 | |
Christoph Hellwig | b620743 | 2019-06-26 15:49:28 +0200 | [diff] [blame] | 441 | iov_iter_bvec(&iter, rw, bvec, nr_bvec, blk_rq_bytes(rq)); |
Shaohua Li | 40326d8 | 2017-08-31 22:09:46 -0700 | [diff] [blame] | 442 | iter.iov_offset = offset; |
Ming Lei | bc07c10 | 2015-08-17 10:31:51 +0800 | [diff] [blame] | 443 | |
| 444 | cmd->iocb.ki_pos = pos; |
| 445 | cmd->iocb.ki_filp = file; |
| 446 | cmd->iocb.ki_complete = lo_rw_aio_complete; |
| 447 | cmd->iocb.ki_flags = IOCB_DIRECT; |
Adam Manzanares | d9a08a9 | 2018-05-22 10:52:19 -0700 | [diff] [blame] | 448 | cmd->iocb.ki_ioprio = IOPRIO_PRIO_VALUE(IOPRIO_CLASS_NONE, 0); |
Ming Lei | bc07c10 | 2015-08-17 10:31:51 +0800 | [diff] [blame] | 449 | |
| 450 | if (rw == WRITE) |
Miklos Szeredi | bb7462b | 2017-02-20 16:51:23 +0100 | [diff] [blame] | 451 | ret = call_write_iter(file, &cmd->iocb, &iter); |
Ming Lei | bc07c10 | 2015-08-17 10:31:51 +0800 | [diff] [blame] | 452 | else |
Miklos Szeredi | bb7462b | 2017-02-20 16:51:23 +0100 | [diff] [blame] | 453 | ret = call_read_iter(file, &cmd->iocb, &iter); |
Ming Lei | bc07c10 | 2015-08-17 10:31:51 +0800 | [diff] [blame] | 454 | |
Shaohua Li | 92d7733 | 2017-09-01 11:15:17 -0700 | [diff] [blame] | 455 | lo_rw_aio_do_completion(cmd); |
| 456 | |
Ming Lei | bc07c10 | 2015-08-17 10:31:51 +0800 | [diff] [blame] | 457 | if (ret != -EIOCBQUEUED) |
Jens Axboe | 6b19b76 | 2021-10-21 09:22:35 -0600 | [diff] [blame] | 458 | lo_rw_aio_complete(&cmd->iocb, ret); |
Ming Lei | bc07c10 | 2015-08-17 10:31:51 +0800 | [diff] [blame] | 459 | return 0; |
| 460 | } |
| 461 | |
Christoph Hellwig | c1c87c2 | 2016-08-04 16:10:01 +0200 | [diff] [blame] | 462 | static int do_req_filebacked(struct loop_device *lo, struct request *rq) |
Ming Lei | bc07c10 | 2015-08-17 10:31:51 +0800 | [diff] [blame] | 463 | { |
| 464 | struct loop_cmd *cmd = blk_mq_rq_to_pdu(rq); |
Christoph Hellwig | c1c87c2 | 2016-08-04 16:10:01 +0200 | [diff] [blame] | 465 | loff_t pos = ((loff_t) blk_rq_pos(rq) << 9) + lo->lo_offset; |
Ming Lei | bc07c10 | 2015-08-17 10:31:51 +0800 | [diff] [blame] | 466 | |
| 467 | /* |
| 468 | * lo_write_simple and lo_read_simple should have been covered |
| 469 | * by io submit style function like lo_rw_aio(), one blocker |
| 470 | * is that lo_read_simple() need to call flush_dcache_page after |
| 471 | * the page is written from kernel, and it isn't easy to handle |
| 472 | * this in io submit style function which submits all segments |
| 473 | * of the req at one time. And direct read IO doesn't need to |
| 474 | * run flush_dcache_page(). |
| 475 | */ |
Christoph Hellwig | c1c87c2 | 2016-08-04 16:10:01 +0200 | [diff] [blame] | 476 | switch (req_op(rq)) { |
| 477 | case REQ_OP_FLUSH: |
| 478 | return lo_req_flush(lo, rq); |
Christoph Hellwig | 19372e2 | 2017-04-05 19:21:15 +0200 | [diff] [blame] | 479 | case REQ_OP_WRITE_ZEROES: |
Darrick J. Wong | efcfec57 | 2019-10-30 20:29:48 -0700 | [diff] [blame] | 480 | /* |
| 481 | * If the caller doesn't want deallocation, call zeroout to |
| 482 | * write zeroes the range. Otherwise, punch them out. |
| 483 | */ |
| 484 | return lo_fallocate(lo, rq, pos, |
| 485 | (rq->cmd_flags & REQ_NOUNMAP) ? |
| 486 | FALLOC_FL_ZERO_RANGE : |
| 487 | FALLOC_FL_PUNCH_HOLE); |
| 488 | case REQ_OP_DISCARD: |
| 489 | return lo_fallocate(lo, rq, pos, FALLOC_FL_PUNCH_HOLE); |
Christoph Hellwig | c1c87c2 | 2016-08-04 16:10:01 +0200 | [diff] [blame] | 490 | case REQ_OP_WRITE: |
Christoph Hellwig | 47e9624 | 2021-10-19 09:56:39 +0200 | [diff] [blame] | 491 | if (cmd->use_aio) |
Christoph Hellwig | c1c87c2 | 2016-08-04 16:10:01 +0200 | [diff] [blame] | 492 | return lo_rw_aio(lo, cmd, pos, WRITE); |
Christoph Hellwig | aa4d861 | 2015-04-07 18:23:29 +0200 | [diff] [blame] | 493 | else |
Christoph Hellwig | c1c87c2 | 2016-08-04 16:10:01 +0200 | [diff] [blame] | 494 | return lo_write_simple(lo, rq, pos); |
| 495 | case REQ_OP_READ: |
Christoph Hellwig | 47e9624 | 2021-10-19 09:56:39 +0200 | [diff] [blame] | 496 | if (cmd->use_aio) |
Christoph Hellwig | c1c87c2 | 2016-08-04 16:10:01 +0200 | [diff] [blame] | 497 | return lo_rw_aio(lo, cmd, pos, READ); |
| 498 | else |
| 499 | return lo_read_simple(lo, rq, pos); |
| 500 | default: |
| 501 | WARN_ON_ONCE(1); |
| 502 | return -EIO; |
Christoph Hellwig | aa4d861 | 2015-04-07 18:23:29 +0200 | [diff] [blame] | 503 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 504 | } |
| 505 | |
Ming Lei | 2e5ab5f | 2015-08-17 10:31:49 +0800 | [diff] [blame] | 506 | static inline void loop_update_dio(struct loop_device *lo) |
| 507 | { |
Ira Weiny | efbe3c2 | 2020-04-30 07:41:33 -0700 | [diff] [blame] | 508 | __loop_update_dio(lo, (lo->lo_backing_file->f_flags & O_DIRECT) | |
| 509 | lo->use_dio); |
Ming Lei | 2e5ab5f | 2015-08-17 10:31:49 +0800 | [diff] [blame] | 510 | } |
| 511 | |
Christoph Hellwig | 0384264 | 2021-06-24 14:32:40 +0200 | [diff] [blame] | 512 | static void loop_reread_partitions(struct loop_device *lo) |
Ming Lei | 06f0e9e | 2015-05-06 12:26:24 +0800 | [diff] [blame] | 513 | { |
| 514 | int rc; |
| 515 | |
Christoph Hellwig | 0384264 | 2021-06-24 14:32:40 +0200 | [diff] [blame] | 516 | mutex_lock(&lo->lo_disk->open_mutex); |
| 517 | rc = bdev_disk_changed(lo->lo_disk, false); |
| 518 | mutex_unlock(&lo->lo_disk->open_mutex); |
Ming Lei | 06f0e9e | 2015-05-06 12:26:24 +0800 | [diff] [blame] | 519 | if (rc) |
| 520 | pr_warn("%s: partition scan of loop%d (%s) failed (rc=%d)\n", |
| 521 | __func__, lo->lo_number, lo->lo_file_name, rc); |
| 522 | } |
| 523 | |
Theodore Ts'o | d2ac838 | 2018-05-07 11:37:58 -0400 | [diff] [blame] | 524 | static inline int is_loop_device(struct file *file) |
| 525 | { |
| 526 | struct inode *i = file->f_mapping->host; |
| 527 | |
Al Viro | 6f24784 | 2021-01-31 19:23:55 -0500 | [diff] [blame] | 528 | return i && S_ISBLK(i->i_mode) && imajor(i) == LOOP_MAJOR; |
Theodore Ts'o | d2ac838 | 2018-05-07 11:37:58 -0400 | [diff] [blame] | 529 | } |
| 530 | |
| 531 | static int loop_validate_file(struct file *file, struct block_device *bdev) |
| 532 | { |
| 533 | struct inode *inode = file->f_mapping->host; |
| 534 | struct file *f = file; |
| 535 | |
| 536 | /* Avoid recursion */ |
| 537 | while (is_loop_device(f)) { |
| 538 | struct loop_device *l; |
| 539 | |
Tetsuo Handa | 3ce6e1f | 2021-07-06 23:40:34 +0900 | [diff] [blame] | 540 | lockdep_assert_held(&loop_validate_mutex); |
Christoph Hellwig | 4e7b567 | 2020-11-23 13:38:40 +0100 | [diff] [blame] | 541 | if (f->f_mapping->host->i_rdev == bdev->bd_dev) |
Theodore Ts'o | d2ac838 | 2018-05-07 11:37:58 -0400 | [diff] [blame] | 542 | return -EBADF; |
| 543 | |
Christoph Hellwig | 4e7b567 | 2020-11-23 13:38:40 +0100 | [diff] [blame] | 544 | l = I_BDEV(f->f_mapping->host)->bd_disk->private_data; |
Tetsuo Handa | 3ce6e1f | 2021-07-06 23:40:34 +0900 | [diff] [blame] | 545 | if (l->lo_state != Lo_bound) |
Theodore Ts'o | d2ac838 | 2018-05-07 11:37:58 -0400 | [diff] [blame] | 546 | return -EINVAL; |
Tetsuo Handa | 3ce6e1f | 2021-07-06 23:40:34 +0900 | [diff] [blame] | 547 | /* Order wrt setting lo->lo_backing_file in loop_configure(). */ |
| 548 | rmb(); |
Theodore Ts'o | d2ac838 | 2018-05-07 11:37:58 -0400 | [diff] [blame] | 549 | f = l->lo_backing_file; |
| 550 | } |
| 551 | if (!S_ISREG(inode->i_mode) && !S_ISBLK(inode->i_mode)) |
| 552 | return -EINVAL; |
| 553 | return 0; |
| 554 | } |
| 555 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 556 | /* |
| 557 | * loop_change_fd switched the backing store of a loopback device to |
| 558 | * a new file. This is useful for operating system installers to free up |
| 559 | * the original file and in High Availability environments to switch to |
| 560 | * an alternative location for the content in case of server meltdown. |
| 561 | * This can only work if the loop device is used read-only, and if the |
| 562 | * new backing store is the same size and type as the old backing store. |
| 563 | */ |
Al Viro | bb21488 | 2008-03-02 09:29:48 -0500 | [diff] [blame] | 564 | static int loop_change_fd(struct loop_device *lo, struct block_device *bdev, |
| 565 | unsigned int arg) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 566 | { |
Tetsuo Handa | 3ce6e1f | 2021-07-06 23:40:34 +0900 | [diff] [blame] | 567 | struct file *file = fget(arg); |
| 568 | struct file *old_file; |
| 569 | int error; |
| 570 | bool partscan; |
| 571 | bool is_loop; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 572 | |
Tetsuo Handa | 3ce6e1f | 2021-07-06 23:40:34 +0900 | [diff] [blame] | 573 | if (!file) |
| 574 | return -EBADF; |
| 575 | is_loop = is_loop_device(file); |
| 576 | error = loop_global_lock_killable(lo, is_loop); |
Jan Kara | c371077 | 2018-11-08 14:01:11 +0100 | [diff] [blame] | 577 | if (error) |
Tetsuo Handa | 3ce6e1f | 2021-07-06 23:40:34 +0900 | [diff] [blame] | 578 | goto out_putf; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 579 | error = -ENXIO; |
| 580 | if (lo->lo_state != Lo_bound) |
Jan Kara | 1dded9a | 2018-11-08 14:01:15 +0100 | [diff] [blame] | 581 | goto out_err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 582 | |
| 583 | /* the loop device has to be read-only */ |
| 584 | error = -EINVAL; |
| 585 | if (!(lo->lo_flags & LO_FLAGS_READ_ONLY)) |
Jan Kara | 1dded9a | 2018-11-08 14:01:15 +0100 | [diff] [blame] | 586 | goto out_err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 587 | |
Theodore Ts'o | d2ac838 | 2018-05-07 11:37:58 -0400 | [diff] [blame] | 588 | error = loop_validate_file(file, bdev); |
| 589 | if (error) |
Jan Kara | 1dded9a | 2018-11-08 14:01:15 +0100 | [diff] [blame] | 590 | goto out_err; |
Theodore Ts'o | d2ac838 | 2018-05-07 11:37:58 -0400 | [diff] [blame] | 591 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 592 | old_file = lo->lo_backing_file; |
| 593 | |
| 594 | error = -EINVAL; |
| 595 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 596 | /* size of the new backing store needs to be the same */ |
| 597 | if (get_loop_size(lo, file) != get_loop_size(lo, old_file)) |
Jan Kara | 1dded9a | 2018-11-08 14:01:15 +0100 | [diff] [blame] | 598 | goto out_err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 599 | |
| 600 | /* and ... switch */ |
Matteo Croce | 9f65c48 | 2021-07-13 01:05:30 +0200 | [diff] [blame] | 601 | disk_force_media_change(lo->lo_disk, DISK_EVENT_MEDIA_CHANGE); |
Omar Sandoval | 43cade80 | 2017-08-24 00:03:44 -0700 | [diff] [blame] | 602 | blk_mq_freeze_queue(lo->lo_queue); |
| 603 | mapping_set_gfp_mask(old_file->f_mapping, lo->old_gfp_mask); |
| 604 | lo->lo_backing_file = file; |
| 605 | lo->old_gfp_mask = mapping_gfp_mask(file->f_mapping); |
| 606 | mapping_set_gfp_mask(file->f_mapping, |
| 607 | lo->old_gfp_mask & ~(__GFP_IO|__GFP_FS)); |
| 608 | loop_update_dio(lo); |
| 609 | blk_mq_unfreeze_queue(lo->lo_queue); |
Jan Kara | 85b0a54 | 2018-11-08 14:01:13 +0100 | [diff] [blame] | 610 | partscan = lo->lo_flags & LO_FLAGS_PARTSCAN; |
Tetsuo Handa | 3ce6e1f | 2021-07-06 23:40:34 +0900 | [diff] [blame] | 611 | loop_global_unlock(lo, is_loop); |
| 612 | |
| 613 | /* |
| 614 | * Flush loop_validate_file() before fput(), for l->lo_backing_file |
| 615 | * might be pointing at old_file which might be the last reference. |
| 616 | */ |
| 617 | if (!is_loop) { |
| 618 | mutex_lock(&loop_validate_mutex); |
| 619 | mutex_unlock(&loop_validate_mutex); |
| 620 | } |
Jan Kara | 1dded9a | 2018-11-08 14:01:15 +0100 | [diff] [blame] | 621 | /* |
Pavel Tatashin | 6cc8e74 | 2021-01-26 09:46:30 -0500 | [diff] [blame] | 622 | * We must drop file reference outside of lo_mutex as dropping |
Christoph Hellwig | a869870 | 2021-05-25 08:12:56 +0200 | [diff] [blame] | 623 | * the file ref can take open_mutex which creates circular locking |
Jan Kara | 1dded9a | 2018-11-08 14:01:15 +0100 | [diff] [blame] | 624 | * dependency. |
| 625 | */ |
| 626 | fput(old_file); |
Jan Kara | 85b0a54 | 2018-11-08 14:01:13 +0100 | [diff] [blame] | 627 | if (partscan) |
Christoph Hellwig | 0384264 | 2021-06-24 14:32:40 +0200 | [diff] [blame] | 628 | loop_reread_partitions(lo); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 629 | return 0; |
| 630 | |
Jan Kara | 1dded9a | 2018-11-08 14:01:15 +0100 | [diff] [blame] | 631 | out_err: |
Tetsuo Handa | 3ce6e1f | 2021-07-06 23:40:34 +0900 | [diff] [blame] | 632 | loop_global_unlock(lo, is_loop); |
| 633 | out_putf: |
| 634 | fput(file); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 635 | return error; |
| 636 | } |
| 637 | |
Milan Broz | ee86273 | 2010-08-23 15:16:00 +0200 | [diff] [blame] | 638 | /* loop sysfs attributes */ |
| 639 | |
| 640 | static ssize_t loop_attr_show(struct device *dev, char *page, |
| 641 | ssize_t (*callback)(struct loop_device *, char *)) |
| 642 | { |
Kay Sievers | 34dd82a | 2011-07-31 22:08:04 +0200 | [diff] [blame] | 643 | struct gendisk *disk = dev_to_disk(dev); |
| 644 | struct loop_device *lo = disk->private_data; |
Milan Broz | ee86273 | 2010-08-23 15:16:00 +0200 | [diff] [blame] | 645 | |
Kay Sievers | 34dd82a | 2011-07-31 22:08:04 +0200 | [diff] [blame] | 646 | return callback(lo, page); |
Milan Broz | ee86273 | 2010-08-23 15:16:00 +0200 | [diff] [blame] | 647 | } |
| 648 | |
| 649 | #define LOOP_ATTR_RO(_name) \ |
| 650 | static ssize_t loop_attr_##_name##_show(struct loop_device *, char *); \ |
| 651 | static ssize_t loop_attr_do_show_##_name(struct device *d, \ |
| 652 | struct device_attribute *attr, char *b) \ |
| 653 | { \ |
| 654 | return loop_attr_show(d, b, loop_attr_##_name##_show); \ |
| 655 | } \ |
| 656 | static struct device_attribute loop_attr_##_name = \ |
Joe Perches | 5657a81 | 2018-05-24 13:38:59 -0600 | [diff] [blame] | 657 | __ATTR(_name, 0444, loop_attr_do_show_##_name, NULL); |
Milan Broz | ee86273 | 2010-08-23 15:16:00 +0200 | [diff] [blame] | 658 | |
| 659 | static ssize_t loop_attr_backing_file_show(struct loop_device *lo, char *buf) |
| 660 | { |
| 661 | ssize_t ret; |
| 662 | char *p = NULL; |
| 663 | |
Kay Sievers | 05eb0f2 | 2011-07-31 22:21:35 +0200 | [diff] [blame] | 664 | spin_lock_irq(&lo->lo_lock); |
Milan Broz | ee86273 | 2010-08-23 15:16:00 +0200 | [diff] [blame] | 665 | if (lo->lo_backing_file) |
Miklos Szeredi | 9bf39ab | 2015-06-19 10:29:13 +0200 | [diff] [blame] | 666 | p = file_path(lo->lo_backing_file, buf, PAGE_SIZE - 1); |
Kay Sievers | 05eb0f2 | 2011-07-31 22:21:35 +0200 | [diff] [blame] | 667 | spin_unlock_irq(&lo->lo_lock); |
Milan Broz | ee86273 | 2010-08-23 15:16:00 +0200 | [diff] [blame] | 668 | |
| 669 | if (IS_ERR_OR_NULL(p)) |
| 670 | ret = PTR_ERR(p); |
| 671 | else { |
| 672 | ret = strlen(p); |
| 673 | memmove(buf, p, ret); |
| 674 | buf[ret++] = '\n'; |
| 675 | buf[ret] = 0; |
| 676 | } |
| 677 | |
| 678 | return ret; |
| 679 | } |
| 680 | |
| 681 | static ssize_t loop_attr_offset_show(struct loop_device *lo, char *buf) |
| 682 | { |
| 683 | return sprintf(buf, "%llu\n", (unsigned long long)lo->lo_offset); |
| 684 | } |
| 685 | |
| 686 | static ssize_t loop_attr_sizelimit_show(struct loop_device *lo, char *buf) |
| 687 | { |
| 688 | return sprintf(buf, "%llu\n", (unsigned long long)lo->lo_sizelimit); |
| 689 | } |
| 690 | |
| 691 | static ssize_t loop_attr_autoclear_show(struct loop_device *lo, char *buf) |
| 692 | { |
| 693 | int autoclear = (lo->lo_flags & LO_FLAGS_AUTOCLEAR); |
| 694 | |
| 695 | return sprintf(buf, "%s\n", autoclear ? "1" : "0"); |
| 696 | } |
| 697 | |
Kay Sievers | e03c8dd | 2011-08-23 20:12:04 +0200 | [diff] [blame] | 698 | static ssize_t loop_attr_partscan_show(struct loop_device *lo, char *buf) |
| 699 | { |
| 700 | int partscan = (lo->lo_flags & LO_FLAGS_PARTSCAN); |
| 701 | |
| 702 | return sprintf(buf, "%s\n", partscan ? "1" : "0"); |
| 703 | } |
| 704 | |
Ming Lei | 2e5ab5f | 2015-08-17 10:31:49 +0800 | [diff] [blame] | 705 | static ssize_t loop_attr_dio_show(struct loop_device *lo, char *buf) |
| 706 | { |
| 707 | int dio = (lo->lo_flags & LO_FLAGS_DIRECT_IO); |
| 708 | |
| 709 | return sprintf(buf, "%s\n", dio ? "1" : "0"); |
| 710 | } |
| 711 | |
Milan Broz | ee86273 | 2010-08-23 15:16:00 +0200 | [diff] [blame] | 712 | LOOP_ATTR_RO(backing_file); |
| 713 | LOOP_ATTR_RO(offset); |
| 714 | LOOP_ATTR_RO(sizelimit); |
| 715 | LOOP_ATTR_RO(autoclear); |
Kay Sievers | e03c8dd | 2011-08-23 20:12:04 +0200 | [diff] [blame] | 716 | LOOP_ATTR_RO(partscan); |
Ming Lei | 2e5ab5f | 2015-08-17 10:31:49 +0800 | [diff] [blame] | 717 | LOOP_ATTR_RO(dio); |
Milan Broz | ee86273 | 2010-08-23 15:16:00 +0200 | [diff] [blame] | 718 | |
| 719 | static struct attribute *loop_attrs[] = { |
| 720 | &loop_attr_backing_file.attr, |
| 721 | &loop_attr_offset.attr, |
| 722 | &loop_attr_sizelimit.attr, |
| 723 | &loop_attr_autoclear.attr, |
Kay Sievers | e03c8dd | 2011-08-23 20:12:04 +0200 | [diff] [blame] | 724 | &loop_attr_partscan.attr, |
Ming Lei | 2e5ab5f | 2015-08-17 10:31:49 +0800 | [diff] [blame] | 725 | &loop_attr_dio.attr, |
Milan Broz | ee86273 | 2010-08-23 15:16:00 +0200 | [diff] [blame] | 726 | NULL, |
| 727 | }; |
| 728 | |
| 729 | static struct attribute_group loop_attribute_group = { |
| 730 | .name = "loop", |
| 731 | .attrs= loop_attrs, |
| 732 | }; |
| 733 | |
Tetsuo Handa | d3349b6 | 2018-05-04 10:58:09 -0600 | [diff] [blame] | 734 | static void loop_sysfs_init(struct loop_device *lo) |
Milan Broz | ee86273 | 2010-08-23 15:16:00 +0200 | [diff] [blame] | 735 | { |
Tetsuo Handa | d3349b6 | 2018-05-04 10:58:09 -0600 | [diff] [blame] | 736 | lo->sysfs_inited = !sysfs_create_group(&disk_to_dev(lo->lo_disk)->kobj, |
| 737 | &loop_attribute_group); |
Milan Broz | ee86273 | 2010-08-23 15:16:00 +0200 | [diff] [blame] | 738 | } |
| 739 | |
| 740 | static void loop_sysfs_exit(struct loop_device *lo) |
| 741 | { |
Tetsuo Handa | d3349b6 | 2018-05-04 10:58:09 -0600 | [diff] [blame] | 742 | if (lo->sysfs_inited) |
| 743 | sysfs_remove_group(&disk_to_dev(lo->lo_disk)->kobj, |
| 744 | &loop_attribute_group); |
Milan Broz | ee86273 | 2010-08-23 15:16:00 +0200 | [diff] [blame] | 745 | } |
| 746 | |
Lukas Czerner | dfaa2ef | 2011-08-19 14:50:46 +0200 | [diff] [blame] | 747 | static void loop_config_discard(struct loop_device *lo) |
| 748 | { |
| 749 | struct file *file = lo->lo_backing_file; |
| 750 | struct inode *inode = file->f_mapping->host; |
| 751 | struct request_queue *q = lo->lo_queue; |
Ming Lei | bcb21c8 | 2020-08-17 18:01:30 +0800 | [diff] [blame] | 752 | u32 granularity, max_discard_sectors; |
Lukas Czerner | dfaa2ef | 2011-08-19 14:50:46 +0200 | [diff] [blame] | 753 | |
| 754 | /* |
Evan Green | c52abf5 | 2020-04-03 16:43:04 +0200 | [diff] [blame] | 755 | * If the backing device is a block device, mirror its zeroing |
| 756 | * capability. Set the discard sectors to the block device's zeroing |
| 757 | * capabilities because loop discards result in blkdev_issue_zeroout(), |
| 758 | * not blkdev_issue_discard(). This maintains consistent behavior with |
| 759 | * file-backed loop devices: discarded regions read back as zero. |
| 760 | */ |
Christoph Hellwig | 47e9624 | 2021-10-19 09:56:39 +0200 | [diff] [blame] | 761 | if (S_ISBLK(inode->i_mode)) { |
Christoph Hellwig | 4e7b567 | 2020-11-23 13:38:40 +0100 | [diff] [blame] | 762 | struct request_queue *backingq = bdev_get_queue(I_BDEV(inode)); |
Evan Green | c52abf5 | 2020-04-03 16:43:04 +0200 | [diff] [blame] | 763 | |
Ming Lei | bcb21c8 | 2020-08-17 18:01:30 +0800 | [diff] [blame] | 764 | max_discard_sectors = backingq->limits.max_write_zeroes_sectors; |
| 765 | granularity = backingq->limits.discard_granularity ?: |
| 766 | queue_physical_block_size(backingq); |
Evan Green | c52abf5 | 2020-04-03 16:43:04 +0200 | [diff] [blame] | 767 | |
| 768 | /* |
Lukas Czerner | dfaa2ef | 2011-08-19 14:50:46 +0200 | [diff] [blame] | 769 | * We use punch hole to reclaim the free space used by the |
Christoph Hellwig | 47e9624 | 2021-10-19 09:56:39 +0200 | [diff] [blame] | 770 | * image a.k.a. discard. |
Lukas Czerner | dfaa2ef | 2011-08-19 14:50:46 +0200 | [diff] [blame] | 771 | */ |
Christoph Hellwig | 47e9624 | 2021-10-19 09:56:39 +0200 | [diff] [blame] | 772 | } else if (!file->f_op->fallocate) { |
Ming Lei | bcb21c8 | 2020-08-17 18:01:30 +0800 | [diff] [blame] | 773 | max_discard_sectors = 0; |
| 774 | granularity = 0; |
Evan Green | c52abf5 | 2020-04-03 16:43:04 +0200 | [diff] [blame] | 775 | |
| 776 | } else { |
Ming Lei | bcb21c8 | 2020-08-17 18:01:30 +0800 | [diff] [blame] | 777 | max_discard_sectors = UINT_MAX >> 9; |
| 778 | granularity = inode->i_sb->s_blocksize; |
Lukas Czerner | dfaa2ef | 2011-08-19 14:50:46 +0200 | [diff] [blame] | 779 | } |
| 780 | |
Ming Lei | bcb21c8 | 2020-08-17 18:01:30 +0800 | [diff] [blame] | 781 | if (max_discard_sectors) { |
| 782 | q->limits.discard_granularity = granularity; |
| 783 | blk_queue_max_discard_sectors(q, max_discard_sectors); |
| 784 | blk_queue_max_write_zeroes_sectors(q, max_discard_sectors); |
Evan Green | c52abf5 | 2020-04-03 16:43:04 +0200 | [diff] [blame] | 785 | blk_queue_flag_set(QUEUE_FLAG_DISCARD, q); |
Ming Lei | bcb21c8 | 2020-08-17 18:01:30 +0800 | [diff] [blame] | 786 | } else { |
| 787 | q->limits.discard_granularity = 0; |
| 788 | blk_queue_max_discard_sectors(q, 0); |
| 789 | blk_queue_max_write_zeroes_sectors(q, 0); |
Evan Green | c52abf5 | 2020-04-03 16:43:04 +0200 | [diff] [blame] | 790 | blk_queue_flag_clear(QUEUE_FLAG_DISCARD, q); |
Ming Lei | bcb21c8 | 2020-08-17 18:01:30 +0800 | [diff] [blame] | 791 | } |
| 792 | q->limits.discard_alignment = 0; |
Lukas Czerner | dfaa2ef | 2011-08-19 14:50:46 +0200 | [diff] [blame] | 793 | } |
| 794 | |
Dan Schatzberg | 87579e9 | 2021-06-28 19:38:15 -0700 | [diff] [blame] | 795 | struct loop_worker { |
| 796 | struct rb_node rb_node; |
| 797 | struct work_struct work; |
| 798 | struct list_head cmd_list; |
| 799 | struct list_head idle_list; |
| 800 | struct loop_device *lo; |
Dan Schatzberg | c74d40e | 2021-06-28 19:38:21 -0700 | [diff] [blame] | 801 | struct cgroup_subsys_state *blkcg_css; |
Dan Schatzberg | 87579e9 | 2021-06-28 19:38:15 -0700 | [diff] [blame] | 802 | unsigned long last_ran_at; |
| 803 | }; |
Ming Lei | e03a3d7 | 2015-08-17 10:31:48 +0800 | [diff] [blame] | 804 | |
Dan Schatzberg | 87579e9 | 2021-06-28 19:38:15 -0700 | [diff] [blame] | 805 | static void loop_workfn(struct work_struct *work); |
| 806 | static void loop_rootcg_workfn(struct work_struct *work); |
| 807 | static void loop_free_idle_workers(struct timer_list *timer); |
NeilBrown | b2ee7d4 | 2017-06-16 15:02:09 +1000 | [diff] [blame] | 808 | |
Dan Schatzberg | 87579e9 | 2021-06-28 19:38:15 -0700 | [diff] [blame] | 809 | #ifdef CONFIG_BLK_CGROUP |
| 810 | static inline int queue_on_root_worker(struct cgroup_subsys_state *css) |
Ming Lei | e03a3d7 | 2015-08-17 10:31:48 +0800 | [diff] [blame] | 811 | { |
Dan Schatzberg | 87579e9 | 2021-06-28 19:38:15 -0700 | [diff] [blame] | 812 | return !css || css == blkcg_root_css; |
| 813 | } |
| 814 | #else |
| 815 | static inline int queue_on_root_worker(struct cgroup_subsys_state *css) |
| 816 | { |
| 817 | return !css; |
| 818 | } |
| 819 | #endif |
| 820 | |
| 821 | static void loop_queue_work(struct loop_device *lo, struct loop_cmd *cmd) |
| 822 | { |
Colin Ian King | 413ec80 | 2022-01-13 00:14:32 +0000 | [diff] [blame] | 823 | struct rb_node **node, *parent = NULL; |
Dan Schatzberg | 87579e9 | 2021-06-28 19:38:15 -0700 | [diff] [blame] | 824 | struct loop_worker *cur_worker, *worker = NULL; |
| 825 | struct work_struct *work; |
| 826 | struct list_head *cmd_list; |
| 827 | |
| 828 | spin_lock_irq(&lo->lo_work_lock); |
| 829 | |
Dan Schatzberg | c74d40e | 2021-06-28 19:38:21 -0700 | [diff] [blame] | 830 | if (queue_on_root_worker(cmd->blkcg_css)) |
Dan Schatzberg | 87579e9 | 2021-06-28 19:38:15 -0700 | [diff] [blame] | 831 | goto queue_work; |
| 832 | |
| 833 | node = &lo->worker_tree.rb_node; |
| 834 | |
| 835 | while (*node) { |
| 836 | parent = *node; |
| 837 | cur_worker = container_of(*node, struct loop_worker, rb_node); |
Dan Schatzberg | c74d40e | 2021-06-28 19:38:21 -0700 | [diff] [blame] | 838 | if (cur_worker->blkcg_css == cmd->blkcg_css) { |
Dan Schatzberg | 87579e9 | 2021-06-28 19:38:15 -0700 | [diff] [blame] | 839 | worker = cur_worker; |
| 840 | break; |
Dan Schatzberg | c74d40e | 2021-06-28 19:38:21 -0700 | [diff] [blame] | 841 | } else if ((long)cur_worker->blkcg_css < (long)cmd->blkcg_css) { |
Dan Schatzberg | 87579e9 | 2021-06-28 19:38:15 -0700 | [diff] [blame] | 842 | node = &(*node)->rb_left; |
| 843 | } else { |
| 844 | node = &(*node)->rb_right; |
| 845 | } |
| 846 | } |
| 847 | if (worker) |
| 848 | goto queue_work; |
| 849 | |
| 850 | worker = kzalloc(sizeof(struct loop_worker), GFP_NOWAIT | __GFP_NOWARN); |
| 851 | /* |
| 852 | * In the event we cannot allocate a worker, just queue on the |
Dan Schatzberg | c74d40e | 2021-06-28 19:38:21 -0700 | [diff] [blame] | 853 | * rootcg worker and issue the I/O as the rootcg |
Dan Schatzberg | 87579e9 | 2021-06-28 19:38:15 -0700 | [diff] [blame] | 854 | */ |
Dan Schatzberg | c74d40e | 2021-06-28 19:38:21 -0700 | [diff] [blame] | 855 | if (!worker) { |
| 856 | cmd->blkcg_css = NULL; |
| 857 | if (cmd->memcg_css) |
| 858 | css_put(cmd->memcg_css); |
| 859 | cmd->memcg_css = NULL; |
Dan Schatzberg | 87579e9 | 2021-06-28 19:38:15 -0700 | [diff] [blame] | 860 | goto queue_work; |
Dan Schatzberg | c74d40e | 2021-06-28 19:38:21 -0700 | [diff] [blame] | 861 | } |
Dan Schatzberg | 87579e9 | 2021-06-28 19:38:15 -0700 | [diff] [blame] | 862 | |
Dan Schatzberg | c74d40e | 2021-06-28 19:38:21 -0700 | [diff] [blame] | 863 | worker->blkcg_css = cmd->blkcg_css; |
| 864 | css_get(worker->blkcg_css); |
Dan Schatzberg | 87579e9 | 2021-06-28 19:38:15 -0700 | [diff] [blame] | 865 | INIT_WORK(&worker->work, loop_workfn); |
| 866 | INIT_LIST_HEAD(&worker->cmd_list); |
| 867 | INIT_LIST_HEAD(&worker->idle_list); |
| 868 | worker->lo = lo; |
| 869 | rb_link_node(&worker->rb_node, parent, node); |
| 870 | rb_insert_color(&worker->rb_node, &lo->worker_tree); |
| 871 | queue_work: |
| 872 | if (worker) { |
| 873 | /* |
| 874 | * We need to remove from the idle list here while |
| 875 | * holding the lock so that the idle timer doesn't |
| 876 | * free the worker |
| 877 | */ |
| 878 | if (!list_empty(&worker->idle_list)) |
| 879 | list_del_init(&worker->idle_list); |
| 880 | work = &worker->work; |
| 881 | cmd_list = &worker->cmd_list; |
| 882 | } else { |
| 883 | work = &lo->rootcg_work; |
| 884 | cmd_list = &lo->rootcg_cmd_list; |
| 885 | } |
| 886 | list_add_tail(&cmd->list_entry, cmd_list); |
| 887 | queue_work(lo->workqueue, work); |
| 888 | spin_unlock_irq(&lo->lo_work_lock); |
Ming Lei | e03a3d7 | 2015-08-17 10:31:48 +0800 | [diff] [blame] | 889 | } |
| 890 | |
Holger Hoffstätte | 56a85fd | 2019-02-12 15:54:24 -0700 | [diff] [blame] | 891 | static void loop_update_rotational(struct loop_device *lo) |
| 892 | { |
| 893 | struct file *file = lo->lo_backing_file; |
| 894 | struct inode *file_inode = file->f_mapping->host; |
| 895 | struct block_device *file_bdev = file_inode->i_sb->s_bdev; |
| 896 | struct request_queue *q = lo->lo_queue; |
| 897 | bool nonrot = true; |
| 898 | |
| 899 | /* not all filesystems (e.g. tmpfs) have a sb->s_bdev */ |
| 900 | if (file_bdev) |
| 901 | nonrot = blk_queue_nonrot(bdev_get_queue(file_bdev)); |
| 902 | |
| 903 | if (nonrot) |
| 904 | blk_queue_flag_set(QUEUE_FLAG_NONROT, q); |
| 905 | else |
| 906 | blk_queue_flag_clear(QUEUE_FLAG_NONROT, q); |
| 907 | } |
| 908 | |
Martijn Coenen | 62ab466 | 2020-05-13 15:38:42 +0200 | [diff] [blame] | 909 | /** |
| 910 | * loop_set_status_from_info - configure device from loop_info |
| 911 | * @lo: struct loop_device to configure |
| 912 | * @info: struct loop_info64 to configure the device with |
| 913 | * |
| 914 | * Configures the loop device parameters according to the passed |
| 915 | * in loop_info64 configuration. |
| 916 | */ |
| 917 | static int |
| 918 | loop_set_status_from_info(struct loop_device *lo, |
| 919 | const struct loop_info64 *info) |
| 920 | { |
Martijn Coenen | 62ab466 | 2020-05-13 15:38:42 +0200 | [diff] [blame] | 921 | if ((unsigned int) info->lo_encrypt_key_size > LO_KEY_SIZE) |
| 922 | return -EINVAL; |
| 923 | |
Christoph Hellwig | 47e9624 | 2021-10-19 09:56:39 +0200 | [diff] [blame] | 924 | switch (info->lo_encrypt_type) { |
| 925 | case LO_CRYPT_NONE: |
| 926 | break; |
| 927 | case LO_CRYPT_XOR: |
| 928 | pr_warn("support for the xor transformation has been removed.\n"); |
| 929 | return -EINVAL; |
| 930 | case LO_CRYPT_CRYPTOAPI: |
| 931 | pr_warn("support for cryptoloop has been removed. Use dm-crypt instead.\n"); |
| 932 | return -EINVAL; |
| 933 | default: |
| 934 | return -EINVAL; |
| 935 | } |
Martijn Coenen | 62ab466 | 2020-05-13 15:38:42 +0200 | [diff] [blame] | 936 | |
| 937 | lo->lo_offset = info->lo_offset; |
| 938 | lo->lo_sizelimit = info->lo_sizelimit; |
| 939 | memcpy(lo->lo_file_name, info->lo_file_name, LO_NAME_SIZE); |
Martijn Coenen | 62ab466 | 2020-05-13 15:38:42 +0200 | [diff] [blame] | 940 | lo->lo_file_name[LO_NAME_SIZE-1] = 0; |
Martijn Coenen | faf1d25 | 2020-05-13 15:38:44 +0200 | [diff] [blame] | 941 | lo->lo_flags = info->lo_flags; |
Martijn Coenen | 62ab466 | 2020-05-13 15:38:42 +0200 | [diff] [blame] | 942 | return 0; |
| 943 | } |
| 944 | |
Martijn Coenen | 3448914 | 2020-05-13 15:38:45 +0200 | [diff] [blame] | 945 | static int loop_configure(struct loop_device *lo, fmode_t mode, |
| 946 | struct block_device *bdev, |
| 947 | const struct loop_config *config) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 948 | { |
Tetsuo Handa | 3ce6e1f | 2021-07-06 23:40:34 +0900 | [diff] [blame] | 949 | struct file *file = fget(config->fd); |
| 950 | struct inode *inode; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 951 | struct address_space *mapping; |
Tetsuo Handa | 3ce6e1f | 2021-07-06 23:40:34 +0900 | [diff] [blame] | 952 | int error; |
| 953 | loff_t size; |
| 954 | bool partscan; |
| 955 | unsigned short bsize; |
| 956 | bool is_loop; |
| 957 | |
| 958 | if (!file) |
| 959 | return -EBADF; |
| 960 | is_loop = is_loop_device(file); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 961 | |
| 962 | /* This is safe, since we have a reference from open(). */ |
| 963 | __module_get(THIS_MODULE); |
| 964 | |
Jan Kara | 33ec3e5 | 2019-05-16 16:01:27 +0200 | [diff] [blame] | 965 | /* |
| 966 | * If we don't hold exclusive handle for the device, upgrade to it |
| 967 | * here to avoid changing device under exclusive owner. |
| 968 | */ |
| 969 | if (!(mode & FMODE_EXCL)) { |
Christoph Hellwig | 37c3fc9 | 2020-11-25 21:20:08 +0100 | [diff] [blame] | 970 | error = bd_prepare_to_claim(bdev, loop_configure); |
Christoph Hellwig | ecbe6bc | 2020-07-16 16:33:09 +0200 | [diff] [blame] | 971 | if (error) |
Jan Kara | 33ec3e5 | 2019-05-16 16:01:27 +0200 | [diff] [blame] | 972 | goto out_putf; |
| 973 | } |
| 974 | |
Tetsuo Handa | 3ce6e1f | 2021-07-06 23:40:34 +0900 | [diff] [blame] | 975 | error = loop_global_lock_killable(lo, is_loop); |
Jan Kara | 757ecf4 | 2018-11-08 14:01:10 +0100 | [diff] [blame] | 976 | if (error) |
Jan Kara | 33ec3e5 | 2019-05-16 16:01:27 +0200 | [diff] [blame] | 977 | goto out_bdev; |
Jan Kara | 757ecf4 | 2018-11-08 14:01:10 +0100 | [diff] [blame] | 978 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 979 | error = -EBUSY; |
| 980 | if (lo->lo_state != Lo_unbound) |
Jan Kara | 757ecf4 | 2018-11-08 14:01:10 +0100 | [diff] [blame] | 981 | goto out_unlock; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 982 | |
Theodore Ts'o | d2ac838 | 2018-05-07 11:37:58 -0400 | [diff] [blame] | 983 | error = loop_validate_file(file, bdev); |
| 984 | if (error) |
Jan Kara | 757ecf4 | 2018-11-08 14:01:10 +0100 | [diff] [blame] | 985 | goto out_unlock; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 986 | |
| 987 | mapping = file->f_mapping; |
| 988 | inode = mapping->host; |
| 989 | |
Martijn Coenen | 3448914 | 2020-05-13 15:38:45 +0200 | [diff] [blame] | 990 | if ((config->info.lo_flags & ~LOOP_CONFIGURE_SETTABLE_FLAGS) != 0) { |
| 991 | error = -EINVAL; |
| 992 | goto out_unlock; |
| 993 | } |
| 994 | |
| 995 | if (config->block_size) { |
Xie Yongji | af3c570 | 2021-10-26 22:40:14 +0800 | [diff] [blame] | 996 | error = blk_validate_block_size(config->block_size); |
Martijn Coenen | 3448914 | 2020-05-13 15:38:45 +0200 | [diff] [blame] | 997 | if (error) |
| 998 | goto out_unlock; |
| 999 | } |
| 1000 | |
| 1001 | error = loop_set_status_from_info(lo, &config->info); |
| 1002 | if (error) |
| 1003 | goto out_unlock; |
| 1004 | |
Christoph Hellwig | 456be14 | 2011-10-17 12:57:20 +0200 | [diff] [blame] | 1005 | if (!(file->f_mode & FMODE_WRITE) || !(mode & FMODE_WRITE) || |
Al Viro | 283e7e5 | 2015-04-03 15:21:59 -0400 | [diff] [blame] | 1006 | !file->f_op->write_iter) |
Martijn Coenen | 3448914 | 2020-05-13 15:38:45 +0200 | [diff] [blame] | 1007 | lo->lo_flags |= LO_FLAGS_READ_ONLY; |
Martijn Coenen | 083a6a5 | 2020-05-13 15:38:36 +0200 | [diff] [blame] | 1008 | |
Dan Schatzberg | 87579e9 | 2021-06-28 19:38:15 -0700 | [diff] [blame] | 1009 | lo->workqueue = alloc_workqueue("loop%d", |
| 1010 | WQ_UNBOUND | WQ_FREEZABLE, |
| 1011 | 0, |
| 1012 | lo->lo_number); |
| 1013 | if (!lo->workqueue) { |
| 1014 | error = -ENOMEM; |
Jan Kara | 757ecf4 | 2018-11-08 14:01:10 +0100 | [diff] [blame] | 1015 | goto out_unlock; |
Dan Schatzberg | 87579e9 | 2021-06-28 19:38:15 -0700 | [diff] [blame] | 1016 | } |
Christoph Hellwig | 456be14 | 2011-10-17 12:57:20 +0200 | [diff] [blame] | 1017 | |
Matteo Croce | 9f65c48 | 2021-07-13 01:05:30 +0200 | [diff] [blame] | 1018 | disk_force_media_change(lo->lo_disk, DISK_EVENT_MEDIA_CHANGE); |
Christoph Hellwig | 7a2f0ce | 2020-11-03 11:00:16 +0100 | [diff] [blame] | 1019 | set_disk_ro(lo->lo_disk, (lo->lo_flags & LO_FLAGS_READ_ONLY) != 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1020 | |
Dan Schatzberg | 87579e9 | 2021-06-28 19:38:15 -0700 | [diff] [blame] | 1021 | INIT_WORK(&lo->rootcg_work, loop_rootcg_workfn); |
| 1022 | INIT_LIST_HEAD(&lo->rootcg_cmd_list); |
| 1023 | INIT_LIST_HEAD(&lo->idle_worker_list); |
| 1024 | lo->worker_tree = RB_ROOT; |
| 1025 | timer_setup(&lo->timer, loop_free_idle_workers, |
| 1026 | TIMER_DEFERRABLE); |
Martijn Coenen | 3448914 | 2020-05-13 15:38:45 +0200 | [diff] [blame] | 1027 | lo->use_dio = lo->lo_flags & LO_FLAGS_DIRECT_IO; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1028 | lo->lo_device = bdev; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1029 | lo->lo_backing_file = file; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1030 | lo->old_gfp_mask = mapping_gfp_mask(mapping); |
| 1031 | mapping_set_gfp_mask(mapping, lo->old_gfp_mask & ~(__GFP_IO|__GFP_FS)); |
| 1032 | |
Martijn Coenen | 3448914 | 2020-05-13 15:38:45 +0200 | [diff] [blame] | 1033 | if (!(lo->lo_flags & LO_FLAGS_READ_ONLY) && file->f_op->fsync) |
Jens Axboe | 21d0727 | 2016-03-30 10:09:35 -0600 | [diff] [blame] | 1034 | blk_queue_write_cache(lo->lo_queue, true, false); |
Nikanth Karthikesan | 68db196 | 2009-03-24 12:29:54 +0100 | [diff] [blame] | 1035 | |
Martijn Coenen | 3448914 | 2020-05-13 15:38:45 +0200 | [diff] [blame] | 1036 | if (config->block_size) |
| 1037 | bsize = config->block_size; |
Linus Torvalds | 96ed320 | 2020-06-02 19:45:12 -0700 | [diff] [blame] | 1038 | else if ((lo->lo_backing_file->f_flags & O_DIRECT) && inode->i_sb->s_bdev) |
Martijn Coenen | 8556011 | 2019-09-04 21:49:01 +0200 | [diff] [blame] | 1039 | /* In case of direct I/O, match underlying block size */ |
Martijn Coenen | 3448914 | 2020-05-13 15:38:45 +0200 | [diff] [blame] | 1040 | bsize = bdev_logical_block_size(inode->i_sb->s_bdev); |
| 1041 | else |
| 1042 | bsize = 512; |
Martijn Coenen | 8556011 | 2019-09-04 21:49:01 +0200 | [diff] [blame] | 1043 | |
Martijn Coenen | 3448914 | 2020-05-13 15:38:45 +0200 | [diff] [blame] | 1044 | blk_queue_logical_block_size(lo->lo_queue, bsize); |
| 1045 | blk_queue_physical_block_size(lo->lo_queue, bsize); |
| 1046 | blk_queue_io_min(lo->lo_queue, bsize); |
Martijn Coenen | 8556011 | 2019-09-04 21:49:01 +0200 | [diff] [blame] | 1047 | |
Kristian Klausen | 2b9ac22 | 2021-06-18 13:51:57 +0200 | [diff] [blame] | 1048 | loop_config_discard(lo); |
Holger Hoffstätte | 56a85fd | 2019-02-12 15:54:24 -0700 | [diff] [blame] | 1049 | loop_update_rotational(lo); |
Ming Lei | 2e5ab5f | 2015-08-17 10:31:49 +0800 | [diff] [blame] | 1050 | loop_update_dio(lo); |
Milan Broz | ee86273 | 2010-08-23 15:16:00 +0200 | [diff] [blame] | 1051 | loop_sysfs_init(lo); |
Martijn Coenen | 79e5dc5 | 2020-08-25 09:18:29 +0200 | [diff] [blame] | 1052 | |
| 1053 | size = get_loop_size(lo, file); |
Martijn Coenen | 5795b6f | 2020-05-13 15:38:37 +0200 | [diff] [blame] | 1054 | loop_set_size(lo, size); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1055 | |
Tetsuo Handa | 3ce6e1f | 2021-07-06 23:40:34 +0900 | [diff] [blame] | 1056 | /* Order wrt reading lo_state in loop_validate_file(). */ |
| 1057 | wmb(); |
| 1058 | |
Serge E. Hallyn | 6c99791 | 2006-09-29 01:59:11 -0700 | [diff] [blame] | 1059 | lo->lo_state = Lo_bound; |
Kay Sievers | e03c8dd | 2011-08-23 20:12:04 +0200 | [diff] [blame] | 1060 | if (part_shift) |
| 1061 | lo->lo_flags |= LO_FLAGS_PARTSCAN; |
Jan Kara | 85b0a54 | 2018-11-08 14:01:13 +0100 | [diff] [blame] | 1062 | partscan = lo->lo_flags & LO_FLAGS_PARTSCAN; |
Lennart Poettering | fe6a8fc | 2020-08-10 19:16:32 +0200 | [diff] [blame] | 1063 | if (partscan) |
Christoph Hellwig | 46e7eac | 2021-11-22 14:06:17 +0100 | [diff] [blame] | 1064 | lo->lo_disk->flags &= ~GENHD_FL_NO_PART; |
Anatol Pomozov | c1681bf | 2013-04-01 09:47:56 -0700 | [diff] [blame] | 1065 | |
Tetsuo Handa | 3ce6e1f | 2021-07-06 23:40:34 +0900 | [diff] [blame] | 1066 | loop_global_unlock(lo, is_loop); |
Jan Kara | 85b0a54 | 2018-11-08 14:01:13 +0100 | [diff] [blame] | 1067 | if (partscan) |
Christoph Hellwig | 0384264 | 2021-06-24 14:32:40 +0200 | [diff] [blame] | 1068 | loop_reread_partitions(lo); |
Christoph Hellwig | 37c3fc9 | 2020-11-25 21:20:08 +0100 | [diff] [blame] | 1069 | if (!(mode & FMODE_EXCL)) |
| 1070 | bd_abort_claiming(bdev, loop_configure); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1071 | return 0; |
| 1072 | |
Jan Kara | 757ecf4 | 2018-11-08 14:01:10 +0100 | [diff] [blame] | 1073 | out_unlock: |
Tetsuo Handa | 3ce6e1f | 2021-07-06 23:40:34 +0900 | [diff] [blame] | 1074 | loop_global_unlock(lo, is_loop); |
Jan Kara | 33ec3e5 | 2019-05-16 16:01:27 +0200 | [diff] [blame] | 1075 | out_bdev: |
Christoph Hellwig | 37c3fc9 | 2020-11-25 21:20:08 +0100 | [diff] [blame] | 1076 | if (!(mode & FMODE_EXCL)) |
| 1077 | bd_abort_claiming(bdev, loop_configure); |
Jan Kara | 757ecf4 | 2018-11-08 14:01:10 +0100 | [diff] [blame] | 1078 | out_putf: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1079 | fput(file); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1080 | /* This is safe: open() is still holding a reference. */ |
| 1081 | module_put(THIS_MODULE); |
| 1082 | return error; |
| 1083 | } |
| 1084 | |
Tetsuo Handa | bf23747 | 2022-02-11 16:15:54 +0900 | [diff] [blame] | 1085 | static void __loop_clr_fd(struct loop_device *lo, bool release) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1086 | { |
Tetsuo Handa | 6050fa4 | 2021-11-24 19:47:40 +0900 | [diff] [blame] | 1087 | struct file *filp; |
Al Viro | b4e3ca1 | 2005-10-21 03:22:34 -0400 | [diff] [blame] | 1088 | gfp_t gfp = lo->old_gfp_mask; |
Dan Schatzberg | 87579e9 | 2021-06-28 19:38:15 -0700 | [diff] [blame] | 1089 | struct loop_worker *pos, *worker; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1090 | |
Tetsuo Handa | 3ce6e1f | 2021-07-06 23:40:34 +0900 | [diff] [blame] | 1091 | /* |
| 1092 | * Flush loop_configure() and loop_change_fd(). It is acceptable for |
| 1093 | * loop_validate_file() to succeed, for actual clear operation has not |
| 1094 | * started yet. |
| 1095 | */ |
| 1096 | mutex_lock(&loop_validate_mutex); |
| 1097 | mutex_unlock(&loop_validate_mutex); |
| 1098 | /* |
| 1099 | * loop_validate_file() now fails because l->lo_state != Lo_bound |
| 1100 | * became visible. |
| 1101 | */ |
| 1102 | |
Tetsuo Handa | 6050fa4 | 2021-11-24 19:47:40 +0900 | [diff] [blame] | 1103 | /* |
| 1104 | * Since this function is called upon "ioctl(LOOP_CLR_FD)" xor "close() |
| 1105 | * after ioctl(LOOP_CLR_FD)", it is a sign of something going wrong if |
| 1106 | * lo->lo_state has changed while waiting for lo->lo_mutex. |
| 1107 | */ |
Pavel Tatashin | 6cc8e74 | 2021-01-26 09:46:30 -0500 | [diff] [blame] | 1108 | mutex_lock(&lo->lo_mutex); |
Tetsuo Handa | 6050fa4 | 2021-11-24 19:47:40 +0900 | [diff] [blame] | 1109 | BUG_ON(lo->lo_state != Lo_rundown); |
| 1110 | mutex_unlock(&lo->lo_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1111 | |
Mauricio Faria de Oliveira | 4ceddce | 2021-02-22 12:41:23 -0300 | [diff] [blame] | 1112 | if (test_bit(QUEUE_FLAG_WC, &lo->lo_queue->queue_flags)) |
| 1113 | blk_queue_write_cache(lo->lo_queue, false, false); |
| 1114 | |
Ming Lei | f893366 | 2015-05-06 12:26:23 +0800 | [diff] [blame] | 1115 | /* freeze request queue during the transition */ |
| 1116 | blk_mq_freeze_queue(lo->lo_queue); |
| 1117 | |
Dan Schatzberg | 87579e9 | 2021-06-28 19:38:15 -0700 | [diff] [blame] | 1118 | destroy_workqueue(lo->workqueue); |
| 1119 | spin_lock_irq(&lo->lo_work_lock); |
| 1120 | list_for_each_entry_safe(worker, pos, &lo->idle_worker_list, |
| 1121 | idle_list) { |
| 1122 | list_del(&worker->idle_list); |
| 1123 | rb_erase(&worker->rb_node, &lo->worker_tree); |
Dan Schatzberg | c74d40e | 2021-06-28 19:38:21 -0700 | [diff] [blame] | 1124 | css_put(worker->blkcg_css); |
Dan Schatzberg | 87579e9 | 2021-06-28 19:38:15 -0700 | [diff] [blame] | 1125 | kfree(worker); |
| 1126 | } |
| 1127 | spin_unlock_irq(&lo->lo_work_lock); |
| 1128 | del_timer_sync(&lo->timer); |
| 1129 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1130 | spin_lock_irq(&lo->lo_lock); |
Tetsuo Handa | 6050fa4 | 2021-11-24 19:47:40 +0900 | [diff] [blame] | 1131 | filp = lo->lo_backing_file; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1132 | lo->lo_backing_file = NULL; |
Kay Sievers | 05eb0f2 | 2011-07-31 22:21:35 +0200 | [diff] [blame] | 1133 | spin_unlock_irq(&lo->lo_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1134 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1135 | lo->lo_device = NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1136 | lo->lo_offset = 0; |
| 1137 | lo->lo_sizelimit = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1138 | memset(lo->lo_file_name, 0, LO_NAME_SIZE); |
Omar Sandoval | 89e4fde | 2017-08-24 00:03:43 -0700 | [diff] [blame] | 1139 | blk_queue_logical_block_size(lo->lo_queue, 512); |
Omar Sandoval | bf09375 | 2017-09-05 14:24:47 -0700 | [diff] [blame] | 1140 | blk_queue_physical_block_size(lo->lo_queue, 512); |
| 1141 | blk_queue_io_min(lo->lo_queue, 512); |
Xie Yongji | e515be8 | 2021-09-22 20:37:09 +0800 | [diff] [blame] | 1142 | invalidate_disk(lo->lo_disk); |
Milan Broz | 51a0bb0 | 2010-10-27 19:51:30 -0600 | [diff] [blame] | 1143 | loop_sysfs_exit(lo); |
Xie Yongji | 19f553d | 2021-09-22 20:37:10 +0800 | [diff] [blame] | 1144 | /* let user-space know about this change */ |
| 1145 | kobject_uevent(&disk_to_dev(lo->lo_disk)->kobj, KOBJ_CHANGE); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1146 | mapping_set_gfp_mask(filp->f_mapping, gfp); |
Tetsuo Handa | bf23747 | 2022-02-11 16:15:54 +0900 | [diff] [blame] | 1147 | /* This is safe: open() is still holding a reference. */ |
| 1148 | module_put(THIS_MODULE); |
Ming Lei | f893366 | 2015-05-06 12:26:23 +0800 | [diff] [blame] | 1149 | blk_mq_unfreeze_queue(lo->lo_queue); |
| 1150 | |
Matteo Croce | 9f65c48 | 2021-07-13 01:05:30 +0200 | [diff] [blame] | 1151 | disk_force_media_change(lo->lo_disk, DISK_EVENT_MEDIA_CHANGE); |
Tetsuo Handa | 6050fa4 | 2021-11-24 19:47:40 +0900 | [diff] [blame] | 1152 | |
| 1153 | if (lo->lo_flags & LO_FLAGS_PARTSCAN) { |
| 1154 | int err; |
| 1155 | |
Tetsuo Handa | bf23747 | 2022-02-11 16:15:54 +0900 | [diff] [blame] | 1156 | /* |
| 1157 | * open_mutex has been held already in release path, so don't |
| 1158 | * acquire it if this function is called in such case. |
| 1159 | * |
| 1160 | * If the reread partition isn't from release path, lo_refcnt |
| 1161 | * must be at least one and it can only become zero when the |
| 1162 | * current holder is released. |
| 1163 | */ |
| 1164 | if (!release) |
| 1165 | mutex_lock(&lo->lo_disk->open_mutex); |
Christoph Hellwig | 0384264 | 2021-06-24 14:32:40 +0200 | [diff] [blame] | 1166 | err = bdev_disk_changed(lo->lo_disk, false); |
Tetsuo Handa | bf23747 | 2022-02-11 16:15:54 +0900 | [diff] [blame] | 1167 | if (!release) |
| 1168 | mutex_unlock(&lo->lo_disk->open_mutex); |
Dongli Zhang | 40853d6 | 2019-02-22 22:10:19 +0800 | [diff] [blame] | 1169 | if (err) |
| 1170 | pr_warn("%s: partition scan of loop%d failed (rc=%d)\n", |
Tetsuo Handa | 6050fa4 | 2021-11-24 19:47:40 +0900 | [diff] [blame] | 1171 | __func__, lo->lo_number, err); |
Jan Kara | d57f337 | 2018-11-08 14:01:12 +0100 | [diff] [blame] | 1172 | /* Device is gone, no point in returning error */ |
Jan Kara | d57f337 | 2018-11-08 14:01:12 +0100 | [diff] [blame] | 1173 | } |
Dongli Zhang | 758a58d | 2019-02-22 22:10:20 +0800 | [diff] [blame] | 1174 | |
Tetsuo Handa | bf23747 | 2022-02-11 16:15:54 +0900 | [diff] [blame] | 1175 | /* |
| 1176 | * lo->lo_state is set to Lo_unbound here after above partscan has |
| 1177 | * finished. There cannot be anybody else entering __loop_clr_fd() as |
| 1178 | * Lo_rundown state protects us from all the other places trying to |
| 1179 | * change the 'lo' device. |
| 1180 | */ |
Dongli Zhang | 758a58d | 2019-02-22 22:10:20 +0800 | [diff] [blame] | 1181 | lo->lo_flags = 0; |
| 1182 | if (!part_shift) |
Christoph Hellwig | 46e7eac | 2021-11-22 14:06:17 +0100 | [diff] [blame] | 1183 | lo->lo_disk->flags |= GENHD_FL_NO_PART; |
Tetsuo Handa | 6050fa4 | 2021-11-24 19:47:40 +0900 | [diff] [blame] | 1184 | mutex_lock(&lo->lo_mutex); |
Dongli Zhang | 758a58d | 2019-02-22 22:10:20 +0800 | [diff] [blame] | 1185 | lo->lo_state = Lo_unbound; |
Pavel Tatashin | 6cc8e74 | 2021-01-26 09:46:30 -0500 | [diff] [blame] | 1186 | mutex_unlock(&lo->lo_mutex); |
Dongli Zhang | 758a58d | 2019-02-22 22:10:20 +0800 | [diff] [blame] | 1187 | |
Tetsuo Handa | bf23747 | 2022-02-11 16:15:54 +0900 | [diff] [blame] | 1188 | /* |
| 1189 | * Need not hold lo_mutex to fput backing file. Calling fput holding |
| 1190 | * lo_mutex triggers a circular lock dependency possibility warning as |
| 1191 | * fput can take open_mutex which is usually taken before lo_mutex. |
| 1192 | */ |
| 1193 | fput(filp); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1194 | } |
| 1195 | |
Jan Kara | a2505b7 | 2018-11-08 14:01:06 +0100 | [diff] [blame] | 1196 | static int loop_clr_fd(struct loop_device *lo) |
| 1197 | { |
Jan Kara | 7ccd079 | 2018-11-08 14:01:07 +0100 | [diff] [blame] | 1198 | int err; |
| 1199 | |
Pavel Tatashin | 6cc8e74 | 2021-01-26 09:46:30 -0500 | [diff] [blame] | 1200 | err = mutex_lock_killable(&lo->lo_mutex); |
Jan Kara | 7ccd079 | 2018-11-08 14:01:07 +0100 | [diff] [blame] | 1201 | if (err) |
| 1202 | return err; |
| 1203 | if (lo->lo_state != Lo_bound) { |
Pavel Tatashin | 6cc8e74 | 2021-01-26 09:46:30 -0500 | [diff] [blame] | 1204 | mutex_unlock(&lo->lo_mutex); |
Jan Kara | a2505b7 | 2018-11-08 14:01:06 +0100 | [diff] [blame] | 1205 | return -ENXIO; |
Jan Kara | 7ccd079 | 2018-11-08 14:01:07 +0100 | [diff] [blame] | 1206 | } |
Jan Kara | a2505b7 | 2018-11-08 14:01:06 +0100 | [diff] [blame] | 1207 | /* |
| 1208 | * If we've explicitly asked to tear down the loop device, |
| 1209 | * and it has an elevated reference count, set it for auto-teardown when |
| 1210 | * the last reference goes away. This stops $!~#$@ udev from |
| 1211 | * preventing teardown because it decided that it needs to run blkid on |
| 1212 | * the loopback device whenever they appear. xfstests is notorious for |
| 1213 | * failing tests because blkid via udev races with a losetup |
| 1214 | * <dev>/do something like mkfs/losetup -d <dev> causing the losetup -d |
| 1215 | * command to fail with EBUSY. |
| 1216 | */ |
| 1217 | if (atomic_read(&lo->lo_refcnt) > 1) { |
| 1218 | lo->lo_flags |= LO_FLAGS_AUTOCLEAR; |
Pavel Tatashin | 6cc8e74 | 2021-01-26 09:46:30 -0500 | [diff] [blame] | 1219 | mutex_unlock(&lo->lo_mutex); |
Jan Kara | a2505b7 | 2018-11-08 14:01:06 +0100 | [diff] [blame] | 1220 | return 0; |
| 1221 | } |
| 1222 | lo->lo_state = Lo_rundown; |
Pavel Tatashin | 6cc8e74 | 2021-01-26 09:46:30 -0500 | [diff] [blame] | 1223 | mutex_unlock(&lo->lo_mutex); |
Jan Kara | a2505b7 | 2018-11-08 14:01:06 +0100 | [diff] [blame] | 1224 | |
Tetsuo Handa | bf23747 | 2022-02-11 16:15:54 +0900 | [diff] [blame] | 1225 | __loop_clr_fd(lo, false); |
Tetsuo Handa | 6050fa4 | 2021-11-24 19:47:40 +0900 | [diff] [blame] | 1226 | return 0; |
Jan Kara | a2505b7 | 2018-11-08 14:01:06 +0100 | [diff] [blame] | 1227 | } |
| 1228 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1229 | static int |
| 1230 | loop_set_status(struct loop_device *lo, const struct loop_info64 *info) |
| 1231 | { |
| 1232 | int err; |
Martijn Coenen | faf1d25 | 2020-05-13 15:38:44 +0200 | [diff] [blame] | 1233 | int prev_lo_flags; |
Jan Kara | 85b0a54 | 2018-11-08 14:01:13 +0100 | [diff] [blame] | 1234 | bool partscan = false; |
Martijn Coenen | 0c3796c | 2020-05-13 15:38:41 +0200 | [diff] [blame] | 1235 | bool size_changed = false; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1236 | |
Pavel Tatashin | 6cc8e74 | 2021-01-26 09:46:30 -0500 | [diff] [blame] | 1237 | err = mutex_lock_killable(&lo->lo_mutex); |
Jan Kara | 550df5fd | 2018-11-08 14:01:09 +0100 | [diff] [blame] | 1238 | if (err) |
| 1239 | return err; |
Jan Kara | 550df5fd | 2018-11-08 14:01:09 +0100 | [diff] [blame] | 1240 | if (lo->lo_state != Lo_bound) { |
| 1241 | err = -ENXIO; |
| 1242 | goto out_unlock; |
| 1243 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1244 | |
Jaegeuk Kim | 5db470e | 2019-01-09 19:17:14 -0800 | [diff] [blame] | 1245 | if (lo->lo_offset != info->lo_offset || |
| 1246 | lo->lo_sizelimit != info->lo_sizelimit) { |
Martijn Coenen | 0c3796c | 2020-05-13 15:38:41 +0200 | [diff] [blame] | 1247 | size_changed = true; |
Jaegeuk Kim | 5db470e | 2019-01-09 19:17:14 -0800 | [diff] [blame] | 1248 | sync_blockdev(lo->lo_device); |
Zheng Bin | f4bd34b | 2020-06-18 12:21:37 +0800 | [diff] [blame] | 1249 | invalidate_bdev(lo->lo_device); |
Jaegeuk Kim | 5db470e | 2019-01-09 19:17:14 -0800 | [diff] [blame] | 1250 | } |
| 1251 | |
Ming Lei | ecdd095 | 2017-02-11 11:40:45 +0800 | [diff] [blame] | 1252 | /* I/O need to be drained during transfer transition */ |
| 1253 | blk_mq_freeze_queue(lo->lo_queue); |
| 1254 | |
Martijn Coenen | 0c3796c | 2020-05-13 15:38:41 +0200 | [diff] [blame] | 1255 | if (size_changed && lo->lo_device->bd_inode->i_mapping->nrpages) { |
Zheng Bin | f4bd34b | 2020-06-18 12:21:37 +0800 | [diff] [blame] | 1256 | /* If any pages were dirtied after invalidate_bdev(), try again */ |
Martijn Coenen | 0c3796c | 2020-05-13 15:38:41 +0200 | [diff] [blame] | 1257 | err = -EAGAIN; |
| 1258 | pr_warn("%s: loop%d (%s) has still dirty pages (nrpages=%lu)\n", |
| 1259 | __func__, lo->lo_number, lo->lo_file_name, |
| 1260 | lo->lo_device->bd_inode->i_mapping->nrpages); |
| 1261 | goto out_unfreeze; |
| 1262 | } |
| 1263 | |
Martijn Coenen | faf1d25 | 2020-05-13 15:38:44 +0200 | [diff] [blame] | 1264 | prev_lo_flags = lo->lo_flags; |
| 1265 | |
Martijn Coenen | 0c3796c | 2020-05-13 15:38:41 +0200 | [diff] [blame] | 1266 | err = loop_set_status_from_info(lo, info); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1267 | if (err) |
Jan Kara | 550df5fd | 2018-11-08 14:01:09 +0100 | [diff] [blame] | 1268 | goto out_unfreeze; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1269 | |
Martijn Coenen | faf1d25 | 2020-05-13 15:38:44 +0200 | [diff] [blame] | 1270 | /* Mask out flags that can't be set using LOOP_SET_STATUS. */ |
Martijn Coenen | 6ac92fb | 2020-06-04 22:25:20 +0200 | [diff] [blame] | 1271 | lo->lo_flags &= LOOP_SET_STATUS_SETTABLE_FLAGS; |
Martijn Coenen | faf1d25 | 2020-05-13 15:38:44 +0200 | [diff] [blame] | 1272 | /* For those flags, use the previous values instead */ |
| 1273 | lo->lo_flags |= prev_lo_flags & ~LOOP_SET_STATUS_SETTABLE_FLAGS; |
| 1274 | /* For flags that can't be cleared, use previous values too */ |
| 1275 | lo->lo_flags |= prev_lo_flags & ~LOOP_SET_STATUS_CLEARABLE_FLAGS; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1276 | |
Martijn Coenen | b0bd158 | 2020-05-13 15:38:39 +0200 | [diff] [blame] | 1277 | if (size_changed) { |
| 1278 | loff_t new_size = get_size(lo->lo_offset, lo->lo_sizelimit, |
| 1279 | lo->lo_backing_file); |
| 1280 | loop_set_size(lo, new_size); |
Arnd Bergmann | b040ad9 | 2017-06-09 12:19:18 +0200 | [diff] [blame] | 1281 | } |
Guo Chao | 541c742 | 2013-02-21 15:16:46 -0800 | [diff] [blame] | 1282 | |
Lukas Czerner | dfaa2ef | 2011-08-19 14:50:46 +0200 | [diff] [blame] | 1283 | loop_config_discard(lo); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1284 | |
Ming Lei | 2e5ab5f | 2015-08-17 10:31:49 +0800 | [diff] [blame] | 1285 | /* update dio if lo_offset or transfer is changed */ |
| 1286 | __loop_update_dio(lo, lo->use_dio); |
| 1287 | |
Jan Kara | 550df5fd | 2018-11-08 14:01:09 +0100 | [diff] [blame] | 1288 | out_unfreeze: |
Ming Lei | ecdd095 | 2017-02-11 11:40:45 +0800 | [diff] [blame] | 1289 | blk_mq_unfreeze_queue(lo->lo_queue); |
Omar Sandoval | e02898b4 | 2017-03-01 10:42:38 -0800 | [diff] [blame] | 1290 | |
Martijn Coenen | faf1d25 | 2020-05-13 15:38:44 +0200 | [diff] [blame] | 1291 | if (!err && (lo->lo_flags & LO_FLAGS_PARTSCAN) && |
| 1292 | !(prev_lo_flags & LO_FLAGS_PARTSCAN)) { |
Christoph Hellwig | 46e7eac | 2021-11-22 14:06:17 +0100 | [diff] [blame] | 1293 | lo->lo_disk->flags &= ~GENHD_FL_NO_PART; |
Jan Kara | 85b0a54 | 2018-11-08 14:01:13 +0100 | [diff] [blame] | 1294 | partscan = true; |
Omar Sandoval | e02898b4 | 2017-03-01 10:42:38 -0800 | [diff] [blame] | 1295 | } |
Jan Kara | 550df5fd | 2018-11-08 14:01:09 +0100 | [diff] [blame] | 1296 | out_unlock: |
Pavel Tatashin | 6cc8e74 | 2021-01-26 09:46:30 -0500 | [diff] [blame] | 1297 | mutex_unlock(&lo->lo_mutex); |
Jan Kara | 85b0a54 | 2018-11-08 14:01:13 +0100 | [diff] [blame] | 1298 | if (partscan) |
Christoph Hellwig | 0384264 | 2021-06-24 14:32:40 +0200 | [diff] [blame] | 1299 | loop_reread_partitions(lo); |
Omar Sandoval | e02898b4 | 2017-03-01 10:42:38 -0800 | [diff] [blame] | 1300 | |
Ming Lei | ecdd095 | 2017-02-11 11:40:45 +0800 | [diff] [blame] | 1301 | return err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1302 | } |
| 1303 | |
| 1304 | static int |
| 1305 | loop_get_status(struct loop_device *lo, struct loop_info64 *info) |
| 1306 | { |
Tetsuo Handa | b1ab5fa | 2018-11-08 14:01:01 +0100 | [diff] [blame] | 1307 | struct path path; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1308 | struct kstat stat; |
Omar Sandoval | 2d1d4c1 | 2018-03-26 21:39:11 -0700 | [diff] [blame] | 1309 | int ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1310 | |
Pavel Tatashin | 6cc8e74 | 2021-01-26 09:46:30 -0500 | [diff] [blame] | 1311 | ret = mutex_lock_killable(&lo->lo_mutex); |
Jan Kara | 4a5ce9b | 2018-11-08 14:01:08 +0100 | [diff] [blame] | 1312 | if (ret) |
| 1313 | return ret; |
Omar Sandoval | 2d1d4c1 | 2018-03-26 21:39:11 -0700 | [diff] [blame] | 1314 | if (lo->lo_state != Lo_bound) { |
Pavel Tatashin | 6cc8e74 | 2021-01-26 09:46:30 -0500 | [diff] [blame] | 1315 | mutex_unlock(&lo->lo_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1316 | return -ENXIO; |
Omar Sandoval | 2d1d4c1 | 2018-03-26 21:39:11 -0700 | [diff] [blame] | 1317 | } |
| 1318 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1319 | memset(info, 0, sizeof(*info)); |
| 1320 | info->lo_number = lo->lo_number; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1321 | info->lo_offset = lo->lo_offset; |
| 1322 | info->lo_sizelimit = lo->lo_sizelimit; |
| 1323 | info->lo_flags = lo->lo_flags; |
| 1324 | memcpy(info->lo_file_name, lo->lo_file_name, LO_NAME_SIZE); |
Omar Sandoval | 2d1d4c1 | 2018-03-26 21:39:11 -0700 | [diff] [blame] | 1325 | |
Pavel Tatashin | 6cc8e74 | 2021-01-26 09:46:30 -0500 | [diff] [blame] | 1326 | /* Drop lo_mutex while we call into the filesystem. */ |
Tetsuo Handa | b1ab5fa | 2018-11-08 14:01:01 +0100 | [diff] [blame] | 1327 | path = lo->lo_backing_file->f_path; |
| 1328 | path_get(&path); |
Pavel Tatashin | 6cc8e74 | 2021-01-26 09:46:30 -0500 | [diff] [blame] | 1329 | mutex_unlock(&lo->lo_mutex); |
Tetsuo Handa | b1ab5fa | 2018-11-08 14:01:01 +0100 | [diff] [blame] | 1330 | ret = vfs_getattr(&path, &stat, STATX_INO, AT_STATX_SYNC_AS_STAT); |
Omar Sandoval | 2d1d4c1 | 2018-03-26 21:39:11 -0700 | [diff] [blame] | 1331 | if (!ret) { |
| 1332 | info->lo_device = huge_encode_dev(stat.dev); |
| 1333 | info->lo_inode = stat.ino; |
| 1334 | info->lo_rdevice = huge_encode_dev(stat.rdev); |
| 1335 | } |
Tetsuo Handa | b1ab5fa | 2018-11-08 14:01:01 +0100 | [diff] [blame] | 1336 | path_put(&path); |
Omar Sandoval | 2d1d4c1 | 2018-03-26 21:39:11 -0700 | [diff] [blame] | 1337 | return ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1338 | } |
| 1339 | |
| 1340 | static void |
| 1341 | loop_info64_from_old(const struct loop_info *info, struct loop_info64 *info64) |
| 1342 | { |
| 1343 | memset(info64, 0, sizeof(*info64)); |
| 1344 | info64->lo_number = info->lo_number; |
| 1345 | info64->lo_device = info->lo_device; |
| 1346 | info64->lo_inode = info->lo_inode; |
| 1347 | info64->lo_rdevice = info->lo_rdevice; |
| 1348 | info64->lo_offset = info->lo_offset; |
| 1349 | info64->lo_sizelimit = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1350 | info64->lo_flags = info->lo_flags; |
Christoph Hellwig | 47e9624 | 2021-10-19 09:56:39 +0200 | [diff] [blame] | 1351 | memcpy(info64->lo_file_name, info->lo_name, LO_NAME_SIZE); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1352 | } |
| 1353 | |
| 1354 | static int |
| 1355 | loop_info64_to_old(const struct loop_info64 *info64, struct loop_info *info) |
| 1356 | { |
| 1357 | memset(info, 0, sizeof(*info)); |
| 1358 | info->lo_number = info64->lo_number; |
| 1359 | info->lo_device = info64->lo_device; |
| 1360 | info->lo_inode = info64->lo_inode; |
| 1361 | info->lo_rdevice = info64->lo_rdevice; |
| 1362 | info->lo_offset = info64->lo_offset; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1363 | info->lo_flags = info64->lo_flags; |
Christoph Hellwig | 47e9624 | 2021-10-19 09:56:39 +0200 | [diff] [blame] | 1364 | memcpy(info->lo_name, info64->lo_file_name, LO_NAME_SIZE); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1365 | |
| 1366 | /* error in case values were truncated */ |
| 1367 | if (info->lo_device != info64->lo_device || |
| 1368 | info->lo_rdevice != info64->lo_rdevice || |
| 1369 | info->lo_inode != info64->lo_inode || |
| 1370 | info->lo_offset != info64->lo_offset) |
| 1371 | return -EOVERFLOW; |
| 1372 | |
| 1373 | return 0; |
| 1374 | } |
| 1375 | |
| 1376 | static int |
| 1377 | loop_set_status_old(struct loop_device *lo, const struct loop_info __user *arg) |
| 1378 | { |
| 1379 | struct loop_info info; |
| 1380 | struct loop_info64 info64; |
| 1381 | |
| 1382 | if (copy_from_user(&info, arg, sizeof (struct loop_info))) |
| 1383 | return -EFAULT; |
| 1384 | loop_info64_from_old(&info, &info64); |
| 1385 | return loop_set_status(lo, &info64); |
| 1386 | } |
| 1387 | |
| 1388 | static int |
| 1389 | loop_set_status64(struct loop_device *lo, const struct loop_info64 __user *arg) |
| 1390 | { |
| 1391 | struct loop_info64 info64; |
| 1392 | |
| 1393 | if (copy_from_user(&info64, arg, sizeof (struct loop_info64))) |
| 1394 | return -EFAULT; |
| 1395 | return loop_set_status(lo, &info64); |
| 1396 | } |
| 1397 | |
| 1398 | static int |
| 1399 | loop_get_status_old(struct loop_device *lo, struct loop_info __user *arg) { |
| 1400 | struct loop_info info; |
| 1401 | struct loop_info64 info64; |
Omar Sandoval | bdac616db | 2018-04-06 09:57:03 -0700 | [diff] [blame] | 1402 | int err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1403 | |
Jan Kara | 4a5ce9b | 2018-11-08 14:01:08 +0100 | [diff] [blame] | 1404 | if (!arg) |
Omar Sandoval | bdac616db | 2018-04-06 09:57:03 -0700 | [diff] [blame] | 1405 | return -EINVAL; |
Omar Sandoval | bdac616db | 2018-04-06 09:57:03 -0700 | [diff] [blame] | 1406 | err = loop_get_status(lo, &info64); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1407 | if (!err) |
| 1408 | err = loop_info64_to_old(&info64, &info); |
| 1409 | if (!err && copy_to_user(arg, &info, sizeof(info))) |
| 1410 | err = -EFAULT; |
| 1411 | |
| 1412 | return err; |
| 1413 | } |
| 1414 | |
| 1415 | static int |
| 1416 | loop_get_status64(struct loop_device *lo, struct loop_info64 __user *arg) { |
| 1417 | struct loop_info64 info64; |
Omar Sandoval | bdac616db | 2018-04-06 09:57:03 -0700 | [diff] [blame] | 1418 | int err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1419 | |
Jan Kara | 4a5ce9b | 2018-11-08 14:01:08 +0100 | [diff] [blame] | 1420 | if (!arg) |
Omar Sandoval | bdac616db | 2018-04-06 09:57:03 -0700 | [diff] [blame] | 1421 | return -EINVAL; |
Omar Sandoval | bdac616db | 2018-04-06 09:57:03 -0700 | [diff] [blame] | 1422 | err = loop_get_status(lo, &info64); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1423 | if (!err && copy_to_user(arg, &info64, sizeof(info64))) |
| 1424 | err = -EFAULT; |
| 1425 | |
| 1426 | return err; |
| 1427 | } |
| 1428 | |
Hannes Reinecke | 51001b7 | 2017-06-08 13:46:44 +0200 | [diff] [blame] | 1429 | static int loop_set_capacity(struct loop_device *lo) |
J. R. Okajima | 53d6660 | 2009-03-31 15:23:43 -0700 | [diff] [blame] | 1430 | { |
Martijn Coenen | 0a6ed1b | 2020-05-13 15:38:40 +0200 | [diff] [blame] | 1431 | loff_t size; |
| 1432 | |
J. R. Okajima | 53d6660 | 2009-03-31 15:23:43 -0700 | [diff] [blame] | 1433 | if (unlikely(lo->lo_state != Lo_bound)) |
Guo Chao | 7b0576a | 2013-02-21 15:16:47 -0800 | [diff] [blame] | 1434 | return -ENXIO; |
J. R. Okajima | 53d6660 | 2009-03-31 15:23:43 -0700 | [diff] [blame] | 1435 | |
Martijn Coenen | 0a6ed1b | 2020-05-13 15:38:40 +0200 | [diff] [blame] | 1436 | size = get_loop_size(lo, lo->lo_backing_file); |
| 1437 | loop_set_size(lo, size); |
Martijn Coenen | 083a6a5 | 2020-05-13 15:38:36 +0200 | [diff] [blame] | 1438 | |
| 1439 | return 0; |
J. R. Okajima | 53d6660 | 2009-03-31 15:23:43 -0700 | [diff] [blame] | 1440 | } |
| 1441 | |
Ming Lei | ab1cb27 | 2015-08-17 10:31:50 +0800 | [diff] [blame] | 1442 | static int loop_set_dio(struct loop_device *lo, unsigned long arg) |
| 1443 | { |
| 1444 | int error = -ENXIO; |
| 1445 | if (lo->lo_state != Lo_bound) |
| 1446 | goto out; |
| 1447 | |
| 1448 | __loop_update_dio(lo, !!arg); |
| 1449 | if (lo->use_dio == !!arg) |
| 1450 | return 0; |
| 1451 | error = -EINVAL; |
| 1452 | out: |
| 1453 | return error; |
| 1454 | } |
| 1455 | |
Omar Sandoval | 89e4fde | 2017-08-24 00:03:43 -0700 | [diff] [blame] | 1456 | static int loop_set_block_size(struct loop_device *lo, unsigned long arg) |
| 1457 | { |
Jaegeuk Kim | 5db470e | 2019-01-09 19:17:14 -0800 | [diff] [blame] | 1458 | int err = 0; |
| 1459 | |
Omar Sandoval | 89e4fde | 2017-08-24 00:03:43 -0700 | [diff] [blame] | 1460 | if (lo->lo_state != Lo_bound) |
| 1461 | return -ENXIO; |
| 1462 | |
Xie Yongji | af3c570 | 2021-10-26 22:40:14 +0800 | [diff] [blame] | 1463 | err = blk_validate_block_size(arg); |
Martijn Coenen | 3448914 | 2020-05-13 15:38:45 +0200 | [diff] [blame] | 1464 | if (err) |
| 1465 | return err; |
Omar Sandoval | 89e4fde | 2017-08-24 00:03:43 -0700 | [diff] [blame] | 1466 | |
Martijn Coenen | 7e81f99 | 2020-03-10 14:12:30 +0100 | [diff] [blame] | 1467 | if (lo->lo_queue->limits.logical_block_size == arg) |
| 1468 | return 0; |
| 1469 | |
| 1470 | sync_blockdev(lo->lo_device); |
Zheng Bin | f4bd34b | 2020-06-18 12:21:37 +0800 | [diff] [blame] | 1471 | invalidate_bdev(lo->lo_device); |
Jaegeuk Kim | 5db470e | 2019-01-09 19:17:14 -0800 | [diff] [blame] | 1472 | |
Omar Sandoval | 89e4fde | 2017-08-24 00:03:43 -0700 | [diff] [blame] | 1473 | blk_mq_freeze_queue(lo->lo_queue); |
| 1474 | |
Zheng Bin | f4bd34b | 2020-06-18 12:21:37 +0800 | [diff] [blame] | 1475 | /* invalidate_bdev should have truncated all the pages */ |
Martijn Coenen | 7e81f99 | 2020-03-10 14:12:30 +0100 | [diff] [blame] | 1476 | if (lo->lo_device->bd_inode->i_mapping->nrpages) { |
Jaegeuk Kim | 5db470e | 2019-01-09 19:17:14 -0800 | [diff] [blame] | 1477 | err = -EAGAIN; |
| 1478 | pr_warn("%s: loop%d (%s) has still dirty pages (nrpages=%lu)\n", |
| 1479 | __func__, lo->lo_number, lo->lo_file_name, |
| 1480 | lo->lo_device->bd_inode->i_mapping->nrpages); |
| 1481 | goto out_unfreeze; |
| 1482 | } |
| 1483 | |
Omar Sandoval | 89e4fde | 2017-08-24 00:03:43 -0700 | [diff] [blame] | 1484 | blk_queue_logical_block_size(lo->lo_queue, arg); |
Omar Sandoval | bf09375 | 2017-09-05 14:24:47 -0700 | [diff] [blame] | 1485 | blk_queue_physical_block_size(lo->lo_queue, arg); |
| 1486 | blk_queue_io_min(lo->lo_queue, arg); |
Omar Sandoval | 89e4fde | 2017-08-24 00:03:43 -0700 | [diff] [blame] | 1487 | loop_update_dio(lo); |
Jaegeuk Kim | 5db470e | 2019-01-09 19:17:14 -0800 | [diff] [blame] | 1488 | out_unfreeze: |
Omar Sandoval | 89e4fde | 2017-08-24 00:03:43 -0700 | [diff] [blame] | 1489 | blk_mq_unfreeze_queue(lo->lo_queue); |
| 1490 | |
Jaegeuk Kim | 5db470e | 2019-01-09 19:17:14 -0800 | [diff] [blame] | 1491 | return err; |
Omar Sandoval | 89e4fde | 2017-08-24 00:03:43 -0700 | [diff] [blame] | 1492 | } |
| 1493 | |
Jan Kara | a131654 | 2018-11-08 14:01:05 +0100 | [diff] [blame] | 1494 | static int lo_simple_ioctl(struct loop_device *lo, unsigned int cmd, |
| 1495 | unsigned long arg) |
| 1496 | { |
| 1497 | int err; |
| 1498 | |
Pavel Tatashin | 6cc8e74 | 2021-01-26 09:46:30 -0500 | [diff] [blame] | 1499 | err = mutex_lock_killable(&lo->lo_mutex); |
Jan Kara | a131654 | 2018-11-08 14:01:05 +0100 | [diff] [blame] | 1500 | if (err) |
| 1501 | return err; |
| 1502 | switch (cmd) { |
| 1503 | case LOOP_SET_CAPACITY: |
| 1504 | err = loop_set_capacity(lo); |
| 1505 | break; |
| 1506 | case LOOP_SET_DIRECT_IO: |
| 1507 | err = loop_set_dio(lo, arg); |
| 1508 | break; |
| 1509 | case LOOP_SET_BLOCK_SIZE: |
| 1510 | err = loop_set_block_size(lo, arg); |
| 1511 | break; |
| 1512 | default: |
Christoph Hellwig | 47e9624 | 2021-10-19 09:56:39 +0200 | [diff] [blame] | 1513 | err = -EINVAL; |
Jan Kara | a131654 | 2018-11-08 14:01:05 +0100 | [diff] [blame] | 1514 | } |
Pavel Tatashin | 6cc8e74 | 2021-01-26 09:46:30 -0500 | [diff] [blame] | 1515 | mutex_unlock(&lo->lo_mutex); |
Jan Kara | a131654 | 2018-11-08 14:01:05 +0100 | [diff] [blame] | 1516 | return err; |
| 1517 | } |
| 1518 | |
Al Viro | bb21488 | 2008-03-02 09:29:48 -0500 | [diff] [blame] | 1519 | static int lo_ioctl(struct block_device *bdev, fmode_t mode, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1520 | unsigned int cmd, unsigned long arg) |
| 1521 | { |
Al Viro | bb21488 | 2008-03-02 09:29:48 -0500 | [diff] [blame] | 1522 | struct loop_device *lo = bdev->bd_disk->private_data; |
Martijn Coenen | 571fae6 | 2020-05-13 15:38:43 +0200 | [diff] [blame] | 1523 | void __user *argp = (void __user *) arg; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1524 | int err; |
| 1525 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1526 | switch (cmd) { |
Martijn Coenen | 3448914 | 2020-05-13 15:38:45 +0200 | [diff] [blame] | 1527 | case LOOP_SET_FD: { |
| 1528 | /* |
| 1529 | * Legacy case - pass in a zeroed out struct loop_config with |
| 1530 | * only the file descriptor set , which corresponds with the |
| 1531 | * default parameters we'd have used otherwise. |
| 1532 | */ |
| 1533 | struct loop_config config; |
| 1534 | |
| 1535 | memset(&config, 0, sizeof(config)); |
| 1536 | config.fd = arg; |
| 1537 | |
| 1538 | return loop_configure(lo, mode, bdev, &config); |
| 1539 | } |
| 1540 | case LOOP_CONFIGURE: { |
| 1541 | struct loop_config config; |
| 1542 | |
| 1543 | if (copy_from_user(&config, argp, sizeof(config))) |
| 1544 | return -EFAULT; |
| 1545 | |
| 1546 | return loop_configure(lo, mode, bdev, &config); |
| 1547 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1548 | case LOOP_CHANGE_FD: |
Jan Kara | c371077 | 2018-11-08 14:01:11 +0100 | [diff] [blame] | 1549 | return loop_change_fd(lo, bdev, arg); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1550 | case LOOP_CLR_FD: |
Jan Kara | 7ccd079 | 2018-11-08 14:01:07 +0100 | [diff] [blame] | 1551 | return loop_clr_fd(lo); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1552 | case LOOP_SET_STATUS: |
Dmitry Monakhov | 7035b5d | 2011-11-16 09:21:49 +0100 | [diff] [blame] | 1553 | err = -EPERM; |
Jan Kara | a131654 | 2018-11-08 14:01:05 +0100 | [diff] [blame] | 1554 | if ((mode & FMODE_WRITE) || capable(CAP_SYS_ADMIN)) { |
Martijn Coenen | 571fae6 | 2020-05-13 15:38:43 +0200 | [diff] [blame] | 1555 | err = loop_set_status_old(lo, argp); |
Jan Kara | a131654 | 2018-11-08 14:01:05 +0100 | [diff] [blame] | 1556 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1557 | break; |
| 1558 | case LOOP_GET_STATUS: |
Martijn Coenen | 571fae6 | 2020-05-13 15:38:43 +0200 | [diff] [blame] | 1559 | return loop_get_status_old(lo, argp); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1560 | case LOOP_SET_STATUS64: |
Dmitry Monakhov | 7035b5d | 2011-11-16 09:21:49 +0100 | [diff] [blame] | 1561 | err = -EPERM; |
Jan Kara | a131654 | 2018-11-08 14:01:05 +0100 | [diff] [blame] | 1562 | if ((mode & FMODE_WRITE) || capable(CAP_SYS_ADMIN)) { |
Martijn Coenen | 571fae6 | 2020-05-13 15:38:43 +0200 | [diff] [blame] | 1563 | err = loop_set_status64(lo, argp); |
Jan Kara | a131654 | 2018-11-08 14:01:05 +0100 | [diff] [blame] | 1564 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1565 | break; |
| 1566 | case LOOP_GET_STATUS64: |
Martijn Coenen | 571fae6 | 2020-05-13 15:38:43 +0200 | [diff] [blame] | 1567 | return loop_get_status64(lo, argp); |
J. R. Okajima | 53d6660 | 2009-03-31 15:23:43 -0700 | [diff] [blame] | 1568 | case LOOP_SET_CAPACITY: |
Ming Lei | ab1cb27 | 2015-08-17 10:31:50 +0800 | [diff] [blame] | 1569 | case LOOP_SET_DIRECT_IO: |
Omar Sandoval | 89e4fde | 2017-08-24 00:03:43 -0700 | [diff] [blame] | 1570 | case LOOP_SET_BLOCK_SIZE: |
Jan Kara | a131654 | 2018-11-08 14:01:05 +0100 | [diff] [blame] | 1571 | if (!(mode & FMODE_WRITE) && !capable(CAP_SYS_ADMIN)) |
| 1572 | return -EPERM; |
Gustavo A. R. Silva | df561f66 | 2020-08-23 17:36:59 -0500 | [diff] [blame] | 1573 | fallthrough; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1574 | default: |
Jan Kara | a131654 | 2018-11-08 14:01:05 +0100 | [diff] [blame] | 1575 | err = lo_simple_ioctl(lo, cmd, arg); |
| 1576 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1577 | } |
Nikanth Karthikesan | f028f3b | 2009-03-24 12:33:41 +0100 | [diff] [blame] | 1578 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1579 | return err; |
| 1580 | } |
| 1581 | |
David Howells | 863d5b82 | 2006-08-29 19:06:14 +0100 | [diff] [blame] | 1582 | #ifdef CONFIG_COMPAT |
| 1583 | struct compat_loop_info { |
| 1584 | compat_int_t lo_number; /* ioctl r/o */ |
| 1585 | compat_dev_t lo_device; /* ioctl r/o */ |
| 1586 | compat_ulong_t lo_inode; /* ioctl r/o */ |
| 1587 | compat_dev_t lo_rdevice; /* ioctl r/o */ |
| 1588 | compat_int_t lo_offset; |
David Howells | 863d5b82 | 2006-08-29 19:06:14 +0100 | [diff] [blame] | 1589 | compat_int_t lo_encrypt_key_size; /* ioctl w/o */ |
| 1590 | compat_int_t lo_flags; /* ioctl r/o */ |
| 1591 | char lo_name[LO_NAME_SIZE]; |
| 1592 | unsigned char lo_encrypt_key[LO_KEY_SIZE]; /* ioctl w/o */ |
| 1593 | compat_ulong_t lo_init[2]; |
| 1594 | char reserved[4]; |
| 1595 | }; |
| 1596 | |
| 1597 | /* |
| 1598 | * Transfer 32-bit compatibility structure in userspace to 64-bit loop info |
| 1599 | * - noinlined to reduce stack space usage in main part of driver |
| 1600 | */ |
| 1601 | static noinline int |
Al Viro | ba674cf | 2006-10-10 22:48:27 +0100 | [diff] [blame] | 1602 | loop_info64_from_compat(const struct compat_loop_info __user *arg, |
David Howells | 863d5b82 | 2006-08-29 19:06:14 +0100 | [diff] [blame] | 1603 | struct loop_info64 *info64) |
| 1604 | { |
| 1605 | struct compat_loop_info info; |
| 1606 | |
| 1607 | if (copy_from_user(&info, arg, sizeof(info))) |
| 1608 | return -EFAULT; |
| 1609 | |
| 1610 | memset(info64, 0, sizeof(*info64)); |
| 1611 | info64->lo_number = info.lo_number; |
| 1612 | info64->lo_device = info.lo_device; |
| 1613 | info64->lo_inode = info.lo_inode; |
| 1614 | info64->lo_rdevice = info.lo_rdevice; |
| 1615 | info64->lo_offset = info.lo_offset; |
| 1616 | info64->lo_sizelimit = 0; |
David Howells | 863d5b82 | 2006-08-29 19:06:14 +0100 | [diff] [blame] | 1617 | info64->lo_flags = info.lo_flags; |
Christoph Hellwig | 47e9624 | 2021-10-19 09:56:39 +0200 | [diff] [blame] | 1618 | memcpy(info64->lo_file_name, info.lo_name, LO_NAME_SIZE); |
David Howells | 863d5b82 | 2006-08-29 19:06:14 +0100 | [diff] [blame] | 1619 | return 0; |
| 1620 | } |
| 1621 | |
| 1622 | /* |
| 1623 | * Transfer 64-bit loop info to 32-bit compatibility structure in userspace |
| 1624 | * - noinlined to reduce stack space usage in main part of driver |
| 1625 | */ |
| 1626 | static noinline int |
| 1627 | loop_info64_to_compat(const struct loop_info64 *info64, |
| 1628 | struct compat_loop_info __user *arg) |
| 1629 | { |
| 1630 | struct compat_loop_info info; |
| 1631 | |
| 1632 | memset(&info, 0, sizeof(info)); |
| 1633 | info.lo_number = info64->lo_number; |
| 1634 | info.lo_device = info64->lo_device; |
| 1635 | info.lo_inode = info64->lo_inode; |
| 1636 | info.lo_rdevice = info64->lo_rdevice; |
| 1637 | info.lo_offset = info64->lo_offset; |
David Howells | 863d5b82 | 2006-08-29 19:06:14 +0100 | [diff] [blame] | 1638 | info.lo_flags = info64->lo_flags; |
Christoph Hellwig | 47e9624 | 2021-10-19 09:56:39 +0200 | [diff] [blame] | 1639 | memcpy(info.lo_name, info64->lo_file_name, LO_NAME_SIZE); |
David Howells | 863d5b82 | 2006-08-29 19:06:14 +0100 | [diff] [blame] | 1640 | |
| 1641 | /* error in case values were truncated */ |
| 1642 | if (info.lo_device != info64->lo_device || |
| 1643 | info.lo_rdevice != info64->lo_rdevice || |
| 1644 | info.lo_inode != info64->lo_inode || |
Christoph Hellwig | 47e9624 | 2021-10-19 09:56:39 +0200 | [diff] [blame] | 1645 | info.lo_offset != info64->lo_offset) |
David Howells | 863d5b82 | 2006-08-29 19:06:14 +0100 | [diff] [blame] | 1646 | return -EOVERFLOW; |
| 1647 | |
| 1648 | if (copy_to_user(arg, &info, sizeof(info))) |
| 1649 | return -EFAULT; |
| 1650 | return 0; |
| 1651 | } |
| 1652 | |
| 1653 | static int |
| 1654 | loop_set_status_compat(struct loop_device *lo, |
| 1655 | const struct compat_loop_info __user *arg) |
| 1656 | { |
| 1657 | struct loop_info64 info64; |
| 1658 | int ret; |
| 1659 | |
| 1660 | ret = loop_info64_from_compat(arg, &info64); |
| 1661 | if (ret < 0) |
| 1662 | return ret; |
| 1663 | return loop_set_status(lo, &info64); |
| 1664 | } |
| 1665 | |
| 1666 | static int |
| 1667 | loop_get_status_compat(struct loop_device *lo, |
| 1668 | struct compat_loop_info __user *arg) |
| 1669 | { |
| 1670 | struct loop_info64 info64; |
Omar Sandoval | bdac616db | 2018-04-06 09:57:03 -0700 | [diff] [blame] | 1671 | int err; |
David Howells | 863d5b82 | 2006-08-29 19:06:14 +0100 | [diff] [blame] | 1672 | |
Jan Kara | 4a5ce9b | 2018-11-08 14:01:08 +0100 | [diff] [blame] | 1673 | if (!arg) |
Omar Sandoval | bdac616db | 2018-04-06 09:57:03 -0700 | [diff] [blame] | 1674 | return -EINVAL; |
Omar Sandoval | bdac616db | 2018-04-06 09:57:03 -0700 | [diff] [blame] | 1675 | err = loop_get_status(lo, &info64); |
David Howells | 863d5b82 | 2006-08-29 19:06:14 +0100 | [diff] [blame] | 1676 | if (!err) |
| 1677 | err = loop_info64_to_compat(&info64, arg); |
| 1678 | return err; |
| 1679 | } |
| 1680 | |
Al Viro | bb21488 | 2008-03-02 09:29:48 -0500 | [diff] [blame] | 1681 | static int lo_compat_ioctl(struct block_device *bdev, fmode_t mode, |
| 1682 | unsigned int cmd, unsigned long arg) |
David Howells | 863d5b82 | 2006-08-29 19:06:14 +0100 | [diff] [blame] | 1683 | { |
Al Viro | bb21488 | 2008-03-02 09:29:48 -0500 | [diff] [blame] | 1684 | struct loop_device *lo = bdev->bd_disk->private_data; |
David Howells | 863d5b82 | 2006-08-29 19:06:14 +0100 | [diff] [blame] | 1685 | int err; |
| 1686 | |
David Howells | 863d5b82 | 2006-08-29 19:06:14 +0100 | [diff] [blame] | 1687 | switch(cmd) { |
| 1688 | case LOOP_SET_STATUS: |
Jan Kara | 550df5fd | 2018-11-08 14:01:09 +0100 | [diff] [blame] | 1689 | err = loop_set_status_compat(lo, |
| 1690 | (const struct compat_loop_info __user *)arg); |
David Howells | 863d5b82 | 2006-08-29 19:06:14 +0100 | [diff] [blame] | 1691 | break; |
| 1692 | case LOOP_GET_STATUS: |
Jan Kara | 4a5ce9b | 2018-11-08 14:01:08 +0100 | [diff] [blame] | 1693 | err = loop_get_status_compat(lo, |
| 1694 | (struct compat_loop_info __user *)arg); |
David Howells | 863d5b82 | 2006-08-29 19:06:14 +0100 | [diff] [blame] | 1695 | break; |
J. R. Okajima | 53d6660 | 2009-03-31 15:23:43 -0700 | [diff] [blame] | 1696 | case LOOP_SET_CAPACITY: |
David Howells | 863d5b82 | 2006-08-29 19:06:14 +0100 | [diff] [blame] | 1697 | case LOOP_CLR_FD: |
| 1698 | case LOOP_GET_STATUS64: |
| 1699 | case LOOP_SET_STATUS64: |
Martijn Coenen | 3448914 | 2020-05-13 15:38:45 +0200 | [diff] [blame] | 1700 | case LOOP_CONFIGURE: |
David Howells | 863d5b82 | 2006-08-29 19:06:14 +0100 | [diff] [blame] | 1701 | arg = (unsigned long) compat_ptr(arg); |
Gustavo A. R. Silva | df561f66 | 2020-08-23 17:36:59 -0500 | [diff] [blame] | 1702 | fallthrough; |
David Howells | 863d5b82 | 2006-08-29 19:06:14 +0100 | [diff] [blame] | 1703 | case LOOP_SET_FD: |
| 1704 | case LOOP_CHANGE_FD: |
Evan Green | 9fea4b3 | 2018-07-02 16:03:46 -0700 | [diff] [blame] | 1705 | case LOOP_SET_BLOCK_SIZE: |
Alessio Balsini | fdbe4ee | 2019-08-07 01:48:28 +0100 | [diff] [blame] | 1706 | case LOOP_SET_DIRECT_IO: |
Al Viro | bb21488 | 2008-03-02 09:29:48 -0500 | [diff] [blame] | 1707 | err = lo_ioctl(bdev, mode, cmd, arg); |
David Howells | 863d5b82 | 2006-08-29 19:06:14 +0100 | [diff] [blame] | 1708 | break; |
| 1709 | default: |
| 1710 | err = -ENOIOCTLCMD; |
| 1711 | break; |
| 1712 | } |
David Howells | 863d5b82 | 2006-08-29 19:06:14 +0100 | [diff] [blame] | 1713 | return err; |
| 1714 | } |
| 1715 | #endif |
| 1716 | |
Al Viro | bb21488 | 2008-03-02 09:29:48 -0500 | [diff] [blame] | 1717 | static int lo_open(struct block_device *bdev, fmode_t mode) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1718 | { |
Christoph Hellwig | 990e781 | 2021-06-05 17:09:50 +0300 | [diff] [blame] | 1719 | struct loop_device *lo = bdev->bd_disk->private_data; |
Jan Kara | 0a42e99 | 2018-11-08 14:01:04 +0100 | [diff] [blame] | 1720 | int err; |
Kay Sievers | 770fe30 | 2011-07-31 22:08:04 +0200 | [diff] [blame] | 1721 | |
Pavel Tatashin | 6cc8e74 | 2021-01-26 09:46:30 -0500 | [diff] [blame] | 1722 | err = mutex_lock_killable(&lo->lo_mutex); |
Pavel Tatashin | 6cc8e74 | 2021-01-26 09:46:30 -0500 | [diff] [blame] | 1723 | if (err) |
| 1724 | return err; |
Christoph Hellwig | 990e781 | 2021-06-05 17:09:50 +0300 | [diff] [blame] | 1725 | if (lo->lo_state == Lo_deleting) |
| 1726 | err = -ENXIO; |
| 1727 | else |
| 1728 | atomic_inc(&lo->lo_refcnt); |
Pavel Tatashin | 6cc8e74 | 2021-01-26 09:46:30 -0500 | [diff] [blame] | 1729 | mutex_unlock(&lo->lo_mutex); |
Christoph Hellwig | 990e781 | 2021-06-05 17:09:50 +0300 | [diff] [blame] | 1730 | return err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1731 | } |
| 1732 | |
Jan Kara | 967d1dc | 2018-11-08 14:01:03 +0100 | [diff] [blame] | 1733 | static void lo_release(struct gendisk *disk, fmode_t mode) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1734 | { |
Pavel Tatashin | 6cc8e74 | 2021-01-26 09:46:30 -0500 | [diff] [blame] | 1735 | struct loop_device *lo = disk->private_data; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1736 | |
Pavel Tatashin | 6cc8e74 | 2021-01-26 09:46:30 -0500 | [diff] [blame] | 1737 | mutex_lock(&lo->lo_mutex); |
Ming Lei | f893366 | 2015-05-06 12:26:23 +0800 | [diff] [blame] | 1738 | if (atomic_dec_return(&lo->lo_refcnt)) |
Jan Kara | 0a42e99 | 2018-11-08 14:01:04 +0100 | [diff] [blame] | 1739 | goto out_unlock; |
Ming Lei | f893366 | 2015-05-06 12:26:23 +0800 | [diff] [blame] | 1740 | |
Milan Broz | 14f2793 | 2008-12-12 14:48:27 +0100 | [diff] [blame] | 1741 | if (lo->lo_flags & LO_FLAGS_AUTOCLEAR) { |
Jan Kara | a2505b7 | 2018-11-08 14:01:06 +0100 | [diff] [blame] | 1742 | if (lo->lo_state != Lo_bound) |
| 1743 | goto out_unlock; |
| 1744 | lo->lo_state = Lo_rundown; |
Pavel Tatashin | 6cc8e74 | 2021-01-26 09:46:30 -0500 | [diff] [blame] | 1745 | mutex_unlock(&lo->lo_mutex); |
Milan Broz | 14f2793 | 2008-12-12 14:48:27 +0100 | [diff] [blame] | 1746 | /* |
| 1747 | * In autoclear mode, stop the loop thread |
| 1748 | * and remove configuration after last close. |
| 1749 | */ |
Tetsuo Handa | bf23747 | 2022-02-11 16:15:54 +0900 | [diff] [blame] | 1750 | __loop_clr_fd(lo, true); |
Jan Kara | 7ccd079 | 2018-11-08 14:01:07 +0100 | [diff] [blame] | 1751 | return; |
Omar Sandoval | 43cade80 | 2017-08-24 00:03:44 -0700 | [diff] [blame] | 1752 | } else if (lo->lo_state == Lo_bound) { |
Milan Broz | 14f2793 | 2008-12-12 14:48:27 +0100 | [diff] [blame] | 1753 | /* |
| 1754 | * Otherwise keep thread (if running) and config, |
| 1755 | * but flush possible ongoing bios in thread. |
| 1756 | */ |
Omar Sandoval | 43cade80 | 2017-08-24 00:03:44 -0700 | [diff] [blame] | 1757 | blk_mq_freeze_queue(lo->lo_queue); |
| 1758 | blk_mq_unfreeze_queue(lo->lo_queue); |
Milan Broz | 14f2793 | 2008-12-12 14:48:27 +0100 | [diff] [blame] | 1759 | } |
David Woodhouse | 96c5865 | 2008-02-06 01:36:27 -0800 | [diff] [blame] | 1760 | |
Jan Kara | 0a42e99 | 2018-11-08 14:01:04 +0100 | [diff] [blame] | 1761 | out_unlock: |
Pavel Tatashin | 6cc8e74 | 2021-01-26 09:46:30 -0500 | [diff] [blame] | 1762 | mutex_unlock(&lo->lo_mutex); |
Linus Torvalds | ae66501 | 2018-01-05 16:26:00 -0800 | [diff] [blame] | 1763 | } |
| 1764 | |
Alexey Dobriyan | 83d5cde | 2009-09-21 17:01:13 -0700 | [diff] [blame] | 1765 | static const struct block_device_operations lo_fops = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1766 | .owner = THIS_MODULE, |
Al Viro | bb21488 | 2008-03-02 09:29:48 -0500 | [diff] [blame] | 1767 | .open = lo_open, |
| 1768 | .release = lo_release, |
| 1769 | .ioctl = lo_ioctl, |
David Howells | 863d5b82 | 2006-08-29 19:06:14 +0100 | [diff] [blame] | 1770 | #ifdef CONFIG_COMPAT |
Al Viro | bb21488 | 2008-03-02 09:29:48 -0500 | [diff] [blame] | 1771 | .compat_ioctl = lo_compat_ioctl, |
David Howells | 863d5b82 | 2006-08-29 19:06:14 +0100 | [diff] [blame] | 1772 | #endif |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1773 | }; |
| 1774 | |
| 1775 | /* |
| 1776 | * And now the modules code and kernel interface. |
| 1777 | */ |
Ken Chen | 7328508 | 2007-05-08 00:28:20 -0700 | [diff] [blame] | 1778 | static int max_loop; |
Joe Perches | 5657a81 | 2018-05-24 13:38:59 -0600 | [diff] [blame] | 1779 | module_param(max_loop, int, 0444); |
Ken Chen | a47653f | 2007-06-08 13:46:44 -0700 | [diff] [blame] | 1780 | MODULE_PARM_DESC(max_loop, "Maximum number of loop devices"); |
Joe Perches | 5657a81 | 2018-05-24 13:38:59 -0600 | [diff] [blame] | 1781 | module_param(max_part, int, 0444); |
Laurent Vivier | 476a481 | 2008-03-26 12:11:53 +0100 | [diff] [blame] | 1782 | MODULE_PARM_DESC(max_part, "Maximum number of partitions per loop device"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1783 | MODULE_LICENSE("GPL"); |
| 1784 | MODULE_ALIAS_BLOCKDEV_MAJOR(LOOP_MAJOR); |
| 1785 | |
Christoph Hellwig | fc17b65 | 2017-06-03 09:38:05 +0200 | [diff] [blame] | 1786 | static blk_status_t loop_queue_rq(struct blk_mq_hw_ctx *hctx, |
Ming Lei | b5dd2f6 | 2014-12-31 13:22:57 +0000 | [diff] [blame] | 1787 | const struct blk_mq_queue_data *bd) |
| 1788 | { |
Jens Axboe | 1894e91 | 2018-04-13 16:24:29 -0600 | [diff] [blame] | 1789 | struct request *rq = bd->rq; |
| 1790 | struct loop_cmd *cmd = blk_mq_rq_to_pdu(rq); |
| 1791 | struct loop_device *lo = rq->q->queuedata; |
Ming Lei | b5dd2f6 | 2014-12-31 13:22:57 +0000 | [diff] [blame] | 1792 | |
Jens Axboe | 1894e91 | 2018-04-13 16:24:29 -0600 | [diff] [blame] | 1793 | blk_mq_start_request(rq); |
Ming Lei | b5dd2f6 | 2014-12-31 13:22:57 +0000 | [diff] [blame] | 1794 | |
Ming Lei | f4aa4c7 | 2015-05-05 19:49:54 +0800 | [diff] [blame] | 1795 | if (lo->lo_state != Lo_bound) |
Christoph Hellwig | fc17b65 | 2017-06-03 09:38:05 +0200 | [diff] [blame] | 1796 | return BLK_STS_IOERR; |
Ming Lei | f4aa4c7 | 2015-05-05 19:49:54 +0800 | [diff] [blame] | 1797 | |
Jens Axboe | 1894e91 | 2018-04-13 16:24:29 -0600 | [diff] [blame] | 1798 | switch (req_op(rq)) { |
Christoph Hellwig | f0225ca | 2016-08-04 16:10:00 +0200 | [diff] [blame] | 1799 | case REQ_OP_FLUSH: |
| 1800 | case REQ_OP_DISCARD: |
Christoph Hellwig | 19372e2 | 2017-04-05 19:21:15 +0200 | [diff] [blame] | 1801 | case REQ_OP_WRITE_ZEROES: |
Ming Lei | bc07c10 | 2015-08-17 10:31:51 +0800 | [diff] [blame] | 1802 | cmd->use_aio = false; |
Christoph Hellwig | f0225ca | 2016-08-04 16:10:00 +0200 | [diff] [blame] | 1803 | break; |
| 1804 | default: |
| 1805 | cmd->use_aio = lo->use_dio; |
| 1806 | break; |
| 1807 | } |
Ming Lei | bc07c10 | 2015-08-17 10:31:51 +0800 | [diff] [blame] | 1808 | |
Shaohua Li | d4478e9 | 2017-09-25 13:07:22 -0600 | [diff] [blame] | 1809 | /* always use the first bio's css */ |
Dan Schatzberg | c74d40e | 2021-06-28 19:38:21 -0700 | [diff] [blame] | 1810 | cmd->blkcg_css = NULL; |
| 1811 | cmd->memcg_css = NULL; |
Shaohua Li | 0b508bc | 2017-09-26 11:02:12 -0700 | [diff] [blame] | 1812 | #ifdef CONFIG_BLK_CGROUP |
Dan Schatzberg | c74d40e | 2021-06-28 19:38:21 -0700 | [diff] [blame] | 1813 | if (rq->bio && rq->bio->bi_blkg) { |
| 1814 | cmd->blkcg_css = &bio_blkcg(rq->bio)->css; |
| 1815 | #ifdef CONFIG_MEMCG |
| 1816 | cmd->memcg_css = |
| 1817 | cgroup_get_e_css(cmd->blkcg_css->cgroup, |
| 1818 | &memory_cgrp_subsys); |
Shaohua Li | d4478e9 | 2017-09-25 13:07:22 -0600 | [diff] [blame] | 1819 | #endif |
Dan Schatzberg | c74d40e | 2021-06-28 19:38:21 -0700 | [diff] [blame] | 1820 | } |
| 1821 | #endif |
Dan Schatzberg | 87579e9 | 2021-06-28 19:38:15 -0700 | [diff] [blame] | 1822 | loop_queue_work(lo, cmd); |
Ming Lei | b5dd2f6 | 2014-12-31 13:22:57 +0000 | [diff] [blame] | 1823 | |
Christoph Hellwig | fc17b65 | 2017-06-03 09:38:05 +0200 | [diff] [blame] | 1824 | return BLK_STS_OK; |
Ming Lei | b5dd2f6 | 2014-12-31 13:22:57 +0000 | [diff] [blame] | 1825 | } |
| 1826 | |
| 1827 | static void loop_handle_cmd(struct loop_cmd *cmd) |
| 1828 | { |
Jens Axboe | 1894e91 | 2018-04-13 16:24:29 -0600 | [diff] [blame] | 1829 | struct request *rq = blk_mq_rq_from_pdu(cmd); |
| 1830 | const bool write = op_is_write(req_op(rq)); |
| 1831 | struct loop_device *lo = rq->q->queuedata; |
Christoph Hellwig | f4829a9 | 2015-09-27 21:01:50 +0200 | [diff] [blame] | 1832 | int ret = 0; |
Dan Schatzberg | c74d40e | 2021-06-28 19:38:21 -0700 | [diff] [blame] | 1833 | struct mem_cgroup *old_memcg = NULL; |
Ming Lei | b5dd2f6 | 2014-12-31 13:22:57 +0000 | [diff] [blame] | 1834 | |
Christoph Hellwig | f4829a9 | 2015-09-27 21:01:50 +0200 | [diff] [blame] | 1835 | if (write && (lo->lo_flags & LO_FLAGS_READ_ONLY)) { |
| 1836 | ret = -EIO; |
Ming Lei | b5dd2f6 | 2014-12-31 13:22:57 +0000 | [diff] [blame] | 1837 | goto failed; |
Christoph Hellwig | f4829a9 | 2015-09-27 21:01:50 +0200 | [diff] [blame] | 1838 | } |
Ming Lei | b5dd2f6 | 2014-12-31 13:22:57 +0000 | [diff] [blame] | 1839 | |
Dan Schatzberg | c74d40e | 2021-06-28 19:38:21 -0700 | [diff] [blame] | 1840 | if (cmd->blkcg_css) |
| 1841 | kthread_associate_blkcg(cmd->blkcg_css); |
| 1842 | if (cmd->memcg_css) |
| 1843 | old_memcg = set_active_memcg( |
| 1844 | mem_cgroup_from_css(cmd->memcg_css)); |
| 1845 | |
Jens Axboe | 1894e91 | 2018-04-13 16:24:29 -0600 | [diff] [blame] | 1846 | ret = do_req_filebacked(lo, rq); |
Dan Schatzberg | c74d40e | 2021-06-28 19:38:21 -0700 | [diff] [blame] | 1847 | |
| 1848 | if (cmd->blkcg_css) |
| 1849 | kthread_associate_blkcg(NULL); |
| 1850 | |
| 1851 | if (cmd->memcg_css) { |
| 1852 | set_active_memcg(old_memcg); |
| 1853 | css_put(cmd->memcg_css); |
| 1854 | } |
Ming Lei | b5dd2f6 | 2014-12-31 13:22:57 +0000 | [diff] [blame] | 1855 | failed: |
Ming Lei | bc07c10 | 2015-08-17 10:31:51 +0800 | [diff] [blame] | 1856 | /* complete non-aio request */ |
Christoph Hellwig | fe2cb29 | 2017-04-20 16:03:02 +0200 | [diff] [blame] | 1857 | if (!cmd->use_aio || ret) { |
Evan Green | 8cd5508 | 2020-04-03 16:43:03 +0200 | [diff] [blame] | 1858 | if (ret == -EOPNOTSUPP) |
| 1859 | cmd->ret = ret; |
| 1860 | else |
| 1861 | cmd->ret = ret ? -EIO : 0; |
Christoph Hellwig | 15f73f5 | 2020-06-11 08:44:47 +0200 | [diff] [blame] | 1862 | if (likely(!blk_should_fake_timeout(rq->q))) |
| 1863 | blk_mq_complete_request(rq); |
Christoph Hellwig | fe2cb29 | 2017-04-20 16:03:02 +0200 | [diff] [blame] | 1864 | } |
Ming Lei | b5dd2f6 | 2014-12-31 13:22:57 +0000 | [diff] [blame] | 1865 | } |
| 1866 | |
Dan Schatzberg | 87579e9 | 2021-06-28 19:38:15 -0700 | [diff] [blame] | 1867 | static void loop_set_timer(struct loop_device *lo) |
Ming Lei | b5dd2f6 | 2014-12-31 13:22:57 +0000 | [diff] [blame] | 1868 | { |
Dan Schatzberg | 87579e9 | 2021-06-28 19:38:15 -0700 | [diff] [blame] | 1869 | timer_reduce(&lo->timer, jiffies + LOOP_IDLE_WORKER_TIMEOUT); |
Ming Lei | b5dd2f6 | 2014-12-31 13:22:57 +0000 | [diff] [blame] | 1870 | } |
| 1871 | |
Dan Schatzberg | 87579e9 | 2021-06-28 19:38:15 -0700 | [diff] [blame] | 1872 | static void loop_process_work(struct loop_worker *worker, |
| 1873 | struct list_head *cmd_list, struct loop_device *lo) |
Ming Lei | b5dd2f6 | 2014-12-31 13:22:57 +0000 | [diff] [blame] | 1874 | { |
Dan Schatzberg | 87579e9 | 2021-06-28 19:38:15 -0700 | [diff] [blame] | 1875 | int orig_flags = current->flags; |
| 1876 | struct loop_cmd *cmd; |
Ming Lei | b5dd2f6 | 2014-12-31 13:22:57 +0000 | [diff] [blame] | 1877 | |
Dan Schatzberg | 87579e9 | 2021-06-28 19:38:15 -0700 | [diff] [blame] | 1878 | current->flags |= PF_LOCAL_THROTTLE | PF_MEMALLOC_NOIO; |
| 1879 | spin_lock_irq(&lo->lo_work_lock); |
| 1880 | while (!list_empty(cmd_list)) { |
| 1881 | cmd = container_of( |
| 1882 | cmd_list->next, struct loop_cmd, list_entry); |
| 1883 | list_del(cmd_list->next); |
| 1884 | spin_unlock_irq(&lo->lo_work_lock); |
| 1885 | |
| 1886 | loop_handle_cmd(cmd); |
| 1887 | cond_resched(); |
| 1888 | |
| 1889 | spin_lock_irq(&lo->lo_work_lock); |
| 1890 | } |
| 1891 | |
| 1892 | /* |
| 1893 | * We only add to the idle list if there are no pending cmds |
| 1894 | * *and* the worker will not run again which ensures that it |
| 1895 | * is safe to free any worker on the idle list |
| 1896 | */ |
| 1897 | if (worker && !work_pending(&worker->work)) { |
| 1898 | worker->last_ran_at = jiffies; |
| 1899 | list_add_tail(&worker->idle_list, &lo->idle_worker_list); |
| 1900 | loop_set_timer(lo); |
| 1901 | } |
| 1902 | spin_unlock_irq(&lo->lo_work_lock); |
| 1903 | current->flags = orig_flags; |
| 1904 | } |
| 1905 | |
| 1906 | static void loop_workfn(struct work_struct *work) |
| 1907 | { |
| 1908 | struct loop_worker *worker = |
| 1909 | container_of(work, struct loop_worker, work); |
| 1910 | loop_process_work(worker, &worker->cmd_list, worker->lo); |
| 1911 | } |
| 1912 | |
| 1913 | static void loop_rootcg_workfn(struct work_struct *work) |
| 1914 | { |
| 1915 | struct loop_device *lo = |
| 1916 | container_of(work, struct loop_device, rootcg_work); |
| 1917 | loop_process_work(NULL, &lo->rootcg_cmd_list, lo); |
| 1918 | } |
| 1919 | |
| 1920 | static void loop_free_idle_workers(struct timer_list *timer) |
| 1921 | { |
| 1922 | struct loop_device *lo = container_of(timer, struct loop_device, timer); |
| 1923 | struct loop_worker *pos, *worker; |
| 1924 | |
| 1925 | spin_lock_irq(&lo->lo_work_lock); |
| 1926 | list_for_each_entry_safe(worker, pos, &lo->idle_worker_list, |
| 1927 | idle_list) { |
| 1928 | if (time_is_after_jiffies(worker->last_ran_at + |
| 1929 | LOOP_IDLE_WORKER_TIMEOUT)) |
| 1930 | break; |
| 1931 | list_del(&worker->idle_list); |
| 1932 | rb_erase(&worker->rb_node, &lo->worker_tree); |
Dan Schatzberg | c74d40e | 2021-06-28 19:38:21 -0700 | [diff] [blame] | 1933 | css_put(worker->blkcg_css); |
Dan Schatzberg | 87579e9 | 2021-06-28 19:38:15 -0700 | [diff] [blame] | 1934 | kfree(worker); |
| 1935 | } |
| 1936 | if (!list_empty(&lo->idle_worker_list)) |
| 1937 | loop_set_timer(lo); |
| 1938 | spin_unlock_irq(&lo->lo_work_lock); |
Ming Lei | b5dd2f6 | 2014-12-31 13:22:57 +0000 | [diff] [blame] | 1939 | } |
| 1940 | |
Eric Biggers | f363b08 | 2017-03-30 13:39:16 -0700 | [diff] [blame] | 1941 | static const struct blk_mq_ops loop_mq_ops = { |
Ming Lei | b5dd2f6 | 2014-12-31 13:22:57 +0000 | [diff] [blame] | 1942 | .queue_rq = loop_queue_rq, |
Christoph Hellwig | fe2cb29 | 2017-04-20 16:03:02 +0200 | [diff] [blame] | 1943 | .complete = lo_complete_rq, |
Ming Lei | b5dd2f6 | 2014-12-31 13:22:57 +0000 | [diff] [blame] | 1944 | }; |
| 1945 | |
Christoph Hellwig | d6da83d | 2021-06-23 16:59:02 +0200 | [diff] [blame] | 1946 | static int loop_add(int i) |
Ken Chen | 7328508 | 2007-05-08 00:28:20 -0700 | [diff] [blame] | 1947 | { |
| 1948 | struct loop_device *lo; |
| 1949 | struct gendisk *disk; |
Kay Sievers | 34dd82a | 2011-07-31 22:08:04 +0200 | [diff] [blame] | 1950 | int err; |
Ken Chen | 7328508 | 2007-05-08 00:28:20 -0700 | [diff] [blame] | 1951 | |
Silva Paulo | 68d740d | 2012-07-14 15:39:58 -0700 | [diff] [blame] | 1952 | err = -ENOMEM; |
Ken Chen | 7328508 | 2007-05-08 00:28:20 -0700 | [diff] [blame] | 1953 | lo = kzalloc(sizeof(*lo), GFP_KERNEL); |
Silva Paulo | 68d740d | 2012-07-14 15:39:58 -0700 | [diff] [blame] | 1954 | if (!lo) |
Ken Chen | 7328508 | 2007-05-08 00:28:20 -0700 | [diff] [blame] | 1955 | goto out; |
Mikulas Patocka | ef7e7c8 | 2013-10-15 14:14:38 -0600 | [diff] [blame] | 1956 | lo->lo_state = Lo_unbound; |
| 1957 | |
Christoph Hellwig | 18d1f20 | 2021-06-23 16:59:05 +0200 | [diff] [blame] | 1958 | err = mutex_lock_killable(&loop_ctl_mutex); |
| 1959 | if (err) |
| 1960 | goto out_free_dev; |
| 1961 | |
Tejun Heo | c718aa6 | 2013-02-27 17:03:58 -0800 | [diff] [blame] | 1962 | /* allocate id, if @id >= 0, we're requesting that specific id */ |
Kay Sievers | 34dd82a | 2011-07-31 22:08:04 +0200 | [diff] [blame] | 1963 | if (i >= 0) { |
Tejun Heo | c718aa6 | 2013-02-27 17:03:58 -0800 | [diff] [blame] | 1964 | err = idr_alloc(&loop_index_idr, lo, i, i + 1, GFP_KERNEL); |
| 1965 | if (err == -ENOSPC) |
Kay Sievers | 34dd82a | 2011-07-31 22:08:04 +0200 | [diff] [blame] | 1966 | err = -EEXIST; |
Kay Sievers | 34dd82a | 2011-07-31 22:08:04 +0200 | [diff] [blame] | 1967 | } else { |
Tejun Heo | c718aa6 | 2013-02-27 17:03:58 -0800 | [diff] [blame] | 1968 | err = idr_alloc(&loop_index_idr, lo, 0, 0, GFP_KERNEL); |
Kay Sievers | 34dd82a | 2011-07-31 22:08:04 +0200 | [diff] [blame] | 1969 | } |
Tetsuo Handa | 1c500ad | 2021-09-02 09:07:35 +0900 | [diff] [blame] | 1970 | mutex_unlock(&loop_ctl_mutex); |
Kay Sievers | 34dd82a | 2011-07-31 22:08:04 +0200 | [diff] [blame] | 1971 | if (err < 0) |
Tetsuo Handa | 1c500ad | 2021-09-02 09:07:35 +0900 | [diff] [blame] | 1972 | goto out_free_dev; |
Tejun Heo | c718aa6 | 2013-02-27 17:03:58 -0800 | [diff] [blame] | 1973 | i = err; |
Ken Chen | 7328508 | 2007-05-08 00:28:20 -0700 | [diff] [blame] | 1974 | |
Ming Lei | b5dd2f6 | 2014-12-31 13:22:57 +0000 | [diff] [blame] | 1975 | lo->tag_set.ops = &loop_mq_ops; |
| 1976 | lo->tag_set.nr_hw_queues = 1; |
| 1977 | lo->tag_set.queue_depth = 128; |
| 1978 | lo->tag_set.numa_node = NUMA_NO_NODE; |
| 1979 | lo->tag_set.cmd_size = sizeof(struct loop_cmd); |
Bart Van Assche | 2112f5c | 2021-08-05 10:42:00 -0700 | [diff] [blame] | 1980 | lo->tag_set.flags = BLK_MQ_F_SHOULD_MERGE | BLK_MQ_F_STACKING | |
| 1981 | BLK_MQ_F_NO_SCHED_BY_DEFAULT; |
Ming Lei | b5dd2f6 | 2014-12-31 13:22:57 +0000 | [diff] [blame] | 1982 | lo->tag_set.driver_data = lo; |
| 1983 | |
| 1984 | err = blk_mq_alloc_tag_set(&lo->tag_set); |
| 1985 | if (err) |
Mikulas Patocka | 3ec981e | 2013-10-14 12:12:24 -0400 | [diff] [blame] | 1986 | goto out_free_idr; |
Ken Chen | 7328508 | 2007-05-08 00:28:20 -0700 | [diff] [blame] | 1987 | |
Christoph Hellwig | 1c99502 | 2021-06-02 09:53:33 +0300 | [diff] [blame] | 1988 | disk = lo->lo_disk = blk_mq_alloc_disk(&lo->tag_set, lo); |
| 1989 | if (IS_ERR(disk)) { |
| 1990 | err = PTR_ERR(disk); |
Ming Lei | b5dd2f6 | 2014-12-31 13:22:57 +0000 | [diff] [blame] | 1991 | goto out_cleanup_tags; |
| 1992 | } |
Christoph Hellwig | 1c99502 | 2021-06-02 09:53:33 +0300 | [diff] [blame] | 1993 | lo->lo_queue = lo->lo_disk->queue; |
Mikulas Patocka | ef7e7c8 | 2013-10-15 14:14:38 -0600 | [diff] [blame] | 1994 | |
Shaohua Li | 54bb0ad | 2017-08-31 22:09:45 -0700 | [diff] [blame] | 1995 | blk_queue_max_hw_sectors(lo->lo_queue, BLK_DEF_MAX_SECTORS); |
Shaohua Li | 40326d8 | 2017-08-31 22:09:46 -0700 | [diff] [blame] | 1996 | |
Ming Lei | 5b5e20f | 2015-08-17 10:31:47 +0800 | [diff] [blame] | 1997 | /* |
Shaohua Li | 40326d8 | 2017-08-31 22:09:46 -0700 | [diff] [blame] | 1998 | * By default, we do buffer IO, so it doesn't make sense to enable |
| 1999 | * merge because the I/O submitted to backing file is handled page by |
| 2000 | * page. For directio mode, merge does help to dispatch bigger request |
| 2001 | * to underlayer disk. We will enable merge once directio is enabled. |
Ming Lei | 5b5e20f | 2015-08-17 10:31:47 +0800 | [diff] [blame] | 2002 | */ |
Bart Van Assche | 8b904b5 | 2018-03-07 17:10:10 -0800 | [diff] [blame] | 2003 | blk_queue_flag_set(QUEUE_FLAG_NOMERGES, lo->lo_queue); |
Ming Lei | 5b5e20f | 2015-08-17 10:31:47 +0800 | [diff] [blame] | 2004 | |
Kay Sievers | e03c8dd | 2011-08-23 20:12:04 +0200 | [diff] [blame] | 2005 | /* |
| 2006 | * Disable partition scanning by default. The in-kernel partition |
| 2007 | * scanning can be requested individually per-device during its |
| 2008 | * setup. Userspace can always add and remove partitions from all |
| 2009 | * devices. The needed partition minors are allocated from the |
| 2010 | * extended minor space, the main loop device numbers will continue |
| 2011 | * to match the loop minors, regardless of the number of partitions |
| 2012 | * used. |
| 2013 | * |
| 2014 | * If max_part is given, partition scanning is globally enabled for |
| 2015 | * all loop devices. The minors for the main loop devices will be |
| 2016 | * multiples of max_part. |
| 2017 | * |
| 2018 | * Note: Global-for-all-devices, set-only-at-init, read-only module |
| 2019 | * parameteters like 'max_loop' and 'max_part' make things needlessly |
| 2020 | * complicated, are too static, inflexible and may surprise |
| 2021 | * userspace tools. Parameters like this in general should be avoided. |
| 2022 | */ |
| 2023 | if (!part_shift) |
Christoph Hellwig | 46e7eac | 2021-11-22 14:06:17 +0100 | [diff] [blame] | 2024 | disk->flags |= GENHD_FL_NO_PART; |
Ming Lei | f893366 | 2015-05-06 12:26:23 +0800 | [diff] [blame] | 2025 | atomic_set(&lo->lo_refcnt, 0); |
Pavel Tatashin | 6cc8e74 | 2021-01-26 09:46:30 -0500 | [diff] [blame] | 2026 | mutex_init(&lo->lo_mutex); |
Ken Chen | 7328508 | 2007-05-08 00:28:20 -0700 | [diff] [blame] | 2027 | lo->lo_number = i; |
Ken Chen | 7328508 | 2007-05-08 00:28:20 -0700 | [diff] [blame] | 2028 | spin_lock_init(&lo->lo_lock); |
Dan Schatzberg | 87579e9 | 2021-06-28 19:38:15 -0700 | [diff] [blame] | 2029 | spin_lock_init(&lo->lo_work_lock); |
Ken Chen | 7328508 | 2007-05-08 00:28:20 -0700 | [diff] [blame] | 2030 | disk->major = LOOP_MAJOR; |
Laurent Vivier | 476a481 | 2008-03-26 12:11:53 +0100 | [diff] [blame] | 2031 | disk->first_minor = i << part_shift; |
Christoph Hellwig | 1c99502 | 2021-06-02 09:53:33 +0300 | [diff] [blame] | 2032 | disk->minors = 1 << part_shift; |
Ken Chen | 7328508 | 2007-05-08 00:28:20 -0700 | [diff] [blame] | 2033 | disk->fops = &lo_fops; |
| 2034 | disk->private_data = lo; |
| 2035 | disk->queue = lo->lo_queue; |
Matteo Croce | 9f65c48 | 2021-07-13 01:05:30 +0200 | [diff] [blame] | 2036 | disk->events = DISK_EVENT_MEDIA_CHANGE; |
| 2037 | disk->event_flags = DISK_EVENT_FLAG_UEVENT; |
Ken Chen | 7328508 | 2007-05-08 00:28:20 -0700 | [diff] [blame] | 2038 | sprintf(disk->disk_name, "loop%d", i); |
Tetsuo Handa | 1c500ad | 2021-09-02 09:07:35 +0900 | [diff] [blame] | 2039 | /* Make this loop device reachable from pathname. */ |
Luis Chamberlain | 905705f | 2021-09-27 14:59:57 -0700 | [diff] [blame] | 2040 | err = add_disk(disk); |
| 2041 | if (err) |
| 2042 | goto out_cleanup_disk; |
| 2043 | |
Tetsuo Handa | 1c500ad | 2021-09-02 09:07:35 +0900 | [diff] [blame] | 2044 | /* Show this loop device. */ |
| 2045 | mutex_lock(&loop_ctl_mutex); |
| 2046 | lo->idr_visible = true; |
Christoph Hellwig | 18d1f20 | 2021-06-23 16:59:05 +0200 | [diff] [blame] | 2047 | mutex_unlock(&loop_ctl_mutex); |
Luis Chamberlain | 905705f | 2021-09-27 14:59:57 -0700 | [diff] [blame] | 2048 | |
Christoph Hellwig | 18d1f20 | 2021-06-23 16:59:05 +0200 | [diff] [blame] | 2049 | return i; |
Ken Chen | 7328508 | 2007-05-08 00:28:20 -0700 | [diff] [blame] | 2050 | |
Luis Chamberlain | 905705f | 2021-09-27 14:59:57 -0700 | [diff] [blame] | 2051 | out_cleanup_disk: |
| 2052 | blk_cleanup_disk(disk); |
Ming Lei | b5dd2f6 | 2014-12-31 13:22:57 +0000 | [diff] [blame] | 2053 | out_cleanup_tags: |
| 2054 | blk_mq_free_tag_set(&lo->tag_set); |
Mikulas Patocka | 3ec981e | 2013-10-14 12:12:24 -0400 | [diff] [blame] | 2055 | out_free_idr: |
Tetsuo Handa | 1c500ad | 2021-09-02 09:07:35 +0900 | [diff] [blame] | 2056 | mutex_lock(&loop_ctl_mutex); |
Mikulas Patocka | 3ec981e | 2013-10-14 12:12:24 -0400 | [diff] [blame] | 2057 | idr_remove(&loop_index_idr, i); |
Christoph Hellwig | 18d1f20 | 2021-06-23 16:59:05 +0200 | [diff] [blame] | 2058 | mutex_unlock(&loop_ctl_mutex); |
Ken Chen | 7328508 | 2007-05-08 00:28:20 -0700 | [diff] [blame] | 2059 | out_free_dev: |
| 2060 | kfree(lo); |
| 2061 | out: |
Kay Sievers | 34dd82a | 2011-07-31 22:08:04 +0200 | [diff] [blame] | 2062 | return err; |
Ken Chen | 7328508 | 2007-05-08 00:28:20 -0700 | [diff] [blame] | 2063 | } |
| 2064 | |
Kay Sievers | 34dd82a | 2011-07-31 22:08:04 +0200 | [diff] [blame] | 2065 | static void loop_remove(struct loop_device *lo) |
Ken Chen | 7328508 | 2007-05-08 00:28:20 -0700 | [diff] [blame] | 2066 | { |
Tetsuo Handa | 1c500ad | 2021-09-02 09:07:35 +0900 | [diff] [blame] | 2067 | /* Make this loop device unreachable from pathname. */ |
NeilBrown | 6cd18e7 | 2015-04-27 14:12:22 +1000 | [diff] [blame] | 2068 | del_gendisk(lo->lo_disk); |
Christoph Hellwig | 1c99502 | 2021-06-02 09:53:33 +0300 | [diff] [blame] | 2069 | blk_cleanup_disk(lo->lo_disk); |
Ming Lei | b5dd2f6 | 2014-12-31 13:22:57 +0000 | [diff] [blame] | 2070 | blk_mq_free_tag_set(&lo->tag_set); |
Tetsuo Handa | 1c500ad | 2021-09-02 09:07:35 +0900 | [diff] [blame] | 2071 | mutex_lock(&loop_ctl_mutex); |
| 2072 | idr_remove(&loop_index_idr, lo->lo_number); |
| 2073 | mutex_unlock(&loop_ctl_mutex); |
| 2074 | /* There is no route which can find this loop device. */ |
Pavel Tatashin | 6cc8e74 | 2021-01-26 09:46:30 -0500 | [diff] [blame] | 2075 | mutex_destroy(&lo->lo_mutex); |
Ken Chen | 7328508 | 2007-05-08 00:28:20 -0700 | [diff] [blame] | 2076 | kfree(lo); |
| 2077 | } |
| 2078 | |
Christoph Hellwig | 8410d38 | 2020-10-29 15:58:33 +0100 | [diff] [blame] | 2079 | static void loop_probe(dev_t dev) |
Ken Chen | 7328508 | 2007-05-08 00:28:20 -0700 | [diff] [blame] | 2080 | { |
Christoph Hellwig | 8410d38 | 2020-10-29 15:58:33 +0100 | [diff] [blame] | 2081 | int idx = MINOR(dev) >> part_shift; |
Christoph Hellwig | 8410d38 | 2020-10-29 15:58:33 +0100 | [diff] [blame] | 2082 | |
| 2083 | if (max_loop && idx >= max_loop) |
| 2084 | return; |
Christoph Hellwig | 4157fe0 | 2021-06-23 16:59:03 +0200 | [diff] [blame] | 2085 | loop_add(idx); |
Christoph Hellwig | f9d1076 | 2021-06-23 16:59:04 +0200 | [diff] [blame] | 2086 | } |
| 2087 | |
| 2088 | static int loop_control_remove(int idx) |
| 2089 | { |
| 2090 | struct loop_device *lo; |
| 2091 | int ret; |
Christoph Hellwig | e5d66a10 | 2021-06-23 16:59:06 +0200 | [diff] [blame] | 2092 | |
| 2093 | if (idx < 0) { |
Tetsuo Handa | e3f9387 | 2021-11-29 19:00:43 +0900 | [diff] [blame] | 2094 | pr_warn_once("deleting an unspecified loop device is not supported.\n"); |
Christoph Hellwig | e5d66a10 | 2021-06-23 16:59:06 +0200 | [diff] [blame] | 2095 | return -EINVAL; |
| 2096 | } |
Christoph Hellwig | f9d1076 | 2021-06-23 16:59:04 +0200 | [diff] [blame] | 2097 | |
Tetsuo Handa | 1c500ad | 2021-09-02 09:07:35 +0900 | [diff] [blame] | 2098 | /* Hide this loop device for serialization. */ |
Christoph Hellwig | f9d1076 | 2021-06-23 16:59:04 +0200 | [diff] [blame] | 2099 | ret = mutex_lock_killable(&loop_ctl_mutex); |
| 2100 | if (ret) |
| 2101 | return ret; |
Christoph Hellwig | b984808 | 2021-06-23 16:59:07 +0200 | [diff] [blame] | 2102 | lo = idr_find(&loop_index_idr, idx); |
Tetsuo Handa | 1c500ad | 2021-09-02 09:07:35 +0900 | [diff] [blame] | 2103 | if (!lo || !lo->idr_visible) |
Christoph Hellwig | b984808 | 2021-06-23 16:59:07 +0200 | [diff] [blame] | 2104 | ret = -ENODEV; |
Tetsuo Handa | 1c500ad | 2021-09-02 09:07:35 +0900 | [diff] [blame] | 2105 | else |
| 2106 | lo->idr_visible = false; |
| 2107 | mutex_unlock(&loop_ctl_mutex); |
| 2108 | if (ret) |
| 2109 | return ret; |
Christoph Hellwig | f9d1076 | 2021-06-23 16:59:04 +0200 | [diff] [blame] | 2110 | |
Tetsuo Handa | 1c500ad | 2021-09-02 09:07:35 +0900 | [diff] [blame] | 2111 | /* Check whether this loop device can be removed. */ |
Christoph Hellwig | f9d1076 | 2021-06-23 16:59:04 +0200 | [diff] [blame] | 2112 | ret = mutex_lock_killable(&lo->lo_mutex); |
| 2113 | if (ret) |
Tetsuo Handa | 1c500ad | 2021-09-02 09:07:35 +0900 | [diff] [blame] | 2114 | goto mark_visible; |
Christoph Hellwig | f9d1076 | 2021-06-23 16:59:04 +0200 | [diff] [blame] | 2115 | if (lo->lo_state != Lo_unbound || |
| 2116 | atomic_read(&lo->lo_refcnt) > 0) { |
| 2117 | mutex_unlock(&lo->lo_mutex); |
| 2118 | ret = -EBUSY; |
Tetsuo Handa | 1c500ad | 2021-09-02 09:07:35 +0900 | [diff] [blame] | 2119 | goto mark_visible; |
Christoph Hellwig | f9d1076 | 2021-06-23 16:59:04 +0200 | [diff] [blame] | 2120 | } |
Tetsuo Handa | 1c500ad | 2021-09-02 09:07:35 +0900 | [diff] [blame] | 2121 | /* Mark this loop device no longer open()-able. */ |
Christoph Hellwig | f9d1076 | 2021-06-23 16:59:04 +0200 | [diff] [blame] | 2122 | lo->lo_state = Lo_deleting; |
| 2123 | mutex_unlock(&lo->lo_mutex); |
| 2124 | |
Christoph Hellwig | f9d1076 | 2021-06-23 16:59:04 +0200 | [diff] [blame] | 2125 | loop_remove(lo); |
Tetsuo Handa | 1c500ad | 2021-09-02 09:07:35 +0900 | [diff] [blame] | 2126 | return 0; |
| 2127 | |
| 2128 | mark_visible: |
| 2129 | /* Show this loop device again. */ |
| 2130 | mutex_lock(&loop_ctl_mutex); |
| 2131 | lo->idr_visible = true; |
Christoph Hellwig | f9d1076 | 2021-06-23 16:59:04 +0200 | [diff] [blame] | 2132 | mutex_unlock(&loop_ctl_mutex); |
| 2133 | return ret; |
| 2134 | } |
| 2135 | |
| 2136 | static int loop_control_get_free(int idx) |
Kay Sievers | 770fe30 | 2011-07-31 22:08:04 +0200 | [diff] [blame] | 2137 | { |
| 2138 | struct loop_device *lo; |
Christoph Hellwig | b984808 | 2021-06-23 16:59:07 +0200 | [diff] [blame] | 2139 | int id, ret; |
Kay Sievers | 770fe30 | 2011-07-31 22:08:04 +0200 | [diff] [blame] | 2140 | |
Jan Kara | 0a42e99 | 2018-11-08 14:01:04 +0100 | [diff] [blame] | 2141 | ret = mutex_lock_killable(&loop_ctl_mutex); |
| 2142 | if (ret) |
| 2143 | return ret; |
Christoph Hellwig | b984808 | 2021-06-23 16:59:07 +0200 | [diff] [blame] | 2144 | idr_for_each_entry(&loop_index_idr, lo, id) { |
Tetsuo Handa | 1c500ad | 2021-09-02 09:07:35 +0900 | [diff] [blame] | 2145 | /* Hitting a race results in creating a new loop device which is harmless. */ |
| 2146 | if (lo->idr_visible && data_race(lo->lo_state) == Lo_unbound) |
Christoph Hellwig | b984808 | 2021-06-23 16:59:07 +0200 | [diff] [blame] | 2147 | goto found; |
| 2148 | } |
Jan Kara | 0a42e99 | 2018-11-08 14:01:04 +0100 | [diff] [blame] | 2149 | mutex_unlock(&loop_ctl_mutex); |
Christoph Hellwig | 18d1f20 | 2021-06-23 16:59:05 +0200 | [diff] [blame] | 2150 | return loop_add(-1); |
Christoph Hellwig | b984808 | 2021-06-23 16:59:07 +0200 | [diff] [blame] | 2151 | found: |
| 2152 | mutex_unlock(&loop_ctl_mutex); |
| 2153 | return id; |
Kay Sievers | 770fe30 | 2011-07-31 22:08:04 +0200 | [diff] [blame] | 2154 | } |
| 2155 | |
Christoph Hellwig | f9d1076 | 2021-06-23 16:59:04 +0200 | [diff] [blame] | 2156 | static long loop_control_ioctl(struct file *file, unsigned int cmd, |
| 2157 | unsigned long parm) |
| 2158 | { |
| 2159 | switch (cmd) { |
| 2160 | case LOOP_CTL_ADD: |
Christoph Hellwig | 18d1f20 | 2021-06-23 16:59:05 +0200 | [diff] [blame] | 2161 | return loop_add(parm); |
Christoph Hellwig | f9d1076 | 2021-06-23 16:59:04 +0200 | [diff] [blame] | 2162 | case LOOP_CTL_REMOVE: |
| 2163 | return loop_control_remove(parm); |
| 2164 | case LOOP_CTL_GET_FREE: |
| 2165 | return loop_control_get_free(parm); |
| 2166 | default: |
| 2167 | return -ENOSYS; |
| 2168 | } |
| 2169 | } |
| 2170 | |
Kay Sievers | 770fe30 | 2011-07-31 22:08:04 +0200 | [diff] [blame] | 2171 | static const struct file_operations loop_ctl_fops = { |
| 2172 | .open = nonseekable_open, |
| 2173 | .unlocked_ioctl = loop_control_ioctl, |
| 2174 | .compat_ioctl = loop_control_ioctl, |
| 2175 | .owner = THIS_MODULE, |
| 2176 | .llseek = noop_llseek, |
| 2177 | }; |
| 2178 | |
| 2179 | static struct miscdevice loop_misc = { |
| 2180 | .minor = LOOP_CTRL_MINOR, |
| 2181 | .name = "loop-control", |
| 2182 | .fops = &loop_ctl_fops, |
| 2183 | }; |
| 2184 | |
| 2185 | MODULE_ALIAS_MISCDEV(LOOP_CTRL_MINOR); |
| 2186 | MODULE_ALIAS("devname:loop-control"); |
| 2187 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2188 | static int __init loop_init(void) |
| 2189 | { |
Ken Chen | a47653f | 2007-06-08 13:46:44 -0700 | [diff] [blame] | 2190 | int i, nr; |
Kay Sievers | 770fe30 | 2011-07-31 22:08:04 +0200 | [diff] [blame] | 2191 | int err; |
Ken Chen | a47653f | 2007-06-08 13:46:44 -0700 | [diff] [blame] | 2192 | |
Laurent Vivier | 476a481 | 2008-03-26 12:11:53 +0100 | [diff] [blame] | 2193 | part_shift = 0; |
Namhyung Kim | ac04fee | 2011-05-27 07:59:25 +0200 | [diff] [blame] | 2194 | if (max_part > 0) { |
Laurent Vivier | 476a481 | 2008-03-26 12:11:53 +0100 | [diff] [blame] | 2195 | part_shift = fls(max_part); |
| 2196 | |
Namhyung Kim | ac04fee | 2011-05-27 07:59:25 +0200 | [diff] [blame] | 2197 | /* |
| 2198 | * Adjust max_part according to part_shift as it is exported |
| 2199 | * to user space so that user can decide correct minor number |
| 2200 | * if [s]he want to create more devices. |
| 2201 | * |
| 2202 | * Note that -1 is required because partition 0 is reserved |
| 2203 | * for the whole disk. |
| 2204 | */ |
| 2205 | max_part = (1UL << part_shift) - 1; |
| 2206 | } |
| 2207 | |
Guo Chao | b1a6650 | 2013-02-21 15:16:49 -0800 | [diff] [blame] | 2208 | if ((1UL << part_shift) > DISK_MAX_PARTS) { |
| 2209 | err = -EINVAL; |
Anton Volkov | a8c1d06 | 2017-08-07 15:37:50 +0300 | [diff] [blame] | 2210 | goto err_out; |
Guo Chao | b1a6650 | 2013-02-21 15:16:49 -0800 | [diff] [blame] | 2211 | } |
Namhyung Kim | 78f4bb3 | 2011-05-24 16:48:54 +0200 | [diff] [blame] | 2212 | |
Guo Chao | b1a6650 | 2013-02-21 15:16:49 -0800 | [diff] [blame] | 2213 | if (max_loop > 1UL << (MINORBITS - part_shift)) { |
| 2214 | err = -EINVAL; |
Anton Volkov | a8c1d06 | 2017-08-07 15:37:50 +0300 | [diff] [blame] | 2215 | goto err_out; |
Guo Chao | b1a6650 | 2013-02-21 15:16:49 -0800 | [diff] [blame] | 2216 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2217 | |
Kay Sievers | d134b00 | 2011-07-31 22:08:04 +0200 | [diff] [blame] | 2218 | /* |
| 2219 | * If max_loop is specified, create that many devices upfront. |
| 2220 | * This also becomes a hard limit. If max_loop is not specified, |
| 2221 | * create CONFIG_BLK_DEV_LOOP_MIN_COUNT loop devices at module |
| 2222 | * init time. Loop devices can be requested on-demand with the |
| 2223 | * /dev/loop-control interface, or be instantiated by accessing |
| 2224 | * a 'dead' device node. |
| 2225 | */ |
Lukas Bulwahn | aeb2b0b | 2020-12-12 06:13:02 +0100 | [diff] [blame] | 2226 | if (max_loop) |
Ken Chen | a47653f | 2007-06-08 13:46:44 -0700 | [diff] [blame] | 2227 | nr = max_loop; |
Lukas Bulwahn | aeb2b0b | 2020-12-12 06:13:02 +0100 | [diff] [blame] | 2228 | else |
Kay Sievers | d134b00 | 2011-07-31 22:08:04 +0200 | [diff] [blame] | 2229 | nr = CONFIG_BLK_DEV_LOOP_MIN_COUNT; |
Ken Chen | a47653f | 2007-06-08 13:46:44 -0700 | [diff] [blame] | 2230 | |
Anton Volkov | a8c1d06 | 2017-08-07 15:37:50 +0300 | [diff] [blame] | 2231 | err = misc_register(&loop_misc); |
| 2232 | if (err < 0) |
| 2233 | goto err_out; |
| 2234 | |
| 2235 | |
Christoph Hellwig | 8410d38 | 2020-10-29 15:58:33 +0100 | [diff] [blame] | 2236 | if (__register_blkdev(LOOP_MAJOR, "loop", loop_probe)) { |
Guo Chao | b1a6650 | 2013-02-21 15:16:49 -0800 | [diff] [blame] | 2237 | err = -EIO; |
| 2238 | goto misc_out; |
| 2239 | } |
Ken Chen | a47653f | 2007-06-08 13:46:44 -0700 | [diff] [blame] | 2240 | |
Kay Sievers | d134b00 | 2011-07-31 22:08:04 +0200 | [diff] [blame] | 2241 | /* pre-create number of devices given by config or max_loop */ |
Kay Sievers | 34dd82a | 2011-07-31 22:08:04 +0200 | [diff] [blame] | 2242 | for (i = 0; i < nr; i++) |
Christoph Hellwig | d6da83d | 2021-06-23 16:59:02 +0200 | [diff] [blame] | 2243 | loop_add(i); |
Kay Sievers | 34dd82a | 2011-07-31 22:08:04 +0200 | [diff] [blame] | 2244 | |
Ken Chen | 7328508 | 2007-05-08 00:28:20 -0700 | [diff] [blame] | 2245 | printk(KERN_INFO "loop: module loaded\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2246 | return 0; |
Guo Chao | b1a6650 | 2013-02-21 15:16:49 -0800 | [diff] [blame] | 2247 | |
| 2248 | misc_out: |
| 2249 | misc_deregister(&loop_misc); |
Anton Volkov | a8c1d06 | 2017-08-07 15:37:50 +0300 | [diff] [blame] | 2250 | err_out: |
Guo Chao | b1a6650 | 2013-02-21 15:16:49 -0800 | [diff] [blame] | 2251 | return err; |
Kay Sievers | 34dd82a | 2011-07-31 22:08:04 +0200 | [diff] [blame] | 2252 | } |
Ken Chen | a47653f | 2007-06-08 13:46:44 -0700 | [diff] [blame] | 2253 | |
Ken Chen | 7328508 | 2007-05-08 00:28:20 -0700 | [diff] [blame] | 2254 | static void __exit loop_exit(void) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2255 | { |
Christoph Hellwig | 8e60947 | 2021-06-23 16:59:08 +0200 | [diff] [blame] | 2256 | struct loop_device *lo; |
| 2257 | int id; |
| 2258 | |
Christoph Hellwig | 8b52d8b | 2021-06-23 16:59:00 +0200 | [diff] [blame] | 2259 | unregister_blkdev(LOOP_MAJOR, "loop"); |
| 2260 | misc_deregister(&loop_misc); |
Luis Chamberlain | 200f933 | 2020-06-19 20:47:27 +0000 | [diff] [blame] | 2261 | |
Tetsuo Handa | 1c500ad | 2021-09-02 09:07:35 +0900 | [diff] [blame] | 2262 | /* |
| 2263 | * There is no need to use loop_ctl_mutex here, for nobody else can |
| 2264 | * access loop_index_idr when this module is unloading (unless forced |
| 2265 | * module unloading is requested). If this is not a clean unloading, |
| 2266 | * we have no means to avoid kernel crash. |
| 2267 | */ |
Christoph Hellwig | 8e60947 | 2021-06-23 16:59:08 +0200 | [diff] [blame] | 2268 | idr_for_each_entry(&loop_index_idr, lo, id) |
| 2269 | loop_remove(lo); |
Christoph Hellwig | bd5c39e | 2021-06-23 16:59:01 +0200 | [diff] [blame] | 2270 | |
| 2271 | idr_destroy(&loop_index_idr); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2272 | } |
| 2273 | |
| 2274 | module_init(loop_init); |
| 2275 | module_exit(loop_exit); |
| 2276 | |
| 2277 | #ifndef MODULE |
| 2278 | static int __init max_loop_setup(char *str) |
| 2279 | { |
| 2280 | max_loop = simple_strtol(str, NULL, 0); |
| 2281 | return 1; |
| 2282 | } |
| 2283 | |
| 2284 | __setup("max_loop=", max_loop_setup); |
| 2285 | #endif |