2 * Incremental.c - support --incremental. Part of:
3 * mdadm - manage Linux "md" devices aka RAID arrays.
5 * Copyright (C) 2006-2012 Neil Brown <neilb@suse.de>
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.
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.
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
23 * Email: <neilb@suse.de>
27 * QVB Post Office, NSW 1230
35 static int count_active(struct supertype
*st
, struct mdinfo
*sra
,
36 int mdfd
, char **availp
,
38 static void find_reject(int mdfd
, struct supertype
*st
, struct mdinfo
*sra
,
39 int number
, __u64 events
, int verbose
,
41 static int try_spare(char *devname
, int *dfdp
, struct dev_policy
*pol
,
42 struct map_ent
*target
,
43 struct supertype
*st
, int verbose
);
45 static int Incremental_container(struct supertype
*st
, char *devname
,
48 int Incremental(char *devname
, struct context
*c
,
51 /* Add this device to an array, creating the array if necessary
52 * and starting the array if sensible or - if runstop>0 - if possible.
54 * This has several steps:
56 * 1/ Check if device is permitted by mdadm.conf, reject if not.
57 * 2/ Find metadata, reject if none appropriate (check
58 * version/name from args)
59 * 3/ Check if there is a match in mdadm.conf
60 * 3a/ if not, check for homehost match. If no match, assemble as
62 * 4/ Determine device number.
63 * - If in mdadm.conf with std name, use that
64 * - UUID in /var/run/mdadm.map use that
65 * - If name is suggestive, use that. unless in use with different uuid.
66 * - Choose a free, high number.
67 * - Use a partitioned device unless strong suggestion not to.
69 * Don't choose partitioned for containers.
70 * 5/ Find out if array already exists
72 * - choose a name, from mdadm.conf or 'name' field in array.
76 * - check one drive in array to make sure metadata is a reasonably
77 * close match. Reject if not (e.g. different type)
79 * 6/ Make sure /var/run/mdadm.map contains this array.
80 * 7/ Is there enough devices to possibly start the array?
81 * For a container, this means running Incremental_container.
82 * 7a/ if not, finish with success.
84 * - read all metadata and arrange devices like -A does
85 * - if number of OK devices match expected, or -R and there are enough,
86 * start the array (auto-readonly).
89 struct mdinfo info
, dinfo
;
90 struct mdinfo
*sra
= NULL
, *d
;
91 struct mddev_ident
*match
;
92 char chosen_name
[1024];
94 struct map_ent
*mp
, *map
= NULL
;
95 int dfd
= -1, mdfd
= -1;
100 mdu_array_info_t ainf
;
101 struct dev_policy
*policy
= NULL
;
102 struct map_ent target_array
;
105 struct createinfo
*ci
= conf_get_create_info();
107 if (stat(devname
, &stb
) < 0) {
109 pr_err("stat failed for %s: %s.\n",
110 devname
, strerror(errno
));
113 if ((stb
.st_mode
& S_IFMT
) != S_IFBLK
) {
115 pr_err("%s is not a block device.\n",
119 dfd
= dev_open(devname
, O_RDONLY
);
122 pr_err("cannot open %s: %s.\n",
123 devname
, strerror(errno
));
126 /* If the device is a container, we do something very different */
127 if (must_be_container(dfd
)) {
129 st
= super_by_fd(dfd
, NULL
);
131 st
->ignore_hw_compat
= 1;
132 if (st
&& st
->ss
->load_container
)
133 rv
= st
->ss
->load_container(st
, dfd
, NULL
);
136 if (!rv
&& st
->ss
->container_content
) {
138 pr_err("failed to get "
139 "exclusive lock on mapfile\n");
140 rv
= Incremental_container(st
, devname
, c
);
145 pr_err("%s is not part of an md array.\n",
150 /* 1/ Check if device is permitted by mdadm.conf */
152 if (!conf_test_dev(devname
)) {
154 pr_err("%s not permitted by mdadm.conf.\n",
159 /* 2/ Find metadata, reject if none appropriate (check
160 * version/name from args) */
162 if (fstat(dfd
, &stb
) < 0) {
164 pr_err("fstat failed for %s: %s.\n",
165 devname
, strerror(errno
));
168 if ((stb
.st_mode
& S_IFMT
) != S_IFBLK
) {
170 pr_err("%s is not a block device.\n",
175 dinfo
.disk
.major
= major(stb
.st_rdev
);
176 dinfo
.disk
.minor
= minor(stb
.st_rdev
);
178 policy
= disk_policy(&dinfo
);
179 have_target
= policy_check_path(&dinfo
, &target_array
);
181 if (st
== NULL
&& (st
= guess_super(dfd
)) == NULL
) {
183 pr_err("no recognisable superblock on %s.\n",
185 rv
= try_spare(devname
, &dfd
, policy
,
186 have_target
? &target_array
: NULL
,
190 st
->ignore_hw_compat
= 1;
191 if (st
->ss
->compare_super
== NULL
||
192 st
->ss
->load_super(st
, dfd
, NULL
)) {
194 pr_err("no RAID superblock on %s.\n",
196 rv
= try_spare(devname
, &dfd
, policy
,
197 have_target
? &target_array
: NULL
,
202 close (dfd
); dfd
= -1;
204 st
->ss
->getinfo_super(st
, &info
, NULL
);
206 /* 3/ Check if there is a match in mdadm.conf */
207 match
= conf_match(st
, &info
, devname
, c
->verbose
, &rv
);
208 if (!match
&& rv
== 2)
211 if (match
&& match
->devname
212 && strcasecmp(match
->devname
, "<ignore>") == 0) {
214 pr_err("array containing %s is explicitly"
215 " ignored by mdadm.conf\n",
220 /* 3a/ if not, check for homehost match. If no match, continue
221 * but don't trust the 'name' in the array. Thus a 'random' minor
222 * number will be assigned, and the device name will be based
226 else if (st
->ss
->match_home(st
, c
->homehost
) == 1)
228 else if (st
->ss
->match_home(st
, "any") == 1)
229 trustworthy
= LOCAL_ANY
;
231 trustworthy
= FOREIGN
;
233 if (!match
&& !conf_test_metadata(st
->ss
->name
, policy
,
234 (trustworthy
== LOCAL
))) {
236 pr_err("%s has metadata type %s for which "
237 "auto-assembly is disabled\n",
238 devname
, st
->ss
->name
);
241 if (trustworthy
== LOCAL_ANY
)
244 /* There are three possible sources for 'autof': command line,
245 * ARRAY line in mdadm.conf, or CREATE line in mdadm.conf.
246 * ARRAY takes precedence, then command line, then
249 if (match
&& match
->autof
)
250 c
->autof
= match
->autof
;
252 c
->autof
= ci
->autof
;
254 name_to_use
= info
.name
;
255 if (name_to_use
[0] == 0 &&
256 info
.array
.level
== LEVEL_CONTAINER
) {
257 name_to_use
= info
.text_version
;
258 trustworthy
= METADATA
;
260 if (name_to_use
[0] && trustworthy
!= LOCAL
&&
261 ! c
->require_homehost
&&
262 conf_name_is_free(name_to_use
))
265 /* strip "hostname:" prefix from name if we have decided
266 * to treat it as LOCAL
268 if (trustworthy
== LOCAL
&& strchr(name_to_use
, ':') != NULL
)
269 name_to_use
= strchr(name_to_use
, ':')+1;
271 /* 4/ Check if array exists.
274 pr_err("failed to get exclusive lock on "
276 /* Now check we can get O_EXCL. If not, probably "mdadm -A" has
279 dfd
= dev_open(devname
, O_RDONLY
|O_EXCL
);
282 pr_err("cannot reopen %s: %s.\n",
283 devname
, strerror(errno
));
286 /* Cannot hold it open while we add the device to the array,
287 * so we must release the O_EXCL and depend on the map_lock()
292 mp
= map_by_uuid(&map
, info
.uuid
);
294 mdfd
= open_dev(mp
->devnm
);
300 /* Couldn't find an existing array, maybe make a new one */
301 mdfd
= create_mddev(match
? match
->devname
: NULL
,
302 name_to_use
, c
->autof
, trustworthy
, chosen_name
);
307 sysfs_init(&info
, mdfd
, NULL
);
309 if (set_array_info(mdfd
, st
, &info
) != 0) {
310 pr_err("failed to set array info for %s: %s\n",
311 chosen_name
, strerror(errno
));
317 dinfo
.disk
.major
= major(stb
.st_rdev
);
318 dinfo
.disk
.minor
= minor(stb
.st_rdev
);
319 if (add_disk(mdfd
, st
, &info
, &dinfo
) != 0) {
320 pr_err("failed to add %s to new array %s: %s.\n",
321 devname
, chosen_name
, strerror(errno
));
322 ioctl(mdfd
, STOP_ARRAY
, 0);
326 sra
= sysfs_read(mdfd
, NULL
, (GET_DEVS
| GET_STATE
|
327 GET_OFFSET
| GET_SIZE
));
329 if (!sra
|| !sra
->devs
|| sra
->devs
->disk
.raid_disk
>= 0) {
330 /* It really should be 'none' - must be old buggy
331 * kernel, and mdadm -I may not be able to complete.
334 ioctl(mdfd
, STOP_ARRAY
, NULL
);
335 pr_err("You have an old buggy kernel which cannot support\n"
336 " --incremental reliably. Aborting.\n");
340 info
.array
.working_disks
= 1;
341 /* 6/ Make sure /var/run/mdadm.map contains this array. */
342 map_update(&map
, fd2devnm(mdfd
),
344 info
.uuid
, chosen_name
);
347 /* - check one drive in array to make sure metadata is a reasonably */
348 /* close match. Reject if not (e.g. different type) */
349 /* - add the device */
353 struct supertype
*st2
;
354 struct mdinfo info2
, *d
;
356 sra
= sysfs_read(mdfd
, NULL
, (GET_DEVS
| GET_STATE
|
357 GET_OFFSET
| GET_SIZE
));
360 strcpy(chosen_name
, mp
->path
);
362 strcpy(chosen_name
, mp
->devnm
);
364 /* It is generally not OK to add non-spare drives to a
365 * running array as they are probably missing because
366 * they failed. However if runstop is 1, then the
367 * array was possibly started early and our best bet is
368 * to add this anyway.
369 * Also if action policy is re-add or better we allow
371 * This doesn't apply to containers as the 'non-spare'
372 * flag has a different meaning. The test has to happen
373 * at the device level there
375 if (!st
->ss
->external
376 && (info
.disk
.state
& (1<<MD_DISK_SYNC
)) != 0
377 && ! policy_action_allows(policy
, st
->ss
->name
,
380 if (ioctl(mdfd
, GET_ARRAY_INFO
, &ainf
) == 0) {
381 pr_err("not adding %s to active array (without --run) %s\n",
382 devname
, chosen_name
);
392 sprintf(dn
, "%d:%d", sra
->devs
->disk
.major
,
393 sra
->devs
->disk
.minor
);
394 dfd2
= dev_open(dn
, O_RDONLY
);
396 pr_err("unable to open %s\n", devname
);
401 if (st2
->ss
->load_super(st2
, dfd2
, NULL
) ||
402 st
->ss
->compare_super(st
, st2
) != 0) {
403 pr_err("metadata mismatch between %s and "
405 devname
, chosen_name
);
411 st2
->ss
->getinfo_super(st2
, &info2
, NULL
);
412 st2
->ss
->free_super(st2
);
413 if (info
.array
.level
!= info2
.array
.level
||
414 memcmp(info
.uuid
, info2
.uuid
, 16) != 0 ||
415 info
.array
.raid_disks
!= info2
.array
.raid_disks
) {
416 pr_err("unexpected difference between %s and %s.\n",
417 chosen_name
, devname
);
422 info
.disk
.major
= major(stb
.st_rdev
);
423 info
.disk
.minor
= minor(stb
.st_rdev
);
424 /* add disk needs to know about containers */
425 if (st
->ss
->external
)
426 sra
->array
.level
= LEVEL_CONTAINER
;
427 err
= add_disk(mdfd
, st
, sra
, &info
);
428 if (err
< 0 && errno
== EBUSY
) {
429 /* could be another device present with the same
430 * disk.number. Find and reject any such
432 find_reject(mdfd
, st
, sra
, info
.disk
.number
,
433 info
.events
, c
->verbose
, chosen_name
);
434 err
= add_disk(mdfd
, st
, sra
, &info
);
436 if (err
< 0 && errno
== EINVAL
&&
437 info
.disk
.state
& (1<<MD_DISK_SYNC
)) {
438 /* Maybe it needs to be added as a spare */
439 if (policy_action_allows(policy
, st
->ss
->name
,
441 info
.disk
.state
&= ~(1<<MD_DISK_SYNC
);
442 err
= add_disk(mdfd
, st
, sra
, &info
);
445 pr_err("can only add %s to %s as a spare, and force-spare is not set.\n",
446 devname
, chosen_name
);
449 pr_err("failed to add %s to existing array %s: %s.\n",
450 devname
, chosen_name
, strerror(errno
));
454 info
.array
.working_disks
= 0;
455 for (d
= sra
->devs
; d
; d
=d
->next
)
456 info
.array
.working_disks
++;
460 /* 7/ Is there enough devices to possibly start the array? */
461 /* 7a/ if not, finish with success. */
462 if (info
.array
.level
== LEVEL_CONTAINER
) {
464 /* Try to assemble within the container */
465 sysfs_uevent(sra
, "change");
467 pr_err("container %s now has %d device%s\n",
468 chosen_name
, info
.array
.working_disks
,
469 info
.array
.working_disks
== 1?"":"s");
470 wait_for(chosen_name
, mdfd
);
471 if (st
->ss
->external
)
472 strcpy(devnm
, fd2devnm(mdfd
));
473 if (st
->ss
->load_container
)
474 rv
= st
->ss
->load_container(st
, mdfd
, NULL
);
478 rv
= Incremental_container(st
, chosen_name
, c
);
481 /* Don't fail the whole -I if a subarray didn't
482 * have enough devices to start yet
485 /* after spare is added, ping monitor for external metadata
486 * so that it can eg. try to rebuild degraded array */
487 if (st
->ss
->external
)
492 /* We have added something to the array, so need to re-read the
493 * state. Eventually this state should be kept up-to-date as
497 sra
= sysfs_read(mdfd
, NULL
, (GET_DEVS
| GET_STATE
|
498 GET_OFFSET
| GET_SIZE
));
499 active_disks
= count_active(st
, sra
, mdfd
, &avail
, &info
);
500 if (enough(info
.array
.level
, info
.array
.raid_disks
,
501 info
.array
.layout
, info
.array
.state
& 1,
504 pr_err("%s attached to %s, not enough to start (%d).\n",
505 devname
, chosen_name
, active_disks
);
511 /* - if number of OK devices match expected, or -R and there */
513 /* + add any bitmap file */
514 /* + start the array (auto-readonly). */
516 if (ioctl(mdfd
, GET_ARRAY_INFO
, &ainf
) == 0) {
518 pr_err("%s attached to %s which is already active.\n",
519 devname
, chosen_name
);
525 if (c
->runstop
> 0 || active_disks
>= info
.array
.working_disks
) {
527 /* Let's try to start it */
529 if (info
.reshape_active
&& !(info
.reshape_active
& RESHAPE_NO_BACKUP
)) {
531 ": %s: This array is being reshaped and cannot be started\n"
532 " by --incremental. Please use --assemble\n",
536 if (match
&& match
->bitmap_file
) {
537 int bmfd
= open(match
->bitmap_file
, O_RDWR
);
539 pr_err("Could not open bitmap file %s.\n",
543 if (ioctl(mdfd
, SET_BITMAP_FILE
, bmfd
) != 0) {
545 pr_err("Failed to set bitmapfile for %s.\n",
551 /* Need to remove from the array any devices which
552 * 'count_active' discerned were too old or inappropriate
554 for (d
= sra
? sra
->devs
: NULL
; d
; d
= d
->next
)
555 if (d
->disk
.state
& (1<<MD_DISK_REMOVED
))
556 remove_disk(mdfd
, st
, sra
, d
);
558 if ((sra
== NULL
|| active_disks
>= info
.array
.working_disks
)
559 && trustworthy
!= FOREIGN
)
560 rv
= ioctl(mdfd
, RUN_ARRAY
, NULL
);
562 rv
= sysfs_set_str(sra
, NULL
,
563 "array_state", "read-auto");
566 pr_err("%s attached to %s, which has been started.\n",
567 devname
, chosen_name
);
569 wait_for(chosen_name
, mdfd
);
570 /* We just started the array, so some devices
571 * might have been evicted from the array
572 * because their event counts were too old.
573 * If the action=re-add policy is in-force for
574 * those devices we should re-add them now.
576 for (dsk
= sra
->devs
; dsk
; dsk
= dsk
->next
) {
577 if (disk_action_allows(dsk
, st
->ss
->name
, act_re_add
) &&
578 add_disk(mdfd
, st
, sra
, dsk
) == 0)
579 pr_err("%s re-added to %s\n",
580 dsk
->sys_name
, chosen_name
);
583 pr_err("%s attached to %s, but failed to start: %s.\n",
584 devname
, chosen_name
, strerror(errno
));
589 pr_err("%s attached to %s, not enough to start safely.\n",
590 devname
, chosen_name
);
600 dev_policy_free(policy
);
609 static void find_reject(int mdfd
, struct supertype
*st
, struct mdinfo
*sra
,
610 int number
, __u64 events
, int verbose
,
613 /* Find a device attached to this array with a disk.number of number
614 * and events less than the passed events, and remove the device.
619 if (ioctl(mdfd
, GET_ARRAY_INFO
, &ra
) == 0)
620 return; /* not safe to remove from active arrays
621 * without thinking more */
623 for (d
= sra
->devs
; d
; d
= d
->next
) {
627 sprintf(dn
, "%d:%d", d
->disk
.major
, d
->disk
.minor
);
628 dfd
= dev_open(dn
, O_RDONLY
);
631 if (st
->ss
->load_super(st
, dfd
, NULL
)) {
635 st
->ss
->getinfo_super(st
, &info
, NULL
);
636 st
->ss
->free_super(st
);
639 if (info
.disk
.number
!= number
||
640 info
.events
>= events
)
643 if (d
->disk
.raid_disk
> -1)
644 sysfs_set_str(sra
, d
, "slot", "none");
645 if (sysfs_set_str(sra
, d
, "state", "remove") == 0)
647 pr_err("removing old device %s from %s\n",
648 d
->sys_name
+4, array_name
);
652 static int count_active(struct supertype
*st
, struct mdinfo
*sra
,
653 int mdfd
, char **availp
,
654 struct mdinfo
*bestinfo
)
656 /* count how many devices in sra think they are active */
660 __u64 max_events
= 0;
672 for (d
= sra
->devs
; d
; d
= d
->next
)
674 for (d
= sra
->devs
, devnum
= 0 ; d
; d
= d
->next
, devnum
++) {
680 sprintf(dn
, "%d:%d", d
->disk
.major
, d
->disk
.minor
);
681 dfd
= dev_open(dn
, O_RDONLY
);
684 ok
= st
->ss
->load_super(st
, dfd
, NULL
);
688 info
.array
.raid_disks
= raid_disks
;
689 st
->ss
->getinfo_super(st
, &info
, devmap
+ raid_disks
* devnum
);
691 raid_disks
= info
.array
.raid_disks
;
692 avail
= xcalloc(raid_disks
, 1);
695 best
= xcalloc(raid_disks
, sizeof(int));
696 devmap
= xcalloc(raid_disks
, numdevs
);
698 st
->ss
->getinfo_super(st
, &info
, devmap
);
701 if (info
.disk
.state
& (1<<MD_DISK_SYNC
))
705 max_events
= info
.events
;
706 avail
[info
.disk
.raid_disk
] = 2;
707 best
[info
.disk
.raid_disk
] = devnum
;
708 st
->ss
->getinfo_super(st
, bestinfo
, NULL
);
709 } else if (info
.events
== max_events
) {
710 avail
[info
.disk
.raid_disk
] = 2;
711 best
[info
.disk
.raid_disk
] = devnum
;
712 } else if (info
.events
== max_events
-1) {
713 if (avail
[info
.disk
.raid_disk
] == 0) {
714 avail
[info
.disk
.raid_disk
] = 1;
715 best
[info
.disk
.raid_disk
] = devnum
;
717 } else if (info
.events
< max_events
- 1)
719 else if (info
.events
== max_events
+1) {
721 max_events
= info
.events
;
722 for (i
= 0; i
< raid_disks
; i
++)
725 avail
[info
.disk
.raid_disk
] = 2;
726 best
[info
.disk
.raid_disk
] = devnum
;
727 st
->ss
->getinfo_super(st
, bestinfo
, NULL
);
728 } else { /* info.events much bigger */
729 memset(avail
, 0, raid_disks
);
730 max_events
= info
.events
;
731 avail
[info
.disk
.raid_disk
] = 2;
732 best
[info
.disk
.raid_disk
] = devnum
;
733 st
->ss
->getinfo_super(st
, bestinfo
, NULL
);
735 } else if (info
.disk
.state
& (1<<MD_DISK_REPLACEMENT
))
737 st
->ss
->free_super(st
);
741 /* We need to reject any device that thinks the best device is
742 * failed or missing */
743 for (b
= 0; b
< raid_disks
; b
++)
747 for (i
= 0 ; i
< raid_disks
; i
++) {
748 if (i
!= b
&& avail
[i
])
749 if (devmap
[raid_disks
* best
[i
] + b
] == 0) {
750 /* This device thinks 'b' is failed -
753 for (d
=sra
->devs
; devnum
; d
= d
->next
)
755 d
->disk
.state
|= (1 << MD_DISK_REMOVED
);
763 return cnt
+ replcnt
;
766 /* test if container has degraded member(s) */
767 static int container_members_max_degradation(struct map_ent
*map
, struct map_ent
*me
)
769 mdu_array_info_t array
;
771 int max_degraded
= 0;
773 for(; map
; map
= map
->next
) {
774 if (!metadata_container_matches(map
->metadata
, me
->devnm
))
776 afd
= open_dev(map
->devnm
);
779 /* most accurate information regarding array degradation */
780 if (ioctl(afd
, GET_ARRAY_INFO
, &array
) >= 0) {
781 int degraded
= array
.raid_disks
- array
.active_disks
-
783 if (degraded
> max_degraded
)
784 max_degraded
= degraded
;
788 return (max_degraded
);
791 static int array_try_spare(char *devname
, int *dfdp
, struct dev_policy
*pol
,
792 struct map_ent
*target
, int bare
,
793 struct supertype
*st
, int verbose
)
795 /* This device doesn't have any md metadata
796 * The device policy allows 'spare' and if !bare, it allows spare-same-slot.
797 * If 'st' is not set, then we only know that some metadata allows this,
798 * others possibly don't.
799 * So look for a container or array to attach the device to.
800 * Prefer 'target' if that is set and the array is found.
802 * If st is set, then only arrays of that type are considered
803 * Return 0 on success, or some exit code on failure, probably 1.
807 struct map_ent
*mp
, *map
= NULL
;
808 struct mdinfo
*chosen
= NULL
;
811 if (fstat(dfd
, &stb
) != 0)
815 * Now we need to find a suitable array to add this to.
816 * We only accept arrays that:
818 * - are in the same domains as the device
819 * - are of an size for which the device will be useful
820 * and we choose the one that is the most degraded
823 if (map_lock(&map
)) {
824 pr_err("failed to get exclusive lock on "
828 for (mp
= map
; mp
; mp
= mp
->next
) {
829 struct supertype
*st2
;
830 struct domainlist
*dl
= NULL
;
832 unsigned long long devsize
;
833 unsigned long long component_size
= 0;
835 if (is_subarray(mp
->metadata
))
838 st2
= st
->ss
->match_metadata_desc(mp
->metadata
);
840 (st
->minor_version
>= 0 &&
841 st
->minor_version
!= st2
->minor_version
)) {
843 pr_err("not adding %s to %s as metadata type doesn't match\n",
850 sra
= sysfs_read(-1, mp
->devnm
,
851 GET_DEVS
|GET_OFFSET
|GET_SIZE
|GET_STATE
|
852 GET_DEGRADED
|GET_COMPONENT
|GET_VERSION
);
854 /* Probably a container - no degraded info */
855 sra
= sysfs_read(-1, mp
->devnm
,
856 GET_DEVS
|GET_OFFSET
|GET_SIZE
|GET_STATE
|
857 GET_COMPONENT
|GET_VERSION
);
859 sra
->array
.failed_disks
= -1;
866 for(i
= 0; !st2
&& superlist
[i
]; i
++)
867 st2
= superlist
[i
]->match_metadata_desc(
871 pr_err("not adding %s to %s"
872 " as metadata not recognised.\n",
876 /* Need to double check the 'act_spare' permissions applies
879 if (!policy_action_allows(pol
, st2
->ss
->name
, act_spare
))
881 if (!bare
&& !policy_action_allows(pol
, st2
->ss
->name
,
882 act_spare_same_slot
))
886 /* update number of failed disks for mostly degraded
887 * container member */
888 if (sra
->array
.failed_disks
== -1)
889 sra
->array
.failed_disks
= container_members_max_degradation(map
, mp
);
891 get_dev_size(dfd
, NULL
, &devsize
);
892 if (sra
->component_size
== 0) {
893 /* true for containers, here we must read superblock
894 * to obtain minimum spare size */
895 struct supertype
*st3
= dup_super(st2
);
896 int mdfd
= open_dev(mp
->devnm
);
901 if (st3
->ss
->load_container
&&
902 !st3
->ss
->load_container(st3
, mdfd
, mp
->path
)) {
903 component_size
= st3
->ss
->min_acceptable_spare_size(st3
);
904 st3
->ss
->free_super(st3
);
909 if ((sra
->component_size
> 0 &&
910 st2
->ss
->avail_size(st2
, devsize
,
912 ? sra
->devs
->data_offset
914 < sra
->component_size
)
916 (sra
->component_size
== 0 && devsize
< component_size
)) {
918 pr_err("not adding %s to %s as it is too small\n",
922 /* test against target.
923 * If 'target' is set and 'bare' is false, we only accept
924 * arrays/containers that match 'target'.
925 * If 'target' is set and 'bare' is true, we prefer the
926 * array which matches 'target'.
927 * target is considered only if we deal with degraded array
929 if (target
&& policy_action_allows(pol
, st2
->ss
->name
,
930 act_spare_same_slot
)) {
931 if (strcmp(target
->metadata
, mp
->metadata
) == 0 &&
932 memcmp(target
->uuid
, mp
->uuid
,
933 sizeof(target
->uuid
)) == 0 &&
934 sra
->array
.failed_disks
> 0) {
935 /* This is our target!! */
940 /* skip to end so we don't check any more */
950 dl
= domain_from_array(sra
, st2
->ss
->name
);
951 if (domain_test(dl
, pol
, st2
->ss
->name
) != 1) {
952 /* domain test fails */
954 pr_err("not adding %s to %s as"
955 " it is not in a compatible domain\n",
960 /* all tests passed, OK to add to this array */
964 } else if (chosen
->array
.failed_disks
< sra
->array
.failed_disks
) {
978 /* add current device to chosen array as a spare */
979 int mdfd
= open_dev(chosen
->sys_name
);
981 struct mddev_dev devlist
;
985 devlist
.writemostly
= 0;
986 devlist
.devname
= devname
;
987 sprintf(devname
, "%d:%d", major(stb
.st_rdev
),
989 devlist
.disposition
= 'a';
992 rv
= Manage_subdevs(chosen
->sys_name
, mdfd
, &devlist
,
998 pr_err("added %s as spare for %s\n",
999 devname
, chosen
->sys_name
);
1001 pr_err("failed to add %s as spare for %s\n",
1002 devname
, chosen
->sys_name
);
1010 static int partition_try_spare(char *devname
, int *dfdp
, struct dev_policy
*pol
,
1011 struct supertype
*st
, int verbose
)
1013 /* we know that at least one partition virtual-metadata is
1014 * allowed to incorporate spares like this device. We need to
1015 * find a suitable device to copy partition information from.
1017 * Getting a list of all disk (not partition) devices is
1018 * slightly non-trivial. We could look at /sys/block, but
1019 * that is theoretically due to be removed. Maybe best to use
1020 * /dev/disk/by-path/?* and ignore names ending '-partNN' as
1021 * we depend on this directory of 'path' info. But that fails
1022 * to find loop devices and probably others. Maybe don't
1023 * worry about that, they aren't the real target.
1025 * So: check things in /dev/disk/by-path to see if they are in
1026 * a compatible domain, then load the partition table and see
1027 * if it is OK for the new device, and choose the largest
1028 * partition table that fits.
1032 char *chosen
= NULL
;
1033 unsigned long long chosen_size
= 0;
1034 struct supertype
*chosen_st
= NULL
;
1037 dir
= opendir("/dev/disk/by-path");
1040 while ((de
= readdir(dir
)) != NULL
) {
1042 struct dev_policy
*pol2
= NULL
;
1043 struct domainlist
*domlist
= NULL
;
1046 struct supertype
*st2
= NULL
;
1047 char *devname
= NULL
;
1048 unsigned long long devsectors
;
1050 if (de
->d_ino
== 0 ||
1051 de
->d_name
[0] == '.' ||
1052 (de
->d_type
!= DT_LNK
&& de
->d_type
!= DT_UNKNOWN
))
1055 ep
= de
->d_name
+ strlen(de
->d_name
);
1056 while (ep
> de
->d_name
&&
1059 if (ep
> de
->d_name
+ 5 &&
1060 strncmp(ep
-5, "-part", 5) == 0)
1061 /* This is a partition - skip it */
1064 pol2
= path_policy(de
->d_name
, type_disk
);
1066 domain_merge(&domlist
, pol2
, st
? st
->ss
->name
: NULL
);
1067 if (domain_test(domlist
, pol
, st
? st
->ss
->name
: NULL
) != 1)
1068 /* new device is incompatible with this device. */
1071 domain_free(domlist
);
1074 if (asprintf(&devname
, "/dev/disk/by-path/%s", de
->d_name
) != 1) {
1078 fd
= open(devname
, O_RDONLY
);
1081 if (get_dev_size(fd
, devname
, &devsectors
) == 0)
1086 st2
= dup_super(st
);
1088 st2
= guess_super_type(fd
, guess_partitions
);
1090 st2
->ss
->load_super(st2
, fd
, NULL
) < 0)
1094 /* Check domain policy again, this time referring to metadata */
1095 domain_merge(&domlist
, pol2
, st2
->ss
->name
);
1096 if (domain_test(domlist
, pol
, st2
->ss
->name
) != 1)
1097 /* Incompatible devices for this metadata type */
1099 if (!policy_action_allows(pol
, st2
->ss
->name
, act_spare
))
1100 /* Some partition types allow sparing, but not
1106 st2
->ss
->getinfo_super(st2
, &info
, NULL
);
1107 if (info
.component_size
> devsectors
)
1108 /* This partitioning doesn't fit in the device */
1111 /* This is an acceptable device to copy partition
1112 * metadata from. We could just stop here, but I
1113 * think I want to keep looking incase a larger
1114 * metadata which makes better use of the device can
1117 if (chosen
== NULL
||
1118 chosen_size
< info
.component_size
) {
1119 chosen_size
= info
.component_size
;
1124 chosen_st
->ss
->free_super(chosen_st
);
1133 domain_free(domlist
);
1134 dev_policy_free(pol2
);
1136 st2
->ss
->free_super(st2
);
1148 /* 'chosen' is the best device we can find. Let's write its
1149 * metadata to devname dfd is read-only so don't use that
1151 fd
= open(devname
, O_RDWR
);
1153 chosen_st
->ss
->store_super(chosen_st
, fd
);
1157 chosen_st
->ss
->free_super(chosen_st
);
1162 static int is_bare(int dfd
)
1164 unsigned long long size
= 0;
1165 char bufpad
[4096 + 4096];
1166 char *buf
= (char*)(((long)bufpad
+ 4096) & ~4095);
1168 if (lseek(dfd
, 0, SEEK_SET
) != 0 ||
1169 read(dfd
, buf
, 4096) != 4096)
1172 if (buf
[0] != '\0' && buf
[0] != '\x5a' && buf
[0] != '\xff')
1174 if (memcmp(buf
, buf
+1, 4095) != 0)
1177 /* OK, first 4K appear blank, try the end. */
1178 get_dev_size(dfd
, NULL
, &size
);
1179 if (lseek(dfd
, size
-4096, SEEK_SET
) < 0 ||
1180 read(dfd
, buf
, 4096) != 4096)
1183 if (buf
[0] != '\0' && buf
[0] != '\x5a' && buf
[0] != '\xff')
1185 if (memcmp(buf
, buf
+1, 4095) != 0)
1191 /* adding a spare to a regular array is quite different from adding one to
1192 * a set-of-partitions virtual array.
1193 * This function determines which is worth trying and tries as appropriate.
1194 * Arrays are given priority over partitions.
1196 static int try_spare(char *devname
, int *dfdp
, struct dev_policy
*pol
,
1197 struct map_ent
*target
,
1198 struct supertype
*st
, int verbose
)
1203 int partitions_ok
= 0;
1207 /* Can only add a spare if device has at least one domain */
1208 if (pol_find(pol
, pol_domain
) == NULL
)
1210 /* And only if some action allows spares */
1211 if (!policy_action_allows(pol
, st
?st
->ss
->name
:NULL
, act_spare
))
1214 /* Now check if the device is bare.
1215 * bare devices can always be added as a spare
1216 * non-bare devices can only be added if spare-same-slot is permitted,
1217 * and this device is replacing a previous device - in which case 'target'
1220 if (!is_bare(dfd
)) {
1221 /* Must have a target and allow same_slot */
1222 /* Later - may allow force_spare without target */
1224 !policy_action_allows(pol
, st
?st
->ss
->name
:NULL
,
1225 act_spare_same_slot
)) {
1227 pr_err("%s is not bare, so not "
1228 "considering as a spare\n",
1236 /* It might be OK to add this device to an array - need to see
1237 * what arrays might be candidates.
1240 /* just try try 'array' or 'partition' based on this metadata */
1241 if (st
->ss
->add_to_super
)
1242 return array_try_spare(devname
, dfdp
, pol
, target
, bare
,
1245 return partition_try_spare(devname
, dfdp
, pol
,
1248 /* No metadata was specified or found so options are open.
1249 * Check for whether any array metadata, or any partition metadata
1250 * might allow adding the spare. This check is just help to avoid
1251 * a more costly scan of all arrays when we can be sure that will
1254 for (i
= 0; (!arrays_ok
|| !partitions_ok
) && superlist
[i
] ; i
++) {
1255 if (superlist
[i
]->add_to_super
&& !arrays_ok
&&
1256 policy_action_allows(pol
, superlist
[i
]->name
, act_spare
))
1258 if (superlist
[i
]->add_to_super
== NULL
&& !partitions_ok
&&
1259 policy_action_allows(pol
, superlist
[i
]->name
, act_spare
))
1264 rv
= array_try_spare(devname
, dfdp
, pol
, target
, bare
,
1266 if (rv
!= 0 && partitions_ok
)
1267 rv
= partition_try_spare(devname
, dfdp
, pol
, st
, verbose
);
1271 int IncrementalScan(int verbose
)
1273 /* look at every device listed in the 'map' file.
1274 * If one is found that is not running then:
1275 * look in mdadm.conf for bitmap file.
1276 * if one exists, but array has none, add it.
1277 * try to start array in auto-readonly mode
1279 struct map_ent
*mapl
= NULL
;
1281 struct mddev_ident
*devs
, *mddev
;
1285 devs
= conf_get_ident(NULL
);
1287 for (me
= mapl
; me
; me
= me
->next
) {
1288 mdu_array_info_t array
;
1289 mdu_bitmap_file_t bmf
;
1291 int mdfd
= open_dev(me
->devnm
);
1295 if (ioctl(mdfd
, GET_ARRAY_INFO
, &array
) == 0 ||
1300 /* Ok, we can try this one. Maybe it needs a bitmap */
1301 for (mddev
= devs
; mddev
; mddev
= mddev
->next
)
1302 if (mddev
->devname
&& me
->path
1303 && devname_matches(mddev
->devname
, me
->path
))
1305 if (mddev
&& mddev
->bitmap_file
) {
1307 * Note: early kernels will wrongly fail this, so it
1311 if (ioctl(mdfd
, GET_ARRAY_INFO
, &bmf
) < 0) {
1312 int bmfd
= open(mddev
->bitmap_file
, O_RDWR
);
1314 added
= ioctl(mdfd
, SET_BITMAP_FILE
,
1321 pr_err("Added bitmap %s to %s\n",
1322 mddev
->bitmap_file
, me
->path
);
1323 else if (errno
!= EEXIST
)
1324 pr_err("Failed to add bitmap to %s: %s\n",
1325 me
->path
, strerror(errno
));
1328 /* FIXME check for reshape_active and consider not
1331 sra
= sysfs_read(mdfd
, NULL
, 0);
1333 if (sysfs_set_str(sra
, NULL
,
1334 "array_state", "read-auto") == 0) {
1336 pr_err("started array %s\n",
1337 me
->path
?: me
->devnm
);
1339 pr_err("failed to start array %s: %s\n",
1340 me
->path
?: me
->devnm
,
1350 static char *container2devname(char *devname
)
1352 char *mdname
= NULL
;
1354 if (devname
[0] == '/') {
1355 int fd
= open(devname
, O_RDONLY
);
1357 mdname
= xstrdup(fd2devnm(fd
));
1362 struct map_ent
*mp
, *map
= NULL
;
1364 if (!parse_uuid(devname
, uuid
))
1366 mp
= map_by_uuid(&map
, uuid
);
1368 mdname
= xstrdup(mp
->devnm
);
1375 static int Incremental_container(struct supertype
*st
, char *devname
,
1378 /* Collect the contents of this container and for each
1379 * array, choose a device name and assemble the array.
1382 struct mdinfo
*list
;
1384 struct map_ent
*map
= NULL
;
1387 struct mddev_ident
*match
;
1389 struct domainlist
*domains
;
1390 struct map_ent
*smp
;
1396 st
->ss
->getinfo_super(st
, &info
, NULL
);
1398 if ((c
->runstop
> 0 && info
.container_enough
>= 0) ||
1399 info
.container_enough
> 0)
1403 pr_err("not enough devices to start the container\n");
1407 match
= conf_match(st
, &info
, devname
, c
->verbose
, &rv
);
1408 if (match
== NULL
&& rv
== 2)
1411 /* Need to compute 'trustworthy' */
1413 trustworthy
= LOCAL
;
1414 else if (st
->ss
->match_home(st
, c
->homehost
) == 1)
1415 trustworthy
= LOCAL
;
1416 else if (st
->ss
->match_home(st
, "any") == 1)
1417 trustworthy
= LOCAL
;
1419 trustworthy
= FOREIGN
;
1421 list
= st
->ss
->container_content(st
, NULL
);
1422 /* when nothing to activate - quit */
1425 for (ra
= list
; ra
; ra
= ra
->next
) {
1427 char chosen_name
[1024];
1429 struct mddev_ident
*match
= NULL
;
1432 /* do not activate arrays blocked by metadata handler */
1433 if (ra
->array
.state
& (1 << MD_SB_BLOCK_VOLUME
)) {
1434 pr_err("Cannot activate array %s in %s.\n",
1435 ra
->text_version
, devname
);
1439 mp
= map_by_uuid(&map
, ra
->uuid
);
1442 mdfd
= open_dev(mp
->devnm
);
1444 strcpy(chosen_name
, mp
->path
);
1446 strcpy(chosen_name
, mp
->devnm
);
1449 /* Check in mdadm.conf for container == devname and
1450 * member == ra->text_version after second slash.
1452 char *sub
= strchr(ra
->text_version
+1, '/');
1453 struct mddev_ident
*array_list
;
1456 array_list
= conf_get_ident(NULL
);
1459 for(; array_list
; array_list
= array_list
->next
) {
1461 if (array_list
->member
== NULL
||
1462 array_list
->container
== NULL
)
1464 if (strcmp(array_list
->member
, sub
) != 0)
1466 if (array_list
->uuid_set
&&
1467 !same_uuid(ra
->uuid
, array_list
->uuid
, st
->ss
->swapuuid
))
1469 dn
= container2devname(array_list
->container
);
1472 if (strncmp(dn
, ra
->text_version
+1,
1474 ra
->text_version
[strlen(dn
)+1] != '/') {
1479 /* we have a match */
1482 pr_err("match found for member %s\n",
1483 array_list
->member
);
1487 if (match
&& match
->devname
&&
1488 strcasecmp(match
->devname
, "<ignore>") == 0) {
1490 pr_err("array %s/%s is "
1491 "explicitly ignored by mdadm.conf\n",
1492 match
->container
, match
->member
);
1496 trustworthy
= LOCAL
;
1498 mdfd
= create_mddev(match
? match
->devname
: NULL
,
1506 pr_err("failed to open %s: %s.\n",
1507 chosen_name
, strerror(errno
));
1511 assemble_container_content(st
, mdfd
, ra
, c
,
1516 /* don't move spares to container with volume being activated
1517 when all volumes are blocked */
1518 if (ra_all
== ra_blocked
)
1521 /* Now move all suitable spares from spare container */
1522 domains
= domain_from_array(list
, st
->ss
->name
);
1523 memcpy(suuid
, uuid_zero
, sizeof(int[4]));
1525 (smp
= map_by_uuid(&map
, suuid
)) != NULL
&&
1526 (sfd
= open(smp
->path
, O_RDONLY
)) >= 0) {
1527 /* spare container found */
1528 struct supertype
*sst
=
1529 super_imsm
.match_metadata_desc("imsm");
1530 struct mdinfo
*sinfo
;
1531 unsigned long long min_size
= 0;
1532 if (st
->ss
->min_acceptable_spare_size
)
1533 min_size
= st
->ss
->min_acceptable_spare_size(st
);
1534 if (!sst
->ss
->load_container(sst
, sfd
, NULL
)) {
1536 sinfo
= container_choose_spares(sst
, min_size
,
1539 sst
->ss
->free_super(sst
);
1542 struct mdinfo
*disks
= sinfo
->devs
;
1544 /* move spare from spare
1545 * container to currently
1551 makedev(disks
->disk
.major
,
1552 disks
->disk
.minor
)))
1554 disks
= disks
->next
;
1557 pr_err("Added %d spare%s to %s\n",
1558 count
, count
>1?"s":"", devname
);
1564 domain_free(domains
);
1569 * IncrementalRemove - Attempt to see if the passed in device belongs to any
1570 * raid arrays, and if so first fail (if needed) and then remove the device.
1572 * @devname - The device we want to remove
1573 * @id_path - name as found in /dev/disk/by-path for this device
1575 * Note: the device name must be a kernel name like "sda", so
1576 * that we can find it in /proc/mdstat
1578 int IncrementalRemove(char *devname
, char *id_path
, int verbose
)
1582 struct mdstat_ent
*ent
;
1583 struct mddev_dev devlist
;
1586 dprintf(Name
": incremental removal without --path <id_path> "
1587 "lacks the possibility to re-add new device in this "
1590 if (strchr(devname
, '/')) {
1591 pr_err("incremental removal requires a "
1592 "kernel device name, not a file: %s\n", devname
);
1595 ent
= mdstat_by_component(devname
);
1597 pr_err("%s does not appear to be a component "
1598 "of any array\n", devname
);
1601 mdfd
= open_dev(ent
->devnm
);
1603 pr_err("Cannot open array %s!!\n", ent
->dev
);
1609 struct map_ent
*map
= NULL
, *me
;
1610 me
= map_by_devnm(&map
, ent
->devnm
);
1612 policy_save_path(id_path
, me
);
1616 memset(&devlist
, 0, sizeof(devlist
));
1617 devlist
.devname
= devname
;
1618 devlist
.disposition
= 'f';
1619 /* for a container, we must fail each member array */
1620 if (ent
->metadata_version
&&
1621 strncmp(ent
->metadata_version
, "external:", 9) == 0) {
1622 struct mdstat_ent
*mdstat
= mdstat_read(0, 0);
1623 struct mdstat_ent
*memb
;
1624 for (memb
= mdstat
; memb
; memb
= memb
->next
)
1625 if (is_container_member(memb
, ent
->dev
)) {
1626 int subfd
= open_dev(memb
->devnm
);
1628 Manage_subdevs(memb
->dev
, subfd
,
1629 &devlist
, verbose
, 0,
1634 free_mdstat(mdstat
);
1636 Manage_subdevs(ent
->dev
, mdfd
, &devlist
, verbose
, 0, NULL
, 0);
1637 devlist
.disposition
= 'r';
1638 rv
= Manage_subdevs(ent
->dev
, mdfd
, &devlist
, verbose
, 0, NULL
, 0);