2 * Copyright (C) 2005-2007 Red Hat, Inc. All rights reserved.
4 * This file is part of the device-mapper userspace tools.
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.
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
16 #include "libdm-targets.h"
17 #include "libdm-common.h"
22 #include <sys/param.h>
23 #include <sys/utsname.h>
25 #define MAX_TARGET_PARAMSIZE 500000
27 /* FIXME Fix interface so this is used only by LVM */
28 #define UUID_PREFIX "LVM-"
30 /* Supported segment types */
43 /* FIXME Add crypt and multipath support */
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" },
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 */
86 struct dm_tree_node
*merge
; /* Snapshot */
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 */
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 */
102 /* Per-device properties */
103 struct load_properties
{
109 uint32_t read_ahead_flags
;
111 unsigned segment_count
;
112 unsigned size_changed
;
115 const char *new_name
;
118 /* Two of these used to join two nodes with uses and used_by. */
119 struct dm_tree_link
{
121 struct dm_tree_node
*node
;
124 struct dm_tree_node
{
125 struct dm_tree
*dtree
;
131 struct dm_list uses
; /* Nodes this node uses */
132 struct dm_list used_by
; /* Nodes that use this node */
134 int activation_priority
; /* 0 gets activated first */
136 uint16_t udev_flags
; /* Udev control flags */
138 void *context
; /* External supplied context */
140 struct load_properties props
; /* For creation/table (re)load */
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) */
153 struct dm_tree
*dm_tree_create(void)
155 struct dm_tree
*dtree
;
157 if (!(dtree
= dm_malloc(sizeof(*dtree
)))) {
158 log_error("dm_tree_create malloc failed");
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;
169 if (!(dtree
->mem
= dm_pool_create("dtree", 1024))) {
170 log_error("dtree pool creation failed");
175 if (!(dtree
->devs
= dm_hash_create(8))) {
176 log_error("dtree hash creation failed");
177 dm_pool_destroy(dtree
->mem
);
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
);
193 void dm_tree_free(struct dm_tree
*dtree
)
198 dm_hash_destroy(dtree
->uuids
);
199 dm_hash_destroy(dtree
->devs
);
200 dm_pool_destroy(dtree
->mem
);
204 static int _nodes_are_linked(const struct dm_tree_node
*parent
,
205 const struct dm_tree_node
*child
)
207 struct dm_tree_link
*dlink
;
209 dm_list_iterate_items(dlink
, &parent
->uses
)
210 if (dlink
->node
== child
)
216 static int _link(struct dm_list
*list
, struct dm_tree_node
*node
)
218 struct dm_tree_link
*dlink
;
220 if (!(dlink
= dm_pool_alloc(node
->dtree
->mem
, sizeof(*dlink
)))) {
221 log_error("dtree link allocation failed");
226 dm_list_add(list
, &dlink
->list
);
231 static int _link_nodes(struct dm_tree_node
*parent
,
232 struct dm_tree_node
*child
)
234 if (_nodes_are_linked(parent
, child
))
237 if (!_link(&parent
->uses
, child
))
240 if (!_link(&child
->used_by
, parent
))
246 static void _unlink(struct dm_list
*list
, struct dm_tree_node
*node
)
248 struct dm_tree_link
*dlink
;
250 dm_list_iterate_items(dlink
, list
)
251 if (dlink
->node
== node
) {
252 dm_list_del(&dlink
->list
);
257 static void _unlink_nodes(struct dm_tree_node
*parent
,
258 struct dm_tree_node
*child
)
260 if (!_nodes_are_linked(parent
, child
))
263 _unlink(&parent
->uses
, child
);
264 _unlink(&child
->used_by
, parent
);
267 static int _add_to_toplevel(struct dm_tree_node
*node
)
269 return _link_nodes(&node
->dtree
->root
, node
);
272 static void _remove_from_toplevel(struct dm_tree_node
*node
)
274 _unlink_nodes(&node
->dtree
->root
, node
);
277 static int _add_to_bottomlevel(struct dm_tree_node
*node
)
279 return _link_nodes(node
, &node
->dtree
->root
);
282 static void _remove_from_bottomlevel(struct dm_tree_node
*node
)
284 _unlink_nodes(node
, &node
->dtree
->root
);
287 static int _link_tree_nodes(struct dm_tree_node
*parent
, struct dm_tree_node
*child
)
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))
294 _remove_from_toplevel(child
);
296 if ((child
== &child
->dtree
->root
)) {
297 if (dm_tree_node_num_children(parent
, 0))
300 _remove_from_bottomlevel(parent
);
302 return _link_nodes(parent
, child
);
305 static struct dm_tree_node
*_create_dm_tree_node(struct dm_tree
*dtree
,
308 struct dm_info
*info
,
312 struct dm_tree_node
*node
;
315 if (!(node
= dm_pool_zalloc(dtree
->mem
, sizeof(*node
)))) {
316 log_error("_create_dm_tree_node alloc failed");
325 node
->context
= context
;
326 node
->udev_flags
= udev_flags
;
327 node
->activation_priority
= 0;
329 dm_list_init(&node
->uses
);
330 dm_list_init(&node
->used_by
);
331 dm_list_init(&node
->props
.segs
);
333 dev
= MKDEV(info
->major
, info
->minor
);
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
);
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
,
347 dm_pool_free(dtree
->mem
, node
);
354 static struct dm_tree_node
*_find_dm_tree_node(struct dm_tree
*dtree
,
355 uint32_t major
, uint32_t minor
)
357 uint64_t dev
= MKDEV(major
, minor
);
359 return dm_hash_lookup_binary(dtree
->devs
, (const char *) &dev
,
363 static struct dm_tree_node
*_find_dm_tree_node_by_uuid(struct dm_tree
*dtree
,
366 struct dm_tree_node
*node
;
368 if ((node
= dm_hash_lookup(dtree
->uuids
, uuid
)))
371 if (strncmp(uuid
, UUID_PREFIX
, sizeof(UUID_PREFIX
) - 1))
374 return dm_hash_lookup(dtree
->uuids
, uuid
+ sizeof(UUID_PREFIX
) - 1);
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
)
381 memset(info
, 0, sizeof(*info
));
383 if (!dm_is_dm_major(major
)) {
390 info
->live_table
= 0;
391 info
->inactive_table
= 0;
396 if (!(*dmt
= dm_task_create(DM_DEVICE_DEPS
))) {
397 log_error("deps dm_task creation failed");
401 if (!dm_task_set_major(*dmt
, major
)) {
402 log_error("_deps: failed to set major for (%" PRIu32
":%" PRIu32
")",
407 if (!dm_task_set_minor(*dmt
, minor
)) {
408 log_error("_deps: failed to set minor for (%" PRIu32
":%" PRIu32
")",
413 if (!dm_task_run(*dmt
)) {
414 log_error("_deps: task run failed for (%" PRIu32
":%" PRIu32
")",
419 if (!dm_task_get_info(*dmt
, info
)) {
420 log_error("_deps: failed to get info for (%" PRIu32
":%" PRIu32
")",
430 if (info
->major
!= major
) {
431 log_error("Inconsistent dtree major number: %u != %u",
435 if (info
->minor
!= minor
) {
436 log_error("Inconsistent dtree minor number: %u != %u",
440 if (!(*name
= dm_pool_strdup(mem
, dm_task_get_name(*dmt
)))) {
441 log_error("name pool_strdup failed");
444 if (!(*uuid
= dm_pool_strdup(mem
, dm_task_get_uuid(*dmt
)))) {
445 log_error("uuid pool_strdup failed");
448 *deps
= dm_task_get_deps(*dmt
);
454 dm_task_destroy(*dmt
);
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
,
463 struct dm_task
*dmt
= NULL
;
465 struct dm_deps
*deps
= NULL
;
466 const char *name
= NULL
;
467 const char *uuid
= NULL
;
468 struct dm_tree_node
*node
= NULL
;
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
))
477 if (!(node
= _create_dm_tree_node(dtree
, name
, uuid
, &info
,
483 if (!_link_tree_nodes(parent
, node
)) {
488 /* If node was already in tree, no need to recurse. */
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
)) {
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
)) {
511 dm_task_destroy(dmt
);
516 static int _node_clear_table(struct dm_tree_node
*dnode
)
519 struct dm_info
*info
;
523 if (!(info
= &dnode
->info
)) {
524 log_error("_node_clear_table failed: missing info");
528 if (!(name
= dm_tree_node_get_name(dnode
))) {
529 log_error("_node_clear_table failed: missing name");
533 /* Is there a table? */
534 if (!info
->exists
|| !info
->inactive_table
)
537 log_verbose("Clearing inactive table %s (%" PRIu32
":%" PRIu32
")",
538 name
, info
->major
, info
->minor
);
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
);
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
);
553 r
= dm_task_run(dmt
);
555 if (!dm_task_get_info(dmt
, info
)) {
556 log_error("_node_clear_table failed: info missing after running task for %s", name
);
560 dm_task_destroy(dmt
);
565 struct dm_tree_node
*dm_tree_add_new_dev(struct dm_tree
*dtree
,
568 uint32_t major
, uint32_t minor
,
573 struct dm_tree_node
*dnode
;
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");
584 if (!(uuid2
= dm_pool_strdup(dtree
->mem
, uuid
))) {
585 log_error("uuid pool_strdup failed");
593 info
.inactive_table
= 0;
596 if (!(dnode
= _create_dm_tree_node(dtree
, name2
, uuid2
, &info
,
600 /* Attach to root node until a table is supplied */
601 if (!_add_to_toplevel(dnode
) || !_add_to_bottomlevel(dnode
))
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");
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;
620 if (clear_inactive
&& !_node_clear_table(dnode
))
623 dnode
->context
= context
;
624 dnode
->udev_flags
= 0;
629 struct dm_tree_node
*dm_tree_add_new_dev_with_udev_flags(struct dm_tree
*dtree
,
639 struct dm_tree_node
*node
;
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
;
649 void dm_tree_node_set_read_ahead(struct dm_tree_node
*dnode
,
651 uint32_t read_ahead_flags
)
653 dnode
->props
.read_ahead
= read_ahead
;
654 dnode
->props
.read_ahead_flags
= read_ahead_flags
;
657 int dm_tree_add_dev(struct dm_tree
*dtree
, uint32_t major
, uint32_t minor
)
659 return _add_dev(dtree
, &dtree
->root
, major
, minor
, 0) ? 1 : 0;
662 int dm_tree_add_dev_with_udev_flags(struct dm_tree
*dtree
, uint32_t major
,
663 uint32_t minor
, uint16_t udev_flags
)
665 return _add_dev(dtree
, &dtree
->root
, major
, minor
, udev_flags
) ? 1 : 0;
668 const char *dm_tree_node_get_name(const struct dm_tree_node
*node
)
670 return node
->info
.exists
? node
->name
: "";
673 const char *dm_tree_node_get_uuid(const struct dm_tree_node
*node
)
675 return node
->info
.exists
? node
->uuid
: "";
678 const struct dm_info
*dm_tree_node_get_info(const struct dm_tree_node
*node
)
683 void *dm_tree_node_get_context(const struct dm_tree_node
*node
)
685 return node
->context
;
688 int dm_tree_node_size_changed(const struct dm_tree_node
*dnode
)
690 return dnode
->props
.size_changed
;
693 int dm_tree_node_num_children(const struct dm_tree_node
*node
, uint32_t inverted
)
696 if (_nodes_are_linked(&node
->dtree
->root
, node
))
698 return dm_list_size(&node
->used_by
);
701 if (_nodes_are_linked(node
, &node
->dtree
->root
))
704 return dm_list_size(&node
->uses
);
708 * Returns 1 if no prefix supplied
710 static int _uuid_prefix_matches(const char *uuid
, const char *uuid_prefix
, size_t uuid_prefix_len
)
715 if (!strncmp(uuid
, uuid_prefix
, uuid_prefix_len
))
718 /* Handle transition: active device uuids might be missing the prefix */
719 if (uuid_prefix_len
<= 4)
722 if (!strncmp(uuid
, UUID_PREFIX
, sizeof(UUID_PREFIX
) - 1))
725 if (strncmp(uuid_prefix
, UUID_PREFIX
, sizeof(UUID_PREFIX
) - 1))
728 if (!strncmp(uuid
, uuid_prefix
+ sizeof(UUID_PREFIX
) - 1, uuid_prefix_len
- (sizeof(UUID_PREFIX
) - 1)))
735 * Returns 1 if no children.
737 static int _children_suspended(struct dm_tree_node
*node
,
739 const char *uuid_prefix
,
740 size_t uuid_prefix_len
)
742 struct dm_list
*list
;
743 struct dm_tree_link
*dlink
;
744 const struct dm_info
*dinfo
;
748 if (_nodes_are_linked(&node
->dtree
->root
, node
))
750 list
= &node
->used_by
;
752 if (_nodes_are_linked(node
, &node
->dtree
->root
))
757 dm_list_iterate_items(dlink
, list
) {
758 if (!(uuid
= dm_tree_node_get_uuid(dlink
->node
))) {
763 /* Ignore if it doesn't belong to this VG */
764 if (!_uuid_prefix_matches(uuid
, uuid_prefix
, uuid_prefix_len
))
767 if (!(dinfo
= dm_tree_node_get_info(dlink
->node
))) {
768 stack
; /* FIXME Is this normal? */
772 if (!dinfo
->suspended
)
780 * Set major and minor to zero for root of tree.
782 struct dm_tree_node
*dm_tree_find_node(struct dm_tree
*dtree
,
786 if (!major
&& !minor
)
789 return _find_dm_tree_node(dtree
, major
, minor
);
793 * Set uuid to NULL for root of tree.
795 struct dm_tree_node
*dm_tree_find_node_by_uuid(struct dm_tree
*dtree
,
801 return _find_dm_tree_node_by_uuid(dtree
, uuid
);
805 * First time set *handle to NULL.
806 * Set inverted to invert the tree.
808 struct dm_tree_node
*dm_tree_next_child(void **handle
,
809 const struct dm_tree_node
*parent
,
812 struct dm_list
**dlink
= (struct dm_list
**) handle
;
813 const struct dm_list
*use_list
;
816 use_list
= &parent
->used_by
;
818 use_list
= &parent
->uses
;
821 *dlink
= dm_list_first(use_list
);
823 *dlink
= dm_list_next(use_list
, *dlink
);
825 return (*dlink
) ? dm_list_item(*dlink
, struct dm_tree_link
)->node
: NULL
;
829 * Deactivate a device with its dependencies if the uuid prefix matches.
831 static int _info_by_dev(uint32_t major
, uint32_t minor
, int with_open_count
,
832 struct dm_info
*info
)
837 if (!(dmt
= dm_task_create(DM_DEVICE_INFO
))) {
838 log_error("_info_by_dev: dm_task creation failed");
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
);
848 if (!with_open_count
&& !dm_task_no_open_count(dmt
))
849 log_error("Failed to disable open_count");
851 if ((r
= dm_task_run(dmt
)))
852 r
= dm_task_get_info(dmt
, info
);
854 dm_task_destroy(dmt
);
859 static int _deactivate_node(const char *name
, uint32_t major
, uint32_t minor
,
860 uint32_t *cookie
, uint16_t udev_flags
)
865 log_verbose("Removing %s (%" PRIu32
":%" PRIu32
")", name
, major
, minor
);
867 if (!(dmt
= dm_task_create(DM_DEVICE_REMOVE
))) {
868 log_error("Deactivation dm_task creation failed for %s", name
);
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
);
877 if (!dm_task_no_open_count(dmt
))
878 log_error("Failed to disable open_count");
880 if (!dm_task_set_cookie(dmt
, cookie
, udev_flags
))
883 r
= dm_task_run(dmt
);
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
));
889 /* FIXME Remove node from tree or mark invalid? */
892 dm_task_destroy(dmt
);
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
)
903 log_verbose("Renaming %s (%" PRIu32
":%" PRIu32
") to %s", old_name
, major
, minor
, new_name
);
905 if (!(dmt
= dm_task_create(DM_DEVICE_RENAME
))) {
906 log_error("Rename dm_task creation failed for %s", old_name
);
910 if (!dm_task_set_name(dmt
, old_name
)) {
911 log_error("Failed to set name for %s rename.", old_name
);
915 if (!dm_task_set_newname(dmt
, new_name
))
918 if (!dm_task_no_open_count(dmt
))
919 log_error("Failed to disable open_count");
921 if (!dm_task_set_cookie(dmt
, cookie
, udev_flags
))
924 r
= dm_task_run(dmt
);
927 dm_task_destroy(dmt
);
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
,
941 log_verbose("Resuming %s (%" PRIu32
":%" PRIu32
")", name
, major
, minor
);
943 if (!(dmt
= dm_task_create(DM_DEVICE_RESUME
))) {
944 log_error("Suspend dm_task creation failed for %s", name
);
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
);
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
);
959 if (!dm_task_no_open_count(dmt
))
960 log_error("Failed to disable open_count");
962 if (!dm_task_set_read_ahead(dmt
, read_ahead
, read_ahead_flags
))
963 log_error("Failed to set read ahead");
965 if (!dm_task_set_cookie(dmt
, cookie
, udev_flags
))
968 if ((r
= dm_task_run(dmt
)))
969 r
= dm_task_get_info(dmt
, newinfo
);
972 dm_task_destroy(dmt
);
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
)
983 log_verbose("Suspending %s (%" PRIu32
":%" PRIu32
")%s%s",
985 skip_lockfs
? "" : " with filesystem sync",
986 no_flush
? "" : " with device flush");
988 if (!(dmt
= dm_task_create(DM_DEVICE_SUSPEND
))) {
989 log_error("Suspend dm_task creation failed for %s", name
);
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
);
999 if (!dm_task_no_open_count(dmt
))
1000 log_error("Failed to disable open_count");
1002 if (skip_lockfs
&& !dm_task_skip_lockfs(dmt
))
1003 log_error("Failed to set skip_lockfs flag.");
1005 if (no_flush
&& !dm_task_no_flush(dmt
))
1006 log_error("Failed to set no_flush flag.");
1008 if ((r
= dm_task_run(dmt
)))
1009 r
= dm_task_get_info(dmt
, newinfo
);
1011 dm_task_destroy(dmt
);
1016 int dm_tree_deactivate_children(struct dm_tree_node
*dnode
,
1017 const char *uuid_prefix
,
1018 size_t uuid_prefix_len
)
1021 void *handle
= NULL
;
1022 struct dm_tree_node
*child
= dnode
;
1023 struct dm_info info
;
1024 const struct dm_info
*dinfo
;
1028 while ((child
= dm_tree_next_child(&handle
, dnode
, 0))) {
1029 if (!(dinfo
= dm_tree_node_get_info(child
))) {
1034 if (!(name
= dm_tree_node_get_name(child
))) {
1039 if (!(uuid
= dm_tree_node_get_uuid(child
))) {
1044 /* Ignore if it doesn't belong to this VG */
1045 if (!_uuid_prefix_matches(uuid
, uuid_prefix
, uuid_prefix_len
))
1048 /* Refresh open_count */
1049 if (!_info_by_dev(dinfo
->major
, dinfo
->minor
, 1, &info
) ||
1053 if (info
.open_count
) {
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
,
1067 if (dm_tree_node_num_children(child
, 0))
1068 if (!dm_tree_deactivate_children(child
, uuid_prefix
, uuid_prefix_len
))
1075 void dm_tree_skip_lockfs(struct dm_tree_node
*dnode
)
1077 dnode
->dtree
->skip_lockfs
= 1;
1080 void dm_tree_use_no_flush_suspend(struct dm_tree_node
*dnode
)
1082 dnode
->dtree
->no_flush
= 1;
1085 int dm_tree_suspend_children(struct dm_tree_node
*dnode
,
1086 const char *uuid_prefix
,
1087 size_t uuid_prefix_len
)
1090 void *handle
= NULL
;
1091 struct dm_tree_node
*child
= dnode
;
1092 struct dm_info info
, newinfo
;
1093 const struct dm_info
*dinfo
;
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
))) {
1104 if (!(name
= dm_tree_node_get_name(child
))) {
1109 if (!(uuid
= dm_tree_node_get_uuid(child
))) {
1114 /* Ignore if it doesn't belong to this VG */
1115 if (!_uuid_prefix_matches(uuid
, uuid_prefix
, uuid_prefix_len
))
1118 /* Ensure immediate parents are already suspended */
1119 if (!_children_suspended(child
, 1, uuid_prefix
, uuid_prefix_len
))
1122 if (!_info_by_dev(dinfo
->major
, dinfo
->minor
, 0, &info
) ||
1123 !info
.exists
|| info
.suspended
)
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
,
1136 /* Update cached info */
1137 child
->info
= newinfo
;
1140 /* Then suspend any child nodes */
1143 while ((child
= dm_tree_next_child(&handle
, dnode
, 0))) {
1144 if (!(uuid
= dm_tree_node_get_uuid(child
))) {
1149 /* Ignore if it doesn't belong to this VG */
1150 if (!_uuid_prefix_matches(uuid
, uuid_prefix
, uuid_prefix_len
))
1153 if (dm_tree_node_num_children(child
, 0))
1154 if (!dm_tree_suspend_children(child
, uuid_prefix
, uuid_prefix_len
))
1161 int dm_tree_activate_children(struct dm_tree_node
*dnode
,
1162 const char *uuid_prefix
,
1163 size_t uuid_prefix_len
)
1166 void *handle
= NULL
;
1167 struct dm_tree_node
*child
= dnode
;
1168 struct dm_info newinfo
;
1173 /* Activate children first */
1174 while ((child
= dm_tree_next_child(&handle
, dnode
, 0))) {
1175 if (!(uuid
= dm_tree_node_get_uuid(child
))) {
1180 if (!_uuid_prefix_matches(uuid
, uuid_prefix
, uuid_prefix_len
))
1183 if (dm_tree_node_num_children(child
, 0))
1184 if (!dm_tree_activate_children(child
, uuid_prefix
, uuid_prefix_len
))
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
))) {
1197 if (!_uuid_prefix_matches(uuid
, uuid_prefix
, uuid_prefix_len
))
1200 if (priority
!= child
->activation_priority
)
1203 if (!(name
= dm_tree_node_get_name(child
))) {
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
);
1218 child
->name
= child
->props
.new_name
;
1219 child
->props
.new_name
= NULL
;
1222 if (!child
->info
.inactive_table
&& !child
->info
.suspended
)
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
,
1235 /* Update cached info */
1236 child
->info
= newinfo
;
1245 static int _create_node(struct dm_tree_node
*dnode
)
1248 struct dm_task
*dmt
;
1250 log_verbose("Creating %s", dnode
->name
);
1252 if (!(dmt
= dm_task_create(DM_DEVICE_CREATE
))) {
1253 log_error("Create dm_task creation failed for %s", dnode
->name
);
1257 if (!dm_task_set_name(dmt
, dnode
->name
)) {
1258 log_error("Failed to set device name for %s", dnode
->name
);
1262 if (!dm_task_set_uuid(dmt
, dnode
->uuid
)) {
1263 log_error("Failed to set uuid for %s", dnode
->name
);
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
);
1274 if (dnode
->props
.read_only
&& !dm_task_set_ro(dmt
)) {
1275 log_error("Failed to set read only flag for %s", dnode
->name
);
1279 if (!dm_task_no_open_count(dmt
))
1280 log_error("Failed to disable open_count");
1282 if ((r
= dm_task_run(dmt
)))
1283 r
= dm_task_get_info(dmt
, &dnode
->info
);
1286 dm_task_destroy(dmt
);
1292 static int _build_dev_string(char *devbuf
, size_t bufsize
, struct dm_tree_node
*node
)
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 "
1297 node
->name
, node
->uuid
, node
->info
.major
, node
->info
.minor
);
1304 /* simplify string emiting code */
1305 #define EMIT_PARAMS(p, str...)\
1308 if ((w = dm_snprintf(params + p, paramsize - (size_t) p, str)) < 0) {\
1309 stack; /* Out of space */\
1318 * Returns: 1 on success, 0 on failure
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
)
1324 struct seg_area
*area
;
1325 char devbuf
[DM_FORMAT_DEV_BUFSIZE
];
1326 unsigned first_time
= 1;
1328 dm_list_iterate_items(area
, &seg
->areas
) {
1329 if (!_build_dev_string(devbuf
, sizeof(devbuf
), area
->dev_node
))
1332 EMIT_PARAMS(*pos
, "%s%s %" PRIu64
, first_time
? "" : " ",
1333 devbuf
, area
->offset
);
1342 * Returns: 1 on success, 0 on failure
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
,
1350 int block_on_error
= 0;
1351 int handle_errors
= 0;
1352 int dm_log_userspace
= 0;
1354 unsigned log_parm_count
;
1356 char logbuf
[DM_FORMAT_DEV_BUFSIZE
];
1357 const char *logtype
;
1363 if ((seg
->flags
& DM_BLOCK_ON_ERROR
)) {
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).
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).
1374 if (strncmp(uts
.release
, "2.6.22", 6) >= 0)
1380 if (seg
->clustered
) {
1381 /* Cluster mirrors require a UUID */
1386 * Cluster mirrors used to have their own log
1387 * types. Now they are accessed through the
1388 * userspace log type.
1390 * The dm-log-userspace module was added to the
1393 if (strncmp(uts
.release
, "2.6.31", 6) >= 0)
1394 dm_log_userspace
= 1;
1400 /* [no]sync, block_on_error etc. */
1401 log_parm_count
+= hweight32(seg
->flags
);
1403 /* "handle_errors" is a feature arg now */
1407 /* DM_CORELOG does not count in the param list */
1408 if (seg
->flags
& DM_CORELOG
)
1411 if (seg
->clustered
) {
1412 log_parm_count
++; /* For UUID */
1414 if (!dm_log_userspace
)
1415 EMIT_PARAMS(pos
, "clustered-");
1417 /* For clustered-* type field inserted later */
1426 if (!_build_dev_string(logbuf
, sizeof(logbuf
), seg
->log
))
1430 if (dm_log_userspace
)
1431 EMIT_PARAMS(pos
, "userspace %u %s clustered-%s",
1432 log_parm_count
, seg
->uuid
, logtype
);
1434 EMIT_PARAMS(pos
, "%s %u", logtype
, log_parm_count
);
1437 EMIT_PARAMS(pos
, " %s", logbuf
);
1439 EMIT_PARAMS(pos
, " %u", seg
->region_size
);
1441 if (seg
->clustered
&& !dm_log_userspace
)
1442 EMIT_PARAMS(pos
, " %s", seg
->uuid
);
1444 if ((seg
->flags
& DM_NOSYNC
))
1445 EMIT_PARAMS(pos
, " nosync");
1446 else if ((seg
->flags
& DM_FORCESYNC
))
1447 EMIT_PARAMS(pos
, " sync");
1450 EMIT_PARAMS(pos
, " block_on_error");
1452 EMIT_PARAMS(pos
, " %u ", seg
->mirror_area_count
);
1454 if ((r
= _emit_areas_line(dmt
, seg
, params
, paramsize
, &pos
)) <= 0)
1458 EMIT_PARAMS(pos
, " 1 handle_errors");
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
,
1470 char originbuf
[DM_FORMAT_DEV_BUFSIZE
], cowbuf
[DM_FORMAT_DEV_BUFSIZE
];
1478 /* Mirrors are pretty complicated - now in separate function */
1479 r
= _mirror_emit_segment_line(dmt
, major
, minor
, seg
, seg_start
,
1485 case SEG_SNAPSHOT_MERGE
:
1486 if (!_build_dev_string(originbuf
, sizeof(originbuf
), seg
->origin
))
1488 if (!_build_dev_string(cowbuf
, sizeof(cowbuf
), seg
->cow
))
1490 EMIT_PARAMS(pos
, "%s %s %c %d", originbuf
, cowbuf
,
1491 seg
->persistent
? 'P' : 'N', seg
->chunk_size
);
1493 case SEG_SNAPSHOT_ORIGIN
:
1494 if (!_build_dev_string(originbuf
, sizeof(originbuf
), seg
->origin
))
1496 EMIT_PARAMS(pos
, "%s", originbuf
);
1499 EMIT_PARAMS(pos
, "%u %u ", seg
->area_count
, seg
->stripe_size
);
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
);
1513 case SEG_SNAPSHOT_ORIGIN
:
1514 case SEG_SNAPSHOT_MERGE
:
1520 if ((r
= _emit_areas_line(dmt
, seg
, params
, paramsize
, &pos
)) <= 0) {
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
);
1531 if (!dm_task_add_target(dmt
, *seg_start
, seg
->size
, dm_segtypes
[seg
->type
].target
, params
))
1534 *seg_start
+= seg
->size
;
1541 static int _emit_segment(struct dm_task
*dmt
, uint32_t major
, uint32_t minor
,
1542 struct load_segment
*seg
, uint64_t *seg_start
)
1545 size_t paramsize
= 4096;
1549 if (!(params
= dm_malloc(paramsize
))) {
1550 log_error("Insufficient space for target parameters.");
1555 ret
= _emit_segment_line(dmt
, major
, minor
, seg
, seg_start
,
1565 log_debug("Insufficient space in params[%" PRIsize_t
1566 "] for target parameters.", paramsize
);
1569 } while (paramsize
< MAX_TARGET_PARAMSIZE
);
1571 log_error("Target parameter size too big. Aborting.");
1575 static int _load_node(struct dm_tree_node
*dnode
)
1578 struct dm_task
*dmt
;
1579 struct load_segment
*seg
;
1580 uint64_t seg_start
= 0;
1582 log_verbose("Loading %s table (%" PRIu32
":%" PRIu32
")", dnode
->name
,
1583 dnode
->info
.major
, dnode
->info
.minor
);
1585 if (!(dmt
= dm_task_create(DM_DEVICE_RELOAD
))) {
1586 log_error("Reload dm_task creation failed for %s", dnode
->name
);
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
);
1596 if (dnode
->props
.read_only
&& !dm_task_set_ro(dmt
)) {
1597 log_error("Failed to set read only flag for %s", dnode
->name
);
1601 if (!dm_task_no_open_count(dmt
))
1602 log_error("Failed to disable open_count");
1604 dm_list_iterate_items(seg
, &dnode
->props
.segs
)
1605 if (!_emit_segment(dmt
, dnode
->info
.major
, dnode
->info
.minor
,
1609 if (!dm_task_suppress_identical_reload(dmt
))
1610 log_error("Failed to suppress reload of identical tables.");
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.",
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 %"
1622 dm_task_get_existing_table_size(dmt
),
1623 seg_start
, dnode
->name
);
1626 dnode
->props
.segment_count
= 0;
1629 dm_task_destroy(dmt
);
1634 int dm_tree_preload_children(struct dm_tree_node
*dnode
,
1635 const char *uuid_prefix
,
1636 size_t uuid_prefix_len
)
1639 void *handle
= NULL
;
1640 struct dm_tree_node
*child
;
1641 struct dm_info newinfo
;
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
)
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
))
1654 if (dm_tree_node_num_children(child
, 0))
1655 if (!dm_tree_preload_children(child
, uuid_prefix
, uuid_prefix_len
))
1658 /* FIXME Cope if name exists with no uuid? */
1659 if (!child
->info
.exists
) {
1660 if (!_create_node(child
)) {
1666 if (!child
->info
.inactive_table
&& child
->props
.segment_count
) {
1667 if (!_load_node(child
)) {
1673 /* Propagate device size change change */
1674 if (child
->props
.size_changed
)
1675 dnode
->props
.size_changed
= 1;
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
)
1681 if (!child
->info
.inactive_table
&& !child
->info
.suspended
)
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
,
1694 /* Update cached info */
1695 child
->info
= newinfo
;
1704 * Returns 1 if unsure.
1706 int dm_tree_children_use_uuid(struct dm_tree_node
*dnode
,
1707 const char *uuid_prefix
,
1708 size_t uuid_prefix_len
)
1710 void *handle
= NULL
;
1711 struct dm_tree_node
*child
= dnode
;
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.");
1720 if (_uuid_prefix_matches(uuid
, uuid_prefix
, uuid_prefix_len
))
1723 if (dm_tree_node_num_children(child
, 0))
1724 dm_tree_children_use_uuid(child
, uuid_prefix
, uuid_prefix_len
);
1733 static struct load_segment
*_add_segment(struct dm_tree_node
*dnode
, unsigned type
, uint64_t size
)
1735 struct load_segment
*seg
;
1737 if (!(seg
= dm_pool_zalloc(dnode
->dtree
->mem
, sizeof(*seg
)))) {
1738 log_error("dtree node segment allocation failed");
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;
1753 dm_list_add(&dnode
->props
.segs
, &seg
->list
);
1754 dnode
->props
.segment_count
++;
1759 int dm_tree_node_add_snapshot_origin_target(struct dm_tree_node
*dnode
,
1761 const char *origin_uuid
)
1763 struct load_segment
*seg
;
1764 struct dm_tree_node
*origin_node
;
1766 if (!(seg
= _add_segment(dnode
, SEG_SNAPSHOT_ORIGIN
, size
)))
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
);
1774 seg
->origin
= origin_node
;
1775 if (!_link_tree_nodes(dnode
, origin_node
))
1778 /* Resume snapshot origins after new snapshots */
1779 dnode
->activation_priority
= 1;
1784 static int _add_snapshot_target(struct dm_tree_node
*node
,
1786 const char *origin_uuid
,
1787 const char *cow_uuid
,
1788 const char *merge_uuid
,
1790 uint32_t chunk_size
)
1792 struct load_segment
*seg
;
1793 struct dm_tree_node
*origin_node
, *cow_node
, *merge_node
;
1796 seg_type
= !merge_uuid
? SEG_SNAPSHOT
: SEG_SNAPSHOT_MERGE
;
1798 if (!(seg
= _add_segment(node
, seg_type
, size
)))
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
);
1806 seg
->origin
= origin_node
;
1807 if (!_link_tree_nodes(node
, origin_node
))
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
);
1815 seg
->cow
= cow_node
;
1816 if (!_link_tree_nodes(node
, cow_node
))
1819 seg
->persistent
= persistent
? 1 : 0;
1820 seg
->chunk_size
= chunk_size
;
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
);
1827 seg
->merge
= merge_node
;
1828 /* must not link merging snapshot, would undermine activation_priority below */
1831 /* Resume snapshot-merge (acting origin) after other snapshots */
1832 node
->activation_priority
= 1;
1834 /* Resume merging snapshot after snapshot-merge */
1835 seg
->merge
->activation_priority
= 2;
1843 int dm_tree_node_add_snapshot_target(struct dm_tree_node
*node
,
1845 const char *origin_uuid
,
1846 const char *cow_uuid
,
1848 uint32_t chunk_size
)
1850 return _add_snapshot_target(node
, size
, origin_uuid
, cow_uuid
,
1851 NULL
, persistent
, chunk_size
);
1854 int dm_tree_node_add_snapshot_merge_target(struct dm_tree_node
*node
,
1856 const char *origin_uuid
,
1857 const char *cow_uuid
,
1858 const char *merge_uuid
,
1859 uint32_t chunk_size
)
1861 return _add_snapshot_target(node
, size
, origin_uuid
, cow_uuid
,
1862 merge_uuid
, 1, chunk_size
);
1865 int dm_tree_node_add_error_target(struct dm_tree_node
*node
,
1868 if (!_add_segment(node
, SEG_ERROR
, size
))
1874 int dm_tree_node_add_zero_target(struct dm_tree_node
*node
,
1877 if (!_add_segment(node
, SEG_ZERO
, size
))
1883 int dm_tree_node_add_linear_target(struct dm_tree_node
*node
,
1886 if (!_add_segment(node
, SEG_LINEAR
, size
))
1892 int dm_tree_node_add_striped_target(struct dm_tree_node
*node
,
1894 uint32_t stripe_size
)
1896 struct load_segment
*seg
;
1898 if (!(seg
= _add_segment(node
, SEG_STRIPED
, size
)))
1901 seg
->stripe_size
= stripe_size
;
1906 int dm_tree_node_add_crypt_target(struct dm_tree_node
*node
,
1909 const char *chainmode
,
1914 struct load_segment
*seg
;
1916 if (!(seg
= _add_segment(node
, SEG_CRYPT
, size
)))
1919 seg
->cipher
= cipher
;
1920 seg
->chainmode
= chainmode
;
1922 seg
->iv_offset
= iv_offset
;
1928 int dm_tree_node_add_mirror_target_log(struct dm_tree_node
*node
,
1929 uint32_t region_size
,
1931 const char *log_uuid
,
1932 unsigned area_count
,
1935 struct dm_tree_node
*log_node
= NULL
;
1936 struct load_segment
*seg
;
1938 if (!node
->props
.segment_count
) {
1939 log_error(INTERNAL_ERROR
"Attempt to add target area to missing segment.");
1943 seg
= dm_list_item(dm_list_last(&node
->props
.segs
), struct load_segment
);
1946 if (!(seg
->uuid
= dm_pool_strdup(node
->dtree
->mem
, log_uuid
))) {
1947 log_error("log uuid pool_strdup failed");
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
);
1956 if (!_link_tree_nodes(node
, log_node
))
1961 seg
->log
= log_node
;
1962 seg
->region_size
= region_size
;
1963 seg
->clustered
= clustered
;
1964 seg
->mirror_area_count
= area_count
;
1970 int dm_tree_node_add_mirror_target(struct dm_tree_node
*node
,
1973 struct load_segment
*seg
;
1975 if (!(seg
= _add_segment(node
, SEG_MIRRORED
, size
)))
1981 static int _add_area(struct dm_tree_node
*node
, struct load_segment
*seg
, struct dm_tree_node
*dev_node
, uint64_t offset
)
1983 struct seg_area
*area
;
1985 if (!(area
= dm_pool_zalloc(node
->dtree
->mem
, sizeof (*area
)))) {
1986 log_error("Failed to allocate target segment area.");
1990 area
->dev_node
= dev_node
;
1991 area
->offset
= offset
;
1993 dm_list_add(&seg
->areas
, &area
->list
);
1999 int dm_tree_node_add_target_area(struct dm_tree_node
*node
,
2000 const char *dev_name
,
2004 struct load_segment
*seg
;
2006 struct dm_tree_node
*dev_node
;
2008 if ((!dev_name
|| !*dev_name
) && (!uuid
|| !*uuid
)) {
2009 log_error("dm_tree_node_add_target_area called without device");
2014 if (!(dev_node
= dm_tree_find_node_by_uuid(node
->dtree
, uuid
))) {
2015 log_error("Couldn't find area uuid %s.", uuid
);
2018 if (!_link_tree_nodes(node
, dev_node
))
2021 if (stat(dev_name
, &info
) < 0) {
2022 log_error("Device %s not found.", dev_name
);
2026 if (!S_ISBLK(info
.st_mode
)) {
2027 log_error("Device %s is not a block device.", dev_name
);
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)))
2037 if (!node
->props
.segment_count
) {
2038 log_error(INTERNAL_ERROR
"Attempt to add target area to missing segment.");
2042 seg
= dm_list_item(dm_list_last(&node
->props
.segs
), struct load_segment
);
2044 if (!_add_area(node
, seg
, dev_node
, offset
))
2050 void dm_tree_set_cookie(struct dm_tree_node
*node
, uint32_t cookie
)
2052 node
->dtree
->cookie
= cookie
;
2055 uint32_t dm_tree_get_cookie(struct dm_tree_node
*node
)
2057 return node
->dtree
->cookie
;