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