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