]> git.ipfire.org Git - thirdparty/mdadm.git/blame - Assemble.c
Fix compare_super to take supertype instead of a superblock.
[thirdparty/mdadm.git] / Assemble.c
CommitLineData
64c4757e 1/*
9a9dab36 2 * mdadm - manage Linux "md" devices aka RAID arrays.
64c4757e 3 *
4f589ad0 4 * Copyright (C) 2001-2006 Neil Brown <neilb@suse.de>
64c4757e
NB
5 *
6 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20 *
21 * Author: Neil Brown
22 * Email: <neilb@cse.unsw.edu.au>
23 * Paper: Neil Brown
24 * School of Computer Science and Engineering
25 * The University of New South Wales
26 * Sydney, 2052
27 * Australia
28 */
29
9a9dab36 30#include "mdadm.h"
41a3b72a 31#include <ctype.h>
64c4757e 32
624920bb
NB
33static int name_matches(char *found, char *required, char *homehost)
34{
35 /* See if the name found matches the required name, possibly
36 * prefixed with 'homehost'
37 */
38 char fnd[33];
39
40 strncpy(fnd, found, 32);
41 fnd[32] = 0;
42 if (strcmp(found, required)==0)
43 return 1;
44 if (homehost) {
45 int l = strlen(homehost);
46 if (l < 32 && fnd[l] == ':' &&
47 strcmp(fnd+l+1, required)==0)
48 return 1;
49 }
50 return 0;
51}
52
82d9eba6 53int Assemble(struct supertype *st, char *mddev, int mdfd,
8aec876d 54 mddev_ident_t ident,
06b0d786 55 mddev_dev_t devlist, char *backup_file,
64c4757e 56 int readonly, int runstop,
e5eac01f 57 char *update, char *homehost,
64c4757e
NB
58 int verbose, int force)
59{
60 /*
52826846
NB
61 * The task of Assemble is to find a collection of
62 * devices that should (according to their superblocks)
63 * form an array, and to give this collection to the MD driver.
64 * In Linux-2.4 and later, this involves submitting a
64c4757e
NB
65 * SET_ARRAY_INFO ioctl with no arg - to prepare
66 * the array - and then submit a number of
67 * ADD_NEW_DISK ioctls to add disks into
68 * the array. Finally RUN_ARRAY might
69 * be submitted to start the array.
70 *
71 * Much of the work of Assemble is in finding and/or
72 * checking the disks to make sure they look right.
73 *
4b1ac34b 74 * If mddev is not set, then scan must be set and we
64c4757e
NB
75 * read through the config file for dev+uuid mapping
76 * We recurse, setting mddev, for each device that
77 * - isn't running
4b1ac34b 78 * - has a valid uuid (or any uuid if !uuidset)
64c4757e
NB
79 *
80 * If mddev is set, we try to determine state of md.
81 * check version - must be at least 0.90.0
82 * check kernel version. must be at least 2.4.
83 * If not, we can possibly fall back on START_ARRAY
84 * Try to GET_ARRAY_INFO.
85 * If possible, give up
86 * If not, try to STOP_ARRAY just to make sure
87 *
88 * If !uuidset and scan, look in conf-file for uuid
89 * If not found, give up
b8a8ccf9 90 * If !devlist and scan and uuidset, get list of devs from conf-file
64c4757e
NB
91 *
92 * For each device:
93 * Check superblock - discard if bad
94 * Check uuid (set if we don't have one) - discard if no match
5787fa49 95 * Check superblock similarity if we have a superblock - discard if different
4b1ac34b 96 * Record events, devicenum
64c4757e 97 * This should give us a list of devices for the array
4b1ac34b 98 * We should collect the most recent event number
64c4757e
NB
99 *
100 * Count disks with recent enough event count
101 * While force && !enough disks
102 * Choose newest rejected disks, update event count
103 * mark clean and rewrite superblock
104 * If recent kernel:
105 * SET_ARRAY_INFO
106 * foreach device with recent events : ADD_NEW_DISK
107 * if runstop == 1 || "enough" disks and runstop==0 -> RUN_ARRAY
108 * If old kernel:
109 * Check the device numbers in superblock are right
110 * update superblock if any changes
111 * START_ARRAY
112 *
113 */
583315d9 114 int clean = 0;
d55e3aef 115 int must_close = 0;
64c4757e 116 int old_linux = 0;
41a3b72a 117 int vers = 0; /* Keep gcc quite - it really is initialised */
4b1ac34b 118 void *first_super = NULL, *super = NULL;
64c4757e
NB
119 struct {
120 char *devname;
98c6faba
NB
121 unsigned int major, minor;
122 unsigned int oldmajor, oldminor;
64c4757e 123 long long events;
64c4757e 124 int uptodate;
d013a55e 125 int state;
52826846 126 int raid_disk;
6a41304b 127 int disk_nr;
5787fa49 128 } *devices;
56eedc1a 129 int *best = NULL; /* indexed by raid_disk */
98c6faba
NB
130 unsigned int bestcnt = 0;
131 int devcnt = 0;
132 unsigned int okcnt, sparecnt;
133 unsigned int req_cnt;
134 unsigned int i;
64c4757e 135 int most_recent = 0;
cd29a5c8 136 int chosen_drive;
52826846 137 int change = 0;
cd29a5c8 138 int inargv = 0;
bf4fb153 139 int bitmap_done;
589395d6 140 int start_partial_ok = (runstop >= 0) && (force || devlist==NULL || mdfd < 0);
98c6faba 141 unsigned int num_devs;
5787fa49 142 mddev_dev_t tmpdev;
4b1ac34b 143 struct mdinfo info;
265e0f17 144 char *avail;
308e1801 145 int nextspare = 0;
8a46fe84 146
682c7051 147 if (get_linux_version() < 2004000)
64c4757e
NB
148 old_linux = 1;
149
8a46fe84
NB
150 if (mdfd >= 0) {
151 vers = md_get_version(mdfd);
152 if (vers <= 0) {
153 fprintf(stderr, Name ": %s appears not to be an md device.\n", mddev);
154 return 1;
155 }
156 if (vers < 9000) {
157 fprintf(stderr, Name ": Assemble requires driver version 0.90.0 or later.\n"
158 " Upgrade your kernel or try --build\n");
159 return 1;
160 }
64c4757e 161
8a46fe84
NB
162 if (ioctl(mdfd, GET_ARRAY_INFO, &info.array)>=0) {
163 fprintf(stderr, Name ": device %s already active - cannot assemble it\n",
164 mddev);
165 return 1;
166 }
167 ioctl(mdfd, STOP_ARRAY, NULL); /* just incase it was started but has no content */
168 }
64c4757e 169 /*
52826846
NB
170 * If any subdevs are listed, then any that don't
171 * match ident are discarded. Remainder must all match and
172 * become the array.
173 * If no subdevs, then we scan all devices in the config file, but
174 * there must be something in the identity
64c4757e 175 */
64c4757e 176
cd29a5c8 177 if (!devlist &&
52826846
NB
178 ident->uuid_set == 0 &&
179 ident->super_minor < 0 &&
180 ident->devices == NULL) {
181 fprintf(stderr, Name ": No identity information available for %s - cannot assemble.\n",
8a46fe84 182 mddev ? mddev : "further assembly");
52826846 183 return 1;
64c4757e 184 }
cd29a5c8 185 if (devlist == NULL)
8aec876d 186 devlist = conf_get_devs();
da6b5ca9
NB
187 else if (mdfd >= 0)
188 inargv = 1;
64c4757e 189
60e1bc1a
NB
190 try_again:
191
5787fa49
NB
192 tmpdev = devlist; num_devs = 0;
193 while (tmpdev) {
da6b5ca9
NB
194 if (tmpdev->used)
195 tmpdev->used = 2;
196 else
197 num_devs++;
5787fa49
NB
198 tmpdev = tmpdev->next;
199 }
5787fa49
NB
200 devices = malloc(num_devs * sizeof(*devices));
201
82d9eba6 202 if (!st && ident->st) st = ident->st;
64c4757e 203
dab6685f 204 if (verbose>0)
82b27616 205 fprintf(stderr, Name ": looking for devices for %s\n",
8a46fe84 206 mddev ? mddev : "further assembly");
82b27616 207
811e6cbe
NB
208 /* first walk the list of devices to find a consistent set
209 * that match the criterea, if that is possible.
210 * We flag the one we like with 'used'.
211 */
212 for (tmpdev = devlist;
213 tmpdev;
214 tmpdev = tmpdev->next) {
215 char *devname = tmpdev->devname;
64c4757e
NB
216 int dfd;
217 struct stat stb;
82d9eba6 218 struct supertype *tst = st;
52826846 219
da6b5ca9
NB
220 if (tmpdev->used > 1) continue;
221
52826846 222 if (ident->devices &&
56eedc1a 223 !match_oneof(ident->devices, devname)) {
6a41304b 224 if ((inargv && verbose>=0) || verbose > 0)
56eedc1a 225 fprintf(stderr, Name ": %s is not one of %s\n", devname, ident->devices);
52826846 226 continue;
56eedc1a 227 }
4b1ac34b 228
8b0dabea 229 dfd = dev_open(devname, O_RDONLY|O_EXCL);
64c4757e 230 if (dfd < 0) {
6a41304b 231 if ((inargv && verbose >= 0) || verbose > 0)
682c7051 232 fprintf(stderr, Name ": cannot open device %s: %s\n",
64c4757e 233 devname, strerror(errno));
da6b5ca9 234 tmpdev->used = 2;
52826846
NB
235 } else if (fstat(dfd, &stb)< 0) {
236 /* Impossible! */
237 fprintf(stderr, Name ": fstat failed for %s: %s\n",
238 devname, strerror(errno));
da6b5ca9 239 tmpdev->used = 2;
cd29a5c8
NB
240 } else if ((stb.st_mode & S_IFMT) != S_IFBLK) {
241 fprintf(stderr, Name ": %s is not a block device.\n",
52826846 242 devname);
da6b5ca9 243 tmpdev->used = 2;
82d9eba6 244 } else if (!tst && (tst = guess_super(dfd)) == NULL) {
6a41304b 245 if ((inargv && verbose >= 0) || verbose > 0)
da6b5ca9
NB
246 fprintf(stderr, Name ": no recogniseable superblock on %s\n",
247 devname);
248 tmpdev->used = 2;
82d9eba6 249 } else if (tst->ss->load_super(tst,dfd, &super, NULL)) {
6a41304b 250 if ((inargv && verbose >= 0) || verbose > 0)
682c7051 251 fprintf( stderr, Name ": no RAID superblock on %s\n",
64c4757e 252 devname);
52826846 253 } else {
68c7d6d7 254 tst->ss->getinfo_super(tst, &info, super);
64c4757e 255 }
c06487ce 256 if (dfd >= 0) close(dfd);
52826846 257
838acbc2 258 if (ident->uuid_set && (!update || strcmp(update, "uuid")!= 0) &&
f277ce36 259 (!super || same_uuid(info.uuid, ident->uuid, tst->ss->swapuuid)==0)) {
6a41304b 260 if ((inargv && verbose >= 0) || verbose > 0)
52826846
NB
261 fprintf(stderr, Name ": %s has wrong uuid.\n",
262 devname);
df37ffc0 263 goto loop;
82b27616 264 }
c4f12c13 265 if (ident->name[0] && (!update || strcmp(update, "name")!= 0) &&
624920bb 266 (!super || name_matches(info.name, ident->name, homehost)==0)) {
6a41304b 267 if ((inargv && verbose >= 0) || verbose > 0)
947fd4dd
NB
268 fprintf(stderr, Name ": %s has wrong name.\n",
269 devname);
df37ffc0 270 goto loop;
947fd4dd 271 }
98c6faba 272 if (ident->super_minor != UnSet &&
4b1ac34b 273 (!super || ident->super_minor != info.array.md_minor)) {
6a41304b 274 if ((inargv && verbose >= 0) || verbose > 0)
52826846 275 fprintf(stderr, Name ": %s has wrong super-minor.\n",
64c4757e 276 devname);
df37ffc0 277 goto loop;
64c4757e 278 }
98c6faba 279 if (ident->level != UnSet &&
4b1ac34b 280 (!super|| ident->level != info.array.level)) {
6a41304b 281 if ((inargv && verbose >= 0) || verbose > 0)
cd29a5c8
NB
282 fprintf(stderr, Name ": %s has wrong raid level.\n",
283 devname);
df37ffc0 284 goto loop;
cd29a5c8 285 }
98c6faba 286 if (ident->raid_disks != UnSet &&
4b1ac34b 287 (!super || ident->raid_disks!= info.array.raid_disks)) {
6a41304b 288 if ((inargv && verbose >= 0) || verbose > 0)
cd29a5c8
NB
289 fprintf(stderr, Name ": %s requires wrong number of drives.\n",
290 devname);
df37ffc0 291 goto loop;
cd29a5c8 292 }
da6b5ca9
NB
293 if (mdfd < 0) {
294 if (tst == NULL || super == NULL)
295 continue;
589395d6 296 if (update == NULL &&
68c7d6d7 297 tst->ss->match_home(tst, super, homehost)==0) {
da6b5ca9
NB
298 if ((inargv && verbose >= 0) || verbose > 0)
299 fprintf(stderr, Name ": %s is not built for host %s.\n",
300 devname, homehost);
301 /* Auto-assemble, and this is not a usable host */
589395d6
NB
302 /* if update != NULL, we are updating the host
303 * name... */
df37ffc0 304 goto loop;
da6b5ca9
NB
305 }
306 }
83b6208e 307 /* If we are this far, then we are nearly commited to this device.
52826846 308 * If the super_block doesn't exist, or doesn't match others,
83b6208e
NB
309 * then we probably cannot continue
310 * However if one of the arrays is for the homehost, and
311 * the other isn't that can disambiguate.
52826846 312 */
52826846 313
4b1ac34b 314 if (!super) {
52826846
NB
315 fprintf(stderr, Name ": %s has no superblock - assembly aborted\n",
316 devname);
68c7d6d7 317 st->ss->free_super(st, first_super);
52826846
NB
318 return 1;
319 }
838acbc2 320
83b6208e
NB
321 if (st == NULL)
322 st = tst;
323 if (st->ss != tst->ss ||
324 st->minor_version != tst->minor_version ||
64557c33 325 st->ss->compare_super(st, tst) != 0) {
83b6208e 326 /* Some mismatch. If exactly one array matches this host,
da6b5ca9
NB
327 * we can resolve on that one.
328 * Or, if we are auto assembling, we just ignore the second
329 * for now.
83b6208e 330 */
da6b5ca9 331 if (mdfd < 0)
df37ffc0 332 goto loop;
83b6208e 333 if (homehost) {
68c7d6d7
NB
334 int first = st->ss->match_home(st, first_super,
335 homehost);
336 int last = tst->ss->match_home(tst, super,
337 homehost);
83b6208e
NB
338 if (first+last == 1) {
339 /* We can do something */
340 if (first) {/* just ignore this one */
341 if ((inargv && verbose >= 0) || verbose > 0)
342 fprintf(stderr, Name ": %s misses out due to wrong homehost\n",
343 devname);
df37ffc0 344 goto loop;
83b6208e
NB
345 } else { /* reject all those sofar */
346 mddev_dev_t td;
347 if ((inargv && verbose >= 0) || verbose > 0)
348 fprintf(stderr, Name ": %s overrides previous devices due to good homehost\n",
349 devname);
350 for (td=devlist; td != tmpdev; td=td->next)
351 if (td->used == 1)
352 td->used = 0;
353 tmpdev->used = 1;
df37ffc0 354 goto loop;
83b6208e
NB
355 }
356 }
357 }
52826846
NB
358 fprintf(stderr, Name ": superblock on %s doesn't match others - assembly aborted\n",
359 devname);
68c7d6d7
NB
360 tst->ss->free_super(tst, super);
361 st->ss->free_super(st, first_super);
52826846 362 return 1;
64c4757e
NB
363 }
364
811e6cbe 365 tmpdev->used = 1;
df37ffc0
NB
366
367 loop:
368 if (super)
68c7d6d7 369 tst->ss->free_super(tst, super);
df37ffc0 370 super = NULL;
811e6cbe
NB
371 }
372
8a46fe84
NB
373 if (mdfd < 0) {
374 /* So... it is up to me to open the device.
375 * We create a name '/dev/md/XXX' based on the info in the
376 * superblock, and call open_mddev on that
377 */
da6b5ca9
NB
378 mdu_array_info_t inf;
379 char *c;
380 if (!first_super) {
d55e3aef 381 return 2;
da6b5ca9 382 }
68c7d6d7 383 st->ss->getinfo_super(st, &info, first_super);
da6b5ca9
NB
384 c = strchr(info.name, ':');
385 if (c) c++; else c= info.name;
41a3b72a
NB
386 if (isdigit(*c) && ((ident->autof & 7)==4 || (ident->autof&7)==6))
387 /* /dev/md/d0 style for partitionable */
388 asprintf(&mddev, "/dev/md/d%s", c);
389 else
390 asprintf(&mddev, "/dev/md/%s", c);
da6b5ca9 391 mdfd = open_mddev(mddev, ident->autof);
60e1bc1a 392 if (mdfd < 0) {
68c7d6d7 393 st->ss->free_super(st, first_super);
60e1bc1a
NB
394 free(devices);
395 first_super = NULL;
396 goto try_again;
397 }
da6b5ca9
NB
398 vers = md_get_version(mdfd);
399 if (ioctl(mdfd, GET_ARRAY_INFO, &inf)==0) {
60e1bc1a
NB
400 for (tmpdev = devlist ;
401 tmpdev && tmpdev->used != 1;
402 tmpdev = tmpdev->next)
403 ;
da6b5ca9 404 fprintf(stderr, Name ": %s already active, cannot restart it!\n", mddev);
60e1bc1a
NB
405 if (tmpdev)
406 fprintf(stderr, Name ": %s needed for %s...\n",
407 mddev, tmpdev->devname);
da6b5ca9 408 close(mdfd);
60e1bc1a 409 mdfd = -1;
68c7d6d7 410 st->ss->free_super(st, first_super);
60e1bc1a
NB
411 free(devices);
412 first_super = NULL;
413 goto try_again;
da6b5ca9 414 }
d55e3aef 415 must_close = 1;
8a46fe84
NB
416 }
417
811e6cbe 418 /* Ok, no bad inconsistancy, we can try updating etc */
bf4fb153 419 bitmap_done = 0;
da6b5ca9 420 for (tmpdev = devlist; tmpdev; tmpdev=tmpdev->next) if (tmpdev->used == 1) {
811e6cbe
NB
421 char *devname = tmpdev->devname;
422 struct stat stb;
5787fa49 423 /* looks like a good enough match to update the super block if needed */
d1f1011b 424#ifndef MDASSEMBLE
5787fa49 425 if (update) {
811e6cbe 426 int dfd;
4b1ac34b
NB
427 /* prepare useful information in info structures */
428 struct stat stb2;
429 fstat(mdfd, &stb2);
7d99579f
NB
430
431 if (strcmp(update, "uuid")==0 &&
432 !ident->uuid_set) {
433 int rfd;
434 if ((rfd = open("/dev/urandom", O_RDONLY)) < 0 ||
435 read(rfd, ident->uuid, 16) != 16) {
436 *(__u32*)(ident->uuid) = random();
437 *(__u32*)(ident->uuid+1) = random();
438 *(__u32*)(ident->uuid+2) = random();
439 *(__u32*)(ident->uuid+3) = random();
440 }
441 if (rfd >= 0) close(rfd);
7d99579f 442 }
811e6cbe
NB
443 dfd = dev_open(devname, O_RDWR|O_EXCL);
444
0430ed48
NB
445 remove_partitions(dfd);
446
811e6cbe 447 st->ss->load_super(st, dfd, &super, NULL);
68c7d6d7 448 st->ss->getinfo_super(st, &info, super);
811e6cbe 449
7d99579f 450 memcpy(info.uuid, ident->uuid, 16);
e5eac01f 451 strcpy(info.name, ident->name);
811e6cbe
NB
452 info.array.md_minor = minor(stb2.st_rdev);
453
68c7d6d7
NB
454 st->ss->update_super(st, &info, super, update,
455 devname, verbose,
e5eac01f
NB
456 ident->uuid_set, homehost);
457 if (strcmp(update, "uuid")==0 &&
458 !ident->uuid_set) {
459 ident->uuid_set = 1;
460 memcpy(ident->uuid, info.uuid, 16);
461 }
b8a8ccf9 462 if (dfd < 0)
5787fa49
NB
463 fprintf(stderr, Name ": Cannot open %s for superblock update\n",
464 devname);
96395475 465 else if (st->ss->store_super(st, dfd, super))
5787fa49
NB
466 fprintf(stderr, Name ": Could not re-write superblock on %s.\n",
467 devname);
468 if (dfd >= 0)
469 close(dfd);
8131b493
NB
470
471 if (strcmp(update, "uuid")==0 &&
bf4fb153
NB
472 ident->bitmap_fd >= 0 && !bitmap_done) {
473 if (bitmap_update_uuid(ident->bitmap_fd, info.uuid, st->ss->swapuuid) != 0)
474 fprintf(stderr, Name ": Could not update uuid on external bitmap.\n");
475 else
476 bitmap_done = 1;
477 }
d1f1011b
NB
478 } else
479#endif
480 {
da6b5ca9
NB
481 int dfd;
482 dfd = dev_open(devname, O_RDWR|O_EXCL);
483
0430ed48
NB
484 remove_partitions(dfd);
485
da6b5ca9 486 st->ss->load_super(st, dfd, &super, NULL);
68c7d6d7 487 st->ss->getinfo_super(st, &info, super);
da6b5ca9 488 close(dfd);
5787fa49
NB
489 }
490
811e6cbe
NB
491 stat(devname, &stb);
492
dab6685f 493 if (verbose > 0)
cd29a5c8 494 fprintf(stderr, Name ": %s is identified as a member of %s, slot %d.\n",
4b1ac34b 495 devname, mddev, info.disk.raid_disk);
64c4757e 496 devices[devcnt].devname = devname;
0df46c2a
NB
497 devices[devcnt].major = major(stb.st_rdev);
498 devices[devcnt].minor = minor(stb.st_rdev);
4b1ac34b
NB
499 devices[devcnt].oldmajor = info.disk.major;
500 devices[devcnt].oldminor = info.disk.minor;
501 devices[devcnt].events = info.events;
502 devices[devcnt].raid_disk = info.disk.raid_disk;
6a41304b 503 devices[devcnt].disk_nr = info.disk.number;
64c4757e 504 devices[devcnt].uptodate = 0;
4b1ac34b 505 devices[devcnt].state = info.disk.state;
64c4757e
NB
506 if (most_recent < devcnt) {
507 if (devices[devcnt].events
508 > devices[most_recent].events)
509 most_recent = devcnt;
510 }
b8a8ccf9 511 if (info.array.level == -4)
5787fa49
NB
512 /* with multipath, the raid_disk from the superblock is meaningless */
513 i = devcnt;
514 else
515 i = devices[devcnt].raid_disk;
308e1801
NB
516 if (i+1 == 0) {
517 if (nextspare < info.array.raid_disks)
518 nextspare = info.array.raid_disks;
519 i = nextspare++;
08110d41
NB
520 } else {
521 if (i >= info.array.raid_disks &&
522 i >= nextspare)
523 nextspare = i+1;
308e1801 524 }
98c6faba 525 if (i < 10000) {
56eedc1a 526 if (i >= bestcnt) {
98c6faba 527 unsigned int newbestcnt = i+10;
56eedc1a 528 int *newbest = malloc(sizeof(int)*newbestcnt);
98c6faba 529 unsigned int c;
56eedc1a
NB
530 for (c=0; c < newbestcnt; c++)
531 if (c < bestcnt)
532 newbest[c] = best[c];
533 else
534 newbest[c] = -1;
535 if (best)free(best);
536 best = newbest;
537 bestcnt = newbestcnt;
538 }
6a255b69
NB
539 if (best[i] >=0 &&
540 devices[best[i]].events == devices[devcnt].events &&
541 devices[best[i]].minor != devices[devcnt].minor &&
542 st->ss->major == 0 &&
543 info.array.level != -4) {
544 /* two different devices with identical superblock.
545 * Could be a mis-detection caused by overlapping
546 * partitions. fail-safe.
547 */
548 fprintf(stderr, Name ": WARNING %s and %s appear"
549 " to have very similar superblocks.\n"
550 " If they are really different, "
551 "please --zero the superblock on one\n"
d2df86e0
NB
552 " If they are the same or overlap,"
553 " please remove one from %s.\n",
554 devices[best[i]].devname, devname,
555 inargv ? "the list" :
556 "the\n DEVICE list in mdadm.conf"
557 );
6a255b69
NB
558 if (must_close) close(mdfd);
559 return 1;
560 }
52826846
NB
561 if (best[i] == -1
562 || devices[best[i]].events < devices[devcnt].events)
563 best[i] = devcnt;
56eedc1a 564 }
64c4757e 565 devcnt++;
64c4757e 566
df37ffc0 567 if (super)
68c7d6d7 568 st->ss->free_super(st, super);
df37ffc0
NB
569 super = NULL;
570 }
4b1ac34b 571
586ed405
NB
572 if (update && strcmp(update, "byteorder")==0)
573 st->minor_version = 90;
574
64c4757e 575 if (devcnt == 0) {
682c7051 576 fprintf(stderr, Name ": no devices found for %s\n",
64c4757e 577 mddev);
68c7d6d7 578 st->ss->free_super(st, first_super);
d55e3aef 579 if (must_close) close(mdfd);
64c4757e
NB
580 return 1;
581 }
4b1ac34b 582
68c7d6d7 583 st->ss->getinfo_super(st, &info, first_super);
583315d9 584 clean = info.array.state & 1;
4b1ac34b 585
64c4757e
NB
586 /* now we have some devices that might be suitable.
587 * I wonder how many
588 */
265e0f17
NB
589 avail = malloc(info.array.raid_disks);
590 memset(avail, 0, info.array.raid_disks);
64c4757e 591 okcnt = 0;
52826846 592 sparecnt=0;
56eedc1a 593 for (i=0; i< bestcnt ;i++) {
64c4757e 594 int j = best[i];
ee04451c
NB
595 int event_margin = 1; /* always allow a difference of '1'
596 * like the kernel does
597 */
64c4757e 598 if (j < 0) continue;
d013a55e
NB
599 /* note: we ignore error flags in multipath arrays
600 * as they don't make sense
601 */
4b1ac34b 602 if (info.array.level != -4)
aa88f531
NB
603 if (!(devices[j].state & (1<<MD_DISK_SYNC))) {
604 if (!(devices[j].state & (1<<MD_DISK_FAULTY)))
605 sparecnt++;
d013a55e 606 continue;
aa88f531 607 }
cd29a5c8 608 if (devices[j].events+event_margin >=
64c4757e
NB
609 devices[most_recent].events) {
610 devices[j].uptodate = 1;
265e0f17 611 if (i < info.array.raid_disks) {
52826846 612 okcnt++;
265e0f17
NB
613 avail[i]=1;
614 } else
52826846 615 sparecnt++;
64c4757e
NB
616 }
617 }
265e0f17 618 while (force && !enough(info.array.level, info.array.raid_disks,
583315d9 619 info.array.layout, 1,
265e0f17 620 avail, okcnt)) {
64c4757e
NB
621 /* Choose the newest best drive which is
622 * not up-to-date, update the superblock
623 * and add it.
624 */
52826846 625 int fd;
c5a6f9a6 626 long long current_events;
cd29a5c8 627 chosen_drive = -1;
4b1ac34b 628 for (i=0; i<info.array.raid_disks && i < bestcnt; i++) {
52826846
NB
629 int j = best[i];
630 if (j>=0 &&
631 !devices[j].uptodate &&
632 devices[j].events > 0 &&
633 (chosen_drive < 0 ||
634 devices[j].events > devices[chosen_drive].events))
635 chosen_drive = j;
636 }
637 if (chosen_drive < 0)
638 break;
c5a6f9a6
NB
639 current_events = devices[chosen_drive].events;
640 add_another:
dab6685f
NB
641 if (verbose >= 0)
642 fprintf(stderr, Name ": forcing event count in %s(%d) from %d upto %d\n",
643 devices[chosen_drive].devname, devices[chosen_drive].raid_disk,
644 (int)(devices[chosen_drive].events),
645 (int)(devices[most_recent].events));
8b0dabea 646 fd = dev_open(devices[chosen_drive].devname, O_RDWR|O_EXCL);
52826846
NB
647 if (fd < 0) {
648 fprintf(stderr, Name ": Couldn't open %s for write - not updating\n",
649 devices[chosen_drive].devname);
650 devices[chosen_drive].events = 0;
651 continue;
652 }
82d9eba6 653 if (st->ss->load_super(st,fd, &super, NULL)) {
52826846
NB
654 close(fd);
655 fprintf(stderr, Name ": RAID superblock disappeared from %s - not updating.\n",
656 devices[chosen_drive].devname);
657 devices[chosen_drive].events = 0;
658 continue;
659 }
4b1ac34b 660 info.events = devices[most_recent].events;
68c7d6d7 661 st->ss->update_super(st, &info, super, "force-one",
67a8c82d
NB
662 devices[chosen_drive].devname, verbose,
663 0, NULL);
4b1ac34b 664
96395475 665 if (st->ss->store_super(st, fd, super)) {
52826846
NB
666 close(fd);
667 fprintf(stderr, Name ": Could not re-write superblock on %s\n",
668 devices[chosen_drive].devname);
669 devices[chosen_drive].events = 0;
68c7d6d7 670 st->ss->free_super(st, super);
52826846
NB
671 continue;
672 }
673 close(fd);
674 devices[chosen_drive].events = devices[most_recent].events;
675 devices[chosen_drive].uptodate = 1;
265e0f17 676 avail[chosen_drive] = 1;
52826846 677 okcnt++;
68c7d6d7 678 st->ss->free_super(st, super);
c5a6f9a6
NB
679
680 /* If there are any other drives of the same vintage,
681 * add them in as well. We can't lose and we might gain
682 */
683 for (i=0; i<info.array.raid_disks && i < bestcnt ; i++) {
684 int j = best[i];
685 if (j >= 0 &&
686 !devices[j].uptodate &&
687 devices[j].events > 0 &&
688 devices[j].events == current_events) {
689 chosen_drive = j;
690 goto add_another;
691 }
692 }
64c4757e 693 }
52826846
NB
694
695 /* Now we want to look at the superblock which the kernel will base things on
696 * and compare the devices that we think are working with the devices that the
697 * superblock thinks are working.
698 * If there are differences and --force is given, then update this chosen
699 * superblock.
700 */
cd29a5c8 701 chosen_drive = -1;
4b1ac34b 702 super = NULL;
56eedc1a 703 for (i=0; chosen_drive < 0 && i<bestcnt; i++) {
52826846
NB
704 int j = best[i];
705 int fd;
4b1ac34b 706
52826846
NB
707 if (j<0)
708 continue;
709 if (!devices[j].uptodate)
710 continue;
711 chosen_drive = j;
8b0dabea 712 if ((fd=dev_open(devices[j].devname, O_RDONLY|O_EXCL))< 0) {
52826846
NB
713 fprintf(stderr, Name ": Cannot open %s: %s\n",
714 devices[j].devname, strerror(errno));
d55e3aef 715 if (must_close) close(mdfd);
52826846
NB
716 return 1;
717 }
82d9eba6 718 if (st->ss->load_super(st,fd, &super, NULL)) {
52826846
NB
719 close(fd);
720 fprintf(stderr, Name ": RAID superblock has disappeared from %s\n",
721 devices[j].devname);
d55e3aef 722 if (must_close) close(mdfd);
52826846
NB
723 return 1;
724 }
725 close(fd);
726 }
4b1ac34b
NB
727 if (super == NULL) {
728 fprintf(stderr, Name ": No suitable drives found for %s\n", mddev);
d55e3aef 729 if (must_close) close(mdfd);
4b1ac34b
NB
730 return 1;
731 }
68c7d6d7 732 st->ss->getinfo_super(st, &info, super);
56eedc1a 733 for (i=0; i<bestcnt; i++) {
52826846 734 int j = best[i];
98c6faba 735 unsigned int desired_state;
11a3e71d 736
4b1ac34b 737 if (i < info.array.raid_disks)
11a3e71d
NB
738 desired_state = (1<<MD_DISK_ACTIVE) | (1<<MD_DISK_SYNC);
739 else
740 desired_state = 0;
741
52826846
NB
742 if (j<0)
743 continue;
744 if (!devices[j].uptodate)
745 continue;
6a41304b 746 info.disk.number = devices[j].disk_nr;
fbf8a0b7 747 info.disk.raid_disk = i;
4b1ac34b
NB
748 info.disk.state = desired_state;
749
52826846 750 if (devices[j].uptodate &&
68c7d6d7
NB
751 st->ss->update_super(st, &info, super, "assemble", NULL,
752 verbose, 0, NULL)) {
52826846 753 if (force) {
dab6685f
NB
754 if (verbose >= 0)
755 fprintf(stderr, Name ": "
756 "clearing FAULTY flag for device %d in %s for %s\n",
757 j, mddev, devices[j].devname);
4b1ac34b 758 change = 1;
52826846 759 } else {
dab6685f
NB
760 if (verbose >= -1)
761 fprintf(stderr, Name ": "
762 "device %d in %s has wrong state in superblock, but %s seems ok\n",
763 i, mddev, devices[j].devname);
52826846
NB
764 }
765 }
4b1ac34b 766#if 0
52826846
NB
767 if (!devices[j].uptodate &&
768 !(super.disks[i].state & (1 << MD_DISK_FAULTY))) {
769 fprintf(stderr, Name ": devices %d of %s is not marked FAULTY in superblock, but cannot be found\n",
770 i, mddev);
771 }
4b1ac34b 772#endif
52826846 773 }
c5a6f9a6
NB
774 if (force && !clean &&
775 !enough(info.array.level, info.array.raid_disks,
776 info.array.layout, clean,
777 avail, okcnt)) {
68c7d6d7 778 change += st->ss->update_super(st, &info, super, "force-array",
67a8c82d
NB
779 devices[chosen_drive].devname, verbose,
780 0, NULL);
583315d9 781 clean = 1;
d013a55e 782 }
52826846 783
4b1ac34b 784 if (change) {
52826846 785 int fd;
8b0dabea 786 fd = dev_open(devices[chosen_drive].devname, O_RDWR|O_EXCL);
52826846 787 if (fd < 0) {
353632d9 788 fprintf(stderr, Name ": Could not open %s for write - cannot Assemble array.\n",
52826846 789 devices[chosen_drive].devname);
d55e3aef 790 if (must_close) close(mdfd);
52826846
NB
791 return 1;
792 }
96395475 793 if (st->ss->store_super(st, fd, super)) {
52826846
NB
794 close(fd);
795 fprintf(stderr, Name ": Could not re-write superblock on %s\n",
796 devices[chosen_drive].devname);
d55e3aef 797 if (must_close) close(mdfd);
52826846
NB
798 return 1;
799 }
800 close(fd);
52826846
NB
801 }
802
353632d9
NB
803 /* If we are in the middle of a reshape we may need to restore saved data
804 * that was moved aside due to the reshape overwriting live data
805 * The code of doing this lives in Grow.c
806 */
39c5a909 807#ifndef MDASSEMBLE
353632d9
NB
808 if (info.reshape_active) {
809 int err = 0;
810 int *fdlist = malloc(sizeof(int)* bestcnt);
811 for (i=0; i<bestcnt; i++) {
812 int j = best[i];
813 if (j >= 0) {
814 fdlist[i] = dev_open(devices[j].devname, O_RDWR|O_EXCL);
815 if (fdlist[i] < 0) {
816 fprintf(stderr, Name ": Could not open %s for write - cannot Assemble array.\n",
817 devices[j].devname);
818 err = 1;
819 break;
820 }
821 } else
822 fdlist[i] = -1;
823 }
824 if (!err)
06b0d786 825 err = Grow_restart(st, &info, fdlist, bestcnt, backup_file);
353632d9
NB
826 while (i>0) {
827 i--;
828 if (fdlist[i]>=0) close(fdlist[i]);
829 }
830 if (err) {
831 fprintf(stderr, Name ": Failed to restore critical section for reshape, sorry.\n");
d55e3aef 832 if (must_close) close(mdfd);
353632d9
NB
833 return err;
834 }
835 }
39c5a909 836#endif
aa88f531
NB
837 /* count number of in-sync devices according to the superblock.
838 * We must have this number to start the array without -s or -R
839 */
4b1ac34b 840 req_cnt = info.array.working_disks;
aa88f531 841
64c4757e
NB
842 /* Almost ready to actually *do* something */
843 if (!old_linux) {
fbf8a0b7
NB
844 int rv;
845 if ((vers % 100) >= 1) { /* can use different versions */
846 mdu_array_info_t inf;
847 memset(&inf, 0, sizeof(inf));
848 inf.major_version = st->ss->major;
849 inf.minor_version = st->minor_version;
850 rv = ioctl(mdfd, SET_ARRAY_INFO, &inf);
b8a8ccf9 851 } else
fbf8a0b7
NB
852 rv = ioctl(mdfd, SET_ARRAY_INFO, NULL);
853
854 if (rv) {
682c7051 855 fprintf(stderr, Name ": SET_ARRAY_INFO failed for %s: %s\n",
64c4757e 856 mddev, strerror(errno));
d55e3aef 857 if (must_close) close(mdfd);
64c4757e
NB
858 return 1;
859 }
11484a63 860 if (ident->bitmap_fd >= 0) {
c82f047c
NB
861 if (ioctl(mdfd, SET_BITMAP_FILE, ident->bitmap_fd) != 0) {
862 fprintf(stderr, Name ": SET_BITMAP_FILE failed.\n");
d55e3aef 863 if (must_close) close(mdfd);
c82f047c
NB
864 return 1;
865 }
7ef02d01
NB
866 } else if (ident->bitmap_file) {
867 /* From config file */
868 int bmfd = open(ident->bitmap_file, O_RDWR);
869 if (bmfd < 0) {
870 fprintf(stderr, Name ": Could not open bitmap file %s\n",
871 ident->bitmap_file);
d55e3aef 872 if (must_close) close(mdfd);
7ef02d01
NB
873 return 1;
874 }
875 if (ioctl(mdfd, SET_BITMAP_FILE, bmfd) != 0) {
876 fprintf(stderr, Name ": Failed to set bitmapfile for %s\n", mddev);
877 close(bmfd);
d55e3aef 878 if (must_close) close(mdfd);
7ef02d01
NB
879 return 1;
880 }
881 close(bmfd);
c82f047c 882 }
7ef02d01 883
52826846 884 /* First, add the raid disks, but add the chosen one last */
56eedc1a 885 for (i=0; i<= bestcnt; i++) {
52826846 886 int j;
56eedc1a 887 if (i < bestcnt) {
52826846
NB
888 j = best[i];
889 if (j == chosen_drive)
890 continue;
891 } else
892 j = chosen_drive;
893
56eedc1a 894 if (j >= 0 /* && devices[j].uptodate */) {
64c4757e
NB
895 mdu_disk_info_t disk;
896 memset(&disk, 0, sizeof(disk));
897 disk.major = devices[j].major;
898 disk.minor = devices[j].minor;
899 if (ioctl(mdfd, ADD_NEW_DISK, &disk)!=0) {
682c7051 900 fprintf(stderr, Name ": failed to add %s to %s: %s\n",
64c4757e
NB
901 devices[j].devname,
902 mddev,
903 strerror(errno));
4b1ac34b 904 if (i < info.array.raid_disks || i == bestcnt)
52826846
NB
905 okcnt--;
906 else
907 sparecnt--;
dab6685f 908 } else if (verbose > 0)
52826846
NB
909 fprintf(stderr, Name ": added %s to %s as %d\n",
910 devices[j].devname, mddev, devices[j].raid_disk);
dab6685f 911 } else if (verbose > 0 && i < info.array.raid_disks)
682c7051 912 fprintf(stderr, Name ": no uptodate device for slot %d of %s\n",
64c4757e
NB
913 i, mddev);
914 }
aba69144 915
64c4757e 916 if (runstop == 1 ||
b8a8ccf9 917 (runstop <= 0 &&
583315d9
NB
918 ( enough(info.array.level, info.array.raid_disks,
919 info.array.layout, clean, avail, okcnt) &&
aa88f531
NB
920 (okcnt >= req_cnt || start_partial_ok)
921 ))) {
82b27616 922 if (ioctl(mdfd, RUN_ARRAY, NULL)==0) {
dab6685f
NB
923 if (verbose >= 0) {
924 fprintf(stderr, Name ": %s has been started with %d drive%s",
925 mddev, okcnt, okcnt==1?"":"s");
b8a8ccf9 926 if (okcnt < info.array.raid_disks)
dab6685f
NB
927 fprintf(stderr, " (out of %d)", info.array.raid_disks);
928 if (sparecnt)
929 fprintf(stderr, " and %d spare%s", sparecnt, sparecnt==1?"":"s");
930 fprintf(stderr, ".\n");
931 }
589395d6
NB
932 if (must_close) {
933 int usecs = 1;
934 close(mdfd);
935 /* There is a nasty race with 'mdadm --monitor'.
936 * If it opens this device before we close it,
937 * it gets an incomplete open on which IO
938 * doesn't work and the capacity if wrong.
939 * If we reopen (to check for layered devices)
940 * before --monitor closes, we loose.
941 *
942 * So: wait upto 1 second for there to be
943 * a non-zero capacity.
944 */
945 while (usecs < 1000) {
946 mdfd = open(mddev, O_RDONLY);
947 if (mdfd >= 0) {
beae1dfe
NB
948 unsigned long long size;
949 if (get_dev_size(mdfd, NULL, &size) &&
589395d6
NB
950 size > 0)
951 break;
952 close(mdfd);
953 }
954 usleep(usecs);
955 usecs <<= 1;
956 }
957 }
64c4757e 958 return 0;
82b27616 959 }
682c7051 960 fprintf(stderr, Name ": failed to RUN_ARRAY %s: %s\n",
64c4757e 961 mddev, strerror(errno));
583315d9
NB
962
963 if (!enough(info.array.level, info.array.raid_disks,
964 info.array.layout, 1, avail, okcnt))
965 fprintf(stderr, Name ": Not enough devices to "
966 "start the array.\n");
967 else if (!enough(info.array.level,
968 info.array.raid_disks,
969 info.array.layout, clean,
970 avail, okcnt))
971 fprintf(stderr, Name ": Not enough devices to "
972 "start the array while not clean "
973 "- consider --force.\n");
974
d55e3aef 975 if (must_close) close(mdfd);
64c4757e
NB
976 return 1;
977 }
82b27616 978 if (runstop == -1) {
b8a8ccf9 979 fprintf(stderr, Name ": %s assembled from %d drive%s",
52826846 980 mddev, okcnt, okcnt==1?"":"s");
b8a8ccf9
NB
981 if (okcnt != info.array.raid_disks)
982 fprintf(stderr, " (out of %d)", info.array.raid_disks);
983 fprintf(stderr, ", but not started.\n");
d55e3aef 984 if (must_close) close(mdfd);
64c4757e 985 return 0;
82b27616 986 }
4855f95c 987 if (verbose >= -1) {
dab6685f
NB
988 fprintf(stderr, Name ": %s assembled from %d drive%s", mddev, okcnt, okcnt==1?"":"s");
989 if (sparecnt)
990 fprintf(stderr, " and %d spare%s", sparecnt, sparecnt==1?"":"s");
583315d9
NB
991 if (!enough(info.array.level, info.array.raid_disks,
992 info.array.layout, 1, avail, okcnt))
dab6685f 993 fprintf(stderr, " - not enough to start the array.\n");
583315d9
NB
994 else if (!enough(info.array.level,
995 info.array.raid_disks,
996 info.array.layout, clean,
997 avail, okcnt))
998 fprintf(stderr, " - not enough to start the "
999 "array while not clean - consider "
1000 "--force.\n");
dab6685f
NB
1001 else {
1002 if (req_cnt == info.array.raid_disks)
1003 fprintf(stderr, " - need all %d to start it", req_cnt);
1004 else
1005 fprintf(stderr, " - need %d of %d to start", req_cnt, info.array.raid_disks);
1006 fprintf(stderr, " (use --run to insist).\n");
1007 }
aa88f531 1008 }
d55e3aef 1009 if (must_close) close(mdfd);
64c4757e 1010 return 1;
82b27616 1011 } else {
52826846
NB
1012 /* The "chosen_drive" is a good choice, and if necessary, the superblock has
1013 * been updated to point to the current locations of devices.
1014 * so we can just start the array
82b27616 1015 */
cd29a5c8 1016 unsigned long dev;
0df46c2a 1017 dev = makedev(devices[chosen_drive].major,
82b27616
NB
1018 devices[chosen_drive].minor);
1019 if (ioctl(mdfd, START_ARRAY, dev)) {
1020 fprintf(stderr, Name ": Cannot start array: %s\n",
1021 strerror(errno));
1022 }
aba69144 1023
64c4757e 1024 }
d55e3aef 1025 if (must_close) close(mdfd);
e0d19036 1026 return 0;
64c4757e 1027}