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