]> git.ipfire.org Git - thirdparty/mdadm.git/blob - Incremental.c
Incremental: Factor out search of mdstat
[thirdparty/mdadm.git] / Incremental.c
1 /*
2 * Incremental.c - support --incremental. Part of:
3 * mdadm - manage Linux "md" devices aka RAID arrays.
4 *
5 * Copyright (C) 2006-2009 Neil Brown <neilb@suse.de>
6 *
7 *
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
21 *
22 * Author: Neil Brown
23 * Email: <neilb@suse.de>
24 * Paper: Neil Brown
25 * Novell Inc
26 * GPO Box Q1283
27 * QVB Post Office, NSW 1230
28 * Australia
29 */
30
31 #include "mdadm.h"
32 #include <dirent.h>
33 #include <ctype.h>
34
35 static int count_active(struct supertype *st, int mdfd, char **availp,
36 struct mdinfo *info);
37 static void find_reject(int mdfd, struct supertype *st, struct mdinfo *sra,
38 int number, __u64 events, int verbose,
39 char *array_name);
40 static int try_spare(char *devname, int *dfdp, struct dev_policy *pol,
41 struct supertype *st, int verbose);
42
43 static int Incremental_container(struct supertype *st, char *devname,
44 int verbose, int runstop, int autof,
45 int trustworthy);
46
47 static struct mddev_ident_s *search_mdstat(struct supertype *st,
48 struct mdinfo *info,
49 char *devname,
50 int verbose, int *rvp);
51
52 int Incremental(char *devname, int verbose, int runstop,
53 struct supertype *st, char *homehost, int require_homehost,
54 int autof)
55 {
56 /* Add this device to an array, creating the array if necessary
57 * and starting the array if sensible or - if runstop>0 - if possible.
58 *
59 * This has several steps:
60 *
61 * 1/ Check if device is permitted by mdadm.conf, reject if not.
62 * 2/ Find metadata, reject if none appropriate (check
63 * version/name from args)
64 * 3/ Check if there is a match in mdadm.conf
65 * 3a/ if not, check for homehost match. If no match, assemble as
66 * a 'foreign' array.
67 * 4/ Determine device number.
68 * - If in mdadm.conf with std name, use that
69 * - UUID in /var/run/mdadm.map use that
70 * - If name is suggestive, use that. unless in use with different uuid.
71 * - Choose a free, high number.
72 * - Use a partitioned device unless strong suggestion not to.
73 * e.g. auto=md
74 * Don't choose partitioned for containers.
75 * 5/ Find out if array already exists
76 * 5a/ if it does not
77 * - choose a name, from mdadm.conf or 'name' field in array.
78 * - create the array
79 * - add the device
80 * 5b/ if it does
81 * - check one drive in array to make sure metadata is a reasonably
82 * close match. Reject if not (e.g. different type)
83 * - add the device
84 * 6/ Make sure /var/run/mdadm.map contains this array.
85 * 7/ Is there enough devices to possibly start the array?
86 * For a container, this means running Incremental_container.
87 * 7a/ if not, finish with success.
88 * 7b/ if yes,
89 * - read all metadata and arrange devices like -A does
90 * - if number of OK devices match expected, or -R and there are enough,
91 * start the array (auto-readonly).
92 */
93 struct stat stb;
94 struct mdinfo info, dinfo;
95 struct mddev_ident_s *match;
96 char chosen_name[1024];
97 int rv = 1;
98 struct map_ent *mp, *map = NULL;
99 int dfd = -1, mdfd = -1;
100 char *avail;
101 int active_disks;
102 int trustworthy = FOREIGN;
103 char *name_to_use;
104 mdu_array_info_t ainf;
105 struct dev_policy *policy = NULL;
106
107 struct createinfo *ci = conf_get_create_info();
108
109
110 /* 1/ Check if device is permitted by mdadm.conf */
111
112 if (!conf_test_dev(devname)) {
113 if (verbose >= 0)
114 fprintf(stderr, Name
115 ": %s not permitted by mdadm.conf.\n",
116 devname);
117 goto out;
118 }
119
120 /* 2/ Find metadata, reject if none appropriate (check
121 * version/name from args) */
122
123 dfd = dev_open(devname, O_RDONLY|O_EXCL);
124 if (dfd < 0) {
125 if (verbose >= 0)
126 fprintf(stderr, Name ": cannot open %s: %s.\n",
127 devname, strerror(errno));
128 goto out;
129 }
130 if (fstat(dfd, &stb) < 0) {
131 if (verbose >= 0)
132 fprintf(stderr, Name ": fstat failed for %s: %s.\n",
133 devname, strerror(errno));
134 goto out;
135 }
136 if ((stb.st_mode & S_IFMT) != S_IFBLK) {
137 if (verbose >= 0)
138 fprintf(stderr, Name ": %s is not a block device.\n",
139 devname);
140 goto out;
141 }
142
143 dinfo.disk.major = major(stb.st_rdev);
144 dinfo.disk.minor = minor(stb.st_rdev);
145
146 policy = disk_policy(&dinfo);
147
148 if (st == NULL && (st = guess_super(dfd)) == NULL) {
149 if (verbose >= 0)
150 fprintf(stderr, Name
151 ": no recognisable superblock on %s.\n",
152 devname);
153 rv = try_spare(devname, &dfd, policy, st, verbose);
154 goto out;
155 }
156 if (st->ss->compare_super == NULL ||
157 st->ss->load_super(st, dfd, NULL)) {
158 if (verbose >= 0)
159 fprintf(stderr, Name ": no RAID superblock on %s.\n",
160 devname);
161 rv = try_spare(devname, &dfd, policy, st, verbose);
162 free(st);
163 goto out;
164 }
165 close (dfd); dfd = -1;
166
167 memset(&info, 0, sizeof(info));
168 st->ss->getinfo_super(st, &info, NULL);
169
170 /* 3/ Check if there is a match in mdadm.conf */
171 match = search_mdstat(st, &info, devname, verbose, &rv);
172 if (!match && rv == 2)
173 goto out;
174
175 if (match && match->devname
176 && strcasecmp(match->devname, "<ignore>") == 0) {
177 if (verbose >= 0)
178 fprintf(stderr, Name ": array containing %s is explicitly"
179 " ignored by mdadm.conf\n",
180 devname);
181 goto out;
182 }
183
184 /* 3a/ if not, check for homehost match. If no match, continue
185 * but don't trust the 'name' in the array. Thus a 'random' minor
186 * number will be assigned, and the device name will be based
187 * on that. */
188 if (match)
189 trustworthy = LOCAL;
190 else if (st->ss->match_home(st, homehost) == 1)
191 trustworthy = LOCAL;
192 else if (st->ss->match_home(st, "any") == 1)
193 trustworthy = LOCAL_ANY;
194 else
195 trustworthy = FOREIGN;
196
197
198 if (!match && !conf_test_metadata(st->ss->name, policy,
199 (trustworthy == LOCAL))) {
200 if (verbose >= 1)
201 fprintf(stderr, Name
202 ": %s has metadata type %s for which "
203 "auto-assembly is disabled\n",
204 devname, st->ss->name);
205 goto out;
206 }
207 if (trustworthy == LOCAL_ANY)
208 trustworthy = LOCAL;
209
210 /* There are three possible sources for 'autof': command line,
211 * ARRAY line in mdadm.conf, or CREATE line in mdadm.conf.
212 * ARRAY takes precedence, then command line, then
213 * CREATE.
214 */
215 if (match && match->autof)
216 autof = match->autof;
217 if (autof == 0)
218 autof = ci->autof;
219
220 if (st->ss->container_content && st->loaded_container) {
221 if ((runstop > 0 && info.container_enough >= 0) ||
222 info.container_enough > 0)
223 /* pass */;
224 else {
225 if (verbose)
226 fprintf(stderr, Name ": not enough devices to start the container\n");
227 rv = 0;
228 goto out;
229 }
230
231 /* This is a pre-built container array, so we do something
232 * rather different.
233 */
234 rv = Incremental_container(st, devname, verbose, runstop,
235 autof, trustworthy);
236 goto out;
237 }
238
239 name_to_use = info.name;
240 if (name_to_use[0] == 0 &&
241 info.array.level == LEVEL_CONTAINER &&
242 trustworthy == LOCAL) {
243 name_to_use = info.text_version;
244 trustworthy = METADATA;
245 }
246 if (name_to_use[0] && trustworthy != LOCAL &&
247 ! require_homehost &&
248 conf_name_is_free(name_to_use))
249 trustworthy = LOCAL;
250
251 /* strip "hostname:" prefix from name if we have decided
252 * to treat it as LOCAL
253 */
254 if (trustworthy == LOCAL && strchr(name_to_use, ':') != NULL)
255 name_to_use = strchr(name_to_use, ':')+1;
256
257 /* 4/ Check if array exists.
258 */
259 if (map_lock(&map))
260 fprintf(stderr, Name ": failed to get exclusive lock on "
261 "mapfile\n");
262 mp = map_by_uuid(&map, info.uuid);
263 if (mp)
264 mdfd = open_dev(mp->devnum);
265 else
266 mdfd = -1;
267
268 if (mdfd < 0) {
269 struct mdinfo *sra;
270
271 /* Couldn't find an existing array, maybe make a new one */
272 mdfd = create_mddev(match ? match->devname : NULL,
273 name_to_use, autof, trustworthy, chosen_name);
274
275 if (mdfd < 0)
276 goto out;
277
278 sysfs_init(&info, mdfd, 0);
279
280 if (set_array_info(mdfd, st, &info) != 0) {
281 fprintf(stderr, Name ": failed to set array info for %s: %s\n",
282 chosen_name, strerror(errno));
283 rv = 2;
284 goto out;
285 }
286
287 dinfo = info;
288 dinfo.disk.major = major(stb.st_rdev);
289 dinfo.disk.minor = minor(stb.st_rdev);
290 if (add_disk(mdfd, st, &info, &dinfo) != 0) {
291 fprintf(stderr, Name ": failed to add %s to %s: %s.\n",
292 devname, chosen_name, strerror(errno));
293 ioctl(mdfd, STOP_ARRAY, 0);
294 rv = 2;
295 goto out;
296 }
297 sra = sysfs_read(mdfd, fd2devnum(mdfd), GET_DEVS);
298 if (!sra || !sra->devs || sra->devs->disk.raid_disk >= 0) {
299 /* It really should be 'none' - must be old buggy
300 * kernel, and mdadm -I may not be able to complete.
301 * So reject it.
302 */
303 ioctl(mdfd, STOP_ARRAY, NULL);
304 fprintf(stderr, Name
305 ": You have an old buggy kernel which cannot support\n"
306 " --incremental reliably. Aborting.\n");
307 sysfs_free(sra);
308 rv = 2;
309 goto out;
310 }
311 info.array.working_disks = 1;
312 sysfs_free(sra);
313 /* 6/ Make sure /var/run/mdadm.map contains this array. */
314 map_update(&map, fd2devnum(mdfd),
315 info.text_version,
316 info.uuid, chosen_name);
317 } else {
318 /* 5b/ if it does */
319 /* - check one drive in array to make sure metadata is a reasonably */
320 /* close match. Reject if not (e.g. different type) */
321 /* - add the device */
322 char dn[20];
323 int dfd2;
324 int err;
325 struct mdinfo *sra;
326 struct supertype *st2;
327 struct mdinfo info2, *d;
328
329 if (mp->path)
330 strcpy(chosen_name, mp->path);
331 else
332 strcpy(chosen_name, devnum2devname(mp->devnum));
333
334 /* It is generally not OK to add non-spare drives to a
335 * running array as they are probably missing because
336 * they failed. However if runstop is 1, then the
337 * array was possibly started early and our best bet is
338 * to add this anyway.
339 * Also if action policy is re-add or better we allow
340 * re-add
341 */
342 if ((info.disk.state & (1<<MD_DISK_SYNC)) != 0
343 && ! policy_action_allows(policy, st->ss->name,
344 act_re_add)
345 && runstop < 1) {
346 int active = 0;
347
348 if (st->ss->external) {
349 char *devname = devnum2devname(fd2devnum(mdfd));
350
351 active = devname && is_container_active(devname);
352 free(devname);
353 } else if (ioctl(mdfd, GET_ARRAY_INFO, &ainf) == 0)
354 active = 1;
355 if (active) {
356 fprintf(stderr, Name
357 ": not adding %s to active array (without --run) %s\n",
358 devname, chosen_name);
359 rv = 2;
360 goto out;
361 }
362 }
363 sra = sysfs_read(mdfd, fd2devnum(mdfd), (GET_DEVS | GET_STATE));
364 if (!sra) {
365 rv = 2;
366 goto out;
367 }
368 if (sra->devs) {
369 sprintf(dn, "%d:%d", sra->devs->disk.major,
370 sra->devs->disk.minor);
371 dfd2 = dev_open(dn, O_RDONLY);
372 st2 = dup_super(st);
373 if (st2->ss->load_super(st2, dfd2, NULL) ||
374 st->ss->compare_super(st, st2) != 0) {
375 fprintf(stderr, Name
376 ": metadata mismatch between %s and "
377 "chosen array %s\n",
378 devname, chosen_name);
379 close(dfd2);
380 rv = 2;
381 goto out;
382 }
383 close(dfd2);
384 memset(&info2, 0, sizeof(info2));
385 st2->ss->getinfo_super(st2, &info2, NULL);
386 st2->ss->free_super(st2);
387 if (info.array.level != info2.array.level ||
388 memcmp(info.uuid, info2.uuid, 16) != 0 ||
389 info.array.raid_disks != info2.array.raid_disks) {
390 fprintf(stderr, Name
391 ": unexpected difference between %s and %s.\n",
392 chosen_name, devname);
393 rv = 2;
394 goto out;
395 }
396 }
397 info2.disk.major = major(stb.st_rdev);
398 info2.disk.minor = minor(stb.st_rdev);
399 /* add disk needs to know about containers */
400 if (st->ss->external)
401 sra->array.level = LEVEL_CONTAINER;
402 err = add_disk(mdfd, st, sra, &info2);
403 if (err < 0 && errno == EBUSY) {
404 /* could be another device present with the same
405 * disk.number. Find and reject any such
406 */
407 find_reject(mdfd, st, sra, info.disk.number,
408 info.events, verbose, chosen_name);
409 err = add_disk(mdfd, st, sra, &info2);
410 }
411 if (err < 0) {
412 fprintf(stderr, Name ": failed to add %s to %s: %s.\n",
413 devname, chosen_name, strerror(errno));
414 rv = 2;
415 goto out;
416 }
417 info.array.working_disks = 0;
418 for (d = sra->devs; d; d=d->next)
419 info.array.working_disks ++;
420
421 }
422
423 /* 7/ Is there enough devices to possibly start the array? */
424 /* 7a/ if not, finish with success. */
425 if (info.array.level == LEVEL_CONTAINER) {
426 /* Try to assemble within the container */
427 map_unlock(&map);
428 sysfs_uevent(&info, "change");
429 if (verbose >= 0)
430 fprintf(stderr, Name
431 ": container %s now has %d devices\n",
432 chosen_name, info.array.working_disks);
433 wait_for(chosen_name, mdfd);
434 close(mdfd);
435 rv = Incremental(chosen_name, verbose, runstop,
436 NULL, homehost, require_homehost, autof);
437 if (rv == 1)
438 /* Don't fail the whole -I if a subarray didn't
439 * have enough devices to start yet
440 */
441 rv = 0;
442 return rv;
443 }
444 avail = NULL;
445 active_disks = count_active(st, mdfd, &avail, &info);
446 if (enough(info.array.level, info.array.raid_disks,
447 info.array.layout, info.array.state & 1,
448 avail, active_disks) == 0) {
449 free(avail);
450 if (verbose >= 0)
451 fprintf(stderr, Name
452 ": %s attached to %s, not enough to start (%d).\n",
453 devname, chosen_name, active_disks);
454 map_unlock(&map);
455 rv = 0;
456 goto out;
457 }
458 free(avail);
459
460 /* 7b/ if yes, */
461 /* - if number of OK devices match expected, or -R and there */
462 /* are enough, */
463 /* + add any bitmap file */
464 /* + start the array (auto-readonly). */
465
466 if (ioctl(mdfd, GET_ARRAY_INFO, &ainf) == 0) {
467 if (verbose >= 0)
468 fprintf(stderr, Name
469 ": %s attached to %s which is already active.\n",
470 devname, chosen_name);
471 map_unlock(&map);
472 rv = 0;
473 goto out;
474 }
475
476 map_unlock(&map);
477 if (runstop > 0 || active_disks >= info.array.working_disks) {
478 struct mdinfo *sra, *dsk;
479 /* Let's try to start it */
480 if (match && match->bitmap_file) {
481 int bmfd = open(match->bitmap_file, O_RDWR);
482 if (bmfd < 0) {
483 fprintf(stderr, Name
484 ": Could not open bitmap file %s.\n",
485 match->bitmap_file);
486 goto out;
487 }
488 if (ioctl(mdfd, SET_BITMAP_FILE, bmfd) != 0) {
489 close(bmfd);
490 fprintf(stderr, Name
491 ": Failed to set bitmapfile for %s.\n",
492 chosen_name);
493 goto out;
494 }
495 close(bmfd);
496 }
497 /* GET_* needed so add_disk works below */
498 sra = sysfs_read(mdfd, fd2devnum(mdfd),
499 GET_DEVS|GET_OFFSET|GET_SIZE|GET_STATE);
500 if ((sra == NULL || active_disks >= info.array.working_disks)
501 && trustworthy != FOREIGN)
502 rv = ioctl(mdfd, RUN_ARRAY, NULL);
503 else
504 rv = sysfs_set_str(sra, NULL,
505 "array_state", "read-auto");
506 if (rv == 0) {
507 if (verbose >= 0)
508 fprintf(stderr, Name
509 ": %s attached to %s, which has been started.\n",
510 devname, chosen_name);
511 rv = 0;
512 wait_for(chosen_name, mdfd);
513 /* We just started the array, so some devices
514 * might have been evicted from the array
515 * because their event counts were too old.
516 * If the action=re-add policy is in-force for
517 * those devices we should re-add them now.
518 */
519 for (dsk = sra->devs; dsk ; dsk = dsk->next) {
520 if (disk_action_allows(dsk, st->ss->name, act_re_add) &&
521 add_disk(mdfd, st, sra, dsk) == 0)
522 fprintf(stderr, Name
523 ": %s re-added to %s\n",
524 dsk->sys_name, chosen_name);
525 }
526 } else {
527 fprintf(stderr, Name
528 ": %s attached to %s, but failed to start: %s.\n",
529 devname, chosen_name, strerror(errno));
530 rv = 1;
531 }
532 } else {
533 if (verbose >= 0)
534 fprintf(stderr, Name
535 ": %s attached to %s, not enough to start safely.\n",
536 devname, chosen_name);
537 rv = 0;
538 }
539 out:
540 if (dfd >= 0)
541 close(dfd);
542 if (mdfd >= 0)
543 close(mdfd);
544 if (policy)
545 dev_policy_free(policy);
546 return rv;
547 }
548
549 static struct mddev_ident_s *search_mdstat(struct supertype *st,
550 struct mdinfo *info,
551 char *devname,
552 int verbose, int *rvp)
553 {
554 struct mddev_ident_s *array_list, *match;
555 array_list = conf_get_ident(NULL);
556 match = NULL;
557 for (; array_list; array_list = array_list->next) {
558 if (array_list->uuid_set &&
559 same_uuid(array_list->uuid, info->uuid, st->ss->swapuuid)
560 == 0) {
561 if (verbose >= 2 && array_list->devname)
562 fprintf(stderr, Name
563 ": UUID differs from %s.\n",
564 array_list->devname);
565 continue;
566 }
567 if (array_list->name[0] &&
568 strcasecmp(array_list->name, info->name) != 0) {
569 if (verbose >= 2 && array_list->devname)
570 fprintf(stderr, Name
571 ": Name differs from %s.\n",
572 array_list->devname);
573 continue;
574 }
575 if (array_list->devices &&
576 !match_oneof(array_list->devices, devname)) {
577 if (verbose >= 2 && array_list->devname)
578 fprintf(stderr, Name
579 ": Not a listed device for %s.\n",
580 array_list->devname);
581 continue;
582 }
583 if (array_list->super_minor != UnSet &&
584 array_list->super_minor != info->array.md_minor) {
585 if (verbose >= 2 && array_list->devname)
586 fprintf(stderr, Name
587 ": Different super-minor to %s.\n",
588 array_list->devname);
589 continue;
590 }
591 if (!array_list->uuid_set &&
592 !array_list->name[0] &&
593 !array_list->devices &&
594 array_list->super_minor == UnSet) {
595 if (verbose >= 2 && array_list->devname)
596 fprintf(stderr, Name
597 ": %s doesn't have any identifying information.\n",
598 array_list->devname);
599 continue;
600 }
601 /* FIXME, should I check raid_disks and level too?? */
602
603 if (match) {
604 if (verbose >= 0) {
605 if (match->devname && array_list->devname)
606 fprintf(stderr, Name
607 ": we match both %s and %s - cannot decide which to use.\n",
608 match->devname, array_list->devname);
609 else
610 fprintf(stderr, Name
611 ": multiple lines in mdadm.conf match\n");
612 }
613 *rvp = 2;
614 match = NULL;
615 break;
616 }
617 match = array_list;
618 }
619 return match;
620 }
621
622 static void find_reject(int mdfd, struct supertype *st, struct mdinfo *sra,
623 int number, __u64 events, int verbose,
624 char *array_name)
625 {
626 /* Find a device attached to this array with a disk.number of number
627 * and events less than the passed events, and remove the device.
628 */
629 struct mdinfo *d;
630 mdu_array_info_t ra;
631
632 if (ioctl(mdfd, GET_ARRAY_INFO, &ra) == 0)
633 return; /* not safe to remove from active arrays
634 * without thinking more */
635
636 for (d = sra->devs; d ; d = d->next) {
637 char dn[10];
638 int dfd;
639 struct mdinfo info;
640 sprintf(dn, "%d:%d", d->disk.major, d->disk.minor);
641 dfd = dev_open(dn, O_RDONLY);
642 if (dfd < 0)
643 continue;
644 if (st->ss->load_super(st, dfd, NULL)) {
645 close(dfd);
646 continue;
647 }
648 st->ss->getinfo_super(st, &info, NULL);
649 st->ss->free_super(st);
650 close(dfd);
651
652 if (info.disk.number != number ||
653 info.events >= events)
654 continue;
655
656 if (d->disk.raid_disk > -1)
657 sysfs_set_str(sra, d, "slot", "none");
658 if (sysfs_set_str(sra, d, "state", "remove") == 0)
659 if (verbose >= 0)
660 fprintf(stderr, Name
661 ": removing old device %s from %s\n",
662 d->sys_name+4, array_name);
663 }
664 }
665
666 static int count_active(struct supertype *st, int mdfd, char **availp,
667 struct mdinfo *bestinfo)
668 {
669 /* count how many devices in sra think they are active */
670 struct mdinfo *d;
671 int cnt = 0, cnt1 = 0;
672 __u64 max_events = 0;
673 struct mdinfo *sra = sysfs_read(mdfd, -1, GET_DEVS | GET_STATE);
674 char *avail = NULL;
675
676 if (!sra)
677 return 0;
678
679 for (d = sra->devs ; d ; d = d->next) {
680 char dn[30];
681 int dfd;
682 int ok;
683 struct mdinfo info;
684
685 sprintf(dn, "%d:%d", d->disk.major, d->disk.minor);
686 dfd = dev_open(dn, O_RDONLY);
687 if (dfd < 0)
688 continue;
689 ok = st->ss->load_super(st, dfd, NULL);
690 close(dfd);
691 if (ok != 0)
692 continue;
693 st->ss->getinfo_super(st, &info, NULL);
694 if (!avail) {
695 avail = malloc(info.array.raid_disks);
696 if (!avail) {
697 fprintf(stderr, Name ": out of memory.\n");
698 exit(1);
699 }
700 memset(avail, 0, info.array.raid_disks);
701 *availp = avail;
702 }
703
704 if (info.disk.state & (1<<MD_DISK_SYNC))
705 {
706 if (cnt == 0) {
707 cnt++;
708 max_events = info.events;
709 avail[info.disk.raid_disk] = 2;
710 st->ss->getinfo_super(st, bestinfo, NULL);
711 } else if (info.events == max_events) {
712 cnt++;
713 avail[info.disk.raid_disk] = 2;
714 } else if (info.events == max_events-1) {
715 cnt1++;
716 avail[info.disk.raid_disk] = 1;
717 } else if (info.events < max_events - 1)
718 ;
719 else if (info.events == max_events+1) {
720 int i;
721 cnt1 = cnt;
722 cnt = 1;
723 max_events = info.events;
724 for (i=0; i<info.array.raid_disks; i++)
725 if (avail[i])
726 avail[i]--;
727 avail[info.disk.raid_disk] = 2;
728 st->ss->getinfo_super(st, bestinfo, NULL);
729 } else { /* info.events much bigger */
730 cnt = 1; cnt1 = 0;
731 memset(avail, 0, info.disk.raid_disk);
732 max_events = info.events;
733 avail[info.disk.raid_disk] = 2;
734 st->ss->getinfo_super(st, bestinfo, NULL);
735 }
736 }
737 st->ss->free_super(st);
738 }
739 return cnt + cnt1;
740 }
741
742 static int array_try_spare(char *devname, int *dfdp, struct dev_policy *pol,
743 struct supertype *st, int verbose)
744 {
745 /* This device doesn't have any md metadata
746 * If it is 'bare' and theh device policy allows 'spare' look for
747 * an array or container to attach it to.
748 * If st is set, then only arrays of that type are considered
749 * Return 0 on success, or some exit code on failure, probably 1.
750 */
751 int rv = -1;
752 struct stat stb;
753 struct map_ent *mp, *map = NULL;
754 struct mdinfo *chosen = NULL;
755 int dfd = *dfdp;
756
757 if (fstat(dfd, &stb) != 0)
758 return 1;
759
760 /*
761 * Now we need to find a suitable array to add this to.
762 * We only accept arrays that:
763 * - match 'st'
764 * - are in the same domains as the device
765 * - are of an size for which the device will be useful
766 * and we choose the one that is the most degraded
767 */
768
769 if (map_lock(&map)) {
770 fprintf(stderr, Name ": failed to get exclusive lock on "
771 "mapfile\n");
772 return 1;
773 }
774 for (mp = map ; mp ; mp = mp->next) {
775 struct supertype *st2;
776 struct domainlist *dl = NULL;
777 struct mdinfo *sra;
778 unsigned long long devsize;
779
780 if (is_subarray(mp->metadata))
781 continue;
782 if (st) {
783 st2 = st->ss->match_metadata_desc(mp->metadata);
784 if (!st2 ||
785 (st->minor_version >= 0 &&
786 st->minor_version != st2->minor_version)) {
787 if (verbose > 1)
788 fprintf(stderr, Name ": not adding %s to %s as metadata type doesn't match\n",
789 devname, mp->path);
790 free(st2);
791 continue;
792 }
793 free(st2);
794 }
795 sra = sysfs_read(-1, mp->devnum,
796 GET_DEVS|GET_OFFSET|GET_SIZE|GET_STATE|
797 GET_DEGRADED|GET_COMPONENT|GET_VERSION);
798 if (!sra) {
799 /* Probably a container - no degraded info */
800 sra = sysfs_read(-1, mp->devnum,
801 GET_DEVS|GET_OFFSET|GET_SIZE|GET_STATE|
802 GET_COMPONENT|GET_VERSION);
803 if (sra)
804 sra->array.failed_disks = 0;
805 }
806 if (!sra)
807 continue;
808 if (st == NULL) {
809 int i;
810 st2 = NULL;
811 for(i=0; !st2 && superlist[i]; i++)
812 st2 = superlist[i]->match_metadata_desc(
813 sra->text_version);
814 } else
815 st2 = st;
816 get_dev_size(dfd, NULL, &devsize);
817 if (st2->ss->avail_size(st2, devsize) < sra->component_size) {
818 if (verbose > 1)
819 fprintf(stderr, Name ": not adding %s to %s as it is too small\n",
820 devname, mp->path);
821 goto next;
822 }
823 dl = domain_from_array(sra, st2->ss->name);
824 if (!domain_test(dl, pol, st2->ss->name)) {
825 /* domain test fails */
826 if (verbose > 1)
827 fprintf(stderr, Name ": not adding %s to %s as it is not in a compatible domain\n",
828 devname, mp->path);
829
830 goto next;
831 }
832 /* all tests passed, OK to add to this array */
833 if (!chosen) {
834 chosen = sra;
835 sra = NULL;
836 } else if (chosen->array.failed_disks < sra->array.failed_disks) {
837 sysfs_free(chosen);
838 chosen = sra;
839 sra = NULL;
840 }
841 next:
842 if (sra)
843 sysfs_free(sra);
844 if (st != st2)
845 free(st2);
846 if (dl)
847 domain_free(dl);
848 }
849 if (chosen) {
850 /* add current device to chosen array as a spare */
851 int mdfd = open_dev(devname2devnum(chosen->sys_name));
852 if (mdfd >= 0) {
853 struct mddev_dev_s devlist;
854 char devname[20];
855 devlist.next = NULL;
856 devlist.used = 0;
857 devlist.re_add = 0;
858 devlist.writemostly = 0;
859 devlist.devname = devname;
860 sprintf(devname, "%d:%d", major(stb.st_rdev),
861 minor(stb.st_rdev));
862 devlist.disposition = 'a';
863 close(dfd);
864 *dfdp = -1;
865 rv = Manage_subdevs(chosen->sys_name, mdfd, &devlist,
866 -1, 0);
867 close(mdfd);
868 }
869 if (verbose > 0) {
870 if (rv == 0)
871 fprintf(stderr, Name ": added %s as spare for %s\n",
872 devname, chosen->sys_name);
873 else
874 fprintf(stderr, Name ": failed to add %s as spare for %s\n",
875 devname, chosen->sys_name);
876 }
877 sysfs_free(chosen);
878 }
879 return rv ? 0 : 1;
880 }
881
882 static int partition_try_spare(char *devname, int *dfdp, struct dev_policy *pol,
883 struct supertype *st, int verbose)
884 {
885 /* we know that at least one partition virtual-metadata is
886 * allowed to incorporate spares like this device. We need to
887 * find a suitable device to copy partition information from.
888 *
889 * Getting a list of all disk (not partition) devices is
890 * slightly non-trivial. We could look at /sys/block, but
891 * that is theoretically due to be removed. Maybe best to use
892 * /dev/disk/by-path/?* and ignore names ending '-partNN' as
893 * we depend on this directory of 'path' info. But that fails
894 * to find loop devices and probably others. Maybe don't
895 * worry about that, they aren't the real target.
896 *
897 * So: check things in /dev/disk/by-path to see if they are in
898 * a compatible domain, then load the partition table and see
899 * if it is OK for the new device, and choose the largest
900 * partition table that fits.
901 */
902 DIR *dir;
903 struct dirent *de;
904 char *chosen = NULL;
905 unsigned long long chosen_size;
906 struct supertype *chosen_st = NULL;
907 int fd;
908
909 dir = opendir("/dev/disk/by-path");
910 if (!dir)
911 return 1;
912 while ((de = readdir(dir)) != NULL) {
913 char *ep;
914 struct dev_policy *pol2 = NULL;
915 struct domainlist *domlist = NULL;
916 int fd = -1;
917 struct mdinfo info;
918 struct supertype *st2 = NULL;
919 char *devname = NULL;
920 unsigned long long devsectors;
921
922 if (de->d_ino == 0 ||
923 de->d_name[0] == '.' ||
924 (de->d_type != DT_LNK && de->d_type != DT_UNKNOWN))
925 goto next;
926
927 ep = de->d_name + strlen(de->d_name);
928 while (ep > de->d_name &&
929 isdigit(ep[-1]))
930 ep--;
931 if (ep > de->d_name + 5 &&
932 strncmp(ep-5, "-part", 5) == 0)
933 /* This is a partition - skip it */
934 goto next;
935
936 pol2 = path_policy(de->d_name, type_disk);
937
938 domain_merge(&domlist, pol2, st ? st->ss->name : NULL);
939 if (domain_test(domlist, pol, st ? st->ss->name : NULL) == 0)
940 /* new device is incompatible with this device. */
941 goto next;
942
943 domain_free(domlist);
944 domlist = NULL;
945
946 asprintf(&devname, "/dev/disk/by-path/%s", de->d_name);
947 fd = open(devname, O_RDONLY);
948 if (fd < 0)
949 goto next;
950 if (get_dev_size(fd, devname, &devsectors) == 0)
951 goto next;
952 devsectors >>= 9;
953
954 if (st)
955 st2 = dup_super(st);
956 else
957 st2 = guess_super_type(fd, guess_partitions);
958 if (st2 == NULL ||
959 st2->ss->load_super(st2, fd, NULL) < 0)
960 goto next;
961
962 if (!st) {
963 /* Check domain policy again, this time referring to metadata */
964 domain_merge(&domlist, pol2, st2->ss->name);
965 if (domain_test(domlist, pol, st2->ss->name) == 0)
966 /* Incompatible devices for this metadata type */
967 goto next;
968 }
969
970 st2->ss->getinfo_super(st2, &info, NULL);
971 if (info.component_size > devsectors)
972 /* This partitioning doesn't fit in the device */
973 goto next;
974
975 /* This is an acceptable device to copy partition
976 * metadata from. We could just stop here, but I
977 * think I want to keep looking incase a larger
978 * metadata which makes better use of the device can
979 * be found.
980 */
981 if (chosen == NULL ||
982 chosen_size < info.component_size) {
983 chosen_size = info.component_size;
984 free(chosen);
985 chosen = devname;
986 devname = NULL;
987 if (chosen_st) {
988 chosen_st->ss->free_super(chosen_st);
989 free(chosen_st);
990 }
991 chosen_st = st2;
992 st2 = NULL;
993 }
994
995 next:
996 free(devname);
997 domain_free(domlist);
998 dev_policy_free(pol2);
999 if (st2)
1000 st2->ss->free_super(st2);
1001 free(st2);
1002
1003 if (fd >= 0)
1004 close(fd);
1005 }
1006
1007 if (!chosen)
1008 return 1;
1009
1010 /* 'chosen' is the best device we can find. Let's write its
1011 * metadata to devname dfd is read-only so don't use that
1012 */
1013 fd = open(devname, O_RDWR);
1014 if (fd >= 0) {
1015 chosen_st->ss->store_super(chosen_st, fd);
1016 close(fd);
1017 }
1018 free(chosen);
1019 chosen_st->ss->free_super(chosen_st);
1020 free(chosen_st);
1021 return 0;
1022 }
1023
1024
1025 /* adding a spare to a regular array is quite different from adding one to
1026 * a set-of-partitions virtual array.
1027 * This function determines which is worth trying and tries as appropriate.
1028 * Arrays are given priority over partitions.
1029 */
1030 static int try_spare(char *devname, int *dfdp, struct dev_policy *pol,
1031 struct supertype *st, int verbose)
1032 {
1033 int i;
1034 int rv;
1035 int arrays_ok = 0;
1036 int partitions_ok = 0;
1037 char bufpad[4096 + 4096];
1038 char *buf = (char*)(((long)bufpad + 4096) & ~4095);
1039 int dfd = *dfdp;
1040
1041 /* Can only add a spare if device has at least one domains */
1042 if (pol_find(pol, pol_domain) == NULL)
1043 return 1;
1044 /* And only if some action allows spares */
1045 if (!policy_action_allows(pol, st?st->ss->name:NULL, act_spare))
1046 return 1;
1047
1048 /* Now check if the device is bare - we don't add non-bare devices
1049 * yet even if action=-spare
1050 */
1051
1052 if (lseek(dfd, 0, SEEK_SET) != 0 ||
1053 read(dfd, buf, 4096) != 4096) {
1054 not_bare:
1055 if (verbose > 1)
1056 fprintf(stderr, Name ": %s is not bare, so not considering as a spare\n",
1057 devname);
1058 return 1;
1059 }
1060 if (buf[0] != '\0' && buf[0] != '\x5a' && buf[0] != '\xff')
1061 goto not_bare;
1062 if (memcmp(buf, buf+1, 4095) != 0)
1063 goto not_bare;
1064
1065 /* OK, first 4K appear blank, try the end. */
1066 if (lseek(dfd, -4096, SEEK_END) < 0 ||
1067 read(dfd, buf, 4096) != 4096)
1068 goto not_bare;
1069
1070 if (buf[0] != '\0' && buf[0] != '\x5a' && buf[0] != '\xff')
1071 goto not_bare;
1072 if (memcmp(buf, buf+1, 4095) != 0)
1073 goto not_bare;
1074
1075 /* This device passes our test for 'is bare'.
1076 * Let's see what policy allows for such things.
1077 */
1078 if (st) {
1079 /* just try try 'array' or 'partition' based on this metadata */
1080 if (st->ss->add_to_super)
1081 return array_try_spare(devname, dfdp, pol,
1082 st, verbose);
1083 else
1084 return partition_try_spare(devname, dfdp, pol,
1085 st, verbose);
1086 }
1087 /* Now see which metadata type support spare */
1088 for (i = 0; (!arrays_ok || !partitions_ok) && superlist[i] ; i++) {
1089 if (superlist[i]->add_to_super && !arrays_ok &&
1090 policy_action_allows(pol, superlist[i]->name, act_spare))
1091 arrays_ok = 1;
1092 if (superlist[i]->add_to_super == NULL && !partitions_ok &&
1093 policy_action_allows(pol, superlist[i]->name, act_spare))
1094 partitions_ok = 1;
1095 }
1096 rv = 0;
1097 if (arrays_ok)
1098 rv = array_try_spare(devname, dfdp, pol, st, verbose);
1099 if (rv == 0 && partitions_ok)
1100 rv = partition_try_spare(devname, dfdp, pol, st, verbose);
1101 return rv;
1102 }
1103
1104 int IncrementalScan(int verbose)
1105 {
1106 /* look at every device listed in the 'map' file.
1107 * If one is found that is not running then:
1108 * look in mdadm.conf for bitmap file.
1109 * if one exists, but array has none, add it.
1110 * try to start array in auto-readonly mode
1111 */
1112 struct map_ent *mapl = NULL;
1113 struct map_ent *me;
1114 mddev_ident_t devs, mddev;
1115 int rv = 0;
1116
1117 map_read(&mapl);
1118 devs = conf_get_ident(NULL);
1119
1120 for (me = mapl ; me ; me = me->next) {
1121 mdu_array_info_t array;
1122 mdu_bitmap_file_t bmf;
1123 struct mdinfo *sra;
1124 int mdfd = open_dev(me->devnum);
1125
1126 if (mdfd < 0)
1127 continue;
1128 if (ioctl(mdfd, GET_ARRAY_INFO, &array) == 0 ||
1129 errno != ENODEV) {
1130 close(mdfd);
1131 continue;
1132 }
1133 /* Ok, we can try this one. Maybe it needs a bitmap */
1134 for (mddev = devs ; mddev ; mddev = mddev->next)
1135 if (mddev->devname && me->path
1136 && devname_matches(mddev->devname, me->path))
1137 break;
1138 if (mddev && mddev->bitmap_file) {
1139 /*
1140 * Note: early kernels will wrongly fail this, so it
1141 * is a hint only
1142 */
1143 int added = -1;
1144 if (ioctl(mdfd, GET_ARRAY_INFO, &bmf) < 0) {
1145 int bmfd = open(mddev->bitmap_file, O_RDWR);
1146 if (bmfd >= 0) {
1147 added = ioctl(mdfd, SET_BITMAP_FILE,
1148 bmfd);
1149 close(bmfd);
1150 }
1151 }
1152 if (verbose >= 0) {
1153 if (added == 0)
1154 fprintf(stderr, Name
1155 ": Added bitmap %s to %s\n",
1156 mddev->bitmap_file, me->path);
1157 else if (errno != EEXIST)
1158 fprintf(stderr, Name
1159 ": Failed to add bitmap to %s: %s\n",
1160 me->path, strerror(errno));
1161 }
1162 }
1163 sra = sysfs_read(mdfd, 0, 0);
1164 if (sra) {
1165 if (sysfs_set_str(sra, NULL,
1166 "array_state", "read-auto") == 0) {
1167 if (verbose >= 0)
1168 fprintf(stderr, Name
1169 ": started array %s\n",
1170 me->path ?: devnum2devname(me->devnum));
1171 } else {
1172 fprintf(stderr, Name
1173 ": failed to start array %s: %s\n",
1174 me->path ?: devnum2devname(me->devnum),
1175 strerror(errno));
1176 rv = 1;
1177 }
1178 }
1179 }
1180 return rv;
1181 }
1182
1183 static char *container2devname(char *devname)
1184 {
1185 char *mdname = NULL;
1186
1187 if (devname[0] == '/') {
1188 int fd = open(devname, O_RDONLY);
1189 if (fd >= 0) {
1190 mdname = devnum2devname(fd2devnum(fd));
1191 close(fd);
1192 }
1193 } else {
1194 int uuid[4];
1195 struct map_ent *mp, *map = NULL;
1196
1197 if (!parse_uuid(devname, uuid))
1198 return mdname;
1199 mp = map_by_uuid(&map, uuid);
1200 if (mp)
1201 mdname = devnum2devname(mp->devnum);
1202 map_free(map);
1203 }
1204
1205 return mdname;
1206 }
1207
1208 static int Incremental_container(struct supertype *st, char *devname, int verbose,
1209 int runstop, int autof, int trustworthy)
1210 {
1211 /* Collect the contents of this container and for each
1212 * array, choose a device name and assemble the array.
1213 */
1214
1215 struct mdinfo *list = st->ss->container_content(st, NULL);
1216 struct mdinfo *ra;
1217 struct map_ent *map = NULL;
1218
1219 if (map_lock(&map))
1220 fprintf(stderr, Name ": failed to get exclusive lock on "
1221 "mapfile\n");
1222
1223 for (ra = list ; ra ; ra = ra->next) {
1224 int mdfd;
1225 char chosen_name[1024];
1226 struct map_ent *mp;
1227 struct mddev_ident_s *match = NULL;
1228
1229 mp = map_by_uuid(&map, ra->uuid);
1230
1231 if (mp) {
1232 mdfd = open_dev(mp->devnum);
1233 if (mp->path)
1234 strcpy(chosen_name, mp->path);
1235 else
1236 strcpy(chosen_name, devnum2devname(mp->devnum));
1237 } else {
1238
1239 /* Check in mdadm.conf for container == devname and
1240 * member == ra->text_version after second slash.
1241 */
1242 char *sub = strchr(ra->text_version+1, '/');
1243 struct mddev_ident_s *array_list;
1244 if (sub) {
1245 sub++;
1246 array_list = conf_get_ident(NULL);
1247 } else
1248 array_list = NULL;
1249 for(; array_list ; array_list = array_list->next) {
1250 char *dn;
1251 if (array_list->member == NULL ||
1252 array_list->container == NULL)
1253 continue;
1254 if (strcmp(array_list->member, sub) != 0)
1255 continue;
1256 if (array_list->uuid_set &&
1257 !same_uuid(ra->uuid, array_list->uuid, st->ss->swapuuid))
1258 continue;
1259 dn = container2devname(array_list->container);
1260 if (dn == NULL)
1261 continue;
1262 if (strncmp(dn, ra->text_version+1,
1263 strlen(dn)) != 0 ||
1264 ra->text_version[strlen(dn)+1] != '/') {
1265 free(dn);
1266 continue;
1267 }
1268 free(dn);
1269 /* we have a match */
1270 match = array_list;
1271 if (verbose>0)
1272 fprintf(stderr, Name ": match found for member %s\n",
1273 array_list->member);
1274 break;
1275 }
1276
1277 if (match && match->devname &&
1278 strcasecmp(match->devname, "<ignore>") == 0) {
1279 if (verbose > 0)
1280 fprintf(stderr, Name ": array %s/%s is "
1281 "explicitly ignored by mdadm.conf\n",
1282 match->container, match->member);
1283 return 2;
1284 }
1285 if (match)
1286 trustworthy = LOCAL;
1287
1288 mdfd = create_mddev(match ? match->devname : NULL,
1289 ra->name,
1290 autof,
1291 trustworthy,
1292 chosen_name);
1293 }
1294
1295 if (mdfd < 0) {
1296 fprintf(stderr, Name ": failed to open %s: %s.\n",
1297 chosen_name, strerror(errno));
1298 return 2;
1299 }
1300
1301 assemble_container_content(st, mdfd, ra, runstop,
1302 chosen_name, verbose);
1303 }
1304 map_unlock(&map);
1305 return 0;
1306 }
1307
1308 /*
1309 * IncrementalRemove - Attempt to see if the passed in device belongs to any
1310 * raid arrays, and if so first fail (if needed) and then remove the device.
1311 *
1312 * @devname - The device we want to remove
1313 *
1314 * Note: the device name must be a kernel name like "sda", so
1315 * that we can find it in /proc/mdstat
1316 */
1317 int IncrementalRemove(char *devname, int verbose)
1318 {
1319 int mdfd;
1320 int rv;
1321 struct mdstat_ent *ent;
1322 struct mddev_dev_s devlist;
1323
1324 if (strchr(devname, '/')) {
1325 fprintf(stderr, Name ": incremental removal requires a "
1326 "kernel device name, not a file: %s\n", devname);
1327 return 1;
1328 }
1329 ent = mdstat_by_component(devname);
1330 if (!ent) {
1331 fprintf(stderr, Name ": %s does not appear to be a component "
1332 "of any array\n", devname);
1333 return 1;
1334 }
1335 mdfd = open_dev(ent->devnum);
1336 if (mdfd < 0) {
1337 fprintf(stderr, Name ": Cannot open array %s!!\n", ent->dev);
1338 return 1;
1339 }
1340 memset(&devlist, 0, sizeof(devlist));
1341 devlist.devname = devname;
1342 devlist.disposition = 'f';
1343 Manage_subdevs(ent->dev, mdfd, &devlist, verbose, 0);
1344 devlist.disposition = 'r';
1345 rv = Manage_subdevs(ent->dev, mdfd, &devlist, verbose, 0);
1346 close(mdfd);
1347 return rv;
1348 }