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