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