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