]> sourceware.org Git - lvm2.git/blame - libdm/libdm-deptree.c
Avoid selecting names under /dev/block if there is an alternative.
[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;
165e4a11 1543
8f26e18c
JEB
1544 r = uname(&uts);
1545 if (r)
1546 return_0;
67b25ed4 1547
8f26e18c
JEB
1548 if ((seg->flags & DM_BLOCK_ON_ERROR)) {
1549 /*
1550 * Originally, block_on_error was an argument to the log
1551 * portion of the mirror CTR table. It was renamed to
1552 * "handle_errors" and now resides in the 'features'
1553 * section of the mirror CTR table (i.e. at the end).
1554 *
1555 * We can identify whether to use "block_on_error" or
1556 * "handle_errors" by the dm-mirror module's version
1557 * number (>= 1.12) or by the kernel version (>= 2.6.22).
1558 */
1559 if (strncmp(uts.release, "2.6.22", 6) >= 0)
1560 handle_errors = 1;
1561 else
1562 block_on_error = 1;
1563 }
1564
1565 if (seg->clustered) {
1566 /* Cluster mirrors require a UUID */
1567 if (!seg->uuid)
1568 return_0;
1569
1570 /*
1571 * Cluster mirrors used to have their own log
1572 * types. Now they are accessed through the
1573 * userspace log type.
1574 *
1575 * The dm-log-userspace module was added to the
1576 * 2.6.31 kernel.
1577 */
5782a385 1578 /* FIXME Replace the broken string comparison! */
8f26e18c
JEB
1579 if (strncmp(uts.release, "2.6.31", 6) >= 0)
1580 dm_log_userspace = 1;
1581 }
1582
1583 /* Region size */
1584 log_parm_count = 1;
1585
1586 /* [no]sync, block_on_error etc. */
1587 log_parm_count += hweight32(seg->flags);
311d6d81 1588
8f26e18c
JEB
1589 /* "handle_errors" is a feature arg now */
1590 if (handle_errors)
1591 log_parm_count--;
1592
1593 /* DM_CORELOG does not count in the param list */
1594 if (seg->flags & DM_CORELOG)
1595 log_parm_count--;
1596
1597 if (seg->clustered) {
1598 log_parm_count++; /* For UUID */
1599
1600 if (!dm_log_userspace)
ffa9b6a5 1601 EMIT_PARAMS(pos, "clustered-");
49b95a5e
JEB
1602 else
1603 /* For clustered-* type field inserted later */
1604 log_parm_count++;
8f26e18c 1605 }
dbcb64b8 1606
8f26e18c
JEB
1607 if (!seg->log)
1608 logtype = "core";
1609 else {
1610 logtype = "disk";
1611 log_parm_count++;
1612 if (!_build_dev_string(logbuf, sizeof(logbuf), seg->log))
1613 return_0;
1614 }
dbcb64b8 1615
8f26e18c
JEB
1616 if (dm_log_userspace)
1617 EMIT_PARAMS(pos, "userspace %u %s clustered-%s",
1618 log_parm_count, seg->uuid, logtype);
1619 else
ffa9b6a5 1620 EMIT_PARAMS(pos, "%s %u", logtype, log_parm_count);
dbcb64b8 1621
8f26e18c
JEB
1622 if (seg->log)
1623 EMIT_PARAMS(pos, " %s", logbuf);
1624
1625 EMIT_PARAMS(pos, " %u", seg->region_size);
dbcb64b8 1626
8f26e18c
JEB
1627 if (seg->clustered && !dm_log_userspace)
1628 EMIT_PARAMS(pos, " %s", seg->uuid);
67b25ed4 1629
8f26e18c
JEB
1630 if ((seg->flags & DM_NOSYNC))
1631 EMIT_PARAMS(pos, " nosync");
1632 else if ((seg->flags & DM_FORCESYNC))
1633 EMIT_PARAMS(pos, " sync");
dbcb64b8 1634
8f26e18c
JEB
1635 if (block_on_error)
1636 EMIT_PARAMS(pos, " block_on_error");
1637
1638 EMIT_PARAMS(pos, " %u ", seg->mirror_area_count);
1639
3c74075f
JEB
1640 if ((r = _emit_areas_line(dmt, seg, params, paramsize, &pos)) <= 0)
1641 return_0;
dbcb64b8 1642
8f26e18c
JEB
1643 if (handle_errors)
1644 EMIT_PARAMS(pos, " 1 handle_errors");
ffa9b6a5 1645
3c74075f 1646 return 1;
8f26e18c
JEB
1647}
1648
1649static int _emit_segment_line(struct dm_task *dmt, uint32_t major,
1650 uint32_t minor, struct load_segment *seg,
1651 uint64_t *seg_start, char *params,
1652 size_t paramsize)
1653{
1654 int pos = 0;
1655 int r;
1656 char originbuf[DM_FORMAT_DEV_BUFSIZE], cowbuf[DM_FORMAT_DEV_BUFSIZE];
dbcb64b8 1657
8f26e18c
JEB
1658 switch(seg->type) {
1659 case SEG_ERROR:
1660 case SEG_ZERO:
1661 case SEG_LINEAR:
1662 break;
1663 case SEG_MIRRORED:
1664 /* Mirrors are pretty complicated - now in separate function */
536f0e08
AK
1665 r = _mirror_emit_segment_line(dmt, major, minor, seg, seg_start,
1666 params, paramsize);
3c74075f
JEB
1667 if (!r)
1668 return_0;
165e4a11 1669 break;
b262f3e1
ZK
1670 case SEG_REPLICATOR:
1671 if ((r = _replicator_emit_segment_line(seg, params, paramsize,
1672 &pos)) <= 0) {
1673 stack;
1674 return r;
1675 }
1676 break;
1677 case SEG_REPLICATOR_DEV:
1678 if (!seg->replicator || !_build_dev_string(originbuf,
1679 sizeof(originbuf),
1680 seg->replicator))
1681 return_0;
1682
1683 EMIT_PARAMS(pos, "%s %" PRIu64, originbuf, seg->rdevice_index);
1684 break;
165e4a11 1685 case SEG_SNAPSHOT:
aa6f4e51 1686 case SEG_SNAPSHOT_MERGE:
b4f1578f
AK
1687 if (!_build_dev_string(originbuf, sizeof(originbuf), seg->origin))
1688 return_0;
1689 if (!_build_dev_string(cowbuf, sizeof(cowbuf), seg->cow))
1690 return_0;
ffa9b6a5
ZK
1691 EMIT_PARAMS(pos, "%s %s %c %d", originbuf, cowbuf,
1692 seg->persistent ? 'P' : 'N', seg->chunk_size);
165e4a11
AK
1693 break;
1694 case SEG_SNAPSHOT_ORIGIN:
b4f1578f
AK
1695 if (!_build_dev_string(originbuf, sizeof(originbuf), seg->origin))
1696 return_0;
ffa9b6a5 1697 EMIT_PARAMS(pos, "%s", originbuf);
165e4a11
AK
1698 break;
1699 case SEG_STRIPED:
609faae9 1700 EMIT_PARAMS(pos, "%u %u ", seg->area_count, seg->stripe_size);
165e4a11 1701 break;
12ca060e 1702 case SEG_CRYPT:
609faae9 1703 EMIT_PARAMS(pos, "%s%s%s%s%s %s %" PRIu64 " ", seg->cipher,
12ca060e
MB
1704 seg->chainmode ? "-" : "", seg->chainmode ?: "",
1705 seg->iv ? "-" : "", seg->iv ?: "", seg->key,
1706 seg->iv_offset != DM_CRYPT_IV_DEFAULT ?
1707 seg->iv_offset : *seg_start);
1708 break;
165e4a11
AK
1709 }
1710
1711 switch(seg->type) {
1712 case SEG_ERROR:
b262f3e1 1713 case SEG_REPLICATOR:
165e4a11
AK
1714 case SEG_SNAPSHOT:
1715 case SEG_SNAPSHOT_ORIGIN:
aa6f4e51 1716 case SEG_SNAPSHOT_MERGE:
165e4a11
AK
1717 case SEG_ZERO:
1718 break;
12ca060e 1719 case SEG_CRYPT:
165e4a11 1720 case SEG_LINEAR:
b262f3e1 1721 case SEG_REPLICATOR_DEV:
165e4a11
AK
1722 case SEG_STRIPED:
1723 if ((r = _emit_areas_line(dmt, seg, params, paramsize, &pos)) <= 0) {
1724 stack;
1725 return r;
1726 }
1727 break;
1728 }
1729
4b2cae46
AK
1730 log_debug("Adding target to (%" PRIu32 ":%" PRIu32 "): %" PRIu64
1731 " %" PRIu64 " %s %s", major, minor,
165e4a11
AK
1732 *seg_start, seg->size, dm_segtypes[seg->type].target, params);
1733
b4f1578f
AK
1734 if (!dm_task_add_target(dmt, *seg_start, seg->size, dm_segtypes[seg->type].target, params))
1735 return_0;
165e4a11
AK
1736
1737 *seg_start += seg->size;
1738
1739 return 1;
1740}
1741
ffa9b6a5
ZK
1742#undef EMIT_PARAMS
1743
4b2cae46
AK
1744static int _emit_segment(struct dm_task *dmt, uint32_t major, uint32_t minor,
1745 struct load_segment *seg, uint64_t *seg_start)
165e4a11
AK
1746{
1747 char *params;
1748 size_t paramsize = 4096;
1749 int ret;
1750
1751 do {
1752 if (!(params = dm_malloc(paramsize))) {
1753 log_error("Insufficient space for target parameters.");
1754 return 0;
1755 }
1756
12ea7cb1 1757 params[0] = '\0';
4b2cae46
AK
1758 ret = _emit_segment_line(dmt, major, minor, seg, seg_start,
1759 params, paramsize);
165e4a11
AK
1760 dm_free(params);
1761
1762 if (!ret)
1763 stack;
1764
1765 if (ret >= 0)
1766 return ret;
1767
1768 log_debug("Insufficient space in params[%" PRIsize_t
1769 "] for target parameters.", paramsize);
1770
1771 paramsize *= 2;
1772 } while (paramsize < MAX_TARGET_PARAMSIZE);
1773
1774 log_error("Target parameter size too big. Aborting.");
1775 return 0;
1776}
1777
b4f1578f 1778static int _load_node(struct dm_tree_node *dnode)
165e4a11
AK
1779{
1780 int r = 0;
1781 struct dm_task *dmt;
1782 struct load_segment *seg;
1783 uint64_t seg_start = 0;
1784
4b2cae46
AK
1785 log_verbose("Loading %s table (%" PRIu32 ":%" PRIu32 ")", dnode->name,
1786 dnode->info.major, dnode->info.minor);
165e4a11
AK
1787
1788 if (!(dmt = dm_task_create(DM_DEVICE_RELOAD))) {
1789 log_error("Reload dm_task creation failed for %s", dnode->name);
1790 return 0;
1791 }
1792
1793 if (!dm_task_set_major(dmt, dnode->info.major) ||
1794 !dm_task_set_minor(dmt, dnode->info.minor)) {
1795 log_error("Failed to set device number for %s reload.", dnode->name);
1796 goto out;
1797 }
1798
1799 if (dnode->props.read_only && !dm_task_set_ro(dmt)) {
1800 log_error("Failed to set read only flag for %s", dnode->name);
1801 goto out;
1802 }
1803
1804 if (!dm_task_no_open_count(dmt))
1805 log_error("Failed to disable open_count");
1806
2c44337b 1807 dm_list_iterate_items(seg, &dnode->props.segs)
4b2cae46
AK
1808 if (!_emit_segment(dmt, dnode->info.major, dnode->info.minor,
1809 seg, &seg_start))
b4f1578f 1810 goto_out;
165e4a11 1811
ec289b64
AK
1812 if (!dm_task_suppress_identical_reload(dmt))
1813 log_error("Failed to suppress reload of identical tables.");
1814
1815 if ((r = dm_task_run(dmt))) {
165e4a11 1816 r = dm_task_get_info(dmt, &dnode->info);
ec289b64
AK
1817 if (r && !dnode->info.inactive_table)
1818 log_verbose("Suppressed %s identical table reload.",
1819 dnode->name);
bb875bb9
AK
1820
1821 if ((dnode->props.size_changed =
1822 (dm_task_get_existing_table_size(dmt) == seg_start) ? 0 : 1))
1823 log_debug("Table size changed from %" PRIu64 " to %"
1824 PRIu64 " for %s",
1825 dm_task_get_existing_table_size(dmt),
1826 seg_start, dnode->name);
ec289b64 1827 }
165e4a11
AK
1828
1829 dnode->props.segment_count = 0;
1830
1831out:
1832 dm_task_destroy(dmt);
1833
1834 return r;
165e4a11
AK
1835}
1836
b4f1578f 1837int dm_tree_preload_children(struct dm_tree_node *dnode,
bb875bb9
AK
1838 const char *uuid_prefix,
1839 size_t uuid_prefix_len)
165e4a11 1840{
2ca6b865 1841 int r = 1;
165e4a11 1842 void *handle = NULL;
b4f1578f 1843 struct dm_tree_node *child;
165e4a11 1844 struct dm_info newinfo;
165e4a11
AK
1845
1846 /* Preload children first */
b4f1578f 1847 while ((child = dm_tree_next_child(&handle, dnode, 0))) {
165e4a11
AK
1848 /* Skip existing non-device-mapper devices */
1849 if (!child->info.exists && child->info.major)
1850 continue;
1851
1852 /* Ignore if it doesn't belong to this VG */
87f98002
AK
1853 if (child->info.exists &&
1854 !_uuid_prefix_matches(child->uuid, uuid_prefix, uuid_prefix_len))
165e4a11
AK
1855 continue;
1856
b4f1578f 1857 if (dm_tree_node_num_children(child, 0))
2ca6b865
MS
1858 if (!dm_tree_preload_children(child, uuid_prefix, uuid_prefix_len))
1859 return_0;
165e4a11 1860
165e4a11
AK
1861 /* FIXME Cope if name exists with no uuid? */
1862 if (!child->info.exists) {
1863 if (!_create_node(child)) {
1864 stack;
1865 return 0;
1866 }
1867 }
1868
1869 if (!child->info.inactive_table && child->props.segment_count) {
1870 if (!_load_node(child)) {
1871 stack;
1872 return 0;
1873 }
1874 }
1875
eb91c4ee
MB
1876 /* Propagate device size change change */
1877 if (child->props.size_changed)
1878 dnode->props.size_changed = 1;
1879
bb875bb9 1880 /* Resume device immediately if it has parents and its size changed */
3776c494 1881 if (!dm_tree_node_num_children(child, 1) || !child->props.size_changed)
165e4a11
AK
1882 continue;
1883
7707ea90
AK
1884 if (!child->info.inactive_table && !child->info.suspended)
1885 continue;
1886
fc795d87 1887 if (!_resume_node(child->name, child->info.major, child->info.minor,
bd90c6b2 1888 child->props.read_ahead, child->props.read_ahead_flags,
f16aea9e 1889 &newinfo, &child->dtree->cookie, child->udev_flags)) {
165e4a11 1890 log_error("Unable to resume %s (%" PRIu32
fc795d87 1891 ":%" PRIu32 ")", child->name, child->info.major,
165e4a11 1892 child->info.minor);
2ca6b865 1893 r = 0;
165e4a11
AK
1894 continue;
1895 }
1896
1897 /* Update cached info */
1898 child->info = newinfo;
1899 }
1900
1901 handle = NULL;
1902
2ca6b865 1903 return r;
165e4a11
AK
1904}
1905
165e4a11
AK
1906/*
1907 * Returns 1 if unsure.
1908 */
b4f1578f 1909int dm_tree_children_use_uuid(struct dm_tree_node *dnode,
165e4a11
AK
1910 const char *uuid_prefix,
1911 size_t uuid_prefix_len)
1912{
1913 void *handle = NULL;
b4f1578f 1914 struct dm_tree_node *child = dnode;
165e4a11
AK
1915 const char *uuid;
1916
b4f1578f
AK
1917 while ((child = dm_tree_next_child(&handle, dnode, 0))) {
1918 if (!(uuid = dm_tree_node_get_uuid(child))) {
1919 log_error("Failed to get uuid for dtree node.");
165e4a11
AK
1920 return 1;
1921 }
1922
87f98002 1923 if (_uuid_prefix_matches(uuid, uuid_prefix, uuid_prefix_len))
165e4a11
AK
1924 return 1;
1925
b4f1578f
AK
1926 if (dm_tree_node_num_children(child, 0))
1927 dm_tree_children_use_uuid(child, uuid_prefix, uuid_prefix_len);
165e4a11
AK
1928 }
1929
1930 return 0;
1931}
1932
1933/*
1934 * Target functions
1935 */
b4f1578f 1936static struct load_segment *_add_segment(struct dm_tree_node *dnode, unsigned type, uint64_t size)
165e4a11
AK
1937{
1938 struct load_segment *seg;
1939
b4f1578f
AK
1940 if (!(seg = dm_pool_zalloc(dnode->dtree->mem, sizeof(*seg)))) {
1941 log_error("dtree node segment allocation failed");
165e4a11
AK
1942 return NULL;
1943 }
1944
1945 seg->type = type;
1946 seg->size = size;
1947 seg->area_count = 0;
2c44337b 1948 dm_list_init(&seg->areas);
165e4a11
AK
1949 seg->stripe_size = 0;
1950 seg->persistent = 0;
1951 seg->chunk_size = 0;
1952 seg->cow = NULL;
1953 seg->origin = NULL;
aa6f4e51 1954 seg->merge = NULL;
165e4a11 1955
2c44337b 1956 dm_list_add(&dnode->props.segs, &seg->list);
165e4a11
AK
1957 dnode->props.segment_count++;
1958
1959 return seg;
1960}
1961
b4f1578f 1962int dm_tree_node_add_snapshot_origin_target(struct dm_tree_node *dnode,
165e4a11
AK
1963 uint64_t size,
1964 const char *origin_uuid)
1965{
1966 struct load_segment *seg;
b4f1578f 1967 struct dm_tree_node *origin_node;
165e4a11 1968
b4f1578f
AK
1969 if (!(seg = _add_segment(dnode, SEG_SNAPSHOT_ORIGIN, size)))
1970 return_0;
165e4a11 1971
b4f1578f 1972 if (!(origin_node = dm_tree_find_node_by_uuid(dnode->dtree, origin_uuid))) {
165e4a11
AK
1973 log_error("Couldn't find snapshot origin uuid %s.", origin_uuid);
1974 return 0;
1975 }
1976
1977 seg->origin = origin_node;
b4f1578f
AK
1978 if (!_link_tree_nodes(dnode, origin_node))
1979 return_0;
165e4a11 1980
56c28292
AK
1981 /* Resume snapshot origins after new snapshots */
1982 dnode->activation_priority = 1;
1983
165e4a11
AK
1984 return 1;
1985}
1986
aa6f4e51
MS
1987static int _add_snapshot_target(struct dm_tree_node *node,
1988 uint64_t size,
1989 const char *origin_uuid,
1990 const char *cow_uuid,
1991 const char *merge_uuid,
1992 int persistent,
1993 uint32_t chunk_size)
165e4a11
AK
1994{
1995 struct load_segment *seg;
aa6f4e51
MS
1996 struct dm_tree_node *origin_node, *cow_node, *merge_node;
1997 unsigned seg_type;
1998
1999 seg_type = !merge_uuid ? SEG_SNAPSHOT : SEG_SNAPSHOT_MERGE;
165e4a11 2000
aa6f4e51 2001 if (!(seg = _add_segment(node, seg_type, size)))
b4f1578f 2002 return_0;
165e4a11 2003
b4f1578f 2004 if (!(origin_node = dm_tree_find_node_by_uuid(node->dtree, origin_uuid))) {
165e4a11
AK
2005 log_error("Couldn't find snapshot origin uuid %s.", origin_uuid);
2006 return 0;
2007 }
2008
2009 seg->origin = origin_node;
b4f1578f
AK
2010 if (!_link_tree_nodes(node, origin_node))
2011 return_0;
165e4a11 2012
b4f1578f 2013 if (!(cow_node = dm_tree_find_node_by_uuid(node->dtree, cow_uuid))) {
aa6f4e51 2014 log_error("Couldn't find snapshot COW device uuid %s.", cow_uuid);
165e4a11
AK
2015 return 0;
2016 }
2017
2018 seg->cow = cow_node;
b4f1578f
AK
2019 if (!_link_tree_nodes(node, cow_node))
2020 return_0;
165e4a11
AK
2021
2022 seg->persistent = persistent ? 1 : 0;
2023 seg->chunk_size = chunk_size;
2024
aa6f4e51
MS
2025 if (merge_uuid) {
2026 if (!(merge_node = dm_tree_find_node_by_uuid(node->dtree, merge_uuid))) {
2027 /* not a pure error, merging snapshot may have been deactivated */
2028 log_verbose("Couldn't find merging snapshot uuid %s.", merge_uuid);
2029 } else {
2030 seg->merge = merge_node;
2031 /* must not link merging snapshot, would undermine activation_priority below */
2032 }
2033
2034 /* Resume snapshot-merge (acting origin) after other snapshots */
2035 node->activation_priority = 1;
2036 if (seg->merge) {
2037 /* Resume merging snapshot after snapshot-merge */
2038 seg->merge->activation_priority = 2;
2039 }
2040 }
2041
165e4a11
AK
2042 return 1;
2043}
2044
aa6f4e51
MS
2045
2046int dm_tree_node_add_snapshot_target(struct dm_tree_node *node,
2047 uint64_t size,
2048 const char *origin_uuid,
2049 const char *cow_uuid,
2050 int persistent,
2051 uint32_t chunk_size)
2052{
2053 return _add_snapshot_target(node, size, origin_uuid, cow_uuid,
2054 NULL, persistent, chunk_size);
2055}
2056
2057int dm_tree_node_add_snapshot_merge_target(struct dm_tree_node *node,
2058 uint64_t size,
2059 const char *origin_uuid,
2060 const char *cow_uuid,
2061 const char *merge_uuid,
2062 uint32_t chunk_size)
2063{
2064 return _add_snapshot_target(node, size, origin_uuid, cow_uuid,
2065 merge_uuid, 1, chunk_size);
2066}
2067
b4f1578f 2068int dm_tree_node_add_error_target(struct dm_tree_node *node,
165e4a11
AK
2069 uint64_t size)
2070{
b4f1578f
AK
2071 if (!_add_segment(node, SEG_ERROR, size))
2072 return_0;
165e4a11
AK
2073
2074 return 1;
2075}
2076
b4f1578f 2077int dm_tree_node_add_zero_target(struct dm_tree_node *node,
165e4a11
AK
2078 uint64_t size)
2079{
b4f1578f
AK
2080 if (!_add_segment(node, SEG_ZERO, size))
2081 return_0;
165e4a11
AK
2082
2083 return 1;
2084}
2085
b4f1578f 2086int dm_tree_node_add_linear_target(struct dm_tree_node *node,
165e4a11
AK
2087 uint64_t size)
2088{
b4f1578f
AK
2089 if (!_add_segment(node, SEG_LINEAR, size))
2090 return_0;
165e4a11
AK
2091
2092 return 1;
2093}
2094
b4f1578f 2095int dm_tree_node_add_striped_target(struct dm_tree_node *node,
165e4a11
AK
2096 uint64_t size,
2097 uint32_t stripe_size)
2098{
2099 struct load_segment *seg;
2100
b4f1578f
AK
2101 if (!(seg = _add_segment(node, SEG_STRIPED, size)))
2102 return_0;
165e4a11
AK
2103
2104 seg->stripe_size = stripe_size;
2105
2106 return 1;
2107}
2108
12ca060e
MB
2109int dm_tree_node_add_crypt_target(struct dm_tree_node *node,
2110 uint64_t size,
2111 const char *cipher,
2112 const char *chainmode,
2113 const char *iv,
2114 uint64_t iv_offset,
2115 const char *key)
2116{
2117 struct load_segment *seg;
2118
2119 if (!(seg = _add_segment(node, SEG_CRYPT, size)))
2120 return_0;
2121
2122 seg->cipher = cipher;
2123 seg->chainmode = chainmode;
2124 seg->iv = iv;
2125 seg->iv_offset = iv_offset;
2126 seg->key = key;
2127
2128 return 1;
2129}
2130
b4f1578f 2131int dm_tree_node_add_mirror_target_log(struct dm_tree_node *node,
165e4a11 2132 uint32_t region_size,
08e64ce5 2133 unsigned clustered,
165e4a11 2134 const char *log_uuid,
ce7ed2c0
AK
2135 unsigned area_count,
2136 uint32_t flags)
165e4a11 2137{
908db078 2138 struct dm_tree_node *log_node = NULL;
165e4a11
AK
2139 struct load_segment *seg;
2140
2141 if (!node->props.segment_count) {
b8175c33 2142 log_error(INTERNAL_ERROR "Attempt to add target area to missing segment.");
165e4a11
AK
2143 return 0;
2144 }
2145
2c44337b 2146 seg = dm_list_item(dm_list_last(&node->props.segs), struct load_segment);
165e4a11 2147
24b026e3 2148 if (log_uuid) {
67b25ed4
AK
2149 if (!(seg->uuid = dm_pool_strdup(node->dtree->mem, log_uuid))) {
2150 log_error("log uuid pool_strdup failed");
2151 return 0;
2152 }
9723090c
AK
2153 if (!(flags & DM_CORELOG)) {
2154 if (!(log_node = dm_tree_find_node_by_uuid(node->dtree, log_uuid))) {
2155 log_error("Couldn't find mirror log uuid %s.", log_uuid);
2156 return 0;
2157 }
2158
2159 if (!_link_tree_nodes(node, log_node))
2160 return_0;
2161 }
165e4a11
AK
2162 }
2163
2164 seg->log = log_node;
165e4a11
AK
2165 seg->region_size = region_size;
2166 seg->clustered = clustered;
2167 seg->mirror_area_count = area_count;
dbcb64b8 2168 seg->flags = flags;
165e4a11
AK
2169
2170 return 1;
2171}
2172
b4f1578f 2173int dm_tree_node_add_mirror_target(struct dm_tree_node *node,
165e4a11
AK
2174 uint64_t size)
2175{
2176 struct load_segment *seg;
2177
b4f1578f
AK
2178 if (!(seg = _add_segment(node, SEG_MIRRORED, size)))
2179 return_0;
165e4a11
AK
2180
2181 return 1;
2182}
2183
b262f3e1
ZK
2184int dm_tree_node_add_replicator_target(struct dm_tree_node *node,
2185 uint64_t size,
2186 const char *rlog_uuid,
2187 const char *rlog_type,
2188 unsigned rsite_index,
2189 dm_replicator_mode_t mode,
2190 uint32_t async_timeout,
2191 uint64_t fall_behind_data,
2192 uint32_t fall_behind_ios)
2193{
2194 struct load_segment *rseg;
2195 struct replicator_site *rsite;
2196
2197 /* Local site0 - adds replicator segment and links rlog device */
2198 if (rsite_index == REPLICATOR_LOCAL_SITE) {
2199 if (node->props.segment_count) {
2200 log_error(INTERNAL_ERROR "Attempt to add replicator segment to already used node.");
2201 return 0;
2202 }
2203
2204 if (!(rseg = _add_segment(node, SEG_REPLICATOR, size)))
2205 return_0;
2206
2207 if (!(rseg->log = dm_tree_find_node_by_uuid(node->dtree, rlog_uuid))) {
2208 log_error("Missing replicator log uuid %s.", rlog_uuid);
2209 return 0;
2210 }
2211
2212 if (!_link_tree_nodes(node, rseg->log))
2213 return_0;
2214
2215 if (strcmp(rlog_type, "ringbuffer") != 0) {
2216 log_error("Unsupported replicator log type %s.", rlog_type);
2217 return 0;
2218 }
2219
2220 if (!(rseg->rlog_type = dm_pool_strdup(node->dtree->mem, rlog_type)))
2221 return_0;
2222
2223 dm_list_init(&rseg->rsites);
2224 rseg->rdevice_count = 0;
2225 node->activation_priority = 1;
2226 }
2227
2228 /* Add site to segment */
2229 if (mode == DM_REPLICATOR_SYNC
2230 && (async_timeout || fall_behind_ios || fall_behind_data)) {
2231 log_error("Async parameters passed for synchronnous replicator.");
2232 return 0;
2233 }
2234
2235 if (node->props.segment_count != 1) {
2236 log_error(INTERNAL_ERROR "Attempt to add remote site area before setting replicator log.");
2237 return 0;
2238 }
2239
2240 rseg = dm_list_item(dm_list_last(&node->props.segs), struct load_segment);
2241 if (rseg->type != SEG_REPLICATOR) {
2242 log_error(INTERNAL_ERROR "Attempt to use non replicator segment %s.",
2243 dm_segtypes[rseg->type].target);
2244 return 0;
2245 }
2246
2247 if (!(rsite = dm_pool_zalloc(node->dtree->mem, sizeof(*rsite)))) {
2248 log_error("Failed to allocate remote site segment.");
2249 return 0;
2250 }
2251
2252 dm_list_add(&rseg->rsites, &rsite->list);
2253 rseg->rsite_count++;
2254
2255 rsite->mode = mode;
2256 rsite->async_timeout = async_timeout;
2257 rsite->fall_behind_data = fall_behind_data;
2258 rsite->fall_behind_ios = fall_behind_ios;
2259 rsite->rsite_index = rsite_index;
2260
2261 return 1;
2262}
2263
2264/* Appends device node to Replicator */
2265int dm_tree_node_add_replicator_dev_target(struct dm_tree_node *node,
2266 uint64_t size,
2267 const char *replicator_uuid,
2268 uint64_t rdevice_index,
2269 const char *rdev_uuid,
2270 unsigned rsite_index,
2271 const char *slog_uuid,
2272 uint32_t slog_flags,
2273 uint32_t slog_region_size)
2274{
2275 struct seg_area *area;
2276 struct load_segment *rseg;
2277 struct load_segment *rep_seg;
2278
2279 if (rsite_index == REPLICATOR_LOCAL_SITE) {
2280 /* Site index for local target */
2281 if (!(rseg = _add_segment(node, SEG_REPLICATOR_DEV, size)))
2282 return_0;
2283
2284 if (!(rseg->replicator = dm_tree_find_node_by_uuid(node->dtree, replicator_uuid))) {
2285 log_error("Missing replicator uuid %s.", replicator_uuid);
2286 return 0;
2287 }
2288
2289 /* Local slink0 for replicator must be always initialized first */
2290 if (rseg->replicator->props.segment_count != 1) {
2291 log_error(INTERNAL_ERROR "Attempt to use non replicator segment.");
2292 return 0;
2293 }
2294
2295 rep_seg = dm_list_item(dm_list_last(&rseg->replicator->props.segs), struct load_segment);
2296 if (rep_seg->type != SEG_REPLICATOR) {
2297 log_error(INTERNAL_ERROR "Attempt to use non replicator segment %s.",
2298 dm_segtypes[rep_seg->type].target);
2299 return 0;
2300 }
2301 rep_seg->rdevice_count++;
2302
2303 if (!_link_tree_nodes(node, rseg->replicator))
2304 return_0;
2305
2306 rseg->rdevice_index = rdevice_index;
2307 } else {
2308 /* Local slink0 for replicator must be always initialized first */
2309 if (node->props.segment_count != 1) {
2310 log_error(INTERNAL_ERROR "Attempt to use non replicator-dev segment.");
2311 return 0;
2312 }
2313
2314 rseg = dm_list_item(dm_list_last(&node->props.segs), struct load_segment);
2315 if (rseg->type != SEG_REPLICATOR_DEV) {
2316 log_error(INTERNAL_ERROR "Attempt to use non replicator-dev segment %s.",
2317 dm_segtypes[rseg->type].target);
2318 return 0;
2319 }
2320 }
2321
2322 if (!(slog_flags & DM_CORELOG) && !slog_uuid) {
2323 log_error("Unspecified sync log uuid.");
2324 return 0;
2325 }
2326
2327 if (!dm_tree_node_add_target_area(node, NULL, rdev_uuid, 0))
2328 return_0;
2329
2330 area = dm_list_item(dm_list_last(&rseg->areas), struct seg_area);
2331
2332 if (!(slog_flags & DM_CORELOG)) {
2333 if (!(area->slog = dm_tree_find_node_by_uuid(node->dtree, slog_uuid))) {
2334 log_error("Couldn't find sync log uuid %s.", slog_uuid);
2335 return 0;
2336 }
2337
2338 if (!_link_tree_nodes(node, area->slog))
2339 return_0;
2340 }
2341
2342 area->flags = slog_flags;
2343 area->region_size = slog_region_size;
2344 area->rsite_index = rsite_index;
2345
2346 return 1;
2347}
2348
b4f1578f 2349static int _add_area(struct dm_tree_node *node, struct load_segment *seg, struct dm_tree_node *dev_node, uint64_t offset)
165e4a11
AK
2350{
2351 struct seg_area *area;
2352
b4f1578f 2353 if (!(area = dm_pool_zalloc(node->dtree->mem, sizeof (*area)))) {
165e4a11
AK
2354 log_error("Failed to allocate target segment area.");
2355 return 0;
2356 }
2357
2358 area->dev_node = dev_node;
2359 area->offset = offset;
2360
2c44337b 2361 dm_list_add(&seg->areas, &area->list);
165e4a11
AK
2362 seg->area_count++;
2363
2364 return 1;
2365}
2366
b4f1578f 2367int dm_tree_node_add_target_area(struct dm_tree_node *node,
165e4a11
AK
2368 const char *dev_name,
2369 const char *uuid,
2370 uint64_t offset)
2371{
2372 struct load_segment *seg;
2373 struct stat info;
b4f1578f 2374 struct dm_tree_node *dev_node;
165e4a11
AK
2375
2376 if ((!dev_name || !*dev_name) && (!uuid || !*uuid)) {
b4f1578f 2377 log_error("dm_tree_node_add_target_area called without device");
165e4a11
AK
2378 return 0;
2379 }
2380
2381 if (uuid) {
b4f1578f 2382 if (!(dev_node = dm_tree_find_node_by_uuid(node->dtree, uuid))) {
165e4a11
AK
2383 log_error("Couldn't find area uuid %s.", uuid);
2384 return 0;
2385 }
b4f1578f
AK
2386 if (!_link_tree_nodes(node, dev_node))
2387 return_0;
165e4a11
AK
2388 } else {
2389 if (stat(dev_name, &info) < 0) {
2390 log_error("Device %s not found.", dev_name);
2391 return 0;
2392 }
2393
2394 if (!S_ISBLK(info.st_mode)) {
2395 log_error("Device %s is not a block device.", dev_name);
2396 return 0;
2397 }
2398
2399 /* FIXME Check correct macro use */
cda69e17
PR
2400 if (!(dev_node = _add_dev(node->dtree, node, MAJOR(info.st_rdev),
2401 MINOR(info.st_rdev), 0)))
b4f1578f 2402 return_0;
165e4a11
AK
2403 }
2404
2405 if (!node->props.segment_count) {
b8175c33 2406 log_error(INTERNAL_ERROR "Attempt to add target area to missing segment.");
165e4a11
AK
2407 return 0;
2408 }
2409
2c44337b 2410 seg = dm_list_item(dm_list_last(&node->props.segs), struct load_segment);
165e4a11 2411
b4f1578f
AK
2412 if (!_add_area(node, seg, dev_node, offset))
2413 return_0;
165e4a11
AK
2414
2415 return 1;
db208f51 2416}
bd90c6b2
AK
2417
2418void dm_tree_set_cookie(struct dm_tree_node *node, uint32_t cookie)
2419{
2420 node->dtree->cookie = cookie;
2421}
2422
2423uint32_t dm_tree_get_cookie(struct dm_tree_node *node)
2424{
2425 return node->dtree->cookie;
2426}
This page took 0.337552 seconds and 5 git commands to generate.