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