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