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