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