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