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)  * Copyright (C) 2001, 2002 Sistina Software (UK) Limited.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300    3)  * Copyright (C) 2004 - 2006 Red Hat, Inc. All rights reserved.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300    4)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300    5)  * This file is released under the GPL.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300    6)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300    7) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300    8) #include "dm-core.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300    9) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   10) #include <linux/module.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   11) #include <linux/vmalloc.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   12) #include <linux/miscdevice.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   13) #include <linux/sched/mm.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   14) #include <linux/init.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   15) #include <linux/wait.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   16) #include <linux/slab.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   17) #include <linux/dm-ioctl.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   18) #include <linux/hdreg.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   19) #include <linux/compat.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   20) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   21) #include <linux/uaccess.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   22) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   23) #define DM_MSG_PREFIX "ioctl"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   24) #define DM_DRIVER_EMAIL "dm-devel@redhat.com"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   25) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   26) struct dm_file {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   27) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   28) 	 * poll will wait until the global event number is greater than
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   29) 	 * this value.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   30) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   31) 	volatile unsigned global_event_nr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   32) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   33) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   34) /*-----------------------------------------------------------------
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   35)  * The ioctl interface needs to be able to look up devices by
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   36)  * name or uuid.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   37)  *---------------------------------------------------------------*/
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   38) struct hash_cell {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   39) 	struct list_head name_list;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   40) 	struct list_head uuid_list;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   41) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   42) 	char *name;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   43) 	char *uuid;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   44) 	struct mapped_device *md;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   45) 	struct dm_table *new_map;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   46) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   47) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   48) struct vers_iter {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   49)     size_t param_size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   50)     struct dm_target_versions *vers, *old_vers;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   51)     char *end;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   52)     uint32_t flags;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   53) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   54) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   55) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   56) #define NUM_BUCKETS 64
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   57) #define MASK_BUCKETS (NUM_BUCKETS - 1)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   58) static struct list_head _name_buckets[NUM_BUCKETS];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   59) static struct list_head _uuid_buckets[NUM_BUCKETS];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   60) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   61) static void dm_hash_remove_all(bool keep_open_devices, bool mark_deferred, bool only_deferred);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   62) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   63) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   64)  * Guards access to both hash tables.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   65)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   66) static DECLARE_RWSEM(_hash_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   67) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   68) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   69)  * Protects use of mdptr to obtain hash cell name and uuid from mapped device.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   70)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   71) static DEFINE_MUTEX(dm_hash_cells_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   72) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   73) static void init_buckets(struct list_head *buckets)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   74) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   75) 	unsigned int i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   76) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   77) 	for (i = 0; i < NUM_BUCKETS; i++)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   78) 		INIT_LIST_HEAD(buckets + i);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   79) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   80) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   81) static int dm_hash_init(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   82) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   83) 	init_buckets(_name_buckets);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   84) 	init_buckets(_uuid_buckets);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   85) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   86) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   87) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   88) static void dm_hash_exit(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   89) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   90) 	dm_hash_remove_all(false, false, false);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   91) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   92) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   93) /*-----------------------------------------------------------------
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   94)  * Hash function:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   95)  * We're not really concerned with the str hash function being
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   96)  * fast since it's only used by the ioctl interface.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   97)  *---------------------------------------------------------------*/
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   98) static unsigned int hash_str(const char *str)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   99) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  100) 	const unsigned int hash_mult = 2654435387U;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  101) 	unsigned int h = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  102) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  103) 	while (*str)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  104) 		h = (h + (unsigned int) *str++) * hash_mult;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  105) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  106) 	return h & MASK_BUCKETS;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  107) }
^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)  * Code for looking up a device by name
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  111)  *---------------------------------------------------------------*/
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  112) static struct hash_cell *__get_name_cell(const char *str)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  113) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  114) 	struct hash_cell *hc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  115) 	unsigned int h = hash_str(str);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  116) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  117) 	list_for_each_entry (hc, _name_buckets + h, name_list)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  118) 		if (!strcmp(hc->name, str)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  119) 			dm_get(hc->md);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  120) 			return hc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  121) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  122) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  123) 	return NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  124) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  125) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  126) static struct hash_cell *__get_uuid_cell(const char *str)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  127) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  128) 	struct hash_cell *hc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  129) 	unsigned int h = hash_str(str);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  130) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  131) 	list_for_each_entry (hc, _uuid_buckets + h, uuid_list)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  132) 		if (!strcmp(hc->uuid, str)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  133) 			dm_get(hc->md);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  134) 			return hc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  135) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  136) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  137) 	return NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  138) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  139) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  140) static struct hash_cell *__get_dev_cell(uint64_t dev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  141) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  142) 	struct mapped_device *md;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  143) 	struct hash_cell *hc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  144) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  145) 	md = dm_get_md(huge_decode_dev(dev));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  146) 	if (!md)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  147) 		return NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  148) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  149) 	hc = dm_get_mdptr(md);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  150) 	if (!hc) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  151) 		dm_put(md);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  152) 		return NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  153) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  154) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  155) 	return hc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  156) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  157) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  158) /*-----------------------------------------------------------------
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  159)  * Inserting, removing and renaming a device.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  160)  *---------------------------------------------------------------*/
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  161) static struct hash_cell *alloc_cell(const char *name, const char *uuid,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  162) 				    struct mapped_device *md)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  163) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  164) 	struct hash_cell *hc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  165) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  166) 	hc = kmalloc(sizeof(*hc), GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  167) 	if (!hc)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  168) 		return NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  169) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  170) 	hc->name = kstrdup(name, GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  171) 	if (!hc->name) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  172) 		kfree(hc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  173) 		return NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  174) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  175) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  176) 	if (!uuid)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  177) 		hc->uuid = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  178) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  179) 	else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  180) 		hc->uuid = kstrdup(uuid, GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  181) 		if (!hc->uuid) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  182) 			kfree(hc->name);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  183) 			kfree(hc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  184) 			return NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  185) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  186) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  187) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  188) 	INIT_LIST_HEAD(&hc->name_list);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  189) 	INIT_LIST_HEAD(&hc->uuid_list);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  190) 	hc->md = md;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  191) 	hc->new_map = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  192) 	return hc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  193) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  194) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  195) static void free_cell(struct hash_cell *hc)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  196) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  197) 	if (hc) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  198) 		kfree(hc->name);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  199) 		kfree(hc->uuid);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  200) 		kfree(hc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  201) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  202) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  203) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  204) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  205)  * The kdev_t and uuid of a device can never change once it is
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  206)  * initially inserted.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  207)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  208) static int dm_hash_insert(const char *name, const char *uuid, struct mapped_device *md)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  209) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  210) 	struct hash_cell *cell, *hc;
^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) 	 * Allocate the new cells.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  214) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  215) 	cell = alloc_cell(name, uuid, md);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  216) 	if (!cell)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  217) 		return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  218) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  219) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  220) 	 * Insert the cell into both hash tables.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  221) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  222) 	down_write(&_hash_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  223) 	hc = __get_name_cell(name);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  224) 	if (hc) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  225) 		dm_put(hc->md);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  226) 		goto bad;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  227) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  228) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  229) 	list_add(&cell->name_list, _name_buckets + hash_str(name));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  230) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  231) 	if (uuid) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  232) 		hc = __get_uuid_cell(uuid);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  233) 		if (hc) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  234) 			list_del(&cell->name_list);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  235) 			dm_put(hc->md);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  236) 			goto bad;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  237) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  238) 		list_add(&cell->uuid_list, _uuid_buckets + hash_str(uuid));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  239) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  240) 	dm_get(md);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  241) 	mutex_lock(&dm_hash_cells_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  242) 	dm_set_mdptr(md, cell);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  243) 	mutex_unlock(&dm_hash_cells_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  244) 	up_write(&_hash_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  245) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  246) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  247) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  248)  bad:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  249) 	up_write(&_hash_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  250) 	free_cell(cell);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  251) 	return -EBUSY;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  252) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  253) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  254) static struct dm_table *__hash_remove(struct hash_cell *hc)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  255) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  256) 	struct dm_table *table;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  257) 	int srcu_idx;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  258) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  259) 	/* remove from the dev hash */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  260) 	list_del(&hc->uuid_list);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  261) 	list_del(&hc->name_list);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  262) 	mutex_lock(&dm_hash_cells_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  263) 	dm_set_mdptr(hc->md, NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  264) 	mutex_unlock(&dm_hash_cells_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  265) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  266) 	table = dm_get_live_table(hc->md, &srcu_idx);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  267) 	if (table)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  268) 		dm_table_event(table);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  269) 	dm_put_live_table(hc->md, srcu_idx);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  270) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  271) 	table = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  272) 	if (hc->new_map)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  273) 		table = hc->new_map;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  274) 	dm_put(hc->md);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  275) 	free_cell(hc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  276) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  277) 	return table;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  278) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  279) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  280) static void dm_hash_remove_all(bool keep_open_devices, bool mark_deferred, bool only_deferred)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  281) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  282) 	int i, dev_skipped;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  283) 	struct hash_cell *hc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  284) 	struct mapped_device *md;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  285) 	struct dm_table *t;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  286) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  287) retry:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  288) 	dev_skipped = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  289) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  290) 	down_write(&_hash_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  291) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  292) 	for (i = 0; i < NUM_BUCKETS; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  293) 		list_for_each_entry(hc, _name_buckets + i, name_list) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  294) 			md = hc->md;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  295) 			dm_get(md);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  296) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  297) 			if (keep_open_devices &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  298) 			    dm_lock_for_deletion(md, mark_deferred, only_deferred)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  299) 				dm_put(md);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  300) 				dev_skipped++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  301) 				continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  302) 			}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  303) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  304) 			t = __hash_remove(hc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  305) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  306) 			up_write(&_hash_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  307) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  308) 			if (t) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  309) 				dm_sync_table(md);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  310) 				dm_table_destroy(t);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  311) 			}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  312) 			dm_put(md);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  313) 			if (likely(keep_open_devices))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  314) 				dm_destroy(md);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  315) 			else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  316) 				dm_destroy_immediate(md);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  317) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  318) 			/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  319) 			 * Some mapped devices may be using other mapped
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  320) 			 * devices, so repeat until we make no further
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  321) 			 * progress.  If a new mapped device is created
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  322) 			 * here it will also get removed.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  323) 			 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  324) 			goto retry;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  325) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  326) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  327) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  328) 	up_write(&_hash_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  329) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  330) 	if (dev_skipped)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  331) 		DMWARN("remove_all left %d open device(s)", dev_skipped);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  332) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  333) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  334) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  335)  * Set the uuid of a hash_cell that isn't already set.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  336)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  337) static void __set_cell_uuid(struct hash_cell *hc, char *new_uuid)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  338) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  339) 	mutex_lock(&dm_hash_cells_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  340) 	hc->uuid = new_uuid;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  341) 	mutex_unlock(&dm_hash_cells_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  342) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  343) 	list_add(&hc->uuid_list, _uuid_buckets + hash_str(new_uuid));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  344) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  345) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  346) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  347)  * Changes the name of a hash_cell and returns the old name for
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  348)  * the caller to free.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  349)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  350) static char *__change_cell_name(struct hash_cell *hc, char *new_name)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  351) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  352) 	char *old_name;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  353) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  354) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  355) 	 * Rename and move the name cell.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  356) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  357) 	list_del(&hc->name_list);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  358) 	old_name = hc->name;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  359) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  360) 	mutex_lock(&dm_hash_cells_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  361) 	hc->name = new_name;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  362) 	mutex_unlock(&dm_hash_cells_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  363) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  364) 	list_add(&hc->name_list, _name_buckets + hash_str(new_name));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  365) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  366) 	return old_name;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  367) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  368) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  369) static struct mapped_device *dm_hash_rename(struct dm_ioctl *param,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  370) 					    const char *new)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  371) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  372) 	char *new_data, *old_name = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  373) 	struct hash_cell *hc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  374) 	struct dm_table *table;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  375) 	struct mapped_device *md;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  376) 	unsigned change_uuid = (param->flags & DM_UUID_FLAG) ? 1 : 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  377) 	int srcu_idx;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  378) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  379) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  380) 	 * duplicate new.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  381) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  382) 	new_data = kstrdup(new, GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  383) 	if (!new_data)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  384) 		return ERR_PTR(-ENOMEM);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  385) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  386) 	down_write(&_hash_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  387) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  388) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  389) 	 * Is new free ?
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  390) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  391) 	if (change_uuid)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  392) 		hc = __get_uuid_cell(new);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  393) 	else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  394) 		hc = __get_name_cell(new);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  395) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  396) 	if (hc) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  397) 		DMWARN("Unable to change %s on mapped device %s to one that "
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  398) 		       "already exists: %s",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  399) 		       change_uuid ? "uuid" : "name",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  400) 		       param->name, new);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  401) 		dm_put(hc->md);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  402) 		up_write(&_hash_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  403) 		kfree(new_data);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  404) 		return ERR_PTR(-EBUSY);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  405) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  406) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  407) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  408) 	 * Is there such a device as 'old' ?
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  409) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  410) 	hc = __get_name_cell(param->name);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  411) 	if (!hc) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  412) 		DMWARN("Unable to rename non-existent device, %s to %s%s",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  413) 		       param->name, change_uuid ? "uuid " : "", new);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  414) 		up_write(&_hash_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  415) 		kfree(new_data);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  416) 		return ERR_PTR(-ENXIO);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  417) 	}
^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) 	 * Does this device already have a uuid?
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  421) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  422) 	if (change_uuid && hc->uuid) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  423) 		DMWARN("Unable to change uuid of mapped device %s to %s "
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  424) 		       "because uuid is already set to %s",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  425) 		       param->name, new, hc->uuid);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  426) 		dm_put(hc->md);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  427) 		up_write(&_hash_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  428) 		kfree(new_data);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  429) 		return ERR_PTR(-EINVAL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  430) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  431) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  432) 	if (change_uuid)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  433) 		__set_cell_uuid(hc, new_data);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  434) 	else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  435) 		old_name = __change_cell_name(hc, new_data);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  436) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  437) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  438) 	 * Wake up any dm event waiters.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  439) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  440) 	table = dm_get_live_table(hc->md, &srcu_idx);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  441) 	if (table)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  442) 		dm_table_event(table);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  443) 	dm_put_live_table(hc->md, srcu_idx);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  444) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  445) 	if (!dm_kobject_uevent(hc->md, KOBJ_CHANGE, param->event_nr))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  446) 		param->flags |= DM_UEVENT_GENERATED_FLAG;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  447) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  448) 	md = hc->md;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  449) 	up_write(&_hash_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  450) 	kfree(old_name);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  451) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  452) 	return md;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  453) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  454) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  455) void dm_deferred_remove(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  456) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  457) 	dm_hash_remove_all(true, false, true);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  458) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  459) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  460) /*-----------------------------------------------------------------
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  461)  * Implementation of the ioctl commands
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  462)  *---------------------------------------------------------------*/
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  463) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  464)  * All the ioctl commands get dispatched to functions with this
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  465)  * prototype.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  466)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  467) typedef int (*ioctl_fn)(struct file *filp, struct dm_ioctl *param, size_t param_size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  468) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  469) static int remove_all(struct file *filp, struct dm_ioctl *param, size_t param_size)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  470) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  471) 	dm_hash_remove_all(true, !!(param->flags & DM_DEFERRED_REMOVE), false);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  472) 	param->data_size = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  473) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  474) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  475) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  476) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  477)  * Round up the ptr to an 8-byte boundary.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  478)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  479) #define ALIGN_MASK 7
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  480) static inline size_t align_val(size_t val)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  481) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  482) 	return (val + ALIGN_MASK) & ~ALIGN_MASK;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  483) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  484) static inline void *align_ptr(void *ptr)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  485) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  486) 	return (void *)align_val((size_t)ptr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  487) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  488) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  489) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  490)  * Retrieves the data payload buffer from an already allocated
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  491)  * struct dm_ioctl.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  492)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  493) static void *get_result_buffer(struct dm_ioctl *param, size_t param_size,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  494) 			       size_t *len)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  495) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  496) 	param->data_start = align_ptr(param + 1) - (void *) param;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  497) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  498) 	if (param->data_start < param_size)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  499) 		*len = param_size - param->data_start;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  500) 	else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  501) 		*len = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  502) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  503) 	return ((void *) param) + param->data_start;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  504) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  505) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  506) static int list_devices(struct file *filp, struct dm_ioctl *param, size_t param_size)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  507) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  508) 	unsigned int i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  509) 	struct hash_cell *hc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  510) 	size_t len, needed = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  511) 	struct gendisk *disk;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  512) 	struct dm_name_list *orig_nl, *nl, *old_nl = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  513) 	uint32_t *event_nr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  514) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  515) 	down_write(&_hash_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  516) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  517) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  518) 	 * Loop through all the devices working out how much
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  519) 	 * space we need.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  520) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  521) 	for (i = 0; i < NUM_BUCKETS; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  522) 		list_for_each_entry (hc, _name_buckets + i, name_list) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  523) 			needed += align_val(offsetof(struct dm_name_list, name) + strlen(hc->name) + 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  524) 			needed += align_val(sizeof(uint32_t));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  525) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  526) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  527) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  528) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  529) 	 * Grab our output buffer.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  530) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  531) 	nl = orig_nl = get_result_buffer(param, param_size, &len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  532) 	if (len < needed || len < sizeof(nl->dev)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  533) 		param->flags |= DM_BUFFER_FULL_FLAG;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  534) 		goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  535) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  536) 	param->data_size = param->data_start + needed;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  537) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  538) 	nl->dev = 0;	/* Flags no data */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  539) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  540) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  541) 	 * Now loop through filling out the names.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  542) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  543) 	for (i = 0; i < NUM_BUCKETS; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  544) 		list_for_each_entry (hc, _name_buckets + i, name_list) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  545) 			if (old_nl)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  546) 				old_nl->next = (uint32_t) ((void *) nl -
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  547) 							   (void *) old_nl);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  548) 			disk = dm_disk(hc->md);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  549) 			nl->dev = huge_encode_dev(disk_devt(disk));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  550) 			nl->next = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  551) 			strcpy(nl->name, hc->name);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  552) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  553) 			old_nl = nl;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  554) 			event_nr = align_ptr(nl->name + strlen(hc->name) + 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  555) 			*event_nr = dm_get_event_nr(hc->md);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  556) 			nl = align_ptr(event_nr + 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  557) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  558) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  559) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  560) 	 * If mismatch happens, security may be compromised due to buffer
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  561) 	 * overflow, so it's better to crash.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  562) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  563) 	BUG_ON((char *)nl - (char *)orig_nl != needed);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  564) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  565)  out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  566) 	up_write(&_hash_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  567) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  568) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  569) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  570) static void list_version_get_needed(struct target_type *tt, void *needed_param)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  571) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  572)     size_t *needed = needed_param;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  573) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  574)     *needed += sizeof(struct dm_target_versions);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  575)     *needed += strlen(tt->name);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  576)     *needed += ALIGN_MASK;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  577) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  578) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  579) static void list_version_get_info(struct target_type *tt, void *param)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  580) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  581)     struct vers_iter *info = param;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  582) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  583)     /* Check space - it might have changed since the first iteration */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  584)     if ((char *)info->vers + sizeof(tt->version) + strlen(tt->name) + 1 >
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  585) 	info->end) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  586) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  587) 	info->flags = DM_BUFFER_FULL_FLAG;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  588) 	return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  589)     }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  590) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  591)     if (info->old_vers)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  592) 	info->old_vers->next = (uint32_t) ((void *)info->vers -
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  593) 					   (void *)info->old_vers);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  594)     info->vers->version[0] = tt->version[0];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  595)     info->vers->version[1] = tt->version[1];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  596)     info->vers->version[2] = tt->version[2];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  597)     info->vers->next = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  598)     strcpy(info->vers->name, tt->name);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  599) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  600)     info->old_vers = info->vers;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  601)     info->vers = align_ptr(((void *) ++info->vers) + strlen(tt->name) + 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  602) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  603) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  604) static int __list_versions(struct dm_ioctl *param, size_t param_size, const char *name)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  605) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  606) 	size_t len, needed = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  607) 	struct dm_target_versions *vers;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  608) 	struct vers_iter iter_info;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  609) 	struct target_type *tt = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  610) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  611) 	if (name) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  612) 		tt = dm_get_target_type(name);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  613) 		if (!tt)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  614) 			return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  615) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  616) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  617) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  618) 	 * Loop through all the devices working out how much
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  619) 	 * space we need.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  620) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  621) 	if (!tt)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  622) 		dm_target_iterate(list_version_get_needed, &needed);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  623) 	else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  624) 		list_version_get_needed(tt, &needed);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  625) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  626) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  627) 	 * Grab our output buffer.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  628) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  629) 	vers = get_result_buffer(param, param_size, &len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  630) 	if (len < needed) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  631) 		param->flags |= DM_BUFFER_FULL_FLAG;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  632) 		goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  633) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  634) 	param->data_size = param->data_start + needed;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  635) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  636) 	iter_info.param_size = param_size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  637) 	iter_info.old_vers = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  638) 	iter_info.vers = vers;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  639) 	iter_info.flags = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  640) 	iter_info.end = (char *)vers+len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  641) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  642) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  643) 	 * Now loop through filling out the names & versions.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  644) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  645) 	if (!tt)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  646) 		dm_target_iterate(list_version_get_info, &iter_info);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  647) 	else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  648) 		list_version_get_info(tt, &iter_info);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  649) 	param->flags |= iter_info.flags;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  650) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  651)  out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  652) 	if (tt)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  653) 		dm_put_target_type(tt);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  654) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  655) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  656) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  657) static int list_versions(struct file *filp, struct dm_ioctl *param, size_t param_size)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  658) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  659) 	return __list_versions(param, param_size, NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  660) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  661) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  662) static int get_target_version(struct file *filp, struct dm_ioctl *param, size_t param_size)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  663) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  664) 	return __list_versions(param, param_size, param->name);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  665) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  666) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  667) static int check_name(const char *name)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  668) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  669) 	if (strchr(name, '/')) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  670) 		DMWARN("invalid device name");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  671) 		return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  672) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  673) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  674) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  675) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  676) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  677) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  678)  * On successful return, the caller must not attempt to acquire
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  679)  * _hash_lock without first calling dm_put_live_table, because dm_table_destroy
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  680)  * waits for this dm_put_live_table and could be called under this lock.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  681)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  682) static struct dm_table *dm_get_inactive_table(struct mapped_device *md, int *srcu_idx)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  683) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  684) 	struct hash_cell *hc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  685) 	struct dm_table *table = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  686) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  687) 	/* increment rcu count, we don't care about the table pointer */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  688) 	dm_get_live_table(md, srcu_idx);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  689) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  690) 	down_read(&_hash_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  691) 	hc = dm_get_mdptr(md);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  692) 	if (!hc || hc->md != md) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  693) 		DMWARN("device has been removed from the dev hash table.");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  694) 		goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  695) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  696) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  697) 	table = hc->new_map;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  698) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  699) out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  700) 	up_read(&_hash_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  701) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  702) 	return table;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  703) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  704) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  705) static struct dm_table *dm_get_live_or_inactive_table(struct mapped_device *md,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  706) 						      struct dm_ioctl *param,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  707) 						      int *srcu_idx)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  708) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  709) 	return (param->flags & DM_QUERY_INACTIVE_TABLE_FLAG) ?
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  710) 		dm_get_inactive_table(md, srcu_idx) : dm_get_live_table(md, srcu_idx);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  711) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  712) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  713) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  714)  * Fills in a dm_ioctl structure, ready for sending back to
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  715)  * userland.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  716)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  717) static void __dev_status(struct mapped_device *md, struct dm_ioctl *param)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  718) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  719) 	struct gendisk *disk = dm_disk(md);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  720) 	struct dm_table *table;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  721) 	int srcu_idx;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  722) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  723) 	param->flags &= ~(DM_SUSPEND_FLAG | DM_READONLY_FLAG |
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  724) 			  DM_ACTIVE_PRESENT_FLAG | DM_INTERNAL_SUSPEND_FLAG);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  725) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  726) 	if (dm_suspended_md(md))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  727) 		param->flags |= DM_SUSPEND_FLAG;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  728) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  729) 	if (dm_suspended_internally_md(md))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  730) 		param->flags |= DM_INTERNAL_SUSPEND_FLAG;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  731) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  732) 	if (dm_test_deferred_remove_flag(md))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  733) 		param->flags |= DM_DEFERRED_REMOVE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  734) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  735) 	param->dev = huge_encode_dev(disk_devt(disk));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  736) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  737) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  738) 	 * Yes, this will be out of date by the time it gets back
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  739) 	 * to userland, but it is still very useful for
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  740) 	 * debugging.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  741) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  742) 	param->open_count = dm_open_count(md);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  743) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  744) 	param->event_nr = dm_get_event_nr(md);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  745) 	param->target_count = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  746) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  747) 	table = dm_get_live_table(md, &srcu_idx);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  748) 	if (table) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  749) 		if (!(param->flags & DM_QUERY_INACTIVE_TABLE_FLAG)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  750) 			if (get_disk_ro(disk))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  751) 				param->flags |= DM_READONLY_FLAG;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  752) 			param->target_count = dm_table_get_num_targets(table);
^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) 		param->flags |= DM_ACTIVE_PRESENT_FLAG;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  756) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  757) 	dm_put_live_table(md, srcu_idx);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  758) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  759) 	if (param->flags & DM_QUERY_INACTIVE_TABLE_FLAG) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  760) 		int srcu_idx;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  761) 		table = dm_get_inactive_table(md, &srcu_idx);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  762) 		if (table) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  763) 			if (!(dm_table_get_mode(table) & FMODE_WRITE))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  764) 				param->flags |= DM_READONLY_FLAG;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  765) 			param->target_count = dm_table_get_num_targets(table);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  766) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  767) 		dm_put_live_table(md, srcu_idx);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  768) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  769) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  770) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  771) static int dev_create(struct file *filp, struct dm_ioctl *param, size_t param_size)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  772) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  773) 	int r, m = DM_ANY_MINOR;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  774) 	struct mapped_device *md;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  775) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  776) 	r = check_name(param->name);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  777) 	if (r)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  778) 		return r;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  779) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  780) 	if (param->flags & DM_PERSISTENT_DEV_FLAG)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  781) 		m = MINOR(huge_decode_dev(param->dev));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  782) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  783) 	r = dm_create(m, &md);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  784) 	if (r)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  785) 		return r;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  786) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  787) 	r = dm_hash_insert(param->name, *param->uuid ? param->uuid : NULL, md);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  788) 	if (r) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  789) 		dm_put(md);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  790) 		dm_destroy(md);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  791) 		return r;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  792) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  793) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  794) 	param->flags &= ~DM_INACTIVE_PRESENT_FLAG;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  795) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  796) 	__dev_status(md, param);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  797) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  798) 	dm_put(md);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  799) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  800) 	return 0;
^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) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  804)  * Always use UUID for lookups if it's present, otherwise use name or dev.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  805)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  806) static struct hash_cell *__find_device_hash_cell(struct dm_ioctl *param)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  807) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  808) 	struct hash_cell *hc = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  809) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  810) 	if (*param->uuid) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  811) 		if (*param->name || param->dev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  812) 			return NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  813) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  814) 		hc = __get_uuid_cell(param->uuid);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  815) 		if (!hc)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  816) 			return NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  817) 	} else if (*param->name) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  818) 		if (param->dev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  819) 			return NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  820) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  821) 		hc = __get_name_cell(param->name);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  822) 		if (!hc)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  823) 			return NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  824) 	} else if (param->dev) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  825) 		hc = __get_dev_cell(param->dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  826) 		if (!hc)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  827) 			return NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  828) 	} else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  829) 		return NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  830) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  831) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  832) 	 * Sneakily write in both the name and the uuid
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  833) 	 * while we have the cell.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  834) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  835) 	strlcpy(param->name, hc->name, sizeof(param->name));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  836) 	if (hc->uuid)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  837) 		strlcpy(param->uuid, hc->uuid, sizeof(param->uuid));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  838) 	else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  839) 		param->uuid[0] = '\0';
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  840) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  841) 	if (hc->new_map)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  842) 		param->flags |= DM_INACTIVE_PRESENT_FLAG;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  843) 	else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  844) 		param->flags &= ~DM_INACTIVE_PRESENT_FLAG;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  845) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  846) 	return hc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  847) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  848) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  849) static struct mapped_device *find_device(struct dm_ioctl *param)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  850) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  851) 	struct hash_cell *hc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  852) 	struct mapped_device *md = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  853) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  854) 	down_read(&_hash_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  855) 	hc = __find_device_hash_cell(param);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  856) 	if (hc)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  857) 		md = hc->md;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  858) 	up_read(&_hash_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  859) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  860) 	return md;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  861) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  862) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  863) static int dev_remove(struct file *filp, struct dm_ioctl *param, size_t param_size)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  864) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  865) 	struct hash_cell *hc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  866) 	struct mapped_device *md;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  867) 	int r;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  868) 	struct dm_table *t;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  869) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  870) 	down_write(&_hash_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  871) 	hc = __find_device_hash_cell(param);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  872) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  873) 	if (!hc) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  874) 		DMDEBUG_LIMIT("device doesn't appear to be in the dev hash table.");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  875) 		up_write(&_hash_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  876) 		return -ENXIO;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  877) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  878) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  879) 	md = hc->md;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  880) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  881) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  882) 	 * Ensure the device is not open and nothing further can open it.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  883) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  884) 	r = dm_lock_for_deletion(md, !!(param->flags & DM_DEFERRED_REMOVE), false);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  885) 	if (r) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  886) 		if (r == -EBUSY && param->flags & DM_DEFERRED_REMOVE) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  887) 			up_write(&_hash_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  888) 			dm_put(md);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  889) 			return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  890) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  891) 		DMDEBUG_LIMIT("unable to remove open device %s", hc->name);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  892) 		up_write(&_hash_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  893) 		dm_put(md);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  894) 		return r;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  895) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  896) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  897) 	t = __hash_remove(hc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  898) 	up_write(&_hash_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  899) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  900) 	if (t) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  901) 		dm_sync_table(md);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  902) 		dm_table_destroy(t);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  903) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  904) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  905) 	param->flags &= ~DM_DEFERRED_REMOVE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  906) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  907) 	if (!dm_kobject_uevent(md, KOBJ_REMOVE, param->event_nr))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  908) 		param->flags |= DM_UEVENT_GENERATED_FLAG;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  909) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  910) 	dm_put(md);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  911) 	dm_destroy(md);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  912) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  913) }
^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)  * Check a string doesn't overrun the chunk of
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  917)  * memory we copied from userland.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  918)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  919) static int invalid_str(char *str, void *end)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  920) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  921) 	while ((void *) str < end)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  922) 		if (!*str++)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  923) 			return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  924) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  925) 	return -EINVAL;
^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 dev_rename(struct file *filp, struct dm_ioctl *param, size_t param_size)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  929) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  930) 	int r;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  931) 	char *new_data = (char *) param + param->data_start;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  932) 	struct mapped_device *md;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  933) 	unsigned change_uuid = (param->flags & DM_UUID_FLAG) ? 1 : 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  934) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  935) 	if (new_data < param->data ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  936) 	    invalid_str(new_data, (void *) param + param_size) || !*new_data ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  937) 	    strlen(new_data) > (change_uuid ? DM_UUID_LEN - 1 : DM_NAME_LEN - 1)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  938) 		DMWARN("Invalid new mapped device name or uuid string supplied.");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  939) 		return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  940) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  941) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  942) 	if (!change_uuid) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  943) 		r = check_name(new_data);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  944) 		if (r)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  945) 			return r;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  946) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  947) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  948) 	md = dm_hash_rename(param, new_data);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  949) 	if (IS_ERR(md))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  950) 		return PTR_ERR(md);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  951) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  952) 	__dev_status(md, param);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  953) 	dm_put(md);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  954) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  955) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  956) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  957) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  958) static int dev_set_geometry(struct file *filp, struct dm_ioctl *param, size_t param_size)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  959) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  960) 	int r = -EINVAL, x;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  961) 	struct mapped_device *md;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  962) 	struct hd_geometry geometry;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  963) 	unsigned long indata[4];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  964) 	char *geostr = (char *) param + param->data_start;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  965) 	char dummy;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  966) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  967) 	md = find_device(param);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  968) 	if (!md)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  969) 		return -ENXIO;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  970) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  971) 	if (geostr < param->data ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  972) 	    invalid_str(geostr, (void *) param + param_size)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  973) 		DMWARN("Invalid geometry supplied.");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  974) 		goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  975) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  976) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  977) 	x = sscanf(geostr, "%lu %lu %lu %lu%c", indata,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  978) 		   indata + 1, indata + 2, indata + 3, &dummy);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  979) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  980) 	if (x != 4) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  981) 		DMWARN("Unable to interpret geometry settings.");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  982) 		goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  983) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  984) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  985) 	if (indata[0] > 65535 || indata[1] > 255 ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  986) 	    indata[2] > 255 || indata[3] > ULONG_MAX) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  987) 		DMWARN("Geometry exceeds range limits.");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  988) 		goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  989) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  990) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  991) 	geometry.cylinders = indata[0];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  992) 	geometry.heads = indata[1];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  993) 	geometry.sectors = indata[2];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  994) 	geometry.start = indata[3];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  995) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  996) 	r = dm_set_geometry(md, &geometry);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  997) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  998) 	param->data_size = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  999) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1000) out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1001) 	dm_put(md);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1002) 	return r;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1003) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1004) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1005) static int do_suspend(struct dm_ioctl *param)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1006) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1007) 	int r = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1008) 	unsigned suspend_flags = DM_SUSPEND_LOCKFS_FLAG;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1009) 	struct mapped_device *md;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1010) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1011) 	md = find_device(param);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1012) 	if (!md)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1013) 		return -ENXIO;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1014) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1015) 	if (param->flags & DM_SKIP_LOCKFS_FLAG)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1016) 		suspend_flags &= ~DM_SUSPEND_LOCKFS_FLAG;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1017) 	if (param->flags & DM_NOFLUSH_FLAG)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1018) 		suspend_flags |= DM_SUSPEND_NOFLUSH_FLAG;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1019) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1020) 	if (!dm_suspended_md(md)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1021) 		r = dm_suspend(md, suspend_flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1022) 		if (r)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1023) 			goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1024) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1025) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1026) 	__dev_status(md, param);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1027) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1028) out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1029) 	dm_put(md);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1030) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1031) 	return r;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1032) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1033) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1034) static int do_resume(struct dm_ioctl *param)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1035) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1036) 	int r = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1037) 	unsigned suspend_flags = DM_SUSPEND_LOCKFS_FLAG;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1038) 	struct hash_cell *hc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1039) 	struct mapped_device *md;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1040) 	struct dm_table *new_map, *old_map = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1041) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1042) 	down_write(&_hash_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1043) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1044) 	hc = __find_device_hash_cell(param);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1045) 	if (!hc) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1046) 		DMDEBUG_LIMIT("device doesn't appear to be in the dev hash table.");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1047) 		up_write(&_hash_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1048) 		return -ENXIO;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1049) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1050) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1051) 	md = hc->md;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1052) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1053) 	new_map = hc->new_map;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1054) 	hc->new_map = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1055) 	param->flags &= ~DM_INACTIVE_PRESENT_FLAG;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1056) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1057) 	up_write(&_hash_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1058) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1059) 	/* Do we need to load a new map ? */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1060) 	if (new_map) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1061) 		/* Suspend if it isn't already suspended */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1062) 		if (param->flags & DM_SKIP_LOCKFS_FLAG)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1063) 			suspend_flags &= ~DM_SUSPEND_LOCKFS_FLAG;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1064) 		if (param->flags & DM_NOFLUSH_FLAG)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1065) 			suspend_flags |= DM_SUSPEND_NOFLUSH_FLAG;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1066) 		if (!dm_suspended_md(md))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1067) 			dm_suspend(md, suspend_flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1068) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1069) 		old_map = dm_swap_table(md, new_map);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1070) 		if (IS_ERR(old_map)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1071) 			dm_sync_table(md);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1072) 			dm_table_destroy(new_map);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1073) 			dm_put(md);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1074) 			return PTR_ERR(old_map);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1075) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1076) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1077) 		if (dm_table_get_mode(new_map) & FMODE_WRITE)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1078) 			set_disk_ro(dm_disk(md), 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1079) 		else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1080) 			set_disk_ro(dm_disk(md), 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1081) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1082) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1083) 	if (dm_suspended_md(md)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1084) 		r = dm_resume(md);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1085) 		if (!r && !dm_kobject_uevent(md, KOBJ_CHANGE, param->event_nr))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1086) 			param->flags |= DM_UEVENT_GENERATED_FLAG;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1087) 	}
^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) 	 * Since dm_swap_table synchronizes RCU, nobody should be in
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1091) 	 * read-side critical section already.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1092) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1093) 	if (old_map)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1094) 		dm_table_destroy(old_map);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1095) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1096) 	if (!r)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1097) 		__dev_status(md, param);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1098) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1099) 	dm_put(md);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1100) 	return r;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1101) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1102) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1103) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1104)  * Set or unset the suspension state of a device.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1105)  * If the device already is in the requested state we just return its status.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1106)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1107) static int dev_suspend(struct file *filp, struct dm_ioctl *param, size_t param_size)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1108) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1109) 	if (param->flags & DM_SUSPEND_FLAG)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1110) 		return do_suspend(param);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1111) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1112) 	return do_resume(param);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1113) }
^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)  * Copies device info back to user space, used by
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1117)  * the create and info ioctls.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1118)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1119) static int dev_status(struct file *filp, struct dm_ioctl *param, size_t param_size)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1120) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1121) 	struct mapped_device *md;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1122) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1123) 	md = find_device(param);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1124) 	if (!md)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1125) 		return -ENXIO;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1126) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1127) 	__dev_status(md, param);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1128) 	dm_put(md);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1129) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1130) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1131) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1132) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1133) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1134)  * Build up the status struct for each target
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1135)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1136) static void retrieve_status(struct dm_table *table,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1137) 			    struct dm_ioctl *param, size_t param_size)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1138) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1139) 	unsigned int i, num_targets;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1140) 	struct dm_target_spec *spec;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1141) 	char *outbuf, *outptr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1142) 	status_type_t type;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1143) 	size_t remaining, len, used = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1144) 	unsigned status_flags = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1145) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1146) 	outptr = outbuf = get_result_buffer(param, param_size, &len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1147) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1148) 	if (param->flags & DM_STATUS_TABLE_FLAG)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1149) 		type = STATUSTYPE_TABLE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1150) 	else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1151) 		type = STATUSTYPE_INFO;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1152) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1153) 	/* Get all the target info */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1154) 	num_targets = dm_table_get_num_targets(table);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1155) 	for (i = 0; i < num_targets; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1156) 		struct dm_target *ti = dm_table_get_target(table, i);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1157) 		size_t l;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1158) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1159) 		remaining = len - (outptr - outbuf);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1160) 		if (remaining <= sizeof(struct dm_target_spec)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1161) 			param->flags |= DM_BUFFER_FULL_FLAG;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1162) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1163) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1164) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1165) 		spec = (struct dm_target_spec *) outptr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1166) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1167) 		spec->status = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1168) 		spec->sector_start = ti->begin;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1169) 		spec->length = ti->len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1170) 		strncpy(spec->target_type, ti->type->name,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1171) 			sizeof(spec->target_type) - 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1172) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1173) 		outptr += sizeof(struct dm_target_spec);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1174) 		remaining = len - (outptr - outbuf);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1175) 		if (remaining <= 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1176) 			param->flags |= DM_BUFFER_FULL_FLAG;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1177) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1178) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1179) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1180) 		/* Get the status/table string from the target driver */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1181) 		if (ti->type->status) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1182) 			if (param->flags & DM_NOFLUSH_FLAG)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1183) 				status_flags |= DM_STATUS_NOFLUSH_FLAG;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1184) 			ti->type->status(ti, type, status_flags, outptr, remaining);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1185) 		} else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1186) 			outptr[0] = '\0';
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1187) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1188) 		l = strlen(outptr) + 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1189) 		if (l == remaining) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1190) 			param->flags |= DM_BUFFER_FULL_FLAG;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1191) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1192) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1193) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1194) 		outptr += l;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1195) 		used = param->data_start + (outptr - outbuf);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1196) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1197) 		outptr = align_ptr(outptr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1198) 		spec->next = outptr - outbuf;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1199) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1200) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1201) 	if (used)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1202) 		param->data_size = used;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1203) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1204) 	param->target_count = num_targets;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1205) }
^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)  * Wait for a device to report an event
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1209)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1210) static int dev_wait(struct file *filp, struct dm_ioctl *param, size_t param_size)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1211) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1212) 	int r = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1213) 	struct mapped_device *md;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1214) 	struct dm_table *table;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1215) 	int srcu_idx;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1216) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1217) 	md = find_device(param);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1218) 	if (!md)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1219) 		return -ENXIO;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1220) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1221) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1222) 	 * Wait for a notification event
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1223) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1224) 	if (dm_wait_event(md, param->event_nr)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1225) 		r = -ERESTARTSYS;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1226) 		goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1227) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1228) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1229) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1230) 	 * The userland program is going to want to know what
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1231) 	 * changed to trigger the event, so we may as well tell
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1232) 	 * him and save an ioctl.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1233) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1234) 	__dev_status(md, param);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1235) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1236) 	table = dm_get_live_or_inactive_table(md, param, &srcu_idx);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1237) 	if (table)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1238) 		retrieve_status(table, param, param_size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1239) 	dm_put_live_table(md, srcu_idx);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1240) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1241) out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1242) 	dm_put(md);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1243) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1244) 	return r;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1245) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1246) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1247) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1248)  * Remember the global event number and make it possible to poll
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1249)  * for further events.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1250)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1251) static int dev_arm_poll(struct file *filp, struct dm_ioctl *param, size_t param_size)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1252) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1253) 	struct dm_file *priv = filp->private_data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1254) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1255) 	priv->global_event_nr = atomic_read(&dm_global_event_nr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1256) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1257) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1258) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1259) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1260) static inline fmode_t get_mode(struct dm_ioctl *param)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1261) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1262) 	fmode_t mode = FMODE_READ | FMODE_WRITE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1263) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1264) 	if (param->flags & DM_READONLY_FLAG)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1265) 		mode = FMODE_READ;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1266) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1267) 	return mode;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1268) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1269) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1270) static int next_target(struct dm_target_spec *last, uint32_t next, void *end,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1271) 		       struct dm_target_spec **spec, char **target_params)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1272) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1273) 	*spec = (struct dm_target_spec *) ((unsigned char *) last + next);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1274) 	*target_params = (char *) (*spec + 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1275) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1276) 	if (*spec < (last + 1))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1277) 		return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1278) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1279) 	return invalid_str(*target_params, end);
^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) static int populate_table(struct dm_table *table,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1283) 			  struct dm_ioctl *param, size_t param_size)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1284) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1285) 	int r;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1286) 	unsigned int i = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1287) 	struct dm_target_spec *spec = (struct dm_target_spec *) param;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1288) 	uint32_t next = param->data_start;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1289) 	void *end = (void *) param + param_size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1290) 	char *target_params;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1291) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1292) 	if (!param->target_count) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1293) 		DMWARN("populate_table: no targets specified");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1294) 		return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1295) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1296) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1297) 	for (i = 0; i < param->target_count; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1298) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1299) 		r = next_target(spec, next, end, &spec, &target_params);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1300) 		if (r) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1301) 			DMWARN("unable to find target");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1302) 			return r;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1303) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1304) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1305) 		r = dm_table_add_target(table, spec->target_type,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1306) 					(sector_t) spec->sector_start,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1307) 					(sector_t) spec->length,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1308) 					target_params);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1309) 		if (r) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1310) 			DMWARN("error adding target to table");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1311) 			return r;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1312) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1313) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1314) 		next = spec->next;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1315) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1316) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1317) 	return dm_table_complete(table);
^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) static bool is_valid_type(enum dm_queue_mode cur, enum dm_queue_mode new)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1321) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1322) 	if (cur == new ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1323) 	    (cur == DM_TYPE_BIO_BASED && new == DM_TYPE_DAX_BIO_BASED))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1324) 		return true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1325) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1326) 	return false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1327) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1328) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1329) static int table_load(struct file *filp, struct dm_ioctl *param, size_t param_size)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1330) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1331) 	int r;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1332) 	struct hash_cell *hc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1333) 	struct dm_table *t, *old_map = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1334) 	struct mapped_device *md;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1335) 	struct target_type *immutable_target_type;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1336) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1337) 	md = find_device(param);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1338) 	if (!md)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1339) 		return -ENXIO;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1340) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1341) 	r = dm_table_create(&t, get_mode(param), param->target_count, md);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1342) 	if (r)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1343) 		goto err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1344) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1345) 	/* Protect md->type and md->queue against concurrent table loads. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1346) 	dm_lock_md_type(md);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1347) 	r = populate_table(t, param, param_size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1348) 	if (r)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1349) 		goto err_unlock_md_type;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1350) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1351) 	immutable_target_type = dm_get_immutable_target_type(md);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1352) 	if (immutable_target_type &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1353) 	    (immutable_target_type != dm_table_get_immutable_target_type(t)) &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1354) 	    !dm_table_get_wildcard_target(t)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1355) 		DMWARN("can't replace immutable target type %s",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1356) 		       immutable_target_type->name);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1357) 		r = -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1358) 		goto err_unlock_md_type;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1359) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1360) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1361) 	if (dm_get_md_type(md) == DM_TYPE_NONE) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1362) 		/* Initial table load: acquire type of table. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1363) 		dm_set_md_type(md, dm_table_get_type(t));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1364) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1365) 		/* setup md->queue to reflect md's type (may block) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1366) 		r = dm_setup_md_queue(md, t);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1367) 		if (r) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1368) 			DMWARN("unable to set up device queue for new table.");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1369) 			goto err_unlock_md_type;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1370) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1371) 	} else if (!is_valid_type(dm_get_md_type(md), dm_table_get_type(t))) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1372) 		DMWARN("can't change device type (old=%u vs new=%u) after initial table load.",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1373) 		       dm_get_md_type(md), dm_table_get_type(t));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1374) 		r = -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1375) 		goto err_unlock_md_type;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1376) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1377) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1378) 	dm_unlock_md_type(md);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1379) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1380) 	/* stage inactive table */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1381) 	down_write(&_hash_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1382) 	hc = dm_get_mdptr(md);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1383) 	if (!hc || hc->md != md) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1384) 		DMWARN("device has been removed from the dev hash table.");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1385) 		up_write(&_hash_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1386) 		r = -ENXIO;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1387) 		goto err_destroy_table;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1388) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1389) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1390) 	if (hc->new_map)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1391) 		old_map = hc->new_map;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1392) 	hc->new_map = t;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1393) 	up_write(&_hash_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1394) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1395) 	param->flags |= DM_INACTIVE_PRESENT_FLAG;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1396) 	__dev_status(md, param);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1397) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1398) 	if (old_map) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1399) 		dm_sync_table(md);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1400) 		dm_table_destroy(old_map);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1401) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1402) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1403) 	dm_put(md);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1404) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1405) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1406) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1407) err_unlock_md_type:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1408) 	dm_unlock_md_type(md);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1409) err_destroy_table:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1410) 	dm_table_destroy(t);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1411) err:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1412) 	dm_put(md);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1413) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1414) 	return r;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1415) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1416) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1417) static int table_clear(struct file *filp, struct dm_ioctl *param, size_t param_size)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1418) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1419) 	struct hash_cell *hc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1420) 	struct mapped_device *md;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1421) 	struct dm_table *old_map = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1422) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1423) 	down_write(&_hash_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1424) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1425) 	hc = __find_device_hash_cell(param);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1426) 	if (!hc) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1427) 		DMDEBUG_LIMIT("device doesn't appear to be in the dev hash table.");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1428) 		up_write(&_hash_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1429) 		return -ENXIO;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1430) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1431) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1432) 	if (hc->new_map) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1433) 		old_map = hc->new_map;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1434) 		hc->new_map = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1435) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1436) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1437) 	param->flags &= ~DM_INACTIVE_PRESENT_FLAG;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1438) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1439) 	__dev_status(hc->md, param);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1440) 	md = hc->md;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1441) 	up_write(&_hash_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1442) 	if (old_map) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1443) 		dm_sync_table(md);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1444) 		dm_table_destroy(old_map);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1445) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1446) 	dm_put(md);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1447) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1448) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1449) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1450) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1451) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1452)  * Retrieves a list of devices used by a particular dm device.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1453)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1454) static void retrieve_deps(struct dm_table *table,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1455) 			  struct dm_ioctl *param, size_t param_size)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1456) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1457) 	unsigned int count = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1458) 	struct list_head *tmp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1459) 	size_t len, needed;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1460) 	struct dm_dev_internal *dd;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1461) 	struct dm_target_deps *deps;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1462) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1463) 	deps = get_result_buffer(param, param_size, &len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1464) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1465) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1466) 	 * Count the devices.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1467) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1468) 	list_for_each (tmp, dm_table_get_devices(table))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1469) 		count++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1470) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1471) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1472) 	 * Check we have enough space.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1473) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1474) 	needed = struct_size(deps, dev, count);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1475) 	if (len < needed) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1476) 		param->flags |= DM_BUFFER_FULL_FLAG;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1477) 		return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1478) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1479) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1480) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1481) 	 * Fill in the devices.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1482) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1483) 	deps->count = count;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1484) 	count = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1485) 	list_for_each_entry (dd, dm_table_get_devices(table), list)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1486) 		deps->dev[count++] = huge_encode_dev(dd->dm_dev->bdev->bd_dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1487) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1488) 	param->data_size = param->data_start + needed;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1489) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1490) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1491) static int table_deps(struct file *filp, struct dm_ioctl *param, size_t param_size)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1492) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1493) 	struct mapped_device *md;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1494) 	struct dm_table *table;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1495) 	int srcu_idx;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1496) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1497) 	md = find_device(param);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1498) 	if (!md)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1499) 		return -ENXIO;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1500) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1501) 	__dev_status(md, param);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1502) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1503) 	table = dm_get_live_or_inactive_table(md, param, &srcu_idx);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1504) 	if (table)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1505) 		retrieve_deps(table, param, param_size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1506) 	dm_put_live_table(md, srcu_idx);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1507) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1508) 	dm_put(md);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1509) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1510) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1511) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1512) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1513) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1514)  * Return the status of a device as a text string for each
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1515)  * target.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1516)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1517) static int table_status(struct file *filp, struct dm_ioctl *param, size_t param_size)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1518) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1519) 	struct mapped_device *md;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1520) 	struct dm_table *table;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1521) 	int srcu_idx;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1522) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1523) 	md = find_device(param);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1524) 	if (!md)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1525) 		return -ENXIO;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1526) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1527) 	__dev_status(md, param);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1528) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1529) 	table = dm_get_live_or_inactive_table(md, param, &srcu_idx);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1530) 	if (table)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1531) 		retrieve_status(table, param, param_size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1532) 	dm_put_live_table(md, srcu_idx);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1533) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1534) 	dm_put(md);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1535) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1536) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1537) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1538) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1539) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1540)  * Process device-mapper dependent messages.  Messages prefixed with '@'
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1541)  * are processed by the DM core.  All others are delivered to the target.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1542)  * Returns a number <= 1 if message was processed by device mapper.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1543)  * Returns 2 if message should be delivered to the target.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1544)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1545) static int message_for_md(struct mapped_device *md, unsigned argc, char **argv,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1546) 			  char *result, unsigned maxlen)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1547) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1548) 	int r;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1549) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1550) 	if (**argv != '@')
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1551) 		return 2; /* no '@' prefix, deliver to target */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1552) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1553) 	if (!strcasecmp(argv[0], "@cancel_deferred_remove")) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1554) 		if (argc != 1) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1555) 			DMERR("Invalid arguments for @cancel_deferred_remove");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1556) 			return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1557) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1558) 		return dm_cancel_deferred_remove(md);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1559) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1560) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1561) 	r = dm_stats_message(md, argc, argv, result, maxlen);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1562) 	if (r < 2)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1563) 		return r;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1564) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1565) 	DMERR("Unsupported message sent to DM core: %s", argv[0]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1566) 	return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1567) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1568) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1569) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1570)  * Pass a message to the target that's at the supplied device offset.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1571)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1572) static int target_message(struct file *filp, struct dm_ioctl *param, size_t param_size)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1573) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1574) 	int r, argc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1575) 	char **argv;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1576) 	struct mapped_device *md;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1577) 	struct dm_table *table;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1578) 	struct dm_target *ti;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1579) 	struct dm_target_msg *tmsg = (void *) param + param->data_start;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1580) 	size_t maxlen;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1581) 	char *result = get_result_buffer(param, param_size, &maxlen);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1582) 	int srcu_idx;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1583) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1584) 	md = find_device(param);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1585) 	if (!md)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1586) 		return -ENXIO;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1587) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1588) 	if (tmsg < (struct dm_target_msg *) param->data ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1589) 	    invalid_str(tmsg->message, (void *) param + param_size)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1590) 		DMWARN("Invalid target message parameters.");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1591) 		r = -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1592) 		goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1593) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1594) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1595) 	r = dm_split_args(&argc, &argv, tmsg->message);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1596) 	if (r) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1597) 		DMWARN("Failed to split target message parameters");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1598) 		goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1599) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1600) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1601) 	if (!argc) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1602) 		DMWARN("Empty message received.");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1603) 		r = -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1604) 		goto out_argv;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1605) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1606) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1607) 	r = message_for_md(md, argc, argv, result, maxlen);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1608) 	if (r <= 1)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1609) 		goto out_argv;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1610) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1611) 	table = dm_get_live_table(md, &srcu_idx);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1612) 	if (!table)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1613) 		goto out_table;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1614) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1615) 	if (dm_deleting_md(md)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1616) 		r = -ENXIO;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1617) 		goto out_table;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1618) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1619) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1620) 	ti = dm_table_find_target(table, tmsg->sector);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1621) 	if (!ti) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1622) 		DMWARN("Target message sector outside device.");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1623) 		r = -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1624) 	} else if (ti->type->message)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1625) 		r = ti->type->message(ti, argc, argv, result, maxlen);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1626) 	else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1627) 		DMWARN("Target type does not support messages");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1628) 		r = -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1629) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1630) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1631)  out_table:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1632) 	dm_put_live_table(md, srcu_idx);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1633)  out_argv:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1634) 	kfree(argv);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1635)  out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1636) 	if (r >= 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1637) 		__dev_status(md, param);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1638) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1639) 	if (r == 1) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1640) 		param->flags |= DM_DATA_OUT_FLAG;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1641) 		if (dm_message_test_buffer_overflow(result, maxlen))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1642) 			param->flags |= DM_BUFFER_FULL_FLAG;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1643) 		else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1644) 			param->data_size = param->data_start + strlen(result) + 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1645) 		r = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1646) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1647) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1648) 	dm_put(md);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1649) 	return r;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1650) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1651) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1652) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1653)  * The ioctl parameter block consists of two parts, a dm_ioctl struct
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1654)  * followed by a data buffer.  This flag is set if the second part,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1655)  * which has a variable size, is not used by the function processing
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1656)  * the ioctl.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1657)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1658) #define IOCTL_FLAGS_NO_PARAMS		1
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1659) #define IOCTL_FLAGS_ISSUE_GLOBAL_EVENT	2
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1660) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1661) /*-----------------------------------------------------------------
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1662)  * Implementation of open/close/ioctl on the special char
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1663)  * device.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1664)  *---------------------------------------------------------------*/
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1665) static ioctl_fn lookup_ioctl(unsigned int cmd, int *ioctl_flags)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1666) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1667) 	static const struct {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1668) 		int cmd;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1669) 		int flags;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1670) 		ioctl_fn fn;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1671) 	} _ioctls[] = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1672) 		{DM_VERSION_CMD, 0, NULL}, /* version is dealt with elsewhere */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1673) 		{DM_REMOVE_ALL_CMD, IOCTL_FLAGS_NO_PARAMS | IOCTL_FLAGS_ISSUE_GLOBAL_EVENT, remove_all},
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1674) 		{DM_LIST_DEVICES_CMD, 0, list_devices},
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1675) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1676) 		{DM_DEV_CREATE_CMD, IOCTL_FLAGS_NO_PARAMS | IOCTL_FLAGS_ISSUE_GLOBAL_EVENT, dev_create},
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1677) 		{DM_DEV_REMOVE_CMD, IOCTL_FLAGS_NO_PARAMS | IOCTL_FLAGS_ISSUE_GLOBAL_EVENT, dev_remove},
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1678) 		{DM_DEV_RENAME_CMD, IOCTL_FLAGS_ISSUE_GLOBAL_EVENT, dev_rename},
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1679) 		{DM_DEV_SUSPEND_CMD, IOCTL_FLAGS_NO_PARAMS, dev_suspend},
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1680) 		{DM_DEV_STATUS_CMD, IOCTL_FLAGS_NO_PARAMS, dev_status},
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1681) 		{DM_DEV_WAIT_CMD, 0, dev_wait},
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1682) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1683) 		{DM_TABLE_LOAD_CMD, 0, table_load},
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1684) 		{DM_TABLE_CLEAR_CMD, IOCTL_FLAGS_NO_PARAMS, table_clear},
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1685) 		{DM_TABLE_DEPS_CMD, 0, table_deps},
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1686) 		{DM_TABLE_STATUS_CMD, 0, table_status},
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1687) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1688) 		{DM_LIST_VERSIONS_CMD, 0, list_versions},
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1689) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1690) 		{DM_TARGET_MSG_CMD, 0, target_message},
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1691) 		{DM_DEV_SET_GEOMETRY_CMD, 0, dev_set_geometry},
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1692) 		{DM_DEV_ARM_POLL, IOCTL_FLAGS_NO_PARAMS, dev_arm_poll},
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1693) 		{DM_GET_TARGET_VERSION, 0, get_target_version},
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1694) 	};
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1695) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1696) 	if (unlikely(cmd >= ARRAY_SIZE(_ioctls)))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1697) 		return NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1698) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1699) 	*ioctl_flags = _ioctls[cmd].flags;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1700) 	return _ioctls[cmd].fn;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1701) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1702) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1703) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1704)  * As well as checking the version compatibility this always
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1705)  * copies the kernel interface version out.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1706)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1707) static int check_version(unsigned int cmd, struct dm_ioctl __user *user)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1708) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1709) 	uint32_t version[3];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1710) 	int r = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1711) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1712) 	if (copy_from_user(version, user->version, sizeof(version)))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1713) 		return -EFAULT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1714) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1715) 	if ((DM_VERSION_MAJOR != version[0]) ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1716) 	    (DM_VERSION_MINOR < version[1])) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1717) 		DMWARN("ioctl interface mismatch: "
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1718) 		       "kernel(%u.%u.%u), user(%u.%u.%u), cmd(%d)",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1719) 		       DM_VERSION_MAJOR, DM_VERSION_MINOR,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1720) 		       DM_VERSION_PATCHLEVEL,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1721) 		       version[0], version[1], version[2], cmd);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1722) 		r = -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1723) 	}
^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) 	 * Fill in the kernel version.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1727) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1728) 	version[0] = DM_VERSION_MAJOR;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1729) 	version[1] = DM_VERSION_MINOR;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1730) 	version[2] = DM_VERSION_PATCHLEVEL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1731) 	if (copy_to_user(user->version, version, sizeof(version)))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1732) 		return -EFAULT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1733) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1734) 	return r;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1735) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1736) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1737) #define DM_PARAMS_MALLOC	0x0001	/* Params allocated with kvmalloc() */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1738) #define DM_WIPE_BUFFER		0x0010	/* Wipe input buffer before returning from ioctl */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1739) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1740) static void free_params(struct dm_ioctl *param, size_t param_size, int param_flags)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1741) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1742) 	if (param_flags & DM_WIPE_BUFFER)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1743) 		memset(param, 0, param_size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1744) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1745) 	if (param_flags & DM_PARAMS_MALLOC)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1746) 		kvfree(param);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1747) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1748) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1749) static int copy_params(struct dm_ioctl __user *user, struct dm_ioctl *param_kernel,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1750) 		       int ioctl_flags, struct dm_ioctl **param, int *param_flags)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1751) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1752) 	struct dm_ioctl *dmi;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1753) 	int secure_data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1754) 	const size_t minimum_data_size = offsetof(struct dm_ioctl, data);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1755) 	unsigned noio_flag;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1756) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1757) 	if (copy_from_user(param_kernel, user, minimum_data_size))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1758) 		return -EFAULT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1759) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1760) 	if (param_kernel->data_size < minimum_data_size)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1761) 		return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1762) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1763) 	secure_data = param_kernel->flags & DM_SECURE_DATA_FLAG;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1764) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1765) 	*param_flags = secure_data ? DM_WIPE_BUFFER : 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1766) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1767) 	if (ioctl_flags & IOCTL_FLAGS_NO_PARAMS) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1768) 		dmi = param_kernel;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1769) 		dmi->data_size = minimum_data_size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1770) 		goto data_copied;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1771) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1772) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1773) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1774) 	 * Use __GFP_HIGH to avoid low memory issues when a device is
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1775) 	 * suspended and the ioctl is needed to resume it.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1776) 	 * Use kmalloc() rather than vmalloc() when we can.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1777) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1778) 	dmi = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1779) 	noio_flag = memalloc_noio_save();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1780) 	dmi = kvmalloc(param_kernel->data_size, GFP_KERNEL | __GFP_HIGH);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1781) 	memalloc_noio_restore(noio_flag);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1782) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1783) 	if (!dmi) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1784) 		if (secure_data && clear_user(user, param_kernel->data_size))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1785) 			return -EFAULT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1786) 		return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1787) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1788) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1789) 	*param_flags |= DM_PARAMS_MALLOC;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1790) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1791) 	/* Copy from param_kernel (which was already copied from user) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1792) 	memcpy(dmi, param_kernel, minimum_data_size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1793) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1794) 	if (copy_from_user(&dmi->data, (char __user *)user + minimum_data_size,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1795) 			   param_kernel->data_size - minimum_data_size))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1796) 		goto bad;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1797) data_copied:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1798) 	/* Wipe the user buffer so we do not return it to userspace */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1799) 	if (secure_data && clear_user(user, param_kernel->data_size))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1800) 		goto bad;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1801) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1802) 	*param = dmi;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1803) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1804) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1805) bad:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1806) 	free_params(dmi, param_kernel->data_size, *param_flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1807) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1808) 	return -EFAULT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1809) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1810) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1811) static int validate_params(uint cmd, struct dm_ioctl *param)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1812) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1813) 	/* Always clear this flag */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1814) 	param->flags &= ~DM_BUFFER_FULL_FLAG;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1815) 	param->flags &= ~DM_UEVENT_GENERATED_FLAG;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1816) 	param->flags &= ~DM_SECURE_DATA_FLAG;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1817) 	param->flags &= ~DM_DATA_OUT_FLAG;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1818) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1819) 	/* Ignores parameters */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1820) 	if (cmd == DM_REMOVE_ALL_CMD ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1821) 	    cmd == DM_LIST_DEVICES_CMD ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1822) 	    cmd == DM_LIST_VERSIONS_CMD)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1823) 		return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1824) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1825) 	if (cmd == DM_DEV_CREATE_CMD) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1826) 		if (!*param->name) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1827) 			DMWARN("name not supplied when creating device");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1828) 			return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1829) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1830) 	} else if (*param->uuid && *param->name) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1831) 		DMWARN("only supply one of name or uuid, cmd(%u)", cmd);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1832) 		return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1833) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1834) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1835) 	/* Ensure strings are terminated */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1836) 	param->name[DM_NAME_LEN - 1] = '\0';
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1837) 	param->uuid[DM_UUID_LEN - 1] = '\0';
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1838) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1839) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1840) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1841) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1842) static int ctl_ioctl(struct file *file, uint command, struct dm_ioctl __user *user)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1843) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1844) 	int r = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1845) 	int ioctl_flags;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1846) 	int param_flags;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1847) 	unsigned int cmd;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1848) 	struct dm_ioctl *param;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1849) 	ioctl_fn fn = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1850) 	size_t input_param_size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1851) 	struct dm_ioctl param_kernel;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1852) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1853) 	/* only root can play with this */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1854) 	if (!capable(CAP_SYS_ADMIN))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1855) 		return -EACCES;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1856) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1857) 	if (_IOC_TYPE(command) != DM_IOCTL)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1858) 		return -ENOTTY;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1859) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1860) 	cmd = _IOC_NR(command);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1861) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1862) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1863) 	 * Check the interface version passed in.  This also
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1864) 	 * writes out the kernel's interface version.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1865) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1866) 	r = check_version(cmd, user);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1867) 	if (r)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1868) 		return r;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1869) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1870) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1871) 	 * Nothing more to do for the version command.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1872) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1873) 	if (cmd == DM_VERSION_CMD)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1874) 		return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1875) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1876) 	fn = lookup_ioctl(cmd, &ioctl_flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1877) 	if (!fn) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1878) 		DMWARN("dm_ctl_ioctl: unknown command 0x%x", command);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1879) 		return -ENOTTY;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1880) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1881) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1882) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1883) 	 * Copy the parameters into kernel space.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1884) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1885) 	r = copy_params(user, &param_kernel, ioctl_flags, &param, &param_flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1886) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1887) 	if (r)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1888) 		return r;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1889) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1890) 	input_param_size = param->data_size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1891) 	r = validate_params(cmd, param);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1892) 	if (r)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1893) 		goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1894) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1895) 	param->data_size = offsetof(struct dm_ioctl, data);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1896) 	r = fn(file, param, input_param_size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1897) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1898) 	if (unlikely(param->flags & DM_BUFFER_FULL_FLAG) &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1899) 	    unlikely(ioctl_flags & IOCTL_FLAGS_NO_PARAMS))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1900) 		DMERR("ioctl %d tried to output some data but has IOCTL_FLAGS_NO_PARAMS set", cmd);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1901) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1902) 	if (!r && ioctl_flags & IOCTL_FLAGS_ISSUE_GLOBAL_EVENT)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1903) 		dm_issue_global_event();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1904) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1905) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1906) 	 * Copy the results back to userland.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1907) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1908) 	if (!r && copy_to_user(user, param, param->data_size))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1909) 		r = -EFAULT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1910) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1911) out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1912) 	free_params(param, input_param_size, param_flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1913) 	return r;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1914) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1915) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1916) static long dm_ctl_ioctl(struct file *file, uint command, ulong u)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1917) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1918) 	return (long)ctl_ioctl(file, command, (struct dm_ioctl __user *)u);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1919) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1920) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1921) #ifdef CONFIG_COMPAT
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1922) static long dm_compat_ctl_ioctl(struct file *file, uint command, ulong u)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1923) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1924) 	return (long)dm_ctl_ioctl(file, command, (ulong) compat_ptr(u));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1925) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1926) #else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1927) #define dm_compat_ctl_ioctl NULL
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1928) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1929) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1930) static int dm_open(struct inode *inode, struct file *filp)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1931) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1932) 	int r;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1933) 	struct dm_file *priv;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1934) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1935) 	r = nonseekable_open(inode, filp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1936) 	if (unlikely(r))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1937) 		return r;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1938) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1939) 	priv = filp->private_data = kmalloc(sizeof(struct dm_file), GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1940) 	if (!priv)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1941) 		return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1942) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1943) 	priv->global_event_nr = atomic_read(&dm_global_event_nr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1944) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1945) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1946) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1947) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1948) static int dm_release(struct inode *inode, struct file *filp)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1949) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1950) 	kfree(filp->private_data);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1951) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1952) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1953) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1954) static __poll_t dm_poll(struct file *filp, poll_table *wait)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1955) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1956) 	struct dm_file *priv = filp->private_data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1957) 	__poll_t mask = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1958) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1959) 	poll_wait(filp, &dm_global_eventq, wait);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1960) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1961) 	if ((int)(atomic_read(&dm_global_event_nr) - priv->global_event_nr) > 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1962) 		mask |= EPOLLIN;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1963) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1964) 	return mask;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1965) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1966) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1967) static const struct file_operations _ctl_fops = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1968) 	.open    = dm_open,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1969) 	.release = dm_release,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1970) 	.poll    = dm_poll,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1971) 	.unlocked_ioctl	 = dm_ctl_ioctl,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1972) 	.compat_ioctl = dm_compat_ctl_ioctl,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1973) 	.owner	 = THIS_MODULE,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1974) 	.llseek  = noop_llseek,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1975) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1976) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1977) static struct miscdevice _dm_misc = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1978) 	.minor		= MAPPER_CTRL_MINOR,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1979) 	.name  		= DM_NAME,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1980) 	.nodename	= DM_DIR "/" DM_CONTROL_NODE,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1981) 	.fops  		= &_ctl_fops
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1982) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1983) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1984) MODULE_ALIAS_MISCDEV(MAPPER_CTRL_MINOR);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1985) MODULE_ALIAS("devname:" DM_DIR "/" DM_CONTROL_NODE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1986) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1987) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1988)  * Create misc character device and link to DM_DIR/control.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1989)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1990) int __init dm_interface_init(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1991) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1992) 	int r;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1993) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1994) 	r = dm_hash_init();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1995) 	if (r)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1996) 		return r;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1997) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1998) 	r = misc_register(&_dm_misc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1999) 	if (r) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2000) 		DMERR("misc_register failed for control device");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2001) 		dm_hash_exit();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2002) 		return r;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2003) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2004) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2005) 	DMINFO("%d.%d.%d%s initialised: %s", DM_VERSION_MAJOR,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2006) 	       DM_VERSION_MINOR, DM_VERSION_PATCHLEVEL, DM_VERSION_EXTRA,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2007) 	       DM_DRIVER_EMAIL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2008) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2009) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2010) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2011) void dm_interface_exit(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2012) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2013) 	misc_deregister(&_dm_misc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2014) 	dm_hash_exit();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2015) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2016) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2017) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2018)  * dm_copy_name_and_uuid - Copy mapped device name & uuid into supplied buffers
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2019)  * @md: Pointer to mapped_device
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2020)  * @name: Buffer (size DM_NAME_LEN) for name
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2021)  * @uuid: Buffer (size DM_UUID_LEN) for uuid or empty string if uuid not defined
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2022)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2023) int dm_copy_name_and_uuid(struct mapped_device *md, char *name, char *uuid)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2024) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2025) 	int r = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2026) 	struct hash_cell *hc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2027) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2028) 	if (!md)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2029) 		return -ENXIO;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2030) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2031) 	mutex_lock(&dm_hash_cells_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2032) 	hc = dm_get_mdptr(md);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2033) 	if (!hc || hc->md != md) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2034) 		r = -ENXIO;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2035) 		goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2036) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2037) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2038) 	if (name)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2039) 		strcpy(name, hc->name);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2040) 	if (uuid)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2041) 		strcpy(uuid, hc->uuid ? : "");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2042) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2043) out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2044) 	mutex_unlock(&dm_hash_cells_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2045) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2046) 	return r;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2047) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2048) EXPORT_SYMBOL_GPL(dm_copy_name_and_uuid);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2049) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2050) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2051)  * dm_early_create - create a mapped device in early boot.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2052)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2053)  * @dmi: Contains main information of the device mapping to be created.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2054)  * @spec_array: array of pointers to struct dm_target_spec. Describes the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2055)  * mapping table of the device.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2056)  * @target_params_array: array of strings with the parameters to a specific
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2057)  * target.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2058)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2059)  * Instead of having the struct dm_target_spec and the parameters for every
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2060)  * target embedded at the end of struct dm_ioctl (as performed in a normal
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2061)  * ioctl), pass them as arguments, so the caller doesn't need to serialize them.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2062)  * The size of the spec_array and target_params_array is given by
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2063)  * @dmi->target_count.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2064)  * This function is supposed to be called in early boot, so locking mechanisms
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2065)  * to protect against concurrent loads are not required.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2066)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2067) int __init dm_early_create(struct dm_ioctl *dmi,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2068) 			   struct dm_target_spec **spec_array,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2069) 			   char **target_params_array)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2070) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2071) 	int r, m = DM_ANY_MINOR;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2072) 	struct dm_table *t, *old_map;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2073) 	struct mapped_device *md;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2074) 	unsigned int i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2075) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2076) 	if (!dmi->target_count)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2077) 		return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2078) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2079) 	r = check_name(dmi->name);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2080) 	if (r)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2081) 		return r;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2082) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2083) 	if (dmi->flags & DM_PERSISTENT_DEV_FLAG)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2084) 		m = MINOR(huge_decode_dev(dmi->dev));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2085) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2086) 	/* alloc dm device */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2087) 	r = dm_create(m, &md);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2088) 	if (r)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2089) 		return r;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2090) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2091) 	/* hash insert */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2092) 	r = dm_hash_insert(dmi->name, *dmi->uuid ? dmi->uuid : NULL, md);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2093) 	if (r)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2094) 		goto err_destroy_dm;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2095) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2096) 	/* alloc table */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2097) 	r = dm_table_create(&t, get_mode(dmi), dmi->target_count, md);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2098) 	if (r)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2099) 		goto err_hash_remove;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2100) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2101) 	/* add targets */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2102) 	for (i = 0; i < dmi->target_count; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2103) 		r = dm_table_add_target(t, spec_array[i]->target_type,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2104) 					(sector_t) spec_array[i]->sector_start,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2105) 					(sector_t) spec_array[i]->length,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2106) 					target_params_array[i]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2107) 		if (r) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2108) 			DMWARN("error adding target to table");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2109) 			goto err_destroy_table;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2110) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2111) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2112) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2113) 	/* finish table */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2114) 	r = dm_table_complete(t);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2115) 	if (r)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2116) 		goto err_destroy_table;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2117) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2118) 	md->type = dm_table_get_type(t);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2119) 	/* setup md->queue to reflect md's type (may block) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2120) 	r = dm_setup_md_queue(md, t);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2121) 	if (r) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2122) 		DMWARN("unable to set up device queue for new table.");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2123) 		goto err_destroy_table;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2124) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2125) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2126) 	/* Set new map */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2127) 	dm_suspend(md, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2128) 	old_map = dm_swap_table(md, t);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2129) 	if (IS_ERR(old_map)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2130) 		r = PTR_ERR(old_map);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2131) 		goto err_destroy_table;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2132) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2133) 	set_disk_ro(dm_disk(md), !!(dmi->flags & DM_READONLY_FLAG));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2134) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2135) 	/* resume device */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2136) 	r = dm_resume(md);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2137) 	if (r)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2138) 		goto err_destroy_table;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2139) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2140) 	DMINFO("%s (%s) is ready", md->disk->disk_name, dmi->name);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2141) 	dm_put(md);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2142) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2143) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2144) err_destroy_table:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2145) 	dm_table_destroy(t);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2146) err_hash_remove:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2147) 	(void) __hash_remove(__get_name_cell(dmi->name));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2148) 	/* release reference from __get_name_cell */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2149) 	dm_put(md);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2150) err_destroy_dm:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2151) 	dm_put(md);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2152) 	dm_destroy(md);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2153) 	return r;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2154) }