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