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