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