dm_ioctl.c revision 1.30.8.1 1 /* $NetBSD: dm_ioctl.c,v 1.30.8.1 2017/05/17 01:44:17 pgoyette 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
50 * counter. It will return active or inactive table depends
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>
83
84 #include <sys/device.h>
85 #include <sys/disk.h>
86 #include <sys/disklabel.h>
87 #include <sys/kmem.h>
88 #include <sys/malloc.h>
89 #include <sys/vnode.h>
90
91 #include <machine/int_fmtio.h>
92
93 #include "netbsd-dm.h"
94 #include "dm.h"
95
96 static uint32_t sc_minor_num;
97 extern const struct dkdriver dmdkdriver;
98 uint32_t dm_dev_counter;
99
100 /* Generic cf_data for device-mapper driver */
101 static struct cfdata dm_cfdata = {
102 .cf_name = "dm",
103 .cf_atname = "dm",
104 .cf_fstate = FSTATE_STAR,
105 .cf_unit = 0
106 };
107 #define DM_REMOVE_FLAG(flag, name) do { \
108 prop_dictionary_get_uint32(dm_dict,DM_IOCTL_FLAGS,&flag); \
109 flag &= ~name; \
110 prop_dictionary_set_uint32(dm_dict,DM_IOCTL_FLAGS,flag); \
111 } while (/*CONSTCOND*/0)
112
113 #define DM_ADD_FLAG(flag, name) do { \
114 prop_dictionary_get_uint32(dm_dict,DM_IOCTL_FLAGS,&flag); \
115 flag |= name; \
116 prop_dictionary_set_uint32(dm_dict,DM_IOCTL_FLAGS,flag); \
117 } while (/*CONSTCOND*/0)
118
119 static int dm_dbg_print_flags(int);
120
121 /*
122 * Print flags sent to the kernel from libevmapper.
123 */
124 static int
125 dm_dbg_print_flags(int flags)
126 {
127 aprint_debug("dbg_print --- %d\n", flags);
128
129 if (flags & DM_READONLY_FLAG)
130 aprint_debug("dbg_flags: DM_READONLY_FLAG set In/Out\n");
131
132 if (flags & DM_SUSPEND_FLAG)
133 aprint_debug("dbg_flags: DM_SUSPEND_FLAG set In/Out \n");
134
135 if (flags & DM_PERSISTENT_DEV_FLAG)
136 aprint_debug("db_flags: DM_PERSISTENT_DEV_FLAG set In\n");
137
138 if (flags & DM_STATUS_TABLE_FLAG)
139 aprint_debug("dbg_flags: DM_STATUS_TABLE_FLAG set In\n");
140
141 if (flags & DM_ACTIVE_PRESENT_FLAG)
142 aprint_debug("dbg_flags: DM_ACTIVE_PRESENT_FLAG set Out\n");
143
144 if (flags & DM_INACTIVE_PRESENT_FLAG)
145 aprint_debug("dbg_flags: DM_INACTIVE_PRESENT_FLAG set Out\n");
146
147 if (flags & DM_BUFFER_FULL_FLAG)
148 aprint_debug("dbg_flags: DM_BUFFER_FULL_FLAG set Out\n");
149
150 if (flags & DM_SKIP_BDGET_FLAG)
151 aprint_debug("dbg_flags: DM_SKIP_BDGET_FLAG set In\n");
152
153 if (flags & DM_SKIP_LOCKFS_FLAG)
154 aprint_debug("dbg_flags: DM_SKIP_LOCKFS_FLAG set In\n");
155
156 if (flags & DM_NOFLUSH_FLAG)
157 aprint_debug("dbg_flags: DM_NOFLUSH_FLAG set In\n");
158
159 return 0;
160 }
161 /*
162 * Get version ioctl call I do it as default therefore this
163 * function is unused now.
164 */
165 int
166 dm_get_version_ioctl(prop_dictionary_t dm_dict)
167 {
168
169 return 0;
170 }
171 /*
172 * Get list of all available targets from global
173 * target list and sent them back to libdevmapper.
174 */
175 int
176 dm_list_versions_ioctl(prop_dictionary_t dm_dict)
177 {
178 prop_array_t target_list;
179 uint32_t flags;
180
181 flags = 0;
182
183 prop_dictionary_get_uint32(dm_dict, DM_IOCTL_FLAGS, &flags);
184
185 dm_dbg_print_flags(flags);
186 target_list = dm_target_prop_list();
187
188 prop_dictionary_set(dm_dict, DM_IOCTL_CMD_DATA, target_list);
189 prop_object_release(target_list);
190
191 return 0;
192 }
193 /*
194 * Create in-kernel entry for device. Device attributes such as name, uuid are
195 * taken from proplib dictionary.
196 *
197 */
198 int
199 dm_dev_create_ioctl(prop_dictionary_t dm_dict)
200 {
201 dm_dev_t *dmv;
202 const char *name, *uuid;
203 int r;
204 uint32_t flags;
205 device_t devt;
206
207 r = 0;
208 flags = 0;
209 name = NULL;
210 uuid = NULL;
211
212 /* Get needed values from dictionary. */
213 prop_dictionary_get_cstring_nocopy(dm_dict, DM_IOCTL_NAME, &name);
214 prop_dictionary_get_cstring_nocopy(dm_dict, DM_IOCTL_UUID, &uuid);
215 prop_dictionary_get_uint32(dm_dict, DM_IOCTL_FLAGS, &flags);
216
217 dm_dbg_print_flags(flags);
218
219 /* Lookup name and uuid if device already exist quit. */
220 if ((dmv = dm_dev_lookup(name, uuid, -1)) != NULL) {
221 DM_ADD_FLAG(flags, DM_EXISTS_FLAG); /* Device already exists */
222 dm_dev_unbusy(dmv);
223 return EEXIST;
224 }
225 if ((devt = config_attach_pseudo(&dm_cfdata)) == NULL) {
226 aprint_error("Unable to attach pseudo device dm/%s\n", name);
227 return (ENOMEM);
228 }
229 if ((dmv = dm_dev_alloc()) == NULL) {
230 device_release(devt);
231 return ENOMEM;
232 }
233
234 if (uuid)
235 strncpy(dmv->uuid, uuid, DM_UUID_LEN);
236 else
237 dmv->uuid[0] = '\0';
238
239 if (name)
240 strlcpy(dmv->name, name, DM_NAME_LEN);
241
242 dmv->minor = (uint64_t)atomic_inc_32_nv(&sc_minor_num);
243 dmv->flags = 0; /* device flags are set when needed */
244 dmv->ref_cnt = 0;
245 dmv->event_nr = 0;
246 dmv->dev_type = 0;
247 dmv->devt = devt;
248
249 dm_table_head_init(&dmv->table_head);
250
251 mutex_init(&dmv->dev_mtx, MUTEX_DEFAULT, IPL_NONE);
252 mutex_init(&dmv->diskp_mtx, MUTEX_DEFAULT, IPL_NONE);
253 cv_init(&dmv->dev_cv, "dm_dev");
254
255 if (flags & DM_READONLY_FLAG)
256 dmv->flags |= DM_READONLY_FLAG;
257
258 prop_dictionary_set_uint32(dm_dict, DM_IOCTL_MINOR, dmv->minor);
259
260 disk_init(dmv->diskp, dmv->name, &dmdkdriver);
261 disk_attach(dmv->diskp);
262
263 dmv->diskp->dk_info = NULL;
264
265 if ((r = dm_dev_insert(dmv)) != 0)
266 dm_dev_free(dmv);
267
268 DM_ADD_FLAG(flags, DM_EXISTS_FLAG);
269 DM_REMOVE_FLAG(flags, DM_INACTIVE_PRESENT_FLAG);
270
271 /* Increment device counter After creating device */
272 atomic_inc_32(&dm_dev_counter);
273
274 device_release(devt);
275 return r;
276 }
277 /*
278 * Get list of created device-mapper devices fromglobal list and
279 * send it to kernel.
280 *
281 * Output dictionary:
282 *
283 * <key>cmd_data</key>
284 * <array>
285 * <dict>
286 * <key>name<key>
287 * <string>...</string>
288 *
289 * <key>dev</key>
290 * <integer>...</integer>
291 * </dict>
292 * </array>
293 *
294 */
295 int
296 dm_dev_list_ioctl(prop_dictionary_t dm_dict)
297 {
298 prop_array_t dev_list;
299
300 uint32_t flags;
301
302 flags = 0;
303
304 prop_dictionary_get_uint32(dm_dict, DM_IOCTL_FLAGS, &flags);
305
306 dm_dbg_print_flags(flags);
307
308 dev_list = dm_dev_prop_list();
309
310 prop_dictionary_set(dm_dict, DM_IOCTL_CMD_DATA, dev_list);
311 prop_object_release(dev_list);
312
313 return 0;
314 }
315 /*
316 * Rename selected devices old name is in struct dm_ioctl.
317 * newname is taken from dictionary
318 *
319 * <key>cmd_data</key>
320 * <array>
321 * <string>...</string>
322 * </array>
323 */
324 int
325 dm_dev_rename_ioctl(prop_dictionary_t dm_dict)
326 {
327 prop_array_t cmd_array;
328 dm_dev_t *dmv;
329
330 const char *name, *uuid, *n_name;
331 uint32_t flags, minor;
332
333 name = NULL;
334 uuid = NULL;
335 minor = 0;
336
337 /* Get needed values from dictionary. */
338 prop_dictionary_get_cstring_nocopy(dm_dict, DM_IOCTL_NAME, &name);
339 prop_dictionary_get_cstring_nocopy(dm_dict, DM_IOCTL_UUID, &uuid);
340 prop_dictionary_get_uint32(dm_dict, DM_IOCTL_FLAGS, &flags);
341 prop_dictionary_get_uint32(dm_dict, DM_IOCTL_MINOR, &minor);
342
343 dm_dbg_print_flags(flags);
344
345 cmd_array = prop_dictionary_get(dm_dict, DM_IOCTL_CMD_DATA);
346
347 prop_array_get_cstring_nocopy(cmd_array, 0, &n_name);
348
349 if (strlen(n_name) + 1 > DM_NAME_LEN)
350 return EINVAL;
351
352 if ((dmv = dm_dev_rem(name, uuid, minor)) == NULL) {
353 DM_REMOVE_FLAG(flags, DM_EXISTS_FLAG);
354 return ENOENT;
355 }
356 /* change device name */
357 /*
358 * XXX How to deal with this change, name only used in
359 * dm_dev_routines, should I add dm_dev_change_name which will run
360 * under the dm_dev_list mutex ?
361 */
362 strlcpy(dmv->name, n_name, DM_NAME_LEN);
363
364 prop_dictionary_set_uint32(dm_dict, DM_IOCTL_OPEN, dmv->table_head.io_cnt);
365 prop_dictionary_set_uint32(dm_dict, DM_IOCTL_MINOR, dmv->minor);
366 prop_dictionary_set_cstring(dm_dict, DM_IOCTL_UUID, dmv->uuid);
367
368 dm_dev_insert(dmv);
369
370 return 0;
371 }
372 /*
373 * Remove device from global list I have to remove active
374 * and inactive tables first.
375 */
376 int
377 dm_dev_remove_ioctl(prop_dictionary_t dm_dict)
378 {
379 dm_dev_t *dmv;
380 const char *name, *uuid;
381 uint32_t flags, minor;
382 device_t devt;
383
384 flags = 0;
385 name = NULL;
386 uuid = NULL;
387
388 /* Get needed values from dictionary. */
389 prop_dictionary_get_cstring_nocopy(dm_dict, DM_IOCTL_NAME, &name);
390 prop_dictionary_get_cstring_nocopy(dm_dict, DM_IOCTL_UUID, &uuid);
391 prop_dictionary_get_uint32(dm_dict, DM_IOCTL_FLAGS, &flags);
392 prop_dictionary_get_uint32(dm_dict, DM_IOCTL_MINOR, &minor);
393
394 dm_dbg_print_flags(flags);
395
396 /*
397 * This seems as hack to me, probably use routine dm_dev_get_devt to
398 * atomicaly get devt from device.
399 */
400 if ((dmv = dm_dev_lookup(name, uuid, minor)) == NULL) {
401 DM_REMOVE_FLAG(flags, DM_EXISTS_FLAG);
402 return ENOENT;
403 }
404 devt = dmv->devt;
405
406 dm_dev_unbusy(dmv);
407
408 /*
409 * This will call dm_detach routine which will actually removes
410 * device.
411 */
412 return config_detach(devt, DETACH_QUIET);
413 }
414 /*
415 * Return actual state of device to libdevmapper.
416 */
417 int
418 dm_dev_status_ioctl(prop_dictionary_t dm_dict)
419 {
420 dm_dev_t *dmv;
421 const char *name, *uuid;
422 uint32_t flags, j, minor;
423
424 name = NULL;
425 uuid = NULL;
426 flags = 0;
427 j = 0;
428
429 prop_dictionary_get_cstring_nocopy(dm_dict, DM_IOCTL_NAME, &name);
430 prop_dictionary_get_cstring_nocopy(dm_dict, DM_IOCTL_UUID, &uuid);
431 prop_dictionary_get_uint32(dm_dict, DM_IOCTL_FLAGS, &flags);
432 prop_dictionary_get_uint32(dm_dict, DM_IOCTL_MINOR, &minor);
433
434 if ((dmv = dm_dev_lookup(name, uuid, minor)) == NULL) {
435 DM_REMOVE_FLAG(flags, DM_EXISTS_FLAG);
436 return ENOENT;
437 }
438 dm_dbg_print_flags(dmv->flags);
439
440 prop_dictionary_set_uint32(dm_dict, DM_IOCTL_OPEN, dmv->table_head.io_cnt);
441 prop_dictionary_set_uint32(dm_dict, DM_IOCTL_MINOR, dmv->minor);
442 prop_dictionary_set_cstring(dm_dict, DM_IOCTL_UUID, dmv->uuid);
443
444 if (dmv->flags & DM_SUSPEND_FLAG)
445 DM_ADD_FLAG(flags, DM_SUSPEND_FLAG);
446
447 /*
448 * Add status flags for tables I have to check both active and
449 * inactive tables.
450 */
451 if ((j = dm_table_get_target_count(&dmv->table_head, DM_TABLE_ACTIVE))) {
452 DM_ADD_FLAG(flags, DM_ACTIVE_PRESENT_FLAG);
453 } else
454 DM_REMOVE_FLAG(flags, DM_ACTIVE_PRESENT_FLAG);
455
456 prop_dictionary_set_uint32(dm_dict, DM_IOCTL_TARGET_COUNT, j);
457
458 if (dm_table_get_target_count(&dmv->table_head, DM_TABLE_INACTIVE))
459 DM_ADD_FLAG(flags, DM_INACTIVE_PRESENT_FLAG);
460 else
461 DM_REMOVE_FLAG(flags, DM_INACTIVE_PRESENT_FLAG);
462
463 dm_dev_unbusy(dmv);
464
465 return 0;
466 }
467 /*
468 * Set only flag to suggest that device is suspended. This call is
469 * not supported in NetBSD.
470 *
471 */
472 int
473 dm_dev_suspend_ioctl(prop_dictionary_t dm_dict)
474 {
475 dm_dev_t *dmv;
476 const char *name, *uuid;
477 uint32_t flags, minor;
478
479 name = NULL;
480 uuid = NULL;
481 flags = 0;
482
483 prop_dictionary_get_cstring_nocopy(dm_dict, DM_IOCTL_NAME, &name);
484 prop_dictionary_get_cstring_nocopy(dm_dict, DM_IOCTL_UUID, &uuid);
485 prop_dictionary_get_uint32(dm_dict, DM_IOCTL_FLAGS, &flags);
486 prop_dictionary_get_uint32(dm_dict, DM_IOCTL_MINOR, &minor);
487
488 if ((dmv = dm_dev_lookup(name, uuid, minor)) == NULL) {
489 DM_REMOVE_FLAG(flags, DM_EXISTS_FLAG);
490 return ENOENT;
491 }
492 atomic_or_32(&dmv->flags, DM_SUSPEND_FLAG);
493
494 dm_dbg_print_flags(dmv->flags);
495
496 prop_dictionary_set_uint32(dm_dict, DM_IOCTL_OPEN, dmv->table_head.io_cnt);
497 prop_dictionary_set_uint32(dm_dict, DM_IOCTL_FLAGS, dmv->flags);
498 prop_dictionary_set_uint32(dm_dict, DM_IOCTL_MINOR, dmv->minor);
499
500 dm_dev_unbusy(dmv);
501
502 /* Add flags to dictionary flag after dmv -> dict copy */
503 DM_ADD_FLAG(flags, DM_EXISTS_FLAG);
504
505 return 0;
506 }
507 /*
508 * Simulate Linux behaviour better and switch tables here and not in
509 * dm_table_load_ioctl.
510 */
511 int
512 dm_dev_resume_ioctl(prop_dictionary_t dm_dict)
513 {
514 dm_dev_t *dmv;
515 const char *name, *uuid;
516 uint32_t flags, minor;
517
518 name = NULL;
519 uuid = NULL;
520 flags = 0;
521
522 /*
523 * char *xml; xml = prop_dictionary_externalize(dm_dict);
524 * printf("%s\n",xml);
525 */
526
527 prop_dictionary_get_cstring_nocopy(dm_dict, DM_IOCTL_NAME, &name);
528 prop_dictionary_get_cstring_nocopy(dm_dict, DM_IOCTL_UUID, &uuid);
529 prop_dictionary_get_uint32(dm_dict, DM_IOCTL_FLAGS, &flags);
530 prop_dictionary_get_uint32(dm_dict, DM_IOCTL_MINOR, &minor);
531
532 /* Remove device from global device list */
533 if ((dmv = dm_dev_lookup(name, uuid, minor)) == NULL) {
534 DM_REMOVE_FLAG(flags, DM_EXISTS_FLAG);
535 return ENOENT;
536 }
537 atomic_and_32(&dmv->flags, ~(DM_SUSPEND_FLAG | DM_INACTIVE_PRESENT_FLAG));
538 atomic_or_32(&dmv->flags, DM_ACTIVE_PRESENT_FLAG);
539
540 dm_table_switch_tables(&dmv->table_head);
541
542 DM_ADD_FLAG(flags, DM_EXISTS_FLAG);
543
544 dmgetproperties(dmv->diskp, &dmv->table_head);
545
546 prop_dictionary_set_uint32(dm_dict, DM_IOCTL_OPEN, dmv->table_head.io_cnt);
547 prop_dictionary_set_uint32(dm_dict, DM_IOCTL_FLAGS, flags);
548 prop_dictionary_set_uint32(dm_dict, DM_IOCTL_MINOR, dmv->minor);
549
550 dm_dev_unbusy(dmv);
551
552 /* Destroy inactive table after resume. */
553 dm_table_destroy(&dmv->table_head, DM_TABLE_INACTIVE);
554
555 return 0;
556 }
557 /*
558 * Table management routines
559 * lvm2tools doens't send name/uuid to kernel with table
560 * for lookup I have to use minor number.
561 */
562
563 /*
564 * Remove inactive table from device. Routines which work's with inactive tables
565 * doesn't need to synchronise with dmstrategy. They can synchronise themselves with mutex?.
566 *
567 */
568 int
569 dm_table_clear_ioctl(prop_dictionary_t dm_dict)
570 {
571 dm_dev_t *dmv;
572 const char *name, *uuid;
573 uint32_t flags, minor;
574
575 dmv = NULL;
576 name = NULL;
577 uuid = NULL;
578 flags = 0;
579 minor = 0;
580
581 prop_dictionary_get_cstring_nocopy(dm_dict, DM_IOCTL_NAME, &name);
582 prop_dictionary_get_cstring_nocopy(dm_dict, DM_IOCTL_UUID, &uuid);
583 prop_dictionary_get_uint32(dm_dict, DM_IOCTL_FLAGS, &flags);
584 prop_dictionary_get_uint32(dm_dict, DM_IOCTL_MINOR, &minor);
585
586 aprint_debug("Clearing inactive table from device: %s--%s\n",
587 name, uuid);
588
589 if ((dmv = dm_dev_lookup(name, uuid, minor)) == NULL) {
590 DM_REMOVE_FLAG(flags, DM_EXISTS_FLAG);
591 return ENOENT;
592 }
593 /* Select unused table */
594 dm_table_destroy(&dmv->table_head, DM_TABLE_INACTIVE);
595
596 atomic_and_32(&dmv->flags, ~DM_INACTIVE_PRESENT_FLAG);
597
598 dm_dev_unbusy(dmv);
599
600 return 0;
601 }
602 /*
603 * Get list of physical devices for active table.
604 * Get dev_t from pdev vnode and insert it into cmd_array.
605 *
606 * XXX. This function is called from lvm2tools to get information
607 * about physical devices, too e.g. during vgcreate.
608 */
609 int
610 dm_table_deps_ioctl(prop_dictionary_t dm_dict)
611 {
612 dm_dev_t *dmv;
613 dm_table_t *tbl;
614 dm_table_entry_t *table_en;
615
616 prop_array_t cmd_array;
617 const char *name, *uuid;
618 uint32_t flags, minor;
619
620 int table_type;
621
622 name = NULL;
623 uuid = NULL;
624 dmv = NULL;
625 flags = 0;
626
627 prop_dictionary_get_cstring_nocopy(dm_dict, DM_IOCTL_NAME, &name);
628 prop_dictionary_get_cstring_nocopy(dm_dict, DM_IOCTL_UUID, &uuid);
629 prop_dictionary_get_uint32(dm_dict, DM_IOCTL_FLAGS, &flags);
630 prop_dictionary_get_uint32(dm_dict, DM_IOCTL_MINOR, &minor);
631
632 /* create array for dev_t's */
633 cmd_array = prop_array_create();
634
635 if ((dmv = dm_dev_lookup(name, uuid, minor)) == NULL) {
636 DM_REMOVE_FLAG(flags, DM_EXISTS_FLAG);
637 return ENOENT;
638 }
639 prop_dictionary_set_uint32(dm_dict, DM_IOCTL_MINOR, dmv->minor);
640 prop_dictionary_set_cstring(dm_dict, DM_IOCTL_NAME, dmv->name);
641 prop_dictionary_set_cstring(dm_dict, DM_IOCTL_UUID, dmv->uuid);
642
643 aprint_debug("Getting table deps for device: %s\n", dmv->name);
644
645 /*
646 * if DM_QUERY_INACTIVE_TABLE_FLAG is passed we need to query
647 * INACTIVE TABLE
648 */
649 if (flags & DM_QUERY_INACTIVE_TABLE_FLAG)
650 table_type = DM_TABLE_INACTIVE;
651 else
652 table_type = DM_TABLE_ACTIVE;
653
654 tbl = dm_table_get_entry(&dmv->table_head, table_type);
655
656 SLIST_FOREACH(table_en, tbl, next)
657 table_en->target->deps(table_en, cmd_array);
658
659 dm_table_release(&dmv->table_head, table_type);
660 dm_dev_unbusy(dmv);
661
662 prop_dictionary_set(dm_dict, DM_IOCTL_CMD_DATA, cmd_array);
663 prop_object_release(cmd_array);
664
665 return 0;
666 }
667 /*
668 * Load new table/tables to device.
669 * Call apropriate target init routine open all physical pdev's and
670 * link them to device. For other targets mirror, strip, snapshot
671 * etc. also add dependency devices to upcalls list.
672 *
673 * Load table to inactive slot table are switched in dm_device_resume_ioctl.
674 * This simulates Linux behaviour better there should not be any difference.
675 *
676 */
677 int
678 dm_table_load_ioctl(prop_dictionary_t dm_dict)
679 {
680 dm_dev_t *dmv;
681 dm_table_entry_t *table_en, *last_table;
682 dm_table_t *tbl;
683 dm_target_t *target;
684
685 prop_object_iterator_t iter;
686 prop_array_t cmd_array;
687 prop_dictionary_t target_dict;
688
689 const char *name, *uuid, *type;
690
691 uint32_t flags, ret, minor;
692
693 char *str;
694
695 ret = 0;
696 flags = 0;
697 name = NULL;
698 uuid = NULL;
699 dmv = NULL;
700 last_table = NULL;
701 str = NULL;
702
703 /*
704 * char *xml; xml = prop_dictionary_externalize(dm_dict);
705 * printf("%s\n",xml);
706 */
707
708 prop_dictionary_get_cstring_nocopy(dm_dict, DM_IOCTL_NAME, &name);
709 prop_dictionary_get_cstring_nocopy(dm_dict, DM_IOCTL_UUID, &uuid);
710 prop_dictionary_get_uint32(dm_dict, DM_IOCTL_FLAGS, &flags);
711 prop_dictionary_get_uint32(dm_dict, DM_IOCTL_MINOR, &minor);
712
713 cmd_array = prop_dictionary_get(dm_dict, DM_IOCTL_CMD_DATA);
714 iter = prop_array_iterator(cmd_array);
715 dm_dbg_print_flags(flags);
716
717 if ((dmv = dm_dev_lookup(name, uuid, minor)) == NULL) {
718 DM_REMOVE_FLAG(flags, DM_EXISTS_FLAG);
719 prop_object_iterator_release(iter);
720 return ENOENT;
721 }
722 aprint_debug("Loading table to device: %s--%d\n", name,
723 dmv->table_head.cur_active_table);
724
725 /*
726 * I have to check if this table slot is not used by another table list.
727 * if it is used I should free them.
728 */
729 if (dmv->flags & DM_INACTIVE_PRESENT_FLAG)
730 dm_table_destroy(&dmv->table_head, DM_TABLE_INACTIVE);
731
732 dm_dbg_print_flags(dmv->flags);
733 tbl = dm_table_get_entry(&dmv->table_head, DM_TABLE_INACTIVE);
734
735 aprint_debug("dmv->name = %s\n", dmv->name);
736
737 prop_dictionary_set_uint32(dm_dict, DM_IOCTL_MINOR, dmv->minor);
738
739 while ((target_dict = prop_object_iterator_next(iter)) != NULL) {
740
741 prop_dictionary_get_cstring_nocopy(target_dict,
742 DM_TABLE_TYPE, &type);
743 /*
744 * If we want to deny table with 2 or more different
745 * target we should do it here
746 */
747 if (((target = dm_target_lookup(type)) == NULL) &&
748 ((target = dm_target_autoload(type)) == NULL)) {
749 dm_table_release(&dmv->table_head, DM_TABLE_INACTIVE);
750 dm_dev_unbusy(dmv);
751 prop_object_iterator_release(iter);
752 return ENOENT;
753 }
754 if ((table_en = kmem_alloc(sizeof(dm_table_entry_t),
755 KM_SLEEP)) == NULL) {
756 dm_table_release(&dmv->table_head, DM_TABLE_INACTIVE);
757 dm_dev_unbusy(dmv);
758 prop_object_iterator_release(iter);
759 return ENOMEM;
760 }
761 prop_dictionary_get_uint64(target_dict, DM_TABLE_START,
762 &table_en->start);
763 prop_dictionary_get_uint64(target_dict, DM_TABLE_LENGTH,
764 &table_en->length);
765
766 table_en->target = target;
767 table_en->dm_dev = dmv;
768 table_en->target_config = NULL;
769
770 /*
771 * There is a parameter string after dm_target_spec
772 * structure which points to /dev/wd0a 284 part of
773 * table. String str points to this text. This can be
774 * null and therefore it should be checked before we try to
775 * use it.
776 */
777 prop_dictionary_get_cstring(target_dict,
778 DM_TABLE_PARAMS, (char **) &str);
779
780 if (SLIST_EMPTY(tbl) || last_table == NULL)
781 /* insert this table to head */
782 SLIST_INSERT_HEAD(tbl, table_en, next);
783 else
784 SLIST_INSERT_AFTER(last_table, table_en, next);
785
786 /*
787 * Params string is different for every target,
788 * therfore I have to pass it to target init
789 * routine and parse parameters there.
790 */
791
792 if ((ret = target->init(dmv, &table_en->target_config,
793 str)) != 0) {
794
795 dm_table_release(&dmv->table_head, DM_TABLE_INACTIVE);
796 dm_table_destroy(&dmv->table_head, DM_TABLE_INACTIVE);
797 free(str, M_TEMP);
798
799 dm_dev_unbusy(dmv);
800 dm_target_unbusy(target);
801 prop_object_iterator_release(iter);
802 return ret;
803 }
804 last_table = table_en;
805 free(str, M_TEMP);
806 }
807 prop_object_iterator_release(iter);
808
809 DM_ADD_FLAG(flags, DM_INACTIVE_PRESENT_FLAG);
810 atomic_or_32(&dmv->flags, DM_INACTIVE_PRESENT_FLAG);
811
812 dm_table_release(&dmv->table_head, DM_TABLE_INACTIVE);
813 dm_dev_unbusy(dmv);
814 return 0;
815 }
816 /*
817 * Get description of all tables loaded to device from kernel
818 * and send it to libdevmapper.
819 *
820 * Output dictionary for every table:
821 *
822 * <key>cmd_data</key>
823 * <array>
824 * <dict>
825 * <key>type<key>
826 * <string>...</string>
827 *
828 * <key>start</key>
829 * <integer>...</integer>
830 *
831 * <key>length</key>
832 * <integer>...</integer>
833 *
834 * <key>params</key>
835 * <string>...</string>
836 * </dict>
837 * </array>
838 *
839 */
840 int
841 dm_table_status_ioctl(prop_dictionary_t dm_dict)
842 {
843 dm_dev_t *dmv;
844 dm_table_t *tbl;
845 dm_table_entry_t *table_en;
846
847 prop_array_t cmd_array;
848 prop_dictionary_t target_dict;
849
850 uint32_t minor, flags;
851
852 const char *name, *uuid;
853 char *params;
854 int table_type;
855
856 dmv = NULL;
857 uuid = NULL;
858 name = NULL;
859 params = NULL;
860 flags = 0;
861
862 prop_dictionary_get_cstring_nocopy(dm_dict, DM_IOCTL_NAME, &name);
863 prop_dictionary_get_cstring_nocopy(dm_dict, DM_IOCTL_UUID, &uuid);
864 prop_dictionary_get_uint32(dm_dict, DM_IOCTL_FLAGS, &flags);
865 prop_dictionary_get_uint32(dm_dict, DM_IOCTL_MINOR, &minor);
866
867 cmd_array = prop_array_create();
868
869 if ((dmv = dm_dev_lookup(name, uuid, minor)) == NULL) {
870 DM_REMOVE_FLAG(flags, DM_EXISTS_FLAG);
871 return ENOENT;
872 }
873 /*
874 * if DM_QUERY_INACTIVE_TABLE_FLAG is passed we need to query
875 * INACTIVE TABLE
876 */
877 if (flags & DM_QUERY_INACTIVE_TABLE_FLAG)
878 table_type = DM_TABLE_INACTIVE;
879 else
880 table_type = DM_TABLE_ACTIVE;
881
882 if (dm_table_get_target_count(&dmv->table_head, DM_TABLE_ACTIVE))
883 DM_ADD_FLAG(flags, DM_ACTIVE_PRESENT_FLAG);
884 else {
885 DM_REMOVE_FLAG(flags, DM_ACTIVE_PRESENT_FLAG);
886
887 if (dm_table_get_target_count(&dmv->table_head, DM_TABLE_INACTIVE))
888 DM_ADD_FLAG(flags, DM_INACTIVE_PRESENT_FLAG);
889 else {
890 DM_REMOVE_FLAG(flags, DM_INACTIVE_PRESENT_FLAG);
891 }
892 }
893
894 if (dmv->flags & DM_SUSPEND_FLAG)
895 DM_ADD_FLAG(flags, DM_SUSPEND_FLAG);
896
897 prop_dictionary_set_uint32(dm_dict, DM_IOCTL_MINOR, dmv->minor);
898
899 aprint_debug("Status of device tables: %s--%d\n",
900 name, dmv->table_head.cur_active_table);
901
902 tbl = dm_table_get_entry(&dmv->table_head, table_type);
903
904 SLIST_FOREACH(table_en, tbl, next) {
905 target_dict = prop_dictionary_create();
906 aprint_debug("%016" PRIu64 ", length %016" PRIu64
907 ", target %s\n", table_en->start, table_en->length,
908 table_en->target->name);
909
910 prop_dictionary_set_uint64(target_dict, DM_TABLE_START,
911 table_en->start);
912 prop_dictionary_set_uint64(target_dict, DM_TABLE_LENGTH,
913 table_en->length);
914
915 prop_dictionary_set_cstring(target_dict, DM_TABLE_TYPE,
916 table_en->target->name);
917
918 /* dm_table_get_cur_actv.table ?? */
919 prop_dictionary_set_int32(target_dict, DM_TABLE_STAT,
920 dmv->table_head.cur_active_table);
921
922 if (flags & DM_STATUS_TABLE_FLAG) {
923 params = table_en->target->status
924 (table_en->target_config);
925
926 if (params != NULL) {
927 prop_dictionary_set_cstring(target_dict,
928 DM_TABLE_PARAMS, params);
929
930 kmem_free(params, DM_MAX_PARAMS_SIZE);
931 }
932 }
933 prop_array_add(cmd_array, target_dict);
934 prop_object_release(target_dict);
935 }
936
937 dm_table_release(&dmv->table_head, table_type);
938 dm_dev_unbusy(dmv);
939
940 prop_dictionary_set_uint32(dm_dict, DM_IOCTL_FLAGS, flags);
941 prop_dictionary_set(dm_dict, DM_IOCTL_CMD_DATA, cmd_array);
942 prop_object_release(cmd_array);
943
944 return 0;
945 }
946
947
948 /*
949 * For every call I have to set kernel driver version.
950 * Because I can have commands supported only in other
951 * newer/later version. This routine is called for every
952 * ioctl command.
953 */
954 int
955 dm_check_version(prop_dictionary_t dm_dict)
956 {
957 size_t i;
958 uint32_t dm_version[3];
959 prop_array_t ver;
960
961 ver = prop_dictionary_get(dm_dict, DM_IOCTL_VERSION);
962
963 for (i = 0; i < 3; i++)
964 prop_array_get_uint32(ver, i, &dm_version[i]);
965
966 if (DM_VERSION_MAJOR != dm_version[0] || DM_VERSION_MINOR < dm_version[1]) {
967 aprint_debug("libdevmapper/kernel version mismatch "
968 "kernel: %d.%d.%d libdevmapper: %d.%d.%d\n",
969 DM_VERSION_MAJOR, DM_VERSION_MINOR, DM_VERSION_PATCHLEVEL,
970 dm_version[0], dm_version[1], dm_version[2]);
971
972 return EIO;
973 }
974 prop_array_set_uint32(ver, 0, DM_VERSION_MAJOR);
975 prop_array_set_uint32(ver, 1, DM_VERSION_MINOR);
976 prop_array_set_uint32(ver, 2, DM_VERSION_PATCHLEVEL);
977
978 prop_dictionary_set(dm_dict, DM_IOCTL_VERSION, ver);
979
980 return 0;
981 }
982