xref: /dragonfly/sys/dev/disk/dm/dm_ioctl.c (revision a32bc35d)
1 /* $NetBSD: dm_ioctl.c,v 1.21 2010/02/25 20:48:58 jakllsch Exp $      */
2 
3 /*
4  * Copyright (c) 2010-2011 Alex Hornung <alex@alexhornung.com>
5  * Copyright (c) 2008 The NetBSD Foundation, Inc.
6  * All rights reserved.
7  *
8  * This code is derived from software contributed to The NetBSD Foundation
9  * by Adam Hamsik.
10  *
11  * Redistribution and use in source and binary forms, with or without
12  * modification, are permitted provided that the following conditions
13  * are met:
14  * 1. Redistributions of source code must retain the above copyright
15  *    notice, this list of conditions and the following disclaimer.
16  * 2. Redistributions in binary form must reproduce the above copyright
17  *    notice, this list of conditions and the following disclaimer in the
18  *    documentation and/or other materials provided with the distribution.
19  *
20  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
21  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
22  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
23  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
24  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
25  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
26  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
27  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
28  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
29  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
30  * POSSIBILITY OF SUCH DAMAGE.
31  */
32 
33 /*
34  * Locking is used to synchronise between ioctl calls and between dm_table's
35  * users.
36  *
37  * ioctl locking:
38  * Simple reference counting, to count users of device will be used routines
39  * dm_dev_busy/dm_dev_unbusy are used for that.
40  * dm_dev_lookup/dm_dev_rem call dm_dev_busy before return(caller is therefore
41  * holder of reference_counter last).
42  *
43  * ioctl routines which change/remove dm_dev parameters must wait on
44  * dm_dev::dev_cv and when last user will call dm_dev_unbusy they will wake
45  * up them.
46  *
47  * table_head locking:
48  * To access table entries dm_table_* routines must be used.
49  *
50  * dm_table_get_entry will increment table users reference
51  * counter. It will return active or inactive table depends
52  * on uint8_t argument.
53  *
54  * dm_table_release must be called for every table_entry from
55  * dm_table_get_entry. Between these to calls tables can'tbe switched
56  * or destroyed.
57  *
58  * dm_table_head_init initialize talbe_entries SLISTS and io_cv.
59  *
60  * dm_table_head_destroy destroy cv.
61  *
62  * There are two types of users for dm_table_head first type will
63  * only read list and try to do anything with it e.g. dmstrategy,
64  * dm_table_size etc. There is another user for table_head which wants
65  * to change table lists e.g. dm_dev_resume_ioctl, dm_dev_remove_ioctl,
66  * dm_table_clear_ioctl.
67  *
68  * NOTE: It is not allowed to call dm_table_destroy, dm_table_switch_tables
69  *       with hold table reference counter. Table reference counter is hold
70  *       after calling dm_table_get_entry routine. After calling this
71  *       function user must call dm_table_release before any writer table
72  *       operation.
73  *
74  * Example: dm_table_get_entry
75  *          dm_table_destroy/dm_table_switch_tables
76  * This exaple will lead to deadlock situation because after dm_table_get_entry
77  * table reference counter is != 0 and dm_table_destroy have to wait on cv until
78  * reference counter is 0.
79  *
80  */
81 
82 #include <sys/types.h>
83 #include <sys/param.h>
84 #include <sys/device.h>
85 #include <sys/malloc.h>
86 #include <sys/vnode.h>
87 #include <dev/disk/dm/dm.h>
88 
89 #include "netbsd-dm.h"
90 
91 #define DM_REMOVE_FLAG(flag, name) do {					\
92 		prop_dictionary_get_uint32(dm_dict,DM_IOCTL_FLAGS,&flag); \
93 		flag &= ~name;						\
94 		prop_dictionary_set_uint32(dm_dict,DM_IOCTL_FLAGS,flag); \
95 } while (/*CONSTCOND*/0)
96 
97 #define DM_ADD_FLAG(flag, name) do {					\
98 		prop_dictionary_get_uint32(dm_dict,DM_IOCTL_FLAGS,&flag); \
99 		flag |= name;						\
100 		prop_dictionary_set_uint32(dm_dict,DM_IOCTL_FLAGS,flag); \
101 } while (/*CONSTCOND*/0)
102 
103 static int dm_dbg_print_flags(int);
104 
105 /*
106  * Print flags sent to the kernel from libevmapper.
107  */
108 static int
109 dm_dbg_print_flags(int flags)
110 {
111 	aprint_debug("dbg_print --- %d\n", flags);
112 
113 	if (flags & DM_READONLY_FLAG)
114 		aprint_debug("dbg_flags: DM_READONLY_FLAG set In/Out\n");
115 
116 	if (flags & DM_SUSPEND_FLAG)
117 		aprint_debug("dbg_flags: DM_SUSPEND_FLAG set In/Out \n");
118 
119 	if (flags & DM_PERSISTENT_DEV_FLAG)
120 		aprint_debug("db_flags: DM_PERSISTENT_DEV_FLAG set In\n");
121 
122 	if (flags & DM_STATUS_TABLE_FLAG)
123 		aprint_debug("dbg_flags: DM_STATUS_TABLE_FLAG set In\n");
124 
125 	if (flags & DM_ACTIVE_PRESENT_FLAG)
126 		aprint_debug("dbg_flags: DM_ACTIVE_PRESENT_FLAG set Out\n");
127 
128 	if (flags & DM_INACTIVE_PRESENT_FLAG)
129 		aprint_debug("dbg_flags: DM_INACTIVE_PRESENT_FLAG set Out\n");
130 
131 	if (flags & DM_BUFFER_FULL_FLAG)
132 		aprint_debug("dbg_flags: DM_BUFFER_FULL_FLAG set Out\n");
133 
134 	if (flags & DM_SKIP_BDGET_FLAG)
135 		aprint_debug("dbg_flags: DM_SKIP_BDGET_FLAG set In\n");
136 
137 	if (flags & DM_SKIP_LOCKFS_FLAG)
138 		aprint_debug("dbg_flags: DM_SKIP_LOCKFS_FLAG set In\n");
139 
140 	if (flags & DM_NOFLUSH_FLAG)
141 		aprint_debug("dbg_flags: DM_NOFLUSH_FLAG set In\n");
142 
143 	return 0;
144 }
145 /*
146  * Get version ioctl call I do it as default therefore this
147  * function is unused now.
148  */
149 int
150 dm_get_version_ioctl(prop_dictionary_t dm_dict)
151 {
152 	return 0;
153 }
154 /*
155  * Get list of all available targets from global
156  * target list and sent them back to libdevmapper.
157  */
158 int
159 dm_list_versions_ioctl(prop_dictionary_t dm_dict)
160 {
161 	prop_array_t target_list;
162 	uint32_t flags;
163 
164 	flags = 0;
165 
166 	prop_dictionary_get_uint32(dm_dict, DM_IOCTL_FLAGS, &flags);
167 
168 	dm_dbg_print_flags(flags);
169 	target_list = dm_target_prop_list();
170 
171 	prop_dictionary_set(dm_dict, DM_IOCTL_CMD_DATA, target_list);
172 	prop_object_release(target_list);
173 
174 	return 0;
175 }
176 /*
177  * Create in-kernel entry for device. Device attributes such as name, uuid are
178  * taken from proplib dictionary.
179  *
180  */
181 int
182 dm_dev_create_ioctl(prop_dictionary_t dm_dict)
183 {
184 	dm_dev_t *dmv;
185 	const char *name, *uuid;
186 	int r, flags;
187 
188 	r = 0;
189 	flags = 0;
190 	name = NULL;
191 	uuid = NULL;
192 
193 	/* Get needed values from dictionary. */
194 	prop_dictionary_get_cstring_nocopy(dm_dict, DM_IOCTL_NAME, &name);
195 	prop_dictionary_get_cstring_nocopy(dm_dict, DM_IOCTL_UUID, &uuid);
196 	prop_dictionary_get_uint32(dm_dict, DM_IOCTL_FLAGS, &flags);
197 
198 	dm_dbg_print_flags(flags);
199 
200 	/* Lookup name and uuid if device already exist quit. */
201 	if ((dmv = dm_dev_lookup(name, uuid, -1)) != NULL) {
202 		DM_ADD_FLAG(flags, DM_EXISTS_FLAG);	/* Device already exists */
203 		dm_dev_unbusy(dmv);
204 		return EEXIST;
205 	}
206 
207 	r = dm_dev_create(&dmv, name, uuid, flags);
208 
209 	if (r == 0) {
210 		prop_dictionary_set_uint32(dm_dict, DM_IOCTL_MINOR, dmv->minor);
211 		DM_ADD_FLAG(flags, DM_EXISTS_FLAG);
212 		DM_REMOVE_FLAG(flags, DM_INACTIVE_PRESENT_FLAG);
213 	}
214 
215 	return r;
216 }
217 /*
218  * Get list of created device-mapper devices fromglobal list and
219  * send it to kernel.
220  *
221  * Output dictionary:
222  *
223  * <key>cmd_data</key>
224  *  <array>
225  *   <dict>
226  *    <key>name<key>
227  *    <string>...</string>
228  *
229  *    <key>dev</key>
230  *    <integer>...</integer>
231  *   </dict>
232  *  </array>
233  *
234  */
235 int
236 dm_dev_list_ioctl(prop_dictionary_t dm_dict)
237 {
238 	prop_array_t dev_list;
239 
240 	uint32_t flags;
241 
242 	flags = 0;
243 
244 	prop_dictionary_get_uint32(dm_dict, DM_IOCTL_FLAGS, &flags);
245 
246 	dm_dbg_print_flags(flags);
247 
248 	dev_list = dm_dev_prop_list();
249 
250 	prop_dictionary_set(dm_dict, DM_IOCTL_CMD_DATA, dev_list);
251 	prop_object_release(dev_list);
252 
253 	return 0;
254 }
255 /*
256  * Rename selected devices old name is in struct dm_ioctl.
257  * newname is taken from dictionary
258  *
259  * <key>cmd_data</key>
260  *  <array>
261  *   <string>...</string>
262  *  </array>
263  */
264 int
265 dm_dev_rename_ioctl(prop_dictionary_t dm_dict)
266 {
267 #if 0
268 	prop_array_t cmd_array;
269 	dm_dev_t *dmv;
270 
271 	const char *name, *uuid, *n_name;
272 	uint32_t flags, minor;
273 
274 	name = NULL;
275 	uuid = NULL;
276 	minor = 0;
277 
278 	/* Get needed values from dictionary. */
279 	prop_dictionary_get_cstring_nocopy(dm_dict, DM_IOCTL_NAME, &name);
280 	prop_dictionary_get_cstring_nocopy(dm_dict, DM_IOCTL_UUID, &uuid);
281 	prop_dictionary_get_uint32(dm_dict, DM_IOCTL_FLAGS, &flags);
282 	prop_dictionary_get_uint32(dm_dict, DM_IOCTL_MINOR, &minor);
283 
284 	dm_dbg_print_flags(flags);
285 
286 	cmd_array = prop_dictionary_get(dm_dict, DM_IOCTL_CMD_DATA);
287 
288 	prop_array_get_cstring_nocopy(cmd_array, 0, &n_name);
289 
290 	if (strlen(n_name) + 1 > DM_NAME_LEN)
291 		return EINVAL;
292 
293 	if ((dmv = dm_dev_rem(NULL, name, uuid, minor)) == NULL) {
294 		DM_REMOVE_FLAG(flags, DM_EXISTS_FLAG);
295 		return ENOENT;
296 	}
297 	/* change device name */
298 	/*
299 	 * XXX How to deal with this change, name only used in
300 	 * dm_dev_routines, should I add dm_dev_change_name which will run
301 	 * under the dm_dev_list mutex ?
302 	 */
303 	strlcpy(dmv->name, n_name, DM_NAME_LEN);
304 
305 	prop_dictionary_set_uint32(dm_dict, DM_IOCTL_OPEN, dmv->table_head.io_cnt);
306 	prop_dictionary_set_uint32(dm_dict, DM_IOCTL_MINOR, dmv->minor);
307 	prop_dictionary_set_cstring(dm_dict, DM_IOCTL_UUID, dmv->uuid);
308 
309 	dm_dev_insert(dmv);
310 #endif
311 
312 	/*
313 	 * XXX: the rename is not yet implemented. The main complication
314 	 *	here is devfs. We'd probably need a new function, rename_dev()
315 	 *	that would trigger a node rename in devfs.
316 	 */
317 	kprintf("dm_dev_rename_ioctl called, but not implemented!\n");
318 	return 0;
319 }
320 
321 /*
322  * Remove device
323  */
324 int
325 dm_dev_remove_ioctl(prop_dictionary_t dm_dict)
326 {
327 	dm_dev_t *dmv;
328 	const char *name, *uuid;
329 	uint32_t flags, minor, is_open;
330 
331 	flags = 0;
332 	name = NULL;
333 	uuid = NULL;
334 
335 	/* Get needed values from dictionary. */
336 	prop_dictionary_get_cstring_nocopy(dm_dict, DM_IOCTL_NAME, &name);
337 	prop_dictionary_get_cstring_nocopy(dm_dict, DM_IOCTL_UUID, &uuid);
338 	prop_dictionary_get_uint32(dm_dict, DM_IOCTL_FLAGS, &flags);
339 	prop_dictionary_get_uint32(dm_dict, DM_IOCTL_MINOR, &minor);
340 
341 	dm_dbg_print_flags(flags);
342 
343 	if ((dmv = dm_dev_lookup(name, uuid, minor)) == NULL) {
344 		DM_REMOVE_FLAG(flags, DM_EXISTS_FLAG);
345 		return ENOENT;
346 	}
347 
348 	is_open = dmv->is_open;
349 
350 	dm_dev_unbusy(dmv);
351 
352 	if (is_open)
353 		return EBUSY;
354 
355 	/*
356 	 * This will call dm_detach routine which will actually remove
357 	 * device.
358 	 */
359 	return dm_dev_remove(dmv);
360 }
361 
362 /*
363  * Try to remove all devices
364  */
365 int
366 dm_dev_remove_all_ioctl(prop_dictionary_t dm_dict)
367 {
368 	uint32_t flags = 0;
369 
370 	/* Get needed values from dictionary. */
371 	prop_dictionary_get_uint32(dm_dict, DM_IOCTL_FLAGS, &flags);
372 
373 	dm_dbg_print_flags(flags);
374 
375 	/* Gently remove all devices, if possible */
376 	return dm_dev_remove_all(1);
377 }
378 
379 /*
380  * Return actual state of device to libdevmapper.
381  */
382 int
383 dm_dev_status_ioctl(prop_dictionary_t dm_dict)
384 {
385 	dm_dev_t *dmv;
386 	const char *name, *uuid;
387 	uint32_t flags, j, minor;
388 
389 	name = NULL;
390 	uuid = NULL;
391 	flags = 0;
392 	j = 0;
393 
394 	prop_dictionary_get_cstring_nocopy(dm_dict, DM_IOCTL_NAME, &name);
395 	prop_dictionary_get_cstring_nocopy(dm_dict, DM_IOCTL_UUID, &uuid);
396 	prop_dictionary_get_uint32(dm_dict, DM_IOCTL_FLAGS, &flags);
397 	prop_dictionary_get_uint32(dm_dict, DM_IOCTL_MINOR, &minor);
398 
399 	if ((dmv = dm_dev_lookup(name, uuid, minor)) == NULL) {
400 		DM_REMOVE_FLAG(flags, DM_EXISTS_FLAG);
401 		return ENOENT;
402 	}
403 	dm_dbg_print_flags(dmv->flags);
404 
405 	prop_dictionary_set_uint32(dm_dict, DM_IOCTL_OPEN, dmv->table_head.io_cnt);
406 	prop_dictionary_set_uint32(dm_dict, DM_IOCTL_MINOR, dmv->minor);
407 	prop_dictionary_set_cstring(dm_dict, DM_IOCTL_UUID, dmv->uuid);
408 
409 	if (dmv->flags & DM_SUSPEND_FLAG)
410 		DM_ADD_FLAG(flags, DM_SUSPEND_FLAG);
411 
412 	/*
413 	 * Add status flags for tables I have to check both active and
414 	 * inactive tables.
415 	 */
416 	if ((j = dm_table_get_target_count(&dmv->table_head, DM_TABLE_ACTIVE))) {
417 		DM_ADD_FLAG(flags, DM_ACTIVE_PRESENT_FLAG);
418 	} else
419 		DM_REMOVE_FLAG(flags, DM_ACTIVE_PRESENT_FLAG);
420 
421 	prop_dictionary_set_uint32(dm_dict, DM_IOCTL_TARGET_COUNT, j);
422 
423 	if (dm_table_get_target_count(&dmv->table_head, DM_TABLE_INACTIVE))
424 		DM_ADD_FLAG(flags, DM_INACTIVE_PRESENT_FLAG);
425 	else
426 		DM_REMOVE_FLAG(flags, DM_INACTIVE_PRESENT_FLAG);
427 
428 	dm_dev_unbusy(dmv);
429 
430 	return 0;
431 }
432 /*
433  * Set only flag to suggest that device is suspended. This call is
434  * not supported in NetBSD.
435  *
436  */
437 int
438 dm_dev_suspend_ioctl(prop_dictionary_t dm_dict)
439 {
440 	dm_dev_t *dmv;
441 	const char *name, *uuid;
442 	uint32_t flags, minor;
443 
444 	name = NULL;
445 	uuid = NULL;
446 	flags = 0;
447 
448 	prop_dictionary_get_cstring_nocopy(dm_dict, DM_IOCTL_NAME, &name);
449 	prop_dictionary_get_cstring_nocopy(dm_dict, DM_IOCTL_UUID, &uuid);
450 	prop_dictionary_get_uint32(dm_dict, DM_IOCTL_FLAGS, &flags);
451 	prop_dictionary_get_uint32(dm_dict, DM_IOCTL_MINOR, &minor);
452 
453 	if ((dmv = dm_dev_lookup(name, uuid, minor)) == NULL) {
454 		DM_REMOVE_FLAG(flags, DM_EXISTS_FLAG);
455 		return ENOENT;
456 	}
457 	atomic_set_int(&dmv->flags, DM_SUSPEND_FLAG);
458 
459 	dm_dbg_print_flags(dmv->flags);
460 
461 	prop_dictionary_set_uint32(dm_dict, DM_IOCTL_OPEN, dmv->table_head.io_cnt);
462 	prop_dictionary_set_uint32(dm_dict, DM_IOCTL_FLAGS, dmv->flags);
463 	prop_dictionary_set_uint32(dm_dict, DM_IOCTL_MINOR, dmv->minor);
464 
465 	dm_dev_unbusy(dmv);
466 
467 	/* Add flags to dictionary flag after dmv -> dict copy */
468 	DM_ADD_FLAG(flags, DM_EXISTS_FLAG);
469 
470 	return 0;
471 }
472 /*
473  * Simulate Linux behaviour better and switch tables here and not in
474  * dm_table_load_ioctl.
475  */
476 int
477 dm_dev_resume_ioctl(prop_dictionary_t dm_dict)
478 {
479 	dm_dev_t *dmv;
480 	const char *name, *uuid;
481 	uint32_t flags, minor;
482 
483 	name = NULL;
484 	uuid = NULL;
485 	flags = 0;
486 
487 	/*
488 	 * char *xml; xml = prop_dictionary_externalize(dm_dict);
489 	 * printf("%s\n",xml);
490 	 */
491 
492 	prop_dictionary_get_cstring_nocopy(dm_dict, DM_IOCTL_NAME, &name);
493 	prop_dictionary_get_cstring_nocopy(dm_dict, DM_IOCTL_UUID, &uuid);
494 	prop_dictionary_get_uint32(dm_dict, DM_IOCTL_FLAGS, &flags);
495 	prop_dictionary_get_uint32(dm_dict, DM_IOCTL_MINOR, &minor);
496 
497 	/* Remove device from global device list */
498 	if ((dmv = dm_dev_lookup(name, uuid, minor)) == NULL) {
499 		DM_REMOVE_FLAG(flags, DM_EXISTS_FLAG);
500 		return ENOENT;
501 	}
502 	atomic_clear_int(&dmv->flags, (DM_SUSPEND_FLAG | DM_INACTIVE_PRESENT_FLAG));
503 	atomic_set_int(&dmv->flags, DM_ACTIVE_PRESENT_FLAG);
504 
505 	dm_table_switch_tables(&dmv->table_head);
506 
507 	DM_ADD_FLAG(flags, DM_EXISTS_FLAG);
508 
509 	dmsetdiskinfo(dmv->diskp, &dmv->table_head);
510 
511 	prop_dictionary_set_uint32(dm_dict, DM_IOCTL_OPEN, dmv->table_head.io_cnt);
512 	prop_dictionary_set_uint32(dm_dict, DM_IOCTL_FLAGS, flags);
513 	prop_dictionary_set_uint32(dm_dict, DM_IOCTL_MINOR, dmv->minor);
514 
515 	dm_dev_unbusy(dmv);
516 
517 	/* Destroy inactive table after resume. */
518 	dm_table_destroy(&dmv->table_head, DM_TABLE_INACTIVE);
519 
520 	return 0;
521 }
522 /*
523  * Table management routines
524  * lvm2tools doens't send name/uuid to kernel with table
525  * for lookup I have to use minor number.
526  */
527 
528 /*
529  * Remove inactive table from device. Routines which work's with inactive tables
530  * doesn't need to synchronise with dmstrategy. They can synchronise themselves with mutex?.
531  *
532  */
533 int
534 dm_table_clear_ioctl(prop_dictionary_t dm_dict)
535 {
536 	dm_dev_t *dmv;
537 	const char *name, *uuid;
538 	uint32_t flags, minor;
539 
540 	dmv = NULL;
541 	name = NULL;
542 	uuid = NULL;
543 	flags = 0;
544 	minor = 0;
545 
546 	prop_dictionary_get_cstring_nocopy(dm_dict, DM_IOCTL_NAME, &name);
547 	prop_dictionary_get_cstring_nocopy(dm_dict, DM_IOCTL_UUID, &uuid);
548 	prop_dictionary_get_uint32(dm_dict, DM_IOCTL_FLAGS, &flags);
549 	prop_dictionary_get_uint32(dm_dict, DM_IOCTL_MINOR, &minor);
550 
551 	aprint_debug("Clearing inactive table from device: %s--%s\n",
552 	    name, uuid);
553 
554 	if ((dmv = dm_dev_lookup(name, uuid, minor)) == NULL) {
555 		DM_REMOVE_FLAG(flags, DM_EXISTS_FLAG);
556 		return ENOENT;
557 	}
558 	/* Select unused table */
559 	dm_table_destroy(&dmv->table_head, DM_TABLE_INACTIVE);
560 
561 	atomic_clear_int(&dmv->flags, DM_INACTIVE_PRESENT_FLAG);
562 
563 	dm_dev_unbusy(dmv);
564 
565 	return 0;
566 }
567 /*
568  * Get list of physical devices for active table.
569  * Get dev_t from pdev vnode and insert it into cmd_array.
570  *
571  * XXX. This function is called from lvm2tools to get information
572  *      about physical devices, too e.g. during vgcreate.
573  */
574 int
575 dm_table_deps_ioctl(prop_dictionary_t dm_dict)
576 {
577 	dm_dev_t *dmv;
578 	dm_table_t *tbl;
579 	dm_table_entry_t *table_en;
580 
581 	prop_array_t cmd_array;
582 	const char *name, *uuid;
583 	uint32_t flags, minor;
584 
585 	int table_type;
586 	size_t i;
587 
588 	name = NULL;
589 	uuid = NULL;
590 	dmv = NULL;
591 	flags = 0;
592 
593 	i = 0;
594 
595 	prop_dictionary_get_cstring_nocopy(dm_dict, DM_IOCTL_NAME, &name);
596 	prop_dictionary_get_cstring_nocopy(dm_dict, DM_IOCTL_UUID, &uuid);
597 	prop_dictionary_get_uint32(dm_dict, DM_IOCTL_FLAGS, &flags);
598 	prop_dictionary_get_uint32(dm_dict, DM_IOCTL_MINOR, &minor);
599 
600 	/* create array for dev_t's */
601 	cmd_array = prop_array_create();
602 
603 	if ((dmv = dm_dev_lookup(name, uuid, minor)) == NULL) {
604 		DM_REMOVE_FLAG(flags, DM_EXISTS_FLAG);
605 		return ENOENT;
606 	}
607 	prop_dictionary_set_uint32(dm_dict, DM_IOCTL_MINOR, dmv->minor);
608 	prop_dictionary_set_cstring(dm_dict, DM_IOCTL_NAME, dmv->name);
609 	prop_dictionary_set_cstring(dm_dict, DM_IOCTL_UUID, dmv->uuid);
610 
611 	aprint_debug("Getting table deps for device: %s\n", dmv->name);
612 
613 	/*
614 	 * if DM_QUERY_INACTIVE_TABLE_FLAG is passed we need to query
615 	 * INACTIVE TABLE
616 	 */
617 	if (flags & DM_QUERY_INACTIVE_TABLE_FLAG)
618 		table_type = DM_TABLE_INACTIVE;
619 	else
620 		table_type = DM_TABLE_ACTIVE;
621 
622 	tbl = dm_table_get_entry(&dmv->table_head, table_type);
623 
624 	SLIST_FOREACH(table_en, tbl, next)
625 	    table_en->target->deps(table_en, cmd_array);
626 
627 	dm_table_release(&dmv->table_head, table_type);
628 	dm_dev_unbusy(dmv);
629 
630 	prop_dictionary_set(dm_dict, DM_IOCTL_CMD_DATA, cmd_array);
631 	prop_object_release(cmd_array);
632 
633 	return 0;
634 }
635 /*
636  * Load new table/tables to device.
637  * Call apropriate target init routine open all physical pdev's and
638  * link them to device. For other targets mirror, strip, snapshot
639  * etc. also add dependency devices to upcalls list.
640  *
641  * Load table to inactive slot table are switched in dm_device_resume_ioctl.
642  * This simulates Linux behaviour better there should not be any difference.
643  *
644  */
645 int
646 dm_table_load_ioctl(prop_dictionary_t dm_dict)
647 {
648 	dm_dev_t *dmv;
649 	dm_table_entry_t *table_en, *last_table;
650 	dm_table_t *tbl;
651 	dm_target_t *target;
652 
653 	prop_object_iterator_t iter;
654 	prop_array_t cmd_array;
655 	prop_dictionary_t target_dict;
656 
657 	const char *name, *uuid, *type;
658 
659 	uint32_t flags, ret, minor;
660 
661 	char *str;
662 
663 	ret = 0;
664 	flags = 0;
665 	name = NULL;
666 	uuid = NULL;
667 	dmv = NULL;
668 	last_table = NULL;
669 	str = NULL;
670 
671 	/*
672 	 * char *xml; xml = prop_dictionary_externalize(dm_dict);
673 	 * printf("%s\n",xml);
674 	 */
675 
676 	prop_dictionary_get_cstring_nocopy(dm_dict, DM_IOCTL_NAME, &name);
677 	prop_dictionary_get_cstring_nocopy(dm_dict, DM_IOCTL_UUID, &uuid);
678 	prop_dictionary_get_uint32(dm_dict, DM_IOCTL_FLAGS, &flags);
679 	prop_dictionary_get_uint32(dm_dict, DM_IOCTL_MINOR, &minor);
680 
681 	cmd_array = prop_dictionary_get(dm_dict, DM_IOCTL_CMD_DATA);
682 	iter = prop_array_iterator(cmd_array);
683 	dm_dbg_print_flags(flags);
684 
685 	if ((dmv = dm_dev_lookup(name, uuid, minor)) == NULL) {
686 		DM_REMOVE_FLAG(flags, DM_EXISTS_FLAG);
687 		return ENOENT;
688 	}
689 	aprint_debug("Loading table to device: %s--%d\n", name,
690 	    dmv->table_head.cur_active_table);
691 
692 	/*
693 	 * I have to check if this table slot is not used by another table list.
694 	 * if it is used I should free them.
695 	 */
696 	if (dmv->flags & DM_INACTIVE_PRESENT_FLAG)
697 		dm_table_destroy(&dmv->table_head, DM_TABLE_INACTIVE);
698 
699 	dm_dbg_print_flags(dmv->flags);
700 	tbl = dm_table_get_entry(&dmv->table_head, DM_TABLE_INACTIVE);
701 
702 	aprint_debug("dmv->name = %s\n", dmv->name);
703 
704 	prop_dictionary_set_uint32(dm_dict, DM_IOCTL_MINOR, dmv->minor);
705 
706 	while ((target_dict = prop_object_iterator_next(iter)) != NULL) {
707 		prop_dictionary_get_cstring_nocopy(target_dict,
708 		    DM_TABLE_TYPE, &type);
709 		/*
710 		 * If we want to deny table with 2 or more different
711 		 * target we should do it here
712 		 */
713 		if (((target = dm_target_lookup(type)) == NULL) &&
714 		    ((target = dm_target_autoload(type)) == NULL)) {
715 			dm_table_release(&dmv->table_head, DM_TABLE_INACTIVE);
716 			dm_dev_unbusy(dmv);
717 			return ENOENT;
718 		}
719 		if ((table_en = kmalloc(sizeof(dm_table_entry_t),
720 			    M_DM, M_WAITOK)) == NULL) {
721 			dm_table_release(&dmv->table_head, DM_TABLE_INACTIVE);
722 			dm_dev_unbusy(dmv);
723 			dm_target_unbusy(target);
724 			return ENOMEM;
725 		}
726 		prop_dictionary_get_uint64(target_dict, DM_TABLE_START,
727 		    &table_en->start);
728 		prop_dictionary_get_uint64(target_dict, DM_TABLE_LENGTH,
729 		    &table_en->length);
730 
731 		aprint_debug("dm_ioctl.c... table_en->start = %ju, "
732 			     "table_en->length = %ju\n",
733 			     (uintmax_t)table_en->start,
734 			     (uintmax_t)table_en->length);
735 
736 		table_en->target = target;
737 		table_en->dm_dev = dmv;
738 		table_en->target_config = NULL;
739 
740 		/*
741 		 * There is a parameter string after dm_target_spec
742 		 * structure which  points to /dev/wd0a 284 part of
743 		 * table. String str points to this text. This can be
744 		 * null and therefore it should be checked before we try to
745 		 * use it.
746 		 */
747 		prop_dictionary_get_cstring(target_dict,
748 		    DM_TABLE_PARAMS, &str);
749 
750 		if (SLIST_EMPTY(tbl))
751 			/* insert this table to head */
752 			SLIST_INSERT_HEAD(tbl, table_en, next);
753 		else
754 			SLIST_INSERT_AFTER(last_table, table_en, next);
755 
756 		/*
757 		 * Params string is different for every target,
758 		 * therfore I have to pass it to target init
759 		 * routine and parse parameters there.
760 		 */
761 		aprint_debug("DM: str passed in is: %s", str);
762 		if ((ret = target->init(dmv, &table_en->target_config,
763 			    str)) != 0) {
764 
765 			dm_table_release(&dmv->table_head, DM_TABLE_INACTIVE);
766 			dm_table_destroy(&dmv->table_head, DM_TABLE_INACTIVE);
767 			kfree(str, M_TEMP);
768 
769 			dm_dev_unbusy(dmv);
770 			dm_target_unbusy(target);
771 			return ret;
772 		}
773 		last_table = table_en;
774 		kfree(str, M_TEMP);
775 	}
776 	prop_object_iterator_release(iter);
777 
778 	DM_ADD_FLAG(flags, DM_INACTIVE_PRESENT_FLAG);
779 	atomic_set_int(&dmv->flags, DM_INACTIVE_PRESENT_FLAG);
780 
781 	dm_table_release(&dmv->table_head, DM_TABLE_INACTIVE);
782 
783 	dm_dev_unbusy(dmv);
784 #if 0
785 	dmsetdiskinfo(dmv->diskp, &dmv->table_head);
786 #endif
787 	return 0;
788 }
789 /*
790  * Get description of all tables loaded to device from kernel
791  * and send it to libdevmapper.
792  *
793  * Output dictionary for every table:
794  *
795  * <key>cmd_data</key>
796  * <array>
797  *   <dict>
798  *    <key>type<key>
799  *    <string>...</string>
800  *
801  *    <key>start</key>
802  *    <integer>...</integer>
803  *
804  *    <key>length</key>
805  *    <integer>...</integer>
806  *
807  *    <key>params</key>
808  *    <string>...</string>
809  *   </dict>
810  * </array>
811  *
812  */
813 int
814 dm_table_status_ioctl(prop_dictionary_t dm_dict)
815 {
816 	dm_dev_t *dmv;
817 	dm_table_t *tbl;
818 	dm_table_entry_t *table_en;
819 
820 	prop_array_t cmd_array;
821 	prop_dictionary_t target_dict;
822 
823 	uint32_t rec_size, minor;
824 
825 	const char *name, *uuid;
826 	char *params;
827 	int flags;
828 	int table_type;
829 
830 	dmv = NULL;
831 	uuid = NULL;
832 	name = NULL;
833 	params = NULL;
834 	flags = 0;
835 	rec_size = 0;
836 
837 	prop_dictionary_get_cstring_nocopy(dm_dict, DM_IOCTL_NAME, &name);
838 	prop_dictionary_get_cstring_nocopy(dm_dict, DM_IOCTL_UUID, &uuid);
839 	prop_dictionary_get_uint32(dm_dict, DM_IOCTL_FLAGS, &flags);
840 	prop_dictionary_get_uint32(dm_dict, DM_IOCTL_MINOR, &minor);
841 
842 	cmd_array = prop_array_create();
843 
844 	if ((dmv = dm_dev_lookup(name, uuid, minor)) == NULL) {
845 		DM_REMOVE_FLAG(flags, DM_EXISTS_FLAG);
846 		return ENOENT;
847 	}
848 	/*
849 	 * if DM_QUERY_INACTIVE_TABLE_FLAG is passed we need to query
850 	 * INACTIVE TABLE
851 	 */
852 	if (flags & DM_QUERY_INACTIVE_TABLE_FLAG)
853 		table_type = DM_TABLE_INACTIVE;
854 	else
855 		table_type = DM_TABLE_ACTIVE;
856 
857 	if (dm_table_get_target_count(&dmv->table_head, DM_TABLE_ACTIVE))
858 		DM_ADD_FLAG(flags, DM_ACTIVE_PRESENT_FLAG);
859 	else {
860 		DM_REMOVE_FLAG(flags, DM_ACTIVE_PRESENT_FLAG);
861 
862 		if (dm_table_get_target_count(&dmv->table_head, DM_TABLE_INACTIVE))
863 			DM_ADD_FLAG(flags, DM_INACTIVE_PRESENT_FLAG);
864 		else {
865 			DM_REMOVE_FLAG(flags, DM_INACTIVE_PRESENT_FLAG);
866 		}
867 	}
868 
869 	if (dmv->flags & DM_SUSPEND_FLAG)
870 		DM_ADD_FLAG(flags, DM_SUSPEND_FLAG);
871 
872 	prop_dictionary_set_uint32(dm_dict, DM_IOCTL_MINOR, dmv->minor);
873 
874 	aprint_debug("Status of device tables: %s--%d\n",
875 	    name, dmv->table_head.cur_active_table);
876 
877 	tbl = dm_table_get_entry(&dmv->table_head, table_type);
878 
879 	SLIST_FOREACH(table_en, tbl, next) {
880 		target_dict = prop_dictionary_create();
881 		aprint_debug("%016" PRIu64 ", length %016" PRIu64
882 		    ", target %s\n", table_en->start, table_en->length,
883 		    table_en->target->name);
884 
885 		prop_dictionary_set_uint64(target_dict, DM_TABLE_START,
886 		    table_en->start);
887 		prop_dictionary_set_uint64(target_dict, DM_TABLE_LENGTH,
888 		    table_en->length);
889 
890 		prop_dictionary_set_cstring(target_dict, DM_TABLE_TYPE,
891 		    table_en->target->name);
892 
893 		/* dm_table_get_cur_actv.table ?? */
894 		prop_dictionary_set_int32(target_dict, DM_TABLE_STAT,
895 		    dmv->table_head.cur_active_table);
896 
897 		if (flags & DM_STATUS_TABLE_FLAG) {
898 			params = table_en->target->status
899 			    (table_en->target_config);
900 
901 			if (params != NULL) {
902 				prop_dictionary_set_cstring(target_dict,
903 				    DM_TABLE_PARAMS, params);
904 
905 				kfree(params, M_DM);
906 			}
907 		}
908 		prop_array_add(cmd_array, target_dict);
909 		prop_object_release(target_dict);
910 	}
911 
912 	dm_table_release(&dmv->table_head, table_type);
913 	dm_dev_unbusy(dmv);
914 
915 	prop_dictionary_set_uint32(dm_dict, DM_IOCTL_FLAGS, flags);
916 	prop_dictionary_set(dm_dict, DM_IOCTL_CMD_DATA, cmd_array);
917 	prop_object_release(cmd_array);
918 
919 	return 0;
920 }
921 
922 int
923 dm_message_ioctl(prop_dictionary_t dm_dict)
924 {
925 	dm_table_t  *tbl;
926 	dm_table_entry_t *table_en;
927 	dm_dev_t *dmv;
928 	const char *name, *uuid;
929 	uint32_t flags, minor;
930 	uint64_t table_start, table_end, sector;
931 	char *msg;
932 	int ret, found = 0;
933 
934 	flags = 0;
935 	name = NULL;
936 	uuid = NULL;
937 
938 	/* Get needed values from dictionary. */
939 	prop_dictionary_get_cstring_nocopy(dm_dict, DM_IOCTL_NAME, &name);
940 	prop_dictionary_get_cstring_nocopy(dm_dict, DM_IOCTL_UUID, &uuid);
941 	prop_dictionary_get_uint32(dm_dict, DM_IOCTL_FLAGS, &flags);
942 	prop_dictionary_get_uint32(dm_dict, DM_IOCTL_MINOR, &minor);
943 	prop_dictionary_get_uint64(dm_dict, DM_MESSAGE_SECTOR, &sector);
944 
945 	dm_dbg_print_flags(flags);
946 
947 	if ((dmv = dm_dev_lookup(name, uuid, minor)) == NULL) {
948 		DM_REMOVE_FLAG(flags, DM_EXISTS_FLAG);
949 		return ENOENT;
950 	}
951 
952 	/* Get message string */
953 	prop_dictionary_get_cstring(dm_dict, DM_MESSAGE_STR, &msg);
954 
955 	tbl = dm_table_get_entry(&dmv->table_head, DM_TABLE_ACTIVE);
956 
957 	ret = EINVAL;
958 
959 	if (sector == 0) {
960 		if (!SLIST_EMPTY(tbl)) {
961 			table_en = SLIST_FIRST(tbl);
962 			found = 1;
963 		}
964 	} else {
965 		SLIST_FOREACH(table_en, tbl, next) {
966 			table_start = table_en->start;
967 			table_end = table_start + (table_en->length);
968 
969 			if ((sector >= table_start) && (sector < table_end)) {
970 				found = 1;
971 				break;
972 			}
973 		}
974 	}
975 
976 	if (found) {
977 		if (table_en->target->message != NULL)
978 			ret = table_en->target->message(table_en, msg);
979 	}
980 
981 	dm_table_release(&dmv->table_head, DM_TABLE_ACTIVE);
982 
983 
984 	kfree(msg, M_TEMP);
985 	dm_dev_unbusy(dmv);
986 
987 	return ret;
988 }
989 
990 /*
991  * For every call I have to set kernel driver version.
992  * Because I can have commands supported only in other
993  * newer/later version. This routine is called for every
994  * ioctl command.
995  */
996 int
997 dm_check_version(prop_dictionary_t dm_dict)
998 {
999 	size_t i;
1000 	int dm_version[3];
1001 	prop_array_t ver;
1002 
1003 	ver = prop_dictionary_get(dm_dict, DM_IOCTL_VERSION);
1004 
1005 	for (i = 0; i < 3; i++)
1006 		prop_array_get_uint32(ver, i, &dm_version[i]);
1007 
1008 	if (DM_VERSION_MAJOR != dm_version[0] || DM_VERSION_MINOR < dm_version[1]) {
1009 		aprint_debug("libdevmapper/kernel version mismatch "
1010 		    "kernel: %d.%d.%d libdevmapper: %d.%d.%d\n",
1011 		    DM_VERSION_MAJOR, DM_VERSION_MINOR, DM_VERSION_PATCHLEVEL,
1012 		    dm_version[0], dm_version[1], dm_version[2]);
1013 
1014 		return EIO;
1015 	}
1016 	prop_array_set_uint32(ver, 0, DM_VERSION_MAJOR);
1017 	prop_array_set_uint32(ver, 1, DM_VERSION_MINOR);
1018 	prop_array_set_uint32(ver, 2, DM_VERSION_PATCHLEVEL);
1019 
1020 	prop_dictionary_set(dm_dict, DM_IOCTL_VERSION, ver);
1021 
1022 	return 0;
1023 }
1024