dm_ioctl.c revision 1.1.2.16 1 /* $NetBSD: dm_ioctl.c,v 1.1.2.16 2008/09/11 13:40:48 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 cmd_array = prop_dictionary_get(dm_dict,DM_IOCTL_CMD_DATA);
613
614 if ((dmv = dm_dev_lookup(name, uuid, minor)) == NULL){
615 DM_REMOVE_FLAG(flags, DM_EXISTS_FLAG);
616 return ENOENT;
617 }
618
619 prop_dictionary_set_uint32(dm_dict, DM_IOCTL_MINOR, dmv->minor);
620 prop_dictionary_set_cstring(dm_dict, DM_IOCTL_NAME, dmv->name);
621 prop_dictionary_set_cstring(dm_dict, DM_IOCTL_UUID, dmv->uuid);
622
623 aprint_verbose("Getting table deps for device: %s\n", dmv->name);
624
625 /* XXX DO I really need to write lock it here */
626 rw_enter(&dmv->dev_rwlock, RW_WRITER);
627
628 SLIST_FOREACH(table_en, &dmv->tables[dmv->cur_active_table], next)
629 table_en->target->deps(table_en, cmd_array);
630
631 rw_exit(&dmv->dev_rwlock);
632
633 return 0;
634 }
635
636 /*
637 * Load new table/tables to device.
638 * Call apropriate target init routine open all physical pdev's and
639 * link them to device. For other targets mirror, strip, snapshot
640 * etc. also add dependency devices to upcalls list.
641 *
642 * Load table to inactive slot table are switched in dm_device_resume_ioctl.
643 * This simulates Linux behaviour better there should not be any difference.
644 *
645 */
646 int
647 dm_table_load_ioctl(prop_dictionary_t dm_dict)
648 {
649 struct dm_dev *dmv;
650 struct dm_table_entry *table_en, *last_table;
651 struct dm_table *tbl;
652 struct dm_target *target;
653
654 prop_object_iterator_t iter;
655 prop_array_t cmd_array;
656 prop_dictionary_t target_dict;
657
658 const char *name, *uuid, *type;
659
660 uint32_t flags, ret, minor;
661
662 char *str;
663
664 ret = 0;
665 flags = 0;
666 name = NULL;
667 uuid = NULL;
668 dmv = NULL;
669 last_table = NULL;
670
671 /* char *xml;
672 xml = prop_dictionary_externalize(dm_dict);
673 printf("%s\n",xml);*/
674
675 prop_dictionary_get_cstring_nocopy(dm_dict, DM_IOCTL_NAME, &name);
676 prop_dictionary_get_cstring_nocopy(dm_dict, DM_IOCTL_UUID, &uuid);
677 prop_dictionary_get_uint32(dm_dict, DM_IOCTL_FLAGS, &flags);
678 prop_dictionary_get_uint32(dm_dict, DM_IOCTL_MINOR, &minor);
679
680 cmd_array = prop_dictionary_get(dm_dict, DM_IOCTL_CMD_DATA);
681 iter = prop_array_iterator(cmd_array);
682
683 dm_dbg_print_flags(flags);
684
685 aprint_normal("Loading table to device: %s--%s\n",name,uuid);
686
687 if ((dmv = dm_dev_lookup(name, uuid, minor)) == NULL){
688 DM_REMOVE_FLAG(flags, DM_EXISTS_FLAG);
689 return ENOENT;
690 }
691
692 printf("dmv->name = %s\n",dmv->name);
693
694 prop_dictionary_set_uint32(dm_dict, DM_IOCTL_MINOR, dmv->minor);
695
696 /* Select unused table */
697 tbl = &dmv->tables[1-dmv->cur_active_table];
698
699 /*
700 * I have to check if this table slot is not used by another table list.
701 * if it is used I should free them.
702 */
703 if (dmv->flags & DM_INACTIVE_PRESENT_FLAG)
704 dm_table_destroy(tbl);
705
706 while((target_dict = prop_object_iterator_next(iter)) != NULL){
707
708 prop_dictionary_get_cstring_nocopy(target_dict,
709 DM_TABLE_TYPE, &type);
710
711 /*
712 * If we want to deny table with 2 or more different
713 * target we should do it here
714 */
715 if ((target = dm_target_lookup_name(type)) == NULL)
716 return ENOENT;
717
718 if ((table_en=kmem_alloc(sizeof(struct dm_table_entry),
719 KM_NOSLEEP)) == NULL)
720 return ENOMEM;
721
722 prop_dictionary_get_uint64(target_dict, DM_TABLE_START,
723 &table_en->start);
724 prop_dictionary_get_uint64(target_dict, DM_TABLE_LENGTH,
725 &table_en->length);
726
727 table_en->target = target;
728 table_en->dm_dev = dmv;
729 table_en->target_config = NULL;
730
731 /*
732 * There is a parameter string after dm_target_spec
733 * structure which points to /dev/wd0a 284 part of
734 * table. String str points to this text.
735 */
736
737 prop_dictionary_get_cstring(target_dict,
738 DM_TABLE_PARAMS, (char**)&str);
739
740
741 if (SLIST_EMPTY(tbl))
742 /* insert this table to head */
743 SLIST_INSERT_HEAD(tbl, table_en, next);
744 else
745 SLIST_INSERT_AFTER(last_table, table_en, next);
746
747 /*
748 * Params string is different for every target,
749 * therfore I have to pass it to target init
750 * routine and parse parameters there.
751 */
752 if (strlen(str) != 0) {
753 if ((ret = target->init(dmv, &table_en->target_config, str)) != 0) {
754 dm_table_destroy(tbl);
755
756 return ret;
757 }
758 }
759
760 last_table = table_en;
761
762 prop_object_release(str);
763 }
764
765 prop_object_release(cmd_array);
766
767 DM_ADD_FLAG(flags, DM_INACTIVE_PRESENT_FLAG);
768 dmv->flags |= DM_INACTIVE_PRESENT_FLAG;
769
770 return 0;
771 }
772
773 /*
774 * Get description of all tables loaded to device from kernel
775 * and send it to libdevmapper.
776 *
777 * Output dictionary for every table:
778 *
779 * <key>cmd_data</key>
780 * <array>
781 * <dict>
782 * <key>type<key>
783 * <string>...</string>
784 *
785 * <key>start</key>
786 * <integer>...</integer>
787 *
788 * <key>length</key>
789 * <integer>...</integer>
790 *
791 * <key>params</key>
792 * <string>...</string>
793 * </dict>
794 * </array>
795 *
796 */
797 int
798 dm_table_status_ioctl(prop_dictionary_t dm_dict)
799 {
800 struct dm_dev *dmv;
801 struct dm_table *tbl;
802 struct dm_table_entry *table_en;
803
804 prop_array_t cmd_array;
805 prop_dictionary_t target_dict;
806
807 uint32_t rec_size, minor;
808
809 const char *name, *uuid;
810 char *params;
811 int flags,i;
812
813 dmv = NULL;
814 uuid = NULL;
815 name = NULL;
816 params = NULL;
817 flags = 0;
818 rec_size = 0;
819 i = 0;
820
821 prop_dictionary_get_cstring_nocopy(dm_dict, DM_IOCTL_NAME, &name);
822 prop_dictionary_get_cstring_nocopy(dm_dict, DM_IOCTL_UUID, &uuid);
823 prop_dictionary_get_uint32(dm_dict, DM_IOCTL_FLAGS, &flags);
824 prop_dictionary_get_uint32(dm_dict, DM_IOCTL_MINOR, &minor);
825
826 cmd_array = prop_dictionary_get(dm_dict, DM_IOCTL_CMD_DATA);
827
828 if ((dmv = dm_dev_lookup(name, uuid, minor)) == NULL){
829 DM_REMOVE_FLAG(flags, DM_EXISTS_FLAG);
830 return ENOENT;
831 }
832
833 prop_dictionary_set_uint32(dm_dict, DM_IOCTL_MINOR, dmv->minor);
834
835 /* I should use mutex here and not rwlock there can be IO operation
836 during this ioctl on device. */
837
838 aprint_normal("Status of device tables: %s--%d\n",
839 name, dmv->cur_active_table);
840
841 if (dmv->flags | DM_SUSPEND_FLAG)
842 DM_ADD_FLAG(flags, DM_SUSPEND_FLAG);
843
844 tbl = &dmv->tables[dmv->cur_active_table];
845
846 if (!SLIST_EMPTY(tbl))
847 DM_ADD_FLAG(flags, DM_ACTIVE_PRESENT_FLAG);
848 else {
849 DM_REMOVE_FLAG(flags, DM_ACTIVE_PRESENT_FLAG);
850
851 tbl = &dmv->tables[1 - dmv->cur_active_table];
852
853 if (!SLIST_EMPTY(tbl))
854 DM_ADD_FLAG(flags, DM_INACTIVE_PRESENT_FLAG);
855 else {
856 DM_REMOVE_FLAG(flags, DM_INACTIVE_PRESENT_FLAG);
857 }
858 }
859
860 SLIST_FOREACH(table_en,tbl,next)
861 {
862 target_dict = prop_dictionary_create();
863 aprint_verbose("%016" PRIu64 ", length %016" PRIu64
864 ", target %s\n", table_en->start, table_en->length,
865 table_en->target->name);
866
867 prop_dictionary_set_uint64(target_dict, DM_TABLE_START,
868 table_en->start);
869 prop_dictionary_set_uint64(target_dict, DM_TABLE_LENGTH,
870 table_en->length);
871
872 prop_dictionary_set_cstring(target_dict, DM_TABLE_TYPE,
873 table_en->target->name);
874
875 prop_dictionary_set_int32(target_dict, DM_TABLE_STAT,
876 dmv->cur_active_table);
877
878 if (flags |= DM_STATUS_TABLE_FLAG) {
879 params = table_en->target->status
880 (table_en->target_config);
881
882 if(params != NULL){
883 prop_dictionary_set_cstring(target_dict,
884 DM_TABLE_PARAMS, params);
885
886 kmem_free(params, strlen(params) + 1);
887 }
888 }
889 prop_array_set(cmd_array, i, target_dict);
890
891 prop_object_release(target_dict);
892
893 i++;
894 }
895
896 return 0;
897 }
898
899
900 /*
901 * For every call I have to set kernel driver version.
902 * Because I can have commands supported only in other
903 * newer/later version. This routine is called for every
904 * ioctl command.
905 */
906 int
907 dm_check_version(prop_dictionary_t dm_dict)
908 {
909 size_t i;
910 int dm_version[3];
911 prop_array_t ver;
912
913 ver = prop_dictionary_get(dm_dict, DM_IOCTL_VERSION);
914
915 for(i=0; i < 3; i++)
916 prop_array_get_uint32(ver, i, &dm_version[i]);
917
918
919 if (DM_VERSION_MAJOR != dm_version[0] || DM_VERSION_MINOR < dm_version[1]){
920 aprint_verbose("libdevmapper/kernel version mismatch "
921 "kernel: %d.%d.%d libdevmapper: %d.%d.%d\n",
922 DM_VERSION_MAJOR, DM_VERSION_MINOR, DM_VERSION_PATCHLEVEL,
923 dm_version[0], dm_version[1], dm_version[2]);
924
925 return EIO;
926 }
927
928 prop_array_set_uint32(ver, 0, DM_VERSION_MAJOR);
929 prop_array_set_uint32(ver, 1, DM_VERSION_MINOR);
930 prop_array_set_uint32(ver, 2, DM_VERSION_PATCHLEVEL);
931
932 return 0;
933 }
934