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