]> sourceware.org Git - lvm2.git/blame - libdm/libdm-deptree.c
move doc to doc dir
[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 }
812e10ac
ZK
1757 if (!params[0])
1758 log_error(INTERNAL_ERROR "Empty parameters for "
1759 "%s %u:%u.", dm_segtypes[seg->type].target,
1760 major, minor);
165e4a11
AK
1761 break;
1762 }
1763
4b2cae46
AK
1764 log_debug("Adding target to (%" PRIu32 ":%" PRIu32 "): %" PRIu64
1765 " %" PRIu64 " %s %s", major, minor,
165e4a11
AK
1766 *seg_start, seg->size, dm_segtypes[seg->type].target, params);
1767
b4f1578f
AK
1768 if (!dm_task_add_target(dmt, *seg_start, seg->size, dm_segtypes[seg->type].target, params))
1769 return_0;
165e4a11
AK
1770
1771 *seg_start += seg->size;
1772
1773 return 1;
1774}
1775
ffa9b6a5
ZK
1776#undef EMIT_PARAMS
1777
4b2cae46
AK
1778static int _emit_segment(struct dm_task *dmt, uint32_t major, uint32_t minor,
1779 struct load_segment *seg, uint64_t *seg_start)
165e4a11
AK
1780{
1781 char *params;
1782 size_t paramsize = 4096;
1783 int ret;
1784
1785 do {
1786 if (!(params = dm_malloc(paramsize))) {
1787 log_error("Insufficient space for target parameters.");
1788 return 0;
1789 }
1790
12ea7cb1 1791 params[0] = '\0';
4b2cae46
AK
1792 ret = _emit_segment_line(dmt, major, minor, seg, seg_start,
1793 params, paramsize);
165e4a11
AK
1794 dm_free(params);
1795
1796 if (!ret)
1797 stack;
1798
1799 if (ret >= 0)
1800 return ret;
1801
1802 log_debug("Insufficient space in params[%" PRIsize_t
1803 "] for target parameters.", paramsize);
1804
1805 paramsize *= 2;
1806 } while (paramsize < MAX_TARGET_PARAMSIZE);
1807
1808 log_error("Target parameter size too big. Aborting.");
1809 return 0;
1810}
1811
b4f1578f 1812static int _load_node(struct dm_tree_node *dnode)
165e4a11
AK
1813{
1814 int r = 0;
1815 struct dm_task *dmt;
1816 struct load_segment *seg;
df390f17 1817 uint64_t seg_start = 0, existing_table_size;
165e4a11 1818
4b2cae46
AK
1819 log_verbose("Loading %s table (%" PRIu32 ":%" PRIu32 ")", dnode->name,
1820 dnode->info.major, dnode->info.minor);
165e4a11
AK
1821
1822 if (!(dmt = dm_task_create(DM_DEVICE_RELOAD))) {
1823 log_error("Reload dm_task creation failed for %s", dnode->name);
1824 return 0;
1825 }
1826
1827 if (!dm_task_set_major(dmt, dnode->info.major) ||
1828 !dm_task_set_minor(dmt, dnode->info.minor)) {
1829 log_error("Failed to set device number for %s reload.", dnode->name);
1830 goto out;
1831 }
1832
1833 if (dnode->props.read_only && !dm_task_set_ro(dmt)) {
1834 log_error("Failed to set read only flag for %s", dnode->name);
1835 goto out;
1836 }
1837
1838 if (!dm_task_no_open_count(dmt))
1839 log_error("Failed to disable open_count");
1840
2c44337b 1841 dm_list_iterate_items(seg, &dnode->props.segs)
4b2cae46
AK
1842 if (!_emit_segment(dmt, dnode->info.major, dnode->info.minor,
1843 seg, &seg_start))
b4f1578f 1844 goto_out;
165e4a11 1845
ec289b64
AK
1846 if (!dm_task_suppress_identical_reload(dmt))
1847 log_error("Failed to suppress reload of identical tables.");
1848
1849 if ((r = dm_task_run(dmt))) {
165e4a11 1850 r = dm_task_get_info(dmt, &dnode->info);
ec289b64
AK
1851 if (r && !dnode->info.inactive_table)
1852 log_verbose("Suppressed %s identical table reload.",
1853 dnode->name);
bb875bb9 1854
df390f17 1855 existing_table_size = dm_task_get_existing_table_size(dmt);
bb875bb9 1856 if ((dnode->props.size_changed =
df390f17 1857 (existing_table_size == seg_start) ? 0 : 1)) {
bb875bb9 1858 log_debug("Table size changed from %" PRIu64 " to %"
df390f17 1859 PRIu64 " for %s", existing_table_size,
bb875bb9 1860 seg_start, dnode->name);
df390f17
AK
1861 /*
1862 * Kernel usually skips size validation on zero-length devices
1863 * now so no need to preload them.
1864 */
1865 /* FIXME In which kernel version did this begin? */
1866 if (!existing_table_size && dnode->props.delay_resume_if_new)
1867 dnode->props.size_changed = 0;
1868 }
ec289b64 1869 }
165e4a11
AK
1870
1871 dnode->props.segment_count = 0;
1872
1873out:
1874 dm_task_destroy(dmt);
1875
1876 return r;
165e4a11
AK
1877}
1878
b4f1578f 1879int dm_tree_preload_children(struct dm_tree_node *dnode,
bb875bb9
AK
1880 const char *uuid_prefix,
1881 size_t uuid_prefix_len)
165e4a11 1882{
2ca6b865 1883 int r = 1;
165e4a11 1884 void *handle = NULL;
b4f1578f 1885 struct dm_tree_node *child;
165e4a11 1886 struct dm_info newinfo;
566515c0 1887 int update_devs_flag = 0;
165e4a11
AK
1888
1889 /* Preload children first */
b4f1578f 1890 while ((child = dm_tree_next_child(&handle, dnode, 0))) {
165e4a11
AK
1891 /* Skip existing non-device-mapper devices */
1892 if (!child->info.exists && child->info.major)
1893 continue;
1894
1895 /* Ignore if it doesn't belong to this VG */
87f98002
AK
1896 if (child->info.exists &&
1897 !_uuid_prefix_matches(child->uuid, uuid_prefix, uuid_prefix_len))
165e4a11
AK
1898 continue;
1899
b4f1578f 1900 if (dm_tree_node_num_children(child, 0))
2ca6b865
MS
1901 if (!dm_tree_preload_children(child, uuid_prefix, uuid_prefix_len))
1902 return_0;
165e4a11 1903
165e4a11
AK
1904 /* FIXME Cope if name exists with no uuid? */
1905 if (!child->info.exists) {
1906 if (!_create_node(child)) {
1907 stack;
1908 return 0;
1909 }
1910 }
1911
1912 if (!child->info.inactive_table && child->props.segment_count) {
1913 if (!_load_node(child)) {
1914 stack;
1915 return 0;
1916 }
1917 }
1918
eb91c4ee
MB
1919 /* Propagate device size change change */
1920 if (child->props.size_changed)
1921 dnode->props.size_changed = 1;
1922
bb875bb9 1923 /* Resume device immediately if it has parents and its size changed */
3776c494 1924 if (!dm_tree_node_num_children(child, 1) || !child->props.size_changed)
165e4a11
AK
1925 continue;
1926
7707ea90
AK
1927 if (!child->info.inactive_table && !child->info.suspended)
1928 continue;
1929
fc795d87 1930 if (!_resume_node(child->name, child->info.major, child->info.minor,
bd90c6b2 1931 child->props.read_ahead, child->props.read_ahead_flags,
1840aa09
AK
1932 &newinfo, &child->dtree->cookie, child->udev_flags,
1933 child->info.suspended)) {
165e4a11 1934 log_error("Unable to resume %s (%" PRIu32
fc795d87 1935 ":%" PRIu32 ")", child->name, child->info.major,
165e4a11 1936 child->info.minor);
2ca6b865 1937 r = 0;
165e4a11
AK
1938 continue;
1939 }
1940
1941 /* Update cached info */
1942 child->info = newinfo;
566515c0
PR
1943
1944 /*
1945 * Prepare for immediate synchronization with udev and flush all stacked
1946 * dev node operations if requested by immediate_dev_node property. But
1947 * finish processing current level in the tree first.
1948 */
1949 if (child->props.immediate_dev_node)
1950 update_devs_flag = 1;
1951
165e4a11
AK
1952 }
1953
1954 handle = NULL;
1955
566515c0
PR
1956 if (update_devs_flag) {
1957 if (!dm_udev_wait(dm_tree_get_cookie(dnode)))
1958 stack;
1959 dm_tree_set_cookie(dnode, 0);
566515c0
PR
1960 }
1961
2ca6b865 1962 return r;
165e4a11
AK
1963}
1964
165e4a11
AK
1965/*
1966 * Returns 1 if unsure.
1967 */
b4f1578f 1968int dm_tree_children_use_uuid(struct dm_tree_node *dnode,
165e4a11
AK
1969 const char *uuid_prefix,
1970 size_t uuid_prefix_len)
1971{
1972 void *handle = NULL;
b4f1578f 1973 struct dm_tree_node *child = dnode;
165e4a11
AK
1974 const char *uuid;
1975
b4f1578f
AK
1976 while ((child = dm_tree_next_child(&handle, dnode, 0))) {
1977 if (!(uuid = dm_tree_node_get_uuid(child))) {
1978 log_error("Failed to get uuid for dtree node.");
165e4a11
AK
1979 return 1;
1980 }
1981
87f98002 1982 if (_uuid_prefix_matches(uuid, uuid_prefix, uuid_prefix_len))
165e4a11
AK
1983 return 1;
1984
b4f1578f
AK
1985 if (dm_tree_node_num_children(child, 0))
1986 dm_tree_children_use_uuid(child, uuid_prefix, uuid_prefix_len);
165e4a11
AK
1987 }
1988
1989 return 0;
1990}
1991
1992/*
1993 * Target functions
1994 */
b4f1578f 1995static struct load_segment *_add_segment(struct dm_tree_node *dnode, unsigned type, uint64_t size)
165e4a11
AK
1996{
1997 struct load_segment *seg;
1998
b4f1578f
AK
1999 if (!(seg = dm_pool_zalloc(dnode->dtree->mem, sizeof(*seg)))) {
2000 log_error("dtree node segment allocation failed");
165e4a11
AK
2001 return NULL;
2002 }
2003
2004 seg->type = type;
2005 seg->size = size;
2006 seg->area_count = 0;
2c44337b 2007 dm_list_init(&seg->areas);
165e4a11
AK
2008 seg->stripe_size = 0;
2009 seg->persistent = 0;
2010 seg->chunk_size = 0;
2011 seg->cow = NULL;
2012 seg->origin = NULL;
aa6f4e51 2013 seg->merge = NULL;
165e4a11 2014
2c44337b 2015 dm_list_add(&dnode->props.segs, &seg->list);
165e4a11
AK
2016 dnode->props.segment_count++;
2017
2018 return seg;
2019}
2020
b4f1578f 2021int dm_tree_node_add_snapshot_origin_target(struct dm_tree_node *dnode,
165e4a11
AK
2022 uint64_t size,
2023 const char *origin_uuid)
2024{
2025 struct load_segment *seg;
b4f1578f 2026 struct dm_tree_node *origin_node;
165e4a11 2027
b4f1578f
AK
2028 if (!(seg = _add_segment(dnode, SEG_SNAPSHOT_ORIGIN, size)))
2029 return_0;
165e4a11 2030
b4f1578f 2031 if (!(origin_node = dm_tree_find_node_by_uuid(dnode->dtree, origin_uuid))) {
165e4a11
AK
2032 log_error("Couldn't find snapshot origin uuid %s.", origin_uuid);
2033 return 0;
2034 }
2035
2036 seg->origin = origin_node;
b4f1578f
AK
2037 if (!_link_tree_nodes(dnode, origin_node))
2038 return_0;
165e4a11 2039
56c28292
AK
2040 /* Resume snapshot origins after new snapshots */
2041 dnode->activation_priority = 1;
2042
165e4a11
AK
2043 return 1;
2044}
2045
aa6f4e51
MS
2046static int _add_snapshot_target(struct dm_tree_node *node,
2047 uint64_t size,
2048 const char *origin_uuid,
2049 const char *cow_uuid,
2050 const char *merge_uuid,
2051 int persistent,
2052 uint32_t chunk_size)
165e4a11
AK
2053{
2054 struct load_segment *seg;
aa6f4e51
MS
2055 struct dm_tree_node *origin_node, *cow_node, *merge_node;
2056 unsigned seg_type;
2057
2058 seg_type = !merge_uuid ? SEG_SNAPSHOT : SEG_SNAPSHOT_MERGE;
165e4a11 2059
aa6f4e51 2060 if (!(seg = _add_segment(node, seg_type, size)))
b4f1578f 2061 return_0;
165e4a11 2062
b4f1578f 2063 if (!(origin_node = dm_tree_find_node_by_uuid(node->dtree, origin_uuid))) {
165e4a11
AK
2064 log_error("Couldn't find snapshot origin uuid %s.", origin_uuid);
2065 return 0;
2066 }
2067
2068 seg->origin = origin_node;
b4f1578f
AK
2069 if (!_link_tree_nodes(node, origin_node))
2070 return_0;
165e4a11 2071
b4f1578f 2072 if (!(cow_node = dm_tree_find_node_by_uuid(node->dtree, cow_uuid))) {
aa6f4e51 2073 log_error("Couldn't find snapshot COW device uuid %s.", cow_uuid);
165e4a11
AK
2074 return 0;
2075 }
2076
2077 seg->cow = cow_node;
b4f1578f
AK
2078 if (!_link_tree_nodes(node, cow_node))
2079 return_0;
165e4a11
AK
2080
2081 seg->persistent = persistent ? 1 : 0;
2082 seg->chunk_size = chunk_size;
2083
aa6f4e51
MS
2084 if (merge_uuid) {
2085 if (!(merge_node = dm_tree_find_node_by_uuid(node->dtree, merge_uuid))) {
2086 /* not a pure error, merging snapshot may have been deactivated */
2087 log_verbose("Couldn't find merging snapshot uuid %s.", merge_uuid);
2088 } else {
2089 seg->merge = merge_node;
2090 /* must not link merging snapshot, would undermine activation_priority below */
2091 }
2092
2093 /* Resume snapshot-merge (acting origin) after other snapshots */
2094 node->activation_priority = 1;
2095 if (seg->merge) {
2096 /* Resume merging snapshot after snapshot-merge */
2097 seg->merge->activation_priority = 2;
2098 }
2099 }
2100
165e4a11
AK
2101 return 1;
2102}
2103
aa6f4e51
MS
2104
2105int dm_tree_node_add_snapshot_target(struct dm_tree_node *node,
2106 uint64_t size,
2107 const char *origin_uuid,
2108 const char *cow_uuid,
2109 int persistent,
2110 uint32_t chunk_size)
2111{
2112 return _add_snapshot_target(node, size, origin_uuid, cow_uuid,
2113 NULL, persistent, chunk_size);
2114}
2115
2116int dm_tree_node_add_snapshot_merge_target(struct dm_tree_node *node,
2117 uint64_t size,
2118 const char *origin_uuid,
2119 const char *cow_uuid,
2120 const char *merge_uuid,
2121 uint32_t chunk_size)
2122{
2123 return _add_snapshot_target(node, size, origin_uuid, cow_uuid,
2124 merge_uuid, 1, chunk_size);
2125}
2126
b4f1578f 2127int dm_tree_node_add_error_target(struct dm_tree_node *node,
165e4a11
AK
2128 uint64_t size)
2129{
b4f1578f
AK
2130 if (!_add_segment(node, SEG_ERROR, size))
2131 return_0;
165e4a11
AK
2132
2133 return 1;
2134}
2135
b4f1578f 2136int dm_tree_node_add_zero_target(struct dm_tree_node *node,
165e4a11
AK
2137 uint64_t size)
2138{
b4f1578f
AK
2139 if (!_add_segment(node, SEG_ZERO, size))
2140 return_0;
165e4a11
AK
2141
2142 return 1;
2143}
2144
b4f1578f 2145int dm_tree_node_add_linear_target(struct dm_tree_node *node,
165e4a11
AK
2146 uint64_t size)
2147{
b4f1578f
AK
2148 if (!_add_segment(node, SEG_LINEAR, size))
2149 return_0;
165e4a11
AK
2150
2151 return 1;
2152}
2153
b4f1578f 2154int dm_tree_node_add_striped_target(struct dm_tree_node *node,
165e4a11
AK
2155 uint64_t size,
2156 uint32_t stripe_size)
2157{
2158 struct load_segment *seg;
2159
b4f1578f
AK
2160 if (!(seg = _add_segment(node, SEG_STRIPED, size)))
2161 return_0;
165e4a11
AK
2162
2163 seg->stripe_size = stripe_size;
2164
2165 return 1;
2166}
2167
12ca060e
MB
2168int dm_tree_node_add_crypt_target(struct dm_tree_node *node,
2169 uint64_t size,
2170 const char *cipher,
2171 const char *chainmode,
2172 const char *iv,
2173 uint64_t iv_offset,
2174 const char *key)
2175{
2176 struct load_segment *seg;
2177
2178 if (!(seg = _add_segment(node, SEG_CRYPT, size)))
2179 return_0;
2180
2181 seg->cipher = cipher;
2182 seg->chainmode = chainmode;
2183 seg->iv = iv;
2184 seg->iv_offset = iv_offset;
2185 seg->key = key;
2186
2187 return 1;
2188}
2189
b4f1578f 2190int dm_tree_node_add_mirror_target_log(struct dm_tree_node *node,
165e4a11 2191 uint32_t region_size,
08e64ce5 2192 unsigned clustered,
165e4a11 2193 const char *log_uuid,
ce7ed2c0
AK
2194 unsigned area_count,
2195 uint32_t flags)
165e4a11 2196{
908db078 2197 struct dm_tree_node *log_node = NULL;
165e4a11
AK
2198 struct load_segment *seg;
2199
2200 if (!node->props.segment_count) {
b8175c33 2201 log_error(INTERNAL_ERROR "Attempt to add target area to missing segment.");
165e4a11
AK
2202 return 0;
2203 }
2204
2c44337b 2205 seg = dm_list_item(dm_list_last(&node->props.segs), struct load_segment);
165e4a11 2206
24b026e3 2207 if (log_uuid) {
67b25ed4
AK
2208 if (!(seg->uuid = dm_pool_strdup(node->dtree->mem, log_uuid))) {
2209 log_error("log uuid pool_strdup failed");
2210 return 0;
2211 }
df390f17
AK
2212 if ((flags & DM_CORELOG))
2213 /* For pvmove: immediate resume (for size validation) isn't needed. */
2214 node->props.delay_resume_if_new = 1;
2215 else {
9723090c
AK
2216 if (!(log_node = dm_tree_find_node_by_uuid(node->dtree, log_uuid))) {
2217 log_error("Couldn't find mirror log uuid %s.", log_uuid);
2218 return 0;
2219 }
2220
566515c0
PR
2221 if (clustered)
2222 log_node->props.immediate_dev_node = 1;
2223
0a99713e
AK
2224 /* The kernel validates the size of disk logs. */
2225 /* FIXME Propagate to any devices below */
2226 log_node->props.delay_resume_if_new = 0;
2227
9723090c
AK
2228 if (!_link_tree_nodes(node, log_node))
2229 return_0;
2230 }
165e4a11
AK
2231 }
2232
2233 seg->log = log_node;
165e4a11
AK
2234 seg->region_size = region_size;
2235 seg->clustered = clustered;
2236 seg->mirror_area_count = area_count;
dbcb64b8 2237 seg->flags = flags;
165e4a11
AK
2238
2239 return 1;
2240}
2241
b4f1578f 2242int dm_tree_node_add_mirror_target(struct dm_tree_node *node,
165e4a11
AK
2243 uint64_t size)
2244{
cbecd3cd 2245 if (!_add_segment(node, SEG_MIRRORED, size))
b4f1578f 2246 return_0;
165e4a11
AK
2247
2248 return 1;
2249}
2250
b262f3e1
ZK
2251int dm_tree_node_add_replicator_target(struct dm_tree_node *node,
2252 uint64_t size,
2253 const char *rlog_uuid,
2254 const char *rlog_type,
2255 unsigned rsite_index,
2256 dm_replicator_mode_t mode,
2257 uint32_t async_timeout,
2258 uint64_t fall_behind_data,
2259 uint32_t fall_behind_ios)
2260{
2261 struct load_segment *rseg;
2262 struct replicator_site *rsite;
2263
2264 /* Local site0 - adds replicator segment and links rlog device */
2265 if (rsite_index == REPLICATOR_LOCAL_SITE) {
2266 if (node->props.segment_count) {
2267 log_error(INTERNAL_ERROR "Attempt to add replicator segment to already used node.");
2268 return 0;
2269 }
2270
2271 if (!(rseg = _add_segment(node, SEG_REPLICATOR, size)))
2272 return_0;
2273
2274 if (!(rseg->log = dm_tree_find_node_by_uuid(node->dtree, rlog_uuid))) {
2275 log_error("Missing replicator log uuid %s.", rlog_uuid);
2276 return 0;
2277 }
2278
2279 if (!_link_tree_nodes(node, rseg->log))
2280 return_0;
2281
2282 if (strcmp(rlog_type, "ringbuffer") != 0) {
2283 log_error("Unsupported replicator log type %s.", rlog_type);
2284 return 0;
2285 }
2286
2287 if (!(rseg->rlog_type = dm_pool_strdup(node->dtree->mem, rlog_type)))
2288 return_0;
2289
2290 dm_list_init(&rseg->rsites);
2291 rseg->rdevice_count = 0;
2292 node->activation_priority = 1;
2293 }
2294
2295 /* Add site to segment */
2296 if (mode == DM_REPLICATOR_SYNC
2297 && (async_timeout || fall_behind_ios || fall_behind_data)) {
2298 log_error("Async parameters passed for synchronnous replicator.");
2299 return 0;
2300 }
2301
2302 if (node->props.segment_count != 1) {
2303 log_error(INTERNAL_ERROR "Attempt to add remote site area before setting replicator log.");
2304 return 0;
2305 }
2306
2307 rseg = dm_list_item(dm_list_last(&node->props.segs), struct load_segment);
2308 if (rseg->type != SEG_REPLICATOR) {
2309 log_error(INTERNAL_ERROR "Attempt to use non replicator segment %s.",
2310 dm_segtypes[rseg->type].target);
2311 return 0;
2312 }
2313
2314 if (!(rsite = dm_pool_zalloc(node->dtree->mem, sizeof(*rsite)))) {
2315 log_error("Failed to allocate remote site segment.");
2316 return 0;
2317 }
2318
2319 dm_list_add(&rseg->rsites, &rsite->list);
2320 rseg->rsite_count++;
2321
2322 rsite->mode = mode;
2323 rsite->async_timeout = async_timeout;
2324 rsite->fall_behind_data = fall_behind_data;
2325 rsite->fall_behind_ios = fall_behind_ios;
2326 rsite->rsite_index = rsite_index;
2327
2328 return 1;
2329}
2330
2331/* Appends device node to Replicator */
2332int dm_tree_node_add_replicator_dev_target(struct dm_tree_node *node,
2333 uint64_t size,
2334 const char *replicator_uuid,
2335 uint64_t rdevice_index,
2336 const char *rdev_uuid,
2337 unsigned rsite_index,
2338 const char *slog_uuid,
2339 uint32_t slog_flags,
2340 uint32_t slog_region_size)
2341{
2342 struct seg_area *area;
2343 struct load_segment *rseg;
2344 struct load_segment *rep_seg;
2345
2346 if (rsite_index == REPLICATOR_LOCAL_SITE) {
2347 /* Site index for local target */
2348 if (!(rseg = _add_segment(node, SEG_REPLICATOR_DEV, size)))
2349 return_0;
2350
2351 if (!(rseg->replicator = dm_tree_find_node_by_uuid(node->dtree, replicator_uuid))) {
2352 log_error("Missing replicator uuid %s.", replicator_uuid);
2353 return 0;
2354 }
2355
2356 /* Local slink0 for replicator must be always initialized first */
2357 if (rseg->replicator->props.segment_count != 1) {
2358 log_error(INTERNAL_ERROR "Attempt to use non replicator segment.");
2359 return 0;
2360 }
2361
2362 rep_seg = dm_list_item(dm_list_last(&rseg->replicator->props.segs), struct load_segment);
2363 if (rep_seg->type != SEG_REPLICATOR) {
2364 log_error(INTERNAL_ERROR "Attempt to use non replicator segment %s.",
2365 dm_segtypes[rep_seg->type].target);
2366 return 0;
2367 }
2368 rep_seg->rdevice_count++;
2369
2370 if (!_link_tree_nodes(node, rseg->replicator))
2371 return_0;
2372
2373 rseg->rdevice_index = rdevice_index;
2374 } else {
2375 /* Local slink0 for replicator must be always initialized first */
2376 if (node->props.segment_count != 1) {
2377 log_error(INTERNAL_ERROR "Attempt to use non replicator-dev segment.");
2378 return 0;
2379 }
2380
2381 rseg = dm_list_item(dm_list_last(&node->props.segs), struct load_segment);
2382 if (rseg->type != SEG_REPLICATOR_DEV) {
2383 log_error(INTERNAL_ERROR "Attempt to use non replicator-dev segment %s.",
2384 dm_segtypes[rseg->type].target);
2385 return 0;
2386 }
2387 }
2388
2389 if (!(slog_flags & DM_CORELOG) && !slog_uuid) {
2390 log_error("Unspecified sync log uuid.");
2391 return 0;
2392 }
2393
2394 if (!dm_tree_node_add_target_area(node, NULL, rdev_uuid, 0))
2395 return_0;
2396
2397 area = dm_list_item(dm_list_last(&rseg->areas), struct seg_area);
2398
2399 if (!(slog_flags & DM_CORELOG)) {
2400 if (!(area->slog = dm_tree_find_node_by_uuid(node->dtree, slog_uuid))) {
2401 log_error("Couldn't find sync log uuid %s.", slog_uuid);
2402 return 0;
2403 }
2404
2405 if (!_link_tree_nodes(node, area->slog))
2406 return_0;
2407 }
2408
2409 area->flags = slog_flags;
2410 area->region_size = slog_region_size;
2411 area->rsite_index = rsite_index;
2412
2413 return 1;
2414}
2415
b4f1578f 2416static int _add_area(struct dm_tree_node *node, struct load_segment *seg, struct dm_tree_node *dev_node, uint64_t offset)
165e4a11
AK
2417{
2418 struct seg_area *area;
2419
b4f1578f 2420 if (!(area = dm_pool_zalloc(node->dtree->mem, sizeof (*area)))) {
165e4a11
AK
2421 log_error("Failed to allocate target segment area.");
2422 return 0;
2423 }
2424
2425 area->dev_node = dev_node;
2426 area->offset = offset;
2427
2c44337b 2428 dm_list_add(&seg->areas, &area->list);
165e4a11
AK
2429 seg->area_count++;
2430
2431 return 1;
2432}
2433
b4f1578f 2434int dm_tree_node_add_target_area(struct dm_tree_node *node,
165e4a11
AK
2435 const char *dev_name,
2436 const char *uuid,
2437 uint64_t offset)
2438{
2439 struct load_segment *seg;
2440 struct stat info;
b4f1578f 2441 struct dm_tree_node *dev_node;
165e4a11
AK
2442
2443 if ((!dev_name || !*dev_name) && (!uuid || !*uuid)) {
b4f1578f 2444 log_error("dm_tree_node_add_target_area called without device");
165e4a11
AK
2445 return 0;
2446 }
2447
2448 if (uuid) {
b4f1578f 2449 if (!(dev_node = dm_tree_find_node_by_uuid(node->dtree, uuid))) {
165e4a11
AK
2450 log_error("Couldn't find area uuid %s.", uuid);
2451 return 0;
2452 }
b4f1578f
AK
2453 if (!_link_tree_nodes(node, dev_node))
2454 return_0;
165e4a11
AK
2455 } else {
2456 if (stat(dev_name, &info) < 0) {
2457 log_error("Device %s not found.", dev_name);
2458 return 0;
2459 }
2460
2461 if (!S_ISBLK(info.st_mode)) {
2462 log_error("Device %s is not a block device.", dev_name);
2463 return 0;
2464 }
2465
2466 /* FIXME Check correct macro use */
cda69e17
PR
2467 if (!(dev_node = _add_dev(node->dtree, node, MAJOR(info.st_rdev),
2468 MINOR(info.st_rdev), 0)))
b4f1578f 2469 return_0;
165e4a11
AK
2470 }
2471
2472 if (!node->props.segment_count) {
b8175c33 2473 log_error(INTERNAL_ERROR "Attempt to add target area to missing segment.");
165e4a11
AK
2474 return 0;
2475 }
2476
2c44337b 2477 seg = dm_list_item(dm_list_last(&node->props.segs), struct load_segment);
165e4a11 2478
b4f1578f
AK
2479 if (!_add_area(node, seg, dev_node, offset))
2480 return_0;
165e4a11
AK
2481
2482 return 1;
db208f51 2483}
bd90c6b2
AK
2484
2485void dm_tree_set_cookie(struct dm_tree_node *node, uint32_t cookie)
2486{
2487 node->dtree->cookie = cookie;
2488}
2489
2490uint32_t dm_tree_get_cookie(struct dm_tree_node *node)
2491{
2492 return node->dtree->cookie;
2493}
This page took 0.386113 seconds and 5 git commands to generate.