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