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