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