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