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