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