xref: /openbmc/linux/drivers/md/dm-ioctl.c (revision 1134e5ae79bab61c05657ca35a6297cf87202e35)
1 /*
2  * Copyright (C) 2001, 2002 Sistina Software (UK) Limited.
3  * Copyright (C) 2004 - 2005 Red Hat, Inc. All rights reserved.
4  *
5  * This file is released under the GPL.
6  */
7 
8 #include "dm.h"
9 
10 #include <linux/module.h>
11 #include <linux/vmalloc.h>
12 #include <linux/miscdevice.h>
13 #include <linux/init.h>
14 #include <linux/wait.h>
15 #include <linux/slab.h>
16 #include <linux/devfs_fs_kernel.h>
17 #include <linux/dm-ioctl.h>
18 
19 #include <asm/uaccess.h>
20 
21 #define DM_DRIVER_EMAIL "dm-devel@redhat.com"
22 
23 /*-----------------------------------------------------------------
24  * The ioctl interface needs to be able to look up devices by
25  * name or uuid.
26  *---------------------------------------------------------------*/
27 struct hash_cell {
28 	struct list_head name_list;
29 	struct list_head uuid_list;
30 
31 	char *name;
32 	char *uuid;
33 	struct mapped_device *md;
34 	struct dm_table *new_map;
35 };
36 
37 struct vers_iter {
38     size_t param_size;
39     struct dm_target_versions *vers, *old_vers;
40     char *end;
41     uint32_t flags;
42 };
43 
44 
45 #define NUM_BUCKETS 64
46 #define MASK_BUCKETS (NUM_BUCKETS - 1)
47 static struct list_head _name_buckets[NUM_BUCKETS];
48 static struct list_head _uuid_buckets[NUM_BUCKETS];
49 
50 static void dm_hash_remove_all(void);
51 
52 /*
53  * Guards access to both hash tables.
54  */
55 static DECLARE_RWSEM(_hash_lock);
56 
57 static void init_buckets(struct list_head *buckets)
58 {
59 	unsigned int i;
60 
61 	for (i = 0; i < NUM_BUCKETS; i++)
62 		INIT_LIST_HEAD(buckets + i);
63 }
64 
65 static int dm_hash_init(void)
66 {
67 	init_buckets(_name_buckets);
68 	init_buckets(_uuid_buckets);
69 	devfs_mk_dir(DM_DIR);
70 	return 0;
71 }
72 
73 static void dm_hash_exit(void)
74 {
75 	dm_hash_remove_all();
76 	devfs_remove(DM_DIR);
77 }
78 
79 /*-----------------------------------------------------------------
80  * Hash function:
81  * We're not really concerned with the str hash function being
82  * fast since it's only used by the ioctl interface.
83  *---------------------------------------------------------------*/
84 static unsigned int hash_str(const char *str)
85 {
86 	const unsigned int hash_mult = 2654435387U;
87 	unsigned int h = 0;
88 
89 	while (*str)
90 		h = (h + (unsigned int) *str++) * hash_mult;
91 
92 	return h & MASK_BUCKETS;
93 }
94 
95 /*-----------------------------------------------------------------
96  * Code for looking up a device by name
97  *---------------------------------------------------------------*/
98 static struct hash_cell *__get_name_cell(const char *str)
99 {
100 	struct hash_cell *hc;
101 	unsigned int h = hash_str(str);
102 
103 	list_for_each_entry (hc, _name_buckets + h, name_list)
104 		if (!strcmp(hc->name, str))
105 			return hc;
106 
107 	return NULL;
108 }
109 
110 static struct hash_cell *__get_uuid_cell(const char *str)
111 {
112 	struct hash_cell *hc;
113 	unsigned int h = hash_str(str);
114 
115 	list_for_each_entry (hc, _uuid_buckets + h, uuid_list)
116 		if (!strcmp(hc->uuid, str))
117 			return hc;
118 
119 	return NULL;
120 }
121 
122 /*-----------------------------------------------------------------
123  * Inserting, removing and renaming a device.
124  *---------------------------------------------------------------*/
125 static struct hash_cell *alloc_cell(const char *name, const char *uuid,
126 				    struct mapped_device *md)
127 {
128 	struct hash_cell *hc;
129 
130 	hc = kmalloc(sizeof(*hc), GFP_KERNEL);
131 	if (!hc)
132 		return NULL;
133 
134 	hc->name = kstrdup(name, GFP_KERNEL);
135 	if (!hc->name) {
136 		kfree(hc);
137 		return NULL;
138 	}
139 
140 	if (!uuid)
141 		hc->uuid = NULL;
142 
143 	else {
144 		hc->uuid = kstrdup(uuid, GFP_KERNEL);
145 		if (!hc->uuid) {
146 			kfree(hc->name);
147 			kfree(hc);
148 			return NULL;
149 		}
150 	}
151 
152 	INIT_LIST_HEAD(&hc->name_list);
153 	INIT_LIST_HEAD(&hc->uuid_list);
154 	hc->md = md;
155 	hc->new_map = NULL;
156 	return hc;
157 }
158 
159 static void free_cell(struct hash_cell *hc)
160 {
161 	if (hc) {
162 		kfree(hc->name);
163 		kfree(hc->uuid);
164 		kfree(hc);
165 	}
166 }
167 
168 /*
169  * devfs stuff.
170  */
171 static int register_with_devfs(struct hash_cell *hc)
172 {
173 	struct gendisk *disk = dm_disk(hc->md);
174 
175 	devfs_mk_bdev(MKDEV(disk->major, disk->first_minor),
176 		      S_IFBLK | S_IRUSR | S_IWUSR | S_IRGRP,
177 		      DM_DIR "/%s", hc->name);
178 	return 0;
179 }
180 
181 static int unregister_with_devfs(struct hash_cell *hc)
182 {
183 	devfs_remove(DM_DIR"/%s", hc->name);
184 	return 0;
185 }
186 
187 /*
188  * The kdev_t and uuid of a device can never change once it is
189  * initially inserted.
190  */
191 static int dm_hash_insert(const char *name, const char *uuid, struct mapped_device *md)
192 {
193 	struct hash_cell *cell;
194 
195 	/*
196 	 * Allocate the new cells.
197 	 */
198 	cell = alloc_cell(name, uuid, md);
199 	if (!cell)
200 		return -ENOMEM;
201 
202 	/*
203 	 * Insert the cell into both hash tables.
204 	 */
205 	down_write(&_hash_lock);
206 	if (__get_name_cell(name))
207 		goto bad;
208 
209 	list_add(&cell->name_list, _name_buckets + hash_str(name));
210 
211 	if (uuid) {
212 		if (__get_uuid_cell(uuid)) {
213 			list_del(&cell->name_list);
214 			goto bad;
215 		}
216 		list_add(&cell->uuid_list, _uuid_buckets + hash_str(uuid));
217 	}
218 	register_with_devfs(cell);
219 	dm_get(md);
220 	dm_set_mdptr(md, cell);
221 	up_write(&_hash_lock);
222 
223 	return 0;
224 
225  bad:
226 	up_write(&_hash_lock);
227 	free_cell(cell);
228 	return -EBUSY;
229 }
230 
231 static void __hash_remove(struct hash_cell *hc)
232 {
233 	struct dm_table *table;
234 
235 	/* remove from the dev hash */
236 	list_del(&hc->uuid_list);
237 	list_del(&hc->name_list);
238 	unregister_with_devfs(hc);
239 	dm_set_mdptr(hc->md, NULL);
240 
241 	table = dm_get_table(hc->md);
242 	if (table) {
243 		dm_table_event(table);
244 		dm_table_put(table);
245 	}
246 
247 	if (hc->new_map)
248 		dm_table_put(hc->new_map);
249 	dm_put(hc->md);
250 	free_cell(hc);
251 }
252 
253 static void dm_hash_remove_all(void)
254 {
255 	int i;
256 	struct hash_cell *hc;
257 	struct list_head *tmp, *n;
258 
259 	down_write(&_hash_lock);
260 	for (i = 0; i < NUM_BUCKETS; i++) {
261 		list_for_each_safe (tmp, n, _name_buckets + i) {
262 			hc = list_entry(tmp, struct hash_cell, name_list);
263 			__hash_remove(hc);
264 		}
265 	}
266 	up_write(&_hash_lock);
267 }
268 
269 static int dm_hash_rename(const char *old, const char *new)
270 {
271 	char *new_name, *old_name;
272 	struct hash_cell *hc;
273 	struct dm_table *table;
274 
275 	/*
276 	 * duplicate new.
277 	 */
278 	new_name = kstrdup(new, GFP_KERNEL);
279 	if (!new_name)
280 		return -ENOMEM;
281 
282 	down_write(&_hash_lock);
283 
284 	/*
285 	 * Is new free ?
286 	 */
287 	hc = __get_name_cell(new);
288 	if (hc) {
289 		DMWARN("asked to rename to an already existing name %s -> %s",
290 		       old, new);
291 		up_write(&_hash_lock);
292 		kfree(new_name);
293 		return -EBUSY;
294 	}
295 
296 	/*
297 	 * Is there such a device as 'old' ?
298 	 */
299 	hc = __get_name_cell(old);
300 	if (!hc) {
301 		DMWARN("asked to rename a non existent device %s -> %s",
302 		       old, new);
303 		up_write(&_hash_lock);
304 		kfree(new_name);
305 		return -ENXIO;
306 	}
307 
308 	/*
309 	 * rename and move the name cell.
310 	 */
311 	unregister_with_devfs(hc);
312 
313 	list_del(&hc->name_list);
314 	old_name = hc->name;
315 	hc->name = new_name;
316 	list_add(&hc->name_list, _name_buckets + hash_str(new_name));
317 
318 	/* rename the device node in devfs */
319 	register_with_devfs(hc);
320 
321 	/*
322 	 * Wake up any dm event waiters.
323 	 */
324 	table = dm_get_table(hc->md);
325 	if (table) {
326 		dm_table_event(table);
327 		dm_table_put(table);
328 	}
329 
330 	up_write(&_hash_lock);
331 	kfree(old_name);
332 	return 0;
333 }
334 
335 /*-----------------------------------------------------------------
336  * Implementation of the ioctl commands
337  *---------------------------------------------------------------*/
338 /*
339  * All the ioctl commands get dispatched to functions with this
340  * prototype.
341  */
342 typedef int (*ioctl_fn)(struct dm_ioctl *param, size_t param_size);
343 
344 static int remove_all(struct dm_ioctl *param, size_t param_size)
345 {
346 	dm_hash_remove_all();
347 	param->data_size = 0;
348 	return 0;
349 }
350 
351 /*
352  * Round up the ptr to an 8-byte boundary.
353  */
354 #define ALIGN_MASK 7
355 static inline void *align_ptr(void *ptr)
356 {
357 	return (void *) (((size_t) (ptr + ALIGN_MASK)) & ~ALIGN_MASK);
358 }
359 
360 /*
361  * Retrieves the data payload buffer from an already allocated
362  * struct dm_ioctl.
363  */
364 static void *get_result_buffer(struct dm_ioctl *param, size_t param_size,
365 			       size_t *len)
366 {
367 	param->data_start = align_ptr(param + 1) - (void *) param;
368 
369 	if (param->data_start < param_size)
370 		*len = param_size - param->data_start;
371 	else
372 		*len = 0;
373 
374 	return ((void *) param) + param->data_start;
375 }
376 
377 static int list_devices(struct dm_ioctl *param, size_t param_size)
378 {
379 	unsigned int i;
380 	struct hash_cell *hc;
381 	size_t len, needed = 0;
382 	struct gendisk *disk;
383 	struct dm_name_list *nl, *old_nl = NULL;
384 
385 	down_write(&_hash_lock);
386 
387 	/*
388 	 * Loop through all the devices working out how much
389 	 * space we need.
390 	 */
391 	for (i = 0; i < NUM_BUCKETS; i++) {
392 		list_for_each_entry (hc, _name_buckets + i, name_list) {
393 			needed += sizeof(struct dm_name_list);
394 			needed += strlen(hc->name) + 1;
395 			needed += ALIGN_MASK;
396 		}
397 	}
398 
399 	/*
400 	 * Grab our output buffer.
401 	 */
402 	nl = get_result_buffer(param, param_size, &len);
403 	if (len < needed) {
404 		param->flags |= DM_BUFFER_FULL_FLAG;
405 		goto out;
406 	}
407 	param->data_size = param->data_start + needed;
408 
409 	nl->dev = 0;	/* Flags no data */
410 
411 	/*
412 	 * Now loop through filling out the names.
413 	 */
414 	for (i = 0; i < NUM_BUCKETS; i++) {
415 		list_for_each_entry (hc, _name_buckets + i, name_list) {
416 			if (old_nl)
417 				old_nl->next = (uint32_t) ((void *) nl -
418 							   (void *) old_nl);
419 			disk = dm_disk(hc->md);
420 			nl->dev = huge_encode_dev(MKDEV(disk->major, disk->first_minor));
421 			nl->next = 0;
422 			strcpy(nl->name, hc->name);
423 
424 			old_nl = nl;
425 			nl = align_ptr(((void *) ++nl) + strlen(hc->name) + 1);
426 		}
427 	}
428 
429  out:
430 	up_write(&_hash_lock);
431 	return 0;
432 }
433 
434 static void list_version_get_needed(struct target_type *tt, void *needed_param)
435 {
436     size_t *needed = needed_param;
437 
438     *needed += sizeof(struct dm_target_versions);
439     *needed += strlen(tt->name);
440     *needed += ALIGN_MASK;
441 }
442 
443 static void list_version_get_info(struct target_type *tt, void *param)
444 {
445     struct vers_iter *info = param;
446 
447     /* Check space - it might have changed since the first iteration */
448     if ((char *)info->vers + sizeof(tt->version) + strlen(tt->name) + 1 >
449 	info->end) {
450 
451 	info->flags = DM_BUFFER_FULL_FLAG;
452 	return;
453     }
454 
455     if (info->old_vers)
456 	info->old_vers->next = (uint32_t) ((void *)info->vers -
457 					   (void *)info->old_vers);
458     info->vers->version[0] = tt->version[0];
459     info->vers->version[1] = tt->version[1];
460     info->vers->version[2] = tt->version[2];
461     info->vers->next = 0;
462     strcpy(info->vers->name, tt->name);
463 
464     info->old_vers = info->vers;
465     info->vers = align_ptr(((void *) ++info->vers) + strlen(tt->name) + 1);
466 }
467 
468 static int list_versions(struct dm_ioctl *param, size_t param_size)
469 {
470 	size_t len, needed = 0;
471 	struct dm_target_versions *vers;
472 	struct vers_iter iter_info;
473 
474 	/*
475 	 * Loop through all the devices working out how much
476 	 * space we need.
477 	 */
478 	dm_target_iterate(list_version_get_needed, &needed);
479 
480 	/*
481 	 * Grab our output buffer.
482 	 */
483 	vers = get_result_buffer(param, param_size, &len);
484 	if (len < needed) {
485 		param->flags |= DM_BUFFER_FULL_FLAG;
486 		goto out;
487 	}
488 	param->data_size = param->data_start + needed;
489 
490 	iter_info.param_size = param_size;
491 	iter_info.old_vers = NULL;
492 	iter_info.vers = vers;
493 	iter_info.flags = 0;
494 	iter_info.end = (char *)vers+len;
495 
496 	/*
497 	 * Now loop through filling out the names & versions.
498 	 */
499 	dm_target_iterate(list_version_get_info, &iter_info);
500 	param->flags |= iter_info.flags;
501 
502  out:
503 	return 0;
504 }
505 
506 
507 
508 static int check_name(const char *name)
509 {
510 	if (strchr(name, '/')) {
511 		DMWARN("invalid device name");
512 		return -EINVAL;
513 	}
514 
515 	return 0;
516 }
517 
518 /*
519  * Fills in a dm_ioctl structure, ready for sending back to
520  * userland.
521  */
522 static int __dev_status(struct mapped_device *md, struct dm_ioctl *param)
523 {
524 	struct gendisk *disk = dm_disk(md);
525 	struct dm_table *table;
526 	struct block_device *bdev;
527 
528 	param->flags &= ~(DM_SUSPEND_FLAG | DM_READONLY_FLAG |
529 			  DM_ACTIVE_PRESENT_FLAG);
530 
531 	if (dm_suspended(md))
532 		param->flags |= DM_SUSPEND_FLAG;
533 
534 	param->dev = huge_encode_dev(MKDEV(disk->major, disk->first_minor));
535 
536 	if (!(param->flags & DM_SKIP_BDGET_FLAG)) {
537 		bdev = bdget_disk(disk, 0);
538 		if (!bdev)
539 			return -ENXIO;
540 
541 		/*
542 		 * Yes, this will be out of date by the time it gets back
543 		 * to userland, but it is still very useful for
544 		 * debugging.
545 		 */
546 		param->open_count = bdev->bd_openers;
547 		bdput(bdev);
548 	} else
549 		param->open_count = -1;
550 
551 	if (disk->policy)
552 		param->flags |= DM_READONLY_FLAG;
553 
554 	param->event_nr = dm_get_event_nr(md);
555 
556 	table = dm_get_table(md);
557 	if (table) {
558 		param->flags |= DM_ACTIVE_PRESENT_FLAG;
559 		param->target_count = dm_table_get_num_targets(table);
560 		dm_table_put(table);
561 	} else
562 		param->target_count = 0;
563 
564 	return 0;
565 }
566 
567 static int dev_create(struct dm_ioctl *param, size_t param_size)
568 {
569 	int r;
570 	struct mapped_device *md;
571 
572 	r = check_name(param->name);
573 	if (r)
574 		return r;
575 
576 	if (param->flags & DM_PERSISTENT_DEV_FLAG)
577 		r = dm_create_with_minor(MINOR(huge_decode_dev(param->dev)), &md);
578 	else
579 		r = dm_create(&md);
580 
581 	if (r)
582 		return r;
583 
584 	r = dm_hash_insert(param->name, *param->uuid ? param->uuid : NULL, md);
585 	if (r) {
586 		dm_put(md);
587 		return r;
588 	}
589 
590 	param->flags &= ~DM_INACTIVE_PRESENT_FLAG;
591 
592 	r = __dev_status(md, param);
593 	dm_put(md);
594 
595 	return r;
596 }
597 
598 /*
599  * Always use UUID for lookups if it's present, otherwise use name or dev.
600  */
601 static struct hash_cell *__find_device_hash_cell(struct dm_ioctl *param)
602 {
603 	struct mapped_device *md;
604 	void *mdptr = NULL;
605 
606 	if (*param->uuid)
607 		return __get_uuid_cell(param->uuid);
608 
609 	if (*param->name)
610 		return __get_name_cell(param->name);
611 
612 	md = dm_get_md(huge_decode_dev(param->dev));
613 	if (md) {
614 		mdptr = dm_get_mdptr(md);
615 		dm_put(md);
616 	}
617 
618 	return mdptr;
619 }
620 
621 static struct mapped_device *find_device(struct dm_ioctl *param)
622 {
623 	struct hash_cell *hc;
624 	struct mapped_device *md = NULL;
625 
626 	down_read(&_hash_lock);
627 	hc = __find_device_hash_cell(param);
628 	if (hc) {
629 		md = hc->md;
630 		dm_get(md);
631 
632 		/*
633 		 * Sneakily write in both the name and the uuid
634 		 * while we have the cell.
635 		 */
636 		strncpy(param->name, hc->name, sizeof(param->name));
637 		if (hc->uuid)
638 			strncpy(param->uuid, hc->uuid, sizeof(param->uuid)-1);
639 		else
640 			param->uuid[0] = '\0';
641 
642 		if (hc->new_map)
643 			param->flags |= DM_INACTIVE_PRESENT_FLAG;
644 		else
645 			param->flags &= ~DM_INACTIVE_PRESENT_FLAG;
646 	}
647 	up_read(&_hash_lock);
648 
649 	return md;
650 }
651 
652 static int dev_remove(struct dm_ioctl *param, size_t param_size)
653 {
654 	struct hash_cell *hc;
655 
656 	down_write(&_hash_lock);
657 	hc = __find_device_hash_cell(param);
658 
659 	if (!hc) {
660 		DMWARN("device doesn't appear to be in the dev hash table.");
661 		up_write(&_hash_lock);
662 		return -ENXIO;
663 	}
664 
665 	__hash_remove(hc);
666 	up_write(&_hash_lock);
667 	param->data_size = 0;
668 	return 0;
669 }
670 
671 /*
672  * Check a string doesn't overrun the chunk of
673  * memory we copied from userland.
674  */
675 static int invalid_str(char *str, void *end)
676 {
677 	while ((void *) str < end)
678 		if (!*str++)
679 			return 0;
680 
681 	return -EINVAL;
682 }
683 
684 static int dev_rename(struct dm_ioctl *param, size_t param_size)
685 {
686 	int r;
687 	char *new_name = (char *) param + param->data_start;
688 
689 	if (new_name < (char *) (param + 1) ||
690 	    invalid_str(new_name, (void *) param + param_size)) {
691 		DMWARN("Invalid new logical volume name supplied.");
692 		return -EINVAL;
693 	}
694 
695 	r = check_name(new_name);
696 	if (r)
697 		return r;
698 
699 	param->data_size = 0;
700 	return dm_hash_rename(param->name, new_name);
701 }
702 
703 static int do_suspend(struct dm_ioctl *param)
704 {
705 	int r = 0;
706 	int do_lockfs = 1;
707 	struct mapped_device *md;
708 
709 	md = find_device(param);
710 	if (!md)
711 		return -ENXIO;
712 
713 	if (param->flags & DM_SKIP_LOCKFS_FLAG)
714 		do_lockfs = 0;
715 
716 	if (!dm_suspended(md))
717 		r = dm_suspend(md, do_lockfs);
718 
719 	if (!r)
720 		r = __dev_status(md, param);
721 
722 	dm_put(md);
723 	return r;
724 }
725 
726 static int do_resume(struct dm_ioctl *param)
727 {
728 	int r = 0;
729 	int do_lockfs = 1;
730 	struct hash_cell *hc;
731 	struct mapped_device *md;
732 	struct dm_table *new_map;
733 
734 	down_write(&_hash_lock);
735 
736 	hc = __find_device_hash_cell(param);
737 	if (!hc) {
738 		DMWARN("device doesn't appear to be in the dev hash table.");
739 		up_write(&_hash_lock);
740 		return -ENXIO;
741 	}
742 
743 	md = hc->md;
744 	dm_get(md);
745 
746 	new_map = hc->new_map;
747 	hc->new_map = NULL;
748 	param->flags &= ~DM_INACTIVE_PRESENT_FLAG;
749 
750 	up_write(&_hash_lock);
751 
752 	/* Do we need to load a new map ? */
753 	if (new_map) {
754 		/* Suspend if it isn't already suspended */
755 		if (param->flags & DM_SKIP_LOCKFS_FLAG)
756 			do_lockfs = 0;
757 		if (!dm_suspended(md))
758 			dm_suspend(md, do_lockfs);
759 
760 		r = dm_swap_table(md, new_map);
761 		if (r) {
762 			dm_put(md);
763 			dm_table_put(new_map);
764 			return r;
765 		}
766 
767 		if (dm_table_get_mode(new_map) & FMODE_WRITE)
768 			set_disk_ro(dm_disk(md), 0);
769 		else
770 			set_disk_ro(dm_disk(md), 1);
771 
772 		dm_table_put(new_map);
773 	}
774 
775 	if (dm_suspended(md))
776 		r = dm_resume(md);
777 
778 	if (!r)
779 		r = __dev_status(md, param);
780 
781 	dm_put(md);
782 	return r;
783 }
784 
785 /*
786  * Set or unset the suspension state of a device.
787  * If the device already is in the requested state we just return its status.
788  */
789 static int dev_suspend(struct dm_ioctl *param, size_t param_size)
790 {
791 	if (param->flags & DM_SUSPEND_FLAG)
792 		return do_suspend(param);
793 
794 	return do_resume(param);
795 }
796 
797 /*
798  * Copies device info back to user space, used by
799  * the create and info ioctls.
800  */
801 static int dev_status(struct dm_ioctl *param, size_t param_size)
802 {
803 	int r;
804 	struct mapped_device *md;
805 
806 	md = find_device(param);
807 	if (!md)
808 		return -ENXIO;
809 
810 	r = __dev_status(md, param);
811 	dm_put(md);
812 	return r;
813 }
814 
815 /*
816  * Build up the status struct for each target
817  */
818 static void retrieve_status(struct dm_table *table,
819 			    struct dm_ioctl *param, size_t param_size)
820 {
821 	unsigned int i, num_targets;
822 	struct dm_target_spec *spec;
823 	char *outbuf, *outptr;
824 	status_type_t type;
825 	size_t remaining, len, used = 0;
826 
827 	outptr = outbuf = get_result_buffer(param, param_size, &len);
828 
829 	if (param->flags & DM_STATUS_TABLE_FLAG)
830 		type = STATUSTYPE_TABLE;
831 	else
832 		type = STATUSTYPE_INFO;
833 
834 	/* Get all the target info */
835 	num_targets = dm_table_get_num_targets(table);
836 	for (i = 0; i < num_targets; i++) {
837 		struct dm_target *ti = dm_table_get_target(table, i);
838 
839 		remaining = len - (outptr - outbuf);
840 		if (remaining <= sizeof(struct dm_target_spec)) {
841 			param->flags |= DM_BUFFER_FULL_FLAG;
842 			break;
843 		}
844 
845 		spec = (struct dm_target_spec *) outptr;
846 
847 		spec->status = 0;
848 		spec->sector_start = ti->begin;
849 		spec->length = ti->len;
850 		strncpy(spec->target_type, ti->type->name,
851 			sizeof(spec->target_type));
852 
853 		outptr += sizeof(struct dm_target_spec);
854 		remaining = len - (outptr - outbuf);
855 		if (remaining <= 0) {
856 			param->flags |= DM_BUFFER_FULL_FLAG;
857 			break;
858 		}
859 
860 		/* Get the status/table string from the target driver */
861 		if (ti->type->status) {
862 			if (ti->type->status(ti, type, outptr, remaining)) {
863 				param->flags |= DM_BUFFER_FULL_FLAG;
864 				break;
865 			}
866 		} else
867 			outptr[0] = '\0';
868 
869 		outptr += strlen(outptr) + 1;
870 		used = param->data_start + (outptr - outbuf);
871 
872 		outptr = align_ptr(outptr);
873 		spec->next = outptr - outbuf;
874 	}
875 
876 	if (used)
877 		param->data_size = used;
878 
879 	param->target_count = num_targets;
880 }
881 
882 /*
883  * Wait for a device to report an event
884  */
885 static int dev_wait(struct dm_ioctl *param, size_t param_size)
886 {
887 	int r;
888 	struct mapped_device *md;
889 	struct dm_table *table;
890 
891 	md = find_device(param);
892 	if (!md)
893 		return -ENXIO;
894 
895 	/*
896 	 * Wait for a notification event
897 	 */
898 	if (dm_wait_event(md, param->event_nr)) {
899 		r = -ERESTARTSYS;
900 		goto out;
901 	}
902 
903 	/*
904 	 * The userland program is going to want to know what
905 	 * changed to trigger the event, so we may as well tell
906 	 * him and save an ioctl.
907 	 */
908 	r = __dev_status(md, param);
909 	if (r)
910 		goto out;
911 
912 	table = dm_get_table(md);
913 	if (table) {
914 		retrieve_status(table, param, param_size);
915 		dm_table_put(table);
916 	}
917 
918  out:
919 	dm_put(md);
920 	return r;
921 }
922 
923 static inline int get_mode(struct dm_ioctl *param)
924 {
925 	int mode = FMODE_READ | FMODE_WRITE;
926 
927 	if (param->flags & DM_READONLY_FLAG)
928 		mode = FMODE_READ;
929 
930 	return mode;
931 }
932 
933 static int next_target(struct dm_target_spec *last, uint32_t next, void *end,
934 		       struct dm_target_spec **spec, char **target_params)
935 {
936 	*spec = (struct dm_target_spec *) ((unsigned char *) last + next);
937 	*target_params = (char *) (*spec + 1);
938 
939 	if (*spec < (last + 1))
940 		return -EINVAL;
941 
942 	return invalid_str(*target_params, end);
943 }
944 
945 static int populate_table(struct dm_table *table,
946 			  struct dm_ioctl *param, size_t param_size)
947 {
948 	int r;
949 	unsigned int i = 0;
950 	struct dm_target_spec *spec = (struct dm_target_spec *) param;
951 	uint32_t next = param->data_start;
952 	void *end = (void *) param + param_size;
953 	char *target_params;
954 
955 	if (!param->target_count) {
956 		DMWARN("populate_table: no targets specified");
957 		return -EINVAL;
958 	}
959 
960 	for (i = 0; i < param->target_count; i++) {
961 
962 		r = next_target(spec, next, end, &spec, &target_params);
963 		if (r) {
964 			DMWARN("unable to find target");
965 			return r;
966 		}
967 
968 		r = dm_table_add_target(table, spec->target_type,
969 					(sector_t) spec->sector_start,
970 					(sector_t) spec->length,
971 					target_params);
972 		if (r) {
973 			DMWARN("error adding target to table");
974 			return r;
975 		}
976 
977 		next = spec->next;
978 	}
979 
980 	return dm_table_complete(table);
981 }
982 
983 static int table_load(struct dm_ioctl *param, size_t param_size)
984 {
985 	int r;
986 	struct hash_cell *hc;
987 	struct dm_table *t;
988 	struct mapped_device *md;
989 
990 	md = find_device(param);
991 	if (!md)
992 		return -ENXIO;
993 
994 	r = dm_table_create(&t, get_mode(param), param->target_count, md);
995 	if (r)
996 		goto out;
997 
998 	r = populate_table(t, param, param_size);
999 	if (r) {
1000 		dm_table_put(t);
1001 		goto out;
1002 	}
1003 
1004 	down_write(&_hash_lock);
1005 	hc = dm_get_mdptr(md);
1006 	if (!hc || hc->md != md) {
1007 		DMWARN("device has been removed from the dev hash table.");
1008 		dm_table_put(t);
1009 		up_write(&_hash_lock);
1010 		r = -ENXIO;
1011 		goto out;
1012 	}
1013 
1014 	if (hc->new_map)
1015 		dm_table_put(hc->new_map);
1016 	hc->new_map = t;
1017 	up_write(&_hash_lock);
1018 
1019 	param->flags |= DM_INACTIVE_PRESENT_FLAG;
1020 	r = __dev_status(md, param);
1021 
1022 out:
1023 	dm_put(md);
1024 
1025 	return r;
1026 }
1027 
1028 static int table_clear(struct dm_ioctl *param, size_t param_size)
1029 {
1030 	int r;
1031 	struct hash_cell *hc;
1032 
1033 	down_write(&_hash_lock);
1034 
1035 	hc = __find_device_hash_cell(param);
1036 	if (!hc) {
1037 		DMWARN("device doesn't appear to be in the dev hash table.");
1038 		up_write(&_hash_lock);
1039 		return -ENXIO;
1040 	}
1041 
1042 	if (hc->new_map) {
1043 		dm_table_put(hc->new_map);
1044 		hc->new_map = NULL;
1045 	}
1046 
1047 	param->flags &= ~DM_INACTIVE_PRESENT_FLAG;
1048 
1049 	r = __dev_status(hc->md, param);
1050 	up_write(&_hash_lock);
1051 	return r;
1052 }
1053 
1054 /*
1055  * Retrieves a list of devices used by a particular dm device.
1056  */
1057 static void retrieve_deps(struct dm_table *table,
1058 			  struct dm_ioctl *param, size_t param_size)
1059 {
1060 	unsigned int count = 0;
1061 	struct list_head *tmp;
1062 	size_t len, needed;
1063 	struct dm_dev *dd;
1064 	struct dm_target_deps *deps;
1065 
1066 	deps = get_result_buffer(param, param_size, &len);
1067 
1068 	/*
1069 	 * Count the devices.
1070 	 */
1071 	list_for_each (tmp, dm_table_get_devices(table))
1072 		count++;
1073 
1074 	/*
1075 	 * Check we have enough space.
1076 	 */
1077 	needed = sizeof(*deps) + (sizeof(*deps->dev) * count);
1078 	if (len < needed) {
1079 		param->flags |= DM_BUFFER_FULL_FLAG;
1080 		return;
1081 	}
1082 
1083 	/*
1084 	 * Fill in the devices.
1085 	 */
1086 	deps->count = count;
1087 	count = 0;
1088 	list_for_each_entry (dd, dm_table_get_devices(table), list)
1089 		deps->dev[count++] = huge_encode_dev(dd->bdev->bd_dev);
1090 
1091 	param->data_size = param->data_start + needed;
1092 }
1093 
1094 static int table_deps(struct dm_ioctl *param, size_t param_size)
1095 {
1096 	int r = 0;
1097 	struct mapped_device *md;
1098 	struct dm_table *table;
1099 
1100 	md = find_device(param);
1101 	if (!md)
1102 		return -ENXIO;
1103 
1104 	r = __dev_status(md, param);
1105 	if (r)
1106 		goto out;
1107 
1108 	table = dm_get_table(md);
1109 	if (table) {
1110 		retrieve_deps(table, param, param_size);
1111 		dm_table_put(table);
1112 	}
1113 
1114  out:
1115 	dm_put(md);
1116 	return r;
1117 }
1118 
1119 /*
1120  * Return the status of a device as a text string for each
1121  * target.
1122  */
1123 static int table_status(struct dm_ioctl *param, size_t param_size)
1124 {
1125 	int r;
1126 	struct mapped_device *md;
1127 	struct dm_table *table;
1128 
1129 	md = find_device(param);
1130 	if (!md)
1131 		return -ENXIO;
1132 
1133 	r = __dev_status(md, param);
1134 	if (r)
1135 		goto out;
1136 
1137 	table = dm_get_table(md);
1138 	if (table) {
1139 		retrieve_status(table, param, param_size);
1140 		dm_table_put(table);
1141 	}
1142 
1143  out:
1144 	dm_put(md);
1145 	return r;
1146 }
1147 
1148 /*
1149  * Pass a message to the target that's at the supplied device offset.
1150  */
1151 static int target_message(struct dm_ioctl *param, size_t param_size)
1152 {
1153 	int r, argc;
1154 	char **argv;
1155 	struct mapped_device *md;
1156 	struct dm_table *table;
1157 	struct dm_target *ti;
1158 	struct dm_target_msg *tmsg = (void *) param + param->data_start;
1159 
1160 	md = find_device(param);
1161 	if (!md)
1162 		return -ENXIO;
1163 
1164 	r = __dev_status(md, param);
1165 	if (r)
1166 		goto out;
1167 
1168 	if (tmsg < (struct dm_target_msg *) (param + 1) ||
1169 	    invalid_str(tmsg->message, (void *) param + param_size)) {
1170 		DMWARN("Invalid target message parameters.");
1171 		r = -EINVAL;
1172 		goto out;
1173 	}
1174 
1175 	r = dm_split_args(&argc, &argv, tmsg->message);
1176 	if (r) {
1177 		DMWARN("Failed to split target message parameters");
1178 		goto out;
1179 	}
1180 
1181 	table = dm_get_table(md);
1182 	if (!table)
1183 		goto out_argv;
1184 
1185 	if (tmsg->sector >= dm_table_get_size(table)) {
1186 		DMWARN("Target message sector outside device.");
1187 		r = -EINVAL;
1188 		goto out_table;
1189 	}
1190 
1191 	ti = dm_table_find_target(table, tmsg->sector);
1192 	if (ti->type->message)
1193 		r = ti->type->message(ti, argc, argv);
1194 	else {
1195 		DMWARN("Target type does not support messages");
1196 		r = -EINVAL;
1197 	}
1198 
1199  out_table:
1200 	dm_table_put(table);
1201  out_argv:
1202 	kfree(argv);
1203  out:
1204 	param->data_size = 0;
1205 	dm_put(md);
1206 	return r;
1207 }
1208 
1209 /*-----------------------------------------------------------------
1210  * Implementation of open/close/ioctl on the special char
1211  * device.
1212  *---------------------------------------------------------------*/
1213 static ioctl_fn lookup_ioctl(unsigned int cmd)
1214 {
1215 	static struct {
1216 		int cmd;
1217 		ioctl_fn fn;
1218 	} _ioctls[] = {
1219 		{DM_VERSION_CMD, NULL},	/* version is dealt with elsewhere */
1220 		{DM_REMOVE_ALL_CMD, remove_all},
1221 		{DM_LIST_DEVICES_CMD, list_devices},
1222 
1223 		{DM_DEV_CREATE_CMD, dev_create},
1224 		{DM_DEV_REMOVE_CMD, dev_remove},
1225 		{DM_DEV_RENAME_CMD, dev_rename},
1226 		{DM_DEV_SUSPEND_CMD, dev_suspend},
1227 		{DM_DEV_STATUS_CMD, dev_status},
1228 		{DM_DEV_WAIT_CMD, dev_wait},
1229 
1230 		{DM_TABLE_LOAD_CMD, table_load},
1231 		{DM_TABLE_CLEAR_CMD, table_clear},
1232 		{DM_TABLE_DEPS_CMD, table_deps},
1233 		{DM_TABLE_STATUS_CMD, table_status},
1234 
1235 		{DM_LIST_VERSIONS_CMD, list_versions},
1236 
1237 		{DM_TARGET_MSG_CMD, target_message}
1238 	};
1239 
1240 	return (cmd >= ARRAY_SIZE(_ioctls)) ? NULL : _ioctls[cmd].fn;
1241 }
1242 
1243 /*
1244  * As well as checking the version compatibility this always
1245  * copies the kernel interface version out.
1246  */
1247 static int check_version(unsigned int cmd, struct dm_ioctl __user *user)
1248 {
1249 	uint32_t version[3];
1250 	int r = 0;
1251 
1252 	if (copy_from_user(version, user->version, sizeof(version)))
1253 		return -EFAULT;
1254 
1255 	if ((DM_VERSION_MAJOR != version[0]) ||
1256 	    (DM_VERSION_MINOR < version[1])) {
1257 		DMWARN("ioctl interface mismatch: "
1258 		       "kernel(%u.%u.%u), user(%u.%u.%u), cmd(%d)",
1259 		       DM_VERSION_MAJOR, DM_VERSION_MINOR,
1260 		       DM_VERSION_PATCHLEVEL,
1261 		       version[0], version[1], version[2], cmd);
1262 		r = -EINVAL;
1263 	}
1264 
1265 	/*
1266 	 * Fill in the kernel version.
1267 	 */
1268 	version[0] = DM_VERSION_MAJOR;
1269 	version[1] = DM_VERSION_MINOR;
1270 	version[2] = DM_VERSION_PATCHLEVEL;
1271 	if (copy_to_user(user->version, version, sizeof(version)))
1272 		return -EFAULT;
1273 
1274 	return r;
1275 }
1276 
1277 static void free_params(struct dm_ioctl *param)
1278 {
1279 	vfree(param);
1280 }
1281 
1282 static int copy_params(struct dm_ioctl __user *user, struct dm_ioctl **param)
1283 {
1284 	struct dm_ioctl tmp, *dmi;
1285 
1286 	if (copy_from_user(&tmp, user, sizeof(tmp)))
1287 		return -EFAULT;
1288 
1289 	if (tmp.data_size < sizeof(tmp))
1290 		return -EINVAL;
1291 
1292 	dmi = (struct dm_ioctl *) vmalloc(tmp.data_size);
1293 	if (!dmi)
1294 		return -ENOMEM;
1295 
1296 	if (copy_from_user(dmi, user, tmp.data_size)) {
1297 		vfree(dmi);
1298 		return -EFAULT;
1299 	}
1300 
1301 	*param = dmi;
1302 	return 0;
1303 }
1304 
1305 static int validate_params(uint cmd, struct dm_ioctl *param)
1306 {
1307 	/* Always clear this flag */
1308 	param->flags &= ~DM_BUFFER_FULL_FLAG;
1309 
1310 	/* Ignores parameters */
1311 	if (cmd == DM_REMOVE_ALL_CMD ||
1312 	    cmd == DM_LIST_DEVICES_CMD ||
1313 	    cmd == DM_LIST_VERSIONS_CMD)
1314 		return 0;
1315 
1316 	if ((cmd == DM_DEV_CREATE_CMD)) {
1317 		if (!*param->name) {
1318 			DMWARN("name not supplied when creating device");
1319 			return -EINVAL;
1320 		}
1321 	} else if ((*param->uuid && *param->name)) {
1322 		DMWARN("only supply one of name or uuid, cmd(%u)", cmd);
1323 		return -EINVAL;
1324 	}
1325 
1326 	/* Ensure strings are terminated */
1327 	param->name[DM_NAME_LEN - 1] = '\0';
1328 	param->uuid[DM_UUID_LEN - 1] = '\0';
1329 
1330 	return 0;
1331 }
1332 
1333 static int ctl_ioctl(struct inode *inode, struct file *file,
1334 		     uint command, ulong u)
1335 {
1336 	int r = 0;
1337 	unsigned int cmd;
1338 	struct dm_ioctl *param;
1339 	struct dm_ioctl __user *user = (struct dm_ioctl __user *) u;
1340 	ioctl_fn fn = NULL;
1341 	size_t param_size;
1342 
1343 	/* only root can play with this */
1344 	if (!capable(CAP_SYS_ADMIN))
1345 		return -EACCES;
1346 
1347 	if (_IOC_TYPE(command) != DM_IOCTL)
1348 		return -ENOTTY;
1349 
1350 	cmd = _IOC_NR(command);
1351 
1352 	/*
1353 	 * Check the interface version passed in.  This also
1354 	 * writes out the kernel's interface version.
1355 	 */
1356 	r = check_version(cmd, user);
1357 	if (r)
1358 		return r;
1359 
1360 	/*
1361 	 * Nothing more to do for the version command.
1362 	 */
1363 	if (cmd == DM_VERSION_CMD)
1364 		return 0;
1365 
1366 	fn = lookup_ioctl(cmd);
1367 	if (!fn) {
1368 		DMWARN("dm_ctl_ioctl: unknown command 0x%x", command);
1369 		return -ENOTTY;
1370 	}
1371 
1372 	/*
1373 	 * Trying to avoid low memory issues when a device is
1374 	 * suspended.
1375 	 */
1376 	current->flags |= PF_MEMALLOC;
1377 
1378 	/*
1379 	 * Copy the parameters into kernel space.
1380 	 */
1381 	r = copy_params(user, &param);
1382 
1383 	current->flags &= ~PF_MEMALLOC;
1384 
1385 	if (r)
1386 		return r;
1387 
1388 	r = validate_params(cmd, param);
1389 	if (r)
1390 		goto out;
1391 
1392 	param_size = param->data_size;
1393 	param->data_size = sizeof(*param);
1394 	r = fn(param, param_size);
1395 
1396 	/*
1397 	 * Copy the results back to userland.
1398 	 */
1399 	if (!r && copy_to_user(user, param, param->data_size))
1400 		r = -EFAULT;
1401 
1402  out:
1403 	free_params(param);
1404 	return r;
1405 }
1406 
1407 static struct file_operations _ctl_fops = {
1408 	.ioctl	 = ctl_ioctl,
1409 	.owner	 = THIS_MODULE,
1410 };
1411 
1412 static struct miscdevice _dm_misc = {
1413 	.minor 		= MISC_DYNAMIC_MINOR,
1414 	.name  		= DM_NAME,
1415 	.devfs_name 	= "mapper/control",
1416 	.fops  		= &_ctl_fops
1417 };
1418 
1419 /*
1420  * Create misc character device and link to DM_DIR/control.
1421  */
1422 int __init dm_interface_init(void)
1423 {
1424 	int r;
1425 
1426 	r = dm_hash_init();
1427 	if (r)
1428 		return r;
1429 
1430 	r = misc_register(&_dm_misc);
1431 	if (r) {
1432 		DMERR("misc_register failed for control device");
1433 		dm_hash_exit();
1434 		return r;
1435 	}
1436 
1437 	DMINFO("%d.%d.%d%s initialised: %s", DM_VERSION_MAJOR,
1438 	       DM_VERSION_MINOR, DM_VERSION_PATCHLEVEL, DM_VERSION_EXTRA,
1439 	       DM_DRIVER_EMAIL);
1440 	return 0;
1441 }
1442 
1443 void dm_interface_exit(void)
1444 {
1445 	if (misc_deregister(&_dm_misc) < 0)
1446 		DMERR("misc_deregister failed for control device");
1447 
1448 	dm_hash_exit();
1449 }
1450