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