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