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