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