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