]>
Commit | Line | Data |
---|---|---|
1 | /* | |
2 | * Copyright (C) 2005-2011 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 "dmlib.h" | |
16 | #include "libdm-targets.h" | |
17 | #include "libdm-common.h" | |
18 | #include "kdev_t.h" | |
19 | #include "dm-ioctl.h" | |
20 | ||
21 | #include <stdarg.h> | |
22 | #include <sys/param.h> | |
23 | #include <sys/utsname.h> | |
24 | ||
25 | #define MAX_TARGET_PARAMSIZE 500000 | |
26 | ||
27 | #define REPLICATOR_LOCAL_SITE 0 | |
28 | ||
29 | /* Supported segment types */ | |
30 | enum { | |
31 | SEG_CRYPT, | |
32 | SEG_ERROR, | |
33 | SEG_LINEAR, | |
34 | SEG_MIRRORED, | |
35 | SEG_REPLICATOR, | |
36 | SEG_REPLICATOR_DEV, | |
37 | SEG_SNAPSHOT, | |
38 | SEG_SNAPSHOT_ORIGIN, | |
39 | SEG_SNAPSHOT_MERGE, | |
40 | SEG_STRIPED, | |
41 | SEG_ZERO, | |
42 | SEG_THIN_POOL, | |
43 | SEG_THIN, | |
44 | SEG_RAID1, | |
45 | SEG_RAID4, | |
46 | SEG_RAID5_LA, | |
47 | SEG_RAID5_RA, | |
48 | SEG_RAID5_LS, | |
49 | SEG_RAID5_RS, | |
50 | SEG_RAID6_ZR, | |
51 | SEG_RAID6_NR, | |
52 | SEG_RAID6_NC, | |
53 | SEG_LAST, | |
54 | }; | |
55 | ||
56 | /* FIXME Add crypt and multipath support */ | |
57 | ||
58 | struct { | |
59 | unsigned type; | |
60 | const char *target; | |
61 | } dm_segtypes[] = { | |
62 | { SEG_CRYPT, "crypt" }, | |
63 | { SEG_ERROR, "error" }, | |
64 | { SEG_LINEAR, "linear" }, | |
65 | { SEG_MIRRORED, "mirror" }, | |
66 | { SEG_REPLICATOR, "replicator" }, | |
67 | { SEG_REPLICATOR_DEV, "replicator-dev" }, | |
68 | { SEG_SNAPSHOT, "snapshot" }, | |
69 | { SEG_SNAPSHOT_ORIGIN, "snapshot-origin" }, | |
70 | { SEG_SNAPSHOT_MERGE, "snapshot-merge" }, | |
71 | { SEG_STRIPED, "striped" }, | |
72 | { SEG_ZERO, "zero"}, | |
73 | { SEG_THIN_POOL, "thin-pool"}, | |
74 | { SEG_THIN, "thin"}, | |
75 | { SEG_RAID1, "raid1"}, | |
76 | { SEG_RAID4, "raid4"}, | |
77 | { SEG_RAID5_LA, "raid5_la"}, | |
78 | { SEG_RAID5_RA, "raid5_ra"}, | |
79 | { SEG_RAID5_LS, "raid5_ls"}, | |
80 | { SEG_RAID5_RS, "raid5_rs"}, | |
81 | { SEG_RAID6_ZR, "raid6_zr"}, | |
82 | { SEG_RAID6_NR, "raid6_nr"}, | |
83 | { SEG_RAID6_NC, "raid6_nc"}, | |
84 | ||
85 | /* | |
86 | *WARNING: Since 'raid' target overloads this 1:1 mapping table | |
87 | * for search do not add new enum elements past them! | |
88 | */ | |
89 | { SEG_RAID5_LS, "raid5"}, /* same as "raid5_ls" (default for MD also) */ | |
90 | { SEG_RAID6_ZR, "raid6"}, /* same as "raid6_zr" */ | |
91 | { SEG_LAST, NULL }, | |
92 | }; | |
93 | ||
94 | /* Some segment types have a list of areas of other devices attached */ | |
95 | struct seg_area { | |
96 | struct dm_list list; | |
97 | ||
98 | struct dm_tree_node *dev_node; | |
99 | ||
100 | uint64_t offset; | |
101 | ||
102 | unsigned rsite_index; /* Replicator site index */ | |
103 | struct dm_tree_node *slog; /* Replicator sync log node */ | |
104 | uint64_t region_size; /* Replicator sync log size */ | |
105 | uint32_t flags; /* Replicator sync log flags */ | |
106 | }; | |
107 | ||
108 | struct dm_thin_message { | |
109 | dm_thin_message_t type; | |
110 | union { | |
111 | struct { | |
112 | uint32_t device_id; | |
113 | uint32_t origin_id; | |
114 | } m_create_snap; | |
115 | struct { | |
116 | uint32_t device_id; | |
117 | } m_create_thin; | |
118 | struct { | |
119 | uint32_t device_id; | |
120 | } m_delete; | |
121 | struct { | |
122 | uint64_t current_id; | |
123 | uint64_t new_id; | |
124 | } m_set_transaction_id; | |
125 | } u; | |
126 | }; | |
127 | ||
128 | struct thin_message { | |
129 | struct dm_list list; | |
130 | struct dm_thin_message message; | |
131 | int expected_errno; | |
132 | }; | |
133 | ||
134 | /* Replicator-log has a list of sites */ | |
135 | /* FIXME: maybe move to seg_area too? */ | |
136 | struct replicator_site { | |
137 | struct dm_list list; | |
138 | ||
139 | unsigned rsite_index; | |
140 | dm_replicator_mode_t mode; | |
141 | uint32_t async_timeout; | |
142 | uint32_t fall_behind_ios; | |
143 | uint64_t fall_behind_data; | |
144 | }; | |
145 | ||
146 | /* Per-segment properties */ | |
147 | struct load_segment { | |
148 | struct dm_list list; | |
149 | ||
150 | unsigned type; | |
151 | ||
152 | uint64_t size; | |
153 | ||
154 | unsigned area_count; /* Linear + Striped + Mirrored + Crypt + Replicator */ | |
155 | struct dm_list areas; /* Linear + Striped + Mirrored + Crypt + Replicator */ | |
156 | ||
157 | uint32_t stripe_size; /* Striped + raid */ | |
158 | ||
159 | int persistent; /* Snapshot */ | |
160 | uint32_t chunk_size; /* Snapshot */ | |
161 | struct dm_tree_node *cow; /* Snapshot */ | |
162 | struct dm_tree_node *origin; /* Snapshot + Snapshot origin */ | |
163 | struct dm_tree_node *merge; /* Snapshot */ | |
164 | ||
165 | struct dm_tree_node *log; /* Mirror + Replicator */ | |
166 | uint32_t region_size; /* Mirror + raid */ | |
167 | unsigned clustered; /* Mirror */ | |
168 | unsigned mirror_area_count; /* Mirror */ | |
169 | uint32_t flags; /* Mirror log */ | |
170 | char *uuid; /* Clustered mirror log */ | |
171 | ||
172 | const char *cipher; /* Crypt */ | |
173 | const char *chainmode; /* Crypt */ | |
174 | const char *iv; /* Crypt */ | |
175 | uint64_t iv_offset; /* Crypt */ | |
176 | const char *key; /* Crypt */ | |
177 | ||
178 | const char *rlog_type; /* Replicator */ | |
179 | struct dm_list rsites; /* Replicator */ | |
180 | unsigned rsite_count; /* Replicator */ | |
181 | unsigned rdevice_count; /* Replicator */ | |
182 | struct dm_tree_node *replicator;/* Replicator-dev */ | |
183 | uint64_t rdevice_index; /* Replicator-dev */ | |
184 | ||
185 | uint64_t rebuilds; /* raid */ | |
186 | ||
187 | struct dm_tree_node *metadata; /* Thin_pool */ | |
188 | struct dm_tree_node *pool; /* Thin_pool, Thin */ | |
189 | struct dm_list thin_messages; /* Thin_pool */ | |
190 | uint64_t transaction_id; /* Thin_pool */ | |
191 | uint64_t low_water_mark; /* Thin_pool */ | |
192 | uint32_t data_block_size; /* Thin_pool */ | |
193 | unsigned skip_block_zeroing; /* Thin_pool */ | |
194 | uint32_t device_id; /* Thin */ | |
195 | ||
196 | }; | |
197 | ||
198 | /* Per-device properties */ | |
199 | struct load_properties { | |
200 | int read_only; | |
201 | uint32_t major; | |
202 | uint32_t minor; | |
203 | ||
204 | uint32_t read_ahead; | |
205 | uint32_t read_ahead_flags; | |
206 | ||
207 | unsigned segment_count; | |
208 | unsigned size_changed; | |
209 | struct dm_list segs; | |
210 | ||
211 | const char *new_name; | |
212 | ||
213 | /* If immediate_dev_node is set to 1, try to create the dev node | |
214 | * as soon as possible (e.g. in preload stage even during traversal | |
215 | * and processing of dm tree). This will also flush all stacked dev | |
216 | * node operations, synchronizing with udev. | |
217 | */ | |
218 | unsigned immediate_dev_node; | |
219 | ||
220 | /* | |
221 | * If the device size changed from zero and this is set, | |
222 | * don't resume the device immediately, even if the device | |
223 | * has parents. This works provided the parents do not | |
224 | * validate the device size and is required by pvmove to | |
225 | * avoid starting the mirror resync operation too early. | |
226 | */ | |
227 | unsigned delay_resume_if_new; | |
228 | ||
229 | /* Send messages for this node in preload */ | |
230 | unsigned send_messages; | |
231 | }; | |
232 | ||
233 | /* Two of these used to join two nodes with uses and used_by. */ | |
234 | struct dm_tree_link { | |
235 | struct dm_list list; | |
236 | struct dm_tree_node *node; | |
237 | }; | |
238 | ||
239 | struct dm_tree_node { | |
240 | struct dm_tree *dtree; | |
241 | ||
242 | const char *name; | |
243 | const char *uuid; | |
244 | struct dm_info info; | |
245 | ||
246 | struct dm_list uses; /* Nodes this node uses */ | |
247 | struct dm_list used_by; /* Nodes that use this node */ | |
248 | ||
249 | int activation_priority; /* 0 gets activated first */ | |
250 | ||
251 | uint16_t udev_flags; /* Udev control flags */ | |
252 | ||
253 | void *context; /* External supplied context */ | |
254 | ||
255 | struct load_properties props; /* For creation/table (re)load */ | |
256 | ||
257 | /* | |
258 | * If presuspend of child node is needed | |
259 | * Note: only direct child is allowed | |
260 | */ | |
261 | struct dm_tree_node *presuspend_node; | |
262 | ||
263 | /* Callback */ | |
264 | dm_node_callback_fn callback; | |
265 | void *callback_data; | |
266 | }; | |
267 | ||
268 | struct dm_tree { | |
269 | struct dm_pool *mem; | |
270 | struct dm_hash_table *devs; | |
271 | struct dm_hash_table *uuids; | |
272 | struct dm_tree_node root; | |
273 | int skip_lockfs; /* 1 skips lockfs (for non-snapshots) */ | |
274 | int no_flush; /* 1 sets noflush (mirrors/multipath) */ | |
275 | int retry_remove; /* 1 retries remove if not successful */ | |
276 | uint32_t cookie; | |
277 | }; | |
278 | ||
279 | /* | |
280 | * Tree functions. | |
281 | */ | |
282 | struct dm_tree *dm_tree_create(void) | |
283 | { | |
284 | struct dm_pool *dmem; | |
285 | struct dm_tree *dtree; | |
286 | ||
287 | if (!(dmem = dm_pool_create("dtree", 1024)) || | |
288 | !(dtree = dm_pool_zalloc(dmem, sizeof(*dtree)))) { | |
289 | log_error("Failed to allocate dtree."); | |
290 | if (dmem) | |
291 | dm_pool_destroy(dmem); | |
292 | return NULL; | |
293 | } | |
294 | ||
295 | dtree->root.dtree = dtree; | |
296 | dm_list_init(&dtree->root.uses); | |
297 | dm_list_init(&dtree->root.used_by); | |
298 | dtree->skip_lockfs = 0; | |
299 | dtree->no_flush = 0; | |
300 | dtree->mem = dmem; | |
301 | ||
302 | if (!(dtree->devs = dm_hash_create(8))) { | |
303 | log_error("dtree hash creation failed"); | |
304 | dm_pool_destroy(dtree->mem); | |
305 | return NULL; | |
306 | } | |
307 | ||
308 | if (!(dtree->uuids = dm_hash_create(32))) { | |
309 | log_error("dtree uuid hash creation failed"); | |
310 | dm_hash_destroy(dtree->devs); | |
311 | dm_pool_destroy(dtree->mem); | |
312 | return NULL; | |
313 | } | |
314 | ||
315 | return dtree; | |
316 | } | |
317 | ||
318 | void dm_tree_free(struct dm_tree *dtree) | |
319 | { | |
320 | if (!dtree) | |
321 | return; | |
322 | ||
323 | dm_hash_destroy(dtree->uuids); | |
324 | dm_hash_destroy(dtree->devs); | |
325 | dm_pool_destroy(dtree->mem); | |
326 | } | |
327 | ||
328 | void dm_tree_set_cookie(struct dm_tree_node *node, uint32_t cookie) | |
329 | { | |
330 | node->dtree->cookie = cookie; | |
331 | } | |
332 | ||
333 | uint32_t dm_tree_get_cookie(struct dm_tree_node *node) | |
334 | { | |
335 | return node->dtree->cookie; | |
336 | } | |
337 | ||
338 | void dm_tree_skip_lockfs(struct dm_tree_node *dnode) | |
339 | { | |
340 | dnode->dtree->skip_lockfs = 1; | |
341 | } | |
342 | ||
343 | void dm_tree_use_no_flush_suspend(struct dm_tree_node *dnode) | |
344 | { | |
345 | dnode->dtree->no_flush = 1; | |
346 | } | |
347 | ||
348 | void dm_tree_retry_remove(struct dm_tree_node *dnode) | |
349 | { | |
350 | dnode->dtree->retry_remove = 1; | |
351 | } | |
352 | ||
353 | /* | |
354 | * Node functions. | |
355 | */ | |
356 | static int _nodes_are_linked(const struct dm_tree_node *parent, | |
357 | const struct dm_tree_node *child) | |
358 | { | |
359 | struct dm_tree_link *dlink; | |
360 | ||
361 | dm_list_iterate_items(dlink, &parent->uses) | |
362 | if (dlink->node == child) | |
363 | return 1; | |
364 | ||
365 | return 0; | |
366 | } | |
367 | ||
368 | static int _link(struct dm_list *list, struct dm_tree_node *node) | |
369 | { | |
370 | struct dm_tree_link *dlink; | |
371 | ||
372 | if (!(dlink = dm_pool_alloc(node->dtree->mem, sizeof(*dlink)))) { | |
373 | log_error("dtree link allocation failed"); | |
374 | return 0; | |
375 | } | |
376 | ||
377 | dlink->node = node; | |
378 | dm_list_add(list, &dlink->list); | |
379 | ||
380 | return 1; | |
381 | } | |
382 | ||
383 | static int _link_nodes(struct dm_tree_node *parent, | |
384 | struct dm_tree_node *child) | |
385 | { | |
386 | if (_nodes_are_linked(parent, child)) | |
387 | return 1; | |
388 | ||
389 | if (!_link(&parent->uses, child)) | |
390 | return 0; | |
391 | ||
392 | if (!_link(&child->used_by, parent)) | |
393 | return 0; | |
394 | ||
395 | return 1; | |
396 | } | |
397 | ||
398 | static void _unlink(struct dm_list *list, struct dm_tree_node *node) | |
399 | { | |
400 | struct dm_tree_link *dlink; | |
401 | ||
402 | dm_list_iterate_items(dlink, list) | |
403 | if (dlink->node == node) { | |
404 | dm_list_del(&dlink->list); | |
405 | break; | |
406 | } | |
407 | } | |
408 | ||
409 | static void _unlink_nodes(struct dm_tree_node *parent, | |
410 | struct dm_tree_node *child) | |
411 | { | |
412 | if (!_nodes_are_linked(parent, child)) | |
413 | return; | |
414 | ||
415 | _unlink(&parent->uses, child); | |
416 | _unlink(&child->used_by, parent); | |
417 | } | |
418 | ||
419 | static int _add_to_toplevel(struct dm_tree_node *node) | |
420 | { | |
421 | return _link_nodes(&node->dtree->root, node); | |
422 | } | |
423 | ||
424 | static void _remove_from_toplevel(struct dm_tree_node *node) | |
425 | { | |
426 | _unlink_nodes(&node->dtree->root, node); | |
427 | } | |
428 | ||
429 | static int _add_to_bottomlevel(struct dm_tree_node *node) | |
430 | { | |
431 | return _link_nodes(node, &node->dtree->root); | |
432 | } | |
433 | ||
434 | static void _remove_from_bottomlevel(struct dm_tree_node *node) | |
435 | { | |
436 | _unlink_nodes(node, &node->dtree->root); | |
437 | } | |
438 | ||
439 | static int _link_tree_nodes(struct dm_tree_node *parent, struct dm_tree_node *child) | |
440 | { | |
441 | /* Don't link to root node if child already has a parent */ | |
442 | if (parent == &parent->dtree->root) { | |
443 | if (dm_tree_node_num_children(child, 1)) | |
444 | return 1; | |
445 | } else | |
446 | _remove_from_toplevel(child); | |
447 | ||
448 | if (child == &child->dtree->root) { | |
449 | if (dm_tree_node_num_children(parent, 0)) | |
450 | return 1; | |
451 | } else | |
452 | _remove_from_bottomlevel(parent); | |
453 | ||
454 | return _link_nodes(parent, child); | |
455 | } | |
456 | ||
457 | static struct dm_tree_node *_create_dm_tree_node(struct dm_tree *dtree, | |
458 | const char *name, | |
459 | const char *uuid, | |
460 | struct dm_info *info, | |
461 | void *context, | |
462 | uint16_t udev_flags) | |
463 | { | |
464 | struct dm_tree_node *node; | |
465 | uint64_t dev; | |
466 | ||
467 | if (!(node = dm_pool_zalloc(dtree->mem, sizeof(*node)))) { | |
468 | log_error("_create_dm_tree_node alloc failed"); | |
469 | return NULL; | |
470 | } | |
471 | ||
472 | node->dtree = dtree; | |
473 | ||
474 | node->name = name; | |
475 | node->uuid = uuid; | |
476 | node->info = *info; | |
477 | node->context = context; | |
478 | node->udev_flags = udev_flags; | |
479 | node->activation_priority = 0; | |
480 | ||
481 | dm_list_init(&node->uses); | |
482 | dm_list_init(&node->used_by); | |
483 | dm_list_init(&node->props.segs); | |
484 | ||
485 | dev = MKDEV(info->major, info->minor); | |
486 | ||
487 | if (!dm_hash_insert_binary(dtree->devs, (const char *) &dev, | |
488 | sizeof(dev), node)) { | |
489 | log_error("dtree node hash insertion failed"); | |
490 | dm_pool_free(dtree->mem, node); | |
491 | return NULL; | |
492 | } | |
493 | ||
494 | if (uuid && *uuid && | |
495 | !dm_hash_insert(dtree->uuids, uuid, node)) { | |
496 | log_error("dtree uuid hash insertion failed"); | |
497 | dm_hash_remove_binary(dtree->devs, (const char *) &dev, | |
498 | sizeof(dev)); | |
499 | dm_pool_free(dtree->mem, node); | |
500 | return NULL; | |
501 | } | |
502 | ||
503 | return node; | |
504 | } | |
505 | ||
506 | static struct dm_tree_node *_find_dm_tree_node(struct dm_tree *dtree, | |
507 | uint32_t major, uint32_t minor) | |
508 | { | |
509 | uint64_t dev = MKDEV(major, minor); | |
510 | ||
511 | return dm_hash_lookup_binary(dtree->devs, (const char *) &dev, | |
512 | sizeof(dev)); | |
513 | } | |
514 | ||
515 | static struct dm_tree_node *_find_dm_tree_node_by_uuid(struct dm_tree *dtree, | |
516 | const char *uuid) | |
517 | { | |
518 | struct dm_tree_node *node; | |
519 | const char *default_uuid_prefix; | |
520 | size_t default_uuid_prefix_len; | |
521 | ||
522 | if ((node = dm_hash_lookup(dtree->uuids, uuid))) | |
523 | return node; | |
524 | ||
525 | default_uuid_prefix = dm_uuid_prefix(); | |
526 | default_uuid_prefix_len = strlen(default_uuid_prefix); | |
527 | ||
528 | if (strncmp(uuid, default_uuid_prefix, default_uuid_prefix_len)) | |
529 | return NULL; | |
530 | ||
531 | return dm_hash_lookup(dtree->uuids, uuid + default_uuid_prefix_len); | |
532 | } | |
533 | ||
534 | void dm_tree_node_set_udev_flags(struct dm_tree_node *dnode, uint16_t udev_flags) | |
535 | ||
536 | { | |
537 | struct dm_info *dinfo = &dnode->info; | |
538 | ||
539 | if (udev_flags != dnode->udev_flags) | |
540 | log_debug("Resetting %s (%" PRIu32 ":%" PRIu32 | |
541 | ") udev_flags from 0x%x to 0x%x", | |
542 | dnode->name, dinfo->major, dinfo->minor, | |
543 | dnode->udev_flags, udev_flags); | |
544 | dnode->udev_flags = udev_flags; | |
545 | } | |
546 | ||
547 | void dm_tree_node_set_read_ahead(struct dm_tree_node *dnode, | |
548 | uint32_t read_ahead, | |
549 | uint32_t read_ahead_flags) | |
550 | { | |
551 | dnode->props.read_ahead = read_ahead; | |
552 | dnode->props.read_ahead_flags = read_ahead_flags; | |
553 | } | |
554 | ||
555 | void dm_tree_node_set_presuspend_node(struct dm_tree_node *node, | |
556 | struct dm_tree_node *presuspend_node) | |
557 | { | |
558 | node->presuspend_node = presuspend_node; | |
559 | } | |
560 | ||
561 | const char *dm_tree_node_get_name(const struct dm_tree_node *node) | |
562 | { | |
563 | return node->info.exists ? node->name : ""; | |
564 | } | |
565 | ||
566 | const char *dm_tree_node_get_uuid(const struct dm_tree_node *node) | |
567 | { | |
568 | return node->info.exists ? node->uuid : ""; | |
569 | } | |
570 | ||
571 | const struct dm_info *dm_tree_node_get_info(const struct dm_tree_node *node) | |
572 | { | |
573 | return &node->info; | |
574 | } | |
575 | ||
576 | void *dm_tree_node_get_context(const struct dm_tree_node *node) | |
577 | { | |
578 | return node->context; | |
579 | } | |
580 | ||
581 | int dm_tree_node_size_changed(const struct dm_tree_node *dnode) | |
582 | { | |
583 | return dnode->props.size_changed; | |
584 | } | |
585 | ||
586 | int dm_tree_node_num_children(const struct dm_tree_node *node, uint32_t inverted) | |
587 | { | |
588 | if (inverted) { | |
589 | if (_nodes_are_linked(&node->dtree->root, node)) | |
590 | return 0; | |
591 | return dm_list_size(&node->used_by); | |
592 | } | |
593 | ||
594 | if (_nodes_are_linked(node, &node->dtree->root)) | |
595 | return 0; | |
596 | ||
597 | return dm_list_size(&node->uses); | |
598 | } | |
599 | ||
600 | /* | |
601 | * Returns 1 if no prefix supplied | |
602 | */ | |
603 | static int _uuid_prefix_matches(const char *uuid, const char *uuid_prefix, size_t uuid_prefix_len) | |
604 | { | |
605 | const char *default_uuid_prefix = dm_uuid_prefix(); | |
606 | size_t default_uuid_prefix_len = strlen(default_uuid_prefix); | |
607 | ||
608 | if (!uuid_prefix) | |
609 | return 1; | |
610 | ||
611 | if (!strncmp(uuid, uuid_prefix, uuid_prefix_len)) | |
612 | return 1; | |
613 | ||
614 | /* Handle transition: active device uuids might be missing the prefix */ | |
615 | if (uuid_prefix_len <= 4) | |
616 | return 0; | |
617 | ||
618 | if (!strncmp(uuid, default_uuid_prefix, default_uuid_prefix_len)) | |
619 | return 0; | |
620 | ||
621 | if (strncmp(uuid_prefix, default_uuid_prefix, default_uuid_prefix_len)) | |
622 | return 0; | |
623 | ||
624 | if (!strncmp(uuid, uuid_prefix + default_uuid_prefix_len, uuid_prefix_len - default_uuid_prefix_len)) | |
625 | return 1; | |
626 | ||
627 | return 0; | |
628 | } | |
629 | ||
630 | /* | |
631 | * Returns 1 if no children. | |
632 | */ | |
633 | static int _children_suspended(struct dm_tree_node *node, | |
634 | uint32_t inverted, | |
635 | const char *uuid_prefix, | |
636 | size_t uuid_prefix_len) | |
637 | { | |
638 | struct dm_list *list; | |
639 | struct dm_tree_link *dlink; | |
640 | const struct dm_info *dinfo; | |
641 | const char *uuid; | |
642 | ||
643 | if (inverted) { | |
644 | if (_nodes_are_linked(&node->dtree->root, node)) | |
645 | return 1; | |
646 | list = &node->used_by; | |
647 | } else { | |
648 | if (_nodes_are_linked(node, &node->dtree->root)) | |
649 | return 1; | |
650 | list = &node->uses; | |
651 | } | |
652 | ||
653 | dm_list_iterate_items(dlink, list) { | |
654 | if (!(uuid = dm_tree_node_get_uuid(dlink->node))) { | |
655 | stack; | |
656 | continue; | |
657 | } | |
658 | ||
659 | /* Ignore if it doesn't belong to this VG */ | |
660 | if (!_uuid_prefix_matches(uuid, uuid_prefix, uuid_prefix_len)) | |
661 | continue; | |
662 | ||
663 | /* Ignore if parent node wants to presuspend this node */ | |
664 | if (dlink->node->presuspend_node == node) | |
665 | continue; | |
666 | ||
667 | if (!(dinfo = dm_tree_node_get_info(dlink->node))) { | |
668 | stack; /* FIXME Is this normal? */ | |
669 | return 0; | |
670 | } | |
671 | ||
672 | if (!dinfo->suspended) | |
673 | return 0; | |
674 | } | |
675 | ||
676 | return 1; | |
677 | } | |
678 | ||
679 | /* | |
680 | * Set major and minor to zero for root of tree. | |
681 | */ | |
682 | struct dm_tree_node *dm_tree_find_node(struct dm_tree *dtree, | |
683 | uint32_t major, | |
684 | uint32_t minor) | |
685 | { | |
686 | if (!major && !minor) | |
687 | return &dtree->root; | |
688 | ||
689 | return _find_dm_tree_node(dtree, major, minor); | |
690 | } | |
691 | ||
692 | /* | |
693 | * Set uuid to NULL for root of tree. | |
694 | */ | |
695 | struct dm_tree_node *dm_tree_find_node_by_uuid(struct dm_tree *dtree, | |
696 | const char *uuid) | |
697 | { | |
698 | if (!uuid || !*uuid) | |
699 | return &dtree->root; | |
700 | ||
701 | return _find_dm_tree_node_by_uuid(dtree, uuid); | |
702 | } | |
703 | ||
704 | /* | |
705 | * First time set *handle to NULL. | |
706 | * Set inverted to invert the tree. | |
707 | */ | |
708 | struct dm_tree_node *dm_tree_next_child(void **handle, | |
709 | const struct dm_tree_node *parent, | |
710 | uint32_t inverted) | |
711 | { | |
712 | struct dm_list **dlink = (struct dm_list **) handle; | |
713 | const struct dm_list *use_list; | |
714 | ||
715 | if (inverted) | |
716 | use_list = &parent->used_by; | |
717 | else | |
718 | use_list = &parent->uses; | |
719 | ||
720 | if (!*dlink) | |
721 | *dlink = dm_list_first(use_list); | |
722 | else | |
723 | *dlink = dm_list_next(use_list, *dlink); | |
724 | ||
725 | return (*dlink) ? dm_list_item(*dlink, struct dm_tree_link)->node : NULL; | |
726 | } | |
727 | ||
728 | static int _deps(struct dm_task **dmt, struct dm_pool *mem, uint32_t major, uint32_t minor, | |
729 | const char **name, const char **uuid, unsigned inactive_table, | |
730 | struct dm_info *info, struct dm_deps **deps) | |
731 | { | |
732 | memset(info, 0, sizeof(*info)); | |
733 | ||
734 | if (!dm_is_dm_major(major)) { | |
735 | if (name) | |
736 | *name = ""; | |
737 | if (uuid) | |
738 | *uuid = ""; | |
739 | *deps = NULL; | |
740 | info->major = major; | |
741 | info->minor = minor; | |
742 | return 1; | |
743 | } | |
744 | ||
745 | if (!(*dmt = dm_task_create(DM_DEVICE_DEPS))) { | |
746 | log_error("deps dm_task creation failed"); | |
747 | return 0; | |
748 | } | |
749 | ||
750 | if (!dm_task_set_major(*dmt, major)) { | |
751 | log_error("_deps: failed to set major for (%" PRIu32 ":%" PRIu32 ")", | |
752 | major, minor); | |
753 | goto failed; | |
754 | } | |
755 | ||
756 | if (!dm_task_set_minor(*dmt, minor)) { | |
757 | log_error("_deps: failed to set minor for (%" PRIu32 ":%" PRIu32 ")", | |
758 | major, minor); | |
759 | goto failed; | |
760 | } | |
761 | ||
762 | if (inactive_table && !dm_task_query_inactive_table(*dmt)) { | |
763 | log_error("_deps: failed to set inactive table for (%" PRIu32 ":%" PRIu32 ")", | |
764 | major, minor); | |
765 | goto failed; | |
766 | } | |
767 | ||
768 | if (!dm_task_run(*dmt)) { | |
769 | log_error("_deps: task run failed for (%" PRIu32 ":%" PRIu32 ")", | |
770 | major, minor); | |
771 | goto failed; | |
772 | } | |
773 | ||
774 | if (!dm_task_get_info(*dmt, info)) { | |
775 | log_error("_deps: failed to get info for (%" PRIu32 ":%" PRIu32 ")", | |
776 | major, minor); | |
777 | goto failed; | |
778 | } | |
779 | ||
780 | if (!info->exists) { | |
781 | if (name) | |
782 | *name = ""; | |
783 | if (uuid) | |
784 | *uuid = ""; | |
785 | *deps = NULL; | |
786 | } else { | |
787 | if (info->major != major) { | |
788 | log_error("Inconsistent dtree major number: %u != %u", | |
789 | major, info->major); | |
790 | goto failed; | |
791 | } | |
792 | if (info->minor != minor) { | |
793 | log_error("Inconsistent dtree minor number: %u != %u", | |
794 | minor, info->minor); | |
795 | goto failed; | |
796 | } | |
797 | if (name && !(*name = dm_pool_strdup(mem, dm_task_get_name(*dmt)))) { | |
798 | log_error("name pool_strdup failed"); | |
799 | goto failed; | |
800 | } | |
801 | if (uuid && !(*uuid = dm_pool_strdup(mem, dm_task_get_uuid(*dmt)))) { | |
802 | log_error("uuid pool_strdup failed"); | |
803 | goto failed; | |
804 | } | |
805 | *deps = dm_task_get_deps(*dmt); | |
806 | } | |
807 | ||
808 | return 1; | |
809 | ||
810 | failed: | |
811 | dm_task_destroy(*dmt); | |
812 | return 0; | |
813 | } | |
814 | ||
815 | /* | |
816 | * Deactivate a device with its dependencies if the uuid prefix matches. | |
817 | */ | |
818 | static int _info_by_dev(uint32_t major, uint32_t minor, int with_open_count, | |
819 | struct dm_info *info, struct dm_pool *mem, | |
820 | const char **name, const char **uuid) | |
821 | { | |
822 | struct dm_task *dmt; | |
823 | int r; | |
824 | ||
825 | if (!(dmt = dm_task_create(DM_DEVICE_INFO))) { | |
826 | log_error("_info_by_dev: dm_task creation failed"); | |
827 | return 0; | |
828 | } | |
829 | ||
830 | if (!dm_task_set_major(dmt, major) || !dm_task_set_minor(dmt, minor)) { | |
831 | log_error("_info_by_dev: Failed to set device number"); | |
832 | dm_task_destroy(dmt); | |
833 | return 0; | |
834 | } | |
835 | ||
836 | if (!with_open_count && !dm_task_no_open_count(dmt)) | |
837 | log_error("Failed to disable open_count"); | |
838 | ||
839 | if (!(r = dm_task_run(dmt))) | |
840 | goto_out; | |
841 | ||
842 | if (!(r = dm_task_get_info(dmt, info))) | |
843 | goto_out; | |
844 | ||
845 | if (name && !(*name = dm_pool_strdup(mem, dm_task_get_name(dmt)))) { | |
846 | log_error("name pool_strdup failed"); | |
847 | r = 0; | |
848 | goto_out; | |
849 | } | |
850 | ||
851 | if (uuid && !(*uuid = dm_pool_strdup(mem, dm_task_get_uuid(dmt)))) { | |
852 | log_error("uuid pool_strdup failed"); | |
853 | r = 0; | |
854 | goto_out; | |
855 | } | |
856 | ||
857 | out: | |
858 | dm_task_destroy(dmt); | |
859 | ||
860 | return r; | |
861 | } | |
862 | ||
863 | static int _check_device_not_in_use(const char *name, struct dm_info *info) | |
864 | { | |
865 | if (!info->exists) | |
866 | return 1; | |
867 | ||
868 | /* If sysfs is not used, use open_count information only. */ | |
869 | if (!*dm_sysfs_dir()) { | |
870 | if (info->open_count) { | |
871 | log_error("Device %s (%" PRIu32 ":%" PRIu32 ") in use", | |
872 | name, info->major, info->minor); | |
873 | return 0; | |
874 | } | |
875 | ||
876 | return 1; | |
877 | } | |
878 | ||
879 | if (dm_device_has_holders(info->major, info->minor)) { | |
880 | log_error("Device %s (%" PRIu32 ":%" PRIu32 ") is used " | |
881 | "by another device.", name, info->major, info->minor); | |
882 | return 0; | |
883 | } | |
884 | ||
885 | if (dm_device_has_mounted_fs(info->major, info->minor)) { | |
886 | log_error("Device %s (%" PRIu32 ":%" PRIu32 ") contains " | |
887 | "a filesystem in use.", name, info->major, info->minor); | |
888 | return 0; | |
889 | } | |
890 | ||
891 | return 1; | |
892 | } | |
893 | ||
894 | /* Check if all parent nodes of given node have open_count == 0 */ | |
895 | static int _node_has_closed_parents(struct dm_tree_node *node, | |
896 | const char *uuid_prefix, | |
897 | size_t uuid_prefix_len) | |
898 | { | |
899 | struct dm_tree_link *dlink; | |
900 | const struct dm_info *dinfo; | |
901 | struct dm_info info; | |
902 | const char *uuid; | |
903 | ||
904 | /* Iterate through parents of this node */ | |
905 | dm_list_iterate_items(dlink, &node->used_by) { | |
906 | if (!(uuid = dm_tree_node_get_uuid(dlink->node))) { | |
907 | stack; | |
908 | continue; | |
909 | } | |
910 | ||
911 | /* Ignore if it doesn't belong to this VG */ | |
912 | if (!_uuid_prefix_matches(uuid, uuid_prefix, uuid_prefix_len)) | |
913 | continue; | |
914 | ||
915 | if (!(dinfo = dm_tree_node_get_info(dlink->node))) { | |
916 | stack; /* FIXME Is this normal? */ | |
917 | return 0; | |
918 | } | |
919 | ||
920 | /* Refresh open_count */ | |
921 | if (!_info_by_dev(dinfo->major, dinfo->minor, 1, &info, NULL, NULL, NULL) || | |
922 | !info.exists) | |
923 | continue; | |
924 | ||
925 | if (info.open_count) { | |
926 | log_debug("Node %s %d:%d has open_count %d", uuid_prefix, | |
927 | dinfo->major, dinfo->minor, info.open_count); | |
928 | return 0; | |
929 | } | |
930 | } | |
931 | ||
932 | return 1; | |
933 | } | |
934 | ||
935 | static int _deactivate_node(const char *name, uint32_t major, uint32_t minor, | |
936 | uint32_t *cookie, uint16_t udev_flags, int retry) | |
937 | { | |
938 | struct dm_task *dmt; | |
939 | int r = 0; | |
940 | ||
941 | log_verbose("Removing %s (%" PRIu32 ":%" PRIu32 ")", name, major, minor); | |
942 | ||
943 | if (!(dmt = dm_task_create(DM_DEVICE_REMOVE))) { | |
944 | log_error("Deactivation 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 deactivation", name); | |
950 | goto out; | |
951 | } | |
952 | ||
953 | if (!dm_task_no_open_count(dmt)) | |
954 | log_error("Failed to disable open_count"); | |
955 | ||
956 | if (cookie) | |
957 | if (!dm_task_set_cookie(dmt, cookie, udev_flags)) | |
958 | goto out; | |
959 | ||
960 | if (retry) | |
961 | dm_task_retry_remove(dmt); | |
962 | ||
963 | r = dm_task_run(dmt); | |
964 | ||
965 | /* FIXME Until kernel returns actual name so dm-iface.c can handle it */ | |
966 | rm_dev_node(name, dmt->cookie_set && !(udev_flags & DM_UDEV_DISABLE_DM_RULES_FLAG), | |
967 | dmt->cookie_set && (udev_flags & DM_UDEV_DISABLE_LIBRARY_FALLBACK)); | |
968 | ||
969 | /* FIXME Remove node from tree or mark invalid? */ | |
970 | ||
971 | out: | |
972 | dm_task_destroy(dmt); | |
973 | ||
974 | return r; | |
975 | } | |
976 | ||
977 | static int _node_clear_table(struct dm_tree_node *dnode, uint16_t udev_flags) | |
978 | { | |
979 | struct dm_task *dmt = NULL, *deps_dmt = NULL; | |
980 | struct dm_info *info, deps_info; | |
981 | struct dm_deps *deps = NULL; | |
982 | const char *name, *uuid; | |
983 | const char *default_uuid_prefix; | |
984 | size_t default_uuid_prefix_len; | |
985 | uint32_t i; | |
986 | int r = 0; | |
987 | ||
988 | if (!(info = &dnode->info)) { | |
989 | log_error("_node_clear_table failed: missing info"); | |
990 | return 0; | |
991 | } | |
992 | ||
993 | if (!(name = dm_tree_node_get_name(dnode))) { | |
994 | log_error("_node_clear_table failed: missing name"); | |
995 | return 0; | |
996 | } | |
997 | ||
998 | /* Is there a table? */ | |
999 | if (!info->exists || !info->inactive_table) | |
1000 | return 1; | |
1001 | ||
1002 | /* Get devices used by inactive table that's about to be deleted. */ | |
1003 | if (!_deps(&deps_dmt, dnode->dtree->mem, info->major, info->minor, NULL, NULL, 1, info, &deps)) { | |
1004 | log_error("Failed to obtain dependencies for %s before clearing table.", name); | |
1005 | return 0; | |
1006 | } | |
1007 | ||
1008 | log_verbose("Clearing inactive table %s (%" PRIu32 ":%" PRIu32 ")", | |
1009 | name, info->major, info->minor); | |
1010 | ||
1011 | if (!(dmt = dm_task_create(DM_DEVICE_CLEAR))) { | |
1012 | log_error("Table clear dm_task creation failed for %s", name); | |
1013 | goto_out; | |
1014 | } | |
1015 | ||
1016 | if (!dm_task_set_major(dmt, info->major) || | |
1017 | !dm_task_set_minor(dmt, info->minor)) { | |
1018 | log_error("Failed to set device number for %s table clear", name); | |
1019 | goto_out; | |
1020 | } | |
1021 | ||
1022 | r = dm_task_run(dmt); | |
1023 | ||
1024 | if (!dm_task_get_info(dmt, info)) { | |
1025 | log_error("_node_clear_table failed: info missing after running task for %s", name); | |
1026 | r = 0; | |
1027 | } | |
1028 | ||
1029 | if (!r || !deps) | |
1030 | goto_out; | |
1031 | ||
1032 | /* | |
1033 | * Remove (incomplete) devices that the inactive table referred to but | |
1034 | * which are not in the tree, no longer referenced and don't have a live | |
1035 | * table. | |
1036 | */ | |
1037 | default_uuid_prefix = dm_uuid_prefix(); | |
1038 | default_uuid_prefix_len = strlen(default_uuid_prefix); | |
1039 | ||
1040 | for (i = 0; i < deps->count; i++) { | |
1041 | /* If already in tree, assume it's under control */ | |
1042 | if (_find_dm_tree_node(dnode->dtree, MAJOR(deps->device[i]), MINOR(deps->device[i]))) | |
1043 | continue; | |
1044 | ||
1045 | if (!_info_by_dev(MAJOR(deps->device[i]), MINOR(deps->device[i]), 1, | |
1046 | &deps_info, dnode->dtree->mem, &name, &uuid)) | |
1047 | continue; | |
1048 | ||
1049 | /* Proceed if device is an 'orphan' - unreferenced and without a live table. */ | |
1050 | if (!deps_info.exists || deps_info.live_table || deps_info.open_count) | |
1051 | continue; | |
1052 | ||
1053 | if (strncmp(uuid, default_uuid_prefix, default_uuid_prefix_len)) | |
1054 | continue; | |
1055 | ||
1056 | /* Remove device. */ | |
1057 | if (!_deactivate_node(name, deps_info.major, deps_info.minor, &dnode->dtree->cookie, udev_flags, 0)) { | |
1058 | log_error("Failed to deactivate no-longer-used device %s (%" | |
1059 | PRIu32 ":%" PRIu32 ")", name, deps_info.major, deps_info.minor); | |
1060 | } else if (deps_info.suspended) | |
1061 | dec_suspended(); | |
1062 | } | |
1063 | ||
1064 | out: | |
1065 | if (dmt) | |
1066 | dm_task_destroy(dmt); | |
1067 | ||
1068 | if (deps_dmt) | |
1069 | dm_task_destroy(deps_dmt); | |
1070 | ||
1071 | return r; | |
1072 | } | |
1073 | ||
1074 | struct dm_tree_node *dm_tree_add_new_dev_with_udev_flags(struct dm_tree *dtree, | |
1075 | const char *name, | |
1076 | const char *uuid, | |
1077 | uint32_t major, | |
1078 | uint32_t minor, | |
1079 | int read_only, | |
1080 | int clear_inactive, | |
1081 | void *context, | |
1082 | uint16_t udev_flags) | |
1083 | { | |
1084 | struct dm_tree_node *dnode; | |
1085 | struct dm_info info; | |
1086 | const char *name2; | |
1087 | const char *uuid2; | |
1088 | ||
1089 | if (!name || !uuid) { | |
1090 | log_error("Cannot add device without name and uuid."); | |
1091 | return NULL; | |
1092 | } | |
1093 | ||
1094 | /* Do we need to add node to tree? */ | |
1095 | if (!(dnode = dm_tree_find_node_by_uuid(dtree, uuid))) { | |
1096 | if (!(name2 = dm_pool_strdup(dtree->mem, name))) { | |
1097 | log_error("name pool_strdup failed"); | |
1098 | return NULL; | |
1099 | } | |
1100 | if (!(uuid2 = dm_pool_strdup(dtree->mem, uuid))) { | |
1101 | log_error("uuid pool_strdup failed"); | |
1102 | return NULL; | |
1103 | } | |
1104 | ||
1105 | memset(&info, 0, sizeof(info)); | |
1106 | ||
1107 | if (!(dnode = _create_dm_tree_node(dtree, name2, uuid2, &info, | |
1108 | context, 0))) | |
1109 | return_NULL; | |
1110 | ||
1111 | /* Attach to root node until a table is supplied */ | |
1112 | if (!_add_to_toplevel(dnode) || !_add_to_bottomlevel(dnode)) | |
1113 | return_NULL; | |
1114 | ||
1115 | dnode->props.major = major; | |
1116 | dnode->props.minor = minor; | |
1117 | dnode->props.new_name = NULL; | |
1118 | dnode->props.size_changed = 0; | |
1119 | } else if (strcmp(name, dnode->name)) { | |
1120 | /* Do we need to rename node? */ | |
1121 | if (!(dnode->props.new_name = dm_pool_strdup(dtree->mem, name))) { | |
1122 | log_error("name pool_strdup failed"); | |
1123 | return NULL; | |
1124 | } | |
1125 | } | |
1126 | ||
1127 | dnode->props.read_only = read_only ? 1 : 0; | |
1128 | dnode->props.read_ahead = DM_READ_AHEAD_AUTO; | |
1129 | dnode->props.read_ahead_flags = 0; | |
1130 | ||
1131 | if (clear_inactive && !_node_clear_table(dnode, udev_flags)) | |
1132 | return_NULL; | |
1133 | ||
1134 | dnode->context = context; | |
1135 | dnode->udev_flags = udev_flags; | |
1136 | ||
1137 | return dnode; | |
1138 | } | |
1139 | ||
1140 | struct dm_tree_node *dm_tree_add_new_dev(struct dm_tree *dtree, const char *name, | |
1141 | const char *uuid, uint32_t major, uint32_t minor, | |
1142 | int read_only, int clear_inactive, void *context) | |
1143 | { | |
1144 | return dm_tree_add_new_dev_with_udev_flags(dtree, name, uuid, major, minor, | |
1145 | read_only, clear_inactive, context, 0); | |
1146 | } | |
1147 | ||
1148 | static struct dm_tree_node *_add_dev(struct dm_tree *dtree, | |
1149 | struct dm_tree_node *parent, | |
1150 | uint32_t major, uint32_t minor, | |
1151 | uint16_t udev_flags) | |
1152 | { | |
1153 | struct dm_task *dmt = NULL; | |
1154 | struct dm_info info; | |
1155 | struct dm_deps *deps = NULL; | |
1156 | const char *name = NULL; | |
1157 | const char *uuid = NULL; | |
1158 | struct dm_tree_node *node = NULL; | |
1159 | uint32_t i; | |
1160 | int new = 0; | |
1161 | ||
1162 | /* Already in tree? */ | |
1163 | if (!(node = _find_dm_tree_node(dtree, major, minor))) { | |
1164 | if (!_deps(&dmt, dtree->mem, major, minor, &name, &uuid, 0, &info, &deps)) | |
1165 | return_NULL; | |
1166 | ||
1167 | if (!(node = _create_dm_tree_node(dtree, name, uuid, &info, | |
1168 | NULL, udev_flags))) | |
1169 | goto_out; | |
1170 | new = 1; | |
1171 | } | |
1172 | ||
1173 | if (!_link_tree_nodes(parent, node)) { | |
1174 | node = NULL; | |
1175 | goto_out; | |
1176 | } | |
1177 | ||
1178 | /* If node was already in tree, no need to recurse. */ | |
1179 | if (!new) | |
1180 | goto out; | |
1181 | ||
1182 | /* Can't recurse if not a mapped device or there are no dependencies */ | |
1183 | if (!node->info.exists || !deps || !deps->count) { | |
1184 | if (!_add_to_bottomlevel(node)) { | |
1185 | stack; | |
1186 | node = NULL; | |
1187 | } | |
1188 | goto out; | |
1189 | } | |
1190 | ||
1191 | /* Add dependencies to tree */ | |
1192 | for (i = 0; i < deps->count; i++) | |
1193 | if (!_add_dev(dtree, node, MAJOR(deps->device[i]), | |
1194 | MINOR(deps->device[i]), udev_flags)) { | |
1195 | node = NULL; | |
1196 | goto_out; | |
1197 | } | |
1198 | ||
1199 | out: | |
1200 | if (dmt) | |
1201 | dm_task_destroy(dmt); | |
1202 | ||
1203 | return node; | |
1204 | } | |
1205 | ||
1206 | int dm_tree_add_dev(struct dm_tree *dtree, uint32_t major, uint32_t minor) | |
1207 | { | |
1208 | return _add_dev(dtree, &dtree->root, major, minor, 0) ? 1 : 0; | |
1209 | } | |
1210 | ||
1211 | int dm_tree_add_dev_with_udev_flags(struct dm_tree *dtree, uint32_t major, | |
1212 | uint32_t minor, uint16_t udev_flags) | |
1213 | { | |
1214 | return _add_dev(dtree, &dtree->root, major, minor, udev_flags) ? 1 : 0; | |
1215 | } | |
1216 | ||
1217 | static int _rename_node(const char *old_name, const char *new_name, uint32_t major, | |
1218 | uint32_t minor, uint32_t *cookie, uint16_t udev_flags) | |
1219 | { | |
1220 | struct dm_task *dmt; | |
1221 | int r = 0; | |
1222 | ||
1223 | log_verbose("Renaming %s (%" PRIu32 ":%" PRIu32 ") to %s", old_name, major, minor, new_name); | |
1224 | ||
1225 | if (!(dmt = dm_task_create(DM_DEVICE_RENAME))) { | |
1226 | log_error("Rename dm_task creation failed for %s", old_name); | |
1227 | return 0; | |
1228 | } | |
1229 | ||
1230 | if (!dm_task_set_name(dmt, old_name)) { | |
1231 | log_error("Failed to set name for %s rename.", old_name); | |
1232 | goto out; | |
1233 | } | |
1234 | ||
1235 | if (!dm_task_set_newname(dmt, new_name)) | |
1236 | goto_out; | |
1237 | ||
1238 | if (!dm_task_no_open_count(dmt)) | |
1239 | log_error("Failed to disable open_count"); | |
1240 | ||
1241 | if (!dm_task_set_cookie(dmt, cookie, udev_flags)) | |
1242 | goto out; | |
1243 | ||
1244 | r = dm_task_run(dmt); | |
1245 | ||
1246 | out: | |
1247 | dm_task_destroy(dmt); | |
1248 | ||
1249 | return r; | |
1250 | } | |
1251 | ||
1252 | /* FIXME Merge with _suspend_node? */ | |
1253 | static int _resume_node(const char *name, uint32_t major, uint32_t minor, | |
1254 | uint32_t read_ahead, uint32_t read_ahead_flags, | |
1255 | struct dm_info *newinfo, uint32_t *cookie, | |
1256 | uint16_t udev_flags, int already_suspended) | |
1257 | { | |
1258 | struct dm_task *dmt; | |
1259 | int r = 0; | |
1260 | ||
1261 | log_verbose("Resuming %s (%" PRIu32 ":%" PRIu32 ")", name, major, minor); | |
1262 | ||
1263 | if (!(dmt = dm_task_create(DM_DEVICE_RESUME))) { | |
1264 | log_debug("Suspend dm_task creation failed for %s.", name); | |
1265 | return 0; | |
1266 | } | |
1267 | ||
1268 | /* FIXME Kernel should fill in name on return instead */ | |
1269 | if (!dm_task_set_name(dmt, name)) { | |
1270 | log_debug("Failed to set device name for %s resumption.", name); | |
1271 | goto out; | |
1272 | } | |
1273 | ||
1274 | if (!dm_task_set_major(dmt, major) || !dm_task_set_minor(dmt, minor)) { | |
1275 | log_error("Failed to set device number for %s resumption.", name); | |
1276 | goto out; | |
1277 | } | |
1278 | ||
1279 | if (!dm_task_no_open_count(dmt)) | |
1280 | log_error("Failed to disable open_count"); | |
1281 | ||
1282 | if (!dm_task_set_read_ahead(dmt, read_ahead, read_ahead_flags)) | |
1283 | log_error("Failed to set read ahead"); | |
1284 | ||
1285 | if (!dm_task_set_cookie(dmt, cookie, udev_flags)) | |
1286 | goto_out; | |
1287 | ||
1288 | if (!(r = dm_task_run(dmt))) | |
1289 | goto_out; | |
1290 | ||
1291 | if (already_suspended) | |
1292 | dec_suspended(); | |
1293 | ||
1294 | if (!(r = dm_task_get_info(dmt, newinfo))) | |
1295 | stack; | |
1296 | ||
1297 | out: | |
1298 | dm_task_destroy(dmt); | |
1299 | ||
1300 | return r; | |
1301 | } | |
1302 | ||
1303 | static int _suspend_node(const char *name, uint32_t major, uint32_t minor, | |
1304 | int skip_lockfs, int no_flush, struct dm_info *newinfo) | |
1305 | { | |
1306 | struct dm_task *dmt; | |
1307 | int r; | |
1308 | ||
1309 | log_verbose("Suspending %s (%" PRIu32 ":%" PRIu32 ")%s%s", | |
1310 | name, major, minor, | |
1311 | skip_lockfs ? "" : " with filesystem sync", | |
1312 | no_flush ? "" : " with device flush"); | |
1313 | ||
1314 | if (!(dmt = dm_task_create(DM_DEVICE_SUSPEND))) { | |
1315 | log_error("Suspend dm_task creation failed for %s", name); | |
1316 | return 0; | |
1317 | } | |
1318 | ||
1319 | if (!dm_task_set_major(dmt, major) || !dm_task_set_minor(dmt, minor)) { | |
1320 | log_error("Failed to set device number for %s suspension.", name); | |
1321 | dm_task_destroy(dmt); | |
1322 | return 0; | |
1323 | } | |
1324 | ||
1325 | if (!dm_task_no_open_count(dmt)) | |
1326 | log_error("Failed to disable open_count"); | |
1327 | ||
1328 | if (skip_lockfs && !dm_task_skip_lockfs(dmt)) | |
1329 | log_error("Failed to set skip_lockfs flag."); | |
1330 | ||
1331 | if (no_flush && !dm_task_no_flush(dmt)) | |
1332 | log_error("Failed to set no_flush flag."); | |
1333 | ||
1334 | if ((r = dm_task_run(dmt))) { | |
1335 | inc_suspended(); | |
1336 | r = dm_task_get_info(dmt, newinfo); | |
1337 | } | |
1338 | ||
1339 | dm_task_destroy(dmt); | |
1340 | ||
1341 | return r; | |
1342 | } | |
1343 | ||
1344 | static int _thin_pool_status_transaction_id(struct dm_tree_node *dnode, uint64_t *transaction_id) | |
1345 | { | |
1346 | struct dm_task *dmt; | |
1347 | int r = 0; | |
1348 | uint64_t start, length; | |
1349 | char *type = NULL; | |
1350 | char *params = NULL; | |
1351 | ||
1352 | if (!(dmt = dm_task_create(DM_DEVICE_STATUS))) | |
1353 | return_0; | |
1354 | ||
1355 | if (!dm_task_set_major(dmt, dnode->info.major) || | |
1356 | !dm_task_set_minor(dmt, dnode->info.minor)) { | |
1357 | log_error("Failed to set major minor."); | |
1358 | goto out; | |
1359 | } | |
1360 | ||
1361 | if (!dm_task_run(dmt)) | |
1362 | goto_out; | |
1363 | ||
1364 | dm_get_next_target(dmt, NULL, &start, &length, &type, ¶ms); | |
1365 | ||
1366 | if (type && (strcmp(type, "thin-pool") != 0)) { | |
1367 | log_error("Expected thin-pool target for %d:%d and got %s.", | |
1368 | dnode->info.major, dnode->info.minor, type); | |
1369 | goto out; | |
1370 | } | |
1371 | ||
1372 | if (!params || (sscanf(params, "%" PRIu64, transaction_id) != 1)) { | |
1373 | log_error("Failed to parse transaction_id from %s.", params); | |
1374 | goto out; | |
1375 | } | |
1376 | ||
1377 | log_debug("Thin pool transaction id: %" PRIu64 " status: %s.", *transaction_id, params); | |
1378 | ||
1379 | r = 1; | |
1380 | out: | |
1381 | dm_task_destroy(dmt); | |
1382 | ||
1383 | return r; | |
1384 | } | |
1385 | ||
1386 | static int _thin_pool_node_message(struct dm_tree_node *dnode, struct thin_message *tm) | |
1387 | { | |
1388 | struct dm_task *dmt; | |
1389 | struct dm_thin_message *m = &tm->message; | |
1390 | char buf[64]; | |
1391 | int r; | |
1392 | ||
1393 | switch (m->type) { | |
1394 | case DM_THIN_MESSAGE_CREATE_SNAP: | |
1395 | r = dm_snprintf(buf, sizeof(buf), "create_snap %u %u", | |
1396 | m->u.m_create_snap.device_id, | |
1397 | m->u.m_create_snap.origin_id); | |
1398 | break; | |
1399 | case DM_THIN_MESSAGE_CREATE_THIN: | |
1400 | r = dm_snprintf(buf, sizeof(buf), "create_thin %u", | |
1401 | m->u.m_create_thin.device_id); | |
1402 | break; | |
1403 | case DM_THIN_MESSAGE_DELETE: | |
1404 | r = dm_snprintf(buf, sizeof(buf), "delete %u", | |
1405 | m->u.m_delete.device_id); | |
1406 | break; | |
1407 | case DM_THIN_MESSAGE_SET_TRANSACTION_ID: | |
1408 | r = dm_snprintf(buf, sizeof(buf), | |
1409 | "set_transaction_id %" PRIu64 " %" PRIu64, | |
1410 | m->u.m_set_transaction_id.current_id, | |
1411 | m->u.m_set_transaction_id.new_id); | |
1412 | break; | |
1413 | default: | |
1414 | r = -1; | |
1415 | } | |
1416 | ||
1417 | if (r < 0) { | |
1418 | log_error("Failed to prepare message."); | |
1419 | return 0; | |
1420 | } | |
1421 | ||
1422 | r = 0; | |
1423 | ||
1424 | if (!(dmt = dm_task_create(DM_DEVICE_TARGET_MSG))) | |
1425 | return_0; | |
1426 | ||
1427 | if (!dm_task_set_major(dmt, dnode->info.major) || | |
1428 | !dm_task_set_minor(dmt, dnode->info.minor)) { | |
1429 | log_error("Failed to set message major minor."); | |
1430 | goto out; | |
1431 | } | |
1432 | ||
1433 | if (!dm_task_set_message(dmt, buf)) | |
1434 | goto_out; | |
1435 | ||
1436 | /* Internal functionality of dm_task */ | |
1437 | dmt->expected_errno = tm->expected_errno; | |
1438 | ||
1439 | if (!dm_task_run(dmt)) | |
1440 | goto_out; | |
1441 | ||
1442 | r = 1; | |
1443 | out: | |
1444 | dm_task_destroy(dmt); | |
1445 | ||
1446 | return r; | |
1447 | } | |
1448 | ||
1449 | static int _node_send_messages(struct dm_tree_node *dnode, | |
1450 | const char *uuid_prefix, | |
1451 | size_t uuid_prefix_len) | |
1452 | { | |
1453 | struct load_segment *seg; | |
1454 | struct thin_message *tmsg; | |
1455 | uint64_t trans_id; | |
1456 | const char *uuid; | |
1457 | ||
1458 | if (!dnode->info.exists || (dm_list_size(&dnode->props.segs) != 1)) | |
1459 | return 1; | |
1460 | ||
1461 | seg = dm_list_item(dm_list_last(&dnode->props.segs), struct load_segment); | |
1462 | if (seg->type != SEG_THIN_POOL) | |
1463 | return 1; | |
1464 | ||
1465 | if (!(uuid = dm_tree_node_get_uuid(dnode))) | |
1466 | return_0; | |
1467 | ||
1468 | if (!_uuid_prefix_matches(uuid, uuid_prefix, uuid_prefix_len)) { | |
1469 | log_debug("UUID \"%s\" does not match.", uuid); | |
1470 | return 1; | |
1471 | } | |
1472 | ||
1473 | if (!_thin_pool_status_transaction_id(dnode, &trans_id)) | |
1474 | goto_bad; | |
1475 | ||
1476 | if (trans_id == seg->transaction_id) | |
1477 | return 1; /* In sync - skip messages */ | |
1478 | ||
1479 | if (trans_id != (seg->transaction_id - 1)) { | |
1480 | log_error("Thin pool transaction_id=%" PRIu64 ", while expected: %" PRIu64 ".", | |
1481 | trans_id, seg->transaction_id - 1); | |
1482 | goto bad; /* Nothing to send */ | |
1483 | } | |
1484 | ||
1485 | dm_list_iterate_items(tmsg, &seg->thin_messages) | |
1486 | if (!(_thin_pool_node_message(dnode, tmsg))) | |
1487 | goto_bad; | |
1488 | ||
1489 | return 1; | |
1490 | bad: | |
1491 | /* Try to deactivate */ | |
1492 | if (!(dm_tree_deactivate_children(dnode, uuid_prefix, uuid_prefix_len))) | |
1493 | log_error("Failed to deactivate %s", dnode->name); | |
1494 | ||
1495 | return 0; | |
1496 | } | |
1497 | ||
1498 | /* | |
1499 | * FIXME Don't attempt to deactivate known internal dependencies. | |
1500 | */ | |
1501 | static int _dm_tree_deactivate_children(struct dm_tree_node *dnode, | |
1502 | const char *uuid_prefix, | |
1503 | size_t uuid_prefix_len, | |
1504 | unsigned level) | |
1505 | { | |
1506 | int r = 1; | |
1507 | void *handle = NULL; | |
1508 | struct dm_tree_node *child = dnode; | |
1509 | struct dm_info info; | |
1510 | const struct dm_info *dinfo; | |
1511 | const char *name; | |
1512 | const char *uuid; | |
1513 | ||
1514 | while ((child = dm_tree_next_child(&handle, dnode, 0))) { | |
1515 | if (!(dinfo = dm_tree_node_get_info(child))) { | |
1516 | stack; | |
1517 | continue; | |
1518 | } | |
1519 | ||
1520 | if (!(name = dm_tree_node_get_name(child))) { | |
1521 | stack; | |
1522 | continue; | |
1523 | } | |
1524 | ||
1525 | if (!(uuid = dm_tree_node_get_uuid(child))) { | |
1526 | stack; | |
1527 | continue; | |
1528 | } | |
1529 | ||
1530 | /* Ignore if it doesn't belong to this VG */ | |
1531 | if (!_uuid_prefix_matches(uuid, uuid_prefix, uuid_prefix_len)) | |
1532 | continue; | |
1533 | ||
1534 | /* Refresh open_count */ | |
1535 | if (!_info_by_dev(dinfo->major, dinfo->minor, 1, &info, NULL, NULL, NULL) || | |
1536 | !info.exists) | |
1537 | continue; | |
1538 | ||
1539 | if (info.open_count) { | |
1540 | /* Skip internal non-toplevel opened nodes */ | |
1541 | if (level) | |
1542 | continue; | |
1543 | ||
1544 | /* When retry is not allowed, error */ | |
1545 | if (!child->dtree->retry_remove) { | |
1546 | log_error("Unable to deactivate open %s (%" PRIu32 | |
1547 | ":%" PRIu32 ")", name, info.major, info.minor); | |
1548 | r = 0; | |
1549 | continue; | |
1550 | } | |
1551 | ||
1552 | /* Check toplevel node for holders/mounted fs */ | |
1553 | if (!_check_device_not_in_use(name, &info)) { | |
1554 | stack; | |
1555 | r = 0; | |
1556 | continue; | |
1557 | } | |
1558 | /* Go on with retry */ | |
1559 | } | |
1560 | ||
1561 | /* Also checking open_count in parent nodes of presuspend_node */ | |
1562 | if ((child->presuspend_node && | |
1563 | !_node_has_closed_parents(child->presuspend_node, | |
1564 | uuid_prefix, uuid_prefix_len))) { | |
1565 | /* Only report error from (likely non-internal) dependency at top level */ | |
1566 | if (!level) { | |
1567 | log_error("Unable to deactivate open %s (%" PRIu32 | |
1568 | ":%" PRIu32 ")", name, info.major, | |
1569 | info.minor); | |
1570 | r = 0; | |
1571 | } | |
1572 | continue; | |
1573 | } | |
1574 | ||
1575 | /* Suspend child node first if requested */ | |
1576 | if (child->presuspend_node && | |
1577 | !dm_tree_suspend_children(child, uuid_prefix, uuid_prefix_len)) | |
1578 | continue; | |
1579 | ||
1580 | if (!_deactivate_node(name, info.major, info.minor, | |
1581 | &child->dtree->cookie, child->udev_flags, | |
1582 | (level == 0) ? child->dtree->retry_remove : 0)) { | |
1583 | log_error("Unable to deactivate %s (%" PRIu32 | |
1584 | ":%" PRIu32 ")", name, info.major, | |
1585 | info.minor); | |
1586 | r = 0; | |
1587 | continue; | |
1588 | } else if (info.suspended) | |
1589 | dec_suspended(); | |
1590 | ||
1591 | if (child->callback && | |
1592 | !child->callback(child, DM_NODE_CALLBACK_DEACTIVATED, | |
1593 | child->callback_data)) | |
1594 | stack; | |
1595 | // FIXME: We need to let lvremove pass, | |
1596 | // so for now deactivation ignores check result | |
1597 | //r = 0; // FIXME: _node_clear_table() without callback ? | |
1598 | ||
1599 | if (dm_tree_node_num_children(child, 0) && | |
1600 | !_dm_tree_deactivate_children(child, uuid_prefix, uuid_prefix_len, level + 1)) | |
1601 | return_0; | |
1602 | } | |
1603 | ||
1604 | return r; | |
1605 | } | |
1606 | ||
1607 | int dm_tree_deactivate_children(struct dm_tree_node *dnode, | |
1608 | const char *uuid_prefix, | |
1609 | size_t uuid_prefix_len) | |
1610 | { | |
1611 | return _dm_tree_deactivate_children(dnode, uuid_prefix, uuid_prefix_len, 0); | |
1612 | } | |
1613 | ||
1614 | int dm_tree_suspend_children(struct dm_tree_node *dnode, | |
1615 | const char *uuid_prefix, | |
1616 | size_t uuid_prefix_len) | |
1617 | { | |
1618 | int r = 1; | |
1619 | void *handle = NULL; | |
1620 | struct dm_tree_node *child = dnode; | |
1621 | struct dm_info info, newinfo; | |
1622 | const struct dm_info *dinfo; | |
1623 | const char *name; | |
1624 | const char *uuid; | |
1625 | ||
1626 | /* Suspend nodes at this level of the tree */ | |
1627 | while ((child = dm_tree_next_child(&handle, dnode, 0))) { | |
1628 | if (!(dinfo = dm_tree_node_get_info(child))) { | |
1629 | stack; | |
1630 | continue; | |
1631 | } | |
1632 | ||
1633 | if (!(name = dm_tree_node_get_name(child))) { | |
1634 | stack; | |
1635 | continue; | |
1636 | } | |
1637 | ||
1638 | if (!(uuid = dm_tree_node_get_uuid(child))) { | |
1639 | stack; | |
1640 | continue; | |
1641 | } | |
1642 | ||
1643 | /* Ignore if it doesn't belong to this VG */ | |
1644 | if (!_uuid_prefix_matches(uuid, uuid_prefix, uuid_prefix_len)) | |
1645 | continue; | |
1646 | ||
1647 | /* Ensure immediate parents are already suspended */ | |
1648 | if (!_children_suspended(child, 1, uuid_prefix, uuid_prefix_len)) | |
1649 | continue; | |
1650 | ||
1651 | if (!_info_by_dev(dinfo->major, dinfo->minor, 0, &info, NULL, NULL, NULL) || | |
1652 | !info.exists || info.suspended) | |
1653 | continue; | |
1654 | ||
1655 | if (!_suspend_node(name, info.major, info.minor, | |
1656 | child->dtree->skip_lockfs, | |
1657 | child->dtree->no_flush, &newinfo)) { | |
1658 | log_error("Unable to suspend %s (%" PRIu32 | |
1659 | ":%" PRIu32 ")", name, info.major, | |
1660 | info.minor); | |
1661 | r = 0; | |
1662 | continue; | |
1663 | } | |
1664 | ||
1665 | /* Update cached info */ | |
1666 | child->info = newinfo; | |
1667 | } | |
1668 | ||
1669 | /* Then suspend any child nodes */ | |
1670 | handle = NULL; | |
1671 | ||
1672 | while ((child = dm_tree_next_child(&handle, dnode, 0))) { | |
1673 | if (!(uuid = dm_tree_node_get_uuid(child))) { | |
1674 | stack; | |
1675 | continue; | |
1676 | } | |
1677 | ||
1678 | /* Ignore if it doesn't belong to this VG */ | |
1679 | if (!_uuid_prefix_matches(uuid, uuid_prefix, uuid_prefix_len)) | |
1680 | continue; | |
1681 | ||
1682 | if (dm_tree_node_num_children(child, 0)) | |
1683 | if (!dm_tree_suspend_children(child, uuid_prefix, uuid_prefix_len)) | |
1684 | return_0; | |
1685 | } | |
1686 | ||
1687 | return r; | |
1688 | } | |
1689 | ||
1690 | int dm_tree_activate_children(struct dm_tree_node *dnode, | |
1691 | const char *uuid_prefix, | |
1692 | size_t uuid_prefix_len) | |
1693 | { | |
1694 | int r = 1; | |
1695 | void *handle = NULL; | |
1696 | struct dm_tree_node *child = dnode; | |
1697 | struct dm_info newinfo; | |
1698 | const char *name; | |
1699 | const char *uuid; | |
1700 | int priority; | |
1701 | ||
1702 | /* Activate children first */ | |
1703 | while ((child = dm_tree_next_child(&handle, dnode, 0))) { | |
1704 | if (!(uuid = dm_tree_node_get_uuid(child))) { | |
1705 | stack; | |
1706 | continue; | |
1707 | } | |
1708 | ||
1709 | if (!_uuid_prefix_matches(uuid, uuid_prefix, uuid_prefix_len)) | |
1710 | continue; | |
1711 | ||
1712 | if (dm_tree_node_num_children(child, 0)) | |
1713 | if (!dm_tree_activate_children(child, uuid_prefix, uuid_prefix_len)) | |
1714 | return_0; | |
1715 | } | |
1716 | ||
1717 | handle = NULL; | |
1718 | ||
1719 | for (priority = 0; priority < 3; priority++) { | |
1720 | while ((child = dm_tree_next_child(&handle, dnode, 0))) { | |
1721 | if (priority != child->activation_priority) | |
1722 | continue; | |
1723 | ||
1724 | if (!(uuid = dm_tree_node_get_uuid(child))) { | |
1725 | stack; | |
1726 | continue; | |
1727 | } | |
1728 | ||
1729 | if (!_uuid_prefix_matches(uuid, uuid_prefix, uuid_prefix_len)) | |
1730 | continue; | |
1731 | ||
1732 | if (!(name = dm_tree_node_get_name(child))) { | |
1733 | stack; | |
1734 | continue; | |
1735 | } | |
1736 | ||
1737 | /* Rename? */ | |
1738 | if (child->props.new_name) { | |
1739 | if (!_rename_node(name, child->props.new_name, child->info.major, | |
1740 | child->info.minor, &child->dtree->cookie, | |
1741 | child->udev_flags)) { | |
1742 | log_error("Failed to rename %s (%" PRIu32 | |
1743 | ":%" PRIu32 ") to %s", name, child->info.major, | |
1744 | child->info.minor, child->props.new_name); | |
1745 | return 0; | |
1746 | } | |
1747 | child->name = child->props.new_name; | |
1748 | child->props.new_name = NULL; | |
1749 | } | |
1750 | ||
1751 | if (!child->info.inactive_table && !child->info.suspended) | |
1752 | continue; | |
1753 | ||
1754 | if (!_resume_node(child->name, child->info.major, child->info.minor, | |
1755 | child->props.read_ahead, child->props.read_ahead_flags, | |
1756 | &newinfo, &child->dtree->cookie, child->udev_flags, child->info.suspended)) { | |
1757 | log_error("Unable to resume %s (%" PRIu32 | |
1758 | ":%" PRIu32 ")", child->name, child->info.major, | |
1759 | child->info.minor); | |
1760 | r = 0; | |
1761 | continue; | |
1762 | } | |
1763 | ||
1764 | /* Update cached info */ | |
1765 | child->info = newinfo; | |
1766 | } | |
1767 | } | |
1768 | ||
1769 | /* | |
1770 | * FIXME: Implement delayed error reporting | |
1771 | * activation should be stopped only in the case, | |
1772 | * the submission of transation_id message fails, | |
1773 | * resume should continue further, just whole command | |
1774 | * has to report failure. | |
1775 | */ | |
1776 | if (r && dnode->props.send_messages && | |
1777 | !(r = _node_send_messages(dnode, uuid_prefix, uuid_prefix_len))) | |
1778 | stack; | |
1779 | ||
1780 | handle = NULL; | |
1781 | ||
1782 | return r; | |
1783 | } | |
1784 | ||
1785 | static int _create_node(struct dm_tree_node *dnode) | |
1786 | { | |
1787 | int r = 0; | |
1788 | struct dm_task *dmt; | |
1789 | ||
1790 | log_verbose("Creating %s", dnode->name); | |
1791 | ||
1792 | if (!(dmt = dm_task_create(DM_DEVICE_CREATE))) { | |
1793 | log_error("Create dm_task creation failed for %s", dnode->name); | |
1794 | return 0; | |
1795 | } | |
1796 | ||
1797 | if (!dm_task_set_name(dmt, dnode->name)) { | |
1798 | log_error("Failed to set device name for %s", dnode->name); | |
1799 | goto out; | |
1800 | } | |
1801 | ||
1802 | if (!dm_task_set_uuid(dmt, dnode->uuid)) { | |
1803 | log_error("Failed to set uuid for %s", dnode->name); | |
1804 | goto out; | |
1805 | } | |
1806 | ||
1807 | if (dnode->props.major && | |
1808 | (!dm_task_set_major(dmt, dnode->props.major) || | |
1809 | !dm_task_set_minor(dmt, dnode->props.minor))) { | |
1810 | log_error("Failed to set device number for %s creation.", dnode->name); | |
1811 | goto out; | |
1812 | } | |
1813 | ||
1814 | if (dnode->props.read_only && !dm_task_set_ro(dmt)) { | |
1815 | log_error("Failed to set read only flag for %s", dnode->name); | |
1816 | goto out; | |
1817 | } | |
1818 | ||
1819 | if (!dm_task_no_open_count(dmt)) | |
1820 | log_error("Failed to disable open_count"); | |
1821 | ||
1822 | if ((r = dm_task_run(dmt))) | |
1823 | r = dm_task_get_info(dmt, &dnode->info); | |
1824 | ||
1825 | out: | |
1826 | dm_task_destroy(dmt); | |
1827 | ||
1828 | return r; | |
1829 | } | |
1830 | ||
1831 | ||
1832 | static int _build_dev_string(char *devbuf, size_t bufsize, struct dm_tree_node *node) | |
1833 | { | |
1834 | if (!dm_format_dev(devbuf, bufsize, node->info.major, node->info.minor)) { | |
1835 | log_error("Failed to format %s device number for %s as dm " | |
1836 | "target (%u,%u)", | |
1837 | node->name, node->uuid, node->info.major, node->info.minor); | |
1838 | return 0; | |
1839 | } | |
1840 | ||
1841 | return 1; | |
1842 | } | |
1843 | ||
1844 | /* simplify string emiting code */ | |
1845 | #define EMIT_PARAMS(p, str...)\ | |
1846 | do {\ | |
1847 | int w;\ | |
1848 | if ((w = dm_snprintf(params + p, paramsize - (size_t) p, str)) < 0) {\ | |
1849 | stack; /* Out of space */\ | |
1850 | return -1;\ | |
1851 | }\ | |
1852 | p += w;\ | |
1853 | } while (0) | |
1854 | ||
1855 | /* | |
1856 | * _emit_areas_line | |
1857 | * | |
1858 | * Returns: 1 on success, 0 on failure | |
1859 | */ | |
1860 | static int _emit_areas_line(struct dm_task *dmt __attribute__((unused)), | |
1861 | struct load_segment *seg, char *params, | |
1862 | size_t paramsize, int *pos) | |
1863 | { | |
1864 | struct seg_area *area; | |
1865 | char devbuf[DM_FORMAT_DEV_BUFSIZE]; | |
1866 | unsigned first_time = 1; | |
1867 | const char *logtype, *synctype; | |
1868 | unsigned log_parm_count; | |
1869 | ||
1870 | dm_list_iterate_items(area, &seg->areas) { | |
1871 | switch (seg->type) { | |
1872 | case SEG_REPLICATOR_DEV: | |
1873 | if (!_build_dev_string(devbuf, sizeof(devbuf), area->dev_node)) | |
1874 | return_0; | |
1875 | ||
1876 | EMIT_PARAMS(*pos, " %d 1 %s", area->rsite_index, devbuf); | |
1877 | if (first_time) | |
1878 | EMIT_PARAMS(*pos, " nolog 0"); | |
1879 | else { | |
1880 | /* Remote devices */ | |
1881 | log_parm_count = (area->flags & | |
1882 | (DM_NOSYNC | DM_FORCESYNC)) ? 2 : 1; | |
1883 | ||
1884 | if (!area->slog) { | |
1885 | devbuf[0] = 0; /* Only core log parameters */ | |
1886 | logtype = "core"; | |
1887 | } else { | |
1888 | devbuf[0] = ' '; /* Extra space before device name */ | |
1889 | if (!_build_dev_string(devbuf + 1, | |
1890 | sizeof(devbuf) - 1, | |
1891 | area->slog)) | |
1892 | return_0; | |
1893 | logtype = "disk"; | |
1894 | log_parm_count++; /* Extra sync log device name parameter */ | |
1895 | } | |
1896 | ||
1897 | EMIT_PARAMS(*pos, " %s %u%s %" PRIu64, logtype, | |
1898 | log_parm_count, devbuf, area->region_size); | |
1899 | ||
1900 | synctype = (area->flags & DM_NOSYNC) ? | |
1901 | " nosync" : (area->flags & DM_FORCESYNC) ? | |
1902 | " sync" : NULL; | |
1903 | ||
1904 | if (synctype) | |
1905 | EMIT_PARAMS(*pos, "%s", synctype); | |
1906 | } | |
1907 | break; | |
1908 | case SEG_RAID1: | |
1909 | case SEG_RAID4: | |
1910 | case SEG_RAID5_LA: | |
1911 | case SEG_RAID5_RA: | |
1912 | case SEG_RAID5_LS: | |
1913 | case SEG_RAID5_RS: | |
1914 | case SEG_RAID6_ZR: | |
1915 | case SEG_RAID6_NR: | |
1916 | case SEG_RAID6_NC: | |
1917 | if (!area->dev_node) { | |
1918 | EMIT_PARAMS(*pos, " -"); | |
1919 | break; | |
1920 | } | |
1921 | if (!_build_dev_string(devbuf, sizeof(devbuf), area->dev_node)) | |
1922 | return_0; | |
1923 | ||
1924 | EMIT_PARAMS(*pos, " %s", devbuf); | |
1925 | break; | |
1926 | default: | |
1927 | if (!_build_dev_string(devbuf, sizeof(devbuf), area->dev_node)) | |
1928 | return_0; | |
1929 | ||
1930 | EMIT_PARAMS(*pos, "%s%s %" PRIu64, first_time ? "" : " ", | |
1931 | devbuf, area->offset); | |
1932 | } | |
1933 | ||
1934 | first_time = 0; | |
1935 | } | |
1936 | ||
1937 | return 1; | |
1938 | } | |
1939 | ||
1940 | static int _replicator_emit_segment_line(const struct load_segment *seg, char *params, | |
1941 | size_t paramsize, int *pos) | |
1942 | { | |
1943 | const struct load_segment *rlog_seg; | |
1944 | struct replicator_site *rsite; | |
1945 | char rlogbuf[DM_FORMAT_DEV_BUFSIZE]; | |
1946 | unsigned parm_count; | |
1947 | ||
1948 | if (!seg->log || !_build_dev_string(rlogbuf, sizeof(rlogbuf), seg->log)) | |
1949 | return_0; | |
1950 | ||
1951 | rlog_seg = dm_list_item(dm_list_last(&seg->log->props.segs), | |
1952 | struct load_segment); | |
1953 | ||
1954 | EMIT_PARAMS(*pos, "%s 4 %s 0 auto %" PRIu64, | |
1955 | seg->rlog_type, rlogbuf, rlog_seg->size); | |
1956 | ||
1957 | dm_list_iterate_items(rsite, &seg->rsites) { | |
1958 | parm_count = (rsite->fall_behind_data | |
1959 | || rsite->fall_behind_ios | |
1960 | || rsite->async_timeout) ? 4 : 2; | |
1961 | ||
1962 | EMIT_PARAMS(*pos, " blockdev %u %u %s", parm_count, rsite->rsite_index, | |
1963 | (rsite->mode == DM_REPLICATOR_SYNC) ? "synchronous" : "asynchronous"); | |
1964 | ||
1965 | if (rsite->fall_behind_data) | |
1966 | EMIT_PARAMS(*pos, " data %" PRIu64, rsite->fall_behind_data); | |
1967 | else if (rsite->fall_behind_ios) | |
1968 | EMIT_PARAMS(*pos, " ios %" PRIu32, rsite->fall_behind_ios); | |
1969 | else if (rsite->async_timeout) | |
1970 | EMIT_PARAMS(*pos, " timeout %" PRIu32, rsite->async_timeout); | |
1971 | } | |
1972 | ||
1973 | return 1; | |
1974 | } | |
1975 | ||
1976 | /* | |
1977 | * Returns: 1 on success, 0 on failure | |
1978 | */ | |
1979 | static int _mirror_emit_segment_line(struct dm_task *dmt, struct load_segment *seg, | |
1980 | char *params, size_t paramsize) | |
1981 | { | |
1982 | int block_on_error = 0; | |
1983 | int handle_errors = 0; | |
1984 | int dm_log_userspace = 0; | |
1985 | struct utsname uts; | |
1986 | unsigned log_parm_count; | |
1987 | int pos = 0, parts; | |
1988 | char logbuf[DM_FORMAT_DEV_BUFSIZE]; | |
1989 | const char *logtype; | |
1990 | unsigned kmaj = 0, kmin = 0, krel = 0; | |
1991 | ||
1992 | if (uname(&uts) == -1) { | |
1993 | log_error("Cannot read kernel release version."); | |
1994 | return 0; | |
1995 | } | |
1996 | ||
1997 | /* Kernels with a major number of 2 always had 3 parts. */ | |
1998 | parts = sscanf(uts.release, "%u.%u.%u", &kmaj, &kmin, &krel); | |
1999 | if (parts < 1 || (kmaj < 3 && parts < 3)) { | |
2000 | log_error("Wrong kernel release version %s.", uts.release); | |
2001 | return 0; | |
2002 | } | |
2003 | ||
2004 | if ((seg->flags & DM_BLOCK_ON_ERROR)) { | |
2005 | /* | |
2006 | * Originally, block_on_error was an argument to the log | |
2007 | * portion of the mirror CTR table. It was renamed to | |
2008 | * "handle_errors" and now resides in the 'features' | |
2009 | * section of the mirror CTR table (i.e. at the end). | |
2010 | * | |
2011 | * We can identify whether to use "block_on_error" or | |
2012 | * "handle_errors" by the dm-mirror module's version | |
2013 | * number (>= 1.12) or by the kernel version (>= 2.6.22). | |
2014 | */ | |
2015 | if (KERNEL_VERSION(kmaj, kmin, krel) >= KERNEL_VERSION(2, 6, 22)) | |
2016 | handle_errors = 1; | |
2017 | else | |
2018 | block_on_error = 1; | |
2019 | } | |
2020 | ||
2021 | if (seg->clustered) { | |
2022 | /* Cluster mirrors require a UUID */ | |
2023 | if (!seg->uuid) | |
2024 | return_0; | |
2025 | ||
2026 | /* | |
2027 | * Cluster mirrors used to have their own log | |
2028 | * types. Now they are accessed through the | |
2029 | * userspace log type. | |
2030 | * | |
2031 | * The dm-log-userspace module was added to the | |
2032 | * 2.6.31 kernel. | |
2033 | */ | |
2034 | if (KERNEL_VERSION(kmaj, kmin, krel) >= KERNEL_VERSION(2, 6, 31)) | |
2035 | dm_log_userspace = 1; | |
2036 | } | |
2037 | ||
2038 | /* Region size */ | |
2039 | log_parm_count = 1; | |
2040 | ||
2041 | /* [no]sync, block_on_error etc. */ | |
2042 | log_parm_count += hweight32(seg->flags); | |
2043 | ||
2044 | /* "handle_errors" is a feature arg now */ | |
2045 | if (handle_errors) | |
2046 | log_parm_count--; | |
2047 | ||
2048 | /* DM_CORELOG does not count in the param list */ | |
2049 | if (seg->flags & DM_CORELOG) | |
2050 | log_parm_count--; | |
2051 | ||
2052 | if (seg->clustered) { | |
2053 | log_parm_count++; /* For UUID */ | |
2054 | ||
2055 | if (!dm_log_userspace) | |
2056 | EMIT_PARAMS(pos, "clustered-"); | |
2057 | else | |
2058 | /* For clustered-* type field inserted later */ | |
2059 | log_parm_count++; | |
2060 | } | |
2061 | ||
2062 | if (!seg->log) | |
2063 | logtype = "core"; | |
2064 | else { | |
2065 | logtype = "disk"; | |
2066 | log_parm_count++; | |
2067 | if (!_build_dev_string(logbuf, sizeof(logbuf), seg->log)) | |
2068 | return_0; | |
2069 | } | |
2070 | ||
2071 | if (dm_log_userspace) | |
2072 | EMIT_PARAMS(pos, "userspace %u %s clustered-%s", | |
2073 | log_parm_count, seg->uuid, logtype); | |
2074 | else | |
2075 | EMIT_PARAMS(pos, "%s %u", logtype, log_parm_count); | |
2076 | ||
2077 | if (seg->log) | |
2078 | EMIT_PARAMS(pos, " %s", logbuf); | |
2079 | ||
2080 | EMIT_PARAMS(pos, " %u", seg->region_size); | |
2081 | ||
2082 | if (seg->clustered && !dm_log_userspace) | |
2083 | EMIT_PARAMS(pos, " %s", seg->uuid); | |
2084 | ||
2085 | if ((seg->flags & DM_NOSYNC)) | |
2086 | EMIT_PARAMS(pos, " nosync"); | |
2087 | else if ((seg->flags & DM_FORCESYNC)) | |
2088 | EMIT_PARAMS(pos, " sync"); | |
2089 | ||
2090 | if (block_on_error) | |
2091 | EMIT_PARAMS(pos, " block_on_error"); | |
2092 | ||
2093 | EMIT_PARAMS(pos, " %u ", seg->mirror_area_count); | |
2094 | ||
2095 | if (_emit_areas_line(dmt, seg, params, paramsize, &pos) <= 0) | |
2096 | return_0; | |
2097 | ||
2098 | if (handle_errors) | |
2099 | EMIT_PARAMS(pos, " 1 handle_errors"); | |
2100 | ||
2101 | return 1; | |
2102 | } | |
2103 | ||
2104 | static int _raid_emit_segment_line(struct dm_task *dmt, uint32_t major, | |
2105 | uint32_t minor, struct load_segment *seg, | |
2106 | uint64_t *seg_start, char *params, | |
2107 | size_t paramsize) | |
2108 | { | |
2109 | uint32_t i; | |
2110 | int param_count = 1; /* mandatory 'chunk size'/'stripe size' arg */ | |
2111 | int pos = 0; | |
2112 | ||
2113 | if ((seg->flags & DM_NOSYNC) || (seg->flags & DM_FORCESYNC)) | |
2114 | param_count++; | |
2115 | ||
2116 | if (seg->region_size) | |
2117 | param_count += 2; | |
2118 | ||
2119 | /* rebuilds is 64-bit */ | |
2120 | param_count += 2 * hweight32(seg->rebuilds & 0xFFFFFFFF); | |
2121 | param_count += 2 * hweight32(seg->rebuilds >> 32); | |
2122 | ||
2123 | if ((seg->type == SEG_RAID1) && seg->stripe_size) | |
2124 | log_error("WARNING: Ignoring RAID1 stripe size"); | |
2125 | ||
2126 | EMIT_PARAMS(pos, "%s %d %u", dm_segtypes[seg->type].target, | |
2127 | param_count, seg->stripe_size); | |
2128 | ||
2129 | if (seg->flags & DM_NOSYNC) | |
2130 | EMIT_PARAMS(pos, " nosync"); | |
2131 | else if (seg->flags & DM_FORCESYNC) | |
2132 | EMIT_PARAMS(pos, " sync"); | |
2133 | ||
2134 | if (seg->region_size) | |
2135 | EMIT_PARAMS(pos, " region_size %u", seg->region_size); | |
2136 | ||
2137 | for (i = 0; i < (seg->area_count / 2); i++) | |
2138 | if (seg->rebuilds & (1 << i)) | |
2139 | EMIT_PARAMS(pos, " rebuild %u", i); | |
2140 | ||
2141 | /* Print number of metadata/data device pairs */ | |
2142 | EMIT_PARAMS(pos, " %u", seg->area_count/2); | |
2143 | ||
2144 | if (_emit_areas_line(dmt, seg, params, paramsize, &pos) <= 0) | |
2145 | return_0; | |
2146 | ||
2147 | return 1; | |
2148 | } | |
2149 | ||
2150 | static int _emit_segment_line(struct dm_task *dmt, uint32_t major, | |
2151 | uint32_t minor, struct load_segment *seg, | |
2152 | uint64_t *seg_start, char *params, | |
2153 | size_t paramsize) | |
2154 | { | |
2155 | int pos = 0; | |
2156 | int r; | |
2157 | int target_type_is_raid = 0; | |
2158 | char originbuf[DM_FORMAT_DEV_BUFSIZE], cowbuf[DM_FORMAT_DEV_BUFSIZE]; | |
2159 | char pool[DM_FORMAT_DEV_BUFSIZE], metadata[DM_FORMAT_DEV_BUFSIZE]; | |
2160 | ||
2161 | switch(seg->type) { | |
2162 | case SEG_ERROR: | |
2163 | case SEG_ZERO: | |
2164 | case SEG_LINEAR: | |
2165 | break; | |
2166 | case SEG_MIRRORED: | |
2167 | /* Mirrors are pretty complicated - now in separate function */ | |
2168 | r = _mirror_emit_segment_line(dmt, seg, params, paramsize); | |
2169 | if (!r) | |
2170 | return_0; | |
2171 | break; | |
2172 | case SEG_REPLICATOR: | |
2173 | if ((r = _replicator_emit_segment_line(seg, params, paramsize, | |
2174 | &pos)) <= 0) { | |
2175 | stack; | |
2176 | return r; | |
2177 | } | |
2178 | break; | |
2179 | case SEG_REPLICATOR_DEV: | |
2180 | if (!seg->replicator || !_build_dev_string(originbuf, | |
2181 | sizeof(originbuf), | |
2182 | seg->replicator)) | |
2183 | return_0; | |
2184 | ||
2185 | EMIT_PARAMS(pos, "%s %" PRIu64, originbuf, seg->rdevice_index); | |
2186 | break; | |
2187 | case SEG_SNAPSHOT: | |
2188 | case SEG_SNAPSHOT_MERGE: | |
2189 | if (!_build_dev_string(originbuf, sizeof(originbuf), seg->origin)) | |
2190 | return_0; | |
2191 | if (!_build_dev_string(cowbuf, sizeof(cowbuf), seg->cow)) | |
2192 | return_0; | |
2193 | EMIT_PARAMS(pos, "%s %s %c %d", originbuf, cowbuf, | |
2194 | seg->persistent ? 'P' : 'N', seg->chunk_size); | |
2195 | break; | |
2196 | case SEG_SNAPSHOT_ORIGIN: | |
2197 | if (!_build_dev_string(originbuf, sizeof(originbuf), seg->origin)) | |
2198 | return_0; | |
2199 | EMIT_PARAMS(pos, "%s", originbuf); | |
2200 | break; | |
2201 | case SEG_STRIPED: | |
2202 | EMIT_PARAMS(pos, "%u %u ", seg->area_count, seg->stripe_size); | |
2203 | break; | |
2204 | case SEG_CRYPT: | |
2205 | EMIT_PARAMS(pos, "%s%s%s%s%s %s %" PRIu64 " ", seg->cipher, | |
2206 | seg->chainmode ? "-" : "", seg->chainmode ?: "", | |
2207 | seg->iv ? "-" : "", seg->iv ?: "", seg->key, | |
2208 | seg->iv_offset != DM_CRYPT_IV_DEFAULT ? | |
2209 | seg->iv_offset : *seg_start); | |
2210 | break; | |
2211 | case SEG_RAID1: | |
2212 | case SEG_RAID4: | |
2213 | case SEG_RAID5_LA: | |
2214 | case SEG_RAID5_RA: | |
2215 | case SEG_RAID5_LS: | |
2216 | case SEG_RAID5_RS: | |
2217 | case SEG_RAID6_ZR: | |
2218 | case SEG_RAID6_NR: | |
2219 | case SEG_RAID6_NC: | |
2220 | target_type_is_raid = 1; | |
2221 | r = _raid_emit_segment_line(dmt, major, minor, seg, seg_start, | |
2222 | params, paramsize); | |
2223 | if (!r) | |
2224 | return_0; | |
2225 | ||
2226 | break; | |
2227 | case SEG_THIN_POOL: | |
2228 | if (!_build_dev_string(metadata, sizeof(metadata), seg->metadata)) | |
2229 | return_0; | |
2230 | if (!_build_dev_string(pool, sizeof(pool), seg->pool)) | |
2231 | return_0; | |
2232 | EMIT_PARAMS(pos, "%s %s %d %" PRIu64 " %s", metadata, pool, | |
2233 | seg->data_block_size, seg->low_water_mark, | |
2234 | seg->skip_block_zeroing ? "1 skip_block_zeroing" : "0"); | |
2235 | break; | |
2236 | case SEG_THIN: | |
2237 | if (!_build_dev_string(pool, sizeof(pool), seg->pool)) | |
2238 | return_0; | |
2239 | EMIT_PARAMS(pos, "%s %d", pool, seg->device_id); | |
2240 | break; | |
2241 | } | |
2242 | ||
2243 | switch(seg->type) { | |
2244 | case SEG_ERROR: | |
2245 | case SEG_REPLICATOR: | |
2246 | case SEG_SNAPSHOT: | |
2247 | case SEG_SNAPSHOT_ORIGIN: | |
2248 | case SEG_SNAPSHOT_MERGE: | |
2249 | case SEG_ZERO: | |
2250 | case SEG_THIN_POOL: | |
2251 | case SEG_THIN: | |
2252 | break; | |
2253 | case SEG_CRYPT: | |
2254 | case SEG_LINEAR: | |
2255 | case SEG_REPLICATOR_DEV: | |
2256 | case SEG_STRIPED: | |
2257 | if ((r = _emit_areas_line(dmt, seg, params, paramsize, &pos)) <= 0) { | |
2258 | stack; | |
2259 | return r; | |
2260 | } | |
2261 | if (!params[0]) { | |
2262 | log_error("No parameters supplied for %s target " | |
2263 | "%u:%u.", dm_segtypes[seg->type].target, | |
2264 | major, minor); | |
2265 | return 0; | |
2266 | } | |
2267 | break; | |
2268 | } | |
2269 | ||
2270 | log_debug("Adding target to (%" PRIu32 ":%" PRIu32 "): %" PRIu64 | |
2271 | " %" PRIu64 " %s %s", major, minor, | |
2272 | *seg_start, seg->size, target_type_is_raid ? "raid" : | |
2273 | dm_segtypes[seg->type].target, params); | |
2274 | ||
2275 | if (!dm_task_add_target(dmt, *seg_start, seg->size, | |
2276 | target_type_is_raid ? "raid" : | |
2277 | dm_segtypes[seg->type].target, params)) | |
2278 | return_0; | |
2279 | ||
2280 | *seg_start += seg->size; | |
2281 | ||
2282 | return 1; | |
2283 | } | |
2284 | ||
2285 | #undef EMIT_PARAMS | |
2286 | ||
2287 | static int _emit_segment(struct dm_task *dmt, uint32_t major, uint32_t minor, | |
2288 | struct load_segment *seg, uint64_t *seg_start) | |
2289 | { | |
2290 | char *params; | |
2291 | size_t paramsize = 4096; | |
2292 | int ret; | |
2293 | ||
2294 | do { | |
2295 | if (!(params = dm_malloc(paramsize))) { | |
2296 | log_error("Insufficient space for target parameters."); | |
2297 | return 0; | |
2298 | } | |
2299 | ||
2300 | params[0] = '\0'; | |
2301 | ret = _emit_segment_line(dmt, major, minor, seg, seg_start, | |
2302 | params, paramsize); | |
2303 | dm_free(params); | |
2304 | ||
2305 | if (!ret) | |
2306 | stack; | |
2307 | ||
2308 | if (ret >= 0) | |
2309 | return ret; | |
2310 | ||
2311 | log_debug("Insufficient space in params[%" PRIsize_t | |
2312 | "] for target parameters.", paramsize); | |
2313 | ||
2314 | paramsize *= 2; | |
2315 | } while (paramsize < MAX_TARGET_PARAMSIZE); | |
2316 | ||
2317 | log_error("Target parameter size too big. Aborting."); | |
2318 | return 0; | |
2319 | } | |
2320 | ||
2321 | static int _load_node(struct dm_tree_node *dnode) | |
2322 | { | |
2323 | int r = 0; | |
2324 | struct dm_task *dmt; | |
2325 | struct load_segment *seg; | |
2326 | uint64_t seg_start = 0, existing_table_size; | |
2327 | ||
2328 | log_verbose("Loading %s table (%" PRIu32 ":%" PRIu32 ")", dnode->name, | |
2329 | dnode->info.major, dnode->info.minor); | |
2330 | ||
2331 | if (!(dmt = dm_task_create(DM_DEVICE_RELOAD))) { | |
2332 | log_error("Reload dm_task creation failed for %s", dnode->name); | |
2333 | return 0; | |
2334 | } | |
2335 | ||
2336 | if (!dm_task_set_major(dmt, dnode->info.major) || | |
2337 | !dm_task_set_minor(dmt, dnode->info.minor)) { | |
2338 | log_error("Failed to set device number for %s reload.", dnode->name); | |
2339 | goto out; | |
2340 | } | |
2341 | ||
2342 | if (dnode->props.read_only && !dm_task_set_ro(dmt)) { | |
2343 | log_error("Failed to set read only flag for %s", dnode->name); | |
2344 | goto out; | |
2345 | } | |
2346 | ||
2347 | if (!dm_task_no_open_count(dmt)) | |
2348 | log_error("Failed to disable open_count"); | |
2349 | ||
2350 | dm_list_iterate_items(seg, &dnode->props.segs) | |
2351 | if (!_emit_segment(dmt, dnode->info.major, dnode->info.minor, | |
2352 | seg, &seg_start)) | |
2353 | goto_out; | |
2354 | ||
2355 | if (!dm_task_suppress_identical_reload(dmt)) | |
2356 | log_error("Failed to suppress reload of identical tables."); | |
2357 | ||
2358 | if ((r = dm_task_run(dmt))) { | |
2359 | r = dm_task_get_info(dmt, &dnode->info); | |
2360 | if (r && !dnode->info.inactive_table) | |
2361 | log_verbose("Suppressed %s identical table reload.", | |
2362 | dnode->name); | |
2363 | ||
2364 | existing_table_size = dm_task_get_existing_table_size(dmt); | |
2365 | if ((dnode->props.size_changed = | |
2366 | (existing_table_size == seg_start) ? 0 : 1)) { | |
2367 | /* | |
2368 | * Kernel usually skips size validation on zero-length devices | |
2369 | * now so no need to preload them. | |
2370 | */ | |
2371 | /* FIXME In which kernel version did this begin? */ | |
2372 | if (!existing_table_size && dnode->props.delay_resume_if_new) | |
2373 | dnode->props.size_changed = 0; | |
2374 | ||
2375 | log_debug("Table size changed from %" PRIu64 " to %" | |
2376 | PRIu64 " for %s (%" PRIu32 ":%" PRIu32 ").%s", | |
2377 | existing_table_size, seg_start, dnode->name, | |
2378 | dnode->info.major, dnode->info.minor, | |
2379 | dnode->props.size_changed ? "" : " (Ignoring.)"); | |
2380 | } | |
2381 | } | |
2382 | ||
2383 | dnode->props.segment_count = 0; | |
2384 | ||
2385 | out: | |
2386 | dm_task_destroy(dmt); | |
2387 | ||
2388 | return r; | |
2389 | } | |
2390 | ||
2391 | int dm_tree_preload_children(struct dm_tree_node *dnode, | |
2392 | const char *uuid_prefix, | |
2393 | size_t uuid_prefix_len) | |
2394 | { | |
2395 | int r = 1; | |
2396 | void *handle = NULL; | |
2397 | struct dm_tree_node *child; | |
2398 | struct dm_info newinfo; | |
2399 | int update_devs_flag = 0; | |
2400 | ||
2401 | /* Preload children first */ | |
2402 | while ((child = dm_tree_next_child(&handle, dnode, 0))) { | |
2403 | /* Skip existing non-device-mapper devices */ | |
2404 | if (!child->info.exists && child->info.major) | |
2405 | continue; | |
2406 | ||
2407 | /* Ignore if it doesn't belong to this VG */ | |
2408 | if (child->info.exists && | |
2409 | !_uuid_prefix_matches(child->uuid, uuid_prefix, uuid_prefix_len)) | |
2410 | continue; | |
2411 | ||
2412 | if (dm_tree_node_num_children(child, 0)) | |
2413 | if (!dm_tree_preload_children(child, uuid_prefix, uuid_prefix_len)) | |
2414 | return_0; | |
2415 | ||
2416 | /* FIXME Cope if name exists with no uuid? */ | |
2417 | if (!child->info.exists && !_create_node(child)) | |
2418 | return_0; | |
2419 | ||
2420 | if (!child->info.inactive_table && | |
2421 | child->props.segment_count && | |
2422 | !_load_node(child)) | |
2423 | return_0; | |
2424 | ||
2425 | /* Propagate device size change change */ | |
2426 | if (child->props.size_changed) | |
2427 | dnode->props.size_changed = 1; | |
2428 | ||
2429 | /* Resume device immediately if it has parents and its size changed */ | |
2430 | if (!dm_tree_node_num_children(child, 1) || !child->props.size_changed) | |
2431 | continue; | |
2432 | ||
2433 | if (!child->info.inactive_table && !child->info.suspended) | |
2434 | continue; | |
2435 | ||
2436 | if (!_resume_node(child->name, child->info.major, child->info.minor, | |
2437 | child->props.read_ahead, child->props.read_ahead_flags, | |
2438 | &newinfo, &child->dtree->cookie, child->udev_flags, | |
2439 | child->info.suspended)) { | |
2440 | log_error("Unable to resume %s (%" PRIu32 | |
2441 | ":%" PRIu32 ")", child->name, child->info.major, | |
2442 | child->info.minor); | |
2443 | r = 0; | |
2444 | continue; | |
2445 | } | |
2446 | ||
2447 | /* Update cached info */ | |
2448 | child->info = newinfo; | |
2449 | /* | |
2450 | * Prepare for immediate synchronization with udev and flush all stacked | |
2451 | * dev node operations if requested by immediate_dev_node property. But | |
2452 | * finish processing current level in the tree first. | |
2453 | */ | |
2454 | if (child->props.immediate_dev_node) | |
2455 | update_devs_flag = 1; | |
2456 | } | |
2457 | ||
2458 | if (update_devs_flag || | |
2459 | (!dnode->info.exists && dnode->callback)) { | |
2460 | if (!dm_udev_wait(dm_tree_get_cookie(dnode))) | |
2461 | stack; | |
2462 | dm_tree_set_cookie(dnode, 0); | |
2463 | ||
2464 | if (!dnode->info.exists && dnode->callback && | |
2465 | !dnode->callback(child, DM_NODE_CALLBACK_PRELOADED, | |
2466 | dnode->callback_data)) | |
2467 | return_0; | |
2468 | } | |
2469 | ||
2470 | return r; | |
2471 | } | |
2472 | ||
2473 | /* | |
2474 | * Returns 1 if unsure. | |
2475 | */ | |
2476 | int dm_tree_children_use_uuid(struct dm_tree_node *dnode, | |
2477 | const char *uuid_prefix, | |
2478 | size_t uuid_prefix_len) | |
2479 | { | |
2480 | void *handle = NULL; | |
2481 | struct dm_tree_node *child = dnode; | |
2482 | const char *uuid; | |
2483 | ||
2484 | while ((child = dm_tree_next_child(&handle, dnode, 0))) { | |
2485 | if (!(uuid = dm_tree_node_get_uuid(child))) { | |
2486 | log_error("Failed to get uuid for dtree node."); | |
2487 | return 1; | |
2488 | } | |
2489 | ||
2490 | if (_uuid_prefix_matches(uuid, uuid_prefix, uuid_prefix_len)) | |
2491 | return 1; | |
2492 | ||
2493 | if (dm_tree_node_num_children(child, 0)) | |
2494 | dm_tree_children_use_uuid(child, uuid_prefix, uuid_prefix_len); | |
2495 | } | |
2496 | ||
2497 | return 0; | |
2498 | } | |
2499 | ||
2500 | /* | |
2501 | * Target functions | |
2502 | */ | |
2503 | static struct load_segment *_add_segment(struct dm_tree_node *dnode, unsigned type, uint64_t size) | |
2504 | { | |
2505 | struct load_segment *seg; | |
2506 | ||
2507 | if (!(seg = dm_pool_zalloc(dnode->dtree->mem, sizeof(*seg)))) { | |
2508 | log_error("dtree node segment allocation failed"); | |
2509 | return NULL; | |
2510 | } | |
2511 | ||
2512 | seg->type = type; | |
2513 | seg->size = size; | |
2514 | seg->area_count = 0; | |
2515 | dm_list_init(&seg->areas); | |
2516 | seg->stripe_size = 0; | |
2517 | seg->persistent = 0; | |
2518 | seg->chunk_size = 0; | |
2519 | seg->cow = NULL; | |
2520 | seg->origin = NULL; | |
2521 | seg->merge = NULL; | |
2522 | ||
2523 | dm_list_add(&dnode->props.segs, &seg->list); | |
2524 | dnode->props.segment_count++; | |
2525 | ||
2526 | return seg; | |
2527 | } | |
2528 | ||
2529 | int dm_tree_node_add_snapshot_origin_target(struct dm_tree_node *dnode, | |
2530 | uint64_t size, | |
2531 | const char *origin_uuid) | |
2532 | { | |
2533 | struct load_segment *seg; | |
2534 | struct dm_tree_node *origin_node; | |
2535 | ||
2536 | if (!(seg = _add_segment(dnode, SEG_SNAPSHOT_ORIGIN, size))) | |
2537 | return_0; | |
2538 | ||
2539 | if (!(origin_node = dm_tree_find_node_by_uuid(dnode->dtree, origin_uuid))) { | |
2540 | log_error("Couldn't find snapshot origin uuid %s.", origin_uuid); | |
2541 | return 0; | |
2542 | } | |
2543 | ||
2544 | seg->origin = origin_node; | |
2545 | if (!_link_tree_nodes(dnode, origin_node)) | |
2546 | return_0; | |
2547 | ||
2548 | /* Resume snapshot origins after new snapshots */ | |
2549 | dnode->activation_priority = 1; | |
2550 | ||
2551 | return 1; | |
2552 | } | |
2553 | ||
2554 | static int _add_snapshot_target(struct dm_tree_node *node, | |
2555 | uint64_t size, | |
2556 | const char *origin_uuid, | |
2557 | const char *cow_uuid, | |
2558 | const char *merge_uuid, | |
2559 | int persistent, | |
2560 | uint32_t chunk_size) | |
2561 | { | |
2562 | struct load_segment *seg; | |
2563 | struct dm_tree_node *origin_node, *cow_node, *merge_node; | |
2564 | unsigned seg_type; | |
2565 | ||
2566 | seg_type = !merge_uuid ? SEG_SNAPSHOT : SEG_SNAPSHOT_MERGE; | |
2567 | ||
2568 | if (!(seg = _add_segment(node, seg_type, size))) | |
2569 | return_0; | |
2570 | ||
2571 | if (!(origin_node = dm_tree_find_node_by_uuid(node->dtree, origin_uuid))) { | |
2572 | log_error("Couldn't find snapshot origin uuid %s.", origin_uuid); | |
2573 | return 0; | |
2574 | } | |
2575 | ||
2576 | seg->origin = origin_node; | |
2577 | if (!_link_tree_nodes(node, origin_node)) | |
2578 | return_0; | |
2579 | ||
2580 | if (!(cow_node = dm_tree_find_node_by_uuid(node->dtree, cow_uuid))) { | |
2581 | log_error("Couldn't find snapshot COW device uuid %s.", cow_uuid); | |
2582 | return 0; | |
2583 | } | |
2584 | ||
2585 | seg->cow = cow_node; | |
2586 | if (!_link_tree_nodes(node, cow_node)) | |
2587 | return_0; | |
2588 | ||
2589 | seg->persistent = persistent ? 1 : 0; | |
2590 | seg->chunk_size = chunk_size; | |
2591 | ||
2592 | if (merge_uuid) { | |
2593 | if (!(merge_node = dm_tree_find_node_by_uuid(node->dtree, merge_uuid))) { | |
2594 | /* not a pure error, merging snapshot may have been deactivated */ | |
2595 | log_verbose("Couldn't find merging snapshot uuid %s.", merge_uuid); | |
2596 | } else { | |
2597 | seg->merge = merge_node; | |
2598 | /* must not link merging snapshot, would undermine activation_priority below */ | |
2599 | } | |
2600 | ||
2601 | /* Resume snapshot-merge (acting origin) after other snapshots */ | |
2602 | node->activation_priority = 1; | |
2603 | if (seg->merge) { | |
2604 | /* Resume merging snapshot after snapshot-merge */ | |
2605 | seg->merge->activation_priority = 2; | |
2606 | } | |
2607 | } | |
2608 | ||
2609 | return 1; | |
2610 | } | |
2611 | ||
2612 | ||
2613 | int dm_tree_node_add_snapshot_target(struct dm_tree_node *node, | |
2614 | uint64_t size, | |
2615 | const char *origin_uuid, | |
2616 | const char *cow_uuid, | |
2617 | int persistent, | |
2618 | uint32_t chunk_size) | |
2619 | { | |
2620 | return _add_snapshot_target(node, size, origin_uuid, cow_uuid, | |
2621 | NULL, persistent, chunk_size); | |
2622 | } | |
2623 | ||
2624 | int dm_tree_node_add_snapshot_merge_target(struct dm_tree_node *node, | |
2625 | uint64_t size, | |
2626 | const char *origin_uuid, | |
2627 | const char *cow_uuid, | |
2628 | const char *merge_uuid, | |
2629 | uint32_t chunk_size) | |
2630 | { | |
2631 | return _add_snapshot_target(node, size, origin_uuid, cow_uuid, | |
2632 | merge_uuid, 1, chunk_size); | |
2633 | } | |
2634 | ||
2635 | int dm_tree_node_add_error_target(struct dm_tree_node *node, | |
2636 | uint64_t size) | |
2637 | { | |
2638 | if (!_add_segment(node, SEG_ERROR, size)) | |
2639 | return_0; | |
2640 | ||
2641 | return 1; | |
2642 | } | |
2643 | ||
2644 | int dm_tree_node_add_zero_target(struct dm_tree_node *node, | |
2645 | uint64_t size) | |
2646 | { | |
2647 | if (!_add_segment(node, SEG_ZERO, size)) | |
2648 | return_0; | |
2649 | ||
2650 | return 1; | |
2651 | } | |
2652 | ||
2653 | int dm_tree_node_add_linear_target(struct dm_tree_node *node, | |
2654 | uint64_t size) | |
2655 | { | |
2656 | if (!_add_segment(node, SEG_LINEAR, size)) | |
2657 | return_0; | |
2658 | ||
2659 | return 1; | |
2660 | } | |
2661 | ||
2662 | int dm_tree_node_add_striped_target(struct dm_tree_node *node, | |
2663 | uint64_t size, | |
2664 | uint32_t stripe_size) | |
2665 | { | |
2666 | struct load_segment *seg; | |
2667 | ||
2668 | if (!(seg = _add_segment(node, SEG_STRIPED, size))) | |
2669 | return_0; | |
2670 | ||
2671 | seg->stripe_size = stripe_size; | |
2672 | ||
2673 | return 1; | |
2674 | } | |
2675 | ||
2676 | int dm_tree_node_add_crypt_target(struct dm_tree_node *node, | |
2677 | uint64_t size, | |
2678 | const char *cipher, | |
2679 | const char *chainmode, | |
2680 | const char *iv, | |
2681 | uint64_t iv_offset, | |
2682 | const char *key) | |
2683 | { | |
2684 | struct load_segment *seg; | |
2685 | ||
2686 | if (!(seg = _add_segment(node, SEG_CRYPT, size))) | |
2687 | return_0; | |
2688 | ||
2689 | seg->cipher = cipher; | |
2690 | seg->chainmode = chainmode; | |
2691 | seg->iv = iv; | |
2692 | seg->iv_offset = iv_offset; | |
2693 | seg->key = key; | |
2694 | ||
2695 | return 1; | |
2696 | } | |
2697 | ||
2698 | int dm_tree_node_add_mirror_target_log(struct dm_tree_node *node, | |
2699 | uint32_t region_size, | |
2700 | unsigned clustered, | |
2701 | const char *log_uuid, | |
2702 | unsigned area_count, | |
2703 | uint32_t flags) | |
2704 | { | |
2705 | struct dm_tree_node *log_node = NULL; | |
2706 | struct load_segment *seg; | |
2707 | ||
2708 | if (!node->props.segment_count) { | |
2709 | log_error(INTERNAL_ERROR "Attempt to add target area to missing segment."); | |
2710 | return 0; | |
2711 | } | |
2712 | ||
2713 | seg = dm_list_item(dm_list_last(&node->props.segs), struct load_segment); | |
2714 | ||
2715 | if (log_uuid) { | |
2716 | if (!(seg->uuid = dm_pool_strdup(node->dtree->mem, log_uuid))) { | |
2717 | log_error("log uuid pool_strdup failed"); | |
2718 | return 0; | |
2719 | } | |
2720 | if ((flags & DM_CORELOG)) | |
2721 | /* For pvmove: immediate resume (for size validation) isn't needed. */ | |
2722 | node->props.delay_resume_if_new = 1; | |
2723 | else { | |
2724 | if (!(log_node = dm_tree_find_node_by_uuid(node->dtree, log_uuid))) { | |
2725 | log_error("Couldn't find mirror log uuid %s.", log_uuid); | |
2726 | return 0; | |
2727 | } | |
2728 | ||
2729 | if (clustered) | |
2730 | log_node->props.immediate_dev_node = 1; | |
2731 | ||
2732 | /* The kernel validates the size of disk logs. */ | |
2733 | /* FIXME Propagate to any devices below */ | |
2734 | log_node->props.delay_resume_if_new = 0; | |
2735 | ||
2736 | if (!_link_tree_nodes(node, log_node)) | |
2737 | return_0; | |
2738 | } | |
2739 | } | |
2740 | ||
2741 | seg->log = log_node; | |
2742 | seg->region_size = region_size; | |
2743 | seg->clustered = clustered; | |
2744 | seg->mirror_area_count = area_count; | |
2745 | seg->flags = flags; | |
2746 | ||
2747 | return 1; | |
2748 | } | |
2749 | ||
2750 | int dm_tree_node_add_mirror_target(struct dm_tree_node *node, | |
2751 | uint64_t size) | |
2752 | { | |
2753 | if (!_add_segment(node, SEG_MIRRORED, size)) | |
2754 | return_0; | |
2755 | ||
2756 | return 1; | |
2757 | } | |
2758 | ||
2759 | int dm_tree_node_add_raid_target(struct dm_tree_node *node, | |
2760 | uint64_t size, | |
2761 | const char *raid_type, | |
2762 | uint32_t region_size, | |
2763 | uint32_t stripe_size, | |
2764 | uint64_t rebuilds, | |
2765 | uint64_t flags) | |
2766 | { | |
2767 | int i; | |
2768 | struct load_segment *seg = NULL; | |
2769 | ||
2770 | for (i = 0; dm_segtypes[i].target && !seg; i++) | |
2771 | if (!strcmp(raid_type, dm_segtypes[i].target)) | |
2772 | if (!(seg = _add_segment(node, | |
2773 | dm_segtypes[i].type, size))) | |
2774 | return_0; | |
2775 | ||
2776 | if (!seg) | |
2777 | return_0; | |
2778 | ||
2779 | seg->region_size = region_size; | |
2780 | seg->stripe_size = stripe_size; | |
2781 | seg->area_count = 0; | |
2782 | seg->rebuilds = rebuilds; | |
2783 | seg->flags = flags; | |
2784 | ||
2785 | return 1; | |
2786 | } | |
2787 | ||
2788 | int dm_tree_node_add_replicator_target(struct dm_tree_node *node, | |
2789 | uint64_t size, | |
2790 | const char *rlog_uuid, | |
2791 | const char *rlog_type, | |
2792 | unsigned rsite_index, | |
2793 | dm_replicator_mode_t mode, | |
2794 | uint32_t async_timeout, | |
2795 | uint64_t fall_behind_data, | |
2796 | uint32_t fall_behind_ios) | |
2797 | { | |
2798 | struct load_segment *rseg; | |
2799 | struct replicator_site *rsite; | |
2800 | ||
2801 | /* Local site0 - adds replicator segment and links rlog device */ | |
2802 | if (rsite_index == REPLICATOR_LOCAL_SITE) { | |
2803 | if (node->props.segment_count) { | |
2804 | log_error(INTERNAL_ERROR "Attempt to add replicator segment to already used node."); | |
2805 | return 0; | |
2806 | } | |
2807 | ||
2808 | if (!(rseg = _add_segment(node, SEG_REPLICATOR, size))) | |
2809 | return_0; | |
2810 | ||
2811 | if (!(rseg->log = dm_tree_find_node_by_uuid(node->dtree, rlog_uuid))) { | |
2812 | log_error("Missing replicator log uuid %s.", rlog_uuid); | |
2813 | return 0; | |
2814 | } | |
2815 | ||
2816 | if (!_link_tree_nodes(node, rseg->log)) | |
2817 | return_0; | |
2818 | ||
2819 | if (strcmp(rlog_type, "ringbuffer") != 0) { | |
2820 | log_error("Unsupported replicator log type %s.", rlog_type); | |
2821 | return 0; | |
2822 | } | |
2823 | ||
2824 | if (!(rseg->rlog_type = dm_pool_strdup(node->dtree->mem, rlog_type))) | |
2825 | return_0; | |
2826 | ||
2827 | dm_list_init(&rseg->rsites); | |
2828 | rseg->rdevice_count = 0; | |
2829 | node->activation_priority = 1; | |
2830 | } | |
2831 | ||
2832 | /* Add site to segment */ | |
2833 | if (mode == DM_REPLICATOR_SYNC | |
2834 | && (async_timeout || fall_behind_ios || fall_behind_data)) { | |
2835 | log_error("Async parameters passed for synchronnous replicator."); | |
2836 | return 0; | |
2837 | } | |
2838 | ||
2839 | if (node->props.segment_count != 1) { | |
2840 | log_error(INTERNAL_ERROR "Attempt to add remote site area before setting replicator log."); | |
2841 | return 0; | |
2842 | } | |
2843 | ||
2844 | rseg = dm_list_item(dm_list_last(&node->props.segs), struct load_segment); | |
2845 | if (rseg->type != SEG_REPLICATOR) { | |
2846 | log_error(INTERNAL_ERROR "Attempt to use non replicator segment %s.", | |
2847 | dm_segtypes[rseg->type].target); | |
2848 | return 0; | |
2849 | } | |
2850 | ||
2851 | if (!(rsite = dm_pool_zalloc(node->dtree->mem, sizeof(*rsite)))) { | |
2852 | log_error("Failed to allocate remote site segment."); | |
2853 | return 0; | |
2854 | } | |
2855 | ||
2856 | dm_list_add(&rseg->rsites, &rsite->list); | |
2857 | rseg->rsite_count++; | |
2858 | ||
2859 | rsite->mode = mode; | |
2860 | rsite->async_timeout = async_timeout; | |
2861 | rsite->fall_behind_data = fall_behind_data; | |
2862 | rsite->fall_behind_ios = fall_behind_ios; | |
2863 | rsite->rsite_index = rsite_index; | |
2864 | ||
2865 | return 1; | |
2866 | } | |
2867 | ||
2868 | /* Appends device node to Replicator */ | |
2869 | int dm_tree_node_add_replicator_dev_target(struct dm_tree_node *node, | |
2870 | uint64_t size, | |
2871 | const char *replicator_uuid, | |
2872 | uint64_t rdevice_index, | |
2873 | const char *rdev_uuid, | |
2874 | unsigned rsite_index, | |
2875 | const char *slog_uuid, | |
2876 | uint32_t slog_flags, | |
2877 | uint32_t slog_region_size) | |
2878 | { | |
2879 | struct seg_area *area; | |
2880 | struct load_segment *rseg; | |
2881 | struct load_segment *rep_seg; | |
2882 | ||
2883 | if (rsite_index == REPLICATOR_LOCAL_SITE) { | |
2884 | /* Site index for local target */ | |
2885 | if (!(rseg = _add_segment(node, SEG_REPLICATOR_DEV, size))) | |
2886 | return_0; | |
2887 | ||
2888 | if (!(rseg->replicator = dm_tree_find_node_by_uuid(node->dtree, replicator_uuid))) { | |
2889 | log_error("Missing replicator uuid %s.", replicator_uuid); | |
2890 | return 0; | |
2891 | } | |
2892 | ||
2893 | /* Local slink0 for replicator must be always initialized first */ | |
2894 | if (rseg->replicator->props.segment_count != 1) { | |
2895 | log_error(INTERNAL_ERROR "Attempt to use non replicator segment."); | |
2896 | return 0; | |
2897 | } | |
2898 | ||
2899 | rep_seg = dm_list_item(dm_list_last(&rseg->replicator->props.segs), struct load_segment); | |
2900 | if (rep_seg->type != SEG_REPLICATOR) { | |
2901 | log_error(INTERNAL_ERROR "Attempt to use non replicator segment %s.", | |
2902 | dm_segtypes[rep_seg->type].target); | |
2903 | return 0; | |
2904 | } | |
2905 | rep_seg->rdevice_count++; | |
2906 | ||
2907 | if (!_link_tree_nodes(node, rseg->replicator)) | |
2908 | return_0; | |
2909 | ||
2910 | rseg->rdevice_index = rdevice_index; | |
2911 | } else { | |
2912 | /* Local slink0 for replicator must be always initialized first */ | |
2913 | if (node->props.segment_count != 1) { | |
2914 | log_error(INTERNAL_ERROR "Attempt to use non replicator-dev segment."); | |
2915 | return 0; | |
2916 | } | |
2917 | ||
2918 | rseg = dm_list_item(dm_list_last(&node->props.segs), struct load_segment); | |
2919 | if (rseg->type != SEG_REPLICATOR_DEV) { | |
2920 | log_error(INTERNAL_ERROR "Attempt to use non replicator-dev segment %s.", | |
2921 | dm_segtypes[rseg->type].target); | |
2922 | return 0; | |
2923 | } | |
2924 | } | |
2925 | ||
2926 | if (!(slog_flags & DM_CORELOG) && !slog_uuid) { | |
2927 | log_error("Unspecified sync log uuid."); | |
2928 | return 0; | |
2929 | } | |
2930 | ||
2931 | if (!dm_tree_node_add_target_area(node, NULL, rdev_uuid, 0)) | |
2932 | return_0; | |
2933 | ||
2934 | area = dm_list_item(dm_list_last(&rseg->areas), struct seg_area); | |
2935 | ||
2936 | if (!(slog_flags & DM_CORELOG)) { | |
2937 | if (!(area->slog = dm_tree_find_node_by_uuid(node->dtree, slog_uuid))) { | |
2938 | log_error("Couldn't find sync log uuid %s.", slog_uuid); | |
2939 | return 0; | |
2940 | } | |
2941 | ||
2942 | if (!_link_tree_nodes(node, area->slog)) | |
2943 | return_0; | |
2944 | } | |
2945 | ||
2946 | area->flags = slog_flags; | |
2947 | area->region_size = slog_region_size; | |
2948 | area->rsite_index = rsite_index; | |
2949 | ||
2950 | return 1; | |
2951 | } | |
2952 | ||
2953 | static int _thin_validate_device_id(uint32_t device_id) | |
2954 | { | |
2955 | if (device_id > DM_THIN_MAX_DEVICE_ID) { | |
2956 | log_error("Device id %u is higher then %u.", | |
2957 | device_id, DM_THIN_MAX_DEVICE_ID); | |
2958 | return 0; | |
2959 | } | |
2960 | ||
2961 | return 1; | |
2962 | } | |
2963 | ||
2964 | int dm_tree_node_add_thin_pool_target(struct dm_tree_node *node, | |
2965 | uint64_t size, | |
2966 | uint64_t transaction_id, | |
2967 | const char *metadata_uuid, | |
2968 | const char *pool_uuid, | |
2969 | uint32_t data_block_size, | |
2970 | uint64_t low_water_mark, | |
2971 | unsigned skip_block_zeroing) | |
2972 | { | |
2973 | struct load_segment *seg, *mseg; | |
2974 | uint64_t devsize = 0; | |
2975 | /* | |
2976 | * Max supported size for thin pool metadata device | |
2977 | * Limitation is hardcoded into kernel and bigger | |
2978 | * device size is not accepted. (16978542592) | |
2979 | */ | |
2980 | const uint64_t max_metadata_size = | |
2981 | 255ULL * (1 << 14) * (4096 / (1 << 9)) - 256 * 1024; | |
2982 | ||
2983 | if (data_block_size < DM_THIN_MIN_DATA_BLOCK_SIZE) { | |
2984 | log_error("Data block size %u is lower then %u sectors.", | |
2985 | data_block_size, DM_THIN_MIN_DATA_BLOCK_SIZE); | |
2986 | return 0; | |
2987 | } | |
2988 | ||
2989 | if (data_block_size > DM_THIN_MAX_DATA_BLOCK_SIZE) { | |
2990 | log_error("Data block size %u is higher then %u sectors.", | |
2991 | data_block_size, DM_THIN_MAX_DATA_BLOCK_SIZE); | |
2992 | return 0; | |
2993 | } | |
2994 | ||
2995 | if (!(seg = _add_segment(node, SEG_THIN_POOL, size))) | |
2996 | return_0; | |
2997 | ||
2998 | if (!(seg->metadata = dm_tree_find_node_by_uuid(node->dtree, metadata_uuid))) { | |
2999 | log_error("Missing metadata uuid %s.", metadata_uuid); | |
3000 | return 0; | |
3001 | } | |
3002 | ||
3003 | if (!_link_tree_nodes(node, seg->metadata)) | |
3004 | return_0; | |
3005 | ||
3006 | /* FIXME: more complex target may need more tweaks */ | |
3007 | dm_list_iterate_items(mseg, &seg->metadata->props.segs) { | |
3008 | devsize += mseg->size; | |
3009 | if (devsize > max_metadata_size) { | |
3010 | log_debug("Ignoring %" PRIu64 " of device.", | |
3011 | devsize - max_metadata_size); | |
3012 | mseg->size -= (devsize - max_metadata_size); | |
3013 | devsize = max_metadata_size; | |
3014 | /* FIXME: drop remaining segs */ | |
3015 | } | |
3016 | } | |
3017 | ||
3018 | if (!(seg->pool = dm_tree_find_node_by_uuid(node->dtree, pool_uuid))) { | |
3019 | log_error("Missing pool uuid %s.", pool_uuid); | |
3020 | return 0; | |
3021 | } | |
3022 | ||
3023 | if (!_link_tree_nodes(node, seg->pool)) | |
3024 | return_0; | |
3025 | ||
3026 | node->props.send_messages = 1; | |
3027 | seg->transaction_id = transaction_id; | |
3028 | seg->low_water_mark = low_water_mark; | |
3029 | seg->data_block_size = data_block_size; | |
3030 | seg->skip_block_zeroing = skip_block_zeroing; | |
3031 | dm_list_init(&seg->thin_messages); | |
3032 | ||
3033 | return 1; | |
3034 | } | |
3035 | ||
3036 | int dm_tree_node_add_thin_pool_message(struct dm_tree_node *node, | |
3037 | dm_thin_message_t type, | |
3038 | uint64_t id1, uint64_t id2) | |
3039 | { | |
3040 | struct load_segment *seg; | |
3041 | struct thin_message *tm; | |
3042 | ||
3043 | if (node->props.segment_count != 1) { | |
3044 | log_error("Thin pool node must have only one segment."); | |
3045 | return 0; | |
3046 | } | |
3047 | ||
3048 | seg = dm_list_item(dm_list_last(&node->props.segs), struct load_segment); | |
3049 | if (seg->type != SEG_THIN_POOL) { | |
3050 | log_error("Thin pool node has segment type %s.", | |
3051 | dm_segtypes[seg->type].target); | |
3052 | return 0; | |
3053 | } | |
3054 | ||
3055 | if (!(tm = dm_pool_zalloc(node->dtree->mem, sizeof (*tm)))) { | |
3056 | log_error("Failed to allocate thin message."); | |
3057 | return 0; | |
3058 | } | |
3059 | ||
3060 | switch (type) { | |
3061 | case DM_THIN_MESSAGE_CREATE_SNAP: | |
3062 | /* If the thin origin is active, it must be suspend first! */ | |
3063 | if (id1 == id2) { | |
3064 | log_error("Cannot use same device id for origin and its snapshot."); | |
3065 | return 0; | |
3066 | } | |
3067 | if (!_thin_validate_device_id(id1) || | |
3068 | !_thin_validate_device_id(id2)) | |
3069 | return_0; | |
3070 | tm->message.u.m_create_snap.device_id = id1; | |
3071 | tm->message.u.m_create_snap.origin_id = id2; | |
3072 | break; | |
3073 | case DM_THIN_MESSAGE_CREATE_THIN: | |
3074 | if (!_thin_validate_device_id(id1)) | |
3075 | return_0; | |
3076 | tm->message.u.m_create_thin.device_id = id1; | |
3077 | tm->expected_errno = EEXIST; | |
3078 | break; | |
3079 | case DM_THIN_MESSAGE_DELETE: | |
3080 | if (!_thin_validate_device_id(id1)) | |
3081 | return_0; | |
3082 | tm->message.u.m_delete.device_id = id1; | |
3083 | tm->expected_errno = ENODATA; | |
3084 | break; | |
3085 | case DM_THIN_MESSAGE_SET_TRANSACTION_ID: | |
3086 | if ((id1 + 1) != id2) { | |
3087 | log_error("New transaction id must be sequential."); | |
3088 | return 0; /* FIXME: Maybe too strict here? */ | |
3089 | } | |
3090 | if (id2 != seg->transaction_id) { | |
3091 | log_error("Current transaction id is different from thin pool."); | |
3092 | return 0; /* FIXME: Maybe too strict here? */ | |
3093 | } | |
3094 | tm->message.u.m_set_transaction_id.current_id = id1; | |
3095 | tm->message.u.m_set_transaction_id.new_id = id2; | |
3096 | break; | |
3097 | default: | |
3098 | log_error("Unsupported message type %d.", (int) type); | |
3099 | return 0; | |
3100 | } | |
3101 | ||
3102 | tm->message.type = type; | |
3103 | dm_list_add(&seg->thin_messages, &tm->list); | |
3104 | ||
3105 | return 1; | |
3106 | } | |
3107 | ||
3108 | int dm_tree_node_add_thin_target(struct dm_tree_node *node, | |
3109 | uint64_t size, | |
3110 | const char *pool_uuid, | |
3111 | uint32_t device_id) | |
3112 | { | |
3113 | struct dm_tree_node *pool; | |
3114 | struct load_segment *seg; | |
3115 | ||
3116 | if (!(pool = dm_tree_find_node_by_uuid(node->dtree, pool_uuid))) { | |
3117 | log_error("Missing thin pool uuid %s.", pool_uuid); | |
3118 | return 0; | |
3119 | } | |
3120 | ||
3121 | if (!_link_tree_nodes(node, pool)) | |
3122 | return_0; | |
3123 | ||
3124 | if (!_thin_validate_device_id(device_id)) | |
3125 | return_0; | |
3126 | ||
3127 | if (!(seg = _add_segment(node, SEG_THIN, size))) | |
3128 | return_0; | |
3129 | ||
3130 | seg->pool = pool; | |
3131 | seg->device_id = device_id; | |
3132 | ||
3133 | return 1; | |
3134 | } | |
3135 | ||
3136 | ||
3137 | int dm_get_status_thin_pool(struct dm_pool *mem, const char *params, | |
3138 | struct dm_status_thin_pool **status) | |
3139 | { | |
3140 | struct dm_status_thin_pool *s; | |
3141 | ||
3142 | if (!(s = dm_pool_zalloc(mem, sizeof(struct dm_status_thin_pool)))) { | |
3143 | log_error("Failed to allocate thin_pool status structure."); | |
3144 | return 0; | |
3145 | } | |
3146 | ||
3147 | /* FIXME: add support for held metadata root */ | |
3148 | if (sscanf(params, "%" PRIu64 " %" PRIu64 "/%" PRIu64 " %" PRIu64 "/%" PRIu64, | |
3149 | &s->transaction_id, | |
3150 | &s->used_metadata_blocks, | |
3151 | &s->total_metadata_blocks, | |
3152 | &s->used_data_blocks, | |
3153 | &s->total_data_blocks) != 5) { | |
3154 | log_error("Failed to parse thin pool params: %s.", params); | |
3155 | return 0; | |
3156 | } | |
3157 | ||
3158 | *status = s; | |
3159 | ||
3160 | return 1; | |
3161 | } | |
3162 | ||
3163 | int dm_get_status_thin(struct dm_pool *mem, const char *params, | |
3164 | struct dm_status_thin **status) | |
3165 | { | |
3166 | struct dm_status_thin *s; | |
3167 | ||
3168 | if (!(s = dm_pool_zalloc(mem, sizeof(struct dm_status_thin)))) { | |
3169 | log_error("Failed to allocate thin status structure."); | |
3170 | return 0; | |
3171 | } | |
3172 | ||
3173 | if (strchr(params, '-')) { | |
3174 | s->mapped_sectors = 0; | |
3175 | s->highest_mapped_sector = 0; | |
3176 | } else if (sscanf(params, "%" PRIu64 " %" PRIu64, | |
3177 | &s->mapped_sectors, | |
3178 | &s->highest_mapped_sector) != 2) { | |
3179 | log_error("Failed to parse thin params: %s.", params); | |
3180 | return 0; | |
3181 | } | |
3182 | ||
3183 | *status = s; | |
3184 | ||
3185 | return 1; | |
3186 | } | |
3187 | ||
3188 | static int _add_area(struct dm_tree_node *node, struct load_segment *seg, struct dm_tree_node *dev_node, uint64_t offset) | |
3189 | { | |
3190 | struct seg_area *area; | |
3191 | ||
3192 | if (!(area = dm_pool_zalloc(node->dtree->mem, sizeof (*area)))) { | |
3193 | log_error("Failed to allocate target segment area."); | |
3194 | return 0; | |
3195 | } | |
3196 | ||
3197 | area->dev_node = dev_node; | |
3198 | area->offset = offset; | |
3199 | ||
3200 | dm_list_add(&seg->areas, &area->list); | |
3201 | seg->area_count++; | |
3202 | ||
3203 | return 1; | |
3204 | } | |
3205 | ||
3206 | int dm_tree_node_add_target_area(struct dm_tree_node *node, | |
3207 | const char *dev_name, | |
3208 | const char *uuid, | |
3209 | uint64_t offset) | |
3210 | { | |
3211 | struct load_segment *seg; | |
3212 | struct stat info; | |
3213 | struct dm_tree_node *dev_node; | |
3214 | ||
3215 | if ((!dev_name || !*dev_name) && (!uuid || !*uuid)) { | |
3216 | log_error("dm_tree_node_add_target_area called without device"); | |
3217 | return 0; | |
3218 | } | |
3219 | ||
3220 | if (uuid) { | |
3221 | if (!(dev_node = dm_tree_find_node_by_uuid(node->dtree, uuid))) { | |
3222 | log_error("Couldn't find area uuid %s.", uuid); | |
3223 | return 0; | |
3224 | } | |
3225 | if (!_link_tree_nodes(node, dev_node)) | |
3226 | return_0; | |
3227 | } else { | |
3228 | if (stat(dev_name, &info) < 0) { | |
3229 | log_error("Device %s not found.", dev_name); | |
3230 | return 0; | |
3231 | } | |
3232 | ||
3233 | if (!S_ISBLK(info.st_mode)) { | |
3234 | log_error("Device %s is not a block device.", dev_name); | |
3235 | return 0; | |
3236 | } | |
3237 | ||
3238 | /* FIXME Check correct macro use */ | |
3239 | if (!(dev_node = _add_dev(node->dtree, node, MAJOR(info.st_rdev), | |
3240 | MINOR(info.st_rdev), 0))) | |
3241 | return_0; | |
3242 | } | |
3243 | ||
3244 | if (!node->props.segment_count) { | |
3245 | log_error(INTERNAL_ERROR "Attempt to add target area to missing segment."); | |
3246 | return 0; | |
3247 | } | |
3248 | ||
3249 | seg = dm_list_item(dm_list_last(&node->props.segs), struct load_segment); | |
3250 | ||
3251 | if (!_add_area(node, seg, dev_node, offset)) | |
3252 | return_0; | |
3253 | ||
3254 | return 1; | |
3255 | } | |
3256 | ||
3257 | int dm_tree_node_add_null_area(struct dm_tree_node *node, uint64_t offset) | |
3258 | { | |
3259 | struct load_segment *seg; | |
3260 | ||
3261 | seg = dm_list_item(dm_list_last(&node->props.segs), struct load_segment); | |
3262 | ||
3263 | switch (seg->type) { | |
3264 | case SEG_RAID1: | |
3265 | case SEG_RAID4: | |
3266 | case SEG_RAID5_LA: | |
3267 | case SEG_RAID5_RA: | |
3268 | case SEG_RAID5_LS: | |
3269 | case SEG_RAID5_RS: | |
3270 | case SEG_RAID6_ZR: | |
3271 | case SEG_RAID6_NR: | |
3272 | case SEG_RAID6_NC: | |
3273 | break; | |
3274 | default: | |
3275 | log_error("dm_tree_node_add_null_area() called on an unsupported segment type"); | |
3276 | return 0; | |
3277 | } | |
3278 | ||
3279 | if (!_add_area(node, seg, NULL, offset)) | |
3280 | return_0; | |
3281 | ||
3282 | return 1; | |
3283 | } | |
3284 | ||
3285 | void dm_tree_node_set_callback(struct dm_tree_node *dnode, | |
3286 | dm_node_callback_fn cb, void *data) | |
3287 | { | |
3288 | dnode->callback = cb; | |
3289 | dnode->callback_data = data; | |
3290 | } |