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 struct thin_message
{
113 struct dm_thin_message message
;
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 struct dm_list thin_messages
; /* Thin_pool */
173 uint64_t low_water_mark
; /* Thin_pool */
174 uint32_t data_block_size
; /* Thin_pool */
175 unsigned skip_block_zeroing
; /* Thin_pool */
176 uint32_t device_id
; /* Thin */
180 /* Per-device properties */
181 struct load_properties
{
187 uint32_t read_ahead_flags
;
189 uint64_t thin_pool_transaction_id
; /* Thin_pool */
191 unsigned segment_count
;
192 unsigned size_changed
;
195 const char *new_name
;
197 /* If immediate_dev_node is set to 1, try to create the dev node
198 * as soon as possible (e.g. in preload stage even during traversal
199 * and processing of dm tree). This will also flush all stacked dev
200 * node operations, synchronizing with udev.
202 unsigned immediate_dev_node
;
205 * If the device size changed from zero and this is set,
206 * don't resume the device immediately, even if the device
207 * has parents. This works provided the parents do not
208 * validate the device size and is required by pvmove to
209 * avoid starting the mirror resync operation too early.
211 unsigned delay_resume_if_new
;
214 /* Two of these used to join two nodes with uses and used_by. */
215 struct dm_tree_link
{
217 struct dm_tree_node
*node
;
220 struct dm_tree_node
{
221 struct dm_tree
*dtree
;
227 struct dm_list uses
; /* Nodes this node uses */
228 struct dm_list used_by
; /* Nodes that use this node */
230 int activation_priority
; /* 0 gets activated first */
232 uint16_t udev_flags
; /* Udev control flags */
234 void *context
; /* External supplied context */
236 struct load_properties props
; /* For creation/table (re)load */
239 * If presuspend of child node is needed
240 * Note: only direct child is allowed
242 struct dm_tree_node
*presuspend_node
;
247 struct dm_hash_table
*devs
;
248 struct dm_hash_table
*uuids
;
249 struct dm_tree_node root
;
250 int skip_lockfs
; /* 1 skips lockfs (for non-snapshots) */
251 int no_flush
; /* 1 sets noflush (mirrors/multipath) */
252 int retry_remove
; /* 1 retries remove if not successful */
256 struct dm_tree
*dm_tree_create(void)
258 struct dm_pool
*dmem
;
259 struct dm_tree
*dtree
;
261 if (!(dmem
= dm_pool_create("dtree", 1024)) ||
262 !(dtree
= dm_pool_zalloc(dmem
, sizeof(*dtree
)))) {
263 log_error("Failed to allocate dtree.");
265 dm_pool_destroy(dmem
);
269 dtree
->root
.dtree
= dtree
;
270 dm_list_init(&dtree
->root
.uses
);
271 dm_list_init(&dtree
->root
.used_by
);
272 dtree
->skip_lockfs
= 0;
276 if (!(dtree
->devs
= dm_hash_create(8))) {
277 log_error("dtree hash creation failed");
278 dm_pool_destroy(dtree
->mem
);
282 if (!(dtree
->uuids
= dm_hash_create(32))) {
283 log_error("dtree uuid hash creation failed");
284 dm_hash_destroy(dtree
->devs
);
285 dm_pool_destroy(dtree
->mem
);
292 void dm_tree_free(struct dm_tree
*dtree
)
297 dm_hash_destroy(dtree
->uuids
);
298 dm_hash_destroy(dtree
->devs
);
299 dm_pool_destroy(dtree
->mem
);
302 static int _nodes_are_linked(const struct dm_tree_node
*parent
,
303 const struct dm_tree_node
*child
)
305 struct dm_tree_link
*dlink
;
307 dm_list_iterate_items(dlink
, &parent
->uses
)
308 if (dlink
->node
== child
)
314 static int _link(struct dm_list
*list
, struct dm_tree_node
*node
)
316 struct dm_tree_link
*dlink
;
318 if (!(dlink
= dm_pool_alloc(node
->dtree
->mem
, sizeof(*dlink
)))) {
319 log_error("dtree link allocation failed");
324 dm_list_add(list
, &dlink
->list
);
329 static int _link_nodes(struct dm_tree_node
*parent
,
330 struct dm_tree_node
*child
)
332 if (_nodes_are_linked(parent
, child
))
335 if (!_link(&parent
->uses
, child
))
338 if (!_link(&child
->used_by
, parent
))
344 static void _unlink(struct dm_list
*list
, struct dm_tree_node
*node
)
346 struct dm_tree_link
*dlink
;
348 dm_list_iterate_items(dlink
, list
)
349 if (dlink
->node
== node
) {
350 dm_list_del(&dlink
->list
);
355 static void _unlink_nodes(struct dm_tree_node
*parent
,
356 struct dm_tree_node
*child
)
358 if (!_nodes_are_linked(parent
, child
))
361 _unlink(&parent
->uses
, child
);
362 _unlink(&child
->used_by
, parent
);
365 static int _add_to_toplevel(struct dm_tree_node
*node
)
367 return _link_nodes(&node
->dtree
->root
, node
);
370 static void _remove_from_toplevel(struct dm_tree_node
*node
)
372 _unlink_nodes(&node
->dtree
->root
, node
);
375 static int _add_to_bottomlevel(struct dm_tree_node
*node
)
377 return _link_nodes(node
, &node
->dtree
->root
);
380 static void _remove_from_bottomlevel(struct dm_tree_node
*node
)
382 _unlink_nodes(node
, &node
->dtree
->root
);
385 static int _link_tree_nodes(struct dm_tree_node
*parent
, struct dm_tree_node
*child
)
387 /* Don't link to root node if child already has a parent */
388 if (parent
== &parent
->dtree
->root
) {
389 if (dm_tree_node_num_children(child
, 1))
392 _remove_from_toplevel(child
);
394 if (child
== &child
->dtree
->root
) {
395 if (dm_tree_node_num_children(parent
, 0))
398 _remove_from_bottomlevel(parent
);
400 return _link_nodes(parent
, child
);
403 static struct dm_tree_node
*_create_dm_tree_node(struct dm_tree
*dtree
,
406 struct dm_info
*info
,
410 struct dm_tree_node
*node
;
413 if (!(node
= dm_pool_zalloc(dtree
->mem
, sizeof(*node
)))) {
414 log_error("_create_dm_tree_node alloc failed");
423 node
->context
= context
;
424 node
->udev_flags
= udev_flags
;
425 node
->activation_priority
= 0;
427 dm_list_init(&node
->uses
);
428 dm_list_init(&node
->used_by
);
429 dm_list_init(&node
->props
.segs
);
431 dev
= MKDEV(info
->major
, info
->minor
);
433 if (!dm_hash_insert_binary(dtree
->devs
, (const char *) &dev
,
434 sizeof(dev
), node
)) {
435 log_error("dtree node hash insertion failed");
436 dm_pool_free(dtree
->mem
, node
);
441 !dm_hash_insert(dtree
->uuids
, uuid
, node
)) {
442 log_error("dtree uuid hash insertion failed");
443 dm_hash_remove_binary(dtree
->devs
, (const char *) &dev
,
445 dm_pool_free(dtree
->mem
, node
);
452 static struct dm_tree_node
*_find_dm_tree_node(struct dm_tree
*dtree
,
453 uint32_t major
, uint32_t minor
)
455 uint64_t dev
= MKDEV(major
, minor
);
457 return dm_hash_lookup_binary(dtree
->devs
, (const char *) &dev
,
461 static struct dm_tree_node
*_find_dm_tree_node_by_uuid(struct dm_tree
*dtree
,
464 struct dm_tree_node
*node
;
466 if ((node
= dm_hash_lookup(dtree
->uuids
, uuid
)))
469 if (strncmp(uuid
, UUID_PREFIX
, sizeof(UUID_PREFIX
) - 1))
472 return dm_hash_lookup(dtree
->uuids
, uuid
+ sizeof(UUID_PREFIX
) - 1);
475 static int _deps(struct dm_task
**dmt
, struct dm_pool
*mem
, uint32_t major
, uint32_t minor
,
476 const char **name
, const char **uuid
,
477 struct dm_info
*info
, struct dm_deps
**deps
)
479 memset(info
, 0, sizeof(*info
));
481 if (!dm_is_dm_major(major
)) {
488 info
->live_table
= 0;
489 info
->inactive_table
= 0;
494 if (!(*dmt
= dm_task_create(DM_DEVICE_DEPS
))) {
495 log_error("deps dm_task creation failed");
499 if (!dm_task_set_major(*dmt
, major
)) {
500 log_error("_deps: failed to set major for (%" PRIu32
":%" PRIu32
")",
505 if (!dm_task_set_minor(*dmt
, minor
)) {
506 log_error("_deps: failed to set minor for (%" PRIu32
":%" PRIu32
")",
511 if (!dm_task_run(*dmt
)) {
512 log_error("_deps: task run failed for (%" PRIu32
":%" PRIu32
")",
517 if (!dm_task_get_info(*dmt
, info
)) {
518 log_error("_deps: failed to get info for (%" PRIu32
":%" PRIu32
")",
528 if (info
->major
!= major
) {
529 log_error("Inconsistent dtree major number: %u != %u",
533 if (info
->minor
!= minor
) {
534 log_error("Inconsistent dtree minor number: %u != %u",
538 if (!(*name
= dm_pool_strdup(mem
, dm_task_get_name(*dmt
)))) {
539 log_error("name pool_strdup failed");
542 if (!(*uuid
= dm_pool_strdup(mem
, dm_task_get_uuid(*dmt
)))) {
543 log_error("uuid pool_strdup failed");
546 *deps
= dm_task_get_deps(*dmt
);
552 dm_task_destroy(*dmt
);
556 static struct dm_tree_node
*_add_dev(struct dm_tree
*dtree
,
557 struct dm_tree_node
*parent
,
558 uint32_t major
, uint32_t minor
,
561 struct dm_task
*dmt
= NULL
;
563 struct dm_deps
*deps
= NULL
;
564 const char *name
= NULL
;
565 const char *uuid
= NULL
;
566 struct dm_tree_node
*node
= NULL
;
570 /* Already in tree? */
571 if (!(node
= _find_dm_tree_node(dtree
, major
, minor
))) {
572 if (!_deps(&dmt
, dtree
->mem
, major
, minor
, &name
, &uuid
, &info
, &deps
))
575 if (!(node
= _create_dm_tree_node(dtree
, name
, uuid
, &info
,
581 if (!_link_tree_nodes(parent
, node
)) {
586 /* If node was already in tree, no need to recurse. */
590 /* Can't recurse if not a mapped device or there are no dependencies */
591 if (!node
->info
.exists
|| !deps
->count
) {
592 if (!_add_to_bottomlevel(node
)) {
599 /* Add dependencies to tree */
600 for (i
= 0; i
< deps
->count
; i
++)
601 if (!_add_dev(dtree
, node
, MAJOR(deps
->device
[i
]),
602 MINOR(deps
->device
[i
]), udev_flags
)) {
609 dm_task_destroy(dmt
);
614 static int _node_clear_table(struct dm_tree_node
*dnode
)
617 struct dm_info
*info
;
621 if (!(info
= &dnode
->info
)) {
622 log_error("_node_clear_table failed: missing info");
626 if (!(name
= dm_tree_node_get_name(dnode
))) {
627 log_error("_node_clear_table failed: missing name");
631 /* Is there a table? */
632 if (!info
->exists
|| !info
->inactive_table
)
635 // FIXME Get inactive deps. If any dev referenced has 1 opener and no live table, remove it after the clear.
637 log_verbose("Clearing inactive table %s (%" PRIu32
":%" PRIu32
")",
638 name
, info
->major
, info
->minor
);
640 if (!(dmt
= dm_task_create(DM_DEVICE_CLEAR
))) {
641 log_error("Table clear dm_task creation failed for %s", name
);
645 if (!dm_task_set_major(dmt
, info
->major
) ||
646 !dm_task_set_minor(dmt
, info
->minor
)) {
647 log_error("Failed to set device number for %s table clear", name
);
648 dm_task_destroy(dmt
);
652 r
= dm_task_run(dmt
);
654 if (!dm_task_get_info(dmt
, info
)) {
655 log_error("_node_clear_table failed: info missing after running task for %s", name
);
659 dm_task_destroy(dmt
);
664 struct dm_tree_node
*dm_tree_add_new_dev(struct dm_tree
*dtree
,
667 uint32_t major
, uint32_t minor
,
672 struct dm_tree_node
*dnode
;
677 /* Do we need to add node to tree? */
678 if (!(dnode
= dm_tree_find_node_by_uuid(dtree
, uuid
))) {
679 if (!(name2
= dm_pool_strdup(dtree
->mem
, name
))) {
680 log_error("name pool_strdup failed");
683 if (!(uuid2
= dm_pool_strdup(dtree
->mem
, uuid
))) {
684 log_error("uuid pool_strdup failed");
692 info
.inactive_table
= 0;
695 if (!(dnode
= _create_dm_tree_node(dtree
, name2
, uuid2
, &info
,
699 /* Attach to root node until a table is supplied */
700 if (!_add_to_toplevel(dnode
) || !_add_to_bottomlevel(dnode
))
703 dnode
->props
.major
= major
;
704 dnode
->props
.minor
= minor
;
705 dnode
->props
.new_name
= NULL
;
706 dnode
->props
.size_changed
= 0;
707 } else if (strcmp(name
, dnode
->name
)) {
708 /* Do we need to rename node? */
709 if (!(dnode
->props
.new_name
= dm_pool_strdup(dtree
->mem
, name
))) {
710 log_error("name pool_strdup failed");
715 dnode
->props
.read_only
= read_only
? 1 : 0;
716 dnode
->props
.read_ahead
= DM_READ_AHEAD_AUTO
;
717 dnode
->props
.read_ahead_flags
= 0;
719 if (clear_inactive
&& !_node_clear_table(dnode
))
722 dnode
->context
= context
;
723 dnode
->udev_flags
= 0;
728 struct dm_tree_node
*dm_tree_add_new_dev_with_udev_flags(struct dm_tree
*dtree
,
738 struct dm_tree_node
*node
;
740 if ((node
= dm_tree_add_new_dev(dtree
, name
, uuid
, major
, minor
, read_only
,
741 clear_inactive
, context
)))
742 node
->udev_flags
= udev_flags
;
747 void dm_tree_node_set_udev_flags(struct dm_tree_node
*dnode
, uint16_t udev_flags
)
750 struct dm_info
*dinfo
= &dnode
->info
;
752 if (udev_flags
!= dnode
->udev_flags
)
753 log_debug("Resetting %s (%" PRIu32
":%" PRIu32
754 ") udev_flags from 0x%x to 0x%x",
755 dnode
->name
, dinfo
->major
, dinfo
->minor
,
756 dnode
->udev_flags
, udev_flags
);
757 dnode
->udev_flags
= udev_flags
;
760 void dm_tree_node_set_read_ahead(struct dm_tree_node
*dnode
,
762 uint32_t read_ahead_flags
)
764 dnode
->props
.read_ahead
= read_ahead
;
765 dnode
->props
.read_ahead_flags
= read_ahead_flags
;
768 void dm_tree_node_set_presuspend_node(struct dm_tree_node
*node
,
769 struct dm_tree_node
*presuspend_node
)
771 node
->presuspend_node
= presuspend_node
;
774 int dm_tree_add_dev(struct dm_tree
*dtree
, uint32_t major
, uint32_t minor
)
776 return _add_dev(dtree
, &dtree
->root
, major
, minor
, 0) ? 1 : 0;
779 int dm_tree_add_dev_with_udev_flags(struct dm_tree
*dtree
, uint32_t major
,
780 uint32_t minor
, uint16_t udev_flags
)
782 return _add_dev(dtree
, &dtree
->root
, major
, minor
, udev_flags
) ? 1 : 0;
785 const char *dm_tree_node_get_name(const struct dm_tree_node
*node
)
787 return node
->info
.exists
? node
->name
: "";
790 const char *dm_tree_node_get_uuid(const struct dm_tree_node
*node
)
792 return node
->info
.exists
? node
->uuid
: "";
795 const struct dm_info
*dm_tree_node_get_info(const struct dm_tree_node
*node
)
800 void *dm_tree_node_get_context(const struct dm_tree_node
*node
)
802 return node
->context
;
805 int dm_tree_node_size_changed(const struct dm_tree_node
*dnode
)
807 return dnode
->props
.size_changed
;
810 int dm_tree_node_num_children(const struct dm_tree_node
*node
, uint32_t inverted
)
813 if (_nodes_are_linked(&node
->dtree
->root
, node
))
815 return dm_list_size(&node
->used_by
);
818 if (_nodes_are_linked(node
, &node
->dtree
->root
))
821 return dm_list_size(&node
->uses
);
825 * Returns 1 if no prefix supplied
827 static int _uuid_prefix_matches(const char *uuid
, const char *uuid_prefix
, size_t uuid_prefix_len
)
832 if (!strncmp(uuid
, uuid_prefix
, uuid_prefix_len
))
835 /* Handle transition: active device uuids might be missing the prefix */
836 if (uuid_prefix_len
<= 4)
839 if (!strncmp(uuid
, UUID_PREFIX
, sizeof(UUID_PREFIX
) - 1))
842 if (strncmp(uuid_prefix
, UUID_PREFIX
, sizeof(UUID_PREFIX
) - 1))
845 if (!strncmp(uuid
, uuid_prefix
+ sizeof(UUID_PREFIX
) - 1, uuid_prefix_len
- (sizeof(UUID_PREFIX
) - 1)))
852 * Returns 1 if no children.
854 static int _children_suspended(struct dm_tree_node
*node
,
856 const char *uuid_prefix
,
857 size_t uuid_prefix_len
)
859 struct dm_list
*list
;
860 struct dm_tree_link
*dlink
;
861 const struct dm_info
*dinfo
;
865 if (_nodes_are_linked(&node
->dtree
->root
, node
))
867 list
= &node
->used_by
;
869 if (_nodes_are_linked(node
, &node
->dtree
->root
))
874 dm_list_iterate_items(dlink
, list
) {
875 if (!(uuid
= dm_tree_node_get_uuid(dlink
->node
))) {
880 /* Ignore if it doesn't belong to this VG */
881 if (!_uuid_prefix_matches(uuid
, uuid_prefix
, uuid_prefix_len
))
884 /* Ignore if parent node wants to presuspend this node */
885 if (dlink
->node
->presuspend_node
== node
)
888 if (!(dinfo
= dm_tree_node_get_info(dlink
->node
))) {
889 stack
; /* FIXME Is this normal? */
893 if (!dinfo
->suspended
)
901 * Set major and minor to zero for root of tree.
903 struct dm_tree_node
*dm_tree_find_node(struct dm_tree
*dtree
,
907 if (!major
&& !minor
)
910 return _find_dm_tree_node(dtree
, major
, minor
);
914 * Set uuid to NULL for root of tree.
916 struct dm_tree_node
*dm_tree_find_node_by_uuid(struct dm_tree
*dtree
,
922 return _find_dm_tree_node_by_uuid(dtree
, uuid
);
926 * First time set *handle to NULL.
927 * Set inverted to invert the tree.
929 struct dm_tree_node
*dm_tree_next_child(void **handle
,
930 const struct dm_tree_node
*parent
,
933 struct dm_list
**dlink
= (struct dm_list
**) handle
;
934 const struct dm_list
*use_list
;
937 use_list
= &parent
->used_by
;
939 use_list
= &parent
->uses
;
942 *dlink
= dm_list_first(use_list
);
944 *dlink
= dm_list_next(use_list
, *dlink
);
946 return (*dlink
) ? dm_list_item(*dlink
, struct dm_tree_link
)->node
: NULL
;
950 * Deactivate a device with its dependencies if the uuid prefix matches.
952 static int _info_by_dev(uint32_t major
, uint32_t minor
, int with_open_count
,
953 struct dm_info
*info
)
958 if (!(dmt
= dm_task_create(DM_DEVICE_INFO
))) {
959 log_error("_info_by_dev: dm_task creation failed");
963 if (!dm_task_set_major(dmt
, major
) || !dm_task_set_minor(dmt
, minor
)) {
964 log_error("_info_by_dev: Failed to set device number");
965 dm_task_destroy(dmt
);
969 if (!with_open_count
&& !dm_task_no_open_count(dmt
))
970 log_error("Failed to disable open_count");
972 if ((r
= dm_task_run(dmt
)))
973 r
= dm_task_get_info(dmt
, info
);
975 dm_task_destroy(dmt
);
980 static int _check_device_not_in_use(const char *name
, struct dm_info
*info
)
985 /* If sysfs is not used, use open_count information only. */
986 if (!*dm_sysfs_dir()) {
987 if (info
->open_count
) {
988 log_error("Device %s (%" PRIu32
":%" PRIu32
") in use",
989 name
, info
->major
, info
->minor
);
996 if (dm_device_has_holders(info
->major
, info
->minor
)) {
997 log_error("Device %s (%" PRIu32
":%" PRIu32
") is used "
998 "by another device.", name
, info
->major
, info
->minor
);
1002 if (dm_device_has_mounted_fs(info
->major
, info
->minor
)) {
1003 log_error("Device %s (%" PRIu32
":%" PRIu32
") contains "
1004 "a filesystem in use.", name
, info
->major
, info
->minor
);
1011 /* Check if all parent nodes of given node have open_count == 0 */
1012 static int _node_has_closed_parents(struct dm_tree_node
*node
,
1013 const char *uuid_prefix
,
1014 size_t uuid_prefix_len
)
1016 struct dm_tree_link
*dlink
;
1017 const struct dm_info
*dinfo
;
1018 struct dm_info info
;
1021 /* Iterate through parents of this node */
1022 dm_list_iterate_items(dlink
, &node
->used_by
) {
1023 if (!(uuid
= dm_tree_node_get_uuid(dlink
->node
))) {
1028 /* Ignore if it doesn't belong to this VG */
1029 if (!_uuid_prefix_matches(uuid
, uuid_prefix
, uuid_prefix_len
))
1032 if (!(dinfo
= dm_tree_node_get_info(dlink
->node
))) {
1033 stack
; /* FIXME Is this normal? */
1037 /* Refresh open_count */
1038 if (!_info_by_dev(dinfo
->major
, dinfo
->minor
, 1, &info
) ||
1042 if (info
.open_count
) {
1043 log_debug("Node %s %d:%d has open_count %d", uuid_prefix
,
1044 dinfo
->major
, dinfo
->minor
, info
.open_count
);
1052 static int _deactivate_node(const char *name
, uint32_t major
, uint32_t minor
,
1053 uint32_t *cookie
, uint16_t udev_flags
, int retry
)
1055 struct dm_task
*dmt
;
1058 log_verbose("Removing %s (%" PRIu32
":%" PRIu32
")", name
, major
, minor
);
1060 if (!(dmt
= dm_task_create(DM_DEVICE_REMOVE
))) {
1061 log_error("Deactivation dm_task creation failed for %s", name
);
1065 if (!dm_task_set_major(dmt
, major
) || !dm_task_set_minor(dmt
, minor
)) {
1066 log_error("Failed to set device number for %s deactivation", name
);
1070 if (!dm_task_no_open_count(dmt
))
1071 log_error("Failed to disable open_count");
1073 if (!dm_task_set_cookie(dmt
, cookie
, udev_flags
))
1078 dm_task_retry_remove(dmt
);
1080 r
= dm_task_run(dmt
);
1082 /* FIXME Until kernel returns actual name so dm-iface.c can handle it */
1083 rm_dev_node(name
, dmt
->cookie_set
&& !(udev_flags
& DM_UDEV_DISABLE_DM_RULES_FLAG
),
1084 dmt
->cookie_set
&& (udev_flags
& DM_UDEV_DISABLE_LIBRARY_FALLBACK
));
1086 /* FIXME Remove node from tree or mark invalid? */
1089 dm_task_destroy(dmt
);
1094 static int _rename_node(const char *old_name
, const char *new_name
, uint32_t major
,
1095 uint32_t minor
, uint32_t *cookie
, uint16_t udev_flags
)
1097 struct dm_task
*dmt
;
1100 log_verbose("Renaming %s (%" PRIu32
":%" PRIu32
") to %s", old_name
, major
, minor
, new_name
);
1102 if (!(dmt
= dm_task_create(DM_DEVICE_RENAME
))) {
1103 log_error("Rename dm_task creation failed for %s", old_name
);
1107 if (!dm_task_set_name(dmt
, old_name
)) {
1108 log_error("Failed to set name for %s rename.", old_name
);
1112 if (!dm_task_set_newname(dmt
, new_name
))
1115 if (!dm_task_no_open_count(dmt
))
1116 log_error("Failed to disable open_count");
1118 if (!dm_task_set_cookie(dmt
, cookie
, udev_flags
))
1121 r
= dm_task_run(dmt
);
1124 dm_task_destroy(dmt
);
1129 /* FIXME Merge with _suspend_node? */
1130 static int _resume_node(const char *name
, uint32_t major
, uint32_t minor
,
1131 uint32_t read_ahead
, uint32_t read_ahead_flags
,
1132 struct dm_info
*newinfo
, uint32_t *cookie
,
1133 uint16_t udev_flags
, int already_suspended
)
1135 struct dm_task
*dmt
;
1138 log_verbose("Resuming %s (%" PRIu32
":%" PRIu32
")", name
, major
, minor
);
1140 if (!(dmt
= dm_task_create(DM_DEVICE_RESUME
))) {
1141 log_debug("Suspend dm_task creation failed for %s.", name
);
1145 /* FIXME Kernel should fill in name on return instead */
1146 if (!dm_task_set_name(dmt
, name
)) {
1147 log_debug("Failed to set device name for %s resumption.", name
);
1151 if (!dm_task_set_major(dmt
, major
) || !dm_task_set_minor(dmt
, minor
)) {
1152 log_error("Failed to set device number for %s resumption.", name
);
1156 if (!dm_task_no_open_count(dmt
))
1157 log_error("Failed to disable open_count");
1159 if (!dm_task_set_read_ahead(dmt
, read_ahead
, read_ahead_flags
))
1160 log_error("Failed to set read ahead");
1162 if (!dm_task_set_cookie(dmt
, cookie
, udev_flags
))
1165 if (!(r
= dm_task_run(dmt
)))
1168 if (already_suspended
)
1171 if (!(r
= dm_task_get_info(dmt
, newinfo
)))
1175 dm_task_destroy(dmt
);
1180 static int _suspend_node(const char *name
, uint32_t major
, uint32_t minor
,
1181 int skip_lockfs
, int no_flush
, struct dm_info
*newinfo
)
1183 struct dm_task
*dmt
;
1186 log_verbose("Suspending %s (%" PRIu32
":%" PRIu32
")%s%s",
1188 skip_lockfs
? "" : " with filesystem sync",
1189 no_flush
? "" : " with device flush");
1191 if (!(dmt
= dm_task_create(DM_DEVICE_SUSPEND
))) {
1192 log_error("Suspend dm_task creation failed for %s", name
);
1196 if (!dm_task_set_major(dmt
, major
) || !dm_task_set_minor(dmt
, minor
)) {
1197 log_error("Failed to set device number for %s suspension.", name
);
1198 dm_task_destroy(dmt
);
1202 if (!dm_task_no_open_count(dmt
))
1203 log_error("Failed to disable open_count");
1205 if (skip_lockfs
&& !dm_task_skip_lockfs(dmt
))
1206 log_error("Failed to set skip_lockfs flag.");
1208 if (no_flush
&& !dm_task_no_flush(dmt
))
1209 log_error("Failed to set no_flush flag.");
1211 if ((r
= dm_task_run(dmt
))) {
1213 r
= dm_task_get_info(dmt
, newinfo
);
1216 dm_task_destroy(dmt
);
1221 static int _thin_pool_status_transaction_id(struct dm_tree_node
*dnode
, uint64_t *transaction_id
)
1223 struct dm_task
*dmt
;
1225 uint64_t start
, length
;
1227 char *params
= NULL
;
1229 if (!(dmt
= dm_task_create(DM_DEVICE_STATUS
)))
1232 if (!dm_task_set_major(dmt
, dnode
->info
.major
) ||
1233 !dm_task_set_minor(dmt
, dnode
->info
.minor
)) {
1234 log_error("Failed to set major minor.");
1238 if (!dm_task_run(dmt
))
1241 dm_get_next_target(dmt
, NULL
, &start
, &length
, &type
, ¶ms
);
1243 if (type
&& (strcmp(type
, "thin-pool") != 0)) {
1244 log_error(INTERNAL_ERROR
1245 "Expected thin-pool target for %d:%d and got %s.",
1246 dnode
->info
.major
, dnode
->info
.minor
, type
);
1250 if (!params
|| (sscanf(params
, "%" PRIu64
, transaction_id
) != 1)) {
1251 log_error(INTERNAL_ERROR
1252 "Failed to parse transaction_id from %s.", params
);
1256 log_debug("Thin pool transaction id: %" PRIu64
" status: %s.", *transaction_id
, params
);
1260 dm_task_destroy(dmt
);
1265 static int _thin_pool_node_message(struct dm_tree_node
*dnode
, struct thin_message
*tm
)
1267 struct dm_task
*dmt
;
1268 struct dm_thin_message
*m
= &tm
->message
;
1273 case DM_THIN_MESSAGE_CREATE_SNAP
:
1274 r
= dm_snprintf(buf
, sizeof(buf
), "create_snap %u %u",
1275 m
->u
.m_create_snap
.device_id
,
1276 m
->u
.m_create_snap
.origin_id
);
1278 case DM_THIN_MESSAGE_CREATE_THIN
:
1279 r
= dm_snprintf(buf
, sizeof(buf
), "create_thin %u",
1280 m
->u
.m_create_thin
.device_id
);
1282 case DM_THIN_MESSAGE_DELETE
:
1283 r
= dm_snprintf(buf
, sizeof(buf
), "delete %u",
1284 m
->u
.m_delete
.device_id
);
1286 case DM_THIN_MESSAGE_TRIM
:
1287 r
= dm_snprintf(buf
, sizeof(buf
), "trim %u %" PRIu64
,
1288 m
->u
.m_trim
.device_id
,
1289 m
->u
.m_trim
.new_size
);
1291 case DM_THIN_MESSAGE_SET_TRANSACTION_ID
:
1292 r
= dm_snprintf(buf
, sizeof(buf
),
1293 "set_transaction_id %" PRIu64
" %" PRIu64
,
1294 m
->u
.m_set_transaction_id
.current_id
,
1295 m
->u
.m_set_transaction_id
.new_id
);
1300 log_error("Failed to prepare message.");
1306 if (!(dmt
= dm_task_create(DM_DEVICE_TARGET_MSG
)))
1309 if (!dm_task_set_major(dmt
, dnode
->info
.major
) ||
1310 !dm_task_set_minor(dmt
, dnode
->info
.minor
)) {
1311 log_error("Failed to set message major minor.");
1315 if (!dm_task_set_message(dmt
, buf
))
1318 /* Internal functionality of dm_task */
1319 dmt
->expected_errno
= tm
->expected_errno
;
1321 if (!dm_task_run(dmt
))
1326 dm_task_destroy(dmt
);
1331 static int _node_send_messages(struct dm_tree_node
*dnode
,
1332 const char *uuid_prefix
,
1333 size_t uuid_prefix_len
)
1335 struct load_segment
*seg
;
1336 struct thin_message
*tmsg
;
1340 if ((dnode
== &dnode
->dtree
->root
) || /* root has props.segs uninitialized */
1341 !dnode
->info
.exists
|| (dm_list_size(&dnode
->props
.segs
) != 1))
1344 seg
= dm_list_item(dm_list_last(&dnode
->props
.segs
), struct load_segment
);
1345 if (seg
->type
!= SEG_THIN_POOL
)
1348 if (!(uuid
= dm_tree_node_get_uuid(dnode
)))
1351 if (!_uuid_prefix_matches(uuid
, uuid_prefix
, uuid_prefix_len
)) {
1352 log_debug("UUID \"%s\" does not match.", uuid
);
1356 if (!_thin_pool_status_transaction_id(dnode
, &trans_id
))
1359 if (trans_id
== dnode
->props
.thin_pool_transaction_id
)
1360 return 1; /* In sync - skip messages */
1362 if (trans_id
!= (dnode
->props
.thin_pool_transaction_id
- 1)) {
1363 log_error("Thin pool transaction_id=%" PRIu64
", while expected: %" PRIu64
".",
1364 trans_id
, dnode
->props
.thin_pool_transaction_id
- 1);
1365 return 0; /* Nothing to send */
1368 dm_list_iterate_items(tmsg
, &seg
->thin_messages
)
1369 if (!(_thin_pool_node_message(dnode
, tmsg
)))
1376 * FIXME Don't attempt to deactivate known internal dependencies.
1378 static int _dm_tree_deactivate_children(struct dm_tree_node
*dnode
,
1379 const char *uuid_prefix
,
1380 size_t uuid_prefix_len
,
1384 void *handle
= NULL
;
1385 struct dm_tree_node
*child
= dnode
;
1386 struct dm_info info
;
1387 const struct dm_info
*dinfo
;
1391 while ((child
= dm_tree_next_child(&handle
, dnode
, 0))) {
1392 if (!(dinfo
= dm_tree_node_get_info(child
))) {
1397 if (!(name
= dm_tree_node_get_name(child
))) {
1402 if (!(uuid
= dm_tree_node_get_uuid(child
))) {
1407 /* Ignore if it doesn't belong to this VG */
1408 if (!_uuid_prefix_matches(uuid
, uuid_prefix
, uuid_prefix_len
))
1411 /* Refresh open_count */
1412 if (!_info_by_dev(dinfo
->major
, dinfo
->minor
, 1, &info
) ||
1416 if (info
.open_count
) {
1417 /* Skip internal non-toplevel opened nodes */
1421 /* When retry is not allowed, error */
1422 if (!child
->dtree
->retry_remove
) {
1423 log_error("Unable to deactivate open %s (%" PRIu32
1424 ":%" PRIu32
")", name
, info
.major
, info
.minor
);
1429 /* Check toplevel node for holders/mounted fs */
1430 if (!_check_device_not_in_use(name
, &info
)) {
1435 /* Go on with retry */
1438 /* Also checking open_count in parent nodes of presuspend_node */
1439 if ((child
->presuspend_node
&&
1440 !_node_has_closed_parents(child
->presuspend_node
,
1441 uuid_prefix
, uuid_prefix_len
))) {
1442 /* Only report error from (likely non-internal) dependency at top level */
1444 log_error("Unable to deactivate open %s (%" PRIu32
1445 ":%" PRIu32
")", name
, info
.major
,
1452 /* Suspend child node first if requested */
1453 if (child
->presuspend_node
&&
1454 !dm_tree_suspend_children(child
, uuid_prefix
, uuid_prefix_len
))
1457 if (!_deactivate_node(name
, info
.major
, info
.minor
,
1458 &child
->dtree
->cookie
, child
->udev_flags
,
1459 (level
== 0) ? child
->dtree
->retry_remove
: 0)) {
1460 log_error("Unable to deactivate %s (%" PRIu32
1461 ":%" PRIu32
")", name
, info
.major
,
1465 } else if (info
.suspended
)
1468 if (dm_tree_node_num_children(child
, 0)) {
1469 if (!_dm_tree_deactivate_children(child
, uuid_prefix
, uuid_prefix_len
, level
+ 1))
1477 int dm_tree_deactivate_children(struct dm_tree_node
*dnode
,
1478 const char *uuid_prefix
,
1479 size_t uuid_prefix_len
)
1481 return _dm_tree_deactivate_children(dnode
, uuid_prefix
, uuid_prefix_len
, 0);
1484 void dm_tree_skip_lockfs(struct dm_tree_node
*dnode
)
1486 dnode
->dtree
->skip_lockfs
= 1;
1489 void dm_tree_use_no_flush_suspend(struct dm_tree_node
*dnode
)
1491 dnode
->dtree
->no_flush
= 1;
1494 void dm_tree_retry_remove(struct dm_tree_node
*dnode
)
1496 dnode
->dtree
->retry_remove
= 1;
1499 int dm_tree_suspend_children(struct dm_tree_node
*dnode
,
1500 const char *uuid_prefix
,
1501 size_t uuid_prefix_len
)
1504 void *handle
= NULL
;
1505 struct dm_tree_node
*child
= dnode
;
1506 struct dm_info info
, newinfo
;
1507 const struct dm_info
*dinfo
;
1511 /* Suspend nodes at this level of the tree */
1512 while ((child
= dm_tree_next_child(&handle
, dnode
, 0))) {
1513 if (!(dinfo
= dm_tree_node_get_info(child
))) {
1518 if (!(name
= dm_tree_node_get_name(child
))) {
1523 if (!(uuid
= dm_tree_node_get_uuid(child
))) {
1528 /* Ignore if it doesn't belong to this VG */
1529 if (!_uuid_prefix_matches(uuid
, uuid_prefix
, uuid_prefix_len
))
1532 /* Ensure immediate parents are already suspended */
1533 if (!_children_suspended(child
, 1, uuid_prefix
, uuid_prefix_len
))
1536 if (!_info_by_dev(dinfo
->major
, dinfo
->minor
, 0, &info
) ||
1537 !info
.exists
|| info
.suspended
)
1540 if (!_suspend_node(name
, info
.major
, info
.minor
,
1541 child
->dtree
->skip_lockfs
,
1542 child
->dtree
->no_flush
, &newinfo
)) {
1543 log_error("Unable to suspend %s (%" PRIu32
1544 ":%" PRIu32
")", name
, info
.major
,
1550 /* Update cached info */
1551 child
->info
= newinfo
;
1554 /* Then suspend any child nodes */
1557 while ((child
= dm_tree_next_child(&handle
, dnode
, 0))) {
1558 if (!(uuid
= dm_tree_node_get_uuid(child
))) {
1563 /* Ignore if it doesn't belong to this VG */
1564 if (!_uuid_prefix_matches(uuid
, uuid_prefix
, uuid_prefix_len
))
1567 if (dm_tree_node_num_children(child
, 0))
1568 if (!dm_tree_suspend_children(child
, uuid_prefix
, uuid_prefix_len
))
1575 int dm_tree_activate_children(struct dm_tree_node
*dnode
,
1576 const char *uuid_prefix
,
1577 size_t uuid_prefix_len
)
1580 void *handle
= NULL
;
1581 struct dm_tree_node
*child
= dnode
;
1582 struct dm_info newinfo
;
1587 /* Activate children first */
1588 while ((child
= dm_tree_next_child(&handle
, dnode
, 0))) {
1589 if (!(uuid
= dm_tree_node_get_uuid(child
))) {
1594 if (!_uuid_prefix_matches(uuid
, uuid_prefix
, uuid_prefix_len
))
1597 if (dm_tree_node_num_children(child
, 0))
1598 if (!dm_tree_activate_children(child
, uuid_prefix
, uuid_prefix_len
))
1604 for (priority
= 0; priority
< 3; priority
++) {
1605 while ((child
= dm_tree_next_child(&handle
, dnode
, 0))) {
1606 if (priority
!= child
->activation_priority
)
1609 if (!(uuid
= dm_tree_node_get_uuid(child
))) {
1614 if (!_uuid_prefix_matches(uuid
, uuid_prefix
, uuid_prefix_len
))
1617 if (!(name
= dm_tree_node_get_name(child
))) {
1623 if (child
->props
.new_name
) {
1624 if (!_rename_node(name
, child
->props
.new_name
, child
->info
.major
,
1625 child
->info
.minor
, &child
->dtree
->cookie
,
1626 child
->udev_flags
)) {
1627 log_error("Failed to rename %s (%" PRIu32
1628 ":%" PRIu32
") to %s", name
, child
->info
.major
,
1629 child
->info
.minor
, child
->props
.new_name
);
1632 child
->name
= child
->props
.new_name
;
1633 child
->props
.new_name
= NULL
;
1636 if (!child
->info
.inactive_table
&& !child
->info
.suspended
)
1639 if (!_resume_node(child
->name
, child
->info
.major
, child
->info
.minor
,
1640 child
->props
.read_ahead
, child
->props
.read_ahead_flags
,
1641 &newinfo
, &child
->dtree
->cookie
, child
->udev_flags
, child
->info
.suspended
)) {
1642 log_error("Unable to resume %s (%" PRIu32
1643 ":%" PRIu32
")", child
->name
, child
->info
.major
,
1649 /* Update cached info */
1650 child
->info
= newinfo
;
1659 static int _create_node(struct dm_tree_node
*dnode
)
1662 struct dm_task
*dmt
;
1664 log_verbose("Creating %s", dnode
->name
);
1666 if (!(dmt
= dm_task_create(DM_DEVICE_CREATE
))) {
1667 log_error("Create dm_task creation failed for %s", dnode
->name
);
1671 if (!dm_task_set_name(dmt
, dnode
->name
)) {
1672 log_error("Failed to set device name for %s", dnode
->name
);
1676 if (!dm_task_set_uuid(dmt
, dnode
->uuid
)) {
1677 log_error("Failed to set uuid for %s", dnode
->name
);
1681 if (dnode
->props
.major
&&
1682 (!dm_task_set_major(dmt
, dnode
->props
.major
) ||
1683 !dm_task_set_minor(dmt
, dnode
->props
.minor
))) {
1684 log_error("Failed to set device number for %s creation.", dnode
->name
);
1688 if (dnode
->props
.read_only
&& !dm_task_set_ro(dmt
)) {
1689 log_error("Failed to set read only flag for %s", dnode
->name
);
1693 if (!dm_task_no_open_count(dmt
))
1694 log_error("Failed to disable open_count");
1696 if ((r
= dm_task_run(dmt
)))
1697 r
= dm_task_get_info(dmt
, &dnode
->info
);
1700 dm_task_destroy(dmt
);
1706 static int _build_dev_string(char *devbuf
, size_t bufsize
, struct dm_tree_node
*node
)
1708 if (!dm_format_dev(devbuf
, bufsize
, node
->info
.major
, node
->info
.minor
)) {
1709 log_error("Failed to format %s device number for %s as dm "
1711 node
->name
, node
->uuid
, node
->info
.major
, node
->info
.minor
);
1718 /* simplify string emiting code */
1719 #define EMIT_PARAMS(p, str...)\
1722 if ((w = dm_snprintf(params + p, paramsize - (size_t) p, str)) < 0) {\
1723 stack; /* Out of space */\
1732 * Returns: 1 on success, 0 on failure
1734 static int _emit_areas_line(struct dm_task
*dmt
__attribute__((unused
)),
1735 struct load_segment
*seg
, char *params
,
1736 size_t paramsize
, int *pos
)
1738 struct seg_area
*area
;
1739 char devbuf
[DM_FORMAT_DEV_BUFSIZE
];
1740 unsigned first_time
= 1;
1741 const char *logtype
, *synctype
;
1742 unsigned log_parm_count
;
1744 dm_list_iterate_items(area
, &seg
->areas
) {
1745 switch (seg
->type
) {
1746 case SEG_REPLICATOR_DEV
:
1747 if (!_build_dev_string(devbuf
, sizeof(devbuf
), area
->dev_node
))
1750 EMIT_PARAMS(*pos
, " %d 1 %s", area
->rsite_index
, devbuf
);
1752 EMIT_PARAMS(*pos
, " nolog 0");
1754 /* Remote devices */
1755 log_parm_count
= (area
->flags
&
1756 (DM_NOSYNC
| DM_FORCESYNC
)) ? 2 : 1;
1759 devbuf
[0] = 0; /* Only core log parameters */
1762 devbuf
[0] = ' '; /* Extra space before device name */
1763 if (!_build_dev_string(devbuf
+ 1,
1768 log_parm_count
++; /* Extra sync log device name parameter */
1771 EMIT_PARAMS(*pos
, " %s %u%s %" PRIu64
, logtype
,
1772 log_parm_count
, devbuf
, area
->region_size
);
1774 synctype
= (area
->flags
& DM_NOSYNC
) ?
1775 " nosync" : (area
->flags
& DM_FORCESYNC
) ?
1779 EMIT_PARAMS(*pos
, "%s", synctype
);
1791 if (!area
->dev_node
) {
1792 EMIT_PARAMS(*pos
, " -");
1795 if (!_build_dev_string(devbuf
, sizeof(devbuf
), area
->dev_node
))
1798 EMIT_PARAMS(*pos
, " %s", devbuf
);
1801 if (!_build_dev_string(devbuf
, sizeof(devbuf
), area
->dev_node
))
1804 EMIT_PARAMS(*pos
, "%s%s %" PRIu64
, first_time
? "" : " ",
1805 devbuf
, area
->offset
);
1814 static int _replicator_emit_segment_line(const struct load_segment
*seg
, char *params
,
1815 size_t paramsize
, int *pos
)
1817 const struct load_segment
*rlog_seg
;
1818 struct replicator_site
*rsite
;
1819 char rlogbuf
[DM_FORMAT_DEV_BUFSIZE
];
1820 unsigned parm_count
;
1822 if (!seg
->log
|| !_build_dev_string(rlogbuf
, sizeof(rlogbuf
), seg
->log
))
1825 rlog_seg
= dm_list_item(dm_list_last(&seg
->log
->props
.segs
),
1826 struct load_segment
);
1828 EMIT_PARAMS(*pos
, "%s 4 %s 0 auto %" PRIu64
,
1829 seg
->rlog_type
, rlogbuf
, rlog_seg
->size
);
1831 dm_list_iterate_items(rsite
, &seg
->rsites
) {
1832 parm_count
= (rsite
->fall_behind_data
1833 || rsite
->fall_behind_ios
1834 || rsite
->async_timeout
) ? 4 : 2;
1836 EMIT_PARAMS(*pos
, " blockdev %u %u %s", parm_count
, rsite
->rsite_index
,
1837 (rsite
->mode
== DM_REPLICATOR_SYNC
) ? "synchronous" : "asynchronous");
1839 if (rsite
->fall_behind_data
)
1840 EMIT_PARAMS(*pos
, " data %" PRIu64
, rsite
->fall_behind_data
);
1841 else if (rsite
->fall_behind_ios
)
1842 EMIT_PARAMS(*pos
, " ios %" PRIu32
, rsite
->fall_behind_ios
);
1843 else if (rsite
->async_timeout
)
1844 EMIT_PARAMS(*pos
, " timeout %" PRIu32
, rsite
->async_timeout
);
1851 * Returns: 1 on success, 0 on failure
1853 static int _mirror_emit_segment_line(struct dm_task
*dmt
, struct load_segment
*seg
,
1854 char *params
, size_t paramsize
)
1856 int block_on_error
= 0;
1857 int handle_errors
= 0;
1858 int dm_log_userspace
= 0;
1860 unsigned log_parm_count
;
1862 char logbuf
[DM_FORMAT_DEV_BUFSIZE
];
1863 const char *logtype
;
1864 unsigned kmaj
= 0, kmin
= 0, krel
= 0;
1866 if (uname(&uts
) == -1) {
1867 log_error("Cannot read kernel release version.");
1871 /* Kernels with a major number of 2 always had 3 parts. */
1872 parts
= sscanf(uts
.release
, "%u.%u.%u", &kmaj
, &kmin
, &krel
);
1873 if (parts
< 1 || (kmaj
< 3 && parts
< 3)) {
1874 log_error("Wrong kernel release version %s.", uts
.release
);
1878 if ((seg
->flags
& DM_BLOCK_ON_ERROR
)) {
1880 * Originally, block_on_error was an argument to the log
1881 * portion of the mirror CTR table. It was renamed to
1882 * "handle_errors" and now resides in the 'features'
1883 * section of the mirror CTR table (i.e. at the end).
1885 * We can identify whether to use "block_on_error" or
1886 * "handle_errors" by the dm-mirror module's version
1887 * number (>= 1.12) or by the kernel version (>= 2.6.22).
1889 if (KERNEL_VERSION(kmaj
, kmin
, krel
) >= KERNEL_VERSION(2, 6, 22))
1895 if (seg
->clustered
) {
1896 /* Cluster mirrors require a UUID */
1901 * Cluster mirrors used to have their own log
1902 * types. Now they are accessed through the
1903 * userspace log type.
1905 * The dm-log-userspace module was added to the
1908 if (KERNEL_VERSION(kmaj
, kmin
, krel
) >= KERNEL_VERSION(2, 6, 31))
1909 dm_log_userspace
= 1;
1915 /* [no]sync, block_on_error etc. */
1916 log_parm_count
+= hweight32(seg
->flags
);
1918 /* "handle_errors" is a feature arg now */
1922 /* DM_CORELOG does not count in the param list */
1923 if (seg
->flags
& DM_CORELOG
)
1926 if (seg
->clustered
) {
1927 log_parm_count
++; /* For UUID */
1929 if (!dm_log_userspace
)
1930 EMIT_PARAMS(pos
, "clustered-");
1932 /* For clustered-* type field inserted later */
1941 if (!_build_dev_string(logbuf
, sizeof(logbuf
), seg
->log
))
1945 if (dm_log_userspace
)
1946 EMIT_PARAMS(pos
, "userspace %u %s clustered-%s",
1947 log_parm_count
, seg
->uuid
, logtype
);
1949 EMIT_PARAMS(pos
, "%s %u", logtype
, log_parm_count
);
1952 EMIT_PARAMS(pos
, " %s", logbuf
);
1954 EMIT_PARAMS(pos
, " %u", seg
->region_size
);
1956 if (seg
->clustered
&& !dm_log_userspace
)
1957 EMIT_PARAMS(pos
, " %s", seg
->uuid
);
1959 if ((seg
->flags
& DM_NOSYNC
))
1960 EMIT_PARAMS(pos
, " nosync");
1961 else if ((seg
->flags
& DM_FORCESYNC
))
1962 EMIT_PARAMS(pos
, " sync");
1965 EMIT_PARAMS(pos
, " block_on_error");
1967 EMIT_PARAMS(pos
, " %u ", seg
->mirror_area_count
);
1969 if (_emit_areas_line(dmt
, seg
, params
, paramsize
, &pos
) <= 0)
1973 EMIT_PARAMS(pos
, " 1 handle_errors");
1978 static int _raid_emit_segment_line(struct dm_task
*dmt
, uint32_t major
,
1979 uint32_t minor
, struct load_segment
*seg
,
1980 uint64_t *seg_start
, char *params
,
1984 int param_count
= 1; /* mandatory 'chunk size'/'stripe size' arg */
1987 if ((seg
->flags
& DM_NOSYNC
) || (seg
->flags
& DM_FORCESYNC
))
1990 if (seg
->region_size
)
1993 /* rebuilds is 64-bit */
1994 param_count
+= 2 * hweight32(seg
->rebuilds
& 0xFFFFFFFF);
1995 param_count
+= 2 * hweight32(seg
->rebuilds
>> 32);
1997 if ((seg
->type
== SEG_RAID1
) && seg
->stripe_size
)
1998 log_error("WARNING: Ignoring RAID1 stripe size");
2000 EMIT_PARAMS(pos
, "%s %d %u", dm_segtypes
[seg
->type
].target
,
2001 param_count
, seg
->stripe_size
);
2003 if (seg
->flags
& DM_NOSYNC
)
2004 EMIT_PARAMS(pos
, " nosync");
2005 else if (seg
->flags
& DM_FORCESYNC
)
2006 EMIT_PARAMS(pos
, " sync");
2008 if (seg
->region_size
)
2009 EMIT_PARAMS(pos
, " region_size %u", seg
->region_size
);
2011 for (i
= 0; i
< (seg
->area_count
/ 2); i
++)
2012 if (seg
->rebuilds
& (1 << i
))
2013 EMIT_PARAMS(pos
, " rebuild %u", i
);
2015 /* Print number of metadata/data device pairs */
2016 EMIT_PARAMS(pos
, " %u", seg
->area_count
/2);
2018 if (_emit_areas_line(dmt
, seg
, params
, paramsize
, &pos
) <= 0)
2024 static int _emit_segment_line(struct dm_task
*dmt
, uint32_t major
,
2025 uint32_t minor
, struct load_segment
*seg
,
2026 uint64_t *seg_start
, char *params
,
2031 int target_type_is_raid
= 0;
2032 char originbuf
[DM_FORMAT_DEV_BUFSIZE
], cowbuf
[DM_FORMAT_DEV_BUFSIZE
];
2033 char pool
[DM_FORMAT_DEV_BUFSIZE
], metadata
[DM_FORMAT_DEV_BUFSIZE
];
2041 /* Mirrors are pretty complicated - now in separate function */
2042 r
= _mirror_emit_segment_line(dmt
, seg
, params
, paramsize
);
2046 case SEG_REPLICATOR
:
2047 if ((r
= _replicator_emit_segment_line(seg
, params
, paramsize
,
2053 case SEG_REPLICATOR_DEV
:
2054 if (!seg
->replicator
|| !_build_dev_string(originbuf
,
2059 EMIT_PARAMS(pos
, "%s %" PRIu64
, originbuf
, seg
->rdevice_index
);
2062 case SEG_SNAPSHOT_MERGE
:
2063 if (!_build_dev_string(originbuf
, sizeof(originbuf
), seg
->origin
))
2065 if (!_build_dev_string(cowbuf
, sizeof(cowbuf
), seg
->cow
))
2067 EMIT_PARAMS(pos
, "%s %s %c %d", originbuf
, cowbuf
,
2068 seg
->persistent
? 'P' : 'N', seg
->chunk_size
);
2070 case SEG_SNAPSHOT_ORIGIN
:
2071 if (!_build_dev_string(originbuf
, sizeof(originbuf
), seg
->origin
))
2073 EMIT_PARAMS(pos
, "%s", originbuf
);
2076 EMIT_PARAMS(pos
, "%u %u ", seg
->area_count
, seg
->stripe_size
);
2079 EMIT_PARAMS(pos
, "%s%s%s%s%s %s %" PRIu64
" ", seg
->cipher
,
2080 seg
->chainmode
? "-" : "", seg
->chainmode
?: "",
2081 seg
->iv
? "-" : "", seg
->iv
?: "", seg
->key
,
2082 seg
->iv_offset
!= DM_CRYPT_IV_DEFAULT
?
2083 seg
->iv_offset
: *seg_start
);
2094 target_type_is_raid
= 1;
2095 r
= _raid_emit_segment_line(dmt
, major
, minor
, seg
, seg_start
,
2102 if (!_build_dev_string(metadata
, sizeof(metadata
), seg
->metadata
))
2104 if (!_build_dev_string(pool
, sizeof(pool
), seg
->pool
))
2106 EMIT_PARAMS(pos
, "%s %s %d %" PRIu64
" %s", metadata
, pool
,
2107 seg
->data_block_size
, seg
->low_water_mark
,
2108 seg
->skip_block_zeroing
? "1 skip_block_zeroing" : "0");
2111 if (!_build_dev_string(pool
, sizeof(pool
), seg
->pool
))
2113 EMIT_PARAMS(pos
, "%s %d", pool
, seg
->device_id
);
2119 case SEG_REPLICATOR
:
2121 case SEG_SNAPSHOT_ORIGIN
:
2122 case SEG_SNAPSHOT_MERGE
:
2129 case SEG_REPLICATOR_DEV
:
2131 if ((r
= _emit_areas_line(dmt
, seg
, params
, paramsize
, &pos
)) <= 0) {
2136 log_error("No parameters supplied for %s target "
2137 "%u:%u.", dm_segtypes
[seg
->type
].target
,
2144 log_debug("Adding target to (%" PRIu32
":%" PRIu32
"): %" PRIu64
2145 " %" PRIu64
" %s %s", major
, minor
,
2146 *seg_start
, seg
->size
, target_type_is_raid
? "raid" :
2147 dm_segtypes
[seg
->type
].target
, params
);
2149 if (!dm_task_add_target(dmt
, *seg_start
, seg
->size
,
2150 target_type_is_raid
? "raid" :
2151 dm_segtypes
[seg
->type
].target
, params
))
2154 *seg_start
+= seg
->size
;
2161 static int _emit_segment(struct dm_task
*dmt
, uint32_t major
, uint32_t minor
,
2162 struct load_segment
*seg
, uint64_t *seg_start
)
2165 size_t paramsize
= 4096;
2169 if (!(params
= dm_malloc(paramsize
))) {
2170 log_error("Insufficient space for target parameters.");
2175 ret
= _emit_segment_line(dmt
, major
, minor
, seg
, seg_start
,
2185 log_debug("Insufficient space in params[%" PRIsize_t
2186 "] for target parameters.", paramsize
);
2189 } while (paramsize
< MAX_TARGET_PARAMSIZE
);
2191 log_error("Target parameter size too big. Aborting.");
2195 static int _load_node(struct dm_tree_node
*dnode
)
2198 struct dm_task
*dmt
;
2199 struct load_segment
*seg
;
2200 uint64_t seg_start
= 0, existing_table_size
;
2202 log_verbose("Loading %s table (%" PRIu32
":%" PRIu32
")", dnode
->name
,
2203 dnode
->info
.major
, dnode
->info
.minor
);
2205 if (!(dmt
= dm_task_create(DM_DEVICE_RELOAD
))) {
2206 log_error("Reload dm_task creation failed for %s", dnode
->name
);
2210 if (!dm_task_set_major(dmt
, dnode
->info
.major
) ||
2211 !dm_task_set_minor(dmt
, dnode
->info
.minor
)) {
2212 log_error("Failed to set device number for %s reload.", dnode
->name
);
2216 if (dnode
->props
.read_only
&& !dm_task_set_ro(dmt
)) {
2217 log_error("Failed to set read only flag for %s", dnode
->name
);
2221 if (!dm_task_no_open_count(dmt
))
2222 log_error("Failed to disable open_count");
2224 dm_list_iterate_items(seg
, &dnode
->props
.segs
)
2225 if (!_emit_segment(dmt
, dnode
->info
.major
, dnode
->info
.minor
,
2229 if (!dm_task_suppress_identical_reload(dmt
))
2230 log_error("Failed to suppress reload of identical tables.");
2232 if ((r
= dm_task_run(dmt
))) {
2233 r
= dm_task_get_info(dmt
, &dnode
->info
);
2234 if (r
&& !dnode
->info
.inactive_table
)
2235 log_verbose("Suppressed %s identical table reload.",
2238 existing_table_size
= dm_task_get_existing_table_size(dmt
);
2239 if ((dnode
->props
.size_changed
=
2240 (existing_table_size
== seg_start
) ? 0 : 1)) {
2241 log_debug("Table size changed from %" PRIu64
" to %"
2242 PRIu64
" for %s", existing_table_size
,
2243 seg_start
, dnode
->name
);
2245 * Kernel usually skips size validation on zero-length devices
2246 * now so no need to preload them.
2248 /* FIXME In which kernel version did this begin? */
2249 if (!existing_table_size
&& dnode
->props
.delay_resume_if_new
)
2250 dnode
->props
.size_changed
= 0;
2254 dnode
->props
.segment_count
= 0;
2257 dm_task_destroy(dmt
);
2262 int dm_tree_preload_children(struct dm_tree_node
*dnode
,
2263 const char *uuid_prefix
,
2264 size_t uuid_prefix_len
)
2267 void *handle
= NULL
;
2268 struct dm_tree_node
*child
;
2269 struct dm_info newinfo
;
2270 int update_devs_flag
= 0;
2272 /* Preload children first */
2273 while ((child
= dm_tree_next_child(&handle
, dnode
, 0))) {
2274 /* Skip existing non-device-mapper devices */
2275 if (!child
->info
.exists
&& child
->info
.major
)
2278 /* Ignore if it doesn't belong to this VG */
2279 if (child
->info
.exists
&&
2280 !_uuid_prefix_matches(child
->uuid
, uuid_prefix
, uuid_prefix_len
))
2283 if (dm_tree_node_num_children(child
, 0))
2284 if (!dm_tree_preload_children(child
, uuid_prefix
, uuid_prefix_len
))
2287 /* FIXME Cope if name exists with no uuid? */
2288 if (!child
->info
.exists
&& !_create_node(child
))
2291 if (!child
->info
.inactive_table
&&
2292 child
->props
.segment_count
&&
2296 /* Propagate device size change change */
2297 if (child
->props
.size_changed
)
2298 dnode
->props
.size_changed
= 1;
2300 /* Resume device immediately if it has parents and its size changed */
2301 if (!dm_tree_node_num_children(child
, 1) || !child
->props
.size_changed
)
2304 if (!child
->info
.inactive_table
&& !child
->info
.suspended
)
2307 if (!_resume_node(child
->name
, child
->info
.major
, child
->info
.minor
,
2308 child
->props
.read_ahead
, child
->props
.read_ahead_flags
,
2309 &newinfo
, &child
->dtree
->cookie
, child
->udev_flags
,
2310 child
->info
.suspended
)) {
2311 log_error("Unable to resume %s (%" PRIu32
2312 ":%" PRIu32
")", child
->name
, child
->info
.major
,
2318 /* Update cached info */
2319 child
->info
= newinfo
;
2322 * Prepare for immediate synchronization with udev and flush all stacked
2323 * dev node operations if requested by immediate_dev_node property. But
2324 * finish processing current level in the tree first.
2326 if (child
->props
.immediate_dev_node
)
2327 update_devs_flag
= 1;
2332 if (update_devs_flag
) {
2333 if (!dm_udev_wait(dm_tree_get_cookie(dnode
)))
2335 dm_tree_set_cookie(dnode
, 0);
2338 if (r
&& !_node_send_messages(dnode
, uuid_prefix
, uuid_prefix_len
)) {
2340 if (!(dm_tree_deactivate_children(dnode
, uuid_prefix
, uuid_prefix_len
)))
2341 log_error("Failed to deactivate %s", dnode
->name
);
2349 * Returns 1 if unsure.
2351 int dm_tree_children_use_uuid(struct dm_tree_node
*dnode
,
2352 const char *uuid_prefix
,
2353 size_t uuid_prefix_len
)
2355 void *handle
= NULL
;
2356 struct dm_tree_node
*child
= dnode
;
2359 while ((child
= dm_tree_next_child(&handle
, dnode
, 0))) {
2360 if (!(uuid
= dm_tree_node_get_uuid(child
))) {
2361 log_error("Failed to get uuid for dtree node.");
2365 if (_uuid_prefix_matches(uuid
, uuid_prefix
, uuid_prefix_len
))
2368 if (dm_tree_node_num_children(child
, 0))
2369 dm_tree_children_use_uuid(child
, uuid_prefix
, uuid_prefix_len
);
2378 static struct load_segment
*_add_segment(struct dm_tree_node
*dnode
, unsigned type
, uint64_t size
)
2380 struct load_segment
*seg
;
2382 if (!(seg
= dm_pool_zalloc(dnode
->dtree
->mem
, sizeof(*seg
)))) {
2383 log_error("dtree node segment allocation failed");
2389 seg
->area_count
= 0;
2390 dm_list_init(&seg
->areas
);
2391 seg
->stripe_size
= 0;
2392 seg
->persistent
= 0;
2393 seg
->chunk_size
= 0;
2398 dm_list_add(&dnode
->props
.segs
, &seg
->list
);
2399 dnode
->props
.segment_count
++;
2404 int dm_tree_node_add_snapshot_origin_target(struct dm_tree_node
*dnode
,
2406 const char *origin_uuid
)
2408 struct load_segment
*seg
;
2409 struct dm_tree_node
*origin_node
;
2411 if (!(seg
= _add_segment(dnode
, SEG_SNAPSHOT_ORIGIN
, size
)))
2414 if (!(origin_node
= dm_tree_find_node_by_uuid(dnode
->dtree
, origin_uuid
))) {
2415 log_error("Couldn't find snapshot origin uuid %s.", origin_uuid
);
2419 seg
->origin
= origin_node
;
2420 if (!_link_tree_nodes(dnode
, origin_node
))
2423 /* Resume snapshot origins after new snapshots */
2424 dnode
->activation_priority
= 1;
2429 static int _add_snapshot_target(struct dm_tree_node
*node
,
2431 const char *origin_uuid
,
2432 const char *cow_uuid
,
2433 const char *merge_uuid
,
2435 uint32_t chunk_size
)
2437 struct load_segment
*seg
;
2438 struct dm_tree_node
*origin_node
, *cow_node
, *merge_node
;
2441 seg_type
= !merge_uuid
? SEG_SNAPSHOT
: SEG_SNAPSHOT_MERGE
;
2443 if (!(seg
= _add_segment(node
, seg_type
, size
)))
2446 if (!(origin_node
= dm_tree_find_node_by_uuid(node
->dtree
, origin_uuid
))) {
2447 log_error("Couldn't find snapshot origin uuid %s.", origin_uuid
);
2451 seg
->origin
= origin_node
;
2452 if (!_link_tree_nodes(node
, origin_node
))
2455 if (!(cow_node
= dm_tree_find_node_by_uuid(node
->dtree
, cow_uuid
))) {
2456 log_error("Couldn't find snapshot COW device uuid %s.", cow_uuid
);
2460 seg
->cow
= cow_node
;
2461 if (!_link_tree_nodes(node
, cow_node
))
2464 seg
->persistent
= persistent
? 1 : 0;
2465 seg
->chunk_size
= chunk_size
;
2468 if (!(merge_node
= dm_tree_find_node_by_uuid(node
->dtree
, merge_uuid
))) {
2469 /* not a pure error, merging snapshot may have been deactivated */
2470 log_verbose("Couldn't find merging snapshot uuid %s.", merge_uuid
);
2472 seg
->merge
= merge_node
;
2473 /* must not link merging snapshot, would undermine activation_priority below */
2476 /* Resume snapshot-merge (acting origin) after other snapshots */
2477 node
->activation_priority
= 1;
2479 /* Resume merging snapshot after snapshot-merge */
2480 seg
->merge
->activation_priority
= 2;
2488 int dm_tree_node_add_snapshot_target(struct dm_tree_node
*node
,
2490 const char *origin_uuid
,
2491 const char *cow_uuid
,
2493 uint32_t chunk_size
)
2495 return _add_snapshot_target(node
, size
, origin_uuid
, cow_uuid
,
2496 NULL
, persistent
, chunk_size
);
2499 int dm_tree_node_add_snapshot_merge_target(struct dm_tree_node
*node
,
2501 const char *origin_uuid
,
2502 const char *cow_uuid
,
2503 const char *merge_uuid
,
2504 uint32_t chunk_size
)
2506 return _add_snapshot_target(node
, size
, origin_uuid
, cow_uuid
,
2507 merge_uuid
, 1, chunk_size
);
2510 int dm_tree_node_add_error_target(struct dm_tree_node
*node
,
2513 if (!_add_segment(node
, SEG_ERROR
, size
))
2519 int dm_tree_node_add_zero_target(struct dm_tree_node
*node
,
2522 if (!_add_segment(node
, SEG_ZERO
, size
))
2528 int dm_tree_node_add_linear_target(struct dm_tree_node
*node
,
2531 if (!_add_segment(node
, SEG_LINEAR
, size
))
2537 int dm_tree_node_add_striped_target(struct dm_tree_node
*node
,
2539 uint32_t stripe_size
)
2541 struct load_segment
*seg
;
2543 if (!(seg
= _add_segment(node
, SEG_STRIPED
, size
)))
2546 seg
->stripe_size
= stripe_size
;
2551 int dm_tree_node_add_crypt_target(struct dm_tree_node
*node
,
2554 const char *chainmode
,
2559 struct load_segment
*seg
;
2561 if (!(seg
= _add_segment(node
, SEG_CRYPT
, size
)))
2564 seg
->cipher
= cipher
;
2565 seg
->chainmode
= chainmode
;
2567 seg
->iv_offset
= iv_offset
;
2573 int dm_tree_node_add_mirror_target_log(struct dm_tree_node
*node
,
2574 uint32_t region_size
,
2576 const char *log_uuid
,
2577 unsigned area_count
,
2580 struct dm_tree_node
*log_node
= NULL
;
2581 struct load_segment
*seg
;
2583 if (!node
->props
.segment_count
) {
2584 log_error(INTERNAL_ERROR
"Attempt to add target area to missing segment.");
2588 seg
= dm_list_item(dm_list_last(&node
->props
.segs
), struct load_segment
);
2591 if (!(seg
->uuid
= dm_pool_strdup(node
->dtree
->mem
, log_uuid
))) {
2592 log_error("log uuid pool_strdup failed");
2595 if ((flags
& DM_CORELOG
))
2596 /* For pvmove: immediate resume (for size validation) isn't needed. */
2597 node
->props
.delay_resume_if_new
= 1;
2599 if (!(log_node
= dm_tree_find_node_by_uuid(node
->dtree
, log_uuid
))) {
2600 log_error("Couldn't find mirror log uuid %s.", log_uuid
);
2605 log_node
->props
.immediate_dev_node
= 1;
2607 /* The kernel validates the size of disk logs. */
2608 /* FIXME Propagate to any devices below */
2609 log_node
->props
.delay_resume_if_new
= 0;
2611 if (!_link_tree_nodes(node
, log_node
))
2616 seg
->log
= log_node
;
2617 seg
->region_size
= region_size
;
2618 seg
->clustered
= clustered
;
2619 seg
->mirror_area_count
= area_count
;
2625 int dm_tree_node_add_mirror_target(struct dm_tree_node
*node
,
2628 if (!_add_segment(node
, SEG_MIRRORED
, size
))
2634 int dm_tree_node_add_raid_target(struct dm_tree_node
*node
,
2636 const char *raid_type
,
2637 uint32_t region_size
,
2638 uint32_t stripe_size
,
2643 struct load_segment
*seg
= NULL
;
2645 for (i
= 0; dm_segtypes
[i
].target
&& !seg
; i
++)
2646 if (!strcmp(raid_type
, dm_segtypes
[i
].target
))
2647 if (!(seg
= _add_segment(node
,
2648 dm_segtypes
[i
].type
, size
)))
2654 seg
->region_size
= region_size
;
2655 seg
->stripe_size
= stripe_size
;
2656 seg
->area_count
= 0;
2657 seg
->rebuilds
= rebuilds
;
2662 int dm_tree_node_add_replicator_target(struct dm_tree_node
*node
,
2664 const char *rlog_uuid
,
2665 const char *rlog_type
,
2666 unsigned rsite_index
,
2667 dm_replicator_mode_t mode
,
2668 uint32_t async_timeout
,
2669 uint64_t fall_behind_data
,
2670 uint32_t fall_behind_ios
)
2672 struct load_segment
*rseg
;
2673 struct replicator_site
*rsite
;
2675 /* Local site0 - adds replicator segment and links rlog device */
2676 if (rsite_index
== REPLICATOR_LOCAL_SITE
) {
2677 if (node
->props
.segment_count
) {
2678 log_error(INTERNAL_ERROR
"Attempt to add replicator segment to already used node.");
2682 if (!(rseg
= _add_segment(node
, SEG_REPLICATOR
, size
)))
2685 if (!(rseg
->log
= dm_tree_find_node_by_uuid(node
->dtree
, rlog_uuid
))) {
2686 log_error("Missing replicator log uuid %s.", rlog_uuid
);
2690 if (!_link_tree_nodes(node
, rseg
->log
))
2693 if (strcmp(rlog_type
, "ringbuffer") != 0) {
2694 log_error("Unsupported replicator log type %s.", rlog_type
);
2698 if (!(rseg
->rlog_type
= dm_pool_strdup(node
->dtree
->mem
, rlog_type
)))
2701 dm_list_init(&rseg
->rsites
);
2702 rseg
->rdevice_count
= 0;
2703 node
->activation_priority
= 1;
2706 /* Add site to segment */
2707 if (mode
== DM_REPLICATOR_SYNC
2708 && (async_timeout
|| fall_behind_ios
|| fall_behind_data
)) {
2709 log_error("Async parameters passed for synchronnous replicator.");
2713 if (node
->props
.segment_count
!= 1) {
2714 log_error(INTERNAL_ERROR
"Attempt to add remote site area before setting replicator log.");
2718 rseg
= dm_list_item(dm_list_last(&node
->props
.segs
), struct load_segment
);
2719 if (rseg
->type
!= SEG_REPLICATOR
) {
2720 log_error(INTERNAL_ERROR
"Attempt to use non replicator segment %s.",
2721 dm_segtypes
[rseg
->type
].target
);
2725 if (!(rsite
= dm_pool_zalloc(node
->dtree
->mem
, sizeof(*rsite
)))) {
2726 log_error("Failed to allocate remote site segment.");
2730 dm_list_add(&rseg
->rsites
, &rsite
->list
);
2731 rseg
->rsite_count
++;
2734 rsite
->async_timeout
= async_timeout
;
2735 rsite
->fall_behind_data
= fall_behind_data
;
2736 rsite
->fall_behind_ios
= fall_behind_ios
;
2737 rsite
->rsite_index
= rsite_index
;
2742 /* Appends device node to Replicator */
2743 int dm_tree_node_add_replicator_dev_target(struct dm_tree_node
*node
,
2745 const char *replicator_uuid
,
2746 uint64_t rdevice_index
,
2747 const char *rdev_uuid
,
2748 unsigned rsite_index
,
2749 const char *slog_uuid
,
2750 uint32_t slog_flags
,
2751 uint32_t slog_region_size
)
2753 struct seg_area
*area
;
2754 struct load_segment
*rseg
;
2755 struct load_segment
*rep_seg
;
2757 if (rsite_index
== REPLICATOR_LOCAL_SITE
) {
2758 /* Site index for local target */
2759 if (!(rseg
= _add_segment(node
, SEG_REPLICATOR_DEV
, size
)))
2762 if (!(rseg
->replicator
= dm_tree_find_node_by_uuid(node
->dtree
, replicator_uuid
))) {
2763 log_error("Missing replicator uuid %s.", replicator_uuid
);
2767 /* Local slink0 for replicator must be always initialized first */
2768 if (rseg
->replicator
->props
.segment_count
!= 1) {
2769 log_error(INTERNAL_ERROR
"Attempt to use non replicator segment.");
2773 rep_seg
= dm_list_item(dm_list_last(&rseg
->replicator
->props
.segs
), struct load_segment
);
2774 if (rep_seg
->type
!= SEG_REPLICATOR
) {
2775 log_error(INTERNAL_ERROR
"Attempt to use non replicator segment %s.",
2776 dm_segtypes
[rep_seg
->type
].target
);
2779 rep_seg
->rdevice_count
++;
2781 if (!_link_tree_nodes(node
, rseg
->replicator
))
2784 rseg
->rdevice_index
= rdevice_index
;
2786 /* Local slink0 for replicator must be always initialized first */
2787 if (node
->props
.segment_count
!= 1) {
2788 log_error(INTERNAL_ERROR
"Attempt to use non replicator-dev segment.");
2792 rseg
= dm_list_item(dm_list_last(&node
->props
.segs
), struct load_segment
);
2793 if (rseg
->type
!= SEG_REPLICATOR_DEV
) {
2794 log_error(INTERNAL_ERROR
"Attempt to use non replicator-dev segment %s.",
2795 dm_segtypes
[rseg
->type
].target
);
2800 if (!(slog_flags
& DM_CORELOG
) && !slog_uuid
) {
2801 log_error("Unspecified sync log uuid.");
2805 if (!dm_tree_node_add_target_area(node
, NULL
, rdev_uuid
, 0))
2808 area
= dm_list_item(dm_list_last(&rseg
->areas
), struct seg_area
);
2810 if (!(slog_flags
& DM_CORELOG
)) {
2811 if (!(area
->slog
= dm_tree_find_node_by_uuid(node
->dtree
, slog_uuid
))) {
2812 log_error("Couldn't find sync log uuid %s.", slog_uuid
);
2816 if (!_link_tree_nodes(node
, area
->slog
))
2820 area
->flags
= slog_flags
;
2821 area
->region_size
= slog_region_size
;
2822 area
->rsite_index
= rsite_index
;
2827 static int _thin_validate_device_id(uint32_t device_id
)
2829 if (device_id
> DM_THIN_MAX_DEVICE_ID
) {
2830 log_error("Device id %u is higher then %u.",
2831 device_id
, DM_THIN_MAX_DEVICE_ID
);
2838 int dm_tree_node_add_thin_pool_target(struct dm_tree_node
*node
,
2840 uint64_t transaction_id
,
2841 const char *metadata_uuid
,
2842 const char *pool_uuid
,
2843 uint32_t data_block_size
,
2844 uint64_t low_water_mark
,
2845 unsigned skip_block_zeroing
)
2847 struct load_segment
*seg
;
2849 if (data_block_size
< DM_THIN_MIN_DATA_BLOCK_SIZE
) {
2850 log_error("Data block size %u is lower then %u sectors.",
2851 data_block_size
, DM_THIN_MIN_DATA_BLOCK_SIZE
);
2855 if (data_block_size
> DM_THIN_MAX_DATA_BLOCK_SIZE
) {
2856 log_error("Data block size %u is higher then %u sectors.",
2857 data_block_size
, DM_THIN_MAX_DATA_BLOCK_SIZE
);
2861 if (!(seg
= _add_segment(node
, SEG_THIN_POOL
, size
)))
2864 if (!(seg
->metadata
= dm_tree_find_node_by_uuid(node
->dtree
, metadata_uuid
))) {
2865 log_error("Missing metadata uuid %s.", metadata_uuid
);
2869 if (!_link_tree_nodes(node
, seg
->metadata
))
2872 if (!(seg
->pool
= dm_tree_find_node_by_uuid(node
->dtree
, pool_uuid
))) {
2873 log_error("Missing pool uuid %s.", pool_uuid
);
2877 if (!_link_tree_nodes(node
, seg
->pool
))
2880 node
->props
.thin_pool_transaction_id
= transaction_id
; // compare on resume
2881 seg
->low_water_mark
= low_water_mark
;
2882 seg
->data_block_size
= data_block_size
;
2883 seg
->skip_block_zeroing
= skip_block_zeroing
;
2884 dm_list_init(&seg
->thin_messages
);
2889 int dm_tree_node_add_thin_pool_message(struct dm_tree_node
*node
,
2890 const struct dm_thin_message
*message
)
2892 struct load_segment
*seg
;
2893 struct thin_message
*tm
;
2895 if (node
->props
.segment_count
!= 1) {
2896 log_error("Thin pool node must have only one segment.");
2900 seg
= dm_list_item(dm_list_last(&node
->props
.segs
), struct load_segment
);
2901 if (seg
->type
!= SEG_THIN_POOL
) {
2902 log_error("Thin pool node has segment type %s.",
2903 dm_segtypes
[seg
->type
].target
);
2907 if (!(tm
= dm_pool_zalloc(node
->dtree
->mem
, sizeof (*tm
)))) {
2908 log_error("Failed to allocate thin message.");
2912 switch (message
->type
) {
2913 case DM_THIN_MESSAGE_CREATE_SNAP
:
2914 /* If the thin origin is active, it must be suspend first! */
2915 if (message
->u
.m_create_snap
.device_id
== message
->u
.m_create_snap
.origin_id
) {
2916 log_error("Cannot use same device id for origin and its snapshot.");
2919 if (!_thin_validate_device_id(message
->u
.m_create_snap
.device_id
) ||
2920 !_thin_validate_device_id(message
->u
.m_create_snap
.origin_id
))
2922 tm
->message
.u
.m_create_snap
= message
->u
.m_create_snap
;
2924 case DM_THIN_MESSAGE_CREATE_THIN
:
2925 if (!_thin_validate_device_id(message
->u
.m_create_thin
.device_id
))
2927 tm
->message
.u
.m_create_thin
= message
->u
.m_create_thin
;
2928 tm
->expected_errno
= EEXIST
;
2930 case DM_THIN_MESSAGE_DELETE
:
2931 if (!_thin_validate_device_id(message
->u
.m_delete
.device_id
))
2933 tm
->message
.u
.m_delete
= message
->u
.m_delete
;
2934 tm
->expected_errno
= ENODATA
;
2936 case DM_THIN_MESSAGE_TRIM
:
2937 if (!_thin_validate_device_id(message
->u
.m_trim
.device_id
))
2939 tm
->message
.u
.m_trim
= message
->u
.m_trim
;
2941 case DM_THIN_MESSAGE_SET_TRANSACTION_ID
:
2942 if (message
->u
.m_set_transaction_id
.current_id
!=
2943 (message
->u
.m_set_transaction_id
.new_id
- 1)) {
2944 log_error("New transaction_id must be sequential.");
2945 return 0; /* FIXME: Maybe too strict here? */
2947 tm
->message
.u
.m_set_transaction_id
= message
->u
.m_set_transaction_id
;
2950 log_error("Unsupported message type %d.", (int) message
->type
);
2954 tm
->message
.type
= message
->type
;
2955 dm_list_add(&seg
->thin_messages
, &tm
->list
);
2960 int dm_tree_node_add_thin_target(struct dm_tree_node
*node
,
2962 const char *thin_pool_uuid
,
2965 struct load_segment
*seg
;
2967 if (!_thin_validate_device_id(device_id
))
2970 if (!(seg
= _add_segment(node
, SEG_THIN
, size
)))
2973 if (!(seg
->pool
= dm_tree_find_node_by_uuid(node
->dtree
, thin_pool_uuid
))) {
2974 log_error("Missing thin pool uuid %s.", thin_pool_uuid
);
2978 if (!_link_tree_nodes(node
, seg
->pool
))
2981 seg
->device_id
= device_id
;
2986 static int _add_area(struct dm_tree_node
*node
, struct load_segment
*seg
, struct dm_tree_node
*dev_node
, uint64_t offset
)
2988 struct seg_area
*area
;
2990 if (!(area
= dm_pool_zalloc(node
->dtree
->mem
, sizeof (*area
)))) {
2991 log_error("Failed to allocate target segment area.");
2995 area
->dev_node
= dev_node
;
2996 area
->offset
= offset
;
2998 dm_list_add(&seg
->areas
, &area
->list
);
3004 int dm_tree_node_add_target_area(struct dm_tree_node
*node
,
3005 const char *dev_name
,
3009 struct load_segment
*seg
;
3011 struct dm_tree_node
*dev_node
;
3013 if ((!dev_name
|| !*dev_name
) && (!uuid
|| !*uuid
)) {
3014 log_error("dm_tree_node_add_target_area called without device");
3019 if (!(dev_node
= dm_tree_find_node_by_uuid(node
->dtree
, uuid
))) {
3020 log_error("Couldn't find area uuid %s.", uuid
);
3023 if (!_link_tree_nodes(node
, dev_node
))
3026 if (stat(dev_name
, &info
) < 0) {
3027 log_error("Device %s not found.", dev_name
);
3031 if (!S_ISBLK(info
.st_mode
)) {
3032 log_error("Device %s is not a block device.", dev_name
);
3036 /* FIXME Check correct macro use */
3037 if (!(dev_node
= _add_dev(node
->dtree
, node
, MAJOR(info
.st_rdev
),
3038 MINOR(info
.st_rdev
), 0)))
3042 if (!node
->props
.segment_count
) {
3043 log_error(INTERNAL_ERROR
"Attempt to add target area to missing segment.");
3047 seg
= dm_list_item(dm_list_last(&node
->props
.segs
), struct load_segment
);
3049 if (!_add_area(node
, seg
, dev_node
, offset
))
3055 int dm_tree_node_add_null_area(struct dm_tree_node
*node
, uint64_t offset
)
3057 struct load_segment
*seg
;
3059 seg
= dm_list_item(dm_list_last(&node
->props
.segs
), struct load_segment
);
3061 switch (seg
->type
) {
3073 log_error("dm_tree_node_add_null_area() called on an unsupported segment type");
3077 if (!_add_area(node
, seg
, NULL
, offset
))
3083 void dm_tree_set_cookie(struct dm_tree_node
*node
, uint32_t cookie
)
3085 node
->dtree
->cookie
= cookie
;
3088 uint32_t dm_tree_get_cookie(struct dm_tree_node
*node
)
3090 return node
->dtree
->cookie
;