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 */
42 /* FIXME Add crypt and multipath support */
48 { SEG_CRYPT
, "crypt" },
49 { SEG_ERROR
, "error" },
50 { SEG_LINEAR
, "linear" },
51 { SEG_MIRRORED
, "mirror" },
52 { SEG_SNAPSHOT
, "snapshot" },
53 { SEG_SNAPSHOT_ORIGIN
, "snapshot-origin" },
54 { SEG_STRIPED
, "striped" },
58 /* Some segment types have a list of areas of other devices attached */
62 struct dm_tree_node
*dev_node
;
67 /* Per-segment properties */
75 unsigned area_count
; /* Linear + Striped + Mirrored + Crypt */
76 struct dm_list areas
; /* Linear + Striped + Mirrored + Crypt */
78 uint32_t stripe_size
; /* Striped */
80 int persistent
; /* Snapshot */
81 uint32_t chunk_size
; /* Snapshot */
82 struct dm_tree_node
*cow
; /* Snapshot */
83 struct dm_tree_node
*origin
; /* Snapshot + Snapshot origin */
85 struct dm_tree_node
*log
; /* Mirror */
86 uint32_t region_size
; /* Mirror */
87 unsigned clustered
; /* Mirror */
88 unsigned mirror_area_count
; /* Mirror */
89 uint32_t flags
; /* Mirror log */
90 char *uuid
; /* Clustered mirror log */
92 const char *cipher
; /* Crypt */
93 const char *chainmode
; /* Crypt */
94 const char *iv
; /* Crypt */
95 uint64_t iv_offset
; /* Crypt */
96 const char *key
; /* Crypt */
99 /* Per-device properties */
100 struct load_properties
{
106 uint32_t read_ahead_flags
;
108 unsigned segment_count
;
109 unsigned size_changed
;
112 const char *new_name
;
115 /* Two of these used to join two nodes with uses and used_by. */
116 struct dm_tree_link
{
118 struct dm_tree_node
*node
;
121 struct dm_tree_node
{
122 struct dm_tree
*dtree
;
128 struct dm_list uses
; /* Nodes this node uses */
129 struct dm_list used_by
; /* Nodes that use this node */
131 int activation_priority
; /* 0 gets activated first */
133 uint16_t udev_flags
; /* Udev control flags */
135 void *context
; /* External supplied context */
137 struct load_properties props
; /* For creation/table (re)load */
142 struct dm_hash_table
*devs
;
143 struct dm_hash_table
*uuids
;
144 struct dm_tree_node root
;
145 int skip_lockfs
; /* 1 skips lockfs (for non-snapshots) */
146 int no_flush
; /* 1 sets noflush (mirrors/multipath) */
150 struct dm_tree
*dm_tree_create(void)
152 struct dm_tree
*dtree
;
154 if (!(dtree
= dm_malloc(sizeof(*dtree
)))) {
155 log_error("dm_tree_create malloc failed");
159 memset(dtree
, 0, sizeof(*dtree
));
160 dtree
->root
.dtree
= dtree
;
161 dm_list_init(&dtree
->root
.uses
);
162 dm_list_init(&dtree
->root
.used_by
);
163 dtree
->skip_lockfs
= 0;
166 if (!(dtree
->mem
= dm_pool_create("dtree", 1024))) {
167 log_error("dtree pool creation failed");
172 if (!(dtree
->devs
= dm_hash_create(8))) {
173 log_error("dtree hash creation failed");
174 dm_pool_destroy(dtree
->mem
);
179 if (!(dtree
->uuids
= dm_hash_create(32))) {
180 log_error("dtree uuid hash creation failed");
181 dm_hash_destroy(dtree
->devs
);
182 dm_pool_destroy(dtree
->mem
);
190 void dm_tree_free(struct dm_tree
*dtree
)
195 dm_hash_destroy(dtree
->uuids
);
196 dm_hash_destroy(dtree
->devs
);
197 dm_pool_destroy(dtree
->mem
);
201 static int _nodes_are_linked(struct dm_tree_node
*parent
,
202 struct dm_tree_node
*child
)
204 struct dm_tree_link
*dlink
;
206 dm_list_iterate_items(dlink
, &parent
->uses
)
207 if (dlink
->node
== child
)
213 static int _link(struct dm_list
*list
, struct dm_tree_node
*node
)
215 struct dm_tree_link
*dlink
;
217 if (!(dlink
= dm_pool_alloc(node
->dtree
->mem
, sizeof(*dlink
)))) {
218 log_error("dtree link allocation failed");
223 dm_list_add(list
, &dlink
->list
);
228 static int _link_nodes(struct dm_tree_node
*parent
,
229 struct dm_tree_node
*child
)
231 if (_nodes_are_linked(parent
, child
))
234 if (!_link(&parent
->uses
, child
))
237 if (!_link(&child
->used_by
, parent
))
243 static void _unlink(struct dm_list
*list
, struct dm_tree_node
*node
)
245 struct dm_tree_link
*dlink
;
247 dm_list_iterate_items(dlink
, list
)
248 if (dlink
->node
== node
) {
249 dm_list_del(&dlink
->list
);
254 static void _unlink_nodes(struct dm_tree_node
*parent
,
255 struct dm_tree_node
*child
)
257 if (!_nodes_are_linked(parent
, child
))
260 _unlink(&parent
->uses
, child
);
261 _unlink(&child
->used_by
, parent
);
264 static int _add_to_toplevel(struct dm_tree_node
*node
)
266 return _link_nodes(&node
->dtree
->root
, node
);
269 static void _remove_from_toplevel(struct dm_tree_node
*node
)
271 _unlink_nodes(&node
->dtree
->root
, node
);
274 static int _add_to_bottomlevel(struct dm_tree_node
*node
)
276 return _link_nodes(node
, &node
->dtree
->root
);
279 static void _remove_from_bottomlevel(struct dm_tree_node
*node
)
281 _unlink_nodes(node
, &node
->dtree
->root
);
284 static int _link_tree_nodes(struct dm_tree_node
*parent
, struct dm_tree_node
*child
)
286 /* Don't link to root node if child already has a parent */
287 if ((parent
== &parent
->dtree
->root
)) {
288 if (dm_tree_node_num_children(child
, 1))
291 _remove_from_toplevel(child
);
293 if ((child
== &child
->dtree
->root
)) {
294 if (dm_tree_node_num_children(parent
, 0))
297 _remove_from_bottomlevel(parent
);
299 return _link_nodes(parent
, child
);
302 static struct dm_tree_node
*_create_dm_tree_node(struct dm_tree
*dtree
,
305 struct dm_info
*info
,
309 struct dm_tree_node
*node
;
312 if (!(node
= dm_pool_zalloc(dtree
->mem
, sizeof(*node
)))) {
313 log_error("_create_dm_tree_node alloc failed");
322 node
->context
= context
;
323 node
->udev_flags
= udev_flags
;
324 node
->activation_priority
= 0;
326 dm_list_init(&node
->uses
);
327 dm_list_init(&node
->used_by
);
328 dm_list_init(&node
->props
.segs
);
330 dev
= MKDEV(info
->major
, info
->minor
);
332 if (!dm_hash_insert_binary(dtree
->devs
, (const char *) &dev
,
333 sizeof(dev
), node
)) {
334 log_error("dtree node hash insertion failed");
335 dm_pool_free(dtree
->mem
, node
);
340 !dm_hash_insert(dtree
->uuids
, uuid
, node
)) {
341 log_error("dtree uuid hash insertion failed");
342 dm_hash_remove_binary(dtree
->devs
, (const char *) &dev
,
344 dm_pool_free(dtree
->mem
, node
);
351 static struct dm_tree_node
*_find_dm_tree_node(struct dm_tree
*dtree
,
352 uint32_t major
, uint32_t minor
)
354 uint64_t dev
= MKDEV(major
, minor
);
356 return dm_hash_lookup_binary(dtree
->devs
, (const char *) &dev
,
360 static struct dm_tree_node
*_find_dm_tree_node_by_uuid(struct dm_tree
*dtree
,
363 struct dm_tree_node
*node
;
365 if ((node
= dm_hash_lookup(dtree
->uuids
, uuid
)))
368 if (strncmp(uuid
, UUID_PREFIX
, sizeof(UUID_PREFIX
) - 1))
371 return dm_hash_lookup(dtree
->uuids
, uuid
+ sizeof(UUID_PREFIX
) - 1);
374 static int _deps(struct dm_task
**dmt
, struct dm_pool
*mem
, uint32_t major
, uint32_t minor
,
375 const char **name
, const char **uuid
,
376 struct dm_info
*info
, struct dm_deps
**deps
)
378 memset(info
, 0, sizeof(*info
));
380 if (!dm_is_dm_major(major
)) {
387 info
->live_table
= 0;
388 info
->inactive_table
= 0;
393 if (!(*dmt
= dm_task_create(DM_DEVICE_DEPS
))) {
394 log_error("deps dm_task creation failed");
398 if (!dm_task_set_major(*dmt
, major
)) {
399 log_error("_deps: failed to set major for (%" PRIu32
":%" PRIu32
")",
404 if (!dm_task_set_minor(*dmt
, minor
)) {
405 log_error("_deps: failed to set minor for (%" PRIu32
":%" PRIu32
")",
410 if (!dm_task_run(*dmt
)) {
411 log_error("_deps: task run failed for (%" PRIu32
":%" PRIu32
")",
416 if (!dm_task_get_info(*dmt
, info
)) {
417 log_error("_deps: failed to get info for (%" PRIu32
":%" PRIu32
")",
427 if (info
->major
!= major
) {
428 log_error("Inconsistent dtree major number: %u != %u",
432 if (info
->minor
!= minor
) {
433 log_error("Inconsistent dtree minor number: %u != %u",
437 if (!(*name
= dm_pool_strdup(mem
, dm_task_get_name(*dmt
)))) {
438 log_error("name pool_strdup failed");
441 if (!(*uuid
= dm_pool_strdup(mem
, dm_task_get_uuid(*dmt
)))) {
442 log_error("uuid pool_strdup failed");
445 *deps
= dm_task_get_deps(*dmt
);
451 dm_task_destroy(*dmt
);
455 static struct dm_tree_node
*_add_dev(struct dm_tree
*dtree
,
456 struct dm_tree_node
*parent
,
457 uint32_t major
, uint32_t minor
,
460 struct dm_task
*dmt
= NULL
;
462 struct dm_deps
*deps
= NULL
;
463 const char *name
= NULL
;
464 const char *uuid
= NULL
;
465 struct dm_tree_node
*node
= NULL
;
469 /* Already in tree? */
470 if (!(node
= _find_dm_tree_node(dtree
, major
, minor
))) {
471 if (!_deps(&dmt
, dtree
->mem
, major
, minor
, &name
, &uuid
, &info
, &deps
))
474 if (!(node
= _create_dm_tree_node(dtree
, name
, uuid
, &info
,
480 if (!_link_tree_nodes(parent
, node
)) {
485 /* If node was already in tree, no need to recurse. */
489 /* Can't recurse if not a mapped device or there are no dependencies */
490 if (!node
->info
.exists
|| !deps
->count
) {
491 if (!_add_to_bottomlevel(node
)) {
498 /* Add dependencies to tree */
499 for (i
= 0; i
< deps
->count
; i
++)
500 if (!_add_dev(dtree
, node
, MAJOR(deps
->device
[i
]),
501 MINOR(deps
->device
[i
]), udev_flags
)) {
508 dm_task_destroy(dmt
);
513 static int _node_clear_table(struct dm_tree_node
*dnode
)
516 struct dm_info
*info
;
520 if (!(info
= &dnode
->info
)) {
521 log_error("_node_clear_table failed: missing info");
525 if (!(name
= dm_tree_node_get_name(dnode
))) {
526 log_error("_node_clear_table failed: missing name");
530 /* Is there a table? */
531 if (!info
->exists
|| !info
->inactive_table
)
534 log_verbose("Clearing inactive table %s (%" PRIu32
":%" PRIu32
")",
535 name
, info
->major
, info
->minor
);
537 if (!(dmt
= dm_task_create(DM_DEVICE_CLEAR
))) {
538 dm_task_destroy(dmt
);
539 log_error("Table clear dm_task creation failed for %s", name
);
543 if (!dm_task_set_major(dmt
, info
->major
) ||
544 !dm_task_set_minor(dmt
, info
->minor
)) {
545 log_error("Failed to set device number for %s table clear", name
);
546 dm_task_destroy(dmt
);
550 r
= dm_task_run(dmt
);
552 if (!dm_task_get_info(dmt
, info
)) {
553 log_error("_node_clear_table failed: info missing after running task for %s", name
);
557 dm_task_destroy(dmt
);
562 struct dm_tree_node
*dm_tree_add_new_dev(struct dm_tree
*dtree
,
565 uint32_t major
, uint32_t minor
,
570 struct dm_tree_node
*dnode
;
575 /* Do we need to add node to tree? */
576 if (!(dnode
= dm_tree_find_node_by_uuid(dtree
, uuid
))) {
577 if (!(name2
= dm_pool_strdup(dtree
->mem
, name
))) {
578 log_error("name pool_strdup failed");
581 if (!(uuid2
= dm_pool_strdup(dtree
->mem
, uuid
))) {
582 log_error("uuid pool_strdup failed");
590 info
.inactive_table
= 0;
593 if (!(dnode
= _create_dm_tree_node(dtree
, name2
, uuid2
, &info
,
597 /* Attach to root node until a table is supplied */
598 if (!_add_to_toplevel(dnode
) || !_add_to_bottomlevel(dnode
))
601 dnode
->props
.major
= major
;
602 dnode
->props
.minor
= minor
;
603 dnode
->props
.new_name
= NULL
;
604 dnode
->props
.size_changed
= 0;
605 } else if (strcmp(name
, dnode
->name
)) {
606 /* Do we need to rename node? */
607 if (!(dnode
->props
.new_name
= dm_pool_strdup(dtree
->mem
, name
))) {
608 log_error("name pool_strdup failed");
613 dnode
->props
.read_only
= read_only
? 1 : 0;
614 dnode
->props
.read_ahead
= DM_READ_AHEAD_AUTO
;
615 dnode
->props
.read_ahead_flags
= 0;
617 if (clear_inactive
&& !_node_clear_table(dnode
))
620 dnode
->context
= context
;
621 dnode
->udev_flags
= 0;
626 struct dm_tree_node
*dm_tree_add_new_dev_with_udev_flags(struct dm_tree
*dtree
,
636 struct dm_tree_node
*node
;
638 if ((node
= dm_tree_add_new_dev(dtree
, name
, uuid
, major
, minor
, read_only
,
639 clear_inactive
, context
)))
640 node
->udev_flags
= udev_flags
;
646 void dm_tree_node_set_read_ahead(struct dm_tree_node
*dnode
,
648 uint32_t read_ahead_flags
)
650 dnode
->props
.read_ahead
= read_ahead
;
651 dnode
->props
.read_ahead_flags
= read_ahead_flags
;
654 int dm_tree_add_dev(struct dm_tree
*dtree
, uint32_t major
, uint32_t minor
)
656 return _add_dev(dtree
, &dtree
->root
, major
, minor
, 0) ? 1 : 0;
659 int dm_tree_add_dev_with_udev_flags(struct dm_tree
*dtree
, uint32_t major
,
660 uint32_t minor
, uint16_t udev_flags
)
662 return _add_dev(dtree
, &dtree
->root
, major
, minor
, udev_flags
) ? 1 : 0;
665 const char *dm_tree_node_get_name(struct dm_tree_node
*node
)
667 return node
->info
.exists
? node
->name
: "";
670 const char *dm_tree_node_get_uuid(struct dm_tree_node
*node
)
672 return node
->info
.exists
? node
->uuid
: "";
675 const struct dm_info
*dm_tree_node_get_info(struct dm_tree_node
*node
)
680 void *dm_tree_node_get_context(struct dm_tree_node
*node
)
682 return node
->context
;
685 int dm_tree_node_size_changed(struct dm_tree_node
*dnode
)
687 return dnode
->props
.size_changed
;
690 int dm_tree_node_num_children(struct dm_tree_node
*node
, uint32_t inverted
)
693 if (_nodes_are_linked(&node
->dtree
->root
, node
))
695 return dm_list_size(&node
->used_by
);
698 if (_nodes_are_linked(node
, &node
->dtree
->root
))
701 return dm_list_size(&node
->uses
);
705 * Returns 1 if no prefix supplied
707 static int _uuid_prefix_matches(const char *uuid
, const char *uuid_prefix
, size_t uuid_prefix_len
)
712 if (!strncmp(uuid
, uuid_prefix
, uuid_prefix_len
))
715 /* Handle transition: active device uuids might be missing the prefix */
716 if (uuid_prefix_len
<= 4)
719 if (!strncmp(uuid
, UUID_PREFIX
, sizeof(UUID_PREFIX
) - 1))
722 if (strncmp(uuid_prefix
, UUID_PREFIX
, sizeof(UUID_PREFIX
) - 1))
725 if (!strncmp(uuid
, uuid_prefix
+ sizeof(UUID_PREFIX
) - 1, uuid_prefix_len
- (sizeof(UUID_PREFIX
) - 1)))
732 * Returns 1 if no children.
734 static int _children_suspended(struct dm_tree_node
*node
,
736 const char *uuid_prefix
,
737 size_t uuid_prefix_len
)
739 struct dm_list
*list
;
740 struct dm_tree_link
*dlink
;
741 const struct dm_info
*dinfo
;
745 if (_nodes_are_linked(&node
->dtree
->root
, node
))
747 list
= &node
->used_by
;
749 if (_nodes_are_linked(node
, &node
->dtree
->root
))
754 dm_list_iterate_items(dlink
, list
) {
755 if (!(uuid
= dm_tree_node_get_uuid(dlink
->node
))) {
760 /* Ignore if it doesn't belong to this VG */
761 if (!_uuid_prefix_matches(uuid
, uuid_prefix
, uuid_prefix_len
))
764 if (!(dinfo
= dm_tree_node_get_info(dlink
->node
))) {
765 stack
; /* FIXME Is this normal? */
769 if (!dinfo
->suspended
)
777 * Set major and minor to zero for root of tree.
779 struct dm_tree_node
*dm_tree_find_node(struct dm_tree
*dtree
,
783 if (!major
&& !minor
)
786 return _find_dm_tree_node(dtree
, major
, minor
);
790 * Set uuid to NULL for root of tree.
792 struct dm_tree_node
*dm_tree_find_node_by_uuid(struct dm_tree
*dtree
,
798 return _find_dm_tree_node_by_uuid(dtree
, uuid
);
802 * First time set *handle to NULL.
803 * Set inverted to invert the tree.
805 struct dm_tree_node
*dm_tree_next_child(void **handle
,
806 struct dm_tree_node
*parent
,
809 struct dm_list
**dlink
= (struct dm_list
**) handle
;
810 struct dm_list
*use_list
;
813 use_list
= &parent
->used_by
;
815 use_list
= &parent
->uses
;
818 *dlink
= dm_list_first(use_list
);
820 *dlink
= dm_list_next(use_list
, *dlink
);
822 return (*dlink
) ? dm_list_item(*dlink
, struct dm_tree_link
)->node
: NULL
;
826 * Deactivate a device with its dependencies if the uuid prefix matches.
828 static int _info_by_dev(uint32_t major
, uint32_t minor
, int with_open_count
,
829 struct dm_info
*info
)
834 if (!(dmt
= dm_task_create(DM_DEVICE_INFO
))) {
835 log_error("_info_by_dev: dm_task creation failed");
839 if (!dm_task_set_major(dmt
, major
) || !dm_task_set_minor(dmt
, minor
)) {
840 log_error("_info_by_dev: Failed to set device number");
841 dm_task_destroy(dmt
);
845 if (!with_open_count
&& !dm_task_no_open_count(dmt
))
846 log_error("Failed to disable open_count");
848 if ((r
= dm_task_run(dmt
)))
849 r
= dm_task_get_info(dmt
, info
);
851 dm_task_destroy(dmt
);
856 static int _deactivate_node(const char *name
, uint32_t major
, uint32_t minor
,
857 uint32_t *cookie
, uint16_t udev_flags
)
862 log_verbose("Removing %s (%" PRIu32
":%" PRIu32
")", name
, major
, minor
);
864 if (!(dmt
= dm_task_create(DM_DEVICE_REMOVE
))) {
865 log_error("Deactivation dm_task creation failed for %s", name
);
869 if (!dm_task_set_major(dmt
, major
) || !dm_task_set_minor(dmt
, minor
)) {
870 log_error("Failed to set device number for %s deactivation", name
);
874 if (!dm_task_no_open_count(dmt
))
875 log_error("Failed to disable open_count");
877 if (!dm_task_set_cookie(dmt
, cookie
, udev_flags
))
880 r
= dm_task_run(dmt
);
882 /* FIXME Until kernel returns actual name so dm-ioctl.c can handle it */
883 rm_dev_node(name
, dmt
->cookie_set
&&
884 !(udev_flags
& DM_UDEV_DISABLE_DM_RULES_FLAG
));
886 /* FIXME Remove node from tree or mark invalid? */
889 dm_task_destroy(dmt
);
894 static int _rename_node(const char *old_name
, const char *new_name
, uint32_t major
,
895 uint32_t minor
, uint32_t *cookie
, uint16_t udev_flags
)
900 log_verbose("Renaming %s (%" PRIu32
":%" PRIu32
") to %s", old_name
, major
, minor
, new_name
);
902 if (!(dmt
= dm_task_create(DM_DEVICE_RENAME
))) {
903 log_error("Rename dm_task creation failed for %s", old_name
);
907 if (!dm_task_set_name(dmt
, old_name
)) {
908 log_error("Failed to set name for %s rename.", old_name
);
912 if (!dm_task_set_newname(dmt
, new_name
))
915 if (!dm_task_no_open_count(dmt
))
916 log_error("Failed to disable open_count");
918 if (!dm_task_set_cookie(dmt
, cookie
, udev_flags
))
921 r
= dm_task_run(dmt
);
924 dm_task_destroy(dmt
);
929 /* FIXME Merge with _suspend_node? */
930 static int _resume_node(const char *name
, uint32_t major
, uint32_t minor
,
931 uint32_t read_ahead
, uint32_t read_ahead_flags
,
932 struct dm_info
*newinfo
, uint32_t *cookie
,
938 log_verbose("Resuming %s (%" PRIu32
":%" PRIu32
")", name
, major
, minor
);
940 if (!(dmt
= dm_task_create(DM_DEVICE_RESUME
))) {
941 log_error("Suspend dm_task creation failed for %s", name
);
945 /* FIXME Kernel should fill in name on return instead */
946 if (!dm_task_set_name(dmt
, name
)) {
947 log_error("Failed to set readahead device name for %s", name
);
951 if (!dm_task_set_major(dmt
, major
) || !dm_task_set_minor(dmt
, minor
)) {
952 log_error("Failed to set device number for %s resumption.", name
);
956 if (!dm_task_no_open_count(dmt
))
957 log_error("Failed to disable open_count");
959 if (!dm_task_set_read_ahead(dmt
, read_ahead
, read_ahead_flags
))
960 log_error("Failed to set read ahead");
962 if (!dm_task_set_cookie(dmt
, cookie
, udev_flags
))
965 if ((r
= dm_task_run(dmt
)))
966 r
= dm_task_get_info(dmt
, newinfo
);
969 dm_task_destroy(dmt
);
974 static int _suspend_node(const char *name
, uint32_t major
, uint32_t minor
,
975 int skip_lockfs
, int no_flush
, struct dm_info
*newinfo
)
980 log_verbose("Suspending %s (%" PRIu32
":%" PRIu32
")%s%s",
982 skip_lockfs
? "" : " with filesystem sync",
983 no_flush
? "" : " with device flush");
985 if (!(dmt
= dm_task_create(DM_DEVICE_SUSPEND
))) {
986 log_error("Suspend dm_task creation failed for %s", name
);
990 if (!dm_task_set_major(dmt
, major
) || !dm_task_set_minor(dmt
, minor
)) {
991 log_error("Failed to set device number for %s suspension.", name
);
992 dm_task_destroy(dmt
);
996 if (!dm_task_no_open_count(dmt
))
997 log_error("Failed to disable open_count");
999 if (skip_lockfs
&& !dm_task_skip_lockfs(dmt
))
1000 log_error("Failed to set skip_lockfs flag.");
1002 if (no_flush
&& !dm_task_no_flush(dmt
))
1003 log_error("Failed to set no_flush flag.");
1005 if ((r
= dm_task_run(dmt
)))
1006 r
= dm_task_get_info(dmt
, newinfo
);
1008 dm_task_destroy(dmt
);
1013 int dm_tree_deactivate_children(struct dm_tree_node
*dnode
,
1014 const char *uuid_prefix
,
1015 size_t uuid_prefix_len
)
1018 void *handle
= NULL
;
1019 struct dm_tree_node
*child
= dnode
;
1020 struct dm_info info
;
1021 const struct dm_info
*dinfo
;
1025 while ((child
= dm_tree_next_child(&handle
, dnode
, 0))) {
1026 if (!(dinfo
= dm_tree_node_get_info(child
))) {
1031 if (!(name
= dm_tree_node_get_name(child
))) {
1036 if (!(uuid
= dm_tree_node_get_uuid(child
))) {
1041 /* Ignore if it doesn't belong to this VG */
1042 if (!_uuid_prefix_matches(uuid
, uuid_prefix
, uuid_prefix_len
))
1045 /* Refresh open_count */
1046 if (!_info_by_dev(dinfo
->major
, dinfo
->minor
, 1, &info
) ||
1047 !info
.exists
|| info
.open_count
)
1050 if (!_deactivate_node(name
, info
.major
, info
.minor
,
1051 &child
->dtree
->cookie
, child
->udev_flags
)) {
1052 log_error("Unable to deactivate %s (%" PRIu32
1053 ":%" PRIu32
")", name
, info
.major
,
1059 if (dm_tree_node_num_children(child
, 0))
1060 if (!dm_tree_deactivate_children(child
, uuid_prefix
, uuid_prefix_len
))
1067 void dm_tree_skip_lockfs(struct dm_tree_node
*dnode
)
1069 dnode
->dtree
->skip_lockfs
= 1;
1072 void dm_tree_use_no_flush_suspend(struct dm_tree_node
*dnode
)
1074 dnode
->dtree
->no_flush
= 1;
1077 int dm_tree_suspend_children(struct dm_tree_node
*dnode
,
1078 const char *uuid_prefix
,
1079 size_t uuid_prefix_len
)
1082 void *handle
= NULL
;
1083 struct dm_tree_node
*child
= dnode
;
1084 struct dm_info info
, newinfo
;
1085 const struct dm_info
*dinfo
;
1089 /* Suspend nodes at this level of the tree */
1090 while ((child
= dm_tree_next_child(&handle
, dnode
, 0))) {
1091 if (!(dinfo
= dm_tree_node_get_info(child
))) {
1096 if (!(name
= dm_tree_node_get_name(child
))) {
1101 if (!(uuid
= dm_tree_node_get_uuid(child
))) {
1106 /* Ignore if it doesn't belong to this VG */
1107 if (!_uuid_prefix_matches(uuid
, uuid_prefix
, uuid_prefix_len
))
1110 /* Ensure immediate parents are already suspended */
1111 if (!_children_suspended(child
, 1, uuid_prefix
, uuid_prefix_len
))
1114 if (!_info_by_dev(dinfo
->major
, dinfo
->minor
, 0, &info
) ||
1115 !info
.exists
|| info
.suspended
)
1118 if (!_suspend_node(name
, info
.major
, info
.minor
,
1119 child
->dtree
->skip_lockfs
,
1120 child
->dtree
->no_flush
, &newinfo
)) {
1121 log_error("Unable to suspend %s (%" PRIu32
1122 ":%" PRIu32
")", name
, info
.major
,
1128 /* Update cached info */
1129 child
->info
= newinfo
;
1132 /* Then suspend any child nodes */
1135 while ((child
= dm_tree_next_child(&handle
, dnode
, 0))) {
1136 if (!(uuid
= dm_tree_node_get_uuid(child
))) {
1141 /* Ignore if it doesn't belong to this VG */
1142 if (!_uuid_prefix_matches(uuid
, uuid_prefix
, uuid_prefix_len
))
1145 if (dm_tree_node_num_children(child
, 0))
1146 if (!dm_tree_suspend_children(child
, uuid_prefix
, uuid_prefix_len
))
1153 int dm_tree_activate_children(struct dm_tree_node
*dnode
,
1154 const char *uuid_prefix
,
1155 size_t uuid_prefix_len
)
1158 void *handle
= NULL
;
1159 struct dm_tree_node
*child
= dnode
;
1160 struct dm_info newinfo
;
1165 /* Activate children first */
1166 while ((child
= dm_tree_next_child(&handle
, dnode
, 0))) {
1167 if (!(uuid
= dm_tree_node_get_uuid(child
))) {
1172 if (!_uuid_prefix_matches(uuid
, uuid_prefix
, uuid_prefix_len
))
1175 if (dm_tree_node_num_children(child
, 0))
1176 if (!dm_tree_activate_children(child
, uuid_prefix
, uuid_prefix_len
))
1182 for (priority
= 0; priority
< 2; priority
++) {
1183 while ((child
= dm_tree_next_child(&handle
, dnode
, 0))) {
1184 if (!(uuid
= dm_tree_node_get_uuid(child
))) {
1189 if (!_uuid_prefix_matches(uuid
, uuid_prefix
, uuid_prefix_len
))
1192 if (priority
!= child
->activation_priority
)
1195 if (!(name
= dm_tree_node_get_name(child
))) {
1201 if (child
->props
.new_name
) {
1202 if (!_rename_node(name
, child
->props
.new_name
, child
->info
.major
,
1203 child
->info
.minor
, &child
->dtree
->cookie
,
1204 child
->udev_flags
)) {
1205 log_error("Failed to rename %s (%" PRIu32
1206 ":%" PRIu32
") to %s", name
, child
->info
.major
,
1207 child
->info
.minor
, child
->props
.new_name
);
1210 child
->name
= child
->props
.new_name
;
1211 child
->props
.new_name
= NULL
;
1214 if (!child
->info
.inactive_table
&& !child
->info
.suspended
)
1217 if (!_resume_node(child
->name
, child
->info
.major
, child
->info
.minor
,
1218 child
->props
.read_ahead
, child
->props
.read_ahead_flags
,
1219 &newinfo
, &child
->dtree
->cookie
, child
->udev_flags
)) {
1220 log_error("Unable to resume %s (%" PRIu32
1221 ":%" PRIu32
")", child
->name
, child
->info
.major
,
1227 /* Update cached info */
1228 child
->info
= newinfo
;
1237 static int _create_node(struct dm_tree_node
*dnode
)
1240 struct dm_task
*dmt
;
1242 log_verbose("Creating %s", dnode
->name
);
1244 if (!(dmt
= dm_task_create(DM_DEVICE_CREATE
))) {
1245 log_error("Create dm_task creation failed for %s", dnode
->name
);
1249 if (!dm_task_set_name(dmt
, dnode
->name
)) {
1250 log_error("Failed to set device name for %s", dnode
->name
);
1254 if (!dm_task_set_uuid(dmt
, dnode
->uuid
)) {
1255 log_error("Failed to set uuid for %s", dnode
->name
);
1259 if (dnode
->props
.major
&&
1260 (!dm_task_set_major(dmt
, dnode
->props
.major
) ||
1261 !dm_task_set_minor(dmt
, dnode
->props
.minor
))) {
1262 log_error("Failed to set device number for %s creation.", dnode
->name
);
1266 if (dnode
->props
.read_only
&& !dm_task_set_ro(dmt
)) {
1267 log_error("Failed to set read only flag for %s", dnode
->name
);
1271 if (!dm_task_no_open_count(dmt
))
1272 log_error("Failed to disable open_count");
1274 if ((r
= dm_task_run(dmt
)))
1275 r
= dm_task_get_info(dmt
, &dnode
->info
);
1278 dm_task_destroy(dmt
);
1284 static int _build_dev_string(char *devbuf
, size_t bufsize
, struct dm_tree_node
*node
)
1286 if (!dm_format_dev(devbuf
, bufsize
, node
->info
.major
, node
->info
.minor
)) {
1287 log_error("Failed to format %s device number for %s as dm "
1289 node
->name
, node
->uuid
, node
->info
.major
, node
->info
.minor
);
1296 /* simplify string emiting code */
1297 #define EMIT_PARAMS(p, str...)\
1300 if ((w = dm_snprintf(params + p, paramsize - (size_t) p, str)) < 0) {\
1301 stack; /* Out of space */\
1310 * Returns: 1 on success, 0 on failure
1312 static int _emit_areas_line(struct dm_task
*dmt
__attribute((unused
)),
1313 struct load_segment
*seg
, char *params
,
1314 size_t paramsize
, int *pos
)
1316 struct seg_area
*area
;
1317 char devbuf
[DM_FORMAT_DEV_BUFSIZE
];
1318 unsigned first_time
= 1;
1320 dm_list_iterate_items(area
, &seg
->areas
) {
1321 if (!_build_dev_string(devbuf
, sizeof(devbuf
), area
->dev_node
))
1324 EMIT_PARAMS(*pos
, "%s%s %" PRIu64
, first_time
? "" : " ",
1325 devbuf
, area
->offset
);
1334 * Returns: 1 on success, 0 on failure
1336 static int _mirror_emit_segment_line(struct dm_task
*dmt
, uint32_t major
,
1337 uint32_t minor
, struct load_segment
*seg
,
1338 uint64_t *seg_start
, char *params
,
1342 int block_on_error
= 0;
1343 int handle_errors
= 0;
1344 int dm_log_userspace
= 0;
1346 unsigned log_parm_count
;
1348 char logbuf
[DM_FORMAT_DEV_BUFSIZE
];
1349 const char *logtype
;
1355 if ((seg
->flags
& DM_BLOCK_ON_ERROR
)) {
1357 * Originally, block_on_error was an argument to the log
1358 * portion of the mirror CTR table. It was renamed to
1359 * "handle_errors" and now resides in the 'features'
1360 * section of the mirror CTR table (i.e. at the end).
1362 * We can identify whether to use "block_on_error" or
1363 * "handle_errors" by the dm-mirror module's version
1364 * number (>= 1.12) or by the kernel version (>= 2.6.22).
1366 if (strncmp(uts
.release
, "2.6.22", 6) >= 0)
1372 if (seg
->clustered
) {
1373 /* Cluster mirrors require a UUID */
1378 * Cluster mirrors used to have their own log
1379 * types. Now they are accessed through the
1380 * userspace log type.
1382 * The dm-log-userspace module was added to the
1385 if (strncmp(uts
.release
, "2.6.31", 6) >= 0)
1386 dm_log_userspace
= 1;
1392 /* [no]sync, block_on_error etc. */
1393 log_parm_count
+= hweight32(seg
->flags
);
1395 /* "handle_errors" is a feature arg now */
1399 /* DM_CORELOG does not count in the param list */
1400 if (seg
->flags
& DM_CORELOG
)
1403 if (seg
->clustered
) {
1404 log_parm_count
++; /* For UUID */
1406 if (!dm_log_userspace
)
1407 EMIT_PARAMS(pos
, "clustered-");
1415 if (!_build_dev_string(logbuf
, sizeof(logbuf
), seg
->log
))
1419 if (dm_log_userspace
)
1420 EMIT_PARAMS(pos
, "userspace %u %s clustered-%s",
1421 log_parm_count
, seg
->uuid
, logtype
);
1423 EMIT_PARAMS(pos
, "%s %u", logtype
, log_parm_count
);
1426 EMIT_PARAMS(pos
, " %s", logbuf
);
1428 EMIT_PARAMS(pos
, " %u", seg
->region_size
);
1430 if (seg
->clustered
&& !dm_log_userspace
)
1431 EMIT_PARAMS(pos
, " %s", seg
->uuid
);
1433 if ((seg
->flags
& DM_NOSYNC
))
1434 EMIT_PARAMS(pos
, " nosync");
1435 else if ((seg
->flags
& DM_FORCESYNC
))
1436 EMIT_PARAMS(pos
, " sync");
1439 EMIT_PARAMS(pos
, " block_on_error");
1441 EMIT_PARAMS(pos
, " %u ", seg
->mirror_area_count
);
1443 if ((r
= _emit_areas_line(dmt
, seg
, params
, paramsize
, &pos
)) <= 0)
1447 EMIT_PARAMS(pos
, " 1 handle_errors");
1452 static int _emit_segment_line(struct dm_task
*dmt
, uint32_t major
,
1453 uint32_t minor
, struct load_segment
*seg
,
1454 uint64_t *seg_start
, char *params
,
1459 char originbuf
[DM_FORMAT_DEV_BUFSIZE
], cowbuf
[DM_FORMAT_DEV_BUFSIZE
];
1467 /* Mirrors are pretty complicated - now in separate function */
1468 r
= _mirror_emit_segment_line(dmt
, major
, minor
, seg
, seg_start
,
1474 if (!_build_dev_string(originbuf
, sizeof(originbuf
), seg
->origin
))
1476 if (!_build_dev_string(cowbuf
, sizeof(cowbuf
), seg
->cow
))
1478 EMIT_PARAMS(pos
, "%s %s %c %d", originbuf
, cowbuf
,
1479 seg
->persistent
? 'P' : 'N', seg
->chunk_size
);
1481 case SEG_SNAPSHOT_ORIGIN
:
1482 if (!_build_dev_string(originbuf
, sizeof(originbuf
), seg
->origin
))
1484 EMIT_PARAMS(pos
, "%s", originbuf
);
1487 EMIT_PARAMS(pos
, "%u %u ", seg
->area_count
, seg
->stripe_size
);
1490 EMIT_PARAMS(pos
, "%s%s%s%s%s %s %" PRIu64
" ", seg
->cipher
,
1491 seg
->chainmode
? "-" : "", seg
->chainmode
?: "",
1492 seg
->iv
? "-" : "", seg
->iv
?: "", seg
->key
,
1493 seg
->iv_offset
!= DM_CRYPT_IV_DEFAULT
?
1494 seg
->iv_offset
: *seg_start
);
1501 case SEG_SNAPSHOT_ORIGIN
:
1507 if ((r
= _emit_areas_line(dmt
, seg
, params
, paramsize
, &pos
)) <= 0) {
1514 log_debug("Adding target to (%" PRIu32
":%" PRIu32
"): %" PRIu64
1515 " %" PRIu64
" %s %s", major
, minor
,
1516 *seg_start
, seg
->size
, dm_segtypes
[seg
->type
].target
, params
);
1518 if (!dm_task_add_target(dmt
, *seg_start
, seg
->size
, dm_segtypes
[seg
->type
].target
, params
))
1521 *seg_start
+= seg
->size
;
1528 static int _emit_segment(struct dm_task
*dmt
, uint32_t major
, uint32_t minor
,
1529 struct load_segment
*seg
, uint64_t *seg_start
)
1532 size_t paramsize
= 4096;
1536 if (!(params
= dm_malloc(paramsize
))) {
1537 log_error("Insufficient space for target parameters.");
1542 ret
= _emit_segment_line(dmt
, major
, minor
, seg
, seg_start
,
1552 log_debug("Insufficient space in params[%" PRIsize_t
1553 "] for target parameters.", paramsize
);
1556 } while (paramsize
< MAX_TARGET_PARAMSIZE
);
1558 log_error("Target parameter size too big. Aborting.");
1562 static int _load_node(struct dm_tree_node
*dnode
)
1565 struct dm_task
*dmt
;
1566 struct load_segment
*seg
;
1567 uint64_t seg_start
= 0;
1569 log_verbose("Loading %s table (%" PRIu32
":%" PRIu32
")", dnode
->name
,
1570 dnode
->info
.major
, dnode
->info
.minor
);
1572 if (!(dmt
= dm_task_create(DM_DEVICE_RELOAD
))) {
1573 log_error("Reload dm_task creation failed for %s", dnode
->name
);
1577 if (!dm_task_set_major(dmt
, dnode
->info
.major
) ||
1578 !dm_task_set_minor(dmt
, dnode
->info
.minor
)) {
1579 log_error("Failed to set device number for %s reload.", dnode
->name
);
1583 if (dnode
->props
.read_only
&& !dm_task_set_ro(dmt
)) {
1584 log_error("Failed to set read only flag for %s", dnode
->name
);
1588 if (!dm_task_no_open_count(dmt
))
1589 log_error("Failed to disable open_count");
1591 dm_list_iterate_items(seg
, &dnode
->props
.segs
)
1592 if (!_emit_segment(dmt
, dnode
->info
.major
, dnode
->info
.minor
,
1596 if (!dm_task_suppress_identical_reload(dmt
))
1597 log_error("Failed to suppress reload of identical tables.");
1599 if ((r
= dm_task_run(dmt
))) {
1600 r
= dm_task_get_info(dmt
, &dnode
->info
);
1601 if (r
&& !dnode
->info
.inactive_table
)
1602 log_verbose("Suppressed %s identical table reload.",
1605 if ((dnode
->props
.size_changed
=
1606 (dm_task_get_existing_table_size(dmt
) == seg_start
) ? 0 : 1))
1607 log_debug("Table size changed from %" PRIu64
" to %"
1609 dm_task_get_existing_table_size(dmt
),
1610 seg_start
, dnode
->name
);
1613 dnode
->props
.segment_count
= 0;
1616 dm_task_destroy(dmt
);
1621 int dm_tree_preload_children(struct dm_tree_node
*dnode
,
1622 const char *uuid_prefix
,
1623 size_t uuid_prefix_len
)
1626 void *handle
= NULL
;
1627 struct dm_tree_node
*child
;
1628 struct dm_info newinfo
;
1630 /* Preload children first */
1631 while ((child
= dm_tree_next_child(&handle
, dnode
, 0))) {
1632 /* Skip existing non-device-mapper devices */
1633 if (!child
->info
.exists
&& child
->info
.major
)
1636 /* Ignore if it doesn't belong to this VG */
1637 if (child
->info
.exists
&&
1638 !_uuid_prefix_matches(child
->uuid
, uuid_prefix
, uuid_prefix_len
))
1641 if (dm_tree_node_num_children(child
, 0))
1642 if (!dm_tree_preload_children(child
, uuid_prefix
, uuid_prefix_len
))
1645 /* FIXME Cope if name exists with no uuid? */
1646 if (!child
->info
.exists
) {
1647 if (!_create_node(child
)) {
1653 if (!child
->info
.inactive_table
&& child
->props
.segment_count
) {
1654 if (!_load_node(child
)) {
1660 /* Propagate device size change change */
1661 if (child
->props
.size_changed
)
1662 dnode
->props
.size_changed
= 1;
1664 /* Resume device immediately if it has parents and its size changed */
1665 if (!dm_tree_node_num_children(child
, 1) || !child
->props
.size_changed
)
1668 if (!child
->info
.inactive_table
&& !child
->info
.suspended
)
1671 if (!_resume_node(child
->name
, child
->info
.major
, child
->info
.minor
,
1672 child
->props
.read_ahead
, child
->props
.read_ahead_flags
,
1673 &newinfo
, &child
->dtree
->cookie
, child
->udev_flags
)) {
1674 log_error("Unable to resume %s (%" PRIu32
1675 ":%" PRIu32
")", child
->name
, child
->info
.major
,
1681 /* Update cached info */
1682 child
->info
= newinfo
;
1691 * Returns 1 if unsure.
1693 int dm_tree_children_use_uuid(struct dm_tree_node
*dnode
,
1694 const char *uuid_prefix
,
1695 size_t uuid_prefix_len
)
1697 void *handle
= NULL
;
1698 struct dm_tree_node
*child
= dnode
;
1701 while ((child
= dm_tree_next_child(&handle
, dnode
, 0))) {
1702 if (!(uuid
= dm_tree_node_get_uuid(child
))) {
1703 log_error("Failed to get uuid for dtree node.");
1707 if (_uuid_prefix_matches(uuid
, uuid_prefix
, uuid_prefix_len
))
1710 if (dm_tree_node_num_children(child
, 0))
1711 dm_tree_children_use_uuid(child
, uuid_prefix
, uuid_prefix_len
);
1720 static struct load_segment
*_add_segment(struct dm_tree_node
*dnode
, unsigned type
, uint64_t size
)
1722 struct load_segment
*seg
;
1724 if (!(seg
= dm_pool_zalloc(dnode
->dtree
->mem
, sizeof(*seg
)))) {
1725 log_error("dtree node segment allocation failed");
1731 seg
->area_count
= 0;
1732 dm_list_init(&seg
->areas
);
1733 seg
->stripe_size
= 0;
1734 seg
->persistent
= 0;
1735 seg
->chunk_size
= 0;
1739 dm_list_add(&dnode
->props
.segs
, &seg
->list
);
1740 dnode
->props
.segment_count
++;
1745 int dm_tree_node_add_snapshot_origin_target(struct dm_tree_node
*dnode
,
1747 const char *origin_uuid
)
1749 struct load_segment
*seg
;
1750 struct dm_tree_node
*origin_node
;
1752 if (!(seg
= _add_segment(dnode
, SEG_SNAPSHOT_ORIGIN
, size
)))
1755 if (!(origin_node
= dm_tree_find_node_by_uuid(dnode
->dtree
, origin_uuid
))) {
1756 log_error("Couldn't find snapshot origin uuid %s.", origin_uuid
);
1760 seg
->origin
= origin_node
;
1761 if (!_link_tree_nodes(dnode
, origin_node
))
1764 /* Resume snapshot origins after new snapshots */
1765 dnode
->activation_priority
= 1;
1770 int dm_tree_node_add_snapshot_target(struct dm_tree_node
*node
,
1772 const char *origin_uuid
,
1773 const char *cow_uuid
,
1775 uint32_t chunk_size
)
1777 struct load_segment
*seg
;
1778 struct dm_tree_node
*origin_node
, *cow_node
;
1780 if (!(seg
= _add_segment(node
, SEG_SNAPSHOT
, size
)))
1783 if (!(origin_node
= dm_tree_find_node_by_uuid(node
->dtree
, origin_uuid
))) {
1784 log_error("Couldn't find snapshot origin uuid %s.", origin_uuid
);
1788 seg
->origin
= origin_node
;
1789 if (!_link_tree_nodes(node
, origin_node
))
1792 if (!(cow_node
= dm_tree_find_node_by_uuid(node
->dtree
, cow_uuid
))) {
1793 log_error("Couldn't find snapshot origin uuid %s.", cow_uuid
);
1797 seg
->cow
= cow_node
;
1798 if (!_link_tree_nodes(node
, cow_node
))
1801 seg
->persistent
= persistent
? 1 : 0;
1802 seg
->chunk_size
= chunk_size
;
1807 int dm_tree_node_add_error_target(struct dm_tree_node
*node
,
1810 if (!_add_segment(node
, SEG_ERROR
, size
))
1816 int dm_tree_node_add_zero_target(struct dm_tree_node
*node
,
1819 if (!_add_segment(node
, SEG_ZERO
, size
))
1825 int dm_tree_node_add_linear_target(struct dm_tree_node
*node
,
1828 if (!_add_segment(node
, SEG_LINEAR
, size
))
1834 int dm_tree_node_add_striped_target(struct dm_tree_node
*node
,
1836 uint32_t stripe_size
)
1838 struct load_segment
*seg
;
1840 if (!(seg
= _add_segment(node
, SEG_STRIPED
, size
)))
1843 seg
->stripe_size
= stripe_size
;
1848 int dm_tree_node_add_crypt_target(struct dm_tree_node
*node
,
1851 const char *chainmode
,
1856 struct load_segment
*seg
;
1858 if (!(seg
= _add_segment(node
, SEG_CRYPT
, size
)))
1861 seg
->cipher
= cipher
;
1862 seg
->chainmode
= chainmode
;
1864 seg
->iv_offset
= iv_offset
;
1870 int dm_tree_node_add_mirror_target_log(struct dm_tree_node
*node
,
1871 uint32_t region_size
,
1873 const char *log_uuid
,
1874 unsigned area_count
,
1877 struct dm_tree_node
*log_node
= NULL
;
1878 struct load_segment
*seg
;
1880 if (!node
->props
.segment_count
) {
1881 log_error("Internal error: Attempt to add target area to missing segment.");
1885 seg
= dm_list_item(dm_list_last(&node
->props
.segs
), struct load_segment
);
1888 if (!(seg
->uuid
= dm_pool_strdup(node
->dtree
->mem
, log_uuid
))) {
1889 log_error("log uuid pool_strdup failed");
1892 if (!(flags
& DM_CORELOG
)) {
1893 if (!(log_node
= dm_tree_find_node_by_uuid(node
->dtree
, log_uuid
))) {
1894 log_error("Couldn't find mirror log uuid %s.", log_uuid
);
1898 if (!_link_tree_nodes(node
, log_node
))
1903 seg
->log
= log_node
;
1904 seg
->region_size
= region_size
;
1905 seg
->clustered
= clustered
;
1906 seg
->mirror_area_count
= area_count
;
1912 int dm_tree_node_add_mirror_target(struct dm_tree_node
*node
,
1915 struct load_segment
*seg
;
1917 if (!(seg
= _add_segment(node
, SEG_MIRRORED
, size
)))
1923 static int _add_area(struct dm_tree_node
*node
, struct load_segment
*seg
, struct dm_tree_node
*dev_node
, uint64_t offset
)
1925 struct seg_area
*area
;
1927 if (!(area
= dm_pool_zalloc(node
->dtree
->mem
, sizeof (*area
)))) {
1928 log_error("Failed to allocate target segment area.");
1932 area
->dev_node
= dev_node
;
1933 area
->offset
= offset
;
1935 dm_list_add(&seg
->areas
, &area
->list
);
1941 int dm_tree_node_add_target_area(struct dm_tree_node
*node
,
1942 const char *dev_name
,
1946 struct load_segment
*seg
;
1948 struct dm_tree_node
*dev_node
;
1950 if ((!dev_name
|| !*dev_name
) && (!uuid
|| !*uuid
)) {
1951 log_error("dm_tree_node_add_target_area called without device");
1956 if (!(dev_node
= dm_tree_find_node_by_uuid(node
->dtree
, uuid
))) {
1957 log_error("Couldn't find area uuid %s.", uuid
);
1960 if (!_link_tree_nodes(node
, dev_node
))
1963 if (stat(dev_name
, &info
) < 0) {
1964 log_error("Device %s not found.", dev_name
);
1968 if (!S_ISBLK(info
.st_mode
)) {
1969 log_error("Device %s is not a block device.", dev_name
);
1973 /* FIXME Check correct macro use */
1974 if (!(dev_node
= _add_dev(node
->dtree
, node
, MAJOR(info
.st_rdev
),
1975 MINOR(info
.st_rdev
), 0)))
1979 if (!node
->props
.segment_count
) {
1980 log_error("Internal error: Attempt to add target area to missing segment.");
1984 seg
= dm_list_item(dm_list_last(&node
->props
.segs
), struct load_segment
);
1986 if (!_add_area(node
, seg
, dev_node
, offset
))
1992 void dm_tree_set_cookie(struct dm_tree_node
*node
, uint32_t cookie
)
1994 node
->dtree
->cookie
= cookie
;
1997 uint32_t dm_tree_get_cookie(struct dm_tree_node
*node
)
1999 return node
->dtree
->cookie
;