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