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