2 * Copyright (C) 2005-2010 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 #define REPLICATOR_LOCAL_SITE 0
32 /* Supported segment types */
47 /* FIXME Add crypt and multipath support */
53 { SEG_CRYPT
, "crypt" },
54 { SEG_ERROR
, "error" },
55 { SEG_LINEAR
, "linear" },
56 { SEG_MIRRORED
, "mirror" },
57 { SEG_REPLICATOR
, "replicator" },
58 { SEG_REPLICATOR_DEV
, "replicator-dev" },
59 { SEG_SNAPSHOT
, "snapshot" },
60 { SEG_SNAPSHOT_ORIGIN
, "snapshot-origin" },
61 { SEG_SNAPSHOT_MERGE
, "snapshot-merge" },
62 { SEG_STRIPED
, "striped" },
66 /* Some segment types have a list of areas of other devices attached */
70 struct dm_tree_node
*dev_node
;
74 unsigned rsite_index
; /* Replicator site index */
75 struct dm_tree_node
*slog
; /* Replicator sync log node */
76 uint64_t region_size
; /* Replicator sync log size */
77 uint32_t flags
; /* Replicator sync log flags */
80 /* Replicator-log has a list of sites */
81 /* FIXME: maybe move to seg_area too? */
82 struct replicator_site
{
86 dm_replicator_mode_t mode
;
87 uint32_t async_timeout
;
88 uint32_t fall_behind_ios
;
89 uint64_t fall_behind_data
;
92 /* Per-segment properties */
100 unsigned area_count
; /* Linear + Striped + Mirrored + Crypt + Replicator */
101 struct dm_list areas
; /* Linear + Striped + Mirrored + Crypt + Replicator */
103 uint32_t stripe_size
; /* Striped */
105 int persistent
; /* Snapshot */
106 uint32_t chunk_size
; /* Snapshot */
107 struct dm_tree_node
*cow
; /* Snapshot */
108 struct dm_tree_node
*origin
; /* Snapshot + Snapshot origin */
109 struct dm_tree_node
*merge
; /* Snapshot */
111 struct dm_tree_node
*log
; /* Mirror + Replicator */
112 uint32_t region_size
; /* Mirror */
113 unsigned clustered
; /* Mirror */
114 unsigned mirror_area_count
; /* Mirror */
115 uint32_t flags
; /* Mirror log */
116 char *uuid
; /* Clustered mirror log */
118 const char *cipher
; /* Crypt */
119 const char *chainmode
; /* Crypt */
120 const char *iv
; /* Crypt */
121 uint64_t iv_offset
; /* Crypt */
122 const char *key
; /* Crypt */
124 const char *rlog_type
; /* Replicator */
125 struct dm_list rsites
; /* Replicator */
126 unsigned rsite_count
; /* Replicator */
127 unsigned rdevice_count
; /* Replicator */
128 struct dm_tree_node
*replicator
;/* Replicator-dev */
129 uint64_t rdevice_index
; /* Replicator-dev */
132 /* Per-device properties */
133 struct load_properties
{
139 uint32_t read_ahead_flags
;
141 unsigned segment_count
;
142 unsigned size_changed
;
145 const char *new_name
;
147 /* If immediate_dev_node is set to 1, try to create the dev node
148 * as soon as possible (e.g. in preload stage even during traversal
149 * and processing of dm tree). This will also flush all stacked dev
150 * node operations, synchronizing with udev.
152 int immediate_dev_node
;
155 /* Two of these used to join two nodes with uses and used_by. */
156 struct dm_tree_link
{
158 struct dm_tree_node
*node
;
161 struct dm_tree_node
{
162 struct dm_tree
*dtree
;
168 struct dm_list uses
; /* Nodes this node uses */
169 struct dm_list used_by
; /* Nodes that use this node */
171 int activation_priority
; /* 0 gets activated first */
173 uint16_t udev_flags
; /* Udev control flags */
175 void *context
; /* External supplied context */
177 struct load_properties props
; /* For creation/table (re)load */
180 * If presuspend of child node is needed
181 * Note: only direct child is allowed
183 struct dm_tree_node
*presuspend_node
;
188 struct dm_hash_table
*devs
;
189 struct dm_hash_table
*uuids
;
190 struct dm_tree_node root
;
191 int skip_lockfs
; /* 1 skips lockfs (for non-snapshots) */
192 int no_flush
; /* 1 sets noflush (mirrors/multipath) */
196 struct dm_tree
*dm_tree_create(void)
198 struct dm_tree
*dtree
;
200 if (!(dtree
= dm_zalloc(sizeof(*dtree
)))) {
201 log_error("dm_tree_create malloc failed");
205 dtree
->root
.dtree
= dtree
;
206 dm_list_init(&dtree
->root
.uses
);
207 dm_list_init(&dtree
->root
.used_by
);
208 dtree
->skip_lockfs
= 0;
211 if (!(dtree
->mem
= dm_pool_create("dtree", 1024))) {
212 log_error("dtree pool creation failed");
217 if (!(dtree
->devs
= dm_hash_create(8))) {
218 log_error("dtree hash creation failed");
219 dm_pool_destroy(dtree
->mem
);
224 if (!(dtree
->uuids
= dm_hash_create(32))) {
225 log_error("dtree uuid hash creation failed");
226 dm_hash_destroy(dtree
->devs
);
227 dm_pool_destroy(dtree
->mem
);
235 void dm_tree_free(struct dm_tree
*dtree
)
240 dm_hash_destroy(dtree
->uuids
);
241 dm_hash_destroy(dtree
->devs
);
242 dm_pool_destroy(dtree
->mem
);
246 static int _nodes_are_linked(const struct dm_tree_node
*parent
,
247 const struct dm_tree_node
*child
)
249 struct dm_tree_link
*dlink
;
251 dm_list_iterate_items(dlink
, &parent
->uses
)
252 if (dlink
->node
== child
)
258 static int _link(struct dm_list
*list
, struct dm_tree_node
*node
)
260 struct dm_tree_link
*dlink
;
262 if (!(dlink
= dm_pool_alloc(node
->dtree
->mem
, sizeof(*dlink
)))) {
263 log_error("dtree link allocation failed");
268 dm_list_add(list
, &dlink
->list
);
273 static int _link_nodes(struct dm_tree_node
*parent
,
274 struct dm_tree_node
*child
)
276 if (_nodes_are_linked(parent
, child
))
279 if (!_link(&parent
->uses
, child
))
282 if (!_link(&child
->used_by
, parent
))
288 static void _unlink(struct dm_list
*list
, struct dm_tree_node
*node
)
290 struct dm_tree_link
*dlink
;
292 dm_list_iterate_items(dlink
, list
)
293 if (dlink
->node
== node
) {
294 dm_list_del(&dlink
->list
);
299 static void _unlink_nodes(struct dm_tree_node
*parent
,
300 struct dm_tree_node
*child
)
302 if (!_nodes_are_linked(parent
, child
))
305 _unlink(&parent
->uses
, child
);
306 _unlink(&child
->used_by
, parent
);
309 static int _add_to_toplevel(struct dm_tree_node
*node
)
311 return _link_nodes(&node
->dtree
->root
, node
);
314 static void _remove_from_toplevel(struct dm_tree_node
*node
)
316 _unlink_nodes(&node
->dtree
->root
, node
);
319 static int _add_to_bottomlevel(struct dm_tree_node
*node
)
321 return _link_nodes(node
, &node
->dtree
->root
);
324 static void _remove_from_bottomlevel(struct dm_tree_node
*node
)
326 _unlink_nodes(node
, &node
->dtree
->root
);
329 static int _link_tree_nodes(struct dm_tree_node
*parent
, struct dm_tree_node
*child
)
331 /* Don't link to root node if child already has a parent */
332 if ((parent
== &parent
->dtree
->root
)) {
333 if (dm_tree_node_num_children(child
, 1))
336 _remove_from_toplevel(child
);
338 if ((child
== &child
->dtree
->root
)) {
339 if (dm_tree_node_num_children(parent
, 0))
342 _remove_from_bottomlevel(parent
);
344 return _link_nodes(parent
, child
);
347 static struct dm_tree_node
*_create_dm_tree_node(struct dm_tree
*dtree
,
350 struct dm_info
*info
,
354 struct dm_tree_node
*node
;
357 if (!(node
= dm_pool_zalloc(dtree
->mem
, sizeof(*node
)))) {
358 log_error("_create_dm_tree_node alloc failed");
367 node
->context
= context
;
368 node
->udev_flags
= udev_flags
;
369 node
->activation_priority
= 0;
371 dm_list_init(&node
->uses
);
372 dm_list_init(&node
->used_by
);
373 dm_list_init(&node
->props
.segs
);
375 dev
= MKDEV(info
->major
, info
->minor
);
377 if (!dm_hash_insert_binary(dtree
->devs
, (const char *) &dev
,
378 sizeof(dev
), node
)) {
379 log_error("dtree node hash insertion failed");
380 dm_pool_free(dtree
->mem
, node
);
385 !dm_hash_insert(dtree
->uuids
, uuid
, node
)) {
386 log_error("dtree uuid hash insertion failed");
387 dm_hash_remove_binary(dtree
->devs
, (const char *) &dev
,
389 dm_pool_free(dtree
->mem
, node
);
396 static struct dm_tree_node
*_find_dm_tree_node(struct dm_tree
*dtree
,
397 uint32_t major
, uint32_t minor
)
399 uint64_t dev
= MKDEV(major
, minor
);
401 return dm_hash_lookup_binary(dtree
->devs
, (const char *) &dev
,
405 static struct dm_tree_node
*_find_dm_tree_node_by_uuid(struct dm_tree
*dtree
,
408 struct dm_tree_node
*node
;
410 if ((node
= dm_hash_lookup(dtree
->uuids
, uuid
)))
413 if (strncmp(uuid
, UUID_PREFIX
, sizeof(UUID_PREFIX
) - 1))
416 return dm_hash_lookup(dtree
->uuids
, uuid
+ sizeof(UUID_PREFIX
) - 1);
419 static int _deps(struct dm_task
**dmt
, struct dm_pool
*mem
, uint32_t major
, uint32_t minor
,
420 const char **name
, const char **uuid
,
421 struct dm_info
*info
, struct dm_deps
**deps
)
423 memset(info
, 0, sizeof(*info
));
425 if (!dm_is_dm_major(major
)) {
432 info
->live_table
= 0;
433 info
->inactive_table
= 0;
438 if (!(*dmt
= dm_task_create(DM_DEVICE_DEPS
))) {
439 log_error("deps dm_task creation failed");
443 if (!dm_task_set_major(*dmt
, major
)) {
444 log_error("_deps: failed to set major for (%" PRIu32
":%" PRIu32
")",
449 if (!dm_task_set_minor(*dmt
, minor
)) {
450 log_error("_deps: failed to set minor for (%" PRIu32
":%" PRIu32
")",
455 if (!dm_task_run(*dmt
)) {
456 log_error("_deps: task run failed for (%" PRIu32
":%" PRIu32
")",
461 if (!dm_task_get_info(*dmt
, info
)) {
462 log_error("_deps: failed to get info for (%" PRIu32
":%" PRIu32
")",
472 if (info
->major
!= major
) {
473 log_error("Inconsistent dtree major number: %u != %u",
477 if (info
->minor
!= minor
) {
478 log_error("Inconsistent dtree minor number: %u != %u",
482 if (!(*name
= dm_pool_strdup(mem
, dm_task_get_name(*dmt
)))) {
483 log_error("name pool_strdup failed");
486 if (!(*uuid
= dm_pool_strdup(mem
, dm_task_get_uuid(*dmt
)))) {
487 log_error("uuid pool_strdup failed");
490 *deps
= dm_task_get_deps(*dmt
);
496 dm_task_destroy(*dmt
);
500 static struct dm_tree_node
*_add_dev(struct dm_tree
*dtree
,
501 struct dm_tree_node
*parent
,
502 uint32_t major
, uint32_t minor
,
505 struct dm_task
*dmt
= NULL
;
507 struct dm_deps
*deps
= NULL
;
508 const char *name
= NULL
;
509 const char *uuid
= NULL
;
510 struct dm_tree_node
*node
= NULL
;
514 /* Already in tree? */
515 if (!(node
= _find_dm_tree_node(dtree
, major
, minor
))) {
516 if (!_deps(&dmt
, dtree
->mem
, major
, minor
, &name
, &uuid
, &info
, &deps
))
519 if (!(node
= _create_dm_tree_node(dtree
, name
, uuid
, &info
,
525 if (!_link_tree_nodes(parent
, node
)) {
530 /* If node was already in tree, no need to recurse. */
534 /* Can't recurse if not a mapped device or there are no dependencies */
535 if (!node
->info
.exists
|| !deps
->count
) {
536 if (!_add_to_bottomlevel(node
)) {
543 /* Add dependencies to tree */
544 for (i
= 0; i
< deps
->count
; i
++)
545 if (!_add_dev(dtree
, node
, MAJOR(deps
->device
[i
]),
546 MINOR(deps
->device
[i
]), udev_flags
)) {
553 dm_task_destroy(dmt
);
558 static int _node_clear_table(struct dm_tree_node
*dnode
)
561 struct dm_info
*info
;
565 if (!(info
= &dnode
->info
)) {
566 log_error("_node_clear_table failed: missing info");
570 if (!(name
= dm_tree_node_get_name(dnode
))) {
571 log_error("_node_clear_table failed: missing name");
575 /* Is there a table? */
576 if (!info
->exists
|| !info
->inactive_table
)
579 log_verbose("Clearing inactive table %s (%" PRIu32
":%" PRIu32
")",
580 name
, info
->major
, info
->minor
);
582 if (!(dmt
= dm_task_create(DM_DEVICE_CLEAR
))) {
583 log_error("Table clear dm_task creation failed for %s", name
);
587 if (!dm_task_set_major(dmt
, info
->major
) ||
588 !dm_task_set_minor(dmt
, info
->minor
)) {
589 log_error("Failed to set device number for %s table clear", name
);
590 dm_task_destroy(dmt
);
594 r
= dm_task_run(dmt
);
596 if (!dm_task_get_info(dmt
, info
)) {
597 log_error("_node_clear_table failed: info missing after running task for %s", name
);
601 dm_task_destroy(dmt
);
606 struct dm_tree_node
*dm_tree_add_new_dev(struct dm_tree
*dtree
,
609 uint32_t major
, uint32_t minor
,
614 struct dm_tree_node
*dnode
;
619 /* Do we need to add node to tree? */
620 if (!(dnode
= dm_tree_find_node_by_uuid(dtree
, uuid
))) {
621 if (!(name2
= dm_pool_strdup(dtree
->mem
, name
))) {
622 log_error("name pool_strdup failed");
625 if (!(uuid2
= dm_pool_strdup(dtree
->mem
, uuid
))) {
626 log_error("uuid pool_strdup failed");
634 info
.inactive_table
= 0;
637 if (!(dnode
= _create_dm_tree_node(dtree
, name2
, uuid2
, &info
,
641 /* Attach to root node until a table is supplied */
642 if (!_add_to_toplevel(dnode
) || !_add_to_bottomlevel(dnode
))
645 dnode
->props
.major
= major
;
646 dnode
->props
.minor
= minor
;
647 dnode
->props
.new_name
= NULL
;
648 dnode
->props
.size_changed
= 0;
649 } else if (strcmp(name
, dnode
->name
)) {
650 /* Do we need to rename node? */
651 if (!(dnode
->props
.new_name
= dm_pool_strdup(dtree
->mem
, name
))) {
652 log_error("name pool_strdup failed");
657 dnode
->props
.read_only
= read_only
? 1 : 0;
658 dnode
->props
.read_ahead
= DM_READ_AHEAD_AUTO
;
659 dnode
->props
.read_ahead_flags
= 0;
661 if (clear_inactive
&& !_node_clear_table(dnode
))
664 dnode
->context
= context
;
665 dnode
->udev_flags
= 0;
670 struct dm_tree_node
*dm_tree_add_new_dev_with_udev_flags(struct dm_tree
*dtree
,
680 struct dm_tree_node
*node
;
682 if ((node
= dm_tree_add_new_dev(dtree
, name
, uuid
, major
, minor
, read_only
,
683 clear_inactive
, context
)))
684 node
->udev_flags
= udev_flags
;
690 void dm_tree_node_set_read_ahead(struct dm_tree_node
*dnode
,
692 uint32_t read_ahead_flags
)
694 dnode
->props
.read_ahead
= read_ahead
;
695 dnode
->props
.read_ahead_flags
= read_ahead_flags
;
698 void dm_tree_node_set_presuspend_node(struct dm_tree_node
*node
,
699 struct dm_tree_node
*presuspend_node
)
701 node
->presuspend_node
= presuspend_node
;
704 int dm_tree_add_dev(struct dm_tree
*dtree
, uint32_t major
, uint32_t minor
)
706 return _add_dev(dtree
, &dtree
->root
, major
, minor
, 0) ? 1 : 0;
709 int dm_tree_add_dev_with_udev_flags(struct dm_tree
*dtree
, uint32_t major
,
710 uint32_t minor
, uint16_t udev_flags
)
712 return _add_dev(dtree
, &dtree
->root
, major
, minor
, udev_flags
) ? 1 : 0;
715 const char *dm_tree_node_get_name(const struct dm_tree_node
*node
)
717 return node
->info
.exists
? node
->name
: "";
720 const char *dm_tree_node_get_uuid(const struct dm_tree_node
*node
)
722 return node
->info
.exists
? node
->uuid
: "";
725 const struct dm_info
*dm_tree_node_get_info(const struct dm_tree_node
*node
)
730 void *dm_tree_node_get_context(const struct dm_tree_node
*node
)
732 return node
->context
;
735 int dm_tree_node_size_changed(const struct dm_tree_node
*dnode
)
737 return dnode
->props
.size_changed
;
740 int dm_tree_node_num_children(const struct dm_tree_node
*node
, uint32_t inverted
)
743 if (_nodes_are_linked(&node
->dtree
->root
, node
))
745 return dm_list_size(&node
->used_by
);
748 if (_nodes_are_linked(node
, &node
->dtree
->root
))
751 return dm_list_size(&node
->uses
);
755 * Returns 1 if no prefix supplied
757 static int _uuid_prefix_matches(const char *uuid
, const char *uuid_prefix
, size_t uuid_prefix_len
)
762 if (!strncmp(uuid
, uuid_prefix
, uuid_prefix_len
))
765 /* Handle transition: active device uuids might be missing the prefix */
766 if (uuid_prefix_len
<= 4)
769 if (!strncmp(uuid
, UUID_PREFIX
, sizeof(UUID_PREFIX
) - 1))
772 if (strncmp(uuid_prefix
, UUID_PREFIX
, sizeof(UUID_PREFIX
) - 1))
775 if (!strncmp(uuid
, uuid_prefix
+ sizeof(UUID_PREFIX
) - 1, uuid_prefix_len
- (sizeof(UUID_PREFIX
) - 1)))
782 * Returns 1 if no children.
784 static int _children_suspended(struct dm_tree_node
*node
,
786 const char *uuid_prefix
,
787 size_t uuid_prefix_len
)
789 struct dm_list
*list
;
790 struct dm_tree_link
*dlink
;
791 const struct dm_info
*dinfo
;
795 if (_nodes_are_linked(&node
->dtree
->root
, node
))
797 list
= &node
->used_by
;
799 if (_nodes_are_linked(node
, &node
->dtree
->root
))
804 dm_list_iterate_items(dlink
, list
) {
805 if (!(uuid
= dm_tree_node_get_uuid(dlink
->node
))) {
810 /* Ignore if it doesn't belong to this VG */
811 if (!_uuid_prefix_matches(uuid
, uuid_prefix
, uuid_prefix_len
))
814 /* Ignore if parent node wants to presuspend this node */
815 if (dlink
->node
->presuspend_node
== node
)
818 if (!(dinfo
= dm_tree_node_get_info(dlink
->node
))) {
819 stack
; /* FIXME Is this normal? */
823 if (!dinfo
->suspended
)
831 * Set major and minor to zero for root of tree.
833 struct dm_tree_node
*dm_tree_find_node(struct dm_tree
*dtree
,
837 if (!major
&& !minor
)
840 return _find_dm_tree_node(dtree
, major
, minor
);
844 * Set uuid to NULL for root of tree.
846 struct dm_tree_node
*dm_tree_find_node_by_uuid(struct dm_tree
*dtree
,
852 return _find_dm_tree_node_by_uuid(dtree
, uuid
);
856 * First time set *handle to NULL.
857 * Set inverted to invert the tree.
859 struct dm_tree_node
*dm_tree_next_child(void **handle
,
860 const struct dm_tree_node
*parent
,
863 struct dm_list
**dlink
= (struct dm_list
**) handle
;
864 const struct dm_list
*use_list
;
867 use_list
= &parent
->used_by
;
869 use_list
= &parent
->uses
;
872 *dlink
= dm_list_first(use_list
);
874 *dlink
= dm_list_next(use_list
, *dlink
);
876 return (*dlink
) ? dm_list_item(*dlink
, struct dm_tree_link
)->node
: NULL
;
880 * Deactivate a device with its dependencies if the uuid prefix matches.
882 static int _info_by_dev(uint32_t major
, uint32_t minor
, int with_open_count
,
883 struct dm_info
*info
)
888 if (!(dmt
= dm_task_create(DM_DEVICE_INFO
))) {
889 log_error("_info_by_dev: dm_task creation failed");
893 if (!dm_task_set_major(dmt
, major
) || !dm_task_set_minor(dmt
, minor
)) {
894 log_error("_info_by_dev: Failed to set device number");
895 dm_task_destroy(dmt
);
899 if (!with_open_count
&& !dm_task_no_open_count(dmt
))
900 log_error("Failed to disable open_count");
902 if ((r
= dm_task_run(dmt
)))
903 r
= dm_task_get_info(dmt
, info
);
905 dm_task_destroy(dmt
);
910 /* Check if all parent nodes of given node have open_count == 0 */
911 static int _node_has_closed_parents(struct dm_tree_node
*node
,
912 const char *uuid_prefix
,
913 size_t uuid_prefix_len
)
915 struct dm_tree_link
*dlink
;
916 const struct dm_info
*dinfo
;
920 /* Iterate through parents of this node */
921 dm_list_iterate_items(dlink
, &node
->used_by
) {
922 if (!(uuid
= dm_tree_node_get_uuid(dlink
->node
))) {
927 /* Ignore if it doesn't belong to this VG */
928 if (!_uuid_prefix_matches(uuid
, uuid_prefix
, uuid_prefix_len
))
931 if (!(dinfo
= dm_tree_node_get_info(dlink
->node
))) {
932 stack
; /* FIXME Is this normal? */
936 /* Refresh open_count */
937 if (!_info_by_dev(dinfo
->major
, dinfo
->minor
, 1, &info
) ||
941 if (info
.open_count
) {
942 log_debug("Node %s %d:%d has open_count %d", uuid_prefix
,
943 dinfo
->major
, dinfo
->minor
, info
.open_count
);
951 static int _deactivate_node(const char *name
, uint32_t major
, uint32_t minor
,
952 uint32_t *cookie
, uint16_t udev_flags
)
957 log_verbose("Removing %s (%" PRIu32
":%" PRIu32
")", name
, major
, minor
);
959 if (!(dmt
= dm_task_create(DM_DEVICE_REMOVE
))) {
960 log_error("Deactivation dm_task creation failed for %s", name
);
964 if (!dm_task_set_major(dmt
, major
) || !dm_task_set_minor(dmt
, minor
)) {
965 log_error("Failed to set device number for %s deactivation", name
);
969 if (!dm_task_no_open_count(dmt
))
970 log_error("Failed to disable open_count");
972 if (!dm_task_set_cookie(dmt
, cookie
, udev_flags
))
975 r
= dm_task_run(dmt
);
977 /* FIXME Until kernel returns actual name so dm-ioctl.c can handle it */
978 rm_dev_node(name
, dmt
->cookie_set
&&
979 !(udev_flags
& DM_UDEV_DISABLE_DM_RULES_FLAG
));
981 /* FIXME Remove node from tree or mark invalid? */
984 dm_task_destroy(dmt
);
989 static int _rename_node(const char *old_name
, const char *new_name
, uint32_t major
,
990 uint32_t minor
, uint32_t *cookie
, uint16_t udev_flags
)
995 log_verbose("Renaming %s (%" PRIu32
":%" PRIu32
") to %s", old_name
, major
, minor
, new_name
);
997 if (!(dmt
= dm_task_create(DM_DEVICE_RENAME
))) {
998 log_error("Rename dm_task creation failed for %s", old_name
);
1002 if (!dm_task_set_name(dmt
, old_name
)) {
1003 log_error("Failed to set name for %s rename.", old_name
);
1007 if (!dm_task_set_newname(dmt
, new_name
))
1010 if (!dm_task_no_open_count(dmt
))
1011 log_error("Failed to disable open_count");
1013 if (!dm_task_set_cookie(dmt
, cookie
, udev_flags
))
1016 r
= dm_task_run(dmt
);
1019 dm_task_destroy(dmt
);
1024 /* FIXME Merge with _suspend_node? */
1025 static int _resume_node(const char *name
, uint32_t major
, uint32_t minor
,
1026 uint32_t read_ahead
, uint32_t read_ahead_flags
,
1027 struct dm_info
*newinfo
, uint32_t *cookie
,
1028 uint16_t udev_flags
)
1030 struct dm_task
*dmt
;
1033 log_verbose("Resuming %s (%" PRIu32
":%" PRIu32
")", name
, major
, minor
);
1035 if (!(dmt
= dm_task_create(DM_DEVICE_RESUME
))) {
1036 log_error("Suspend dm_task creation failed for %s", name
);
1040 /* FIXME Kernel should fill in name on return instead */
1041 if (!dm_task_set_name(dmt
, name
)) {
1042 log_error("Failed to set readahead device name for %s", name
);
1046 if (!dm_task_set_major(dmt
, major
) || !dm_task_set_minor(dmt
, minor
)) {
1047 log_error("Failed to set device number for %s resumption.", name
);
1051 if (!dm_task_no_open_count(dmt
))
1052 log_error("Failed to disable open_count");
1054 if (!dm_task_set_read_ahead(dmt
, read_ahead
, read_ahead_flags
))
1055 log_error("Failed to set read ahead");
1057 if (!dm_task_set_cookie(dmt
, cookie
, udev_flags
))
1060 if ((r
= dm_task_run(dmt
)))
1061 r
= dm_task_get_info(dmt
, newinfo
);
1064 dm_task_destroy(dmt
);
1069 static int _suspend_node(const char *name
, uint32_t major
, uint32_t minor
,
1070 int skip_lockfs
, int no_flush
, struct dm_info
*newinfo
)
1072 struct dm_task
*dmt
;
1075 log_verbose("Suspending %s (%" PRIu32
":%" PRIu32
")%s%s",
1077 skip_lockfs
? "" : " with filesystem sync",
1078 no_flush
? "" : " with device flush");
1080 if (!(dmt
= dm_task_create(DM_DEVICE_SUSPEND
))) {
1081 log_error("Suspend dm_task creation failed for %s", name
);
1085 if (!dm_task_set_major(dmt
, major
) || !dm_task_set_minor(dmt
, minor
)) {
1086 log_error("Failed to set device number for %s suspension.", name
);
1087 dm_task_destroy(dmt
);
1091 if (!dm_task_no_open_count(dmt
))
1092 log_error("Failed to disable open_count");
1094 if (skip_lockfs
&& !dm_task_skip_lockfs(dmt
))
1095 log_error("Failed to set skip_lockfs flag.");
1097 if (no_flush
&& !dm_task_no_flush(dmt
))
1098 log_error("Failed to set no_flush flag.");
1100 if ((r
= dm_task_run(dmt
)))
1101 r
= dm_task_get_info(dmt
, newinfo
);
1103 dm_task_destroy(dmt
);
1109 * FIXME Don't attempt to deactivate known internal dependencies.
1111 static int _dm_tree_deactivate_children(struct dm_tree_node
*dnode
,
1112 const char *uuid_prefix
,
1113 size_t uuid_prefix_len
,
1117 void *handle
= NULL
;
1118 struct dm_tree_node
*child
= dnode
;
1119 struct dm_info info
;
1120 const struct dm_info
*dinfo
;
1124 while ((child
= dm_tree_next_child(&handle
, dnode
, 0))) {
1125 if (!(dinfo
= dm_tree_node_get_info(child
))) {
1130 if (!(name
= dm_tree_node_get_name(child
))) {
1135 if (!(uuid
= dm_tree_node_get_uuid(child
))) {
1140 /* Ignore if it doesn't belong to this VG */
1141 if (!_uuid_prefix_matches(uuid
, uuid_prefix
, uuid_prefix_len
))
1144 /* Refresh open_count */
1145 if (!_info_by_dev(dinfo
->major
, dinfo
->minor
, 1, &info
) ||
1149 /* Also checking open_count in parent nodes of presuspend_node */
1150 if (info
.open_count
||
1151 (child
->presuspend_node
&&
1152 !_node_has_closed_parents(child
->presuspend_node
,
1153 uuid_prefix
, uuid_prefix_len
))) {
1154 /* Only report error from (likely non-internal) dependency at top level */
1156 log_error("Unable to deactivate open %s (%" PRIu32
1157 ":%" PRIu32
")", name
, info
.major
,
1164 /* Suspend child node first if requested */
1165 if (child
->presuspend_node
&&
1166 !dm_tree_suspend_children(child
, uuid_prefix
, uuid_prefix_len
))
1169 if (!_deactivate_node(name
, info
.major
, info
.minor
,
1170 &child
->dtree
->cookie
, child
->udev_flags
)) {
1171 log_error("Unable to deactivate %s (%" PRIu32
1172 ":%" PRIu32
")", name
, info
.major
,
1178 if (dm_tree_node_num_children(child
, 0)) {
1179 if (!_dm_tree_deactivate_children(child
, uuid_prefix
, uuid_prefix_len
, level
+ 1))
1187 int dm_tree_deactivate_children(struct dm_tree_node
*dnode
,
1188 const char *uuid_prefix
,
1189 size_t uuid_prefix_len
)
1191 return _dm_tree_deactivate_children(dnode
, uuid_prefix
, uuid_prefix_len
, 0);
1194 void dm_tree_skip_lockfs(struct dm_tree_node
*dnode
)
1196 dnode
->dtree
->skip_lockfs
= 1;
1199 void dm_tree_use_no_flush_suspend(struct dm_tree_node
*dnode
)
1201 dnode
->dtree
->no_flush
= 1;
1204 int dm_tree_suspend_children(struct dm_tree_node
*dnode
,
1205 const char *uuid_prefix
,
1206 size_t uuid_prefix_len
)
1209 void *handle
= NULL
;
1210 struct dm_tree_node
*child
= dnode
;
1211 struct dm_info info
, newinfo
;
1212 const struct dm_info
*dinfo
;
1216 /* Suspend nodes at this level of the tree */
1217 while ((child
= dm_tree_next_child(&handle
, dnode
, 0))) {
1218 if (!(dinfo
= dm_tree_node_get_info(child
))) {
1223 if (!(name
= dm_tree_node_get_name(child
))) {
1228 if (!(uuid
= dm_tree_node_get_uuid(child
))) {
1233 /* Ignore if it doesn't belong to this VG */
1234 if (!_uuid_prefix_matches(uuid
, uuid_prefix
, uuid_prefix_len
))
1237 /* Ensure immediate parents are already suspended */
1238 if (!_children_suspended(child
, 1, uuid_prefix
, uuid_prefix_len
))
1241 if (!_info_by_dev(dinfo
->major
, dinfo
->minor
, 0, &info
) ||
1242 !info
.exists
|| info
.suspended
)
1245 if (!_suspend_node(name
, info
.major
, info
.minor
,
1246 child
->dtree
->skip_lockfs
,
1247 child
->dtree
->no_flush
, &newinfo
)) {
1248 log_error("Unable to suspend %s (%" PRIu32
1249 ":%" PRIu32
")", name
, info
.major
,
1255 /* Update cached info */
1256 child
->info
= newinfo
;
1259 /* Then suspend any child nodes */
1262 while ((child
= dm_tree_next_child(&handle
, dnode
, 0))) {
1263 if (!(uuid
= dm_tree_node_get_uuid(child
))) {
1268 /* Ignore if it doesn't belong to this VG */
1269 if (!_uuid_prefix_matches(uuid
, uuid_prefix
, uuid_prefix_len
))
1272 if (dm_tree_node_num_children(child
, 0))
1273 if (!dm_tree_suspend_children(child
, uuid_prefix
, uuid_prefix_len
))
1280 int dm_tree_activate_children(struct dm_tree_node
*dnode
,
1281 const char *uuid_prefix
,
1282 size_t uuid_prefix_len
)
1285 void *handle
= NULL
;
1286 struct dm_tree_node
*child
= dnode
;
1287 struct dm_info newinfo
;
1292 /* Activate children first */
1293 while ((child
= dm_tree_next_child(&handle
, dnode
, 0))) {
1294 if (!(uuid
= dm_tree_node_get_uuid(child
))) {
1299 if (!_uuid_prefix_matches(uuid
, uuid_prefix
, uuid_prefix_len
))
1302 if (dm_tree_node_num_children(child
, 0))
1303 if (!dm_tree_activate_children(child
, uuid_prefix
, uuid_prefix_len
))
1309 for (priority
= 0; priority
< 3; priority
++) {
1310 while ((child
= dm_tree_next_child(&handle
, dnode
, 0))) {
1311 if (!(uuid
= dm_tree_node_get_uuid(child
))) {
1316 if (!_uuid_prefix_matches(uuid
, uuid_prefix
, uuid_prefix_len
))
1319 if (priority
!= child
->activation_priority
)
1322 if (!(name
= dm_tree_node_get_name(child
))) {
1328 if (child
->props
.new_name
) {
1329 if (!_rename_node(name
, child
->props
.new_name
, child
->info
.major
,
1330 child
->info
.minor
, &child
->dtree
->cookie
,
1331 child
->udev_flags
)) {
1332 log_error("Failed to rename %s (%" PRIu32
1333 ":%" PRIu32
") to %s", name
, child
->info
.major
,
1334 child
->info
.minor
, child
->props
.new_name
);
1337 child
->name
= child
->props
.new_name
;
1338 child
->props
.new_name
= NULL
;
1341 if (!child
->info
.inactive_table
&& !child
->info
.suspended
)
1344 if (!_resume_node(child
->name
, child
->info
.major
, child
->info
.minor
,
1345 child
->props
.read_ahead
, child
->props
.read_ahead_flags
,
1346 &newinfo
, &child
->dtree
->cookie
, child
->udev_flags
)) {
1347 log_error("Unable to resume %s (%" PRIu32
1348 ":%" PRIu32
")", child
->name
, child
->info
.major
,
1354 /* Update cached info */
1355 child
->info
= newinfo
;
1364 static int _create_node(struct dm_tree_node
*dnode
)
1367 struct dm_task
*dmt
;
1369 log_verbose("Creating %s", dnode
->name
);
1371 if (!(dmt
= dm_task_create(DM_DEVICE_CREATE
))) {
1372 log_error("Create dm_task creation failed for %s", dnode
->name
);
1376 if (!dm_task_set_name(dmt
, dnode
->name
)) {
1377 log_error("Failed to set device name for %s", dnode
->name
);
1381 if (!dm_task_set_uuid(dmt
, dnode
->uuid
)) {
1382 log_error("Failed to set uuid for %s", dnode
->name
);
1386 if (dnode
->props
.major
&&
1387 (!dm_task_set_major(dmt
, dnode
->props
.major
) ||
1388 !dm_task_set_minor(dmt
, dnode
->props
.minor
))) {
1389 log_error("Failed to set device number for %s creation.", dnode
->name
);
1393 if (dnode
->props
.read_only
&& !dm_task_set_ro(dmt
)) {
1394 log_error("Failed to set read only flag for %s", dnode
->name
);
1398 if (!dm_task_no_open_count(dmt
))
1399 log_error("Failed to disable open_count");
1401 if ((r
= dm_task_run(dmt
)))
1402 r
= dm_task_get_info(dmt
, &dnode
->info
);
1405 dm_task_destroy(dmt
);
1411 static int _build_dev_string(char *devbuf
, size_t bufsize
, struct dm_tree_node
*node
)
1413 if (!dm_format_dev(devbuf
, bufsize
, node
->info
.major
, node
->info
.minor
)) {
1414 log_error("Failed to format %s device number for %s as dm "
1416 node
->name
, node
->uuid
, node
->info
.major
, node
->info
.minor
);
1423 /* simplify string emiting code */
1424 #define EMIT_PARAMS(p, str...)\
1427 if ((w = dm_snprintf(params + p, paramsize - (size_t) p, str)) < 0) {\
1428 stack; /* Out of space */\
1437 * Returns: 1 on success, 0 on failure
1439 static int _emit_areas_line(struct dm_task
*dmt
__attribute__((unused
)),
1440 struct load_segment
*seg
, char *params
,
1441 size_t paramsize
, int *pos
)
1443 struct seg_area
*area
;
1444 char devbuf
[DM_FORMAT_DEV_BUFSIZE
];
1445 unsigned first_time
= 1;
1446 const char *logtype
, *synctype
;
1447 unsigned log_parm_count
;
1449 dm_list_iterate_items(area
, &seg
->areas
) {
1450 if (!_build_dev_string(devbuf
, sizeof(devbuf
), area
->dev_node
))
1453 switch (seg
->type
) {
1454 case SEG_REPLICATOR_DEV
:
1455 EMIT_PARAMS(*pos
, " %d 1 %s", area
->rsite_index
, devbuf
);
1457 EMIT_PARAMS(*pos
, " nolog 0");
1459 /* Remote devices */
1460 log_parm_count
= (area
->flags
&
1461 (DM_NOSYNC
| DM_FORCESYNC
)) ? 2 : 1;
1464 devbuf
[0] = 0; /* Only core log parameters */
1467 devbuf
[0] = ' '; /* Extra space before device name */
1468 if (!_build_dev_string(devbuf
+ 1,
1473 log_parm_count
++; /* Extra sync log device name parameter */
1476 EMIT_PARAMS(*pos
, " %s %u%s %" PRIu64
, logtype
,
1477 log_parm_count
, devbuf
, area
->region_size
);
1479 synctype
= (area
->flags
& DM_NOSYNC
) ?
1480 " nosync" : (area
->flags
& DM_FORCESYNC
) ?
1484 EMIT_PARAMS(*pos
, "%s", synctype
);
1488 EMIT_PARAMS(*pos
, "%s%s %" PRIu64
, first_time
? "" : " ",
1489 devbuf
, area
->offset
);
1498 static int _replicator_emit_segment_line(const struct load_segment
*seg
, char *params
,
1499 size_t paramsize
, int *pos
)
1501 const struct load_segment
*rlog_seg
;
1502 struct replicator_site
*rsite
;
1503 char rlogbuf
[DM_FORMAT_DEV_BUFSIZE
];
1504 unsigned parm_count
;
1506 if (!seg
->log
|| !_build_dev_string(rlogbuf
, sizeof(rlogbuf
), seg
->log
))
1509 rlog_seg
= dm_list_item(dm_list_last(&seg
->log
->props
.segs
),
1510 struct load_segment
);
1512 EMIT_PARAMS(*pos
, "%s 4 %s 0 auto %" PRIu64
,
1513 seg
->rlog_type
, rlogbuf
, rlog_seg
->size
);
1515 dm_list_iterate_items(rsite
, &seg
->rsites
) {
1516 parm_count
= (rsite
->fall_behind_data
1517 || rsite
->fall_behind_ios
1518 || rsite
->async_timeout
) ? 4 : 2;
1520 EMIT_PARAMS(*pos
, " blockdev %u %u %s", parm_count
, rsite
->rsite_index
,
1521 (rsite
->mode
== DM_REPLICATOR_SYNC
) ? "synchronous" : "asynchronous");
1523 if (rsite
->fall_behind_data
)
1524 EMIT_PARAMS(*pos
, " data %" PRIu64
, rsite
->fall_behind_data
);
1525 else if (rsite
->fall_behind_ios
)
1526 EMIT_PARAMS(*pos
, " ios %" PRIu32
, rsite
->fall_behind_ios
);
1527 else if (rsite
->async_timeout
)
1528 EMIT_PARAMS(*pos
, " timeout %" PRIu32
, rsite
->async_timeout
);
1535 * Returns: 1 on success, 0 on failure
1537 static int _mirror_emit_segment_line(struct dm_task
*dmt
, uint32_t major
,
1538 uint32_t minor
, struct load_segment
*seg
,
1539 uint64_t *seg_start
, char *params
,
1542 int block_on_error
= 0;
1543 int handle_errors
= 0;
1544 int dm_log_userspace
= 0;
1546 unsigned log_parm_count
;
1548 char logbuf
[DM_FORMAT_DEV_BUFSIZE
];
1549 const char *logtype
;
1550 unsigned kmaj
, kmin
, krel
;
1552 if (uname(&uts
) == -1 || sscanf(uts
.release
, "%u.%u.%u", &kmaj
, &kmin
, &krel
) != 3) {
1553 log_error("Cannot read kernel release version");
1557 if ((seg
->flags
& DM_BLOCK_ON_ERROR
)) {
1559 * Originally, block_on_error was an argument to the log
1560 * portion of the mirror CTR table. It was renamed to
1561 * "handle_errors" and now resides in the 'features'
1562 * section of the mirror CTR table (i.e. at the end).
1564 * We can identify whether to use "block_on_error" or
1565 * "handle_errors" by the dm-mirror module's version
1566 * number (>= 1.12) or by the kernel version (>= 2.6.22).
1568 if (KERNEL_VERSION(kmaj
, kmin
, krel
) >= KERNEL_VERSION(2, 6, 22))
1574 if (seg
->clustered
) {
1575 /* Cluster mirrors require a UUID */
1580 * Cluster mirrors used to have their own log
1581 * types. Now they are accessed through the
1582 * userspace log type.
1584 * The dm-log-userspace module was added to the
1587 if (KERNEL_VERSION(kmaj
, kmin
, krel
) >= KERNEL_VERSION(2, 6, 31))
1588 dm_log_userspace
= 1;
1594 /* [no]sync, block_on_error etc. */
1595 log_parm_count
+= hweight32(seg
->flags
);
1597 /* "handle_errors" is a feature arg now */
1601 /* DM_CORELOG does not count in the param list */
1602 if (seg
->flags
& DM_CORELOG
)
1605 if (seg
->clustered
) {
1606 log_parm_count
++; /* For UUID */
1608 if (!dm_log_userspace
)
1609 EMIT_PARAMS(pos
, "clustered-");
1611 /* For clustered-* type field inserted later */
1620 if (!_build_dev_string(logbuf
, sizeof(logbuf
), seg
->log
))
1624 if (dm_log_userspace
)
1625 EMIT_PARAMS(pos
, "userspace %u %s clustered-%s",
1626 log_parm_count
, seg
->uuid
, logtype
);
1628 EMIT_PARAMS(pos
, "%s %u", logtype
, log_parm_count
);
1631 EMIT_PARAMS(pos
, " %s", logbuf
);
1633 EMIT_PARAMS(pos
, " %u", seg
->region_size
);
1635 if (seg
->clustered
&& !dm_log_userspace
)
1636 EMIT_PARAMS(pos
, " %s", seg
->uuid
);
1638 if ((seg
->flags
& DM_NOSYNC
))
1639 EMIT_PARAMS(pos
, " nosync");
1640 else if ((seg
->flags
& DM_FORCESYNC
))
1641 EMIT_PARAMS(pos
, " sync");
1644 EMIT_PARAMS(pos
, " block_on_error");
1646 EMIT_PARAMS(pos
, " %u ", seg
->mirror_area_count
);
1648 if (_emit_areas_line(dmt
, seg
, params
, paramsize
, &pos
) <= 0)
1652 EMIT_PARAMS(pos
, " 1 handle_errors");
1657 static int _emit_segment_line(struct dm_task
*dmt
, uint32_t major
,
1658 uint32_t minor
, struct load_segment
*seg
,
1659 uint64_t *seg_start
, char *params
,
1664 char originbuf
[DM_FORMAT_DEV_BUFSIZE
], cowbuf
[DM_FORMAT_DEV_BUFSIZE
];
1672 /* Mirrors are pretty complicated - now in separate function */
1673 r
= _mirror_emit_segment_line(dmt
, major
, minor
, seg
, seg_start
,
1678 case SEG_REPLICATOR
:
1679 if ((r
= _replicator_emit_segment_line(seg
, params
, paramsize
,
1685 case SEG_REPLICATOR_DEV
:
1686 if (!seg
->replicator
|| !_build_dev_string(originbuf
,
1691 EMIT_PARAMS(pos
, "%s %" PRIu64
, originbuf
, seg
->rdevice_index
);
1694 case SEG_SNAPSHOT_MERGE
:
1695 if (!_build_dev_string(originbuf
, sizeof(originbuf
), seg
->origin
))
1697 if (!_build_dev_string(cowbuf
, sizeof(cowbuf
), seg
->cow
))
1699 EMIT_PARAMS(pos
, "%s %s %c %d", originbuf
, cowbuf
,
1700 seg
->persistent
? 'P' : 'N', seg
->chunk_size
);
1702 case SEG_SNAPSHOT_ORIGIN
:
1703 if (!_build_dev_string(originbuf
, sizeof(originbuf
), seg
->origin
))
1705 EMIT_PARAMS(pos
, "%s", originbuf
);
1708 EMIT_PARAMS(pos
, "%u %u ", seg
->area_count
, seg
->stripe_size
);
1711 EMIT_PARAMS(pos
, "%s%s%s%s%s %s %" PRIu64
" ", seg
->cipher
,
1712 seg
->chainmode
? "-" : "", seg
->chainmode
?: "",
1713 seg
->iv
? "-" : "", seg
->iv
?: "", seg
->key
,
1714 seg
->iv_offset
!= DM_CRYPT_IV_DEFAULT
?
1715 seg
->iv_offset
: *seg_start
);
1721 case SEG_REPLICATOR
:
1723 case SEG_SNAPSHOT_ORIGIN
:
1724 case SEG_SNAPSHOT_MERGE
:
1729 case SEG_REPLICATOR_DEV
:
1731 if ((r
= _emit_areas_line(dmt
, seg
, params
, paramsize
, &pos
)) <= 0) {
1738 log_debug("Adding target to (%" PRIu32
":%" PRIu32
"): %" PRIu64
1739 " %" PRIu64
" %s %s", major
, minor
,
1740 *seg_start
, seg
->size
, dm_segtypes
[seg
->type
].target
, params
);
1742 if (!dm_task_add_target(dmt
, *seg_start
, seg
->size
, dm_segtypes
[seg
->type
].target
, params
))
1745 *seg_start
+= seg
->size
;
1752 static int _emit_segment(struct dm_task
*dmt
, uint32_t major
, uint32_t minor
,
1753 struct load_segment
*seg
, uint64_t *seg_start
)
1756 size_t paramsize
= 4096;
1760 if (!(params
= dm_malloc(paramsize
))) {
1761 log_error("Insufficient space for target parameters.");
1766 ret
= _emit_segment_line(dmt
, major
, minor
, seg
, seg_start
,
1776 log_debug("Insufficient space in params[%" PRIsize_t
1777 "] for target parameters.", paramsize
);
1780 } while (paramsize
< MAX_TARGET_PARAMSIZE
);
1782 log_error("Target parameter size too big. Aborting.");
1786 static int _load_node(struct dm_tree_node
*dnode
)
1789 struct dm_task
*dmt
;
1790 struct load_segment
*seg
;
1791 uint64_t seg_start
= 0;
1793 log_verbose("Loading %s table (%" PRIu32
":%" PRIu32
")", dnode
->name
,
1794 dnode
->info
.major
, dnode
->info
.minor
);
1796 if (!(dmt
= dm_task_create(DM_DEVICE_RELOAD
))) {
1797 log_error("Reload dm_task creation failed for %s", dnode
->name
);
1801 if (!dm_task_set_major(dmt
, dnode
->info
.major
) ||
1802 !dm_task_set_minor(dmt
, dnode
->info
.minor
)) {
1803 log_error("Failed to set device number for %s reload.", dnode
->name
);
1807 if (dnode
->props
.read_only
&& !dm_task_set_ro(dmt
)) {
1808 log_error("Failed to set read only flag for %s", dnode
->name
);
1812 if (!dm_task_no_open_count(dmt
))
1813 log_error("Failed to disable open_count");
1815 dm_list_iterate_items(seg
, &dnode
->props
.segs
)
1816 if (!_emit_segment(dmt
, dnode
->info
.major
, dnode
->info
.minor
,
1820 if (!dm_task_suppress_identical_reload(dmt
))
1821 log_error("Failed to suppress reload of identical tables.");
1823 if ((r
= dm_task_run(dmt
))) {
1824 r
= dm_task_get_info(dmt
, &dnode
->info
);
1825 if (r
&& !dnode
->info
.inactive_table
)
1826 log_verbose("Suppressed %s identical table reload.",
1829 if ((dnode
->props
.size_changed
=
1830 (dm_task_get_existing_table_size(dmt
) == seg_start
) ? 0 : 1))
1831 log_debug("Table size changed from %" PRIu64
" to %"
1833 dm_task_get_existing_table_size(dmt
),
1834 seg_start
, dnode
->name
);
1837 dnode
->props
.segment_count
= 0;
1840 dm_task_destroy(dmt
);
1845 int dm_tree_preload_children(struct dm_tree_node
*dnode
,
1846 const char *uuid_prefix
,
1847 size_t uuid_prefix_len
)
1850 void *handle
= NULL
;
1851 struct dm_tree_node
*child
;
1852 struct dm_info newinfo
;
1853 int update_devs_flag
= 0;
1855 /* Preload children first */
1856 while ((child
= dm_tree_next_child(&handle
, dnode
, 0))) {
1857 /* Skip existing non-device-mapper devices */
1858 if (!child
->info
.exists
&& child
->info
.major
)
1861 /* Ignore if it doesn't belong to this VG */
1862 if (child
->info
.exists
&&
1863 !_uuid_prefix_matches(child
->uuid
, uuid_prefix
, uuid_prefix_len
))
1866 if (dm_tree_node_num_children(child
, 0))
1867 if (!dm_tree_preload_children(child
, uuid_prefix
, uuid_prefix_len
))
1870 /* FIXME Cope if name exists with no uuid? */
1871 if (!child
->info
.exists
) {
1872 if (!_create_node(child
)) {
1878 if (!child
->info
.inactive_table
&& child
->props
.segment_count
) {
1879 if (!_load_node(child
)) {
1885 /* Propagate device size change change */
1886 if (child
->props
.size_changed
)
1887 dnode
->props
.size_changed
= 1;
1889 /* Resume device immediately if it has parents and its size changed */
1890 if (!dm_tree_node_num_children(child
, 1) || !child
->props
.size_changed
)
1893 if (!child
->info
.inactive_table
&& !child
->info
.suspended
)
1896 if (!_resume_node(child
->name
, child
->info
.major
, child
->info
.minor
,
1897 child
->props
.read_ahead
, child
->props
.read_ahead_flags
,
1898 &newinfo
, &child
->dtree
->cookie
, child
->udev_flags
)) {
1899 log_error("Unable to resume %s (%" PRIu32
1900 ":%" PRIu32
")", child
->name
, child
->info
.major
,
1906 /* Update cached info */
1907 child
->info
= newinfo
;
1910 * Prepare for immediate synchronization with udev and flush all stacked
1911 * dev node operations if requested by immediate_dev_node property. But
1912 * finish processing current level in the tree first.
1914 if (child
->props
.immediate_dev_node
)
1915 update_devs_flag
= 1;
1921 if (update_devs_flag
) {
1922 if (!dm_udev_wait(dm_tree_get_cookie(dnode
)))
1924 dm_tree_set_cookie(dnode
, 0);
1925 dm_task_update_nodes();
1932 * Returns 1 if unsure.
1934 int dm_tree_children_use_uuid(struct dm_tree_node
*dnode
,
1935 const char *uuid_prefix
,
1936 size_t uuid_prefix_len
)
1938 void *handle
= NULL
;
1939 struct dm_tree_node
*child
= dnode
;
1942 while ((child
= dm_tree_next_child(&handle
, dnode
, 0))) {
1943 if (!(uuid
= dm_tree_node_get_uuid(child
))) {
1944 log_error("Failed to get uuid for dtree node.");
1948 if (_uuid_prefix_matches(uuid
, uuid_prefix
, uuid_prefix_len
))
1951 if (dm_tree_node_num_children(child
, 0))
1952 dm_tree_children_use_uuid(child
, uuid_prefix
, uuid_prefix_len
);
1961 static struct load_segment
*_add_segment(struct dm_tree_node
*dnode
, unsigned type
, uint64_t size
)
1963 struct load_segment
*seg
;
1965 if (!(seg
= dm_pool_zalloc(dnode
->dtree
->mem
, sizeof(*seg
)))) {
1966 log_error("dtree node segment allocation failed");
1972 seg
->area_count
= 0;
1973 dm_list_init(&seg
->areas
);
1974 seg
->stripe_size
= 0;
1975 seg
->persistent
= 0;
1976 seg
->chunk_size
= 0;
1981 dm_list_add(&dnode
->props
.segs
, &seg
->list
);
1982 dnode
->props
.segment_count
++;
1987 int dm_tree_node_add_snapshot_origin_target(struct dm_tree_node
*dnode
,
1989 const char *origin_uuid
)
1991 struct load_segment
*seg
;
1992 struct dm_tree_node
*origin_node
;
1994 if (!(seg
= _add_segment(dnode
, SEG_SNAPSHOT_ORIGIN
, size
)))
1997 if (!(origin_node
= dm_tree_find_node_by_uuid(dnode
->dtree
, origin_uuid
))) {
1998 log_error("Couldn't find snapshot origin uuid %s.", origin_uuid
);
2002 seg
->origin
= origin_node
;
2003 if (!_link_tree_nodes(dnode
, origin_node
))
2006 /* Resume snapshot origins after new snapshots */
2007 dnode
->activation_priority
= 1;
2012 static int _add_snapshot_target(struct dm_tree_node
*node
,
2014 const char *origin_uuid
,
2015 const char *cow_uuid
,
2016 const char *merge_uuid
,
2018 uint32_t chunk_size
)
2020 struct load_segment
*seg
;
2021 struct dm_tree_node
*origin_node
, *cow_node
, *merge_node
;
2024 seg_type
= !merge_uuid
? SEG_SNAPSHOT
: SEG_SNAPSHOT_MERGE
;
2026 if (!(seg
= _add_segment(node
, seg_type
, size
)))
2029 if (!(origin_node
= dm_tree_find_node_by_uuid(node
->dtree
, origin_uuid
))) {
2030 log_error("Couldn't find snapshot origin uuid %s.", origin_uuid
);
2034 seg
->origin
= origin_node
;
2035 if (!_link_tree_nodes(node
, origin_node
))
2038 if (!(cow_node
= dm_tree_find_node_by_uuid(node
->dtree
, cow_uuid
))) {
2039 log_error("Couldn't find snapshot COW device uuid %s.", cow_uuid
);
2043 seg
->cow
= cow_node
;
2044 if (!_link_tree_nodes(node
, cow_node
))
2047 seg
->persistent
= persistent
? 1 : 0;
2048 seg
->chunk_size
= chunk_size
;
2051 if (!(merge_node
= dm_tree_find_node_by_uuid(node
->dtree
, merge_uuid
))) {
2052 /* not a pure error, merging snapshot may have been deactivated */
2053 log_verbose("Couldn't find merging snapshot uuid %s.", merge_uuid
);
2055 seg
->merge
= merge_node
;
2056 /* must not link merging snapshot, would undermine activation_priority below */
2059 /* Resume snapshot-merge (acting origin) after other snapshots */
2060 node
->activation_priority
= 1;
2062 /* Resume merging snapshot after snapshot-merge */
2063 seg
->merge
->activation_priority
= 2;
2071 int dm_tree_node_add_snapshot_target(struct dm_tree_node
*node
,
2073 const char *origin_uuid
,
2074 const char *cow_uuid
,
2076 uint32_t chunk_size
)
2078 return _add_snapshot_target(node
, size
, origin_uuid
, cow_uuid
,
2079 NULL
, persistent
, chunk_size
);
2082 int dm_tree_node_add_snapshot_merge_target(struct dm_tree_node
*node
,
2084 const char *origin_uuid
,
2085 const char *cow_uuid
,
2086 const char *merge_uuid
,
2087 uint32_t chunk_size
)
2089 return _add_snapshot_target(node
, size
, origin_uuid
, cow_uuid
,
2090 merge_uuid
, 1, chunk_size
);
2093 int dm_tree_node_add_error_target(struct dm_tree_node
*node
,
2096 if (!_add_segment(node
, SEG_ERROR
, size
))
2102 int dm_tree_node_add_zero_target(struct dm_tree_node
*node
,
2105 if (!_add_segment(node
, SEG_ZERO
, size
))
2111 int dm_tree_node_add_linear_target(struct dm_tree_node
*node
,
2114 if (!_add_segment(node
, SEG_LINEAR
, size
))
2120 int dm_tree_node_add_striped_target(struct dm_tree_node
*node
,
2122 uint32_t stripe_size
)
2124 struct load_segment
*seg
;
2126 if (!(seg
= _add_segment(node
, SEG_STRIPED
, size
)))
2129 seg
->stripe_size
= stripe_size
;
2134 int dm_tree_node_add_crypt_target(struct dm_tree_node
*node
,
2137 const char *chainmode
,
2142 struct load_segment
*seg
;
2144 if (!(seg
= _add_segment(node
, SEG_CRYPT
, size
)))
2147 seg
->cipher
= cipher
;
2148 seg
->chainmode
= chainmode
;
2150 seg
->iv_offset
= iv_offset
;
2156 int dm_tree_node_add_mirror_target_log(struct dm_tree_node
*node
,
2157 uint32_t region_size
,
2159 const char *log_uuid
,
2160 unsigned area_count
,
2163 struct dm_tree_node
*log_node
= NULL
;
2164 struct load_segment
*seg
;
2166 if (!node
->props
.segment_count
) {
2167 log_error(INTERNAL_ERROR
"Attempt to add target area to missing segment.");
2171 seg
= dm_list_item(dm_list_last(&node
->props
.segs
), struct load_segment
);
2174 if (!(seg
->uuid
= dm_pool_strdup(node
->dtree
->mem
, log_uuid
))) {
2175 log_error("log uuid pool_strdup failed");
2178 if (!(flags
& DM_CORELOG
)) {
2179 if (!(log_node
= dm_tree_find_node_by_uuid(node
->dtree
, log_uuid
))) {
2180 log_error("Couldn't find mirror log uuid %s.", log_uuid
);
2185 log_node
->props
.immediate_dev_node
= 1;
2187 if (!_link_tree_nodes(node
, log_node
))
2192 seg
->log
= log_node
;
2193 seg
->region_size
= region_size
;
2194 seg
->clustered
= clustered
;
2195 seg
->mirror_area_count
= area_count
;
2201 int dm_tree_node_add_mirror_target(struct dm_tree_node
*node
,
2204 if (!_add_segment(node
, SEG_MIRRORED
, size
))
2210 int dm_tree_node_add_replicator_target(struct dm_tree_node
*node
,
2212 const char *rlog_uuid
,
2213 const char *rlog_type
,
2214 unsigned rsite_index
,
2215 dm_replicator_mode_t mode
,
2216 uint32_t async_timeout
,
2217 uint64_t fall_behind_data
,
2218 uint32_t fall_behind_ios
)
2220 struct load_segment
*rseg
;
2221 struct replicator_site
*rsite
;
2223 /* Local site0 - adds replicator segment and links rlog device */
2224 if (rsite_index
== REPLICATOR_LOCAL_SITE
) {
2225 if (node
->props
.segment_count
) {
2226 log_error(INTERNAL_ERROR
"Attempt to add replicator segment to already used node.");
2230 if (!(rseg
= _add_segment(node
, SEG_REPLICATOR
, size
)))
2233 if (!(rseg
->log
= dm_tree_find_node_by_uuid(node
->dtree
, rlog_uuid
))) {
2234 log_error("Missing replicator log uuid %s.", rlog_uuid
);
2238 if (!_link_tree_nodes(node
, rseg
->log
))
2241 if (strcmp(rlog_type
, "ringbuffer") != 0) {
2242 log_error("Unsupported replicator log type %s.", rlog_type
);
2246 if (!(rseg
->rlog_type
= dm_pool_strdup(node
->dtree
->mem
, rlog_type
)))
2249 dm_list_init(&rseg
->rsites
);
2250 rseg
->rdevice_count
= 0;
2251 node
->activation_priority
= 1;
2254 /* Add site to segment */
2255 if (mode
== DM_REPLICATOR_SYNC
2256 && (async_timeout
|| fall_behind_ios
|| fall_behind_data
)) {
2257 log_error("Async parameters passed for synchronnous replicator.");
2261 if (node
->props
.segment_count
!= 1) {
2262 log_error(INTERNAL_ERROR
"Attempt to add remote site area before setting replicator log.");
2266 rseg
= dm_list_item(dm_list_last(&node
->props
.segs
), struct load_segment
);
2267 if (rseg
->type
!= SEG_REPLICATOR
) {
2268 log_error(INTERNAL_ERROR
"Attempt to use non replicator segment %s.",
2269 dm_segtypes
[rseg
->type
].target
);
2273 if (!(rsite
= dm_pool_zalloc(node
->dtree
->mem
, sizeof(*rsite
)))) {
2274 log_error("Failed to allocate remote site segment.");
2278 dm_list_add(&rseg
->rsites
, &rsite
->list
);
2279 rseg
->rsite_count
++;
2282 rsite
->async_timeout
= async_timeout
;
2283 rsite
->fall_behind_data
= fall_behind_data
;
2284 rsite
->fall_behind_ios
= fall_behind_ios
;
2285 rsite
->rsite_index
= rsite_index
;
2290 /* Appends device node to Replicator */
2291 int dm_tree_node_add_replicator_dev_target(struct dm_tree_node
*node
,
2293 const char *replicator_uuid
,
2294 uint64_t rdevice_index
,
2295 const char *rdev_uuid
,
2296 unsigned rsite_index
,
2297 const char *slog_uuid
,
2298 uint32_t slog_flags
,
2299 uint32_t slog_region_size
)
2301 struct seg_area
*area
;
2302 struct load_segment
*rseg
;
2303 struct load_segment
*rep_seg
;
2305 if (rsite_index
== REPLICATOR_LOCAL_SITE
) {
2306 /* Site index for local target */
2307 if (!(rseg
= _add_segment(node
, SEG_REPLICATOR_DEV
, size
)))
2310 if (!(rseg
->replicator
= dm_tree_find_node_by_uuid(node
->dtree
, replicator_uuid
))) {
2311 log_error("Missing replicator uuid %s.", replicator_uuid
);
2315 /* Local slink0 for replicator must be always initialized first */
2316 if (rseg
->replicator
->props
.segment_count
!= 1) {
2317 log_error(INTERNAL_ERROR
"Attempt to use non replicator segment.");
2321 rep_seg
= dm_list_item(dm_list_last(&rseg
->replicator
->props
.segs
), struct load_segment
);
2322 if (rep_seg
->type
!= SEG_REPLICATOR
) {
2323 log_error(INTERNAL_ERROR
"Attempt to use non replicator segment %s.",
2324 dm_segtypes
[rep_seg
->type
].target
);
2327 rep_seg
->rdevice_count
++;
2329 if (!_link_tree_nodes(node
, rseg
->replicator
))
2332 rseg
->rdevice_index
= rdevice_index
;
2334 /* Local slink0 for replicator must be always initialized first */
2335 if (node
->props
.segment_count
!= 1) {
2336 log_error(INTERNAL_ERROR
"Attempt to use non replicator-dev segment.");
2340 rseg
= dm_list_item(dm_list_last(&node
->props
.segs
), struct load_segment
);
2341 if (rseg
->type
!= SEG_REPLICATOR_DEV
) {
2342 log_error(INTERNAL_ERROR
"Attempt to use non replicator-dev segment %s.",
2343 dm_segtypes
[rseg
->type
].target
);
2348 if (!(slog_flags
& DM_CORELOG
) && !slog_uuid
) {
2349 log_error("Unspecified sync log uuid.");
2353 if (!dm_tree_node_add_target_area(node
, NULL
, rdev_uuid
, 0))
2356 area
= dm_list_item(dm_list_last(&rseg
->areas
), struct seg_area
);
2358 if (!(slog_flags
& DM_CORELOG
)) {
2359 if (!(area
->slog
= dm_tree_find_node_by_uuid(node
->dtree
, slog_uuid
))) {
2360 log_error("Couldn't find sync log uuid %s.", slog_uuid
);
2364 if (!_link_tree_nodes(node
, area
->slog
))
2368 area
->flags
= slog_flags
;
2369 area
->region_size
= slog_region_size
;
2370 area
->rsite_index
= rsite_index
;
2375 static int _add_area(struct dm_tree_node
*node
, struct load_segment
*seg
, struct dm_tree_node
*dev_node
, uint64_t offset
)
2377 struct seg_area
*area
;
2379 if (!(area
= dm_pool_zalloc(node
->dtree
->mem
, sizeof (*area
)))) {
2380 log_error("Failed to allocate target segment area.");
2384 area
->dev_node
= dev_node
;
2385 area
->offset
= offset
;
2387 dm_list_add(&seg
->areas
, &area
->list
);
2393 int dm_tree_node_add_target_area(struct dm_tree_node
*node
,
2394 const char *dev_name
,
2398 struct load_segment
*seg
;
2400 struct dm_tree_node
*dev_node
;
2402 if ((!dev_name
|| !*dev_name
) && (!uuid
|| !*uuid
)) {
2403 log_error("dm_tree_node_add_target_area called without device");
2408 if (!(dev_node
= dm_tree_find_node_by_uuid(node
->dtree
, uuid
))) {
2409 log_error("Couldn't find area uuid %s.", uuid
);
2412 if (!_link_tree_nodes(node
, dev_node
))
2415 if (stat(dev_name
, &info
) < 0) {
2416 log_error("Device %s not found.", dev_name
);
2420 if (!S_ISBLK(info
.st_mode
)) {
2421 log_error("Device %s is not a block device.", dev_name
);
2425 /* FIXME Check correct macro use */
2426 if (!(dev_node
= _add_dev(node
->dtree
, node
, MAJOR(info
.st_rdev
),
2427 MINOR(info
.st_rdev
), 0)))
2431 if (!node
->props
.segment_count
) {
2432 log_error(INTERNAL_ERROR
"Attempt to add target area to missing segment.");
2436 seg
= dm_list_item(dm_list_last(&node
->props
.segs
), struct load_segment
);
2438 if (!_add_area(node
, seg
, dev_node
, offset
))
2444 void dm_tree_set_cookie(struct dm_tree_node
*node
, uint32_t cookie
)
2446 node
->dtree
->cookie
= cookie
;
2449 uint32_t dm_tree_get_cookie(struct dm_tree_node
*node
)
2451 return node
->dtree
->cookie
;