1*7c604eeaShaad /* $NetBSD: mirrored.c,v 1.1.1.2 2009/12/02 00:26:26 haad Exp $ */
256a34939Shaad
356a34939Shaad /*
456a34939Shaad * Copyright (C) 2003-2004 Sistina Software, Inc. All rights reserved.
556a34939Shaad * Copyright (C) 2004-2007 Red Hat, Inc. All rights reserved.
656a34939Shaad *
756a34939Shaad * This file is part of LVM2.
856a34939Shaad *
956a34939Shaad * This copyrighted material is made available to anyone wishing to use,
1056a34939Shaad * modify, copy, or redistribute it subject to the terms and conditions
1156a34939Shaad * of the GNU Lesser General Public License v.2.1.
1256a34939Shaad *
1356a34939Shaad * You should have received a copy of the GNU Lesser General Public License
1456a34939Shaad * along with this program; if not, write to the Free Software Foundation,
1556a34939Shaad * Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
1656a34939Shaad */
1756a34939Shaad
1856a34939Shaad #include "lib.h"
1956a34939Shaad #include "toolcontext.h"
2056a34939Shaad #include "metadata.h"
2156a34939Shaad #include "segtype.h"
2256a34939Shaad #include "display.h"
2356a34939Shaad #include "text_export.h"
2456a34939Shaad #include "text_import.h"
2556a34939Shaad #include "config.h"
2656a34939Shaad #include "defaults.h"
2756a34939Shaad #include "lvm-string.h"
2856a34939Shaad #include "targets.h"
2956a34939Shaad #include "activate.h"
3056a34939Shaad #include "sharedlib.h"
3156a34939Shaad #include "str_list.h"
3256a34939Shaad
3356a34939Shaad #ifdef DMEVENTD
3456a34939Shaad # include "libdevmapper-event.h"
3556a34939Shaad #endif
3656a34939Shaad
3756a34939Shaad static int _block_on_error_available = 0;
3856a34939Shaad static unsigned _mirror_attributes = 0;
3956a34939Shaad
4056a34939Shaad enum {
4156a34939Shaad MIRR_DISABLED,
4256a34939Shaad MIRR_RUNNING,
4356a34939Shaad MIRR_COMPLETED
4456a34939Shaad };
4556a34939Shaad
4656a34939Shaad struct mirror_state {
4756a34939Shaad uint32_t default_region_size;
4856a34939Shaad };
4956a34939Shaad
_mirrored_name(const struct lv_segment * seg)5056a34939Shaad static const char *_mirrored_name(const struct lv_segment *seg)
5156a34939Shaad {
5256a34939Shaad return seg->segtype->name;
5356a34939Shaad }
5456a34939Shaad
_mirrored_display(const struct lv_segment * seg)5556a34939Shaad static void _mirrored_display(const struct lv_segment *seg)
5656a34939Shaad {
5756a34939Shaad const char *size;
5856a34939Shaad uint32_t s;
5956a34939Shaad
6056a34939Shaad log_print(" Mirrors\t\t%u", seg->area_count);
6156a34939Shaad log_print(" Mirror size\t\t%u", seg->area_len);
6256a34939Shaad if (seg->log_lv)
6356a34939Shaad log_print(" Mirror log volume\t%s", seg->log_lv->name);
6456a34939Shaad
6556a34939Shaad if (seg->region_size) {
6656a34939Shaad size = display_size(seg->lv->vg->cmd,
6756a34939Shaad (uint64_t) seg->region_size);
6856a34939Shaad log_print(" Mirror region size\t%s", size);
6956a34939Shaad }
7056a34939Shaad
7156a34939Shaad log_print(" Mirror original:");
7256a34939Shaad display_stripe(seg, 0, " ");
7356a34939Shaad log_print(" Mirror destinations:");
7456a34939Shaad for (s = 1; s < seg->area_count; s++)
7556a34939Shaad display_stripe(seg, s, " ");
7656a34939Shaad log_print(" ");
7756a34939Shaad }
7856a34939Shaad
_mirrored_text_import_area_count(struct config_node * sn,uint32_t * area_count)7956a34939Shaad static int _mirrored_text_import_area_count(struct config_node *sn, uint32_t *area_count)
8056a34939Shaad {
8156a34939Shaad if (!get_config_uint32(sn, "mirror_count", area_count)) {
8256a34939Shaad log_error("Couldn't read 'mirror_count' for "
83*7c604eeaShaad "segment '%s'.", config_parent_name(sn));
8456a34939Shaad return 0;
8556a34939Shaad }
8656a34939Shaad
8756a34939Shaad return 1;
8856a34939Shaad }
8956a34939Shaad
_mirrored_text_import(struct lv_segment * seg,const struct config_node * sn,struct dm_hash_table * pv_hash)9056a34939Shaad static int _mirrored_text_import(struct lv_segment *seg, const struct config_node *sn,
9156a34939Shaad struct dm_hash_table *pv_hash)
9256a34939Shaad {
9356a34939Shaad const struct config_node *cn;
9456a34939Shaad char *logname = NULL;
9556a34939Shaad
9656a34939Shaad if (find_config_node(sn, "extents_moved")) {
9756a34939Shaad if (get_config_uint32(sn, "extents_moved",
9856a34939Shaad &seg->extents_copied))
9956a34939Shaad seg->status |= PVMOVE;
10056a34939Shaad else {
10156a34939Shaad log_error("Couldn't read 'extents_moved' for "
102*7c604eeaShaad "segment %s of logical volume %s.",
103*7c604eeaShaad config_parent_name(sn), seg->lv->name);
10456a34939Shaad return 0;
10556a34939Shaad }
10656a34939Shaad }
10756a34939Shaad
10856a34939Shaad if (find_config_node(sn, "region_size")) {
10956a34939Shaad if (!get_config_uint32(sn, "region_size",
11056a34939Shaad &seg->region_size)) {
11156a34939Shaad log_error("Couldn't read 'region_size' for "
112*7c604eeaShaad "segment %s of logical volume %s.",
113*7c604eeaShaad config_parent_name(sn), seg->lv->name);
11456a34939Shaad return 0;
11556a34939Shaad }
11656a34939Shaad }
11756a34939Shaad
11856a34939Shaad if ((cn = find_config_node(sn, "mirror_log"))) {
11956a34939Shaad if (!cn->v || !cn->v->v.str) {
12056a34939Shaad log_error("Mirror log type must be a string.");
12156a34939Shaad return 0;
12256a34939Shaad }
12356a34939Shaad logname = cn->v->v.str;
12456a34939Shaad if (!(seg->log_lv = find_lv(seg->lv->vg, logname))) {
125*7c604eeaShaad log_error("Unrecognised mirror log in "
126*7c604eeaShaad "segment %s of logical volume %s.",
127*7c604eeaShaad config_parent_name(sn), seg->lv->name);
12856a34939Shaad return 0;
12956a34939Shaad }
13056a34939Shaad seg->log_lv->status |= MIRROR_LOG;
13156a34939Shaad }
13256a34939Shaad
13356a34939Shaad if (logname && !seg->region_size) {
134*7c604eeaShaad log_error("Missing region size for mirror log for "
135*7c604eeaShaad "segment %s of logical volume %s.",
136*7c604eeaShaad config_parent_name(sn), seg->lv->name);
13756a34939Shaad return 0;
13856a34939Shaad }
13956a34939Shaad
14056a34939Shaad if (!(cn = find_config_node(sn, "mirrors"))) {
141*7c604eeaShaad log_error("Couldn't find mirrors array for "
142*7c604eeaShaad "segment %s of logical volume %s.",
143*7c604eeaShaad config_parent_name(sn), seg->lv->name);
14456a34939Shaad return 0;
14556a34939Shaad }
14656a34939Shaad
14756a34939Shaad return text_import_areas(seg, sn, cn, pv_hash, MIRROR_IMAGE);
14856a34939Shaad }
14956a34939Shaad
_mirrored_text_export(const struct lv_segment * seg,struct formatter * f)15056a34939Shaad static int _mirrored_text_export(const struct lv_segment *seg, struct formatter *f)
15156a34939Shaad {
15256a34939Shaad outf(f, "mirror_count = %u", seg->area_count);
15356a34939Shaad if (seg->status & PVMOVE)
15456a34939Shaad out_size(f, (uint64_t) seg->extents_copied * seg->lv->vg->extent_size,
15556a34939Shaad "extents_moved = %" PRIu32, seg->extents_copied);
15656a34939Shaad if (seg->log_lv)
15756a34939Shaad outf(f, "mirror_log = \"%s\"", seg->log_lv->name);
15856a34939Shaad if (seg->region_size)
15956a34939Shaad outf(f, "region_size = %" PRIu32, seg->region_size);
16056a34939Shaad
16156a34939Shaad return out_areas(f, seg, "mirror");
16256a34939Shaad }
16356a34939Shaad
16456a34939Shaad #ifdef DEVMAPPER_SUPPORT
_mirrored_init_target(struct dm_pool * mem,struct cmd_context * cmd)16556a34939Shaad static struct mirror_state *_mirrored_init_target(struct dm_pool *mem,
16656a34939Shaad struct cmd_context *cmd)
16756a34939Shaad {
16856a34939Shaad struct mirror_state *mirr_state;
16956a34939Shaad
17056a34939Shaad if (!(mirr_state = dm_pool_alloc(mem, sizeof(*mirr_state)))) {
17156a34939Shaad log_error("struct mirr_state allocation failed");
17256a34939Shaad return NULL;
17356a34939Shaad }
17456a34939Shaad
17556a34939Shaad mirr_state->default_region_size = 2 *
17656a34939Shaad find_config_tree_int(cmd,
17756a34939Shaad "activation/mirror_region_size",
17856a34939Shaad DEFAULT_MIRROR_REGION_SIZE);
17956a34939Shaad
18056a34939Shaad return mirr_state;
18156a34939Shaad }
18256a34939Shaad
_mirrored_target_percent(void ** target_state,percent_range_t * percent_range,struct dm_pool * mem,struct cmd_context * cmd,struct lv_segment * seg,char * params,uint64_t * total_numerator,uint64_t * total_denominator)183*7c604eeaShaad static int _mirrored_target_percent(void **target_state,
184*7c604eeaShaad percent_range_t *percent_range,
185*7c604eeaShaad struct dm_pool *mem,
186*7c604eeaShaad struct cmd_context *cmd,
187*7c604eeaShaad struct lv_segment *seg, char *params,
188*7c604eeaShaad uint64_t *total_numerator,
18956a34939Shaad uint64_t *total_denominator)
19056a34939Shaad {
19156a34939Shaad struct mirror_state *mirr_state;
19256a34939Shaad uint64_t numerator, denominator;
19356a34939Shaad unsigned mirror_count, m;
19456a34939Shaad int used;
19556a34939Shaad char *pos = params;
19656a34939Shaad
19756a34939Shaad if (!*target_state)
19856a34939Shaad *target_state = _mirrored_init_target(mem, cmd);
19956a34939Shaad
20056a34939Shaad mirr_state = *target_state;
20156a34939Shaad
20256a34939Shaad /* Status line: <#mirrors> (maj:min)+ <synced>/<total_regions> */
20356a34939Shaad log_debug("Mirror status: %s", params);
20456a34939Shaad
20556a34939Shaad if (sscanf(pos, "%u %n", &mirror_count, &used) != 1) {
20656a34939Shaad log_error("Failure parsing mirror status mirror count: %s",
20756a34939Shaad params);
20856a34939Shaad return 0;
20956a34939Shaad }
21056a34939Shaad pos += used;
21156a34939Shaad
21256a34939Shaad for (m = 0; m < mirror_count; m++) {
21356a34939Shaad if (sscanf(pos, "%*x:%*x %n", &used) != 0) {
21456a34939Shaad log_error("Failure parsing mirror status devices: %s",
21556a34939Shaad params);
21656a34939Shaad return 0;
21756a34939Shaad }
21856a34939Shaad pos += used;
21956a34939Shaad }
22056a34939Shaad
22156a34939Shaad if (sscanf(pos, "%" PRIu64 "/%" PRIu64 "%n", &numerator, &denominator,
22256a34939Shaad &used) != 2) {
22356a34939Shaad log_error("Failure parsing mirror status fraction: %s", params);
22456a34939Shaad return 0;
22556a34939Shaad }
22656a34939Shaad pos += used;
22756a34939Shaad
22856a34939Shaad *total_numerator += numerator;
22956a34939Shaad *total_denominator += denominator;
23056a34939Shaad
23156a34939Shaad if (seg)
23256a34939Shaad seg->extents_copied = seg->area_len * numerator / denominator;
23356a34939Shaad
234*7c604eeaShaad if (numerator == denominator)
235*7c604eeaShaad *percent_range = PERCENT_100;
236*7c604eeaShaad else if (numerator == 0)
237*7c604eeaShaad *percent_range = PERCENT_0;
238*7c604eeaShaad else
239*7c604eeaShaad *percent_range = PERCENT_0_TO_100;
240*7c604eeaShaad
24156a34939Shaad return 1;
24256a34939Shaad }
24356a34939Shaad
_add_log(struct dev_manager * dm,struct lv_segment * seg,struct dm_tree_node * node,uint32_t area_count,uint32_t region_size)24456a34939Shaad static int _add_log(struct dev_manager *dm, struct lv_segment *seg,
24556a34939Shaad struct dm_tree_node *node, uint32_t area_count, uint32_t region_size)
24656a34939Shaad {
24756a34939Shaad unsigned clustered = 0;
24856a34939Shaad char *log_dlid = NULL;
24956a34939Shaad uint32_t log_flags = 0;
25056a34939Shaad
25156a34939Shaad /*
25256a34939Shaad * Use clustered mirror log for non-exclusive activation
25356a34939Shaad * in clustered VG.
25456a34939Shaad */
25556a34939Shaad if ((!(seg->lv->status & ACTIVATE_EXCL) &&
25656a34939Shaad (vg_is_clustered(seg->lv->vg))))
25756a34939Shaad clustered = 1;
25856a34939Shaad
25956a34939Shaad if (seg->log_lv) {
26056a34939Shaad /* If disk log, use its UUID */
26156a34939Shaad if (!(log_dlid = build_dlid(dm, seg->log_lv->lvid.s, NULL))) {
26256a34939Shaad log_error("Failed to build uuid for log LV %s.",
26356a34939Shaad seg->log_lv->name);
26456a34939Shaad return 0;
26556a34939Shaad }
26656a34939Shaad } else {
26756a34939Shaad /* If core log, use mirror's UUID and set DM_CORELOG flag */
26856a34939Shaad if (!(log_dlid = build_dlid(dm, seg->lv->lvid.s, NULL))) {
26956a34939Shaad log_error("Failed to build uuid for mirror LV %s.",
27056a34939Shaad seg->lv->name);
27156a34939Shaad return 0;
27256a34939Shaad }
27356a34939Shaad log_flags |= DM_CORELOG;
27456a34939Shaad }
27556a34939Shaad
27656a34939Shaad if (mirror_in_sync() && !(seg->status & PVMOVE))
27756a34939Shaad log_flags |= DM_NOSYNC;
27856a34939Shaad
27956a34939Shaad if (_block_on_error_available && !(seg->status & PVMOVE))
28056a34939Shaad log_flags |= DM_BLOCK_ON_ERROR;
28156a34939Shaad
28256a34939Shaad return dm_tree_node_add_mirror_target_log(node, region_size, clustered, log_dlid, area_count, log_flags);
28356a34939Shaad }
28456a34939Shaad
_mirrored_add_target_line(struct dev_manager * dm,struct dm_pool * mem,struct cmd_context * cmd,void ** target_state,struct lv_segment * seg,struct dm_tree_node * node,uint64_t len,uint32_t * pvmove_mirror_count)28556a34939Shaad static int _mirrored_add_target_line(struct dev_manager *dm, struct dm_pool *mem,
28656a34939Shaad struct cmd_context *cmd, void **target_state,
28756a34939Shaad struct lv_segment *seg,
28856a34939Shaad struct dm_tree_node *node, uint64_t len,
28956a34939Shaad uint32_t *pvmove_mirror_count)
29056a34939Shaad {
29156a34939Shaad struct mirror_state *mirr_state;
29256a34939Shaad uint32_t area_count = seg->area_count;
29356a34939Shaad unsigned start_area = 0u;
29456a34939Shaad int mirror_status = MIRR_RUNNING;
295*7c604eeaShaad uint32_t region_size;
29656a34939Shaad int r;
29756a34939Shaad
29856a34939Shaad if (!*target_state)
29956a34939Shaad *target_state = _mirrored_init_target(mem, cmd);
30056a34939Shaad
30156a34939Shaad mirr_state = *target_state;
30256a34939Shaad
30356a34939Shaad /*
30456a34939Shaad * Mirror segment could have only 1 area temporarily
30556a34939Shaad * if the segment is under conversion.
30656a34939Shaad */
30756a34939Shaad if (seg->area_count == 1)
30856a34939Shaad mirror_status = MIRR_DISABLED;
30956a34939Shaad
31056a34939Shaad /*
31156a34939Shaad * For pvmove, only have one mirror segment RUNNING at once.
31256a34939Shaad * Segments before this are COMPLETED and use 2nd area.
31356a34939Shaad * Segments after this are DISABLED and use 1st area.
31456a34939Shaad */
31556a34939Shaad if (seg->status & PVMOVE) {
31656a34939Shaad if (seg->extents_copied == seg->area_len) {
31756a34939Shaad mirror_status = MIRR_COMPLETED;
31856a34939Shaad start_area = 1;
31956a34939Shaad } else if ((*pvmove_mirror_count)++) {
32056a34939Shaad mirror_status = MIRR_DISABLED;
32156a34939Shaad area_count = 1;
32256a34939Shaad }
32356a34939Shaad /* else MIRR_RUNNING */
32456a34939Shaad }
32556a34939Shaad
32656a34939Shaad if (mirror_status != MIRR_RUNNING) {
32756a34939Shaad if (!dm_tree_node_add_linear_target(node, len))
32856a34939Shaad return_0;
32956a34939Shaad goto done;
33056a34939Shaad }
33156a34939Shaad
33256a34939Shaad if (!(seg->status & PVMOVE)) {
33356a34939Shaad if (!seg->region_size) {
33456a34939Shaad log_error("Missing region size for mirror segment.");
33556a34939Shaad return 0;
33656a34939Shaad }
33756a34939Shaad region_size = seg->region_size;
33856a34939Shaad
339*7c604eeaShaad } else
340*7c604eeaShaad region_size = adjusted_mirror_region_size(seg->lv->vg->extent_size,
341*7c604eeaShaad seg->area_len,
342*7c604eeaShaad mirr_state->default_region_size);
34356a34939Shaad
34456a34939Shaad if (!dm_tree_node_add_mirror_target(node, len))
34556a34939Shaad return_0;
34656a34939Shaad
34756a34939Shaad if ((r = _add_log(dm, seg, node, area_count, region_size)) <= 0) {
34856a34939Shaad stack;
34956a34939Shaad return r;
35056a34939Shaad }
35156a34939Shaad
35256a34939Shaad done:
35356a34939Shaad return add_areas_line(dm, seg, node, start_area, area_count);
35456a34939Shaad }
35556a34939Shaad
_mirrored_target_present(struct cmd_context * cmd,const struct lv_segment * seg,unsigned * attributes)356*7c604eeaShaad static int _mirrored_target_present(struct cmd_context *cmd,
357*7c604eeaShaad const struct lv_segment *seg,
35856a34939Shaad unsigned *attributes)
35956a34939Shaad {
36056a34939Shaad static int _mirrored_checked = 0;
36156a34939Shaad static int _mirrored_present = 0;
36256a34939Shaad uint32_t maj, min, patchlevel;
36356a34939Shaad unsigned maj2, min2, patchlevel2;
36456a34939Shaad char vsn[80];
36556a34939Shaad
36656a34939Shaad if (!_mirrored_checked) {
367*7c604eeaShaad _mirrored_present = target_present(cmd, "mirror", 1);
36856a34939Shaad
36956a34939Shaad /*
370*7c604eeaShaad * block_on_error available as "block_on_error" log
371*7c604eeaShaad * argument with mirror target >= 1.1 and <= 1.11
37256a34939Shaad * or with 1.0 in RHEL4U3 driver >= 4.5
373*7c604eeaShaad *
374*7c604eeaShaad * block_on_error available as "handle_errors" mirror
375*7c604eeaShaad * argument with mirror target >= 1.12.
376*7c604eeaShaad *
377*7c604eeaShaad * libdm-deptree.c is smart enough to handle the differences
378*7c604eeaShaad * between block_on_error and handle_errors for all
379*7c604eeaShaad * mirror target versions >= 1.1
38056a34939Shaad */
38156a34939Shaad /* FIXME Move this into libdevmapper */
38256a34939Shaad
38356a34939Shaad if (target_version("mirror", &maj, &min, &patchlevel) &&
38456a34939Shaad maj == 1 &&
385*7c604eeaShaad ((min >= 1) ||
38656a34939Shaad (min == 0 && driver_version(vsn, sizeof(vsn)) &&
38756a34939Shaad sscanf(vsn, "%u.%u.%u", &maj2, &min2, &patchlevel2) == 3 &&
38856a34939Shaad maj2 == 4 && min2 == 5 && patchlevel2 == 0))) /* RHEL4U3 */
38956a34939Shaad _block_on_error_available = 1;
39056a34939Shaad }
39156a34939Shaad
39256a34939Shaad /*
39356a34939Shaad * Check only for modules if atttributes requested and no previous check.
39456a34939Shaad * FIXME: Fails incorrectly if cmirror was built into kernel.
39556a34939Shaad */
39656a34939Shaad if (attributes) {
397*7c604eeaShaad if (!_mirror_attributes && module_present(cmd, "log-clustered"))
39856a34939Shaad _mirror_attributes |= MIRROR_LOG_CLUSTERED;
39956a34939Shaad *attributes = _mirror_attributes;
40056a34939Shaad }
40156a34939Shaad _mirrored_checked = 1;
40256a34939Shaad
40356a34939Shaad return _mirrored_present;
40456a34939Shaad }
40556a34939Shaad
40656a34939Shaad #ifdef DMEVENTD
_get_mirror_dso_path(struct cmd_context * cmd,char ** dso)40756a34939Shaad static int _get_mirror_dso_path(struct cmd_context *cmd, char **dso)
40856a34939Shaad {
40956a34939Shaad char *path;
41056a34939Shaad const char *libpath;
41156a34939Shaad
41256a34939Shaad if (!(path = dm_pool_alloc(cmd->mem, PATH_MAX))) {
41356a34939Shaad log_error("Failed to allocate dmeventd library path.");
41456a34939Shaad return 0;
41556a34939Shaad }
41656a34939Shaad
41756a34939Shaad libpath = find_config_tree_str(cmd, "dmeventd/mirror_library",
41856a34939Shaad DEFAULT_DMEVENTD_MIRROR_LIB);
41956a34939Shaad
42056a34939Shaad get_shared_library_path(cmd, libpath, path, PATH_MAX);
42156a34939Shaad
42256a34939Shaad *dso = path;
42356a34939Shaad
42456a34939Shaad return 1;
42556a34939Shaad }
42656a34939Shaad
_create_dm_event_handler(const char * dmname,const char * dso,enum dm_event_mask mask)42756a34939Shaad static struct dm_event_handler *_create_dm_event_handler(const char *dmname,
42856a34939Shaad const char *dso,
42956a34939Shaad enum dm_event_mask mask)
43056a34939Shaad {
43156a34939Shaad struct dm_event_handler *dmevh;
43256a34939Shaad
43356a34939Shaad if (!(dmevh = dm_event_handler_create()))
43456a34939Shaad return_0;
43556a34939Shaad
43656a34939Shaad if (dm_event_handler_set_dso(dmevh, dso))
43756a34939Shaad goto fail;
43856a34939Shaad
43956a34939Shaad if (dm_event_handler_set_dev_name(dmevh, dmname))
44056a34939Shaad goto fail;
44156a34939Shaad
44256a34939Shaad dm_event_handler_set_event_mask(dmevh, mask);
44356a34939Shaad return dmevh;
44456a34939Shaad
44556a34939Shaad fail:
44656a34939Shaad dm_event_handler_destroy(dmevh);
44756a34939Shaad return NULL;
44856a34939Shaad }
44956a34939Shaad
_target_monitored(struct lv_segment * seg,int * pending)45056a34939Shaad static int _target_monitored(struct lv_segment *seg, int *pending)
45156a34939Shaad {
45256a34939Shaad char *dso, *name;
45356a34939Shaad struct logical_volume *lv;
45456a34939Shaad struct volume_group *vg;
45556a34939Shaad enum dm_event_mask evmask = 0;
45656a34939Shaad struct dm_event_handler *dmevh;
45756a34939Shaad
45856a34939Shaad lv = seg->lv;
45956a34939Shaad vg = lv->vg;
46056a34939Shaad
46156a34939Shaad *pending = 0;
46256a34939Shaad if (!_get_mirror_dso_path(vg->cmd, &dso))
46356a34939Shaad return_0;
46456a34939Shaad
46556a34939Shaad if (!(name = build_dm_name(vg->cmd->mem, vg->name, lv->name, NULL)))
46656a34939Shaad return_0;
46756a34939Shaad
46856a34939Shaad if (!(dmevh = _create_dm_event_handler(name, dso, DM_EVENT_ALL_ERRORS)))
46956a34939Shaad return_0;
47056a34939Shaad
47156a34939Shaad if (dm_event_get_registered_device(dmevh, 0)) {
47256a34939Shaad dm_event_handler_destroy(dmevh);
47356a34939Shaad return 0;
47456a34939Shaad }
47556a34939Shaad
47656a34939Shaad evmask = dm_event_handler_get_event_mask(dmevh);
47756a34939Shaad if (evmask & DM_EVENT_REGISTRATION_PENDING) {
47856a34939Shaad *pending = 1;
47956a34939Shaad evmask &= ~DM_EVENT_REGISTRATION_PENDING;
48056a34939Shaad }
48156a34939Shaad
48256a34939Shaad dm_event_handler_destroy(dmevh);
48356a34939Shaad
48456a34939Shaad return evmask;
48556a34939Shaad }
48656a34939Shaad
48756a34939Shaad /* FIXME This gets run while suspended and performs banned operations. */
_target_set_events(struct lv_segment * seg,int evmask __attribute ((unused)),int set)48856a34939Shaad static int _target_set_events(struct lv_segment *seg,
48956a34939Shaad int evmask __attribute((unused)), int set)
49056a34939Shaad {
49156a34939Shaad char *dso, *name;
49256a34939Shaad struct logical_volume *lv;
49356a34939Shaad struct volume_group *vg;
49456a34939Shaad struct dm_event_handler *dmevh;
49556a34939Shaad int r;
49656a34939Shaad
49756a34939Shaad lv = seg->lv;
49856a34939Shaad vg = lv->vg;
49956a34939Shaad
50056a34939Shaad if (!_get_mirror_dso_path(vg->cmd, &dso))
50156a34939Shaad return_0;
50256a34939Shaad
50356a34939Shaad if (!(name = build_dm_name(vg->cmd->mem, vg->name, lv->name, NULL)))
50456a34939Shaad return_0;
50556a34939Shaad
50656a34939Shaad if (!(dmevh = _create_dm_event_handler(name, dso, DM_EVENT_ALL_ERRORS)))
50756a34939Shaad return_0;
50856a34939Shaad
50956a34939Shaad r = set ? dm_event_register_handler(dmevh) : dm_event_unregister_handler(dmevh);
51056a34939Shaad dm_event_handler_destroy(dmevh);
51156a34939Shaad if (!r)
51256a34939Shaad return_0;
51356a34939Shaad
51456a34939Shaad log_info("%s %s for events", set ? "Monitored" : "Unmonitored", name);
51556a34939Shaad
51656a34939Shaad return 1;
51756a34939Shaad }
51856a34939Shaad
_target_monitor_events(struct lv_segment * seg,int events)51956a34939Shaad static int _target_monitor_events(struct lv_segment *seg, int events)
52056a34939Shaad {
52156a34939Shaad return _target_set_events(seg, events, 1);
52256a34939Shaad }
52356a34939Shaad
_target_unmonitor_events(struct lv_segment * seg,int events)52456a34939Shaad static int _target_unmonitor_events(struct lv_segment *seg, int events)
52556a34939Shaad {
52656a34939Shaad return _target_set_events(seg, events, 0);
52756a34939Shaad }
52856a34939Shaad
52956a34939Shaad #endif /* DMEVENTD */
53056a34939Shaad #endif /* DEVMAPPER_SUPPORT */
53156a34939Shaad
_mirrored_modules_needed(struct dm_pool * mem,const struct lv_segment * seg,struct dm_list * modules)53256a34939Shaad static int _mirrored_modules_needed(struct dm_pool *mem,
53356a34939Shaad const struct lv_segment *seg,
53456a34939Shaad struct dm_list *modules)
53556a34939Shaad {
53656a34939Shaad if (seg->log_lv &&
53756a34939Shaad !list_segment_modules(mem, first_seg(seg->log_lv), modules))
53856a34939Shaad return_0;
53956a34939Shaad
54056a34939Shaad if (vg_is_clustered(seg->lv->vg) &&
54156a34939Shaad !str_list_add(mem, modules, "clog")) {
54256a34939Shaad log_error("cluster log string list allocation failed");
54356a34939Shaad return 0;
54456a34939Shaad }
54556a34939Shaad
54656a34939Shaad if (!str_list_add(mem, modules, "mirror")) {
54756a34939Shaad log_error("mirror string list allocation failed");
54856a34939Shaad return 0;
54956a34939Shaad }
55056a34939Shaad
55156a34939Shaad return 1;
55256a34939Shaad }
55356a34939Shaad
_mirrored_destroy(const struct segment_type * segtype)55456a34939Shaad static void _mirrored_destroy(const struct segment_type *segtype)
55556a34939Shaad {
55656a34939Shaad dm_free((void *) segtype);
55756a34939Shaad }
55856a34939Shaad
55956a34939Shaad static struct segtype_handler _mirrored_ops = {
56056a34939Shaad .name = _mirrored_name,
56156a34939Shaad .display = _mirrored_display,
56256a34939Shaad .text_import_area_count = _mirrored_text_import_area_count,
56356a34939Shaad .text_import = _mirrored_text_import,
56456a34939Shaad .text_export = _mirrored_text_export,
56556a34939Shaad #ifdef DEVMAPPER_SUPPORT
56656a34939Shaad .add_target_line = _mirrored_add_target_line,
56756a34939Shaad .target_percent = _mirrored_target_percent,
56856a34939Shaad .target_present = _mirrored_target_present,
56956a34939Shaad #ifdef DMEVENTD
57056a34939Shaad .target_monitored = _target_monitored,
57156a34939Shaad .target_monitor_events = _target_monitor_events,
57256a34939Shaad .target_unmonitor_events = _target_unmonitor_events,
57356a34939Shaad #endif
57456a34939Shaad #endif
57556a34939Shaad .modules_needed = _mirrored_modules_needed,
57656a34939Shaad .destroy = _mirrored_destroy,
57756a34939Shaad };
57856a34939Shaad
57956a34939Shaad #ifdef MIRRORED_INTERNAL
init_mirrored_segtype(struct cmd_context * cmd)58056a34939Shaad struct segment_type *init_mirrored_segtype(struct cmd_context *cmd)
58156a34939Shaad #else /* Shared */
58256a34939Shaad struct segment_type *init_segtype(struct cmd_context *cmd);
58356a34939Shaad struct segment_type *init_segtype(struct cmd_context *cmd)
58456a34939Shaad #endif
58556a34939Shaad {
58656a34939Shaad struct segment_type *segtype = dm_malloc(sizeof(*segtype));
58756a34939Shaad
58856a34939Shaad if (!segtype)
58956a34939Shaad return_NULL;
59056a34939Shaad
59156a34939Shaad segtype->cmd = cmd;
59256a34939Shaad segtype->ops = &_mirrored_ops;
59356a34939Shaad segtype->name = "mirror";
59456a34939Shaad segtype->private = NULL;
59556a34939Shaad segtype->flags = SEG_AREAS_MIRRORED | SEG_MONITORED;
59656a34939Shaad
59756a34939Shaad log_very_verbose("Initialised segtype: %s", segtype->name);
59856a34939Shaad
59956a34939Shaad return segtype;
60056a34939Shaad }
601