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