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