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 #define THIN_MIN_DATA_SIZE 128
33 #define THIN_MAX_DATA_SIZE 2097152
34 #define THIN_MAX_DEVICE_ID ((1 << 24) - 1)
38 /* Supported segment types */
65 /* FIXME Add crypt and multipath support */
71 { SEG_CRYPT
, "crypt" },
72 { SEG_ERROR
, "error" },
73 { SEG_LINEAR
, "linear" },
74 { SEG_MIRRORED
, "mirror" },
75 { SEG_REPLICATOR
, "replicator" },
76 { SEG_REPLICATOR_DEV
, "replicator-dev" },
77 { SEG_SNAPSHOT
, "snapshot" },
78 { SEG_SNAPSHOT_ORIGIN
, "snapshot-origin" },
79 { SEG_SNAPSHOT_MERGE
, "snapshot-merge" },
80 { SEG_STRIPED
, "striped" },
82 { SEG_THIN_POOL
, "thin-pool"},
84 { SEG_RAID1
, "raid1"},
85 { SEG_RAID4
, "raid4"},
86 { SEG_RAID5_LA
, "raid5_la"},
87 { SEG_RAID5_RA
, "raid5_ra"},
88 { SEG_RAID5_LS
, "raid5_ls"},
89 { SEG_RAID5_RS
, "raid5_rs"},
90 { SEG_RAID6_ZR
, "raid6_zr"},
91 { SEG_RAID6_NR
, "raid6_nr"},
92 { SEG_RAID6_NC
, "raid6_nc"},
95 *WARNING: Since 'raid' target overloads this 1:1 mapping table
96 * for search do not add new enum elements past them!
98 { SEG_RAID5_LS
, "raid5"}, /* same as "raid5_ls" (default for MD also) */
99 { SEG_RAID6_ZR
, "raid6"}, /* same as "raid6_zr" */
103 /* Some segment types have a list of areas of other devices attached */
107 struct dm_tree_node
*dev_node
;
111 unsigned rsite_index
; /* Replicator site index */
112 struct dm_tree_node
*slog
; /* Replicator sync log node */
113 uint64_t region_size
; /* Replicator sync log size */
114 uint32_t flags
; /* Replicator sync log flags */
117 /* Replicator-log has a list of sites */
118 /* FIXME: maybe move to seg_area too? */
119 struct replicator_site
{
122 unsigned rsite_index
;
123 dm_replicator_mode_t mode
;
124 uint32_t async_timeout
;
125 uint32_t fall_behind_ios
;
126 uint64_t fall_behind_data
;
129 /* Per-segment properties */
130 struct load_segment
{
137 unsigned area_count
; /* Linear + Striped + Mirrored + Crypt + Replicator */
138 struct dm_list areas
; /* Linear + Striped + Mirrored + Crypt + Replicator */
140 uint32_t stripe_size
; /* Striped + raid */
142 int persistent
; /* Snapshot */
143 uint32_t chunk_size
; /* Snapshot */
144 struct dm_tree_node
*cow
; /* Snapshot */
145 struct dm_tree_node
*origin
; /* Snapshot + Snapshot origin */
146 struct dm_tree_node
*merge
; /* Snapshot */
148 struct dm_tree_node
*log
; /* Mirror + Replicator */
149 uint32_t region_size
; /* Mirror + raid */
150 unsigned clustered
; /* Mirror */
151 unsigned mirror_area_count
; /* Mirror */
152 uint32_t flags
; /* Mirror log */
153 char *uuid
; /* Clustered mirror log */
155 const char *cipher
; /* Crypt */
156 const char *chainmode
; /* Crypt */
157 const char *iv
; /* Crypt */
158 uint64_t iv_offset
; /* Crypt */
159 const char *key
; /* Crypt */
161 const char *rlog_type
; /* Replicator */
162 struct dm_list rsites
; /* Replicator */
163 unsigned rsite_count
; /* Replicator */
164 unsigned rdevice_count
; /* Replicator */
165 struct dm_tree_node
*replicator
;/* Replicator-dev */
166 uint64_t rdevice_index
; /* Replicator-dev */
168 uint64_t rebuilds
; /* raid */
170 struct dm_tree_node
*metadata
; /* Thin_pool */
171 struct dm_tree_node
*pool
; /* Thin_pool, Thin */
172 uint64_t low_water_mark_size
; /* Thin_pool */
173 uint32_t data_block_size
; /* Thin_pool */
174 unsigned skip_block_zeroing
; /* Thin_pool */
175 uint32_t device_id
; /* Thin */
179 /* Per-device properties */
180 struct load_properties
{
186 uint32_t read_ahead_flags
;
188 uint64_t thin_pool_transaction_id
; /* Thin_pool */
190 unsigned segment_count
;
191 unsigned size_changed
;
194 const char *new_name
;
196 /* If immediate_dev_node is set to 1, try to create the dev node
197 * as soon as possible (e.g. in preload stage even during traversal
198 * and processing of dm tree). This will also flush all stacked dev
199 * node operations, synchronizing with udev.
201 unsigned immediate_dev_node
;
204 * If the device size changed from zero and this is set,
205 * don't resume the device immediately, even if the device
206 * has parents. This works provided the parents do not
207 * validate the device size and is required by pvmove to
208 * avoid starting the mirror resync operation too early.
210 unsigned delay_resume_if_new
;
213 /* Two of these used to join two nodes with uses and used_by. */
214 struct dm_tree_link
{
216 struct dm_tree_node
*node
;
219 struct dm_tree_node
{
220 struct dm_tree
*dtree
;
226 struct dm_list uses
; /* Nodes this node uses */
227 struct dm_list used_by
; /* Nodes that use this node */
229 int activation_priority
; /* 0 gets activated first */
231 uint16_t udev_flags
; /* Udev control flags */
233 void *context
; /* External supplied context */
235 struct load_properties props
; /* For creation/table (re)load */
238 * If presuspend of child node is needed
239 * Note: only direct child is allowed
241 struct dm_tree_node
*presuspend_node
;
246 struct dm_hash_table
*devs
;
247 struct dm_hash_table
*uuids
;
248 struct dm_tree_node root
;
249 int skip_lockfs
; /* 1 skips lockfs (for non-snapshots) */
250 int no_flush
; /* 1 sets noflush (mirrors/multipath) */
251 int retry_remove
; /* 1 retries remove if not successful */
255 struct dm_tree
*dm_tree_create(void)
257 struct dm_tree
*dtree
;
259 if (!(dtree
= dm_zalloc(sizeof(*dtree
)))) {
260 log_error("dm_tree_create malloc failed");
264 dtree
->root
.dtree
= dtree
;
265 dm_list_init(&dtree
->root
.uses
);
266 dm_list_init(&dtree
->root
.used_by
);
267 dtree
->skip_lockfs
= 0;
270 if (!(dtree
->mem
= dm_pool_create("dtree", 1024))) {
271 log_error("dtree pool creation failed");
276 if (!(dtree
->devs
= dm_hash_create(8))) {
277 log_error("dtree hash creation failed");
278 dm_pool_destroy(dtree
->mem
);
283 if (!(dtree
->uuids
= dm_hash_create(32))) {
284 log_error("dtree uuid hash creation failed");
285 dm_hash_destroy(dtree
->devs
);
286 dm_pool_destroy(dtree
->mem
);
294 void dm_tree_free(struct dm_tree
*dtree
)
299 dm_hash_destroy(dtree
->uuids
);
300 dm_hash_destroy(dtree
->devs
);
301 dm_pool_destroy(dtree
->mem
);
305 static int _nodes_are_linked(const struct dm_tree_node
*parent
,
306 const struct dm_tree_node
*child
)
308 struct dm_tree_link
*dlink
;
310 dm_list_iterate_items(dlink
, &parent
->uses
)
311 if (dlink
->node
== child
)
317 static int _link(struct dm_list
*list
, struct dm_tree_node
*node
)
319 struct dm_tree_link
*dlink
;
321 if (!(dlink
= dm_pool_alloc(node
->dtree
->mem
, sizeof(*dlink
)))) {
322 log_error("dtree link allocation failed");
327 dm_list_add(list
, &dlink
->list
);
332 static int _link_nodes(struct dm_tree_node
*parent
,
333 struct dm_tree_node
*child
)
335 if (_nodes_are_linked(parent
, child
))
338 if (!_link(&parent
->uses
, child
))
341 if (!_link(&child
->used_by
, parent
))
347 static void _unlink(struct dm_list
*list
, struct dm_tree_node
*node
)
349 struct dm_tree_link
*dlink
;
351 dm_list_iterate_items(dlink
, list
)
352 if (dlink
->node
== node
) {
353 dm_list_del(&dlink
->list
);
358 static void _unlink_nodes(struct dm_tree_node
*parent
,
359 struct dm_tree_node
*child
)
361 if (!_nodes_are_linked(parent
, child
))
364 _unlink(&parent
->uses
, child
);
365 _unlink(&child
->used_by
, parent
);
368 static int _add_to_toplevel(struct dm_tree_node
*node
)
370 return _link_nodes(&node
->dtree
->root
, node
);
373 static void _remove_from_toplevel(struct dm_tree_node
*node
)
375 _unlink_nodes(&node
->dtree
->root
, node
);
378 static int _add_to_bottomlevel(struct dm_tree_node
*node
)
380 return _link_nodes(node
, &node
->dtree
->root
);
383 static void _remove_from_bottomlevel(struct dm_tree_node
*node
)
385 _unlink_nodes(node
, &node
->dtree
->root
);
388 static int _link_tree_nodes(struct dm_tree_node
*parent
, struct dm_tree_node
*child
)
390 /* Don't link to root node if child already has a parent */
391 if (parent
== &parent
->dtree
->root
) {
392 if (dm_tree_node_num_children(child
, 1))
395 _remove_from_toplevel(child
);
397 if (child
== &child
->dtree
->root
) {
398 if (dm_tree_node_num_children(parent
, 0))
401 _remove_from_bottomlevel(parent
);
403 return _link_nodes(parent
, child
);
406 static struct dm_tree_node
*_create_dm_tree_node(struct dm_tree
*dtree
,
409 struct dm_info
*info
,
413 struct dm_tree_node
*node
;
416 if (!(node
= dm_pool_zalloc(dtree
->mem
, sizeof(*node
)))) {
417 log_error("_create_dm_tree_node alloc failed");
426 node
->context
= context
;
427 node
->udev_flags
= udev_flags
;
428 node
->activation_priority
= 0;
430 dm_list_init(&node
->uses
);
431 dm_list_init(&node
->used_by
);
432 dm_list_init(&node
->props
.segs
);
434 dev
= MKDEV(info
->major
, info
->minor
);
436 if (!dm_hash_insert_binary(dtree
->devs
, (const char *) &dev
,
437 sizeof(dev
), node
)) {
438 log_error("dtree node hash insertion failed");
439 dm_pool_free(dtree
->mem
, node
);
444 !dm_hash_insert(dtree
->uuids
, uuid
, node
)) {
445 log_error("dtree uuid hash insertion failed");
446 dm_hash_remove_binary(dtree
->devs
, (const char *) &dev
,
448 dm_pool_free(dtree
->mem
, node
);
455 static struct dm_tree_node
*_find_dm_tree_node(struct dm_tree
*dtree
,
456 uint32_t major
, uint32_t minor
)
458 uint64_t dev
= MKDEV(major
, minor
);
460 return dm_hash_lookup_binary(dtree
->devs
, (const char *) &dev
,
464 static struct dm_tree_node
*_find_dm_tree_node_by_uuid(struct dm_tree
*dtree
,
467 struct dm_tree_node
*node
;
469 if ((node
= dm_hash_lookup(dtree
->uuids
, uuid
)))
472 if (strncmp(uuid
, UUID_PREFIX
, sizeof(UUID_PREFIX
) - 1))
475 return dm_hash_lookup(dtree
->uuids
, uuid
+ sizeof(UUID_PREFIX
) - 1);
478 static int _deps(struct dm_task
**dmt
, struct dm_pool
*mem
, uint32_t major
, uint32_t minor
,
479 const char **name
, const char **uuid
,
480 struct dm_info
*info
, struct dm_deps
**deps
)
482 memset(info
, 0, sizeof(*info
));
484 if (!dm_is_dm_major(major
)) {
491 info
->live_table
= 0;
492 info
->inactive_table
= 0;
497 if (!(*dmt
= dm_task_create(DM_DEVICE_DEPS
))) {
498 log_error("deps dm_task creation failed");
502 if (!dm_task_set_major(*dmt
, major
)) {
503 log_error("_deps: failed to set major for (%" PRIu32
":%" PRIu32
")",
508 if (!dm_task_set_minor(*dmt
, minor
)) {
509 log_error("_deps: failed to set minor for (%" PRIu32
":%" PRIu32
")",
514 if (!dm_task_run(*dmt
)) {
515 log_error("_deps: task run failed for (%" PRIu32
":%" PRIu32
")",
520 if (!dm_task_get_info(*dmt
, info
)) {
521 log_error("_deps: failed to get info for (%" PRIu32
":%" PRIu32
")",
531 if (info
->major
!= major
) {
532 log_error("Inconsistent dtree major number: %u != %u",
536 if (info
->minor
!= minor
) {
537 log_error("Inconsistent dtree minor number: %u != %u",
541 if (!(*name
= dm_pool_strdup(mem
, dm_task_get_name(*dmt
)))) {
542 log_error("name pool_strdup failed");
545 if (!(*uuid
= dm_pool_strdup(mem
, dm_task_get_uuid(*dmt
)))) {
546 log_error("uuid pool_strdup failed");
549 *deps
= dm_task_get_deps(*dmt
);
555 dm_task_destroy(*dmt
);
559 static struct dm_tree_node
*_add_dev(struct dm_tree
*dtree
,
560 struct dm_tree_node
*parent
,
561 uint32_t major
, uint32_t minor
,
564 struct dm_task
*dmt
= NULL
;
566 struct dm_deps
*deps
= NULL
;
567 const char *name
= NULL
;
568 const char *uuid
= NULL
;
569 struct dm_tree_node
*node
= NULL
;
573 /* Already in tree? */
574 if (!(node
= _find_dm_tree_node(dtree
, major
, minor
))) {
575 if (!_deps(&dmt
, dtree
->mem
, major
, minor
, &name
, &uuid
, &info
, &deps
))
578 if (!(node
= _create_dm_tree_node(dtree
, name
, uuid
, &info
,
584 if (!_link_tree_nodes(parent
, node
)) {
589 /* If node was already in tree, no need to recurse. */
593 /* Can't recurse if not a mapped device or there are no dependencies */
594 if (!node
->info
.exists
|| !deps
->count
) {
595 if (!_add_to_bottomlevel(node
)) {
602 /* Add dependencies to tree */
603 for (i
= 0; i
< deps
->count
; i
++)
604 if (!_add_dev(dtree
, node
, MAJOR(deps
->device
[i
]),
605 MINOR(deps
->device
[i
]), udev_flags
)) {
612 dm_task_destroy(dmt
);
617 static int _node_clear_table(struct dm_tree_node
*dnode
)
620 struct dm_info
*info
;
624 if (!(info
= &dnode
->info
)) {
625 log_error("_node_clear_table failed: missing info");
629 if (!(name
= dm_tree_node_get_name(dnode
))) {
630 log_error("_node_clear_table failed: missing name");
634 /* Is there a table? */
635 if (!info
->exists
|| !info
->inactive_table
)
638 // FIXME Get inactive deps. If any dev referenced has 1 opener and no live table, remove it after the clear.
640 log_verbose("Clearing inactive table %s (%" PRIu32
":%" PRIu32
")",
641 name
, info
->major
, info
->minor
);
643 if (!(dmt
= dm_task_create(DM_DEVICE_CLEAR
))) {
644 log_error("Table clear dm_task creation failed for %s", name
);
648 if (!dm_task_set_major(dmt
, info
->major
) ||
649 !dm_task_set_minor(dmt
, info
->minor
)) {
650 log_error("Failed to set device number for %s table clear", name
);
651 dm_task_destroy(dmt
);
655 r
= dm_task_run(dmt
);
657 if (!dm_task_get_info(dmt
, info
)) {
658 log_error("_node_clear_table failed: info missing after running task for %s", name
);
662 dm_task_destroy(dmt
);
667 struct dm_tree_node
*dm_tree_add_new_dev(struct dm_tree
*dtree
,
670 uint32_t major
, uint32_t minor
,
675 struct dm_tree_node
*dnode
;
680 /* Do we need to add node to tree? */
681 if (!(dnode
= dm_tree_find_node_by_uuid(dtree
, uuid
))) {
682 if (!(name2
= dm_pool_strdup(dtree
->mem
, name
))) {
683 log_error("name pool_strdup failed");
686 if (!(uuid2
= dm_pool_strdup(dtree
->mem
, uuid
))) {
687 log_error("uuid pool_strdup failed");
695 info
.inactive_table
= 0;
698 if (!(dnode
= _create_dm_tree_node(dtree
, name2
, uuid2
, &info
,
702 /* Attach to root node until a table is supplied */
703 if (!_add_to_toplevel(dnode
) || !_add_to_bottomlevel(dnode
))
706 dnode
->props
.major
= major
;
707 dnode
->props
.minor
= minor
;
708 dnode
->props
.new_name
= NULL
;
709 dnode
->props
.size_changed
= 0;
710 } else if (strcmp(name
, dnode
->name
)) {
711 /* Do we need to rename node? */
712 if (!(dnode
->props
.new_name
= dm_pool_strdup(dtree
->mem
, name
))) {
713 log_error("name pool_strdup failed");
718 dnode
->props
.read_only
= read_only
? 1 : 0;
719 dnode
->props
.read_ahead
= DM_READ_AHEAD_AUTO
;
720 dnode
->props
.read_ahead_flags
= 0;
722 if (clear_inactive
&& !_node_clear_table(dnode
))
725 dnode
->context
= context
;
726 dnode
->udev_flags
= 0;
731 struct dm_tree_node
*dm_tree_add_new_dev_with_udev_flags(struct dm_tree
*dtree
,
741 struct dm_tree_node
*node
;
743 if ((node
= dm_tree_add_new_dev(dtree
, name
, uuid
, major
, minor
, read_only
,
744 clear_inactive
, context
)))
745 node
->udev_flags
= udev_flags
;
751 void dm_tree_node_set_read_ahead(struct dm_tree_node
*dnode
,
753 uint32_t read_ahead_flags
)
755 dnode
->props
.read_ahead
= read_ahead
;
756 dnode
->props
.read_ahead_flags
= read_ahead_flags
;
759 void dm_tree_node_set_presuspend_node(struct dm_tree_node
*node
,
760 struct dm_tree_node
*presuspend_node
)
762 node
->presuspend_node
= presuspend_node
;
765 int dm_tree_add_dev(struct dm_tree
*dtree
, uint32_t major
, uint32_t minor
)
767 return _add_dev(dtree
, &dtree
->root
, major
, minor
, 0) ? 1 : 0;
770 int dm_tree_add_dev_with_udev_flags(struct dm_tree
*dtree
, uint32_t major
,
771 uint32_t minor
, uint16_t udev_flags
)
773 return _add_dev(dtree
, &dtree
->root
, major
, minor
, udev_flags
) ? 1 : 0;
776 const char *dm_tree_node_get_name(const struct dm_tree_node
*node
)
778 return node
->info
.exists
? node
->name
: "";
781 const char *dm_tree_node_get_uuid(const struct dm_tree_node
*node
)
783 return node
->info
.exists
? node
->uuid
: "";
786 const struct dm_info
*dm_tree_node_get_info(const struct dm_tree_node
*node
)
791 void *dm_tree_node_get_context(const struct dm_tree_node
*node
)
793 return node
->context
;
796 int dm_tree_node_size_changed(const struct dm_tree_node
*dnode
)
798 return dnode
->props
.size_changed
;
801 int dm_tree_node_num_children(const struct dm_tree_node
*node
, uint32_t inverted
)
804 if (_nodes_are_linked(&node
->dtree
->root
, node
))
806 return dm_list_size(&node
->used_by
);
809 if (_nodes_are_linked(node
, &node
->dtree
->root
))
812 return dm_list_size(&node
->uses
);
816 * Returns 1 if no prefix supplied
818 static int _uuid_prefix_matches(const char *uuid
, const char *uuid_prefix
, size_t uuid_prefix_len
)
823 if (!strncmp(uuid
, uuid_prefix
, uuid_prefix_len
))
826 /* Handle transition: active device uuids might be missing the prefix */
827 if (uuid_prefix_len
<= 4)
830 if (!strncmp(uuid
, UUID_PREFIX
, sizeof(UUID_PREFIX
) - 1))
833 if (strncmp(uuid_prefix
, UUID_PREFIX
, sizeof(UUID_PREFIX
) - 1))
836 if (!strncmp(uuid
, uuid_prefix
+ sizeof(UUID_PREFIX
) - 1, uuid_prefix_len
- (sizeof(UUID_PREFIX
) - 1)))
843 * Returns 1 if no children.
845 static int _children_suspended(struct dm_tree_node
*node
,
847 const char *uuid_prefix
,
848 size_t uuid_prefix_len
)
850 struct dm_list
*list
;
851 struct dm_tree_link
*dlink
;
852 const struct dm_info
*dinfo
;
856 if (_nodes_are_linked(&node
->dtree
->root
, node
))
858 list
= &node
->used_by
;
860 if (_nodes_are_linked(node
, &node
->dtree
->root
))
865 dm_list_iterate_items(dlink
, list
) {
866 if (!(uuid
= dm_tree_node_get_uuid(dlink
->node
))) {
871 /* Ignore if it doesn't belong to this VG */
872 if (!_uuid_prefix_matches(uuid
, uuid_prefix
, uuid_prefix_len
))
875 /* Ignore if parent node wants to presuspend this node */
876 if (dlink
->node
->presuspend_node
== node
)
879 if (!(dinfo
= dm_tree_node_get_info(dlink
->node
))) {
880 stack
; /* FIXME Is this normal? */
884 if (!dinfo
->suspended
)
892 * Set major and minor to zero for root of tree.
894 struct dm_tree_node
*dm_tree_find_node(struct dm_tree
*dtree
,
898 if (!major
&& !minor
)
901 return _find_dm_tree_node(dtree
, major
, minor
);
905 * Set uuid to NULL for root of tree.
907 struct dm_tree_node
*dm_tree_find_node_by_uuid(struct dm_tree
*dtree
,
913 return _find_dm_tree_node_by_uuid(dtree
, uuid
);
917 * First time set *handle to NULL.
918 * Set inverted to invert the tree.
920 struct dm_tree_node
*dm_tree_next_child(void **handle
,
921 const struct dm_tree_node
*parent
,
924 struct dm_list
**dlink
= (struct dm_list
**) handle
;
925 const struct dm_list
*use_list
;
928 use_list
= &parent
->used_by
;
930 use_list
= &parent
->uses
;
933 *dlink
= dm_list_first(use_list
);
935 *dlink
= dm_list_next(use_list
, *dlink
);
937 return (*dlink
) ? dm_list_item(*dlink
, struct dm_tree_link
)->node
: NULL
;
941 * Deactivate a device with its dependencies if the uuid prefix matches.
943 static int _info_by_dev(uint32_t major
, uint32_t minor
, int with_open_count
,
944 struct dm_info
*info
)
949 if (!(dmt
= dm_task_create(DM_DEVICE_INFO
))) {
950 log_error("_info_by_dev: dm_task creation failed");
954 if (!dm_task_set_major(dmt
, major
) || !dm_task_set_minor(dmt
, minor
)) {
955 log_error("_info_by_dev: Failed to set device number");
956 dm_task_destroy(dmt
);
960 if (!with_open_count
&& !dm_task_no_open_count(dmt
))
961 log_error("Failed to disable open_count");
963 if ((r
= dm_task_run(dmt
)))
964 r
= dm_task_get_info(dmt
, info
);
966 dm_task_destroy(dmt
);
971 static int _check_device_not_in_use(struct dm_info
*info
)
976 /* If sysfs is not used, use open_count information only. */
977 if (!*dm_sysfs_dir()) {
978 if (info
->open_count
) {
979 log_error("Device %" PRIu32
":%" PRIu32
" in use",
980 info
->major
, info
->minor
);
987 if (dm_device_has_holders(info
->major
, info
->minor
)) {
988 log_error("Device %" PRIu32
":%" PRIu32
" is used "
989 "by another device.", info
->major
, info
->minor
);
993 if (dm_device_has_mounted_fs(info
->major
, info
->minor
)) {
994 log_error("Device %" PRIu32
":%" PRIu32
" contains "
995 "a filesystem in use.", info
->major
, info
->minor
);
1002 /* Check if all parent nodes of given node have open_count == 0 */
1003 static int _node_has_closed_parents(struct dm_tree_node
*node
,
1004 const char *uuid_prefix
,
1005 size_t uuid_prefix_len
)
1007 struct dm_tree_link
*dlink
;
1008 const struct dm_info
*dinfo
;
1009 struct dm_info info
;
1012 /* Iterate through parents of this node */
1013 dm_list_iterate_items(dlink
, &node
->used_by
) {
1014 if (!(uuid
= dm_tree_node_get_uuid(dlink
->node
))) {
1019 /* Ignore if it doesn't belong to this VG */
1020 if (!_uuid_prefix_matches(uuid
, uuid_prefix
, uuid_prefix_len
))
1023 if (!(dinfo
= dm_tree_node_get_info(dlink
->node
))) {
1024 stack
; /* FIXME Is this normal? */
1028 /* Refresh open_count */
1029 if (!_info_by_dev(dinfo
->major
, dinfo
->minor
, 1, &info
) ||
1033 if (info
.open_count
) {
1034 log_debug("Node %s %d:%d has open_count %d", uuid_prefix
,
1035 dinfo
->major
, dinfo
->minor
, info
.open_count
);
1043 static int _deactivate_node(const char *name
, uint32_t major
, uint32_t minor
,
1044 uint32_t *cookie
, uint16_t udev_flags
, int retry
)
1046 struct dm_task
*dmt
;
1049 log_verbose("Removing %s (%" PRIu32
":%" PRIu32
")", name
, major
, minor
);
1051 if (!(dmt
= dm_task_create(DM_DEVICE_REMOVE
))) {
1052 log_error("Deactivation dm_task creation failed for %s", name
);
1056 if (!dm_task_set_major(dmt
, major
) || !dm_task_set_minor(dmt
, minor
)) {
1057 log_error("Failed to set device number for %s deactivation", name
);
1061 if (!dm_task_no_open_count(dmt
))
1062 log_error("Failed to disable open_count");
1064 if (!dm_task_set_cookie(dmt
, cookie
, udev_flags
))
1069 dm_task_retry_remove(dmt
);
1071 r
= dm_task_run(dmt
);
1073 /* FIXME Until kernel returns actual name so dm-iface.c can handle it */
1074 rm_dev_node(name
, dmt
->cookie_set
&& !(udev_flags
& DM_UDEV_DISABLE_DM_RULES_FLAG
),
1075 dmt
->cookie_set
&& (udev_flags
& DM_UDEV_DISABLE_LIBRARY_FALLBACK
));
1077 /* FIXME Remove node from tree or mark invalid? */
1080 dm_task_destroy(dmt
);
1085 static int _rename_node(const char *old_name
, const char *new_name
, uint32_t major
,
1086 uint32_t minor
, uint32_t *cookie
, uint16_t udev_flags
)
1088 struct dm_task
*dmt
;
1091 log_verbose("Renaming %s (%" PRIu32
":%" PRIu32
") to %s", old_name
, major
, minor
, new_name
);
1093 if (!(dmt
= dm_task_create(DM_DEVICE_RENAME
))) {
1094 log_error("Rename dm_task creation failed for %s", old_name
);
1098 if (!dm_task_set_name(dmt
, old_name
)) {
1099 log_error("Failed to set name for %s rename.", old_name
);
1103 if (!dm_task_set_newname(dmt
, new_name
))
1106 if (!dm_task_no_open_count(dmt
))
1107 log_error("Failed to disable open_count");
1109 if (!dm_task_set_cookie(dmt
, cookie
, udev_flags
))
1112 r
= dm_task_run(dmt
);
1115 dm_task_destroy(dmt
);
1120 /* FIXME Merge with _suspend_node? */
1121 static int _resume_node(const char *name
, uint32_t major
, uint32_t minor
,
1122 uint32_t read_ahead
, uint32_t read_ahead_flags
,
1123 struct dm_info
*newinfo
, uint32_t *cookie
,
1124 uint16_t udev_flags
, int already_suspended
)
1126 struct dm_task
*dmt
;
1129 log_verbose("Resuming %s (%" PRIu32
":%" PRIu32
")", name
, major
, minor
);
1131 if (!(dmt
= dm_task_create(DM_DEVICE_RESUME
))) {
1132 log_debug("Suspend dm_task creation failed for %s.", name
);
1136 /* FIXME Kernel should fill in name on return instead */
1137 if (!dm_task_set_name(dmt
, name
)) {
1138 log_debug("Failed to set device name for %s resumption.", name
);
1142 if (!dm_task_set_major(dmt
, major
) || !dm_task_set_minor(dmt
, minor
)) {
1143 log_error("Failed to set device number for %s resumption.", name
);
1147 if (!dm_task_no_open_count(dmt
))
1148 log_error("Failed to disable open_count");
1150 if (!dm_task_set_read_ahead(dmt
, read_ahead
, read_ahead_flags
))
1151 log_error("Failed to set read ahead");
1153 if (!dm_task_set_cookie(dmt
, cookie
, udev_flags
))
1156 if (!(r
= dm_task_run(dmt
)))
1159 if (already_suspended
)
1162 if (!(r
= dm_task_get_info(dmt
, newinfo
)))
1166 dm_task_destroy(dmt
);
1171 static int _suspend_node(const char *name
, uint32_t major
, uint32_t minor
,
1172 int skip_lockfs
, int no_flush
, struct dm_info
*newinfo
)
1174 struct dm_task
*dmt
;
1177 log_verbose("Suspending %s (%" PRIu32
":%" PRIu32
")%s%s",
1179 skip_lockfs
? "" : " with filesystem sync",
1180 no_flush
? "" : " with device flush");
1182 if (!(dmt
= dm_task_create(DM_DEVICE_SUSPEND
))) {
1183 log_error("Suspend dm_task creation failed for %s", name
);
1187 if (!dm_task_set_major(dmt
, major
) || !dm_task_set_minor(dmt
, minor
)) {
1188 log_error("Failed to set device number for %s suspension.", name
);
1189 dm_task_destroy(dmt
);
1193 if (!dm_task_no_open_count(dmt
))
1194 log_error("Failed to disable open_count");
1196 if (skip_lockfs
&& !dm_task_skip_lockfs(dmt
))
1197 log_error("Failed to set skip_lockfs flag.");
1199 if (no_flush
&& !dm_task_no_flush(dmt
))
1200 log_error("Failed to set no_flush flag.");
1202 if ((r
= dm_task_run(dmt
))) {
1204 r
= dm_task_get_info(dmt
, newinfo
);
1207 dm_task_destroy(dmt
);
1212 static int _check_thin_pool_transaction_id(const char *name
, uint32_t major
, uint32_t minor
,
1213 uint64_t transaction_id
)
1215 struct dm_task
*dmt
;
1217 uint64_t start
, length
;
1219 char *params
= NULL
;
1220 uint64_t t_id
= transaction_id
; // FIXME: fake
1222 log_verbose("Checking transaction id %s (%" PRIu32
":%" PRIu32
")", name
, major
, minor
);
1224 if (!(dmt
= dm_task_create(DM_DEVICE_STATUS
))) {
1225 log_debug("Device status dm_task creation failed for %s.", name
);
1229 if (!dm_task_set_name(dmt
, name
)) {
1230 log_debug("Failed to set device name for %s status.", name
);
1234 if (!dm_task_set_major_minor(dmt
, major
, minor
, 1)) {
1235 log_error("Failed to set device number for %s status.", name
);
1239 if (!dm_task_no_open_count(dmt
))
1240 log_error("Failed to disable open_count");
1242 if (!(r
= dm_task_run(dmt
)))
1245 dm_get_next_target(dmt
, NULL
, &start
, &length
, &type
, ¶ms
);
1246 log_verbose("PARSE params %s", params
); // FIXME: parse status
1248 r
= (transaction_id
== t_id
);
1251 dm_task_destroy(dmt
);
1257 * FIXME Don't attempt to deactivate known internal dependencies.
1259 static int _dm_tree_deactivate_children(struct dm_tree_node
*dnode
,
1260 const char *uuid_prefix
,
1261 size_t uuid_prefix_len
,
1265 void *handle
= NULL
;
1266 struct dm_tree_node
*child
= dnode
;
1267 struct dm_info info
;
1268 const struct dm_info
*dinfo
;
1272 while ((child
= dm_tree_next_child(&handle
, dnode
, 0))) {
1273 if (!(dinfo
= dm_tree_node_get_info(child
))) {
1278 if (!(name
= dm_tree_node_get_name(child
))) {
1283 if (!(uuid
= dm_tree_node_get_uuid(child
))) {
1288 /* Ignore if it doesn't belong to this VG */
1289 if (!_uuid_prefix_matches(uuid
, uuid_prefix
, uuid_prefix_len
))
1292 /* Refresh open_count */
1293 if (!_info_by_dev(dinfo
->major
, dinfo
->minor
, 1, &info
) ||
1297 if (!_check_device_not_in_use(&info
))
1300 /* Also checking open_count in parent nodes of presuspend_node */
1301 if ((child
->presuspend_node
&&
1302 !_node_has_closed_parents(child
->presuspend_node
,
1303 uuid_prefix
, uuid_prefix_len
))) {
1304 /* Only report error from (likely non-internal) dependency at top level */
1306 log_error("Unable to deactivate open %s (%" PRIu32
1307 ":%" PRIu32
")", name
, info
.major
,
1314 /* Suspend child node first if requested */
1315 if (child
->presuspend_node
&&
1316 !dm_tree_suspend_children(child
, uuid_prefix
, uuid_prefix_len
))
1319 if (!_deactivate_node(name
, info
.major
, info
.minor
,
1320 &child
->dtree
->cookie
, child
->udev_flags
,
1321 child
->dtree
->retry_remove
)) {
1322 log_error("Unable to deactivate %s (%" PRIu32
1323 ":%" PRIu32
")", name
, info
.major
,
1327 } else if (info
.suspended
)
1330 if (dm_tree_node_num_children(child
, 0)) {
1331 if (!_dm_tree_deactivate_children(child
, uuid_prefix
, uuid_prefix_len
, level
+ 1))
1339 int dm_tree_deactivate_children(struct dm_tree_node
*dnode
,
1340 const char *uuid_prefix
,
1341 size_t uuid_prefix_len
)
1343 return _dm_tree_deactivate_children(dnode
, uuid_prefix
, uuid_prefix_len
, 0);
1346 void dm_tree_skip_lockfs(struct dm_tree_node
*dnode
)
1348 dnode
->dtree
->skip_lockfs
= 1;
1351 void dm_tree_use_no_flush_suspend(struct dm_tree_node
*dnode
)
1353 dnode
->dtree
->no_flush
= 1;
1356 void dm_tree_retry_remove(struct dm_tree_node
*dnode
)
1358 dnode
->dtree
->retry_remove
= 1;
1361 int dm_tree_suspend_children(struct dm_tree_node
*dnode
,
1362 const char *uuid_prefix
,
1363 size_t uuid_prefix_len
)
1366 void *handle
= NULL
;
1367 struct dm_tree_node
*child
= dnode
;
1368 struct dm_info info
, newinfo
;
1369 const struct dm_info
*dinfo
;
1373 /* Suspend nodes at this level of the tree */
1374 while ((child
= dm_tree_next_child(&handle
, dnode
, 0))) {
1375 if (!(dinfo
= dm_tree_node_get_info(child
))) {
1380 if (!(name
= dm_tree_node_get_name(child
))) {
1385 if (!(uuid
= dm_tree_node_get_uuid(child
))) {
1390 /* Ignore if it doesn't belong to this VG */
1391 if (!_uuid_prefix_matches(uuid
, uuid_prefix
, uuid_prefix_len
))
1394 /* Ensure immediate parents are already suspended */
1395 if (!_children_suspended(child
, 1, uuid_prefix
, uuid_prefix_len
))
1398 if (!_info_by_dev(dinfo
->major
, dinfo
->minor
, 0, &info
) ||
1399 !info
.exists
|| info
.suspended
)
1402 if (!_suspend_node(name
, info
.major
, info
.minor
,
1403 child
->dtree
->skip_lockfs
,
1404 child
->dtree
->no_flush
, &newinfo
)) {
1405 log_error("Unable to suspend %s (%" PRIu32
1406 ":%" PRIu32
")", name
, info
.major
,
1412 /* Update cached info */
1413 child
->info
= newinfo
;
1416 /* Then suspend any child nodes */
1419 while ((child
= dm_tree_next_child(&handle
, dnode
, 0))) {
1420 if (!(uuid
= dm_tree_node_get_uuid(child
))) {
1425 /* Ignore if it doesn't belong to this VG */
1426 if (!_uuid_prefix_matches(uuid
, uuid_prefix
, uuid_prefix_len
))
1429 if (dm_tree_node_num_children(child
, 0))
1430 if (!dm_tree_suspend_children(child
, uuid_prefix
, uuid_prefix_len
))
1437 int dm_tree_activate_children(struct dm_tree_node
*dnode
,
1438 const char *uuid_prefix
,
1439 size_t uuid_prefix_len
)
1442 void *handle
= NULL
;
1443 struct dm_tree_node
*child
= dnode
;
1444 struct dm_info newinfo
;
1449 /* Activate children first */
1450 while ((child
= dm_tree_next_child(&handle
, dnode
, 0))) {
1451 if (!(uuid
= dm_tree_node_get_uuid(child
))) {
1456 if (!_uuid_prefix_matches(uuid
, uuid_prefix
, uuid_prefix_len
))
1459 if (dm_tree_node_num_children(child
, 0))
1460 if (!dm_tree_activate_children(child
, uuid_prefix
, uuid_prefix_len
))
1466 for (priority
= 0; priority
< 3; priority
++) {
1467 while ((child
= dm_tree_next_child(&handle
, dnode
, 0))) {
1468 if (priority
!= child
->activation_priority
)
1471 if (!(uuid
= dm_tree_node_get_uuid(child
))) {
1476 if (!_uuid_prefix_matches(uuid
, uuid_prefix
, uuid_prefix_len
))
1479 if (!(name
= dm_tree_node_get_name(child
))) {
1485 if (child
->props
.new_name
) {
1486 if (!_rename_node(name
, child
->props
.new_name
, child
->info
.major
,
1487 child
->info
.minor
, &child
->dtree
->cookie
,
1488 child
->udev_flags
)) {
1489 log_error("Failed to rename %s (%" PRIu32
1490 ":%" PRIu32
") to %s", name
, child
->info
.major
,
1491 child
->info
.minor
, child
->props
.new_name
);
1494 child
->name
= child
->props
.new_name
;
1495 child
->props
.new_name
= NULL
;
1498 if (!child
->info
.inactive_table
&& !child
->info
.suspended
)
1501 if (!_resume_node(child
->name
, child
->info
.major
, child
->info
.minor
,
1502 child
->props
.read_ahead
, child
->props
.read_ahead_flags
,
1503 &newinfo
, &child
->dtree
->cookie
, child
->udev_flags
, child
->info
.suspended
)) {
1504 log_error("Unable to resume %s (%" PRIu32
1505 ":%" PRIu32
")", child
->name
, child
->info
.major
,
1511 /* Update cached info */
1512 child
->info
= newinfo
;
1514 /* FIXME: trial version - to avoid use of unsynchronized thin_pool transaction_id */
1515 if (child
->props
.thin_pool_transaction_id
&&
1516 !_check_thin_pool_transaction_id(child
->name
, child
->info
.major
,
1518 child
->props
.thin_pool_transaction_id
)) {
1520 if (!(dm_tree_deactivate_children(child
, uuid_prefix
, uuid_prefix_len
)))
1521 log_error("Failed to deactivate %s", child
->name
);
1533 static int _create_node(struct dm_tree_node
*dnode
)
1536 struct dm_task
*dmt
;
1538 log_verbose("Creating %s", dnode
->name
);
1540 if (!(dmt
= dm_task_create(DM_DEVICE_CREATE
))) {
1541 log_error("Create dm_task creation failed for %s", dnode
->name
);
1545 if (!dm_task_set_name(dmt
, dnode
->name
)) {
1546 log_error("Failed to set device name for %s", dnode
->name
);
1550 if (!dm_task_set_uuid(dmt
, dnode
->uuid
)) {
1551 log_error("Failed to set uuid for %s", dnode
->name
);
1555 if (dnode
->props
.major
&&
1556 (!dm_task_set_major(dmt
, dnode
->props
.major
) ||
1557 !dm_task_set_minor(dmt
, dnode
->props
.minor
))) {
1558 log_error("Failed to set device number for %s creation.", dnode
->name
);
1562 if (dnode
->props
.read_only
&& !dm_task_set_ro(dmt
)) {
1563 log_error("Failed to set read only flag for %s", dnode
->name
);
1567 if (!dm_task_no_open_count(dmt
))
1568 log_error("Failed to disable open_count");
1570 if ((r
= dm_task_run(dmt
)))
1571 r
= dm_task_get_info(dmt
, &dnode
->info
);
1574 dm_task_destroy(dmt
);
1580 static int _build_dev_string(char *devbuf
, size_t bufsize
, struct dm_tree_node
*node
)
1582 if (!dm_format_dev(devbuf
, bufsize
, node
->info
.major
, node
->info
.minor
)) {
1583 log_error("Failed to format %s device number for %s as dm "
1585 node
->name
, node
->uuid
, node
->info
.major
, node
->info
.minor
);
1592 /* simplify string emiting code */
1593 #define EMIT_PARAMS(p, str...)\
1596 if ((w = dm_snprintf(params + p, paramsize - (size_t) p, str)) < 0) {\
1597 stack; /* Out of space */\
1606 * Returns: 1 on success, 0 on failure
1608 static int _emit_areas_line(struct dm_task
*dmt
__attribute__((unused
)),
1609 struct load_segment
*seg
, char *params
,
1610 size_t paramsize
, int *pos
)
1612 struct seg_area
*area
;
1613 char devbuf
[DM_FORMAT_DEV_BUFSIZE
];
1614 unsigned first_time
= 1;
1615 const char *logtype
, *synctype
;
1616 unsigned log_parm_count
;
1618 dm_list_iterate_items(area
, &seg
->areas
) {
1619 switch (seg
->type
) {
1620 case SEG_REPLICATOR_DEV
:
1621 if (!_build_dev_string(devbuf
, sizeof(devbuf
), area
->dev_node
))
1624 EMIT_PARAMS(*pos
, " %d 1 %s", area
->rsite_index
, devbuf
);
1626 EMIT_PARAMS(*pos
, " nolog 0");
1628 /* Remote devices */
1629 log_parm_count
= (area
->flags
&
1630 (DM_NOSYNC
| DM_FORCESYNC
)) ? 2 : 1;
1633 devbuf
[0] = 0; /* Only core log parameters */
1636 devbuf
[0] = ' '; /* Extra space before device name */
1637 if (!_build_dev_string(devbuf
+ 1,
1642 log_parm_count
++; /* Extra sync log device name parameter */
1645 EMIT_PARAMS(*pos
, " %s %u%s %" PRIu64
, logtype
,
1646 log_parm_count
, devbuf
, area
->region_size
);
1648 synctype
= (area
->flags
& DM_NOSYNC
) ?
1649 " nosync" : (area
->flags
& DM_FORCESYNC
) ?
1653 EMIT_PARAMS(*pos
, "%s", synctype
);
1665 if (!area
->dev_node
) {
1666 EMIT_PARAMS(*pos
, " -");
1669 if (!_build_dev_string(devbuf
, sizeof(devbuf
), area
->dev_node
))
1672 EMIT_PARAMS(*pos
, " %s", devbuf
);
1675 if (!_build_dev_string(devbuf
, sizeof(devbuf
), area
->dev_node
))
1678 EMIT_PARAMS(*pos
, "%s%s %" PRIu64
, first_time
? "" : " ",
1679 devbuf
, area
->offset
);
1688 static int _replicator_emit_segment_line(const struct load_segment
*seg
, char *params
,
1689 size_t paramsize
, int *pos
)
1691 const struct load_segment
*rlog_seg
;
1692 struct replicator_site
*rsite
;
1693 char rlogbuf
[DM_FORMAT_DEV_BUFSIZE
];
1694 unsigned parm_count
;
1696 if (!seg
->log
|| !_build_dev_string(rlogbuf
, sizeof(rlogbuf
), seg
->log
))
1699 rlog_seg
= dm_list_item(dm_list_last(&seg
->log
->props
.segs
),
1700 struct load_segment
);
1702 EMIT_PARAMS(*pos
, "%s 4 %s 0 auto %" PRIu64
,
1703 seg
->rlog_type
, rlogbuf
, rlog_seg
->size
);
1705 dm_list_iterate_items(rsite
, &seg
->rsites
) {
1706 parm_count
= (rsite
->fall_behind_data
1707 || rsite
->fall_behind_ios
1708 || rsite
->async_timeout
) ? 4 : 2;
1710 EMIT_PARAMS(*pos
, " blockdev %u %u %s", parm_count
, rsite
->rsite_index
,
1711 (rsite
->mode
== DM_REPLICATOR_SYNC
) ? "synchronous" : "asynchronous");
1713 if (rsite
->fall_behind_data
)
1714 EMIT_PARAMS(*pos
, " data %" PRIu64
, rsite
->fall_behind_data
);
1715 else if (rsite
->fall_behind_ios
)
1716 EMIT_PARAMS(*pos
, " ios %" PRIu32
, rsite
->fall_behind_ios
);
1717 else if (rsite
->async_timeout
)
1718 EMIT_PARAMS(*pos
, " timeout %" PRIu32
, rsite
->async_timeout
);
1725 * Returns: 1 on success, 0 on failure
1727 static int _mirror_emit_segment_line(struct dm_task
*dmt
, struct load_segment
*seg
,
1728 char *params
, size_t paramsize
)
1730 int block_on_error
= 0;
1731 int handle_errors
= 0;
1732 int dm_log_userspace
= 0;
1734 unsigned log_parm_count
;
1736 char logbuf
[DM_FORMAT_DEV_BUFSIZE
];
1737 const char *logtype
;
1738 unsigned kmaj
= 0, kmin
= 0, krel
= 0;
1740 if (uname(&uts
) == -1) {
1741 log_error("Cannot read kernel release version.");
1745 /* Kernels with a major number of 2 always had 3 parts. */
1746 parts
= sscanf(uts
.release
, "%u.%u.%u", &kmaj
, &kmin
, &krel
);
1747 if (parts
< 1 || (kmaj
< 3 && parts
< 3)) {
1748 log_error("Wrong kernel release version %s.", uts
.release
);
1752 if ((seg
->flags
& DM_BLOCK_ON_ERROR
)) {
1754 * Originally, block_on_error was an argument to the log
1755 * portion of the mirror CTR table. It was renamed to
1756 * "handle_errors" and now resides in the 'features'
1757 * section of the mirror CTR table (i.e. at the end).
1759 * We can identify whether to use "block_on_error" or
1760 * "handle_errors" by the dm-mirror module's version
1761 * number (>= 1.12) or by the kernel version (>= 2.6.22).
1763 if (KERNEL_VERSION(kmaj
, kmin
, krel
) >= KERNEL_VERSION(2, 6, 22))
1769 if (seg
->clustered
) {
1770 /* Cluster mirrors require a UUID */
1775 * Cluster mirrors used to have their own log
1776 * types. Now they are accessed through the
1777 * userspace log type.
1779 * The dm-log-userspace module was added to the
1782 if (KERNEL_VERSION(kmaj
, kmin
, krel
) >= KERNEL_VERSION(2, 6, 31))
1783 dm_log_userspace
= 1;
1789 /* [no]sync, block_on_error etc. */
1790 log_parm_count
+= hweight32(seg
->flags
);
1792 /* "handle_errors" is a feature arg now */
1796 /* DM_CORELOG does not count in the param list */
1797 if (seg
->flags
& DM_CORELOG
)
1800 if (seg
->clustered
) {
1801 log_parm_count
++; /* For UUID */
1803 if (!dm_log_userspace
)
1804 EMIT_PARAMS(pos
, "clustered-");
1806 /* For clustered-* type field inserted later */
1815 if (!_build_dev_string(logbuf
, sizeof(logbuf
), seg
->log
))
1819 if (dm_log_userspace
)
1820 EMIT_PARAMS(pos
, "userspace %u %s clustered-%s",
1821 log_parm_count
, seg
->uuid
, logtype
);
1823 EMIT_PARAMS(pos
, "%s %u", logtype
, log_parm_count
);
1826 EMIT_PARAMS(pos
, " %s", logbuf
);
1828 EMIT_PARAMS(pos
, " %u", seg
->region_size
);
1830 if (seg
->clustered
&& !dm_log_userspace
)
1831 EMIT_PARAMS(pos
, " %s", seg
->uuid
);
1833 if ((seg
->flags
& DM_NOSYNC
))
1834 EMIT_PARAMS(pos
, " nosync");
1835 else if ((seg
->flags
& DM_FORCESYNC
))
1836 EMIT_PARAMS(pos
, " sync");
1839 EMIT_PARAMS(pos
, " block_on_error");
1841 EMIT_PARAMS(pos
, " %u ", seg
->mirror_area_count
);
1843 if (_emit_areas_line(dmt
, seg
, params
, paramsize
, &pos
) <= 0)
1847 EMIT_PARAMS(pos
, " 1 handle_errors");
1852 static int _raid_emit_segment_line(struct dm_task
*dmt
, uint32_t major
,
1853 uint32_t minor
, struct load_segment
*seg
,
1854 uint64_t *seg_start
, char *params
,
1858 int param_count
= 1; /* mandatory 'chunk size'/'stripe size' arg */
1861 if ((seg
->flags
& DM_NOSYNC
) || (seg
->flags
& DM_FORCESYNC
))
1864 if (seg
->region_size
)
1867 tmp
= (uint32_t *)(&seg
->rebuilds
); /* rebuilds is 64-bit */
1868 param_count
+= 2 * hweight32(tmp
[0]);
1869 param_count
+= 2 * hweight32(tmp
[1]);
1871 if ((seg
->type
== SEG_RAID1
) && seg
->stripe_size
)
1872 log_error("WARNING: Ignoring RAID1 stripe size");
1874 EMIT_PARAMS(pos
, "%s %d %u", dm_segtypes
[seg
->type
].target
,
1875 param_count
, seg
->stripe_size
);
1877 if (seg
->flags
& DM_NOSYNC
)
1878 EMIT_PARAMS(pos
, " nosync");
1879 else if (seg
->flags
& DM_FORCESYNC
)
1880 EMIT_PARAMS(pos
, " sync");
1882 if (seg
->region_size
)
1883 EMIT_PARAMS(pos
, " region_size %u", seg
->region_size
);
1885 for (i
= 0; i
< (seg
->area_count
/ 2); i
++)
1886 if (seg
->rebuilds
& (1 << i
))
1887 EMIT_PARAMS(pos
, " rebuild %u", i
);
1889 /* Print number of metadata/data device pairs */
1890 EMIT_PARAMS(pos
, " %u", seg
->area_count
/2);
1892 if (_emit_areas_line(dmt
, seg
, params
, paramsize
, &pos
) <= 0)
1898 static int _emit_segment_line(struct dm_task
*dmt
, uint32_t major
,
1899 uint32_t minor
, struct load_segment
*seg
,
1900 uint64_t *seg_start
, char *params
,
1905 int target_type_is_raid
= 0;
1906 char originbuf
[DM_FORMAT_DEV_BUFSIZE
], cowbuf
[DM_FORMAT_DEV_BUFSIZE
];
1907 char pool
[DM_FORMAT_DEV_BUFSIZE
], metadata
[DM_FORMAT_DEV_BUFSIZE
];
1915 /* Mirrors are pretty complicated - now in separate function */
1916 r
= _mirror_emit_segment_line(dmt
, seg
, params
, paramsize
);
1920 case SEG_REPLICATOR
:
1921 if ((r
= _replicator_emit_segment_line(seg
, params
, paramsize
,
1927 case SEG_REPLICATOR_DEV
:
1928 if (!seg
->replicator
|| !_build_dev_string(originbuf
,
1933 EMIT_PARAMS(pos
, "%s %" PRIu64
, originbuf
, seg
->rdevice_index
);
1936 case SEG_SNAPSHOT_MERGE
:
1937 if (!_build_dev_string(originbuf
, sizeof(originbuf
), seg
->origin
))
1939 if (!_build_dev_string(cowbuf
, sizeof(cowbuf
), seg
->cow
))
1941 EMIT_PARAMS(pos
, "%s %s %c %d", originbuf
, cowbuf
,
1942 seg
->persistent
? 'P' : 'N', seg
->chunk_size
);
1944 case SEG_SNAPSHOT_ORIGIN
:
1945 if (!_build_dev_string(originbuf
, sizeof(originbuf
), seg
->origin
))
1947 EMIT_PARAMS(pos
, "%s", originbuf
);
1950 EMIT_PARAMS(pos
, "%u %u ", seg
->area_count
, seg
->stripe_size
);
1953 EMIT_PARAMS(pos
, "%s%s%s%s%s %s %" PRIu64
" ", seg
->cipher
,
1954 seg
->chainmode
? "-" : "", seg
->chainmode
?: "",
1955 seg
->iv
? "-" : "", seg
->iv
?: "", seg
->key
,
1956 seg
->iv_offset
!= DM_CRYPT_IV_DEFAULT
?
1957 seg
->iv_offset
: *seg_start
);
1968 target_type_is_raid
= 1;
1969 r
= _raid_emit_segment_line(dmt
, major
, minor
, seg
, seg_start
,
1976 if (!_build_dev_string(metadata
, sizeof(metadata
), seg
->metadata
))
1978 if (!_build_dev_string(pool
, sizeof(pool
), seg
->pool
))
1980 EMIT_PARAMS(pos
, "%s %s %d %" PRIu64
" %s", metadata
, pool
,
1981 seg
->data_block_size
, seg
->low_water_mark_size
,
1982 seg
->skip_block_zeroing
? "1 skip_block_zeroing" : "");
1985 if (!_build_dev_string(pool
, sizeof(pool
), seg
->pool
))
1987 EMIT_PARAMS(pos
, "%s %d", pool
, seg
->device_id
);
1993 case SEG_REPLICATOR
:
1995 case SEG_SNAPSHOT_ORIGIN
:
1996 case SEG_SNAPSHOT_MERGE
:
2003 case SEG_REPLICATOR_DEV
:
2005 if ((r
= _emit_areas_line(dmt
, seg
, params
, paramsize
, &pos
)) <= 0) {
2010 log_error("No parameters supplied for %s target "
2011 "%u:%u.", dm_segtypes
[seg
->type
].target
,
2018 log_debug("Adding target to (%" PRIu32
":%" PRIu32
"): %" PRIu64
2019 " %" PRIu64
" %s %s", major
, minor
,
2020 *seg_start
, seg
->size
, target_type_is_raid
? "raid" :
2021 dm_segtypes
[seg
->type
].target
, params
);
2023 if (!dm_task_add_target(dmt
, *seg_start
, seg
->size
,
2024 target_type_is_raid
? "raid" :
2025 dm_segtypes
[seg
->type
].target
, params
))
2028 *seg_start
+= seg
->size
;
2035 static int _emit_segment(struct dm_task
*dmt
, uint32_t major
, uint32_t minor
,
2036 struct load_segment
*seg
, uint64_t *seg_start
)
2039 size_t paramsize
= 4096;
2043 if (!(params
= dm_malloc(paramsize
))) {
2044 log_error("Insufficient space for target parameters.");
2049 ret
= _emit_segment_line(dmt
, major
, minor
, seg
, seg_start
,
2059 log_debug("Insufficient space in params[%" PRIsize_t
2060 "] for target parameters.", paramsize
);
2063 } while (paramsize
< MAX_TARGET_PARAMSIZE
);
2065 log_error("Target parameter size too big. Aborting.");
2069 static int _load_node(struct dm_tree_node
*dnode
)
2072 struct dm_task
*dmt
;
2073 struct load_segment
*seg
;
2074 uint64_t seg_start
= 0, existing_table_size
;
2076 log_verbose("Loading %s table (%" PRIu32
":%" PRIu32
")", dnode
->name
,
2077 dnode
->info
.major
, dnode
->info
.minor
);
2079 if (!(dmt
= dm_task_create(DM_DEVICE_RELOAD
))) {
2080 log_error("Reload dm_task creation failed for %s", dnode
->name
);
2084 if (!dm_task_set_major(dmt
, dnode
->info
.major
) ||
2085 !dm_task_set_minor(dmt
, dnode
->info
.minor
)) {
2086 log_error("Failed to set device number for %s reload.", dnode
->name
);
2090 if (dnode
->props
.read_only
&& !dm_task_set_ro(dmt
)) {
2091 log_error("Failed to set read only flag for %s", dnode
->name
);
2095 if (!dm_task_no_open_count(dmt
))
2096 log_error("Failed to disable open_count");
2098 dm_list_iterate_items(seg
, &dnode
->props
.segs
)
2099 if (!_emit_segment(dmt
, dnode
->info
.major
, dnode
->info
.minor
,
2103 if (!dm_task_suppress_identical_reload(dmt
))
2104 log_error("Failed to suppress reload of identical tables.");
2106 if ((r
= dm_task_run(dmt
))) {
2107 r
= dm_task_get_info(dmt
, &dnode
->info
);
2108 if (r
&& !dnode
->info
.inactive_table
)
2109 log_verbose("Suppressed %s identical table reload.",
2112 existing_table_size
= dm_task_get_existing_table_size(dmt
);
2113 if ((dnode
->props
.size_changed
=
2114 (existing_table_size
== seg_start
) ? 0 : 1)) {
2115 log_debug("Table size changed from %" PRIu64
" to %"
2116 PRIu64
" for %s", existing_table_size
,
2117 seg_start
, dnode
->name
);
2119 * Kernel usually skips size validation on zero-length devices
2120 * now so no need to preload them.
2122 /* FIXME In which kernel version did this begin? */
2123 if (!existing_table_size
&& dnode
->props
.delay_resume_if_new
)
2124 dnode
->props
.size_changed
= 0;
2128 dnode
->props
.segment_count
= 0;
2131 dm_task_destroy(dmt
);
2136 int dm_tree_preload_children(struct dm_tree_node
*dnode
,
2137 const char *uuid_prefix
,
2138 size_t uuid_prefix_len
)
2141 void *handle
= NULL
;
2142 struct dm_tree_node
*child
;
2143 struct dm_info newinfo
;
2144 int update_devs_flag
= 0;
2146 /* Preload children first */
2147 while ((child
= dm_tree_next_child(&handle
, dnode
, 0))) {
2148 /* Skip existing non-device-mapper devices */
2149 if (!child
->info
.exists
&& child
->info
.major
)
2152 /* Ignore if it doesn't belong to this VG */
2153 if (child
->info
.exists
&&
2154 !_uuid_prefix_matches(child
->uuid
, uuid_prefix
, uuid_prefix_len
))
2157 if (dm_tree_node_num_children(child
, 0))
2158 if (!dm_tree_preload_children(child
, uuid_prefix
, uuid_prefix_len
))
2161 /* FIXME Cope if name exists with no uuid? */
2162 if (!child
->info
.exists
) {
2163 if (!_create_node(child
)) {
2169 if (!child
->info
.inactive_table
&& child
->props
.segment_count
) {
2170 if (!_load_node(child
)) {
2176 /* Propagate device size change change */
2177 if (child
->props
.size_changed
)
2178 dnode
->props
.size_changed
= 1;
2180 /* Resume device immediately if it has parents and its size changed */
2181 if (!dm_tree_node_num_children(child
, 1) || !child
->props
.size_changed
)
2184 if (!child
->info
.inactive_table
&& !child
->info
.suspended
)
2187 if (!_resume_node(child
->name
, child
->info
.major
, child
->info
.minor
,
2188 child
->props
.read_ahead
, child
->props
.read_ahead_flags
,
2189 &newinfo
, &child
->dtree
->cookie
, child
->udev_flags
,
2190 child
->info
.suspended
)) {
2191 log_error("Unable to resume %s (%" PRIu32
2192 ":%" PRIu32
")", child
->name
, child
->info
.major
,
2198 /* Update cached info */
2199 child
->info
= newinfo
;
2202 * Prepare for immediate synchronization with udev and flush all stacked
2203 * dev node operations if requested by immediate_dev_node property. But
2204 * finish processing current level in the tree first.
2206 if (child
->props
.immediate_dev_node
)
2207 update_devs_flag
= 1;
2209 /* FIXME: trial version - to avoid use of unsynchronized thin_pool transaction_id */
2210 if (child
->props
.thin_pool_transaction_id
&&
2211 !_check_thin_pool_transaction_id(child
->name
, child
->info
.major
,
2213 child
->props
.thin_pool_transaction_id
)) {
2215 if (!(dm_tree_deactivate_children(child
, uuid_prefix
, uuid_prefix_len
)))
2216 log_error("Failed to deactivate %s", child
->name
);
2224 if (update_devs_flag
) {
2225 if (!dm_udev_wait(dm_tree_get_cookie(dnode
)))
2227 dm_tree_set_cookie(dnode
, 0);
2234 * Returns 1 if unsure.
2236 int dm_tree_children_use_uuid(struct dm_tree_node
*dnode
,
2237 const char *uuid_prefix
,
2238 size_t uuid_prefix_len
)
2240 void *handle
= NULL
;
2241 struct dm_tree_node
*child
= dnode
;
2244 while ((child
= dm_tree_next_child(&handle
, dnode
, 0))) {
2245 if (!(uuid
= dm_tree_node_get_uuid(child
))) {
2246 log_error("Failed to get uuid for dtree node.");
2250 if (_uuid_prefix_matches(uuid
, uuid_prefix
, uuid_prefix_len
))
2253 if (dm_tree_node_num_children(child
, 0))
2254 dm_tree_children_use_uuid(child
, uuid_prefix
, uuid_prefix_len
);
2263 static struct load_segment
*_add_segment(struct dm_tree_node
*dnode
, unsigned type
, uint64_t size
)
2265 struct load_segment
*seg
;
2267 if (!(seg
= dm_pool_zalloc(dnode
->dtree
->mem
, sizeof(*seg
)))) {
2268 log_error("dtree node segment allocation failed");
2274 seg
->area_count
= 0;
2275 dm_list_init(&seg
->areas
);
2276 seg
->stripe_size
= 0;
2277 seg
->persistent
= 0;
2278 seg
->chunk_size
= 0;
2283 dm_list_add(&dnode
->props
.segs
, &seg
->list
);
2284 dnode
->props
.segment_count
++;
2289 int dm_tree_node_add_snapshot_origin_target(struct dm_tree_node
*dnode
,
2291 const char *origin_uuid
)
2293 struct load_segment
*seg
;
2294 struct dm_tree_node
*origin_node
;
2296 if (!(seg
= _add_segment(dnode
, SEG_SNAPSHOT_ORIGIN
, size
)))
2299 if (!(origin_node
= dm_tree_find_node_by_uuid(dnode
->dtree
, origin_uuid
))) {
2300 log_error("Couldn't find snapshot origin uuid %s.", origin_uuid
);
2304 seg
->origin
= origin_node
;
2305 if (!_link_tree_nodes(dnode
, origin_node
))
2308 /* Resume snapshot origins after new snapshots */
2309 dnode
->activation_priority
= 1;
2314 static int _add_snapshot_target(struct dm_tree_node
*node
,
2316 const char *origin_uuid
,
2317 const char *cow_uuid
,
2318 const char *merge_uuid
,
2320 uint32_t chunk_size
)
2322 struct load_segment
*seg
;
2323 struct dm_tree_node
*origin_node
, *cow_node
, *merge_node
;
2326 seg_type
= !merge_uuid
? SEG_SNAPSHOT
: SEG_SNAPSHOT_MERGE
;
2328 if (!(seg
= _add_segment(node
, seg_type
, size
)))
2331 if (!(origin_node
= dm_tree_find_node_by_uuid(node
->dtree
, origin_uuid
))) {
2332 log_error("Couldn't find snapshot origin uuid %s.", origin_uuid
);
2336 seg
->origin
= origin_node
;
2337 if (!_link_tree_nodes(node
, origin_node
))
2340 if (!(cow_node
= dm_tree_find_node_by_uuid(node
->dtree
, cow_uuid
))) {
2341 log_error("Couldn't find snapshot COW device uuid %s.", cow_uuid
);
2345 seg
->cow
= cow_node
;
2346 if (!_link_tree_nodes(node
, cow_node
))
2349 seg
->persistent
= persistent
? 1 : 0;
2350 seg
->chunk_size
= chunk_size
;
2353 if (!(merge_node
= dm_tree_find_node_by_uuid(node
->dtree
, merge_uuid
))) {
2354 /* not a pure error, merging snapshot may have been deactivated */
2355 log_verbose("Couldn't find merging snapshot uuid %s.", merge_uuid
);
2357 seg
->merge
= merge_node
;
2358 /* must not link merging snapshot, would undermine activation_priority below */
2361 /* Resume snapshot-merge (acting origin) after other snapshots */
2362 node
->activation_priority
= 1;
2364 /* Resume merging snapshot after snapshot-merge */
2365 seg
->merge
->activation_priority
= 2;
2373 int dm_tree_node_add_snapshot_target(struct dm_tree_node
*node
,
2375 const char *origin_uuid
,
2376 const char *cow_uuid
,
2378 uint32_t chunk_size
)
2380 return _add_snapshot_target(node
, size
, origin_uuid
, cow_uuid
,
2381 NULL
, persistent
, chunk_size
);
2384 int dm_tree_node_add_snapshot_merge_target(struct dm_tree_node
*node
,
2386 const char *origin_uuid
,
2387 const char *cow_uuid
,
2388 const char *merge_uuid
,
2389 uint32_t chunk_size
)
2391 return _add_snapshot_target(node
, size
, origin_uuid
, cow_uuid
,
2392 merge_uuid
, 1, chunk_size
);
2395 int dm_tree_node_add_error_target(struct dm_tree_node
*node
,
2398 if (!_add_segment(node
, SEG_ERROR
, size
))
2404 int dm_tree_node_add_zero_target(struct dm_tree_node
*node
,
2407 if (!_add_segment(node
, SEG_ZERO
, size
))
2413 int dm_tree_node_add_linear_target(struct dm_tree_node
*node
,
2416 if (!_add_segment(node
, SEG_LINEAR
, size
))
2422 int dm_tree_node_add_striped_target(struct dm_tree_node
*node
,
2424 uint32_t stripe_size
)
2426 struct load_segment
*seg
;
2428 if (!(seg
= _add_segment(node
, SEG_STRIPED
, size
)))
2431 seg
->stripe_size
= stripe_size
;
2436 int dm_tree_node_add_crypt_target(struct dm_tree_node
*node
,
2439 const char *chainmode
,
2444 struct load_segment
*seg
;
2446 if (!(seg
= _add_segment(node
, SEG_CRYPT
, size
)))
2449 seg
->cipher
= cipher
;
2450 seg
->chainmode
= chainmode
;
2452 seg
->iv_offset
= iv_offset
;
2458 int dm_tree_node_add_mirror_target_log(struct dm_tree_node
*node
,
2459 uint32_t region_size
,
2461 const char *log_uuid
,
2462 unsigned area_count
,
2465 struct dm_tree_node
*log_node
= NULL
;
2466 struct load_segment
*seg
;
2468 if (!node
->props
.segment_count
) {
2469 log_error(INTERNAL_ERROR
"Attempt to add target area to missing segment.");
2473 seg
= dm_list_item(dm_list_last(&node
->props
.segs
), struct load_segment
);
2476 if (!(seg
->uuid
= dm_pool_strdup(node
->dtree
->mem
, log_uuid
))) {
2477 log_error("log uuid pool_strdup failed");
2480 if ((flags
& DM_CORELOG
))
2481 /* For pvmove: immediate resume (for size validation) isn't needed. */
2482 node
->props
.delay_resume_if_new
= 1;
2484 if (!(log_node
= dm_tree_find_node_by_uuid(node
->dtree
, log_uuid
))) {
2485 log_error("Couldn't find mirror log uuid %s.", log_uuid
);
2490 log_node
->props
.immediate_dev_node
= 1;
2492 /* The kernel validates the size of disk logs. */
2493 /* FIXME Propagate to any devices below */
2494 log_node
->props
.delay_resume_if_new
= 0;
2496 if (!_link_tree_nodes(node
, log_node
))
2501 seg
->log
= log_node
;
2502 seg
->region_size
= region_size
;
2503 seg
->clustered
= clustered
;
2504 seg
->mirror_area_count
= area_count
;
2510 int dm_tree_node_add_mirror_target(struct dm_tree_node
*node
,
2513 if (!_add_segment(node
, SEG_MIRRORED
, size
))
2519 int dm_tree_node_add_raid_target(struct dm_tree_node
*node
,
2521 const char *raid_type
,
2522 uint32_t region_size
,
2523 uint32_t stripe_size
,
2528 struct load_segment
*seg
= NULL
;
2530 for (i
= 0; dm_segtypes
[i
].target
&& !seg
; i
++)
2531 if (!strcmp(raid_type
, dm_segtypes
[i
].target
))
2532 if (!(seg
= _add_segment(node
,
2533 dm_segtypes
[i
].type
, size
)))
2539 seg
->region_size
= region_size
;
2540 seg
->stripe_size
= stripe_size
;
2541 seg
->area_count
= 0;
2542 seg
->rebuilds
= rebuilds
;
2547 int dm_tree_node_add_replicator_target(struct dm_tree_node
*node
,
2549 const char *rlog_uuid
,
2550 const char *rlog_type
,
2551 unsigned rsite_index
,
2552 dm_replicator_mode_t mode
,
2553 uint32_t async_timeout
,
2554 uint64_t fall_behind_data
,
2555 uint32_t fall_behind_ios
)
2557 struct load_segment
*rseg
;
2558 struct replicator_site
*rsite
;
2560 /* Local site0 - adds replicator segment and links rlog device */
2561 if (rsite_index
== REPLICATOR_LOCAL_SITE
) {
2562 if (node
->props
.segment_count
) {
2563 log_error(INTERNAL_ERROR
"Attempt to add replicator segment to already used node.");
2567 if (!(rseg
= _add_segment(node
, SEG_REPLICATOR
, size
)))
2570 if (!(rseg
->log
= dm_tree_find_node_by_uuid(node
->dtree
, rlog_uuid
))) {
2571 log_error("Missing replicator log uuid %s.", rlog_uuid
);
2575 if (!_link_tree_nodes(node
, rseg
->log
))
2578 if (strcmp(rlog_type
, "ringbuffer") != 0) {
2579 log_error("Unsupported replicator log type %s.", rlog_type
);
2583 if (!(rseg
->rlog_type
= dm_pool_strdup(node
->dtree
->mem
, rlog_type
)))
2586 dm_list_init(&rseg
->rsites
);
2587 rseg
->rdevice_count
= 0;
2588 node
->activation_priority
= 1;
2591 /* Add site to segment */
2592 if (mode
== DM_REPLICATOR_SYNC
2593 && (async_timeout
|| fall_behind_ios
|| fall_behind_data
)) {
2594 log_error("Async parameters passed for synchronnous replicator.");
2598 if (node
->props
.segment_count
!= 1) {
2599 log_error(INTERNAL_ERROR
"Attempt to add remote site area before setting replicator log.");
2603 rseg
= dm_list_item(dm_list_last(&node
->props
.segs
), struct load_segment
);
2604 if (rseg
->type
!= SEG_REPLICATOR
) {
2605 log_error(INTERNAL_ERROR
"Attempt to use non replicator segment %s.",
2606 dm_segtypes
[rseg
->type
].target
);
2610 if (!(rsite
= dm_pool_zalloc(node
->dtree
->mem
, sizeof(*rsite
)))) {
2611 log_error("Failed to allocate remote site segment.");
2615 dm_list_add(&rseg
->rsites
, &rsite
->list
);
2616 rseg
->rsite_count
++;
2619 rsite
->async_timeout
= async_timeout
;
2620 rsite
->fall_behind_data
= fall_behind_data
;
2621 rsite
->fall_behind_ios
= fall_behind_ios
;
2622 rsite
->rsite_index
= rsite_index
;
2627 /* Appends device node to Replicator */
2628 int dm_tree_node_add_replicator_dev_target(struct dm_tree_node
*node
,
2630 const char *replicator_uuid
,
2631 uint64_t rdevice_index
,
2632 const char *rdev_uuid
,
2633 unsigned rsite_index
,
2634 const char *slog_uuid
,
2635 uint32_t slog_flags
,
2636 uint32_t slog_region_size
)
2638 struct seg_area
*area
;
2639 struct load_segment
*rseg
;
2640 struct load_segment
*rep_seg
;
2642 if (rsite_index
== REPLICATOR_LOCAL_SITE
) {
2643 /* Site index for local target */
2644 if (!(rseg
= _add_segment(node
, SEG_REPLICATOR_DEV
, size
)))
2647 if (!(rseg
->replicator
= dm_tree_find_node_by_uuid(node
->dtree
, replicator_uuid
))) {
2648 log_error("Missing replicator uuid %s.", replicator_uuid
);
2652 /* Local slink0 for replicator must be always initialized first */
2653 if (rseg
->replicator
->props
.segment_count
!= 1) {
2654 log_error(INTERNAL_ERROR
"Attempt to use non replicator segment.");
2658 rep_seg
= dm_list_item(dm_list_last(&rseg
->replicator
->props
.segs
), struct load_segment
);
2659 if (rep_seg
->type
!= SEG_REPLICATOR
) {
2660 log_error(INTERNAL_ERROR
"Attempt to use non replicator segment %s.",
2661 dm_segtypes
[rep_seg
->type
].target
);
2664 rep_seg
->rdevice_count
++;
2666 if (!_link_tree_nodes(node
, rseg
->replicator
))
2669 rseg
->rdevice_index
= rdevice_index
;
2671 /* Local slink0 for replicator must be always initialized first */
2672 if (node
->props
.segment_count
!= 1) {
2673 log_error(INTERNAL_ERROR
"Attempt to use non replicator-dev segment.");
2677 rseg
= dm_list_item(dm_list_last(&node
->props
.segs
), struct load_segment
);
2678 if (rseg
->type
!= SEG_REPLICATOR_DEV
) {
2679 log_error(INTERNAL_ERROR
"Attempt to use non replicator-dev segment %s.",
2680 dm_segtypes
[rseg
->type
].target
);
2685 if (!(slog_flags
& DM_CORELOG
) && !slog_uuid
) {
2686 log_error("Unspecified sync log uuid.");
2690 if (!dm_tree_node_add_target_area(node
, NULL
, rdev_uuid
, 0))
2693 area
= dm_list_item(dm_list_last(&rseg
->areas
), struct seg_area
);
2695 if (!(slog_flags
& DM_CORELOG
)) {
2696 if (!(area
->slog
= dm_tree_find_node_by_uuid(node
->dtree
, slog_uuid
))) {
2697 log_error("Couldn't find sync log uuid %s.", slog_uuid
);
2701 if (!_link_tree_nodes(node
, area
->slog
))
2705 area
->flags
= slog_flags
;
2706 area
->region_size
= slog_region_size
;
2707 area
->rsite_index
= rsite_index
;
2712 int dm_tree_node_add_thin_pool_target(struct dm_tree_node
*node
,
2714 uint64_t transaction_id
,
2715 const char *pool_uuid
,
2716 const char *metadata_uuid
,
2717 uint32_t data_block_size
,
2718 uint64_t low_water_mark_size
,
2719 unsigned skip_block_zeroing
)
2721 struct load_segment
*seg
;
2723 if (data_block_size
< THIN_MIN_DATA_SIZE
) {
2724 log_error("Data block size %d is lower then "
2725 QUOTE(THIN_MIN_DATA_SIZE
) " sectors.",
2730 if (data_block_size
> THIN_MAX_DATA_SIZE
) {
2731 log_error("Data block size %d is higher then "
2732 QUOTE(THIN_MAX_DATA_SIZE
) " sectors.",
2737 if (!(seg
= _add_segment(node
, SEG_THIN_POOL
, size
)))
2740 if (!(seg
->metadata
= dm_tree_find_node_by_uuid(node
->dtree
, metadata_uuid
))) {
2741 log_error("Missing metadata uuid %s.", metadata_uuid
);
2745 if (!_link_tree_nodes(node
, seg
->metadata
))
2748 if (!(seg
->pool
= dm_tree_find_node_by_uuid(node
->dtree
, pool_uuid
))) {
2749 log_error("Missing pool uuid %s.", pool_uuid
);
2753 if (!_link_tree_nodes(node
, seg
->pool
))
2756 node
->props
.thin_pool_transaction_id
= transaction_id
; // compare on resume
2757 seg
->low_water_mark_size
= low_water_mark_size
;
2758 seg
->data_block_size
= data_block_size
;
2759 seg
->skip_block_zeroing
= skip_block_zeroing
;
2764 int dm_tree_node_add_thin_target(struct dm_tree_node
*node
,
2766 const char *thin_pool_uuid
,
2769 struct load_segment
*seg
;
2771 if (device_id
> THIN_MAX_DEVICE_ID
) {
2772 log_error("Device id %d is higher then " QUOTE(THIN_MAX_DEVICE_ID
) ".",
2777 if (!(seg
= _add_segment(node
, SEG_THIN
, size
)))
2780 if (!(seg
->pool
= dm_tree_find_node_by_uuid(node
->dtree
, thin_pool_uuid
))) {
2781 log_error("Missing thin pool uuid %s.", thin_pool_uuid
);
2785 if (!_link_tree_nodes(node
, seg
->pool
))
2788 seg
->device_id
= device_id
;
2793 static int _add_area(struct dm_tree_node
*node
, struct load_segment
*seg
, struct dm_tree_node
*dev_node
, uint64_t offset
)
2795 struct seg_area
*area
;
2797 if (!(area
= dm_pool_zalloc(node
->dtree
->mem
, sizeof (*area
)))) {
2798 log_error("Failed to allocate target segment area.");
2802 area
->dev_node
= dev_node
;
2803 area
->offset
= offset
;
2805 dm_list_add(&seg
->areas
, &area
->list
);
2811 int dm_tree_node_add_target_area(struct dm_tree_node
*node
,
2812 const char *dev_name
,
2816 struct load_segment
*seg
;
2818 struct dm_tree_node
*dev_node
;
2820 if ((!dev_name
|| !*dev_name
) && (!uuid
|| !*uuid
)) {
2821 log_error("dm_tree_node_add_target_area called without device");
2826 if (!(dev_node
= dm_tree_find_node_by_uuid(node
->dtree
, uuid
))) {
2827 log_error("Couldn't find area uuid %s.", uuid
);
2830 if (!_link_tree_nodes(node
, dev_node
))
2833 if (stat(dev_name
, &info
) < 0) {
2834 log_error("Device %s not found.", dev_name
);
2838 if (!S_ISBLK(info
.st_mode
)) {
2839 log_error("Device %s is not a block device.", dev_name
);
2843 /* FIXME Check correct macro use */
2844 if (!(dev_node
= _add_dev(node
->dtree
, node
, MAJOR(info
.st_rdev
),
2845 MINOR(info
.st_rdev
), 0)))
2849 if (!node
->props
.segment_count
) {
2850 log_error(INTERNAL_ERROR
"Attempt to add target area to missing segment.");
2854 seg
= dm_list_item(dm_list_last(&node
->props
.segs
), struct load_segment
);
2856 if (!_add_area(node
, seg
, dev_node
, offset
))
2862 int dm_tree_node_add_null_area(struct dm_tree_node
*node
, uint64_t offset
)
2864 struct load_segment
*seg
;
2866 seg
= dm_list_item(dm_list_last(&node
->props
.segs
), struct load_segment
);
2868 switch (seg
->type
) {
2880 log_error("dm_tree_node_add_null_area() called on an unsupported segment type");
2884 if (!_add_area(node
, seg
, NULL
, offset
))
2890 void dm_tree_set_cookie(struct dm_tree_node
*node
, uint32_t cookie
)
2892 node
->dtree
->cookie
= cookie
;
2895 uint32_t dm_tree_get_cookie(struct dm_tree_node
*node
)
2897 return node
->dtree
->cookie
;