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