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