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