]> git.ipfire.org Git - thirdparty/mdadm.git/blame - Incremental.c
config: enhance name matching in conf_get_ident.
[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 *
5 * Copyright (C) 2006 Neil Brown <neilb@suse.de>
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);
38
39int Incremental(char *devname, int verbose, int runstop,
0ac91628
N
40 struct supertype *st, char *homehost, int require_homehost,
41 int autof)
8382f19b
NB
42{
43 /* Add this device to an array, creating the array if necessary
598f0d58 44 * and starting the array if sensible or - if runstop>0 - if possible.
8382f19b
NB
45 *
46 * This has several steps:
47 *
48 * 1/ Check if device is permitted by mdadm.conf, reject if not.
49 * 2/ Find metadata, reject if none appropriate (check
50 * version/name from args)
51 * 3/ Check if there is a match in mdadm.conf
215bb3f7
N
52 * 3a/ if not, check for homehost match. If no match, assemble as
53 * a 'foreign' array.
8382f19b
NB
54 * 4/ Determine device number.
55 * - If in mdadm.conf with std name, use that
56 * - UUID in /var/run/mdadm.map use that
57 * - If name is suggestive, use that. unless in use with different uuid.
58 * - Choose a free, high number.
59 * - Use a partitioned device unless strong suggestion not to.
60 * e.g. auto=md
352452c3 61 * Don't choose partitioned for containers.
8382f19b
NB
62 * 5/ Find out if array already exists
63 * 5a/ if it does not
64 * - choose a name, from mdadm.conf or 'name' field in array.
65 * - create the array
66 * - add the device
67 * 5b/ if it does
68 * - check one drive in array to make sure metadata is a reasonably
69 * close match. Reject if not (e.g. different type)
70 * - add the device
71 * 6/ Make sure /var/run/mdadm.map contains this array.
72 * 7/ Is there enough devices to possibly start the array?
352452c3 73 * For a container, this means running Incremental_container.
8382f19b
NB
74 * 7a/ if not, finish with success.
75 * 7b/ if yes,
76 * - read all metadata and arrange devices like -A does
77 * - if number of OK devices match expected, or -R and there are enough,
78 * start the array (auto-readonly).
79 */
80 struct stat stb;
f35f2525 81 struct mdinfo info;
8382f19b
NB
82 struct mddev_ident_s *array_list, *match;
83 char chosen_name[1024];
84 int rv;
8382f19b
NB
85 struct map_ent *mp, *map = NULL;
86 int dfd, mdfd;
87 char *avail;
88 int active_disks;
215bb3f7 89 int trustworthy = FOREIGN;
d7288ddc 90 char *name_to_use;
ad5bc697 91 mdu_array_info_t ainf;
8382f19b
NB
92
93 struct createinfo *ci = conf_get_create_info();
94
8382f19b 95
352452c3 96 /* 1/ Check if device is permitted by mdadm.conf */
8382f19b
NB
97
98 if (!conf_test_dev(devname)) {
99 if (verbose >= 0)
100 fprintf(stderr, Name
101 ": %s not permitted by mdadm.conf.\n",
102 devname);
103 return 1;
104 }
105
106 /* 2/ Find metadata, reject if none appropriate (check
107 * version/name from args) */
108
109 dfd = dev_open(devname, O_RDONLY|O_EXCL);
110 if (dfd < 0) {
111 if (verbose >= 0)
112 fprintf(stderr, Name ": cannot open %s: %s.\n",
113 devname, strerror(errno));
114 return 1;
115 }
116 if (fstat(dfd, &stb) < 0) {
117 if (verbose >= 0)
118 fprintf(stderr, Name ": fstat failed for %s: %s.\n",
119 devname, strerror(errno));
120 close(dfd);
121 return 1;
122 }
123 if ((stb.st_mode & S_IFMT) != S_IFBLK) {
124 if (verbose >= 0)
125 fprintf(stderr, Name ": %s is not a block device.\n",
126 devname);
127 close(dfd);
128 return 1;
129 }
130
131 if (st == NULL && (st = guess_super(dfd)) == NULL) {
132 if (verbose >= 0)
133 fprintf(stderr, Name
134 ": no recognisable superblock on %s.\n",
135 devname);
136 close(dfd);
137 return 1;
138 }
3da92f27 139 if (st->ss->load_super(st, dfd, NULL)) {
8382f19b
NB
140 if (verbose >= 0)
141 fprintf(stderr, Name ": no RAID superblock on %s.\n",
142 devname);
143 close(dfd);
144 return 1;
145 }
8382f19b
NB
146 close (dfd);
147
f35f2525 148 memset(&info, 0, sizeof(info));
598f0d58 149 st->ss->getinfo_super(st, &info);
8382f19b
NB
150 /* 3/ Check if there is a match in mdadm.conf */
151
152 array_list = conf_get_ident(NULL);
153 match = NULL;
154 for (; array_list; array_list = array_list->next) {
155 if (array_list->uuid_set &&
156 same_uuid(array_list->uuid, info.uuid, st->ss->swapuuid)
157 == 0) {
fe056d1f 158 if (verbose >= 2 && array_list->devname)
8382f19b
NB
159 fprintf(stderr, Name
160 ": UUID differs from %s.\n",
161 array_list->devname);
162 continue;
163 }
164 if (array_list->name[0] &&
165 strcasecmp(array_list->name, info.name) != 0) {
fe056d1f 166 if (verbose >= 2 && array_list->devname)
8382f19b
NB
167 fprintf(stderr, Name
168 ": Name differs from %s.\n",
169 array_list->devname);
170 continue;
171 }
172 if (array_list->devices &&
173 !match_oneof(array_list->devices, devname)) {
fe056d1f 174 if (verbose >= 2 && array_list->devname)
8382f19b
NB
175 fprintf(stderr, Name
176 ": Not a listed device for %s.\n",
177 array_list->devname);
178 continue;
179 }
180 if (array_list->super_minor != UnSet &&
181 array_list->super_minor != info.array.md_minor) {
fe056d1f 182 if (verbose >= 2 && array_list->devname)
8382f19b
NB
183 fprintf(stderr, Name
184 ": Different super-minor to %s.\n",
185 array_list->devname);
186 continue;
187 }
188 if (!array_list->uuid_set &&
189 !array_list->name[0] &&
190 !array_list->devices &&
191 array_list->super_minor == UnSet) {
fe056d1f 192 if (verbose >= 2 && array_list->devname)
8382f19b
NB
193 fprintf(stderr, Name
194 ": %s doesn't have any identifying information.\n",
195 array_list->devname);
196 continue;
197 }
198 /* FIXME, should I check raid_disks and level too?? */
199
200 if (match) {
fe056d1f
N
201 if (verbose >= 0) {
202 if (match->devname && array_list->devname)
203 fprintf(stderr, Name
8382f19b 204 ": we match both %s and %s - cannot decide which to use.\n",
fe056d1f
N
205 match->devname, array_list->devname);
206 else
207 fprintf(stderr, Name
208 ": multiple lines in mdadm.conf match\n");
209 }
8382f19b
NB
210 return 2;
211 }
212 match = array_list;
213 }
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);
221 return 1;
222 }
223
31015d57
N
224 if (!match && !conf_test_metadata(st->ss->name)) {
225 if (verbose >= 1)
226 fprintf(stderr, Name
227 ": %s has metadata type %s for which "
228 "auto-assembly is disabled\n",
229 devname, st->ss->name);
230 return 1;
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;
745f72f6
N
239 else if ((homehost == NULL ||
240 st->ss->match_home(st, homehost) != 1) &&
241 st->ss->match_home(st, "any") != 1)
215bb3f7 242 trustworthy = FOREIGN;
215bb3f7 243 else
ecb02e31 244 trustworthy = LOCAL;
e12e1d1d 245
4ef2f11e
N
246 /* There are three possible sources for 'autof': command line,
247 * ARRAY line in mdadm.conf, or CREATE line in mdadm.conf.
350ac35d
N
248 * ARRAY takes precedence, then command line, then
249 * CREATE.
4ef2f11e 250 */
350ac35d 251 if (match && match->autof)
4ef2f11e
N
252 autof = match->autof;
253 if (autof == 0)
254 autof = ci->autof;
255
215bb3f7
N
256 if (st->ss->container_content && st->loaded_container) {
257 /* This is a pre-built container array, so we do something
258 * rather different.
259 */
260 return Incremental_container(st, devname, verbose, runstop,
261 autof, trustworthy);
8382f19b 262 }
ecb02e31
N
263 name_to_use = strchr(info.name, ':');
264 if (name_to_use)
265 name_to_use++;
266 else
267 name_to_use = info.name;
268
0ac91628 269 if (name_to_use[0] == 0 &&
ecb02e31
N
270 info.array.level == LEVEL_CONTAINER &&
271 trustworthy == LOCAL) {
272 name_to_use = info.text_version;
273 trustworthy = METADATA;
274 }
0ac91628
N
275 if (name_to_use[0] && trustworthy != LOCAL &&
276 ! require_homehost &&
277 conf_name_is_free(name_to_use))
278 trustworthy = LOCAL;
ecb02e31 279
ad5bc697 280 /* 4/ Check if array exists.
215bb3f7 281 */
ad5bc697 282 map_lock(&map);
215bb3f7 283 mp = map_by_uuid(&map, info.uuid);
ffb3e494 284 if (mp) {
215bb3f7 285 mdfd = open_mddev(mp->path, 0);
ffb3e494
N
286 if (mdfd < 0 && mddev_busy(mp->devnum)) {
287 /* maybe udev hasn't created it yet. */
288 char buf[50];
289 sprintf(buf, "%d:%d", dev2major(mp->devnum),
290 dev2minor(mp->devnum));
291 mdfd = dev_open(buf, O_RDWR);
292 }
293 } else
215bb3f7 294 mdfd = -1;
d7288ddc 295
8382f19b 296 if (mdfd < 0) {
7e0f6979 297 struct mdinfo *sra;
c5afc314 298 struct mdinfo dinfo;
8382f19b 299
215bb3f7 300 /* Couldn't find an existing array, maybe make a new one */
ffb3e494 301 mdfd = create_mddev(match ? match->devname : NULL,
e12e1d1d 302 name_to_use, autof, trustworthy, chosen_name);
215bb3f7
N
303
304 if (mdfd < 0)
305 return 1;
306
307 sysfs_init(&info, mdfd, 0);
308
f35f2525
N
309 if (set_array_info(mdfd, st, &info) != 0) {
310 fprintf(stderr, Name ": failed to set array info for %s: %s\n",
8382f19b
NB
311 chosen_name, strerror(errno));
312 close(mdfd);
313 return 2;
314 }
7801ac20 315
c5afc314
N
316 dinfo = info;
317 dinfo.disk.major = major(stb.st_rdev);
318 dinfo.disk.minor = minor(stb.st_rdev);
319 if (add_disk(mdfd, st, &info, &dinfo) != 0) {
8382f19b
NB
320 fprintf(stderr, Name ": failed to add %s to %s: %s.\n",
321 devname, chosen_name, strerror(errno));
322 ioctl(mdfd, STOP_ARRAY, 0);
323 close(mdfd);
324 return 2;
325 }
197e3eb6 326 sra = sysfs_read(mdfd, fd2devnum(mdfd), GET_DEVS);
06c7f68e 327 if (!sra || !sra->devs || sra->devs->disk.raid_disk >= 0) {
8382f19b
NB
328 /* It really should be 'none' - must be old buggy
329 * kernel, and mdadm -I may not be able to complete.
330 * So reject it.
331 */
332 ioctl(mdfd, STOP_ARRAY, NULL);
333 fprintf(stderr, Name
334 ": You have an old buggy kernel which cannot support\n"
335 " --incremental reliably. Aborting.\n");
336 close(mdfd);
337 sysfs_free(sra);
338 return 2;
339 }
c5afc314 340 info.array.working_disks = 1;
f35f2525 341 sysfs_free(sra);
ad5bc697
N
342 /* 6/ Make sure /var/run/mdadm.map contains this array. */
343 map_update(&map, fd2devnum(mdfd),
344 info.text_version,
345 info.uuid, chosen_name);
8382f19b
NB
346 } else {
347 /* 5b/ if it does */
348 /* - check one drive in array to make sure metadata is a reasonably */
349 /* close match. Reject if not (e.g. different type) */
350 /* - add the device */
351 char dn[20];
352 int dfd2;
8382f19b 353 int err;
7e0f6979 354 struct mdinfo *sra;
3da92f27 355 struct supertype *st2;
c5afc314 356 struct mdinfo info2, *d;
215bb3f7
N
357
358 strcpy(chosen_name, mp->path);
359
197e3eb6 360 sra = sysfs_read(mdfd, fd2devnum(mdfd), (GET_DEVS | GET_STATE));
2cdb6489 361
7c548327
N
362 if (sra->devs) {
363 sprintf(dn, "%d:%d", sra->devs->disk.major,
364 sra->devs->disk.minor);
365 dfd2 = dev_open(dn, O_RDONLY);
366 st2 = dup_super(st);
367 if (st2->ss->load_super(st2, dfd2, NULL) ||
368 st->ss->compare_super(st, st2) != 0) {
369 fprintf(stderr, Name
370 ": metadata mismatch between %s and "
371 "chosen array %s\n",
372 devname, chosen_name);
373 close(mdfd);
374 close(dfd2);
375 return 2;
376 }
8382f19b 377 close(dfd2);
7c548327
N
378 memset(&info2, 0, sizeof(info2));
379 st2->ss->getinfo_super(st2, &info2);
380 st2->ss->free_super(st2);
381 if (info.array.level != info2.array.level ||
382 memcmp(info.uuid, info2.uuid, 16) != 0 ||
383 info.array.raid_disks != info2.array.raid_disks) {
384 fprintf(stderr, Name
385 ": unexpected difference between %s and %s.\n",
386 chosen_name, devname);
387 close(mdfd);
388 return 2;
389 }
8382f19b 390 }
7801ac20
N
391 info2.disk.major = major(stb.st_rdev);
392 info2.disk.minor = minor(stb.st_rdev);
c5afc314
N
393 /* add disk needs to know about containers */
394 if (st->ss->external)
395 sra->array.level = LEVEL_CONTAINER;
7801ac20 396 err = add_disk(mdfd, st2, sra, &info2);
8382f19b
NB
397 if (err < 0 && errno == EBUSY) {
398 /* could be another device present with the same
399 * disk.number. Find and reject any such
400 */
401 find_reject(mdfd, st, sra, info.disk.number,
402 info.events, verbose, chosen_name);
7801ac20 403 err = add_disk(mdfd, st2, sra, &info2);
8382f19b
NB
404 }
405 if (err < 0) {
406 fprintf(stderr, Name ": failed to add %s to %s: %s.\n",
407 devname, chosen_name, strerror(errno));
408 close(mdfd);
409 return 2;
410 }
c5afc314
N
411 info.array.working_disks = 0;
412 for (d = sra->devs; d; d=d->next)
413 info.array.working_disks ++;
414
8382f19b 415 }
8382f19b
NB
416
417 /* 7/ Is there enough devices to possibly start the array? */
418 /* 7a/ if not, finish with success. */
352452c3
N
419 if (info.array.level == LEVEL_CONTAINER) {
420 /* Try to assemble within the container */
ad5bc697 421 map_unlock(&map);
97590376 422 sysfs_uevent(&info, "change");
c5afc314
N
423 if (verbose >= 0)
424 fprintf(stderr, Name
425 ": container %s now has %d devices\n",
426 chosen_name, info.array.working_disks);
a7c6e3fb
N
427 wait_for(chosen_name, mdfd);
428 close(mdfd);
fdb482f9
DW
429 if (runstop < 0)
430 return 0; /* don't try to assemble */
03b7f6c6 431 rv = Incremental(chosen_name, verbose, runstop,
0ac91628 432 NULL, homehost, require_homehost, autof);
03b7f6c6
N
433 if (rv == 1)
434 /* Don't fail the whole -I if a subarray didn't
435 * have enough devices to start yet
436 */
437 rv = 0;
438 return rv;
352452c3 439 }
f8409e54 440 avail = NULL;
8382f19b
NB
441 active_disks = count_active(st, mdfd, &avail, &info);
442 if (enough(info.array.level, info.array.raid_disks,
443 info.array.layout, info.array.state & 1,
fdb482f9
DW
444 avail, active_disks) == 0 ||
445 (runstop < 0 && active_disks < info.array.raid_disks)) {
8382f19b
NB
446 free(avail);
447 if (verbose >= 0)
448 fprintf(stderr, Name
449 ": %s attached to %s, not enough to start (%d).\n",
450 devname, chosen_name, active_disks);
ad5bc697 451 map_unlock(&map);
8382f19b
NB
452 close(mdfd);
453 return 0;
454 }
455 free(avail);
456
457 /* 7b/ if yes, */
458 /* - if number of OK devices match expected, or -R and there */
459 /* are enough, */
460 /* + add any bitmap file */
461 /* + start the array (auto-readonly). */
8382f19b
NB
462
463 if (ioctl(mdfd, GET_ARRAY_INFO, &ainf) == 0) {
464 if (verbose >= 0)
465 fprintf(stderr, Name
466 ": %s attached to %s which is already active.\n",
467 devname, chosen_name);
ad5bc697
N
468 close(mdfd);
469 map_unlock(&map);
8382f19b
NB
470 return 0;
471 }
ad5bc697
N
472
473 map_unlock(&map);
8382f19b 474 if (runstop > 0 || active_disks >= info.array.working_disks) {
7e0f6979 475 struct mdinfo *sra;
8382f19b
NB
476 /* Let's try to start it */
477 if (match && match->bitmap_file) {
478 int bmfd = open(match->bitmap_file, O_RDWR);
479 if (bmfd < 0) {
480 fprintf(stderr, Name
481 ": Could not open bitmap file %s.\n",
482 match->bitmap_file);
483 close(mdfd);
484 return 1;
485 }
486 if (ioctl(mdfd, SET_BITMAP_FILE, bmfd) != 0) {
487 close(bmfd);
488 fprintf(stderr, Name
489 ": Failed to set bitmapfile for %s.\n",
490 chosen_name);
491 close(mdfd);
492 return 1;
493 }
494 close(bmfd);
495 }
197e3eb6 496 sra = sysfs_read(mdfd, fd2devnum(mdfd), 0);
7b403fef 497 if ((sra == NULL || active_disks >= info.array.working_disks)
215bb3f7 498 && trustworthy != FOREIGN)
8382f19b
NB
499 rv = ioctl(mdfd, RUN_ARRAY, NULL);
500 else
501 rv = sysfs_set_str(sra, NULL,
502 "array_state", "read-auto");
503 if (rv == 0) {
504 if (verbose >= 0)
505 fprintf(stderr, Name
506 ": %s attached to %s, which has been started.\n",
507 devname, chosen_name);
508 rv = 0;
a7c6e3fb 509 wait_for(chosen_name, mdfd);
8382f19b
NB
510 } else {
511 fprintf(stderr, Name
512 ": %s attached to %s, but failed to start: %s.\n",
513 devname, chosen_name, strerror(errno));
514 rv = 1;
515 }
516 } else {
517 if (verbose >= 0)
518 fprintf(stderr, Name
519 ": %s attached to %s, not enough to start safely.\n",
520 devname, chosen_name);
521 rv = 0;
522 }
523 close(mdfd);
524 return rv;
525}
526
7e0f6979 527static void find_reject(int mdfd, struct supertype *st, struct mdinfo *sra,
8382f19b
NB
528 int number, __u64 events, int verbose,
529 char *array_name)
530{
3da92f27 531 /* Find a device attached to this array with a disk.number of number
8382f19b
NB
532 * and events less than the passed events, and remove the device.
533 */
06c7f68e 534 struct mdinfo *d;
8382f19b
NB
535 mdu_array_info_t ra;
536
537 if (ioctl(mdfd, GET_ARRAY_INFO, &ra) == 0)
538 return; /* not safe to remove from active arrays
539 * without thinking more */
540
541 for (d = sra->devs; d ; d = d->next) {
542 char dn[10];
543 int dfd;
8382f19b 544 struct mdinfo info;
06c7f68e 545 sprintf(dn, "%d:%d", d->disk.major, d->disk.minor);
8382f19b
NB
546 dfd = dev_open(dn, O_RDONLY);
547 if (dfd < 0)
548 continue;
3da92f27 549 if (st->ss->load_super(st, dfd, NULL)) {
8382f19b
NB
550 close(dfd);
551 continue;
552 }
3da92f27
NB
553 st->ss->getinfo_super(st, &info);
554 st->ss->free_super(st);
8382f19b
NB
555 close(dfd);
556
557 if (info.disk.number != number ||
558 info.events >= events)
559 continue;
560
06c7f68e 561 if (d->disk.raid_disk > -1)
8382f19b
NB
562 sysfs_set_str(sra, d, "slot", "none");
563 if (sysfs_set_str(sra, d, "state", "remove") == 0)
564 if (verbose >= 0)
565 fprintf(stderr, Name
566 ": removing old device %s from %s\n",
06c7f68e 567 d->sys_name+4, array_name);
8382f19b
NB
568 }
569}
570
571static int count_active(struct supertype *st, int mdfd, char **availp,
572 struct mdinfo *bestinfo)
573{
574 /* count how many devices in sra think they are active */
06c7f68e 575 struct mdinfo *d;
8382f19b
NB
576 int cnt = 0, cnt1 = 0;
577 __u64 max_events = 0;
7e0f6979 578 struct mdinfo *sra = sysfs_read(mdfd, -1, GET_DEVS | GET_STATE);
8382f19b
NB
579 char *avail = NULL;
580
581 for (d = sra->devs ; d ; d = d->next) {
582 char dn[30];
583 int dfd;
8382f19b
NB
584 int ok;
585 struct mdinfo info;
586
06c7f68e 587 sprintf(dn, "%d:%d", d->disk.major, d->disk.minor);
8382f19b
NB
588 dfd = dev_open(dn, O_RDONLY);
589 if (dfd < 0)
590 continue;
3da92f27 591 ok = st->ss->load_super(st, dfd, NULL);
8382f19b
NB
592 close(dfd);
593 if (ok != 0)
594 continue;
3da92f27 595 st->ss->getinfo_super(st, &info);
43aaf431
DL
596 if (!avail) {
597 avail = malloc(info.array.raid_disks);
598 if (!avail) {
599 fprintf(stderr, Name ": out of memory.\n");
600 exit(1);
601 }
602 memset(avail, 0, info.array.raid_disks);
603 *availp = avail;
604 }
605
8382f19b
NB
606 if (info.disk.state & (1<<MD_DISK_SYNC))
607 {
8382f19b
NB
608 if (cnt == 0) {
609 cnt++;
610 max_events = info.events;
611 avail[info.disk.raid_disk] = 2;
3da92f27 612 st->ss->getinfo_super(st, bestinfo);
8382f19b
NB
613 } else if (info.events == max_events) {
614 cnt++;
615 avail[info.disk.raid_disk] = 2;
616 } else if (info.events == max_events-1) {
617 cnt1++;
618 avail[info.disk.raid_disk] = 1;
619 } else if (info.events < max_events - 1)
620 ;
621 else if (info.events == max_events+1) {
622 int i;
623 cnt1 = cnt;
624 cnt = 1;
625 max_events = info.events;
626 for (i=0; i<info.array.raid_disks; i++)
627 if (avail[i])
628 avail[i]--;
629 avail[info.disk.raid_disk] = 2;
3da92f27 630 st->ss->getinfo_super(st, bestinfo);
8382f19b
NB
631 } else { /* info.events much bigger */
632 cnt = 1; cnt1 = 0;
633 memset(avail, 0, info.disk.raid_disk);
634 max_events = info.events;
3da92f27 635 st->ss->getinfo_super(st, bestinfo);
8382f19b
NB
636 }
637 }
3da92f27 638 st->ss->free_super(st);
8382f19b
NB
639 }
640 return cnt + cnt1;
641}
642
8382f19b
NB
643int IncrementalScan(int verbose)
644{
645 /* look at every device listed in the 'map' file.
646 * If one is found that is not running then:
647 * look in mdadm.conf for bitmap file.
648 * if one exists, but array has none, add it.
649 * try to start array in auto-readonly mode
650 */
651 struct map_ent *mapl = NULL;
652 struct map_ent *me;
653 mddev_ident_t devs, mddev;
654 int rv = 0;
655
656 map_read(&mapl);
657 devs = conf_get_ident(NULL);
658
659 for (me = mapl ; me ; me = me->next) {
8382f19b
NB
660 mdu_array_info_t array;
661 mdu_bitmap_file_t bmf;
7e0f6979 662 struct mdinfo *sra;
215bb3f7
N
663 int mdfd = open_mddev(me->path, 0);
664
8382f19b
NB
665 if (mdfd < 0)
666 continue;
667 if (ioctl(mdfd, GET_ARRAY_INFO, &array) == 0 ||
668 errno != ENODEV) {
669 close(mdfd);
670 continue;
671 }
672 /* Ok, we can try this one. Maybe it needs a bitmap */
673 for (mddev = devs ; mddev ; mddev = mddev->next)
fe056d1f
N
674 if (mddev->devname
675 && strcmp(mddev->devname, me->path) == 0)
8382f19b
NB
676 break;
677 if (mddev && mddev->bitmap_file) {
678 /*
679 * Note: early kernels will wrongly fail this, so it
680 * is a hint only
681 */
682 int added = -1;
683 if (ioctl(mdfd, GET_ARRAY_INFO, &bmf) < 0) {
684 int bmfd = open(mddev->bitmap_file, O_RDWR);
685 if (bmfd >= 0) {
686 added = ioctl(mdfd, SET_BITMAP_FILE,
687 bmfd);
688 close(bmfd);
689 }
690 }
691 if (verbose >= 0) {
692 if (added == 0)
693 fprintf(stderr, Name
694 ": Added bitmap %s to %s\n",
695 mddev->bitmap_file, me->path);
696 else if (errno != EEXIST)
697 fprintf(stderr, Name
698 ": Failed to add bitmap to %s: %s\n",
699 me->path, strerror(errno));
700 }
701 }
702 sra = sysfs_read(mdfd, 0, 0);
703 if (sra) {
704 if (sysfs_set_str(sra, NULL,
705 "array_state", "read-auto") == 0) {
706 if (verbose >= 0)
707 fprintf(stderr, Name
708 ": started array %s\n",
709 me->path);
710 } else {
711 fprintf(stderr, Name
712 ": failed to start array %s: %s\n",
713 me->path, strerror(errno));
714 rv = 1;
715 }
716 }
717 }
718 return rv;
719}
598f0d58 720
1771a6e2
N
721static char *container2devname(char *devname)
722{
723 char *mdname = NULL;
724
725 if (devname[0] == '/') {
726 int fd = open(devname, O_RDONLY);
727 if (fd >= 0) {
728 mdname = devnum2devname(fd2devnum(fd));
729 close(fd);
730 }
731 } else {
732 int uuid[4];
733 struct map_ent *mp, *map = NULL;
734
735 if (!parse_uuid(devname, uuid))
736 return mdname;
737 mp = map_by_uuid(&map, uuid);
738 if (mp)
739 mdname = devnum2devname(mp->devnum);
740 map_free(map);
741 }
742
743 return mdname;
744}
745
598f0d58 746int Incremental_container(struct supertype *st, char *devname, int verbose,
215bb3f7 747 int runstop, int autof, int trustworthy)
598f0d58
NB
748{
749 /* Collect the contents of this container and for each
750 * array, choose a device name and assemble the array.
751 */
752
753 struct mdinfo *list = st->ss->container_content(st);
754 struct mdinfo *ra;
ad5bc697
N
755 struct map_ent *map = NULL;
756
757 map_lock(&map);
598f0d58
NB
758
759 for (ra = list ; ra ; ra = ra->next) {
598f0d58
NB
760 int mdfd;
761 char chosen_name[1024];
ad5bc697 762 struct map_ent *mp;
dbb44303 763 struct mddev_ident_s *match = NULL;
598f0d58 764
c5afc314 765 mp = map_by_uuid(&map, ra->uuid);
d7288ddc 766
30926600
N
767 if (mp) {
768 mdfd = open_dev(mp->devnum);
769 strcpy(chosen_name, mp->path);
770 } else {
dbb44303 771
112cace6 772 /* Check in mdadm.conf for container == devname and
dbb44303
N
773 * member == ra->text_version after second slash.
774 */
775 char *sub = strchr(ra->text_version+1, '/');
776 struct mddev_ident_s *array_list;
777 if (sub) {
778 sub++;
779 array_list = conf_get_ident(NULL);
780 } else
781 array_list = NULL;
782 for(; array_list ; array_list = array_list->next) {
dbb44303
N
783 char *dn;
784 if (array_list->member == NULL ||
785 array_list->container == NULL)
786 continue;
787 if (strcmp(array_list->member, sub) != 0)
788 continue;
71d60c48
DW
789 if (array_list->uuid_set &&
790 !same_uuid(ra->uuid, array_list->uuid, st->ss->swapuuid))
791 continue;
1771a6e2
N
792 dn = container2devname(array_list->container);
793 if (dn == NULL)
dbb44303 794 continue;
dbb44303
N
795 if (strncmp(dn, ra->text_version+1,
796 strlen(dn)) != 0 ||
797 ra->text_version[strlen(dn)+1] != '/') {
798 free(dn);
799 continue;
800 }
801 free(dn);
802 /* we have a match */
803 match = array_list;
71d60c48
DW
804 if (verbose>0)
805 fprintf(stderr, Name ": match found for member %s\n",
806 array_list->member);
dbb44303
N
807 break;
808 }
dbb44303 809
112cace6
N
810 if (match && match->devname &&
811 strcasecmp(match->devname, "<ignore>") == 0) {
812 if (verbose > 0)
813 fprintf(stderr, Name ": array %s/%s is "
814 "explicitly ignored by mdadm.conf\n",
815 match->container, match->member);
816 return 2;
817 }
05833051
N
818 if (match)
819 trustworthy = LOCAL;
112cace6 820
30926600
N
821 mdfd = create_mddev(match ? match->devname : NULL,
822 ra->name,
823 autof,
824 trustworthy,
825 chosen_name);
826 }
598f0d58
NB
827
828 if (mdfd < 0) {
829 fprintf(stderr, Name ": failed to open %s: %s.\n",
830 chosen_name, strerror(errno));
831 return 2;
832 }
833
03b7f6c6
N
834 assemble_container_content(st, mdfd, ra, runstop,
835 chosen_name, verbose);
598f0d58 836 }
ad5bc697 837 map_unlock(&map);
598f0d58
NB
838 return 0;
839}