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