Debian Codegraph @

daemons/dmeventd/plugins/mirror/dmeventd_mirror.c

raw
1 /*
2 * Copyright (C) 2005-2017 Red Hat, Inc. All rights reserved.
3 *
4 * This file is part of LVM2.
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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
13 */
14
15 #include "lib/misc/lib.h"
16 #include "daemons/dmeventd/plugins/lvm2/dmeventd_lvm.h"
17 #include "daemons/dmeventd/libdevmapper-event.h"
18 #include "lib/activate/activate.h"
19
20 /* FIXME Reformat to 80 char lines. */
21
22 #define ME_IGNORE 0
23 #define ME_INSYNC 1
24 #define ME_FAILURE 2
25
26 struct dso_state {
27 struct dm_pool *mem;
28 char cmd_lvconvert[512];
29 };
30
31 DM_EVENT_LOG_FN("mirr")
32
33 static void _process_status_code(dm_status_mirror_health_t health,
34 uint32_t major, uint32_t minor,
35 const char *dev_type, int *r)
36 {
37 /*
38 * A => Alive - No failures
39 * D => Dead - A write failure occurred leaving mirror out-of-sync
40 * F => Flush failed.
41 * S => Sync - A synchronization failure occurred, mirror out-of-sync
42 * R => Read - A read failure occurred, mirror data unaffected
43 * U => Unclassified failure (bug)
44 */
45 switch (health) {
46 case DM_STATUS_MIRROR_ALIVE:
47 return;
48 case DM_STATUS_MIRROR_FLUSH_FAILED:
49 log_error("%s device %u:%u flush failed.",
50 dev_type, major, minor);
51 *r = ME_FAILURE;
52 break;
53 case DM_STATUS_MIRROR_SYNC_FAILED:
54 log_error("%s device %u:%u sync failed.",
55 dev_type, major, minor);
56 break;
57 case DM_STATUS_MIRROR_READ_FAILED:
58 log_error("%s device %u:%u read failed.",
59 dev_type, major, minor);
60 break;
61 default:
62 log_error("%s device %u:%u has failed (%c).",
63 dev_type, major, minor, (char)health);
64 *r = ME_FAILURE;
65 break;
66 }
67 }
68
69 static int _get_mirror_event(struct dso_state *state, char *params)
70 {
71 int r = ME_INSYNC;
72 unsigned i;
73 struct dm_status_mirror *ms;
74
75 if (!dm_get_status_mirror(state->mem, params, &ms)) {
76 log_error("Unable to parse mirror status string.");
77 return ME_IGNORE;
78 }
79
80 /* Check for bad mirror devices */
81 for (i = 0; i < ms->dev_count; ++i)
82 _process_status_code(ms->devs[i].health,
83 ms->devs[i].major, ms->devs[i].minor,
84 i ? "Secondary mirror" : "Primary mirror", &r);
85
86 /* Check for bad disk log device */
87 for (i = 0; i < ms->log_count; ++i)
88 _process_status_code(ms->logs[i].health,
89 ms->logs[i].major, ms->logs[i].minor,
90 "Log", &r);
91
92 /* Ignore if not in-sync */
93 if ((r == ME_INSYNC) && (ms->insync_regions != ms->total_regions))
94 r = ME_IGNORE;
95
96 dm_pool_free(state->mem, ms);
97
98 return r;
99 }
100
101 static int _remove_failed_devices(const char *cmd_lvconvert, const char *device)
102 {
103 /* if repair goes OK, report success even if lvscan has failed */
104 if (!dmeventd_lvm2_run_with_lock(cmd_lvconvert)) {
105 log_error("Repair of mirrored device %s failed.", device);
106 return 0;
107 }
108
109 log_info("Repair of mirrored device %s finished successfully.", device);
110
111 return 1;
112 }
113
114 void process_event(struct dm_task *dmt,
115 enum dm_event_mask evmask __attribute__((unused)),
116 void **user)
117 {
118 struct dso_state *state = *user;
119 void *next = NULL;
120 uint64_t start, length;
121 char *target_type = NULL;
122 char *params;
123 const char *device = dm_task_get_name(dmt);
124
125 do {
126 next = dm_get_next_target(dmt, next, &start, &length,
127 &target_type, &params);
128
129 if (!target_type) {
130 log_info("%s mapping lost.", device);
131 continue;
132 }
133
134 if (strcmp(target_type, TARGET_NAME_MIRROR)) {
135 log_info("%s has unmirrored portion.", device);
136 continue;
137 }
138
139 switch(_get_mirror_event(state, params)) {
140 case ME_INSYNC:
141 /* FIXME: all we really know is that this
142 _part_ of the device is in sync
143 Also, this is not an error
144 */
145 log_notice("%s is now in-sync.", device);
146 break;
147 case ME_FAILURE:
148 log_error("Device failure in %s.", device);
149 if (!_remove_failed_devices(state->cmd_lvconvert, device))
150 /* FIXME Why are all the error return codes unused? Get rid of them? */
151 log_error("Failed to remove faulty devices in %s.",
152 device);
153 /* Should check before warning user that device is now linear
154 else
155 log_notice("%s is now a linear device.",
156 device);
157 */
158 break;
159 case ME_IGNORE:
160 break;
161 default:
162 /* FIXME Provide value then! */
163 log_warn("WARNING: %s received unknown event.", device);
164 }
165 } while (next);
166 }
167
168 int register_device(const char *device,
169 const char *uuid __attribute__((unused)),
170 int major __attribute__((unused)),
171 int minor __attribute__((unused)),
172 void **user)
173 {
174 struct dso_state *state;
175
176 if (!dmeventd_lvm2_init_with_pool("mirror_state", state))
177 goto_bad;
178
179 /* CANNOT use --config as this disables cached content */
180 if (!dmeventd_lvm2_command(state->mem, state->cmd_lvconvert, sizeof(state->cmd_lvconvert),
181 "lvconvert --repair --use-policies", device))
182 goto_bad;
183
184 *user = state;
185
186 log_info("Monitoring mirror device %s for events.", device);
187
188 return 1;
189 bad:
190 log_error("Failed to monitor mirror %s.", device);
191
192 if (state)
193 dmeventd_lvm2_exit_with_pool(state);
194
195 return 0;
196 }
197
198 int unregister_device(const char *device,
199 const char *uuid __attribute__((unused)),
200 int major __attribute__((unused)),
201 int minor __attribute__((unused)),
202 void **user)
203 {
204 struct dso_state *state = *user;
205
206 dmeventd_lvm2_exit_with_pool(state);
207 log_info("No longer monitoring mirror device %s for events.",
208 device);
209
210 return 1;
211 }