]>
Commit | Line | Data |
---|---|---|
52826846 | 1 | /* |
9a9dab36 | 2 | * mdadm - manage Linux "md" devices aka RAID arrays. |
52826846 | 3 | * |
e736b623 | 4 | * Copyright (C) 2001-2009 Neil Brown <neilb@suse.de> |
52826846 NB |
5 | * |
6 | * | |
7 | * This program is free software; you can redistribute it and/or modify | |
8 | * it under the terms of the GNU General Public License as published by | |
9 | * the Free Software Foundation; either version 2 of the License, or | |
10 | * (at your option) any later version. | |
11 | * | |
12 | * This program is distributed in the hope that it will be useful, | |
13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
15 | * GNU General Public License for more details. | |
16 | * | |
17 | * You should have received a copy of the GNU General Public License | |
18 | * along with this program; if not, write to the Free Software | |
19 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA | |
20 | * | |
21 | * Author: Neil Brown | |
e736b623 | 22 | * Email: <neilb@suse.de> |
52826846 NB |
23 | */ |
24 | ||
9a9dab36 | 25 | #include "mdadm.h" |
52826846 NB |
26 | #include "md_p.h" |
27 | #include "md_u.h" | |
e0d19036 | 28 | #include <sys/wait.h> |
4450e59f | 29 | #include <limits.h> |
773135f5 | 30 | #include <syslog.h> |
af3396da | 31 | #ifndef NO_LIBUDEV |
49b69533 | 32 | #include <libudev.h> |
af3396da | 33 | #endif |
52826846 | 34 | |
2e0172b1 N |
35 | struct state { |
36 | char *devname; | |
4dd2df09 | 37 | char devnm[32]; /* to sync with mdstat info */ |
1d13b599 | 38 | unsigned int utime; |
2e0172b1 N |
39 | int err; |
40 | char *spare_group; | |
41 | int active, working, failed, spare, raid; | |
f1661bd7 | 42 | int from_config; |
73ff0732 | 43 | int from_auto; |
2e0172b1 | 44 | int expected_spares; |
b0599bda N |
45 | int devstate[MAX_DISKS]; |
46 | dev_t devid[MAX_DISKS]; | |
2e0172b1 | 47 | int percent; |
4dd2df09 N |
48 | char parent_devnm[32]; /* For subarray, devnm of parent. |
49 | * For others, "" | |
50 | */ | |
2e0172b1 | 51 | struct supertype *metadata; |
c3621c0a ML |
52 | struct state *subarray;/* for a container it is a link to first subarray |
53 | * for a subarray it is a link to next subarray | |
54 | * in the same container */ | |
55 | struct state *parent; /* for a subarray it is a link to its container | |
56 | */ | |
2e0172b1 N |
57 | struct state *next; |
58 | }; | |
59 | ||
e0bd6a96 N |
60 | struct alert_info { |
61 | char *mailaddr; | |
62 | char *mailfrom; | |
63 | char *alert_cmd; | |
64 | int dosyslog; | |
65 | }; | |
2e0172b1 N |
66 | static int make_daemon(char *pidfile); |
67 | static int check_one_sharer(int scan); | |
7f3b2d1d | 68 | static void write_autorebuild_pid(void); |
e0bd6a96 | 69 | static void alert(char *event, char *dev, char *disc, struct alert_info *info); |
a90e1050 | 70 | static int check_array(struct state *st, struct mdstat_ent *mdstat, |
e0bd6a96 | 71 | int test, struct alert_info *info, |
c2ecf5f6 | 72 | int increments, char *prefer); |
83f3bc5f | 73 | static int add_new_arrays(struct mdstat_ent *mdstat, struct state **statelist, |
e0bd6a96 N |
74 | int test, struct alert_info *info); |
75 | static void try_spare_migration(struct state *statelist, struct alert_info *info); | |
c3621c0a | 76 | static void link_containers_with_subarrays(struct state *list); |
af3396da | 77 | #ifndef NO_LIBUDEV |
49b69533 | 78 | static int check_udev_activity(void); |
af3396da | 79 | #endif |
2e0172b1 | 80 | |
a655e550 | 81 | int Monitor(struct mddev_dev *devlist, |
52826846 | 82 | char *mailaddr, char *alert_cmd, |
95c50205 N |
83 | struct context *c, |
84 | int daemonise, int oneshot, | |
85 | int dosyslog, char *pidfile, int increments, | |
86 | int share) | |
52826846 NB |
87 | { |
88 | /* | |
89 | * Every few seconds, scan every md device looking for changes | |
90 | * When a change is found, log it, possibly run the alert command, | |
91 | * and possibly send Email | |
92 | * | |
93 | * For each array, we record: | |
94 | * Update time | |
95 | * active/working/failed/spare drives | |
96 | * State of each device. | |
e0d19036 | 97 | * %rebuilt if rebuilding |
52826846 NB |
98 | * |
99 | * If the update time changes, check out all the data again | |
100 | * It is possible that we cannot get the state of each device | |
101 | * due to bugs in the md kernel module. | |
e0d19036 NB |
102 | * We also read /proc/mdstat to get rebuild percent, |
103 | * and to get state on all active devices incase of kernel bug. | |
52826846 | 104 | * |
e0d19036 NB |
105 | * Events are: |
106 | * Fail | |
107 | * An active device had Faulty set or Active/Sync removed | |
108 | * FailSpare | |
109 | * A spare device had Faulty set | |
110 | * SpareActive | |
111 | * An active device had a reverse transition | |
112 | * RebuildStarted | |
113 | * percent went from -1 to +ve | |
9a36a9b7 ZB |
114 | * RebuildNN |
115 | * percent went from below to not-below NN% | |
e0d19036 NB |
116 | * DeviceDisappeared |
117 | * Couldn't access a device which was previously visible | |
52826846 NB |
118 | * |
119 | * if we detect an array with active<raid and spare==0 | |
120 | * we look at other arrays that have same spare-group | |
121 | * If we find one with active==raid and spare>0, | |
122 | * and if we can get_disk_info and find a name | |
123 | * Then we hot-remove and hot-add to the other array | |
124 | * | |
e0d19036 NB |
125 | * If devlist is NULL, then we can monitor everything because --scan |
126 | * was given. We get an initial list from config file and add anything | |
127 | * that appears in /proc/mdstat | |
52826846 NB |
128 | */ |
129 | ||
2e0172b1 | 130 | struct state *statelist = NULL; |
b657208c | 131 | struct state *st2; |
52826846 | 132 | int finished = 0; |
e0d19036 | 133 | struct mdstat_ent *mdstat = NULL; |
72362f18 | 134 | char *mailfrom; |
e0bd6a96 | 135 | struct alert_info info; |
9f3dd454 | 136 | struct mddev_ident *mdlist; |
af3396da | 137 | int delay_for_event = c->delay; |
e0d19036 | 138 | |
d013a55e | 139 | if (!mailaddr) { |
8aec876d | 140 | mailaddr = conf_get_mailaddr(); |
95c50205 | 141 | if (mailaddr && ! c->scan) |
e7b84f9d | 142 | pr_err("Monitor using email address \"%s\" from config file\n", |
d013a55e NB |
143 | mailaddr); |
144 | } | |
8aec876d | 145 | mailfrom = conf_get_mailfrom(); |
4948b8f7 | 146 | |
d013a55e | 147 | if (!alert_cmd) { |
8aec876d | 148 | alert_cmd = conf_get_program(); |
f566ef45 | 149 | if (alert_cmd && !c->scan) |
e7b84f9d | 150 | pr_err("Monitor using program \"%s\" from config file\n", |
d013a55e NB |
151 | alert_cmd); |
152 | } | |
95c50205 | 153 | if (c->scan && !mailaddr && !alert_cmd && !dosyslog) { |
e7b84f9d | 154 | pr_err("No mail address or alert command - not monitoring.\n"); |
d013a55e | 155 | return 1; |
56eedc1a | 156 | } |
e0bd6a96 N |
157 | info.alert_cmd = alert_cmd; |
158 | info.mailaddr = mailaddr; | |
159 | info.mailfrom = mailfrom; | |
160 | info.dosyslog = dosyslog; | |
d013a55e | 161 | |
7f3b2d1d BK |
162 | if (share){ |
163 | if (check_one_sharer(c->scan)) | |
164 | return 1; | |
165 | } | |
166 | ||
44d337f0 AC |
167 | if (daemonise) { |
168 | int rv = make_daemon(pidfile); | |
169 | if (rv >= 0) | |
170 | return rv; | |
171 | } | |
e0d19036 | 172 | |
1011e834 | 173 | if (share) |
7f3b2d1d | 174 | write_autorebuild_pid(); |
edde9560 | 175 | |
e0d19036 | 176 | if (devlist == NULL) { |
9f3dd454 | 177 | mdlist = conf_get_ident(NULL); |
f566ef45 | 178 | for (; mdlist; mdlist = mdlist->next) { |
fe056d1f | 179 | struct state *st; |
f566ef45 | 180 | |
fe056d1f N |
181 | if (mdlist->devname == NULL) |
182 | continue; | |
112cace6 N |
183 | if (strcasecmp(mdlist->devname, "<ignore>") == 0) |
184 | continue; | |
e702f392 | 185 | |
503975b9 | 186 | st = xcalloc(1, sizeof *st); |
db2d001c | 187 | if (mdlist->devname[0] == '/') |
503975b9 | 188 | st->devname = xstrdup(mdlist->devname); |
db2d001c | 189 | else { |
503975b9 | 190 | st->devname = xmalloc(8+strlen(mdlist->devname)+1); |
db2d001c N |
191 | strcpy(strcpy(st->devname, "/dev/md/"), |
192 | mdlist->devname); | |
193 | } | |
e702f392 KT |
194 | if (!is_mddev(mdlist->devname)) |
195 | return 1; | |
e0d19036 | 196 | st->next = statelist; |
4dd2df09 | 197 | st->devnm[0] = 0; |
9dad51d4 | 198 | st->percent = RESYNC_UNKNOWN; |
f1661bd7 | 199 | st->from_config = 1; |
feb716e9 | 200 | st->expected_spares = mdlist->spare_disks; |
e0d19036 | 201 | if (mdlist->spare_group) |
503975b9 | 202 | st->spare_group = xstrdup(mdlist->spare_group); |
e0d19036 NB |
203 | statelist = st; |
204 | } | |
205 | } else { | |
a655e550 | 206 | struct mddev_dev *dv; |
f566ef45 JS |
207 | |
208 | for (dv = devlist; dv; dv = dv->next) { | |
e702f392 KT |
209 | struct state *st; |
210 | ||
211 | if (!is_mddev(dv->devname)) | |
212 | return 1; | |
213 | ||
214 | st = xcalloc(1, sizeof *st); | |
9f3dd454 | 215 | mdlist = conf_get_ident(dv->devname); |
503975b9 | 216 | st->devname = xstrdup(dv->devname); |
e0d19036 | 217 | st->next = statelist; |
4dd2df09 | 218 | st->devnm[0] = 0; |
9dad51d4 | 219 | st->percent = RESYNC_UNKNOWN; |
feb716e9 | 220 | st->expected_spares = -1; |
e5329c37 NB |
221 | if (mdlist) { |
222 | st->expected_spares = mdlist->spare_disks; | |
223 | if (mdlist->spare_group) | |
503975b9 | 224 | st->spare_group = xstrdup(mdlist->spare_group); |
e5329c37 | 225 | } |
e0d19036 NB |
226 | statelist = st; |
227 | } | |
228 | } | |
229 | ||
f566ef45 | 230 | while (!finished) { |
aa88f531 | 231 | int new_found = 0; |
73ff0732 | 232 | struct state *st, **stp; |
a90e1050 | 233 | int anydegraded = 0; |
007087d0 | 234 | int anyredundant = 0; |
e0d19036 NB |
235 | |
236 | if (mdstat) | |
237 | free_mdstat(mdstat); | |
f566ef45 | 238 | mdstat = mdstat_read(oneshot ? 0 : 1, 0); |
e0d19036 | 239 | |
007087d0 | 240 | for (st = statelist; st; st = st->next) { |
95c50205 N |
241 | if (check_array(st, mdstat, c->test, &info, |
242 | increments, c->prefer)) | |
a90e1050 | 243 | anydegraded = 1; |
007087d0 MT |
244 | /* for external arrays, metadata is filled for |
245 | * containers only | |
246 | */ | |
247 | if (st->metadata && st->metadata->ss->external) | |
248 | continue; | |
249 | if (st->err == 0 && !anyredundant) | |
250 | anyredundant = 1; | |
251 | } | |
1011e834 | 252 | |
e0d19036 | 253 | /* now check if there are any new devices found in mdstat */ |
95c50205 N |
254 | if (c->scan) |
255 | new_found = add_new_arrays(mdstat, &statelist, c->test, | |
e0bd6a96 | 256 | &info); |
2e0172b1 | 257 | |
e0d19036 NB |
258 | /* If an array has active < raid && spare == 0 && spare_group != NULL |
259 | * Look for another array with spare > 0 and active == raid and same spare_group | |
49b69533 | 260 | * if found, choose a device and hotremove/hotadd |
e0d19036 | 261 | */ |
a90e1050 | 262 | if (share && anydegraded) |
e0bd6a96 | 263 | try_spare_migration(statelist, &info); |
aa88f531 NB |
264 | if (!new_found) { |
265 | if (oneshot) | |
266 | break; | |
007087d0 | 267 | else if (!anyredundant) { |
8c80d305 | 268 | pr_err("No array with redundancy detected, stopping\n"); |
007087d0 MT |
269 | break; |
270 | } | |
e2308733 | 271 | else { |
af3396da | 272 | #ifndef NO_LIBUDEV |
cab9c67d | 273 | /* |
af3396da MT |
274 | * Wait for udevd to finish new devices |
275 | * processing. | |
cab9c67d | 276 | */ |
af3396da MT |
277 | if (mdstat_wait(delay_for_event) && |
278 | check_udev_activity()) | |
49b69533 | 279 | pr_err("Error while waiting for UDEV to complete new devices processing\n"); |
af3396da MT |
280 | #else |
281 | int wait_result = mdstat_wait(delay_for_event); | |
282 | /* | |
283 | * Give chance to process new device | |
284 | */ | |
285 | if (wait_result != 0) { | |
286 | if (c->delay > 5) | |
287 | delay_for_event = 5; | |
288 | } else | |
289 | delay_for_event = c->delay; | |
290 | #endif | |
e2308733 MT |
291 | mdstat_close(); |
292 | } | |
aa88f531 | 293 | } |
95c50205 | 294 | c->test = 0; |
73ff0732 N |
295 | |
296 | for (stp = &statelist; (st = *stp) != NULL; ) { | |
297 | if (st->from_auto && st->err > 5) { | |
298 | *stp = st->next; | |
299 | free(st->devname); | |
300 | free(st->spare_group); | |
301 | free(st); | |
302 | } else | |
303 | stp = &st->next; | |
304 | } | |
52826846 | 305 | } |
b657208c JS |
306 | for (st2 = statelist; st2; st2 = statelist) { |
307 | statelist = st2->next; | |
308 | free(st2); | |
309 | } | |
310 | ||
b5e64645 NB |
311 | if (pidfile) |
312 | unlink(pidfile); | |
52826846 NB |
313 | return 0; |
314 | } | |
315 | ||
2e0172b1 N |
316 | static int make_daemon(char *pidfile) |
317 | { | |
44d337f0 AC |
318 | /* Return: |
319 | * -1 in the forked daemon | |
320 | * 0 in the parent | |
321 | * 1 on error | |
322 | * so a none-negative becomes the exit code. | |
323 | */ | |
2e0172b1 N |
324 | int pid = fork(); |
325 | if (pid > 0) { | |
326 | if (!pidfile) | |
327 | printf("%d\n", pid); | |
328 | else { | |
ca4b156b MT |
329 | FILE *pid_file = NULL; |
330 | int fd = open(pidfile, O_WRONLY | O_CREAT | O_TRUNC, | |
331 | 0644); | |
332 | if (fd >= 0) | |
333 | pid_file = fdopen(fd, "w"); | |
2e0172b1 N |
334 | if (!pid_file) |
335 | perror("cannot create pid file"); | |
336 | else { | |
337 | fprintf(pid_file,"%d\n", pid); | |
338 | fclose(pid_file); | |
339 | } | |
340 | } | |
341 | return 0; | |
342 | } | |
343 | if (pid < 0) { | |
344 | perror("daemonise"); | |
345 | return 1; | |
346 | } | |
ff6bb131 | 347 | manage_fork_fds(0); |
2e0172b1 | 348 | setsid(); |
44d337f0 | 349 | return -1; |
2e0172b1 N |
350 | } |
351 | ||
352 | static int check_one_sharer(int scan) | |
353 | { | |
185ec439 CL |
354 | int pid; |
355 | FILE *comm_fp; | |
2e0172b1 | 356 | FILE *fp; |
7f3b2d1d BK |
357 | char comm_path[PATH_MAX]; |
358 | char path[PATH_MAX]; | |
185ec439 CL |
359 | char comm[20]; |
360 | ||
0011874f JS |
361 | sprintf(path, "%s/autorebuild.pid", MDMON_DIR); |
362 | fp = fopen(path, "r"); | |
2e0172b1 | 363 | if (fp) { |
71204a50 N |
364 | if (fscanf(fp, "%d", &pid) != 1) |
365 | pid = -1; | |
185ec439 CL |
366 | snprintf(comm_path, sizeof(comm_path), |
367 | "/proc/%d/comm", pid); | |
368 | comm_fp = fopen(comm_path, "r"); | |
369 | if (comm_fp) { | |
1c66260d | 370 | if (fscanf(comm_fp, "%19s", comm) && |
185ec439 CL |
371 | strncmp(basename(comm), Name, strlen(Name)) == 0) { |
372 | if (scan) { | |
373 | pr_err("Only one autorebuild process allowed in scan mode, aborting\n"); | |
374 | fclose(comm_fp); | |
375 | fclose(fp); | |
376 | return 1; | |
377 | } else { | |
378 | pr_err("Warning: One autorebuild process already running.\n"); | |
379 | } | |
2e0172b1 | 380 | } |
185ec439 | 381 | fclose(comm_fp); |
2e0172b1 N |
382 | } |
383 | fclose(fp); | |
384 | } | |
7f3b2d1d BK |
385 | return 0; |
386 | } | |
387 | ||
388 | static void write_autorebuild_pid() | |
389 | { | |
390 | char path[PATH_MAX]; | |
391 | int pid; | |
ca4b156b | 392 | FILE *fp = NULL; |
7f3b2d1d BK |
393 | sprintf(path, "%s/autorebuild.pid", MDMON_DIR); |
394 | ||
ca4b156b | 395 | if (mkdir(MDMON_DIR, 0700) < 0 && errno != EEXIST) { |
7f3b2d1d BK |
396 | pr_err("Can't create autorebuild.pid file\n"); |
397 | } else { | |
ca4b156b MT |
398 | int fd = open(path, O_WRONLY | O_CREAT | O_TRUNC, 0700); |
399 | ||
400 | if (fd >= 0) | |
401 | fp = fdopen(fd, "w"); | |
402 | ||
7f3b2d1d | 403 | if (!fp) |
7a862a02 | 404 | pr_err("Can't create autorebuild.pid file\n"); |
7f3b2d1d BK |
405 | else { |
406 | pid = getpid(); | |
407 | fprintf(fp, "%d\n", pid); | |
408 | fclose(fp); | |
2e0172b1 N |
409 | } |
410 | } | |
2e0172b1 | 411 | } |
52826846 | 412 | |
e0bd6a96 | 413 | static void alert(char *event, char *dev, char *disc, struct alert_info *info) |
52826846 | 414 | { |
773135f5 NB |
415 | int priority; |
416 | ||
b1bac75b | 417 | if (!info->alert_cmd && !info->mailaddr && !info->dosyslog) { |
cd29a5c8 | 418 | time_t now = time(0); |
aba69144 | 419 | |
f566ef45 JS |
420 | printf("%1.15s: %s on %s %s\n", ctime(&now) + 4, |
421 | event, dev, disc?disc:"unknown device"); | |
cd29a5c8 | 422 | } |
e0bd6a96 | 423 | if (info->alert_cmd) { |
52826846 NB |
424 | int pid = fork(); |
425 | switch(pid) { | |
426 | default: | |
427 | waitpid(pid, NULL, 0); | |
428 | break; | |
429 | case -1: | |
430 | break; | |
431 | case 0: | |
e0bd6a96 N |
432 | execl(info->alert_cmd, info->alert_cmd, |
433 | event, dev, disc, NULL); | |
52826846 NB |
434 | exit(2); |
435 | } | |
436 | } | |
f566ef45 JS |
437 | if (info->mailaddr && (strncmp(event, "Fail", 4) == 0 || |
438 | strncmp(event, "Test", 4) == 0 || | |
439 | strncmp(event, "Spares", 6) == 0 || | |
440 | strncmp(event, "Degrade", 7) == 0)) { | |
52826846 NB |
441 | FILE *mp = popen(Sendmail, "w"); |
442 | if (mp) { | |
008e1100 | 443 | FILE *mdstat; |
52826846 | 444 | char hname[256]; |
83a379cf | 445 | |
52826846 | 446 | gethostname(hname, sizeof(hname)); |
83a379cf LF |
447 | signal_s(SIGPIPE, SIG_IGN); |
448 | ||
e0bd6a96 N |
449 | if (info->mailfrom) |
450 | fprintf(mp, "From: %s\n", info->mailfrom); | |
4948b8f7 | 451 | else |
f566ef45 JS |
452 | fprintf(mp, "From: %s monitoring <root>\n", |
453 | Name); | |
e0bd6a96 N |
454 | fprintf(mp, "To: %s\n", info->mailaddr); |
455 | fprintf(mp, "Subject: %s event on %s:%s\n\n", | |
456 | event, dev, hname); | |
52826846 | 457 | |
e0bd6a96 | 458 | fprintf(mp, |
7a862a02 | 459 | "This is an automatically generated mail message from %s\n", Name); |
52826846 NB |
460 | fprintf(mp, "running on %s\n\n", hname); |
461 | ||
e0bd6a96 | 462 | fprintf(mp, |
7a862a02 | 463 | "A %s event had been detected on md device %s.\n\n", event, dev); |
52826846 | 464 | |
37dfc3d6 | 465 | if (disc && disc[0] != ' ') |
e0bd6a96 | 466 | fprintf(mp, |
7a862a02 | 467 | "It could be related to component device %s.\n\n", disc); |
37dfc3d6 NB |
468 | if (disc && disc[0] == ' ') |
469 | fprintf(mp, "Extra information:%s.\n\n", disc); | |
52826846 NB |
470 | |
471 | fprintf(mp, "Faithfully yours, etc.\n"); | |
008e1100 NB |
472 | |
473 | mdstat = fopen("/proc/mdstat", "r"); | |
474 | if (mdstat) { | |
475 | char buf[8192]; | |
476 | int n; | |
e0bd6a96 | 477 | fprintf(mp, |
7a862a02 | 478 | "\nP.S. The /proc/mdstat file currently contains the following:\n\n"); |
f566ef45 JS |
479 | while ((n = fread(buf, 1, sizeof(buf), |
480 | mdstat)) > 0) | |
481 | n = fwrite(buf, 1, n, mp); | |
008e1100 NB |
482 | fclose(mdstat); |
483 | } | |
6278fb3a | 484 | pclose(mp); |
52826846 | 485 | } |
52826846 | 486 | } |
773135f5 NB |
487 | |
488 | /* log the event to syslog maybe */ | |
e0bd6a96 | 489 | if (info->dosyslog) { |
773135f5 NB |
490 | /* Log at a different severity depending on the event. |
491 | * | |
492 | * These are the critical events: */ | |
f566ef45 JS |
493 | if (strncmp(event, "Fail", 4) == 0 || |
494 | strncmp(event, "Degrade", 7) == 0 || | |
495 | strncmp(event, "DeviceDisappeared", 17) == 0) | |
773135f5 NB |
496 | priority = LOG_CRIT; |
497 | /* Good to know about, but are not failures: */ | |
f566ef45 JS |
498 | else if (strncmp(event, "Rebuild", 7) == 0 || |
499 | strncmp(event, "MoveSpare", 9) == 0 || | |
d1732eeb | 500 | strncmp(event, "Spares", 6) != 0) |
773135f5 NB |
501 | priority = LOG_WARNING; |
502 | /* Everything else: */ | |
503 | else | |
504 | priority = LOG_INFO; | |
505 | ||
b033913a | 506 | if (disc && disc[0] != ' ') |
e0bd6a96 | 507 | syslog(priority, |
7a862a02 | 508 | "%s event detected on md device %s, component device %s", event, dev, disc); |
b033913a N |
509 | else if (disc) |
510 | syslog(priority, | |
511 | "%s event detected on md device %s: %s", | |
512 | event, dev, disc); | |
773135f5 | 513 | else |
e0bd6a96 N |
514 | syslog(priority, |
515 | "%s event detected on md device %s", | |
516 | event, dev); | |
773135f5 | 517 | } |
52826846 | 518 | } |
b90c0e9a | 519 | |
a90e1050 | 520 | static int check_array(struct state *st, struct mdstat_ent *mdstat, |
e0bd6a96 | 521 | int test, struct alert_info *ainfo, |
c2ecf5f6 | 522 | int increments, char *prefer) |
2e0172b1 | 523 | { |
ff044d6b AC |
524 | /* Update the state 'st' to reflect any changes shown in mdstat, |
525 | * or found by directly examining the array, and return | |
526 | * '1' if the array is degraded, or '0' if it is optimal (or dead). | |
527 | */ | |
b0599bda | 528 | struct { int state, major, minor; } info[MAX_DISKS]; |
aed5f5c3 | 529 | struct mdinfo *sra = NULL; |
2e0172b1 N |
530 | mdu_array_info_t array; |
531 | struct mdstat_ent *mse = NULL, *mse2; | |
532 | char *dev = st->devname; | |
b9a0309c | 533 | int fd; |
2e0172b1 | 534 | int i; |
9e6d9291 N |
535 | int remaining_disks; |
536 | int last_disk; | |
721b662b | 537 | int new_array = 0; |
1830e74b | 538 | int retval; |
802961a2 | 539 | int is_container = 0; |
b3ab4e4d | 540 | unsigned long redundancy_only_flags = 0; |
2e0172b1 N |
541 | |
542 | if (test) | |
e0bd6a96 | 543 | alert("TestMessage", dev, NULL, ainfo); |
b9a0309c | 544 | |
1830e74b JS |
545 | retval = 0; |
546 | ||
2e0172b1 | 547 | fd = open(dev, O_RDONLY); |
13e5d845 JS |
548 | if (fd < 0) |
549 | goto disappeared; | |
b9a0309c | 550 | |
802961a2 MT |
551 | if (st->devnm[0] == 0) |
552 | strcpy(st->devnm, fd2devnm(fd)); | |
553 | ||
554 | for (mse2 = mdstat; mse2; mse2 = mse2->next) | |
555 | if (strcmp(mse2->devnm, st->devnm) == 0) { | |
556 | mse2->devnm[0] = 0; /* flag it as "used" */ | |
557 | mse = mse2; | |
558 | } | |
559 | ||
560 | if (!mse) { | |
561 | /* duplicated array in statelist | |
562 | * or re-created after reading mdstat | |
563 | */ | |
564 | st->err++; | |
565 | goto out; | |
566 | } | |
567 | ||
568 | if (mse->level == NULL) | |
569 | is_container = 1; | |
570 | ||
2dab69c9 | 571 | if (!is_container && !md_array_active(fd)) |
13e5d845 | 572 | goto disappeared; |
b9a0309c | 573 | |
2e0172b1 | 574 | fcntl(fd, F_SETFD, FD_CLOEXEC); |
13e5d845 JS |
575 | if (md_get_array_info(fd, &array) < 0) |
576 | goto disappeared; | |
577 | ||
b3ab4e4d MT |
578 | if (!is_container && map_name(pers, mse->level) > 0) |
579 | redundancy_only_flags |= GET_MISMATCH; | |
802961a2 MT |
580 | |
581 | sra = sysfs_read(-1, st->devnm, GET_LEVEL | GET_DISKS | GET_DEVS | | |
b3ab4e4d | 582 | GET_STATE | redundancy_only_flags); |
aed5f5c3 | 583 | |
aed5f5c3 JS |
584 | if (!sra) |
585 | goto disappeared; | |
586 | ||
2e0172b1 N |
587 | /* It's much easier to list what array levels can't |
588 | * have a device disappear than all of them that can | |
589 | */ | |
48bc2ade | 590 | if (sra->array.level == 0 || sra->array.level == -1) { |
f1661bd7 | 591 | if (!st->err && !st->from_config) |
04e27c20 | 592 | alert("DeviceDisappeared", dev, " Wrong-Level", ainfo); |
73ff0732 | 593 | st->err++; |
1830e74b | 594 | goto out; |
2e0172b1 | 595 | } |
2e0172b1 | 596 | |
2e0172b1 N |
597 | /* this array is in /proc/mdstat */ |
598 | if (array.utime == 0) | |
599 | /* external arrays don't update utime, so | |
600 | * just make sure it is always different. */ | |
601 | array.utime = st->utime + 1;; | |
602 | ||
0f760384 | 603 | if (st->err) { |
73ff0732 | 604 | /* New array appeared where previously had an error */ |
0f760384 N |
605 | st->err = 0; |
606 | st->percent = RESYNC_NONE; | |
721b662b | 607 | new_array = 1; |
007087d0 MT |
608 | if (!is_container) |
609 | alert("NewArray", st->devname, NULL, ainfo); | |
0f760384 N |
610 | } |
611 | ||
b8e5713c | 612 | if (st->utime == array.utime && st->failed == sra->array.failed_disks && |
e5eb6857 | 613 | st->working == sra->array.working_disks && |
b98943a4 | 614 | st->spare == sra->array.spare_disks && |
d7be7d87 | 615 | (mse == NULL || (mse->percent == st->percent))) { |
ff044d6b | 616 | if ((st->active < st->raid) && st->spare == 0) |
1830e74b JS |
617 | retval = 1; |
618 | goto out; | |
2e0172b1 N |
619 | } |
620 | if (st->utime == 0 && /* new array */ | |
f27904a5 | 621 | mse->pattern && strchr(mse->pattern, '_') /* degraded */) |
e0bd6a96 | 622 | alert("DegradedArray", dev, NULL, ainfo); |
2e0172b1 | 623 | |
b98943a4 JS |
624 | if (st->utime == 0 && /* new array */ st->expected_spares > 0 && |
625 | sra->array.spare_disks < st->expected_spares) | |
e0bd6a96 | 626 | alert("SparesMissing", dev, NULL, ainfo); |
9dad51d4 | 627 | if (st->percent < 0 && st->percent != RESYNC_UNKNOWN && |
2e0172b1 | 628 | mse->percent >= 0) |
e0bd6a96 | 629 | alert("RebuildStarted", dev, NULL, ainfo); |
f27904a5 | 630 | if (st->percent >= 0 && mse->percent >= 0 && |
2e0172b1 | 631 | (mse->percent / increments) > (st->percent / increments)) { |
97589839 | 632 | char percentalert[18]; |
f27904a5 JS |
633 | /* |
634 | * "RebuildNN" (10 chars) or "RebuildStarted" (15 chars) | |
635 | */ | |
2e0172b1 N |
636 | |
637 | if((mse->percent / increments) == 0) | |
f27904a5 JS |
638 | snprintf(percentalert, sizeof(percentalert), |
639 | "RebuildStarted"); | |
2e0172b1 | 640 | else |
f27904a5 JS |
641 | snprintf(percentalert, sizeof(percentalert), |
642 | "Rebuild%02d", mse->percent); | |
2e0172b1 | 643 | |
e0bd6a96 | 644 | alert(percentalert, dev, NULL, ainfo); |
2e0172b1 N |
645 | } |
646 | ||
f27904a5 | 647 | if (mse->percent == RESYNC_NONE && st->percent >= 0) { |
2e0172b1 N |
648 | /* Rebuild/sync/whatever just finished. |
649 | * If there is a number in /mismatch_cnt, | |
650 | * we should report that. | |
651 | */ | |
2e0172b1 | 652 | if (sra && sra->mismatch_cnt > 0) { |
8453f8d0 LD |
653 | char cnt[80]; |
654 | snprintf(cnt, sizeof(cnt), | |
655 | " mismatches found: %d (on raid level %d)", | |
f566ef45 | 656 | sra->mismatch_cnt, sra->array.level); |
e0bd6a96 | 657 | alert("RebuildFinished", dev, cnt, ainfo); |
2e0172b1 | 658 | } else |
e0bd6a96 | 659 | alert("RebuildFinished", dev, NULL, ainfo); |
2e0172b1 N |
660 | } |
661 | st->percent = mse->percent; | |
662 | ||
b98943a4 | 663 | remaining_disks = sra->array.nr_disks; |
f27904a5 | 664 | for (i = 0; i < MAX_DISKS && remaining_disks > 0; i++) { |
2e0172b1 N |
665 | mdu_disk_info_t disc; |
666 | disc.number = i; | |
d97572f5 | 667 | if (md_get_disk_info(fd, &disc) >= 0) { |
2e0172b1 N |
668 | info[i].state = disc.state; |
669 | info[i].major = disc.major; | |
670 | info[i].minor = disc.minor; | |
9e6d9291 N |
671 | if (disc.major || disc.minor) |
672 | remaining_disks --; | |
2e0172b1 N |
673 | } else |
674 | info[i].major = info[i].minor = 0; | |
675 | } | |
9e6d9291 | 676 | last_disk = i; |
2e0172b1 | 677 | |
4019ad07 JL |
678 | if (mse->metadata_version && |
679 | strncmp(mse->metadata_version, "external:", 9) == 0 && | |
4dd2df09 N |
680 | is_subarray(mse->metadata_version+9)) { |
681 | char *sl; | |
f566ef45 | 682 | strcpy(st->parent_devnm, mse->metadata_version + 10); |
4dd2df09 N |
683 | sl = strchr(st->parent_devnm, '/'); |
684 | if (sl) | |
685 | *sl = 0; | |
686 | } else | |
687 | st->parent_devnm[0] = 0; | |
f27904a5 | 688 | if (st->metadata == NULL && st->parent_devnm[0] == 0) |
2e0172b1 N |
689 | st->metadata = super_by_fd(fd, NULL); |
690 | ||
f566ef45 JS |
691 | for (i = 0; i < MAX_DISKS; i++) { |
692 | mdu_disk_info_t disc = {0, 0, 0, 0, 0}; | |
693 | int newstate = 0; | |
2e0172b1 N |
694 | int change; |
695 | char *dv = NULL; | |
696 | disc.number = i; | |
f27904a5 | 697 | if (i < last_disk && (info[i].major || info[i].minor)) { |
2e0172b1 | 698 | newstate = info[i].state; |
f27904a5 JS |
699 | dv = map_dev_preferred(info[i].major, info[i].minor, 1, |
700 | prefer); | |
2e0172b1 N |
701 | disc.state = newstate; |
702 | disc.major = info[i].major; | |
703 | disc.minor = info[i].minor; | |
721b662b N |
704 | } else |
705 | newstate = (1 << MD_DISK_REMOVED); | |
706 | ||
2e0172b1 | 707 | if (dv == NULL && st->devid[i]) |
f27904a5 JS |
708 | dv = map_dev_preferred(major(st->devid[i]), |
709 | minor(st->devid[i]), 1, prefer); | |
2e0172b1 | 710 | change = newstate ^ st->devstate[i]; |
721b662b | 711 | if (st->utime && change && !st->err && !new_array) { |
f27904a5 | 712 | if ((st->devstate[i]&change) & (1 << MD_DISK_SYNC)) |
e0bd6a96 | 713 | alert("Fail", dev, dv, ainfo); |
f27904a5 | 714 | else if ((newstate & (1 << MD_DISK_FAULTY)) && |
2e0172b1 | 715 | (disc.major || disc.minor) && |
f27904a5 JS |
716 | st->devid[i] == makedev(disc.major, |
717 | disc.minor)) | |
e0bd6a96 | 718 | alert("FailSpare", dev, dv, ainfo); |
f27904a5 | 719 | else if ((newstate&change) & (1 << MD_DISK_SYNC)) |
e0bd6a96 | 720 | alert("SpareActive", dev, dv, ainfo); |
2e0172b1 N |
721 | } |
722 | st->devstate[i] = newstate; | |
723 | st->devid[i] = makedev(disc.major, disc.minor); | |
724 | } | |
b98943a4 | 725 | st->active = sra->array.active_disks; |
e5eb6857 | 726 | st->working = sra->array.working_disks; |
b98943a4 | 727 | st->spare = sra->array.spare_disks; |
b8e5713c | 728 | st->failed = sra->array.failed_disks; |
2e0172b1 | 729 | st->utime = array.utime; |
12a9d21f | 730 | st->raid = sra->array.raid_disks; |
2e0172b1 | 731 | st->err = 0; |
a90e1050 | 732 | if ((st->active < st->raid) && st->spare == 0) |
1830e74b JS |
733 | retval = 1; |
734 | ||
735 | out: | |
aed5f5c3 JS |
736 | if (sra) |
737 | sysfs_free(sra); | |
802961a2 | 738 | if (fd >= 0) |
13e5d845 | 739 | close(fd); |
1830e74b | 740 | return retval; |
13e5d845 JS |
741 | |
742 | disappeared: | |
007087d0 | 743 | if (!st->err && !is_container) |
13e5d845 JS |
744 | alert("DeviceDisappeared", dev, NULL, ainfo); |
745 | st->err++; | |
746 | goto out; | |
2e0172b1 N |
747 | } |
748 | ||
83f3bc5f | 749 | static int add_new_arrays(struct mdstat_ent *mdstat, struct state **statelist, |
e0bd6a96 | 750 | int test, struct alert_info *info) |
2e0172b1 N |
751 | { |
752 | struct mdstat_ent *mse; | |
753 | int new_found = 0; | |
1e08717f | 754 | char *name; |
2e0172b1 | 755 | |
f566ef45 | 756 | for (mse = mdstat; mse; mse = mse->next) |
d7be7d87 | 757 | if (mse->devnm[0] && (!mse->level || /* retrieve containers */ |
f566ef45 JS |
758 | (strcmp(mse->level, "raid0") != 0 && |
759 | strcmp(mse->level, "linear") != 0))) { | |
503975b9 | 760 | struct state *st = xcalloc(1, sizeof *st); |
2e0172b1 N |
761 | mdu_array_info_t array; |
762 | int fd; | |
1e08717f SV |
763 | |
764 | name = get_md_name(mse->devnm); | |
765 | if (!name) { | |
766 | free(st); | |
767 | continue; | |
768 | } | |
769 | ||
770 | st->devname = xstrdup(name); | |
2e0172b1 | 771 | if ((fd = open(st->devname, O_RDONLY)) < 0 || |
9cd39f01 | 772 | md_get_array_info(fd, &array) < 0) { |
2e0172b1 | 773 | /* no such array */ |
9cd39f01 JS |
774 | if (fd >= 0) |
775 | close(fd); | |
2e0172b1 N |
776 | put_md_name(st->devname); |
777 | free(st->devname); | |
778 | if (st->metadata) { | |
779 | st->metadata->ss->free_super(st->metadata); | |
780 | free(st->metadata); | |
781 | } | |
782 | free(st); | |
783 | continue; | |
784 | } | |
785 | close(fd); | |
83f3bc5f | 786 | st->next = *statelist; |
2e0172b1 | 787 | st->err = 1; |
73ff0732 | 788 | st->from_auto = 1; |
4dd2df09 | 789 | strcpy(st->devnm, mse->devnm); |
9dad51d4 | 790 | st->percent = RESYNC_UNKNOWN; |
2e0172b1 | 791 | st->expected_spares = -1; |
eb28e119 | 792 | if (mse->metadata_version && |
f566ef45 JS |
793 | strncmp(mse->metadata_version, |
794 | "external:", 9) == 0 && | |
4dd2df09 N |
795 | is_subarray(mse->metadata_version+9)) { |
796 | char *sl; | |
797 | strcpy(st->parent_devnm, | |
798 | mse->metadata_version+10); | |
799 | sl = strchr(st->parent_devnm, '/'); | |
800 | *sl = 0; | |
801 | } else | |
802 | st->parent_devnm[0] = 0; | |
83f3bc5f | 803 | *statelist = st; |
2e0172b1 | 804 | if (test) |
e0bd6a96 | 805 | alert("TestMessage", st->devname, NULL, info); |
2e0172b1 N |
806 | new_found = 1; |
807 | } | |
808 | return new_found; | |
809 | } | |
810 | ||
fbfdcb06 AO |
811 | static int get_required_spare_criteria(struct state *st, |
812 | struct spare_criteria *sc) | |
80e7f8c3 AC |
813 | { |
814 | int fd; | |
80e7f8c3 | 815 | |
f566ef45 | 816 | if (!st->metadata || !st->metadata->ss->get_spare_criteria) { |
fbfdcb06 | 817 | sc->min_size = 0; |
4b57ecf6 | 818 | sc->sector_size = 0; |
de697acc | 819 | return 0; |
300f5033 | 820 | } |
80e7f8c3 AC |
821 | |
822 | fd = open(st->devname, O_RDONLY); | |
823 | if (fd < 0) | |
de697acc AC |
824 | return 1; |
825 | if (st->metadata->ss->external) | |
826 | st->metadata->ss->load_container(st->metadata, fd, st->devname); | |
827 | else | |
828 | st->metadata->ss->load_super(st->metadata, fd, st->devname); | |
80e7f8c3 | 829 | close(fd); |
de697acc AC |
830 | if (!st->metadata->sb) |
831 | return 1; | |
fbfdcb06 AO |
832 | |
833 | st->metadata->ss->get_spare_criteria(st->metadata, sc); | |
80e7f8c3 AC |
834 | st->metadata->ss->free_super(st->metadata); |
835 | ||
de697acc | 836 | return 0; |
80e7f8c3 AC |
837 | } |
838 | ||
5ec0f373 | 839 | static int check_donor(struct state *from, struct state *to) |
2feb22ef | 840 | { |
66f5c4b6 N |
841 | struct state *sub; |
842 | ||
2feb22ef N |
843 | if (from == to) |
844 | return 0; | |
66f5c4b6 N |
845 | if (from->parent) |
846 | /* Cannot move from a member */ | |
2feb22ef | 847 | return 0; |
ff044d6b AC |
848 | if (from->err) |
849 | return 0; | |
66f5c4b6 N |
850 | for (sub = from->subarray; sub; sub = sub->subarray) |
851 | /* If source array has degraded subarrays, don't | |
852 | * remove anything | |
853 | */ | |
854 | if (sub->active < sub->raid) | |
855 | return 0; | |
856 | if (from->metadata->ss->external == 0) | |
857 | if (from->active < from->raid) | |
858 | return 0; | |
2feb22ef N |
859 | if (from->spare <= 0) |
860 | return 0; | |
e78dda3b | 861 | return 1; |
2feb22ef N |
862 | } |
863 | ||
0f0749ad | 864 | static dev_t choose_spare(struct state *from, struct state *to, |
f566ef45 | 865 | struct domainlist *domlist, struct spare_criteria *sc) |
0fa21e85 N |
866 | { |
867 | int d; | |
0f0749ad | 868 | dev_t dev = 0; |
0fa21e85 | 869 | |
b0599bda | 870 | for (d = from->raid; !dev && d < MAX_DISKS; d++) { |
f566ef45 | 871 | if (from->devid[d] > 0 && from->devstate[d] == 0) { |
0fa21e85 N |
872 | struct dev_policy *pol; |
873 | unsigned long long dev_size; | |
4b57ecf6 | 874 | unsigned int dev_sector_size; |
0fa21e85 | 875 | |
bfd76b93 CA |
876 | if (to->metadata->ss->external && |
877 | test_partition_from_id(from->devid[d])) | |
878 | continue; | |
879 | ||
fbfdcb06 | 880 | if (sc->min_size && |
0fa21e85 | 881 | dev_size_from_id(from->devid[d], &dev_size) && |
fbfdcb06 | 882 | dev_size < sc->min_size) |
0fa21e85 N |
883 | continue; |
884 | ||
4b57ecf6 AO |
885 | if (sc->sector_size && |
886 | dev_sector_size_from_id(from->devid[d], | |
887 | &dev_sector_size) && | |
888 | sc->sector_size != dev_sector_size) | |
889 | continue; | |
890 | ||
4dd2df09 | 891 | pol = devid_policy(from->devid[d]); |
0fa21e85 N |
892 | if (from->spare_group) |
893 | pol_add(&pol, pol_domain, | |
894 | from->spare_group, NULL); | |
f566ef45 JS |
895 | if (domain_test(domlist, pol, |
896 | to->metadata->ss->name) == 1) | |
0fa21e85 N |
897 | dev = from->devid[d]; |
898 | dev_policy_free(pol); | |
899 | } | |
900 | } | |
901 | return dev; | |
902 | } | |
903 | ||
0f0749ad | 904 | static dev_t container_choose_spare(struct state *from, struct state *to, |
f0b85306 | 905 | struct domainlist *domlist, |
fbfdcb06 | 906 | struct spare_criteria *sc, int active) |
5739e0d0 N |
907 | { |
908 | /* This is similar to choose_spare, but we cannot trust devstate, | |
909 | * so we need to read the metadata instead | |
910 | */ | |
326727d9 | 911 | struct mdinfo *list; |
5739e0d0 | 912 | struct supertype *st = from->metadata; |
ff044d6b | 913 | int fd = open(from->devname, O_RDONLY); |
5739e0d0 | 914 | int err; |
0f0749ad | 915 | dev_t dev = 0; |
5739e0d0 N |
916 | |
917 | if (fd < 0) | |
918 | return 0; | |
326727d9 AC |
919 | if (!st->ss->getinfo_super_disks) { |
920 | close(fd); | |
5739e0d0 | 921 | return 0; |
326727d9 | 922 | } |
1011e834 | 923 | |
5739e0d0 N |
924 | err = st->ss->load_container(st, fd, NULL); |
925 | close(fd); | |
926 | if (err) | |
927 | return 0; | |
1011e834 | 928 | |
a1e49d69 CA |
929 | if (from == to) { |
930 | /* We must check if number of active disks has not increased | |
931 | * since ioctl in main loop. mdmon may have added spare | |
932 | * to subarray. If so we do not need to look for more spares | |
933 | * so return non zero value */ | |
934 | int active_cnt = 0; | |
935 | struct mdinfo *dp; | |
936 | list = st->ss->getinfo_super_disks(st); | |
937 | if (!list) { | |
938 | st->ss->free_super(st); | |
939 | return 1; | |
940 | } | |
941 | dp = list->devs; | |
942 | while (dp) { | |
f566ef45 JS |
943 | if (dp->disk.state & (1 << MD_DISK_SYNC) && |
944 | !(dp->disk.state & (1 << MD_DISK_FAULTY))) | |
a1e49d69 CA |
945 | active_cnt++; |
946 | dp = dp->next; | |
947 | } | |
948 | sysfs_free(list); | |
949 | if (active < active_cnt) { | |
950 | /* Spare just activated.*/ | |
951 | st->ss->free_super(st); | |
952 | return 1; | |
953 | } | |
954 | } | |
955 | ||
326727d9 | 956 | /* We only need one spare so full list not needed */ |
fbfdcb06 | 957 | list = container_choose_spares(st, sc, domlist, from->spare_group, |
326727d9 AC |
958 | to->metadata->ss->name, 1); |
959 | if (list) { | |
960 | struct mdinfo *disks = list->devs; | |
961 | if (disks) | |
962 | dev = makedev(disks->disk.major, disks->disk.minor); | |
963 | sysfs_free(list); | |
5739e0d0 | 964 | } |
326727d9 | 965 | st->ss->free_super(st); |
5739e0d0 N |
966 | return dev; |
967 | } | |
968 | ||
e0bd6a96 | 969 | static void try_spare_migration(struct state *statelist, struct alert_info *info) |
2e0172b1 | 970 | { |
66f5c4b6 N |
971 | struct state *from; |
972 | struct state *st; | |
fbfdcb06 | 973 | struct spare_criteria sc; |
c3621c0a ML |
974 | |
975 | link_containers_with_subarrays(statelist); | |
66f5c4b6 | 976 | for (st = statelist; st; st = st->next) |
f566ef45 | 977 | if (st->active < st->raid && st->spare == 0 && !st->err) { |
e78dda3b N |
978 | struct domainlist *domlist = NULL; |
979 | int d; | |
66f5c4b6 N |
980 | struct state *to = st; |
981 | ||
4dd2df09 | 982 | if (to->parent_devnm[0] && !to->parent) |
c0dc0ad5 CA |
983 | /* subarray monitored without parent container |
984 | * we can't move spares here */ | |
985 | continue; | |
1011e834 | 986 | |
66f5c4b6 N |
987 | if (to->parent) |
988 | /* member of a container */ | |
989 | to = to->parent; | |
e78dda3b | 990 | |
fbfdcb06 | 991 | if (get_required_spare_criteria(to, &sc)) |
de697acc | 992 | continue; |
e9a2ac02 AC |
993 | if (to->metadata->ss->external) { |
994 | /* We must make sure there is | |
995 | * no suitable spare in container already. | |
996 | * If there is we don't add more */ | |
997 | dev_t devid = container_choose_spare( | |
fbfdcb06 | 998 | to, to, NULL, &sc, st->active); |
e9a2ac02 AC |
999 | if (devid > 0) |
1000 | continue; | |
1001 | } | |
b0599bda | 1002 | for (d = 0; d < MAX_DISKS; d++) |
e78dda3b N |
1003 | if (to->devid[d]) |
1004 | domainlist_add_dev(&domlist, | |
1005 | to->devid[d], | |
1006 | to->metadata->ss->name); | |
1007 | if (to->spare_group) | |
1008 | domain_add(&domlist, to->spare_group); | |
5ec0f373 ML |
1009 | /* |
1010 | * No spare migration if the destination | |
1011 | * has no domain. Skip this array. | |
1012 | */ | |
1013 | if (!domlist) | |
1014 | continue; | |
0fa21e85 | 1015 | for (from=statelist ; from ; from=from->next) { |
0f0749ad | 1016 | dev_t devid; |
5ec0f373 | 1017 | if (!check_donor(from, to)) |
0fa21e85 | 1018 | continue; |
5739e0d0 N |
1019 | if (from->metadata->ss->external) |
1020 | devid = container_choose_spare( | |
fbfdcb06 | 1021 | from, to, domlist, &sc, 0); |
5739e0d0 | 1022 | else |
f0b85306 | 1023 | devid = choose_spare(from, to, domlist, |
fbfdcb06 | 1024 | &sc); |
f566ef45 JS |
1025 | if (devid > 0 && |
1026 | move_spare(from->devname, to->devname, | |
1027 | devid)) { | |
1028 | alert("MoveSpare", to->devname, | |
1029 | from->devname, info); | |
d52bb542 AC |
1030 | break; |
1031 | } | |
0fa21e85 | 1032 | } |
e78dda3b | 1033 | domain_free(domlist); |
2e0172b1 N |
1034 | } |
1035 | } | |
c3621c0a ML |
1036 | |
1037 | /* search the statelist to connect external | |
1038 | * metadata subarrays with their containers | |
1039 | * We always completely rebuild the tree from scratch as | |
1040 | * that is safest considering the possibility of entries | |
1041 | * disappearing or changing. | |
1042 | */ | |
1043 | static void link_containers_with_subarrays(struct state *list) | |
1044 | { | |
1045 | struct state *st; | |
1046 | struct state *cont; | |
1047 | for (st = list; st; st = st->next) { | |
1048 | st->parent = NULL; | |
1049 | st->subarray = NULL; | |
1050 | } | |
1051 | for (st = list; st; st = st->next) | |
4dd2df09 | 1052 | if (st->parent_devnm[0]) |
c3621c0a | 1053 | for (cont = list; cont; cont = cont->next) |
f566ef45 | 1054 | if (!cont->err && cont->parent_devnm[0] == 0 && |
4dd2df09 | 1055 | strcmp(cont->devnm, st->parent_devnm) == 0) { |
c3621c0a ML |
1056 | st->parent = cont; |
1057 | st->subarray = cont->subarray; | |
1058 | cont->subarray = st; | |
1059 | break; | |
1060 | } | |
1061 | } | |
1062 | ||
af3396da | 1063 | #ifndef NO_LIBUDEV |
49b69533 OS |
1064 | /* function: check_udev_activity |
1065 | * Description: Function waits for udev to finish | |
1066 | * events processing. | |
1067 | * Returns: | |
1068 | * 1 - detected error while opening udev | |
1069 | * 2 - timeout | |
1070 | * 0 - successfull completion | |
1071 | */ | |
1072 | static int check_udev_activity(void) | |
1073 | { | |
1074 | struct udev *udev = NULL; | |
1075 | struct udev_queue *udev_queue = NULL; | |
1076 | int timeout_cnt = 30; | |
1077 | int rc = 0; | |
1078 | ||
1079 | /* | |
1080 | * In rare cases systemd may not have udevm, | |
1081 | * in such cases just exit with rc 0 | |
1082 | */ | |
1083 | if (!use_udev()) | |
1084 | goto out; | |
1085 | ||
1086 | udev = udev_new(); | |
1087 | if (!udev) { | |
1088 | rc = 1; | |
1089 | goto out; | |
1090 | } | |
1091 | ||
1092 | udev_queue = udev_queue_new(udev); | |
1093 | if (!udev_queue) { | |
1094 | rc = 1; | |
1095 | goto out; | |
1096 | } | |
1097 | ||
1098 | if (udev_queue_get_queue_is_empty(udev_queue)) | |
1099 | goto out; | |
1100 | ||
1101 | while (!udev_queue_get_queue_is_empty(udev_queue)) { | |
1102 | sleep(1); | |
1103 | ||
1104 | if (timeout_cnt) | |
1105 | timeout_cnt--; | |
1106 | else { | |
1107 | rc = 2; | |
1108 | goto out; | |
1109 | } | |
1110 | } | |
1111 | ||
1112 | out: | |
1113 | if (udev_queue) | |
1114 | udev_queue_unref(udev_queue); | |
1115 | if (udev) | |
1116 | udev_unref(udev); | |
1117 | return rc; | |
1118 | } | |
af3396da | 1119 | #endif |
49b69533 | 1120 | |
b90c0e9a NB |
1121 | /* Not really Monitor but ... */ |
1122 | int Wait(char *dev) | |
1123 | { | |
4dd2df09 | 1124 | char devnm[32]; |
cb91230c TM |
1125 | dev_t rdev; |
1126 | char *tmp; | |
b90c0e9a | 1127 | int rv = 1; |
d3f6cf4f | 1128 | int frozen_remaining = 3; |
b90c0e9a | 1129 | |
cb91230c | 1130 | if (!stat_is_blkdev(dev, &rdev)) |
b90c0e9a | 1131 | return 2; |
cb91230c TM |
1132 | |
1133 | tmp = devid2devnm(rdev); | |
1134 | if (!tmp) { | |
1135 | pr_err("Cannot get md device name.\n"); | |
1136 | return 2; | |
1137 | } | |
1138 | ||
1139 | strcpy(devnm, tmp); | |
b90c0e9a NB |
1140 | |
1141 | while(1) { | |
1142 | struct mdstat_ent *ms = mdstat_read(1, 0); | |
1143 | struct mdstat_ent *e; | |
1144 | ||
f566ef45 | 1145 | for (e = ms; e; e = e->next) |
4dd2df09 | 1146 | if (strcmp(e->devnm, devnm) == 0) |
b90c0e9a NB |
1147 | break; |
1148 | ||
d3f6cf4f | 1149 | if (e && e->percent == RESYNC_NONE) { |
276be514 N |
1150 | /* We could be in the brief pause before something |
1151 | * starts. /proc/mdstat doesn't show that, but | |
1152 | * sync_action does. | |
1153 | */ | |
1154 | struct mdinfo mdi; | |
1155 | char buf[21]; | |
dae13137 JS |
1156 | |
1157 | if (sysfs_init(&mdi, -1, devnm)) | |
1158 | return 2; | |
276be514 N |
1159 | if (sysfs_get_str(&mdi, NULL, "sync_action", |
1160 | buf, 20) > 0 && | |
d3f6cf4f | 1161 | strcmp(buf,"idle\n") != 0) { |
276be514 | 1162 | e->percent = RESYNC_UNKNOWN; |
d3f6cf4f N |
1163 | if (strcmp(buf, "frozen\n") == 0) { |
1164 | if (frozen_remaining == 0) | |
1165 | e->percent = RESYNC_NONE; | |
1166 | else | |
1167 | frozen_remaining -= 1; | |
1168 | } | |
1169 | } | |
276be514 | 1170 | } |
639c3c10 | 1171 | if (!e || e->percent == RESYNC_NONE) { |
e7783ee6 | 1172 | if (e && e->metadata_version && |
c94709e8 DW |
1173 | strncmp(e->metadata_version, "external:", 9) == 0) { |
1174 | if (is_subarray(&e->metadata_version[9])) | |
1175 | ping_monitor(&e->metadata_version[9]); | |
1176 | else | |
4dd2df09 | 1177 | ping_monitor(devnm); |
c94709e8 | 1178 | } |
b90c0e9a NB |
1179 | free_mdstat(ms); |
1180 | return rv; | |
1181 | } | |
89a10d84 | 1182 | free_mdstat(ms); |
b90c0e9a NB |
1183 | rv = 0; |
1184 | mdstat_wait(5); | |
1185 | } | |
1186 | } | |
679eb882 | 1187 | |
43ebc910 GP |
1188 | /* The state "broken" is used only for RAID0/LINEAR - it's the same as |
1189 | * "clean", but used in case the array has one or more members missing. | |
1190 | */ | |
679eb882 | 1191 | static char *clean_states[] = { |
43ebc910 | 1192 | "clear", "inactive", "readonly", "read-auto", "clean", "broken", NULL }; |
679eb882 | 1193 | |
1ea04629 | 1194 | int WaitClean(char *dev, int verbose) |
679eb882 N |
1195 | { |
1196 | int fd; | |
1197 | struct mdinfo *mdi; | |
1198 | int rv = 1; | |
4dd2df09 | 1199 | char devnm[32]; |
679eb882 | 1200 | |
9e04ac1c ZL |
1201 | if (!stat_is_blkdev(dev, NULL)) |
1202 | return 2; | |
679eb882 N |
1203 | fd = open(dev, O_RDONLY); |
1204 | if (fd < 0) { | |
1205 | if (verbose) | |
e7b84f9d | 1206 | pr_err("Couldn't open %s: %s\n", dev, strerror(errno)); |
679eb882 N |
1207 | return 1; |
1208 | } | |
1209 | ||
4dd2df09 N |
1210 | strcpy(devnm, fd2devnm(fd)); |
1211 | mdi = sysfs_read(fd, devnm, GET_VERSION|GET_LEVEL|GET_SAFEMODE); | |
679eb882 N |
1212 | if (!mdi) { |
1213 | if (verbose) | |
7a862a02 | 1214 | pr_err("Failed to read sysfs attributes for %s\n", dev); |
679eb882 N |
1215 | close(fd); |
1216 | return 0; | |
1217 | } | |
1218 | ||
1219 | switch(mdi->array.level) { | |
1220 | case LEVEL_LINEAR: | |
1221 | case LEVEL_MULTIPATH: | |
1222 | case 0: | |
1223 | /* safemode delay is irrelevant for these levels */ | |
1224 | rv = 0; | |
1225 | } | |
1226 | ||
1227 | /* for internal metadata the kernel handles the final clean | |
1228 | * transition, containers can never be dirty | |
1229 | */ | |
1230 | if (!is_subarray(mdi->text_version)) | |
1231 | rv = 0; | |
1232 | ||
1233 | /* safemode disabled ? */ | |
1234 | if (mdi->safe_mode_delay == 0) | |
1235 | rv = 0; | |
1236 | ||
1237 | if (rv) { | |
4dd2df09 | 1238 | int state_fd = sysfs_open(fd2devnm(fd), NULL, "array_state"); |
679eb882 | 1239 | char buf[20]; |
efc67e8e | 1240 | int delay = 5000; |
679eb882 N |
1241 | |
1242 | /* minimize the safe_mode_delay and prepare to wait up to 5s | |
1243 | * for writes to quiesce | |
1244 | */ | |
1245 | sysfs_set_safemode(mdi, 1); | |
679eb882 N |
1246 | |
1247 | /* wait for array_state to be clean */ | |
1248 | while (1) { | |
1249 | rv = read(state_fd, buf, sizeof(buf)); | |
1250 | if (rv < 0) | |
1251 | break; | |
43ebc910 GP |
1252 | if (sysfs_match_word(buf, clean_states) < |
1253 | (int)ARRAY_SIZE(clean_states) - 1) | |
679eb882 | 1254 | break; |
efc67e8e | 1255 | rv = sysfs_wait(state_fd, &delay); |
679eb882 N |
1256 | if (rv < 0 && errno != EINTR) |
1257 | break; | |
1258 | lseek(state_fd, 0, SEEK_SET); | |
1259 | } | |
1260 | if (rv < 0) | |
1261 | rv = 1; | |
1ea04629 | 1262 | else if (ping_monitor(mdi->text_version) == 0) { |
679eb882 N |
1263 | /* we need to ping to close the window between array |
1264 | * state transitioning to clean and the metadata being | |
1265 | * marked clean | |
1266 | */ | |
1267 | rv = 0; | |
1ea04629 | 1268 | } else { |
679eb882 | 1269 | rv = 1; |
1ea04629 MT |
1270 | pr_err("Error connecting monitor with %s\n", dev); |
1271 | } | |
679eb882 | 1272 | if (rv && verbose) |
f566ef45 | 1273 | pr_err("Error waiting for %s to be clean\n", dev); |
679eb882 N |
1274 | |
1275 | /* restore the original safe_mode_delay */ | |
1276 | sysfs_set_safemode(mdi, mdi->safe_mode_delay); | |
1277 | close(state_fd); | |
1278 | } | |
1279 | ||
1280 | sysfs_free(mdi); | |
1281 | close(fd); | |
1282 | ||
1283 | return rv; | |
1284 | } |