2 * Copyright (C) 2003-2004 Sistina Software, Inc. All rights reserved.
3 * Copyright (C) 2004-2007 Red Hat, Inc. All rights reserved.
5 * This file is part of LVM2.
7 * This copyrighted material is made available to anyone wishing to use,
8 * modify, copy, or redistribute it subject to the terms and conditions
9 * of the GNU Lesser General Public License v.2.1.
11 * You should have received a copy of the GNU Lesser General Public License
12 * along with this program; if not, write to the Free Software Foundation,
13 * Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17 #include "toolcontext.h"
21 #include "text_export.h"
22 #include "text_import.h"
25 #include "lvm-string.h"
28 #include "sharedlib.h"
31 #include <sys/utsname.h>
33 static int _block_on_error_available = 0;
34 static unsigned _mirror_attributes = 0;
43 uint32_t default_region_size;
46 static const char *_mirrored_name(const struct lv_segment *seg)
48 return seg->segtype->name;
51 static void _mirrored_display(const struct lv_segment *seg)
56 log_print(" Mirrors\t\t%u", seg->area_count);
57 log_print(" Mirror size\t\t%u", seg->area_len);
59 log_print(" Mirror log volume\t%s", seg->log_lv->name);
61 if (seg->region_size) {
62 size = display_size(seg->lv->vg->cmd,
63 (uint64_t) seg->region_size);
64 log_print(" Mirror region size\t%s", size);
67 log_print(" Mirror original:");
68 display_stripe(seg, 0, " ");
69 log_print(" Mirror destinations:");
70 for (s = 1; s < seg->area_count; s++)
71 display_stripe(seg, s, " ");
75 static int _mirrored_text_import_area_count(const struct config_node *sn, uint32_t *area_count)
77 if (!get_config_uint32(sn, "mirror_count", area_count)) {
78 log_error("Couldn't read 'mirror_count' for "
79 "segment '%s'.", config_parent_name(sn));
86 static int _mirrored_text_import(struct lv_segment *seg, const struct config_node *sn,
87 struct dm_hash_table *pv_hash)
89 const struct config_node *cn;
90 const char *logname = NULL;
92 if (find_config_node(sn, "extents_moved")) {
93 if (get_config_uint32(sn, "extents_moved",
94 &seg->extents_copied))
95 seg->status |= PVMOVE;
97 log_error("Couldn't read 'extents_moved' for "
98 "segment %s of logical volume %s.",
99 config_parent_name(sn), seg->lv->name);
104 if (find_config_node(sn, "region_size")) {
105 if (!get_config_uint32(sn, "region_size",
106 &seg->region_size)) {
107 log_error("Couldn't read 'region_size' for "
108 "segment %s of logical volume %s.",
109 config_parent_name(sn), seg->lv->name);
114 if ((cn = find_config_node(sn, "mirror_log"))) {
115 if (!cn->v || !cn->v->v.str) {
116 log_error("Mirror log type must be a string.");
119 logname = cn->v->v.str;
120 if (!(seg->log_lv = find_lv(seg->lv->vg, logname))) {
121 log_error("Unrecognised mirror log in "
122 "segment %s of logical volume %s.",
123 config_parent_name(sn), seg->lv->name);
126 seg->log_lv->status |= MIRROR_LOG;
129 if (logname && !seg->region_size) {
130 log_error("Missing region size for mirror log for "
131 "segment %s of logical volume %s.",
132 config_parent_name(sn), seg->lv->name);
136 if (!(cn = find_config_node(sn, "mirrors"))) {
137 log_error("Couldn't find mirrors array for "
138 "segment %s of logical volume %s.",
139 config_parent_name(sn), seg->lv->name);
143 return text_import_areas(seg, sn, cn, pv_hash, MIRROR_IMAGE);
146 static int _mirrored_text_export(const struct lv_segment *seg, struct formatter *f)
148 outf(f, "mirror_count = %u", seg->area_count);
149 if (seg->status & PVMOVE)
150 outsize(f, (uint64_t) seg->extents_copied * seg->lv->vg->extent_size,
151 "extents_moved = %" PRIu32, seg->extents_copied);
153 outf(f, "mirror_log = \"%s\"", seg->log_lv->name);
154 if (seg->region_size)
155 outf(f, "region_size = %" PRIu32, seg->region_size);
157 return out_areas(f, seg, "mirror");
160 #ifdef DEVMAPPER_SUPPORT
161 static struct mirror_state *_mirrored_init_target(struct dm_pool *mem,
162 struct cmd_context *cmd)
164 struct mirror_state *mirr_state;
166 if (!(mirr_state = dm_pool_alloc(mem, sizeof(*mirr_state)))) {
167 log_error("struct mirr_state allocation failed");
171 mirr_state->default_region_size = 2 *
172 find_config_tree_int(cmd,
173 "activation/mirror_region_size",
174 DEFAULT_MIRROR_REGION_SIZE);
179 static int _mirrored_target_percent(void **target_state,
182 struct cmd_context *cmd,
183 struct lv_segment *seg, char *params,
184 uint64_t *total_numerator,
185 uint64_t *total_denominator)
187 struct mirror_state *mirr_state;
188 uint64_t numerator, denominator;
189 unsigned mirror_count, m;
194 *target_state = _mirrored_init_target(mem, cmd);
196 mirr_state = *target_state;
198 /* Status line: <#mirrors> (maj:min)+ <synced>/<total_regions> */
199 log_debug("Mirror status: %s", params);
201 if (sscanf(pos, "%u %n", &mirror_count, &used) != 1) {
202 log_error("Failure parsing mirror status mirror count: %s",
208 for (m = 0; m < mirror_count; m++) {
209 if (sscanf(pos, "%*x:%*x %n", &used) != 0) {
210 log_error("Failure parsing mirror status devices: %s",
217 if (sscanf(pos, "%" PRIu64 "/%" PRIu64 "%n", &numerator, &denominator,
219 log_error("Failure parsing mirror status fraction: %s", params);
224 *total_numerator += numerator;
225 *total_denominator += denominator;
228 seg->extents_copied = seg->area_len * numerator / denominator;
230 *percent = make_percent(numerator, denominator);
235 static int _mirrored_transient_status(struct lv_segment *seg, char *params)
238 struct logical_volume *lv = seg->lv;
241 char **args, **log_args;
242 struct logical_volume **images;
243 struct logical_volume *log;
244 int num_devs, log_argc;
248 log_very_verbose("Mirrored transient status: \"%s\"", params);
250 /* number of devices */
251 if (!dm_split_words(params, 1, 0, &p))
254 if (!(num_devs = atoi(p)))
259 if (num_devs > DEFAULT_MIRROR_MAX_IMAGES) {
260 log_error("Unexpectedly many (%d) mirror images in %s.",
265 args = alloca((num_devs + 5) * sizeof(char *));
266 images = alloca(num_devs * sizeof(struct logical_volume *));
268 if (dm_split_words(p, num_devs + 4, 0, args) < num_devs + 4)
271 log_argc = atoi(args[3 + num_devs]);
272 log_args = alloca(log_argc * sizeof(char *));
275 log_error("Unexpectedly many (%d) log arguments in %s.",
281 if (dm_split_words(args[3 + num_devs] + strlen(args[3 + num_devs]) + 1,
282 log_argc, 0, log_args) < log_argc)
285 if (num_devs != seg->area_count) {
286 log_error("Active mirror has a wrong number of mirror images!");
287 log_error("Metadata says %d, kernel says %d.", seg->area_count, num_devs);
291 if (!strcmp(log_args[0], "disk")) {
293 log = first_seg(lv)->log_lv;
294 if (!lv_info(lv->vg->cmd, log, 0, &info, 0, 0)) {
295 log_error("Check for existence of mirror log %s failed.",
299 log_debug("Found mirror log at %d:%d", info.major, info.minor);
300 sprintf(buf, "%d:%d", info.major, info.minor);
301 if (strcmp(buf, log_args[1])) {
302 log_error("Mirror log mismatch. Metadata says %s, kernel says %s.",
306 log_very_verbose("Status of log (%s): %s", buf, log_args[2]);
307 if (log_args[2][0] != 'A') {
308 log->status |= PARTIAL_LV;
313 for (i = 0; i < num_devs; ++i)
316 for (i = 0; i < seg->area_count; ++i) {
318 if (!lv_info(lv->vg->cmd, seg_lv(seg, i), 0, &info, 0, 0)) {
319 log_error("Check for existence of mirror image %s failed.",
320 seg_lv(seg, i)->name);
323 log_debug("Found mirror image at %d:%d", info.major, info.minor);
324 sprintf(buf, "%d:%d", info.major, info.minor);
325 for (j = 0; j < num_devs; ++j) {
326 if (!strcmp(buf, args[j])) {
327 log_debug("Match: metadata image %d matches kernel image %d", i, j);
328 images[j] = seg_lv(seg, i);
333 status = args[2 + num_devs];
335 for (i = 0; i < num_devs; ++i) {
337 log_error("Failed to find image %d (%s).", i, args[i]);
340 log_very_verbose("Status of image %d: %c", i, status[i]);
341 if (status[i] != 'A') {
342 images[i]->status |= PARTIAL_LV;
347 /* update PARTIAL_LV flags across the VG */
349 vg_mark_partial_lvs(lv->vg);
354 static int _add_log(struct dm_pool *mem, struct lv_segment *seg,
355 struct dm_tree_node *node, uint32_t area_count, uint32_t region_size)
357 unsigned clustered = 0;
358 char *log_dlid = NULL;
359 uint32_t log_flags = 0;
362 * Use clustered mirror log for non-exclusive activation
365 if ((!(seg->lv->status & ACTIVATE_EXCL) &&
366 (vg_is_clustered(seg->lv->vg))))
370 /* If disk log, use its UUID */
371 if (!(log_dlid = build_dm_uuid(mem, seg->log_lv->lvid.s, NULL))) {
372 log_error("Failed to build uuid for log LV %s.",
377 /* If core log, use mirror's UUID and set DM_CORELOG flag */
378 if (!(log_dlid = build_dm_uuid(mem, seg->lv->lvid.s, NULL))) {
379 log_error("Failed to build uuid for mirror LV %s.",
383 log_flags |= DM_CORELOG;
386 if (mirror_in_sync() && !(seg->status & PVMOVE))
387 log_flags |= DM_NOSYNC;
389 if (_block_on_error_available && !(seg->status & PVMOVE))
390 log_flags |= DM_BLOCK_ON_ERROR;
392 return dm_tree_node_add_mirror_target_log(node, region_size, clustered, log_dlid, area_count, log_flags);
395 static int _mirrored_add_target_line(struct dev_manager *dm, struct dm_pool *mem,
396 struct cmd_context *cmd, void **target_state,
397 struct lv_segment *seg,
398 struct dm_tree_node *node, uint64_t len,
399 uint32_t *pvmove_mirror_count)
401 struct mirror_state *mirr_state;
402 uint32_t area_count = seg->area_count;
403 unsigned start_area = 0u;
404 int mirror_status = MIRR_RUNNING;
405 uint32_t region_size;
409 *target_state = _mirrored_init_target(mem, cmd);
411 mirr_state = *target_state;
414 * Mirror segment could have only 1 area temporarily
415 * if the segment is under conversion.
417 if (seg->area_count == 1)
418 mirror_status = MIRR_DISABLED;
421 * For pvmove, only have one mirror segment RUNNING at once.
422 * Segments before this are COMPLETED and use 2nd area.
423 * Segments after this are DISABLED and use 1st area.
425 if (seg->status & PVMOVE) {
426 if (seg->extents_copied == seg->area_len) {
427 mirror_status = MIRR_COMPLETED;
429 } else if ((*pvmove_mirror_count)++) {
430 mirror_status = MIRR_DISABLED;
433 /* else MIRR_RUNNING */
436 if (mirror_status != MIRR_RUNNING) {
437 if (!dm_tree_node_add_linear_target(node, len))
442 if (!(seg->status & PVMOVE)) {
443 if (!seg->region_size) {
444 log_error("Missing region size for mirror segment.");
447 region_size = seg->region_size;
450 region_size = adjusted_mirror_region_size(seg->lv->vg->extent_size,
452 mirr_state->default_region_size);
454 if (!dm_tree_node_add_mirror_target(node, len))
457 if ((r = _add_log(mem, seg, node, area_count, region_size)) <= 0) {
463 return add_areas_line(dm, seg, node, start_area, area_count);
466 static int _mirrored_target_present(struct cmd_context *cmd,
467 const struct lv_segment *seg,
468 unsigned *attributes)
470 static int _mirrored_checked = 0;
471 static int _mirrored_present = 0;
472 uint32_t maj, min, patchlevel;
473 unsigned maj2, min2, patchlevel2;
476 unsigned kmaj, kmin, krel;
478 if (!_mirrored_checked) {
479 _mirrored_present = target_present(cmd, "mirror", 1);
482 * block_on_error available as "block_on_error" log
483 * argument with mirror target >= 1.1 and <= 1.11
484 * or with 1.0 in RHEL4U3 driver >= 4.5
486 * block_on_error available as "handle_errors" mirror
487 * argument with mirror target >= 1.12.
489 * libdm-deptree.c is smart enough to handle the differences
490 * between block_on_error and handle_errors for all
491 * mirror target versions >= 1.1
493 /* FIXME Move this into libdevmapper */
495 if (target_version("mirror", &maj, &min, &patchlevel) &&
498 (min == 0 && driver_version(vsn, sizeof(vsn)) &&
499 sscanf(vsn, "%u.%u.%u", &maj2, &min2, &patchlevel2) == 3 &&
500 maj2 == 4 && min2 == 5 && patchlevel2 == 0))) /* RHEL4U3 */
501 _block_on_error_available = 1;
505 * Check only for modules if atttributes requested and no previous check.
506 * FIXME: Fails incorrectly if cmirror was built into kernel.
509 if (!_mirror_attributes) {
511 * The dm-log-userspace module was added to the
515 (sscanf(uts.release, "%u.%u.%u", &kmaj, &kmin, &krel) == 3) &&
516 KERNEL_VERSION(kmaj, kmin, krel) < KERNEL_VERSION(2, 6, 31)) {
517 if (module_present(cmd, "log-clustered"))
518 _mirror_attributes |= MIRROR_LOG_CLUSTERED;
519 } else if (module_present(cmd, "log-userspace"))
520 _mirror_attributes |= MIRROR_LOG_CLUSTERED;
522 if (!(_mirror_attributes & MIRROR_LOG_CLUSTERED))
523 log_verbose("Cluster mirror log module is not available");
526 * The cluster mirror log daemon must be running,
527 * otherwise, the kernel module will fail to make
530 #ifdef CMIRRORD_PIDFILE
531 if (!dm_daemon_is_running(CMIRRORD_PIDFILE)) {
532 log_verbose("Cluster mirror log daemon is not running");
533 _mirror_attributes &= ~MIRROR_LOG_CLUSTERED;
536 log_verbose("Cluster mirror log daemon not included in build");
537 _mirror_attributes &= ~MIRROR_LOG_CLUSTERED;
540 *attributes = _mirror_attributes;
542 _mirrored_checked = 1;
544 return _mirrored_present;
548 static const char *_get_mirror_dso_path(struct cmd_context *cmd)
550 return get_monitor_dso_path(cmd, find_config_tree_str(cmd, "dmeventd/mirror_library",
551 DEFAULT_DMEVENTD_MIRROR_LIB));
554 /* FIXME Cache this */
555 static int _target_registered(struct lv_segment *seg, int *pending)
557 return target_registered_with_dmeventd(seg->lv->vg->cmd, _get_mirror_dso_path(seg->lv->vg->cmd),
561 /* FIXME This gets run while suspended and performs banned operations. */
562 static int _target_set_events(struct lv_segment *seg, int evmask, int set)
564 return target_register_events(seg->lv->vg->cmd, _get_mirror_dso_path(seg->lv->vg->cmd),
565 seg->lv, evmask, set, 0);
568 static int _target_monitor_events(struct lv_segment *seg, int events)
570 return _target_set_events(seg, events, 1);
573 static int _target_unmonitor_events(struct lv_segment *seg, int events)
575 return _target_set_events(seg, events, 0);
578 #endif /* DMEVENTD */
579 #endif /* DEVMAPPER_SUPPORT */
581 static int _mirrored_modules_needed(struct dm_pool *mem,
582 const struct lv_segment *seg,
583 struct dm_list *modules)
586 !list_segment_modules(mem, first_seg(seg->log_lv), modules))
589 if (vg_is_clustered(seg->lv->vg) &&
590 !str_list_add(mem, modules, "clog")) {
591 log_error("cluster log string list allocation failed");
595 if (!str_list_add(mem, modules, "mirror")) {
596 log_error("mirror string list allocation failed");
603 static void _mirrored_destroy(struct segment_type *segtype)
608 static struct segtype_handler _mirrored_ops = {
609 .name = _mirrored_name,
610 .display = _mirrored_display,
611 .text_import_area_count = _mirrored_text_import_area_count,
612 .text_import = _mirrored_text_import,
613 .text_export = _mirrored_text_export,
614 #ifdef DEVMAPPER_SUPPORT
615 .add_target_line = _mirrored_add_target_line,
616 .target_percent = _mirrored_target_percent,
617 .target_present = _mirrored_target_present,
618 .check_transient_status = _mirrored_transient_status,
620 .target_monitored = _target_registered,
621 .target_monitor_events = _target_monitor_events,
622 .target_unmonitor_events = _target_unmonitor_events,
625 .modules_needed = _mirrored_modules_needed,
626 .destroy = _mirrored_destroy,
629 #ifdef MIRRORED_INTERNAL
630 struct segment_type *init_mirrored_segtype(struct cmd_context *cmd)
632 struct segment_type *init_segtype(struct cmd_context *cmd);
633 struct segment_type *init_segtype(struct cmd_context *cmd)
636 struct segment_type *segtype = dm_malloc(sizeof(*segtype));
642 segtype->ops = &_mirrored_ops;
643 segtype->name = "mirror";
644 segtype->private = NULL;
645 segtype->flags = SEG_AREAS_MIRRORED;
648 if (_get_mirror_dso_path(cmd))
649 segtype->flags |= SEG_MONITORED;
652 log_very_verbose("Initialised segtype: %s", segtype->name);