| 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/config/defaults.h" |
| 19 |
|
| 20 |
/* Hold enough elements for the maximum number of RAID images */ |
| 21 |
#define RAID_DEVS_ELEMS ((DEFAULT_RAID_MAX_IMAGES + 63) / 64) |
| 22 |
|
| 23 |
struct dso_state { |
| 24 |
struct dm_pool *mem; |
| 25 |
char cmd_lvconvert[512]; |
| 26 |
uint64_t raid_devs[RAID_DEVS_ELEMS]; |
| 27 |
int failed; |
| 28 |
int warned; |
| 29 |
}; |
| 30 |
|
| 31 |
DM_EVENT_LOG_FN("raid") |
| 32 |
|
| 33 |
/* FIXME Reformat to 80 char lines. */ |
| 34 |
|
| 35 |
static int _process_raid_event(struct dso_state *state, char *params, const char *device) |
| 36 |
{ |
| 37 |
struct dm_status_raid *status; |
| 38 |
const char *d; |
| 39 |
int dead = 0, r = 1; |
| 40 |
uint32_t dev; |
| 41 |
|
| 42 |
if (!dm_get_status_raid(state->mem, params, &status)) { |
| 43 |
log_error("Failed to process status line for %s.", device); |
| 44 |
return 0; |
| 45 |
} |
| 46 |
|
| 47 |
d = status->dev_health; |
| 48 |
while ((d = strchr(d, 'D'))) { |
| 49 |
dev = (uint32_t)(d - status->dev_health); |
| 50 |
|
| 51 |
if (!(state->raid_devs[dev / 64] & (UINT64_C(1) << (dev % 64)))) { |
| 52 |
state->raid_devs[dev / 64] |= (UINT64_C(1) << (dev % 64)); |
| 53 |
log_warn("WARNING: Device #%u of %s array, %s, has failed.", |
| 54 |
dev, status->raid_type, device); |
| 55 |
} |
| 56 |
|
| 57 |
d++; |
| 58 |
dead = 1; |
| 59 |
} |
| 60 |
|
| 61 |
/* |
| 62 |
* if we are converting from non-RAID to RAID (e.g. linear -> raid1) |
| 63 |
* and too many original devices die, such that we cannot continue |
| 64 |
* the "recover" operation, the sync action will go to "idle", the |
| 65 |
* unsynced devs will remain at 'a', and the original devices will |
| 66 |
* NOT SWITCH TO 'D', but will remain at 'A' - hoping to be revived. |
| 67 |
* |
| 68 |
* This is simply the way the kernel works... |
| 69 |
*/ |
| 70 |
if (!strcmp(status->sync_action, "idle") && |
| 71 |
(status->dev_health[0] == 'a') && |
| 72 |
(status->insync_regions < status->total_regions)) { |
| 73 |
log_error("Primary sources for new RAID, %s, have failed.", |
| 74 |
device); |
| 75 |
dead = 1; /* run it through LVM repair */ |
| 76 |
} |
| 77 |
|
| 78 |
if (dead) { |
| 79 |
/* |
| 80 |
* Use the first event to run a repair ignoring any additional ones. |
| 81 |
* |
| 82 |
* We presume lvconvert to do pre-repair |
| 83 |
* checks to avoid bloat in this plugin. |
| 84 |
*/ |
| 85 |
if (!state->warned && status->insync_regions < status->total_regions) { |
| 86 |
state->warned = 1; |
| 87 |
log_warn("WARNING: waiting for resynchronization to finish " |
| 88 |
"before initiating repair on RAID device %s.", device); |
| 89 |
/* Fall through to allow lvconvert to run. */ |
| 90 |
} |
| 91 |
|
| 92 |
if (state->failed) |
| 93 |
goto out; /* already reported */ |
| 94 |
|
| 95 |
state->failed = 1; |
| 96 |
|
| 97 |
/* if repair goes OK, report success even if lvscan has failed */ |
| 98 |
if (!dmeventd_lvm2_run_with_lock(state->cmd_lvconvert)) { |
| 99 |
log_error("Repair of RAID device %s failed.", device); |
| 100 |
r = 0; |
| 101 |
} |
| 102 |
} else { |
| 103 |
state->failed = 0; |
| 104 |
if (status->insync_regions == status->total_regions) |
| 105 |
memset(&state->raid_devs, 0, sizeof(state->raid_devs)); |
| 106 |
log_info("%s array, %s, is %s in-sync.", |
| 107 |
status->raid_type, device, |
| 108 |
(status->insync_regions == status->total_regions) ? "now" : "not"); |
| 109 |
} |
| 110 |
out: |
| 111 |
dm_pool_free(state->mem, status); |
| 112 |
|
| 113 |
return r; |
| 114 |
} |
| 115 |
|
| 116 |
void process_event(struct dm_task *dmt, |
| 117 |
enum dm_event_mask evmask __attribute__((unused)), |
| 118 |
void **user) |
| 119 |
{ |
| 120 |
struct dso_state *state = *user; |
| 121 |
void *next = NULL; |
| 122 |
uint64_t start, length; |
| 123 |
char *target_type = NULL; |
| 124 |
char *params; |
| 125 |
const char *device = dm_task_get_name(dmt); |
| 126 |
|
| 127 |
do { |
| 128 |
next = dm_get_next_target(dmt, next, &start, &length, |
| 129 |
&target_type, ¶ms); |
| 130 |
|
| 131 |
if (!target_type) { |
| 132 |
log_info("%s mapping lost.", device); |
| 133 |
continue; |
| 134 |
} |
| 135 |
|
| 136 |
if (strcmp(target_type, "raid")) { |
| 137 |
log_info("%s has non-raid portion.", device); |
| 138 |
continue; |
| 139 |
} |
| 140 |
|
| 141 |
if (!_process_raid_event(state, params, device)) |
| 142 |
log_error("Failed to process event for %s.", |
| 143 |
device); |
| 144 |
} while (next); |
| 145 |
} |
| 146 |
|
| 147 |
int register_device(const char *device, |
| 148 |
const char *uuid __attribute__((unused)), |
| 149 |
int major __attribute__((unused)), |
| 150 |
int minor __attribute__((unused)), |
| 151 |
void **user) |
| 152 |
{ |
| 153 |
struct dso_state *state; |
| 154 |
|
| 155 |
if (!dmeventd_lvm2_init_with_pool("raid_state", state)) |
| 156 |
goto_bad; |
| 157 |
|
| 158 |
if (!dmeventd_lvm2_command(state->mem, state->cmd_lvconvert, sizeof(state->cmd_lvconvert), |
| 159 |
"lvconvert --repair --use-policies", device)) |
| 160 |
goto_bad; |
| 161 |
|
| 162 |
*user = state; |
| 163 |
|
| 164 |
log_info("Monitoring RAID device %s for events.", device); |
| 165 |
|
| 166 |
return 1; |
| 167 |
bad: |
| 168 |
log_error("Failed to monitor RAID %s.", device); |
| 169 |
|
| 170 |
if (state) |
| 171 |
dmeventd_lvm2_exit_with_pool(state); |
| 172 |
|
| 173 |
return 0; |
| 174 |
} |
| 175 |
|
| 176 |
int unregister_device(const char *device, |
| 177 |
const char *uuid __attribute__((unused)), |
| 178 |
int major __attribute__((unused)), |
| 179 |
int minor __attribute__((unused)), |
| 180 |
void **user) |
| 181 |
{ |
| 182 |
struct dso_state *state = *user; |
| 183 |
|
| 184 |
dmeventd_lvm2_exit_with_pool(state); |
| 185 |
log_info("No longer monitoring RAID device %s for events.", |
| 186 |
device); |
| 187 |
|
| 188 |
return 1; |
| 189 |
} |