blob: 150012ffb387f593ec00ae2403a72452bc89579f [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
Dmitry Monakhov7035b5d2011-11-16 09:21:49 +0100136static loff_t get_size(loff_t offset, loff_t sizelimit, struct file *file)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700137{
Guo Chaob7a1da62013-02-21 15:16:50 -0800138 loff_t loopsize;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700139
140 /* Compute loopsize in bytes */
Guo Chaob7a1da62013-02-21 15:16:50 -0800141 loopsize = i_size_read(file->f_mapping->host);
142 if (offset > 0)
143 loopsize -= offset;
144 /* offset is beyond i_size, weird but possible */
Dmitry Monakhov7035b5d2011-11-16 09:21:49 +0100145 if (loopsize < 0)
146 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700147
Dmitry Monakhov7035b5d2011-11-16 09:21:49 +0100148 if (sizelimit > 0 && sizelimit < loopsize)
149 loopsize = sizelimit;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700150 /*
151 * Unfortunately, if we want to do I/O on the device,
152 * the number of 512-byte sectors has to fit into a sector_t.
153 */
154 return loopsize >> 9;
155}
156
Dmitry Monakhov7035b5d2011-11-16 09:21:49 +0100157static loff_t get_loop_size(struct loop_device *lo, struct file *file)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700158{
Dmitry Monakhov7035b5d2011-11-16 09:21:49 +0100159 return get_size(lo->lo_offset, lo->lo_sizelimit, file);
160}
161
Ming Lei2e5ab5f2015-08-17 10:31:49 +0800162static void __loop_update_dio(struct loop_device *lo, bool dio)
163{
164 struct file *file = lo->lo_backing_file;
165 struct address_space *mapping = file->f_mapping;
166 struct inode *inode = mapping->host;
167 unsigned short sb_bsize = 0;
168 unsigned dio_align = 0;
169 bool use_dio;
170
171 if (inode->i_sb->s_bdev) {
172 sb_bsize = bdev_logical_block_size(inode->i_sb->s_bdev);
173 dio_align = sb_bsize - 1;
174 }
175
176 /*
177 * We support direct I/O only if lo_offset is aligned with the
178 * logical I/O size of backing device, and the logical block
Christoph Hellwig47e96242021-10-19 09:56:39 +0200179 * size of loop is bigger than the backing device's.
Ming Lei2e5ab5f2015-08-17 10:31:49 +0800180 *
181 * TODO: the above condition may be loosed in the future, and
182 * direct I/O may be switched runtime at that time because most
Masahiro Yamada89d790a2017-02-27 14:29:01 -0800183 * of requests in sane applications should be PAGE_SIZE aligned
Ming Lei2e5ab5f2015-08-17 10:31:49 +0800184 */
185 if (dio) {
186 if (queue_logical_block_size(lo->lo_queue) >= sb_bsize &&
187 !(lo->lo_offset & dio_align) &&
Christoph Hellwig47e96242021-10-19 09:56:39 +0200188 mapping->a_ops->direct_IO)
Ming Lei2e5ab5f2015-08-17 10:31:49 +0800189 use_dio = true;
190 else
191 use_dio = false;
192 } else {
193 use_dio = false;
194 }
195
196 if (lo->use_dio == use_dio)
197 return;
198
199 /* flush dirty pages before changing direct IO */
200 vfs_fsync(file, 0);
201
202 /*
203 * The flag of LO_FLAGS_DIRECT_IO is handled similarly with
204 * LO_FLAGS_READ_ONLY, both are set from kernel, and losetup
205 * will get updated by ioctl(LOOP_GET_STATUS)
206 */
Martijn Coenen0fbcf572020-03-10 14:06:54 +0100207 if (lo->lo_state == Lo_bound)
208 blk_mq_freeze_queue(lo->lo_queue);
Ming Lei2e5ab5f2015-08-17 10:31:49 +0800209 lo->use_dio = use_dio;
Shaohua Li40326d82017-08-31 22:09:46 -0700210 if (use_dio) {
Bart Van Assche8b904b52018-03-07 17:10:10 -0800211 blk_queue_flag_clear(QUEUE_FLAG_NOMERGES, lo->lo_queue);
Ming Lei2e5ab5f2015-08-17 10:31:49 +0800212 lo->lo_flags |= LO_FLAGS_DIRECT_IO;
Shaohua Li40326d82017-08-31 22:09:46 -0700213 } else {
Bart Van Assche8b904b52018-03-07 17:10:10 -0800214 blk_queue_flag_set(QUEUE_FLAG_NOMERGES, lo->lo_queue);
Ming Lei2e5ab5f2015-08-17 10:31:49 +0800215 lo->lo_flags &= ~LO_FLAGS_DIRECT_IO;
Shaohua Li40326d82017-08-31 22:09:46 -0700216 }
Martijn Coenen0fbcf572020-03-10 14:06:54 +0100217 if (lo->lo_state == Lo_bound)
218 blk_mq_unfreeze_queue(lo->lo_queue);
Ming Lei2e5ab5f2015-08-17 10:31:49 +0800219}
220
Martijn Coenen5795b6f2020-05-13 15:38:37 +0200221/**
222 * loop_set_size() - sets device size and notifies userspace
223 * @lo: struct loop_device to set the size for
224 * @size: new size of the loop device
225 *
226 * Callers must validate that the size passed into this function fits into
227 * a sector_t, eg using loop_validate_size()
228 */
229static void loop_set_size(struct loop_device *lo, loff_t size)
230{
Christoph Hellwig449f4ec2020-11-16 15:56:56 +0100231 if (!set_capacity_and_notify(lo->lo_disk, size))
Christoph Hellwig3b4f85d2020-11-16 15:56:53 +0100232 kobject_uevent(&disk_to_dev(lo->lo_disk)->kobj, KOBJ_CHANGE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700233}
234
Christoph Hellwigaa4d8612015-04-07 18:23:29 +0200235static int lo_write_bvec(struct file *file, struct bio_vec *bvec, loff_t *ppos)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700236{
Christoph Hellwigaa4d8612015-04-07 18:23:29 +0200237 struct iov_iter i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700238 ssize_t bw;
Al Viro283e7e52015-04-03 15:21:59 -0400239
Christoph Hellwigb6207432019-06-26 15:49:28 +0200240 iov_iter_bvec(&i, WRITE, bvec, 1, bvec->bv_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700241
Al Viro03d95eb2013-03-20 13:04:20 -0400242 file_start_write(file);
Christoph Hellwigabbb65892017-05-27 11:16:52 +0300243 bw = vfs_iter_write(file, &i, ppos, 0);
Al Viro03d95eb2013-03-20 13:04:20 -0400244 file_end_write(file);
Christoph Hellwigaa4d8612015-04-07 18:23:29 +0200245
246 if (likely(bw == bvec->bv_len))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700247 return 0;
Christoph Hellwigaa4d8612015-04-07 18:23:29 +0200248
249 printk_ratelimited(KERN_ERR
250 "loop: Write error at byte offset %llu, length %i.\n",
251 (unsigned long long)*ppos, bvec->bv_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700252 if (bw >= 0)
253 bw = -EIO;
254 return bw;
255}
256
Christoph Hellwigaa4d8612015-04-07 18:23:29 +0200257static int lo_write_simple(struct loop_device *lo, struct request *rq,
258 loff_t pos)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700259{
Christoph Hellwigaa4d8612015-04-07 18:23:29 +0200260 struct bio_vec bvec;
261 struct req_iterator iter;
262 int ret = 0;
263
264 rq_for_each_segment(bvec, rq, iter) {
265 ret = lo_write_bvec(lo->lo_backing_file, &bvec, &pos);
266 if (ret < 0)
267 break;
268 cond_resched();
269 }
270
271 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700272}
273
Christoph Hellwigaa4d8612015-04-07 18:23:29 +0200274static int lo_read_simple(struct loop_device *lo, struct request *rq,
275 loff_t pos)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700276{
Kent Overstreet79886132013-11-23 17:19:00 -0800277 struct bio_vec bvec;
Ming Lei30112012014-12-31 13:22:58 +0000278 struct req_iterator iter;
Christoph Hellwigaa4d8612015-04-07 18:23:29 +0200279 struct iov_iter i;
280 ssize_t len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700281
Ming Lei30112012014-12-31 13:22:58 +0000282 rq_for_each_segment(bvec, rq, iter) {
Christoph Hellwigb6207432019-06-26 15:49:28 +0200283 iov_iter_bvec(&i, READ, &bvec, 1, bvec.bv_len);
Christoph Hellwig18e97102017-05-27 11:16:51 +0300284 len = vfs_iter_read(lo->lo_backing_file, &i, &pos, 0);
Christoph Hellwigaa4d8612015-04-07 18:23:29 +0200285 if (len < 0)
286 return len;
Dave Young306df072012-02-08 22:07:19 +0100287
Christoph Hellwigaa4d8612015-04-07 18:23:29 +0200288 flush_dcache_page(bvec.bv_page);
289
290 if (len != bvec.bv_len) {
Ming Lei30112012014-12-31 13:22:58 +0000291 struct bio *bio;
292
293 __rq_for_each_bio(bio, rq)
294 zero_fill_bio(bio);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700295 break;
Dave Young306df072012-02-08 22:07:19 +0100296 }
Christoph Hellwigaa4d8612015-04-07 18:23:29 +0200297 cond_resched();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700298 }
Christoph Hellwigaa4d8612015-04-07 18:23:29 +0200299
Dave Young306df072012-02-08 22:07:19 +0100300 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700301}
302
Darrick J. Wongefcfec572019-10-30 20:29:48 -0700303static int lo_fallocate(struct loop_device *lo, struct request *rq, loff_t pos,
304 int mode)
Ming Leicf655d92014-12-31 13:22:59 +0000305{
306 /*
Darrick J. Wongefcfec572019-10-30 20:29:48 -0700307 * We use fallocate to manipulate the space mappings used by the image
Christoph Hellwig47e96242021-10-19 09:56:39 +0200308 * a.k.a. discard/zerorange.
Ming Leicf655d92014-12-31 13:22:59 +0000309 */
310 struct file *file = lo->lo_backing_file;
Evan Greenc52abf52020-04-03 16:43:04 +0200311 struct request_queue *q = lo->lo_queue;
Ming Leicf655d92014-12-31 13:22:59 +0000312 int ret;
313
Darrick J. Wongefcfec572019-10-30 20:29:48 -0700314 mode |= FALLOC_FL_KEEP_SIZE;
315
Evan Greenc52abf52020-04-03 16:43:04 +0200316 if (!blk_queue_discard(q)) {
Ming Leicf655d92014-12-31 13:22:59 +0000317 ret = -EOPNOTSUPP;
318 goto out;
319 }
320
321 ret = file->f_op->fallocate(file, mode, pos, blk_rq_bytes(rq));
322 if (unlikely(ret && ret != -EINVAL && ret != -EOPNOTSUPP))
323 ret = -EIO;
324 out:
325 return ret;
326}
327
328static int lo_req_flush(struct loop_device *lo, struct request *rq)
329{
330 struct file *file = lo->lo_backing_file;
331 int ret = vfs_fsync(file, 0);
332 if (unlikely(ret && ret != -EINVAL))
333 ret = -EIO;
334
335 return ret;
336}
337
Christoph Hellwigfe2cb292017-04-20 16:03:02 +0200338static void lo_complete_rq(struct request *rq)
Ming Leibc07c102015-08-17 10:31:51 +0800339{
Christoph Hellwigfe2cb292017-04-20 16:03:02 +0200340 struct loop_cmd *cmd = blk_mq_rq_to_pdu(rq);
Jens Axboef9de14b2018-04-13 16:25:57 -0600341 blk_status_t ret = BLK_STS_OK;
Ming Leibc07c102015-08-17 10:31:51 +0800342
Jens Axboef9de14b2018-04-13 16:25:57 -0600343 if (!cmd->use_aio || cmd->ret < 0 || cmd->ret == blk_rq_bytes(rq) ||
344 req_op(rq) != REQ_OP_READ) {
345 if (cmd->ret < 0)
Evan Green8cd55082020-04-03 16:43:03 +0200346 ret = errno_to_blk_status(cmd->ret);
Jens Axboef9de14b2018-04-13 16:25:57 -0600347 goto end_io;
Ming Leibc07c102015-08-17 10:31:51 +0800348 }
Christoph Hellwigfe2cb292017-04-20 16:03:02 +0200349
Jens Axboef9de14b2018-04-13 16:25:57 -0600350 /*
351 * Short READ - if we got some data, advance our request and
352 * retry it. If we got no data, end the rest with EIO.
353 */
354 if (cmd->ret) {
355 blk_update_request(rq, BLK_STS_OK, cmd->ret);
356 cmd->ret = 0;
357 blk_mq_requeue_request(rq, true);
358 } else {
359 if (cmd->use_aio) {
360 struct bio *bio = rq->bio;
361
362 while (bio) {
363 zero_fill_bio(bio);
364 bio = bio->bi_next;
365 }
366 }
367 ret = BLK_STS_IOERR;
368end_io:
369 blk_mq_end_request(rq, ret);
370 }
Ming Leibc07c102015-08-17 10:31:51 +0800371}
372
Shaohua Li92d77332017-09-01 11:15:17 -0700373static void lo_rw_aio_do_completion(struct loop_cmd *cmd)
374{
Jens Axboe1894e912018-04-13 16:24:29 -0600375 struct request *rq = blk_mq_rq_from_pdu(cmd);
376
Shaohua Li92d77332017-09-01 11:15:17 -0700377 if (!atomic_dec_and_test(&cmd->ref))
378 return;
379 kfree(cmd->bvec);
380 cmd->bvec = NULL;
Christoph Hellwig15f73f52020-06-11 08:44:47 +0200381 if (likely(!blk_should_fake_timeout(rq->q)))
382 blk_mq_complete_request(rq);
Shaohua Li92d77332017-09-01 11:15:17 -0700383}
384
Jens Axboe6b19b762021-10-21 09:22:35 -0600385static void lo_rw_aio_complete(struct kiocb *iocb, long ret)
Ming Leibc07c102015-08-17 10:31:51 +0800386{
387 struct loop_cmd *cmd = container_of(iocb, struct loop_cmd, iocb);
Ming Leibc07c102015-08-17 10:31:51 +0800388
Christoph Hellwigfe2cb292017-04-20 16:03:02 +0200389 cmd->ret = ret;
Shaohua Li92d77332017-09-01 11:15:17 -0700390 lo_rw_aio_do_completion(cmd);
Ming Leibc07c102015-08-17 10:31:51 +0800391}
392
393static int lo_rw_aio(struct loop_device *lo, struct loop_cmd *cmd,
394 loff_t pos, bool rw)
395{
396 struct iov_iter iter;
Ming Lei86af5952019-02-15 19:13:17 +0800397 struct req_iterator rq_iter;
Ming Leibc07c102015-08-17 10:31:51 +0800398 struct bio_vec *bvec;
Jens Axboe1894e912018-04-13 16:24:29 -0600399 struct request *rq = blk_mq_rq_from_pdu(cmd);
Shaohua Li40326d82017-08-31 22:09:46 -0700400 struct bio *bio = rq->bio;
Ming Leibc07c102015-08-17 10:31:51 +0800401 struct file *file = lo->lo_backing_file;
Ming Lei86af5952019-02-15 19:13:17 +0800402 struct bio_vec tmp;
Shaohua Li40326d82017-08-31 22:09:46 -0700403 unsigned int offset;
Ming Lei86af5952019-02-15 19:13:17 +0800404 int nr_bvec = 0;
Ming Leibc07c102015-08-17 10:31:51 +0800405 int ret;
406
Ming Lei86af5952019-02-15 19:13:17 +0800407 rq_for_each_bvec(tmp, rq, rq_iter)
408 nr_bvec++;
Ming Leibc07c102015-08-17 10:31:51 +0800409
Ming Lei86af5952019-02-15 19:13:17 +0800410 if (rq->bio != rq->biotail) {
411
412 bvec = kmalloc_array(nr_bvec, sizeof(struct bio_vec),
Kees Cook6da2ec52018-06-12 13:55:00 -0700413 GFP_NOIO);
Shaohua Li40326d82017-08-31 22:09:46 -0700414 if (!bvec)
415 return -EIO;
416 cmd->bvec = bvec;
417
418 /*
419 * The bios of the request may be started from the middle of
420 * the 'bvec' because of bio splitting, so we can't directly
Ming Lei86af5952019-02-15 19:13:17 +0800421 * copy bio->bi_iov_vec to new bvec. The rq_for_each_bvec
Shaohua Li40326d82017-08-31 22:09:46 -0700422 * API will take care of all details for us.
423 */
Ming Lei86af5952019-02-15 19:13:17 +0800424 rq_for_each_bvec(tmp, rq, rq_iter) {
Shaohua Li40326d82017-08-31 22:09:46 -0700425 *bvec = tmp;
426 bvec++;
427 }
428 bvec = cmd->bvec;
429 offset = 0;
430 } else {
431 /*
432 * Same here, this bio may be started from the middle of the
433 * 'bvec' because of bio splitting, so offset from the bvec
434 * must be passed to iov iterator
435 */
436 offset = bio->bi_iter.bi_bvec_done;
437 bvec = __bvec_iter_bvec(bio->bi_io_vec, bio->bi_iter);
Shaohua Li40326d82017-08-31 22:09:46 -0700438 }
Shaohua Li92d77332017-09-01 11:15:17 -0700439 atomic_set(&cmd->ref, 2);
Shaohua Li40326d82017-08-31 22:09:46 -0700440
Christoph Hellwigb6207432019-06-26 15:49:28 +0200441 iov_iter_bvec(&iter, rw, bvec, nr_bvec, blk_rq_bytes(rq));
Shaohua Li40326d82017-08-31 22:09:46 -0700442 iter.iov_offset = offset;
Ming Leibc07c102015-08-17 10:31:51 +0800443
444 cmd->iocb.ki_pos = pos;
445 cmd->iocb.ki_filp = file;
446 cmd->iocb.ki_complete = lo_rw_aio_complete;
447 cmd->iocb.ki_flags = IOCB_DIRECT;
Adam Manzanaresd9a08a92018-05-22 10:52:19 -0700448 cmd->iocb.ki_ioprio = IOPRIO_PRIO_VALUE(IOPRIO_CLASS_NONE, 0);
Ming Leibc07c102015-08-17 10:31:51 +0800449
450 if (rw == WRITE)
Miklos Szeredibb7462b2017-02-20 16:51:23 +0100451 ret = call_write_iter(file, &cmd->iocb, &iter);
Ming Leibc07c102015-08-17 10:31:51 +0800452 else
Miklos Szeredibb7462b2017-02-20 16:51:23 +0100453 ret = call_read_iter(file, &cmd->iocb, &iter);
Ming Leibc07c102015-08-17 10:31:51 +0800454
Shaohua Li92d77332017-09-01 11:15:17 -0700455 lo_rw_aio_do_completion(cmd);
456
Ming Leibc07c102015-08-17 10:31:51 +0800457 if (ret != -EIOCBQUEUED)
Jens Axboe6b19b762021-10-21 09:22:35 -0600458 lo_rw_aio_complete(&cmd->iocb, ret);
Ming Leibc07c102015-08-17 10:31:51 +0800459 return 0;
460}
461
Christoph Hellwigc1c87c22016-08-04 16:10:01 +0200462static int do_req_filebacked(struct loop_device *lo, struct request *rq)
Ming Leibc07c102015-08-17 10:31:51 +0800463{
464 struct loop_cmd *cmd = blk_mq_rq_to_pdu(rq);
Christoph Hellwigc1c87c22016-08-04 16:10:01 +0200465 loff_t pos = ((loff_t) blk_rq_pos(rq) << 9) + lo->lo_offset;
Ming Leibc07c102015-08-17 10:31:51 +0800466
467 /*
468 * lo_write_simple and lo_read_simple should have been covered
469 * by io submit style function like lo_rw_aio(), one blocker
470 * is that lo_read_simple() need to call flush_dcache_page after
471 * the page is written from kernel, and it isn't easy to handle
472 * this in io submit style function which submits all segments
473 * of the req at one time. And direct read IO doesn't need to
474 * run flush_dcache_page().
475 */
Christoph Hellwigc1c87c22016-08-04 16:10:01 +0200476 switch (req_op(rq)) {
477 case REQ_OP_FLUSH:
478 return lo_req_flush(lo, rq);
Christoph Hellwig19372e22017-04-05 19:21:15 +0200479 case REQ_OP_WRITE_ZEROES:
Darrick J. Wongefcfec572019-10-30 20:29:48 -0700480 /*
481 * If the caller doesn't want deallocation, call zeroout to
482 * write zeroes the range. Otherwise, punch them out.
483 */
484 return lo_fallocate(lo, rq, pos,
485 (rq->cmd_flags & REQ_NOUNMAP) ?
486 FALLOC_FL_ZERO_RANGE :
487 FALLOC_FL_PUNCH_HOLE);
488 case REQ_OP_DISCARD:
489 return lo_fallocate(lo, rq, pos, FALLOC_FL_PUNCH_HOLE);
Christoph Hellwigc1c87c22016-08-04 16:10:01 +0200490 case REQ_OP_WRITE:
Christoph Hellwig47e96242021-10-19 09:56:39 +0200491 if (cmd->use_aio)
Christoph Hellwigc1c87c22016-08-04 16:10:01 +0200492 return lo_rw_aio(lo, cmd, pos, WRITE);
Christoph Hellwigaa4d8612015-04-07 18:23:29 +0200493 else
Christoph Hellwigc1c87c22016-08-04 16:10:01 +0200494 return lo_write_simple(lo, rq, pos);
495 case REQ_OP_READ:
Christoph Hellwig47e96242021-10-19 09:56:39 +0200496 if (cmd->use_aio)
Christoph Hellwigc1c87c22016-08-04 16:10:01 +0200497 return lo_rw_aio(lo, cmd, pos, READ);
498 else
499 return lo_read_simple(lo, rq, pos);
500 default:
501 WARN_ON_ONCE(1);
502 return -EIO;
Christoph Hellwigaa4d8612015-04-07 18:23:29 +0200503 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700504}
505
Ming Lei2e5ab5f2015-08-17 10:31:49 +0800506static inline void loop_update_dio(struct loop_device *lo)
507{
Ira Weinyefbe3c22020-04-30 07:41:33 -0700508 __loop_update_dio(lo, (lo->lo_backing_file->f_flags & O_DIRECT) |
509 lo->use_dio);
Ming Lei2e5ab5f2015-08-17 10:31:49 +0800510}
511
Christoph Hellwig03842642021-06-24 14:32:40 +0200512static void loop_reread_partitions(struct loop_device *lo)
Ming Lei06f0e9e2015-05-06 12:26:24 +0800513{
514 int rc;
515
Christoph Hellwig03842642021-06-24 14:32:40 +0200516 mutex_lock(&lo->lo_disk->open_mutex);
517 rc = bdev_disk_changed(lo->lo_disk, false);
518 mutex_unlock(&lo->lo_disk->open_mutex);
Ming Lei06f0e9e2015-05-06 12:26:24 +0800519 if (rc)
520 pr_warn("%s: partition scan of loop%d (%s) failed (rc=%d)\n",
521 __func__, lo->lo_number, lo->lo_file_name, rc);
522}
523
Theodore Ts'od2ac8382018-05-07 11:37:58 -0400524static inline int is_loop_device(struct file *file)
525{
526 struct inode *i = file->f_mapping->host;
527
Al Viro6f247842021-01-31 19:23:55 -0500528 return i && S_ISBLK(i->i_mode) && imajor(i) == LOOP_MAJOR;
Theodore Ts'od2ac8382018-05-07 11:37:58 -0400529}
530
531static int loop_validate_file(struct file *file, struct block_device *bdev)
532{
533 struct inode *inode = file->f_mapping->host;
534 struct file *f = file;
535
536 /* Avoid recursion */
537 while (is_loop_device(f)) {
538 struct loop_device *l;
539
Tetsuo Handa3ce6e1f2021-07-06 23:40:34 +0900540 lockdep_assert_held(&loop_validate_mutex);
Christoph Hellwig4e7b5672020-11-23 13:38:40 +0100541 if (f->f_mapping->host->i_rdev == bdev->bd_dev)
Theodore Ts'od2ac8382018-05-07 11:37:58 -0400542 return -EBADF;
543
Christoph Hellwig4e7b5672020-11-23 13:38:40 +0100544 l = I_BDEV(f->f_mapping->host)->bd_disk->private_data;
Tetsuo Handa3ce6e1f2021-07-06 23:40:34 +0900545 if (l->lo_state != Lo_bound)
Theodore Ts'od2ac8382018-05-07 11:37:58 -0400546 return -EINVAL;
Tetsuo Handa3ce6e1f2021-07-06 23:40:34 +0900547 /* Order wrt setting lo->lo_backing_file in loop_configure(). */
548 rmb();
Theodore Ts'od2ac8382018-05-07 11:37:58 -0400549 f = l->lo_backing_file;
550 }
551 if (!S_ISREG(inode->i_mode) && !S_ISBLK(inode->i_mode))
552 return -EINVAL;
553 return 0;
554}
555
Linus Torvalds1da177e2005-04-16 15:20:36 -0700556/*
557 * loop_change_fd switched the backing store of a loopback device to
558 * a new file. This is useful for operating system installers to free up
559 * the original file and in High Availability environments to switch to
560 * an alternative location for the content in case of server meltdown.
561 * This can only work if the loop device is used read-only, and if the
562 * new backing store is the same size and type as the old backing store.
563 */
Al Virobb214882008-03-02 09:29:48 -0500564static int loop_change_fd(struct loop_device *lo, struct block_device *bdev,
565 unsigned int arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700566{
Tetsuo Handa3ce6e1f2021-07-06 23:40:34 +0900567 struct file *file = fget(arg);
568 struct file *old_file;
569 int error;
570 bool partscan;
571 bool is_loop;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700572
Tetsuo Handa3ce6e1f2021-07-06 23:40:34 +0900573 if (!file)
574 return -EBADF;
575 is_loop = is_loop_device(file);
576 error = loop_global_lock_killable(lo, is_loop);
Jan Karac3710772018-11-08 14:01:11 +0100577 if (error)
Tetsuo Handa3ce6e1f2021-07-06 23:40:34 +0900578 goto out_putf;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700579 error = -ENXIO;
580 if (lo->lo_state != Lo_bound)
Jan Kara1dded9a2018-11-08 14:01:15 +0100581 goto out_err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700582
583 /* the loop device has to be read-only */
584 error = -EINVAL;
585 if (!(lo->lo_flags & LO_FLAGS_READ_ONLY))
Jan Kara1dded9a2018-11-08 14:01:15 +0100586 goto out_err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700587
Theodore Ts'od2ac8382018-05-07 11:37:58 -0400588 error = loop_validate_file(file, bdev);
589 if (error)
Jan Kara1dded9a2018-11-08 14:01:15 +0100590 goto out_err;
Theodore Ts'od2ac8382018-05-07 11:37:58 -0400591
Linus Torvalds1da177e2005-04-16 15:20:36 -0700592 old_file = lo->lo_backing_file;
593
594 error = -EINVAL;
595
Linus Torvalds1da177e2005-04-16 15:20:36 -0700596 /* size of the new backing store needs to be the same */
597 if (get_loop_size(lo, file) != get_loop_size(lo, old_file))
Jan Kara1dded9a2018-11-08 14:01:15 +0100598 goto out_err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700599
600 /* and ... switch */
Matteo Croce9f65c482021-07-13 01:05:30 +0200601 disk_force_media_change(lo->lo_disk, DISK_EVENT_MEDIA_CHANGE);
Omar Sandoval43cade802017-08-24 00:03:44 -0700602 blk_mq_freeze_queue(lo->lo_queue);
603 mapping_set_gfp_mask(old_file->f_mapping, lo->old_gfp_mask);
604 lo->lo_backing_file = file;
605 lo->old_gfp_mask = mapping_gfp_mask(file->f_mapping);
606 mapping_set_gfp_mask(file->f_mapping,
607 lo->old_gfp_mask & ~(__GFP_IO|__GFP_FS));
608 loop_update_dio(lo);
609 blk_mq_unfreeze_queue(lo->lo_queue);
Jan Kara85b0a542018-11-08 14:01:13 +0100610 partscan = lo->lo_flags & LO_FLAGS_PARTSCAN;
Tetsuo Handa3ce6e1f2021-07-06 23:40:34 +0900611 loop_global_unlock(lo, is_loop);
612
613 /*
614 * Flush loop_validate_file() before fput(), for l->lo_backing_file
615 * might be pointing at old_file which might be the last reference.
616 */
617 if (!is_loop) {
618 mutex_lock(&loop_validate_mutex);
619 mutex_unlock(&loop_validate_mutex);
620 }
Jan Kara1dded9a2018-11-08 14:01:15 +0100621 /*
Pavel Tatashin6cc8e742021-01-26 09:46:30 -0500622 * We must drop file reference outside of lo_mutex as dropping
Christoph Hellwiga8698702021-05-25 08:12:56 +0200623 * the file ref can take open_mutex which creates circular locking
Jan Kara1dded9a2018-11-08 14:01:15 +0100624 * dependency.
625 */
626 fput(old_file);
Jan Kara85b0a542018-11-08 14:01:13 +0100627 if (partscan)
Christoph Hellwig03842642021-06-24 14:32:40 +0200628 loop_reread_partitions(lo);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700629 return 0;
630
Jan Kara1dded9a2018-11-08 14:01:15 +0100631out_err:
Tetsuo Handa3ce6e1f2021-07-06 23:40:34 +0900632 loop_global_unlock(lo, is_loop);
633out_putf:
634 fput(file);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700635 return error;
636}
637
Milan Brozee862732010-08-23 15:16:00 +0200638/* loop sysfs attributes */
639
640static ssize_t loop_attr_show(struct device *dev, char *page,
641 ssize_t (*callback)(struct loop_device *, char *))
642{
Kay Sievers34dd82a2011-07-31 22:08:04 +0200643 struct gendisk *disk = dev_to_disk(dev);
644 struct loop_device *lo = disk->private_data;
Milan Brozee862732010-08-23 15:16:00 +0200645
Kay Sievers34dd82a2011-07-31 22:08:04 +0200646 return callback(lo, page);
Milan Brozee862732010-08-23 15:16:00 +0200647}
648
649#define LOOP_ATTR_RO(_name) \
650static ssize_t loop_attr_##_name##_show(struct loop_device *, char *); \
651static ssize_t loop_attr_do_show_##_name(struct device *d, \
652 struct device_attribute *attr, char *b) \
653{ \
654 return loop_attr_show(d, b, loop_attr_##_name##_show); \
655} \
656static struct device_attribute loop_attr_##_name = \
Joe Perches5657a812018-05-24 13:38:59 -0600657 __ATTR(_name, 0444, loop_attr_do_show_##_name, NULL);
Milan Brozee862732010-08-23 15:16:00 +0200658
659static ssize_t loop_attr_backing_file_show(struct loop_device *lo, char *buf)
660{
661 ssize_t ret;
662 char *p = NULL;
663
Kay Sievers05eb0f22011-07-31 22:21:35 +0200664 spin_lock_irq(&lo->lo_lock);
Milan Brozee862732010-08-23 15:16:00 +0200665 if (lo->lo_backing_file)
Miklos Szeredi9bf39ab2015-06-19 10:29:13 +0200666 p = file_path(lo->lo_backing_file, buf, PAGE_SIZE - 1);
Kay Sievers05eb0f22011-07-31 22:21:35 +0200667 spin_unlock_irq(&lo->lo_lock);
Milan Brozee862732010-08-23 15:16:00 +0200668
669 if (IS_ERR_OR_NULL(p))
670 ret = PTR_ERR(p);
671 else {
672 ret = strlen(p);
673 memmove(buf, p, ret);
674 buf[ret++] = '\n';
675 buf[ret] = 0;
676 }
677
678 return ret;
679}
680
681static ssize_t loop_attr_offset_show(struct loop_device *lo, char *buf)
682{
683 return sprintf(buf, "%llu\n", (unsigned long long)lo->lo_offset);
684}
685
686static ssize_t loop_attr_sizelimit_show(struct loop_device *lo, char *buf)
687{
688 return sprintf(buf, "%llu\n", (unsigned long long)lo->lo_sizelimit);
689}
690
691static ssize_t loop_attr_autoclear_show(struct loop_device *lo, char *buf)
692{
693 int autoclear = (lo->lo_flags & LO_FLAGS_AUTOCLEAR);
694
695 return sprintf(buf, "%s\n", autoclear ? "1" : "0");
696}
697
Kay Sieverse03c8dd2011-08-23 20:12:04 +0200698static ssize_t loop_attr_partscan_show(struct loop_device *lo, char *buf)
699{
700 int partscan = (lo->lo_flags & LO_FLAGS_PARTSCAN);
701
702 return sprintf(buf, "%s\n", partscan ? "1" : "0");
703}
704
Ming Lei2e5ab5f2015-08-17 10:31:49 +0800705static ssize_t loop_attr_dio_show(struct loop_device *lo, char *buf)
706{
707 int dio = (lo->lo_flags & LO_FLAGS_DIRECT_IO);
708
709 return sprintf(buf, "%s\n", dio ? "1" : "0");
710}
711
Milan Brozee862732010-08-23 15:16:00 +0200712LOOP_ATTR_RO(backing_file);
713LOOP_ATTR_RO(offset);
714LOOP_ATTR_RO(sizelimit);
715LOOP_ATTR_RO(autoclear);
Kay Sieverse03c8dd2011-08-23 20:12:04 +0200716LOOP_ATTR_RO(partscan);
Ming Lei2e5ab5f2015-08-17 10:31:49 +0800717LOOP_ATTR_RO(dio);
Milan Brozee862732010-08-23 15:16:00 +0200718
719static struct attribute *loop_attrs[] = {
720 &loop_attr_backing_file.attr,
721 &loop_attr_offset.attr,
722 &loop_attr_sizelimit.attr,
723 &loop_attr_autoclear.attr,
Kay Sieverse03c8dd2011-08-23 20:12:04 +0200724 &loop_attr_partscan.attr,
Ming Lei2e5ab5f2015-08-17 10:31:49 +0800725 &loop_attr_dio.attr,
Milan Brozee862732010-08-23 15:16:00 +0200726 NULL,
727};
728
729static struct attribute_group loop_attribute_group = {
730 .name = "loop",
731 .attrs= loop_attrs,
732};
733
Tetsuo Handad3349b62018-05-04 10:58:09 -0600734static void loop_sysfs_init(struct loop_device *lo)
Milan Brozee862732010-08-23 15:16:00 +0200735{
Tetsuo Handad3349b62018-05-04 10:58:09 -0600736 lo->sysfs_inited = !sysfs_create_group(&disk_to_dev(lo->lo_disk)->kobj,
737 &loop_attribute_group);
Milan Brozee862732010-08-23 15:16:00 +0200738}
739
740static void loop_sysfs_exit(struct loop_device *lo)
741{
Tetsuo Handad3349b62018-05-04 10:58:09 -0600742 if (lo->sysfs_inited)
743 sysfs_remove_group(&disk_to_dev(lo->lo_disk)->kobj,
744 &loop_attribute_group);
Milan Brozee862732010-08-23 15:16:00 +0200745}
746
Lukas Czernerdfaa2ef2011-08-19 14:50:46 +0200747static void loop_config_discard(struct loop_device *lo)
748{
749 struct file *file = lo->lo_backing_file;
750 struct inode *inode = file->f_mapping->host;
751 struct request_queue *q = lo->lo_queue;
Ming Leibcb21c82020-08-17 18:01:30 +0800752 u32 granularity, max_discard_sectors;
Lukas Czernerdfaa2ef2011-08-19 14:50:46 +0200753
754 /*
Evan Greenc52abf52020-04-03 16:43:04 +0200755 * If the backing device is a block device, mirror its zeroing
756 * capability. Set the discard sectors to the block device's zeroing
757 * capabilities because loop discards result in blkdev_issue_zeroout(),
758 * not blkdev_issue_discard(). This maintains consistent behavior with
759 * file-backed loop devices: discarded regions read back as zero.
760 */
Christoph Hellwig47e96242021-10-19 09:56:39 +0200761 if (S_ISBLK(inode->i_mode)) {
Christoph Hellwig4e7b5672020-11-23 13:38:40 +0100762 struct request_queue *backingq = bdev_get_queue(I_BDEV(inode));
Evan Greenc52abf52020-04-03 16:43:04 +0200763
Ming Leibcb21c82020-08-17 18:01:30 +0800764 max_discard_sectors = backingq->limits.max_write_zeroes_sectors;
765 granularity = backingq->limits.discard_granularity ?:
766 queue_physical_block_size(backingq);
Evan Greenc52abf52020-04-03 16:43:04 +0200767
768 /*
Lukas Czernerdfaa2ef2011-08-19 14:50:46 +0200769 * We use punch hole to reclaim the free space used by the
Christoph Hellwig47e96242021-10-19 09:56:39 +0200770 * image a.k.a. discard.
Lukas Czernerdfaa2ef2011-08-19 14:50:46 +0200771 */
Christoph Hellwig47e96242021-10-19 09:56:39 +0200772 } else if (!file->f_op->fallocate) {
Ming Leibcb21c82020-08-17 18:01:30 +0800773 max_discard_sectors = 0;
774 granularity = 0;
Evan Greenc52abf52020-04-03 16:43:04 +0200775
776 } else {
Ming Leibcb21c82020-08-17 18:01:30 +0800777 max_discard_sectors = UINT_MAX >> 9;
778 granularity = inode->i_sb->s_blocksize;
Lukas Czernerdfaa2ef2011-08-19 14:50:46 +0200779 }
780
Ming Leibcb21c82020-08-17 18:01:30 +0800781 if (max_discard_sectors) {
782 q->limits.discard_granularity = granularity;
783 blk_queue_max_discard_sectors(q, max_discard_sectors);
784 blk_queue_max_write_zeroes_sectors(q, max_discard_sectors);
Evan Greenc52abf52020-04-03 16:43:04 +0200785 blk_queue_flag_set(QUEUE_FLAG_DISCARD, q);
Ming Leibcb21c82020-08-17 18:01:30 +0800786 } else {
787 q->limits.discard_granularity = 0;
788 blk_queue_max_discard_sectors(q, 0);
789 blk_queue_max_write_zeroes_sectors(q, 0);
Evan Greenc52abf52020-04-03 16:43:04 +0200790 blk_queue_flag_clear(QUEUE_FLAG_DISCARD, q);
Ming Leibcb21c82020-08-17 18:01:30 +0800791 }
792 q->limits.discard_alignment = 0;
Lukas Czernerdfaa2ef2011-08-19 14:50:46 +0200793}
794
Dan Schatzberg87579e92021-06-28 19:38:15 -0700795struct loop_worker {
796 struct rb_node rb_node;
797 struct work_struct work;
798 struct list_head cmd_list;
799 struct list_head idle_list;
800 struct loop_device *lo;
Dan Schatzbergc74d40e2021-06-28 19:38:21 -0700801 struct cgroup_subsys_state *blkcg_css;
Dan Schatzberg87579e92021-06-28 19:38:15 -0700802 unsigned long last_ran_at;
803};
Ming Leie03a3d72015-08-17 10:31:48 +0800804
Dan Schatzberg87579e92021-06-28 19:38:15 -0700805static void loop_workfn(struct work_struct *work);
806static void loop_rootcg_workfn(struct work_struct *work);
807static void loop_free_idle_workers(struct timer_list *timer);
NeilBrownb2ee7d42017-06-16 15:02:09 +1000808
Dan Schatzberg87579e92021-06-28 19:38:15 -0700809#ifdef CONFIG_BLK_CGROUP
810static inline int queue_on_root_worker(struct cgroup_subsys_state *css)
Ming Leie03a3d72015-08-17 10:31:48 +0800811{
Dan Schatzberg87579e92021-06-28 19:38:15 -0700812 return !css || css == blkcg_root_css;
813}
814#else
815static inline int queue_on_root_worker(struct cgroup_subsys_state *css)
816{
817 return !css;
818}
819#endif
820
821static void loop_queue_work(struct loop_device *lo, struct loop_cmd *cmd)
822{
Colin Ian King413ec802022-01-13 00:14:32 +0000823 struct rb_node **node, *parent = NULL;
Dan Schatzberg87579e92021-06-28 19:38:15 -0700824 struct loop_worker *cur_worker, *worker = NULL;
825 struct work_struct *work;
826 struct list_head *cmd_list;
827
828 spin_lock_irq(&lo->lo_work_lock);
829
Dan Schatzbergc74d40e2021-06-28 19:38:21 -0700830 if (queue_on_root_worker(cmd->blkcg_css))
Dan Schatzberg87579e92021-06-28 19:38:15 -0700831 goto queue_work;
832
833 node = &lo->worker_tree.rb_node;
834
835 while (*node) {
836 parent = *node;
837 cur_worker = container_of(*node, struct loop_worker, rb_node);
Dan Schatzbergc74d40e2021-06-28 19:38:21 -0700838 if (cur_worker->blkcg_css == cmd->blkcg_css) {
Dan Schatzberg87579e92021-06-28 19:38:15 -0700839 worker = cur_worker;
840 break;
Dan Schatzbergc74d40e2021-06-28 19:38:21 -0700841 } else if ((long)cur_worker->blkcg_css < (long)cmd->blkcg_css) {
Dan Schatzberg87579e92021-06-28 19:38:15 -0700842 node = &(*node)->rb_left;
843 } else {
844 node = &(*node)->rb_right;
845 }
846 }
847 if (worker)
848 goto queue_work;
849
850 worker = kzalloc(sizeof(struct loop_worker), GFP_NOWAIT | __GFP_NOWARN);
851 /*
852 * In the event we cannot allocate a worker, just queue on the
Dan Schatzbergc74d40e2021-06-28 19:38:21 -0700853 * rootcg worker and issue the I/O as the rootcg
Dan Schatzberg87579e92021-06-28 19:38:15 -0700854 */
Dan Schatzbergc74d40e2021-06-28 19:38:21 -0700855 if (!worker) {
856 cmd->blkcg_css = NULL;
857 if (cmd->memcg_css)
858 css_put(cmd->memcg_css);
859 cmd->memcg_css = NULL;
Dan Schatzberg87579e92021-06-28 19:38:15 -0700860 goto queue_work;
Dan Schatzbergc74d40e2021-06-28 19:38:21 -0700861 }
Dan Schatzberg87579e92021-06-28 19:38:15 -0700862
Dan Schatzbergc74d40e2021-06-28 19:38:21 -0700863 worker->blkcg_css = cmd->blkcg_css;
864 css_get(worker->blkcg_css);
Dan Schatzberg87579e92021-06-28 19:38:15 -0700865 INIT_WORK(&worker->work, loop_workfn);
866 INIT_LIST_HEAD(&worker->cmd_list);
867 INIT_LIST_HEAD(&worker->idle_list);
868 worker->lo = lo;
869 rb_link_node(&worker->rb_node, parent, node);
870 rb_insert_color(&worker->rb_node, &lo->worker_tree);
871queue_work:
872 if (worker) {
873 /*
874 * We need to remove from the idle list here while
875 * holding the lock so that the idle timer doesn't
876 * free the worker
877 */
878 if (!list_empty(&worker->idle_list))
879 list_del_init(&worker->idle_list);
880 work = &worker->work;
881 cmd_list = &worker->cmd_list;
882 } else {
883 work = &lo->rootcg_work;
884 cmd_list = &lo->rootcg_cmd_list;
885 }
886 list_add_tail(&cmd->list_entry, cmd_list);
887 queue_work(lo->workqueue, work);
888 spin_unlock_irq(&lo->lo_work_lock);
Ming Leie03a3d72015-08-17 10:31:48 +0800889}
890
Holger Hoffstätte56a85fd2019-02-12 15:54:24 -0700891static void loop_update_rotational(struct loop_device *lo)
892{
893 struct file *file = lo->lo_backing_file;
894 struct inode *file_inode = file->f_mapping->host;
895 struct block_device *file_bdev = file_inode->i_sb->s_bdev;
896 struct request_queue *q = lo->lo_queue;
897 bool nonrot = true;
898
899 /* not all filesystems (e.g. tmpfs) have a sb->s_bdev */
900 if (file_bdev)
901 nonrot = blk_queue_nonrot(bdev_get_queue(file_bdev));
902
903 if (nonrot)
904 blk_queue_flag_set(QUEUE_FLAG_NONROT, q);
905 else
906 blk_queue_flag_clear(QUEUE_FLAG_NONROT, q);
907}
908
Martijn Coenen62ab4662020-05-13 15:38:42 +0200909/**
910 * loop_set_status_from_info - configure device from loop_info
911 * @lo: struct loop_device to configure
912 * @info: struct loop_info64 to configure the device with
913 *
914 * Configures the loop device parameters according to the passed
915 * in loop_info64 configuration.
916 */
917static int
918loop_set_status_from_info(struct loop_device *lo,
919 const struct loop_info64 *info)
920{
Martijn Coenen62ab4662020-05-13 15:38:42 +0200921 if ((unsigned int) info->lo_encrypt_key_size > LO_KEY_SIZE)
922 return -EINVAL;
923
Christoph Hellwig47e96242021-10-19 09:56:39 +0200924 switch (info->lo_encrypt_type) {
925 case LO_CRYPT_NONE:
926 break;
927 case LO_CRYPT_XOR:
928 pr_warn("support for the xor transformation has been removed.\n");
929 return -EINVAL;
930 case LO_CRYPT_CRYPTOAPI:
931 pr_warn("support for cryptoloop has been removed. Use dm-crypt instead.\n");
932 return -EINVAL;
933 default:
934 return -EINVAL;
935 }
Martijn Coenen62ab4662020-05-13 15:38:42 +0200936
937 lo->lo_offset = info->lo_offset;
938 lo->lo_sizelimit = info->lo_sizelimit;
939 memcpy(lo->lo_file_name, info->lo_file_name, LO_NAME_SIZE);
Martijn Coenen62ab4662020-05-13 15:38:42 +0200940 lo->lo_file_name[LO_NAME_SIZE-1] = 0;
Martijn Coenenfaf1d252020-05-13 15:38:44 +0200941 lo->lo_flags = info->lo_flags;
Martijn Coenen62ab4662020-05-13 15:38:42 +0200942 return 0;
943}
944
Martijn Coenen34489142020-05-13 15:38:45 +0200945static int loop_configure(struct loop_device *lo, fmode_t mode,
946 struct block_device *bdev,
947 const struct loop_config *config)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700948{
Tetsuo Handa3ce6e1f2021-07-06 23:40:34 +0900949 struct file *file = fget(config->fd);
950 struct inode *inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700951 struct address_space *mapping;
Tetsuo Handa3ce6e1f2021-07-06 23:40:34 +0900952 int error;
953 loff_t size;
954 bool partscan;
955 unsigned short bsize;
956 bool is_loop;
957
958 if (!file)
959 return -EBADF;
960 is_loop = is_loop_device(file);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700961
962 /* This is safe, since we have a reference from open(). */
963 __module_get(THIS_MODULE);
964
Jan Kara33ec3e52019-05-16 16:01:27 +0200965 /*
966 * If we don't hold exclusive handle for the device, upgrade to it
967 * here to avoid changing device under exclusive owner.
968 */
969 if (!(mode & FMODE_EXCL)) {
Christoph Hellwig37c3fc92020-11-25 21:20:08 +0100970 error = bd_prepare_to_claim(bdev, loop_configure);
Christoph Hellwigecbe6bc2020-07-16 16:33:09 +0200971 if (error)
Jan Kara33ec3e52019-05-16 16:01:27 +0200972 goto out_putf;
973 }
974
Tetsuo Handa3ce6e1f2021-07-06 23:40:34 +0900975 error = loop_global_lock_killable(lo, is_loop);
Jan Kara757ecf42018-11-08 14:01:10 +0100976 if (error)
Jan Kara33ec3e52019-05-16 16:01:27 +0200977 goto out_bdev;
Jan Kara757ecf42018-11-08 14:01:10 +0100978
Linus Torvalds1da177e2005-04-16 15:20:36 -0700979 error = -EBUSY;
980 if (lo->lo_state != Lo_unbound)
Jan Kara757ecf42018-11-08 14:01:10 +0100981 goto out_unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700982
Theodore Ts'od2ac8382018-05-07 11:37:58 -0400983 error = loop_validate_file(file, bdev);
984 if (error)
Jan Kara757ecf42018-11-08 14:01:10 +0100985 goto out_unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700986
987 mapping = file->f_mapping;
988 inode = mapping->host;
989
Martijn Coenen34489142020-05-13 15:38:45 +0200990 if ((config->info.lo_flags & ~LOOP_CONFIGURE_SETTABLE_FLAGS) != 0) {
991 error = -EINVAL;
992 goto out_unlock;
993 }
994
995 if (config->block_size) {
Xie Yongjiaf3c5702021-10-26 22:40:14 +0800996 error = blk_validate_block_size(config->block_size);
Martijn Coenen34489142020-05-13 15:38:45 +0200997 if (error)
998 goto out_unlock;
999 }
1000
1001 error = loop_set_status_from_info(lo, &config->info);
1002 if (error)
1003 goto out_unlock;
1004
Christoph Hellwig456be142011-10-17 12:57:20 +02001005 if (!(file->f_mode & FMODE_WRITE) || !(mode & FMODE_WRITE) ||
Al Viro283e7e52015-04-03 15:21:59 -04001006 !file->f_op->write_iter)
Martijn Coenen34489142020-05-13 15:38:45 +02001007 lo->lo_flags |= LO_FLAGS_READ_ONLY;
Martijn Coenen083a6a52020-05-13 15:38:36 +02001008
Dan Schatzberg87579e92021-06-28 19:38:15 -07001009 lo->workqueue = alloc_workqueue("loop%d",
1010 WQ_UNBOUND | WQ_FREEZABLE,
1011 0,
1012 lo->lo_number);
1013 if (!lo->workqueue) {
1014 error = -ENOMEM;
Jan Kara757ecf42018-11-08 14:01:10 +01001015 goto out_unlock;
Dan Schatzberg87579e92021-06-28 19:38:15 -07001016 }
Christoph Hellwig456be142011-10-17 12:57:20 +02001017
Matteo Croce9f65c482021-07-13 01:05:30 +02001018 disk_force_media_change(lo->lo_disk, DISK_EVENT_MEDIA_CHANGE);
Christoph Hellwig7a2f0ce2020-11-03 11:00:16 +01001019 set_disk_ro(lo->lo_disk, (lo->lo_flags & LO_FLAGS_READ_ONLY) != 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001020
Dan Schatzberg87579e92021-06-28 19:38:15 -07001021 INIT_WORK(&lo->rootcg_work, loop_rootcg_workfn);
1022 INIT_LIST_HEAD(&lo->rootcg_cmd_list);
1023 INIT_LIST_HEAD(&lo->idle_worker_list);
1024 lo->worker_tree = RB_ROOT;
1025 timer_setup(&lo->timer, loop_free_idle_workers,
1026 TIMER_DEFERRABLE);
Martijn Coenen34489142020-05-13 15:38:45 +02001027 lo->use_dio = lo->lo_flags & LO_FLAGS_DIRECT_IO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001028 lo->lo_device = bdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001029 lo->lo_backing_file = file;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001030 lo->old_gfp_mask = mapping_gfp_mask(mapping);
1031 mapping_set_gfp_mask(mapping, lo->old_gfp_mask & ~(__GFP_IO|__GFP_FS));
1032
Martijn Coenen34489142020-05-13 15:38:45 +02001033 if (!(lo->lo_flags & LO_FLAGS_READ_ONLY) && file->f_op->fsync)
Jens Axboe21d07272016-03-30 10:09:35 -06001034 blk_queue_write_cache(lo->lo_queue, true, false);
Nikanth Karthikesan68db1962009-03-24 12:29:54 +01001035
Martijn Coenen34489142020-05-13 15:38:45 +02001036 if (config->block_size)
1037 bsize = config->block_size;
Linus Torvalds96ed3202020-06-02 19:45:12 -07001038 else if ((lo->lo_backing_file->f_flags & O_DIRECT) && inode->i_sb->s_bdev)
Martijn Coenen85560112019-09-04 21:49:01 +02001039 /* In case of direct I/O, match underlying block size */
Martijn Coenen34489142020-05-13 15:38:45 +02001040 bsize = bdev_logical_block_size(inode->i_sb->s_bdev);
1041 else
1042 bsize = 512;
Martijn Coenen85560112019-09-04 21:49:01 +02001043
Martijn Coenen34489142020-05-13 15:38:45 +02001044 blk_queue_logical_block_size(lo->lo_queue, bsize);
1045 blk_queue_physical_block_size(lo->lo_queue, bsize);
1046 blk_queue_io_min(lo->lo_queue, bsize);
Martijn Coenen85560112019-09-04 21:49:01 +02001047
Kristian Klausen2b9ac222021-06-18 13:51:57 +02001048 loop_config_discard(lo);
Holger Hoffstätte56a85fd2019-02-12 15:54:24 -07001049 loop_update_rotational(lo);
Ming Lei2e5ab5f2015-08-17 10:31:49 +08001050 loop_update_dio(lo);
Milan Brozee862732010-08-23 15:16:00 +02001051 loop_sysfs_init(lo);
Martijn Coenen79e5dc52020-08-25 09:18:29 +02001052
1053 size = get_loop_size(lo, file);
Martijn Coenen5795b6f2020-05-13 15:38:37 +02001054 loop_set_size(lo, size);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001055
Tetsuo Handa3ce6e1f2021-07-06 23:40:34 +09001056 /* Order wrt reading lo_state in loop_validate_file(). */
1057 wmb();
1058
Serge E. Hallyn6c997912006-09-29 01:59:11 -07001059 lo->lo_state = Lo_bound;
Kay Sieverse03c8dd2011-08-23 20:12:04 +02001060 if (part_shift)
1061 lo->lo_flags |= LO_FLAGS_PARTSCAN;
Jan Kara85b0a542018-11-08 14:01:13 +01001062 partscan = lo->lo_flags & LO_FLAGS_PARTSCAN;
Lennart Poetteringfe6a8fc2020-08-10 19:16:32 +02001063 if (partscan)
Christoph Hellwig46e7eac2021-11-22 14:06:17 +01001064 lo->lo_disk->flags &= ~GENHD_FL_NO_PART;
Anatol Pomozovc1681bf2013-04-01 09:47:56 -07001065
Tetsuo Handa3ce6e1f2021-07-06 23:40:34 +09001066 loop_global_unlock(lo, is_loop);
Jan Kara85b0a542018-11-08 14:01:13 +01001067 if (partscan)
Christoph Hellwig03842642021-06-24 14:32:40 +02001068 loop_reread_partitions(lo);
Christoph Hellwig37c3fc92020-11-25 21:20:08 +01001069 if (!(mode & FMODE_EXCL))
1070 bd_abort_claiming(bdev, loop_configure);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001071 return 0;
1072
Jan Kara757ecf42018-11-08 14:01:10 +01001073out_unlock:
Tetsuo Handa3ce6e1f2021-07-06 23:40:34 +09001074 loop_global_unlock(lo, is_loop);
Jan Kara33ec3e52019-05-16 16:01:27 +02001075out_bdev:
Christoph Hellwig37c3fc92020-11-25 21:20:08 +01001076 if (!(mode & FMODE_EXCL))
1077 bd_abort_claiming(bdev, loop_configure);
Jan Kara757ecf42018-11-08 14:01:10 +01001078out_putf:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001079 fput(file);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001080 /* This is safe: open() is still holding a reference. */
1081 module_put(THIS_MODULE);
1082 return error;
1083}
1084
Tetsuo Handabf237472022-02-11 16:15:54 +09001085static void __loop_clr_fd(struct loop_device *lo, bool release)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001086{
Tetsuo Handa6050fa42021-11-24 19:47:40 +09001087 struct file *filp;
Al Virob4e3ca12005-10-21 03:22:34 -04001088 gfp_t gfp = lo->old_gfp_mask;
Dan Schatzberg87579e92021-06-28 19:38:15 -07001089 struct loop_worker *pos, *worker;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001090
Tetsuo Handa3ce6e1f2021-07-06 23:40:34 +09001091 /*
1092 * Flush loop_configure() and loop_change_fd(). It is acceptable for
1093 * loop_validate_file() to succeed, for actual clear operation has not
1094 * started yet.
1095 */
1096 mutex_lock(&loop_validate_mutex);
1097 mutex_unlock(&loop_validate_mutex);
1098 /*
1099 * loop_validate_file() now fails because l->lo_state != Lo_bound
1100 * became visible.
1101 */
1102
Tetsuo Handa6050fa42021-11-24 19:47:40 +09001103 /*
1104 * Since this function is called upon "ioctl(LOOP_CLR_FD)" xor "close()
1105 * after ioctl(LOOP_CLR_FD)", it is a sign of something going wrong if
1106 * lo->lo_state has changed while waiting for lo->lo_mutex.
1107 */
Pavel Tatashin6cc8e742021-01-26 09:46:30 -05001108 mutex_lock(&lo->lo_mutex);
Tetsuo Handa6050fa42021-11-24 19:47:40 +09001109 BUG_ON(lo->lo_state != Lo_rundown);
1110 mutex_unlock(&lo->lo_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001111
Mauricio Faria de Oliveira4ceddce2021-02-22 12:41:23 -03001112 if (test_bit(QUEUE_FLAG_WC, &lo->lo_queue->queue_flags))
1113 blk_queue_write_cache(lo->lo_queue, false, false);
1114
Ming Leif8933662015-05-06 12:26:23 +08001115 /* freeze request queue during the transition */
1116 blk_mq_freeze_queue(lo->lo_queue);
1117
Dan Schatzberg87579e92021-06-28 19:38:15 -07001118 destroy_workqueue(lo->workqueue);
1119 spin_lock_irq(&lo->lo_work_lock);
1120 list_for_each_entry_safe(worker, pos, &lo->idle_worker_list,
1121 idle_list) {
1122 list_del(&worker->idle_list);
1123 rb_erase(&worker->rb_node, &lo->worker_tree);
Dan Schatzbergc74d40e2021-06-28 19:38:21 -07001124 css_put(worker->blkcg_css);
Dan Schatzberg87579e92021-06-28 19:38:15 -07001125 kfree(worker);
1126 }
1127 spin_unlock_irq(&lo->lo_work_lock);
1128 del_timer_sync(&lo->timer);
1129
Linus Torvalds1da177e2005-04-16 15:20:36 -07001130 spin_lock_irq(&lo->lo_lock);
Tetsuo Handa6050fa42021-11-24 19:47:40 +09001131 filp = lo->lo_backing_file;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001132 lo->lo_backing_file = NULL;
Kay Sievers05eb0f22011-07-31 22:21:35 +02001133 spin_unlock_irq(&lo->lo_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001134
Linus Torvalds1da177e2005-04-16 15:20:36 -07001135 lo->lo_device = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001136 lo->lo_offset = 0;
1137 lo->lo_sizelimit = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001138 memset(lo->lo_file_name, 0, LO_NAME_SIZE);
Omar Sandoval89e4fde2017-08-24 00:03:43 -07001139 blk_queue_logical_block_size(lo->lo_queue, 512);
Omar Sandovalbf093752017-09-05 14:24:47 -07001140 blk_queue_physical_block_size(lo->lo_queue, 512);
1141 blk_queue_io_min(lo->lo_queue, 512);
Xie Yongjie515be82021-09-22 20:37:09 +08001142 invalidate_disk(lo->lo_disk);
Milan Broz51a0bb02010-10-27 19:51:30 -06001143 loop_sysfs_exit(lo);
Xie Yongji19f553d2021-09-22 20:37:10 +08001144 /* let user-space know about this change */
1145 kobject_uevent(&disk_to_dev(lo->lo_disk)->kobj, KOBJ_CHANGE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001146 mapping_set_gfp_mask(filp->f_mapping, gfp);
Tetsuo Handabf237472022-02-11 16:15:54 +09001147 /* This is safe: open() is still holding a reference. */
1148 module_put(THIS_MODULE);
Ming Leif8933662015-05-06 12:26:23 +08001149 blk_mq_unfreeze_queue(lo->lo_queue);
1150
Matteo Croce9f65c482021-07-13 01:05:30 +02001151 disk_force_media_change(lo->lo_disk, DISK_EVENT_MEDIA_CHANGE);
Tetsuo Handa6050fa42021-11-24 19:47:40 +09001152
1153 if (lo->lo_flags & LO_FLAGS_PARTSCAN) {
1154 int err;
1155
Tetsuo Handabf237472022-02-11 16:15:54 +09001156 /*
1157 * open_mutex has been held already in release path, so don't
1158 * acquire it if this function is called in such case.
1159 *
1160 * If the reread partition isn't from release path, lo_refcnt
1161 * must be at least one and it can only become zero when the
1162 * current holder is released.
1163 */
1164 if (!release)
1165 mutex_lock(&lo->lo_disk->open_mutex);
Christoph Hellwig03842642021-06-24 14:32:40 +02001166 err = bdev_disk_changed(lo->lo_disk, false);
Tetsuo Handabf237472022-02-11 16:15:54 +09001167 if (!release)
1168 mutex_unlock(&lo->lo_disk->open_mutex);
Dongli Zhang40853d62019-02-22 22:10:19 +08001169 if (err)
1170 pr_warn("%s: partition scan of loop%d failed (rc=%d)\n",
Tetsuo Handa6050fa42021-11-24 19:47:40 +09001171 __func__, lo->lo_number, err);
Jan Karad57f3372018-11-08 14:01:12 +01001172 /* Device is gone, no point in returning error */
Jan Karad57f3372018-11-08 14:01:12 +01001173 }
Dongli Zhang758a58d2019-02-22 22:10:20 +08001174
Tetsuo Handabf237472022-02-11 16:15:54 +09001175 /*
1176 * lo->lo_state is set to Lo_unbound here after above partscan has
1177 * finished. There cannot be anybody else entering __loop_clr_fd() as
1178 * Lo_rundown state protects us from all the other places trying to
1179 * change the 'lo' device.
1180 */
Dongli Zhang758a58d2019-02-22 22:10:20 +08001181 lo->lo_flags = 0;
1182 if (!part_shift)
Christoph Hellwig46e7eac2021-11-22 14:06:17 +01001183 lo->lo_disk->flags |= GENHD_FL_NO_PART;
Tetsuo Handa6050fa42021-11-24 19:47:40 +09001184 mutex_lock(&lo->lo_mutex);
Dongli Zhang758a58d2019-02-22 22:10:20 +08001185 lo->lo_state = Lo_unbound;
Pavel Tatashin6cc8e742021-01-26 09:46:30 -05001186 mutex_unlock(&lo->lo_mutex);
Dongli Zhang758a58d2019-02-22 22:10:20 +08001187
Tetsuo Handabf237472022-02-11 16:15:54 +09001188 /*
1189 * Need not hold lo_mutex to fput backing file. Calling fput holding
1190 * lo_mutex triggers a circular lock dependency possibility warning as
1191 * fput can take open_mutex which is usually taken before lo_mutex.
1192 */
1193 fput(filp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001194}
1195
Jan Karaa2505b72018-11-08 14:01:06 +01001196static int loop_clr_fd(struct loop_device *lo)
1197{
Jan Kara7ccd0792018-11-08 14:01:07 +01001198 int err;
1199
Pavel Tatashin6cc8e742021-01-26 09:46:30 -05001200 err = mutex_lock_killable(&lo->lo_mutex);
Jan Kara7ccd0792018-11-08 14:01:07 +01001201 if (err)
1202 return err;
1203 if (lo->lo_state != Lo_bound) {
Pavel Tatashin6cc8e742021-01-26 09:46:30 -05001204 mutex_unlock(&lo->lo_mutex);
Jan Karaa2505b72018-11-08 14:01:06 +01001205 return -ENXIO;
Jan Kara7ccd0792018-11-08 14:01:07 +01001206 }
Jan Karaa2505b72018-11-08 14:01:06 +01001207 /*
1208 * If we've explicitly asked to tear down the loop device,
1209 * and it has an elevated reference count, set it for auto-teardown when
1210 * the last reference goes away. This stops $!~#$@ udev from
1211 * preventing teardown because it decided that it needs to run blkid on
1212 * the loopback device whenever they appear. xfstests is notorious for
1213 * failing tests because blkid via udev races with a losetup
1214 * <dev>/do something like mkfs/losetup -d <dev> causing the losetup -d
1215 * command to fail with EBUSY.
1216 */
1217 if (atomic_read(&lo->lo_refcnt) > 1) {
1218 lo->lo_flags |= LO_FLAGS_AUTOCLEAR;
Pavel Tatashin6cc8e742021-01-26 09:46:30 -05001219 mutex_unlock(&lo->lo_mutex);
Jan Karaa2505b72018-11-08 14:01:06 +01001220 return 0;
1221 }
1222 lo->lo_state = Lo_rundown;
Pavel Tatashin6cc8e742021-01-26 09:46:30 -05001223 mutex_unlock(&lo->lo_mutex);
Jan Karaa2505b72018-11-08 14:01:06 +01001224
Tetsuo Handabf237472022-02-11 16:15:54 +09001225 __loop_clr_fd(lo, false);
Tetsuo Handa6050fa42021-11-24 19:47:40 +09001226 return 0;
Jan Karaa2505b72018-11-08 14:01:06 +01001227}
1228
Linus Torvalds1da177e2005-04-16 15:20:36 -07001229static int
1230loop_set_status(struct loop_device *lo, const struct loop_info64 *info)
1231{
1232 int err;
Martijn Coenenfaf1d252020-05-13 15:38:44 +02001233 int prev_lo_flags;
Jan Kara85b0a542018-11-08 14:01:13 +01001234 bool partscan = false;
Martijn Coenen0c3796c2020-05-13 15:38:41 +02001235 bool size_changed = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001236
Pavel Tatashin6cc8e742021-01-26 09:46:30 -05001237 err = mutex_lock_killable(&lo->lo_mutex);
Jan Kara550df5fd2018-11-08 14:01:09 +01001238 if (err)
1239 return err;
Jan Kara550df5fd2018-11-08 14:01:09 +01001240 if (lo->lo_state != Lo_bound) {
1241 err = -ENXIO;
1242 goto out_unlock;
1243 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001244
Jaegeuk Kim5db470e2019-01-09 19:17:14 -08001245 if (lo->lo_offset != info->lo_offset ||
1246 lo->lo_sizelimit != info->lo_sizelimit) {
Martijn Coenen0c3796c2020-05-13 15:38:41 +02001247 size_changed = true;
Jaegeuk Kim5db470e2019-01-09 19:17:14 -08001248 sync_blockdev(lo->lo_device);
Zheng Binf4bd34b2020-06-18 12:21:37 +08001249 invalidate_bdev(lo->lo_device);
Jaegeuk Kim5db470e2019-01-09 19:17:14 -08001250 }
1251
Ming Leiecdd0952017-02-11 11:40:45 +08001252 /* I/O need to be drained during transfer transition */
1253 blk_mq_freeze_queue(lo->lo_queue);
1254
Martijn Coenen0c3796c2020-05-13 15:38:41 +02001255 if (size_changed && lo->lo_device->bd_inode->i_mapping->nrpages) {
Zheng Binf4bd34b2020-06-18 12:21:37 +08001256 /* If any pages were dirtied after invalidate_bdev(), try again */
Martijn Coenen0c3796c2020-05-13 15:38:41 +02001257 err = -EAGAIN;
1258 pr_warn("%s: loop%d (%s) has still dirty pages (nrpages=%lu)\n",
1259 __func__, lo->lo_number, lo->lo_file_name,
1260 lo->lo_device->bd_inode->i_mapping->nrpages);
1261 goto out_unfreeze;
1262 }
1263
Martijn Coenenfaf1d252020-05-13 15:38:44 +02001264 prev_lo_flags = lo->lo_flags;
1265
Martijn Coenen0c3796c2020-05-13 15:38:41 +02001266 err = loop_set_status_from_info(lo, info);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001267 if (err)
Jan Kara550df5fd2018-11-08 14:01:09 +01001268 goto out_unfreeze;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001269
Martijn Coenenfaf1d252020-05-13 15:38:44 +02001270 /* Mask out flags that can't be set using LOOP_SET_STATUS. */
Martijn Coenen6ac92fb2020-06-04 22:25:20 +02001271 lo->lo_flags &= LOOP_SET_STATUS_SETTABLE_FLAGS;
Martijn Coenenfaf1d252020-05-13 15:38:44 +02001272 /* For those flags, use the previous values instead */
1273 lo->lo_flags |= prev_lo_flags & ~LOOP_SET_STATUS_SETTABLE_FLAGS;
1274 /* For flags that can't be cleared, use previous values too */
1275 lo->lo_flags |= prev_lo_flags & ~LOOP_SET_STATUS_CLEARABLE_FLAGS;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001276
Martijn Coenenb0bd1582020-05-13 15:38:39 +02001277 if (size_changed) {
1278 loff_t new_size = get_size(lo->lo_offset, lo->lo_sizelimit,
1279 lo->lo_backing_file);
1280 loop_set_size(lo, new_size);
Arnd Bergmannb040ad92017-06-09 12:19:18 +02001281 }
Guo Chao541c7422013-02-21 15:16:46 -08001282
Lukas Czernerdfaa2ef2011-08-19 14:50:46 +02001283 loop_config_discard(lo);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001284
Ming Lei2e5ab5f2015-08-17 10:31:49 +08001285 /* update dio if lo_offset or transfer is changed */
1286 __loop_update_dio(lo, lo->use_dio);
1287
Jan Kara550df5fd2018-11-08 14:01:09 +01001288out_unfreeze:
Ming Leiecdd0952017-02-11 11:40:45 +08001289 blk_mq_unfreeze_queue(lo->lo_queue);
Omar Sandovale02898b42017-03-01 10:42:38 -08001290
Martijn Coenenfaf1d252020-05-13 15:38:44 +02001291 if (!err && (lo->lo_flags & LO_FLAGS_PARTSCAN) &&
1292 !(prev_lo_flags & LO_FLAGS_PARTSCAN)) {
Christoph Hellwig46e7eac2021-11-22 14:06:17 +01001293 lo->lo_disk->flags &= ~GENHD_FL_NO_PART;
Jan Kara85b0a542018-11-08 14:01:13 +01001294 partscan = true;
Omar Sandovale02898b42017-03-01 10:42:38 -08001295 }
Jan Kara550df5fd2018-11-08 14:01:09 +01001296out_unlock:
Pavel Tatashin6cc8e742021-01-26 09:46:30 -05001297 mutex_unlock(&lo->lo_mutex);
Jan Kara85b0a542018-11-08 14:01:13 +01001298 if (partscan)
Christoph Hellwig03842642021-06-24 14:32:40 +02001299 loop_reread_partitions(lo);
Omar Sandovale02898b42017-03-01 10:42:38 -08001300
Ming Leiecdd0952017-02-11 11:40:45 +08001301 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001302}
1303
1304static int
1305loop_get_status(struct loop_device *lo, struct loop_info64 *info)
1306{
Tetsuo Handab1ab5fa2018-11-08 14:01:01 +01001307 struct path path;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001308 struct kstat stat;
Omar Sandoval2d1d4c12018-03-26 21:39:11 -07001309 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001310
Pavel Tatashin6cc8e742021-01-26 09:46:30 -05001311 ret = mutex_lock_killable(&lo->lo_mutex);
Jan Kara4a5ce9b2018-11-08 14:01:08 +01001312 if (ret)
1313 return ret;
Omar Sandoval2d1d4c12018-03-26 21:39:11 -07001314 if (lo->lo_state != Lo_bound) {
Pavel Tatashin6cc8e742021-01-26 09:46:30 -05001315 mutex_unlock(&lo->lo_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001316 return -ENXIO;
Omar Sandoval2d1d4c12018-03-26 21:39:11 -07001317 }
1318
Linus Torvalds1da177e2005-04-16 15:20:36 -07001319 memset(info, 0, sizeof(*info));
1320 info->lo_number = lo->lo_number;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001321 info->lo_offset = lo->lo_offset;
1322 info->lo_sizelimit = lo->lo_sizelimit;
1323 info->lo_flags = lo->lo_flags;
1324 memcpy(info->lo_file_name, lo->lo_file_name, LO_NAME_SIZE);
Omar Sandoval2d1d4c12018-03-26 21:39:11 -07001325
Pavel Tatashin6cc8e742021-01-26 09:46:30 -05001326 /* Drop lo_mutex while we call into the filesystem. */
Tetsuo Handab1ab5fa2018-11-08 14:01:01 +01001327 path = lo->lo_backing_file->f_path;
1328 path_get(&path);
Pavel Tatashin6cc8e742021-01-26 09:46:30 -05001329 mutex_unlock(&lo->lo_mutex);
Tetsuo Handab1ab5fa2018-11-08 14:01:01 +01001330 ret = vfs_getattr(&path, &stat, STATX_INO, AT_STATX_SYNC_AS_STAT);
Omar Sandoval2d1d4c12018-03-26 21:39:11 -07001331 if (!ret) {
1332 info->lo_device = huge_encode_dev(stat.dev);
1333 info->lo_inode = stat.ino;
1334 info->lo_rdevice = huge_encode_dev(stat.rdev);
1335 }
Tetsuo Handab1ab5fa2018-11-08 14:01:01 +01001336 path_put(&path);
Omar Sandoval2d1d4c12018-03-26 21:39:11 -07001337 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001338}
1339
1340static void
1341loop_info64_from_old(const struct loop_info *info, struct loop_info64 *info64)
1342{
1343 memset(info64, 0, sizeof(*info64));
1344 info64->lo_number = info->lo_number;
1345 info64->lo_device = info->lo_device;
1346 info64->lo_inode = info->lo_inode;
1347 info64->lo_rdevice = info->lo_rdevice;
1348 info64->lo_offset = info->lo_offset;
1349 info64->lo_sizelimit = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001350 info64->lo_flags = info->lo_flags;
Christoph Hellwig47e96242021-10-19 09:56:39 +02001351 memcpy(info64->lo_file_name, info->lo_name, LO_NAME_SIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001352}
1353
1354static int
1355loop_info64_to_old(const struct loop_info64 *info64, struct loop_info *info)
1356{
1357 memset(info, 0, sizeof(*info));
1358 info->lo_number = info64->lo_number;
1359 info->lo_device = info64->lo_device;
1360 info->lo_inode = info64->lo_inode;
1361 info->lo_rdevice = info64->lo_rdevice;
1362 info->lo_offset = info64->lo_offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001363 info->lo_flags = info64->lo_flags;
Christoph Hellwig47e96242021-10-19 09:56:39 +02001364 memcpy(info->lo_name, info64->lo_file_name, LO_NAME_SIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001365
1366 /* error in case values were truncated */
1367 if (info->lo_device != info64->lo_device ||
1368 info->lo_rdevice != info64->lo_rdevice ||
1369 info->lo_inode != info64->lo_inode ||
1370 info->lo_offset != info64->lo_offset)
1371 return -EOVERFLOW;
1372
1373 return 0;
1374}
1375
1376static int
1377loop_set_status_old(struct loop_device *lo, const struct loop_info __user *arg)
1378{
1379 struct loop_info info;
1380 struct loop_info64 info64;
1381
1382 if (copy_from_user(&info, arg, sizeof (struct loop_info)))
1383 return -EFAULT;
1384 loop_info64_from_old(&info, &info64);
1385 return loop_set_status(lo, &info64);
1386}
1387
1388static int
1389loop_set_status64(struct loop_device *lo, const struct loop_info64 __user *arg)
1390{
1391 struct loop_info64 info64;
1392
1393 if (copy_from_user(&info64, arg, sizeof (struct loop_info64)))
1394 return -EFAULT;
1395 return loop_set_status(lo, &info64);
1396}
1397
1398static int
1399loop_get_status_old(struct loop_device *lo, struct loop_info __user *arg) {
1400 struct loop_info info;
1401 struct loop_info64 info64;
Omar Sandovalbdac616db2018-04-06 09:57:03 -07001402 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001403
Jan Kara4a5ce9b2018-11-08 14:01:08 +01001404 if (!arg)
Omar Sandovalbdac616db2018-04-06 09:57:03 -07001405 return -EINVAL;
Omar Sandovalbdac616db2018-04-06 09:57:03 -07001406 err = loop_get_status(lo, &info64);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001407 if (!err)
1408 err = loop_info64_to_old(&info64, &info);
1409 if (!err && copy_to_user(arg, &info, sizeof(info)))
1410 err = -EFAULT;
1411
1412 return err;
1413}
1414
1415static int
1416loop_get_status64(struct loop_device *lo, struct loop_info64 __user *arg) {
1417 struct loop_info64 info64;
Omar Sandovalbdac616db2018-04-06 09:57:03 -07001418 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001419
Jan Kara4a5ce9b2018-11-08 14:01:08 +01001420 if (!arg)
Omar Sandovalbdac616db2018-04-06 09:57:03 -07001421 return -EINVAL;
Omar Sandovalbdac616db2018-04-06 09:57:03 -07001422 err = loop_get_status(lo, &info64);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001423 if (!err && copy_to_user(arg, &info64, sizeof(info64)))
1424 err = -EFAULT;
1425
1426 return err;
1427}
1428
Hannes Reinecke51001b72017-06-08 13:46:44 +02001429static int loop_set_capacity(struct loop_device *lo)
J. R. Okajima53d66602009-03-31 15:23:43 -07001430{
Martijn Coenen0a6ed1b2020-05-13 15:38:40 +02001431 loff_t size;
1432
J. R. Okajima53d66602009-03-31 15:23:43 -07001433 if (unlikely(lo->lo_state != Lo_bound))
Guo Chao7b0576a2013-02-21 15:16:47 -08001434 return -ENXIO;
J. R. Okajima53d66602009-03-31 15:23:43 -07001435
Martijn Coenen0a6ed1b2020-05-13 15:38:40 +02001436 size = get_loop_size(lo, lo->lo_backing_file);
1437 loop_set_size(lo, size);
Martijn Coenen083a6a52020-05-13 15:38:36 +02001438
1439 return 0;
J. R. Okajima53d66602009-03-31 15:23:43 -07001440}
1441
Ming Leiab1cb272015-08-17 10:31:50 +08001442static int loop_set_dio(struct loop_device *lo, unsigned long arg)
1443{
1444 int error = -ENXIO;
1445 if (lo->lo_state != Lo_bound)
1446 goto out;
1447
1448 __loop_update_dio(lo, !!arg);
1449 if (lo->use_dio == !!arg)
1450 return 0;
1451 error = -EINVAL;
1452 out:
1453 return error;
1454}
1455
Omar Sandoval89e4fde2017-08-24 00:03:43 -07001456static int loop_set_block_size(struct loop_device *lo, unsigned long arg)
1457{
Jaegeuk Kim5db470e2019-01-09 19:17:14 -08001458 int err = 0;
1459
Omar Sandoval89e4fde2017-08-24 00:03:43 -07001460 if (lo->lo_state != Lo_bound)
1461 return -ENXIO;
1462
Xie Yongjiaf3c5702021-10-26 22:40:14 +08001463 err = blk_validate_block_size(arg);
Martijn Coenen34489142020-05-13 15:38:45 +02001464 if (err)
1465 return err;
Omar Sandoval89e4fde2017-08-24 00:03:43 -07001466
Martijn Coenen7e81f992020-03-10 14:12:30 +01001467 if (lo->lo_queue->limits.logical_block_size == arg)
1468 return 0;
1469
1470 sync_blockdev(lo->lo_device);
Zheng Binf4bd34b2020-06-18 12:21:37 +08001471 invalidate_bdev(lo->lo_device);
Jaegeuk Kim5db470e2019-01-09 19:17:14 -08001472
Omar Sandoval89e4fde2017-08-24 00:03:43 -07001473 blk_mq_freeze_queue(lo->lo_queue);
1474
Zheng Binf4bd34b2020-06-18 12:21:37 +08001475 /* invalidate_bdev should have truncated all the pages */
Martijn Coenen7e81f992020-03-10 14:12:30 +01001476 if (lo->lo_device->bd_inode->i_mapping->nrpages) {
Jaegeuk Kim5db470e2019-01-09 19:17:14 -08001477 err = -EAGAIN;
1478 pr_warn("%s: loop%d (%s) has still dirty pages (nrpages=%lu)\n",
1479 __func__, lo->lo_number, lo->lo_file_name,
1480 lo->lo_device->bd_inode->i_mapping->nrpages);
1481 goto out_unfreeze;
1482 }
1483
Omar Sandoval89e4fde2017-08-24 00:03:43 -07001484 blk_queue_logical_block_size(lo->lo_queue, arg);
Omar Sandovalbf093752017-09-05 14:24:47 -07001485 blk_queue_physical_block_size(lo->lo_queue, arg);
1486 blk_queue_io_min(lo->lo_queue, arg);
Omar Sandoval89e4fde2017-08-24 00:03:43 -07001487 loop_update_dio(lo);
Jaegeuk Kim5db470e2019-01-09 19:17:14 -08001488out_unfreeze:
Omar Sandoval89e4fde2017-08-24 00:03:43 -07001489 blk_mq_unfreeze_queue(lo->lo_queue);
1490
Jaegeuk Kim5db470e2019-01-09 19:17:14 -08001491 return err;
Omar Sandoval89e4fde2017-08-24 00:03:43 -07001492}
1493
Jan Karaa1316542018-11-08 14:01:05 +01001494static int lo_simple_ioctl(struct loop_device *lo, unsigned int cmd,
1495 unsigned long arg)
1496{
1497 int err;
1498
Pavel Tatashin6cc8e742021-01-26 09:46:30 -05001499 err = mutex_lock_killable(&lo->lo_mutex);
Jan Karaa1316542018-11-08 14:01:05 +01001500 if (err)
1501 return err;
1502 switch (cmd) {
1503 case LOOP_SET_CAPACITY:
1504 err = loop_set_capacity(lo);
1505 break;
1506 case LOOP_SET_DIRECT_IO:
1507 err = loop_set_dio(lo, arg);
1508 break;
1509 case LOOP_SET_BLOCK_SIZE:
1510 err = loop_set_block_size(lo, arg);
1511 break;
1512 default:
Christoph Hellwig47e96242021-10-19 09:56:39 +02001513 err = -EINVAL;
Jan Karaa1316542018-11-08 14:01:05 +01001514 }
Pavel Tatashin6cc8e742021-01-26 09:46:30 -05001515 mutex_unlock(&lo->lo_mutex);
Jan Karaa1316542018-11-08 14:01:05 +01001516 return err;
1517}
1518
Al Virobb214882008-03-02 09:29:48 -05001519static int lo_ioctl(struct block_device *bdev, fmode_t mode,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001520 unsigned int cmd, unsigned long arg)
1521{
Al Virobb214882008-03-02 09:29:48 -05001522 struct loop_device *lo = bdev->bd_disk->private_data;
Martijn Coenen571fae62020-05-13 15:38:43 +02001523 void __user *argp = (void __user *) arg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001524 int err;
1525
Linus Torvalds1da177e2005-04-16 15:20:36 -07001526 switch (cmd) {
Martijn Coenen34489142020-05-13 15:38:45 +02001527 case LOOP_SET_FD: {
1528 /*
1529 * Legacy case - pass in a zeroed out struct loop_config with
1530 * only the file descriptor set , which corresponds with the
1531 * default parameters we'd have used otherwise.
1532 */
1533 struct loop_config config;
1534
1535 memset(&config, 0, sizeof(config));
1536 config.fd = arg;
1537
1538 return loop_configure(lo, mode, bdev, &config);
1539 }
1540 case LOOP_CONFIGURE: {
1541 struct loop_config config;
1542
1543 if (copy_from_user(&config, argp, sizeof(config)))
1544 return -EFAULT;
1545
1546 return loop_configure(lo, mode, bdev, &config);
1547 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001548 case LOOP_CHANGE_FD:
Jan Karac3710772018-11-08 14:01:11 +01001549 return loop_change_fd(lo, bdev, arg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001550 case LOOP_CLR_FD:
Jan Kara7ccd0792018-11-08 14:01:07 +01001551 return loop_clr_fd(lo);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001552 case LOOP_SET_STATUS:
Dmitry Monakhov7035b5d2011-11-16 09:21:49 +01001553 err = -EPERM;
Jan Karaa1316542018-11-08 14:01:05 +01001554 if ((mode & FMODE_WRITE) || capable(CAP_SYS_ADMIN)) {
Martijn Coenen571fae62020-05-13 15:38:43 +02001555 err = loop_set_status_old(lo, argp);
Jan Karaa1316542018-11-08 14:01:05 +01001556 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001557 break;
1558 case LOOP_GET_STATUS:
Martijn Coenen571fae62020-05-13 15:38:43 +02001559 return loop_get_status_old(lo, argp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001560 case LOOP_SET_STATUS64:
Dmitry Monakhov7035b5d2011-11-16 09:21:49 +01001561 err = -EPERM;
Jan Karaa1316542018-11-08 14:01:05 +01001562 if ((mode & FMODE_WRITE) || capable(CAP_SYS_ADMIN)) {
Martijn Coenen571fae62020-05-13 15:38:43 +02001563 err = loop_set_status64(lo, argp);
Jan Karaa1316542018-11-08 14:01:05 +01001564 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001565 break;
1566 case LOOP_GET_STATUS64:
Martijn Coenen571fae62020-05-13 15:38:43 +02001567 return loop_get_status64(lo, argp);
J. R. Okajima53d66602009-03-31 15:23:43 -07001568 case LOOP_SET_CAPACITY:
Ming Leiab1cb272015-08-17 10:31:50 +08001569 case LOOP_SET_DIRECT_IO:
Omar Sandoval89e4fde2017-08-24 00:03:43 -07001570 case LOOP_SET_BLOCK_SIZE:
Jan Karaa1316542018-11-08 14:01:05 +01001571 if (!(mode & FMODE_WRITE) && !capable(CAP_SYS_ADMIN))
1572 return -EPERM;
Gustavo A. R. Silvadf561f662020-08-23 17:36:59 -05001573 fallthrough;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001574 default:
Jan Karaa1316542018-11-08 14:01:05 +01001575 err = lo_simple_ioctl(lo, cmd, arg);
1576 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001577 }
Nikanth Karthikesanf028f3b2009-03-24 12:33:41 +01001578
Linus Torvalds1da177e2005-04-16 15:20:36 -07001579 return err;
1580}
1581
David Howells863d5b822006-08-29 19:06:14 +01001582#ifdef CONFIG_COMPAT
1583struct compat_loop_info {
1584 compat_int_t lo_number; /* ioctl r/o */
1585 compat_dev_t lo_device; /* ioctl r/o */
1586 compat_ulong_t lo_inode; /* ioctl r/o */
1587 compat_dev_t lo_rdevice; /* ioctl r/o */
1588 compat_int_t lo_offset;
David Howells863d5b822006-08-29 19:06:14 +01001589 compat_int_t lo_encrypt_key_size; /* ioctl w/o */
1590 compat_int_t lo_flags; /* ioctl r/o */
1591 char lo_name[LO_NAME_SIZE];
1592 unsigned char lo_encrypt_key[LO_KEY_SIZE]; /* ioctl w/o */
1593 compat_ulong_t lo_init[2];
1594 char reserved[4];
1595};
1596
1597/*
1598 * Transfer 32-bit compatibility structure in userspace to 64-bit loop info
1599 * - noinlined to reduce stack space usage in main part of driver
1600 */
1601static noinline int
Al Viroba674cf2006-10-10 22:48:27 +01001602loop_info64_from_compat(const struct compat_loop_info __user *arg,
David Howells863d5b822006-08-29 19:06:14 +01001603 struct loop_info64 *info64)
1604{
1605 struct compat_loop_info info;
1606
1607 if (copy_from_user(&info, arg, sizeof(info)))
1608 return -EFAULT;
1609
1610 memset(info64, 0, sizeof(*info64));
1611 info64->lo_number = info.lo_number;
1612 info64->lo_device = info.lo_device;
1613 info64->lo_inode = info.lo_inode;
1614 info64->lo_rdevice = info.lo_rdevice;
1615 info64->lo_offset = info.lo_offset;
1616 info64->lo_sizelimit = 0;
David Howells863d5b822006-08-29 19:06:14 +01001617 info64->lo_flags = info.lo_flags;
Christoph Hellwig47e96242021-10-19 09:56:39 +02001618 memcpy(info64->lo_file_name, info.lo_name, LO_NAME_SIZE);
David Howells863d5b822006-08-29 19:06:14 +01001619 return 0;
1620}
1621
1622/*
1623 * Transfer 64-bit loop info to 32-bit compatibility structure in userspace
1624 * - noinlined to reduce stack space usage in main part of driver
1625 */
1626static noinline int
1627loop_info64_to_compat(const struct loop_info64 *info64,
1628 struct compat_loop_info __user *arg)
1629{
1630 struct compat_loop_info info;
1631
1632 memset(&info, 0, sizeof(info));
1633 info.lo_number = info64->lo_number;
1634 info.lo_device = info64->lo_device;
1635 info.lo_inode = info64->lo_inode;
1636 info.lo_rdevice = info64->lo_rdevice;
1637 info.lo_offset = info64->lo_offset;
David Howells863d5b822006-08-29 19:06:14 +01001638 info.lo_flags = info64->lo_flags;
Christoph Hellwig47e96242021-10-19 09:56:39 +02001639 memcpy(info.lo_name, info64->lo_file_name, LO_NAME_SIZE);
David Howells863d5b822006-08-29 19:06:14 +01001640
1641 /* error in case values were truncated */
1642 if (info.lo_device != info64->lo_device ||
1643 info.lo_rdevice != info64->lo_rdevice ||
1644 info.lo_inode != info64->lo_inode ||
Christoph Hellwig47e96242021-10-19 09:56:39 +02001645 info.lo_offset != info64->lo_offset)
David Howells863d5b822006-08-29 19:06:14 +01001646 return -EOVERFLOW;
1647
1648 if (copy_to_user(arg, &info, sizeof(info)))
1649 return -EFAULT;
1650 return 0;
1651}
1652
1653static int
1654loop_set_status_compat(struct loop_device *lo,
1655 const struct compat_loop_info __user *arg)
1656{
1657 struct loop_info64 info64;
1658 int ret;
1659
1660 ret = loop_info64_from_compat(arg, &info64);
1661 if (ret < 0)
1662 return ret;
1663 return loop_set_status(lo, &info64);
1664}
1665
1666static int
1667loop_get_status_compat(struct loop_device *lo,
1668 struct compat_loop_info __user *arg)
1669{
1670 struct loop_info64 info64;
Omar Sandovalbdac616db2018-04-06 09:57:03 -07001671 int err;
David Howells863d5b822006-08-29 19:06:14 +01001672
Jan Kara4a5ce9b2018-11-08 14:01:08 +01001673 if (!arg)
Omar Sandovalbdac616db2018-04-06 09:57:03 -07001674 return -EINVAL;
Omar Sandovalbdac616db2018-04-06 09:57:03 -07001675 err = loop_get_status(lo, &info64);
David Howells863d5b822006-08-29 19:06:14 +01001676 if (!err)
1677 err = loop_info64_to_compat(&info64, arg);
1678 return err;
1679}
1680
Al Virobb214882008-03-02 09:29:48 -05001681static int lo_compat_ioctl(struct block_device *bdev, fmode_t mode,
1682 unsigned int cmd, unsigned long arg)
David Howells863d5b822006-08-29 19:06:14 +01001683{
Al Virobb214882008-03-02 09:29:48 -05001684 struct loop_device *lo = bdev->bd_disk->private_data;
David Howells863d5b822006-08-29 19:06:14 +01001685 int err;
1686
David Howells863d5b822006-08-29 19:06:14 +01001687 switch(cmd) {
1688 case LOOP_SET_STATUS:
Jan Kara550df5fd2018-11-08 14:01:09 +01001689 err = loop_set_status_compat(lo,
1690 (const struct compat_loop_info __user *)arg);
David Howells863d5b822006-08-29 19:06:14 +01001691 break;
1692 case LOOP_GET_STATUS:
Jan Kara4a5ce9b2018-11-08 14:01:08 +01001693 err = loop_get_status_compat(lo,
1694 (struct compat_loop_info __user *)arg);
David Howells863d5b822006-08-29 19:06:14 +01001695 break;
J. R. Okajima53d66602009-03-31 15:23:43 -07001696 case LOOP_SET_CAPACITY:
David Howells863d5b822006-08-29 19:06:14 +01001697 case LOOP_CLR_FD:
1698 case LOOP_GET_STATUS64:
1699 case LOOP_SET_STATUS64:
Martijn Coenen34489142020-05-13 15:38:45 +02001700 case LOOP_CONFIGURE:
David Howells863d5b822006-08-29 19:06:14 +01001701 arg = (unsigned long) compat_ptr(arg);
Gustavo A. R. Silvadf561f662020-08-23 17:36:59 -05001702 fallthrough;
David Howells863d5b822006-08-29 19:06:14 +01001703 case LOOP_SET_FD:
1704 case LOOP_CHANGE_FD:
Evan Green9fea4b32018-07-02 16:03:46 -07001705 case LOOP_SET_BLOCK_SIZE:
Alessio Balsinifdbe4ee2019-08-07 01:48:28 +01001706 case LOOP_SET_DIRECT_IO:
Al Virobb214882008-03-02 09:29:48 -05001707 err = lo_ioctl(bdev, mode, cmd, arg);
David Howells863d5b822006-08-29 19:06:14 +01001708 break;
1709 default:
1710 err = -ENOIOCTLCMD;
1711 break;
1712 }
David Howells863d5b822006-08-29 19:06:14 +01001713 return err;
1714}
1715#endif
1716
Al Virobb214882008-03-02 09:29:48 -05001717static int lo_open(struct block_device *bdev, fmode_t mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001718{
Christoph Hellwig990e7812021-06-05 17:09:50 +03001719 struct loop_device *lo = bdev->bd_disk->private_data;
Jan Kara0a42e992018-11-08 14:01:04 +01001720 int err;
Kay Sievers770fe302011-07-31 22:08:04 +02001721
Pavel Tatashin6cc8e742021-01-26 09:46:30 -05001722 err = mutex_lock_killable(&lo->lo_mutex);
Pavel Tatashin6cc8e742021-01-26 09:46:30 -05001723 if (err)
1724 return err;
Christoph Hellwig990e7812021-06-05 17:09:50 +03001725 if (lo->lo_state == Lo_deleting)
1726 err = -ENXIO;
1727 else
1728 atomic_inc(&lo->lo_refcnt);
Pavel Tatashin6cc8e742021-01-26 09:46:30 -05001729 mutex_unlock(&lo->lo_mutex);
Christoph Hellwig990e7812021-06-05 17:09:50 +03001730 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001731}
1732
Jan Kara967d1dc2018-11-08 14:01:03 +01001733static void lo_release(struct gendisk *disk, fmode_t mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001734{
Pavel Tatashin6cc8e742021-01-26 09:46:30 -05001735 struct loop_device *lo = disk->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001736
Pavel Tatashin6cc8e742021-01-26 09:46:30 -05001737 mutex_lock(&lo->lo_mutex);
Ming Leif8933662015-05-06 12:26:23 +08001738 if (atomic_dec_return(&lo->lo_refcnt))
Jan Kara0a42e992018-11-08 14:01:04 +01001739 goto out_unlock;
Ming Leif8933662015-05-06 12:26:23 +08001740
Milan Broz14f27932008-12-12 14:48:27 +01001741 if (lo->lo_flags & LO_FLAGS_AUTOCLEAR) {
Jan Karaa2505b72018-11-08 14:01:06 +01001742 if (lo->lo_state != Lo_bound)
1743 goto out_unlock;
1744 lo->lo_state = Lo_rundown;
Pavel Tatashin6cc8e742021-01-26 09:46:30 -05001745 mutex_unlock(&lo->lo_mutex);
Milan Broz14f27932008-12-12 14:48:27 +01001746 /*
1747 * In autoclear mode, stop the loop thread
1748 * and remove configuration after last close.
1749 */
Tetsuo Handabf237472022-02-11 16:15:54 +09001750 __loop_clr_fd(lo, true);
Jan Kara7ccd0792018-11-08 14:01:07 +01001751 return;
Omar Sandoval43cade802017-08-24 00:03:44 -07001752 } else if (lo->lo_state == Lo_bound) {
Milan Broz14f27932008-12-12 14:48:27 +01001753 /*
1754 * Otherwise keep thread (if running) and config,
1755 * but flush possible ongoing bios in thread.
1756 */
Omar Sandoval43cade802017-08-24 00:03:44 -07001757 blk_mq_freeze_queue(lo->lo_queue);
1758 blk_mq_unfreeze_queue(lo->lo_queue);
Milan Broz14f27932008-12-12 14:48:27 +01001759 }
David Woodhouse96c58652008-02-06 01:36:27 -08001760
Jan Kara0a42e992018-11-08 14:01:04 +01001761out_unlock:
Pavel Tatashin6cc8e742021-01-26 09:46:30 -05001762 mutex_unlock(&lo->lo_mutex);
Linus Torvaldsae665012018-01-05 16:26:00 -08001763}
1764
Alexey Dobriyan83d5cde2009-09-21 17:01:13 -07001765static const struct block_device_operations lo_fops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001766 .owner = THIS_MODULE,
Al Virobb214882008-03-02 09:29:48 -05001767 .open = lo_open,
1768 .release = lo_release,
1769 .ioctl = lo_ioctl,
David Howells863d5b822006-08-29 19:06:14 +01001770#ifdef CONFIG_COMPAT
Al Virobb214882008-03-02 09:29:48 -05001771 .compat_ioctl = lo_compat_ioctl,
David Howells863d5b822006-08-29 19:06:14 +01001772#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001773};
1774
1775/*
1776 * And now the modules code and kernel interface.
1777 */
Ken Chen73285082007-05-08 00:28:20 -07001778static int max_loop;
Joe Perches5657a812018-05-24 13:38:59 -06001779module_param(max_loop, int, 0444);
Ken Chena47653f2007-06-08 13:46:44 -07001780MODULE_PARM_DESC(max_loop, "Maximum number of loop devices");
Joe Perches5657a812018-05-24 13:38:59 -06001781module_param(max_part, int, 0444);
Laurent Vivier476a4812008-03-26 12:11:53 +01001782MODULE_PARM_DESC(max_part, "Maximum number of partitions per loop device");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001783MODULE_LICENSE("GPL");
1784MODULE_ALIAS_BLOCKDEV_MAJOR(LOOP_MAJOR);
1785
Christoph Hellwigfc17b652017-06-03 09:38:05 +02001786static blk_status_t loop_queue_rq(struct blk_mq_hw_ctx *hctx,
Ming Leib5dd2f62014-12-31 13:22:57 +00001787 const struct blk_mq_queue_data *bd)
1788{
Jens Axboe1894e912018-04-13 16:24:29 -06001789 struct request *rq = bd->rq;
1790 struct loop_cmd *cmd = blk_mq_rq_to_pdu(rq);
1791 struct loop_device *lo = rq->q->queuedata;
Ming Leib5dd2f62014-12-31 13:22:57 +00001792
Jens Axboe1894e912018-04-13 16:24:29 -06001793 blk_mq_start_request(rq);
Ming Leib5dd2f62014-12-31 13:22:57 +00001794
Ming Leif4aa4c72015-05-05 19:49:54 +08001795 if (lo->lo_state != Lo_bound)
Christoph Hellwigfc17b652017-06-03 09:38:05 +02001796 return BLK_STS_IOERR;
Ming Leif4aa4c72015-05-05 19:49:54 +08001797
Jens Axboe1894e912018-04-13 16:24:29 -06001798 switch (req_op(rq)) {
Christoph Hellwigf0225ca2016-08-04 16:10:00 +02001799 case REQ_OP_FLUSH:
1800 case REQ_OP_DISCARD:
Christoph Hellwig19372e22017-04-05 19:21:15 +02001801 case REQ_OP_WRITE_ZEROES:
Ming Leibc07c102015-08-17 10:31:51 +08001802 cmd->use_aio = false;
Christoph Hellwigf0225ca2016-08-04 16:10:00 +02001803 break;
1804 default:
1805 cmd->use_aio = lo->use_dio;
1806 break;
1807 }
Ming Leibc07c102015-08-17 10:31:51 +08001808
Shaohua Lid4478e92017-09-25 13:07:22 -06001809 /* always use the first bio's css */
Dan Schatzbergc74d40e2021-06-28 19:38:21 -07001810 cmd->blkcg_css = NULL;
1811 cmd->memcg_css = NULL;
Shaohua Li0b508bc2017-09-26 11:02:12 -07001812#ifdef CONFIG_BLK_CGROUP
Dan Schatzbergc74d40e2021-06-28 19:38:21 -07001813 if (rq->bio && rq->bio->bi_blkg) {
1814 cmd->blkcg_css = &bio_blkcg(rq->bio)->css;
1815#ifdef CONFIG_MEMCG
1816 cmd->memcg_css =
1817 cgroup_get_e_css(cmd->blkcg_css->cgroup,
1818 &memory_cgrp_subsys);
Shaohua Lid4478e92017-09-25 13:07:22 -06001819#endif
Dan Schatzbergc74d40e2021-06-28 19:38:21 -07001820 }
1821#endif
Dan Schatzberg87579e92021-06-28 19:38:15 -07001822 loop_queue_work(lo, cmd);
Ming Leib5dd2f62014-12-31 13:22:57 +00001823
Christoph Hellwigfc17b652017-06-03 09:38:05 +02001824 return BLK_STS_OK;
Ming Leib5dd2f62014-12-31 13:22:57 +00001825}
1826
1827static void loop_handle_cmd(struct loop_cmd *cmd)
1828{
Jens Axboe1894e912018-04-13 16:24:29 -06001829 struct request *rq = blk_mq_rq_from_pdu(cmd);
1830 const bool write = op_is_write(req_op(rq));
1831 struct loop_device *lo = rq->q->queuedata;
Christoph Hellwigf4829a92015-09-27 21:01:50 +02001832 int ret = 0;
Dan Schatzbergc74d40e2021-06-28 19:38:21 -07001833 struct mem_cgroup *old_memcg = NULL;
Ming Leib5dd2f62014-12-31 13:22:57 +00001834
Christoph Hellwigf4829a92015-09-27 21:01:50 +02001835 if (write && (lo->lo_flags & LO_FLAGS_READ_ONLY)) {
1836 ret = -EIO;
Ming Leib5dd2f62014-12-31 13:22:57 +00001837 goto failed;
Christoph Hellwigf4829a92015-09-27 21:01:50 +02001838 }
Ming Leib5dd2f62014-12-31 13:22:57 +00001839
Dan Schatzbergc74d40e2021-06-28 19:38:21 -07001840 if (cmd->blkcg_css)
1841 kthread_associate_blkcg(cmd->blkcg_css);
1842 if (cmd->memcg_css)
1843 old_memcg = set_active_memcg(
1844 mem_cgroup_from_css(cmd->memcg_css));
1845
Jens Axboe1894e912018-04-13 16:24:29 -06001846 ret = do_req_filebacked(lo, rq);
Dan Schatzbergc74d40e2021-06-28 19:38:21 -07001847
1848 if (cmd->blkcg_css)
1849 kthread_associate_blkcg(NULL);
1850
1851 if (cmd->memcg_css) {
1852 set_active_memcg(old_memcg);
1853 css_put(cmd->memcg_css);
1854 }
Ming Leib5dd2f62014-12-31 13:22:57 +00001855 failed:
Ming Leibc07c102015-08-17 10:31:51 +08001856 /* complete non-aio request */
Christoph Hellwigfe2cb292017-04-20 16:03:02 +02001857 if (!cmd->use_aio || ret) {
Evan Green8cd55082020-04-03 16:43:03 +02001858 if (ret == -EOPNOTSUPP)
1859 cmd->ret = ret;
1860 else
1861 cmd->ret = ret ? -EIO : 0;
Christoph Hellwig15f73f52020-06-11 08:44:47 +02001862 if (likely(!blk_should_fake_timeout(rq->q)))
1863 blk_mq_complete_request(rq);
Christoph Hellwigfe2cb292017-04-20 16:03:02 +02001864 }
Ming Leib5dd2f62014-12-31 13:22:57 +00001865}
1866
Dan Schatzberg87579e92021-06-28 19:38:15 -07001867static void loop_set_timer(struct loop_device *lo)
Ming Leib5dd2f62014-12-31 13:22:57 +00001868{
Dan Schatzberg87579e92021-06-28 19:38:15 -07001869 timer_reduce(&lo->timer, jiffies + LOOP_IDLE_WORKER_TIMEOUT);
Ming Leib5dd2f62014-12-31 13:22:57 +00001870}
1871
Dan Schatzberg87579e92021-06-28 19:38:15 -07001872static void loop_process_work(struct loop_worker *worker,
1873 struct list_head *cmd_list, struct loop_device *lo)
Ming Leib5dd2f62014-12-31 13:22:57 +00001874{
Dan Schatzberg87579e92021-06-28 19:38:15 -07001875 int orig_flags = current->flags;
1876 struct loop_cmd *cmd;
Ming Leib5dd2f62014-12-31 13:22:57 +00001877
Dan Schatzberg87579e92021-06-28 19:38:15 -07001878 current->flags |= PF_LOCAL_THROTTLE | PF_MEMALLOC_NOIO;
1879 spin_lock_irq(&lo->lo_work_lock);
1880 while (!list_empty(cmd_list)) {
1881 cmd = container_of(
1882 cmd_list->next, struct loop_cmd, list_entry);
1883 list_del(cmd_list->next);
1884 spin_unlock_irq(&lo->lo_work_lock);
1885
1886 loop_handle_cmd(cmd);
1887 cond_resched();
1888
1889 spin_lock_irq(&lo->lo_work_lock);
1890 }
1891
1892 /*
1893 * We only add to the idle list if there are no pending cmds
1894 * *and* the worker will not run again which ensures that it
1895 * is safe to free any worker on the idle list
1896 */
1897 if (worker && !work_pending(&worker->work)) {
1898 worker->last_ran_at = jiffies;
1899 list_add_tail(&worker->idle_list, &lo->idle_worker_list);
1900 loop_set_timer(lo);
1901 }
1902 spin_unlock_irq(&lo->lo_work_lock);
1903 current->flags = orig_flags;
1904}
1905
1906static void loop_workfn(struct work_struct *work)
1907{
1908 struct loop_worker *worker =
1909 container_of(work, struct loop_worker, work);
1910 loop_process_work(worker, &worker->cmd_list, worker->lo);
1911}
1912
1913static void loop_rootcg_workfn(struct work_struct *work)
1914{
1915 struct loop_device *lo =
1916 container_of(work, struct loop_device, rootcg_work);
1917 loop_process_work(NULL, &lo->rootcg_cmd_list, lo);
1918}
1919
1920static void loop_free_idle_workers(struct timer_list *timer)
1921{
1922 struct loop_device *lo = container_of(timer, struct loop_device, timer);
1923 struct loop_worker *pos, *worker;
1924
1925 spin_lock_irq(&lo->lo_work_lock);
1926 list_for_each_entry_safe(worker, pos, &lo->idle_worker_list,
1927 idle_list) {
1928 if (time_is_after_jiffies(worker->last_ran_at +
1929 LOOP_IDLE_WORKER_TIMEOUT))
1930 break;
1931 list_del(&worker->idle_list);
1932 rb_erase(&worker->rb_node, &lo->worker_tree);
Dan Schatzbergc74d40e2021-06-28 19:38:21 -07001933 css_put(worker->blkcg_css);
Dan Schatzberg87579e92021-06-28 19:38:15 -07001934 kfree(worker);
1935 }
1936 if (!list_empty(&lo->idle_worker_list))
1937 loop_set_timer(lo);
1938 spin_unlock_irq(&lo->lo_work_lock);
Ming Leib5dd2f62014-12-31 13:22:57 +00001939}
1940
Eric Biggersf363b082017-03-30 13:39:16 -07001941static const struct blk_mq_ops loop_mq_ops = {
Ming Leib5dd2f62014-12-31 13:22:57 +00001942 .queue_rq = loop_queue_rq,
Christoph Hellwigfe2cb292017-04-20 16:03:02 +02001943 .complete = lo_complete_rq,
Ming Leib5dd2f62014-12-31 13:22:57 +00001944};
1945
Christoph Hellwigd6da83d2021-06-23 16:59:02 +02001946static int loop_add(int i)
Ken Chen73285082007-05-08 00:28:20 -07001947{
1948 struct loop_device *lo;
1949 struct gendisk *disk;
Kay Sievers34dd82a2011-07-31 22:08:04 +02001950 int err;
Ken Chen73285082007-05-08 00:28:20 -07001951
Silva Paulo68d740d2012-07-14 15:39:58 -07001952 err = -ENOMEM;
Ken Chen73285082007-05-08 00:28:20 -07001953 lo = kzalloc(sizeof(*lo), GFP_KERNEL);
Silva Paulo68d740d2012-07-14 15:39:58 -07001954 if (!lo)
Ken Chen73285082007-05-08 00:28:20 -07001955 goto out;
Mikulas Patockaef7e7c82013-10-15 14:14:38 -06001956 lo->lo_state = Lo_unbound;
1957
Christoph Hellwig18d1f202021-06-23 16:59:05 +02001958 err = mutex_lock_killable(&loop_ctl_mutex);
1959 if (err)
1960 goto out_free_dev;
1961
Tejun Heoc718aa62013-02-27 17:03:58 -08001962 /* allocate id, if @id >= 0, we're requesting that specific id */
Kay Sievers34dd82a2011-07-31 22:08:04 +02001963 if (i >= 0) {
Tejun Heoc718aa62013-02-27 17:03:58 -08001964 err = idr_alloc(&loop_index_idr, lo, i, i + 1, GFP_KERNEL);
1965 if (err == -ENOSPC)
Kay Sievers34dd82a2011-07-31 22:08:04 +02001966 err = -EEXIST;
Kay Sievers34dd82a2011-07-31 22:08:04 +02001967 } else {
Tejun Heoc718aa62013-02-27 17:03:58 -08001968 err = idr_alloc(&loop_index_idr, lo, 0, 0, GFP_KERNEL);
Kay Sievers34dd82a2011-07-31 22:08:04 +02001969 }
Tetsuo Handa1c500ad2021-09-02 09:07:35 +09001970 mutex_unlock(&loop_ctl_mutex);
Kay Sievers34dd82a2011-07-31 22:08:04 +02001971 if (err < 0)
Tetsuo Handa1c500ad2021-09-02 09:07:35 +09001972 goto out_free_dev;
Tejun Heoc718aa62013-02-27 17:03:58 -08001973 i = err;
Ken Chen73285082007-05-08 00:28:20 -07001974
Ming Leib5dd2f62014-12-31 13:22:57 +00001975 lo->tag_set.ops = &loop_mq_ops;
1976 lo->tag_set.nr_hw_queues = 1;
1977 lo->tag_set.queue_depth = 128;
1978 lo->tag_set.numa_node = NUMA_NO_NODE;
1979 lo->tag_set.cmd_size = sizeof(struct loop_cmd);
Bart Van Assche2112f5c2021-08-05 10:42:00 -07001980 lo->tag_set.flags = BLK_MQ_F_SHOULD_MERGE | BLK_MQ_F_STACKING |
1981 BLK_MQ_F_NO_SCHED_BY_DEFAULT;
Ming Leib5dd2f62014-12-31 13:22:57 +00001982 lo->tag_set.driver_data = lo;
1983
1984 err = blk_mq_alloc_tag_set(&lo->tag_set);
1985 if (err)
Mikulas Patocka3ec981e2013-10-14 12:12:24 -04001986 goto out_free_idr;
Ken Chen73285082007-05-08 00:28:20 -07001987
Christoph Hellwig1c995022021-06-02 09:53:33 +03001988 disk = lo->lo_disk = blk_mq_alloc_disk(&lo->tag_set, lo);
1989 if (IS_ERR(disk)) {
1990 err = PTR_ERR(disk);
Ming Leib5dd2f62014-12-31 13:22:57 +00001991 goto out_cleanup_tags;
1992 }
Christoph Hellwig1c995022021-06-02 09:53:33 +03001993 lo->lo_queue = lo->lo_disk->queue;
Mikulas Patockaef7e7c82013-10-15 14:14:38 -06001994
Shaohua Li54bb0ad2017-08-31 22:09:45 -07001995 blk_queue_max_hw_sectors(lo->lo_queue, BLK_DEF_MAX_SECTORS);
Shaohua Li40326d82017-08-31 22:09:46 -07001996
Ming Lei5b5e20f2015-08-17 10:31:47 +08001997 /*
Shaohua Li40326d82017-08-31 22:09:46 -07001998 * By default, we do buffer IO, so it doesn't make sense to enable
1999 * merge because the I/O submitted to backing file is handled page by
2000 * page. For directio mode, merge does help to dispatch bigger request
2001 * to underlayer disk. We will enable merge once directio is enabled.
Ming Lei5b5e20f2015-08-17 10:31:47 +08002002 */
Bart Van Assche8b904b52018-03-07 17:10:10 -08002003 blk_queue_flag_set(QUEUE_FLAG_NOMERGES, lo->lo_queue);
Ming Lei5b5e20f2015-08-17 10:31:47 +08002004
Kay Sieverse03c8dd2011-08-23 20:12:04 +02002005 /*
2006 * Disable partition scanning by default. The in-kernel partition
2007 * scanning can be requested individually per-device during its
2008 * setup. Userspace can always add and remove partitions from all
2009 * devices. The needed partition minors are allocated from the
2010 * extended minor space, the main loop device numbers will continue
2011 * to match the loop minors, regardless of the number of partitions
2012 * used.
2013 *
2014 * If max_part is given, partition scanning is globally enabled for
2015 * all loop devices. The minors for the main loop devices will be
2016 * multiples of max_part.
2017 *
2018 * Note: Global-for-all-devices, set-only-at-init, read-only module
2019 * parameteters like 'max_loop' and 'max_part' make things needlessly
2020 * complicated, are too static, inflexible and may surprise
2021 * userspace tools. Parameters like this in general should be avoided.
2022 */
2023 if (!part_shift)
Christoph Hellwig46e7eac2021-11-22 14:06:17 +01002024 disk->flags |= GENHD_FL_NO_PART;
Ming Leif8933662015-05-06 12:26:23 +08002025 atomic_set(&lo->lo_refcnt, 0);
Pavel Tatashin6cc8e742021-01-26 09:46:30 -05002026 mutex_init(&lo->lo_mutex);
Ken Chen73285082007-05-08 00:28:20 -07002027 lo->lo_number = i;
Ken Chen73285082007-05-08 00:28:20 -07002028 spin_lock_init(&lo->lo_lock);
Dan Schatzberg87579e92021-06-28 19:38:15 -07002029 spin_lock_init(&lo->lo_work_lock);
Ken Chen73285082007-05-08 00:28:20 -07002030 disk->major = LOOP_MAJOR;
Laurent Vivier476a4812008-03-26 12:11:53 +01002031 disk->first_minor = i << part_shift;
Christoph Hellwig1c995022021-06-02 09:53:33 +03002032 disk->minors = 1 << part_shift;
Ken Chen73285082007-05-08 00:28:20 -07002033 disk->fops = &lo_fops;
2034 disk->private_data = lo;
2035 disk->queue = lo->lo_queue;
Matteo Croce9f65c482021-07-13 01:05:30 +02002036 disk->events = DISK_EVENT_MEDIA_CHANGE;
2037 disk->event_flags = DISK_EVENT_FLAG_UEVENT;
Ken Chen73285082007-05-08 00:28:20 -07002038 sprintf(disk->disk_name, "loop%d", i);
Tetsuo Handa1c500ad2021-09-02 09:07:35 +09002039 /* Make this loop device reachable from pathname. */
Luis Chamberlain905705f2021-09-27 14:59:57 -07002040 err = add_disk(disk);
2041 if (err)
2042 goto out_cleanup_disk;
2043
Tetsuo Handa1c500ad2021-09-02 09:07:35 +09002044 /* Show this loop device. */
2045 mutex_lock(&loop_ctl_mutex);
2046 lo->idr_visible = true;
Christoph Hellwig18d1f202021-06-23 16:59:05 +02002047 mutex_unlock(&loop_ctl_mutex);
Luis Chamberlain905705f2021-09-27 14:59:57 -07002048
Christoph Hellwig18d1f202021-06-23 16:59:05 +02002049 return i;
Ken Chen73285082007-05-08 00:28:20 -07002050
Luis Chamberlain905705f2021-09-27 14:59:57 -07002051out_cleanup_disk:
2052 blk_cleanup_disk(disk);
Ming Leib5dd2f62014-12-31 13:22:57 +00002053out_cleanup_tags:
2054 blk_mq_free_tag_set(&lo->tag_set);
Mikulas Patocka3ec981e2013-10-14 12:12:24 -04002055out_free_idr:
Tetsuo Handa1c500ad2021-09-02 09:07:35 +09002056 mutex_lock(&loop_ctl_mutex);
Mikulas Patocka3ec981e2013-10-14 12:12:24 -04002057 idr_remove(&loop_index_idr, i);
Christoph Hellwig18d1f202021-06-23 16:59:05 +02002058 mutex_unlock(&loop_ctl_mutex);
Ken Chen73285082007-05-08 00:28:20 -07002059out_free_dev:
2060 kfree(lo);
2061out:
Kay Sievers34dd82a2011-07-31 22:08:04 +02002062 return err;
Ken Chen73285082007-05-08 00:28:20 -07002063}
2064
Kay Sievers34dd82a2011-07-31 22:08:04 +02002065static void loop_remove(struct loop_device *lo)
Ken Chen73285082007-05-08 00:28:20 -07002066{
Tetsuo Handa1c500ad2021-09-02 09:07:35 +09002067 /* Make this loop device unreachable from pathname. */
NeilBrown6cd18e72015-04-27 14:12:22 +10002068 del_gendisk(lo->lo_disk);
Christoph Hellwig1c995022021-06-02 09:53:33 +03002069 blk_cleanup_disk(lo->lo_disk);
Ming Leib5dd2f62014-12-31 13:22:57 +00002070 blk_mq_free_tag_set(&lo->tag_set);
Tetsuo Handa1c500ad2021-09-02 09:07:35 +09002071 mutex_lock(&loop_ctl_mutex);
2072 idr_remove(&loop_index_idr, lo->lo_number);
2073 mutex_unlock(&loop_ctl_mutex);
2074 /* There is no route which can find this loop device. */
Pavel Tatashin6cc8e742021-01-26 09:46:30 -05002075 mutex_destroy(&lo->lo_mutex);
Ken Chen73285082007-05-08 00:28:20 -07002076 kfree(lo);
2077}
2078
Christoph Hellwig8410d382020-10-29 15:58:33 +01002079static void loop_probe(dev_t dev)
Ken Chen73285082007-05-08 00:28:20 -07002080{
Christoph Hellwig8410d382020-10-29 15:58:33 +01002081 int idx = MINOR(dev) >> part_shift;
Christoph Hellwig8410d382020-10-29 15:58:33 +01002082
2083 if (max_loop && idx >= max_loop)
2084 return;
Christoph Hellwig4157fe02021-06-23 16:59:03 +02002085 loop_add(idx);
Christoph Hellwigf9d10762021-06-23 16:59:04 +02002086}
2087
2088static int loop_control_remove(int idx)
2089{
2090 struct loop_device *lo;
2091 int ret;
Christoph Hellwige5d66a102021-06-23 16:59:06 +02002092
2093 if (idx < 0) {
Tetsuo Handae3f93872021-11-29 19:00:43 +09002094 pr_warn_once("deleting an unspecified loop device is not supported.\n");
Christoph Hellwige5d66a102021-06-23 16:59:06 +02002095 return -EINVAL;
2096 }
Christoph Hellwigf9d10762021-06-23 16:59:04 +02002097
Tetsuo Handa1c500ad2021-09-02 09:07:35 +09002098 /* Hide this loop device for serialization. */
Christoph Hellwigf9d10762021-06-23 16:59:04 +02002099 ret = mutex_lock_killable(&loop_ctl_mutex);
2100 if (ret)
2101 return ret;
Christoph Hellwigb9848082021-06-23 16:59:07 +02002102 lo = idr_find(&loop_index_idr, idx);
Tetsuo Handa1c500ad2021-09-02 09:07:35 +09002103 if (!lo || !lo->idr_visible)
Christoph Hellwigb9848082021-06-23 16:59:07 +02002104 ret = -ENODEV;
Tetsuo Handa1c500ad2021-09-02 09:07:35 +09002105 else
2106 lo->idr_visible = false;
2107 mutex_unlock(&loop_ctl_mutex);
2108 if (ret)
2109 return ret;
Christoph Hellwigf9d10762021-06-23 16:59:04 +02002110
Tetsuo Handa1c500ad2021-09-02 09:07:35 +09002111 /* Check whether this loop device can be removed. */
Christoph Hellwigf9d10762021-06-23 16:59:04 +02002112 ret = mutex_lock_killable(&lo->lo_mutex);
2113 if (ret)
Tetsuo Handa1c500ad2021-09-02 09:07:35 +09002114 goto mark_visible;
Christoph Hellwigf9d10762021-06-23 16:59:04 +02002115 if (lo->lo_state != Lo_unbound ||
2116 atomic_read(&lo->lo_refcnt) > 0) {
2117 mutex_unlock(&lo->lo_mutex);
2118 ret = -EBUSY;
Tetsuo Handa1c500ad2021-09-02 09:07:35 +09002119 goto mark_visible;
Christoph Hellwigf9d10762021-06-23 16:59:04 +02002120 }
Tetsuo Handa1c500ad2021-09-02 09:07:35 +09002121 /* Mark this loop device no longer open()-able. */
Christoph Hellwigf9d10762021-06-23 16:59:04 +02002122 lo->lo_state = Lo_deleting;
2123 mutex_unlock(&lo->lo_mutex);
2124
Christoph Hellwigf9d10762021-06-23 16:59:04 +02002125 loop_remove(lo);
Tetsuo Handa1c500ad2021-09-02 09:07:35 +09002126 return 0;
2127
2128mark_visible:
2129 /* Show this loop device again. */
2130 mutex_lock(&loop_ctl_mutex);
2131 lo->idr_visible = true;
Christoph Hellwigf9d10762021-06-23 16:59:04 +02002132 mutex_unlock(&loop_ctl_mutex);
2133 return ret;
2134}
2135
2136static int loop_control_get_free(int idx)
Kay Sievers770fe302011-07-31 22:08:04 +02002137{
2138 struct loop_device *lo;
Christoph Hellwigb9848082021-06-23 16:59:07 +02002139 int id, ret;
Kay Sievers770fe302011-07-31 22:08:04 +02002140
Jan Kara0a42e992018-11-08 14:01:04 +01002141 ret = mutex_lock_killable(&loop_ctl_mutex);
2142 if (ret)
2143 return ret;
Christoph Hellwigb9848082021-06-23 16:59:07 +02002144 idr_for_each_entry(&loop_index_idr, lo, id) {
Tetsuo Handa1c500ad2021-09-02 09:07:35 +09002145 /* Hitting a race results in creating a new loop device which is harmless. */
2146 if (lo->idr_visible && data_race(lo->lo_state) == Lo_unbound)
Christoph Hellwigb9848082021-06-23 16:59:07 +02002147 goto found;
2148 }
Jan Kara0a42e992018-11-08 14:01:04 +01002149 mutex_unlock(&loop_ctl_mutex);
Christoph Hellwig18d1f202021-06-23 16:59:05 +02002150 return loop_add(-1);
Christoph Hellwigb9848082021-06-23 16:59:07 +02002151found:
2152 mutex_unlock(&loop_ctl_mutex);
2153 return id;
Kay Sievers770fe302011-07-31 22:08:04 +02002154}
2155
Christoph Hellwigf9d10762021-06-23 16:59:04 +02002156static long loop_control_ioctl(struct file *file, unsigned int cmd,
2157 unsigned long parm)
2158{
2159 switch (cmd) {
2160 case LOOP_CTL_ADD:
Christoph Hellwig18d1f202021-06-23 16:59:05 +02002161 return loop_add(parm);
Christoph Hellwigf9d10762021-06-23 16:59:04 +02002162 case LOOP_CTL_REMOVE:
2163 return loop_control_remove(parm);
2164 case LOOP_CTL_GET_FREE:
2165 return loop_control_get_free(parm);
2166 default:
2167 return -ENOSYS;
2168 }
2169}
2170
Kay Sievers770fe302011-07-31 22:08:04 +02002171static const struct file_operations loop_ctl_fops = {
2172 .open = nonseekable_open,
2173 .unlocked_ioctl = loop_control_ioctl,
2174 .compat_ioctl = loop_control_ioctl,
2175 .owner = THIS_MODULE,
2176 .llseek = noop_llseek,
2177};
2178
2179static struct miscdevice loop_misc = {
2180 .minor = LOOP_CTRL_MINOR,
2181 .name = "loop-control",
2182 .fops = &loop_ctl_fops,
2183};
2184
2185MODULE_ALIAS_MISCDEV(LOOP_CTRL_MINOR);
2186MODULE_ALIAS("devname:loop-control");
2187
Linus Torvalds1da177e2005-04-16 15:20:36 -07002188static int __init loop_init(void)
2189{
Ken Chena47653f2007-06-08 13:46:44 -07002190 int i, nr;
Kay Sievers770fe302011-07-31 22:08:04 +02002191 int err;
Ken Chena47653f2007-06-08 13:46:44 -07002192
Laurent Vivier476a4812008-03-26 12:11:53 +01002193 part_shift = 0;
Namhyung Kimac04fee2011-05-27 07:59:25 +02002194 if (max_part > 0) {
Laurent Vivier476a4812008-03-26 12:11:53 +01002195 part_shift = fls(max_part);
2196
Namhyung Kimac04fee2011-05-27 07:59:25 +02002197 /*
2198 * Adjust max_part according to part_shift as it is exported
2199 * to user space so that user can decide correct minor number
2200 * if [s]he want to create more devices.
2201 *
2202 * Note that -1 is required because partition 0 is reserved
2203 * for the whole disk.
2204 */
2205 max_part = (1UL << part_shift) - 1;
2206 }
2207
Guo Chaob1a66502013-02-21 15:16:49 -08002208 if ((1UL << part_shift) > DISK_MAX_PARTS) {
2209 err = -EINVAL;
Anton Volkova8c1d062017-08-07 15:37:50 +03002210 goto err_out;
Guo Chaob1a66502013-02-21 15:16:49 -08002211 }
Namhyung Kim78f4bb32011-05-24 16:48:54 +02002212
Guo Chaob1a66502013-02-21 15:16:49 -08002213 if (max_loop > 1UL << (MINORBITS - part_shift)) {
2214 err = -EINVAL;
Anton Volkova8c1d062017-08-07 15:37:50 +03002215 goto err_out;
Guo Chaob1a66502013-02-21 15:16:49 -08002216 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002217
Kay Sieversd134b002011-07-31 22:08:04 +02002218 /*
2219 * If max_loop is specified, create that many devices upfront.
2220 * This also becomes a hard limit. If max_loop is not specified,
2221 * create CONFIG_BLK_DEV_LOOP_MIN_COUNT loop devices at module
2222 * init time. Loop devices can be requested on-demand with the
2223 * /dev/loop-control interface, or be instantiated by accessing
2224 * a 'dead' device node.
2225 */
Lukas Bulwahnaeb2b0b2020-12-12 06:13:02 +01002226 if (max_loop)
Ken Chena47653f2007-06-08 13:46:44 -07002227 nr = max_loop;
Lukas Bulwahnaeb2b0b2020-12-12 06:13:02 +01002228 else
Kay Sieversd134b002011-07-31 22:08:04 +02002229 nr = CONFIG_BLK_DEV_LOOP_MIN_COUNT;
Ken Chena47653f2007-06-08 13:46:44 -07002230
Anton Volkova8c1d062017-08-07 15:37:50 +03002231 err = misc_register(&loop_misc);
2232 if (err < 0)
2233 goto err_out;
2234
2235
Christoph Hellwig8410d382020-10-29 15:58:33 +01002236 if (__register_blkdev(LOOP_MAJOR, "loop", loop_probe)) {
Guo Chaob1a66502013-02-21 15:16:49 -08002237 err = -EIO;
2238 goto misc_out;
2239 }
Ken Chena47653f2007-06-08 13:46:44 -07002240
Kay Sieversd134b002011-07-31 22:08:04 +02002241 /* pre-create number of devices given by config or max_loop */
Kay Sievers34dd82a2011-07-31 22:08:04 +02002242 for (i = 0; i < nr; i++)
Christoph Hellwigd6da83d2021-06-23 16:59:02 +02002243 loop_add(i);
Kay Sievers34dd82a2011-07-31 22:08:04 +02002244
Ken Chen73285082007-05-08 00:28:20 -07002245 printk(KERN_INFO "loop: module loaded\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002246 return 0;
Guo Chaob1a66502013-02-21 15:16:49 -08002247
2248misc_out:
2249 misc_deregister(&loop_misc);
Anton Volkova8c1d062017-08-07 15:37:50 +03002250err_out:
Guo Chaob1a66502013-02-21 15:16:49 -08002251 return err;
Kay Sievers34dd82a2011-07-31 22:08:04 +02002252}
Ken Chena47653f2007-06-08 13:46:44 -07002253
Ken Chen73285082007-05-08 00:28:20 -07002254static void __exit loop_exit(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002255{
Christoph Hellwig8e609472021-06-23 16:59:08 +02002256 struct loop_device *lo;
2257 int id;
2258
Christoph Hellwig8b52d8b2021-06-23 16:59:00 +02002259 unregister_blkdev(LOOP_MAJOR, "loop");
2260 misc_deregister(&loop_misc);
Luis Chamberlain200f9332020-06-19 20:47:27 +00002261
Tetsuo Handa1c500ad2021-09-02 09:07:35 +09002262 /*
2263 * There is no need to use loop_ctl_mutex here, for nobody else can
2264 * access loop_index_idr when this module is unloading (unless forced
2265 * module unloading is requested). If this is not a clean unloading,
2266 * we have no means to avoid kernel crash.
2267 */
Christoph Hellwig8e609472021-06-23 16:59:08 +02002268 idr_for_each_entry(&loop_index_idr, lo, id)
2269 loop_remove(lo);
Christoph Hellwigbd5c39e2021-06-23 16:59:01 +02002270
2271 idr_destroy(&loop_index_idr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002272}
2273
2274module_init(loop_init);
2275module_exit(loop_exit);
2276
2277#ifndef MODULE
2278static int __init max_loop_setup(char *str)
2279{
2280 max_loop = simple_strtol(str, NULL, 0);
2281 return 1;
2282}
2283
2284__setup("max_loop=", max_loop_setup);
2285#endif