]>
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 | |
e27d562b NB |
33 | /* The largest number of disks current arrays can manage is 384 |
34 | * This really should be dynamically, but that will have to wait | |
35 | * At least it isn't MD_SB_DISKS. | |
36 | */ | |
37 | #define MaxDisks 384 | |
2e0172b1 N |
38 | struct state { |
39 | char *devname; | |
40 | int devnum; /* to sync with mdstat info */ | |
41 | long utime; | |
42 | int err; | |
43 | char *spare_group; | |
44 | int active, working, failed, spare, raid; | |
45 | int expected_spares; | |
46 | int devstate[MaxDisks]; | |
47 | unsigned devid[MaxDisks]; | |
48 | int percent; | |
49 | int parent_dev; /* For subarray, devnum of parent. | |
50 | * For others, NoMdDev | |
51 | */ | |
52 | struct supertype *metadata; | |
c3621c0a ML |
53 | struct state *subarray;/* for a container it is a link to first subarray |
54 | * for a subarray it is a link to next subarray | |
55 | * in the same container */ | |
56 | struct state *parent; /* for a subarray it is a link to its container | |
57 | */ | |
2e0172b1 N |
58 | struct state *next; |
59 | }; | |
60 | ||
61 | static int make_daemon(char *pidfile); | |
62 | static int check_one_sharer(int scan); | |
63 | static void alert(char *event, char *dev, char *disc, char *mailaddr, char *mailfrom, | |
64 | char *cmd, int dosyslog); | |
65 | static void check_array(struct state *st, struct mdstat_ent *mdstat, | |
66 | int test, char *mailaddr, | |
67 | char *mailfrom, char *alert_cmd, int dosyslog, | |
68 | int increments); | |
69 | static int add_new_arrays(struct mdstat_ent *mdstat, struct state *statelist, | |
70 | int test, char *mailaddr, char *mailfrom, | |
71 | char *alert_cmd, int dosyslog); | |
72 | static void try_spare_migration(struct state *statelist, | |
73 | char *mailaddr, char *mailfrom, | |
74 | char *alert_cmd, int dosyslog); | |
c3621c0a | 75 | static void link_containers_with_subarrays(struct state *list); |
2e0172b1 | 76 | |
a655e550 | 77 | int Monitor(struct mddev_dev *devlist, |
52826846 | 78 | char *mailaddr, char *alert_cmd, |
aa88f531 | 79 | int period, int daemonise, int scan, int oneshot, |
edde9560 AC |
80 | int dosyslog, int test, 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; |
52826846 | 126 | int finished = 0; |
e0d19036 | 127 | struct mdstat_ent *mdstat = NULL; |
4948b8f7 | 128 | char *mailfrom = NULL; |
e0d19036 | 129 | |
d013a55e | 130 | if (!mailaddr) { |
8aec876d | 131 | mailaddr = conf_get_mailaddr(); |
d013a55e | 132 | if (mailaddr && ! scan) |
56eedc1a | 133 | fprintf(stderr, Name ": Monitor using email address \"%s\" from config file\n", |
d013a55e NB |
134 | mailaddr); |
135 | } | |
8aec876d | 136 | mailfrom = conf_get_mailfrom(); |
4948b8f7 | 137 | |
d013a55e | 138 | if (!alert_cmd) { |
8aec876d | 139 | alert_cmd = conf_get_program(); |
d013a55e | 140 | if (alert_cmd && ! scan) |
56eedc1a | 141 | fprintf(stderr, Name ": Monitor using program \"%s\" from config file\n", |
d013a55e NB |
142 | alert_cmd); |
143 | } | |
56eedc1a NB |
144 | if (scan && !mailaddr && !alert_cmd) { |
145 | fprintf(stderr, Name ": No mail address or alert command - not monitoring.\n"); | |
d013a55e | 146 | return 1; |
56eedc1a | 147 | } |
d013a55e | 148 | |
2e0172b1 N |
149 | if (daemonise) |
150 | if (make_daemon(pidfile)) | |
d013a55e | 151 | return 1; |
e0d19036 | 152 | |
2e0172b1 N |
153 | if (share) |
154 | if (check_one_sharer(scan)) | |
155 | return 1; | |
edde9560 | 156 | |
e0d19036 | 157 | if (devlist == NULL) { |
fa56eddb | 158 | struct mddev_ident *mdlist = conf_get_ident(NULL); |
e0d19036 | 159 | for (; mdlist; mdlist=mdlist->next) { |
fe056d1f N |
160 | struct state *st; |
161 | if (mdlist->devname == NULL) | |
162 | continue; | |
112cace6 N |
163 | if (strcasecmp(mdlist->devname, "<ignore>") == 0) |
164 | continue; | |
fe056d1f | 165 | st = malloc(sizeof *st); |
e0d19036 NB |
166 | if (st == NULL) |
167 | continue; | |
db2d001c N |
168 | if (mdlist->devname[0] == '/') |
169 | st->devname = strdup(mdlist->devname); | |
170 | else { | |
171 | st->devname = malloc(8+strlen(mdlist->devname)+1); | |
172 | strcpy(strcpy(st->devname, "/dev/md/"), | |
173 | mdlist->devname); | |
174 | } | |
e0d19036 NB |
175 | st->utime = 0; |
176 | st->next = statelist; | |
d013a55e | 177 | st->err = 0; |
4450e59f | 178 | st->devnum = INT_MAX; |
e0d19036 | 179 | st->percent = -2; |
feb716e9 | 180 | st->expected_spares = mdlist->spare_disks; |
e0d19036 NB |
181 | if (mdlist->spare_group) |
182 | st->spare_group = strdup(mdlist->spare_group); | |
183 | else | |
184 | st->spare_group = NULL; | |
185 | statelist = st; | |
186 | } | |
187 | } else { | |
a655e550 | 188 | struct mddev_dev *dv; |
e0d19036 | 189 | for (dv=devlist ; dv; dv=dv->next) { |
fa56eddb | 190 | struct mddev_ident *mdlist = conf_get_ident(dv->devname); |
e0d19036 NB |
191 | struct state *st = malloc(sizeof *st); |
192 | if (st == NULL) | |
193 | continue; | |
194 | st->devname = strdup(dv->devname); | |
195 | st->utime = 0; | |
196 | st->next = statelist; | |
d013a55e | 197 | st->err = 0; |
4450e59f | 198 | st->devnum = INT_MAX; |
e0d19036 | 199 | st->percent = -2; |
feb716e9 | 200 | st->expected_spares = -1; |
e0d19036 | 201 | st->spare_group = NULL; |
e5329c37 NB |
202 | if (mdlist) { |
203 | st->expected_spares = mdlist->spare_disks; | |
204 | if (mdlist->spare_group) | |
205 | st->spare_group = strdup(mdlist->spare_group); | |
206 | } | |
e0d19036 NB |
207 | statelist = st; |
208 | } | |
209 | } | |
210 | ||
211 | ||
212 | while (! finished) { | |
aa88f531 | 213 | int new_found = 0; |
e0d19036 NB |
214 | struct state *st; |
215 | ||
216 | if (mdstat) | |
217 | free_mdstat(mdstat); | |
22a88995 | 218 | mdstat = mdstat_read(oneshot?0:1, 0); |
e0d19036 | 219 | |
2e0172b1 N |
220 | for (st=statelist; st; st=st->next) |
221 | check_array(st, mdstat, test, mailaddr, mailfrom, | |
222 | alert_cmd, dosyslog, increments); | |
223 | ||
e0d19036 | 224 | /* now check if there are any new devices found in mdstat */ |
2e0172b1 N |
225 | if (scan) |
226 | new_found = add_new_arrays(mdstat, statelist, test, | |
227 | mailaddr, mailfrom, alert_cmd, | |
228 | dosyslog); | |
229 | ||
e0d19036 NB |
230 | /* If an array has active < raid && spare == 0 && spare_group != NULL |
231 | * Look for another array with spare > 0 and active == raid and same spare_group | |
232 | * if found, choose a device and hotremove/hotadd | |
233 | */ | |
2e0172b1 N |
234 | if (share) |
235 | try_spare_migration(statelist, mailaddr, mailfrom, | |
236 | alert_cmd, dosyslog); | |
aa88f531 NB |
237 | if (!new_found) { |
238 | if (oneshot) | |
239 | break; | |
240 | else | |
dd0781e5 | 241 | mdstat_wait(period); |
aa88f531 | 242 | } |
98c6faba | 243 | test = 0; |
52826846 | 244 | } |
b5e64645 NB |
245 | if (pidfile) |
246 | unlink(pidfile); | |
52826846 NB |
247 | return 0; |
248 | } | |
249 | ||
2e0172b1 N |
250 | static int make_daemon(char *pidfile) |
251 | { | |
252 | int pid = fork(); | |
253 | if (pid > 0) { | |
254 | if (!pidfile) | |
255 | printf("%d\n", pid); | |
256 | else { | |
257 | FILE *pid_file; | |
258 | pid_file=fopen(pidfile, "w"); | |
259 | if (!pid_file) | |
260 | perror("cannot create pid file"); | |
261 | else { | |
262 | fprintf(pid_file,"%d\n", pid); | |
263 | fclose(pid_file); | |
264 | } | |
265 | } | |
266 | return 0; | |
267 | } | |
268 | if (pid < 0) { | |
269 | perror("daemonise"); | |
270 | return 1; | |
271 | } | |
272 | close(0); | |
273 | open("/dev/null", O_RDWR); | |
274 | dup2(0,1); | |
275 | dup2(0,2); | |
276 | setsid(); | |
277 | return 0; | |
278 | } | |
279 | ||
280 | static int check_one_sharer(int scan) | |
281 | { | |
282 | int pid, rv; | |
283 | FILE *fp; | |
284 | char dir[20]; | |
285 | struct stat buf; | |
286 | fp = fopen("/var/run/mdadm/autorebuild.pid", "r"); | |
287 | if (fp) { | |
288 | fscanf(fp, "%d", &pid); | |
289 | sprintf(dir, "/proc/%d", pid); | |
290 | rv = stat(dir, &buf); | |
291 | if (rv != -1) { | |
292 | if (scan) { | |
293 | fprintf(stderr, Name ": Only one " | |
294 | "autorebuild process allowed" | |
295 | " in scan mode, aborting\n"); | |
296 | fclose(fp); | |
297 | return 1; | |
298 | } else { | |
299 | fprintf(stderr, Name ": Warning: One" | |
300 | " autorebuild process already" | |
301 | " running."); | |
302 | } | |
303 | } | |
304 | fclose(fp); | |
305 | } | |
306 | if (scan) { | |
307 | fp = fopen("/var/run/mdadm/autorebuild.pid", "w"); | |
308 | if (!fp) | |
309 | fprintf(stderr, Name ": Cannot create" | |
310 | " autorebuild.pid " | |
311 | "file\n"); | |
312 | else { | |
313 | pid = getpid(); | |
314 | fprintf(fp, "%d\n", pid); | |
315 | fclose(fp); | |
316 | } | |
317 | } | |
318 | return 0; | |
319 | } | |
52826846 | 320 | |
4948b8f7 | 321 | static void alert(char *event, char *dev, char *disc, char *mailaddr, char *mailfrom, char *cmd, |
773135f5 | 322 | int dosyslog) |
52826846 | 323 | { |
773135f5 NB |
324 | int priority; |
325 | ||
cd29a5c8 NB |
326 | if (!cmd && !mailaddr) { |
327 | time_t now = time(0); | |
aba69144 | 328 | |
e0d19036 | 329 | printf("%1.15s: %s on %s %s\n", ctime(&now)+4, event, dev, disc?disc:"unknown device"); |
cd29a5c8 | 330 | } |
52826846 NB |
331 | if (cmd) { |
332 | int pid = fork(); | |
333 | switch(pid) { | |
334 | default: | |
335 | waitpid(pid, NULL, 0); | |
336 | break; | |
337 | case -1: | |
338 | break; | |
339 | case 0: | |
340 | execl(cmd, cmd, event, dev, disc, NULL); | |
341 | exit(2); | |
342 | } | |
343 | } | |
aba69144 NB |
344 | if (mailaddr && |
345 | (strncmp(event, "Fail", 4)==0 || | |
98c6faba | 346 | strncmp(event, "Test", 4)==0 || |
d1732eeb | 347 | strncmp(event, "Spares", 6)==0 || |
aa88f531 | 348 | strncmp(event, "Degrade", 7)==0)) { |
52826846 NB |
349 | FILE *mp = popen(Sendmail, "w"); |
350 | if (mp) { | |
008e1100 | 351 | FILE *mdstat; |
52826846 NB |
352 | char hname[256]; |
353 | gethostname(hname, sizeof(hname)); | |
354 | signal(SIGPIPE, SIG_IGN); | |
4948b8f7 NB |
355 | if (mailfrom) |
356 | fprintf(mp, "From: %s\n", mailfrom); | |
357 | else | |
358 | fprintf(mp, "From: " Name " monitoring <root>\n"); | |
52826846 NB |
359 | fprintf(mp, "To: %s\n", mailaddr); |
360 | fprintf(mp, "Subject: %s event on %s:%s\n\n", event, dev, hname); | |
361 | ||
362 | fprintf(mp, "This is an automatically generated mail message from " Name "\n"); | |
363 | fprintf(mp, "running on %s\n\n", hname); | |
364 | ||
365 | fprintf(mp, "A %s event had been detected on md device %s.\n\n", event, dev); | |
366 | ||
37dfc3d6 | 367 | if (disc && disc[0] != ' ') |
56eedc1a | 368 | fprintf(mp, "It could be related to component device %s.\n\n", disc); |
37dfc3d6 NB |
369 | if (disc && disc[0] == ' ') |
370 | fprintf(mp, "Extra information:%s.\n\n", disc); | |
52826846 NB |
371 | |
372 | fprintf(mp, "Faithfully yours, etc.\n"); | |
008e1100 NB |
373 | |
374 | mdstat = fopen("/proc/mdstat", "r"); | |
375 | if (mdstat) { | |
376 | char buf[8192]; | |
377 | int n; | |
a524a7ee | 378 | fprintf(mp, "\nP.S. The /proc/mdstat file currently contains the following:\n\n"); |
008e1100 | 379 | while ( (n=fread(buf, 1, sizeof(buf), mdstat)) > 0) |
9fca7d62 | 380 | n=fwrite(buf, 1, n, mp); /* yes, i don't care about the result */ |
008e1100 NB |
381 | fclose(mdstat); |
382 | } | |
6278fb3a | 383 | pclose(mp); |
52826846 NB |
384 | } |
385 | ||
386 | } | |
773135f5 NB |
387 | |
388 | /* log the event to syslog maybe */ | |
389 | if (dosyslog) { | |
390 | /* Log at a different severity depending on the event. | |
391 | * | |
392 | * These are the critical events: */ | |
393 | if (strncmp(event, "Fail", 4)==0 || | |
394 | strncmp(event, "Degrade", 7)==0 || | |
395 | strncmp(event, "DeviceDisappeared", 17)==0) | |
396 | priority = LOG_CRIT; | |
397 | /* Good to know about, but are not failures: */ | |
398 | else if (strncmp(event, "Rebuild", 7)==0 || | |
d1732eeb NB |
399 | strncmp(event, "MoveSpare", 9)==0 || |
400 | strncmp(event, "Spares", 6) != 0) | |
773135f5 NB |
401 | priority = LOG_WARNING; |
402 | /* Everything else: */ | |
403 | else | |
404 | priority = LOG_INFO; | |
405 | ||
406 | if (disc) | |
407 | syslog(priority, "%s event detected on md device %s, component device %s", event, dev, disc); | |
408 | else | |
409 | syslog(priority, "%s event detected on md device %s", event, dev); | |
410 | } | |
52826846 | 411 | } |
b90c0e9a | 412 | |
2e0172b1 N |
413 | static void check_array(struct state *st, struct mdstat_ent *mdstat, |
414 | int test, char *mailaddr, | |
415 | char *mailfrom, char *alert_cmd, int dosyslog, | |
416 | int increments) | |
417 | { | |
418 | struct { int state, major, minor; } info[MaxDisks]; | |
419 | mdu_array_info_t array; | |
420 | struct mdstat_ent *mse = NULL, *mse2; | |
421 | char *dev = st->devname; | |
422 | int fd; | |
423 | int i; | |
424 | ||
425 | if (test) | |
426 | alert("TestMessage", dev, NULL, mailaddr, mailfrom, alert_cmd, dosyslog); | |
427 | fd = open(dev, O_RDONLY); | |
428 | if (fd < 0) { | |
429 | if (!st->err) | |
430 | alert("DeviceDisappeared", dev, NULL, | |
431 | mailaddr, mailfrom, alert_cmd, dosyslog); | |
432 | /* fprintf(stderr, Name ": cannot open %s: %s\n", | |
433 | dev, strerror(errno)); | |
434 | */ st->err=1; | |
435 | return; | |
436 | } | |
437 | fcntl(fd, F_SETFD, FD_CLOEXEC); | |
438 | if (ioctl(fd, GET_ARRAY_INFO, &array)<0) { | |
439 | if (!st->err) | |
440 | alert("DeviceDisappeared", dev, NULL, | |
441 | mailaddr, mailfrom, alert_cmd, dosyslog); | |
442 | /* fprintf(stderr, Name ": cannot get array info for %s: %s\n", | |
443 | dev, strerror(errno)); | |
444 | */ st->err=1; | |
445 | close(fd); | |
446 | return; | |
447 | } | |
448 | /* It's much easier to list what array levels can't | |
449 | * have a device disappear than all of them that can | |
450 | */ | |
451 | if (array.level == 0 || array.level == -1) { | |
452 | if (!st->err) | |
453 | alert("DeviceDisappeared", dev, "Wrong-Level", | |
454 | mailaddr, mailfrom, alert_cmd, dosyslog); | |
455 | st->err = 1; | |
456 | close(fd); | |
457 | return; | |
458 | } | |
459 | if (st->devnum == INT_MAX) { | |
460 | struct stat stb; | |
461 | if (fstat(fd, &stb) == 0 && | |
462 | (S_IFMT&stb.st_mode)==S_IFBLK) { | |
463 | if (major(stb.st_rdev) == MD_MAJOR) | |
464 | st->devnum = minor(stb.st_rdev); | |
465 | else | |
466 | st->devnum = -1- (minor(stb.st_rdev)>>6); | |
467 | } | |
468 | } | |
469 | ||
470 | for (mse2 = mdstat ; mse2 ; mse2=mse2->next) | |
471 | if (mse2->devnum == st->devnum) { | |
472 | mse2->devnum = INT_MAX; /* flag it as "used" */ | |
473 | mse = mse2; | |
474 | } | |
475 | ||
476 | if (!mse) { | |
477 | /* duplicated array in statelist | |
478 | * or re-created after reading mdstat*/ | |
479 | st->err = 1; | |
480 | close(fd); | |
481 | return; | |
482 | } | |
483 | /* this array is in /proc/mdstat */ | |
484 | if (array.utime == 0) | |
485 | /* external arrays don't update utime, so | |
486 | * just make sure it is always different. */ | |
487 | array.utime = st->utime + 1;; | |
488 | ||
489 | if (st->utime == array.utime && | |
490 | st->failed == array.failed_disks && | |
491 | st->working == array.working_disks && | |
492 | st->spare == array.spare_disks && | |
493 | (mse == NULL || ( | |
494 | mse->percent == st->percent | |
495 | ))) { | |
496 | close(fd); | |
497 | st->err = 0; | |
498 | return; | |
499 | } | |
500 | if (st->utime == 0 && /* new array */ | |
501 | mse->pattern && strchr(mse->pattern, '_') /* degraded */ | |
502 | ) | |
503 | alert("DegradedArray", dev, NULL, mailaddr, mailfrom, alert_cmd, dosyslog); | |
504 | ||
505 | if (st->utime == 0 && /* new array */ | |
506 | st->expected_spares > 0 && | |
507 | array.spare_disks < st->expected_spares) | |
508 | alert("SparesMissing", dev, NULL, mailaddr, mailfrom, alert_cmd, dosyslog); | |
509 | if (st->percent == -1 && | |
510 | mse->percent >= 0) | |
511 | alert("RebuildStarted", dev, NULL, mailaddr, mailfrom, alert_cmd, dosyslog); | |
512 | if (st->percent >= 0 && | |
513 | mse->percent >= 0 && | |
514 | (mse->percent / increments) > (st->percent / increments)) { | |
515 | char percentalert[15]; // "RebuildNN" (10 chars) or "RebuildStarted" (15 chars) | |
516 | ||
517 | if((mse->percent / increments) == 0) | |
518 | snprintf(percentalert, sizeof(percentalert), "RebuildStarted"); | |
519 | else | |
520 | snprintf(percentalert, sizeof(percentalert), "Rebuild%02d", mse->percent); | |
521 | ||
522 | alert(percentalert, | |
523 | dev, NULL, mailaddr, mailfrom, alert_cmd, dosyslog); | |
524 | } | |
525 | ||
526 | if (mse->percent == -1 && | |
527 | st->percent >= 0) { | |
528 | /* Rebuild/sync/whatever just finished. | |
529 | * If there is a number in /mismatch_cnt, | |
530 | * we should report that. | |
531 | */ | |
532 | struct mdinfo *sra = | |
533 | sysfs_read(-1, st->devnum, GET_MISMATCH); | |
534 | if (sra && sra->mismatch_cnt > 0) { | |
535 | char cnt[40]; | |
536 | sprintf(cnt, " mismatches found: %d", sra->mismatch_cnt); | |
537 | alert("RebuildFinished", dev, cnt, mailaddr, mailfrom, alert_cmd, dosyslog); | |
538 | } else | |
539 | alert("RebuildFinished", dev, NULL, mailaddr, mailfrom, alert_cmd, dosyslog); | |
540 | if (sra) | |
541 | free(sra); | |
542 | } | |
543 | st->percent = mse->percent; | |
544 | ||
545 | for (i=0; i<MaxDisks && i <= array.raid_disks + array.nr_disks; | |
546 | i++) { | |
547 | mdu_disk_info_t disc; | |
548 | disc.number = i; | |
549 | if (ioctl(fd, GET_DISK_INFO, &disc) >= 0) { | |
550 | info[i].state = disc.state; | |
551 | info[i].major = disc.major; | |
552 | info[i].minor = disc.minor; | |
553 | } else | |
554 | info[i].major = info[i].minor = 0; | |
555 | } | |
556 | ||
557 | if (strncmp(mse->metadata_version, "external:", 9) == 0 && | |
558 | is_subarray(mse->metadata_version+9)) | |
559 | st->parent_dev = | |
560 | devname2devnum(mse->metadata_version+10); | |
561 | else | |
562 | st->parent_dev = NoMdDev; | |
563 | if (st->metadata == NULL && | |
564 | st->parent_dev == NoMdDev) | |
565 | st->metadata = super_by_fd(fd, NULL); | |
566 | ||
567 | close(fd); | |
568 | ||
569 | for (i=0; i<MaxDisks; i++) { | |
570 | mdu_disk_info_t disc = {0,0,0,0,0}; | |
571 | int newstate=0; | |
572 | int change; | |
573 | char *dv = NULL; | |
574 | disc.number = i; | |
575 | if (i > array.raid_disks + array.nr_disks) { | |
576 | newstate = 0; | |
577 | disc.major = disc.minor = 0; | |
578 | } else if (info[i].major || info[i].minor) { | |
579 | newstate = info[i].state; | |
580 | dv = map_dev(info[i].major, info[i].minor, 1); | |
581 | disc.state = newstate; | |
582 | disc.major = info[i].major; | |
583 | disc.minor = info[i].minor; | |
584 | } else if (mse && mse->pattern && i < (int)strlen(mse->pattern)) { | |
585 | switch(mse->pattern[i]) { | |
586 | case 'U': newstate = 6 /* ACTIVE/SYNC */; break; | |
587 | case '_': newstate = 0; break; | |
588 | } | |
589 | disc.major = disc.minor = 0; | |
590 | } | |
591 | if (dv == NULL && st->devid[i]) | |
592 | dv = map_dev(major(st->devid[i]), | |
593 | minor(st->devid[i]), 1); | |
594 | change = newstate ^ st->devstate[i]; | |
595 | if (st->utime && change && !st->err) { | |
596 | if (i < array.raid_disks && | |
597 | (((newstate&change)&(1<<MD_DISK_FAULTY)) || | |
598 | ((st->devstate[i]&change)&(1<<MD_DISK_ACTIVE)) || | |
599 | ((st->devstate[i]&change)&(1<<MD_DISK_SYNC))) | |
600 | ) | |
601 | alert("Fail", dev, dv, mailaddr, mailfrom, alert_cmd, dosyslog); | |
602 | else if (i >= array.raid_disks && | |
603 | (disc.major || disc.minor) && | |
604 | st->devid[i] == makedev(disc.major, disc.minor) && | |
605 | ((newstate&change)&(1<<MD_DISK_FAULTY)) | |
606 | ) | |
607 | alert("FailSpare", dev, dv, mailaddr, mailfrom, alert_cmd, dosyslog); | |
608 | else if (i < array.raid_disks && | |
609 | ! (newstate & (1<<MD_DISK_REMOVED)) && | |
610 | (((st->devstate[i]&change)&(1<<MD_DISK_FAULTY)) || | |
611 | ((newstate&change)&(1<<MD_DISK_ACTIVE)) || | |
612 | ((newstate&change)&(1<<MD_DISK_SYNC))) | |
613 | ) | |
614 | alert("SpareActive", dev, dv, mailaddr, mailfrom, alert_cmd, dosyslog); | |
615 | } | |
616 | st->devstate[i] = newstate; | |
617 | st->devid[i] = makedev(disc.major, disc.minor); | |
618 | } | |
619 | st->active = array.active_disks; | |
620 | st->working = array.working_disks; | |
621 | st->spare = array.spare_disks; | |
622 | st->failed = array.failed_disks; | |
623 | st->utime = array.utime; | |
624 | st->raid = array.raid_disks; | |
625 | st->err = 0; | |
626 | } | |
627 | ||
628 | static int add_new_arrays(struct mdstat_ent *mdstat, struct state *statelist, | |
629 | int test, char *mailaddr, char *mailfrom, | |
630 | char *alert_cmd, int dosyslog) | |
631 | { | |
632 | struct mdstat_ent *mse; | |
633 | int new_found = 0; | |
634 | ||
635 | for (mse=mdstat; mse; mse=mse->next) | |
636 | if (mse->devnum != INT_MAX && | |
637 | (!mse->level || /* retrieve containers */ | |
638 | (strcmp(mse->level, "raid0") != 0 && | |
639 | strcmp(mse->level, "linear") != 0)) | |
640 | ) { | |
641 | struct state *st = malloc(sizeof *st); | |
642 | mdu_array_info_t array; | |
643 | int fd; | |
644 | if (st == NULL) | |
645 | continue; | |
646 | st->devname = strdup(get_md_name(mse->devnum)); | |
647 | if ((fd = open(st->devname, O_RDONLY)) < 0 || | |
648 | ioctl(fd, GET_ARRAY_INFO, &array)< 0) { | |
649 | /* no such array */ | |
650 | if (fd >=0) close(fd); | |
651 | put_md_name(st->devname); | |
652 | free(st->devname); | |
653 | if (st->metadata) { | |
654 | st->metadata->ss->free_super(st->metadata); | |
655 | free(st->metadata); | |
656 | } | |
657 | free(st); | |
658 | continue; | |
659 | } | |
660 | close(fd); | |
661 | st->utime = 0; | |
662 | st->next = statelist; | |
663 | st->err = 1; | |
664 | st->devnum = mse->devnum; | |
665 | st->percent = -2; | |
666 | st->spare_group = NULL; | |
667 | st->expected_spares = -1; | |
668 | if (strncmp(mse->metadata_version, "external:", 9) == 0 && | |
669 | is_subarray(mse->metadata_version+9)) | |
670 | st->parent_dev = | |
671 | devname2devnum(mse->metadata_version+10); | |
672 | else | |
673 | st->parent_dev = NoMdDev; | |
674 | st->metadata = NULL; | |
675 | statelist = st; | |
676 | if (test) | |
677 | alert("TestMessage", st->devname, NULL, mailaddr, mailfrom, alert_cmd, dosyslog); | |
678 | alert("NewArray", st->devname, NULL, mailaddr, mailfrom, alert_cmd, dosyslog); | |
679 | new_found = 1; | |
680 | } | |
681 | return new_found; | |
682 | } | |
683 | ||
684 | static void try_spare_migration(struct state *statelist, | |
685 | char *mailaddr, char *mailfrom, | |
686 | char *alert_cmd, int dosyslog) | |
687 | { | |
688 | struct state *st; | |
c3621c0a ML |
689 | |
690 | link_containers_with_subarrays(statelist); | |
2e0172b1 N |
691 | for (st = statelist; st; st=st->next) |
692 | if (st->active < st->raid && | |
693 | st->spare == 0 && | |
694 | st->spare_group != NULL) { | |
695 | struct state *st2; | |
696 | for (st2=statelist ; st2 ; st2=st2->next) | |
697 | if (st2 != st && | |
698 | st2->spare > 0 && | |
699 | st2->active == st2->raid && | |
700 | st2->spare_group != NULL && | |
701 | strcmp(st->spare_group, st2->spare_group) == 0) { | |
702 | /* try to remove and add */ | |
703 | int fd1 = open(st->devname, O_RDONLY); | |
704 | int fd2 = open(st2->devname, O_RDONLY); | |
705 | int dev = -1; | |
706 | int d; | |
707 | if (fd1 < 0 || fd2 < 0) { | |
708 | if (fd1>=0) close(fd1); | |
709 | if (fd2>=0) close(fd2); | |
710 | continue; | |
711 | } | |
712 | for (d=st2->raid; d < MaxDisks; d++) { | |
713 | if (st2->devid[d] > 0 && | |
714 | st2->devstate[d] == 0) { | |
715 | dev = st2->devid[d]; | |
716 | break; | |
717 | } | |
718 | } | |
719 | if (dev > 0) { | |
720 | struct mddev_dev devlist; | |
721 | char devname[20]; | |
722 | devlist.next = NULL; | |
723 | devlist.used = 0; | |
724 | devlist.re_add = 0; | |
725 | devlist.writemostly = 0; | |
726 | devlist.devname = devname; | |
727 | sprintf(devname, "%d:%d", major(dev), minor(dev)); | |
728 | ||
729 | devlist.disposition = 'r'; | |
730 | if (Manage_subdevs(st2->devname, fd2, &devlist, -1, 0) == 0) { | |
731 | devlist.disposition = 'a'; | |
732 | if (Manage_subdevs(st->devname, fd1, &devlist, -1, 0) == 0) { | |
733 | alert("MoveSpare", st->devname, st2->devname, mailaddr, mailfrom, alert_cmd, dosyslog); | |
734 | close(fd1); | |
735 | close(fd2); | |
736 | break; | |
737 | } | |
738 | else Manage_subdevs(st2->devname, fd2, &devlist, -1, 0); | |
739 | } | |
740 | } | |
741 | close(fd1); | |
742 | close(fd2); | |
743 | } | |
744 | } | |
745 | } | |
c3621c0a ML |
746 | |
747 | /* search the statelist to connect external | |
748 | * metadata subarrays with their containers | |
749 | * We always completely rebuild the tree from scratch as | |
750 | * that is safest considering the possibility of entries | |
751 | * disappearing or changing. | |
752 | */ | |
753 | static void link_containers_with_subarrays(struct state *list) | |
754 | { | |
755 | struct state *st; | |
756 | struct state *cont; | |
757 | for (st = list; st; st = st->next) { | |
758 | st->parent = NULL; | |
759 | st->subarray = NULL; | |
760 | } | |
761 | for (st = list; st; st = st->next) | |
762 | if (st->parent_dev != NoMdDev) | |
763 | for (cont = list; cont; cont = cont->next) | |
764 | if (!cont->err && | |
765 | cont->parent_dev == NoMdDev && | |
766 | cont->devnum == st->parent_dev) { | |
767 | st->parent = cont; | |
768 | st->subarray = cont->subarray; | |
769 | cont->subarray = st; | |
770 | break; | |
771 | } | |
772 | } | |
773 | ||
b90c0e9a NB |
774 | /* Not really Monitor but ... */ |
775 | int Wait(char *dev) | |
776 | { | |
777 | struct stat stb; | |
778 | int devnum; | |
779 | int rv = 1; | |
780 | ||
781 | if (stat(dev, &stb) != 0) { | |
782 | fprintf(stderr, Name ": Cannot find %s: %s\n", dev, | |
783 | strerror(errno)); | |
784 | return 2; | |
785 | } | |
c94709e8 | 786 | devnum = stat2devnum(&stb); |
b90c0e9a NB |
787 | |
788 | while(1) { | |
789 | struct mdstat_ent *ms = mdstat_read(1, 0); | |
790 | struct mdstat_ent *e; | |
791 | ||
792 | for (e=ms ; e; e=e->next) | |
793 | if (e->devnum == devnum) | |
794 | break; | |
795 | ||
796 | if (!e || e->percent < 0) { | |
e7783ee6 | 797 | if (e && e->metadata_version && |
c94709e8 DW |
798 | strncmp(e->metadata_version, "external:", 9) == 0) { |
799 | if (is_subarray(&e->metadata_version[9])) | |
800 | ping_monitor(&e->metadata_version[9]); | |
801 | else | |
802 | ping_monitor(devnum2devname(devnum)); | |
803 | } | |
b90c0e9a NB |
804 | free_mdstat(ms); |
805 | return rv; | |
806 | } | |
89a10d84 | 807 | free_mdstat(ms); |
b90c0e9a NB |
808 | rv = 0; |
809 | mdstat_wait(5); | |
810 | } | |
811 | } |