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