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