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