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