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