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