summaryrefslogtreecommitdiff
path: root/daemons/dmeventd/plugins/mirror/dmeventd_mirror.c
blob: f3abb67f58f6755b7cbc807a7f39e820a2c0e048 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
/*
 * Copyright (C) 2005-2015 Red Hat, Inc. All rights reserved.
 *
 * This file is part of LVM2.
 *
 * This copyrighted material is made available to anyone wishing to use,
 * modify, copy, or redistribute it subject to the terms and conditions
 * of the GNU Lesser General Public License v.2.1.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with this program; if not, write to the Free Software Foundation,
 * Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 */

#include "lib.h"
#include "libdevmapper-event.h"
#include "dmeventd_lvm.h"
#include "defaults.h"

/* FIXME Reformat to 80 char lines. */

#define ME_IGNORE    0
#define ME_INSYNC    1
#define ME_FAILURE   2

struct dso_state {
	struct dm_pool *mem;
	char cmd_lvscan[512];
	char cmd_lvconvert[512];
};

DM_EVENT_LOG_FN("mirr")

static void _process_status_code(dm_status_mirror_health_t health,
				 uint32_t major, uint32_t minor,
				 const char *dev_type, int *r)
{
	/*
	 *    A => Alive - No failures
	 *    D => Dead - A write failure occurred leaving mirror out-of-sync
	 *    F => Flush failed.
	 *    S => Sync - A sychronization failure occurred, mirror out-of-sync
	 *    R => Read - A read failure occurred, mirror data unaffected
	 *    U => Unclassified failure (bug)
	 */ 
	switch (health) {
	case DM_STATUS_MIRROR_ALIVE:
		return;
	case DM_STATUS_MIRROR_FLUSH_FAILED:
		log_error("%s device %u:%u flush failed.",
			  dev_type, major, minor);
		*r = ME_FAILURE;
		break;
	case DM_STATUS_MIRROR_SYNC_FAILED:
		log_error("%s device %u:%u sync failed.",
			  dev_type, major, minor);
		break;
	case DM_STATUS_MIRROR_READ_FAILED:
		log_error("%s device %u:%u read failed.",
			  dev_type, major, minor);
		break;
	default:
		log_error("%s device %u:%u has failed (%c).",
			  dev_type, major, minor, (char)health);
		*r = ME_FAILURE;
		break;
	}
}

static int _get_mirror_event(struct dso_state *state, char *params)
{
	int r = ME_INSYNC;
	unsigned i;
	struct dm_status_mirror *ms;

	if (!dm_get_status_mirror(state->mem, params, &ms))
		goto_out;

	/* Check for bad mirror devices */
	for (i = 0; i < ms->dev_count; ++i)
		_process_status_code(ms->devs[i].health,
				     ms->devs[i].major, ms->devs[i].minor,
				     i ? "Secondary mirror" : "Primary mirror", &r);

	/* Check for bad disk log device */
	for (i = 0; i < ms->log_count; ++i)
		_process_status_code(ms->logs[i].health,
				     ms->logs[i].major, ms->logs[i].minor,
				     "Log", &r);

	/* Ignore if not in-sync */
	if ((r == ME_INSYNC) && (ms->insync_regions != ms->total_regions))
		r = ME_IGNORE;

	dm_pool_free(state->mem, ms);

	return r;

out:
	log_error("Unable to parse mirror status string.");

	return ME_IGNORE;
}

static int _remove_failed_devices(const char *cmd_lvscan, const char *cmd_lvconvert)
{
	int r;

	if (!dmeventd_lvm2_run_with_lock(cmd_lvscan))
		log_info("Re-scan of mirrored device failed.");

	/* if repair goes OK, report success even if lvscan has failed */
	r = dmeventd_lvm2_run_with_lock(cmd_lvconvert);

	log_info("Repair of mirrored device %s.",
		 (r) ? "finished successfully" : "failed");

	return r;
}

void process_event(struct dm_task *dmt,
		   enum dm_event_mask event __attribute__((unused)),
		   void **user)
{
	struct dso_state *state = *user;
	void *next = NULL;
	uint64_t start, length;
	char *target_type = NULL;
	char *params;
	const char *device = dm_task_get_name(dmt);

	do {
		next = dm_get_next_target(dmt, next, &start, &length,
					  &target_type, &params);

		if (!target_type) {
			log_info("%s mapping lost.", device);
			continue;
		}

		if (strcmp(target_type, "mirror")) {
			log_info("%s has unmirrored portion.", device);
			continue;
		}

		switch(_get_mirror_event(state, params)) {
		case ME_INSYNC:
			/* FIXME: all we really know is that this
			   _part_ of the device is in sync
			   Also, this is not an error
			*/
			log_notice("%s is now in-sync.", device);
			break;
		case ME_FAILURE:
			log_error("Device failure in %s.", device);
			if (!_remove_failed_devices(state->cmd_lvscan,
						    state->cmd_lvconvert))
				/* FIXME Why are all the error return codes unused? Get rid of them? */
				log_error("Failed to remove faulty devices in %s.",
					  device);
			/* Should check before warning user that device is now linear
			else
				log_notice("%s is now a linear device.",
					   device);
			*/
			break;
		case ME_IGNORE:
			break;
		default:
			/* FIXME Provide value then! */
			log_info("Unknown event received.");
		}
	} while (next);
}

int register_device(const char *device,
		    const char *uuid __attribute__((unused)),
		    int major __attribute__((unused)),
		    int minor __attribute__((unused)),
		    void **user)
{
	struct dso_state *state;

	if (!dmeventd_lvm2_init_with_pool("mirror_state", state))
		goto_bad;

	if (!dmeventd_lvm2_command(state->mem, state->cmd_lvscan, sizeof(state->cmd_lvscan),
				   "lvscan --cache", device)) {
		dmeventd_lvm2_exit_with_pool(state);
		goto_bad;
	}

	if (!dmeventd_lvm2_command(state->mem, state->cmd_lvconvert, sizeof(state->cmd_lvconvert),
				   "lvconvert --repair --use-policies", device)) {
		dmeventd_lvm2_exit_with_pool(state);
		goto_bad;
	}

	*user = state;

	log_info("Monitoring mirror device %s for events.", device);

	return 1;
bad:
	log_error("Failed to monitor mirror %s.", device);

	return 0;
}

int unregister_device(const char *device,
		      const char *uuid __attribute__((unused)),
		      int major __attribute__((unused)),
		      int minor __attribute__((unused)),
		      void **user)
{
	struct dso_state *state = *user;

	dmeventd_lvm2_exit_with_pool(state);
	log_info("No longer monitoring mirror device %s for events.",
		 device);

	return 1;
}