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