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