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