]> sourceware.org Git - lvm2.git/blame - libdm/libdm-deptree.c
Use 'lvm lvresize' instead of 'lvresize' in fsadm.
[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
eb91c4ee
MB
643int dm_tree_node_size_changed(struct dm_tree_node *dnode)
644{
645 return dnode->props.size_changed;
646}
647
b4f1578f 648int dm_tree_node_num_children(struct dm_tree_node *node, uint32_t inverted)
3d0480ed
AK
649{
650 if (inverted) {
b4f1578f 651 if (_nodes_are_linked(&node->dtree->root, node))
3d0480ed 652 return 0;
2c44337b 653 return dm_list_size(&node->used_by);
3d0480ed
AK
654 }
655
b4f1578f 656 if (_nodes_are_linked(node, &node->dtree->root))
3d0480ed
AK
657 return 0;
658
2c44337b 659 return dm_list_size(&node->uses);
3d0480ed
AK
660}
661
2b69db1f
AK
662/*
663 * Returns 1 if no prefix supplied
664 */
665static int _uuid_prefix_matches(const char *uuid, const char *uuid_prefix, size_t uuid_prefix_len)
666{
667 if (!uuid_prefix)
668 return 1;
669
670 if (!strncmp(uuid, uuid_prefix, uuid_prefix_len))
671 return 1;
672
673 /* Handle transition: active device uuids might be missing the prefix */
674 if (uuid_prefix_len <= 4)
675 return 0;
676
87f98002 677 if (!strncmp(uuid, UUID_PREFIX, sizeof(UUID_PREFIX) - 1))
872dea04
AK
678 return 0;
679
87f98002 680 if (strncmp(uuid_prefix, UUID_PREFIX, sizeof(UUID_PREFIX) - 1))
2b69db1f
AK
681 return 0;
682
87f98002 683 if (!strncmp(uuid, uuid_prefix + sizeof(UUID_PREFIX) - 1, uuid_prefix_len - (sizeof(UUID_PREFIX) - 1)))
2b69db1f
AK
684 return 1;
685
686 return 0;
687}
688
690a5da2
AK
689/*
690 * Returns 1 if no children.
691 */
b4f1578f 692static int _children_suspended(struct dm_tree_node *node,
690a5da2
AK
693 uint32_t inverted,
694 const char *uuid_prefix,
695 size_t uuid_prefix_len)
696{
2c44337b 697 struct dm_list *list;
b4f1578f 698 struct dm_tree_link *dlink;
690a5da2
AK
699 const struct dm_info *dinfo;
700 const char *uuid;
701
702 if (inverted) {
b4f1578f 703 if (_nodes_are_linked(&node->dtree->root, node))
690a5da2
AK
704 return 1;
705 list = &node->used_by;
706 } else {
b4f1578f 707 if (_nodes_are_linked(node, &node->dtree->root))
690a5da2
AK
708 return 1;
709 list = &node->uses;
710 }
711
2c44337b 712 dm_list_iterate_items(dlink, list) {
b4f1578f 713 if (!(uuid = dm_tree_node_get_uuid(dlink->node))) {
690a5da2
AK
714 stack;
715 continue;
716 }
717
718 /* Ignore if it doesn't belong to this VG */
2b69db1f 719 if (!_uuid_prefix_matches(uuid, uuid_prefix, uuid_prefix_len))
690a5da2
AK
720 continue;
721
b4f1578f
AK
722 if (!(dinfo = dm_tree_node_get_info(dlink->node))) {
723 stack; /* FIXME Is this normal? */
690a5da2
AK
724 return 0;
725 }
726
727 if (!dinfo->suspended)
728 return 0;
729 }
730
731 return 1;
732}
733
3d0480ed
AK
734/*
735 * Set major and minor to zero for root of tree.
736 */
b4f1578f 737struct dm_tree_node *dm_tree_find_node(struct dm_tree *dtree,
3d0480ed
AK
738 uint32_t major,
739 uint32_t minor)
740{
741 if (!major && !minor)
b4f1578f 742 return &dtree->root;
3d0480ed 743
b4f1578f 744 return _find_dm_tree_node(dtree, major, minor);
3d0480ed
AK
745}
746
165e4a11
AK
747/*
748 * Set uuid to NULL for root of tree.
749 */
b4f1578f 750struct dm_tree_node *dm_tree_find_node_by_uuid(struct dm_tree *dtree,
165e4a11
AK
751 const char *uuid)
752{
753 if (!uuid || !*uuid)
b4f1578f 754 return &dtree->root;
165e4a11 755
b4f1578f 756 return _find_dm_tree_node_by_uuid(dtree, uuid);
165e4a11
AK
757}
758
3d0480ed
AK
759/*
760 * First time set *handle to NULL.
761 * Set inverted to invert the tree.
762 */
b4f1578f
AK
763struct dm_tree_node *dm_tree_next_child(void **handle,
764 struct dm_tree_node *parent,
3d0480ed
AK
765 uint32_t inverted)
766{
2c44337b
AK
767 struct dm_list **dlink = (struct dm_list **) handle;
768 struct dm_list *use_list;
3d0480ed
AK
769
770 if (inverted)
771 use_list = &parent->used_by;
772 else
773 use_list = &parent->uses;
774
775 if (!*dlink)
2c44337b 776 *dlink = dm_list_first(use_list);
3d0480ed 777 else
2c44337b 778 *dlink = dm_list_next(use_list, *dlink);
3d0480ed 779
2c44337b 780 return (*dlink) ? dm_list_item(*dlink, struct dm_tree_link)->node : NULL;
3d0480ed
AK
781}
782
3e8c6b73 783/*
a6d97ede 784 * Deactivate a device with its dependencies if the uuid prefix matches.
3e8c6b73 785 */
db208f51
AK
786static int _info_by_dev(uint32_t major, uint32_t minor, int with_open_count,
787 struct dm_info *info)
3e8c6b73
AK
788{
789 struct dm_task *dmt;
790 int r;
791
792 if (!(dmt = dm_task_create(DM_DEVICE_INFO))) {
793 log_error("_info_by_dev: dm_task creation failed");
794 return 0;
795 }
796
797 if (!dm_task_set_major(dmt, major) || !dm_task_set_minor(dmt, minor)) {
798 log_error("_info_by_dev: Failed to set device number");
799 dm_task_destroy(dmt);
800 return 0;
801 }
802
db208f51
AK
803 if (!with_open_count && !dm_task_no_open_count(dmt))
804 log_error("Failed to disable open_count");
805
3e8c6b73
AK
806 if ((r = dm_task_run(dmt)))
807 r = dm_task_get_info(dmt, info);
808
809 dm_task_destroy(dmt);
810
811 return r;
812}
813
814static int _deactivate_node(const char *name, uint32_t major, uint32_t minor)
815{
816 struct dm_task *dmt;
817 int r;
818
819 log_verbose("Removing %s (%" PRIu32 ":%" PRIu32 ")", name, major, minor);
820
821 if (!(dmt = dm_task_create(DM_DEVICE_REMOVE))) {
822 log_error("Deactivation dm_task creation failed for %s", name);
823 return 0;
824 }
825
826 if (!dm_task_set_major(dmt, major) || !dm_task_set_minor(dmt, minor)) {
827 log_error("Failed to set device number for %s deactivation", name);
828 dm_task_destroy(dmt);
829 return 0;
830 }
831
832 if (!dm_task_no_open_count(dmt))
833 log_error("Failed to disable open_count");
834
835 r = dm_task_run(dmt);
836
165e4a11
AK
837 /* FIXME Until kernel returns actual name so dm-ioctl.c can handle it */
838 rm_dev_node(name);
839
db208f51
AK
840 /* FIXME Remove node from tree or mark invalid? */
841
842 dm_task_destroy(dmt);
843
844 return r;
845}
846
165e4a11
AK
847static int _rename_node(const char *old_name, const char *new_name, uint32_t major, uint32_t minor)
848{
849 struct dm_task *dmt;
850 int r = 0;
851
852 log_verbose("Renaming %s (%" PRIu32 ":%" PRIu32 ") to %s", old_name, major, minor, new_name);
853
854 if (!(dmt = dm_task_create(DM_DEVICE_RENAME))) {
855 log_error("Rename dm_task creation failed for %s", old_name);
856 return 0;
857 }
858
859 if (!dm_task_set_name(dmt, old_name)) {
860 log_error("Failed to set name for %s rename.", old_name);
861 goto out;
862 }
863
b4f1578f
AK
864 if (!dm_task_set_newname(dmt, new_name))
865 goto_out;
165e4a11
AK
866
867 if (!dm_task_no_open_count(dmt))
868 log_error("Failed to disable open_count");
869
870 r = dm_task_run(dmt);
871
872out:
873 dm_task_destroy(dmt);
874
875 return r;
876}
877
165e4a11
AK
878/* FIXME Merge with _suspend_node? */
879static int _resume_node(const char *name, uint32_t major, uint32_t minor,
52b84409 880 uint32_t read_ahead, uint32_t read_ahead_flags,
165e4a11
AK
881 struct dm_info *newinfo)
882{
883 struct dm_task *dmt;
884 int r;
885
886 log_verbose("Resuming %s (%" PRIu32 ":%" PRIu32 ")", name, major, minor);
887
888 if (!(dmt = dm_task_create(DM_DEVICE_RESUME))) {
889 log_error("Suspend dm_task creation failed for %s", name);
890 return 0;
891 }
892
0b7d16bc
AK
893 /* FIXME Kernel should fill in name on return instead */
894 if (!dm_task_set_name(dmt, name)) {
895 log_error("Failed to set readahead device name for %s", name);
896 dm_task_destroy(dmt);
897 return 0;
898 }
899
165e4a11
AK
900 if (!dm_task_set_major(dmt, major) || !dm_task_set_minor(dmt, minor)) {
901 log_error("Failed to set device number for %s resumption.", name);
902 dm_task_destroy(dmt);
903 return 0;
904 }
905
906 if (!dm_task_no_open_count(dmt))
907 log_error("Failed to disable open_count");
908
52b84409
AK
909 if (!dm_task_set_read_ahead(dmt, read_ahead, read_ahead_flags))
910 log_error("Failed to set read ahead");
911
165e4a11
AK
912 if ((r = dm_task_run(dmt)))
913 r = dm_task_get_info(dmt, newinfo);
914
915 dm_task_destroy(dmt);
916
917 return r;
918}
919
db208f51 920static int _suspend_node(const char *name, uint32_t major, uint32_t minor,
b9ffd32c 921 int skip_lockfs, int no_flush, struct dm_info *newinfo)
db208f51
AK
922{
923 struct dm_task *dmt;
924 int r;
925
b9ffd32c
AK
926 log_verbose("Suspending %s (%" PRIu32 ":%" PRIu32 ")%s%s",
927 name, major, minor,
928 skip_lockfs ? "" : " with filesystem sync",
6e1898a5 929 no_flush ? "" : " with device flush");
db208f51
AK
930
931 if (!(dmt = dm_task_create(DM_DEVICE_SUSPEND))) {
932 log_error("Suspend dm_task creation failed for %s", name);
933 return 0;
934 }
935
936 if (!dm_task_set_major(dmt, major) || !dm_task_set_minor(dmt, minor)) {
937 log_error("Failed to set device number for %s suspension.", name);
938 dm_task_destroy(dmt);
939 return 0;
940 }
941
942 if (!dm_task_no_open_count(dmt))
943 log_error("Failed to disable open_count");
944
c55b1410
AK
945 if (skip_lockfs && !dm_task_skip_lockfs(dmt))
946 log_error("Failed to set skip_lockfs flag.");
947
b9ffd32c
AK
948 if (no_flush && !dm_task_no_flush(dmt))
949 log_error("Failed to set no_flush flag.");
950
db208f51
AK
951 if ((r = dm_task_run(dmt)))
952 r = dm_task_get_info(dmt, newinfo);
953
3e8c6b73
AK
954 dm_task_destroy(dmt);
955
956 return r;
957}
958
b4f1578f 959int dm_tree_deactivate_children(struct dm_tree_node *dnode,
a6d97ede
AK
960 const char *uuid_prefix,
961 size_t uuid_prefix_len)
3e8c6b73
AK
962{
963 void *handle = NULL;
b4f1578f 964 struct dm_tree_node *child = dnode;
3e8c6b73
AK
965 struct dm_info info;
966 const struct dm_info *dinfo;
967 const char *name;
968 const char *uuid;
969
b4f1578f
AK
970 while ((child = dm_tree_next_child(&handle, dnode, 0))) {
971 if (!(dinfo = dm_tree_node_get_info(child))) {
3e8c6b73
AK
972 stack;
973 continue;
974 }
975
b4f1578f 976 if (!(name = dm_tree_node_get_name(child))) {
3e8c6b73
AK
977 stack;
978 continue;
979 }
980
b4f1578f 981 if (!(uuid = dm_tree_node_get_uuid(child))) {
3e8c6b73
AK
982 stack;
983 continue;
984 }
985
986 /* Ignore if it doesn't belong to this VG */
2b69db1f 987 if (!_uuid_prefix_matches(uuid, uuid_prefix, uuid_prefix_len))
3e8c6b73 988 continue;
3e8c6b73
AK
989
990 /* Refresh open_count */
db208f51 991 if (!_info_by_dev(dinfo->major, dinfo->minor, 1, &info) ||
3e8c6b73
AK
992 !info.exists || info.open_count)
993 continue;
994
995 if (!_deactivate_node(name, info.major, info.minor)) {
996 log_error("Unable to deactivate %s (%" PRIu32
997 ":%" PRIu32 ")", name, info.major,
998 info.minor);
999 continue;
1000 }
1001
b4f1578f
AK
1002 if (dm_tree_node_num_children(child, 0))
1003 dm_tree_deactivate_children(child, uuid_prefix, uuid_prefix_len);
3e8c6b73
AK
1004 }
1005
1006 return 1;
1007}
db208f51 1008
c55b1410
AK
1009void dm_tree_skip_lockfs(struct dm_tree_node *dnode)
1010{
1011 dnode->dtree->skip_lockfs = 1;
1012}
1013
b9ffd32c
AK
1014void dm_tree_use_no_flush_suspend(struct dm_tree_node *dnode)
1015{
1016 dnode->dtree->no_flush = 1;
1017}
1018
b4f1578f 1019int dm_tree_suspend_children(struct dm_tree_node *dnode,
db208f51
AK
1020 const char *uuid_prefix,
1021 size_t uuid_prefix_len)
1022{
1023 void *handle = NULL;
b4f1578f 1024 struct dm_tree_node *child = dnode;
db208f51
AK
1025 struct dm_info info, newinfo;
1026 const struct dm_info *dinfo;
1027 const char *name;
1028 const char *uuid;
1029
690a5da2 1030 /* Suspend nodes at this level of the tree */
b4f1578f
AK
1031 while ((child = dm_tree_next_child(&handle, dnode, 0))) {
1032 if (!(dinfo = dm_tree_node_get_info(child))) {
db208f51
AK
1033 stack;
1034 continue;
1035 }
1036
b4f1578f 1037 if (!(name = dm_tree_node_get_name(child))) {
db208f51
AK
1038 stack;
1039 continue;
1040 }
1041
b4f1578f 1042 if (!(uuid = dm_tree_node_get_uuid(child))) {
db208f51
AK
1043 stack;
1044 continue;
1045 }
1046
1047 /* Ignore if it doesn't belong to this VG */
2b69db1f 1048 if (!_uuid_prefix_matches(uuid, uuid_prefix, uuid_prefix_len))
db208f51
AK
1049 continue;
1050
690a5da2
AK
1051 /* Ensure immediate parents are already suspended */
1052 if (!_children_suspended(child, 1, uuid_prefix, uuid_prefix_len))
1053 continue;
1054
db208f51 1055 if (!_info_by_dev(dinfo->major, dinfo->minor, 0, &info) ||
b700541f 1056 !info.exists || info.suspended)
db208f51
AK
1057 continue;
1058
c55b1410 1059 if (!_suspend_node(name, info.major, info.minor,
b9ffd32c
AK
1060 child->dtree->skip_lockfs,
1061 child->dtree->no_flush, &newinfo)) {
db208f51
AK
1062 log_error("Unable to suspend %s (%" PRIu32
1063 ":%" PRIu32 ")", name, info.major,
1064 info.minor);
1065 continue;
1066 }
1067
1068 /* Update cached info */
1069 child->info = newinfo;
690a5da2
AK
1070 }
1071
1072 /* Then suspend any child nodes */
1073 handle = NULL;
1074
b4f1578f
AK
1075 while ((child = dm_tree_next_child(&handle, dnode, 0))) {
1076 if (!(uuid = dm_tree_node_get_uuid(child))) {
690a5da2
AK
1077 stack;
1078 continue;
1079 }
1080
1081 /* Ignore if it doesn't belong to this VG */
87f98002 1082 if (!_uuid_prefix_matches(uuid, uuid_prefix, uuid_prefix_len))
690a5da2 1083 continue;
db208f51 1084
b4f1578f
AK
1085 if (dm_tree_node_num_children(child, 0))
1086 dm_tree_suspend_children(child, uuid_prefix, uuid_prefix_len);
db208f51
AK
1087 }
1088
1089 return 1;
1090}
1091
b4f1578f 1092int dm_tree_activate_children(struct dm_tree_node *dnode,
db208f51
AK
1093 const char *uuid_prefix,
1094 size_t uuid_prefix_len)
1095{
1096 void *handle = NULL;
b4f1578f 1097 struct dm_tree_node *child = dnode;
165e4a11
AK
1098 struct dm_info newinfo;
1099 const char *name;
db208f51 1100 const char *uuid;
56c28292 1101 int priority;
db208f51 1102
165e4a11 1103 /* Activate children first */
b4f1578f
AK
1104 while ((child = dm_tree_next_child(&handle, dnode, 0))) {
1105 if (!(uuid = dm_tree_node_get_uuid(child))) {
165e4a11
AK
1106 stack;
1107 continue;
db208f51
AK
1108 }
1109
908db078
AK
1110 if (!_uuid_prefix_matches(uuid, uuid_prefix, uuid_prefix_len))
1111 continue;
db208f51 1112
b4f1578f
AK
1113 if (dm_tree_node_num_children(child, 0))
1114 dm_tree_activate_children(child, uuid_prefix, uuid_prefix_len);
56c28292 1115 }
165e4a11 1116
56c28292 1117 handle = NULL;
165e4a11 1118
56c28292
AK
1119 for (priority = 0; priority < 2; priority++) {
1120 while ((child = dm_tree_next_child(&handle, dnode, 0))) {
1121 if (!(uuid = dm_tree_node_get_uuid(child))) {
1122 stack;
1123 continue;
165e4a11 1124 }
165e4a11 1125
56c28292
AK
1126 if (!_uuid_prefix_matches(uuid, uuid_prefix, uuid_prefix_len))
1127 continue;
165e4a11 1128
56c28292
AK
1129 if (priority != child->activation_priority)
1130 continue;
165e4a11 1131
56c28292
AK
1132 if (!(name = dm_tree_node_get_name(child))) {
1133 stack;
1134 continue;
1135 }
1136
1137 /* Rename? */
1138 if (child->props.new_name) {
1139 if (!_rename_node(name, child->props.new_name, child->info.major, child->info.minor)) {
1140 log_error("Failed to rename %s (%" PRIu32
1141 ":%" PRIu32 ") to %s", name, child->info.major,
1142 child->info.minor, child->props.new_name);
1143 return 0;
1144 }
1145 child->name = child->props.new_name;
1146 child->props.new_name = NULL;
1147 }
1148
1149 if (!child->info.inactive_table && !child->info.suspended)
1150 continue;
1151
bafa2f39 1152 if (!_resume_node(child->name, child->info.major, child->info.minor,
52b84409
AK
1153 child->props.read_ahead,
1154 child->props.read_ahead_flags, &newinfo)) {
56c28292 1155 log_error("Unable to resume %s (%" PRIu32
bafa2f39 1156 ":%" PRIu32 ")", child->name, child->info.major,
56c28292
AK
1157 child->info.minor);
1158 continue;
1159 }
1160
1161 /* Update cached info */
1162 child->info = newinfo;
1163 }
db208f51
AK
1164 }
1165
165e4a11
AK
1166 handle = NULL;
1167
1168 return 1;
1169}
1170
b4f1578f 1171static int _create_node(struct dm_tree_node *dnode)
165e4a11
AK
1172{
1173 int r = 0;
1174 struct dm_task *dmt;
1175
1176 log_verbose("Creating %s", dnode->name);
1177
1178 if (!(dmt = dm_task_create(DM_DEVICE_CREATE))) {
1179 log_error("Create dm_task creation failed for %s", dnode->name);
1180 return 0;
1181 }
1182
1183 if (!dm_task_set_name(dmt, dnode->name)) {
1184 log_error("Failed to set device name for %s", dnode->name);
1185 goto out;
1186 }
1187
1188 if (!dm_task_set_uuid(dmt, dnode->uuid)) {
1189 log_error("Failed to set uuid for %s", dnode->name);
1190 goto out;
1191 }
1192
1193 if (dnode->props.major &&
1194 (!dm_task_set_major(dmt, dnode->props.major) ||
1195 !dm_task_set_minor(dmt, dnode->props.minor))) {
1196 log_error("Failed to set device number for %s creation.", dnode->name);
1197 goto out;
1198 }
1199
1200 if (dnode->props.read_only && !dm_task_set_ro(dmt)) {
1201 log_error("Failed to set read only flag for %s", dnode->name);
1202 goto out;
1203 }
1204
1205 if (!dm_task_no_open_count(dmt))
1206 log_error("Failed to disable open_count");
1207
1208 if ((r = dm_task_run(dmt)))
1209 r = dm_task_get_info(dmt, &dnode->info);
1210
1211out:
1212 dm_task_destroy(dmt);
1213
1214 return r;
1215}
1216
1217
b4f1578f 1218static int _build_dev_string(char *devbuf, size_t bufsize, struct dm_tree_node *node)
165e4a11
AK
1219{
1220 if (!dm_format_dev(devbuf, bufsize, node->info.major, node->info.minor)) {
1221 log_error("Failed to format %s device number for %s as dm "
1222 "target (%u,%u)",
1223 node->name, node->uuid, node->info.major, node->info.minor);
1224 return 0;
1225 }
1226
1227 return 1;
1228}
1229
ffa9b6a5
ZK
1230/* simplify string emiting code */
1231#define EMIT_PARAMS(p, str...)\
7b6c011c
AK
1232do {\
1233 int w;\
1234 if ((w = dm_snprintf(params + p, paramsize - (size_t) p, str)) < 0) {\
1235 stack; /* Out of space */\
1236 return -1;\
1237 }\
1238 p += w;\
1239} while (0)
ffa9b6a5 1240
4dcaa230
AK
1241static int _emit_areas_line(struct dm_task *dmt __attribute((unused)),
1242 struct load_segment *seg, char *params,
1243 size_t paramsize, int *pos)
165e4a11
AK
1244{
1245 struct seg_area *area;
7d7d93ac 1246 char devbuf[DM_FORMAT_DEV_BUFSIZE];
165e4a11 1247
2c44337b 1248 dm_list_iterate_items(area, &seg->areas) {
b4f1578f
AK
1249 if (!_build_dev_string(devbuf, sizeof(devbuf), area->dev_node))
1250 return_0;
165e4a11 1251
ffa9b6a5 1252 EMIT_PARAMS(*pos, " %s %" PRIu64, devbuf, area->offset);
165e4a11
AK
1253 }
1254
1255 return 1;
1256}
1257
1258static int _emit_segment_line(struct dm_task *dmt, struct load_segment *seg, uint64_t *seg_start, char *params, size_t paramsize)
1259{
dbcb64b8 1260 unsigned log_parm_count;
ffa9b6a5
ZK
1261 int pos = 0;
1262 int r;
7d7d93ac
AK
1263 char originbuf[DM_FORMAT_DEV_BUFSIZE], cowbuf[DM_FORMAT_DEV_BUFSIZE];
1264 char logbuf[DM_FORMAT_DEV_BUFSIZE];
dbcb64b8 1265 const char *logtype;
165e4a11
AK
1266
1267 switch(seg->type) {
1268 case SEG_ERROR:
1269 case SEG_ZERO:
165e4a11
AK
1270 case SEG_LINEAR:
1271 break;
1272 case SEG_MIRRORED:
67b25ed4 1273 log_parm_count = 1; /* Region size */
2d2b610f 1274 log_parm_count += hweight32(seg->flags); /* [no]sync, block_on_error etc. */
67b25ed4 1275
311d6d81
AK
1276 if (seg->flags & DM_CORELOG)
1277 log_parm_count--; /* DM_CORELOG does not count in the param list */
1278
165e4a11 1279 if (seg->clustered) {
311d6d81
AK
1280 if (seg->uuid)
1281 log_parm_count++;
ffa9b6a5 1282 EMIT_PARAMS(pos, "clustered-");
165e4a11 1283 }
dbcb64b8 1284
dbcb64b8
AK
1285 if (!seg->log)
1286 logtype = "core";
1287 else {
1288 logtype = "disk";
1289 log_parm_count++;
b4f1578f
AK
1290 if (!_build_dev_string(logbuf, sizeof(logbuf), seg->log))
1291 return_0;
dbcb64b8
AK
1292 }
1293
ffa9b6a5 1294 EMIT_PARAMS(pos, "%s %u", logtype, log_parm_count);
dbcb64b8 1295
ffa9b6a5
ZK
1296 if (seg->log)
1297 EMIT_PARAMS(pos, " %s", logbuf);
dbcb64b8 1298
ffa9b6a5 1299 EMIT_PARAMS(pos, " %u", seg->region_size);
67b25ed4 1300
ffa9b6a5
ZK
1301 if (seg->clustered && seg->uuid)
1302 EMIT_PARAMS(pos, " %s", seg->uuid);
dbcb64b8 1303
ffa9b6a5
ZK
1304 if ((seg->flags & DM_NOSYNC))
1305 EMIT_PARAMS(pos, " nosync");
1306 else if ((seg->flags & DM_FORCESYNC))
1307 EMIT_PARAMS(pos, " sync");
dbcb64b8 1308
ffa9b6a5
ZK
1309 if ((seg->flags & DM_BLOCK_ON_ERROR))
1310 EMIT_PARAMS(pos, " block_on_error");
1311
1312 EMIT_PARAMS(pos, " %u", seg->mirror_area_count);
dbcb64b8 1313
165e4a11
AK
1314 break;
1315 case SEG_SNAPSHOT:
b4f1578f
AK
1316 if (!_build_dev_string(originbuf, sizeof(originbuf), seg->origin))
1317 return_0;
1318 if (!_build_dev_string(cowbuf, sizeof(cowbuf), seg->cow))
1319 return_0;
ffa9b6a5
ZK
1320 EMIT_PARAMS(pos, "%s %s %c %d", originbuf, cowbuf,
1321 seg->persistent ? 'P' : 'N', seg->chunk_size);
165e4a11
AK
1322 break;
1323 case SEG_SNAPSHOT_ORIGIN:
b4f1578f
AK
1324 if (!_build_dev_string(originbuf, sizeof(originbuf), seg->origin))
1325 return_0;
ffa9b6a5 1326 EMIT_PARAMS(pos, "%s", originbuf);
165e4a11
AK
1327 break;
1328 case SEG_STRIPED:
ffa9b6a5 1329 EMIT_PARAMS(pos, "%u %u", seg->area_count, seg->stripe_size);
165e4a11
AK
1330 break;
1331 }
1332
1333 switch(seg->type) {
1334 case SEG_ERROR:
1335 case SEG_SNAPSHOT:
1336 case SEG_SNAPSHOT_ORIGIN:
1337 case SEG_ZERO:
1338 break;
1339 case SEG_LINEAR:
1340 case SEG_MIRRORED:
1341 case SEG_STRIPED:
1342 if ((r = _emit_areas_line(dmt, seg, params, paramsize, &pos)) <= 0) {
1343 stack;
1344 return r;
1345 }
1346 break;
1347 }
1348
1349 log_debug("Adding target: %" PRIu64 " %" PRIu64 " %s %s",
1350 *seg_start, seg->size, dm_segtypes[seg->type].target, params);
1351
b4f1578f
AK
1352 if (!dm_task_add_target(dmt, *seg_start, seg->size, dm_segtypes[seg->type].target, params))
1353 return_0;
165e4a11
AK
1354
1355 *seg_start += seg->size;
1356
1357 return 1;
1358}
1359
ffa9b6a5
ZK
1360#undef EMIT_PARAMS
1361
165e4a11
AK
1362static int _emit_segment(struct dm_task *dmt, struct load_segment *seg,
1363 uint64_t *seg_start)
1364{
1365 char *params;
1366 size_t paramsize = 4096;
1367 int ret;
1368
1369 do {
1370 if (!(params = dm_malloc(paramsize))) {
1371 log_error("Insufficient space for target parameters.");
1372 return 0;
1373 }
1374
12ea7cb1 1375 params[0] = '\0';
165e4a11
AK
1376 ret = _emit_segment_line(dmt, seg, seg_start, params, paramsize);
1377 dm_free(params);
1378
1379 if (!ret)
1380 stack;
1381
1382 if (ret >= 0)
1383 return ret;
1384
1385 log_debug("Insufficient space in params[%" PRIsize_t
1386 "] for target parameters.", paramsize);
1387
1388 paramsize *= 2;
1389 } while (paramsize < MAX_TARGET_PARAMSIZE);
1390
1391 log_error("Target parameter size too big. Aborting.");
1392 return 0;
1393}
1394
b4f1578f 1395static int _load_node(struct dm_tree_node *dnode)
165e4a11
AK
1396{
1397 int r = 0;
1398 struct dm_task *dmt;
1399 struct load_segment *seg;
1400 uint64_t seg_start = 0;
1401
1402 log_verbose("Loading %s table", dnode->name);
1403
1404 if (!(dmt = dm_task_create(DM_DEVICE_RELOAD))) {
1405 log_error("Reload dm_task creation failed for %s", dnode->name);
1406 return 0;
1407 }
1408
1409 if (!dm_task_set_major(dmt, dnode->info.major) ||
1410 !dm_task_set_minor(dmt, dnode->info.minor)) {
1411 log_error("Failed to set device number for %s reload.", dnode->name);
1412 goto out;
1413 }
1414
1415 if (dnode->props.read_only && !dm_task_set_ro(dmt)) {
1416 log_error("Failed to set read only flag for %s", dnode->name);
1417 goto out;
1418 }
1419
1420 if (!dm_task_no_open_count(dmt))
1421 log_error("Failed to disable open_count");
1422
2c44337b 1423 dm_list_iterate_items(seg, &dnode->props.segs)
b4f1578f
AK
1424 if (!_emit_segment(dmt, seg, &seg_start))
1425 goto_out;
165e4a11 1426
ec289b64
AK
1427 if (!dm_task_suppress_identical_reload(dmt))
1428 log_error("Failed to suppress reload of identical tables.");
1429
1430 if ((r = dm_task_run(dmt))) {
165e4a11 1431 r = dm_task_get_info(dmt, &dnode->info);
ec289b64
AK
1432 if (r && !dnode->info.inactive_table)
1433 log_verbose("Suppressed %s identical table reload.",
1434 dnode->name);
bb875bb9
AK
1435
1436 if ((dnode->props.size_changed =
1437 (dm_task_get_existing_table_size(dmt) == seg_start) ? 0 : 1))
1438 log_debug("Table size changed from %" PRIu64 " to %"
1439 PRIu64 " for %s",
1440 dm_task_get_existing_table_size(dmt),
1441 seg_start, dnode->name);
ec289b64 1442 }
165e4a11
AK
1443
1444 dnode->props.segment_count = 0;
1445
1446out:
1447 dm_task_destroy(dmt);
1448
1449 return r;
165e4a11
AK
1450}
1451
b4f1578f 1452int dm_tree_preload_children(struct dm_tree_node *dnode,
bb875bb9
AK
1453 const char *uuid_prefix,
1454 size_t uuid_prefix_len)
165e4a11
AK
1455{
1456 void *handle = NULL;
b4f1578f 1457 struct dm_tree_node *child;
165e4a11 1458 struct dm_info newinfo;
165e4a11
AK
1459
1460 /* Preload children first */
b4f1578f 1461 while ((child = dm_tree_next_child(&handle, dnode, 0))) {
165e4a11
AK
1462 /* Skip existing non-device-mapper devices */
1463 if (!child->info.exists && child->info.major)
1464 continue;
1465
1466 /* Ignore if it doesn't belong to this VG */
87f98002
AK
1467 if (child->info.exists &&
1468 !_uuid_prefix_matches(child->uuid, uuid_prefix, uuid_prefix_len))
165e4a11
AK
1469 continue;
1470
b4f1578f 1471 if (dm_tree_node_num_children(child, 0))
e6a6954e 1472 dm_tree_preload_children(child, uuid_prefix, uuid_prefix_len);
165e4a11 1473
165e4a11
AK
1474 /* FIXME Cope if name exists with no uuid? */
1475 if (!child->info.exists) {
1476 if (!_create_node(child)) {
1477 stack;
1478 return 0;
1479 }
1480 }
1481
1482 if (!child->info.inactive_table && child->props.segment_count) {
1483 if (!_load_node(child)) {
1484 stack;
1485 return 0;
1486 }
1487 }
1488
eb91c4ee
MB
1489 /* Propagate device size change change */
1490 if (child->props.size_changed)
1491 dnode->props.size_changed = 1;
1492
bb875bb9 1493 /* Resume device immediately if it has parents and its size changed */
3776c494 1494 if (!dm_tree_node_num_children(child, 1) || !child->props.size_changed)
165e4a11
AK
1495 continue;
1496
7707ea90
AK
1497 if (!child->info.inactive_table && !child->info.suspended)
1498 continue;
1499
fc795d87 1500 if (!_resume_node(child->name, child->info.major, child->info.minor,
52b84409
AK
1501 child->props.read_ahead,
1502 child->props.read_ahead_flags, &newinfo)) {
165e4a11 1503 log_error("Unable to resume %s (%" PRIu32
fc795d87 1504 ":%" PRIu32 ")", child->name, child->info.major,
165e4a11
AK
1505 child->info.minor);
1506 continue;
1507 }
1508
1509 /* Update cached info */
1510 child->info = newinfo;
1511 }
1512
1513 handle = NULL;
1514
1515 return 1;
1516}
1517
165e4a11
AK
1518/*
1519 * Returns 1 if unsure.
1520 */
b4f1578f 1521int dm_tree_children_use_uuid(struct dm_tree_node *dnode,
165e4a11
AK
1522 const char *uuid_prefix,
1523 size_t uuid_prefix_len)
1524{
1525 void *handle = NULL;
b4f1578f 1526 struct dm_tree_node *child = dnode;
165e4a11
AK
1527 const char *uuid;
1528
b4f1578f
AK
1529 while ((child = dm_tree_next_child(&handle, dnode, 0))) {
1530 if (!(uuid = dm_tree_node_get_uuid(child))) {
1531 log_error("Failed to get uuid for dtree node.");
165e4a11
AK
1532 return 1;
1533 }
1534
87f98002 1535 if (_uuid_prefix_matches(uuid, uuid_prefix, uuid_prefix_len))
165e4a11
AK
1536 return 1;
1537
b4f1578f
AK
1538 if (dm_tree_node_num_children(child, 0))
1539 dm_tree_children_use_uuid(child, uuid_prefix, uuid_prefix_len);
165e4a11
AK
1540 }
1541
1542 return 0;
1543}
1544
1545/*
1546 * Target functions
1547 */
b4f1578f 1548static struct load_segment *_add_segment(struct dm_tree_node *dnode, unsigned type, uint64_t size)
165e4a11
AK
1549{
1550 struct load_segment *seg;
1551
b4f1578f
AK
1552 if (!(seg = dm_pool_zalloc(dnode->dtree->mem, sizeof(*seg)))) {
1553 log_error("dtree node segment allocation failed");
165e4a11
AK
1554 return NULL;
1555 }
1556
1557 seg->type = type;
1558 seg->size = size;
1559 seg->area_count = 0;
2c44337b 1560 dm_list_init(&seg->areas);
165e4a11
AK
1561 seg->stripe_size = 0;
1562 seg->persistent = 0;
1563 seg->chunk_size = 0;
1564 seg->cow = NULL;
1565 seg->origin = NULL;
1566
2c44337b 1567 dm_list_add(&dnode->props.segs, &seg->list);
165e4a11
AK
1568 dnode->props.segment_count++;
1569
1570 return seg;
1571}
1572
b4f1578f 1573int dm_tree_node_add_snapshot_origin_target(struct dm_tree_node *dnode,
165e4a11
AK
1574 uint64_t size,
1575 const char *origin_uuid)
1576{
1577 struct load_segment *seg;
b4f1578f 1578 struct dm_tree_node *origin_node;
165e4a11 1579
b4f1578f
AK
1580 if (!(seg = _add_segment(dnode, SEG_SNAPSHOT_ORIGIN, size)))
1581 return_0;
165e4a11 1582
b4f1578f 1583 if (!(origin_node = dm_tree_find_node_by_uuid(dnode->dtree, origin_uuid))) {
165e4a11
AK
1584 log_error("Couldn't find snapshot origin uuid %s.", origin_uuid);
1585 return 0;
1586 }
1587
1588 seg->origin = origin_node;
b4f1578f
AK
1589 if (!_link_tree_nodes(dnode, origin_node))
1590 return_0;
165e4a11 1591
56c28292
AK
1592 /* Resume snapshot origins after new snapshots */
1593 dnode->activation_priority = 1;
1594
165e4a11
AK
1595 return 1;
1596}
1597
b4f1578f 1598int dm_tree_node_add_snapshot_target(struct dm_tree_node *node,
165e4a11
AK
1599 uint64_t size,
1600 const char *origin_uuid,
1601 const char *cow_uuid,
1602 int persistent,
1603 uint32_t chunk_size)
1604{
1605 struct load_segment *seg;
b4f1578f 1606 struct dm_tree_node *origin_node, *cow_node;
165e4a11 1607
b4f1578f
AK
1608 if (!(seg = _add_segment(node, SEG_SNAPSHOT, size)))
1609 return_0;
165e4a11 1610
b4f1578f 1611 if (!(origin_node = dm_tree_find_node_by_uuid(node->dtree, origin_uuid))) {
165e4a11
AK
1612 log_error("Couldn't find snapshot origin uuid %s.", origin_uuid);
1613 return 0;
1614 }
1615
1616 seg->origin = origin_node;
b4f1578f
AK
1617 if (!_link_tree_nodes(node, origin_node))
1618 return_0;
165e4a11 1619
b4f1578f 1620 if (!(cow_node = dm_tree_find_node_by_uuid(node->dtree, cow_uuid))) {
165e4a11
AK
1621 log_error("Couldn't find snapshot origin uuid %s.", cow_uuid);
1622 return 0;
1623 }
1624
1625 seg->cow = cow_node;
b4f1578f
AK
1626 if (!_link_tree_nodes(node, cow_node))
1627 return_0;
165e4a11
AK
1628
1629 seg->persistent = persistent ? 1 : 0;
1630 seg->chunk_size = chunk_size;
1631
1632 return 1;
1633}
1634
b4f1578f 1635int dm_tree_node_add_error_target(struct dm_tree_node *node,
165e4a11
AK
1636 uint64_t size)
1637{
b4f1578f
AK
1638 if (!_add_segment(node, SEG_ERROR, size))
1639 return_0;
165e4a11
AK
1640
1641 return 1;
1642}
1643
b4f1578f 1644int dm_tree_node_add_zero_target(struct dm_tree_node *node,
165e4a11
AK
1645 uint64_t size)
1646{
b4f1578f
AK
1647 if (!_add_segment(node, SEG_ZERO, size))
1648 return_0;
165e4a11
AK
1649
1650 return 1;
1651}
1652
b4f1578f 1653int dm_tree_node_add_linear_target(struct dm_tree_node *node,
165e4a11
AK
1654 uint64_t size)
1655{
b4f1578f
AK
1656 if (!_add_segment(node, SEG_LINEAR, size))
1657 return_0;
165e4a11
AK
1658
1659 return 1;
1660}
1661
b4f1578f 1662int dm_tree_node_add_striped_target(struct dm_tree_node *node,
165e4a11
AK
1663 uint64_t size,
1664 uint32_t stripe_size)
1665{
1666 struct load_segment *seg;
1667
b4f1578f
AK
1668 if (!(seg = _add_segment(node, SEG_STRIPED, size)))
1669 return_0;
165e4a11
AK
1670
1671 seg->stripe_size = stripe_size;
1672
1673 return 1;
1674}
1675
b4f1578f 1676int dm_tree_node_add_mirror_target_log(struct dm_tree_node *node,
165e4a11
AK
1677 uint32_t region_size,
1678 unsigned clustered,
1679 const char *log_uuid,
ce7ed2c0
AK
1680 unsigned area_count,
1681 uint32_t flags)
165e4a11 1682{
908db078 1683 struct dm_tree_node *log_node = NULL;
165e4a11
AK
1684 struct load_segment *seg;
1685
1686 if (!node->props.segment_count) {
1687 log_error("Internal error: Attempt to add target area to missing segment.");
1688 return 0;
1689 }
1690
2c44337b 1691 seg = dm_list_item(dm_list_last(&node->props.segs), struct load_segment);
165e4a11 1692
24b026e3 1693 if (log_uuid) {
67b25ed4
AK
1694 if (!(seg->uuid = dm_pool_strdup(node->dtree->mem, log_uuid))) {
1695 log_error("log uuid pool_strdup failed");
1696 return 0;
1697 }
9723090c
AK
1698 if (!(flags & DM_CORELOG)) {
1699 if (!(log_node = dm_tree_find_node_by_uuid(node->dtree, log_uuid))) {
1700 log_error("Couldn't find mirror log uuid %s.", log_uuid);
1701 return 0;
1702 }
1703
1704 if (!_link_tree_nodes(node, log_node))
1705 return_0;
1706 }
165e4a11
AK
1707 }
1708
1709 seg->log = log_node;
165e4a11
AK
1710 seg->region_size = region_size;
1711 seg->clustered = clustered;
1712 seg->mirror_area_count = area_count;
dbcb64b8 1713 seg->flags = flags;
165e4a11
AK
1714
1715 return 1;
1716}
1717
b4f1578f 1718int dm_tree_node_add_mirror_target(struct dm_tree_node *node,
165e4a11
AK
1719 uint64_t size)
1720{
1721 struct load_segment *seg;
1722
b4f1578f
AK
1723 if (!(seg = _add_segment(node, SEG_MIRRORED, size)))
1724 return_0;
165e4a11
AK
1725
1726 return 1;
1727}
1728
b4f1578f 1729static int _add_area(struct dm_tree_node *node, struct load_segment *seg, struct dm_tree_node *dev_node, uint64_t offset)
165e4a11
AK
1730{
1731 struct seg_area *area;
1732
b4f1578f 1733 if (!(area = dm_pool_zalloc(node->dtree->mem, sizeof (*area)))) {
165e4a11
AK
1734 log_error("Failed to allocate target segment area.");
1735 return 0;
1736 }
1737
1738 area->dev_node = dev_node;
1739 area->offset = offset;
1740
2c44337b 1741 dm_list_add(&seg->areas, &area->list);
165e4a11
AK
1742 seg->area_count++;
1743
1744 return 1;
1745}
1746
b4f1578f 1747int dm_tree_node_add_target_area(struct dm_tree_node *node,
165e4a11
AK
1748 const char *dev_name,
1749 const char *uuid,
1750 uint64_t offset)
1751{
1752 struct load_segment *seg;
1753 struct stat info;
b4f1578f 1754 struct dm_tree_node *dev_node;
165e4a11
AK
1755
1756 if ((!dev_name || !*dev_name) && (!uuid || !*uuid)) {
b4f1578f 1757 log_error("dm_tree_node_add_target_area called without device");
165e4a11
AK
1758 return 0;
1759 }
1760
1761 if (uuid) {
b4f1578f 1762 if (!(dev_node = dm_tree_find_node_by_uuid(node->dtree, uuid))) {
165e4a11
AK
1763 log_error("Couldn't find area uuid %s.", uuid);
1764 return 0;
1765 }
b4f1578f
AK
1766 if (!_link_tree_nodes(node, dev_node))
1767 return_0;
165e4a11
AK
1768 } else {
1769 if (stat(dev_name, &info) < 0) {
1770 log_error("Device %s not found.", dev_name);
1771 return 0;
1772 }
1773
1774 if (!S_ISBLK(info.st_mode)) {
1775 log_error("Device %s is not a block device.", dev_name);
1776 return 0;
1777 }
1778
1779 /* FIXME Check correct macro use */
b4f1578f
AK
1780 if (!(dev_node = _add_dev(node->dtree, node, MAJOR(info.st_rdev), MINOR(info.st_rdev))))
1781 return_0;
165e4a11
AK
1782 }
1783
1784 if (!node->props.segment_count) {
1785 log_error("Internal error: Attempt to add target area to missing segment.");
1786 return 0;
1787 }
1788
2c44337b 1789 seg = dm_list_item(dm_list_last(&node->props.segs), struct load_segment);
165e4a11 1790
b4f1578f
AK
1791 if (!_add_area(node, seg, dev_node, offset))
1792 return_0;
165e4a11
AK
1793
1794 return 1;
db208f51 1795}
This page took 0.248552 seconds and 5 git commands to generate.