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