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