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