]> sourceware.org Git - lvm2.git/blame - libdm/libdm-deptree.c
Use execvp for clvmd restart
[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. */
950 if (!*dm_sysfs_dir())
951 return !info->open_count;
952
953 if (dm_device_has_holders(info->major, info->minor)) {
954 log_error("Device %" PRIu32 ":%" PRIu32 " is used "
955 "by another device.", info->major, info->minor);
956 return 0;
957 }
958
959 if (dm_device_has_mounted_fs(info->major, info->minor)) {
960 log_error("Device %" PRIu32 ":%" PRIu32 " contains "
961 "a filesystem in use.", info->major, info->minor);
962 return 0;
963 }
964
965 return 1;
966}
967
f3ef15ef
ZK
968/* Check if all parent nodes of given node have open_count == 0 */
969static int _node_has_closed_parents(struct dm_tree_node *node,
970 const char *uuid_prefix,
971 size_t uuid_prefix_len)
972{
973 struct dm_tree_link *dlink;
974 const struct dm_info *dinfo;
975 struct dm_info info;
976 const char *uuid;
977
978 /* Iterate through parents of this node */
979 dm_list_iterate_items(dlink, &node->used_by) {
980 if (!(uuid = dm_tree_node_get_uuid(dlink->node))) {
981 stack;
982 continue;
983 }
984
985 /* Ignore if it doesn't belong to this VG */
986 if (!_uuid_prefix_matches(uuid, uuid_prefix, uuid_prefix_len))
987 continue;
988
989 if (!(dinfo = dm_tree_node_get_info(dlink->node))) {
990 stack; /* FIXME Is this normal? */
991 return 0;
992 }
993
994 /* Refresh open_count */
995 if (!_info_by_dev(dinfo->major, dinfo->minor, 1, &info) ||
996 !info.exists)
997 continue;
998
eb418883
ZK
999 if (info.open_count) {
1000 log_debug("Node %s %d:%d has open_count %d", uuid_prefix,
1001 dinfo->major, dinfo->minor, info.open_count);
f3ef15ef 1002 return 0;
eb418883 1003 }
f3ef15ef
ZK
1004 }
1005
1006 return 1;
1007}
1008
f16aea9e 1009static int _deactivate_node(const char *name, uint32_t major, uint32_t minor,
787200ef 1010 uint32_t *cookie, uint16_t udev_flags, int retry)
3e8c6b73
AK
1011{
1012 struct dm_task *dmt;
bd90c6b2 1013 int r = 0;
3e8c6b73
AK
1014
1015 log_verbose("Removing %s (%" PRIu32 ":%" PRIu32 ")", name, major, minor);
1016
1017 if (!(dmt = dm_task_create(DM_DEVICE_REMOVE))) {
1018 log_error("Deactivation dm_task creation failed for %s", name);
1019 return 0;
1020 }
1021
1022 if (!dm_task_set_major(dmt, major) || !dm_task_set_minor(dmt, minor)) {
1023 log_error("Failed to set device number for %s deactivation", name);
bd90c6b2 1024 goto out;
3e8c6b73
AK
1025 }
1026
1027 if (!dm_task_no_open_count(dmt))
1028 log_error("Failed to disable open_count");
1029
f16aea9e 1030 if (!dm_task_set_cookie(dmt, cookie, udev_flags))
bd90c6b2
AK
1031 goto out;
1032
787200ef
PR
1033
1034 if (retry)
1035 dm_task_retry_remove(dmt);
1036
3e8c6b73
AK
1037 r = dm_task_run(dmt);
1038
0437bccc
AK
1039 /* FIXME Until kernel returns actual name so dm-iface.c can handle it */
1040 rm_dev_node(name, dmt->cookie_set && !(udev_flags & DM_UDEV_DISABLE_DM_RULES_FLAG),
9032898e 1041 dmt->cookie_set && (udev_flags & DM_UDEV_DISABLE_LIBRARY_FALLBACK));
165e4a11 1042
db208f51
AK
1043 /* FIXME Remove node from tree or mark invalid? */
1044
bd90c6b2 1045out:
db208f51
AK
1046 dm_task_destroy(dmt);
1047
1048 return r;
1049}
1050
bd90c6b2 1051static int _rename_node(const char *old_name, const char *new_name, uint32_t major,
f16aea9e 1052 uint32_t minor, uint32_t *cookie, uint16_t udev_flags)
165e4a11
AK
1053{
1054 struct dm_task *dmt;
1055 int r = 0;
1056
1057 log_verbose("Renaming %s (%" PRIu32 ":%" PRIu32 ") to %s", old_name, major, minor, new_name);
1058
1059 if (!(dmt = dm_task_create(DM_DEVICE_RENAME))) {
1060 log_error("Rename dm_task creation failed for %s", old_name);
1061 return 0;
1062 }
1063
1064 if (!dm_task_set_name(dmt, old_name)) {
1065 log_error("Failed to set name for %s rename.", old_name);
1066 goto out;
1067 }
1068
b4f1578f 1069 if (!dm_task_set_newname(dmt, new_name))
40e5fd8b 1070 goto_out;
165e4a11
AK
1071
1072 if (!dm_task_no_open_count(dmt))
1073 log_error("Failed to disable open_count");
1074
f16aea9e 1075 if (!dm_task_set_cookie(dmt, cookie, udev_flags))
bd90c6b2
AK
1076 goto out;
1077
165e4a11
AK
1078 r = dm_task_run(dmt);
1079
1080out:
1081 dm_task_destroy(dmt);
1082
1083 return r;
1084}
1085
165e4a11
AK
1086/* FIXME Merge with _suspend_node? */
1087static int _resume_node(const char *name, uint32_t major, uint32_t minor,
52b84409 1088 uint32_t read_ahead, uint32_t read_ahead_flags,
f16aea9e 1089 struct dm_info *newinfo, uint32_t *cookie,
1840aa09 1090 uint16_t udev_flags, int already_suspended)
165e4a11
AK
1091{
1092 struct dm_task *dmt;
bd90c6b2 1093 int r = 0;
165e4a11
AK
1094
1095 log_verbose("Resuming %s (%" PRIu32 ":%" PRIu32 ")", name, major, minor);
1096
1097 if (!(dmt = dm_task_create(DM_DEVICE_RESUME))) {
1098 log_error("Suspend dm_task creation failed for %s", name);
1099 return 0;
1100 }
1101
0b7d16bc
AK
1102 /* FIXME Kernel should fill in name on return instead */
1103 if (!dm_task_set_name(dmt, name)) {
1104 log_error("Failed to set readahead device name for %s", name);
bd90c6b2 1105 goto out;
0b7d16bc
AK
1106 }
1107
165e4a11
AK
1108 if (!dm_task_set_major(dmt, major) || !dm_task_set_minor(dmt, minor)) {
1109 log_error("Failed to set device number for %s resumption.", name);
bd90c6b2 1110 goto out;
165e4a11
AK
1111 }
1112
1113 if (!dm_task_no_open_count(dmt))
1114 log_error("Failed to disable open_count");
1115
52b84409
AK
1116 if (!dm_task_set_read_ahead(dmt, read_ahead, read_ahead_flags))
1117 log_error("Failed to set read ahead");
1118
f16aea9e 1119 if (!dm_task_set_cookie(dmt, cookie, udev_flags))
bd90c6b2
AK
1120 goto out;
1121
1840aa09
AK
1122 if ((r = dm_task_run(dmt))) {
1123 if (already_suspended)
1124 dec_suspended();
165e4a11 1125 r = dm_task_get_info(dmt, newinfo);
1840aa09 1126 }
165e4a11 1127
bd90c6b2 1128out:
165e4a11
AK
1129 dm_task_destroy(dmt);
1130
1131 return r;
1132}
1133
db208f51 1134static int _suspend_node(const char *name, uint32_t major, uint32_t minor,
b9ffd32c 1135 int skip_lockfs, int no_flush, struct dm_info *newinfo)
db208f51
AK
1136{
1137 struct dm_task *dmt;
1138 int r;
1139
b9ffd32c
AK
1140 log_verbose("Suspending %s (%" PRIu32 ":%" PRIu32 ")%s%s",
1141 name, major, minor,
1142 skip_lockfs ? "" : " with filesystem sync",
6e1898a5 1143 no_flush ? "" : " with device flush");
db208f51
AK
1144
1145 if (!(dmt = dm_task_create(DM_DEVICE_SUSPEND))) {
1146 log_error("Suspend dm_task creation failed for %s", name);
1147 return 0;
1148 }
1149
1150 if (!dm_task_set_major(dmt, major) || !dm_task_set_minor(dmt, minor)) {
1151 log_error("Failed to set device number for %s suspension.", name);
1152 dm_task_destroy(dmt);
1153 return 0;
1154 }
1155
1156 if (!dm_task_no_open_count(dmt))
1157 log_error("Failed to disable open_count");
1158
c55b1410
AK
1159 if (skip_lockfs && !dm_task_skip_lockfs(dmt))
1160 log_error("Failed to set skip_lockfs flag.");
1161
b9ffd32c
AK
1162 if (no_flush && !dm_task_no_flush(dmt))
1163 log_error("Failed to set no_flush flag.");
1164
1840aa09
AK
1165 if ((r = dm_task_run(dmt))) {
1166 inc_suspended();
db208f51 1167 r = dm_task_get_info(dmt, newinfo);
1840aa09 1168 }
db208f51 1169
3e8c6b73
AK
1170 dm_task_destroy(dmt);
1171
1172 return r;
1173}
1174
18e0f934
AK
1175/*
1176 * FIXME Don't attempt to deactivate known internal dependencies.
1177 */
1178static int _dm_tree_deactivate_children(struct dm_tree_node *dnode,
1179 const char *uuid_prefix,
1180 size_t uuid_prefix_len,
1181 unsigned level)
3e8c6b73 1182{
b7eb2ad0 1183 int r = 1;
3e8c6b73 1184 void *handle = NULL;
b4f1578f 1185 struct dm_tree_node *child = dnode;
3e8c6b73
AK
1186 struct dm_info info;
1187 const struct dm_info *dinfo;
1188 const char *name;
1189 const char *uuid;
1190
b4f1578f
AK
1191 while ((child = dm_tree_next_child(&handle, dnode, 0))) {
1192 if (!(dinfo = dm_tree_node_get_info(child))) {
3e8c6b73
AK
1193 stack;
1194 continue;
1195 }
1196
b4f1578f 1197 if (!(name = dm_tree_node_get_name(child))) {
3e8c6b73
AK
1198 stack;
1199 continue;
1200 }
1201
b4f1578f 1202 if (!(uuid = dm_tree_node_get_uuid(child))) {
3e8c6b73
AK
1203 stack;
1204 continue;
1205 }
1206
1207 /* Ignore if it doesn't belong to this VG */
2b69db1f 1208 if (!_uuid_prefix_matches(uuid, uuid_prefix, uuid_prefix_len))
3e8c6b73 1209 continue;
3e8c6b73
AK
1210
1211 /* Refresh open_count */
db208f51 1212 if (!_info_by_dev(dinfo->major, dinfo->minor, 1, &info) ||
f55021f4 1213 !info.exists)
3e8c6b73
AK
1214 continue;
1215
125712be
PR
1216 if (!_check_device_not_in_use(&info))
1217 continue;
1218
f3ef15ef 1219 /* Also checking open_count in parent nodes of presuspend_node */
125712be 1220 if ((child->presuspend_node &&
f3ef15ef
ZK
1221 !_node_has_closed_parents(child->presuspend_node,
1222 uuid_prefix, uuid_prefix_len))) {
18e0f934
AK
1223 /* Only report error from (likely non-internal) dependency at top level */
1224 if (!level) {
1225 log_error("Unable to deactivate open %s (%" PRIu32
1226 ":%" PRIu32 ")", name, info.major,
1227 info.minor);
1228 r = 0;
1229 }
f55021f4
AK
1230 continue;
1231 }
1232
76d1aec8
ZK
1233 /* Suspend child node first if requested */
1234 if (child->presuspend_node &&
1235 !dm_tree_suspend_children(child, uuid_prefix, uuid_prefix_len))
1236 continue;
1237
f16aea9e 1238 if (!_deactivate_node(name, info.major, info.minor,
787200ef
PR
1239 &child->dtree->cookie, child->udev_flags,
1240 child->dtree->retry_remove)) {
3e8c6b73
AK
1241 log_error("Unable to deactivate %s (%" PRIu32
1242 ":%" PRIu32 ")", name, info.major,
1243 info.minor);
b7eb2ad0 1244 r = 0;
3e8c6b73 1245 continue;
f4249251
AK
1246 } else if (info.suspended)
1247 dec_suspended();
3e8c6b73 1248
18e0f934
AK
1249 if (dm_tree_node_num_children(child, 0)) {
1250 if (!_dm_tree_deactivate_children(child, uuid_prefix, uuid_prefix_len, level + 1))
b7eb2ad0 1251 return_0;
18e0f934 1252 }
3e8c6b73
AK
1253 }
1254
b7eb2ad0 1255 return r;
3e8c6b73 1256}
db208f51 1257
18e0f934
AK
1258int dm_tree_deactivate_children(struct dm_tree_node *dnode,
1259 const char *uuid_prefix,
1260 size_t uuid_prefix_len)
1261{
1262 return _dm_tree_deactivate_children(dnode, uuid_prefix, uuid_prefix_len, 0);
1263}
1264
c55b1410
AK
1265void dm_tree_skip_lockfs(struct dm_tree_node *dnode)
1266{
1267 dnode->dtree->skip_lockfs = 1;
1268}
1269
b9ffd32c
AK
1270void dm_tree_use_no_flush_suspend(struct dm_tree_node *dnode)
1271{
1272 dnode->dtree->no_flush = 1;
1273}
1274
787200ef
PR
1275void dm_tree_retry_remove(struct dm_tree_node *dnode)
1276{
1277 dnode->dtree->retry_remove = 1;
1278}
1279
b4f1578f 1280int dm_tree_suspend_children(struct dm_tree_node *dnode,
08e64ce5
ZK
1281 const char *uuid_prefix,
1282 size_t uuid_prefix_len)
db208f51 1283{
68085c93 1284 int r = 1;
db208f51 1285 void *handle = NULL;
b4f1578f 1286 struct dm_tree_node *child = dnode;
db208f51
AK
1287 struct dm_info info, newinfo;
1288 const struct dm_info *dinfo;
1289 const char *name;
1290 const char *uuid;
1291
690a5da2 1292 /* Suspend nodes at this level of the tree */
b4f1578f
AK
1293 while ((child = dm_tree_next_child(&handle, dnode, 0))) {
1294 if (!(dinfo = dm_tree_node_get_info(child))) {
db208f51
AK
1295 stack;
1296 continue;
1297 }
1298
b4f1578f 1299 if (!(name = dm_tree_node_get_name(child))) {
db208f51
AK
1300 stack;
1301 continue;
1302 }
1303
b4f1578f 1304 if (!(uuid = dm_tree_node_get_uuid(child))) {
db208f51
AK
1305 stack;
1306 continue;
1307 }
1308
1309 /* Ignore if it doesn't belong to this VG */
2b69db1f 1310 if (!_uuid_prefix_matches(uuid, uuid_prefix, uuid_prefix_len))
db208f51
AK
1311 continue;
1312
690a5da2
AK
1313 /* Ensure immediate parents are already suspended */
1314 if (!_children_suspended(child, 1, uuid_prefix, uuid_prefix_len))
1315 continue;
1316
db208f51 1317 if (!_info_by_dev(dinfo->major, dinfo->minor, 0, &info) ||
b700541f 1318 !info.exists || info.suspended)
db208f51
AK
1319 continue;
1320
c55b1410 1321 if (!_suspend_node(name, info.major, info.minor,
b9ffd32c
AK
1322 child->dtree->skip_lockfs,
1323 child->dtree->no_flush, &newinfo)) {
db208f51
AK
1324 log_error("Unable to suspend %s (%" PRIu32
1325 ":%" PRIu32 ")", name, info.major,
1326 info.minor);
68085c93 1327 r = 0;
db208f51
AK
1328 continue;
1329 }
1330
1331 /* Update cached info */
1332 child->info = newinfo;
690a5da2
AK
1333 }
1334
1335 /* Then suspend any child nodes */
1336 handle = NULL;
1337
b4f1578f
AK
1338 while ((child = dm_tree_next_child(&handle, dnode, 0))) {
1339 if (!(uuid = dm_tree_node_get_uuid(child))) {
690a5da2
AK
1340 stack;
1341 continue;
1342 }
1343
1344 /* Ignore if it doesn't belong to this VG */
87f98002 1345 if (!_uuid_prefix_matches(uuid, uuid_prefix, uuid_prefix_len))
690a5da2 1346 continue;
db208f51 1347
b4f1578f 1348 if (dm_tree_node_num_children(child, 0))
68085c93
MS
1349 if (!dm_tree_suspend_children(child, uuid_prefix, uuid_prefix_len))
1350 return_0;
db208f51
AK
1351 }
1352
68085c93 1353 return r;
db208f51
AK
1354}
1355
b4f1578f 1356int dm_tree_activate_children(struct dm_tree_node *dnode,
db208f51
AK
1357 const char *uuid_prefix,
1358 size_t uuid_prefix_len)
1359{
2ca6b865 1360 int r = 1;
db208f51 1361 void *handle = NULL;
b4f1578f 1362 struct dm_tree_node *child = dnode;
165e4a11
AK
1363 struct dm_info newinfo;
1364 const char *name;
db208f51 1365 const char *uuid;
56c28292 1366 int priority;
db208f51 1367
165e4a11 1368 /* Activate children first */
b4f1578f
AK
1369 while ((child = dm_tree_next_child(&handle, dnode, 0))) {
1370 if (!(uuid = dm_tree_node_get_uuid(child))) {
165e4a11
AK
1371 stack;
1372 continue;
db208f51
AK
1373 }
1374
908db078
AK
1375 if (!_uuid_prefix_matches(uuid, uuid_prefix, uuid_prefix_len))
1376 continue;
db208f51 1377
b4f1578f 1378 if (dm_tree_node_num_children(child, 0))
2ca6b865
MS
1379 if (!dm_tree_activate_children(child, uuid_prefix, uuid_prefix_len))
1380 return_0;
56c28292 1381 }
165e4a11 1382
56c28292 1383 handle = NULL;
165e4a11 1384
aa6f4e51 1385 for (priority = 0; priority < 3; priority++) {
56c28292
AK
1386 while ((child = dm_tree_next_child(&handle, dnode, 0))) {
1387 if (!(uuid = dm_tree_node_get_uuid(child))) {
1388 stack;
1389 continue;
165e4a11 1390 }
165e4a11 1391
56c28292
AK
1392 if (!_uuid_prefix_matches(uuid, uuid_prefix, uuid_prefix_len))
1393 continue;
165e4a11 1394
56c28292
AK
1395 if (priority != child->activation_priority)
1396 continue;
165e4a11 1397
56c28292
AK
1398 if (!(name = dm_tree_node_get_name(child))) {
1399 stack;
1400 continue;
1401 }
1402
1403 /* Rename? */
1404 if (child->props.new_name) {
bd90c6b2 1405 if (!_rename_node(name, child->props.new_name, child->info.major,
f16aea9e
PR
1406 child->info.minor, &child->dtree->cookie,
1407 child->udev_flags)) {
56c28292
AK
1408 log_error("Failed to rename %s (%" PRIu32
1409 ":%" PRIu32 ") to %s", name, child->info.major,
1410 child->info.minor, child->props.new_name);
1411 return 0;
1412 }
1413 child->name = child->props.new_name;
1414 child->props.new_name = NULL;
1415 }
1416
1417 if (!child->info.inactive_table && !child->info.suspended)
1418 continue;
1419
bafa2f39 1420 if (!_resume_node(child->name, child->info.major, child->info.minor,
bd90c6b2 1421 child->props.read_ahead, child->props.read_ahead_flags,
1840aa09 1422 &newinfo, &child->dtree->cookie, child->udev_flags, child->info.suspended)) {
56c28292 1423 log_error("Unable to resume %s (%" PRIu32
bafa2f39 1424 ":%" PRIu32 ")", child->name, child->info.major,
56c28292 1425 child->info.minor);
2ca6b865 1426 r = 0;
56c28292
AK
1427 continue;
1428 }
1429
1430 /* Update cached info */
1431 child->info = newinfo;
1432 }
db208f51
AK
1433 }
1434
165e4a11
AK
1435 handle = NULL;
1436
2ca6b865 1437 return r;
165e4a11
AK
1438}
1439
b4f1578f 1440static int _create_node(struct dm_tree_node *dnode)
165e4a11
AK
1441{
1442 int r = 0;
1443 struct dm_task *dmt;
1444
1445 log_verbose("Creating %s", dnode->name);
1446
1447 if (!(dmt = dm_task_create(DM_DEVICE_CREATE))) {
1448 log_error("Create dm_task creation failed for %s", dnode->name);
1449 return 0;
1450 }
1451
1452 if (!dm_task_set_name(dmt, dnode->name)) {
1453 log_error("Failed to set device name for %s", dnode->name);
1454 goto out;
1455 }
1456
1457 if (!dm_task_set_uuid(dmt, dnode->uuid)) {
1458 log_error("Failed to set uuid for %s", dnode->name);
1459 goto out;
1460 }
1461
1462 if (dnode->props.major &&
1463 (!dm_task_set_major(dmt, dnode->props.major) ||
1464 !dm_task_set_minor(dmt, dnode->props.minor))) {
1465 log_error("Failed to set device number for %s creation.", dnode->name);
1466 goto out;
1467 }
1468
1469 if (dnode->props.read_only && !dm_task_set_ro(dmt)) {
1470 log_error("Failed to set read only flag for %s", dnode->name);
1471 goto out;
1472 }
1473
1474 if (!dm_task_no_open_count(dmt))
1475 log_error("Failed to disable open_count");
1476
1477 if ((r = dm_task_run(dmt)))
1478 r = dm_task_get_info(dmt, &dnode->info);
1479
1480out:
1481 dm_task_destroy(dmt);
1482
1483 return r;
1484}
1485
1486
b4f1578f 1487static int _build_dev_string(char *devbuf, size_t bufsize, struct dm_tree_node *node)
165e4a11
AK
1488{
1489 if (!dm_format_dev(devbuf, bufsize, node->info.major, node->info.minor)) {
40e5fd8b
AK
1490 log_error("Failed to format %s device number for %s as dm "
1491 "target (%u,%u)",
1492 node->name, node->uuid, node->info.major, node->info.minor);
1493 return 0;
165e4a11
AK
1494 }
1495
1496 return 1;
1497}
1498
ffa9b6a5
ZK
1499/* simplify string emiting code */
1500#define EMIT_PARAMS(p, str...)\
7b6c011c
AK
1501do {\
1502 int w;\
1503 if ((w = dm_snprintf(params + p, paramsize - (size_t) p, str)) < 0) {\
1504 stack; /* Out of space */\
1505 return -1;\
1506 }\
1507 p += w;\
1508} while (0)
ffa9b6a5 1509
3c74075f
JEB
1510/*
1511 * _emit_areas_line
1512 *
1513 * Returns: 1 on success, 0 on failure
1514 */
08f1ddea 1515static int _emit_areas_line(struct dm_task *dmt __attribute__((unused)),
4dcaa230
AK
1516 struct load_segment *seg, char *params,
1517 size_t paramsize, int *pos)
165e4a11
AK
1518{
1519 struct seg_area *area;
7d7d93ac 1520 char devbuf[DM_FORMAT_DEV_BUFSIZE];
609faae9 1521 unsigned first_time = 1;
db3c1ac1 1522 const char *logtype, *synctype;
b262f3e1 1523 unsigned log_parm_count;
165e4a11 1524
2c44337b 1525 dm_list_iterate_items(area, &seg->areas) {
b262f3e1
ZK
1526 switch (seg->type) {
1527 case SEG_REPLICATOR_DEV:
6d04311e
JEB
1528 if (!_build_dev_string(devbuf, sizeof(devbuf), area->dev_node))
1529 return_0;
1530
b262f3e1
ZK
1531 EMIT_PARAMS(*pos, " %d 1 %s", area->rsite_index, devbuf);
1532 if (first_time)
1533 EMIT_PARAMS(*pos, " nolog 0");
1534 else {
1535 /* Remote devices */
1536 log_parm_count = (area->flags &
1537 (DM_NOSYNC | DM_FORCESYNC)) ? 2 : 1;
1538
1539 if (!area->slog) {
1540 devbuf[0] = 0; /* Only core log parameters */
1541 logtype = "core";
1542 } else {
1543 devbuf[0] = ' '; /* Extra space before device name */
1544 if (!_build_dev_string(devbuf + 1,
1545 sizeof(devbuf) - 1,
1546 area->slog))
1547 return_0;
1548 logtype = "disk";
1549 log_parm_count++; /* Extra sync log device name parameter */
1550 }
1551
1552 EMIT_PARAMS(*pos, " %s %u%s %" PRIu64, logtype,
1553 log_parm_count, devbuf, area->region_size);
1554
db3c1ac1
AK
1555 synctype = (area->flags & DM_NOSYNC) ?
1556 " nosync" : (area->flags & DM_FORCESYNC) ?
1557 " sync" : NULL;
b262f3e1 1558
db3c1ac1
AK
1559 if (synctype)
1560 EMIT_PARAMS(*pos, "%s", synctype);
b262f3e1
ZK
1561 }
1562 break;
cac52ca4
JEB
1563 case SEG_RAID1:
1564 case SEG_RAID4:
1565 case SEG_RAID5_LA:
1566 case SEG_RAID5_RA:
1567 case SEG_RAID5_LS:
1568 case SEG_RAID5_RS:
1569 case SEG_RAID6_ZR:
1570 case SEG_RAID6_NR:
1571 case SEG_RAID6_NC:
6d04311e
JEB
1572 if (!area->dev_node) {
1573 EMIT_PARAMS(*pos, " -");
1574 break;
1575 }
1576 if (!_build_dev_string(devbuf, sizeof(devbuf), area->dev_node))
1577 return_0;
1578
cac52ca4
JEB
1579 EMIT_PARAMS(*pos, " %s", devbuf);
1580 break;
b262f3e1 1581 default:
6d04311e
JEB
1582 if (!_build_dev_string(devbuf, sizeof(devbuf), area->dev_node))
1583 return_0;
1584
b262f3e1
ZK
1585 EMIT_PARAMS(*pos, "%s%s %" PRIu64, first_time ? "" : " ",
1586 devbuf, area->offset);
1587 }
609faae9
AK
1588
1589 first_time = 0;
165e4a11
AK
1590 }
1591
1592 return 1;
1593}
1594
b262f3e1
ZK
1595static int _replicator_emit_segment_line(const struct load_segment *seg, char *params,
1596 size_t paramsize, int *pos)
1597{
1598 const struct load_segment *rlog_seg;
1599 struct replicator_site *rsite;
1600 char rlogbuf[DM_FORMAT_DEV_BUFSIZE];
1601 unsigned parm_count;
1602
1603 if (!seg->log || !_build_dev_string(rlogbuf, sizeof(rlogbuf), seg->log))
1604 return_0;
1605
1606 rlog_seg = dm_list_item(dm_list_last(&seg->log->props.segs),
1607 struct load_segment);
1608
1609 EMIT_PARAMS(*pos, "%s 4 %s 0 auto %" PRIu64,
1610 seg->rlog_type, rlogbuf, rlog_seg->size);
1611
1612 dm_list_iterate_items(rsite, &seg->rsites) {
1613 parm_count = (rsite->fall_behind_data
1614 || rsite->fall_behind_ios
1615 || rsite->async_timeout) ? 4 : 2;
1616
1617 EMIT_PARAMS(*pos, " blockdev %u %u %s", parm_count, rsite->rsite_index,
1618 (rsite->mode == DM_REPLICATOR_SYNC) ? "synchronous" : "asynchronous");
1619
1620 if (rsite->fall_behind_data)
1621 EMIT_PARAMS(*pos, " data %" PRIu64, rsite->fall_behind_data);
1622 else if (rsite->fall_behind_ios)
1623 EMIT_PARAMS(*pos, " ios %" PRIu32, rsite->fall_behind_ios);
1624 else if (rsite->async_timeout)
1625 EMIT_PARAMS(*pos, " timeout %" PRIu32, rsite->async_timeout);
1626 }
1627
1628 return 1;
1629}
1630
3c74075f 1631/*
3c74075f
JEB
1632 * Returns: 1 on success, 0 on failure
1633 */
beecb1e1
ZK
1634static int _mirror_emit_segment_line(struct dm_task *dmt, struct load_segment *seg,
1635 char *params, size_t paramsize)
165e4a11 1636{
8f26e18c
JEB
1637 int block_on_error = 0;
1638 int handle_errors = 0;
1639 int dm_log_userspace = 0;
1640 struct utsname uts;
dbcb64b8 1641 unsigned log_parm_count;
b39fdcf4 1642 int pos = 0, parts;
7d7d93ac 1643 char logbuf[DM_FORMAT_DEV_BUFSIZE];
dbcb64b8 1644 const char *logtype;
b39fdcf4 1645 unsigned kmaj = 0, kmin = 0, krel = 0;
165e4a11 1646
b39fdcf4
MB
1647 if (uname(&uts) == -1) {
1648 log_error("Cannot read kernel release version.");
1649 return 0;
1650 }
1651
1652 /* Kernels with a major number of 2 always had 3 parts. */
1653 parts = sscanf(uts.release, "%u.%u.%u", &kmaj, &kmin, &krel);
1654 if (parts < 1 || (kmaj < 3 && parts < 3)) {
1655 log_error("Wrong kernel release version %s.", uts.release);
30a65310
ZK
1656 return 0;
1657 }
67b25ed4 1658
8f26e18c
JEB
1659 if ((seg->flags & DM_BLOCK_ON_ERROR)) {
1660 /*
1661 * Originally, block_on_error was an argument to the log
1662 * portion of the mirror CTR table. It was renamed to
1663 * "handle_errors" and now resides in the 'features'
1664 * section of the mirror CTR table (i.e. at the end).
1665 *
1666 * We can identify whether to use "block_on_error" or
1667 * "handle_errors" by the dm-mirror module's version
1668 * number (>= 1.12) or by the kernel version (>= 2.6.22).
1669 */
ba61f848 1670 if (KERNEL_VERSION(kmaj, kmin, krel) >= KERNEL_VERSION(2, 6, 22))
8f26e18c
JEB
1671 handle_errors = 1;
1672 else
1673 block_on_error = 1;
1674 }
1675
1676 if (seg->clustered) {
1677 /* Cluster mirrors require a UUID */
1678 if (!seg->uuid)
1679 return_0;
1680
1681 /*
1682 * Cluster mirrors used to have their own log
1683 * types. Now they are accessed through the
1684 * userspace log type.
1685 *
1686 * The dm-log-userspace module was added to the
1687 * 2.6.31 kernel.
1688 */
ba61f848 1689 if (KERNEL_VERSION(kmaj, kmin, krel) >= KERNEL_VERSION(2, 6, 31))
8f26e18c
JEB
1690 dm_log_userspace = 1;
1691 }
1692
1693 /* Region size */
1694 log_parm_count = 1;
1695
1696 /* [no]sync, block_on_error etc. */
1697 log_parm_count += hweight32(seg->flags);
311d6d81 1698
8f26e18c
JEB
1699 /* "handle_errors" is a feature arg now */
1700 if (handle_errors)
1701 log_parm_count--;
1702
1703 /* DM_CORELOG does not count in the param list */
1704 if (seg->flags & DM_CORELOG)
1705 log_parm_count--;
1706
1707 if (seg->clustered) {
1708 log_parm_count++; /* For UUID */
1709
1710 if (!dm_log_userspace)
ffa9b6a5 1711 EMIT_PARAMS(pos, "clustered-");
49b95a5e
JEB
1712 else
1713 /* For clustered-* type field inserted later */
1714 log_parm_count++;
8f26e18c 1715 }
dbcb64b8 1716
8f26e18c
JEB
1717 if (!seg->log)
1718 logtype = "core";
1719 else {
1720 logtype = "disk";
1721 log_parm_count++;
1722 if (!_build_dev_string(logbuf, sizeof(logbuf), seg->log))
1723 return_0;
1724 }
dbcb64b8 1725
8f26e18c
JEB
1726 if (dm_log_userspace)
1727 EMIT_PARAMS(pos, "userspace %u %s clustered-%s",
1728 log_parm_count, seg->uuid, logtype);
1729 else
ffa9b6a5 1730 EMIT_PARAMS(pos, "%s %u", logtype, log_parm_count);
dbcb64b8 1731
8f26e18c
JEB
1732 if (seg->log)
1733 EMIT_PARAMS(pos, " %s", logbuf);
1734
1735 EMIT_PARAMS(pos, " %u", seg->region_size);
dbcb64b8 1736
8f26e18c
JEB
1737 if (seg->clustered && !dm_log_userspace)
1738 EMIT_PARAMS(pos, " %s", seg->uuid);
67b25ed4 1739
8f26e18c
JEB
1740 if ((seg->flags & DM_NOSYNC))
1741 EMIT_PARAMS(pos, " nosync");
1742 else if ((seg->flags & DM_FORCESYNC))
1743 EMIT_PARAMS(pos, " sync");
dbcb64b8 1744
8f26e18c
JEB
1745 if (block_on_error)
1746 EMIT_PARAMS(pos, " block_on_error");
1747
1748 EMIT_PARAMS(pos, " %u ", seg->mirror_area_count);
1749
5f3325fc 1750 if (_emit_areas_line(dmt, seg, params, paramsize, &pos) <= 0)
3c74075f 1751 return_0;
dbcb64b8 1752
8f26e18c
JEB
1753 if (handle_errors)
1754 EMIT_PARAMS(pos, " 1 handle_errors");
ffa9b6a5 1755
3c74075f 1756 return 1;
8f26e18c
JEB
1757}
1758
cac52ca4
JEB
1759static int _raid_emit_segment_line(struct dm_task *dmt, uint32_t major,
1760 uint32_t minor, struct load_segment *seg,
1761 uint64_t *seg_start, char *params,
1762 size_t paramsize)
1763{
f439e65b 1764 uint32_t i, *tmp;
cac52ca4
JEB
1765 int param_count = 1; /* mandatory 'chunk size'/'stripe size' arg */
1766 int pos = 0;
1767
1768 if ((seg->flags & DM_NOSYNC) || (seg->flags & DM_FORCESYNC))
1769 param_count++;
1770
1771 if (seg->region_size)
1772 param_count += 2;
1773
f439e65b
JEB
1774 tmp = (uint32_t *)(&seg->rebuilds); /* rebuilds is 64-bit */
1775 param_count += 2 * hweight32(tmp[0]);
1776 param_count += 2 * hweight32(tmp[1]);
1777
cac52ca4
JEB
1778 if ((seg->type == SEG_RAID1) && seg->stripe_size)
1779 log_error("WARNING: Ignoring RAID1 stripe size");
1780
1781 EMIT_PARAMS(pos, "%s %d %u", dm_segtypes[seg->type].target,
1782 param_count, seg->stripe_size);
1783
1784 if (seg->flags & DM_NOSYNC)
1785 EMIT_PARAMS(pos, " nosync");
1786 else if (seg->flags & DM_FORCESYNC)
1787 EMIT_PARAMS(pos, " sync");
1788
1789 if (seg->region_size)
1790 EMIT_PARAMS(pos, " region_size %u", seg->region_size);
1791
f439e65b
JEB
1792 for (i = 0; i < (seg->area_count / 2); i++)
1793 if (seg->rebuilds & (1 << i))
1794 EMIT_PARAMS(pos, " rebuild %u", i);
1795
cac52ca4
JEB
1796 /* Print number of metadata/data device pairs */
1797 EMIT_PARAMS(pos, " %u", seg->area_count/2);
1798
1799 if (_emit_areas_line(dmt, seg, params, paramsize, &pos) <= 0)
1800 return_0;
1801
1802 return 1;
1803}
1804
8f26e18c
JEB
1805static int _emit_segment_line(struct dm_task *dmt, uint32_t major,
1806 uint32_t minor, struct load_segment *seg,
1807 uint64_t *seg_start, char *params,
1808 size_t paramsize)
1809{
1810 int pos = 0;
1811 int r;
cac52ca4 1812 int target_type_is_raid = 0;
8f26e18c 1813 char originbuf[DM_FORMAT_DEV_BUFSIZE], cowbuf[DM_FORMAT_DEV_BUFSIZE];
dbcb64b8 1814
8f26e18c
JEB
1815 switch(seg->type) {
1816 case SEG_ERROR:
1817 case SEG_ZERO:
1818 case SEG_LINEAR:
1819 break;
1820 case SEG_MIRRORED:
1821 /* Mirrors are pretty complicated - now in separate function */
beecb1e1 1822 r = _mirror_emit_segment_line(dmt, seg, params, paramsize);
3c74075f
JEB
1823 if (!r)
1824 return_0;
165e4a11 1825 break;
b262f3e1
ZK
1826 case SEG_REPLICATOR:
1827 if ((r = _replicator_emit_segment_line(seg, params, paramsize,
1828 &pos)) <= 0) {
1829 stack;
1830 return r;
1831 }
1832 break;
1833 case SEG_REPLICATOR_DEV:
1834 if (!seg->replicator || !_build_dev_string(originbuf,
1835 sizeof(originbuf),
1836 seg->replicator))
1837 return_0;
1838
1839 EMIT_PARAMS(pos, "%s %" PRIu64, originbuf, seg->rdevice_index);
1840 break;
165e4a11 1841 case SEG_SNAPSHOT:
aa6f4e51 1842 case SEG_SNAPSHOT_MERGE:
b4f1578f
AK
1843 if (!_build_dev_string(originbuf, sizeof(originbuf), seg->origin))
1844 return_0;
1845 if (!_build_dev_string(cowbuf, sizeof(cowbuf), seg->cow))
1846 return_0;
ffa9b6a5
ZK
1847 EMIT_PARAMS(pos, "%s %s %c %d", originbuf, cowbuf,
1848 seg->persistent ? 'P' : 'N', seg->chunk_size);
165e4a11
AK
1849 break;
1850 case SEG_SNAPSHOT_ORIGIN:
b4f1578f
AK
1851 if (!_build_dev_string(originbuf, sizeof(originbuf), seg->origin))
1852 return_0;
ffa9b6a5 1853 EMIT_PARAMS(pos, "%s", originbuf);
165e4a11
AK
1854 break;
1855 case SEG_STRIPED:
609faae9 1856 EMIT_PARAMS(pos, "%u %u ", seg->area_count, seg->stripe_size);
165e4a11 1857 break;
12ca060e 1858 case SEG_CRYPT:
609faae9 1859 EMIT_PARAMS(pos, "%s%s%s%s%s %s %" PRIu64 " ", seg->cipher,
12ca060e
MB
1860 seg->chainmode ? "-" : "", seg->chainmode ?: "",
1861 seg->iv ? "-" : "", seg->iv ?: "", seg->key,
1862 seg->iv_offset != DM_CRYPT_IV_DEFAULT ?
1863 seg->iv_offset : *seg_start);
1864 break;
cac52ca4
JEB
1865 case SEG_RAID1:
1866 case SEG_RAID4:
1867 case SEG_RAID5_LA:
1868 case SEG_RAID5_RA:
1869 case SEG_RAID5_LS:
1870 case SEG_RAID5_RS:
1871 case SEG_RAID6_ZR:
1872 case SEG_RAID6_NR:
1873 case SEG_RAID6_NC:
1874 target_type_is_raid = 1;
1875 r = _raid_emit_segment_line(dmt, major, minor, seg, seg_start,
1876 params, paramsize);
1877 if (!r)
1878 return_0;
1879
1880 break;
165e4a11
AK
1881 }
1882
1883 switch(seg->type) {
1884 case SEG_ERROR:
b262f3e1 1885 case SEG_REPLICATOR:
165e4a11
AK
1886 case SEG_SNAPSHOT:
1887 case SEG_SNAPSHOT_ORIGIN:
aa6f4e51 1888 case SEG_SNAPSHOT_MERGE:
165e4a11
AK
1889 case SEG_ZERO:
1890 break;
12ca060e 1891 case SEG_CRYPT:
165e4a11 1892 case SEG_LINEAR:
b262f3e1 1893 case SEG_REPLICATOR_DEV:
165e4a11
AK
1894 case SEG_STRIPED:
1895 if ((r = _emit_areas_line(dmt, seg, params, paramsize, &pos)) <= 0) {
1896 stack;
1897 return r;
1898 }
b6793963
AK
1899 if (!params[0]) {
1900 log_error("No parameters supplied for %s target "
1901 "%u:%u.", dm_segtypes[seg->type].target,
812e10ac 1902 major, minor);
b6793963
AK
1903 return 0;
1904 }
165e4a11
AK
1905 break;
1906 }
1907
4b2cae46
AK
1908 log_debug("Adding target to (%" PRIu32 ":%" PRIu32 "): %" PRIu64
1909 " %" PRIu64 " %s %s", major, minor,
f439e65b
JEB
1910 *seg_start, seg->size, target_type_is_raid ? "raid" :
1911 dm_segtypes[seg->type].target, params);
165e4a11 1912
cac52ca4
JEB
1913 if (!dm_task_add_target(dmt, *seg_start, seg->size,
1914 target_type_is_raid ? "raid" :
1915 dm_segtypes[seg->type].target, params))
b4f1578f 1916 return_0;
165e4a11
AK
1917
1918 *seg_start += seg->size;
1919
1920 return 1;
1921}
1922
ffa9b6a5
ZK
1923#undef EMIT_PARAMS
1924
4b2cae46
AK
1925static int _emit_segment(struct dm_task *dmt, uint32_t major, uint32_t minor,
1926 struct load_segment *seg, uint64_t *seg_start)
165e4a11
AK
1927{
1928 char *params;
1929 size_t paramsize = 4096;
1930 int ret;
1931
1932 do {
1933 if (!(params = dm_malloc(paramsize))) {
1934 log_error("Insufficient space for target parameters.");
1935 return 0;
1936 }
1937
12ea7cb1 1938 params[0] = '\0';
4b2cae46
AK
1939 ret = _emit_segment_line(dmt, major, minor, seg, seg_start,
1940 params, paramsize);
165e4a11
AK
1941 dm_free(params);
1942
1943 if (!ret)
1944 stack;
1945
1946 if (ret >= 0)
1947 return ret;
1948
1949 log_debug("Insufficient space in params[%" PRIsize_t
1950 "] for target parameters.", paramsize);
1951
1952 paramsize *= 2;
1953 } while (paramsize < MAX_TARGET_PARAMSIZE);
1954
1955 log_error("Target parameter size too big. Aborting.");
1956 return 0;
1957}
1958
b4f1578f 1959static int _load_node(struct dm_tree_node *dnode)
165e4a11
AK
1960{
1961 int r = 0;
1962 struct dm_task *dmt;
1963 struct load_segment *seg;
df390f17 1964 uint64_t seg_start = 0, existing_table_size;
165e4a11 1965
4b2cae46
AK
1966 log_verbose("Loading %s table (%" PRIu32 ":%" PRIu32 ")", dnode->name,
1967 dnode->info.major, dnode->info.minor);
165e4a11
AK
1968
1969 if (!(dmt = dm_task_create(DM_DEVICE_RELOAD))) {
1970 log_error("Reload dm_task creation failed for %s", dnode->name);
1971 return 0;
1972 }
1973
1974 if (!dm_task_set_major(dmt, dnode->info.major) ||
1975 !dm_task_set_minor(dmt, dnode->info.minor)) {
1976 log_error("Failed to set device number for %s reload.", dnode->name);
1977 goto out;
1978 }
1979
1980 if (dnode->props.read_only && !dm_task_set_ro(dmt)) {
1981 log_error("Failed to set read only flag for %s", dnode->name);
1982 goto out;
1983 }
1984
1985 if (!dm_task_no_open_count(dmt))
1986 log_error("Failed to disable open_count");
1987
2c44337b 1988 dm_list_iterate_items(seg, &dnode->props.segs)
4b2cae46
AK
1989 if (!_emit_segment(dmt, dnode->info.major, dnode->info.minor,
1990 seg, &seg_start))
b4f1578f 1991 goto_out;
165e4a11 1992
ec289b64
AK
1993 if (!dm_task_suppress_identical_reload(dmt))
1994 log_error("Failed to suppress reload of identical tables.");
1995
1996 if ((r = dm_task_run(dmt))) {
165e4a11 1997 r = dm_task_get_info(dmt, &dnode->info);
ec289b64
AK
1998 if (r && !dnode->info.inactive_table)
1999 log_verbose("Suppressed %s identical table reload.",
2000 dnode->name);
bb875bb9 2001
df390f17 2002 existing_table_size = dm_task_get_existing_table_size(dmt);
bb875bb9 2003 if ((dnode->props.size_changed =
df390f17 2004 (existing_table_size == seg_start) ? 0 : 1)) {
bb875bb9 2005 log_debug("Table size changed from %" PRIu64 " to %"
df390f17 2006 PRIu64 " for %s", existing_table_size,
bb875bb9 2007 seg_start, dnode->name);
df390f17
AK
2008 /*
2009 * Kernel usually skips size validation on zero-length devices
2010 * now so no need to preload them.
2011 */
2012 /* FIXME In which kernel version did this begin? */
2013 if (!existing_table_size && dnode->props.delay_resume_if_new)
2014 dnode->props.size_changed = 0;
2015 }
ec289b64 2016 }
165e4a11
AK
2017
2018 dnode->props.segment_count = 0;
2019
2020out:
2021 dm_task_destroy(dmt);
2022
2023 return r;
165e4a11
AK
2024}
2025
b4f1578f 2026int dm_tree_preload_children(struct dm_tree_node *dnode,
bb875bb9
AK
2027 const char *uuid_prefix,
2028 size_t uuid_prefix_len)
165e4a11 2029{
2ca6b865 2030 int r = 1;
165e4a11 2031 void *handle = NULL;
b4f1578f 2032 struct dm_tree_node *child;
165e4a11 2033 struct dm_info newinfo;
566515c0 2034 int update_devs_flag = 0;
165e4a11
AK
2035
2036 /* Preload children first */
b4f1578f 2037 while ((child = dm_tree_next_child(&handle, dnode, 0))) {
165e4a11
AK
2038 /* Skip existing non-device-mapper devices */
2039 if (!child->info.exists && child->info.major)
2040 continue;
2041
2042 /* Ignore if it doesn't belong to this VG */
87f98002
AK
2043 if (child->info.exists &&
2044 !_uuid_prefix_matches(child->uuid, uuid_prefix, uuid_prefix_len))
165e4a11
AK
2045 continue;
2046
b4f1578f 2047 if (dm_tree_node_num_children(child, 0))
2ca6b865
MS
2048 if (!dm_tree_preload_children(child, uuid_prefix, uuid_prefix_len))
2049 return_0;
165e4a11 2050
165e4a11
AK
2051 /* FIXME Cope if name exists with no uuid? */
2052 if (!child->info.exists) {
2053 if (!_create_node(child)) {
2054 stack;
2055 return 0;
2056 }
2057 }
2058
2059 if (!child->info.inactive_table && child->props.segment_count) {
2060 if (!_load_node(child)) {
2061 stack;
2062 return 0;
2063 }
2064 }
2065
eb91c4ee
MB
2066 /* Propagate device size change change */
2067 if (child->props.size_changed)
2068 dnode->props.size_changed = 1;
2069
bb875bb9 2070 /* Resume device immediately if it has parents and its size changed */
3776c494 2071 if (!dm_tree_node_num_children(child, 1) || !child->props.size_changed)
165e4a11
AK
2072 continue;
2073
7707ea90
AK
2074 if (!child->info.inactive_table && !child->info.suspended)
2075 continue;
2076
fc795d87 2077 if (!_resume_node(child->name, child->info.major, child->info.minor,
bd90c6b2 2078 child->props.read_ahead, child->props.read_ahead_flags,
1840aa09
AK
2079 &newinfo, &child->dtree->cookie, child->udev_flags,
2080 child->info.suspended)) {
165e4a11 2081 log_error("Unable to resume %s (%" PRIu32
fc795d87 2082 ":%" PRIu32 ")", child->name, child->info.major,
165e4a11 2083 child->info.minor);
2ca6b865 2084 r = 0;
165e4a11
AK
2085 continue;
2086 }
2087
2088 /* Update cached info */
2089 child->info = newinfo;
566515c0
PR
2090
2091 /*
2092 * Prepare for immediate synchronization with udev and flush all stacked
2093 * dev node operations if requested by immediate_dev_node property. But
2094 * finish processing current level in the tree first.
2095 */
2096 if (child->props.immediate_dev_node)
2097 update_devs_flag = 1;
2098
165e4a11
AK
2099 }
2100
2101 handle = NULL;
2102
566515c0
PR
2103 if (update_devs_flag) {
2104 if (!dm_udev_wait(dm_tree_get_cookie(dnode)))
2105 stack;
2106 dm_tree_set_cookie(dnode, 0);
566515c0
PR
2107 }
2108
2ca6b865 2109 return r;
165e4a11
AK
2110}
2111
165e4a11
AK
2112/*
2113 * Returns 1 if unsure.
2114 */
b4f1578f 2115int dm_tree_children_use_uuid(struct dm_tree_node *dnode,
165e4a11
AK
2116 const char *uuid_prefix,
2117 size_t uuid_prefix_len)
2118{
2119 void *handle = NULL;
b4f1578f 2120 struct dm_tree_node *child = dnode;
165e4a11
AK
2121 const char *uuid;
2122
b4f1578f
AK
2123 while ((child = dm_tree_next_child(&handle, dnode, 0))) {
2124 if (!(uuid = dm_tree_node_get_uuid(child))) {
2125 log_error("Failed to get uuid for dtree node.");
165e4a11
AK
2126 return 1;
2127 }
2128
87f98002 2129 if (_uuid_prefix_matches(uuid, uuid_prefix, uuid_prefix_len))
165e4a11
AK
2130 return 1;
2131
b4f1578f
AK
2132 if (dm_tree_node_num_children(child, 0))
2133 dm_tree_children_use_uuid(child, uuid_prefix, uuid_prefix_len);
165e4a11
AK
2134 }
2135
2136 return 0;
2137}
2138
2139/*
2140 * Target functions
2141 */
b4f1578f 2142static struct load_segment *_add_segment(struct dm_tree_node *dnode, unsigned type, uint64_t size)
165e4a11
AK
2143{
2144 struct load_segment *seg;
2145
b4f1578f
AK
2146 if (!(seg = dm_pool_zalloc(dnode->dtree->mem, sizeof(*seg)))) {
2147 log_error("dtree node segment allocation failed");
165e4a11
AK
2148 return NULL;
2149 }
2150
2151 seg->type = type;
2152 seg->size = size;
2153 seg->area_count = 0;
2c44337b 2154 dm_list_init(&seg->areas);
165e4a11
AK
2155 seg->stripe_size = 0;
2156 seg->persistent = 0;
2157 seg->chunk_size = 0;
2158 seg->cow = NULL;
2159 seg->origin = NULL;
aa6f4e51 2160 seg->merge = NULL;
165e4a11 2161
2c44337b 2162 dm_list_add(&dnode->props.segs, &seg->list);
165e4a11
AK
2163 dnode->props.segment_count++;
2164
2165 return seg;
2166}
2167
b4f1578f 2168int dm_tree_node_add_snapshot_origin_target(struct dm_tree_node *dnode,
40e5fd8b
AK
2169 uint64_t size,
2170 const char *origin_uuid)
165e4a11
AK
2171{
2172 struct load_segment *seg;
b4f1578f 2173 struct dm_tree_node *origin_node;
165e4a11 2174
b4f1578f
AK
2175 if (!(seg = _add_segment(dnode, SEG_SNAPSHOT_ORIGIN, size)))
2176 return_0;
165e4a11 2177
b4f1578f 2178 if (!(origin_node = dm_tree_find_node_by_uuid(dnode->dtree, origin_uuid))) {
165e4a11
AK
2179 log_error("Couldn't find snapshot origin uuid %s.", origin_uuid);
2180 return 0;
2181 }
2182
2183 seg->origin = origin_node;
b4f1578f
AK
2184 if (!_link_tree_nodes(dnode, origin_node))
2185 return_0;
165e4a11 2186
56c28292
AK
2187 /* Resume snapshot origins after new snapshots */
2188 dnode->activation_priority = 1;
2189
165e4a11
AK
2190 return 1;
2191}
2192
aa6f4e51
MS
2193static int _add_snapshot_target(struct dm_tree_node *node,
2194 uint64_t size,
2195 const char *origin_uuid,
2196 const char *cow_uuid,
2197 const char *merge_uuid,
2198 int persistent,
2199 uint32_t chunk_size)
165e4a11
AK
2200{
2201 struct load_segment *seg;
aa6f4e51
MS
2202 struct dm_tree_node *origin_node, *cow_node, *merge_node;
2203 unsigned seg_type;
2204
2205 seg_type = !merge_uuid ? SEG_SNAPSHOT : SEG_SNAPSHOT_MERGE;
165e4a11 2206
aa6f4e51 2207 if (!(seg = _add_segment(node, seg_type, size)))
b4f1578f 2208 return_0;
165e4a11 2209
b4f1578f 2210 if (!(origin_node = dm_tree_find_node_by_uuid(node->dtree, origin_uuid))) {
165e4a11
AK
2211 log_error("Couldn't find snapshot origin uuid %s.", origin_uuid);
2212 return 0;
2213 }
2214
2215 seg->origin = origin_node;
b4f1578f
AK
2216 if (!_link_tree_nodes(node, origin_node))
2217 return_0;
165e4a11 2218
b4f1578f 2219 if (!(cow_node = dm_tree_find_node_by_uuid(node->dtree, cow_uuid))) {
aa6f4e51 2220 log_error("Couldn't find snapshot COW device uuid %s.", cow_uuid);
165e4a11
AK
2221 return 0;
2222 }
2223
2224 seg->cow = cow_node;
b4f1578f
AK
2225 if (!_link_tree_nodes(node, cow_node))
2226 return_0;
165e4a11
AK
2227
2228 seg->persistent = persistent ? 1 : 0;
2229 seg->chunk_size = chunk_size;
2230
aa6f4e51
MS
2231 if (merge_uuid) {
2232 if (!(merge_node = dm_tree_find_node_by_uuid(node->dtree, merge_uuid))) {
2233 /* not a pure error, merging snapshot may have been deactivated */
2234 log_verbose("Couldn't find merging snapshot uuid %s.", merge_uuid);
2235 } else {
2236 seg->merge = merge_node;
2237 /* must not link merging snapshot, would undermine activation_priority below */
2238 }
2239
2240 /* Resume snapshot-merge (acting origin) after other snapshots */
2241 node->activation_priority = 1;
2242 if (seg->merge) {
2243 /* Resume merging snapshot after snapshot-merge */
2244 seg->merge->activation_priority = 2;
2245 }
2246 }
2247
165e4a11
AK
2248 return 1;
2249}
2250
aa6f4e51
MS
2251
2252int dm_tree_node_add_snapshot_target(struct dm_tree_node *node,
2253 uint64_t size,
2254 const char *origin_uuid,
2255 const char *cow_uuid,
2256 int persistent,
2257 uint32_t chunk_size)
2258{
2259 return _add_snapshot_target(node, size, origin_uuid, cow_uuid,
2260 NULL, persistent, chunk_size);
2261}
2262
2263int dm_tree_node_add_snapshot_merge_target(struct dm_tree_node *node,
2264 uint64_t size,
2265 const char *origin_uuid,
2266 const char *cow_uuid,
2267 const char *merge_uuid,
2268 uint32_t chunk_size)
2269{
2270 return _add_snapshot_target(node, size, origin_uuid, cow_uuid,
2271 merge_uuid, 1, chunk_size);
2272}
2273
b4f1578f 2274int dm_tree_node_add_error_target(struct dm_tree_node *node,
40e5fd8b 2275 uint64_t size)
165e4a11 2276{
b4f1578f
AK
2277 if (!_add_segment(node, SEG_ERROR, size))
2278 return_0;
165e4a11
AK
2279
2280 return 1;
2281}
2282
b4f1578f 2283int dm_tree_node_add_zero_target(struct dm_tree_node *node,
40e5fd8b 2284 uint64_t size)
165e4a11 2285{
b4f1578f
AK
2286 if (!_add_segment(node, SEG_ZERO, size))
2287 return_0;
165e4a11
AK
2288
2289 return 1;
2290}
2291
b4f1578f 2292int dm_tree_node_add_linear_target(struct dm_tree_node *node,
40e5fd8b 2293 uint64_t size)
165e4a11 2294{
b4f1578f
AK
2295 if (!_add_segment(node, SEG_LINEAR, size))
2296 return_0;
165e4a11
AK
2297
2298 return 1;
2299}
2300
b4f1578f 2301int dm_tree_node_add_striped_target(struct dm_tree_node *node,
40e5fd8b
AK
2302 uint64_t size,
2303 uint32_t stripe_size)
165e4a11
AK
2304{
2305 struct load_segment *seg;
2306
b4f1578f
AK
2307 if (!(seg = _add_segment(node, SEG_STRIPED, size)))
2308 return_0;
165e4a11
AK
2309
2310 seg->stripe_size = stripe_size;
2311
2312 return 1;
2313}
2314
12ca060e
MB
2315int dm_tree_node_add_crypt_target(struct dm_tree_node *node,
2316 uint64_t size,
2317 const char *cipher,
2318 const char *chainmode,
2319 const char *iv,
2320 uint64_t iv_offset,
2321 const char *key)
2322{
2323 struct load_segment *seg;
2324
2325 if (!(seg = _add_segment(node, SEG_CRYPT, size)))
2326 return_0;
2327
2328 seg->cipher = cipher;
2329 seg->chainmode = chainmode;
2330 seg->iv = iv;
2331 seg->iv_offset = iv_offset;
2332 seg->key = key;
2333
2334 return 1;
2335}
2336
b4f1578f 2337int dm_tree_node_add_mirror_target_log(struct dm_tree_node *node,
165e4a11 2338 uint32_t region_size,
08e64ce5 2339 unsigned clustered,
165e4a11 2340 const char *log_uuid,
ce7ed2c0
AK
2341 unsigned area_count,
2342 uint32_t flags)
165e4a11 2343{
908db078 2344 struct dm_tree_node *log_node = NULL;
165e4a11
AK
2345 struct load_segment *seg;
2346
2347 if (!node->props.segment_count) {
b8175c33 2348 log_error(INTERNAL_ERROR "Attempt to add target area to missing segment.");
165e4a11
AK
2349 return 0;
2350 }
2351
2c44337b 2352 seg = dm_list_item(dm_list_last(&node->props.segs), struct load_segment);
165e4a11 2353
24b026e3 2354 if (log_uuid) {
67b25ed4
AK
2355 if (!(seg->uuid = dm_pool_strdup(node->dtree->mem, log_uuid))) {
2356 log_error("log uuid pool_strdup failed");
2357 return 0;
2358 }
df390f17
AK
2359 if ((flags & DM_CORELOG))
2360 /* For pvmove: immediate resume (for size validation) isn't needed. */
2361 node->props.delay_resume_if_new = 1;
2362 else {
9723090c
AK
2363 if (!(log_node = dm_tree_find_node_by_uuid(node->dtree, log_uuid))) {
2364 log_error("Couldn't find mirror log uuid %s.", log_uuid);
2365 return 0;
2366 }
2367
566515c0
PR
2368 if (clustered)
2369 log_node->props.immediate_dev_node = 1;
2370
0a99713e
AK
2371 /* The kernel validates the size of disk logs. */
2372 /* FIXME Propagate to any devices below */
2373 log_node->props.delay_resume_if_new = 0;
2374
9723090c
AK
2375 if (!_link_tree_nodes(node, log_node))
2376 return_0;
2377 }
165e4a11
AK
2378 }
2379
2380 seg->log = log_node;
165e4a11
AK
2381 seg->region_size = region_size;
2382 seg->clustered = clustered;
2383 seg->mirror_area_count = area_count;
dbcb64b8 2384 seg->flags = flags;
165e4a11
AK
2385
2386 return 1;
2387}
2388
b4f1578f 2389int dm_tree_node_add_mirror_target(struct dm_tree_node *node,
40e5fd8b 2390 uint64_t size)
165e4a11 2391{
cbecd3cd 2392 if (!_add_segment(node, SEG_MIRRORED, size))
b4f1578f 2393 return_0;
165e4a11
AK
2394
2395 return 1;
2396}
2397
cac52ca4
JEB
2398int dm_tree_node_add_raid_target(struct dm_tree_node *node,
2399 uint64_t size,
2400 const char *raid_type,
2401 uint32_t region_size,
2402 uint32_t stripe_size,
f439e65b 2403 uint64_t rebuilds,
cac52ca4
JEB
2404 uint64_t reserved2)
2405{
2406 int i;
2407 struct load_segment *seg = NULL;
2408
2409 for (i = 0; dm_segtypes[i].target && !seg; i++)
2410 if (!strcmp(raid_type, dm_segtypes[i].target))
2411 if (!(seg = _add_segment(node,
2412 dm_segtypes[i].type, size)))
2413 return_0;
2414
b2fa9b43
JEB
2415 if (!seg)
2416 return_0;
2417
cac52ca4
JEB
2418 seg->region_size = region_size;
2419 seg->stripe_size = stripe_size;
2420 seg->area_count = 0;
f439e65b 2421 seg->rebuilds = rebuilds;
cac52ca4
JEB
2422
2423 return 1;
2424}
2425
b262f3e1
ZK
2426int dm_tree_node_add_replicator_target(struct dm_tree_node *node,
2427 uint64_t size,
2428 const char *rlog_uuid,
2429 const char *rlog_type,
2430 unsigned rsite_index,
2431 dm_replicator_mode_t mode,
2432 uint32_t async_timeout,
2433 uint64_t fall_behind_data,
2434 uint32_t fall_behind_ios)
2435{
2436 struct load_segment *rseg;
2437 struct replicator_site *rsite;
2438
2439 /* Local site0 - adds replicator segment and links rlog device */
2440 if (rsite_index == REPLICATOR_LOCAL_SITE) {
2441 if (node->props.segment_count) {
2442 log_error(INTERNAL_ERROR "Attempt to add replicator segment to already used node.");
2443 return 0;
2444 }
2445
2446 if (!(rseg = _add_segment(node, SEG_REPLICATOR, size)))
2447 return_0;
2448
2449 if (!(rseg->log = dm_tree_find_node_by_uuid(node->dtree, rlog_uuid))) {
2450 log_error("Missing replicator log uuid %s.", rlog_uuid);
2451 return 0;
2452 }
2453
2454 if (!_link_tree_nodes(node, rseg->log))
2455 return_0;
2456
2457 if (strcmp(rlog_type, "ringbuffer") != 0) {
2458 log_error("Unsupported replicator log type %s.", rlog_type);
2459 return 0;
2460 }
2461
2462 if (!(rseg->rlog_type = dm_pool_strdup(node->dtree->mem, rlog_type)))
2463 return_0;
2464
2465 dm_list_init(&rseg->rsites);
2466 rseg->rdevice_count = 0;
2467 node->activation_priority = 1;
2468 }
2469
2470 /* Add site to segment */
2471 if (mode == DM_REPLICATOR_SYNC
2472 && (async_timeout || fall_behind_ios || fall_behind_data)) {
2473 log_error("Async parameters passed for synchronnous replicator.");
2474 return 0;
2475 }
2476
2477 if (node->props.segment_count != 1) {
2478 log_error(INTERNAL_ERROR "Attempt to add remote site area before setting replicator log.");
2479 return 0;
2480 }
2481
2482 rseg = dm_list_item(dm_list_last(&node->props.segs), struct load_segment);
2483 if (rseg->type != SEG_REPLICATOR) {
2484 log_error(INTERNAL_ERROR "Attempt to use non replicator segment %s.",
2485 dm_segtypes[rseg->type].target);
2486 return 0;
2487 }
2488
2489 if (!(rsite = dm_pool_zalloc(node->dtree->mem, sizeof(*rsite)))) {
2490 log_error("Failed to allocate remote site segment.");
2491 return 0;
2492 }
2493
2494 dm_list_add(&rseg->rsites, &rsite->list);
2495 rseg->rsite_count++;
2496
2497 rsite->mode = mode;
2498 rsite->async_timeout = async_timeout;
2499 rsite->fall_behind_data = fall_behind_data;
2500 rsite->fall_behind_ios = fall_behind_ios;
2501 rsite->rsite_index = rsite_index;
2502
2503 return 1;
2504}
2505
2506/* Appends device node to Replicator */
2507int dm_tree_node_add_replicator_dev_target(struct dm_tree_node *node,
2508 uint64_t size,
2509 const char *replicator_uuid,
2510 uint64_t rdevice_index,
2511 const char *rdev_uuid,
2512 unsigned rsite_index,
2513 const char *slog_uuid,
2514 uint32_t slog_flags,
2515 uint32_t slog_region_size)
2516{
2517 struct seg_area *area;
2518 struct load_segment *rseg;
2519 struct load_segment *rep_seg;
2520
2521 if (rsite_index == REPLICATOR_LOCAL_SITE) {
2522 /* Site index for local target */
2523 if (!(rseg = _add_segment(node, SEG_REPLICATOR_DEV, size)))
2524 return_0;
2525
2526 if (!(rseg->replicator = dm_tree_find_node_by_uuid(node->dtree, replicator_uuid))) {
2527 log_error("Missing replicator uuid %s.", replicator_uuid);
2528 return 0;
2529 }
2530
2531 /* Local slink0 for replicator must be always initialized first */
2532 if (rseg->replicator->props.segment_count != 1) {
2533 log_error(INTERNAL_ERROR "Attempt to use non replicator segment.");
2534 return 0;
2535 }
2536
2537 rep_seg = dm_list_item(dm_list_last(&rseg->replicator->props.segs), struct load_segment);
2538 if (rep_seg->type != SEG_REPLICATOR) {
2539 log_error(INTERNAL_ERROR "Attempt to use non replicator segment %s.",
2540 dm_segtypes[rep_seg->type].target);
2541 return 0;
2542 }
2543 rep_seg->rdevice_count++;
2544
2545 if (!_link_tree_nodes(node, rseg->replicator))
2546 return_0;
2547
2548 rseg->rdevice_index = rdevice_index;
2549 } else {
2550 /* Local slink0 for replicator must be always initialized first */
2551 if (node->props.segment_count != 1) {
2552 log_error(INTERNAL_ERROR "Attempt to use non replicator-dev segment.");
2553 return 0;
2554 }
2555
2556 rseg = dm_list_item(dm_list_last(&node->props.segs), struct load_segment);
2557 if (rseg->type != SEG_REPLICATOR_DEV) {
2558 log_error(INTERNAL_ERROR "Attempt to use non replicator-dev segment %s.",
2559 dm_segtypes[rseg->type].target);
2560 return 0;
2561 }
2562 }
2563
2564 if (!(slog_flags & DM_CORELOG) && !slog_uuid) {
2565 log_error("Unspecified sync log uuid.");
2566 return 0;
2567 }
2568
2569 if (!dm_tree_node_add_target_area(node, NULL, rdev_uuid, 0))
2570 return_0;
2571
2572 area = dm_list_item(dm_list_last(&rseg->areas), struct seg_area);
2573
2574 if (!(slog_flags & DM_CORELOG)) {
2575 if (!(area->slog = dm_tree_find_node_by_uuid(node->dtree, slog_uuid))) {
2576 log_error("Couldn't find sync log uuid %s.", slog_uuid);
2577 return 0;
2578 }
2579
2580 if (!_link_tree_nodes(node, area->slog))
2581 return_0;
2582 }
2583
2584 area->flags = slog_flags;
2585 area->region_size = slog_region_size;
2586 area->rsite_index = rsite_index;
2587
2588 return 1;
2589}
2590
b4f1578f 2591static int _add_area(struct dm_tree_node *node, struct load_segment *seg, struct dm_tree_node *dev_node, uint64_t offset)
165e4a11
AK
2592{
2593 struct seg_area *area;
2594
b4f1578f 2595 if (!(area = dm_pool_zalloc(node->dtree->mem, sizeof (*area)))) {
165e4a11
AK
2596 log_error("Failed to allocate target segment area.");
2597 return 0;
2598 }
2599
2600 area->dev_node = dev_node;
2601 area->offset = offset;
2602
2c44337b 2603 dm_list_add(&seg->areas, &area->list);
165e4a11
AK
2604 seg->area_count++;
2605
2606 return 1;
2607}
2608
b4f1578f 2609int dm_tree_node_add_target_area(struct dm_tree_node *node,
40e5fd8b
AK
2610 const char *dev_name,
2611 const char *uuid,
2612 uint64_t offset)
165e4a11
AK
2613{
2614 struct load_segment *seg;
2615 struct stat info;
b4f1578f 2616 struct dm_tree_node *dev_node;
165e4a11
AK
2617
2618 if ((!dev_name || !*dev_name) && (!uuid || !*uuid)) {
b4f1578f 2619 log_error("dm_tree_node_add_target_area called without device");
165e4a11
AK
2620 return 0;
2621 }
2622
2623 if (uuid) {
b4f1578f 2624 if (!(dev_node = dm_tree_find_node_by_uuid(node->dtree, uuid))) {
165e4a11
AK
2625 log_error("Couldn't find area uuid %s.", uuid);
2626 return 0;
2627 }
b4f1578f
AK
2628 if (!_link_tree_nodes(node, dev_node))
2629 return_0;
165e4a11 2630 } else {
6d04311e 2631 if (stat(dev_name, &info) < 0) {
165e4a11
AK
2632 log_error("Device %s not found.", dev_name);
2633 return 0;
2634 }
2635
40e5fd8b 2636 if (!S_ISBLK(info.st_mode)) {
165e4a11
AK
2637 log_error("Device %s is not a block device.", dev_name);
2638 return 0;
2639 }
2640
2641 /* FIXME Check correct macro use */
cda69e17
PR
2642 if (!(dev_node = _add_dev(node->dtree, node, MAJOR(info.st_rdev),
2643 MINOR(info.st_rdev), 0)))
b4f1578f 2644 return_0;
165e4a11
AK
2645 }
2646
2647 if (!node->props.segment_count) {
b8175c33 2648 log_error(INTERNAL_ERROR "Attempt to add target area to missing segment.");
165e4a11
AK
2649 return 0;
2650 }
2651
2c44337b 2652 seg = dm_list_item(dm_list_last(&node->props.segs), struct load_segment);
165e4a11 2653
b4f1578f
AK
2654 if (!_add_area(node, seg, dev_node, offset))
2655 return_0;
165e4a11
AK
2656
2657 return 1;
db208f51 2658}
bd90c6b2 2659
6d04311e
JEB
2660int dm_tree_node_add_null_area(struct dm_tree_node *node, uint64_t offset)
2661{
2662 struct load_segment *seg;
2663
2664 seg = dm_list_item(dm_list_last(&node->props.segs), struct load_segment);
2665
415c0690
AK
2666 switch (seg->type) {
2667 case SEG_RAID1:
2668 case SEG_RAID4:
2669 case SEG_RAID5_LA:
2670 case SEG_RAID5_RA:
2671 case SEG_RAID5_LS:
2672 case SEG_RAID5_RS:
2673 case SEG_RAID6_ZR:
2674 case SEG_RAID6_NR:
2675 case SEG_RAID6_NC:
2676 break;
2677 default:
2678 log_error("dm_tree_node_add_null_area() called on an unsupported segment type");
2679 return 0;
2680 }
2681
6d04311e
JEB
2682 if (!_add_area(node, seg, NULL, offset))
2683 return_0;
2684
2685 return 1;
2686}
2687
bd90c6b2
AK
2688void dm_tree_set_cookie(struct dm_tree_node *node, uint32_t cookie)
2689{
2690 node->dtree->cookie = cookie;
2691}
2692
2693uint32_t dm_tree_get_cookie(struct dm_tree_node *node)
2694{
2695 return node->dtree->cookie;
2696}
This page took 0.393709 seconds and 5 git commands to generate.