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