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