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