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