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