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