]> git.ipfire.org Git - thirdparty/mdadm.git/blame - Incremental.c
Incremental: adjust to the new naming scheme.
[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;
84 int devnum;
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;
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) {
157 if (verbose >= 2)
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) {
165 if (verbose >= 2)
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)) {
173 if (verbose >= 2)
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) {
181 if (verbose >= 2)
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) {
191 if (verbose >= 2)
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) {
200 if (verbose >= 0)
201 fprintf(stderr, Name
202 ": we match both %s and %s - cannot decide which to use.\n",
203 match->devname, array_list->devname);
204 return 2;
205 }
206 match = array_list;
207 }
208
7b403fef
N
209 /* 3a/ if not, check for homehost match. If no match, continue
210 * but don't trust the 'name' in the array. Thus a 'random' minor
211 * number will be assigned, and the device name will be based
212 * on that. */
215bb3f7
N
213 if (match)
214 trustworthy = LOCAL;
215 else if (homehost == NULL ||
216 st->ss->match_home(st, homehost) != 1)
217 trustworthy = FOREIGN;
218 name_to_use = strchr(info.name, ':');
219 if (name_to_use)
220 name_to_use++;
221 else
d7288ddc
N
222 name_to_use = info.name;
223
4ef2f11e
N
224 /* There are three possible sources for 'autof': command line,
225 * ARRAY line in mdadm.conf, or CREATE line in mdadm.conf.
350ac35d
N
226 * ARRAY takes precedence, then command line, then
227 * CREATE.
4ef2f11e 228 */
350ac35d 229 if (match && match->autof)
4ef2f11e
N
230 autof = match->autof;
231 if (autof == 0)
232 autof = ci->autof;
233
215bb3f7
N
234 if (st->ss->container_content && st->loaded_container) {
235 /* This is a pre-built container array, so we do something
236 * rather different.
237 */
238 return Incremental_container(st, devname, verbose, runstop,
239 autof, trustworthy);
8382f19b 240 }
215bb3f7
N
241 /* 4/ Check is array exists.
242 */
243 mp = map_by_uuid(&map, info.uuid);
244 if (mp)
245 mdfd = open_mddev(mp->path, 0);
246 else
247 mdfd = -1;
d7288ddc 248
8382f19b 249 if (mdfd < 0) {
7e0f6979 250 struct mdinfo *sra;
c5afc314 251 struct mdinfo dinfo;
8382f19b 252
215bb3f7
N
253 /* Couldn't find an existing array, maybe make a new one */
254 mdfd = create_mddev(mp ? mp->path : match ? match->devname : NULL,
255 info.name, autof, trustworthy, chosen_name);
256
257
258 if (mdfd < 0)
259 return 1;
260
261 sysfs_init(&info, mdfd, 0);
262
f35f2525
N
263 if (set_array_info(mdfd, st, &info) != 0) {
264 fprintf(stderr, Name ": failed to set array info for %s: %s\n",
8382f19b
NB
265 chosen_name, strerror(errno));
266 close(mdfd);
267 return 2;
268 }
7801ac20 269
c5afc314
N
270 dinfo = info;
271 dinfo.disk.major = major(stb.st_rdev);
272 dinfo.disk.minor = minor(stb.st_rdev);
273 if (add_disk(mdfd, st, &info, &dinfo) != 0) {
8382f19b
NB
274 fprintf(stderr, Name ": failed to add %s to %s: %s.\n",
275 devname, chosen_name, strerror(errno));
276 ioctl(mdfd, STOP_ARRAY, 0);
277 close(mdfd);
278 return 2;
279 }
280 sra = sysfs_read(mdfd, devnum, GET_DEVS);
06c7f68e 281 if (!sra || !sra->devs || sra->devs->disk.raid_disk >= 0) {
8382f19b
NB
282 /* It really should be 'none' - must be old buggy
283 * kernel, and mdadm -I may not be able to complete.
284 * So reject it.
285 */
286 ioctl(mdfd, STOP_ARRAY, NULL);
287 fprintf(stderr, Name
288 ": You have an old buggy kernel which cannot support\n"
289 " --incremental reliably. Aborting.\n");
290 close(mdfd);
291 sysfs_free(sra);
292 return 2;
293 }
c5afc314 294 info.array.working_disks = 1;
f35f2525 295 sysfs_free(sra);
8382f19b
NB
296 } else {
297 /* 5b/ if it does */
298 /* - check one drive in array to make sure metadata is a reasonably */
299 /* close match. Reject if not (e.g. different type) */
300 /* - add the device */
301 char dn[20];
302 int dfd2;
8382f19b 303 int err;
7e0f6979 304 struct mdinfo *sra;
3da92f27 305 struct supertype *st2;
c5afc314 306 struct mdinfo info2, *d;
215bb3f7
N
307
308 strcpy(chosen_name, mp->path);
309
b8ac1967 310 sra = sysfs_read(mdfd, devnum, (GET_DEVS | GET_STATE));
2cdb6489 311
06c7f68e
NB
312 sprintf(dn, "%d:%d", sra->devs->disk.major,
313 sra->devs->disk.minor);
8382f19b 314 dfd2 = dev_open(dn, O_RDONLY);
3da92f27 315 st2 = dup_super(st);
b8ac1967
NB
316 if (st2->ss->load_super(st2, dfd2, NULL) ||
317 st->ss->compare_super(st, st2) != 0) {
8382f19b 318 fprintf(stderr, Name
b8ac1967
NB
319 ": metadata mismatch between %s and "
320 "chosen array %s\n",
321 devname, chosen_name);
8382f19b
NB
322 close(mdfd);
323 close(dfd2);
324 return 2;
325 }
326 close(dfd2);
f35f2525 327 memset(&info2, 0, sizeof(info2));
3da92f27
NB
328 st2->ss->getinfo_super(st2, &info2);
329 st2->ss->free_super(st2);
8382f19b
NB
330 if (info.array.level != info2.array.level ||
331 memcmp(info.uuid, info2.uuid, 16) != 0 ||
332 info.array.raid_disks != info2.array.raid_disks) {
333 fprintf(stderr, Name
334 ": unexpected difference between %s and %s.\n",
335 chosen_name, devname);
336 close(mdfd);
337 return 2;
338 }
7801ac20
N
339 info2.disk.major = major(stb.st_rdev);
340 info2.disk.minor = minor(stb.st_rdev);
c5afc314
N
341 /* add disk needs to know about containers */
342 if (st->ss->external)
343 sra->array.level = LEVEL_CONTAINER;
7801ac20 344 err = add_disk(mdfd, st2, sra, &info2);
8382f19b
NB
345 if (err < 0 && errno == EBUSY) {
346 /* could be another device present with the same
347 * disk.number. Find and reject any such
348 */
349 find_reject(mdfd, st, sra, info.disk.number,
350 info.events, verbose, chosen_name);
7801ac20 351 err = add_disk(mdfd, st2, sra, &info2);
8382f19b
NB
352 }
353 if (err < 0) {
354 fprintf(stderr, Name ": failed to add %s to %s: %s.\n",
355 devname, chosen_name, strerror(errno));
356 close(mdfd);
357 return 2;
358 }
c5afc314
N
359 info.array.working_disks = 0;
360 for (d = sra->devs; d; d=d->next)
361 info.array.working_disks ++;
362
8382f19b
NB
363 }
364 /* 6/ Make sure /var/run/mdadm.map contains this array. */
215bb3f7 365 map_update(&map, fd2devnum(mdfd),
1522c538 366 info.text_version,
8382f19b
NB
367 info.uuid, chosen_name);
368
369 /* 7/ Is there enough devices to possibly start the array? */
370 /* 7a/ if not, finish with success. */
352452c3
N
371 if (info.array.level == LEVEL_CONTAINER) {
372 /* Try to assemble within the container */
c5afc314
N
373 close(mdfd);
374 if (verbose >= 0)
375 fprintf(stderr, Name
376 ": container %s now has %d devices\n",
377 chosen_name, info.array.working_disks);
352452c3
N
378 return Incremental(chosen_name, verbose, runstop,
379 NULL, homehost, autof);
380 }
f8409e54 381 avail = NULL;
8382f19b
NB
382 active_disks = count_active(st, mdfd, &avail, &info);
383 if (enough(info.array.level, info.array.raid_disks,
384 info.array.layout, info.array.state & 1,
385 avail, active_disks) == 0) {
386 free(avail);
387 if (verbose >= 0)
388 fprintf(stderr, Name
389 ": %s attached to %s, not enough to start (%d).\n",
390 devname, chosen_name, active_disks);
391 close(mdfd);
392 return 0;
393 }
394 free(avail);
395
396 /* 7b/ if yes, */
397 /* - if number of OK devices match expected, or -R and there */
398 /* are enough, */
399 /* + add any bitmap file */
400 /* + start the array (auto-readonly). */
401{
402 mdu_array_info_t ainf;
403
404 if (ioctl(mdfd, GET_ARRAY_INFO, &ainf) == 0) {
405 if (verbose >= 0)
406 fprintf(stderr, Name
407 ": %s attached to %s which is already active.\n",
408 devname, chosen_name);
409 close (mdfd);
410 return 0;
411 }
412}
413 if (runstop > 0 || active_disks >= info.array.working_disks) {
7e0f6979 414 struct mdinfo *sra;
8382f19b
NB
415 /* Let's try to start it */
416 if (match && match->bitmap_file) {
417 int bmfd = open(match->bitmap_file, O_RDWR);
418 if (bmfd < 0) {
419 fprintf(stderr, Name
420 ": Could not open bitmap file %s.\n",
421 match->bitmap_file);
422 close(mdfd);
423 return 1;
424 }
425 if (ioctl(mdfd, SET_BITMAP_FILE, bmfd) != 0) {
426 close(bmfd);
427 fprintf(stderr, Name
428 ": Failed to set bitmapfile for %s.\n",
429 chosen_name);
430 close(mdfd);
431 return 1;
432 }
433 close(bmfd);
434 }
435 sra = sysfs_read(mdfd, devnum, 0);
7b403fef 436 if ((sra == NULL || active_disks >= info.array.working_disks)
215bb3f7 437 && trustworthy != FOREIGN)
8382f19b
NB
438 rv = ioctl(mdfd, RUN_ARRAY, NULL);
439 else
440 rv = sysfs_set_str(sra, NULL,
441 "array_state", "read-auto");
442 if (rv == 0) {
443 if (verbose >= 0)
444 fprintf(stderr, Name
445 ": %s attached to %s, which has been started.\n",
446 devname, chosen_name);
447 rv = 0;
448 } else {
449 fprintf(stderr, Name
450 ": %s attached to %s, but failed to start: %s.\n",
451 devname, chosen_name, strerror(errno));
452 rv = 1;
453 }
454 } else {
455 if (verbose >= 0)
456 fprintf(stderr, Name
457 ": %s attached to %s, not enough to start safely.\n",
458 devname, chosen_name);
459 rv = 0;
460 }
461 close(mdfd);
462 return rv;
463}
464
7e0f6979 465static void find_reject(int mdfd, struct supertype *st, struct mdinfo *sra,
8382f19b
NB
466 int number, __u64 events, int verbose,
467 char *array_name)
468{
3da92f27 469 /* Find a device attached to this array with a disk.number of number
8382f19b
NB
470 * and events less than the passed events, and remove the device.
471 */
06c7f68e 472 struct mdinfo *d;
8382f19b
NB
473 mdu_array_info_t ra;
474
475 if (ioctl(mdfd, GET_ARRAY_INFO, &ra) == 0)
476 return; /* not safe to remove from active arrays
477 * without thinking more */
478
479 for (d = sra->devs; d ; d = d->next) {
480 char dn[10];
481 int dfd;
8382f19b 482 struct mdinfo info;
06c7f68e 483 sprintf(dn, "%d:%d", d->disk.major, d->disk.minor);
8382f19b
NB
484 dfd = dev_open(dn, O_RDONLY);
485 if (dfd < 0)
486 continue;
3da92f27 487 if (st->ss->load_super(st, dfd, NULL)) {
8382f19b
NB
488 close(dfd);
489 continue;
490 }
3da92f27
NB
491 st->ss->getinfo_super(st, &info);
492 st->ss->free_super(st);
8382f19b
NB
493 close(dfd);
494
495 if (info.disk.number != number ||
496 info.events >= events)
497 continue;
498
06c7f68e 499 if (d->disk.raid_disk > -1)
8382f19b
NB
500 sysfs_set_str(sra, d, "slot", "none");
501 if (sysfs_set_str(sra, d, "state", "remove") == 0)
502 if (verbose >= 0)
503 fprintf(stderr, Name
504 ": removing old device %s from %s\n",
06c7f68e 505 d->sys_name+4, array_name);
8382f19b
NB
506 }
507}
508
509static int count_active(struct supertype *st, int mdfd, char **availp,
510 struct mdinfo *bestinfo)
511{
512 /* count how many devices in sra think they are active */
06c7f68e 513 struct mdinfo *d;
8382f19b
NB
514 int cnt = 0, cnt1 = 0;
515 __u64 max_events = 0;
7e0f6979 516 struct mdinfo *sra = sysfs_read(mdfd, -1, GET_DEVS | GET_STATE);
8382f19b
NB
517 char *avail = NULL;
518
519 for (d = sra->devs ; d ; d = d->next) {
520 char dn[30];
521 int dfd;
8382f19b
NB
522 int ok;
523 struct mdinfo info;
524
06c7f68e 525 sprintf(dn, "%d:%d", d->disk.major, d->disk.minor);
8382f19b
NB
526 dfd = dev_open(dn, O_RDONLY);
527 if (dfd < 0)
528 continue;
3da92f27 529 ok = st->ss->load_super(st, dfd, NULL);
8382f19b
NB
530 close(dfd);
531 if (ok != 0)
532 continue;
3da92f27 533 st->ss->getinfo_super(st, &info);
43aaf431
DL
534 if (!avail) {
535 avail = malloc(info.array.raid_disks);
536 if (!avail) {
537 fprintf(stderr, Name ": out of memory.\n");
538 exit(1);
539 }
540 memset(avail, 0, info.array.raid_disks);
541 *availp = avail;
542 }
543
8382f19b
NB
544 if (info.disk.state & (1<<MD_DISK_SYNC))
545 {
8382f19b
NB
546 if (cnt == 0) {
547 cnt++;
548 max_events = info.events;
549 avail[info.disk.raid_disk] = 2;
3da92f27 550 st->ss->getinfo_super(st, bestinfo);
8382f19b
NB
551 } else if (info.events == max_events) {
552 cnt++;
553 avail[info.disk.raid_disk] = 2;
554 } else if (info.events == max_events-1) {
555 cnt1++;
556 avail[info.disk.raid_disk] = 1;
557 } else if (info.events < max_events - 1)
558 ;
559 else if (info.events == max_events+1) {
560 int i;
561 cnt1 = cnt;
562 cnt = 1;
563 max_events = info.events;
564 for (i=0; i<info.array.raid_disks; i++)
565 if (avail[i])
566 avail[i]--;
567 avail[info.disk.raid_disk] = 2;
3da92f27 568 st->ss->getinfo_super(st, bestinfo);
8382f19b
NB
569 } else { /* info.events much bigger */
570 cnt = 1; cnt1 = 0;
571 memset(avail, 0, info.disk.raid_disk);
572 max_events = info.events;
3da92f27 573 st->ss->getinfo_super(st, bestinfo);
8382f19b
NB
574 }
575 }
3da92f27 576 st->ss->free_super(st);
8382f19b
NB
577 }
578 return cnt + cnt1;
579}
580
581void RebuildMap(void)
582{
583 struct mdstat_ent *mdstat = mdstat_read(0, 0);
584 struct mdstat_ent *md;
585 struct map_ent *map = NULL;
586 int mdp = get_mdp_major();
587
588 for (md = mdstat ; md ; md = md->next) {
7e0f6979 589 struct mdinfo *sra = sysfs_read(-1, md->devnum, GET_DEVS);
06c7f68e 590 struct mdinfo *sd;
8382f19b
NB
591
592 for (sd = sra->devs ; sd ; sd = sd->next) {
593 char dn[30];
594 int dfd;
595 int ok;
596 struct supertype *st;
597 char *path;
8382f19b
NB
598 struct mdinfo info;
599
06c7f68e 600 sprintf(dn, "%d:%d", sd->disk.major, sd->disk.minor);
8382f19b
NB
601 dfd = dev_open(dn, O_RDONLY);
602 if (dfd < 0)
603 continue;
604 st = guess_super(dfd);
605 if ( st == NULL)
606 ok = -1;
607 else
3da92f27 608 ok = st->ss->load_super(st, dfd, NULL);
8382f19b
NB
609 close(dfd);
610 if (ok != 0)
611 continue;
3da92f27 612 st->ss->getinfo_super(st, &info);
8382f19b
NB
613 if (md->devnum > 0)
614 path = map_dev(MD_MAJOR, md->devnum, 0);
615 else
616 path = map_dev(mdp, (-1-md->devnum)<< 6, 0);
1522c538
NB
617 map_add(&map, md->devnum,
618 info.text_version,
8382f19b 619 info.uuid, path ? : "/unknown");
3da92f27 620 st->ss->free_super(st);
8382f19b
NB
621 break;
622 }
623 }
624 map_write(map);
625 map_free(map);
626}
627
628int IncrementalScan(int verbose)
629{
630 /* look at every device listed in the 'map' file.
631 * If one is found that is not running then:
632 * look in mdadm.conf for bitmap file.
633 * if one exists, but array has none, add it.
634 * try to start array in auto-readonly mode
635 */
636 struct map_ent *mapl = NULL;
637 struct map_ent *me;
638 mddev_ident_t devs, mddev;
639 int rv = 0;
640
641 map_read(&mapl);
642 devs = conf_get_ident(NULL);
643
644 for (me = mapl ; me ; me = me->next) {
8382f19b
NB
645 mdu_array_info_t array;
646 mdu_bitmap_file_t bmf;
7e0f6979 647 struct mdinfo *sra;
215bb3f7
N
648 int mdfd = open_mddev(me->path, 0);
649
8382f19b
NB
650 if (mdfd < 0)
651 continue;
652 if (ioctl(mdfd, GET_ARRAY_INFO, &array) == 0 ||
653 errno != ENODEV) {
654 close(mdfd);
655 continue;
656 }
657 /* Ok, we can try this one. Maybe it needs a bitmap */
658 for (mddev = devs ; mddev ; mddev = mddev->next)
659 if (strcmp(mddev->devname, me->path) == 0)
660 break;
661 if (mddev && mddev->bitmap_file) {
662 /*
663 * Note: early kernels will wrongly fail this, so it
664 * is a hint only
665 */
666 int added = -1;
667 if (ioctl(mdfd, GET_ARRAY_INFO, &bmf) < 0) {
668 int bmfd = open(mddev->bitmap_file, O_RDWR);
669 if (bmfd >= 0) {
670 added = ioctl(mdfd, SET_BITMAP_FILE,
671 bmfd);
672 close(bmfd);
673 }
674 }
675 if (verbose >= 0) {
676 if (added == 0)
677 fprintf(stderr, Name
678 ": Added bitmap %s to %s\n",
679 mddev->bitmap_file, me->path);
680 else if (errno != EEXIST)
681 fprintf(stderr, Name
682 ": Failed to add bitmap to %s: %s\n",
683 me->path, strerror(errno));
684 }
685 }
686 sra = sysfs_read(mdfd, 0, 0);
687 if (sra) {
688 if (sysfs_set_str(sra, NULL,
689 "array_state", "read-auto") == 0) {
690 if (verbose >= 0)
691 fprintf(stderr, Name
692 ": started array %s\n",
693 me->path);
694 } else {
695 fprintf(stderr, Name
696 ": failed to start array %s: %s\n",
697 me->path, strerror(errno));
698 rv = 1;
699 }
700 }
701 }
702 return rv;
703}
598f0d58
NB
704
705int Incremental_container(struct supertype *st, char *devname, int verbose,
215bb3f7 706 int runstop, int autof, int trustworthy)
598f0d58
NB
707{
708 /* Collect the contents of this container and for each
709 * array, choose a device name and assemble the array.
710 */
711
712 struct mdinfo *list = st->ss->container_content(st);
713 struct mdinfo *ra;
714
715 for (ra = list ; ra ; ra = ra->next) {
04c0634e 716 struct mdinfo *dev, *sra;
598f0d58
NB
717 int mdfd;
718 char chosen_name[1024];
c5afc314
N
719 int working = 0, preexist = 0;
720 struct map_ent *mp, *map = NULL;
dbb44303 721 struct mddev_ident_s *match = NULL;
598f0d58 722
c5afc314 723 mp = map_by_uuid(&map, ra->uuid);
d7288ddc 724
dbb44303
N
725 if (!mp) {
726
727 /* Check in mdadm.conf for devices == devname and
728 * member == ra->text_version after second slash.
729 */
730 char *sub = strchr(ra->text_version+1, '/');
731 struct mddev_ident_s *array_list;
732 if (sub) {
733 sub++;
734 array_list = conf_get_ident(NULL);
735 } else
736 array_list = NULL;
737 for(; array_list ; array_list = array_list->next) {
738 int fd;
739 char *dn;
740 if (array_list->member == NULL ||
741 array_list->container == NULL)
742 continue;
743 if (strcmp(array_list->member, sub) != 0)
744 continue;
71d60c48
DW
745 if (array_list->uuid_set &&
746 !same_uuid(ra->uuid, array_list->uuid, st->ss->swapuuid))
747 continue;
dbb44303
N
748 fd = open(array_list->container, O_RDONLY);
749 if (fd < 0)
750 continue;
751 dn = devnum2devname(fd2devnum(fd));
752 close(fd);
753 if (strncmp(dn, ra->text_version+1,
754 strlen(dn)) != 0 ||
755 ra->text_version[strlen(dn)+1] != '/') {
756 free(dn);
757 continue;
758 }
759 free(dn);
760 /* we have a match */
761 match = array_list;
71d60c48
DW
762 if (verbose>0)
763 fprintf(stderr, Name ": match found for member %s\n",
764 array_list->member);
dbb44303
N
765 break;
766 }
767 }
768
215bb3f7
N
769 mdfd = create_mddev(match ? match->devname : NULL,
770 ra->name,
771 autof,
772 trustworthy,
773 chosen_name);
598f0d58
NB
774
775 if (mdfd < 0) {
776 fprintf(stderr, Name ": failed to open %s: %s.\n",
777 chosen_name, strerror(errno));
778 return 2;
779 }
780
04c0634e 781
f35f2525 782 sysfs_init(ra, mdfd, 0);
04c0634e
N
783
784 sra = sysfs_read(mdfd, 0, GET_VERSION);
785 if (sra == NULL || strcmp(sra->text_version, ra->text_version) != 0)
786 if (sysfs_set_array(ra, md_get_version(mdfd)) != 0)
787 return 1;
788 if (sra)
789 sysfs_free(sra);
790
2318b9f0 791 for (dev = ra->devs; dev; dev = dev->next)
f35f2525 792 if (sysfs_add_disk(ra, dev) == 0)
598f0d58 793 working++;
c5afc314
N
794 else if (errno == EEXIST)
795 preexist++;
796 if (working == 0)
797 /* Nothing new, don't try to start */ ;
798 else if (runstop > 0 ||
799 (working + preexist) >= ra->array.working_disks) {
598f0d58
NB
800 switch(ra->array.level) {
801 case LEVEL_LINEAR:
802 case LEVEL_MULTIPATH:
803 case 0:
f35f2525 804 sysfs_set_str(ra, NULL, "array_state",
598f0d58
NB
805 "active");
806 break;
807 default:
f35f2525 808 sysfs_set_str(ra, NULL, "array_state",
598f0d58 809 "readonly");
75aa18b5 810 /* start mdmon if needed. */
8850ee3e
N
811 if (!mdmon_running(st->container_dev))
812 start_mdmon(st->container_dev);
813 ping_monitor(devnum2devname(st->container_dev));
598f0d58
NB
814 break;
815 }
f35f2525 816 sysfs_set_safemode(ra, ra->safe_mode_delay);
c5afc314
N
817 if (verbose >= 0) {
818 fprintf(stderr, Name
d7288ddc 819 ": Started %s with %d devices",
c5afc314
N
820 chosen_name, working + preexist);
821 if (preexist)
822 fprintf(stderr, " (%d new)", working);
823 fprintf(stderr, "\n");
824 }
75aa18b5 825 /* FIXME should have an O_EXCL and wait for read-auto */
598f0d58
NB
826 } else
827 if (verbose >= 0)
c5afc314 828 fprintf(stderr, Name
d7288ddc 829 ": %s assembled with %d devices but "
c5afc314
N
830 "not started\n",
831 chosen_name, working);
215bb3f7 832 map_update(&map, fd2devnum(mdfd),
c5afc314
N
833 ra->text_version,
834 ra->uuid, chosen_name);
215bb3f7 835 close(mdfd);
598f0d58
NB
836 }
837 return 0;
838}