blob: ab7ca5989097ac70372f77f4fb60964a98c98177 [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>
56#include <linux/file.h>
57#include <linux/stat.h>
58#include <linux/errno.h>
59#include <linux/major.h>
60#include <linux/wait.h>
61#include <linux/blkdev.h>
62#include <linux/blkpg.h>
63#include <linux/init.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070064#include <linux/swap.h>
65#include <linux/slab.h>
David Howells863d5b822006-08-29 19:06:14 +010066#include <linux/compat.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070067#include <linux/suspend.h>
Rafael J. Wysocki83144182007-07-17 04:03:35 -070068#include <linux/freezer.h>
Arnd Bergmann2a48fc02010-06-02 14:28:52 +020069#include <linux/mutex.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070070#include <linux/writeback.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070071#include <linux/completion.h>
72#include <linux/highmem.h>
Serge E. Hallyn6c997912006-09-29 01:59:11 -070073#include <linux/kthread.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>
Adam Manzanaresd9a08a92018-05-22 10:52:19 -070081
Al Viro83a87612013-05-12 10:14:07 -040082#include "loop.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070083
Linus Torvalds7c0f6ba2016-12-24 11:46:01 -080084#include <linux/uaccess.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070085
Kay Sievers34dd82a2011-07-31 22:08:04 +020086static DEFINE_IDR(loop_index_idr);
Tetsuo Handa310ca162018-11-08 14:01:02 +010087static DEFINE_MUTEX(loop_ctl_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -070088
Laurent Vivier476a4812008-03-26 12:11:53 +010089static int max_part;
90static int part_shift;
91
Linus Torvalds1da177e2005-04-16 15:20:36 -070092static int transfer_xor(struct loop_device *lo, int cmd,
93 struct page *raw_page, unsigned raw_off,
94 struct page *loop_page, unsigned loop_off,
95 int size, sector_t real_block)
96{
Cong Wangcfd80052011-11-25 23:14:18 +080097 char *raw_buf = kmap_atomic(raw_page) + raw_off;
98 char *loop_buf = kmap_atomic(loop_page) + loop_off;
Linus Torvalds1da177e2005-04-16 15:20:36 -070099 char *in, *out, *key;
100 int i, keysize;
101
102 if (cmd == READ) {
103 in = raw_buf;
104 out = loop_buf;
105 } else {
106 in = loop_buf;
107 out = raw_buf;
108 }
109
110 key = lo->lo_encrypt_key;
111 keysize = lo->lo_encrypt_key_size;
112 for (i = 0; i < size; i++)
113 *out++ = *in++ ^ key[(i & 511) % keysize];
114
Cong Wangcfd80052011-11-25 23:14:18 +0800115 kunmap_atomic(loop_buf);
116 kunmap_atomic(raw_buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700117 cond_resched();
118 return 0;
119}
120
121static int xor_init(struct loop_device *lo, const struct loop_info64 *info)
122{
123 if (unlikely(info->lo_encrypt_key_size <= 0))
124 return -EINVAL;
125 return 0;
126}
127
128static struct loop_func_table none_funcs = {
129 .number = LO_CRYPT_NONE,
Christoph Hellwigaa4d8612015-04-07 18:23:29 +0200130};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700131
132static struct loop_func_table xor_funcs = {
133 .number = LO_CRYPT_XOR,
134 .transfer = transfer_xor,
135 .init = xor_init
Christoph Hellwigaa4d8612015-04-07 18:23:29 +0200136};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700137
138/* xfer_funcs[0] is special - its release function is never called */
139static struct loop_func_table *xfer_funcs[MAX_LO_CRYPT] = {
140 &none_funcs,
141 &xor_funcs
142};
143
Dmitry Monakhov7035b5d2011-11-16 09:21:49 +0100144static loff_t get_size(loff_t offset, loff_t sizelimit, struct file *file)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700145{
Guo Chaob7a1da62013-02-21 15:16:50 -0800146 loff_t loopsize;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700147
148 /* Compute loopsize in bytes */
Guo Chaob7a1da62013-02-21 15:16:50 -0800149 loopsize = i_size_read(file->f_mapping->host);
150 if (offset > 0)
151 loopsize -= offset;
152 /* offset is beyond i_size, weird but possible */
Dmitry Monakhov7035b5d2011-11-16 09:21:49 +0100153 if (loopsize < 0)
154 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700155
Dmitry Monakhov7035b5d2011-11-16 09:21:49 +0100156 if (sizelimit > 0 && sizelimit < loopsize)
157 loopsize = sizelimit;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700158 /*
159 * Unfortunately, if we want to do I/O on the device,
160 * the number of 512-byte sectors has to fit into a sector_t.
161 */
162 return loopsize >> 9;
163}
164
Dmitry Monakhov7035b5d2011-11-16 09:21:49 +0100165static loff_t get_loop_size(struct loop_device *lo, struct file *file)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700166{
Dmitry Monakhov7035b5d2011-11-16 09:21:49 +0100167 return get_size(lo->lo_offset, lo->lo_sizelimit, file);
168}
169
Ming Lei2e5ab5f2015-08-17 10:31:49 +0800170static void __loop_update_dio(struct loop_device *lo, bool dio)
171{
172 struct file *file = lo->lo_backing_file;
173 struct address_space *mapping = file->f_mapping;
174 struct inode *inode = mapping->host;
175 unsigned short sb_bsize = 0;
176 unsigned dio_align = 0;
177 bool use_dio;
178
179 if (inode->i_sb->s_bdev) {
180 sb_bsize = bdev_logical_block_size(inode->i_sb->s_bdev);
181 dio_align = sb_bsize - 1;
182 }
183
184 /*
185 * We support direct I/O only if lo_offset is aligned with the
186 * logical I/O size of backing device, and the logical block
187 * size of loop is bigger than the backing device's and the loop
188 * needn't transform transfer.
189 *
190 * TODO: the above condition may be loosed in the future, and
191 * direct I/O may be switched runtime at that time because most
Masahiro Yamada89d790a2017-02-27 14:29:01 -0800192 * of requests in sane applications should be PAGE_SIZE aligned
Ming Lei2e5ab5f2015-08-17 10:31:49 +0800193 */
194 if (dio) {
195 if (queue_logical_block_size(lo->lo_queue) >= sb_bsize &&
196 !(lo->lo_offset & dio_align) &&
197 mapping->a_ops->direct_IO &&
198 !lo->transfer)
199 use_dio = true;
200 else
201 use_dio = false;
202 } else {
203 use_dio = false;
204 }
205
206 if (lo->use_dio == use_dio)
207 return;
208
209 /* flush dirty pages before changing direct IO */
210 vfs_fsync(file, 0);
211
212 /*
213 * The flag of LO_FLAGS_DIRECT_IO is handled similarly with
214 * LO_FLAGS_READ_ONLY, both are set from kernel, and losetup
215 * will get updated by ioctl(LOOP_GET_STATUS)
216 */
217 blk_mq_freeze_queue(lo->lo_queue);
218 lo->use_dio = use_dio;
Shaohua Li40326d82017-08-31 22:09:46 -0700219 if (use_dio) {
Bart Van Assche8b904b52018-03-07 17:10:10 -0800220 blk_queue_flag_clear(QUEUE_FLAG_NOMERGES, lo->lo_queue);
Ming Lei2e5ab5f2015-08-17 10:31:49 +0800221 lo->lo_flags |= LO_FLAGS_DIRECT_IO;
Shaohua Li40326d82017-08-31 22:09:46 -0700222 } else {
Bart Van Assche8b904b52018-03-07 17:10:10 -0800223 blk_queue_flag_set(QUEUE_FLAG_NOMERGES, lo->lo_queue);
Ming Lei2e5ab5f2015-08-17 10:31:49 +0800224 lo->lo_flags &= ~LO_FLAGS_DIRECT_IO;
Shaohua Li40326d82017-08-31 22:09:46 -0700225 }
Ming Lei2e5ab5f2015-08-17 10:31:49 +0800226 blk_mq_unfreeze_queue(lo->lo_queue);
227}
228
Dmitry Monakhov7035b5d2011-11-16 09:21:49 +0100229static int
Omar Sandoval1e6ec9e2017-08-23 14:54:59 -0700230figure_loop_size(struct loop_device *lo, loff_t offset, loff_t sizelimit)
Dmitry Monakhov7035b5d2011-11-16 09:21:49 +0100231{
232 loff_t size = get_size(offset, sizelimit, lo->lo_backing_file);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700233 sector_t x = (sector_t)size;
Guo Chao7b0576a2013-02-21 15:16:47 -0800234 struct block_device *bdev = lo->lo_device;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700235
236 if (unlikely((loff_t)x != size))
237 return -EFBIG;
Dmitry Monakhov7035b5d2011-11-16 09:21:49 +0100238 if (lo->lo_offset != offset)
239 lo->lo_offset = offset;
240 if (lo->lo_sizelimit != sizelimit)
241 lo->lo_sizelimit = sizelimit;
Ken Chen73285082007-05-08 00:28:20 -0700242 set_capacity(lo->lo_disk, x);
Guo Chao7b0576a2013-02-21 15:16:47 -0800243 bd_set_size(bdev, (loff_t)get_capacity(bdev->bd_disk) << 9);
244 /* let user-space know about the new size */
245 kobject_uevent(&disk_to_dev(bdev->bd_disk)->kobj, KOBJ_CHANGE);
Dmitry Monakhov7035b5d2011-11-16 09:21:49 +0100246 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700247}
248
249static inline int
250lo_do_transfer(struct loop_device *lo, int cmd,
251 struct page *rpage, unsigned roffs,
252 struct page *lpage, unsigned loffs,
253 int size, sector_t rblock)
254{
Christoph Hellwigaa4d8612015-04-07 18:23:29 +0200255 int ret;
256
257 ret = lo->transfer(lo, cmd, rpage, roffs, lpage, loffs, size, rblock);
258 if (likely(!ret))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700259 return 0;
260
Christoph Hellwigaa4d8612015-04-07 18:23:29 +0200261 printk_ratelimited(KERN_ERR
262 "loop: Transfer error at byte offset %llu, length %i.\n",
263 (unsigned long long)rblock << 9, size);
264 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700265}
266
Christoph Hellwigaa4d8612015-04-07 18:23:29 +0200267static int lo_write_bvec(struct file *file, struct bio_vec *bvec, loff_t *ppos)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700268{
Christoph Hellwigaa4d8612015-04-07 18:23:29 +0200269 struct iov_iter i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700270 ssize_t bw;
Al Viro283e7e52015-04-03 15:21:59 -0400271
Christoph Hellwigb6207432019-06-26 15:49:28 +0200272 iov_iter_bvec(&i, WRITE, bvec, 1, bvec->bv_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700273
Al Viro03d95eb2013-03-20 13:04:20 -0400274 file_start_write(file);
Christoph Hellwigabbb65892017-05-27 11:16:52 +0300275 bw = vfs_iter_write(file, &i, ppos, 0);
Al Viro03d95eb2013-03-20 13:04:20 -0400276 file_end_write(file);
Christoph Hellwigaa4d8612015-04-07 18:23:29 +0200277
278 if (likely(bw == bvec->bv_len))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700279 return 0;
Christoph Hellwigaa4d8612015-04-07 18:23:29 +0200280
281 printk_ratelimited(KERN_ERR
282 "loop: Write error at byte offset %llu, length %i.\n",
283 (unsigned long long)*ppos, bvec->bv_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700284 if (bw >= 0)
285 bw = -EIO;
286 return bw;
287}
288
Christoph Hellwigaa4d8612015-04-07 18:23:29 +0200289static int lo_write_simple(struct loop_device *lo, struct request *rq,
290 loff_t pos)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700291{
Christoph Hellwigaa4d8612015-04-07 18:23:29 +0200292 struct bio_vec bvec;
293 struct req_iterator iter;
294 int ret = 0;
295
296 rq_for_each_segment(bvec, rq, iter) {
297 ret = lo_write_bvec(lo->lo_backing_file, &bvec, &pos);
298 if (ret < 0)
299 break;
300 cond_resched();
301 }
302
303 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700304}
305
Christoph Hellwigaa4d8612015-04-07 18:23:29 +0200306/*
Christoph Hellwig456be142011-10-17 12:57:20 +0200307 * This is the slow, transforming version that needs to double buffer the
308 * data as it cannot do the transformations in place without having direct
309 * access to the destination pages of the backing file.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700310 */
Christoph Hellwigaa4d8612015-04-07 18:23:29 +0200311static int lo_write_transfer(struct loop_device *lo, struct request *rq,
312 loff_t pos)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700313{
Christoph Hellwigaa4d8612015-04-07 18:23:29 +0200314 struct bio_vec bvec, b;
Ming Lei30112012014-12-31 13:22:58 +0000315 struct req_iterator iter;
Christoph Hellwigaa4d8612015-04-07 18:23:29 +0200316 struct page *page;
Kent Overstreet79886132013-11-23 17:19:00 -0800317 int ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700318
Christoph Hellwigaa4d8612015-04-07 18:23:29 +0200319 page = alloc_page(GFP_NOIO);
320 if (unlikely(!page))
321 return -ENOMEM;
Christoph Hellwig456be142011-10-17 12:57:20 +0200322
Ming Lei30112012014-12-31 13:22:58 +0000323 rq_for_each_segment(bvec, rq, iter) {
Christoph Hellwigaa4d8612015-04-07 18:23:29 +0200324 ret = lo_do_transfer(lo, WRITE, page, 0, bvec.bv_page,
325 bvec.bv_offset, bvec.bv_len, pos >> 9);
326 if (unlikely(ret))
327 break;
328
329 b.bv_page = page;
330 b.bv_offset = 0;
331 b.bv_len = bvec.bv_len;
332 ret = lo_write_bvec(lo->lo_backing_file, &b, &pos);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700333 if (ret < 0)
334 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700335 }
Christoph Hellwigaa4d8612015-04-07 18:23:29 +0200336
337 __free_page(page);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700338 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700339}
340
Christoph Hellwigaa4d8612015-04-07 18:23:29 +0200341static int lo_read_simple(struct loop_device *lo, struct request *rq,
342 loff_t pos)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700343{
Kent Overstreet79886132013-11-23 17:19:00 -0800344 struct bio_vec bvec;
Ming Lei30112012014-12-31 13:22:58 +0000345 struct req_iterator iter;
Christoph Hellwigaa4d8612015-04-07 18:23:29 +0200346 struct iov_iter i;
347 ssize_t len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700348
Ming Lei30112012014-12-31 13:22:58 +0000349 rq_for_each_segment(bvec, rq, iter) {
Christoph Hellwigb6207432019-06-26 15:49:28 +0200350 iov_iter_bvec(&i, READ, &bvec, 1, bvec.bv_len);
Christoph Hellwig18e97102017-05-27 11:16:51 +0300351 len = vfs_iter_read(lo->lo_backing_file, &i, &pos, 0);
Christoph Hellwigaa4d8612015-04-07 18:23:29 +0200352 if (len < 0)
353 return len;
Dave Young306df072012-02-08 22:07:19 +0100354
Christoph Hellwigaa4d8612015-04-07 18:23:29 +0200355 flush_dcache_page(bvec.bv_page);
356
357 if (len != bvec.bv_len) {
Ming Lei30112012014-12-31 13:22:58 +0000358 struct bio *bio;
359
360 __rq_for_each_bio(bio, rq)
361 zero_fill_bio(bio);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700362 break;
Dave Young306df072012-02-08 22:07:19 +0100363 }
Christoph Hellwigaa4d8612015-04-07 18:23:29 +0200364 cond_resched();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700365 }
Christoph Hellwigaa4d8612015-04-07 18:23:29 +0200366
Dave Young306df072012-02-08 22:07:19 +0100367 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700368}
369
Christoph Hellwigaa4d8612015-04-07 18:23:29 +0200370static int lo_read_transfer(struct loop_device *lo, struct request *rq,
371 loff_t pos)
372{
373 struct bio_vec bvec, b;
374 struct req_iterator iter;
375 struct iov_iter i;
376 struct page *page;
377 ssize_t len;
378 int ret = 0;
379
380 page = alloc_page(GFP_NOIO);
381 if (unlikely(!page))
382 return -ENOMEM;
383
384 rq_for_each_segment(bvec, rq, iter) {
385 loff_t offset = pos;
386
387 b.bv_page = page;
388 b.bv_offset = 0;
389 b.bv_len = bvec.bv_len;
390
Christoph Hellwigb6207432019-06-26 15:49:28 +0200391 iov_iter_bvec(&i, READ, &b, 1, b.bv_len);
Christoph Hellwig18e97102017-05-27 11:16:51 +0300392 len = vfs_iter_read(lo->lo_backing_file, &i, &pos, 0);
Christoph Hellwigaa4d8612015-04-07 18:23:29 +0200393 if (len < 0) {
394 ret = len;
395 goto out_free_page;
396 }
397
398 ret = lo_do_transfer(lo, READ, page, 0, bvec.bv_page,
399 bvec.bv_offset, len, offset >> 9);
400 if (ret)
401 goto out_free_page;
402
403 flush_dcache_page(bvec.bv_page);
404
405 if (len != bvec.bv_len) {
406 struct bio *bio;
407
408 __rq_for_each_bio(bio, rq)
409 zero_fill_bio(bio);
410 break;
411 }
412 }
413
414 ret = 0;
415out_free_page:
416 __free_page(page);
417 return ret;
418}
419
Ming Leicf655d92014-12-31 13:22:59 +0000420static int lo_discard(struct loop_device *lo, struct request *rq, loff_t pos)
421{
422 /*
423 * We use punch hole to reclaim the free space used by the
424 * image a.k.a. discard. However we do not support discard if
425 * encryption is enabled, because it may give an attacker
426 * useful information.
427 */
428 struct file *file = lo->lo_backing_file;
429 int mode = FALLOC_FL_PUNCH_HOLE | FALLOC_FL_KEEP_SIZE;
430 int ret;
431
432 if ((!file->f_op->fallocate) || lo->lo_encrypt_key_size) {
433 ret = -EOPNOTSUPP;
434 goto out;
435 }
436
437 ret = file->f_op->fallocate(file, mode, pos, blk_rq_bytes(rq));
438 if (unlikely(ret && ret != -EINVAL && ret != -EOPNOTSUPP))
439 ret = -EIO;
440 out:
441 return ret;
442}
443
444static int lo_req_flush(struct loop_device *lo, struct request *rq)
445{
446 struct file *file = lo->lo_backing_file;
447 int ret = vfs_fsync(file, 0);
448 if (unlikely(ret && ret != -EINVAL))
449 ret = -EIO;
450
451 return ret;
452}
453
Christoph Hellwigfe2cb292017-04-20 16:03:02 +0200454static void lo_complete_rq(struct request *rq)
Ming Leibc07c102015-08-17 10:31:51 +0800455{
Christoph Hellwigfe2cb292017-04-20 16:03:02 +0200456 struct loop_cmd *cmd = blk_mq_rq_to_pdu(rq);
Jens Axboef9de14b2018-04-13 16:25:57 -0600457 blk_status_t ret = BLK_STS_OK;
Ming Leibc07c102015-08-17 10:31:51 +0800458
Jens Axboef9de14b2018-04-13 16:25:57 -0600459 if (!cmd->use_aio || cmd->ret < 0 || cmd->ret == blk_rq_bytes(rq) ||
460 req_op(rq) != REQ_OP_READ) {
461 if (cmd->ret < 0)
462 ret = BLK_STS_IOERR;
463 goto end_io;
Ming Leibc07c102015-08-17 10:31:51 +0800464 }
Christoph Hellwigfe2cb292017-04-20 16:03:02 +0200465
Jens Axboef9de14b2018-04-13 16:25:57 -0600466 /*
467 * Short READ - if we got some data, advance our request and
468 * retry it. If we got no data, end the rest with EIO.
469 */
470 if (cmd->ret) {
471 blk_update_request(rq, BLK_STS_OK, cmd->ret);
472 cmd->ret = 0;
473 blk_mq_requeue_request(rq, true);
474 } else {
475 if (cmd->use_aio) {
476 struct bio *bio = rq->bio;
477
478 while (bio) {
479 zero_fill_bio(bio);
480 bio = bio->bi_next;
481 }
482 }
483 ret = BLK_STS_IOERR;
484end_io:
485 blk_mq_end_request(rq, ret);
486 }
Ming Leibc07c102015-08-17 10:31:51 +0800487}
488
Shaohua Li92d77332017-09-01 11:15:17 -0700489static void lo_rw_aio_do_completion(struct loop_cmd *cmd)
490{
Jens Axboe1894e912018-04-13 16:24:29 -0600491 struct request *rq = blk_mq_rq_from_pdu(cmd);
492
Shaohua Li92d77332017-09-01 11:15:17 -0700493 if (!atomic_dec_and_test(&cmd->ref))
494 return;
495 kfree(cmd->bvec);
496 cmd->bvec = NULL;
Jens Axboe1894e912018-04-13 16:24:29 -0600497 blk_mq_complete_request(rq);
Shaohua Li92d77332017-09-01 11:15:17 -0700498}
499
Ming Leibc07c102015-08-17 10:31:51 +0800500static void lo_rw_aio_complete(struct kiocb *iocb, long ret, long ret2)
501{
502 struct loop_cmd *cmd = container_of(iocb, struct loop_cmd, iocb);
Ming Leibc07c102015-08-17 10:31:51 +0800503
Shaohua Lid4478e92017-09-25 13:07:22 -0600504 if (cmd->css)
505 css_put(cmd->css);
Christoph Hellwigfe2cb292017-04-20 16:03:02 +0200506 cmd->ret = ret;
Shaohua Li92d77332017-09-01 11:15:17 -0700507 lo_rw_aio_do_completion(cmd);
Ming Leibc07c102015-08-17 10:31:51 +0800508}
509
510static int lo_rw_aio(struct loop_device *lo, struct loop_cmd *cmd,
511 loff_t pos, bool rw)
512{
513 struct iov_iter iter;
Ming Lei86af5952019-02-15 19:13:17 +0800514 struct req_iterator rq_iter;
Ming Leibc07c102015-08-17 10:31:51 +0800515 struct bio_vec *bvec;
Jens Axboe1894e912018-04-13 16:24:29 -0600516 struct request *rq = blk_mq_rq_from_pdu(cmd);
Shaohua Li40326d82017-08-31 22:09:46 -0700517 struct bio *bio = rq->bio;
Ming Leibc07c102015-08-17 10:31:51 +0800518 struct file *file = lo->lo_backing_file;
Ming Lei86af5952019-02-15 19:13:17 +0800519 struct bio_vec tmp;
Shaohua Li40326d82017-08-31 22:09:46 -0700520 unsigned int offset;
Ming Lei86af5952019-02-15 19:13:17 +0800521 int nr_bvec = 0;
Ming Leibc07c102015-08-17 10:31:51 +0800522 int ret;
523
Ming Lei86af5952019-02-15 19:13:17 +0800524 rq_for_each_bvec(tmp, rq, rq_iter)
525 nr_bvec++;
Ming Leibc07c102015-08-17 10:31:51 +0800526
Ming Lei86af5952019-02-15 19:13:17 +0800527 if (rq->bio != rq->biotail) {
528
529 bvec = kmalloc_array(nr_bvec, sizeof(struct bio_vec),
Kees Cook6da2ec52018-06-12 13:55:00 -0700530 GFP_NOIO);
Shaohua Li40326d82017-08-31 22:09:46 -0700531 if (!bvec)
532 return -EIO;
533 cmd->bvec = bvec;
534
535 /*
536 * The bios of the request may be started from the middle of
537 * the 'bvec' because of bio splitting, so we can't directly
Ming Lei86af5952019-02-15 19:13:17 +0800538 * copy bio->bi_iov_vec to new bvec. The rq_for_each_bvec
Shaohua Li40326d82017-08-31 22:09:46 -0700539 * API will take care of all details for us.
540 */
Ming Lei86af5952019-02-15 19:13:17 +0800541 rq_for_each_bvec(tmp, rq, rq_iter) {
Shaohua Li40326d82017-08-31 22:09:46 -0700542 *bvec = tmp;
543 bvec++;
544 }
545 bvec = cmd->bvec;
546 offset = 0;
547 } else {
548 /*
549 * Same here, this bio may be started from the middle of the
550 * 'bvec' because of bio splitting, so offset from the bvec
551 * must be passed to iov iterator
552 */
553 offset = bio->bi_iter.bi_bvec_done;
554 bvec = __bvec_iter_bvec(bio->bi_io_vec, bio->bi_iter);
Shaohua Li40326d82017-08-31 22:09:46 -0700555 }
Shaohua Li92d77332017-09-01 11:15:17 -0700556 atomic_set(&cmd->ref, 2);
Shaohua Li40326d82017-08-31 22:09:46 -0700557
Christoph Hellwigb6207432019-06-26 15:49:28 +0200558 iov_iter_bvec(&iter, rw, bvec, nr_bvec, blk_rq_bytes(rq));
Shaohua Li40326d82017-08-31 22:09:46 -0700559 iter.iov_offset = offset;
Ming Leibc07c102015-08-17 10:31:51 +0800560
561 cmd->iocb.ki_pos = pos;
562 cmd->iocb.ki_filp = file;
563 cmd->iocb.ki_complete = lo_rw_aio_complete;
564 cmd->iocb.ki_flags = IOCB_DIRECT;
Adam Manzanaresd9a08a92018-05-22 10:52:19 -0700565 cmd->iocb.ki_ioprio = IOPRIO_PRIO_VALUE(IOPRIO_CLASS_NONE, 0);
Shaohua Lid4478e92017-09-25 13:07:22 -0600566 if (cmd->css)
567 kthread_associate_blkcg(cmd->css);
Ming Leibc07c102015-08-17 10:31:51 +0800568
569 if (rw == WRITE)
Miklos Szeredibb7462b2017-02-20 16:51:23 +0100570 ret = call_write_iter(file, &cmd->iocb, &iter);
Ming Leibc07c102015-08-17 10:31:51 +0800571 else
Miklos Szeredibb7462b2017-02-20 16:51:23 +0100572 ret = call_read_iter(file, &cmd->iocb, &iter);
Ming Leibc07c102015-08-17 10:31:51 +0800573
Shaohua Li92d77332017-09-01 11:15:17 -0700574 lo_rw_aio_do_completion(cmd);
Shaohua Lid4478e92017-09-25 13:07:22 -0600575 kthread_associate_blkcg(NULL);
Shaohua Li92d77332017-09-01 11:15:17 -0700576
Ming Leibc07c102015-08-17 10:31:51 +0800577 if (ret != -EIOCBQUEUED)
578 cmd->iocb.ki_complete(&cmd->iocb, ret, 0);
579 return 0;
580}
581
Christoph Hellwigc1c87c22016-08-04 16:10:01 +0200582static int do_req_filebacked(struct loop_device *lo, struct request *rq)
Ming Leibc07c102015-08-17 10:31:51 +0800583{
584 struct loop_cmd *cmd = blk_mq_rq_to_pdu(rq);
Christoph Hellwigc1c87c22016-08-04 16:10:01 +0200585 loff_t pos = ((loff_t) blk_rq_pos(rq) << 9) + lo->lo_offset;
Ming Leibc07c102015-08-17 10:31:51 +0800586
587 /*
588 * lo_write_simple and lo_read_simple should have been covered
589 * by io submit style function like lo_rw_aio(), one blocker
590 * is that lo_read_simple() need to call flush_dcache_page after
591 * the page is written from kernel, and it isn't easy to handle
592 * this in io submit style function which submits all segments
593 * of the req at one time. And direct read IO doesn't need to
594 * run flush_dcache_page().
595 */
Christoph Hellwigc1c87c22016-08-04 16:10:01 +0200596 switch (req_op(rq)) {
597 case REQ_OP_FLUSH:
598 return lo_req_flush(lo, rq);
599 case REQ_OP_DISCARD:
Christoph Hellwig19372e22017-04-05 19:21:15 +0200600 case REQ_OP_WRITE_ZEROES:
Christoph Hellwigc1c87c22016-08-04 16:10:01 +0200601 return lo_discard(lo, rq, pos);
602 case REQ_OP_WRITE:
Christoph Hellwigaa4d8612015-04-07 18:23:29 +0200603 if (lo->transfer)
Christoph Hellwigc1c87c22016-08-04 16:10:01 +0200604 return lo_write_transfer(lo, rq, pos);
605 else if (cmd->use_aio)
606 return lo_rw_aio(lo, cmd, pos, WRITE);
Christoph Hellwigaa4d8612015-04-07 18:23:29 +0200607 else
Christoph Hellwigc1c87c22016-08-04 16:10:01 +0200608 return lo_write_simple(lo, rq, pos);
609 case REQ_OP_READ:
610 if (lo->transfer)
611 return lo_read_transfer(lo, rq, pos);
612 else if (cmd->use_aio)
613 return lo_rw_aio(lo, cmd, pos, READ);
614 else
615 return lo_read_simple(lo, rq, pos);
616 default:
617 WARN_ON_ONCE(1);
618 return -EIO;
Christoph Hellwigaa4d8612015-04-07 18:23:29 +0200619 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700620}
621
Ming Lei2e5ab5f2015-08-17 10:31:49 +0800622static inline void loop_update_dio(struct loop_device *lo)
623{
624 __loop_update_dio(lo, io_is_direct(lo->lo_backing_file) |
625 lo->use_dio);
626}
627
Ming Lei06f0e9e2015-05-06 12:26:24 +0800628static void loop_reread_partitions(struct loop_device *lo,
629 struct block_device *bdev)
630{
631 int rc;
632
Jan Karad57f3372018-11-08 14:01:12 +0100633 rc = blkdev_reread_part(bdev);
Ming Lei06f0e9e2015-05-06 12:26:24 +0800634 if (rc)
635 pr_warn("%s: partition scan of loop%d (%s) failed (rc=%d)\n",
636 __func__, lo->lo_number, lo->lo_file_name, rc);
637}
638
Theodore Ts'od2ac8382018-05-07 11:37:58 -0400639static inline int is_loop_device(struct file *file)
640{
641 struct inode *i = file->f_mapping->host;
642
643 return i && S_ISBLK(i->i_mode) && MAJOR(i->i_rdev) == LOOP_MAJOR;
644}
645
646static int loop_validate_file(struct file *file, struct block_device *bdev)
647{
648 struct inode *inode = file->f_mapping->host;
649 struct file *f = file;
650
651 /* Avoid recursion */
652 while (is_loop_device(f)) {
653 struct loop_device *l;
654
655 if (f->f_mapping->host->i_bdev == bdev)
656 return -EBADF;
657
658 l = f->f_mapping->host->i_bdev->bd_disk->private_data;
Dongli Zhangf7c8a412019-03-18 20:23:17 +0800659 if (l->lo_state != Lo_bound) {
Theodore Ts'od2ac8382018-05-07 11:37:58 -0400660 return -EINVAL;
661 }
662 f = l->lo_backing_file;
663 }
664 if (!S_ISREG(inode->i_mode) && !S_ISBLK(inode->i_mode))
665 return -EINVAL;
666 return 0;
667}
668
Linus Torvalds1da177e2005-04-16 15:20:36 -0700669/*
670 * loop_change_fd switched the backing store of a loopback device to
671 * a new file. This is useful for operating system installers to free up
672 * the original file and in High Availability environments to switch to
673 * an alternative location for the content in case of server meltdown.
674 * This can only work if the loop device is used read-only, and if the
675 * new backing store is the same size and type as the old backing store.
676 */
Al Virobb214882008-03-02 09:29:48 -0500677static int loop_change_fd(struct loop_device *lo, struct block_device *bdev,
678 unsigned int arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700679{
Jan Kara1dded9a2018-11-08 14:01:15 +0100680 struct file *file = NULL, *old_file;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700681 int error;
Jan Kara85b0a542018-11-08 14:01:13 +0100682 bool partscan;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700683
Jan Karac28445f2018-11-08 14:01:16 +0100684 error = mutex_lock_killable(&loop_ctl_mutex);
Jan Karac3710772018-11-08 14:01:11 +0100685 if (error)
686 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700687 error = -ENXIO;
688 if (lo->lo_state != Lo_bound)
Jan Kara1dded9a2018-11-08 14:01:15 +0100689 goto out_err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700690
691 /* the loop device has to be read-only */
692 error = -EINVAL;
693 if (!(lo->lo_flags & LO_FLAGS_READ_ONLY))
Jan Kara1dded9a2018-11-08 14:01:15 +0100694 goto out_err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700695
696 error = -EBADF;
697 file = fget(arg);
698 if (!file)
Jan Kara1dded9a2018-11-08 14:01:15 +0100699 goto out_err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700700
Theodore Ts'od2ac8382018-05-07 11:37:58 -0400701 error = loop_validate_file(file, bdev);
702 if (error)
Jan Kara1dded9a2018-11-08 14:01:15 +0100703 goto out_err;
Theodore Ts'od2ac8382018-05-07 11:37:58 -0400704
Linus Torvalds1da177e2005-04-16 15:20:36 -0700705 old_file = lo->lo_backing_file;
706
707 error = -EINVAL;
708
Linus Torvalds1da177e2005-04-16 15:20:36 -0700709 /* size of the new backing store needs to be the same */
710 if (get_loop_size(lo, file) != get_loop_size(lo, old_file))
Jan Kara1dded9a2018-11-08 14:01:15 +0100711 goto out_err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700712
713 /* and ... switch */
Omar Sandoval43cade802017-08-24 00:03:44 -0700714 blk_mq_freeze_queue(lo->lo_queue);
715 mapping_set_gfp_mask(old_file->f_mapping, lo->old_gfp_mask);
716 lo->lo_backing_file = file;
717 lo->old_gfp_mask = mapping_gfp_mask(file->f_mapping);
718 mapping_set_gfp_mask(file->f_mapping,
719 lo->old_gfp_mask & ~(__GFP_IO|__GFP_FS));
720 loop_update_dio(lo);
721 blk_mq_unfreeze_queue(lo->lo_queue);
Jan Kara85b0a542018-11-08 14:01:13 +0100722 partscan = lo->lo_flags & LO_FLAGS_PARTSCAN;
Jan Karac3710772018-11-08 14:01:11 +0100723 mutex_unlock(&loop_ctl_mutex);
Jan Kara1dded9a2018-11-08 14:01:15 +0100724 /*
725 * We must drop file reference outside of loop_ctl_mutex as dropping
726 * the file ref can take bd_mutex which creates circular locking
727 * dependency.
728 */
729 fput(old_file);
Jan Kara85b0a542018-11-08 14:01:13 +0100730 if (partscan)
731 loop_reread_partitions(lo, bdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700732 return 0;
733
Jan Kara1dded9a2018-11-08 14:01:15 +0100734out_err:
Jan Karac3710772018-11-08 14:01:11 +0100735 mutex_unlock(&loop_ctl_mutex);
Jan Kara1dded9a2018-11-08 14:01:15 +0100736 if (file)
737 fput(file);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700738 return error;
739}
740
Milan Brozee862732010-08-23 15:16:00 +0200741/* loop sysfs attributes */
742
743static ssize_t loop_attr_show(struct device *dev, char *page,
744 ssize_t (*callback)(struct loop_device *, char *))
745{
Kay Sievers34dd82a2011-07-31 22:08:04 +0200746 struct gendisk *disk = dev_to_disk(dev);
747 struct loop_device *lo = disk->private_data;
Milan Brozee862732010-08-23 15:16:00 +0200748
Kay Sievers34dd82a2011-07-31 22:08:04 +0200749 return callback(lo, page);
Milan Brozee862732010-08-23 15:16:00 +0200750}
751
752#define LOOP_ATTR_RO(_name) \
753static ssize_t loop_attr_##_name##_show(struct loop_device *, char *); \
754static ssize_t loop_attr_do_show_##_name(struct device *d, \
755 struct device_attribute *attr, char *b) \
756{ \
757 return loop_attr_show(d, b, loop_attr_##_name##_show); \
758} \
759static struct device_attribute loop_attr_##_name = \
Joe Perches5657a812018-05-24 13:38:59 -0600760 __ATTR(_name, 0444, loop_attr_do_show_##_name, NULL);
Milan Brozee862732010-08-23 15:16:00 +0200761
762static ssize_t loop_attr_backing_file_show(struct loop_device *lo, char *buf)
763{
764 ssize_t ret;
765 char *p = NULL;
766
Kay Sievers05eb0f22011-07-31 22:21:35 +0200767 spin_lock_irq(&lo->lo_lock);
Milan Brozee862732010-08-23 15:16:00 +0200768 if (lo->lo_backing_file)
Miklos Szeredi9bf39ab2015-06-19 10:29:13 +0200769 p = file_path(lo->lo_backing_file, buf, PAGE_SIZE - 1);
Kay Sievers05eb0f22011-07-31 22:21:35 +0200770 spin_unlock_irq(&lo->lo_lock);
Milan Brozee862732010-08-23 15:16:00 +0200771
772 if (IS_ERR_OR_NULL(p))
773 ret = PTR_ERR(p);
774 else {
775 ret = strlen(p);
776 memmove(buf, p, ret);
777 buf[ret++] = '\n';
778 buf[ret] = 0;
779 }
780
781 return ret;
782}
783
784static ssize_t loop_attr_offset_show(struct loop_device *lo, char *buf)
785{
786 return sprintf(buf, "%llu\n", (unsigned long long)lo->lo_offset);
787}
788
789static ssize_t loop_attr_sizelimit_show(struct loop_device *lo, char *buf)
790{
791 return sprintf(buf, "%llu\n", (unsigned long long)lo->lo_sizelimit);
792}
793
794static ssize_t loop_attr_autoclear_show(struct loop_device *lo, char *buf)
795{
796 int autoclear = (lo->lo_flags & LO_FLAGS_AUTOCLEAR);
797
798 return sprintf(buf, "%s\n", autoclear ? "1" : "0");
799}
800
Kay Sieverse03c8dd2011-08-23 20:12:04 +0200801static ssize_t loop_attr_partscan_show(struct loop_device *lo, char *buf)
802{
803 int partscan = (lo->lo_flags & LO_FLAGS_PARTSCAN);
804
805 return sprintf(buf, "%s\n", partscan ? "1" : "0");
806}
807
Ming Lei2e5ab5f2015-08-17 10:31:49 +0800808static ssize_t loop_attr_dio_show(struct loop_device *lo, char *buf)
809{
810 int dio = (lo->lo_flags & LO_FLAGS_DIRECT_IO);
811
812 return sprintf(buf, "%s\n", dio ? "1" : "0");
813}
814
Milan Brozee862732010-08-23 15:16:00 +0200815LOOP_ATTR_RO(backing_file);
816LOOP_ATTR_RO(offset);
817LOOP_ATTR_RO(sizelimit);
818LOOP_ATTR_RO(autoclear);
Kay Sieverse03c8dd2011-08-23 20:12:04 +0200819LOOP_ATTR_RO(partscan);
Ming Lei2e5ab5f2015-08-17 10:31:49 +0800820LOOP_ATTR_RO(dio);
Milan Brozee862732010-08-23 15:16:00 +0200821
822static struct attribute *loop_attrs[] = {
823 &loop_attr_backing_file.attr,
824 &loop_attr_offset.attr,
825 &loop_attr_sizelimit.attr,
826 &loop_attr_autoclear.attr,
Kay Sieverse03c8dd2011-08-23 20:12:04 +0200827 &loop_attr_partscan.attr,
Ming Lei2e5ab5f2015-08-17 10:31:49 +0800828 &loop_attr_dio.attr,
Milan Brozee862732010-08-23 15:16:00 +0200829 NULL,
830};
831
832static struct attribute_group loop_attribute_group = {
833 .name = "loop",
834 .attrs= loop_attrs,
835};
836
Tetsuo Handad3349b62018-05-04 10:58:09 -0600837static void loop_sysfs_init(struct loop_device *lo)
Milan Brozee862732010-08-23 15:16:00 +0200838{
Tetsuo Handad3349b62018-05-04 10:58:09 -0600839 lo->sysfs_inited = !sysfs_create_group(&disk_to_dev(lo->lo_disk)->kobj,
840 &loop_attribute_group);
Milan Brozee862732010-08-23 15:16:00 +0200841}
842
843static void loop_sysfs_exit(struct loop_device *lo)
844{
Tetsuo Handad3349b62018-05-04 10:58:09 -0600845 if (lo->sysfs_inited)
846 sysfs_remove_group(&disk_to_dev(lo->lo_disk)->kobj,
847 &loop_attribute_group);
Milan Brozee862732010-08-23 15:16:00 +0200848}
849
Lukas Czernerdfaa2ef2011-08-19 14:50:46 +0200850static void loop_config_discard(struct loop_device *lo)
851{
852 struct file *file = lo->lo_backing_file;
853 struct inode *inode = file->f_mapping->host;
854 struct request_queue *q = lo->lo_queue;
855
856 /*
857 * We use punch hole to reclaim the free space used by the
Olaf Hering12a64d22014-01-21 14:39:24 -0800858 * image a.k.a. discard. However we do not support discard if
Lukas Czernerdfaa2ef2011-08-19 14:50:46 +0200859 * encryption is enabled, because it may give an attacker
860 * useful information.
861 */
862 if ((!file->f_op->fallocate) ||
863 lo->lo_encrypt_key_size) {
864 q->limits.discard_granularity = 0;
865 q->limits.discard_alignment = 0;
Jens Axboe2bb4cd52015-07-14 08:15:12 -0600866 blk_queue_max_discard_sectors(q, 0);
Christoph Hellwig19372e22017-04-05 19:21:15 +0200867 blk_queue_max_write_zeroes_sectors(q, 0);
Bart Van Assche8b904b52018-03-07 17:10:10 -0800868 blk_queue_flag_clear(QUEUE_FLAG_DISCARD, q);
Lukas Czernerdfaa2ef2011-08-19 14:50:46 +0200869 return;
870 }
871
872 q->limits.discard_granularity = inode->i_sb->s_blocksize;
Lukas Czernerdfaf3c02011-12-02 14:47:03 +0100873 q->limits.discard_alignment = 0;
Hannes Reineckef2c6df72017-06-08 13:46:45 +0200874
Omar Sandoval1e6ec9e2017-08-23 14:54:59 -0700875 blk_queue_max_discard_sectors(q, UINT_MAX >> 9);
876 blk_queue_max_write_zeroes_sectors(q, UINT_MAX >> 9);
Bart Van Assche8b904b52018-03-07 17:10:10 -0800877 blk_queue_flag_set(QUEUE_FLAG_DISCARD, q);
Lukas Czernerdfaa2ef2011-08-19 14:50:46 +0200878}
879
Ming Leie03a3d72015-08-17 10:31:48 +0800880static void loop_unprepare_queue(struct loop_device *lo)
881{
Petr Mladek39891442016-10-11 13:55:20 -0700882 kthread_flush_worker(&lo->worker);
Ming Leie03a3d72015-08-17 10:31:48 +0800883 kthread_stop(lo->worker_task);
884}
885
NeilBrownb2ee7d42017-06-16 15:02:09 +1000886static int loop_kthread_worker_fn(void *worker_ptr)
887{
Mikulas Patockad0a255e2019-08-08 11:17:01 -0400888 current->flags |= PF_LESS_THROTTLE | PF_MEMALLOC_NOIO;
NeilBrownb2ee7d42017-06-16 15:02:09 +1000889 return kthread_worker_fn(worker_ptr);
890}
891
Ming Leie03a3d72015-08-17 10:31:48 +0800892static int loop_prepare_queue(struct loop_device *lo)
893{
Petr Mladek39891442016-10-11 13:55:20 -0700894 kthread_init_worker(&lo->worker);
NeilBrownb2ee7d42017-06-16 15:02:09 +1000895 lo->worker_task = kthread_run(loop_kthread_worker_fn,
Ming Leie03a3d72015-08-17 10:31:48 +0800896 &lo->worker, "loop%d", lo->lo_number);
897 if (IS_ERR(lo->worker_task))
898 return -ENOMEM;
899 set_user_nice(lo->worker_task, MIN_NICE);
900 return 0;
901}
902
Holger Hoffstätte56a85fd2019-02-12 15:54:24 -0700903static void loop_update_rotational(struct loop_device *lo)
904{
905 struct file *file = lo->lo_backing_file;
906 struct inode *file_inode = file->f_mapping->host;
907 struct block_device *file_bdev = file_inode->i_sb->s_bdev;
908 struct request_queue *q = lo->lo_queue;
909 bool nonrot = true;
910
911 /* not all filesystems (e.g. tmpfs) have a sb->s_bdev */
912 if (file_bdev)
913 nonrot = blk_queue_nonrot(bdev_get_queue(file_bdev));
914
915 if (nonrot)
916 blk_queue_flag_set(QUEUE_FLAG_NONROT, q);
917 else
918 blk_queue_flag_clear(QUEUE_FLAG_NONROT, q);
919}
920
Al Virobb214882008-03-02 09:29:48 -0500921static int loop_set_fd(struct loop_device *lo, fmode_t mode,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700922 struct block_device *bdev, unsigned int arg)
923{
Theodore Ts'od2ac8382018-05-07 11:37:58 -0400924 struct file *file;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700925 struct inode *inode;
926 struct address_space *mapping;
Jan Kara89e524c02019-07-30 13:10:14 +0200927 struct block_device *claimed_bdev = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700928 int lo_flags = 0;
929 int error;
930 loff_t size;
Jan Kara85b0a542018-11-08 14:01:13 +0100931 bool partscan;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700932
933 /* This is safe, since we have a reference from open(). */
934 __module_get(THIS_MODULE);
935
936 error = -EBADF;
937 file = fget(arg);
938 if (!file)
939 goto out;
940
Jan Kara33ec3e52019-05-16 16:01:27 +0200941 /*
942 * If we don't hold exclusive handle for the device, upgrade to it
943 * here to avoid changing device under exclusive owner.
944 */
945 if (!(mode & FMODE_EXCL)) {
Jan Kara89e524c02019-07-30 13:10:14 +0200946 claimed_bdev = bd_start_claiming(bdev, loop_set_fd);
947 if (IS_ERR(claimed_bdev)) {
948 error = PTR_ERR(claimed_bdev);
Jan Kara33ec3e52019-05-16 16:01:27 +0200949 goto out_putf;
Jan Kara89e524c02019-07-30 13:10:14 +0200950 }
Jan Kara33ec3e52019-05-16 16:01:27 +0200951 }
952
Jan Karac28445f2018-11-08 14:01:16 +0100953 error = mutex_lock_killable(&loop_ctl_mutex);
Jan Kara757ecf42018-11-08 14:01:10 +0100954 if (error)
Jan Kara33ec3e52019-05-16 16:01:27 +0200955 goto out_bdev;
Jan Kara757ecf42018-11-08 14:01:10 +0100956
Linus Torvalds1da177e2005-04-16 15:20:36 -0700957 error = -EBUSY;
958 if (lo->lo_state != Lo_unbound)
Jan Kara757ecf42018-11-08 14:01:10 +0100959 goto out_unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700960
Theodore Ts'od2ac8382018-05-07 11:37:58 -0400961 error = loop_validate_file(file, bdev);
962 if (error)
Jan Kara757ecf42018-11-08 14:01:10 +0100963 goto out_unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700964
965 mapping = file->f_mapping;
966 inode = mapping->host;
967
Christoph Hellwig456be142011-10-17 12:57:20 +0200968 if (!(file->f_mode & FMODE_WRITE) || !(mode & FMODE_WRITE) ||
Al Viro283e7e52015-04-03 15:21:59 -0400969 !file->f_op->write_iter)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700970 lo_flags |= LO_FLAGS_READ_ONLY;
971
Christoph Hellwig456be142011-10-17 12:57:20 +0200972 error = -EFBIG;
973 size = get_loop_size(lo, file);
974 if ((loff_t)(sector_t)size != size)
Jan Kara757ecf42018-11-08 14:01:10 +0100975 goto out_unlock;
Ming Leie03a3d72015-08-17 10:31:48 +0800976 error = loop_prepare_queue(lo);
977 if (error)
Jan Kara757ecf42018-11-08 14:01:10 +0100978 goto out_unlock;
Christoph Hellwig456be142011-10-17 12:57:20 +0200979
980 error = 0;
981
Linus Torvalds1da177e2005-04-16 15:20:36 -0700982 set_device_ro(bdev, (lo_flags & LO_FLAGS_READ_ONLY) != 0);
983
Ming Lei2e5ab5f2015-08-17 10:31:49 +0800984 lo->use_dio = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700985 lo->lo_device = bdev;
986 lo->lo_flags = lo_flags;
987 lo->lo_backing_file = file;
Christoph Hellwigaa4d8612015-04-07 18:23:29 +0200988 lo->transfer = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700989 lo->ioctl = NULL;
990 lo->lo_sizelimit = 0;
991 lo->old_gfp_mask = mapping_gfp_mask(mapping);
992 mapping_set_gfp_mask(mapping, lo->old_gfp_mask & ~(__GFP_IO|__GFP_FS));
993
Nikanth Karthikesan68db1962009-03-24 12:29:54 +0100994 if (!(lo_flags & LO_FLAGS_READ_ONLY) && file->f_op->fsync)
Jens Axboe21d07272016-03-30 10:09:35 -0600995 blk_queue_write_cache(lo->lo_queue, true, false);
Nikanth Karthikesan68db1962009-03-24 12:29:54 +0100996
Holger Hoffstätte56a85fd2019-02-12 15:54:24 -0700997 loop_update_rotational(lo);
Ming Lei2e5ab5f2015-08-17 10:31:49 +0800998 loop_update_dio(lo);
Ken Chen73285082007-05-08 00:28:20 -0700999 set_capacity(lo->lo_disk, size);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001000 bd_set_size(bdev, size << 9);
Milan Brozee862732010-08-23 15:16:00 +02001001 loop_sysfs_init(lo);
David Zeuthenc3473c62010-05-03 14:08:59 +02001002 /* let user-space know about the new size */
1003 kobject_uevent(&disk_to_dev(bdev->bd_disk)->kobj, KOBJ_CHANGE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001004
Omar Sandoval8a0740c2017-08-24 00:03:41 -07001005 set_blocksize(bdev, S_ISBLK(inode->i_mode) ?
1006 block_size(inode->i_bdev) : PAGE_SIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001007
Serge E. Hallyn6c997912006-09-29 01:59:11 -07001008 lo->lo_state = Lo_bound;
Kay Sieverse03c8dd2011-08-23 20:12:04 +02001009 if (part_shift)
1010 lo->lo_flags |= LO_FLAGS_PARTSCAN;
Jan Kara85b0a542018-11-08 14:01:13 +01001011 partscan = lo->lo_flags & LO_FLAGS_PARTSCAN;
Anatol Pomozovc1681bf2013-04-01 09:47:56 -07001012
1013 /* Grab the block_device to prevent its destruction after we
Jan Karaa2505b72018-11-08 14:01:06 +01001014 * put /dev/loopXX inode. Later in __loop_clr_fd() we bdput(bdev).
Anatol Pomozovc1681bf2013-04-01 09:47:56 -07001015 */
1016 bdgrab(bdev);
Jan Kara757ecf42018-11-08 14:01:10 +01001017 mutex_unlock(&loop_ctl_mutex);
Jan Kara85b0a542018-11-08 14:01:13 +01001018 if (partscan)
1019 loop_reread_partitions(lo, bdev);
Jan Kara89e524c02019-07-30 13:10:14 +02001020 if (claimed_bdev)
1021 bd_abort_claiming(bdev, claimed_bdev, loop_set_fd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001022 return 0;
1023
Jan Kara757ecf42018-11-08 14:01:10 +01001024out_unlock:
1025 mutex_unlock(&loop_ctl_mutex);
Jan Kara33ec3e52019-05-16 16:01:27 +02001026out_bdev:
Jan Kara89e524c02019-07-30 13:10:14 +02001027 if (claimed_bdev)
1028 bd_abort_claiming(bdev, claimed_bdev, loop_set_fd);
Jan Kara757ecf42018-11-08 14:01:10 +01001029out_putf:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001030 fput(file);
Jan Kara757ecf42018-11-08 14:01:10 +01001031out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001032 /* This is safe: open() is still holding a reference. */
1033 module_put(THIS_MODULE);
1034 return error;
1035}
1036
1037static int
1038loop_release_xfer(struct loop_device *lo)
1039{
1040 int err = 0;
1041 struct loop_func_table *xfer = lo->lo_encryption;
1042
1043 if (xfer) {
1044 if (xfer->release)
1045 err = xfer->release(lo);
1046 lo->transfer = NULL;
1047 lo->lo_encryption = NULL;
1048 module_put(xfer->owner);
1049 }
1050 return err;
1051}
1052
1053static int
1054loop_init_xfer(struct loop_device *lo, struct loop_func_table *xfer,
1055 const struct loop_info64 *i)
1056{
1057 int err = 0;
1058
1059 if (xfer) {
1060 struct module *owner = xfer->owner;
1061
1062 if (!try_module_get(owner))
1063 return -EINVAL;
1064 if (xfer->init)
1065 err = xfer->init(lo, i);
1066 if (err)
1067 module_put(owner);
1068 else
1069 lo->lo_encryption = xfer;
1070 }
1071 return err;
1072}
1073
Jan Kara0da03ca2018-11-08 14:01:14 +01001074static int __loop_clr_fd(struct loop_device *lo, bool release)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001075{
Jan Kara7ccd0792018-11-08 14:01:07 +01001076 struct file *filp = NULL;
Al Virob4e3ca12005-10-21 03:22:34 -04001077 gfp_t gfp = lo->old_gfp_mask;
Ayan George4c823cc2011-09-21 10:02:13 +02001078 struct block_device *bdev = lo->lo_device;
Jan Kara7ccd0792018-11-08 14:01:07 +01001079 int err = 0;
Jan Kara0da03ca2018-11-08 14:01:14 +01001080 bool partscan = false;
1081 int lo_number;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001082
Jan Kara7ccd0792018-11-08 14:01:07 +01001083 mutex_lock(&loop_ctl_mutex);
1084 if (WARN_ON_ONCE(lo->lo_state != Lo_rundown)) {
1085 err = -ENXIO;
1086 goto out_unlock;
1087 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001088
Jan Kara7ccd0792018-11-08 14:01:07 +01001089 filp = lo->lo_backing_file;
1090 if (filp == NULL) {
1091 err = -EINVAL;
1092 goto out_unlock;
1093 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001094
Ming Leif8933662015-05-06 12:26:23 +08001095 /* freeze request queue during the transition */
1096 blk_mq_freeze_queue(lo->lo_queue);
1097
Linus Torvalds1da177e2005-04-16 15:20:36 -07001098 spin_lock_irq(&lo->lo_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001099 lo->lo_backing_file = NULL;
Kay Sievers05eb0f22011-07-31 22:21:35 +02001100 spin_unlock_irq(&lo->lo_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001101
1102 loop_release_xfer(lo);
1103 lo->transfer = NULL;
1104 lo->ioctl = NULL;
1105 lo->lo_device = NULL;
1106 lo->lo_encryption = NULL;
1107 lo->lo_offset = 0;
1108 lo->lo_sizelimit = 0;
1109 lo->lo_encrypt_key_size = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001110 memset(lo->lo_encrypt_key, 0, LO_KEY_SIZE);
1111 memset(lo->lo_crypt_name, 0, LO_NAME_SIZE);
1112 memset(lo->lo_file_name, 0, LO_NAME_SIZE);
Omar Sandoval89e4fde2017-08-24 00:03:43 -07001113 blk_queue_logical_block_size(lo->lo_queue, 512);
Omar Sandovalbf093752017-09-05 14:24:47 -07001114 blk_queue_physical_block_size(lo->lo_queue, 512);
1115 blk_queue_io_min(lo->lo_queue, 512);
Anatol Pomozovc1681bf2013-04-01 09:47:56 -07001116 if (bdev) {
1117 bdput(bdev);
Al Virobb214882008-03-02 09:29:48 -05001118 invalidate_bdev(bdev);
Jeff Laytoneedffa22018-05-21 14:35:03 -04001119 bdev->bd_inode->i_mapping->wb_err = 0;
Anatol Pomozovc1681bf2013-04-01 09:47:56 -07001120 }
Ken Chen73285082007-05-08 00:28:20 -07001121 set_capacity(lo->lo_disk, 0);
Milan Broz51a0bb02010-10-27 19:51:30 -06001122 loop_sysfs_exit(lo);
David Zeuthenc3473c62010-05-03 14:08:59 +02001123 if (bdev) {
Al Virobb214882008-03-02 09:29:48 -05001124 bd_set_size(bdev, 0);
David Zeuthenc3473c62010-05-03 14:08:59 +02001125 /* let user-space know about this change */
1126 kobject_uevent(&disk_to_dev(bdev->bd_disk)->kobj, KOBJ_CHANGE);
1127 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001128 mapping_set_gfp_mask(filp->f_mapping, gfp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001129 /* This is safe: open() is still holding a reference. */
1130 module_put(THIS_MODULE);
Ming Leif8933662015-05-06 12:26:23 +08001131 blk_mq_unfreeze_queue(lo->lo_queue);
1132
Jan Kara0da03ca2018-11-08 14:01:14 +01001133 partscan = lo->lo_flags & LO_FLAGS_PARTSCAN && bdev;
1134 lo_number = lo->lo_number;
Jan Kara0da03ca2018-11-08 14:01:14 +01001135 loop_unprepare_queue(lo);
1136out_unlock:
1137 mutex_unlock(&loop_ctl_mutex);
1138 if (partscan) {
Jan Karad57f3372018-11-08 14:01:12 +01001139 /*
1140 * bd_mutex has been held already in release path, so don't
1141 * acquire it if this function is called in such case.
1142 *
1143 * If the reread partition isn't from release path, lo_refcnt
1144 * must be at least one and it can only become zero when the
1145 * current holder is released.
1146 */
Jan Kara0da03ca2018-11-08 14:01:14 +01001147 if (release)
Jan Karad57f3372018-11-08 14:01:12 +01001148 err = __blkdev_reread_part(bdev);
1149 else
1150 err = blkdev_reread_part(bdev);
Dongli Zhang40853d62019-02-22 22:10:19 +08001151 if (err)
1152 pr_warn("%s: partition scan of loop%d failed (rc=%d)\n",
1153 __func__, lo_number, err);
Jan Karad57f3372018-11-08 14:01:12 +01001154 /* Device is gone, no point in returning error */
1155 err = 0;
1156 }
Dongli Zhang758a58d2019-02-22 22:10:20 +08001157
1158 /*
1159 * lo->lo_state is set to Lo_unbound here after above partscan has
1160 * finished.
1161 *
1162 * There cannot be anybody else entering __loop_clr_fd() as
1163 * lo->lo_backing_file is already cleared and Lo_rundown state
1164 * protects us from all the other places trying to change the 'lo'
1165 * device.
1166 */
1167 mutex_lock(&loop_ctl_mutex);
1168 lo->lo_flags = 0;
1169 if (!part_shift)
1170 lo->lo_disk->flags |= GENHD_FL_NO_PART_SCAN;
1171 lo->lo_state = Lo_unbound;
1172 mutex_unlock(&loop_ctl_mutex);
1173
Nikanth Karthikesanf028f3b2009-03-24 12:33:41 +01001174 /*
Tetsuo Handa310ca162018-11-08 14:01:02 +01001175 * Need not hold loop_ctl_mutex to fput backing file.
1176 * Calling fput holding loop_ctl_mutex triggers a circular
Nikanth Karthikesanf028f3b2009-03-24 12:33:41 +01001177 * lock dependency possibility warning as fput can take
Tetsuo Handa310ca162018-11-08 14:01:02 +01001178 * bd_mutex which is usually taken before loop_ctl_mutex.
Nikanth Karthikesanf028f3b2009-03-24 12:33:41 +01001179 */
Jan Kara7ccd0792018-11-08 14:01:07 +01001180 if (filp)
1181 fput(filp);
1182 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001183}
1184
Jan Karaa2505b72018-11-08 14:01:06 +01001185static int loop_clr_fd(struct loop_device *lo)
1186{
Jan Kara7ccd0792018-11-08 14:01:07 +01001187 int err;
1188
Jan Karac28445f2018-11-08 14:01:16 +01001189 err = mutex_lock_killable(&loop_ctl_mutex);
Jan Kara7ccd0792018-11-08 14:01:07 +01001190 if (err)
1191 return err;
1192 if (lo->lo_state != Lo_bound) {
1193 mutex_unlock(&loop_ctl_mutex);
Jan Karaa2505b72018-11-08 14:01:06 +01001194 return -ENXIO;
Jan Kara7ccd0792018-11-08 14:01:07 +01001195 }
Jan Karaa2505b72018-11-08 14:01:06 +01001196 /*
1197 * If we've explicitly asked to tear down the loop device,
1198 * and it has an elevated reference count, set it for auto-teardown when
1199 * the last reference goes away. This stops $!~#$@ udev from
1200 * preventing teardown because it decided that it needs to run blkid on
1201 * the loopback device whenever they appear. xfstests is notorious for
1202 * failing tests because blkid via udev races with a losetup
1203 * <dev>/do something like mkfs/losetup -d <dev> causing the losetup -d
1204 * command to fail with EBUSY.
1205 */
1206 if (atomic_read(&lo->lo_refcnt) > 1) {
1207 lo->lo_flags |= LO_FLAGS_AUTOCLEAR;
1208 mutex_unlock(&loop_ctl_mutex);
1209 return 0;
1210 }
1211 lo->lo_state = Lo_rundown;
Jan Kara7ccd0792018-11-08 14:01:07 +01001212 mutex_unlock(&loop_ctl_mutex);
Jan Karaa2505b72018-11-08 14:01:06 +01001213
Jan Kara0da03ca2018-11-08 14:01:14 +01001214 return __loop_clr_fd(lo, false);
Jan Karaa2505b72018-11-08 14:01:06 +01001215}
1216
Linus Torvalds1da177e2005-04-16 15:20:36 -07001217static int
1218loop_set_status(struct loop_device *lo, const struct loop_info64 *info)
1219{
1220 int err;
1221 struct loop_func_table *xfer;
Eric W. Biedermane4849732012-02-11 11:23:51 -08001222 kuid_t uid = current_uid();
Jan Kara85b0a542018-11-08 14:01:13 +01001223 struct block_device *bdev;
1224 bool partscan = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001225
Jan Karac28445f2018-11-08 14:01:16 +01001226 err = mutex_lock_killable(&loop_ctl_mutex);
Jan Kara550df5fd2018-11-08 14:01:09 +01001227 if (err)
1228 return err;
David Howellsb0fafa82008-11-14 10:38:41 +11001229 if (lo->lo_encrypt_key_size &&
Eric W. Biedermane4849732012-02-11 11:23:51 -08001230 !uid_eq(lo->lo_key_owner, uid) &&
Jan Kara550df5fd2018-11-08 14:01:09 +01001231 !capable(CAP_SYS_ADMIN)) {
1232 err = -EPERM;
1233 goto out_unlock;
1234 }
1235 if (lo->lo_state != Lo_bound) {
1236 err = -ENXIO;
1237 goto out_unlock;
1238 }
1239 if ((unsigned int) info->lo_encrypt_key_size > LO_KEY_SIZE) {
1240 err = -EINVAL;
1241 goto out_unlock;
1242 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001243
Jaegeuk Kim5db470e2019-01-09 19:17:14 -08001244 if (lo->lo_offset != info->lo_offset ||
1245 lo->lo_sizelimit != info->lo_sizelimit) {
1246 sync_blockdev(lo->lo_device);
1247 kill_bdev(lo->lo_device);
1248 }
1249
Ming Leiecdd0952017-02-11 11:40:45 +08001250 /* I/O need to be drained during transfer transition */
1251 blk_mq_freeze_queue(lo->lo_queue);
1252
Linus Torvalds1da177e2005-04-16 15:20:36 -07001253 err = loop_release_xfer(lo);
1254 if (err)
Jan Kara550df5fd2018-11-08 14:01:09 +01001255 goto out_unfreeze;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001256
1257 if (info->lo_encrypt_type) {
1258 unsigned int type = info->lo_encrypt_type;
1259
Tetsuo Handa1e047ea2018-04-06 10:03:17 +09001260 if (type >= MAX_LO_CRYPT) {
1261 err = -EINVAL;
Jan Kara550df5fd2018-11-08 14:01:09 +01001262 goto out_unfreeze;
Tetsuo Handa1e047ea2018-04-06 10:03:17 +09001263 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001264 xfer = xfer_funcs[type];
Tetsuo Handa1e047ea2018-04-06 10:03:17 +09001265 if (xfer == NULL) {
1266 err = -EINVAL;
Jan Kara550df5fd2018-11-08 14:01:09 +01001267 goto out_unfreeze;
Tetsuo Handa1e047ea2018-04-06 10:03:17 +09001268 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001269 } else
1270 xfer = NULL;
1271
1272 err = loop_init_xfer(lo, xfer, info);
1273 if (err)
Jan Kara550df5fd2018-11-08 14:01:09 +01001274 goto out_unfreeze;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001275
1276 if (lo->lo_offset != info->lo_offset ||
Omar Sandoval1e6ec9e2017-08-23 14:54:59 -07001277 lo->lo_sizelimit != info->lo_sizelimit) {
Jaegeuk Kim5db470e2019-01-09 19:17:14 -08001278 /* kill_bdev should have truncated all the pages */
1279 if (lo->lo_device->bd_inode->i_mapping->nrpages) {
1280 err = -EAGAIN;
1281 pr_warn("%s: loop%d (%s) has still dirty pages (nrpages=%lu)\n",
1282 __func__, lo->lo_number, lo->lo_file_name,
1283 lo->lo_device->bd_inode->i_mapping->nrpages);
1284 goto out_unfreeze;
1285 }
Omar Sandoval1e6ec9e2017-08-23 14:54:59 -07001286 if (figure_loop_size(lo, info->lo_offset, info->lo_sizelimit)) {
Ming Leiecdd0952017-02-11 11:40:45 +08001287 err = -EFBIG;
Jan Kara550df5fd2018-11-08 14:01:09 +01001288 goto out_unfreeze;
Ming Leiecdd0952017-02-11 11:40:45 +08001289 }
Arnd Bergmannb040ad92017-06-09 12:19:18 +02001290 }
Guo Chao541c7422013-02-21 15:16:46 -08001291
Lukas Czernerdfaa2ef2011-08-19 14:50:46 +02001292 loop_config_discard(lo);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001293
1294 memcpy(lo->lo_file_name, info->lo_file_name, LO_NAME_SIZE);
1295 memcpy(lo->lo_crypt_name, info->lo_crypt_name, LO_NAME_SIZE);
1296 lo->lo_file_name[LO_NAME_SIZE-1] = 0;
1297 lo->lo_crypt_name[LO_NAME_SIZE-1] = 0;
1298
1299 if (!xfer)
1300 xfer = &none_funcs;
1301 lo->transfer = xfer->transfer;
1302 lo->ioctl = xfer->ioctl;
1303
David Woodhouse96c58652008-02-06 01:36:27 -08001304 if ((lo->lo_flags & LO_FLAGS_AUTOCLEAR) !=
1305 (info->lo_flags & LO_FLAGS_AUTOCLEAR))
1306 lo->lo_flags ^= LO_FLAGS_AUTOCLEAR;
1307
Linus Torvalds1da177e2005-04-16 15:20:36 -07001308 lo->lo_encrypt_key_size = info->lo_encrypt_key_size;
1309 lo->lo_init[0] = info->lo_init[0];
1310 lo->lo_init[1] = info->lo_init[1];
1311 if (info->lo_encrypt_key_size) {
1312 memcpy(lo->lo_encrypt_key, info->lo_encrypt_key,
1313 info->lo_encrypt_key_size);
David Howellsb0fafa82008-11-14 10:38:41 +11001314 lo->lo_key_owner = uid;
Christoph Hellwigaa4d8612015-04-07 18:23:29 +02001315 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001316
Ming Lei2e5ab5f2015-08-17 10:31:49 +08001317 /* update dio if lo_offset or transfer is changed */
1318 __loop_update_dio(lo, lo->use_dio);
1319
Jan Kara550df5fd2018-11-08 14:01:09 +01001320out_unfreeze:
Ming Leiecdd0952017-02-11 11:40:45 +08001321 blk_mq_unfreeze_queue(lo->lo_queue);
Omar Sandovale02898b42017-03-01 10:42:38 -08001322
1323 if (!err && (info->lo_flags & LO_FLAGS_PARTSCAN) &&
1324 !(lo->lo_flags & LO_FLAGS_PARTSCAN)) {
1325 lo->lo_flags |= LO_FLAGS_PARTSCAN;
1326 lo->lo_disk->flags &= ~GENHD_FL_NO_PART_SCAN;
Jan Kara85b0a542018-11-08 14:01:13 +01001327 bdev = lo->lo_device;
1328 partscan = true;
Omar Sandovale02898b42017-03-01 10:42:38 -08001329 }
Jan Kara550df5fd2018-11-08 14:01:09 +01001330out_unlock:
1331 mutex_unlock(&loop_ctl_mutex);
Jan Kara85b0a542018-11-08 14:01:13 +01001332 if (partscan)
1333 loop_reread_partitions(lo, bdev);
Omar Sandovale02898b42017-03-01 10:42:38 -08001334
Ming Leiecdd0952017-02-11 11:40:45 +08001335 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001336}
1337
1338static int
1339loop_get_status(struct loop_device *lo, struct loop_info64 *info)
1340{
Tetsuo Handab1ab5fa2018-11-08 14:01:01 +01001341 struct path path;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001342 struct kstat stat;
Omar Sandoval2d1d4c12018-03-26 21:39:11 -07001343 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001344
Jan Karac28445f2018-11-08 14:01:16 +01001345 ret = mutex_lock_killable(&loop_ctl_mutex);
Jan Kara4a5ce9b2018-11-08 14:01:08 +01001346 if (ret)
1347 return ret;
Omar Sandoval2d1d4c12018-03-26 21:39:11 -07001348 if (lo->lo_state != Lo_bound) {
Tetsuo Handa310ca162018-11-08 14:01:02 +01001349 mutex_unlock(&loop_ctl_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001350 return -ENXIO;
Omar Sandoval2d1d4c12018-03-26 21:39:11 -07001351 }
1352
Linus Torvalds1da177e2005-04-16 15:20:36 -07001353 memset(info, 0, sizeof(*info));
1354 info->lo_number = lo->lo_number;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001355 info->lo_offset = lo->lo_offset;
1356 info->lo_sizelimit = lo->lo_sizelimit;
1357 info->lo_flags = lo->lo_flags;
1358 memcpy(info->lo_file_name, lo->lo_file_name, LO_NAME_SIZE);
1359 memcpy(info->lo_crypt_name, lo->lo_crypt_name, LO_NAME_SIZE);
1360 info->lo_encrypt_type =
1361 lo->lo_encryption ? lo->lo_encryption->number : 0;
1362 if (lo->lo_encrypt_key_size && capable(CAP_SYS_ADMIN)) {
1363 info->lo_encrypt_key_size = lo->lo_encrypt_key_size;
1364 memcpy(info->lo_encrypt_key, lo->lo_encrypt_key,
1365 lo->lo_encrypt_key_size);
1366 }
Omar Sandoval2d1d4c12018-03-26 21:39:11 -07001367
Tetsuo Handa310ca162018-11-08 14:01:02 +01001368 /* Drop loop_ctl_mutex while we call into the filesystem. */
Tetsuo Handab1ab5fa2018-11-08 14:01:01 +01001369 path = lo->lo_backing_file->f_path;
1370 path_get(&path);
Tetsuo Handa310ca162018-11-08 14:01:02 +01001371 mutex_unlock(&loop_ctl_mutex);
Tetsuo Handab1ab5fa2018-11-08 14:01:01 +01001372 ret = vfs_getattr(&path, &stat, STATX_INO, AT_STATX_SYNC_AS_STAT);
Omar Sandoval2d1d4c12018-03-26 21:39:11 -07001373 if (!ret) {
1374 info->lo_device = huge_encode_dev(stat.dev);
1375 info->lo_inode = stat.ino;
1376 info->lo_rdevice = huge_encode_dev(stat.rdev);
1377 }
Tetsuo Handab1ab5fa2018-11-08 14:01:01 +01001378 path_put(&path);
Omar Sandoval2d1d4c12018-03-26 21:39:11 -07001379 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001380}
1381
1382static void
1383loop_info64_from_old(const struct loop_info *info, struct loop_info64 *info64)
1384{
1385 memset(info64, 0, sizeof(*info64));
1386 info64->lo_number = info->lo_number;
1387 info64->lo_device = info->lo_device;
1388 info64->lo_inode = info->lo_inode;
1389 info64->lo_rdevice = info->lo_rdevice;
1390 info64->lo_offset = info->lo_offset;
1391 info64->lo_sizelimit = 0;
1392 info64->lo_encrypt_type = info->lo_encrypt_type;
1393 info64->lo_encrypt_key_size = info->lo_encrypt_key_size;
1394 info64->lo_flags = info->lo_flags;
1395 info64->lo_init[0] = info->lo_init[0];
1396 info64->lo_init[1] = info->lo_init[1];
1397 if (info->lo_encrypt_type == LO_CRYPT_CRYPTOAPI)
1398 memcpy(info64->lo_crypt_name, info->lo_name, LO_NAME_SIZE);
1399 else
1400 memcpy(info64->lo_file_name, info->lo_name, LO_NAME_SIZE);
1401 memcpy(info64->lo_encrypt_key, info->lo_encrypt_key, LO_KEY_SIZE);
1402}
1403
1404static int
1405loop_info64_to_old(const struct loop_info64 *info64, struct loop_info *info)
1406{
1407 memset(info, 0, sizeof(*info));
1408 info->lo_number = info64->lo_number;
1409 info->lo_device = info64->lo_device;
1410 info->lo_inode = info64->lo_inode;
1411 info->lo_rdevice = info64->lo_rdevice;
1412 info->lo_offset = info64->lo_offset;
1413 info->lo_encrypt_type = info64->lo_encrypt_type;
1414 info->lo_encrypt_key_size = info64->lo_encrypt_key_size;
1415 info->lo_flags = info64->lo_flags;
1416 info->lo_init[0] = info64->lo_init[0];
1417 info->lo_init[1] = info64->lo_init[1];
1418 if (info->lo_encrypt_type == LO_CRYPT_CRYPTOAPI)
1419 memcpy(info->lo_name, info64->lo_crypt_name, LO_NAME_SIZE);
1420 else
1421 memcpy(info->lo_name, info64->lo_file_name, LO_NAME_SIZE);
1422 memcpy(info->lo_encrypt_key, info64->lo_encrypt_key, LO_KEY_SIZE);
1423
1424 /* error in case values were truncated */
1425 if (info->lo_device != info64->lo_device ||
1426 info->lo_rdevice != info64->lo_rdevice ||
1427 info->lo_inode != info64->lo_inode ||
1428 info->lo_offset != info64->lo_offset)
1429 return -EOVERFLOW;
1430
1431 return 0;
1432}
1433
1434static int
1435loop_set_status_old(struct loop_device *lo, const struct loop_info __user *arg)
1436{
1437 struct loop_info info;
1438 struct loop_info64 info64;
1439
1440 if (copy_from_user(&info, arg, sizeof (struct loop_info)))
1441 return -EFAULT;
1442 loop_info64_from_old(&info, &info64);
1443 return loop_set_status(lo, &info64);
1444}
1445
1446static int
1447loop_set_status64(struct loop_device *lo, const struct loop_info64 __user *arg)
1448{
1449 struct loop_info64 info64;
1450
1451 if (copy_from_user(&info64, arg, sizeof (struct loop_info64)))
1452 return -EFAULT;
1453 return loop_set_status(lo, &info64);
1454}
1455
1456static int
1457loop_get_status_old(struct loop_device *lo, struct loop_info __user *arg) {
1458 struct loop_info info;
1459 struct loop_info64 info64;
Omar Sandovalbdac616db2018-04-06 09:57:03 -07001460 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001461
Jan Kara4a5ce9b2018-11-08 14:01:08 +01001462 if (!arg)
Omar Sandovalbdac616db2018-04-06 09:57:03 -07001463 return -EINVAL;
Omar Sandovalbdac616db2018-04-06 09:57:03 -07001464 err = loop_get_status(lo, &info64);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001465 if (!err)
1466 err = loop_info64_to_old(&info64, &info);
1467 if (!err && copy_to_user(arg, &info, sizeof(info)))
1468 err = -EFAULT;
1469
1470 return err;
1471}
1472
1473static int
1474loop_get_status64(struct loop_device *lo, struct loop_info64 __user *arg) {
1475 struct loop_info64 info64;
Omar Sandovalbdac616db2018-04-06 09:57:03 -07001476 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001477
Jan Kara4a5ce9b2018-11-08 14:01:08 +01001478 if (!arg)
Omar Sandovalbdac616db2018-04-06 09:57:03 -07001479 return -EINVAL;
Omar Sandovalbdac616db2018-04-06 09:57:03 -07001480 err = loop_get_status(lo, &info64);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001481 if (!err && copy_to_user(arg, &info64, sizeof(info64)))
1482 err = -EFAULT;
1483
1484 return err;
1485}
1486
Hannes Reinecke51001b72017-06-08 13:46:44 +02001487static int loop_set_capacity(struct loop_device *lo)
J. R. Okajima53d66602009-03-31 15:23:43 -07001488{
J. R. Okajima53d66602009-03-31 15:23:43 -07001489 if (unlikely(lo->lo_state != Lo_bound))
Guo Chao7b0576a2013-02-21 15:16:47 -08001490 return -ENXIO;
J. R. Okajima53d66602009-03-31 15:23:43 -07001491
Omar Sandoval1e6ec9e2017-08-23 14:54:59 -07001492 return figure_loop_size(lo, lo->lo_offset, lo->lo_sizelimit);
J. R. Okajima53d66602009-03-31 15:23:43 -07001493}
1494
Ming Leiab1cb272015-08-17 10:31:50 +08001495static int loop_set_dio(struct loop_device *lo, unsigned long arg)
1496{
1497 int error = -ENXIO;
1498 if (lo->lo_state != Lo_bound)
1499 goto out;
1500
1501 __loop_update_dio(lo, !!arg);
1502 if (lo->use_dio == !!arg)
1503 return 0;
1504 error = -EINVAL;
1505 out:
1506 return error;
1507}
1508
Omar Sandoval89e4fde2017-08-24 00:03:43 -07001509static int loop_set_block_size(struct loop_device *lo, unsigned long arg)
1510{
Jaegeuk Kim5db470e2019-01-09 19:17:14 -08001511 int err = 0;
1512
Omar Sandoval89e4fde2017-08-24 00:03:43 -07001513 if (lo->lo_state != Lo_bound)
1514 return -ENXIO;
1515
1516 if (arg < 512 || arg > PAGE_SIZE || !is_power_of_2(arg))
1517 return -EINVAL;
1518
Jaegeuk Kim5db470e2019-01-09 19:17:14 -08001519 if (lo->lo_queue->limits.logical_block_size != arg) {
1520 sync_blockdev(lo->lo_device);
1521 kill_bdev(lo->lo_device);
1522 }
1523
Omar Sandoval89e4fde2017-08-24 00:03:43 -07001524 blk_mq_freeze_queue(lo->lo_queue);
1525
Jaegeuk Kim5db470e2019-01-09 19:17:14 -08001526 /* kill_bdev should have truncated all the pages */
1527 if (lo->lo_queue->limits.logical_block_size != arg &&
1528 lo->lo_device->bd_inode->i_mapping->nrpages) {
1529 err = -EAGAIN;
1530 pr_warn("%s: loop%d (%s) has still dirty pages (nrpages=%lu)\n",
1531 __func__, lo->lo_number, lo->lo_file_name,
1532 lo->lo_device->bd_inode->i_mapping->nrpages);
1533 goto out_unfreeze;
1534 }
1535
Omar Sandoval89e4fde2017-08-24 00:03:43 -07001536 blk_queue_logical_block_size(lo->lo_queue, arg);
Omar Sandovalbf093752017-09-05 14:24:47 -07001537 blk_queue_physical_block_size(lo->lo_queue, arg);
1538 blk_queue_io_min(lo->lo_queue, arg);
Omar Sandoval89e4fde2017-08-24 00:03:43 -07001539 loop_update_dio(lo);
Jaegeuk Kim5db470e2019-01-09 19:17:14 -08001540out_unfreeze:
Omar Sandoval89e4fde2017-08-24 00:03:43 -07001541 blk_mq_unfreeze_queue(lo->lo_queue);
1542
Jaegeuk Kim5db470e2019-01-09 19:17:14 -08001543 return err;
Omar Sandoval89e4fde2017-08-24 00:03:43 -07001544}
1545
Jan Karaa1316542018-11-08 14:01:05 +01001546static int lo_simple_ioctl(struct loop_device *lo, unsigned int cmd,
1547 unsigned long arg)
1548{
1549 int err;
1550
Jan Karac28445f2018-11-08 14:01:16 +01001551 err = mutex_lock_killable(&loop_ctl_mutex);
Jan Karaa1316542018-11-08 14:01:05 +01001552 if (err)
1553 return err;
1554 switch (cmd) {
1555 case LOOP_SET_CAPACITY:
1556 err = loop_set_capacity(lo);
1557 break;
1558 case LOOP_SET_DIRECT_IO:
1559 err = loop_set_dio(lo, arg);
1560 break;
1561 case LOOP_SET_BLOCK_SIZE:
1562 err = loop_set_block_size(lo, arg);
1563 break;
1564 default:
1565 err = lo->ioctl ? lo->ioctl(lo, cmd, arg) : -EINVAL;
1566 }
1567 mutex_unlock(&loop_ctl_mutex);
1568 return err;
1569}
1570
Al Virobb214882008-03-02 09:29:48 -05001571static int lo_ioctl(struct block_device *bdev, fmode_t mode,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001572 unsigned int cmd, unsigned long arg)
1573{
Al Virobb214882008-03-02 09:29:48 -05001574 struct loop_device *lo = bdev->bd_disk->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001575 int err;
1576
Linus Torvalds1da177e2005-04-16 15:20:36 -07001577 switch (cmd) {
1578 case LOOP_SET_FD:
Jan Kara757ecf42018-11-08 14:01:10 +01001579 return loop_set_fd(lo, mode, bdev, arg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001580 case LOOP_CHANGE_FD:
Jan Karac3710772018-11-08 14:01:11 +01001581 return loop_change_fd(lo, bdev, arg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001582 case LOOP_CLR_FD:
Jan Kara7ccd0792018-11-08 14:01:07 +01001583 return loop_clr_fd(lo);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001584 case LOOP_SET_STATUS:
Dmitry Monakhov7035b5d2011-11-16 09:21:49 +01001585 err = -EPERM;
Jan Karaa1316542018-11-08 14:01:05 +01001586 if ((mode & FMODE_WRITE) || capable(CAP_SYS_ADMIN)) {
Dmitry Monakhov7035b5d2011-11-16 09:21:49 +01001587 err = loop_set_status_old(lo,
1588 (struct loop_info __user *)arg);
Jan Karaa1316542018-11-08 14:01:05 +01001589 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001590 break;
1591 case LOOP_GET_STATUS:
Jan Kara4a5ce9b2018-11-08 14:01:08 +01001592 return loop_get_status_old(lo, (struct loop_info __user *) arg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001593 case LOOP_SET_STATUS64:
Dmitry Monakhov7035b5d2011-11-16 09:21:49 +01001594 err = -EPERM;
Jan Karaa1316542018-11-08 14:01:05 +01001595 if ((mode & FMODE_WRITE) || capable(CAP_SYS_ADMIN)) {
Dmitry Monakhov7035b5d2011-11-16 09:21:49 +01001596 err = loop_set_status64(lo,
1597 (struct loop_info64 __user *) arg);
Jan Karaa1316542018-11-08 14:01:05 +01001598 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001599 break;
1600 case LOOP_GET_STATUS64:
Jan Kara4a5ce9b2018-11-08 14:01:08 +01001601 return loop_get_status64(lo, (struct loop_info64 __user *) arg);
J. R. Okajima53d66602009-03-31 15:23:43 -07001602 case LOOP_SET_CAPACITY:
Ming Leiab1cb272015-08-17 10:31:50 +08001603 case LOOP_SET_DIRECT_IO:
Omar Sandoval89e4fde2017-08-24 00:03:43 -07001604 case LOOP_SET_BLOCK_SIZE:
Jan Karaa1316542018-11-08 14:01:05 +01001605 if (!(mode & FMODE_WRITE) && !capable(CAP_SYS_ADMIN))
1606 return -EPERM;
1607 /* Fall through */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001608 default:
Jan Karaa1316542018-11-08 14:01:05 +01001609 err = lo_simple_ioctl(lo, cmd, arg);
1610 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001611 }
Nikanth Karthikesanf028f3b2009-03-24 12:33:41 +01001612
Linus Torvalds1da177e2005-04-16 15:20:36 -07001613 return err;
1614}
1615
David Howells863d5b822006-08-29 19:06:14 +01001616#ifdef CONFIG_COMPAT
1617struct compat_loop_info {
1618 compat_int_t lo_number; /* ioctl r/o */
1619 compat_dev_t lo_device; /* ioctl r/o */
1620 compat_ulong_t lo_inode; /* ioctl r/o */
1621 compat_dev_t lo_rdevice; /* ioctl r/o */
1622 compat_int_t lo_offset;
1623 compat_int_t lo_encrypt_type;
1624 compat_int_t lo_encrypt_key_size; /* ioctl w/o */
1625 compat_int_t lo_flags; /* ioctl r/o */
1626 char lo_name[LO_NAME_SIZE];
1627 unsigned char lo_encrypt_key[LO_KEY_SIZE]; /* ioctl w/o */
1628 compat_ulong_t lo_init[2];
1629 char reserved[4];
1630};
1631
1632/*
1633 * Transfer 32-bit compatibility structure in userspace to 64-bit loop info
1634 * - noinlined to reduce stack space usage in main part of driver
1635 */
1636static noinline int
Al Viroba674cf2006-10-10 22:48:27 +01001637loop_info64_from_compat(const struct compat_loop_info __user *arg,
David Howells863d5b822006-08-29 19:06:14 +01001638 struct loop_info64 *info64)
1639{
1640 struct compat_loop_info info;
1641
1642 if (copy_from_user(&info, arg, sizeof(info)))
1643 return -EFAULT;
1644
1645 memset(info64, 0, sizeof(*info64));
1646 info64->lo_number = info.lo_number;
1647 info64->lo_device = info.lo_device;
1648 info64->lo_inode = info.lo_inode;
1649 info64->lo_rdevice = info.lo_rdevice;
1650 info64->lo_offset = info.lo_offset;
1651 info64->lo_sizelimit = 0;
1652 info64->lo_encrypt_type = info.lo_encrypt_type;
1653 info64->lo_encrypt_key_size = info.lo_encrypt_key_size;
1654 info64->lo_flags = info.lo_flags;
1655 info64->lo_init[0] = info.lo_init[0];
1656 info64->lo_init[1] = info.lo_init[1];
1657 if (info.lo_encrypt_type == LO_CRYPT_CRYPTOAPI)
1658 memcpy(info64->lo_crypt_name, info.lo_name, LO_NAME_SIZE);
1659 else
1660 memcpy(info64->lo_file_name, info.lo_name, LO_NAME_SIZE);
1661 memcpy(info64->lo_encrypt_key, info.lo_encrypt_key, LO_KEY_SIZE);
1662 return 0;
1663}
1664
1665/*
1666 * Transfer 64-bit loop info to 32-bit compatibility structure in userspace
1667 * - noinlined to reduce stack space usage in main part of driver
1668 */
1669static noinline int
1670loop_info64_to_compat(const struct loop_info64 *info64,
1671 struct compat_loop_info __user *arg)
1672{
1673 struct compat_loop_info info;
1674
1675 memset(&info, 0, sizeof(info));
1676 info.lo_number = info64->lo_number;
1677 info.lo_device = info64->lo_device;
1678 info.lo_inode = info64->lo_inode;
1679 info.lo_rdevice = info64->lo_rdevice;
1680 info.lo_offset = info64->lo_offset;
1681 info.lo_encrypt_type = info64->lo_encrypt_type;
1682 info.lo_encrypt_key_size = info64->lo_encrypt_key_size;
1683 info.lo_flags = info64->lo_flags;
1684 info.lo_init[0] = info64->lo_init[0];
1685 info.lo_init[1] = info64->lo_init[1];
1686 if (info.lo_encrypt_type == LO_CRYPT_CRYPTOAPI)
1687 memcpy(info.lo_name, info64->lo_crypt_name, LO_NAME_SIZE);
1688 else
1689 memcpy(info.lo_name, info64->lo_file_name, LO_NAME_SIZE);
1690 memcpy(info.lo_encrypt_key, info64->lo_encrypt_key, LO_KEY_SIZE);
1691
1692 /* error in case values were truncated */
1693 if (info.lo_device != info64->lo_device ||
1694 info.lo_rdevice != info64->lo_rdevice ||
1695 info.lo_inode != info64->lo_inode ||
1696 info.lo_offset != info64->lo_offset ||
1697 info.lo_init[0] != info64->lo_init[0] ||
1698 info.lo_init[1] != info64->lo_init[1])
1699 return -EOVERFLOW;
1700
1701 if (copy_to_user(arg, &info, sizeof(info)))
1702 return -EFAULT;
1703 return 0;
1704}
1705
1706static int
1707loop_set_status_compat(struct loop_device *lo,
1708 const struct compat_loop_info __user *arg)
1709{
1710 struct loop_info64 info64;
1711 int ret;
1712
1713 ret = loop_info64_from_compat(arg, &info64);
1714 if (ret < 0)
1715 return ret;
1716 return loop_set_status(lo, &info64);
1717}
1718
1719static int
1720loop_get_status_compat(struct loop_device *lo,
1721 struct compat_loop_info __user *arg)
1722{
1723 struct loop_info64 info64;
Omar Sandovalbdac616db2018-04-06 09:57:03 -07001724 int err;
David Howells863d5b822006-08-29 19:06:14 +01001725
Jan Kara4a5ce9b2018-11-08 14:01:08 +01001726 if (!arg)
Omar Sandovalbdac616db2018-04-06 09:57:03 -07001727 return -EINVAL;
Omar Sandovalbdac616db2018-04-06 09:57:03 -07001728 err = loop_get_status(lo, &info64);
David Howells863d5b822006-08-29 19:06:14 +01001729 if (!err)
1730 err = loop_info64_to_compat(&info64, arg);
1731 return err;
1732}
1733
Al Virobb214882008-03-02 09:29:48 -05001734static int lo_compat_ioctl(struct block_device *bdev, fmode_t mode,
1735 unsigned int cmd, unsigned long arg)
David Howells863d5b822006-08-29 19:06:14 +01001736{
Al Virobb214882008-03-02 09:29:48 -05001737 struct loop_device *lo = bdev->bd_disk->private_data;
David Howells863d5b822006-08-29 19:06:14 +01001738 int err;
1739
David Howells863d5b822006-08-29 19:06:14 +01001740 switch(cmd) {
1741 case LOOP_SET_STATUS:
Jan Kara550df5fd2018-11-08 14:01:09 +01001742 err = loop_set_status_compat(lo,
1743 (const struct compat_loop_info __user *)arg);
David Howells863d5b822006-08-29 19:06:14 +01001744 break;
1745 case LOOP_GET_STATUS:
Jan Kara4a5ce9b2018-11-08 14:01:08 +01001746 err = loop_get_status_compat(lo,
1747 (struct compat_loop_info __user *)arg);
David Howells863d5b822006-08-29 19:06:14 +01001748 break;
J. R. Okajima53d66602009-03-31 15:23:43 -07001749 case LOOP_SET_CAPACITY:
David Howells863d5b822006-08-29 19:06:14 +01001750 case LOOP_CLR_FD:
1751 case LOOP_GET_STATUS64:
1752 case LOOP_SET_STATUS64:
1753 arg = (unsigned long) compat_ptr(arg);
Gustavo A. R. Silvad893ff82018-07-02 12:52:06 -05001754 /* fall through */
David Howells863d5b822006-08-29 19:06:14 +01001755 case LOOP_SET_FD:
1756 case LOOP_CHANGE_FD:
Evan Green9fea4b32018-07-02 16:03:46 -07001757 case LOOP_SET_BLOCK_SIZE:
Al Virobb214882008-03-02 09:29:48 -05001758 err = lo_ioctl(bdev, mode, cmd, arg);
David Howells863d5b822006-08-29 19:06:14 +01001759 break;
1760 default:
1761 err = -ENOIOCTLCMD;
1762 break;
1763 }
David Howells863d5b822006-08-29 19:06:14 +01001764 return err;
1765}
1766#endif
1767
Al Virobb214882008-03-02 09:29:48 -05001768static int lo_open(struct block_device *bdev, fmode_t mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001769{
Kay Sievers770fe302011-07-31 22:08:04 +02001770 struct loop_device *lo;
Jan Kara0a42e992018-11-08 14:01:04 +01001771 int err;
Kay Sievers770fe302011-07-31 22:08:04 +02001772
Jan Kara0a42e992018-11-08 14:01:04 +01001773 err = mutex_lock_killable(&loop_ctl_mutex);
1774 if (err)
1775 return err;
Kay Sievers770fe302011-07-31 22:08:04 +02001776 lo = bdev->bd_disk->private_data;
1777 if (!lo) {
1778 err = -ENXIO;
1779 goto out;
1780 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001781
Ming Leif8933662015-05-06 12:26:23 +08001782 atomic_inc(&lo->lo_refcnt);
Kay Sievers770fe302011-07-31 22:08:04 +02001783out:
Jan Kara0a42e992018-11-08 14:01:04 +01001784 mutex_unlock(&loop_ctl_mutex);
Kay Sievers770fe302011-07-31 22:08:04 +02001785 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001786}
1787
Jan Kara967d1dc2018-11-08 14:01:03 +01001788static void lo_release(struct gendisk *disk, fmode_t mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001789{
Jan Kara967d1dc2018-11-08 14:01:03 +01001790 struct loop_device *lo;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001791
Jan Kara0a42e992018-11-08 14:01:04 +01001792 mutex_lock(&loop_ctl_mutex);
Jan Kara967d1dc2018-11-08 14:01:03 +01001793 lo = disk->private_data;
Ming Leif8933662015-05-06 12:26:23 +08001794 if (atomic_dec_return(&lo->lo_refcnt))
Jan Kara0a42e992018-11-08 14:01:04 +01001795 goto out_unlock;
Ming Leif8933662015-05-06 12:26:23 +08001796
Milan Broz14f27932008-12-12 14:48:27 +01001797 if (lo->lo_flags & LO_FLAGS_AUTOCLEAR) {
Jan Karaa2505b72018-11-08 14:01:06 +01001798 if (lo->lo_state != Lo_bound)
1799 goto out_unlock;
1800 lo->lo_state = Lo_rundown;
Jan Kara7ccd0792018-11-08 14:01:07 +01001801 mutex_unlock(&loop_ctl_mutex);
Milan Broz14f27932008-12-12 14:48:27 +01001802 /*
1803 * In autoclear mode, stop the loop thread
1804 * and remove configuration after last close.
1805 */
Jan Kara0da03ca2018-11-08 14:01:14 +01001806 __loop_clr_fd(lo, true);
Jan Kara7ccd0792018-11-08 14:01:07 +01001807 return;
Omar Sandoval43cade802017-08-24 00:03:44 -07001808 } else if (lo->lo_state == Lo_bound) {
Milan Broz14f27932008-12-12 14:48:27 +01001809 /*
1810 * Otherwise keep thread (if running) and config,
1811 * but flush possible ongoing bios in thread.
1812 */
Omar Sandoval43cade802017-08-24 00:03:44 -07001813 blk_mq_freeze_queue(lo->lo_queue);
1814 blk_mq_unfreeze_queue(lo->lo_queue);
Milan Broz14f27932008-12-12 14:48:27 +01001815 }
David Woodhouse96c58652008-02-06 01:36:27 -08001816
Jan Kara0a42e992018-11-08 14:01:04 +01001817out_unlock:
Tetsuo Handa310ca162018-11-08 14:01:02 +01001818 mutex_unlock(&loop_ctl_mutex);
Linus Torvaldsae665012018-01-05 16:26:00 -08001819}
1820
Alexey Dobriyan83d5cde2009-09-21 17:01:13 -07001821static const struct block_device_operations lo_fops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001822 .owner = THIS_MODULE,
Al Virobb214882008-03-02 09:29:48 -05001823 .open = lo_open,
1824 .release = lo_release,
1825 .ioctl = lo_ioctl,
David Howells863d5b822006-08-29 19:06:14 +01001826#ifdef CONFIG_COMPAT
Al Virobb214882008-03-02 09:29:48 -05001827 .compat_ioctl = lo_compat_ioctl,
David Howells863d5b822006-08-29 19:06:14 +01001828#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001829};
1830
1831/*
1832 * And now the modules code and kernel interface.
1833 */
Ken Chen73285082007-05-08 00:28:20 -07001834static int max_loop;
Joe Perches5657a812018-05-24 13:38:59 -06001835module_param(max_loop, int, 0444);
Ken Chena47653f2007-06-08 13:46:44 -07001836MODULE_PARM_DESC(max_loop, "Maximum number of loop devices");
Joe Perches5657a812018-05-24 13:38:59 -06001837module_param(max_part, int, 0444);
Laurent Vivier476a4812008-03-26 12:11:53 +01001838MODULE_PARM_DESC(max_part, "Maximum number of partitions per loop device");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001839MODULE_LICENSE("GPL");
1840MODULE_ALIAS_BLOCKDEV_MAJOR(LOOP_MAJOR);
1841
1842int loop_register_transfer(struct loop_func_table *funcs)
1843{
1844 unsigned int n = funcs->number;
1845
1846 if (n >= MAX_LO_CRYPT || xfer_funcs[n])
1847 return -EINVAL;
1848 xfer_funcs[n] = funcs;
1849 return 0;
1850}
1851
Kay Sievers34dd82a2011-07-31 22:08:04 +02001852static int unregister_transfer_cb(int id, void *ptr, void *data)
1853{
1854 struct loop_device *lo = ptr;
1855 struct loop_func_table *xfer = data;
1856
Tetsuo Handa310ca162018-11-08 14:01:02 +01001857 mutex_lock(&loop_ctl_mutex);
Kay Sievers34dd82a2011-07-31 22:08:04 +02001858 if (lo->lo_encryption == xfer)
1859 loop_release_xfer(lo);
Tetsuo Handa310ca162018-11-08 14:01:02 +01001860 mutex_unlock(&loop_ctl_mutex);
Kay Sievers34dd82a2011-07-31 22:08:04 +02001861 return 0;
1862}
1863
Linus Torvalds1da177e2005-04-16 15:20:36 -07001864int loop_unregister_transfer(int number)
1865{
1866 unsigned int n = number;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001867 struct loop_func_table *xfer;
1868
1869 if (n == 0 || n >= MAX_LO_CRYPT || (xfer = xfer_funcs[n]) == NULL)
1870 return -EINVAL;
1871
1872 xfer_funcs[n] = NULL;
Kay Sievers34dd82a2011-07-31 22:08:04 +02001873 idr_for_each(&loop_index_idr, &unregister_transfer_cb, xfer);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001874 return 0;
1875}
1876
1877EXPORT_SYMBOL(loop_register_transfer);
1878EXPORT_SYMBOL(loop_unregister_transfer);
1879
Christoph Hellwigfc17b652017-06-03 09:38:05 +02001880static blk_status_t loop_queue_rq(struct blk_mq_hw_ctx *hctx,
Ming Leib5dd2f62014-12-31 13:22:57 +00001881 const struct blk_mq_queue_data *bd)
1882{
Jens Axboe1894e912018-04-13 16:24:29 -06001883 struct request *rq = bd->rq;
1884 struct loop_cmd *cmd = blk_mq_rq_to_pdu(rq);
1885 struct loop_device *lo = rq->q->queuedata;
Ming Leib5dd2f62014-12-31 13:22:57 +00001886
Jens Axboe1894e912018-04-13 16:24:29 -06001887 blk_mq_start_request(rq);
Ming Leib5dd2f62014-12-31 13:22:57 +00001888
Ming Leif4aa4c72015-05-05 19:49:54 +08001889 if (lo->lo_state != Lo_bound)
Christoph Hellwigfc17b652017-06-03 09:38:05 +02001890 return BLK_STS_IOERR;
Ming Leif4aa4c72015-05-05 19:49:54 +08001891
Jens Axboe1894e912018-04-13 16:24:29 -06001892 switch (req_op(rq)) {
Christoph Hellwigf0225ca2016-08-04 16:10:00 +02001893 case REQ_OP_FLUSH:
1894 case REQ_OP_DISCARD:
Christoph Hellwig19372e22017-04-05 19:21:15 +02001895 case REQ_OP_WRITE_ZEROES:
Ming Leibc07c102015-08-17 10:31:51 +08001896 cmd->use_aio = false;
Christoph Hellwigf0225ca2016-08-04 16:10:00 +02001897 break;
1898 default:
1899 cmd->use_aio = lo->use_dio;
1900 break;
1901 }
Ming Leibc07c102015-08-17 10:31:51 +08001902
Shaohua Lid4478e92017-09-25 13:07:22 -06001903 /* always use the first bio's css */
Shaohua Li0b508bc2017-09-26 11:02:12 -07001904#ifdef CONFIG_BLK_CGROUP
Dennis Zhoudb6638d2018-12-05 12:10:35 -05001905 if (cmd->use_aio && rq->bio && rq->bio->bi_blkg) {
1906 cmd->css = &bio_blkcg(rq->bio)->css;
Shaohua Lid4478e92017-09-25 13:07:22 -06001907 css_get(cmd->css);
1908 } else
1909#endif
1910 cmd->css = NULL;
Petr Mladek39891442016-10-11 13:55:20 -07001911 kthread_queue_work(&lo->worker, &cmd->work);
Ming Leib5dd2f62014-12-31 13:22:57 +00001912
Christoph Hellwigfc17b652017-06-03 09:38:05 +02001913 return BLK_STS_OK;
Ming Leib5dd2f62014-12-31 13:22:57 +00001914}
1915
1916static void loop_handle_cmd(struct loop_cmd *cmd)
1917{
Jens Axboe1894e912018-04-13 16:24:29 -06001918 struct request *rq = blk_mq_rq_from_pdu(cmd);
1919 const bool write = op_is_write(req_op(rq));
1920 struct loop_device *lo = rq->q->queuedata;
Christoph Hellwigf4829a92015-09-27 21:01:50 +02001921 int ret = 0;
Ming Leib5dd2f62014-12-31 13:22:57 +00001922
Christoph Hellwigf4829a92015-09-27 21:01:50 +02001923 if (write && (lo->lo_flags & LO_FLAGS_READ_ONLY)) {
1924 ret = -EIO;
Ming Leib5dd2f62014-12-31 13:22:57 +00001925 goto failed;
Christoph Hellwigf4829a92015-09-27 21:01:50 +02001926 }
Ming Leib5dd2f62014-12-31 13:22:57 +00001927
Jens Axboe1894e912018-04-13 16:24:29 -06001928 ret = do_req_filebacked(lo, rq);
Ming Leib5dd2f62014-12-31 13:22:57 +00001929 failed:
Ming Leibc07c102015-08-17 10:31:51 +08001930 /* complete non-aio request */
Christoph Hellwigfe2cb292017-04-20 16:03:02 +02001931 if (!cmd->use_aio || ret) {
1932 cmd->ret = ret ? -EIO : 0;
Jens Axboe1894e912018-04-13 16:24:29 -06001933 blk_mq_complete_request(rq);
Christoph Hellwigfe2cb292017-04-20 16:03:02 +02001934 }
Ming Leib5dd2f62014-12-31 13:22:57 +00001935}
1936
Ming Leie03a3d72015-08-17 10:31:48 +08001937static void loop_queue_work(struct kthread_work *work)
Ming Leib5dd2f62014-12-31 13:22:57 +00001938{
1939 struct loop_cmd *cmd =
Ming Leie03a3d72015-08-17 10:31:48 +08001940 container_of(work, struct loop_cmd, work);
Ming Leib5dd2f62014-12-31 13:22:57 +00001941
1942 loop_handle_cmd(cmd);
1943}
1944
Christoph Hellwigd6296d392017-05-01 10:19:08 -06001945static int loop_init_request(struct blk_mq_tag_set *set, struct request *rq,
1946 unsigned int hctx_idx, unsigned int numa_node)
Ming Leib5dd2f62014-12-31 13:22:57 +00001947{
1948 struct loop_cmd *cmd = blk_mq_rq_to_pdu(rq);
1949
Petr Mladek39891442016-10-11 13:55:20 -07001950 kthread_init_work(&cmd->work, loop_queue_work);
Ming Leib5dd2f62014-12-31 13:22:57 +00001951 return 0;
1952}
1953
Eric Biggersf363b082017-03-30 13:39:16 -07001954static const struct blk_mq_ops loop_mq_ops = {
Ming Leib5dd2f62014-12-31 13:22:57 +00001955 .queue_rq = loop_queue_rq,
Ming Leib5dd2f62014-12-31 13:22:57 +00001956 .init_request = loop_init_request,
Christoph Hellwigfe2cb292017-04-20 16:03:02 +02001957 .complete = lo_complete_rq,
Ming Leib5dd2f62014-12-31 13:22:57 +00001958};
1959
Kay Sievers34dd82a2011-07-31 22:08:04 +02001960static int loop_add(struct loop_device **l, int i)
Ken Chen73285082007-05-08 00:28:20 -07001961{
1962 struct loop_device *lo;
1963 struct gendisk *disk;
Kay Sievers34dd82a2011-07-31 22:08:04 +02001964 int err;
Ken Chen73285082007-05-08 00:28:20 -07001965
Silva Paulo68d740d2012-07-14 15:39:58 -07001966 err = -ENOMEM;
Ken Chen73285082007-05-08 00:28:20 -07001967 lo = kzalloc(sizeof(*lo), GFP_KERNEL);
Silva Paulo68d740d2012-07-14 15:39:58 -07001968 if (!lo)
Ken Chen73285082007-05-08 00:28:20 -07001969 goto out;
Kay Sievers34dd82a2011-07-31 22:08:04 +02001970
Mikulas Patockaef7e7c82013-10-15 14:14:38 -06001971 lo->lo_state = Lo_unbound;
1972
Tejun Heoc718aa62013-02-27 17:03:58 -08001973 /* allocate id, if @id >= 0, we're requesting that specific id */
Kay Sievers34dd82a2011-07-31 22:08:04 +02001974 if (i >= 0) {
Tejun Heoc718aa62013-02-27 17:03:58 -08001975 err = idr_alloc(&loop_index_idr, lo, i, i + 1, GFP_KERNEL);
1976 if (err == -ENOSPC)
Kay Sievers34dd82a2011-07-31 22:08:04 +02001977 err = -EEXIST;
Kay Sievers34dd82a2011-07-31 22:08:04 +02001978 } else {
Tejun Heoc718aa62013-02-27 17:03:58 -08001979 err = idr_alloc(&loop_index_idr, lo, 0, 0, GFP_KERNEL);
Kay Sievers34dd82a2011-07-31 22:08:04 +02001980 }
1981 if (err < 0)
1982 goto out_free_dev;
Tejun Heoc718aa62013-02-27 17:03:58 -08001983 i = err;
Ken Chen73285082007-05-08 00:28:20 -07001984
Wei Yongjun183cfb52013-03-22 08:59:19 -06001985 err = -ENOMEM;
Ming Leib5dd2f62014-12-31 13:22:57 +00001986 lo->tag_set.ops = &loop_mq_ops;
1987 lo->tag_set.nr_hw_queues = 1;
1988 lo->tag_set.queue_depth = 128;
1989 lo->tag_set.numa_node = NUMA_NO_NODE;
1990 lo->tag_set.cmd_size = sizeof(struct loop_cmd);
Ming Lei56d18f62019-02-15 19:13:24 +08001991 lo->tag_set.flags = BLK_MQ_F_SHOULD_MERGE;
Ming Leib5dd2f62014-12-31 13:22:57 +00001992 lo->tag_set.driver_data = lo;
1993
1994 err = blk_mq_alloc_tag_set(&lo->tag_set);
1995 if (err)
Mikulas Patocka3ec981e2013-10-14 12:12:24 -04001996 goto out_free_idr;
Ken Chen73285082007-05-08 00:28:20 -07001997
Ming Leib5dd2f62014-12-31 13:22:57 +00001998 lo->lo_queue = blk_mq_init_queue(&lo->tag_set);
Chengguang Xu38a34992018-12-16 17:35:00 +08001999 if (IS_ERR(lo->lo_queue)) {
Ming Leib5dd2f62014-12-31 13:22:57 +00002000 err = PTR_ERR(lo->lo_queue);
2001 goto out_cleanup_tags;
2002 }
Mikulas Patockaef7e7c82013-10-15 14:14:38 -06002003 lo->lo_queue->queuedata = lo;
2004
Shaohua Li54bb0ad2017-08-31 22:09:45 -07002005 blk_queue_max_hw_sectors(lo->lo_queue, BLK_DEF_MAX_SECTORS);
Shaohua Li40326d82017-08-31 22:09:46 -07002006
Ming Lei5b5e20f2015-08-17 10:31:47 +08002007 /*
Shaohua Li40326d82017-08-31 22:09:46 -07002008 * By default, we do buffer IO, so it doesn't make sense to enable
2009 * merge because the I/O submitted to backing file is handled page by
2010 * page. For directio mode, merge does help to dispatch bigger request
2011 * to underlayer disk. We will enable merge once directio is enabled.
Ming Lei5b5e20f2015-08-17 10:31:47 +08002012 */
Bart Van Assche8b904b52018-03-07 17:10:10 -08002013 blk_queue_flag_set(QUEUE_FLAG_NOMERGES, lo->lo_queue);
Ming Lei5b5e20f2015-08-17 10:31:47 +08002014
Minfei Huang7a649732016-06-07 10:05:15 +08002015 err = -ENOMEM;
Laurent Vivier476a4812008-03-26 12:11:53 +01002016 disk = lo->lo_disk = alloc_disk(1 << part_shift);
Ken Chen73285082007-05-08 00:28:20 -07002017 if (!disk)
2018 goto out_free_queue;
2019
Kay Sieverse03c8dd2011-08-23 20:12:04 +02002020 /*
2021 * Disable partition scanning by default. The in-kernel partition
2022 * scanning can be requested individually per-device during its
2023 * setup. Userspace can always add and remove partitions from all
2024 * devices. The needed partition minors are allocated from the
2025 * extended minor space, the main loop device numbers will continue
2026 * to match the loop minors, regardless of the number of partitions
2027 * used.
2028 *
2029 * If max_part is given, partition scanning is globally enabled for
2030 * all loop devices. The minors for the main loop devices will be
2031 * multiples of max_part.
2032 *
2033 * Note: Global-for-all-devices, set-only-at-init, read-only module
2034 * parameteters like 'max_loop' and 'max_part' make things needlessly
2035 * complicated, are too static, inflexible and may surprise
2036 * userspace tools. Parameters like this in general should be avoided.
2037 */
2038 if (!part_shift)
2039 disk->flags |= GENHD_FL_NO_PART_SCAN;
2040 disk->flags |= GENHD_FL_EXT_DEVT;
Ming Leif8933662015-05-06 12:26:23 +08002041 atomic_set(&lo->lo_refcnt, 0);
Ken Chen73285082007-05-08 00:28:20 -07002042 lo->lo_number = i;
Ken Chen73285082007-05-08 00:28:20 -07002043 spin_lock_init(&lo->lo_lock);
2044 disk->major = LOOP_MAJOR;
Laurent Vivier476a4812008-03-26 12:11:53 +01002045 disk->first_minor = i << part_shift;
Ken Chen73285082007-05-08 00:28:20 -07002046 disk->fops = &lo_fops;
2047 disk->private_data = lo;
2048 disk->queue = lo->lo_queue;
2049 sprintf(disk->disk_name, "loop%d", i);
Kay Sievers34dd82a2011-07-31 22:08:04 +02002050 add_disk(disk);
2051 *l = lo;
2052 return lo->lo_number;
Ken Chen73285082007-05-08 00:28:20 -07002053
2054out_free_queue:
2055 blk_cleanup_queue(lo->lo_queue);
Ming Leib5dd2f62014-12-31 13:22:57 +00002056out_cleanup_tags:
2057 blk_mq_free_tag_set(&lo->tag_set);
Mikulas Patocka3ec981e2013-10-14 12:12:24 -04002058out_free_idr:
2059 idr_remove(&loop_index_idr, i);
Ken Chen73285082007-05-08 00:28:20 -07002060out_free_dev:
2061 kfree(lo);
2062out:
Kay Sievers34dd82a2011-07-31 22:08:04 +02002063 return err;
Ken Chen73285082007-05-08 00:28:20 -07002064}
2065
Kay Sievers34dd82a2011-07-31 22:08:04 +02002066static void loop_remove(struct loop_device *lo)
Ken Chen73285082007-05-08 00:28:20 -07002067{
NeilBrown6cd18e72015-04-27 14:12:22 +10002068 del_gendisk(lo->lo_disk);
Bart Van Assche0fa8ebd2018-02-28 10:15:28 -08002069 blk_cleanup_queue(lo->lo_queue);
Ming Leib5dd2f62014-12-31 13:22:57 +00002070 blk_mq_free_tag_set(&lo->tag_set);
Ken Chen73285082007-05-08 00:28:20 -07002071 put_disk(lo->lo_disk);
Ken Chen73285082007-05-08 00:28:20 -07002072 kfree(lo);
2073}
2074
Kay Sievers770fe302011-07-31 22:08:04 +02002075static int find_free_cb(int id, void *ptr, void *data)
Ken Chena47653f2007-06-08 13:46:44 -07002076{
Kay Sievers770fe302011-07-31 22:08:04 +02002077 struct loop_device *lo = ptr;
2078 struct loop_device **l = data;
Ken Chena47653f2007-06-08 13:46:44 -07002079
Kay Sievers770fe302011-07-31 22:08:04 +02002080 if (lo->lo_state == Lo_unbound) {
2081 *l = lo;
2082 return 1;
Ken Chena47653f2007-06-08 13:46:44 -07002083 }
Kay Sievers770fe302011-07-31 22:08:04 +02002084 return 0;
Ken Chena47653f2007-06-08 13:46:44 -07002085}
2086
Kay Sievers34dd82a2011-07-31 22:08:04 +02002087static int loop_lookup(struct loop_device **l, int i)
Ken Chena47653f2007-06-08 13:46:44 -07002088{
Ken Chena47653f2007-06-08 13:46:44 -07002089 struct loop_device *lo;
Kay Sievers34dd82a2011-07-31 22:08:04 +02002090 int ret = -ENODEV;
Ken Chena47653f2007-06-08 13:46:44 -07002091
Kay Sievers770fe302011-07-31 22:08:04 +02002092 if (i < 0) {
2093 int err;
2094
2095 err = idr_for_each(&loop_index_idr, &find_free_cb, &lo);
2096 if (err == 1) {
2097 *l = lo;
2098 ret = lo->lo_number;
2099 }
2100 goto out;
2101 }
2102
2103 /* lookup and return a specific i */
Kay Sievers34dd82a2011-07-31 22:08:04 +02002104 lo = idr_find(&loop_index_idr, i);
Ken Chena47653f2007-06-08 13:46:44 -07002105 if (lo) {
Kay Sievers34dd82a2011-07-31 22:08:04 +02002106 *l = lo;
2107 ret = lo->lo_number;
Ken Chena47653f2007-06-08 13:46:44 -07002108 }
Kay Sievers770fe302011-07-31 22:08:04 +02002109out:
Kay Sievers34dd82a2011-07-31 22:08:04 +02002110 return ret;
Ken Chena47653f2007-06-08 13:46:44 -07002111}
2112
Ken Chen73285082007-05-08 00:28:20 -07002113static struct kobject *loop_probe(dev_t dev, int *part, void *data)
2114{
Al Viro705962c2007-05-13 05:52:32 -04002115 struct loop_device *lo;
Al Viro07002e92007-05-12 16:23:15 -04002116 struct kobject *kobj;
Kay Sievers34dd82a2011-07-31 22:08:04 +02002117 int err;
Ken Chen73285082007-05-08 00:28:20 -07002118
Jan Kara0a42e992018-11-08 14:01:04 +01002119 mutex_lock(&loop_ctl_mutex);
Kay Sievers34dd82a2011-07-31 22:08:04 +02002120 err = loop_lookup(&lo, MINOR(dev) >> part_shift);
2121 if (err < 0)
2122 err = loop_add(&lo, MINOR(dev) >> part_shift);
2123 if (err < 0)
Mikulas Patockaa207f592013-10-14 12:13:24 -04002124 kobj = NULL;
Kay Sievers34dd82a2011-07-31 22:08:04 +02002125 else
Jan Kara3079c222018-02-26 13:01:38 +01002126 kobj = get_disk_and_module(lo->lo_disk);
Jan Kara0a42e992018-11-08 14:01:04 +01002127 mutex_unlock(&loop_ctl_mutex);
Ken Chen73285082007-05-08 00:28:20 -07002128
2129 *part = 0;
Al Viro07002e92007-05-12 16:23:15 -04002130 return kobj;
Ken Chen73285082007-05-08 00:28:20 -07002131}
2132
Kay Sievers770fe302011-07-31 22:08:04 +02002133static long loop_control_ioctl(struct file *file, unsigned int cmd,
2134 unsigned long parm)
2135{
2136 struct loop_device *lo;
Jan Kara0a42e992018-11-08 14:01:04 +01002137 int ret;
Kay Sievers770fe302011-07-31 22:08:04 +02002138
Jan Kara0a42e992018-11-08 14:01:04 +01002139 ret = mutex_lock_killable(&loop_ctl_mutex);
2140 if (ret)
2141 return ret;
2142
2143 ret = -ENOSYS;
Kay Sievers770fe302011-07-31 22:08:04 +02002144 switch (cmd) {
2145 case LOOP_CTL_ADD:
2146 ret = loop_lookup(&lo, parm);
2147 if (ret >= 0) {
2148 ret = -EEXIST;
2149 break;
2150 }
2151 ret = loop_add(&lo, parm);
2152 break;
2153 case LOOP_CTL_REMOVE:
2154 ret = loop_lookup(&lo, parm);
2155 if (ret < 0)
2156 break;
Kay Sievers770fe302011-07-31 22:08:04 +02002157 if (lo->lo_state != Lo_unbound) {
2158 ret = -EBUSY;
Kay Sievers770fe302011-07-31 22:08:04 +02002159 break;
2160 }
Ming Leif8933662015-05-06 12:26:23 +08002161 if (atomic_read(&lo->lo_refcnt) > 0) {
Kay Sievers770fe302011-07-31 22:08:04 +02002162 ret = -EBUSY;
Kay Sievers770fe302011-07-31 22:08:04 +02002163 break;
2164 }
2165 lo->lo_disk->private_data = NULL;
Kay Sievers770fe302011-07-31 22:08:04 +02002166 idr_remove(&loop_index_idr, lo->lo_number);
2167 loop_remove(lo);
2168 break;
2169 case LOOP_CTL_GET_FREE:
2170 ret = loop_lookup(&lo, -1);
2171 if (ret >= 0)
2172 break;
2173 ret = loop_add(&lo, -1);
2174 }
Jan Kara0a42e992018-11-08 14:01:04 +01002175 mutex_unlock(&loop_ctl_mutex);
Kay Sievers770fe302011-07-31 22:08:04 +02002176
2177 return ret;
2178}
2179
2180static const struct file_operations loop_ctl_fops = {
2181 .open = nonseekable_open,
2182 .unlocked_ioctl = loop_control_ioctl,
2183 .compat_ioctl = loop_control_ioctl,
2184 .owner = THIS_MODULE,
2185 .llseek = noop_llseek,
2186};
2187
2188static struct miscdevice loop_misc = {
2189 .minor = LOOP_CTRL_MINOR,
2190 .name = "loop-control",
2191 .fops = &loop_ctl_fops,
2192};
2193
2194MODULE_ALIAS_MISCDEV(LOOP_CTRL_MINOR);
2195MODULE_ALIAS("devname:loop-control");
2196
Linus Torvalds1da177e2005-04-16 15:20:36 -07002197static int __init loop_init(void)
2198{
Ken Chena47653f2007-06-08 13:46:44 -07002199 int i, nr;
2200 unsigned long range;
Kay Sievers34dd82a2011-07-31 22:08:04 +02002201 struct loop_device *lo;
Kay Sievers770fe302011-07-31 22:08:04 +02002202 int err;
Ken Chena47653f2007-06-08 13:46:44 -07002203
Laurent Vivier476a4812008-03-26 12:11:53 +01002204 part_shift = 0;
Namhyung Kimac04fee2011-05-27 07:59:25 +02002205 if (max_part > 0) {
Laurent Vivier476a4812008-03-26 12:11:53 +01002206 part_shift = fls(max_part);
2207
Namhyung Kimac04fee2011-05-27 07:59:25 +02002208 /*
2209 * Adjust max_part according to part_shift as it is exported
2210 * to user space so that user can decide correct minor number
2211 * if [s]he want to create more devices.
2212 *
2213 * Note that -1 is required because partition 0 is reserved
2214 * for the whole disk.
2215 */
2216 max_part = (1UL << part_shift) - 1;
2217 }
2218
Guo Chaob1a66502013-02-21 15:16:49 -08002219 if ((1UL << part_shift) > DISK_MAX_PARTS) {
2220 err = -EINVAL;
Anton Volkova8c1d062017-08-07 15:37:50 +03002221 goto err_out;
Guo Chaob1a66502013-02-21 15:16:49 -08002222 }
Namhyung Kim78f4bb32011-05-24 16:48:54 +02002223
Guo Chaob1a66502013-02-21 15:16:49 -08002224 if (max_loop > 1UL << (MINORBITS - part_shift)) {
2225 err = -EINVAL;
Anton Volkova8c1d062017-08-07 15:37:50 +03002226 goto err_out;
Guo Chaob1a66502013-02-21 15:16:49 -08002227 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002228
Kay Sieversd134b002011-07-31 22:08:04 +02002229 /*
2230 * If max_loop is specified, create that many devices upfront.
2231 * This also becomes a hard limit. If max_loop is not specified,
2232 * create CONFIG_BLK_DEV_LOOP_MIN_COUNT loop devices at module
2233 * init time. Loop devices can be requested on-demand with the
2234 * /dev/loop-control interface, or be instantiated by accessing
2235 * a 'dead' device node.
2236 */
Ken Chen73285082007-05-08 00:28:20 -07002237 if (max_loop) {
Ken Chena47653f2007-06-08 13:46:44 -07002238 nr = max_loop;
Namhyung Kima1c15c52011-05-24 16:48:55 +02002239 range = max_loop << part_shift;
Ken Chena47653f2007-06-08 13:46:44 -07002240 } else {
Kay Sieversd134b002011-07-31 22:08:04 +02002241 nr = CONFIG_BLK_DEV_LOOP_MIN_COUNT;
Namhyung Kima1c15c52011-05-24 16:48:55 +02002242 range = 1UL << MINORBITS;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002243 }
Ken Chena47653f2007-06-08 13:46:44 -07002244
Anton Volkova8c1d062017-08-07 15:37:50 +03002245 err = misc_register(&loop_misc);
2246 if (err < 0)
2247 goto err_out;
2248
2249
Guo Chaob1a66502013-02-21 15:16:49 -08002250 if (register_blkdev(LOOP_MAJOR, "loop")) {
2251 err = -EIO;
2252 goto misc_out;
2253 }
Ken Chena47653f2007-06-08 13:46:44 -07002254
Ken Chena47653f2007-06-08 13:46:44 -07002255 blk_register_region(MKDEV(LOOP_MAJOR, 0), range,
2256 THIS_MODULE, loop_probe, NULL, NULL);
2257
Kay Sieversd134b002011-07-31 22:08:04 +02002258 /* pre-create number of devices given by config or max_loop */
Jan Kara0a42e992018-11-08 14:01:04 +01002259 mutex_lock(&loop_ctl_mutex);
Kay Sievers34dd82a2011-07-31 22:08:04 +02002260 for (i = 0; i < nr; i++)
2261 loop_add(&lo, i);
Jan Kara0a42e992018-11-08 14:01:04 +01002262 mutex_unlock(&loop_ctl_mutex);
Kay Sievers34dd82a2011-07-31 22:08:04 +02002263
Ken Chen73285082007-05-08 00:28:20 -07002264 printk(KERN_INFO "loop: module loaded\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002265 return 0;
Guo Chaob1a66502013-02-21 15:16:49 -08002266
2267misc_out:
2268 misc_deregister(&loop_misc);
Anton Volkova8c1d062017-08-07 15:37:50 +03002269err_out:
Guo Chaob1a66502013-02-21 15:16:49 -08002270 return err;
Kay Sievers34dd82a2011-07-31 22:08:04 +02002271}
Ken Chena47653f2007-06-08 13:46:44 -07002272
Kay Sievers34dd82a2011-07-31 22:08:04 +02002273static int loop_exit_cb(int id, void *ptr, void *data)
2274{
2275 struct loop_device *lo = ptr;
Ken Chena47653f2007-06-08 13:46:44 -07002276
Kay Sievers34dd82a2011-07-31 22:08:04 +02002277 loop_remove(lo);
2278 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002279}
2280
Ken Chen73285082007-05-08 00:28:20 -07002281static void __exit loop_exit(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002282{
Ken Chena47653f2007-06-08 13:46:44 -07002283 unsigned long range;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002284
Namhyung Kima1c15c52011-05-24 16:48:55 +02002285 range = max_loop ? max_loop << part_shift : 1UL << MINORBITS;
Ken Chena47653f2007-06-08 13:46:44 -07002286
Kay Sievers34dd82a2011-07-31 22:08:04 +02002287 idr_for_each(&loop_index_idr, &loop_exit_cb, NULL);
Kay Sievers34dd82a2011-07-31 22:08:04 +02002288 idr_destroy(&loop_index_idr);
Ken Chen73285082007-05-08 00:28:20 -07002289
Ken Chena47653f2007-06-08 13:46:44 -07002290 blk_unregister_region(MKDEV(LOOP_MAJOR, 0), range);
Akinobu Mita00d59402007-07-17 04:03:46 -07002291 unregister_blkdev(LOOP_MAJOR, "loop");
Kay Sievers770fe302011-07-31 22:08:04 +02002292
2293 misc_deregister(&loop_misc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002294}
2295
2296module_init(loop_init);
2297module_exit(loop_exit);
2298
2299#ifndef MODULE
2300static int __init max_loop_setup(char *str)
2301{
2302 max_loop = simple_strtol(str, NULL, 0);
2303 return 1;
2304}
2305
2306__setup("max_loop=", max_loop_setup);
2307#endif