]>
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" | |
18 | #include "list.h" | |
19 | #include "kdev_t.h" | |
0782ad50 | 20 | #include "dm-ioctl.h" |
3d0480ed AK |
21 | |
22 | #include <stdarg.h> | |
23 | #include <sys/param.h> | |
24 | ||
165e4a11 AK |
25 | #define MAX_TARGET_PARAMSIZE 500000 |
26 | ||
87f98002 AK |
27 | /* FIXME Fix interface so this is used only by LVM */ |
28 | #define UUID_PREFIX "LVM-" | |
29 | ||
165e4a11 AK |
30 | /* Supported segment types */ |
31 | enum { | |
32 | SEG_ERROR, | |
33 | SEG_LINEAR, | |
34 | SEG_MIRRORED, | |
35 | SEG_SNAPSHOT, | |
36 | SEG_SNAPSHOT_ORIGIN, | |
37 | SEG_STRIPED, | |
38 | SEG_ZERO, | |
39 | }; | |
b4f1578f | 40 | |
165e4a11 AK |
41 | /* FIXME Add crypt and multipath support */ |
42 | ||
43 | struct { | |
44 | unsigned type; | |
45 | const char *target; | |
46 | } dm_segtypes[] = { | |
47 | { SEG_ERROR, "error" }, | |
48 | { SEG_LINEAR, "linear" }, | |
49 | { SEG_MIRRORED, "mirror" }, | |
50 | { SEG_SNAPSHOT, "snapshot" }, | |
51 | { SEG_SNAPSHOT_ORIGIN, "snapshot-origin" }, | |
52 | { SEG_STRIPED, "striped" }, | |
53 | { SEG_ZERO, "zero"}, | |
54 | }; | |
55 | ||
56 | /* Some segment types have a list of areas of other devices attached */ | |
57 | struct seg_area { | |
58 | struct list list; | |
59 | ||
b4f1578f | 60 | struct dm_tree_node *dev_node; |
165e4a11 AK |
61 | |
62 | uint64_t offset; | |
63 | }; | |
64 | ||
65 | /* Per-segment properties */ | |
66 | struct load_segment { | |
67 | struct list list; | |
68 | ||
69 | unsigned type; | |
70 | ||
71 | uint64_t size; | |
72 | ||
73 | unsigned area_count; /* Linear + Striped + Mirrored */ | |
74 | struct list areas; /* Linear + Striped + Mirrored */ | |
75 | ||
76 | uint32_t stripe_size; /* Striped */ | |
77 | ||
78 | int persistent; /* Snapshot */ | |
79 | uint32_t chunk_size; /* Snapshot */ | |
b4f1578f AK |
80 | struct dm_tree_node *cow; /* Snapshot */ |
81 | struct dm_tree_node *origin; /* Snapshot + Snapshot origin */ | |
165e4a11 | 82 | |
b4f1578f | 83 | struct dm_tree_node *log; /* Mirror */ |
165e4a11 AK |
84 | uint32_t region_size; /* Mirror */ |
85 | unsigned clustered; /* Mirror */ | |
86 | unsigned mirror_area_count; /* Mirror */ | |
dbcb64b8 | 87 | uint32_t flags; /* Mirror log */ |
67b25ed4 | 88 | char *uuid; /* Clustered mirror log */ |
165e4a11 AK |
89 | }; |
90 | ||
91 | /* Per-device properties */ | |
92 | struct load_properties { | |
93 | int read_only; | |
94 | uint32_t major; | |
95 | uint32_t minor; | |
96 | ||
52b84409 AK |
97 | uint32_t read_ahead; |
98 | uint32_t read_ahead_flags; | |
99 | ||
165e4a11 | 100 | unsigned segment_count; |
bb875bb9 | 101 | unsigned size_changed; |
165e4a11 AK |
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; | |
bb875bb9 | 606 | dnode->props.size_changed = 0; |
165e4a11 AK |
607 | } else if (strcmp(name, dnode->name)) { |
608 | /* Do we need to rename node? */ | |
b4f1578f | 609 | if (!(dnode->props.new_name = dm_pool_strdup(dtree->mem, name))) { |
165e4a11 AK |
610 | log_error("name pool_strdup failed"); |
611 | return 0; | |
612 | } | |
613 | } | |
614 | ||
615 | dnode->props.read_only = read_only ? 1 : 0; | |
52b84409 AK |
616 | dnode->props.read_ahead = DM_READ_AHEAD_AUTO; |
617 | dnode->props.read_ahead_flags = 0; | |
165e4a11 | 618 | |
b4f1578f AK |
619 | if (clear_inactive && !_node_clear_table(dnode)) |
620 | return_NULL; | |
165e4a11 AK |
621 | |
622 | dnode->context = context; | |
623 | ||
624 | return dnode; | |
625 | } | |
626 | ||
52b84409 AK |
627 | void dm_tree_node_set_read_ahead(struct dm_tree_node *dnode, |
628 | uint32_t read_ahead, | |
629 | uint32_t read_ahead_flags) | |
630 | { | |
631 | dnode->props.read_ahead = read_ahead; | |
632 | dnode->props.read_ahead_flags = read_ahead_flags; | |
633 | } | |
634 | ||
b4f1578f | 635 | int dm_tree_add_dev(struct dm_tree *dtree, uint32_t major, uint32_t minor) |
3d0480ed | 636 | { |
b4f1578f | 637 | return _add_dev(dtree, &dtree->root, major, minor) ? 1 : 0; |
3d0480ed AK |
638 | } |
639 | ||
b4f1578f | 640 | const char *dm_tree_node_get_name(struct dm_tree_node *node) |
3d0480ed AK |
641 | { |
642 | return node->info.exists ? node->name : ""; | |
643 | } | |
644 | ||
b4f1578f | 645 | const char *dm_tree_node_get_uuid(struct dm_tree_node *node) |
3d0480ed AK |
646 | { |
647 | return node->info.exists ? node->uuid : ""; | |
648 | } | |
649 | ||
b4f1578f | 650 | const struct dm_info *dm_tree_node_get_info(struct dm_tree_node *node) |
3d0480ed AK |
651 | { |
652 | return &node->info; | |
653 | } | |
654 | ||
b4f1578f | 655 | void *dm_tree_node_get_context(struct dm_tree_node *node) |
165e4a11 AK |
656 | { |
657 | return node->context; | |
658 | } | |
659 | ||
b4f1578f | 660 | int dm_tree_node_num_children(struct dm_tree_node *node, uint32_t inverted) |
3d0480ed AK |
661 | { |
662 | if (inverted) { | |
b4f1578f | 663 | if (_nodes_are_linked(&node->dtree->root, node)) |
3d0480ed AK |
664 | return 0; |
665 | return list_size(&node->used_by); | |
666 | } | |
667 | ||
b4f1578f | 668 | if (_nodes_are_linked(node, &node->dtree->root)) |
3d0480ed AK |
669 | return 0; |
670 | ||
671 | return list_size(&node->uses); | |
672 | } | |
673 | ||
2b69db1f AK |
674 | /* |
675 | * Returns 1 if no prefix supplied | |
676 | */ | |
677 | static int _uuid_prefix_matches(const char *uuid, const char *uuid_prefix, size_t uuid_prefix_len) | |
678 | { | |
679 | if (!uuid_prefix) | |
680 | return 1; | |
681 | ||
682 | if (!strncmp(uuid, uuid_prefix, uuid_prefix_len)) | |
683 | return 1; | |
684 | ||
685 | /* Handle transition: active device uuids might be missing the prefix */ | |
686 | if (uuid_prefix_len <= 4) | |
687 | return 0; | |
688 | ||
87f98002 | 689 | if (!strncmp(uuid, UUID_PREFIX, sizeof(UUID_PREFIX) - 1)) |
872dea04 AK |
690 | return 0; |
691 | ||
87f98002 | 692 | if (strncmp(uuid_prefix, UUID_PREFIX, sizeof(UUID_PREFIX) - 1)) |
2b69db1f AK |
693 | return 0; |
694 | ||
87f98002 | 695 | if (!strncmp(uuid, uuid_prefix + sizeof(UUID_PREFIX) - 1, uuid_prefix_len - (sizeof(UUID_PREFIX) - 1))) |
2b69db1f AK |
696 | return 1; |
697 | ||
698 | return 0; | |
699 | } | |
700 | ||
690a5da2 AK |
701 | /* |
702 | * Returns 1 if no children. | |
703 | */ | |
b4f1578f | 704 | static int _children_suspended(struct dm_tree_node *node, |
690a5da2 AK |
705 | uint32_t inverted, |
706 | const char *uuid_prefix, | |
707 | size_t uuid_prefix_len) | |
708 | { | |
709 | struct list *list; | |
b4f1578f | 710 | struct dm_tree_link *dlink; |
690a5da2 AK |
711 | const struct dm_info *dinfo; |
712 | const char *uuid; | |
713 | ||
714 | if (inverted) { | |
b4f1578f | 715 | if (_nodes_are_linked(&node->dtree->root, node)) |
690a5da2 AK |
716 | return 1; |
717 | list = &node->used_by; | |
718 | } else { | |
b4f1578f | 719 | if (_nodes_are_linked(node, &node->dtree->root)) |
690a5da2 AK |
720 | return 1; |
721 | list = &node->uses; | |
722 | } | |
723 | ||
724 | list_iterate_items(dlink, list) { | |
b4f1578f | 725 | if (!(uuid = dm_tree_node_get_uuid(dlink->node))) { |
690a5da2 AK |
726 | stack; |
727 | continue; | |
728 | } | |
729 | ||
730 | /* Ignore if it doesn't belong to this VG */ | |
2b69db1f | 731 | if (!_uuid_prefix_matches(uuid, uuid_prefix, uuid_prefix_len)) |
690a5da2 AK |
732 | continue; |
733 | ||
b4f1578f AK |
734 | if (!(dinfo = dm_tree_node_get_info(dlink->node))) { |
735 | stack; /* FIXME Is this normal? */ | |
690a5da2 AK |
736 | return 0; |
737 | } | |
738 | ||
739 | if (!dinfo->suspended) | |
740 | return 0; | |
741 | } | |
742 | ||
743 | return 1; | |
744 | } | |
745 | ||
3d0480ed AK |
746 | /* |
747 | * Set major and minor to zero for root of tree. | |
748 | */ | |
b4f1578f | 749 | struct dm_tree_node *dm_tree_find_node(struct dm_tree *dtree, |
3d0480ed AK |
750 | uint32_t major, |
751 | uint32_t minor) | |
752 | { | |
753 | if (!major && !minor) | |
b4f1578f | 754 | return &dtree->root; |
3d0480ed | 755 | |
b4f1578f | 756 | return _find_dm_tree_node(dtree, major, minor); |
3d0480ed AK |
757 | } |
758 | ||
165e4a11 AK |
759 | /* |
760 | * Set uuid to NULL for root of tree. | |
761 | */ | |
b4f1578f | 762 | struct dm_tree_node *dm_tree_find_node_by_uuid(struct dm_tree *dtree, |
165e4a11 AK |
763 | const char *uuid) |
764 | { | |
765 | if (!uuid || !*uuid) | |
b4f1578f | 766 | return &dtree->root; |
165e4a11 | 767 | |
b4f1578f | 768 | return _find_dm_tree_node_by_uuid(dtree, uuid); |
165e4a11 AK |
769 | } |
770 | ||
3d0480ed AK |
771 | /* |
772 | * First time set *handle to NULL. | |
773 | * Set inverted to invert the tree. | |
774 | */ | |
b4f1578f AK |
775 | struct dm_tree_node *dm_tree_next_child(void **handle, |
776 | struct dm_tree_node *parent, | |
3d0480ed AK |
777 | uint32_t inverted) |
778 | { | |
779 | struct list **dlink = (struct list **) handle; | |
780 | struct list *use_list; | |
781 | ||
782 | if (inverted) | |
783 | use_list = &parent->used_by; | |
784 | else | |
785 | use_list = &parent->uses; | |
786 | ||
787 | if (!*dlink) | |
788 | *dlink = list_first(use_list); | |
789 | else | |
790 | *dlink = list_next(use_list, *dlink); | |
791 | ||
b4f1578f | 792 | return (*dlink) ? list_item(*dlink, struct dm_tree_link)->node : NULL; |
3d0480ed AK |
793 | } |
794 | ||
3e8c6b73 | 795 | /* |
a6d97ede | 796 | * Deactivate a device with its dependencies if the uuid prefix matches. |
3e8c6b73 | 797 | */ |
db208f51 AK |
798 | static int _info_by_dev(uint32_t major, uint32_t minor, int with_open_count, |
799 | struct dm_info *info) | |
3e8c6b73 AK |
800 | { |
801 | struct dm_task *dmt; | |
802 | int r; | |
803 | ||
804 | if (!(dmt = dm_task_create(DM_DEVICE_INFO))) { | |
805 | log_error("_info_by_dev: dm_task creation failed"); | |
806 | return 0; | |
807 | } | |
808 | ||
809 | if (!dm_task_set_major(dmt, major) || !dm_task_set_minor(dmt, minor)) { | |
810 | log_error("_info_by_dev: Failed to set device number"); | |
811 | dm_task_destroy(dmt); | |
812 | return 0; | |
813 | } | |
814 | ||
db208f51 AK |
815 | if (!with_open_count && !dm_task_no_open_count(dmt)) |
816 | log_error("Failed to disable open_count"); | |
817 | ||
3e8c6b73 AK |
818 | if ((r = dm_task_run(dmt))) |
819 | r = dm_task_get_info(dmt, info); | |
820 | ||
821 | dm_task_destroy(dmt); | |
822 | ||
823 | return r; | |
824 | } | |
825 | ||
826 | static int _deactivate_node(const char *name, uint32_t major, uint32_t minor) | |
827 | { | |
828 | struct dm_task *dmt; | |
829 | int r; | |
830 | ||
831 | log_verbose("Removing %s (%" PRIu32 ":%" PRIu32 ")", name, major, minor); | |
832 | ||
833 | if (!(dmt = dm_task_create(DM_DEVICE_REMOVE))) { | |
834 | log_error("Deactivation dm_task creation failed for %s", name); | |
835 | return 0; | |
836 | } | |
837 | ||
838 | if (!dm_task_set_major(dmt, major) || !dm_task_set_minor(dmt, minor)) { | |
839 | log_error("Failed to set device number for %s deactivation", name); | |
840 | dm_task_destroy(dmt); | |
841 | return 0; | |
842 | } | |
843 | ||
844 | if (!dm_task_no_open_count(dmt)) | |
845 | log_error("Failed to disable open_count"); | |
846 | ||
847 | r = dm_task_run(dmt); | |
848 | ||
165e4a11 AK |
849 | /* FIXME Until kernel returns actual name so dm-ioctl.c can handle it */ |
850 | rm_dev_node(name); | |
851 | ||
db208f51 AK |
852 | /* FIXME Remove node from tree or mark invalid? */ |
853 | ||
854 | dm_task_destroy(dmt); | |
855 | ||
856 | return r; | |
857 | } | |
858 | ||
165e4a11 AK |
859 | static int _rename_node(const char *old_name, const char *new_name, uint32_t major, uint32_t minor) |
860 | { | |
861 | struct dm_task *dmt; | |
862 | int r = 0; | |
863 | ||
864 | log_verbose("Renaming %s (%" PRIu32 ":%" PRIu32 ") to %s", old_name, major, minor, new_name); | |
865 | ||
866 | if (!(dmt = dm_task_create(DM_DEVICE_RENAME))) { | |
867 | log_error("Rename dm_task creation failed for %s", old_name); | |
868 | return 0; | |
869 | } | |
870 | ||
871 | if (!dm_task_set_name(dmt, old_name)) { | |
872 | log_error("Failed to set name for %s rename.", old_name); | |
873 | goto out; | |
874 | } | |
875 | ||
b4f1578f AK |
876 | if (!dm_task_set_newname(dmt, new_name)) |
877 | goto_out; | |
165e4a11 AK |
878 | |
879 | if (!dm_task_no_open_count(dmt)) | |
880 | log_error("Failed to disable open_count"); | |
881 | ||
882 | r = dm_task_run(dmt); | |
883 | ||
884 | out: | |
885 | dm_task_destroy(dmt); | |
886 | ||
887 | return r; | |
888 | } | |
889 | ||
165e4a11 AK |
890 | /* FIXME Merge with _suspend_node? */ |
891 | static int _resume_node(const char *name, uint32_t major, uint32_t minor, | |
52b84409 | 892 | uint32_t read_ahead, uint32_t read_ahead_flags, |
165e4a11 AK |
893 | struct dm_info *newinfo) |
894 | { | |
895 | struct dm_task *dmt; | |
896 | int r; | |
897 | ||
898 | log_verbose("Resuming %s (%" PRIu32 ":%" PRIu32 ")", name, major, minor); | |
899 | ||
900 | if (!(dmt = dm_task_create(DM_DEVICE_RESUME))) { | |
901 | log_error("Suspend dm_task creation failed for %s", name); | |
902 | return 0; | |
903 | } | |
904 | ||
0b7d16bc AK |
905 | /* FIXME Kernel should fill in name on return instead */ |
906 | if (!dm_task_set_name(dmt, name)) { | |
907 | log_error("Failed to set readahead device name for %s", name); | |
908 | dm_task_destroy(dmt); | |
909 | return 0; | |
910 | } | |
911 | ||
165e4a11 AK |
912 | if (!dm_task_set_major(dmt, major) || !dm_task_set_minor(dmt, minor)) { |
913 | log_error("Failed to set device number for %s resumption.", name); | |
914 | dm_task_destroy(dmt); | |
915 | return 0; | |
916 | } | |
917 | ||
918 | if (!dm_task_no_open_count(dmt)) | |
919 | log_error("Failed to disable open_count"); | |
920 | ||
52b84409 AK |
921 | if (!dm_task_set_read_ahead(dmt, read_ahead, read_ahead_flags)) |
922 | log_error("Failed to set read ahead"); | |
923 | ||
165e4a11 AK |
924 | if ((r = dm_task_run(dmt))) |
925 | r = dm_task_get_info(dmt, newinfo); | |
926 | ||
927 | dm_task_destroy(dmt); | |
928 | ||
929 | return r; | |
930 | } | |
931 | ||
db208f51 | 932 | static int _suspend_node(const char *name, uint32_t major, uint32_t minor, |
b9ffd32c | 933 | int skip_lockfs, int no_flush, struct dm_info *newinfo) |
db208f51 AK |
934 | { |
935 | struct dm_task *dmt; | |
936 | int r; | |
937 | ||
b9ffd32c AK |
938 | log_verbose("Suspending %s (%" PRIu32 ":%" PRIu32 ")%s%s", |
939 | name, major, minor, | |
940 | skip_lockfs ? "" : " with filesystem sync", | |
6e1898a5 | 941 | no_flush ? "" : " with device flush"); |
db208f51 AK |
942 | |
943 | if (!(dmt = dm_task_create(DM_DEVICE_SUSPEND))) { | |
944 | log_error("Suspend dm_task creation failed for %s", name); | |
945 | return 0; | |
946 | } | |
947 | ||
948 | if (!dm_task_set_major(dmt, major) || !dm_task_set_minor(dmt, minor)) { | |
949 | log_error("Failed to set device number for %s suspension.", name); | |
950 | dm_task_destroy(dmt); | |
951 | return 0; | |
952 | } | |
953 | ||
954 | if (!dm_task_no_open_count(dmt)) | |
955 | log_error("Failed to disable open_count"); | |
956 | ||
c55b1410 AK |
957 | if (skip_lockfs && !dm_task_skip_lockfs(dmt)) |
958 | log_error("Failed to set skip_lockfs flag."); | |
959 | ||
b9ffd32c AK |
960 | if (no_flush && !dm_task_no_flush(dmt)) |
961 | log_error("Failed to set no_flush flag."); | |
962 | ||
db208f51 AK |
963 | if ((r = dm_task_run(dmt))) |
964 | r = dm_task_get_info(dmt, newinfo); | |
965 | ||
3e8c6b73 AK |
966 | dm_task_destroy(dmt); |
967 | ||
968 | return r; | |
969 | } | |
970 | ||
b4f1578f | 971 | int dm_tree_deactivate_children(struct dm_tree_node *dnode, |
a6d97ede AK |
972 | const char *uuid_prefix, |
973 | size_t uuid_prefix_len) | |
3e8c6b73 AK |
974 | { |
975 | void *handle = NULL; | |
b4f1578f | 976 | struct dm_tree_node *child = dnode; |
3e8c6b73 AK |
977 | struct dm_info info; |
978 | const struct dm_info *dinfo; | |
979 | const char *name; | |
980 | const char *uuid; | |
981 | ||
b4f1578f AK |
982 | while ((child = dm_tree_next_child(&handle, dnode, 0))) { |
983 | if (!(dinfo = dm_tree_node_get_info(child))) { | |
3e8c6b73 AK |
984 | stack; |
985 | continue; | |
986 | } | |
987 | ||
b4f1578f | 988 | if (!(name = dm_tree_node_get_name(child))) { |
3e8c6b73 AK |
989 | stack; |
990 | continue; | |
991 | } | |
992 | ||
b4f1578f | 993 | if (!(uuid = dm_tree_node_get_uuid(child))) { |
3e8c6b73 AK |
994 | stack; |
995 | continue; | |
996 | } | |
997 | ||
998 | /* Ignore if it doesn't belong to this VG */ | |
2b69db1f | 999 | if (!_uuid_prefix_matches(uuid, uuid_prefix, uuid_prefix_len)) |
3e8c6b73 | 1000 | continue; |
3e8c6b73 AK |
1001 | |
1002 | /* Refresh open_count */ | |
db208f51 | 1003 | if (!_info_by_dev(dinfo->major, dinfo->minor, 1, &info) || |
3e8c6b73 AK |
1004 | !info.exists || info.open_count) |
1005 | continue; | |
1006 | ||
1007 | if (!_deactivate_node(name, info.major, info.minor)) { | |
1008 | log_error("Unable to deactivate %s (%" PRIu32 | |
1009 | ":%" PRIu32 ")", name, info.major, | |
1010 | info.minor); | |
1011 | continue; | |
1012 | } | |
1013 | ||
b4f1578f AK |
1014 | if (dm_tree_node_num_children(child, 0)) |
1015 | dm_tree_deactivate_children(child, uuid_prefix, uuid_prefix_len); | |
3e8c6b73 AK |
1016 | } |
1017 | ||
1018 | return 1; | |
1019 | } | |
db208f51 | 1020 | |
c55b1410 AK |
1021 | void dm_tree_skip_lockfs(struct dm_tree_node *dnode) |
1022 | { | |
1023 | dnode->dtree->skip_lockfs = 1; | |
1024 | } | |
1025 | ||
b9ffd32c AK |
1026 | void dm_tree_use_no_flush_suspend(struct dm_tree_node *dnode) |
1027 | { | |
1028 | dnode->dtree->no_flush = 1; | |
1029 | } | |
1030 | ||
b4f1578f | 1031 | int dm_tree_suspend_children(struct dm_tree_node *dnode, |
db208f51 AK |
1032 | const char *uuid_prefix, |
1033 | size_t uuid_prefix_len) | |
1034 | { | |
1035 | void *handle = NULL; | |
b4f1578f | 1036 | struct dm_tree_node *child = dnode; |
db208f51 AK |
1037 | struct dm_info info, newinfo; |
1038 | const struct dm_info *dinfo; | |
1039 | const char *name; | |
1040 | const char *uuid; | |
1041 | ||
690a5da2 | 1042 | /* Suspend nodes at this level of the tree */ |
b4f1578f AK |
1043 | while ((child = dm_tree_next_child(&handle, dnode, 0))) { |
1044 | if (!(dinfo = dm_tree_node_get_info(child))) { | |
db208f51 AK |
1045 | stack; |
1046 | continue; | |
1047 | } | |
1048 | ||
b4f1578f | 1049 | if (!(name = dm_tree_node_get_name(child))) { |
db208f51 AK |
1050 | stack; |
1051 | continue; | |
1052 | } | |
1053 | ||
b4f1578f | 1054 | if (!(uuid = dm_tree_node_get_uuid(child))) { |
db208f51 AK |
1055 | stack; |
1056 | continue; | |
1057 | } | |
1058 | ||
1059 | /* Ignore if it doesn't belong to this VG */ | |
2b69db1f | 1060 | if (!_uuid_prefix_matches(uuid, uuid_prefix, uuid_prefix_len)) |
db208f51 AK |
1061 | continue; |
1062 | ||
690a5da2 AK |
1063 | /* Ensure immediate parents are already suspended */ |
1064 | if (!_children_suspended(child, 1, uuid_prefix, uuid_prefix_len)) | |
1065 | continue; | |
1066 | ||
db208f51 | 1067 | if (!_info_by_dev(dinfo->major, dinfo->minor, 0, &info) || |
b700541f | 1068 | !info.exists || info.suspended) |
db208f51 AK |
1069 | continue; |
1070 | ||
c55b1410 | 1071 | if (!_suspend_node(name, info.major, info.minor, |
b9ffd32c AK |
1072 | child->dtree->skip_lockfs, |
1073 | child->dtree->no_flush, &newinfo)) { | |
db208f51 AK |
1074 | log_error("Unable to suspend %s (%" PRIu32 |
1075 | ":%" PRIu32 ")", name, info.major, | |
1076 | info.minor); | |
1077 | continue; | |
1078 | } | |
1079 | ||
1080 | /* Update cached info */ | |
1081 | child->info = newinfo; | |
690a5da2 AK |
1082 | } |
1083 | ||
1084 | /* Then suspend any child nodes */ | |
1085 | handle = NULL; | |
1086 | ||
b4f1578f AK |
1087 | while ((child = dm_tree_next_child(&handle, dnode, 0))) { |
1088 | if (!(uuid = dm_tree_node_get_uuid(child))) { | |
690a5da2 AK |
1089 | stack; |
1090 | continue; | |
1091 | } | |
1092 | ||
1093 | /* Ignore if it doesn't belong to this VG */ | |
87f98002 | 1094 | if (!_uuid_prefix_matches(uuid, uuid_prefix, uuid_prefix_len)) |
690a5da2 | 1095 | continue; |
db208f51 | 1096 | |
b4f1578f AK |
1097 | if (dm_tree_node_num_children(child, 0)) |
1098 | dm_tree_suspend_children(child, uuid_prefix, uuid_prefix_len); | |
db208f51 AK |
1099 | } |
1100 | ||
1101 | return 1; | |
1102 | } | |
1103 | ||
b4f1578f | 1104 | int dm_tree_activate_children(struct dm_tree_node *dnode, |
db208f51 AK |
1105 | const char *uuid_prefix, |
1106 | size_t uuid_prefix_len) | |
1107 | { | |
1108 | void *handle = NULL; | |
b4f1578f | 1109 | struct dm_tree_node *child = dnode; |
165e4a11 AK |
1110 | struct dm_info newinfo; |
1111 | const char *name; | |
db208f51 | 1112 | const char *uuid; |
56c28292 | 1113 | int priority; |
db208f51 | 1114 | |
165e4a11 | 1115 | /* Activate children first */ |
b4f1578f AK |
1116 | while ((child = dm_tree_next_child(&handle, dnode, 0))) { |
1117 | if (!(uuid = dm_tree_node_get_uuid(child))) { | |
165e4a11 AK |
1118 | stack; |
1119 | continue; | |
db208f51 AK |
1120 | } |
1121 | ||
908db078 AK |
1122 | if (!_uuid_prefix_matches(uuid, uuid_prefix, uuid_prefix_len)) |
1123 | continue; | |
db208f51 | 1124 | |
b4f1578f AK |
1125 | if (dm_tree_node_num_children(child, 0)) |
1126 | dm_tree_activate_children(child, uuid_prefix, uuid_prefix_len); | |
56c28292 | 1127 | } |
165e4a11 | 1128 | |
56c28292 | 1129 | handle = NULL; |
165e4a11 | 1130 | |
56c28292 AK |
1131 | for (priority = 0; priority < 2; priority++) { |
1132 | while ((child = dm_tree_next_child(&handle, dnode, 0))) { | |
1133 | if (!(uuid = dm_tree_node_get_uuid(child))) { | |
1134 | stack; | |
1135 | continue; | |
165e4a11 | 1136 | } |
165e4a11 | 1137 | |
56c28292 AK |
1138 | if (!_uuid_prefix_matches(uuid, uuid_prefix, uuid_prefix_len)) |
1139 | continue; | |
165e4a11 | 1140 | |
56c28292 AK |
1141 | if (priority != child->activation_priority) |
1142 | continue; | |
165e4a11 | 1143 | |
56c28292 AK |
1144 | if (!(name = dm_tree_node_get_name(child))) { |
1145 | stack; | |
1146 | continue; | |
1147 | } | |
1148 | ||
1149 | /* Rename? */ | |
1150 | if (child->props.new_name) { | |
1151 | if (!_rename_node(name, child->props.new_name, child->info.major, child->info.minor)) { | |
1152 | log_error("Failed to rename %s (%" PRIu32 | |
1153 | ":%" PRIu32 ") to %s", name, child->info.major, | |
1154 | child->info.minor, child->props.new_name); | |
1155 | return 0; | |
1156 | } | |
1157 | child->name = child->props.new_name; | |
1158 | child->props.new_name = NULL; | |
1159 | } | |
1160 | ||
1161 | if (!child->info.inactive_table && !child->info.suspended) | |
1162 | continue; | |
1163 | ||
bafa2f39 | 1164 | if (!_resume_node(child->name, child->info.major, child->info.minor, |
52b84409 AK |
1165 | child->props.read_ahead, |
1166 | child->props.read_ahead_flags, &newinfo)) { | |
56c28292 | 1167 | log_error("Unable to resume %s (%" PRIu32 |
bafa2f39 | 1168 | ":%" PRIu32 ")", child->name, child->info.major, |
56c28292 AK |
1169 | child->info.minor); |
1170 | continue; | |
1171 | } | |
1172 | ||
1173 | /* Update cached info */ | |
1174 | child->info = newinfo; | |
1175 | } | |
db208f51 AK |
1176 | } |
1177 | ||
165e4a11 AK |
1178 | handle = NULL; |
1179 | ||
1180 | return 1; | |
1181 | } | |
1182 | ||
b4f1578f | 1183 | static int _create_node(struct dm_tree_node *dnode) |
165e4a11 AK |
1184 | { |
1185 | int r = 0; | |
1186 | struct dm_task *dmt; | |
1187 | ||
1188 | log_verbose("Creating %s", dnode->name); | |
1189 | ||
1190 | if (!(dmt = dm_task_create(DM_DEVICE_CREATE))) { | |
1191 | log_error("Create dm_task creation failed for %s", dnode->name); | |
1192 | return 0; | |
1193 | } | |
1194 | ||
1195 | if (!dm_task_set_name(dmt, dnode->name)) { | |
1196 | log_error("Failed to set device name for %s", dnode->name); | |
1197 | goto out; | |
1198 | } | |
1199 | ||
1200 | if (!dm_task_set_uuid(dmt, dnode->uuid)) { | |
1201 | log_error("Failed to set uuid for %s", dnode->name); | |
1202 | goto out; | |
1203 | } | |
1204 | ||
1205 | if (dnode->props.major && | |
1206 | (!dm_task_set_major(dmt, dnode->props.major) || | |
1207 | !dm_task_set_minor(dmt, dnode->props.minor))) { | |
1208 | log_error("Failed to set device number for %s creation.", dnode->name); | |
1209 | goto out; | |
1210 | } | |
1211 | ||
1212 | if (dnode->props.read_only && !dm_task_set_ro(dmt)) { | |
1213 | log_error("Failed to set read only flag for %s", dnode->name); | |
1214 | goto out; | |
1215 | } | |
1216 | ||
1217 | if (!dm_task_no_open_count(dmt)) | |
1218 | log_error("Failed to disable open_count"); | |
1219 | ||
1220 | if ((r = dm_task_run(dmt))) | |
1221 | r = dm_task_get_info(dmt, &dnode->info); | |
1222 | ||
1223 | out: | |
1224 | dm_task_destroy(dmt); | |
1225 | ||
1226 | return r; | |
1227 | } | |
1228 | ||
1229 | ||
b4f1578f | 1230 | static int _build_dev_string(char *devbuf, size_t bufsize, struct dm_tree_node *node) |
165e4a11 AK |
1231 | { |
1232 | if (!dm_format_dev(devbuf, bufsize, node->info.major, node->info.minor)) { | |
1233 | log_error("Failed to format %s device number for %s as dm " | |
1234 | "target (%u,%u)", | |
1235 | node->name, node->uuid, node->info.major, node->info.minor); | |
1236 | return 0; | |
1237 | } | |
1238 | ||
1239 | return 1; | |
1240 | } | |
1241 | ||
4dcaa230 AK |
1242 | static int _emit_areas_line(struct dm_task *dmt __attribute((unused)), |
1243 | struct load_segment *seg, char *params, | |
1244 | size_t paramsize, int *pos) | |
165e4a11 AK |
1245 | { |
1246 | struct seg_area *area; | |
7d7d93ac | 1247 | char devbuf[DM_FORMAT_DEV_BUFSIZE]; |
165e4a11 AK |
1248 | int tw; |
1249 | const char *prefix = ""; | |
1250 | ||
1251 | list_iterate_items(area, &seg->areas) { | |
b4f1578f AK |
1252 | if (!_build_dev_string(devbuf, sizeof(devbuf), area->dev_node)) |
1253 | return_0; | |
165e4a11 AK |
1254 | |
1255 | if ((tw = _dm_snprintf(params + *pos, paramsize - *pos, "%s%s %" PRIu64, | |
1256 | prefix, devbuf, area->offset)) < 0) { | |
b4f1578f | 1257 | stack; /* Out of space */ |
165e4a11 AK |
1258 | return -1; |
1259 | } | |
1260 | ||
1261 | prefix = " "; | |
1262 | *pos += tw; | |
1263 | } | |
1264 | ||
1265 | return 1; | |
1266 | } | |
1267 | ||
1268 | static int _emit_segment_line(struct dm_task *dmt, struct load_segment *seg, uint64_t *seg_start, char *params, size_t paramsize) | |
1269 | { | |
dbcb64b8 | 1270 | unsigned log_parm_count; |
165e4a11 AK |
1271 | int pos = 0; |
1272 | int tw; | |
1273 | int r; | |
7d7d93ac AK |
1274 | char originbuf[DM_FORMAT_DEV_BUFSIZE], cowbuf[DM_FORMAT_DEV_BUFSIZE]; |
1275 | char logbuf[DM_FORMAT_DEV_BUFSIZE]; | |
dbcb64b8 | 1276 | const char *logtype; |
165e4a11 AK |
1277 | |
1278 | switch(seg->type) { | |
1279 | case SEG_ERROR: | |
1280 | case SEG_ZERO: | |
165e4a11 AK |
1281 | case SEG_LINEAR: |
1282 | break; | |
1283 | case SEG_MIRRORED: | |
67b25ed4 | 1284 | log_parm_count = 1; /* Region size */ |
2d2b610f | 1285 | log_parm_count += hweight32(seg->flags); /* [no]sync, block_on_error etc. */ |
67b25ed4 | 1286 | |
311d6d81 AK |
1287 | if (seg->flags & DM_CORELOG) |
1288 | log_parm_count--; /* DM_CORELOG does not count in the param list */ | |
1289 | ||
165e4a11 | 1290 | if (seg->clustered) { |
311d6d81 AK |
1291 | if (seg->uuid) |
1292 | log_parm_count++; | |
d6d46615 | 1293 | if ((tw = _dm_snprintf(params + pos, paramsize - pos, "clustered-")) < 0) { |
b4f1578f | 1294 | stack; /* Out of space */ |
165e4a11 AK |
1295 | return -1; |
1296 | } | |
1297 | pos += tw; | |
1298 | } | |
dbcb64b8 | 1299 | |
dbcb64b8 AK |
1300 | if (!seg->log) |
1301 | logtype = "core"; | |
1302 | else { | |
1303 | logtype = "disk"; | |
1304 | log_parm_count++; | |
b4f1578f AK |
1305 | if (!_build_dev_string(logbuf, sizeof(logbuf), seg->log)) |
1306 | return_0; | |
dbcb64b8 AK |
1307 | } |
1308 | ||
1309 | if ((tw = _dm_snprintf(params + pos, paramsize - pos, "%s %u ", logtype, log_parm_count)) < 0) { | |
1310 | stack; /* Out of space */ | |
1311 | return -1; | |
1312 | } | |
1313 | pos += tw; | |
1314 | ||
1315 | if (seg->log) { | |
1316 | if ((tw = _dm_snprintf(params + pos, paramsize - pos, "%s ", logbuf)) < 0) { | |
b4f1578f | 1317 | stack; /* Out of space */ |
165e4a11 AK |
1318 | return -1; |
1319 | } | |
1320 | pos += tw; | |
1321 | } | |
dbcb64b8 AK |
1322 | |
1323 | if ((tw = _dm_snprintf(params + pos, paramsize - pos, "%u ", seg->region_size)) < 0) { | |
b4f1578f | 1324 | stack; /* Out of space */ |
165e4a11 AK |
1325 | return -1; |
1326 | } | |
1327 | pos += tw; | |
dbcb64b8 | 1328 | |
67b25ed4 AK |
1329 | if (seg->clustered && seg->uuid) { |
1330 | if ((tw = _dm_snprintf(params + pos, paramsize - pos, "%s ", seg->uuid)) < 0) { | |
1331 | stack; /* Out of space */ | |
1332 | return -1; | |
1333 | } | |
1334 | pos += tw; | |
1335 | } | |
1336 | ||
dbcb64b8 AK |
1337 | if ((seg->flags & DM_NOSYNC)) { |
1338 | if ((tw = _dm_snprintf(params + pos, paramsize - pos, "nosync ")) < 0) { | |
1339 | stack; /* Out of space */ | |
1340 | return -1; | |
1341 | } | |
1342 | pos += tw; | |
1343 | } else if ((seg->flags & DM_FORCESYNC)) { | |
1344 | if ((tw = _dm_snprintf(params + pos, paramsize - pos, "sync ")) < 0) { | |
1345 | stack; /* Out of space */ | |
1346 | return -1; | |
1347 | } | |
1348 | pos += tw; | |
1349 | } | |
1350 | ||
1351 | if ((seg->flags & DM_BLOCK_ON_ERROR)) { | |
1352 | if ((tw = _dm_snprintf(params + pos, paramsize - pos, "block_on_error ")) < 0) { | |
1353 | stack; /* Out of space */ | |
1354 | return -1; | |
1355 | } | |
1356 | pos += tw; | |
1357 | } | |
1358 | ||
1359 | if ((tw = _dm_snprintf(params + pos, paramsize - pos, "%u ", seg->mirror_area_count)) < 0) { | |
1360 | stack; /* Out of space */ | |
1361 | return -1; | |
1362 | } | |
1363 | pos += tw; | |
1364 | ||
165e4a11 AK |
1365 | break; |
1366 | case SEG_SNAPSHOT: | |
b4f1578f AK |
1367 | if (!_build_dev_string(originbuf, sizeof(originbuf), seg->origin)) |
1368 | return_0; | |
1369 | if (!_build_dev_string(cowbuf, sizeof(cowbuf), seg->cow)) | |
1370 | return_0; | |
165e4a11 AK |
1371 | if ((pos = _dm_snprintf(params, paramsize, "%s %s %c %d", |
1372 | originbuf, cowbuf, | |
1373 | seg->persistent ? 'P' : 'N', | |
1374 | seg->chunk_size)) < 0) { | |
b4f1578f | 1375 | stack; /* Out of space */ |
165e4a11 AK |
1376 | return -1; |
1377 | } | |
1378 | break; | |
1379 | case SEG_SNAPSHOT_ORIGIN: | |
b4f1578f AK |
1380 | if (!_build_dev_string(originbuf, sizeof(originbuf), seg->origin)) |
1381 | return_0; | |
165e4a11 AK |
1382 | if ((pos = _dm_snprintf(params, paramsize, "%s", |
1383 | originbuf)) < 0) { | |
b4f1578f | 1384 | stack; /* Out of space */ |
165e4a11 AK |
1385 | return -1; |
1386 | } | |
1387 | break; | |
1388 | case SEG_STRIPED: | |
1389 | if ((pos = _dm_snprintf(params, paramsize, "%u %u ", | |
1390 | seg->area_count, | |
1391 | seg->stripe_size)) < 0) { | |
b4f1578f | 1392 | stack; /* Out of space */ |
165e4a11 AK |
1393 | return -1; |
1394 | } | |
1395 | break; | |
1396 | } | |
1397 | ||
1398 | switch(seg->type) { | |
1399 | case SEG_ERROR: | |
1400 | case SEG_SNAPSHOT: | |
1401 | case SEG_SNAPSHOT_ORIGIN: | |
1402 | case SEG_ZERO: | |
1403 | break; | |
1404 | case SEG_LINEAR: | |
1405 | case SEG_MIRRORED: | |
1406 | case SEG_STRIPED: | |
1407 | if ((r = _emit_areas_line(dmt, seg, params, paramsize, &pos)) <= 0) { | |
1408 | stack; | |
1409 | return r; | |
1410 | } | |
1411 | break; | |
1412 | } | |
1413 | ||
1414 | log_debug("Adding target: %" PRIu64 " %" PRIu64 " %s %s", | |
1415 | *seg_start, seg->size, dm_segtypes[seg->type].target, params); | |
1416 | ||
b4f1578f AK |
1417 | if (!dm_task_add_target(dmt, *seg_start, seg->size, dm_segtypes[seg->type].target, params)) |
1418 | return_0; | |
165e4a11 AK |
1419 | |
1420 | *seg_start += seg->size; | |
1421 | ||
1422 | return 1; | |
1423 | } | |
1424 | ||
1425 | static int _emit_segment(struct dm_task *dmt, struct load_segment *seg, | |
1426 | uint64_t *seg_start) | |
1427 | { | |
1428 | char *params; | |
1429 | size_t paramsize = 4096; | |
1430 | int ret; | |
1431 | ||
1432 | do { | |
1433 | if (!(params = dm_malloc(paramsize))) { | |
1434 | log_error("Insufficient space for target parameters."); | |
1435 | return 0; | |
1436 | } | |
1437 | ||
12ea7cb1 | 1438 | params[0] = '\0'; |
165e4a11 AK |
1439 | ret = _emit_segment_line(dmt, seg, seg_start, params, paramsize); |
1440 | dm_free(params); | |
1441 | ||
1442 | if (!ret) | |
1443 | stack; | |
1444 | ||
1445 | if (ret >= 0) | |
1446 | return ret; | |
1447 | ||
1448 | log_debug("Insufficient space in params[%" PRIsize_t | |
1449 | "] for target parameters.", paramsize); | |
1450 | ||
1451 | paramsize *= 2; | |
1452 | } while (paramsize < MAX_TARGET_PARAMSIZE); | |
1453 | ||
1454 | log_error("Target parameter size too big. Aborting."); | |
1455 | return 0; | |
1456 | } | |
1457 | ||
b4f1578f | 1458 | static int _load_node(struct dm_tree_node *dnode) |
165e4a11 AK |
1459 | { |
1460 | int r = 0; | |
1461 | struct dm_task *dmt; | |
1462 | struct load_segment *seg; | |
1463 | uint64_t seg_start = 0; | |
1464 | ||
1465 | log_verbose("Loading %s table", dnode->name); | |
1466 | ||
1467 | if (!(dmt = dm_task_create(DM_DEVICE_RELOAD))) { | |
1468 | log_error("Reload dm_task creation failed for %s", dnode->name); | |
1469 | return 0; | |
1470 | } | |
1471 | ||
1472 | if (!dm_task_set_major(dmt, dnode->info.major) || | |
1473 | !dm_task_set_minor(dmt, dnode->info.minor)) { | |
1474 | log_error("Failed to set device number for %s reload.", dnode->name); | |
1475 | goto out; | |
1476 | } | |
1477 | ||
1478 | if (dnode->props.read_only && !dm_task_set_ro(dmt)) { | |
1479 | log_error("Failed to set read only flag for %s", dnode->name); | |
1480 | goto out; | |
1481 | } | |
1482 | ||
1483 | if (!dm_task_no_open_count(dmt)) | |
1484 | log_error("Failed to disable open_count"); | |
1485 | ||
1486 | list_iterate_items(seg, &dnode->props.segs) | |
b4f1578f AK |
1487 | if (!_emit_segment(dmt, seg, &seg_start)) |
1488 | goto_out; | |
165e4a11 | 1489 | |
ec289b64 AK |
1490 | if (!dm_task_suppress_identical_reload(dmt)) |
1491 | log_error("Failed to suppress reload of identical tables."); | |
1492 | ||
1493 | if ((r = dm_task_run(dmt))) { | |
165e4a11 | 1494 | r = dm_task_get_info(dmt, &dnode->info); |
ec289b64 AK |
1495 | if (r && !dnode->info.inactive_table) |
1496 | log_verbose("Suppressed %s identical table reload.", | |
1497 | dnode->name); | |
bb875bb9 AK |
1498 | |
1499 | if ((dnode->props.size_changed = | |
1500 | (dm_task_get_existing_table_size(dmt) == seg_start) ? 0 : 1)) | |
1501 | log_debug("Table size changed from %" PRIu64 " to %" | |
1502 | PRIu64 " for %s", | |
1503 | dm_task_get_existing_table_size(dmt), | |
1504 | seg_start, dnode->name); | |
ec289b64 | 1505 | } |
165e4a11 AK |
1506 | |
1507 | dnode->props.segment_count = 0; | |
1508 | ||
1509 | out: | |
1510 | dm_task_destroy(dmt); | |
1511 | ||
1512 | return r; | |
165e4a11 AK |
1513 | } |
1514 | ||
b4f1578f | 1515 | int dm_tree_preload_children(struct dm_tree_node *dnode, |
bb875bb9 AK |
1516 | const char *uuid_prefix, |
1517 | size_t uuid_prefix_len) | |
165e4a11 AK |
1518 | { |
1519 | void *handle = NULL; | |
b4f1578f | 1520 | struct dm_tree_node *child; |
165e4a11 | 1521 | struct dm_info newinfo; |
165e4a11 AK |
1522 | |
1523 | /* Preload children first */ | |
b4f1578f | 1524 | while ((child = dm_tree_next_child(&handle, dnode, 0))) { |
165e4a11 AK |
1525 | /* Skip existing non-device-mapper devices */ |
1526 | if (!child->info.exists && child->info.major) | |
1527 | continue; | |
1528 | ||
1529 | /* Ignore if it doesn't belong to this VG */ | |
87f98002 AK |
1530 | if (child->info.exists && |
1531 | !_uuid_prefix_matches(child->uuid, uuid_prefix, uuid_prefix_len)) | |
165e4a11 AK |
1532 | continue; |
1533 | ||
b4f1578f | 1534 | if (dm_tree_node_num_children(child, 0)) |
e6a6954e | 1535 | dm_tree_preload_children(child, uuid_prefix, uuid_prefix_len); |
165e4a11 | 1536 | |
165e4a11 AK |
1537 | /* FIXME Cope if name exists with no uuid? */ |
1538 | if (!child->info.exists) { | |
1539 | if (!_create_node(child)) { | |
1540 | stack; | |
1541 | return 0; | |
1542 | } | |
1543 | } | |
1544 | ||
1545 | if (!child->info.inactive_table && child->props.segment_count) { | |
1546 | if (!_load_node(child)) { | |
1547 | stack; | |
1548 | return 0; | |
1549 | } | |
1550 | } | |
1551 | ||
bb875bb9 | 1552 | /* Resume device immediately if it has parents and its size changed */ |
3776c494 | 1553 | if (!dm_tree_node_num_children(child, 1) || !child->props.size_changed) |
165e4a11 AK |
1554 | continue; |
1555 | ||
7707ea90 AK |
1556 | if (!child->info.inactive_table && !child->info.suspended) |
1557 | continue; | |
1558 | ||
fc795d87 | 1559 | if (!_resume_node(child->name, child->info.major, child->info.minor, |
52b84409 AK |
1560 | child->props.read_ahead, |
1561 | child->props.read_ahead_flags, &newinfo)) { | |
165e4a11 | 1562 | log_error("Unable to resume %s (%" PRIu32 |
fc795d87 | 1563 | ":%" PRIu32 ")", child->name, child->info.major, |
165e4a11 AK |
1564 | child->info.minor); |
1565 | continue; | |
1566 | } | |
1567 | ||
1568 | /* Update cached info */ | |
1569 | child->info = newinfo; | |
1570 | } | |
1571 | ||
1572 | handle = NULL; | |
1573 | ||
1574 | return 1; | |
1575 | } | |
1576 | ||
165e4a11 AK |
1577 | /* |
1578 | * Returns 1 if unsure. | |
1579 | */ | |
b4f1578f | 1580 | int dm_tree_children_use_uuid(struct dm_tree_node *dnode, |
165e4a11 AK |
1581 | const char *uuid_prefix, |
1582 | size_t uuid_prefix_len) | |
1583 | { | |
1584 | void *handle = NULL; | |
b4f1578f | 1585 | struct dm_tree_node *child = dnode; |
165e4a11 AK |
1586 | const char *uuid; |
1587 | ||
b4f1578f AK |
1588 | while ((child = dm_tree_next_child(&handle, dnode, 0))) { |
1589 | if (!(uuid = dm_tree_node_get_uuid(child))) { | |
1590 | log_error("Failed to get uuid for dtree node."); | |
165e4a11 AK |
1591 | return 1; |
1592 | } | |
1593 | ||
87f98002 | 1594 | if (_uuid_prefix_matches(uuid, uuid_prefix, uuid_prefix_len)) |
165e4a11 AK |
1595 | return 1; |
1596 | ||
b4f1578f AK |
1597 | if (dm_tree_node_num_children(child, 0)) |
1598 | dm_tree_children_use_uuid(child, uuid_prefix, uuid_prefix_len); | |
165e4a11 AK |
1599 | } |
1600 | ||
1601 | return 0; | |
1602 | } | |
1603 | ||
1604 | /* | |
1605 | * Target functions | |
1606 | */ | |
b4f1578f | 1607 | static struct load_segment *_add_segment(struct dm_tree_node *dnode, unsigned type, uint64_t size) |
165e4a11 AK |
1608 | { |
1609 | struct load_segment *seg; | |
1610 | ||
b4f1578f AK |
1611 | if (!(seg = dm_pool_zalloc(dnode->dtree->mem, sizeof(*seg)))) { |
1612 | log_error("dtree node segment allocation failed"); | |
165e4a11 AK |
1613 | return NULL; |
1614 | } | |
1615 | ||
1616 | seg->type = type; | |
1617 | seg->size = size; | |
1618 | seg->area_count = 0; | |
1619 | list_init(&seg->areas); | |
1620 | seg->stripe_size = 0; | |
1621 | seg->persistent = 0; | |
1622 | seg->chunk_size = 0; | |
1623 | seg->cow = NULL; | |
1624 | seg->origin = NULL; | |
1625 | ||
1626 | list_add(&dnode->props.segs, &seg->list); | |
1627 | dnode->props.segment_count++; | |
1628 | ||
1629 | return seg; | |
1630 | } | |
1631 | ||
b4f1578f | 1632 | int dm_tree_node_add_snapshot_origin_target(struct dm_tree_node *dnode, |
165e4a11 AK |
1633 | uint64_t size, |
1634 | const char *origin_uuid) | |
1635 | { | |
1636 | struct load_segment *seg; | |
b4f1578f | 1637 | struct dm_tree_node *origin_node; |
165e4a11 | 1638 | |
b4f1578f AK |
1639 | if (!(seg = _add_segment(dnode, SEG_SNAPSHOT_ORIGIN, size))) |
1640 | return_0; | |
165e4a11 | 1641 | |
b4f1578f | 1642 | if (!(origin_node = dm_tree_find_node_by_uuid(dnode->dtree, origin_uuid))) { |
165e4a11 AK |
1643 | log_error("Couldn't find snapshot origin uuid %s.", origin_uuid); |
1644 | return 0; | |
1645 | } | |
1646 | ||
1647 | seg->origin = origin_node; | |
b4f1578f AK |
1648 | if (!_link_tree_nodes(dnode, origin_node)) |
1649 | return_0; | |
165e4a11 | 1650 | |
56c28292 AK |
1651 | /* Resume snapshot origins after new snapshots */ |
1652 | dnode->activation_priority = 1; | |
1653 | ||
165e4a11 AK |
1654 | return 1; |
1655 | } | |
1656 | ||
b4f1578f | 1657 | int dm_tree_node_add_snapshot_target(struct dm_tree_node *node, |
165e4a11 AK |
1658 | uint64_t size, |
1659 | const char *origin_uuid, | |
1660 | const char *cow_uuid, | |
1661 | int persistent, | |
1662 | uint32_t chunk_size) | |
1663 | { | |
1664 | struct load_segment *seg; | |
b4f1578f | 1665 | struct dm_tree_node *origin_node, *cow_node; |
165e4a11 | 1666 | |
b4f1578f AK |
1667 | if (!(seg = _add_segment(node, SEG_SNAPSHOT, size))) |
1668 | return_0; | |
165e4a11 | 1669 | |
b4f1578f | 1670 | if (!(origin_node = dm_tree_find_node_by_uuid(node->dtree, origin_uuid))) { |
165e4a11 AK |
1671 | log_error("Couldn't find snapshot origin uuid %s.", origin_uuid); |
1672 | return 0; | |
1673 | } | |
1674 | ||
1675 | seg->origin = origin_node; | |
b4f1578f AK |
1676 | if (!_link_tree_nodes(node, origin_node)) |
1677 | return_0; | |
165e4a11 | 1678 | |
b4f1578f | 1679 | if (!(cow_node = dm_tree_find_node_by_uuid(node->dtree, cow_uuid))) { |
165e4a11 AK |
1680 | log_error("Couldn't find snapshot origin uuid %s.", cow_uuid); |
1681 | return 0; | |
1682 | } | |
1683 | ||
1684 | seg->cow = cow_node; | |
b4f1578f AK |
1685 | if (!_link_tree_nodes(node, cow_node)) |
1686 | return_0; | |
165e4a11 AK |
1687 | |
1688 | seg->persistent = persistent ? 1 : 0; | |
1689 | seg->chunk_size = chunk_size; | |
1690 | ||
1691 | return 1; | |
1692 | } | |
1693 | ||
b4f1578f | 1694 | int dm_tree_node_add_error_target(struct dm_tree_node *node, |
165e4a11 AK |
1695 | uint64_t size) |
1696 | { | |
b4f1578f AK |
1697 | if (!_add_segment(node, SEG_ERROR, size)) |
1698 | return_0; | |
165e4a11 AK |
1699 | |
1700 | return 1; | |
1701 | } | |
1702 | ||
b4f1578f | 1703 | int dm_tree_node_add_zero_target(struct dm_tree_node *node, |
165e4a11 AK |
1704 | uint64_t size) |
1705 | { | |
b4f1578f AK |
1706 | if (!_add_segment(node, SEG_ZERO, size)) |
1707 | return_0; | |
165e4a11 AK |
1708 | |
1709 | return 1; | |
1710 | } | |
1711 | ||
b4f1578f | 1712 | int dm_tree_node_add_linear_target(struct dm_tree_node *node, |
165e4a11 AK |
1713 | uint64_t size) |
1714 | { | |
b4f1578f AK |
1715 | if (!_add_segment(node, SEG_LINEAR, size)) |
1716 | return_0; | |
165e4a11 AK |
1717 | |
1718 | return 1; | |
1719 | } | |
1720 | ||
b4f1578f | 1721 | int dm_tree_node_add_striped_target(struct dm_tree_node *node, |
165e4a11 AK |
1722 | uint64_t size, |
1723 | uint32_t stripe_size) | |
1724 | { | |
1725 | struct load_segment *seg; | |
1726 | ||
b4f1578f AK |
1727 | if (!(seg = _add_segment(node, SEG_STRIPED, size))) |
1728 | return_0; | |
165e4a11 AK |
1729 | |
1730 | seg->stripe_size = stripe_size; | |
1731 | ||
1732 | return 1; | |
1733 | } | |
1734 | ||
b4f1578f | 1735 | int dm_tree_node_add_mirror_target_log(struct dm_tree_node *node, |
165e4a11 AK |
1736 | uint32_t region_size, |
1737 | unsigned clustered, | |
1738 | const char *log_uuid, | |
ce7ed2c0 AK |
1739 | unsigned area_count, |
1740 | uint32_t flags) | |
165e4a11 | 1741 | { |
908db078 | 1742 | struct dm_tree_node *log_node = NULL; |
165e4a11 AK |
1743 | struct load_segment *seg; |
1744 | ||
1745 | if (!node->props.segment_count) { | |
1746 | log_error("Internal error: Attempt to add target area to missing segment."); | |
1747 | return 0; | |
1748 | } | |
1749 | ||
1750 | seg = list_item(list_last(&node->props.segs), struct load_segment); | |
1751 | ||
24b026e3 | 1752 | if (log_uuid) { |
67b25ed4 AK |
1753 | if (!(seg->uuid = dm_pool_strdup(node->dtree->mem, log_uuid))) { |
1754 | log_error("log uuid pool_strdup failed"); | |
1755 | return 0; | |
1756 | } | |
9723090c AK |
1757 | if (!(flags & DM_CORELOG)) { |
1758 | if (!(log_node = dm_tree_find_node_by_uuid(node->dtree, log_uuid))) { | |
1759 | log_error("Couldn't find mirror log uuid %s.", log_uuid); | |
1760 | return 0; | |
1761 | } | |
1762 | ||
1763 | if (!_link_tree_nodes(node, log_node)) | |
1764 | return_0; | |
1765 | } | |
165e4a11 AK |
1766 | } |
1767 | ||
1768 | seg->log = log_node; | |
165e4a11 AK |
1769 | seg->region_size = region_size; |
1770 | seg->clustered = clustered; | |
1771 | seg->mirror_area_count = area_count; | |
dbcb64b8 | 1772 | seg->flags = flags; |
165e4a11 AK |
1773 | |
1774 | return 1; | |
1775 | } | |
1776 | ||
b4f1578f | 1777 | int dm_tree_node_add_mirror_target(struct dm_tree_node *node, |
165e4a11 AK |
1778 | uint64_t size) |
1779 | { | |
1780 | struct load_segment *seg; | |
1781 | ||
b4f1578f AK |
1782 | if (!(seg = _add_segment(node, SEG_MIRRORED, size))) |
1783 | return_0; | |
165e4a11 AK |
1784 | |
1785 | return 1; | |
1786 | } | |
1787 | ||
b4f1578f | 1788 | static int _add_area(struct dm_tree_node *node, struct load_segment *seg, struct dm_tree_node *dev_node, uint64_t offset) |
165e4a11 AK |
1789 | { |
1790 | struct seg_area *area; | |
1791 | ||
b4f1578f | 1792 | if (!(area = dm_pool_zalloc(node->dtree->mem, sizeof (*area)))) { |
165e4a11 AK |
1793 | log_error("Failed to allocate target segment area."); |
1794 | return 0; | |
1795 | } | |
1796 | ||
1797 | area->dev_node = dev_node; | |
1798 | area->offset = offset; | |
1799 | ||
1800 | list_add(&seg->areas, &area->list); | |
1801 | seg->area_count++; | |
1802 | ||
1803 | return 1; | |
1804 | } | |
1805 | ||
b4f1578f | 1806 | int dm_tree_node_add_target_area(struct dm_tree_node *node, |
165e4a11 AK |
1807 | const char *dev_name, |
1808 | const char *uuid, | |
1809 | uint64_t offset) | |
1810 | { | |
1811 | struct load_segment *seg; | |
1812 | struct stat info; | |
b4f1578f | 1813 | struct dm_tree_node *dev_node; |
165e4a11 AK |
1814 | |
1815 | if ((!dev_name || !*dev_name) && (!uuid || !*uuid)) { | |
b4f1578f | 1816 | log_error("dm_tree_node_add_target_area called without device"); |
165e4a11 AK |
1817 | return 0; |
1818 | } | |
1819 | ||
1820 | if (uuid) { | |
b4f1578f | 1821 | if (!(dev_node = dm_tree_find_node_by_uuid(node->dtree, uuid))) { |
165e4a11 AK |
1822 | log_error("Couldn't find area uuid %s.", uuid); |
1823 | return 0; | |
1824 | } | |
b4f1578f AK |
1825 | if (!_link_tree_nodes(node, dev_node)) |
1826 | return_0; | |
165e4a11 AK |
1827 | } else { |
1828 | if (stat(dev_name, &info) < 0) { | |
1829 | log_error("Device %s not found.", dev_name); | |
1830 | return 0; | |
1831 | } | |
1832 | ||
1833 | if (!S_ISBLK(info.st_mode)) { | |
1834 | log_error("Device %s is not a block device.", dev_name); | |
1835 | return 0; | |
1836 | } | |
1837 | ||
1838 | /* FIXME Check correct macro use */ | |
b4f1578f AK |
1839 | if (!(dev_node = _add_dev(node->dtree, node, MAJOR(info.st_rdev), MINOR(info.st_rdev)))) |
1840 | return_0; | |
165e4a11 AK |
1841 | } |
1842 | ||
1843 | if (!node->props.segment_count) { | |
1844 | log_error("Internal error: Attempt to add target area to missing segment."); | |
1845 | return 0; | |
1846 | } | |
1847 | ||
1848 | seg = list_item(list_last(&node->props.segs), struct load_segment); | |
1849 | ||
b4f1578f AK |
1850 | if (!_add_area(node, seg, dev_node, offset)) |
1851 | return_0; | |
165e4a11 AK |
1852 | |
1853 | return 1; | |
db208f51 | 1854 | } |