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