]> sourceware.org Git - lvm2.git/blame - libdm/libdm-deptree.c
Add the ability to split an image from the mirror and track changes.
[lvm2.git] / libdm / libdm-deptree.c
CommitLineData
3d0480ed 1/*
4ef2bf27 2 * Copyright (C) 2005-2010 Red Hat, Inc. All rights reserved.
3d0480ed
AK
3 *
4 * This file is part of the device-mapper userspace tools.
5 *
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.
9 *
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
13 */
14
3e5b6ed2 15#include "dmlib.h"
3d0480ed
AK
16#include "libdm-targets.h"
17#include "libdm-common.h"
3d0480ed 18#include "kdev_t.h"
0782ad50 19#include "dm-ioctl.h"
3d0480ed
AK
20
21#include <stdarg.h>
22#include <sys/param.h>
8f26e18c 23#include <sys/utsname.h>
3d0480ed 24
165e4a11
AK
25#define MAX_TARGET_PARAMSIZE 500000
26
87f98002
AK
27/* FIXME Fix interface so this is used only by LVM */
28#define UUID_PREFIX "LVM-"
29
b262f3e1
ZK
30#define REPLICATOR_LOCAL_SITE 0
31
165e4a11
AK
32/* Supported segment types */
33enum {
12ca060e
MB
34 SEG_CRYPT,
35 SEG_ERROR,
165e4a11
AK
36 SEG_LINEAR,
37 SEG_MIRRORED,
b262f3e1
ZK
38 SEG_REPLICATOR,
39 SEG_REPLICATOR_DEV,
165e4a11
AK
40 SEG_SNAPSHOT,
41 SEG_SNAPSHOT_ORIGIN,
aa6f4e51 42 SEG_SNAPSHOT_MERGE,
165e4a11
AK
43 SEG_STRIPED,
44 SEG_ZERO,
cac52ca4
JEB
45 SEG_RAID1,
46 SEG_RAID4,
47 SEG_RAID5_LA,
48 SEG_RAID5_RA,
49 SEG_RAID5_LS,
50 SEG_RAID5_RS,
51 SEG_RAID6_ZR,
52 SEG_RAID6_NR,
53 SEG_RAID6_NC,
54 SEG_LAST,
165e4a11 55};
b4f1578f 56
165e4a11
AK
57/* FIXME Add crypt and multipath support */
58
59struct {
60 unsigned type;
61 const char *target;
62} dm_segtypes[] = {
12ca060e 63 { SEG_CRYPT, "crypt" },
165e4a11
AK
64 { SEG_ERROR, "error" },
65 { SEG_LINEAR, "linear" },
66 { SEG_MIRRORED, "mirror" },
b262f3e1
ZK
67 { SEG_REPLICATOR, "replicator" },
68 { SEG_REPLICATOR_DEV, "replicator-dev" },
165e4a11
AK
69 { SEG_SNAPSHOT, "snapshot" },
70 { SEG_SNAPSHOT_ORIGIN, "snapshot-origin" },
aa6f4e51 71 { SEG_SNAPSHOT_MERGE, "snapshot-merge" },
165e4a11
AK
72 { SEG_STRIPED, "striped" },
73 { SEG_ZERO, "zero"},
cac52ca4
JEB
74 { SEG_RAID1, "raid1"},
75 { SEG_RAID4, "raid4"},
76 { SEG_RAID5_LA, "raid5_la"},
77 { SEG_RAID5_RA, "raid5_ra"},
78 { SEG_RAID5_LS, "raid5_ls"},
79 { SEG_RAID5_RS, "raid5_rs"},
80 { SEG_RAID6_ZR, "raid6_zr"},
81 { SEG_RAID6_NR, "raid6_nr"},
82 { SEG_RAID6_NC, "raid6_nc"},
83 { SEG_RAID5_LS, "raid5"}, /* same as "raid5_ls" (default for MD also) */
84 { SEG_RAID6_ZR, "raid6"}, /* same as "raid6_zr" */
85 { SEG_LAST, NULL },
165e4a11
AK
86};
87
88/* Some segment types have a list of areas of other devices attached */
89struct seg_area {
2c44337b 90 struct dm_list list;
165e4a11 91
b4f1578f 92 struct dm_tree_node *dev_node;
165e4a11
AK
93
94 uint64_t offset;
b262f3e1
ZK
95
96 unsigned rsite_index; /* Replicator site index */
97 struct dm_tree_node *slog; /* Replicator sync log node */
98 uint64_t region_size; /* Replicator sync log size */
99 uint32_t flags; /* Replicator sync log flags */
100};
101
102/* Replicator-log has a list of sites */
103/* FIXME: maybe move to seg_area too? */
104struct replicator_site {
105 struct dm_list list;
106
107 unsigned rsite_index;
108 dm_replicator_mode_t mode;
109 uint32_t async_timeout;
110 uint32_t fall_behind_ios;
111 uint64_t fall_behind_data;
165e4a11
AK
112};
113
114/* Per-segment properties */
115struct load_segment {
2c44337b 116 struct dm_list list;
165e4a11
AK
117
118 unsigned type;
119
120 uint64_t size;
121
b262f3e1
ZK
122 unsigned area_count; /* Linear + Striped + Mirrored + Crypt + Replicator */
123 struct dm_list areas; /* Linear + Striped + Mirrored + Crypt + Replicator */
165e4a11 124
cac52ca4 125 uint32_t stripe_size; /* Striped + raid */
165e4a11
AK
126
127 int persistent; /* Snapshot */
128 uint32_t chunk_size; /* Snapshot */
b4f1578f
AK
129 struct dm_tree_node *cow; /* Snapshot */
130 struct dm_tree_node *origin; /* Snapshot + Snapshot origin */
aa6f4e51 131 struct dm_tree_node *merge; /* Snapshot */
165e4a11 132
b262f3e1 133 struct dm_tree_node *log; /* Mirror + Replicator */
cac52ca4 134 uint32_t region_size; /* Mirror + raid */
165e4a11
AK
135 unsigned clustered; /* Mirror */
136 unsigned mirror_area_count; /* Mirror */
dbcb64b8 137 uint32_t flags; /* Mirror log */
67b25ed4 138 char *uuid; /* Clustered mirror log */
12ca060e
MB
139
140 const char *cipher; /* Crypt */
141 const char *chainmode; /* Crypt */
142 const char *iv; /* Crypt */
143 uint64_t iv_offset; /* Crypt */
144 const char *key; /* Crypt */
b262f3e1
ZK
145
146 const char *rlog_type; /* Replicator */
147 struct dm_list rsites; /* Replicator */
148 unsigned rsite_count; /* Replicator */
149 unsigned rdevice_count; /* Replicator */
150 struct dm_tree_node *replicator;/* Replicator-dev */
151 uint64_t rdevice_index; /* Replicator-dev */
165e4a11
AK
152};
153
154/* Per-device properties */
155struct load_properties {
156 int read_only;
157 uint32_t major;
158 uint32_t minor;
159
52b84409
AK
160 uint32_t read_ahead;
161 uint32_t read_ahead_flags;
162
165e4a11 163 unsigned segment_count;
bb875bb9 164 unsigned size_changed;
2c44337b 165 struct dm_list segs;
165e4a11
AK
166
167 const char *new_name;
566515c0
PR
168
169 /* If immediate_dev_node is set to 1, try to create the dev node
170 * as soon as possible (e.g. in preload stage even during traversal
171 * and processing of dm tree). This will also flush all stacked dev
172 * node operations, synchronizing with udev.
173 */
df390f17
AK
174 unsigned immediate_dev_node;
175
176 /*
177 * If the device size changed from zero and this is set,
178 * don't resume the device immediately, even if the device
179 * has parents. This works provided the parents do not
180 * validate the device size and is required by pvmove to
181 * avoid starting the mirror resync operation too early.
182 */
183 unsigned delay_resume_if_new;
165e4a11
AK
184};
185
186/* Two of these used to join two nodes with uses and used_by. */
b4f1578f 187struct dm_tree_link {
2c44337b 188 struct dm_list list;
b4f1578f 189 struct dm_tree_node *node;
165e4a11
AK
190};
191
b4f1578f
AK
192struct dm_tree_node {
193 struct dm_tree *dtree;
3d0480ed
AK
194
195 const char *name;
196 const char *uuid;
197 struct dm_info info;
198
2c44337b
AK
199 struct dm_list uses; /* Nodes this node uses */
200 struct dm_list used_by; /* Nodes that use this node */
165e4a11 201
56c28292
AK
202 int activation_priority; /* 0 gets activated first */
203
f16aea9e
PR
204 uint16_t udev_flags; /* Udev control flags */
205
165e4a11
AK
206 void *context; /* External supplied context */
207
208 struct load_properties props; /* For creation/table (re)load */
76d1aec8
ZK
209
210 /*
211 * If presuspend of child node is needed
212 * Note: only direct child is allowed
213 */
214 struct dm_tree_node *presuspend_node;
3d0480ed
AK
215};
216
b4f1578f 217struct dm_tree {
a3f6b2ce
AK
218 struct dm_pool *mem;
219 struct dm_hash_table *devs;
165e4a11 220 struct dm_hash_table *uuids;
b4f1578f 221 struct dm_tree_node root;
c55b1410 222 int skip_lockfs; /* 1 skips lockfs (for non-snapshots) */
b9ffd32c 223 int no_flush; /* 1 sets noflush (mirrors/multipath) */
bd90c6b2 224 uint32_t cookie;
3d0480ed
AK
225};
226
b4f1578f 227struct dm_tree *dm_tree_create(void)
3d0480ed 228{
b4f1578f 229 struct dm_tree *dtree;
3d0480ed 230
ac0252ca 231 if (!(dtree = dm_zalloc(sizeof(*dtree)))) {
b4f1578f 232 log_error("dm_tree_create malloc failed");
3d0480ed
AK
233 return NULL;
234 }
235
b4f1578f 236 dtree->root.dtree = dtree;
2c44337b
AK
237 dm_list_init(&dtree->root.uses);
238 dm_list_init(&dtree->root.used_by);
c55b1410 239 dtree->skip_lockfs = 0;
b9ffd32c 240 dtree->no_flush = 0;
3d0480ed 241
b4f1578f
AK
242 if (!(dtree->mem = dm_pool_create("dtree", 1024))) {
243 log_error("dtree pool creation failed");
244 dm_free(dtree);
3d0480ed
AK
245 return NULL;
246 }
247
b4f1578f
AK
248 if (!(dtree->devs = dm_hash_create(8))) {
249 log_error("dtree hash creation failed");
250 dm_pool_destroy(dtree->mem);
251 dm_free(dtree);
3d0480ed
AK
252 return NULL;
253 }
254
b4f1578f
AK
255 if (!(dtree->uuids = dm_hash_create(32))) {
256 log_error("dtree uuid hash creation failed");
257 dm_hash_destroy(dtree->devs);
258 dm_pool_destroy(dtree->mem);
259 dm_free(dtree);
165e4a11
AK
260 return NULL;
261 }
262
b4f1578f 263 return dtree;
3d0480ed
AK
264}
265
b4f1578f 266void dm_tree_free(struct dm_tree *dtree)
3d0480ed 267{
b4f1578f 268 if (!dtree)
3d0480ed
AK
269 return;
270
b4f1578f
AK
271 dm_hash_destroy(dtree->uuids);
272 dm_hash_destroy(dtree->devs);
273 dm_pool_destroy(dtree->mem);
274 dm_free(dtree);
3d0480ed
AK
275}
276
04bde319
ZK
277static int _nodes_are_linked(const struct dm_tree_node *parent,
278 const struct dm_tree_node *child)
3d0480ed 279{
b4f1578f 280 struct dm_tree_link *dlink;
3d0480ed 281
2c44337b 282 dm_list_iterate_items(dlink, &parent->uses)
3d0480ed
AK
283 if (dlink->node == child)
284 return 1;
3d0480ed
AK
285
286 return 0;
287}
288
2c44337b 289static int _link(struct dm_list *list, struct dm_tree_node *node)
3d0480ed 290{
b4f1578f 291 struct dm_tree_link *dlink;
3d0480ed 292
b4f1578f
AK
293 if (!(dlink = dm_pool_alloc(node->dtree->mem, sizeof(*dlink)))) {
294 log_error("dtree link allocation failed");
3d0480ed
AK
295 return 0;
296 }
297
298 dlink->node = node;
2c44337b 299 dm_list_add(list, &dlink->list);
3d0480ed
AK
300
301 return 1;
302}
303
b4f1578f
AK
304static int _link_nodes(struct dm_tree_node *parent,
305 struct dm_tree_node *child)
3d0480ed
AK
306{
307 if (_nodes_are_linked(parent, child))
308 return 1;
309
310 if (!_link(&parent->uses, child))
311 return 0;
312
313 if (!_link(&child->used_by, parent))
314 return 0;
315
316 return 1;
317}
318
2c44337b 319static void _unlink(struct dm_list *list, struct dm_tree_node *node)
3d0480ed 320{
b4f1578f 321 struct dm_tree_link *dlink;
3d0480ed 322
2c44337b 323 dm_list_iterate_items(dlink, list)
3d0480ed 324 if (dlink->node == node) {
2c44337b 325 dm_list_del(&dlink->list);
3d0480ed
AK
326 break;
327 }
3d0480ed
AK
328}
329
b4f1578f
AK
330static void _unlink_nodes(struct dm_tree_node *parent,
331 struct dm_tree_node *child)
3d0480ed
AK
332{
333 if (!_nodes_are_linked(parent, child))
334 return;
335
336 _unlink(&parent->uses, child);
337 _unlink(&child->used_by, parent);
338}
339
b4f1578f 340static int _add_to_toplevel(struct dm_tree_node *node)
165e4a11 341{
b4f1578f 342 return _link_nodes(&node->dtree->root, node);
165e4a11
AK
343}
344
b4f1578f 345static void _remove_from_toplevel(struct dm_tree_node *node)
3d0480ed 346{
b1ebf028 347 _unlink_nodes(&node->dtree->root, node);
3d0480ed
AK
348}
349
b4f1578f 350static int _add_to_bottomlevel(struct dm_tree_node *node)
3d0480ed 351{
b4f1578f 352 return _link_nodes(node, &node->dtree->root);
3d0480ed
AK
353}
354
b4f1578f 355static void _remove_from_bottomlevel(struct dm_tree_node *node)
165e4a11 356{
b1ebf028 357 _unlink_nodes(node, &node->dtree->root);
165e4a11
AK
358}
359
b4f1578f 360static int _link_tree_nodes(struct dm_tree_node *parent, struct dm_tree_node *child)
165e4a11
AK
361{
362 /* Don't link to root node if child already has a parent */
f77736ca 363 if (parent == &parent->dtree->root) {
b4f1578f 364 if (dm_tree_node_num_children(child, 1))
165e4a11
AK
365 return 1;
366 } else
367 _remove_from_toplevel(child);
368
f77736ca 369 if (child == &child->dtree->root) {
b4f1578f 370 if (dm_tree_node_num_children(parent, 0))
165e4a11
AK
371 return 1;
372 } else
373 _remove_from_bottomlevel(parent);
374
375 return _link_nodes(parent, child);
376}
377
b4f1578f 378static struct dm_tree_node *_create_dm_tree_node(struct dm_tree *dtree,
3d0480ed
AK
379 const char *name,
380 const char *uuid,
165e4a11 381 struct dm_info *info,
f16aea9e
PR
382 void *context,
383 uint16_t udev_flags)
3d0480ed 384{
b4f1578f 385 struct dm_tree_node *node;
3d0480ed
AK
386 uint64_t dev;
387
b4f1578f
AK
388 if (!(node = dm_pool_zalloc(dtree->mem, sizeof(*node)))) {
389 log_error("_create_dm_tree_node alloc failed");
3d0480ed
AK
390 return NULL;
391 }
392
b4f1578f 393 node->dtree = dtree;
3d0480ed
AK
394
395 node->name = name;
396 node->uuid = uuid;
397 node->info = *info;
165e4a11 398 node->context = context;
f16aea9e 399 node->udev_flags = udev_flags;
56c28292 400 node->activation_priority = 0;
3d0480ed 401
2c44337b
AK
402 dm_list_init(&node->uses);
403 dm_list_init(&node->used_by);
404 dm_list_init(&node->props.segs);
3d0480ed
AK
405
406 dev = MKDEV(info->major, info->minor);
407
b4f1578f 408 if (!dm_hash_insert_binary(dtree->devs, (const char *) &dev,
3d0480ed 409 sizeof(dev), node)) {
b4f1578f
AK
410 log_error("dtree node hash insertion failed");
411 dm_pool_free(dtree->mem, node);
3d0480ed
AK
412 return NULL;
413 }
414
165e4a11 415 if (uuid && *uuid &&
b4f1578f
AK
416 !dm_hash_insert(dtree->uuids, uuid, node)) {
417 log_error("dtree uuid hash insertion failed");
418 dm_hash_remove_binary(dtree->devs, (const char *) &dev,
165e4a11 419 sizeof(dev));
b4f1578f 420 dm_pool_free(dtree->mem, node);
165e4a11
AK
421 return NULL;
422 }
423
3d0480ed
AK
424 return node;
425}
426
b4f1578f 427static struct dm_tree_node *_find_dm_tree_node(struct dm_tree *dtree,
3d0480ed
AK
428 uint32_t major, uint32_t minor)
429{
430 uint64_t dev = MKDEV(major, minor);
431
b4f1578f 432 return dm_hash_lookup_binary(dtree->devs, (const char *) &dev,
3d0480ed
AK
433 sizeof(dev));
434}
435
b4f1578f 436static struct dm_tree_node *_find_dm_tree_node_by_uuid(struct dm_tree *dtree,
165e4a11
AK
437 const char *uuid)
438{
87f98002
AK
439 struct dm_tree_node *node;
440
441 if ((node = dm_hash_lookup(dtree->uuids, uuid)))
442 return node;
443
444 if (strncmp(uuid, UUID_PREFIX, sizeof(UUID_PREFIX) - 1))
445 return NULL;
446
447 return dm_hash_lookup(dtree->uuids, uuid + sizeof(UUID_PREFIX) - 1);
165e4a11
AK
448}
449
a3f6b2ce 450static int _deps(struct dm_task **dmt, struct dm_pool *mem, uint32_t major, uint32_t minor,
3d0480ed
AK
451 const char **name, const char **uuid,
452 struct dm_info *info, struct dm_deps **deps)
453{
454 memset(info, 0, sizeof(*info));
455
456 if (!dm_is_dm_major(major)) {
457 *name = "";
458 *uuid = "";
459 *deps = NULL;
460 info->major = major;
461 info->minor = minor;
462 info->exists = 0;
165e4a11
AK
463 info->live_table = 0;
464 info->inactive_table = 0;
465 info->read_only = 0;
3d0480ed
AK
466 return 1;
467 }
468
469 if (!(*dmt = dm_task_create(DM_DEVICE_DEPS))) {
470 log_error("deps dm_task creation failed");
471 return 0;
472 }
473
b4f1578f
AK
474 if (!dm_task_set_major(*dmt, major)) {
475 log_error("_deps: failed to set major for (%" PRIu32 ":%" PRIu32 ")",
476 major, minor);
3d0480ed 477 goto failed;
b4f1578f 478 }
3d0480ed 479
b4f1578f
AK
480 if (!dm_task_set_minor(*dmt, minor)) {
481 log_error("_deps: failed to set minor for (%" PRIu32 ":%" PRIu32 ")",
482 major, minor);
3d0480ed 483 goto failed;
b4f1578f 484 }
3d0480ed 485
b4f1578f
AK
486 if (!dm_task_run(*dmt)) {
487 log_error("_deps: task run failed for (%" PRIu32 ":%" PRIu32 ")",
488 major, minor);
3d0480ed 489 goto failed;
b4f1578f 490 }
3d0480ed 491
b4f1578f
AK
492 if (!dm_task_get_info(*dmt, info)) {
493 log_error("_deps: failed to get info for (%" PRIu32 ":%" PRIu32 ")",
494 major, minor);
3d0480ed 495 goto failed;
b4f1578f 496 }
3d0480ed
AK
497
498 if (!info->exists) {
499 *name = "";
500 *uuid = "";
501 *deps = NULL;
502 } else {
503 if (info->major != major) {
b4f1578f 504 log_error("Inconsistent dtree major number: %u != %u",
3d0480ed
AK
505 major, info->major);
506 goto failed;
507 }
508 if (info->minor != minor) {
b4f1578f 509 log_error("Inconsistent dtree minor number: %u != %u",
3d0480ed
AK
510 minor, info->minor);
511 goto failed;
512 }
a3f6b2ce 513 if (!(*name = dm_pool_strdup(mem, dm_task_get_name(*dmt)))) {
3d0480ed
AK
514 log_error("name pool_strdup failed");
515 goto failed;
516 }
a3f6b2ce 517 if (!(*uuid = dm_pool_strdup(mem, dm_task_get_uuid(*dmt)))) {
3d0480ed
AK
518 log_error("uuid pool_strdup failed");
519 goto failed;
520 }
521 *deps = dm_task_get_deps(*dmt);
522 }
523
524 return 1;
525
526failed:
527 dm_task_destroy(*dmt);
528 return 0;
529}
530
b4f1578f
AK
531static struct dm_tree_node *_add_dev(struct dm_tree *dtree,
532 struct dm_tree_node *parent,
cda69e17
PR
533 uint32_t major, uint32_t minor,
534 uint16_t udev_flags)
3d0480ed
AK
535{
536 struct dm_task *dmt = NULL;
537 struct dm_info info;
538 struct dm_deps *deps = NULL;
539 const char *name = NULL;
540 const char *uuid = NULL;
b4f1578f 541 struct dm_tree_node *node = NULL;
3d0480ed 542 uint32_t i;
3d0480ed
AK
543 int new = 0;
544
545 /* Already in tree? */
b4f1578f
AK
546 if (!(node = _find_dm_tree_node(dtree, major, minor))) {
547 if (!_deps(&dmt, dtree->mem, major, minor, &name, &uuid, &info, &deps))
548 return_NULL;
3d0480ed 549
f16aea9e 550 if (!(node = _create_dm_tree_node(dtree, name, uuid, &info,
cda69e17 551 NULL, udev_flags)))
b4f1578f 552 goto_out;
3d0480ed
AK
553 new = 1;
554 }
555
165e4a11
AK
556 if (!_link_tree_nodes(parent, node)) {
557 node = NULL;
b4f1578f 558 goto_out;
165e4a11 559 }
3d0480ed
AK
560
561 /* If node was already in tree, no need to recurse. */
562 if (!new)
165e4a11 563 goto out;
3d0480ed
AK
564
565 /* Can't recurse if not a mapped device or there are no dependencies */
566 if (!node->info.exists || !deps->count) {
b4f1578f
AK
567 if (!_add_to_bottomlevel(node)) {
568 stack;
165e4a11 569 node = NULL;
b4f1578f 570 }
165e4a11 571 goto out;
3d0480ed
AK
572 }
573
574 /* Add dependencies to tree */
575 for (i = 0; i < deps->count; i++)
b4f1578f 576 if (!_add_dev(dtree, node, MAJOR(deps->device[i]),
cda69e17 577 MINOR(deps->device[i]), udev_flags)) {
165e4a11 578 node = NULL;
b4f1578f 579 goto_out;
165e4a11 580 }
3d0480ed 581
3d0480ed
AK
582out:
583 if (dmt)
584 dm_task_destroy(dmt);
585
165e4a11
AK
586 return node;
587}
588
b4f1578f 589static int _node_clear_table(struct dm_tree_node *dnode)
165e4a11
AK
590{
591 struct dm_task *dmt;
592 struct dm_info *info;
593 const char *name;
594 int r;
595
596 if (!(info = &dnode->info)) {
b4f1578f 597 log_error("_node_clear_table failed: missing info");
165e4a11
AK
598 return 0;
599 }
600
b4f1578f
AK
601 if (!(name = dm_tree_node_get_name(dnode))) {
602 log_error("_node_clear_table failed: missing name");
165e4a11
AK
603 return 0;
604 }
605
606 /* Is there a table? */
607 if (!info->exists || !info->inactive_table)
608 return 1;
609
610 log_verbose("Clearing inactive table %s (%" PRIu32 ":%" PRIu32 ")",
611 name, info->major, info->minor);
612
613 if (!(dmt = dm_task_create(DM_DEVICE_CLEAR))) {
165e4a11
AK
614 log_error("Table clear dm_task creation failed for %s", name);
615 return 0;
616 }
617
618 if (!dm_task_set_major(dmt, info->major) ||
619 !dm_task_set_minor(dmt, info->minor)) {
620 log_error("Failed to set device number for %s table clear", name);
621 dm_task_destroy(dmt);
622 return 0;
623 }
624
625 r = dm_task_run(dmt);
626
627 if (!dm_task_get_info(dmt, info)) {
b4f1578f 628 log_error("_node_clear_table failed: info missing after running task for %s", name);
165e4a11
AK
629 r = 0;
630 }
631
632 dm_task_destroy(dmt);
633
3d0480ed
AK
634 return r;
635}
636
b4f1578f 637struct dm_tree_node *dm_tree_add_new_dev(struct dm_tree *dtree,
165e4a11
AK
638 const char *name,
639 const char *uuid,
640 uint32_t major, uint32_t minor,
641 int read_only,
642 int clear_inactive,
643 void *context)
644{
b4f1578f 645 struct dm_tree_node *dnode;
165e4a11
AK
646 struct dm_info info;
647 const char *name2;
648 const char *uuid2;
649
650 /* Do we need to add node to tree? */
b4f1578f
AK
651 if (!(dnode = dm_tree_find_node_by_uuid(dtree, uuid))) {
652 if (!(name2 = dm_pool_strdup(dtree->mem, name))) {
165e4a11
AK
653 log_error("name pool_strdup failed");
654 return NULL;
655 }
b4f1578f 656 if (!(uuid2 = dm_pool_strdup(dtree->mem, uuid))) {
165e4a11
AK
657 log_error("uuid pool_strdup failed");
658 return NULL;
659 }
660
661 info.major = 0;
662 info.minor = 0;
663 info.exists = 0;
664 info.live_table = 0;
665 info.inactive_table = 0;
666 info.read_only = 0;
667
f16aea9e
PR
668 if (!(dnode = _create_dm_tree_node(dtree, name2, uuid2, &info,
669 context, 0)))
b4f1578f 670 return_NULL;
165e4a11
AK
671
672 /* Attach to root node until a table is supplied */
b4f1578f
AK
673 if (!_add_to_toplevel(dnode) || !_add_to_bottomlevel(dnode))
674 return_NULL;
165e4a11
AK
675
676 dnode->props.major = major;
677 dnode->props.minor = minor;
678 dnode->props.new_name = NULL;
bb875bb9 679 dnode->props.size_changed = 0;
165e4a11
AK
680 } else if (strcmp(name, dnode->name)) {
681 /* Do we need to rename node? */
b4f1578f 682 if (!(dnode->props.new_name = dm_pool_strdup(dtree->mem, name))) {
165e4a11
AK
683 log_error("name pool_strdup failed");
684 return 0;
685 }
686 }
687
688 dnode->props.read_only = read_only ? 1 : 0;
52b84409
AK
689 dnode->props.read_ahead = DM_READ_AHEAD_AUTO;
690 dnode->props.read_ahead_flags = 0;
165e4a11 691
b4f1578f
AK
692 if (clear_inactive && !_node_clear_table(dnode))
693 return_NULL;
165e4a11
AK
694
695 dnode->context = context;
f16aea9e 696 dnode->udev_flags = 0;
165e4a11
AK
697
698 return dnode;
699}
700
f16aea9e
PR
701struct dm_tree_node *dm_tree_add_new_dev_with_udev_flags(struct dm_tree *dtree,
702 const char *name,
703 const char *uuid,
704 uint32_t major,
705 uint32_t minor,
706 int read_only,
707 int clear_inactive,
708 void *context,
709 uint16_t udev_flags)
710{
711 struct dm_tree_node *node;
712
713 if ((node = dm_tree_add_new_dev(dtree, name, uuid, major, minor, read_only,
714 clear_inactive, context)))
715 node->udev_flags = udev_flags;
716
717 return node;
718}
719
720
52b84409
AK
721void dm_tree_node_set_read_ahead(struct dm_tree_node *dnode,
722 uint32_t read_ahead,
723 uint32_t read_ahead_flags)
08e64ce5 724{
52b84409
AK
725 dnode->props.read_ahead = read_ahead;
726 dnode->props.read_ahead_flags = read_ahead_flags;
727}
728
76d1aec8
ZK
729void dm_tree_node_set_presuspend_node(struct dm_tree_node *node,
730 struct dm_tree_node *presuspend_node)
731{
732 node->presuspend_node = presuspend_node;
733}
734
b4f1578f 735int dm_tree_add_dev(struct dm_tree *dtree, uint32_t major, uint32_t minor)
3d0480ed 736{
cda69e17
PR
737 return _add_dev(dtree, &dtree->root, major, minor, 0) ? 1 : 0;
738}
739
740int dm_tree_add_dev_with_udev_flags(struct dm_tree *dtree, uint32_t major,
741 uint32_t minor, uint16_t udev_flags)
742{
743 return _add_dev(dtree, &dtree->root, major, minor, udev_flags) ? 1 : 0;
3d0480ed
AK
744}
745
04bde319 746const char *dm_tree_node_get_name(const struct dm_tree_node *node)
3d0480ed
AK
747{
748 return node->info.exists ? node->name : "";
749}
750
04bde319 751const char *dm_tree_node_get_uuid(const struct dm_tree_node *node)
3d0480ed
AK
752{
753 return node->info.exists ? node->uuid : "";
754}
755
04bde319 756const struct dm_info *dm_tree_node_get_info(const struct dm_tree_node *node)
3d0480ed
AK
757{
758 return &node->info;
759}
760
04bde319 761void *dm_tree_node_get_context(const struct dm_tree_node *node)
165e4a11
AK
762{
763 return node->context;
764}
765
04bde319 766int dm_tree_node_size_changed(const struct dm_tree_node *dnode)
eb91c4ee
MB
767{
768 return dnode->props.size_changed;
769}
770
04bde319 771int dm_tree_node_num_children(const struct dm_tree_node *node, uint32_t inverted)
3d0480ed
AK
772{
773 if (inverted) {
b4f1578f 774 if (_nodes_are_linked(&node->dtree->root, node))
3d0480ed 775 return 0;
2c44337b 776 return dm_list_size(&node->used_by);
3d0480ed
AK
777 }
778
b4f1578f 779 if (_nodes_are_linked(node, &node->dtree->root))
3d0480ed
AK
780 return 0;
781
2c44337b 782 return dm_list_size(&node->uses);
3d0480ed
AK
783}
784
2b69db1f
AK
785/*
786 * Returns 1 if no prefix supplied
787 */
788static int _uuid_prefix_matches(const char *uuid, const char *uuid_prefix, size_t uuid_prefix_len)
789{
790 if (!uuid_prefix)
791 return 1;
792
793 if (!strncmp(uuid, uuid_prefix, uuid_prefix_len))
794 return 1;
795
796 /* Handle transition: active device uuids might be missing the prefix */
797 if (uuid_prefix_len <= 4)
798 return 0;
799
87f98002 800 if (!strncmp(uuid, UUID_PREFIX, sizeof(UUID_PREFIX) - 1))
872dea04
AK
801 return 0;
802
87f98002 803 if (strncmp(uuid_prefix, UUID_PREFIX, sizeof(UUID_PREFIX) - 1))
2b69db1f
AK
804 return 0;
805
87f98002 806 if (!strncmp(uuid, uuid_prefix + sizeof(UUID_PREFIX) - 1, uuid_prefix_len - (sizeof(UUID_PREFIX) - 1)))
2b69db1f
AK
807 return 1;
808
809 return 0;
810}
811
690a5da2
AK
812/*
813 * Returns 1 if no children.
814 */
b4f1578f 815static int _children_suspended(struct dm_tree_node *node,
690a5da2
AK
816 uint32_t inverted,
817 const char *uuid_prefix,
818 size_t uuid_prefix_len)
819{
2c44337b 820 struct dm_list *list;
b4f1578f 821 struct dm_tree_link *dlink;
690a5da2
AK
822 const struct dm_info *dinfo;
823 const char *uuid;
824
825 if (inverted) {
b4f1578f 826 if (_nodes_are_linked(&node->dtree->root, node))
690a5da2
AK
827 return 1;
828 list = &node->used_by;
829 } else {
b4f1578f 830 if (_nodes_are_linked(node, &node->dtree->root))
690a5da2
AK
831 return 1;
832 list = &node->uses;
833 }
834
2c44337b 835 dm_list_iterate_items(dlink, list) {
b4f1578f 836 if (!(uuid = dm_tree_node_get_uuid(dlink->node))) {
690a5da2
AK
837 stack;
838 continue;
839 }
840
841 /* Ignore if it doesn't belong to this VG */
2b69db1f 842 if (!_uuid_prefix_matches(uuid, uuid_prefix, uuid_prefix_len))
690a5da2
AK
843 continue;
844
76d1aec8
ZK
845 /* Ignore if parent node wants to presuspend this node */
846 if (dlink->node->presuspend_node == node)
847 continue;
848
b4f1578f
AK
849 if (!(dinfo = dm_tree_node_get_info(dlink->node))) {
850 stack; /* FIXME Is this normal? */
690a5da2
AK
851 return 0;
852 }
853
854 if (!dinfo->suspended)
855 return 0;
856 }
857
858 return 1;
859}
860
3d0480ed
AK
861/*
862 * Set major and minor to zero for root of tree.
863 */
b4f1578f 864struct dm_tree_node *dm_tree_find_node(struct dm_tree *dtree,
3d0480ed
AK
865 uint32_t major,
866 uint32_t minor)
867{
868 if (!major && !minor)
b4f1578f 869 return &dtree->root;
3d0480ed 870
b4f1578f 871 return _find_dm_tree_node(dtree, major, minor);
3d0480ed
AK
872}
873
165e4a11
AK
874/*
875 * Set uuid to NULL for root of tree.
876 */
b4f1578f 877struct dm_tree_node *dm_tree_find_node_by_uuid(struct dm_tree *dtree,
165e4a11
AK
878 const char *uuid)
879{
880 if (!uuid || !*uuid)
b4f1578f 881 return &dtree->root;
165e4a11 882
b4f1578f 883 return _find_dm_tree_node_by_uuid(dtree, uuid);
165e4a11
AK
884}
885
3d0480ed
AK
886/*
887 * First time set *handle to NULL.
888 * Set inverted to invert the tree.
889 */
b4f1578f 890struct dm_tree_node *dm_tree_next_child(void **handle,
04bde319
ZK
891 const struct dm_tree_node *parent,
892 uint32_t inverted)
3d0480ed 893{
2c44337b 894 struct dm_list **dlink = (struct dm_list **) handle;
04bde319 895 const struct dm_list *use_list;
3d0480ed
AK
896
897 if (inverted)
898 use_list = &parent->used_by;
899 else
900 use_list = &parent->uses;
901
902 if (!*dlink)
2c44337b 903 *dlink = dm_list_first(use_list);
3d0480ed 904 else
2c44337b 905 *dlink = dm_list_next(use_list, *dlink);
3d0480ed 906
2c44337b 907 return (*dlink) ? dm_list_item(*dlink, struct dm_tree_link)->node : NULL;
3d0480ed
AK
908}
909
3e8c6b73 910/*
a6d97ede 911 * Deactivate a device with its dependencies if the uuid prefix matches.
3e8c6b73 912 */
db208f51
AK
913static int _info_by_dev(uint32_t major, uint32_t minor, int with_open_count,
914 struct dm_info *info)
3e8c6b73
AK
915{
916 struct dm_task *dmt;
917 int r;
918
919 if (!(dmt = dm_task_create(DM_DEVICE_INFO))) {
920 log_error("_info_by_dev: dm_task creation failed");
921 return 0;
922 }
923
924 if (!dm_task_set_major(dmt, major) || !dm_task_set_minor(dmt, minor)) {
925 log_error("_info_by_dev: Failed to set device number");
926 dm_task_destroy(dmt);
927 return 0;
928 }
929
db208f51
AK
930 if (!with_open_count && !dm_task_no_open_count(dmt))
931 log_error("Failed to disable open_count");
932
3e8c6b73
AK
933 if ((r = dm_task_run(dmt)))
934 r = dm_task_get_info(dmt, info);
935
936 dm_task_destroy(dmt);
937
938 return r;
939}
940
f3ef15ef
ZK
941/* Check if all parent nodes of given node have open_count == 0 */
942static int _node_has_closed_parents(struct dm_tree_node *node,
943 const char *uuid_prefix,
944 size_t uuid_prefix_len)
945{
946 struct dm_tree_link *dlink;
947 const struct dm_info *dinfo;
948 struct dm_info info;
949 const char *uuid;
950
951 /* Iterate through parents of this node */
952 dm_list_iterate_items(dlink, &node->used_by) {
953 if (!(uuid = dm_tree_node_get_uuid(dlink->node))) {
954 stack;
955 continue;
956 }
957
958 /* Ignore if it doesn't belong to this VG */
959 if (!_uuid_prefix_matches(uuid, uuid_prefix, uuid_prefix_len))
960 continue;
961
962 if (!(dinfo = dm_tree_node_get_info(dlink->node))) {
963 stack; /* FIXME Is this normal? */
964 return 0;
965 }
966
967 /* Refresh open_count */
968 if (!_info_by_dev(dinfo->major, dinfo->minor, 1, &info) ||
969 !info.exists)
970 continue;
971
eb418883
ZK
972 if (info.open_count) {
973 log_debug("Node %s %d:%d has open_count %d", uuid_prefix,
974 dinfo->major, dinfo->minor, info.open_count);
f3ef15ef 975 return 0;
eb418883 976 }
f3ef15ef
ZK
977 }
978
979 return 1;
980}
981
f16aea9e
PR
982static int _deactivate_node(const char *name, uint32_t major, uint32_t minor,
983 uint32_t *cookie, uint16_t udev_flags)
3e8c6b73
AK
984{
985 struct dm_task *dmt;
bd90c6b2 986 int r = 0;
3e8c6b73
AK
987
988 log_verbose("Removing %s (%" PRIu32 ":%" PRIu32 ")", name, major, minor);
989
990 if (!(dmt = dm_task_create(DM_DEVICE_REMOVE))) {
991 log_error("Deactivation dm_task creation failed for %s", name);
992 return 0;
993 }
994
995 if (!dm_task_set_major(dmt, major) || !dm_task_set_minor(dmt, minor)) {
996 log_error("Failed to set device number for %s deactivation", name);
bd90c6b2 997 goto out;
3e8c6b73
AK
998 }
999
1000 if (!dm_task_no_open_count(dmt))
1001 log_error("Failed to disable open_count");
1002
f16aea9e 1003 if (!dm_task_set_cookie(dmt, cookie, udev_flags))
bd90c6b2
AK
1004 goto out;
1005
3e8c6b73
AK
1006 r = dm_task_run(dmt);
1007
0437bccc
AK
1008 /* FIXME Until kernel returns actual name so dm-iface.c can handle it */
1009 rm_dev_node(name, dmt->cookie_set && !(udev_flags & DM_UDEV_DISABLE_DM_RULES_FLAG),
9032898e 1010 dmt->cookie_set && (udev_flags & DM_UDEV_DISABLE_LIBRARY_FALLBACK));
165e4a11 1011
db208f51
AK
1012 /* FIXME Remove node from tree or mark invalid? */
1013
bd90c6b2 1014out:
db208f51
AK
1015 dm_task_destroy(dmt);
1016
1017 return r;
1018}
1019
bd90c6b2 1020static int _rename_node(const char *old_name, const char *new_name, uint32_t major,
f16aea9e 1021 uint32_t minor, uint32_t *cookie, uint16_t udev_flags)
165e4a11
AK
1022{
1023 struct dm_task *dmt;
1024 int r = 0;
1025
1026 log_verbose("Renaming %s (%" PRIu32 ":%" PRIu32 ") to %s", old_name, major, minor, new_name);
1027
1028 if (!(dmt = dm_task_create(DM_DEVICE_RENAME))) {
1029 log_error("Rename dm_task creation failed for %s", old_name);
1030 return 0;
1031 }
1032
1033 if (!dm_task_set_name(dmt, old_name)) {
1034 log_error("Failed to set name for %s rename.", old_name);
1035 goto out;
1036 }
1037
b4f1578f
AK
1038 if (!dm_task_set_newname(dmt, new_name))
1039 goto_out;
165e4a11
AK
1040
1041 if (!dm_task_no_open_count(dmt))
1042 log_error("Failed to disable open_count");
1043
f16aea9e 1044 if (!dm_task_set_cookie(dmt, cookie, udev_flags))
bd90c6b2
AK
1045 goto out;
1046
165e4a11
AK
1047 r = dm_task_run(dmt);
1048
1049out:
1050 dm_task_destroy(dmt);
1051
1052 return r;
1053}
1054
165e4a11
AK
1055/* FIXME Merge with _suspend_node? */
1056static int _resume_node(const char *name, uint32_t major, uint32_t minor,
52b84409 1057 uint32_t read_ahead, uint32_t read_ahead_flags,
f16aea9e 1058 struct dm_info *newinfo, uint32_t *cookie,
1840aa09 1059 uint16_t udev_flags, int already_suspended)
165e4a11
AK
1060{
1061 struct dm_task *dmt;
bd90c6b2 1062 int r = 0;
165e4a11
AK
1063
1064 log_verbose("Resuming %s (%" PRIu32 ":%" PRIu32 ")", name, major, minor);
1065
1066 if (!(dmt = dm_task_create(DM_DEVICE_RESUME))) {
1067 log_error("Suspend dm_task creation failed for %s", name);
1068 return 0;
1069 }
1070
0b7d16bc
AK
1071 /* FIXME Kernel should fill in name on return instead */
1072 if (!dm_task_set_name(dmt, name)) {
1073 log_error("Failed to set readahead device name for %s", name);
bd90c6b2 1074 goto out;
0b7d16bc
AK
1075 }
1076
165e4a11
AK
1077 if (!dm_task_set_major(dmt, major) || !dm_task_set_minor(dmt, minor)) {
1078 log_error("Failed to set device number for %s resumption.", name);
bd90c6b2 1079 goto out;
165e4a11
AK
1080 }
1081
1082 if (!dm_task_no_open_count(dmt))
1083 log_error("Failed to disable open_count");
1084
52b84409
AK
1085 if (!dm_task_set_read_ahead(dmt, read_ahead, read_ahead_flags))
1086 log_error("Failed to set read ahead");
1087
f16aea9e 1088 if (!dm_task_set_cookie(dmt, cookie, udev_flags))
bd90c6b2
AK
1089 goto out;
1090
1840aa09
AK
1091 if ((r = dm_task_run(dmt))) {
1092 if (already_suspended)
1093 dec_suspended();
165e4a11 1094 r = dm_task_get_info(dmt, newinfo);
1840aa09 1095 }
165e4a11 1096
bd90c6b2 1097out:
165e4a11
AK
1098 dm_task_destroy(dmt);
1099
1100 return r;
1101}
1102
db208f51 1103static int _suspend_node(const char *name, uint32_t major, uint32_t minor,
b9ffd32c 1104 int skip_lockfs, int no_flush, struct dm_info *newinfo)
db208f51
AK
1105{
1106 struct dm_task *dmt;
1107 int r;
1108
b9ffd32c
AK
1109 log_verbose("Suspending %s (%" PRIu32 ":%" PRIu32 ")%s%s",
1110 name, major, minor,
1111 skip_lockfs ? "" : " with filesystem sync",
6e1898a5 1112 no_flush ? "" : " with device flush");
db208f51
AK
1113
1114 if (!(dmt = dm_task_create(DM_DEVICE_SUSPEND))) {
1115 log_error("Suspend dm_task creation failed for %s", name);
1116 return 0;
1117 }
1118
1119 if (!dm_task_set_major(dmt, major) || !dm_task_set_minor(dmt, minor)) {
1120 log_error("Failed to set device number for %s suspension.", name);
1121 dm_task_destroy(dmt);
1122 return 0;
1123 }
1124
1125 if (!dm_task_no_open_count(dmt))
1126 log_error("Failed to disable open_count");
1127
c55b1410
AK
1128 if (skip_lockfs && !dm_task_skip_lockfs(dmt))
1129 log_error("Failed to set skip_lockfs flag.");
1130
b9ffd32c
AK
1131 if (no_flush && !dm_task_no_flush(dmt))
1132 log_error("Failed to set no_flush flag.");
1133
1840aa09
AK
1134 if ((r = dm_task_run(dmt))) {
1135 inc_suspended();
db208f51 1136 r = dm_task_get_info(dmt, newinfo);
1840aa09 1137 }
db208f51 1138
3e8c6b73
AK
1139 dm_task_destroy(dmt);
1140
1141 return r;
1142}
1143
18e0f934
AK
1144/*
1145 * FIXME Don't attempt to deactivate known internal dependencies.
1146 */
1147static int _dm_tree_deactivate_children(struct dm_tree_node *dnode,
1148 const char *uuid_prefix,
1149 size_t uuid_prefix_len,
1150 unsigned level)
3e8c6b73 1151{
b7eb2ad0 1152 int r = 1;
3e8c6b73 1153 void *handle = NULL;
b4f1578f 1154 struct dm_tree_node *child = dnode;
3e8c6b73
AK
1155 struct dm_info info;
1156 const struct dm_info *dinfo;
1157 const char *name;
1158 const char *uuid;
1159
b4f1578f
AK
1160 while ((child = dm_tree_next_child(&handle, dnode, 0))) {
1161 if (!(dinfo = dm_tree_node_get_info(child))) {
3e8c6b73
AK
1162 stack;
1163 continue;
1164 }
1165
b4f1578f 1166 if (!(name = dm_tree_node_get_name(child))) {
3e8c6b73
AK
1167 stack;
1168 continue;
1169 }
1170
b4f1578f 1171 if (!(uuid = dm_tree_node_get_uuid(child))) {
3e8c6b73
AK
1172 stack;
1173 continue;
1174 }
1175
1176 /* Ignore if it doesn't belong to this VG */
2b69db1f 1177 if (!_uuid_prefix_matches(uuid, uuid_prefix, uuid_prefix_len))
3e8c6b73 1178 continue;
3e8c6b73
AK
1179
1180 /* Refresh open_count */
db208f51 1181 if (!_info_by_dev(dinfo->major, dinfo->minor, 1, &info) ||
f55021f4 1182 !info.exists)
3e8c6b73
AK
1183 continue;
1184
f3ef15ef
ZK
1185 /* Also checking open_count in parent nodes of presuspend_node */
1186 if (info.open_count ||
1187 (child->presuspend_node &&
1188 !_node_has_closed_parents(child->presuspend_node,
1189 uuid_prefix, uuid_prefix_len))) {
18e0f934
AK
1190 /* Only report error from (likely non-internal) dependency at top level */
1191 if (!level) {
1192 log_error("Unable to deactivate open %s (%" PRIu32
1193 ":%" PRIu32 ")", name, info.major,
1194 info.minor);
1195 r = 0;
1196 }
f55021f4
AK
1197 continue;
1198 }
1199
76d1aec8
ZK
1200 /* Suspend child node first if requested */
1201 if (child->presuspend_node &&
1202 !dm_tree_suspend_children(child, uuid_prefix, uuid_prefix_len))
1203 continue;
1204
f16aea9e 1205 if (!_deactivate_node(name, info.major, info.minor,
584d1fb7 1206 &child->dtree->cookie, child->udev_flags)) {
3e8c6b73
AK
1207 log_error("Unable to deactivate %s (%" PRIu32
1208 ":%" PRIu32 ")", name, info.major,
1209 info.minor);
b7eb2ad0 1210 r = 0;
3e8c6b73 1211 continue;
f4249251
AK
1212 } else if (info.suspended)
1213 dec_suspended();
3e8c6b73 1214
18e0f934
AK
1215 if (dm_tree_node_num_children(child, 0)) {
1216 if (!_dm_tree_deactivate_children(child, uuid_prefix, uuid_prefix_len, level + 1))
b7eb2ad0 1217 return_0;
18e0f934 1218 }
3e8c6b73
AK
1219 }
1220
b7eb2ad0 1221 return r;
3e8c6b73 1222}
db208f51 1223
18e0f934
AK
1224int dm_tree_deactivate_children(struct dm_tree_node *dnode,
1225 const char *uuid_prefix,
1226 size_t uuid_prefix_len)
1227{
1228 return _dm_tree_deactivate_children(dnode, uuid_prefix, uuid_prefix_len, 0);
1229}
1230
c55b1410
AK
1231void dm_tree_skip_lockfs(struct dm_tree_node *dnode)
1232{
1233 dnode->dtree->skip_lockfs = 1;
1234}
1235
b9ffd32c
AK
1236void dm_tree_use_no_flush_suspend(struct dm_tree_node *dnode)
1237{
1238 dnode->dtree->no_flush = 1;
1239}
1240
b4f1578f 1241int dm_tree_suspend_children(struct dm_tree_node *dnode,
08e64ce5
ZK
1242 const char *uuid_prefix,
1243 size_t uuid_prefix_len)
db208f51 1244{
68085c93 1245 int r = 1;
db208f51 1246 void *handle = NULL;
b4f1578f 1247 struct dm_tree_node *child = dnode;
db208f51
AK
1248 struct dm_info info, newinfo;
1249 const struct dm_info *dinfo;
1250 const char *name;
1251 const char *uuid;
1252
690a5da2 1253 /* Suspend nodes at this level of the tree */
b4f1578f
AK
1254 while ((child = dm_tree_next_child(&handle, dnode, 0))) {
1255 if (!(dinfo = dm_tree_node_get_info(child))) {
db208f51
AK
1256 stack;
1257 continue;
1258 }
1259
b4f1578f 1260 if (!(name = dm_tree_node_get_name(child))) {
db208f51
AK
1261 stack;
1262 continue;
1263 }
1264
b4f1578f 1265 if (!(uuid = dm_tree_node_get_uuid(child))) {
db208f51
AK
1266 stack;
1267 continue;
1268 }
1269
1270 /* Ignore if it doesn't belong to this VG */
2b69db1f 1271 if (!_uuid_prefix_matches(uuid, uuid_prefix, uuid_prefix_len))
db208f51
AK
1272 continue;
1273
690a5da2
AK
1274 /* Ensure immediate parents are already suspended */
1275 if (!_children_suspended(child, 1, uuid_prefix, uuid_prefix_len))
1276 continue;
1277
db208f51 1278 if (!_info_by_dev(dinfo->major, dinfo->minor, 0, &info) ||
b700541f 1279 !info.exists || info.suspended)
db208f51
AK
1280 continue;
1281
c55b1410 1282 if (!_suspend_node(name, info.major, info.minor,
b9ffd32c
AK
1283 child->dtree->skip_lockfs,
1284 child->dtree->no_flush, &newinfo)) {
db208f51
AK
1285 log_error("Unable to suspend %s (%" PRIu32
1286 ":%" PRIu32 ")", name, info.major,
1287 info.minor);
68085c93 1288 r = 0;
db208f51
AK
1289 continue;
1290 }
1291
1292 /* Update cached info */
1293 child->info = newinfo;
690a5da2
AK
1294 }
1295
1296 /* Then suspend any child nodes */
1297 handle = NULL;
1298
b4f1578f
AK
1299 while ((child = dm_tree_next_child(&handle, dnode, 0))) {
1300 if (!(uuid = dm_tree_node_get_uuid(child))) {
690a5da2
AK
1301 stack;
1302 continue;
1303 }
1304
1305 /* Ignore if it doesn't belong to this VG */
87f98002 1306 if (!_uuid_prefix_matches(uuid, uuid_prefix, uuid_prefix_len))
690a5da2 1307 continue;
db208f51 1308
b4f1578f 1309 if (dm_tree_node_num_children(child, 0))
68085c93
MS
1310 if (!dm_tree_suspend_children(child, uuid_prefix, uuid_prefix_len))
1311 return_0;
db208f51
AK
1312 }
1313
68085c93 1314 return r;
db208f51
AK
1315}
1316
b4f1578f 1317int dm_tree_activate_children(struct dm_tree_node *dnode,
db208f51
AK
1318 const char *uuid_prefix,
1319 size_t uuid_prefix_len)
1320{
2ca6b865 1321 int r = 1;
db208f51 1322 void *handle = NULL;
b4f1578f 1323 struct dm_tree_node *child = dnode;
165e4a11
AK
1324 struct dm_info newinfo;
1325 const char *name;
db208f51 1326 const char *uuid;
56c28292 1327 int priority;
db208f51 1328
165e4a11 1329 /* Activate children first */
b4f1578f
AK
1330 while ((child = dm_tree_next_child(&handle, dnode, 0))) {
1331 if (!(uuid = dm_tree_node_get_uuid(child))) {
165e4a11
AK
1332 stack;
1333 continue;
db208f51
AK
1334 }
1335
908db078
AK
1336 if (!_uuid_prefix_matches(uuid, uuid_prefix, uuid_prefix_len))
1337 continue;
db208f51 1338
b4f1578f 1339 if (dm_tree_node_num_children(child, 0))
2ca6b865
MS
1340 if (!dm_tree_activate_children(child, uuid_prefix, uuid_prefix_len))
1341 return_0;
56c28292 1342 }
165e4a11 1343
56c28292 1344 handle = NULL;
165e4a11 1345
aa6f4e51 1346 for (priority = 0; priority < 3; priority++) {
56c28292
AK
1347 while ((child = dm_tree_next_child(&handle, dnode, 0))) {
1348 if (!(uuid = dm_tree_node_get_uuid(child))) {
1349 stack;
1350 continue;
165e4a11 1351 }
165e4a11 1352
56c28292
AK
1353 if (!_uuid_prefix_matches(uuid, uuid_prefix, uuid_prefix_len))
1354 continue;
165e4a11 1355
56c28292
AK
1356 if (priority != child->activation_priority)
1357 continue;
165e4a11 1358
56c28292
AK
1359 if (!(name = dm_tree_node_get_name(child))) {
1360 stack;
1361 continue;
1362 }
1363
1364 /* Rename? */
1365 if (child->props.new_name) {
bd90c6b2 1366 if (!_rename_node(name, child->props.new_name, child->info.major,
f16aea9e
PR
1367 child->info.minor, &child->dtree->cookie,
1368 child->udev_flags)) {
56c28292
AK
1369 log_error("Failed to rename %s (%" PRIu32
1370 ":%" PRIu32 ") to %s", name, child->info.major,
1371 child->info.minor, child->props.new_name);
1372 return 0;
1373 }
1374 child->name = child->props.new_name;
1375 child->props.new_name = NULL;
1376 }
1377
1378 if (!child->info.inactive_table && !child->info.suspended)
1379 continue;
1380
bafa2f39 1381 if (!_resume_node(child->name, child->info.major, child->info.minor,
bd90c6b2 1382 child->props.read_ahead, child->props.read_ahead_flags,
1840aa09 1383 &newinfo, &child->dtree->cookie, child->udev_flags, child->info.suspended)) {
56c28292 1384 log_error("Unable to resume %s (%" PRIu32
bafa2f39 1385 ":%" PRIu32 ")", child->name, child->info.major,
56c28292 1386 child->info.minor);
2ca6b865 1387 r = 0;
56c28292
AK
1388 continue;
1389 }
1390
1391 /* Update cached info */
1392 child->info = newinfo;
1393 }
db208f51
AK
1394 }
1395
165e4a11
AK
1396 handle = NULL;
1397
2ca6b865 1398 return r;
165e4a11
AK
1399}
1400
b4f1578f 1401static int _create_node(struct dm_tree_node *dnode)
165e4a11
AK
1402{
1403 int r = 0;
1404 struct dm_task *dmt;
1405
1406 log_verbose("Creating %s", dnode->name);
1407
1408 if (!(dmt = dm_task_create(DM_DEVICE_CREATE))) {
1409 log_error("Create dm_task creation failed for %s", dnode->name);
1410 return 0;
1411 }
1412
1413 if (!dm_task_set_name(dmt, dnode->name)) {
1414 log_error("Failed to set device name for %s", dnode->name);
1415 goto out;
1416 }
1417
1418 if (!dm_task_set_uuid(dmt, dnode->uuid)) {
1419 log_error("Failed to set uuid for %s", dnode->name);
1420 goto out;
1421 }
1422
1423 if (dnode->props.major &&
1424 (!dm_task_set_major(dmt, dnode->props.major) ||
1425 !dm_task_set_minor(dmt, dnode->props.minor))) {
1426 log_error("Failed to set device number for %s creation.", dnode->name);
1427 goto out;
1428 }
1429
1430 if (dnode->props.read_only && !dm_task_set_ro(dmt)) {
1431 log_error("Failed to set read only flag for %s", dnode->name);
1432 goto out;
1433 }
1434
1435 if (!dm_task_no_open_count(dmt))
1436 log_error("Failed to disable open_count");
1437
1438 if ((r = dm_task_run(dmt)))
1439 r = dm_task_get_info(dmt, &dnode->info);
1440
1441out:
1442 dm_task_destroy(dmt);
1443
1444 return r;
1445}
1446
1447
b4f1578f 1448static int _build_dev_string(char *devbuf, size_t bufsize, struct dm_tree_node *node)
165e4a11
AK
1449{
1450 if (!dm_format_dev(devbuf, bufsize, node->info.major, node->info.minor)) {
1451 log_error("Failed to format %s device number for %s as dm "
1452 "target (%u,%u)",
1453 node->name, node->uuid, node->info.major, node->info.minor);
1454 return 0;
1455 }
1456
1457 return 1;
1458}
1459
ffa9b6a5
ZK
1460/* simplify string emiting code */
1461#define EMIT_PARAMS(p, str...)\
7b6c011c
AK
1462do {\
1463 int w;\
1464 if ((w = dm_snprintf(params + p, paramsize - (size_t) p, str)) < 0) {\
1465 stack; /* Out of space */\
1466 return -1;\
1467 }\
1468 p += w;\
1469} while (0)
ffa9b6a5 1470
3c74075f
JEB
1471/*
1472 * _emit_areas_line
1473 *
1474 * Returns: 1 on success, 0 on failure
1475 */
08f1ddea 1476static int _emit_areas_line(struct dm_task *dmt __attribute__((unused)),
4dcaa230
AK
1477 struct load_segment *seg, char *params,
1478 size_t paramsize, int *pos)
165e4a11
AK
1479{
1480 struct seg_area *area;
7d7d93ac 1481 char devbuf[DM_FORMAT_DEV_BUFSIZE];
609faae9 1482 unsigned first_time = 1;
db3c1ac1 1483 const char *logtype, *synctype;
b262f3e1 1484 unsigned log_parm_count;
165e4a11 1485
2c44337b 1486 dm_list_iterate_items(area, &seg->areas) {
b262f3e1
ZK
1487 switch (seg->type) {
1488 case SEG_REPLICATOR_DEV:
6d04311e
JEB
1489 if (!_build_dev_string(devbuf, sizeof(devbuf), area->dev_node))
1490 return_0;
1491
b262f3e1
ZK
1492 EMIT_PARAMS(*pos, " %d 1 %s", area->rsite_index, devbuf);
1493 if (first_time)
1494 EMIT_PARAMS(*pos, " nolog 0");
1495 else {
1496 /* Remote devices */
1497 log_parm_count = (area->flags &
1498 (DM_NOSYNC | DM_FORCESYNC)) ? 2 : 1;
1499
1500 if (!area->slog) {
1501 devbuf[0] = 0; /* Only core log parameters */
1502 logtype = "core";
1503 } else {
1504 devbuf[0] = ' '; /* Extra space before device name */
1505 if (!_build_dev_string(devbuf + 1,
1506 sizeof(devbuf) - 1,
1507 area->slog))
1508 return_0;
1509 logtype = "disk";
1510 log_parm_count++; /* Extra sync log device name parameter */
1511 }
1512
1513 EMIT_PARAMS(*pos, " %s %u%s %" PRIu64, logtype,
1514 log_parm_count, devbuf, area->region_size);
1515
db3c1ac1
AK
1516 synctype = (area->flags & DM_NOSYNC) ?
1517 " nosync" : (area->flags & DM_FORCESYNC) ?
1518 " sync" : NULL;
b262f3e1 1519
db3c1ac1
AK
1520 if (synctype)
1521 EMIT_PARAMS(*pos, "%s", synctype);
b262f3e1
ZK
1522 }
1523 break;
cac52ca4
JEB
1524 case SEG_RAID1:
1525 case SEG_RAID4:
1526 case SEG_RAID5_LA:
1527 case SEG_RAID5_RA:
1528 case SEG_RAID5_LS:
1529 case SEG_RAID5_RS:
1530 case SEG_RAID6_ZR:
1531 case SEG_RAID6_NR:
1532 case SEG_RAID6_NC:
6d04311e
JEB
1533 if (!area->dev_node) {
1534 EMIT_PARAMS(*pos, " -");
1535 break;
1536 }
1537 if (!_build_dev_string(devbuf, sizeof(devbuf), area->dev_node))
1538 return_0;
1539
cac52ca4
JEB
1540 EMIT_PARAMS(*pos, " %s", devbuf);
1541 break;
b262f3e1 1542 default:
6d04311e
JEB
1543 if (!_build_dev_string(devbuf, sizeof(devbuf), area->dev_node))
1544 return_0;
1545
b262f3e1
ZK
1546 EMIT_PARAMS(*pos, "%s%s %" PRIu64, first_time ? "" : " ",
1547 devbuf, area->offset);
1548 }
609faae9
AK
1549
1550 first_time = 0;
165e4a11
AK
1551 }
1552
1553 return 1;
1554}
1555
b262f3e1
ZK
1556static int _replicator_emit_segment_line(const struct load_segment *seg, char *params,
1557 size_t paramsize, int *pos)
1558{
1559 const struct load_segment *rlog_seg;
1560 struct replicator_site *rsite;
1561 char rlogbuf[DM_FORMAT_DEV_BUFSIZE];
1562 unsigned parm_count;
1563
1564 if (!seg->log || !_build_dev_string(rlogbuf, sizeof(rlogbuf), seg->log))
1565 return_0;
1566
1567 rlog_seg = dm_list_item(dm_list_last(&seg->log->props.segs),
1568 struct load_segment);
1569
1570 EMIT_PARAMS(*pos, "%s 4 %s 0 auto %" PRIu64,
1571 seg->rlog_type, rlogbuf, rlog_seg->size);
1572
1573 dm_list_iterate_items(rsite, &seg->rsites) {
1574 parm_count = (rsite->fall_behind_data
1575 || rsite->fall_behind_ios
1576 || rsite->async_timeout) ? 4 : 2;
1577
1578 EMIT_PARAMS(*pos, " blockdev %u %u %s", parm_count, rsite->rsite_index,
1579 (rsite->mode == DM_REPLICATOR_SYNC) ? "synchronous" : "asynchronous");
1580
1581 if (rsite->fall_behind_data)
1582 EMIT_PARAMS(*pos, " data %" PRIu64, rsite->fall_behind_data);
1583 else if (rsite->fall_behind_ios)
1584 EMIT_PARAMS(*pos, " ios %" PRIu32, rsite->fall_behind_ios);
1585 else if (rsite->async_timeout)
1586 EMIT_PARAMS(*pos, " timeout %" PRIu32, rsite->async_timeout);
1587 }
1588
1589 return 1;
1590}
1591
3c74075f 1592/*
3c74075f
JEB
1593 * Returns: 1 on success, 0 on failure
1594 */
536f0e08
AK
1595static int _mirror_emit_segment_line(struct dm_task *dmt, uint32_t major,
1596 uint32_t minor, struct load_segment *seg,
1597 uint64_t *seg_start, char *params,
1598 size_t paramsize)
165e4a11 1599{
8f26e18c
JEB
1600 int block_on_error = 0;
1601 int handle_errors = 0;
1602 int dm_log_userspace = 0;
1603 struct utsname uts;
dbcb64b8 1604 unsigned log_parm_count;
b39fdcf4 1605 int pos = 0, parts;
7d7d93ac 1606 char logbuf[DM_FORMAT_DEV_BUFSIZE];
dbcb64b8 1607 const char *logtype;
b39fdcf4 1608 unsigned kmaj = 0, kmin = 0, krel = 0;
165e4a11 1609
b39fdcf4
MB
1610 if (uname(&uts) == -1) {
1611 log_error("Cannot read kernel release version.");
1612 return 0;
1613 }
1614
1615 /* Kernels with a major number of 2 always had 3 parts. */
1616 parts = sscanf(uts.release, "%u.%u.%u", &kmaj, &kmin, &krel);
1617 if (parts < 1 || (kmaj < 3 && parts < 3)) {
1618 log_error("Wrong kernel release version %s.", uts.release);
30a65310
ZK
1619 return 0;
1620 }
67b25ed4 1621
8f26e18c
JEB
1622 if ((seg->flags & DM_BLOCK_ON_ERROR)) {
1623 /*
1624 * Originally, block_on_error was an argument to the log
1625 * portion of the mirror CTR table. It was renamed to
1626 * "handle_errors" and now resides in the 'features'
1627 * section of the mirror CTR table (i.e. at the end).
1628 *
1629 * We can identify whether to use "block_on_error" or
1630 * "handle_errors" by the dm-mirror module's version
1631 * number (>= 1.12) or by the kernel version (>= 2.6.22).
1632 */
ba61f848 1633 if (KERNEL_VERSION(kmaj, kmin, krel) >= KERNEL_VERSION(2, 6, 22))
8f26e18c
JEB
1634 handle_errors = 1;
1635 else
1636 block_on_error = 1;
1637 }
1638
1639 if (seg->clustered) {
1640 /* Cluster mirrors require a UUID */
1641 if (!seg->uuid)
1642 return_0;
1643
1644 /*
1645 * Cluster mirrors used to have their own log
1646 * types. Now they are accessed through the
1647 * userspace log type.
1648 *
1649 * The dm-log-userspace module was added to the
1650 * 2.6.31 kernel.
1651 */
ba61f848 1652 if (KERNEL_VERSION(kmaj, kmin, krel) >= KERNEL_VERSION(2, 6, 31))
8f26e18c
JEB
1653 dm_log_userspace = 1;
1654 }
1655
1656 /* Region size */
1657 log_parm_count = 1;
1658
1659 /* [no]sync, block_on_error etc. */
1660 log_parm_count += hweight32(seg->flags);
311d6d81 1661
8f26e18c
JEB
1662 /* "handle_errors" is a feature arg now */
1663 if (handle_errors)
1664 log_parm_count--;
1665
1666 /* DM_CORELOG does not count in the param list */
1667 if (seg->flags & DM_CORELOG)
1668 log_parm_count--;
1669
1670 if (seg->clustered) {
1671 log_parm_count++; /* For UUID */
1672
1673 if (!dm_log_userspace)
ffa9b6a5 1674 EMIT_PARAMS(pos, "clustered-");
49b95a5e
JEB
1675 else
1676 /* For clustered-* type field inserted later */
1677 log_parm_count++;
8f26e18c 1678 }
dbcb64b8 1679
8f26e18c
JEB
1680 if (!seg->log)
1681 logtype = "core";
1682 else {
1683 logtype = "disk";
1684 log_parm_count++;
1685 if (!_build_dev_string(logbuf, sizeof(logbuf), seg->log))
1686 return_0;
1687 }
dbcb64b8 1688
8f26e18c
JEB
1689 if (dm_log_userspace)
1690 EMIT_PARAMS(pos, "userspace %u %s clustered-%s",
1691 log_parm_count, seg->uuid, logtype);
1692 else
ffa9b6a5 1693 EMIT_PARAMS(pos, "%s %u", logtype, log_parm_count);
dbcb64b8 1694
8f26e18c
JEB
1695 if (seg->log)
1696 EMIT_PARAMS(pos, " %s", logbuf);
1697
1698 EMIT_PARAMS(pos, " %u", seg->region_size);
dbcb64b8 1699
8f26e18c
JEB
1700 if (seg->clustered && !dm_log_userspace)
1701 EMIT_PARAMS(pos, " %s", seg->uuid);
67b25ed4 1702
8f26e18c
JEB
1703 if ((seg->flags & DM_NOSYNC))
1704 EMIT_PARAMS(pos, " nosync");
1705 else if ((seg->flags & DM_FORCESYNC))
1706 EMIT_PARAMS(pos, " sync");
dbcb64b8 1707
8f26e18c
JEB
1708 if (block_on_error)
1709 EMIT_PARAMS(pos, " block_on_error");
1710
1711 EMIT_PARAMS(pos, " %u ", seg->mirror_area_count);
1712
5f3325fc 1713 if (_emit_areas_line(dmt, seg, params, paramsize, &pos) <= 0)
3c74075f 1714 return_0;
dbcb64b8 1715
8f26e18c
JEB
1716 if (handle_errors)
1717 EMIT_PARAMS(pos, " 1 handle_errors");
ffa9b6a5 1718
3c74075f 1719 return 1;
8f26e18c
JEB
1720}
1721
cac52ca4
JEB
1722static int _raid_emit_segment_line(struct dm_task *dmt, uint32_t major,
1723 uint32_t minor, struct load_segment *seg,
1724 uint64_t *seg_start, char *params,
1725 size_t paramsize)
1726{
1727 int param_count = 1; /* mandatory 'chunk size'/'stripe size' arg */
1728 int pos = 0;
1729
1730 if ((seg->flags & DM_NOSYNC) || (seg->flags & DM_FORCESYNC))
1731 param_count++;
1732
1733 if (seg->region_size)
1734 param_count += 2;
1735
1736 if ((seg->type == SEG_RAID1) && seg->stripe_size)
1737 log_error("WARNING: Ignoring RAID1 stripe size");
1738
1739 EMIT_PARAMS(pos, "%s %d %u", dm_segtypes[seg->type].target,
1740 param_count, seg->stripe_size);
1741
1742 if (seg->flags & DM_NOSYNC)
1743 EMIT_PARAMS(pos, " nosync");
1744 else if (seg->flags & DM_FORCESYNC)
1745 EMIT_PARAMS(pos, " sync");
1746
1747 if (seg->region_size)
1748 EMIT_PARAMS(pos, " region_size %u", seg->region_size);
1749
1750 /* Print number of metadata/data device pairs */
1751 EMIT_PARAMS(pos, " %u", seg->area_count/2);
1752
1753 if (_emit_areas_line(dmt, seg, params, paramsize, &pos) <= 0)
1754 return_0;
1755
1756 return 1;
1757}
1758
8f26e18c
JEB
1759static int _emit_segment_line(struct dm_task *dmt, uint32_t major,
1760 uint32_t minor, struct load_segment *seg,
1761 uint64_t *seg_start, char *params,
1762 size_t paramsize)
1763{
1764 int pos = 0;
1765 int r;
cac52ca4 1766 int target_type_is_raid = 0;
8f26e18c 1767 char originbuf[DM_FORMAT_DEV_BUFSIZE], cowbuf[DM_FORMAT_DEV_BUFSIZE];
dbcb64b8 1768
8f26e18c
JEB
1769 switch(seg->type) {
1770 case SEG_ERROR:
1771 case SEG_ZERO:
1772 case SEG_LINEAR:
1773 break;
1774 case SEG_MIRRORED:
1775 /* Mirrors are pretty complicated - now in separate function */
536f0e08
AK
1776 r = _mirror_emit_segment_line(dmt, major, minor, seg, seg_start,
1777 params, paramsize);
3c74075f
JEB
1778 if (!r)
1779 return_0;
165e4a11 1780 break;
b262f3e1
ZK
1781 case SEG_REPLICATOR:
1782 if ((r = _replicator_emit_segment_line(seg, params, paramsize,
1783 &pos)) <= 0) {
1784 stack;
1785 return r;
1786 }
1787 break;
1788 case SEG_REPLICATOR_DEV:
1789 if (!seg->replicator || !_build_dev_string(originbuf,
1790 sizeof(originbuf),
1791 seg->replicator))
1792 return_0;
1793
1794 EMIT_PARAMS(pos, "%s %" PRIu64, originbuf, seg->rdevice_index);
1795 break;
165e4a11 1796 case SEG_SNAPSHOT:
aa6f4e51 1797 case SEG_SNAPSHOT_MERGE:
b4f1578f
AK
1798 if (!_build_dev_string(originbuf, sizeof(originbuf), seg->origin))
1799 return_0;
1800 if (!_build_dev_string(cowbuf, sizeof(cowbuf), seg->cow))
1801 return_0;
ffa9b6a5
ZK
1802 EMIT_PARAMS(pos, "%s %s %c %d", originbuf, cowbuf,
1803 seg->persistent ? 'P' : 'N', seg->chunk_size);
165e4a11
AK
1804 break;
1805 case SEG_SNAPSHOT_ORIGIN:
b4f1578f
AK
1806 if (!_build_dev_string(originbuf, sizeof(originbuf), seg->origin))
1807 return_0;
ffa9b6a5 1808 EMIT_PARAMS(pos, "%s", originbuf);
165e4a11
AK
1809 break;
1810 case SEG_STRIPED:
609faae9 1811 EMIT_PARAMS(pos, "%u %u ", seg->area_count, seg->stripe_size);
165e4a11 1812 break;
12ca060e 1813 case SEG_CRYPT:
609faae9 1814 EMIT_PARAMS(pos, "%s%s%s%s%s %s %" PRIu64 " ", seg->cipher,
12ca060e
MB
1815 seg->chainmode ? "-" : "", seg->chainmode ?: "",
1816 seg->iv ? "-" : "", seg->iv ?: "", seg->key,
1817 seg->iv_offset != DM_CRYPT_IV_DEFAULT ?
1818 seg->iv_offset : *seg_start);
1819 break;
cac52ca4
JEB
1820 case SEG_RAID1:
1821 case SEG_RAID4:
1822 case SEG_RAID5_LA:
1823 case SEG_RAID5_RA:
1824 case SEG_RAID5_LS:
1825 case SEG_RAID5_RS:
1826 case SEG_RAID6_ZR:
1827 case SEG_RAID6_NR:
1828 case SEG_RAID6_NC:
1829 target_type_is_raid = 1;
1830 r = _raid_emit_segment_line(dmt, major, minor, seg, seg_start,
1831 params, paramsize);
1832 if (!r)
1833 return_0;
1834
1835 break;
165e4a11
AK
1836 }
1837
1838 switch(seg->type) {
1839 case SEG_ERROR:
b262f3e1 1840 case SEG_REPLICATOR:
165e4a11
AK
1841 case SEG_SNAPSHOT:
1842 case SEG_SNAPSHOT_ORIGIN:
aa6f4e51 1843 case SEG_SNAPSHOT_MERGE:
165e4a11
AK
1844 case SEG_ZERO:
1845 break;
12ca060e 1846 case SEG_CRYPT:
165e4a11 1847 case SEG_LINEAR:
b262f3e1 1848 case SEG_REPLICATOR_DEV:
165e4a11
AK
1849 case SEG_STRIPED:
1850 if ((r = _emit_areas_line(dmt, seg, params, paramsize, &pos)) <= 0) {
1851 stack;
1852 return r;
1853 }
b6793963
AK
1854 if (!params[0]) {
1855 log_error("No parameters supplied for %s target "
1856 "%u:%u.", dm_segtypes[seg->type].target,
812e10ac 1857 major, minor);
b6793963
AK
1858 return 0;
1859 }
165e4a11
AK
1860 break;
1861 }
1862
4b2cae46
AK
1863 log_debug("Adding target to (%" PRIu32 ":%" PRIu32 "): %" PRIu64
1864 " %" PRIu64 " %s %s", major, minor,
165e4a11
AK
1865 *seg_start, seg->size, dm_segtypes[seg->type].target, params);
1866
cac52ca4
JEB
1867 if (!dm_task_add_target(dmt, *seg_start, seg->size,
1868 target_type_is_raid ? "raid" :
1869 dm_segtypes[seg->type].target, params))
b4f1578f 1870 return_0;
165e4a11
AK
1871
1872 *seg_start += seg->size;
1873
1874 return 1;
1875}
1876
ffa9b6a5
ZK
1877#undef EMIT_PARAMS
1878
4b2cae46
AK
1879static int _emit_segment(struct dm_task *dmt, uint32_t major, uint32_t minor,
1880 struct load_segment *seg, uint64_t *seg_start)
165e4a11
AK
1881{
1882 char *params;
1883 size_t paramsize = 4096;
1884 int ret;
1885
1886 do {
1887 if (!(params = dm_malloc(paramsize))) {
1888 log_error("Insufficient space for target parameters.");
1889 return 0;
1890 }
1891
12ea7cb1 1892 params[0] = '\0';
4b2cae46
AK
1893 ret = _emit_segment_line(dmt, major, minor, seg, seg_start,
1894 params, paramsize);
165e4a11
AK
1895 dm_free(params);
1896
1897 if (!ret)
1898 stack;
1899
1900 if (ret >= 0)
1901 return ret;
1902
1903 log_debug("Insufficient space in params[%" PRIsize_t
1904 "] for target parameters.", paramsize);
1905
1906 paramsize *= 2;
1907 } while (paramsize < MAX_TARGET_PARAMSIZE);
1908
1909 log_error("Target parameter size too big. Aborting.");
1910 return 0;
1911}
1912
b4f1578f 1913static int _load_node(struct dm_tree_node *dnode)
165e4a11
AK
1914{
1915 int r = 0;
1916 struct dm_task *dmt;
1917 struct load_segment *seg;
df390f17 1918 uint64_t seg_start = 0, existing_table_size;
165e4a11 1919
4b2cae46
AK
1920 log_verbose("Loading %s table (%" PRIu32 ":%" PRIu32 ")", dnode->name,
1921 dnode->info.major, dnode->info.minor);
165e4a11
AK
1922
1923 if (!(dmt = dm_task_create(DM_DEVICE_RELOAD))) {
1924 log_error("Reload dm_task creation failed for %s", dnode->name);
1925 return 0;
1926 }
1927
1928 if (!dm_task_set_major(dmt, dnode->info.major) ||
1929 !dm_task_set_minor(dmt, dnode->info.minor)) {
1930 log_error("Failed to set device number for %s reload.", dnode->name);
1931 goto out;
1932 }
1933
1934 if (dnode->props.read_only && !dm_task_set_ro(dmt)) {
1935 log_error("Failed to set read only flag for %s", dnode->name);
1936 goto out;
1937 }
1938
1939 if (!dm_task_no_open_count(dmt))
1940 log_error("Failed to disable open_count");
1941
2c44337b 1942 dm_list_iterate_items(seg, &dnode->props.segs)
4b2cae46
AK
1943 if (!_emit_segment(dmt, dnode->info.major, dnode->info.minor,
1944 seg, &seg_start))
b4f1578f 1945 goto_out;
165e4a11 1946
ec289b64
AK
1947 if (!dm_task_suppress_identical_reload(dmt))
1948 log_error("Failed to suppress reload of identical tables.");
1949
1950 if ((r = dm_task_run(dmt))) {
165e4a11 1951 r = dm_task_get_info(dmt, &dnode->info);
ec289b64
AK
1952 if (r && !dnode->info.inactive_table)
1953 log_verbose("Suppressed %s identical table reload.",
1954 dnode->name);
bb875bb9 1955
df390f17 1956 existing_table_size = dm_task_get_existing_table_size(dmt);
bb875bb9 1957 if ((dnode->props.size_changed =
df390f17 1958 (existing_table_size == seg_start) ? 0 : 1)) {
bb875bb9 1959 log_debug("Table size changed from %" PRIu64 " to %"
df390f17 1960 PRIu64 " for %s", existing_table_size,
bb875bb9 1961 seg_start, dnode->name);
df390f17
AK
1962 /*
1963 * Kernel usually skips size validation on zero-length devices
1964 * now so no need to preload them.
1965 */
1966 /* FIXME In which kernel version did this begin? */
1967 if (!existing_table_size && dnode->props.delay_resume_if_new)
1968 dnode->props.size_changed = 0;
1969 }
ec289b64 1970 }
165e4a11
AK
1971
1972 dnode->props.segment_count = 0;
1973
1974out:
1975 dm_task_destroy(dmt);
1976
1977 return r;
165e4a11
AK
1978}
1979
b4f1578f 1980int dm_tree_preload_children(struct dm_tree_node *dnode,
bb875bb9
AK
1981 const char *uuid_prefix,
1982 size_t uuid_prefix_len)
165e4a11 1983{
2ca6b865 1984 int r = 1;
165e4a11 1985 void *handle = NULL;
b4f1578f 1986 struct dm_tree_node *child;
165e4a11 1987 struct dm_info newinfo;
566515c0 1988 int update_devs_flag = 0;
165e4a11
AK
1989
1990 /* Preload children first */
b4f1578f 1991 while ((child = dm_tree_next_child(&handle, dnode, 0))) {
165e4a11
AK
1992 /* Skip existing non-device-mapper devices */
1993 if (!child->info.exists && child->info.major)
1994 continue;
1995
1996 /* Ignore if it doesn't belong to this VG */
87f98002
AK
1997 if (child->info.exists &&
1998 !_uuid_prefix_matches(child->uuid, uuid_prefix, uuid_prefix_len))
165e4a11
AK
1999 continue;
2000
b4f1578f 2001 if (dm_tree_node_num_children(child, 0))
2ca6b865
MS
2002 if (!dm_tree_preload_children(child, uuid_prefix, uuid_prefix_len))
2003 return_0;
165e4a11 2004
165e4a11
AK
2005 /* FIXME Cope if name exists with no uuid? */
2006 if (!child->info.exists) {
2007 if (!_create_node(child)) {
2008 stack;
2009 return 0;
2010 }
2011 }
2012
2013 if (!child->info.inactive_table && child->props.segment_count) {
2014 if (!_load_node(child)) {
2015 stack;
2016 return 0;
2017 }
2018 }
2019
eb91c4ee
MB
2020 /* Propagate device size change change */
2021 if (child->props.size_changed)
2022 dnode->props.size_changed = 1;
2023
bb875bb9 2024 /* Resume device immediately if it has parents and its size changed */
3776c494 2025 if (!dm_tree_node_num_children(child, 1) || !child->props.size_changed)
165e4a11
AK
2026 continue;
2027
7707ea90
AK
2028 if (!child->info.inactive_table && !child->info.suspended)
2029 continue;
2030
fc795d87 2031 if (!_resume_node(child->name, child->info.major, child->info.minor,
bd90c6b2 2032 child->props.read_ahead, child->props.read_ahead_flags,
1840aa09
AK
2033 &newinfo, &child->dtree->cookie, child->udev_flags,
2034 child->info.suspended)) {
165e4a11 2035 log_error("Unable to resume %s (%" PRIu32
fc795d87 2036 ":%" PRIu32 ")", child->name, child->info.major,
165e4a11 2037 child->info.minor);
2ca6b865 2038 r = 0;
165e4a11
AK
2039 continue;
2040 }
2041
2042 /* Update cached info */
2043 child->info = newinfo;
566515c0
PR
2044
2045 /*
2046 * Prepare for immediate synchronization with udev and flush all stacked
2047 * dev node operations if requested by immediate_dev_node property. But
2048 * finish processing current level in the tree first.
2049 */
2050 if (child->props.immediate_dev_node)
2051 update_devs_flag = 1;
2052
165e4a11
AK
2053 }
2054
2055 handle = NULL;
2056
566515c0
PR
2057 if (update_devs_flag) {
2058 if (!dm_udev_wait(dm_tree_get_cookie(dnode)))
2059 stack;
2060 dm_tree_set_cookie(dnode, 0);
566515c0
PR
2061 }
2062
2ca6b865 2063 return r;
165e4a11
AK
2064}
2065
165e4a11
AK
2066/*
2067 * Returns 1 if unsure.
2068 */
b4f1578f 2069int dm_tree_children_use_uuid(struct dm_tree_node *dnode,
165e4a11
AK
2070 const char *uuid_prefix,
2071 size_t uuid_prefix_len)
2072{
2073 void *handle = NULL;
b4f1578f 2074 struct dm_tree_node *child = dnode;
165e4a11
AK
2075 const char *uuid;
2076
b4f1578f
AK
2077 while ((child = dm_tree_next_child(&handle, dnode, 0))) {
2078 if (!(uuid = dm_tree_node_get_uuid(child))) {
2079 log_error("Failed to get uuid for dtree node.");
165e4a11
AK
2080 return 1;
2081 }
2082
87f98002 2083 if (_uuid_prefix_matches(uuid, uuid_prefix, uuid_prefix_len))
165e4a11
AK
2084 return 1;
2085
b4f1578f
AK
2086 if (dm_tree_node_num_children(child, 0))
2087 dm_tree_children_use_uuid(child, uuid_prefix, uuid_prefix_len);
165e4a11
AK
2088 }
2089
2090 return 0;
2091}
2092
2093/*
2094 * Target functions
2095 */
b4f1578f 2096static struct load_segment *_add_segment(struct dm_tree_node *dnode, unsigned type, uint64_t size)
165e4a11
AK
2097{
2098 struct load_segment *seg;
2099
b4f1578f
AK
2100 if (!(seg = dm_pool_zalloc(dnode->dtree->mem, sizeof(*seg)))) {
2101 log_error("dtree node segment allocation failed");
165e4a11
AK
2102 return NULL;
2103 }
2104
2105 seg->type = type;
2106 seg->size = size;
2107 seg->area_count = 0;
2c44337b 2108 dm_list_init(&seg->areas);
165e4a11
AK
2109 seg->stripe_size = 0;
2110 seg->persistent = 0;
2111 seg->chunk_size = 0;
2112 seg->cow = NULL;
2113 seg->origin = NULL;
aa6f4e51 2114 seg->merge = NULL;
165e4a11 2115
2c44337b 2116 dm_list_add(&dnode->props.segs, &seg->list);
165e4a11
AK
2117 dnode->props.segment_count++;
2118
2119 return seg;
2120}
2121
b4f1578f 2122int dm_tree_node_add_snapshot_origin_target(struct dm_tree_node *dnode,
165e4a11
AK
2123 uint64_t size,
2124 const char *origin_uuid)
2125{
2126 struct load_segment *seg;
b4f1578f 2127 struct dm_tree_node *origin_node;
165e4a11 2128
b4f1578f
AK
2129 if (!(seg = _add_segment(dnode, SEG_SNAPSHOT_ORIGIN, size)))
2130 return_0;
165e4a11 2131
b4f1578f 2132 if (!(origin_node = dm_tree_find_node_by_uuid(dnode->dtree, origin_uuid))) {
165e4a11
AK
2133 log_error("Couldn't find snapshot origin uuid %s.", origin_uuid);
2134 return 0;
2135 }
2136
2137 seg->origin = origin_node;
b4f1578f
AK
2138 if (!_link_tree_nodes(dnode, origin_node))
2139 return_0;
165e4a11 2140
56c28292
AK
2141 /* Resume snapshot origins after new snapshots */
2142 dnode->activation_priority = 1;
2143
165e4a11
AK
2144 return 1;
2145}
2146
aa6f4e51
MS
2147static int _add_snapshot_target(struct dm_tree_node *node,
2148 uint64_t size,
2149 const char *origin_uuid,
2150 const char *cow_uuid,
2151 const char *merge_uuid,
2152 int persistent,
2153 uint32_t chunk_size)
165e4a11
AK
2154{
2155 struct load_segment *seg;
aa6f4e51
MS
2156 struct dm_tree_node *origin_node, *cow_node, *merge_node;
2157 unsigned seg_type;
2158
2159 seg_type = !merge_uuid ? SEG_SNAPSHOT : SEG_SNAPSHOT_MERGE;
165e4a11 2160
aa6f4e51 2161 if (!(seg = _add_segment(node, seg_type, size)))
b4f1578f 2162 return_0;
165e4a11 2163
b4f1578f 2164 if (!(origin_node = dm_tree_find_node_by_uuid(node->dtree, origin_uuid))) {
165e4a11
AK
2165 log_error("Couldn't find snapshot origin uuid %s.", origin_uuid);
2166 return 0;
2167 }
2168
2169 seg->origin = origin_node;
b4f1578f
AK
2170 if (!_link_tree_nodes(node, origin_node))
2171 return_0;
165e4a11 2172
b4f1578f 2173 if (!(cow_node = dm_tree_find_node_by_uuid(node->dtree, cow_uuid))) {
aa6f4e51 2174 log_error("Couldn't find snapshot COW device uuid %s.", cow_uuid);
165e4a11
AK
2175 return 0;
2176 }
2177
2178 seg->cow = cow_node;
b4f1578f
AK
2179 if (!_link_tree_nodes(node, cow_node))
2180 return_0;
165e4a11
AK
2181
2182 seg->persistent = persistent ? 1 : 0;
2183 seg->chunk_size = chunk_size;
2184
aa6f4e51
MS
2185 if (merge_uuid) {
2186 if (!(merge_node = dm_tree_find_node_by_uuid(node->dtree, merge_uuid))) {
2187 /* not a pure error, merging snapshot may have been deactivated */
2188 log_verbose("Couldn't find merging snapshot uuid %s.", merge_uuid);
2189 } else {
2190 seg->merge = merge_node;
2191 /* must not link merging snapshot, would undermine activation_priority below */
2192 }
2193
2194 /* Resume snapshot-merge (acting origin) after other snapshots */
2195 node->activation_priority = 1;
2196 if (seg->merge) {
2197 /* Resume merging snapshot after snapshot-merge */
2198 seg->merge->activation_priority = 2;
2199 }
2200 }
2201
165e4a11
AK
2202 return 1;
2203}
2204
aa6f4e51
MS
2205
2206int dm_tree_node_add_snapshot_target(struct dm_tree_node *node,
2207 uint64_t size,
2208 const char *origin_uuid,
2209 const char *cow_uuid,
2210 int persistent,
2211 uint32_t chunk_size)
2212{
2213 return _add_snapshot_target(node, size, origin_uuid, cow_uuid,
2214 NULL, persistent, chunk_size);
2215}
2216
2217int dm_tree_node_add_snapshot_merge_target(struct dm_tree_node *node,
2218 uint64_t size,
2219 const char *origin_uuid,
2220 const char *cow_uuid,
2221 const char *merge_uuid,
2222 uint32_t chunk_size)
2223{
2224 return _add_snapshot_target(node, size, origin_uuid, cow_uuid,
2225 merge_uuid, 1, chunk_size);
2226}
2227
b4f1578f 2228int dm_tree_node_add_error_target(struct dm_tree_node *node,
165e4a11
AK
2229 uint64_t size)
2230{
b4f1578f
AK
2231 if (!_add_segment(node, SEG_ERROR, size))
2232 return_0;
165e4a11
AK
2233
2234 return 1;
2235}
2236
b4f1578f 2237int dm_tree_node_add_zero_target(struct dm_tree_node *node,
165e4a11
AK
2238 uint64_t size)
2239{
b4f1578f
AK
2240 if (!_add_segment(node, SEG_ZERO, size))
2241 return_0;
165e4a11
AK
2242
2243 return 1;
2244}
2245
b4f1578f 2246int dm_tree_node_add_linear_target(struct dm_tree_node *node,
165e4a11
AK
2247 uint64_t size)
2248{
b4f1578f
AK
2249 if (!_add_segment(node, SEG_LINEAR, size))
2250 return_0;
165e4a11
AK
2251
2252 return 1;
2253}
2254
b4f1578f 2255int dm_tree_node_add_striped_target(struct dm_tree_node *node,
165e4a11
AK
2256 uint64_t size,
2257 uint32_t stripe_size)
2258{
2259 struct load_segment *seg;
2260
b4f1578f
AK
2261 if (!(seg = _add_segment(node, SEG_STRIPED, size)))
2262 return_0;
165e4a11
AK
2263
2264 seg->stripe_size = stripe_size;
2265
2266 return 1;
2267}
2268
12ca060e
MB
2269int dm_tree_node_add_crypt_target(struct dm_tree_node *node,
2270 uint64_t size,
2271 const char *cipher,
2272 const char *chainmode,
2273 const char *iv,
2274 uint64_t iv_offset,
2275 const char *key)
2276{
2277 struct load_segment *seg;
2278
2279 if (!(seg = _add_segment(node, SEG_CRYPT, size)))
2280 return_0;
2281
2282 seg->cipher = cipher;
2283 seg->chainmode = chainmode;
2284 seg->iv = iv;
2285 seg->iv_offset = iv_offset;
2286 seg->key = key;
2287
2288 return 1;
2289}
2290
b4f1578f 2291int dm_tree_node_add_mirror_target_log(struct dm_tree_node *node,
165e4a11 2292 uint32_t region_size,
08e64ce5 2293 unsigned clustered,
165e4a11 2294 const char *log_uuid,
ce7ed2c0
AK
2295 unsigned area_count,
2296 uint32_t flags)
165e4a11 2297{
908db078 2298 struct dm_tree_node *log_node = NULL;
165e4a11
AK
2299 struct load_segment *seg;
2300
2301 if (!node->props.segment_count) {
b8175c33 2302 log_error(INTERNAL_ERROR "Attempt to add target area to missing segment.");
165e4a11
AK
2303 return 0;
2304 }
2305
2c44337b 2306 seg = dm_list_item(dm_list_last(&node->props.segs), struct load_segment);
165e4a11 2307
24b026e3 2308 if (log_uuid) {
67b25ed4
AK
2309 if (!(seg->uuid = dm_pool_strdup(node->dtree->mem, log_uuid))) {
2310 log_error("log uuid pool_strdup failed");
2311 return 0;
2312 }
df390f17
AK
2313 if ((flags & DM_CORELOG))
2314 /* For pvmove: immediate resume (for size validation) isn't needed. */
2315 node->props.delay_resume_if_new = 1;
2316 else {
9723090c
AK
2317 if (!(log_node = dm_tree_find_node_by_uuid(node->dtree, log_uuid))) {
2318 log_error("Couldn't find mirror log uuid %s.", log_uuid);
2319 return 0;
2320 }
2321
566515c0
PR
2322 if (clustered)
2323 log_node->props.immediate_dev_node = 1;
2324
0a99713e
AK
2325 /* The kernel validates the size of disk logs. */
2326 /* FIXME Propagate to any devices below */
2327 log_node->props.delay_resume_if_new = 0;
2328
9723090c
AK
2329 if (!_link_tree_nodes(node, log_node))
2330 return_0;
2331 }
165e4a11
AK
2332 }
2333
2334 seg->log = log_node;
165e4a11
AK
2335 seg->region_size = region_size;
2336 seg->clustered = clustered;
2337 seg->mirror_area_count = area_count;
dbcb64b8 2338 seg->flags = flags;
165e4a11
AK
2339
2340 return 1;
2341}
2342
b4f1578f 2343int dm_tree_node_add_mirror_target(struct dm_tree_node *node,
165e4a11
AK
2344 uint64_t size)
2345{
cbecd3cd 2346 if (!_add_segment(node, SEG_MIRRORED, size))
b4f1578f 2347 return_0;
165e4a11
AK
2348
2349 return 1;
2350}
2351
cac52ca4
JEB
2352int dm_tree_node_add_raid_target(struct dm_tree_node *node,
2353 uint64_t size,
2354 const char *raid_type,
2355 uint32_t region_size,
2356 uint32_t stripe_size,
2357 uint64_t reserved1,
2358 uint64_t reserved2)
2359{
2360 int i;
2361 struct load_segment *seg = NULL;
2362
2363 for (i = 0; dm_segtypes[i].target && !seg; i++)
2364 if (!strcmp(raid_type, dm_segtypes[i].target))
2365 if (!(seg = _add_segment(node,
2366 dm_segtypes[i].type, size)))
2367 return_0;
2368
b2fa9b43
JEB
2369 if (!seg)
2370 return_0;
2371
cac52ca4
JEB
2372 seg->region_size = region_size;
2373 seg->stripe_size = stripe_size;
2374 seg->area_count = 0;
2375
2376 return 1;
2377}
2378
b262f3e1
ZK
2379int dm_tree_node_add_replicator_target(struct dm_tree_node *node,
2380 uint64_t size,
2381 const char *rlog_uuid,
2382 const char *rlog_type,
2383 unsigned rsite_index,
2384 dm_replicator_mode_t mode,
2385 uint32_t async_timeout,
2386 uint64_t fall_behind_data,
2387 uint32_t fall_behind_ios)
2388{
2389 struct load_segment *rseg;
2390 struct replicator_site *rsite;
2391
2392 /* Local site0 - adds replicator segment and links rlog device */
2393 if (rsite_index == REPLICATOR_LOCAL_SITE) {
2394 if (node->props.segment_count) {
2395 log_error(INTERNAL_ERROR "Attempt to add replicator segment to already used node.");
2396 return 0;
2397 }
2398
2399 if (!(rseg = _add_segment(node, SEG_REPLICATOR, size)))
2400 return_0;
2401
2402 if (!(rseg->log = dm_tree_find_node_by_uuid(node->dtree, rlog_uuid))) {
2403 log_error("Missing replicator log uuid %s.", rlog_uuid);
2404 return 0;
2405 }
2406
2407 if (!_link_tree_nodes(node, rseg->log))
2408 return_0;
2409
2410 if (strcmp(rlog_type, "ringbuffer") != 0) {
2411 log_error("Unsupported replicator log type %s.", rlog_type);
2412 return 0;
2413 }
2414
2415 if (!(rseg->rlog_type = dm_pool_strdup(node->dtree->mem, rlog_type)))
2416 return_0;
2417
2418 dm_list_init(&rseg->rsites);
2419 rseg->rdevice_count = 0;
2420 node->activation_priority = 1;
2421 }
2422
2423 /* Add site to segment */
2424 if (mode == DM_REPLICATOR_SYNC
2425 && (async_timeout || fall_behind_ios || fall_behind_data)) {
2426 log_error("Async parameters passed for synchronnous replicator.");
2427 return 0;
2428 }
2429
2430 if (node->props.segment_count != 1) {
2431 log_error(INTERNAL_ERROR "Attempt to add remote site area before setting replicator log.");
2432 return 0;
2433 }
2434
2435 rseg = dm_list_item(dm_list_last(&node->props.segs), struct load_segment);
2436 if (rseg->type != SEG_REPLICATOR) {
2437 log_error(INTERNAL_ERROR "Attempt to use non replicator segment %s.",
2438 dm_segtypes[rseg->type].target);
2439 return 0;
2440 }
2441
2442 if (!(rsite = dm_pool_zalloc(node->dtree->mem, sizeof(*rsite)))) {
2443 log_error("Failed to allocate remote site segment.");
2444 return 0;
2445 }
2446
2447 dm_list_add(&rseg->rsites, &rsite->list);
2448 rseg->rsite_count++;
2449
2450 rsite->mode = mode;
2451 rsite->async_timeout = async_timeout;
2452 rsite->fall_behind_data = fall_behind_data;
2453 rsite->fall_behind_ios = fall_behind_ios;
2454 rsite->rsite_index = rsite_index;
2455
2456 return 1;
2457}
2458
2459/* Appends device node to Replicator */
2460int dm_tree_node_add_replicator_dev_target(struct dm_tree_node *node,
2461 uint64_t size,
2462 const char *replicator_uuid,
2463 uint64_t rdevice_index,
2464 const char *rdev_uuid,
2465 unsigned rsite_index,
2466 const char *slog_uuid,
2467 uint32_t slog_flags,
2468 uint32_t slog_region_size)
2469{
2470 struct seg_area *area;
2471 struct load_segment *rseg;
2472 struct load_segment *rep_seg;
2473
2474 if (rsite_index == REPLICATOR_LOCAL_SITE) {
2475 /* Site index for local target */
2476 if (!(rseg = _add_segment(node, SEG_REPLICATOR_DEV, size)))
2477 return_0;
2478
2479 if (!(rseg->replicator = dm_tree_find_node_by_uuid(node->dtree, replicator_uuid))) {
2480 log_error("Missing replicator uuid %s.", replicator_uuid);
2481 return 0;
2482 }
2483
2484 /* Local slink0 for replicator must be always initialized first */
2485 if (rseg->replicator->props.segment_count != 1) {
2486 log_error(INTERNAL_ERROR "Attempt to use non replicator segment.");
2487 return 0;
2488 }
2489
2490 rep_seg = dm_list_item(dm_list_last(&rseg->replicator->props.segs), struct load_segment);
2491 if (rep_seg->type != SEG_REPLICATOR) {
2492 log_error(INTERNAL_ERROR "Attempt to use non replicator segment %s.",
2493 dm_segtypes[rep_seg->type].target);
2494 return 0;
2495 }
2496 rep_seg->rdevice_count++;
2497
2498 if (!_link_tree_nodes(node, rseg->replicator))
2499 return_0;
2500
2501 rseg->rdevice_index = rdevice_index;
2502 } else {
2503 /* Local slink0 for replicator must be always initialized first */
2504 if (node->props.segment_count != 1) {
2505 log_error(INTERNAL_ERROR "Attempt to use non replicator-dev segment.");
2506 return 0;
2507 }
2508
2509 rseg = dm_list_item(dm_list_last(&node->props.segs), struct load_segment);
2510 if (rseg->type != SEG_REPLICATOR_DEV) {
2511 log_error(INTERNAL_ERROR "Attempt to use non replicator-dev segment %s.",
2512 dm_segtypes[rseg->type].target);
2513 return 0;
2514 }
2515 }
2516
2517 if (!(slog_flags & DM_CORELOG) && !slog_uuid) {
2518 log_error("Unspecified sync log uuid.");
2519 return 0;
2520 }
2521
2522 if (!dm_tree_node_add_target_area(node, NULL, rdev_uuid, 0))
2523 return_0;
2524
2525 area = dm_list_item(dm_list_last(&rseg->areas), struct seg_area);
2526
2527 if (!(slog_flags & DM_CORELOG)) {
2528 if (!(area->slog = dm_tree_find_node_by_uuid(node->dtree, slog_uuid))) {
2529 log_error("Couldn't find sync log uuid %s.", slog_uuid);
2530 return 0;
2531 }
2532
2533 if (!_link_tree_nodes(node, area->slog))
2534 return_0;
2535 }
2536
2537 area->flags = slog_flags;
2538 area->region_size = slog_region_size;
2539 area->rsite_index = rsite_index;
2540
2541 return 1;
2542}
2543
b4f1578f 2544static int _add_area(struct dm_tree_node *node, struct load_segment *seg, struct dm_tree_node *dev_node, uint64_t offset)
165e4a11
AK
2545{
2546 struct seg_area *area;
2547
b4f1578f 2548 if (!(area = dm_pool_zalloc(node->dtree->mem, sizeof (*area)))) {
165e4a11
AK
2549 log_error("Failed to allocate target segment area.");
2550 return 0;
2551 }
2552
2553 area->dev_node = dev_node;
2554 area->offset = offset;
2555
2c44337b 2556 dm_list_add(&seg->areas, &area->list);
165e4a11
AK
2557 seg->area_count++;
2558
2559 return 1;
2560}
2561
b4f1578f 2562int dm_tree_node_add_target_area(struct dm_tree_node *node,
165e4a11
AK
2563 const char *dev_name,
2564 const char *uuid,
2565 uint64_t offset)
2566{
2567 struct load_segment *seg;
2568 struct stat info;
b4f1578f 2569 struct dm_tree_node *dev_node;
165e4a11
AK
2570
2571 if ((!dev_name || !*dev_name) && (!uuid || !*uuid)) {
b4f1578f 2572 log_error("dm_tree_node_add_target_area called without device");
165e4a11
AK
2573 return 0;
2574 }
2575
2576 if (uuid) {
b4f1578f 2577 if (!(dev_node = dm_tree_find_node_by_uuid(node->dtree, uuid))) {
165e4a11
AK
2578 log_error("Couldn't find area uuid %s.", uuid);
2579 return 0;
2580 }
b4f1578f
AK
2581 if (!_link_tree_nodes(node, dev_node))
2582 return_0;
165e4a11 2583 } else {
6d04311e 2584 if (stat(dev_name, &info) < 0) {
165e4a11
AK
2585 log_error("Device %s not found.", dev_name);
2586 return 0;
2587 }
2588
2589 if (!S_ISBLK(info.st_mode)) {
2590 log_error("Device %s is not a block device.", dev_name);
2591 return 0;
2592 }
2593
2594 /* FIXME Check correct macro use */
cda69e17
PR
2595 if (!(dev_node = _add_dev(node->dtree, node, MAJOR(info.st_rdev),
2596 MINOR(info.st_rdev), 0)))
b4f1578f 2597 return_0;
165e4a11
AK
2598 }
2599
2600 if (!node->props.segment_count) {
b8175c33 2601 log_error(INTERNAL_ERROR "Attempt to add target area to missing segment.");
165e4a11
AK
2602 return 0;
2603 }
2604
2c44337b 2605 seg = dm_list_item(dm_list_last(&node->props.segs), struct load_segment);
165e4a11 2606
b4f1578f
AK
2607 if (!_add_area(node, seg, dev_node, offset))
2608 return_0;
165e4a11
AK
2609
2610 return 1;
db208f51 2611}
bd90c6b2 2612
6d04311e
JEB
2613int dm_tree_node_add_null_area(struct dm_tree_node *node, uint64_t offset)
2614{
2615 struct load_segment *seg;
2616
2617 seg = dm_list_item(dm_list_last(&node->props.segs), struct load_segment);
2618
2619 if (!_add_area(node, seg, NULL, offset))
2620 return_0;
2621
2622 return 1;
2623}
2624
bd90c6b2
AK
2625void dm_tree_set_cookie(struct dm_tree_node *node, uint32_t cookie)
2626{
2627 node->dtree->cookie = cookie;
2628}
2629
2630uint32_t dm_tree_get_cookie(struct dm_tree_node *node)
2631{
2632 return node->dtree->cookie;
2633}
This page took 0.39091 seconds and 5 git commands to generate.