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
) ||
1050 !info
.exists
|| info
.open_count
)
1053 if (!_deactivate_node(name
, info
.major
, info
.minor
,
1054 &child
->dtree
->cookie
, child
->udev_flags
)) {
1055 log_error("Unable to deactivate %s (%" PRIu32
1056 ":%" PRIu32
")", name
, info
.major
,
1062 if (dm_tree_node_num_children(child
, 0))
1063 if (!dm_tree_deactivate_children(child
, uuid_prefix
, uuid_prefix_len
))
1070 void dm_tree_skip_lockfs(struct dm_tree_node
*dnode
)
1072 dnode
->dtree
->skip_lockfs
= 1;
1075 void dm_tree_use_no_flush_suspend(struct dm_tree_node
*dnode
)
1077 dnode
->dtree
->no_flush
= 1;
1080 int dm_tree_suspend_children(struct dm_tree_node
*dnode
,
1081 const char *uuid_prefix
,
1082 size_t uuid_prefix_len
)
1085 void *handle
= NULL
;
1086 struct dm_tree_node
*child
= dnode
;
1087 struct dm_info info
, newinfo
;
1088 const struct dm_info
*dinfo
;
1092 /* Suspend nodes at this level of the tree */
1093 while ((child
= dm_tree_next_child(&handle
, dnode
, 0))) {
1094 if (!(dinfo
= dm_tree_node_get_info(child
))) {
1099 if (!(name
= dm_tree_node_get_name(child
))) {
1104 if (!(uuid
= dm_tree_node_get_uuid(child
))) {
1109 /* Ignore if it doesn't belong to this VG */
1110 if (!_uuid_prefix_matches(uuid
, uuid_prefix
, uuid_prefix_len
))
1113 /* Ensure immediate parents are already suspended */
1114 if (!_children_suspended(child
, 1, uuid_prefix
, uuid_prefix_len
))
1117 if (!_info_by_dev(dinfo
->major
, dinfo
->minor
, 0, &info
) ||
1118 !info
.exists
|| info
.suspended
)
1121 if (!_suspend_node(name
, info
.major
, info
.minor
,
1122 child
->dtree
->skip_lockfs
,
1123 child
->dtree
->no_flush
, &newinfo
)) {
1124 log_error("Unable to suspend %s (%" PRIu32
1125 ":%" PRIu32
")", name
, info
.major
,
1131 /* Update cached info */
1132 child
->info
= newinfo
;
1135 /* Then suspend any child nodes */
1138 while ((child
= dm_tree_next_child(&handle
, dnode
, 0))) {
1139 if (!(uuid
= dm_tree_node_get_uuid(child
))) {
1144 /* Ignore if it doesn't belong to this VG */
1145 if (!_uuid_prefix_matches(uuid
, uuid_prefix
, uuid_prefix_len
))
1148 if (dm_tree_node_num_children(child
, 0))
1149 if (!dm_tree_suspend_children(child
, uuid_prefix
, uuid_prefix_len
))
1156 int dm_tree_activate_children(struct dm_tree_node
*dnode
,
1157 const char *uuid_prefix
,
1158 size_t uuid_prefix_len
)
1161 void *handle
= NULL
;
1162 struct dm_tree_node
*child
= dnode
;
1163 struct dm_info newinfo
;
1168 /* Activate children first */
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 (dm_tree_node_num_children(child
, 0))
1179 if (!dm_tree_activate_children(child
, uuid_prefix
, uuid_prefix_len
))
1185 for (priority
= 0; priority
< 3; priority
++) {
1186 while ((child
= dm_tree_next_child(&handle
, dnode
, 0))) {
1187 if (!(uuid
= dm_tree_node_get_uuid(child
))) {
1192 if (!_uuid_prefix_matches(uuid
, uuid_prefix
, uuid_prefix_len
))
1195 if (priority
!= child
->activation_priority
)
1198 if (!(name
= dm_tree_node_get_name(child
))) {
1204 if (child
->props
.new_name
) {
1205 if (!_rename_node(name
, child
->props
.new_name
, child
->info
.major
,
1206 child
->info
.minor
, &child
->dtree
->cookie
,
1207 child
->udev_flags
)) {
1208 log_error("Failed to rename %s (%" PRIu32
1209 ":%" PRIu32
") to %s", name
, child
->info
.major
,
1210 child
->info
.minor
, child
->props
.new_name
);
1213 child
->name
= child
->props
.new_name
;
1214 child
->props
.new_name
= NULL
;
1217 if (!child
->info
.inactive_table
&& !child
->info
.suspended
)
1220 if (!_resume_node(child
->name
, child
->info
.major
, child
->info
.minor
,
1221 child
->props
.read_ahead
, child
->props
.read_ahead_flags
,
1222 &newinfo
, &child
->dtree
->cookie
, child
->udev_flags
)) {
1223 log_error("Unable to resume %s (%" PRIu32
1224 ":%" PRIu32
")", child
->name
, child
->info
.major
,
1230 /* Update cached info */
1231 child
->info
= newinfo
;
1240 static int _create_node(struct dm_tree_node
*dnode
)
1243 struct dm_task
*dmt
;
1245 log_verbose("Creating %s", dnode
->name
);
1247 if (!(dmt
= dm_task_create(DM_DEVICE_CREATE
))) {
1248 log_error("Create dm_task creation failed for %s", dnode
->name
);
1252 if (!dm_task_set_name(dmt
, dnode
->name
)) {
1253 log_error("Failed to set device name for %s", dnode
->name
);
1257 if (!dm_task_set_uuid(dmt
, dnode
->uuid
)) {
1258 log_error("Failed to set uuid for %s", dnode
->name
);
1262 if (dnode
->props
.major
&&
1263 (!dm_task_set_major(dmt
, dnode
->props
.major
) ||
1264 !dm_task_set_minor(dmt
, dnode
->props
.minor
))) {
1265 log_error("Failed to set device number for %s creation.", dnode
->name
);
1269 if (dnode
->props
.read_only
&& !dm_task_set_ro(dmt
)) {
1270 log_error("Failed to set read only flag for %s", dnode
->name
);
1274 if (!dm_task_no_open_count(dmt
))
1275 log_error("Failed to disable open_count");
1277 if ((r
= dm_task_run(dmt
)))
1278 r
= dm_task_get_info(dmt
, &dnode
->info
);
1281 dm_task_destroy(dmt
);
1287 static int _build_dev_string(char *devbuf
, size_t bufsize
, struct dm_tree_node
*node
)
1289 if (!dm_format_dev(devbuf
, bufsize
, node
->info
.major
, node
->info
.minor
)) {
1290 log_error("Failed to format %s device number for %s as dm "
1292 node
->name
, node
->uuid
, node
->info
.major
, node
->info
.minor
);
1299 /* simplify string emiting code */
1300 #define EMIT_PARAMS(p, str...)\
1303 if ((w = dm_snprintf(params + p, paramsize - (size_t) p, str)) < 0) {\
1304 stack; /* Out of space */\
1313 * Returns: 1 on success, 0 on failure
1315 static int _emit_areas_line(struct dm_task
*dmt
__attribute((unused
)),
1316 struct load_segment
*seg
, char *params
,
1317 size_t paramsize
, int *pos
)
1319 struct seg_area
*area
;
1320 char devbuf
[DM_FORMAT_DEV_BUFSIZE
];
1321 unsigned first_time
= 1;
1323 dm_list_iterate_items(area
, &seg
->areas
) {
1324 if (!_build_dev_string(devbuf
, sizeof(devbuf
), area
->dev_node
))
1327 EMIT_PARAMS(*pos
, "%s%s %" PRIu64
, first_time
? "" : " ",
1328 devbuf
, area
->offset
);
1337 * Returns: 1 on success, 0 on failure
1339 static int _mirror_emit_segment_line(struct dm_task
*dmt
, uint32_t major
,
1340 uint32_t minor
, struct load_segment
*seg
,
1341 uint64_t *seg_start
, char *params
,
1345 int block_on_error
= 0;
1346 int handle_errors
= 0;
1347 int dm_log_userspace
= 0;
1349 unsigned log_parm_count
;
1351 char logbuf
[DM_FORMAT_DEV_BUFSIZE
];
1352 const char *logtype
;
1358 if ((seg
->flags
& DM_BLOCK_ON_ERROR
)) {
1360 * Originally, block_on_error was an argument to the log
1361 * portion of the mirror CTR table. It was renamed to
1362 * "handle_errors" and now resides in the 'features'
1363 * section of the mirror CTR table (i.e. at the end).
1365 * We can identify whether to use "block_on_error" or
1366 * "handle_errors" by the dm-mirror module's version
1367 * number (>= 1.12) or by the kernel version (>= 2.6.22).
1369 if (strncmp(uts
.release
, "2.6.22", 6) >= 0)
1375 if (seg
->clustered
) {
1376 /* Cluster mirrors require a UUID */
1381 * Cluster mirrors used to have their own log
1382 * types. Now they are accessed through the
1383 * userspace log type.
1385 * The dm-log-userspace module was added to the
1388 if (strncmp(uts
.release
, "2.6.31", 6) >= 0)
1389 dm_log_userspace
= 1;
1395 /* [no]sync, block_on_error etc. */
1396 log_parm_count
+= hweight32(seg
->flags
);
1398 /* "handle_errors" is a feature arg now */
1402 /* DM_CORELOG does not count in the param list */
1403 if (seg
->flags
& DM_CORELOG
)
1406 if (seg
->clustered
) {
1407 log_parm_count
++; /* For UUID */
1409 if (!dm_log_userspace
)
1410 EMIT_PARAMS(pos
, "clustered-");
1418 if (!_build_dev_string(logbuf
, sizeof(logbuf
), seg
->log
))
1422 if (dm_log_userspace
)
1423 EMIT_PARAMS(pos
, "userspace %u %s clustered-%s",
1424 log_parm_count
, seg
->uuid
, logtype
);
1426 EMIT_PARAMS(pos
, "%s %u", logtype
, log_parm_count
);
1429 EMIT_PARAMS(pos
, " %s", logbuf
);
1431 EMIT_PARAMS(pos
, " %u", seg
->region_size
);
1433 if (seg
->clustered
&& !dm_log_userspace
)
1434 EMIT_PARAMS(pos
, " %s", seg
->uuid
);
1436 if ((seg
->flags
& DM_NOSYNC
))
1437 EMIT_PARAMS(pos
, " nosync");
1438 else if ((seg
->flags
& DM_FORCESYNC
))
1439 EMIT_PARAMS(pos
, " sync");
1442 EMIT_PARAMS(pos
, " block_on_error");
1444 EMIT_PARAMS(pos
, " %u ", seg
->mirror_area_count
);
1446 if ((r
= _emit_areas_line(dmt
, seg
, params
, paramsize
, &pos
)) <= 0)
1450 EMIT_PARAMS(pos
, " 1 handle_errors");
1455 static int _emit_segment_line(struct dm_task
*dmt
, uint32_t major
,
1456 uint32_t minor
, struct load_segment
*seg
,
1457 uint64_t *seg_start
, char *params
,
1462 char originbuf
[DM_FORMAT_DEV_BUFSIZE
], cowbuf
[DM_FORMAT_DEV_BUFSIZE
];
1470 /* Mirrors are pretty complicated - now in separate function */
1471 r
= _mirror_emit_segment_line(dmt
, major
, minor
, seg
, seg_start
,
1477 case SEG_SNAPSHOT_MERGE
:
1478 if (!_build_dev_string(originbuf
, sizeof(originbuf
), seg
->origin
))
1480 if (!_build_dev_string(cowbuf
, sizeof(cowbuf
), seg
->cow
))
1482 EMIT_PARAMS(pos
, "%s %s %c %d", originbuf
, cowbuf
,
1483 seg
->persistent
? 'P' : 'N', seg
->chunk_size
);
1485 case SEG_SNAPSHOT_ORIGIN
:
1486 if (!_build_dev_string(originbuf
, sizeof(originbuf
), seg
->origin
))
1488 EMIT_PARAMS(pos
, "%s", originbuf
);
1491 EMIT_PARAMS(pos
, "%u %u ", seg
->area_count
, seg
->stripe_size
);
1494 EMIT_PARAMS(pos
, "%s%s%s%s%s %s %" PRIu64
" ", seg
->cipher
,
1495 seg
->chainmode
? "-" : "", seg
->chainmode
?: "",
1496 seg
->iv
? "-" : "", seg
->iv
?: "", seg
->key
,
1497 seg
->iv_offset
!= DM_CRYPT_IV_DEFAULT
?
1498 seg
->iv_offset
: *seg_start
);
1505 case SEG_SNAPSHOT_ORIGIN
:
1506 case SEG_SNAPSHOT_MERGE
:
1512 if ((r
= _emit_areas_line(dmt
, seg
, params
, paramsize
, &pos
)) <= 0) {
1519 log_debug("Adding target to (%" PRIu32
":%" PRIu32
"): %" PRIu64
1520 " %" PRIu64
" %s %s", major
, minor
,
1521 *seg_start
, seg
->size
, dm_segtypes
[seg
->type
].target
, params
);
1523 if (!dm_task_add_target(dmt
, *seg_start
, seg
->size
, dm_segtypes
[seg
->type
].target
, params
))
1526 *seg_start
+= seg
->size
;
1533 static int _emit_segment(struct dm_task
*dmt
, uint32_t major
, uint32_t minor
,
1534 struct load_segment
*seg
, uint64_t *seg_start
)
1537 size_t paramsize
= 4096;
1541 if (!(params
= dm_malloc(paramsize
))) {
1542 log_error("Insufficient space for target parameters.");
1547 ret
= _emit_segment_line(dmt
, major
, minor
, seg
, seg_start
,
1557 log_debug("Insufficient space in params[%" PRIsize_t
1558 "] for target parameters.", paramsize
);
1561 } while (paramsize
< MAX_TARGET_PARAMSIZE
);
1563 log_error("Target parameter size too big. Aborting.");
1567 static int _load_node(struct dm_tree_node
*dnode
)
1570 struct dm_task
*dmt
;
1571 struct load_segment
*seg
;
1572 uint64_t seg_start
= 0;
1574 log_verbose("Loading %s table (%" PRIu32
":%" PRIu32
")", dnode
->name
,
1575 dnode
->info
.major
, dnode
->info
.minor
);
1577 if (!(dmt
= dm_task_create(DM_DEVICE_RELOAD
))) {
1578 log_error("Reload dm_task creation failed for %s", dnode
->name
);
1582 if (!dm_task_set_major(dmt
, dnode
->info
.major
) ||
1583 !dm_task_set_minor(dmt
, dnode
->info
.minor
)) {
1584 log_error("Failed to set device number for %s reload.", dnode
->name
);
1588 if (dnode
->props
.read_only
&& !dm_task_set_ro(dmt
)) {
1589 log_error("Failed to set read only flag for %s", dnode
->name
);
1593 if (!dm_task_no_open_count(dmt
))
1594 log_error("Failed to disable open_count");
1596 dm_list_iterate_items(seg
, &dnode
->props
.segs
)
1597 if (!_emit_segment(dmt
, dnode
->info
.major
, dnode
->info
.minor
,
1601 if (!dm_task_suppress_identical_reload(dmt
))
1602 log_error("Failed to suppress reload of identical tables.");
1604 if ((r
= dm_task_run(dmt
))) {
1605 r
= dm_task_get_info(dmt
, &dnode
->info
);
1606 if (r
&& !dnode
->info
.inactive_table
)
1607 log_verbose("Suppressed %s identical table reload.",
1610 if ((dnode
->props
.size_changed
=
1611 (dm_task_get_existing_table_size(dmt
) == seg_start
) ? 0 : 1))
1612 log_debug("Table size changed from %" PRIu64
" to %"
1614 dm_task_get_existing_table_size(dmt
),
1615 seg_start
, dnode
->name
);
1618 dnode
->props
.segment_count
= 0;
1621 dm_task_destroy(dmt
);
1626 int dm_tree_preload_children(struct dm_tree_node
*dnode
,
1627 const char *uuid_prefix
,
1628 size_t uuid_prefix_len
)
1631 void *handle
= NULL
;
1632 struct dm_tree_node
*child
;
1633 struct dm_info newinfo
;
1635 /* Preload children first */
1636 while ((child
= dm_tree_next_child(&handle
, dnode
, 0))) {
1637 /* Skip existing non-device-mapper devices */
1638 if (!child
->info
.exists
&& child
->info
.major
)
1641 /* Ignore if it doesn't belong to this VG */
1642 if (child
->info
.exists
&&
1643 !_uuid_prefix_matches(child
->uuid
, uuid_prefix
, uuid_prefix_len
))
1646 if (dm_tree_node_num_children(child
, 0))
1647 if (!dm_tree_preload_children(child
, uuid_prefix
, uuid_prefix_len
))
1650 /* FIXME Cope if name exists with no uuid? */
1651 if (!child
->info
.exists
) {
1652 if (!_create_node(child
)) {
1658 if (!child
->info
.inactive_table
&& child
->props
.segment_count
) {
1659 if (!_load_node(child
)) {
1665 /* Propagate device size change change */
1666 if (child
->props
.size_changed
)
1667 dnode
->props
.size_changed
= 1;
1669 /* Resume device immediately if it has parents and its size changed */
1670 if (!dm_tree_node_num_children(child
, 1) || !child
->props
.size_changed
)
1673 if (!child
->info
.inactive_table
&& !child
->info
.suspended
)
1676 if (!_resume_node(child
->name
, child
->info
.major
, child
->info
.minor
,
1677 child
->props
.read_ahead
, child
->props
.read_ahead_flags
,
1678 &newinfo
, &child
->dtree
->cookie
, child
->udev_flags
)) {
1679 log_error("Unable to resume %s (%" PRIu32
1680 ":%" PRIu32
")", child
->name
, child
->info
.major
,
1686 /* Update cached info */
1687 child
->info
= newinfo
;
1696 * Returns 1 if unsure.
1698 int dm_tree_children_use_uuid(struct dm_tree_node
*dnode
,
1699 const char *uuid_prefix
,
1700 size_t uuid_prefix_len
)
1702 void *handle
= NULL
;
1703 struct dm_tree_node
*child
= dnode
;
1706 while ((child
= dm_tree_next_child(&handle
, dnode
, 0))) {
1707 if (!(uuid
= dm_tree_node_get_uuid(child
))) {
1708 log_error("Failed to get uuid for dtree node.");
1712 if (_uuid_prefix_matches(uuid
, uuid_prefix
, uuid_prefix_len
))
1715 if (dm_tree_node_num_children(child
, 0))
1716 dm_tree_children_use_uuid(child
, uuid_prefix
, uuid_prefix_len
);
1725 static struct load_segment
*_add_segment(struct dm_tree_node
*dnode
, unsigned type
, uint64_t size
)
1727 struct load_segment
*seg
;
1729 if (!(seg
= dm_pool_zalloc(dnode
->dtree
->mem
, sizeof(*seg
)))) {
1730 log_error("dtree node segment allocation failed");
1736 seg
->area_count
= 0;
1737 dm_list_init(&seg
->areas
);
1738 seg
->stripe_size
= 0;
1739 seg
->persistent
= 0;
1740 seg
->chunk_size
= 0;
1745 dm_list_add(&dnode
->props
.segs
, &seg
->list
);
1746 dnode
->props
.segment_count
++;
1751 int dm_tree_node_add_snapshot_origin_target(struct dm_tree_node
*dnode
,
1753 const char *origin_uuid
)
1755 struct load_segment
*seg
;
1756 struct dm_tree_node
*origin_node
;
1758 if (!(seg
= _add_segment(dnode
, SEG_SNAPSHOT_ORIGIN
, size
)))
1761 if (!(origin_node
= dm_tree_find_node_by_uuid(dnode
->dtree
, origin_uuid
))) {
1762 log_error("Couldn't find snapshot origin uuid %s.", origin_uuid
);
1766 seg
->origin
= origin_node
;
1767 if (!_link_tree_nodes(dnode
, origin_node
))
1770 /* Resume snapshot origins after new snapshots */
1771 dnode
->activation_priority
= 1;
1776 static int _add_snapshot_target(struct dm_tree_node
*node
,
1778 const char *origin_uuid
,
1779 const char *cow_uuid
,
1780 const char *merge_uuid
,
1782 uint32_t chunk_size
)
1784 struct load_segment
*seg
;
1785 struct dm_tree_node
*origin_node
, *cow_node
, *merge_node
;
1788 seg_type
= !merge_uuid
? SEG_SNAPSHOT
: SEG_SNAPSHOT_MERGE
;
1790 if (!(seg
= _add_segment(node
, seg_type
, size
)))
1793 if (!(origin_node
= dm_tree_find_node_by_uuid(node
->dtree
, origin_uuid
))) {
1794 log_error("Couldn't find snapshot origin uuid %s.", origin_uuid
);
1798 seg
->origin
= origin_node
;
1799 if (!_link_tree_nodes(node
, origin_node
))
1802 if (!(cow_node
= dm_tree_find_node_by_uuid(node
->dtree
, cow_uuid
))) {
1803 log_error("Couldn't find snapshot COW device uuid %s.", cow_uuid
);
1807 seg
->cow
= cow_node
;
1808 if (!_link_tree_nodes(node
, cow_node
))
1811 seg
->persistent
= persistent
? 1 : 0;
1812 seg
->chunk_size
= chunk_size
;
1815 if (!(merge_node
= dm_tree_find_node_by_uuid(node
->dtree
, merge_uuid
))) {
1816 /* not a pure error, merging snapshot may have been deactivated */
1817 log_verbose("Couldn't find merging snapshot uuid %s.", merge_uuid
);
1819 seg
->merge
= merge_node
;
1820 /* must not link merging snapshot, would undermine activation_priority below */
1823 /* Resume snapshot-merge (acting origin) after other snapshots */
1824 node
->activation_priority
= 1;
1826 /* Resume merging snapshot after snapshot-merge */
1827 seg
->merge
->activation_priority
= 2;
1835 int dm_tree_node_add_snapshot_target(struct dm_tree_node
*node
,
1837 const char *origin_uuid
,
1838 const char *cow_uuid
,
1840 uint32_t chunk_size
)
1842 return _add_snapshot_target(node
, size
, origin_uuid
, cow_uuid
,
1843 NULL
, persistent
, chunk_size
);
1846 int dm_tree_node_add_snapshot_merge_target(struct dm_tree_node
*node
,
1848 const char *origin_uuid
,
1849 const char *cow_uuid
,
1850 const char *merge_uuid
,
1851 uint32_t chunk_size
)
1853 return _add_snapshot_target(node
, size
, origin_uuid
, cow_uuid
,
1854 merge_uuid
, 1, chunk_size
);
1857 int dm_tree_node_add_error_target(struct dm_tree_node
*node
,
1860 if (!_add_segment(node
, SEG_ERROR
, size
))
1866 int dm_tree_node_add_zero_target(struct dm_tree_node
*node
,
1869 if (!_add_segment(node
, SEG_ZERO
, size
))
1875 int dm_tree_node_add_linear_target(struct dm_tree_node
*node
,
1878 if (!_add_segment(node
, SEG_LINEAR
, size
))
1884 int dm_tree_node_add_striped_target(struct dm_tree_node
*node
,
1886 uint32_t stripe_size
)
1888 struct load_segment
*seg
;
1890 if (!(seg
= _add_segment(node
, SEG_STRIPED
, size
)))
1893 seg
->stripe_size
= stripe_size
;
1898 int dm_tree_node_add_crypt_target(struct dm_tree_node
*node
,
1901 const char *chainmode
,
1906 struct load_segment
*seg
;
1908 if (!(seg
= _add_segment(node
, SEG_CRYPT
, size
)))
1911 seg
->cipher
= cipher
;
1912 seg
->chainmode
= chainmode
;
1914 seg
->iv_offset
= iv_offset
;
1920 int dm_tree_node_add_mirror_target_log(struct dm_tree_node
*node
,
1921 uint32_t region_size
,
1923 const char *log_uuid
,
1924 unsigned area_count
,
1927 struct dm_tree_node
*log_node
= NULL
;
1928 struct load_segment
*seg
;
1930 if (!node
->props
.segment_count
) {
1931 log_error("Internal error: Attempt to add target area to missing segment.");
1935 seg
= dm_list_item(dm_list_last(&node
->props
.segs
), struct load_segment
);
1938 if (!(seg
->uuid
= dm_pool_strdup(node
->dtree
->mem
, log_uuid
))) {
1939 log_error("log uuid pool_strdup failed");
1942 if (!(flags
& DM_CORELOG
)) {
1943 if (!(log_node
= dm_tree_find_node_by_uuid(node
->dtree
, log_uuid
))) {
1944 log_error("Couldn't find mirror log uuid %s.", log_uuid
);
1948 if (!_link_tree_nodes(node
, log_node
))
1953 seg
->log
= log_node
;
1954 seg
->region_size
= region_size
;
1955 seg
->clustered
= clustered
;
1956 seg
->mirror_area_count
= area_count
;
1962 int dm_tree_node_add_mirror_target(struct dm_tree_node
*node
,
1965 struct load_segment
*seg
;
1967 if (!(seg
= _add_segment(node
, SEG_MIRRORED
, size
)))
1973 static int _add_area(struct dm_tree_node
*node
, struct load_segment
*seg
, struct dm_tree_node
*dev_node
, uint64_t offset
)
1975 struct seg_area
*area
;
1977 if (!(area
= dm_pool_zalloc(node
->dtree
->mem
, sizeof (*area
)))) {
1978 log_error("Failed to allocate target segment area.");
1982 area
->dev_node
= dev_node
;
1983 area
->offset
= offset
;
1985 dm_list_add(&seg
->areas
, &area
->list
);
1991 int dm_tree_node_add_target_area(struct dm_tree_node
*node
,
1992 const char *dev_name
,
1996 struct load_segment
*seg
;
1998 struct dm_tree_node
*dev_node
;
2000 if ((!dev_name
|| !*dev_name
) && (!uuid
|| !*uuid
)) {
2001 log_error("dm_tree_node_add_target_area called without device");
2006 if (!(dev_node
= dm_tree_find_node_by_uuid(node
->dtree
, uuid
))) {
2007 log_error("Couldn't find area uuid %s.", uuid
);
2010 if (!_link_tree_nodes(node
, dev_node
))
2013 if (stat(dev_name
, &info
) < 0) {
2014 log_error("Device %s not found.", dev_name
);
2018 if (!S_ISBLK(info
.st_mode
)) {
2019 log_error("Device %s is not a block device.", dev_name
);
2023 /* FIXME Check correct macro use */
2024 if (!(dev_node
= _add_dev(node
->dtree
, node
, MAJOR(info
.st_rdev
),
2025 MINOR(info
.st_rdev
), 0)))
2029 if (!node
->props
.segment_count
) {
2030 log_error("Internal error: Attempt to add target area to missing segment.");
2034 seg
= dm_list_item(dm_list_last(&node
->props
.segs
), struct load_segment
);
2036 if (!_add_area(node
, seg
, dev_node
, offset
))
2042 void dm_tree_set_cookie(struct dm_tree_node
*node
, uint32_t cookie
)
2044 node
->dtree
->cookie
= cookie
;
2047 uint32_t dm_tree_get_cookie(struct dm_tree_node
*node
)
2049 return node
->dtree
->cookie
;