dm_ioctl.c revision 1.1.2.8 1 /* $NetBSD: dm_ioctl.c,v 1.1.2.8 2008/08/19 13:30:36 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
185 if ((dmv = dm_dev_alloc()) == NULL)
186 return ENOMEM;
187 /* printf("%d---%d\n",sizeof(dmv->uuid),DM_UUID_LEN);
188 printf("%s\n",uuid);
189 if (uuid)
190 memcpy(dmv->uuid,uuid,DM_UUID_LEN);*/
191 printf("%s\n",name);
192 if (name)
193 strlcpy(dmv->name, name, DM_NAME_LEN);
194
195 dmv->flags = flags;
196
197 dmv->minor = ++(dm_sc->sc_minor_num);
198
199 dmv->ref_cnt = 0;
200 dmv->event_nr = 0;
201 dmv->cur_active_table = 0;
202
203 dmv->dev_type = 0;
204 dmv->dm_dklabel = NULL;
205
206 /* Initialize tables. */
207 SLIST_INIT(&dmv->tables[0]);
208 SLIST_INIT(&dmv->tables[1]);
209
210 /* init device list of physical devices. */
211 SLIST_INIT(&dmv->pdevs);
212
213 prop_dictionary_set_uint64(dm_dict, DM_IOCTL_DEV, dmv->minor);
214
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 if (flags & DM_READONLY_FLAG)
241 dm_dev_free(dmv);
242 else {
243 r = dm_dev_insert(dmv);
244 DM_ADD_FLAG(flags, DM_EXISTS_FLAG);
245 }
246
247
248 return r;
249 }
250
251 /*
252 * Get list of created device-mapper devices fromglobal list and
253 * send it to kernel.
254 *
255 * Output dictionary:
256 *
257 * <key>cmd_data</key>
258 * <array>
259 * <dict>
260 * <key>name<key>
261 * <string>...</string>
262 *
263 * <key>dev</key>
264 * <integer>...</integer>
265 * </dict>
266 * </array>
267 *
268 *
269 * Locking: dev_mutex, dev_rwlock ??
270 */
271 int
272 dm_dev_list_ioctl(prop_dictionary_t dm_dict)
273 {
274 prop_array_t dev_list;
275
276 int r;
277 uint32_t flags;
278
279 r = 0;
280 flags = 0;
281
282 aprint_verbose("dm_dev_list called\n");
283
284 prop_dictionary_get_uint32(dm_dict, DM_IOCTL_FLAGS, &flags);
285
286 dm_dbg_print_flags(flags);
287
288 dev_list = dm_dev_prop_list();
289
290 prop_dictionary_set(dm_dict, DM_IOCTL_CMD_DATA, dev_list);
291
292 return 0;
293 }
294
295 /*
296 * Rename selected devices old name is in struct dm_ioctl.
297 * newname is taken from dictionary
298 *
299 * <key>cmd_data</key>
300 * <array>
301 * <string>...</string>
302 * </array>
303 *
304 * Locking: dev_mutex, dev_rwlock ??
305 */
306 int
307 dm_dev_rename_ioctl(prop_dictionary_t dm_dict)
308 {
309 prop_array_t cmd_array;
310 struct dm_dev *dmv;
311
312 const char *name,*uuid,*n_name;
313 uint32_t flags;
314
315 name = NULL;
316 uuid = NULL;
317
318 /* Get needed values from dictionary. */
319 prop_dictionary_get_cstring_nocopy(dm_dict, DM_IOCTL_NAME, &name);
320 prop_dictionary_get_cstring_nocopy(dm_dict, DM_IOCTL_UUID, &uuid);
321 prop_dictionary_get_uint32(dm_dict, DM_IOCTL_FLAGS, &flags);
322 cmd_array = prop_dictionary_get(dm_dict, DM_IOCTL_CMD_DATA);
323
324 prop_array_get_cstring_nocopy(cmd_array, 0, &n_name);
325
326 if (strlen(n_name) + 1 > DM_NAME_LEN)
327 return EINVAL;
328
329 if (((dmv = dm_dev_lookup_uuid(uuid)) == NULL) ||
330 (dmv = dm_dev_lookup_name(name)) == NULL) {
331 DM_REMOVE_FLAG(flags, DM_EXISTS_FLAG);
332 return ENOENT;
333 }
334
335 /* Test readonly flag change anything only if it is not set*/
336 if (!(flags & DM_READONLY_FLAG))
337 strlcpy(dmv->name, n_name, DM_NAME_LEN);
338
339 return 0;
340 }
341
342 /*
343 * Remove device from global list I have to remove active
344 * and inactive tables first and decrement reference_counters
345 * for used pdevs.
346 *
347 * Locking: dev_mutex, dev_rwlock
348 */
349 int
350 dm_dev_remove_ioctl(prop_dictionary_t dm_dict)
351 {
352 struct dm_dev *dmv;
353
354 const char *name, *uuid;
355 uint32_t flags;
356
357 flags = 0;
358 name = NULL;
359 uuid = NULL;
360
361 /* Get needed values from dictionary. */
362 prop_dictionary_get_cstring_nocopy(dm_dict, DM_IOCTL_NAME, &name);
363 prop_dictionary_get_cstring_nocopy(dm_dict, DM_IOCTL_UUID, &uuid);
364 prop_dictionary_get_uint32(dm_dict, DM_IOCTL_FLAGS, &flags);
365
366 dm_dbg_print_flags(flags);
367
368 if (((dmv = dm_dev_lookup_uuid(uuid)) == NULL) &&
369 (dmv = dm_dev_lookup_name(name)) == NULL) {
370 DM_REMOVE_FLAG(flags, DM_EXISTS_FLAG);
371 return ENOENT;
372 }
373
374 if (!(flags & DM_READONLY_FLAG)) {
375 /*
376 * Write lock rw lock firs and then remove all stuff.
377 */
378 rw_enter(&dmv->dev_rwlock, RW_WRITER);
379
380 /* Destroy active table first. */
381 dm_table_destroy(&dmv->tables[dmv->cur_active_table]);
382
383 /* Destroy unactive table if exits, too. */
384 if (!SLIST_EMPTY(&dmv->tables[1 - dmv->cur_active_table]))
385 dm_table_destroy(&dmv->tables[1 - dmv->cur_active_table]);
386
387 /* Decrement reference counters for all pdevs from this
388 device if they are unused close vnode and remove them
389 from global pdev list, too. */
390 dm_pdev_decr(&dmv->pdevs);
391
392 /* Test readonly flag change anything only if it is not set*/
393
394 dm_dev_rem(name); /* XXX. try to use uuid, too */
395
396 rw_exit(&dmv->dev_rwlock);
397 }
398 return 0;
399 }
400
401 /*
402 * Return actual state of device to libdevmapper.
403 *
404 * Locking: dev_mutex ??
405 */
406 int
407 dm_dev_status_ioctl(prop_dictionary_t dm_dict)
408 {
409 struct dm_table *tbl;
410 struct dm_dev *dmv;
411 const char *name, *uuid;
412 uint32_t flags;
413
414 name = NULL;
415 uuid = NULL;
416 flags = 0;
417
418 prop_dictionary_get_cstring_nocopy(dm_dict, DM_IOCTL_NAME, &name);
419 prop_dictionary_get_cstring_nocopy(dm_dict, DM_IOCTL_UUID, &uuid);
420 prop_dictionary_get_uint32(dm_dict, DM_IOCTL_FLAGS, &flags);
421
422 if (((dmv = dm_dev_lookup_name(name)) == NULL) &&
423 ((dmv = dm_dev_lookup_uuid(uuid)) == NULL)) {
424 DM_REMOVE_FLAG(flags, DM_EXISTS_FLAG);
425 return 0;
426 }
427
428 prop_dictionary_set_uint32(dm_dict, DM_IOCTL_OPEN, dmv->ref_cnt);
429 prop_dictionary_set_uint32(dm_dict, DM_IOCTL_FLAGS, dmv->flags);
430
431 prop_dictionary_set_uint64(dm_dict, DM_IOCTL_DEV, dmv->minor);
432
433 tbl = &dmv->tables[dmv->cur_active_table];
434
435 if (tbl != NULL)
436 DM_ADD_FLAG(flags, DM_ACTIVE_PRESENT_FLAG);
437 else
438 DM_REMOVE_FLAG(flags, DM_ACTIVE_PRESENT_FLAG);
439
440 return 0;
441 }
442
443 /*
444 * Set only flag to suggest that device is suspended. This call is
445 * not supported in NetBSD.
446 *
447 * Locking: null
448 */
449 int
450 dm_dev_suspend_ioctl(prop_dictionary_t dm_dict)
451 {
452 struct dm_dev *dmv;
453 const char *name, *uuid;
454 uint32_t flags;
455
456 name = NULL;
457 uuid = NULL;
458 flags = 0;
459
460 prop_dictionary_get_cstring_nocopy(dm_dict, DM_IOCTL_NAME, &name);
461 prop_dictionary_get_cstring_nocopy(dm_dict, DM_IOCTL_UUID, &uuid);
462 prop_dictionary_get_uint32(dm_dict, DM_IOCTL_FLAGS, &flags);
463
464 if (((dmv = dm_dev_lookup_name(name)) == NULL) &&
465 ((dmv = dm_dev_lookup_uuid(uuid)) == NULL)) {
466 DM_REMOVE_FLAG(flags, DM_EXISTS_FLAG);
467 return 0;
468 }
469
470 dmv->flags |= DM_SUSPEND_FLAG;
471
472 prop_dictionary_set_uint32(dm_dict, DM_IOCTL_OPEN, dmv->ref_cnt);
473 prop_dictionary_set_uint32(dm_dict, DM_IOCTL_FLAGS, dmv->flags);
474
475 prop_dictionary_set_uint64(dm_dict, DM_IOCTL_DEV, dmv->minor);
476
477 return 0;
478 }
479
480 /*
481 * Simulate Linux behaviour better and switch tables here and not in
482 * dm_table_load_ioctl.
483 *
484 * Locking: dev_mutex ??, dev_rwlock
485 */
486 int
487 dm_dev_resume_ioctl(prop_dictionary_t dm_dict)
488 {
489 struct dm_dev *dmv;
490 const char *name, *uuid;
491 uint32_t flags;
492
493 name = NULL;
494 uuid = NULL;
495 flags = 0;
496
497 prop_dictionary_get_cstring_nocopy(dm_dict, DM_IOCTL_NAME, &name);
498 prop_dictionary_get_cstring_nocopy(dm_dict, DM_IOCTL_UUID, &uuid);
499 prop_dictionary_get_uint32(dm_dict, DM_IOCTL_FLAGS, &flags);
500
501 if (((dmv = dm_dev_lookup_name(name)) == NULL) &&
502 ((dmv = dm_dev_lookup_uuid(uuid)) == NULL)) {
503 DM_REMOVE_FLAG(flags, DM_EXISTS_FLAG);
504 return 0;
505 }
506
507 rw_enter(&dmv->dev_rwlock, RW_WRITER);
508
509 dmv->flags &= ~DM_SUSPEND_FLAG;
510
511 prop_dictionary_set_uint32(dm_dict, DM_IOCTL_OPEN, dmv->ref_cnt);
512 prop_dictionary_set_uint32(dm_dict, DM_IOCTL_FLAGS, dmv->flags);
513
514 prop_dictionary_set_uint64(dm_dict, DM_IOCTL_DEV, dmv->minor);
515
516 dmv->cur_active_table = 1 - dmv->cur_active_table;
517
518 rw_exit(&dmv->dev_rwlock);
519
520 return 0;
521 }
522
523 /*
524 * Table management routines
525 * lvm2tools doens't send name/uuid to kernel with table
526 * for lookup I have to use minor number.
527 */
528
529
530 /*
531 * Remove inactive table from device. Routines which work's with inactive tables
532 * doesn't need to held write rw_lock. They can synchronise themselves with mutex?.
533 *
534 * Locking: dev_mutex
535 */
536 int
537 dm_table_clear_ioctl(prop_dictionary_t dm_dict)
538 {
539 struct dm_dev *dmv;
540 struct dm_table *tbl;
541
542 const char *name, *uuid;
543 int flags;
544
545 dmv = NULL;
546 name = NULL;
547 uuid = NULL;
548
549 prop_dictionary_get_cstring_nocopy(dm_dict, DM_IOCTL_NAME, &name);
550 prop_dictionary_get_cstring_nocopy(dm_dict, DM_IOCTL_UUID, &uuid);
551 prop_dictionary_get_uint32(dm_dict, DM_IOCTL_FLAGS, &flags);
552
553 aprint_verbose("Clearing inactive table from device: %s--%s\n",
554 name, uuid);
555
556 if (((dmv = dm_dev_lookup_name(name)) == NULL) &&
557 ((dmv = dm_dev_lookup_uuid(uuid)) == NULL)) {
558 DM_REMOVE_FLAG(flags, DM_EXISTS_FLAG);
559 return ENOENT;
560 }
561
562 /* Select unused table */
563 tbl = &dmv->tables[1-dmv->cur_active_table];
564
565 /* Test readonly flag change anything only if it is not set*/
566 if (!(flags & DM_READONLY_FLAG))
567 dm_table_destroy(tbl);
568
569 dmv->dev_type = 0;
570
571 return 0;
572 }
573
574 /*
575 * Get list of physical devices for active table.
576 * Get dev_t from pdev vnode and insert it into cmd_array.
577 *
578 * XXX. This function is called from lvm2tools to get information
579 * about physical devices, too e.g. during vgcreate.
580 *
581 * Locking: dev_mutex ??, dev_rwlock
582 */
583 int
584 dm_table_deps_ioctl(prop_dictionary_t dm_dict)
585 {
586 int error;
587 struct dm_dev *dmv;
588 struct dm_pdev *dmp;
589 struct vattr va;
590
591 prop_array_t cmd_array;
592
593 const char *name, *uuid;
594 int flags;
595 uint64_t dev;
596
597 size_t i;
598
599 name = NULL;
600 uuid = NULL;
601 dmv = NULL;
602 flags = 0;
603
604 i=0;
605
606 prop_dictionary_get_cstring_nocopy(dm_dict, DM_IOCTL_NAME, &name);
607 prop_dictionary_get_cstring_nocopy(dm_dict, DM_IOCTL_UUID, &uuid);
608 prop_dictionary_get_uint32(dm_dict, DM_IOCTL_FLAGS, &flags);
609 prop_dictionary_get_uint64(dm_dict, DM_IOCTL_DEV, &dev);
610
611 cmd_array = prop_dictionary_get(dm_dict,DM_IOCTL_CMD_DATA);
612
613 if (((dmv = dm_dev_lookup_name(name)) == NULL) &&
614 ((dmv = dm_dev_lookup_uuid(uuid)) == NULL) &&
615 ((dmv = dm_dev_lookup_minor(minor(dev))) == NULL)) {
616 DM_REMOVE_FLAG(flags, DM_EXISTS_FLAG);
617 return ENOENT;
618 }
619
620 aprint_verbose("Getting table deps for device: %s\n", dmv->name);
621
622 /* XXX DO I really need to write lock it here */
623 rw_enter(&dmv->dev_rwlock, RW_WRITER);
624
625 SLIST_FOREACH(dmp, &dmv->pdevs, next_pdev){
626
627 if ((error = VOP_GETATTR(dmp->pdev_vnode, &va, curlwp->l_cred))
628 != 0)
629 return (error);
630
631 prop_array_set_uint64(cmd_array, i, (uint64_t)va.va_rdev);
632
633 i++;
634 }
635
636 rw_exit(&dmv->dev_rwlock);
637
638 char *xml;
639 xml = prop_dictionary_externalize(dm_dict);
640 printf("%s\n",xml);
641
642 return 0;
643 }
644
645 /*
646 * Load new table/tables to device.
647 * Call apropriate target init routine open all physical pdev's and
648 * link them to device. For other targets mirror, strip, snapshot
649 * etc. also add dependency devices to upcalls list.
650 *
651 * Load table to inactive slot table are switched in dm_device_resume_ioctl.
652 * This simulates Linux behaviour better there should not be any difference.
653 *
654 * Locking: dev_mtx
655 */
656 int
657 dm_table_load_ioctl(prop_dictionary_t dm_dict)
658 {
659 struct dm_dev *dmv;
660 struct dm_pdev *dmp;
661 struct dm_table_entry *table_en, *last_table;
662 struct dm_table *tbl;
663 struct dm_target *target;
664
665 prop_object_iterator_t iter;
666 prop_array_t cmd_array;
667 prop_dictionary_t target_dict;
668
669 const char *name, *uuid, *type;
670
671 uint32_t flags, ret;
672
673 uint64_t dev;
674
675 char *str;
676
677 ret = 0;
678 flags = 0;
679 name = NULL;
680 uuid = NULL;
681 dmv = NULL;
682 dmp = NULL;
683 last_table = NULL;
684
685 char *xml;
686 xml = prop_dictionary_externalize(dm_dict);
687 printf("%s\n",xml);
688
689 prop_dictionary_get_cstring_nocopy(dm_dict, DM_IOCTL_NAME, &name);
690 prop_dictionary_get_cstring_nocopy(dm_dict, DM_IOCTL_UUID, &uuid);
691 prop_dictionary_get_uint32(dm_dict, DM_IOCTL_FLAGS, &flags);
692 prop_dictionary_get_uint64(dm_dict, DM_IOCTL_DEV, &dev);
693
694 cmd_array = prop_dictionary_get(dm_dict, DM_IOCTL_CMD_DATA);
695 iter = prop_array_iterator(cmd_array);
696
697 dm_dbg_print_flags(flags);
698
699 aprint_verbose("Loading table to device: %s--%s\n",name,uuid);
700
701 if (((dmv = dm_dev_lookup_name(name)) == NULL) &&
702 ((dmv = dm_dev_lookup_uuid(uuid)) == NULL) &&
703 ((dmv = dm_dev_lookup_minor(minor(dev))) == NULL)) {
704 DM_REMOVE_FLAG(flags, DM_EXISTS_FLAG);
705 return ENOENT;
706 }
707
708 /* Select unused table */
709 tbl = &dmv->tables[1-dmv->cur_active_table];
710
711 /*
712 * I have to check if this table slot is not used by another table list.
713 * if it is used I should free them.
714 */
715
716 dm_table_destroy(tbl);
717
718 while((target_dict = prop_object_iterator_next(iter)) != NULL){
719
720 prop_dictionary_get_cstring_nocopy(target_dict,
721 DM_TABLE_TYPE, &type);
722
723 /*
724 * If we want to deny table with 2 or more different
725 * target we should do it here
726 */
727 if ((target = dm_target_lookup_name(type)) == NULL)
728 return ENOENT;
729
730 if ((table_en=kmem_alloc(sizeof(struct dm_table_entry),
731 KM_NOSLEEP)) == NULL)
732 return ENOMEM;
733
734 prop_dictionary_get_uint64(target_dict, DM_TABLE_START,
735 &table_en->start);
736 prop_dictionary_get_uint64(target_dict, DM_TABLE_LENGTH,
737 &table_en->length);
738
739 table_en->target=target;
740 table_en->dm_dev=dmv;
741
742 /*
743 * There is a parameter string after dm_target_spec
744 * structure which points to /dev/wd0a 284 part of
745 * table. String str points to this text.
746 */
747
748 prop_dictionary_get_cstring(target_dict,
749 DM_TABLE_PARAMS, (char**)&str);
750
751 /* Test readonly flag change anything only if it is not set*/
752 if (!(flags & DM_READONLY_FLAG)){
753
754 if (SLIST_EMPTY(tbl))
755 /* insert this table to head */
756 SLIST_INSERT_HEAD(tbl, table_en, next);
757 else
758 SLIST_INSERT_AFTER(last_table, table_en, next);
759
760 /*
761 * Params string is different for every target,
762 * therfore I have to pass it to target init
763 * routine and parse parameters there.
764 */
765 if (target->init != NULL && strlen(str) != 0)
766 ret = target->init(dmv, &table_en->target_config,
767 str);
768
769 if (ret != 0)
770 return ret;
771
772 last_table = table_en;
773
774 } else {
775 kmem_free(table_en, sizeof(struct dm_table_entry));
776 dm_pdev_destroy(dmp);
777 }
778
779 prop_object_release(str);
780 }
781
782 prop_object_release(cmd_array);
783
784 DM_ADD_FLAG(flags, DM_ACTIVE_PRESENT_FLAG);
785
786 return 0;
787 }
788
789 /*
790 * Get description of all tables loaded to device from kernel
791 * and send it to libdevmapper.
792 *
793 * Output dictionary for every table:
794 *
795 * <key>cmd_data</key>
796 * <array>
797 * <dict>
798 * <key>type<key>
799 * <string>...</string>
800 *
801 * <key>start</key>
802 * <integer>...</integer>
803 *
804 * <key>length</key>
805 * <integer>...</integer>
806 *
807 * <key>params</key>
808 * <string>...</string>
809 * </dict>
810 * </array>
811 *
812 * Locking: dev_mtx
813 */
814 int
815 dm_table_status_ioctl(prop_dictionary_t dm_dict)
816 {
817 struct dm_dev *dmv;
818 struct dm_table *tbl;
819 struct dm_table_entry *table_en;
820
821 prop_array_t cmd_array;
822 prop_dictionary_t target_dict;
823
824 uint32_t rec_size;
825 uint64_t dev;
826
827 const char *name, *uuid;
828 char *params;
829 int flags,i;
830
831 dmv = NULL;
832 uuid = NULL;
833 name = NULL;
834 params = NULL;
835 flags = 0;
836 rec_size = 0;
837 i=0;
838
839 prop_dictionary_get_cstring_nocopy(dm_dict, DM_IOCTL_NAME, &name);
840 prop_dictionary_get_cstring_nocopy(dm_dict, DM_IOCTL_UUID, &uuid);
841 prop_dictionary_get_uint32(dm_dict, DM_IOCTL_FLAGS, &flags);
842 prop_dictionary_get_uint64(dm_dict, DM_IOCTL_DEV, &dev);
843
844 cmd_array = prop_dictionary_get(dm_dict, DM_IOCTL_CMD_DATA);
845
846 if (((dmv = dm_dev_lookup_name(name)) == NULL) &&
847 ((dmv = dm_dev_lookup_uuid(uuid)) == NULL) &&
848 ((dmv = dm_dev_lookup_minor(minor(dev)))) == NULL) {
849 DM_REMOVE_FLAG(flags, DM_EXISTS_FLAG);
850 return ENOENT;
851 }
852
853 /* I should use mutex here and not rwlock there can be IO operation
854 during this ioctl on device. */
855
856 /* mutex_enter(&dmv->dev_mtx); */
857
858 aprint_verbose("Status of device tables: %s--%d\n",
859 name, dmv->cur_active_table);
860
861 tbl = &dmv->tables[dmv->cur_active_table];
862
863 if (tbl != NULL)
864 DM_ADD_FLAG(flags, DM_ACTIVE_PRESENT_FLAG);
865 else {
866 DM_REMOVE_FLAG(flags, DM_ACTIVE_PRESENT_FLAG);
867
868 return 0;
869 }
870
871 SLIST_FOREACH(table_en,tbl,next)
872 {
873 target_dict = prop_dictionary_create();
874 aprint_verbose("%016" PRIu64 ", length %016" PRIu64
875 ", target %s\n", table_en->start, table_en->length,
876 table_en->target->name);
877
878 prop_dictionary_set_uint64(target_dict, DM_TABLE_START,
879 table_en->start);
880 prop_dictionary_set_uint64(target_dict, DM_TABLE_LENGTH,
881 table_en->length);
882
883 prop_dictionary_set_cstring(target_dict, DM_TABLE_TYPE,
884 table_en->target->name);
885
886 prop_dictionary_set_int32(target_dict, DM_TABLE_STAT,
887 dmv->cur_active_table);
888
889 if (flags |= DM_STATUS_TABLE_FLAG) {
890 params = table_en->target->status
891 (table_en->target_config);
892
893 if(params != NULL){
894 prop_dictionary_set_cstring(target_dict,
895 DM_TABLE_PARAMS, params);
896
897 kmem_free(params,strlen(params)+1);
898 }
899 }
900 prop_array_set(cmd_array, i, target_dict);
901
902 prop_object_release(target_dict);
903
904 i++;
905 }
906
907 /*mutex_exit(&dmv->dev_mtx);*/
908
909 return 0;
910 }
911
912
913 /*
914 * For every call I have to set kernel driver version.
915 * Because I can have commands supported only in other
916 * newer/later version. This routine is called for every
917 * ioctl command.
918 */
919 int
920 dm_check_version(prop_dictionary_t dm_dict)
921 {
922 size_t i;
923 int dm_version[3];
924 prop_array_t ver;
925 int r;
926
927 r = 0;
928
929 ver = prop_dictionary_get(dm_dict, DM_IOCTL_VERSION);
930
931 for(i=0; i < 3; i++)
932 prop_array_get_uint32(ver, i, &dm_version[i]);
933
934
935 if (DM_VERSION_MAJOR != dm_version[0] ||
936 DM_VERSION_MINOR < dm_version[1])
937 {
938 aprint_verbose("libdevmapper/kernel version mismatch "
939 "kernel: %d.%d.%d libdevmapper: %d.%d.%d\n",
940 DM_VERSION_MAJOR, DM_VERSION_MINOR, DM_VERSION_PATCHLEVEL,
941 dm_version[0], dm_version[1], dm_version[2]);
942
943 r = EIO;
944 goto out;
945 }
946
947 prop_array_set_uint32(ver, 0, DM_VERSION_MAJOR);
948 prop_array_set_uint32(ver, 1, DM_VERSION_MINOR);
949 prop_array_set_uint32(ver, 2, DM_VERSION_PATCHLEVEL);
950
951 out:
952 return r;
953 }
954