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