Debian Codegraph @

daemons/dmeventd/plugins/vdo/dmeventd_vdo.c

raw
1 /*
2 * Copyright (C) 2018 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
19 /*
20 * Use parser from new device_mapper library.
21 * Although during compilation we can see dm_vdo_status_parse()
22 * in runtime we are linked against systems libdm 'older' library
23 * which does not provide this symbol and plugin fails to load
24 */
25 /* coverity[unnecessary_header] used for parsing */
26 #include "device_mapper/vdo/status.c"
27
28 #include <sys/wait.h>
29 #include <stdarg.h>
30
31 /* First warning when VDO pool is 80% full. */
32 #define WARNING_THRESH (DM_PERCENT_1 * 80)
33 /* Run a check every 5%. */
34 #define CHECK_STEP (DM_PERCENT_1 * 5)
35 /* Do not bother checking VDO pool is less than 50% full. */
36 #define CHECK_MINIMUM (DM_PERCENT_1 * 50)
37
38 #define MAX_FAILS (256) /* ~42 mins between cmd call retry with 10s delay */
39
40 #define VDO_DEBUG 0
41
42 struct dso_state {
43 struct dm_pool *mem;
44 int percent_check;
45 int percent;
46 uint64_t known_data_size;
47 unsigned fails;
48 unsigned max_fails;
49 int restore_sigset;
50 sigset_t old_sigset;
51 pid_t pid;
52 char *argv[3];
53 const char *cmd_str;
54 const char *name;
55 };
56
57 DM_EVENT_LOG_FN("vdo")
58
59 static int _run_command(struct dso_state *state)
60 {
61 char val[16];
62 int i;
63
64 /* Mark for possible lvm2 command we are running from dmeventd
65 * lvm2 will not try to talk back to dmeventd while processing it */
66 (void) setenv("LVM_RUN_BY_DMEVENTD", "1", 1);
67
68 if (state->percent) {
69 /* Prepare some known data to env vars for easy use */
70 if (dm_snprintf(val, sizeof(val), "%d",
71 state->percent / DM_PERCENT_1) != -1)
72 (void) setenv("DMEVENTD_VDO_POOL", val, 1);
73 } else {
74 /* For an error event it's for a user to check status and decide */
75 log_debug("Error event processing.");
76 }
77
78 log_verbose("Executing command: %s", state->cmd_str);
79
80 /* TODO:
81 * Support parallel run of 'task' and it's waitpid maintenance
82 * ATM we can't handle signaling of SIGALRM
83 * as signalling is not allowed while 'process_event()' is running
84 */
85 if (!(state->pid = fork())) {
86 /* child */
87 (void) close(0);
88 for (i = 3; i < 255; ++i) (void) close(i);
89 execvp(state->argv[0], state->argv);
90 _exit(errno);
91 } else if (state->pid == -1) {
92 log_error("Can't fork command %s.", state->cmd_str);
93 state->fails = 1;
94 return 0;
95 }
96
97 return 1;
98 }
99
100 static int _use_policy(struct dm_task *dmt, struct dso_state *state)
101 {
102 #if VDO_DEBUG
103 log_debug("dmeventd executes: %s.", state->cmd_str);
104 #endif
105 if (state->argv[0])
106 return _run_command(state);
107
108 if (!dmeventd_lvm2_run_with_lock(state->cmd_str)) {
109 log_error("Failed command for %s.", dm_task_get_name(dmt));
110 state->fails = 1;
111 return 0;
112 }
113
114 state->fails = 0;
115
116 return 1;
117 }
118
119 /* Check if executed command has finished
120 * Only 1 command may run */
121 static int _wait_for_pid(struct dso_state *state)
122 {
123 int status = 0;
124
125 if (state->pid == -1)
126 return 1;
127
128 if (!waitpid(state->pid, &status, WNOHANG))
129 return 0;
130
131 /* Wait for finish */
132 if (WIFEXITED(status)) {
133 log_verbose("Child %d exited with status %d.",
134 state->pid, WEXITSTATUS(status));
135 state->fails = WEXITSTATUS(status) ? 1 : 0;
136 } else {
137 if (WIFSIGNALED(status))
138 log_verbose("Child %d was terminated with status %d.",
139 state->pid, WTERMSIG(status));
140 state->fails = 1;
141 }
142
143 state->pid = -1;
144
145 return 1;
146 }
147
148 void process_event(struct dm_task *dmt,
149 enum dm_event_mask evmask __attribute__((unused)),
150 void **user)
151 {
152 const char *device = dm_task_get_name(dmt);
153 struct dso_state *state = *user;
154 void *next = NULL;
155 uint64_t start, length;
156 char *target_type = NULL;
157 char *params;
158 int needs_policy = 0;
159 struct dm_task *new_dmt = NULL;
160 struct dm_vdo_status_parse_result vdop = { .status = NULL };
161
162 #if VDO_DEBUG
163 log_debug("Watch for VDO %s:%.2f%%.", state->name,
164 dm_percent_to_round_float(state->percent_check, 2));
165 #endif
166 if (!_wait_for_pid(state)) {
167 log_warn("WARNING: Skipping event, child %d is still running (%s).",
168 state->pid, state->cmd_str);
169 return;
170 }
171
172 if (evmask & DM_EVENT_DEVICE_ERROR) {
173 #if VDO_DEBUG
174 log_debug("VDO event error.");
175 #endif
176 /* Error -> no need to check and do instant resize */
177 state->percent = 0;
178 if (_use_policy(dmt, state))
179 goto out;
180
181 stack;
182
183 if (!(new_dmt = dm_task_create(DM_DEVICE_STATUS)))
184 goto_out;
185
186 if (!dm_task_set_uuid(new_dmt, dm_task_get_uuid(dmt)))
187 goto_out;
188
189 /* Non-blocking status read */
190 if (!dm_task_no_flush(new_dmt))
191 log_warn("WARNING: Can't set no_flush for dm status.");
192
193 if (!dm_task_run(new_dmt))
194 goto_out;
195
196 dmt = new_dmt;
197 }
198
199 dm_get_next_target(dmt, next, &start, &length, &target_type, &params);
200
201 if (!target_type || (strcmp(target_type, "vdo") != 0)) {
202 log_error("Invalid target type.");
203 goto out;
204 }
205
206 if (!dm_vdo_status_parse(state->mem, params, &vdop)) {
207 log_error("Failed to parse status.");
208 goto out;
209 }
210
211 state->percent = dm_make_percent(vdop.status->used_blocks,
212 vdop.status->total_blocks);
213
214 #if VDO_DEBUG
215 log_debug("VDO %s status %.2f%% " FMTu64 "/" FMTu64 ".",
216 state->name, dm_percent_to_round_float(state->percent, 2),
217 vdop.status->used_blocks, vdop.status->total_blocks);
218 #endif
219
220 /* VDO pool size had changed. Clear the threshold. */
221 if (state->known_data_size != vdop.status->total_blocks) {
222 state->percent_check = CHECK_MINIMUM;
223 state->known_data_size = vdop.status->total_blocks;
224 state->fails = 0;
225 }
226
227 /*
228 * Trigger action when threshold boundary is exceeded.
229 * Report 80% threshold warning when it's used above 80%.
230 * Only 100% is exception as it cannot be surpassed so policy
231 * action is called for: >50%, >55% ... >95%, 100%
232 */
233 if ((state->percent > WARNING_THRESH) &&
234 (state->percent > state->percent_check))
235 log_warn("WARNING: VDO %s %s is now %.2f%% full.",
236 state->name, device,
237 dm_percent_to_round_float(state->percent, 2));
238 if (state->percent > CHECK_MINIMUM) {
239 /* Run action when usage raised more than CHECK_STEP since the last time */
240 if (state->percent > state->percent_check)
241 needs_policy = 1;
242 state->percent_check = (state->percent / CHECK_STEP + 1) * CHECK_STEP;
243 if (state->percent_check == DM_PERCENT_100)
244 state->percent_check--; /* Can't get bigger then 100% */
245 } else
246 state->percent_check = CHECK_MINIMUM;
247
248 /* Reduce number of _use_policy() calls by power-of-2 factor till frequency of MAX_FAILS is reached.
249 * Avoids too high number of error retries, yet shows some status messages in log regularly.
250 * i.e. PV could have been pvmoved and VG/LV was locked for a while...
251 */
252 if (state->fails) {
253 if (state->fails++ <= state->max_fails) {
254 log_debug("Postponing frequently failing policy (%u <= %u).",
255 state->fails - 1, state->max_fails);
256 goto out;
257 }
258 if (state->max_fails < MAX_FAILS)
259 state->max_fails <<= 1;
260 state->fails = needs_policy = 1; /* Retry failing command */
261 } else
262 state->max_fails = 1; /* Reset on success */
263
264 if (needs_policy)
265 _use_policy(dmt, state);
266 out:
267 if (vdop.status)
268 dm_pool_free(state->mem, vdop.status);
269
270 if (new_dmt)
271 dm_task_destroy(new_dmt);
272 }
273
274 /* Handle SIGCHLD for a thread */
275 static void _sig_child(int signum __attribute__((unused)))
276 {
277 /* empty SIG_IGN */;
278 }
279
280 /* Setup handler for SIGCHLD when executing external command
281 * to get quick 'waitpid()' reaction
282 * It will interrupt syscall just like SIGALRM and
283 * invoke process_event().
284 */
285 static void _init_thread_signals(struct dso_state *state)
286 {
287 struct sigaction act = { .sa_handler = _sig_child };
288 sigset_t my_sigset;
289
290 sigemptyset(&my_sigset);
291
292 if (sigaction(SIGCHLD, &act, NULL))
293 log_warn("WARNING: Failed to set SIGCHLD action.");
294 else if (sigaddset(&my_sigset, SIGCHLD))
295 log_warn("WARNING: Failed to add SIGCHLD to set.");
296 else if (pthread_sigmask(SIG_UNBLOCK, &my_sigset, &state->old_sigset))
297 log_warn("WARNING: Failed to unblock SIGCHLD.");
298 else
299 state->restore_sigset = 1;
300 }
301
302 static void _restore_thread_signals(struct dso_state *state)
303 {
304 if (state->restore_sigset &&
305 pthread_sigmask(SIG_SETMASK, &state->old_sigset, NULL))
306 log_warn("WARNING: Failed to block SIGCHLD.");
307 }
308
309 int register_device(const char *device,
310 const char *uuid,
311 int major __attribute__((unused)),
312 int minor __attribute__((unused)),
313 void **user)
314 {
315 struct dso_state *state;
316 const char *cmd;
317 char *str;
318 char cmd_str[PATH_MAX + 128 + 2]; /* cmd ' ' vg/lv \0 */
319 const char *name = "pool";
320
321 if (!dmeventd_lvm2_init_with_pool("vdo_pool_state", state))
322 goto_bad;
323
324 state->cmd_str = "";
325
326 /* Search for command for LVM- prefixed devices only */
327 cmd = (strncmp(uuid, "LVM-", 4) == 0) ? "_dmeventd_vdo_command" : "";
328
329 if (!dmeventd_lvm2_command(state->mem, cmd_str, sizeof(cmd_str), cmd, device))
330 goto_bad;
331
332 if (strncmp(cmd_str, "lvm ", 4) == 0) {
333 if (!(state->cmd_str = dm_pool_strdup(state->mem, cmd_str + 4))) {
334 log_error("Failed to copy lvm VDO command.");
335 goto bad;
336 }
337 } else if (cmd_str[0] == '/') {
338 if (!(state->cmd_str = dm_pool_strdup(state->mem, cmd_str))) {
339 log_error("Failed to copy VDO command.");
340 goto bad;
341 }
342
343 /* Find last space before 'vg/lv' */
344 if (!(str = strrchr(state->cmd_str, ' ')))
345 goto inval;
346
347 if (!(state->argv[0] = dm_pool_strndup(state->mem, state->cmd_str,
348 str - state->cmd_str))) {
349 log_error("Failed to copy command.");
350 goto bad;
351 }
352
353 state->argv[1] = str + 1; /* 1 argument - vg/lv */
354 _init_thread_signals(state);
355 } else if (cmd[0] == 0) {
356 state->name = "volume"; /* What to use with 'others?' */
357 } else/* Unsupported command format */
358 goto inval;
359
360 state->pid = -1;
361 state->name = name;
362 *user = state;
363
364 log_info("Monitoring VDO %s %s.", name, device);
365
366 return 1;
367 inval:
368 log_error("Invalid command for monitoring: %s.", cmd_str);
369 bad:
370 log_error("Failed to monitor VDO %s %s.", name, device);
371
372 if (state)
373 dmeventd_lvm2_exit_with_pool(state);
374
375 return 0;
376 }
377
378 int unregister_device(const char *device,
379 const char *uuid __attribute__((unused)),
380 int major __attribute__((unused)),
381 int minor __attribute__((unused)),
382 void **user)
383 {
384 struct dso_state *state = *user;
385 const char *name = state->name;
386 int i;
387
388 for (i = 0; !_wait_for_pid(state) && (i < 6); ++i) {
389 if (i == 0)
390 /* Give it 2 seconds, then try to terminate & kill it */
391 log_verbose("Child %d still not finished (%s) waiting.",
392 state->pid, state->cmd_str);
393 else if (i == 3) {
394 log_warn("WARNING: Terminating child %d.", state->pid);
395 kill(state->pid, SIGINT);
396 kill(state->pid, SIGTERM);
397 } else if (i == 5) {
398 log_warn("WARNING: Killing child %d.", state->pid);
399 kill(state->pid, SIGKILL);
400 }
401 sleep(1);
402 }
403
404 if (state->pid != -1)
405 log_warn("WARNING: Cannot kill child %d!", state->pid);
406
407 _restore_thread_signals(state);
408
409 dmeventd_lvm2_exit_with_pool(state);
410 log_info("No longer monitoring VDO %s %s.", name, device);
411
412 return 1;
413 }