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