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