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