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