Add mbr pseudo metadata handler.
[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"
32
33static int count_active(struct supertype *st, int mdfd, char **availp,
34 struct mdinfo *info);
7e0f6979 35static void find_reject(int mdfd, struct supertype *st, struct mdinfo *sra,
8382f19b
NB
36 int number, __u64 events, int verbose,
37 char *array_name);
f08605b3
N
38static int try_spare(char *devname, int *dfdp, struct dev_policy *pol,
39 struct supertype *st, int verbose);
8382f19b
NB
40
41int Incremental(char *devname, int verbose, int runstop,
0ac91628
N
42 struct supertype *st, char *homehost, int require_homehost,
43 int autof)
8382f19b
NB
44{
45 /* Add this device to an array, creating the array if necessary
598f0d58 46 * and starting the array if sensible or - if runstop>0 - if possible.
8382f19b
NB
47 *
48 * This has several steps:
49 *
50 * 1/ Check if device is permitted by mdadm.conf, reject if not.
51 * 2/ Find metadata, reject if none appropriate (check
52 * version/name from args)
53 * 3/ Check if there is a match in mdadm.conf
215bb3f7
N
54 * 3a/ if not, check for homehost match. If no match, assemble as
55 * a 'foreign' array.
8382f19b
NB
56 * 4/ Determine device number.
57 * - If in mdadm.conf with std name, use that
58 * - UUID in /var/run/mdadm.map use that
59 * - If name is suggestive, use that. unless in use with different uuid.
60 * - Choose a free, high number.
61 * - Use a partitioned device unless strong suggestion not to.
62 * e.g. auto=md
352452c3 63 * Don't choose partitioned for containers.
8382f19b
NB
64 * 5/ Find out if array already exists
65 * 5a/ if it does not
66 * - choose a name, from mdadm.conf or 'name' field in array.
67 * - create the array
68 * - add the device
69 * 5b/ if it does
70 * - check one drive in array to make sure metadata is a reasonably
71 * close match. Reject if not (e.g. different type)
72 * - add the device
73 * 6/ Make sure /var/run/mdadm.map contains this array.
74 * 7/ Is there enough devices to possibly start the array?
352452c3 75 * For a container, this means running Incremental_container.
8382f19b
NB
76 * 7a/ if not, finish with success.
77 * 7b/ if yes,
78 * - read all metadata and arrange devices like -A does
79 * - if number of OK devices match expected, or -R and there are enough,
80 * start the array (auto-readonly).
81 */
82 struct stat stb;
7e83544b 83 struct mdinfo info, dinfo;
8382f19b
NB
84 struct mddev_ident_s *array_list, *match;
85 char chosen_name[1024];
15d4a7e4 86 int rv = 1;
8382f19b 87 struct map_ent *mp, *map = NULL;
15d4a7e4 88 int dfd = -1, mdfd = -1;
8382f19b
NB
89 char *avail;
90 int active_disks;
215bb3f7 91 int trustworthy = FOREIGN;
d7288ddc 92 char *name_to_use;
ad5bc697 93 mdu_array_info_t ainf;
7e83544b 94 struct dev_policy *policy = NULL;
8382f19b
NB
95
96 struct createinfo *ci = conf_get_create_info();
97
8382f19b 98
352452c3 99 /* 1/ Check if device is permitted by mdadm.conf */
8382f19b
NB
100
101 if (!conf_test_dev(devname)) {
102 if (verbose >= 0)
103 fprintf(stderr, Name
104 ": %s not permitted by mdadm.conf.\n",
105 devname);
15d4a7e4 106 goto out;
8382f19b
NB
107 }
108
109 /* 2/ Find metadata, reject if none appropriate (check
110 * version/name from args) */
111
112 dfd = dev_open(devname, O_RDONLY|O_EXCL);
113 if (dfd < 0) {
114 if (verbose >= 0)
115 fprintf(stderr, Name ": cannot open %s: %s.\n",
116 devname, strerror(errno));
15d4a7e4 117 goto out;
8382f19b
NB
118 }
119 if (fstat(dfd, &stb) < 0) {
120 if (verbose >= 0)
121 fprintf(stderr, Name ": fstat failed for %s: %s.\n",
122 devname, strerror(errno));
15d4a7e4 123 goto out;
8382f19b
NB
124 }
125 if ((stb.st_mode & S_IFMT) != S_IFBLK) {
126 if (verbose >= 0)
127 fprintf(stderr, Name ": %s is not a block device.\n",
128 devname);
15d4a7e4 129 goto out;
8382f19b
NB
130 }
131
7e83544b
N
132 dinfo.disk.major = major(stb.st_rdev);
133 dinfo.disk.minor = minor(stb.st_rdev);
134
135 policy = disk_policy(&dinfo);
136
8382f19b
NB
137 if (st == NULL && (st = guess_super(dfd)) == NULL) {
138 if (verbose >= 0)
139 fprintf(stderr, Name
140 ": no recognisable superblock on %s.\n",
141 devname);
f08605b3 142 rv = try_spare(devname, &dfd, policy, st, verbose);
15d4a7e4 143 goto out;
8382f19b 144 }
0f22b998
N
145 if (st->ss->compare_super == NULL ||
146 st->ss->load_super(st, dfd, NULL)) {
8382f19b
NB
147 if (verbose >= 0)
148 fprintf(stderr, Name ": no RAID superblock on %s.\n",
149 devname);
f08605b3 150 rv = try_spare(devname, &dfd, policy, st, verbose);
0f22b998 151 free(st);
15d4a7e4 152 goto out;
8382f19b 153 }
15d4a7e4 154 close (dfd); dfd = -1;
8382f19b 155
f35f2525 156 memset(&info, 0, sizeof(info));
598f0d58 157 st->ss->getinfo_super(st, &info);
8382f19b
NB
158 /* 3/ Check if there is a match in mdadm.conf */
159
160 array_list = conf_get_ident(NULL);
161 match = NULL;
162 for (; array_list; array_list = array_list->next) {
163 if (array_list->uuid_set &&
164 same_uuid(array_list->uuid, info.uuid, st->ss->swapuuid)
165 == 0) {
fe056d1f 166 if (verbose >= 2 && array_list->devname)
8382f19b
NB
167 fprintf(stderr, Name
168 ": UUID differs from %s.\n",
169 array_list->devname);
170 continue;
171 }
172 if (array_list->name[0] &&
173 strcasecmp(array_list->name, info.name) != 0) {
fe056d1f 174 if (verbose >= 2 && array_list->devname)
8382f19b
NB
175 fprintf(stderr, Name
176 ": Name differs from %s.\n",
177 array_list->devname);
178 continue;
179 }
180 if (array_list->devices &&
181 !match_oneof(array_list->devices, devname)) {
fe056d1f 182 if (verbose >= 2 && array_list->devname)
8382f19b
NB
183 fprintf(stderr, Name
184 ": Not a listed device for %s.\n",
185 array_list->devname);
186 continue;
187 }
188 if (array_list->super_minor != UnSet &&
189 array_list->super_minor != info.array.md_minor) {
fe056d1f 190 if (verbose >= 2 && array_list->devname)
8382f19b
NB
191 fprintf(stderr, Name
192 ": Different super-minor to %s.\n",
193 array_list->devname);
194 continue;
195 }
196 if (!array_list->uuid_set &&
197 !array_list->name[0] &&
198 !array_list->devices &&
199 array_list->super_minor == UnSet) {
fe056d1f 200 if (verbose >= 2 && array_list->devname)
8382f19b
NB
201 fprintf(stderr, Name
202 ": %s doesn't have any identifying information.\n",
203 array_list->devname);
204 continue;
205 }
206 /* FIXME, should I check raid_disks and level too?? */
207
208 if (match) {
fe056d1f
N
209 if (verbose >= 0) {
210 if (match->devname && array_list->devname)
211 fprintf(stderr, Name
8382f19b 212 ": we match both %s and %s - cannot decide which to use.\n",
fe056d1f
N
213 match->devname, array_list->devname);
214 else
215 fprintf(stderr, Name
216 ": multiple lines in mdadm.conf match\n");
217 }
15d4a7e4
N
218 rv = 2;
219 goto out;
8382f19b
NB
220 }
221 match = array_list;
222 }
223
112cace6
N
224 if (match && match->devname
225 && strcasecmp(match->devname, "<ignore>") == 0) {
226 if (verbose >= 0)
227 fprintf(stderr, Name ": array containing %s is explicitly"
228 " ignored by mdadm.conf\n",
229 devname);
15d4a7e4 230 goto out;
112cace6
N
231 }
232
7b403fef
N
233 /* 3a/ if not, check for homehost match. If no match, continue
234 * but don't trust the 'name' in the array. Thus a 'random' minor
235 * number will be assigned, and the device name will be based
236 * on that. */
215bb3f7
N
237 if (match)
238 trustworthy = LOCAL;
d1d3482b
N
239 else if (st->ss->match_home(st, homehost) == 1)
240 trustworthy = LOCAL;
241 else if (st->ss->match_home(st, "any") == 1)
242 trustworthy = LOCAL_ANY;
215bb3f7 243 else
d1d3482b
N
244 trustworthy = FOREIGN;
245
246
247 if (!match && !conf_test_metadata(st->ss->name,
248 (trustworthy == LOCAL))) {
249 if (verbose >= 1)
250 fprintf(stderr, Name
251 ": %s has metadata type %s for which "
252 "auto-assembly is disabled\n",
253 devname, st->ss->name);
15d4a7e4 254 goto out;
d1d3482b
N
255 }
256 if (trustworthy == LOCAL_ANY)
ecb02e31 257 trustworthy = LOCAL;
e12e1d1d 258
4ef2f11e
N
259 /* There are three possible sources for 'autof': command line,
260 * ARRAY line in mdadm.conf, or CREATE line in mdadm.conf.
350ac35d
N
261 * ARRAY takes precedence, then command line, then
262 * CREATE.
4ef2f11e 263 */
350ac35d 264 if (match && match->autof)
4ef2f11e
N
265 autof = match->autof;
266 if (autof == 0)
267 autof = ci->autof;
268
215bb3f7 269 if (st->ss->container_content && st->loaded_container) {
97b4d0e9
DW
270 if ((runstop > 0 && info.container_enough >= 0) ||
271 info.container_enough > 0)
272 /* pass */;
273 else {
274 if (verbose)
275 fprintf(stderr, Name ": not enough devices to start the container\n");
15d4a7e4
N
276 rv = 0;
277 goto out;
97b4d0e9
DW
278 }
279
215bb3f7
N
280 /* This is a pre-built container array, so we do something
281 * rather different.
282 */
15d4a7e4 283 rv = Incremental_container(st, devname, verbose, runstop,
215bb3f7 284 autof, trustworthy);
15d4a7e4 285 goto out;
8382f19b 286 }
ecb02e31 287
7cdc0872 288 name_to_use = info.name;
0ac91628 289 if (name_to_use[0] == 0 &&
ecb02e31
N
290 info.array.level == LEVEL_CONTAINER &&
291 trustworthy == LOCAL) {
292 name_to_use = info.text_version;
293 trustworthy = METADATA;
294 }
0ac91628
N
295 if (name_to_use[0] && trustworthy != LOCAL &&
296 ! require_homehost &&
297 conf_name_is_free(name_to_use))
298 trustworthy = LOCAL;
ecb02e31 299
7cdc0872
N
300 /* strip "hostname:" prefix from name if we have decided
301 * to treat it as LOCAL
302 */
303 if (trustworthy == LOCAL && strchr(name_to_use, ':') != NULL)
304 name_to_use = strchr(name_to_use, ':')+1;
305
ad5bc697 306 /* 4/ Check if array exists.
215bb3f7 307 */
93c861ee
DL
308 if (map_lock(&map))
309 fprintf(stderr, Name ": failed to get exclusive lock on "
310 "mapfile\n");
215bb3f7 311 mp = map_by_uuid(&map, info.uuid);
339c2d6c
N
312 if (mp)
313 mdfd = open_dev(mp->devnum);
314 else
215bb3f7 315 mdfd = -1;
d7288ddc 316
8382f19b 317 if (mdfd < 0) {
7e0f6979 318 struct mdinfo *sra;
8382f19b 319
215bb3f7 320 /* Couldn't find an existing array, maybe make a new one */
ffb3e494 321 mdfd = create_mddev(match ? match->devname : NULL,
e12e1d1d 322 name_to_use, autof, trustworthy, chosen_name);
215bb3f7
N
323
324 if (mdfd < 0)
15d4a7e4 325 goto out;
215bb3f7
N
326
327 sysfs_init(&info, mdfd, 0);
328
f35f2525
N
329 if (set_array_info(mdfd, st, &info) != 0) {
330 fprintf(stderr, Name ": failed to set array info for %s: %s\n",
8382f19b 331 chosen_name, strerror(errno));
15d4a7e4
N
332 rv = 2;
333 goto out;
8382f19b 334 }
7801ac20 335
c5afc314
N
336 dinfo = info;
337 dinfo.disk.major = major(stb.st_rdev);
338 dinfo.disk.minor = minor(stb.st_rdev);
339 if (add_disk(mdfd, st, &info, &dinfo) != 0) {
8382f19b
NB
340 fprintf(stderr, Name ": failed to add %s to %s: %s.\n",
341 devname, chosen_name, strerror(errno));
342 ioctl(mdfd, STOP_ARRAY, 0);
15d4a7e4
N
343 rv = 2;
344 goto out;
8382f19b 345 }
197e3eb6 346 sra = sysfs_read(mdfd, fd2devnum(mdfd), GET_DEVS);
06c7f68e 347 if (!sra || !sra->devs || sra->devs->disk.raid_disk >= 0) {
8382f19b
NB
348 /* It really should be 'none' - must be old buggy
349 * kernel, and mdadm -I may not be able to complete.
350 * So reject it.
351 */
352 ioctl(mdfd, STOP_ARRAY, NULL);
353 fprintf(stderr, Name
354 ": You have an old buggy kernel which cannot support\n"
355 " --incremental reliably. Aborting.\n");
8382f19b 356 sysfs_free(sra);
15d4a7e4
N
357 rv = 2;
358 goto out;
8382f19b 359 }
c5afc314 360 info.array.working_disks = 1;
f35f2525 361 sysfs_free(sra);
ad5bc697
N
362 /* 6/ Make sure /var/run/mdadm.map contains this array. */
363 map_update(&map, fd2devnum(mdfd),
364 info.text_version,
365 info.uuid, chosen_name);
8382f19b
NB
366 } else {
367 /* 5b/ if it does */
368 /* - check one drive in array to make sure metadata is a reasonably */
369 /* close match. Reject if not (e.g. different type) */
370 /* - add the device */
371 char dn[20];
372 int dfd2;
8382f19b 373 int err;
7e0f6979 374 struct mdinfo *sra;
3da92f27 375 struct supertype *st2;
c5afc314 376 struct mdinfo info2, *d;
215bb3f7 377
339c2d6c
N
378 if (mp->path)
379 strcpy(chosen_name, mp->path);
380 else
381 strcpy(chosen_name, devnum2devname(mp->devnum));
215bb3f7 382
ef83fe7c
N
383 /* It is generally not OK to add non-spare drives to a
384 * running array as they are probably missing because
385 * they failed. However if runstop is 1, then the
7e83544b
N
386 * array was possibly started early and our best bet is
387 * to add this anyway.
388 * Also if action policy is re-add or better we allow
389 * re-add
3a6ec29a 390 */
ef83fe7c 391 if ((info.disk.state & (1<<MD_DISK_SYNC)) != 0
7e83544b
N
392 && ! policy_action_allows(policy, st->ss->name,
393 act_re_add)
ef83fe7c 394 && runstop < 1) {
1dccfff9
DW
395 int active = 0;
396
397 if (st->ss->external) {
398 char *devname = devnum2devname(fd2devnum(mdfd));
399
400 active = devname && is_container_active(devname);
401 free(devname);
402 } else if (ioctl(mdfd, GET_ARRAY_INFO, &ainf) == 0)
403 active = 1;
404 if (active) {
3a6ec29a
N
405 fprintf(stderr, Name
406 ": not adding %s to active array (without --run) %s\n",
407 devname, chosen_name);
15d4a7e4
N
408 rv = 2;
409 goto out;
3a6ec29a
N
410 }
411 }
197e3eb6 412 sra = sysfs_read(mdfd, fd2devnum(mdfd), (GET_DEVS | GET_STATE));
15d4a7e4
N
413 if (!sra) {
414 rv = 2;
415 goto out;
416 }
7c548327
N
417 if (sra->devs) {
418 sprintf(dn, "%d:%d", sra->devs->disk.major,
419 sra->devs->disk.minor);
420 dfd2 = dev_open(dn, O_RDONLY);
421 st2 = dup_super(st);
422 if (st2->ss->load_super(st2, dfd2, NULL) ||
423 st->ss->compare_super(st, st2) != 0) {
424 fprintf(stderr, Name
425 ": metadata mismatch between %s and "
426 "chosen array %s\n",
427 devname, chosen_name);
7c548327 428 close(dfd2);
15d4a7e4
N
429 rv = 2;
430 goto out;
7c548327 431 }
8382f19b 432 close(dfd2);
7c548327
N
433 memset(&info2, 0, sizeof(info2));
434 st2->ss->getinfo_super(st2, &info2);
435 st2->ss->free_super(st2);
436 if (info.array.level != info2.array.level ||
437 memcmp(info.uuid, info2.uuid, 16) != 0 ||
438 info.array.raid_disks != info2.array.raid_disks) {
439 fprintf(stderr, Name
440 ": unexpected difference between %s and %s.\n",
441 chosen_name, devname);
15d4a7e4
N
442 rv = 2;
443 goto out;
7c548327 444 }
8382f19b 445 }
7801ac20
N
446 info2.disk.major = major(stb.st_rdev);
447 info2.disk.minor = minor(stb.st_rdev);
c5afc314
N
448 /* add disk needs to know about containers */
449 if (st->ss->external)
450 sra->array.level = LEVEL_CONTAINER;
ac7de9d9 451 err = add_disk(mdfd, st, sra, &info2);
8382f19b
NB
452 if (err < 0 && errno == EBUSY) {
453 /* could be another device present with the same
454 * disk.number. Find and reject any such
455 */
456 find_reject(mdfd, st, sra, info.disk.number,
457 info.events, verbose, chosen_name);
ac7de9d9 458 err = add_disk(mdfd, st, sra, &info2);
8382f19b
NB
459 }
460 if (err < 0) {
461 fprintf(stderr, Name ": failed to add %s to %s: %s.\n",
462 devname, chosen_name, strerror(errno));
15d4a7e4
N
463 rv = 2;
464 goto out;
8382f19b 465 }
c5afc314
N
466 info.array.working_disks = 0;
467 for (d = sra->devs; d; d=d->next)
468 info.array.working_disks ++;
469
8382f19b 470 }
8382f19b
NB
471
472 /* 7/ Is there enough devices to possibly start the array? */
473 /* 7a/ if not, finish with success. */
352452c3
N
474 if (info.array.level == LEVEL_CONTAINER) {
475 /* Try to assemble within the container */
ad5bc697 476 map_unlock(&map);
97590376 477 sysfs_uevent(&info, "change");
c5afc314
N
478 if (verbose >= 0)
479 fprintf(stderr, Name
480 ": container %s now has %d devices\n",
481 chosen_name, info.array.working_disks);
a7c6e3fb
N
482 wait_for(chosen_name, mdfd);
483 close(mdfd);
03b7f6c6 484 rv = Incremental(chosen_name, verbose, runstop,
0ac91628 485 NULL, homehost, require_homehost, autof);
03b7f6c6
N
486 if (rv == 1)
487 /* Don't fail the whole -I if a subarray didn't
488 * have enough devices to start yet
489 */
490 rv = 0;
491 return rv;
352452c3 492 }
f8409e54 493 avail = NULL;
8382f19b
NB
494 active_disks = count_active(st, mdfd, &avail, &info);
495 if (enough(info.array.level, info.array.raid_disks,
496 info.array.layout, info.array.state & 1,
3288b419 497 avail, active_disks) == 0) {
8382f19b
NB
498 free(avail);
499 if (verbose >= 0)
500 fprintf(stderr, Name
501 ": %s attached to %s, not enough to start (%d).\n",
502 devname, chosen_name, active_disks);
ad5bc697 503 map_unlock(&map);
15d4a7e4
N
504 rv = 0;
505 goto out;
8382f19b
NB
506 }
507 free(avail);
508
509 /* 7b/ if yes, */
510 /* - if number of OK devices match expected, or -R and there */
511 /* are enough, */
512 /* + add any bitmap file */
513 /* + start the array (auto-readonly). */
8382f19b
NB
514
515 if (ioctl(mdfd, GET_ARRAY_INFO, &ainf) == 0) {
516 if (verbose >= 0)
517 fprintf(stderr, Name
518 ": %s attached to %s which is already active.\n",
519 devname, chosen_name);
ad5bc697 520 map_unlock(&map);
15d4a7e4
N
521 rv = 0;
522 goto out;
8382f19b 523 }
ad5bc697
N
524
525 map_unlock(&map);
8382f19b 526 if (runstop > 0 || active_disks >= info.array.working_disks) {
7e83544b 527 struct mdinfo *sra, *dsk;
8382f19b
NB
528 /* Let's try to start it */
529 if (match && match->bitmap_file) {
530 int bmfd = open(match->bitmap_file, O_RDWR);
531 if (bmfd < 0) {
532 fprintf(stderr, Name
533 ": Could not open bitmap file %s.\n",
534 match->bitmap_file);
15d4a7e4 535 goto out;
8382f19b
NB
536 }
537 if (ioctl(mdfd, SET_BITMAP_FILE, bmfd) != 0) {
538 close(bmfd);
539 fprintf(stderr, Name
540 ": Failed to set bitmapfile for %s.\n",
541 chosen_name);
15d4a7e4 542 goto out;
8382f19b
NB
543 }
544 close(bmfd);
545 }
7e83544b
N
546 /* GET_* needed so add_disk works below */
547 sra = sysfs_read(mdfd, fd2devnum(mdfd),
548 GET_DEVS|GET_OFFSET|GET_SIZE|GET_STATE);
7b403fef 549 if ((sra == NULL || active_disks >= info.array.working_disks)
215bb3f7 550 && trustworthy != FOREIGN)
8382f19b
NB
551 rv = ioctl(mdfd, RUN_ARRAY, NULL);
552 else
553 rv = sysfs_set_str(sra, NULL,
554 "array_state", "read-auto");
555 if (rv == 0) {
556 if (verbose >= 0)
557 fprintf(stderr, Name
7e83544b 558 ": %s attached to %s, which has been started.\n",
8382f19b
NB
559 devname, chosen_name);
560 rv = 0;
a7c6e3fb 561 wait_for(chosen_name, mdfd);
7e83544b
N
562 /* We just started the array, so some devices
563 * might have been evicted from the array
564 * because their event counts were too old.
565 * If the action=re-add policy is in-force for
566 * those devices we should re-add them now.
567 */
568 for (dsk = sra->devs; dsk ; dsk = dsk->next) {
569 if (disk_action_allows(dsk, st->ss->name, act_re_add) &&
570 add_disk(mdfd, st, sra, dsk) == 0)
571 fprintf(stderr, Name
572 ": %s re-added to %s\n",
573 dsk->sys_name, chosen_name);
574 }
8382f19b
NB
575 } else {
576 fprintf(stderr, Name
577 ": %s attached to %s, but failed to start: %s.\n",
578 devname, chosen_name, strerror(errno));
579 rv = 1;
580 }
581 } else {
582 if (verbose >= 0)
583 fprintf(stderr, Name
584 ": %s attached to %s, not enough to start safely.\n",
585 devname, chosen_name);
586 rv = 0;
587 }
15d4a7e4
N
588out:
589 if (dfd >= 0)
590 close(dfd);
591 if (mdfd >= 0)
592 close(mdfd);
7e83544b
N
593 if (policy)
594 dev_policy_free(policy);
8382f19b
NB
595 return rv;
596}
597
7e0f6979 598static void find_reject(int mdfd, struct supertype *st, struct mdinfo *sra,
8382f19b
NB
599 int number, __u64 events, int verbose,
600 char *array_name)
601{
3da92f27 602 /* Find a device attached to this array with a disk.number of number
8382f19b
NB
603 * and events less than the passed events, and remove the device.
604 */
06c7f68e 605 struct mdinfo *d;
8382f19b
NB
606 mdu_array_info_t ra;
607
608 if (ioctl(mdfd, GET_ARRAY_INFO, &ra) == 0)
609 return; /* not safe to remove from active arrays
610 * without thinking more */
611
612 for (d = sra->devs; d ; d = d->next) {
613 char dn[10];
614 int dfd;
8382f19b 615 struct mdinfo info;
06c7f68e 616 sprintf(dn, "%d:%d", d->disk.major, d->disk.minor);
8382f19b
NB
617 dfd = dev_open(dn, O_RDONLY);
618 if (dfd < 0)
619 continue;
3da92f27 620 if (st->ss->load_super(st, dfd, NULL)) {
8382f19b
NB
621 close(dfd);
622 continue;
623 }
3da92f27
NB
624 st->ss->getinfo_super(st, &info);
625 st->ss->free_super(st);
8382f19b
NB
626 close(dfd);
627
628 if (info.disk.number != number ||
629 info.events >= events)
630 continue;
631
06c7f68e 632 if (d->disk.raid_disk > -1)
8382f19b
NB
633 sysfs_set_str(sra, d, "slot", "none");
634 if (sysfs_set_str(sra, d, "state", "remove") == 0)
635 if (verbose >= 0)
636 fprintf(stderr, Name
637 ": removing old device %s from %s\n",
06c7f68e 638 d->sys_name+4, array_name);
8382f19b
NB
639 }
640}
641
642static int count_active(struct supertype *st, int mdfd, char **availp,
643 struct mdinfo *bestinfo)
644{
645 /* count how many devices in sra think they are active */
06c7f68e 646 struct mdinfo *d;
8382f19b
NB
647 int cnt = 0, cnt1 = 0;
648 __u64 max_events = 0;
7e0f6979 649 struct mdinfo *sra = sysfs_read(mdfd, -1, GET_DEVS | GET_STATE);
8382f19b
NB
650 char *avail = NULL;
651
b526e52d
DW
652 if (!sra)
653 return 0;
654
8382f19b
NB
655 for (d = sra->devs ; d ; d = d->next) {
656 char dn[30];
657 int dfd;
8382f19b
NB
658 int ok;
659 struct mdinfo info;
660
06c7f68e 661 sprintf(dn, "%d:%d", d->disk.major, d->disk.minor);
8382f19b
NB
662 dfd = dev_open(dn, O_RDONLY);
663 if (dfd < 0)
664 continue;
3da92f27 665 ok = st->ss->load_super(st, dfd, NULL);
8382f19b
NB
666 close(dfd);
667 if (ok != 0)
668 continue;
3da92f27 669 st->ss->getinfo_super(st, &info);
43aaf431
DL
670 if (!avail) {
671 avail = malloc(info.array.raid_disks);
672 if (!avail) {
673 fprintf(stderr, Name ": out of memory.\n");
674 exit(1);
675 }
676 memset(avail, 0, info.array.raid_disks);
677 *availp = avail;
678 }
679
8382f19b
NB
680 if (info.disk.state & (1<<MD_DISK_SYNC))
681 {
8382f19b
NB
682 if (cnt == 0) {
683 cnt++;
684 max_events = info.events;
685 avail[info.disk.raid_disk] = 2;
3da92f27 686 st->ss->getinfo_super(st, bestinfo);
8382f19b
NB
687 } else if (info.events == max_events) {
688 cnt++;
689 avail[info.disk.raid_disk] = 2;
690 } else if (info.events == max_events-1) {
691 cnt1++;
692 avail[info.disk.raid_disk] = 1;
693 } else if (info.events < max_events - 1)
694 ;
695 else if (info.events == max_events+1) {
696 int i;
697 cnt1 = cnt;
698 cnt = 1;
699 max_events = info.events;
700 for (i=0; i<info.array.raid_disks; i++)
701 if (avail[i])
702 avail[i]--;
703 avail[info.disk.raid_disk] = 2;
3da92f27 704 st->ss->getinfo_super(st, bestinfo);
8382f19b
NB
705 } else { /* info.events much bigger */
706 cnt = 1; cnt1 = 0;
707 memset(avail, 0, info.disk.raid_disk);
708 max_events = info.events;
3da92f27 709 st->ss->getinfo_super(st, bestinfo);
8382f19b
NB
710 }
711 }
3da92f27 712 st->ss->free_super(st);
8382f19b
NB
713 }
714 return cnt + cnt1;
715}
716
f08605b3
N
717static int try_spare(char *devname, int *dfdp, struct dev_policy *pol,
718 struct supertype *st, int verbose)
719{
720 /* This device doesn't have any md metadata
721 * If it is 'bare' and theh device policy allows 'spare' look for
722 * an array or container to attach it to.
723 * If st is set, then only arrays of that type are considered
724 * Return 0 on success, or some exit code on failure, probably 1.
725 */
726 int rv = -1;
727 char bufpad[4096 + 4096];
728 char *buf = (char*)(((long)bufpad + 4096) & ~4095);
729 struct stat stb;
730 struct map_ent *mp, *map = NULL;
731 struct mdinfo *chosen = NULL;
732 int dfd = *dfdp;
733
734 /* First check policy */
735 if (!policy_action_allows(pol, st?st->ss->name:NULL, act_spare))
736 return 1;
737
738 if (fstat(dfd, &stb) != 0)
739 return 1;
740 /* Now check if the device is bare - we don't add non-bare devices
741 * yet even if action=-spare
742 */
743
744 if (lseek(dfd, 0, SEEK_SET) != 0 ||
745 read(dfd, buf, 4096) != 4096) {
746 not_bare:
747 if (verbose > 1)
748 fprintf(stderr, Name ": %s is not bare, so not considering as a spare\n",
749 devname);
750 return 1;
751 }
752 if (buf[0] != '\0' && buf[0] != '\x5a' && buf[0] != '\xff')
753 goto not_bare;
754 if (memcmp(buf, buf+1, 4095) != 0)
755 goto not_bare;
756
757 /* OK, first 4K appear blank, try the end. */
758 if (lseek(dfd, -4096, SEEK_END) < 0 ||
759 read(dfd, buf, 4096) != 4096)
760 goto not_bare;
761
762 if (buf[0] != '\0' && buf[0] != '\x5a' && buf[0] != '\xff')
763 goto not_bare;
764 if (memcmp(buf, buf+1, 4095) != 0)
765 goto not_bare;
766
767 /* This device passes our test for 'is bare'.
768 * Now we need to find a suitable array to add this to.
769 * We only accept arrays that:
770 * - match 'st'
771 * - are in the same domains as the device
772 * - are of an size for which the device will be useful
773 * and we choose the one that is the most degraded
774 */
775
776 if (map_lock(&map)) {
777 fprintf(stderr, Name ": failed to get exclusive lock on "
778 "mapfile\n");
779 return 1;
780 }
781 for (mp = map ; mp ; mp = mp->next) {
782 struct supertype *st2;
783 struct domainlist *dl = NULL;
784 struct mdinfo *sra;
785 unsigned long long devsize;
786
787 if (is_subarray(mp->metadata))
788 continue;
789 if (st) {
790 st2 = st->ss->match_metadata_desc(mp->metadata);
791 if (!st2 ||
792 (st->minor_version >= 0 &&
793 st->minor_version != st2->minor_version)) {
794 if (verbose > 1)
795 fprintf(stderr, Name ": not adding %s to %s as metadata type doesn't match\n",
796 devname, mp->path);
797 free(st2);
798 continue;
799 }
800 free(st2);
801 }
802 sra = sysfs_read(-1, mp->devnum,
803 GET_DEVS|GET_OFFSET|GET_SIZE|GET_STATE|
804 GET_DEGRADED|GET_COMPONENT|GET_VERSION);
805 if (!sra) {
806 /* Probably a container - no degraded info */
807 sra = sysfs_read(-1, mp->devnum,
808 GET_DEVS|GET_OFFSET|GET_SIZE|GET_STATE|
809 GET_COMPONENT|GET_VERSION);
810 if (sra)
811 sra->array.failed_disks = 0;
812 }
813 if (!sra)
814 continue;
815 if (st == NULL) {
816 int i;
817 st2 = NULL;
818 for(i=0; !st2 && superlist[i]; i++)
819 st2 = superlist[i]->match_metadata_desc(
820 sra->text_version);
821 } else
822 st2 = st;
823 get_dev_size(dfd, NULL, &devsize);
824 if (st2->ss->avail_size(st2, devsize) < sra->component_size) {
825 if (verbose > 1)
826 fprintf(stderr, Name ": not adding %s to %s as it is too small\n",
827 devname, mp->path);
828 goto next;
829 }
830 dl = domain_from_array(sra, st2->ss->name);
831 if (!domain_test(dl, pol, st2->ss->name)) {
832 /* domain test fails */
833 if (verbose > 1)
834 fprintf(stderr, Name ": not adding %s to %s as it is not in a compatible domain\n",
835 devname, mp->path);
836
837 goto next;
838 }
839 /* all tests passed, OK to add to this array */
840 if (!chosen) {
841 chosen = sra;
842 sra = NULL;
843 } else if (chosen->array.failed_disks < sra->array.failed_disks) {
844 sysfs_free(chosen);
845 chosen = sra;
846 sra = NULL;
847 }
848 next:
849 if (sra)
850 sysfs_free(sra);
851 if (st != st2)
852 free(st2);
853 if (dl)
854 domain_free(dl);
855 }
856 if (chosen) {
857 /* add current device to chosen array as a spare */
858 int mdfd = open_dev(devname2devnum(chosen->sys_name));
859 if (mdfd >= 0) {
860 struct mddev_dev_s devlist;
861 char devname[20];
862 devlist.next = NULL;
863 devlist.used = 0;
864 devlist.re_add = 0;
865 devlist.writemostly = 0;
866 devlist.devname = devname;
867 sprintf(devname, "%d:%d", major(stb.st_rdev),
868 minor(stb.st_rdev));
869 devlist.disposition = 'a';
870 close(dfd);
871 *dfdp = -1;
872 rv = Manage_subdevs(chosen->sys_name, mdfd, &devlist,
873 -1, 0);
874 close(mdfd);
875 }
876 if (verbose > 0) {
877 if (rv == 0)
878 fprintf(stderr, Name ": added %s as spare for %s\n",
879 devname, chosen->sys_name);
880 else
881 fprintf(stderr, Name ": failed to add %s as spare for %s\n",
882 devname, chosen->sys_name);
883 }
884 sysfs_free(chosen);
885 }
886 return rv ? 0 : 1;
887}
888
8382f19b
NB
889int IncrementalScan(int verbose)
890{
891 /* look at every device listed in the 'map' file.
892 * If one is found that is not running then:
893 * look in mdadm.conf for bitmap file.
894 * if one exists, but array has none, add it.
895 * try to start array in auto-readonly mode
896 */
897 struct map_ent *mapl = NULL;
898 struct map_ent *me;
899 mddev_ident_t devs, mddev;
900 int rv = 0;
901
902 map_read(&mapl);
903 devs = conf_get_ident(NULL);
904
905 for (me = mapl ; me ; me = me->next) {
8382f19b
NB
906 mdu_array_info_t array;
907 mdu_bitmap_file_t bmf;
7e0f6979 908 struct mdinfo *sra;
339c2d6c 909 int mdfd = open_dev(me->devnum);
215bb3f7 910
8382f19b
NB
911 if (mdfd < 0)
912 continue;
913 if (ioctl(mdfd, GET_ARRAY_INFO, &array) == 0 ||
914 errno != ENODEV) {
915 close(mdfd);
916 continue;
917 }
918 /* Ok, we can try this one. Maybe it needs a bitmap */
919 for (mddev = devs ; mddev ; mddev = mddev->next)
339c2d6c 920 if (mddev->devname && me->path
2400e6eb 921 && devname_matches(mddev->devname, me->path))
8382f19b
NB
922 break;
923 if (mddev && mddev->bitmap_file) {
924 /*
925 * Note: early kernels will wrongly fail this, so it
926 * is a hint only
927 */
928 int added = -1;
929 if (ioctl(mdfd, GET_ARRAY_INFO, &bmf) < 0) {
930 int bmfd = open(mddev->bitmap_file, O_RDWR);
931 if (bmfd >= 0) {
932 added = ioctl(mdfd, SET_BITMAP_FILE,
933 bmfd);
934 close(bmfd);
935 }
936 }
937 if (verbose >= 0) {
938 if (added == 0)
939 fprintf(stderr, Name
940 ": Added bitmap %s to %s\n",
941 mddev->bitmap_file, me->path);
942 else if (errno != EEXIST)
943 fprintf(stderr, Name
944 ": Failed to add bitmap to %s: %s\n",
945 me->path, strerror(errno));
946 }
947 }
948 sra = sysfs_read(mdfd, 0, 0);
949 if (sra) {
950 if (sysfs_set_str(sra, NULL,
951 "array_state", "read-auto") == 0) {
952 if (verbose >= 0)
953 fprintf(stderr, Name
954 ": started array %s\n",
339c2d6c 955 me->path ?: devnum2devname(me->devnum));
8382f19b
NB
956 } else {
957 fprintf(stderr, Name
958 ": failed to start array %s: %s\n",
339c2d6c
N
959 me->path ?: devnum2devname(me->devnum),
960 strerror(errno));
8382f19b
NB
961 rv = 1;
962 }
963 }
964 }
965 return rv;
966}
598f0d58 967
1771a6e2
N
968static char *container2devname(char *devname)
969{
970 char *mdname = NULL;
971
972 if (devname[0] == '/') {
973 int fd = open(devname, O_RDONLY);
974 if (fd >= 0) {
975 mdname = devnum2devname(fd2devnum(fd));
976 close(fd);
977 }
978 } else {
979 int uuid[4];
980 struct map_ent *mp, *map = NULL;
981
982 if (!parse_uuid(devname, uuid))
983 return mdname;
984 mp = map_by_uuid(&map, uuid);
985 if (mp)
986 mdname = devnum2devname(mp->devnum);
987 map_free(map);
988 }
989
990 return mdname;
991}
992
598f0d58 993int Incremental_container(struct supertype *st, char *devname, int verbose,
215bb3f7 994 int runstop, int autof, int trustworthy)
598f0d58
NB
995{
996 /* Collect the contents of this container and for each
997 * array, choose a device name and assemble the array.
998 */
999
1000 struct mdinfo *list = st->ss->container_content(st);
1001 struct mdinfo *ra;
ad5bc697
N
1002 struct map_ent *map = NULL;
1003
93c861ee
DL
1004 if (map_lock(&map))
1005 fprintf(stderr, Name ": failed to get exclusive lock on "
1006 "mapfile\n");
598f0d58
NB
1007
1008 for (ra = list ; ra ; ra = ra->next) {
598f0d58
NB
1009 int mdfd;
1010 char chosen_name[1024];
ad5bc697 1011 struct map_ent *mp;
dbb44303 1012 struct mddev_ident_s *match = NULL;
598f0d58 1013
c5afc314 1014 mp = map_by_uuid(&map, ra->uuid);
d7288ddc 1015
30926600
N
1016 if (mp) {
1017 mdfd = open_dev(mp->devnum);
339c2d6c
N
1018 if (mp->path)
1019 strcpy(chosen_name, mp->path);
1020 else
1021 strcpy(chosen_name, devnum2devname(mp->devnum));
30926600 1022 } else {
dbb44303 1023
112cace6 1024 /* Check in mdadm.conf for container == devname and
dbb44303
N
1025 * member == ra->text_version after second slash.
1026 */
1027 char *sub = strchr(ra->text_version+1, '/');
1028 struct mddev_ident_s *array_list;
1029 if (sub) {
1030 sub++;
1031 array_list = conf_get_ident(NULL);
1032 } else
1033 array_list = NULL;
1034 for(; array_list ; array_list = array_list->next) {
dbb44303
N
1035 char *dn;
1036 if (array_list->member == NULL ||
1037 array_list->container == NULL)
1038 continue;
1039 if (strcmp(array_list->member, sub) != 0)
1040 continue;
71d60c48
DW
1041 if (array_list->uuid_set &&
1042 !same_uuid(ra->uuid, array_list->uuid, st->ss->swapuuid))
1043 continue;
1771a6e2
N
1044 dn = container2devname(array_list->container);
1045 if (dn == NULL)
dbb44303 1046 continue;
dbb44303
N
1047 if (strncmp(dn, ra->text_version+1,
1048 strlen(dn)) != 0 ||
1049 ra->text_version[strlen(dn)+1] != '/') {
1050 free(dn);
1051 continue;
1052 }
1053 free(dn);
1054 /* we have a match */
1055 match = array_list;
71d60c48
DW
1056 if (verbose>0)
1057 fprintf(stderr, Name ": match found for member %s\n",
1058 array_list->member);
dbb44303
N
1059 break;
1060 }
dbb44303 1061
112cace6
N
1062 if (match && match->devname &&
1063 strcasecmp(match->devname, "<ignore>") == 0) {
1064 if (verbose > 0)
1065 fprintf(stderr, Name ": array %s/%s is "
1066 "explicitly ignored by mdadm.conf\n",
1067 match->container, match->member);
1068 return 2;
1069 }
05833051
N
1070 if (match)
1071 trustworthy = LOCAL;
112cace6 1072
30926600
N
1073 mdfd = create_mddev(match ? match->devname : NULL,
1074 ra->name,
1075 autof,
1076 trustworthy,
1077 chosen_name);
1078 }
598f0d58
NB
1079
1080 if (mdfd < 0) {
1081 fprintf(stderr, Name ": failed to open %s: %s.\n",
1082 chosen_name, strerror(errno));
1083 return 2;
1084 }
1085
03b7f6c6
N
1086 assemble_container_content(st, mdfd, ra, runstop,
1087 chosen_name, verbose);
598f0d58 1088 }
ad5bc697 1089 map_unlock(&map);
598f0d58
NB
1090 return 0;
1091}
29ba4804
N
1092
1093/*
1094 * IncrementalRemove - Attempt to see if the passed in device belongs to any
1095 * raid arrays, and if so first fail (if needed) and then remove the device.
1096 *
1097 * @devname - The device we want to remove
1098 *
1099 * Note: the device name must be a kernel name like "sda", so
1100 * that we can find it in /proc/mdstat
1101 */
1102int IncrementalRemove(char *devname, int verbose)
1103{
1104 int mdfd;
aae3cdc3 1105 int rv;
29ba4804
N
1106 struct mdstat_ent *ent;
1107 struct mddev_dev_s devlist;
1108
1109 if (strchr(devname, '/')) {
1110 fprintf(stderr, Name ": incremental removal requires a "
1111 "kernel device name, not a file: %s\n", devname);
1112 return 1;
1113 }
1114 ent = mdstat_by_component(devname);
1115 if (!ent) {
1116 fprintf(stderr, Name ": %s does not appear to be a component "
1117 "of any array\n", devname);
1118 return 1;
1119 }
1120 mdfd = open_dev(ent->devnum);
1121 if (mdfd < 0) {
1122 fprintf(stderr, Name ": Cannot open array %s!!\n", ent->dev);
1123 return 1;
1124 }
1125 memset(&devlist, 0, sizeof(devlist));
1126 devlist.devname = devname;
1127 devlist.disposition = 'f';
7d2e6486 1128 Manage_subdevs(ent->dev, mdfd, &devlist, verbose, 0);
29ba4804 1129 devlist.disposition = 'r';
aae3cdc3
PC
1130 rv = Manage_subdevs(ent->dev, mdfd, &devlist, verbose, 0);
1131 close(mdfd);
1132 return rv;
29ba4804 1133}