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