s390/vfio-ap: reset queues associated with adapter for queue unbound from driver
[linux-block.git] / drivers / block / loop.c
CommitLineData
f21e6e18 1// SPDX-License-Identifier: GPL-2.0-only
1da177e4 2/*
f21e6e18 3 * Copyright 1993 by Theodore Ts'o.
1da177e4 4 */
1da177e4
LT
5#include <linux/module.h>
6#include <linux/moduleparam.h>
7#include <linux/sched.h>
8#include <linux/fs.h>
4ee60ec1 9#include <linux/pagemap.h>
1da177e4
LT
10#include <linux/file.h>
11#include <linux/stat.h>
12#include <linux/errno.h>
13#include <linux/major.h>
14#include <linux/wait.h>
1da177e4
LT
15#include <linux/blkpg.h>
16#include <linux/init.h>
1da177e4
LT
17#include <linux/swap.h>
18#include <linux/slab.h>
863d5b82 19#include <linux/compat.h>
1da177e4 20#include <linux/suspend.h>
83144186 21#include <linux/freezer.h>
2a48fc0a 22#include <linux/mutex.h>
1da177e4 23#include <linux/writeback.h>
1da177e4
LT
24#include <linux/completion.h>
25#include <linux/highmem.h>
d6b29d7c 26#include <linux/splice.h>
ee862730 27#include <linux/sysfs.h>
770fe30a 28#include <linux/miscdevice.h>
dfaa2ef6 29#include <linux/falloc.h>
283e7e5d 30#include <linux/uio.h>
d9a08a9e 31#include <linux/ioprio.h>
db6638d7 32#include <linux/blk-cgroup.h>
c74d40e8 33#include <linux/sched/mm.h>
06582bc8 34#include <linux/statfs.h>
754d9679
CH
35#include <linux/uaccess.h>
36#include <linux/blk-mq.h>
37#include <linux/spinlock.h>
38#include <uapi/linux/loop.h>
d9a08a9e 39
754d9679
CH
40/* Possible states of device */
41enum {
42 Lo_unbound,
43 Lo_bound,
44 Lo_rundown,
45 Lo_deleting,
46};
d9a08a9e 47
754d9679
CH
48struct loop_func_table;
49
50struct loop_device {
51 int lo_number;
52 loff_t lo_offset;
53 loff_t lo_sizelimit;
54 int lo_flags;
55 char lo_file_name[LO_NAME_SIZE];
56
57 struct file * lo_backing_file;
58 struct block_device *lo_device;
59
60 gfp_t old_gfp_mask;
61
62 spinlock_t lo_lock;
63 int lo_state;
64 spinlock_t lo_work_lock;
65 struct workqueue_struct *workqueue;
66 struct work_struct rootcg_work;
67 struct list_head rootcg_cmd_list;
68 struct list_head idle_worker_list;
69 struct rb_root worker_tree;
70 struct timer_list timer;
71 bool use_dio;
72 bool sysfs_inited;
73
74 struct request_queue *lo_queue;
75 struct blk_mq_tag_set tag_set;
76 struct gendisk *lo_disk;
77 struct mutex lo_mutex;
78 bool idr_visible;
79};
1da177e4 80
754d9679
CH
81struct loop_cmd {
82 struct list_head list_entry;
83 bool use_aio; /* use AIO interface to handle I/O */
84 atomic_t ref; /* only for aio */
85 long ret;
86 struct kiocb iocb;
87 struct bio_vec *bvec;
88 struct cgroup_subsys_state *blkcg_css;
89 struct cgroup_subsys_state *memcg_css;
90};
1da177e4 91
87579e9b 92#define LOOP_IDLE_WORKER_TIMEOUT (60 * HZ)
e152a05f 93#define LOOP_DEFAULT_HW_Q_DEPTH 128
87579e9b 94
34dd82af 95static DEFINE_IDR(loop_index_idr);
310ca162 96static DEFINE_MUTEX(loop_ctl_mutex);
3ce6e1f6
TH
97static DEFINE_MUTEX(loop_validate_mutex);
98
99/**
100 * loop_global_lock_killable() - take locks for safe loop_validate_file() test
101 *
102 * @lo: struct loop_device
103 * @global: true if @lo is about to bind another "struct loop_device", false otherwise
104 *
105 * Returns 0 on success, -EINTR otherwise.
106 *
107 * Since loop_validate_file() traverses on other "struct loop_device" if
108 * is_loop_device() is true, we need a global lock for serializing concurrent
109 * loop_configure()/loop_change_fd()/__loop_clr_fd() calls.
110 */
111static int loop_global_lock_killable(struct loop_device *lo, bool global)
112{
113 int err;
114
115 if (global) {
116 err = mutex_lock_killable(&loop_validate_mutex);
117 if (err)
118 return err;
119 }
120 err = mutex_lock_killable(&lo->lo_mutex);
121 if (err && global)
122 mutex_unlock(&loop_validate_mutex);
123 return err;
124}
125
126/**
127 * loop_global_unlock() - release locks taken by loop_global_lock_killable()
128 *
129 * @lo: struct loop_device
130 * @global: true if @lo was about to bind another "struct loop_device", false otherwise
131 */
132static void loop_global_unlock(struct loop_device *lo, bool global)
133{
134 mutex_unlock(&lo->lo_mutex);
135 if (global)
136 mutex_unlock(&loop_validate_mutex);
137}
1da177e4 138
476a4813
LV
139static int max_part;
140static int part_shift;
141
7035b5df 142static loff_t get_size(loff_t offset, loff_t sizelimit, struct file *file)
1da177e4 143{
b7a1da69 144 loff_t loopsize;
1da177e4
LT
145
146 /* Compute loopsize in bytes */
b7a1da69
GC
147 loopsize = i_size_read(file->f_mapping->host);
148 if (offset > 0)
149 loopsize -= offset;
150 /* offset is beyond i_size, weird but possible */
7035b5df
DM
151 if (loopsize < 0)
152 return 0;
1da177e4 153
7035b5df
DM
154 if (sizelimit > 0 && sizelimit < loopsize)
155 loopsize = sizelimit;
1da177e4
LT
156 /*
157 * Unfortunately, if we want to do I/O on the device,
158 * the number of 512-byte sectors has to fit into a sector_t.
159 */
160 return loopsize >> 9;
161}
162
7035b5df
DM
163static loff_t get_loop_size(struct loop_device *lo, struct file *file)
164{
165 return get_size(lo->lo_offset, lo->lo_sizelimit, file);
166}
167
2e5ab5f3
ML
168static void __loop_update_dio(struct loop_device *lo, bool dio)
169{
170 struct file *file = lo->lo_backing_file;
171 struct address_space *mapping = file->f_mapping;
172 struct inode *inode = mapping->host;
173 unsigned short sb_bsize = 0;
174 unsigned dio_align = 0;
175 bool use_dio;
176
177 if (inode->i_sb->s_bdev) {
178 sb_bsize = bdev_logical_block_size(inode->i_sb->s_bdev);
179 dio_align = sb_bsize - 1;
180 }
181
182 /*
183 * We support direct I/O only if lo_offset is aligned with the
184 * logical I/O size of backing device, and the logical block
47e96246 185 * size of loop is bigger than the backing device's.
2e5ab5f3
ML
186 *
187 * TODO: the above condition may be loosed in the future, and
188 * direct I/O may be switched runtime at that time because most
89d790ab 189 * of requests in sane applications should be PAGE_SIZE aligned
2e5ab5f3
ML
190 */
191 if (dio) {
192 if (queue_logical_block_size(lo->lo_queue) >= sb_bsize &&
a2ad63da
N
193 !(lo->lo_offset & dio_align) &&
194 (file->f_mode & FMODE_CAN_ODIRECT))
2e5ab5f3
ML
195 use_dio = true;
196 else
197 use_dio = false;
198 } else {
199 use_dio = false;
200 }
201
202 if (lo->use_dio == use_dio)
203 return;
204
205 /* flush dirty pages before changing direct IO */
206 vfs_fsync(file, 0);
207
208 /*
209 * The flag of LO_FLAGS_DIRECT_IO is handled similarly with
210 * LO_FLAGS_READ_ONLY, both are set from kernel, and losetup
211 * will get updated by ioctl(LOOP_GET_STATUS)
212 */
0fbcf579
MC
213 if (lo->lo_state == Lo_bound)
214 blk_mq_freeze_queue(lo->lo_queue);
2e5ab5f3 215 lo->use_dio = use_dio;
40326d8a 216 if (use_dio) {
8b904b5b 217 blk_queue_flag_clear(QUEUE_FLAG_NOMERGES, lo->lo_queue);
2e5ab5f3 218 lo->lo_flags |= LO_FLAGS_DIRECT_IO;
40326d8a 219 } else {
8b904b5b 220 blk_queue_flag_set(QUEUE_FLAG_NOMERGES, lo->lo_queue);
2e5ab5f3 221 lo->lo_flags &= ~LO_FLAGS_DIRECT_IO;
40326d8a 222 }
0fbcf579
MC
223 if (lo->lo_state == Lo_bound)
224 blk_mq_unfreeze_queue(lo->lo_queue);
2e5ab5f3
ML
225}
226
5795b6f5
MC
227/**
228 * loop_set_size() - sets device size and notifies userspace
229 * @lo: struct loop_device to set the size for
230 * @size: new size of the loop device
231 *
232 * Callers must validate that the size passed into this function fits into
233 * a sector_t, eg using loop_validate_size()
234 */
235static void loop_set_size(struct loop_device *lo, loff_t size)
236{
449f4ec9 237 if (!set_capacity_and_notify(lo->lo_disk, size))
3b4f85d0 238 kobject_uevent(&disk_to_dev(lo->lo_disk)->kobj, KOBJ_CHANGE);
5795b6f5
MC
239}
240
aa4d8616 241static int lo_write_bvec(struct file *file, struct bio_vec *bvec, loff_t *ppos)
1da177e4 242{
aa4d8616 243 struct iov_iter i;
1da177e4 244 ssize_t bw;
283e7e5d 245
de4eda9d 246 iov_iter_bvec(&i, ITER_SOURCE, bvec, 1, bvec->bv_len);
1da177e4 247
abbb6589 248 bw = vfs_iter_write(file, &i, ppos, 0);
aa4d8616
CH
249
250 if (likely(bw == bvec->bv_len))
1da177e4 251 return 0;
aa4d8616
CH
252
253 printk_ratelimited(KERN_ERR
254 "loop: Write error at byte offset %llu, length %i.\n",
255 (unsigned long long)*ppos, bvec->bv_len);
1da177e4
LT
256 if (bw >= 0)
257 bw = -EIO;
258 return bw;
259}
260
aa4d8616
CH
261static int lo_write_simple(struct loop_device *lo, struct request *rq,
262 loff_t pos)
1da177e4 263{
aa4d8616
CH
264 struct bio_vec bvec;
265 struct req_iterator iter;
266 int ret = 0;
267
268 rq_for_each_segment(bvec, rq, iter) {
269 ret = lo_write_bvec(lo->lo_backing_file, &bvec, &pos);
270 if (ret < 0)
271 break;
272 cond_resched();
273 }
274
275 return ret;
1da177e4
LT
276}
277
aa4d8616
CH
278static int lo_read_simple(struct loop_device *lo, struct request *rq,
279 loff_t pos)
1da177e4 280{
aa4d8616
CH
281 struct bio_vec bvec;
282 struct req_iterator iter;
283 struct iov_iter i;
284 ssize_t len;
1da177e4 285
aa4d8616 286 rq_for_each_segment(bvec, rq, iter) {
de4eda9d 287 iov_iter_bvec(&i, ITER_DEST, &bvec, 1, bvec.bv_len);
18e9710e 288 len = vfs_iter_read(lo->lo_backing_file, &i, &pos, 0);
aa4d8616
CH
289 if (len < 0)
290 return len;
1da177e4 291
aa4d8616 292 flush_dcache_page(bvec.bv_page);
fd582140 293
aa4d8616
CH
294 if (len != bvec.bv_len) {
295 struct bio *bio;
1da177e4 296
aa4d8616
CH
297 __rq_for_each_bio(bio, rq)
298 zero_fill_bio(bio);
299 break;
300 }
301 cond_resched();
302 }
303
304 return 0;
fd582140
JA
305}
306
efcfec57
DW
307static int lo_fallocate(struct loop_device *lo, struct request *rq, loff_t pos,
308 int mode)
cf655d95
ML
309{
310 /*
efcfec57 311 * We use fallocate to manipulate the space mappings used by the image
47e96246 312 * a.k.a. discard/zerorange.
cf655d95
ML
313 */
314 struct file *file = lo->lo_backing_file;
cf655d95
ML
315 int ret;
316
efcfec57
DW
317 mode |= FALLOC_FL_KEEP_SIZE;
318
70200574
CH
319 if (!bdev_max_discard_sectors(lo->lo_device))
320 return -EOPNOTSUPP;
cf655d95
ML
321
322 ret = file->f_op->fallocate(file, mode, pos, blk_rq_bytes(rq));
323 if (unlikely(ret && ret != -EINVAL && ret != -EOPNOTSUPP))
70200574 324 return -EIO;
cf655d95
ML
325 return ret;
326}
327
328static int lo_req_flush(struct loop_device *lo, struct request *rq)
329{
9c64e38c 330 int ret = vfs_fsync(lo->lo_backing_file, 0);
cf655d95
ML
331 if (unlikely(ret && ret != -EINVAL))
332 ret = -EIO;
333
334 return ret;
335}
336
fe2cb290 337static void lo_complete_rq(struct request *rq)
bc07c10a 338{
fe2cb290 339 struct loop_cmd *cmd = blk_mq_rq_to_pdu(rq);
f9de14bc 340 blk_status_t ret = BLK_STS_OK;
bc07c10a 341
f9de14bc
JA
342 if (!cmd->use_aio || cmd->ret < 0 || cmd->ret == blk_rq_bytes(rq) ||
343 req_op(rq) != REQ_OP_READ) {
344 if (cmd->ret < 0)
8cd55087 345 ret = errno_to_blk_status(cmd->ret);
f9de14bc 346 goto end_io;
bc07c10a 347 }
fe2cb290 348
f9de14bc
JA
349 /*
350 * Short READ - if we got some data, advance our request and
351 * retry it. If we got no data, end the rest with EIO.
352 */
353 if (cmd->ret) {
354 blk_update_request(rq, BLK_STS_OK, cmd->ret);
355 cmd->ret = 0;
356 blk_mq_requeue_request(rq, true);
357 } else {
358 if (cmd->use_aio) {
359 struct bio *bio = rq->bio;
360
361 while (bio) {
362 zero_fill_bio(bio);
363 bio = bio->bi_next;
364 }
365 }
366 ret = BLK_STS_IOERR;
367end_io:
368 blk_mq_end_request(rq, ret);
369 }
bc07c10a
ML
370}
371
92d77332
SL
372static void lo_rw_aio_do_completion(struct loop_cmd *cmd)
373{
1894e916
JA
374 struct request *rq = blk_mq_rq_from_pdu(cmd);
375
92d77332
SL
376 if (!atomic_dec_and_test(&cmd->ref))
377 return;
378 kfree(cmd->bvec);
379 cmd->bvec = NULL;
15f73f5b
CH
380 if (likely(!blk_should_fake_timeout(rq->q)))
381 blk_mq_complete_request(rq);
92d77332
SL
382}
383
6b19b766 384static void lo_rw_aio_complete(struct kiocb *iocb, long ret)
bc07c10a
ML
385{
386 struct loop_cmd *cmd = container_of(iocb, struct loop_cmd, iocb);
bc07c10a 387
fe2cb290 388 cmd->ret = ret;
92d77332 389 lo_rw_aio_do_completion(cmd);
bc07c10a
ML
390}
391
392static int lo_rw_aio(struct loop_device *lo, struct loop_cmd *cmd,
de4eda9d 393 loff_t pos, int rw)
bc07c10a
ML
394{
395 struct iov_iter iter;
86af5952 396 struct req_iterator rq_iter;
bc07c10a 397 struct bio_vec *bvec;
1894e916 398 struct request *rq = blk_mq_rq_from_pdu(cmd);
40326d8a 399 struct bio *bio = rq->bio;
bc07c10a 400 struct file *file = lo->lo_backing_file;
86af5952 401 struct bio_vec tmp;
40326d8a 402 unsigned int offset;
86af5952 403 int nr_bvec = 0;
bc07c10a
ML
404 int ret;
405
86af5952
ML
406 rq_for_each_bvec(tmp, rq, rq_iter)
407 nr_bvec++;
408
40326d8a 409 if (rq->bio != rq->biotail) {
40326d8a 410
86af5952 411 bvec = kmalloc_array(nr_bvec, sizeof(struct bio_vec),
6da2ec56 412 GFP_NOIO);
40326d8a
SL
413 if (!bvec)
414 return -EIO;
415 cmd->bvec = bvec;
416
417 /*
418 * The bios of the request may be started from the middle of
419 * the 'bvec' because of bio splitting, so we can't directly
86af5952 420 * copy bio->bi_iov_vec to new bvec. The rq_for_each_bvec
40326d8a
SL
421 * API will take care of all details for us.
422 */
86af5952 423 rq_for_each_bvec(tmp, rq, rq_iter) {
40326d8a
SL
424 *bvec = tmp;
425 bvec++;
426 }
427 bvec = cmd->bvec;
428 offset = 0;
429 } else {
430 /*
431 * Same here, this bio may be started from the middle of the
432 * 'bvec' because of bio splitting, so offset from the bvec
433 * must be passed to iov iterator
434 */
435 offset = bio->bi_iter.bi_bvec_done;
436 bvec = __bvec_iter_bvec(bio->bi_io_vec, bio->bi_iter);
40326d8a 437 }
92d77332 438 atomic_set(&cmd->ref, 2);
bc07c10a 439
b6207430 440 iov_iter_bvec(&iter, rw, bvec, nr_bvec, blk_rq_bytes(rq));
40326d8a 441 iter.iov_offset = offset;
bc07c10a
ML
442
443 cmd->iocb.ki_pos = pos;
444 cmd->iocb.ki_filp = file;
445 cmd->iocb.ki_complete = lo_rw_aio_complete;
446 cmd->iocb.ki_flags = IOCB_DIRECT;
d9a08a9e 447 cmd->iocb.ki_ioprio = IOPRIO_PRIO_VALUE(IOPRIO_CLASS_NONE, 0);
bc07c10a 448
de4eda9d 449 if (rw == ITER_SOURCE)
bb7462b6 450 ret = call_write_iter(file, &cmd->iocb, &iter);
bc07c10a 451 else
bb7462b6 452 ret = call_read_iter(file, &cmd->iocb, &iter);
bc07c10a 453
92d77332
SL
454 lo_rw_aio_do_completion(cmd);
455
bc07c10a 456 if (ret != -EIOCBQUEUED)
6b19b766 457 lo_rw_aio_complete(&cmd->iocb, ret);
bc07c10a
ML
458 return 0;
459}
460
c1c87c2b 461static int do_req_filebacked(struct loop_device *lo, struct request *rq)
bc07c10a
ML
462{
463 struct loop_cmd *cmd = blk_mq_rq_to_pdu(rq);
c1c87c2b 464 loff_t pos = ((loff_t) blk_rq_pos(rq) << 9) + lo->lo_offset;
bc07c10a
ML
465
466 /*
467 * lo_write_simple and lo_read_simple should have been covered
468 * by io submit style function like lo_rw_aio(), one blocker
469 * is that lo_read_simple() need to call flush_dcache_page after
470 * the page is written from kernel, and it isn't easy to handle
471 * this in io submit style function which submits all segments
472 * of the req at one time. And direct read IO doesn't need to
473 * run flush_dcache_page().
474 */
c1c87c2b
CH
475 switch (req_op(rq)) {
476 case REQ_OP_FLUSH:
477 return lo_req_flush(lo, rq);
19372e27 478 case REQ_OP_WRITE_ZEROES:
efcfec57
DW
479 /*
480 * If the caller doesn't want deallocation, call zeroout to
481 * write zeroes the range. Otherwise, punch them out.
482 */
483 return lo_fallocate(lo, rq, pos,
484 (rq->cmd_flags & REQ_NOUNMAP) ?
485 FALLOC_FL_ZERO_RANGE :
486 FALLOC_FL_PUNCH_HOLE);
487 case REQ_OP_DISCARD:
488 return lo_fallocate(lo, rq, pos, FALLOC_FL_PUNCH_HOLE);
c1c87c2b 489 case REQ_OP_WRITE:
47e96246 490 if (cmd->use_aio)
de4eda9d 491 return lo_rw_aio(lo, cmd, pos, ITER_SOURCE);
af65aa8e 492 else
c1c87c2b
CH
493 return lo_write_simple(lo, rq, pos);
494 case REQ_OP_READ:
47e96246 495 if (cmd->use_aio)
de4eda9d 496 return lo_rw_aio(lo, cmd, pos, ITER_DEST);
aa4d8616 497 else
c1c87c2b
CH
498 return lo_read_simple(lo, rq, pos);
499 default:
500 WARN_ON_ONCE(1);
501 return -EIO;
aa4d8616 502 }
1da177e4
LT
503}
504
2e5ab5f3
ML
505static inline void loop_update_dio(struct loop_device *lo)
506{
efbe3c24
IW
507 __loop_update_dio(lo, (lo->lo_backing_file->f_flags & O_DIRECT) |
508 lo->use_dio);
2e5ab5f3
ML
509}
510
0384264e 511static void loop_reread_partitions(struct loop_device *lo)
06f0e9e6
ML
512{
513 int rc;
514
0384264e
CH
515 mutex_lock(&lo->lo_disk->open_mutex);
516 rc = bdev_disk_changed(lo->lo_disk, false);
517 mutex_unlock(&lo->lo_disk->open_mutex);
06f0e9e6
ML
518 if (rc)
519 pr_warn("%s: partition scan of loop%d (%s) failed (rc=%d)\n",
520 __func__, lo->lo_number, lo->lo_file_name, rc);
521}
522
d2ac838e
TT
523static inline int is_loop_device(struct file *file)
524{
525 struct inode *i = file->f_mapping->host;
526
6f24784f 527 return i && S_ISBLK(i->i_mode) && imajor(i) == LOOP_MAJOR;
d2ac838e
TT
528}
529
530static int loop_validate_file(struct file *file, struct block_device *bdev)
531{
532 struct inode *inode = file->f_mapping->host;
533 struct file *f = file;
534
535 /* Avoid recursion */
536 while (is_loop_device(f)) {
537 struct loop_device *l;
538
3ce6e1f6 539 lockdep_assert_held(&loop_validate_mutex);
4e7b5671 540 if (f->f_mapping->host->i_rdev == bdev->bd_dev)
d2ac838e
TT
541 return -EBADF;
542
4e7b5671 543 l = I_BDEV(f->f_mapping->host)->bd_disk->private_data;
3ce6e1f6 544 if (l->lo_state != Lo_bound)
d2ac838e 545 return -EINVAL;
3ce6e1f6
TH
546 /* Order wrt setting lo->lo_backing_file in loop_configure(). */
547 rmb();
d2ac838e
TT
548 f = l->lo_backing_file;
549 }
550 if (!S_ISREG(inode->i_mode) && !S_ISBLK(inode->i_mode))
551 return -EINVAL;
552 return 0;
553}
554
1da177e4
LT
555/*
556 * loop_change_fd switched the backing store of a loopback device to
557 * a new file. This is useful for operating system installers to free up
558 * the original file and in High Availability environments to switch to
559 * an alternative location for the content in case of server meltdown.
560 * This can only work if the loop device is used read-only, and if the
561 * new backing store is the same size and type as the old backing store.
562 */
bb214884
AV
563static int loop_change_fd(struct loop_device *lo, struct block_device *bdev,
564 unsigned int arg)
1da177e4 565{
3ce6e1f6
TH
566 struct file *file = fget(arg);
567 struct file *old_file;
568 int error;
569 bool partscan;
570 bool is_loop;
1da177e4 571
3ce6e1f6
TH
572 if (!file)
573 return -EBADF;
498ef5c7
CH
574
575 /* suppress uevents while reconfiguring the device */
576 dev_set_uevent_suppress(disk_to_dev(lo->lo_disk), 1);
577
3ce6e1f6
TH
578 is_loop = is_loop_device(file);
579 error = loop_global_lock_killable(lo, is_loop);
c3710770 580 if (error)
3ce6e1f6 581 goto out_putf;
1da177e4
LT
582 error = -ENXIO;
583 if (lo->lo_state != Lo_bound)
1dded9ac 584 goto out_err;
1da177e4
LT
585
586 /* the loop device has to be read-only */
587 error = -EINVAL;
588 if (!(lo->lo_flags & LO_FLAGS_READ_ONLY))
1dded9ac 589 goto out_err;
1da177e4 590
d2ac838e
TT
591 error = loop_validate_file(file, bdev);
592 if (error)
1dded9ac 593 goto out_err;
d2ac838e 594
1da177e4
LT
595 old_file = lo->lo_backing_file;
596
597 error = -EINVAL;
598
1da177e4
LT
599 /* size of the new backing store needs to be the same */
600 if (get_loop_size(lo, file) != get_loop_size(lo, old_file))
1dded9ac 601 goto out_err;
1da177e4
LT
602
603 /* and ... switch */
ab6860f6 604 disk_force_media_change(lo->lo_disk);
43cade80
OS
605 blk_mq_freeze_queue(lo->lo_queue);
606 mapping_set_gfp_mask(old_file->f_mapping, lo->old_gfp_mask);
607 lo->lo_backing_file = file;
608 lo->old_gfp_mask = mapping_gfp_mask(file->f_mapping);
609 mapping_set_gfp_mask(file->f_mapping,
610 lo->old_gfp_mask & ~(__GFP_IO|__GFP_FS));
611 loop_update_dio(lo);
612 blk_mq_unfreeze_queue(lo->lo_queue);
85b0a54a 613 partscan = lo->lo_flags & LO_FLAGS_PARTSCAN;
3ce6e1f6
TH
614 loop_global_unlock(lo, is_loop);
615
616 /*
617 * Flush loop_validate_file() before fput(), for l->lo_backing_file
618 * might be pointing at old_file which might be the last reference.
619 */
620 if (!is_loop) {
621 mutex_lock(&loop_validate_mutex);
622 mutex_unlock(&loop_validate_mutex);
623 }
1dded9ac 624 /*
6cc8e743 625 * We must drop file reference outside of lo_mutex as dropping
a8698707 626 * the file ref can take open_mutex which creates circular locking
1dded9ac
JK
627 * dependency.
628 */
629 fput(old_file);
85b0a54a 630 if (partscan)
0384264e 631 loop_reread_partitions(lo);
498ef5c7
CH
632
633 error = 0;
634done:
635 /* enable and uncork uevent now that we are done */
636 dev_set_uevent_suppress(disk_to_dev(lo->lo_disk), 0);
637 return error;
1da177e4 638
1dded9ac 639out_err:
3ce6e1f6
TH
640 loop_global_unlock(lo, is_loop);
641out_putf:
642 fput(file);
498ef5c7 643 goto done;
1da177e4
LT
644}
645
ee862730
MB
646/* loop sysfs attributes */
647
648static ssize_t loop_attr_show(struct device *dev, char *page,
649 ssize_t (*callback)(struct loop_device *, char *))
650{
34dd82af
KS
651 struct gendisk *disk = dev_to_disk(dev);
652 struct loop_device *lo = disk->private_data;
ee862730 653
34dd82af 654 return callback(lo, page);
ee862730
MB
655}
656
657#define LOOP_ATTR_RO(_name) \
658static ssize_t loop_attr_##_name##_show(struct loop_device *, char *); \
659static ssize_t loop_attr_do_show_##_name(struct device *d, \
660 struct device_attribute *attr, char *b) \
661{ \
662 return loop_attr_show(d, b, loop_attr_##_name##_show); \
663} \
664static struct device_attribute loop_attr_##_name = \
5657a819 665 __ATTR(_name, 0444, loop_attr_do_show_##_name, NULL);
ee862730
MB
666
667static ssize_t loop_attr_backing_file_show(struct loop_device *lo, char *buf)
668{
669 ssize_t ret;
670 char *p = NULL;
671
05eb0f25 672 spin_lock_irq(&lo->lo_lock);
ee862730 673 if (lo->lo_backing_file)
9bf39ab2 674 p = file_path(lo->lo_backing_file, buf, PAGE_SIZE - 1);
05eb0f25 675 spin_unlock_irq(&lo->lo_lock);
ee862730
MB
676
677 if (IS_ERR_OR_NULL(p))
678 ret = PTR_ERR(p);
679 else {
680 ret = strlen(p);
681 memmove(buf, p, ret);
682 buf[ret++] = '\n';
683 buf[ret] = 0;
684 }
685
686 return ret;
687}
688
689static ssize_t loop_attr_offset_show(struct loop_device *lo, char *buf)
690{
b27824d3 691 return sysfs_emit(buf, "%llu\n", (unsigned long long)lo->lo_offset);
ee862730
MB
692}
693
694static ssize_t loop_attr_sizelimit_show(struct loop_device *lo, char *buf)
695{
b27824d3 696 return sysfs_emit(buf, "%llu\n", (unsigned long long)lo->lo_sizelimit);
ee862730
MB
697}
698
699static ssize_t loop_attr_autoclear_show(struct loop_device *lo, char *buf)
700{
701 int autoclear = (lo->lo_flags & LO_FLAGS_AUTOCLEAR);
702
b27824d3 703 return sysfs_emit(buf, "%s\n", autoclear ? "1" : "0");
ee862730
MB
704}
705
e03c8dd1
KS
706static ssize_t loop_attr_partscan_show(struct loop_device *lo, char *buf)
707{
708 int partscan = (lo->lo_flags & LO_FLAGS_PARTSCAN);
709
b27824d3 710 return sysfs_emit(buf, "%s\n", partscan ? "1" : "0");
e03c8dd1
KS
711}
712
2e5ab5f3
ML
713static ssize_t loop_attr_dio_show(struct loop_device *lo, char *buf)
714{
715 int dio = (lo->lo_flags & LO_FLAGS_DIRECT_IO);
716
b27824d3 717 return sysfs_emit(buf, "%s\n", dio ? "1" : "0");
2e5ab5f3
ML
718}
719
ee862730
MB
720LOOP_ATTR_RO(backing_file);
721LOOP_ATTR_RO(offset);
722LOOP_ATTR_RO(sizelimit);
723LOOP_ATTR_RO(autoclear);
e03c8dd1 724LOOP_ATTR_RO(partscan);
2e5ab5f3 725LOOP_ATTR_RO(dio);
ee862730
MB
726
727static struct attribute *loop_attrs[] = {
728 &loop_attr_backing_file.attr,
729 &loop_attr_offset.attr,
730 &loop_attr_sizelimit.attr,
731 &loop_attr_autoclear.attr,
e03c8dd1 732 &loop_attr_partscan.attr,
2e5ab5f3 733 &loop_attr_dio.attr,
ee862730
MB
734 NULL,
735};
736
737static struct attribute_group loop_attribute_group = {
738 .name = "loop",
739 .attrs= loop_attrs,
740};
741
d3349b6b 742static void loop_sysfs_init(struct loop_device *lo)
ee862730 743{
d3349b6b
TH
744 lo->sysfs_inited = !sysfs_create_group(&disk_to_dev(lo->lo_disk)->kobj,
745 &loop_attribute_group);
ee862730
MB
746}
747
748static void loop_sysfs_exit(struct loop_device *lo)
749{
d3349b6b
TH
750 if (lo->sysfs_inited)
751 sysfs_remove_group(&disk_to_dev(lo->lo_disk)->kobj,
752 &loop_attribute_group);
ee862730
MB
753}
754
dfaa2ef6
LC
755static void loop_config_discard(struct loop_device *lo)
756{
757 struct file *file = lo->lo_backing_file;
758 struct inode *inode = file->f_mapping->host;
759 struct request_queue *q = lo->lo_queue;
bcb21c8c 760 u32 granularity, max_discard_sectors;
dfaa2ef6 761
c52abf56
EG
762 /*
763 * If the backing device is a block device, mirror its zeroing
764 * capability. Set the discard sectors to the block device's zeroing
765 * capabilities because loop discards result in blkdev_issue_zeroout(),
766 * not blkdev_issue_discard(). This maintains consistent behavior with
767 * file-backed loop devices: discarded regions read back as zero.
768 */
47e96246 769 if (S_ISBLK(inode->i_mode)) {
4e7b5671 770 struct request_queue *backingq = bdev_get_queue(I_BDEV(inode));
c52abf56 771
bcb21c8c 772 max_discard_sectors = backingq->limits.max_write_zeroes_sectors;
7b47ef52 773 granularity = bdev_discard_granularity(I_BDEV(inode)) ?:
bcb21c8c 774 queue_physical_block_size(backingq);
c52abf56 775
dfaa2ef6
LC
776 /*
777 * We use punch hole to reclaim the free space used by the
47e96246 778 * image a.k.a. discard.
dfaa2ef6 779 */
47e96246 780 } else if (!file->f_op->fallocate) {
bcb21c8c
ML
781 max_discard_sectors = 0;
782 granularity = 0;
dfaa2ef6 783
c52abf56 784 } else {
06582bc8
ML
785 struct kstatfs sbuf;
786
bcb21c8c 787 max_discard_sectors = UINT_MAX >> 9;
06582bc8
ML
788 if (!vfs_statfs(&file->f_path, &sbuf))
789 granularity = sbuf.f_bsize;
790 else
791 max_discard_sectors = 0;
c52abf56
EG
792 }
793
bcb21c8c
ML
794 if (max_discard_sectors) {
795 q->limits.discard_granularity = granularity;
796 blk_queue_max_discard_sectors(q, max_discard_sectors);
797 blk_queue_max_write_zeroes_sectors(q, max_discard_sectors);
bcb21c8c
ML
798 } else {
799 q->limits.discard_granularity = 0;
800 blk_queue_max_discard_sectors(q, 0);
801 blk_queue_max_write_zeroes_sectors(q, 0);
bcb21c8c 802 }
dfaa2ef6
LC
803}
804
87579e9b
DS
805struct loop_worker {
806 struct rb_node rb_node;
807 struct work_struct work;
808 struct list_head cmd_list;
809 struct list_head idle_list;
810 struct loop_device *lo;
c74d40e8 811 struct cgroup_subsys_state *blkcg_css;
87579e9b
DS
812 unsigned long last_ran_at;
813};
814
815static void loop_workfn(struct work_struct *work);
87579e9b
DS
816
817#ifdef CONFIG_BLK_CGROUP
818static inline int queue_on_root_worker(struct cgroup_subsys_state *css)
e03a3d7a 819{
87579e9b 820 return !css || css == blkcg_root_css;
e03a3d7a 821}
87579e9b
DS
822#else
823static inline int queue_on_root_worker(struct cgroup_subsys_state *css)
b2ee7d46 824{
87579e9b 825 return !css;
b2ee7d46 826}
87579e9b 827#endif
b2ee7d46 828
87579e9b 829static void loop_queue_work(struct loop_device *lo, struct loop_cmd *cmd)
e03a3d7a 830{
413ec805 831 struct rb_node **node, *parent = NULL;
87579e9b
DS
832 struct loop_worker *cur_worker, *worker = NULL;
833 struct work_struct *work;
834 struct list_head *cmd_list;
835
836 spin_lock_irq(&lo->lo_work_lock);
837
c74d40e8 838 if (queue_on_root_worker(cmd->blkcg_css))
87579e9b
DS
839 goto queue_work;
840
841 node = &lo->worker_tree.rb_node;
842
843 while (*node) {
844 parent = *node;
845 cur_worker = container_of(*node, struct loop_worker, rb_node);
c74d40e8 846 if (cur_worker->blkcg_css == cmd->blkcg_css) {
87579e9b
DS
847 worker = cur_worker;
848 break;
c74d40e8 849 } else if ((long)cur_worker->blkcg_css < (long)cmd->blkcg_css) {
87579e9b
DS
850 node = &(*node)->rb_left;
851 } else {
852 node = &(*node)->rb_right;
853 }
854 }
855 if (worker)
856 goto queue_work;
857
858 worker = kzalloc(sizeof(struct loop_worker), GFP_NOWAIT | __GFP_NOWARN);
859 /*
860 * In the event we cannot allocate a worker, just queue on the
c74d40e8 861 * rootcg worker and issue the I/O as the rootcg
87579e9b 862 */
c74d40e8
DS
863 if (!worker) {
864 cmd->blkcg_css = NULL;
865 if (cmd->memcg_css)
866 css_put(cmd->memcg_css);
867 cmd->memcg_css = NULL;
87579e9b 868 goto queue_work;
c74d40e8 869 }
87579e9b 870
c74d40e8
DS
871 worker->blkcg_css = cmd->blkcg_css;
872 css_get(worker->blkcg_css);
87579e9b
DS
873 INIT_WORK(&worker->work, loop_workfn);
874 INIT_LIST_HEAD(&worker->cmd_list);
875 INIT_LIST_HEAD(&worker->idle_list);
876 worker->lo = lo;
877 rb_link_node(&worker->rb_node, parent, node);
878 rb_insert_color(&worker->rb_node, &lo->worker_tree);
879queue_work:
880 if (worker) {
881 /*
882 * We need to remove from the idle list here while
883 * holding the lock so that the idle timer doesn't
884 * free the worker
885 */
886 if (!list_empty(&worker->idle_list))
887 list_del_init(&worker->idle_list);
888 work = &worker->work;
889 cmd_list = &worker->cmd_list;
890 } else {
891 work = &lo->rootcg_work;
892 cmd_list = &lo->rootcg_cmd_list;
893 }
894 list_add_tail(&cmd->list_entry, cmd_list);
895 queue_work(lo->workqueue, work);
896 spin_unlock_irq(&lo->lo_work_lock);
e03a3d7a
ML
897}
898
2cf429b5
CH
899static void loop_set_timer(struct loop_device *lo)
900{
901 timer_reduce(&lo->timer, jiffies + LOOP_IDLE_WORKER_TIMEOUT);
902}
903
904static void loop_free_idle_workers(struct loop_device *lo, bool delete_all)
905{
906 struct loop_worker *pos, *worker;
907
908 spin_lock_irq(&lo->lo_work_lock);
909 list_for_each_entry_safe(worker, pos, &lo->idle_worker_list,
910 idle_list) {
911 if (!delete_all &&
912 time_is_after_jiffies(worker->last_ran_at +
913 LOOP_IDLE_WORKER_TIMEOUT))
914 break;
915 list_del(&worker->idle_list);
916 rb_erase(&worker->rb_node, &lo->worker_tree);
917 css_put(worker->blkcg_css);
918 kfree(worker);
919 }
920 if (!list_empty(&lo->idle_worker_list))
921 loop_set_timer(lo);
922 spin_unlock_irq(&lo->lo_work_lock);
923}
924
925static void loop_free_idle_workers_timer(struct timer_list *timer)
926{
927 struct loop_device *lo = container_of(timer, struct loop_device, timer);
928
929 return loop_free_idle_workers(lo, false);
930}
931
56a85fd8
HH
932static void loop_update_rotational(struct loop_device *lo)
933{
934 struct file *file = lo->lo_backing_file;
935 struct inode *file_inode = file->f_mapping->host;
936 struct block_device *file_bdev = file_inode->i_sb->s_bdev;
937 struct request_queue *q = lo->lo_queue;
938 bool nonrot = true;
939
940 /* not all filesystems (e.g. tmpfs) have a sb->s_bdev */
941 if (file_bdev)
10f0d2a5 942 nonrot = bdev_nonrot(file_bdev);
56a85fd8
HH
943
944 if (nonrot)
945 blk_queue_flag_set(QUEUE_FLAG_NONROT, q);
946 else
947 blk_queue_flag_clear(QUEUE_FLAG_NONROT, q);
948}
949
62ab466c
MC
950/**
951 * loop_set_status_from_info - configure device from loop_info
952 * @lo: struct loop_device to configure
953 * @info: struct loop_info64 to configure the device with
954 *
955 * Configures the loop device parameters according to the passed
956 * in loop_info64 configuration.
957 */
958static int
959loop_set_status_from_info(struct loop_device *lo,
960 const struct loop_info64 *info)
961{
62ab466c
MC
962 if ((unsigned int) info->lo_encrypt_key_size > LO_KEY_SIZE)
963 return -EINVAL;
964
47e96246
CH
965 switch (info->lo_encrypt_type) {
966 case LO_CRYPT_NONE:
967 break;
968 case LO_CRYPT_XOR:
969 pr_warn("support for the xor transformation has been removed.\n");
970 return -EINVAL;
971 case LO_CRYPT_CRYPTOAPI:
972 pr_warn("support for cryptoloop has been removed. Use dm-crypt instead.\n");
973 return -EINVAL;
974 default:
975 return -EINVAL;
976 }
62ab466c 977
9f6ad5d5
ZJ
978 /* Avoid assigning overflow values */
979 if (info->lo_offset > LLONG_MAX || info->lo_sizelimit > LLONG_MAX)
980 return -EOVERFLOW;
981
62ab466c
MC
982 lo->lo_offset = info->lo_offset;
983 lo->lo_sizelimit = info->lo_sizelimit;
c490a0b5 984
62ab466c 985 memcpy(lo->lo_file_name, info->lo_file_name, LO_NAME_SIZE);
62ab466c 986 lo->lo_file_name[LO_NAME_SIZE-1] = 0;
faf1d254 987 lo->lo_flags = info->lo_flags;
62ab466c
MC
988 return 0;
989}
990
05bdb996 991static int loop_configure(struct loop_device *lo, blk_mode_t mode,
3448914e
MC
992 struct block_device *bdev,
993 const struct loop_config *config)
1da177e4 994{
3ce6e1f6
TH
995 struct file *file = fget(config->fd);
996 struct inode *inode;
1da177e4 997 struct address_space *mapping;
3ce6e1f6
TH
998 int error;
999 loff_t size;
1000 bool partscan;
1001 unsigned short bsize;
1002 bool is_loop;
1003
1004 if (!file)
1005 return -EBADF;
1006 is_loop = is_loop_device(file);
1da177e4
LT
1007
1008 /* This is safe, since we have a reference from open(). */
1009 __module_get(THIS_MODULE);
1010
33ec3e53
JK
1011 /*
1012 * If we don't hold exclusive handle for the device, upgrade to it
1013 * here to avoid changing device under exclusive owner.
1014 */
05bdb996 1015 if (!(mode & BLK_OPEN_EXCL)) {
0718afd4 1016 error = bd_prepare_to_claim(bdev, loop_configure, NULL);
ecbe6bc0 1017 if (error)
33ec3e53
JK
1018 goto out_putf;
1019 }
1020
3ce6e1f6 1021 error = loop_global_lock_killable(lo, is_loop);
757ecf40 1022 if (error)
33ec3e53 1023 goto out_bdev;
757ecf40 1024
1da177e4
LT
1025 error = -EBUSY;
1026 if (lo->lo_state != Lo_unbound)
757ecf40 1027 goto out_unlock;
1da177e4 1028
d2ac838e
TT
1029 error = loop_validate_file(file, bdev);
1030 if (error)
757ecf40 1031 goto out_unlock;
1da177e4
LT
1032
1033 mapping = file->f_mapping;
1034 inode = mapping->host;
1035
3448914e
MC
1036 if ((config->info.lo_flags & ~LOOP_CONFIGURE_SETTABLE_FLAGS) != 0) {
1037 error = -EINVAL;
1038 goto out_unlock;
1039 }
1040
1041 if (config->block_size) {
af3c570f 1042 error = blk_validate_block_size(config->block_size);
3448914e
MC
1043 if (error)
1044 goto out_unlock;
1045 }
1046
1047 error = loop_set_status_from_info(lo, &config->info);
1048 if (error)
1049 goto out_unlock;
1050
05bdb996 1051 if (!(file->f_mode & FMODE_WRITE) || !(mode & BLK_OPEN_WRITE) ||
283e7e5d 1052 !file->f_op->write_iter)
3448914e 1053 lo->lo_flags |= LO_FLAGS_READ_ONLY;
ba52de12 1054
87579e9b 1055 if (!lo->workqueue) {
d292dc80
CH
1056 lo->workqueue = alloc_workqueue("loop%d",
1057 WQ_UNBOUND | WQ_FREEZABLE,
1058 0, lo->lo_number);
1059 if (!lo->workqueue) {
1060 error = -ENOMEM;
1061 goto out_unlock;
1062 }
87579e9b 1063 }
1da177e4 1064
bb430b69
AR
1065 /* suppress uevents while reconfiguring the device */
1066 dev_set_uevent_suppress(disk_to_dev(lo->lo_disk), 1);
1067
ab6860f6 1068 disk_force_media_change(lo->lo_disk);
7a2f0ce1 1069 set_disk_ro(lo->lo_disk, (lo->lo_flags & LO_FLAGS_READ_ONLY) != 0);
1da177e4 1070
3448914e 1071 lo->use_dio = lo->lo_flags & LO_FLAGS_DIRECT_IO;
1da177e4 1072 lo->lo_device = bdev;
1da177e4 1073 lo->lo_backing_file = file;
1da177e4
LT
1074 lo->old_gfp_mask = mapping_gfp_mask(mapping);
1075 mapping_set_gfp_mask(mapping, lo->old_gfp_mask & ~(__GFP_IO|__GFP_FS));
1076
3448914e 1077 if (!(lo->lo_flags & LO_FLAGS_READ_ONLY) && file->f_op->fsync)
21d0727f 1078 blk_queue_write_cache(lo->lo_queue, true, false);
68db1961 1079
3448914e
MC
1080 if (config->block_size)
1081 bsize = config->block_size;
96ed320d 1082 else if ((lo->lo_backing_file->f_flags & O_DIRECT) && inode->i_sb->s_bdev)
85560117 1083 /* In case of direct I/O, match underlying block size */
3448914e
MC
1084 bsize = bdev_logical_block_size(inode->i_sb->s_bdev);
1085 else
1086 bsize = 512;
85560117 1087
3448914e
MC
1088 blk_queue_logical_block_size(lo->lo_queue, bsize);
1089 blk_queue_physical_block_size(lo->lo_queue, bsize);
1090 blk_queue_io_min(lo->lo_queue, bsize);
85560117 1091
2b9ac22b 1092 loop_config_discard(lo);
56a85fd8 1093 loop_update_rotational(lo);
2e5ab5f3 1094 loop_update_dio(lo);
ee862730 1095 loop_sysfs_init(lo);
79e5dc59
MC
1096
1097 size = get_loop_size(lo, file);
5795b6f5 1098 loop_set_size(lo, size);
1da177e4 1099
3ce6e1f6
TH
1100 /* Order wrt reading lo_state in loop_validate_file(). */
1101 wmb();
1102
6c997918 1103 lo->lo_state = Lo_bound;
e03c8dd1
KS
1104 if (part_shift)
1105 lo->lo_flags |= LO_FLAGS_PARTSCAN;
85b0a54a 1106 partscan = lo->lo_flags & LO_FLAGS_PARTSCAN;
fe6a8fc5 1107 if (partscan)
b9684a71 1108 clear_bit(GD_SUPPRESS_PART_SCAN, &lo->lo_disk->state);
c1681bf8 1109
bb430b69
AR
1110 /* enable and uncork uevent now that we are done */
1111 dev_set_uevent_suppress(disk_to_dev(lo->lo_disk), 0);
1112
3ce6e1f6 1113 loop_global_unlock(lo, is_loop);
85b0a54a 1114 if (partscan)
0384264e 1115 loop_reread_partitions(lo);
bb430b69 1116
05bdb996 1117 if (!(mode & BLK_OPEN_EXCL))
37c3fc9a 1118 bd_abort_claiming(bdev, loop_configure);
498ef5c7 1119
bb430b69 1120 return 0;
1da177e4 1121
757ecf40 1122out_unlock:
3ce6e1f6 1123 loop_global_unlock(lo, is_loop);
33ec3e53 1124out_bdev:
05bdb996 1125 if (!(mode & BLK_OPEN_EXCL))
37c3fc9a 1126 bd_abort_claiming(bdev, loop_configure);
757ecf40 1127out_putf:
1da177e4 1128 fput(file);
1da177e4
LT
1129 /* This is safe: open() is still holding a reference. */
1130 module_put(THIS_MODULE);
bb430b69 1131 return error;
1da177e4
LT
1132}
1133
bf23747e 1134static void __loop_clr_fd(struct loop_device *lo, bool release)
1da177e4 1135{
6050fa4c 1136 struct file *filp;
b4e3ca1a 1137 gfp_t gfp = lo->old_gfp_mask;
1da177e4 1138
4ceddce5
MFO
1139 if (test_bit(QUEUE_FLAG_WC, &lo->lo_queue->queue_flags))
1140 blk_queue_write_cache(lo->lo_queue, false, false);
1141
1fe0b1ac
CH
1142 /*
1143 * Freeze the request queue when unbinding on a live file descriptor and
1144 * thus an open device. When called from ->release we are guaranteed
1145 * that there is no I/O in progress already.
1146 */
1147 if (!release)
1148 blk_mq_freeze_queue(lo->lo_queue);
87579e9b 1149
1da177e4 1150 spin_lock_irq(&lo->lo_lock);
6050fa4c 1151 filp = lo->lo_backing_file;
1da177e4 1152 lo->lo_backing_file = NULL;
05eb0f25 1153 spin_unlock_irq(&lo->lo_lock);
1da177e4 1154
1da177e4 1155 lo->lo_device = NULL;
1da177e4
LT
1156 lo->lo_offset = 0;
1157 lo->lo_sizelimit = 0;
1da177e4 1158 memset(lo->lo_file_name, 0, LO_NAME_SIZE);
89e4fdec 1159 blk_queue_logical_block_size(lo->lo_queue, 512);
bf093753
OS
1160 blk_queue_physical_block_size(lo->lo_queue, 512);
1161 blk_queue_io_min(lo->lo_queue, 512);
e515be8f 1162 invalidate_disk(lo->lo_disk);
51a0bb0c 1163 loop_sysfs_exit(lo);
19f553db
XY
1164 /* let user-space know about this change */
1165 kobject_uevent(&disk_to_dev(lo->lo_disk)->kobj, KOBJ_CHANGE);
1da177e4 1166 mapping_set_gfp_mask(filp->f_mapping, gfp);
bf23747e
TH
1167 /* This is safe: open() is still holding a reference. */
1168 module_put(THIS_MODULE);
1fe0b1ac
CH
1169 if (!release)
1170 blk_mq_unfreeze_queue(lo->lo_queue);
f8933667 1171
ab6860f6 1172 disk_force_media_change(lo->lo_disk);
6050fa4c
TH
1173
1174 if (lo->lo_flags & LO_FLAGS_PARTSCAN) {
1175 int err;
1176
bf23747e
TH
1177 /*
1178 * open_mutex has been held already in release path, so don't
1179 * acquire it if this function is called in such case.
1180 *
1181 * If the reread partition isn't from release path, lo_refcnt
1182 * must be at least one and it can only become zero when the
1183 * current holder is released.
1184 */
1185 if (!release)
1186 mutex_lock(&lo->lo_disk->open_mutex);
0384264e 1187 err = bdev_disk_changed(lo->lo_disk, false);
bf23747e
TH
1188 if (!release)
1189 mutex_unlock(&lo->lo_disk->open_mutex);
40853d6f
DZ
1190 if (err)
1191 pr_warn("%s: partition scan of loop%d failed (rc=%d)\n",
6050fa4c 1192 __func__, lo->lo_number, err);
d57f3374 1193 /* Device is gone, no point in returning error */
d57f3374 1194 }
758a58d0 1195
bf23747e
TH
1196 /*
1197 * lo->lo_state is set to Lo_unbound here after above partscan has
1198 * finished. There cannot be anybody else entering __loop_clr_fd() as
1199 * Lo_rundown state protects us from all the other places trying to
1200 * change the 'lo' device.
1201 */
758a58d0
DZ
1202 lo->lo_flags = 0;
1203 if (!part_shift)
b9684a71 1204 set_bit(GD_SUPPRESS_PART_SCAN, &lo->lo_disk->state);
6050fa4c 1205 mutex_lock(&lo->lo_mutex);
758a58d0 1206 lo->lo_state = Lo_unbound;
6cc8e743 1207 mutex_unlock(&lo->lo_mutex);
322c4293 1208
bf23747e
TH
1209 /*
1210 * Need not hold lo_mutex to fput backing file. Calling fput holding
1211 * lo_mutex triggers a circular lock dependency possibility warning as
1212 * fput can take open_mutex which is usually taken before lo_mutex.
1213 */
1214 fput(filp);
1da177e4
LT
1215}
1216
a2505b79
JK
1217static int loop_clr_fd(struct loop_device *lo)
1218{
7ccd0791
JK
1219 int err;
1220
158eaeba
TH
1221 /*
1222 * Since lo_ioctl() is called without locks held, it is possible that
1223 * loop_configure()/loop_change_fd() and loop_clr_fd() run in parallel.
1224 *
1225 * Therefore, use global lock when setting Lo_rundown state in order to
1226 * make sure that loop_validate_file() will fail if the "struct file"
1227 * which loop_configure()/loop_change_fd() found via fget() was this
1228 * loop device.
1229 */
1230 err = loop_global_lock_killable(lo, true);
7ccd0791
JK
1231 if (err)
1232 return err;
1233 if (lo->lo_state != Lo_bound) {
158eaeba 1234 loop_global_unlock(lo, true);
a2505b79 1235 return -ENXIO;
7ccd0791 1236 }
a2505b79
JK
1237 /*
1238 * If we've explicitly asked to tear down the loop device,
1239 * and it has an elevated reference count, set it for auto-teardown when
1240 * the last reference goes away. This stops $!~#$@ udev from
1241 * preventing teardown because it decided that it needs to run blkid on
1242 * the loopback device whenever they appear. xfstests is notorious for
1243 * failing tests because blkid via udev races with a losetup
1244 * <dev>/do something like mkfs/losetup -d <dev> causing the losetup -d
1245 * command to fail with EBUSY.
1246 */
a0e286b6 1247 if (disk_openers(lo->lo_disk) > 1) {
a2505b79 1248 lo->lo_flags |= LO_FLAGS_AUTOCLEAR;
158eaeba 1249 loop_global_unlock(lo, true);
a2505b79
JK
1250 return 0;
1251 }
1252 lo->lo_state = Lo_rundown;
158eaeba 1253 loop_global_unlock(lo, true);
a2505b79 1254
bf23747e 1255 __loop_clr_fd(lo, false);
6050fa4c 1256 return 0;
a2505b79
JK
1257}
1258
1da177e4
LT
1259static int
1260loop_set_status(struct loop_device *lo, const struct loop_info64 *info)
1261{
1262 int err;
faf1d254 1263 int prev_lo_flags;
85b0a54a 1264 bool partscan = false;
0c3796c2 1265 bool size_changed = false;
1da177e4 1266
6cc8e743 1267 err = mutex_lock_killable(&lo->lo_mutex);
550df5fd
JK
1268 if (err)
1269 return err;
550df5fd
JK
1270 if (lo->lo_state != Lo_bound) {
1271 err = -ENXIO;
1272 goto out_unlock;
1273 }
1da177e4 1274
5db470e2
JK
1275 if (lo->lo_offset != info->lo_offset ||
1276 lo->lo_sizelimit != info->lo_sizelimit) {
0c3796c2 1277 size_changed = true;
5db470e2 1278 sync_blockdev(lo->lo_device);
f4bd34b1 1279 invalidate_bdev(lo->lo_device);
5db470e2
JK
1280 }
1281
ecdd0959
ML
1282 /* I/O need to be drained during transfer transition */
1283 blk_mq_freeze_queue(lo->lo_queue);
1284
faf1d254 1285 prev_lo_flags = lo->lo_flags;
1da177e4 1286
0c3796c2 1287 err = loop_set_status_from_info(lo, info);
1da177e4 1288 if (err)
550df5fd 1289 goto out_unfreeze;
1da177e4 1290
faf1d254 1291 /* Mask out flags that can't be set using LOOP_SET_STATUS. */
6ac92fb5 1292 lo->lo_flags &= LOOP_SET_STATUS_SETTABLE_FLAGS;
faf1d254
MC
1293 /* For those flags, use the previous values instead */
1294 lo->lo_flags |= prev_lo_flags & ~LOOP_SET_STATUS_SETTABLE_FLAGS;
1295 /* For flags that can't be cleared, use previous values too */
1296 lo->lo_flags |= prev_lo_flags & ~LOOP_SET_STATUS_CLEARABLE_FLAGS;
1297
b0bd158d
MC
1298 if (size_changed) {
1299 loff_t new_size = get_size(lo->lo_offset, lo->lo_sizelimit,
1300 lo->lo_backing_file);
1301 loop_set_size(lo, new_size);
b040ad9c 1302 }
541c742a 1303
dfaa2ef6 1304 loop_config_discard(lo);
1da177e4 1305
2e5ab5f3
ML
1306 /* update dio if lo_offset or transfer is changed */
1307 __loop_update_dio(lo, lo->use_dio);
1308
550df5fd 1309out_unfreeze:
ecdd0959 1310 blk_mq_unfreeze_queue(lo->lo_queue);
e02898b4 1311
faf1d254
MC
1312 if (!err && (lo->lo_flags & LO_FLAGS_PARTSCAN) &&
1313 !(prev_lo_flags & LO_FLAGS_PARTSCAN)) {
b9684a71 1314 clear_bit(GD_SUPPRESS_PART_SCAN, &lo->lo_disk->state);
85b0a54a 1315 partscan = true;
e02898b4 1316 }
550df5fd 1317out_unlock:
6cc8e743 1318 mutex_unlock(&lo->lo_mutex);
85b0a54a 1319 if (partscan)
0384264e 1320 loop_reread_partitions(lo);
e02898b4 1321
ecdd0959 1322 return err;
1da177e4
LT
1323}
1324
1325static int
1326loop_get_status(struct loop_device *lo, struct loop_info64 *info)
1327{
b1ab5fa3 1328 struct path path;
1da177e4 1329 struct kstat stat;
2d1d4c1e 1330 int ret;
1da177e4 1331
6cc8e743 1332 ret = mutex_lock_killable(&lo->lo_mutex);
4a5ce9ba
JK
1333 if (ret)
1334 return ret;
2d1d4c1e 1335 if (lo->lo_state != Lo_bound) {
6cc8e743 1336 mutex_unlock(&lo->lo_mutex);
1da177e4 1337 return -ENXIO;
2d1d4c1e
OS
1338 }
1339
1da177e4
LT
1340 memset(info, 0, sizeof(*info));
1341 info->lo_number = lo->lo_number;
1da177e4
LT
1342 info->lo_offset = lo->lo_offset;
1343 info->lo_sizelimit = lo->lo_sizelimit;
1344 info->lo_flags = lo->lo_flags;
1345 memcpy(info->lo_file_name, lo->lo_file_name, LO_NAME_SIZE);
2d1d4c1e 1346
6cc8e743 1347 /* Drop lo_mutex while we call into the filesystem. */
b1ab5fa3
TH
1348 path = lo->lo_backing_file->f_path;
1349 path_get(&path);
6cc8e743 1350 mutex_unlock(&lo->lo_mutex);
b1ab5fa3 1351 ret = vfs_getattr(&path, &stat, STATX_INO, AT_STATX_SYNC_AS_STAT);
2d1d4c1e
OS
1352 if (!ret) {
1353 info->lo_device = huge_encode_dev(stat.dev);
1354 info->lo_inode = stat.ino;
1355 info->lo_rdevice = huge_encode_dev(stat.rdev);
1356 }
b1ab5fa3 1357 path_put(&path);
2d1d4c1e 1358 return ret;
1da177e4
LT
1359}
1360
1361static void
1362loop_info64_from_old(const struct loop_info *info, struct loop_info64 *info64)
1363{
1364 memset(info64, 0, sizeof(*info64));
1365 info64->lo_number = info->lo_number;
1366 info64->lo_device = info->lo_device;
1367 info64->lo_inode = info->lo_inode;
1368 info64->lo_rdevice = info->lo_rdevice;
1369 info64->lo_offset = info->lo_offset;
1370 info64->lo_sizelimit = 0;
1da177e4 1371 info64->lo_flags = info->lo_flags;
47e96246 1372 memcpy(info64->lo_file_name, info->lo_name, LO_NAME_SIZE);
1da177e4
LT
1373}
1374
1375static int
1376loop_info64_to_old(const struct loop_info64 *info64, struct loop_info *info)
1377{
1378 memset(info, 0, sizeof(*info));
1379 info->lo_number = info64->lo_number;
1380 info->lo_device = info64->lo_device;
1381 info->lo_inode = info64->lo_inode;
1382 info->lo_rdevice = info64->lo_rdevice;
1383 info->lo_offset = info64->lo_offset;
1da177e4 1384 info->lo_flags = info64->lo_flags;
47e96246 1385 memcpy(info->lo_name, info64->lo_file_name, LO_NAME_SIZE);
1da177e4
LT
1386
1387 /* error in case values were truncated */
1388 if (info->lo_device != info64->lo_device ||
1389 info->lo_rdevice != info64->lo_rdevice ||
1390 info->lo_inode != info64->lo_inode ||
1391 info->lo_offset != info64->lo_offset)
1392 return -EOVERFLOW;
1393
1394 return 0;
1395}
1396
1397static int
1398loop_set_status_old(struct loop_device *lo, const struct loop_info __user *arg)
1399{
1400 struct loop_info info;
1401 struct loop_info64 info64;
1402
1403 if (copy_from_user(&info, arg, sizeof (struct loop_info)))
1404 return -EFAULT;
1405 loop_info64_from_old(&info, &info64);
1406 return loop_set_status(lo, &info64);
1407}
1408
1409static int
1410loop_set_status64(struct loop_device *lo, const struct loop_info64 __user *arg)
1411{
1412 struct loop_info64 info64;
1413
1414 if (copy_from_user(&info64, arg, sizeof (struct loop_info64)))
1415 return -EFAULT;
1416 return loop_set_status(lo, &info64);
1417}
1418
1419static int
1420loop_get_status_old(struct loop_device *lo, struct loop_info __user *arg) {
1421 struct loop_info info;
1422 struct loop_info64 info64;
bdac616d 1423 int err;
1da177e4 1424
4a5ce9ba 1425 if (!arg)
bdac616d 1426 return -EINVAL;
bdac616d 1427 err = loop_get_status(lo, &info64);
1da177e4
LT
1428 if (!err)
1429 err = loop_info64_to_old(&info64, &info);
1430 if (!err && copy_to_user(arg, &info, sizeof(info)))
1431 err = -EFAULT;
1432
1433 return err;
1434}
1435
1436static int
1437loop_get_status64(struct loop_device *lo, struct loop_info64 __user *arg) {
1438 struct loop_info64 info64;
bdac616d 1439 int err;
1da177e4 1440
4a5ce9ba 1441 if (!arg)
bdac616d 1442 return -EINVAL;
bdac616d 1443 err = loop_get_status(lo, &info64);
1da177e4
LT
1444 if (!err && copy_to_user(arg, &info64, sizeof(info64)))
1445 err = -EFAULT;
1446
1447 return err;
1448}
1449
51001b7d 1450static int loop_set_capacity(struct loop_device *lo)
53d66608 1451{
0a6ed1b5
MC
1452 loff_t size;
1453
53d66608 1454 if (unlikely(lo->lo_state != Lo_bound))
7b0576a3 1455 return -ENXIO;
53d66608 1456
0a6ed1b5
MC
1457 size = get_loop_size(lo, lo->lo_backing_file);
1458 loop_set_size(lo, size);
083a6a50
MC
1459
1460 return 0;
53d66608
O
1461}
1462
ab1cb278
ML
1463static int loop_set_dio(struct loop_device *lo, unsigned long arg)
1464{
1465 int error = -ENXIO;
1466 if (lo->lo_state != Lo_bound)
1467 goto out;
1468
1469 __loop_update_dio(lo, !!arg);
1470 if (lo->use_dio == !!arg)
1471 return 0;
1472 error = -EINVAL;
1473 out:
1474 return error;
1475}
1476
89e4fdec
OS
1477static int loop_set_block_size(struct loop_device *lo, unsigned long arg)
1478{
5db470e2
JK
1479 int err = 0;
1480
89e4fdec
OS
1481 if (lo->lo_state != Lo_bound)
1482 return -ENXIO;
1483
af3c570f 1484 err = blk_validate_block_size(arg);
3448914e
MC
1485 if (err)
1486 return err;
89e4fdec 1487
7e81f99a
MC
1488 if (lo->lo_queue->limits.logical_block_size == arg)
1489 return 0;
1490
1491 sync_blockdev(lo->lo_device);
f4bd34b1 1492 invalidate_bdev(lo->lo_device);
5db470e2 1493
89e4fdec 1494 blk_mq_freeze_queue(lo->lo_queue);
89e4fdec 1495 blk_queue_logical_block_size(lo->lo_queue, arg);
bf093753
OS
1496 blk_queue_physical_block_size(lo->lo_queue, arg);
1497 blk_queue_io_min(lo->lo_queue, arg);
89e4fdec 1498 loop_update_dio(lo);
89e4fdec
OS
1499 blk_mq_unfreeze_queue(lo->lo_queue);
1500
5db470e2 1501 return err;
89e4fdec
OS
1502}
1503
a1316544
JK
1504static int lo_simple_ioctl(struct loop_device *lo, unsigned int cmd,
1505 unsigned long arg)
1da177e4 1506{
1da177e4
LT
1507 int err;
1508
6cc8e743 1509 err = mutex_lock_killable(&lo->lo_mutex);
3148ffbd 1510 if (err)
a1316544
JK
1511 return err;
1512 switch (cmd) {
1513 case LOOP_SET_CAPACITY:
1514 err = loop_set_capacity(lo);
1515 break;
1516 case LOOP_SET_DIRECT_IO:
1517 err = loop_set_dio(lo, arg);
1518 break;
1519 case LOOP_SET_BLOCK_SIZE:
1520 err = loop_set_block_size(lo, arg);
1521 break;
1522 default:
47e96246 1523 err = -EINVAL;
a1316544 1524 }
6cc8e743 1525 mutex_unlock(&lo->lo_mutex);
a1316544
JK
1526 return err;
1527}
1528
05bdb996 1529static int lo_ioctl(struct block_device *bdev, blk_mode_t mode,
a1316544
JK
1530 unsigned int cmd, unsigned long arg)
1531{
1532 struct loop_device *lo = bdev->bd_disk->private_data;
571fae6e 1533 void __user *argp = (void __user *) arg;
a1316544 1534 int err;
3148ffbd 1535
1da177e4 1536 switch (cmd) {
3448914e
MC
1537 case LOOP_SET_FD: {
1538 /*
1539 * Legacy case - pass in a zeroed out struct loop_config with
1540 * only the file descriptor set , which corresponds with the
1541 * default parameters we'd have used otherwise.
1542 */
1543 struct loop_config config;
1544
1545 memset(&config, 0, sizeof(config));
1546 config.fd = arg;
1547
1548 return loop_configure(lo, mode, bdev, &config);
1549 }
1550 case LOOP_CONFIGURE: {
1551 struct loop_config config;
1552
1553 if (copy_from_user(&config, argp, sizeof(config)))
1554 return -EFAULT;
1555
1556 return loop_configure(lo, mode, bdev, &config);
1557 }
1da177e4 1558 case LOOP_CHANGE_FD:
c3710770 1559 return loop_change_fd(lo, bdev, arg);
1da177e4 1560 case LOOP_CLR_FD:
7ccd0791 1561 return loop_clr_fd(lo);
1da177e4 1562 case LOOP_SET_STATUS:
7035b5df 1563 err = -EPERM;
05bdb996 1564 if ((mode & BLK_OPEN_WRITE) || capable(CAP_SYS_ADMIN))
571fae6e 1565 err = loop_set_status_old(lo, argp);
1da177e4
LT
1566 break;
1567 case LOOP_GET_STATUS:
571fae6e 1568 return loop_get_status_old(lo, argp);
1da177e4 1569 case LOOP_SET_STATUS64:
7035b5df 1570 err = -EPERM;
05bdb996 1571 if ((mode & BLK_OPEN_WRITE) || capable(CAP_SYS_ADMIN))
571fae6e 1572 err = loop_set_status64(lo, argp);
1da177e4
LT
1573 break;
1574 case LOOP_GET_STATUS64:
571fae6e 1575 return loop_get_status64(lo, argp);
a1316544 1576 case LOOP_SET_CAPACITY:
ab1cb278 1577 case LOOP_SET_DIRECT_IO:
89e4fdec 1578 case LOOP_SET_BLOCK_SIZE:
05bdb996 1579 if (!(mode & BLK_OPEN_WRITE) && !capable(CAP_SYS_ADMIN))
a1316544 1580 return -EPERM;
df561f66 1581 fallthrough;
1da177e4 1582 default:
a1316544
JK
1583 err = lo_simple_ioctl(lo, cmd, arg);
1584 break;
1da177e4 1585 }
f028f3b2 1586
1da177e4
LT
1587 return err;
1588}
1589
863d5b82
DH
1590#ifdef CONFIG_COMPAT
1591struct compat_loop_info {
1592 compat_int_t lo_number; /* ioctl r/o */
1593 compat_dev_t lo_device; /* ioctl r/o */
1594 compat_ulong_t lo_inode; /* ioctl r/o */
1595 compat_dev_t lo_rdevice; /* ioctl r/o */
1596 compat_int_t lo_offset;
f941c51e 1597 compat_int_t lo_encrypt_type; /* obsolete, ignored */
863d5b82
DH
1598 compat_int_t lo_encrypt_key_size; /* ioctl w/o */
1599 compat_int_t lo_flags; /* ioctl r/o */
1600 char lo_name[LO_NAME_SIZE];
1601 unsigned char lo_encrypt_key[LO_KEY_SIZE]; /* ioctl w/o */
1602 compat_ulong_t lo_init[2];
1603 char reserved[4];
1604};
1605
1606/*
1607 * Transfer 32-bit compatibility structure in userspace to 64-bit loop info
1608 * - noinlined to reduce stack space usage in main part of driver
1609 */
1610static noinline int
ba674cfc 1611loop_info64_from_compat(const struct compat_loop_info __user *arg,
863d5b82
DH
1612 struct loop_info64 *info64)
1613{
1614 struct compat_loop_info info;
1615
1616 if (copy_from_user(&info, arg, sizeof(info)))
1617 return -EFAULT;
1618
1619 memset(info64, 0, sizeof(*info64));
1620 info64->lo_number = info.lo_number;
1621 info64->lo_device = info.lo_device;
1622 info64->lo_inode = info.lo_inode;
1623 info64->lo_rdevice = info.lo_rdevice;
1624 info64->lo_offset = info.lo_offset;
1625 info64->lo_sizelimit = 0;
863d5b82 1626 info64->lo_flags = info.lo_flags;
47e96246 1627 memcpy(info64->lo_file_name, info.lo_name, LO_NAME_SIZE);
863d5b82
DH
1628 return 0;
1629}
1630
1631/*
1632 * Transfer 64-bit loop info to 32-bit compatibility structure in userspace
1633 * - noinlined to reduce stack space usage in main part of driver
1634 */
1635static noinline int
1636loop_info64_to_compat(const struct loop_info64 *info64,
1637 struct compat_loop_info __user *arg)
1638{
1639 struct compat_loop_info info;
1640
1641 memset(&info, 0, sizeof(info));
1642 info.lo_number = info64->lo_number;
1643 info.lo_device = info64->lo_device;
1644 info.lo_inode = info64->lo_inode;
1645 info.lo_rdevice = info64->lo_rdevice;
1646 info.lo_offset = info64->lo_offset;
863d5b82 1647 info.lo_flags = info64->lo_flags;
47e96246 1648 memcpy(info.lo_name, info64->lo_file_name, LO_NAME_SIZE);
863d5b82
DH
1649
1650 /* error in case values were truncated */
1651 if (info.lo_device != info64->lo_device ||
1652 info.lo_rdevice != info64->lo_rdevice ||
1653 info.lo_inode != info64->lo_inode ||
47e96246 1654 info.lo_offset != info64->lo_offset)
863d5b82
DH
1655 return -EOVERFLOW;
1656
1657 if (copy_to_user(arg, &info, sizeof(info)))
1658 return -EFAULT;
1659 return 0;
1660}
1661
1662static int
1663loop_set_status_compat(struct loop_device *lo,
1664 const struct compat_loop_info __user *arg)
1665{
1666 struct loop_info64 info64;
1667 int ret;
1668
1669 ret = loop_info64_from_compat(arg, &info64);
1670 if (ret < 0)
1671 return ret;
1672 return loop_set_status(lo, &info64);
1673}
1674
1675static int
1676loop_get_status_compat(struct loop_device *lo,
1677 struct compat_loop_info __user *arg)
1678{
1679 struct loop_info64 info64;
bdac616d 1680 int err;
863d5b82 1681
4a5ce9ba 1682 if (!arg)
bdac616d 1683 return -EINVAL;
bdac616d 1684 err = loop_get_status(lo, &info64);
863d5b82
DH
1685 if (!err)
1686 err = loop_info64_to_compat(&info64, arg);
1687 return err;
1688}
1689
05bdb996 1690static int lo_compat_ioctl(struct block_device *bdev, blk_mode_t mode,
bb214884 1691 unsigned int cmd, unsigned long arg)
863d5b82 1692{
bb214884 1693 struct loop_device *lo = bdev->bd_disk->private_data;
863d5b82
DH
1694 int err;
1695
863d5b82
DH
1696 switch(cmd) {
1697 case LOOP_SET_STATUS:
550df5fd
JK
1698 err = loop_set_status_compat(lo,
1699 (const struct compat_loop_info __user *)arg);
863d5b82
DH
1700 break;
1701 case LOOP_GET_STATUS:
4a5ce9ba
JK
1702 err = loop_get_status_compat(lo,
1703 (struct compat_loop_info __user *)arg);
863d5b82 1704 break;
53d66608 1705 case LOOP_SET_CAPACITY:
863d5b82
DH
1706 case LOOP_CLR_FD:
1707 case LOOP_GET_STATUS64:
1708 case LOOP_SET_STATUS64:
3448914e 1709 case LOOP_CONFIGURE:
863d5b82 1710 arg = (unsigned long) compat_ptr(arg);
df561f66 1711 fallthrough;
863d5b82
DH
1712 case LOOP_SET_FD:
1713 case LOOP_CHANGE_FD:
9fea4b39 1714 case LOOP_SET_BLOCK_SIZE:
fdbe4eee 1715 case LOOP_SET_DIRECT_IO:
bb214884 1716 err = lo_ioctl(bdev, mode, cmd, arg);
863d5b82
DH
1717 break;
1718 default:
1719 err = -ENOIOCTLCMD;
1720 break;
1721 }
863d5b82
DH
1722 return err;
1723}
1724#endif
1725
ae220766 1726static void lo_release(struct gendisk *disk)
1da177e4 1727{
6cc8e743 1728 struct loop_device *lo = disk->private_data;
1da177e4 1729
a0e286b6
CH
1730 if (disk_openers(disk) > 0)
1731 return;
14f27939 1732
a0e286b6
CH
1733 mutex_lock(&lo->lo_mutex);
1734 if (lo->lo_state == Lo_bound && (lo->lo_flags & LO_FLAGS_AUTOCLEAR)) {
a2505b79 1735 lo->lo_state = Lo_rundown;
6cc8e743 1736 mutex_unlock(&lo->lo_mutex);
14f27939
MB
1737 /*
1738 * In autoclear mode, stop the loop thread
1739 * and remove configuration after last close.
1740 */
bf23747e 1741 __loop_clr_fd(lo, true);
7ccd0791 1742 return;
14f27939 1743 }
6cc8e743 1744 mutex_unlock(&lo->lo_mutex);
ae665016
LT
1745}
1746
d2c7f56f
CH
1747static void lo_free_disk(struct gendisk *disk)
1748{
1749 struct loop_device *lo = disk->private_data;
1750
d292dc80
CH
1751 if (lo->workqueue)
1752 destroy_workqueue(lo->workqueue);
1753 loop_free_idle_workers(lo, true);
292a089d 1754 timer_shutdown_sync(&lo->timer);
d2c7f56f
CH
1755 mutex_destroy(&lo->lo_mutex);
1756 kfree(lo);
1757}
1758
83d5cde4 1759static const struct block_device_operations lo_fops = {
1da177e4 1760 .owner = THIS_MODULE,
bb214884
AV
1761 .release = lo_release,
1762 .ioctl = lo_ioctl,
863d5b82 1763#ifdef CONFIG_COMPAT
bb214884 1764 .compat_ioctl = lo_compat_ioctl,
863d5b82 1765#endif
d2c7f56f 1766 .free_disk = lo_free_disk,
1da177e4
LT
1767};
1768
1769/*
1770 * And now the modules code and kernel interface.
1771 */
85c50197
IM
1772
1773/*
1774 * If max_loop is specified, create that many devices upfront.
1775 * This also becomes a hard limit. If max_loop is not specified,
bb5faa99
MFO
1776 * the default isn't a hard limit (as before commit 85c50197716c
1777 * changed the default value from 0 for max_loop=0 reasons), just
85c50197
IM
1778 * create CONFIG_BLK_DEV_LOOP_MIN_COUNT loop devices at module
1779 * init time. Loop devices can be requested on-demand with the
1780 * /dev/loop-control interface, or be instantiated by accessing
1781 * a 'dead' device node.
1782 */
1783static int max_loop = CONFIG_BLK_DEV_LOOP_MIN_COUNT;
bb5faa99
MFO
1784
1785#ifdef CONFIG_BLOCK_LEGACY_AUTOLOAD
1786static bool max_loop_specified;
1787
1788static int max_loop_param_set_int(const char *val,
1789 const struct kernel_param *kp)
1790{
1791 int ret;
1792
1793 ret = param_set_int(val, kp);
1794 if (ret < 0)
1795 return ret;
1796
1797 max_loop_specified = true;
1798 return 0;
1799}
1800
1801static const struct kernel_param_ops max_loop_param_ops = {
1802 .set = max_loop_param_set_int,
1803 .get = param_get_int,
1804};
1805
1806module_param_cb(max_loop, &max_loop_param_ops, &max_loop, 0444);
a47653fc 1807MODULE_PARM_DESC(max_loop, "Maximum number of loop devices");
bb5faa99
MFO
1808#else
1809module_param(max_loop, int, 0444);
1810MODULE_PARM_DESC(max_loop, "Initial number of loop devices");
1811#endif
1812
5657a819 1813module_param(max_part, int, 0444);
476a4813 1814MODULE_PARM_DESC(max_part, "Maximum number of partitions per loop device");
ef44c508
CK
1815
1816static int hw_queue_depth = LOOP_DEFAULT_HW_Q_DEPTH;
1817
1818static int loop_set_hw_queue_depth(const char *s, const struct kernel_param *p)
1819{
e152a05f 1820 int qd, ret;
ef44c508 1821
e152a05f
BVA
1822 ret = kstrtoint(s, 0, &qd);
1823 if (ret < 0)
1824 return ret;
1825 if (qd < 1)
1826 return -EINVAL;
1827 hw_queue_depth = qd;
1828 return 0;
ef44c508
CK
1829}
1830
1831static const struct kernel_param_ops loop_hw_qdepth_param_ops = {
1832 .set = loop_set_hw_queue_depth,
1833 .get = param_get_int,
1834};
1835
1836device_param_cb(hw_queue_depth, &loop_hw_qdepth_param_ops, &hw_queue_depth, 0444);
e152a05f 1837MODULE_PARM_DESC(hw_queue_depth, "Queue depth for each hardware queue. Default: " __stringify(LOOP_DEFAULT_HW_Q_DEPTH));
ef44c508 1838
1da177e4
LT
1839MODULE_LICENSE("GPL");
1840MODULE_ALIAS_BLOCKDEV_MAJOR(LOOP_MAJOR);
1841
fc17b653 1842static blk_status_t loop_queue_rq(struct blk_mq_hw_ctx *hctx,
b5dd2f60
ML
1843 const struct blk_mq_queue_data *bd)
1844{
1894e916
JA
1845 struct request *rq = bd->rq;
1846 struct loop_cmd *cmd = blk_mq_rq_to_pdu(rq);
1847 struct loop_device *lo = rq->q->queuedata;
b5dd2f60 1848
1894e916 1849 blk_mq_start_request(rq);
b5dd2f60 1850
f4aa4c7b 1851 if (lo->lo_state != Lo_bound)
fc17b653 1852 return BLK_STS_IOERR;
f4aa4c7b 1853
1894e916 1854 switch (req_op(rq)) {
f0225cac
CH
1855 case REQ_OP_FLUSH:
1856 case REQ_OP_DISCARD:
19372e27 1857 case REQ_OP_WRITE_ZEROES:
bc07c10a 1858 cmd->use_aio = false;
f0225cac
CH
1859 break;
1860 default:
1861 cmd->use_aio = lo->use_dio;
1862 break;
1863 }
bc07c10a 1864
d4478e92 1865 /* always use the first bio's css */
c74d40e8
DS
1866 cmd->blkcg_css = NULL;
1867 cmd->memcg_css = NULL;
0b508bc9 1868#ifdef CONFIG_BLK_CGROUP
bbb1ebe7
CH
1869 if (rq->bio) {
1870 cmd->blkcg_css = bio_blkcg_css(rq->bio);
c74d40e8 1871#ifdef CONFIG_MEMCG
bbb1ebe7
CH
1872 if (cmd->blkcg_css) {
1873 cmd->memcg_css =
1874 cgroup_get_e_css(cmd->blkcg_css->cgroup,
1875 &memory_cgrp_subsys);
1876 }
c74d40e8
DS
1877#endif
1878 }
d4478e92 1879#endif
87579e9b 1880 loop_queue_work(lo, cmd);
b5dd2f60 1881
fc17b653 1882 return BLK_STS_OK;
b5dd2f60
ML
1883}
1884
1885static void loop_handle_cmd(struct loop_cmd *cmd)
1886{
9b0cb770
BVA
1887 struct cgroup_subsys_state *cmd_blkcg_css = cmd->blkcg_css;
1888 struct cgroup_subsys_state *cmd_memcg_css = cmd->memcg_css;
1894e916
JA
1889 struct request *rq = blk_mq_rq_from_pdu(cmd);
1890 const bool write = op_is_write(req_op(rq));
1891 struct loop_device *lo = rq->q->queuedata;
f4829a9b 1892 int ret = 0;
c74d40e8 1893 struct mem_cgroup *old_memcg = NULL;
9b0cb770 1894 const bool use_aio = cmd->use_aio;
b5dd2f60 1895
f4829a9b
CH
1896 if (write && (lo->lo_flags & LO_FLAGS_READ_ONLY)) {
1897 ret = -EIO;
b5dd2f60 1898 goto failed;
f4829a9b 1899 }
b5dd2f60 1900
9b0cb770
BVA
1901 if (cmd_blkcg_css)
1902 kthread_associate_blkcg(cmd_blkcg_css);
1903 if (cmd_memcg_css)
c74d40e8 1904 old_memcg = set_active_memcg(
9b0cb770 1905 mem_cgroup_from_css(cmd_memcg_css));
c74d40e8 1906
9b0cb770
BVA
1907 /*
1908 * do_req_filebacked() may call blk_mq_complete_request() synchronously
1909 * or asynchronously if using aio. Hence, do not touch 'cmd' after
1910 * do_req_filebacked() has returned unless we are sure that 'cmd' has
1911 * not yet been completed.
1912 */
1894e916 1913 ret = do_req_filebacked(lo, rq);
c74d40e8 1914
9b0cb770 1915 if (cmd_blkcg_css)
c74d40e8
DS
1916 kthread_associate_blkcg(NULL);
1917
9b0cb770 1918 if (cmd_memcg_css) {
c74d40e8 1919 set_active_memcg(old_memcg);
9b0cb770 1920 css_put(cmd_memcg_css);
c74d40e8 1921 }
b5dd2f60 1922 failed:
bc07c10a 1923 /* complete non-aio request */
9b0cb770 1924 if (!use_aio || ret) {
8cd55087
EG
1925 if (ret == -EOPNOTSUPP)
1926 cmd->ret = ret;
1927 else
1928 cmd->ret = ret ? -EIO : 0;
15f73f5b
CH
1929 if (likely(!blk_should_fake_timeout(rq->q)))
1930 blk_mq_complete_request(rq);
fe2cb290 1931 }
b5dd2f60
ML
1932}
1933
87579e9b
DS
1934static void loop_process_work(struct loop_worker *worker,
1935 struct list_head *cmd_list, struct loop_device *lo)
b5dd2f60 1936{
87579e9b
DS
1937 int orig_flags = current->flags;
1938 struct loop_cmd *cmd;
b5dd2f60 1939
87579e9b
DS
1940 current->flags |= PF_LOCAL_THROTTLE | PF_MEMALLOC_NOIO;
1941 spin_lock_irq(&lo->lo_work_lock);
1942 while (!list_empty(cmd_list)) {
1943 cmd = container_of(
1944 cmd_list->next, struct loop_cmd, list_entry);
1945 list_del(cmd_list->next);
1946 spin_unlock_irq(&lo->lo_work_lock);
1947
1948 loop_handle_cmd(cmd);
1949 cond_resched();
1950
1951 spin_lock_irq(&lo->lo_work_lock);
1952 }
1953
1954 /*
1955 * We only add to the idle list if there are no pending cmds
1956 * *and* the worker will not run again which ensures that it
1957 * is safe to free any worker on the idle list
1958 */
1959 if (worker && !work_pending(&worker->work)) {
1960 worker->last_ran_at = jiffies;
1961 list_add_tail(&worker->idle_list, &lo->idle_worker_list);
1962 loop_set_timer(lo);
1963 }
1964 spin_unlock_irq(&lo->lo_work_lock);
1965 current->flags = orig_flags;
b5dd2f60
ML
1966}
1967
87579e9b 1968static void loop_workfn(struct work_struct *work)
b5dd2f60 1969{
87579e9b
DS
1970 struct loop_worker *worker =
1971 container_of(work, struct loop_worker, work);
1972 loop_process_work(worker, &worker->cmd_list, worker->lo);
1973}
b5dd2f60 1974
87579e9b
DS
1975static void loop_rootcg_workfn(struct work_struct *work)
1976{
1977 struct loop_device *lo =
1978 container_of(work, struct loop_device, rootcg_work);
1979 loop_process_work(NULL, &lo->rootcg_cmd_list, lo);
1980}
1981
f363b089 1982static const struct blk_mq_ops loop_mq_ops = {
b5dd2f60 1983 .queue_rq = loop_queue_rq,
fe2cb290 1984 .complete = lo_complete_rq,
b5dd2f60
ML
1985};
1986
d6da83d0 1987static int loop_add(int i)
73285082
KC
1988{
1989 struct loop_device *lo;
1990 struct gendisk *disk;
34dd82af 1991 int err;
73285082 1992
68d740d7 1993 err = -ENOMEM;
73285082 1994 lo = kzalloc(sizeof(*lo), GFP_KERNEL);
68d740d7 1995 if (!lo)
73285082 1996 goto out;
b15ed546
CH
1997 lo->worker_tree = RB_ROOT;
1998 INIT_LIST_HEAD(&lo->idle_worker_list);
1999 timer_setup(&lo->timer, loop_free_idle_workers_timer, TIMER_DEFERRABLE);
ef7e7c82
MP
2000 lo->lo_state = Lo_unbound;
2001
18d1f200
CH
2002 err = mutex_lock_killable(&loop_ctl_mutex);
2003 if (err)
2004 goto out_free_dev;
2005
c718aa65 2006 /* allocate id, if @id >= 0, we're requesting that specific id */
34dd82af 2007 if (i >= 0) {
c718aa65
TH
2008 err = idr_alloc(&loop_index_idr, lo, i, i + 1, GFP_KERNEL);
2009 if (err == -ENOSPC)
34dd82af 2010 err = -EEXIST;
34dd82af 2011 } else {
c718aa65 2012 err = idr_alloc(&loop_index_idr, lo, 0, 0, GFP_KERNEL);
34dd82af 2013 }
1c500ad7 2014 mutex_unlock(&loop_ctl_mutex);
34dd82af 2015 if (err < 0)
1c500ad7 2016 goto out_free_dev;
c718aa65 2017 i = err;
73285082 2018
b5dd2f60
ML
2019 lo->tag_set.ops = &loop_mq_ops;
2020 lo->tag_set.nr_hw_queues = 1;
ef44c508 2021 lo->tag_set.queue_depth = hw_queue_depth;
b5dd2f60
ML
2022 lo->tag_set.numa_node = NUMA_NO_NODE;
2023 lo->tag_set.cmd_size = sizeof(struct loop_cmd);
2112f5c1
BVA
2024 lo->tag_set.flags = BLK_MQ_F_SHOULD_MERGE | BLK_MQ_F_STACKING |
2025 BLK_MQ_F_NO_SCHED_BY_DEFAULT;
b5dd2f60
ML
2026 lo->tag_set.driver_data = lo;
2027
2028 err = blk_mq_alloc_tag_set(&lo->tag_set);
2029 if (err)
3ec981e3 2030 goto out_free_idr;
73285082 2031
1c99502f
CH
2032 disk = lo->lo_disk = blk_mq_alloc_disk(&lo->tag_set, lo);
2033 if (IS_ERR(disk)) {
2034 err = PTR_ERR(disk);
b5dd2f60
ML
2035 goto out_cleanup_tags;
2036 }
1c99502f 2037 lo->lo_queue = lo->lo_disk->queue;
ef7e7c82 2038
54bb0ade 2039 blk_queue_max_hw_sectors(lo->lo_queue, BLK_DEF_MAX_SECTORS);
40326d8a 2040
5b5e20f4 2041 /*
40326d8a
SL
2042 * By default, we do buffer IO, so it doesn't make sense to enable
2043 * merge because the I/O submitted to backing file is handled page by
2044 * page. For directio mode, merge does help to dispatch bigger request
2045 * to underlayer disk. We will enable merge once directio is enabled.
5b5e20f4 2046 */
8b904b5b 2047 blk_queue_flag_set(QUEUE_FLAG_NOMERGES, lo->lo_queue);
5b5e20f4 2048
e03c8dd1
KS
2049 /*
2050 * Disable partition scanning by default. The in-kernel partition
2051 * scanning can be requested individually per-device during its
2052 * setup. Userspace can always add and remove partitions from all
2053 * devices. The needed partition minors are allocated from the
2054 * extended minor space, the main loop device numbers will continue
2055 * to match the loop minors, regardless of the number of partitions
2056 * used.
2057 *
2058 * If max_part is given, partition scanning is globally enabled for
2059 * all loop devices. The minors for the main loop devices will be
2060 * multiples of max_part.
2061 *
2062 * Note: Global-for-all-devices, set-only-at-init, read-only module
2063 * parameteters like 'max_loop' and 'max_part' make things needlessly
2064 * complicated, are too static, inflexible and may surprise
2065 * userspace tools. Parameters like this in general should be avoided.
2066 */
2067 if (!part_shift)
b9684a71 2068 set_bit(GD_SUPPRESS_PART_SCAN, &disk->state);
6cc8e743 2069 mutex_init(&lo->lo_mutex);
73285082 2070 lo->lo_number = i;
73285082 2071 spin_lock_init(&lo->lo_lock);
87579e9b 2072 spin_lock_init(&lo->lo_work_lock);
d292dc80
CH
2073 INIT_WORK(&lo->rootcg_work, loop_rootcg_workfn);
2074 INIT_LIST_HEAD(&lo->rootcg_cmd_list);
73285082 2075 disk->major = LOOP_MAJOR;
476a4813 2076 disk->first_minor = i << part_shift;
1c99502f 2077 disk->minors = 1 << part_shift;
73285082
KC
2078 disk->fops = &lo_fops;
2079 disk->private_data = lo;
2080 disk->queue = lo->lo_queue;
9f65c489
MC
2081 disk->events = DISK_EVENT_MEDIA_CHANGE;
2082 disk->event_flags = DISK_EVENT_FLAG_UEVENT;
73285082 2083 sprintf(disk->disk_name, "loop%d", i);
1c500ad7 2084 /* Make this loop device reachable from pathname. */
905705f0
LC
2085 err = add_disk(disk);
2086 if (err)
2087 goto out_cleanup_disk;
2088
1c500ad7
TH
2089 /* Show this loop device. */
2090 mutex_lock(&loop_ctl_mutex);
2091 lo->idr_visible = true;
18d1f200 2092 mutex_unlock(&loop_ctl_mutex);
905705f0 2093
18d1f200 2094 return i;
73285082 2095
905705f0 2096out_cleanup_disk:
8b9ab626 2097 put_disk(disk);
b5dd2f60
ML
2098out_cleanup_tags:
2099 blk_mq_free_tag_set(&lo->tag_set);
3ec981e3 2100out_free_idr:
1c500ad7 2101 mutex_lock(&loop_ctl_mutex);
3ec981e3 2102 idr_remove(&loop_index_idr, i);
18d1f200 2103 mutex_unlock(&loop_ctl_mutex);
73285082
KC
2104out_free_dev:
2105 kfree(lo);
2106out:
34dd82af 2107 return err;
73285082
KC
2108}
2109
34dd82af 2110static void loop_remove(struct loop_device *lo)
1da177e4 2111{
1c500ad7 2112 /* Make this loop device unreachable from pathname. */
6cd18e71 2113 del_gendisk(lo->lo_disk);
b5dd2f60 2114 blk_mq_free_tag_set(&lo->tag_set);
ce8d7861 2115
1c500ad7
TH
2116 mutex_lock(&loop_ctl_mutex);
2117 idr_remove(&loop_index_idr, lo->lo_number);
2118 mutex_unlock(&loop_ctl_mutex);
d2c7f56f
CH
2119
2120 put_disk(lo->lo_disk);
73285082 2121}
1da177e4 2122
23881aec 2123#ifdef CONFIG_BLOCK_LEGACY_AUTOLOAD
8410d38c 2124static void loop_probe(dev_t dev)
73285082 2125{
8410d38c 2126 int idx = MINOR(dev) >> part_shift;
8410d38c 2127
bb5faa99 2128 if (max_loop_specified && max_loop && idx >= max_loop)
8410d38c 2129 return;
4157fe0b 2130 loop_add(idx);
f9d10764 2131}
23881aec
MFO
2132#else
2133#define loop_probe NULL
2134#endif /* !CONFIG_BLOCK_LEGACY_AUTOLOAD */
f9d10764
CH
2135
2136static int loop_control_remove(int idx)
2137{
2138 struct loop_device *lo;
2139 int ret;
e5d66a10
CH
2140
2141 if (idx < 0) {
e3f9387a 2142 pr_warn_once("deleting an unspecified loop device is not supported.\n");
e5d66a10
CH
2143 return -EINVAL;
2144 }
f9d10764 2145
1c500ad7 2146 /* Hide this loop device for serialization. */
f9d10764
CH
2147 ret = mutex_lock_killable(&loop_ctl_mutex);
2148 if (ret)
2149 return ret;
b9848081 2150 lo = idr_find(&loop_index_idr, idx);
1c500ad7 2151 if (!lo || !lo->idr_visible)
b9848081 2152 ret = -ENODEV;
1c500ad7
TH
2153 else
2154 lo->idr_visible = false;
2155 mutex_unlock(&loop_ctl_mutex);
2156 if (ret)
2157 return ret;
f9d10764 2158
1c500ad7 2159 /* Check whether this loop device can be removed. */
f9d10764
CH
2160 ret = mutex_lock_killable(&lo->lo_mutex);
2161 if (ret)
1c500ad7 2162 goto mark_visible;
a0e286b6 2163 if (lo->lo_state != Lo_unbound || disk_openers(lo->lo_disk) > 0) {
f9d10764
CH
2164 mutex_unlock(&lo->lo_mutex);
2165 ret = -EBUSY;
1c500ad7 2166 goto mark_visible;
f9d10764 2167 }
a0e286b6 2168 /* Mark this loop device as no more bound, but not quite unbound yet */
f9d10764
CH
2169 lo->lo_state = Lo_deleting;
2170 mutex_unlock(&lo->lo_mutex);
2171
f9d10764 2172 loop_remove(lo);
1c500ad7
TH
2173 return 0;
2174
2175mark_visible:
2176 /* Show this loop device again. */
2177 mutex_lock(&loop_ctl_mutex);
2178 lo->idr_visible = true;
f9d10764
CH
2179 mutex_unlock(&loop_ctl_mutex);
2180 return ret;
2181}
2182
2183static int loop_control_get_free(int idx)
770fe30a
KS
2184{
2185 struct loop_device *lo;
b9848081 2186 int id, ret;
770fe30a 2187
0a42e99b
JK
2188 ret = mutex_lock_killable(&loop_ctl_mutex);
2189 if (ret)
2190 return ret;
b9848081 2191 idr_for_each_entry(&loop_index_idr, lo, id) {
1c500ad7
TH
2192 /* Hitting a race results in creating a new loop device which is harmless. */
2193 if (lo->idr_visible && data_race(lo->lo_state) == Lo_unbound)
b9848081
CH
2194 goto found;
2195 }
f9d10764 2196 mutex_unlock(&loop_ctl_mutex);
18d1f200 2197 return loop_add(-1);
b9848081
CH
2198found:
2199 mutex_unlock(&loop_ctl_mutex);
2200 return id;
f9d10764 2201}
0a42e99b 2202
f9d10764
CH
2203static long loop_control_ioctl(struct file *file, unsigned int cmd,
2204 unsigned long parm)
2205{
770fe30a
KS
2206 switch (cmd) {
2207 case LOOP_CTL_ADD:
18d1f200 2208 return loop_add(parm);
770fe30a 2209 case LOOP_CTL_REMOVE:
f9d10764 2210 return loop_control_remove(parm);
770fe30a 2211 case LOOP_CTL_GET_FREE:
f9d10764
CH
2212 return loop_control_get_free(parm);
2213 default:
2214 return -ENOSYS;
770fe30a 2215 }
770fe30a
KS
2216}
2217
2218static const struct file_operations loop_ctl_fops = {
2219 .open = nonseekable_open,
2220 .unlocked_ioctl = loop_control_ioctl,
2221 .compat_ioctl = loop_control_ioctl,
2222 .owner = THIS_MODULE,
2223 .llseek = noop_llseek,
2224};
2225
2226static struct miscdevice loop_misc = {
2227 .minor = LOOP_CTRL_MINOR,
2228 .name = "loop-control",
2229 .fops = &loop_ctl_fops,
2230};
2231
2232MODULE_ALIAS_MISCDEV(LOOP_CTRL_MINOR);
2233MODULE_ALIAS("devname:loop-control");
2234
73285082
KC
2235static int __init loop_init(void)
2236{
85c50197 2237 int i;
770fe30a 2238 int err;
a47653fc 2239
476a4813 2240 part_shift = 0;
ac04fee0 2241 if (max_part > 0) {
476a4813
LV
2242 part_shift = fls(max_part);
2243
ac04fee0
NK
2244 /*
2245 * Adjust max_part according to part_shift as it is exported
2246 * to user space so that user can decide correct minor number
2247 * if [s]he want to create more devices.
2248 *
2249 * Note that -1 is required because partition 0 is reserved
2250 * for the whole disk.
2251 */
2252 max_part = (1UL << part_shift) - 1;
2253 }
2254
b1a66504
GC
2255 if ((1UL << part_shift) > DISK_MAX_PARTS) {
2256 err = -EINVAL;
a8c1d064 2257 goto err_out;
b1a66504 2258 }
78f4bb36 2259
b1a66504
GC
2260 if (max_loop > 1UL << (MINORBITS - part_shift)) {
2261 err = -EINVAL;
a8c1d064 2262 goto err_out;
b1a66504 2263 }
1da177e4 2264
a8c1d064
AV
2265 err = misc_register(&loop_misc);
2266 if (err < 0)
2267 goto err_out;
2268
2269
8410d38c 2270 if (__register_blkdev(LOOP_MAJOR, "loop", loop_probe)) {
b1a66504
GC
2271 err = -EIO;
2272 goto misc_out;
2273 }
1da177e4 2274
d134b00b 2275 /* pre-create number of devices given by config or max_loop */
85c50197 2276 for (i = 0; i < max_loop; i++)
d6da83d0 2277 loop_add(i);
34dd82af 2278
73285082 2279 printk(KERN_INFO "loop: module loaded\n");
1da177e4 2280 return 0;
b1a66504
GC
2281
2282misc_out:
2283 misc_deregister(&loop_misc);
a8c1d064 2284err_out:
b1a66504 2285 return err;
34dd82af 2286}
a47653fc 2287
73285082 2288static void __exit loop_exit(void)
1da177e4 2289{
8e60947d
CH
2290 struct loop_device *lo;
2291 int id;
2292
00d59405 2293 unregister_blkdev(LOOP_MAJOR, "loop");
770fe30a 2294 misc_deregister(&loop_misc);
200f9337 2295
1c500ad7
TH
2296 /*
2297 * There is no need to use loop_ctl_mutex here, for nobody else can
2298 * access loop_index_idr when this module is unloading (unless forced
2299 * module unloading is requested). If this is not a clean unloading,
2300 * we have no means to avoid kernel crash.
2301 */
8e60947d
CH
2302 idr_for_each_entry(&loop_index_idr, lo, id)
2303 loop_remove(lo);
bd5c39ed
CH
2304
2305 idr_destroy(&loop_index_idr);
1da177e4
LT
2306}
2307
2308module_init(loop_init);
2309module_exit(loop_exit);
2310
2311#ifndef MODULE
2312static int __init max_loop_setup(char *str)
2313{
2314 max_loop = simple_strtol(str, NULL, 0);
bb5faa99
MFO
2315#ifdef CONFIG_BLOCK_LEGACY_AUTOLOAD
2316 max_loop_specified = true;
2317#endif
1da177e4
LT
2318 return 1;
2319}
2320
2321__setup("max_loop=", max_loop_setup);
2322#endif