]>
Commit | Line | Data |
---|---|---|
64c4757e | 1 | /* |
9a9dab36 | 2 | * mdadm - manage Linux "md" devices aka RAID arrays. |
64c4757e | 3 | * |
e736b623 | 4 | * Copyright (C) 2001-2009 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 | |
e736b623 | 22 | * Email: <neilb@suse.de> |
64c4757e NB |
23 | */ |
24 | ||
9a9dab36 | 25 | #include "mdadm.h" |
41a3b72a | 26 | #include <ctype.h> |
64c4757e | 27 | |
624920bb NB |
28 | static int name_matches(char *found, char *required, char *homehost) |
29 | { | |
30 | /* See if the name found matches the required name, possibly | |
31 | * prefixed with 'homehost' | |
32 | */ | |
33 | char fnd[33]; | |
34 | ||
35 | strncpy(fnd, found, 32); | |
36 | fnd[32] = 0; | |
37 | if (strcmp(found, required)==0) | |
38 | return 1; | |
39 | if (homehost) { | |
40 | int l = strlen(homehost); | |
41 | if (l < 32 && fnd[l] == ':' && | |
42 | strcmp(fnd+l+1, required)==0) | |
43 | return 1; | |
44 | } | |
45 | return 0; | |
46 | } | |
47 | ||
9008ed1c | 48 | static int is_member_busy(char *metadata_version) |
2de8884f DW |
49 | { |
50 | /* check if the given member array is active */ | |
51 | struct mdstat_ent *mdstat = mdstat_read(1, 0); | |
52 | struct mdstat_ent *ent; | |
53 | int busy = 0; | |
54 | ||
55 | for (ent = mdstat; ent; ent = ent->next) { | |
56 | if (ent->metadata_version == NULL) | |
57 | continue; | |
58 | if (strncmp(ent->metadata_version, "external:", 9) != 0) | |
59 | continue; | |
60 | if (!is_subarray(&ent->metadata_version[9])) | |
61 | continue; | |
62 | /* Skip first char - it can be '/' or '-' */ | |
63 | if (strcmp(&ent->metadata_version[10], metadata_version+1) == 0) { | |
64 | busy = 1; | |
65 | break; | |
66 | } | |
67 | } | |
68 | free_mdstat(mdstat); | |
69 | ||
70 | return busy; | |
71 | } | |
72 | ||
fa56eddb | 73 | static int ident_matches(struct mddev_ident *ident, |
08fb91a3 N |
74 | struct mdinfo *content, |
75 | struct supertype *tst, | |
76 | char *homehost, | |
77 | char *update, char *devname) | |
78 | { | |
79 | ||
80 | if (ident->uuid_set && (!update || strcmp(update, "uuid")!= 0) && | |
cbeeb0e5 AC |
81 | same_uuid(content->uuid, ident->uuid, tst->ss->swapuuid)==0 && |
82 | memcmp(content->uuid, uuid_zero, sizeof(int[4])) != 0) { | |
08fb91a3 N |
83 | if (devname) |
84 | fprintf(stderr, Name ": %s has wrong uuid.\n", | |
85 | devname); | |
86 | return 0; | |
87 | } | |
88 | if (ident->name[0] && (!update || strcmp(update, "name")!= 0) && | |
89 | name_matches(content->name, ident->name, homehost)==0) { | |
90 | if (devname) | |
91 | fprintf(stderr, Name ": %s has wrong name.\n", | |
92 | devname); | |
93 | return 0; | |
94 | } | |
95 | if (ident->super_minor != UnSet && | |
96 | ident->super_minor != content->array.md_minor) { | |
97 | if (devname) | |
98 | fprintf(stderr, Name ": %s has wrong super-minor.\n", | |
99 | devname); | |
100 | return 0; | |
101 | } | |
102 | if (ident->level != UnSet && | |
103 | ident->level != content->array.level) { | |
104 | if (devname) | |
105 | fprintf(stderr, Name ": %s has wrong raid level.\n", | |
106 | devname); | |
107 | return 0; | |
108 | } | |
109 | if (ident->raid_disks != UnSet && | |
110 | ident->raid_disks!= content->array.raid_disks) { | |
111 | if (devname) | |
112 | fprintf(stderr, Name ": %s requires wrong number of drives.\n", | |
113 | devname); | |
114 | return 0; | |
115 | } | |
805d30b2 N |
116 | if (ident->member && ident->member[0]) { |
117 | /* content->text_version must match */ | |
118 | char *s = strchr(content->text_version+1, '/'); | |
119 | if (s == NULL) { | |
120 | if (devname) | |
121 | fprintf(stderr, Name ": %s is not a container and one is required.\n", | |
122 | devname); | |
123 | return 0; | |
124 | } else if (strcmp(ident->member, s+1) != 0) { | |
125 | if (devname) | |
126 | fprintf(stderr, Name ": skipping wrong member %s is %s\n", | |
127 | content->text_version, devname); | |
128 | return 0; | |
129 | } | |
130 | } | |
08fb91a3 N |
131 | return 1; |
132 | } | |
133 | ||
134 | ||
7f91af49 | 135 | int Assemble(struct supertype *st, char *mddev, |
fa56eddb | 136 | struct mddev_ident *ident, |
87f26d14 N |
137 | struct mddev_dev *devlist, |
138 | char *backup_file, int invalid_backup, | |
64c4757e | 139 | int readonly, int runstop, |
0ac91628 | 140 | char *update, char *homehost, int require_homehost, |
b76b30e0 | 141 | int verbose, int force, int freeze_reshape) |
64c4757e NB |
142 | { |
143 | /* | |
52826846 NB |
144 | * The task of Assemble is to find a collection of |
145 | * devices that should (according to their superblocks) | |
146 | * form an array, and to give this collection to the MD driver. | |
147 | * In Linux-2.4 and later, this involves submitting a | |
64c4757e NB |
148 | * SET_ARRAY_INFO ioctl with no arg - to prepare |
149 | * the array - and then submit a number of | |
150 | * ADD_NEW_DISK ioctls to add disks into | |
151 | * the array. Finally RUN_ARRAY might | |
152 | * be submitted to start the array. | |
153 | * | |
154 | * Much of the work of Assemble is in finding and/or | |
155 | * checking the disks to make sure they look right. | |
156 | * | |
4b1ac34b | 157 | * If mddev is not set, then scan must be set and we |
64c4757e NB |
158 | * read through the config file for dev+uuid mapping |
159 | * We recurse, setting mddev, for each device that | |
160 | * - isn't running | |
4b1ac34b | 161 | * - has a valid uuid (or any uuid if !uuidset) |
64c4757e NB |
162 | * |
163 | * If mddev is set, we try to determine state of md. | |
164 | * check version - must be at least 0.90.0 | |
165 | * check kernel version. must be at least 2.4. | |
166 | * If not, we can possibly fall back on START_ARRAY | |
167 | * Try to GET_ARRAY_INFO. | |
168 | * If possible, give up | |
169 | * If not, try to STOP_ARRAY just to make sure | |
170 | * | |
171 | * If !uuidset and scan, look in conf-file for uuid | |
172 | * If not found, give up | |
b8a8ccf9 | 173 | * If !devlist and scan and uuidset, get list of devs from conf-file |
64c4757e NB |
174 | * |
175 | * For each device: | |
176 | * Check superblock - discard if bad | |
177 | * Check uuid (set if we don't have one) - discard if no match | |
5787fa49 | 178 | * Check superblock similarity if we have a superblock - discard if different |
4b1ac34b | 179 | * Record events, devicenum |
64c4757e | 180 | * This should give us a list of devices for the array |
4b1ac34b | 181 | * We should collect the most recent event number |
64c4757e NB |
182 | * |
183 | * Count disks with recent enough event count | |
184 | * While force && !enough disks | |
185 | * Choose newest rejected disks, update event count | |
186 | * mark clean and rewrite superblock | |
187 | * If recent kernel: | |
188 | * SET_ARRAY_INFO | |
189 | * foreach device with recent events : ADD_NEW_DISK | |
190 | * if runstop == 1 || "enough" disks and runstop==0 -> RUN_ARRAY | |
191 | * If old kernel: | |
192 | * Check the device numbers in superblock are right | |
193 | * update superblock if any changes | |
194 | * START_ARRAY | |
195 | * | |
196 | */ | |
c30e5369 N |
197 | int mdfd; |
198 | int clean; | |
f05641cf N |
199 | int auto_assem = (mddev == NULL && !ident->uuid_set && |
200 | ident->super_minor == UnSet && ident->name[0] == 0 | |
aa7c284c | 201 | && (ident->container == NULL || ident->member == NULL)); |
64c4757e | 202 | int old_linux = 0; |
c30e5369 | 203 | int vers = vers; /* Keep gcc quite - it really is initialised */ |
64c4757e NB |
204 | struct { |
205 | char *devname; | |
213ee40b NB |
206 | int uptodate; /* set once we decide that this device is as |
207 | * recent as everything else in the array. | |
208 | */ | |
209 | struct mdinfo i; | |
5787fa49 | 210 | } *devices; |
02e7c5b7 | 211 | char *devmap; |
56eedc1a | 212 | int *best = NULL; /* indexed by raid_disk */ |
f21e18ca | 213 | int bestcnt = 0; |
98c6faba | 214 | int devcnt = 0; |
1ff98339 | 215 | unsigned int okcnt, sparecnt, rebuilding_cnt; |
98c6faba | 216 | unsigned int req_cnt; |
f21e18ca | 217 | int i; |
64c4757e | 218 | int most_recent = 0; |
cd29a5c8 | 219 | int chosen_drive; |
52826846 | 220 | int change = 0; |
cd29a5c8 | 221 | int inargv = 0; |
52437b4f | 222 | int report_missmatch; |
a6482415 | 223 | #ifndef MDASSEMBLE |
bf4fb153 | 224 | int bitmap_done; |
a6482415 | 225 | #endif |
7f91af49 N |
226 | int start_partial_ok = (runstop >= 0) && |
227 | (force || devlist==NULL || auto_assem); | |
98c6faba | 228 | unsigned int num_devs; |
a655e550 | 229 | struct mddev_dev *tmpdev; |
4b1ac34b | 230 | struct mdinfo info; |
98dbd966 | 231 | struct mdinfo *content = NULL; |
265e0f17 | 232 | char *avail; |
308e1801 | 233 | int nextspare = 0; |
197e3eb6 | 234 | char *name = NULL; |
69207ff6 | 235 | int trustworthy; |
a04d5763 | 236 | char chosen_name[1024]; |
cbeeb0e5 | 237 | struct domainlist *domains = NULL; |
8a46fe84 | 238 | |
682c7051 | 239 | if (get_linux_version() < 2004000) |
64c4757e NB |
240 | old_linux = 1; |
241 | ||
64c4757e | 242 | /* |
52826846 NB |
243 | * If any subdevs are listed, then any that don't |
244 | * match ident are discarded. Remainder must all match and | |
245 | * become the array. | |
246 | * If no subdevs, then we scan all devices in the config file, but | |
247 | * there must be something in the identity | |
64c4757e | 248 | */ |
64c4757e | 249 | |
cd29a5c8 | 250 | if (!devlist && |
52826846 | 251 | ident->uuid_set == 0 && |
f21e18ca | 252 | (ident->super_minor < 0 || ident->super_minor == UnSet) && |
e0fe762a N |
253 | ident->name[0] == 0 && |
254 | (ident->container == NULL || ident->member == NULL) && | |
52826846 NB |
255 | ident->devices == NULL) { |
256 | fprintf(stderr, Name ": No identity information available for %s - cannot assemble.\n", | |
8a46fe84 | 257 | mddev ? mddev : "further assembly"); |
52826846 | 258 | return 1; |
64c4757e | 259 | } |
71d60c48 | 260 | |
cd29a5c8 | 261 | if (devlist == NULL) |
8aec876d | 262 | devlist = conf_get_devs(); |
7f91af49 | 263 | else if (mddev) |
da6b5ca9 | 264 | inargv = 1; |
64c4757e | 265 | |
52437b4f | 266 | report_missmatch = ((inargv && verbose >= 0) || verbose > 0); |
60e1bc1a | 267 | try_again: |
c30e5369 N |
268 | /* We come back here when doing auto-assembly and attempting some |
269 | * set of devices failed. Those are now marked as ->used==2 and | |
270 | * we ignore them and try again | |
271 | */ | |
60e1bc1a | 272 | |
5787fa49 NB |
273 | tmpdev = devlist; num_devs = 0; |
274 | while (tmpdev) { | |
da6b5ca9 NB |
275 | if (tmpdev->used) |
276 | tmpdev->used = 2; | |
277 | else | |
278 | num_devs++; | |
5787fa49 NB |
279 | tmpdev = tmpdev->next; |
280 | } | |
5787fa49 | 281 | |
82d9eba6 | 282 | if (!st && ident->st) st = ident->st; |
64c4757e | 283 | |
dab6685f | 284 | if (verbose>0) |
82b27616 | 285 | fprintf(stderr, Name ": looking for devices for %s\n", |
8a46fe84 | 286 | mddev ? mddev : "further assembly"); |
82b27616 | 287 | |
811e6cbe NB |
288 | /* first walk the list of devices to find a consistent set |
289 | * that match the criterea, if that is possible. | |
c30e5369 | 290 | * We flag the ones we like with 'used'. |
811e6cbe NB |
291 | */ |
292 | for (tmpdev = devlist; | |
293 | tmpdev; | |
5083d66b | 294 | tmpdev = tmpdev ? tmpdev->next : NULL) { |
811e6cbe | 295 | char *devname = tmpdev->devname; |
64c4757e NB |
296 | int dfd; |
297 | struct stat stb; | |
518a60f3 | 298 | struct supertype *tst; |
4e8d9f0a | 299 | struct dev_policy *pol = NULL; |
5083d66b | 300 | int found_container = 0; |
52826846 | 301 | |
da6b5ca9 NB |
302 | if (tmpdev->used > 1) continue; |
303 | ||
52826846 | 304 | if (ident->devices && |
56eedc1a | 305 | !match_oneof(ident->devices, devname)) { |
52437b4f | 306 | if (report_missmatch) |
56eedc1a | 307 | fprintf(stderr, Name ": %s is not one of %s\n", devname, ident->devices); |
52826846 | 308 | continue; |
56eedc1a | 309 | } |
4b1ac34b | 310 | |
518a60f3 JS |
311 | tst = dup_super(st); |
312 | ||
8b0dabea | 313 | dfd = dev_open(devname, O_RDONLY|O_EXCL); |
64c4757e | 314 | if (dfd < 0) { |
52437b4f | 315 | if (report_missmatch) |
682c7051 | 316 | fprintf(stderr, Name ": cannot open device %s: %s\n", |
64c4757e | 317 | devname, strerror(errno)); |
da6b5ca9 | 318 | tmpdev->used = 2; |
52826846 NB |
319 | } else if (fstat(dfd, &stb)< 0) { |
320 | /* Impossible! */ | |
321 | fprintf(stderr, Name ": fstat failed for %s: %s\n", | |
322 | devname, strerror(errno)); | |
da6b5ca9 | 323 | tmpdev->used = 2; |
cd29a5c8 NB |
324 | } else if ((stb.st_mode & S_IFMT) != S_IFBLK) { |
325 | fprintf(stderr, Name ": %s is not a block device.\n", | |
52826846 | 326 | devname); |
da6b5ca9 | 327 | tmpdev->used = 2; |
5083d66b N |
328 | } else if (must_be_container(dfd)) { |
329 | if (st) { | |
330 | /* already found some components, this cannot | |
331 | * be another one. | |
332 | */ | |
333 | if (report_missmatch) | |
334 | fprintf(stderr, Name ": %s is a container, but we are looking for components\n", | |
335 | devname); | |
336 | tmpdev->used = 2; | |
71204a50 | 337 | #if !defined(MDASSEMBLE) || defined(MDASSEMBLE) && defined(MDASSEMBLE_AUTO) |
5083d66b N |
338 | } if (!tst && (tst = super_by_fd(dfd, NULL)) == NULL) { |
339 | if (report_missmatch) | |
340 | fprintf(stderr, Name ": not a recognisable container: %s\n", | |
341 | devname); | |
342 | tmpdev->used = 2; | |
71204a50 | 343 | #endif |
417f346e HCP |
344 | } else if (!tst->ss->load_container |
345 | || tst->ss->load_container(tst, dfd, NULL)) { | |
5083d66b N |
346 | if (report_missmatch) |
347 | fprintf(stderr, Name ": no correct container type: %s\n", | |
348 | devname); | |
349 | tmpdev->used = 2; | |
350 | } else if (auto_assem && | |
351 | !conf_test_metadata(tst->ss->name, (pol = devnum_policy(stb.st_rdev)), | |
352 | tst->ss->match_home(tst, homehost) == 1)) { | |
353 | if (report_missmatch) | |
354 | fprintf(stderr, Name ": %s has metadata type %s for which " | |
355 | "auto-assembly is disabled\n", | |
356 | devname, tst->ss->name); | |
357 | tmpdev->used = 2; | |
358 | } else | |
359 | found_container = 1; | |
52826846 | 360 | } else { |
5083d66b N |
361 | if (!tst && (tst = guess_super(dfd)) == NULL) { |
362 | if (report_missmatch) | |
363 | fprintf(stderr, Name ": no recogniseable superblock on %s\n", | |
364 | devname); | |
365 | tmpdev->used = 2; | |
366 | } else if (tst->ss->load_super(tst,dfd, NULL)) { | |
367 | if (report_missmatch) | |
368 | fprintf(stderr, Name ": no RAID superblock on %s\n", | |
369 | devname); | |
370 | tmpdev->used = 2; | |
371 | } else if (tst->ss->compare_super == NULL) { | |
372 | if (report_missmatch) | |
373 | fprintf(stderr, Name ": Cannot assemble %s metadata on %s\n", | |
374 | tst->ss->name, devname); | |
375 | tmpdev->used = 2; | |
376 | } else if (auto_assem && st == NULL && | |
377 | !conf_test_metadata(tst->ss->name, (pol = devnum_policy(stb.st_rdev)), | |
378 | tst->ss->match_home(tst, homehost) == 1)) { | |
379 | if (report_missmatch) | |
380 | fprintf(stderr, Name ": %s has metadata type %s for which " | |
381 | "auto-assembly is disabled\n", | |
382 | devname, tst->ss->name); | |
383 | tmpdev->used = 2; | |
384 | } | |
64c4757e | 385 | } |
c06487ce | 386 | if (dfd >= 0) close(dfd); |
d68ea4d7 | 387 | if (tmpdev->used == 2) { |
d4386799 | 388 | if (auto_assem || !inargv) |
d68ea4d7 N |
389 | /* Ignore unrecognised devices during auto-assembly */ |
390 | goto loop; | |
391 | if (ident->uuid_set || ident->name[0] || | |
392 | ident->super_minor != UnSet) | |
393 | /* Ignore unrecognised device if looking for | |
394 | * specific array */ | |
395 | goto loop; | |
396 | ||
397 | ||
398 | fprintf(stderr, Name ": %s has no superblock - assembly aborted\n", | |
399 | devname); | |
400 | if (st) | |
401 | st->ss->free_super(st); | |
402 | dev_policy_free(pol); | |
cbeeb0e5 | 403 | domain_free(domains); |
d68ea4d7 N |
404 | return 1; |
405 | } | |
52826846 | 406 | |
5083d66b | 407 | if (found_container) { |
9008ed1c N |
408 | /* tmpdev is a container. We need to be either |
409 | * looking for a member, or auto-assembling | |
410 | */ | |
9008ed1c N |
411 | |
412 | if (ident->container) { | |
413 | if (ident->container[0] == '/' && | |
414 | !same_dev(ident->container, devname)) { | |
415 | if (report_missmatch) | |
416 | fprintf(stderr, Name ": %s is not the container required (%s)\n", | |
417 | devname, ident->container); | |
418 | goto loop; | |
419 | } | |
420 | if (ident->container[0] != '/') { | |
421 | /* we have a uuid */ | |
422 | int uuid[4]; | |
87477e6d N |
423 | |
424 | content = &info; | |
87477e6d N |
425 | tst->ss->getinfo_super(tst, content, NULL); |
426 | ||
9008ed1c N |
427 | if (!parse_uuid(ident->container, uuid) || |
428 | !same_uuid(content->uuid, uuid, tst->ss->swapuuid)) { | |
429 | if (report_missmatch) | |
430 | fprintf(stderr, Name ": %s has wrong UUID to be required container\n", | |
431 | devname); | |
432 | goto loop; | |
433 | } | |
434 | } | |
435 | } | |
436 | /* It is worth looking inside this container. | |
437 | */ | |
7636b5a8 N |
438 | if (verbose > 0) |
439 | fprintf(stderr, Name ": looking in container %s\n", | |
440 | devname); | |
1415fe4b | 441 | |
88cef9b3 N |
442 | for (content = tst->ss->container_content(tst, NULL); |
443 | content; | |
444 | content = content->next) { | |
1415fe4b | 445 | |
88cef9b3 N |
446 | if (!ident_matches(ident, content, tst, |
447 | homehost, update, | |
448 | report_missmatch ? devname : NULL)) | |
449 | /* message already printed */; | |
450 | else if (is_member_busy(content->text_version)) { | |
451 | if (report_missmatch) | |
452 | fprintf(stderr, Name ": member %s in %s is already assembled\n", | |
453 | content->text_version, | |
454 | devname); | |
81219e70 LM |
455 | } else if (content->array.state & (1<<MD_SB_BLOCK_VOLUME)) { |
456 | /* do not assemble arrays with unsupported configurations */ | |
457 | fprintf(stderr, Name ": Cannot activate member %s in %s.\n", | |
458 | content->text_version, | |
459 | devname); | |
88cef9b3 N |
460 | } else |
461 | break; | |
462 | } | |
463 | if (!content) { | |
f7ad3ccc | 464 | tmpdev->used = 2; |
88cef9b3 | 465 | goto loop; /* empty container */ |
fa031239 | 466 | } |
88cef9b3 | 467 | |
9008ed1c | 468 | st = tst; tst = NULL; |
4c1c3ad8 | 469 | if (!auto_assem && inargv && tmpdev->next != NULL) { |
9008ed1c N |
470 | fprintf(stderr, Name ": %s is a container, but is not " |
471 | "only device given: confused and aborting\n", | |
472 | devname); | |
473 | st->ss->free_super(st); | |
4e8d9f0a | 474 | dev_policy_free(pol); |
cbeeb0e5 | 475 | domain_free(domains); |
9008ed1c N |
476 | return 1; |
477 | } | |
7636b5a8 N |
478 | if (verbose > 0) |
479 | fprintf(stderr, Name ": found match on member %s in %s\n", | |
480 | content->text_version, devname); | |
bac0d92e | 481 | |
5083d66b N |
482 | /* make sure we finished the loop */ |
483 | tmpdev = NULL; | |
bac0d92e | 484 | goto loop; |
5083d66b | 485 | } else { |
bac0d92e | 486 | |
5083d66b | 487 | content = &info; |
5083d66b N |
488 | tst->ss->getinfo_super(tst, content, NULL); |
489 | ||
490 | if (!ident_matches(ident, content, tst, | |
491 | homehost, update, | |
492 | report_missmatch ? devname : NULL)) | |
df37ffc0 | 493 | goto loop; |
cbeeb0e5 | 494 | |
5083d66b N |
495 | if (st == NULL) |
496 | st = dup_super(tst); | |
497 | if (st->minor_version == -1) | |
498 | st->minor_version = tst->minor_version; | |
ed7fc6b4 AC |
499 | |
500 | if (memcmp(content->uuid, uuid_zero, | |
501 | sizeof(int[4])) == 0) { | |
502 | /* this is a floating spare. It cannot define | |
503 | * an array unless there are no more arrays of | |
504 | * this type to be found. It can be included | |
505 | * in an array of this type though. | |
506 | */ | |
507 | tmpdev->used = 3; | |
508 | goto loop; | |
509 | } | |
510 | ||
5083d66b N |
511 | if (st->ss != tst->ss || |
512 | st->minor_version != tst->minor_version || | |
513 | st->ss->compare_super(st, tst) != 0) { | |
514 | /* Some mismatch. If exactly one array matches this host, | |
515 | * we can resolve on that one. | |
516 | * Or, if we are auto assembling, we just ignore the second | |
517 | * for now. | |
518 | */ | |
519 | if (auto_assem) | |
520 | goto loop; | |
521 | if (homehost) { | |
522 | int first = st->ss->match_home(st, homehost); | |
523 | int last = tst->ss->match_home(tst, homehost); | |
524 | if (first != last && | |
525 | (first == 1 || last == 1)) { | |
526 | /* We can do something */ | |
527 | if (first) {/* just ignore this one */ | |
528 | if (report_missmatch) | |
529 | fprintf(stderr, Name ": %s misses out due to wrong homehost\n", | |
530 | devname); | |
531 | goto loop; | |
532 | } else { /* reject all those sofar */ | |
533 | struct mddev_dev *td; | |
534 | if (report_missmatch) | |
535 | fprintf(stderr, Name ": %s overrides previous devices due to good homehost\n", | |
536 | devname); | |
537 | for (td=devlist; td != tmpdev; td=td->next) | |
538 | if (td->used == 1) | |
539 | td->used = 0; | |
540 | tmpdev->used = 1; | |
541 | goto loop; | |
542 | } | |
83b6208e NB |
543 | } |
544 | } | |
5083d66b N |
545 | fprintf(stderr, Name ": superblock on %s doesn't match others - assembly aborted\n", |
546 | devname); | |
547 | tst->ss->free_super(tst); | |
548 | st->ss->free_super(st); | |
549 | dev_policy_free(pol); | |
cbeeb0e5 | 550 | domain_free(domains); |
5083d66b | 551 | return 1; |
83b6208e | 552 | } |
5083d66b | 553 | tmpdev->used = 1; |
64c4757e | 554 | } |
df37ffc0 | 555 | loop: |
cbeeb0e5 | 556 | /* Collect domain information from members only */ |
26b05aea AC |
557 | if (tmpdev && tmpdev->used == 1) { |
558 | if (!pol) | |
559 | pol = devnum_policy(stb.st_rdev); | |
cbeeb0e5 | 560 | domain_merge(&domains, pol, tst?tst->ss->name:NULL); |
26b05aea | 561 | } |
4e8d9f0a N |
562 | dev_policy_free(pol); |
563 | pol = NULL; | |
3d2b16e7 NB |
564 | if (tst) |
565 | tst->ss->free_super(tst); | |
811e6cbe NB |
566 | } |
567 | ||
ed7fc6b4 | 568 | /* Check if we found some imsm spares but no members */ |
3c7b4a25 CA |
569 | if ((auto_assem || |
570 | (ident->uuid_set && | |
571 | memcmp(uuid_zero, ident->uuid,sizeof(uuid_zero)) == 0)) && | |
572 | (!st || !st->sb)) | |
ed7fc6b4 AC |
573 | for (tmpdev = devlist; tmpdev; tmpdev = tmpdev->next) { |
574 | if (tmpdev->used != 3) | |
575 | continue; | |
576 | tmpdev->used = 1; | |
577 | content = &info; | |
578 | ||
579 | if (!st->sb) { | |
580 | /* we need sb from one of the spares */ | |
581 | int dfd = dev_open(tmpdev->devname, O_RDONLY); | |
582 | if (dfd < 0 || | |
583 | st->ss->load_super(st, dfd, NULL)) | |
584 | tmpdev->used = 2; | |
585 | if (dfd > 0) | |
586 | close(dfd); | |
587 | } | |
588 | } | |
589 | ||
cbeeb0e5 AC |
590 | /* Now reject spares that don't match domains of identified members */ |
591 | for (tmpdev = devlist; tmpdev; tmpdev = tmpdev->next) { | |
592 | struct stat stb; | |
593 | if (tmpdev->used != 3) | |
594 | continue; | |
595 | if (stat(tmpdev->devname, &stb)< 0) { | |
596 | fprintf(stderr, Name ": fstat failed for %s: %s\n", | |
597 | tmpdev->devname, strerror(errno)); | |
598 | tmpdev->used = 2; | |
599 | } else { | |
a5d10dce N |
600 | struct dev_policy *pol = devnum_policy(stb.st_rdev); |
601 | int dt = domain_test(domains, pol, NULL); | |
602 | if (inargv && dt != 0) | |
603 | /* take this spare as domains match | |
604 | * if there are any */ | |
605 | tmpdev->used = 1; | |
606 | else if (!inargv && dt == 1) | |
607 | /* device wasn't explicitly listed, so need | |
608 | * explicit domain match - which we have */ | |
cbeeb0e5 AC |
609 | tmpdev->used = 1; |
610 | else | |
611 | /* if domains don't match mark as unused */ | |
612 | tmpdev->used = 0; | |
613 | dev_policy_free(pol); | |
614 | } | |
615 | } | |
616 | domain_free(domains); | |
617 | ||
98dbd966 | 618 | if (!st || !st->sb || !content) |
c30e5369 N |
619 | return 2; |
620 | ||
05833051 | 621 | /* Now need to open the array device. Use create_mddev */ |
98dbd966 | 622 | if (content == &info) |
a5d85af7 | 623 | st->ss->getinfo_super(st, content, NULL); |
3d2c4fc7 | 624 | |
69207ff6 | 625 | trustworthy = FOREIGN; |
05833051 | 626 | name = content->name; |
745f72f6 N |
627 | switch (st->ss->match_home(st, homehost) |
628 | ?: st->ss->match_home(st, "any")) { | |
69207ff6 N |
629 | case 1: |
630 | trustworthy = LOCAL; | |
98dbd966 | 631 | name = strchr(content->name, ':'); |
69207ff6 N |
632 | if (name) |
633 | name++; | |
3d2c4fc7 | 634 | else |
98dbd966 | 635 | name = content->name; |
69207ff6 | 636 | break; |
c30e5369 | 637 | } |
05833051 | 638 | if (!auto_assem) |
aa7c284c | 639 | /* If the array is listed in mdadm.conf or on |
ac2ecf55 N |
640 | * command line, then we trust the name |
641 | * even if the array doesn't look local | |
642 | */ | |
643 | trustworthy = LOCAL; | |
644 | ||
05833051 | 645 | if (name[0] == 0 && |
98dbd966 DW |
646 | content->array.level == LEVEL_CONTAINER) { |
647 | name = content->text_version; | |
a4bc1720 N |
648 | trustworthy = METADATA; |
649 | } | |
0ac91628 N |
650 | |
651 | if (name[0] && trustworthy != LOCAL && | |
652 | ! require_homehost && | |
653 | conf_name_is_free(name)) | |
654 | trustworthy = LOCAL; | |
655 | ||
7cdc0872 N |
656 | if (trustworthy == LOCAL && |
657 | strchr(name, ':')) | |
658 | /* Ignore 'host:' prefix of name */ | |
659 | name = strchr(name, ':')+1; | |
660 | ||
a04d5763 N |
661 | mdfd = create_mddev(mddev, name, ident->autof, trustworthy, |
662 | chosen_name); | |
c30e5369 N |
663 | if (mdfd < 0) { |
664 | st->ss->free_super(st); | |
c30e5369 | 665 | if (auto_assem) |
60e1bc1a | 666 | goto try_again; |
c30e5369 N |
667 | return 1; |
668 | } | |
a04d5763 | 669 | mddev = chosen_name; |
c30e5369 N |
670 | vers = md_get_version(mdfd); |
671 | if (vers < 9000) { | |
672 | fprintf(stderr, Name ": Assemble requires driver version 0.90.0 or later.\n" | |
673 | " Upgrade your kernel or try --build\n"); | |
674 | close(mdfd); | |
675 | return 1; | |
676 | } | |
66afdfa9 | 677 | if (mddev_busy(fd2devnum(mdfd))) { |
c30e5369 N |
678 | fprintf(stderr, Name ": %s already active, cannot restart it!\n", |
679 | mddev); | |
680 | for (tmpdev = devlist ; | |
681 | tmpdev && tmpdev->used != 1; | |
682 | tmpdev = tmpdev->next) | |
683 | ; | |
684 | if (tmpdev && auto_assem) | |
685 | fprintf(stderr, Name ": %s needed for %s...\n", | |
686 | mddev, tmpdev->devname); | |
687 | close(mdfd); | |
688 | mdfd = -3; | |
689 | st->ss->free_super(st); | |
c30e5369 N |
690 | if (auto_assem) |
691 | goto try_again; | |
692 | return 1; | |
8a46fe84 | 693 | } |
c30e5369 | 694 | ioctl(mdfd, STOP_ARRAY, NULL); /* just incase it was started but has no content */ |
8a46fe84 | 695 | |
9008ed1c N |
696 | #ifndef MDASSEMBLE |
697 | if (content != &info) { | |
698 | /* This is a member of a container. Try starting the array. */ | |
588bebfc AK |
699 | int err; |
700 | err = assemble_container_content(st, mdfd, content, runstop, | |
49680258 | 701 | chosen_name, verbose, |
b76b30e0 | 702 | backup_file, freeze_reshape); |
588bebfc | 703 | close(mdfd); |
588bebfc | 704 | return err; |
9008ed1c | 705 | } |
a6482415 | 706 | bitmap_done = 0; |
9008ed1c | 707 | #endif |
811e6cbe | 708 | /* Ok, no bad inconsistancy, we can try updating etc */ |
d7f7ebb7 | 709 | devices = malloc(num_devs * sizeof(*devices)); |
02e7c5b7 | 710 | devmap = calloc(num_devs * content->array.raid_disks, 1); |
da6b5ca9 | 711 | for (tmpdev = devlist; tmpdev; tmpdev=tmpdev->next) if (tmpdev->used == 1) { |
811e6cbe NB |
712 | char *devname = tmpdev->devname; |
713 | struct stat stb; | |
5787fa49 | 714 | /* looks like a good enough match to update the super block if needed */ |
d1f1011b | 715 | #ifndef MDASSEMBLE |
5787fa49 | 716 | if (update) { |
811e6cbe | 717 | int dfd; |
4b1ac34b NB |
718 | /* prepare useful information in info structures */ |
719 | struct stat stb2; | |
3da92f27 | 720 | struct supertype *tst; |
1e2b2765 | 721 | int err; |
4b1ac34b | 722 | fstat(mdfd, &stb2); |
7d99579f NB |
723 | |
724 | if (strcmp(update, "uuid")==0 && | |
725 | !ident->uuid_set) { | |
726 | int rfd; | |
727 | if ((rfd = open("/dev/urandom", O_RDONLY)) < 0 || | |
728 | read(rfd, ident->uuid, 16) != 16) { | |
729 | *(__u32*)(ident->uuid) = random(); | |
730 | *(__u32*)(ident->uuid+1) = random(); | |
731 | *(__u32*)(ident->uuid+2) = random(); | |
732 | *(__u32*)(ident->uuid+3) = random(); | |
733 | } | |
734 | if (rfd >= 0) close(rfd); | |
7d99579f | 735 | } |
811e6cbe NB |
736 | dfd = dev_open(devname, O_RDWR|O_EXCL); |
737 | ||
3da92f27 | 738 | tst = dup_super(st); |
9f22b13f N |
739 | if (dfd < 0 || tst->ss->load_super(tst, dfd, NULL) != 0) { |
740 | fprintf(stderr, Name ": cannot re-read metadata from %s - aborting\n", | |
741 | devname); | |
742 | if (dfd >= 0) | |
743 | close(dfd); | |
744 | close(mdfd); | |
d7f7ebb7 | 745 | free(devices); |
02e7c5b7 | 746 | free(devmap); |
9f22b13f N |
747 | return 1; |
748 | } | |
02e7c5b7 | 749 | tst->ss->getinfo_super(tst, content, devmap + devcnt * content->array.raid_disks); |
811e6cbe | 750 | |
98dbd966 DW |
751 | memcpy(content->uuid, ident->uuid, 16); |
752 | strcpy(content->name, ident->name); | |
753 | content->array.md_minor = minor(stb2.st_rdev); | |
811e6cbe | 754 | |
1e2b2765 N |
755 | if (strcmp(update, "byteorder") == 0) |
756 | err = 0; | |
757 | else | |
758 | err = tst->ss->update_super(tst, content, update, | |
759 | devname, verbose, | |
760 | ident->uuid_set, | |
761 | homehost); | |
762 | if (err < 0) { | |
763 | fprintf(stderr, | |
764 | Name ": --update=%s not understood" | |
765 | " for %s metadata\n", | |
766 | update, tst->ss->name); | |
767 | tst->ss->free_super(tst); | |
768 | free(tst); | |
769 | close(mdfd); | |
770 | close(dfd); | |
d7f7ebb7 | 771 | free(devices); |
02e7c5b7 | 772 | free(devmap); |
1e2b2765 N |
773 | return 1; |
774 | } | |
e5eac01f NB |
775 | if (strcmp(update, "uuid")==0 && |
776 | !ident->uuid_set) { | |
777 | ident->uuid_set = 1; | |
98dbd966 | 778 | memcpy(ident->uuid, content->uuid, 16); |
e5eac01f | 779 | } |
1e2b2765 | 780 | if (tst->ss->store_super(tst, dfd)) |
5787fa49 NB |
781 | fprintf(stderr, Name ": Could not re-write superblock on %s.\n", |
782 | devname); | |
1e2b2765 | 783 | close(dfd); |
8131b493 NB |
784 | |
785 | if (strcmp(update, "uuid")==0 && | |
bf4fb153 | 786 | ident->bitmap_fd >= 0 && !bitmap_done) { |
3da92f27 | 787 | if (bitmap_update_uuid(ident->bitmap_fd, |
98dbd966 | 788 | content->uuid, |
3da92f27 | 789 | tst->ss->swapuuid) != 0) |
bf4fb153 NB |
790 | fprintf(stderr, Name ": Could not update uuid on external bitmap.\n"); |
791 | else | |
792 | bitmap_done = 1; | |
793 | } | |
3da92f27 | 794 | tst->ss->free_super(tst); |
d1f1011b NB |
795 | } else |
796 | #endif | |
797 | { | |
30e1b9a5 | 798 | struct supertype *tst = dup_super(st); |
da6b5ca9 NB |
799 | int dfd; |
800 | dfd = dev_open(devname, O_RDWR|O_EXCL); | |
801 | ||
9f22b13f N |
802 | if (dfd < 0 || tst->ss->load_super(tst, dfd, NULL) != 0) { |
803 | fprintf(stderr, Name ": cannot re-read metadata from %s - aborting\n", | |
804 | devname); | |
805 | if (dfd >= 0) | |
806 | close(dfd); | |
807 | close(mdfd); | |
d7f7ebb7 | 808 | free(devices); |
02e7c5b7 | 809 | free(devmap); |
9f22b13f N |
810 | return 1; |
811 | } | |
02e7c5b7 | 812 | tst->ss->getinfo_super(tst, content, devmap + devcnt * content->array.raid_disks); |
3da92f27 | 813 | tst->ss->free_super(tst); |
da6b5ca9 | 814 | close(dfd); |
5787fa49 NB |
815 | } |
816 | ||
811e6cbe NB |
817 | stat(devname, &stb); |
818 | ||
dab6685f | 819 | if (verbose > 0) |
cd29a5c8 | 820 | fprintf(stderr, Name ": %s is identified as a member of %s, slot %d.\n", |
98dbd966 | 821 | devname, mddev, content->disk.raid_disk); |
64c4757e | 822 | devices[devcnt].devname = devname; |
64c4757e | 823 | devices[devcnt].uptodate = 0; |
98dbd966 | 824 | devices[devcnt].i = *content; |
213ee40b NB |
825 | devices[devcnt].i.disk.major = major(stb.st_rdev); |
826 | devices[devcnt].i.disk.minor = minor(stb.st_rdev); | |
64c4757e | 827 | if (most_recent < devcnt) { |
213ee40b NB |
828 | if (devices[devcnt].i.events |
829 | > devices[most_recent].i.events) | |
64c4757e NB |
830 | most_recent = devcnt; |
831 | } | |
df0d4ea0 | 832 | if (content->array.level == LEVEL_MULTIPATH) |
5787fa49 NB |
833 | /* with multipath, the raid_disk from the superblock is meaningless */ |
834 | i = devcnt; | |
835 | else | |
213ee40b | 836 | i = devices[devcnt].i.disk.raid_disk; |
308e1801 | 837 | if (i+1 == 0) { |
98dbd966 DW |
838 | if (nextspare < content->array.raid_disks) |
839 | nextspare = content->array.raid_disks; | |
308e1801 | 840 | i = nextspare++; |
08110d41 | 841 | } else { |
98dbd966 | 842 | if (i >= content->array.raid_disks && |
08110d41 NB |
843 | i >= nextspare) |
844 | nextspare = i+1; | |
308e1801 | 845 | } |
98c6faba | 846 | if (i < 10000) { |
56eedc1a | 847 | if (i >= bestcnt) { |
f21e18ca | 848 | int newbestcnt = i+10; |
56eedc1a | 849 | int *newbest = malloc(sizeof(int)*newbestcnt); |
f21e18ca | 850 | int c; |
56eedc1a NB |
851 | for (c=0; c < newbestcnt; c++) |
852 | if (c < bestcnt) | |
853 | newbest[c] = best[c]; | |
854 | else | |
855 | newbest[c] = -1; | |
856 | if (best)free(best); | |
857 | best = newbest; | |
858 | bestcnt = newbestcnt; | |
859 | } | |
6a255b69 | 860 | if (best[i] >=0 && |
213ee40b NB |
861 | devices[best[i]].i.events |
862 | == devices[devcnt].i.events | |
863 | && (devices[best[i]].i.disk.minor | |
864 | != devices[devcnt].i.disk.minor) | |
b8ac1967 | 865 | && st->ss == &super0 |
98dbd966 | 866 | && content->array.level != LEVEL_MULTIPATH) { |
6a255b69 NB |
867 | /* two different devices with identical superblock. |
868 | * Could be a mis-detection caused by overlapping | |
869 | * partitions. fail-safe. | |
870 | */ | |
871 | fprintf(stderr, Name ": WARNING %s and %s appear" | |
872 | " to have very similar superblocks.\n" | |
873 | " If they are really different, " | |
874 | "please --zero the superblock on one\n" | |
d2df86e0 NB |
875 | " If they are the same or overlap," |
876 | " please remove one from %s.\n", | |
877 | devices[best[i]].devname, devname, | |
878 | inargv ? "the list" : | |
879 | "the\n DEVICE list in mdadm.conf" | |
880 | ); | |
7f91af49 | 881 | close(mdfd); |
d7f7ebb7 | 882 | free(devices); |
02e7c5b7 | 883 | free(devmap); |
6a255b69 NB |
884 | return 1; |
885 | } | |
52826846 | 886 | if (best[i] == -1 |
213ee40b NB |
887 | || (devices[best[i]].i.events |
888 | < devices[devcnt].i.events)) | |
52826846 | 889 | best[i] = devcnt; |
56eedc1a | 890 | } |
64c4757e | 891 | devcnt++; |
df37ffc0 | 892 | } |
4b1ac34b | 893 | |
64c4757e | 894 | if (devcnt == 0) { |
682c7051 | 895 | fprintf(stderr, Name ": no devices found for %s\n", |
64c4757e | 896 | mddev); |
3d2b16e7 NB |
897 | if (st) |
898 | st->ss->free_super(st); | |
7f91af49 | 899 | close(mdfd); |
d7f7ebb7 | 900 | free(devices); |
02e7c5b7 | 901 | free(devmap); |
64c4757e NB |
902 | return 1; |
903 | } | |
4b1ac34b | 904 | |
3d2b16e7 NB |
905 | if (update && strcmp(update, "byteorder")==0) |
906 | st->minor_version = 90; | |
907 | ||
a5d85af7 | 908 | st->ss->getinfo_super(st, content, NULL); |
98dbd966 | 909 | clean = content->array.state & 1; |
4b1ac34b | 910 | |
64c4757e NB |
911 | /* now we have some devices that might be suitable. |
912 | * I wonder how many | |
913 | */ | |
98dbd966 DW |
914 | avail = malloc(content->array.raid_disks); |
915 | memset(avail, 0, content->array.raid_disks); | |
64c4757e | 916 | okcnt = 0; |
52826846 | 917 | sparecnt=0; |
1ff98339 | 918 | rebuilding_cnt=0; |
f21e18ca | 919 | for (i=0; i< bestcnt; i++) { |
64c4757e | 920 | int j = best[i]; |
ee04451c NB |
921 | int event_margin = 1; /* always allow a difference of '1' |
922 | * like the kernel does | |
923 | */ | |
64c4757e | 924 | if (j < 0) continue; |
d013a55e NB |
925 | /* note: we ignore error flags in multipath arrays |
926 | * as they don't make sense | |
927 | */ | |
df0d4ea0 | 928 | if (content->array.level != LEVEL_MULTIPATH) |
f22385f9 | 929 | if (!(devices[j].i.disk.state & (1<<MD_DISK_ACTIVE))) { |
213ee40b | 930 | if (!(devices[j].i.disk.state |
ed7fc6b4 AC |
931 | & (1<<MD_DISK_FAULTY))) { |
932 | devices[j].uptodate = 1; | |
aa88f531 | 933 | sparecnt++; |
ed7fc6b4 | 934 | } |
d013a55e | 935 | continue; |
aa88f531 | 936 | } |
02e7c5b7 N |
937 | /* If this devices thinks that 'most_recent' has failed, then |
938 | * we must reject this device. | |
939 | */ | |
940 | if (j != most_recent && | |
941 | content->array.raid_disks > 0 && | |
942 | devices[most_recent].i.disk.raid_disk >= 0 && | |
943 | devmap[j * content->array.raid_disks + devices[most_recent].i.disk.raid_disk] == 0) { | |
944 | if (verbose > -1) | |
945 | fprintf(stderr, Name ": ignoring %s as it reports %s as failed\n", | |
946 | devices[j].devname, devices[most_recent].devname); | |
947 | best[i] = -1; | |
948 | continue; | |
949 | } | |
213ee40b NB |
950 | if (devices[j].i.events+event_margin >= |
951 | devices[most_recent].i.events) { | |
64c4757e | 952 | devices[j].uptodate = 1; |
1ff98339 | 953 | if (i < content->array.raid_disks) { |
dcc4210f DW |
954 | if (devices[j].i.recovery_start == MaxSector || |
955 | (content->reshape_active && | |
956 | j >= content->array.raid_disks - content->delta_disks)) { | |
1ff98339 N |
957 | okcnt++; |
958 | avail[i]=1; | |
959 | } else | |
960 | rebuilding_cnt++; | |
265e0f17 | 961 | } else |
52826846 | 962 | sparecnt++; |
64c4757e NB |
963 | } |
964 | } | |
02e7c5b7 | 965 | free(devmap); |
98dbd966 DW |
966 | while (force && !enough(content->array.level, content->array.raid_disks, |
967 | content->array.layout, 1, | |
265e0f17 | 968 | avail, okcnt)) { |
64c4757e NB |
969 | /* Choose the newest best drive which is |
970 | * not up-to-date, update the superblock | |
971 | * and add it. | |
972 | */ | |
52826846 | 973 | int fd; |
3da92f27 | 974 | struct supertype *tst; |
f21e18ca | 975 | unsigned long long current_events; |
cd29a5c8 | 976 | chosen_drive = -1; |
f21e18ca | 977 | for (i = 0; i < content->array.raid_disks && i < bestcnt; i++) { |
52826846 NB |
978 | int j = best[i]; |
979 | if (j>=0 && | |
980 | !devices[j].uptodate && | |
921d9e16 | 981 | devices[j].i.recovery_start == MaxSector && |
52826846 | 982 | (chosen_drive < 0 || |
213ee40b NB |
983 | devices[j].i.events |
984 | > devices[chosen_drive].i.events)) | |
52826846 NB |
985 | chosen_drive = j; |
986 | } | |
987 | if (chosen_drive < 0) | |
988 | break; | |
213ee40b | 989 | current_events = devices[chosen_drive].i.events; |
c5a6f9a6 | 990 | add_another: |
dab6685f NB |
991 | if (verbose >= 0) |
992 | fprintf(stderr, Name ": forcing event count in %s(%d) from %d upto %d\n", | |
213ee40b NB |
993 | devices[chosen_drive].devname, |
994 | devices[chosen_drive].i.disk.raid_disk, | |
995 | (int)(devices[chosen_drive].i.events), | |
996 | (int)(devices[most_recent].i.events)); | |
8b0dabea | 997 | fd = dev_open(devices[chosen_drive].devname, O_RDWR|O_EXCL); |
52826846 NB |
998 | if (fd < 0) { |
999 | fprintf(stderr, Name ": Couldn't open %s for write - not updating\n", | |
1000 | devices[chosen_drive].devname); | |
213ee40b | 1001 | devices[chosen_drive].i.events = 0; |
52826846 NB |
1002 | continue; |
1003 | } | |
3da92f27 | 1004 | tst = dup_super(st); |
60b435db | 1005 | if (tst->ss->load_super(tst,fd, NULL)) { |
52826846 NB |
1006 | close(fd); |
1007 | fprintf(stderr, Name ": RAID superblock disappeared from %s - not updating.\n", | |
1008 | devices[chosen_drive].devname); | |
213ee40b | 1009 | devices[chosen_drive].i.events = 0; |
52826846 NB |
1010 | continue; |
1011 | } | |
98dbd966 DW |
1012 | content->events = devices[most_recent].i.events; |
1013 | tst->ss->update_super(tst, content, "force-one", | |
67a8c82d NB |
1014 | devices[chosen_drive].devname, verbose, |
1015 | 0, NULL); | |
4b1ac34b | 1016 | |
3da92f27 | 1017 | if (tst->ss->store_super(tst, fd)) { |
52826846 NB |
1018 | close(fd); |
1019 | fprintf(stderr, Name ": Could not re-write superblock on %s\n", | |
1020 | devices[chosen_drive].devname); | |
213ee40b | 1021 | devices[chosen_drive].i.events = 0; |
3da92f27 | 1022 | tst->ss->free_super(tst); |
52826846 NB |
1023 | continue; |
1024 | } | |
1025 | close(fd); | |
213ee40b | 1026 | devices[chosen_drive].i.events = devices[most_recent].i.events; |
52826846 | 1027 | devices[chosen_drive].uptodate = 1; |
265e0f17 | 1028 | avail[chosen_drive] = 1; |
52826846 | 1029 | okcnt++; |
3da92f27 | 1030 | tst->ss->free_super(tst); |
c5a6f9a6 NB |
1031 | |
1032 | /* If there are any other drives of the same vintage, | |
1033 | * add them in as well. We can't lose and we might gain | |
1034 | */ | |
f21e18ca | 1035 | for (i = 0; i < content->array.raid_disks && i < bestcnt ; i++) { |
c5a6f9a6 NB |
1036 | int j = best[i]; |
1037 | if (j >= 0 && | |
1038 | !devices[j].uptodate && | |
213ee40b | 1039 | devices[j].i.events == current_events) { |
c5a6f9a6 NB |
1040 | chosen_drive = j; |
1041 | goto add_another; | |
1042 | } | |
1043 | } | |
64c4757e | 1044 | } |
52826846 NB |
1045 | |
1046 | /* Now we want to look at the superblock which the kernel will base things on | |
1047 | * and compare the devices that we think are working with the devices that the | |
1048 | * superblock thinks are working. | |
1049 | * If there are differences and --force is given, then update this chosen | |
1050 | * superblock. | |
1051 | */ | |
cd29a5c8 | 1052 | chosen_drive = -1; |
3da92f27 | 1053 | st->ss->free_super(st); |
56eedc1a | 1054 | for (i=0; chosen_drive < 0 && i<bestcnt; i++) { |
52826846 NB |
1055 | int j = best[i]; |
1056 | int fd; | |
4b1ac34b | 1057 | |
52826846 NB |
1058 | if (j<0) |
1059 | continue; | |
1060 | if (!devices[j].uptodate) | |
1061 | continue; | |
a28232b8 N |
1062 | if (devices[j].i.events < devices[most_recent].i.events) |
1063 | continue; | |
52826846 | 1064 | chosen_drive = j; |
8b0dabea | 1065 | if ((fd=dev_open(devices[j].devname, O_RDONLY|O_EXCL))< 0) { |
52826846 NB |
1066 | fprintf(stderr, Name ": Cannot open %s: %s\n", |
1067 | devices[j].devname, strerror(errno)); | |
7f91af49 | 1068 | close(mdfd); |
d7f7ebb7 | 1069 | free(devices); |
52826846 NB |
1070 | return 1; |
1071 | } | |
3da92f27 | 1072 | if (st->ss->load_super(st,fd, NULL)) { |
52826846 NB |
1073 | close(fd); |
1074 | fprintf(stderr, Name ": RAID superblock has disappeared from %s\n", | |
1075 | devices[j].devname); | |
7f91af49 | 1076 | close(mdfd); |
d7f7ebb7 | 1077 | free(devices); |
52826846 NB |
1078 | return 1; |
1079 | } | |
1080 | close(fd); | |
1081 | } | |
3da92f27 | 1082 | if (st->sb == NULL) { |
4b1ac34b | 1083 | fprintf(stderr, Name ": No suitable drives found for %s\n", mddev); |
7f91af49 | 1084 | close(mdfd); |
d7f7ebb7 | 1085 | free(devices); |
4b1ac34b NB |
1086 | return 1; |
1087 | } | |
a5d85af7 | 1088 | st->ss->getinfo_super(st, content, NULL); |
f35f2525 | 1089 | #ifndef MDASSEMBLE |
98dbd966 | 1090 | sysfs_init(content, mdfd, 0); |
f35f2525 | 1091 | #endif |
56eedc1a | 1092 | for (i=0; i<bestcnt; i++) { |
52826846 | 1093 | int j = best[i]; |
98c6faba | 1094 | unsigned int desired_state; |
11a3e71d | 1095 | |
98dbd966 | 1096 | if (i < content->array.raid_disks) |
11a3e71d NB |
1097 | desired_state = (1<<MD_DISK_ACTIVE) | (1<<MD_DISK_SYNC); |
1098 | else | |
1099 | desired_state = 0; | |
1100 | ||
52826846 NB |
1101 | if (j<0) |
1102 | continue; | |
1103 | if (!devices[j].uptodate) | |
1104 | continue; | |
4b1ac34b | 1105 | |
213ee40b | 1106 | devices[j].i.disk.state = desired_state; |
4e9a6ff7 N |
1107 | if (!(devices[j].i.array.state & 1)) |
1108 | clean = 0; | |
213ee40b NB |
1109 | |
1110 | if (st->ss->update_super(st, &devices[j].i, "assemble", NULL, | |
68c7d6d7 | 1111 | verbose, 0, NULL)) { |
52826846 | 1112 | if (force) { |
dab6685f NB |
1113 | if (verbose >= 0) |
1114 | fprintf(stderr, Name ": " | |
1115 | "clearing FAULTY flag for device %d in %s for %s\n", | |
1116 | j, mddev, devices[j].devname); | |
4b1ac34b | 1117 | change = 1; |
52826846 | 1118 | } else { |
dab6685f NB |
1119 | if (verbose >= -1) |
1120 | fprintf(stderr, Name ": " | |
1121 | "device %d in %s has wrong state in superblock, but %s seems ok\n", | |
1122 | i, mddev, devices[j].devname); | |
52826846 NB |
1123 | } |
1124 | } | |
4b1ac34b | 1125 | #if 0 |
213ee40b | 1126 | if (!(super.disks[i].i.disk.state & (1 << MD_DISK_FAULTY))) { |
52826846 NB |
1127 | fprintf(stderr, Name ": devices %d of %s is not marked FAULTY in superblock, but cannot be found\n", |
1128 | i, mddev); | |
1129 | } | |
4b1ac34b | 1130 | #endif |
52826846 | 1131 | } |
c5a6f9a6 | 1132 | if (force && !clean && |
98dbd966 DW |
1133 | !enough(content->array.level, content->array.raid_disks, |
1134 | content->array.layout, clean, | |
c5a6f9a6 | 1135 | avail, okcnt)) { |
98dbd966 | 1136 | change += st->ss->update_super(st, content, "force-array", |
67a8c82d NB |
1137 | devices[chosen_drive].devname, verbose, |
1138 | 0, NULL); | |
583315d9 | 1139 | clean = 1; |
d013a55e | 1140 | } |
52826846 | 1141 | |
4b1ac34b | 1142 | if (change) { |
52826846 | 1143 | int fd; |
8b0dabea | 1144 | fd = dev_open(devices[chosen_drive].devname, O_RDWR|O_EXCL); |
52826846 | 1145 | if (fd < 0) { |
353632d9 | 1146 | fprintf(stderr, Name ": Could not open %s for write - cannot Assemble array.\n", |
52826846 | 1147 | devices[chosen_drive].devname); |
7f91af49 | 1148 | close(mdfd); |
d7f7ebb7 | 1149 | free(devices); |
52826846 NB |
1150 | return 1; |
1151 | } | |
3da92f27 | 1152 | if (st->ss->store_super(st, fd)) { |
52826846 NB |
1153 | close(fd); |
1154 | fprintf(stderr, Name ": Could not re-write superblock on %s\n", | |
1155 | devices[chosen_drive].devname); | |
7f91af49 | 1156 | close(mdfd); |
d7f7ebb7 | 1157 | free(devices); |
52826846 NB |
1158 | return 1; |
1159 | } | |
a28232b8 N |
1160 | if (verbose >= 0) |
1161 | fprintf(stderr, Name ": Marking array %s as 'clean'\n", | |
1162 | mddev); | |
52826846 | 1163 | close(fd); |
52826846 NB |
1164 | } |
1165 | ||
353632d9 NB |
1166 | /* If we are in the middle of a reshape we may need to restore saved data |
1167 | * that was moved aside due to the reshape overwriting live data | |
1168 | * The code of doing this lives in Grow.c | |
1169 | */ | |
39c5a909 | 1170 | #ifndef MDASSEMBLE |
98dbd966 | 1171 | if (content->reshape_active) { |
353632d9 NB |
1172 | int err = 0; |
1173 | int *fdlist = malloc(sizeof(int)* bestcnt); | |
cd77ac4e | 1174 | if (verbose > 0) |
ea0ebe96 N |
1175 | fprintf(stderr, Name ":%s has an active reshape - checking " |
1176 | "if critical section needs to be restored\n", | |
1177 | chosen_name); | |
353632d9 NB |
1178 | for (i=0; i<bestcnt; i++) { |
1179 | int j = best[i]; | |
1180 | if (j >= 0) { | |
1181 | fdlist[i] = dev_open(devices[j].devname, O_RDWR|O_EXCL); | |
1182 | if (fdlist[i] < 0) { | |
1183 | fprintf(stderr, Name ": Could not open %s for write - cannot Assemble array.\n", | |
1184 | devices[j].devname); | |
1185 | err = 1; | |
1186 | break; | |
1187 | } | |
1188 | } else | |
1189 | fdlist[i] = -1; | |
1190 | } | |
87f26d14 | 1191 | if (!err) { |
ba53ea59 AK |
1192 | if (st->ss->external && st->ss->recover_backup) |
1193 | err = st->ss->recover_backup(st, content); | |
1194 | else | |
1195 | err = Grow_restart(st, content, fdlist, bestcnt, | |
1196 | backup_file, verbose > 0); | |
87f26d14 N |
1197 | if (err && invalid_backup) { |
1198 | if (verbose > 0) | |
1199 | fprintf(stderr, Name ": continuing" | |
1200 | " without restoring backup\n"); | |
1201 | err = 0; | |
1202 | } | |
1203 | } | |
353632d9 NB |
1204 | while (i>0) { |
1205 | i--; | |
1206 | if (fdlist[i]>=0) close(fdlist[i]); | |
1207 | } | |
1208 | if (err) { | |
1209 | fprintf(stderr, Name ": Failed to restore critical section for reshape, sorry.\n"); | |
e9e43ec3 N |
1210 | if (backup_file == NULL) |
1211 | fprintf(stderr," Possibly you needed to specify the --backup-file\n"); | |
7f91af49 | 1212 | close(mdfd); |
d7f7ebb7 | 1213 | free(devices); |
353632d9 NB |
1214 | return err; |
1215 | } | |
1216 | } | |
39c5a909 | 1217 | #endif |
aa88f531 NB |
1218 | /* count number of in-sync devices according to the superblock. |
1219 | * We must have this number to start the array without -s or -R | |
1220 | */ | |
98dbd966 | 1221 | req_cnt = content->array.working_disks; |
aa88f531 | 1222 | |
64c4757e NB |
1223 | /* Almost ready to actually *do* something */ |
1224 | if (!old_linux) { | |
fbf8a0b7 | 1225 | int rv; |
a19c88b8 | 1226 | |
a04d5763 N |
1227 | /* First, fill in the map, so that udev can find our name |
1228 | * as soon as we become active. | |
1229 | */ | |
98dbd966 DW |
1230 | map_update(NULL, fd2devnum(mdfd), content->text_version, |
1231 | content->uuid, chosen_name); | |
a04d5763 | 1232 | |
98dbd966 | 1233 | rv = set_array_info(mdfd, st, content); |
fbf8a0b7 | 1234 | if (rv) { |
f35f2525 | 1235 | fprintf(stderr, Name ": failed to set array info for %s: %s\n", |
64c4757e | 1236 | mddev, strerror(errno)); |
24af7a87 | 1237 | ioctl(mdfd, STOP_ARRAY, NULL); |
7f91af49 | 1238 | close(mdfd); |
d7f7ebb7 | 1239 | free(devices); |
64c4757e NB |
1240 | return 1; |
1241 | } | |
11484a63 | 1242 | if (ident->bitmap_fd >= 0) { |
c82f047c NB |
1243 | if (ioctl(mdfd, SET_BITMAP_FILE, ident->bitmap_fd) != 0) { |
1244 | fprintf(stderr, Name ": SET_BITMAP_FILE failed.\n"); | |
24af7a87 | 1245 | ioctl(mdfd, STOP_ARRAY, NULL); |
7f91af49 | 1246 | close(mdfd); |
d7f7ebb7 | 1247 | free(devices); |
c82f047c NB |
1248 | return 1; |
1249 | } | |
7ef02d01 NB |
1250 | } else if (ident->bitmap_file) { |
1251 | /* From config file */ | |
1252 | int bmfd = open(ident->bitmap_file, O_RDWR); | |
1253 | if (bmfd < 0) { | |
1254 | fprintf(stderr, Name ": Could not open bitmap file %s\n", | |
1255 | ident->bitmap_file); | |
24af7a87 | 1256 | ioctl(mdfd, STOP_ARRAY, NULL); |
7f91af49 | 1257 | close(mdfd); |
d7f7ebb7 | 1258 | free(devices); |
7ef02d01 NB |
1259 | return 1; |
1260 | } | |
1261 | if (ioctl(mdfd, SET_BITMAP_FILE, bmfd) != 0) { | |
1262 | fprintf(stderr, Name ": Failed to set bitmapfile for %s\n", mddev); | |
1263 | close(bmfd); | |
24af7a87 | 1264 | ioctl(mdfd, STOP_ARRAY, NULL); |
7f91af49 | 1265 | close(mdfd); |
d7f7ebb7 | 1266 | free(devices); |
7ef02d01 NB |
1267 | return 1; |
1268 | } | |
1269 | close(bmfd); | |
c82f047c | 1270 | } |
7ef02d01 | 1271 | |
52826846 | 1272 | /* First, add the raid disks, but add the chosen one last */ |
56eedc1a | 1273 | for (i=0; i<= bestcnt; i++) { |
52826846 | 1274 | int j; |
56eedc1a | 1275 | if (i < bestcnt) { |
52826846 NB |
1276 | j = best[i]; |
1277 | if (j == chosen_drive) | |
1278 | continue; | |
1279 | } else | |
1280 | j = chosen_drive; | |
1281 | ||
56eedc1a | 1282 | if (j >= 0 /* && devices[j].uptodate */) { |
484ae54d N |
1283 | int dfd = dev_open(devices[j].devname, |
1284 | O_RDWR|O_EXCL); | |
1285 | if (dfd >= 0) { | |
1286 | remove_partitions(dfd); | |
1287 | close(dfd); | |
1288 | } | |
98dbd966 | 1289 | rv = add_disk(mdfd, st, content, &devices[j].i); |
7801ac20 | 1290 | |
a19c88b8 | 1291 | if (rv) { |
213ee40b NB |
1292 | fprintf(stderr, Name ": failed to add " |
1293 | "%s to %s: %s\n", | |
64c4757e NB |
1294 | devices[j].devname, |
1295 | mddev, | |
1296 | strerror(errno)); | |
98dbd966 | 1297 | if (i < content->array.raid_disks |
213ee40b | 1298 | || i == bestcnt) |
52826846 NB |
1299 | okcnt--; |
1300 | else | |
1301 | sparecnt--; | |
dab6685f | 1302 | } else if (verbose > 0) |
213ee40b NB |
1303 | fprintf(stderr, Name ": added %s " |
1304 | "to %s as %d\n", | |
1305 | devices[j].devname, mddev, | |
1306 | devices[j].i.disk.raid_disk); | |
98dbd966 | 1307 | } else if (verbose > 0 && i < content->array.raid_disks) |
213ee40b NB |
1308 | fprintf(stderr, Name ": no uptodate device for " |
1309 | "slot %d of %s\n", | |
64c4757e NB |
1310 | i, mddev); |
1311 | } | |
aba69144 | 1312 | |
98dbd966 | 1313 | if (content->array.level == LEVEL_CONTAINER) { |
598f0d58 NB |
1314 | if (verbose >= 0) { |
1315 | fprintf(stderr, Name ": Container %s has been " | |
1316 | "assembled with %d drive%s", | |
57ed8c91 | 1317 | mddev, okcnt+sparecnt, okcnt+sparecnt==1?"":"s"); |
f21e18ca | 1318 | if (okcnt < (unsigned)content->array.raid_disks) |
598f0d58 | 1319 | fprintf(stderr, " (out of %d)", |
98dbd966 | 1320 | content->array.raid_disks); |
598f0d58 NB |
1321 | fprintf(stderr, "\n"); |
1322 | } | |
ac597b1c | 1323 | st->ss->free_super(st); |
98dbd966 | 1324 | sysfs_uevent(content, "change"); |
a7c6e3fb | 1325 | wait_for(chosen_name, mdfd); |
7f91af49 | 1326 | close(mdfd); |
d7f7ebb7 | 1327 | free(devices); |
598f0d58 NB |
1328 | return 0; |
1329 | } | |
1330 | ||
64c4757e | 1331 | if (runstop == 1 || |
b8a8ccf9 | 1332 | (runstop <= 0 && |
98dbd966 DW |
1333 | ( enough(content->array.level, content->array.raid_disks, |
1334 | content->array.layout, clean, avail, okcnt) && | |
1ff98339 | 1335 | (okcnt + rebuilding_cnt >= req_cnt || start_partial_ok) |
aa88f531 | 1336 | ))) { |
e9e43ec3 N |
1337 | /* This array is good-to-go. |
1338 | * If a reshape is in progress then we might need to | |
1339 | * continue monitoring it. In that case we start | |
1340 | * it read-only and let the grow code make it writable. | |
1341 | */ | |
1342 | int rv; | |
eb3929a4 | 1343 | #ifndef MDASSEMBLE |
e9e43ec3 | 1344 | if (content->reshape_active && |
3bd58dc6 AK |
1345 | content->delta_disks <= 0) { |
1346 | rv = sysfs_set_str(content, NULL, | |
1347 | "array_state", "readonly"); | |
1348 | if (rv == 0) | |
1349 | rv = Grow_continue(mdfd, st, content, | |
1350 | backup_file, | |
1351 | freeze_reshape); | |
1352 | } else | |
eb3929a4 | 1353 | #endif |
e9e43ec3 N |
1354 | rv = ioctl(mdfd, RUN_ARRAY, NULL); |
1355 | if (rv == 0) { | |
dab6685f NB |
1356 | if (verbose >= 0) { |
1357 | fprintf(stderr, Name ": %s has been started with %d drive%s", | |
1358 | mddev, okcnt, okcnt==1?"":"s"); | |
f21e18ca | 1359 | if (okcnt < (unsigned)content->array.raid_disks) |
98dbd966 | 1360 | fprintf(stderr, " (out of %d)", content->array.raid_disks); |
1ff98339 N |
1361 | if (rebuilding_cnt) |
1362 | fprintf(stderr, "%s %d rebuilding", sparecnt?",":" and", rebuilding_cnt); | |
dab6685f NB |
1363 | if (sparecnt) |
1364 | fprintf(stderr, " and %d spare%s", sparecnt, sparecnt==1?"":"s"); | |
1365 | fprintf(stderr, ".\n"); | |
1366 | } | |
8a659c33 N |
1367 | if (content->reshape_active && |
1368 | content->array.level >= 4 && | |
1369 | content->array.level <= 6) { | |
acee8e89 N |
1370 | /* might need to increase the size |
1371 | * of the stripe cache - default is 256 | |
1372 | */ | |
8a659c33 | 1373 | if (256 < 4 * (content->array.chunk_size/4096)) { |
acee8e89 N |
1374 | struct mdinfo *sra = sysfs_read(mdfd, 0, 0); |
1375 | if (sra) | |
1376 | sysfs_set_num(sra, NULL, | |
1377 | "stripe_cache_size", | |
8a659c33 | 1378 | (4 * content->array.chunk_size / 4096) + 1); |
83366b33 | 1379 | sysfs_free(sra); |
acee8e89 N |
1380 | } |
1381 | } | |
7e83544b N |
1382 | if (okcnt < (unsigned)content->array.raid_disks) { |
1383 | /* If any devices did not get added | |
1384 | * because the kernel rejected them based | |
1385 | * on event count, try adding them | |
1386 | * again providing the action policy is | |
1387 | * 're-add' or greater. The bitmap | |
1388 | * might allow them to be included, or | |
1389 | * they will become spares. | |
1390 | */ | |
b787bec6 | 1391 | for (i = 0; i < bestcnt; i++) { |
7e83544b N |
1392 | int j = best[i]; |
1393 | if (j >= 0 && !devices[j].uptodate) { | |
1394 | if (!disk_action_allows(&devices[j].i, st->ss->name, act_re_add)) | |
1395 | continue; | |
1396 | rv = add_disk(mdfd, st, content, | |
1397 | &devices[j].i); | |
1398 | if (rv == 0 && verbose >= 0) | |
1399 | fprintf(stderr, | |
1400 | Name ": %s has been re-added.\n", | |
1401 | devices[j].devname); | |
1402 | } | |
1403 | } | |
1404 | } | |
a7c6e3fb | 1405 | wait_for(mddev, mdfd); |
7f91af49 N |
1406 | close(mdfd); |
1407 | if (auto_assem) { | |
589395d6 | 1408 | int usecs = 1; |
589395d6 NB |
1409 | /* There is a nasty race with 'mdadm --monitor'. |
1410 | * If it opens this device before we close it, | |
1411 | * it gets an incomplete open on which IO | |
598f0d58 NB |
1412 | * doesn't work and the capacity is |
1413 | * wrong. | |
589395d6 NB |
1414 | * If we reopen (to check for layered devices) |
1415 | * before --monitor closes, we loose. | |
1416 | * | |
1417 | * So: wait upto 1 second for there to be | |
1418 | * a non-zero capacity. | |
1419 | */ | |
1420 | while (usecs < 1000) { | |
1421 | mdfd = open(mddev, O_RDONLY); | |
1422 | if (mdfd >= 0) { | |
beae1dfe NB |
1423 | unsigned long long size; |
1424 | if (get_dev_size(mdfd, NULL, &size) && | |
589395d6 NB |
1425 | size > 0) |
1426 | break; | |
1427 | close(mdfd); | |
1428 | } | |
1429 | usleep(usecs); | |
1430 | usecs <<= 1; | |
1431 | } | |
1432 | } | |
d7f7ebb7 | 1433 | free(devices); |
64c4757e | 1434 | return 0; |
82b27616 | 1435 | } |
682c7051 | 1436 | fprintf(stderr, Name ": failed to RUN_ARRAY %s: %s\n", |
64c4757e | 1437 | mddev, strerror(errno)); |
583315d9 | 1438 | |
98dbd966 DW |
1439 | if (!enough(content->array.level, content->array.raid_disks, |
1440 | content->array.layout, 1, avail, okcnt)) | |
583315d9 NB |
1441 | fprintf(stderr, Name ": Not enough devices to " |
1442 | "start the array.\n"); | |
98dbd966 DW |
1443 | else if (!enough(content->array.level, |
1444 | content->array.raid_disks, | |
1445 | content->array.layout, clean, | |
583315d9 NB |
1446 | avail, okcnt)) |
1447 | fprintf(stderr, Name ": Not enough devices to " | |
1448 | "start the array while not clean " | |
1449 | "- consider --force.\n"); | |
1450 | ||
7f91af49 | 1451 | if (auto_assem) |
1c203a4b | 1452 | ioctl(mdfd, STOP_ARRAY, NULL); |
7f91af49 | 1453 | close(mdfd); |
d7f7ebb7 | 1454 | free(devices); |
64c4757e NB |
1455 | return 1; |
1456 | } | |
82b27616 | 1457 | if (runstop == -1) { |
b8a8ccf9 | 1458 | fprintf(stderr, Name ": %s assembled from %d drive%s", |
52826846 | 1459 | mddev, okcnt, okcnt==1?"":"s"); |
f21e18ca | 1460 | if (okcnt != (unsigned)content->array.raid_disks) |
98dbd966 | 1461 | fprintf(stderr, " (out of %d)", content->array.raid_disks); |
b8a8ccf9 | 1462 | fprintf(stderr, ", but not started.\n"); |
7f91af49 | 1463 | close(mdfd); |
d7f7ebb7 | 1464 | free(devices); |
64c4757e | 1465 | return 0; |
82b27616 | 1466 | } |
4855f95c | 1467 | if (verbose >= -1) { |
dab6685f | 1468 | fprintf(stderr, Name ": %s assembled from %d drive%s", mddev, okcnt, okcnt==1?"":"s"); |
1ff98339 N |
1469 | if (rebuilding_cnt) |
1470 | fprintf(stderr, "%s %d rebuilding", sparecnt?", ":" and ", rebuilding_cnt); | |
dab6685f NB |
1471 | if (sparecnt) |
1472 | fprintf(stderr, " and %d spare%s", sparecnt, sparecnt==1?"":"s"); | |
98dbd966 DW |
1473 | if (!enough(content->array.level, content->array.raid_disks, |
1474 | content->array.layout, 1, avail, okcnt)) | |
dab6685f | 1475 | fprintf(stderr, " - not enough to start the array.\n"); |
98dbd966 DW |
1476 | else if (!enough(content->array.level, |
1477 | content->array.raid_disks, | |
1478 | content->array.layout, clean, | |
583315d9 NB |
1479 | avail, okcnt)) |
1480 | fprintf(stderr, " - not enough to start the " | |
1481 | "array while not clean - consider " | |
1482 | "--force.\n"); | |
dab6685f | 1483 | else { |
f21e18ca | 1484 | if (req_cnt == (unsigned)content->array.raid_disks) |
dab6685f NB |
1485 | fprintf(stderr, " - need all %d to start it", req_cnt); |
1486 | else | |
98dbd966 | 1487 | fprintf(stderr, " - need %d of %d to start", req_cnt, content->array.raid_disks); |
dab6685f NB |
1488 | fprintf(stderr, " (use --run to insist).\n"); |
1489 | } | |
aa88f531 | 1490 | } |
7f91af49 | 1491 | if (auto_assem) |
1c203a4b | 1492 | ioctl(mdfd, STOP_ARRAY, NULL); |
56a8da69 | 1493 | close(mdfd); |
d7f7ebb7 | 1494 | free(devices); |
64c4757e | 1495 | return 1; |
82b27616 | 1496 | } else { |
52826846 NB |
1497 | /* The "chosen_drive" is a good choice, and if necessary, the superblock has |
1498 | * been updated to point to the current locations of devices. | |
1499 | * so we can just start the array | |
82b27616 | 1500 | */ |
cd29a5c8 | 1501 | unsigned long dev; |
213ee40b NB |
1502 | dev = makedev(devices[chosen_drive].i.disk.major, |
1503 | devices[chosen_drive].i.disk.minor); | |
82b27616 NB |
1504 | if (ioctl(mdfd, START_ARRAY, dev)) { |
1505 | fprintf(stderr, Name ": Cannot start array: %s\n", | |
1506 | strerror(errno)); | |
1507 | } | |
aba69144 | 1508 | |
64c4757e | 1509 | } |
7f91af49 | 1510 | close(mdfd); |
d7f7ebb7 | 1511 | free(devices); |
e0d19036 | 1512 | return 0; |
64c4757e | 1513 | } |
6234c63c DW |
1514 | |
1515 | #ifndef MDASSEMBLE | |
1516 | int assemble_container_content(struct supertype *st, int mdfd, | |
1517 | struct mdinfo *content, int runstop, | |
49680258 | 1518 | char *chosen_name, int verbose, |
b76b30e0 | 1519 | char *backup_file, int freeze_reshape) |
6234c63c DW |
1520 | { |
1521 | struct mdinfo *dev, *sra; | |
1522 | int working = 0, preexist = 0; | |
882029c8 | 1523 | int expansion = 0; |
6234c63c | 1524 | struct map_ent *map = NULL; |
7af03341 | 1525 | int old_raid_disks; |
6234c63c DW |
1526 | |
1527 | sysfs_init(content, mdfd, 0); | |
1528 | ||
1529 | sra = sysfs_read(mdfd, 0, GET_VERSION); | |
1530 | if (sra == NULL || strcmp(sra->text_version, content->text_version) != 0) | |
22472ee1 JS |
1531 | if (sysfs_set_array(content, md_get_version(mdfd)) != 0) { |
1532 | if (sra) | |
1533 | sysfs_free(sra); | |
6234c63c | 1534 | return 1; |
22472ee1 | 1535 | } |
588bebfc | 1536 | |
6e75048b | 1537 | if (st->ss->external && content->recovery_blocked) |
b8063f07 AK |
1538 | block_subarray(content); |
1539 | ||
6234c63c DW |
1540 | if (sra) |
1541 | sysfs_free(sra); | |
7af03341 | 1542 | old_raid_disks = content->array.raid_disks - content->delta_disks; |
6234c63c | 1543 | for (dev = content->devs; dev; dev = dev->next) |
14032016 | 1544 | if (sysfs_add_disk(content, dev, 1) == 0) { |
7af03341 | 1545 | if (dev->disk.raid_disk >= old_raid_disks && |
14032016 AK |
1546 | content->reshape_active) |
1547 | expansion++; | |
1548 | else | |
1549 | working++; | |
1550 | } else if (errno == EEXIST) | |
6234c63c | 1551 | preexist++; |
588bebfc | 1552 | if (working == 0) |
7cb2aa33 | 1553 | return 1;/* Nothing new, don't try to start */ |
588bebfc | 1554 | |
8b4e5ea9 N |
1555 | map_update(&map, fd2devnum(mdfd), |
1556 | content->text_version, | |
1557 | content->uuid, chosen_name); | |
1558 | ||
1559 | if (runstop > 0 || | |
882029c8 AK |
1560 | (working + preexist + expansion) >= |
1561 | content->array.working_disks) { | |
bb50e5d3 | 1562 | int err; |
81219e70 | 1563 | int start_reshape; |
a714580e | 1564 | |
81219e70 LM |
1565 | /* There are two types of reshape: container wide or sub-array specific |
1566 | * Check if metadata requests blocking container wide reshapes | |
1567 | */ | |
1568 | start_reshape = (content->reshape_active && | |
1569 | !((content->reshape_active == CONTAINER_RESHAPE) && | |
1570 | (content->array.state & (1<<MD_SB_BLOCK_CONTAINER_RESHAPE)))); | |
1571 | if (start_reshape) { | |
49680258 | 1572 | int spare = content->array.raid_disks + expansion; |
3f54bd62 AK |
1573 | if (restore_backup(st, content, |
1574 | working, | |
1575 | spare, backup_file, verbose) == 1) | |
49680258 | 1576 | return 1; |
49680258 | 1577 | |
3bd58dc6 AK |
1578 | err = sysfs_set_str(content, NULL, |
1579 | "array_state", "readonly"); | |
1580 | if (err) | |
1581 | return 1; | |
1582 | ||
a93ada3b AK |
1583 | if (st->ss->external) { |
1584 | if (!mdmon_running(st->container_dev)) | |
1585 | start_mdmon(st->container_dev); | |
1586 | ping_monitor_by_id(st->container_dev); | |
7728e1c6 LD |
1587 | if (mdmon_running(st->container_dev) && |
1588 | st->update_tail == NULL) | |
1589 | st->update_tail = &st->updates; | |
a93ada3b AK |
1590 | } |
1591 | ||
b76b30e0 AK |
1592 | err = Grow_continue(mdfd, st, content, backup_file, |
1593 | freeze_reshape); | |
49680258 | 1594 | } else switch(content->array.level) { |
6234c63c DW |
1595 | case LEVEL_LINEAR: |
1596 | case LEVEL_MULTIPATH: | |
1597 | case 0: | |
bb50e5d3 N |
1598 | err = sysfs_set_str(content, NULL, "array_state", |
1599 | "active"); | |
6234c63c DW |
1600 | break; |
1601 | default: | |
bb50e5d3 | 1602 | err = sysfs_set_str(content, NULL, "array_state", |
6234c63c DW |
1603 | "readonly"); |
1604 | /* start mdmon if needed. */ | |
bb50e5d3 N |
1605 | if (!err) { |
1606 | if (!mdmon_running(st->container_dev)) | |
1607 | start_mdmon(st->container_dev); | |
983fff45 | 1608 | ping_monitor_by_id(st->container_dev); |
bb50e5d3 | 1609 | } |
6234c63c DW |
1610 | break; |
1611 | } | |
bb50e5d3 N |
1612 | if (!err) |
1613 | sysfs_set_safemode(content, content->safe_mode_delay); | |
6234c63c | 1614 | if (verbose >= 0) { |
bb50e5d3 N |
1615 | if (err) |
1616 | fprintf(stderr, Name | |
576d0280 N |
1617 | ": array %s now has %d device%s", |
1618 | chosen_name, working + preexist, | |
1619 | working + preexist == 1 ? "":"s"); | |
bb50e5d3 N |
1620 | else |
1621 | fprintf(stderr, Name | |
576d0280 N |
1622 | ": Started %s with %d device%s", |
1623 | chosen_name, working + preexist, | |
1624 | working + preexist == 1 ? "":"s"); | |
6234c63c DW |
1625 | if (preexist) |
1626 | fprintf(stderr, " (%d new)", working); | |
882029c8 AK |
1627 | if (expansion) |
1628 | fprintf(stderr, " ( + %d for expansion)", | |
1629 | expansion); | |
6234c63c DW |
1630 | fprintf(stderr, "\n"); |
1631 | } | |
bb50e5d3 | 1632 | if (!err) |
a7c6e3fb | 1633 | wait_for(chosen_name, mdfd); |
588bebfc | 1634 | return err; |
6234c63c | 1635 | /* FIXME should have an O_EXCL and wait for read-auto */ |
7cb2aa33 | 1636 | } else { |
88716263 | 1637 | if (verbose >= 0) { |
6234c63c | 1638 | fprintf(stderr, Name |
88716263 N |
1639 | ": %s assembled with %d device%s", |
1640 | chosen_name, preexist + working, | |
1641 | preexist + working == 1 ? "":"s"); | |
1642 | if (preexist) | |
1643 | fprintf(stderr, " (%d new)", working); | |
1644 | fprintf(stderr, " but not started\n"); | |
1645 | } | |
7cb2aa33 N |
1646 | return 1; |
1647 | } | |
6234c63c DW |
1648 | } |
1649 | #endif | |
1650 |