]> sourceware.org Git - lvm2.git/blame - libdm/libdm-deptree.c
Set block_on_error parameter if available.
[lvm2.git] / libdm / libdm-deptree.c
CommitLineData
3d0480ed
AK
1/*
2 * Copyright (C) 2005 Red Hat, Inc. All rights reserved.
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
15#include "lib.h"
16#include "libdm-targets.h"
17#include "libdm-common.h"
18#include "list.h"
19#include "kdev_t.h"
3d0480ed
AK
20
21#include <stdarg.h>
22#include <sys/param.h>
23
24#include <linux/dm-ioctl.h>
25
165e4a11
AK
26#define MAX_TARGET_PARAMSIZE 500000
27
87f98002
AK
28/* FIXME Fix interface so this is used only by LVM */
29#define UUID_PREFIX "LVM-"
30
165e4a11
AK
31/* Supported segment types */
32enum {
33 SEG_ERROR,
34 SEG_LINEAR,
35 SEG_MIRRORED,
36 SEG_SNAPSHOT,
37 SEG_SNAPSHOT_ORIGIN,
38 SEG_STRIPED,
39 SEG_ZERO,
40};
b4f1578f 41
165e4a11
AK
42/* FIXME Add crypt and multipath support */
43
44struct {
45 unsigned type;
46 const char *target;
47} dm_segtypes[] = {
48 { SEG_ERROR, "error" },
49 { SEG_LINEAR, "linear" },
50 { SEG_MIRRORED, "mirror" },
51 { SEG_SNAPSHOT, "snapshot" },
52 { SEG_SNAPSHOT_ORIGIN, "snapshot-origin" },
53 { SEG_STRIPED, "striped" },
54 { SEG_ZERO, "zero"},
55};
56
57/* Some segment types have a list of areas of other devices attached */
58struct seg_area {
59 struct list list;
60
b4f1578f 61 struct dm_tree_node *dev_node;
165e4a11
AK
62
63 uint64_t offset;
64};
65
66/* Per-segment properties */
67struct load_segment {
68 struct list list;
69
70 unsigned type;
71
72 uint64_t size;
73
74 unsigned area_count; /* Linear + Striped + Mirrored */
75 struct list areas; /* Linear + Striped + Mirrored */
76
77 uint32_t stripe_size; /* Striped */
78
79 int persistent; /* Snapshot */
80 uint32_t chunk_size; /* Snapshot */
b4f1578f
AK
81 struct dm_tree_node *cow; /* Snapshot */
82 struct dm_tree_node *origin; /* Snapshot + Snapshot origin */
165e4a11 83
b4f1578f 84 struct dm_tree_node *log; /* Mirror */
165e4a11
AK
85 uint32_t region_size; /* Mirror */
86 unsigned clustered; /* Mirror */
87 unsigned mirror_area_count; /* Mirror */
dbcb64b8 88 uint32_t flags; /* Mirror log */
165e4a11
AK
89};
90
91/* Per-device properties */
92struct load_properties {
93 int read_only;
94 uint32_t major;
95 uint32_t minor;
96
97 unsigned segment_count;
98 struct list segs;
99
100 const char *new_name;
101};
102
103/* Two of these used to join two nodes with uses and used_by. */
b4f1578f 104struct dm_tree_link {
165e4a11 105 struct list list;
b4f1578f 106 struct dm_tree_node *node;
165e4a11
AK
107};
108
b4f1578f
AK
109struct dm_tree_node {
110 struct dm_tree *dtree;
3d0480ed
AK
111
112 const char *name;
113 const char *uuid;
114 struct dm_info info;
115
116 struct list uses; /* Nodes this node uses */
117 struct list used_by; /* Nodes that use this node */
165e4a11 118
56c28292
AK
119 int activation_priority; /* 0 gets activated first */
120
165e4a11
AK
121 void *context; /* External supplied context */
122
123 struct load_properties props; /* For creation/table (re)load */
3d0480ed
AK
124};
125
b4f1578f 126struct dm_tree {
a3f6b2ce
AK
127 struct dm_pool *mem;
128 struct dm_hash_table *devs;
165e4a11 129 struct dm_hash_table *uuids;
b4f1578f 130 struct dm_tree_node root;
3d0480ed
AK
131};
132
165e4a11
AK
133/* FIXME Consider exporting this */
134static int _dm_snprintf(char *buf, size_t bufsize, const char *format, ...)
135{
136 int n;
137 va_list ap;
138
139 va_start(ap, format);
140 n = vsnprintf(buf, bufsize, format, ap);
141 va_end(ap);
142
143 if (n < 0 || (n > bufsize - 1))
144 return -1;
145
146 return n;
147}
3d0480ed 148
b4f1578f 149struct dm_tree *dm_tree_create(void)
3d0480ed 150{
b4f1578f 151 struct dm_tree *dtree;
3d0480ed 152
b4f1578f
AK
153 if (!(dtree = dm_malloc(sizeof(*dtree)))) {
154 log_error("dm_tree_create malloc failed");
3d0480ed
AK
155 return NULL;
156 }
157
b4f1578f
AK
158 memset(dtree, 0, sizeof(*dtree));
159 dtree->root.dtree = dtree;
160 list_init(&dtree->root.uses);
161 list_init(&dtree->root.used_by);
3d0480ed 162
b4f1578f
AK
163 if (!(dtree->mem = dm_pool_create("dtree", 1024))) {
164 log_error("dtree pool creation failed");
165 dm_free(dtree);
3d0480ed
AK
166 return NULL;
167 }
168
b4f1578f
AK
169 if (!(dtree->devs = dm_hash_create(8))) {
170 log_error("dtree hash creation failed");
171 dm_pool_destroy(dtree->mem);
172 dm_free(dtree);
3d0480ed
AK
173 return NULL;
174 }
175
b4f1578f
AK
176 if (!(dtree->uuids = dm_hash_create(32))) {
177 log_error("dtree uuid hash creation failed");
178 dm_hash_destroy(dtree->devs);
179 dm_pool_destroy(dtree->mem);
180 dm_free(dtree);
165e4a11
AK
181 return NULL;
182 }
183
b4f1578f 184 return dtree;
3d0480ed
AK
185}
186
b4f1578f 187void dm_tree_free(struct dm_tree *dtree)
3d0480ed 188{
b4f1578f 189 if (!dtree)
3d0480ed
AK
190 return;
191
b4f1578f
AK
192 dm_hash_destroy(dtree->uuids);
193 dm_hash_destroy(dtree->devs);
194 dm_pool_destroy(dtree->mem);
195 dm_free(dtree);
3d0480ed
AK
196}
197
b4f1578f
AK
198static int _nodes_are_linked(struct dm_tree_node *parent,
199 struct dm_tree_node *child)
3d0480ed 200{
b4f1578f 201 struct dm_tree_link *dlink;
3d0480ed 202
165e4a11 203 list_iterate_items(dlink, &parent->uses)
3d0480ed
AK
204 if (dlink->node == child)
205 return 1;
3d0480ed
AK
206
207 return 0;
208}
209
b4f1578f 210static int _link(struct list *list, struct dm_tree_node *node)
3d0480ed 211{
b4f1578f 212 struct dm_tree_link *dlink;
3d0480ed 213
b4f1578f
AK
214 if (!(dlink = dm_pool_alloc(node->dtree->mem, sizeof(*dlink)))) {
215 log_error("dtree link allocation failed");
3d0480ed
AK
216 return 0;
217 }
218
219 dlink->node = node;
220 list_add(list, &dlink->list);
221
222 return 1;
223}
224
b4f1578f
AK
225static int _link_nodes(struct dm_tree_node *parent,
226 struct dm_tree_node *child)
3d0480ed
AK
227{
228 if (_nodes_are_linked(parent, child))
229 return 1;
230
231 if (!_link(&parent->uses, child))
232 return 0;
233
234 if (!_link(&child->used_by, parent))
235 return 0;
236
237 return 1;
238}
239
b4f1578f 240static void _unlink(struct list *list, struct dm_tree_node *node)
3d0480ed 241{
b4f1578f 242 struct dm_tree_link *dlink;
3d0480ed 243
165e4a11 244 list_iterate_items(dlink, list)
3d0480ed
AK
245 if (dlink->node == node) {
246 list_del(&dlink->list);
247 break;
248 }
3d0480ed
AK
249}
250
b4f1578f
AK
251static void _unlink_nodes(struct dm_tree_node *parent,
252 struct dm_tree_node *child)
3d0480ed
AK
253{
254 if (!_nodes_are_linked(parent, child))
255 return;
256
257 _unlink(&parent->uses, child);
258 _unlink(&child->used_by, parent);
259}
260
b4f1578f 261static int _add_to_toplevel(struct dm_tree_node *node)
165e4a11 262{
b4f1578f 263 return _link_nodes(&node->dtree->root, node);
165e4a11
AK
264}
265
b4f1578f 266static void _remove_from_toplevel(struct dm_tree_node *node)
3d0480ed 267{
b4f1578f 268 return _unlink_nodes(&node->dtree->root, node);
3d0480ed
AK
269}
270
b4f1578f 271static int _add_to_bottomlevel(struct dm_tree_node *node)
3d0480ed 272{
b4f1578f 273 return _link_nodes(node, &node->dtree->root);
3d0480ed
AK
274}
275
b4f1578f 276static void _remove_from_bottomlevel(struct dm_tree_node *node)
165e4a11 277{
b4f1578f 278 return _unlink_nodes(node, &node->dtree->root);
165e4a11
AK
279}
280
b4f1578f 281static int _link_tree_nodes(struct dm_tree_node *parent, struct dm_tree_node *child)
165e4a11
AK
282{
283 /* Don't link to root node if child already has a parent */
b4f1578f
AK
284 if ((parent == &parent->dtree->root)) {
285 if (dm_tree_node_num_children(child, 1))
165e4a11
AK
286 return 1;
287 } else
288 _remove_from_toplevel(child);
289
b4f1578f
AK
290 if ((child == &child->dtree->root)) {
291 if (dm_tree_node_num_children(parent, 0))
165e4a11
AK
292 return 1;
293 } else
294 _remove_from_bottomlevel(parent);
295
296 return _link_nodes(parent, child);
297}
298
b4f1578f 299static struct dm_tree_node *_create_dm_tree_node(struct dm_tree *dtree,
3d0480ed
AK
300 const char *name,
301 const char *uuid,
165e4a11
AK
302 struct dm_info *info,
303 void *context)
3d0480ed 304{
b4f1578f 305 struct dm_tree_node *node;
3d0480ed
AK
306 uint64_t dev;
307
b4f1578f
AK
308 if (!(node = dm_pool_zalloc(dtree->mem, sizeof(*node)))) {
309 log_error("_create_dm_tree_node alloc failed");
3d0480ed
AK
310 return NULL;
311 }
312
b4f1578f 313 node->dtree = dtree;
3d0480ed
AK
314
315 node->name = name;
316 node->uuid = uuid;
317 node->info = *info;
165e4a11 318 node->context = context;
56c28292 319 node->activation_priority = 0;
3d0480ed
AK
320
321 list_init(&node->uses);
322 list_init(&node->used_by);
165e4a11 323 list_init(&node->props.segs);
3d0480ed
AK
324
325 dev = MKDEV(info->major, info->minor);
326
b4f1578f 327 if (!dm_hash_insert_binary(dtree->devs, (const char *) &dev,
3d0480ed 328 sizeof(dev), node)) {
b4f1578f
AK
329 log_error("dtree node hash insertion failed");
330 dm_pool_free(dtree->mem, node);
3d0480ed
AK
331 return NULL;
332 }
333
165e4a11 334 if (uuid && *uuid &&
b4f1578f
AK
335 !dm_hash_insert(dtree->uuids, uuid, node)) {
336 log_error("dtree uuid hash insertion failed");
337 dm_hash_remove_binary(dtree->devs, (const char *) &dev,
165e4a11 338 sizeof(dev));
b4f1578f 339 dm_pool_free(dtree->mem, node);
165e4a11
AK
340 return NULL;
341 }
342
3d0480ed
AK
343 return node;
344}
345
b4f1578f 346static struct dm_tree_node *_find_dm_tree_node(struct dm_tree *dtree,
3d0480ed
AK
347 uint32_t major, uint32_t minor)
348{
349 uint64_t dev = MKDEV(major, minor);
350
b4f1578f 351 return dm_hash_lookup_binary(dtree->devs, (const char *) &dev,
3d0480ed
AK
352 sizeof(dev));
353}
354
b4f1578f 355static struct dm_tree_node *_find_dm_tree_node_by_uuid(struct dm_tree *dtree,
165e4a11
AK
356 const char *uuid)
357{
87f98002
AK
358 struct dm_tree_node *node;
359
360 if ((node = dm_hash_lookup(dtree->uuids, uuid)))
361 return node;
362
363 if (strncmp(uuid, UUID_PREFIX, sizeof(UUID_PREFIX) - 1))
364 return NULL;
365
366 return dm_hash_lookup(dtree->uuids, uuid + sizeof(UUID_PREFIX) - 1);
165e4a11
AK
367}
368
a3f6b2ce 369static int _deps(struct dm_task **dmt, struct dm_pool *mem, uint32_t major, uint32_t minor,
3d0480ed
AK
370 const char **name, const char **uuid,
371 struct dm_info *info, struct dm_deps **deps)
372{
373 memset(info, 0, sizeof(*info));
374
375 if (!dm_is_dm_major(major)) {
376 *name = "";
377 *uuid = "";
378 *deps = NULL;
379 info->major = major;
380 info->minor = minor;
381 info->exists = 0;
165e4a11
AK
382 info->live_table = 0;
383 info->inactive_table = 0;
384 info->read_only = 0;
3d0480ed
AK
385 return 1;
386 }
387
388 if (!(*dmt = dm_task_create(DM_DEVICE_DEPS))) {
389 log_error("deps dm_task creation failed");
390 return 0;
391 }
392
b4f1578f
AK
393 if (!dm_task_set_major(*dmt, major)) {
394 log_error("_deps: failed to set major for (%" PRIu32 ":%" PRIu32 ")",
395 major, minor);
3d0480ed 396 goto failed;
b4f1578f 397 }
3d0480ed 398
b4f1578f
AK
399 if (!dm_task_set_minor(*dmt, minor)) {
400 log_error("_deps: failed to set minor for (%" PRIu32 ":%" PRIu32 ")",
401 major, minor);
3d0480ed 402 goto failed;
b4f1578f 403 }
3d0480ed 404
b4f1578f
AK
405 if (!dm_task_run(*dmt)) {
406 log_error("_deps: task run failed for (%" PRIu32 ":%" PRIu32 ")",
407 major, minor);
3d0480ed 408 goto failed;
b4f1578f 409 }
3d0480ed 410
b4f1578f
AK
411 if (!dm_task_get_info(*dmt, info)) {
412 log_error("_deps: failed to get info for (%" PRIu32 ":%" PRIu32 ")",
413 major, minor);
3d0480ed 414 goto failed;
b4f1578f 415 }
3d0480ed
AK
416
417 if (!info->exists) {
418 *name = "";
419 *uuid = "";
420 *deps = NULL;
421 } else {
422 if (info->major != major) {
b4f1578f 423 log_error("Inconsistent dtree major number: %u != %u",
3d0480ed
AK
424 major, info->major);
425 goto failed;
426 }
427 if (info->minor != minor) {
b4f1578f 428 log_error("Inconsistent dtree minor number: %u != %u",
3d0480ed
AK
429 minor, info->minor);
430 goto failed;
431 }
a3f6b2ce 432 if (!(*name = dm_pool_strdup(mem, dm_task_get_name(*dmt)))) {
3d0480ed
AK
433 log_error("name pool_strdup failed");
434 goto failed;
435 }
a3f6b2ce 436 if (!(*uuid = dm_pool_strdup(mem, dm_task_get_uuid(*dmt)))) {
3d0480ed
AK
437 log_error("uuid pool_strdup failed");
438 goto failed;
439 }
440 *deps = dm_task_get_deps(*dmt);
441 }
442
443 return 1;
444
445failed:
446 dm_task_destroy(*dmt);
447 return 0;
448}
449
b4f1578f
AK
450static struct dm_tree_node *_add_dev(struct dm_tree *dtree,
451 struct dm_tree_node *parent,
165e4a11 452 uint32_t major, uint32_t minor)
3d0480ed
AK
453{
454 struct dm_task *dmt = NULL;
455 struct dm_info info;
456 struct dm_deps *deps = NULL;
457 const char *name = NULL;
458 const char *uuid = NULL;
b4f1578f 459 struct dm_tree_node *node = NULL;
3d0480ed 460 uint32_t i;
3d0480ed
AK
461 int new = 0;
462
463 /* Already in tree? */
b4f1578f
AK
464 if (!(node = _find_dm_tree_node(dtree, major, minor))) {
465 if (!_deps(&dmt, dtree->mem, major, minor, &name, &uuid, &info, &deps))
466 return_NULL;
3d0480ed 467
b4f1578f 468 if (!(node = _create_dm_tree_node(dtree, name, uuid,
165e4a11 469 &info, NULL)))
b4f1578f 470 goto_out;
3d0480ed
AK
471 new = 1;
472 }
473
165e4a11
AK
474 if (!_link_tree_nodes(parent, node)) {
475 node = NULL;
b4f1578f 476 goto_out;
165e4a11 477 }
3d0480ed
AK
478
479 /* If node was already in tree, no need to recurse. */
480 if (!new)
165e4a11 481 goto out;
3d0480ed
AK
482
483 /* Can't recurse if not a mapped device or there are no dependencies */
484 if (!node->info.exists || !deps->count) {
b4f1578f
AK
485 if (!_add_to_bottomlevel(node)) {
486 stack;
165e4a11 487 node = NULL;
b4f1578f 488 }
165e4a11 489 goto out;
3d0480ed
AK
490 }
491
492 /* Add dependencies to tree */
493 for (i = 0; i < deps->count; i++)
b4f1578f 494 if (!_add_dev(dtree, node, MAJOR(deps->device[i]),
165e4a11
AK
495 MINOR(deps->device[i]))) {
496 node = NULL;
b4f1578f 497 goto_out;
165e4a11 498 }
3d0480ed 499
3d0480ed
AK
500out:
501 if (dmt)
502 dm_task_destroy(dmt);
503
165e4a11
AK
504 return node;
505}
506
b4f1578f 507static int _node_clear_table(struct dm_tree_node *dnode)
165e4a11
AK
508{
509 struct dm_task *dmt;
510 struct dm_info *info;
511 const char *name;
512 int r;
513
514 if (!(info = &dnode->info)) {
b4f1578f 515 log_error("_node_clear_table failed: missing info");
165e4a11
AK
516 return 0;
517 }
518
b4f1578f
AK
519 if (!(name = dm_tree_node_get_name(dnode))) {
520 log_error("_node_clear_table failed: missing name");
165e4a11
AK
521 return 0;
522 }
523
524 /* Is there a table? */
525 if (!info->exists || !info->inactive_table)
526 return 1;
527
528 log_verbose("Clearing inactive table %s (%" PRIu32 ":%" PRIu32 ")",
529 name, info->major, info->minor);
530
531 if (!(dmt = dm_task_create(DM_DEVICE_CLEAR))) {
532 dm_task_destroy(dmt);
533 log_error("Table clear dm_task creation failed for %s", name);
534 return 0;
535 }
536
537 if (!dm_task_set_major(dmt, info->major) ||
538 !dm_task_set_minor(dmt, info->minor)) {
539 log_error("Failed to set device number for %s table clear", name);
540 dm_task_destroy(dmt);
541 return 0;
542 }
543
544 r = dm_task_run(dmt);
545
546 if (!dm_task_get_info(dmt, info)) {
b4f1578f 547 log_error("_node_clear_table failed: info missing after running task for %s", name);
165e4a11
AK
548 r = 0;
549 }
550
551 dm_task_destroy(dmt);
552
3d0480ed
AK
553 return r;
554}
555
b4f1578f 556struct dm_tree_node *dm_tree_add_new_dev(struct dm_tree *dtree,
165e4a11
AK
557 const char *name,
558 const char *uuid,
559 uint32_t major, uint32_t minor,
560 int read_only,
561 int clear_inactive,
562 void *context)
563{
b4f1578f 564 struct dm_tree_node *dnode;
165e4a11
AK
565 struct dm_info info;
566 const char *name2;
567 const char *uuid2;
568
569 /* Do we need to add node to tree? */
b4f1578f
AK
570 if (!(dnode = dm_tree_find_node_by_uuid(dtree, uuid))) {
571 if (!(name2 = dm_pool_strdup(dtree->mem, name))) {
165e4a11
AK
572 log_error("name pool_strdup failed");
573 return NULL;
574 }
b4f1578f 575 if (!(uuid2 = dm_pool_strdup(dtree->mem, uuid))) {
165e4a11
AK
576 log_error("uuid pool_strdup failed");
577 return NULL;
578 }
579
580 info.major = 0;
581 info.minor = 0;
582 info.exists = 0;
583 info.live_table = 0;
584 info.inactive_table = 0;
585 info.read_only = 0;
586
b4f1578f
AK
587 if (!(dnode = _create_dm_tree_node(dtree, name2, uuid2,
588 &info, context)))
589 return_NULL;
165e4a11
AK
590
591 /* Attach to root node until a table is supplied */
b4f1578f
AK
592 if (!_add_to_toplevel(dnode) || !_add_to_bottomlevel(dnode))
593 return_NULL;
165e4a11
AK
594
595 dnode->props.major = major;
596 dnode->props.minor = minor;
597 dnode->props.new_name = NULL;
598 } else if (strcmp(name, dnode->name)) {
599 /* Do we need to rename node? */
b4f1578f 600 if (!(dnode->props.new_name = dm_pool_strdup(dtree->mem, name))) {
165e4a11
AK
601 log_error("name pool_strdup failed");
602 return 0;
603 }
604 }
605
606 dnode->props.read_only = read_only ? 1 : 0;
607
b4f1578f
AK
608 if (clear_inactive && !_node_clear_table(dnode))
609 return_NULL;
165e4a11
AK
610
611 dnode->context = context;
612
613 return dnode;
614}
615
b4f1578f 616int dm_tree_add_dev(struct dm_tree *dtree, uint32_t major, uint32_t minor)
3d0480ed 617{
b4f1578f 618 return _add_dev(dtree, &dtree->root, major, minor) ? 1 : 0;
3d0480ed
AK
619}
620
b4f1578f 621const char *dm_tree_node_get_name(struct dm_tree_node *node)
3d0480ed
AK
622{
623 return node->info.exists ? node->name : "";
624}
625
b4f1578f 626const char *dm_tree_node_get_uuid(struct dm_tree_node *node)
3d0480ed
AK
627{
628 return node->info.exists ? node->uuid : "";
629}
630
b4f1578f 631const struct dm_info *dm_tree_node_get_info(struct dm_tree_node *node)
3d0480ed
AK
632{
633 return &node->info;
634}
635
b4f1578f 636void *dm_tree_node_get_context(struct dm_tree_node *node)
165e4a11
AK
637{
638 return node->context;
639}
640
b4f1578f 641int dm_tree_node_num_children(struct dm_tree_node *node, uint32_t inverted)
3d0480ed
AK
642{
643 if (inverted) {
b4f1578f 644 if (_nodes_are_linked(&node->dtree->root, node))
3d0480ed
AK
645 return 0;
646 return list_size(&node->used_by);
647 }
648
b4f1578f 649 if (_nodes_are_linked(node, &node->dtree->root))
3d0480ed
AK
650 return 0;
651
652 return list_size(&node->uses);
653}
654
2b69db1f
AK
655/*
656 * Returns 1 if no prefix supplied
657 */
658static int _uuid_prefix_matches(const char *uuid, const char *uuid_prefix, size_t uuid_prefix_len)
659{
660 if (!uuid_prefix)
661 return 1;
662
663 if (!strncmp(uuid, uuid_prefix, uuid_prefix_len))
664 return 1;
665
666 /* Handle transition: active device uuids might be missing the prefix */
667 if (uuid_prefix_len <= 4)
668 return 0;
669
87f98002 670 if (!strncmp(uuid, UUID_PREFIX, sizeof(UUID_PREFIX) - 1))
872dea04
AK
671 return 0;
672
87f98002 673 if (strncmp(uuid_prefix, UUID_PREFIX, sizeof(UUID_PREFIX) - 1))
2b69db1f
AK
674 return 0;
675
87f98002 676 if (!strncmp(uuid, uuid_prefix + sizeof(UUID_PREFIX) - 1, uuid_prefix_len - (sizeof(UUID_PREFIX) - 1)))
2b69db1f
AK
677 return 1;
678
679 return 0;
680}
681
690a5da2
AK
682/*
683 * Returns 1 if no children.
684 */
b4f1578f 685static int _children_suspended(struct dm_tree_node *node,
690a5da2
AK
686 uint32_t inverted,
687 const char *uuid_prefix,
688 size_t uuid_prefix_len)
689{
690 struct list *list;
b4f1578f 691 struct dm_tree_link *dlink;
690a5da2
AK
692 const struct dm_info *dinfo;
693 const char *uuid;
694
695 if (inverted) {
b4f1578f 696 if (_nodes_are_linked(&node->dtree->root, node))
690a5da2
AK
697 return 1;
698 list = &node->used_by;
699 } else {
b4f1578f 700 if (_nodes_are_linked(node, &node->dtree->root))
690a5da2
AK
701 return 1;
702 list = &node->uses;
703 }
704
705 list_iterate_items(dlink, list) {
b4f1578f 706 if (!(uuid = dm_tree_node_get_uuid(dlink->node))) {
690a5da2
AK
707 stack;
708 continue;
709 }
710
711 /* Ignore if it doesn't belong to this VG */
2b69db1f 712 if (!_uuid_prefix_matches(uuid, uuid_prefix, uuid_prefix_len))
690a5da2
AK
713 continue;
714
b4f1578f
AK
715 if (!(dinfo = dm_tree_node_get_info(dlink->node))) {
716 stack; /* FIXME Is this normal? */
690a5da2
AK
717 return 0;
718 }
719
720 if (!dinfo->suspended)
721 return 0;
722 }
723
724 return 1;
725}
726
3d0480ed
AK
727/*
728 * Set major and minor to zero for root of tree.
729 */
b4f1578f 730struct dm_tree_node *dm_tree_find_node(struct dm_tree *dtree,
3d0480ed
AK
731 uint32_t major,
732 uint32_t minor)
733{
734 if (!major && !minor)
b4f1578f 735 return &dtree->root;
3d0480ed 736
b4f1578f 737 return _find_dm_tree_node(dtree, major, minor);
3d0480ed
AK
738}
739
165e4a11
AK
740/*
741 * Set uuid to NULL for root of tree.
742 */
b4f1578f 743struct dm_tree_node *dm_tree_find_node_by_uuid(struct dm_tree *dtree,
165e4a11
AK
744 const char *uuid)
745{
746 if (!uuid || !*uuid)
b4f1578f 747 return &dtree->root;
165e4a11 748
b4f1578f 749 return _find_dm_tree_node_by_uuid(dtree, uuid);
165e4a11
AK
750}
751
3d0480ed
AK
752/*
753 * First time set *handle to NULL.
754 * Set inverted to invert the tree.
755 */
b4f1578f
AK
756struct dm_tree_node *dm_tree_next_child(void **handle,
757 struct dm_tree_node *parent,
3d0480ed
AK
758 uint32_t inverted)
759{
760 struct list **dlink = (struct list **) handle;
761 struct list *use_list;
762
763 if (inverted)
764 use_list = &parent->used_by;
765 else
766 use_list = &parent->uses;
767
768 if (!*dlink)
769 *dlink = list_first(use_list);
770 else
771 *dlink = list_next(use_list, *dlink);
772
b4f1578f 773 return (*dlink) ? list_item(*dlink, struct dm_tree_link)->node : NULL;
3d0480ed
AK
774}
775
3e8c6b73 776/*
a6d97ede 777 * Deactivate a device with its dependencies if the uuid prefix matches.
3e8c6b73 778 */
db208f51
AK
779static int _info_by_dev(uint32_t major, uint32_t minor, int with_open_count,
780 struct dm_info *info)
3e8c6b73
AK
781{
782 struct dm_task *dmt;
783 int r;
784
785 if (!(dmt = dm_task_create(DM_DEVICE_INFO))) {
786 log_error("_info_by_dev: dm_task creation failed");
787 return 0;
788 }
789
790 if (!dm_task_set_major(dmt, major) || !dm_task_set_minor(dmt, minor)) {
791 log_error("_info_by_dev: Failed to set device number");
792 dm_task_destroy(dmt);
793 return 0;
794 }
795
db208f51
AK
796 if (!with_open_count && !dm_task_no_open_count(dmt))
797 log_error("Failed to disable open_count");
798
3e8c6b73
AK
799 if ((r = dm_task_run(dmt)))
800 r = dm_task_get_info(dmt, info);
801
802 dm_task_destroy(dmt);
803
804 return r;
805}
806
807static int _deactivate_node(const char *name, uint32_t major, uint32_t minor)
808{
809 struct dm_task *dmt;
810 int r;
811
812 log_verbose("Removing %s (%" PRIu32 ":%" PRIu32 ")", name, major, minor);
813
814 if (!(dmt = dm_task_create(DM_DEVICE_REMOVE))) {
815 log_error("Deactivation dm_task creation failed for %s", name);
816 return 0;
817 }
818
819 if (!dm_task_set_major(dmt, major) || !dm_task_set_minor(dmt, minor)) {
820 log_error("Failed to set device number for %s deactivation", name);
821 dm_task_destroy(dmt);
822 return 0;
823 }
824
825 if (!dm_task_no_open_count(dmt))
826 log_error("Failed to disable open_count");
827
828 r = dm_task_run(dmt);
829
165e4a11
AK
830 /* FIXME Until kernel returns actual name so dm-ioctl.c can handle it */
831 rm_dev_node(name);
832
db208f51
AK
833 /* FIXME Remove node from tree or mark invalid? */
834
835 dm_task_destroy(dmt);
836
837 return r;
838}
839
165e4a11
AK
840static int _rename_node(const char *old_name, const char *new_name, uint32_t major, uint32_t minor)
841{
842 struct dm_task *dmt;
843 int r = 0;
844
845 log_verbose("Renaming %s (%" PRIu32 ":%" PRIu32 ") to %s", old_name, major, minor, new_name);
846
847 if (!(dmt = dm_task_create(DM_DEVICE_RENAME))) {
848 log_error("Rename dm_task creation failed for %s", old_name);
849 return 0;
850 }
851
852 if (!dm_task_set_name(dmt, old_name)) {
853 log_error("Failed to set name for %s rename.", old_name);
854 goto out;
855 }
856
b4f1578f
AK
857 if (!dm_task_set_newname(dmt, new_name))
858 goto_out;
165e4a11
AK
859
860 if (!dm_task_no_open_count(dmt))
861 log_error("Failed to disable open_count");
862
863 r = dm_task_run(dmt);
864
865out:
866 dm_task_destroy(dmt);
867
868 return r;
869}
870
165e4a11
AK
871/* FIXME Merge with _suspend_node? */
872static int _resume_node(const char *name, uint32_t major, uint32_t minor,
873 struct dm_info *newinfo)
874{
875 struct dm_task *dmt;
876 int r;
877
878 log_verbose("Resuming %s (%" PRIu32 ":%" PRIu32 ")", name, major, minor);
879
880 if (!(dmt = dm_task_create(DM_DEVICE_RESUME))) {
881 log_error("Suspend dm_task creation failed for %s", name);
882 return 0;
883 }
884
885 if (!dm_task_set_major(dmt, major) || !dm_task_set_minor(dmt, minor)) {
886 log_error("Failed to set device number for %s resumption.", name);
887 dm_task_destroy(dmt);
888 return 0;
889 }
890
891 if (!dm_task_no_open_count(dmt))
892 log_error("Failed to disable open_count");
893
894 if ((r = dm_task_run(dmt)))
895 r = dm_task_get_info(dmt, newinfo);
896
897 dm_task_destroy(dmt);
898
899 return r;
900}
901
db208f51
AK
902static int _suspend_node(const char *name, uint32_t major, uint32_t minor,
903 struct dm_info *newinfo)
904{
905 struct dm_task *dmt;
906 int r;
907
908 log_verbose("Suspending %s (%" PRIu32 ":%" PRIu32 ")", name, major, minor);
909
910 if (!(dmt = dm_task_create(DM_DEVICE_SUSPEND))) {
911 log_error("Suspend dm_task creation failed for %s", name);
912 return 0;
913 }
914
915 if (!dm_task_set_major(dmt, major) || !dm_task_set_minor(dmt, minor)) {
916 log_error("Failed to set device number for %s suspension.", name);
917 dm_task_destroy(dmt);
918 return 0;
919 }
920
921 if (!dm_task_no_open_count(dmt))
922 log_error("Failed to disable open_count");
923
924 if ((r = dm_task_run(dmt)))
925 r = dm_task_get_info(dmt, newinfo);
926
3e8c6b73
AK
927 dm_task_destroy(dmt);
928
929 return r;
930}
931
b4f1578f 932int dm_tree_deactivate_children(struct dm_tree_node *dnode,
a6d97ede
AK
933 const char *uuid_prefix,
934 size_t uuid_prefix_len)
3e8c6b73
AK
935{
936 void *handle = NULL;
b4f1578f 937 struct dm_tree_node *child = dnode;
3e8c6b73
AK
938 struct dm_info info;
939 const struct dm_info *dinfo;
940 const char *name;
941 const char *uuid;
942
b4f1578f
AK
943 while ((child = dm_tree_next_child(&handle, dnode, 0))) {
944 if (!(dinfo = dm_tree_node_get_info(child))) {
3e8c6b73
AK
945 stack;
946 continue;
947 }
948
b4f1578f 949 if (!(name = dm_tree_node_get_name(child))) {
3e8c6b73
AK
950 stack;
951 continue;
952 }
953
b4f1578f 954 if (!(uuid = dm_tree_node_get_uuid(child))) {
3e8c6b73
AK
955 stack;
956 continue;
957 }
958
959 /* Ignore if it doesn't belong to this VG */
2b69db1f 960 if (!_uuid_prefix_matches(uuid, uuid_prefix, uuid_prefix_len))
3e8c6b73 961 continue;
3e8c6b73
AK
962
963 /* Refresh open_count */
db208f51 964 if (!_info_by_dev(dinfo->major, dinfo->minor, 1, &info) ||
3e8c6b73
AK
965 !info.exists || info.open_count)
966 continue;
967
968 if (!_deactivate_node(name, info.major, info.minor)) {
969 log_error("Unable to deactivate %s (%" PRIu32
970 ":%" PRIu32 ")", name, info.major,
971 info.minor);
972 continue;
973 }
974
b4f1578f
AK
975 if (dm_tree_node_num_children(child, 0))
976 dm_tree_deactivate_children(child, uuid_prefix, uuid_prefix_len);
3e8c6b73
AK
977 }
978
979 return 1;
980}
db208f51 981
b4f1578f 982int dm_tree_suspend_children(struct dm_tree_node *dnode,
db208f51
AK
983 const char *uuid_prefix,
984 size_t uuid_prefix_len)
985{
986 void *handle = NULL;
b4f1578f 987 struct dm_tree_node *child = dnode;
db208f51
AK
988 struct dm_info info, newinfo;
989 const struct dm_info *dinfo;
990 const char *name;
991 const char *uuid;
992
690a5da2 993 /* Suspend nodes at this level of the tree */
b4f1578f
AK
994 while ((child = dm_tree_next_child(&handle, dnode, 0))) {
995 if (!(dinfo = dm_tree_node_get_info(child))) {
db208f51
AK
996 stack;
997 continue;
998 }
999
b4f1578f 1000 if (!(name = dm_tree_node_get_name(child))) {
db208f51
AK
1001 stack;
1002 continue;
1003 }
1004
b4f1578f 1005 if (!(uuid = dm_tree_node_get_uuid(child))) {
db208f51
AK
1006 stack;
1007 continue;
1008 }
1009
1010 /* Ignore if it doesn't belong to this VG */
2b69db1f 1011 if (!_uuid_prefix_matches(uuid, uuid_prefix, uuid_prefix_len))
db208f51
AK
1012 continue;
1013
690a5da2
AK
1014 /* Ensure immediate parents are already suspended */
1015 if (!_children_suspended(child, 1, uuid_prefix, uuid_prefix_len))
1016 continue;
1017
db208f51
AK
1018 if (!_info_by_dev(dinfo->major, dinfo->minor, 0, &info) ||
1019 !info.exists)
1020 continue;
1021
1022 if (!_suspend_node(name, info.major, info.minor, &newinfo)) {
1023 log_error("Unable to suspend %s (%" PRIu32
1024 ":%" PRIu32 ")", name, info.major,
1025 info.minor);
1026 continue;
1027 }
1028
1029 /* Update cached info */
1030 child->info = newinfo;
690a5da2
AK
1031 }
1032
1033 /* Then suspend any child nodes */
1034 handle = NULL;
1035
b4f1578f
AK
1036 while ((child = dm_tree_next_child(&handle, dnode, 0))) {
1037 if (!(uuid = dm_tree_node_get_uuid(child))) {
690a5da2
AK
1038 stack;
1039 continue;
1040 }
1041
1042 /* Ignore if it doesn't belong to this VG */
87f98002 1043 if (!_uuid_prefix_matches(uuid, uuid_prefix, uuid_prefix_len))
690a5da2 1044 continue;
db208f51 1045
b4f1578f
AK
1046 if (dm_tree_node_num_children(child, 0))
1047 dm_tree_suspend_children(child, uuid_prefix, uuid_prefix_len);
db208f51
AK
1048 }
1049
1050 return 1;
1051}
1052
b4f1578f 1053int dm_tree_activate_children(struct dm_tree_node *dnode,
db208f51
AK
1054 const char *uuid_prefix,
1055 size_t uuid_prefix_len)
1056{
1057 void *handle = NULL;
b4f1578f 1058 struct dm_tree_node *child = dnode;
165e4a11
AK
1059 struct dm_info newinfo;
1060 const char *name;
db208f51 1061 const char *uuid;
56c28292 1062 int priority;
db208f51 1063
165e4a11 1064 /* Activate children first */
b4f1578f
AK
1065 while ((child = dm_tree_next_child(&handle, dnode, 0))) {
1066 if (!(uuid = dm_tree_node_get_uuid(child))) {
165e4a11
AK
1067 stack;
1068 continue;
db208f51
AK
1069 }
1070
908db078
AK
1071 if (!_uuid_prefix_matches(uuid, uuid_prefix, uuid_prefix_len))
1072 continue;
db208f51 1073
b4f1578f
AK
1074 if (dm_tree_node_num_children(child, 0))
1075 dm_tree_activate_children(child, uuid_prefix, uuid_prefix_len);
56c28292 1076 }
165e4a11 1077
56c28292 1078 handle = NULL;
165e4a11 1079
56c28292
AK
1080 for (priority = 0; priority < 2; priority++) {
1081 while ((child = dm_tree_next_child(&handle, dnode, 0))) {
1082 if (!(uuid = dm_tree_node_get_uuid(child))) {
1083 stack;
1084 continue;
165e4a11 1085 }
165e4a11 1086
56c28292
AK
1087 if (!_uuid_prefix_matches(uuid, uuid_prefix, uuid_prefix_len))
1088 continue;
165e4a11 1089
56c28292
AK
1090 if (priority != child->activation_priority)
1091 continue;
165e4a11 1092
56c28292
AK
1093 if (!(name = dm_tree_node_get_name(child))) {
1094 stack;
1095 continue;
1096 }
1097
1098 /* Rename? */
1099 if (child->props.new_name) {
1100 if (!_rename_node(name, child->props.new_name, child->info.major, child->info.minor)) {
1101 log_error("Failed to rename %s (%" PRIu32
1102 ":%" PRIu32 ") to %s", name, child->info.major,
1103 child->info.minor, child->props.new_name);
1104 return 0;
1105 }
1106 child->name = child->props.new_name;
1107 child->props.new_name = NULL;
1108 }
1109
1110 if (!child->info.inactive_table && !child->info.suspended)
1111 continue;
1112
1113 if (!_resume_node(name, child->info.major, child->info.minor, &newinfo)) {
1114 log_error("Unable to resume %s (%" PRIu32
1115 ":%" PRIu32 ")", name, child->info.major,
1116 child->info.minor);
1117 continue;
1118 }
1119
1120 /* Update cached info */
1121 child->info = newinfo;
1122 }
db208f51
AK
1123 }
1124
165e4a11
AK
1125 handle = NULL;
1126
1127 return 1;
1128}
1129
b4f1578f 1130static int _create_node(struct dm_tree_node *dnode)
165e4a11
AK
1131{
1132 int r = 0;
1133 struct dm_task *dmt;
1134
1135 log_verbose("Creating %s", dnode->name);
1136
1137 if (!(dmt = dm_task_create(DM_DEVICE_CREATE))) {
1138 log_error("Create dm_task creation failed for %s", dnode->name);
1139 return 0;
1140 }
1141
1142 if (!dm_task_set_name(dmt, dnode->name)) {
1143 log_error("Failed to set device name for %s", dnode->name);
1144 goto out;
1145 }
1146
1147 if (!dm_task_set_uuid(dmt, dnode->uuid)) {
1148 log_error("Failed to set uuid for %s", dnode->name);
1149 goto out;
1150 }
1151
1152 if (dnode->props.major &&
1153 (!dm_task_set_major(dmt, dnode->props.major) ||
1154 !dm_task_set_minor(dmt, dnode->props.minor))) {
1155 log_error("Failed to set device number for %s creation.", dnode->name);
1156 goto out;
1157 }
1158
1159 if (dnode->props.read_only && !dm_task_set_ro(dmt)) {
1160 log_error("Failed to set read only flag for %s", dnode->name);
1161 goto out;
1162 }
1163
1164 if (!dm_task_no_open_count(dmt))
1165 log_error("Failed to disable open_count");
1166
1167 if ((r = dm_task_run(dmt)))
1168 r = dm_task_get_info(dmt, &dnode->info);
1169
1170out:
1171 dm_task_destroy(dmt);
1172
1173 return r;
1174}
1175
1176
b4f1578f 1177static int _build_dev_string(char *devbuf, size_t bufsize, struct dm_tree_node *node)
165e4a11
AK
1178{
1179 if (!dm_format_dev(devbuf, bufsize, node->info.major, node->info.minor)) {
1180 log_error("Failed to format %s device number for %s as dm "
1181 "target (%u,%u)",
1182 node->name, node->uuid, node->info.major, node->info.minor);
1183 return 0;
1184 }
1185
1186 return 1;
1187}
1188
1189static int _emit_areas_line(struct dm_task *dmt, struct load_segment *seg, char *params, size_t paramsize, int *pos)
1190{
1191 struct seg_area *area;
1192 char devbuf[10];
1193 int tw;
1194 const char *prefix = "";
1195
1196 list_iterate_items(area, &seg->areas) {
b4f1578f
AK
1197 if (!_build_dev_string(devbuf, sizeof(devbuf), area->dev_node))
1198 return_0;
165e4a11
AK
1199
1200 if ((tw = _dm_snprintf(params + *pos, paramsize - *pos, "%s%s %" PRIu64,
1201 prefix, devbuf, area->offset)) < 0) {
b4f1578f 1202 stack; /* Out of space */
165e4a11
AK
1203 return -1;
1204 }
1205
1206 prefix = " ";
1207 *pos += tw;
1208 }
1209
1210 return 1;
1211}
1212
1213static int _emit_segment_line(struct dm_task *dmt, struct load_segment *seg, uint64_t *seg_start, char *params, size_t paramsize)
1214{
dbcb64b8 1215 unsigned log_parm_count;
165e4a11
AK
1216 int pos = 0;
1217 int tw;
1218 int r;
1219 char originbuf[10], cowbuf[10], logbuf[10];
dbcb64b8 1220 const char *logtype;
165e4a11
AK
1221
1222 switch(seg->type) {
1223 case SEG_ERROR:
1224 case SEG_ZERO:
1225 params[0] = '\0';
1226 case SEG_LINEAR:
1227 break;
1228 case SEG_MIRRORED:
1229 if (seg->clustered) {
1230 if ((tw = _dm_snprintf(params + pos, paramsize - pos, "clustered ")) < 0) {
b4f1578f 1231 stack; /* Out of space */
165e4a11
AK
1232 return -1;
1233 }
1234 pos += tw;
1235 }
dbcb64b8
AK
1236
1237 log_parm_count = hweight32(seg->flags) + 1;
1238
1239 if (!seg->log)
1240 logtype = "core";
1241 else {
1242 logtype = "disk";
1243 log_parm_count++;
b4f1578f
AK
1244 if (!_build_dev_string(logbuf, sizeof(logbuf), seg->log))
1245 return_0;
dbcb64b8
AK
1246 }
1247
1248 if ((tw = _dm_snprintf(params + pos, paramsize - pos, "%s %u ", logtype, log_parm_count)) < 0) {
1249 stack; /* Out of space */
1250 return -1;
1251 }
1252 pos += tw;
1253
1254 if (seg->log) {
1255 if ((tw = _dm_snprintf(params + pos, paramsize - pos, "%s ", logbuf)) < 0) {
b4f1578f 1256 stack; /* Out of space */
165e4a11
AK
1257 return -1;
1258 }
1259 pos += tw;
1260 }
dbcb64b8
AK
1261
1262 if ((tw = _dm_snprintf(params + pos, paramsize - pos, "%u ", seg->region_size)) < 0) {
b4f1578f 1263 stack; /* Out of space */
165e4a11
AK
1264 return -1;
1265 }
1266 pos += tw;
dbcb64b8
AK
1267
1268 if ((seg->flags & DM_NOSYNC)) {
1269 if ((tw = _dm_snprintf(params + pos, paramsize - pos, "nosync ")) < 0) {
1270 stack; /* Out of space */
1271 return -1;
1272 }
1273 pos += tw;
1274 } else if ((seg->flags & DM_FORCESYNC)) {
1275 if ((tw = _dm_snprintf(params + pos, paramsize - pos, "sync ")) < 0) {
1276 stack; /* Out of space */
1277 return -1;
1278 }
1279 pos += tw;
1280 }
1281
1282 if ((seg->flags & DM_BLOCK_ON_ERROR)) {
1283 if ((tw = _dm_snprintf(params + pos, paramsize - pos, "block_on_error ")) < 0) {
1284 stack; /* Out of space */
1285 return -1;
1286 }
1287 pos += tw;
1288 }
1289
1290 if ((tw = _dm_snprintf(params + pos, paramsize - pos, "%u ", seg->mirror_area_count)) < 0) {
1291 stack; /* Out of space */
1292 return -1;
1293 }
1294 pos += tw;
1295
165e4a11
AK
1296 break;
1297 case SEG_SNAPSHOT:
b4f1578f
AK
1298 if (!_build_dev_string(originbuf, sizeof(originbuf), seg->origin))
1299 return_0;
1300 if (!_build_dev_string(cowbuf, sizeof(cowbuf), seg->cow))
1301 return_0;
165e4a11
AK
1302 if ((pos = _dm_snprintf(params, paramsize, "%s %s %c %d",
1303 originbuf, cowbuf,
1304 seg->persistent ? 'P' : 'N',
1305 seg->chunk_size)) < 0) {
b4f1578f 1306 stack; /* Out of space */
165e4a11
AK
1307 return -1;
1308 }
1309 break;
1310 case SEG_SNAPSHOT_ORIGIN:
b4f1578f
AK
1311 if (!_build_dev_string(originbuf, sizeof(originbuf), seg->origin))
1312 return_0;
165e4a11
AK
1313 if ((pos = _dm_snprintf(params, paramsize, "%s",
1314 originbuf)) < 0) {
b4f1578f 1315 stack; /* Out of space */
165e4a11
AK
1316 return -1;
1317 }
1318 break;
1319 case SEG_STRIPED:
1320 if ((pos = _dm_snprintf(params, paramsize, "%u %u ",
1321 seg->area_count,
1322 seg->stripe_size)) < 0) {
b4f1578f 1323 stack; /* Out of space */
165e4a11
AK
1324 return -1;
1325 }
1326 break;
1327 }
1328
1329 switch(seg->type) {
1330 case SEG_ERROR:
1331 case SEG_SNAPSHOT:
1332 case SEG_SNAPSHOT_ORIGIN:
1333 case SEG_ZERO:
1334 break;
1335 case SEG_LINEAR:
1336 case SEG_MIRRORED:
1337 case SEG_STRIPED:
1338 if ((r = _emit_areas_line(dmt, seg, params, paramsize, &pos)) <= 0) {
1339 stack;
1340 return r;
1341 }
1342 break;
1343 }
1344
1345 log_debug("Adding target: %" PRIu64 " %" PRIu64 " %s %s",
1346 *seg_start, seg->size, dm_segtypes[seg->type].target, params);
1347
b4f1578f
AK
1348 if (!dm_task_add_target(dmt, *seg_start, seg->size, dm_segtypes[seg->type].target, params))
1349 return_0;
165e4a11
AK
1350
1351 *seg_start += seg->size;
1352
1353 return 1;
1354}
1355
1356static int _emit_segment(struct dm_task *dmt, struct load_segment *seg,
1357 uint64_t *seg_start)
1358{
1359 char *params;
1360 size_t paramsize = 4096;
1361 int ret;
1362
1363 do {
1364 if (!(params = dm_malloc(paramsize))) {
1365 log_error("Insufficient space for target parameters.");
1366 return 0;
1367 }
1368
1369 ret = _emit_segment_line(dmt, seg, seg_start, params, paramsize);
1370 dm_free(params);
1371
1372 if (!ret)
1373 stack;
1374
1375 if (ret >= 0)
1376 return ret;
1377
1378 log_debug("Insufficient space in params[%" PRIsize_t
1379 "] for target parameters.", paramsize);
1380
1381 paramsize *= 2;
1382 } while (paramsize < MAX_TARGET_PARAMSIZE);
1383
1384 log_error("Target parameter size too big. Aborting.");
1385 return 0;
1386}
1387
b4f1578f 1388static int _load_node(struct dm_tree_node *dnode)
165e4a11
AK
1389{
1390 int r = 0;
1391 struct dm_task *dmt;
1392 struct load_segment *seg;
1393 uint64_t seg_start = 0;
1394
1395 log_verbose("Loading %s table", dnode->name);
1396
1397 if (!(dmt = dm_task_create(DM_DEVICE_RELOAD))) {
1398 log_error("Reload dm_task creation failed for %s", dnode->name);
1399 return 0;
1400 }
1401
1402 if (!dm_task_set_major(dmt, dnode->info.major) ||
1403 !dm_task_set_minor(dmt, dnode->info.minor)) {
1404 log_error("Failed to set device number for %s reload.", 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 list_iterate_items(seg, &dnode->props.segs)
b4f1578f
AK
1417 if (!_emit_segment(dmt, seg, &seg_start))
1418 goto_out;
165e4a11 1419
ec289b64
AK
1420 if (!dm_task_suppress_identical_reload(dmt))
1421 log_error("Failed to suppress reload of identical tables.");
1422
1423 if ((r = dm_task_run(dmt))) {
165e4a11 1424 r = dm_task_get_info(dmt, &dnode->info);
ec289b64
AK
1425 if (r && !dnode->info.inactive_table)
1426 log_verbose("Suppressed %s identical table reload.",
1427 dnode->name);
1428 }
165e4a11
AK
1429
1430 dnode->props.segment_count = 0;
1431
1432out:
1433 dm_task_destroy(dmt);
1434
1435 return r;
165e4a11
AK
1436}
1437
b4f1578f 1438int dm_tree_preload_children(struct dm_tree_node *dnode,
165e4a11 1439 const char *uuid_prefix,
e6a6954e 1440 size_t uuid_prefix_len)
165e4a11
AK
1441{
1442 void *handle = NULL;
b4f1578f 1443 struct dm_tree_node *child;
165e4a11
AK
1444 struct dm_info newinfo;
1445 const char *name;
1446
1447 /* Preload children first */
b4f1578f 1448 while ((child = dm_tree_next_child(&handle, dnode, 0))) {
165e4a11
AK
1449 /* Skip existing non-device-mapper devices */
1450 if (!child->info.exists && child->info.major)
1451 continue;
1452
1453 /* Ignore if it doesn't belong to this VG */
87f98002
AK
1454 if (child->info.exists &&
1455 !_uuid_prefix_matches(child->uuid, uuid_prefix, uuid_prefix_len))
165e4a11
AK
1456 continue;
1457
b4f1578f 1458 if (dm_tree_node_num_children(child, 0))
e6a6954e 1459 dm_tree_preload_children(child, uuid_prefix, uuid_prefix_len);
165e4a11 1460
b4f1578f 1461 if (!(name = dm_tree_node_get_name(child))) {
165e4a11
AK
1462 stack;
1463 continue;
1464 }
1465
1466 /* FIXME Cope if name exists with no uuid? */
1467 if (!child->info.exists) {
1468 if (!_create_node(child)) {
1469 stack;
1470 return 0;
1471 }
1472 }
1473
1474 if (!child->info.inactive_table && child->props.segment_count) {
1475 if (!_load_node(child)) {
1476 stack;
1477 return 0;
1478 }
1479 }
1480
1481 /* Resume device immediately if it has parents */
abbca212 1482 if (!dm_tree_node_num_children(child, 1))
165e4a11
AK
1483 continue;
1484
7707ea90
AK
1485 if (!child->info.inactive_table && !child->info.suspended)
1486 continue;
1487
165e4a11
AK
1488 if (!_resume_node(name, child->info.major, child->info.minor, &newinfo)) {
1489 log_error("Unable to resume %s (%" PRIu32
1490 ":%" PRIu32 ")", name, child->info.major,
1491 child->info.minor);
1492 continue;
1493 }
1494
1495 /* Update cached info */
1496 child->info = newinfo;
1497 }
1498
1499 handle = NULL;
1500
1501 return 1;
1502}
1503
165e4a11
AK
1504/*
1505 * Returns 1 if unsure.
1506 */
b4f1578f 1507int dm_tree_children_use_uuid(struct dm_tree_node *dnode,
165e4a11
AK
1508 const char *uuid_prefix,
1509 size_t uuid_prefix_len)
1510{
1511 void *handle = NULL;
b4f1578f 1512 struct dm_tree_node *child = dnode;
165e4a11
AK
1513 const char *uuid;
1514
b4f1578f
AK
1515 while ((child = dm_tree_next_child(&handle, dnode, 0))) {
1516 if (!(uuid = dm_tree_node_get_uuid(child))) {
1517 log_error("Failed to get uuid for dtree node.");
165e4a11
AK
1518 return 1;
1519 }
1520
87f98002 1521 if (_uuid_prefix_matches(uuid, uuid_prefix, uuid_prefix_len))
165e4a11
AK
1522 return 1;
1523
b4f1578f
AK
1524 if (dm_tree_node_num_children(child, 0))
1525 dm_tree_children_use_uuid(child, uuid_prefix, uuid_prefix_len);
165e4a11
AK
1526 }
1527
1528 return 0;
1529}
1530
1531/*
1532 * Target functions
1533 */
b4f1578f 1534static struct load_segment *_add_segment(struct dm_tree_node *dnode, unsigned type, uint64_t size)
165e4a11
AK
1535{
1536 struct load_segment *seg;
1537
b4f1578f
AK
1538 if (!(seg = dm_pool_zalloc(dnode->dtree->mem, sizeof(*seg)))) {
1539 log_error("dtree node segment allocation failed");
165e4a11
AK
1540 return NULL;
1541 }
1542
1543 seg->type = type;
1544 seg->size = size;
1545 seg->area_count = 0;
1546 list_init(&seg->areas);
1547 seg->stripe_size = 0;
1548 seg->persistent = 0;
1549 seg->chunk_size = 0;
1550 seg->cow = NULL;
1551 seg->origin = NULL;
1552
1553 list_add(&dnode->props.segs, &seg->list);
1554 dnode->props.segment_count++;
1555
1556 return seg;
1557}
1558
b4f1578f 1559int dm_tree_node_add_snapshot_origin_target(struct dm_tree_node *dnode,
165e4a11
AK
1560 uint64_t size,
1561 const char *origin_uuid)
1562{
1563 struct load_segment *seg;
b4f1578f 1564 struct dm_tree_node *origin_node;
165e4a11 1565
b4f1578f
AK
1566 if (!(seg = _add_segment(dnode, SEG_SNAPSHOT_ORIGIN, size)))
1567 return_0;
165e4a11 1568
b4f1578f 1569 if (!(origin_node = dm_tree_find_node_by_uuid(dnode->dtree, origin_uuid))) {
165e4a11
AK
1570 log_error("Couldn't find snapshot origin uuid %s.", origin_uuid);
1571 return 0;
1572 }
1573
1574 seg->origin = origin_node;
b4f1578f
AK
1575 if (!_link_tree_nodes(dnode, origin_node))
1576 return_0;
165e4a11 1577
56c28292
AK
1578 /* Resume snapshot origins after new snapshots */
1579 dnode->activation_priority = 1;
1580
165e4a11
AK
1581 return 1;
1582}
1583
b4f1578f 1584int dm_tree_node_add_snapshot_target(struct dm_tree_node *node,
165e4a11
AK
1585 uint64_t size,
1586 const char *origin_uuid,
1587 const char *cow_uuid,
1588 int persistent,
1589 uint32_t chunk_size)
1590{
1591 struct load_segment *seg;
b4f1578f 1592 struct dm_tree_node *origin_node, *cow_node;
165e4a11 1593
b4f1578f
AK
1594 if (!(seg = _add_segment(node, SEG_SNAPSHOT, size)))
1595 return_0;
165e4a11 1596
b4f1578f 1597 if (!(origin_node = dm_tree_find_node_by_uuid(node->dtree, origin_uuid))) {
165e4a11
AK
1598 log_error("Couldn't find snapshot origin uuid %s.", origin_uuid);
1599 return 0;
1600 }
1601
1602 seg->origin = origin_node;
b4f1578f
AK
1603 if (!_link_tree_nodes(node, origin_node))
1604 return_0;
165e4a11 1605
b4f1578f 1606 if (!(cow_node = dm_tree_find_node_by_uuid(node->dtree, cow_uuid))) {
165e4a11
AK
1607 log_error("Couldn't find snapshot origin uuid %s.", cow_uuid);
1608 return 0;
1609 }
1610
1611 seg->cow = cow_node;
b4f1578f
AK
1612 if (!_link_tree_nodes(node, cow_node))
1613 return_0;
165e4a11
AK
1614
1615 seg->persistent = persistent ? 1 : 0;
1616 seg->chunk_size = chunk_size;
1617
1618 return 1;
1619}
1620
b4f1578f 1621int dm_tree_node_add_error_target(struct dm_tree_node *node,
165e4a11
AK
1622 uint64_t size)
1623{
b4f1578f
AK
1624 if (!_add_segment(node, SEG_ERROR, size))
1625 return_0;
165e4a11
AK
1626
1627 return 1;
1628}
1629
b4f1578f 1630int dm_tree_node_add_zero_target(struct dm_tree_node *node,
165e4a11
AK
1631 uint64_t size)
1632{
b4f1578f
AK
1633 if (!_add_segment(node, SEG_ZERO, size))
1634 return_0;
165e4a11
AK
1635
1636 return 1;
1637}
1638
b4f1578f 1639int dm_tree_node_add_linear_target(struct dm_tree_node *node,
165e4a11
AK
1640 uint64_t size)
1641{
b4f1578f
AK
1642 if (!_add_segment(node, SEG_LINEAR, size))
1643 return_0;
165e4a11
AK
1644
1645 return 1;
1646}
1647
b4f1578f 1648int dm_tree_node_add_striped_target(struct dm_tree_node *node,
165e4a11
AK
1649 uint64_t size,
1650 uint32_t stripe_size)
1651{
1652 struct load_segment *seg;
1653
b4f1578f
AK
1654 if (!(seg = _add_segment(node, SEG_STRIPED, size)))
1655 return_0;
165e4a11
AK
1656
1657 seg->stripe_size = stripe_size;
1658
1659 return 1;
1660}
1661
b4f1578f 1662int dm_tree_node_add_mirror_target_log(struct dm_tree_node *node,
165e4a11
AK
1663 uint32_t region_size,
1664 unsigned clustered,
dbcb64b8 1665 uint32_t flags,
165e4a11
AK
1666 const char *log_uuid,
1667 unsigned area_count)
1668{
908db078 1669 struct dm_tree_node *log_node = NULL;
165e4a11
AK
1670 struct load_segment *seg;
1671
1672 if (!node->props.segment_count) {
1673 log_error("Internal error: Attempt to add target area to missing segment.");
1674 return 0;
1675 }
1676
1677 seg = list_item(list_last(&node->props.segs), struct load_segment);
1678
24b026e3
AK
1679 if (log_uuid) {
1680 if (!(log_node = dm_tree_find_node_by_uuid(node->dtree, log_uuid))) {
1681 log_error("Couldn't find mirror log uuid %s.", log_uuid);
1682 return 0;
1683 }
1684 if (!_link_tree_nodes(node, log_node))
1685 return_0;
165e4a11
AK
1686 }
1687
1688 seg->log = log_node;
165e4a11
AK
1689 seg->region_size = region_size;
1690 seg->clustered = clustered;
1691 seg->mirror_area_count = area_count;
dbcb64b8 1692 seg->flags = flags;
165e4a11
AK
1693
1694 return 1;
1695}
1696
b4f1578f 1697int dm_tree_node_add_mirror_target(struct dm_tree_node *node,
165e4a11
AK
1698 uint64_t size)
1699{
1700 struct load_segment *seg;
1701
b4f1578f
AK
1702 if (!(seg = _add_segment(node, SEG_MIRRORED, size)))
1703 return_0;
165e4a11
AK
1704
1705 return 1;
1706}
1707
b4f1578f 1708static int _add_area(struct dm_tree_node *node, struct load_segment *seg, struct dm_tree_node *dev_node, uint64_t offset)
165e4a11
AK
1709{
1710 struct seg_area *area;
1711
b4f1578f 1712 if (!(area = dm_pool_zalloc(node->dtree->mem, sizeof (*area)))) {
165e4a11
AK
1713 log_error("Failed to allocate target segment area.");
1714 return 0;
1715 }
1716
1717 area->dev_node = dev_node;
1718 area->offset = offset;
1719
1720 list_add(&seg->areas, &area->list);
1721 seg->area_count++;
1722
1723 return 1;
1724}
1725
b4f1578f 1726int dm_tree_node_add_target_area(struct dm_tree_node *node,
165e4a11
AK
1727 const char *dev_name,
1728 const char *uuid,
1729 uint64_t offset)
1730{
1731 struct load_segment *seg;
1732 struct stat info;
b4f1578f 1733 struct dm_tree_node *dev_node;
165e4a11
AK
1734
1735 if ((!dev_name || !*dev_name) && (!uuid || !*uuid)) {
b4f1578f 1736 log_error("dm_tree_node_add_target_area called without device");
165e4a11
AK
1737 return 0;
1738 }
1739
1740 if (uuid) {
b4f1578f 1741 if (!(dev_node = dm_tree_find_node_by_uuid(node->dtree, uuid))) {
165e4a11
AK
1742 log_error("Couldn't find area uuid %s.", uuid);
1743 return 0;
1744 }
b4f1578f
AK
1745 if (!_link_tree_nodes(node, dev_node))
1746 return_0;
165e4a11
AK
1747 } else {
1748 if (stat(dev_name, &info) < 0) {
1749 log_error("Device %s not found.", dev_name);
1750 return 0;
1751 }
1752
1753 if (!S_ISBLK(info.st_mode)) {
1754 log_error("Device %s is not a block device.", dev_name);
1755 return 0;
1756 }
1757
1758 /* FIXME Check correct macro use */
b4f1578f
AK
1759 if (!(dev_node = _add_dev(node->dtree, node, MAJOR(info.st_rdev), MINOR(info.st_rdev))))
1760 return_0;
165e4a11
AK
1761 }
1762
1763 if (!node->props.segment_count) {
1764 log_error("Internal error: Attempt to add target area to missing segment.");
1765 return 0;
1766 }
1767
1768 seg = list_item(list_last(&node->props.segs), struct load_segment);
1769
b4f1578f
AK
1770 if (!_add_area(node, seg, dev_node, offset))
1771 return_0;
165e4a11
AK
1772
1773 return 1;
db208f51 1774}
This page took 0.233014 seconds and 5 git commands to generate.