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 uint32_t data_block_size
; /* Thin_pool */
173 uint64_t low_water_mark
; /* Thin_pool */
174 unsigned skip_block_zeroeing
; /* Thin_pool */
175 uint32_t device_id
; /* Thin */
179 /* Per-device properties */
180 struct load_properties
{
186 uint32_t read_ahead_flags
;
188 unsigned segment_count
;
189 unsigned size_changed
;
192 const char *new_name
;
194 /* If immediate_dev_node is set to 1, try to create the dev node
195 * as soon as possible (e.g. in preload stage even during traversal
196 * and processing of dm tree). This will also flush all stacked dev
197 * node operations, synchronizing with udev.
199 unsigned immediate_dev_node
;
202 * If the device size changed from zero and this is set,
203 * don't resume the device immediately, even if the device
204 * has parents. This works provided the parents do not
205 * validate the device size and is required by pvmove to
206 * avoid starting the mirror resync operation too early.
208 unsigned delay_resume_if_new
;
211 /* Two of these used to join two nodes with uses and used_by. */
212 struct dm_tree_link
{
214 struct dm_tree_node
*node
;
217 struct dm_tree_node
{
218 struct dm_tree
*dtree
;
224 struct dm_list uses
; /* Nodes this node uses */
225 struct dm_list used_by
; /* Nodes that use this node */
227 int activation_priority
; /* 0 gets activated first */
229 uint16_t udev_flags
; /* Udev control flags */
231 void *context
; /* External supplied context */
233 struct load_properties props
; /* For creation/table (re)load */
236 * If presuspend of child node is needed
237 * Note: only direct child is allowed
239 struct dm_tree_node
*presuspend_node
;
244 struct dm_hash_table
*devs
;
245 struct dm_hash_table
*uuids
;
246 struct dm_tree_node root
;
247 int skip_lockfs
; /* 1 skips lockfs (for non-snapshots) */
248 int no_flush
; /* 1 sets noflush (mirrors/multipath) */
249 int retry_remove
; /* 1 retries remove if not successful */
253 struct dm_tree
*dm_tree_create(void)
255 struct dm_tree
*dtree
;
257 if (!(dtree
= dm_zalloc(sizeof(*dtree
)))) {
258 log_error("dm_tree_create malloc failed");
262 dtree
->root
.dtree
= dtree
;
263 dm_list_init(&dtree
->root
.uses
);
264 dm_list_init(&dtree
->root
.used_by
);
265 dtree
->skip_lockfs
= 0;
268 if (!(dtree
->mem
= dm_pool_create("dtree", 1024))) {
269 log_error("dtree pool creation failed");
274 if (!(dtree
->devs
= dm_hash_create(8))) {
275 log_error("dtree hash creation failed");
276 dm_pool_destroy(dtree
->mem
);
281 if (!(dtree
->uuids
= dm_hash_create(32))) {
282 log_error("dtree uuid hash creation failed");
283 dm_hash_destroy(dtree
->devs
);
284 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
);
303 static int _nodes_are_linked(const struct dm_tree_node
*parent
,
304 const struct dm_tree_node
*child
)
306 struct dm_tree_link
*dlink
;
308 dm_list_iterate_items(dlink
, &parent
->uses
)
309 if (dlink
->node
== child
)
315 static int _link(struct dm_list
*list
, struct dm_tree_node
*node
)
317 struct dm_tree_link
*dlink
;
319 if (!(dlink
= dm_pool_alloc(node
->dtree
->mem
, sizeof(*dlink
)))) {
320 log_error("dtree link allocation failed");
325 dm_list_add(list
, &dlink
->list
);
330 static int _link_nodes(struct dm_tree_node
*parent
,
331 struct dm_tree_node
*child
)
333 if (_nodes_are_linked(parent
, child
))
336 if (!_link(&parent
->uses
, child
))
339 if (!_link(&child
->used_by
, parent
))
345 static void _unlink(struct dm_list
*list
, struct dm_tree_node
*node
)
347 struct dm_tree_link
*dlink
;
349 dm_list_iterate_items(dlink
, list
)
350 if (dlink
->node
== node
) {
351 dm_list_del(&dlink
->list
);
356 static void _unlink_nodes(struct dm_tree_node
*parent
,
357 struct dm_tree_node
*child
)
359 if (!_nodes_are_linked(parent
, child
))
362 _unlink(&parent
->uses
, child
);
363 _unlink(&child
->used_by
, parent
);
366 static int _add_to_toplevel(struct dm_tree_node
*node
)
368 return _link_nodes(&node
->dtree
->root
, node
);
371 static void _remove_from_toplevel(struct dm_tree_node
*node
)
373 _unlink_nodes(&node
->dtree
->root
, node
);
376 static int _add_to_bottomlevel(struct dm_tree_node
*node
)
378 return _link_nodes(node
, &node
->dtree
->root
);
381 static void _remove_from_bottomlevel(struct dm_tree_node
*node
)
383 _unlink_nodes(node
, &node
->dtree
->root
);
386 static int _link_tree_nodes(struct dm_tree_node
*parent
, struct dm_tree_node
*child
)
388 /* Don't link to root node if child already has a parent */
389 if (parent
== &parent
->dtree
->root
) {
390 if (dm_tree_node_num_children(child
, 1))
393 _remove_from_toplevel(child
);
395 if (child
== &child
->dtree
->root
) {
396 if (dm_tree_node_num_children(parent
, 0))
399 _remove_from_bottomlevel(parent
);
401 return _link_nodes(parent
, child
);
404 static struct dm_tree_node
*_create_dm_tree_node(struct dm_tree
*dtree
,
407 struct dm_info
*info
,
411 struct dm_tree_node
*node
;
414 if (!(node
= dm_pool_zalloc(dtree
->mem
, sizeof(*node
)))) {
415 log_error("_create_dm_tree_node alloc failed");
424 node
->context
= context
;
425 node
->udev_flags
= udev_flags
;
426 node
->activation_priority
= 0;
428 dm_list_init(&node
->uses
);
429 dm_list_init(&node
->used_by
);
430 dm_list_init(&node
->props
.segs
);
432 dev
= MKDEV(info
->major
, info
->minor
);
434 if (!dm_hash_insert_binary(dtree
->devs
, (const char *) &dev
,
435 sizeof(dev
), node
)) {
436 log_error("dtree node hash insertion failed");
437 dm_pool_free(dtree
->mem
, node
);
442 !dm_hash_insert(dtree
->uuids
, uuid
, node
)) {
443 log_error("dtree uuid hash insertion failed");
444 dm_hash_remove_binary(dtree
->devs
, (const char *) &dev
,
446 dm_pool_free(dtree
->mem
, node
);
453 static struct dm_tree_node
*_find_dm_tree_node(struct dm_tree
*dtree
,
454 uint32_t major
, uint32_t minor
)
456 uint64_t dev
= MKDEV(major
, minor
);
458 return dm_hash_lookup_binary(dtree
->devs
, (const char *) &dev
,
462 static struct dm_tree_node
*_find_dm_tree_node_by_uuid(struct dm_tree
*dtree
,
465 struct dm_tree_node
*node
;
467 if ((node
= dm_hash_lookup(dtree
->uuids
, uuid
)))
470 if (strncmp(uuid
, UUID_PREFIX
, sizeof(UUID_PREFIX
) - 1))
473 return dm_hash_lookup(dtree
->uuids
, uuid
+ sizeof(UUID_PREFIX
) - 1);
476 static int _deps(struct dm_task
**dmt
, struct dm_pool
*mem
, uint32_t major
, uint32_t minor
,
477 const char **name
, const char **uuid
,
478 struct dm_info
*info
, struct dm_deps
**deps
)
480 memset(info
, 0, sizeof(*info
));
482 if (!dm_is_dm_major(major
)) {
489 info
->live_table
= 0;
490 info
->inactive_table
= 0;
495 if (!(*dmt
= dm_task_create(DM_DEVICE_DEPS
))) {
496 log_error("deps dm_task creation failed");
500 if (!dm_task_set_major(*dmt
, major
)) {
501 log_error("_deps: failed to set major for (%" PRIu32
":%" PRIu32
")",
506 if (!dm_task_set_minor(*dmt
, minor
)) {
507 log_error("_deps: failed to set minor for (%" PRIu32
":%" PRIu32
")",
512 if (!dm_task_run(*dmt
)) {
513 log_error("_deps: task run failed for (%" PRIu32
":%" PRIu32
")",
518 if (!dm_task_get_info(*dmt
, info
)) {
519 log_error("_deps: failed to get info for (%" PRIu32
":%" PRIu32
")",
529 if (info
->major
!= major
) {
530 log_error("Inconsistent dtree major number: %u != %u",
534 if (info
->minor
!= minor
) {
535 log_error("Inconsistent dtree minor number: %u != %u",
539 if (!(*name
= dm_pool_strdup(mem
, dm_task_get_name(*dmt
)))) {
540 log_error("name pool_strdup failed");
543 if (!(*uuid
= dm_pool_strdup(mem
, dm_task_get_uuid(*dmt
)))) {
544 log_error("uuid pool_strdup failed");
547 *deps
= dm_task_get_deps(*dmt
);
553 dm_task_destroy(*dmt
);
557 static struct dm_tree_node
*_add_dev(struct dm_tree
*dtree
,
558 struct dm_tree_node
*parent
,
559 uint32_t major
, uint32_t minor
,
562 struct dm_task
*dmt
= NULL
;
564 struct dm_deps
*deps
= NULL
;
565 const char *name
= NULL
;
566 const char *uuid
= NULL
;
567 struct dm_tree_node
*node
= NULL
;
571 /* Already in tree? */
572 if (!(node
= _find_dm_tree_node(dtree
, major
, minor
))) {
573 if (!_deps(&dmt
, dtree
->mem
, major
, minor
, &name
, &uuid
, &info
, &deps
))
576 if (!(node
= _create_dm_tree_node(dtree
, name
, uuid
, &info
,
582 if (!_link_tree_nodes(parent
, node
)) {
587 /* If node was already in tree, no need to recurse. */
591 /* Can't recurse if not a mapped device or there are no dependencies */
592 if (!node
->info
.exists
|| !deps
->count
) {
593 if (!_add_to_bottomlevel(node
)) {
600 /* Add dependencies to tree */
601 for (i
= 0; i
< deps
->count
; i
++)
602 if (!_add_dev(dtree
, node
, MAJOR(deps
->device
[i
]),
603 MINOR(deps
->device
[i
]), udev_flags
)) {
610 dm_task_destroy(dmt
);
615 static int _node_clear_table(struct dm_tree_node
*dnode
)
618 struct dm_info
*info
;
622 if (!(info
= &dnode
->info
)) {
623 log_error("_node_clear_table failed: missing info");
627 if (!(name
= dm_tree_node_get_name(dnode
))) {
628 log_error("_node_clear_table failed: missing name");
632 /* Is there a table? */
633 if (!info
->exists
|| !info
->inactive_table
)
636 // FIXME Get inactive deps. If any dev referenced has 1 opener and no live table, remove it after the clear.
638 log_verbose("Clearing inactive table %s (%" PRIu32
":%" PRIu32
")",
639 name
, info
->major
, info
->minor
);
641 if (!(dmt
= dm_task_create(DM_DEVICE_CLEAR
))) {
642 log_error("Table clear dm_task creation failed for %s", name
);
646 if (!dm_task_set_major(dmt
, info
->major
) ||
647 !dm_task_set_minor(dmt
, info
->minor
)) {
648 log_error("Failed to set device number for %s table clear", name
);
649 dm_task_destroy(dmt
);
653 r
= dm_task_run(dmt
);
655 if (!dm_task_get_info(dmt
, info
)) {
656 log_error("_node_clear_table failed: info missing after running task for %s", name
);
660 dm_task_destroy(dmt
);
665 struct dm_tree_node
*dm_tree_add_new_dev(struct dm_tree
*dtree
,
668 uint32_t major
, uint32_t minor
,
673 struct dm_tree_node
*dnode
;
678 /* Do we need to add node to tree? */
679 if (!(dnode
= dm_tree_find_node_by_uuid(dtree
, uuid
))) {
680 if (!(name2
= dm_pool_strdup(dtree
->mem
, name
))) {
681 log_error("name pool_strdup failed");
684 if (!(uuid2
= dm_pool_strdup(dtree
->mem
, uuid
))) {
685 log_error("uuid pool_strdup failed");
693 info
.inactive_table
= 0;
696 if (!(dnode
= _create_dm_tree_node(dtree
, name2
, uuid2
, &info
,
700 /* Attach to root node until a table is supplied */
701 if (!_add_to_toplevel(dnode
) || !_add_to_bottomlevel(dnode
))
704 dnode
->props
.major
= major
;
705 dnode
->props
.minor
= minor
;
706 dnode
->props
.new_name
= NULL
;
707 dnode
->props
.size_changed
= 0;
708 } else if (strcmp(name
, dnode
->name
)) {
709 /* Do we need to rename node? */
710 if (!(dnode
->props
.new_name
= dm_pool_strdup(dtree
->mem
, name
))) {
711 log_error("name pool_strdup failed");
716 dnode
->props
.read_only
= read_only
? 1 : 0;
717 dnode
->props
.read_ahead
= DM_READ_AHEAD_AUTO
;
718 dnode
->props
.read_ahead_flags
= 0;
720 if (clear_inactive
&& !_node_clear_table(dnode
))
723 dnode
->context
= context
;
724 dnode
->udev_flags
= 0;
729 struct dm_tree_node
*dm_tree_add_new_dev_with_udev_flags(struct dm_tree
*dtree
,
739 struct dm_tree_node
*node
;
741 if ((node
= dm_tree_add_new_dev(dtree
, name
, uuid
, major
, minor
, read_only
,
742 clear_inactive
, context
)))
743 node
->udev_flags
= udev_flags
;
749 void dm_tree_node_set_read_ahead(struct dm_tree_node
*dnode
,
751 uint32_t read_ahead_flags
)
753 dnode
->props
.read_ahead
= read_ahead
;
754 dnode
->props
.read_ahead_flags
= read_ahead_flags
;
757 void dm_tree_node_set_presuspend_node(struct dm_tree_node
*node
,
758 struct dm_tree_node
*presuspend_node
)
760 node
->presuspend_node
= presuspend_node
;
763 int dm_tree_add_dev(struct dm_tree
*dtree
, uint32_t major
, uint32_t minor
)
765 return _add_dev(dtree
, &dtree
->root
, major
, minor
, 0) ? 1 : 0;
768 int dm_tree_add_dev_with_udev_flags(struct dm_tree
*dtree
, uint32_t major
,
769 uint32_t minor
, uint16_t udev_flags
)
771 return _add_dev(dtree
, &dtree
->root
, major
, minor
, udev_flags
) ? 1 : 0;
774 const char *dm_tree_node_get_name(const struct dm_tree_node
*node
)
776 return node
->info
.exists
? node
->name
: "";
779 const char *dm_tree_node_get_uuid(const struct dm_tree_node
*node
)
781 return node
->info
.exists
? node
->uuid
: "";
784 const struct dm_info
*dm_tree_node_get_info(const struct dm_tree_node
*node
)
789 void *dm_tree_node_get_context(const struct dm_tree_node
*node
)
791 return node
->context
;
794 int dm_tree_node_size_changed(const struct dm_tree_node
*dnode
)
796 return dnode
->props
.size_changed
;
799 int dm_tree_node_num_children(const struct dm_tree_node
*node
, uint32_t inverted
)
802 if (_nodes_are_linked(&node
->dtree
->root
, node
))
804 return dm_list_size(&node
->used_by
);
807 if (_nodes_are_linked(node
, &node
->dtree
->root
))
810 return dm_list_size(&node
->uses
);
814 * Returns 1 if no prefix supplied
816 static int _uuid_prefix_matches(const char *uuid
, const char *uuid_prefix
, size_t uuid_prefix_len
)
821 if (!strncmp(uuid
, uuid_prefix
, uuid_prefix_len
))
824 /* Handle transition: active device uuids might be missing the prefix */
825 if (uuid_prefix_len
<= 4)
828 if (!strncmp(uuid
, UUID_PREFIX
, sizeof(UUID_PREFIX
) - 1))
831 if (strncmp(uuid_prefix
, UUID_PREFIX
, sizeof(UUID_PREFIX
) - 1))
834 if (!strncmp(uuid
, uuid_prefix
+ sizeof(UUID_PREFIX
) - 1, uuid_prefix_len
- (sizeof(UUID_PREFIX
) - 1)))
841 * Returns 1 if no children.
843 static int _children_suspended(struct dm_tree_node
*node
,
845 const char *uuid_prefix
,
846 size_t uuid_prefix_len
)
848 struct dm_list
*list
;
849 struct dm_tree_link
*dlink
;
850 const struct dm_info
*dinfo
;
854 if (_nodes_are_linked(&node
->dtree
->root
, node
))
856 list
= &node
->used_by
;
858 if (_nodes_are_linked(node
, &node
->dtree
->root
))
863 dm_list_iterate_items(dlink
, list
) {
864 if (!(uuid
= dm_tree_node_get_uuid(dlink
->node
))) {
869 /* Ignore if it doesn't belong to this VG */
870 if (!_uuid_prefix_matches(uuid
, uuid_prefix
, uuid_prefix_len
))
873 /* Ignore if parent node wants to presuspend this node */
874 if (dlink
->node
->presuspend_node
== node
)
877 if (!(dinfo
= dm_tree_node_get_info(dlink
->node
))) {
878 stack
; /* FIXME Is this normal? */
882 if (!dinfo
->suspended
)
890 * Set major and minor to zero for root of tree.
892 struct dm_tree_node
*dm_tree_find_node(struct dm_tree
*dtree
,
896 if (!major
&& !minor
)
899 return _find_dm_tree_node(dtree
, major
, minor
);
903 * Set uuid to NULL for root of tree.
905 struct dm_tree_node
*dm_tree_find_node_by_uuid(struct dm_tree
*dtree
,
911 return _find_dm_tree_node_by_uuid(dtree
, uuid
);
915 * First time set *handle to NULL.
916 * Set inverted to invert the tree.
918 struct dm_tree_node
*dm_tree_next_child(void **handle
,
919 const struct dm_tree_node
*parent
,
922 struct dm_list
**dlink
= (struct dm_list
**) handle
;
923 const struct dm_list
*use_list
;
926 use_list
= &parent
->used_by
;
928 use_list
= &parent
->uses
;
931 *dlink
= dm_list_first(use_list
);
933 *dlink
= dm_list_next(use_list
, *dlink
);
935 return (*dlink
) ? dm_list_item(*dlink
, struct dm_tree_link
)->node
: NULL
;
939 * Deactivate a device with its dependencies if the uuid prefix matches.
941 static int _info_by_dev(uint32_t major
, uint32_t minor
, int with_open_count
,
942 struct dm_info
*info
)
947 if (!(dmt
= dm_task_create(DM_DEVICE_INFO
))) {
948 log_error("_info_by_dev: dm_task creation failed");
952 if (!dm_task_set_major(dmt
, major
) || !dm_task_set_minor(dmt
, minor
)) {
953 log_error("_info_by_dev: Failed to set device number");
954 dm_task_destroy(dmt
);
958 if (!with_open_count
&& !dm_task_no_open_count(dmt
))
959 log_error("Failed to disable open_count");
961 if ((r
= dm_task_run(dmt
)))
962 r
= dm_task_get_info(dmt
, info
);
964 dm_task_destroy(dmt
);
969 static int _check_device_not_in_use(struct dm_info
*info
)
974 /* If sysfs is not used, use open_count information only. */
975 if (!*dm_sysfs_dir()) {
976 if (info
->open_count
) {
977 log_error("Device %" PRIu32
":%" PRIu32
" in use",
978 info
->major
, info
->minor
);
985 if (dm_device_has_holders(info
->major
, info
->minor
)) {
986 log_error("Device %" PRIu32
":%" PRIu32
" is used "
987 "by another device.", info
->major
, info
->minor
);
991 if (dm_device_has_mounted_fs(info
->major
, info
->minor
)) {
992 log_error("Device %" PRIu32
":%" PRIu32
" contains "
993 "a filesystem in use.", info
->major
, info
->minor
);
1000 /* Check if all parent nodes of given node have open_count == 0 */
1001 static int _node_has_closed_parents(struct dm_tree_node
*node
,
1002 const char *uuid_prefix
,
1003 size_t uuid_prefix_len
)
1005 struct dm_tree_link
*dlink
;
1006 const struct dm_info
*dinfo
;
1007 struct dm_info info
;
1010 /* Iterate through parents of this node */
1011 dm_list_iterate_items(dlink
, &node
->used_by
) {
1012 if (!(uuid
= dm_tree_node_get_uuid(dlink
->node
))) {
1017 /* Ignore if it doesn't belong to this VG */
1018 if (!_uuid_prefix_matches(uuid
, uuid_prefix
, uuid_prefix_len
))
1021 if (!(dinfo
= dm_tree_node_get_info(dlink
->node
))) {
1022 stack
; /* FIXME Is this normal? */
1026 /* Refresh open_count */
1027 if (!_info_by_dev(dinfo
->major
, dinfo
->minor
, 1, &info
) ||
1031 if (info
.open_count
) {
1032 log_debug("Node %s %d:%d has open_count %d", uuid_prefix
,
1033 dinfo
->major
, dinfo
->minor
, info
.open_count
);
1041 static int _deactivate_node(const char *name
, uint32_t major
, uint32_t minor
,
1042 uint32_t *cookie
, uint16_t udev_flags
, int retry
)
1044 struct dm_task
*dmt
;
1047 log_verbose("Removing %s (%" PRIu32
":%" PRIu32
")", name
, major
, minor
);
1049 if (!(dmt
= dm_task_create(DM_DEVICE_REMOVE
))) {
1050 log_error("Deactivation dm_task creation failed for %s", name
);
1054 if (!dm_task_set_major(dmt
, major
) || !dm_task_set_minor(dmt
, minor
)) {
1055 log_error("Failed to set device number for %s deactivation", name
);
1059 if (!dm_task_no_open_count(dmt
))
1060 log_error("Failed to disable open_count");
1062 if (!dm_task_set_cookie(dmt
, cookie
, udev_flags
))
1067 dm_task_retry_remove(dmt
);
1069 r
= dm_task_run(dmt
);
1071 /* FIXME Until kernel returns actual name so dm-iface.c can handle it */
1072 rm_dev_node(name
, dmt
->cookie_set
&& !(udev_flags
& DM_UDEV_DISABLE_DM_RULES_FLAG
),
1073 dmt
->cookie_set
&& (udev_flags
& DM_UDEV_DISABLE_LIBRARY_FALLBACK
));
1075 /* FIXME Remove node from tree or mark invalid? */
1078 dm_task_destroy(dmt
);
1083 static int _rename_node(const char *old_name
, const char *new_name
, uint32_t major
,
1084 uint32_t minor
, uint32_t *cookie
, uint16_t udev_flags
)
1086 struct dm_task
*dmt
;
1089 log_verbose("Renaming %s (%" PRIu32
":%" PRIu32
") to %s", old_name
, major
, minor
, new_name
);
1091 if (!(dmt
= dm_task_create(DM_DEVICE_RENAME
))) {
1092 log_error("Rename dm_task creation failed for %s", old_name
);
1096 if (!dm_task_set_name(dmt
, old_name
)) {
1097 log_error("Failed to set name for %s rename.", old_name
);
1101 if (!dm_task_set_newname(dmt
, new_name
))
1104 if (!dm_task_no_open_count(dmt
))
1105 log_error("Failed to disable open_count");
1107 if (!dm_task_set_cookie(dmt
, cookie
, udev_flags
))
1110 r
= dm_task_run(dmt
);
1113 dm_task_destroy(dmt
);
1118 /* FIXME Merge with _suspend_node? */
1119 static int _resume_node(const char *name
, uint32_t major
, uint32_t minor
,
1120 uint32_t read_ahead
, uint32_t read_ahead_flags
,
1121 struct dm_info
*newinfo
, uint32_t *cookie
,
1122 uint16_t udev_flags
, int already_suspended
)
1124 struct dm_task
*dmt
;
1127 log_verbose("Resuming %s (%" PRIu32
":%" PRIu32
")", name
, major
, minor
);
1129 if (!(dmt
= dm_task_create(DM_DEVICE_RESUME
))) {
1130 log_debug("Suspend dm_task creation failed for %s.", name
);
1134 /* FIXME Kernel should fill in name on return instead */
1135 if (!dm_task_set_name(dmt
, name
)) {
1136 log_debug("Failed to set device name for %s resumption.", name
);
1140 if (!dm_task_set_major(dmt
, major
) || !dm_task_set_minor(dmt
, minor
)) {
1141 log_error("Failed to set device number for %s resumption.", name
);
1145 if (!dm_task_no_open_count(dmt
))
1146 log_error("Failed to disable open_count");
1148 if (!dm_task_set_read_ahead(dmt
, read_ahead
, read_ahead_flags
))
1149 log_error("Failed to set read ahead");
1151 if (!dm_task_set_cookie(dmt
, cookie
, udev_flags
))
1154 if (!(r
= dm_task_run(dmt
)))
1157 if (already_suspended
)
1160 if (!(r
= dm_task_get_info(dmt
, newinfo
)))
1164 dm_task_destroy(dmt
);
1169 static int _suspend_node(const char *name
, uint32_t major
, uint32_t minor
,
1170 int skip_lockfs
, int no_flush
, struct dm_info
*newinfo
)
1172 struct dm_task
*dmt
;
1175 log_verbose("Suspending %s (%" PRIu32
":%" PRIu32
")%s%s",
1177 skip_lockfs
? "" : " with filesystem sync",
1178 no_flush
? "" : " with device flush");
1180 if (!(dmt
= dm_task_create(DM_DEVICE_SUSPEND
))) {
1181 log_error("Suspend dm_task creation failed for %s", name
);
1185 if (!dm_task_set_major(dmt
, major
) || !dm_task_set_minor(dmt
, minor
)) {
1186 log_error("Failed to set device number for %s suspension.", name
);
1187 dm_task_destroy(dmt
);
1191 if (!dm_task_no_open_count(dmt
))
1192 log_error("Failed to disable open_count");
1194 if (skip_lockfs
&& !dm_task_skip_lockfs(dmt
))
1195 log_error("Failed to set skip_lockfs flag.");
1197 if (no_flush
&& !dm_task_no_flush(dmt
))
1198 log_error("Failed to set no_flush flag.");
1200 if ((r
= dm_task_run(dmt
))) {
1202 r
= dm_task_get_info(dmt
, newinfo
);
1205 dm_task_destroy(dmt
);
1211 * FIXME Don't attempt to deactivate known internal dependencies.
1213 static int _dm_tree_deactivate_children(struct dm_tree_node
*dnode
,
1214 const char *uuid_prefix
,
1215 size_t uuid_prefix_len
,
1219 void *handle
= NULL
;
1220 struct dm_tree_node
*child
= dnode
;
1221 struct dm_info info
;
1222 const struct dm_info
*dinfo
;
1226 while ((child
= dm_tree_next_child(&handle
, dnode
, 0))) {
1227 if (!(dinfo
= dm_tree_node_get_info(child
))) {
1232 if (!(name
= dm_tree_node_get_name(child
))) {
1237 if (!(uuid
= dm_tree_node_get_uuid(child
))) {
1242 /* Ignore if it doesn't belong to this VG */
1243 if (!_uuid_prefix_matches(uuid
, uuid_prefix
, uuid_prefix_len
))
1246 /* Refresh open_count */
1247 if (!_info_by_dev(dinfo
->major
, dinfo
->minor
, 1, &info
) ||
1251 if (!_check_device_not_in_use(&info
))
1254 /* Also checking open_count in parent nodes of presuspend_node */
1255 if ((child
->presuspend_node
&&
1256 !_node_has_closed_parents(child
->presuspend_node
,
1257 uuid_prefix
, uuid_prefix_len
))) {
1258 /* Only report error from (likely non-internal) dependency at top level */
1260 log_error("Unable to deactivate open %s (%" PRIu32
1261 ":%" PRIu32
")", name
, info
.major
,
1268 /* Suspend child node first if requested */
1269 if (child
->presuspend_node
&&
1270 !dm_tree_suspend_children(child
, uuid_prefix
, uuid_prefix_len
))
1273 if (!_deactivate_node(name
, info
.major
, info
.minor
,
1274 &child
->dtree
->cookie
, child
->udev_flags
,
1275 child
->dtree
->retry_remove
)) {
1276 log_error("Unable to deactivate %s (%" PRIu32
1277 ":%" PRIu32
")", name
, info
.major
,
1281 } else if (info
.suspended
)
1284 if (dm_tree_node_num_children(child
, 0)) {
1285 if (!_dm_tree_deactivate_children(child
, uuid_prefix
, uuid_prefix_len
, level
+ 1))
1293 int dm_tree_deactivate_children(struct dm_tree_node
*dnode
,
1294 const char *uuid_prefix
,
1295 size_t uuid_prefix_len
)
1297 return _dm_tree_deactivate_children(dnode
, uuid_prefix
, uuid_prefix_len
, 0);
1300 void dm_tree_skip_lockfs(struct dm_tree_node
*dnode
)
1302 dnode
->dtree
->skip_lockfs
= 1;
1305 void dm_tree_use_no_flush_suspend(struct dm_tree_node
*dnode
)
1307 dnode
->dtree
->no_flush
= 1;
1310 void dm_tree_retry_remove(struct dm_tree_node
*dnode
)
1312 dnode
->dtree
->retry_remove
= 1;
1315 int dm_tree_suspend_children(struct dm_tree_node
*dnode
,
1316 const char *uuid_prefix
,
1317 size_t uuid_prefix_len
)
1320 void *handle
= NULL
;
1321 struct dm_tree_node
*child
= dnode
;
1322 struct dm_info info
, newinfo
;
1323 const struct dm_info
*dinfo
;
1327 /* Suspend nodes at this level of the tree */
1328 while ((child
= dm_tree_next_child(&handle
, dnode
, 0))) {
1329 if (!(dinfo
= dm_tree_node_get_info(child
))) {
1334 if (!(name
= dm_tree_node_get_name(child
))) {
1339 if (!(uuid
= dm_tree_node_get_uuid(child
))) {
1344 /* Ignore if it doesn't belong to this VG */
1345 if (!_uuid_prefix_matches(uuid
, uuid_prefix
, uuid_prefix_len
))
1348 /* Ensure immediate parents are already suspended */
1349 if (!_children_suspended(child
, 1, uuid_prefix
, uuid_prefix_len
))
1352 if (!_info_by_dev(dinfo
->major
, dinfo
->minor
, 0, &info
) ||
1353 !info
.exists
|| info
.suspended
)
1356 if (!_suspend_node(name
, info
.major
, info
.minor
,
1357 child
->dtree
->skip_lockfs
,
1358 child
->dtree
->no_flush
, &newinfo
)) {
1359 log_error("Unable to suspend %s (%" PRIu32
1360 ":%" PRIu32
")", name
, info
.major
,
1366 /* Update cached info */
1367 child
->info
= newinfo
;
1370 /* Then suspend any child nodes */
1373 while ((child
= dm_tree_next_child(&handle
, dnode
, 0))) {
1374 if (!(uuid
= dm_tree_node_get_uuid(child
))) {
1379 /* Ignore if it doesn't belong to this VG */
1380 if (!_uuid_prefix_matches(uuid
, uuid_prefix
, uuid_prefix_len
))
1383 if (dm_tree_node_num_children(child
, 0))
1384 if (!dm_tree_suspend_children(child
, uuid_prefix
, uuid_prefix_len
))
1391 int dm_tree_activate_children(struct dm_tree_node
*dnode
,
1392 const char *uuid_prefix
,
1393 size_t uuid_prefix_len
)
1396 void *handle
= NULL
;
1397 struct dm_tree_node
*child
= dnode
;
1398 struct dm_info newinfo
;
1403 /* Activate children first */
1404 while ((child
= dm_tree_next_child(&handle
, dnode
, 0))) {
1405 if (!(uuid
= dm_tree_node_get_uuid(child
))) {
1410 if (!_uuid_prefix_matches(uuid
, uuid_prefix
, uuid_prefix_len
))
1413 if (dm_tree_node_num_children(child
, 0))
1414 if (!dm_tree_activate_children(child
, uuid_prefix
, uuid_prefix_len
))
1420 for (priority
= 0; priority
< 3; priority
++) {
1421 while ((child
= dm_tree_next_child(&handle
, dnode
, 0))) {
1422 if (priority
!= child
->activation_priority
)
1425 if (!(uuid
= dm_tree_node_get_uuid(child
))) {
1430 if (!_uuid_prefix_matches(uuid
, uuid_prefix
, uuid_prefix_len
))
1433 if (!(name
= dm_tree_node_get_name(child
))) {
1439 if (child
->props
.new_name
) {
1440 if (!_rename_node(name
, child
->props
.new_name
, child
->info
.major
,
1441 child
->info
.minor
, &child
->dtree
->cookie
,
1442 child
->udev_flags
)) {
1443 log_error("Failed to rename %s (%" PRIu32
1444 ":%" PRIu32
") to %s", name
, child
->info
.major
,
1445 child
->info
.minor
, child
->props
.new_name
);
1448 child
->name
= child
->props
.new_name
;
1449 child
->props
.new_name
= NULL
;
1452 if (!child
->info
.inactive_table
&& !child
->info
.suspended
)
1455 if (!_resume_node(child
->name
, child
->info
.major
, child
->info
.minor
,
1456 child
->props
.read_ahead
, child
->props
.read_ahead_flags
,
1457 &newinfo
, &child
->dtree
->cookie
, child
->udev_flags
, child
->info
.suspended
)) {
1458 log_error("Unable to resume %s (%" PRIu32
1459 ":%" PRIu32
")", child
->name
, child
->info
.major
,
1465 /* Update cached info */
1466 child
->info
= newinfo
;
1475 static int _create_node(struct dm_tree_node
*dnode
)
1478 struct dm_task
*dmt
;
1480 log_verbose("Creating %s", dnode
->name
);
1482 if (!(dmt
= dm_task_create(DM_DEVICE_CREATE
))) {
1483 log_error("Create dm_task creation failed for %s", dnode
->name
);
1487 if (!dm_task_set_name(dmt
, dnode
->name
)) {
1488 log_error("Failed to set device name for %s", dnode
->name
);
1492 if (!dm_task_set_uuid(dmt
, dnode
->uuid
)) {
1493 log_error("Failed to set uuid for %s", dnode
->name
);
1497 if (dnode
->props
.major
&&
1498 (!dm_task_set_major(dmt
, dnode
->props
.major
) ||
1499 !dm_task_set_minor(dmt
, dnode
->props
.minor
))) {
1500 log_error("Failed to set device number for %s creation.", dnode
->name
);
1504 if (dnode
->props
.read_only
&& !dm_task_set_ro(dmt
)) {
1505 log_error("Failed to set read only flag for %s", dnode
->name
);
1509 if (!dm_task_no_open_count(dmt
))
1510 log_error("Failed to disable open_count");
1512 if ((r
= dm_task_run(dmt
)))
1513 r
= dm_task_get_info(dmt
, &dnode
->info
);
1516 dm_task_destroy(dmt
);
1522 static int _build_dev_string(char *devbuf
, size_t bufsize
, struct dm_tree_node
*node
)
1524 if (!dm_format_dev(devbuf
, bufsize
, node
->info
.major
, node
->info
.minor
)) {
1525 log_error("Failed to format %s device number for %s as dm "
1527 node
->name
, node
->uuid
, node
->info
.major
, node
->info
.minor
);
1534 /* simplify string emiting code */
1535 #define EMIT_PARAMS(p, str...)\
1538 if ((w = dm_snprintf(params + p, paramsize - (size_t) p, str)) < 0) {\
1539 stack; /* Out of space */\
1548 * Returns: 1 on success, 0 on failure
1550 static int _emit_areas_line(struct dm_task
*dmt
__attribute__((unused
)),
1551 struct load_segment
*seg
, char *params
,
1552 size_t paramsize
, int *pos
)
1554 struct seg_area
*area
;
1555 char devbuf
[DM_FORMAT_DEV_BUFSIZE
];
1556 unsigned first_time
= 1;
1557 const char *logtype
, *synctype
;
1558 unsigned log_parm_count
;
1560 dm_list_iterate_items(area
, &seg
->areas
) {
1561 switch (seg
->type
) {
1562 case SEG_REPLICATOR_DEV
:
1563 if (!_build_dev_string(devbuf
, sizeof(devbuf
), area
->dev_node
))
1566 EMIT_PARAMS(*pos
, " %d 1 %s", area
->rsite_index
, devbuf
);
1568 EMIT_PARAMS(*pos
, " nolog 0");
1570 /* Remote devices */
1571 log_parm_count
= (area
->flags
&
1572 (DM_NOSYNC
| DM_FORCESYNC
)) ? 2 : 1;
1575 devbuf
[0] = 0; /* Only core log parameters */
1578 devbuf
[0] = ' '; /* Extra space before device name */
1579 if (!_build_dev_string(devbuf
+ 1,
1584 log_parm_count
++; /* Extra sync log device name parameter */
1587 EMIT_PARAMS(*pos
, " %s %u%s %" PRIu64
, logtype
,
1588 log_parm_count
, devbuf
, area
->region_size
);
1590 synctype
= (area
->flags
& DM_NOSYNC
) ?
1591 " nosync" : (area
->flags
& DM_FORCESYNC
) ?
1595 EMIT_PARAMS(*pos
, "%s", synctype
);
1607 if (!area
->dev_node
) {
1608 EMIT_PARAMS(*pos
, " -");
1611 if (!_build_dev_string(devbuf
, sizeof(devbuf
), area
->dev_node
))
1614 EMIT_PARAMS(*pos
, " %s", devbuf
);
1617 if (!_build_dev_string(devbuf
, sizeof(devbuf
), area
->dev_node
))
1620 EMIT_PARAMS(*pos
, "%s%s %" PRIu64
, first_time
? "" : " ",
1621 devbuf
, area
->offset
);
1630 static int _replicator_emit_segment_line(const struct load_segment
*seg
, char *params
,
1631 size_t paramsize
, int *pos
)
1633 const struct load_segment
*rlog_seg
;
1634 struct replicator_site
*rsite
;
1635 char rlogbuf
[DM_FORMAT_DEV_BUFSIZE
];
1636 unsigned parm_count
;
1638 if (!seg
->log
|| !_build_dev_string(rlogbuf
, sizeof(rlogbuf
), seg
->log
))
1641 rlog_seg
= dm_list_item(dm_list_last(&seg
->log
->props
.segs
),
1642 struct load_segment
);
1644 EMIT_PARAMS(*pos
, "%s 4 %s 0 auto %" PRIu64
,
1645 seg
->rlog_type
, rlogbuf
, rlog_seg
->size
);
1647 dm_list_iterate_items(rsite
, &seg
->rsites
) {
1648 parm_count
= (rsite
->fall_behind_data
1649 || rsite
->fall_behind_ios
1650 || rsite
->async_timeout
) ? 4 : 2;
1652 EMIT_PARAMS(*pos
, " blockdev %u %u %s", parm_count
, rsite
->rsite_index
,
1653 (rsite
->mode
== DM_REPLICATOR_SYNC
) ? "synchronous" : "asynchronous");
1655 if (rsite
->fall_behind_data
)
1656 EMIT_PARAMS(*pos
, " data %" PRIu64
, rsite
->fall_behind_data
);
1657 else if (rsite
->fall_behind_ios
)
1658 EMIT_PARAMS(*pos
, " ios %" PRIu32
, rsite
->fall_behind_ios
);
1659 else if (rsite
->async_timeout
)
1660 EMIT_PARAMS(*pos
, " timeout %" PRIu32
, rsite
->async_timeout
);
1667 * Returns: 1 on success, 0 on failure
1669 static int _mirror_emit_segment_line(struct dm_task
*dmt
, struct load_segment
*seg
,
1670 char *params
, size_t paramsize
)
1672 int block_on_error
= 0;
1673 int handle_errors
= 0;
1674 int dm_log_userspace
= 0;
1676 unsigned log_parm_count
;
1678 char logbuf
[DM_FORMAT_DEV_BUFSIZE
];
1679 const char *logtype
;
1680 unsigned kmaj
= 0, kmin
= 0, krel
= 0;
1682 if (uname(&uts
) == -1) {
1683 log_error("Cannot read kernel release version.");
1687 /* Kernels with a major number of 2 always had 3 parts. */
1688 parts
= sscanf(uts
.release
, "%u.%u.%u", &kmaj
, &kmin
, &krel
);
1689 if (parts
< 1 || (kmaj
< 3 && parts
< 3)) {
1690 log_error("Wrong kernel release version %s.", uts
.release
);
1694 if ((seg
->flags
& DM_BLOCK_ON_ERROR
)) {
1696 * Originally, block_on_error was an argument to the log
1697 * portion of the mirror CTR table. It was renamed to
1698 * "handle_errors" and now resides in the 'features'
1699 * section of the mirror CTR table (i.e. at the end).
1701 * We can identify whether to use "block_on_error" or
1702 * "handle_errors" by the dm-mirror module's version
1703 * number (>= 1.12) or by the kernel version (>= 2.6.22).
1705 if (KERNEL_VERSION(kmaj
, kmin
, krel
) >= KERNEL_VERSION(2, 6, 22))
1711 if (seg
->clustered
) {
1712 /* Cluster mirrors require a UUID */
1717 * Cluster mirrors used to have their own log
1718 * types. Now they are accessed through the
1719 * userspace log type.
1721 * The dm-log-userspace module was added to the
1724 if (KERNEL_VERSION(kmaj
, kmin
, krel
) >= KERNEL_VERSION(2, 6, 31))
1725 dm_log_userspace
= 1;
1731 /* [no]sync, block_on_error etc. */
1732 log_parm_count
+= hweight32(seg
->flags
);
1734 /* "handle_errors" is a feature arg now */
1738 /* DM_CORELOG does not count in the param list */
1739 if (seg
->flags
& DM_CORELOG
)
1742 if (seg
->clustered
) {
1743 log_parm_count
++; /* For UUID */
1745 if (!dm_log_userspace
)
1746 EMIT_PARAMS(pos
, "clustered-");
1748 /* For clustered-* type field inserted later */
1757 if (!_build_dev_string(logbuf
, sizeof(logbuf
), seg
->log
))
1761 if (dm_log_userspace
)
1762 EMIT_PARAMS(pos
, "userspace %u %s clustered-%s",
1763 log_parm_count
, seg
->uuid
, logtype
);
1765 EMIT_PARAMS(pos
, "%s %u", logtype
, log_parm_count
);
1768 EMIT_PARAMS(pos
, " %s", logbuf
);
1770 EMIT_PARAMS(pos
, " %u", seg
->region_size
);
1772 if (seg
->clustered
&& !dm_log_userspace
)
1773 EMIT_PARAMS(pos
, " %s", seg
->uuid
);
1775 if ((seg
->flags
& DM_NOSYNC
))
1776 EMIT_PARAMS(pos
, " nosync");
1777 else if ((seg
->flags
& DM_FORCESYNC
))
1778 EMIT_PARAMS(pos
, " sync");
1781 EMIT_PARAMS(pos
, " block_on_error");
1783 EMIT_PARAMS(pos
, " %u ", seg
->mirror_area_count
);
1785 if (_emit_areas_line(dmt
, seg
, params
, paramsize
, &pos
) <= 0)
1789 EMIT_PARAMS(pos
, " 1 handle_errors");
1794 static int _raid_emit_segment_line(struct dm_task
*dmt
, uint32_t major
,
1795 uint32_t minor
, struct load_segment
*seg
,
1796 uint64_t *seg_start
, char *params
,
1800 int param_count
= 1; /* mandatory 'chunk size'/'stripe size' arg */
1803 if ((seg
->flags
& DM_NOSYNC
) || (seg
->flags
& DM_FORCESYNC
))
1806 if (seg
->region_size
)
1809 tmp
= (uint32_t *)(&seg
->rebuilds
); /* rebuilds is 64-bit */
1810 param_count
+= 2 * hweight32(tmp
[0]);
1811 param_count
+= 2 * hweight32(tmp
[1]);
1813 if ((seg
->type
== SEG_RAID1
) && seg
->stripe_size
)
1814 log_error("WARNING: Ignoring RAID1 stripe size");
1816 EMIT_PARAMS(pos
, "%s %d %u", dm_segtypes
[seg
->type
].target
,
1817 param_count
, seg
->stripe_size
);
1819 if (seg
->flags
& DM_NOSYNC
)
1820 EMIT_PARAMS(pos
, " nosync");
1821 else if (seg
->flags
& DM_FORCESYNC
)
1822 EMIT_PARAMS(pos
, " sync");
1824 if (seg
->region_size
)
1825 EMIT_PARAMS(pos
, " region_size %u", seg
->region_size
);
1827 for (i
= 0; i
< (seg
->area_count
/ 2); i
++)
1828 if (seg
->rebuilds
& (1 << i
))
1829 EMIT_PARAMS(pos
, " rebuild %u", i
);
1831 /* Print number of metadata/data device pairs */
1832 EMIT_PARAMS(pos
, " %u", seg
->area_count
/2);
1834 if (_emit_areas_line(dmt
, seg
, params
, paramsize
, &pos
) <= 0)
1840 static int _emit_segment_line(struct dm_task
*dmt
, uint32_t major
,
1841 uint32_t minor
, struct load_segment
*seg
,
1842 uint64_t *seg_start
, char *params
,
1847 int target_type_is_raid
= 0;
1848 char originbuf
[DM_FORMAT_DEV_BUFSIZE
], cowbuf
[DM_FORMAT_DEV_BUFSIZE
];
1849 char pool
[DM_FORMAT_DEV_BUFSIZE
], metadata
[DM_FORMAT_DEV_BUFSIZE
];
1857 /* Mirrors are pretty complicated - now in separate function */
1858 r
= _mirror_emit_segment_line(dmt
, seg
, params
, paramsize
);
1862 case SEG_REPLICATOR
:
1863 if ((r
= _replicator_emit_segment_line(seg
, params
, paramsize
,
1869 case SEG_REPLICATOR_DEV
:
1870 if (!seg
->replicator
|| !_build_dev_string(originbuf
,
1875 EMIT_PARAMS(pos
, "%s %" PRIu64
, originbuf
, seg
->rdevice_index
);
1878 case SEG_SNAPSHOT_MERGE
:
1879 if (!_build_dev_string(originbuf
, sizeof(originbuf
), seg
->origin
))
1881 if (!_build_dev_string(cowbuf
, sizeof(cowbuf
), seg
->cow
))
1883 EMIT_PARAMS(pos
, "%s %s %c %d", originbuf
, cowbuf
,
1884 seg
->persistent
? 'P' : 'N', seg
->chunk_size
);
1886 case SEG_SNAPSHOT_ORIGIN
:
1887 if (!_build_dev_string(originbuf
, sizeof(originbuf
), seg
->origin
))
1889 EMIT_PARAMS(pos
, "%s", originbuf
);
1892 EMIT_PARAMS(pos
, "%u %u ", seg
->area_count
, seg
->stripe_size
);
1895 EMIT_PARAMS(pos
, "%s%s%s%s%s %s %" PRIu64
" ", seg
->cipher
,
1896 seg
->chainmode
? "-" : "", seg
->chainmode
?: "",
1897 seg
->iv
? "-" : "", seg
->iv
?: "", seg
->key
,
1898 seg
->iv_offset
!= DM_CRYPT_IV_DEFAULT
?
1899 seg
->iv_offset
: *seg_start
);
1910 target_type_is_raid
= 1;
1911 r
= _raid_emit_segment_line(dmt
, major
, minor
, seg
, seg_start
,
1918 if (!_build_dev_string(metadata
, sizeof(metadata
), seg
->metadata
))
1920 if (!_build_dev_string(pool
, sizeof(pool
), seg
->pool
))
1922 EMIT_PARAMS(pos
, "%s %s %d %" PRIu64
" %s", metadata
, pool
,
1923 seg
->data_block_size
, seg
->low_water_mark
,
1924 seg
->skip_block_zeroeing
? "1 skip_block_zeroing" : "");
1927 if (!_build_dev_string(pool
, sizeof(pool
), seg
->pool
))
1929 EMIT_PARAMS(pos
, "%s %d", pool
, seg
->device_id
);
1935 case SEG_REPLICATOR
:
1937 case SEG_SNAPSHOT_ORIGIN
:
1938 case SEG_SNAPSHOT_MERGE
:
1945 case SEG_REPLICATOR_DEV
:
1947 if ((r
= _emit_areas_line(dmt
, seg
, params
, paramsize
, &pos
)) <= 0) {
1952 log_error("No parameters supplied for %s target "
1953 "%u:%u.", dm_segtypes
[seg
->type
].target
,
1960 log_debug("Adding target to (%" PRIu32
":%" PRIu32
"): %" PRIu64
1961 " %" PRIu64
" %s %s", major
, minor
,
1962 *seg_start
, seg
->size
, target_type_is_raid
? "raid" :
1963 dm_segtypes
[seg
->type
].target
, params
);
1965 if (!dm_task_add_target(dmt
, *seg_start
, seg
->size
,
1966 target_type_is_raid
? "raid" :
1967 dm_segtypes
[seg
->type
].target
, params
))
1970 *seg_start
+= seg
->size
;
1977 static int _emit_segment(struct dm_task
*dmt
, uint32_t major
, uint32_t minor
,
1978 struct load_segment
*seg
, uint64_t *seg_start
)
1981 size_t paramsize
= 4096;
1985 if (!(params
= dm_malloc(paramsize
))) {
1986 log_error("Insufficient space for target parameters.");
1991 ret
= _emit_segment_line(dmt
, major
, minor
, seg
, seg_start
,
2001 log_debug("Insufficient space in params[%" PRIsize_t
2002 "] for target parameters.", paramsize
);
2005 } while (paramsize
< MAX_TARGET_PARAMSIZE
);
2007 log_error("Target parameter size too big. Aborting.");
2011 static int _load_node(struct dm_tree_node
*dnode
)
2014 struct dm_task
*dmt
;
2015 struct load_segment
*seg
;
2016 uint64_t seg_start
= 0, existing_table_size
;
2018 log_verbose("Loading %s table (%" PRIu32
":%" PRIu32
")", dnode
->name
,
2019 dnode
->info
.major
, dnode
->info
.minor
);
2021 if (!(dmt
= dm_task_create(DM_DEVICE_RELOAD
))) {
2022 log_error("Reload dm_task creation failed for %s", dnode
->name
);
2026 if (!dm_task_set_major(dmt
, dnode
->info
.major
) ||
2027 !dm_task_set_minor(dmt
, dnode
->info
.minor
)) {
2028 log_error("Failed to set device number for %s reload.", dnode
->name
);
2032 if (dnode
->props
.read_only
&& !dm_task_set_ro(dmt
)) {
2033 log_error("Failed to set read only flag for %s", dnode
->name
);
2037 if (!dm_task_no_open_count(dmt
))
2038 log_error("Failed to disable open_count");
2040 dm_list_iterate_items(seg
, &dnode
->props
.segs
)
2041 if (!_emit_segment(dmt
, dnode
->info
.major
, dnode
->info
.minor
,
2045 if (!dm_task_suppress_identical_reload(dmt
))
2046 log_error("Failed to suppress reload of identical tables.");
2048 if ((r
= dm_task_run(dmt
))) {
2049 r
= dm_task_get_info(dmt
, &dnode
->info
);
2050 if (r
&& !dnode
->info
.inactive_table
)
2051 log_verbose("Suppressed %s identical table reload.",
2054 existing_table_size
= dm_task_get_existing_table_size(dmt
);
2055 if ((dnode
->props
.size_changed
=
2056 (existing_table_size
== seg_start
) ? 0 : 1)) {
2057 log_debug("Table size changed from %" PRIu64
" to %"
2058 PRIu64
" for %s", existing_table_size
,
2059 seg_start
, dnode
->name
);
2061 * Kernel usually skips size validation on zero-length devices
2062 * now so no need to preload them.
2064 /* FIXME In which kernel version did this begin? */
2065 if (!existing_table_size
&& dnode
->props
.delay_resume_if_new
)
2066 dnode
->props
.size_changed
= 0;
2070 dnode
->props
.segment_count
= 0;
2073 dm_task_destroy(dmt
);
2078 int dm_tree_preload_children(struct dm_tree_node
*dnode
,
2079 const char *uuid_prefix
,
2080 size_t uuid_prefix_len
)
2083 void *handle
= NULL
;
2084 struct dm_tree_node
*child
;
2085 struct dm_info newinfo
;
2086 int update_devs_flag
= 0;
2088 /* Preload children first */
2089 while ((child
= dm_tree_next_child(&handle
, dnode
, 0))) {
2090 /* Skip existing non-device-mapper devices */
2091 if (!child
->info
.exists
&& child
->info
.major
)
2094 /* Ignore if it doesn't belong to this VG */
2095 if (child
->info
.exists
&&
2096 !_uuid_prefix_matches(child
->uuid
, uuid_prefix
, uuid_prefix_len
))
2099 if (dm_tree_node_num_children(child
, 0))
2100 if (!dm_tree_preload_children(child
, uuid_prefix
, uuid_prefix_len
))
2103 /* FIXME Cope if name exists with no uuid? */
2104 if (!child
->info
.exists
) {
2105 if (!_create_node(child
)) {
2111 if (!child
->info
.inactive_table
&& child
->props
.segment_count
) {
2112 if (!_load_node(child
)) {
2118 /* Propagate device size change change */
2119 if (child
->props
.size_changed
)
2120 dnode
->props
.size_changed
= 1;
2122 /* Resume device immediately if it has parents and its size changed */
2123 if (!dm_tree_node_num_children(child
, 1) || !child
->props
.size_changed
)
2126 if (!child
->info
.inactive_table
&& !child
->info
.suspended
)
2129 if (!_resume_node(child
->name
, child
->info
.major
, child
->info
.minor
,
2130 child
->props
.read_ahead
, child
->props
.read_ahead_flags
,
2131 &newinfo
, &child
->dtree
->cookie
, child
->udev_flags
,
2132 child
->info
.suspended
)) {
2133 log_error("Unable to resume %s (%" PRIu32
2134 ":%" PRIu32
")", child
->name
, child
->info
.major
,
2140 /* Update cached info */
2141 child
->info
= newinfo
;
2144 * Prepare for immediate synchronization with udev and flush all stacked
2145 * dev node operations if requested by immediate_dev_node property. But
2146 * finish processing current level in the tree first.
2148 if (child
->props
.immediate_dev_node
)
2149 update_devs_flag
= 1;
2155 if (update_devs_flag
) {
2156 if (!dm_udev_wait(dm_tree_get_cookie(dnode
)))
2158 dm_tree_set_cookie(dnode
, 0);
2165 * Returns 1 if unsure.
2167 int dm_tree_children_use_uuid(struct dm_tree_node
*dnode
,
2168 const char *uuid_prefix
,
2169 size_t uuid_prefix_len
)
2171 void *handle
= NULL
;
2172 struct dm_tree_node
*child
= dnode
;
2175 while ((child
= dm_tree_next_child(&handle
, dnode
, 0))) {
2176 if (!(uuid
= dm_tree_node_get_uuid(child
))) {
2177 log_error("Failed to get uuid for dtree node.");
2181 if (_uuid_prefix_matches(uuid
, uuid_prefix
, uuid_prefix_len
))
2184 if (dm_tree_node_num_children(child
, 0))
2185 dm_tree_children_use_uuid(child
, uuid_prefix
, uuid_prefix_len
);
2194 static struct load_segment
*_add_segment(struct dm_tree_node
*dnode
, unsigned type
, uint64_t size
)
2196 struct load_segment
*seg
;
2198 if (!(seg
= dm_pool_zalloc(dnode
->dtree
->mem
, sizeof(*seg
)))) {
2199 log_error("dtree node segment allocation failed");
2205 seg
->area_count
= 0;
2206 dm_list_init(&seg
->areas
);
2207 seg
->stripe_size
= 0;
2208 seg
->persistent
= 0;
2209 seg
->chunk_size
= 0;
2214 dm_list_add(&dnode
->props
.segs
, &seg
->list
);
2215 dnode
->props
.segment_count
++;
2220 int dm_tree_node_add_snapshot_origin_target(struct dm_tree_node
*dnode
,
2222 const char *origin_uuid
)
2224 struct load_segment
*seg
;
2225 struct dm_tree_node
*origin_node
;
2227 if (!(seg
= _add_segment(dnode
, SEG_SNAPSHOT_ORIGIN
, size
)))
2230 if (!(origin_node
= dm_tree_find_node_by_uuid(dnode
->dtree
, origin_uuid
))) {
2231 log_error("Couldn't find snapshot origin uuid %s.", origin_uuid
);
2235 seg
->origin
= origin_node
;
2236 if (!_link_tree_nodes(dnode
, origin_node
))
2239 /* Resume snapshot origins after new snapshots */
2240 dnode
->activation_priority
= 1;
2245 static int _add_snapshot_target(struct dm_tree_node
*node
,
2247 const char *origin_uuid
,
2248 const char *cow_uuid
,
2249 const char *merge_uuid
,
2251 uint32_t chunk_size
)
2253 struct load_segment
*seg
;
2254 struct dm_tree_node
*origin_node
, *cow_node
, *merge_node
;
2257 seg_type
= !merge_uuid
? SEG_SNAPSHOT
: SEG_SNAPSHOT_MERGE
;
2259 if (!(seg
= _add_segment(node
, seg_type
, size
)))
2262 if (!(origin_node
= dm_tree_find_node_by_uuid(node
->dtree
, origin_uuid
))) {
2263 log_error("Couldn't find snapshot origin uuid %s.", origin_uuid
);
2267 seg
->origin
= origin_node
;
2268 if (!_link_tree_nodes(node
, origin_node
))
2271 if (!(cow_node
= dm_tree_find_node_by_uuid(node
->dtree
, cow_uuid
))) {
2272 log_error("Couldn't find snapshot COW device uuid %s.", cow_uuid
);
2276 seg
->cow
= cow_node
;
2277 if (!_link_tree_nodes(node
, cow_node
))
2280 seg
->persistent
= persistent
? 1 : 0;
2281 seg
->chunk_size
= chunk_size
;
2284 if (!(merge_node
= dm_tree_find_node_by_uuid(node
->dtree
, merge_uuid
))) {
2285 /* not a pure error, merging snapshot may have been deactivated */
2286 log_verbose("Couldn't find merging snapshot uuid %s.", merge_uuid
);
2288 seg
->merge
= merge_node
;
2289 /* must not link merging snapshot, would undermine activation_priority below */
2292 /* Resume snapshot-merge (acting origin) after other snapshots */
2293 node
->activation_priority
= 1;
2295 /* Resume merging snapshot after snapshot-merge */
2296 seg
->merge
->activation_priority
= 2;
2304 int dm_tree_node_add_snapshot_target(struct dm_tree_node
*node
,
2306 const char *origin_uuid
,
2307 const char *cow_uuid
,
2309 uint32_t chunk_size
)
2311 return _add_snapshot_target(node
, size
, origin_uuid
, cow_uuid
,
2312 NULL
, persistent
, chunk_size
);
2315 int dm_tree_node_add_snapshot_merge_target(struct dm_tree_node
*node
,
2317 const char *origin_uuid
,
2318 const char *cow_uuid
,
2319 const char *merge_uuid
,
2320 uint32_t chunk_size
)
2322 return _add_snapshot_target(node
, size
, origin_uuid
, cow_uuid
,
2323 merge_uuid
, 1, chunk_size
);
2326 int dm_tree_node_add_error_target(struct dm_tree_node
*node
,
2329 if (!_add_segment(node
, SEG_ERROR
, size
))
2335 int dm_tree_node_add_zero_target(struct dm_tree_node
*node
,
2338 if (!_add_segment(node
, SEG_ZERO
, size
))
2344 int dm_tree_node_add_linear_target(struct dm_tree_node
*node
,
2347 if (!_add_segment(node
, SEG_LINEAR
, size
))
2353 int dm_tree_node_add_striped_target(struct dm_tree_node
*node
,
2355 uint32_t stripe_size
)
2357 struct load_segment
*seg
;
2359 if (!(seg
= _add_segment(node
, SEG_STRIPED
, size
)))
2362 seg
->stripe_size
= stripe_size
;
2367 int dm_tree_node_add_crypt_target(struct dm_tree_node
*node
,
2370 const char *chainmode
,
2375 struct load_segment
*seg
;
2377 if (!(seg
= _add_segment(node
, SEG_CRYPT
, size
)))
2380 seg
->cipher
= cipher
;
2381 seg
->chainmode
= chainmode
;
2383 seg
->iv_offset
= iv_offset
;
2389 int dm_tree_node_add_mirror_target_log(struct dm_tree_node
*node
,
2390 uint32_t region_size
,
2392 const char *log_uuid
,
2393 unsigned area_count
,
2396 struct dm_tree_node
*log_node
= NULL
;
2397 struct load_segment
*seg
;
2399 if (!node
->props
.segment_count
) {
2400 log_error(INTERNAL_ERROR
"Attempt to add target area to missing segment.");
2404 seg
= dm_list_item(dm_list_last(&node
->props
.segs
), struct load_segment
);
2407 if (!(seg
->uuid
= dm_pool_strdup(node
->dtree
->mem
, log_uuid
))) {
2408 log_error("log uuid pool_strdup failed");
2411 if ((flags
& DM_CORELOG
))
2412 /* For pvmove: immediate resume (for size validation) isn't needed. */
2413 node
->props
.delay_resume_if_new
= 1;
2415 if (!(log_node
= dm_tree_find_node_by_uuid(node
->dtree
, log_uuid
))) {
2416 log_error("Couldn't find mirror log uuid %s.", log_uuid
);
2421 log_node
->props
.immediate_dev_node
= 1;
2423 /* The kernel validates the size of disk logs. */
2424 /* FIXME Propagate to any devices below */
2425 log_node
->props
.delay_resume_if_new
= 0;
2427 if (!_link_tree_nodes(node
, log_node
))
2432 seg
->log
= log_node
;
2433 seg
->region_size
= region_size
;
2434 seg
->clustered
= clustered
;
2435 seg
->mirror_area_count
= area_count
;
2441 int dm_tree_node_add_mirror_target(struct dm_tree_node
*node
,
2444 if (!_add_segment(node
, SEG_MIRRORED
, size
))
2450 int dm_tree_node_add_raid_target(struct dm_tree_node
*node
,
2452 const char *raid_type
,
2453 uint32_t region_size
,
2454 uint32_t stripe_size
,
2459 struct load_segment
*seg
= NULL
;
2461 for (i
= 0; dm_segtypes
[i
].target
&& !seg
; i
++)
2462 if (!strcmp(raid_type
, dm_segtypes
[i
].target
))
2463 if (!(seg
= _add_segment(node
,
2464 dm_segtypes
[i
].type
, size
)))
2470 seg
->region_size
= region_size
;
2471 seg
->stripe_size
= stripe_size
;
2472 seg
->area_count
= 0;
2473 seg
->rebuilds
= rebuilds
;
2478 int dm_tree_node_add_replicator_target(struct dm_tree_node
*node
,
2480 const char *rlog_uuid
,
2481 const char *rlog_type
,
2482 unsigned rsite_index
,
2483 dm_replicator_mode_t mode
,
2484 uint32_t async_timeout
,
2485 uint64_t fall_behind_data
,
2486 uint32_t fall_behind_ios
)
2488 struct load_segment
*rseg
;
2489 struct replicator_site
*rsite
;
2491 /* Local site0 - adds replicator segment and links rlog device */
2492 if (rsite_index
== REPLICATOR_LOCAL_SITE
) {
2493 if (node
->props
.segment_count
) {
2494 log_error(INTERNAL_ERROR
"Attempt to add replicator segment to already used node.");
2498 if (!(rseg
= _add_segment(node
, SEG_REPLICATOR
, size
)))
2501 if (!(rseg
->log
= dm_tree_find_node_by_uuid(node
->dtree
, rlog_uuid
))) {
2502 log_error("Missing replicator log uuid %s.", rlog_uuid
);
2506 if (!_link_tree_nodes(node
, rseg
->log
))
2509 if (strcmp(rlog_type
, "ringbuffer") != 0) {
2510 log_error("Unsupported replicator log type %s.", rlog_type
);
2514 if (!(rseg
->rlog_type
= dm_pool_strdup(node
->dtree
->mem
, rlog_type
)))
2517 dm_list_init(&rseg
->rsites
);
2518 rseg
->rdevice_count
= 0;
2519 node
->activation_priority
= 1;
2522 /* Add site to segment */
2523 if (mode
== DM_REPLICATOR_SYNC
2524 && (async_timeout
|| fall_behind_ios
|| fall_behind_data
)) {
2525 log_error("Async parameters passed for synchronnous replicator.");
2529 if (node
->props
.segment_count
!= 1) {
2530 log_error(INTERNAL_ERROR
"Attempt to add remote site area before setting replicator log.");
2534 rseg
= dm_list_item(dm_list_last(&node
->props
.segs
), struct load_segment
);
2535 if (rseg
->type
!= SEG_REPLICATOR
) {
2536 log_error(INTERNAL_ERROR
"Attempt to use non replicator segment %s.",
2537 dm_segtypes
[rseg
->type
].target
);
2541 if (!(rsite
= dm_pool_zalloc(node
->dtree
->mem
, sizeof(*rsite
)))) {
2542 log_error("Failed to allocate remote site segment.");
2546 dm_list_add(&rseg
->rsites
, &rsite
->list
);
2547 rseg
->rsite_count
++;
2550 rsite
->async_timeout
= async_timeout
;
2551 rsite
->fall_behind_data
= fall_behind_data
;
2552 rsite
->fall_behind_ios
= fall_behind_ios
;
2553 rsite
->rsite_index
= rsite_index
;
2558 /* Appends device node to Replicator */
2559 int dm_tree_node_add_replicator_dev_target(struct dm_tree_node
*node
,
2561 const char *replicator_uuid
,
2562 uint64_t rdevice_index
,
2563 const char *rdev_uuid
,
2564 unsigned rsite_index
,
2565 const char *slog_uuid
,
2566 uint32_t slog_flags
,
2567 uint32_t slog_region_size
)
2569 struct seg_area
*area
;
2570 struct load_segment
*rseg
;
2571 struct load_segment
*rep_seg
;
2573 if (rsite_index
== REPLICATOR_LOCAL_SITE
) {
2574 /* Site index for local target */
2575 if (!(rseg
= _add_segment(node
, SEG_REPLICATOR_DEV
, size
)))
2578 if (!(rseg
->replicator
= dm_tree_find_node_by_uuid(node
->dtree
, replicator_uuid
))) {
2579 log_error("Missing replicator uuid %s.", replicator_uuid
);
2583 /* Local slink0 for replicator must be always initialized first */
2584 if (rseg
->replicator
->props
.segment_count
!= 1) {
2585 log_error(INTERNAL_ERROR
"Attempt to use non replicator segment.");
2589 rep_seg
= dm_list_item(dm_list_last(&rseg
->replicator
->props
.segs
), struct load_segment
);
2590 if (rep_seg
->type
!= SEG_REPLICATOR
) {
2591 log_error(INTERNAL_ERROR
"Attempt to use non replicator segment %s.",
2592 dm_segtypes
[rep_seg
->type
].target
);
2595 rep_seg
->rdevice_count
++;
2597 if (!_link_tree_nodes(node
, rseg
->replicator
))
2600 rseg
->rdevice_index
= rdevice_index
;
2602 /* Local slink0 for replicator must be always initialized first */
2603 if (node
->props
.segment_count
!= 1) {
2604 log_error(INTERNAL_ERROR
"Attempt to use non replicator-dev segment.");
2608 rseg
= dm_list_item(dm_list_last(&node
->props
.segs
), struct load_segment
);
2609 if (rseg
->type
!= SEG_REPLICATOR_DEV
) {
2610 log_error(INTERNAL_ERROR
"Attempt to use non replicator-dev segment %s.",
2611 dm_segtypes
[rseg
->type
].target
);
2616 if (!(slog_flags
& DM_CORELOG
) && !slog_uuid
) {
2617 log_error("Unspecified sync log uuid.");
2621 if (!dm_tree_node_add_target_area(node
, NULL
, rdev_uuid
, 0))
2624 area
= dm_list_item(dm_list_last(&rseg
->areas
), struct seg_area
);
2626 if (!(slog_flags
& DM_CORELOG
)) {
2627 if (!(area
->slog
= dm_tree_find_node_by_uuid(node
->dtree
, slog_uuid
))) {
2628 log_error("Couldn't find sync log uuid %s.", slog_uuid
);
2632 if (!_link_tree_nodes(node
, area
->slog
))
2636 area
->flags
= slog_flags
;
2637 area
->region_size
= slog_region_size
;
2638 area
->rsite_index
= rsite_index
;
2643 int dm_tree_node_add_thin_pool_target(struct dm_tree_node
*node
,
2645 uint64_t transation_id
,
2646 const char *pool_uuid
,
2647 const char *metadata_uuid
,
2648 uint32_t data_block_size
,
2649 uint64_t low_water_mark
,
2650 unsigned skip_block_zeroeing
)
2652 struct load_segment
*seg
;
2654 if (data_block_size
< THIN_MIN_DATA_SIZE
) {
2655 log_error("Data block size %d is lower then "
2656 QUOTE(THIN_MIN_DATA_SIZE
) " sectors.",
2661 if (data_block_size
> THIN_MAX_DATA_SIZE
) {
2662 log_error("Data block size %d is higher then "
2663 QUOTE(THIN_MAX_DATA_SIZE
) " sectors.",
2668 if (!(seg
= _add_segment(node
, SEG_THIN_POOL
, size
)))
2671 if (!(seg
->metadata
= dm_tree_find_node_by_uuid(node
->dtree
, metadata_uuid
))) {
2672 log_error("Missing metadata uuid %s.", metadata_uuid
);
2676 if (!_link_tree_nodes(node
, seg
->metadata
))
2679 if (!(seg
->pool
= dm_tree_find_node_by_uuid(node
->dtree
, pool_uuid
))) {
2680 log_error("Missing pool uuid %s.", pool_uuid
);
2684 if (!_link_tree_nodes(node
, seg
->pool
))
2687 seg
->data_block_size
= data_block_size
;
2688 seg
->low_water_mark
= low_water_mark
;
2689 seg
->skip_block_zeroeing
= skip_block_zeroeing
;
2694 int dm_tree_node_add_thin_target(struct dm_tree_node
*node
,
2696 const char *thin_pool_uuid
,
2699 struct load_segment
*seg
;
2701 if (device_id
> THIN_MAX_DEVICE_ID
) {
2702 log_error("Device id %d is higher then " QUOTE(THIN_MAX_DEVICE_ID
) ".",
2707 if (!(seg
= _add_segment(node
, SEG_THIN
, size
)))
2710 if (!(seg
->pool
= dm_tree_find_node_by_uuid(node
->dtree
, thin_pool_uuid
))) {
2711 log_error("Missing thin pool uuid %s.", thin_pool_uuid
);
2715 if (!_link_tree_nodes(node
, seg
->pool
))
2718 seg
->device_id
= device_id
;
2723 static int _add_area(struct dm_tree_node
*node
, struct load_segment
*seg
, struct dm_tree_node
*dev_node
, uint64_t offset
)
2725 struct seg_area
*area
;
2727 if (!(area
= dm_pool_zalloc(node
->dtree
->mem
, sizeof (*area
)))) {
2728 log_error("Failed to allocate target segment area.");
2732 area
->dev_node
= dev_node
;
2733 area
->offset
= offset
;
2735 dm_list_add(&seg
->areas
, &area
->list
);
2741 int dm_tree_node_add_target_area(struct dm_tree_node
*node
,
2742 const char *dev_name
,
2746 struct load_segment
*seg
;
2748 struct dm_tree_node
*dev_node
;
2750 if ((!dev_name
|| !*dev_name
) && (!uuid
|| !*uuid
)) {
2751 log_error("dm_tree_node_add_target_area called without device");
2756 if (!(dev_node
= dm_tree_find_node_by_uuid(node
->dtree
, uuid
))) {
2757 log_error("Couldn't find area uuid %s.", uuid
);
2760 if (!_link_tree_nodes(node
, dev_node
))
2763 if (stat(dev_name
, &info
) < 0) {
2764 log_error("Device %s not found.", dev_name
);
2768 if (!S_ISBLK(info
.st_mode
)) {
2769 log_error("Device %s is not a block device.", dev_name
);
2773 /* FIXME Check correct macro use */
2774 if (!(dev_node
= _add_dev(node
->dtree
, node
, MAJOR(info
.st_rdev
),
2775 MINOR(info
.st_rdev
), 0)))
2779 if (!node
->props
.segment_count
) {
2780 log_error(INTERNAL_ERROR
"Attempt to add target area to missing segment.");
2784 seg
= dm_list_item(dm_list_last(&node
->props
.segs
), struct load_segment
);
2786 if (!_add_area(node
, seg
, dev_node
, offset
))
2792 int dm_tree_node_add_null_area(struct dm_tree_node
*node
, uint64_t offset
)
2794 struct load_segment
*seg
;
2796 seg
= dm_list_item(dm_list_last(&node
->props
.segs
), struct load_segment
);
2798 switch (seg
->type
) {
2810 log_error("dm_tree_node_add_null_area() called on an unsupported segment type");
2814 if (!_add_area(node
, seg
, NULL
, offset
))
2820 void dm_tree_set_cookie(struct dm_tree_node
*node
, uint32_t cookie
)
2822 node
->dtree
->cookie
= cookie
;
2825 uint32_t dm_tree_get_cookie(struct dm_tree_node
*node
)
2827 return node
->dtree
->cookie
;