]> sourceware.org Git - lvm2.git/blame - libdm/libdm-deptree.c
Replace incomplete pvmove activation failure recovery code with a message.
[lvm2.git] / libdm / libdm-deptree.c
CommitLineData
3d0480ed 1/*
4ef2bf27 2 * Copyright (C) 2005-2010 Red Hat, Inc. All rights reserved.
3d0480ed
AK
3 *
4 * This file is part of the device-mapper userspace tools.
5 *
6 * This copyrighted material is made available to anyone wishing to use,
7 * modify, copy, or redistribute it subject to the terms and conditions
8 * of the GNU Lesser General Public License v.2.1.
9 *
10 * You should have received a copy of the GNU Lesser General Public License
11 * along with this program; if not, write to the Free Software Foundation,
12 * Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
13 */
14
3e5b6ed2 15#include "dmlib.h"
3d0480ed
AK
16#include "libdm-targets.h"
17#include "libdm-common.h"
3d0480ed 18#include "kdev_t.h"
0782ad50 19#include "dm-ioctl.h"
3d0480ed
AK
20
21#include <stdarg.h>
22#include <sys/param.h>
8f26e18c 23#include <sys/utsname.h>
3d0480ed 24
165e4a11
AK
25#define MAX_TARGET_PARAMSIZE 500000
26
87f98002
AK
27/* FIXME Fix interface so this is used only by LVM */
28#define UUID_PREFIX "LVM-"
29
b262f3e1
ZK
30#define REPLICATOR_LOCAL_SITE 0
31
165e4a11
AK
32/* Supported segment types */
33enum {
12ca060e
MB
34 SEG_CRYPT,
35 SEG_ERROR,
165e4a11
AK
36 SEG_LINEAR,
37 SEG_MIRRORED,
b262f3e1
ZK
38 SEG_REPLICATOR,
39 SEG_REPLICATOR_DEV,
165e4a11
AK
40 SEG_SNAPSHOT,
41 SEG_SNAPSHOT_ORIGIN,
aa6f4e51 42 SEG_SNAPSHOT_MERGE,
165e4a11
AK
43 SEG_STRIPED,
44 SEG_ZERO,
cac52ca4
JEB
45 SEG_RAID1,
46 SEG_RAID4,
47 SEG_RAID5_LA,
48 SEG_RAID5_RA,
49 SEG_RAID5_LS,
50 SEG_RAID5_RS,
51 SEG_RAID6_ZR,
52 SEG_RAID6_NR,
53 SEG_RAID6_NC,
54 SEG_LAST,
165e4a11 55};
b4f1578f 56
165e4a11
AK
57/* FIXME Add crypt and multipath support */
58
59struct {
60 unsigned type;
61 const char *target;
62} dm_segtypes[] = {
12ca060e 63 { SEG_CRYPT, "crypt" },
165e4a11
AK
64 { SEG_ERROR, "error" },
65 { SEG_LINEAR, "linear" },
66 { SEG_MIRRORED, "mirror" },
b262f3e1
ZK
67 { SEG_REPLICATOR, "replicator" },
68 { SEG_REPLICATOR_DEV, "replicator-dev" },
165e4a11
AK
69 { SEG_SNAPSHOT, "snapshot" },
70 { SEG_SNAPSHOT_ORIGIN, "snapshot-origin" },
aa6f4e51 71 { SEG_SNAPSHOT_MERGE, "snapshot-merge" },
165e4a11
AK
72 { SEG_STRIPED, "striped" },
73 { SEG_ZERO, "zero"},
cac52ca4
JEB
74 { SEG_RAID1, "raid1"},
75 { SEG_RAID4, "raid4"},
76 { SEG_RAID5_LA, "raid5_la"},
77 { SEG_RAID5_RA, "raid5_ra"},
78 { SEG_RAID5_LS, "raid5_ls"},
79 { SEG_RAID5_RS, "raid5_rs"},
80 { SEG_RAID6_ZR, "raid6_zr"},
81 { SEG_RAID6_NR, "raid6_nr"},
82 { SEG_RAID6_NC, "raid6_nc"},
83 { SEG_RAID5_LS, "raid5"}, /* same as "raid5_ls" (default for MD also) */
84 { SEG_RAID6_ZR, "raid6"}, /* same as "raid6_zr" */
85 { SEG_LAST, NULL },
165e4a11
AK
86};
87
88/* Some segment types have a list of areas of other devices attached */
89struct seg_area {
2c44337b 90 struct dm_list list;
165e4a11 91
b4f1578f 92 struct dm_tree_node *dev_node;
165e4a11
AK
93
94 uint64_t offset;
b262f3e1
ZK
95
96 unsigned rsite_index; /* Replicator site index */
97 struct dm_tree_node *slog; /* Replicator sync log node */
98 uint64_t region_size; /* Replicator sync log size */
99 uint32_t flags; /* Replicator sync log flags */
100};
101
102/* Replicator-log has a list of sites */
103/* FIXME: maybe move to seg_area too? */
104struct replicator_site {
105 struct dm_list list;
106
107 unsigned rsite_index;
108 dm_replicator_mode_t mode;
109 uint32_t async_timeout;
110 uint32_t fall_behind_ios;
111 uint64_t fall_behind_data;
165e4a11
AK
112};
113
114/* Per-segment properties */
115struct load_segment {
2c44337b 116 struct dm_list list;
165e4a11
AK
117
118 unsigned type;
119
120 uint64_t size;
121
b262f3e1
ZK
122 unsigned area_count; /* Linear + Striped + Mirrored + Crypt + Replicator */
123 struct dm_list areas; /* Linear + Striped + Mirrored + Crypt + Replicator */
165e4a11 124
cac52ca4 125 uint32_t stripe_size; /* Striped + raid */
165e4a11
AK
126
127 int persistent; /* Snapshot */
128 uint32_t chunk_size; /* Snapshot */
b4f1578f
AK
129 struct dm_tree_node *cow; /* Snapshot */
130 struct dm_tree_node *origin; /* Snapshot + Snapshot origin */
aa6f4e51 131 struct dm_tree_node *merge; /* Snapshot */
165e4a11 132
b262f3e1 133 struct dm_tree_node *log; /* Mirror + Replicator */
cac52ca4 134 uint32_t region_size; /* Mirror + raid */
165e4a11
AK
135 unsigned clustered; /* Mirror */
136 unsigned mirror_area_count; /* Mirror */
dbcb64b8 137 uint32_t flags; /* Mirror log */
67b25ed4 138 char *uuid; /* Clustered mirror log */
12ca060e
MB
139
140 const char *cipher; /* Crypt */
141 const char *chainmode; /* Crypt */
142 const char *iv; /* Crypt */
143 uint64_t iv_offset; /* Crypt */
144 const char *key; /* Crypt */
b262f3e1
ZK
145
146 const char *rlog_type; /* Replicator */
147 struct dm_list rsites; /* Replicator */
148 unsigned rsite_count; /* Replicator */
149 unsigned rdevice_count; /* Replicator */
150 struct dm_tree_node *replicator;/* Replicator-dev */
151 uint64_t rdevice_index; /* Replicator-dev */
f439e65b 152
40e5fd8b 153 uint64_t rebuilds; /* raid */
165e4a11
AK
154};
155
156/* Per-device properties */
157struct load_properties {
158 int read_only;
159 uint32_t major;
160 uint32_t minor;
161
52b84409
AK
162 uint32_t read_ahead;
163 uint32_t read_ahead_flags;
164
165e4a11 165 unsigned segment_count;
bb875bb9 166 unsigned size_changed;
2c44337b 167 struct dm_list segs;
165e4a11
AK
168
169 const char *new_name;
566515c0
PR
170
171 /* If immediate_dev_node is set to 1, try to create the dev node
172 * as soon as possible (e.g. in preload stage even during traversal
173 * and processing of dm tree). This will also flush all stacked dev
174 * node operations, synchronizing with udev.
175 */
df390f17
AK
176 unsigned immediate_dev_node;
177
178 /*
179 * If the device size changed from zero and this is set,
180 * don't resume the device immediately, even if the device
181 * has parents. This works provided the parents do not
182 * validate the device size and is required by pvmove to
183 * avoid starting the mirror resync operation too early.
184 */
185 unsigned delay_resume_if_new;
165e4a11
AK
186};
187
188/* Two of these used to join two nodes with uses and used_by. */
b4f1578f 189struct dm_tree_link {
2c44337b 190 struct dm_list list;
b4f1578f 191 struct dm_tree_node *node;
165e4a11
AK
192};
193
b4f1578f
AK
194struct dm_tree_node {
195 struct dm_tree *dtree;
3d0480ed 196
40e5fd8b
AK
197 const char *name;
198 const char *uuid;
199 struct dm_info info;
3d0480ed 200
40e5fd8b
AK
201 struct dm_list uses; /* Nodes this node uses */
202 struct dm_list used_by; /* Nodes that use this node */
165e4a11 203
56c28292
AK
204 int activation_priority; /* 0 gets activated first */
205
f16aea9e
PR
206 uint16_t udev_flags; /* Udev control flags */
207
165e4a11
AK
208 void *context; /* External supplied context */
209
210 struct load_properties props; /* For creation/table (re)load */
76d1aec8
ZK
211
212 /*
213 * If presuspend of child node is needed
214 * Note: only direct child is allowed
215 */
216 struct dm_tree_node *presuspend_node;
3d0480ed
AK
217};
218
b4f1578f 219struct dm_tree {
a3f6b2ce
AK
220 struct dm_pool *mem;
221 struct dm_hash_table *devs;
165e4a11 222 struct dm_hash_table *uuids;
b4f1578f 223 struct dm_tree_node root;
c55b1410 224 int skip_lockfs; /* 1 skips lockfs (for non-snapshots) */
787200ef
PR
225 int no_flush; /* 1 sets noflush (mirrors/multipath) */
226 int retry_remove; /* 1 retries remove if not successful */
bd90c6b2 227 uint32_t cookie;
3d0480ed
AK
228};
229
b4f1578f 230struct dm_tree *dm_tree_create(void)
3d0480ed 231{
b4f1578f 232 struct dm_tree *dtree;
3d0480ed 233
ac0252ca 234 if (!(dtree = dm_zalloc(sizeof(*dtree)))) {
b4f1578f 235 log_error("dm_tree_create malloc failed");
3d0480ed
AK
236 return NULL;
237 }
238
b4f1578f 239 dtree->root.dtree = dtree;
2c44337b
AK
240 dm_list_init(&dtree->root.uses);
241 dm_list_init(&dtree->root.used_by);
c55b1410 242 dtree->skip_lockfs = 0;
b9ffd32c 243 dtree->no_flush = 0;
3d0480ed 244
b4f1578f
AK
245 if (!(dtree->mem = dm_pool_create("dtree", 1024))) {
246 log_error("dtree pool creation failed");
247 dm_free(dtree);
3d0480ed
AK
248 return NULL;
249 }
250
b4f1578f
AK
251 if (!(dtree->devs = dm_hash_create(8))) {
252 log_error("dtree hash creation failed");
253 dm_pool_destroy(dtree->mem);
254 dm_free(dtree);
3d0480ed
AK
255 return NULL;
256 }
257
b4f1578f
AK
258 if (!(dtree->uuids = dm_hash_create(32))) {
259 log_error("dtree uuid hash creation failed");
260 dm_hash_destroy(dtree->devs);
261 dm_pool_destroy(dtree->mem);
262 dm_free(dtree);
165e4a11
AK
263 return NULL;
264 }
265
b4f1578f 266 return dtree;
3d0480ed
AK
267}
268
b4f1578f 269void dm_tree_free(struct dm_tree *dtree)
3d0480ed 270{
b4f1578f 271 if (!dtree)
3d0480ed
AK
272 return;
273
b4f1578f
AK
274 dm_hash_destroy(dtree->uuids);
275 dm_hash_destroy(dtree->devs);
276 dm_pool_destroy(dtree->mem);
277 dm_free(dtree);
3d0480ed
AK
278}
279
04bde319
ZK
280static int _nodes_are_linked(const struct dm_tree_node *parent,
281 const struct dm_tree_node *child)
3d0480ed 282{
b4f1578f 283 struct dm_tree_link *dlink;
3d0480ed 284
2c44337b 285 dm_list_iterate_items(dlink, &parent->uses)
3d0480ed
AK
286 if (dlink->node == child)
287 return 1;
3d0480ed
AK
288
289 return 0;
290}
291
2c44337b 292static int _link(struct dm_list *list, struct dm_tree_node *node)
3d0480ed 293{
b4f1578f 294 struct dm_tree_link *dlink;
3d0480ed 295
b4f1578f
AK
296 if (!(dlink = dm_pool_alloc(node->dtree->mem, sizeof(*dlink)))) {
297 log_error("dtree link allocation failed");
3d0480ed
AK
298 return 0;
299 }
300
301 dlink->node = node;
2c44337b 302 dm_list_add(list, &dlink->list);
3d0480ed
AK
303
304 return 1;
305}
306
b4f1578f
AK
307static int _link_nodes(struct dm_tree_node *parent,
308 struct dm_tree_node *child)
3d0480ed
AK
309{
310 if (_nodes_are_linked(parent, child))
311 return 1;
312
313 if (!_link(&parent->uses, child))
314 return 0;
315
316 if (!_link(&child->used_by, parent))
317 return 0;
318
319 return 1;
320}
321
2c44337b 322static void _unlink(struct dm_list *list, struct dm_tree_node *node)
3d0480ed 323{
b4f1578f 324 struct dm_tree_link *dlink;
3d0480ed 325
2c44337b 326 dm_list_iterate_items(dlink, list)
3d0480ed 327 if (dlink->node == node) {
2c44337b 328 dm_list_del(&dlink->list);
3d0480ed
AK
329 break;
330 }
3d0480ed
AK
331}
332
b4f1578f
AK
333static void _unlink_nodes(struct dm_tree_node *parent,
334 struct dm_tree_node *child)
3d0480ed
AK
335{
336 if (!_nodes_are_linked(parent, child))
337 return;
338
339 _unlink(&parent->uses, child);
340 _unlink(&child->used_by, parent);
341}
342
b4f1578f 343static int _add_to_toplevel(struct dm_tree_node *node)
165e4a11 344{
b4f1578f 345 return _link_nodes(&node->dtree->root, node);
165e4a11
AK
346}
347
b4f1578f 348static void _remove_from_toplevel(struct dm_tree_node *node)
3d0480ed 349{
b1ebf028 350 _unlink_nodes(&node->dtree->root, node);
3d0480ed
AK
351}
352
b4f1578f 353static int _add_to_bottomlevel(struct dm_tree_node *node)
3d0480ed 354{
b4f1578f 355 return _link_nodes(node, &node->dtree->root);
3d0480ed
AK
356}
357
b4f1578f 358static void _remove_from_bottomlevel(struct dm_tree_node *node)
165e4a11 359{
b1ebf028 360 _unlink_nodes(node, &node->dtree->root);
165e4a11
AK
361}
362
b4f1578f 363static int _link_tree_nodes(struct dm_tree_node *parent, struct dm_tree_node *child)
165e4a11
AK
364{
365 /* Don't link to root node if child already has a parent */
f77736ca 366 if (parent == &parent->dtree->root) {
b4f1578f 367 if (dm_tree_node_num_children(child, 1))
165e4a11
AK
368 return 1;
369 } else
370 _remove_from_toplevel(child);
371
f77736ca 372 if (child == &child->dtree->root) {
b4f1578f 373 if (dm_tree_node_num_children(parent, 0))
165e4a11
AK
374 return 1;
375 } else
376 _remove_from_bottomlevel(parent);
377
378 return _link_nodes(parent, child);
379}
380
b4f1578f 381static struct dm_tree_node *_create_dm_tree_node(struct dm_tree *dtree,
3d0480ed
AK
382 const char *name,
383 const char *uuid,
165e4a11 384 struct dm_info *info,
f16aea9e
PR
385 void *context,
386 uint16_t udev_flags)
3d0480ed 387{
b4f1578f 388 struct dm_tree_node *node;
3d0480ed
AK
389 uint64_t dev;
390
b4f1578f
AK
391 if (!(node = dm_pool_zalloc(dtree->mem, sizeof(*node)))) {
392 log_error("_create_dm_tree_node alloc failed");
3d0480ed
AK
393 return NULL;
394 }
395
b4f1578f 396 node->dtree = dtree;
3d0480ed
AK
397
398 node->name = name;
399 node->uuid = uuid;
400 node->info = *info;
165e4a11 401 node->context = context;
f16aea9e 402 node->udev_flags = udev_flags;
56c28292 403 node->activation_priority = 0;
3d0480ed 404
2c44337b
AK
405 dm_list_init(&node->uses);
406 dm_list_init(&node->used_by);
407 dm_list_init(&node->props.segs);
3d0480ed
AK
408
409 dev = MKDEV(info->major, info->minor);
410
b4f1578f 411 if (!dm_hash_insert_binary(dtree->devs, (const char *) &dev,
3d0480ed 412 sizeof(dev), node)) {
b4f1578f
AK
413 log_error("dtree node hash insertion failed");
414 dm_pool_free(dtree->mem, node);
3d0480ed
AK
415 return NULL;
416 }
417
165e4a11 418 if (uuid && *uuid &&
b4f1578f
AK
419 !dm_hash_insert(dtree->uuids, uuid, node)) {
420 log_error("dtree uuid hash insertion failed");
421 dm_hash_remove_binary(dtree->devs, (const char *) &dev,
165e4a11 422 sizeof(dev));
b4f1578f 423 dm_pool_free(dtree->mem, node);
165e4a11
AK
424 return NULL;
425 }
426
3d0480ed
AK
427 return node;
428}
429
b4f1578f 430static struct dm_tree_node *_find_dm_tree_node(struct dm_tree *dtree,
3d0480ed
AK
431 uint32_t major, uint32_t minor)
432{
433 uint64_t dev = MKDEV(major, minor);
434
b4f1578f 435 return dm_hash_lookup_binary(dtree->devs, (const char *) &dev,
3d0480ed
AK
436 sizeof(dev));
437}
438
b4f1578f 439static struct dm_tree_node *_find_dm_tree_node_by_uuid(struct dm_tree *dtree,
165e4a11
AK
440 const char *uuid)
441{
87f98002
AK
442 struct dm_tree_node *node;
443
444 if ((node = dm_hash_lookup(dtree->uuids, uuid)))
445 return node;
446
447 if (strncmp(uuid, UUID_PREFIX, sizeof(UUID_PREFIX) - 1))
448 return NULL;
449
450 return dm_hash_lookup(dtree->uuids, uuid + sizeof(UUID_PREFIX) - 1);
165e4a11
AK
451}
452
a3f6b2ce 453static int _deps(struct dm_task **dmt, struct dm_pool *mem, uint32_t major, uint32_t minor,
3d0480ed
AK
454 const char **name, const char **uuid,
455 struct dm_info *info, struct dm_deps **deps)
456{
457 memset(info, 0, sizeof(*info));
458
459 if (!dm_is_dm_major(major)) {
460 *name = "";
461 *uuid = "";
462 *deps = NULL;
463 info->major = major;
464 info->minor = minor;
465 info->exists = 0;
165e4a11
AK
466 info->live_table = 0;
467 info->inactive_table = 0;
468 info->read_only = 0;
3d0480ed
AK
469 return 1;
470 }
471
472 if (!(*dmt = dm_task_create(DM_DEVICE_DEPS))) {
473 log_error("deps dm_task creation failed");
474 return 0;
475 }
476
b4f1578f
AK
477 if (!dm_task_set_major(*dmt, major)) {
478 log_error("_deps: failed to set major for (%" PRIu32 ":%" PRIu32 ")",
479 major, minor);
3d0480ed 480 goto failed;
b4f1578f 481 }
3d0480ed 482
b4f1578f
AK
483 if (!dm_task_set_minor(*dmt, minor)) {
484 log_error("_deps: failed to set minor for (%" PRIu32 ":%" PRIu32 ")",
485 major, minor);
3d0480ed 486 goto failed;
b4f1578f 487 }
3d0480ed 488
b4f1578f
AK
489 if (!dm_task_run(*dmt)) {
490 log_error("_deps: task run failed for (%" PRIu32 ":%" PRIu32 ")",
491 major, minor);
3d0480ed 492 goto failed;
b4f1578f 493 }
3d0480ed 494
b4f1578f
AK
495 if (!dm_task_get_info(*dmt, info)) {
496 log_error("_deps: failed to get info for (%" PRIu32 ":%" PRIu32 ")",
497 major, minor);
3d0480ed 498 goto failed;
b4f1578f 499 }
3d0480ed
AK
500
501 if (!info->exists) {
502 *name = "";
503 *uuid = "";
504 *deps = NULL;
505 } else {
506 if (info->major != major) {
b4f1578f 507 log_error("Inconsistent dtree major number: %u != %u",
3d0480ed
AK
508 major, info->major);
509 goto failed;
510 }
511 if (info->minor != minor) {
b4f1578f 512 log_error("Inconsistent dtree minor number: %u != %u",
3d0480ed
AK
513 minor, info->minor);
514 goto failed;
515 }
a3f6b2ce 516 if (!(*name = dm_pool_strdup(mem, dm_task_get_name(*dmt)))) {
3d0480ed
AK
517 log_error("name pool_strdup failed");
518 goto failed;
519 }
a3f6b2ce 520 if (!(*uuid = dm_pool_strdup(mem, dm_task_get_uuid(*dmt)))) {
3d0480ed
AK
521 log_error("uuid pool_strdup failed");
522 goto failed;
523 }
524 *deps = dm_task_get_deps(*dmt);
525 }
526
527 return 1;
528
529failed:
530 dm_task_destroy(*dmt);
531 return 0;
532}
533
b4f1578f
AK
534static struct dm_tree_node *_add_dev(struct dm_tree *dtree,
535 struct dm_tree_node *parent,
cda69e17
PR
536 uint32_t major, uint32_t minor,
537 uint16_t udev_flags)
3d0480ed
AK
538{
539 struct dm_task *dmt = NULL;
540 struct dm_info info;
541 struct dm_deps *deps = NULL;
542 const char *name = NULL;
543 const char *uuid = NULL;
b4f1578f 544 struct dm_tree_node *node = NULL;
3d0480ed 545 uint32_t i;
3d0480ed
AK
546 int new = 0;
547
548 /* Already in tree? */
b4f1578f
AK
549 if (!(node = _find_dm_tree_node(dtree, major, minor))) {
550 if (!_deps(&dmt, dtree->mem, major, minor, &name, &uuid, &info, &deps))
551 return_NULL;
3d0480ed 552
f16aea9e 553 if (!(node = _create_dm_tree_node(dtree, name, uuid, &info,
cda69e17 554 NULL, udev_flags)))
b4f1578f 555 goto_out;
3d0480ed
AK
556 new = 1;
557 }
558
165e4a11
AK
559 if (!_link_tree_nodes(parent, node)) {
560 node = NULL;
b4f1578f 561 goto_out;
165e4a11 562 }
3d0480ed
AK
563
564 /* If node was already in tree, no need to recurse. */
565 if (!new)
165e4a11 566 goto out;
3d0480ed
AK
567
568 /* Can't recurse if not a mapped device or there are no dependencies */
569 if (!node->info.exists || !deps->count) {
b4f1578f
AK
570 if (!_add_to_bottomlevel(node)) {
571 stack;
165e4a11 572 node = NULL;
b4f1578f 573 }
165e4a11 574 goto out;
3d0480ed
AK
575 }
576
577 /* Add dependencies to tree */
578 for (i = 0; i < deps->count; i++)
b4f1578f 579 if (!_add_dev(dtree, node, MAJOR(deps->device[i]),
cda69e17 580 MINOR(deps->device[i]), udev_flags)) {
165e4a11 581 node = NULL;
b4f1578f 582 goto_out;
165e4a11 583 }
3d0480ed 584
3d0480ed
AK
585out:
586 if (dmt)
587 dm_task_destroy(dmt);
588
165e4a11
AK
589 return node;
590}
591
b4f1578f 592static int _node_clear_table(struct dm_tree_node *dnode)
165e4a11
AK
593{
594 struct dm_task *dmt;
595 struct dm_info *info;
596 const char *name;
597 int r;
598
599 if (!(info = &dnode->info)) {
b4f1578f 600 log_error("_node_clear_table failed: missing info");
165e4a11
AK
601 return 0;
602 }
603
b4f1578f
AK
604 if (!(name = dm_tree_node_get_name(dnode))) {
605 log_error("_node_clear_table failed: missing name");
165e4a11
AK
606 return 0;
607 }
608
609 /* Is there a table? */
610 if (!info->exists || !info->inactive_table)
611 return 1;
612
613 log_verbose("Clearing inactive table %s (%" PRIu32 ":%" PRIu32 ")",
614 name, info->major, info->minor);
615
616 if (!(dmt = dm_task_create(DM_DEVICE_CLEAR))) {
165e4a11
AK
617 log_error("Table clear dm_task creation failed for %s", name);
618 return 0;
619 }
620
621 if (!dm_task_set_major(dmt, info->major) ||
622 !dm_task_set_minor(dmt, info->minor)) {
623 log_error("Failed to set device number for %s table clear", name);
624 dm_task_destroy(dmt);
625 return 0;
626 }
627
628 r = dm_task_run(dmt);
629
630 if (!dm_task_get_info(dmt, info)) {
b4f1578f 631 log_error("_node_clear_table failed: info missing after running task for %s", name);
165e4a11
AK
632 r = 0;
633 }
634
635 dm_task_destroy(dmt);
636
3d0480ed
AK
637 return r;
638}
639
b4f1578f 640struct dm_tree_node *dm_tree_add_new_dev(struct dm_tree *dtree,
165e4a11
AK
641 const char *name,
642 const char *uuid,
643 uint32_t major, uint32_t minor,
644 int read_only,
645 int clear_inactive,
646 void *context)
647{
b4f1578f 648 struct dm_tree_node *dnode;
165e4a11
AK
649 struct dm_info info;
650 const char *name2;
651 const char *uuid2;
652
653 /* Do we need to add node to tree? */
b4f1578f
AK
654 if (!(dnode = dm_tree_find_node_by_uuid(dtree, uuid))) {
655 if (!(name2 = dm_pool_strdup(dtree->mem, name))) {
165e4a11
AK
656 log_error("name pool_strdup failed");
657 return NULL;
658 }
b4f1578f 659 if (!(uuid2 = dm_pool_strdup(dtree->mem, uuid))) {
165e4a11
AK
660 log_error("uuid pool_strdup failed");
661 return NULL;
662 }
663
664 info.major = 0;
665 info.minor = 0;
666 info.exists = 0;
667 info.live_table = 0;
668 info.inactive_table = 0;
669 info.read_only = 0;
670
f16aea9e
PR
671 if (!(dnode = _create_dm_tree_node(dtree, name2, uuid2, &info,
672 context, 0)))
b4f1578f 673 return_NULL;
165e4a11
AK
674
675 /* Attach to root node until a table is supplied */
b4f1578f
AK
676 if (!_add_to_toplevel(dnode) || !_add_to_bottomlevel(dnode))
677 return_NULL;
165e4a11
AK
678
679 dnode->props.major = major;
680 dnode->props.minor = minor;
681 dnode->props.new_name = NULL;
bb875bb9 682 dnode->props.size_changed = 0;
165e4a11
AK
683 } else if (strcmp(name, dnode->name)) {
684 /* Do we need to rename node? */
b4f1578f 685 if (!(dnode->props.new_name = dm_pool_strdup(dtree->mem, name))) {
165e4a11
AK
686 log_error("name pool_strdup failed");
687 return 0;
688 }
689 }
690
691 dnode->props.read_only = read_only ? 1 : 0;
52b84409
AK
692 dnode->props.read_ahead = DM_READ_AHEAD_AUTO;
693 dnode->props.read_ahead_flags = 0;
165e4a11 694
b4f1578f
AK
695 if (clear_inactive && !_node_clear_table(dnode))
696 return_NULL;
165e4a11
AK
697
698 dnode->context = context;
f16aea9e 699 dnode->udev_flags = 0;
165e4a11
AK
700
701 return dnode;
702}
703
f16aea9e
PR
704struct dm_tree_node *dm_tree_add_new_dev_with_udev_flags(struct dm_tree *dtree,
705 const char *name,
706 const char *uuid,
707 uint32_t major,
708 uint32_t minor,
709 int read_only,
710 int clear_inactive,
711 void *context,
712 uint16_t udev_flags)
713{
714 struct dm_tree_node *node;
715
716 if ((node = dm_tree_add_new_dev(dtree, name, uuid, major, minor, read_only,
717 clear_inactive, context)))
718 node->udev_flags = udev_flags;
719
720 return node;
721}
722
723
52b84409
AK
724void dm_tree_node_set_read_ahead(struct dm_tree_node *dnode,
725 uint32_t read_ahead,
726 uint32_t read_ahead_flags)
08e64ce5 727{
52b84409
AK
728 dnode->props.read_ahead = read_ahead;
729 dnode->props.read_ahead_flags = read_ahead_flags;
730}
731
76d1aec8
ZK
732void dm_tree_node_set_presuspend_node(struct dm_tree_node *node,
733 struct dm_tree_node *presuspend_node)
734{
735 node->presuspend_node = presuspend_node;
736}
737
b4f1578f 738int dm_tree_add_dev(struct dm_tree *dtree, uint32_t major, uint32_t minor)
3d0480ed 739{
cda69e17
PR
740 return _add_dev(dtree, &dtree->root, major, minor, 0) ? 1 : 0;
741}
742
743int dm_tree_add_dev_with_udev_flags(struct dm_tree *dtree, uint32_t major,
744 uint32_t minor, uint16_t udev_flags)
745{
746 return _add_dev(dtree, &dtree->root, major, minor, udev_flags) ? 1 : 0;
3d0480ed
AK
747}
748
04bde319 749const char *dm_tree_node_get_name(const struct dm_tree_node *node)
3d0480ed
AK
750{
751 return node->info.exists ? node->name : "";
752}
753
04bde319 754const char *dm_tree_node_get_uuid(const struct dm_tree_node *node)
3d0480ed
AK
755{
756 return node->info.exists ? node->uuid : "";
757}
758
04bde319 759const struct dm_info *dm_tree_node_get_info(const struct dm_tree_node *node)
3d0480ed
AK
760{
761 return &node->info;
762}
763
04bde319 764void *dm_tree_node_get_context(const struct dm_tree_node *node)
165e4a11
AK
765{
766 return node->context;
767}
768
04bde319 769int dm_tree_node_size_changed(const struct dm_tree_node *dnode)
eb91c4ee
MB
770{
771 return dnode->props.size_changed;
772}
773
04bde319 774int dm_tree_node_num_children(const struct dm_tree_node *node, uint32_t inverted)
3d0480ed
AK
775{
776 if (inverted) {
b4f1578f 777 if (_nodes_are_linked(&node->dtree->root, node))
3d0480ed 778 return 0;
2c44337b 779 return dm_list_size(&node->used_by);
3d0480ed
AK
780 }
781
b4f1578f 782 if (_nodes_are_linked(node, &node->dtree->root))
3d0480ed
AK
783 return 0;
784
2c44337b 785 return dm_list_size(&node->uses);
3d0480ed
AK
786}
787
2b69db1f
AK
788/*
789 * Returns 1 if no prefix supplied
790 */
791static int _uuid_prefix_matches(const char *uuid, const char *uuid_prefix, size_t uuid_prefix_len)
792{
793 if (!uuid_prefix)
794 return 1;
795
796 if (!strncmp(uuid, uuid_prefix, uuid_prefix_len))
797 return 1;
798
799 /* Handle transition: active device uuids might be missing the prefix */
800 if (uuid_prefix_len <= 4)
801 return 0;
802
87f98002 803 if (!strncmp(uuid, UUID_PREFIX, sizeof(UUID_PREFIX) - 1))
872dea04
AK
804 return 0;
805
87f98002 806 if (strncmp(uuid_prefix, UUID_PREFIX, sizeof(UUID_PREFIX) - 1))
2b69db1f
AK
807 return 0;
808
87f98002 809 if (!strncmp(uuid, uuid_prefix + sizeof(UUID_PREFIX) - 1, uuid_prefix_len - (sizeof(UUID_PREFIX) - 1)))
2b69db1f
AK
810 return 1;
811
812 return 0;
813}
814
690a5da2
AK
815/*
816 * Returns 1 if no children.
817 */
b4f1578f 818static int _children_suspended(struct dm_tree_node *node,
690a5da2
AK
819 uint32_t inverted,
820 const char *uuid_prefix,
821 size_t uuid_prefix_len)
822{
2c44337b 823 struct dm_list *list;
b4f1578f 824 struct dm_tree_link *dlink;
690a5da2
AK
825 const struct dm_info *dinfo;
826 const char *uuid;
827
828 if (inverted) {
b4f1578f 829 if (_nodes_are_linked(&node->dtree->root, node))
690a5da2
AK
830 return 1;
831 list = &node->used_by;
832 } else {
b4f1578f 833 if (_nodes_are_linked(node, &node->dtree->root))
690a5da2
AK
834 return 1;
835 list = &node->uses;
836 }
837
2c44337b 838 dm_list_iterate_items(dlink, list) {
b4f1578f 839 if (!(uuid = dm_tree_node_get_uuid(dlink->node))) {
690a5da2
AK
840 stack;
841 continue;
842 }
843
844 /* Ignore if it doesn't belong to this VG */
2b69db1f 845 if (!_uuid_prefix_matches(uuid, uuid_prefix, uuid_prefix_len))
690a5da2
AK
846 continue;
847
76d1aec8
ZK
848 /* Ignore if parent node wants to presuspend this node */
849 if (dlink->node->presuspend_node == node)
850 continue;
851
b4f1578f
AK
852 if (!(dinfo = dm_tree_node_get_info(dlink->node))) {
853 stack; /* FIXME Is this normal? */
690a5da2
AK
854 return 0;
855 }
856
857 if (!dinfo->suspended)
858 return 0;
859 }
860
861 return 1;
862}
863
3d0480ed
AK
864/*
865 * Set major and minor to zero for root of tree.
866 */
b4f1578f 867struct dm_tree_node *dm_tree_find_node(struct dm_tree *dtree,
3d0480ed
AK
868 uint32_t major,
869 uint32_t minor)
870{
871 if (!major && !minor)
b4f1578f 872 return &dtree->root;
3d0480ed 873
b4f1578f 874 return _find_dm_tree_node(dtree, major, minor);
3d0480ed
AK
875}
876
165e4a11
AK
877/*
878 * Set uuid to NULL for root of tree.
879 */
b4f1578f 880struct dm_tree_node *dm_tree_find_node_by_uuid(struct dm_tree *dtree,
165e4a11
AK
881 const char *uuid)
882{
883 if (!uuid || !*uuid)
b4f1578f 884 return &dtree->root;
165e4a11 885
b4f1578f 886 return _find_dm_tree_node_by_uuid(dtree, uuid);
165e4a11
AK
887}
888
3d0480ed
AK
889/*
890 * First time set *handle to NULL.
891 * Set inverted to invert the tree.
892 */
b4f1578f 893struct dm_tree_node *dm_tree_next_child(void **handle,
04bde319
ZK
894 const struct dm_tree_node *parent,
895 uint32_t inverted)
3d0480ed 896{
2c44337b 897 struct dm_list **dlink = (struct dm_list **) handle;
04bde319 898 const struct dm_list *use_list;
3d0480ed
AK
899
900 if (inverted)
901 use_list = &parent->used_by;
902 else
903 use_list = &parent->uses;
904
905 if (!*dlink)
2c44337b 906 *dlink = dm_list_first(use_list);
3d0480ed 907 else
2c44337b 908 *dlink = dm_list_next(use_list, *dlink);
3d0480ed 909
2c44337b 910 return (*dlink) ? dm_list_item(*dlink, struct dm_tree_link)->node : NULL;
3d0480ed
AK
911}
912
3e8c6b73 913/*
a6d97ede 914 * Deactivate a device with its dependencies if the uuid prefix matches.
3e8c6b73 915 */
db208f51
AK
916static int _info_by_dev(uint32_t major, uint32_t minor, int with_open_count,
917 struct dm_info *info)
3e8c6b73
AK
918{
919 struct dm_task *dmt;
920 int r;
921
922 if (!(dmt = dm_task_create(DM_DEVICE_INFO))) {
923 log_error("_info_by_dev: dm_task creation failed");
924 return 0;
925 }
926
927 if (!dm_task_set_major(dmt, major) || !dm_task_set_minor(dmt, minor)) {
928 log_error("_info_by_dev: Failed to set device number");
929 dm_task_destroy(dmt);
930 return 0;
931 }
932
db208f51
AK
933 if (!with_open_count && !dm_task_no_open_count(dmt))
934 log_error("Failed to disable open_count");
935
3e8c6b73
AK
936 if ((r = dm_task_run(dmt)))
937 r = dm_task_get_info(dmt, info);
938
939 dm_task_destroy(dmt);
940
941 return r;
942}
943
125712be
PR
944static int _check_device_not_in_use(struct dm_info *info)
945{
946 if (!info->exists)
947 return 1;
948
949 /* If sysfs is not used, use open_count information only. */
c3e5b497
PR
950 if (!*dm_sysfs_dir()) {
951 if (info->open_count) {
952 log_error("Device %" PRIu32 ":%" PRIu32 " in use",
953 info->major, info->minor);
954 return 0;
955 }
956
957 return 1;
958 }
125712be
PR
959
960 if (dm_device_has_holders(info->major, info->minor)) {
961 log_error("Device %" PRIu32 ":%" PRIu32 " is used "
962 "by another device.", info->major, info->minor);
963 return 0;
964 }
965
966 if (dm_device_has_mounted_fs(info->major, info->minor)) {
967 log_error("Device %" PRIu32 ":%" PRIu32 " contains "
968 "a filesystem in use.", info->major, info->minor);
969 return 0;
970 }
971
972 return 1;
973}
974
f3ef15ef
ZK
975/* Check if all parent nodes of given node have open_count == 0 */
976static int _node_has_closed_parents(struct dm_tree_node *node,
977 const char *uuid_prefix,
978 size_t uuid_prefix_len)
979{
980 struct dm_tree_link *dlink;
981 const struct dm_info *dinfo;
982 struct dm_info info;
983 const char *uuid;
984
985 /* Iterate through parents of this node */
986 dm_list_iterate_items(dlink, &node->used_by) {
987 if (!(uuid = dm_tree_node_get_uuid(dlink->node))) {
988 stack;
989 continue;
990 }
991
992 /* Ignore if it doesn't belong to this VG */
993 if (!_uuid_prefix_matches(uuid, uuid_prefix, uuid_prefix_len))
994 continue;
995
996 if (!(dinfo = dm_tree_node_get_info(dlink->node))) {
997 stack; /* FIXME Is this normal? */
998 return 0;
999 }
1000
1001 /* Refresh open_count */
1002 if (!_info_by_dev(dinfo->major, dinfo->minor, 1, &info) ||
1003 !info.exists)
1004 continue;
1005
eb418883
ZK
1006 if (info.open_count) {
1007 log_debug("Node %s %d:%d has open_count %d", uuid_prefix,
1008 dinfo->major, dinfo->minor, info.open_count);
f3ef15ef 1009 return 0;
eb418883 1010 }
f3ef15ef
ZK
1011 }
1012
1013 return 1;
1014}
1015
f16aea9e 1016static int _deactivate_node(const char *name, uint32_t major, uint32_t minor,
787200ef 1017 uint32_t *cookie, uint16_t udev_flags, int retry)
3e8c6b73
AK
1018{
1019 struct dm_task *dmt;
bd90c6b2 1020 int r = 0;
3e8c6b73
AK
1021
1022 log_verbose("Removing %s (%" PRIu32 ":%" PRIu32 ")", name, major, minor);
1023
1024 if (!(dmt = dm_task_create(DM_DEVICE_REMOVE))) {
1025 log_error("Deactivation dm_task creation failed for %s", name);
1026 return 0;
1027 }
1028
1029 if (!dm_task_set_major(dmt, major) || !dm_task_set_minor(dmt, minor)) {
1030 log_error("Failed to set device number for %s deactivation", name);
bd90c6b2 1031 goto out;
3e8c6b73
AK
1032 }
1033
1034 if (!dm_task_no_open_count(dmt))
1035 log_error("Failed to disable open_count");
1036
f16aea9e 1037 if (!dm_task_set_cookie(dmt, cookie, udev_flags))
bd90c6b2
AK
1038 goto out;
1039
787200ef
PR
1040
1041 if (retry)
1042 dm_task_retry_remove(dmt);
1043
3e8c6b73
AK
1044 r = dm_task_run(dmt);
1045
0437bccc
AK
1046 /* FIXME Until kernel returns actual name so dm-iface.c can handle it */
1047 rm_dev_node(name, dmt->cookie_set && !(udev_flags & DM_UDEV_DISABLE_DM_RULES_FLAG),
9032898e 1048 dmt->cookie_set && (udev_flags & DM_UDEV_DISABLE_LIBRARY_FALLBACK));
165e4a11 1049
db208f51
AK
1050 /* FIXME Remove node from tree or mark invalid? */
1051
bd90c6b2 1052out:
db208f51
AK
1053 dm_task_destroy(dmt);
1054
1055 return r;
1056}
1057
bd90c6b2 1058static int _rename_node(const char *old_name, const char *new_name, uint32_t major,
f16aea9e 1059 uint32_t minor, uint32_t *cookie, uint16_t udev_flags)
165e4a11
AK
1060{
1061 struct dm_task *dmt;
1062 int r = 0;
1063
1064 log_verbose("Renaming %s (%" PRIu32 ":%" PRIu32 ") to %s", old_name, major, minor, new_name);
1065
1066 if (!(dmt = dm_task_create(DM_DEVICE_RENAME))) {
1067 log_error("Rename dm_task creation failed for %s", old_name);
1068 return 0;
1069 }
1070
1071 if (!dm_task_set_name(dmt, old_name)) {
1072 log_error("Failed to set name for %s rename.", old_name);
1073 goto out;
1074 }
1075
b4f1578f 1076 if (!dm_task_set_newname(dmt, new_name))
40e5fd8b 1077 goto_out;
165e4a11
AK
1078
1079 if (!dm_task_no_open_count(dmt))
1080 log_error("Failed to disable open_count");
1081
f16aea9e 1082 if (!dm_task_set_cookie(dmt, cookie, udev_flags))
bd90c6b2
AK
1083 goto out;
1084
165e4a11
AK
1085 r = dm_task_run(dmt);
1086
1087out:
1088 dm_task_destroy(dmt);
1089
1090 return r;
1091}
1092
165e4a11
AK
1093/* FIXME Merge with _suspend_node? */
1094static int _resume_node(const char *name, uint32_t major, uint32_t minor,
52b84409 1095 uint32_t read_ahead, uint32_t read_ahead_flags,
f16aea9e 1096 struct dm_info *newinfo, uint32_t *cookie,
1840aa09 1097 uint16_t udev_flags, int already_suspended)
165e4a11
AK
1098{
1099 struct dm_task *dmt;
bd90c6b2 1100 int r = 0;
165e4a11
AK
1101
1102 log_verbose("Resuming %s (%" PRIu32 ":%" PRIu32 ")", name, major, minor);
1103
1104 if (!(dmt = dm_task_create(DM_DEVICE_RESUME))) {
1105 log_error("Suspend dm_task creation failed for %s", name);
1106 return 0;
1107 }
1108
0b7d16bc
AK
1109 /* FIXME Kernel should fill in name on return instead */
1110 if (!dm_task_set_name(dmt, name)) {
1111 log_error("Failed to set readahead device name for %s", name);
bd90c6b2 1112 goto out;
0b7d16bc
AK
1113 }
1114
165e4a11
AK
1115 if (!dm_task_set_major(dmt, major) || !dm_task_set_minor(dmt, minor)) {
1116 log_error("Failed to set device number for %s resumption.", name);
bd90c6b2 1117 goto out;
165e4a11
AK
1118 }
1119
1120 if (!dm_task_no_open_count(dmt))
1121 log_error("Failed to disable open_count");
1122
52b84409
AK
1123 if (!dm_task_set_read_ahead(dmt, read_ahead, read_ahead_flags))
1124 log_error("Failed to set read ahead");
1125
f16aea9e 1126 if (!dm_task_set_cookie(dmt, cookie, udev_flags))
bd90c6b2
AK
1127 goto out;
1128
1840aa09
AK
1129 if ((r = dm_task_run(dmt))) {
1130 if (already_suspended)
1131 dec_suspended();
165e4a11 1132 r = dm_task_get_info(dmt, newinfo);
1840aa09 1133 }
165e4a11 1134
bd90c6b2 1135out:
165e4a11
AK
1136 dm_task_destroy(dmt);
1137
1138 return r;
1139}
1140
db208f51 1141static int _suspend_node(const char *name, uint32_t major, uint32_t minor,
b9ffd32c 1142 int skip_lockfs, int no_flush, struct dm_info *newinfo)
db208f51
AK
1143{
1144 struct dm_task *dmt;
1145 int r;
1146
b9ffd32c
AK
1147 log_verbose("Suspending %s (%" PRIu32 ":%" PRIu32 ")%s%s",
1148 name, major, minor,
1149 skip_lockfs ? "" : " with filesystem sync",
6e1898a5 1150 no_flush ? "" : " with device flush");
db208f51
AK
1151
1152 if (!(dmt = dm_task_create(DM_DEVICE_SUSPEND))) {
1153 log_error("Suspend dm_task creation failed for %s", name);
1154 return 0;
1155 }
1156
1157 if (!dm_task_set_major(dmt, major) || !dm_task_set_minor(dmt, minor)) {
1158 log_error("Failed to set device number for %s suspension.", name);
1159 dm_task_destroy(dmt);
1160 return 0;
1161 }
1162
1163 if (!dm_task_no_open_count(dmt))
1164 log_error("Failed to disable open_count");
1165
c55b1410
AK
1166 if (skip_lockfs && !dm_task_skip_lockfs(dmt))
1167 log_error("Failed to set skip_lockfs flag.");
1168
b9ffd32c
AK
1169 if (no_flush && !dm_task_no_flush(dmt))
1170 log_error("Failed to set no_flush flag.");
1171
1840aa09
AK
1172 if ((r = dm_task_run(dmt))) {
1173 inc_suspended();
db208f51 1174 r = dm_task_get_info(dmt, newinfo);
1840aa09 1175 }
db208f51 1176
3e8c6b73
AK
1177 dm_task_destroy(dmt);
1178
1179 return r;
1180}
1181
18e0f934
AK
1182/*
1183 * FIXME Don't attempt to deactivate known internal dependencies.
1184 */
1185static int _dm_tree_deactivate_children(struct dm_tree_node *dnode,
1186 const char *uuid_prefix,
1187 size_t uuid_prefix_len,
1188 unsigned level)
3e8c6b73 1189{
b7eb2ad0 1190 int r = 1;
3e8c6b73 1191 void *handle = NULL;
b4f1578f 1192 struct dm_tree_node *child = dnode;
3e8c6b73
AK
1193 struct dm_info info;
1194 const struct dm_info *dinfo;
1195 const char *name;
1196 const char *uuid;
1197
b4f1578f
AK
1198 while ((child = dm_tree_next_child(&handle, dnode, 0))) {
1199 if (!(dinfo = dm_tree_node_get_info(child))) {
3e8c6b73
AK
1200 stack;
1201 continue;
1202 }
1203
b4f1578f 1204 if (!(name = dm_tree_node_get_name(child))) {
3e8c6b73
AK
1205 stack;
1206 continue;
1207 }
1208
b4f1578f 1209 if (!(uuid = dm_tree_node_get_uuid(child))) {
3e8c6b73
AK
1210 stack;
1211 continue;
1212 }
1213
1214 /* Ignore if it doesn't belong to this VG */
2b69db1f 1215 if (!_uuid_prefix_matches(uuid, uuid_prefix, uuid_prefix_len))
3e8c6b73 1216 continue;
3e8c6b73
AK
1217
1218 /* Refresh open_count */
db208f51 1219 if (!_info_by_dev(dinfo->major, dinfo->minor, 1, &info) ||
f55021f4 1220 !info.exists)
3e8c6b73
AK
1221 continue;
1222
125712be
PR
1223 if (!_check_device_not_in_use(&info))
1224 continue;
1225
f3ef15ef 1226 /* Also checking open_count in parent nodes of presuspend_node */
125712be 1227 if ((child->presuspend_node &&
f3ef15ef
ZK
1228 !_node_has_closed_parents(child->presuspend_node,
1229 uuid_prefix, uuid_prefix_len))) {
18e0f934
AK
1230 /* Only report error from (likely non-internal) dependency at top level */
1231 if (!level) {
1232 log_error("Unable to deactivate open %s (%" PRIu32
1233 ":%" PRIu32 ")", name, info.major,
1234 info.minor);
1235 r = 0;
1236 }
f55021f4
AK
1237 continue;
1238 }
1239
76d1aec8
ZK
1240 /* Suspend child node first if requested */
1241 if (child->presuspend_node &&
1242 !dm_tree_suspend_children(child, uuid_prefix, uuid_prefix_len))
1243 continue;
1244
f16aea9e 1245 if (!_deactivate_node(name, info.major, info.minor,
787200ef
PR
1246 &child->dtree->cookie, child->udev_flags,
1247 child->dtree->retry_remove)) {
3e8c6b73
AK
1248 log_error("Unable to deactivate %s (%" PRIu32
1249 ":%" PRIu32 ")", name, info.major,
1250 info.minor);
b7eb2ad0 1251 r = 0;
3e8c6b73 1252 continue;
f4249251
AK
1253 } else if (info.suspended)
1254 dec_suspended();
3e8c6b73 1255
18e0f934
AK
1256 if (dm_tree_node_num_children(child, 0)) {
1257 if (!_dm_tree_deactivate_children(child, uuid_prefix, uuid_prefix_len, level + 1))
b7eb2ad0 1258 return_0;
18e0f934 1259 }
3e8c6b73
AK
1260 }
1261
b7eb2ad0 1262 return r;
3e8c6b73 1263}
db208f51 1264
18e0f934
AK
1265int dm_tree_deactivate_children(struct dm_tree_node *dnode,
1266 const char *uuid_prefix,
1267 size_t uuid_prefix_len)
1268{
1269 return _dm_tree_deactivate_children(dnode, uuid_prefix, uuid_prefix_len, 0);
1270}
1271
c55b1410
AK
1272void dm_tree_skip_lockfs(struct dm_tree_node *dnode)
1273{
1274 dnode->dtree->skip_lockfs = 1;
1275}
1276
b9ffd32c
AK
1277void dm_tree_use_no_flush_suspend(struct dm_tree_node *dnode)
1278{
1279 dnode->dtree->no_flush = 1;
1280}
1281
787200ef
PR
1282void dm_tree_retry_remove(struct dm_tree_node *dnode)
1283{
1284 dnode->dtree->retry_remove = 1;
1285}
1286
b4f1578f 1287int dm_tree_suspend_children(struct dm_tree_node *dnode,
08e64ce5
ZK
1288 const char *uuid_prefix,
1289 size_t uuid_prefix_len)
db208f51 1290{
68085c93 1291 int r = 1;
db208f51 1292 void *handle = NULL;
b4f1578f 1293 struct dm_tree_node *child = dnode;
db208f51
AK
1294 struct dm_info info, newinfo;
1295 const struct dm_info *dinfo;
1296 const char *name;
1297 const char *uuid;
1298
690a5da2 1299 /* Suspend nodes at this level of the tree */
b4f1578f
AK
1300 while ((child = dm_tree_next_child(&handle, dnode, 0))) {
1301 if (!(dinfo = dm_tree_node_get_info(child))) {
db208f51
AK
1302 stack;
1303 continue;
1304 }
1305
b4f1578f 1306 if (!(name = dm_tree_node_get_name(child))) {
db208f51
AK
1307 stack;
1308 continue;
1309 }
1310
b4f1578f 1311 if (!(uuid = dm_tree_node_get_uuid(child))) {
db208f51
AK
1312 stack;
1313 continue;
1314 }
1315
1316 /* Ignore if it doesn't belong to this VG */
2b69db1f 1317 if (!_uuid_prefix_matches(uuid, uuid_prefix, uuid_prefix_len))
db208f51
AK
1318 continue;
1319
690a5da2
AK
1320 /* Ensure immediate parents are already suspended */
1321 if (!_children_suspended(child, 1, uuid_prefix, uuid_prefix_len))
1322 continue;
1323
db208f51 1324 if (!_info_by_dev(dinfo->major, dinfo->minor, 0, &info) ||
b700541f 1325 !info.exists || info.suspended)
db208f51
AK
1326 continue;
1327
c55b1410 1328 if (!_suspend_node(name, info.major, info.minor,
b9ffd32c
AK
1329 child->dtree->skip_lockfs,
1330 child->dtree->no_flush, &newinfo)) {
db208f51
AK
1331 log_error("Unable to suspend %s (%" PRIu32
1332 ":%" PRIu32 ")", name, info.major,
1333 info.minor);
68085c93 1334 r = 0;
db208f51
AK
1335 continue;
1336 }
1337
1338 /* Update cached info */
1339 child->info = newinfo;
690a5da2
AK
1340 }
1341
1342 /* Then suspend any child nodes */
1343 handle = NULL;
1344
b4f1578f
AK
1345 while ((child = dm_tree_next_child(&handle, dnode, 0))) {
1346 if (!(uuid = dm_tree_node_get_uuid(child))) {
690a5da2
AK
1347 stack;
1348 continue;
1349 }
1350
1351 /* Ignore if it doesn't belong to this VG */
87f98002 1352 if (!_uuid_prefix_matches(uuid, uuid_prefix, uuid_prefix_len))
690a5da2 1353 continue;
db208f51 1354
b4f1578f 1355 if (dm_tree_node_num_children(child, 0))
68085c93
MS
1356 if (!dm_tree_suspend_children(child, uuid_prefix, uuid_prefix_len))
1357 return_0;
db208f51
AK
1358 }
1359
68085c93 1360 return r;
db208f51
AK
1361}
1362
b4f1578f 1363int dm_tree_activate_children(struct dm_tree_node *dnode,
db208f51
AK
1364 const char *uuid_prefix,
1365 size_t uuid_prefix_len)
1366{
2ca6b865 1367 int r = 1;
db208f51 1368 void *handle = NULL;
b4f1578f 1369 struct dm_tree_node *child = dnode;
165e4a11
AK
1370 struct dm_info newinfo;
1371 const char *name;
db208f51 1372 const char *uuid;
56c28292 1373 int priority;
db208f51 1374
165e4a11 1375 /* Activate children first */
b4f1578f
AK
1376 while ((child = dm_tree_next_child(&handle, dnode, 0))) {
1377 if (!(uuid = dm_tree_node_get_uuid(child))) {
165e4a11
AK
1378 stack;
1379 continue;
db208f51
AK
1380 }
1381
908db078
AK
1382 if (!_uuid_prefix_matches(uuid, uuid_prefix, uuid_prefix_len))
1383 continue;
db208f51 1384
b4f1578f 1385 if (dm_tree_node_num_children(child, 0))
2ca6b865
MS
1386 if (!dm_tree_activate_children(child, uuid_prefix, uuid_prefix_len))
1387 return_0;
56c28292 1388 }
165e4a11 1389
56c28292 1390 handle = NULL;
165e4a11 1391
aa6f4e51 1392 for (priority = 0; priority < 3; priority++) {
56c28292
AK
1393 while ((child = dm_tree_next_child(&handle, dnode, 0))) {
1394 if (!(uuid = dm_tree_node_get_uuid(child))) {
1395 stack;
1396 continue;
165e4a11 1397 }
165e4a11 1398
56c28292
AK
1399 if (!_uuid_prefix_matches(uuid, uuid_prefix, uuid_prefix_len))
1400 continue;
165e4a11 1401
56c28292
AK
1402 if (priority != child->activation_priority)
1403 continue;
165e4a11 1404
56c28292
AK
1405 if (!(name = dm_tree_node_get_name(child))) {
1406 stack;
1407 continue;
1408 }
1409
1410 /* Rename? */
1411 if (child->props.new_name) {
bd90c6b2 1412 if (!_rename_node(name, child->props.new_name, child->info.major,
f16aea9e
PR
1413 child->info.minor, &child->dtree->cookie,
1414 child->udev_flags)) {
56c28292
AK
1415 log_error("Failed to rename %s (%" PRIu32
1416 ":%" PRIu32 ") to %s", name, child->info.major,
1417 child->info.minor, child->props.new_name);
1418 return 0;
1419 }
1420 child->name = child->props.new_name;
1421 child->props.new_name = NULL;
1422 }
1423
1424 if (!child->info.inactive_table && !child->info.suspended)
1425 continue;
1426
bafa2f39 1427 if (!_resume_node(child->name, child->info.major, child->info.minor,
bd90c6b2 1428 child->props.read_ahead, child->props.read_ahead_flags,
1840aa09 1429 &newinfo, &child->dtree->cookie, child->udev_flags, child->info.suspended)) {
56c28292 1430 log_error("Unable to resume %s (%" PRIu32
bafa2f39 1431 ":%" PRIu32 ")", child->name, child->info.major,
56c28292 1432 child->info.minor);
2ca6b865 1433 r = 0;
56c28292
AK
1434 continue;
1435 }
1436
1437 /* Update cached info */
1438 child->info = newinfo;
1439 }
db208f51
AK
1440 }
1441
165e4a11
AK
1442 handle = NULL;
1443
2ca6b865 1444 return r;
165e4a11
AK
1445}
1446
b4f1578f 1447static int _create_node(struct dm_tree_node *dnode)
165e4a11
AK
1448{
1449 int r = 0;
1450 struct dm_task *dmt;
1451
1452 log_verbose("Creating %s", dnode->name);
1453
1454 if (!(dmt = dm_task_create(DM_DEVICE_CREATE))) {
1455 log_error("Create dm_task creation failed for %s", dnode->name);
1456 return 0;
1457 }
1458
1459 if (!dm_task_set_name(dmt, dnode->name)) {
1460 log_error("Failed to set device name for %s", dnode->name);
1461 goto out;
1462 }
1463
1464 if (!dm_task_set_uuid(dmt, dnode->uuid)) {
1465 log_error("Failed to set uuid for %s", dnode->name);
1466 goto out;
1467 }
1468
1469 if (dnode->props.major &&
1470 (!dm_task_set_major(dmt, dnode->props.major) ||
1471 !dm_task_set_minor(dmt, dnode->props.minor))) {
1472 log_error("Failed to set device number for %s creation.", dnode->name);
1473 goto out;
1474 }
1475
1476 if (dnode->props.read_only && !dm_task_set_ro(dmt)) {
1477 log_error("Failed to set read only flag for %s", dnode->name);
1478 goto out;
1479 }
1480
1481 if (!dm_task_no_open_count(dmt))
1482 log_error("Failed to disable open_count");
1483
1484 if ((r = dm_task_run(dmt)))
1485 r = dm_task_get_info(dmt, &dnode->info);
1486
1487out:
1488 dm_task_destroy(dmt);
1489
1490 return r;
1491}
1492
1493
b4f1578f 1494static int _build_dev_string(char *devbuf, size_t bufsize, struct dm_tree_node *node)
165e4a11
AK
1495{
1496 if (!dm_format_dev(devbuf, bufsize, node->info.major, node->info.minor)) {
40e5fd8b
AK
1497 log_error("Failed to format %s device number for %s as dm "
1498 "target (%u,%u)",
1499 node->name, node->uuid, node->info.major, node->info.minor);
1500 return 0;
165e4a11
AK
1501 }
1502
1503 return 1;
1504}
1505
ffa9b6a5
ZK
1506/* simplify string emiting code */
1507#define EMIT_PARAMS(p, str...)\
7b6c011c
AK
1508do {\
1509 int w;\
1510 if ((w = dm_snprintf(params + p, paramsize - (size_t) p, str)) < 0) {\
1511 stack; /* Out of space */\
1512 return -1;\
1513 }\
1514 p += w;\
1515} while (0)
ffa9b6a5 1516
3c74075f
JEB
1517/*
1518 * _emit_areas_line
1519 *
1520 * Returns: 1 on success, 0 on failure
1521 */
08f1ddea 1522static int _emit_areas_line(struct dm_task *dmt __attribute__((unused)),
4dcaa230
AK
1523 struct load_segment *seg, char *params,
1524 size_t paramsize, int *pos)
165e4a11
AK
1525{
1526 struct seg_area *area;
7d7d93ac 1527 char devbuf[DM_FORMAT_DEV_BUFSIZE];
609faae9 1528 unsigned first_time = 1;
db3c1ac1 1529 const char *logtype, *synctype;
b262f3e1 1530 unsigned log_parm_count;
165e4a11 1531
2c44337b 1532 dm_list_iterate_items(area, &seg->areas) {
b262f3e1
ZK
1533 switch (seg->type) {
1534 case SEG_REPLICATOR_DEV:
6d04311e
JEB
1535 if (!_build_dev_string(devbuf, sizeof(devbuf), area->dev_node))
1536 return_0;
1537
b262f3e1
ZK
1538 EMIT_PARAMS(*pos, " %d 1 %s", area->rsite_index, devbuf);
1539 if (first_time)
1540 EMIT_PARAMS(*pos, " nolog 0");
1541 else {
1542 /* Remote devices */
1543 log_parm_count = (area->flags &
1544 (DM_NOSYNC | DM_FORCESYNC)) ? 2 : 1;
1545
1546 if (!area->slog) {
1547 devbuf[0] = 0; /* Only core log parameters */
1548 logtype = "core";
1549 } else {
1550 devbuf[0] = ' '; /* Extra space before device name */
1551 if (!_build_dev_string(devbuf + 1,
1552 sizeof(devbuf) - 1,
1553 area->slog))
1554 return_0;
1555 logtype = "disk";
1556 log_parm_count++; /* Extra sync log device name parameter */
1557 }
1558
1559 EMIT_PARAMS(*pos, " %s %u%s %" PRIu64, logtype,
1560 log_parm_count, devbuf, area->region_size);
1561
db3c1ac1
AK
1562 synctype = (area->flags & DM_NOSYNC) ?
1563 " nosync" : (area->flags & DM_FORCESYNC) ?
1564 " sync" : NULL;
b262f3e1 1565
db3c1ac1
AK
1566 if (synctype)
1567 EMIT_PARAMS(*pos, "%s", synctype);
b262f3e1
ZK
1568 }
1569 break;
cac52ca4
JEB
1570 case SEG_RAID1:
1571 case SEG_RAID4:
1572 case SEG_RAID5_LA:
1573 case SEG_RAID5_RA:
1574 case SEG_RAID5_LS:
1575 case SEG_RAID5_RS:
1576 case SEG_RAID6_ZR:
1577 case SEG_RAID6_NR:
1578 case SEG_RAID6_NC:
6d04311e
JEB
1579 if (!area->dev_node) {
1580 EMIT_PARAMS(*pos, " -");
1581 break;
1582 }
1583 if (!_build_dev_string(devbuf, sizeof(devbuf), area->dev_node))
1584 return_0;
1585
cac52ca4
JEB
1586 EMIT_PARAMS(*pos, " %s", devbuf);
1587 break;
b262f3e1 1588 default:
6d04311e
JEB
1589 if (!_build_dev_string(devbuf, sizeof(devbuf), area->dev_node))
1590 return_0;
1591
b262f3e1
ZK
1592 EMIT_PARAMS(*pos, "%s%s %" PRIu64, first_time ? "" : " ",
1593 devbuf, area->offset);
1594 }
609faae9
AK
1595
1596 first_time = 0;
165e4a11
AK
1597 }
1598
1599 return 1;
1600}
1601
b262f3e1
ZK
1602static int _replicator_emit_segment_line(const struct load_segment *seg, char *params,
1603 size_t paramsize, int *pos)
1604{
1605 const struct load_segment *rlog_seg;
1606 struct replicator_site *rsite;
1607 char rlogbuf[DM_FORMAT_DEV_BUFSIZE];
1608 unsigned parm_count;
1609
1610 if (!seg->log || !_build_dev_string(rlogbuf, sizeof(rlogbuf), seg->log))
1611 return_0;
1612
1613 rlog_seg = dm_list_item(dm_list_last(&seg->log->props.segs),
1614 struct load_segment);
1615
1616 EMIT_PARAMS(*pos, "%s 4 %s 0 auto %" PRIu64,
1617 seg->rlog_type, rlogbuf, rlog_seg->size);
1618
1619 dm_list_iterate_items(rsite, &seg->rsites) {
1620 parm_count = (rsite->fall_behind_data
1621 || rsite->fall_behind_ios
1622 || rsite->async_timeout) ? 4 : 2;
1623
1624 EMIT_PARAMS(*pos, " blockdev %u %u %s", parm_count, rsite->rsite_index,
1625 (rsite->mode == DM_REPLICATOR_SYNC) ? "synchronous" : "asynchronous");
1626
1627 if (rsite->fall_behind_data)
1628 EMIT_PARAMS(*pos, " data %" PRIu64, rsite->fall_behind_data);
1629 else if (rsite->fall_behind_ios)
1630 EMIT_PARAMS(*pos, " ios %" PRIu32, rsite->fall_behind_ios);
1631 else if (rsite->async_timeout)
1632 EMIT_PARAMS(*pos, " timeout %" PRIu32, rsite->async_timeout);
1633 }
1634
1635 return 1;
1636}
1637
3c74075f 1638/*
3c74075f
JEB
1639 * Returns: 1 on success, 0 on failure
1640 */
beecb1e1
ZK
1641static int _mirror_emit_segment_line(struct dm_task *dmt, struct load_segment *seg,
1642 char *params, size_t paramsize)
165e4a11 1643{
8f26e18c
JEB
1644 int block_on_error = 0;
1645 int handle_errors = 0;
1646 int dm_log_userspace = 0;
1647 struct utsname uts;
dbcb64b8 1648 unsigned log_parm_count;
b39fdcf4 1649 int pos = 0, parts;
7d7d93ac 1650 char logbuf[DM_FORMAT_DEV_BUFSIZE];
dbcb64b8 1651 const char *logtype;
b39fdcf4 1652 unsigned kmaj = 0, kmin = 0, krel = 0;
165e4a11 1653
b39fdcf4
MB
1654 if (uname(&uts) == -1) {
1655 log_error("Cannot read kernel release version.");
1656 return 0;
1657 }
1658
1659 /* Kernels with a major number of 2 always had 3 parts. */
1660 parts = sscanf(uts.release, "%u.%u.%u", &kmaj, &kmin, &krel);
1661 if (parts < 1 || (kmaj < 3 && parts < 3)) {
1662 log_error("Wrong kernel release version %s.", uts.release);
30a65310
ZK
1663 return 0;
1664 }
67b25ed4 1665
8f26e18c
JEB
1666 if ((seg->flags & DM_BLOCK_ON_ERROR)) {
1667 /*
1668 * Originally, block_on_error was an argument to the log
1669 * portion of the mirror CTR table. It was renamed to
1670 * "handle_errors" and now resides in the 'features'
1671 * section of the mirror CTR table (i.e. at the end).
1672 *
1673 * We can identify whether to use "block_on_error" or
1674 * "handle_errors" by the dm-mirror module's version
1675 * number (>= 1.12) or by the kernel version (>= 2.6.22).
1676 */
ba61f848 1677 if (KERNEL_VERSION(kmaj, kmin, krel) >= KERNEL_VERSION(2, 6, 22))
8f26e18c
JEB
1678 handle_errors = 1;
1679 else
1680 block_on_error = 1;
1681 }
1682
1683 if (seg->clustered) {
1684 /* Cluster mirrors require a UUID */
1685 if (!seg->uuid)
1686 return_0;
1687
1688 /*
1689 * Cluster mirrors used to have their own log
1690 * types. Now they are accessed through the
1691 * userspace log type.
1692 *
1693 * The dm-log-userspace module was added to the
1694 * 2.6.31 kernel.
1695 */
ba61f848 1696 if (KERNEL_VERSION(kmaj, kmin, krel) >= KERNEL_VERSION(2, 6, 31))
8f26e18c
JEB
1697 dm_log_userspace = 1;
1698 }
1699
1700 /* Region size */
1701 log_parm_count = 1;
1702
1703 /* [no]sync, block_on_error etc. */
1704 log_parm_count += hweight32(seg->flags);
311d6d81 1705
8f26e18c
JEB
1706 /* "handle_errors" is a feature arg now */
1707 if (handle_errors)
1708 log_parm_count--;
1709
1710 /* DM_CORELOG does not count in the param list */
1711 if (seg->flags & DM_CORELOG)
1712 log_parm_count--;
1713
1714 if (seg->clustered) {
1715 log_parm_count++; /* For UUID */
1716
1717 if (!dm_log_userspace)
ffa9b6a5 1718 EMIT_PARAMS(pos, "clustered-");
49b95a5e
JEB
1719 else
1720 /* For clustered-* type field inserted later */
1721 log_parm_count++;
8f26e18c 1722 }
dbcb64b8 1723
8f26e18c
JEB
1724 if (!seg->log)
1725 logtype = "core";
1726 else {
1727 logtype = "disk";
1728 log_parm_count++;
1729 if (!_build_dev_string(logbuf, sizeof(logbuf), seg->log))
1730 return_0;
1731 }
dbcb64b8 1732
8f26e18c
JEB
1733 if (dm_log_userspace)
1734 EMIT_PARAMS(pos, "userspace %u %s clustered-%s",
1735 log_parm_count, seg->uuid, logtype);
1736 else
ffa9b6a5 1737 EMIT_PARAMS(pos, "%s %u", logtype, log_parm_count);
dbcb64b8 1738
8f26e18c
JEB
1739 if (seg->log)
1740 EMIT_PARAMS(pos, " %s", logbuf);
1741
1742 EMIT_PARAMS(pos, " %u", seg->region_size);
dbcb64b8 1743
8f26e18c
JEB
1744 if (seg->clustered && !dm_log_userspace)
1745 EMIT_PARAMS(pos, " %s", seg->uuid);
67b25ed4 1746
8f26e18c
JEB
1747 if ((seg->flags & DM_NOSYNC))
1748 EMIT_PARAMS(pos, " nosync");
1749 else if ((seg->flags & DM_FORCESYNC))
1750 EMIT_PARAMS(pos, " sync");
dbcb64b8 1751
8f26e18c
JEB
1752 if (block_on_error)
1753 EMIT_PARAMS(pos, " block_on_error");
1754
1755 EMIT_PARAMS(pos, " %u ", seg->mirror_area_count);
1756
5f3325fc 1757 if (_emit_areas_line(dmt, seg, params, paramsize, &pos) <= 0)
3c74075f 1758 return_0;
dbcb64b8 1759
8f26e18c
JEB
1760 if (handle_errors)
1761 EMIT_PARAMS(pos, " 1 handle_errors");
ffa9b6a5 1762
3c74075f 1763 return 1;
8f26e18c
JEB
1764}
1765
cac52ca4
JEB
1766static int _raid_emit_segment_line(struct dm_task *dmt, uint32_t major,
1767 uint32_t minor, struct load_segment *seg,
1768 uint64_t *seg_start, char *params,
1769 size_t paramsize)
1770{
f439e65b 1771 uint32_t i, *tmp;
cac52ca4
JEB
1772 int param_count = 1; /* mandatory 'chunk size'/'stripe size' arg */
1773 int pos = 0;
1774
1775 if ((seg->flags & DM_NOSYNC) || (seg->flags & DM_FORCESYNC))
1776 param_count++;
1777
1778 if (seg->region_size)
1779 param_count += 2;
1780
f439e65b
JEB
1781 tmp = (uint32_t *)(&seg->rebuilds); /* rebuilds is 64-bit */
1782 param_count += 2 * hweight32(tmp[0]);
1783 param_count += 2 * hweight32(tmp[1]);
1784
cac52ca4
JEB
1785 if ((seg->type == SEG_RAID1) && seg->stripe_size)
1786 log_error("WARNING: Ignoring RAID1 stripe size");
1787
1788 EMIT_PARAMS(pos, "%s %d %u", dm_segtypes[seg->type].target,
1789 param_count, seg->stripe_size);
1790
1791 if (seg->flags & DM_NOSYNC)
1792 EMIT_PARAMS(pos, " nosync");
1793 else if (seg->flags & DM_FORCESYNC)
1794 EMIT_PARAMS(pos, " sync");
1795
1796 if (seg->region_size)
1797 EMIT_PARAMS(pos, " region_size %u", seg->region_size);
1798
f439e65b
JEB
1799 for (i = 0; i < (seg->area_count / 2); i++)
1800 if (seg->rebuilds & (1 << i))
1801 EMIT_PARAMS(pos, " rebuild %u", i);
1802
cac52ca4
JEB
1803 /* Print number of metadata/data device pairs */
1804 EMIT_PARAMS(pos, " %u", seg->area_count/2);
1805
1806 if (_emit_areas_line(dmt, seg, params, paramsize, &pos) <= 0)
1807 return_0;
1808
1809 return 1;
1810}
1811
8f26e18c
JEB
1812static int _emit_segment_line(struct dm_task *dmt, uint32_t major,
1813 uint32_t minor, struct load_segment *seg,
1814 uint64_t *seg_start, char *params,
1815 size_t paramsize)
1816{
1817 int pos = 0;
1818 int r;
cac52ca4 1819 int target_type_is_raid = 0;
8f26e18c 1820 char originbuf[DM_FORMAT_DEV_BUFSIZE], cowbuf[DM_FORMAT_DEV_BUFSIZE];
dbcb64b8 1821
8f26e18c
JEB
1822 switch(seg->type) {
1823 case SEG_ERROR:
1824 case SEG_ZERO:
1825 case SEG_LINEAR:
1826 break;
1827 case SEG_MIRRORED:
1828 /* Mirrors are pretty complicated - now in separate function */
beecb1e1 1829 r = _mirror_emit_segment_line(dmt, seg, params, paramsize);
3c74075f
JEB
1830 if (!r)
1831 return_0;
165e4a11 1832 break;
b262f3e1
ZK
1833 case SEG_REPLICATOR:
1834 if ((r = _replicator_emit_segment_line(seg, params, paramsize,
1835 &pos)) <= 0) {
1836 stack;
1837 return r;
1838 }
1839 break;
1840 case SEG_REPLICATOR_DEV:
1841 if (!seg->replicator || !_build_dev_string(originbuf,
1842 sizeof(originbuf),
1843 seg->replicator))
1844 return_0;
1845
1846 EMIT_PARAMS(pos, "%s %" PRIu64, originbuf, seg->rdevice_index);
1847 break;
165e4a11 1848 case SEG_SNAPSHOT:
aa6f4e51 1849 case SEG_SNAPSHOT_MERGE:
b4f1578f
AK
1850 if (!_build_dev_string(originbuf, sizeof(originbuf), seg->origin))
1851 return_0;
1852 if (!_build_dev_string(cowbuf, sizeof(cowbuf), seg->cow))
1853 return_0;
ffa9b6a5
ZK
1854 EMIT_PARAMS(pos, "%s %s %c %d", originbuf, cowbuf,
1855 seg->persistent ? 'P' : 'N', seg->chunk_size);
165e4a11
AK
1856 break;
1857 case SEG_SNAPSHOT_ORIGIN:
b4f1578f
AK
1858 if (!_build_dev_string(originbuf, sizeof(originbuf), seg->origin))
1859 return_0;
ffa9b6a5 1860 EMIT_PARAMS(pos, "%s", originbuf);
165e4a11
AK
1861 break;
1862 case SEG_STRIPED:
609faae9 1863 EMIT_PARAMS(pos, "%u %u ", seg->area_count, seg->stripe_size);
165e4a11 1864 break;
12ca060e 1865 case SEG_CRYPT:
609faae9 1866 EMIT_PARAMS(pos, "%s%s%s%s%s %s %" PRIu64 " ", seg->cipher,
12ca060e
MB
1867 seg->chainmode ? "-" : "", seg->chainmode ?: "",
1868 seg->iv ? "-" : "", seg->iv ?: "", seg->key,
1869 seg->iv_offset != DM_CRYPT_IV_DEFAULT ?
1870 seg->iv_offset : *seg_start);
1871 break;
cac52ca4
JEB
1872 case SEG_RAID1:
1873 case SEG_RAID4:
1874 case SEG_RAID5_LA:
1875 case SEG_RAID5_RA:
1876 case SEG_RAID5_LS:
1877 case SEG_RAID5_RS:
1878 case SEG_RAID6_ZR:
1879 case SEG_RAID6_NR:
1880 case SEG_RAID6_NC:
1881 target_type_is_raid = 1;
1882 r = _raid_emit_segment_line(dmt, major, minor, seg, seg_start,
1883 params, paramsize);
1884 if (!r)
1885 return_0;
1886
1887 break;
165e4a11
AK
1888 }
1889
1890 switch(seg->type) {
1891 case SEG_ERROR:
b262f3e1 1892 case SEG_REPLICATOR:
165e4a11
AK
1893 case SEG_SNAPSHOT:
1894 case SEG_SNAPSHOT_ORIGIN:
aa6f4e51 1895 case SEG_SNAPSHOT_MERGE:
165e4a11
AK
1896 case SEG_ZERO:
1897 break;
12ca060e 1898 case SEG_CRYPT:
165e4a11 1899 case SEG_LINEAR:
b262f3e1 1900 case SEG_REPLICATOR_DEV:
165e4a11
AK
1901 case SEG_STRIPED:
1902 if ((r = _emit_areas_line(dmt, seg, params, paramsize, &pos)) <= 0) {
1903 stack;
1904 return r;
1905 }
b6793963
AK
1906 if (!params[0]) {
1907 log_error("No parameters supplied for %s target "
1908 "%u:%u.", dm_segtypes[seg->type].target,
812e10ac 1909 major, minor);
b6793963
AK
1910 return 0;
1911 }
165e4a11
AK
1912 break;
1913 }
1914
4b2cae46
AK
1915 log_debug("Adding target to (%" PRIu32 ":%" PRIu32 "): %" PRIu64
1916 " %" PRIu64 " %s %s", major, minor,
f439e65b
JEB
1917 *seg_start, seg->size, target_type_is_raid ? "raid" :
1918 dm_segtypes[seg->type].target, params);
165e4a11 1919
cac52ca4
JEB
1920 if (!dm_task_add_target(dmt, *seg_start, seg->size,
1921 target_type_is_raid ? "raid" :
1922 dm_segtypes[seg->type].target, params))
b4f1578f 1923 return_0;
165e4a11
AK
1924
1925 *seg_start += seg->size;
1926
1927 return 1;
1928}
1929
ffa9b6a5
ZK
1930#undef EMIT_PARAMS
1931
4b2cae46
AK
1932static int _emit_segment(struct dm_task *dmt, uint32_t major, uint32_t minor,
1933 struct load_segment *seg, uint64_t *seg_start)
165e4a11
AK
1934{
1935 char *params;
1936 size_t paramsize = 4096;
1937 int ret;
1938
1939 do {
1940 if (!(params = dm_malloc(paramsize))) {
1941 log_error("Insufficient space for target parameters.");
1942 return 0;
1943 }
1944
12ea7cb1 1945 params[0] = '\0';
4b2cae46
AK
1946 ret = _emit_segment_line(dmt, major, minor, seg, seg_start,
1947 params, paramsize);
165e4a11
AK
1948 dm_free(params);
1949
1950 if (!ret)
1951 stack;
1952
1953 if (ret >= 0)
1954 return ret;
1955
1956 log_debug("Insufficient space in params[%" PRIsize_t
1957 "] for target parameters.", paramsize);
1958
1959 paramsize *= 2;
1960 } while (paramsize < MAX_TARGET_PARAMSIZE);
1961
1962 log_error("Target parameter size too big. Aborting.");
1963 return 0;
1964}
1965
b4f1578f 1966static int _load_node(struct dm_tree_node *dnode)
165e4a11
AK
1967{
1968 int r = 0;
1969 struct dm_task *dmt;
1970 struct load_segment *seg;
df390f17 1971 uint64_t seg_start = 0, existing_table_size;
165e4a11 1972
4b2cae46
AK
1973 log_verbose("Loading %s table (%" PRIu32 ":%" PRIu32 ")", dnode->name,
1974 dnode->info.major, dnode->info.minor);
165e4a11
AK
1975
1976 if (!(dmt = dm_task_create(DM_DEVICE_RELOAD))) {
1977 log_error("Reload dm_task creation failed for %s", dnode->name);
1978 return 0;
1979 }
1980
1981 if (!dm_task_set_major(dmt, dnode->info.major) ||
1982 !dm_task_set_minor(dmt, dnode->info.minor)) {
1983 log_error("Failed to set device number for %s reload.", dnode->name);
1984 goto out;
1985 }
1986
1987 if (dnode->props.read_only && !dm_task_set_ro(dmt)) {
1988 log_error("Failed to set read only flag for %s", dnode->name);
1989 goto out;
1990 }
1991
1992 if (!dm_task_no_open_count(dmt))
1993 log_error("Failed to disable open_count");
1994
2c44337b 1995 dm_list_iterate_items(seg, &dnode->props.segs)
4b2cae46
AK
1996 if (!_emit_segment(dmt, dnode->info.major, dnode->info.minor,
1997 seg, &seg_start))
b4f1578f 1998 goto_out;
165e4a11 1999
ec289b64
AK
2000 if (!dm_task_suppress_identical_reload(dmt))
2001 log_error("Failed to suppress reload of identical tables.");
2002
2003 if ((r = dm_task_run(dmt))) {
165e4a11 2004 r = dm_task_get_info(dmt, &dnode->info);
ec289b64
AK
2005 if (r && !dnode->info.inactive_table)
2006 log_verbose("Suppressed %s identical table reload.",
2007 dnode->name);
bb875bb9 2008
df390f17 2009 existing_table_size = dm_task_get_existing_table_size(dmt);
bb875bb9 2010 if ((dnode->props.size_changed =
df390f17 2011 (existing_table_size == seg_start) ? 0 : 1)) {
bb875bb9 2012 log_debug("Table size changed from %" PRIu64 " to %"
df390f17 2013 PRIu64 " for %s", existing_table_size,
bb875bb9 2014 seg_start, dnode->name);
df390f17
AK
2015 /*
2016 * Kernel usually skips size validation on zero-length devices
2017 * now so no need to preload them.
2018 */
2019 /* FIXME In which kernel version did this begin? */
2020 if (!existing_table_size && dnode->props.delay_resume_if_new)
2021 dnode->props.size_changed = 0;
2022 }
ec289b64 2023 }
165e4a11
AK
2024
2025 dnode->props.segment_count = 0;
2026
2027out:
2028 dm_task_destroy(dmt);
2029
2030 return r;
165e4a11
AK
2031}
2032
b4f1578f 2033int dm_tree_preload_children(struct dm_tree_node *dnode,
bb875bb9
AK
2034 const char *uuid_prefix,
2035 size_t uuid_prefix_len)
165e4a11 2036{
2ca6b865 2037 int r = 1;
165e4a11 2038 void *handle = NULL;
b4f1578f 2039 struct dm_tree_node *child;
165e4a11 2040 struct dm_info newinfo;
566515c0 2041 int update_devs_flag = 0;
165e4a11
AK
2042
2043 /* Preload children first */
b4f1578f 2044 while ((child = dm_tree_next_child(&handle, dnode, 0))) {
165e4a11
AK
2045 /* Skip existing non-device-mapper devices */
2046 if (!child->info.exists && child->info.major)
2047 continue;
2048
2049 /* Ignore if it doesn't belong to this VG */
87f98002
AK
2050 if (child->info.exists &&
2051 !_uuid_prefix_matches(child->uuid, uuid_prefix, uuid_prefix_len))
165e4a11
AK
2052 continue;
2053
b4f1578f 2054 if (dm_tree_node_num_children(child, 0))
2ca6b865
MS
2055 if (!dm_tree_preload_children(child, uuid_prefix, uuid_prefix_len))
2056 return_0;
165e4a11 2057
165e4a11
AK
2058 /* FIXME Cope if name exists with no uuid? */
2059 if (!child->info.exists) {
2060 if (!_create_node(child)) {
2061 stack;
2062 return 0;
2063 }
2064 }
2065
2066 if (!child->info.inactive_table && child->props.segment_count) {
2067 if (!_load_node(child)) {
2068 stack;
2069 return 0;
2070 }
2071 }
2072
eb91c4ee
MB
2073 /* Propagate device size change change */
2074 if (child->props.size_changed)
2075 dnode->props.size_changed = 1;
2076
bb875bb9 2077 /* Resume device immediately if it has parents and its size changed */
3776c494 2078 if (!dm_tree_node_num_children(child, 1) || !child->props.size_changed)
165e4a11
AK
2079 continue;
2080
7707ea90
AK
2081 if (!child->info.inactive_table && !child->info.suspended)
2082 continue;
2083
fc795d87 2084 if (!_resume_node(child->name, child->info.major, child->info.minor,
bd90c6b2 2085 child->props.read_ahead, child->props.read_ahead_flags,
1840aa09
AK
2086 &newinfo, &child->dtree->cookie, child->udev_flags,
2087 child->info.suspended)) {
165e4a11 2088 log_error("Unable to resume %s (%" PRIu32
fc795d87 2089 ":%" PRIu32 ")", child->name, child->info.major,
165e4a11 2090 child->info.minor);
2ca6b865 2091 r = 0;
165e4a11
AK
2092 continue;
2093 }
2094
2095 /* Update cached info */
2096 child->info = newinfo;
566515c0
PR
2097
2098 /*
2099 * Prepare for immediate synchronization with udev and flush all stacked
2100 * dev node operations if requested by immediate_dev_node property. But
2101 * finish processing current level in the tree first.
2102 */
2103 if (child->props.immediate_dev_node)
2104 update_devs_flag = 1;
2105
165e4a11
AK
2106 }
2107
2108 handle = NULL;
2109
566515c0
PR
2110 if (update_devs_flag) {
2111 if (!dm_udev_wait(dm_tree_get_cookie(dnode)))
2112 stack;
2113 dm_tree_set_cookie(dnode, 0);
566515c0
PR
2114 }
2115
2ca6b865 2116 return r;
165e4a11
AK
2117}
2118
165e4a11
AK
2119/*
2120 * Returns 1 if unsure.
2121 */
b4f1578f 2122int dm_tree_children_use_uuid(struct dm_tree_node *dnode,
165e4a11
AK
2123 const char *uuid_prefix,
2124 size_t uuid_prefix_len)
2125{
2126 void *handle = NULL;
b4f1578f 2127 struct dm_tree_node *child = dnode;
165e4a11
AK
2128 const char *uuid;
2129
b4f1578f
AK
2130 while ((child = dm_tree_next_child(&handle, dnode, 0))) {
2131 if (!(uuid = dm_tree_node_get_uuid(child))) {
2132 log_error("Failed to get uuid for dtree node.");
165e4a11
AK
2133 return 1;
2134 }
2135
87f98002 2136 if (_uuid_prefix_matches(uuid, uuid_prefix, uuid_prefix_len))
165e4a11
AK
2137 return 1;
2138
b4f1578f
AK
2139 if (dm_tree_node_num_children(child, 0))
2140 dm_tree_children_use_uuid(child, uuid_prefix, uuid_prefix_len);
165e4a11
AK
2141 }
2142
2143 return 0;
2144}
2145
2146/*
2147 * Target functions
2148 */
b4f1578f 2149static struct load_segment *_add_segment(struct dm_tree_node *dnode, unsigned type, uint64_t size)
165e4a11
AK
2150{
2151 struct load_segment *seg;
2152
b4f1578f
AK
2153 if (!(seg = dm_pool_zalloc(dnode->dtree->mem, sizeof(*seg)))) {
2154 log_error("dtree node segment allocation failed");
165e4a11
AK
2155 return NULL;
2156 }
2157
2158 seg->type = type;
2159 seg->size = size;
2160 seg->area_count = 0;
2c44337b 2161 dm_list_init(&seg->areas);
165e4a11
AK
2162 seg->stripe_size = 0;
2163 seg->persistent = 0;
2164 seg->chunk_size = 0;
2165 seg->cow = NULL;
2166 seg->origin = NULL;
aa6f4e51 2167 seg->merge = NULL;
165e4a11 2168
2c44337b 2169 dm_list_add(&dnode->props.segs, &seg->list);
165e4a11
AK
2170 dnode->props.segment_count++;
2171
2172 return seg;
2173}
2174
b4f1578f 2175int dm_tree_node_add_snapshot_origin_target(struct dm_tree_node *dnode,
40e5fd8b
AK
2176 uint64_t size,
2177 const char *origin_uuid)
165e4a11
AK
2178{
2179 struct load_segment *seg;
b4f1578f 2180 struct dm_tree_node *origin_node;
165e4a11 2181
b4f1578f
AK
2182 if (!(seg = _add_segment(dnode, SEG_SNAPSHOT_ORIGIN, size)))
2183 return_0;
165e4a11 2184
b4f1578f 2185 if (!(origin_node = dm_tree_find_node_by_uuid(dnode->dtree, origin_uuid))) {
165e4a11
AK
2186 log_error("Couldn't find snapshot origin uuid %s.", origin_uuid);
2187 return 0;
2188 }
2189
2190 seg->origin = origin_node;
b4f1578f
AK
2191 if (!_link_tree_nodes(dnode, origin_node))
2192 return_0;
165e4a11 2193
56c28292
AK
2194 /* Resume snapshot origins after new snapshots */
2195 dnode->activation_priority = 1;
2196
165e4a11
AK
2197 return 1;
2198}
2199
aa6f4e51
MS
2200static int _add_snapshot_target(struct dm_tree_node *node,
2201 uint64_t size,
2202 const char *origin_uuid,
2203 const char *cow_uuid,
2204 const char *merge_uuid,
2205 int persistent,
2206 uint32_t chunk_size)
165e4a11
AK
2207{
2208 struct load_segment *seg;
aa6f4e51
MS
2209 struct dm_tree_node *origin_node, *cow_node, *merge_node;
2210 unsigned seg_type;
2211
2212 seg_type = !merge_uuid ? SEG_SNAPSHOT : SEG_SNAPSHOT_MERGE;
165e4a11 2213
aa6f4e51 2214 if (!(seg = _add_segment(node, seg_type, size)))
b4f1578f 2215 return_0;
165e4a11 2216
b4f1578f 2217 if (!(origin_node = dm_tree_find_node_by_uuid(node->dtree, origin_uuid))) {
165e4a11
AK
2218 log_error("Couldn't find snapshot origin uuid %s.", origin_uuid);
2219 return 0;
2220 }
2221
2222 seg->origin = origin_node;
b4f1578f
AK
2223 if (!_link_tree_nodes(node, origin_node))
2224 return_0;
165e4a11 2225
b4f1578f 2226 if (!(cow_node = dm_tree_find_node_by_uuid(node->dtree, cow_uuid))) {
aa6f4e51 2227 log_error("Couldn't find snapshot COW device uuid %s.", cow_uuid);
165e4a11
AK
2228 return 0;
2229 }
2230
2231 seg->cow = cow_node;
b4f1578f
AK
2232 if (!_link_tree_nodes(node, cow_node))
2233 return_0;
165e4a11
AK
2234
2235 seg->persistent = persistent ? 1 : 0;
2236 seg->chunk_size = chunk_size;
2237
aa6f4e51
MS
2238 if (merge_uuid) {
2239 if (!(merge_node = dm_tree_find_node_by_uuid(node->dtree, merge_uuid))) {
2240 /* not a pure error, merging snapshot may have been deactivated */
2241 log_verbose("Couldn't find merging snapshot uuid %s.", merge_uuid);
2242 } else {
2243 seg->merge = merge_node;
2244 /* must not link merging snapshot, would undermine activation_priority below */
2245 }
2246
2247 /* Resume snapshot-merge (acting origin) after other snapshots */
2248 node->activation_priority = 1;
2249 if (seg->merge) {
2250 /* Resume merging snapshot after snapshot-merge */
2251 seg->merge->activation_priority = 2;
2252 }
2253 }
2254
165e4a11
AK
2255 return 1;
2256}
2257
aa6f4e51
MS
2258
2259int dm_tree_node_add_snapshot_target(struct dm_tree_node *node,
2260 uint64_t size,
2261 const char *origin_uuid,
2262 const char *cow_uuid,
2263 int persistent,
2264 uint32_t chunk_size)
2265{
2266 return _add_snapshot_target(node, size, origin_uuid, cow_uuid,
2267 NULL, persistent, chunk_size);
2268}
2269
2270int dm_tree_node_add_snapshot_merge_target(struct dm_tree_node *node,
2271 uint64_t size,
2272 const char *origin_uuid,
2273 const char *cow_uuid,
2274 const char *merge_uuid,
2275 uint32_t chunk_size)
2276{
2277 return _add_snapshot_target(node, size, origin_uuid, cow_uuid,
2278 merge_uuid, 1, chunk_size);
2279}
2280
b4f1578f 2281int dm_tree_node_add_error_target(struct dm_tree_node *node,
40e5fd8b 2282 uint64_t size)
165e4a11 2283{
b4f1578f
AK
2284 if (!_add_segment(node, SEG_ERROR, size))
2285 return_0;
165e4a11
AK
2286
2287 return 1;
2288}
2289
b4f1578f 2290int dm_tree_node_add_zero_target(struct dm_tree_node *node,
40e5fd8b 2291 uint64_t size)
165e4a11 2292{
b4f1578f
AK
2293 if (!_add_segment(node, SEG_ZERO, size))
2294 return_0;
165e4a11
AK
2295
2296 return 1;
2297}
2298
b4f1578f 2299int dm_tree_node_add_linear_target(struct dm_tree_node *node,
40e5fd8b 2300 uint64_t size)
165e4a11 2301{
b4f1578f
AK
2302 if (!_add_segment(node, SEG_LINEAR, size))
2303 return_0;
165e4a11
AK
2304
2305 return 1;
2306}
2307
b4f1578f 2308int dm_tree_node_add_striped_target(struct dm_tree_node *node,
40e5fd8b
AK
2309 uint64_t size,
2310 uint32_t stripe_size)
165e4a11
AK
2311{
2312 struct load_segment *seg;
2313
b4f1578f
AK
2314 if (!(seg = _add_segment(node, SEG_STRIPED, size)))
2315 return_0;
165e4a11
AK
2316
2317 seg->stripe_size = stripe_size;
2318
2319 return 1;
2320}
2321
12ca060e
MB
2322int dm_tree_node_add_crypt_target(struct dm_tree_node *node,
2323 uint64_t size,
2324 const char *cipher,
2325 const char *chainmode,
2326 const char *iv,
2327 uint64_t iv_offset,
2328 const char *key)
2329{
2330 struct load_segment *seg;
2331
2332 if (!(seg = _add_segment(node, SEG_CRYPT, size)))
2333 return_0;
2334
2335 seg->cipher = cipher;
2336 seg->chainmode = chainmode;
2337 seg->iv = iv;
2338 seg->iv_offset = iv_offset;
2339 seg->key = key;
2340
2341 return 1;
2342}
2343
b4f1578f 2344int dm_tree_node_add_mirror_target_log(struct dm_tree_node *node,
165e4a11 2345 uint32_t region_size,
08e64ce5 2346 unsigned clustered,
165e4a11 2347 const char *log_uuid,
ce7ed2c0
AK
2348 unsigned area_count,
2349 uint32_t flags)
165e4a11 2350{
908db078 2351 struct dm_tree_node *log_node = NULL;
165e4a11
AK
2352 struct load_segment *seg;
2353
2354 if (!node->props.segment_count) {
b8175c33 2355 log_error(INTERNAL_ERROR "Attempt to add target area to missing segment.");
165e4a11
AK
2356 return 0;
2357 }
2358
2c44337b 2359 seg = dm_list_item(dm_list_last(&node->props.segs), struct load_segment);
165e4a11 2360
24b026e3 2361 if (log_uuid) {
67b25ed4
AK
2362 if (!(seg->uuid = dm_pool_strdup(node->dtree->mem, log_uuid))) {
2363 log_error("log uuid pool_strdup failed");
2364 return 0;
2365 }
df390f17
AK
2366 if ((flags & DM_CORELOG))
2367 /* For pvmove: immediate resume (for size validation) isn't needed. */
2368 node->props.delay_resume_if_new = 1;
2369 else {
9723090c
AK
2370 if (!(log_node = dm_tree_find_node_by_uuid(node->dtree, log_uuid))) {
2371 log_error("Couldn't find mirror log uuid %s.", log_uuid);
2372 return 0;
2373 }
2374
566515c0
PR
2375 if (clustered)
2376 log_node->props.immediate_dev_node = 1;
2377
0a99713e
AK
2378 /* The kernel validates the size of disk logs. */
2379 /* FIXME Propagate to any devices below */
2380 log_node->props.delay_resume_if_new = 0;
2381
9723090c
AK
2382 if (!_link_tree_nodes(node, log_node))
2383 return_0;
2384 }
165e4a11
AK
2385 }
2386
2387 seg->log = log_node;
165e4a11
AK
2388 seg->region_size = region_size;
2389 seg->clustered = clustered;
2390 seg->mirror_area_count = area_count;
dbcb64b8 2391 seg->flags = flags;
165e4a11
AK
2392
2393 return 1;
2394}
2395
b4f1578f 2396int dm_tree_node_add_mirror_target(struct dm_tree_node *node,
40e5fd8b 2397 uint64_t size)
165e4a11 2398{
cbecd3cd 2399 if (!_add_segment(node, SEG_MIRRORED, size))
b4f1578f 2400 return_0;
165e4a11
AK
2401
2402 return 1;
2403}
2404
cac52ca4
JEB
2405int dm_tree_node_add_raid_target(struct dm_tree_node *node,
2406 uint64_t size,
2407 const char *raid_type,
2408 uint32_t region_size,
2409 uint32_t stripe_size,
f439e65b 2410 uint64_t rebuilds,
cac52ca4
JEB
2411 uint64_t reserved2)
2412{
2413 int i;
2414 struct load_segment *seg = NULL;
2415
2416 for (i = 0; dm_segtypes[i].target && !seg; i++)
2417 if (!strcmp(raid_type, dm_segtypes[i].target))
2418 if (!(seg = _add_segment(node,
2419 dm_segtypes[i].type, size)))
2420 return_0;
2421
b2fa9b43
JEB
2422 if (!seg)
2423 return_0;
2424
cac52ca4
JEB
2425 seg->region_size = region_size;
2426 seg->stripe_size = stripe_size;
2427 seg->area_count = 0;
f439e65b 2428 seg->rebuilds = rebuilds;
cac52ca4
JEB
2429
2430 return 1;
2431}
2432
b262f3e1
ZK
2433int dm_tree_node_add_replicator_target(struct dm_tree_node *node,
2434 uint64_t size,
2435 const char *rlog_uuid,
2436 const char *rlog_type,
2437 unsigned rsite_index,
2438 dm_replicator_mode_t mode,
2439 uint32_t async_timeout,
2440 uint64_t fall_behind_data,
2441 uint32_t fall_behind_ios)
2442{
2443 struct load_segment *rseg;
2444 struct replicator_site *rsite;
2445
2446 /* Local site0 - adds replicator segment and links rlog device */
2447 if (rsite_index == REPLICATOR_LOCAL_SITE) {
2448 if (node->props.segment_count) {
2449 log_error(INTERNAL_ERROR "Attempt to add replicator segment to already used node.");
2450 return 0;
2451 }
2452
2453 if (!(rseg = _add_segment(node, SEG_REPLICATOR, size)))
2454 return_0;
2455
2456 if (!(rseg->log = dm_tree_find_node_by_uuid(node->dtree, rlog_uuid))) {
2457 log_error("Missing replicator log uuid %s.", rlog_uuid);
2458 return 0;
2459 }
2460
2461 if (!_link_tree_nodes(node, rseg->log))
2462 return_0;
2463
2464 if (strcmp(rlog_type, "ringbuffer") != 0) {
2465 log_error("Unsupported replicator log type %s.", rlog_type);
2466 return 0;
2467 }
2468
2469 if (!(rseg->rlog_type = dm_pool_strdup(node->dtree->mem, rlog_type)))
2470 return_0;
2471
2472 dm_list_init(&rseg->rsites);
2473 rseg->rdevice_count = 0;
2474 node->activation_priority = 1;
2475 }
2476
2477 /* Add site to segment */
2478 if (mode == DM_REPLICATOR_SYNC
2479 && (async_timeout || fall_behind_ios || fall_behind_data)) {
2480 log_error("Async parameters passed for synchronnous replicator.");
2481 return 0;
2482 }
2483
2484 if (node->props.segment_count != 1) {
2485 log_error(INTERNAL_ERROR "Attempt to add remote site area before setting replicator log.");
2486 return 0;
2487 }
2488
2489 rseg = dm_list_item(dm_list_last(&node->props.segs), struct load_segment);
2490 if (rseg->type != SEG_REPLICATOR) {
2491 log_error(INTERNAL_ERROR "Attempt to use non replicator segment %s.",
2492 dm_segtypes[rseg->type].target);
2493 return 0;
2494 }
2495
2496 if (!(rsite = dm_pool_zalloc(node->dtree->mem, sizeof(*rsite)))) {
2497 log_error("Failed to allocate remote site segment.");
2498 return 0;
2499 }
2500
2501 dm_list_add(&rseg->rsites, &rsite->list);
2502 rseg->rsite_count++;
2503
2504 rsite->mode = mode;
2505 rsite->async_timeout = async_timeout;
2506 rsite->fall_behind_data = fall_behind_data;
2507 rsite->fall_behind_ios = fall_behind_ios;
2508 rsite->rsite_index = rsite_index;
2509
2510 return 1;
2511}
2512
2513/* Appends device node to Replicator */
2514int dm_tree_node_add_replicator_dev_target(struct dm_tree_node *node,
2515 uint64_t size,
2516 const char *replicator_uuid,
2517 uint64_t rdevice_index,
2518 const char *rdev_uuid,
2519 unsigned rsite_index,
2520 const char *slog_uuid,
2521 uint32_t slog_flags,
2522 uint32_t slog_region_size)
2523{
2524 struct seg_area *area;
2525 struct load_segment *rseg;
2526 struct load_segment *rep_seg;
2527
2528 if (rsite_index == REPLICATOR_LOCAL_SITE) {
2529 /* Site index for local target */
2530 if (!(rseg = _add_segment(node, SEG_REPLICATOR_DEV, size)))
2531 return_0;
2532
2533 if (!(rseg->replicator = dm_tree_find_node_by_uuid(node->dtree, replicator_uuid))) {
2534 log_error("Missing replicator uuid %s.", replicator_uuid);
2535 return 0;
2536 }
2537
2538 /* Local slink0 for replicator must be always initialized first */
2539 if (rseg->replicator->props.segment_count != 1) {
2540 log_error(INTERNAL_ERROR "Attempt to use non replicator segment.");
2541 return 0;
2542 }
2543
2544 rep_seg = dm_list_item(dm_list_last(&rseg->replicator->props.segs), struct load_segment);
2545 if (rep_seg->type != SEG_REPLICATOR) {
2546 log_error(INTERNAL_ERROR "Attempt to use non replicator segment %s.",
2547 dm_segtypes[rep_seg->type].target);
2548 return 0;
2549 }
2550 rep_seg->rdevice_count++;
2551
2552 if (!_link_tree_nodes(node, rseg->replicator))
2553 return_0;
2554
2555 rseg->rdevice_index = rdevice_index;
2556 } else {
2557 /* Local slink0 for replicator must be always initialized first */
2558 if (node->props.segment_count != 1) {
2559 log_error(INTERNAL_ERROR "Attempt to use non replicator-dev segment.");
2560 return 0;
2561 }
2562
2563 rseg = dm_list_item(dm_list_last(&node->props.segs), struct load_segment);
2564 if (rseg->type != SEG_REPLICATOR_DEV) {
2565 log_error(INTERNAL_ERROR "Attempt to use non replicator-dev segment %s.",
2566 dm_segtypes[rseg->type].target);
2567 return 0;
2568 }
2569 }
2570
2571 if (!(slog_flags & DM_CORELOG) && !slog_uuid) {
2572 log_error("Unspecified sync log uuid.");
2573 return 0;
2574 }
2575
2576 if (!dm_tree_node_add_target_area(node, NULL, rdev_uuid, 0))
2577 return_0;
2578
2579 area = dm_list_item(dm_list_last(&rseg->areas), struct seg_area);
2580
2581 if (!(slog_flags & DM_CORELOG)) {
2582 if (!(area->slog = dm_tree_find_node_by_uuid(node->dtree, slog_uuid))) {
2583 log_error("Couldn't find sync log uuid %s.", slog_uuid);
2584 return 0;
2585 }
2586
2587 if (!_link_tree_nodes(node, area->slog))
2588 return_0;
2589 }
2590
2591 area->flags = slog_flags;
2592 area->region_size = slog_region_size;
2593 area->rsite_index = rsite_index;
2594
2595 return 1;
2596}
2597
b4f1578f 2598static int _add_area(struct dm_tree_node *node, struct load_segment *seg, struct dm_tree_node *dev_node, uint64_t offset)
165e4a11
AK
2599{
2600 struct seg_area *area;
2601
b4f1578f 2602 if (!(area = dm_pool_zalloc(node->dtree->mem, sizeof (*area)))) {
165e4a11
AK
2603 log_error("Failed to allocate target segment area.");
2604 return 0;
2605 }
2606
2607 area->dev_node = dev_node;
2608 area->offset = offset;
2609
2c44337b 2610 dm_list_add(&seg->areas, &area->list);
165e4a11
AK
2611 seg->area_count++;
2612
2613 return 1;
2614}
2615
b4f1578f 2616int dm_tree_node_add_target_area(struct dm_tree_node *node,
40e5fd8b
AK
2617 const char *dev_name,
2618 const char *uuid,
2619 uint64_t offset)
165e4a11
AK
2620{
2621 struct load_segment *seg;
2622 struct stat info;
b4f1578f 2623 struct dm_tree_node *dev_node;
165e4a11
AK
2624
2625 if ((!dev_name || !*dev_name) && (!uuid || !*uuid)) {
b4f1578f 2626 log_error("dm_tree_node_add_target_area called without device");
165e4a11
AK
2627 return 0;
2628 }
2629
2630 if (uuid) {
b4f1578f 2631 if (!(dev_node = dm_tree_find_node_by_uuid(node->dtree, uuid))) {
165e4a11
AK
2632 log_error("Couldn't find area uuid %s.", uuid);
2633 return 0;
2634 }
b4f1578f
AK
2635 if (!_link_tree_nodes(node, dev_node))
2636 return_0;
165e4a11 2637 } else {
6d04311e 2638 if (stat(dev_name, &info) < 0) {
165e4a11
AK
2639 log_error("Device %s not found.", dev_name);
2640 return 0;
2641 }
2642
40e5fd8b 2643 if (!S_ISBLK(info.st_mode)) {
165e4a11
AK
2644 log_error("Device %s is not a block device.", dev_name);
2645 return 0;
2646 }
2647
2648 /* FIXME Check correct macro use */
cda69e17
PR
2649 if (!(dev_node = _add_dev(node->dtree, node, MAJOR(info.st_rdev),
2650 MINOR(info.st_rdev), 0)))
b4f1578f 2651 return_0;
165e4a11
AK
2652 }
2653
2654 if (!node->props.segment_count) {
b8175c33 2655 log_error(INTERNAL_ERROR "Attempt to add target area to missing segment.");
165e4a11
AK
2656 return 0;
2657 }
2658
2c44337b 2659 seg = dm_list_item(dm_list_last(&node->props.segs), struct load_segment);
165e4a11 2660
b4f1578f
AK
2661 if (!_add_area(node, seg, dev_node, offset))
2662 return_0;
165e4a11
AK
2663
2664 return 1;
db208f51 2665}
bd90c6b2 2666
6d04311e
JEB
2667int dm_tree_node_add_null_area(struct dm_tree_node *node, uint64_t offset)
2668{
2669 struct load_segment *seg;
2670
2671 seg = dm_list_item(dm_list_last(&node->props.segs), struct load_segment);
2672
415c0690
AK
2673 switch (seg->type) {
2674 case SEG_RAID1:
2675 case SEG_RAID4:
2676 case SEG_RAID5_LA:
2677 case SEG_RAID5_RA:
2678 case SEG_RAID5_LS:
2679 case SEG_RAID5_RS:
2680 case SEG_RAID6_ZR:
2681 case SEG_RAID6_NR:
2682 case SEG_RAID6_NC:
2683 break;
2684 default:
2685 log_error("dm_tree_node_add_null_area() called on an unsupported segment type");
2686 return 0;
2687 }
2688
6d04311e
JEB
2689 if (!_add_area(node, seg, NULL, offset))
2690 return_0;
2691
2692 return 1;
2693}
2694
bd90c6b2
AK
2695void dm_tree_set_cookie(struct dm_tree_node *node, uint32_t cookie)
2696{
2697 node->dtree->cookie = cookie;
2698}
2699
2700uint32_t dm_tree_get_cookie(struct dm_tree_node *node)
2701{
2702 return node->dtree->cookie;
2703}
This page took 0.459217 seconds and 5 git commands to generate.