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