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