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