]> git.ipfire.org Git - thirdparty/mdadm.git/blame - Assemble.c
Never remove device nodes of active devices.
[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"
64c4757e 31
624920bb
NB
32static int name_matches(char *found, char *required, char *homehost)
33{
34 /* See if the name found matches the required name, possibly
35 * prefixed with 'homehost'
36 */
37 char fnd[33];
38
39 strncpy(fnd, found, 32);
40 fnd[32] = 0;
41 if (strcmp(found, required)==0)
42 return 1;
43 if (homehost) {
44 int l = strlen(homehost);
45 if (l < 32 && fnd[l] == ':' &&
46 strcmp(fnd+l+1, required)==0)
47 return 1;
48 }
49 return 0;
50}
51
82d9eba6 52int Assemble(struct supertype *st, char *mddev, int mdfd,
52826846 53 mddev_ident_t ident, char *conffile,
06b0d786 54 mddev_dev_t devlist, char *backup_file,
64c4757e 55 int readonly, int runstop,
e5eac01f 56 char *update, char *homehost,
64c4757e
NB
57 int verbose, int force)
58{
59 /*
52826846
NB
60 * The task of Assemble is to find a collection of
61 * devices that should (according to their superblocks)
62 * form an array, and to give this collection to the MD driver.
63 * In Linux-2.4 and later, this involves submitting a
64c4757e
NB
64 * SET_ARRAY_INFO ioctl with no arg - to prepare
65 * the array - and then submit a number of
66 * ADD_NEW_DISK ioctls to add disks into
67 * the array. Finally RUN_ARRAY might
68 * be submitted to start the array.
69 *
70 * Much of the work of Assemble is in finding and/or
71 * checking the disks to make sure they look right.
72 *
4b1ac34b 73 * If mddev is not set, then scan must be set and we
64c4757e
NB
74 * read through the config file for dev+uuid mapping
75 * We recurse, setting mddev, for each device that
76 * - isn't running
4b1ac34b 77 * - has a valid uuid (or any uuid if !uuidset)
64c4757e
NB
78 *
79 * If mddev is set, we try to determine state of md.
80 * check version - must be at least 0.90.0
81 * check kernel version. must be at least 2.4.
82 * If not, we can possibly fall back on START_ARRAY
83 * Try to GET_ARRAY_INFO.
84 * If possible, give up
85 * If not, try to STOP_ARRAY just to make sure
86 *
87 * If !uuidset and scan, look in conf-file for uuid
88 * If not found, give up
b8a8ccf9 89 * If !devlist and scan and uuidset, get list of devs from conf-file
64c4757e
NB
90 *
91 * For each device:
92 * Check superblock - discard if bad
93 * Check uuid (set if we don't have one) - discard if no match
5787fa49 94 * Check superblock similarity if we have a superblock - discard if different
4b1ac34b 95 * Record events, devicenum
64c4757e 96 * This should give us a list of devices for the array
4b1ac34b 97 * We should collect the most recent event number
64c4757e
NB
98 *
99 * Count disks with recent enough event count
100 * While force && !enough disks
101 * Choose newest rejected disks, update event count
102 * mark clean and rewrite superblock
103 * If recent kernel:
104 * SET_ARRAY_INFO
105 * foreach device with recent events : ADD_NEW_DISK
106 * if runstop == 1 || "enough" disks and runstop==0 -> RUN_ARRAY
107 * If old kernel:
108 * Check the device numbers in superblock are right
109 * update superblock if any changes
110 * START_ARRAY
111 *
112 */
113 int old_linux = 0;
114 int vers;
4b1ac34b 115 void *first_super = NULL, *super = NULL;
64c4757e
NB
116 struct {
117 char *devname;
98c6faba
NB
118 unsigned int major, minor;
119 unsigned int oldmajor, oldminor;
64c4757e 120 long long events;
64c4757e 121 int uptodate;
d013a55e 122 int state;
52826846 123 int raid_disk;
6a41304b 124 int disk_nr;
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;
b8a8ccf9 136 int start_partial_ok = (runstop >= 0) && (force || devlist==NULL);
98c6faba 137 unsigned int num_devs;
5787fa49 138 mddev_dev_t tmpdev;
4b1ac34b 139 struct mdinfo info;
265e0f17 140 char *avail;
308e1801 141 int nextspare = 0;
8a46fe84 142
682c7051 143 if (get_linux_version() < 2004000)
64c4757e
NB
144 old_linux = 1;
145
8a46fe84
NB
146 if (mdfd >= 0) {
147 vers = md_get_version(mdfd);
148 if (vers <= 0) {
149 fprintf(stderr, Name ": %s appears not to be an md device.\n", mddev);
150 return 1;
151 }
152 if (vers < 9000) {
153 fprintf(stderr, Name ": Assemble requires driver version 0.90.0 or later.\n"
154 " Upgrade your kernel or try --build\n");
155 return 1;
156 }
64c4757e 157
8a46fe84
NB
158 if (ioctl(mdfd, GET_ARRAY_INFO, &info.array)>=0) {
159 fprintf(stderr, Name ": device %s already active - cannot assemble it\n",
160 mddev);
161 return 1;
162 }
163 ioctl(mdfd, STOP_ARRAY, NULL); /* just incase it was started but has no content */
164 }
64c4757e 165 /*
52826846
NB
166 * If any subdevs are listed, then any that don't
167 * match ident are discarded. Remainder must all match and
168 * become the array.
169 * If no subdevs, then we scan all devices in the config file, but
170 * there must be something in the identity
64c4757e 171 */
64c4757e 172
cd29a5c8 173 if (!devlist &&
52826846
NB
174 ident->uuid_set == 0 &&
175 ident->super_minor < 0 &&
176 ident->devices == NULL) {
177 fprintf(stderr, Name ": No identity information available for %s - cannot assemble.\n",
8a46fe84 178 mddev ? mddev : "further assembly");
52826846 179 return 1;
64c4757e 180 }
cd29a5c8 181 if (devlist == NULL)
64c4757e 182 devlist = conf_get_devs(conffile);
da6b5ca9
NB
183 else if (mdfd >= 0)
184 inargv = 1;
64c4757e 185
5787fa49
NB
186 tmpdev = devlist; num_devs = 0;
187 while (tmpdev) {
da6b5ca9
NB
188 if (tmpdev->used)
189 tmpdev->used = 2;
190 else
191 num_devs++;
5787fa49
NB
192 tmpdev = tmpdev->next;
193 }
5787fa49
NB
194 devices = malloc(num_devs * sizeof(*devices));
195
82d9eba6 196 if (!st && ident->st) st = ident->st;
64c4757e 197
dab6685f 198 if (verbose>0)
82b27616 199 fprintf(stderr, Name ": looking for devices for %s\n",
8a46fe84 200 mddev ? mddev : "further assembly");
82b27616 201
811e6cbe
NB
202 /* first walk the list of devices to find a consistent set
203 * that match the criterea, if that is possible.
204 * We flag the one we like with 'used'.
205 */
206 for (tmpdev = devlist;
207 tmpdev;
208 tmpdev = tmpdev->next) {
209 char *devname = tmpdev->devname;
64c4757e
NB
210 int dfd;
211 struct stat stb;
82d9eba6 212 struct supertype *tst = st;
52826846 213
da6b5ca9
NB
214 if (tmpdev->used > 1) continue;
215
52826846 216 if (ident->devices &&
56eedc1a 217 !match_oneof(ident->devices, devname)) {
6a41304b 218 if ((inargv && verbose>=0) || verbose > 0)
56eedc1a 219 fprintf(stderr, Name ": %s is not one of %s\n", devname, ident->devices);
52826846 220 continue;
56eedc1a 221 }
4b1ac34b
NB
222
223 if (super) {
224 free(super);
225 super = NULL;
226 }
52826846 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;
82d9eba6 248 } else if (tst->ss->load_super(tst,dfd, &super, 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 {
31317663 253 tst->ss->getinfo_super(&info, super);
64c4757e 254 }
c06487ce 255 if (dfd >= 0) close(dfd);
52826846 256
838acbc2 257 if (ident->uuid_set && (!update || strcmp(update, "uuid")!= 0) &&
f277ce36 258 (!super || same_uuid(info.uuid, ident->uuid, tst->ss->swapuuid)==0)) {
6a41304b 259 if ((inargv && verbose >= 0) || verbose > 0)
52826846
NB
260 fprintf(stderr, Name ": %s has wrong uuid.\n",
261 devname);
262 continue;
82b27616 263 }
c4f12c13 264 if (ident->name[0] && (!update || strcmp(update, "name")!= 0) &&
624920bb 265 (!super || name_matches(info.name, ident->name, homehost)==0)) {
6a41304b 266 if ((inargv && verbose >= 0) || verbose > 0)
947fd4dd
NB
267 fprintf(stderr, Name ": %s has wrong name.\n",
268 devname);
269 continue;
270 }
98c6faba 271 if (ident->super_minor != UnSet &&
4b1ac34b 272 (!super || ident->super_minor != info.array.md_minor)) {
6a41304b 273 if ((inargv && verbose >= 0) || verbose > 0)
52826846 274 fprintf(stderr, Name ": %s has wrong super-minor.\n",
64c4757e
NB
275 devname);
276 continue;
277 }
98c6faba 278 if (ident->level != UnSet &&
4b1ac34b 279 (!super|| ident->level != info.array.level)) {
6a41304b 280 if ((inargv && verbose >= 0) || verbose > 0)
cd29a5c8
NB
281 fprintf(stderr, Name ": %s has wrong raid level.\n",
282 devname);
283 continue;
284 }
98c6faba 285 if (ident->raid_disks != UnSet &&
4b1ac34b 286 (!super || ident->raid_disks!= info.array.raid_disks)) {
6a41304b 287 if ((inargv && verbose >= 0) || verbose > 0)
cd29a5c8
NB
288 fprintf(stderr, Name ": %s requires wrong number of drives.\n",
289 devname);
290 continue;
291 }
da6b5ca9
NB
292 if (mdfd < 0) {
293 if (tst == NULL || super == NULL)
294 continue;
295 if (tst->ss->match_home(super, homehost)==0) {
296 if ((inargv && verbose >= 0) || verbose > 0)
297 fprintf(stderr, Name ": %s is not built for host %s.\n",
298 devname, homehost);
299 /* Auto-assemble, and this is not a usable host */
300 continue;
301 }
302 }
83b6208e 303 /* If we are this far, then we are nearly commited to this device.
52826846 304 * If the super_block doesn't exist, or doesn't match others,
83b6208e
NB
305 * then we probably cannot continue
306 * However if one of the arrays is for the homehost, and
307 * the other isn't that can disambiguate.
52826846 308 */
52826846 309
4b1ac34b 310 if (!super) {
52826846
NB
311 fprintf(stderr, Name ": %s has no superblock - assembly aborted\n",
312 devname);
4b1ac34b 313 free(first_super);
52826846
NB
314 return 1;
315 }
838acbc2 316
83b6208e
NB
317 if (st == NULL)
318 st = tst;
319 if (st->ss != tst->ss ||
320 st->minor_version != tst->minor_version ||
321 st->ss->compare_super(&first_super, super) != 0) {
322 /* Some mismatch. If exactly one array matches this host,
da6b5ca9
NB
323 * we can resolve on that one.
324 * Or, if we are auto assembling, we just ignore the second
325 * for now.
83b6208e 326 */
da6b5ca9
NB
327 if (mdfd < 0)
328 continue;
83b6208e
NB
329 if (homehost) {
330 int first = st->ss->match_home(first_super, homehost);
331 int last = tst->ss->match_home(super, homehost);
332 if (first+last == 1) {
333 /* We can do something */
334 if (first) {/* just ignore this one */
335 if ((inargv && verbose >= 0) || verbose > 0)
336 fprintf(stderr, Name ": %s misses out due to wrong homehost\n",
337 devname);
338 continue;
339 } else { /* reject all those sofar */
340 mddev_dev_t td;
341 if ((inargv && verbose >= 0) || verbose > 0)
342 fprintf(stderr, Name ": %s overrides previous devices due to good homehost\n",
343 devname);
344 for (td=devlist; td != tmpdev; td=td->next)
345 if (td->used == 1)
346 td->used = 0;
347 tmpdev->used = 1;
348 continue;
349 }
350 }
351 }
52826846
NB
352 fprintf(stderr, Name ": superblock on %s doesn't match others - assembly aborted\n",
353 devname);
4b1ac34b
NB
354 free(super);
355 free(first_super);
52826846 356 return 1;
64c4757e
NB
357 }
358
811e6cbe
NB
359 tmpdev->used = 1;
360 }
361
8a46fe84
NB
362 if (mdfd < 0) {
363 /* So... it is up to me to open the device.
364 * We create a name '/dev/md/XXX' based on the info in the
365 * superblock, and call open_mddev on that
366 */
da6b5ca9
NB
367 mdu_array_info_t inf;
368 char *c;
369 if (!first_super) {
370 return 1;
371 }
372 st->ss->getinfo_super(&info, first_super);
373 c = strchr(info.name, ':');
374 if (c) c++; else c= info.name;
375 asprintf(&mddev, "/dev/md/%s", c);
376 mdfd = open_mddev(mddev, ident->autof);
8a46fe84
NB
377 if (mdfd < 0)
378 return mdfd;
da6b5ca9
NB
379 vers = md_get_version(mdfd);
380 if (ioctl(mdfd, GET_ARRAY_INFO, &inf)==0) {
381 fprintf(stderr, Name ": %s already active, cannot restart it!\n", mddev);
382 close(mdfd);
383 free(first_super);
384 return 1;
385 }
8a46fe84
NB
386 }
387
811e6cbe 388 /* Ok, no bad inconsistancy, we can try updating etc */
da6b5ca9 389 for (tmpdev = devlist; tmpdev; tmpdev=tmpdev->next) if (tmpdev->used == 1) {
811e6cbe
NB
390 char *devname = tmpdev->devname;
391 struct stat stb;
5787fa49
NB
392 /* looks like a good enough match to update the super block if needed */
393 if (update) {
811e6cbe 394 int dfd;
4b1ac34b
NB
395 /* prepare useful information in info structures */
396 struct stat stb2;
397 fstat(mdfd, &stb2);
7d99579f
NB
398
399 if (strcmp(update, "uuid")==0 &&
400 !ident->uuid_set) {
401 int rfd;
402 if ((rfd = open("/dev/urandom", O_RDONLY)) < 0 ||
403 read(rfd, ident->uuid, 16) != 16) {
404 *(__u32*)(ident->uuid) = random();
405 *(__u32*)(ident->uuid+1) = random();
406 *(__u32*)(ident->uuid+2) = random();
407 *(__u32*)(ident->uuid+3) = random();
408 }
409 if (rfd >= 0) close(rfd);
7d99579f 410 }
811e6cbe
NB
411 dfd = dev_open(devname, O_RDWR|O_EXCL);
412
413 if (super) {
414 free(super);
415 super = NULL;
416 }
417
418 st->ss->load_super(st, dfd, &super, NULL);
419 st->ss->getinfo_super(&info, super);
420
7d99579f 421 memcpy(info.uuid, ident->uuid, 16);
e5eac01f 422 strcpy(info.name, ident->name);
811e6cbe
NB
423 info.array.md_minor = minor(stb2.st_rdev);
424
e5eac01f
NB
425 st->ss->update_super(&info, super, update, devname, verbose,
426 ident->uuid_set, homehost);
427 if (strcmp(update, "uuid")==0 &&
428 !ident->uuid_set) {
429 ident->uuid_set = 1;
430 memcpy(ident->uuid, info.uuid, 16);
431 }
b8a8ccf9 432 if (dfd < 0)
5787fa49
NB
433 fprintf(stderr, Name ": Cannot open %s for superblock update\n",
434 devname);
96395475 435 else if (st->ss->store_super(st, dfd, super))
5787fa49
NB
436 fprintf(stderr, Name ": Could not re-write superblock on %s.\n",
437 devname);
438 if (dfd >= 0)
439 close(dfd);
8131b493
NB
440
441 if (strcmp(update, "uuid")==0 &&
442 ident->bitmap_fd)
443 bitmap_update_uuid(ident->bitmap_fd, info.uuid);
da6b5ca9
NB
444 } else {
445 int dfd;
446 dfd = dev_open(devname, O_RDWR|O_EXCL);
447
448 if (super) {
449 free(super);
450 super = NULL;
451 }
452
453 st->ss->load_super(st, dfd, &super, NULL);
454 st->ss->getinfo_super(&info, super);
455 close(dfd);
5787fa49
NB
456 }
457
811e6cbe
NB
458 stat(devname, &stb);
459
dab6685f 460 if (verbose > 0)
cd29a5c8 461 fprintf(stderr, Name ": %s is identified as a member of %s, slot %d.\n",
4b1ac34b 462 devname, mddev, info.disk.raid_disk);
64c4757e 463 devices[devcnt].devname = devname;
0df46c2a
NB
464 devices[devcnt].major = major(stb.st_rdev);
465 devices[devcnt].minor = minor(stb.st_rdev);
4b1ac34b
NB
466 devices[devcnt].oldmajor = info.disk.major;
467 devices[devcnt].oldminor = info.disk.minor;
468 devices[devcnt].events = info.events;
469 devices[devcnt].raid_disk = info.disk.raid_disk;
6a41304b 470 devices[devcnt].disk_nr = info.disk.number;
64c4757e 471 devices[devcnt].uptodate = 0;
4b1ac34b 472 devices[devcnt].state = info.disk.state;
64c4757e
NB
473 if (most_recent < devcnt) {
474 if (devices[devcnt].events
475 > devices[most_recent].events)
476 most_recent = devcnt;
477 }
b8a8ccf9 478 if (info.array.level == -4)
5787fa49
NB
479 /* with multipath, the raid_disk from the superblock is meaningless */
480 i = devcnt;
481 else
482 i = devices[devcnt].raid_disk;
308e1801
NB
483 if (i+1 == 0) {
484 if (nextspare < info.array.raid_disks)
485 nextspare = info.array.raid_disks;
486 i = nextspare++;
487 }
98c6faba 488 if (i < 10000) {
56eedc1a 489 if (i >= bestcnt) {
98c6faba 490 unsigned int newbestcnt = i+10;
56eedc1a 491 int *newbest = malloc(sizeof(int)*newbestcnt);
98c6faba 492 unsigned int c;
56eedc1a
NB
493 for (c=0; c < newbestcnt; c++)
494 if (c < bestcnt)
495 newbest[c] = best[c];
496 else
497 newbest[c] = -1;
498 if (best)free(best);
499 best = newbest;
500 bestcnt = newbestcnt;
501 }
52826846
NB
502 if (best[i] == -1
503 || devices[best[i]].events < devices[devcnt].events)
504 best[i] = devcnt;
56eedc1a 505 }
64c4757e
NB
506 devcnt++;
507 }
508
4b1ac34b
NB
509 if (super)
510 free(super);
511 super = NULL;
512
586ed405
NB
513 if (update && strcmp(update, "byteorder")==0)
514 st->minor_version = 90;
515
64c4757e 516 if (devcnt == 0) {
682c7051 517 fprintf(stderr, Name ": no devices found for %s\n",
64c4757e 518 mddev);
4b1ac34b 519 free(first_super);
64c4757e
NB
520 return 1;
521 }
4b1ac34b 522
31317663 523 st->ss->getinfo_super(&info, first_super);
4b1ac34b 524
64c4757e
NB
525 /* now we have some devices that might be suitable.
526 * I wonder how many
527 */
265e0f17
NB
528 avail = malloc(info.array.raid_disks);
529 memset(avail, 0, info.array.raid_disks);
64c4757e 530 okcnt = 0;
52826846 531 sparecnt=0;
56eedc1a 532 for (i=0; i< bestcnt ;i++) {
64c4757e 533 int j = best[i];
ee04451c
NB
534 int event_margin = 1; /* always allow a difference of '1'
535 * like the kernel does
536 */
64c4757e 537 if (j < 0) continue;
d013a55e
NB
538 /* note: we ignore error flags in multipath arrays
539 * as they don't make sense
540 */
4b1ac34b 541 if (info.array.level != -4)
aa88f531
NB
542 if (!(devices[j].state & (1<<MD_DISK_SYNC))) {
543 if (!(devices[j].state & (1<<MD_DISK_FAULTY)))
544 sparecnt++;
d013a55e 545 continue;
aa88f531 546 }
cd29a5c8 547 if (devices[j].events+event_margin >=
64c4757e
NB
548 devices[most_recent].events) {
549 devices[j].uptodate = 1;
265e0f17 550 if (i < info.array.raid_disks) {
52826846 551 okcnt++;
265e0f17
NB
552 avail[i]=1;
553 } else
52826846 554 sparecnt++;
64c4757e
NB
555 }
556 }
265e0f17
NB
557 while (force && !enough(info.array.level, info.array.raid_disks,
558 info.array.layout,
559 avail, okcnt)) {
64c4757e
NB
560 /* Choose the newest best drive which is
561 * not up-to-date, update the superblock
562 * and add it.
563 */
52826846 564 int fd;
cd29a5c8 565 chosen_drive = -1;
4b1ac34b 566 for (i=0; i<info.array.raid_disks && i < bestcnt; i++) {
52826846
NB
567 int j = best[i];
568 if (j>=0 &&
569 !devices[j].uptodate &&
570 devices[j].events > 0 &&
571 (chosen_drive < 0 ||
572 devices[j].events > devices[chosen_drive].events))
573 chosen_drive = j;
574 }
575 if (chosen_drive < 0)
576 break;
dab6685f
NB
577 if (verbose >= 0)
578 fprintf(stderr, Name ": forcing event count in %s(%d) from %d upto %d\n",
579 devices[chosen_drive].devname, devices[chosen_drive].raid_disk,
580 (int)(devices[chosen_drive].events),
581 (int)(devices[most_recent].events));
8b0dabea 582 fd = dev_open(devices[chosen_drive].devname, O_RDWR|O_EXCL);
52826846
NB
583 if (fd < 0) {
584 fprintf(stderr, Name ": Couldn't open %s for write - not updating\n",
585 devices[chosen_drive].devname);
586 devices[chosen_drive].events = 0;
587 continue;
588 }
82d9eba6 589 if (st->ss->load_super(st,fd, &super, NULL)) {
52826846
NB
590 close(fd);
591 fprintf(stderr, Name ": RAID superblock disappeared from %s - not updating.\n",
592 devices[chosen_drive].devname);
593 devices[chosen_drive].events = 0;
594 continue;
595 }
4b1ac34b 596 info.events = devices[most_recent].events;
e5eac01f 597 st->ss->update_super(&info, super, "force", devices[chosen_drive].devname, verbose, 0, NULL);
4b1ac34b 598
96395475 599 if (st->ss->store_super(st, fd, super)) {
52826846
NB
600 close(fd);
601 fprintf(stderr, Name ": Could not re-write superblock on %s\n",
602 devices[chosen_drive].devname);
603 devices[chosen_drive].events = 0;
4b1ac34b 604 free(super);
52826846
NB
605 continue;
606 }
607 close(fd);
608 devices[chosen_drive].events = devices[most_recent].events;
609 devices[chosen_drive].uptodate = 1;
265e0f17 610 avail[chosen_drive] = 1;
52826846 611 okcnt++;
4b1ac34b 612 free(super);
64c4757e 613 }
52826846
NB
614
615 /* Now we want to look at the superblock which the kernel will base things on
616 * and compare the devices that we think are working with the devices that the
617 * superblock thinks are working.
618 * If there are differences and --force is given, then update this chosen
619 * superblock.
620 */
cd29a5c8 621 chosen_drive = -1;
4b1ac34b 622 super = NULL;
56eedc1a 623 for (i=0; chosen_drive < 0 && i<bestcnt; i++) {
52826846
NB
624 int j = best[i];
625 int fd;
4b1ac34b 626
52826846
NB
627 if (j<0)
628 continue;
629 if (!devices[j].uptodate)
630 continue;
631 chosen_drive = j;
8b0dabea 632 if ((fd=dev_open(devices[j].devname, O_RDONLY|O_EXCL))< 0) {
52826846
NB
633 fprintf(stderr, Name ": Cannot open %s: %s\n",
634 devices[j].devname, strerror(errno));
635 return 1;
636 }
82d9eba6 637 if (st->ss->load_super(st,fd, &super, NULL)) {
52826846
NB
638 close(fd);
639 fprintf(stderr, Name ": RAID superblock has disappeared from %s\n",
640 devices[j].devname);
641 return 1;
642 }
643 close(fd);
644 }
4b1ac34b
NB
645 if (super == NULL) {
646 fprintf(stderr, Name ": No suitable drives found for %s\n", mddev);
647 return 1;
648 }
31317663 649 st->ss->getinfo_super(&info, super);
56eedc1a 650 for (i=0; i<bestcnt; i++) {
52826846 651 int j = best[i];
98c6faba 652 unsigned int desired_state;
11a3e71d 653
4b1ac34b 654 if (i < info.array.raid_disks)
11a3e71d
NB
655 desired_state = (1<<MD_DISK_ACTIVE) | (1<<MD_DISK_SYNC);
656 else
657 desired_state = 0;
658
52826846
NB
659 if (j<0)
660 continue;
661 if (!devices[j].uptodate)
662 continue;
6a41304b 663 info.disk.number = devices[j].disk_nr;
fbf8a0b7 664 info.disk.raid_disk = i;
4b1ac34b
NB
665 info.disk.state = desired_state;
666
52826846 667 if (devices[j].uptodate &&
e5eac01f 668 st->ss->update_super(&info, super, "assemble", NULL, verbose, 0, NULL)) {
52826846 669 if (force) {
dab6685f
NB
670 if (verbose >= 0)
671 fprintf(stderr, Name ": "
672 "clearing FAULTY flag for device %d in %s for %s\n",
673 j, mddev, devices[j].devname);
4b1ac34b 674 change = 1;
52826846 675 } else {
dab6685f
NB
676 if (verbose >= -1)
677 fprintf(stderr, Name ": "
678 "device %d in %s has wrong state in superblock, but %s seems ok\n",
679 i, mddev, devices[j].devname);
52826846
NB
680 }
681 }
4b1ac34b 682#if 0
52826846
NB
683 if (!devices[j].uptodate &&
684 !(super.disks[i].state & (1 << MD_DISK_FAULTY))) {
685 fprintf(stderr, Name ": devices %d of %s is not marked FAULTY in superblock, but cannot be found\n",
686 i, mddev);
687 }
4b1ac34b 688#endif
52826846 689 }
4b1ac34b
NB
690 if (force && okcnt == info.array.raid_disks-1) {
691 /* FIXME check event count */
b8a8ccf9 692 change += st->ss->update_super(&info, super, "force",
e5eac01f 693 devices[chosen_drive].devname, verbose, 0, NULL);
d013a55e 694 }
52826846 695
4b1ac34b 696 if (change) {
52826846 697 int fd;
8b0dabea 698 fd = dev_open(devices[chosen_drive].devname, O_RDWR|O_EXCL);
52826846 699 if (fd < 0) {
353632d9 700 fprintf(stderr, Name ": Could not open %s for write - cannot Assemble array.\n",
52826846
NB
701 devices[chosen_drive].devname);
702 return 1;
703 }
96395475 704 if (st->ss->store_super(st, fd, super)) {
52826846
NB
705 close(fd);
706 fprintf(stderr, Name ": Could not re-write superblock on %s\n",
707 devices[chosen_drive].devname);
708 return 1;
709 }
710 close(fd);
52826846
NB
711 }
712
353632d9
NB
713 /* If we are in the middle of a reshape we may need to restore saved data
714 * that was moved aside due to the reshape overwriting live data
715 * The code of doing this lives in Grow.c
716 */
39c5a909 717#ifndef MDASSEMBLE
353632d9
NB
718 if (info.reshape_active) {
719 int err = 0;
720 int *fdlist = malloc(sizeof(int)* bestcnt);
721 for (i=0; i<bestcnt; i++) {
722 int j = best[i];
723 if (j >= 0) {
724 fdlist[i] = dev_open(devices[j].devname, O_RDWR|O_EXCL);
725 if (fdlist[i] < 0) {
726 fprintf(stderr, Name ": Could not open %s for write - cannot Assemble array.\n",
727 devices[j].devname);
728 err = 1;
729 break;
730 }
731 } else
732 fdlist[i] = -1;
733 }
734 if (!err)
06b0d786 735 err = Grow_restart(st, &info, fdlist, bestcnt, backup_file);
353632d9
NB
736 while (i>0) {
737 i--;
738 if (fdlist[i]>=0) close(fdlist[i]);
739 }
740 if (err) {
741 fprintf(stderr, Name ": Failed to restore critical section for reshape, sorry.\n");
742 return err;
743 }
744 }
39c5a909 745#endif
aa88f531
NB
746 /* count number of in-sync devices according to the superblock.
747 * We must have this number to start the array without -s or -R
748 */
4b1ac34b 749 req_cnt = info.array.working_disks;
aa88f531 750
64c4757e
NB
751 /* Almost ready to actually *do* something */
752 if (!old_linux) {
fbf8a0b7
NB
753 int rv;
754 if ((vers % 100) >= 1) { /* can use different versions */
755 mdu_array_info_t inf;
756 memset(&inf, 0, sizeof(inf));
757 inf.major_version = st->ss->major;
758 inf.minor_version = st->minor_version;
759 rv = ioctl(mdfd, SET_ARRAY_INFO, &inf);
b8a8ccf9 760 } else
fbf8a0b7
NB
761 rv = ioctl(mdfd, SET_ARRAY_INFO, NULL);
762
763 if (rv) {
682c7051 764 fprintf(stderr, Name ": SET_ARRAY_INFO failed for %s: %s\n",
64c4757e
NB
765 mddev, strerror(errno));
766 return 1;
767 }
11484a63 768 if (ident->bitmap_fd >= 0) {
c82f047c
NB
769 if (ioctl(mdfd, SET_BITMAP_FILE, ident->bitmap_fd) != 0) {
770 fprintf(stderr, Name ": SET_BITMAP_FILE failed.\n");
771 return 1;
772 }
7ef02d01
NB
773 } else if (ident->bitmap_file) {
774 /* From config file */
775 int bmfd = open(ident->bitmap_file, O_RDWR);
776 if (bmfd < 0) {
777 fprintf(stderr, Name ": Could not open bitmap file %s\n",
778 ident->bitmap_file);
779 return 1;
780 }
781 if (ioctl(mdfd, SET_BITMAP_FILE, bmfd) != 0) {
782 fprintf(stderr, Name ": Failed to set bitmapfile for %s\n", mddev);
783 close(bmfd);
784 return 1;
785 }
786 close(bmfd);
c82f047c 787 }
7ef02d01 788
52826846 789 /* First, add the raid disks, but add the chosen one last */
56eedc1a 790 for (i=0; i<= bestcnt; i++) {
52826846 791 int j;
56eedc1a 792 if (i < bestcnt) {
52826846
NB
793 j = best[i];
794 if (j == chosen_drive)
795 continue;
796 } else
797 j = chosen_drive;
798
56eedc1a 799 if (j >= 0 /* && devices[j].uptodate */) {
64c4757e
NB
800 mdu_disk_info_t disk;
801 memset(&disk, 0, sizeof(disk));
802 disk.major = devices[j].major;
803 disk.minor = devices[j].minor;
804 if (ioctl(mdfd, ADD_NEW_DISK, &disk)!=0) {
682c7051 805 fprintf(stderr, Name ": failed to add %s to %s: %s\n",
64c4757e
NB
806 devices[j].devname,
807 mddev,
808 strerror(errno));
4b1ac34b 809 if (i < info.array.raid_disks || i == bestcnt)
52826846
NB
810 okcnt--;
811 else
812 sparecnt--;
dab6685f 813 } else if (verbose > 0)
52826846
NB
814 fprintf(stderr, Name ": added %s to %s as %d\n",
815 devices[j].devname, mddev, devices[j].raid_disk);
dab6685f 816 } else if (verbose > 0 && i < info.array.raid_disks)
682c7051 817 fprintf(stderr, Name ": no uptodate device for slot %d of %s\n",
64c4757e
NB
818 i, mddev);
819 }
52826846 820
64c4757e 821 if (runstop == 1 ||
b8a8ccf9 822 (runstop <= 0 &&
265e0f17 823 ( enough(info.array.level, info.array.raid_disks, info.array.layout, avail, okcnt) &&
aa88f531
NB
824 (okcnt >= req_cnt || start_partial_ok)
825 ))) {
82b27616 826 if (ioctl(mdfd, RUN_ARRAY, NULL)==0) {
dab6685f
NB
827 if (verbose >= 0) {
828 fprintf(stderr, Name ": %s has been started with %d drive%s",
829 mddev, okcnt, okcnt==1?"":"s");
b8a8ccf9 830 if (okcnt < info.array.raid_disks)
dab6685f
NB
831 fprintf(stderr, " (out of %d)", info.array.raid_disks);
832 if (sparecnt)
833 fprintf(stderr, " and %d spare%s", sparecnt, sparecnt==1?"":"s");
834 fprintf(stderr, ".\n");
835 }
64c4757e 836 return 0;
82b27616 837 }
682c7051 838 fprintf(stderr, Name ": failed to RUN_ARRAY %s: %s\n",
64c4757e
NB
839 mddev, strerror(errno));
840 return 1;
841 }
82b27616 842 if (runstop == -1) {
b8a8ccf9 843 fprintf(stderr, Name ": %s assembled from %d drive%s",
52826846 844 mddev, okcnt, okcnt==1?"":"s");
b8a8ccf9
NB
845 if (okcnt != info.array.raid_disks)
846 fprintf(stderr, " (out of %d)", info.array.raid_disks);
847 fprintf(stderr, ", but not started.\n");
64c4757e 848 return 0;
82b27616 849 }
dab6685f
NB
850 if (verbose >= 0) {
851 fprintf(stderr, Name ": %s assembled from %d drive%s", mddev, okcnt, okcnt==1?"":"s");
852 if (sparecnt)
853 fprintf(stderr, " and %d spare%s", sparecnt, sparecnt==1?"":"s");
265e0f17 854 if (!enough(info.array.level, info.array.raid_disks, info.array.layout, avail, okcnt))
dab6685f
NB
855 fprintf(stderr, " - not enough to start the array.\n");
856 else {
857 if (req_cnt == info.array.raid_disks)
858 fprintf(stderr, " - need all %d to start it", req_cnt);
859 else
860 fprintf(stderr, " - need %d of %d to start", req_cnt, info.array.raid_disks);
861 fprintf(stderr, " (use --run to insist).\n");
862 }
aa88f531 863 }
64c4757e 864 return 1;
82b27616 865 } else {
52826846
NB
866 /* The "chosen_drive" is a good choice, and if necessary, the superblock has
867 * been updated to point to the current locations of devices.
868 * so we can just start the array
82b27616 869 */
cd29a5c8 870 unsigned long dev;
0df46c2a 871 dev = makedev(devices[chosen_drive].major,
82b27616
NB
872 devices[chosen_drive].minor);
873 if (ioctl(mdfd, START_ARRAY, dev)) {
874 fprintf(stderr, Name ": Cannot start array: %s\n",
875 strerror(errno));
876 }
877
64c4757e 878 }
e0d19036 879 return 0;
64c4757e 880}