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