]> git.ipfire.org Git - thirdparty/mdadm.git/blob - Incremental.c
Add support for auto-partitioning base devices.
[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);
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,
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);
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);
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);
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);
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);
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 st->ss->getinfo_super(st, bestinfo);
712 }
713 }
714 st->ss->free_super(st);
715 }
716 return cnt + cnt1;
717 }
718
719 static int array_try_spare(char *devname, int *dfdp, struct dev_policy *pol,
720 struct supertype *st, int verbose)
721 {
722 /* This device doesn't have any md metadata
723 * If it is 'bare' and theh device policy allows 'spare' look for
724 * an array or container to attach it to.
725 * If st is set, then only arrays of that type are considered
726 * Return 0 on success, or some exit code on failure, probably 1.
727 */
728 int rv = -1;
729 struct stat stb;
730 struct map_ent *mp, *map = NULL;
731 struct mdinfo *chosen = NULL;
732 int dfd = *dfdp;
733
734 if (fstat(dfd, &stb) != 0)
735 return 1;
736
737 /*
738 * Now we need to find a suitable array to add this to.
739 * We only accept arrays that:
740 * - match 'st'
741 * - are in the same domains as the device
742 * - are of an size for which the device will be useful
743 * and we choose the one that is the most degraded
744 */
745
746 if (map_lock(&map)) {
747 fprintf(stderr, Name ": failed to get exclusive lock on "
748 "mapfile\n");
749 return 1;
750 }
751 for (mp = map ; mp ; mp = mp->next) {
752 struct supertype *st2;
753 struct domainlist *dl = NULL;
754 struct mdinfo *sra;
755 unsigned long long devsize;
756
757 if (is_subarray(mp->metadata))
758 continue;
759 if (st) {
760 st2 = st->ss->match_metadata_desc(mp->metadata);
761 if (!st2 ||
762 (st->minor_version >= 0 &&
763 st->minor_version != st2->minor_version)) {
764 if (verbose > 1)
765 fprintf(stderr, Name ": not adding %s to %s as metadata type doesn't match\n",
766 devname, mp->path);
767 free(st2);
768 continue;
769 }
770 free(st2);
771 }
772 sra = sysfs_read(-1, mp->devnum,
773 GET_DEVS|GET_OFFSET|GET_SIZE|GET_STATE|
774 GET_DEGRADED|GET_COMPONENT|GET_VERSION);
775 if (!sra) {
776 /* Probably a container - no degraded info */
777 sra = sysfs_read(-1, mp->devnum,
778 GET_DEVS|GET_OFFSET|GET_SIZE|GET_STATE|
779 GET_COMPONENT|GET_VERSION);
780 if (sra)
781 sra->array.failed_disks = 0;
782 }
783 if (!sra)
784 continue;
785 if (st == NULL) {
786 int i;
787 st2 = NULL;
788 for(i=0; !st2 && superlist[i]; i++)
789 st2 = superlist[i]->match_metadata_desc(
790 sra->text_version);
791 } else
792 st2 = st;
793 get_dev_size(dfd, NULL, &devsize);
794 if (st2->ss->avail_size(st2, devsize) < sra->component_size) {
795 if (verbose > 1)
796 fprintf(stderr, Name ": not adding %s to %s as it is too small\n",
797 devname, mp->path);
798 goto next;
799 }
800 dl = domain_from_array(sra, st2->ss->name);
801 if (!domain_test(dl, pol, st2->ss->name)) {
802 /* domain test fails */
803 if (verbose > 1)
804 fprintf(stderr, Name ": not adding %s to %s as it is not in a compatible domain\n",
805 devname, mp->path);
806
807 goto next;
808 }
809 /* all tests passed, OK to add to this array */
810 if (!chosen) {
811 chosen = sra;
812 sra = NULL;
813 } else if (chosen->array.failed_disks < sra->array.failed_disks) {
814 sysfs_free(chosen);
815 chosen = sra;
816 sra = NULL;
817 }
818 next:
819 if (sra)
820 sysfs_free(sra);
821 if (st != st2)
822 free(st2);
823 if (dl)
824 domain_free(dl);
825 }
826 if (chosen) {
827 /* add current device to chosen array as a spare */
828 int mdfd = open_dev(devname2devnum(chosen->sys_name));
829 if (mdfd >= 0) {
830 struct mddev_dev_s devlist;
831 char devname[20];
832 devlist.next = NULL;
833 devlist.used = 0;
834 devlist.re_add = 0;
835 devlist.writemostly = 0;
836 devlist.devname = devname;
837 sprintf(devname, "%d:%d", major(stb.st_rdev),
838 minor(stb.st_rdev));
839 devlist.disposition = 'a';
840 close(dfd);
841 *dfdp = -1;
842 rv = Manage_subdevs(chosen->sys_name, mdfd, &devlist,
843 -1, 0);
844 close(mdfd);
845 }
846 if (verbose > 0) {
847 if (rv == 0)
848 fprintf(stderr, Name ": added %s as spare for %s\n",
849 devname, chosen->sys_name);
850 else
851 fprintf(stderr, Name ": failed to add %s as spare for %s\n",
852 devname, chosen->sys_name);
853 }
854 sysfs_free(chosen);
855 }
856 return rv ? 0 : 1;
857 }
858
859 static int partition_try_spare(char *devname, int *dfdp, struct dev_policy *pol,
860 struct supertype *st, int verbose)
861 {
862 /* we know that at least one partition virtual-metadata is
863 * allowed to incorporate spares like this device. We need to
864 * find a suitable device to copy partition information from.
865 *
866 * Getting a list of all disk (not partition) devices is
867 * slightly non-trivial. We could look at /sys/block, but
868 * that is theoretically due to be removed. Maybe best to use
869 * /dev/disk/by-path/?* and ignore names ending '-partNN' as
870 * we depend on this directory of 'path' info. But that fails
871 * to find loop devices and probably others. Maybe don't
872 * worry about that, they aren't the real target.
873 *
874 * So: check things in /dev/disk/by-path to see if they are in
875 * a compatible domain, then load the partition table and see
876 * if it is OK for the new device, and choose the largest
877 * partition table that fits.
878 */
879 DIR *dir;
880 struct dirent *de;
881 char *chosen = NULL;
882 unsigned long long chosen_size;
883 struct supertype *chosen_st = NULL;
884 int fd;
885
886 dir = opendir("/dev/disk/by-path");
887 if (!dir)
888 return 1;
889 while ((de = readdir(dir)) != NULL) {
890 char *ep;
891 struct dev_policy *pol2 = NULL;
892 struct domainlist *domlist = NULL;
893 int fd = -1;
894 struct mdinfo info;
895 struct supertype *st2 = NULL;
896 char *devname = NULL;
897 unsigned long long devsectors;
898
899 if (de->d_ino == 0 ||
900 de->d_name[0] == '.' ||
901 (de->d_type != DT_LNK && de->d_type != DT_UNKNOWN))
902 goto next;
903
904 ep = de->d_name + strlen(de->d_name);
905 while (ep > de->d_name &&
906 isdigit(ep[-1]))
907 ep--;
908 if (ep > de->d_name + 5 &&
909 strncmp(ep-5, "-part", 5) == 0)
910 /* This is a partition - skip it */
911 goto next;
912
913 pol2 = path_policy(de->d_name, type_disk);
914
915 domain_merge(&domlist, pol2, st ? st->ss->name : NULL);
916 if (domain_test(domlist, pol, st ? st->ss->name : NULL) == 0)
917 /* new device is incompatible with this device. */
918 goto next;
919
920 domain_free(domlist);
921 domlist = NULL;
922
923 asprintf(&devname, "/dev/disk/by-path/%s", de->d_name);
924 fd = open(devname, O_RDONLY);
925 if (fd < 0)
926 goto next;
927 if (get_dev_size(fd, devname, &devsectors) == 0)
928 goto next;
929 devsectors >>= 9;
930
931 if (st)
932 st2 = dup_super(st);
933 else
934 st2 = guess_super_type(fd, guess_partitions);
935 if (st2 == NULL ||
936 st2->ss->load_super(st2, fd, NULL) < 0)
937 goto next;
938
939 if (!st) {
940 /* Check domain policy again, this time referring to metadata */
941 domain_merge(&domlist, pol2, st2->ss->name);
942 if (domain_test(domlist, pol, st2->ss->name) == 0)
943 /* Incompatible devices for this metadata type */
944 goto next;
945 }
946
947 st2->ss->getinfo_super(st2, &info);
948 if (info.component_size > devsectors)
949 /* This partitioning doesn't fit in the device */
950 goto next;
951
952 /* This is an acceptable device to copy partition
953 * metadata from. We could just stop here, but I
954 * think I want to keep looking incase a larger
955 * metadata which makes better use of the device can
956 * be found.
957 */
958 if (chosen == NULL ||
959 chosen_size < info.component_size) {
960 chosen_size = info.component_size;
961 free(chosen);
962 chosen = devname;
963 devname = NULL;
964 if (chosen_st) {
965 chosen_st->ss->free_super(chosen_st);
966 free(chosen_st);
967 }
968 chosen_st = st2;
969 st2 = NULL;
970 }
971
972 next:
973 free(devname);
974 domain_free(domlist);
975 dev_policy_free(pol2);
976 if (st2)
977 st2->ss->free_super(st2);
978 free(st2);
979
980 if (fd >= 0)
981 close(fd);
982 }
983
984 if (!chosen)
985 return 1;
986
987 /* 'chosen' is the best device we can find. Let's write its
988 * metadata to devname dfd is read-only so don't use that
989 */
990 fd = open(devname, O_RDWR);
991 if (fd >= 0) {
992 chosen_st->ss->store_super(chosen_st, fd);
993 close(fd);
994 }
995 free(chosen);
996 chosen_st->ss->free_super(chosen_st);
997 free(chosen_st);
998 return 0;
999 }
1000
1001
1002 /* adding a spare to a regular array is quite different from adding one to
1003 * a set-of-partitions virtual array.
1004 * This function determines which is worth trying and tries as appropriate.
1005 * Arrays are given priority over partitions.
1006 */
1007 static int try_spare(char *devname, int *dfdp, struct dev_policy *pol,
1008 struct supertype *st, int verbose)
1009 {
1010 int i;
1011 int rv;
1012 int arrays_ok = 0;
1013 int partitions_ok = 0;
1014 char bufpad[4096 + 4096];
1015 char *buf = (char*)(((long)bufpad + 4096) & ~4095);
1016 int dfd = *dfdp;
1017
1018 /* Can only add a spare if device has at least one domains */
1019 if (pol_find(pol, pol_domain) == NULL)
1020 return 1;
1021 /* And only if some action allows spares */
1022 if (!policy_action_allows(pol, st?st->ss->name:NULL, act_spare))
1023 return 1;
1024
1025 /* Now check if the device is bare - we don't add non-bare devices
1026 * yet even if action=-spare
1027 */
1028
1029 if (lseek(dfd, 0, SEEK_SET) != 0 ||
1030 read(dfd, buf, 4096) != 4096) {
1031 not_bare:
1032 if (verbose > 1)
1033 fprintf(stderr, Name ": %s is not bare, so not considering as a spare\n",
1034 devname);
1035 return 1;
1036 }
1037 if (buf[0] != '\0' && buf[0] != '\x5a' && buf[0] != '\xff')
1038 goto not_bare;
1039 if (memcmp(buf, buf+1, 4095) != 0)
1040 goto not_bare;
1041
1042 /* OK, first 4K appear blank, try the end. */
1043 if (lseek(dfd, -4096, SEEK_END) < 0 ||
1044 read(dfd, buf, 4096) != 4096)
1045 goto not_bare;
1046
1047 if (buf[0] != '\0' && buf[0] != '\x5a' && buf[0] != '\xff')
1048 goto not_bare;
1049 if (memcmp(buf, buf+1, 4095) != 0)
1050 goto not_bare;
1051
1052 /* This device passes our test for 'is bare'.
1053 * Let's see what policy allows for such things.
1054 */
1055 if (st) {
1056 /* just try try 'array' or 'partition' based on this metadata */
1057 if (st->ss->add_to_super)
1058 return array_try_spare(devname, dfdp, pol,
1059 st, verbose);
1060 else
1061 return partition_try_spare(devname, dfdp, pol,
1062 st, verbose);
1063 }
1064 /* Now see which metadata type support spare */
1065 for (i = 0; (!arrays_ok || !partitions_ok) && superlist[i] ; i++) {
1066 if (superlist[i]->add_to_super && !arrays_ok &&
1067 policy_action_allows(pol, superlist[i]->name, act_spare))
1068 arrays_ok = 1;
1069 if (superlist[i]->add_to_super == NULL && !partitions_ok &&
1070 policy_action_allows(pol, superlist[i]->name, act_spare))
1071 partitions_ok = 1;
1072 }
1073 rv = 0;
1074 if (arrays_ok)
1075 rv = array_try_spare(devname, dfdp, pol, st, verbose);
1076 if (rv == 0 && partitions_ok)
1077 rv = partition_try_spare(devname, dfdp, pol, st, verbose);
1078 return rv;
1079 }
1080
1081 int IncrementalScan(int verbose)
1082 {
1083 /* look at every device listed in the 'map' file.
1084 * If one is found that is not running then:
1085 * look in mdadm.conf for bitmap file.
1086 * if one exists, but array has none, add it.
1087 * try to start array in auto-readonly mode
1088 */
1089 struct map_ent *mapl = NULL;
1090 struct map_ent *me;
1091 mddev_ident_t devs, mddev;
1092 int rv = 0;
1093
1094 map_read(&mapl);
1095 devs = conf_get_ident(NULL);
1096
1097 for (me = mapl ; me ; me = me->next) {
1098 mdu_array_info_t array;
1099 mdu_bitmap_file_t bmf;
1100 struct mdinfo *sra;
1101 int mdfd = open_dev(me->devnum);
1102
1103 if (mdfd < 0)
1104 continue;
1105 if (ioctl(mdfd, GET_ARRAY_INFO, &array) == 0 ||
1106 errno != ENODEV) {
1107 close(mdfd);
1108 continue;
1109 }
1110 /* Ok, we can try this one. Maybe it needs a bitmap */
1111 for (mddev = devs ; mddev ; mddev = mddev->next)
1112 if (mddev->devname && me->path
1113 && devname_matches(mddev->devname, me->path))
1114 break;
1115 if (mddev && mddev->bitmap_file) {
1116 /*
1117 * Note: early kernels will wrongly fail this, so it
1118 * is a hint only
1119 */
1120 int added = -1;
1121 if (ioctl(mdfd, GET_ARRAY_INFO, &bmf) < 0) {
1122 int bmfd = open(mddev->bitmap_file, O_RDWR);
1123 if (bmfd >= 0) {
1124 added = ioctl(mdfd, SET_BITMAP_FILE,
1125 bmfd);
1126 close(bmfd);
1127 }
1128 }
1129 if (verbose >= 0) {
1130 if (added == 0)
1131 fprintf(stderr, Name
1132 ": Added bitmap %s to %s\n",
1133 mddev->bitmap_file, me->path);
1134 else if (errno != EEXIST)
1135 fprintf(stderr, Name
1136 ": Failed to add bitmap to %s: %s\n",
1137 me->path, strerror(errno));
1138 }
1139 }
1140 sra = sysfs_read(mdfd, 0, 0);
1141 if (sra) {
1142 if (sysfs_set_str(sra, NULL,
1143 "array_state", "read-auto") == 0) {
1144 if (verbose >= 0)
1145 fprintf(stderr, Name
1146 ": started array %s\n",
1147 me->path ?: devnum2devname(me->devnum));
1148 } else {
1149 fprintf(stderr, Name
1150 ": failed to start array %s: %s\n",
1151 me->path ?: devnum2devname(me->devnum),
1152 strerror(errno));
1153 rv = 1;
1154 }
1155 }
1156 }
1157 return rv;
1158 }
1159
1160 static char *container2devname(char *devname)
1161 {
1162 char *mdname = NULL;
1163
1164 if (devname[0] == '/') {
1165 int fd = open(devname, O_RDONLY);
1166 if (fd >= 0) {
1167 mdname = devnum2devname(fd2devnum(fd));
1168 close(fd);
1169 }
1170 } else {
1171 int uuid[4];
1172 struct map_ent *mp, *map = NULL;
1173
1174 if (!parse_uuid(devname, uuid))
1175 return mdname;
1176 mp = map_by_uuid(&map, uuid);
1177 if (mp)
1178 mdname = devnum2devname(mp->devnum);
1179 map_free(map);
1180 }
1181
1182 return mdname;
1183 }
1184
1185 int Incremental_container(struct supertype *st, char *devname, int verbose,
1186 int runstop, int autof, int trustworthy)
1187 {
1188 /* Collect the contents of this container and for each
1189 * array, choose a device name and assemble the array.
1190 */
1191
1192 struct mdinfo *list = st->ss->container_content(st);
1193 struct mdinfo *ra;
1194 struct map_ent *map = NULL;
1195
1196 if (map_lock(&map))
1197 fprintf(stderr, Name ": failed to get exclusive lock on "
1198 "mapfile\n");
1199
1200 for (ra = list ; ra ; ra = ra->next) {
1201 int mdfd;
1202 char chosen_name[1024];
1203 struct map_ent *mp;
1204 struct mddev_ident_s *match = NULL;
1205
1206 mp = map_by_uuid(&map, ra->uuid);
1207
1208 if (mp) {
1209 mdfd = open_dev(mp->devnum);
1210 if (mp->path)
1211 strcpy(chosen_name, mp->path);
1212 else
1213 strcpy(chosen_name, devnum2devname(mp->devnum));
1214 } else {
1215
1216 /* Check in mdadm.conf for container == devname and
1217 * member == ra->text_version after second slash.
1218 */
1219 char *sub = strchr(ra->text_version+1, '/');
1220 struct mddev_ident_s *array_list;
1221 if (sub) {
1222 sub++;
1223 array_list = conf_get_ident(NULL);
1224 } else
1225 array_list = NULL;
1226 for(; array_list ; array_list = array_list->next) {
1227 char *dn;
1228 if (array_list->member == NULL ||
1229 array_list->container == NULL)
1230 continue;
1231 if (strcmp(array_list->member, sub) != 0)
1232 continue;
1233 if (array_list->uuid_set &&
1234 !same_uuid(ra->uuid, array_list->uuid, st->ss->swapuuid))
1235 continue;
1236 dn = container2devname(array_list->container);
1237 if (dn == NULL)
1238 continue;
1239 if (strncmp(dn, ra->text_version+1,
1240 strlen(dn)) != 0 ||
1241 ra->text_version[strlen(dn)+1] != '/') {
1242 free(dn);
1243 continue;
1244 }
1245 free(dn);
1246 /* we have a match */
1247 match = array_list;
1248 if (verbose>0)
1249 fprintf(stderr, Name ": match found for member %s\n",
1250 array_list->member);
1251 break;
1252 }
1253
1254 if (match && match->devname &&
1255 strcasecmp(match->devname, "<ignore>") == 0) {
1256 if (verbose > 0)
1257 fprintf(stderr, Name ": array %s/%s is "
1258 "explicitly ignored by mdadm.conf\n",
1259 match->container, match->member);
1260 return 2;
1261 }
1262 if (match)
1263 trustworthy = LOCAL;
1264
1265 mdfd = create_mddev(match ? match->devname : NULL,
1266 ra->name,
1267 autof,
1268 trustworthy,
1269 chosen_name);
1270 }
1271
1272 if (mdfd < 0) {
1273 fprintf(stderr, Name ": failed to open %s: %s.\n",
1274 chosen_name, strerror(errno));
1275 return 2;
1276 }
1277
1278 assemble_container_content(st, mdfd, ra, runstop,
1279 chosen_name, verbose);
1280 }
1281 map_unlock(&map);
1282 return 0;
1283 }
1284
1285 /*
1286 * IncrementalRemove - Attempt to see if the passed in device belongs to any
1287 * raid arrays, and if so first fail (if needed) and then remove the device.
1288 *
1289 * @devname - The device we want to remove
1290 *
1291 * Note: the device name must be a kernel name like "sda", so
1292 * that we can find it in /proc/mdstat
1293 */
1294 int IncrementalRemove(char *devname, int verbose)
1295 {
1296 int mdfd;
1297 int rv;
1298 struct mdstat_ent *ent;
1299 struct mddev_dev_s devlist;
1300
1301 if (strchr(devname, '/')) {
1302 fprintf(stderr, Name ": incremental removal requires a "
1303 "kernel device name, not a file: %s\n", devname);
1304 return 1;
1305 }
1306 ent = mdstat_by_component(devname);
1307 if (!ent) {
1308 fprintf(stderr, Name ": %s does not appear to be a component "
1309 "of any array\n", devname);
1310 return 1;
1311 }
1312 mdfd = open_dev(ent->devnum);
1313 if (mdfd < 0) {
1314 fprintf(stderr, Name ": Cannot open array %s!!\n", ent->dev);
1315 return 1;
1316 }
1317 memset(&devlist, 0, sizeof(devlist));
1318 devlist.devname = devname;
1319 devlist.disposition = 'f';
1320 Manage_subdevs(ent->dev, mdfd, &devlist, verbose, 0);
1321 devlist.disposition = 'r';
1322 rv = Manage_subdevs(ent->dev, mdfd, &devlist, verbose, 0);
1323 close(mdfd);
1324 return rv;
1325 }