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