]> sourceware.org Git - lvm2.git/blob - libdm/libdm-deptree.c
Fix incorrect removal of symlinks after LV deactivation fails.
[lvm2.git] / libdm / libdm-deptree.c
1 /*
2 * Copyright (C) 2005-2007 Red Hat, Inc. All rights reserved.
3 *
4 * This file is part of the device-mapper userspace tools.
5 *
6 * This copyrighted material is made available to anyone wishing to use,
7 * modify, copy, or redistribute it subject to the terms and conditions
8 * of the GNU Lesser General Public License v.2.1.
9 *
10 * You should have received a copy of the GNU Lesser General Public License
11 * along with this program; if not, write to the Free Software Foundation,
12 * Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
13 */
14
15 #include "dmlib.h"
16 #include "libdm-targets.h"
17 #include "libdm-common.h"
18 #include "kdev_t.h"
19 #include "dm-ioctl.h"
20
21 #include <stdarg.h>
22 #include <sys/param.h>
23 #include <sys/utsname.h>
24
25 #define MAX_TARGET_PARAMSIZE 500000
26
27 /* FIXME Fix interface so this is used only by LVM */
28 #define UUID_PREFIX "LVM-"
29
30 /* Supported segment types */
31 enum {
32 SEG_CRYPT,
33 SEG_ERROR,
34 SEG_LINEAR,
35 SEG_MIRRORED,
36 SEG_SNAPSHOT,
37 SEG_SNAPSHOT_ORIGIN,
38 SEG_SNAPSHOT_MERGE,
39 SEG_STRIPED,
40 SEG_ZERO,
41 };
42
43 /* FIXME Add crypt and multipath support */
44
45 struct {
46 unsigned type;
47 const char *target;
48 } dm_segtypes[] = {
49 { SEG_CRYPT, "crypt" },
50 { SEG_ERROR, "error" },
51 { SEG_LINEAR, "linear" },
52 { SEG_MIRRORED, "mirror" },
53 { SEG_SNAPSHOT, "snapshot" },
54 { SEG_SNAPSHOT_ORIGIN, "snapshot-origin" },
55 { SEG_SNAPSHOT_MERGE, "snapshot-merge" },
56 { SEG_STRIPED, "striped" },
57 { SEG_ZERO, "zero"},
58 };
59
60 /* Some segment types have a list of areas of other devices attached */
61 struct seg_area {
62 struct dm_list list;
63
64 struct dm_tree_node *dev_node;
65
66 uint64_t offset;
67 };
68
69 /* Per-segment properties */
70 struct load_segment {
71 struct dm_list list;
72
73 unsigned type;
74
75 uint64_t size;
76
77 unsigned area_count; /* Linear + Striped + Mirrored + Crypt */
78 struct dm_list areas; /* Linear + Striped + Mirrored + Crypt */
79
80 uint32_t stripe_size; /* Striped */
81
82 int persistent; /* Snapshot */
83 uint32_t chunk_size; /* Snapshot */
84 struct dm_tree_node *cow; /* Snapshot */
85 struct dm_tree_node *origin; /* Snapshot + Snapshot origin */
86 struct dm_tree_node *merge; /* Snapshot */
87
88 struct dm_tree_node *log; /* Mirror */
89 uint32_t region_size; /* Mirror */
90 unsigned clustered; /* Mirror */
91 unsigned mirror_area_count; /* Mirror */
92 uint32_t flags; /* Mirror log */
93 char *uuid; /* Clustered mirror log */
94
95 const char *cipher; /* Crypt */
96 const char *chainmode; /* Crypt */
97 const char *iv; /* Crypt */
98 uint64_t iv_offset; /* Crypt */
99 const char *key; /* Crypt */
100 };
101
102 /* Per-device properties */
103 struct load_properties {
104 int read_only;
105 uint32_t major;
106 uint32_t minor;
107
108 uint32_t read_ahead;
109 uint32_t read_ahead_flags;
110
111 unsigned segment_count;
112 unsigned size_changed;
113 struct dm_list segs;
114
115 const char *new_name;
116 };
117
118 /* Two of these used to join two nodes with uses and used_by. */
119 struct dm_tree_link {
120 struct dm_list list;
121 struct dm_tree_node *node;
122 };
123
124 struct dm_tree_node {
125 struct dm_tree *dtree;
126
127 const char *name;
128 const char *uuid;
129 struct dm_info info;
130
131 struct dm_list uses; /* Nodes this node uses */
132 struct dm_list used_by; /* Nodes that use this node */
133
134 int activation_priority; /* 0 gets activated first */
135
136 uint16_t udev_flags; /* Udev control flags */
137
138 void *context; /* External supplied context */
139
140 struct load_properties props; /* For creation/table (re)load */
141 };
142
143 struct dm_tree {
144 struct dm_pool *mem;
145 struct dm_hash_table *devs;
146 struct dm_hash_table *uuids;
147 struct dm_tree_node root;
148 int skip_lockfs; /* 1 skips lockfs (for non-snapshots) */
149 int no_flush; /* 1 sets noflush (mirrors/multipath) */
150 uint32_t cookie;
151 };
152
153 struct dm_tree *dm_tree_create(void)
154 {
155 struct dm_tree *dtree;
156
157 if (!(dtree = dm_malloc(sizeof(*dtree)))) {
158 log_error("dm_tree_create malloc failed");
159 return NULL;
160 }
161
162 memset(dtree, 0, sizeof(*dtree));
163 dtree->root.dtree = dtree;
164 dm_list_init(&dtree->root.uses);
165 dm_list_init(&dtree->root.used_by);
166 dtree->skip_lockfs = 0;
167 dtree->no_flush = 0;
168
169 if (!(dtree->mem = dm_pool_create("dtree", 1024))) {
170 log_error("dtree pool creation failed");
171 dm_free(dtree);
172 return NULL;
173 }
174
175 if (!(dtree->devs = dm_hash_create(8))) {
176 log_error("dtree hash creation failed");
177 dm_pool_destroy(dtree->mem);
178 dm_free(dtree);
179 return NULL;
180 }
181
182 if (!(dtree->uuids = dm_hash_create(32))) {
183 log_error("dtree uuid hash creation failed");
184 dm_hash_destroy(dtree->devs);
185 dm_pool_destroy(dtree->mem);
186 dm_free(dtree);
187 return NULL;
188 }
189
190 return dtree;
191 }
192
193 void dm_tree_free(struct dm_tree *dtree)
194 {
195 if (!dtree)
196 return;
197
198 dm_hash_destroy(dtree->uuids);
199 dm_hash_destroy(dtree->devs);
200 dm_pool_destroy(dtree->mem);
201 dm_free(dtree);
202 }
203
204 static int _nodes_are_linked(const struct dm_tree_node *parent,
205 const struct dm_tree_node *child)
206 {
207 struct dm_tree_link *dlink;
208
209 dm_list_iterate_items(dlink, &parent->uses)
210 if (dlink->node == child)
211 return 1;
212
213 return 0;
214 }
215
216 static int _link(struct dm_list *list, struct dm_tree_node *node)
217 {
218 struct dm_tree_link *dlink;
219
220 if (!(dlink = dm_pool_alloc(node->dtree->mem, sizeof(*dlink)))) {
221 log_error("dtree link allocation failed");
222 return 0;
223 }
224
225 dlink->node = node;
226 dm_list_add(list, &dlink->list);
227
228 return 1;
229 }
230
231 static int _link_nodes(struct dm_tree_node *parent,
232 struct dm_tree_node *child)
233 {
234 if (_nodes_are_linked(parent, child))
235 return 1;
236
237 if (!_link(&parent->uses, child))
238 return 0;
239
240 if (!_link(&child->used_by, parent))
241 return 0;
242
243 return 1;
244 }
245
246 static void _unlink(struct dm_list *list, struct dm_tree_node *node)
247 {
248 struct dm_tree_link *dlink;
249
250 dm_list_iterate_items(dlink, list)
251 if (dlink->node == node) {
252 dm_list_del(&dlink->list);
253 break;
254 }
255 }
256
257 static void _unlink_nodes(struct dm_tree_node *parent,
258 struct dm_tree_node *child)
259 {
260 if (!_nodes_are_linked(parent, child))
261 return;
262
263 _unlink(&parent->uses, child);
264 _unlink(&child->used_by, parent);
265 }
266
267 static int _add_to_toplevel(struct dm_tree_node *node)
268 {
269 return _link_nodes(&node->dtree->root, node);
270 }
271
272 static void _remove_from_toplevel(struct dm_tree_node *node)
273 {
274 _unlink_nodes(&node->dtree->root, node);
275 }
276
277 static int _add_to_bottomlevel(struct dm_tree_node *node)
278 {
279 return _link_nodes(node, &node->dtree->root);
280 }
281
282 static void _remove_from_bottomlevel(struct dm_tree_node *node)
283 {
284 _unlink_nodes(node, &node->dtree->root);
285 }
286
287 static int _link_tree_nodes(struct dm_tree_node *parent, struct dm_tree_node *child)
288 {
289 /* Don't link to root node if child already has a parent */
290 if ((parent == &parent->dtree->root)) {
291 if (dm_tree_node_num_children(child, 1))
292 return 1;
293 } else
294 _remove_from_toplevel(child);
295
296 if ((child == &child->dtree->root)) {
297 if (dm_tree_node_num_children(parent, 0))
298 return 1;
299 } else
300 _remove_from_bottomlevel(parent);
301
302 return _link_nodes(parent, child);
303 }
304
305 static struct dm_tree_node *_create_dm_tree_node(struct dm_tree *dtree,
306 const char *name,
307 const char *uuid,
308 struct dm_info *info,
309 void *context,
310 uint16_t udev_flags)
311 {
312 struct dm_tree_node *node;
313 uint64_t dev;
314
315 if (!(node = dm_pool_zalloc(dtree->mem, sizeof(*node)))) {
316 log_error("_create_dm_tree_node alloc failed");
317 return NULL;
318 }
319
320 node->dtree = dtree;
321
322 node->name = name;
323 node->uuid = uuid;
324 node->info = *info;
325 node->context = context;
326 node->udev_flags = udev_flags;
327 node->activation_priority = 0;
328
329 dm_list_init(&node->uses);
330 dm_list_init(&node->used_by);
331 dm_list_init(&node->props.segs);
332
333 dev = MKDEV(info->major, info->minor);
334
335 if (!dm_hash_insert_binary(dtree->devs, (const char *) &dev,
336 sizeof(dev), node)) {
337 log_error("dtree node hash insertion failed");
338 dm_pool_free(dtree->mem, node);
339 return NULL;
340 }
341
342 if (uuid && *uuid &&
343 !dm_hash_insert(dtree->uuids, uuid, node)) {
344 log_error("dtree uuid hash insertion failed");
345 dm_hash_remove_binary(dtree->devs, (const char *) &dev,
346 sizeof(dev));
347 dm_pool_free(dtree->mem, node);
348 return NULL;
349 }
350
351 return node;
352 }
353
354 static struct dm_tree_node *_find_dm_tree_node(struct dm_tree *dtree,
355 uint32_t major, uint32_t minor)
356 {
357 uint64_t dev = MKDEV(major, minor);
358
359 return dm_hash_lookup_binary(dtree->devs, (const char *) &dev,
360 sizeof(dev));
361 }
362
363 static struct dm_tree_node *_find_dm_tree_node_by_uuid(struct dm_tree *dtree,
364 const char *uuid)
365 {
366 struct dm_tree_node *node;
367
368 if ((node = dm_hash_lookup(dtree->uuids, uuid)))
369 return node;
370
371 if (strncmp(uuid, UUID_PREFIX, sizeof(UUID_PREFIX) - 1))
372 return NULL;
373
374 return dm_hash_lookup(dtree->uuids, uuid + sizeof(UUID_PREFIX) - 1);
375 }
376
377 static int _deps(struct dm_task **dmt, struct dm_pool *mem, uint32_t major, uint32_t minor,
378 const char **name, const char **uuid,
379 struct dm_info *info, struct dm_deps **deps)
380 {
381 memset(info, 0, sizeof(*info));
382
383 if (!dm_is_dm_major(major)) {
384 *name = "";
385 *uuid = "";
386 *deps = NULL;
387 info->major = major;
388 info->minor = minor;
389 info->exists = 0;
390 info->live_table = 0;
391 info->inactive_table = 0;
392 info->read_only = 0;
393 return 1;
394 }
395
396 if (!(*dmt = dm_task_create(DM_DEVICE_DEPS))) {
397 log_error("deps dm_task creation failed");
398 return 0;
399 }
400
401 if (!dm_task_set_major(*dmt, major)) {
402 log_error("_deps: failed to set major for (%" PRIu32 ":%" PRIu32 ")",
403 major, minor);
404 goto failed;
405 }
406
407 if (!dm_task_set_minor(*dmt, minor)) {
408 log_error("_deps: failed to set minor for (%" PRIu32 ":%" PRIu32 ")",
409 major, minor);
410 goto failed;
411 }
412
413 if (!dm_task_run(*dmt)) {
414 log_error("_deps: task run failed for (%" PRIu32 ":%" PRIu32 ")",
415 major, minor);
416 goto failed;
417 }
418
419 if (!dm_task_get_info(*dmt, info)) {
420 log_error("_deps: failed to get info for (%" PRIu32 ":%" PRIu32 ")",
421 major, minor);
422 goto failed;
423 }
424
425 if (!info->exists) {
426 *name = "";
427 *uuid = "";
428 *deps = NULL;
429 } else {
430 if (info->major != major) {
431 log_error("Inconsistent dtree major number: %u != %u",
432 major, info->major);
433 goto failed;
434 }
435 if (info->minor != minor) {
436 log_error("Inconsistent dtree minor number: %u != %u",
437 minor, info->minor);
438 goto failed;
439 }
440 if (!(*name = dm_pool_strdup(mem, dm_task_get_name(*dmt)))) {
441 log_error("name pool_strdup failed");
442 goto failed;
443 }
444 if (!(*uuid = dm_pool_strdup(mem, dm_task_get_uuid(*dmt)))) {
445 log_error("uuid pool_strdup failed");
446 goto failed;
447 }
448 *deps = dm_task_get_deps(*dmt);
449 }
450
451 return 1;
452
453 failed:
454 dm_task_destroy(*dmt);
455 return 0;
456 }
457
458 static struct dm_tree_node *_add_dev(struct dm_tree *dtree,
459 struct dm_tree_node *parent,
460 uint32_t major, uint32_t minor,
461 uint16_t udev_flags)
462 {
463 struct dm_task *dmt = NULL;
464 struct dm_info info;
465 struct dm_deps *deps = NULL;
466 const char *name = NULL;
467 const char *uuid = NULL;
468 struct dm_tree_node *node = NULL;
469 uint32_t i;
470 int new = 0;
471
472 /* Already in tree? */
473 if (!(node = _find_dm_tree_node(dtree, major, minor))) {
474 if (!_deps(&dmt, dtree->mem, major, minor, &name, &uuid, &info, &deps))
475 return_NULL;
476
477 if (!(node = _create_dm_tree_node(dtree, name, uuid, &info,
478 NULL, udev_flags)))
479 goto_out;
480 new = 1;
481 }
482
483 if (!_link_tree_nodes(parent, node)) {
484 node = NULL;
485 goto_out;
486 }
487
488 /* If node was already in tree, no need to recurse. */
489 if (!new)
490 goto out;
491
492 /* Can't recurse if not a mapped device or there are no dependencies */
493 if (!node->info.exists || !deps->count) {
494 if (!_add_to_bottomlevel(node)) {
495 stack;
496 node = NULL;
497 }
498 goto out;
499 }
500
501 /* Add dependencies to tree */
502 for (i = 0; i < deps->count; i++)
503 if (!_add_dev(dtree, node, MAJOR(deps->device[i]),
504 MINOR(deps->device[i]), udev_flags)) {
505 node = NULL;
506 goto_out;
507 }
508
509 out:
510 if (dmt)
511 dm_task_destroy(dmt);
512
513 return node;
514 }
515
516 static int _node_clear_table(struct dm_tree_node *dnode)
517 {
518 struct dm_task *dmt;
519 struct dm_info *info;
520 const char *name;
521 int r;
522
523 if (!(info = &dnode->info)) {
524 log_error("_node_clear_table failed: missing info");
525 return 0;
526 }
527
528 if (!(name = dm_tree_node_get_name(dnode))) {
529 log_error("_node_clear_table failed: missing name");
530 return 0;
531 }
532
533 /* Is there a table? */
534 if (!info->exists || !info->inactive_table)
535 return 1;
536
537 log_verbose("Clearing inactive table %s (%" PRIu32 ":%" PRIu32 ")",
538 name, info->major, info->minor);
539
540 if (!(dmt = dm_task_create(DM_DEVICE_CLEAR))) {
541 dm_task_destroy(dmt);
542 log_error("Table clear dm_task creation failed for %s", name);
543 return 0;
544 }
545
546 if (!dm_task_set_major(dmt, info->major) ||
547 !dm_task_set_minor(dmt, info->minor)) {
548 log_error("Failed to set device number for %s table clear", name);
549 dm_task_destroy(dmt);
550 return 0;
551 }
552
553 r = dm_task_run(dmt);
554
555 if (!dm_task_get_info(dmt, info)) {
556 log_error("_node_clear_table failed: info missing after running task for %s", name);
557 r = 0;
558 }
559
560 dm_task_destroy(dmt);
561
562 return r;
563 }
564
565 struct dm_tree_node *dm_tree_add_new_dev(struct dm_tree *dtree,
566 const char *name,
567 const char *uuid,
568 uint32_t major, uint32_t minor,
569 int read_only,
570 int clear_inactive,
571 void *context)
572 {
573 struct dm_tree_node *dnode;
574 struct dm_info info;
575 const char *name2;
576 const char *uuid2;
577
578 /* Do we need to add node to tree? */
579 if (!(dnode = dm_tree_find_node_by_uuid(dtree, uuid))) {
580 if (!(name2 = dm_pool_strdup(dtree->mem, name))) {
581 log_error("name pool_strdup failed");
582 return NULL;
583 }
584 if (!(uuid2 = dm_pool_strdup(dtree->mem, uuid))) {
585 log_error("uuid pool_strdup failed");
586 return NULL;
587 }
588
589 info.major = 0;
590 info.minor = 0;
591 info.exists = 0;
592 info.live_table = 0;
593 info.inactive_table = 0;
594 info.read_only = 0;
595
596 if (!(dnode = _create_dm_tree_node(dtree, name2, uuid2, &info,
597 context, 0)))
598 return_NULL;
599
600 /* Attach to root node until a table is supplied */
601 if (!_add_to_toplevel(dnode) || !_add_to_bottomlevel(dnode))
602 return_NULL;
603
604 dnode->props.major = major;
605 dnode->props.minor = minor;
606 dnode->props.new_name = NULL;
607 dnode->props.size_changed = 0;
608 } else if (strcmp(name, dnode->name)) {
609 /* Do we need to rename node? */
610 if (!(dnode->props.new_name = dm_pool_strdup(dtree->mem, name))) {
611 log_error("name pool_strdup failed");
612 return 0;
613 }
614 }
615
616 dnode->props.read_only = read_only ? 1 : 0;
617 dnode->props.read_ahead = DM_READ_AHEAD_AUTO;
618 dnode->props.read_ahead_flags = 0;
619
620 if (clear_inactive && !_node_clear_table(dnode))
621 return_NULL;
622
623 dnode->context = context;
624 dnode->udev_flags = 0;
625
626 return dnode;
627 }
628
629 struct dm_tree_node *dm_tree_add_new_dev_with_udev_flags(struct dm_tree *dtree,
630 const char *name,
631 const char *uuid,
632 uint32_t major,
633 uint32_t minor,
634 int read_only,
635 int clear_inactive,
636 void *context,
637 uint16_t udev_flags)
638 {
639 struct dm_tree_node *node;
640
641 if ((node = dm_tree_add_new_dev(dtree, name, uuid, major, minor, read_only,
642 clear_inactive, context)))
643 node->udev_flags = udev_flags;
644
645 return node;
646 }
647
648
649 void dm_tree_node_set_read_ahead(struct dm_tree_node *dnode,
650 uint32_t read_ahead,
651 uint32_t read_ahead_flags)
652 {
653 dnode->props.read_ahead = read_ahead;
654 dnode->props.read_ahead_flags = read_ahead_flags;
655 }
656
657 int dm_tree_add_dev(struct dm_tree *dtree, uint32_t major, uint32_t minor)
658 {
659 return _add_dev(dtree, &dtree->root, major, minor, 0) ? 1 : 0;
660 }
661
662 int dm_tree_add_dev_with_udev_flags(struct dm_tree *dtree, uint32_t major,
663 uint32_t minor, uint16_t udev_flags)
664 {
665 return _add_dev(dtree, &dtree->root, major, minor, udev_flags) ? 1 : 0;
666 }
667
668 const char *dm_tree_node_get_name(const struct dm_tree_node *node)
669 {
670 return node->info.exists ? node->name : "";
671 }
672
673 const char *dm_tree_node_get_uuid(const struct dm_tree_node *node)
674 {
675 return node->info.exists ? node->uuid : "";
676 }
677
678 const struct dm_info *dm_tree_node_get_info(const struct dm_tree_node *node)
679 {
680 return &node->info;
681 }
682
683 void *dm_tree_node_get_context(const struct dm_tree_node *node)
684 {
685 return node->context;
686 }
687
688 int dm_tree_node_size_changed(const struct dm_tree_node *dnode)
689 {
690 return dnode->props.size_changed;
691 }
692
693 int dm_tree_node_num_children(const struct dm_tree_node *node, uint32_t inverted)
694 {
695 if (inverted) {
696 if (_nodes_are_linked(&node->dtree->root, node))
697 return 0;
698 return dm_list_size(&node->used_by);
699 }
700
701 if (_nodes_are_linked(node, &node->dtree->root))
702 return 0;
703
704 return dm_list_size(&node->uses);
705 }
706
707 /*
708 * Returns 1 if no prefix supplied
709 */
710 static int _uuid_prefix_matches(const char *uuid, const char *uuid_prefix, size_t uuid_prefix_len)
711 {
712 if (!uuid_prefix)
713 return 1;
714
715 if (!strncmp(uuid, uuid_prefix, uuid_prefix_len))
716 return 1;
717
718 /* Handle transition: active device uuids might be missing the prefix */
719 if (uuid_prefix_len <= 4)
720 return 0;
721
722 if (!strncmp(uuid, UUID_PREFIX, sizeof(UUID_PREFIX) - 1))
723 return 0;
724
725 if (strncmp(uuid_prefix, UUID_PREFIX, sizeof(UUID_PREFIX) - 1))
726 return 0;
727
728 if (!strncmp(uuid, uuid_prefix + sizeof(UUID_PREFIX) - 1, uuid_prefix_len - (sizeof(UUID_PREFIX) - 1)))
729 return 1;
730
731 return 0;
732 }
733
734 /*
735 * Returns 1 if no children.
736 */
737 static int _children_suspended(struct dm_tree_node *node,
738 uint32_t inverted,
739 const char *uuid_prefix,
740 size_t uuid_prefix_len)
741 {
742 struct dm_list *list;
743 struct dm_tree_link *dlink;
744 const struct dm_info *dinfo;
745 const char *uuid;
746
747 if (inverted) {
748 if (_nodes_are_linked(&node->dtree->root, node))
749 return 1;
750 list = &node->used_by;
751 } else {
752 if (_nodes_are_linked(node, &node->dtree->root))
753 return 1;
754 list = &node->uses;
755 }
756
757 dm_list_iterate_items(dlink, list) {
758 if (!(uuid = dm_tree_node_get_uuid(dlink->node))) {
759 stack;
760 continue;
761 }
762
763 /* Ignore if it doesn't belong to this VG */
764 if (!_uuid_prefix_matches(uuid, uuid_prefix, uuid_prefix_len))
765 continue;
766
767 if (!(dinfo = dm_tree_node_get_info(dlink->node))) {
768 stack; /* FIXME Is this normal? */
769 return 0;
770 }
771
772 if (!dinfo->suspended)
773 return 0;
774 }
775
776 return 1;
777 }
778
779 /*
780 * Set major and minor to zero for root of tree.
781 */
782 struct dm_tree_node *dm_tree_find_node(struct dm_tree *dtree,
783 uint32_t major,
784 uint32_t minor)
785 {
786 if (!major && !minor)
787 return &dtree->root;
788
789 return _find_dm_tree_node(dtree, major, minor);
790 }
791
792 /*
793 * Set uuid to NULL for root of tree.
794 */
795 struct dm_tree_node *dm_tree_find_node_by_uuid(struct dm_tree *dtree,
796 const char *uuid)
797 {
798 if (!uuid || !*uuid)
799 return &dtree->root;
800
801 return _find_dm_tree_node_by_uuid(dtree, uuid);
802 }
803
804 /*
805 * First time set *handle to NULL.
806 * Set inverted to invert the tree.
807 */
808 struct dm_tree_node *dm_tree_next_child(void **handle,
809 const struct dm_tree_node *parent,
810 uint32_t inverted)
811 {
812 struct dm_list **dlink = (struct dm_list **) handle;
813 const struct dm_list *use_list;
814
815 if (inverted)
816 use_list = &parent->used_by;
817 else
818 use_list = &parent->uses;
819
820 if (!*dlink)
821 *dlink = dm_list_first(use_list);
822 else
823 *dlink = dm_list_next(use_list, *dlink);
824
825 return (*dlink) ? dm_list_item(*dlink, struct dm_tree_link)->node : NULL;
826 }
827
828 /*
829 * Deactivate a device with its dependencies if the uuid prefix matches.
830 */
831 static int _info_by_dev(uint32_t major, uint32_t minor, int with_open_count,
832 struct dm_info *info)
833 {
834 struct dm_task *dmt;
835 int r;
836
837 if (!(dmt = dm_task_create(DM_DEVICE_INFO))) {
838 log_error("_info_by_dev: dm_task creation failed");
839 return 0;
840 }
841
842 if (!dm_task_set_major(dmt, major) || !dm_task_set_minor(dmt, minor)) {
843 log_error("_info_by_dev: Failed to set device number");
844 dm_task_destroy(dmt);
845 return 0;
846 }
847
848 if (!with_open_count && !dm_task_no_open_count(dmt))
849 log_error("Failed to disable open_count");
850
851 if ((r = dm_task_run(dmt)))
852 r = dm_task_get_info(dmt, info);
853
854 dm_task_destroy(dmt);
855
856 return r;
857 }
858
859 static int _deactivate_node(const char *name, uint32_t major, uint32_t minor,
860 uint32_t *cookie, uint16_t udev_flags)
861 {
862 struct dm_task *dmt;
863 int r = 0;
864
865 log_verbose("Removing %s (%" PRIu32 ":%" PRIu32 ")", name, major, minor);
866
867 if (!(dmt = dm_task_create(DM_DEVICE_REMOVE))) {
868 log_error("Deactivation dm_task creation failed for %s", name);
869 return 0;
870 }
871
872 if (!dm_task_set_major(dmt, major) || !dm_task_set_minor(dmt, minor)) {
873 log_error("Failed to set device number for %s deactivation", name);
874 goto out;
875 }
876
877 if (!dm_task_no_open_count(dmt))
878 log_error("Failed to disable open_count");
879
880 if (!dm_task_set_cookie(dmt, cookie, udev_flags))
881 goto out;
882
883 r = dm_task_run(dmt);
884
885 /* FIXME Until kernel returns actual name so dm-ioctl.c can handle it */
886 rm_dev_node(name, dmt->cookie_set &&
887 !(udev_flags & DM_UDEV_DISABLE_DM_RULES_FLAG));
888
889 /* FIXME Remove node from tree or mark invalid? */
890
891 out:
892 dm_task_destroy(dmt);
893
894 return r;
895 }
896
897 static int _rename_node(const char *old_name, const char *new_name, uint32_t major,
898 uint32_t minor, uint32_t *cookie, uint16_t udev_flags)
899 {
900 struct dm_task *dmt;
901 int r = 0;
902
903 log_verbose("Renaming %s (%" PRIu32 ":%" PRIu32 ") to %s", old_name, major, minor, new_name);
904
905 if (!(dmt = dm_task_create(DM_DEVICE_RENAME))) {
906 log_error("Rename dm_task creation failed for %s", old_name);
907 return 0;
908 }
909
910 if (!dm_task_set_name(dmt, old_name)) {
911 log_error("Failed to set name for %s rename.", old_name);
912 goto out;
913 }
914
915 if (!dm_task_set_newname(dmt, new_name))
916 goto_out;
917
918 if (!dm_task_no_open_count(dmt))
919 log_error("Failed to disable open_count");
920
921 if (!dm_task_set_cookie(dmt, cookie, udev_flags))
922 goto out;
923
924 r = dm_task_run(dmt);
925
926 out:
927 dm_task_destroy(dmt);
928
929 return r;
930 }
931
932 /* FIXME Merge with _suspend_node? */
933 static int _resume_node(const char *name, uint32_t major, uint32_t minor,
934 uint32_t read_ahead, uint32_t read_ahead_flags,
935 struct dm_info *newinfo, uint32_t *cookie,
936 uint16_t udev_flags)
937 {
938 struct dm_task *dmt;
939 int r = 0;
940
941 log_verbose("Resuming %s (%" PRIu32 ":%" PRIu32 ")", name, major, minor);
942
943 if (!(dmt = dm_task_create(DM_DEVICE_RESUME))) {
944 log_error("Suspend dm_task creation failed for %s", name);
945 return 0;
946 }
947
948 /* FIXME Kernel should fill in name on return instead */
949 if (!dm_task_set_name(dmt, name)) {
950 log_error("Failed to set readahead device name for %s", name);
951 goto out;
952 }
953
954 if (!dm_task_set_major(dmt, major) || !dm_task_set_minor(dmt, minor)) {
955 log_error("Failed to set device number for %s resumption.", name);
956 goto out;
957 }
958
959 if (!dm_task_no_open_count(dmt))
960 log_error("Failed to disable open_count");
961
962 if (!dm_task_set_read_ahead(dmt, read_ahead, read_ahead_flags))
963 log_error("Failed to set read ahead");
964
965 if (!dm_task_set_cookie(dmt, cookie, udev_flags))
966 goto out;
967
968 if ((r = dm_task_run(dmt)))
969 r = dm_task_get_info(dmt, newinfo);
970
971 out:
972 dm_task_destroy(dmt);
973
974 return r;
975 }
976
977 static int _suspend_node(const char *name, uint32_t major, uint32_t minor,
978 int skip_lockfs, int no_flush, struct dm_info *newinfo)
979 {
980 struct dm_task *dmt;
981 int r;
982
983 log_verbose("Suspending %s (%" PRIu32 ":%" PRIu32 ")%s%s",
984 name, major, minor,
985 skip_lockfs ? "" : " with filesystem sync",
986 no_flush ? "" : " with device flush");
987
988 if (!(dmt = dm_task_create(DM_DEVICE_SUSPEND))) {
989 log_error("Suspend dm_task creation failed for %s", name);
990 return 0;
991 }
992
993 if (!dm_task_set_major(dmt, major) || !dm_task_set_minor(dmt, minor)) {
994 log_error("Failed to set device number for %s suspension.", name);
995 dm_task_destroy(dmt);
996 return 0;
997 }
998
999 if (!dm_task_no_open_count(dmt))
1000 log_error("Failed to disable open_count");
1001
1002 if (skip_lockfs && !dm_task_skip_lockfs(dmt))
1003 log_error("Failed to set skip_lockfs flag.");
1004
1005 if (no_flush && !dm_task_no_flush(dmt))
1006 log_error("Failed to set no_flush flag.");
1007
1008 if ((r = dm_task_run(dmt)))
1009 r = dm_task_get_info(dmt, newinfo);
1010
1011 dm_task_destroy(dmt);
1012
1013 return r;
1014 }
1015
1016 int dm_tree_deactivate_children(struct dm_tree_node *dnode,
1017 const char *uuid_prefix,
1018 size_t uuid_prefix_len)
1019 {
1020 int r = 1;
1021 void *handle = NULL;
1022 struct dm_tree_node *child = dnode;
1023 struct dm_info info;
1024 const struct dm_info *dinfo;
1025 const char *name;
1026 const char *uuid;
1027
1028 while ((child = dm_tree_next_child(&handle, dnode, 0))) {
1029 if (!(dinfo = dm_tree_node_get_info(child))) {
1030 stack;
1031 continue;
1032 }
1033
1034 if (!(name = dm_tree_node_get_name(child))) {
1035 stack;
1036 continue;
1037 }
1038
1039 if (!(uuid = dm_tree_node_get_uuid(child))) {
1040 stack;
1041 continue;
1042 }
1043
1044 /* Ignore if it doesn't belong to this VG */
1045 if (!_uuid_prefix_matches(uuid, uuid_prefix, uuid_prefix_len))
1046 continue;
1047
1048 /* Refresh open_count */
1049 if (!_info_by_dev(dinfo->major, dinfo->minor, 1, &info) ||
1050 !info.exists)
1051 continue;
1052
1053 if (info.open_count) {
1054 r = 0;
1055 continue;
1056 }
1057
1058 if (!_deactivate_node(name, info.major, info.minor,
1059 &child->dtree->cookie, child->udev_flags)) {
1060 log_error("Unable to deactivate %s (%" PRIu32
1061 ":%" PRIu32 ")", name, info.major,
1062 info.minor);
1063 r = 0;
1064 continue;
1065 }
1066
1067 if (dm_tree_node_num_children(child, 0))
1068 if (!dm_tree_deactivate_children(child, uuid_prefix, uuid_prefix_len))
1069 return_0;
1070 }
1071
1072 return r;
1073 }
1074
1075 void dm_tree_skip_lockfs(struct dm_tree_node *dnode)
1076 {
1077 dnode->dtree->skip_lockfs = 1;
1078 }
1079
1080 void dm_tree_use_no_flush_suspend(struct dm_tree_node *dnode)
1081 {
1082 dnode->dtree->no_flush = 1;
1083 }
1084
1085 int dm_tree_suspend_children(struct dm_tree_node *dnode,
1086 const char *uuid_prefix,
1087 size_t uuid_prefix_len)
1088 {
1089 int r = 1;
1090 void *handle = NULL;
1091 struct dm_tree_node *child = dnode;
1092 struct dm_info info, newinfo;
1093 const struct dm_info *dinfo;
1094 const char *name;
1095 const char *uuid;
1096
1097 /* Suspend nodes at this level of the tree */
1098 while ((child = dm_tree_next_child(&handle, dnode, 0))) {
1099 if (!(dinfo = dm_tree_node_get_info(child))) {
1100 stack;
1101 continue;
1102 }
1103
1104 if (!(name = dm_tree_node_get_name(child))) {
1105 stack;
1106 continue;
1107 }
1108
1109 if (!(uuid = dm_tree_node_get_uuid(child))) {
1110 stack;
1111 continue;
1112 }
1113
1114 /* Ignore if it doesn't belong to this VG */
1115 if (!_uuid_prefix_matches(uuid, uuid_prefix, uuid_prefix_len))
1116 continue;
1117
1118 /* Ensure immediate parents are already suspended */
1119 if (!_children_suspended(child, 1, uuid_prefix, uuid_prefix_len))
1120 continue;
1121
1122 if (!_info_by_dev(dinfo->major, dinfo->minor, 0, &info) ||
1123 !info.exists || info.suspended)
1124 continue;
1125
1126 if (!_suspend_node(name, info.major, info.minor,
1127 child->dtree->skip_lockfs,
1128 child->dtree->no_flush, &newinfo)) {
1129 log_error("Unable to suspend %s (%" PRIu32
1130 ":%" PRIu32 ")", name, info.major,
1131 info.minor);
1132 r = 0;
1133 continue;
1134 }
1135
1136 /* Update cached info */
1137 child->info = newinfo;
1138 }
1139
1140 /* Then suspend any child nodes */
1141 handle = NULL;
1142
1143 while ((child = dm_tree_next_child(&handle, dnode, 0))) {
1144 if (!(uuid = dm_tree_node_get_uuid(child))) {
1145 stack;
1146 continue;
1147 }
1148
1149 /* Ignore if it doesn't belong to this VG */
1150 if (!_uuid_prefix_matches(uuid, uuid_prefix, uuid_prefix_len))
1151 continue;
1152
1153 if (dm_tree_node_num_children(child, 0))
1154 if (!dm_tree_suspend_children(child, uuid_prefix, uuid_prefix_len))
1155 return_0;
1156 }
1157
1158 return r;
1159 }
1160
1161 int dm_tree_activate_children(struct dm_tree_node *dnode,
1162 const char *uuid_prefix,
1163 size_t uuid_prefix_len)
1164 {
1165 int r = 1;
1166 void *handle = NULL;
1167 struct dm_tree_node *child = dnode;
1168 struct dm_info newinfo;
1169 const char *name;
1170 const char *uuid;
1171 int priority;
1172
1173 /* Activate children first */
1174 while ((child = dm_tree_next_child(&handle, dnode, 0))) {
1175 if (!(uuid = dm_tree_node_get_uuid(child))) {
1176 stack;
1177 continue;
1178 }
1179
1180 if (!_uuid_prefix_matches(uuid, uuid_prefix, uuid_prefix_len))
1181 continue;
1182
1183 if (dm_tree_node_num_children(child, 0))
1184 if (!dm_tree_activate_children(child, uuid_prefix, uuid_prefix_len))
1185 return_0;
1186 }
1187
1188 handle = NULL;
1189
1190 for (priority = 0; priority < 3; priority++) {
1191 while ((child = dm_tree_next_child(&handle, dnode, 0))) {
1192 if (!(uuid = dm_tree_node_get_uuid(child))) {
1193 stack;
1194 continue;
1195 }
1196
1197 if (!_uuid_prefix_matches(uuid, uuid_prefix, uuid_prefix_len))
1198 continue;
1199
1200 if (priority != child->activation_priority)
1201 continue;
1202
1203 if (!(name = dm_tree_node_get_name(child))) {
1204 stack;
1205 continue;
1206 }
1207
1208 /* Rename? */
1209 if (child->props.new_name) {
1210 if (!_rename_node(name, child->props.new_name, child->info.major,
1211 child->info.minor, &child->dtree->cookie,
1212 child->udev_flags)) {
1213 log_error("Failed to rename %s (%" PRIu32
1214 ":%" PRIu32 ") to %s", name, child->info.major,
1215 child->info.minor, child->props.new_name);
1216 return 0;
1217 }
1218 child->name = child->props.new_name;
1219 child->props.new_name = NULL;
1220 }
1221
1222 if (!child->info.inactive_table && !child->info.suspended)
1223 continue;
1224
1225 if (!_resume_node(child->name, child->info.major, child->info.minor,
1226 child->props.read_ahead, child->props.read_ahead_flags,
1227 &newinfo, &child->dtree->cookie, child->udev_flags)) {
1228 log_error("Unable to resume %s (%" PRIu32
1229 ":%" PRIu32 ")", child->name, child->info.major,
1230 child->info.minor);
1231 r = 0;
1232 continue;
1233 }
1234
1235 /* Update cached info */
1236 child->info = newinfo;
1237 }
1238 }
1239
1240 handle = NULL;
1241
1242 return r;
1243 }
1244
1245 static int _create_node(struct dm_tree_node *dnode)
1246 {
1247 int r = 0;
1248 struct dm_task *dmt;
1249
1250 log_verbose("Creating %s", dnode->name);
1251
1252 if (!(dmt = dm_task_create(DM_DEVICE_CREATE))) {
1253 log_error("Create dm_task creation failed for %s", dnode->name);
1254 return 0;
1255 }
1256
1257 if (!dm_task_set_name(dmt, dnode->name)) {
1258 log_error("Failed to set device name for %s", dnode->name);
1259 goto out;
1260 }
1261
1262 if (!dm_task_set_uuid(dmt, dnode->uuid)) {
1263 log_error("Failed to set uuid for %s", dnode->name);
1264 goto out;
1265 }
1266
1267 if (dnode->props.major &&
1268 (!dm_task_set_major(dmt, dnode->props.major) ||
1269 !dm_task_set_minor(dmt, dnode->props.minor))) {
1270 log_error("Failed to set device number for %s creation.", dnode->name);
1271 goto out;
1272 }
1273
1274 if (dnode->props.read_only && !dm_task_set_ro(dmt)) {
1275 log_error("Failed to set read only flag for %s", dnode->name);
1276 goto out;
1277 }
1278
1279 if (!dm_task_no_open_count(dmt))
1280 log_error("Failed to disable open_count");
1281
1282 if ((r = dm_task_run(dmt)))
1283 r = dm_task_get_info(dmt, &dnode->info);
1284
1285 out:
1286 dm_task_destroy(dmt);
1287
1288 return r;
1289 }
1290
1291
1292 static int _build_dev_string(char *devbuf, size_t bufsize, struct dm_tree_node *node)
1293 {
1294 if (!dm_format_dev(devbuf, bufsize, node->info.major, node->info.minor)) {
1295 log_error("Failed to format %s device number for %s as dm "
1296 "target (%u,%u)",
1297 node->name, node->uuid, node->info.major, node->info.minor);
1298 return 0;
1299 }
1300
1301 return 1;
1302 }
1303
1304 /* simplify string emiting code */
1305 #define EMIT_PARAMS(p, str...)\
1306 do {\
1307 int w;\
1308 if ((w = dm_snprintf(params + p, paramsize - (size_t) p, str)) < 0) {\
1309 stack; /* Out of space */\
1310 return -1;\
1311 }\
1312 p += w;\
1313 } while (0)
1314
1315 /*
1316 * _emit_areas_line
1317 *
1318 * Returns: 1 on success, 0 on failure
1319 */
1320 static int _emit_areas_line(struct dm_task *dmt __attribute((unused)),
1321 struct load_segment *seg, char *params,
1322 size_t paramsize, int *pos)
1323 {
1324 struct seg_area *area;
1325 char devbuf[DM_FORMAT_DEV_BUFSIZE];
1326 unsigned first_time = 1;
1327
1328 dm_list_iterate_items(area, &seg->areas) {
1329 if (!_build_dev_string(devbuf, sizeof(devbuf), area->dev_node))
1330 return_0;
1331
1332 EMIT_PARAMS(*pos, "%s%s %" PRIu64, first_time ? "" : " ",
1333 devbuf, area->offset);
1334
1335 first_time = 0;
1336 }
1337
1338 return 1;
1339 }
1340
1341 /*
1342 * Returns: 1 on success, 0 on failure
1343 */
1344 static int _mirror_emit_segment_line(struct dm_task *dmt, uint32_t major,
1345 uint32_t minor, struct load_segment *seg,
1346 uint64_t *seg_start, char *params,
1347 size_t paramsize)
1348 {
1349 int r;
1350 int block_on_error = 0;
1351 int handle_errors = 0;
1352 int dm_log_userspace = 0;
1353 struct utsname uts;
1354 unsigned log_parm_count;
1355 int pos = 0;
1356 char logbuf[DM_FORMAT_DEV_BUFSIZE];
1357 const char *logtype;
1358
1359 r = uname(&uts);
1360 if (r)
1361 return_0;
1362
1363 if ((seg->flags & DM_BLOCK_ON_ERROR)) {
1364 /*
1365 * Originally, block_on_error was an argument to the log
1366 * portion of the mirror CTR table. It was renamed to
1367 * "handle_errors" and now resides in the 'features'
1368 * section of the mirror CTR table (i.e. at the end).
1369 *
1370 * We can identify whether to use "block_on_error" or
1371 * "handle_errors" by the dm-mirror module's version
1372 * number (>= 1.12) or by the kernel version (>= 2.6.22).
1373 */
1374 if (strncmp(uts.release, "2.6.22", 6) >= 0)
1375 handle_errors = 1;
1376 else
1377 block_on_error = 1;
1378 }
1379
1380 if (seg->clustered) {
1381 /* Cluster mirrors require a UUID */
1382 if (!seg->uuid)
1383 return_0;
1384
1385 /*
1386 * Cluster mirrors used to have their own log
1387 * types. Now they are accessed through the
1388 * userspace log type.
1389 *
1390 * The dm-log-userspace module was added to the
1391 * 2.6.31 kernel.
1392 */
1393 if (strncmp(uts.release, "2.6.31", 6) >= 0)
1394 dm_log_userspace = 1;
1395 }
1396
1397 /* Region size */
1398 log_parm_count = 1;
1399
1400 /* [no]sync, block_on_error etc. */
1401 log_parm_count += hweight32(seg->flags);
1402
1403 /* "handle_errors" is a feature arg now */
1404 if (handle_errors)
1405 log_parm_count--;
1406
1407 /* DM_CORELOG does not count in the param list */
1408 if (seg->flags & DM_CORELOG)
1409 log_parm_count--;
1410
1411 if (seg->clustered) {
1412 log_parm_count++; /* For UUID */
1413
1414 if (!dm_log_userspace)
1415 EMIT_PARAMS(pos, "clustered-");
1416 else
1417 /* For clustered-* type field inserted later */
1418 log_parm_count++;
1419 }
1420
1421 if (!seg->log)
1422 logtype = "core";
1423 else {
1424 logtype = "disk";
1425 log_parm_count++;
1426 if (!_build_dev_string(logbuf, sizeof(logbuf), seg->log))
1427 return_0;
1428 }
1429
1430 if (dm_log_userspace)
1431 EMIT_PARAMS(pos, "userspace %u %s clustered-%s",
1432 log_parm_count, seg->uuid, logtype);
1433 else
1434 EMIT_PARAMS(pos, "%s %u", logtype, log_parm_count);
1435
1436 if (seg->log)
1437 EMIT_PARAMS(pos, " %s", logbuf);
1438
1439 EMIT_PARAMS(pos, " %u", seg->region_size);
1440
1441 if (seg->clustered && !dm_log_userspace)
1442 EMIT_PARAMS(pos, " %s", seg->uuid);
1443
1444 if ((seg->flags & DM_NOSYNC))
1445 EMIT_PARAMS(pos, " nosync");
1446 else if ((seg->flags & DM_FORCESYNC))
1447 EMIT_PARAMS(pos, " sync");
1448
1449 if (block_on_error)
1450 EMIT_PARAMS(pos, " block_on_error");
1451
1452 EMIT_PARAMS(pos, " %u ", seg->mirror_area_count);
1453
1454 if ((r = _emit_areas_line(dmt, seg, params, paramsize, &pos)) <= 0)
1455 return_0;
1456
1457 if (handle_errors)
1458 EMIT_PARAMS(pos, " 1 handle_errors");
1459
1460 return 1;
1461 }
1462
1463 static int _emit_segment_line(struct dm_task *dmt, uint32_t major,
1464 uint32_t minor, struct load_segment *seg,
1465 uint64_t *seg_start, char *params,
1466 size_t paramsize)
1467 {
1468 int pos = 0;
1469 int r;
1470 char originbuf[DM_FORMAT_DEV_BUFSIZE], cowbuf[DM_FORMAT_DEV_BUFSIZE];
1471
1472 switch(seg->type) {
1473 case SEG_ERROR:
1474 case SEG_ZERO:
1475 case SEG_LINEAR:
1476 break;
1477 case SEG_MIRRORED:
1478 /* Mirrors are pretty complicated - now in separate function */
1479 r = _mirror_emit_segment_line(dmt, major, minor, seg, seg_start,
1480 params, paramsize);
1481 if (!r)
1482 return_0;
1483 break;
1484 case SEG_SNAPSHOT:
1485 case SEG_SNAPSHOT_MERGE:
1486 if (!_build_dev_string(originbuf, sizeof(originbuf), seg->origin))
1487 return_0;
1488 if (!_build_dev_string(cowbuf, sizeof(cowbuf), seg->cow))
1489 return_0;
1490 EMIT_PARAMS(pos, "%s %s %c %d", originbuf, cowbuf,
1491 seg->persistent ? 'P' : 'N', seg->chunk_size);
1492 break;
1493 case SEG_SNAPSHOT_ORIGIN:
1494 if (!_build_dev_string(originbuf, sizeof(originbuf), seg->origin))
1495 return_0;
1496 EMIT_PARAMS(pos, "%s", originbuf);
1497 break;
1498 case SEG_STRIPED:
1499 EMIT_PARAMS(pos, "%u %u ", seg->area_count, seg->stripe_size);
1500 break;
1501 case SEG_CRYPT:
1502 EMIT_PARAMS(pos, "%s%s%s%s%s %s %" PRIu64 " ", seg->cipher,
1503 seg->chainmode ? "-" : "", seg->chainmode ?: "",
1504 seg->iv ? "-" : "", seg->iv ?: "", seg->key,
1505 seg->iv_offset != DM_CRYPT_IV_DEFAULT ?
1506 seg->iv_offset : *seg_start);
1507 break;
1508 }
1509
1510 switch(seg->type) {
1511 case SEG_ERROR:
1512 case SEG_SNAPSHOT:
1513 case SEG_SNAPSHOT_ORIGIN:
1514 case SEG_SNAPSHOT_MERGE:
1515 case SEG_ZERO:
1516 break;
1517 case SEG_CRYPT:
1518 case SEG_LINEAR:
1519 case SEG_STRIPED:
1520 if ((r = _emit_areas_line(dmt, seg, params, paramsize, &pos)) <= 0) {
1521 stack;
1522 return r;
1523 }
1524 break;
1525 }
1526
1527 log_debug("Adding target to (%" PRIu32 ":%" PRIu32 "): %" PRIu64
1528 " %" PRIu64 " %s %s", major, minor,
1529 *seg_start, seg->size, dm_segtypes[seg->type].target, params);
1530
1531 if (!dm_task_add_target(dmt, *seg_start, seg->size, dm_segtypes[seg->type].target, params))
1532 return_0;
1533
1534 *seg_start += seg->size;
1535
1536 return 1;
1537 }
1538
1539 #undef EMIT_PARAMS
1540
1541 static int _emit_segment(struct dm_task *dmt, uint32_t major, uint32_t minor,
1542 struct load_segment *seg, uint64_t *seg_start)
1543 {
1544 char *params;
1545 size_t paramsize = 4096;
1546 int ret;
1547
1548 do {
1549 if (!(params = dm_malloc(paramsize))) {
1550 log_error("Insufficient space for target parameters.");
1551 return 0;
1552 }
1553
1554 params[0] = '\0';
1555 ret = _emit_segment_line(dmt, major, minor, seg, seg_start,
1556 params, paramsize);
1557 dm_free(params);
1558
1559 if (!ret)
1560 stack;
1561
1562 if (ret >= 0)
1563 return ret;
1564
1565 log_debug("Insufficient space in params[%" PRIsize_t
1566 "] for target parameters.", paramsize);
1567
1568 paramsize *= 2;
1569 } while (paramsize < MAX_TARGET_PARAMSIZE);
1570
1571 log_error("Target parameter size too big. Aborting.");
1572 return 0;
1573 }
1574
1575 static int _load_node(struct dm_tree_node *dnode)
1576 {
1577 int r = 0;
1578 struct dm_task *dmt;
1579 struct load_segment *seg;
1580 uint64_t seg_start = 0;
1581
1582 log_verbose("Loading %s table (%" PRIu32 ":%" PRIu32 ")", dnode->name,
1583 dnode->info.major, dnode->info.minor);
1584
1585 if (!(dmt = dm_task_create(DM_DEVICE_RELOAD))) {
1586 log_error("Reload dm_task creation failed for %s", dnode->name);
1587 return 0;
1588 }
1589
1590 if (!dm_task_set_major(dmt, dnode->info.major) ||
1591 !dm_task_set_minor(dmt, dnode->info.minor)) {
1592 log_error("Failed to set device number for %s reload.", dnode->name);
1593 goto out;
1594 }
1595
1596 if (dnode->props.read_only && !dm_task_set_ro(dmt)) {
1597 log_error("Failed to set read only flag for %s", dnode->name);
1598 goto out;
1599 }
1600
1601 if (!dm_task_no_open_count(dmt))
1602 log_error("Failed to disable open_count");
1603
1604 dm_list_iterate_items(seg, &dnode->props.segs)
1605 if (!_emit_segment(dmt, dnode->info.major, dnode->info.minor,
1606 seg, &seg_start))
1607 goto_out;
1608
1609 if (!dm_task_suppress_identical_reload(dmt))
1610 log_error("Failed to suppress reload of identical tables.");
1611
1612 if ((r = dm_task_run(dmt))) {
1613 r = dm_task_get_info(dmt, &dnode->info);
1614 if (r && !dnode->info.inactive_table)
1615 log_verbose("Suppressed %s identical table reload.",
1616 dnode->name);
1617
1618 if ((dnode->props.size_changed =
1619 (dm_task_get_existing_table_size(dmt) == seg_start) ? 0 : 1))
1620 log_debug("Table size changed from %" PRIu64 " to %"
1621 PRIu64 " for %s",
1622 dm_task_get_existing_table_size(dmt),
1623 seg_start, dnode->name);
1624 }
1625
1626 dnode->props.segment_count = 0;
1627
1628 out:
1629 dm_task_destroy(dmt);
1630
1631 return r;
1632 }
1633
1634 int dm_tree_preload_children(struct dm_tree_node *dnode,
1635 const char *uuid_prefix,
1636 size_t uuid_prefix_len)
1637 {
1638 int r = 1;
1639 void *handle = NULL;
1640 struct dm_tree_node *child;
1641 struct dm_info newinfo;
1642
1643 /* Preload children first */
1644 while ((child = dm_tree_next_child(&handle, dnode, 0))) {
1645 /* Skip existing non-device-mapper devices */
1646 if (!child->info.exists && child->info.major)
1647 continue;
1648
1649 /* Ignore if it doesn't belong to this VG */
1650 if (child->info.exists &&
1651 !_uuid_prefix_matches(child->uuid, uuid_prefix, uuid_prefix_len))
1652 continue;
1653
1654 if (dm_tree_node_num_children(child, 0))
1655 if (!dm_tree_preload_children(child, uuid_prefix, uuid_prefix_len))
1656 return_0;
1657
1658 /* FIXME Cope if name exists with no uuid? */
1659 if (!child->info.exists) {
1660 if (!_create_node(child)) {
1661 stack;
1662 return 0;
1663 }
1664 }
1665
1666 if (!child->info.inactive_table && child->props.segment_count) {
1667 if (!_load_node(child)) {
1668 stack;
1669 return 0;
1670 }
1671 }
1672
1673 /* Propagate device size change change */
1674 if (child->props.size_changed)
1675 dnode->props.size_changed = 1;
1676
1677 /* Resume device immediately if it has parents and its size changed */
1678 if (!dm_tree_node_num_children(child, 1) || !child->props.size_changed)
1679 continue;
1680
1681 if (!child->info.inactive_table && !child->info.suspended)
1682 continue;
1683
1684 if (!_resume_node(child->name, child->info.major, child->info.minor,
1685 child->props.read_ahead, child->props.read_ahead_flags,
1686 &newinfo, &child->dtree->cookie, child->udev_flags)) {
1687 log_error("Unable to resume %s (%" PRIu32
1688 ":%" PRIu32 ")", child->name, child->info.major,
1689 child->info.minor);
1690 r = 0;
1691 continue;
1692 }
1693
1694 /* Update cached info */
1695 child->info = newinfo;
1696 }
1697
1698 handle = NULL;
1699
1700 return r;
1701 }
1702
1703 /*
1704 * Returns 1 if unsure.
1705 */
1706 int dm_tree_children_use_uuid(struct dm_tree_node *dnode,
1707 const char *uuid_prefix,
1708 size_t uuid_prefix_len)
1709 {
1710 void *handle = NULL;
1711 struct dm_tree_node *child = dnode;
1712 const char *uuid;
1713
1714 while ((child = dm_tree_next_child(&handle, dnode, 0))) {
1715 if (!(uuid = dm_tree_node_get_uuid(child))) {
1716 log_error("Failed to get uuid for dtree node.");
1717 return 1;
1718 }
1719
1720 if (_uuid_prefix_matches(uuid, uuid_prefix, uuid_prefix_len))
1721 return 1;
1722
1723 if (dm_tree_node_num_children(child, 0))
1724 dm_tree_children_use_uuid(child, uuid_prefix, uuid_prefix_len);
1725 }
1726
1727 return 0;
1728 }
1729
1730 /*
1731 * Target functions
1732 */
1733 static struct load_segment *_add_segment(struct dm_tree_node *dnode, unsigned type, uint64_t size)
1734 {
1735 struct load_segment *seg;
1736
1737 if (!(seg = dm_pool_zalloc(dnode->dtree->mem, sizeof(*seg)))) {
1738 log_error("dtree node segment allocation failed");
1739 return NULL;
1740 }
1741
1742 seg->type = type;
1743 seg->size = size;
1744 seg->area_count = 0;
1745 dm_list_init(&seg->areas);
1746 seg->stripe_size = 0;
1747 seg->persistent = 0;
1748 seg->chunk_size = 0;
1749 seg->cow = NULL;
1750 seg->origin = NULL;
1751 seg->merge = NULL;
1752
1753 dm_list_add(&dnode->props.segs, &seg->list);
1754 dnode->props.segment_count++;
1755
1756 return seg;
1757 }
1758
1759 int dm_tree_node_add_snapshot_origin_target(struct dm_tree_node *dnode,
1760 uint64_t size,
1761 const char *origin_uuid)
1762 {
1763 struct load_segment *seg;
1764 struct dm_tree_node *origin_node;
1765
1766 if (!(seg = _add_segment(dnode, SEG_SNAPSHOT_ORIGIN, size)))
1767 return_0;
1768
1769 if (!(origin_node = dm_tree_find_node_by_uuid(dnode->dtree, origin_uuid))) {
1770 log_error("Couldn't find snapshot origin uuid %s.", origin_uuid);
1771 return 0;
1772 }
1773
1774 seg->origin = origin_node;
1775 if (!_link_tree_nodes(dnode, origin_node))
1776 return_0;
1777
1778 /* Resume snapshot origins after new snapshots */
1779 dnode->activation_priority = 1;
1780
1781 return 1;
1782 }
1783
1784 static int _add_snapshot_target(struct dm_tree_node *node,
1785 uint64_t size,
1786 const char *origin_uuid,
1787 const char *cow_uuid,
1788 const char *merge_uuid,
1789 int persistent,
1790 uint32_t chunk_size)
1791 {
1792 struct load_segment *seg;
1793 struct dm_tree_node *origin_node, *cow_node, *merge_node;
1794 unsigned seg_type;
1795
1796 seg_type = !merge_uuid ? SEG_SNAPSHOT : SEG_SNAPSHOT_MERGE;
1797
1798 if (!(seg = _add_segment(node, seg_type, size)))
1799 return_0;
1800
1801 if (!(origin_node = dm_tree_find_node_by_uuid(node->dtree, origin_uuid))) {
1802 log_error("Couldn't find snapshot origin uuid %s.", origin_uuid);
1803 return 0;
1804 }
1805
1806 seg->origin = origin_node;
1807 if (!_link_tree_nodes(node, origin_node))
1808 return_0;
1809
1810 if (!(cow_node = dm_tree_find_node_by_uuid(node->dtree, cow_uuid))) {
1811 log_error("Couldn't find snapshot COW device uuid %s.", cow_uuid);
1812 return 0;
1813 }
1814
1815 seg->cow = cow_node;
1816 if (!_link_tree_nodes(node, cow_node))
1817 return_0;
1818
1819 seg->persistent = persistent ? 1 : 0;
1820 seg->chunk_size = chunk_size;
1821
1822 if (merge_uuid) {
1823 if (!(merge_node = dm_tree_find_node_by_uuid(node->dtree, merge_uuid))) {
1824 /* not a pure error, merging snapshot may have been deactivated */
1825 log_verbose("Couldn't find merging snapshot uuid %s.", merge_uuid);
1826 } else {
1827 seg->merge = merge_node;
1828 /* must not link merging snapshot, would undermine activation_priority below */
1829 }
1830
1831 /* Resume snapshot-merge (acting origin) after other snapshots */
1832 node->activation_priority = 1;
1833 if (seg->merge) {
1834 /* Resume merging snapshot after snapshot-merge */
1835 seg->merge->activation_priority = 2;
1836 }
1837 }
1838
1839 return 1;
1840 }
1841
1842
1843 int dm_tree_node_add_snapshot_target(struct dm_tree_node *node,
1844 uint64_t size,
1845 const char *origin_uuid,
1846 const char *cow_uuid,
1847 int persistent,
1848 uint32_t chunk_size)
1849 {
1850 return _add_snapshot_target(node, size, origin_uuid, cow_uuid,
1851 NULL, persistent, chunk_size);
1852 }
1853
1854 int dm_tree_node_add_snapshot_merge_target(struct dm_tree_node *node,
1855 uint64_t size,
1856 const char *origin_uuid,
1857 const char *cow_uuid,
1858 const char *merge_uuid,
1859 uint32_t chunk_size)
1860 {
1861 return _add_snapshot_target(node, size, origin_uuid, cow_uuid,
1862 merge_uuid, 1, chunk_size);
1863 }
1864
1865 int dm_tree_node_add_error_target(struct dm_tree_node *node,
1866 uint64_t size)
1867 {
1868 if (!_add_segment(node, SEG_ERROR, size))
1869 return_0;
1870
1871 return 1;
1872 }
1873
1874 int dm_tree_node_add_zero_target(struct dm_tree_node *node,
1875 uint64_t size)
1876 {
1877 if (!_add_segment(node, SEG_ZERO, size))
1878 return_0;
1879
1880 return 1;
1881 }
1882
1883 int dm_tree_node_add_linear_target(struct dm_tree_node *node,
1884 uint64_t size)
1885 {
1886 if (!_add_segment(node, SEG_LINEAR, size))
1887 return_0;
1888
1889 return 1;
1890 }
1891
1892 int dm_tree_node_add_striped_target(struct dm_tree_node *node,
1893 uint64_t size,
1894 uint32_t stripe_size)
1895 {
1896 struct load_segment *seg;
1897
1898 if (!(seg = _add_segment(node, SEG_STRIPED, size)))
1899 return_0;
1900
1901 seg->stripe_size = stripe_size;
1902
1903 return 1;
1904 }
1905
1906 int dm_tree_node_add_crypt_target(struct dm_tree_node *node,
1907 uint64_t size,
1908 const char *cipher,
1909 const char *chainmode,
1910 const char *iv,
1911 uint64_t iv_offset,
1912 const char *key)
1913 {
1914 struct load_segment *seg;
1915
1916 if (!(seg = _add_segment(node, SEG_CRYPT, size)))
1917 return_0;
1918
1919 seg->cipher = cipher;
1920 seg->chainmode = chainmode;
1921 seg->iv = iv;
1922 seg->iv_offset = iv_offset;
1923 seg->key = key;
1924
1925 return 1;
1926 }
1927
1928 int dm_tree_node_add_mirror_target_log(struct dm_tree_node *node,
1929 uint32_t region_size,
1930 unsigned clustered,
1931 const char *log_uuid,
1932 unsigned area_count,
1933 uint32_t flags)
1934 {
1935 struct dm_tree_node *log_node = NULL;
1936 struct load_segment *seg;
1937
1938 if (!node->props.segment_count) {
1939 log_error(INTERNAL_ERROR "Attempt to add target area to missing segment.");
1940 return 0;
1941 }
1942
1943 seg = dm_list_item(dm_list_last(&node->props.segs), struct load_segment);
1944
1945 if (log_uuid) {
1946 if (!(seg->uuid = dm_pool_strdup(node->dtree->mem, log_uuid))) {
1947 log_error("log uuid pool_strdup failed");
1948 return 0;
1949 }
1950 if (!(flags & DM_CORELOG)) {
1951 if (!(log_node = dm_tree_find_node_by_uuid(node->dtree, log_uuid))) {
1952 log_error("Couldn't find mirror log uuid %s.", log_uuid);
1953 return 0;
1954 }
1955
1956 if (!_link_tree_nodes(node, log_node))
1957 return_0;
1958 }
1959 }
1960
1961 seg->log = log_node;
1962 seg->region_size = region_size;
1963 seg->clustered = clustered;
1964 seg->mirror_area_count = area_count;
1965 seg->flags = flags;
1966
1967 return 1;
1968 }
1969
1970 int dm_tree_node_add_mirror_target(struct dm_tree_node *node,
1971 uint64_t size)
1972 {
1973 struct load_segment *seg;
1974
1975 if (!(seg = _add_segment(node, SEG_MIRRORED, size)))
1976 return_0;
1977
1978 return 1;
1979 }
1980
1981 static int _add_area(struct dm_tree_node *node, struct load_segment *seg, struct dm_tree_node *dev_node, uint64_t offset)
1982 {
1983 struct seg_area *area;
1984
1985 if (!(area = dm_pool_zalloc(node->dtree->mem, sizeof (*area)))) {
1986 log_error("Failed to allocate target segment area.");
1987 return 0;
1988 }
1989
1990 area->dev_node = dev_node;
1991 area->offset = offset;
1992
1993 dm_list_add(&seg->areas, &area->list);
1994 seg->area_count++;
1995
1996 return 1;
1997 }
1998
1999 int dm_tree_node_add_target_area(struct dm_tree_node *node,
2000 const char *dev_name,
2001 const char *uuid,
2002 uint64_t offset)
2003 {
2004 struct load_segment *seg;
2005 struct stat info;
2006 struct dm_tree_node *dev_node;
2007
2008 if ((!dev_name || !*dev_name) && (!uuid || !*uuid)) {
2009 log_error("dm_tree_node_add_target_area called without device");
2010 return 0;
2011 }
2012
2013 if (uuid) {
2014 if (!(dev_node = dm_tree_find_node_by_uuid(node->dtree, uuid))) {
2015 log_error("Couldn't find area uuid %s.", uuid);
2016 return 0;
2017 }
2018 if (!_link_tree_nodes(node, dev_node))
2019 return_0;
2020 } else {
2021 if (stat(dev_name, &info) < 0) {
2022 log_error("Device %s not found.", dev_name);
2023 return 0;
2024 }
2025
2026 if (!S_ISBLK(info.st_mode)) {
2027 log_error("Device %s is not a block device.", dev_name);
2028 return 0;
2029 }
2030
2031 /* FIXME Check correct macro use */
2032 if (!(dev_node = _add_dev(node->dtree, node, MAJOR(info.st_rdev),
2033 MINOR(info.st_rdev), 0)))
2034 return_0;
2035 }
2036
2037 if (!node->props.segment_count) {
2038 log_error(INTERNAL_ERROR "Attempt to add target area to missing segment.");
2039 return 0;
2040 }
2041
2042 seg = dm_list_item(dm_list_last(&node->props.segs), struct load_segment);
2043
2044 if (!_add_area(node, seg, dev_node, offset))
2045 return_0;
2046
2047 return 1;
2048 }
2049
2050 void dm_tree_set_cookie(struct dm_tree_node *node, uint32_t cookie)
2051 {
2052 node->dtree->cookie = cookie;
2053 }
2054
2055 uint32_t dm_tree_get_cookie(struct dm_tree_node *node)
2056 {
2057 return node->dtree->cookie;
2058 }
This page took 0.127166 seconds and 6 git commands to generate.