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