1 /* $NetBSD: libdm-deptree.c,v 1.4 2009/12/02 00:58:03 haad Exp $ */
4 * Copyright (C) 2005-2007 Red Hat, Inc. All rights reserved.
6 * This file is part of the device-mapper userspace tools.
8 * This copyrighted material is made available to anyone wishing to use,
9 * modify, copy, or redistribute it subject to the terms and conditions
10 * of the GNU Lesser General Public License v.2.1.
12 * You should have received a copy of the GNU Lesser General Public License
13 * along with this program; if not, write to the Free Software Foundation,
14 * Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18 #include "libdm-targets.h"
19 #include "libdm-common.h"
24 #include <sys/param.h>
25 #include <sys/utsname.h>
27 #define MAX_TARGET_PARAMSIZE 500000
29 /* FIXME Fix interface so this is used only by LVM */
30 #define UUID_PREFIX "LVM-"
32 /* Supported segment types */
44 /* FIXME Add crypt and multipath support */
50 { SEG_CRYPT, "crypt" },
51 { SEG_ERROR, "error" },
52 { SEG_LINEAR, "linear" },
53 { SEG_MIRRORED, "mirror" },
54 { SEG_SNAPSHOT, "snapshot" },
55 { SEG_SNAPSHOT_ORIGIN, "snapshot-origin" },
56 { SEG_STRIPED, "striped" },
60 /* Some segment types have a list of areas of other devices attached */
64 struct dm_tree_node *dev_node;
69 /* Per-segment properties */
77 unsigned area_count; /* Linear + Striped + Mirrored + Crypt */
78 struct dm_list areas; /* Linear + Striped + Mirrored + Crypt */
80 uint32_t stripe_size; /* Striped */
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 */
87 struct dm_tree_node *log; /* Mirror */
88 uint32_t region_size; /* Mirror */
89 unsigned clustered; /* Mirror */
90 unsigned mirror_area_count; /* Mirror */
91 uint32_t flags; /* Mirror log */
92 char *uuid; /* Clustered mirror log */
94 const char *cipher; /* Crypt */
95 const char *chainmode; /* Crypt */
96 const char *iv; /* Crypt */
97 uint64_t iv_offset; /* Crypt */
98 const char *key; /* Crypt */
101 /* Per-device properties */
102 struct load_properties {
108 uint32_t read_ahead_flags;
110 unsigned segment_count;
111 unsigned size_changed;
114 const char *new_name;
117 /* Two of these used to join two nodes with uses and used_by. */
118 struct dm_tree_link {
120 struct dm_tree_node *node;
123 struct dm_tree_node {
124 struct dm_tree *dtree;
130 struct dm_list uses; /* Nodes this node uses */
131 struct dm_list used_by; /* Nodes that use this node */
133 int activation_priority; /* 0 gets activated first */
135 uint16_t udev_flags; /* Udev control flags */
137 void *context; /* External supplied context */
139 struct load_properties props; /* For creation/table (re)load */
144 struct dm_hash_table *devs;
145 struct dm_hash_table *uuids;
146 struct dm_tree_node root;
147 int skip_lockfs; /* 1 skips lockfs (for non-snapshots) */
148 int no_flush; /* 1 sets noflush (mirrors/multipath) */
152 struct dm_tree *dm_tree_create(void)
154 struct dm_tree *dtree;
156 if (!(dtree = dm_malloc(sizeof(*dtree)))) {
157 log_error("dm_tree_create malloc failed");
161 memset(dtree, 0, sizeof(*dtree));
162 dtree->root.dtree = dtree;
163 dm_list_init(&dtree->root.uses);
164 dm_list_init(&dtree->root.used_by);
165 dtree->skip_lockfs = 0;
168 if (!(dtree->mem = dm_pool_create("dtree", 1024))) {
169 log_error("dtree pool creation failed");
174 if (!(dtree->devs = dm_hash_create(8))) {
175 log_error("dtree hash creation failed");
176 dm_pool_destroy(dtree->mem);
181 if (!(dtree->uuids = dm_hash_create(32))) {
182 log_error("dtree uuid hash creation failed");
183 dm_hash_destroy(dtree->devs);
184 dm_pool_destroy(dtree->mem);
192 void dm_tree_free(struct dm_tree *dtree)
197 dm_hash_destroy(dtree->uuids);
198 dm_hash_destroy(dtree->devs);
199 dm_pool_destroy(dtree->mem);
203 static int _nodes_are_linked(struct dm_tree_node *parent,
204 struct dm_tree_node *child)
206 struct dm_tree_link *dlink;
208 dm_list_iterate_items(dlink, &parent->uses)
209 if (dlink->node == child)
215 static int _link(struct dm_list *list, struct dm_tree_node *node)
217 struct dm_tree_link *dlink;
219 if (!(dlink = dm_pool_alloc(node->dtree->mem, sizeof(*dlink)))) {
220 log_error("dtree link allocation failed");
225 dm_list_add(list, &dlink->list);
230 static int _link_nodes(struct dm_tree_node *parent,
231 struct dm_tree_node *child)
233 if (_nodes_are_linked(parent, child))
236 if (!_link(&parent->uses, child))
239 if (!_link(&child->used_by, parent))
245 static void _unlink(struct dm_list *list, struct dm_tree_node *node)
247 struct dm_tree_link *dlink;
249 dm_list_iterate_items(dlink, list)
250 if (dlink->node == node) {
251 dm_list_del(&dlink->list);
256 static void _unlink_nodes(struct dm_tree_node *parent,
257 struct dm_tree_node *child)
259 if (!_nodes_are_linked(parent, child))
262 _unlink(&parent->uses, child);
263 _unlink(&child->used_by, parent);
266 static int _add_to_toplevel(struct dm_tree_node *node)
268 return _link_nodes(&node->dtree->root, node);
271 static void _remove_from_toplevel(struct dm_tree_node *node)
273 return _unlink_nodes(&node->dtree->root, node);
276 static int _add_to_bottomlevel(struct dm_tree_node *node)
278 return _link_nodes(node, &node->dtree->root);
281 static void _remove_from_bottomlevel(struct dm_tree_node *node)
283 return _unlink_nodes(node, &node->dtree->root);
286 static int _link_tree_nodes(struct dm_tree_node *parent, struct dm_tree_node *child)
288 /* Don't link to root node if child already has a parent */
289 if ((parent == &parent->dtree->root)) {
290 if (dm_tree_node_num_children(child, 1))
293 _remove_from_toplevel(child);
295 if ((child == &child->dtree->root)) {
296 if (dm_tree_node_num_children(parent, 0))
299 _remove_from_bottomlevel(parent);
301 return _link_nodes(parent, child);
304 static struct dm_tree_node *_create_dm_tree_node(struct dm_tree *dtree,
307 struct dm_info *info,
311 struct dm_tree_node *node;
314 if (!(node = dm_pool_zalloc(dtree->mem, sizeof(*node)))) {
315 log_error("_create_dm_tree_node alloc failed");
324 node->context = context;
325 node->udev_flags = udev_flags;
326 node->activation_priority = 0;
328 dm_list_init(&node->uses);
329 dm_list_init(&node->used_by);
330 dm_list_init(&node->props.segs);
332 dev = MKDEV(info->major, info->minor);
334 if (!dm_hash_insert_binary(dtree->devs, (const char *) &dev,
335 sizeof(dev), node)) {
336 log_error("dtree node hash insertion failed");
337 dm_pool_free(dtree->mem, node);
342 !dm_hash_insert(dtree->uuids, uuid, node)) {
343 log_error("dtree uuid hash insertion failed");
344 dm_hash_remove_binary(dtree->devs, (const char *) &dev,
346 dm_pool_free(dtree->mem, node);
353 static struct dm_tree_node *_find_dm_tree_node(struct dm_tree *dtree,
354 uint32_t major, uint32_t minor)
356 uint64_t dev = MKDEV(major, minor);
358 return dm_hash_lookup_binary(dtree->devs, (const char *) &dev,
362 static struct dm_tree_node *_find_dm_tree_node_by_uuid(struct dm_tree *dtree,
365 struct dm_tree_node *node;
367 if ((node = dm_hash_lookup(dtree->uuids, uuid)))
370 if (strncmp(uuid, UUID_PREFIX, sizeof(UUID_PREFIX) - 1))
373 return dm_hash_lookup(dtree->uuids, uuid + sizeof(UUID_PREFIX) - 1);
376 static int _deps(struct dm_task **dmt, struct dm_pool *mem, uint32_t major, uint32_t minor,
377 const char **name, const char **uuid,
378 struct dm_info *info, struct dm_deps **deps)
380 memset(info, 0, sizeof(*info));
382 if (!dm_is_dm_major(major)) {
389 info->live_table = 0;
390 info->inactive_table = 0;
395 if (!(*dmt = dm_task_create(DM_DEVICE_DEPS))) {
396 log_error("deps dm_task creation failed");
400 if (!dm_task_set_major(*dmt, major)) {
401 log_error("_deps: failed to set major for (%" PRIu32 ":%" PRIu32 ")",
406 if (!dm_task_set_minor(*dmt, minor)) {
407 log_error("_deps: failed to set minor for (%" PRIu32 ":%" PRIu32 ")",
412 if (!dm_task_run(*dmt)) {
413 log_error("_deps: task run failed for (%" PRIu32 ":%" PRIu32 ")",
418 if (!dm_task_get_info(*dmt, info)) {
419 log_error("_deps: failed to get info for (%" PRIu32 ":%" PRIu32 ")",
429 if (info->major != major) {
430 log_error("Inconsistent dtree major number: %u != %u",
434 if (info->minor != minor) {
435 log_error("Inconsistent dtree minor number: %u != %u",
439 if (!(*name = dm_pool_strdup(mem, dm_task_get_name(*dmt)))) {
440 log_error("name pool_strdup failed");
443 if (!(*uuid = dm_pool_strdup(mem, dm_task_get_uuid(*dmt)))) {
444 log_error("uuid pool_strdup failed");
447 *deps = dm_task_get_deps(*dmt);
453 dm_task_destroy(*dmt);
457 static struct dm_tree_node *_add_dev(struct dm_tree *dtree,
458 struct dm_tree_node *parent,
459 uint32_t major, uint32_t minor)
461 struct dm_task *dmt = NULL;
463 struct dm_deps *deps = NULL;
464 const char *name = NULL;
465 const char *uuid = NULL;
466 struct dm_tree_node *node = NULL;
470 /* Already in tree? */
471 if (!(node = _find_dm_tree_node(dtree, major, minor))) {
472 if (!_deps(&dmt, dtree->mem, major, minor, &name, &uuid, &info, &deps))
475 if (!(node = _create_dm_tree_node(dtree, name, uuid, &info,
481 if (!_link_tree_nodes(parent, node)) {
486 /* If node was already in tree, no need to recurse. */
490 /* Can't recurse if not a mapped device or there are no dependencies */
491 if (!node->info.exists || !deps->count) {
492 if (!_add_to_bottomlevel(node)) {
499 /* Add dependencies to tree */
500 for (i = 0; i < deps->count; i++)
501 if (!_add_dev(dtree, node, MAJOR(deps->device[i]),
502 MINOR(deps->device[i]))) {
509 dm_task_destroy(dmt);
514 static int _node_clear_table(struct dm_tree_node *dnode)
517 struct dm_info *info;
521 if (!(info = &dnode->info)) {
522 log_error("_node_clear_table failed: missing info");
526 if (!(name = dm_tree_node_get_name(dnode))) {
527 log_error("_node_clear_table failed: missing name");
531 /* Is there a table? */
532 if (!info->exists || !info->inactive_table)
535 log_verbose("Clearing inactive table %s (%" PRIu32 ":%" PRIu32 ")",
536 name, info->major, info->minor);
538 if (!(dmt = dm_task_create(DM_DEVICE_CLEAR))) {
539 dm_task_destroy(dmt);
540 log_error("Table clear dm_task creation failed for %s", name);
544 if (!dm_task_set_major(dmt, info->major) ||
545 !dm_task_set_minor(dmt, info->minor)) {
546 log_error("Failed to set device number for %s table clear", name);
547 dm_task_destroy(dmt);
551 r = dm_task_run(dmt);
553 if (!dm_task_get_info(dmt, info)) {
554 log_error("_node_clear_table failed: info missing after running task for %s", name);
558 dm_task_destroy(dmt);
563 struct dm_tree_node *dm_tree_add_new_dev(struct dm_tree *dtree,
566 uint32_t major, uint32_t minor,
571 struct dm_tree_node *dnode;
576 /* Do we need to add node to tree? */
577 if (!(dnode = dm_tree_find_node_by_uuid(dtree, uuid))) {
578 if (!(name2 = dm_pool_strdup(dtree->mem, name))) {
579 log_error("name pool_strdup failed");
582 if (!(uuid2 = dm_pool_strdup(dtree->mem, uuid))) {
583 log_error("uuid pool_strdup failed");
591 info.inactive_table = 0;
594 if (!(dnode = _create_dm_tree_node(dtree, name2, uuid2, &info,
598 /* Attach to root node until a table is supplied */
599 if (!_add_to_toplevel(dnode) || !_add_to_bottomlevel(dnode))
602 dnode->props.major = major;
603 dnode->props.minor = minor;
604 dnode->props.new_name = NULL;
605 dnode->props.size_changed = 0;
606 } else if (strcmp(name, dnode->name)) {
607 /* Do we need to rename node? */
608 if (!(dnode->props.new_name = dm_pool_strdup(dtree->mem, name))) {
609 log_error("name pool_strdup failed");
614 dnode->props.read_only = read_only ? 1 : 0;
615 dnode->props.read_ahead = DM_READ_AHEAD_AUTO;
616 dnode->props.read_ahead_flags = 0;
618 if (clear_inactive && !_node_clear_table(dnode))
621 dnode->context = context;
622 dnode->udev_flags = 0;
627 struct dm_tree_node *dm_tree_add_new_dev_with_udev_flags(struct dm_tree *dtree,
637 struct dm_tree_node *node;
639 if ((node = dm_tree_add_new_dev(dtree, name, uuid, major, minor, read_only,
640 clear_inactive, context)))
641 node->udev_flags = udev_flags;
647 void dm_tree_node_set_read_ahead(struct dm_tree_node *dnode,
649 uint32_t read_ahead_flags)
651 dnode->props.read_ahead = read_ahead;
652 dnode->props.read_ahead_flags = read_ahead_flags;
655 int dm_tree_add_dev(struct dm_tree *dtree, uint32_t major, uint32_t minor)
657 return _add_dev(dtree, &dtree->root, major, minor) ? 1 : 0;
660 const char *dm_tree_node_get_name(struct dm_tree_node *node)
662 return node->info.exists ? node->name : "";
665 const char *dm_tree_node_get_uuid(struct dm_tree_node *node)
667 return node->info.exists ? node->uuid : "";
670 const struct dm_info *dm_tree_node_get_info(struct dm_tree_node *node)
675 void *dm_tree_node_get_context(struct dm_tree_node *node)
677 return node->context;
680 int dm_tree_node_size_changed(struct dm_tree_node *dnode)
682 return dnode->props.size_changed;
685 int dm_tree_node_num_children(struct dm_tree_node *node, uint32_t inverted)
688 if (_nodes_are_linked(&node->dtree->root, node))
690 return dm_list_size(&node->used_by);
693 if (_nodes_are_linked(node, &node->dtree->root))
696 return dm_list_size(&node->uses);
700 * Returns 1 if no prefix supplied
702 static int _uuid_prefix_matches(const char *uuid, const char *uuid_prefix, size_t uuid_prefix_len)
707 if (!strncmp(uuid, uuid_prefix, uuid_prefix_len))
710 /* Handle transition: active device uuids might be missing the prefix */
711 if (uuid_prefix_len <= 4)
714 if (!strncmp(uuid, UUID_PREFIX, sizeof(UUID_PREFIX) - 1))
717 if (strncmp(uuid_prefix, UUID_PREFIX, sizeof(UUID_PREFIX) - 1))
720 if (!strncmp(uuid, uuid_prefix + sizeof(UUID_PREFIX) - 1, uuid_prefix_len - (sizeof(UUID_PREFIX) - 1)))
727 * Returns 1 if no children.
729 static int _children_suspended(struct dm_tree_node *node,
731 const char *uuid_prefix,
732 size_t uuid_prefix_len)
734 struct dm_list *list;
735 struct dm_tree_link *dlink;
736 const struct dm_info *dinfo;
740 if (_nodes_are_linked(&node->dtree->root, node))
742 list = &node->used_by;
744 if (_nodes_are_linked(node, &node->dtree->root))
749 dm_list_iterate_items(dlink, list) {
750 if (!(uuid = dm_tree_node_get_uuid(dlink->node))) {
755 /* Ignore if it doesn't belong to this VG */
756 if (!_uuid_prefix_matches(uuid, uuid_prefix, uuid_prefix_len))
759 if (!(dinfo = dm_tree_node_get_info(dlink->node))) {
760 stack; /* FIXME Is this normal? */
764 if (!dinfo->suspended)
772 * Set major and minor to zero for root of tree.
774 struct dm_tree_node *dm_tree_find_node(struct dm_tree *dtree,
778 if (!major && !minor)
781 return _find_dm_tree_node(dtree, major, minor);
785 * Set uuid to NULL for root of tree.
787 struct dm_tree_node *dm_tree_find_node_by_uuid(struct dm_tree *dtree,
793 return _find_dm_tree_node_by_uuid(dtree, uuid);
797 * First time set *handle to NULL.
798 * Set inverted to invert the tree.
800 struct dm_tree_node *dm_tree_next_child(void **handle,
801 struct dm_tree_node *parent,
804 struct dm_list **dlink = (struct dm_list **) handle;
805 struct dm_list *use_list;
808 use_list = &parent->used_by;
810 use_list = &parent->uses;
813 *dlink = dm_list_first(use_list);
815 *dlink = dm_list_next(use_list, *dlink);
817 return (*dlink) ? dm_list_item(*dlink, struct dm_tree_link)->node : NULL;
821 * Deactivate a device with its dependencies if the uuid prefix matches.
823 static int _info_by_dev(uint32_t major, uint32_t minor, int with_open_count,
824 struct dm_info *info)
829 if (!(dmt = dm_task_create(DM_DEVICE_INFO))) {
830 log_error("_info_by_dev: dm_task creation failed");
834 if (!dm_task_set_major(dmt, major) || !dm_task_set_minor(dmt, minor)) {
835 log_error("_info_by_dev: Failed to set device number");
836 dm_task_destroy(dmt);
840 if (!with_open_count && !dm_task_no_open_count(dmt))
841 log_error("Failed to disable open_count");
843 if ((r = dm_task_run(dmt)))
844 r = dm_task_get_info(dmt, info);
846 dm_task_destroy(dmt);
851 static int _deactivate_node(const char *name, uint32_t major, uint32_t minor,
852 uint32_t *cookie, uint16_t udev_flags)
857 log_verbose("Removing %s (%" PRIu32 ":%" PRIu32 ")", name, major, minor);
859 if (!(dmt = dm_task_create(DM_DEVICE_REMOVE))) {
860 log_error("Deactivation dm_task creation failed for %s", name);
864 if (!dm_task_set_major(dmt, major) || !dm_task_set_minor(dmt, minor)) {
865 log_error("Failed to set device number for %s deactivation", name);
869 if (!dm_task_no_open_count(dmt))
870 log_error("Failed to disable open_count");
872 if (!dm_task_set_cookie(dmt, cookie, udev_flags))
875 r = dm_task_run(dmt);
877 /* FIXME Until kernel returns actual name so dm-ioctl.c can handle it */
878 rm_dev_node(name, dmt->cookie_set);
880 /* FIXME Remove node from tree or mark invalid? */
883 dm_task_destroy(dmt);
888 static int _rename_node(const char *old_name, const char *new_name, uint32_t major,
889 uint32_t minor, uint32_t *cookie, uint16_t udev_flags)
894 log_verbose("Renaming %s (%" PRIu32 ":%" PRIu32 ") to %s", old_name, major, minor, new_name);
896 if (!(dmt = dm_task_create(DM_DEVICE_RENAME))) {
897 log_error("Rename dm_task creation failed for %s", old_name);
901 if (!dm_task_set_name(dmt, old_name)) {
902 log_error("Failed to set name for %s rename.", old_name);
906 if (!dm_task_set_newname(dmt, new_name))
909 if (!dm_task_no_open_count(dmt))
910 log_error("Failed to disable open_count");
912 if (!dm_task_set_cookie(dmt, cookie, udev_flags))
915 r = dm_task_run(dmt);
918 dm_task_destroy(dmt);
923 /* FIXME Merge with _suspend_node? */
924 static int _resume_node(const char *name, uint32_t major, uint32_t minor,
925 uint32_t read_ahead, uint32_t read_ahead_flags,
926 struct dm_info *newinfo, uint32_t *cookie,
932 log_verbose("Resuming %s (%" PRIu32 ":%" PRIu32 ")", name, major, minor);
934 if (!(dmt = dm_task_create(DM_DEVICE_RESUME))) {
935 log_error("Suspend dm_task creation failed for %s", name);
939 /* FIXME Kernel should fill in name on return instead */
940 if (!dm_task_set_name(dmt, name)) {
941 log_error("Failed to set readahead device name for %s", name);
945 if (!dm_task_set_major(dmt, major) || !dm_task_set_minor(dmt, minor)) {
946 log_error("Failed to set device number for %s resumption.", name);
950 if (!dm_task_no_open_count(dmt))
951 log_error("Failed to disable open_count");
953 if (!dm_task_set_read_ahead(dmt, read_ahead, read_ahead_flags))
954 log_error("Failed to set read ahead");
956 if (!dm_task_set_cookie(dmt, cookie, udev_flags))
959 if ((r = dm_task_run(dmt)))
960 r = dm_task_get_info(dmt, newinfo);
963 dm_task_destroy(dmt);
968 static int _suspend_node(const char *name, uint32_t major, uint32_t minor,
969 int skip_lockfs, int no_flush, struct dm_info *newinfo)
974 log_verbose("Suspending %s (%" PRIu32 ":%" PRIu32 ")%s%s",
976 skip_lockfs ? "" : " with filesystem sync",
977 no_flush ? "" : " with device flush");
979 if (!(dmt = dm_task_create(DM_DEVICE_SUSPEND))) {
980 log_error("Suspend dm_task creation failed for %s", name);
984 if (!dm_task_set_major(dmt, major) || !dm_task_set_minor(dmt, minor)) {
985 log_error("Failed to set device number for %s suspension.", name);
986 dm_task_destroy(dmt);
990 if (!dm_task_no_open_count(dmt))
991 log_error("Failed to disable open_count");
993 if (skip_lockfs && !dm_task_skip_lockfs(dmt))
994 log_error("Failed to set skip_lockfs flag.");
996 if (no_flush && !dm_task_no_flush(dmt))
997 log_error("Failed to set no_flush flag.");
999 if ((r = dm_task_run(dmt)))
1000 r = dm_task_get_info(dmt, newinfo);
1002 dm_task_destroy(dmt);
1007 int dm_tree_deactivate_children(struct dm_tree_node *dnode,
1008 const char *uuid_prefix,
1009 size_t uuid_prefix_len)
1011 void *handle = NULL;
1012 struct dm_tree_node *child = dnode;
1013 struct dm_info info;
1014 const struct dm_info *dinfo;
1018 while ((child = dm_tree_next_child(&handle, dnode, 0))) {
1019 if (!(dinfo = dm_tree_node_get_info(child))) {
1024 if (!(name = dm_tree_node_get_name(child))) {
1029 if (!(uuid = dm_tree_node_get_uuid(child))) {
1034 /* Ignore if it doesn't belong to this VG */
1035 if (!_uuid_prefix_matches(uuid, uuid_prefix, uuid_prefix_len))
1038 /* Refresh open_count */
1039 if (!_info_by_dev(dinfo->major, dinfo->minor, 1, &info) ||
1040 !info.exists || info.open_count)
1043 if (!_deactivate_node(name, info.major, info.minor,
1044 &child->dtree->cookie, child->udev_flags)) {
1045 log_error("Unable to deactivate %s (%" PRIu32
1046 ":%" PRIu32 ")", name, info.major,
1051 if (dm_tree_node_num_children(child, 0))
1052 dm_tree_deactivate_children(child, uuid_prefix, uuid_prefix_len);
1058 void dm_tree_skip_lockfs(struct dm_tree_node *dnode)
1060 dnode->dtree->skip_lockfs = 1;
1063 void dm_tree_use_no_flush_suspend(struct dm_tree_node *dnode)
1065 dnode->dtree->no_flush = 1;
1068 int dm_tree_suspend_children(struct dm_tree_node *dnode,
1069 const char *uuid_prefix,
1070 size_t uuid_prefix_len)
1072 void *handle = NULL;
1073 struct dm_tree_node *child = dnode;
1074 struct dm_info info, newinfo;
1075 const struct dm_info *dinfo;
1079 /* Suspend nodes at this level of the tree */
1080 while ((child = dm_tree_next_child(&handle, dnode, 0))) {
1081 if (!(dinfo = dm_tree_node_get_info(child))) {
1086 if (!(name = dm_tree_node_get_name(child))) {
1091 if (!(uuid = dm_tree_node_get_uuid(child))) {
1096 /* Ignore if it doesn't belong to this VG */
1097 if (!_uuid_prefix_matches(uuid, uuid_prefix, uuid_prefix_len))
1100 /* Ensure immediate parents are already suspended */
1101 if (!_children_suspended(child, 1, uuid_prefix, uuid_prefix_len))
1104 if (!_info_by_dev(dinfo->major, dinfo->minor, 0, &info) ||
1105 !info.exists || info.suspended)
1108 if (!_suspend_node(name, info.major, info.minor,
1109 child->dtree->skip_lockfs,
1110 child->dtree->no_flush, &newinfo)) {
1111 log_error("Unable to suspend %s (%" PRIu32
1112 ":%" PRIu32 ")", name, info.major,
1117 /* Update cached info */
1118 child->info = newinfo;
1121 /* Then suspend any child nodes */
1124 while ((child = dm_tree_next_child(&handle, dnode, 0))) {
1125 if (!(uuid = dm_tree_node_get_uuid(child))) {
1130 /* Ignore if it doesn't belong to this VG */
1131 if (!_uuid_prefix_matches(uuid, uuid_prefix, uuid_prefix_len))
1134 if (dm_tree_node_num_children(child, 0))
1135 dm_tree_suspend_children(child, uuid_prefix, uuid_prefix_len);
1141 int dm_tree_activate_children(struct dm_tree_node *dnode,
1142 const char *uuid_prefix,
1143 size_t uuid_prefix_len)
1145 void *handle = NULL;
1146 struct dm_tree_node *child = dnode;
1147 struct dm_info newinfo;
1152 /* Activate children first */
1153 while ((child = dm_tree_next_child(&handle, dnode, 0))) {
1154 if (!(uuid = dm_tree_node_get_uuid(child))) {
1159 if (!_uuid_prefix_matches(uuid, uuid_prefix, uuid_prefix_len))
1162 if (dm_tree_node_num_children(child, 0))
1163 dm_tree_activate_children(child, uuid_prefix, uuid_prefix_len);
1168 for (priority = 0; priority < 2; priority++) {
1169 while ((child = dm_tree_next_child(&handle, dnode, 0))) {
1170 if (!(uuid = dm_tree_node_get_uuid(child))) {
1175 if (!_uuid_prefix_matches(uuid, uuid_prefix, uuid_prefix_len))
1178 if (priority != child->activation_priority)
1181 if (!(name = dm_tree_node_get_name(child))) {
1187 if (child->props.new_name) {
1188 if (!_rename_node(name, child->props.new_name, child->info.major,
1189 child->info.minor, &child->dtree->cookie,
1190 child->udev_flags)) {
1191 log_error("Failed to rename %s (%" PRIu32
1192 ":%" PRIu32 ") to %s", name, child->info.major,
1193 child->info.minor, child->props.new_name);
1196 child->name = child->props.new_name;
1197 child->props.new_name = NULL;
1200 if (!child->info.inactive_table && !child->info.suspended)
1203 if (!_resume_node(child->name, child->info.major, child->info.minor,
1204 child->props.read_ahead, child->props.read_ahead_flags,
1205 &newinfo, &child->dtree->cookie, child->udev_flags)) {
1206 log_error("Unable to resume %s (%" PRIu32
1207 ":%" PRIu32 ")", child->name, child->info.major,
1212 /* Update cached info */
1213 child->info = newinfo;
1222 static int _create_node(struct dm_tree_node *dnode)
1225 struct dm_task *dmt;
1227 log_verbose("Creating %s", dnode->name);
1229 if (!(dmt = dm_task_create(DM_DEVICE_CREATE))) {
1230 log_error("Create dm_task creation failed for %s", dnode->name);
1234 if (!dm_task_set_name(dmt, dnode->name)) {
1235 log_error("Failed to set device name for %s", dnode->name);
1239 if (!dm_task_set_uuid(dmt, dnode->uuid)) {
1240 log_error("Failed to set uuid for %s", dnode->name);
1244 if (dnode->props.major &&
1245 (!dm_task_set_major(dmt, dnode->props.major) ||
1246 !dm_task_set_minor(dmt, dnode->props.minor))) {
1247 log_error("Failed to set device number for %s creation.", dnode->name);
1251 if (dnode->props.read_only && !dm_task_set_ro(dmt)) {
1252 log_error("Failed to set read only flag for %s", dnode->name);
1256 if (!dm_task_no_open_count(dmt))
1257 log_error("Failed to disable open_count");
1259 if ((r = dm_task_run(dmt)))
1260 r = dm_task_get_info(dmt, &dnode->info);
1263 dm_task_destroy(dmt);
1269 static int _build_dev_string(char *devbuf, size_t bufsize, struct dm_tree_node *node)
1271 if (!dm_format_dev(devbuf, bufsize, node->info.major, node->info.minor)) {
1272 log_error("Failed to format %s device number for %s as dm "
1274 node->name, node->uuid, node->info.major, node->info.minor);
1281 /* simplify string emiting code */
1282 #define EMIT_PARAMS(p, str...)\
1285 if ((w = dm_snprintf(params + p, paramsize - (size_t) p, str)) < 0) {\
1286 stack; /* Out of space */\
1295 * Returns: 1 on success, 0 on failure
1297 static int _emit_areas_line(struct dm_task *dmt __attribute((unused)),
1298 struct load_segment *seg, char *params,
1299 size_t paramsize, int *pos)
1301 struct seg_area *area;
1302 char devbuf[DM_FORMAT_DEV_BUFSIZE];
1303 unsigned first_time = 1;
1305 dm_list_iterate_items(area, &seg->areas) {
1306 if (!_build_dev_string(devbuf, sizeof(devbuf), area->dev_node))
1309 EMIT_PARAMS(*pos, "%s%s %" PRIu64, first_time ? "" : " ",
1310 devbuf, area->offset);
1319 * Returns: 1 on success, 0 on failure
1321 static int _mirror_emit_segment_line(struct dm_task *dmt, uint32_t major,
1322 uint32_t minor, struct load_segment *seg,
1323 uint64_t *seg_start, char *params,
1327 int block_on_error = 0;
1328 int handle_errors = 0;
1329 int dm_log_userspace = 0;
1331 unsigned log_parm_count;
1333 char logbuf[DM_FORMAT_DEV_BUFSIZE];
1334 const char *logtype;
1340 if ((seg->flags & DM_BLOCK_ON_ERROR)) {
1342 * Originally, block_on_error was an argument to the log
1343 * portion of the mirror CTR table. It was renamed to
1344 * "handle_errors" and now resides in the 'features'
1345 * section of the mirror CTR table (i.e. at the end).
1347 * We can identify whether to use "block_on_error" or
1348 * "handle_errors" by the dm-mirror module's version
1349 * number (>= 1.12) or by the kernel version (>= 2.6.22).
1351 if (strncmp(uts.release, "2.6.22", 6) >= 0)
1357 if (seg->clustered) {
1358 /* Cluster mirrors require a UUID */
1363 * Cluster mirrors used to have their own log
1364 * types. Now they are accessed through the
1365 * userspace log type.
1367 * The dm-log-userspace module was added to the
1370 if (strncmp(uts.release, "2.6.31", 6) >= 0)
1371 dm_log_userspace = 1;
1377 /* [no]sync, block_on_error etc. */
1378 log_parm_count += hweight32(seg->flags);
1380 /* "handle_errors" is a feature arg now */
1384 /* DM_CORELOG does not count in the param list */
1385 if (seg->flags & DM_CORELOG)
1388 if (seg->clustered) {
1389 log_parm_count++; /* For UUID */
1391 if (!dm_log_userspace)
1392 EMIT_PARAMS(pos, "clustered-");
1400 if (!_build_dev_string(logbuf, sizeof(logbuf), seg->log))
1404 if (dm_log_userspace)
1405 EMIT_PARAMS(pos, "userspace %u %s clustered-%s",
1406 log_parm_count, seg->uuid, logtype);
1408 EMIT_PARAMS(pos, "%s %u", logtype, log_parm_count);
1411 EMIT_PARAMS(pos, " %s", logbuf);
1413 EMIT_PARAMS(pos, " %u", seg->region_size);
1415 if (seg->clustered && !dm_log_userspace)
1416 EMIT_PARAMS(pos, " %s", seg->uuid);
1418 if ((seg->flags & DM_NOSYNC))
1419 EMIT_PARAMS(pos, " nosync");
1420 else if ((seg->flags & DM_FORCESYNC))
1421 EMIT_PARAMS(pos, " sync");
1424 EMIT_PARAMS(pos, " block_on_error");
1426 EMIT_PARAMS(pos, " %u ", seg->mirror_area_count);
1428 if ((r = _emit_areas_line(dmt, seg, params, paramsize, &pos)) <= 0)
1432 EMIT_PARAMS(pos, " 1 handle_errors");
1437 static int _emit_segment_line(struct dm_task *dmt, uint32_t major,
1438 uint32_t minor, struct load_segment *seg,
1439 uint64_t *seg_start, char *params,
1444 char originbuf[DM_FORMAT_DEV_BUFSIZE], cowbuf[DM_FORMAT_DEV_BUFSIZE];
1452 /* Mirrors are pretty complicated - now in separate function */
1453 r = _mirror_emit_segment_line(dmt, major, minor, seg, seg_start,
1459 if (!_build_dev_string(originbuf, sizeof(originbuf), seg->origin))
1461 if (!_build_dev_string(cowbuf, sizeof(cowbuf), seg->cow))
1463 EMIT_PARAMS(pos, "%s %s %c %d", originbuf, cowbuf,
1464 seg->persistent ? 'P' : 'N', seg->chunk_size);
1466 case SEG_SNAPSHOT_ORIGIN:
1467 if (!_build_dev_string(originbuf, sizeof(originbuf), seg->origin))
1469 EMIT_PARAMS(pos, "%s", originbuf);
1472 EMIT_PARAMS(pos, "%u %u ", seg->area_count, seg->stripe_size);
1475 EMIT_PARAMS(pos, "%s%s%s%s%s %s %" PRIu64 " ", seg->cipher,
1476 seg->chainmode ? "-" : "", seg->chainmode ?: "",
1477 seg->iv ? "-" : "", seg->iv ?: "", seg->key,
1478 seg->iv_offset != DM_CRYPT_IV_DEFAULT ?
1479 seg->iv_offset : *seg_start);
1486 case SEG_SNAPSHOT_ORIGIN:
1492 if ((r = _emit_areas_line(dmt, seg, params, paramsize, &pos)) <= 0) {
1499 log_debug("Adding target to (%" PRIu32 ":%" PRIu32 "): %" PRIu64
1500 " %" PRIu64 " %s %s", major, minor,
1501 *seg_start, seg->size, dm_segtypes[seg->type].target, params);
1503 if (!dm_task_add_target(dmt, *seg_start, seg->size, dm_segtypes[seg->type].target, params))
1506 *seg_start += seg->size;
1513 static int _emit_segment(struct dm_task *dmt, uint32_t major, uint32_t minor,
1514 struct load_segment *seg, uint64_t *seg_start)
1517 size_t paramsize = 4096;
1521 if (!(params = dm_malloc(paramsize))) {
1522 log_error("Insufficient space for target parameters.");
1527 ret = _emit_segment_line(dmt, major, minor, seg, seg_start,
1537 log_debug("Insufficient space in params[%" PRIsize_t
1538 "] for target parameters.", paramsize);
1541 } while (paramsize < MAX_TARGET_PARAMSIZE);
1543 log_error("Target parameter size too big. Aborting.");
1547 static int _load_node(struct dm_tree_node *dnode)
1550 struct dm_task *dmt;
1551 struct load_segment *seg;
1552 uint64_t seg_start = 0;
1554 log_verbose("Loading %s table (%" PRIu32 ":%" PRIu32 ")", dnode->name,
1555 dnode->info.major, dnode->info.minor);
1557 if (!(dmt = dm_task_create(DM_DEVICE_RELOAD))) {
1558 log_error("Reload dm_task creation failed for %s", dnode->name);
1562 if (!dm_task_set_major(dmt, dnode->info.major) ||
1563 !dm_task_set_minor(dmt, dnode->info.minor)) {
1564 log_error("Failed to set device number for %s reload.", dnode->name);
1568 if (dnode->props.read_only && !dm_task_set_ro(dmt)) {
1569 log_error("Failed to set read only flag for %s", dnode->name);
1573 if (!dm_task_no_open_count(dmt))
1574 log_error("Failed to disable open_count");
1576 dm_list_iterate_items(seg, &dnode->props.segs)
1577 if (!_emit_segment(dmt, dnode->info.major, dnode->info.minor,
1581 if (!dm_task_suppress_identical_reload(dmt))
1582 log_error("Failed to suppress reload of identical tables.");
1584 if ((r = dm_task_run(dmt))) {
1585 r = dm_task_get_info(dmt, &dnode->info);
1586 if (r && !dnode->info.inactive_table)
1587 log_verbose("Suppressed %s identical table reload.",
1590 if ((dnode->props.size_changed =
1591 (dm_task_get_existing_table_size(dmt) == seg_start) ? 0 : 1))
1592 log_debug("Table size changed from %" PRIu64 " to %"
1594 dm_task_get_existing_table_size(dmt),
1595 seg_start, dnode->name);
1598 dnode->props.segment_count = 0;
1601 dm_task_destroy(dmt);
1606 int dm_tree_preload_children(struct dm_tree_node *dnode,
1607 const char *uuid_prefix,
1608 size_t uuid_prefix_len)
1610 void *handle = NULL;
1611 struct dm_tree_node *child;
1612 struct dm_info newinfo;
1614 /* Preload children first */
1615 while ((child = dm_tree_next_child(&handle, dnode, 0))) {
1616 /* Skip existing non-device-mapper devices */
1617 if (!child->info.exists && child->info.major)
1620 /* Ignore if it doesn't belong to this VG */
1621 if (child->info.exists &&
1622 !_uuid_prefix_matches(child->uuid, uuid_prefix, uuid_prefix_len))
1625 if (dm_tree_node_num_children(child, 0))
1626 dm_tree_preload_children(child, uuid_prefix, uuid_prefix_len);
1628 /* FIXME Cope if name exists with no uuid? */
1629 if (!child->info.exists) {
1630 if (!_create_node(child)) {
1636 if (!child->info.inactive_table && child->props.segment_count) {
1637 if (!_load_node(child)) {
1643 /* Propagate device size change change */
1644 if (child->props.size_changed)
1645 dnode->props.size_changed = 1;
1647 /* Resume device immediately if it has parents and its size changed */
1648 if (!dm_tree_node_num_children(child, 1) || !child->props.size_changed)
1651 if (!child->info.inactive_table && !child->info.suspended)
1654 if (!_resume_node(child->name, child->info.major, child->info.minor,
1655 child->props.read_ahead, child->props.read_ahead_flags,
1656 &newinfo, &child->dtree->cookie, child->udev_flags)) {
1657 log_error("Unable to resume %s (%" PRIu32
1658 ":%" PRIu32 ")", child->name, child->info.major,
1663 /* Update cached info */
1664 child->info = newinfo;
1673 * Returns 1 if unsure.
1675 int dm_tree_children_use_uuid(struct dm_tree_node *dnode,
1676 const char *uuid_prefix,
1677 size_t uuid_prefix_len)
1679 void *handle = NULL;
1680 struct dm_tree_node *child = dnode;
1683 while ((child = dm_tree_next_child(&handle, dnode, 0))) {
1684 if (!(uuid = dm_tree_node_get_uuid(child))) {
1685 log_error("Failed to get uuid for dtree node.");
1689 if (_uuid_prefix_matches(uuid, uuid_prefix, uuid_prefix_len))
1692 if (dm_tree_node_num_children(child, 0))
1693 dm_tree_children_use_uuid(child, uuid_prefix, uuid_prefix_len);
1702 static struct load_segment *_add_segment(struct dm_tree_node *dnode, unsigned type, uint64_t size)
1704 struct load_segment *seg;
1706 if (!(seg = dm_pool_zalloc(dnode->dtree->mem, sizeof(*seg)))) {
1707 log_error("dtree node segment allocation failed");
1713 seg->area_count = 0;
1714 dm_list_init(&seg->areas);
1715 seg->stripe_size = 0;
1716 seg->persistent = 0;
1717 seg->chunk_size = 0;
1721 dm_list_add(&dnode->props.segs, &seg->list);
1722 dnode->props.segment_count++;
1727 int dm_tree_node_add_snapshot_origin_target(struct dm_tree_node *dnode,
1729 const char *origin_uuid)
1731 struct load_segment *seg;
1732 struct dm_tree_node *origin_node;
1734 if (!(seg = _add_segment(dnode, SEG_SNAPSHOT_ORIGIN, size)))
1737 if (!(origin_node = dm_tree_find_node_by_uuid(dnode->dtree, origin_uuid))) {
1738 log_error("Couldn't find snapshot origin uuid %s.", origin_uuid);
1742 seg->origin = origin_node;
1743 if (!_link_tree_nodes(dnode, origin_node))
1746 /* Resume snapshot origins after new snapshots */
1747 dnode->activation_priority = 1;
1752 int dm_tree_node_add_snapshot_target(struct dm_tree_node *node,
1754 const char *origin_uuid,
1755 const char *cow_uuid,
1757 uint32_t chunk_size)
1759 struct load_segment *seg;
1760 struct dm_tree_node *origin_node, *cow_node;
1762 if (!(seg = _add_segment(node, SEG_SNAPSHOT, size)))
1765 if (!(origin_node = dm_tree_find_node_by_uuid(node->dtree, origin_uuid))) {
1766 log_error("Couldn't find snapshot origin uuid %s.", origin_uuid);
1770 seg->origin = origin_node;
1771 if (!_link_tree_nodes(node, origin_node))
1774 if (!(cow_node = dm_tree_find_node_by_uuid(node->dtree, cow_uuid))) {
1775 log_error("Couldn't find snapshot origin uuid %s.", cow_uuid);
1779 seg->cow = cow_node;
1780 if (!_link_tree_nodes(node, cow_node))
1783 seg->persistent = persistent ? 1 : 0;
1784 seg->chunk_size = chunk_size;
1789 int dm_tree_node_add_error_target(struct dm_tree_node *node,
1792 if (!_add_segment(node, SEG_ERROR, size))
1798 int dm_tree_node_add_zero_target(struct dm_tree_node *node,
1801 if (!_add_segment(node, SEG_ZERO, size))
1807 int dm_tree_node_add_linear_target(struct dm_tree_node *node,
1810 if (!_add_segment(node, SEG_LINEAR, size))
1816 int dm_tree_node_add_striped_target(struct dm_tree_node *node,
1818 uint32_t stripe_size)
1820 struct load_segment *seg;
1822 if (!(seg = _add_segment(node, SEG_STRIPED, size)))
1825 seg->stripe_size = stripe_size;
1830 int dm_tree_node_add_crypt_target(struct dm_tree_node *node,
1833 const char *chainmode,
1838 struct load_segment *seg;
1840 if (!(seg = _add_segment(node, SEG_CRYPT, size)))
1843 seg->cipher = cipher;
1844 seg->chainmode = chainmode;
1846 seg->iv_offset = iv_offset;
1852 int dm_tree_node_add_mirror_target_log(struct dm_tree_node *node,
1853 uint32_t region_size,
1855 const char *log_uuid,
1856 unsigned area_count,
1859 struct dm_tree_node *log_node = NULL;
1860 struct load_segment *seg;
1862 if (!node->props.segment_count) {
1863 log_error("Internal error: Attempt to add target area to missing segment.");
1867 seg = dm_list_item(dm_list_last(&node->props.segs), struct load_segment);
1870 if (!(seg->uuid = dm_pool_strdup(node->dtree->mem, log_uuid))) {
1871 log_error("log uuid pool_strdup failed");
1874 if (!(flags & DM_CORELOG)) {
1875 if (!(log_node = dm_tree_find_node_by_uuid(node->dtree, log_uuid))) {
1876 log_error("Couldn't find mirror log uuid %s.", log_uuid);
1880 if (!_link_tree_nodes(node, log_node))
1885 seg->log = log_node;
1886 seg->region_size = region_size;
1887 seg->clustered = clustered;
1888 seg->mirror_area_count = area_count;
1894 int dm_tree_node_add_mirror_target(struct dm_tree_node *node,
1897 struct load_segment *seg;
1899 if (!(seg = _add_segment(node, SEG_MIRRORED, size)))
1905 static int _add_area(struct dm_tree_node *node, struct load_segment *seg, struct dm_tree_node *dev_node, uint64_t offset)
1907 struct seg_area *area;
1909 if (!(area = dm_pool_zalloc(node->dtree->mem, sizeof (*area)))) {
1910 log_error("Failed to allocate target segment area.");
1914 area->dev_node = dev_node;
1915 area->offset = offset;
1917 dm_list_add(&seg->areas, &area->list);
1923 int dm_tree_node_add_target_area(struct dm_tree_node *node,
1924 const char *dev_name,
1928 struct load_segment *seg;
1930 struct dm_tree_node *dev_node;
1932 if ((!dev_name || !*dev_name) && (!uuid || !*uuid)) {
1933 log_error("dm_tree_node_add_target_area called without device");
1938 if (!(dev_node = dm_tree_find_node_by_uuid(node->dtree, uuid))) {
1939 log_error("Couldn't find area uuid %s.", uuid);
1942 if (!_link_tree_nodes(node, dev_node))
1945 if (stat(dev_name, &info) < 0) {
1946 log_error("Device %s not found.", dev_name);
1950 if (!S_ISBLK(info.st_mode)) {
1951 log_error("Device %s is not a block device.", dev_name);
1955 if (S_ISBLK(info.st_mode)) {
1956 log_error("Device %s is a block device. Use raw devices on NetBSD.", dev_name);
1960 /* FIXME Check correct macro use */
1961 if (!(dev_node = _add_dev(node->dtree, node, MAJOR(info.st_rdev), MINOR(info.st_rdev))))
1965 if (!node->props.segment_count) {
1966 log_error("Internal error: Attempt to add target area to missing segment.");
1970 seg = dm_list_item(dm_list_last(&node->props.segs), struct load_segment);
1972 if (!_add_area(node, seg, dev_node, offset))
1978 void dm_tree_set_cookie(struct dm_tree_node *node, uint32_t cookie)
1980 node->dtree->cookie = cookie;
1983 uint32_t dm_tree_get_cookie(struct dm_tree_node *node)
1985 return node->dtree->cookie;