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