]> git.ipfire.org Git - thirdparty/mdadm.git/blame - Incremental.c
Fix component size checks in validate_super0.
[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
a5d85af7 205 st->ss->getinfo_super(st, &info, NULL);
8382f19b 206
3a97f210
N
207 /* 3/ Check if there is a match in mdadm.conf */
208 match = search_mdstat(st, &info, devname, verbose, &rv);
209 if (!match && rv == 2)
210 goto out;
8382f19b 211
112cace6
N
212 if (match && match->devname
213 && strcasecmp(match->devname, "<ignore>") == 0) {
214 if (verbose >= 0)
215 fprintf(stderr, Name ": array containing %s is explicitly"
216 " ignored by mdadm.conf\n",
217 devname);
15d4a7e4 218 goto out;
112cace6
N
219 }
220
7b403fef
N
221 /* 3a/ if not, check for homehost match. If no match, continue
222 * but don't trust the 'name' in the array. Thus a 'random' minor
223 * number will be assigned, and the device name will be based
224 * on that. */
215bb3f7
N
225 if (match)
226 trustworthy = LOCAL;
d1d3482b
N
227 else if (st->ss->match_home(st, homehost) == 1)
228 trustworthy = LOCAL;
229 else if (st->ss->match_home(st, "any") == 1)
230 trustworthy = LOCAL_ANY;
215bb3f7 231 else
d1d3482b
N
232 trustworthy = FOREIGN;
233
234
4e8d9f0a 235 if (!match && !conf_test_metadata(st->ss->name, policy,
d1d3482b
N
236 (trustworthy == LOCAL))) {
237 if (verbose >= 1)
238 fprintf(stderr, Name
239 ": %s has metadata type %s for which "
240 "auto-assembly is disabled\n",
241 devname, st->ss->name);
15d4a7e4 242 goto out;
d1d3482b
N
243 }
244 if (trustworthy == LOCAL_ANY)
ecb02e31 245 trustworthy = LOCAL;
e12e1d1d 246
4ef2f11e
N
247 /* There are three possible sources for 'autof': command line,
248 * ARRAY line in mdadm.conf, or CREATE line in mdadm.conf.
350ac35d
N
249 * ARRAY takes precedence, then command line, then
250 * CREATE.
4ef2f11e 251 */
350ac35d 252 if (match && match->autof)
4ef2f11e
N
253 autof = match->autof;
254 if (autof == 0)
255 autof = ci->autof;
256
7cdc0872 257 name_to_use = info.name;
0ac91628 258 if (name_to_use[0] == 0 &&
ecb02e31
N
259 info.array.level == LEVEL_CONTAINER &&
260 trustworthy == LOCAL) {
261 name_to_use = info.text_version;
262 trustworthy = METADATA;
263 }
0ac91628
N
264 if (name_to_use[0] && trustworthy != LOCAL &&
265 ! require_homehost &&
266 conf_name_is_free(name_to_use))
267 trustworthy = LOCAL;
ecb02e31 268
7cdc0872
N
269 /* strip "hostname:" prefix from name if we have decided
270 * to treat it as LOCAL
271 */
272 if (trustworthy == LOCAL && strchr(name_to_use, ':') != NULL)
273 name_to_use = strchr(name_to_use, ':')+1;
274
ad5bc697 275 /* 4/ Check if array exists.
215bb3f7 276 */
93c861ee
DL
277 if (map_lock(&map))
278 fprintf(stderr, Name ": failed to get exclusive lock on "
279 "mapfile\n");
215bb3f7 280 mp = map_by_uuid(&map, info.uuid);
339c2d6c
N
281 if (mp)
282 mdfd = open_dev(mp->devnum);
283 else
215bb3f7 284 mdfd = -1;
d7288ddc 285
8382f19b 286 if (mdfd < 0) {
8382f19b 287
215bb3f7 288 /* Couldn't find an existing array, maybe make a new one */
ffb3e494 289 mdfd = create_mddev(match ? match->devname : NULL,
e12e1d1d 290 name_to_use, autof, trustworthy, chosen_name);
215bb3f7
N
291
292 if (mdfd < 0)
15d4a7e4 293 goto out;
215bb3f7
N
294
295 sysfs_init(&info, mdfd, 0);
296
f35f2525
N
297 if (set_array_info(mdfd, st, &info) != 0) {
298 fprintf(stderr, Name ": failed to set array info for %s: %s\n",
8382f19b 299 chosen_name, strerror(errno));
15d4a7e4
N
300 rv = 2;
301 goto out;
8382f19b 302 }
7801ac20 303
c5afc314
N
304 dinfo = info;
305 dinfo.disk.major = major(stb.st_rdev);
306 dinfo.disk.minor = minor(stb.st_rdev);
307 if (add_disk(mdfd, st, &info, &dinfo) != 0) {
8382f19b
NB
308 fprintf(stderr, Name ": failed to add %s to %s: %s.\n",
309 devname, chosen_name, strerror(errno));
310 ioctl(mdfd, STOP_ARRAY, 0);
15d4a7e4
N
311 rv = 2;
312 goto out;
8382f19b 313 }
2bf740b3
N
314 sra = sysfs_read(mdfd, -1, (GET_DEVS | GET_STATE |
315 GET_OFFSET | GET_SIZE));
316
06c7f68e 317 if (!sra || !sra->devs || sra->devs->disk.raid_disk >= 0) {
8382f19b
NB
318 /* It really should be 'none' - must be old buggy
319 * kernel, and mdadm -I may not be able to complete.
320 * So reject it.
321 */
322 ioctl(mdfd, STOP_ARRAY, NULL);
323 fprintf(stderr, Name
324 ": You have an old buggy kernel which cannot support\n"
325 " --incremental reliably. Aborting.\n");
8382f19b 326 sysfs_free(sra);
15d4a7e4
N
327 rv = 2;
328 goto out;
8382f19b 329 }
c5afc314 330 info.array.working_disks = 1;
ad5bc697
N
331 /* 6/ Make sure /var/run/mdadm.map contains this array. */
332 map_update(&map, fd2devnum(mdfd),
333 info.text_version,
334 info.uuid, chosen_name);
8382f19b
NB
335 } else {
336 /* 5b/ if it does */
337 /* - check one drive in array to make sure metadata is a reasonably */
338 /* close match. Reject if not (e.g. different type) */
339 /* - add the device */
340 char dn[20];
341 int dfd2;
8382f19b 342 int err;
3da92f27 343 struct supertype *st2;
c5afc314 344 struct mdinfo info2, *d;
215bb3f7 345
2bf740b3
N
346 sra = sysfs_read(mdfd, -1, (GET_DEVS | GET_STATE |
347 GET_OFFSET | GET_SIZE));
348
339c2d6c
N
349 if (mp->path)
350 strcpy(chosen_name, mp->path);
351 else
352 strcpy(chosen_name, devnum2devname(mp->devnum));
215bb3f7 353
ef83fe7c
N
354 /* It is generally not OK to add non-spare drives to a
355 * running array as they are probably missing because
356 * they failed. However if runstop is 1, then the
7e83544b
N
357 * array was possibly started early and our best bet is
358 * to add this anyway.
359 * Also if action policy is re-add or better we allow
e5e5d7ce
N
360 * re-add.
361 * This doesn't apply to containers as the 'non-spare'
362 * flag has a different meaning. The test has to happen
363 * at the device level there
3a6ec29a 364 */
e5e5d7ce
N
365 if (!st->ss->external
366 && (info.disk.state & (1<<MD_DISK_SYNC)) != 0
7e83544b
N
367 && ! policy_action_allows(policy, st->ss->name,
368 act_re_add)
ef83fe7c 369 && runstop < 1) {
e5e5d7ce 370 if (ioctl(mdfd, GET_ARRAY_INFO, &ainf) == 0) {
3a6ec29a
N
371 fprintf(stderr, Name
372 ": not adding %s to active array (without --run) %s\n",
373 devname, chosen_name);
15d4a7e4
N
374 rv = 2;
375 goto out;
3a6ec29a
N
376 }
377 }
15d4a7e4
N
378 if (!sra) {
379 rv = 2;
380 goto out;
381 }
7c548327
N
382 if (sra->devs) {
383 sprintf(dn, "%d:%d", sra->devs->disk.major,
384 sra->devs->disk.minor);
385 dfd2 = dev_open(dn, O_RDONLY);
386 st2 = dup_super(st);
387 if (st2->ss->load_super(st2, dfd2, NULL) ||
388 st->ss->compare_super(st, st2) != 0) {
389 fprintf(stderr, Name
390 ": metadata mismatch between %s and "
391 "chosen array %s\n",
392 devname, chosen_name);
7c548327 393 close(dfd2);
15d4a7e4
N
394 rv = 2;
395 goto out;
7c548327 396 }
8382f19b 397 close(dfd2);
a5d85af7 398 st2->ss->getinfo_super(st2, &info2, NULL);
7c548327
N
399 st2->ss->free_super(st2);
400 if (info.array.level != info2.array.level ||
401 memcmp(info.uuid, info2.uuid, 16) != 0 ||
402 info.array.raid_disks != info2.array.raid_disks) {
403 fprintf(stderr, Name
404 ": unexpected difference between %s and %s.\n",
405 chosen_name, devname);
15d4a7e4
N
406 rv = 2;
407 goto out;
7c548327 408 }
8382f19b 409 }
7801ac20
N
410 info2.disk.major = major(stb.st_rdev);
411 info2.disk.minor = minor(stb.st_rdev);
c5afc314
N
412 /* add disk needs to know about containers */
413 if (st->ss->external)
414 sra->array.level = LEVEL_CONTAINER;
ac7de9d9 415 err = add_disk(mdfd, st, sra, &info2);
8382f19b
NB
416 if (err < 0 && errno == EBUSY) {
417 /* could be another device present with the same
418 * disk.number. Find and reject any such
419 */
420 find_reject(mdfd, st, sra, info.disk.number,
421 info.events, verbose, chosen_name);
ac7de9d9 422 err = add_disk(mdfd, st, sra, &info2);
8382f19b
NB
423 }
424 if (err < 0) {
425 fprintf(stderr, Name ": failed to add %s to %s: %s.\n",
426 devname, chosen_name, strerror(errno));
15d4a7e4
N
427 rv = 2;
428 goto out;
8382f19b 429 }
c5afc314
N
430 info.array.working_disks = 0;
431 for (d = sra->devs; d; d=d->next)
432 info.array.working_disks ++;
433
8382f19b 434 }
8382f19b
NB
435
436 /* 7/ Is there enough devices to possibly start the array? */
437 /* 7a/ if not, finish with success. */
352452c3 438 if (info.array.level == LEVEL_CONTAINER) {
7b0bbd0f 439 int devnum = devnum; /* defined and used iff ->external */
352452c3 440 /* Try to assemble within the container */
ad5bc697 441 map_unlock(&map);
97590376 442 sysfs_uevent(&info, "change");
c5afc314
N
443 if (verbose >= 0)
444 fprintf(stderr, Name
445 ": container %s now has %d devices\n",
446 chosen_name, info.array.working_disks);
a7c6e3fb 447 wait_for(chosen_name, mdfd);
ed690d36 448 if (st->ss->external)
983fff45 449 devnum = fd2devnum(mdfd);
a7c6e3fb 450 close(mdfd);
2bf740b3 451 sysfs_free(sra);
03b7f6c6 452 rv = Incremental(chosen_name, verbose, runstop,
0ac91628 453 NULL, homehost, require_homehost, autof);
03b7f6c6
N
454 if (rv == 1)
455 /* Don't fail the whole -I if a subarray didn't
456 * have enough devices to start yet
457 */
458 rv = 0;
ed690d36
PC
459 /* after spare is added, ping monitor for external metadata
460 * so that it can eg. try to rebuild degraded array */
983fff45
AK
461 if (st->ss->external)
462 ping_monitor_by_id(devnum);
03b7f6c6 463 return rv;
352452c3 464 }
de6ae750 465
2bf740b3
N
466 /* We have added something to the array, so need to re-read the
467 * state. Eventually this state should be kept up-to-date as
468 * things change.
469 */
470 sysfs_free(sra);
471 sra = sysfs_read(mdfd, -1, (GET_DEVS | GET_STATE |
472 GET_OFFSET | GET_SIZE));
473 active_disks = count_active(st, sra, mdfd, &avail, &info);
8382f19b
NB
474 if (enough(info.array.level, info.array.raid_disks,
475 info.array.layout, info.array.state & 1,
3288b419 476 avail, active_disks) == 0) {
8382f19b
NB
477 if (verbose >= 0)
478 fprintf(stderr, Name
479 ": %s attached to %s, not enough to start (%d).\n",
480 devname, chosen_name, active_disks);
ad5bc697 481 map_unlock(&map);
15d4a7e4
N
482 rv = 0;
483 goto out;
8382f19b 484 }
8382f19b
NB
485
486 /* 7b/ if yes, */
487 /* - if number of OK devices match expected, or -R and there */
488 /* are enough, */
489 /* + add any bitmap file */
490 /* + start the array (auto-readonly). */
8382f19b
NB
491
492 if (ioctl(mdfd, GET_ARRAY_INFO, &ainf) == 0) {
493 if (verbose >= 0)
494 fprintf(stderr, Name
495 ": %s attached to %s which is already active.\n",
496 devname, chosen_name);
ad5bc697 497 map_unlock(&map);
15d4a7e4
N
498 rv = 0;
499 goto out;
8382f19b 500 }
ad5bc697
N
501
502 map_unlock(&map);
8382f19b 503 if (runstop > 0 || active_disks >= info.array.working_disks) {
2bf740b3 504 struct mdinfo *dsk;
8382f19b
NB
505 /* Let's try to start it */
506 if (match && match->bitmap_file) {
507 int bmfd = open(match->bitmap_file, O_RDWR);
508 if (bmfd < 0) {
509 fprintf(stderr, Name
510 ": Could not open bitmap file %s.\n",
511 match->bitmap_file);
15d4a7e4 512 goto out;
8382f19b
NB
513 }
514 if (ioctl(mdfd, SET_BITMAP_FILE, bmfd) != 0) {
515 close(bmfd);
516 fprintf(stderr, Name
517 ": Failed to set bitmapfile for %s.\n",
518 chosen_name);
15d4a7e4 519 goto out;
8382f19b
NB
520 }
521 close(bmfd);
522 }
de6ae750
N
523 /* Need to remove from the array any devices which
524 * 'count_active' discerned were too old or inappropriate
525 */
526 for (d = sra ? sra->devs : NULL ; d ; d = d->next)
527 if (d->disk.state & (1<<MD_DISK_REMOVED))
528 remove_disk(mdfd, st, sra, d);
2bf740b3 529
7b403fef 530 if ((sra == NULL || active_disks >= info.array.working_disks)
215bb3f7 531 && trustworthy != FOREIGN)
8382f19b
NB
532 rv = ioctl(mdfd, RUN_ARRAY, NULL);
533 else
534 rv = sysfs_set_str(sra, NULL,
535 "array_state", "read-auto");
536 if (rv == 0) {
537 if (verbose >= 0)
538 fprintf(stderr, Name
7e83544b 539 ": %s attached to %s, which has been started.\n",
8382f19b
NB
540 devname, chosen_name);
541 rv = 0;
a7c6e3fb 542 wait_for(chosen_name, mdfd);
7e83544b
N
543 /* We just started the array, so some devices
544 * might have been evicted from the array
545 * because their event counts were too old.
546 * If the action=re-add policy is in-force for
547 * those devices we should re-add them now.
548 */
549 for (dsk = sra->devs; dsk ; dsk = dsk->next) {
550 if (disk_action_allows(dsk, st->ss->name, act_re_add) &&
551 add_disk(mdfd, st, sra, dsk) == 0)
552 fprintf(stderr, Name
553 ": %s re-added to %s\n",
554 dsk->sys_name, chosen_name);
555 }
8382f19b
NB
556 } else {
557 fprintf(stderr, Name
558 ": %s attached to %s, but failed to start: %s.\n",
559 devname, chosen_name, strerror(errno));
560 rv = 1;
561 }
562 } else {
563 if (verbose >= 0)
564 fprintf(stderr, Name
565 ": %s attached to %s, not enough to start safely.\n",
566 devname, chosen_name);
567 rv = 0;
568 }
15d4a7e4 569out:
de6ae750 570 free(avail);
15d4a7e4
N
571 if (dfd >= 0)
572 close(dfd);
573 if (mdfd >= 0)
574 close(mdfd);
7e83544b
N
575 if (policy)
576 dev_policy_free(policy);
2bf740b3
N
577 if (sra)
578 sysfs_free(sra);
8382f19b
NB
579 return rv;
580}
581
fa56eddb 582static struct mddev_ident *search_mdstat(struct supertype *st,
3a97f210
N
583 struct mdinfo *info,
584 char *devname,
585 int verbose, int *rvp)
586{
fa56eddb 587 struct mddev_ident *array_list, *match;
3a97f210
N
588 array_list = conf_get_ident(NULL);
589 match = NULL;
590 for (; array_list; array_list = array_list->next) {
591 if (array_list->uuid_set &&
592 same_uuid(array_list->uuid, info->uuid, st->ss->swapuuid)
593 == 0) {
594 if (verbose >= 2 && array_list->devname)
595 fprintf(stderr, Name
596 ": UUID differs from %s.\n",
597 array_list->devname);
598 continue;
599 }
600 if (array_list->name[0] &&
601 strcasecmp(array_list->name, info->name) != 0) {
602 if (verbose >= 2 && array_list->devname)
603 fprintf(stderr, Name
604 ": Name differs from %s.\n",
605 array_list->devname);
606 continue;
607 }
608 if (array_list->devices &&
609 !match_oneof(array_list->devices, devname)) {
610 if (verbose >= 2 && array_list->devname)
611 fprintf(stderr, Name
612 ": Not a listed device for %s.\n",
613 array_list->devname);
614 continue;
615 }
616 if (array_list->super_minor != UnSet &&
617 array_list->super_minor != info->array.md_minor) {
618 if (verbose >= 2 && array_list->devname)
619 fprintf(stderr, Name
620 ": Different super-minor to %s.\n",
621 array_list->devname);
622 continue;
623 }
624 if (!array_list->uuid_set &&
625 !array_list->name[0] &&
626 !array_list->devices &&
627 array_list->super_minor == UnSet) {
628 if (verbose >= 2 && array_list->devname)
629 fprintf(stderr, Name
630 ": %s doesn't have any identifying information.\n",
631 array_list->devname);
632 continue;
633 }
634 /* FIXME, should I check raid_disks and level too?? */
635
636 if (match) {
637 if (verbose >= 0) {
638 if (match->devname && array_list->devname)
639 fprintf(stderr, Name
640 ": we match both %s and %s - cannot decide which to use.\n",
641 match->devname, array_list->devname);
642 else
643 fprintf(stderr, Name
644 ": multiple lines in mdadm.conf match\n");
645 }
646 *rvp = 2;
647 match = NULL;
648 break;
649 }
650 match = array_list;
651 }
652 return match;
653}
654
7e0f6979 655static void find_reject(int mdfd, struct supertype *st, struct mdinfo *sra,
8382f19b
NB
656 int number, __u64 events, int verbose,
657 char *array_name)
658{
3da92f27 659 /* Find a device attached to this array with a disk.number of number
8382f19b
NB
660 * and events less than the passed events, and remove the device.
661 */
06c7f68e 662 struct mdinfo *d;
8382f19b
NB
663 mdu_array_info_t ra;
664
665 if (ioctl(mdfd, GET_ARRAY_INFO, &ra) == 0)
666 return; /* not safe to remove from active arrays
667 * without thinking more */
668
669 for (d = sra->devs; d ; d = d->next) {
670 char dn[10];
671 int dfd;
8382f19b 672 struct mdinfo info;
06c7f68e 673 sprintf(dn, "%d:%d", d->disk.major, d->disk.minor);
8382f19b
NB
674 dfd = dev_open(dn, O_RDONLY);
675 if (dfd < 0)
676 continue;
3da92f27 677 if (st->ss->load_super(st, dfd, NULL)) {
8382f19b
NB
678 close(dfd);
679 continue;
680 }
a5d85af7 681 st->ss->getinfo_super(st, &info, NULL);
3da92f27 682 st->ss->free_super(st);
8382f19b
NB
683 close(dfd);
684
685 if (info.disk.number != number ||
686 info.events >= events)
687 continue;
688
06c7f68e 689 if (d->disk.raid_disk > -1)
8382f19b
NB
690 sysfs_set_str(sra, d, "slot", "none");
691 if (sysfs_set_str(sra, d, "state", "remove") == 0)
692 if (verbose >= 0)
693 fprintf(stderr, Name
694 ": removing old device %s from %s\n",
06c7f68e 695 d->sys_name+4, array_name);
8382f19b
NB
696 }
697}
698
2bf740b3
N
699static int count_active(struct supertype *st, struct mdinfo *sra,
700 int mdfd, char **availp,
8382f19b
NB
701 struct mdinfo *bestinfo)
702{
703 /* count how many devices in sra think they are active */
06c7f68e 704 struct mdinfo *d;
de6ae750 705 int cnt = 0;
8382f19b 706 __u64 max_events = 0;
8382f19b 707 char *avail = NULL;
e4c72d1d 708 int *best = NULL;
de6ae750
N
709 char *devmap = NULL;
710 int numdevs = 0;
711 int devnum;
712 int b, i;
713 int raid_disks = 0;
8382f19b 714
b526e52d
DW
715 if (!sra)
716 return 0;
717
de6ae750
N
718 for (d = sra->devs ; d ; d = d->next)
719 numdevs++;
720 for (d = sra->devs, devnum=0 ; d ; d = d->next, devnum++) {
8382f19b
NB
721 char dn[30];
722 int dfd;
8382f19b
NB
723 int ok;
724 struct mdinfo info;
725
06c7f68e 726 sprintf(dn, "%d:%d", d->disk.major, d->disk.minor);
8382f19b
NB
727 dfd = dev_open(dn, O_RDONLY);
728 if (dfd < 0)
729 continue;
3da92f27 730 ok = st->ss->load_super(st, dfd, NULL);
8382f19b
NB
731 close(dfd);
732 if (ok != 0)
733 continue;
de6ae750
N
734 info.array.raid_disks = raid_disks;
735 st->ss->getinfo_super(st, &info, devmap + raid_disks * devnum);
43aaf431 736 if (!avail) {
de6ae750
N
737 raid_disks = info.array.raid_disks;
738 avail = calloc(raid_disks, 1);
43aaf431
DL
739 if (!avail) {
740 fprintf(stderr, Name ": out of memory.\n");
741 exit(1);
742 }
43aaf431 743 *availp = avail;
de6ae750
N
744
745 best = calloc(raid_disks, sizeof(int));
746 devmap = calloc(raid_disks * numdevs, 1);
747
748 st->ss->getinfo_super(st, &info, devmap);
43aaf431
DL
749 }
750
8382f19b
NB
751 if (info.disk.state & (1<<MD_DISK_SYNC))
752 {
8382f19b
NB
753 if (cnt == 0) {
754 cnt++;
755 max_events = info.events;
756 avail[info.disk.raid_disk] = 2;
de6ae750 757 best[info.disk.raid_disk] = devnum;
a5d85af7 758 st->ss->getinfo_super(st, bestinfo, NULL);
8382f19b 759 } else if (info.events == max_events) {
8382f19b 760 avail[info.disk.raid_disk] = 2;
de6ae750 761 best[info.disk.raid_disk] = devnum;
8382f19b 762 } else if (info.events == max_events-1) {
de6ae750
N
763 if (avail[info.disk.raid_disk] == 0) {
764 avail[info.disk.raid_disk] = 1;
765 best[info.disk.raid_disk] = devnum;
766 }
8382f19b
NB
767 } else if (info.events < max_events - 1)
768 ;
769 else if (info.events == max_events+1) {
770 int i;
8382f19b 771 max_events = info.events;
de6ae750 772 for (i=0; i < raid_disks; i++)
8382f19b
NB
773 if (avail[i])
774 avail[i]--;
775 avail[info.disk.raid_disk] = 2;
de6ae750 776 best[info.disk.raid_disk] = devnum;
a5d85af7 777 st->ss->getinfo_super(st, bestinfo, NULL);
8382f19b 778 } else { /* info.events much bigger */
47c7a4be 779 memset(avail, 0, raid_disks);
8382f19b 780 max_events = info.events;
1d1a9f87 781 avail[info.disk.raid_disk] = 2;
47c7a4be 782 best[info.disk.raid_disk] = devnum;
a5d85af7 783 st->ss->getinfo_super(st, bestinfo, NULL);
8382f19b
NB
784 }
785 }
3da92f27 786 st->ss->free_super(st);
8382f19b 787 }
de6ae750
N
788 if (!avail)
789 return 0;
790 /* We need to reject any device that thinks the best device is
791 * failed or missing */
792 for (b = 0; b < raid_disks; b++)
793 if (avail[b] == 2)
794 break;
795 cnt = 0;
796 for (i = 0 ; i < raid_disks ; i++) {
797 if (i != b && avail[i])
798 if (devmap[raid_disks * best[i] + b] == 0) {
799 /* This device thinks 'b' is failed -
800 * don't use it */
801 devnum = best[i];
802 for (d=sra->devs ; devnum; d = d->next)
803 devnum--;
804 d->disk.state |= (1 << MD_DISK_REMOVED);
805 avail[i] = 0;
806 }
807 if (avail[i])
808 cnt++;
809 }
810 free(best);
811 free(devmap);
812 return cnt;
8382f19b
NB
813}
814
5be68a07
PC
815/* test if container has degraded member(s) */
816static int container_members_max_degradation(struct map_ent *map, struct map_ent *me)
817{
818 mdu_array_info_t array;
819 int afd;
820 int max_degraded = 0;
821
822 for(; map; map = map->next) {
823 if (!is_subarray(map->metadata) ||
824 devname2devnum(map->metadata+1) != me->devnum)
825 continue;
826 afd = open_dev(map->devnum);
827 if (afd < 0)
828 continue;
829 /* most accurate information regarding array degradation */
830 if (ioctl(afd, GET_ARRAY_INFO, &array) >= 0) {
831 int degraded = array.raid_disks - array.active_disks -
832 array.spare_disks;
833 if (degraded > max_degraded)
834 max_degraded = degraded;
835 }
836 close(afd);
837 }
838 return (max_degraded);
839}
840
56e8be85 841static int array_try_spare(char *devname, int *dfdp, struct dev_policy *pol,
d2db3045 842 struct map_ent *target, int bare,
56e8be85 843 struct supertype *st, int verbose)
f08605b3
N
844{
845 /* This device doesn't have any md metadata
d2db3045
N
846 * The device policy allows 'spare' and if !bare, it allows spare-same-slot.
847 * If 'st' is not set, then we only know that some metadata allows this,
848 * others possibly don't.
849 * So look for a container or array to attach the device to.
850 * Prefer 'target' if that is set and the array is found.
851 *
f08605b3
N
852 * If st is set, then only arrays of that type are considered
853 * Return 0 on success, or some exit code on failure, probably 1.
854 */
8659d089 855 int rv = 1;
f08605b3
N
856 struct stat stb;
857 struct map_ent *mp, *map = NULL;
858 struct mdinfo *chosen = NULL;
859 int dfd = *dfdp;
860
f08605b3
N
861 if (fstat(dfd, &stb) != 0)
862 return 1;
f08605b3 863
56e8be85 864 /*
f08605b3
N
865 * Now we need to find a suitable array to add this to.
866 * We only accept arrays that:
867 * - match 'st'
868 * - are in the same domains as the device
869 * - are of an size for which the device will be useful
870 * and we choose the one that is the most degraded
871 */
872
873 if (map_lock(&map)) {
874 fprintf(stderr, Name ": failed to get exclusive lock on "
875 "mapfile\n");
876 return 1;
877 }
878 for (mp = map ; mp ; mp = mp->next) {
879 struct supertype *st2;
880 struct domainlist *dl = NULL;
881 struct mdinfo *sra;
882 unsigned long long devsize;
71204a50 883 unsigned long long component_size = 0;
f08605b3
N
884
885 if (is_subarray(mp->metadata))
886 continue;
887 if (st) {
888 st2 = st->ss->match_metadata_desc(mp->metadata);
889 if (!st2 ||
890 (st->minor_version >= 0 &&
891 st->minor_version != st2->minor_version)) {
892 if (verbose > 1)
893 fprintf(stderr, Name ": not adding %s to %s as metadata type doesn't match\n",
894 devname, mp->path);
895 free(st2);
896 continue;
897 }
898 free(st2);
899 }
900 sra = sysfs_read(-1, mp->devnum,
901 GET_DEVS|GET_OFFSET|GET_SIZE|GET_STATE|
902 GET_DEGRADED|GET_COMPONENT|GET_VERSION);
903 if (!sra) {
904 /* Probably a container - no degraded info */
905 sra = sysfs_read(-1, mp->devnum,
906 GET_DEVS|GET_OFFSET|GET_SIZE|GET_STATE|
907 GET_COMPONENT|GET_VERSION);
908 if (sra)
5be68a07 909 sra->array.failed_disks = -1;
f08605b3
N
910 }
911 if (!sra)
912 continue;
913 if (st == NULL) {
914 int i;
915 st2 = NULL;
916 for(i=0; !st2 && superlist[i]; i++)
917 st2 = superlist[i]->match_metadata_desc(
918 sra->text_version);
6e57f80a
N
919 if (!st2) {
920 if (verbose > 1)
921 fprintf(stderr, Name ": not adding %s to %s"
922 " as metadata not recognised.\n",
923 devname, mp->path);
924 goto next;
925 }
625b2507
N
926 /* Need to double check the 'act_spare' permissions applies
927 * to this metadata.
928 */
929 if (!policy_action_allows(pol, st2->ss->name, act_spare))
930 goto next;
d2db3045
N
931 if (!bare && !policy_action_allows(pol, st2->ss->name,
932 act_spare_same_slot))
933 goto next;
f08605b3
N
934 } else
935 st2 = st;
5be68a07
PC
936 /* update number of failed disks for mostly degraded
937 * container member */
938 if (sra->array.failed_disks == -1)
939 sra->array.failed_disks = container_members_max_degradation(map, mp);
940
f08605b3 941 get_dev_size(dfd, NULL, &devsize);
48865704
PC
942 if (sra->component_size == 0) {
943 /* true for containers, here we must read superblock
944 * to obtain minimum spare size */
945 struct supertype *st3 = dup_super(st2);
946 int mdfd = open_dev(mp->devnum);
947 if (!mdfd)
948 goto next;
949 if (st3->ss->load_container &&
950 !st3->ss->load_container(st3, mdfd, mp->path)) {
951 component_size = st3->ss->min_acceptable_spare_size(st3);
952 st3->ss->free_super(st3);
953 }
954 free(st3);
955 close(mdfd);
956 }
957 if ((sra->component_size > 0 &&
958 st2->ss->avail_size(st2, devsize) < sra->component_size)
959 ||
960 (sra->component_size == 0 && devsize < component_size)) {
f08605b3
N
961 if (verbose > 1)
962 fprintf(stderr, Name ": not adding %s to %s as it is too small\n",
963 devname, mp->path);
964 goto next;
965 }
d2db3045
N
966 /* test against target.
967 * If 'target' is set and 'bare' is false, we only accept
968 * arrays/containers that match 'target'.
969 * If 'target' is set and 'bare' is true, we prefer the
970 * array which matches 'target'.
b4924220 971 * target is considered only if we deal with degraded array
d2db3045 972 */
fee6a49e
PC
973 if (target && policy_action_allows(pol, st2->ss->name,
974 act_spare_same_slot)) {
d2db3045
N
975 if (strcmp(target->metadata, mp->metadata) == 0 &&
976 memcmp(target->uuid, mp->uuid,
b4924220
PC
977 sizeof(target->uuid)) == 0 &&
978 sra->array.failed_disks > 0) {
d2db3045
N
979 /* This is our target!! */
980 if (chosen)
981 sysfs_free(chosen);
982 chosen = sra;
983 sra = NULL;
984 /* skip to end so we don't check any more */
985 while (mp->next)
986 mp = mp->next;
987 goto next;
988 }
989 /* not our target */
990 if (!bare)
991 goto next;
992 }
993
b6b3f0f7 994 dl = domain_from_array(sra, st2->ss->name);
e5508b36 995 if (domain_test(dl, pol, st2->ss->name) != 1) {
b6b3f0f7
PC
996 /* domain test fails */
997 if (verbose > 1)
998 fprintf(stderr, Name ": not adding %s to %s as"
999 " it is not in a compatible domain\n",
1000 devname, mp->path);
1001
1002 goto next;
1003 }
f08605b3
N
1004 /* all tests passed, OK to add to this array */
1005 if (!chosen) {
1006 chosen = sra;
1007 sra = NULL;
1008 } else if (chosen->array.failed_disks < sra->array.failed_disks) {
1009 sysfs_free(chosen);
1010 chosen = sra;
1011 sra = NULL;
1012 }
1013 next:
1014 if (sra)
1015 sysfs_free(sra);
1016 if (st != st2)
1017 free(st2);
1018 if (dl)
1019 domain_free(dl);
1020 }
1021 if (chosen) {
1022 /* add current device to chosen array as a spare */
1023 int mdfd = open_dev(devname2devnum(chosen->sys_name));
1024 if (mdfd >= 0) {
a655e550 1025 struct mddev_dev devlist;
f08605b3
N
1026 char devname[20];
1027 devlist.next = NULL;
1028 devlist.used = 0;
1029 devlist.re_add = 0;
1030 devlist.writemostly = 0;
1031 devlist.devname = devname;
1032 sprintf(devname, "%d:%d", major(stb.st_rdev),
1033 minor(stb.st_rdev));
1034 devlist.disposition = 'a';
1035 close(dfd);
1036 *dfdp = -1;
1037 rv = Manage_subdevs(chosen->sys_name, mdfd, &devlist,
833bb0f8 1038 -1, 0, NULL);
f08605b3
N
1039 close(mdfd);
1040 }
1041 if (verbose > 0) {
1042 if (rv == 0)
1043 fprintf(stderr, Name ": added %s as spare for %s\n",
1044 devname, chosen->sys_name);
1045 else
1046 fprintf(stderr, Name ": failed to add %s as spare for %s\n",
1047 devname, chosen->sys_name);
1048 }
1049 sysfs_free(chosen);
1050 }
8659d089 1051 return rv;
f08605b3
N
1052}
1053
56e8be85
N
1054static int partition_try_spare(char *devname, int *dfdp, struct dev_policy *pol,
1055 struct supertype *st, int verbose)
1056{
61018da0
N
1057 /* we know that at least one partition virtual-metadata is
1058 * allowed to incorporate spares like this device. We need to
1059 * find a suitable device to copy partition information from.
1060 *
1061 * Getting a list of all disk (not partition) devices is
1062 * slightly non-trivial. We could look at /sys/block, but
1063 * that is theoretically due to be removed. Maybe best to use
1064 * /dev/disk/by-path/?* and ignore names ending '-partNN' as
1065 * we depend on this directory of 'path' info. But that fails
1066 * to find loop devices and probably others. Maybe don't
1067 * worry about that, they aren't the real target.
1068 *
1069 * So: check things in /dev/disk/by-path to see if they are in
1070 * a compatible domain, then load the partition table and see
1071 * if it is OK for the new device, and choose the largest
1072 * partition table that fits.
1073 */
1074 DIR *dir;
1075 struct dirent *de;
1076 char *chosen = NULL;
71204a50 1077 unsigned long long chosen_size = 0;
61018da0
N
1078 struct supertype *chosen_st = NULL;
1079 int fd;
1080
1081 dir = opendir("/dev/disk/by-path");
1082 if (!dir)
1083 return 1;
1084 while ((de = readdir(dir)) != NULL) {
1085 char *ep;
1086 struct dev_policy *pol2 = NULL;
1087 struct domainlist *domlist = NULL;
1088 int fd = -1;
1089 struct mdinfo info;
1090 struct supertype *st2 = NULL;
1091 char *devname = NULL;
1092 unsigned long long devsectors;
1093
1094 if (de->d_ino == 0 ||
1095 de->d_name[0] == '.' ||
1096 (de->d_type != DT_LNK && de->d_type != DT_UNKNOWN))
1097 goto next;
1098
1099 ep = de->d_name + strlen(de->d_name);
1100 while (ep > de->d_name &&
1101 isdigit(ep[-1]))
1102 ep--;
1103 if (ep > de->d_name + 5 &&
1104 strncmp(ep-5, "-part", 5) == 0)
1105 /* This is a partition - skip it */
1106 goto next;
1107
1108 pol2 = path_policy(de->d_name, type_disk);
1109
1110 domain_merge(&domlist, pol2, st ? st->ss->name : NULL);
e5508b36 1111 if (domain_test(domlist, pol, st ? st->ss->name : NULL) != 1)
61018da0
N
1112 /* new device is incompatible with this device. */
1113 goto next;
1114
1115 domain_free(domlist);
1116 domlist = NULL;
1117
71204a50
N
1118 if (asprintf(&devname, "/dev/disk/by-path/%s", de->d_name) != 1) {
1119 devname = NULL;
1120 goto next;
1121 }
61018da0
N
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);
e5508b36 1140 if (domain_test(domlist, pol, st2->ss->name) != 1)
61018da0
N
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 1436
47c74f3f
N
1437 st->ss->getinfo_super(st, &info, NULL);
1438
1439 if ((runstop > 0 && info.container_enough >= 0) ||
1440 info.container_enough > 0)
1441 /* pass */;
1442 else {
1443 if (verbose)
1444 fprintf(stderr, Name ": not enough devices to start the container\n");
1445 return 0;
1446 }
1447
1448 match = search_mdstat(st, &info, devname, verbose, &rv);
1449 if (match == NULL && rv == 2)
1450 return rv;
1451
1452 /* Need to compute 'trustworthy' */
1453 if (match)
1454 trustworthy = LOCAL;
1455 else if (st->ss->match_home(st, homehost) == 1)
1456 trustworthy = LOCAL;
1457 else if (st->ss->match_home(st, "any") == 1)
1458 trustworthy = LOCAL;
1459 else
1460 trustworthy = FOREIGN;
ad5bc697 1461
47c74f3f 1462 list = st->ss->container_content(st, NULL);
93c861ee
DL
1463 if (map_lock(&map))
1464 fprintf(stderr, Name ": failed to get exclusive lock on "
1465 "mapfile\n");
a06d022d 1466 /* do not assemble arrays that might have bad blocks */
75c2df65 1467 if (list && list->array.state & (1<<MD_SB_BBM_ERRORS)) {
a06d022d
KW
1468 fprintf(stderr, Name ": BBM log found in metadata. "
1469 "Cannot activate array(s).\n");
51cf7419
KW
1470 /* free container data and exit */
1471 sysfs_free(list);
1472 return 2;
a06d022d 1473 }
598f0d58
NB
1474
1475 for (ra = list ; ra ; ra = ra->next) {
598f0d58
NB
1476 int mdfd;
1477 char chosen_name[1024];
ad5bc697 1478 struct map_ent *mp;
fa56eddb 1479 struct mddev_ident *match = NULL;
598f0d58 1480
c5afc314 1481 mp = map_by_uuid(&map, ra->uuid);
d7288ddc 1482
30926600
N
1483 if (mp) {
1484 mdfd = open_dev(mp->devnum);
339c2d6c
N
1485 if (mp->path)
1486 strcpy(chosen_name, mp->path);
1487 else
1488 strcpy(chosen_name, devnum2devname(mp->devnum));
30926600 1489 } else {
dbb44303 1490
112cace6 1491 /* Check in mdadm.conf for container == devname and
dbb44303
N
1492 * member == ra->text_version after second slash.
1493 */
1494 char *sub = strchr(ra->text_version+1, '/');
fa56eddb 1495 struct mddev_ident *array_list;
dbb44303
N
1496 if (sub) {
1497 sub++;
1498 array_list = conf_get_ident(NULL);
1499 } else
1500 array_list = NULL;
1501 for(; array_list ; array_list = array_list->next) {
dbb44303
N
1502 char *dn;
1503 if (array_list->member == NULL ||
1504 array_list->container == NULL)
1505 continue;
1506 if (strcmp(array_list->member, sub) != 0)
1507 continue;
71d60c48
DW
1508 if (array_list->uuid_set &&
1509 !same_uuid(ra->uuid, array_list->uuid, st->ss->swapuuid))
1510 continue;
1771a6e2
N
1511 dn = container2devname(array_list->container);
1512 if (dn == NULL)
dbb44303 1513 continue;
dbb44303
N
1514 if (strncmp(dn, ra->text_version+1,
1515 strlen(dn)) != 0 ||
1516 ra->text_version[strlen(dn)+1] != '/') {
1517 free(dn);
1518 continue;
1519 }
1520 free(dn);
1521 /* we have a match */
1522 match = array_list;
71d60c48
DW
1523 if (verbose>0)
1524 fprintf(stderr, Name ": match found for member %s\n",
1525 array_list->member);
dbb44303
N
1526 break;
1527 }
dbb44303 1528
112cace6
N
1529 if (match && match->devname &&
1530 strcasecmp(match->devname, "<ignore>") == 0) {
1531 if (verbose > 0)
1532 fprintf(stderr, Name ": array %s/%s is "
1533 "explicitly ignored by mdadm.conf\n",
1534 match->container, match->member);
1535 return 2;
1536 }
05833051
N
1537 if (match)
1538 trustworthy = LOCAL;
112cace6 1539
30926600
N
1540 mdfd = create_mddev(match ? match->devname : NULL,
1541 ra->name,
1542 autof,
1543 trustworthy,
1544 chosen_name);
1545 }
598f0d58
NB
1546
1547 if (mdfd < 0) {
1548 fprintf(stderr, Name ": failed to open %s: %s.\n",
1549 chosen_name, strerror(errno));
1550 return 2;
1551 }
1552
03b7f6c6 1553 assemble_container_content(st, mdfd, ra, runstop,
49680258 1554 chosen_name, verbose, NULL);
588bebfc 1555 close(mdfd);
598f0d58 1556 }
037e12d6
AC
1557
1558 /* Now move all suitable spares from spare container */
1559 domains = domain_from_array(list, st->ss->name);
1560 memcpy(suuid, uuid_zero, sizeof(int[4]));
1561 if (domains &&
1562 (smp = map_by_uuid(&map, suuid)) != NULL &&
1563 (sfd = open(smp->path, O_RDONLY)) >= 0) {
1564 /* spare container found */
1565 struct supertype *sst =
1566 super_imsm.match_metadata_desc("imsm");
1567 struct mdinfo *sinfo;
1568 unsigned long long min_size = 0;
1569 if (st->ss->min_acceptable_spare_size)
1570 min_size = st->ss->min_acceptable_spare_size(st);
1571 if (!sst->ss->load_container(sst, sfd, NULL)) {
1572 close(sfd);
1573 sinfo = container_choose_spares(sst, min_size,
1574 domains, NULL,
1575 st->ss->name, 0);
1576 sst->ss->free_super(sst);
1577 if (sinfo){
1578 int count = 0;
1579 struct mdinfo *disks = sinfo->devs;
1580 while (disks) {
1581 /* move spare from spare
1582 * container to currently
1583 * assembled one
1584 */
1585 if (move_spare(
1586 smp->path,
1587 devname,
1588 makedev(disks->disk.major,
1589 disks->disk.minor)))
1590 count++;
1591 disks = disks->next;
1592 }
1593 if (count)
1594 fprintf(stderr, Name
1595 ": Added %d spare%s to %s\n",
1596 count, count>1?"s":"", devname);
1597 }
1598 sysfs_free(sinfo);
1599 } else
1600 close(sfd);
1601 }
1602 domain_free(domains);
ad5bc697 1603 map_unlock(&map);
598f0d58
NB
1604 return 0;
1605}
29ba4804
N
1606
1607/*
1608 * IncrementalRemove - Attempt to see if the passed in device belongs to any
1609 * raid arrays, and if so first fail (if needed) and then remove the device.
1610 *
1611 * @devname - The device we want to remove
950bc344 1612 * @id_path - name as found in /dev/disk/by-path for this device
29ba4804
N
1613 *
1614 * Note: the device name must be a kernel name like "sda", so
1615 * that we can find it in /proc/mdstat
1616 */
950bc344 1617int IncrementalRemove(char *devname, char *id_path, int verbose)
29ba4804
N
1618{
1619 int mdfd;
aae3cdc3 1620 int rv;
29ba4804 1621 struct mdstat_ent *ent;
a655e550 1622 struct mddev_dev devlist;
29ba4804 1623
950bc344
PC
1624 if (!id_path)
1625 dprintf(Name ": incremental removal without --path <id_path> "
1626 "lacks the possibility to re-add new device in this "
1627 "port\n");
1628
29ba4804
N
1629 if (strchr(devname, '/')) {
1630 fprintf(stderr, Name ": incremental removal requires a "
1631 "kernel device name, not a file: %s\n", devname);
1632 return 1;
1633 }
1634 ent = mdstat_by_component(devname);
1635 if (!ent) {
1636 fprintf(stderr, Name ": %s does not appear to be a component "
1637 "of any array\n", devname);
1638 return 1;
1639 }
1640 mdfd = open_dev(ent->devnum);
1641 if (mdfd < 0) {
1642 fprintf(stderr, Name ": Cannot open array %s!!\n", ent->dev);
08387a04 1643 free_mdstat(ent);
29ba4804
N
1644 return 1;
1645 }
403410eb
PC
1646
1647 if (id_path) {
1648 struct map_ent *map = NULL, *me;
1649 me = map_by_devnum(&map, ent->devnum);
1650 if (me)
1651 policy_save_path(id_path, me);
1652 map_free(map);
1653 }
1654
29ba4804
N
1655 memset(&devlist, 0, sizeof(devlist));
1656 devlist.devname = devname;
1657 devlist.disposition = 'f';
08387a04
N
1658 /* for a container, we must fail each member array */
1659 if (ent->metadata_version &&
1660 strncmp(ent->metadata_version, "external:", 9) == 0) {
1661 struct mdstat_ent *mdstat = mdstat_read(0, 0);
1662 struct mdstat_ent *memb;
1663 for (memb = mdstat ; memb ; memb = memb->next)
1664 if (is_container_member(memb, ent->dev)) {
1665 int subfd = open_dev(memb->devnum);
1666 if (subfd >= 0) {
1667 Manage_subdevs(memb->dev, subfd,
833bb0f8
N
1668 &devlist, verbose, 0,
1669 NULL);
08387a04
N
1670 close(subfd);
1671 }
1672 }
1673 free_mdstat(mdstat);
1674 } else
833bb0f8 1675 Manage_subdevs(ent->dev, mdfd, &devlist, verbose, 0, NULL);
29ba4804 1676 devlist.disposition = 'r';
833bb0f8 1677 rv = Manage_subdevs(ent->dev, mdfd, &devlist, verbose, 0, NULL);
aae3cdc3 1678 close(mdfd);
08387a04 1679 free_mdstat(ent);
aae3cdc3 1680 return rv;
29ba4804 1681}