]> git.ipfire.org Git - thirdparty/mdadm.git/blame - Incremental.c
Two small fixes related to enough()
[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
8382f19b 199 if (st == NULL && (st = guess_super(dfd)) == 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,
11b6d91d 205 st, 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");
591 if (rv == 0) {
9ca39acb
N
592 if (c->export) {
593 printf("MD_STARTED=yes\n");
594 } else if (c->verbose >= 0)
e7b84f9d
N
595 pr_err("%s attached to %s, which has been started.\n",
596 devname, chosen_name);
8382f19b 597 rv = 0;
a7c6e3fb 598 wait_for(chosen_name, mdfd);
7e83544b
N
599 /* We just started the array, so some devices
600 * might have been evicted from the array
601 * because their event counts were too old.
602 * If the action=re-add policy is in-force for
603 * those devices we should re-add them now.
604 */
605 for (dsk = sra->devs; dsk ; dsk = dsk->next) {
606 if (disk_action_allows(dsk, st->ss->name, act_re_add) &&
607 add_disk(mdfd, st, sra, dsk) == 0)
e7b84f9d
N
608 pr_err("%s re-added to %s\n",
609 dsk->sys_name, chosen_name);
7e83544b 610 }
8382f19b 611 } else {
e7b84f9d
N
612 pr_err("%s attached to %s, but failed to start: %s.\n",
613 devname, chosen_name, strerror(errno));
8382f19b
NB
614 rv = 1;
615 }
616 } else {
9ca39acb
N
617 if (c->export) {
618 printf("MD_STARTED=unsafe\n");
619 } else if (c->verbose >= 0)
e7b84f9d
N
620 pr_err("%s attached to %s, not enough to start safely.\n",
621 devname, chosen_name);
8382f19b
NB
622 rv = 0;
623 }
15d4a7e4 624out:
de6ae750 625 free(avail);
15d4a7e4
N
626 if (dfd >= 0)
627 close(dfd);
628 if (mdfd >= 0)
629 close(mdfd);
7e83544b
N
630 if (policy)
631 dev_policy_free(policy);
2bf740b3
N
632 if (sra)
633 sysfs_free(sra);
8382f19b 634 return rv;
25824e2d
JS
635out_unlock:
636 map_unlock(&map);
637 goto out;
8382f19b
NB
638}
639
7e0f6979 640static void find_reject(int mdfd, struct supertype *st, struct mdinfo *sra,
8382f19b
NB
641 int number, __u64 events, int verbose,
642 char *array_name)
643{
3da92f27 644 /* Find a device attached to this array with a disk.number of number
8382f19b
NB
645 * and events less than the passed events, and remove the device.
646 */
06c7f68e 647 struct mdinfo *d;
8382f19b
NB
648 mdu_array_info_t ra;
649
650 if (ioctl(mdfd, GET_ARRAY_INFO, &ra) == 0)
651 return; /* not safe to remove from active arrays
652 * without thinking more */
653
654 for (d = sra->devs; d ; d = d->next) {
655 char dn[10];
656 int dfd;
8382f19b 657 struct mdinfo info;
06c7f68e 658 sprintf(dn, "%d:%d", d->disk.major, d->disk.minor);
8382f19b
NB
659 dfd = dev_open(dn, O_RDONLY);
660 if (dfd < 0)
661 continue;
3da92f27 662 if (st->ss->load_super(st, dfd, NULL)) {
8382f19b
NB
663 close(dfd);
664 continue;
665 }
a5d85af7 666 st->ss->getinfo_super(st, &info, NULL);
3da92f27 667 st->ss->free_super(st);
8382f19b
NB
668 close(dfd);
669
670 if (info.disk.number != number ||
671 info.events >= events)
672 continue;
673
06c7f68e 674 if (d->disk.raid_disk > -1)
8382f19b
NB
675 sysfs_set_str(sra, d, "slot", "none");
676 if (sysfs_set_str(sra, d, "state", "remove") == 0)
677 if (verbose >= 0)
e7b84f9d
N
678 pr_err("removing old device %s from %s\n",
679 d->sys_name+4, array_name);
8382f19b
NB
680 }
681}
682
2bf740b3
N
683static int count_active(struct supertype *st, struct mdinfo *sra,
684 int mdfd, char **availp,
8382f19b
NB
685 struct mdinfo *bestinfo)
686{
687 /* count how many devices in sra think they are active */
06c7f68e 688 struct mdinfo *d;
de6ae750 689 int cnt = 0;
72e7fb13 690 int replcnt = 0;
8382f19b 691 __u64 max_events = 0;
8382f19b 692 char *avail = NULL;
e4c72d1d 693 int *best = NULL;
de6ae750
N
694 char *devmap = NULL;
695 int numdevs = 0;
696 int devnum;
697 int b, i;
698 int raid_disks = 0;
8382f19b 699
b526e52d
DW
700 if (!sra)
701 return 0;
702
de6ae750
N
703 for (d = sra->devs ; d ; d = d->next)
704 numdevs++;
ca3b6696 705 for (d = sra->devs, devnum = 0 ; d ; d = d->next, devnum++) {
8382f19b
NB
706 char dn[30];
707 int dfd;
8382f19b
NB
708 int ok;
709 struct mdinfo info;
710
06c7f68e 711 sprintf(dn, "%d:%d", d->disk.major, d->disk.minor);
8382f19b
NB
712 dfd = dev_open(dn, O_RDONLY);
713 if (dfd < 0)
714 continue;
3da92f27 715 ok = st->ss->load_super(st, dfd, NULL);
8382f19b
NB
716 close(dfd);
717 if (ok != 0)
718 continue;
de6ae750
N
719 info.array.raid_disks = raid_disks;
720 st->ss->getinfo_super(st, &info, devmap + raid_disks * devnum);
43aaf431 721 if (!avail) {
de6ae750 722 raid_disks = info.array.raid_disks;
503975b9 723 avail = xcalloc(raid_disks, 1);
43aaf431 724 *availp = avail;
de6ae750 725
503975b9
N
726 best = xcalloc(raid_disks, sizeof(int));
727 devmap = xcalloc(raid_disks, numdevs);
de6ae750
N
728
729 st->ss->getinfo_super(st, &info, devmap);
43aaf431
DL
730 }
731
8382f19b
NB
732 if (info.disk.state & (1<<MD_DISK_SYNC))
733 {
8382f19b
NB
734 if (cnt == 0) {
735 cnt++;
736 max_events = info.events;
737 avail[info.disk.raid_disk] = 2;
de6ae750 738 best[info.disk.raid_disk] = devnum;
a5d85af7 739 st->ss->getinfo_super(st, bestinfo, NULL);
8382f19b 740 } else if (info.events == max_events) {
8382f19b 741 avail[info.disk.raid_disk] = 2;
de6ae750 742 best[info.disk.raid_disk] = devnum;
8382f19b 743 } else if (info.events == max_events-1) {
de6ae750
N
744 if (avail[info.disk.raid_disk] == 0) {
745 avail[info.disk.raid_disk] = 1;
746 best[info.disk.raid_disk] = devnum;
747 }
8382f19b
NB
748 } else if (info.events < max_events - 1)
749 ;
750 else if (info.events == max_events+1) {
751 int i;
8382f19b 752 max_events = info.events;
ca3b6696 753 for (i = 0; i < raid_disks; i++)
8382f19b
NB
754 if (avail[i])
755 avail[i]--;
756 avail[info.disk.raid_disk] = 2;
de6ae750 757 best[info.disk.raid_disk] = devnum;
a5d85af7 758 st->ss->getinfo_super(st, bestinfo, NULL);
8382f19b 759 } else { /* info.events much bigger */
47c7a4be 760 memset(avail, 0, raid_disks);
8382f19b 761 max_events = info.events;
1d1a9f87 762 avail[info.disk.raid_disk] = 2;
47c7a4be 763 best[info.disk.raid_disk] = devnum;
a5d85af7 764 st->ss->getinfo_super(st, bestinfo, NULL);
8382f19b 765 }
72e7fb13
N
766 } else if (info.disk.state & (1<<MD_DISK_REPLACEMENT))
767 replcnt++;
3da92f27 768 st->ss->free_super(st);
8382f19b 769 }
de6ae750
N
770 if (!avail)
771 return 0;
772 /* We need to reject any device that thinks the best device is
773 * failed or missing */
774 for (b = 0; b < raid_disks; b++)
775 if (avail[b] == 2)
776 break;
777 cnt = 0;
778 for (i = 0 ; i < raid_disks ; i++) {
779 if (i != b && avail[i])
780 if (devmap[raid_disks * best[i] + b] == 0) {
781 /* This device thinks 'b' is failed -
782 * don't use it */
783 devnum = best[i];
784 for (d=sra->devs ; devnum; d = d->next)
785 devnum--;
786 d->disk.state |= (1 << MD_DISK_REMOVED);
787 avail[i] = 0;
788 }
789 if (avail[i])
790 cnt++;
791 }
f80057ae
N
792 /* Also need to reject any spare device with an event count that
793 * is too high
794 */
795 for (d = sra->devs; d; d = d->next) {
796 if (!(d->disk.state & (1<<MD_DISK_SYNC)) &&
797 d->events > max_events)
798 d->disk.state |= (1 << MD_DISK_REMOVED);
799 }
de6ae750
N
800 free(best);
801 free(devmap);
72e7fb13 802 return cnt + replcnt;
8382f19b
NB
803}
804
5be68a07
PC
805/* test if container has degraded member(s) */
806static int container_members_max_degradation(struct map_ent *map, struct map_ent *me)
807{
808 mdu_array_info_t array;
809 int afd;
810 int max_degraded = 0;
811
812 for(; map; map = map->next) {
4dd2df09 813 if (!metadata_container_matches(map->metadata, me->devnm))
5be68a07 814 continue;
4dd2df09 815 afd = open_dev(map->devnm);
5be68a07
PC
816 if (afd < 0)
817 continue;
818 /* most accurate information regarding array degradation */
819 if (ioctl(afd, GET_ARRAY_INFO, &array) >= 0) {
820 int degraded = array.raid_disks - array.active_disks -
821 array.spare_disks;
822 if (degraded > max_degraded)
823 max_degraded = degraded;
824 }
825 close(afd);
826 }
827 return (max_degraded);
828}
829
56e8be85 830static int array_try_spare(char *devname, int *dfdp, struct dev_policy *pol,
d2db3045 831 struct map_ent *target, int bare,
56e8be85 832 struct supertype *st, int verbose)
f08605b3
N
833{
834 /* This device doesn't have any md metadata
d2db3045
N
835 * The device policy allows 'spare' and if !bare, it allows spare-same-slot.
836 * If 'st' is not set, then we only know that some metadata allows this,
837 * others possibly don't.
838 * So look for a container or array to attach the device to.
839 * Prefer 'target' if that is set and the array is found.
840 *
f08605b3
N
841 * If st is set, then only arrays of that type are considered
842 * Return 0 on success, or some exit code on failure, probably 1.
843 */
8659d089 844 int rv = 1;
f08605b3
N
845 struct stat stb;
846 struct map_ent *mp, *map = NULL;
847 struct mdinfo *chosen = NULL;
848 int dfd = *dfdp;
849
f08605b3
N
850 if (fstat(dfd, &stb) != 0)
851 return 1;
f08605b3 852
56e8be85 853 /*
f08605b3
N
854 * Now we need to find a suitable array to add this to.
855 * We only accept arrays that:
856 * - match 'st'
857 * - are in the same domains as the device
858 * - are of an size for which the device will be useful
859 * and we choose the one that is the most degraded
860 */
861
862 if (map_lock(&map)) {
e7b84f9d 863 pr_err("failed to get exclusive lock on "
f08605b3
N
864 "mapfile\n");
865 return 1;
866 }
867 for (mp = map ; mp ; mp = mp->next) {
868 struct supertype *st2;
869 struct domainlist *dl = NULL;
870 struct mdinfo *sra;
871 unsigned long long devsize;
71204a50 872 unsigned long long component_size = 0;
f08605b3
N
873
874 if (is_subarray(mp->metadata))
875 continue;
876 if (st) {
877 st2 = st->ss->match_metadata_desc(mp->metadata);
878 if (!st2 ||
879 (st->minor_version >= 0 &&
880 st->minor_version != st2->minor_version)) {
881 if (verbose > 1)
e7b84f9d 882 pr_err("not adding %s to %s as metadata type doesn't match\n",
f08605b3
N
883 devname, mp->path);
884 free(st2);
885 continue;
886 }
887 free(st2);
888 }
4dd2df09 889 sra = sysfs_read(-1, mp->devnm,
f08605b3
N
890 GET_DEVS|GET_OFFSET|GET_SIZE|GET_STATE|
891 GET_DEGRADED|GET_COMPONENT|GET_VERSION);
892 if (!sra) {
893 /* Probably a container - no degraded info */
4dd2df09 894 sra = sysfs_read(-1, mp->devnm,
f08605b3
N
895 GET_DEVS|GET_OFFSET|GET_SIZE|GET_STATE|
896 GET_COMPONENT|GET_VERSION);
897 if (sra)
5be68a07 898 sra->array.failed_disks = -1;
f08605b3
N
899 }
900 if (!sra)
901 continue;
902 if (st == NULL) {
903 int i;
904 st2 = NULL;
ca3b6696 905 for(i = 0; !st2 && superlist[i]; i++)
f08605b3
N
906 st2 = superlist[i]->match_metadata_desc(
907 sra->text_version);
6e57f80a
N
908 if (!st2) {
909 if (verbose > 1)
e7b84f9d 910 pr_err("not adding %s to %s"
6e57f80a
N
911 " as metadata not recognised.\n",
912 devname, mp->path);
913 goto next;
914 }
625b2507
N
915 /* Need to double check the 'act_spare' permissions applies
916 * to this metadata.
917 */
918 if (!policy_action_allows(pol, st2->ss->name, act_spare))
919 goto next;
d2db3045
N
920 if (!bare && !policy_action_allows(pol, st2->ss->name,
921 act_spare_same_slot))
922 goto next;
f08605b3
N
923 } else
924 st2 = st;
5be68a07
PC
925 /* update number of failed disks for mostly degraded
926 * container member */
927 if (sra->array.failed_disks == -1)
928 sra->array.failed_disks = container_members_max_degradation(map, mp);
929
f08605b3 930 get_dev_size(dfd, NULL, &devsize);
48865704
PC
931 if (sra->component_size == 0) {
932 /* true for containers, here we must read superblock
933 * to obtain minimum spare size */
934 struct supertype *st3 = dup_super(st2);
4dd2df09 935 int mdfd = open_dev(mp->devnm);
f56128b9 936 if (mdfd < 0) {
1fdeb8a0 937 free(st3);
48865704 938 goto next;
1fdeb8a0 939 }
48865704
PC
940 if (st3->ss->load_container &&
941 !st3->ss->load_container(st3, mdfd, mp->path)) {
942 component_size = st3->ss->min_acceptable_spare_size(st3);
943 st3->ss->free_super(st3);
944 }
945 free(st3);
946 close(mdfd);
947 }
948 if ((sra->component_size > 0 &&
387fcd59
N
949 st2->ss->avail_size(st2, devsize,
950 sra->devs
951 ? sra->devs->data_offset
952 : INVALID_SECTORS)
953 < sra->component_size)
48865704
PC
954 ||
955 (sra->component_size == 0 && devsize < component_size)) {
f08605b3 956 if (verbose > 1)
e7b84f9d 957 pr_err("not adding %s to %s as it is too small\n",
f08605b3
N
958 devname, mp->path);
959 goto next;
960 }
d2db3045
N
961 /* test against target.
962 * If 'target' is set and 'bare' is false, we only accept
963 * arrays/containers that match 'target'.
964 * If 'target' is set and 'bare' is true, we prefer the
965 * array which matches 'target'.
b4924220 966 * target is considered only if we deal with degraded array
d2db3045 967 */
fee6a49e
PC
968 if (target && policy_action_allows(pol, st2->ss->name,
969 act_spare_same_slot)) {
d2db3045
N
970 if (strcmp(target->metadata, mp->metadata) == 0 &&
971 memcmp(target->uuid, mp->uuid,
b4924220
PC
972 sizeof(target->uuid)) == 0 &&
973 sra->array.failed_disks > 0) {
d2db3045
N
974 /* This is our target!! */
975 if (chosen)
976 sysfs_free(chosen);
977 chosen = sra;
978 sra = NULL;
979 /* skip to end so we don't check any more */
980 while (mp->next)
981 mp = mp->next;
982 goto next;
983 }
984 /* not our target */
985 if (!bare)
986 goto next;
987 }
988
b6b3f0f7 989 dl = domain_from_array(sra, st2->ss->name);
e5508b36 990 if (domain_test(dl, pol, st2->ss->name) != 1) {
b6b3f0f7
PC
991 /* domain test fails */
992 if (verbose > 1)
e7b84f9d 993 pr_err("not adding %s to %s as"
b6b3f0f7
PC
994 " it is not in a compatible domain\n",
995 devname, mp->path);
996
997 goto next;
998 }
f08605b3
N
999 /* all tests passed, OK to add to this array */
1000 if (!chosen) {
1001 chosen = sra;
1002 sra = NULL;
1003 } else if (chosen->array.failed_disks < sra->array.failed_disks) {
1004 sysfs_free(chosen);
1005 chosen = sra;
1006 sra = NULL;
1007 }
1008 next:
1009 if (sra)
1010 sysfs_free(sra);
1011 if (st != st2)
1012 free(st2);
1013 if (dl)
1014 domain_free(dl);
1015 }
1016 if (chosen) {
1017 /* add current device to chosen array as a spare */
4dd2df09 1018 int mdfd = open_dev(chosen->sys_name);
f08605b3 1019 if (mdfd >= 0) {
a655e550 1020 struct mddev_dev devlist;
f08605b3
N
1021 char devname[20];
1022 devlist.next = NULL;
1023 devlist.used = 0;
f08605b3
N
1024 devlist.writemostly = 0;
1025 devlist.devname = devname;
1026 sprintf(devname, "%d:%d", major(stb.st_rdev),
1027 minor(stb.st_rdev));
1028 devlist.disposition = 'a';
1029 close(dfd);
1030 *dfdp = -1;
1031 rv = Manage_subdevs(chosen->sys_name, mdfd, &devlist,
11b391ec 1032 -1, 0, NULL, 0);
f08605b3
N
1033 close(mdfd);
1034 }
1035 if (verbose > 0) {
1036 if (rv == 0)
e7b84f9d 1037 pr_err("added %s as spare for %s\n",
f08605b3
N
1038 devname, chosen->sys_name);
1039 else
e7b84f9d 1040 pr_err("failed to add %s as spare for %s\n",
f08605b3
N
1041 devname, chosen->sys_name);
1042 }
1043 sysfs_free(chosen);
1044 }
015da8f5 1045 map_unlock(&map);
8659d089 1046 return rv;
f08605b3
N
1047}
1048
56e8be85
N
1049static int partition_try_spare(char *devname, int *dfdp, struct dev_policy *pol,
1050 struct supertype *st, int verbose)
1051{
61018da0
N
1052 /* we know that at least one partition virtual-metadata is
1053 * allowed to incorporate spares like this device. We need to
1054 * find a suitable device to copy partition information from.
1055 *
1056 * Getting a list of all disk (not partition) devices is
1057 * slightly non-trivial. We could look at /sys/block, but
1058 * that is theoretically due to be removed. Maybe best to use
1059 * /dev/disk/by-path/?* and ignore names ending '-partNN' as
1060 * we depend on this directory of 'path' info. But that fails
1061 * to find loop devices and probably others. Maybe don't
1062 * worry about that, they aren't the real target.
1063 *
1064 * So: check things in /dev/disk/by-path to see if they are in
1065 * a compatible domain, then load the partition table and see
1066 * if it is OK for the new device, and choose the largest
1067 * partition table that fits.
1068 */
1069 DIR *dir;
1070 struct dirent *de;
1071 char *chosen = NULL;
71204a50 1072 unsigned long long chosen_size = 0;
61018da0
N
1073 struct supertype *chosen_st = NULL;
1074 int fd;
1075
1076 dir = opendir("/dev/disk/by-path");
1077 if (!dir)
1078 return 1;
1079 while ((de = readdir(dir)) != NULL) {
1080 char *ep;
1081 struct dev_policy *pol2 = NULL;
1082 struct domainlist *domlist = NULL;
1083 int fd = -1;
1084 struct mdinfo info;
1085 struct supertype *st2 = NULL;
1086 char *devname = NULL;
1087 unsigned long long devsectors;
1088
1089 if (de->d_ino == 0 ||
1090 de->d_name[0] == '.' ||
1091 (de->d_type != DT_LNK && de->d_type != DT_UNKNOWN))
1092 goto next;
1093
1094 ep = de->d_name + strlen(de->d_name);
1095 while (ep > de->d_name &&
1096 isdigit(ep[-1]))
1097 ep--;
1098 if (ep > de->d_name + 5 &&
1099 strncmp(ep-5, "-part", 5) == 0)
1100 /* This is a partition - skip it */
1101 goto next;
1102
1103 pol2 = path_policy(de->d_name, type_disk);
1104
1105 domain_merge(&domlist, pol2, st ? st->ss->name : NULL);
e5508b36 1106 if (domain_test(domlist, pol, st ? st->ss->name : NULL) != 1)
61018da0
N
1107 /* new device is incompatible with this device. */
1108 goto next;
1109
1110 domain_free(domlist);
1111 domlist = NULL;
1112
71204a50
N
1113 if (asprintf(&devname, "/dev/disk/by-path/%s", de->d_name) != 1) {
1114 devname = NULL;
1115 goto next;
1116 }
61018da0
N
1117 fd = open(devname, O_RDONLY);
1118 if (fd < 0)
1119 goto next;
1120 if (get_dev_size(fd, devname, &devsectors) == 0)
1121 goto next;
1122 devsectors >>= 9;
1123
1124 if (st)
1125 st2 = dup_super(st);
1126 else
1127 st2 = guess_super_type(fd, guess_partitions);
1128 if (st2 == NULL ||
1129 st2->ss->load_super(st2, fd, NULL) < 0)
1130 goto next;
1131
1132 if (!st) {
1133 /* Check domain policy again, this time referring to metadata */
1134 domain_merge(&domlist, pol2, st2->ss->name);
e5508b36 1135 if (domain_test(domlist, pol, st2->ss->name) != 1)
61018da0
N
1136 /* Incompatible devices for this metadata type */
1137 goto next;
625b2507
N
1138 if (!policy_action_allows(pol, st2->ss->name, act_spare))
1139 /* Some partition types allow sparing, but not
1140 * this one.
1141 */
1142 goto next;
61018da0
N
1143 }
1144
a5d85af7 1145 st2->ss->getinfo_super(st2, &info, NULL);
61018da0
N
1146 if (info.component_size > devsectors)
1147 /* This partitioning doesn't fit in the device */
1148 goto next;
1149
1150 /* This is an acceptable device to copy partition
1151 * metadata from. We could just stop here, but I
1152 * think I want to keep looking incase a larger
1153 * metadata which makes better use of the device can
1154 * be found.
1155 */
1156 if (chosen == NULL ||
1157 chosen_size < info.component_size) {
1158 chosen_size = info.component_size;
1159 free(chosen);
1160 chosen = devname;
1161 devname = NULL;
1162 if (chosen_st) {
1163 chosen_st->ss->free_super(chosen_st);
1164 free(chosen_st);
1165 }
1166 chosen_st = st2;
1167 st2 = NULL;
1168 }
1169
1170 next:
1171 free(devname);
1172 domain_free(domlist);
1173 dev_policy_free(pol2);
1174 if (st2)
1175 st2->ss->free_super(st2);
1176 free(st2);
1177
1178 if (fd >= 0)
1179 close(fd);
1180 }
1181
be5c60e3
JS
1182 closedir(dir);
1183
61018da0
N
1184 if (!chosen)
1185 return 1;
1186
1187 /* 'chosen' is the best device we can find. Let's write its
1188 * metadata to devname dfd is read-only so don't use that
1189 */
1190 fd = open(devname, O_RDWR);
1191 if (fd >= 0) {
1192 chosen_st->ss->store_super(chosen_st, fd);
1193 close(fd);
1194 }
1195 free(chosen);
1196 chosen_st->ss->free_super(chosen_st);
1197 free(chosen_st);
1198 return 0;
56e8be85
N
1199}
1200
52e965c2
N
1201static int is_bare(int dfd)
1202{
1203 unsigned long long size = 0;
1204 char bufpad[4096 + 4096];
1205 char *buf = (char*)(((long)bufpad + 4096) & ~4095);
1206
1207 if (lseek(dfd, 0, SEEK_SET) != 0 ||
1208 read(dfd, buf, 4096) != 4096)
1209 return 0;
1210
1211 if (buf[0] != '\0' && buf[0] != '\x5a' && buf[0] != '\xff')
1212 return 0;
1213 if (memcmp(buf, buf+1, 4095) != 0)
1214 return 0;
1215
1216 /* OK, first 4K appear blank, try the end. */
1217 get_dev_size(dfd, NULL, &size);
1218 if (lseek(dfd, size-4096, SEEK_SET) < 0 ||
1219 read(dfd, buf, 4096) != 4096)
1220 return 0;
1221
1222 if (buf[0] != '\0' && buf[0] != '\x5a' && buf[0] != '\xff')
1223 return 0;
1224 if (memcmp(buf, buf+1, 4095) != 0)
1225 return 0;
1226
1227 return 1;
1228}
56e8be85
N
1229
1230/* adding a spare to a regular array is quite different from adding one to
1231 * a set-of-partitions virtual array.
1232 * This function determines which is worth trying and tries as appropriate.
1233 * Arrays are given priority over partitions.
1234 */
1235static int try_spare(char *devname, int *dfdp, struct dev_policy *pol,
d2db3045 1236 struct map_ent *target,
56e8be85
N
1237 struct supertype *st, int verbose)
1238{
1239 int i;
1240 int rv;
1241 int arrays_ok = 0;
1242 int partitions_ok = 0;
56e8be85 1243 int dfd = *dfdp;
d2db3045 1244 int bare;
56e8be85 1245
d2db3045 1246 /* Can only add a spare if device has at least one domain */
56e8be85
N
1247 if (pol_find(pol, pol_domain) == NULL)
1248 return 1;
1249 /* And only if some action allows spares */
1250 if (!policy_action_allows(pol, st?st->ss->name:NULL, act_spare))
1251 return 1;
1252
d2db3045
N
1253 /* Now check if the device is bare.
1254 * bare devices can always be added as a spare
1255 * non-bare devices can only be added if spare-same-slot is permitted,
1256 * and this device is replacing a previous device - in which case 'target'
1257 * will be set.
56e8be85 1258 */
52e965c2 1259 if (!is_bare(dfd)) {
d2db3045
N
1260 /* Must have a target and allow same_slot */
1261 /* Later - may allow force_spare without target */
1262 if (!target ||
1263 !policy_action_allows(pol, st?st->ss->name:NULL,
1264 act_spare_same_slot)) {
1265 if (verbose > 1)
e7b84f9d 1266 pr_err("%s is not bare, so not "
d2db3045
N
1267 "considering as a spare\n",
1268 devname);
1269 return 1;
1270 }
1271 bare = 0;
1272 } else
1273 bare = 1;
56e8be85 1274
d2db3045
N
1275 /* It might be OK to add this device to an array - need to see
1276 * what arrays might be candidates.
56e8be85
N
1277 */
1278 if (st) {
1279 /* just try try 'array' or 'partition' based on this metadata */
1280 if (st->ss->add_to_super)
d2db3045 1281 return array_try_spare(devname, dfdp, pol, target, bare,
56e8be85
N
1282 st, verbose);
1283 else
1284 return partition_try_spare(devname, dfdp, pol,
1285 st, verbose);
1286 }
d2db3045
N
1287 /* No metadata was specified or found so options are open.
1288 * Check for whether any array metadata, or any partition metadata
1289 * might allow adding the spare. This check is just help to avoid
1290 * a more costly scan of all arrays when we can be sure that will
1291 * fail.
1292 */
56e8be85
N
1293 for (i = 0; (!arrays_ok || !partitions_ok) && superlist[i] ; i++) {
1294 if (superlist[i]->add_to_super && !arrays_ok &&
1295 policy_action_allows(pol, superlist[i]->name, act_spare))
1296 arrays_ok = 1;
1297 if (superlist[i]->add_to_super == NULL && !partitions_ok &&
1298 policy_action_allows(pol, superlist[i]->name, act_spare))
1299 partitions_ok = 1;
1300 }
aaccda44 1301 rv = 1;
56e8be85 1302 if (arrays_ok)
d2db3045
N
1303 rv = array_try_spare(devname, dfdp, pol, target, bare,
1304 st, verbose);
aaccda44 1305 if (rv != 0 && partitions_ok)
56e8be85
N
1306 rv = partition_try_spare(devname, dfdp, pol, st, verbose);
1307 return rv;
1308}
1309
d5a40416 1310int IncrementalScan(struct context *c, char *devnm)
8382f19b
NB
1311{
1312 /* look at every device listed in the 'map' file.
1313 * If one is found that is not running then:
1314 * look in mdadm.conf for bitmap file.
1315 * if one exists, but array has none, add it.
1316 * try to start array in auto-readonly mode
1317 */
1318 struct map_ent *mapl = NULL;
1319 struct map_ent *me;
fa56eddb 1320 struct mddev_ident *devs, *mddev;
8382f19b 1321 int rv = 0;
d5a40416
N
1322 char container[32];
1323 char *only = NULL;
8382f19b
NB
1324
1325 map_read(&mapl);
1326 devs = conf_get_ident(NULL);
1327
d5a40416 1328restart:
8382f19b 1329 for (me = mapl ; me ; me = me->next) {
8382f19b
NB
1330 mdu_array_info_t array;
1331 mdu_bitmap_file_t bmf;
7e0f6979 1332 struct mdinfo *sra;
d3786cdc
N
1333 int mdfd;
1334
1335 if (devnm && strcmp(devnm, me->devnm) != 0)
1336 continue;
d5a40416
N
1337 if (devnm && me->metadata[0] == '/') {
1338 char *sl;
1339 /* member array, need to work on container */
1340 strncpy(container, me->metadata+1, 32);
1341 container[31] = 0;
1342 sl = strchr(container, '/');
1343 if (sl)
1344 *sl = 0;
1345 only = devnm;
1346 devnm = container;
1347 goto restart;
1348 }
d3786cdc 1349 mdfd = open_dev(me->devnm);
215bb3f7 1350
8382f19b
NB
1351 if (mdfd < 0)
1352 continue;
d5a40416
N
1353 if (!isdigit(me->metadata[0])) {
1354 /* must be a container */
1355 struct supertype *st = super_by_fd(mdfd, NULL);
1356 int ret = 0;
1357 struct map_ent *map = NULL;
1358 if (st)
1359 st->ignore_hw_compat = 1;
1360 if (st && st->ss->load_container)
1361 ret = st->ss->load_container(st, mdfd, NULL);
1362 close(mdfd);
1363 if (!ret && st->ss->container_content) {
1364 if (map_lock(&map))
1365 pr_err("failed to get exclusive lock on mapfile\n");
1366 ret = Incremental_container(st, me->path, c, only);
1367 map_unlock(&map);
1368 }
1369 if (ret)
1370 rv = 1;
1371 continue;
1372 }
8382f19b
NB
1373 if (ioctl(mdfd, GET_ARRAY_INFO, &array) == 0 ||
1374 errno != ENODEV) {
1375 close(mdfd);
1376 continue;
1377 }
1378 /* Ok, we can try this one. Maybe it needs a bitmap */
1379 for (mddev = devs ; mddev ; mddev = mddev->next)
339c2d6c 1380 if (mddev->devname && me->path
2400e6eb 1381 && devname_matches(mddev->devname, me->path))
8382f19b
NB
1382 break;
1383 if (mddev && mddev->bitmap_file) {
1384 /*
1385 * Note: early kernels will wrongly fail this, so it
1386 * is a hint only
1387 */
1388 int added = -1;
1389 if (ioctl(mdfd, GET_ARRAY_INFO, &bmf) < 0) {
1390 int bmfd = open(mddev->bitmap_file, O_RDWR);
1391 if (bmfd >= 0) {
1392 added = ioctl(mdfd, SET_BITMAP_FILE,
1393 bmfd);
1394 close(bmfd);
1395 }
1396 }
d5a40416 1397 if (c->verbose >= 0) {
8382f19b 1398 if (added == 0)
e7b84f9d
N
1399 pr_err("Added bitmap %s to %s\n",
1400 mddev->bitmap_file, me->path);
8382f19b 1401 else if (errno != EEXIST)
e7b84f9d
N
1402 pr_err("Failed to add bitmap to %s: %s\n",
1403 me->path, strerror(errno));
8382f19b
NB
1404 }
1405 }
5e88ab2e
N
1406 /* FIXME check for reshape_active and consider not
1407 * starting array.
1408 */
4dd2df09 1409 sra = sysfs_read(mdfd, NULL, 0);
8382f19b
NB
1410 if (sra) {
1411 if (sysfs_set_str(sra, NULL,
1412 "array_state", "read-auto") == 0) {
d5a40416 1413 if (c->verbose >= 0)
e7b84f9d 1414 pr_err("started array %s\n",
4dd2df09 1415 me->path ?: me->devnm);
8382f19b 1416 } else {
e7b84f9d 1417 pr_err("failed to start array %s: %s\n",
4dd2df09 1418 me->path ?: me->devnm,
e7b84f9d 1419 strerror(errno));
8382f19b
NB
1420 rv = 1;
1421 }
b1efa6c2 1422 sysfs_free(sra);
8382f19b
NB
1423 }
1424 }
1425 return rv;
1426}
598f0d58 1427
1771a6e2
N
1428static char *container2devname(char *devname)
1429{
1430 char *mdname = NULL;
1431
1432 if (devname[0] == '/') {
1433 int fd = open(devname, O_RDONLY);
1434 if (fd >= 0) {
4dd2df09 1435 mdname = xstrdup(fd2devnm(fd));
1771a6e2
N
1436 close(fd);
1437 }
1438 } else {
1439 int uuid[4];
1440 struct map_ent *mp, *map = NULL;
ca3b6696 1441
1771a6e2
N
1442 if (!parse_uuid(devname, uuid))
1443 return mdname;
1444 mp = map_by_uuid(&map, uuid);
1445 if (mp)
4dd2df09 1446 mdname = xstrdup(mp->devnm);
1771a6e2
N
1447 map_free(map);
1448 }
1449
1450 return mdname;
1451}
1452
47c74f3f 1453static int Incremental_container(struct supertype *st, char *devname,
d5a40416 1454 struct context *c, char *only)
598f0d58
NB
1455{
1456 /* Collect the contents of this container and for each
1457 * array, choose a device name and assemble the array.
1458 */
1459
47c74f3f 1460 struct mdinfo *list;
598f0d58 1461 struct mdinfo *ra;
ad5bc697 1462 struct map_ent *map = NULL;
47c74f3f
N
1463 struct mdinfo info;
1464 int trustworthy;
fa56eddb 1465 struct mddev_ident *match;
47c74f3f 1466 int rv = 0;
037e12d6
AC
1467 struct domainlist *domains;
1468 struct map_ent *smp;
1469 int suuid[4];
1470 int sfd;
81219e70
LM
1471 int ra_blocked = 0;
1472 int ra_all = 0;
9ca39acb 1473 int result = 0;
47c74f3f 1474
47c74f3f
N
1475 st->ss->getinfo_super(st, &info, NULL);
1476
11b6d91d 1477 if ((c->runstop > 0 && info.container_enough >= 0) ||
47c74f3f
N
1478 info.container_enough > 0)
1479 /* pass */;
1480 else {
9ca39acb
N
1481 if (c->export) {
1482 printf("MD_STARTED=no\n");
1483 } else if (c->verbose)
e7b84f9d 1484 pr_err("not enough devices to start the container\n");
47c74f3f
N
1485 return 0;
1486 }
1487
11b6d91d 1488 match = conf_match(st, &info, devname, c->verbose, &rv);
47c74f3f
N
1489 if (match == NULL && rv == 2)
1490 return rv;
1491
1492 /* Need to compute 'trustworthy' */
1493 if (match)
1494 trustworthy = LOCAL;
11b6d91d 1495 else if (st->ss->match_home(st, c->homehost) == 1)
47c74f3f
N
1496 trustworthy = LOCAL;
1497 else if (st->ss->match_home(st, "any") == 1)
1498 trustworthy = LOCAL;
1499 else
1500 trustworthy = FOREIGN;
ad5bc697 1501
47c74f3f 1502 list = st->ss->container_content(st, NULL);
81219e70 1503 /* when nothing to activate - quit */
9ca39acb
N
1504 if (list == NULL) {
1505 if (c->export) {
1506 printf("MD_STARTED=nothing\n");
1507 }
81219e70 1508 return 0;
9ca39acb 1509 }
598f0d58 1510 for (ra = list ; ra ; ra = ra->next) {
598f0d58
NB
1511 int mdfd;
1512 char chosen_name[1024];
ad5bc697 1513 struct map_ent *mp;
fa56eddb 1514 struct mddev_ident *match = NULL;
598f0d58 1515
81219e70
LM
1516 ra_all++;
1517 /* do not activate arrays blocked by metadata handler */
1518 if (ra->array.state & (1 << MD_SB_BLOCK_VOLUME)) {
e7b84f9d 1519 pr_err("Cannot activate array %s in %s.\n",
81219e70
LM
1520 ra->text_version, devname);
1521 ra_blocked++;
1522 continue;
1523 }
c5afc314 1524 mp = map_by_uuid(&map, ra->uuid);
d7288ddc 1525
30926600 1526 if (mp) {
4dd2df09 1527 mdfd = open_dev(mp->devnm);
339c2d6c
N
1528 if (mp->path)
1529 strcpy(chosen_name, mp->path);
1530 else
4dd2df09 1531 strcpy(chosen_name, mp->devnm);
d5a40416 1532 } else if (!only) {
dbb44303 1533
112cace6 1534 /* Check in mdadm.conf for container == devname and
dbb44303
N
1535 * member == ra->text_version after second slash.
1536 */
1537 char *sub = strchr(ra->text_version+1, '/');
fa56eddb 1538 struct mddev_ident *array_list;
dbb44303
N
1539 if (sub) {
1540 sub++;
1541 array_list = conf_get_ident(NULL);
1542 } else
1543 array_list = NULL;
1544 for(; array_list ; array_list = array_list->next) {
dbb44303
N
1545 char *dn;
1546 if (array_list->member == NULL ||
1547 array_list->container == NULL)
1548 continue;
1549 if (strcmp(array_list->member, sub) != 0)
1550 continue;
71d60c48
DW
1551 if (array_list->uuid_set &&
1552 !same_uuid(ra->uuid, array_list->uuid, st->ss->swapuuid))
1553 continue;
1771a6e2
N
1554 dn = container2devname(array_list->container);
1555 if (dn == NULL)
dbb44303 1556 continue;
dbb44303
N
1557 if (strncmp(dn, ra->text_version+1,
1558 strlen(dn)) != 0 ||
1559 ra->text_version[strlen(dn)+1] != '/') {
1560 free(dn);
1561 continue;
1562 }
1563 free(dn);
1564 /* we have a match */
1565 match = array_list;
11b6d91d 1566 if (c->verbose>0)
e7b84f9d 1567 pr_err("match found for member %s\n",
71d60c48 1568 array_list->member);
dbb44303
N
1569 break;
1570 }
dbb44303 1571
112cace6
N
1572 if (match && match->devname &&
1573 strcasecmp(match->devname, "<ignore>") == 0) {
11b6d91d 1574 if (c->verbose > 0)
e7b84f9d
N
1575 pr_err("array %s/%s is "
1576 "explicitly ignored by mdadm.conf\n",
1577 match->container, match->member);
eb8b9516 1578 continue;
112cace6 1579 }
05833051
N
1580 if (match)
1581 trustworthy = LOCAL;
112cace6 1582
30926600
N
1583 mdfd = create_mddev(match ? match->devname : NULL,
1584 ra->name,
11b6d91d 1585 c->autof,
30926600
N
1586 trustworthy,
1587 chosen_name);
1588 }
d5a40416
N
1589 if (only && (!mp || strcmp(mp->devnm, only) != 0))
1590 continue;
598f0d58
NB
1591
1592 if (mdfd < 0) {
e7b84f9d 1593 pr_err("failed to open %s: %s.\n",
598f0d58
NB
1594 chosen_name, strerror(errno));
1595 return 2;
1596 }
1597
11b6d91d 1598 assemble_container_content(st, mdfd, ra, c,
9ca39acb 1599 chosen_name, &result);
588bebfc 1600 close(mdfd);
598f0d58 1601 }
9ca39acb
N
1602 if (c->export && result) {
1603 char sep = '=';
1604 printf("MD_STARTED");
1605 if (result & INCR_NO) {
1606 printf("%cno", sep);
1607 sep = ',';
1608 }
1609 if (result & INCR_UNSAFE) {
1610 printf("%cunsafe", sep);
1611 sep = ',';
1612 }
1613 if (result & INCR_ALREADY) {
1614 printf("%calready", sep);
1615 sep = ',';
1616 }
1617 if (result & INCR_YES) {
1618 printf("%cyes", sep);
1619 sep = ',';
1620 }
1621 printf("\n");
1622 }
037e12d6 1623
81219e70
LM
1624 /* don't move spares to container with volume being activated
1625 when all volumes are blocked */
ad098cdd 1626 if (ra_all == ra_blocked)
81219e70 1627 return 0;
81219e70 1628
037e12d6
AC
1629 /* Now move all suitable spares from spare container */
1630 domains = domain_from_array(list, st->ss->name);
1631 memcpy(suuid, uuid_zero, sizeof(int[4]));
1632 if (domains &&
1633 (smp = map_by_uuid(&map, suuid)) != NULL &&
1634 (sfd = open(smp->path, O_RDONLY)) >= 0) {
1635 /* spare container found */
1636 struct supertype *sst =
1637 super_imsm.match_metadata_desc("imsm");
1638 struct mdinfo *sinfo;
1639 unsigned long long min_size = 0;
1640 if (st->ss->min_acceptable_spare_size)
1641 min_size = st->ss->min_acceptable_spare_size(st);
1642 if (!sst->ss->load_container(sst, sfd, NULL)) {
1643 close(sfd);
1644 sinfo = container_choose_spares(sst, min_size,
1645 domains, NULL,
1646 st->ss->name, 0);
1647 sst->ss->free_super(sst);
1648 if (sinfo){
1649 int count = 0;
1650 struct mdinfo *disks = sinfo->devs;
1651 while (disks) {
1652 /* move spare from spare
1653 * container to currently
1654 * assembled one
1655 */
1656 if (move_spare(
1657 smp->path,
1658 devname,
1659 makedev(disks->disk.major,
1660 disks->disk.minor)))
1661 count++;
1662 disks = disks->next;
1663 }
1664 if (count)
e7b84f9d
N
1665 pr_err("Added %d spare%s to %s\n",
1666 count, count>1?"s":"", devname);
037e12d6
AC
1667 }
1668 sysfs_free(sinfo);
1669 } else
1670 close(sfd);
1671 }
1672 domain_free(domains);
598f0d58
NB
1673 return 0;
1674}
29ba4804 1675
6b63c1a4
N
1676static void run_udisks(char *arg1, char *arg2)
1677{
1678 int pid = fork();
1679 int status;
1680 if (pid == 0) {
1681 execl("/usr/bin/udisks", "udisks", arg1, arg2, NULL);
1682 execl("/bin/udisks", "udisks", arg1, arg2, NULL);
1683 exit(1);
1684 }
1685 while (pid > 0 && wait(&status) != pid)
1686 ;
1687}
1688
29ba4804
N
1689/*
1690 * IncrementalRemove - Attempt to see if the passed in device belongs to any
1691 * raid arrays, and if so first fail (if needed) and then remove the device.
1692 *
1693 * @devname - The device we want to remove
950bc344 1694 * @id_path - name as found in /dev/disk/by-path for this device
29ba4804
N
1695 *
1696 * Note: the device name must be a kernel name like "sda", so
1697 * that we can find it in /proc/mdstat
1698 */
950bc344 1699int IncrementalRemove(char *devname, char *id_path, int verbose)
29ba4804
N
1700{
1701 int mdfd;
8af530b0 1702 int rv = 0;
29ba4804 1703 struct mdstat_ent *ent;
a655e550 1704 struct mddev_dev devlist;
8af530b0
N
1705 struct mdinfo mdi;
1706 char buf[32];
29ba4804 1707
950bc344
PC
1708 if (!id_path)
1709 dprintf(Name ": incremental removal without --path <id_path> "
1710 "lacks the possibility to re-add new device in this "
1711 "port\n");
1712
29ba4804 1713 if (strchr(devname, '/')) {
e7b84f9d 1714 pr_err("incremental removal requires a "
29ba4804
N
1715 "kernel device name, not a file: %s\n", devname);
1716 return 1;
1717 }
1718 ent = mdstat_by_component(devname);
1719 if (!ent) {
041b815f
N
1720 if (verbose >= 0)
1721 pr_err("%s does not appear to be a component "
1722 "of any array\n", devname);
29ba4804
N
1723 return 1;
1724 }
8af530b0
N
1725 sysfs_init(&mdi, -1, ent->devnm);
1726 if (sysfs_get_str(&mdi, NULL, "array_state",
1727 buf, sizeof(buf)) > 0) {
1728 if (strncmp(buf, "active", 6) == 0 ||
1729 strncmp(buf, "clean", 5) == 0)
1730 sysfs_set_str(&mdi, NULL,
1731 "array_state", "read-auto");
1732 }
4dd2df09 1733 mdfd = open_dev(ent->devnm);
29ba4804 1734 if (mdfd < 0) {
041b815f
N
1735 if (verbose >= 0)
1736 pr_err("Cannot open array %s!!\n", ent->dev);
08387a04 1737 free_mdstat(ent);
29ba4804
N
1738 return 1;
1739 }
403410eb
PC
1740
1741 if (id_path) {
1742 struct map_ent *map = NULL, *me;
4dd2df09 1743 me = map_by_devnm(&map, ent->devnm);
403410eb
PC
1744 if (me)
1745 policy_save_path(id_path, me);
1746 map_free(map);
1747 }
1748
29ba4804
N
1749 memset(&devlist, 0, sizeof(devlist));
1750 devlist.devname = devname;
1751 devlist.disposition = 'f';
08387a04
N
1752 /* for a container, we must fail each member array */
1753 if (ent->metadata_version &&
1754 strncmp(ent->metadata_version, "external:", 9) == 0) {
1755 struct mdstat_ent *mdstat = mdstat_read(0, 0);
1756 struct mdstat_ent *memb;
1757 for (memb = mdstat ; memb ; memb = memb->next)
1758 if (is_container_member(memb, ent->dev)) {
4dd2df09 1759 int subfd = open_dev(memb->devnm);
08387a04 1760 if (subfd >= 0) {
8af530b0
N
1761 rv |= Manage_subdevs(
1762 memb->dev, subfd,
1763 &devlist, verbose, 0,
1764 NULL, 0);
08387a04
N
1765 close(subfd);
1766 }
1767 }
1768 free_mdstat(mdstat);
1769 } else
8af530b0
N
1770 rv |= Manage_subdevs(ent->dev, mdfd, &devlist,
1771 verbose, 0, NULL, 0);
1772 if (rv & 2) {
6b63c1a4
N
1773 /* Failed due to EBUSY, try to stop the array.
1774 * Give udisks a chance to unmount it first.
8af530b0 1775 */
6b63c1a4
N
1776 int devid = devnm2devid(ent->devnm);
1777 run_udisks("--unmount", map_dev(major(devid),minor(devid), 0));
fe7e0e64 1778 rv = Manage_stop(ent->dev, mdfd, verbose, 1);
8af530b0
N
1779 if (rv)
1780 /* At least we can try to trigger a 'remove' */
1781 sysfs_uevent(&mdi, "remove");
6b63c1a4
N
1782 if (verbose) {
1783 if (rv)
1784 pr_err("Fail to stop %s too.\n", ent->devnm);
1785 }
8af530b0
N
1786 } else {
1787 devlist.disposition = 'r';
1788 rv = Manage_subdevs(ent->dev, mdfd, &devlist,
1789 verbose, 0, NULL, 0);
1790 }
aae3cdc3 1791 close(mdfd);
08387a04 1792 free_mdstat(ent);
aae3cdc3 1793 return rv;
29ba4804 1794}