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