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