]>
Commit | Line | Data |
---|---|---|
3d0480ed | 1 | /* |
4ef2bf27 | 2 | * Copyright (C) 2005-2010 Red Hat, Inc. All rights reserved. |
3d0480ed AK |
3 | * |
4 | * This file is part of the device-mapper userspace tools. | |
5 | * | |
6 | * This copyrighted material is made available to anyone wishing to use, | |
7 | * modify, copy, or redistribute it subject to the terms and conditions | |
8 | * of the GNU Lesser General Public License v.2.1. | |
9 | * | |
10 | * You should have received a copy of the GNU Lesser General Public License | |
11 | * along with this program; if not, write to the Free Software Foundation, | |
12 | * Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA | |
13 | */ | |
14 | ||
3e5b6ed2 | 15 | #include "dmlib.h" |
3d0480ed AK |
16 | #include "libdm-targets.h" |
17 | #include "libdm-common.h" | |
3d0480ed | 18 | #include "kdev_t.h" |
0782ad50 | 19 | #include "dm-ioctl.h" |
3d0480ed AK |
20 | |
21 | #include <stdarg.h> | |
22 | #include <sys/param.h> | |
8f26e18c | 23 | #include <sys/utsname.h> |
3d0480ed | 24 | |
165e4a11 AK |
25 | #define MAX_TARGET_PARAMSIZE 500000 |
26 | ||
87f98002 AK |
27 | /* FIXME Fix interface so this is used only by LVM */ |
28 | #define UUID_PREFIX "LVM-" | |
29 | ||
b262f3e1 ZK |
30 | #define REPLICATOR_LOCAL_SITE 0 |
31 | ||
165e4a11 AK |
32 | /* Supported segment types */ |
33 | enum { | |
12ca060e MB |
34 | SEG_CRYPT, |
35 | SEG_ERROR, | |
165e4a11 AK |
36 | SEG_LINEAR, |
37 | SEG_MIRRORED, | |
b262f3e1 ZK |
38 | SEG_REPLICATOR, |
39 | SEG_REPLICATOR_DEV, | |
165e4a11 AK |
40 | SEG_SNAPSHOT, |
41 | SEG_SNAPSHOT_ORIGIN, | |
aa6f4e51 | 42 | SEG_SNAPSHOT_MERGE, |
165e4a11 AK |
43 | SEG_STRIPED, |
44 | SEG_ZERO, | |
cac52ca4 JEB |
45 | SEG_RAID1, |
46 | SEG_RAID4, | |
47 | SEG_RAID5_LA, | |
48 | SEG_RAID5_RA, | |
49 | SEG_RAID5_LS, | |
50 | SEG_RAID5_RS, | |
51 | SEG_RAID6_ZR, | |
52 | SEG_RAID6_NR, | |
53 | SEG_RAID6_NC, | |
54 | SEG_LAST, | |
165e4a11 | 55 | }; |
b4f1578f | 56 | |
165e4a11 AK |
57 | /* FIXME Add crypt and multipath support */ |
58 | ||
59 | struct { | |
60 | unsigned type; | |
61 | const char *target; | |
62 | } dm_segtypes[] = { | |
12ca060e | 63 | { SEG_CRYPT, "crypt" }, |
165e4a11 AK |
64 | { SEG_ERROR, "error" }, |
65 | { SEG_LINEAR, "linear" }, | |
66 | { SEG_MIRRORED, "mirror" }, | |
b262f3e1 ZK |
67 | { SEG_REPLICATOR, "replicator" }, |
68 | { SEG_REPLICATOR_DEV, "replicator-dev" }, | |
165e4a11 AK |
69 | { SEG_SNAPSHOT, "snapshot" }, |
70 | { SEG_SNAPSHOT_ORIGIN, "snapshot-origin" }, | |
aa6f4e51 | 71 | { SEG_SNAPSHOT_MERGE, "snapshot-merge" }, |
165e4a11 AK |
72 | { SEG_STRIPED, "striped" }, |
73 | { SEG_ZERO, "zero"}, | |
cac52ca4 JEB |
74 | { SEG_RAID1, "raid1"}, |
75 | { SEG_RAID4, "raid4"}, | |
76 | { SEG_RAID5_LA, "raid5_la"}, | |
77 | { SEG_RAID5_RA, "raid5_ra"}, | |
78 | { SEG_RAID5_LS, "raid5_ls"}, | |
79 | { SEG_RAID5_RS, "raid5_rs"}, | |
80 | { SEG_RAID6_ZR, "raid6_zr"}, | |
81 | { SEG_RAID6_NR, "raid6_nr"}, | |
82 | { SEG_RAID6_NC, "raid6_nc"}, | |
83 | { SEG_RAID5_LS, "raid5"}, /* same as "raid5_ls" (default for MD also) */ | |
84 | { SEG_RAID6_ZR, "raid6"}, /* same as "raid6_zr" */ | |
85 | { SEG_LAST, NULL }, | |
165e4a11 AK |
86 | }; |
87 | ||
88 | /* Some segment types have a list of areas of other devices attached */ | |
89 | struct seg_area { | |
2c44337b | 90 | struct dm_list list; |
165e4a11 | 91 | |
b4f1578f | 92 | struct dm_tree_node *dev_node; |
165e4a11 AK |
93 | |
94 | uint64_t offset; | |
b262f3e1 ZK |
95 | |
96 | unsigned rsite_index; /* Replicator site index */ | |
97 | struct dm_tree_node *slog; /* Replicator sync log node */ | |
98 | uint64_t region_size; /* Replicator sync log size */ | |
99 | uint32_t flags; /* Replicator sync log flags */ | |
100 | }; | |
101 | ||
102 | /* Replicator-log has a list of sites */ | |
103 | /* FIXME: maybe move to seg_area too? */ | |
104 | struct replicator_site { | |
105 | struct dm_list list; | |
106 | ||
107 | unsigned rsite_index; | |
108 | dm_replicator_mode_t mode; | |
109 | uint32_t async_timeout; | |
110 | uint32_t fall_behind_ios; | |
111 | uint64_t fall_behind_data; | |
165e4a11 AK |
112 | }; |
113 | ||
114 | /* Per-segment properties */ | |
115 | struct load_segment { | |
2c44337b | 116 | struct dm_list list; |
165e4a11 AK |
117 | |
118 | unsigned type; | |
119 | ||
120 | uint64_t size; | |
121 | ||
b262f3e1 ZK |
122 | unsigned area_count; /* Linear + Striped + Mirrored + Crypt + Replicator */ |
123 | struct dm_list areas; /* Linear + Striped + Mirrored + Crypt + Replicator */ | |
165e4a11 | 124 | |
cac52ca4 | 125 | uint32_t stripe_size; /* Striped + raid */ |
165e4a11 AK |
126 | |
127 | int persistent; /* Snapshot */ | |
128 | uint32_t chunk_size; /* Snapshot */ | |
b4f1578f AK |
129 | struct dm_tree_node *cow; /* Snapshot */ |
130 | struct dm_tree_node *origin; /* Snapshot + Snapshot origin */ | |
aa6f4e51 | 131 | struct dm_tree_node *merge; /* Snapshot */ |
165e4a11 | 132 | |
b262f3e1 | 133 | struct dm_tree_node *log; /* Mirror + Replicator */ |
cac52ca4 | 134 | uint32_t region_size; /* Mirror + raid */ |
165e4a11 AK |
135 | unsigned clustered; /* Mirror */ |
136 | unsigned mirror_area_count; /* Mirror */ | |
dbcb64b8 | 137 | uint32_t flags; /* Mirror log */ |
67b25ed4 | 138 | char *uuid; /* Clustered mirror log */ |
12ca060e MB |
139 | |
140 | const char *cipher; /* Crypt */ | |
141 | const char *chainmode; /* Crypt */ | |
142 | const char *iv; /* Crypt */ | |
143 | uint64_t iv_offset; /* Crypt */ | |
144 | const char *key; /* Crypt */ | |
b262f3e1 ZK |
145 | |
146 | const char *rlog_type; /* Replicator */ | |
147 | struct dm_list rsites; /* Replicator */ | |
148 | unsigned rsite_count; /* Replicator */ | |
149 | unsigned rdevice_count; /* Replicator */ | |
150 | struct dm_tree_node *replicator;/* Replicator-dev */ | |
151 | uint64_t rdevice_index; /* Replicator-dev */ | |
f439e65b | 152 | |
40e5fd8b | 153 | uint64_t rebuilds; /* raid */ |
165e4a11 AK |
154 | }; |
155 | ||
156 | /* Per-device properties */ | |
157 | struct load_properties { | |
158 | int read_only; | |
159 | uint32_t major; | |
160 | uint32_t minor; | |
161 | ||
52b84409 AK |
162 | uint32_t read_ahead; |
163 | uint32_t read_ahead_flags; | |
164 | ||
165e4a11 | 165 | unsigned segment_count; |
bb875bb9 | 166 | unsigned size_changed; |
2c44337b | 167 | struct dm_list segs; |
165e4a11 AK |
168 | |
169 | const char *new_name; | |
566515c0 PR |
170 | |
171 | /* If immediate_dev_node is set to 1, try to create the dev node | |
172 | * as soon as possible (e.g. in preload stage even during traversal | |
173 | * and processing of dm tree). This will also flush all stacked dev | |
174 | * node operations, synchronizing with udev. | |
175 | */ | |
df390f17 AK |
176 | unsigned immediate_dev_node; |
177 | ||
178 | /* | |
179 | * If the device size changed from zero and this is set, | |
180 | * don't resume the device immediately, even if the device | |
181 | * has parents. This works provided the parents do not | |
182 | * validate the device size and is required by pvmove to | |
183 | * avoid starting the mirror resync operation too early. | |
184 | */ | |
185 | unsigned delay_resume_if_new; | |
165e4a11 AK |
186 | }; |
187 | ||
188 | /* Two of these used to join two nodes with uses and used_by. */ | |
b4f1578f | 189 | struct dm_tree_link { |
2c44337b | 190 | struct dm_list list; |
b4f1578f | 191 | struct dm_tree_node *node; |
165e4a11 AK |
192 | }; |
193 | ||
b4f1578f AK |
194 | struct dm_tree_node { |
195 | struct dm_tree *dtree; | |
3d0480ed | 196 | |
40e5fd8b AK |
197 | const char *name; |
198 | const char *uuid; | |
199 | struct dm_info info; | |
3d0480ed | 200 | |
40e5fd8b AK |
201 | struct dm_list uses; /* Nodes this node uses */ |
202 | struct dm_list used_by; /* Nodes that use this node */ | |
165e4a11 | 203 | |
56c28292 AK |
204 | int activation_priority; /* 0 gets activated first */ |
205 | ||
f16aea9e PR |
206 | uint16_t udev_flags; /* Udev control flags */ |
207 | ||
165e4a11 AK |
208 | void *context; /* External supplied context */ |
209 | ||
210 | struct load_properties props; /* For creation/table (re)load */ | |
76d1aec8 ZK |
211 | |
212 | /* | |
213 | * If presuspend of child node is needed | |
214 | * Note: only direct child is allowed | |
215 | */ | |
216 | struct dm_tree_node *presuspend_node; | |
3d0480ed AK |
217 | }; |
218 | ||
b4f1578f | 219 | struct dm_tree { |
a3f6b2ce AK |
220 | struct dm_pool *mem; |
221 | struct dm_hash_table *devs; | |
165e4a11 | 222 | struct dm_hash_table *uuids; |
b4f1578f | 223 | struct dm_tree_node root; |
c55b1410 | 224 | int skip_lockfs; /* 1 skips lockfs (for non-snapshots) */ |
b9ffd32c | 225 | int no_flush; /* 1 sets noflush (mirrors/multipath) */ |
bd90c6b2 | 226 | uint32_t cookie; |
3d0480ed AK |
227 | }; |
228 | ||
b4f1578f | 229 | struct dm_tree *dm_tree_create(void) |
3d0480ed | 230 | { |
b4f1578f | 231 | struct dm_tree *dtree; |
3d0480ed | 232 | |
ac0252ca | 233 | if (!(dtree = dm_zalloc(sizeof(*dtree)))) { |
b4f1578f | 234 | log_error("dm_tree_create malloc failed"); |
3d0480ed AK |
235 | return NULL; |
236 | } | |
237 | ||
b4f1578f | 238 | dtree->root.dtree = dtree; |
2c44337b AK |
239 | dm_list_init(&dtree->root.uses); |
240 | dm_list_init(&dtree->root.used_by); | |
c55b1410 | 241 | dtree->skip_lockfs = 0; |
b9ffd32c | 242 | dtree->no_flush = 0; |
3d0480ed | 243 | |
b4f1578f AK |
244 | if (!(dtree->mem = dm_pool_create("dtree", 1024))) { |
245 | log_error("dtree pool creation failed"); | |
246 | dm_free(dtree); | |
3d0480ed AK |
247 | return NULL; |
248 | } | |
249 | ||
b4f1578f AK |
250 | if (!(dtree->devs = dm_hash_create(8))) { |
251 | log_error("dtree hash creation failed"); | |
252 | dm_pool_destroy(dtree->mem); | |
253 | dm_free(dtree); | |
3d0480ed AK |
254 | return NULL; |
255 | } | |
256 | ||
b4f1578f AK |
257 | if (!(dtree->uuids = dm_hash_create(32))) { |
258 | log_error("dtree uuid hash creation failed"); | |
259 | dm_hash_destroy(dtree->devs); | |
260 | dm_pool_destroy(dtree->mem); | |
261 | dm_free(dtree); | |
165e4a11 AK |
262 | return NULL; |
263 | } | |
264 | ||
b4f1578f | 265 | return dtree; |
3d0480ed AK |
266 | } |
267 | ||
b4f1578f | 268 | void dm_tree_free(struct dm_tree *dtree) |
3d0480ed | 269 | { |
b4f1578f | 270 | if (!dtree) |
3d0480ed AK |
271 | return; |
272 | ||
b4f1578f AK |
273 | dm_hash_destroy(dtree->uuids); |
274 | dm_hash_destroy(dtree->devs); | |
275 | dm_pool_destroy(dtree->mem); | |
276 | dm_free(dtree); | |
3d0480ed AK |
277 | } |
278 | ||
04bde319 ZK |
279 | static int _nodes_are_linked(const struct dm_tree_node *parent, |
280 | const struct dm_tree_node *child) | |
3d0480ed | 281 | { |
b4f1578f | 282 | struct dm_tree_link *dlink; |
3d0480ed | 283 | |
2c44337b | 284 | dm_list_iterate_items(dlink, &parent->uses) |
3d0480ed AK |
285 | if (dlink->node == child) |
286 | return 1; | |
3d0480ed AK |
287 | |
288 | return 0; | |
289 | } | |
290 | ||
2c44337b | 291 | static int _link(struct dm_list *list, struct dm_tree_node *node) |
3d0480ed | 292 | { |
b4f1578f | 293 | struct dm_tree_link *dlink; |
3d0480ed | 294 | |
b4f1578f AK |
295 | if (!(dlink = dm_pool_alloc(node->dtree->mem, sizeof(*dlink)))) { |
296 | log_error("dtree link allocation failed"); | |
3d0480ed AK |
297 | return 0; |
298 | } | |
299 | ||
300 | dlink->node = node; | |
2c44337b | 301 | dm_list_add(list, &dlink->list); |
3d0480ed AK |
302 | |
303 | return 1; | |
304 | } | |
305 | ||
b4f1578f AK |
306 | static int _link_nodes(struct dm_tree_node *parent, |
307 | struct dm_tree_node *child) | |
3d0480ed AK |
308 | { |
309 | if (_nodes_are_linked(parent, child)) | |
310 | return 1; | |
311 | ||
312 | if (!_link(&parent->uses, child)) | |
313 | return 0; | |
314 | ||
315 | if (!_link(&child->used_by, parent)) | |
316 | return 0; | |
317 | ||
318 | return 1; | |
319 | } | |
320 | ||
2c44337b | 321 | static void _unlink(struct dm_list *list, struct dm_tree_node *node) |
3d0480ed | 322 | { |
b4f1578f | 323 | struct dm_tree_link *dlink; |
3d0480ed | 324 | |
2c44337b | 325 | dm_list_iterate_items(dlink, list) |
3d0480ed | 326 | if (dlink->node == node) { |
2c44337b | 327 | dm_list_del(&dlink->list); |
3d0480ed AK |
328 | break; |
329 | } | |
3d0480ed AK |
330 | } |
331 | ||
b4f1578f AK |
332 | static void _unlink_nodes(struct dm_tree_node *parent, |
333 | struct dm_tree_node *child) | |
3d0480ed AK |
334 | { |
335 | if (!_nodes_are_linked(parent, child)) | |
336 | return; | |
337 | ||
338 | _unlink(&parent->uses, child); | |
339 | _unlink(&child->used_by, parent); | |
340 | } | |
341 | ||
b4f1578f | 342 | static int _add_to_toplevel(struct dm_tree_node *node) |
165e4a11 | 343 | { |
b4f1578f | 344 | return _link_nodes(&node->dtree->root, node); |
165e4a11 AK |
345 | } |
346 | ||
b4f1578f | 347 | static void _remove_from_toplevel(struct dm_tree_node *node) |
3d0480ed | 348 | { |
b1ebf028 | 349 | _unlink_nodes(&node->dtree->root, node); |
3d0480ed AK |
350 | } |
351 | ||
b4f1578f | 352 | static int _add_to_bottomlevel(struct dm_tree_node *node) |
3d0480ed | 353 | { |
b4f1578f | 354 | return _link_nodes(node, &node->dtree->root); |
3d0480ed AK |
355 | } |
356 | ||
b4f1578f | 357 | static void _remove_from_bottomlevel(struct dm_tree_node *node) |
165e4a11 | 358 | { |
b1ebf028 | 359 | _unlink_nodes(node, &node->dtree->root); |
165e4a11 AK |
360 | } |
361 | ||
b4f1578f | 362 | static int _link_tree_nodes(struct dm_tree_node *parent, struct dm_tree_node *child) |
165e4a11 AK |
363 | { |
364 | /* Don't link to root node if child already has a parent */ | |
f77736ca | 365 | if (parent == &parent->dtree->root) { |
b4f1578f | 366 | if (dm_tree_node_num_children(child, 1)) |
165e4a11 AK |
367 | return 1; |
368 | } else | |
369 | _remove_from_toplevel(child); | |
370 | ||
f77736ca | 371 | if (child == &child->dtree->root) { |
b4f1578f | 372 | if (dm_tree_node_num_children(parent, 0)) |
165e4a11 AK |
373 | return 1; |
374 | } else | |
375 | _remove_from_bottomlevel(parent); | |
376 | ||
377 | return _link_nodes(parent, child); | |
378 | } | |
379 | ||
b4f1578f | 380 | static struct dm_tree_node *_create_dm_tree_node(struct dm_tree *dtree, |
3d0480ed AK |
381 | const char *name, |
382 | const char *uuid, | |
165e4a11 | 383 | struct dm_info *info, |
f16aea9e PR |
384 | void *context, |
385 | uint16_t udev_flags) | |
3d0480ed | 386 | { |
b4f1578f | 387 | struct dm_tree_node *node; |
3d0480ed AK |
388 | uint64_t dev; |
389 | ||
b4f1578f AK |
390 | if (!(node = dm_pool_zalloc(dtree->mem, sizeof(*node)))) { |
391 | log_error("_create_dm_tree_node alloc failed"); | |
3d0480ed AK |
392 | return NULL; |
393 | } | |
394 | ||
b4f1578f | 395 | node->dtree = dtree; |
3d0480ed AK |
396 | |
397 | node->name = name; | |
398 | node->uuid = uuid; | |
399 | node->info = *info; | |
165e4a11 | 400 | node->context = context; |
f16aea9e | 401 | node->udev_flags = udev_flags; |
56c28292 | 402 | node->activation_priority = 0; |
3d0480ed | 403 | |
2c44337b AK |
404 | dm_list_init(&node->uses); |
405 | dm_list_init(&node->used_by); | |
406 | dm_list_init(&node->props.segs); | |
3d0480ed AK |
407 | |
408 | dev = MKDEV(info->major, info->minor); | |
409 | ||
b4f1578f | 410 | if (!dm_hash_insert_binary(dtree->devs, (const char *) &dev, |
3d0480ed | 411 | sizeof(dev), node)) { |
b4f1578f AK |
412 | log_error("dtree node hash insertion failed"); |
413 | dm_pool_free(dtree->mem, node); | |
3d0480ed AK |
414 | return NULL; |
415 | } | |
416 | ||
165e4a11 | 417 | if (uuid && *uuid && |
b4f1578f AK |
418 | !dm_hash_insert(dtree->uuids, uuid, node)) { |
419 | log_error("dtree uuid hash insertion failed"); | |
420 | dm_hash_remove_binary(dtree->devs, (const char *) &dev, | |
165e4a11 | 421 | sizeof(dev)); |
b4f1578f | 422 | dm_pool_free(dtree->mem, node); |
165e4a11 AK |
423 | return NULL; |
424 | } | |
425 | ||
3d0480ed AK |
426 | return node; |
427 | } | |
428 | ||
b4f1578f | 429 | static struct dm_tree_node *_find_dm_tree_node(struct dm_tree *dtree, |
3d0480ed AK |
430 | uint32_t major, uint32_t minor) |
431 | { | |
432 | uint64_t dev = MKDEV(major, minor); | |
433 | ||
b4f1578f | 434 | return dm_hash_lookup_binary(dtree->devs, (const char *) &dev, |
3d0480ed AK |
435 | sizeof(dev)); |
436 | } | |
437 | ||
b4f1578f | 438 | static struct dm_tree_node *_find_dm_tree_node_by_uuid(struct dm_tree *dtree, |
165e4a11 AK |
439 | const char *uuid) |
440 | { | |
87f98002 AK |
441 | struct dm_tree_node *node; |
442 | ||
443 | if ((node = dm_hash_lookup(dtree->uuids, uuid))) | |
444 | return node; | |
445 | ||
446 | if (strncmp(uuid, UUID_PREFIX, sizeof(UUID_PREFIX) - 1)) | |
447 | return NULL; | |
448 | ||
449 | return dm_hash_lookup(dtree->uuids, uuid + sizeof(UUID_PREFIX) - 1); | |
165e4a11 AK |
450 | } |
451 | ||
a3f6b2ce | 452 | static int _deps(struct dm_task **dmt, struct dm_pool *mem, uint32_t major, uint32_t minor, |
3d0480ed AK |
453 | const char **name, const char **uuid, |
454 | struct dm_info *info, struct dm_deps **deps) | |
455 | { | |
456 | memset(info, 0, sizeof(*info)); | |
457 | ||
458 | if (!dm_is_dm_major(major)) { | |
459 | *name = ""; | |
460 | *uuid = ""; | |
461 | *deps = NULL; | |
462 | info->major = major; | |
463 | info->minor = minor; | |
464 | info->exists = 0; | |
165e4a11 AK |
465 | info->live_table = 0; |
466 | info->inactive_table = 0; | |
467 | info->read_only = 0; | |
3d0480ed AK |
468 | return 1; |
469 | } | |
470 | ||
471 | if (!(*dmt = dm_task_create(DM_DEVICE_DEPS))) { | |
472 | log_error("deps dm_task creation failed"); | |
473 | return 0; | |
474 | } | |
475 | ||
b4f1578f AK |
476 | if (!dm_task_set_major(*dmt, major)) { |
477 | log_error("_deps: failed to set major for (%" PRIu32 ":%" PRIu32 ")", | |
478 | major, minor); | |
3d0480ed | 479 | goto failed; |
b4f1578f | 480 | } |
3d0480ed | 481 | |
b4f1578f AK |
482 | if (!dm_task_set_minor(*dmt, minor)) { |
483 | log_error("_deps: failed to set minor for (%" PRIu32 ":%" PRIu32 ")", | |
484 | major, minor); | |
3d0480ed | 485 | goto failed; |
b4f1578f | 486 | } |
3d0480ed | 487 | |
b4f1578f AK |
488 | if (!dm_task_run(*dmt)) { |
489 | log_error("_deps: task run failed for (%" PRIu32 ":%" PRIu32 ")", | |
490 | major, minor); | |
3d0480ed | 491 | goto failed; |
b4f1578f | 492 | } |
3d0480ed | 493 | |
b4f1578f AK |
494 | if (!dm_task_get_info(*dmt, info)) { |
495 | log_error("_deps: failed to get info for (%" PRIu32 ":%" PRIu32 ")", | |
496 | major, minor); | |
3d0480ed | 497 | goto failed; |
b4f1578f | 498 | } |
3d0480ed AK |
499 | |
500 | if (!info->exists) { | |
501 | *name = ""; | |
502 | *uuid = ""; | |
503 | *deps = NULL; | |
504 | } else { | |
505 | if (info->major != major) { | |
b4f1578f | 506 | log_error("Inconsistent dtree major number: %u != %u", |
3d0480ed AK |
507 | major, info->major); |
508 | goto failed; | |
509 | } | |
510 | if (info->minor != minor) { | |
b4f1578f | 511 | log_error("Inconsistent dtree minor number: %u != %u", |
3d0480ed AK |
512 | minor, info->minor); |
513 | goto failed; | |
514 | } | |
a3f6b2ce | 515 | if (!(*name = dm_pool_strdup(mem, dm_task_get_name(*dmt)))) { |
3d0480ed AK |
516 | log_error("name pool_strdup failed"); |
517 | goto failed; | |
518 | } | |
a3f6b2ce | 519 | if (!(*uuid = dm_pool_strdup(mem, dm_task_get_uuid(*dmt)))) { |
3d0480ed AK |
520 | log_error("uuid pool_strdup failed"); |
521 | goto failed; | |
522 | } | |
523 | *deps = dm_task_get_deps(*dmt); | |
524 | } | |
525 | ||
526 | return 1; | |
527 | ||
528 | failed: | |
529 | dm_task_destroy(*dmt); | |
530 | return 0; | |
531 | } | |
532 | ||
b4f1578f AK |
533 | static struct dm_tree_node *_add_dev(struct dm_tree *dtree, |
534 | struct dm_tree_node *parent, | |
cda69e17 PR |
535 | uint32_t major, uint32_t minor, |
536 | uint16_t udev_flags) | |
3d0480ed AK |
537 | { |
538 | struct dm_task *dmt = NULL; | |
539 | struct dm_info info; | |
540 | struct dm_deps *deps = NULL; | |
541 | const char *name = NULL; | |
542 | const char *uuid = NULL; | |
b4f1578f | 543 | struct dm_tree_node *node = NULL; |
3d0480ed | 544 | uint32_t i; |
3d0480ed AK |
545 | int new = 0; |
546 | ||
547 | /* Already in tree? */ | |
b4f1578f AK |
548 | if (!(node = _find_dm_tree_node(dtree, major, minor))) { |
549 | if (!_deps(&dmt, dtree->mem, major, minor, &name, &uuid, &info, &deps)) | |
550 | return_NULL; | |
3d0480ed | 551 | |
f16aea9e | 552 | if (!(node = _create_dm_tree_node(dtree, name, uuid, &info, |
cda69e17 | 553 | NULL, udev_flags))) |
b4f1578f | 554 | goto_out; |
3d0480ed AK |
555 | new = 1; |
556 | } | |
557 | ||
165e4a11 AK |
558 | if (!_link_tree_nodes(parent, node)) { |
559 | node = NULL; | |
b4f1578f | 560 | goto_out; |
165e4a11 | 561 | } |
3d0480ed AK |
562 | |
563 | /* If node was already in tree, no need to recurse. */ | |
564 | if (!new) | |
165e4a11 | 565 | goto out; |
3d0480ed AK |
566 | |
567 | /* Can't recurse if not a mapped device or there are no dependencies */ | |
568 | if (!node->info.exists || !deps->count) { | |
b4f1578f AK |
569 | if (!_add_to_bottomlevel(node)) { |
570 | stack; | |
165e4a11 | 571 | node = NULL; |
b4f1578f | 572 | } |
165e4a11 | 573 | goto out; |
3d0480ed AK |
574 | } |
575 | ||
576 | /* Add dependencies to tree */ | |
577 | for (i = 0; i < deps->count; i++) | |
b4f1578f | 578 | if (!_add_dev(dtree, node, MAJOR(deps->device[i]), |
cda69e17 | 579 | MINOR(deps->device[i]), udev_flags)) { |
165e4a11 | 580 | node = NULL; |
b4f1578f | 581 | goto_out; |
165e4a11 | 582 | } |
3d0480ed | 583 | |
3d0480ed AK |
584 | out: |
585 | if (dmt) | |
586 | dm_task_destroy(dmt); | |
587 | ||
165e4a11 AK |
588 | return node; |
589 | } | |
590 | ||
b4f1578f | 591 | static int _node_clear_table(struct dm_tree_node *dnode) |
165e4a11 AK |
592 | { |
593 | struct dm_task *dmt; | |
594 | struct dm_info *info; | |
595 | const char *name; | |
596 | int r; | |
597 | ||
598 | if (!(info = &dnode->info)) { | |
b4f1578f | 599 | log_error("_node_clear_table failed: missing info"); |
165e4a11 AK |
600 | return 0; |
601 | } | |
602 | ||
b4f1578f AK |
603 | if (!(name = dm_tree_node_get_name(dnode))) { |
604 | log_error("_node_clear_table failed: missing name"); | |
165e4a11 AK |
605 | return 0; |
606 | } | |
607 | ||
608 | /* Is there a table? */ | |
609 | if (!info->exists || !info->inactive_table) | |
610 | return 1; | |
611 | ||
612 | log_verbose("Clearing inactive table %s (%" PRIu32 ":%" PRIu32 ")", | |
613 | name, info->major, info->minor); | |
614 | ||
615 | if (!(dmt = dm_task_create(DM_DEVICE_CLEAR))) { | |
165e4a11 AK |
616 | log_error("Table clear dm_task creation failed for %s", name); |
617 | return 0; | |
618 | } | |
619 | ||
620 | if (!dm_task_set_major(dmt, info->major) || | |
621 | !dm_task_set_minor(dmt, info->minor)) { | |
622 | log_error("Failed to set device number for %s table clear", name); | |
623 | dm_task_destroy(dmt); | |
624 | return 0; | |
625 | } | |
626 | ||
627 | r = dm_task_run(dmt); | |
628 | ||
629 | if (!dm_task_get_info(dmt, info)) { | |
b4f1578f | 630 | log_error("_node_clear_table failed: info missing after running task for %s", name); |
165e4a11 AK |
631 | r = 0; |
632 | } | |
633 | ||
634 | dm_task_destroy(dmt); | |
635 | ||
3d0480ed AK |
636 | return r; |
637 | } | |
638 | ||
b4f1578f | 639 | struct dm_tree_node *dm_tree_add_new_dev(struct dm_tree *dtree, |
165e4a11 AK |
640 | const char *name, |
641 | const char *uuid, | |
642 | uint32_t major, uint32_t minor, | |
643 | int read_only, | |
644 | int clear_inactive, | |
645 | void *context) | |
646 | { | |
b4f1578f | 647 | struct dm_tree_node *dnode; |
165e4a11 AK |
648 | struct dm_info info; |
649 | const char *name2; | |
650 | const char *uuid2; | |
651 | ||
652 | /* Do we need to add node to tree? */ | |
b4f1578f AK |
653 | if (!(dnode = dm_tree_find_node_by_uuid(dtree, uuid))) { |
654 | if (!(name2 = dm_pool_strdup(dtree->mem, name))) { | |
165e4a11 AK |
655 | log_error("name pool_strdup failed"); |
656 | return NULL; | |
657 | } | |
b4f1578f | 658 | if (!(uuid2 = dm_pool_strdup(dtree->mem, uuid))) { |
165e4a11 AK |
659 | log_error("uuid pool_strdup failed"); |
660 | return NULL; | |
661 | } | |
662 | ||
663 | info.major = 0; | |
664 | info.minor = 0; | |
665 | info.exists = 0; | |
666 | info.live_table = 0; | |
667 | info.inactive_table = 0; | |
668 | info.read_only = 0; | |
669 | ||
f16aea9e PR |
670 | if (!(dnode = _create_dm_tree_node(dtree, name2, uuid2, &info, |
671 | context, 0))) | |
b4f1578f | 672 | return_NULL; |
165e4a11 AK |
673 | |
674 | /* Attach to root node until a table is supplied */ | |
b4f1578f AK |
675 | if (!_add_to_toplevel(dnode) || !_add_to_bottomlevel(dnode)) |
676 | return_NULL; | |
165e4a11 AK |
677 | |
678 | dnode->props.major = major; | |
679 | dnode->props.minor = minor; | |
680 | dnode->props.new_name = NULL; | |
bb875bb9 | 681 | dnode->props.size_changed = 0; |
165e4a11 AK |
682 | } else if (strcmp(name, dnode->name)) { |
683 | /* Do we need to rename node? */ | |
b4f1578f | 684 | if (!(dnode->props.new_name = dm_pool_strdup(dtree->mem, name))) { |
165e4a11 AK |
685 | log_error("name pool_strdup failed"); |
686 | return 0; | |
687 | } | |
688 | } | |
689 | ||
690 | dnode->props.read_only = read_only ? 1 : 0; | |
52b84409 AK |
691 | dnode->props.read_ahead = DM_READ_AHEAD_AUTO; |
692 | dnode->props.read_ahead_flags = 0; | |
165e4a11 | 693 | |
b4f1578f AK |
694 | if (clear_inactive && !_node_clear_table(dnode)) |
695 | return_NULL; | |
165e4a11 AK |
696 | |
697 | dnode->context = context; | |
f16aea9e | 698 | dnode->udev_flags = 0; |
165e4a11 AK |
699 | |
700 | return dnode; | |
701 | } | |
702 | ||
f16aea9e PR |
703 | struct dm_tree_node *dm_tree_add_new_dev_with_udev_flags(struct dm_tree *dtree, |
704 | const char *name, | |
705 | const char *uuid, | |
706 | uint32_t major, | |
707 | uint32_t minor, | |
708 | int read_only, | |
709 | int clear_inactive, | |
710 | void *context, | |
711 | uint16_t udev_flags) | |
712 | { | |
713 | struct dm_tree_node *node; | |
714 | ||
715 | if ((node = dm_tree_add_new_dev(dtree, name, uuid, major, minor, read_only, | |
716 | clear_inactive, context))) | |
717 | node->udev_flags = udev_flags; | |
718 | ||
719 | return node; | |
720 | } | |
721 | ||
722 | ||
52b84409 AK |
723 | void dm_tree_node_set_read_ahead(struct dm_tree_node *dnode, |
724 | uint32_t read_ahead, | |
725 | uint32_t read_ahead_flags) | |
08e64ce5 | 726 | { |
52b84409 AK |
727 | dnode->props.read_ahead = read_ahead; |
728 | dnode->props.read_ahead_flags = read_ahead_flags; | |
729 | } | |
730 | ||
76d1aec8 ZK |
731 | void dm_tree_node_set_presuspend_node(struct dm_tree_node *node, |
732 | struct dm_tree_node *presuspend_node) | |
733 | { | |
734 | node->presuspend_node = presuspend_node; | |
735 | } | |
736 | ||
b4f1578f | 737 | int dm_tree_add_dev(struct dm_tree *dtree, uint32_t major, uint32_t minor) |
3d0480ed | 738 | { |
cda69e17 PR |
739 | return _add_dev(dtree, &dtree->root, major, minor, 0) ? 1 : 0; |
740 | } | |
741 | ||
742 | int dm_tree_add_dev_with_udev_flags(struct dm_tree *dtree, uint32_t major, | |
743 | uint32_t minor, uint16_t udev_flags) | |
744 | { | |
745 | return _add_dev(dtree, &dtree->root, major, minor, udev_flags) ? 1 : 0; | |
3d0480ed AK |
746 | } |
747 | ||
04bde319 | 748 | const char *dm_tree_node_get_name(const struct dm_tree_node *node) |
3d0480ed AK |
749 | { |
750 | return node->info.exists ? node->name : ""; | |
751 | } | |
752 | ||
04bde319 | 753 | const char *dm_tree_node_get_uuid(const struct dm_tree_node *node) |
3d0480ed AK |
754 | { |
755 | return node->info.exists ? node->uuid : ""; | |
756 | } | |
757 | ||
04bde319 | 758 | const struct dm_info *dm_tree_node_get_info(const struct dm_tree_node *node) |
3d0480ed AK |
759 | { |
760 | return &node->info; | |
761 | } | |
762 | ||
04bde319 | 763 | void *dm_tree_node_get_context(const struct dm_tree_node *node) |
165e4a11 AK |
764 | { |
765 | return node->context; | |
766 | } | |
767 | ||
04bde319 | 768 | int dm_tree_node_size_changed(const struct dm_tree_node *dnode) |
eb91c4ee MB |
769 | { |
770 | return dnode->props.size_changed; | |
771 | } | |
772 | ||
04bde319 | 773 | int dm_tree_node_num_children(const struct dm_tree_node *node, uint32_t inverted) |
3d0480ed AK |
774 | { |
775 | if (inverted) { | |
b4f1578f | 776 | if (_nodes_are_linked(&node->dtree->root, node)) |
3d0480ed | 777 | return 0; |
2c44337b | 778 | return dm_list_size(&node->used_by); |
3d0480ed AK |
779 | } |
780 | ||
b4f1578f | 781 | if (_nodes_are_linked(node, &node->dtree->root)) |
3d0480ed AK |
782 | return 0; |
783 | ||
2c44337b | 784 | return dm_list_size(&node->uses); |
3d0480ed AK |
785 | } |
786 | ||
2b69db1f AK |
787 | /* |
788 | * Returns 1 if no prefix supplied | |
789 | */ | |
790 | static int _uuid_prefix_matches(const char *uuid, const char *uuid_prefix, size_t uuid_prefix_len) | |
791 | { | |
792 | if (!uuid_prefix) | |
793 | return 1; | |
794 | ||
795 | if (!strncmp(uuid, uuid_prefix, uuid_prefix_len)) | |
796 | return 1; | |
797 | ||
798 | /* Handle transition: active device uuids might be missing the prefix */ | |
799 | if (uuid_prefix_len <= 4) | |
800 | return 0; | |
801 | ||
87f98002 | 802 | if (!strncmp(uuid, UUID_PREFIX, sizeof(UUID_PREFIX) - 1)) |
872dea04 AK |
803 | return 0; |
804 | ||
87f98002 | 805 | if (strncmp(uuid_prefix, UUID_PREFIX, sizeof(UUID_PREFIX) - 1)) |
2b69db1f AK |
806 | return 0; |
807 | ||
87f98002 | 808 | if (!strncmp(uuid, uuid_prefix + sizeof(UUID_PREFIX) - 1, uuid_prefix_len - (sizeof(UUID_PREFIX) - 1))) |
2b69db1f AK |
809 | return 1; |
810 | ||
811 | return 0; | |
812 | } | |
813 | ||
690a5da2 AK |
814 | /* |
815 | * Returns 1 if no children. | |
816 | */ | |
b4f1578f | 817 | static int _children_suspended(struct dm_tree_node *node, |
690a5da2 AK |
818 | uint32_t inverted, |
819 | const char *uuid_prefix, | |
820 | size_t uuid_prefix_len) | |
821 | { | |
2c44337b | 822 | struct dm_list *list; |
b4f1578f | 823 | struct dm_tree_link *dlink; |
690a5da2 AK |
824 | const struct dm_info *dinfo; |
825 | const char *uuid; | |
826 | ||
827 | if (inverted) { | |
b4f1578f | 828 | if (_nodes_are_linked(&node->dtree->root, node)) |
690a5da2 AK |
829 | return 1; |
830 | list = &node->used_by; | |
831 | } else { | |
b4f1578f | 832 | if (_nodes_are_linked(node, &node->dtree->root)) |
690a5da2 AK |
833 | return 1; |
834 | list = &node->uses; | |
835 | } | |
836 | ||
2c44337b | 837 | dm_list_iterate_items(dlink, list) { |
b4f1578f | 838 | if (!(uuid = dm_tree_node_get_uuid(dlink->node))) { |
690a5da2 AK |
839 | stack; |
840 | continue; | |
841 | } | |
842 | ||
843 | /* Ignore if it doesn't belong to this VG */ | |
2b69db1f | 844 | if (!_uuid_prefix_matches(uuid, uuid_prefix, uuid_prefix_len)) |
690a5da2 AK |
845 | continue; |
846 | ||
76d1aec8 ZK |
847 | /* Ignore if parent node wants to presuspend this node */ |
848 | if (dlink->node->presuspend_node == node) | |
849 | continue; | |
850 | ||
b4f1578f AK |
851 | if (!(dinfo = dm_tree_node_get_info(dlink->node))) { |
852 | stack; /* FIXME Is this normal? */ | |
690a5da2 AK |
853 | return 0; |
854 | } | |
855 | ||
856 | if (!dinfo->suspended) | |
857 | return 0; | |
858 | } | |
859 | ||
860 | return 1; | |
861 | } | |
862 | ||
3d0480ed AK |
863 | /* |
864 | * Set major and minor to zero for root of tree. | |
865 | */ | |
b4f1578f | 866 | struct dm_tree_node *dm_tree_find_node(struct dm_tree *dtree, |
3d0480ed AK |
867 | uint32_t major, |
868 | uint32_t minor) | |
869 | { | |
870 | if (!major && !minor) | |
b4f1578f | 871 | return &dtree->root; |
3d0480ed | 872 | |
b4f1578f | 873 | return _find_dm_tree_node(dtree, major, minor); |
3d0480ed AK |
874 | } |
875 | ||
165e4a11 AK |
876 | /* |
877 | * Set uuid to NULL for root of tree. | |
878 | */ | |
b4f1578f | 879 | struct dm_tree_node *dm_tree_find_node_by_uuid(struct dm_tree *dtree, |
165e4a11 AK |
880 | const char *uuid) |
881 | { | |
882 | if (!uuid || !*uuid) | |
b4f1578f | 883 | return &dtree->root; |
165e4a11 | 884 | |
b4f1578f | 885 | return _find_dm_tree_node_by_uuid(dtree, uuid); |
165e4a11 AK |
886 | } |
887 | ||
3d0480ed AK |
888 | /* |
889 | * First time set *handle to NULL. | |
890 | * Set inverted to invert the tree. | |
891 | */ | |
b4f1578f | 892 | struct dm_tree_node *dm_tree_next_child(void **handle, |
04bde319 ZK |
893 | const struct dm_tree_node *parent, |
894 | uint32_t inverted) | |
3d0480ed | 895 | { |
2c44337b | 896 | struct dm_list **dlink = (struct dm_list **) handle; |
04bde319 | 897 | const struct dm_list *use_list; |
3d0480ed AK |
898 | |
899 | if (inverted) | |
900 | use_list = &parent->used_by; | |
901 | else | |
902 | use_list = &parent->uses; | |
903 | ||
904 | if (!*dlink) | |
2c44337b | 905 | *dlink = dm_list_first(use_list); |
3d0480ed | 906 | else |
2c44337b | 907 | *dlink = dm_list_next(use_list, *dlink); |
3d0480ed | 908 | |
2c44337b | 909 | return (*dlink) ? dm_list_item(*dlink, struct dm_tree_link)->node : NULL; |
3d0480ed AK |
910 | } |
911 | ||
3e8c6b73 | 912 | /* |
a6d97ede | 913 | * Deactivate a device with its dependencies if the uuid prefix matches. |
3e8c6b73 | 914 | */ |
db208f51 AK |
915 | static int _info_by_dev(uint32_t major, uint32_t minor, int with_open_count, |
916 | struct dm_info *info) | |
3e8c6b73 AK |
917 | { |
918 | struct dm_task *dmt; | |
919 | int r; | |
920 | ||
921 | if (!(dmt = dm_task_create(DM_DEVICE_INFO))) { | |
922 | log_error("_info_by_dev: dm_task creation failed"); | |
923 | return 0; | |
924 | } | |
925 | ||
926 | if (!dm_task_set_major(dmt, major) || !dm_task_set_minor(dmt, minor)) { | |
927 | log_error("_info_by_dev: Failed to set device number"); | |
928 | dm_task_destroy(dmt); | |
929 | return 0; | |
930 | } | |
931 | ||
db208f51 AK |
932 | if (!with_open_count && !dm_task_no_open_count(dmt)) |
933 | log_error("Failed to disable open_count"); | |
934 | ||
3e8c6b73 AK |
935 | if ((r = dm_task_run(dmt))) |
936 | r = dm_task_get_info(dmt, info); | |
937 | ||
938 | dm_task_destroy(dmt); | |
939 | ||
940 | return r; | |
941 | } | |
942 | ||
f3ef15ef ZK |
943 | /* Check if all parent nodes of given node have open_count == 0 */ |
944 | static int _node_has_closed_parents(struct dm_tree_node *node, | |
945 | const char *uuid_prefix, | |
946 | size_t uuid_prefix_len) | |
947 | { | |
948 | struct dm_tree_link *dlink; | |
949 | const struct dm_info *dinfo; | |
950 | struct dm_info info; | |
951 | const char *uuid; | |
952 | ||
953 | /* Iterate through parents of this node */ | |
954 | dm_list_iterate_items(dlink, &node->used_by) { | |
955 | if (!(uuid = dm_tree_node_get_uuid(dlink->node))) { | |
956 | stack; | |
957 | continue; | |
958 | } | |
959 | ||
960 | /* Ignore if it doesn't belong to this VG */ | |
961 | if (!_uuid_prefix_matches(uuid, uuid_prefix, uuid_prefix_len)) | |
962 | continue; | |
963 | ||
964 | if (!(dinfo = dm_tree_node_get_info(dlink->node))) { | |
965 | stack; /* FIXME Is this normal? */ | |
966 | return 0; | |
967 | } | |
968 | ||
969 | /* Refresh open_count */ | |
970 | if (!_info_by_dev(dinfo->major, dinfo->minor, 1, &info) || | |
971 | !info.exists) | |
972 | continue; | |
973 | ||
eb418883 ZK |
974 | if (info.open_count) { |
975 | log_debug("Node %s %d:%d has open_count %d", uuid_prefix, | |
976 | dinfo->major, dinfo->minor, info.open_count); | |
f3ef15ef | 977 | return 0; |
eb418883 | 978 | } |
f3ef15ef ZK |
979 | } |
980 | ||
981 | return 1; | |
982 | } | |
983 | ||
f16aea9e PR |
984 | static int _deactivate_node(const char *name, uint32_t major, uint32_t minor, |
985 | uint32_t *cookie, uint16_t udev_flags) | |
3e8c6b73 AK |
986 | { |
987 | struct dm_task *dmt; | |
bd90c6b2 | 988 | int r = 0; |
3e8c6b73 AK |
989 | |
990 | log_verbose("Removing %s (%" PRIu32 ":%" PRIu32 ")", name, major, minor); | |
991 | ||
992 | if (!(dmt = dm_task_create(DM_DEVICE_REMOVE))) { | |
993 | log_error("Deactivation dm_task creation failed for %s", name); | |
994 | return 0; | |
995 | } | |
996 | ||
997 | if (!dm_task_set_major(dmt, major) || !dm_task_set_minor(dmt, minor)) { | |
998 | log_error("Failed to set device number for %s deactivation", name); | |
bd90c6b2 | 999 | goto out; |
3e8c6b73 AK |
1000 | } |
1001 | ||
1002 | if (!dm_task_no_open_count(dmt)) | |
1003 | log_error("Failed to disable open_count"); | |
1004 | ||
f16aea9e | 1005 | if (!dm_task_set_cookie(dmt, cookie, udev_flags)) |
bd90c6b2 AK |
1006 | goto out; |
1007 | ||
3e8c6b73 AK |
1008 | r = dm_task_run(dmt); |
1009 | ||
0437bccc AK |
1010 | /* FIXME Until kernel returns actual name so dm-iface.c can handle it */ |
1011 | rm_dev_node(name, dmt->cookie_set && !(udev_flags & DM_UDEV_DISABLE_DM_RULES_FLAG), | |
9032898e | 1012 | dmt->cookie_set && (udev_flags & DM_UDEV_DISABLE_LIBRARY_FALLBACK)); |
165e4a11 | 1013 | |
db208f51 AK |
1014 | /* FIXME Remove node from tree or mark invalid? */ |
1015 | ||
bd90c6b2 | 1016 | out: |
db208f51 AK |
1017 | dm_task_destroy(dmt); |
1018 | ||
1019 | return r; | |
1020 | } | |
1021 | ||
bd90c6b2 | 1022 | static int _rename_node(const char *old_name, const char *new_name, uint32_t major, |
f16aea9e | 1023 | uint32_t minor, uint32_t *cookie, uint16_t udev_flags) |
165e4a11 AK |
1024 | { |
1025 | struct dm_task *dmt; | |
1026 | int r = 0; | |
1027 | ||
1028 | log_verbose("Renaming %s (%" PRIu32 ":%" PRIu32 ") to %s", old_name, major, minor, new_name); | |
1029 | ||
1030 | if (!(dmt = dm_task_create(DM_DEVICE_RENAME))) { | |
1031 | log_error("Rename dm_task creation failed for %s", old_name); | |
1032 | return 0; | |
1033 | } | |
1034 | ||
1035 | if (!dm_task_set_name(dmt, old_name)) { | |
1036 | log_error("Failed to set name for %s rename.", old_name); | |
1037 | goto out; | |
1038 | } | |
1039 | ||
b4f1578f | 1040 | if (!dm_task_set_newname(dmt, new_name)) |
40e5fd8b | 1041 | goto_out; |
165e4a11 AK |
1042 | |
1043 | if (!dm_task_no_open_count(dmt)) | |
1044 | log_error("Failed to disable open_count"); | |
1045 | ||
f16aea9e | 1046 | if (!dm_task_set_cookie(dmt, cookie, udev_flags)) |
bd90c6b2 AK |
1047 | goto out; |
1048 | ||
165e4a11 AK |
1049 | r = dm_task_run(dmt); |
1050 | ||
1051 | out: | |
1052 | dm_task_destroy(dmt); | |
1053 | ||
1054 | return r; | |
1055 | } | |
1056 | ||
165e4a11 AK |
1057 | /* FIXME Merge with _suspend_node? */ |
1058 | static int _resume_node(const char *name, uint32_t major, uint32_t minor, | |
52b84409 | 1059 | uint32_t read_ahead, uint32_t read_ahead_flags, |
f16aea9e | 1060 | struct dm_info *newinfo, uint32_t *cookie, |
1840aa09 | 1061 | uint16_t udev_flags, int already_suspended) |
165e4a11 AK |
1062 | { |
1063 | struct dm_task *dmt; | |
bd90c6b2 | 1064 | int r = 0; |
165e4a11 AK |
1065 | |
1066 | log_verbose("Resuming %s (%" PRIu32 ":%" PRIu32 ")", name, major, minor); | |
1067 | ||
1068 | if (!(dmt = dm_task_create(DM_DEVICE_RESUME))) { | |
1069 | log_error("Suspend dm_task creation failed for %s", name); | |
1070 | return 0; | |
1071 | } | |
1072 | ||
0b7d16bc AK |
1073 | /* FIXME Kernel should fill in name on return instead */ |
1074 | if (!dm_task_set_name(dmt, name)) { | |
1075 | log_error("Failed to set readahead device name for %s", name); | |
bd90c6b2 | 1076 | goto out; |
0b7d16bc AK |
1077 | } |
1078 | ||
165e4a11 AK |
1079 | if (!dm_task_set_major(dmt, major) || !dm_task_set_minor(dmt, minor)) { |
1080 | log_error("Failed to set device number for %s resumption.", name); | |
bd90c6b2 | 1081 | goto out; |
165e4a11 AK |
1082 | } |
1083 | ||
1084 | if (!dm_task_no_open_count(dmt)) | |
1085 | log_error("Failed to disable open_count"); | |
1086 | ||
52b84409 AK |
1087 | if (!dm_task_set_read_ahead(dmt, read_ahead, read_ahead_flags)) |
1088 | log_error("Failed to set read ahead"); | |
1089 | ||
f16aea9e | 1090 | if (!dm_task_set_cookie(dmt, cookie, udev_flags)) |
bd90c6b2 AK |
1091 | goto out; |
1092 | ||
1840aa09 AK |
1093 | if ((r = dm_task_run(dmt))) { |
1094 | if (already_suspended) | |
1095 | dec_suspended(); | |
165e4a11 | 1096 | r = dm_task_get_info(dmt, newinfo); |
1840aa09 | 1097 | } |
165e4a11 | 1098 | |
bd90c6b2 | 1099 | out: |
165e4a11 AK |
1100 | dm_task_destroy(dmt); |
1101 | ||
1102 | return r; | |
1103 | } | |
1104 | ||
db208f51 | 1105 | static int _suspend_node(const char *name, uint32_t major, uint32_t minor, |
b9ffd32c | 1106 | int skip_lockfs, int no_flush, struct dm_info *newinfo) |
db208f51 AK |
1107 | { |
1108 | struct dm_task *dmt; | |
1109 | int r; | |
1110 | ||
b9ffd32c AK |
1111 | log_verbose("Suspending %s (%" PRIu32 ":%" PRIu32 ")%s%s", |
1112 | name, major, minor, | |
1113 | skip_lockfs ? "" : " with filesystem sync", | |
6e1898a5 | 1114 | no_flush ? "" : " with device flush"); |
db208f51 AK |
1115 | |
1116 | if (!(dmt = dm_task_create(DM_DEVICE_SUSPEND))) { | |
1117 | log_error("Suspend dm_task creation failed for %s", name); | |
1118 | return 0; | |
1119 | } | |
1120 | ||
1121 | if (!dm_task_set_major(dmt, major) || !dm_task_set_minor(dmt, minor)) { | |
1122 | log_error("Failed to set device number for %s suspension.", name); | |
1123 | dm_task_destroy(dmt); | |
1124 | return 0; | |
1125 | } | |
1126 | ||
1127 | if (!dm_task_no_open_count(dmt)) | |
1128 | log_error("Failed to disable open_count"); | |
1129 | ||
c55b1410 AK |
1130 | if (skip_lockfs && !dm_task_skip_lockfs(dmt)) |
1131 | log_error("Failed to set skip_lockfs flag."); | |
1132 | ||
b9ffd32c AK |
1133 | if (no_flush && !dm_task_no_flush(dmt)) |
1134 | log_error("Failed to set no_flush flag."); | |
1135 | ||
1840aa09 AK |
1136 | if ((r = dm_task_run(dmt))) { |
1137 | inc_suspended(); | |
db208f51 | 1138 | r = dm_task_get_info(dmt, newinfo); |
1840aa09 | 1139 | } |
db208f51 | 1140 | |
3e8c6b73 AK |
1141 | dm_task_destroy(dmt); |
1142 | ||
1143 | return r; | |
1144 | } | |
1145 | ||
18e0f934 AK |
1146 | /* |
1147 | * FIXME Don't attempt to deactivate known internal dependencies. | |
1148 | */ | |
1149 | static int _dm_tree_deactivate_children(struct dm_tree_node *dnode, | |
1150 | const char *uuid_prefix, | |
1151 | size_t uuid_prefix_len, | |
1152 | unsigned level) | |
3e8c6b73 | 1153 | { |
b7eb2ad0 | 1154 | int r = 1; |
3e8c6b73 | 1155 | void *handle = NULL; |
b4f1578f | 1156 | struct dm_tree_node *child = dnode; |
3e8c6b73 AK |
1157 | struct dm_info info; |
1158 | const struct dm_info *dinfo; | |
1159 | const char *name; | |
1160 | const char *uuid; | |
1161 | ||
b4f1578f AK |
1162 | while ((child = dm_tree_next_child(&handle, dnode, 0))) { |
1163 | if (!(dinfo = dm_tree_node_get_info(child))) { | |
3e8c6b73 AK |
1164 | stack; |
1165 | continue; | |
1166 | } | |
1167 | ||
b4f1578f | 1168 | if (!(name = dm_tree_node_get_name(child))) { |
3e8c6b73 AK |
1169 | stack; |
1170 | continue; | |
1171 | } | |
1172 | ||
b4f1578f | 1173 | if (!(uuid = dm_tree_node_get_uuid(child))) { |
3e8c6b73 AK |
1174 | stack; |
1175 | continue; | |
1176 | } | |
1177 | ||
1178 | /* Ignore if it doesn't belong to this VG */ | |
2b69db1f | 1179 | if (!_uuid_prefix_matches(uuid, uuid_prefix, uuid_prefix_len)) |
3e8c6b73 | 1180 | continue; |
3e8c6b73 AK |
1181 | |
1182 | /* Refresh open_count */ | |
db208f51 | 1183 | if (!_info_by_dev(dinfo->major, dinfo->minor, 1, &info) || |
f55021f4 | 1184 | !info.exists) |
3e8c6b73 AK |
1185 | continue; |
1186 | ||
f3ef15ef ZK |
1187 | /* Also checking open_count in parent nodes of presuspend_node */ |
1188 | if (info.open_count || | |
1189 | (child->presuspend_node && | |
1190 | !_node_has_closed_parents(child->presuspend_node, | |
1191 | uuid_prefix, uuid_prefix_len))) { | |
18e0f934 AK |
1192 | /* Only report error from (likely non-internal) dependency at top level */ |
1193 | if (!level) { | |
1194 | log_error("Unable to deactivate open %s (%" PRIu32 | |
1195 | ":%" PRIu32 ")", name, info.major, | |
1196 | info.minor); | |
1197 | r = 0; | |
1198 | } | |
f55021f4 AK |
1199 | continue; |
1200 | } | |
1201 | ||
76d1aec8 ZK |
1202 | /* Suspend child node first if requested */ |
1203 | if (child->presuspend_node && | |
1204 | !dm_tree_suspend_children(child, uuid_prefix, uuid_prefix_len)) | |
1205 | continue; | |
1206 | ||
f16aea9e | 1207 | if (!_deactivate_node(name, info.major, info.minor, |
584d1fb7 | 1208 | &child->dtree->cookie, child->udev_flags)) { |
3e8c6b73 AK |
1209 | log_error("Unable to deactivate %s (%" PRIu32 |
1210 | ":%" PRIu32 ")", name, info.major, | |
1211 | info.minor); | |
b7eb2ad0 | 1212 | r = 0; |
3e8c6b73 | 1213 | continue; |
f4249251 AK |
1214 | } else if (info.suspended) |
1215 | dec_suspended(); | |
3e8c6b73 | 1216 | |
18e0f934 AK |
1217 | if (dm_tree_node_num_children(child, 0)) { |
1218 | if (!_dm_tree_deactivate_children(child, uuid_prefix, uuid_prefix_len, level + 1)) | |
b7eb2ad0 | 1219 | return_0; |
18e0f934 | 1220 | } |
3e8c6b73 AK |
1221 | } |
1222 | ||
b7eb2ad0 | 1223 | return r; |
3e8c6b73 | 1224 | } |
db208f51 | 1225 | |
18e0f934 AK |
1226 | int dm_tree_deactivate_children(struct dm_tree_node *dnode, |
1227 | const char *uuid_prefix, | |
1228 | size_t uuid_prefix_len) | |
1229 | { | |
1230 | return _dm_tree_deactivate_children(dnode, uuid_prefix, uuid_prefix_len, 0); | |
1231 | } | |
1232 | ||
c55b1410 AK |
1233 | void dm_tree_skip_lockfs(struct dm_tree_node *dnode) |
1234 | { | |
1235 | dnode->dtree->skip_lockfs = 1; | |
1236 | } | |
1237 | ||
b9ffd32c AK |
1238 | void dm_tree_use_no_flush_suspend(struct dm_tree_node *dnode) |
1239 | { | |
1240 | dnode->dtree->no_flush = 1; | |
1241 | } | |
1242 | ||
b4f1578f | 1243 | int dm_tree_suspend_children(struct dm_tree_node *dnode, |
08e64ce5 ZK |
1244 | const char *uuid_prefix, |
1245 | size_t uuid_prefix_len) | |
db208f51 | 1246 | { |
68085c93 | 1247 | int r = 1; |
db208f51 | 1248 | void *handle = NULL; |
b4f1578f | 1249 | struct dm_tree_node *child = dnode; |
db208f51 AK |
1250 | struct dm_info info, newinfo; |
1251 | const struct dm_info *dinfo; | |
1252 | const char *name; | |
1253 | const char *uuid; | |
1254 | ||
690a5da2 | 1255 | /* Suspend nodes at this level of the tree */ |
b4f1578f AK |
1256 | while ((child = dm_tree_next_child(&handle, dnode, 0))) { |
1257 | if (!(dinfo = dm_tree_node_get_info(child))) { | |
db208f51 AK |
1258 | stack; |
1259 | continue; | |
1260 | } | |
1261 | ||
b4f1578f | 1262 | if (!(name = dm_tree_node_get_name(child))) { |
db208f51 AK |
1263 | stack; |
1264 | continue; | |
1265 | } | |
1266 | ||
b4f1578f | 1267 | if (!(uuid = dm_tree_node_get_uuid(child))) { |
db208f51 AK |
1268 | stack; |
1269 | continue; | |
1270 | } | |
1271 | ||
1272 | /* Ignore if it doesn't belong to this VG */ | |
2b69db1f | 1273 | if (!_uuid_prefix_matches(uuid, uuid_prefix, uuid_prefix_len)) |
db208f51 AK |
1274 | continue; |
1275 | ||
690a5da2 AK |
1276 | /* Ensure immediate parents are already suspended */ |
1277 | if (!_children_suspended(child, 1, uuid_prefix, uuid_prefix_len)) | |
1278 | continue; | |
1279 | ||
db208f51 | 1280 | if (!_info_by_dev(dinfo->major, dinfo->minor, 0, &info) || |
b700541f | 1281 | !info.exists || info.suspended) |
db208f51 AK |
1282 | continue; |
1283 | ||
c55b1410 | 1284 | if (!_suspend_node(name, info.major, info.minor, |
b9ffd32c AK |
1285 | child->dtree->skip_lockfs, |
1286 | child->dtree->no_flush, &newinfo)) { | |
db208f51 AK |
1287 | log_error("Unable to suspend %s (%" PRIu32 |
1288 | ":%" PRIu32 ")", name, info.major, | |
1289 | info.minor); | |
68085c93 | 1290 | r = 0; |
db208f51 AK |
1291 | continue; |
1292 | } | |
1293 | ||
1294 | /* Update cached info */ | |
1295 | child->info = newinfo; | |
690a5da2 AK |
1296 | } |
1297 | ||
1298 | /* Then suspend any child nodes */ | |
1299 | handle = NULL; | |
1300 | ||
b4f1578f AK |
1301 | while ((child = dm_tree_next_child(&handle, dnode, 0))) { |
1302 | if (!(uuid = dm_tree_node_get_uuid(child))) { | |
690a5da2 AK |
1303 | stack; |
1304 | continue; | |
1305 | } | |
1306 | ||
1307 | /* Ignore if it doesn't belong to this VG */ | |
87f98002 | 1308 | if (!_uuid_prefix_matches(uuid, uuid_prefix, uuid_prefix_len)) |
690a5da2 | 1309 | continue; |
db208f51 | 1310 | |
b4f1578f | 1311 | if (dm_tree_node_num_children(child, 0)) |
68085c93 MS |
1312 | if (!dm_tree_suspend_children(child, uuid_prefix, uuid_prefix_len)) |
1313 | return_0; | |
db208f51 AK |
1314 | } |
1315 | ||
68085c93 | 1316 | return r; |
db208f51 AK |
1317 | } |
1318 | ||
b4f1578f | 1319 | int dm_tree_activate_children(struct dm_tree_node *dnode, |
db208f51 AK |
1320 | const char *uuid_prefix, |
1321 | size_t uuid_prefix_len) | |
1322 | { | |
2ca6b865 | 1323 | int r = 1; |
db208f51 | 1324 | void *handle = NULL; |
b4f1578f | 1325 | struct dm_tree_node *child = dnode; |
165e4a11 AK |
1326 | struct dm_info newinfo; |
1327 | const char *name; | |
db208f51 | 1328 | const char *uuid; |
56c28292 | 1329 | int priority; |
db208f51 | 1330 | |
165e4a11 | 1331 | /* Activate children first */ |
b4f1578f AK |
1332 | while ((child = dm_tree_next_child(&handle, dnode, 0))) { |
1333 | if (!(uuid = dm_tree_node_get_uuid(child))) { | |
165e4a11 AK |
1334 | stack; |
1335 | continue; | |
db208f51 AK |
1336 | } |
1337 | ||
908db078 AK |
1338 | if (!_uuid_prefix_matches(uuid, uuid_prefix, uuid_prefix_len)) |
1339 | continue; | |
db208f51 | 1340 | |
b4f1578f | 1341 | if (dm_tree_node_num_children(child, 0)) |
2ca6b865 MS |
1342 | if (!dm_tree_activate_children(child, uuid_prefix, uuid_prefix_len)) |
1343 | return_0; | |
56c28292 | 1344 | } |
165e4a11 | 1345 | |
56c28292 | 1346 | handle = NULL; |
165e4a11 | 1347 | |
aa6f4e51 | 1348 | for (priority = 0; priority < 3; priority++) { |
56c28292 AK |
1349 | while ((child = dm_tree_next_child(&handle, dnode, 0))) { |
1350 | if (!(uuid = dm_tree_node_get_uuid(child))) { | |
1351 | stack; | |
1352 | continue; | |
165e4a11 | 1353 | } |
165e4a11 | 1354 | |
56c28292 AK |
1355 | if (!_uuid_prefix_matches(uuid, uuid_prefix, uuid_prefix_len)) |
1356 | continue; | |
165e4a11 | 1357 | |
56c28292 AK |
1358 | if (priority != child->activation_priority) |
1359 | continue; | |
165e4a11 | 1360 | |
56c28292 AK |
1361 | if (!(name = dm_tree_node_get_name(child))) { |
1362 | stack; | |
1363 | continue; | |
1364 | } | |
1365 | ||
1366 | /* Rename? */ | |
1367 | if (child->props.new_name) { | |
bd90c6b2 | 1368 | if (!_rename_node(name, child->props.new_name, child->info.major, |
f16aea9e PR |
1369 | child->info.minor, &child->dtree->cookie, |
1370 | child->udev_flags)) { | |
56c28292 AK |
1371 | log_error("Failed to rename %s (%" PRIu32 |
1372 | ":%" PRIu32 ") to %s", name, child->info.major, | |
1373 | child->info.minor, child->props.new_name); | |
1374 | return 0; | |
1375 | } | |
1376 | child->name = child->props.new_name; | |
1377 | child->props.new_name = NULL; | |
1378 | } | |
1379 | ||
1380 | if (!child->info.inactive_table && !child->info.suspended) | |
1381 | continue; | |
1382 | ||
bafa2f39 | 1383 | if (!_resume_node(child->name, child->info.major, child->info.minor, |
bd90c6b2 | 1384 | child->props.read_ahead, child->props.read_ahead_flags, |
1840aa09 | 1385 | &newinfo, &child->dtree->cookie, child->udev_flags, child->info.suspended)) { |
56c28292 | 1386 | log_error("Unable to resume %s (%" PRIu32 |
bafa2f39 | 1387 | ":%" PRIu32 ")", child->name, child->info.major, |
56c28292 | 1388 | child->info.minor); |
2ca6b865 | 1389 | r = 0; |
56c28292 AK |
1390 | continue; |
1391 | } | |
1392 | ||
1393 | /* Update cached info */ | |
1394 | child->info = newinfo; | |
1395 | } | |
db208f51 AK |
1396 | } |
1397 | ||
165e4a11 AK |
1398 | handle = NULL; |
1399 | ||
2ca6b865 | 1400 | return r; |
165e4a11 AK |
1401 | } |
1402 | ||
b4f1578f | 1403 | static int _create_node(struct dm_tree_node *dnode) |
165e4a11 AK |
1404 | { |
1405 | int r = 0; | |
1406 | struct dm_task *dmt; | |
1407 | ||
1408 | log_verbose("Creating %s", dnode->name); | |
1409 | ||
1410 | if (!(dmt = dm_task_create(DM_DEVICE_CREATE))) { | |
1411 | log_error("Create dm_task creation failed for %s", dnode->name); | |
1412 | return 0; | |
1413 | } | |
1414 | ||
1415 | if (!dm_task_set_name(dmt, dnode->name)) { | |
1416 | log_error("Failed to set device name for %s", dnode->name); | |
1417 | goto out; | |
1418 | } | |
1419 | ||
1420 | if (!dm_task_set_uuid(dmt, dnode->uuid)) { | |
1421 | log_error("Failed to set uuid for %s", dnode->name); | |
1422 | goto out; | |
1423 | } | |
1424 | ||
1425 | if (dnode->props.major && | |
1426 | (!dm_task_set_major(dmt, dnode->props.major) || | |
1427 | !dm_task_set_minor(dmt, dnode->props.minor))) { | |
1428 | log_error("Failed to set device number for %s creation.", dnode->name); | |
1429 | goto out; | |
1430 | } | |
1431 | ||
1432 | if (dnode->props.read_only && !dm_task_set_ro(dmt)) { | |
1433 | log_error("Failed to set read only flag for %s", dnode->name); | |
1434 | goto out; | |
1435 | } | |
1436 | ||
1437 | if (!dm_task_no_open_count(dmt)) | |
1438 | log_error("Failed to disable open_count"); | |
1439 | ||
1440 | if ((r = dm_task_run(dmt))) | |
1441 | r = dm_task_get_info(dmt, &dnode->info); | |
1442 | ||
1443 | out: | |
1444 | dm_task_destroy(dmt); | |
1445 | ||
1446 | return r; | |
1447 | } | |
1448 | ||
1449 | ||
b4f1578f | 1450 | static int _build_dev_string(char *devbuf, size_t bufsize, struct dm_tree_node *node) |
165e4a11 AK |
1451 | { |
1452 | if (!dm_format_dev(devbuf, bufsize, node->info.major, node->info.minor)) { | |
40e5fd8b AK |
1453 | log_error("Failed to format %s device number for %s as dm " |
1454 | "target (%u,%u)", | |
1455 | node->name, node->uuid, node->info.major, node->info.minor); | |
1456 | return 0; | |
165e4a11 AK |
1457 | } |
1458 | ||
1459 | return 1; | |
1460 | } | |
1461 | ||
ffa9b6a5 ZK |
1462 | /* simplify string emiting code */ |
1463 | #define EMIT_PARAMS(p, str...)\ | |
7b6c011c AK |
1464 | do {\ |
1465 | int w;\ | |
1466 | if ((w = dm_snprintf(params + p, paramsize - (size_t) p, str)) < 0) {\ | |
1467 | stack; /* Out of space */\ | |
1468 | return -1;\ | |
1469 | }\ | |
1470 | p += w;\ | |
1471 | } while (0) | |
ffa9b6a5 | 1472 | |
3c74075f JEB |
1473 | /* |
1474 | * _emit_areas_line | |
1475 | * | |
1476 | * Returns: 1 on success, 0 on failure | |
1477 | */ | |
08f1ddea | 1478 | static int _emit_areas_line(struct dm_task *dmt __attribute__((unused)), |
4dcaa230 AK |
1479 | struct load_segment *seg, char *params, |
1480 | size_t paramsize, int *pos) | |
165e4a11 AK |
1481 | { |
1482 | struct seg_area *area; | |
7d7d93ac | 1483 | char devbuf[DM_FORMAT_DEV_BUFSIZE]; |
609faae9 | 1484 | unsigned first_time = 1; |
db3c1ac1 | 1485 | const char *logtype, *synctype; |
b262f3e1 | 1486 | unsigned log_parm_count; |
165e4a11 | 1487 | |
2c44337b | 1488 | dm_list_iterate_items(area, &seg->areas) { |
b262f3e1 ZK |
1489 | switch (seg->type) { |
1490 | case SEG_REPLICATOR_DEV: | |
6d04311e JEB |
1491 | if (!_build_dev_string(devbuf, sizeof(devbuf), area->dev_node)) |
1492 | return_0; | |
1493 | ||
b262f3e1 ZK |
1494 | EMIT_PARAMS(*pos, " %d 1 %s", area->rsite_index, devbuf); |
1495 | if (first_time) | |
1496 | EMIT_PARAMS(*pos, " nolog 0"); | |
1497 | else { | |
1498 | /* Remote devices */ | |
1499 | log_parm_count = (area->flags & | |
1500 | (DM_NOSYNC | DM_FORCESYNC)) ? 2 : 1; | |
1501 | ||
1502 | if (!area->slog) { | |
1503 | devbuf[0] = 0; /* Only core log parameters */ | |
1504 | logtype = "core"; | |
1505 | } else { | |
1506 | devbuf[0] = ' '; /* Extra space before device name */ | |
1507 | if (!_build_dev_string(devbuf + 1, | |
1508 | sizeof(devbuf) - 1, | |
1509 | area->slog)) | |
1510 | return_0; | |
1511 | logtype = "disk"; | |
1512 | log_parm_count++; /* Extra sync log device name parameter */ | |
1513 | } | |
1514 | ||
1515 | EMIT_PARAMS(*pos, " %s %u%s %" PRIu64, logtype, | |
1516 | log_parm_count, devbuf, area->region_size); | |
1517 | ||
db3c1ac1 AK |
1518 | synctype = (area->flags & DM_NOSYNC) ? |
1519 | " nosync" : (area->flags & DM_FORCESYNC) ? | |
1520 | " sync" : NULL; | |
b262f3e1 | 1521 | |
db3c1ac1 AK |
1522 | if (synctype) |
1523 | EMIT_PARAMS(*pos, "%s", synctype); | |
b262f3e1 ZK |
1524 | } |
1525 | break; | |
cac52ca4 JEB |
1526 | case SEG_RAID1: |
1527 | case SEG_RAID4: | |
1528 | case SEG_RAID5_LA: | |
1529 | case SEG_RAID5_RA: | |
1530 | case SEG_RAID5_LS: | |
1531 | case SEG_RAID5_RS: | |
1532 | case SEG_RAID6_ZR: | |
1533 | case SEG_RAID6_NR: | |
1534 | case SEG_RAID6_NC: | |
6d04311e JEB |
1535 | if (!area->dev_node) { |
1536 | EMIT_PARAMS(*pos, " -"); | |
1537 | break; | |
1538 | } | |
1539 | if (!_build_dev_string(devbuf, sizeof(devbuf), area->dev_node)) | |
1540 | return_0; | |
1541 | ||
cac52ca4 JEB |
1542 | EMIT_PARAMS(*pos, " %s", devbuf); |
1543 | break; | |
b262f3e1 | 1544 | default: |
6d04311e JEB |
1545 | if (!_build_dev_string(devbuf, sizeof(devbuf), area->dev_node)) |
1546 | return_0; | |
1547 | ||
b262f3e1 ZK |
1548 | EMIT_PARAMS(*pos, "%s%s %" PRIu64, first_time ? "" : " ", |
1549 | devbuf, area->offset); | |
1550 | } | |
609faae9 AK |
1551 | |
1552 | first_time = 0; | |
165e4a11 AK |
1553 | } |
1554 | ||
1555 | return 1; | |
1556 | } | |
1557 | ||
b262f3e1 ZK |
1558 | static int _replicator_emit_segment_line(const struct load_segment *seg, char *params, |
1559 | size_t paramsize, int *pos) | |
1560 | { | |
1561 | const struct load_segment *rlog_seg; | |
1562 | struct replicator_site *rsite; | |
1563 | char rlogbuf[DM_FORMAT_DEV_BUFSIZE]; | |
1564 | unsigned parm_count; | |
1565 | ||
1566 | if (!seg->log || !_build_dev_string(rlogbuf, sizeof(rlogbuf), seg->log)) | |
1567 | return_0; | |
1568 | ||
1569 | rlog_seg = dm_list_item(dm_list_last(&seg->log->props.segs), | |
1570 | struct load_segment); | |
1571 | ||
1572 | EMIT_PARAMS(*pos, "%s 4 %s 0 auto %" PRIu64, | |
1573 | seg->rlog_type, rlogbuf, rlog_seg->size); | |
1574 | ||
1575 | dm_list_iterate_items(rsite, &seg->rsites) { | |
1576 | parm_count = (rsite->fall_behind_data | |
1577 | || rsite->fall_behind_ios | |
1578 | || rsite->async_timeout) ? 4 : 2; | |
1579 | ||
1580 | EMIT_PARAMS(*pos, " blockdev %u %u %s", parm_count, rsite->rsite_index, | |
1581 | (rsite->mode == DM_REPLICATOR_SYNC) ? "synchronous" : "asynchronous"); | |
1582 | ||
1583 | if (rsite->fall_behind_data) | |
1584 | EMIT_PARAMS(*pos, " data %" PRIu64, rsite->fall_behind_data); | |
1585 | else if (rsite->fall_behind_ios) | |
1586 | EMIT_PARAMS(*pos, " ios %" PRIu32, rsite->fall_behind_ios); | |
1587 | else if (rsite->async_timeout) | |
1588 | EMIT_PARAMS(*pos, " timeout %" PRIu32, rsite->async_timeout); | |
1589 | } | |
1590 | ||
1591 | return 1; | |
1592 | } | |
1593 | ||
3c74075f | 1594 | /* |
3c74075f JEB |
1595 | * Returns: 1 on success, 0 on failure |
1596 | */ | |
536f0e08 AK |
1597 | static int _mirror_emit_segment_line(struct dm_task *dmt, uint32_t major, |
1598 | uint32_t minor, struct load_segment *seg, | |
1599 | uint64_t *seg_start, char *params, | |
1600 | size_t paramsize) | |
165e4a11 | 1601 | { |
8f26e18c JEB |
1602 | int block_on_error = 0; |
1603 | int handle_errors = 0; | |
1604 | int dm_log_userspace = 0; | |
1605 | struct utsname uts; | |
dbcb64b8 | 1606 | unsigned log_parm_count; |
b39fdcf4 | 1607 | int pos = 0, parts; |
7d7d93ac | 1608 | char logbuf[DM_FORMAT_DEV_BUFSIZE]; |
dbcb64b8 | 1609 | const char *logtype; |
b39fdcf4 | 1610 | unsigned kmaj = 0, kmin = 0, krel = 0; |
165e4a11 | 1611 | |
b39fdcf4 MB |
1612 | if (uname(&uts) == -1) { |
1613 | log_error("Cannot read kernel release version."); | |
1614 | return 0; | |
1615 | } | |
1616 | ||
1617 | /* Kernels with a major number of 2 always had 3 parts. */ | |
1618 | parts = sscanf(uts.release, "%u.%u.%u", &kmaj, &kmin, &krel); | |
1619 | if (parts < 1 || (kmaj < 3 && parts < 3)) { | |
1620 | log_error("Wrong kernel release version %s.", uts.release); | |
30a65310 ZK |
1621 | return 0; |
1622 | } | |
67b25ed4 | 1623 | |
8f26e18c JEB |
1624 | if ((seg->flags & DM_BLOCK_ON_ERROR)) { |
1625 | /* | |
1626 | * Originally, block_on_error was an argument to the log | |
1627 | * portion of the mirror CTR table. It was renamed to | |
1628 | * "handle_errors" and now resides in the 'features' | |
1629 | * section of the mirror CTR table (i.e. at the end). | |
1630 | * | |
1631 | * We can identify whether to use "block_on_error" or | |
1632 | * "handle_errors" by the dm-mirror module's version | |
1633 | * number (>= 1.12) or by the kernel version (>= 2.6.22). | |
1634 | */ | |
ba61f848 | 1635 | if (KERNEL_VERSION(kmaj, kmin, krel) >= KERNEL_VERSION(2, 6, 22)) |
8f26e18c JEB |
1636 | handle_errors = 1; |
1637 | else | |
1638 | block_on_error = 1; | |
1639 | } | |
1640 | ||
1641 | if (seg->clustered) { | |
1642 | /* Cluster mirrors require a UUID */ | |
1643 | if (!seg->uuid) | |
1644 | return_0; | |
1645 | ||
1646 | /* | |
1647 | * Cluster mirrors used to have their own log | |
1648 | * types. Now they are accessed through the | |
1649 | * userspace log type. | |
1650 | * | |
1651 | * The dm-log-userspace module was added to the | |
1652 | * 2.6.31 kernel. | |
1653 | */ | |
ba61f848 | 1654 | if (KERNEL_VERSION(kmaj, kmin, krel) >= KERNEL_VERSION(2, 6, 31)) |
8f26e18c JEB |
1655 | dm_log_userspace = 1; |
1656 | } | |
1657 | ||
1658 | /* Region size */ | |
1659 | log_parm_count = 1; | |
1660 | ||
1661 | /* [no]sync, block_on_error etc. */ | |
1662 | log_parm_count += hweight32(seg->flags); | |
311d6d81 | 1663 | |
8f26e18c JEB |
1664 | /* "handle_errors" is a feature arg now */ |
1665 | if (handle_errors) | |
1666 | log_parm_count--; | |
1667 | ||
1668 | /* DM_CORELOG does not count in the param list */ | |
1669 | if (seg->flags & DM_CORELOG) | |
1670 | log_parm_count--; | |
1671 | ||
1672 | if (seg->clustered) { | |
1673 | log_parm_count++; /* For UUID */ | |
1674 | ||
1675 | if (!dm_log_userspace) | |
ffa9b6a5 | 1676 | EMIT_PARAMS(pos, "clustered-"); |
49b95a5e JEB |
1677 | else |
1678 | /* For clustered-* type field inserted later */ | |
1679 | log_parm_count++; | |
8f26e18c | 1680 | } |
dbcb64b8 | 1681 | |
8f26e18c JEB |
1682 | if (!seg->log) |
1683 | logtype = "core"; | |
1684 | else { | |
1685 | logtype = "disk"; | |
1686 | log_parm_count++; | |
1687 | if (!_build_dev_string(logbuf, sizeof(logbuf), seg->log)) | |
1688 | return_0; | |
1689 | } | |
dbcb64b8 | 1690 | |
8f26e18c JEB |
1691 | if (dm_log_userspace) |
1692 | EMIT_PARAMS(pos, "userspace %u %s clustered-%s", | |
1693 | log_parm_count, seg->uuid, logtype); | |
1694 | else | |
ffa9b6a5 | 1695 | EMIT_PARAMS(pos, "%s %u", logtype, log_parm_count); |
dbcb64b8 | 1696 | |
8f26e18c JEB |
1697 | if (seg->log) |
1698 | EMIT_PARAMS(pos, " %s", logbuf); | |
1699 | ||
1700 | EMIT_PARAMS(pos, " %u", seg->region_size); | |
dbcb64b8 | 1701 | |
8f26e18c JEB |
1702 | if (seg->clustered && !dm_log_userspace) |
1703 | EMIT_PARAMS(pos, " %s", seg->uuid); | |
67b25ed4 | 1704 | |
8f26e18c JEB |
1705 | if ((seg->flags & DM_NOSYNC)) |
1706 | EMIT_PARAMS(pos, " nosync"); | |
1707 | else if ((seg->flags & DM_FORCESYNC)) | |
1708 | EMIT_PARAMS(pos, " sync"); | |
dbcb64b8 | 1709 | |
8f26e18c JEB |
1710 | if (block_on_error) |
1711 | EMIT_PARAMS(pos, " block_on_error"); | |
1712 | ||
1713 | EMIT_PARAMS(pos, " %u ", seg->mirror_area_count); | |
1714 | ||
5f3325fc | 1715 | if (_emit_areas_line(dmt, seg, params, paramsize, &pos) <= 0) |
3c74075f | 1716 | return_0; |
dbcb64b8 | 1717 | |
8f26e18c JEB |
1718 | if (handle_errors) |
1719 | EMIT_PARAMS(pos, " 1 handle_errors"); | |
ffa9b6a5 | 1720 | |
3c74075f | 1721 | return 1; |
8f26e18c JEB |
1722 | } |
1723 | ||
cac52ca4 JEB |
1724 | static int _raid_emit_segment_line(struct dm_task *dmt, uint32_t major, |
1725 | uint32_t minor, struct load_segment *seg, | |
1726 | uint64_t *seg_start, char *params, | |
1727 | size_t paramsize) | |
1728 | { | |
f439e65b | 1729 | uint32_t i, *tmp; |
cac52ca4 JEB |
1730 | int param_count = 1; /* mandatory 'chunk size'/'stripe size' arg */ |
1731 | int pos = 0; | |
1732 | ||
1733 | if ((seg->flags & DM_NOSYNC) || (seg->flags & DM_FORCESYNC)) | |
1734 | param_count++; | |
1735 | ||
1736 | if (seg->region_size) | |
1737 | param_count += 2; | |
1738 | ||
f439e65b JEB |
1739 | tmp = (uint32_t *)(&seg->rebuilds); /* rebuilds is 64-bit */ |
1740 | param_count += 2 * hweight32(tmp[0]); | |
1741 | param_count += 2 * hweight32(tmp[1]); | |
1742 | ||
cac52ca4 JEB |
1743 | if ((seg->type == SEG_RAID1) && seg->stripe_size) |
1744 | log_error("WARNING: Ignoring RAID1 stripe size"); | |
1745 | ||
1746 | EMIT_PARAMS(pos, "%s %d %u", dm_segtypes[seg->type].target, | |
1747 | param_count, seg->stripe_size); | |
1748 | ||
1749 | if (seg->flags & DM_NOSYNC) | |
1750 | EMIT_PARAMS(pos, " nosync"); | |
1751 | else if (seg->flags & DM_FORCESYNC) | |
1752 | EMIT_PARAMS(pos, " sync"); | |
1753 | ||
1754 | if (seg->region_size) | |
1755 | EMIT_PARAMS(pos, " region_size %u", seg->region_size); | |
1756 | ||
f439e65b JEB |
1757 | for (i = 0; i < (seg->area_count / 2); i++) |
1758 | if (seg->rebuilds & (1 << i)) | |
1759 | EMIT_PARAMS(pos, " rebuild %u", i); | |
1760 | ||
cac52ca4 JEB |
1761 | /* Print number of metadata/data device pairs */ |
1762 | EMIT_PARAMS(pos, " %u", seg->area_count/2); | |
1763 | ||
1764 | if (_emit_areas_line(dmt, seg, params, paramsize, &pos) <= 0) | |
1765 | return_0; | |
1766 | ||
1767 | return 1; | |
1768 | } | |
1769 | ||
8f26e18c JEB |
1770 | static int _emit_segment_line(struct dm_task *dmt, uint32_t major, |
1771 | uint32_t minor, struct load_segment *seg, | |
1772 | uint64_t *seg_start, char *params, | |
1773 | size_t paramsize) | |
1774 | { | |
1775 | int pos = 0; | |
1776 | int r; | |
cac52ca4 | 1777 | int target_type_is_raid = 0; |
8f26e18c | 1778 | char originbuf[DM_FORMAT_DEV_BUFSIZE], cowbuf[DM_FORMAT_DEV_BUFSIZE]; |
dbcb64b8 | 1779 | |
8f26e18c JEB |
1780 | switch(seg->type) { |
1781 | case SEG_ERROR: | |
1782 | case SEG_ZERO: | |
1783 | case SEG_LINEAR: | |
1784 | break; | |
1785 | case SEG_MIRRORED: | |
1786 | /* Mirrors are pretty complicated - now in separate function */ | |
536f0e08 AK |
1787 | r = _mirror_emit_segment_line(dmt, major, minor, seg, seg_start, |
1788 | params, paramsize); | |
3c74075f JEB |
1789 | if (!r) |
1790 | return_0; | |
165e4a11 | 1791 | break; |
b262f3e1 ZK |
1792 | case SEG_REPLICATOR: |
1793 | if ((r = _replicator_emit_segment_line(seg, params, paramsize, | |
1794 | &pos)) <= 0) { | |
1795 | stack; | |
1796 | return r; | |
1797 | } | |
1798 | break; | |
1799 | case SEG_REPLICATOR_DEV: | |
1800 | if (!seg->replicator || !_build_dev_string(originbuf, | |
1801 | sizeof(originbuf), | |
1802 | seg->replicator)) | |
1803 | return_0; | |
1804 | ||
1805 | EMIT_PARAMS(pos, "%s %" PRIu64, originbuf, seg->rdevice_index); | |
1806 | break; | |
165e4a11 | 1807 | case SEG_SNAPSHOT: |
aa6f4e51 | 1808 | case SEG_SNAPSHOT_MERGE: |
b4f1578f AK |
1809 | if (!_build_dev_string(originbuf, sizeof(originbuf), seg->origin)) |
1810 | return_0; | |
1811 | if (!_build_dev_string(cowbuf, sizeof(cowbuf), seg->cow)) | |
1812 | return_0; | |
ffa9b6a5 ZK |
1813 | EMIT_PARAMS(pos, "%s %s %c %d", originbuf, cowbuf, |
1814 | seg->persistent ? 'P' : 'N', seg->chunk_size); | |
165e4a11 AK |
1815 | break; |
1816 | case SEG_SNAPSHOT_ORIGIN: | |
b4f1578f AK |
1817 | if (!_build_dev_string(originbuf, sizeof(originbuf), seg->origin)) |
1818 | return_0; | |
ffa9b6a5 | 1819 | EMIT_PARAMS(pos, "%s", originbuf); |
165e4a11 AK |
1820 | break; |
1821 | case SEG_STRIPED: | |
609faae9 | 1822 | EMIT_PARAMS(pos, "%u %u ", seg->area_count, seg->stripe_size); |
165e4a11 | 1823 | break; |
12ca060e | 1824 | case SEG_CRYPT: |
609faae9 | 1825 | EMIT_PARAMS(pos, "%s%s%s%s%s %s %" PRIu64 " ", seg->cipher, |
12ca060e MB |
1826 | seg->chainmode ? "-" : "", seg->chainmode ?: "", |
1827 | seg->iv ? "-" : "", seg->iv ?: "", seg->key, | |
1828 | seg->iv_offset != DM_CRYPT_IV_DEFAULT ? | |
1829 | seg->iv_offset : *seg_start); | |
1830 | break; | |
cac52ca4 JEB |
1831 | case SEG_RAID1: |
1832 | case SEG_RAID4: | |
1833 | case SEG_RAID5_LA: | |
1834 | case SEG_RAID5_RA: | |
1835 | case SEG_RAID5_LS: | |
1836 | case SEG_RAID5_RS: | |
1837 | case SEG_RAID6_ZR: | |
1838 | case SEG_RAID6_NR: | |
1839 | case SEG_RAID6_NC: | |
1840 | target_type_is_raid = 1; | |
1841 | r = _raid_emit_segment_line(dmt, major, minor, seg, seg_start, | |
1842 | params, paramsize); | |
1843 | if (!r) | |
1844 | return_0; | |
1845 | ||
1846 | break; | |
165e4a11 AK |
1847 | } |
1848 | ||
1849 | switch(seg->type) { | |
1850 | case SEG_ERROR: | |
b262f3e1 | 1851 | case SEG_REPLICATOR: |
165e4a11 AK |
1852 | case SEG_SNAPSHOT: |
1853 | case SEG_SNAPSHOT_ORIGIN: | |
aa6f4e51 | 1854 | case SEG_SNAPSHOT_MERGE: |
165e4a11 AK |
1855 | case SEG_ZERO: |
1856 | break; | |
12ca060e | 1857 | case SEG_CRYPT: |
165e4a11 | 1858 | case SEG_LINEAR: |
b262f3e1 | 1859 | case SEG_REPLICATOR_DEV: |
165e4a11 AK |
1860 | case SEG_STRIPED: |
1861 | if ((r = _emit_areas_line(dmt, seg, params, paramsize, &pos)) <= 0) { | |
1862 | stack; | |
1863 | return r; | |
1864 | } | |
b6793963 AK |
1865 | if (!params[0]) { |
1866 | log_error("No parameters supplied for %s target " | |
1867 | "%u:%u.", dm_segtypes[seg->type].target, | |
812e10ac | 1868 | major, minor); |
b6793963 AK |
1869 | return 0; |
1870 | } | |
165e4a11 AK |
1871 | break; |
1872 | } | |
1873 | ||
4b2cae46 AK |
1874 | log_debug("Adding target to (%" PRIu32 ":%" PRIu32 "): %" PRIu64 |
1875 | " %" PRIu64 " %s %s", major, minor, | |
f439e65b JEB |
1876 | *seg_start, seg->size, target_type_is_raid ? "raid" : |
1877 | dm_segtypes[seg->type].target, params); | |
165e4a11 | 1878 | |
cac52ca4 JEB |
1879 | if (!dm_task_add_target(dmt, *seg_start, seg->size, |
1880 | target_type_is_raid ? "raid" : | |
1881 | dm_segtypes[seg->type].target, params)) | |
b4f1578f | 1882 | return_0; |
165e4a11 AK |
1883 | |
1884 | *seg_start += seg->size; | |
1885 | ||
1886 | return 1; | |
1887 | } | |
1888 | ||
ffa9b6a5 ZK |
1889 | #undef EMIT_PARAMS |
1890 | ||
4b2cae46 AK |
1891 | static int _emit_segment(struct dm_task *dmt, uint32_t major, uint32_t minor, |
1892 | struct load_segment *seg, uint64_t *seg_start) | |
165e4a11 AK |
1893 | { |
1894 | char *params; | |
1895 | size_t paramsize = 4096; | |
1896 | int ret; | |
1897 | ||
1898 | do { | |
1899 | if (!(params = dm_malloc(paramsize))) { | |
1900 | log_error("Insufficient space for target parameters."); | |
1901 | return 0; | |
1902 | } | |
1903 | ||
12ea7cb1 | 1904 | params[0] = '\0'; |
4b2cae46 AK |
1905 | ret = _emit_segment_line(dmt, major, minor, seg, seg_start, |
1906 | params, paramsize); | |
165e4a11 AK |
1907 | dm_free(params); |
1908 | ||
1909 | if (!ret) | |
1910 | stack; | |
1911 | ||
1912 | if (ret >= 0) | |
1913 | return ret; | |
1914 | ||
1915 | log_debug("Insufficient space in params[%" PRIsize_t | |
1916 | "] for target parameters.", paramsize); | |
1917 | ||
1918 | paramsize *= 2; | |
1919 | } while (paramsize < MAX_TARGET_PARAMSIZE); | |
1920 | ||
1921 | log_error("Target parameter size too big. Aborting."); | |
1922 | return 0; | |
1923 | } | |
1924 | ||
b4f1578f | 1925 | static int _load_node(struct dm_tree_node *dnode) |
165e4a11 AK |
1926 | { |
1927 | int r = 0; | |
1928 | struct dm_task *dmt; | |
1929 | struct load_segment *seg; | |
df390f17 | 1930 | uint64_t seg_start = 0, existing_table_size; |
165e4a11 | 1931 | |
4b2cae46 AK |
1932 | log_verbose("Loading %s table (%" PRIu32 ":%" PRIu32 ")", dnode->name, |
1933 | dnode->info.major, dnode->info.minor); | |
165e4a11 AK |
1934 | |
1935 | if (!(dmt = dm_task_create(DM_DEVICE_RELOAD))) { | |
1936 | log_error("Reload dm_task creation failed for %s", dnode->name); | |
1937 | return 0; | |
1938 | } | |
1939 | ||
1940 | if (!dm_task_set_major(dmt, dnode->info.major) || | |
1941 | !dm_task_set_minor(dmt, dnode->info.minor)) { | |
1942 | log_error("Failed to set device number for %s reload.", dnode->name); | |
1943 | goto out; | |
1944 | } | |
1945 | ||
1946 | if (dnode->props.read_only && !dm_task_set_ro(dmt)) { | |
1947 | log_error("Failed to set read only flag for %s", dnode->name); | |
1948 | goto out; | |
1949 | } | |
1950 | ||
1951 | if (!dm_task_no_open_count(dmt)) | |
1952 | log_error("Failed to disable open_count"); | |
1953 | ||
2c44337b | 1954 | dm_list_iterate_items(seg, &dnode->props.segs) |
4b2cae46 AK |
1955 | if (!_emit_segment(dmt, dnode->info.major, dnode->info.minor, |
1956 | seg, &seg_start)) | |
b4f1578f | 1957 | goto_out; |
165e4a11 | 1958 | |
ec289b64 AK |
1959 | if (!dm_task_suppress_identical_reload(dmt)) |
1960 | log_error("Failed to suppress reload of identical tables."); | |
1961 | ||
1962 | if ((r = dm_task_run(dmt))) { | |
165e4a11 | 1963 | r = dm_task_get_info(dmt, &dnode->info); |
ec289b64 AK |
1964 | if (r && !dnode->info.inactive_table) |
1965 | log_verbose("Suppressed %s identical table reload.", | |
1966 | dnode->name); | |
bb875bb9 | 1967 | |
df390f17 | 1968 | existing_table_size = dm_task_get_existing_table_size(dmt); |
bb875bb9 | 1969 | if ((dnode->props.size_changed = |
df390f17 | 1970 | (existing_table_size == seg_start) ? 0 : 1)) { |
bb875bb9 | 1971 | log_debug("Table size changed from %" PRIu64 " to %" |
df390f17 | 1972 | PRIu64 " for %s", existing_table_size, |
bb875bb9 | 1973 | seg_start, dnode->name); |
df390f17 AK |
1974 | /* |
1975 | * Kernel usually skips size validation on zero-length devices | |
1976 | * now so no need to preload them. | |
1977 | */ | |
1978 | /* FIXME In which kernel version did this begin? */ | |
1979 | if (!existing_table_size && dnode->props.delay_resume_if_new) | |
1980 | dnode->props.size_changed = 0; | |
1981 | } | |
ec289b64 | 1982 | } |
165e4a11 AK |
1983 | |
1984 | dnode->props.segment_count = 0; | |
1985 | ||
1986 | out: | |
1987 | dm_task_destroy(dmt); | |
1988 | ||
1989 | return r; | |
165e4a11 AK |
1990 | } |
1991 | ||
b4f1578f | 1992 | int dm_tree_preload_children(struct dm_tree_node *dnode, |
bb875bb9 AK |
1993 | const char *uuid_prefix, |
1994 | size_t uuid_prefix_len) | |
165e4a11 | 1995 | { |
2ca6b865 | 1996 | int r = 1; |
165e4a11 | 1997 | void *handle = NULL; |
b4f1578f | 1998 | struct dm_tree_node *child; |
165e4a11 | 1999 | struct dm_info newinfo; |
566515c0 | 2000 | int update_devs_flag = 0; |
165e4a11 AK |
2001 | |
2002 | /* Preload children first */ | |
b4f1578f | 2003 | while ((child = dm_tree_next_child(&handle, dnode, 0))) { |
165e4a11 AK |
2004 | /* Skip existing non-device-mapper devices */ |
2005 | if (!child->info.exists && child->info.major) | |
2006 | continue; | |
2007 | ||
2008 | /* Ignore if it doesn't belong to this VG */ | |
87f98002 AK |
2009 | if (child->info.exists && |
2010 | !_uuid_prefix_matches(child->uuid, uuid_prefix, uuid_prefix_len)) | |
165e4a11 AK |
2011 | continue; |
2012 | ||
b4f1578f | 2013 | if (dm_tree_node_num_children(child, 0)) |
2ca6b865 MS |
2014 | if (!dm_tree_preload_children(child, uuid_prefix, uuid_prefix_len)) |
2015 | return_0; | |
165e4a11 | 2016 | |
165e4a11 AK |
2017 | /* FIXME Cope if name exists with no uuid? */ |
2018 | if (!child->info.exists) { | |
2019 | if (!_create_node(child)) { | |
2020 | stack; | |
2021 | return 0; | |
2022 | } | |
2023 | } | |
2024 | ||
2025 | if (!child->info.inactive_table && child->props.segment_count) { | |
2026 | if (!_load_node(child)) { | |
2027 | stack; | |
2028 | return 0; | |
2029 | } | |
2030 | } | |
2031 | ||
eb91c4ee MB |
2032 | /* Propagate device size change change */ |
2033 | if (child->props.size_changed) | |
2034 | dnode->props.size_changed = 1; | |
2035 | ||
bb875bb9 | 2036 | /* Resume device immediately if it has parents and its size changed */ |
3776c494 | 2037 | if (!dm_tree_node_num_children(child, 1) || !child->props.size_changed) |
165e4a11 AK |
2038 | continue; |
2039 | ||
7707ea90 AK |
2040 | if (!child->info.inactive_table && !child->info.suspended) |
2041 | continue; | |
2042 | ||
fc795d87 | 2043 | if (!_resume_node(child->name, child->info.major, child->info.minor, |
bd90c6b2 | 2044 | child->props.read_ahead, child->props.read_ahead_flags, |
1840aa09 AK |
2045 | &newinfo, &child->dtree->cookie, child->udev_flags, |
2046 | child->info.suspended)) { | |
165e4a11 | 2047 | log_error("Unable to resume %s (%" PRIu32 |
fc795d87 | 2048 | ":%" PRIu32 ")", child->name, child->info.major, |
165e4a11 | 2049 | child->info.minor); |
2ca6b865 | 2050 | r = 0; |
165e4a11 AK |
2051 | continue; |
2052 | } | |
2053 | ||
2054 | /* Update cached info */ | |
2055 | child->info = newinfo; | |
566515c0 PR |
2056 | |
2057 | /* | |
2058 | * Prepare for immediate synchronization with udev and flush all stacked | |
2059 | * dev node operations if requested by immediate_dev_node property. But | |
2060 | * finish processing current level in the tree first. | |
2061 | */ | |
2062 | if (child->props.immediate_dev_node) | |
2063 | update_devs_flag = 1; | |
2064 | ||
165e4a11 AK |
2065 | } |
2066 | ||
2067 | handle = NULL; | |
2068 | ||
566515c0 PR |
2069 | if (update_devs_flag) { |
2070 | if (!dm_udev_wait(dm_tree_get_cookie(dnode))) | |
2071 | stack; | |
2072 | dm_tree_set_cookie(dnode, 0); | |
566515c0 PR |
2073 | } |
2074 | ||
2ca6b865 | 2075 | return r; |
165e4a11 AK |
2076 | } |
2077 | ||
165e4a11 AK |
2078 | /* |
2079 | * Returns 1 if unsure. | |
2080 | */ | |
b4f1578f | 2081 | int dm_tree_children_use_uuid(struct dm_tree_node *dnode, |
165e4a11 AK |
2082 | const char *uuid_prefix, |
2083 | size_t uuid_prefix_len) | |
2084 | { | |
2085 | void *handle = NULL; | |
b4f1578f | 2086 | struct dm_tree_node *child = dnode; |
165e4a11 AK |
2087 | const char *uuid; |
2088 | ||
b4f1578f AK |
2089 | while ((child = dm_tree_next_child(&handle, dnode, 0))) { |
2090 | if (!(uuid = dm_tree_node_get_uuid(child))) { | |
2091 | log_error("Failed to get uuid for dtree node."); | |
165e4a11 AK |
2092 | return 1; |
2093 | } | |
2094 | ||
87f98002 | 2095 | if (_uuid_prefix_matches(uuid, uuid_prefix, uuid_prefix_len)) |
165e4a11 AK |
2096 | return 1; |
2097 | ||
b4f1578f AK |
2098 | if (dm_tree_node_num_children(child, 0)) |
2099 | dm_tree_children_use_uuid(child, uuid_prefix, uuid_prefix_len); | |
165e4a11 AK |
2100 | } |
2101 | ||
2102 | return 0; | |
2103 | } | |
2104 | ||
2105 | /* | |
2106 | * Target functions | |
2107 | */ | |
b4f1578f | 2108 | static struct load_segment *_add_segment(struct dm_tree_node *dnode, unsigned type, uint64_t size) |
165e4a11 AK |
2109 | { |
2110 | struct load_segment *seg; | |
2111 | ||
b4f1578f AK |
2112 | if (!(seg = dm_pool_zalloc(dnode->dtree->mem, sizeof(*seg)))) { |
2113 | log_error("dtree node segment allocation failed"); | |
165e4a11 AK |
2114 | return NULL; |
2115 | } | |
2116 | ||
2117 | seg->type = type; | |
2118 | seg->size = size; | |
2119 | seg->area_count = 0; | |
2c44337b | 2120 | dm_list_init(&seg->areas); |
165e4a11 AK |
2121 | seg->stripe_size = 0; |
2122 | seg->persistent = 0; | |
2123 | seg->chunk_size = 0; | |
2124 | seg->cow = NULL; | |
2125 | seg->origin = NULL; | |
aa6f4e51 | 2126 | seg->merge = NULL; |
165e4a11 | 2127 | |
2c44337b | 2128 | dm_list_add(&dnode->props.segs, &seg->list); |
165e4a11 AK |
2129 | dnode->props.segment_count++; |
2130 | ||
2131 | return seg; | |
2132 | } | |
2133 | ||
b4f1578f | 2134 | int dm_tree_node_add_snapshot_origin_target(struct dm_tree_node *dnode, |
40e5fd8b AK |
2135 | uint64_t size, |
2136 | const char *origin_uuid) | |
165e4a11 AK |
2137 | { |
2138 | struct load_segment *seg; | |
b4f1578f | 2139 | struct dm_tree_node *origin_node; |
165e4a11 | 2140 | |
b4f1578f AK |
2141 | if (!(seg = _add_segment(dnode, SEG_SNAPSHOT_ORIGIN, size))) |
2142 | return_0; | |
165e4a11 | 2143 | |
b4f1578f | 2144 | if (!(origin_node = dm_tree_find_node_by_uuid(dnode->dtree, origin_uuid))) { |
165e4a11 AK |
2145 | log_error("Couldn't find snapshot origin uuid %s.", origin_uuid); |
2146 | return 0; | |
2147 | } | |
2148 | ||
2149 | seg->origin = origin_node; | |
b4f1578f AK |
2150 | if (!_link_tree_nodes(dnode, origin_node)) |
2151 | return_0; | |
165e4a11 | 2152 | |
56c28292 AK |
2153 | /* Resume snapshot origins after new snapshots */ |
2154 | dnode->activation_priority = 1; | |
2155 | ||
165e4a11 AK |
2156 | return 1; |
2157 | } | |
2158 | ||
aa6f4e51 MS |
2159 | static int _add_snapshot_target(struct dm_tree_node *node, |
2160 | uint64_t size, | |
2161 | const char *origin_uuid, | |
2162 | const char *cow_uuid, | |
2163 | const char *merge_uuid, | |
2164 | int persistent, | |
2165 | uint32_t chunk_size) | |
165e4a11 AK |
2166 | { |
2167 | struct load_segment *seg; | |
aa6f4e51 MS |
2168 | struct dm_tree_node *origin_node, *cow_node, *merge_node; |
2169 | unsigned seg_type; | |
2170 | ||
2171 | seg_type = !merge_uuid ? SEG_SNAPSHOT : SEG_SNAPSHOT_MERGE; | |
165e4a11 | 2172 | |
aa6f4e51 | 2173 | if (!(seg = _add_segment(node, seg_type, size))) |
b4f1578f | 2174 | return_0; |
165e4a11 | 2175 | |
b4f1578f | 2176 | if (!(origin_node = dm_tree_find_node_by_uuid(node->dtree, origin_uuid))) { |
165e4a11 AK |
2177 | log_error("Couldn't find snapshot origin uuid %s.", origin_uuid); |
2178 | return 0; | |
2179 | } | |
2180 | ||
2181 | seg->origin = origin_node; | |
b4f1578f AK |
2182 | if (!_link_tree_nodes(node, origin_node)) |
2183 | return_0; | |
165e4a11 | 2184 | |
b4f1578f | 2185 | if (!(cow_node = dm_tree_find_node_by_uuid(node->dtree, cow_uuid))) { |
aa6f4e51 | 2186 | log_error("Couldn't find snapshot COW device uuid %s.", cow_uuid); |
165e4a11 AK |
2187 | return 0; |
2188 | } | |
2189 | ||
2190 | seg->cow = cow_node; | |
b4f1578f AK |
2191 | if (!_link_tree_nodes(node, cow_node)) |
2192 | return_0; | |
165e4a11 AK |
2193 | |
2194 | seg->persistent = persistent ? 1 : 0; | |
2195 | seg->chunk_size = chunk_size; | |
2196 | ||
aa6f4e51 MS |
2197 | if (merge_uuid) { |
2198 | if (!(merge_node = dm_tree_find_node_by_uuid(node->dtree, merge_uuid))) { | |
2199 | /* not a pure error, merging snapshot may have been deactivated */ | |
2200 | log_verbose("Couldn't find merging snapshot uuid %s.", merge_uuid); | |
2201 | } else { | |
2202 | seg->merge = merge_node; | |
2203 | /* must not link merging snapshot, would undermine activation_priority below */ | |
2204 | } | |
2205 | ||
2206 | /* Resume snapshot-merge (acting origin) after other snapshots */ | |
2207 | node->activation_priority = 1; | |
2208 | if (seg->merge) { | |
2209 | /* Resume merging snapshot after snapshot-merge */ | |
2210 | seg->merge->activation_priority = 2; | |
2211 | } | |
2212 | } | |
2213 | ||
165e4a11 AK |
2214 | return 1; |
2215 | } | |
2216 | ||
aa6f4e51 MS |
2217 | |
2218 | int dm_tree_node_add_snapshot_target(struct dm_tree_node *node, | |
2219 | uint64_t size, | |
2220 | const char *origin_uuid, | |
2221 | const char *cow_uuid, | |
2222 | int persistent, | |
2223 | uint32_t chunk_size) | |
2224 | { | |
2225 | return _add_snapshot_target(node, size, origin_uuid, cow_uuid, | |
2226 | NULL, persistent, chunk_size); | |
2227 | } | |
2228 | ||
2229 | int dm_tree_node_add_snapshot_merge_target(struct dm_tree_node *node, | |
2230 | uint64_t size, | |
2231 | const char *origin_uuid, | |
2232 | const char *cow_uuid, | |
2233 | const char *merge_uuid, | |
2234 | uint32_t chunk_size) | |
2235 | { | |
2236 | return _add_snapshot_target(node, size, origin_uuid, cow_uuid, | |
2237 | merge_uuid, 1, chunk_size); | |
2238 | } | |
2239 | ||
b4f1578f | 2240 | int dm_tree_node_add_error_target(struct dm_tree_node *node, |
40e5fd8b | 2241 | uint64_t size) |
165e4a11 | 2242 | { |
b4f1578f AK |
2243 | if (!_add_segment(node, SEG_ERROR, size)) |
2244 | return_0; | |
165e4a11 AK |
2245 | |
2246 | return 1; | |
2247 | } | |
2248 | ||
b4f1578f | 2249 | int dm_tree_node_add_zero_target(struct dm_tree_node *node, |
40e5fd8b | 2250 | uint64_t size) |
165e4a11 | 2251 | { |
b4f1578f AK |
2252 | if (!_add_segment(node, SEG_ZERO, size)) |
2253 | return_0; | |
165e4a11 AK |
2254 | |
2255 | return 1; | |
2256 | } | |
2257 | ||
b4f1578f | 2258 | int dm_tree_node_add_linear_target(struct dm_tree_node *node, |
40e5fd8b | 2259 | uint64_t size) |
165e4a11 | 2260 | { |
b4f1578f AK |
2261 | if (!_add_segment(node, SEG_LINEAR, size)) |
2262 | return_0; | |
165e4a11 AK |
2263 | |
2264 | return 1; | |
2265 | } | |
2266 | ||
b4f1578f | 2267 | int dm_tree_node_add_striped_target(struct dm_tree_node *node, |
40e5fd8b AK |
2268 | uint64_t size, |
2269 | uint32_t stripe_size) | |
165e4a11 AK |
2270 | { |
2271 | struct load_segment *seg; | |
2272 | ||
b4f1578f AK |
2273 | if (!(seg = _add_segment(node, SEG_STRIPED, size))) |
2274 | return_0; | |
165e4a11 AK |
2275 | |
2276 | seg->stripe_size = stripe_size; | |
2277 | ||
2278 | return 1; | |
2279 | } | |
2280 | ||
12ca060e MB |
2281 | int dm_tree_node_add_crypt_target(struct dm_tree_node *node, |
2282 | uint64_t size, | |
2283 | const char *cipher, | |
2284 | const char *chainmode, | |
2285 | const char *iv, | |
2286 | uint64_t iv_offset, | |
2287 | const char *key) | |
2288 | { | |
2289 | struct load_segment *seg; | |
2290 | ||
2291 | if (!(seg = _add_segment(node, SEG_CRYPT, size))) | |
2292 | return_0; | |
2293 | ||
2294 | seg->cipher = cipher; | |
2295 | seg->chainmode = chainmode; | |
2296 | seg->iv = iv; | |
2297 | seg->iv_offset = iv_offset; | |
2298 | seg->key = key; | |
2299 | ||
2300 | return 1; | |
2301 | } | |
2302 | ||
b4f1578f | 2303 | int dm_tree_node_add_mirror_target_log(struct dm_tree_node *node, |
165e4a11 | 2304 | uint32_t region_size, |
08e64ce5 | 2305 | unsigned clustered, |
165e4a11 | 2306 | const char *log_uuid, |
ce7ed2c0 AK |
2307 | unsigned area_count, |
2308 | uint32_t flags) | |
165e4a11 | 2309 | { |
908db078 | 2310 | struct dm_tree_node *log_node = NULL; |
165e4a11 AK |
2311 | struct load_segment *seg; |
2312 | ||
2313 | if (!node->props.segment_count) { | |
b8175c33 | 2314 | log_error(INTERNAL_ERROR "Attempt to add target area to missing segment."); |
165e4a11 AK |
2315 | return 0; |
2316 | } | |
2317 | ||
2c44337b | 2318 | seg = dm_list_item(dm_list_last(&node->props.segs), struct load_segment); |
165e4a11 | 2319 | |
24b026e3 | 2320 | if (log_uuid) { |
67b25ed4 AK |
2321 | if (!(seg->uuid = dm_pool_strdup(node->dtree->mem, log_uuid))) { |
2322 | log_error("log uuid pool_strdup failed"); | |
2323 | return 0; | |
2324 | } | |
df390f17 AK |
2325 | if ((flags & DM_CORELOG)) |
2326 | /* For pvmove: immediate resume (for size validation) isn't needed. */ | |
2327 | node->props.delay_resume_if_new = 1; | |
2328 | else { | |
9723090c AK |
2329 | if (!(log_node = dm_tree_find_node_by_uuid(node->dtree, log_uuid))) { |
2330 | log_error("Couldn't find mirror log uuid %s.", log_uuid); | |
2331 | return 0; | |
2332 | } | |
2333 | ||
566515c0 PR |
2334 | if (clustered) |
2335 | log_node->props.immediate_dev_node = 1; | |
2336 | ||
0a99713e AK |
2337 | /* The kernel validates the size of disk logs. */ |
2338 | /* FIXME Propagate to any devices below */ | |
2339 | log_node->props.delay_resume_if_new = 0; | |
2340 | ||
9723090c AK |
2341 | if (!_link_tree_nodes(node, log_node)) |
2342 | return_0; | |
2343 | } | |
165e4a11 AK |
2344 | } |
2345 | ||
2346 | seg->log = log_node; | |
165e4a11 AK |
2347 | seg->region_size = region_size; |
2348 | seg->clustered = clustered; | |
2349 | seg->mirror_area_count = area_count; | |
dbcb64b8 | 2350 | seg->flags = flags; |
165e4a11 AK |
2351 | |
2352 | return 1; | |
2353 | } | |
2354 | ||
b4f1578f | 2355 | int dm_tree_node_add_mirror_target(struct dm_tree_node *node, |
40e5fd8b | 2356 | uint64_t size) |
165e4a11 | 2357 | { |
cbecd3cd | 2358 | if (!_add_segment(node, SEG_MIRRORED, size)) |
b4f1578f | 2359 | return_0; |
165e4a11 AK |
2360 | |
2361 | return 1; | |
2362 | } | |
2363 | ||
cac52ca4 JEB |
2364 | int dm_tree_node_add_raid_target(struct dm_tree_node *node, |
2365 | uint64_t size, | |
2366 | const char *raid_type, | |
2367 | uint32_t region_size, | |
2368 | uint32_t stripe_size, | |
f439e65b | 2369 | uint64_t rebuilds, |
cac52ca4 JEB |
2370 | uint64_t reserved2) |
2371 | { | |
2372 | int i; | |
2373 | struct load_segment *seg = NULL; | |
2374 | ||
2375 | for (i = 0; dm_segtypes[i].target && !seg; i++) | |
2376 | if (!strcmp(raid_type, dm_segtypes[i].target)) | |
2377 | if (!(seg = _add_segment(node, | |
2378 | dm_segtypes[i].type, size))) | |
2379 | return_0; | |
2380 | ||
b2fa9b43 JEB |
2381 | if (!seg) |
2382 | return_0; | |
2383 | ||
cac52ca4 JEB |
2384 | seg->region_size = region_size; |
2385 | seg->stripe_size = stripe_size; | |
2386 | seg->area_count = 0; | |
f439e65b | 2387 | seg->rebuilds = rebuilds; |
cac52ca4 JEB |
2388 | |
2389 | return 1; | |
2390 | } | |
2391 | ||
b262f3e1 ZK |
2392 | int dm_tree_node_add_replicator_target(struct dm_tree_node *node, |
2393 | uint64_t size, | |
2394 | const char *rlog_uuid, | |
2395 | const char *rlog_type, | |
2396 | unsigned rsite_index, | |
2397 | dm_replicator_mode_t mode, | |
2398 | uint32_t async_timeout, | |
2399 | uint64_t fall_behind_data, | |
2400 | uint32_t fall_behind_ios) | |
2401 | { | |
2402 | struct load_segment *rseg; | |
2403 | struct replicator_site *rsite; | |
2404 | ||
2405 | /* Local site0 - adds replicator segment and links rlog device */ | |
2406 | if (rsite_index == REPLICATOR_LOCAL_SITE) { | |
2407 | if (node->props.segment_count) { | |
2408 | log_error(INTERNAL_ERROR "Attempt to add replicator segment to already used node."); | |
2409 | return 0; | |
2410 | } | |
2411 | ||
2412 | if (!(rseg = _add_segment(node, SEG_REPLICATOR, size))) | |
2413 | return_0; | |
2414 | ||
2415 | if (!(rseg->log = dm_tree_find_node_by_uuid(node->dtree, rlog_uuid))) { | |
2416 | log_error("Missing replicator log uuid %s.", rlog_uuid); | |
2417 | return 0; | |
2418 | } | |
2419 | ||
2420 | if (!_link_tree_nodes(node, rseg->log)) | |
2421 | return_0; | |
2422 | ||
2423 | if (strcmp(rlog_type, "ringbuffer") != 0) { | |
2424 | log_error("Unsupported replicator log type %s.", rlog_type); | |
2425 | return 0; | |
2426 | } | |
2427 | ||
2428 | if (!(rseg->rlog_type = dm_pool_strdup(node->dtree->mem, rlog_type))) | |
2429 | return_0; | |
2430 | ||
2431 | dm_list_init(&rseg->rsites); | |
2432 | rseg->rdevice_count = 0; | |
2433 | node->activation_priority = 1; | |
2434 | } | |
2435 | ||
2436 | /* Add site to segment */ | |
2437 | if (mode == DM_REPLICATOR_SYNC | |
2438 | && (async_timeout || fall_behind_ios || fall_behind_data)) { | |
2439 | log_error("Async parameters passed for synchronnous replicator."); | |
2440 | return 0; | |
2441 | } | |
2442 | ||
2443 | if (node->props.segment_count != 1) { | |
2444 | log_error(INTERNAL_ERROR "Attempt to add remote site area before setting replicator log."); | |
2445 | return 0; | |
2446 | } | |
2447 | ||
2448 | rseg = dm_list_item(dm_list_last(&node->props.segs), struct load_segment); | |
2449 | if (rseg->type != SEG_REPLICATOR) { | |
2450 | log_error(INTERNAL_ERROR "Attempt to use non replicator segment %s.", | |
2451 | dm_segtypes[rseg->type].target); | |
2452 | return 0; | |
2453 | } | |
2454 | ||
2455 | if (!(rsite = dm_pool_zalloc(node->dtree->mem, sizeof(*rsite)))) { | |
2456 | log_error("Failed to allocate remote site segment."); | |
2457 | return 0; | |
2458 | } | |
2459 | ||
2460 | dm_list_add(&rseg->rsites, &rsite->list); | |
2461 | rseg->rsite_count++; | |
2462 | ||
2463 | rsite->mode = mode; | |
2464 | rsite->async_timeout = async_timeout; | |
2465 | rsite->fall_behind_data = fall_behind_data; | |
2466 | rsite->fall_behind_ios = fall_behind_ios; | |
2467 | rsite->rsite_index = rsite_index; | |
2468 | ||
2469 | return 1; | |
2470 | } | |
2471 | ||
2472 | /* Appends device node to Replicator */ | |
2473 | int dm_tree_node_add_replicator_dev_target(struct dm_tree_node *node, | |
2474 | uint64_t size, | |
2475 | const char *replicator_uuid, | |
2476 | uint64_t rdevice_index, | |
2477 | const char *rdev_uuid, | |
2478 | unsigned rsite_index, | |
2479 | const char *slog_uuid, | |
2480 | uint32_t slog_flags, | |
2481 | uint32_t slog_region_size) | |
2482 | { | |
2483 | struct seg_area *area; | |
2484 | struct load_segment *rseg; | |
2485 | struct load_segment *rep_seg; | |
2486 | ||
2487 | if (rsite_index == REPLICATOR_LOCAL_SITE) { | |
2488 | /* Site index for local target */ | |
2489 | if (!(rseg = _add_segment(node, SEG_REPLICATOR_DEV, size))) | |
2490 | return_0; | |
2491 | ||
2492 | if (!(rseg->replicator = dm_tree_find_node_by_uuid(node->dtree, replicator_uuid))) { | |
2493 | log_error("Missing replicator uuid %s.", replicator_uuid); | |
2494 | return 0; | |
2495 | } | |
2496 | ||
2497 | /* Local slink0 for replicator must be always initialized first */ | |
2498 | if (rseg->replicator->props.segment_count != 1) { | |
2499 | log_error(INTERNAL_ERROR "Attempt to use non replicator segment."); | |
2500 | return 0; | |
2501 | } | |
2502 | ||
2503 | rep_seg = dm_list_item(dm_list_last(&rseg->replicator->props.segs), struct load_segment); | |
2504 | if (rep_seg->type != SEG_REPLICATOR) { | |
2505 | log_error(INTERNAL_ERROR "Attempt to use non replicator segment %s.", | |
2506 | dm_segtypes[rep_seg->type].target); | |
2507 | return 0; | |
2508 | } | |
2509 | rep_seg->rdevice_count++; | |
2510 | ||
2511 | if (!_link_tree_nodes(node, rseg->replicator)) | |
2512 | return_0; | |
2513 | ||
2514 | rseg->rdevice_index = rdevice_index; | |
2515 | } else { | |
2516 | /* Local slink0 for replicator must be always initialized first */ | |
2517 | if (node->props.segment_count != 1) { | |
2518 | log_error(INTERNAL_ERROR "Attempt to use non replicator-dev segment."); | |
2519 | return 0; | |
2520 | } | |
2521 | ||
2522 | rseg = dm_list_item(dm_list_last(&node->props.segs), struct load_segment); | |
2523 | if (rseg->type != SEG_REPLICATOR_DEV) { | |
2524 | log_error(INTERNAL_ERROR "Attempt to use non replicator-dev segment %s.", | |
2525 | dm_segtypes[rseg->type].target); | |
2526 | return 0; | |
2527 | } | |
2528 | } | |
2529 | ||
2530 | if (!(slog_flags & DM_CORELOG) && !slog_uuid) { | |
2531 | log_error("Unspecified sync log uuid."); | |
2532 | return 0; | |
2533 | } | |
2534 | ||
2535 | if (!dm_tree_node_add_target_area(node, NULL, rdev_uuid, 0)) | |
2536 | return_0; | |
2537 | ||
2538 | area = dm_list_item(dm_list_last(&rseg->areas), struct seg_area); | |
2539 | ||
2540 | if (!(slog_flags & DM_CORELOG)) { | |
2541 | if (!(area->slog = dm_tree_find_node_by_uuid(node->dtree, slog_uuid))) { | |
2542 | log_error("Couldn't find sync log uuid %s.", slog_uuid); | |
2543 | return 0; | |
2544 | } | |
2545 | ||
2546 | if (!_link_tree_nodes(node, area->slog)) | |
2547 | return_0; | |
2548 | } | |
2549 | ||
2550 | area->flags = slog_flags; | |
2551 | area->region_size = slog_region_size; | |
2552 | area->rsite_index = rsite_index; | |
2553 | ||
2554 | return 1; | |
2555 | } | |
2556 | ||
b4f1578f | 2557 | static int _add_area(struct dm_tree_node *node, struct load_segment *seg, struct dm_tree_node *dev_node, uint64_t offset) |
165e4a11 AK |
2558 | { |
2559 | struct seg_area *area; | |
2560 | ||
b4f1578f | 2561 | if (!(area = dm_pool_zalloc(node->dtree->mem, sizeof (*area)))) { |
165e4a11 AK |
2562 | log_error("Failed to allocate target segment area."); |
2563 | return 0; | |
2564 | } | |
2565 | ||
2566 | area->dev_node = dev_node; | |
2567 | area->offset = offset; | |
2568 | ||
2c44337b | 2569 | dm_list_add(&seg->areas, &area->list); |
165e4a11 AK |
2570 | seg->area_count++; |
2571 | ||
2572 | return 1; | |
2573 | } | |
2574 | ||
b4f1578f | 2575 | int dm_tree_node_add_target_area(struct dm_tree_node *node, |
40e5fd8b AK |
2576 | const char *dev_name, |
2577 | const char *uuid, | |
2578 | uint64_t offset) | |
165e4a11 AK |
2579 | { |
2580 | struct load_segment *seg; | |
2581 | struct stat info; | |
b4f1578f | 2582 | struct dm_tree_node *dev_node; |
165e4a11 AK |
2583 | |
2584 | if ((!dev_name || !*dev_name) && (!uuid || !*uuid)) { | |
b4f1578f | 2585 | log_error("dm_tree_node_add_target_area called without device"); |
165e4a11 AK |
2586 | return 0; |
2587 | } | |
2588 | ||
2589 | if (uuid) { | |
b4f1578f | 2590 | if (!(dev_node = dm_tree_find_node_by_uuid(node->dtree, uuid))) { |
165e4a11 AK |
2591 | log_error("Couldn't find area uuid %s.", uuid); |
2592 | return 0; | |
2593 | } | |
b4f1578f AK |
2594 | if (!_link_tree_nodes(node, dev_node)) |
2595 | return_0; | |
165e4a11 | 2596 | } else { |
6d04311e | 2597 | if (stat(dev_name, &info) < 0) { |
165e4a11 AK |
2598 | log_error("Device %s not found.", dev_name); |
2599 | return 0; | |
2600 | } | |
2601 | ||
40e5fd8b | 2602 | if (!S_ISBLK(info.st_mode)) { |
165e4a11 AK |
2603 | log_error("Device %s is not a block device.", dev_name); |
2604 | return 0; | |
2605 | } | |
2606 | ||
2607 | /* FIXME Check correct macro use */ | |
cda69e17 PR |
2608 | if (!(dev_node = _add_dev(node->dtree, node, MAJOR(info.st_rdev), |
2609 | MINOR(info.st_rdev), 0))) | |
b4f1578f | 2610 | return_0; |
165e4a11 AK |
2611 | } |
2612 | ||
2613 | if (!node->props.segment_count) { | |
b8175c33 | 2614 | log_error(INTERNAL_ERROR "Attempt to add target area to missing segment."); |
165e4a11 AK |
2615 | return 0; |
2616 | } | |
2617 | ||
2c44337b | 2618 | seg = dm_list_item(dm_list_last(&node->props.segs), struct load_segment); |
165e4a11 | 2619 | |
b4f1578f AK |
2620 | if (!_add_area(node, seg, dev_node, offset)) |
2621 | return_0; | |
165e4a11 AK |
2622 | |
2623 | return 1; | |
db208f51 | 2624 | } |
bd90c6b2 | 2625 | |
6d04311e JEB |
2626 | int dm_tree_node_add_null_area(struct dm_tree_node *node, uint64_t offset) |
2627 | { | |
2628 | struct load_segment *seg; | |
2629 | ||
2630 | seg = dm_list_item(dm_list_last(&node->props.segs), struct load_segment); | |
2631 | ||
415c0690 AK |
2632 | switch (seg->type) { |
2633 | case SEG_RAID1: | |
2634 | case SEG_RAID4: | |
2635 | case SEG_RAID5_LA: | |
2636 | case SEG_RAID5_RA: | |
2637 | case SEG_RAID5_LS: | |
2638 | case SEG_RAID5_RS: | |
2639 | case SEG_RAID6_ZR: | |
2640 | case SEG_RAID6_NR: | |
2641 | case SEG_RAID6_NC: | |
2642 | break; | |
2643 | default: | |
2644 | log_error("dm_tree_node_add_null_area() called on an unsupported segment type"); | |
2645 | return 0; | |
2646 | } | |
2647 | ||
6d04311e JEB |
2648 | if (!_add_area(node, seg, NULL, offset)) |
2649 | return_0; | |
2650 | ||
2651 | return 1; | |
2652 | } | |
2653 | ||
bd90c6b2 AK |
2654 | void dm_tree_set_cookie(struct dm_tree_node *node, uint32_t cookie) |
2655 | { | |
2656 | node->dtree->cookie = cookie; | |
2657 | } | |
2658 | ||
2659 | uint32_t dm_tree_get_cookie(struct dm_tree_node *node) | |
2660 | { | |
2661 | return node->dtree->cookie; | |
2662 | } |