2 * Copyright (C) 2005-2011 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 */
59 /* FIXME Add crypt and multipath support */
65 { SEG_CRYPT
, "crypt" },
66 { SEG_ERROR
, "error" },
67 { SEG_LINEAR
, "linear" },
68 { SEG_MIRRORED
, "mirror" },
69 { SEG_REPLICATOR
, "replicator" },
70 { SEG_REPLICATOR_DEV
, "replicator-dev" },
71 { SEG_SNAPSHOT
, "snapshot" },
72 { SEG_SNAPSHOT_ORIGIN
, "snapshot-origin" },
73 { SEG_SNAPSHOT_MERGE
, "snapshot-merge" },
74 { SEG_STRIPED
, "striped" },
76 { SEG_THIN_POOL
, "thin-pool"},
78 { SEG_RAID1
, "raid1"},
79 { SEG_RAID4
, "raid4"},
80 { SEG_RAID5_LA
, "raid5_la"},
81 { SEG_RAID5_RA
, "raid5_ra"},
82 { SEG_RAID5_LS
, "raid5_ls"},
83 { SEG_RAID5_RS
, "raid5_rs"},
84 { SEG_RAID6_ZR
, "raid6_zr"},
85 { SEG_RAID6_NR
, "raid6_nr"},
86 { SEG_RAID6_NC
, "raid6_nc"},
89 *WARNING: Since 'raid' target overloads this 1:1 mapping table
90 * for search do not add new enum elements past them!
92 { SEG_RAID5_LS
, "raid5"}, /* same as "raid5_ls" (default for MD also) */
93 { SEG_RAID6_ZR
, "raid6"}, /* same as "raid6_zr" */
97 /* Some segment types have a list of areas of other devices attached */
101 struct dm_tree_node
*dev_node
;
105 unsigned rsite_index
; /* Replicator site index */
106 struct dm_tree_node
*slog
; /* Replicator sync log node */
107 uint64_t region_size
; /* Replicator sync log size */
108 uint32_t flags
; /* Replicator sync log flags */
111 /* Replicator-log has a list of sites */
112 /* FIXME: maybe move to seg_area too? */
113 struct replicator_site
{
116 unsigned rsite_index
;
117 dm_replicator_mode_t mode
;
118 uint32_t async_timeout
;
119 uint32_t fall_behind_ios
;
120 uint64_t fall_behind_data
;
123 /* Per-segment properties */
124 struct load_segment
{
131 unsigned area_count
; /* Linear + Striped + Mirrored + Crypt + Replicator */
132 struct dm_list areas
; /* Linear + Striped + Mirrored + Crypt + Replicator */
134 uint32_t stripe_size
; /* Striped + raid */
136 int persistent
; /* Snapshot */
137 uint32_t chunk_size
; /* Snapshot */
138 struct dm_tree_node
*cow
; /* Snapshot */
139 struct dm_tree_node
*origin
; /* Snapshot + Snapshot origin */
140 struct dm_tree_node
*merge
; /* Snapshot */
142 struct dm_tree_node
*log
; /* Mirror + Replicator */
143 uint32_t region_size
; /* Mirror + raid */
144 unsigned clustered
; /* Mirror */
145 unsigned mirror_area_count
; /* Mirror */
146 uint32_t flags
; /* Mirror log */
147 char *uuid
; /* Clustered mirror log */
149 const char *cipher
; /* Crypt */
150 const char *chainmode
; /* Crypt */
151 const char *iv
; /* Crypt */
152 uint64_t iv_offset
; /* Crypt */
153 const char *key
; /* Crypt */
155 const char *rlog_type
; /* Replicator */
156 struct dm_list rsites
; /* Replicator */
157 unsigned rsite_count
; /* Replicator */
158 unsigned rdevice_count
; /* Replicator */
159 struct dm_tree_node
*replicator
;/* Replicator-dev */
160 uint64_t rdevice_index
; /* Replicator-dev */
162 uint64_t rebuilds
; /* raid */
164 struct dm_tree_node
*metadata
; /* Thin_pool */
165 struct dm_tree_node
*pool
; /* Thin_pool, Thin */
166 uint64_t low_water_mark_size
; /* Thin_pool */
167 uint32_t data_block_size
; /* Thin_pool */
168 unsigned skip_block_zeroing
; /* Thin_pool */
169 uint32_t device_id
; /* Thin */
173 /* Per-device properties */
174 struct load_properties
{
180 uint32_t read_ahead_flags
;
182 uint64_t thin_pool_transaction_id
; /* Thin_pool */
184 unsigned segment_count
;
185 unsigned size_changed
;
188 const char *new_name
;
190 /* If immediate_dev_node is set to 1, try to create the dev node
191 * as soon as possible (e.g. in preload stage even during traversal
192 * and processing of dm tree). This will also flush all stacked dev
193 * node operations, synchronizing with udev.
195 unsigned immediate_dev_node
;
198 * If the device size changed from zero and this is set,
199 * don't resume the device immediately, even if the device
200 * has parents. This works provided the parents do not
201 * validate the device size and is required by pvmove to
202 * avoid starting the mirror resync operation too early.
204 unsigned delay_resume_if_new
;
207 /* Two of these used to join two nodes with uses and used_by. */
208 struct dm_tree_link
{
210 struct dm_tree_node
*node
;
213 struct dm_tree_node
{
214 struct dm_tree
*dtree
;
220 struct dm_list uses
; /* Nodes this node uses */
221 struct dm_list used_by
; /* Nodes that use this node */
223 int activation_priority
; /* 0 gets activated first */
225 uint16_t udev_flags
; /* Udev control flags */
227 void *context
; /* External supplied context */
229 struct load_properties props
; /* For creation/table (re)load */
232 * If presuspend of child node is needed
233 * Note: only direct child is allowed
235 struct dm_tree_node
*presuspend_node
;
240 struct dm_hash_table
*devs
;
241 struct dm_hash_table
*uuids
;
242 struct dm_tree_node root
;
243 int skip_lockfs
; /* 1 skips lockfs (for non-snapshots) */
244 int no_flush
; /* 1 sets noflush (mirrors/multipath) */
245 int retry_remove
; /* 1 retries remove if not successful */
249 struct dm_tree
*dm_tree_create(void)
251 struct dm_pool
*dmem
;
252 struct dm_tree
*dtree
;
254 if (!(dmem
= dm_pool_create("dtree", 1024)) ||
255 !(dtree
= dm_pool_zalloc(dmem
, sizeof(*dtree
)))) {
256 log_error("Failed to allocate dtree.");
258 dm_pool_destroy(dmem
);
262 dtree
->root
.dtree
= dtree
;
263 dm_list_init(&dtree
->root
.uses
);
264 dm_list_init(&dtree
->root
.used_by
);
265 dtree
->skip_lockfs
= 0;
269 if (!(dtree
->devs
= dm_hash_create(8))) {
270 log_error("dtree hash creation failed");
271 dm_pool_destroy(dtree
->mem
);
275 if (!(dtree
->uuids
= dm_hash_create(32))) {
276 log_error("dtree uuid hash creation failed");
277 dm_hash_destroy(dtree
->devs
);
278 dm_pool_destroy(dtree
->mem
);
285 void dm_tree_free(struct dm_tree
*dtree
)
290 dm_hash_destroy(dtree
->uuids
);
291 dm_hash_destroy(dtree
->devs
);
292 dm_pool_destroy(dtree
->mem
);
295 static int _nodes_are_linked(const struct dm_tree_node
*parent
,
296 const struct dm_tree_node
*child
)
298 struct dm_tree_link
*dlink
;
300 dm_list_iterate_items(dlink
, &parent
->uses
)
301 if (dlink
->node
== child
)
307 static int _link(struct dm_list
*list
, struct dm_tree_node
*node
)
309 struct dm_tree_link
*dlink
;
311 if (!(dlink
= dm_pool_alloc(node
->dtree
->mem
, sizeof(*dlink
)))) {
312 log_error("dtree link allocation failed");
317 dm_list_add(list
, &dlink
->list
);
322 static int _link_nodes(struct dm_tree_node
*parent
,
323 struct dm_tree_node
*child
)
325 if (_nodes_are_linked(parent
, child
))
328 if (!_link(&parent
->uses
, child
))
331 if (!_link(&child
->used_by
, parent
))
337 static void _unlink(struct dm_list
*list
, struct dm_tree_node
*node
)
339 struct dm_tree_link
*dlink
;
341 dm_list_iterate_items(dlink
, list
)
342 if (dlink
->node
== node
) {
343 dm_list_del(&dlink
->list
);
348 static void _unlink_nodes(struct dm_tree_node
*parent
,
349 struct dm_tree_node
*child
)
351 if (!_nodes_are_linked(parent
, child
))
354 _unlink(&parent
->uses
, child
);
355 _unlink(&child
->used_by
, parent
);
358 static int _add_to_toplevel(struct dm_tree_node
*node
)
360 return _link_nodes(&node
->dtree
->root
, node
);
363 static void _remove_from_toplevel(struct dm_tree_node
*node
)
365 _unlink_nodes(&node
->dtree
->root
, node
);
368 static int _add_to_bottomlevel(struct dm_tree_node
*node
)
370 return _link_nodes(node
, &node
->dtree
->root
);
373 static void _remove_from_bottomlevel(struct dm_tree_node
*node
)
375 _unlink_nodes(node
, &node
->dtree
->root
);
378 static int _link_tree_nodes(struct dm_tree_node
*parent
, struct dm_tree_node
*child
)
380 /* Don't link to root node if child already has a parent */
381 if (parent
== &parent
->dtree
->root
) {
382 if (dm_tree_node_num_children(child
, 1))
385 _remove_from_toplevel(child
);
387 if (child
== &child
->dtree
->root
) {
388 if (dm_tree_node_num_children(parent
, 0))
391 _remove_from_bottomlevel(parent
);
393 return _link_nodes(parent
, child
);
396 static struct dm_tree_node
*_create_dm_tree_node(struct dm_tree
*dtree
,
399 struct dm_info
*info
,
403 struct dm_tree_node
*node
;
406 if (!(node
= dm_pool_zalloc(dtree
->mem
, sizeof(*node
)))) {
407 log_error("_create_dm_tree_node alloc failed");
416 node
->context
= context
;
417 node
->udev_flags
= udev_flags
;
418 node
->activation_priority
= 0;
420 dm_list_init(&node
->uses
);
421 dm_list_init(&node
->used_by
);
422 dm_list_init(&node
->props
.segs
);
424 dev
= MKDEV(info
->major
, info
->minor
);
426 if (!dm_hash_insert_binary(dtree
->devs
, (const char *) &dev
,
427 sizeof(dev
), node
)) {
428 log_error("dtree node hash insertion failed");
429 dm_pool_free(dtree
->mem
, node
);
434 !dm_hash_insert(dtree
->uuids
, uuid
, node
)) {
435 log_error("dtree uuid hash insertion failed");
436 dm_hash_remove_binary(dtree
->devs
, (const char *) &dev
,
438 dm_pool_free(dtree
->mem
, node
);
445 static struct dm_tree_node
*_find_dm_tree_node(struct dm_tree
*dtree
,
446 uint32_t major
, uint32_t minor
)
448 uint64_t dev
= MKDEV(major
, minor
);
450 return dm_hash_lookup_binary(dtree
->devs
, (const char *) &dev
,
454 static struct dm_tree_node
*_find_dm_tree_node_by_uuid(struct dm_tree
*dtree
,
457 struct dm_tree_node
*node
;
459 if ((node
= dm_hash_lookup(dtree
->uuids
, uuid
)))
462 if (strncmp(uuid
, UUID_PREFIX
, sizeof(UUID_PREFIX
) - 1))
465 return dm_hash_lookup(dtree
->uuids
, uuid
+ sizeof(UUID_PREFIX
) - 1);
468 static int _deps(struct dm_task
**dmt
, struct dm_pool
*mem
, uint32_t major
, uint32_t minor
,
469 const char **name
, const char **uuid
,
470 struct dm_info
*info
, struct dm_deps
**deps
)
472 memset(info
, 0, sizeof(*info
));
474 if (!dm_is_dm_major(major
)) {
481 info
->live_table
= 0;
482 info
->inactive_table
= 0;
487 if (!(*dmt
= dm_task_create(DM_DEVICE_DEPS
))) {
488 log_error("deps dm_task creation failed");
492 if (!dm_task_set_major(*dmt
, major
)) {
493 log_error("_deps: failed to set major for (%" PRIu32
":%" PRIu32
")",
498 if (!dm_task_set_minor(*dmt
, minor
)) {
499 log_error("_deps: failed to set minor for (%" PRIu32
":%" PRIu32
")",
504 if (!dm_task_run(*dmt
)) {
505 log_error("_deps: task run failed for (%" PRIu32
":%" PRIu32
")",
510 if (!dm_task_get_info(*dmt
, info
)) {
511 log_error("_deps: failed to get info for (%" PRIu32
":%" PRIu32
")",
521 if (info
->major
!= major
) {
522 log_error("Inconsistent dtree major number: %u != %u",
526 if (info
->minor
!= minor
) {
527 log_error("Inconsistent dtree minor number: %u != %u",
531 if (!(*name
= dm_pool_strdup(mem
, dm_task_get_name(*dmt
)))) {
532 log_error("name pool_strdup failed");
535 if (!(*uuid
= dm_pool_strdup(mem
, dm_task_get_uuid(*dmt
)))) {
536 log_error("uuid pool_strdup failed");
539 *deps
= dm_task_get_deps(*dmt
);
545 dm_task_destroy(*dmt
);
549 static struct dm_tree_node
*_add_dev(struct dm_tree
*dtree
,
550 struct dm_tree_node
*parent
,
551 uint32_t major
, uint32_t minor
,
554 struct dm_task
*dmt
= NULL
;
556 struct dm_deps
*deps
= NULL
;
557 const char *name
= NULL
;
558 const char *uuid
= NULL
;
559 struct dm_tree_node
*node
= NULL
;
563 /* Already in tree? */
564 if (!(node
= _find_dm_tree_node(dtree
, major
, minor
))) {
565 if (!_deps(&dmt
, dtree
->mem
, major
, minor
, &name
, &uuid
, &info
, &deps
))
568 if (!(node
= _create_dm_tree_node(dtree
, name
, uuid
, &info
,
574 if (!_link_tree_nodes(parent
, node
)) {
579 /* If node was already in tree, no need to recurse. */
583 /* Can't recurse if not a mapped device or there are no dependencies */
584 if (!node
->info
.exists
|| !deps
->count
) {
585 if (!_add_to_bottomlevel(node
)) {
592 /* Add dependencies to tree */
593 for (i
= 0; i
< deps
->count
; i
++)
594 if (!_add_dev(dtree
, node
, MAJOR(deps
->device
[i
]),
595 MINOR(deps
->device
[i
]), udev_flags
)) {
602 dm_task_destroy(dmt
);
607 static int _node_clear_table(struct dm_tree_node
*dnode
)
610 struct dm_info
*info
;
614 if (!(info
= &dnode
->info
)) {
615 log_error("_node_clear_table failed: missing info");
619 if (!(name
= dm_tree_node_get_name(dnode
))) {
620 log_error("_node_clear_table failed: missing name");
624 /* Is there a table? */
625 if (!info
->exists
|| !info
->inactive_table
)
628 // FIXME Get inactive deps. If any dev referenced has 1 opener and no live table, remove it after the clear.
630 log_verbose("Clearing inactive table %s (%" PRIu32
":%" PRIu32
")",
631 name
, info
->major
, info
->minor
);
633 if (!(dmt
= dm_task_create(DM_DEVICE_CLEAR
))) {
634 log_error("Table clear dm_task creation failed for %s", name
);
638 if (!dm_task_set_major(dmt
, info
->major
) ||
639 !dm_task_set_minor(dmt
, info
->minor
)) {
640 log_error("Failed to set device number for %s table clear", name
);
641 dm_task_destroy(dmt
);
645 r
= dm_task_run(dmt
);
647 if (!dm_task_get_info(dmt
, info
)) {
648 log_error("_node_clear_table failed: info missing after running task for %s", name
);
652 dm_task_destroy(dmt
);
657 struct dm_tree_node
*dm_tree_add_new_dev(struct dm_tree
*dtree
,
660 uint32_t major
, uint32_t minor
,
665 struct dm_tree_node
*dnode
;
670 /* Do we need to add node to tree? */
671 if (!(dnode
= dm_tree_find_node_by_uuid(dtree
, uuid
))) {
672 if (!(name2
= dm_pool_strdup(dtree
->mem
, name
))) {
673 log_error("name pool_strdup failed");
676 if (!(uuid2
= dm_pool_strdup(dtree
->mem
, uuid
))) {
677 log_error("uuid pool_strdup failed");
685 info
.inactive_table
= 0;
688 if (!(dnode
= _create_dm_tree_node(dtree
, name2
, uuid2
, &info
,
692 /* Attach to root node until a table is supplied */
693 if (!_add_to_toplevel(dnode
) || !_add_to_bottomlevel(dnode
))
696 dnode
->props
.major
= major
;
697 dnode
->props
.minor
= minor
;
698 dnode
->props
.new_name
= NULL
;
699 dnode
->props
.size_changed
= 0;
700 } else if (strcmp(name
, dnode
->name
)) {
701 /* Do we need to rename node? */
702 if (!(dnode
->props
.new_name
= dm_pool_strdup(dtree
->mem
, name
))) {
703 log_error("name pool_strdup failed");
708 dnode
->props
.read_only
= read_only
? 1 : 0;
709 dnode
->props
.read_ahead
= DM_READ_AHEAD_AUTO
;
710 dnode
->props
.read_ahead_flags
= 0;
712 if (clear_inactive
&& !_node_clear_table(dnode
))
715 dnode
->context
= context
;
716 dnode
->udev_flags
= 0;
721 struct dm_tree_node
*dm_tree_add_new_dev_with_udev_flags(struct dm_tree
*dtree
,
731 struct dm_tree_node
*node
;
733 if ((node
= dm_tree_add_new_dev(dtree
, name
, uuid
, major
, minor
, read_only
,
734 clear_inactive
, context
)))
735 node
->udev_flags
= udev_flags
;
740 void dm_tree_node_set_udev_flags(struct dm_tree_node
*dnode
, uint16_t udev_flags
)
743 struct dm_info
*dinfo
= &dnode
->info
;
745 if (udev_flags
!= dnode
->udev_flags
)
746 log_debug("Resetting %s (%" PRIu32
":%" PRIu32
747 ") udev_flags from 0x%x to 0x%x",
748 dnode
->name
, dinfo
->major
, dinfo
->minor
,
749 dnode
->udev_flags
, udev_flags
);
750 dnode
->udev_flags
= udev_flags
;
753 void dm_tree_node_set_read_ahead(struct dm_tree_node
*dnode
,
755 uint32_t read_ahead_flags
)
757 dnode
->props
.read_ahead
= read_ahead
;
758 dnode
->props
.read_ahead_flags
= read_ahead_flags
;
761 void dm_tree_node_set_presuspend_node(struct dm_tree_node
*node
,
762 struct dm_tree_node
*presuspend_node
)
764 node
->presuspend_node
= presuspend_node
;
767 int dm_tree_add_dev(struct dm_tree
*dtree
, uint32_t major
, uint32_t minor
)
769 return _add_dev(dtree
, &dtree
->root
, major
, minor
, 0) ? 1 : 0;
772 int dm_tree_add_dev_with_udev_flags(struct dm_tree
*dtree
, uint32_t major
,
773 uint32_t minor
, uint16_t udev_flags
)
775 return _add_dev(dtree
, &dtree
->root
, major
, minor
, udev_flags
) ? 1 : 0;
778 const char *dm_tree_node_get_name(const struct dm_tree_node
*node
)
780 return node
->info
.exists
? node
->name
: "";
783 const char *dm_tree_node_get_uuid(const struct dm_tree_node
*node
)
785 return node
->info
.exists
? node
->uuid
: "";
788 const struct dm_info
*dm_tree_node_get_info(const struct dm_tree_node
*node
)
793 void *dm_tree_node_get_context(const struct dm_tree_node
*node
)
795 return node
->context
;
798 int dm_tree_node_size_changed(const struct dm_tree_node
*dnode
)
800 return dnode
->props
.size_changed
;
803 int dm_tree_node_num_children(const struct dm_tree_node
*node
, uint32_t inverted
)
806 if (_nodes_are_linked(&node
->dtree
->root
, node
))
808 return dm_list_size(&node
->used_by
);
811 if (_nodes_are_linked(node
, &node
->dtree
->root
))
814 return dm_list_size(&node
->uses
);
818 * Returns 1 if no prefix supplied
820 static int _uuid_prefix_matches(const char *uuid
, const char *uuid_prefix
, size_t uuid_prefix_len
)
825 if (!strncmp(uuid
, uuid_prefix
, uuid_prefix_len
))
828 /* Handle transition: active device uuids might be missing the prefix */
829 if (uuid_prefix_len
<= 4)
832 if (!strncmp(uuid
, UUID_PREFIX
, sizeof(UUID_PREFIX
) - 1))
835 if (strncmp(uuid_prefix
, UUID_PREFIX
, sizeof(UUID_PREFIX
) - 1))
838 if (!strncmp(uuid
, uuid_prefix
+ sizeof(UUID_PREFIX
) - 1, uuid_prefix_len
- (sizeof(UUID_PREFIX
) - 1)))
845 * Returns 1 if no children.
847 static int _children_suspended(struct dm_tree_node
*node
,
849 const char *uuid_prefix
,
850 size_t uuid_prefix_len
)
852 struct dm_list
*list
;
853 struct dm_tree_link
*dlink
;
854 const struct dm_info
*dinfo
;
858 if (_nodes_are_linked(&node
->dtree
->root
, node
))
860 list
= &node
->used_by
;
862 if (_nodes_are_linked(node
, &node
->dtree
->root
))
867 dm_list_iterate_items(dlink
, list
) {
868 if (!(uuid
= dm_tree_node_get_uuid(dlink
->node
))) {
873 /* Ignore if it doesn't belong to this VG */
874 if (!_uuid_prefix_matches(uuid
, uuid_prefix
, uuid_prefix_len
))
877 /* Ignore if parent node wants to presuspend this node */
878 if (dlink
->node
->presuspend_node
== node
)
881 if (!(dinfo
= dm_tree_node_get_info(dlink
->node
))) {
882 stack
; /* FIXME Is this normal? */
886 if (!dinfo
->suspended
)
894 * Set major and minor to zero for root of tree.
896 struct dm_tree_node
*dm_tree_find_node(struct dm_tree
*dtree
,
900 if (!major
&& !minor
)
903 return _find_dm_tree_node(dtree
, major
, minor
);
907 * Set uuid to NULL for root of tree.
909 struct dm_tree_node
*dm_tree_find_node_by_uuid(struct dm_tree
*dtree
,
915 return _find_dm_tree_node_by_uuid(dtree
, uuid
);
919 * First time set *handle to NULL.
920 * Set inverted to invert the tree.
922 struct dm_tree_node
*dm_tree_next_child(void **handle
,
923 const struct dm_tree_node
*parent
,
926 struct dm_list
**dlink
= (struct dm_list
**) handle
;
927 const struct dm_list
*use_list
;
930 use_list
= &parent
->used_by
;
932 use_list
= &parent
->uses
;
935 *dlink
= dm_list_first(use_list
);
937 *dlink
= dm_list_next(use_list
, *dlink
);
939 return (*dlink
) ? dm_list_item(*dlink
, struct dm_tree_link
)->node
: NULL
;
943 * Deactivate a device with its dependencies if the uuid prefix matches.
945 static int _info_by_dev(uint32_t major
, uint32_t minor
, int with_open_count
,
946 struct dm_info
*info
)
951 if (!(dmt
= dm_task_create(DM_DEVICE_INFO
))) {
952 log_error("_info_by_dev: dm_task creation failed");
956 if (!dm_task_set_major(dmt
, major
) || !dm_task_set_minor(dmt
, minor
)) {
957 log_error("_info_by_dev: Failed to set device number");
958 dm_task_destroy(dmt
);
962 if (!with_open_count
&& !dm_task_no_open_count(dmt
))
963 log_error("Failed to disable open_count");
965 if ((r
= dm_task_run(dmt
)))
966 r
= dm_task_get_info(dmt
, info
);
968 dm_task_destroy(dmt
);
973 static int _check_device_not_in_use(struct dm_info
*info
)
978 /* If sysfs is not used, use open_count information only. */
979 if (!*dm_sysfs_dir()) {
980 if (info
->open_count
) {
981 log_error("Device %" PRIu32
":%" PRIu32
" in use",
982 info
->major
, info
->minor
);
989 if (dm_device_has_holders(info
->major
, info
->minor
)) {
990 log_error("Device %" PRIu32
":%" PRIu32
" is used "
991 "by another device.", info
->major
, info
->minor
);
995 if (dm_device_has_mounted_fs(info
->major
, info
->minor
)) {
996 log_error("Device %" PRIu32
":%" PRIu32
" contains "
997 "a filesystem in use.", info
->major
, info
->minor
);
1004 /* Check if all parent nodes of given node have open_count == 0 */
1005 static int _node_has_closed_parents(struct dm_tree_node
*node
,
1006 const char *uuid_prefix
,
1007 size_t uuid_prefix_len
)
1009 struct dm_tree_link
*dlink
;
1010 const struct dm_info
*dinfo
;
1011 struct dm_info info
;
1014 /* Iterate through parents of this node */
1015 dm_list_iterate_items(dlink
, &node
->used_by
) {
1016 if (!(uuid
= dm_tree_node_get_uuid(dlink
->node
))) {
1021 /* Ignore if it doesn't belong to this VG */
1022 if (!_uuid_prefix_matches(uuid
, uuid_prefix
, uuid_prefix_len
))
1025 if (!(dinfo
= dm_tree_node_get_info(dlink
->node
))) {
1026 stack
; /* FIXME Is this normal? */
1030 /* Refresh open_count */
1031 if (!_info_by_dev(dinfo
->major
, dinfo
->minor
, 1, &info
) ||
1035 if (info
.open_count
) {
1036 log_debug("Node %s %d:%d has open_count %d", uuid_prefix
,
1037 dinfo
->major
, dinfo
->minor
, info
.open_count
);
1045 static int _deactivate_node(const char *name
, uint32_t major
, uint32_t minor
,
1046 uint32_t *cookie
, uint16_t udev_flags
, int retry
)
1048 struct dm_task
*dmt
;
1051 log_verbose("Removing %s (%" PRIu32
":%" PRIu32
")", name
, major
, minor
);
1053 if (!(dmt
= dm_task_create(DM_DEVICE_REMOVE
))) {
1054 log_error("Deactivation dm_task creation failed for %s", name
);
1058 if (!dm_task_set_major(dmt
, major
) || !dm_task_set_minor(dmt
, minor
)) {
1059 log_error("Failed to set device number for %s deactivation", name
);
1063 if (!dm_task_no_open_count(dmt
))
1064 log_error("Failed to disable open_count");
1066 if (!dm_task_set_cookie(dmt
, cookie
, udev_flags
))
1071 dm_task_retry_remove(dmt
);
1073 r
= dm_task_run(dmt
);
1075 /* FIXME Until kernel returns actual name so dm-iface.c can handle it */
1076 rm_dev_node(name
, dmt
->cookie_set
&& !(udev_flags
& DM_UDEV_DISABLE_DM_RULES_FLAG
),
1077 dmt
->cookie_set
&& (udev_flags
& DM_UDEV_DISABLE_LIBRARY_FALLBACK
));
1079 /* FIXME Remove node from tree or mark invalid? */
1082 dm_task_destroy(dmt
);
1087 static int _rename_node(const char *old_name
, const char *new_name
, uint32_t major
,
1088 uint32_t minor
, uint32_t *cookie
, uint16_t udev_flags
)
1090 struct dm_task
*dmt
;
1093 log_verbose("Renaming %s (%" PRIu32
":%" PRIu32
") to %s", old_name
, major
, minor
, new_name
);
1095 if (!(dmt
= dm_task_create(DM_DEVICE_RENAME
))) {
1096 log_error("Rename dm_task creation failed for %s", old_name
);
1100 if (!dm_task_set_name(dmt
, old_name
)) {
1101 log_error("Failed to set name for %s rename.", old_name
);
1105 if (!dm_task_set_newname(dmt
, new_name
))
1108 if (!dm_task_no_open_count(dmt
))
1109 log_error("Failed to disable open_count");
1111 if (!dm_task_set_cookie(dmt
, cookie
, udev_flags
))
1114 r
= dm_task_run(dmt
);
1117 dm_task_destroy(dmt
);
1122 /* FIXME Merge with _suspend_node? */
1123 static int _resume_node(const char *name
, uint32_t major
, uint32_t minor
,
1124 uint32_t read_ahead
, uint32_t read_ahead_flags
,
1125 struct dm_info
*newinfo
, uint32_t *cookie
,
1126 uint16_t udev_flags
, int already_suspended
)
1128 struct dm_task
*dmt
;
1131 log_verbose("Resuming %s (%" PRIu32
":%" PRIu32
")", name
, major
, minor
);
1133 if (!(dmt
= dm_task_create(DM_DEVICE_RESUME
))) {
1134 log_debug("Suspend dm_task creation failed for %s.", name
);
1138 /* FIXME Kernel should fill in name on return instead */
1139 if (!dm_task_set_name(dmt
, name
)) {
1140 log_debug("Failed to set device name for %s resumption.", name
);
1144 if (!dm_task_set_major(dmt
, major
) || !dm_task_set_minor(dmt
, minor
)) {
1145 log_error("Failed to set device number for %s resumption.", name
);
1149 if (!dm_task_no_open_count(dmt
))
1150 log_error("Failed to disable open_count");
1152 if (!dm_task_set_read_ahead(dmt
, read_ahead
, read_ahead_flags
))
1153 log_error("Failed to set read ahead");
1155 if (!dm_task_set_cookie(dmt
, cookie
, udev_flags
))
1158 if (!(r
= dm_task_run(dmt
)))
1161 if (already_suspended
)
1164 if (!(r
= dm_task_get_info(dmt
, newinfo
)))
1168 dm_task_destroy(dmt
);
1173 static int _suspend_node(const char *name
, uint32_t major
, uint32_t minor
,
1174 int skip_lockfs
, int no_flush
, struct dm_info
*newinfo
)
1176 struct dm_task
*dmt
;
1179 log_verbose("Suspending %s (%" PRIu32
":%" PRIu32
")%s%s",
1181 skip_lockfs
? "" : " with filesystem sync",
1182 no_flush
? "" : " with device flush");
1184 if (!(dmt
= dm_task_create(DM_DEVICE_SUSPEND
))) {
1185 log_error("Suspend dm_task creation failed for %s", name
);
1189 if (!dm_task_set_major(dmt
, major
) || !dm_task_set_minor(dmt
, minor
)) {
1190 log_error("Failed to set device number for %s suspension.", name
);
1191 dm_task_destroy(dmt
);
1195 if (!dm_task_no_open_count(dmt
))
1196 log_error("Failed to disable open_count");
1198 if (skip_lockfs
&& !dm_task_skip_lockfs(dmt
))
1199 log_error("Failed to set skip_lockfs flag.");
1201 if (no_flush
&& !dm_task_no_flush(dmt
))
1202 log_error("Failed to set no_flush flag.");
1204 if ((r
= dm_task_run(dmt
))) {
1206 r
= dm_task_get_info(dmt
, newinfo
);
1209 dm_task_destroy(dmt
);
1214 static int _check_thin_pool_transaction_id(const char *name
, uint32_t major
, uint32_t minor
,
1215 uint64_t transaction_id
)
1217 struct dm_task
*dmt
;
1219 uint64_t start
, length
;
1221 char *params
= NULL
;
1222 uint64_t t_id
= transaction_id
; // FIXME: fake
1224 log_verbose("Checking transaction id %s (%" PRIu32
":%" PRIu32
")", name
, major
, minor
);
1226 if (!(dmt
= dm_task_create(DM_DEVICE_STATUS
))) {
1227 log_debug("Device status dm_task creation failed for %s.", name
);
1231 if (!dm_task_set_name(dmt
, name
)) {
1232 log_debug("Failed to set device name for %s status.", name
);
1236 if (!dm_task_set_major_minor(dmt
, major
, minor
, 1)) {
1237 log_error("Failed to set device number for %s status.", name
);
1241 if (!dm_task_no_open_count(dmt
))
1242 log_error("Failed to disable open_count");
1244 if (!(r
= dm_task_run(dmt
)))
1247 dm_get_next_target(dmt
, NULL
, &start
, &length
, &type
, ¶ms
);
1248 log_verbose("PARSE params %s", params
); // FIXME: parse status
1250 r
= (transaction_id
== t_id
);
1253 dm_task_destroy(dmt
);
1259 * FIXME Don't attempt to deactivate known internal dependencies.
1261 static int _dm_tree_deactivate_children(struct dm_tree_node
*dnode
,
1262 const char *uuid_prefix
,
1263 size_t uuid_prefix_len
,
1267 void *handle
= NULL
;
1268 struct dm_tree_node
*child
= dnode
;
1269 struct dm_info info
;
1270 const struct dm_info
*dinfo
;
1274 while ((child
= dm_tree_next_child(&handle
, dnode
, 0))) {
1275 if (!(dinfo
= dm_tree_node_get_info(child
))) {
1280 if (!(name
= dm_tree_node_get_name(child
))) {
1285 if (!(uuid
= dm_tree_node_get_uuid(child
))) {
1290 /* Ignore if it doesn't belong to this VG */
1291 if (!_uuid_prefix_matches(uuid
, uuid_prefix
, uuid_prefix_len
))
1294 /* Refresh open_count */
1295 if (!_info_by_dev(dinfo
->major
, dinfo
->minor
, 1, &info
) ||
1299 if (!_check_device_not_in_use(&info
))
1302 /* Also checking open_count in parent nodes of presuspend_node */
1303 if ((child
->presuspend_node
&&
1304 !_node_has_closed_parents(child
->presuspend_node
,
1305 uuid_prefix
, uuid_prefix_len
))) {
1306 /* Only report error from (likely non-internal) dependency at top level */
1308 log_error("Unable to deactivate open %s (%" PRIu32
1309 ":%" PRIu32
")", name
, info
.major
,
1316 /* Suspend child node first if requested */
1317 if (child
->presuspend_node
&&
1318 !dm_tree_suspend_children(child
, uuid_prefix
, uuid_prefix_len
))
1321 if (!_deactivate_node(name
, info
.major
, info
.minor
,
1322 &child
->dtree
->cookie
, child
->udev_flags
,
1323 child
->dtree
->retry_remove
)) {
1324 log_error("Unable to deactivate %s (%" PRIu32
1325 ":%" PRIu32
")", name
, info
.major
,
1329 } else if (info
.suspended
)
1332 if (dm_tree_node_num_children(child
, 0)) {
1333 if (!_dm_tree_deactivate_children(child
, uuid_prefix
, uuid_prefix_len
, level
+ 1))
1341 int dm_tree_deactivate_children(struct dm_tree_node
*dnode
,
1342 const char *uuid_prefix
,
1343 size_t uuid_prefix_len
)
1345 return _dm_tree_deactivate_children(dnode
, uuid_prefix
, uuid_prefix_len
, 0);
1348 void dm_tree_skip_lockfs(struct dm_tree_node
*dnode
)
1350 dnode
->dtree
->skip_lockfs
= 1;
1353 void dm_tree_use_no_flush_suspend(struct dm_tree_node
*dnode
)
1355 dnode
->dtree
->no_flush
= 1;
1358 void dm_tree_retry_remove(struct dm_tree_node
*dnode
)
1360 dnode
->dtree
->retry_remove
= 1;
1363 int dm_tree_suspend_children(struct dm_tree_node
*dnode
,
1364 const char *uuid_prefix
,
1365 size_t uuid_prefix_len
)
1368 void *handle
= NULL
;
1369 struct dm_tree_node
*child
= dnode
;
1370 struct dm_info info
, newinfo
;
1371 const struct dm_info
*dinfo
;
1375 /* Suspend nodes at this level of the tree */
1376 while ((child
= dm_tree_next_child(&handle
, dnode
, 0))) {
1377 if (!(dinfo
= dm_tree_node_get_info(child
))) {
1382 if (!(name
= dm_tree_node_get_name(child
))) {
1387 if (!(uuid
= dm_tree_node_get_uuid(child
))) {
1392 /* Ignore if it doesn't belong to this VG */
1393 if (!_uuid_prefix_matches(uuid
, uuid_prefix
, uuid_prefix_len
))
1396 /* Ensure immediate parents are already suspended */
1397 if (!_children_suspended(child
, 1, uuid_prefix
, uuid_prefix_len
))
1400 if (!_info_by_dev(dinfo
->major
, dinfo
->minor
, 0, &info
) ||
1401 !info
.exists
|| info
.suspended
)
1404 if (!_suspend_node(name
, info
.major
, info
.minor
,
1405 child
->dtree
->skip_lockfs
,
1406 child
->dtree
->no_flush
, &newinfo
)) {
1407 log_error("Unable to suspend %s (%" PRIu32
1408 ":%" PRIu32
")", name
, info
.major
,
1414 /* Update cached info */
1415 child
->info
= newinfo
;
1418 /* Then suspend any child nodes */
1421 while ((child
= dm_tree_next_child(&handle
, dnode
, 0))) {
1422 if (!(uuid
= dm_tree_node_get_uuid(child
))) {
1427 /* Ignore if it doesn't belong to this VG */
1428 if (!_uuid_prefix_matches(uuid
, uuid_prefix
, uuid_prefix_len
))
1431 if (dm_tree_node_num_children(child
, 0))
1432 if (!dm_tree_suspend_children(child
, uuid_prefix
, uuid_prefix_len
))
1439 int dm_tree_activate_children(struct dm_tree_node
*dnode
,
1440 const char *uuid_prefix
,
1441 size_t uuid_prefix_len
)
1444 void *handle
= NULL
;
1445 struct dm_tree_node
*child
= dnode
;
1446 struct dm_info newinfo
;
1451 /* Activate children first */
1452 while ((child
= dm_tree_next_child(&handle
, dnode
, 0))) {
1453 if (!(uuid
= dm_tree_node_get_uuid(child
))) {
1458 if (!_uuid_prefix_matches(uuid
, uuid_prefix
, uuid_prefix_len
))
1461 if (dm_tree_node_num_children(child
, 0))
1462 if (!dm_tree_activate_children(child
, uuid_prefix
, uuid_prefix_len
))
1468 for (priority
= 0; priority
< 3; priority
++) {
1469 while ((child
= dm_tree_next_child(&handle
, dnode
, 0))) {
1470 if (priority
!= child
->activation_priority
)
1473 if (!(uuid
= dm_tree_node_get_uuid(child
))) {
1478 if (!_uuid_prefix_matches(uuid
, uuid_prefix
, uuid_prefix_len
))
1481 if (!(name
= dm_tree_node_get_name(child
))) {
1487 if (child
->props
.new_name
) {
1488 if (!_rename_node(name
, child
->props
.new_name
, child
->info
.major
,
1489 child
->info
.minor
, &child
->dtree
->cookie
,
1490 child
->udev_flags
)) {
1491 log_error("Failed to rename %s (%" PRIu32
1492 ":%" PRIu32
") to %s", name
, child
->info
.major
,
1493 child
->info
.minor
, child
->props
.new_name
);
1496 child
->name
= child
->props
.new_name
;
1497 child
->props
.new_name
= NULL
;
1500 if (!child
->info
.inactive_table
&& !child
->info
.suspended
)
1503 if (!_resume_node(child
->name
, child
->info
.major
, child
->info
.minor
,
1504 child
->props
.read_ahead
, child
->props
.read_ahead_flags
,
1505 &newinfo
, &child
->dtree
->cookie
, child
->udev_flags
, child
->info
.suspended
)) {
1506 log_error("Unable to resume %s (%" PRIu32
1507 ":%" PRIu32
")", child
->name
, child
->info
.major
,
1513 /* Update cached info */
1514 child
->info
= newinfo
;
1523 static int _create_node(struct dm_tree_node
*dnode
)
1526 struct dm_task
*dmt
;
1528 log_verbose("Creating %s", dnode
->name
);
1530 if (!(dmt
= dm_task_create(DM_DEVICE_CREATE
))) {
1531 log_error("Create dm_task creation failed for %s", dnode
->name
);
1535 if (!dm_task_set_name(dmt
, dnode
->name
)) {
1536 log_error("Failed to set device name for %s", dnode
->name
);
1540 if (!dm_task_set_uuid(dmt
, dnode
->uuid
)) {
1541 log_error("Failed to set uuid for %s", dnode
->name
);
1545 if (dnode
->props
.major
&&
1546 (!dm_task_set_major(dmt
, dnode
->props
.major
) ||
1547 !dm_task_set_minor(dmt
, dnode
->props
.minor
))) {
1548 log_error("Failed to set device number for %s creation.", dnode
->name
);
1552 if (dnode
->props
.read_only
&& !dm_task_set_ro(dmt
)) {
1553 log_error("Failed to set read only flag for %s", dnode
->name
);
1557 if (!dm_task_no_open_count(dmt
))
1558 log_error("Failed to disable open_count");
1560 if ((r
= dm_task_run(dmt
)))
1561 r
= dm_task_get_info(dmt
, &dnode
->info
);
1564 dm_task_destroy(dmt
);
1570 static int _build_dev_string(char *devbuf
, size_t bufsize
, struct dm_tree_node
*node
)
1572 if (!dm_format_dev(devbuf
, bufsize
, node
->info
.major
, node
->info
.minor
)) {
1573 log_error("Failed to format %s device number for %s as dm "
1575 node
->name
, node
->uuid
, node
->info
.major
, node
->info
.minor
);
1582 /* simplify string emiting code */
1583 #define EMIT_PARAMS(p, str...)\
1586 if ((w = dm_snprintf(params + p, paramsize - (size_t) p, str)) < 0) {\
1587 stack; /* Out of space */\
1596 * Returns: 1 on success, 0 on failure
1598 static int _emit_areas_line(struct dm_task
*dmt
__attribute__((unused
)),
1599 struct load_segment
*seg
, char *params
,
1600 size_t paramsize
, int *pos
)
1602 struct seg_area
*area
;
1603 char devbuf
[DM_FORMAT_DEV_BUFSIZE
];
1604 unsigned first_time
= 1;
1605 const char *logtype
, *synctype
;
1606 unsigned log_parm_count
;
1608 dm_list_iterate_items(area
, &seg
->areas
) {
1609 switch (seg
->type
) {
1610 case SEG_REPLICATOR_DEV
:
1611 if (!_build_dev_string(devbuf
, sizeof(devbuf
), area
->dev_node
))
1614 EMIT_PARAMS(*pos
, " %d 1 %s", area
->rsite_index
, devbuf
);
1616 EMIT_PARAMS(*pos
, " nolog 0");
1618 /* Remote devices */
1619 log_parm_count
= (area
->flags
&
1620 (DM_NOSYNC
| DM_FORCESYNC
)) ? 2 : 1;
1623 devbuf
[0] = 0; /* Only core log parameters */
1626 devbuf
[0] = ' '; /* Extra space before device name */
1627 if (!_build_dev_string(devbuf
+ 1,
1632 log_parm_count
++; /* Extra sync log device name parameter */
1635 EMIT_PARAMS(*pos
, " %s %u%s %" PRIu64
, logtype
,
1636 log_parm_count
, devbuf
, area
->region_size
);
1638 synctype
= (area
->flags
& DM_NOSYNC
) ?
1639 " nosync" : (area
->flags
& DM_FORCESYNC
) ?
1643 EMIT_PARAMS(*pos
, "%s", synctype
);
1655 if (!area
->dev_node
) {
1656 EMIT_PARAMS(*pos
, " -");
1659 if (!_build_dev_string(devbuf
, sizeof(devbuf
), area
->dev_node
))
1662 EMIT_PARAMS(*pos
, " %s", devbuf
);
1665 if (!_build_dev_string(devbuf
, sizeof(devbuf
), area
->dev_node
))
1668 EMIT_PARAMS(*pos
, "%s%s %" PRIu64
, first_time
? "" : " ",
1669 devbuf
, area
->offset
);
1678 static int _replicator_emit_segment_line(const struct load_segment
*seg
, char *params
,
1679 size_t paramsize
, int *pos
)
1681 const struct load_segment
*rlog_seg
;
1682 struct replicator_site
*rsite
;
1683 char rlogbuf
[DM_FORMAT_DEV_BUFSIZE
];
1684 unsigned parm_count
;
1686 if (!seg
->log
|| !_build_dev_string(rlogbuf
, sizeof(rlogbuf
), seg
->log
))
1689 rlog_seg
= dm_list_item(dm_list_last(&seg
->log
->props
.segs
),
1690 struct load_segment
);
1692 EMIT_PARAMS(*pos
, "%s 4 %s 0 auto %" PRIu64
,
1693 seg
->rlog_type
, rlogbuf
, rlog_seg
->size
);
1695 dm_list_iterate_items(rsite
, &seg
->rsites
) {
1696 parm_count
= (rsite
->fall_behind_data
1697 || rsite
->fall_behind_ios
1698 || rsite
->async_timeout
) ? 4 : 2;
1700 EMIT_PARAMS(*pos
, " blockdev %u %u %s", parm_count
, rsite
->rsite_index
,
1701 (rsite
->mode
== DM_REPLICATOR_SYNC
) ? "synchronous" : "asynchronous");
1703 if (rsite
->fall_behind_data
)
1704 EMIT_PARAMS(*pos
, " data %" PRIu64
, rsite
->fall_behind_data
);
1705 else if (rsite
->fall_behind_ios
)
1706 EMIT_PARAMS(*pos
, " ios %" PRIu32
, rsite
->fall_behind_ios
);
1707 else if (rsite
->async_timeout
)
1708 EMIT_PARAMS(*pos
, " timeout %" PRIu32
, rsite
->async_timeout
);
1715 * Returns: 1 on success, 0 on failure
1717 static int _mirror_emit_segment_line(struct dm_task
*dmt
, struct load_segment
*seg
,
1718 char *params
, size_t paramsize
)
1720 int block_on_error
= 0;
1721 int handle_errors
= 0;
1722 int dm_log_userspace
= 0;
1724 unsigned log_parm_count
;
1726 char logbuf
[DM_FORMAT_DEV_BUFSIZE
];
1727 const char *logtype
;
1728 unsigned kmaj
= 0, kmin
= 0, krel
= 0;
1730 if (uname(&uts
) == -1) {
1731 log_error("Cannot read kernel release version.");
1735 /* Kernels with a major number of 2 always had 3 parts. */
1736 parts
= sscanf(uts
.release
, "%u.%u.%u", &kmaj
, &kmin
, &krel
);
1737 if (parts
< 1 || (kmaj
< 3 && parts
< 3)) {
1738 log_error("Wrong kernel release version %s.", uts
.release
);
1742 if ((seg
->flags
& DM_BLOCK_ON_ERROR
)) {
1744 * Originally, block_on_error was an argument to the log
1745 * portion of the mirror CTR table. It was renamed to
1746 * "handle_errors" and now resides in the 'features'
1747 * section of the mirror CTR table (i.e. at the end).
1749 * We can identify whether to use "block_on_error" or
1750 * "handle_errors" by the dm-mirror module's version
1751 * number (>= 1.12) or by the kernel version (>= 2.6.22).
1753 if (KERNEL_VERSION(kmaj
, kmin
, krel
) >= KERNEL_VERSION(2, 6, 22))
1759 if (seg
->clustered
) {
1760 /* Cluster mirrors require a UUID */
1765 * Cluster mirrors used to have their own log
1766 * types. Now they are accessed through the
1767 * userspace log type.
1769 * The dm-log-userspace module was added to the
1772 if (KERNEL_VERSION(kmaj
, kmin
, krel
) >= KERNEL_VERSION(2, 6, 31))
1773 dm_log_userspace
= 1;
1779 /* [no]sync, block_on_error etc. */
1780 log_parm_count
+= hweight32(seg
->flags
);
1782 /* "handle_errors" is a feature arg now */
1786 /* DM_CORELOG does not count in the param list */
1787 if (seg
->flags
& DM_CORELOG
)
1790 if (seg
->clustered
) {
1791 log_parm_count
++; /* For UUID */
1793 if (!dm_log_userspace
)
1794 EMIT_PARAMS(pos
, "clustered-");
1796 /* For clustered-* type field inserted later */
1805 if (!_build_dev_string(logbuf
, sizeof(logbuf
), seg
->log
))
1809 if (dm_log_userspace
)
1810 EMIT_PARAMS(pos
, "userspace %u %s clustered-%s",
1811 log_parm_count
, seg
->uuid
, logtype
);
1813 EMIT_PARAMS(pos
, "%s %u", logtype
, log_parm_count
);
1816 EMIT_PARAMS(pos
, " %s", logbuf
);
1818 EMIT_PARAMS(pos
, " %u", seg
->region_size
);
1820 if (seg
->clustered
&& !dm_log_userspace
)
1821 EMIT_PARAMS(pos
, " %s", seg
->uuid
);
1823 if ((seg
->flags
& DM_NOSYNC
))
1824 EMIT_PARAMS(pos
, " nosync");
1825 else if ((seg
->flags
& DM_FORCESYNC
))
1826 EMIT_PARAMS(pos
, " sync");
1829 EMIT_PARAMS(pos
, " block_on_error");
1831 EMIT_PARAMS(pos
, " %u ", seg
->mirror_area_count
);
1833 if (_emit_areas_line(dmt
, seg
, params
, paramsize
, &pos
) <= 0)
1837 EMIT_PARAMS(pos
, " 1 handle_errors");
1842 static int _raid_emit_segment_line(struct dm_task
*dmt
, uint32_t major
,
1843 uint32_t minor
, struct load_segment
*seg
,
1844 uint64_t *seg_start
, char *params
,
1848 int param_count
= 1; /* mandatory 'chunk size'/'stripe size' arg */
1851 if ((seg
->flags
& DM_NOSYNC
) || (seg
->flags
& DM_FORCESYNC
))
1854 if (seg
->region_size
)
1857 /* rebuilds is 64-bit */
1858 param_count
+= 2 * hweight32(seg
->rebuilds
& 0xFFFFFFFF);
1859 param_count
+= 2 * hweight32(seg
->rebuilds
>> 32);
1861 if ((seg
->type
== SEG_RAID1
) && seg
->stripe_size
)
1862 log_error("WARNING: Ignoring RAID1 stripe size");
1864 EMIT_PARAMS(pos
, "%s %d %u", dm_segtypes
[seg
->type
].target
,
1865 param_count
, seg
->stripe_size
);
1867 if (seg
->flags
& DM_NOSYNC
)
1868 EMIT_PARAMS(pos
, " nosync");
1869 else if (seg
->flags
& DM_FORCESYNC
)
1870 EMIT_PARAMS(pos
, " sync");
1872 if (seg
->region_size
)
1873 EMIT_PARAMS(pos
, " region_size %u", seg
->region_size
);
1875 for (i
= 0; i
< (seg
->area_count
/ 2); i
++)
1876 if (seg
->rebuilds
& (1 << i
))
1877 EMIT_PARAMS(pos
, " rebuild %u", i
);
1879 /* Print number of metadata/data device pairs */
1880 EMIT_PARAMS(pos
, " %u", seg
->area_count
/2);
1882 if (_emit_areas_line(dmt
, seg
, params
, paramsize
, &pos
) <= 0)
1888 static int _emit_segment_line(struct dm_task
*dmt
, uint32_t major
,
1889 uint32_t minor
, struct load_segment
*seg
,
1890 uint64_t *seg_start
, char *params
,
1895 int target_type_is_raid
= 0;
1896 char originbuf
[DM_FORMAT_DEV_BUFSIZE
], cowbuf
[DM_FORMAT_DEV_BUFSIZE
];
1897 char pool
[DM_FORMAT_DEV_BUFSIZE
], metadata
[DM_FORMAT_DEV_BUFSIZE
];
1905 /* Mirrors are pretty complicated - now in separate function */
1906 r
= _mirror_emit_segment_line(dmt
, seg
, params
, paramsize
);
1910 case SEG_REPLICATOR
:
1911 if ((r
= _replicator_emit_segment_line(seg
, params
, paramsize
,
1917 case SEG_REPLICATOR_DEV
:
1918 if (!seg
->replicator
|| !_build_dev_string(originbuf
,
1923 EMIT_PARAMS(pos
, "%s %" PRIu64
, originbuf
, seg
->rdevice_index
);
1926 case SEG_SNAPSHOT_MERGE
:
1927 if (!_build_dev_string(originbuf
, sizeof(originbuf
), seg
->origin
))
1929 if (!_build_dev_string(cowbuf
, sizeof(cowbuf
), seg
->cow
))
1931 EMIT_PARAMS(pos
, "%s %s %c %d", originbuf
, cowbuf
,
1932 seg
->persistent
? 'P' : 'N', seg
->chunk_size
);
1934 case SEG_SNAPSHOT_ORIGIN
:
1935 if (!_build_dev_string(originbuf
, sizeof(originbuf
), seg
->origin
))
1937 EMIT_PARAMS(pos
, "%s", originbuf
);
1940 EMIT_PARAMS(pos
, "%u %u ", seg
->area_count
, seg
->stripe_size
);
1943 EMIT_PARAMS(pos
, "%s%s%s%s%s %s %" PRIu64
" ", seg
->cipher
,
1944 seg
->chainmode
? "-" : "", seg
->chainmode
?: "",
1945 seg
->iv
? "-" : "", seg
->iv
?: "", seg
->key
,
1946 seg
->iv_offset
!= DM_CRYPT_IV_DEFAULT
?
1947 seg
->iv_offset
: *seg_start
);
1958 target_type_is_raid
= 1;
1959 r
= _raid_emit_segment_line(dmt
, major
, minor
, seg
, seg_start
,
1966 if (!_build_dev_string(metadata
, sizeof(metadata
), seg
->metadata
))
1968 if (!_build_dev_string(pool
, sizeof(pool
), seg
->pool
))
1970 EMIT_PARAMS(pos
, "%s %s %d %" PRIu64
" %s", metadata
, pool
,
1971 seg
->data_block_size
, seg
->low_water_mark_size
,
1972 seg
->skip_block_zeroing
? "1 skip_block_zeroing" : "");
1975 if (!_build_dev_string(pool
, sizeof(pool
), seg
->pool
))
1977 EMIT_PARAMS(pos
, "%s %d", pool
, seg
->device_id
);
1983 case SEG_REPLICATOR
:
1985 case SEG_SNAPSHOT_ORIGIN
:
1986 case SEG_SNAPSHOT_MERGE
:
1993 case SEG_REPLICATOR_DEV
:
1995 if ((r
= _emit_areas_line(dmt
, seg
, params
, paramsize
, &pos
)) <= 0) {
2000 log_error("No parameters supplied for %s target "
2001 "%u:%u.", dm_segtypes
[seg
->type
].target
,
2008 log_debug("Adding target to (%" PRIu32
":%" PRIu32
"): %" PRIu64
2009 " %" PRIu64
" %s %s", major
, minor
,
2010 *seg_start
, seg
->size
, target_type_is_raid
? "raid" :
2011 dm_segtypes
[seg
->type
].target
, params
);
2013 if (!dm_task_add_target(dmt
, *seg_start
, seg
->size
,
2014 target_type_is_raid
? "raid" :
2015 dm_segtypes
[seg
->type
].target
, params
))
2018 *seg_start
+= seg
->size
;
2025 static int _emit_segment(struct dm_task
*dmt
, uint32_t major
, uint32_t minor
,
2026 struct load_segment
*seg
, uint64_t *seg_start
)
2029 size_t paramsize
= 4096;
2033 if (!(params
= dm_malloc(paramsize
))) {
2034 log_error("Insufficient space for target parameters.");
2039 ret
= _emit_segment_line(dmt
, major
, minor
, seg
, seg_start
,
2049 log_debug("Insufficient space in params[%" PRIsize_t
2050 "] for target parameters.", paramsize
);
2053 } while (paramsize
< MAX_TARGET_PARAMSIZE
);
2055 log_error("Target parameter size too big. Aborting.");
2059 static int _load_node(struct dm_tree_node
*dnode
)
2062 struct dm_task
*dmt
;
2063 struct load_segment
*seg
;
2064 uint64_t seg_start
= 0, existing_table_size
;
2066 log_verbose("Loading %s table (%" PRIu32
":%" PRIu32
")", dnode
->name
,
2067 dnode
->info
.major
, dnode
->info
.minor
);
2069 if (!(dmt
= dm_task_create(DM_DEVICE_RELOAD
))) {
2070 log_error("Reload dm_task creation failed for %s", dnode
->name
);
2074 if (!dm_task_set_major(dmt
, dnode
->info
.major
) ||
2075 !dm_task_set_minor(dmt
, dnode
->info
.minor
)) {
2076 log_error("Failed to set device number for %s reload.", dnode
->name
);
2080 if (dnode
->props
.read_only
&& !dm_task_set_ro(dmt
)) {
2081 log_error("Failed to set read only flag for %s", dnode
->name
);
2085 if (!dm_task_no_open_count(dmt
))
2086 log_error("Failed to disable open_count");
2088 dm_list_iterate_items(seg
, &dnode
->props
.segs
)
2089 if (!_emit_segment(dmt
, dnode
->info
.major
, dnode
->info
.minor
,
2093 if (!dm_task_suppress_identical_reload(dmt
))
2094 log_error("Failed to suppress reload of identical tables.");
2096 if ((r
= dm_task_run(dmt
))) {
2097 r
= dm_task_get_info(dmt
, &dnode
->info
);
2098 if (r
&& !dnode
->info
.inactive_table
)
2099 log_verbose("Suppressed %s identical table reload.",
2102 existing_table_size
= dm_task_get_existing_table_size(dmt
);
2103 if ((dnode
->props
.size_changed
=
2104 (existing_table_size
== seg_start
) ? 0 : 1)) {
2105 log_debug("Table size changed from %" PRIu64
" to %"
2106 PRIu64
" for %s", existing_table_size
,
2107 seg_start
, dnode
->name
);
2109 * Kernel usually skips size validation on zero-length devices
2110 * now so no need to preload them.
2112 /* FIXME In which kernel version did this begin? */
2113 if (!existing_table_size
&& dnode
->props
.delay_resume_if_new
)
2114 dnode
->props
.size_changed
= 0;
2118 dnode
->props
.segment_count
= 0;
2121 dm_task_destroy(dmt
);
2126 int dm_tree_preload_children(struct dm_tree_node
*dnode
,
2127 const char *uuid_prefix
,
2128 size_t uuid_prefix_len
)
2131 void *handle
= NULL
;
2132 struct dm_tree_node
*child
;
2133 struct dm_info newinfo
;
2134 int update_devs_flag
= 0;
2136 /* Preload children first */
2137 while ((child
= dm_tree_next_child(&handle
, dnode
, 0))) {
2138 /* Skip existing non-device-mapper devices */
2139 if (!child
->info
.exists
&& child
->info
.major
)
2142 /* Ignore if it doesn't belong to this VG */
2143 if (child
->info
.exists
&&
2144 !_uuid_prefix_matches(child
->uuid
, uuid_prefix
, uuid_prefix_len
))
2147 if (dm_tree_node_num_children(child
, 0))
2148 if (!dm_tree_preload_children(child
, uuid_prefix
, uuid_prefix_len
))
2151 /* FIXME Cope if name exists with no uuid? */
2152 if (!child
->info
.exists
) {
2153 if (!_create_node(child
)) {
2159 if (!child
->info
.inactive_table
&& child
->props
.segment_count
) {
2160 if (!_load_node(child
)) {
2166 /* Propagate device size change change */
2167 if (child
->props
.size_changed
)
2168 dnode
->props
.size_changed
= 1;
2170 /* Resume device immediately if it has parents and its size changed */
2171 if (!dm_tree_node_num_children(child
, 1) || !child
->props
.size_changed
)
2174 if (!child
->info
.inactive_table
&& !child
->info
.suspended
)
2177 if (!_resume_node(child
->name
, child
->info
.major
, child
->info
.minor
,
2178 child
->props
.read_ahead
, child
->props
.read_ahead_flags
,
2179 &newinfo
, &child
->dtree
->cookie
, child
->udev_flags
,
2180 child
->info
.suspended
)) {
2181 log_error("Unable to resume %s (%" PRIu32
2182 ":%" PRIu32
")", child
->name
, child
->info
.major
,
2188 /* Update cached info */
2189 child
->info
= newinfo
;
2192 * Prepare for immediate synchronization with udev and flush all stacked
2193 * dev node operations if requested by immediate_dev_node property. But
2194 * finish processing current level in the tree first.
2196 if (child
->props
.immediate_dev_node
)
2197 update_devs_flag
= 1;
2202 if (update_devs_flag
) {
2203 if (!dm_udev_wait(dm_tree_get_cookie(dnode
)))
2205 dm_tree_set_cookie(dnode
, 0);
2212 * Returns 1 if unsure.
2214 int dm_tree_children_use_uuid(struct dm_tree_node
*dnode
,
2215 const char *uuid_prefix
,
2216 size_t uuid_prefix_len
)
2218 void *handle
= NULL
;
2219 struct dm_tree_node
*child
= dnode
;
2222 while ((child
= dm_tree_next_child(&handle
, dnode
, 0))) {
2223 if (!(uuid
= dm_tree_node_get_uuid(child
))) {
2224 log_error("Failed to get uuid for dtree node.");
2228 if (_uuid_prefix_matches(uuid
, uuid_prefix
, uuid_prefix_len
))
2231 if (dm_tree_node_num_children(child
, 0))
2232 dm_tree_children_use_uuid(child
, uuid_prefix
, uuid_prefix_len
);
2241 static struct load_segment
*_add_segment(struct dm_tree_node
*dnode
, unsigned type
, uint64_t size
)
2243 struct load_segment
*seg
;
2245 if (!(seg
= dm_pool_zalloc(dnode
->dtree
->mem
, sizeof(*seg
)))) {
2246 log_error("dtree node segment allocation failed");
2252 seg
->area_count
= 0;
2253 dm_list_init(&seg
->areas
);
2254 seg
->stripe_size
= 0;
2255 seg
->persistent
= 0;
2256 seg
->chunk_size
= 0;
2261 dm_list_add(&dnode
->props
.segs
, &seg
->list
);
2262 dnode
->props
.segment_count
++;
2267 int dm_tree_node_add_snapshot_origin_target(struct dm_tree_node
*dnode
,
2269 const char *origin_uuid
)
2271 struct load_segment
*seg
;
2272 struct dm_tree_node
*origin_node
;
2274 if (!(seg
= _add_segment(dnode
, SEG_SNAPSHOT_ORIGIN
, size
)))
2277 if (!(origin_node
= dm_tree_find_node_by_uuid(dnode
->dtree
, origin_uuid
))) {
2278 log_error("Couldn't find snapshot origin uuid %s.", origin_uuid
);
2282 seg
->origin
= origin_node
;
2283 if (!_link_tree_nodes(dnode
, origin_node
))
2286 /* Resume snapshot origins after new snapshots */
2287 dnode
->activation_priority
= 1;
2292 static int _add_snapshot_target(struct dm_tree_node
*node
,
2294 const char *origin_uuid
,
2295 const char *cow_uuid
,
2296 const char *merge_uuid
,
2298 uint32_t chunk_size
)
2300 struct load_segment
*seg
;
2301 struct dm_tree_node
*origin_node
, *cow_node
, *merge_node
;
2304 seg_type
= !merge_uuid
? SEG_SNAPSHOT
: SEG_SNAPSHOT_MERGE
;
2306 if (!(seg
= _add_segment(node
, seg_type
, size
)))
2309 if (!(origin_node
= dm_tree_find_node_by_uuid(node
->dtree
, origin_uuid
))) {
2310 log_error("Couldn't find snapshot origin uuid %s.", origin_uuid
);
2314 seg
->origin
= origin_node
;
2315 if (!_link_tree_nodes(node
, origin_node
))
2318 if (!(cow_node
= dm_tree_find_node_by_uuid(node
->dtree
, cow_uuid
))) {
2319 log_error("Couldn't find snapshot COW device uuid %s.", cow_uuid
);
2323 seg
->cow
= cow_node
;
2324 if (!_link_tree_nodes(node
, cow_node
))
2327 seg
->persistent
= persistent
? 1 : 0;
2328 seg
->chunk_size
= chunk_size
;
2331 if (!(merge_node
= dm_tree_find_node_by_uuid(node
->dtree
, merge_uuid
))) {
2332 /* not a pure error, merging snapshot may have been deactivated */
2333 log_verbose("Couldn't find merging snapshot uuid %s.", merge_uuid
);
2335 seg
->merge
= merge_node
;
2336 /* must not link merging snapshot, would undermine activation_priority below */
2339 /* Resume snapshot-merge (acting origin) after other snapshots */
2340 node
->activation_priority
= 1;
2342 /* Resume merging snapshot after snapshot-merge */
2343 seg
->merge
->activation_priority
= 2;
2351 int dm_tree_node_add_snapshot_target(struct dm_tree_node
*node
,
2353 const char *origin_uuid
,
2354 const char *cow_uuid
,
2356 uint32_t chunk_size
)
2358 return _add_snapshot_target(node
, size
, origin_uuid
, cow_uuid
,
2359 NULL
, persistent
, chunk_size
);
2362 int dm_tree_node_add_snapshot_merge_target(struct dm_tree_node
*node
,
2364 const char *origin_uuid
,
2365 const char *cow_uuid
,
2366 const char *merge_uuid
,
2367 uint32_t chunk_size
)
2369 return _add_snapshot_target(node
, size
, origin_uuid
, cow_uuid
,
2370 merge_uuid
, 1, chunk_size
);
2373 int dm_tree_node_add_error_target(struct dm_tree_node
*node
,
2376 if (!_add_segment(node
, SEG_ERROR
, size
))
2382 int dm_tree_node_add_zero_target(struct dm_tree_node
*node
,
2385 if (!_add_segment(node
, SEG_ZERO
, size
))
2391 int dm_tree_node_add_linear_target(struct dm_tree_node
*node
,
2394 if (!_add_segment(node
, SEG_LINEAR
, size
))
2400 int dm_tree_node_add_striped_target(struct dm_tree_node
*node
,
2402 uint32_t stripe_size
)
2404 struct load_segment
*seg
;
2406 if (!(seg
= _add_segment(node
, SEG_STRIPED
, size
)))
2409 seg
->stripe_size
= stripe_size
;
2414 int dm_tree_node_add_crypt_target(struct dm_tree_node
*node
,
2417 const char *chainmode
,
2422 struct load_segment
*seg
;
2424 if (!(seg
= _add_segment(node
, SEG_CRYPT
, size
)))
2427 seg
->cipher
= cipher
;
2428 seg
->chainmode
= chainmode
;
2430 seg
->iv_offset
= iv_offset
;
2436 int dm_tree_node_add_mirror_target_log(struct dm_tree_node
*node
,
2437 uint32_t region_size
,
2439 const char *log_uuid
,
2440 unsigned area_count
,
2443 struct dm_tree_node
*log_node
= NULL
;
2444 struct load_segment
*seg
;
2446 if (!node
->props
.segment_count
) {
2447 log_error(INTERNAL_ERROR
"Attempt to add target area to missing segment.");
2451 seg
= dm_list_item(dm_list_last(&node
->props
.segs
), struct load_segment
);
2454 if (!(seg
->uuid
= dm_pool_strdup(node
->dtree
->mem
, log_uuid
))) {
2455 log_error("log uuid pool_strdup failed");
2458 if ((flags
& DM_CORELOG
))
2459 /* For pvmove: immediate resume (for size validation) isn't needed. */
2460 node
->props
.delay_resume_if_new
= 1;
2462 if (!(log_node
= dm_tree_find_node_by_uuid(node
->dtree
, log_uuid
))) {
2463 log_error("Couldn't find mirror log uuid %s.", log_uuid
);
2468 log_node
->props
.immediate_dev_node
= 1;
2470 /* The kernel validates the size of disk logs. */
2471 /* FIXME Propagate to any devices below */
2472 log_node
->props
.delay_resume_if_new
= 0;
2474 if (!_link_tree_nodes(node
, log_node
))
2479 seg
->log
= log_node
;
2480 seg
->region_size
= region_size
;
2481 seg
->clustered
= clustered
;
2482 seg
->mirror_area_count
= area_count
;
2488 int dm_tree_node_add_mirror_target(struct dm_tree_node
*node
,
2491 if (!_add_segment(node
, SEG_MIRRORED
, size
))
2497 int dm_tree_node_add_raid_target(struct dm_tree_node
*node
,
2499 const char *raid_type
,
2500 uint32_t region_size
,
2501 uint32_t stripe_size
,
2506 struct load_segment
*seg
= NULL
;
2508 for (i
= 0; dm_segtypes
[i
].target
&& !seg
; i
++)
2509 if (!strcmp(raid_type
, dm_segtypes
[i
].target
))
2510 if (!(seg
= _add_segment(node
,
2511 dm_segtypes
[i
].type
, size
)))
2517 seg
->region_size
= region_size
;
2518 seg
->stripe_size
= stripe_size
;
2519 seg
->area_count
= 0;
2520 seg
->rebuilds
= rebuilds
;
2525 int dm_tree_node_add_replicator_target(struct dm_tree_node
*node
,
2527 const char *rlog_uuid
,
2528 const char *rlog_type
,
2529 unsigned rsite_index
,
2530 dm_replicator_mode_t mode
,
2531 uint32_t async_timeout
,
2532 uint64_t fall_behind_data
,
2533 uint32_t fall_behind_ios
)
2535 struct load_segment
*rseg
;
2536 struct replicator_site
*rsite
;
2538 /* Local site0 - adds replicator segment and links rlog device */
2539 if (rsite_index
== REPLICATOR_LOCAL_SITE
) {
2540 if (node
->props
.segment_count
) {
2541 log_error(INTERNAL_ERROR
"Attempt to add replicator segment to already used node.");
2545 if (!(rseg
= _add_segment(node
, SEG_REPLICATOR
, size
)))
2548 if (!(rseg
->log
= dm_tree_find_node_by_uuid(node
->dtree
, rlog_uuid
))) {
2549 log_error("Missing replicator log uuid %s.", rlog_uuid
);
2553 if (!_link_tree_nodes(node
, rseg
->log
))
2556 if (strcmp(rlog_type
, "ringbuffer") != 0) {
2557 log_error("Unsupported replicator log type %s.", rlog_type
);
2561 if (!(rseg
->rlog_type
= dm_pool_strdup(node
->dtree
->mem
, rlog_type
)))
2564 dm_list_init(&rseg
->rsites
);
2565 rseg
->rdevice_count
= 0;
2566 node
->activation_priority
= 1;
2569 /* Add site to segment */
2570 if (mode
== DM_REPLICATOR_SYNC
2571 && (async_timeout
|| fall_behind_ios
|| fall_behind_data
)) {
2572 log_error("Async parameters passed for synchronnous replicator.");
2576 if (node
->props
.segment_count
!= 1) {
2577 log_error(INTERNAL_ERROR
"Attempt to add remote site area before setting replicator log.");
2581 rseg
= dm_list_item(dm_list_last(&node
->props
.segs
), struct load_segment
);
2582 if (rseg
->type
!= SEG_REPLICATOR
) {
2583 log_error(INTERNAL_ERROR
"Attempt to use non replicator segment %s.",
2584 dm_segtypes
[rseg
->type
].target
);
2588 if (!(rsite
= dm_pool_zalloc(node
->dtree
->mem
, sizeof(*rsite
)))) {
2589 log_error("Failed to allocate remote site segment.");
2593 dm_list_add(&rseg
->rsites
, &rsite
->list
);
2594 rseg
->rsite_count
++;
2597 rsite
->async_timeout
= async_timeout
;
2598 rsite
->fall_behind_data
= fall_behind_data
;
2599 rsite
->fall_behind_ios
= fall_behind_ios
;
2600 rsite
->rsite_index
= rsite_index
;
2605 /* Appends device node to Replicator */
2606 int dm_tree_node_add_replicator_dev_target(struct dm_tree_node
*node
,
2608 const char *replicator_uuid
,
2609 uint64_t rdevice_index
,
2610 const char *rdev_uuid
,
2611 unsigned rsite_index
,
2612 const char *slog_uuid
,
2613 uint32_t slog_flags
,
2614 uint32_t slog_region_size
)
2616 struct seg_area
*area
;
2617 struct load_segment
*rseg
;
2618 struct load_segment
*rep_seg
;
2620 if (rsite_index
== REPLICATOR_LOCAL_SITE
) {
2621 /* Site index for local target */
2622 if (!(rseg
= _add_segment(node
, SEG_REPLICATOR_DEV
, size
)))
2625 if (!(rseg
->replicator
= dm_tree_find_node_by_uuid(node
->dtree
, replicator_uuid
))) {
2626 log_error("Missing replicator uuid %s.", replicator_uuid
);
2630 /* Local slink0 for replicator must be always initialized first */
2631 if (rseg
->replicator
->props
.segment_count
!= 1) {
2632 log_error(INTERNAL_ERROR
"Attempt to use non replicator segment.");
2636 rep_seg
= dm_list_item(dm_list_last(&rseg
->replicator
->props
.segs
), struct load_segment
);
2637 if (rep_seg
->type
!= SEG_REPLICATOR
) {
2638 log_error(INTERNAL_ERROR
"Attempt to use non replicator segment %s.",
2639 dm_segtypes
[rep_seg
->type
].target
);
2642 rep_seg
->rdevice_count
++;
2644 if (!_link_tree_nodes(node
, rseg
->replicator
))
2647 rseg
->rdevice_index
= rdevice_index
;
2649 /* Local slink0 for replicator must be always initialized first */
2650 if (node
->props
.segment_count
!= 1) {
2651 log_error(INTERNAL_ERROR
"Attempt to use non replicator-dev segment.");
2655 rseg
= dm_list_item(dm_list_last(&node
->props
.segs
), struct load_segment
);
2656 if (rseg
->type
!= SEG_REPLICATOR_DEV
) {
2657 log_error(INTERNAL_ERROR
"Attempt to use non replicator-dev segment %s.",
2658 dm_segtypes
[rseg
->type
].target
);
2663 if (!(slog_flags
& DM_CORELOG
) && !slog_uuid
) {
2664 log_error("Unspecified sync log uuid.");
2668 if (!dm_tree_node_add_target_area(node
, NULL
, rdev_uuid
, 0))
2671 area
= dm_list_item(dm_list_last(&rseg
->areas
), struct seg_area
);
2673 if (!(slog_flags
& DM_CORELOG
)) {
2674 if (!(area
->slog
= dm_tree_find_node_by_uuid(node
->dtree
, slog_uuid
))) {
2675 log_error("Couldn't find sync log uuid %s.", slog_uuid
);
2679 if (!_link_tree_nodes(node
, area
->slog
))
2683 area
->flags
= slog_flags
;
2684 area
->region_size
= slog_region_size
;
2685 area
->rsite_index
= rsite_index
;
2690 int dm_tree_node_add_thin_pool_target(struct dm_tree_node
*node
,
2692 uint64_t transaction_id
,
2693 const char *pool_uuid
,
2694 const char *metadata_uuid
,
2695 uint32_t data_block_size
,
2696 uint64_t low_water_mark_size
,
2697 unsigned skip_block_zeroing
)
2699 struct load_segment
*seg
;
2701 if (data_block_size
< DM_THIN_MIN_DATA_SIZE
) {
2702 log_error("Data block size %u is lower then %u sectors.",
2703 data_block_size
, DM_THIN_MIN_DATA_SIZE
);
2707 if (data_block_size
> DM_THIN_MAX_DATA_SIZE
) {
2708 log_error("Data block size %u is higher then %u sectors.",
2709 data_block_size
, DM_THIN_MAX_DATA_SIZE
);
2713 if (!(seg
= _add_segment(node
, SEG_THIN_POOL
, size
)))
2716 if (!(seg
->metadata
= dm_tree_find_node_by_uuid(node
->dtree
, metadata_uuid
))) {
2717 log_error("Missing metadata uuid %s.", metadata_uuid
);
2721 if (!_link_tree_nodes(node
, seg
->metadata
))
2724 if (!(seg
->pool
= dm_tree_find_node_by_uuid(node
->dtree
, pool_uuid
))) {
2725 log_error("Missing pool uuid %s.", pool_uuid
);
2729 if (!_link_tree_nodes(node
, seg
->pool
))
2732 node
->props
.thin_pool_transaction_id
= transaction_id
; // compare on resume
2733 seg
->low_water_mark_size
= low_water_mark_size
;
2734 seg
->data_block_size
= data_block_size
;
2735 seg
->skip_block_zeroing
= skip_block_zeroing
;
2740 int dm_tree_node_add_thin_target(struct dm_tree_node
*node
,
2742 const char *thin_pool_uuid
,
2745 struct load_segment
*seg
;
2747 if (device_id
> DM_THIN_MAX_DEVICE_ID
) {
2748 log_error("Device id %u is higher then %u.",
2749 device_id
, DM_THIN_MAX_DEVICE_ID
);
2753 if (!(seg
= _add_segment(node
, SEG_THIN
, size
)))
2756 if (!(seg
->pool
= dm_tree_find_node_by_uuid(node
->dtree
, thin_pool_uuid
))) {
2757 log_error("Missing thin pool uuid %s.", thin_pool_uuid
);
2761 if (!_link_tree_nodes(node
, seg
->pool
))
2764 seg
->device_id
= device_id
;
2769 static int _add_area(struct dm_tree_node
*node
, struct load_segment
*seg
, struct dm_tree_node
*dev_node
, uint64_t offset
)
2771 struct seg_area
*area
;
2773 if (!(area
= dm_pool_zalloc(node
->dtree
->mem
, sizeof (*area
)))) {
2774 log_error("Failed to allocate target segment area.");
2778 area
->dev_node
= dev_node
;
2779 area
->offset
= offset
;
2781 dm_list_add(&seg
->areas
, &area
->list
);
2787 int dm_tree_node_add_target_area(struct dm_tree_node
*node
,
2788 const char *dev_name
,
2792 struct load_segment
*seg
;
2794 struct dm_tree_node
*dev_node
;
2796 if ((!dev_name
|| !*dev_name
) && (!uuid
|| !*uuid
)) {
2797 log_error("dm_tree_node_add_target_area called without device");
2802 if (!(dev_node
= dm_tree_find_node_by_uuid(node
->dtree
, uuid
))) {
2803 log_error("Couldn't find area uuid %s.", uuid
);
2806 if (!_link_tree_nodes(node
, dev_node
))
2809 if (stat(dev_name
, &info
) < 0) {
2810 log_error("Device %s not found.", dev_name
);
2814 if (!S_ISBLK(info
.st_mode
)) {
2815 log_error("Device %s is not a block device.", dev_name
);
2819 /* FIXME Check correct macro use */
2820 if (!(dev_node
= _add_dev(node
->dtree
, node
, MAJOR(info
.st_rdev
),
2821 MINOR(info
.st_rdev
), 0)))
2825 if (!node
->props
.segment_count
) {
2826 log_error(INTERNAL_ERROR
"Attempt to add target area to missing segment.");
2830 seg
= dm_list_item(dm_list_last(&node
->props
.segs
), struct load_segment
);
2832 if (!_add_area(node
, seg
, dev_node
, offset
))
2838 int dm_tree_node_add_null_area(struct dm_tree_node
*node
, uint64_t offset
)
2840 struct load_segment
*seg
;
2842 seg
= dm_list_item(dm_list_last(&node
->props
.segs
), struct load_segment
);
2844 switch (seg
->type
) {
2856 log_error("dm_tree_node_add_null_area() called on an unsupported segment type");
2860 if (!_add_area(node
, seg
, NULL
, offset
))
2866 void dm_tree_set_cookie(struct dm_tree_node
*node
, uint32_t cookie
)
2868 node
->dtree
->cookie
= cookie
;
2871 uint32_t dm_tree_get_cookie(struct dm_tree_node
*node
)
2873 return node
->dtree
->cookie
;