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