Sync usage with man page.
[netbsd-mini2440.git] / external / gpl2 / lvm2 / dist / libdm / libdm-deptree.c
blobd17f53db8d9d5f10a86b35f503ef5a03f36955b9
1 /* $NetBSD: libdm-deptree.c,v 1.3 2009/02/18 12:16:13 haad Exp $ */
3 /*
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
17 #include "dmlib.h"
18 #include "libdm-targets.h"
19 #include "libdm-common.h"
20 #include "kdev_t.h"
21 #include "dm-ioctl.h"
23 #include <stdarg.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 */
33 enum {
34 SEG_CRYPT,
35 SEG_ERROR,
36 SEG_LINEAR,
37 SEG_MIRRORED,
38 SEG_SNAPSHOT,
39 SEG_SNAPSHOT_ORIGIN,
40 SEG_STRIPED,
41 SEG_ZERO,
44 /* FIXME Add crypt and multipath support */
46 struct {
47 unsigned type;
48 const char *target;
49 } dm_segtypes[] = {
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" },
57 { SEG_ZERO, "zero"},
60 /* Some segment types have a list of areas of other devices attached */
61 struct seg_area {
62 struct dm_list list;
64 struct dm_tree_node *dev_node;
66 uint64_t offset;
69 /* Per-segment properties */
70 struct load_segment {
71 struct dm_list list;
73 unsigned type;
75 uint64_t size;
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 {
103 int read_only;
104 uint32_t major;
105 uint32_t minor;
107 uint32_t read_ahead;
108 uint32_t read_ahead_flags;
110 unsigned segment_count;
111 unsigned size_changed;
112 struct dm_list segs;
114 const char *new_name;
117 /* Two of these used to join two nodes with uses and used_by. */
118 struct dm_tree_link {
119 struct dm_list list;
120 struct dm_tree_node *node;
123 struct dm_tree_node {
124 struct dm_tree *dtree;
126 const char *name;
127 const char *uuid;
128 struct dm_info info;
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 */
142 struct dm_tree {
143 struct dm_pool *mem;
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) */
149 uint32_t cookie;
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");
158 return NULL;
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;
166 dtree->no_flush = 0;
168 if (!(dtree->mem = dm_pool_create("dtree", 1024))) {
169 log_error("dtree pool creation failed");
170 dm_free(dtree);
171 return NULL;
174 if (!(dtree->devs = dm_hash_create(8))) {
175 log_error("dtree hash creation failed");
176 dm_pool_destroy(dtree->mem);
177 dm_free(dtree);
178 return NULL;
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);
185 dm_free(dtree);
186 return NULL;
189 return dtree;
192 void dm_tree_free(struct dm_tree *dtree)
194 if (!dtree)
195 return;
197 dm_hash_destroy(dtree->uuids);
198 dm_hash_destroy(dtree->devs);
199 dm_pool_destroy(dtree->mem);
200 dm_free(dtree);
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)
210 return 1;
212 return 0;
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");
221 return 0;
224 dlink->node = node;
225 dm_list_add(list, &dlink->list);
227 return 1;
230 static int _link_nodes(struct dm_tree_node *parent,
231 struct dm_tree_node *child)
233 if (_nodes_are_linked(parent, child))
234 return 1;
236 if (!_link(&parent->uses, child))
237 return 0;
239 if (!_link(&child->used_by, parent))
240 return 0;
242 return 1;
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);
252 break;
256 static void _unlink_nodes(struct dm_tree_node *parent,
257 struct dm_tree_node *child)
259 if (!_nodes_are_linked(parent, child))
260 return;
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))
291 return 1;
292 } else
293 _remove_from_toplevel(child);
295 if ((child == &child->dtree->root)) {
296 if (dm_tree_node_num_children(parent, 0))
297 return 1;
298 } else
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,
305 const char *name,
306 const char *uuid,
307 struct dm_info *info,
308 void *context,
309 uint16_t udev_flags)
311 struct dm_tree_node *node;
312 uint64_t dev;
314 if (!(node = dm_pool_zalloc(dtree->mem, sizeof(*node)))) {
315 log_error("_create_dm_tree_node alloc failed");
316 return NULL;
319 node->dtree = dtree;
321 node->name = name;
322 node->uuid = uuid;
323 node->info = *info;
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);
338 return NULL;
341 if (uuid && *uuid &&
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,
345 sizeof(dev));
346 dm_pool_free(dtree->mem, node);
347 return NULL;
350 return 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,
359 sizeof(dev));
362 static struct dm_tree_node *_find_dm_tree_node_by_uuid(struct dm_tree *dtree,
363 const char *uuid)
365 struct dm_tree_node *node;
367 if ((node = dm_hash_lookup(dtree->uuids, uuid)))
368 return node;
370 if (strncmp(uuid, UUID_PREFIX, sizeof(UUID_PREFIX) - 1))
371 return NULL;
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)) {
383 *name = "";
384 *uuid = "";
385 *deps = NULL;
386 info->major = major;
387 info->minor = minor;
388 info->exists = 0;
389 info->live_table = 0;
390 info->inactive_table = 0;
391 info->read_only = 0;
392 return 1;
395 if (!(*dmt = dm_task_create(DM_DEVICE_DEPS))) {
396 log_error("deps dm_task creation failed");
397 return 0;
400 if (!dm_task_set_major(*dmt, major)) {
401 log_error("_deps: failed to set major for (%" PRIu32 ":%" PRIu32 ")",
402 major, minor);
403 goto failed;
406 if (!dm_task_set_minor(*dmt, minor)) {
407 log_error("_deps: failed to set minor for (%" PRIu32 ":%" PRIu32 ")",
408 major, minor);
409 goto failed;
412 if (!dm_task_run(*dmt)) {
413 log_error("_deps: task run failed for (%" PRIu32 ":%" PRIu32 ")",
414 major, minor);
415 goto failed;
418 if (!dm_task_get_info(*dmt, info)) {
419 log_error("_deps: failed to get info for (%" PRIu32 ":%" PRIu32 ")",
420 major, minor);
421 goto failed;
424 if (!info->exists) {
425 *name = "";
426 *uuid = "";
427 *deps = NULL;
428 } else {
429 if (info->major != major) {
430 log_error("Inconsistent dtree major number: %u != %u",
431 major, info->major);
432 goto failed;
434 if (info->minor != minor) {
435 log_error("Inconsistent dtree minor number: %u != %u",
436 minor, info->minor);
437 goto failed;
439 if (!(*name = dm_pool_strdup(mem, dm_task_get_name(*dmt)))) {
440 log_error("name pool_strdup failed");
441 goto failed;
443 if (!(*uuid = dm_pool_strdup(mem, dm_task_get_uuid(*dmt)))) {
444 log_error("uuid pool_strdup failed");
445 goto failed;
447 *deps = dm_task_get_deps(*dmt);
450 return 1;
452 failed:
453 dm_task_destroy(*dmt);
454 return 0;
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;
462 struct dm_info info;
463 struct dm_deps *deps = NULL;
464 const char *name = NULL;
465 const char *uuid = NULL;
466 struct dm_tree_node *node = NULL;
467 uint32_t i;
468 int new = 0;
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))
473 return_NULL;
475 if (!(node = _create_dm_tree_node(dtree, name, uuid, &info,
476 NULL, 0)))
477 goto_out;
478 new = 1;
481 if (!_link_tree_nodes(parent, node)) {
482 node = NULL;
483 goto_out;
486 /* If node was already in tree, no need to recurse. */
487 if (!new)
488 goto out;
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)) {
493 stack;
494 node = NULL;
496 goto out;
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]))) {
503 node = NULL;
504 goto_out;
507 out:
508 if (dmt)
509 dm_task_destroy(dmt);
511 return node;
514 static int _node_clear_table(struct dm_tree_node *dnode)
516 struct dm_task *dmt;
517 struct dm_info *info;
518 const char *name;
519 int r;
521 if (!(info = &dnode->info)) {
522 log_error("_node_clear_table failed: missing info");
523 return 0;
526 if (!(name = dm_tree_node_get_name(dnode))) {
527 log_error("_node_clear_table failed: missing name");
528 return 0;
531 /* Is there a table? */
532 if (!info->exists || !info->inactive_table)
533 return 1;
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);
541 return 0;
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);
548 return 0;
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);
555 r = 0;
558 dm_task_destroy(dmt);
560 return r;
563 struct dm_tree_node *dm_tree_add_new_dev(struct dm_tree *dtree,
564 const char *name,
565 const char *uuid,
566 uint32_t major, uint32_t minor,
567 int read_only,
568 int clear_inactive,
569 void *context)
571 struct dm_tree_node *dnode;
572 struct dm_info info;
573 const char *name2;
574 const char *uuid2;
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");
580 return NULL;
582 if (!(uuid2 = dm_pool_strdup(dtree->mem, uuid))) {
583 log_error("uuid pool_strdup failed");
584 return NULL;
587 info.major = 0;
588 info.minor = 0;
589 info.exists = 0;
590 info.live_table = 0;
591 info.inactive_table = 0;
592 info.read_only = 0;
594 if (!(dnode = _create_dm_tree_node(dtree, name2, uuid2, &info,
595 context, 0)))
596 return_NULL;
598 /* Attach to root node until a table is supplied */
599 if (!_add_to_toplevel(dnode) || !_add_to_bottomlevel(dnode))
600 return_NULL;
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");
610 return 0;
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))
619 return_NULL;
621 dnode->context = context;
622 dnode->udev_flags = 0;
624 return dnode;
627 struct dm_tree_node *dm_tree_add_new_dev_with_udev_flags(struct dm_tree *dtree,
628 const char *name,
629 const char *uuid,
630 uint32_t major,
631 uint32_t minor,
632 int read_only,
633 int clear_inactive,
634 void *context,
635 uint16_t udev_flags)
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;
643 return node;
647 void dm_tree_node_set_read_ahead(struct dm_tree_node *dnode,
648 uint32_t read_ahead,
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)
672 return &node->info;
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)
687 if (inverted) {
688 if (_nodes_are_linked(&node->dtree->root, node))
689 return 0;
690 return dm_list_size(&node->used_by);
693 if (_nodes_are_linked(node, &node->dtree->root))
694 return 0;
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)
704 if (!uuid_prefix)
705 return 1;
707 if (!strncmp(uuid, uuid_prefix, uuid_prefix_len))
708 return 1;
710 /* Handle transition: active device uuids might be missing the prefix */
711 if (uuid_prefix_len <= 4)
712 return 0;
714 if (!strncmp(uuid, UUID_PREFIX, sizeof(UUID_PREFIX) - 1))
715 return 0;
717 if (strncmp(uuid_prefix, UUID_PREFIX, sizeof(UUID_PREFIX) - 1))
718 return 0;
720 if (!strncmp(uuid, uuid_prefix + sizeof(UUID_PREFIX) - 1, uuid_prefix_len - (sizeof(UUID_PREFIX) - 1)))
721 return 1;
723 return 0;
727 * Returns 1 if no children.
729 static int _children_suspended(struct dm_tree_node *node,
730 uint32_t inverted,
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;
737 const char *uuid;
739 if (inverted) {
740 if (_nodes_are_linked(&node->dtree->root, node))
741 return 1;
742 list = &node->used_by;
743 } else {
744 if (_nodes_are_linked(node, &node->dtree->root))
745 return 1;
746 list = &node->uses;
749 dm_list_iterate_items(dlink, list) {
750 if (!(uuid = dm_tree_node_get_uuid(dlink->node))) {
751 stack;
752 continue;
755 /* Ignore if it doesn't belong to this VG */
756 if (!_uuid_prefix_matches(uuid, uuid_prefix, uuid_prefix_len))
757 continue;
759 if (!(dinfo = dm_tree_node_get_info(dlink->node))) {
760 stack; /* FIXME Is this normal? */
761 return 0;
764 if (!dinfo->suspended)
765 return 0;
768 return 1;
772 * Set major and minor to zero for root of tree.
774 struct dm_tree_node *dm_tree_find_node(struct dm_tree *dtree,
775 uint32_t major,
776 uint32_t minor)
778 if (!major && !minor)
779 return &dtree->root;
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,
788 const char *uuid)
790 if (!uuid || !*uuid)
791 return &dtree->root;
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,
802 uint32_t inverted)
804 struct dm_list **dlink = (struct dm_list **) handle;
805 struct dm_list *use_list;
807 if (inverted)
808 use_list = &parent->used_by;
809 else
810 use_list = &parent->uses;
812 if (!*dlink)
813 *dlink = dm_list_first(use_list);
814 else
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)
826 struct dm_task *dmt;
827 int r;
829 if (!(dmt = dm_task_create(DM_DEVICE_INFO))) {
830 log_error("_info_by_dev: dm_task creation failed");
831 return 0;
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);
837 return 0;
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);
848 return r;
851 static int _deactivate_node(const char *name, uint32_t major, uint32_t minor,
852 uint32_t *cookie, uint16_t udev_flags)
854 struct dm_task *dmt;
855 int r = 0;
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);
861 return 0;
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);
866 goto out;
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))
873 goto out;
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? */
882 out:
883 dm_task_destroy(dmt);
885 return r;
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)
891 struct dm_task *dmt;
892 int r = 0;
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);
898 return 0;
901 if (!dm_task_set_name(dmt, old_name)) {
902 log_error("Failed to set name for %s rename.", old_name);
903 goto out;
906 if (!dm_task_set_newname(dmt, new_name))
907 goto_out;
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))
913 goto out;
915 r = dm_task_run(dmt);
917 out:
918 dm_task_destroy(dmt);
920 return r;
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,
927 uint16_t udev_flags)
929 struct dm_task *dmt;
930 int r = 0;
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);
936 return 0;
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);
942 goto out;
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);
947 goto out;
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))
957 goto out;
959 if ((r = dm_task_run(dmt)))
960 r = dm_task_get_info(dmt, newinfo);
962 out:
963 dm_task_destroy(dmt);
965 return r;
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)
971 struct dm_task *dmt;
972 int r;
974 log_verbose("Suspending %s (%" PRIu32 ":%" PRIu32 ")%s%s",
975 name, major, minor,
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);
981 return 0;
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);
987 return 0;
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);
1004 return r;
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;
1015 const char *name;
1016 const char *uuid;
1018 while ((child = dm_tree_next_child(&handle, dnode, 0))) {
1019 if (!(dinfo = dm_tree_node_get_info(child))) {
1020 stack;
1021 continue;
1024 if (!(name = dm_tree_node_get_name(child))) {
1025 stack;
1026 continue;
1029 if (!(uuid = dm_tree_node_get_uuid(child))) {
1030 stack;
1031 continue;
1034 /* Ignore if it doesn't belong to this VG */
1035 if (!_uuid_prefix_matches(uuid, uuid_prefix, uuid_prefix_len))
1036 continue;
1038 /* Refresh open_count */
1039 if (!_info_by_dev(dinfo->major, dinfo->minor, 1, &info) ||
1040 !info.exists || info.open_count)
1041 continue;
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,
1047 info.minor);
1048 continue;
1051 if (dm_tree_node_num_children(child, 0))
1052 dm_tree_deactivate_children(child, uuid_prefix, uuid_prefix_len);
1055 return 1;
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;
1076 const char *name;
1077 const char *uuid;
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))) {
1082 stack;
1083 continue;
1086 if (!(name = dm_tree_node_get_name(child))) {
1087 stack;
1088 continue;
1091 if (!(uuid = dm_tree_node_get_uuid(child))) {
1092 stack;
1093 continue;
1096 /* Ignore if it doesn't belong to this VG */
1097 if (!_uuid_prefix_matches(uuid, uuid_prefix, uuid_prefix_len))
1098 continue;
1100 /* Ensure immediate parents are already suspended */
1101 if (!_children_suspended(child, 1, uuid_prefix, uuid_prefix_len))
1102 continue;
1104 if (!_info_by_dev(dinfo->major, dinfo->minor, 0, &info) ||
1105 !info.exists || info.suspended)
1106 continue;
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,
1113 info.minor);
1114 continue;
1117 /* Update cached info */
1118 child->info = newinfo;
1121 /* Then suspend any child nodes */
1122 handle = NULL;
1124 while ((child = dm_tree_next_child(&handle, dnode, 0))) {
1125 if (!(uuid = dm_tree_node_get_uuid(child))) {
1126 stack;
1127 continue;
1130 /* Ignore if it doesn't belong to this VG */
1131 if (!_uuid_prefix_matches(uuid, uuid_prefix, uuid_prefix_len))
1132 continue;
1134 if (dm_tree_node_num_children(child, 0))
1135 dm_tree_suspend_children(child, uuid_prefix, uuid_prefix_len);
1138 return 1;
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;
1148 const char *name;
1149 const char *uuid;
1150 int priority;
1152 /* Activate children first */
1153 while ((child = dm_tree_next_child(&handle, dnode, 0))) {
1154 if (!(uuid = dm_tree_node_get_uuid(child))) {
1155 stack;
1156 continue;
1159 if (!_uuid_prefix_matches(uuid, uuid_prefix, uuid_prefix_len))
1160 continue;
1162 if (dm_tree_node_num_children(child, 0))
1163 dm_tree_activate_children(child, uuid_prefix, uuid_prefix_len);
1166 handle = NULL;
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))) {
1171 stack;
1172 continue;
1175 if (!_uuid_prefix_matches(uuid, uuid_prefix, uuid_prefix_len))
1176 continue;
1178 if (priority != child->activation_priority)
1179 continue;
1181 if (!(name = dm_tree_node_get_name(child))) {
1182 stack;
1183 continue;
1186 /* Rename? */
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);
1194 return 0;
1196 child->name = child->props.new_name;
1197 child->props.new_name = NULL;
1200 if (!child->info.inactive_table && !child->info.suspended)
1201 continue;
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,
1208 child->info.minor);
1209 continue;
1212 /* Update cached info */
1213 child->info = newinfo;
1217 handle = NULL;
1219 return 1;
1222 static int _create_node(struct dm_tree_node *dnode)
1224 int r = 0;
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);
1231 return 0;
1234 if (!dm_task_set_name(dmt, dnode->name)) {
1235 log_error("Failed to set device name for %s", dnode->name);
1236 goto out;
1239 if (!dm_task_set_uuid(dmt, dnode->uuid)) {
1240 log_error("Failed to set uuid for %s", dnode->name);
1241 goto out;
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);
1248 goto out;
1251 if (dnode->props.read_only && !dm_task_set_ro(dmt)) {
1252 log_error("Failed to set read only flag for %s", dnode->name);
1253 goto out;
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);
1262 out:
1263 dm_task_destroy(dmt);
1265 return r;
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 "
1273 "target (%u,%u)",
1274 node->name, node->uuid, node->info.major, node->info.minor);
1275 return 0;
1278 return 1;
1281 /* simplify string emiting code */
1282 #define EMIT_PARAMS(p, str...)\
1283 do {\
1284 int w;\
1285 if ((w = dm_snprintf(params + p, paramsize - (size_t) p, str)) < 0) {\
1286 stack; /* Out of space */\
1287 return -1;\
1289 p += w;\
1290 } while (0)
1293 * _emit_areas_line
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))
1307 return_0;
1309 EMIT_PARAMS(*pos, "%s%s %" PRIu64, first_time ? "" : " ",
1310 devbuf, area->offset);
1312 first_time = 0;
1315 return 1;
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,
1324 size_t paramsize)
1326 int r;
1327 int block_on_error = 0;
1328 int handle_errors = 0;
1329 int dm_log_userspace = 0;
1330 struct utsname uts;
1331 unsigned log_parm_count;
1332 int pos = 0;
1333 char logbuf[DM_FORMAT_DEV_BUFSIZE];
1334 const char *logtype;
1336 r = uname(&uts);
1337 if (r)
1338 return_0;
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)
1352 handle_errors = 1;
1353 else
1354 block_on_error = 1;
1357 if (seg->clustered) {
1358 /* Cluster mirrors require a UUID */
1359 if (!seg->uuid)
1360 return_0;
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
1368 * 2.6.31 kernel.
1370 if (strncmp(uts.release, "2.6.31", 6) >= 0)
1371 dm_log_userspace = 1;
1374 /* Region size */
1375 log_parm_count = 1;
1377 /* [no]sync, block_on_error etc. */
1378 log_parm_count += hweight32(seg->flags);
1380 /* "handle_errors" is a feature arg now */
1381 if (handle_errors)
1382 log_parm_count--;
1384 /* DM_CORELOG does not count in the param list */
1385 if (seg->flags & DM_CORELOG)
1386 log_parm_count--;
1388 if (seg->clustered) {
1389 log_parm_count++; /* For UUID */
1391 if (!dm_log_userspace)
1392 EMIT_PARAMS(pos, "clustered-");
1395 if (!seg->log)
1396 logtype = "core";
1397 else {
1398 logtype = "disk";
1399 log_parm_count++;
1400 if (!_build_dev_string(logbuf, sizeof(logbuf), seg->log))
1401 return_0;
1404 if (dm_log_userspace)
1405 EMIT_PARAMS(pos, "userspace %u %s clustered-%s",
1406 log_parm_count, seg->uuid, logtype);
1407 else
1408 EMIT_PARAMS(pos, "%s %u", logtype, log_parm_count);
1410 if (seg->log)
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");
1423 if (block_on_error)
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)
1429 return_0;
1431 if (handle_errors)
1432 EMIT_PARAMS(pos, " 1 handle_errors");
1434 return 1;
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,
1440 size_t paramsize)
1442 int pos = 0;
1443 int r;
1444 char originbuf[DM_FORMAT_DEV_BUFSIZE], cowbuf[DM_FORMAT_DEV_BUFSIZE];
1446 switch(seg->type) {
1447 case SEG_ERROR:
1448 case SEG_ZERO:
1449 case SEG_LINEAR:
1450 break;
1451 case SEG_MIRRORED:
1452 /* Mirrors are pretty complicated - now in separate function */
1453 r = _mirror_emit_segment_line(dmt, major, minor, seg, seg_start,
1454 params, paramsize);
1455 if (!r)
1456 return_0;
1457 break;
1458 case SEG_SNAPSHOT:
1459 if (!_build_dev_string(originbuf, sizeof(originbuf), seg->origin))
1460 return_0;
1461 if (!_build_dev_string(cowbuf, sizeof(cowbuf), seg->cow))
1462 return_0;
1463 EMIT_PARAMS(pos, "%s %s %c %d", originbuf, cowbuf,
1464 seg->persistent ? 'P' : 'N', seg->chunk_size);
1465 break;
1466 case SEG_SNAPSHOT_ORIGIN:
1467 if (!_build_dev_string(originbuf, sizeof(originbuf), seg->origin))
1468 return_0;
1469 EMIT_PARAMS(pos, "%s", originbuf);
1470 break;
1471 case SEG_STRIPED:
1472 EMIT_PARAMS(pos, "%u %u ", seg->area_count, seg->stripe_size);
1473 break;
1474 case SEG_CRYPT:
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);
1480 break;
1483 switch(seg->type) {
1484 case SEG_ERROR:
1485 case SEG_SNAPSHOT:
1486 case SEG_SNAPSHOT_ORIGIN:
1487 case SEG_ZERO:
1488 break;
1489 case SEG_CRYPT:
1490 case SEG_LINEAR:
1491 case SEG_STRIPED:
1492 if ((r = _emit_areas_line(dmt, seg, params, paramsize, &pos)) <= 0) {
1493 stack;
1494 return r;
1496 break;
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))
1504 return_0;
1506 *seg_start += seg->size;
1508 return 1;
1511 #undef EMIT_PARAMS
1513 static int _emit_segment(struct dm_task *dmt, uint32_t major, uint32_t minor,
1514 struct load_segment *seg, uint64_t *seg_start)
1516 char *params;
1517 size_t paramsize = 4096;
1518 int ret;
1520 do {
1521 if (!(params = dm_malloc(paramsize))) {
1522 log_error("Insufficient space for target parameters.");
1523 return 0;
1526 params[0] = '\0';
1527 ret = _emit_segment_line(dmt, major, minor, seg, seg_start,
1528 params, paramsize);
1529 dm_free(params);
1531 if (!ret)
1532 stack;
1534 if (ret >= 0)
1535 return ret;
1537 log_debug("Insufficient space in params[%" PRIsize_t
1538 "] for target parameters.", paramsize);
1540 paramsize *= 2;
1541 } while (paramsize < MAX_TARGET_PARAMSIZE);
1543 log_error("Target parameter size too big. Aborting.");
1544 return 0;
1547 static int _load_node(struct dm_tree_node *dnode)
1549 int r = 0;
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);
1559 return 0;
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);
1565 goto out;
1568 if (dnode->props.read_only && !dm_task_set_ro(dmt)) {
1569 log_error("Failed to set read only flag for %s", dnode->name);
1570 goto out;
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,
1578 seg, &seg_start))
1579 goto_out;
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.",
1588 dnode->name);
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 %"
1593 PRIu64 " for %s",
1594 dm_task_get_existing_table_size(dmt),
1595 seg_start, dnode->name);
1598 dnode->props.segment_count = 0;
1600 out:
1601 dm_task_destroy(dmt);
1603 return r;
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)
1618 continue;
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))
1623 continue;
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)) {
1631 stack;
1632 return 0;
1636 if (!child->info.inactive_table && child->props.segment_count) {
1637 if (!_load_node(child)) {
1638 stack;
1639 return 0;
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)
1649 continue;
1651 if (!child->info.inactive_table && !child->info.suspended)
1652 continue;
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,
1659 child->info.minor);
1660 continue;
1663 /* Update cached info */
1664 child->info = newinfo;
1667 handle = NULL;
1669 return 1;
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;
1681 const char *uuid;
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.");
1686 return 1;
1689 if (_uuid_prefix_matches(uuid, uuid_prefix, uuid_prefix_len))
1690 return 1;
1692 if (dm_tree_node_num_children(child, 0))
1693 dm_tree_children_use_uuid(child, uuid_prefix, uuid_prefix_len);
1696 return 0;
1700 * Target functions
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");
1708 return NULL;
1711 seg->type = type;
1712 seg->size = size;
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;
1718 seg->cow = NULL;
1719 seg->origin = NULL;
1721 dm_list_add(&dnode->props.segs, &seg->list);
1722 dnode->props.segment_count++;
1724 return seg;
1727 int dm_tree_node_add_snapshot_origin_target(struct dm_tree_node *dnode,
1728 uint64_t size,
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)))
1735 return_0;
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);
1739 return 0;
1742 seg->origin = origin_node;
1743 if (!_link_tree_nodes(dnode, origin_node))
1744 return_0;
1746 /* Resume snapshot origins after new snapshots */
1747 dnode->activation_priority = 1;
1749 return 1;
1752 int dm_tree_node_add_snapshot_target(struct dm_tree_node *node,
1753 uint64_t size,
1754 const char *origin_uuid,
1755 const char *cow_uuid,
1756 int persistent,
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)))
1763 return_0;
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);
1767 return 0;
1770 seg->origin = origin_node;
1771 if (!_link_tree_nodes(node, origin_node))
1772 return_0;
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);
1776 return 0;
1779 seg->cow = cow_node;
1780 if (!_link_tree_nodes(node, cow_node))
1781 return_0;
1783 seg->persistent = persistent ? 1 : 0;
1784 seg->chunk_size = chunk_size;
1786 return 1;
1789 int dm_tree_node_add_error_target(struct dm_tree_node *node,
1790 uint64_t size)
1792 if (!_add_segment(node, SEG_ERROR, size))
1793 return_0;
1795 return 1;
1798 int dm_tree_node_add_zero_target(struct dm_tree_node *node,
1799 uint64_t size)
1801 if (!_add_segment(node, SEG_ZERO, size))
1802 return_0;
1804 return 1;
1807 int dm_tree_node_add_linear_target(struct dm_tree_node *node,
1808 uint64_t size)
1810 if (!_add_segment(node, SEG_LINEAR, size))
1811 return_0;
1813 return 1;
1816 int dm_tree_node_add_striped_target(struct dm_tree_node *node,
1817 uint64_t size,
1818 uint32_t stripe_size)
1820 struct load_segment *seg;
1822 if (!(seg = _add_segment(node, SEG_STRIPED, size)))
1823 return_0;
1825 seg->stripe_size = stripe_size;
1827 return 1;
1830 int dm_tree_node_add_crypt_target(struct dm_tree_node *node,
1831 uint64_t size,
1832 const char *cipher,
1833 const char *chainmode,
1834 const char *iv,
1835 uint64_t iv_offset,
1836 const char *key)
1838 struct load_segment *seg;
1840 if (!(seg = _add_segment(node, SEG_CRYPT, size)))
1841 return_0;
1843 seg->cipher = cipher;
1844 seg->chainmode = chainmode;
1845 seg->iv = iv;
1846 seg->iv_offset = iv_offset;
1847 seg->key = key;
1849 return 1;
1852 int dm_tree_node_add_mirror_target_log(struct dm_tree_node *node,
1853 uint32_t region_size,
1854 unsigned clustered,
1855 const char *log_uuid,
1856 unsigned area_count,
1857 uint32_t flags)
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.");
1864 return 0;
1867 seg = dm_list_item(dm_list_last(&node->props.segs), struct load_segment);
1869 if (log_uuid) {
1870 if (!(seg->uuid = dm_pool_strdup(node->dtree->mem, log_uuid))) {
1871 log_error("log uuid pool_strdup failed");
1872 return 0;
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);
1877 return 0;
1880 if (!_link_tree_nodes(node, log_node))
1881 return_0;
1885 seg->log = log_node;
1886 seg->region_size = region_size;
1887 seg->clustered = clustered;
1888 seg->mirror_area_count = area_count;
1889 seg->flags = flags;
1891 return 1;
1894 int dm_tree_node_add_mirror_target(struct dm_tree_node *node,
1895 uint64_t size)
1897 struct load_segment *seg;
1899 if (!(seg = _add_segment(node, SEG_MIRRORED, size)))
1900 return_0;
1902 return 1;
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.");
1911 return 0;
1914 area->dev_node = dev_node;
1915 area->offset = offset;
1917 dm_list_add(&seg->areas, &area->list);
1918 seg->area_count++;
1920 return 1;
1923 int dm_tree_node_add_target_area(struct dm_tree_node *node,
1924 const char *dev_name,
1925 const char *uuid,
1926 uint64_t offset)
1928 struct load_segment *seg;
1929 struct stat info;
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");
1934 return 0;
1937 if (uuid) {
1938 if (!(dev_node = dm_tree_find_node_by_uuid(node->dtree, uuid))) {
1939 log_error("Couldn't find area uuid %s.", uuid);
1940 return 0;
1942 if (!_link_tree_nodes(node, dev_node))
1943 return_0;
1944 } else {
1945 if (stat(dev_name, &info) < 0) {
1946 log_error("Device %s not found.", dev_name);
1947 return 0;
1949 #ifndef __NetBSD__
1950 if (!S_ISBLK(info.st_mode)) {
1951 log_error("Device %s is not a block device.", dev_name);
1952 return 0;
1954 #else
1955 if (S_ISBLK(info.st_mode)) {
1956 log_error("Device %s is a block device. Use raw devices on NetBSD.", dev_name);
1957 return 0;
1959 #endif
1960 /* FIXME Check correct macro use */
1961 if (!(dev_node = _add_dev(node->dtree, node, MAJOR(info.st_rdev), MINOR(info.st_rdev))))
1962 return_0;
1965 if (!node->props.segment_count) {
1966 log_error("Internal error: Attempt to add target area to missing segment.");
1967 return 0;
1970 seg = dm_list_item(dm_list_last(&node->props.segs), struct load_segment);
1972 if (!_add_area(node, seg, dev_node, offset))
1973 return_0;
1975 return 1;
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;