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 #define REPLICATOR_LOCAL_SITE 0
29 /* Supported segment types */
56 /* FIXME Add crypt and multipath support */
62 { SEG_CRYPT
, "crypt" },
63 { SEG_ERROR
, "error" },
64 { SEG_LINEAR
, "linear" },
65 { SEG_MIRRORED
, "mirror" },
66 { SEG_REPLICATOR
, "replicator" },
67 { SEG_REPLICATOR_DEV
, "replicator-dev" },
68 { SEG_SNAPSHOT
, "snapshot" },
69 { SEG_SNAPSHOT_ORIGIN
, "snapshot-origin" },
70 { SEG_SNAPSHOT_MERGE
, "snapshot-merge" },
71 { SEG_STRIPED
, "striped" },
73 { SEG_THIN_POOL
, "thin-pool"},
75 { SEG_RAID1
, "raid1"},
76 { SEG_RAID4
, "raid4"},
77 { SEG_RAID5_LA
, "raid5_la"},
78 { SEG_RAID5_RA
, "raid5_ra"},
79 { SEG_RAID5_LS
, "raid5_ls"},
80 { SEG_RAID5_RS
, "raid5_rs"},
81 { SEG_RAID6_ZR
, "raid6_zr"},
82 { SEG_RAID6_NR
, "raid6_nr"},
83 { SEG_RAID6_NC
, "raid6_nc"},
86 *WARNING: Since 'raid' target overloads this 1:1 mapping table
87 * for search do not add new enum elements past them!
89 { SEG_RAID5_LS
, "raid5"}, /* same as "raid5_ls" (default for MD also) */
90 { SEG_RAID6_ZR
, "raid6"}, /* same as "raid6_zr" */
94 /* Some segment types have a list of areas of other devices attached */
98 struct dm_tree_node
*dev_node
;
102 unsigned rsite_index
; /* Replicator site index */
103 struct dm_tree_node
*slog
; /* Replicator sync log node */
104 uint64_t region_size
; /* Replicator sync log size */
105 uint32_t flags
; /* Replicator sync log flags */
108 struct dm_thin_message
{
109 dm_thin_message_t type
;
124 } m_set_transaction_id
;
132 struct thin_message
{
134 struct dm_thin_message message
;
138 /* Replicator-log has a list of sites */
139 /* FIXME: maybe move to seg_area too? */
140 struct replicator_site
{
143 unsigned rsite_index
;
144 dm_replicator_mode_t mode
;
145 uint32_t async_timeout
;
146 uint32_t fall_behind_ios
;
147 uint64_t fall_behind_data
;
150 /* Per-segment properties */
151 struct load_segment
{
158 unsigned area_count
; /* Linear + Striped + Mirrored + Crypt + Replicator */
159 struct dm_list areas
; /* Linear + Striped + Mirrored + Crypt + Replicator */
161 uint32_t stripe_size
; /* Striped + raid */
163 int persistent
; /* Snapshot */
164 uint32_t chunk_size
; /* Snapshot */
165 struct dm_tree_node
*cow
; /* Snapshot */
166 struct dm_tree_node
*origin
; /* Snapshot + Snapshot origin */
167 struct dm_tree_node
*merge
; /* Snapshot */
169 struct dm_tree_node
*log
; /* Mirror + Replicator */
170 uint32_t region_size
; /* Mirror + raid */
171 unsigned clustered
; /* Mirror */
172 unsigned mirror_area_count
; /* Mirror */
173 uint32_t flags
; /* Mirror log */
174 char *uuid
; /* Clustered mirror log */
176 const char *cipher
; /* Crypt */
177 const char *chainmode
; /* Crypt */
178 const char *iv
; /* Crypt */
179 uint64_t iv_offset
; /* Crypt */
180 const char *key
; /* Crypt */
182 const char *rlog_type
; /* Replicator */
183 struct dm_list rsites
; /* Replicator */
184 unsigned rsite_count
; /* Replicator */
185 unsigned rdevice_count
; /* Replicator */
186 struct dm_tree_node
*replicator
;/* Replicator-dev */
187 uint64_t rdevice_index
; /* Replicator-dev */
189 uint64_t rebuilds
; /* raid */
191 struct dm_tree_node
*metadata
; /* Thin_pool */
192 struct dm_tree_node
*pool
; /* Thin_pool, Thin */
193 struct dm_list thin_messages
; /* Thin_pool */
194 uint64_t transaction_id
; /* Thin_pool */
195 uint64_t low_water_mark
; /* Thin_pool */
196 uint32_t data_block_size
; /* Thin_pool */
197 unsigned skip_block_zeroing
; /* Thin_pool */
198 uint32_t device_id
; /* Thin */
202 /* Per-device properties */
203 struct load_properties
{
209 uint32_t read_ahead_flags
;
211 unsigned segment_count
;
212 unsigned size_changed
;
215 const char *new_name
;
217 /* If immediate_dev_node is set to 1, try to create the dev node
218 * as soon as possible (e.g. in preload stage even during traversal
219 * and processing of dm tree). This will also flush all stacked dev
220 * node operations, synchronizing with udev.
222 unsigned immediate_dev_node
;
225 * If the device size changed from zero and this is set,
226 * don't resume the device immediately, even if the device
227 * has parents. This works provided the parents do not
228 * validate the device size and is required by pvmove to
229 * avoid starting the mirror resync operation too early.
231 unsigned delay_resume_if_new
;
233 /* Send messages for this node in preload */
234 unsigned send_messages
;
237 /* Two of these used to join two nodes with uses and used_by. */
238 struct dm_tree_link
{
240 struct dm_tree_node
*node
;
243 struct dm_tree_node
{
244 struct dm_tree
*dtree
;
250 struct dm_list uses
; /* Nodes this node uses */
251 struct dm_list used_by
; /* Nodes that use this node */
253 int activation_priority
; /* 0 gets activated first */
255 uint16_t udev_flags
; /* Udev control flags */
257 void *context
; /* External supplied context */
259 struct load_properties props
; /* For creation/table (re)load */
262 * If presuspend of child node is needed
263 * Note: only direct child is allowed
265 struct dm_tree_node
*presuspend_node
;
270 struct dm_hash_table
*devs
;
271 struct dm_hash_table
*uuids
;
272 struct dm_tree_node root
;
273 int skip_lockfs
; /* 1 skips lockfs (for non-snapshots) */
274 int no_flush
; /* 1 sets noflush (mirrors/multipath) */
275 int retry_remove
; /* 1 retries remove if not successful */
282 struct dm_tree
*dm_tree_create(void)
284 struct dm_pool
*dmem
;
285 struct dm_tree
*dtree
;
287 if (!(dmem
= dm_pool_create("dtree", 1024)) ||
288 !(dtree
= dm_pool_zalloc(dmem
, sizeof(*dtree
)))) {
289 log_error("Failed to allocate dtree.");
291 dm_pool_destroy(dmem
);
295 dtree
->root
.dtree
= dtree
;
296 dm_list_init(&dtree
->root
.uses
);
297 dm_list_init(&dtree
->root
.used_by
);
298 dtree
->skip_lockfs
= 0;
302 if (!(dtree
->devs
= dm_hash_create(8))) {
303 log_error("dtree hash creation failed");
304 dm_pool_destroy(dtree
->mem
);
308 if (!(dtree
->uuids
= dm_hash_create(32))) {
309 log_error("dtree uuid hash creation failed");
310 dm_hash_destroy(dtree
->devs
);
311 dm_pool_destroy(dtree
->mem
);
318 void dm_tree_free(struct dm_tree
*dtree
)
323 dm_hash_destroy(dtree
->uuids
);
324 dm_hash_destroy(dtree
->devs
);
325 dm_pool_destroy(dtree
->mem
);
328 void dm_tree_set_cookie(struct dm_tree_node
*node
, uint32_t cookie
)
330 node
->dtree
->cookie
= cookie
;
333 uint32_t dm_tree_get_cookie(struct dm_tree_node
*node
)
335 return node
->dtree
->cookie
;
338 void dm_tree_skip_lockfs(struct dm_tree_node
*dnode
)
340 dnode
->dtree
->skip_lockfs
= 1;
343 void dm_tree_use_no_flush_suspend(struct dm_tree_node
*dnode
)
345 dnode
->dtree
->no_flush
= 1;
348 void dm_tree_retry_remove(struct dm_tree_node
*dnode
)
350 dnode
->dtree
->retry_remove
= 1;
356 static int _nodes_are_linked(const struct dm_tree_node
*parent
,
357 const struct dm_tree_node
*child
)
359 struct dm_tree_link
*dlink
;
361 dm_list_iterate_items(dlink
, &parent
->uses
)
362 if (dlink
->node
== child
)
368 static int _link(struct dm_list
*list
, struct dm_tree_node
*node
)
370 struct dm_tree_link
*dlink
;
372 if (!(dlink
= dm_pool_alloc(node
->dtree
->mem
, sizeof(*dlink
)))) {
373 log_error("dtree link allocation failed");
378 dm_list_add(list
, &dlink
->list
);
383 static int _link_nodes(struct dm_tree_node
*parent
,
384 struct dm_tree_node
*child
)
386 if (_nodes_are_linked(parent
, child
))
389 if (!_link(&parent
->uses
, child
))
392 if (!_link(&child
->used_by
, parent
))
398 static void _unlink(struct dm_list
*list
, struct dm_tree_node
*node
)
400 struct dm_tree_link
*dlink
;
402 dm_list_iterate_items(dlink
, list
)
403 if (dlink
->node
== node
) {
404 dm_list_del(&dlink
->list
);
409 static void _unlink_nodes(struct dm_tree_node
*parent
,
410 struct dm_tree_node
*child
)
412 if (!_nodes_are_linked(parent
, child
))
415 _unlink(&parent
->uses
, child
);
416 _unlink(&child
->used_by
, parent
);
419 static int _add_to_toplevel(struct dm_tree_node
*node
)
421 return _link_nodes(&node
->dtree
->root
, node
);
424 static void _remove_from_toplevel(struct dm_tree_node
*node
)
426 _unlink_nodes(&node
->dtree
->root
, node
);
429 static int _add_to_bottomlevel(struct dm_tree_node
*node
)
431 return _link_nodes(node
, &node
->dtree
->root
);
434 static void _remove_from_bottomlevel(struct dm_tree_node
*node
)
436 _unlink_nodes(node
, &node
->dtree
->root
);
439 static int _link_tree_nodes(struct dm_tree_node
*parent
, struct dm_tree_node
*child
)
441 /* Don't link to root node if child already has a parent */
442 if (parent
== &parent
->dtree
->root
) {
443 if (dm_tree_node_num_children(child
, 1))
446 _remove_from_toplevel(child
);
448 if (child
== &child
->dtree
->root
) {
449 if (dm_tree_node_num_children(parent
, 0))
452 _remove_from_bottomlevel(parent
);
454 return _link_nodes(parent
, child
);
457 static struct dm_tree_node
*_create_dm_tree_node(struct dm_tree
*dtree
,
460 struct dm_info
*info
,
464 struct dm_tree_node
*node
;
467 if (!(node
= dm_pool_zalloc(dtree
->mem
, sizeof(*node
)))) {
468 log_error("_create_dm_tree_node alloc failed");
477 node
->context
= context
;
478 node
->udev_flags
= udev_flags
;
479 node
->activation_priority
= 0;
481 dm_list_init(&node
->uses
);
482 dm_list_init(&node
->used_by
);
483 dm_list_init(&node
->props
.segs
);
485 dev
= MKDEV(info
->major
, info
->minor
);
487 if (!dm_hash_insert_binary(dtree
->devs
, (const char *) &dev
,
488 sizeof(dev
), node
)) {
489 log_error("dtree node hash insertion failed");
490 dm_pool_free(dtree
->mem
, node
);
495 !dm_hash_insert(dtree
->uuids
, uuid
, node
)) {
496 log_error("dtree uuid hash insertion failed");
497 dm_hash_remove_binary(dtree
->devs
, (const char *) &dev
,
499 dm_pool_free(dtree
->mem
, node
);
506 static struct dm_tree_node
*_find_dm_tree_node(struct dm_tree
*dtree
,
507 uint32_t major
, uint32_t minor
)
509 uint64_t dev
= MKDEV(major
, minor
);
511 return dm_hash_lookup_binary(dtree
->devs
, (const char *) &dev
,
515 static struct dm_tree_node
*_find_dm_tree_node_by_uuid(struct dm_tree
*dtree
,
518 struct dm_tree_node
*node
;
519 const char *default_uuid_prefix
;
520 size_t default_uuid_prefix_len
;
522 if ((node
= dm_hash_lookup(dtree
->uuids
, uuid
)))
525 default_uuid_prefix
= dm_uuid_prefix();
526 default_uuid_prefix_len
= strlen(default_uuid_prefix
);
528 if (strncmp(uuid
, default_uuid_prefix
, default_uuid_prefix_len
))
531 return dm_hash_lookup(dtree
->uuids
, uuid
+ default_uuid_prefix_len
);
534 void dm_tree_node_set_udev_flags(struct dm_tree_node
*dnode
, uint16_t udev_flags
)
537 struct dm_info
*dinfo
= &dnode
->info
;
539 if (udev_flags
!= dnode
->udev_flags
)
540 log_debug("Resetting %s (%" PRIu32
":%" PRIu32
541 ") udev_flags from 0x%x to 0x%x",
542 dnode
->name
, dinfo
->major
, dinfo
->minor
,
543 dnode
->udev_flags
, udev_flags
);
544 dnode
->udev_flags
= udev_flags
;
547 void dm_tree_node_set_read_ahead(struct dm_tree_node
*dnode
,
549 uint32_t read_ahead_flags
)
551 dnode
->props
.read_ahead
= read_ahead
;
552 dnode
->props
.read_ahead_flags
= read_ahead_flags
;
555 void dm_tree_node_set_presuspend_node(struct dm_tree_node
*node
,
556 struct dm_tree_node
*presuspend_node
)
558 node
->presuspend_node
= presuspend_node
;
561 const char *dm_tree_node_get_name(const struct dm_tree_node
*node
)
563 return node
->info
.exists
? node
->name
: "";
566 const char *dm_tree_node_get_uuid(const struct dm_tree_node
*node
)
568 return node
->info
.exists
? node
->uuid
: "";
571 const struct dm_info
*dm_tree_node_get_info(const struct dm_tree_node
*node
)
576 void *dm_tree_node_get_context(const struct dm_tree_node
*node
)
578 return node
->context
;
581 int dm_tree_node_size_changed(const struct dm_tree_node
*dnode
)
583 return dnode
->props
.size_changed
;
586 int dm_tree_node_num_children(const struct dm_tree_node
*node
, uint32_t inverted
)
589 if (_nodes_are_linked(&node
->dtree
->root
, node
))
591 return dm_list_size(&node
->used_by
);
594 if (_nodes_are_linked(node
, &node
->dtree
->root
))
597 return dm_list_size(&node
->uses
);
601 * Returns 1 if no prefix supplied
603 static int _uuid_prefix_matches(const char *uuid
, const char *uuid_prefix
, size_t uuid_prefix_len
)
605 const char *default_uuid_prefix
= dm_uuid_prefix();
606 size_t default_uuid_prefix_len
= strlen(default_uuid_prefix
);
611 if (!strncmp(uuid
, uuid_prefix
, uuid_prefix_len
))
614 /* Handle transition: active device uuids might be missing the prefix */
615 if (uuid_prefix_len
<= 4)
618 if (!strncmp(uuid
, default_uuid_prefix
, default_uuid_prefix_len
))
621 if (strncmp(uuid_prefix
, default_uuid_prefix
, default_uuid_prefix_len
))
624 if (!strncmp(uuid
, uuid_prefix
+ default_uuid_prefix_len
, uuid_prefix_len
- default_uuid_prefix_len
))
631 * Returns 1 if no children.
633 static int _children_suspended(struct dm_tree_node
*node
,
635 const char *uuid_prefix
,
636 size_t uuid_prefix_len
)
638 struct dm_list
*list
;
639 struct dm_tree_link
*dlink
;
640 const struct dm_info
*dinfo
;
644 if (_nodes_are_linked(&node
->dtree
->root
, node
))
646 list
= &node
->used_by
;
648 if (_nodes_are_linked(node
, &node
->dtree
->root
))
653 dm_list_iterate_items(dlink
, list
) {
654 if (!(uuid
= dm_tree_node_get_uuid(dlink
->node
))) {
659 /* Ignore if it doesn't belong to this VG */
660 if (!_uuid_prefix_matches(uuid
, uuid_prefix
, uuid_prefix_len
))
663 /* Ignore if parent node wants to presuspend this node */
664 if (dlink
->node
->presuspend_node
== node
)
667 if (!(dinfo
= dm_tree_node_get_info(dlink
->node
))) {
668 stack
; /* FIXME Is this normal? */
672 if (!dinfo
->suspended
)
680 * Set major and minor to zero for root of tree.
682 struct dm_tree_node
*dm_tree_find_node(struct dm_tree
*dtree
,
686 if (!major
&& !minor
)
689 return _find_dm_tree_node(dtree
, major
, minor
);
693 * Set uuid to NULL for root of tree.
695 struct dm_tree_node
*dm_tree_find_node_by_uuid(struct dm_tree
*dtree
,
701 return _find_dm_tree_node_by_uuid(dtree
, uuid
);
705 * First time set *handle to NULL.
706 * Set inverted to invert the tree.
708 struct dm_tree_node
*dm_tree_next_child(void **handle
,
709 const struct dm_tree_node
*parent
,
712 struct dm_list
**dlink
= (struct dm_list
**) handle
;
713 const struct dm_list
*use_list
;
716 use_list
= &parent
->used_by
;
718 use_list
= &parent
->uses
;
721 *dlink
= dm_list_first(use_list
);
723 *dlink
= dm_list_next(use_list
, *dlink
);
725 return (*dlink
) ? dm_list_item(*dlink
, struct dm_tree_link
)->node
: NULL
;
728 static int _deps(struct dm_task
**dmt
, struct dm_pool
*mem
, uint32_t major
, uint32_t minor
,
729 const char **name
, const char **uuid
, unsigned inactive_table
,
730 struct dm_info
*info
, struct dm_deps
**deps
)
732 memset(info
, 0, sizeof(*info
));
734 if (!dm_is_dm_major(major
)) {
743 info
->live_table
= 0;
744 info
->inactive_table
= 0;
749 if (!(*dmt
= dm_task_create(DM_DEVICE_DEPS
))) {
750 log_error("deps dm_task creation failed");
754 if (!dm_task_set_major(*dmt
, major
)) {
755 log_error("_deps: failed to set major for (%" PRIu32
":%" PRIu32
")",
760 if (!dm_task_set_minor(*dmt
, minor
)) {
761 log_error("_deps: failed to set minor for (%" PRIu32
":%" PRIu32
")",
766 if (inactive_table
&& !dm_task_query_inactive_table(*dmt
)) {
767 log_error("_deps: failed to set inactive table for (%" PRIu32
":%" PRIu32
")",
772 if (!dm_task_run(*dmt
)) {
773 log_error("_deps: task run failed for (%" PRIu32
":%" PRIu32
")",
778 if (!dm_task_get_info(*dmt
, info
)) {
779 log_error("_deps: failed to get info for (%" PRIu32
":%" PRIu32
")",
791 if (info
->major
!= major
) {
792 log_error("Inconsistent dtree major number: %u != %u",
796 if (info
->minor
!= minor
) {
797 log_error("Inconsistent dtree minor number: %u != %u",
801 if (name
&& !(*name
= dm_pool_strdup(mem
, dm_task_get_name(*dmt
)))) {
802 log_error("name pool_strdup failed");
805 if (uuid
&& !(*uuid
= dm_pool_strdup(mem
, dm_task_get_uuid(*dmt
)))) {
806 log_error("uuid pool_strdup failed");
809 *deps
= dm_task_get_deps(*dmt
);
815 dm_task_destroy(*dmt
);
820 * Deactivate a device with its dependencies if the uuid prefix matches.
822 static int _info_by_dev(uint32_t major
, uint32_t minor
, int with_open_count
,
823 struct dm_info
*info
, struct dm_pool
*mem
,
824 const char **name
, const char **uuid
)
829 if (!(dmt
= dm_task_create(DM_DEVICE_INFO
))) {
830 log_error("_info_by_dev: dm_task creation failed");
834 if (!dm_task_set_major(dmt
, major
) || !dm_task_set_minor(dmt
, minor
)) {
835 log_error("_info_by_dev: Failed to set device number");
836 dm_task_destroy(dmt
);
840 if (!with_open_count
&& !dm_task_no_open_count(dmt
))
841 log_error("Failed to disable open_count");
843 if (!(r
= dm_task_run(dmt
)))
846 if (!(r
= dm_task_get_info(dmt
, info
)))
849 if (name
&& !(*name
= dm_pool_strdup(mem
, dm_task_get_name(dmt
)))) {
850 log_error("name pool_strdup failed");
855 if (uuid
&& !(*uuid
= dm_pool_strdup(mem
, dm_task_get_uuid(dmt
)))) {
856 log_error("uuid pool_strdup failed");
862 dm_task_destroy(dmt
);
867 static int _check_device_not_in_use(const char *name
, struct dm_info
*info
)
872 /* If sysfs is not used, use open_count information only. */
873 if (!*dm_sysfs_dir()) {
874 if (info
->open_count
) {
875 log_error("Device %s (%" PRIu32
":%" PRIu32
") in use",
876 name
, info
->major
, info
->minor
);
883 if (dm_device_has_holders(info
->major
, info
->minor
)) {
884 log_error("Device %s (%" PRIu32
":%" PRIu32
") is used "
885 "by another device.", name
, info
->major
, info
->minor
);
889 if (dm_device_has_mounted_fs(info
->major
, info
->minor
)) {
890 log_error("Device %s (%" PRIu32
":%" PRIu32
") contains "
891 "a filesystem in use.", name
, info
->major
, info
->minor
);
898 /* Check if all parent nodes of given node have open_count == 0 */
899 static int _node_has_closed_parents(struct dm_tree_node
*node
,
900 const char *uuid_prefix
,
901 size_t uuid_prefix_len
)
903 struct dm_tree_link
*dlink
;
904 const struct dm_info
*dinfo
;
908 /* Iterate through parents of this node */
909 dm_list_iterate_items(dlink
, &node
->used_by
) {
910 if (!(uuid
= dm_tree_node_get_uuid(dlink
->node
))) {
915 /* Ignore if it doesn't belong to this VG */
916 if (!_uuid_prefix_matches(uuid
, uuid_prefix
, uuid_prefix_len
))
919 if (!(dinfo
= dm_tree_node_get_info(dlink
->node
))) {
920 stack
; /* FIXME Is this normal? */
924 /* Refresh open_count */
925 if (!_info_by_dev(dinfo
->major
, dinfo
->minor
, 1, &info
, NULL
, NULL
, NULL
) ||
929 if (info
.open_count
) {
930 log_debug("Node %s %d:%d has open_count %d", uuid_prefix
,
931 dinfo
->major
, dinfo
->minor
, info
.open_count
);
939 static int _deactivate_node(const char *name
, uint32_t major
, uint32_t minor
,
940 uint32_t *cookie
, uint16_t udev_flags
, int retry
)
945 log_verbose("Removing %s (%" PRIu32
":%" PRIu32
")", name
, major
, minor
);
947 if (!(dmt
= dm_task_create(DM_DEVICE_REMOVE
))) {
948 log_error("Deactivation dm_task creation failed for %s", name
);
952 if (!dm_task_set_major(dmt
, major
) || !dm_task_set_minor(dmt
, minor
)) {
953 log_error("Failed to set device number for %s deactivation", name
);
957 if (!dm_task_no_open_count(dmt
))
958 log_error("Failed to disable open_count");
961 if (!dm_task_set_cookie(dmt
, cookie
, udev_flags
))
965 dm_task_retry_remove(dmt
);
967 r
= dm_task_run(dmt
);
969 /* FIXME Until kernel returns actual name so dm-iface.c can handle it */
970 rm_dev_node(name
, dmt
->cookie_set
&& !(udev_flags
& DM_UDEV_DISABLE_DM_RULES_FLAG
),
971 dmt
->cookie_set
&& (udev_flags
& DM_UDEV_DISABLE_LIBRARY_FALLBACK
));
973 /* FIXME Remove node from tree or mark invalid? */
976 dm_task_destroy(dmt
);
981 static int _node_clear_table(struct dm_tree_node
*dnode
, uint16_t udev_flags
)
983 struct dm_task
*dmt
= NULL
, *deps_dmt
= NULL
;
984 struct dm_info
*info
, deps_info
;
985 struct dm_deps
*deps
= NULL
;
986 const char *name
, *uuid
;
987 const char *default_uuid_prefix
;
988 size_t default_uuid_prefix_len
;
992 if (!(info
= &dnode
->info
)) {
993 log_error("_node_clear_table failed: missing info");
997 if (!(name
= dm_tree_node_get_name(dnode
))) {
998 log_error("_node_clear_table failed: missing name");
1002 /* Is there a table? */
1003 if (!info
->exists
|| !info
->inactive_table
)
1006 /* Get devices used by inactive table that's about to be deleted. */
1007 if (!_deps(&deps_dmt
, dnode
->dtree
->mem
, info
->major
, info
->minor
, NULL
, NULL
, 1, info
, &deps
)) {
1008 log_error("Failed to obtain dependencies for %s before clearing table.", name
);
1012 log_verbose("Clearing inactive table %s (%" PRIu32
":%" PRIu32
")",
1013 name
, info
->major
, info
->minor
);
1015 if (!(dmt
= dm_task_create(DM_DEVICE_CLEAR
))) {
1016 log_error("Table clear dm_task creation failed for %s", name
);
1020 if (!dm_task_set_major(dmt
, info
->major
) ||
1021 !dm_task_set_minor(dmt
, info
->minor
)) {
1022 log_error("Failed to set device number for %s table clear", name
);
1026 r
= dm_task_run(dmt
);
1028 if (!dm_task_get_info(dmt
, info
)) {
1029 log_error("_node_clear_table failed: info missing after running task for %s", name
);
1037 * Remove (incomplete) devices that the inactive table referred to but
1038 * which are not in the tree, no longer referenced and don't have a live
1041 default_uuid_prefix
= dm_uuid_prefix();
1042 default_uuid_prefix_len
= strlen(default_uuid_prefix
);
1044 for (i
= 0; i
< deps
->count
; i
++) {
1045 /* If already in tree, assume it's under control */
1046 if (_find_dm_tree_node(dnode
->dtree
, MAJOR(deps
->device
[i
]), MINOR(deps
->device
[i
])))
1049 if (!_info_by_dev(MAJOR(deps
->device
[i
]), MINOR(deps
->device
[i
]), 1,
1050 &deps_info
, dnode
->dtree
->mem
, &name
, &uuid
))
1053 /* Proceed if device is an 'orphan' - unreferenced and without a live table. */
1054 if (!deps_info
.exists
|| deps_info
.live_table
|| deps_info
.open_count
)
1057 if (strncmp(uuid
, default_uuid_prefix
, default_uuid_prefix_len
))
1060 /* Remove device. */
1061 if (!_deactivate_node(name
, deps_info
.major
, deps_info
.minor
, &dnode
->dtree
->cookie
, udev_flags
, 0)) {
1062 log_error("Failed to deactivate no-longer-used device %s (%"
1063 PRIu32
":%" PRIu32
")", name
, deps_info
.major
, deps_info
.minor
);
1064 } else if (deps_info
.suspended
)
1070 dm_task_destroy(dmt
);
1073 dm_task_destroy(deps_dmt
);
1078 struct dm_tree_node
*dm_tree_add_new_dev_with_udev_flags(struct dm_tree
*dtree
,
1086 uint16_t udev_flags
)
1088 struct dm_tree_node
*dnode
;
1089 struct dm_info info
;
1093 /* Do we need to add node to tree? */
1094 if (!(dnode
= dm_tree_find_node_by_uuid(dtree
, uuid
))) {
1095 if (!(name2
= dm_pool_strdup(dtree
->mem
, name
))) {
1096 log_error("name pool_strdup failed");
1099 if (!(uuid2
= dm_pool_strdup(dtree
->mem
, uuid
))) {
1100 log_error("uuid pool_strdup failed");
1104 memset(&info
, 0, sizeof(info
));
1106 if (!(dnode
= _create_dm_tree_node(dtree
, name2
, uuid2
, &info
,
1110 /* Attach to root node until a table is supplied */
1111 if (!_add_to_toplevel(dnode
) || !_add_to_bottomlevel(dnode
))
1114 dnode
->props
.major
= major
;
1115 dnode
->props
.minor
= minor
;
1116 dnode
->props
.new_name
= NULL
;
1117 dnode
->props
.size_changed
= 0;
1118 } else if (strcmp(name
, dnode
->name
)) {
1119 /* Do we need to rename node? */
1120 if (!(dnode
->props
.new_name
= dm_pool_strdup(dtree
->mem
, name
))) {
1121 log_error("name pool_strdup failed");
1126 dnode
->props
.read_only
= read_only
? 1 : 0;
1127 dnode
->props
.read_ahead
= DM_READ_AHEAD_AUTO
;
1128 dnode
->props
.read_ahead_flags
= 0;
1130 if (clear_inactive
&& !_node_clear_table(dnode
, udev_flags
))
1133 dnode
->context
= context
;
1134 dnode
->udev_flags
= udev_flags
;
1139 struct dm_tree_node
*dm_tree_add_new_dev(struct dm_tree
*dtree
, const char *name
,
1140 const char *uuid
, uint32_t major
, uint32_t minor
,
1141 int read_only
, int clear_inactive
, void *context
)
1143 return dm_tree_add_new_dev_with_udev_flags(dtree
, name
, uuid
, major
, minor
,
1144 read_only
, clear_inactive
, context
, 0);
1147 static struct dm_tree_node
*_add_dev(struct dm_tree
*dtree
,
1148 struct dm_tree_node
*parent
,
1149 uint32_t major
, uint32_t minor
,
1150 uint16_t udev_flags
)
1152 struct dm_task
*dmt
= NULL
;
1153 struct dm_info info
;
1154 struct dm_deps
*deps
= NULL
;
1155 const char *name
= NULL
;
1156 const char *uuid
= NULL
;
1157 struct dm_tree_node
*node
= NULL
;
1161 /* Already in tree? */
1162 if (!(node
= _find_dm_tree_node(dtree
, major
, minor
))) {
1163 if (!_deps(&dmt
, dtree
->mem
, major
, minor
, &name
, &uuid
, 0, &info
, &deps
))
1166 if (!(node
= _create_dm_tree_node(dtree
, name
, uuid
, &info
,
1172 if (!_link_tree_nodes(parent
, node
)) {
1177 /* If node was already in tree, no need to recurse. */
1181 /* Can't recurse if not a mapped device or there are no dependencies */
1182 if (!node
->info
.exists
|| !deps
->count
) {
1183 if (!_add_to_bottomlevel(node
)) {
1190 /* Add dependencies to tree */
1191 for (i
= 0; i
< deps
->count
; i
++)
1192 if (!_add_dev(dtree
, node
, MAJOR(deps
->device
[i
]),
1193 MINOR(deps
->device
[i
]), udev_flags
)) {
1200 dm_task_destroy(dmt
);
1205 int dm_tree_add_dev(struct dm_tree
*dtree
, uint32_t major
, uint32_t minor
)
1207 return _add_dev(dtree
, &dtree
->root
, major
, minor
, 0) ? 1 : 0;
1210 int dm_tree_add_dev_with_udev_flags(struct dm_tree
*dtree
, uint32_t major
,
1211 uint32_t minor
, uint16_t udev_flags
)
1213 return _add_dev(dtree
, &dtree
->root
, major
, minor
, udev_flags
) ? 1 : 0;
1216 static int _rename_node(const char *old_name
, const char *new_name
, uint32_t major
,
1217 uint32_t minor
, uint32_t *cookie
, uint16_t udev_flags
)
1219 struct dm_task
*dmt
;
1222 log_verbose("Renaming %s (%" PRIu32
":%" PRIu32
") to %s", old_name
, major
, minor
, new_name
);
1224 if (!(dmt
= dm_task_create(DM_DEVICE_RENAME
))) {
1225 log_error("Rename dm_task creation failed for %s", old_name
);
1229 if (!dm_task_set_name(dmt
, old_name
)) {
1230 log_error("Failed to set name for %s rename.", old_name
);
1234 if (!dm_task_set_newname(dmt
, new_name
))
1237 if (!dm_task_no_open_count(dmt
))
1238 log_error("Failed to disable open_count");
1240 if (!dm_task_set_cookie(dmt
, cookie
, udev_flags
))
1243 r
= dm_task_run(dmt
);
1246 dm_task_destroy(dmt
);
1251 /* FIXME Merge with _suspend_node? */
1252 static int _resume_node(const char *name
, uint32_t major
, uint32_t minor
,
1253 uint32_t read_ahead
, uint32_t read_ahead_flags
,
1254 struct dm_info
*newinfo
, uint32_t *cookie
,
1255 uint16_t udev_flags
, int already_suspended
)
1257 struct dm_task
*dmt
;
1260 log_verbose("Resuming %s (%" PRIu32
":%" PRIu32
")", name
, major
, minor
);
1262 if (!(dmt
= dm_task_create(DM_DEVICE_RESUME
))) {
1263 log_debug("Suspend dm_task creation failed for %s.", name
);
1267 /* FIXME Kernel should fill in name on return instead */
1268 if (!dm_task_set_name(dmt
, name
)) {
1269 log_debug("Failed to set device name for %s resumption.", name
);
1273 if (!dm_task_set_major(dmt
, major
) || !dm_task_set_minor(dmt
, minor
)) {
1274 log_error("Failed to set device number for %s resumption.", name
);
1278 if (!dm_task_no_open_count(dmt
))
1279 log_error("Failed to disable open_count");
1281 if (!dm_task_set_read_ahead(dmt
, read_ahead
, read_ahead_flags
))
1282 log_error("Failed to set read ahead");
1284 if (!dm_task_set_cookie(dmt
, cookie
, udev_flags
))
1287 if (!(r
= dm_task_run(dmt
)))
1290 if (already_suspended
)
1293 if (!(r
= dm_task_get_info(dmt
, newinfo
)))
1297 dm_task_destroy(dmt
);
1302 static int _suspend_node(const char *name
, uint32_t major
, uint32_t minor
,
1303 int skip_lockfs
, int no_flush
, struct dm_info
*newinfo
)
1305 struct dm_task
*dmt
;
1308 log_verbose("Suspending %s (%" PRIu32
":%" PRIu32
")%s%s",
1310 skip_lockfs
? "" : " with filesystem sync",
1311 no_flush
? "" : " with device flush");
1313 if (!(dmt
= dm_task_create(DM_DEVICE_SUSPEND
))) {
1314 log_error("Suspend dm_task creation failed for %s", name
);
1318 if (!dm_task_set_major(dmt
, major
) || !dm_task_set_minor(dmt
, minor
)) {
1319 log_error("Failed to set device number for %s suspension.", name
);
1320 dm_task_destroy(dmt
);
1324 if (!dm_task_no_open_count(dmt
))
1325 log_error("Failed to disable open_count");
1327 if (skip_lockfs
&& !dm_task_skip_lockfs(dmt
))
1328 log_error("Failed to set skip_lockfs flag.");
1330 if (no_flush
&& !dm_task_no_flush(dmt
))
1331 log_error("Failed to set no_flush flag.");
1333 if ((r
= dm_task_run(dmt
))) {
1335 r
= dm_task_get_info(dmt
, newinfo
);
1338 dm_task_destroy(dmt
);
1343 static int _thin_pool_status_transaction_id(struct dm_tree_node
*dnode
, uint64_t *transaction_id
)
1345 struct dm_task
*dmt
;
1347 uint64_t start
, length
;
1349 char *params
= NULL
;
1351 if (!(dmt
= dm_task_create(DM_DEVICE_STATUS
)))
1354 if (!dm_task_set_major(dmt
, dnode
->info
.major
) ||
1355 !dm_task_set_minor(dmt
, dnode
->info
.minor
)) {
1356 log_error("Failed to set major minor.");
1360 if (!dm_task_run(dmt
))
1363 dm_get_next_target(dmt
, NULL
, &start
, &length
, &type
, ¶ms
);
1365 if (type
&& (strcmp(type
, "thin-pool") != 0)) {
1366 log_error("Expected thin-pool target for %d:%d and got %s.",
1367 dnode
->info
.major
, dnode
->info
.minor
, type
);
1371 if (!params
|| (sscanf(params
, "%" PRIu64
, transaction_id
) != 1)) {
1372 log_error("Failed to parse transaction_id from %s.", params
);
1376 log_debug("Thin pool transaction id: %" PRIu64
" status: %s.", *transaction_id
, params
);
1380 dm_task_destroy(dmt
);
1385 static int _thin_pool_node_message(struct dm_tree_node
*dnode
, struct thin_message
*tm
)
1387 struct dm_task
*dmt
;
1388 struct dm_thin_message
*m
= &tm
->message
;
1393 case DM_THIN_MESSAGE_CREATE_SNAP
:
1394 r
= dm_snprintf(buf
, sizeof(buf
), "create_snap %u %u",
1395 m
->u
.m_create_snap
.device_id
,
1396 m
->u
.m_create_snap
.origin_id
);
1398 case DM_THIN_MESSAGE_CREATE_THIN
:
1399 r
= dm_snprintf(buf
, sizeof(buf
), "create_thin %u",
1400 m
->u
.m_create_thin
.device_id
);
1402 case DM_THIN_MESSAGE_DELETE
:
1403 r
= dm_snprintf(buf
, sizeof(buf
), "delete %u",
1404 m
->u
.m_delete
.device_id
);
1406 case DM_THIN_MESSAGE_TRIM
:
1407 r
= dm_snprintf(buf
, sizeof(buf
), "trim %u %" PRIu64
,
1408 m
->u
.m_trim
.device_id
,
1409 m
->u
.m_trim
.new_size
);
1411 case DM_THIN_MESSAGE_SET_TRANSACTION_ID
:
1412 r
= dm_snprintf(buf
, sizeof(buf
),
1413 "set_transaction_id %" PRIu64
" %" PRIu64
,
1414 m
->u
.m_set_transaction_id
.current_id
,
1415 m
->u
.m_set_transaction_id
.new_id
);
1422 log_error("Failed to prepare message.");
1428 if (!(dmt
= dm_task_create(DM_DEVICE_TARGET_MSG
)))
1431 if (!dm_task_set_major(dmt
, dnode
->info
.major
) ||
1432 !dm_task_set_minor(dmt
, dnode
->info
.minor
)) {
1433 log_error("Failed to set message major minor.");
1437 if (!dm_task_set_message(dmt
, buf
))
1440 /* Internal functionality of dm_task */
1441 dmt
->expected_errno
= tm
->expected_errno
;
1443 if (!dm_task_run(dmt
))
1448 dm_task_destroy(dmt
);
1453 static int _node_send_messages(struct dm_tree_node
*dnode
,
1454 const char *uuid_prefix
,
1455 size_t uuid_prefix_len
)
1457 struct load_segment
*seg
;
1458 struct thin_message
*tmsg
;
1462 if (!dnode
->info
.exists
|| (dm_list_size(&dnode
->props
.segs
) != 1))
1465 seg
= dm_list_item(dm_list_last(&dnode
->props
.segs
), struct load_segment
);
1466 if (seg
->type
!= SEG_THIN_POOL
)
1469 if (!(uuid
= dm_tree_node_get_uuid(dnode
)))
1472 if (!_uuid_prefix_matches(uuid
, uuid_prefix
, uuid_prefix_len
)) {
1473 log_debug("UUID \"%s\" does not match.", uuid
);
1477 if (!_thin_pool_status_transaction_id(dnode
, &trans_id
))
1480 if (trans_id
== seg
->transaction_id
)
1481 return 1; /* In sync - skip messages */
1483 if (trans_id
!= (seg
->transaction_id
- 1)) {
1484 log_error("Thin pool transaction_id=%" PRIu64
", while expected: %" PRIu64
".",
1485 trans_id
, seg
->transaction_id
- 1);
1486 goto bad
; /* Nothing to send */
1489 dm_list_iterate_items(tmsg
, &seg
->thin_messages
)
1490 if (!(_thin_pool_node_message(dnode
, tmsg
)))
1495 /* Try to deactivate */
1496 if (!(dm_tree_deactivate_children(dnode
, uuid_prefix
, uuid_prefix_len
)))
1497 log_error("Failed to deactivate %s", dnode
->name
);
1503 * FIXME Don't attempt to deactivate known internal dependencies.
1505 static int _dm_tree_deactivate_children(struct dm_tree_node
*dnode
,
1506 const char *uuid_prefix
,
1507 size_t uuid_prefix_len
,
1511 void *handle
= NULL
;
1512 struct dm_tree_node
*child
= dnode
;
1513 struct dm_info info
;
1514 const struct dm_info
*dinfo
;
1518 while ((child
= dm_tree_next_child(&handle
, dnode
, 0))) {
1519 if (!(dinfo
= dm_tree_node_get_info(child
))) {
1524 if (!(name
= dm_tree_node_get_name(child
))) {
1529 if (!(uuid
= dm_tree_node_get_uuid(child
))) {
1534 /* Ignore if it doesn't belong to this VG */
1535 if (!_uuid_prefix_matches(uuid
, uuid_prefix
, uuid_prefix_len
))
1538 /* Refresh open_count */
1539 if (!_info_by_dev(dinfo
->major
, dinfo
->minor
, 1, &info
, NULL
, NULL
, NULL
) ||
1543 if (info
.open_count
) {
1544 /* Skip internal non-toplevel opened nodes */
1548 /* When retry is not allowed, error */
1549 if (!child
->dtree
->retry_remove
) {
1550 log_error("Unable to deactivate open %s (%" PRIu32
1551 ":%" PRIu32
")", name
, info
.major
, info
.minor
);
1556 /* Check toplevel node for holders/mounted fs */
1557 if (!_check_device_not_in_use(name
, &info
)) {
1562 /* Go on with retry */
1565 /* Also checking open_count in parent nodes of presuspend_node */
1566 if ((child
->presuspend_node
&&
1567 !_node_has_closed_parents(child
->presuspend_node
,
1568 uuid_prefix
, uuid_prefix_len
))) {
1569 /* Only report error from (likely non-internal) dependency at top level */
1571 log_error("Unable to deactivate open %s (%" PRIu32
1572 ":%" PRIu32
")", name
, info
.major
,
1579 /* Suspend child node first if requested */
1580 if (child
->presuspend_node
&&
1581 !dm_tree_suspend_children(child
, uuid_prefix
, uuid_prefix_len
))
1584 if (!_deactivate_node(name
, info
.major
, info
.minor
,
1585 &child
->dtree
->cookie
, child
->udev_flags
,
1586 (level
== 0) ? child
->dtree
->retry_remove
: 0)) {
1587 log_error("Unable to deactivate %s (%" PRIu32
1588 ":%" PRIu32
")", name
, info
.major
,
1592 } else if (info
.suspended
)
1595 if (dm_tree_node_num_children(child
, 0)) {
1596 if (!_dm_tree_deactivate_children(child
, uuid_prefix
, uuid_prefix_len
, level
+ 1))
1604 int dm_tree_deactivate_children(struct dm_tree_node
*dnode
,
1605 const char *uuid_prefix
,
1606 size_t uuid_prefix_len
)
1608 return _dm_tree_deactivate_children(dnode
, uuid_prefix
, uuid_prefix_len
, 0);
1611 int dm_tree_suspend_children(struct dm_tree_node
*dnode
,
1612 const char *uuid_prefix
,
1613 size_t uuid_prefix_len
)
1616 void *handle
= NULL
;
1617 struct dm_tree_node
*child
= dnode
;
1618 struct dm_info info
, newinfo
;
1619 const struct dm_info
*dinfo
;
1623 /* Suspend nodes at this level of the tree */
1624 while ((child
= dm_tree_next_child(&handle
, dnode
, 0))) {
1625 if (!(dinfo
= dm_tree_node_get_info(child
))) {
1630 if (!(name
= dm_tree_node_get_name(child
))) {
1635 if (!(uuid
= dm_tree_node_get_uuid(child
))) {
1640 /* Ignore if it doesn't belong to this VG */
1641 if (!_uuid_prefix_matches(uuid
, uuid_prefix
, uuid_prefix_len
))
1644 /* Ensure immediate parents are already suspended */
1645 if (!_children_suspended(child
, 1, uuid_prefix
, uuid_prefix_len
))
1648 if (!_info_by_dev(dinfo
->major
, dinfo
->minor
, 0, &info
, NULL
, NULL
, NULL
) ||
1649 !info
.exists
|| info
.suspended
)
1652 if (!_suspend_node(name
, info
.major
, info
.minor
,
1653 child
->dtree
->skip_lockfs
,
1654 child
->dtree
->no_flush
, &newinfo
)) {
1655 log_error("Unable to suspend %s (%" PRIu32
1656 ":%" PRIu32
")", name
, info
.major
,
1662 /* Update cached info */
1663 child
->info
= newinfo
;
1666 /* Then suspend any child nodes */
1669 while ((child
= dm_tree_next_child(&handle
, dnode
, 0))) {
1670 if (!(uuid
= dm_tree_node_get_uuid(child
))) {
1675 /* Ignore if it doesn't belong to this VG */
1676 if (!_uuid_prefix_matches(uuid
, uuid_prefix
, uuid_prefix_len
))
1679 if (dm_tree_node_num_children(child
, 0))
1680 if (!dm_tree_suspend_children(child
, uuid_prefix
, uuid_prefix_len
))
1687 int dm_tree_activate_children(struct dm_tree_node
*dnode
,
1688 const char *uuid_prefix
,
1689 size_t uuid_prefix_len
)
1692 void *handle
= NULL
;
1693 struct dm_tree_node
*child
= dnode
;
1694 struct dm_info newinfo
;
1699 /* Activate children first */
1700 while ((child
= dm_tree_next_child(&handle
, dnode
, 0))) {
1701 if (!(uuid
= dm_tree_node_get_uuid(child
))) {
1706 if (!_uuid_prefix_matches(uuid
, uuid_prefix
, uuid_prefix_len
))
1709 if (dm_tree_node_num_children(child
, 0))
1710 if (!dm_tree_activate_children(child
, uuid_prefix
, uuid_prefix_len
))
1716 for (priority
= 0; priority
< 3; priority
++) {
1717 while ((child
= dm_tree_next_child(&handle
, dnode
, 0))) {
1718 if (priority
!= child
->activation_priority
)
1721 if (!(uuid
= dm_tree_node_get_uuid(child
))) {
1726 if (!_uuid_prefix_matches(uuid
, uuid_prefix
, uuid_prefix_len
))
1729 if (!(name
= dm_tree_node_get_name(child
))) {
1735 if (child
->props
.new_name
) {
1736 if (!_rename_node(name
, child
->props
.new_name
, child
->info
.major
,
1737 child
->info
.minor
, &child
->dtree
->cookie
,
1738 child
->udev_flags
)) {
1739 log_error("Failed to rename %s (%" PRIu32
1740 ":%" PRIu32
") to %s", name
, child
->info
.major
,
1741 child
->info
.minor
, child
->props
.new_name
);
1744 child
->name
= child
->props
.new_name
;
1745 child
->props
.new_name
= NULL
;
1748 if (!child
->info
.inactive_table
&& !child
->info
.suspended
)
1751 if (!_resume_node(child
->name
, child
->info
.major
, child
->info
.minor
,
1752 child
->props
.read_ahead
, child
->props
.read_ahead_flags
,
1753 &newinfo
, &child
->dtree
->cookie
, child
->udev_flags
, child
->info
.suspended
)) {
1754 log_error("Unable to resume %s (%" PRIu32
1755 ":%" PRIu32
")", child
->name
, child
->info
.major
,
1761 /* Update cached info */
1762 child
->info
= newinfo
;
1767 * FIXME: Implement delayed error reporting
1768 * activation should be stopped only in the case,
1769 * the submission of transation_id message fails,
1770 * resume should continue further, just whole command
1771 * has to report failure.
1773 if (r
&& dnode
->props
.send_messages
&&
1774 !(r
= _node_send_messages(dnode
, uuid_prefix
, uuid_prefix_len
)))
1782 static int _create_node(struct dm_tree_node
*dnode
)
1785 struct dm_task
*dmt
;
1787 log_verbose("Creating %s", dnode
->name
);
1789 if (!(dmt
= dm_task_create(DM_DEVICE_CREATE
))) {
1790 log_error("Create dm_task creation failed for %s", dnode
->name
);
1794 if (!dm_task_set_name(dmt
, dnode
->name
)) {
1795 log_error("Failed to set device name for %s", dnode
->name
);
1799 if (!dm_task_set_uuid(dmt
, dnode
->uuid
)) {
1800 log_error("Failed to set uuid for %s", dnode
->name
);
1804 if (dnode
->props
.major
&&
1805 (!dm_task_set_major(dmt
, dnode
->props
.major
) ||
1806 !dm_task_set_minor(dmt
, dnode
->props
.minor
))) {
1807 log_error("Failed to set device number for %s creation.", dnode
->name
);
1811 if (dnode
->props
.read_only
&& !dm_task_set_ro(dmt
)) {
1812 log_error("Failed to set read only flag for %s", dnode
->name
);
1816 if (!dm_task_no_open_count(dmt
))
1817 log_error("Failed to disable open_count");
1819 if ((r
= dm_task_run(dmt
)))
1820 r
= dm_task_get_info(dmt
, &dnode
->info
);
1823 dm_task_destroy(dmt
);
1829 static int _build_dev_string(char *devbuf
, size_t bufsize
, struct dm_tree_node
*node
)
1831 if (!dm_format_dev(devbuf
, bufsize
, node
->info
.major
, node
->info
.minor
)) {
1832 log_error("Failed to format %s device number for %s as dm "
1834 node
->name
, node
->uuid
, node
->info
.major
, node
->info
.minor
);
1841 /* simplify string emiting code */
1842 #define EMIT_PARAMS(p, str...)\
1845 if ((w = dm_snprintf(params + p, paramsize - (size_t) p, str)) < 0) {\
1846 stack; /* Out of space */\
1855 * Returns: 1 on success, 0 on failure
1857 static int _emit_areas_line(struct dm_task
*dmt
__attribute__((unused
)),
1858 struct load_segment
*seg
, char *params
,
1859 size_t paramsize
, int *pos
)
1861 struct seg_area
*area
;
1862 char devbuf
[DM_FORMAT_DEV_BUFSIZE
];
1863 unsigned first_time
= 1;
1864 const char *logtype
, *synctype
;
1865 unsigned log_parm_count
;
1867 dm_list_iterate_items(area
, &seg
->areas
) {
1868 switch (seg
->type
) {
1869 case SEG_REPLICATOR_DEV
:
1870 if (!_build_dev_string(devbuf
, sizeof(devbuf
), area
->dev_node
))
1873 EMIT_PARAMS(*pos
, " %d 1 %s", area
->rsite_index
, devbuf
);
1875 EMIT_PARAMS(*pos
, " nolog 0");
1877 /* Remote devices */
1878 log_parm_count
= (area
->flags
&
1879 (DM_NOSYNC
| DM_FORCESYNC
)) ? 2 : 1;
1882 devbuf
[0] = 0; /* Only core log parameters */
1885 devbuf
[0] = ' '; /* Extra space before device name */
1886 if (!_build_dev_string(devbuf
+ 1,
1891 log_parm_count
++; /* Extra sync log device name parameter */
1894 EMIT_PARAMS(*pos
, " %s %u%s %" PRIu64
, logtype
,
1895 log_parm_count
, devbuf
, area
->region_size
);
1897 synctype
= (area
->flags
& DM_NOSYNC
) ?
1898 " nosync" : (area
->flags
& DM_FORCESYNC
) ?
1902 EMIT_PARAMS(*pos
, "%s", synctype
);
1914 if (!area
->dev_node
) {
1915 EMIT_PARAMS(*pos
, " -");
1918 if (!_build_dev_string(devbuf
, sizeof(devbuf
), area
->dev_node
))
1921 EMIT_PARAMS(*pos
, " %s", devbuf
);
1924 if (!_build_dev_string(devbuf
, sizeof(devbuf
), area
->dev_node
))
1927 EMIT_PARAMS(*pos
, "%s%s %" PRIu64
, first_time
? "" : " ",
1928 devbuf
, area
->offset
);
1937 static int _replicator_emit_segment_line(const struct load_segment
*seg
, char *params
,
1938 size_t paramsize
, int *pos
)
1940 const struct load_segment
*rlog_seg
;
1941 struct replicator_site
*rsite
;
1942 char rlogbuf
[DM_FORMAT_DEV_BUFSIZE
];
1943 unsigned parm_count
;
1945 if (!seg
->log
|| !_build_dev_string(rlogbuf
, sizeof(rlogbuf
), seg
->log
))
1948 rlog_seg
= dm_list_item(dm_list_last(&seg
->log
->props
.segs
),
1949 struct load_segment
);
1951 EMIT_PARAMS(*pos
, "%s 4 %s 0 auto %" PRIu64
,
1952 seg
->rlog_type
, rlogbuf
, rlog_seg
->size
);
1954 dm_list_iterate_items(rsite
, &seg
->rsites
) {
1955 parm_count
= (rsite
->fall_behind_data
1956 || rsite
->fall_behind_ios
1957 || rsite
->async_timeout
) ? 4 : 2;
1959 EMIT_PARAMS(*pos
, " blockdev %u %u %s", parm_count
, rsite
->rsite_index
,
1960 (rsite
->mode
== DM_REPLICATOR_SYNC
) ? "synchronous" : "asynchronous");
1962 if (rsite
->fall_behind_data
)
1963 EMIT_PARAMS(*pos
, " data %" PRIu64
, rsite
->fall_behind_data
);
1964 else if (rsite
->fall_behind_ios
)
1965 EMIT_PARAMS(*pos
, " ios %" PRIu32
, rsite
->fall_behind_ios
);
1966 else if (rsite
->async_timeout
)
1967 EMIT_PARAMS(*pos
, " timeout %" PRIu32
, rsite
->async_timeout
);
1974 * Returns: 1 on success, 0 on failure
1976 static int _mirror_emit_segment_line(struct dm_task
*dmt
, struct load_segment
*seg
,
1977 char *params
, size_t paramsize
)
1979 int block_on_error
= 0;
1980 int handle_errors
= 0;
1981 int dm_log_userspace
= 0;
1983 unsigned log_parm_count
;
1985 char logbuf
[DM_FORMAT_DEV_BUFSIZE
];
1986 const char *logtype
;
1987 unsigned kmaj
= 0, kmin
= 0, krel
= 0;
1989 if (uname(&uts
) == -1) {
1990 log_error("Cannot read kernel release version.");
1994 /* Kernels with a major number of 2 always had 3 parts. */
1995 parts
= sscanf(uts
.release
, "%u.%u.%u", &kmaj
, &kmin
, &krel
);
1996 if (parts
< 1 || (kmaj
< 3 && parts
< 3)) {
1997 log_error("Wrong kernel release version %s.", uts
.release
);
2001 if ((seg
->flags
& DM_BLOCK_ON_ERROR
)) {
2003 * Originally, block_on_error was an argument to the log
2004 * portion of the mirror CTR table. It was renamed to
2005 * "handle_errors" and now resides in the 'features'
2006 * section of the mirror CTR table (i.e. at the end).
2008 * We can identify whether to use "block_on_error" or
2009 * "handle_errors" by the dm-mirror module's version
2010 * number (>= 1.12) or by the kernel version (>= 2.6.22).
2012 if (KERNEL_VERSION(kmaj
, kmin
, krel
) >= KERNEL_VERSION(2, 6, 22))
2018 if (seg
->clustered
) {
2019 /* Cluster mirrors require a UUID */
2024 * Cluster mirrors used to have their own log
2025 * types. Now they are accessed through the
2026 * userspace log type.
2028 * The dm-log-userspace module was added to the
2031 if (KERNEL_VERSION(kmaj
, kmin
, krel
) >= KERNEL_VERSION(2, 6, 31))
2032 dm_log_userspace
= 1;
2038 /* [no]sync, block_on_error etc. */
2039 log_parm_count
+= hweight32(seg
->flags
);
2041 /* "handle_errors" is a feature arg now */
2045 /* DM_CORELOG does not count in the param list */
2046 if (seg
->flags
& DM_CORELOG
)
2049 if (seg
->clustered
) {
2050 log_parm_count
++; /* For UUID */
2052 if (!dm_log_userspace
)
2053 EMIT_PARAMS(pos
, "clustered-");
2055 /* For clustered-* type field inserted later */
2064 if (!_build_dev_string(logbuf
, sizeof(logbuf
), seg
->log
))
2068 if (dm_log_userspace
)
2069 EMIT_PARAMS(pos
, "userspace %u %s clustered-%s",
2070 log_parm_count
, seg
->uuid
, logtype
);
2072 EMIT_PARAMS(pos
, "%s %u", logtype
, log_parm_count
);
2075 EMIT_PARAMS(pos
, " %s", logbuf
);
2077 EMIT_PARAMS(pos
, " %u", seg
->region_size
);
2079 if (seg
->clustered
&& !dm_log_userspace
)
2080 EMIT_PARAMS(pos
, " %s", seg
->uuid
);
2082 if ((seg
->flags
& DM_NOSYNC
))
2083 EMIT_PARAMS(pos
, " nosync");
2084 else if ((seg
->flags
& DM_FORCESYNC
))
2085 EMIT_PARAMS(pos
, " sync");
2088 EMIT_PARAMS(pos
, " block_on_error");
2090 EMIT_PARAMS(pos
, " %u ", seg
->mirror_area_count
);
2092 if (_emit_areas_line(dmt
, seg
, params
, paramsize
, &pos
) <= 0)
2096 EMIT_PARAMS(pos
, " 1 handle_errors");
2101 static int _raid_emit_segment_line(struct dm_task
*dmt
, uint32_t major
,
2102 uint32_t minor
, struct load_segment
*seg
,
2103 uint64_t *seg_start
, char *params
,
2107 int param_count
= 1; /* mandatory 'chunk size'/'stripe size' arg */
2110 if ((seg
->flags
& DM_NOSYNC
) || (seg
->flags
& DM_FORCESYNC
))
2113 if (seg
->region_size
)
2116 /* rebuilds is 64-bit */
2117 param_count
+= 2 * hweight32(seg
->rebuilds
& 0xFFFFFFFF);
2118 param_count
+= 2 * hweight32(seg
->rebuilds
>> 32);
2120 if ((seg
->type
== SEG_RAID1
) && seg
->stripe_size
)
2121 log_error("WARNING: Ignoring RAID1 stripe size");
2123 EMIT_PARAMS(pos
, "%s %d %u", dm_segtypes
[seg
->type
].target
,
2124 param_count
, seg
->stripe_size
);
2126 if (seg
->flags
& DM_NOSYNC
)
2127 EMIT_PARAMS(pos
, " nosync");
2128 else if (seg
->flags
& DM_FORCESYNC
)
2129 EMIT_PARAMS(pos
, " sync");
2131 if (seg
->region_size
)
2132 EMIT_PARAMS(pos
, " region_size %u", seg
->region_size
);
2134 for (i
= 0; i
< (seg
->area_count
/ 2); i
++)
2135 if (seg
->rebuilds
& (1 << i
))
2136 EMIT_PARAMS(pos
, " rebuild %u", i
);
2138 /* Print number of metadata/data device pairs */
2139 EMIT_PARAMS(pos
, " %u", seg
->area_count
/2);
2141 if (_emit_areas_line(dmt
, seg
, params
, paramsize
, &pos
) <= 0)
2147 static int _emit_segment_line(struct dm_task
*dmt
, uint32_t major
,
2148 uint32_t minor
, struct load_segment
*seg
,
2149 uint64_t *seg_start
, char *params
,
2154 int target_type_is_raid
= 0;
2155 char originbuf
[DM_FORMAT_DEV_BUFSIZE
], cowbuf
[DM_FORMAT_DEV_BUFSIZE
];
2156 char pool
[DM_FORMAT_DEV_BUFSIZE
], metadata
[DM_FORMAT_DEV_BUFSIZE
];
2164 /* Mirrors are pretty complicated - now in separate function */
2165 r
= _mirror_emit_segment_line(dmt
, seg
, params
, paramsize
);
2169 case SEG_REPLICATOR
:
2170 if ((r
= _replicator_emit_segment_line(seg
, params
, paramsize
,
2176 case SEG_REPLICATOR_DEV
:
2177 if (!seg
->replicator
|| !_build_dev_string(originbuf
,
2182 EMIT_PARAMS(pos
, "%s %" PRIu64
, originbuf
, seg
->rdevice_index
);
2185 case SEG_SNAPSHOT_MERGE
:
2186 if (!_build_dev_string(originbuf
, sizeof(originbuf
), seg
->origin
))
2188 if (!_build_dev_string(cowbuf
, sizeof(cowbuf
), seg
->cow
))
2190 EMIT_PARAMS(pos
, "%s %s %c %d", originbuf
, cowbuf
,
2191 seg
->persistent
? 'P' : 'N', seg
->chunk_size
);
2193 case SEG_SNAPSHOT_ORIGIN
:
2194 if (!_build_dev_string(originbuf
, sizeof(originbuf
), seg
->origin
))
2196 EMIT_PARAMS(pos
, "%s", originbuf
);
2199 EMIT_PARAMS(pos
, "%u %u ", seg
->area_count
, seg
->stripe_size
);
2202 EMIT_PARAMS(pos
, "%s%s%s%s%s %s %" PRIu64
" ", seg
->cipher
,
2203 seg
->chainmode
? "-" : "", seg
->chainmode
?: "",
2204 seg
->iv
? "-" : "", seg
->iv
?: "", seg
->key
,
2205 seg
->iv_offset
!= DM_CRYPT_IV_DEFAULT
?
2206 seg
->iv_offset
: *seg_start
);
2217 target_type_is_raid
= 1;
2218 r
= _raid_emit_segment_line(dmt
, major
, minor
, seg
, seg_start
,
2225 if (!_build_dev_string(metadata
, sizeof(metadata
), seg
->metadata
))
2227 if (!_build_dev_string(pool
, sizeof(pool
), seg
->pool
))
2229 EMIT_PARAMS(pos
, "%s %s %d %" PRIu64
" %s", metadata
, pool
,
2230 seg
->data_block_size
, seg
->low_water_mark
,
2231 seg
->skip_block_zeroing
? "1 skip_block_zeroing" : "0");
2234 if (!_build_dev_string(pool
, sizeof(pool
), seg
->pool
))
2236 EMIT_PARAMS(pos
, "%s %d", pool
, seg
->device_id
);
2242 case SEG_REPLICATOR
:
2244 case SEG_SNAPSHOT_ORIGIN
:
2245 case SEG_SNAPSHOT_MERGE
:
2252 case SEG_REPLICATOR_DEV
:
2254 if ((r
= _emit_areas_line(dmt
, seg
, params
, paramsize
, &pos
)) <= 0) {
2259 log_error("No parameters supplied for %s target "
2260 "%u:%u.", dm_segtypes
[seg
->type
].target
,
2267 log_debug("Adding target to (%" PRIu32
":%" PRIu32
"): %" PRIu64
2268 " %" PRIu64
" %s %s", major
, minor
,
2269 *seg_start
, seg
->size
, target_type_is_raid
? "raid" :
2270 dm_segtypes
[seg
->type
].target
, params
);
2272 if (!dm_task_add_target(dmt
, *seg_start
, seg
->size
,
2273 target_type_is_raid
? "raid" :
2274 dm_segtypes
[seg
->type
].target
, params
))
2277 *seg_start
+= seg
->size
;
2284 static int _emit_segment(struct dm_task
*dmt
, uint32_t major
, uint32_t minor
,
2285 struct load_segment
*seg
, uint64_t *seg_start
)
2288 size_t paramsize
= 4096;
2292 if (!(params
= dm_malloc(paramsize
))) {
2293 log_error("Insufficient space for target parameters.");
2298 ret
= _emit_segment_line(dmt
, major
, minor
, seg
, seg_start
,
2308 log_debug("Insufficient space in params[%" PRIsize_t
2309 "] for target parameters.", paramsize
);
2312 } while (paramsize
< MAX_TARGET_PARAMSIZE
);
2314 log_error("Target parameter size too big. Aborting.");
2318 static int _load_node(struct dm_tree_node
*dnode
)
2321 struct dm_task
*dmt
;
2322 struct load_segment
*seg
;
2323 uint64_t seg_start
= 0, existing_table_size
;
2325 log_verbose("Loading %s table (%" PRIu32
":%" PRIu32
")", dnode
->name
,
2326 dnode
->info
.major
, dnode
->info
.minor
);
2328 if (!(dmt
= dm_task_create(DM_DEVICE_RELOAD
))) {
2329 log_error("Reload dm_task creation failed for %s", dnode
->name
);
2333 if (!dm_task_set_major(dmt
, dnode
->info
.major
) ||
2334 !dm_task_set_minor(dmt
, dnode
->info
.minor
)) {
2335 log_error("Failed to set device number for %s reload.", dnode
->name
);
2339 if (dnode
->props
.read_only
&& !dm_task_set_ro(dmt
)) {
2340 log_error("Failed to set read only flag for %s", dnode
->name
);
2344 if (!dm_task_no_open_count(dmt
))
2345 log_error("Failed to disable open_count");
2347 dm_list_iterate_items(seg
, &dnode
->props
.segs
)
2348 if (!_emit_segment(dmt
, dnode
->info
.major
, dnode
->info
.minor
,
2352 if (!dm_task_suppress_identical_reload(dmt
))
2353 log_error("Failed to suppress reload of identical tables.");
2355 if ((r
= dm_task_run(dmt
))) {
2356 r
= dm_task_get_info(dmt
, &dnode
->info
);
2357 if (r
&& !dnode
->info
.inactive_table
)
2358 log_verbose("Suppressed %s identical table reload.",
2361 existing_table_size
= dm_task_get_existing_table_size(dmt
);
2362 if ((dnode
->props
.size_changed
=
2363 (existing_table_size
== seg_start
) ? 0 : 1)) {
2364 log_debug("Table size changed from %" PRIu64
" to %"
2365 PRIu64
" for %s", existing_table_size
,
2366 seg_start
, dnode
->name
);
2368 * Kernel usually skips size validation on zero-length devices
2369 * now so no need to preload them.
2371 /* FIXME In which kernel version did this begin? */
2372 if (!existing_table_size
&& dnode
->props
.delay_resume_if_new
)
2373 dnode
->props
.size_changed
= 0;
2377 dnode
->props
.segment_count
= 0;
2380 dm_task_destroy(dmt
);
2385 int dm_tree_preload_children(struct dm_tree_node
*dnode
,
2386 const char *uuid_prefix
,
2387 size_t uuid_prefix_len
)
2390 void *handle
= NULL
;
2391 struct dm_tree_node
*child
;
2392 struct dm_info newinfo
;
2393 int update_devs_flag
= 0;
2395 /* Preload children first */
2396 while ((child
= dm_tree_next_child(&handle
, dnode
, 0))) {
2397 /* Skip existing non-device-mapper devices */
2398 if (!child
->info
.exists
&& child
->info
.major
)
2401 /* Ignore if it doesn't belong to this VG */
2402 if (child
->info
.exists
&&
2403 !_uuid_prefix_matches(child
->uuid
, uuid_prefix
, uuid_prefix_len
))
2406 if (dm_tree_node_num_children(child
, 0))
2407 if (!dm_tree_preload_children(child
, uuid_prefix
, uuid_prefix_len
))
2410 /* FIXME Cope if name exists with no uuid? */
2411 if (!child
->info
.exists
&& !_create_node(child
))
2414 if (!child
->info
.inactive_table
&&
2415 child
->props
.segment_count
&&
2419 /* Propagate device size change change */
2420 if (child
->props
.size_changed
)
2421 dnode
->props
.size_changed
= 1;
2423 /* Resume device immediately if it has parents and its size changed */
2424 if (!dm_tree_node_num_children(child
, 1) || !child
->props
.size_changed
)
2427 if (!child
->info
.inactive_table
&& !child
->info
.suspended
)
2430 if (!_resume_node(child
->name
, child
->info
.major
, child
->info
.minor
,
2431 child
->props
.read_ahead
, child
->props
.read_ahead_flags
,
2432 &newinfo
, &child
->dtree
->cookie
, child
->udev_flags
,
2433 child
->info
.suspended
)) {
2434 log_error("Unable to resume %s (%" PRIu32
2435 ":%" PRIu32
")", child
->name
, child
->info
.major
,
2441 /* Update cached info */
2442 child
->info
= newinfo
;
2444 * Prepare for immediate synchronization with udev and flush all stacked
2445 * dev node operations if requested by immediate_dev_node property. But
2446 * finish processing current level in the tree first.
2448 if (child
->props
.immediate_dev_node
)
2449 update_devs_flag
= 1;
2452 if (update_devs_flag
) {
2453 if (!dm_udev_wait(dm_tree_get_cookie(dnode
)))
2455 dm_tree_set_cookie(dnode
, 0);
2462 * Returns 1 if unsure.
2464 int dm_tree_children_use_uuid(struct dm_tree_node
*dnode
,
2465 const char *uuid_prefix
,
2466 size_t uuid_prefix_len
)
2468 void *handle
= NULL
;
2469 struct dm_tree_node
*child
= dnode
;
2472 while ((child
= dm_tree_next_child(&handle
, dnode
, 0))) {
2473 if (!(uuid
= dm_tree_node_get_uuid(child
))) {
2474 log_error("Failed to get uuid for dtree node.");
2478 if (_uuid_prefix_matches(uuid
, uuid_prefix
, uuid_prefix_len
))
2481 if (dm_tree_node_num_children(child
, 0))
2482 dm_tree_children_use_uuid(child
, uuid_prefix
, uuid_prefix_len
);
2491 static struct load_segment
*_add_segment(struct dm_tree_node
*dnode
, unsigned type
, uint64_t size
)
2493 struct load_segment
*seg
;
2495 if (!(seg
= dm_pool_zalloc(dnode
->dtree
->mem
, sizeof(*seg
)))) {
2496 log_error("dtree node segment allocation failed");
2502 seg
->area_count
= 0;
2503 dm_list_init(&seg
->areas
);
2504 seg
->stripe_size
= 0;
2505 seg
->persistent
= 0;
2506 seg
->chunk_size
= 0;
2511 dm_list_add(&dnode
->props
.segs
, &seg
->list
);
2512 dnode
->props
.segment_count
++;
2517 int dm_tree_node_add_snapshot_origin_target(struct dm_tree_node
*dnode
,
2519 const char *origin_uuid
)
2521 struct load_segment
*seg
;
2522 struct dm_tree_node
*origin_node
;
2524 if (!(seg
= _add_segment(dnode
, SEG_SNAPSHOT_ORIGIN
, size
)))
2527 if (!(origin_node
= dm_tree_find_node_by_uuid(dnode
->dtree
, origin_uuid
))) {
2528 log_error("Couldn't find snapshot origin uuid %s.", origin_uuid
);
2532 seg
->origin
= origin_node
;
2533 if (!_link_tree_nodes(dnode
, origin_node
))
2536 /* Resume snapshot origins after new snapshots */
2537 dnode
->activation_priority
= 1;
2542 static int _add_snapshot_target(struct dm_tree_node
*node
,
2544 const char *origin_uuid
,
2545 const char *cow_uuid
,
2546 const char *merge_uuid
,
2548 uint32_t chunk_size
)
2550 struct load_segment
*seg
;
2551 struct dm_tree_node
*origin_node
, *cow_node
, *merge_node
;
2554 seg_type
= !merge_uuid
? SEG_SNAPSHOT
: SEG_SNAPSHOT_MERGE
;
2556 if (!(seg
= _add_segment(node
, seg_type
, size
)))
2559 if (!(origin_node
= dm_tree_find_node_by_uuid(node
->dtree
, origin_uuid
))) {
2560 log_error("Couldn't find snapshot origin uuid %s.", origin_uuid
);
2564 seg
->origin
= origin_node
;
2565 if (!_link_tree_nodes(node
, origin_node
))
2568 if (!(cow_node
= dm_tree_find_node_by_uuid(node
->dtree
, cow_uuid
))) {
2569 log_error("Couldn't find snapshot COW device uuid %s.", cow_uuid
);
2573 seg
->cow
= cow_node
;
2574 if (!_link_tree_nodes(node
, cow_node
))
2577 seg
->persistent
= persistent
? 1 : 0;
2578 seg
->chunk_size
= chunk_size
;
2581 if (!(merge_node
= dm_tree_find_node_by_uuid(node
->dtree
, merge_uuid
))) {
2582 /* not a pure error, merging snapshot may have been deactivated */
2583 log_verbose("Couldn't find merging snapshot uuid %s.", merge_uuid
);
2585 seg
->merge
= merge_node
;
2586 /* must not link merging snapshot, would undermine activation_priority below */
2589 /* Resume snapshot-merge (acting origin) after other snapshots */
2590 node
->activation_priority
= 1;
2592 /* Resume merging snapshot after snapshot-merge */
2593 seg
->merge
->activation_priority
= 2;
2601 int dm_tree_node_add_snapshot_target(struct dm_tree_node
*node
,
2603 const char *origin_uuid
,
2604 const char *cow_uuid
,
2606 uint32_t chunk_size
)
2608 return _add_snapshot_target(node
, size
, origin_uuid
, cow_uuid
,
2609 NULL
, persistent
, chunk_size
);
2612 int dm_tree_node_add_snapshot_merge_target(struct dm_tree_node
*node
,
2614 const char *origin_uuid
,
2615 const char *cow_uuid
,
2616 const char *merge_uuid
,
2617 uint32_t chunk_size
)
2619 return _add_snapshot_target(node
, size
, origin_uuid
, cow_uuid
,
2620 merge_uuid
, 1, chunk_size
);
2623 int dm_tree_node_add_error_target(struct dm_tree_node
*node
,
2626 if (!_add_segment(node
, SEG_ERROR
, size
))
2632 int dm_tree_node_add_zero_target(struct dm_tree_node
*node
,
2635 if (!_add_segment(node
, SEG_ZERO
, size
))
2641 int dm_tree_node_add_linear_target(struct dm_tree_node
*node
,
2644 if (!_add_segment(node
, SEG_LINEAR
, size
))
2650 int dm_tree_node_add_striped_target(struct dm_tree_node
*node
,
2652 uint32_t stripe_size
)
2654 struct load_segment
*seg
;
2656 if (!(seg
= _add_segment(node
, SEG_STRIPED
, size
)))
2659 seg
->stripe_size
= stripe_size
;
2664 int dm_tree_node_add_crypt_target(struct dm_tree_node
*node
,
2667 const char *chainmode
,
2672 struct load_segment
*seg
;
2674 if (!(seg
= _add_segment(node
, SEG_CRYPT
, size
)))
2677 seg
->cipher
= cipher
;
2678 seg
->chainmode
= chainmode
;
2680 seg
->iv_offset
= iv_offset
;
2686 int dm_tree_node_add_mirror_target_log(struct dm_tree_node
*node
,
2687 uint32_t region_size
,
2689 const char *log_uuid
,
2690 unsigned area_count
,
2693 struct dm_tree_node
*log_node
= NULL
;
2694 struct load_segment
*seg
;
2696 if (!node
->props
.segment_count
) {
2697 log_error(INTERNAL_ERROR
"Attempt to add target area to missing segment.");
2701 seg
= dm_list_item(dm_list_last(&node
->props
.segs
), struct load_segment
);
2704 if (!(seg
->uuid
= dm_pool_strdup(node
->dtree
->mem
, log_uuid
))) {
2705 log_error("log uuid pool_strdup failed");
2708 if ((flags
& DM_CORELOG
))
2709 /* For pvmove: immediate resume (for size validation) isn't needed. */
2710 node
->props
.delay_resume_if_new
= 1;
2712 if (!(log_node
= dm_tree_find_node_by_uuid(node
->dtree
, log_uuid
))) {
2713 log_error("Couldn't find mirror log uuid %s.", log_uuid
);
2718 log_node
->props
.immediate_dev_node
= 1;
2720 /* The kernel validates the size of disk logs. */
2721 /* FIXME Propagate to any devices below */
2722 log_node
->props
.delay_resume_if_new
= 0;
2724 if (!_link_tree_nodes(node
, log_node
))
2729 seg
->log
= log_node
;
2730 seg
->region_size
= region_size
;
2731 seg
->clustered
= clustered
;
2732 seg
->mirror_area_count
= area_count
;
2738 int dm_tree_node_add_mirror_target(struct dm_tree_node
*node
,
2741 if (!_add_segment(node
, SEG_MIRRORED
, size
))
2747 int dm_tree_node_add_raid_target(struct dm_tree_node
*node
,
2749 const char *raid_type
,
2750 uint32_t region_size
,
2751 uint32_t stripe_size
,
2756 struct load_segment
*seg
= NULL
;
2758 for (i
= 0; dm_segtypes
[i
].target
&& !seg
; i
++)
2759 if (!strcmp(raid_type
, dm_segtypes
[i
].target
))
2760 if (!(seg
= _add_segment(node
,
2761 dm_segtypes
[i
].type
, size
)))
2767 seg
->region_size
= region_size
;
2768 seg
->stripe_size
= stripe_size
;
2769 seg
->area_count
= 0;
2770 seg
->rebuilds
= rebuilds
;
2775 int dm_tree_node_add_replicator_target(struct dm_tree_node
*node
,
2777 const char *rlog_uuid
,
2778 const char *rlog_type
,
2779 unsigned rsite_index
,
2780 dm_replicator_mode_t mode
,
2781 uint32_t async_timeout
,
2782 uint64_t fall_behind_data
,
2783 uint32_t fall_behind_ios
)
2785 struct load_segment
*rseg
;
2786 struct replicator_site
*rsite
;
2788 /* Local site0 - adds replicator segment and links rlog device */
2789 if (rsite_index
== REPLICATOR_LOCAL_SITE
) {
2790 if (node
->props
.segment_count
) {
2791 log_error(INTERNAL_ERROR
"Attempt to add replicator segment to already used node.");
2795 if (!(rseg
= _add_segment(node
, SEG_REPLICATOR
, size
)))
2798 if (!(rseg
->log
= dm_tree_find_node_by_uuid(node
->dtree
, rlog_uuid
))) {
2799 log_error("Missing replicator log uuid %s.", rlog_uuid
);
2803 if (!_link_tree_nodes(node
, rseg
->log
))
2806 if (strcmp(rlog_type
, "ringbuffer") != 0) {
2807 log_error("Unsupported replicator log type %s.", rlog_type
);
2811 if (!(rseg
->rlog_type
= dm_pool_strdup(node
->dtree
->mem
, rlog_type
)))
2814 dm_list_init(&rseg
->rsites
);
2815 rseg
->rdevice_count
= 0;
2816 node
->activation_priority
= 1;
2819 /* Add site to segment */
2820 if (mode
== DM_REPLICATOR_SYNC
2821 && (async_timeout
|| fall_behind_ios
|| fall_behind_data
)) {
2822 log_error("Async parameters passed for synchronnous replicator.");
2826 if (node
->props
.segment_count
!= 1) {
2827 log_error(INTERNAL_ERROR
"Attempt to add remote site area before setting replicator log.");
2831 rseg
= dm_list_item(dm_list_last(&node
->props
.segs
), struct load_segment
);
2832 if (rseg
->type
!= SEG_REPLICATOR
) {
2833 log_error(INTERNAL_ERROR
"Attempt to use non replicator segment %s.",
2834 dm_segtypes
[rseg
->type
].target
);
2838 if (!(rsite
= dm_pool_zalloc(node
->dtree
->mem
, sizeof(*rsite
)))) {
2839 log_error("Failed to allocate remote site segment.");
2843 dm_list_add(&rseg
->rsites
, &rsite
->list
);
2844 rseg
->rsite_count
++;
2847 rsite
->async_timeout
= async_timeout
;
2848 rsite
->fall_behind_data
= fall_behind_data
;
2849 rsite
->fall_behind_ios
= fall_behind_ios
;
2850 rsite
->rsite_index
= rsite_index
;
2855 /* Appends device node to Replicator */
2856 int dm_tree_node_add_replicator_dev_target(struct dm_tree_node
*node
,
2858 const char *replicator_uuid
,
2859 uint64_t rdevice_index
,
2860 const char *rdev_uuid
,
2861 unsigned rsite_index
,
2862 const char *slog_uuid
,
2863 uint32_t slog_flags
,
2864 uint32_t slog_region_size
)
2866 struct seg_area
*area
;
2867 struct load_segment
*rseg
;
2868 struct load_segment
*rep_seg
;
2870 if (rsite_index
== REPLICATOR_LOCAL_SITE
) {
2871 /* Site index for local target */
2872 if (!(rseg
= _add_segment(node
, SEG_REPLICATOR_DEV
, size
)))
2875 if (!(rseg
->replicator
= dm_tree_find_node_by_uuid(node
->dtree
, replicator_uuid
))) {
2876 log_error("Missing replicator uuid %s.", replicator_uuid
);
2880 /* Local slink0 for replicator must be always initialized first */
2881 if (rseg
->replicator
->props
.segment_count
!= 1) {
2882 log_error(INTERNAL_ERROR
"Attempt to use non replicator segment.");
2886 rep_seg
= dm_list_item(dm_list_last(&rseg
->replicator
->props
.segs
), struct load_segment
);
2887 if (rep_seg
->type
!= SEG_REPLICATOR
) {
2888 log_error(INTERNAL_ERROR
"Attempt to use non replicator segment %s.",
2889 dm_segtypes
[rep_seg
->type
].target
);
2892 rep_seg
->rdevice_count
++;
2894 if (!_link_tree_nodes(node
, rseg
->replicator
))
2897 rseg
->rdevice_index
= rdevice_index
;
2899 /* Local slink0 for replicator must be always initialized first */
2900 if (node
->props
.segment_count
!= 1) {
2901 log_error(INTERNAL_ERROR
"Attempt to use non replicator-dev segment.");
2905 rseg
= dm_list_item(dm_list_last(&node
->props
.segs
), struct load_segment
);
2906 if (rseg
->type
!= SEG_REPLICATOR_DEV
) {
2907 log_error(INTERNAL_ERROR
"Attempt to use non replicator-dev segment %s.",
2908 dm_segtypes
[rseg
->type
].target
);
2913 if (!(slog_flags
& DM_CORELOG
) && !slog_uuid
) {
2914 log_error("Unspecified sync log uuid.");
2918 if (!dm_tree_node_add_target_area(node
, NULL
, rdev_uuid
, 0))
2921 area
= dm_list_item(dm_list_last(&rseg
->areas
), struct seg_area
);
2923 if (!(slog_flags
& DM_CORELOG
)) {
2924 if (!(area
->slog
= dm_tree_find_node_by_uuid(node
->dtree
, slog_uuid
))) {
2925 log_error("Couldn't find sync log uuid %s.", slog_uuid
);
2929 if (!_link_tree_nodes(node
, area
->slog
))
2933 area
->flags
= slog_flags
;
2934 area
->region_size
= slog_region_size
;
2935 area
->rsite_index
= rsite_index
;
2940 static int _thin_validate_device_id(uint32_t device_id
)
2942 if (device_id
> DM_THIN_MAX_DEVICE_ID
) {
2943 log_error("Device id %u is higher then %u.",
2944 device_id
, DM_THIN_MAX_DEVICE_ID
);
2951 int dm_tree_node_add_thin_pool_target(struct dm_tree_node
*node
,
2953 uint64_t transaction_id
,
2954 const char *metadata_uuid
,
2955 const char *pool_uuid
,
2956 uint32_t data_block_size
,
2957 uint64_t low_water_mark
,
2958 unsigned skip_block_zeroing
)
2960 struct load_segment
*seg
;
2962 if (data_block_size
< DM_THIN_MIN_DATA_BLOCK_SIZE
) {
2963 log_error("Data block size %u is lower then %u sectors.",
2964 data_block_size
, DM_THIN_MIN_DATA_BLOCK_SIZE
);
2968 if (data_block_size
> DM_THIN_MAX_DATA_BLOCK_SIZE
) {
2969 log_error("Data block size %u is higher then %u sectors.",
2970 data_block_size
, DM_THIN_MAX_DATA_BLOCK_SIZE
);
2974 if (!(seg
= _add_segment(node
, SEG_THIN_POOL
, size
)))
2977 if (!(seg
->metadata
= dm_tree_find_node_by_uuid(node
->dtree
, metadata_uuid
))) {
2978 log_error("Missing metadata uuid %s.", metadata_uuid
);
2982 if (!_link_tree_nodes(node
, seg
->metadata
))
2985 if (!(seg
->pool
= dm_tree_find_node_by_uuid(node
->dtree
, pool_uuid
))) {
2986 log_error("Missing pool uuid %s.", pool_uuid
);
2990 if (!_link_tree_nodes(node
, seg
->pool
))
2993 node
->props
.send_messages
= 1;
2994 seg
->transaction_id
= transaction_id
;
2995 seg
->low_water_mark
= low_water_mark
;
2996 seg
->data_block_size
= data_block_size
;
2997 seg
->skip_block_zeroing
= skip_block_zeroing
;
2998 dm_list_init(&seg
->thin_messages
);
3003 int dm_tree_node_add_thin_pool_message(struct dm_tree_node
*node
,
3004 dm_thin_message_t type
,
3005 uint64_t id1
, uint64_t id2
)
3007 struct load_segment
*seg
;
3008 struct thin_message
*tm
;
3010 if (node
->props
.segment_count
!= 1) {
3011 log_error("Thin pool node must have only one segment.");
3015 seg
= dm_list_item(dm_list_last(&node
->props
.segs
), struct load_segment
);
3016 if (seg
->type
!= SEG_THIN_POOL
) {
3017 log_error("Thin pool node has segment type %s.",
3018 dm_segtypes
[seg
->type
].target
);
3022 if (!(tm
= dm_pool_zalloc(node
->dtree
->mem
, sizeof (*tm
)))) {
3023 log_error("Failed to allocate thin message.");
3028 case DM_THIN_MESSAGE_CREATE_SNAP
:
3029 /* If the thin origin is active, it must be suspend first! */
3031 log_error("Cannot use same device id for origin and its snapshot.");
3034 if (!_thin_validate_device_id(id1
) ||
3035 !_thin_validate_device_id(id2
))
3037 tm
->message
.u
.m_create_snap
.device_id
= id1
;
3038 tm
->message
.u
.m_create_snap
.origin_id
= id2
;
3040 case DM_THIN_MESSAGE_CREATE_THIN
:
3041 if (!_thin_validate_device_id(id1
))
3043 tm
->message
.u
.m_create_thin
.device_id
= id1
;
3044 tm
->expected_errno
= EEXIST
;
3046 case DM_THIN_MESSAGE_DELETE
:
3047 if (!_thin_validate_device_id(id1
))
3049 tm
->message
.u
.m_delete
.device_id
= id1
;
3050 tm
->expected_errno
= ENODATA
;
3052 case DM_THIN_MESSAGE_TRIM
:
3053 if (!_thin_validate_device_id(id1
))
3055 tm
->message
.u
.m_trim
.device_id
= id1
;
3056 tm
->message
.u
.m_trim
.new_size
= id2
;
3058 case DM_THIN_MESSAGE_SET_TRANSACTION_ID
:
3059 if ((id1
+ 1) != id2
) {
3060 log_error("New transaction id must be sequential.");
3061 return 0; /* FIXME: Maybe too strict here? */
3063 if (id2
!= seg
->transaction_id
) {
3064 log_error("Current transaction id is different from thin pool.");
3065 return 0; /* FIXME: Maybe too strict here? */
3067 tm
->message
.u
.m_set_transaction_id
.current_id
= id1
;
3068 tm
->message
.u
.m_set_transaction_id
.new_id
= id2
;
3071 log_error("Unsupported message type %d.", (int) type
);
3075 tm
->message
.type
= type
;
3076 dm_list_add(&seg
->thin_messages
, &tm
->list
);
3081 int dm_tree_node_add_thin_target(struct dm_tree_node
*node
,
3083 const char *pool_uuid
,
3086 struct dm_tree_node
*pool
;
3087 struct load_segment
*seg
;
3089 if (!(pool
= dm_tree_find_node_by_uuid(node
->dtree
, pool_uuid
))) {
3090 log_error("Missing thin pool uuid %s.", pool_uuid
);
3094 if (!_link_tree_nodes(node
, pool
))
3097 if (!_thin_validate_device_id(device_id
))
3100 if (!(seg
= _add_segment(node
, SEG_THIN
, size
)))
3104 seg
->device_id
= device_id
;
3110 int dm_get_status_thin_pool(struct dm_pool
*mem
, const char *params
,
3111 struct dm_status_thin_pool
**status
)
3113 struct dm_status_thin_pool
*s
;
3115 if (!(s
= dm_pool_zalloc(mem
, sizeof(struct dm_status_thin_pool
)))) {
3116 log_error("Failed to allocate thin_pool status structure.");
3120 /* FIXME: add support for held metadata root */
3121 if (sscanf(params
, "%" PRIu64
" %" PRIu64
"/%" PRIu64
" %" PRIu64
"/%" PRIu64
,
3123 &s
->used_metadata_blocks
,
3124 &s
->total_metadata_blocks
,
3125 &s
->used_data_blocks
,
3126 &s
->total_data_blocks
) != 5) {
3127 log_error("Failed to parse thin pool params: %s.", params
);
3136 int dm_get_status_thin(struct dm_pool
*mem
, const char *params
,
3137 struct dm_status_thin
**status
)
3139 struct dm_status_thin
*s
;
3141 if (!(s
= dm_pool_zalloc(mem
, sizeof(struct dm_status_thin
)))) {
3142 log_error("Failed to allocate thin status structure.");
3146 if (strchr(params
, '-')) {
3147 s
->mapped_sectors
= 0;
3148 s
->highest_mapped_sector
= 0;
3149 } else if (sscanf(params
, "%" PRIu64
" %" PRIu64
,
3151 &s
->highest_mapped_sector
) != 2) {
3152 log_error("Failed to parse thin params: %s.", params
);
3161 static int _add_area(struct dm_tree_node
*node
, struct load_segment
*seg
, struct dm_tree_node
*dev_node
, uint64_t offset
)
3163 struct seg_area
*area
;
3165 if (!(area
= dm_pool_zalloc(node
->dtree
->mem
, sizeof (*area
)))) {
3166 log_error("Failed to allocate target segment area.");
3170 area
->dev_node
= dev_node
;
3171 area
->offset
= offset
;
3173 dm_list_add(&seg
->areas
, &area
->list
);
3179 int dm_tree_node_add_target_area(struct dm_tree_node
*node
,
3180 const char *dev_name
,
3184 struct load_segment
*seg
;
3186 struct dm_tree_node
*dev_node
;
3188 if ((!dev_name
|| !*dev_name
) && (!uuid
|| !*uuid
)) {
3189 log_error("dm_tree_node_add_target_area called without device");
3194 if (!(dev_node
= dm_tree_find_node_by_uuid(node
->dtree
, uuid
))) {
3195 log_error("Couldn't find area uuid %s.", uuid
);
3198 if (!_link_tree_nodes(node
, dev_node
))
3201 if (stat(dev_name
, &info
) < 0) {
3202 log_error("Device %s not found.", dev_name
);
3206 if (!S_ISBLK(info
.st_mode
)) {
3207 log_error("Device %s is not a block device.", dev_name
);
3211 /* FIXME Check correct macro use */
3212 if (!(dev_node
= _add_dev(node
->dtree
, node
, MAJOR(info
.st_rdev
),
3213 MINOR(info
.st_rdev
), 0)))
3217 if (!node
->props
.segment_count
) {
3218 log_error(INTERNAL_ERROR
"Attempt to add target area to missing segment.");
3222 seg
= dm_list_item(dm_list_last(&node
->props
.segs
), struct load_segment
);
3224 if (!_add_area(node
, seg
, dev_node
, offset
))
3230 int dm_tree_node_add_null_area(struct dm_tree_node
*node
, uint64_t offset
)
3232 struct load_segment
*seg
;
3234 seg
= dm_list_item(dm_list_last(&node
->props
.segs
), struct load_segment
);
3236 switch (seg
->type
) {
3248 log_error("dm_tree_node_add_null_area() called on an unsupported segment type");
3252 if (!_add_area(node
, seg
, NULL
, offset
))