Orange Pi5 kernel

Deprecated Linux kernel 5.10.110 for OrangePi 5/5B/5+ boards

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