blob: fa1c298a8cfbb87c7d66bf4117de7f84c9b0b42c [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
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 Engelhardt96de0e22007-10-19 23:21:04 +020032 * Erik I. Bolsø, <eriki@himolde.no>, Oct 31, 1999
Linus Torvalds1da177e2005-04-16 15:20:36 -070033 *
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 Piggin4e02ed42008-10-29 14:00:55 -070043 * operations write_begin is not available on the backing filesystem.
Linus Torvalds1da177e2005-04-16 15:20:36 -070044 * 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 Torvalds1da177e2005-04-16 15:20:36 -070052#include <linux/module.h>
53#include <linux/moduleparam.h>
54#include <linux/sched.h>
55#include <linux/fs.h>
Matthew Wilcox (Oracle)4ee60ec2021-05-06 18:02:27 -070056#include <linux/pagemap.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070057#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 Torvalds1da177e2005-04-16 15:20:36 -070065#include <linux/swap.h>
66#include <linux/slab.h>
David Howells863d5b822006-08-29 19:06:14 +010067#include <linux/compat.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070068#include <linux/suspend.h>
Rafael J. Wysocki83144182007-07-17 04:03:35 -070069#include <linux/freezer.h>
Arnd Bergmann2a48fc02010-06-02 14:28:52 +020070#include <linux/mutex.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070071#include <linux/writeback.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070072#include <linux/completion.h>
73#include <linux/highmem.h>
Jens Axboed6b29d72007-06-04 09:59:47 +020074#include <linux/splice.h>
Milan Brozee862732010-08-23 15:16:00 +020075#include <linux/sysfs.h>
Kay Sievers770fe302011-07-31 22:08:04 +020076#include <linux/miscdevice.h>
Lukas Czernerdfaa2ef2011-08-19 14:50:46 +020077#include <linux/falloc.h>
Al Viro283e7e52015-04-03 15:21:59 -040078#include <linux/uio.h>
Adam Manzanaresd9a08a92018-05-22 10:52:19 -070079#include <linux/ioprio.h>
Dennis Zhoudb6638d2018-12-05 12:10:35 -050080#include <linux/blk-cgroup.h>
Dan Schatzbergc74d40e2021-06-28 19:38:21 -070081#include <linux/sched/mm.h>
Adam Manzanaresd9a08a92018-05-22 10:52:19 -070082
Al Viro83a87612013-05-12 10:14:07 -040083#include "loop.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070084
Linus Torvalds7c0f6ba2016-12-24 11:46:01 -080085#include <linux/uaccess.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070086
Dan Schatzberg87579e92021-06-28 19:38:15 -070087#define LOOP_IDLE_WORKER_TIMEOUT (60 * HZ)
88
Kay Sievers34dd82a2011-07-31 22:08:04 +020089static DEFINE_IDR(loop_index_idr);
Tetsuo Handa310ca162018-11-08 14:01:02 +010090static DEFINE_MUTEX(loop_ctl_mutex);
Tetsuo Handa3ce6e1f2021-07-06 23:40:34 +090091static 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 */
105static 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 */
126static 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 Torvalds1da177e2005-04-16 15:20:36 -0700132
Laurent Vivier476a4812008-03-26 12:11:53 +0100133static int max_part;
134static int part_shift;
135
Linus Torvalds1da177e2005-04-16 15:20:36 -0700136static int transfer_xor(struct loop_device *lo, int cmd,
137 struct page *raw_page, unsigned raw_off,
138 struct page *loop_page, unsigned loop_off,
139 int size, sector_t real_block)
140{
Cong Wangcfd80052011-11-25 23:14:18 +0800141 char *raw_buf = kmap_atomic(raw_page) + raw_off;
142 char *loop_buf = kmap_atomic(loop_page) + loop_off;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700143 char *in, *out, *key;
144 int i, keysize;
145
146 if (cmd == READ) {
147 in = raw_buf;
148 out = loop_buf;
149 } else {
150 in = loop_buf;
151 out = raw_buf;
152 }
153
154 key = lo->lo_encrypt_key;
155 keysize = lo->lo_encrypt_key_size;
156 for (i = 0; i < size; i++)
157 *out++ = *in++ ^ key[(i & 511) % keysize];
158
Cong Wangcfd80052011-11-25 23:14:18 +0800159 kunmap_atomic(loop_buf);
160 kunmap_atomic(raw_buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700161 cond_resched();
162 return 0;
163}
164
165static int xor_init(struct loop_device *lo, const struct loop_info64 *info)
166{
167 if (unlikely(info->lo_encrypt_key_size <= 0))
168 return -EINVAL;
169 return 0;
170}
171
172static struct loop_func_table none_funcs = {
173 .number = LO_CRYPT_NONE,
Christoph Hellwigaa4d8612015-04-07 18:23:29 +0200174};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700175
176static struct loop_func_table xor_funcs = {
177 .number = LO_CRYPT_XOR,
178 .transfer = transfer_xor,
179 .init = xor_init
Christoph Hellwigaa4d8612015-04-07 18:23:29 +0200180};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700181
182/* xfer_funcs[0] is special - its release function is never called */
183static struct loop_func_table *xfer_funcs[MAX_LO_CRYPT] = {
184 &none_funcs,
185 &xor_funcs
186};
187
Dmitry Monakhov7035b5d2011-11-16 09:21:49 +0100188static loff_t get_size(loff_t offset, loff_t sizelimit, struct file *file)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700189{
Guo Chaob7a1da62013-02-21 15:16:50 -0800190 loff_t loopsize;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700191
192 /* Compute loopsize in bytes */
Guo Chaob7a1da62013-02-21 15:16:50 -0800193 loopsize = i_size_read(file->f_mapping->host);
194 if (offset > 0)
195 loopsize -= offset;
196 /* offset is beyond i_size, weird but possible */
Dmitry Monakhov7035b5d2011-11-16 09:21:49 +0100197 if (loopsize < 0)
198 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700199
Dmitry Monakhov7035b5d2011-11-16 09:21:49 +0100200 if (sizelimit > 0 && sizelimit < loopsize)
201 loopsize = sizelimit;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700202 /*
203 * Unfortunately, if we want to do I/O on the device,
204 * the number of 512-byte sectors has to fit into a sector_t.
205 */
206 return loopsize >> 9;
207}
208
Dmitry Monakhov7035b5d2011-11-16 09:21:49 +0100209static loff_t get_loop_size(struct loop_device *lo, struct file *file)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700210{
Dmitry Monakhov7035b5d2011-11-16 09:21:49 +0100211 return get_size(lo->lo_offset, lo->lo_sizelimit, file);
212}
213
Ming Lei2e5ab5f2015-08-17 10:31:49 +0800214static void __loop_update_dio(struct loop_device *lo, bool dio)
215{
216 struct file *file = lo->lo_backing_file;
217 struct address_space *mapping = file->f_mapping;
218 struct inode *inode = mapping->host;
219 unsigned short sb_bsize = 0;
220 unsigned dio_align = 0;
221 bool use_dio;
222
223 if (inode->i_sb->s_bdev) {
224 sb_bsize = bdev_logical_block_size(inode->i_sb->s_bdev);
225 dio_align = sb_bsize - 1;
226 }
227
228 /*
229 * We support direct I/O only if lo_offset is aligned with the
230 * logical I/O size of backing device, and the logical block
231 * size of loop is bigger than the backing device's and the loop
232 * needn't transform transfer.
233 *
234 * TODO: the above condition may be loosed in the future, and
235 * direct I/O may be switched runtime at that time because most
Masahiro Yamada89d790a2017-02-27 14:29:01 -0800236 * of requests in sane applications should be PAGE_SIZE aligned
Ming Lei2e5ab5f2015-08-17 10:31:49 +0800237 */
238 if (dio) {
239 if (queue_logical_block_size(lo->lo_queue) >= sb_bsize &&
240 !(lo->lo_offset & dio_align) &&
241 mapping->a_ops->direct_IO &&
242 !lo->transfer)
243 use_dio = true;
244 else
245 use_dio = false;
246 } else {
247 use_dio = false;
248 }
249
250 if (lo->use_dio == use_dio)
251 return;
252
253 /* flush dirty pages before changing direct IO */
254 vfs_fsync(file, 0);
255
256 /*
257 * The flag of LO_FLAGS_DIRECT_IO is handled similarly with
258 * LO_FLAGS_READ_ONLY, both are set from kernel, and losetup
259 * will get updated by ioctl(LOOP_GET_STATUS)
260 */
Martijn Coenen0fbcf572020-03-10 14:06:54 +0100261 if (lo->lo_state == Lo_bound)
262 blk_mq_freeze_queue(lo->lo_queue);
Ming Lei2e5ab5f2015-08-17 10:31:49 +0800263 lo->use_dio = use_dio;
Shaohua Li40326d82017-08-31 22:09:46 -0700264 if (use_dio) {
Bart Van Assche8b904b52018-03-07 17:10:10 -0800265 blk_queue_flag_clear(QUEUE_FLAG_NOMERGES, lo->lo_queue);
Ming Lei2e5ab5f2015-08-17 10:31:49 +0800266 lo->lo_flags |= LO_FLAGS_DIRECT_IO;
Shaohua Li40326d82017-08-31 22:09:46 -0700267 } else {
Bart Van Assche8b904b52018-03-07 17:10:10 -0800268 blk_queue_flag_set(QUEUE_FLAG_NOMERGES, lo->lo_queue);
Ming Lei2e5ab5f2015-08-17 10:31:49 +0800269 lo->lo_flags &= ~LO_FLAGS_DIRECT_IO;
Shaohua Li40326d82017-08-31 22:09:46 -0700270 }
Martijn Coenen0fbcf572020-03-10 14:06:54 +0100271 if (lo->lo_state == Lo_bound)
272 blk_mq_unfreeze_queue(lo->lo_queue);
Ming Lei2e5ab5f2015-08-17 10:31:49 +0800273}
274
Martijn Coenen5795b6f2020-05-13 15:38:37 +0200275/**
Martijn Coenen34489142020-05-13 15:38:45 +0200276 * loop_validate_block_size() - validates the passed in block size
277 * @bsize: size to validate
278 */
Dmitry Monakhov7035b5d2011-11-16 09:21:49 +0100279static int
Martijn Coenen34489142020-05-13 15:38:45 +0200280loop_validate_block_size(unsigned short bsize)
Dmitry Monakhov7035b5d2011-11-16 09:21:49 +0100281{
Martijn Coenen34489142020-05-13 15:38:45 +0200282 if (bsize < 512 || bsize > PAGE_SIZE || !is_power_of_2(bsize))
283 return -EINVAL;
284
285 return 0;
286}
287
288/**
Martijn Coenen5795b6f2020-05-13 15:38:37 +0200289 * loop_set_size() - sets device size and notifies userspace
290 * @lo: struct loop_device to set the size for
291 * @size: new size of the loop device
292 *
293 * Callers must validate that the size passed into this function fits into
294 * a sector_t, eg using loop_validate_size()
295 */
296static void loop_set_size(struct loop_device *lo, loff_t size)
297{
Christoph Hellwig449f4ec2020-11-16 15:56:56 +0100298 if (!set_capacity_and_notify(lo->lo_disk, size))
Christoph Hellwig3b4f85d2020-11-16 15:56:53 +0100299 kobject_uevent(&disk_to_dev(lo->lo_disk)->kobj, KOBJ_CHANGE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700300}
301
302static inline int
303lo_do_transfer(struct loop_device *lo, int cmd,
304 struct page *rpage, unsigned roffs,
305 struct page *lpage, unsigned loffs,
306 int size, sector_t rblock)
307{
Christoph Hellwigaa4d8612015-04-07 18:23:29 +0200308 int ret;
309
310 ret = lo->transfer(lo, cmd, rpage, roffs, lpage, loffs, size, rblock);
311 if (likely(!ret))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700312 return 0;
313
Christoph Hellwigaa4d8612015-04-07 18:23:29 +0200314 printk_ratelimited(KERN_ERR
315 "loop: Transfer error at byte offset %llu, length %i.\n",
316 (unsigned long long)rblock << 9, size);
317 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700318}
319
Christoph Hellwigaa4d8612015-04-07 18:23:29 +0200320static int lo_write_bvec(struct file *file, struct bio_vec *bvec, loff_t *ppos)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700321{
Christoph Hellwigaa4d8612015-04-07 18:23:29 +0200322 struct iov_iter i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700323 ssize_t bw;
Al Viro283e7e52015-04-03 15:21:59 -0400324
Christoph Hellwigb6207432019-06-26 15:49:28 +0200325 iov_iter_bvec(&i, WRITE, bvec, 1, bvec->bv_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700326
Al Viro03d95eb2013-03-20 13:04:20 -0400327 file_start_write(file);
Christoph Hellwigabbb65892017-05-27 11:16:52 +0300328 bw = vfs_iter_write(file, &i, ppos, 0);
Al Viro03d95eb2013-03-20 13:04:20 -0400329 file_end_write(file);
Christoph Hellwigaa4d8612015-04-07 18:23:29 +0200330
331 if (likely(bw == bvec->bv_len))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700332 return 0;
Christoph Hellwigaa4d8612015-04-07 18:23:29 +0200333
334 printk_ratelimited(KERN_ERR
335 "loop: Write error at byte offset %llu, length %i.\n",
336 (unsigned long long)*ppos, bvec->bv_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700337 if (bw >= 0)
338 bw = -EIO;
339 return bw;
340}
341
Christoph Hellwigaa4d8612015-04-07 18:23:29 +0200342static int lo_write_simple(struct loop_device *lo, struct request *rq,
343 loff_t pos)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700344{
Christoph Hellwigaa4d8612015-04-07 18:23:29 +0200345 struct bio_vec bvec;
346 struct req_iterator iter;
347 int ret = 0;
348
349 rq_for_each_segment(bvec, rq, iter) {
350 ret = lo_write_bvec(lo->lo_backing_file, &bvec, &pos);
351 if (ret < 0)
352 break;
353 cond_resched();
354 }
355
356 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700357}
358
Christoph Hellwigaa4d8612015-04-07 18:23:29 +0200359/*
Christoph Hellwig456be142011-10-17 12:57:20 +0200360 * This is the slow, transforming version that needs to double buffer the
361 * data as it cannot do the transformations in place without having direct
362 * access to the destination pages of the backing file.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700363 */
Christoph Hellwigaa4d8612015-04-07 18:23:29 +0200364static int lo_write_transfer(struct loop_device *lo, struct request *rq,
365 loff_t pos)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700366{
Christoph Hellwigaa4d8612015-04-07 18:23:29 +0200367 struct bio_vec bvec, b;
Ming Lei30112012014-12-31 13:22:58 +0000368 struct req_iterator iter;
Christoph Hellwigaa4d8612015-04-07 18:23:29 +0200369 struct page *page;
Kent Overstreet79886132013-11-23 17:19:00 -0800370 int ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700371
Christoph Hellwigaa4d8612015-04-07 18:23:29 +0200372 page = alloc_page(GFP_NOIO);
373 if (unlikely(!page))
374 return -ENOMEM;
Christoph Hellwig456be142011-10-17 12:57:20 +0200375
Ming Lei30112012014-12-31 13:22:58 +0000376 rq_for_each_segment(bvec, rq, iter) {
Christoph Hellwigaa4d8612015-04-07 18:23:29 +0200377 ret = lo_do_transfer(lo, WRITE, page, 0, bvec.bv_page,
378 bvec.bv_offset, bvec.bv_len, pos >> 9);
379 if (unlikely(ret))
380 break;
381
382 b.bv_page = page;
383 b.bv_offset = 0;
384 b.bv_len = bvec.bv_len;
385 ret = lo_write_bvec(lo->lo_backing_file, &b, &pos);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700386 if (ret < 0)
387 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700388 }
Christoph Hellwigaa4d8612015-04-07 18:23:29 +0200389
390 __free_page(page);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700391 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700392}
393
Christoph Hellwigaa4d8612015-04-07 18:23:29 +0200394static int lo_read_simple(struct loop_device *lo, struct request *rq,
395 loff_t pos)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700396{
Kent Overstreet79886132013-11-23 17:19:00 -0800397 struct bio_vec bvec;
Ming Lei30112012014-12-31 13:22:58 +0000398 struct req_iterator iter;
Christoph Hellwigaa4d8612015-04-07 18:23:29 +0200399 struct iov_iter i;
400 ssize_t len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700401
Ming Lei30112012014-12-31 13:22:58 +0000402 rq_for_each_segment(bvec, rq, iter) {
Christoph Hellwigb6207432019-06-26 15:49:28 +0200403 iov_iter_bvec(&i, READ, &bvec, 1, bvec.bv_len);
Christoph Hellwig18e97102017-05-27 11:16:51 +0300404 len = vfs_iter_read(lo->lo_backing_file, &i, &pos, 0);
Christoph Hellwigaa4d8612015-04-07 18:23:29 +0200405 if (len < 0)
406 return len;
Dave Young306df072012-02-08 22:07:19 +0100407
Christoph Hellwigaa4d8612015-04-07 18:23:29 +0200408 flush_dcache_page(bvec.bv_page);
409
410 if (len != bvec.bv_len) {
Ming Lei30112012014-12-31 13:22:58 +0000411 struct bio *bio;
412
413 __rq_for_each_bio(bio, rq)
414 zero_fill_bio(bio);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700415 break;
Dave Young306df072012-02-08 22:07:19 +0100416 }
Christoph Hellwigaa4d8612015-04-07 18:23:29 +0200417 cond_resched();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700418 }
Christoph Hellwigaa4d8612015-04-07 18:23:29 +0200419
Dave Young306df072012-02-08 22:07:19 +0100420 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700421}
422
Christoph Hellwigaa4d8612015-04-07 18:23:29 +0200423static int lo_read_transfer(struct loop_device *lo, struct request *rq,
424 loff_t pos)
425{
426 struct bio_vec bvec, b;
427 struct req_iterator iter;
428 struct iov_iter i;
429 struct page *page;
430 ssize_t len;
431 int ret = 0;
432
433 page = alloc_page(GFP_NOIO);
434 if (unlikely(!page))
435 return -ENOMEM;
436
437 rq_for_each_segment(bvec, rq, iter) {
438 loff_t offset = pos;
439
440 b.bv_page = page;
441 b.bv_offset = 0;
442 b.bv_len = bvec.bv_len;
443
Christoph Hellwigb6207432019-06-26 15:49:28 +0200444 iov_iter_bvec(&i, READ, &b, 1, b.bv_len);
Christoph Hellwig18e97102017-05-27 11:16:51 +0300445 len = vfs_iter_read(lo->lo_backing_file, &i, &pos, 0);
Christoph Hellwigaa4d8612015-04-07 18:23:29 +0200446 if (len < 0) {
447 ret = len;
448 goto out_free_page;
449 }
450
451 ret = lo_do_transfer(lo, READ, page, 0, bvec.bv_page,
452 bvec.bv_offset, len, offset >> 9);
453 if (ret)
454 goto out_free_page;
455
456 flush_dcache_page(bvec.bv_page);
457
458 if (len != bvec.bv_len) {
459 struct bio *bio;
460
461 __rq_for_each_bio(bio, rq)
462 zero_fill_bio(bio);
463 break;
464 }
465 }
466
467 ret = 0;
468out_free_page:
469 __free_page(page);
470 return ret;
471}
472
Darrick J. Wongefcfec572019-10-30 20:29:48 -0700473static int lo_fallocate(struct loop_device *lo, struct request *rq, loff_t pos,
474 int mode)
Ming Leicf655d92014-12-31 13:22:59 +0000475{
476 /*
Darrick J. Wongefcfec572019-10-30 20:29:48 -0700477 * We use fallocate to manipulate the space mappings used by the image
478 * a.k.a. discard/zerorange. However we do not support this if
479 * encryption is enabled, because it may give an attacker useful
480 * information.
Ming Leicf655d92014-12-31 13:22:59 +0000481 */
482 struct file *file = lo->lo_backing_file;
Evan Greenc52abf52020-04-03 16:43:04 +0200483 struct request_queue *q = lo->lo_queue;
Ming Leicf655d92014-12-31 13:22:59 +0000484 int ret;
485
Darrick J. Wongefcfec572019-10-30 20:29:48 -0700486 mode |= FALLOC_FL_KEEP_SIZE;
487
Evan Greenc52abf52020-04-03 16:43:04 +0200488 if (!blk_queue_discard(q)) {
Ming Leicf655d92014-12-31 13:22:59 +0000489 ret = -EOPNOTSUPP;
490 goto out;
491 }
492
493 ret = file->f_op->fallocate(file, mode, pos, blk_rq_bytes(rq));
494 if (unlikely(ret && ret != -EINVAL && ret != -EOPNOTSUPP))
495 ret = -EIO;
496 out:
497 return ret;
498}
499
500static int lo_req_flush(struct loop_device *lo, struct request *rq)
501{
502 struct file *file = lo->lo_backing_file;
503 int ret = vfs_fsync(file, 0);
504 if (unlikely(ret && ret != -EINVAL))
505 ret = -EIO;
506
507 return ret;
508}
509
Christoph Hellwigfe2cb292017-04-20 16:03:02 +0200510static void lo_complete_rq(struct request *rq)
Ming Leibc07c102015-08-17 10:31:51 +0800511{
Christoph Hellwigfe2cb292017-04-20 16:03:02 +0200512 struct loop_cmd *cmd = blk_mq_rq_to_pdu(rq);
Jens Axboef9de14b2018-04-13 16:25:57 -0600513 blk_status_t ret = BLK_STS_OK;
Ming Leibc07c102015-08-17 10:31:51 +0800514
Jens Axboef9de14b2018-04-13 16:25:57 -0600515 if (!cmd->use_aio || cmd->ret < 0 || cmd->ret == blk_rq_bytes(rq) ||
516 req_op(rq) != REQ_OP_READ) {
517 if (cmd->ret < 0)
Evan Green8cd55082020-04-03 16:43:03 +0200518 ret = errno_to_blk_status(cmd->ret);
Jens Axboef9de14b2018-04-13 16:25:57 -0600519 goto end_io;
Ming Leibc07c102015-08-17 10:31:51 +0800520 }
Christoph Hellwigfe2cb292017-04-20 16:03:02 +0200521
Jens Axboef9de14b2018-04-13 16:25:57 -0600522 /*
523 * Short READ - if we got some data, advance our request and
524 * retry it. If we got no data, end the rest with EIO.
525 */
526 if (cmd->ret) {
527 blk_update_request(rq, BLK_STS_OK, cmd->ret);
528 cmd->ret = 0;
529 blk_mq_requeue_request(rq, true);
530 } else {
531 if (cmd->use_aio) {
532 struct bio *bio = rq->bio;
533
534 while (bio) {
535 zero_fill_bio(bio);
536 bio = bio->bi_next;
537 }
538 }
539 ret = BLK_STS_IOERR;
540end_io:
541 blk_mq_end_request(rq, ret);
542 }
Ming Leibc07c102015-08-17 10:31:51 +0800543}
544
Shaohua Li92d77332017-09-01 11:15:17 -0700545static void lo_rw_aio_do_completion(struct loop_cmd *cmd)
546{
Jens Axboe1894e912018-04-13 16:24:29 -0600547 struct request *rq = blk_mq_rq_from_pdu(cmd);
548
Shaohua Li92d77332017-09-01 11:15:17 -0700549 if (!atomic_dec_and_test(&cmd->ref))
550 return;
551 kfree(cmd->bvec);
552 cmd->bvec = NULL;
Christoph Hellwig15f73f52020-06-11 08:44:47 +0200553 if (likely(!blk_should_fake_timeout(rq->q)))
554 blk_mq_complete_request(rq);
Shaohua Li92d77332017-09-01 11:15:17 -0700555}
556
Ming Leibc07c102015-08-17 10:31:51 +0800557static void lo_rw_aio_complete(struct kiocb *iocb, long ret, long ret2)
558{
559 struct loop_cmd *cmd = container_of(iocb, struct loop_cmd, iocb);
Ming Leibc07c102015-08-17 10:31:51 +0800560
Christoph Hellwigfe2cb292017-04-20 16:03:02 +0200561 cmd->ret = ret;
Shaohua Li92d77332017-09-01 11:15:17 -0700562 lo_rw_aio_do_completion(cmd);
Ming Leibc07c102015-08-17 10:31:51 +0800563}
564
565static int lo_rw_aio(struct loop_device *lo, struct loop_cmd *cmd,
566 loff_t pos, bool rw)
567{
568 struct iov_iter iter;
Ming Lei86af5952019-02-15 19:13:17 +0800569 struct req_iterator rq_iter;
Ming Leibc07c102015-08-17 10:31:51 +0800570 struct bio_vec *bvec;
Jens Axboe1894e912018-04-13 16:24:29 -0600571 struct request *rq = blk_mq_rq_from_pdu(cmd);
Shaohua Li40326d82017-08-31 22:09:46 -0700572 struct bio *bio = rq->bio;
Ming Leibc07c102015-08-17 10:31:51 +0800573 struct file *file = lo->lo_backing_file;
Ming Lei86af5952019-02-15 19:13:17 +0800574 struct bio_vec tmp;
Shaohua Li40326d82017-08-31 22:09:46 -0700575 unsigned int offset;
Ming Lei86af5952019-02-15 19:13:17 +0800576 int nr_bvec = 0;
Ming Leibc07c102015-08-17 10:31:51 +0800577 int ret;
578
Ming Lei86af5952019-02-15 19:13:17 +0800579 rq_for_each_bvec(tmp, rq, rq_iter)
580 nr_bvec++;
Ming Leibc07c102015-08-17 10:31:51 +0800581
Ming Lei86af5952019-02-15 19:13:17 +0800582 if (rq->bio != rq->biotail) {
583
584 bvec = kmalloc_array(nr_bvec, sizeof(struct bio_vec),
Kees Cook6da2ec52018-06-12 13:55:00 -0700585 GFP_NOIO);
Shaohua Li40326d82017-08-31 22:09:46 -0700586 if (!bvec)
587 return -EIO;
588 cmd->bvec = bvec;
589
590 /*
591 * The bios of the request may be started from the middle of
592 * the 'bvec' because of bio splitting, so we can't directly
Ming Lei86af5952019-02-15 19:13:17 +0800593 * copy bio->bi_iov_vec to new bvec. The rq_for_each_bvec
Shaohua Li40326d82017-08-31 22:09:46 -0700594 * API will take care of all details for us.
595 */
Ming Lei86af5952019-02-15 19:13:17 +0800596 rq_for_each_bvec(tmp, rq, rq_iter) {
Shaohua Li40326d82017-08-31 22:09:46 -0700597 *bvec = tmp;
598 bvec++;
599 }
600 bvec = cmd->bvec;
601 offset = 0;
602 } else {
603 /*
604 * Same here, this bio may be started from the middle of the
605 * 'bvec' because of bio splitting, so offset from the bvec
606 * must be passed to iov iterator
607 */
608 offset = bio->bi_iter.bi_bvec_done;
609 bvec = __bvec_iter_bvec(bio->bi_io_vec, bio->bi_iter);
Shaohua Li40326d82017-08-31 22:09:46 -0700610 }
Shaohua Li92d77332017-09-01 11:15:17 -0700611 atomic_set(&cmd->ref, 2);
Shaohua Li40326d82017-08-31 22:09:46 -0700612
Christoph Hellwigb6207432019-06-26 15:49:28 +0200613 iov_iter_bvec(&iter, rw, bvec, nr_bvec, blk_rq_bytes(rq));
Shaohua Li40326d82017-08-31 22:09:46 -0700614 iter.iov_offset = offset;
Ming Leibc07c102015-08-17 10:31:51 +0800615
616 cmd->iocb.ki_pos = pos;
617 cmd->iocb.ki_filp = file;
618 cmd->iocb.ki_complete = lo_rw_aio_complete;
619 cmd->iocb.ki_flags = IOCB_DIRECT;
Adam Manzanaresd9a08a92018-05-22 10:52:19 -0700620 cmd->iocb.ki_ioprio = IOPRIO_PRIO_VALUE(IOPRIO_CLASS_NONE, 0);
Ming Leibc07c102015-08-17 10:31:51 +0800621
622 if (rw == WRITE)
Miklos Szeredibb7462b2017-02-20 16:51:23 +0100623 ret = call_write_iter(file, &cmd->iocb, &iter);
Ming Leibc07c102015-08-17 10:31:51 +0800624 else
Miklos Szeredibb7462b2017-02-20 16:51:23 +0100625 ret = call_read_iter(file, &cmd->iocb, &iter);
Ming Leibc07c102015-08-17 10:31:51 +0800626
Shaohua Li92d77332017-09-01 11:15:17 -0700627 lo_rw_aio_do_completion(cmd);
628
Ming Leibc07c102015-08-17 10:31:51 +0800629 if (ret != -EIOCBQUEUED)
630 cmd->iocb.ki_complete(&cmd->iocb, ret, 0);
631 return 0;
632}
633
Christoph Hellwigc1c87c22016-08-04 16:10:01 +0200634static int do_req_filebacked(struct loop_device *lo, struct request *rq)
Ming Leibc07c102015-08-17 10:31:51 +0800635{
636 struct loop_cmd *cmd = blk_mq_rq_to_pdu(rq);
Christoph Hellwigc1c87c22016-08-04 16:10:01 +0200637 loff_t pos = ((loff_t) blk_rq_pos(rq) << 9) + lo->lo_offset;
Ming Leibc07c102015-08-17 10:31:51 +0800638
639 /*
640 * lo_write_simple and lo_read_simple should have been covered
641 * by io submit style function like lo_rw_aio(), one blocker
642 * is that lo_read_simple() need to call flush_dcache_page after
643 * the page is written from kernel, and it isn't easy to handle
644 * this in io submit style function which submits all segments
645 * of the req at one time. And direct read IO doesn't need to
646 * run flush_dcache_page().
647 */
Christoph Hellwigc1c87c22016-08-04 16:10:01 +0200648 switch (req_op(rq)) {
649 case REQ_OP_FLUSH:
650 return lo_req_flush(lo, rq);
Christoph Hellwig19372e22017-04-05 19:21:15 +0200651 case REQ_OP_WRITE_ZEROES:
Darrick J. Wongefcfec572019-10-30 20:29:48 -0700652 /*
653 * If the caller doesn't want deallocation, call zeroout to
654 * write zeroes the range. Otherwise, punch them out.
655 */
656 return lo_fallocate(lo, rq, pos,
657 (rq->cmd_flags & REQ_NOUNMAP) ?
658 FALLOC_FL_ZERO_RANGE :
659 FALLOC_FL_PUNCH_HOLE);
660 case REQ_OP_DISCARD:
661 return lo_fallocate(lo, rq, pos, FALLOC_FL_PUNCH_HOLE);
Christoph Hellwigc1c87c22016-08-04 16:10:01 +0200662 case REQ_OP_WRITE:
Christoph Hellwigaa4d8612015-04-07 18:23:29 +0200663 if (lo->transfer)
Christoph Hellwigc1c87c22016-08-04 16:10:01 +0200664 return lo_write_transfer(lo, rq, pos);
665 else if (cmd->use_aio)
666 return lo_rw_aio(lo, cmd, pos, WRITE);
Christoph Hellwigaa4d8612015-04-07 18:23:29 +0200667 else
Christoph Hellwigc1c87c22016-08-04 16:10:01 +0200668 return lo_write_simple(lo, rq, pos);
669 case REQ_OP_READ:
670 if (lo->transfer)
671 return lo_read_transfer(lo, rq, pos);
672 else if (cmd->use_aio)
673 return lo_rw_aio(lo, cmd, pos, READ);
674 else
675 return lo_read_simple(lo, rq, pos);
676 default:
677 WARN_ON_ONCE(1);
678 return -EIO;
Christoph Hellwigaa4d8612015-04-07 18:23:29 +0200679 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700680}
681
Ming Lei2e5ab5f2015-08-17 10:31:49 +0800682static inline void loop_update_dio(struct loop_device *lo)
683{
Ira Weinyefbe3c22020-04-30 07:41:33 -0700684 __loop_update_dio(lo, (lo->lo_backing_file->f_flags & O_DIRECT) |
685 lo->use_dio);
Ming Lei2e5ab5f2015-08-17 10:31:49 +0800686}
687
Christoph Hellwig03842642021-06-24 14:32:40 +0200688static void loop_reread_partitions(struct loop_device *lo)
Ming Lei06f0e9e2015-05-06 12:26:24 +0800689{
690 int rc;
691
Christoph Hellwig03842642021-06-24 14:32:40 +0200692 mutex_lock(&lo->lo_disk->open_mutex);
693 rc = bdev_disk_changed(lo->lo_disk, false);
694 mutex_unlock(&lo->lo_disk->open_mutex);
Ming Lei06f0e9e2015-05-06 12:26:24 +0800695 if (rc)
696 pr_warn("%s: partition scan of loop%d (%s) failed (rc=%d)\n",
697 __func__, lo->lo_number, lo->lo_file_name, rc);
698}
699
Theodore Ts'od2ac8382018-05-07 11:37:58 -0400700static inline int is_loop_device(struct file *file)
701{
702 struct inode *i = file->f_mapping->host;
703
Al Viro6f247842021-01-31 19:23:55 -0500704 return i && S_ISBLK(i->i_mode) && imajor(i) == LOOP_MAJOR;
Theodore Ts'od2ac8382018-05-07 11:37:58 -0400705}
706
707static int loop_validate_file(struct file *file, struct block_device *bdev)
708{
709 struct inode *inode = file->f_mapping->host;
710 struct file *f = file;
711
712 /* Avoid recursion */
713 while (is_loop_device(f)) {
714 struct loop_device *l;
715
Tetsuo Handa3ce6e1f2021-07-06 23:40:34 +0900716 lockdep_assert_held(&loop_validate_mutex);
Christoph Hellwig4e7b5672020-11-23 13:38:40 +0100717 if (f->f_mapping->host->i_rdev == bdev->bd_dev)
Theodore Ts'od2ac8382018-05-07 11:37:58 -0400718 return -EBADF;
719
Christoph Hellwig4e7b5672020-11-23 13:38:40 +0100720 l = I_BDEV(f->f_mapping->host)->bd_disk->private_data;
Tetsuo Handa3ce6e1f2021-07-06 23:40:34 +0900721 if (l->lo_state != Lo_bound)
Theodore Ts'od2ac8382018-05-07 11:37:58 -0400722 return -EINVAL;
Tetsuo Handa3ce6e1f2021-07-06 23:40:34 +0900723 /* Order wrt setting lo->lo_backing_file in loop_configure(). */
724 rmb();
Theodore Ts'od2ac8382018-05-07 11:37:58 -0400725 f = l->lo_backing_file;
726 }
727 if (!S_ISREG(inode->i_mode) && !S_ISBLK(inode->i_mode))
728 return -EINVAL;
729 return 0;
730}
731
Linus Torvalds1da177e2005-04-16 15:20:36 -0700732/*
733 * loop_change_fd switched the backing store of a loopback device to
734 * a new file. This is useful for operating system installers to free up
735 * the original file and in High Availability environments to switch to
736 * an alternative location for the content in case of server meltdown.
737 * This can only work if the loop device is used read-only, and if the
738 * new backing store is the same size and type as the old backing store.
739 */
Al Virobb214882008-03-02 09:29:48 -0500740static int loop_change_fd(struct loop_device *lo, struct block_device *bdev,
741 unsigned int arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700742{
Tetsuo Handa3ce6e1f2021-07-06 23:40:34 +0900743 struct file *file = fget(arg);
744 struct file *old_file;
745 int error;
746 bool partscan;
747 bool is_loop;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700748
Tetsuo Handa3ce6e1f2021-07-06 23:40:34 +0900749 if (!file)
750 return -EBADF;
751 is_loop = is_loop_device(file);
752 error = loop_global_lock_killable(lo, is_loop);
Jan Karac3710772018-11-08 14:01:11 +0100753 if (error)
Tetsuo Handa3ce6e1f2021-07-06 23:40:34 +0900754 goto out_putf;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700755 error = -ENXIO;
756 if (lo->lo_state != Lo_bound)
Jan Kara1dded9a2018-11-08 14:01:15 +0100757 goto out_err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700758
759 /* the loop device has to be read-only */
760 error = -EINVAL;
761 if (!(lo->lo_flags & LO_FLAGS_READ_ONLY))
Jan Kara1dded9a2018-11-08 14:01:15 +0100762 goto out_err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700763
Theodore Ts'od2ac8382018-05-07 11:37:58 -0400764 error = loop_validate_file(file, bdev);
765 if (error)
Jan Kara1dded9a2018-11-08 14:01:15 +0100766 goto out_err;
Theodore Ts'od2ac8382018-05-07 11:37:58 -0400767
Linus Torvalds1da177e2005-04-16 15:20:36 -0700768 old_file = lo->lo_backing_file;
769
770 error = -EINVAL;
771
Linus Torvalds1da177e2005-04-16 15:20:36 -0700772 /* size of the new backing store needs to be the same */
773 if (get_loop_size(lo, file) != get_loop_size(lo, old_file))
Jan Kara1dded9a2018-11-08 14:01:15 +0100774 goto out_err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700775
776 /* and ... switch */
Matteo Croce9f65c482021-07-13 01:05:30 +0200777 disk_force_media_change(lo->lo_disk, DISK_EVENT_MEDIA_CHANGE);
Omar Sandoval43cade802017-08-24 00:03:44 -0700778 blk_mq_freeze_queue(lo->lo_queue);
779 mapping_set_gfp_mask(old_file->f_mapping, lo->old_gfp_mask);
780 lo->lo_backing_file = file;
781 lo->old_gfp_mask = mapping_gfp_mask(file->f_mapping);
782 mapping_set_gfp_mask(file->f_mapping,
783 lo->old_gfp_mask & ~(__GFP_IO|__GFP_FS));
784 loop_update_dio(lo);
785 blk_mq_unfreeze_queue(lo->lo_queue);
Jan Kara85b0a542018-11-08 14:01:13 +0100786 partscan = lo->lo_flags & LO_FLAGS_PARTSCAN;
Tetsuo Handa3ce6e1f2021-07-06 23:40:34 +0900787 loop_global_unlock(lo, is_loop);
788
789 /*
790 * Flush loop_validate_file() before fput(), for l->lo_backing_file
791 * might be pointing at old_file which might be the last reference.
792 */
793 if (!is_loop) {
794 mutex_lock(&loop_validate_mutex);
795 mutex_unlock(&loop_validate_mutex);
796 }
Jan Kara1dded9a2018-11-08 14:01:15 +0100797 /*
Pavel Tatashin6cc8e742021-01-26 09:46:30 -0500798 * We must drop file reference outside of lo_mutex as dropping
Christoph Hellwiga8698702021-05-25 08:12:56 +0200799 * the file ref can take open_mutex which creates circular locking
Jan Kara1dded9a2018-11-08 14:01:15 +0100800 * dependency.
801 */
802 fput(old_file);
Jan Kara85b0a542018-11-08 14:01:13 +0100803 if (partscan)
Christoph Hellwig03842642021-06-24 14:32:40 +0200804 loop_reread_partitions(lo);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700805 return 0;
806
Jan Kara1dded9a2018-11-08 14:01:15 +0100807out_err:
Tetsuo Handa3ce6e1f2021-07-06 23:40:34 +0900808 loop_global_unlock(lo, is_loop);
809out_putf:
810 fput(file);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700811 return error;
812}
813
Milan Brozee862732010-08-23 15:16:00 +0200814/* loop sysfs attributes */
815
816static ssize_t loop_attr_show(struct device *dev, char *page,
817 ssize_t (*callback)(struct loop_device *, char *))
818{
Kay Sievers34dd82a2011-07-31 22:08:04 +0200819 struct gendisk *disk = dev_to_disk(dev);
820 struct loop_device *lo = disk->private_data;
Milan Brozee862732010-08-23 15:16:00 +0200821
Kay Sievers34dd82a2011-07-31 22:08:04 +0200822 return callback(lo, page);
Milan Brozee862732010-08-23 15:16:00 +0200823}
824
825#define LOOP_ATTR_RO(_name) \
826static ssize_t loop_attr_##_name##_show(struct loop_device *, char *); \
827static ssize_t loop_attr_do_show_##_name(struct device *d, \
828 struct device_attribute *attr, char *b) \
829{ \
830 return loop_attr_show(d, b, loop_attr_##_name##_show); \
831} \
832static struct device_attribute loop_attr_##_name = \
Joe Perches5657a812018-05-24 13:38:59 -0600833 __ATTR(_name, 0444, loop_attr_do_show_##_name, NULL);
Milan Brozee862732010-08-23 15:16:00 +0200834
835static ssize_t loop_attr_backing_file_show(struct loop_device *lo, char *buf)
836{
837 ssize_t ret;
838 char *p = NULL;
839
Kay Sievers05eb0f22011-07-31 22:21:35 +0200840 spin_lock_irq(&lo->lo_lock);
Milan Brozee862732010-08-23 15:16:00 +0200841 if (lo->lo_backing_file)
Miklos Szeredi9bf39ab2015-06-19 10:29:13 +0200842 p = file_path(lo->lo_backing_file, buf, PAGE_SIZE - 1);
Kay Sievers05eb0f22011-07-31 22:21:35 +0200843 spin_unlock_irq(&lo->lo_lock);
Milan Brozee862732010-08-23 15:16:00 +0200844
845 if (IS_ERR_OR_NULL(p))
846 ret = PTR_ERR(p);
847 else {
848 ret = strlen(p);
849 memmove(buf, p, ret);
850 buf[ret++] = '\n';
851 buf[ret] = 0;
852 }
853
854 return ret;
855}
856
857static ssize_t loop_attr_offset_show(struct loop_device *lo, char *buf)
858{
859 return sprintf(buf, "%llu\n", (unsigned long long)lo->lo_offset);
860}
861
862static ssize_t loop_attr_sizelimit_show(struct loop_device *lo, char *buf)
863{
864 return sprintf(buf, "%llu\n", (unsigned long long)lo->lo_sizelimit);
865}
866
867static ssize_t loop_attr_autoclear_show(struct loop_device *lo, char *buf)
868{
869 int autoclear = (lo->lo_flags & LO_FLAGS_AUTOCLEAR);
870
871 return sprintf(buf, "%s\n", autoclear ? "1" : "0");
872}
873
Kay Sieverse03c8dd2011-08-23 20:12:04 +0200874static ssize_t loop_attr_partscan_show(struct loop_device *lo, char *buf)
875{
876 int partscan = (lo->lo_flags & LO_FLAGS_PARTSCAN);
877
878 return sprintf(buf, "%s\n", partscan ? "1" : "0");
879}
880
Ming Lei2e5ab5f2015-08-17 10:31:49 +0800881static ssize_t loop_attr_dio_show(struct loop_device *lo, char *buf)
882{
883 int dio = (lo->lo_flags & LO_FLAGS_DIRECT_IO);
884
885 return sprintf(buf, "%s\n", dio ? "1" : "0");
886}
887
Milan Brozee862732010-08-23 15:16:00 +0200888LOOP_ATTR_RO(backing_file);
889LOOP_ATTR_RO(offset);
890LOOP_ATTR_RO(sizelimit);
891LOOP_ATTR_RO(autoclear);
Kay Sieverse03c8dd2011-08-23 20:12:04 +0200892LOOP_ATTR_RO(partscan);
Ming Lei2e5ab5f2015-08-17 10:31:49 +0800893LOOP_ATTR_RO(dio);
Milan Brozee862732010-08-23 15:16:00 +0200894
895static struct attribute *loop_attrs[] = {
896 &loop_attr_backing_file.attr,
897 &loop_attr_offset.attr,
898 &loop_attr_sizelimit.attr,
899 &loop_attr_autoclear.attr,
Kay Sieverse03c8dd2011-08-23 20:12:04 +0200900 &loop_attr_partscan.attr,
Ming Lei2e5ab5f2015-08-17 10:31:49 +0800901 &loop_attr_dio.attr,
Milan Brozee862732010-08-23 15:16:00 +0200902 NULL,
903};
904
905static struct attribute_group loop_attribute_group = {
906 .name = "loop",
907 .attrs= loop_attrs,
908};
909
Tetsuo Handad3349b62018-05-04 10:58:09 -0600910static void loop_sysfs_init(struct loop_device *lo)
Milan Brozee862732010-08-23 15:16:00 +0200911{
Tetsuo Handad3349b62018-05-04 10:58:09 -0600912 lo->sysfs_inited = !sysfs_create_group(&disk_to_dev(lo->lo_disk)->kobj,
913 &loop_attribute_group);
Milan Brozee862732010-08-23 15:16:00 +0200914}
915
916static void loop_sysfs_exit(struct loop_device *lo)
917{
Tetsuo Handad3349b62018-05-04 10:58:09 -0600918 if (lo->sysfs_inited)
919 sysfs_remove_group(&disk_to_dev(lo->lo_disk)->kobj,
920 &loop_attribute_group);
Milan Brozee862732010-08-23 15:16:00 +0200921}
922
Lukas Czernerdfaa2ef2011-08-19 14:50:46 +0200923static void loop_config_discard(struct loop_device *lo)
924{
925 struct file *file = lo->lo_backing_file;
926 struct inode *inode = file->f_mapping->host;
927 struct request_queue *q = lo->lo_queue;
Ming Leibcb21c82020-08-17 18:01:30 +0800928 u32 granularity, max_discard_sectors;
Lukas Czernerdfaa2ef2011-08-19 14:50:46 +0200929
930 /*
Evan Greenc52abf52020-04-03 16:43:04 +0200931 * If the backing device is a block device, mirror its zeroing
932 * capability. Set the discard sectors to the block device's zeroing
933 * capabilities because loop discards result in blkdev_issue_zeroout(),
934 * not blkdev_issue_discard(). This maintains consistent behavior with
935 * file-backed loop devices: discarded regions read back as zero.
936 */
937 if (S_ISBLK(inode->i_mode) && !lo->lo_encrypt_key_size) {
Christoph Hellwig4e7b5672020-11-23 13:38:40 +0100938 struct request_queue *backingq = bdev_get_queue(I_BDEV(inode));
Evan Greenc52abf52020-04-03 16:43:04 +0200939
Ming Leibcb21c82020-08-17 18:01:30 +0800940 max_discard_sectors = backingq->limits.max_write_zeroes_sectors;
941 granularity = backingq->limits.discard_granularity ?:
942 queue_physical_block_size(backingq);
Evan Greenc52abf52020-04-03 16:43:04 +0200943
944 /*
Lukas Czernerdfaa2ef2011-08-19 14:50:46 +0200945 * We use punch hole to reclaim the free space used by the
Olaf Hering12a64d22014-01-21 14:39:24 -0800946 * image a.k.a. discard. However we do not support discard if
Lukas Czernerdfaa2ef2011-08-19 14:50:46 +0200947 * encryption is enabled, because it may give an attacker
948 * useful information.
949 */
Evan Greenc52abf52020-04-03 16:43:04 +0200950 } else if (!file->f_op->fallocate || lo->lo_encrypt_key_size) {
Ming Leibcb21c82020-08-17 18:01:30 +0800951 max_discard_sectors = 0;
952 granularity = 0;
Evan Greenc52abf52020-04-03 16:43:04 +0200953
954 } else {
Ming Leibcb21c82020-08-17 18:01:30 +0800955 max_discard_sectors = UINT_MAX >> 9;
956 granularity = inode->i_sb->s_blocksize;
Lukas Czernerdfaa2ef2011-08-19 14:50:46 +0200957 }
958
Ming Leibcb21c82020-08-17 18:01:30 +0800959 if (max_discard_sectors) {
960 q->limits.discard_granularity = granularity;
961 blk_queue_max_discard_sectors(q, max_discard_sectors);
962 blk_queue_max_write_zeroes_sectors(q, max_discard_sectors);
Evan Greenc52abf52020-04-03 16:43:04 +0200963 blk_queue_flag_set(QUEUE_FLAG_DISCARD, q);
Ming Leibcb21c82020-08-17 18:01:30 +0800964 } else {
965 q->limits.discard_granularity = 0;
966 blk_queue_max_discard_sectors(q, 0);
967 blk_queue_max_write_zeroes_sectors(q, 0);
Evan Greenc52abf52020-04-03 16:43:04 +0200968 blk_queue_flag_clear(QUEUE_FLAG_DISCARD, q);
Ming Leibcb21c82020-08-17 18:01:30 +0800969 }
970 q->limits.discard_alignment = 0;
Lukas Czernerdfaa2ef2011-08-19 14:50:46 +0200971}
972
Dan Schatzberg87579e92021-06-28 19:38:15 -0700973struct loop_worker {
974 struct rb_node rb_node;
975 struct work_struct work;
976 struct list_head cmd_list;
977 struct list_head idle_list;
978 struct loop_device *lo;
Dan Schatzbergc74d40e2021-06-28 19:38:21 -0700979 struct cgroup_subsys_state *blkcg_css;
Dan Schatzberg87579e92021-06-28 19:38:15 -0700980 unsigned long last_ran_at;
981};
Ming Leie03a3d72015-08-17 10:31:48 +0800982
Dan Schatzberg87579e92021-06-28 19:38:15 -0700983static void loop_workfn(struct work_struct *work);
984static void loop_rootcg_workfn(struct work_struct *work);
985static void loop_free_idle_workers(struct timer_list *timer);
NeilBrownb2ee7d42017-06-16 15:02:09 +1000986
Dan Schatzberg87579e92021-06-28 19:38:15 -0700987#ifdef CONFIG_BLK_CGROUP
988static inline int queue_on_root_worker(struct cgroup_subsys_state *css)
Ming Leie03a3d72015-08-17 10:31:48 +0800989{
Dan Schatzberg87579e92021-06-28 19:38:15 -0700990 return !css || css == blkcg_root_css;
991}
992#else
993static inline int queue_on_root_worker(struct cgroup_subsys_state *css)
994{
995 return !css;
996}
997#endif
998
999static void loop_queue_work(struct loop_device *lo, struct loop_cmd *cmd)
1000{
1001 struct rb_node **node = &(lo->worker_tree.rb_node), *parent = NULL;
1002 struct loop_worker *cur_worker, *worker = NULL;
1003 struct work_struct *work;
1004 struct list_head *cmd_list;
1005
1006 spin_lock_irq(&lo->lo_work_lock);
1007
Dan Schatzbergc74d40e2021-06-28 19:38:21 -07001008 if (queue_on_root_worker(cmd->blkcg_css))
Dan Schatzberg87579e92021-06-28 19:38:15 -07001009 goto queue_work;
1010
1011 node = &lo->worker_tree.rb_node;
1012
1013 while (*node) {
1014 parent = *node;
1015 cur_worker = container_of(*node, struct loop_worker, rb_node);
Dan Schatzbergc74d40e2021-06-28 19:38:21 -07001016 if (cur_worker->blkcg_css == cmd->blkcg_css) {
Dan Schatzberg87579e92021-06-28 19:38:15 -07001017 worker = cur_worker;
1018 break;
Dan Schatzbergc74d40e2021-06-28 19:38:21 -07001019 } else if ((long)cur_worker->blkcg_css < (long)cmd->blkcg_css) {
Dan Schatzberg87579e92021-06-28 19:38:15 -07001020 node = &(*node)->rb_left;
1021 } else {
1022 node = &(*node)->rb_right;
1023 }
1024 }
1025 if (worker)
1026 goto queue_work;
1027
1028 worker = kzalloc(sizeof(struct loop_worker), GFP_NOWAIT | __GFP_NOWARN);
1029 /*
1030 * In the event we cannot allocate a worker, just queue on the
Dan Schatzbergc74d40e2021-06-28 19:38:21 -07001031 * rootcg worker and issue the I/O as the rootcg
Dan Schatzberg87579e92021-06-28 19:38:15 -07001032 */
Dan Schatzbergc74d40e2021-06-28 19:38:21 -07001033 if (!worker) {
1034 cmd->blkcg_css = NULL;
1035 if (cmd->memcg_css)
1036 css_put(cmd->memcg_css);
1037 cmd->memcg_css = NULL;
Dan Schatzberg87579e92021-06-28 19:38:15 -07001038 goto queue_work;
Dan Schatzbergc74d40e2021-06-28 19:38:21 -07001039 }
Dan Schatzberg87579e92021-06-28 19:38:15 -07001040
Dan Schatzbergc74d40e2021-06-28 19:38:21 -07001041 worker->blkcg_css = cmd->blkcg_css;
1042 css_get(worker->blkcg_css);
Dan Schatzberg87579e92021-06-28 19:38:15 -07001043 INIT_WORK(&worker->work, loop_workfn);
1044 INIT_LIST_HEAD(&worker->cmd_list);
1045 INIT_LIST_HEAD(&worker->idle_list);
1046 worker->lo = lo;
1047 rb_link_node(&worker->rb_node, parent, node);
1048 rb_insert_color(&worker->rb_node, &lo->worker_tree);
1049queue_work:
1050 if (worker) {
1051 /*
1052 * We need to remove from the idle list here while
1053 * holding the lock so that the idle timer doesn't
1054 * free the worker
1055 */
1056 if (!list_empty(&worker->idle_list))
1057 list_del_init(&worker->idle_list);
1058 work = &worker->work;
1059 cmd_list = &worker->cmd_list;
1060 } else {
1061 work = &lo->rootcg_work;
1062 cmd_list = &lo->rootcg_cmd_list;
1063 }
1064 list_add_tail(&cmd->list_entry, cmd_list);
1065 queue_work(lo->workqueue, work);
1066 spin_unlock_irq(&lo->lo_work_lock);
Ming Leie03a3d72015-08-17 10:31:48 +08001067}
1068
Holger Hoffstätte56a85fd2019-02-12 15:54:24 -07001069static void loop_update_rotational(struct loop_device *lo)
1070{
1071 struct file *file = lo->lo_backing_file;
1072 struct inode *file_inode = file->f_mapping->host;
1073 struct block_device *file_bdev = file_inode->i_sb->s_bdev;
1074 struct request_queue *q = lo->lo_queue;
1075 bool nonrot = true;
1076
1077 /* not all filesystems (e.g. tmpfs) have a sb->s_bdev */
1078 if (file_bdev)
1079 nonrot = blk_queue_nonrot(bdev_get_queue(file_bdev));
1080
1081 if (nonrot)
1082 blk_queue_flag_set(QUEUE_FLAG_NONROT, q);
1083 else
1084 blk_queue_flag_clear(QUEUE_FLAG_NONROT, q);
1085}
1086
Linus Torvalds1da177e2005-04-16 15:20:36 -07001087static int
1088loop_release_xfer(struct loop_device *lo)
1089{
1090 int err = 0;
1091 struct loop_func_table *xfer = lo->lo_encryption;
1092
1093 if (xfer) {
1094 if (xfer->release)
1095 err = xfer->release(lo);
1096 lo->transfer = NULL;
1097 lo->lo_encryption = NULL;
1098 module_put(xfer->owner);
1099 }
1100 return err;
1101}
1102
1103static int
1104loop_init_xfer(struct loop_device *lo, struct loop_func_table *xfer,
1105 const struct loop_info64 *i)
1106{
1107 int err = 0;
1108
1109 if (xfer) {
1110 struct module *owner = xfer->owner;
1111
1112 if (!try_module_get(owner))
1113 return -EINVAL;
1114 if (xfer->init)
1115 err = xfer->init(lo, i);
1116 if (err)
1117 module_put(owner);
1118 else
1119 lo->lo_encryption = xfer;
1120 }
1121 return err;
1122}
1123
Martijn Coenen62ab4662020-05-13 15:38:42 +02001124/**
1125 * loop_set_status_from_info - configure device from loop_info
1126 * @lo: struct loop_device to configure
1127 * @info: struct loop_info64 to configure the device with
1128 *
1129 * Configures the loop device parameters according to the passed
1130 * in loop_info64 configuration.
1131 */
1132static int
1133loop_set_status_from_info(struct loop_device *lo,
1134 const struct loop_info64 *info)
1135{
1136 int err;
1137 struct loop_func_table *xfer;
1138 kuid_t uid = current_uid();
1139
1140 if ((unsigned int) info->lo_encrypt_key_size > LO_KEY_SIZE)
1141 return -EINVAL;
1142
1143 err = loop_release_xfer(lo);
1144 if (err)
1145 return err;
1146
1147 if (info->lo_encrypt_type) {
1148 unsigned int type = info->lo_encrypt_type;
1149
1150 if (type >= MAX_LO_CRYPT)
1151 return -EINVAL;
1152 xfer = xfer_funcs[type];
1153 if (xfer == NULL)
1154 return -EINVAL;
1155 } else
1156 xfer = NULL;
1157
1158 err = loop_init_xfer(lo, xfer, info);
1159 if (err)
1160 return err;
1161
1162 lo->lo_offset = info->lo_offset;
1163 lo->lo_sizelimit = info->lo_sizelimit;
1164 memcpy(lo->lo_file_name, info->lo_file_name, LO_NAME_SIZE);
1165 memcpy(lo->lo_crypt_name, info->lo_crypt_name, LO_NAME_SIZE);
1166 lo->lo_file_name[LO_NAME_SIZE-1] = 0;
1167 lo->lo_crypt_name[LO_NAME_SIZE-1] = 0;
1168
1169 if (!xfer)
1170 xfer = &none_funcs;
1171 lo->transfer = xfer->transfer;
1172 lo->ioctl = xfer->ioctl;
1173
Martijn Coenenfaf1d252020-05-13 15:38:44 +02001174 lo->lo_flags = info->lo_flags;
Martijn Coenen62ab4662020-05-13 15:38:42 +02001175
1176 lo->lo_encrypt_key_size = info->lo_encrypt_key_size;
1177 lo->lo_init[0] = info->lo_init[0];
1178 lo->lo_init[1] = info->lo_init[1];
1179 if (info->lo_encrypt_key_size) {
1180 memcpy(lo->lo_encrypt_key, info->lo_encrypt_key,
1181 info->lo_encrypt_key_size);
1182 lo->lo_key_owner = uid;
1183 }
1184
1185 return 0;
1186}
1187
Martijn Coenen34489142020-05-13 15:38:45 +02001188static int loop_configure(struct loop_device *lo, fmode_t mode,
1189 struct block_device *bdev,
1190 const struct loop_config *config)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001191{
Tetsuo Handa3ce6e1f2021-07-06 23:40:34 +09001192 struct file *file = fget(config->fd);
1193 struct inode *inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001194 struct address_space *mapping;
Tetsuo Handa3ce6e1f2021-07-06 23:40:34 +09001195 int error;
1196 loff_t size;
1197 bool partscan;
1198 unsigned short bsize;
1199 bool is_loop;
1200
1201 if (!file)
1202 return -EBADF;
1203 is_loop = is_loop_device(file);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001204
1205 /* This is safe, since we have a reference from open(). */
1206 __module_get(THIS_MODULE);
1207
Linus Torvalds1da177e2005-04-16 15:20:36 -07001208 /*
1209 * If we don't hold exclusive handle for the device, upgrade to it
1210 * here to avoid changing device under exclusive owner.
1211 */
1212 if (!(mode & FMODE_EXCL)) {
Christoph Hellwig37c3fc92020-11-25 21:20:08 +01001213 error = bd_prepare_to_claim(bdev, loop_configure);
Christoph Hellwigecbe6bc2020-07-16 16:33:09 +02001214 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001215 goto out_putf;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001216 }
1217
Tetsuo Handa3ce6e1f2021-07-06 23:40:34 +09001218 error = loop_global_lock_killable(lo, is_loop);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001219 if (error)
1220 goto out_bdev;
1221
1222 error = -EBUSY;
1223 if (lo->lo_state != Lo_unbound)
1224 goto out_unlock;
1225
1226 error = loop_validate_file(file, bdev);
1227 if (error)
1228 goto out_unlock;
1229
1230 mapping = file->f_mapping;
1231 inode = mapping->host;
1232
Martijn Coenen34489142020-05-13 15:38:45 +02001233 if ((config->info.lo_flags & ~LOOP_CONFIGURE_SETTABLE_FLAGS) != 0) {
1234 error = -EINVAL;
1235 goto out_unlock;
1236 }
1237
1238 if (config->block_size) {
1239 error = loop_validate_block_size(config->block_size);
1240 if (error)
1241 goto out_unlock;
1242 }
1243
1244 error = loop_set_status_from_info(lo, &config->info);
1245 if (error)
1246 goto out_unlock;
1247
Linus Torvalds1da177e2005-04-16 15:20:36 -07001248 if (!(file->f_mode & FMODE_WRITE) || !(mode & FMODE_WRITE) ||
1249 !file->f_op->write_iter)
Martijn Coenen34489142020-05-13 15:38:45 +02001250 lo->lo_flags |= LO_FLAGS_READ_ONLY;
Martijn Coenen083a6a52020-05-13 15:38:36 +02001251
Dan Schatzberg87579e92021-06-28 19:38:15 -07001252 lo->workqueue = alloc_workqueue("loop%d",
1253 WQ_UNBOUND | WQ_FREEZABLE,
1254 0,
1255 lo->lo_number);
1256 if (!lo->workqueue) {
1257 error = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001258 goto out_unlock;
Dan Schatzberg87579e92021-06-28 19:38:15 -07001259 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001260
Matteo Croce9f65c482021-07-13 01:05:30 +02001261 disk_force_media_change(lo->lo_disk, DISK_EVENT_MEDIA_CHANGE);
Christoph Hellwig7a2f0ce2020-11-03 11:00:16 +01001262 set_disk_ro(lo->lo_disk, (lo->lo_flags & LO_FLAGS_READ_ONLY) != 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001263
Dan Schatzberg87579e92021-06-28 19:38:15 -07001264 INIT_WORK(&lo->rootcg_work, loop_rootcg_workfn);
1265 INIT_LIST_HEAD(&lo->rootcg_cmd_list);
1266 INIT_LIST_HEAD(&lo->idle_worker_list);
1267 lo->worker_tree = RB_ROOT;
1268 timer_setup(&lo->timer, loop_free_idle_workers,
1269 TIMER_DEFERRABLE);
Martijn Coenen34489142020-05-13 15:38:45 +02001270 lo->use_dio = lo->lo_flags & LO_FLAGS_DIRECT_IO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001271 lo->lo_device = bdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001272 lo->lo_backing_file = file;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001273 lo->old_gfp_mask = mapping_gfp_mask(mapping);
1274 mapping_set_gfp_mask(mapping, lo->old_gfp_mask & ~(__GFP_IO|__GFP_FS));
1275
Martijn Coenen34489142020-05-13 15:38:45 +02001276 if (!(lo->lo_flags & LO_FLAGS_READ_ONLY) && file->f_op->fsync)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001277 blk_queue_write_cache(lo->lo_queue, true, false);
1278
Martijn Coenen34489142020-05-13 15:38:45 +02001279 if (config->block_size)
1280 bsize = config->block_size;
Linus Torvalds96ed3202020-06-02 19:45:12 -07001281 else if ((lo->lo_backing_file->f_flags & O_DIRECT) && inode->i_sb->s_bdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001282 /* In case of direct I/O, match underlying block size */
Martijn Coenen34489142020-05-13 15:38:45 +02001283 bsize = bdev_logical_block_size(inode->i_sb->s_bdev);
1284 else
1285 bsize = 512;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001286
Martijn Coenen34489142020-05-13 15:38:45 +02001287 blk_queue_logical_block_size(lo->lo_queue, bsize);
1288 blk_queue_physical_block_size(lo->lo_queue, bsize);
1289 blk_queue_io_min(lo->lo_queue, bsize);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001290
Kristian Klausen2b9ac222021-06-18 13:51:57 +02001291 loop_config_discard(lo);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001292 loop_update_rotational(lo);
1293 loop_update_dio(lo);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001294 loop_sysfs_init(lo);
Martijn Coenen79e5dc52020-08-25 09:18:29 +02001295
1296 size = get_loop_size(lo, file);
Martijn Coenen5795b6f2020-05-13 15:38:37 +02001297 loop_set_size(lo, size);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001298
Tetsuo Handa3ce6e1f2021-07-06 23:40:34 +09001299 /* Order wrt reading lo_state in loop_validate_file(). */
1300 wmb();
1301
Linus Torvalds1da177e2005-04-16 15:20:36 -07001302 lo->lo_state = Lo_bound;
1303 if (part_shift)
1304 lo->lo_flags |= LO_FLAGS_PARTSCAN;
1305 partscan = lo->lo_flags & LO_FLAGS_PARTSCAN;
Lennart Poetteringfe6a8fc2020-08-10 19:16:32 +02001306 if (partscan)
1307 lo->lo_disk->flags &= ~GENHD_FL_NO_PART_SCAN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001308
Tetsuo Handa3ce6e1f2021-07-06 23:40:34 +09001309 loop_global_unlock(lo, is_loop);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001310 if (partscan)
Christoph Hellwig03842642021-06-24 14:32:40 +02001311 loop_reread_partitions(lo);
Christoph Hellwig37c3fc92020-11-25 21:20:08 +01001312 if (!(mode & FMODE_EXCL))
1313 bd_abort_claiming(bdev, loop_configure);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001314 return 0;
1315
1316out_unlock:
Tetsuo Handa3ce6e1f2021-07-06 23:40:34 +09001317 loop_global_unlock(lo, is_loop);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001318out_bdev:
Christoph Hellwig37c3fc92020-11-25 21:20:08 +01001319 if (!(mode & FMODE_EXCL))
1320 bd_abort_claiming(bdev, loop_configure);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001321out_putf:
1322 fput(file);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001323 /* This is safe: open() is still holding a reference. */
1324 module_put(THIS_MODULE);
1325 return error;
1326}
1327
Jan Kara0da03ca2018-11-08 14:01:14 +01001328static int __loop_clr_fd(struct loop_device *lo, bool release)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001329{
Jan Kara7ccd0792018-11-08 14:01:07 +01001330 struct file *filp = NULL;
Al Virob4e3ca12005-10-21 03:22:34 -04001331 gfp_t gfp = lo->old_gfp_mask;
Ayan George4c823cc2011-09-21 10:02:13 +02001332 struct block_device *bdev = lo->lo_device;
Jan Kara7ccd0792018-11-08 14:01:07 +01001333 int err = 0;
Jan Kara0da03ca2018-11-08 14:01:14 +01001334 bool partscan = false;
1335 int lo_number;
Dan Schatzberg87579e92021-06-28 19:38:15 -07001336 struct loop_worker *pos, *worker;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001337
Tetsuo Handa3ce6e1f2021-07-06 23:40:34 +09001338 /*
1339 * Flush loop_configure() and loop_change_fd(). It is acceptable for
1340 * loop_validate_file() to succeed, for actual clear operation has not
1341 * started yet.
1342 */
1343 mutex_lock(&loop_validate_mutex);
1344 mutex_unlock(&loop_validate_mutex);
1345 /*
1346 * loop_validate_file() now fails because l->lo_state != Lo_bound
1347 * became visible.
1348 */
1349
Pavel Tatashin6cc8e742021-01-26 09:46:30 -05001350 mutex_lock(&lo->lo_mutex);
Jan Kara7ccd0792018-11-08 14:01:07 +01001351 if (WARN_ON_ONCE(lo->lo_state != Lo_rundown)) {
1352 err = -ENXIO;
1353 goto out_unlock;
1354 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001355
Jan Kara7ccd0792018-11-08 14:01:07 +01001356 filp = lo->lo_backing_file;
1357 if (filp == NULL) {
1358 err = -EINVAL;
1359 goto out_unlock;
1360 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001361
Mauricio Faria de Oliveira4ceddce2021-02-22 12:41:23 -03001362 if (test_bit(QUEUE_FLAG_WC, &lo->lo_queue->queue_flags))
1363 blk_queue_write_cache(lo->lo_queue, false, false);
1364
Ming Leif8933662015-05-06 12:26:23 +08001365 /* freeze request queue during the transition */
1366 blk_mq_freeze_queue(lo->lo_queue);
1367
Dan Schatzberg87579e92021-06-28 19:38:15 -07001368 destroy_workqueue(lo->workqueue);
1369 spin_lock_irq(&lo->lo_work_lock);
1370 list_for_each_entry_safe(worker, pos, &lo->idle_worker_list,
1371 idle_list) {
1372 list_del(&worker->idle_list);
1373 rb_erase(&worker->rb_node, &lo->worker_tree);
Dan Schatzbergc74d40e2021-06-28 19:38:21 -07001374 css_put(worker->blkcg_css);
Dan Schatzberg87579e92021-06-28 19:38:15 -07001375 kfree(worker);
1376 }
1377 spin_unlock_irq(&lo->lo_work_lock);
1378 del_timer_sync(&lo->timer);
1379
Linus Torvalds1da177e2005-04-16 15:20:36 -07001380 spin_lock_irq(&lo->lo_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001381 lo->lo_backing_file = NULL;
Kay Sievers05eb0f22011-07-31 22:21:35 +02001382 spin_unlock_irq(&lo->lo_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001383
1384 loop_release_xfer(lo);
1385 lo->transfer = NULL;
1386 lo->ioctl = NULL;
1387 lo->lo_device = NULL;
1388 lo->lo_encryption = NULL;
1389 lo->lo_offset = 0;
1390 lo->lo_sizelimit = 0;
1391 lo->lo_encrypt_key_size = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001392 memset(lo->lo_encrypt_key, 0, LO_KEY_SIZE);
1393 memset(lo->lo_crypt_name, 0, LO_NAME_SIZE);
1394 memset(lo->lo_file_name, 0, LO_NAME_SIZE);
Omar Sandoval89e4fde2017-08-24 00:03:43 -07001395 blk_queue_logical_block_size(lo->lo_queue, 512);
Omar Sandovalbf093752017-09-05 14:24:47 -07001396 blk_queue_physical_block_size(lo->lo_queue, 512);
1397 blk_queue_io_min(lo->lo_queue, 512);
Anatol Pomozovc1681bf2013-04-01 09:47:56 -07001398 if (bdev) {
Al Virobb214882008-03-02 09:29:48 -05001399 invalidate_bdev(bdev);
Jeff Laytoneedffa22018-05-21 14:35:03 -04001400 bdev->bd_inode->i_mapping->wb_err = 0;
Anatol Pomozovc1681bf2013-04-01 09:47:56 -07001401 }
Ken Chen73285082007-05-08 00:28:20 -07001402 set_capacity(lo->lo_disk, 0);
Milan Broz51a0bb02010-10-27 19:51:30 -06001403 loop_sysfs_exit(lo);
David Zeuthenc3473c62010-05-03 14:08:59 +02001404 if (bdev) {
David Zeuthenc3473c62010-05-03 14:08:59 +02001405 /* let user-space know about this change */
1406 kobject_uevent(&disk_to_dev(bdev->bd_disk)->kobj, KOBJ_CHANGE);
1407 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001408 mapping_set_gfp_mask(filp->f_mapping, gfp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001409 /* This is safe: open() is still holding a reference. */
1410 module_put(THIS_MODULE);
Ming Leif8933662015-05-06 12:26:23 +08001411 blk_mq_unfreeze_queue(lo->lo_queue);
1412
Jan Kara0da03ca2018-11-08 14:01:14 +01001413 partscan = lo->lo_flags & LO_FLAGS_PARTSCAN && bdev;
1414 lo_number = lo->lo_number;
Matteo Croce9f65c482021-07-13 01:05:30 +02001415 disk_force_media_change(lo->lo_disk, DISK_EVENT_MEDIA_CHANGE);
Jan Kara0da03ca2018-11-08 14:01:14 +01001416out_unlock:
Pavel Tatashin6cc8e742021-01-26 09:46:30 -05001417 mutex_unlock(&lo->lo_mutex);
Jan Kara0da03ca2018-11-08 14:01:14 +01001418 if (partscan) {
Jan Karad57f3372018-11-08 14:01:12 +01001419 /*
Christoph Hellwiga8698702021-05-25 08:12:56 +02001420 * open_mutex has been held already in release path, so don't
Jan Karad57f3372018-11-08 14:01:12 +01001421 * acquire it if this function is called in such case.
1422 *
1423 * If the reread partition isn't from release path, lo_refcnt
1424 * must be at least one and it can only become zero when the
1425 * current holder is released.
1426 */
Christoph Hellwigf0b870d2019-11-14 15:34:36 +01001427 if (!release)
Christoph Hellwig03842642021-06-24 14:32:40 +02001428 mutex_lock(&lo->lo_disk->open_mutex);
1429 err = bdev_disk_changed(lo->lo_disk, false);
Christoph Hellwigf0b870d2019-11-14 15:34:36 +01001430 if (!release)
Christoph Hellwig03842642021-06-24 14:32:40 +02001431 mutex_unlock(&lo->lo_disk->open_mutex);
Dongli Zhang40853d62019-02-22 22:10:19 +08001432 if (err)
1433 pr_warn("%s: partition scan of loop%d failed (rc=%d)\n",
1434 __func__, lo_number, err);
Jan Karad57f3372018-11-08 14:01:12 +01001435 /* Device is gone, no point in returning error */
1436 err = 0;
1437 }
Dongli Zhang758a58d2019-02-22 22:10:20 +08001438
1439 /*
1440 * lo->lo_state is set to Lo_unbound here after above partscan has
1441 * finished.
1442 *
1443 * There cannot be anybody else entering __loop_clr_fd() as
1444 * lo->lo_backing_file is already cleared and Lo_rundown state
1445 * protects us from all the other places trying to change the 'lo'
1446 * device.
1447 */
Pavel Tatashin6cc8e742021-01-26 09:46:30 -05001448 mutex_lock(&lo->lo_mutex);
Dongli Zhang758a58d2019-02-22 22:10:20 +08001449 lo->lo_flags = 0;
1450 if (!part_shift)
1451 lo->lo_disk->flags |= GENHD_FL_NO_PART_SCAN;
1452 lo->lo_state = Lo_unbound;
Pavel Tatashin6cc8e742021-01-26 09:46:30 -05001453 mutex_unlock(&lo->lo_mutex);
Dongli Zhang758a58d2019-02-22 22:10:20 +08001454
Nikanth Karthikesanf028f3b2009-03-24 12:33:41 +01001455 /*
Pavel Tatashin6cc8e742021-01-26 09:46:30 -05001456 * Need not hold lo_mutex to fput backing file. Calling fput holding
1457 * lo_mutex triggers a circular lock dependency possibility warning as
Christoph Hellwiga8698702021-05-25 08:12:56 +02001458 * fput can take open_mutex which is usually taken before lo_mutex.
Nikanth Karthikesanf028f3b2009-03-24 12:33:41 +01001459 */
Jan Kara7ccd0792018-11-08 14:01:07 +01001460 if (filp)
1461 fput(filp);
1462 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001463}
1464
Jan Karaa2505b72018-11-08 14:01:06 +01001465static int loop_clr_fd(struct loop_device *lo)
1466{
Jan Kara7ccd0792018-11-08 14:01:07 +01001467 int err;
1468
Pavel Tatashin6cc8e742021-01-26 09:46:30 -05001469 err = mutex_lock_killable(&lo->lo_mutex);
Jan Kara7ccd0792018-11-08 14:01:07 +01001470 if (err)
1471 return err;
1472 if (lo->lo_state != Lo_bound) {
Pavel Tatashin6cc8e742021-01-26 09:46:30 -05001473 mutex_unlock(&lo->lo_mutex);
Jan Karaa2505b72018-11-08 14:01:06 +01001474 return -ENXIO;
Jan Kara7ccd0792018-11-08 14:01:07 +01001475 }
Jan Karaa2505b72018-11-08 14:01:06 +01001476 /*
1477 * If we've explicitly asked to tear down the loop device,
1478 * and it has an elevated reference count, set it for auto-teardown when
1479 * the last reference goes away. This stops $!~#$@ udev from
1480 * preventing teardown because it decided that it needs to run blkid on
1481 * the loopback device whenever they appear. xfstests is notorious for
1482 * failing tests because blkid via udev races with a losetup
1483 * <dev>/do something like mkfs/losetup -d <dev> causing the losetup -d
1484 * command to fail with EBUSY.
1485 */
1486 if (atomic_read(&lo->lo_refcnt) > 1) {
1487 lo->lo_flags |= LO_FLAGS_AUTOCLEAR;
Pavel Tatashin6cc8e742021-01-26 09:46:30 -05001488 mutex_unlock(&lo->lo_mutex);
Jan Karaa2505b72018-11-08 14:01:06 +01001489 return 0;
1490 }
1491 lo->lo_state = Lo_rundown;
Pavel Tatashin6cc8e742021-01-26 09:46:30 -05001492 mutex_unlock(&lo->lo_mutex);
Jan Karaa2505b72018-11-08 14:01:06 +01001493
Jan Kara0da03ca2018-11-08 14:01:14 +01001494 return __loop_clr_fd(lo, false);
Jan Karaa2505b72018-11-08 14:01:06 +01001495}
1496
Linus Torvalds1da177e2005-04-16 15:20:36 -07001497static int
1498loop_set_status(struct loop_device *lo, const struct loop_info64 *info)
1499{
1500 int err;
Martijn Coenen0c3796c2020-05-13 15:38:41 +02001501 kuid_t uid = current_uid();
Martijn Coenenfaf1d252020-05-13 15:38:44 +02001502 int prev_lo_flags;
Jan Kara85b0a542018-11-08 14:01:13 +01001503 bool partscan = false;
Martijn Coenen0c3796c2020-05-13 15:38:41 +02001504 bool size_changed = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001505
Pavel Tatashin6cc8e742021-01-26 09:46:30 -05001506 err = mutex_lock_killable(&lo->lo_mutex);
Jan Kara550df5fd2018-11-08 14:01:09 +01001507 if (err)
1508 return err;
David Howellsb0fafa82008-11-14 10:38:41 +11001509 if (lo->lo_encrypt_key_size &&
Eric W. Biedermane4849732012-02-11 11:23:51 -08001510 !uid_eq(lo->lo_key_owner, uid) &&
Jan Kara550df5fd2018-11-08 14:01:09 +01001511 !capable(CAP_SYS_ADMIN)) {
1512 err = -EPERM;
1513 goto out_unlock;
1514 }
1515 if (lo->lo_state != Lo_bound) {
1516 err = -ENXIO;
1517 goto out_unlock;
1518 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001519
Jaegeuk Kim5db470e2019-01-09 19:17:14 -08001520 if (lo->lo_offset != info->lo_offset ||
1521 lo->lo_sizelimit != info->lo_sizelimit) {
Martijn Coenen0c3796c2020-05-13 15:38:41 +02001522 size_changed = true;
Jaegeuk Kim5db470e2019-01-09 19:17:14 -08001523 sync_blockdev(lo->lo_device);
Zheng Binf4bd34b2020-06-18 12:21:37 +08001524 invalidate_bdev(lo->lo_device);
Jaegeuk Kim5db470e2019-01-09 19:17:14 -08001525 }
1526
Ming Leiecdd0952017-02-11 11:40:45 +08001527 /* I/O need to be drained during transfer transition */
1528 blk_mq_freeze_queue(lo->lo_queue);
1529
Martijn Coenen0c3796c2020-05-13 15:38:41 +02001530 if (size_changed && lo->lo_device->bd_inode->i_mapping->nrpages) {
Zheng Binf4bd34b2020-06-18 12:21:37 +08001531 /* If any pages were dirtied after invalidate_bdev(), try again */
Martijn Coenen0c3796c2020-05-13 15:38:41 +02001532 err = -EAGAIN;
1533 pr_warn("%s: loop%d (%s) has still dirty pages (nrpages=%lu)\n",
1534 __func__, lo->lo_number, lo->lo_file_name,
1535 lo->lo_device->bd_inode->i_mapping->nrpages);
1536 goto out_unfreeze;
1537 }
1538
Martijn Coenenfaf1d252020-05-13 15:38:44 +02001539 prev_lo_flags = lo->lo_flags;
1540
Martijn Coenen0c3796c2020-05-13 15:38:41 +02001541 err = loop_set_status_from_info(lo, info);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001542 if (err)
Jan Kara550df5fd2018-11-08 14:01:09 +01001543 goto out_unfreeze;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001544
Martijn Coenenfaf1d252020-05-13 15:38:44 +02001545 /* Mask out flags that can't be set using LOOP_SET_STATUS. */
Martijn Coenen6ac92fb2020-06-04 22:25:20 +02001546 lo->lo_flags &= LOOP_SET_STATUS_SETTABLE_FLAGS;
Martijn Coenenfaf1d252020-05-13 15:38:44 +02001547 /* For those flags, use the previous values instead */
1548 lo->lo_flags |= prev_lo_flags & ~LOOP_SET_STATUS_SETTABLE_FLAGS;
1549 /* For flags that can't be cleared, use previous values too */
1550 lo->lo_flags |= prev_lo_flags & ~LOOP_SET_STATUS_CLEARABLE_FLAGS;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001551
Martijn Coenenb0bd1582020-05-13 15:38:39 +02001552 if (size_changed) {
1553 loff_t new_size = get_size(lo->lo_offset, lo->lo_sizelimit,
1554 lo->lo_backing_file);
1555 loop_set_size(lo, new_size);
Arnd Bergmannb040ad92017-06-09 12:19:18 +02001556 }
Guo Chao541c7422013-02-21 15:16:46 -08001557
Lukas Czernerdfaa2ef2011-08-19 14:50:46 +02001558 loop_config_discard(lo);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001559
Ming Lei2e5ab5f2015-08-17 10:31:49 +08001560 /* update dio if lo_offset or transfer is changed */
1561 __loop_update_dio(lo, lo->use_dio);
1562
Jan Kara550df5fd2018-11-08 14:01:09 +01001563out_unfreeze:
Ming Leiecdd0952017-02-11 11:40:45 +08001564 blk_mq_unfreeze_queue(lo->lo_queue);
Omar Sandovale02898b42017-03-01 10:42:38 -08001565
Martijn Coenenfaf1d252020-05-13 15:38:44 +02001566 if (!err && (lo->lo_flags & LO_FLAGS_PARTSCAN) &&
1567 !(prev_lo_flags & LO_FLAGS_PARTSCAN)) {
Omar Sandovale02898b42017-03-01 10:42:38 -08001568 lo->lo_disk->flags &= ~GENHD_FL_NO_PART_SCAN;
Jan Kara85b0a542018-11-08 14:01:13 +01001569 partscan = true;
Omar Sandovale02898b42017-03-01 10:42:38 -08001570 }
Jan Kara550df5fd2018-11-08 14:01:09 +01001571out_unlock:
Pavel Tatashin6cc8e742021-01-26 09:46:30 -05001572 mutex_unlock(&lo->lo_mutex);
Jan Kara85b0a542018-11-08 14:01:13 +01001573 if (partscan)
Christoph Hellwig03842642021-06-24 14:32:40 +02001574 loop_reread_partitions(lo);
Omar Sandovale02898b42017-03-01 10:42:38 -08001575
Ming Leiecdd0952017-02-11 11:40:45 +08001576 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001577}
1578
1579static int
1580loop_get_status(struct loop_device *lo, struct loop_info64 *info)
1581{
Tetsuo Handab1ab5fa2018-11-08 14:01:01 +01001582 struct path path;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001583 struct kstat stat;
Omar Sandoval2d1d4c12018-03-26 21:39:11 -07001584 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001585
Pavel Tatashin6cc8e742021-01-26 09:46:30 -05001586 ret = mutex_lock_killable(&lo->lo_mutex);
Jan Kara4a5ce9b2018-11-08 14:01:08 +01001587 if (ret)
1588 return ret;
Omar Sandoval2d1d4c12018-03-26 21:39:11 -07001589 if (lo->lo_state != Lo_bound) {
Pavel Tatashin6cc8e742021-01-26 09:46:30 -05001590 mutex_unlock(&lo->lo_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001591 return -ENXIO;
Omar Sandoval2d1d4c12018-03-26 21:39:11 -07001592 }
1593
Linus Torvalds1da177e2005-04-16 15:20:36 -07001594 memset(info, 0, sizeof(*info));
1595 info->lo_number = lo->lo_number;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001596 info->lo_offset = lo->lo_offset;
1597 info->lo_sizelimit = lo->lo_sizelimit;
1598 info->lo_flags = lo->lo_flags;
1599 memcpy(info->lo_file_name, lo->lo_file_name, LO_NAME_SIZE);
1600 memcpy(info->lo_crypt_name, lo->lo_crypt_name, LO_NAME_SIZE);
1601 info->lo_encrypt_type =
1602 lo->lo_encryption ? lo->lo_encryption->number : 0;
1603 if (lo->lo_encrypt_key_size && capable(CAP_SYS_ADMIN)) {
1604 info->lo_encrypt_key_size = lo->lo_encrypt_key_size;
1605 memcpy(info->lo_encrypt_key, lo->lo_encrypt_key,
1606 lo->lo_encrypt_key_size);
1607 }
Omar Sandoval2d1d4c12018-03-26 21:39:11 -07001608
Pavel Tatashin6cc8e742021-01-26 09:46:30 -05001609 /* Drop lo_mutex while we call into the filesystem. */
Tetsuo Handab1ab5fa2018-11-08 14:01:01 +01001610 path = lo->lo_backing_file->f_path;
1611 path_get(&path);
Pavel Tatashin6cc8e742021-01-26 09:46:30 -05001612 mutex_unlock(&lo->lo_mutex);
Tetsuo Handab1ab5fa2018-11-08 14:01:01 +01001613 ret = vfs_getattr(&path, &stat, STATX_INO, AT_STATX_SYNC_AS_STAT);
Omar Sandoval2d1d4c12018-03-26 21:39:11 -07001614 if (!ret) {
1615 info->lo_device = huge_encode_dev(stat.dev);
1616 info->lo_inode = stat.ino;
1617 info->lo_rdevice = huge_encode_dev(stat.rdev);
1618 }
Tetsuo Handab1ab5fa2018-11-08 14:01:01 +01001619 path_put(&path);
Omar Sandoval2d1d4c12018-03-26 21:39:11 -07001620 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001621}
1622
1623static void
1624loop_info64_from_old(const struct loop_info *info, struct loop_info64 *info64)
1625{
1626 memset(info64, 0, sizeof(*info64));
1627 info64->lo_number = info->lo_number;
1628 info64->lo_device = info->lo_device;
1629 info64->lo_inode = info->lo_inode;
1630 info64->lo_rdevice = info->lo_rdevice;
1631 info64->lo_offset = info->lo_offset;
1632 info64->lo_sizelimit = 0;
1633 info64->lo_encrypt_type = info->lo_encrypt_type;
1634 info64->lo_encrypt_key_size = info->lo_encrypt_key_size;
1635 info64->lo_flags = info->lo_flags;
1636 info64->lo_init[0] = info->lo_init[0];
1637 info64->lo_init[1] = info->lo_init[1];
1638 if (info->lo_encrypt_type == LO_CRYPT_CRYPTOAPI)
1639 memcpy(info64->lo_crypt_name, info->lo_name, LO_NAME_SIZE);
1640 else
1641 memcpy(info64->lo_file_name, info->lo_name, LO_NAME_SIZE);
1642 memcpy(info64->lo_encrypt_key, info->lo_encrypt_key, LO_KEY_SIZE);
1643}
1644
1645static int
1646loop_info64_to_old(const struct loop_info64 *info64, struct loop_info *info)
1647{
1648 memset(info, 0, sizeof(*info));
1649 info->lo_number = info64->lo_number;
1650 info->lo_device = info64->lo_device;
1651 info->lo_inode = info64->lo_inode;
1652 info->lo_rdevice = info64->lo_rdevice;
1653 info->lo_offset = info64->lo_offset;
1654 info->lo_encrypt_type = info64->lo_encrypt_type;
1655 info->lo_encrypt_key_size = info64->lo_encrypt_key_size;
1656 info->lo_flags = info64->lo_flags;
1657 info->lo_init[0] = info64->lo_init[0];
1658 info->lo_init[1] = info64->lo_init[1];
1659 if (info->lo_encrypt_type == LO_CRYPT_CRYPTOAPI)
1660 memcpy(info->lo_name, info64->lo_crypt_name, LO_NAME_SIZE);
1661 else
1662 memcpy(info->lo_name, info64->lo_file_name, LO_NAME_SIZE);
1663 memcpy(info->lo_encrypt_key, info64->lo_encrypt_key, LO_KEY_SIZE);
1664
1665 /* error in case values were truncated */
1666 if (info->lo_device != info64->lo_device ||
1667 info->lo_rdevice != info64->lo_rdevice ||
1668 info->lo_inode != info64->lo_inode ||
1669 info->lo_offset != info64->lo_offset)
1670 return -EOVERFLOW;
1671
1672 return 0;
1673}
1674
1675static int
1676loop_set_status_old(struct loop_device *lo, const struct loop_info __user *arg)
1677{
1678 struct loop_info info;
1679 struct loop_info64 info64;
1680
1681 if (copy_from_user(&info, arg, sizeof (struct loop_info)))
1682 return -EFAULT;
1683 loop_info64_from_old(&info, &info64);
1684 return loop_set_status(lo, &info64);
1685}
1686
1687static int
1688loop_set_status64(struct loop_device *lo, const struct loop_info64 __user *arg)
1689{
1690 struct loop_info64 info64;
1691
1692 if (copy_from_user(&info64, arg, sizeof (struct loop_info64)))
1693 return -EFAULT;
1694 return loop_set_status(lo, &info64);
1695}
1696
1697static int
1698loop_get_status_old(struct loop_device *lo, struct loop_info __user *arg) {
1699 struct loop_info info;
1700 struct loop_info64 info64;
Omar Sandovalbdac616db2018-04-06 09:57:03 -07001701 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001702
Jan Kara4a5ce9b2018-11-08 14:01:08 +01001703 if (!arg)
Omar Sandovalbdac616db2018-04-06 09:57:03 -07001704 return -EINVAL;
Omar Sandovalbdac616db2018-04-06 09:57:03 -07001705 err = loop_get_status(lo, &info64);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001706 if (!err)
1707 err = loop_info64_to_old(&info64, &info);
1708 if (!err && copy_to_user(arg, &info, sizeof(info)))
1709 err = -EFAULT;
1710
1711 return err;
1712}
1713
1714static int
1715loop_get_status64(struct loop_device *lo, struct loop_info64 __user *arg) {
1716 struct loop_info64 info64;
Omar Sandovalbdac616db2018-04-06 09:57:03 -07001717 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001718
Jan Kara4a5ce9b2018-11-08 14:01:08 +01001719 if (!arg)
Omar Sandovalbdac616db2018-04-06 09:57:03 -07001720 return -EINVAL;
Omar Sandovalbdac616db2018-04-06 09:57:03 -07001721 err = loop_get_status(lo, &info64);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001722 if (!err && copy_to_user(arg, &info64, sizeof(info64)))
1723 err = -EFAULT;
1724
1725 return err;
1726}
1727
Hannes Reinecke51001b72017-06-08 13:46:44 +02001728static int loop_set_capacity(struct loop_device *lo)
J. R. Okajima53d66602009-03-31 15:23:43 -07001729{
Martijn Coenen0a6ed1b2020-05-13 15:38:40 +02001730 loff_t size;
1731
J. R. Okajima53d66602009-03-31 15:23:43 -07001732 if (unlikely(lo->lo_state != Lo_bound))
Guo Chao7b0576a2013-02-21 15:16:47 -08001733 return -ENXIO;
J. R. Okajima53d66602009-03-31 15:23:43 -07001734
Martijn Coenen0a6ed1b2020-05-13 15:38:40 +02001735 size = get_loop_size(lo, lo->lo_backing_file);
1736 loop_set_size(lo, size);
Martijn Coenen083a6a52020-05-13 15:38:36 +02001737
1738 return 0;
J. R. Okajima53d66602009-03-31 15:23:43 -07001739}
1740
Ming Leiab1cb272015-08-17 10:31:50 +08001741static int loop_set_dio(struct loop_device *lo, unsigned long arg)
1742{
1743 int error = -ENXIO;
1744 if (lo->lo_state != Lo_bound)
1745 goto out;
1746
1747 __loop_update_dio(lo, !!arg);
1748 if (lo->use_dio == !!arg)
1749 return 0;
1750 error = -EINVAL;
1751 out:
1752 return error;
1753}
1754
Omar Sandoval89e4fde2017-08-24 00:03:43 -07001755static int loop_set_block_size(struct loop_device *lo, unsigned long arg)
1756{
Jaegeuk Kim5db470e2019-01-09 19:17:14 -08001757 int err = 0;
1758
Omar Sandoval89e4fde2017-08-24 00:03:43 -07001759 if (lo->lo_state != Lo_bound)
1760 return -ENXIO;
1761
Martijn Coenen34489142020-05-13 15:38:45 +02001762 err = loop_validate_block_size(arg);
1763 if (err)
1764 return err;
Omar Sandoval89e4fde2017-08-24 00:03:43 -07001765
Martijn Coenen7e81f992020-03-10 14:12:30 +01001766 if (lo->lo_queue->limits.logical_block_size == arg)
1767 return 0;
1768
1769 sync_blockdev(lo->lo_device);
Zheng Binf4bd34b2020-06-18 12:21:37 +08001770 invalidate_bdev(lo->lo_device);
Jaegeuk Kim5db470e2019-01-09 19:17:14 -08001771
Omar Sandoval89e4fde2017-08-24 00:03:43 -07001772 blk_mq_freeze_queue(lo->lo_queue);
1773
Zheng Binf4bd34b2020-06-18 12:21:37 +08001774 /* invalidate_bdev should have truncated all the pages */
Martijn Coenen7e81f992020-03-10 14:12:30 +01001775 if (lo->lo_device->bd_inode->i_mapping->nrpages) {
Jaegeuk Kim5db470e2019-01-09 19:17:14 -08001776 err = -EAGAIN;
1777 pr_warn("%s: loop%d (%s) has still dirty pages (nrpages=%lu)\n",
1778 __func__, lo->lo_number, lo->lo_file_name,
1779 lo->lo_device->bd_inode->i_mapping->nrpages);
1780 goto out_unfreeze;
1781 }
1782
Omar Sandoval89e4fde2017-08-24 00:03:43 -07001783 blk_queue_logical_block_size(lo->lo_queue, arg);
Omar Sandovalbf093752017-09-05 14:24:47 -07001784 blk_queue_physical_block_size(lo->lo_queue, arg);
1785 blk_queue_io_min(lo->lo_queue, arg);
Omar Sandoval89e4fde2017-08-24 00:03:43 -07001786 loop_update_dio(lo);
Jaegeuk Kim5db470e2019-01-09 19:17:14 -08001787out_unfreeze:
Omar Sandoval89e4fde2017-08-24 00:03:43 -07001788 blk_mq_unfreeze_queue(lo->lo_queue);
1789
Jaegeuk Kim5db470e2019-01-09 19:17:14 -08001790 return err;
Omar Sandoval89e4fde2017-08-24 00:03:43 -07001791}
1792
Jan Karaa1316542018-11-08 14:01:05 +01001793static int lo_simple_ioctl(struct loop_device *lo, unsigned int cmd,
1794 unsigned long arg)
1795{
1796 int err;
1797
Pavel Tatashin6cc8e742021-01-26 09:46:30 -05001798 err = mutex_lock_killable(&lo->lo_mutex);
Jan Karaa1316542018-11-08 14:01:05 +01001799 if (err)
1800 return err;
1801 switch (cmd) {
1802 case LOOP_SET_CAPACITY:
1803 err = loop_set_capacity(lo);
1804 break;
1805 case LOOP_SET_DIRECT_IO:
1806 err = loop_set_dio(lo, arg);
1807 break;
1808 case LOOP_SET_BLOCK_SIZE:
1809 err = loop_set_block_size(lo, arg);
1810 break;
1811 default:
1812 err = lo->ioctl ? lo->ioctl(lo, cmd, arg) : -EINVAL;
1813 }
Pavel Tatashin6cc8e742021-01-26 09:46:30 -05001814 mutex_unlock(&lo->lo_mutex);
Jan Karaa1316542018-11-08 14:01:05 +01001815 return err;
1816}
1817
Al Virobb214882008-03-02 09:29:48 -05001818static int lo_ioctl(struct block_device *bdev, fmode_t mode,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001819 unsigned int cmd, unsigned long arg)
1820{
Al Virobb214882008-03-02 09:29:48 -05001821 struct loop_device *lo = bdev->bd_disk->private_data;
Martijn Coenen571fae62020-05-13 15:38:43 +02001822 void __user *argp = (void __user *) arg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001823 int err;
1824
Linus Torvalds1da177e2005-04-16 15:20:36 -07001825 switch (cmd) {
Martijn Coenen34489142020-05-13 15:38:45 +02001826 case LOOP_SET_FD: {
1827 /*
1828 * Legacy case - pass in a zeroed out struct loop_config with
1829 * only the file descriptor set , which corresponds with the
1830 * default parameters we'd have used otherwise.
1831 */
1832 struct loop_config config;
1833
1834 memset(&config, 0, sizeof(config));
1835 config.fd = arg;
1836
1837 return loop_configure(lo, mode, bdev, &config);
1838 }
1839 case LOOP_CONFIGURE: {
1840 struct loop_config config;
1841
1842 if (copy_from_user(&config, argp, sizeof(config)))
1843 return -EFAULT;
1844
1845 return loop_configure(lo, mode, bdev, &config);
1846 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001847 case LOOP_CHANGE_FD:
Jan Karac3710772018-11-08 14:01:11 +01001848 return loop_change_fd(lo, bdev, arg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001849 case LOOP_CLR_FD:
Jan Kara7ccd0792018-11-08 14:01:07 +01001850 return loop_clr_fd(lo);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001851 case LOOP_SET_STATUS:
Dmitry Monakhov7035b5d2011-11-16 09:21:49 +01001852 err = -EPERM;
Jan Karaa1316542018-11-08 14:01:05 +01001853 if ((mode & FMODE_WRITE) || capable(CAP_SYS_ADMIN)) {
Martijn Coenen571fae62020-05-13 15:38:43 +02001854 err = loop_set_status_old(lo, argp);
Jan Karaa1316542018-11-08 14:01:05 +01001855 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001856 break;
1857 case LOOP_GET_STATUS:
Martijn Coenen571fae62020-05-13 15:38:43 +02001858 return loop_get_status_old(lo, argp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001859 case LOOP_SET_STATUS64:
Dmitry Monakhov7035b5d2011-11-16 09:21:49 +01001860 err = -EPERM;
Jan Karaa1316542018-11-08 14:01:05 +01001861 if ((mode & FMODE_WRITE) || capable(CAP_SYS_ADMIN)) {
Martijn Coenen571fae62020-05-13 15:38:43 +02001862 err = loop_set_status64(lo, argp);
Jan Karaa1316542018-11-08 14:01:05 +01001863 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001864 break;
1865 case LOOP_GET_STATUS64:
Martijn Coenen571fae62020-05-13 15:38:43 +02001866 return loop_get_status64(lo, argp);
J. R. Okajima53d66602009-03-31 15:23:43 -07001867 case LOOP_SET_CAPACITY:
Ming Leiab1cb272015-08-17 10:31:50 +08001868 case LOOP_SET_DIRECT_IO:
Omar Sandoval89e4fde2017-08-24 00:03:43 -07001869 case LOOP_SET_BLOCK_SIZE:
Jan Karaa1316542018-11-08 14:01:05 +01001870 if (!(mode & FMODE_WRITE) && !capable(CAP_SYS_ADMIN))
1871 return -EPERM;
Gustavo A. R. Silvadf561f662020-08-23 17:36:59 -05001872 fallthrough;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001873 default:
Jan Karaa1316542018-11-08 14:01:05 +01001874 err = lo_simple_ioctl(lo, cmd, arg);
1875 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001876 }
Nikanth Karthikesanf028f3b2009-03-24 12:33:41 +01001877
Linus Torvalds1da177e2005-04-16 15:20:36 -07001878 return err;
1879}
1880
David Howells863d5b822006-08-29 19:06:14 +01001881#ifdef CONFIG_COMPAT
1882struct compat_loop_info {
1883 compat_int_t lo_number; /* ioctl r/o */
1884 compat_dev_t lo_device; /* ioctl r/o */
1885 compat_ulong_t lo_inode; /* ioctl r/o */
1886 compat_dev_t lo_rdevice; /* ioctl r/o */
1887 compat_int_t lo_offset;
1888 compat_int_t lo_encrypt_type;
1889 compat_int_t lo_encrypt_key_size; /* ioctl w/o */
1890 compat_int_t lo_flags; /* ioctl r/o */
1891 char lo_name[LO_NAME_SIZE];
1892 unsigned char lo_encrypt_key[LO_KEY_SIZE]; /* ioctl w/o */
1893 compat_ulong_t lo_init[2];
1894 char reserved[4];
1895};
1896
1897/*
1898 * Transfer 32-bit compatibility structure in userspace to 64-bit loop info
1899 * - noinlined to reduce stack space usage in main part of driver
1900 */
1901static noinline int
Al Viroba674cf2006-10-10 22:48:27 +01001902loop_info64_from_compat(const struct compat_loop_info __user *arg,
David Howells863d5b822006-08-29 19:06:14 +01001903 struct loop_info64 *info64)
1904{
1905 struct compat_loop_info info;
1906
1907 if (copy_from_user(&info, arg, sizeof(info)))
1908 return -EFAULT;
1909
1910 memset(info64, 0, sizeof(*info64));
1911 info64->lo_number = info.lo_number;
1912 info64->lo_device = info.lo_device;
1913 info64->lo_inode = info.lo_inode;
1914 info64->lo_rdevice = info.lo_rdevice;
1915 info64->lo_offset = info.lo_offset;
1916 info64->lo_sizelimit = 0;
1917 info64->lo_encrypt_type = info.lo_encrypt_type;
1918 info64->lo_encrypt_key_size = info.lo_encrypt_key_size;
1919 info64->lo_flags = info.lo_flags;
1920 info64->lo_init[0] = info.lo_init[0];
1921 info64->lo_init[1] = info.lo_init[1];
1922 if (info.lo_encrypt_type == LO_CRYPT_CRYPTOAPI)
1923 memcpy(info64->lo_crypt_name, info.lo_name, LO_NAME_SIZE);
1924 else
1925 memcpy(info64->lo_file_name, info.lo_name, LO_NAME_SIZE);
1926 memcpy(info64->lo_encrypt_key, info.lo_encrypt_key, LO_KEY_SIZE);
1927 return 0;
1928}
1929
1930/*
1931 * Transfer 64-bit loop info to 32-bit compatibility structure in userspace
1932 * - noinlined to reduce stack space usage in main part of driver
1933 */
1934static noinline int
1935loop_info64_to_compat(const struct loop_info64 *info64,
1936 struct compat_loop_info __user *arg)
1937{
1938 struct compat_loop_info info;
1939
1940 memset(&info, 0, sizeof(info));
1941 info.lo_number = info64->lo_number;
1942 info.lo_device = info64->lo_device;
1943 info.lo_inode = info64->lo_inode;
1944 info.lo_rdevice = info64->lo_rdevice;
1945 info.lo_offset = info64->lo_offset;
1946 info.lo_encrypt_type = info64->lo_encrypt_type;
1947 info.lo_encrypt_key_size = info64->lo_encrypt_key_size;
1948 info.lo_flags = info64->lo_flags;
1949 info.lo_init[0] = info64->lo_init[0];
1950 info.lo_init[1] = info64->lo_init[1];
1951 if (info.lo_encrypt_type == LO_CRYPT_CRYPTOAPI)
1952 memcpy(info.lo_name, info64->lo_crypt_name, LO_NAME_SIZE);
1953 else
1954 memcpy(info.lo_name, info64->lo_file_name, LO_NAME_SIZE);
1955 memcpy(info.lo_encrypt_key, info64->lo_encrypt_key, LO_KEY_SIZE);
1956
1957 /* error in case values were truncated */
1958 if (info.lo_device != info64->lo_device ||
1959 info.lo_rdevice != info64->lo_rdevice ||
1960 info.lo_inode != info64->lo_inode ||
1961 info.lo_offset != info64->lo_offset ||
1962 info.lo_init[0] != info64->lo_init[0] ||
1963 info.lo_init[1] != info64->lo_init[1])
1964 return -EOVERFLOW;
1965
1966 if (copy_to_user(arg, &info, sizeof(info)))
1967 return -EFAULT;
1968 return 0;
1969}
1970
1971static int
1972loop_set_status_compat(struct loop_device *lo,
1973 const struct compat_loop_info __user *arg)
1974{
1975 struct loop_info64 info64;
1976 int ret;
1977
1978 ret = loop_info64_from_compat(arg, &info64);
1979 if (ret < 0)
1980 return ret;
1981 return loop_set_status(lo, &info64);
1982}
1983
1984static int
1985loop_get_status_compat(struct loop_device *lo,
1986 struct compat_loop_info __user *arg)
1987{
1988 struct loop_info64 info64;
Omar Sandovalbdac616db2018-04-06 09:57:03 -07001989 int err;
David Howells863d5b822006-08-29 19:06:14 +01001990
Jan Kara4a5ce9b2018-11-08 14:01:08 +01001991 if (!arg)
Omar Sandovalbdac616db2018-04-06 09:57:03 -07001992 return -EINVAL;
Omar Sandovalbdac616db2018-04-06 09:57:03 -07001993 err = loop_get_status(lo, &info64);
David Howells863d5b822006-08-29 19:06:14 +01001994 if (!err)
1995 err = loop_info64_to_compat(&info64, arg);
1996 return err;
1997}
1998
Al Virobb214882008-03-02 09:29:48 -05001999static int lo_compat_ioctl(struct block_device *bdev, fmode_t mode,
2000 unsigned int cmd, unsigned long arg)
David Howells863d5b822006-08-29 19:06:14 +01002001{
Al Virobb214882008-03-02 09:29:48 -05002002 struct loop_device *lo = bdev->bd_disk->private_data;
David Howells863d5b822006-08-29 19:06:14 +01002003 int err;
2004
David Howells863d5b822006-08-29 19:06:14 +01002005 switch(cmd) {
2006 case LOOP_SET_STATUS:
Jan Kara550df5fd2018-11-08 14:01:09 +01002007 err = loop_set_status_compat(lo,
2008 (const struct compat_loop_info __user *)arg);
David Howells863d5b822006-08-29 19:06:14 +01002009 break;
2010 case LOOP_GET_STATUS:
Jan Kara4a5ce9b2018-11-08 14:01:08 +01002011 err = loop_get_status_compat(lo,
2012 (struct compat_loop_info __user *)arg);
David Howells863d5b822006-08-29 19:06:14 +01002013 break;
J. R. Okajima53d66602009-03-31 15:23:43 -07002014 case LOOP_SET_CAPACITY:
David Howells863d5b822006-08-29 19:06:14 +01002015 case LOOP_CLR_FD:
2016 case LOOP_GET_STATUS64:
2017 case LOOP_SET_STATUS64:
Martijn Coenen34489142020-05-13 15:38:45 +02002018 case LOOP_CONFIGURE:
David Howells863d5b822006-08-29 19:06:14 +01002019 arg = (unsigned long) compat_ptr(arg);
Gustavo A. R. Silvadf561f662020-08-23 17:36:59 -05002020 fallthrough;
David Howells863d5b822006-08-29 19:06:14 +01002021 case LOOP_SET_FD:
2022 case LOOP_CHANGE_FD:
Evan Green9fea4b32018-07-02 16:03:46 -07002023 case LOOP_SET_BLOCK_SIZE:
Alessio Balsinifdbe4ee2019-08-07 01:48:28 +01002024 case LOOP_SET_DIRECT_IO:
Al Virobb214882008-03-02 09:29:48 -05002025 err = lo_ioctl(bdev, mode, cmd, arg);
David Howells863d5b822006-08-29 19:06:14 +01002026 break;
2027 default:
2028 err = -ENOIOCTLCMD;
2029 break;
2030 }
David Howells863d5b822006-08-29 19:06:14 +01002031 return err;
2032}
2033#endif
2034
Al Virobb214882008-03-02 09:29:48 -05002035static int lo_open(struct block_device *bdev, fmode_t mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002036{
Christoph Hellwig990e7812021-06-05 17:09:50 +03002037 struct loop_device *lo = bdev->bd_disk->private_data;
Jan Kara0a42e992018-11-08 14:01:04 +01002038 int err;
Kay Sievers770fe302011-07-31 22:08:04 +02002039
Pavel Tatashin6cc8e742021-01-26 09:46:30 -05002040 err = mutex_lock_killable(&lo->lo_mutex);
Pavel Tatashin6cc8e742021-01-26 09:46:30 -05002041 if (err)
2042 return err;
Christoph Hellwig990e7812021-06-05 17:09:50 +03002043 if (lo->lo_state == Lo_deleting)
2044 err = -ENXIO;
2045 else
2046 atomic_inc(&lo->lo_refcnt);
Pavel Tatashin6cc8e742021-01-26 09:46:30 -05002047 mutex_unlock(&lo->lo_mutex);
Christoph Hellwig990e7812021-06-05 17:09:50 +03002048 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002049}
2050
Jan Kara967d1dc2018-11-08 14:01:03 +01002051static void lo_release(struct gendisk *disk, fmode_t mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002052{
Pavel Tatashin6cc8e742021-01-26 09:46:30 -05002053 struct loop_device *lo = disk->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002054
Pavel Tatashin6cc8e742021-01-26 09:46:30 -05002055 mutex_lock(&lo->lo_mutex);
Ming Leif8933662015-05-06 12:26:23 +08002056 if (atomic_dec_return(&lo->lo_refcnt))
Jan Kara0a42e992018-11-08 14:01:04 +01002057 goto out_unlock;
Ming Leif8933662015-05-06 12:26:23 +08002058
Milan Broz14f27932008-12-12 14:48:27 +01002059 if (lo->lo_flags & LO_FLAGS_AUTOCLEAR) {
Jan Karaa2505b72018-11-08 14:01:06 +01002060 if (lo->lo_state != Lo_bound)
2061 goto out_unlock;
2062 lo->lo_state = Lo_rundown;
Pavel Tatashin6cc8e742021-01-26 09:46:30 -05002063 mutex_unlock(&lo->lo_mutex);
Milan Broz14f27932008-12-12 14:48:27 +01002064 /*
2065 * In autoclear mode, stop the loop thread
2066 * and remove configuration after last close.
2067 */
Jan Kara0da03ca2018-11-08 14:01:14 +01002068 __loop_clr_fd(lo, true);
Jan Kara7ccd0792018-11-08 14:01:07 +01002069 return;
Omar Sandoval43cade802017-08-24 00:03:44 -07002070 } else if (lo->lo_state == Lo_bound) {
Milan Broz14f27932008-12-12 14:48:27 +01002071 /*
2072 * Otherwise keep thread (if running) and config,
2073 * but flush possible ongoing bios in thread.
2074 */
Omar Sandoval43cade802017-08-24 00:03:44 -07002075 blk_mq_freeze_queue(lo->lo_queue);
2076 blk_mq_unfreeze_queue(lo->lo_queue);
Milan Broz14f27932008-12-12 14:48:27 +01002077 }
David Woodhouse96c58652008-02-06 01:36:27 -08002078
Jan Kara0a42e992018-11-08 14:01:04 +01002079out_unlock:
Pavel Tatashin6cc8e742021-01-26 09:46:30 -05002080 mutex_unlock(&lo->lo_mutex);
Linus Torvaldsae665012018-01-05 16:26:00 -08002081}
2082
Alexey Dobriyan83d5cde2009-09-21 17:01:13 -07002083static const struct block_device_operations lo_fops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002084 .owner = THIS_MODULE,
Al Virobb214882008-03-02 09:29:48 -05002085 .open = lo_open,
2086 .release = lo_release,
2087 .ioctl = lo_ioctl,
David Howells863d5b822006-08-29 19:06:14 +01002088#ifdef CONFIG_COMPAT
Al Virobb214882008-03-02 09:29:48 -05002089 .compat_ioctl = lo_compat_ioctl,
David Howells863d5b822006-08-29 19:06:14 +01002090#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07002091};
2092
2093/*
2094 * And now the modules code and kernel interface.
2095 */
Ken Chen73285082007-05-08 00:28:20 -07002096static int max_loop;
Joe Perches5657a812018-05-24 13:38:59 -06002097module_param(max_loop, int, 0444);
Ken Chena47653f2007-06-08 13:46:44 -07002098MODULE_PARM_DESC(max_loop, "Maximum number of loop devices");
Joe Perches5657a812018-05-24 13:38:59 -06002099module_param(max_part, int, 0444);
Laurent Vivier476a4812008-03-26 12:11:53 +01002100MODULE_PARM_DESC(max_part, "Maximum number of partitions per loop device");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002101MODULE_LICENSE("GPL");
2102MODULE_ALIAS_BLOCKDEV_MAJOR(LOOP_MAJOR);
2103
2104int loop_register_transfer(struct loop_func_table *funcs)
2105{
2106 unsigned int n = funcs->number;
2107
2108 if (n >= MAX_LO_CRYPT || xfer_funcs[n])
2109 return -EINVAL;
2110 xfer_funcs[n] = funcs;
2111 return 0;
2112}
2113
Kay Sievers34dd82a2011-07-31 22:08:04 +02002114static int unregister_transfer_cb(int id, void *ptr, void *data)
2115{
2116 struct loop_device *lo = ptr;
2117 struct loop_func_table *xfer = data;
2118
Pavel Tatashin6cc8e742021-01-26 09:46:30 -05002119 mutex_lock(&lo->lo_mutex);
Kay Sievers34dd82a2011-07-31 22:08:04 +02002120 if (lo->lo_encryption == xfer)
2121 loop_release_xfer(lo);
Pavel Tatashin6cc8e742021-01-26 09:46:30 -05002122 mutex_unlock(&lo->lo_mutex);
Kay Sievers34dd82a2011-07-31 22:08:04 +02002123 return 0;
2124}
2125
Linus Torvalds1da177e2005-04-16 15:20:36 -07002126int loop_unregister_transfer(int number)
2127{
2128 unsigned int n = number;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002129 struct loop_func_table *xfer;
2130
2131 if (n == 0 || n >= MAX_LO_CRYPT || (xfer = xfer_funcs[n]) == NULL)
2132 return -EINVAL;
2133
2134 xfer_funcs[n] = NULL;
Kay Sievers34dd82a2011-07-31 22:08:04 +02002135 idr_for_each(&loop_index_idr, &unregister_transfer_cb, xfer);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002136 return 0;
2137}
2138
2139EXPORT_SYMBOL(loop_register_transfer);
2140EXPORT_SYMBOL(loop_unregister_transfer);
2141
Christoph Hellwigfc17b652017-06-03 09:38:05 +02002142static blk_status_t loop_queue_rq(struct blk_mq_hw_ctx *hctx,
Ming Leib5dd2f62014-12-31 13:22:57 +00002143 const struct blk_mq_queue_data *bd)
2144{
Jens Axboe1894e912018-04-13 16:24:29 -06002145 struct request *rq = bd->rq;
2146 struct loop_cmd *cmd = blk_mq_rq_to_pdu(rq);
2147 struct loop_device *lo = rq->q->queuedata;
Ming Leib5dd2f62014-12-31 13:22:57 +00002148
Jens Axboe1894e912018-04-13 16:24:29 -06002149 blk_mq_start_request(rq);
Ming Leib5dd2f62014-12-31 13:22:57 +00002150
Ming Leif4aa4c72015-05-05 19:49:54 +08002151 if (lo->lo_state != Lo_bound)
Christoph Hellwigfc17b652017-06-03 09:38:05 +02002152 return BLK_STS_IOERR;
Ming Leif4aa4c72015-05-05 19:49:54 +08002153
Jens Axboe1894e912018-04-13 16:24:29 -06002154 switch (req_op(rq)) {
Christoph Hellwigf0225ca2016-08-04 16:10:00 +02002155 case REQ_OP_FLUSH:
2156 case REQ_OP_DISCARD:
Christoph Hellwig19372e22017-04-05 19:21:15 +02002157 case REQ_OP_WRITE_ZEROES:
Ming Leibc07c102015-08-17 10:31:51 +08002158 cmd->use_aio = false;
Christoph Hellwigf0225ca2016-08-04 16:10:00 +02002159 break;
2160 default:
2161 cmd->use_aio = lo->use_dio;
2162 break;
2163 }
Ming Leibc07c102015-08-17 10:31:51 +08002164
Shaohua Lid4478e92017-09-25 13:07:22 -06002165 /* always use the first bio's css */
Dan Schatzbergc74d40e2021-06-28 19:38:21 -07002166 cmd->blkcg_css = NULL;
2167 cmd->memcg_css = NULL;
Shaohua Li0b508bc2017-09-26 11:02:12 -07002168#ifdef CONFIG_BLK_CGROUP
Dan Schatzbergc74d40e2021-06-28 19:38:21 -07002169 if (rq->bio && rq->bio->bi_blkg) {
2170 cmd->blkcg_css = &bio_blkcg(rq->bio)->css;
2171#ifdef CONFIG_MEMCG
2172 cmd->memcg_css =
2173 cgroup_get_e_css(cmd->blkcg_css->cgroup,
2174 &memory_cgrp_subsys);
Shaohua Lid4478e92017-09-25 13:07:22 -06002175#endif
Dan Schatzbergc74d40e2021-06-28 19:38:21 -07002176 }
2177#endif
Dan Schatzberg87579e92021-06-28 19:38:15 -07002178 loop_queue_work(lo, cmd);
Ming Leib5dd2f62014-12-31 13:22:57 +00002179
Christoph Hellwigfc17b652017-06-03 09:38:05 +02002180 return BLK_STS_OK;
Ming Leib5dd2f62014-12-31 13:22:57 +00002181}
2182
2183static void loop_handle_cmd(struct loop_cmd *cmd)
2184{
Jens Axboe1894e912018-04-13 16:24:29 -06002185 struct request *rq = blk_mq_rq_from_pdu(cmd);
2186 const bool write = op_is_write(req_op(rq));
2187 struct loop_device *lo = rq->q->queuedata;
Christoph Hellwigf4829a92015-09-27 21:01:50 +02002188 int ret = 0;
Dan Schatzbergc74d40e2021-06-28 19:38:21 -07002189 struct mem_cgroup *old_memcg = NULL;
Ming Leib5dd2f62014-12-31 13:22:57 +00002190
Christoph Hellwigf4829a92015-09-27 21:01:50 +02002191 if (write && (lo->lo_flags & LO_FLAGS_READ_ONLY)) {
2192 ret = -EIO;
Ming Leib5dd2f62014-12-31 13:22:57 +00002193 goto failed;
Christoph Hellwigf4829a92015-09-27 21:01:50 +02002194 }
Ming Leib5dd2f62014-12-31 13:22:57 +00002195
Dan Schatzbergc74d40e2021-06-28 19:38:21 -07002196 if (cmd->blkcg_css)
2197 kthread_associate_blkcg(cmd->blkcg_css);
2198 if (cmd->memcg_css)
2199 old_memcg = set_active_memcg(
2200 mem_cgroup_from_css(cmd->memcg_css));
2201
Jens Axboe1894e912018-04-13 16:24:29 -06002202 ret = do_req_filebacked(lo, rq);
Dan Schatzbergc74d40e2021-06-28 19:38:21 -07002203
2204 if (cmd->blkcg_css)
2205 kthread_associate_blkcg(NULL);
2206
2207 if (cmd->memcg_css) {
2208 set_active_memcg(old_memcg);
2209 css_put(cmd->memcg_css);
2210 }
Ming Leib5dd2f62014-12-31 13:22:57 +00002211 failed:
Ming Leibc07c102015-08-17 10:31:51 +08002212 /* complete non-aio request */
Christoph Hellwigfe2cb292017-04-20 16:03:02 +02002213 if (!cmd->use_aio || ret) {
Evan Green8cd55082020-04-03 16:43:03 +02002214 if (ret == -EOPNOTSUPP)
2215 cmd->ret = ret;
2216 else
2217 cmd->ret = ret ? -EIO : 0;
Christoph Hellwig15f73f52020-06-11 08:44:47 +02002218 if (likely(!blk_should_fake_timeout(rq->q)))
2219 blk_mq_complete_request(rq);
Christoph Hellwigfe2cb292017-04-20 16:03:02 +02002220 }
Ming Leib5dd2f62014-12-31 13:22:57 +00002221}
2222
Dan Schatzberg87579e92021-06-28 19:38:15 -07002223static void loop_set_timer(struct loop_device *lo)
Ming Leib5dd2f62014-12-31 13:22:57 +00002224{
Dan Schatzberg87579e92021-06-28 19:38:15 -07002225 timer_reduce(&lo->timer, jiffies + LOOP_IDLE_WORKER_TIMEOUT);
Ming Leib5dd2f62014-12-31 13:22:57 +00002226}
2227
Dan Schatzberg87579e92021-06-28 19:38:15 -07002228static void loop_process_work(struct loop_worker *worker,
2229 struct list_head *cmd_list, struct loop_device *lo)
Ming Leib5dd2f62014-12-31 13:22:57 +00002230{
Dan Schatzberg87579e92021-06-28 19:38:15 -07002231 int orig_flags = current->flags;
2232 struct loop_cmd *cmd;
Ming Leib5dd2f62014-12-31 13:22:57 +00002233
Dan Schatzberg87579e92021-06-28 19:38:15 -07002234 current->flags |= PF_LOCAL_THROTTLE | PF_MEMALLOC_NOIO;
2235 spin_lock_irq(&lo->lo_work_lock);
2236 while (!list_empty(cmd_list)) {
2237 cmd = container_of(
2238 cmd_list->next, struct loop_cmd, list_entry);
2239 list_del(cmd_list->next);
2240 spin_unlock_irq(&lo->lo_work_lock);
2241
2242 loop_handle_cmd(cmd);
2243 cond_resched();
2244
2245 spin_lock_irq(&lo->lo_work_lock);
2246 }
2247
2248 /*
2249 * We only add to the idle list if there are no pending cmds
2250 * *and* the worker will not run again which ensures that it
2251 * is safe to free any worker on the idle list
2252 */
2253 if (worker && !work_pending(&worker->work)) {
2254 worker->last_ran_at = jiffies;
2255 list_add_tail(&worker->idle_list, &lo->idle_worker_list);
2256 loop_set_timer(lo);
2257 }
2258 spin_unlock_irq(&lo->lo_work_lock);
2259 current->flags = orig_flags;
2260}
2261
2262static void loop_workfn(struct work_struct *work)
2263{
2264 struct loop_worker *worker =
2265 container_of(work, struct loop_worker, work);
2266 loop_process_work(worker, &worker->cmd_list, worker->lo);
2267}
2268
2269static void loop_rootcg_workfn(struct work_struct *work)
2270{
2271 struct loop_device *lo =
2272 container_of(work, struct loop_device, rootcg_work);
2273 loop_process_work(NULL, &lo->rootcg_cmd_list, lo);
2274}
2275
2276static void loop_free_idle_workers(struct timer_list *timer)
2277{
2278 struct loop_device *lo = container_of(timer, struct loop_device, timer);
2279 struct loop_worker *pos, *worker;
2280
2281 spin_lock_irq(&lo->lo_work_lock);
2282 list_for_each_entry_safe(worker, pos, &lo->idle_worker_list,
2283 idle_list) {
2284 if (time_is_after_jiffies(worker->last_ran_at +
2285 LOOP_IDLE_WORKER_TIMEOUT))
2286 break;
2287 list_del(&worker->idle_list);
2288 rb_erase(&worker->rb_node, &lo->worker_tree);
Dan Schatzbergc74d40e2021-06-28 19:38:21 -07002289 css_put(worker->blkcg_css);
Dan Schatzberg87579e92021-06-28 19:38:15 -07002290 kfree(worker);
2291 }
2292 if (!list_empty(&lo->idle_worker_list))
2293 loop_set_timer(lo);
2294 spin_unlock_irq(&lo->lo_work_lock);
Ming Leib5dd2f62014-12-31 13:22:57 +00002295}
2296
Eric Biggersf363b082017-03-30 13:39:16 -07002297static const struct blk_mq_ops loop_mq_ops = {
Ming Leib5dd2f62014-12-31 13:22:57 +00002298 .queue_rq = loop_queue_rq,
Christoph Hellwigfe2cb292017-04-20 16:03:02 +02002299 .complete = lo_complete_rq,
Ming Leib5dd2f62014-12-31 13:22:57 +00002300};
2301
Christoph Hellwigd6da83d2021-06-23 16:59:02 +02002302static int loop_add(int i)
Ken Chen73285082007-05-08 00:28:20 -07002303{
2304 struct loop_device *lo;
2305 struct gendisk *disk;
Kay Sievers34dd82a2011-07-31 22:08:04 +02002306 int err;
Ken Chen73285082007-05-08 00:28:20 -07002307
Silva Paulo68d740d2012-07-14 15:39:58 -07002308 err = -ENOMEM;
Ken Chen73285082007-05-08 00:28:20 -07002309 lo = kzalloc(sizeof(*lo), GFP_KERNEL);
Silva Paulo68d740d2012-07-14 15:39:58 -07002310 if (!lo)
Ken Chen73285082007-05-08 00:28:20 -07002311 goto out;
Mikulas Patockaef7e7c82013-10-15 14:14:38 -06002312 lo->lo_state = Lo_unbound;
2313
Christoph Hellwig18d1f202021-06-23 16:59:05 +02002314 err = mutex_lock_killable(&loop_ctl_mutex);
2315 if (err)
2316 goto out_free_dev;
2317
Tejun Heoc718aa62013-02-27 17:03:58 -08002318 /* allocate id, if @id >= 0, we're requesting that specific id */
Kay Sievers34dd82a2011-07-31 22:08:04 +02002319 if (i >= 0) {
Tejun Heoc718aa62013-02-27 17:03:58 -08002320 err = idr_alloc(&loop_index_idr, lo, i, i + 1, GFP_KERNEL);
2321 if (err == -ENOSPC)
Kay Sievers34dd82a2011-07-31 22:08:04 +02002322 err = -EEXIST;
Kay Sievers34dd82a2011-07-31 22:08:04 +02002323 } else {
Tejun Heoc718aa62013-02-27 17:03:58 -08002324 err = idr_alloc(&loop_index_idr, lo, 0, 0, GFP_KERNEL);
Kay Sievers34dd82a2011-07-31 22:08:04 +02002325 }
2326 if (err < 0)
Christoph Hellwig18d1f202021-06-23 16:59:05 +02002327 goto out_unlock;
Tejun Heoc718aa62013-02-27 17:03:58 -08002328 i = err;
Ken Chen73285082007-05-08 00:28:20 -07002329
Wei Yongjun183cfb52013-03-22 08:59:19 -06002330 err = -ENOMEM;
Ming Leib5dd2f62014-12-31 13:22:57 +00002331 lo->tag_set.ops = &loop_mq_ops;
2332 lo->tag_set.nr_hw_queues = 1;
2333 lo->tag_set.queue_depth = 128;
2334 lo->tag_set.numa_node = NUMA_NO_NODE;
2335 lo->tag_set.cmd_size = sizeof(struct loop_cmd);
Bart Van Assche2112f5c2021-08-05 10:42:00 -07002336 lo->tag_set.flags = BLK_MQ_F_SHOULD_MERGE | BLK_MQ_F_STACKING |
2337 BLK_MQ_F_NO_SCHED_BY_DEFAULT;
Ming Leib5dd2f62014-12-31 13:22:57 +00002338 lo->tag_set.driver_data = lo;
2339
2340 err = blk_mq_alloc_tag_set(&lo->tag_set);
2341 if (err)
Mikulas Patocka3ec981e2013-10-14 12:12:24 -04002342 goto out_free_idr;
Ken Chen73285082007-05-08 00:28:20 -07002343
Christoph Hellwig1c995022021-06-02 09:53:33 +03002344 disk = lo->lo_disk = blk_mq_alloc_disk(&lo->tag_set, lo);
2345 if (IS_ERR(disk)) {
2346 err = PTR_ERR(disk);
Ming Leib5dd2f62014-12-31 13:22:57 +00002347 goto out_cleanup_tags;
2348 }
Christoph Hellwig1c995022021-06-02 09:53:33 +03002349 lo->lo_queue = lo->lo_disk->queue;
Mikulas Patockaef7e7c82013-10-15 14:14:38 -06002350
Shaohua Li54bb0ad2017-08-31 22:09:45 -07002351 blk_queue_max_hw_sectors(lo->lo_queue, BLK_DEF_MAX_SECTORS);
Shaohua Li40326d82017-08-31 22:09:46 -07002352
Ming Lei5b5e20f2015-08-17 10:31:47 +08002353 /*
Shaohua Li40326d82017-08-31 22:09:46 -07002354 * By default, we do buffer IO, so it doesn't make sense to enable
2355 * merge because the I/O submitted to backing file is handled page by
2356 * page. For directio mode, merge does help to dispatch bigger request
2357 * to underlayer disk. We will enable merge once directio is enabled.
Ming Lei5b5e20f2015-08-17 10:31:47 +08002358 */
Bart Van Assche8b904b52018-03-07 17:10:10 -08002359 blk_queue_flag_set(QUEUE_FLAG_NOMERGES, lo->lo_queue);
Ming Lei5b5e20f2015-08-17 10:31:47 +08002360
Kay Sieverse03c8dd2011-08-23 20:12:04 +02002361 /*
2362 * Disable partition scanning by default. The in-kernel partition
2363 * scanning can be requested individually per-device during its
2364 * setup. Userspace can always add and remove partitions from all
2365 * devices. The needed partition minors are allocated from the
2366 * extended minor space, the main loop device numbers will continue
2367 * to match the loop minors, regardless of the number of partitions
2368 * used.
2369 *
2370 * If max_part is given, partition scanning is globally enabled for
2371 * all loop devices. The minors for the main loop devices will be
2372 * multiples of max_part.
2373 *
2374 * Note: Global-for-all-devices, set-only-at-init, read-only module
2375 * parameteters like 'max_loop' and 'max_part' make things needlessly
2376 * complicated, are too static, inflexible and may surprise
2377 * userspace tools. Parameters like this in general should be avoided.
2378 */
2379 if (!part_shift)
2380 disk->flags |= GENHD_FL_NO_PART_SCAN;
2381 disk->flags |= GENHD_FL_EXT_DEVT;
Ming Leif8933662015-05-06 12:26:23 +08002382 atomic_set(&lo->lo_refcnt, 0);
Pavel Tatashin6cc8e742021-01-26 09:46:30 -05002383 mutex_init(&lo->lo_mutex);
Ken Chen73285082007-05-08 00:28:20 -07002384 lo->lo_number = i;
Ken Chen73285082007-05-08 00:28:20 -07002385 spin_lock_init(&lo->lo_lock);
Dan Schatzberg87579e92021-06-28 19:38:15 -07002386 spin_lock_init(&lo->lo_work_lock);
Ken Chen73285082007-05-08 00:28:20 -07002387 disk->major = LOOP_MAJOR;
Laurent Vivier476a4812008-03-26 12:11:53 +01002388 disk->first_minor = i << part_shift;
Christoph Hellwig1c995022021-06-02 09:53:33 +03002389 disk->minors = 1 << part_shift;
Ken Chen73285082007-05-08 00:28:20 -07002390 disk->fops = &lo_fops;
2391 disk->private_data = lo;
2392 disk->queue = lo->lo_queue;
Matteo Croce9f65c482021-07-13 01:05:30 +02002393 disk->events = DISK_EVENT_MEDIA_CHANGE;
2394 disk->event_flags = DISK_EVENT_FLAG_UEVENT;
Ken Chen73285082007-05-08 00:28:20 -07002395 sprintf(disk->disk_name, "loop%d", i);
Kay Sievers34dd82a2011-07-31 22:08:04 +02002396 add_disk(disk);
Christoph Hellwig18d1f202021-06-23 16:59:05 +02002397 mutex_unlock(&loop_ctl_mutex);
2398 return i;
Ken Chen73285082007-05-08 00:28:20 -07002399
Ming Leib5dd2f62014-12-31 13:22:57 +00002400out_cleanup_tags:
2401 blk_mq_free_tag_set(&lo->tag_set);
Mikulas Patocka3ec981e2013-10-14 12:12:24 -04002402out_free_idr:
2403 idr_remove(&loop_index_idr, i);
Christoph Hellwig18d1f202021-06-23 16:59:05 +02002404out_unlock:
2405 mutex_unlock(&loop_ctl_mutex);
Ken Chen73285082007-05-08 00:28:20 -07002406out_free_dev:
2407 kfree(lo);
2408out:
Kay Sievers34dd82a2011-07-31 22:08:04 +02002409 return err;
Ken Chen73285082007-05-08 00:28:20 -07002410}
2411
Kay Sievers34dd82a2011-07-31 22:08:04 +02002412static void loop_remove(struct loop_device *lo)
Ken Chen73285082007-05-08 00:28:20 -07002413{
NeilBrown6cd18e72015-04-27 14:12:22 +10002414 del_gendisk(lo->lo_disk);
Christoph Hellwig1c995022021-06-02 09:53:33 +03002415 blk_cleanup_disk(lo->lo_disk);
Ming Leib5dd2f62014-12-31 13:22:57 +00002416 blk_mq_free_tag_set(&lo->tag_set);
Pavel Tatashin6cc8e742021-01-26 09:46:30 -05002417 mutex_destroy(&lo->lo_mutex);
Ken Chen73285082007-05-08 00:28:20 -07002418 kfree(lo);
2419}
2420
Christoph Hellwig8410d382020-10-29 15:58:33 +01002421static void loop_probe(dev_t dev)
Ken Chen73285082007-05-08 00:28:20 -07002422{
Christoph Hellwig8410d382020-10-29 15:58:33 +01002423 int idx = MINOR(dev) >> part_shift;
Christoph Hellwig8410d382020-10-29 15:58:33 +01002424
2425 if (max_loop && idx >= max_loop)
2426 return;
Christoph Hellwig4157fe02021-06-23 16:59:03 +02002427 loop_add(idx);
Christoph Hellwigf9d10762021-06-23 16:59:04 +02002428}
2429
2430static int loop_control_remove(int idx)
2431{
2432 struct loop_device *lo;
2433 int ret;
Christoph Hellwige5d66a102021-06-23 16:59:06 +02002434
2435 if (idx < 0) {
2436 pr_warn("deleting an unspecified loop device is not supported.\n");
2437 return -EINVAL;
2438 }
Christoph Hellwigf9d10762021-06-23 16:59:04 +02002439
2440 ret = mutex_lock_killable(&loop_ctl_mutex);
2441 if (ret)
2442 return ret;
2443
Christoph Hellwigb9848082021-06-23 16:59:07 +02002444 lo = idr_find(&loop_index_idr, idx);
2445 if (!lo) {
2446 ret = -ENODEV;
Christoph Hellwigf9d10762021-06-23 16:59:04 +02002447 goto out_unlock_ctrl;
Christoph Hellwigb9848082021-06-23 16:59:07 +02002448 }
Christoph Hellwigf9d10762021-06-23 16:59:04 +02002449
2450 ret = mutex_lock_killable(&lo->lo_mutex);
2451 if (ret)
2452 goto out_unlock_ctrl;
2453 if (lo->lo_state != Lo_unbound ||
2454 atomic_read(&lo->lo_refcnt) > 0) {
2455 mutex_unlock(&lo->lo_mutex);
2456 ret = -EBUSY;
2457 goto out_unlock_ctrl;
2458 }
2459 lo->lo_state = Lo_deleting;
2460 mutex_unlock(&lo->lo_mutex);
2461
2462 idr_remove(&loop_index_idr, lo->lo_number);
2463 loop_remove(lo);
2464out_unlock_ctrl:
2465 mutex_unlock(&loop_ctl_mutex);
2466 return ret;
2467}
2468
2469static int loop_control_get_free(int idx)
Kay Sievers770fe302011-07-31 22:08:04 +02002470{
2471 struct loop_device *lo;
Christoph Hellwigb9848082021-06-23 16:59:07 +02002472 int id, ret;
Kay Sievers770fe302011-07-31 22:08:04 +02002473
Jan Kara0a42e992018-11-08 14:01:04 +01002474 ret = mutex_lock_killable(&loop_ctl_mutex);
2475 if (ret)
2476 return ret;
Christoph Hellwigb9848082021-06-23 16:59:07 +02002477 idr_for_each_entry(&loop_index_idr, lo, id) {
2478 if (lo->lo_state == Lo_unbound)
2479 goto found;
2480 }
Jan Kara0a42e992018-11-08 14:01:04 +01002481 mutex_unlock(&loop_ctl_mutex);
Christoph Hellwig18d1f202021-06-23 16:59:05 +02002482 return loop_add(-1);
Christoph Hellwigb9848082021-06-23 16:59:07 +02002483found:
2484 mutex_unlock(&loop_ctl_mutex);
2485 return id;
Kay Sievers770fe302011-07-31 22:08:04 +02002486}
2487
Christoph Hellwigf9d10762021-06-23 16:59:04 +02002488static long loop_control_ioctl(struct file *file, unsigned int cmd,
2489 unsigned long parm)
2490{
2491 switch (cmd) {
2492 case LOOP_CTL_ADD:
Christoph Hellwig18d1f202021-06-23 16:59:05 +02002493 return loop_add(parm);
Christoph Hellwigf9d10762021-06-23 16:59:04 +02002494 case LOOP_CTL_REMOVE:
2495 return loop_control_remove(parm);
2496 case LOOP_CTL_GET_FREE:
2497 return loop_control_get_free(parm);
2498 default:
2499 return -ENOSYS;
2500 }
2501}
2502
Kay Sievers770fe302011-07-31 22:08:04 +02002503static const struct file_operations loop_ctl_fops = {
2504 .open = nonseekable_open,
2505 .unlocked_ioctl = loop_control_ioctl,
2506 .compat_ioctl = loop_control_ioctl,
2507 .owner = THIS_MODULE,
2508 .llseek = noop_llseek,
2509};
2510
2511static struct miscdevice loop_misc = {
2512 .minor = LOOP_CTRL_MINOR,
2513 .name = "loop-control",
2514 .fops = &loop_ctl_fops,
2515};
2516
2517MODULE_ALIAS_MISCDEV(LOOP_CTRL_MINOR);
2518MODULE_ALIAS("devname:loop-control");
2519
Linus Torvalds1da177e2005-04-16 15:20:36 -07002520static int __init loop_init(void)
2521{
Ken Chena47653f2007-06-08 13:46:44 -07002522 int i, nr;
Kay Sievers770fe302011-07-31 22:08:04 +02002523 int err;
Ken Chena47653f2007-06-08 13:46:44 -07002524
Laurent Vivier476a4812008-03-26 12:11:53 +01002525 part_shift = 0;
Namhyung Kimac04fee2011-05-27 07:59:25 +02002526 if (max_part > 0) {
Laurent Vivier476a4812008-03-26 12:11:53 +01002527 part_shift = fls(max_part);
2528
Namhyung Kimac04fee2011-05-27 07:59:25 +02002529 /*
2530 * Adjust max_part according to part_shift as it is exported
2531 * to user space so that user can decide correct minor number
2532 * if [s]he want to create more devices.
2533 *
2534 * Note that -1 is required because partition 0 is reserved
2535 * for the whole disk.
2536 */
2537 max_part = (1UL << part_shift) - 1;
2538 }
2539
Guo Chaob1a66502013-02-21 15:16:49 -08002540 if ((1UL << part_shift) > DISK_MAX_PARTS) {
2541 err = -EINVAL;
Anton Volkova8c1d062017-08-07 15:37:50 +03002542 goto err_out;
Guo Chaob1a66502013-02-21 15:16:49 -08002543 }
Namhyung Kim78f4bb32011-05-24 16:48:54 +02002544
Guo Chaob1a66502013-02-21 15:16:49 -08002545 if (max_loop > 1UL << (MINORBITS - part_shift)) {
2546 err = -EINVAL;
Anton Volkova8c1d062017-08-07 15:37:50 +03002547 goto err_out;
Guo Chaob1a66502013-02-21 15:16:49 -08002548 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002549
Kay Sieversd134b002011-07-31 22:08:04 +02002550 /*
2551 * If max_loop is specified, create that many devices upfront.
2552 * This also becomes a hard limit. If max_loop is not specified,
2553 * create CONFIG_BLK_DEV_LOOP_MIN_COUNT loop devices at module
2554 * init time. Loop devices can be requested on-demand with the
2555 * /dev/loop-control interface, or be instantiated by accessing
2556 * a 'dead' device node.
2557 */
Lukas Bulwahnaeb2b0b2020-12-12 06:13:02 +01002558 if (max_loop)
Ken Chena47653f2007-06-08 13:46:44 -07002559 nr = max_loop;
Lukas Bulwahnaeb2b0b2020-12-12 06:13:02 +01002560 else
Kay Sieversd134b002011-07-31 22:08:04 +02002561 nr = CONFIG_BLK_DEV_LOOP_MIN_COUNT;
Ken Chena47653f2007-06-08 13:46:44 -07002562
Anton Volkova8c1d062017-08-07 15:37:50 +03002563 err = misc_register(&loop_misc);
2564 if (err < 0)
2565 goto err_out;
2566
2567
Christoph Hellwig8410d382020-10-29 15:58:33 +01002568 if (__register_blkdev(LOOP_MAJOR, "loop", loop_probe)) {
Guo Chaob1a66502013-02-21 15:16:49 -08002569 err = -EIO;
2570 goto misc_out;
2571 }
Ken Chena47653f2007-06-08 13:46:44 -07002572
Kay Sieversd134b002011-07-31 22:08:04 +02002573 /* pre-create number of devices given by config or max_loop */
Kay Sievers34dd82a2011-07-31 22:08:04 +02002574 for (i = 0; i < nr; i++)
Christoph Hellwigd6da83d2021-06-23 16:59:02 +02002575 loop_add(i);
Kay Sievers34dd82a2011-07-31 22:08:04 +02002576
Ken Chen73285082007-05-08 00:28:20 -07002577 printk(KERN_INFO "loop: module loaded\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002578 return 0;
Guo Chaob1a66502013-02-21 15:16:49 -08002579
2580misc_out:
2581 misc_deregister(&loop_misc);
Anton Volkova8c1d062017-08-07 15:37:50 +03002582err_out:
Guo Chaob1a66502013-02-21 15:16:49 -08002583 return err;
Kay Sievers34dd82a2011-07-31 22:08:04 +02002584}
Ken Chena47653f2007-06-08 13:46:44 -07002585
Ken Chen73285082007-05-08 00:28:20 -07002586static void __exit loop_exit(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002587{
Christoph Hellwig8e609472021-06-23 16:59:08 +02002588 struct loop_device *lo;
2589 int id;
2590
Christoph Hellwig8b52d8b2021-06-23 16:59:00 +02002591 unregister_blkdev(LOOP_MAJOR, "loop");
2592 misc_deregister(&loop_misc);
Luis Chamberlain200f9332020-06-19 20:47:27 +00002593
Christoph Hellwigbd5c39e2021-06-23 16:59:01 +02002594 mutex_lock(&loop_ctl_mutex);
Christoph Hellwig8e609472021-06-23 16:59:08 +02002595 idr_for_each_entry(&loop_index_idr, lo, id)
2596 loop_remove(lo);
Luis Chamberlain200f9332020-06-19 20:47:27 +00002597 mutex_unlock(&loop_ctl_mutex);
Christoph Hellwigbd5c39e2021-06-23 16:59:01 +02002598
2599 idr_destroy(&loop_index_idr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002600}
2601
2602module_init(loop_init);
2603module_exit(loop_exit);
2604
2605#ifndef MODULE
2606static int __init max_loop_setup(char *str)
2607{
2608 max_loop = simple_strtol(str, NULL, 0);
2609 return 1;
2610}
2611
2612__setup("max_loop=", max_loop_setup);
2613#endif