]>
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 | ||
56d18859 | 313 | dfd = dev_open(devname, O_RDONLY); |
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 | */ | |
56d18859 N |
411 | /* should be safe to try an exclusive open now, we |
412 | * have rejected anything that some other mdadm might | |
413 | * be looking at | |
414 | */ | |
415 | dfd = dev_open(devname, O_RDONLY | O_EXCL); | |
416 | if (dfd < 0) { | |
417 | if (report_missmatch) | |
418 | fprintf(stderr, Name ": %s is busy - skipping\n", devname); | |
419 | goto loop; | |
420 | } | |
421 | close(dfd); | |
9008ed1c N |
422 | |
423 | if (ident->container) { | |
424 | if (ident->container[0] == '/' && | |
425 | !same_dev(ident->container, devname)) { | |
426 | if (report_missmatch) | |
427 | fprintf(stderr, Name ": %s is not the container required (%s)\n", | |
428 | devname, ident->container); | |
429 | goto loop; | |
430 | } | |
431 | if (ident->container[0] != '/') { | |
432 | /* we have a uuid */ | |
433 | int uuid[4]; | |
87477e6d N |
434 | |
435 | content = &info; | |
87477e6d N |
436 | tst->ss->getinfo_super(tst, content, NULL); |
437 | ||
9008ed1c N |
438 | if (!parse_uuid(ident->container, uuid) || |
439 | !same_uuid(content->uuid, uuid, tst->ss->swapuuid)) { | |
440 | if (report_missmatch) | |
441 | fprintf(stderr, Name ": %s has wrong UUID to be required container\n", | |
442 | devname); | |
443 | goto loop; | |
444 | } | |
445 | } | |
446 | } | |
447 | /* It is worth looking inside this container. | |
448 | */ | |
7636b5a8 N |
449 | if (verbose > 0) |
450 | fprintf(stderr, Name ": looking in container %s\n", | |
451 | devname); | |
1415fe4b | 452 | |
88cef9b3 N |
453 | for (content = tst->ss->container_content(tst, NULL); |
454 | content; | |
455 | content = content->next) { | |
1415fe4b | 456 | |
88cef9b3 N |
457 | if (!ident_matches(ident, content, tst, |
458 | homehost, update, | |
459 | report_missmatch ? devname : NULL)) | |
460 | /* message already printed */; | |
461 | else if (is_member_busy(content->text_version)) { | |
462 | if (report_missmatch) | |
463 | fprintf(stderr, Name ": member %s in %s is already assembled\n", | |
464 | content->text_version, | |
465 | devname); | |
81219e70 LM |
466 | } else if (content->array.state & (1<<MD_SB_BLOCK_VOLUME)) { |
467 | /* do not assemble arrays with unsupported configurations */ | |
468 | fprintf(stderr, Name ": Cannot activate member %s in %s.\n", | |
469 | content->text_version, | |
470 | devname); | |
88cef9b3 N |
471 | } else |
472 | break; | |
473 | } | |
474 | if (!content) { | |
f7ad3ccc | 475 | tmpdev->used = 2; |
88cef9b3 | 476 | goto loop; /* empty container */ |
fa031239 | 477 | } |
88cef9b3 | 478 | |
9008ed1c | 479 | st = tst; tst = NULL; |
4c1c3ad8 | 480 | if (!auto_assem && inargv && tmpdev->next != NULL) { |
9008ed1c N |
481 | fprintf(stderr, Name ": %s is a container, but is not " |
482 | "only device given: confused and aborting\n", | |
483 | devname); | |
484 | st->ss->free_super(st); | |
4e8d9f0a | 485 | dev_policy_free(pol); |
cbeeb0e5 | 486 | domain_free(domains); |
9008ed1c N |
487 | return 1; |
488 | } | |
7636b5a8 N |
489 | if (verbose > 0) |
490 | fprintf(stderr, Name ": found match on member %s in %s\n", | |
491 | content->text_version, devname); | |
bac0d92e | 492 | |
5083d66b N |
493 | /* make sure we finished the loop */ |
494 | tmpdev = NULL; | |
bac0d92e | 495 | goto loop; |
5083d66b | 496 | } else { |
bac0d92e | 497 | |
5083d66b | 498 | content = &info; |
5083d66b N |
499 | tst->ss->getinfo_super(tst, content, NULL); |
500 | ||
501 | if (!ident_matches(ident, content, tst, | |
502 | homehost, update, | |
503 | report_missmatch ? devname : NULL)) | |
df37ffc0 | 504 | goto loop; |
cbeeb0e5 | 505 | |
56d18859 N |
506 | /* should be safe to try an exclusive open now, we |
507 | * have rejected anything that some other mdadm might | |
508 | * be looking at | |
509 | */ | |
510 | dfd = dev_open(devname, O_RDONLY | O_EXCL); | |
511 | if (dfd < 0) { | |
512 | if (report_missmatch) | |
513 | fprintf(stderr, Name ": %s is busy - skipping\n", devname); | |
514 | goto loop; | |
515 | } | |
516 | close(dfd); | |
517 | ||
5083d66b N |
518 | if (st == NULL) |
519 | st = dup_super(tst); | |
520 | if (st->minor_version == -1) | |
521 | st->minor_version = tst->minor_version; | |
ed7fc6b4 AC |
522 | |
523 | if (memcmp(content->uuid, uuid_zero, | |
524 | sizeof(int[4])) == 0) { | |
525 | /* this is a floating spare. It cannot define | |
526 | * an array unless there are no more arrays of | |
527 | * this type to be found. It can be included | |
528 | * in an array of this type though. | |
529 | */ | |
530 | tmpdev->used = 3; | |
531 | goto loop; | |
532 | } | |
533 | ||
5083d66b N |
534 | if (st->ss != tst->ss || |
535 | st->minor_version != tst->minor_version || | |
536 | st->ss->compare_super(st, tst) != 0) { | |
537 | /* Some mismatch. If exactly one array matches this host, | |
538 | * we can resolve on that one. | |
539 | * Or, if we are auto assembling, we just ignore the second | |
540 | * for now. | |
541 | */ | |
542 | if (auto_assem) | |
543 | goto loop; | |
544 | if (homehost) { | |
545 | int first = st->ss->match_home(st, homehost); | |
546 | int last = tst->ss->match_home(tst, homehost); | |
547 | if (first != last && | |
548 | (first == 1 || last == 1)) { | |
549 | /* We can do something */ | |
550 | if (first) {/* just ignore this one */ | |
551 | if (report_missmatch) | |
552 | fprintf(stderr, Name ": %s misses out due to wrong homehost\n", | |
553 | devname); | |
554 | goto loop; | |
555 | } else { /* reject all those sofar */ | |
556 | struct mddev_dev *td; | |
557 | if (report_missmatch) | |
558 | fprintf(stderr, Name ": %s overrides previous devices due to good homehost\n", | |
559 | devname); | |
560 | for (td=devlist; td != tmpdev; td=td->next) | |
561 | if (td->used == 1) | |
562 | td->used = 0; | |
563 | tmpdev->used = 1; | |
564 | goto loop; | |
565 | } | |
83b6208e NB |
566 | } |
567 | } | |
5083d66b N |
568 | fprintf(stderr, Name ": superblock on %s doesn't match others - assembly aborted\n", |
569 | devname); | |
570 | tst->ss->free_super(tst); | |
571 | st->ss->free_super(st); | |
572 | dev_policy_free(pol); | |
cbeeb0e5 | 573 | domain_free(domains); |
5083d66b | 574 | return 1; |
83b6208e | 575 | } |
5083d66b | 576 | tmpdev->used = 1; |
64c4757e | 577 | } |
df37ffc0 | 578 | loop: |
cbeeb0e5 | 579 | /* Collect domain information from members only */ |
26b05aea AC |
580 | if (tmpdev && tmpdev->used == 1) { |
581 | if (!pol) | |
582 | pol = devnum_policy(stb.st_rdev); | |
cbeeb0e5 | 583 | domain_merge(&domains, pol, tst?tst->ss->name:NULL); |
26b05aea | 584 | } |
4e8d9f0a N |
585 | dev_policy_free(pol); |
586 | pol = NULL; | |
3d2b16e7 NB |
587 | if (tst) |
588 | tst->ss->free_super(tst); | |
811e6cbe NB |
589 | } |
590 | ||
ed7fc6b4 | 591 | /* Check if we found some imsm spares but no members */ |
3c7b4a25 CA |
592 | if ((auto_assem || |
593 | (ident->uuid_set && | |
594 | memcmp(uuid_zero, ident->uuid,sizeof(uuid_zero)) == 0)) && | |
595 | (!st || !st->sb)) | |
ed7fc6b4 AC |
596 | for (tmpdev = devlist; tmpdev; tmpdev = tmpdev->next) { |
597 | if (tmpdev->used != 3) | |
598 | continue; | |
599 | tmpdev->used = 1; | |
600 | content = &info; | |
601 | ||
602 | if (!st->sb) { | |
603 | /* we need sb from one of the spares */ | |
604 | int dfd = dev_open(tmpdev->devname, O_RDONLY); | |
605 | if (dfd < 0 || | |
606 | st->ss->load_super(st, dfd, NULL)) | |
607 | tmpdev->used = 2; | |
608 | if (dfd > 0) | |
609 | close(dfd); | |
610 | } | |
611 | } | |
612 | ||
cbeeb0e5 AC |
613 | /* Now reject spares that don't match domains of identified members */ |
614 | for (tmpdev = devlist; tmpdev; tmpdev = tmpdev->next) { | |
615 | struct stat stb; | |
616 | if (tmpdev->used != 3) | |
617 | continue; | |
618 | if (stat(tmpdev->devname, &stb)< 0) { | |
619 | fprintf(stderr, Name ": fstat failed for %s: %s\n", | |
620 | tmpdev->devname, strerror(errno)); | |
621 | tmpdev->used = 2; | |
622 | } else { | |
a5d10dce N |
623 | struct dev_policy *pol = devnum_policy(stb.st_rdev); |
624 | int dt = domain_test(domains, pol, NULL); | |
625 | if (inargv && dt != 0) | |
626 | /* take this spare as domains match | |
627 | * if there are any */ | |
628 | tmpdev->used = 1; | |
629 | else if (!inargv && dt == 1) | |
630 | /* device wasn't explicitly listed, so need | |
631 | * explicit domain match - which we have */ | |
cbeeb0e5 AC |
632 | tmpdev->used = 1; |
633 | else | |
634 | /* if domains don't match mark as unused */ | |
635 | tmpdev->used = 0; | |
636 | dev_policy_free(pol); | |
637 | } | |
638 | } | |
639 | domain_free(domains); | |
640 | ||
98dbd966 | 641 | if (!st || !st->sb || !content) |
c30e5369 N |
642 | return 2; |
643 | ||
05833051 | 644 | /* Now need to open the array device. Use create_mddev */ |
98dbd966 | 645 | if (content == &info) |
a5d85af7 | 646 | st->ss->getinfo_super(st, content, NULL); |
3d2c4fc7 | 647 | |
69207ff6 | 648 | trustworthy = FOREIGN; |
05833051 | 649 | name = content->name; |
745f72f6 N |
650 | switch (st->ss->match_home(st, homehost) |
651 | ?: st->ss->match_home(st, "any")) { | |
69207ff6 N |
652 | case 1: |
653 | trustworthy = LOCAL; | |
98dbd966 | 654 | name = strchr(content->name, ':'); |
69207ff6 N |
655 | if (name) |
656 | name++; | |
3d2c4fc7 | 657 | else |
98dbd966 | 658 | name = content->name; |
69207ff6 | 659 | break; |
c30e5369 | 660 | } |
05833051 | 661 | if (!auto_assem) |
aa7c284c | 662 | /* If the array is listed in mdadm.conf or on |
ac2ecf55 N |
663 | * command line, then we trust the name |
664 | * even if the array doesn't look local | |
665 | */ | |
666 | trustworthy = LOCAL; | |
667 | ||
05833051 | 668 | if (name[0] == 0 && |
98dbd966 DW |
669 | content->array.level == LEVEL_CONTAINER) { |
670 | name = content->text_version; | |
a4bc1720 N |
671 | trustworthy = METADATA; |
672 | } | |
0ac91628 N |
673 | |
674 | if (name[0] && trustworthy != LOCAL && | |
675 | ! require_homehost && | |
676 | conf_name_is_free(name)) | |
677 | trustworthy = LOCAL; | |
678 | ||
7cdc0872 N |
679 | if (trustworthy == LOCAL && |
680 | strchr(name, ':')) | |
681 | /* Ignore 'host:' prefix of name */ | |
682 | name = strchr(name, ':')+1; | |
683 | ||
a04d5763 N |
684 | mdfd = create_mddev(mddev, name, ident->autof, trustworthy, |
685 | chosen_name); | |
c30e5369 N |
686 | if (mdfd < 0) { |
687 | st->ss->free_super(st); | |
c30e5369 | 688 | if (auto_assem) |
60e1bc1a | 689 | goto try_again; |
c30e5369 N |
690 | return 1; |
691 | } | |
a04d5763 | 692 | mddev = chosen_name; |
c30e5369 N |
693 | vers = md_get_version(mdfd); |
694 | if (vers < 9000) { | |
695 | fprintf(stderr, Name ": Assemble requires driver version 0.90.0 or later.\n" | |
696 | " Upgrade your kernel or try --build\n"); | |
697 | close(mdfd); | |
698 | return 1; | |
699 | } | |
66afdfa9 | 700 | if (mddev_busy(fd2devnum(mdfd))) { |
c30e5369 N |
701 | fprintf(stderr, Name ": %s already active, cannot restart it!\n", |
702 | mddev); | |
703 | for (tmpdev = devlist ; | |
704 | tmpdev && tmpdev->used != 1; | |
705 | tmpdev = tmpdev->next) | |
706 | ; | |
707 | if (tmpdev && auto_assem) | |
708 | fprintf(stderr, Name ": %s needed for %s...\n", | |
709 | mddev, tmpdev->devname); | |
710 | close(mdfd); | |
711 | mdfd = -3; | |
712 | st->ss->free_super(st); | |
c30e5369 N |
713 | if (auto_assem) |
714 | goto try_again; | |
715 | return 1; | |
8a46fe84 | 716 | } |
c30e5369 | 717 | ioctl(mdfd, STOP_ARRAY, NULL); /* just incase it was started but has no content */ |
8a46fe84 | 718 | |
9008ed1c N |
719 | #ifndef MDASSEMBLE |
720 | if (content != &info) { | |
721 | /* This is a member of a container. Try starting the array. */ | |
588bebfc AK |
722 | int err; |
723 | err = assemble_container_content(st, mdfd, content, runstop, | |
49680258 | 724 | chosen_name, verbose, |
b76b30e0 | 725 | backup_file, freeze_reshape); |
588bebfc | 726 | close(mdfd); |
588bebfc | 727 | return err; |
9008ed1c | 728 | } |
a6482415 | 729 | bitmap_done = 0; |
9008ed1c | 730 | #endif |
811e6cbe | 731 | /* Ok, no bad inconsistancy, we can try updating etc */ |
d7f7ebb7 | 732 | devices = malloc(num_devs * sizeof(*devices)); |
02e7c5b7 | 733 | devmap = calloc(num_devs * content->array.raid_disks, 1); |
da6b5ca9 | 734 | for (tmpdev = devlist; tmpdev; tmpdev=tmpdev->next) if (tmpdev->used == 1) { |
811e6cbe NB |
735 | char *devname = tmpdev->devname; |
736 | struct stat stb; | |
5787fa49 | 737 | /* looks like a good enough match to update the super block if needed */ |
d1f1011b | 738 | #ifndef MDASSEMBLE |
5787fa49 | 739 | if (update) { |
811e6cbe | 740 | int dfd; |
4b1ac34b NB |
741 | /* prepare useful information in info structures */ |
742 | struct stat stb2; | |
3da92f27 | 743 | struct supertype *tst; |
1e2b2765 | 744 | int err; |
4b1ac34b | 745 | fstat(mdfd, &stb2); |
7d99579f NB |
746 | |
747 | if (strcmp(update, "uuid")==0 && | |
748 | !ident->uuid_set) { | |
749 | int rfd; | |
750 | if ((rfd = open("/dev/urandom", O_RDONLY)) < 0 || | |
751 | read(rfd, ident->uuid, 16) != 16) { | |
752 | *(__u32*)(ident->uuid) = random(); | |
753 | *(__u32*)(ident->uuid+1) = random(); | |
754 | *(__u32*)(ident->uuid+2) = random(); | |
755 | *(__u32*)(ident->uuid+3) = random(); | |
756 | } | |
757 | if (rfd >= 0) close(rfd); | |
7d99579f | 758 | } |
811e6cbe NB |
759 | dfd = dev_open(devname, O_RDWR|O_EXCL); |
760 | ||
3da92f27 | 761 | tst = dup_super(st); |
9f22b13f N |
762 | if (dfd < 0 || tst->ss->load_super(tst, dfd, NULL) != 0) { |
763 | fprintf(stderr, Name ": cannot re-read metadata from %s - aborting\n", | |
764 | devname); | |
765 | if (dfd >= 0) | |
766 | close(dfd); | |
767 | close(mdfd); | |
d7f7ebb7 | 768 | free(devices); |
02e7c5b7 | 769 | free(devmap); |
9f22b13f N |
770 | return 1; |
771 | } | |
02e7c5b7 | 772 | tst->ss->getinfo_super(tst, content, devmap + devcnt * content->array.raid_disks); |
811e6cbe | 773 | |
98dbd966 DW |
774 | memcpy(content->uuid, ident->uuid, 16); |
775 | strcpy(content->name, ident->name); | |
776 | content->array.md_minor = minor(stb2.st_rdev); | |
811e6cbe | 777 | |
1e2b2765 N |
778 | if (strcmp(update, "byteorder") == 0) |
779 | err = 0; | |
780 | else | |
781 | err = tst->ss->update_super(tst, content, update, | |
782 | devname, verbose, | |
783 | ident->uuid_set, | |
784 | homehost); | |
785 | if (err < 0) { | |
786 | fprintf(stderr, | |
787 | Name ": --update=%s not understood" | |
788 | " for %s metadata\n", | |
789 | update, tst->ss->name); | |
790 | tst->ss->free_super(tst); | |
791 | free(tst); | |
792 | close(mdfd); | |
793 | close(dfd); | |
d7f7ebb7 | 794 | free(devices); |
02e7c5b7 | 795 | free(devmap); |
1e2b2765 N |
796 | return 1; |
797 | } | |
e5eac01f NB |
798 | if (strcmp(update, "uuid")==0 && |
799 | !ident->uuid_set) { | |
800 | ident->uuid_set = 1; | |
98dbd966 | 801 | memcpy(ident->uuid, content->uuid, 16); |
e5eac01f | 802 | } |
1e2b2765 | 803 | if (tst->ss->store_super(tst, dfd)) |
5787fa49 NB |
804 | fprintf(stderr, Name ": Could not re-write superblock on %s.\n", |
805 | devname); | |
1e2b2765 | 806 | close(dfd); |
8131b493 NB |
807 | |
808 | if (strcmp(update, "uuid")==0 && | |
bf4fb153 | 809 | ident->bitmap_fd >= 0 && !bitmap_done) { |
3da92f27 | 810 | if (bitmap_update_uuid(ident->bitmap_fd, |
98dbd966 | 811 | content->uuid, |
3da92f27 | 812 | tst->ss->swapuuid) != 0) |
bf4fb153 NB |
813 | fprintf(stderr, Name ": Could not update uuid on external bitmap.\n"); |
814 | else | |
815 | bitmap_done = 1; | |
816 | } | |
3da92f27 | 817 | tst->ss->free_super(tst); |
d1f1011b NB |
818 | } else |
819 | #endif | |
820 | { | |
30e1b9a5 | 821 | struct supertype *tst = dup_super(st); |
da6b5ca9 NB |
822 | int dfd; |
823 | dfd = dev_open(devname, O_RDWR|O_EXCL); | |
824 | ||
9f22b13f N |
825 | if (dfd < 0 || tst->ss->load_super(tst, dfd, NULL) != 0) { |
826 | fprintf(stderr, Name ": cannot re-read metadata from %s - aborting\n", | |
827 | devname); | |
828 | if (dfd >= 0) | |
829 | close(dfd); | |
830 | close(mdfd); | |
d7f7ebb7 | 831 | free(devices); |
02e7c5b7 | 832 | free(devmap); |
9f22b13f N |
833 | return 1; |
834 | } | |
02e7c5b7 | 835 | tst->ss->getinfo_super(tst, content, devmap + devcnt * content->array.raid_disks); |
3da92f27 | 836 | tst->ss->free_super(tst); |
da6b5ca9 | 837 | close(dfd); |
5787fa49 NB |
838 | } |
839 | ||
811e6cbe NB |
840 | stat(devname, &stb); |
841 | ||
dab6685f | 842 | if (verbose > 0) |
cd29a5c8 | 843 | fprintf(stderr, Name ": %s is identified as a member of %s, slot %d.\n", |
98dbd966 | 844 | devname, mddev, content->disk.raid_disk); |
64c4757e | 845 | devices[devcnt].devname = devname; |
64c4757e | 846 | devices[devcnt].uptodate = 0; |
98dbd966 | 847 | devices[devcnt].i = *content; |
213ee40b NB |
848 | devices[devcnt].i.disk.major = major(stb.st_rdev); |
849 | devices[devcnt].i.disk.minor = minor(stb.st_rdev); | |
64c4757e | 850 | if (most_recent < devcnt) { |
213ee40b NB |
851 | if (devices[devcnt].i.events |
852 | > devices[most_recent].i.events) | |
64c4757e NB |
853 | most_recent = devcnt; |
854 | } | |
df0d4ea0 | 855 | if (content->array.level == LEVEL_MULTIPATH) |
5787fa49 NB |
856 | /* with multipath, the raid_disk from the superblock is meaningless */ |
857 | i = devcnt; | |
858 | else | |
213ee40b | 859 | i = devices[devcnt].i.disk.raid_disk; |
308e1801 | 860 | if (i+1 == 0) { |
98dbd966 DW |
861 | if (nextspare < content->array.raid_disks) |
862 | nextspare = content->array.raid_disks; | |
308e1801 | 863 | i = nextspare++; |
08110d41 | 864 | } else { |
98dbd966 | 865 | if (i >= content->array.raid_disks && |
08110d41 NB |
866 | i >= nextspare) |
867 | nextspare = i+1; | |
308e1801 | 868 | } |
98c6faba | 869 | if (i < 10000) { |
56eedc1a | 870 | if (i >= bestcnt) { |
f21e18ca | 871 | int newbestcnt = i+10; |
56eedc1a | 872 | int *newbest = malloc(sizeof(int)*newbestcnt); |
f21e18ca | 873 | int c; |
56eedc1a NB |
874 | for (c=0; c < newbestcnt; c++) |
875 | if (c < bestcnt) | |
876 | newbest[c] = best[c]; | |
877 | else | |
878 | newbest[c] = -1; | |
879 | if (best)free(best); | |
880 | best = newbest; | |
881 | bestcnt = newbestcnt; | |
882 | } | |
6a255b69 | 883 | if (best[i] >=0 && |
213ee40b NB |
884 | devices[best[i]].i.events |
885 | == devices[devcnt].i.events | |
886 | && (devices[best[i]].i.disk.minor | |
887 | != devices[devcnt].i.disk.minor) | |
b8ac1967 | 888 | && st->ss == &super0 |
98dbd966 | 889 | && content->array.level != LEVEL_MULTIPATH) { |
6a255b69 NB |
890 | /* two different devices with identical superblock. |
891 | * Could be a mis-detection caused by overlapping | |
892 | * partitions. fail-safe. | |
893 | */ | |
894 | fprintf(stderr, Name ": WARNING %s and %s appear" | |
895 | " to have very similar superblocks.\n" | |
896 | " If they are really different, " | |
897 | "please --zero the superblock on one\n" | |
d2df86e0 NB |
898 | " If they are the same or overlap," |
899 | " please remove one from %s.\n", | |
900 | devices[best[i]].devname, devname, | |
901 | inargv ? "the list" : | |
902 | "the\n DEVICE list in mdadm.conf" | |
903 | ); | |
7f91af49 | 904 | close(mdfd); |
d7f7ebb7 | 905 | free(devices); |
02e7c5b7 | 906 | free(devmap); |
6a255b69 NB |
907 | return 1; |
908 | } | |
52826846 | 909 | if (best[i] == -1 |
213ee40b NB |
910 | || (devices[best[i]].i.events |
911 | < devices[devcnt].i.events)) | |
52826846 | 912 | best[i] = devcnt; |
56eedc1a | 913 | } |
64c4757e | 914 | devcnt++; |
df37ffc0 | 915 | } |
4b1ac34b | 916 | |
64c4757e | 917 | if (devcnt == 0) { |
682c7051 | 918 | fprintf(stderr, Name ": no devices found for %s\n", |
64c4757e | 919 | mddev); |
3d2b16e7 NB |
920 | if (st) |
921 | st->ss->free_super(st); | |
7f91af49 | 922 | close(mdfd); |
d7f7ebb7 | 923 | free(devices); |
02e7c5b7 | 924 | free(devmap); |
64c4757e NB |
925 | return 1; |
926 | } | |
4b1ac34b | 927 | |
3d2b16e7 NB |
928 | if (update && strcmp(update, "byteorder")==0) |
929 | st->minor_version = 90; | |
930 | ||
a5d85af7 | 931 | st->ss->getinfo_super(st, content, NULL); |
98dbd966 | 932 | clean = content->array.state & 1; |
4b1ac34b | 933 | |
64c4757e NB |
934 | /* now we have some devices that might be suitable. |
935 | * I wonder how many | |
936 | */ | |
98dbd966 DW |
937 | avail = malloc(content->array.raid_disks); |
938 | memset(avail, 0, content->array.raid_disks); | |
64c4757e | 939 | okcnt = 0; |
52826846 | 940 | sparecnt=0; |
1ff98339 | 941 | rebuilding_cnt=0; |
f21e18ca | 942 | for (i=0; i< bestcnt; i++) { |
64c4757e | 943 | int j = best[i]; |
ee04451c NB |
944 | int event_margin = 1; /* always allow a difference of '1' |
945 | * like the kernel does | |
946 | */ | |
64c4757e | 947 | if (j < 0) continue; |
d013a55e NB |
948 | /* note: we ignore error flags in multipath arrays |
949 | * as they don't make sense | |
950 | */ | |
df0d4ea0 | 951 | if (content->array.level != LEVEL_MULTIPATH) |
f22385f9 | 952 | if (!(devices[j].i.disk.state & (1<<MD_DISK_ACTIVE))) { |
213ee40b | 953 | if (!(devices[j].i.disk.state |
ed7fc6b4 AC |
954 | & (1<<MD_DISK_FAULTY))) { |
955 | devices[j].uptodate = 1; | |
aa88f531 | 956 | sparecnt++; |
ed7fc6b4 | 957 | } |
d013a55e | 958 | continue; |
aa88f531 | 959 | } |
de5a472e | 960 | /* If this device thinks that 'most_recent' has failed, then |
02e7c5b7 N |
961 | * we must reject this device. |
962 | */ | |
963 | if (j != most_recent && | |
964 | content->array.raid_disks > 0 && | |
965 | devices[most_recent].i.disk.raid_disk >= 0 && | |
966 | devmap[j * content->array.raid_disks + devices[most_recent].i.disk.raid_disk] == 0) { | |
967 | if (verbose > -1) | |
968 | fprintf(stderr, Name ": ignoring %s as it reports %s as failed\n", | |
969 | devices[j].devname, devices[most_recent].devname); | |
970 | best[i] = -1; | |
971 | continue; | |
972 | } | |
213ee40b NB |
973 | if (devices[j].i.events+event_margin >= |
974 | devices[most_recent].i.events) { | |
64c4757e | 975 | devices[j].uptodate = 1; |
1ff98339 | 976 | if (i < content->array.raid_disks) { |
dcc4210f DW |
977 | if (devices[j].i.recovery_start == MaxSector || |
978 | (content->reshape_active && | |
b720636a N |
979 | ((i >= content->array.raid_disks - content->delta_disks) || |
980 | (i >= content->array.raid_disks - content->delta_disks - 1 | |
981 | && content->array.level == 4)))) { | |
1ff98339 N |
982 | okcnt++; |
983 | avail[i]=1; | |
984 | } else | |
985 | rebuilding_cnt++; | |
265e0f17 | 986 | } else |
52826846 | 987 | sparecnt++; |
64c4757e NB |
988 | } |
989 | } | |
02e7c5b7 | 990 | free(devmap); |
da8fe5aa N |
991 | while (force && |
992 | (!enough(content->array.level, content->array.raid_disks, | |
993 | content->array.layout, 1, | |
994 | avail) | |
995 | || | |
996 | (content->reshape_active && content->delta_disks > 0 && | |
997 | !enough(content->array.level, (content->array.raid_disks | |
998 | - content->delta_disks), | |
999 | content->new_layout, 1, | |
1000 | avail) | |
1001 | ))) { | |
64c4757e NB |
1002 | /* Choose the newest best drive which is |
1003 | * not up-to-date, update the superblock | |
1004 | * and add it. | |
1005 | */ | |
52826846 | 1006 | int fd; |
3da92f27 | 1007 | struct supertype *tst; |
f21e18ca | 1008 | unsigned long long current_events; |
cd29a5c8 | 1009 | chosen_drive = -1; |
f21e18ca | 1010 | for (i = 0; i < content->array.raid_disks && i < bestcnt; i++) { |
52826846 NB |
1011 | int j = best[i]; |
1012 | if (j>=0 && | |
1013 | !devices[j].uptodate && | |
921d9e16 | 1014 | devices[j].i.recovery_start == MaxSector && |
52826846 | 1015 | (chosen_drive < 0 || |
213ee40b NB |
1016 | devices[j].i.events |
1017 | > devices[chosen_drive].i.events)) | |
52826846 NB |
1018 | chosen_drive = j; |
1019 | } | |
1020 | if (chosen_drive < 0) | |
1021 | break; | |
213ee40b | 1022 | current_events = devices[chosen_drive].i.events; |
c5a6f9a6 | 1023 | add_another: |
dab6685f NB |
1024 | if (verbose >= 0) |
1025 | fprintf(stderr, Name ": forcing event count in %s(%d) from %d upto %d\n", | |
213ee40b NB |
1026 | devices[chosen_drive].devname, |
1027 | devices[chosen_drive].i.disk.raid_disk, | |
1028 | (int)(devices[chosen_drive].i.events), | |
1029 | (int)(devices[most_recent].i.events)); | |
8b0dabea | 1030 | fd = dev_open(devices[chosen_drive].devname, O_RDWR|O_EXCL); |
52826846 NB |
1031 | if (fd < 0) { |
1032 | fprintf(stderr, Name ": Couldn't open %s for write - not updating\n", | |
1033 | devices[chosen_drive].devname); | |
213ee40b | 1034 | devices[chosen_drive].i.events = 0; |
52826846 NB |
1035 | continue; |
1036 | } | |
3da92f27 | 1037 | tst = dup_super(st); |
60b435db | 1038 | if (tst->ss->load_super(tst,fd, NULL)) { |
52826846 NB |
1039 | close(fd); |
1040 | fprintf(stderr, Name ": RAID superblock disappeared from %s - not updating.\n", | |
1041 | devices[chosen_drive].devname); | |
213ee40b | 1042 | devices[chosen_drive].i.events = 0; |
52826846 NB |
1043 | continue; |
1044 | } | |
98dbd966 DW |
1045 | content->events = devices[most_recent].i.events; |
1046 | tst->ss->update_super(tst, content, "force-one", | |
67a8c82d NB |
1047 | devices[chosen_drive].devname, verbose, |
1048 | 0, NULL); | |
4b1ac34b | 1049 | |
3da92f27 | 1050 | if (tst->ss->store_super(tst, fd)) { |
52826846 NB |
1051 | close(fd); |
1052 | fprintf(stderr, Name ": Could not re-write superblock on %s\n", | |
1053 | devices[chosen_drive].devname); | |
213ee40b | 1054 | devices[chosen_drive].i.events = 0; |
3da92f27 | 1055 | tst->ss->free_super(tst); |
52826846 NB |
1056 | continue; |
1057 | } | |
1058 | close(fd); | |
213ee40b | 1059 | devices[chosen_drive].i.events = devices[most_recent].i.events; |
52826846 | 1060 | devices[chosen_drive].uptodate = 1; |
265e0f17 | 1061 | avail[chosen_drive] = 1; |
52826846 | 1062 | okcnt++; |
3da92f27 | 1063 | tst->ss->free_super(tst); |
c5a6f9a6 NB |
1064 | |
1065 | /* If there are any other drives of the same vintage, | |
1066 | * add them in as well. We can't lose and we might gain | |
1067 | */ | |
f21e18ca | 1068 | for (i = 0; i < content->array.raid_disks && i < bestcnt ; i++) { |
c5a6f9a6 NB |
1069 | int j = best[i]; |
1070 | if (j >= 0 && | |
1071 | !devices[j].uptodate && | |
213ee40b | 1072 | devices[j].i.events == current_events) { |
c5a6f9a6 NB |
1073 | chosen_drive = j; |
1074 | goto add_another; | |
1075 | } | |
1076 | } | |
64c4757e | 1077 | } |
52826846 NB |
1078 | |
1079 | /* Now we want to look at the superblock which the kernel will base things on | |
1080 | * and compare the devices that we think are working with the devices that the | |
1081 | * superblock thinks are working. | |
1082 | * If there are differences and --force is given, then update this chosen | |
1083 | * superblock. | |
1084 | */ | |
cd29a5c8 | 1085 | chosen_drive = -1; |
3da92f27 | 1086 | st->ss->free_super(st); |
56eedc1a | 1087 | for (i=0; chosen_drive < 0 && i<bestcnt; i++) { |
52826846 NB |
1088 | int j = best[i]; |
1089 | int fd; | |
4b1ac34b | 1090 | |
52826846 NB |
1091 | if (j<0) |
1092 | continue; | |
1093 | if (!devices[j].uptodate) | |
1094 | continue; | |
a28232b8 N |
1095 | if (devices[j].i.events < devices[most_recent].i.events) |
1096 | continue; | |
52826846 | 1097 | chosen_drive = j; |
8b0dabea | 1098 | if ((fd=dev_open(devices[j].devname, O_RDONLY|O_EXCL))< 0) { |
52826846 NB |
1099 | fprintf(stderr, Name ": Cannot open %s: %s\n", |
1100 | devices[j].devname, strerror(errno)); | |
7f91af49 | 1101 | close(mdfd); |
d7f7ebb7 | 1102 | free(devices); |
52826846 NB |
1103 | return 1; |
1104 | } | |
3da92f27 | 1105 | if (st->ss->load_super(st,fd, NULL)) { |
52826846 NB |
1106 | close(fd); |
1107 | fprintf(stderr, Name ": RAID superblock has disappeared from %s\n", | |
1108 | devices[j].devname); | |
7f91af49 | 1109 | close(mdfd); |
d7f7ebb7 | 1110 | free(devices); |
52826846 NB |
1111 | return 1; |
1112 | } | |
1113 | close(fd); | |
1114 | } | |
3da92f27 | 1115 | if (st->sb == NULL) { |
4b1ac34b | 1116 | fprintf(stderr, Name ": No suitable drives found for %s\n", mddev); |
7f91af49 | 1117 | close(mdfd); |
d7f7ebb7 | 1118 | free(devices); |
4b1ac34b NB |
1119 | return 1; |
1120 | } | |
a5d85af7 | 1121 | st->ss->getinfo_super(st, content, NULL); |
f35f2525 | 1122 | #ifndef MDASSEMBLE |
98dbd966 | 1123 | sysfs_init(content, mdfd, 0); |
f35f2525 | 1124 | #endif |
56eedc1a | 1125 | for (i=0; i<bestcnt; i++) { |
52826846 | 1126 | int j = best[i]; |
98c6faba | 1127 | unsigned int desired_state; |
11a3e71d | 1128 | |
98dbd966 | 1129 | if (i < content->array.raid_disks) |
11a3e71d NB |
1130 | desired_state = (1<<MD_DISK_ACTIVE) | (1<<MD_DISK_SYNC); |
1131 | else | |
1132 | desired_state = 0; | |
1133 | ||
52826846 NB |
1134 | if (j<0) |
1135 | continue; | |
1136 | if (!devices[j].uptodate) | |
1137 | continue; | |
4b1ac34b | 1138 | |
213ee40b | 1139 | devices[j].i.disk.state = desired_state; |
4e9a6ff7 N |
1140 | if (!(devices[j].i.array.state & 1)) |
1141 | clean = 0; | |
213ee40b NB |
1142 | |
1143 | if (st->ss->update_super(st, &devices[j].i, "assemble", NULL, | |
68c7d6d7 | 1144 | verbose, 0, NULL)) { |
52826846 | 1145 | if (force) { |
dab6685f NB |
1146 | if (verbose >= 0) |
1147 | fprintf(stderr, Name ": " | |
1148 | "clearing FAULTY flag for device %d in %s for %s\n", | |
1149 | j, mddev, devices[j].devname); | |
4b1ac34b | 1150 | change = 1; |
52826846 | 1151 | } else { |
dab6685f NB |
1152 | if (verbose >= -1) |
1153 | fprintf(stderr, Name ": " | |
1154 | "device %d in %s has wrong state in superblock, but %s seems ok\n", | |
1155 | i, mddev, devices[j].devname); | |
52826846 NB |
1156 | } |
1157 | } | |
4b1ac34b | 1158 | #if 0 |
213ee40b | 1159 | if (!(super.disks[i].i.disk.state & (1 << MD_DISK_FAULTY))) { |
52826846 NB |
1160 | fprintf(stderr, Name ": devices %d of %s is not marked FAULTY in superblock, but cannot be found\n", |
1161 | i, mddev); | |
1162 | } | |
4b1ac34b | 1163 | #endif |
52826846 | 1164 | } |
c5a6f9a6 | 1165 | if (force && !clean && |
98dbd966 DW |
1166 | !enough(content->array.level, content->array.raid_disks, |
1167 | content->array.layout, clean, | |
de5a472e | 1168 | avail)) { |
98dbd966 | 1169 | change += st->ss->update_super(st, content, "force-array", |
67a8c82d NB |
1170 | devices[chosen_drive].devname, verbose, |
1171 | 0, NULL); | |
583315d9 | 1172 | clean = 1; |
d013a55e | 1173 | } |
52826846 | 1174 | |
4b1ac34b | 1175 | if (change) { |
52826846 | 1176 | int fd; |
8b0dabea | 1177 | fd = dev_open(devices[chosen_drive].devname, O_RDWR|O_EXCL); |
52826846 | 1178 | if (fd < 0) { |
353632d9 | 1179 | fprintf(stderr, Name ": Could not open %s for write - cannot Assemble array.\n", |
52826846 | 1180 | devices[chosen_drive].devname); |
7f91af49 | 1181 | close(mdfd); |
d7f7ebb7 | 1182 | free(devices); |
52826846 NB |
1183 | return 1; |
1184 | } | |
3da92f27 | 1185 | if (st->ss->store_super(st, fd)) { |
52826846 NB |
1186 | close(fd); |
1187 | fprintf(stderr, Name ": Could not re-write superblock on %s\n", | |
1188 | devices[chosen_drive].devname); | |
7f91af49 | 1189 | close(mdfd); |
d7f7ebb7 | 1190 | free(devices); |
52826846 NB |
1191 | return 1; |
1192 | } | |
a28232b8 N |
1193 | if (verbose >= 0) |
1194 | fprintf(stderr, Name ": Marking array %s as 'clean'\n", | |
1195 | mddev); | |
52826846 | 1196 | close(fd); |
52826846 NB |
1197 | } |
1198 | ||
353632d9 NB |
1199 | /* If we are in the middle of a reshape we may need to restore saved data |
1200 | * that was moved aside due to the reshape overwriting live data | |
1201 | * The code of doing this lives in Grow.c | |
1202 | */ | |
39c5a909 | 1203 | #ifndef MDASSEMBLE |
98dbd966 | 1204 | if (content->reshape_active) { |
353632d9 NB |
1205 | int err = 0; |
1206 | int *fdlist = malloc(sizeof(int)* bestcnt); | |
cd77ac4e | 1207 | if (verbose > 0) |
ea0ebe96 N |
1208 | fprintf(stderr, Name ":%s has an active reshape - checking " |
1209 | "if critical section needs to be restored\n", | |
1210 | chosen_name); | |
353632d9 NB |
1211 | for (i=0; i<bestcnt; i++) { |
1212 | int j = best[i]; | |
1213 | if (j >= 0) { | |
1214 | fdlist[i] = dev_open(devices[j].devname, O_RDWR|O_EXCL); | |
1215 | if (fdlist[i] < 0) { | |
1216 | fprintf(stderr, Name ": Could not open %s for write - cannot Assemble array.\n", | |
1217 | devices[j].devname); | |
1218 | err = 1; | |
1219 | break; | |
1220 | } | |
1221 | } else | |
1222 | fdlist[i] = -1; | |
1223 | } | |
87f26d14 | 1224 | if (!err) { |
ba53ea59 AK |
1225 | if (st->ss->external && st->ss->recover_backup) |
1226 | err = st->ss->recover_backup(st, content); | |
1227 | else | |
1228 | err = Grow_restart(st, content, fdlist, bestcnt, | |
1229 | backup_file, verbose > 0); | |
87f26d14 N |
1230 | if (err && invalid_backup) { |
1231 | if (verbose > 0) | |
1232 | fprintf(stderr, Name ": continuing" | |
1233 | " without restoring backup\n"); | |
1234 | err = 0; | |
1235 | } | |
1236 | } | |
353632d9 NB |
1237 | while (i>0) { |
1238 | i--; | |
1239 | if (fdlist[i]>=0) close(fdlist[i]); | |
1240 | } | |
1241 | if (err) { | |
1242 | fprintf(stderr, Name ": Failed to restore critical section for reshape, sorry.\n"); | |
e9e43ec3 N |
1243 | if (backup_file == NULL) |
1244 | fprintf(stderr," Possibly you needed to specify the --backup-file\n"); | |
7f91af49 | 1245 | close(mdfd); |
d7f7ebb7 | 1246 | free(devices); |
353632d9 NB |
1247 | return err; |
1248 | } | |
1249 | } | |
39c5a909 | 1250 | #endif |
aa88f531 NB |
1251 | /* count number of in-sync devices according to the superblock. |
1252 | * We must have this number to start the array without -s or -R | |
1253 | */ | |
98dbd966 | 1254 | req_cnt = content->array.working_disks; |
aa88f531 | 1255 | |
64c4757e NB |
1256 | /* Almost ready to actually *do* something */ |
1257 | if (!old_linux) { | |
fbf8a0b7 | 1258 | int rv; |
a19c88b8 | 1259 | |
a04d5763 N |
1260 | /* First, fill in the map, so that udev can find our name |
1261 | * as soon as we become active. | |
1262 | */ | |
98dbd966 DW |
1263 | map_update(NULL, fd2devnum(mdfd), content->text_version, |
1264 | content->uuid, chosen_name); | |
a04d5763 | 1265 | |
98dbd966 | 1266 | rv = set_array_info(mdfd, st, content); |
fbf8a0b7 | 1267 | if (rv) { |
f35f2525 | 1268 | fprintf(stderr, Name ": failed to set array info for %s: %s\n", |
64c4757e | 1269 | mddev, strerror(errno)); |
24af7a87 | 1270 | ioctl(mdfd, STOP_ARRAY, NULL); |
7f91af49 | 1271 | close(mdfd); |
d7f7ebb7 | 1272 | free(devices); |
64c4757e NB |
1273 | return 1; |
1274 | } | |
11484a63 | 1275 | if (ident->bitmap_fd >= 0) { |
c82f047c NB |
1276 | if (ioctl(mdfd, SET_BITMAP_FILE, ident->bitmap_fd) != 0) { |
1277 | fprintf(stderr, Name ": SET_BITMAP_FILE failed.\n"); | |
24af7a87 | 1278 | ioctl(mdfd, STOP_ARRAY, NULL); |
7f91af49 | 1279 | close(mdfd); |
d7f7ebb7 | 1280 | free(devices); |
c82f047c NB |
1281 | return 1; |
1282 | } | |
7ef02d01 NB |
1283 | } else if (ident->bitmap_file) { |
1284 | /* From config file */ | |
1285 | int bmfd = open(ident->bitmap_file, O_RDWR); | |
1286 | if (bmfd < 0) { | |
1287 | fprintf(stderr, Name ": Could not open bitmap file %s\n", | |
1288 | ident->bitmap_file); | |
24af7a87 | 1289 | ioctl(mdfd, STOP_ARRAY, NULL); |
7f91af49 | 1290 | close(mdfd); |
d7f7ebb7 | 1291 | free(devices); |
7ef02d01 NB |
1292 | return 1; |
1293 | } | |
1294 | if (ioctl(mdfd, SET_BITMAP_FILE, bmfd) != 0) { | |
1295 | fprintf(stderr, Name ": Failed to set bitmapfile for %s\n", mddev); | |
1296 | close(bmfd); | |
24af7a87 | 1297 | ioctl(mdfd, STOP_ARRAY, NULL); |
7f91af49 | 1298 | close(mdfd); |
d7f7ebb7 | 1299 | free(devices); |
7ef02d01 NB |
1300 | return 1; |
1301 | } | |
1302 | close(bmfd); | |
c82f047c | 1303 | } |
7ef02d01 | 1304 | |
52826846 | 1305 | /* First, add the raid disks, but add the chosen one last */ |
56eedc1a | 1306 | for (i=0; i<= bestcnt; i++) { |
52826846 | 1307 | int j; |
56eedc1a | 1308 | if (i < bestcnt) { |
52826846 NB |
1309 | j = best[i]; |
1310 | if (j == chosen_drive) | |
1311 | continue; | |
1312 | } else | |
1313 | j = chosen_drive; | |
1314 | ||
56eedc1a | 1315 | if (j >= 0 /* && devices[j].uptodate */) { |
484ae54d N |
1316 | int dfd = dev_open(devices[j].devname, |
1317 | O_RDWR|O_EXCL); | |
1318 | if (dfd >= 0) { | |
1319 | remove_partitions(dfd); | |
1320 | close(dfd); | |
1321 | } | |
98dbd966 | 1322 | rv = add_disk(mdfd, st, content, &devices[j].i); |
7801ac20 | 1323 | |
a19c88b8 | 1324 | if (rv) { |
213ee40b NB |
1325 | fprintf(stderr, Name ": failed to add " |
1326 | "%s to %s: %s\n", | |
64c4757e NB |
1327 | devices[j].devname, |
1328 | mddev, | |
1329 | strerror(errno)); | |
98dbd966 | 1330 | if (i < content->array.raid_disks |
213ee40b | 1331 | || i == bestcnt) |
52826846 NB |
1332 | okcnt--; |
1333 | else | |
1334 | sparecnt--; | |
dab6685f | 1335 | } else if (verbose > 0) |
213ee40b | 1336 | fprintf(stderr, Name ": added %s " |
e62b7785 | 1337 | "to %s as %d%s\n", |
213ee40b | 1338 | devices[j].devname, mddev, |
e62b7785 N |
1339 | devices[j].i.disk.raid_disk, |
1340 | devices[j].uptodate?"": | |
1341 | " (possibly out of date)"); | |
98dbd966 | 1342 | } else if (verbose > 0 && i < content->array.raid_disks) |
213ee40b NB |
1343 | fprintf(stderr, Name ": no uptodate device for " |
1344 | "slot %d of %s\n", | |
64c4757e NB |
1345 | i, mddev); |
1346 | } | |
aba69144 | 1347 | |
98dbd966 | 1348 | if (content->array.level == LEVEL_CONTAINER) { |
598f0d58 NB |
1349 | if (verbose >= 0) { |
1350 | fprintf(stderr, Name ": Container %s has been " | |
1351 | "assembled with %d drive%s", | |
57ed8c91 | 1352 | mddev, okcnt+sparecnt, okcnt+sparecnt==1?"":"s"); |
f21e18ca | 1353 | if (okcnt < (unsigned)content->array.raid_disks) |
598f0d58 | 1354 | fprintf(stderr, " (out of %d)", |
98dbd966 | 1355 | content->array.raid_disks); |
598f0d58 NB |
1356 | fprintf(stderr, "\n"); |
1357 | } | |
ac597b1c | 1358 | st->ss->free_super(st); |
98dbd966 | 1359 | sysfs_uevent(content, "change"); |
a7c6e3fb | 1360 | wait_for(chosen_name, mdfd); |
7f91af49 | 1361 | close(mdfd); |
d7f7ebb7 | 1362 | free(devices); |
598f0d58 NB |
1363 | return 0; |
1364 | } | |
1365 | ||
64c4757e | 1366 | if (runstop == 1 || |
b8a8ccf9 | 1367 | (runstop <= 0 && |
98dbd966 | 1368 | ( enough(content->array.level, content->array.raid_disks, |
de5a472e | 1369 | content->array.layout, clean, avail) && |
1ff98339 | 1370 | (okcnt + rebuilding_cnt >= req_cnt || start_partial_ok) |
aa88f531 | 1371 | ))) { |
e9e43ec3 N |
1372 | /* This array is good-to-go. |
1373 | * If a reshape is in progress then we might need to | |
1374 | * continue monitoring it. In that case we start | |
1375 | * it read-only and let the grow code make it writable. | |
1376 | */ | |
1377 | int rv; | |
eb3929a4 | 1378 | #ifndef MDASSEMBLE |
e9e43ec3 | 1379 | if (content->reshape_active && |
3bd58dc6 AK |
1380 | content->delta_disks <= 0) { |
1381 | rv = sysfs_set_str(content, NULL, | |
1382 | "array_state", "readonly"); | |
1383 | if (rv == 0) | |
1384 | rv = Grow_continue(mdfd, st, content, | |
1385 | backup_file, | |
1386 | freeze_reshape); | |
1387 | } else | |
eb3929a4 | 1388 | #endif |
e9e43ec3 N |
1389 | rv = ioctl(mdfd, RUN_ARRAY, NULL); |
1390 | if (rv == 0) { | |
dab6685f NB |
1391 | if (verbose >= 0) { |
1392 | fprintf(stderr, Name ": %s has been started with %d drive%s", | |
1393 | mddev, okcnt, okcnt==1?"":"s"); | |
f21e18ca | 1394 | if (okcnt < (unsigned)content->array.raid_disks) |
98dbd966 | 1395 | fprintf(stderr, " (out of %d)", content->array.raid_disks); |
1ff98339 N |
1396 | if (rebuilding_cnt) |
1397 | fprintf(stderr, "%s %d rebuilding", sparecnt?",":" and", rebuilding_cnt); | |
dab6685f NB |
1398 | if (sparecnt) |
1399 | fprintf(stderr, " and %d spare%s", sparecnt, sparecnt==1?"":"s"); | |
1400 | fprintf(stderr, ".\n"); | |
1401 | } | |
8a659c33 N |
1402 | if (content->reshape_active && |
1403 | content->array.level >= 4 && | |
1404 | content->array.level <= 6) { | |
acee8e89 N |
1405 | /* might need to increase the size |
1406 | * of the stripe cache - default is 256 | |
1407 | */ | |
8a659c33 | 1408 | if (256 < 4 * (content->array.chunk_size/4096)) { |
acee8e89 N |
1409 | struct mdinfo *sra = sysfs_read(mdfd, 0, 0); |
1410 | if (sra) | |
1411 | sysfs_set_num(sra, NULL, | |
1412 | "stripe_cache_size", | |
8a659c33 | 1413 | (4 * content->array.chunk_size / 4096) + 1); |
83366b33 | 1414 | sysfs_free(sra); |
acee8e89 N |
1415 | } |
1416 | } | |
7e83544b N |
1417 | if (okcnt < (unsigned)content->array.raid_disks) { |
1418 | /* If any devices did not get added | |
1419 | * because the kernel rejected them based | |
1420 | * on event count, try adding them | |
1421 | * again providing the action policy is | |
1422 | * 're-add' or greater. The bitmap | |
1423 | * might allow them to be included, or | |
1424 | * they will become spares. | |
1425 | */ | |
b787bec6 | 1426 | for (i = 0; i < bestcnt; i++) { |
7e83544b N |
1427 | int j = best[i]; |
1428 | if (j >= 0 && !devices[j].uptodate) { | |
1429 | if (!disk_action_allows(&devices[j].i, st->ss->name, act_re_add)) | |
1430 | continue; | |
1431 | rv = add_disk(mdfd, st, content, | |
1432 | &devices[j].i); | |
1433 | if (rv == 0 && verbose >= 0) | |
1434 | fprintf(stderr, | |
1435 | Name ": %s has been re-added.\n", | |
1436 | devices[j].devname); | |
1437 | } | |
1438 | } | |
1439 | } | |
a7c6e3fb | 1440 | wait_for(mddev, mdfd); |
7f91af49 N |
1441 | close(mdfd); |
1442 | if (auto_assem) { | |
589395d6 | 1443 | int usecs = 1; |
589395d6 NB |
1444 | /* There is a nasty race with 'mdadm --monitor'. |
1445 | * If it opens this device before we close it, | |
1446 | * it gets an incomplete open on which IO | |
598f0d58 NB |
1447 | * doesn't work and the capacity is |
1448 | * wrong. | |
589395d6 NB |
1449 | * If we reopen (to check for layered devices) |
1450 | * before --monitor closes, we loose. | |
1451 | * | |
1452 | * So: wait upto 1 second for there to be | |
1453 | * a non-zero capacity. | |
1454 | */ | |
1455 | while (usecs < 1000) { | |
1456 | mdfd = open(mddev, O_RDONLY); | |
1457 | if (mdfd >= 0) { | |
beae1dfe NB |
1458 | unsigned long long size; |
1459 | if (get_dev_size(mdfd, NULL, &size) && | |
589395d6 NB |
1460 | size > 0) |
1461 | break; | |
1462 | close(mdfd); | |
1463 | } | |
1464 | usleep(usecs); | |
1465 | usecs <<= 1; | |
1466 | } | |
1467 | } | |
d7f7ebb7 | 1468 | free(devices); |
64c4757e | 1469 | return 0; |
82b27616 | 1470 | } |
682c7051 | 1471 | fprintf(stderr, Name ": failed to RUN_ARRAY %s: %s\n", |
64c4757e | 1472 | mddev, strerror(errno)); |
583315d9 | 1473 | |
98dbd966 | 1474 | if (!enough(content->array.level, content->array.raid_disks, |
de5a472e | 1475 | content->array.layout, 1, avail)) |
583315d9 NB |
1476 | fprintf(stderr, Name ": Not enough devices to " |
1477 | "start the array.\n"); | |
98dbd966 DW |
1478 | else if (!enough(content->array.level, |
1479 | content->array.raid_disks, | |
1480 | content->array.layout, clean, | |
de5a472e | 1481 | avail)) |
583315d9 NB |
1482 | fprintf(stderr, Name ": Not enough devices to " |
1483 | "start the array while not clean " | |
1484 | "- consider --force.\n"); | |
1485 | ||
7f91af49 | 1486 | if (auto_assem) |
1c203a4b | 1487 | ioctl(mdfd, STOP_ARRAY, NULL); |
7f91af49 | 1488 | close(mdfd); |
d7f7ebb7 | 1489 | free(devices); |
64c4757e NB |
1490 | return 1; |
1491 | } | |
82b27616 | 1492 | if (runstop == -1) { |
b8a8ccf9 | 1493 | fprintf(stderr, Name ": %s assembled from %d drive%s", |
52826846 | 1494 | mddev, okcnt, okcnt==1?"":"s"); |
f21e18ca | 1495 | if (okcnt != (unsigned)content->array.raid_disks) |
98dbd966 | 1496 | fprintf(stderr, " (out of %d)", content->array.raid_disks); |
b8a8ccf9 | 1497 | fprintf(stderr, ", but not started.\n"); |
7f91af49 | 1498 | close(mdfd); |
d7f7ebb7 | 1499 | free(devices); |
64c4757e | 1500 | return 0; |
82b27616 | 1501 | } |
4855f95c | 1502 | if (verbose >= -1) { |
dab6685f | 1503 | fprintf(stderr, Name ": %s assembled from %d drive%s", mddev, okcnt, okcnt==1?"":"s"); |
1ff98339 N |
1504 | if (rebuilding_cnt) |
1505 | fprintf(stderr, "%s %d rebuilding", sparecnt?", ":" and ", rebuilding_cnt); | |
dab6685f NB |
1506 | if (sparecnt) |
1507 | fprintf(stderr, " and %d spare%s", sparecnt, sparecnt==1?"":"s"); | |
98dbd966 | 1508 | if (!enough(content->array.level, content->array.raid_disks, |
de5a472e | 1509 | content->array.layout, 1, avail)) |
dab6685f | 1510 | fprintf(stderr, " - not enough to start the array.\n"); |
98dbd966 DW |
1511 | else if (!enough(content->array.level, |
1512 | content->array.raid_disks, | |
1513 | content->array.layout, clean, | |
de5a472e | 1514 | avail)) |
583315d9 NB |
1515 | fprintf(stderr, " - not enough to start the " |
1516 | "array while not clean - consider " | |
1517 | "--force.\n"); | |
dab6685f | 1518 | else { |
f21e18ca | 1519 | if (req_cnt == (unsigned)content->array.raid_disks) |
dab6685f NB |
1520 | fprintf(stderr, " - need all %d to start it", req_cnt); |
1521 | else | |
98dbd966 | 1522 | fprintf(stderr, " - need %d of %d to start", req_cnt, content->array.raid_disks); |
dab6685f NB |
1523 | fprintf(stderr, " (use --run to insist).\n"); |
1524 | } | |
aa88f531 | 1525 | } |
7f91af49 | 1526 | if (auto_assem) |
1c203a4b | 1527 | ioctl(mdfd, STOP_ARRAY, NULL); |
56a8da69 | 1528 | close(mdfd); |
d7f7ebb7 | 1529 | free(devices); |
64c4757e | 1530 | return 1; |
82b27616 | 1531 | } else { |
52826846 NB |
1532 | /* The "chosen_drive" is a good choice, and if necessary, the superblock has |
1533 | * been updated to point to the current locations of devices. | |
1534 | * so we can just start the array | |
82b27616 | 1535 | */ |
cd29a5c8 | 1536 | unsigned long dev; |
213ee40b NB |
1537 | dev = makedev(devices[chosen_drive].i.disk.major, |
1538 | devices[chosen_drive].i.disk.minor); | |
82b27616 NB |
1539 | if (ioctl(mdfd, START_ARRAY, dev)) { |
1540 | fprintf(stderr, Name ": Cannot start array: %s\n", | |
1541 | strerror(errno)); | |
1542 | } | |
aba69144 | 1543 | |
64c4757e | 1544 | } |
7f91af49 | 1545 | close(mdfd); |
d7f7ebb7 | 1546 | free(devices); |
e0d19036 | 1547 | return 0; |
64c4757e | 1548 | } |
6234c63c DW |
1549 | |
1550 | #ifndef MDASSEMBLE | |
1551 | int assemble_container_content(struct supertype *st, int mdfd, | |
1552 | struct mdinfo *content, int runstop, | |
49680258 | 1553 | char *chosen_name, int verbose, |
b76b30e0 | 1554 | char *backup_file, int freeze_reshape) |
6234c63c DW |
1555 | { |
1556 | struct mdinfo *dev, *sra; | |
1557 | int working = 0, preexist = 0; | |
882029c8 | 1558 | int expansion = 0; |
6234c63c | 1559 | struct map_ent *map = NULL; |
7af03341 | 1560 | int old_raid_disks; |
6234c63c DW |
1561 | |
1562 | sysfs_init(content, mdfd, 0); | |
1563 | ||
1564 | sra = sysfs_read(mdfd, 0, GET_VERSION); | |
1565 | if (sra == NULL || strcmp(sra->text_version, content->text_version) != 0) | |
22472ee1 JS |
1566 | if (sysfs_set_array(content, md_get_version(mdfd)) != 0) { |
1567 | if (sra) | |
1568 | sysfs_free(sra); | |
6234c63c | 1569 | return 1; |
22472ee1 | 1570 | } |
588bebfc | 1571 | |
6e75048b | 1572 | if (st->ss->external && content->recovery_blocked) |
b8063f07 AK |
1573 | block_subarray(content); |
1574 | ||
6234c63c DW |
1575 | if (sra) |
1576 | sysfs_free(sra); | |
7af03341 | 1577 | old_raid_disks = content->array.raid_disks - content->delta_disks; |
6234c63c | 1578 | for (dev = content->devs; dev; dev = dev->next) |
14032016 | 1579 | if (sysfs_add_disk(content, dev, 1) == 0) { |
7af03341 | 1580 | if (dev->disk.raid_disk >= old_raid_disks && |
14032016 AK |
1581 | content->reshape_active) |
1582 | expansion++; | |
1583 | else | |
1584 | working++; | |
1585 | } else if (errno == EEXIST) | |
6234c63c | 1586 | preexist++; |
111e9fda | 1587 | if (working + expansion == 0) |
7cb2aa33 | 1588 | return 1;/* Nothing new, don't try to start */ |
588bebfc | 1589 | |
8b4e5ea9 N |
1590 | map_update(&map, fd2devnum(mdfd), |
1591 | content->text_version, | |
1592 | content->uuid, chosen_name); | |
1593 | ||
1594 | if (runstop > 0 || | |
882029c8 AK |
1595 | (working + preexist + expansion) >= |
1596 | content->array.working_disks) { | |
bb50e5d3 | 1597 | int err; |
81219e70 | 1598 | int start_reshape; |
a714580e | 1599 | |
81219e70 LM |
1600 | /* There are two types of reshape: container wide or sub-array specific |
1601 | * Check if metadata requests blocking container wide reshapes | |
1602 | */ | |
1603 | start_reshape = (content->reshape_active && | |
1604 | !((content->reshape_active == CONTAINER_RESHAPE) && | |
1605 | (content->array.state & (1<<MD_SB_BLOCK_CONTAINER_RESHAPE)))); | |
1606 | if (start_reshape) { | |
49680258 | 1607 | int spare = content->array.raid_disks + expansion; |
3f54bd62 AK |
1608 | if (restore_backup(st, content, |
1609 | working, | |
1610 | spare, backup_file, verbose) == 1) | |
49680258 | 1611 | return 1; |
49680258 | 1612 | |
3bd58dc6 AK |
1613 | err = sysfs_set_str(content, NULL, |
1614 | "array_state", "readonly"); | |
1615 | if (err) | |
1616 | return 1; | |
1617 | ||
a93ada3b AK |
1618 | if (st->ss->external) { |
1619 | if (!mdmon_running(st->container_dev)) | |
1620 | start_mdmon(st->container_dev); | |
1621 | ping_monitor_by_id(st->container_dev); | |
7728e1c6 LD |
1622 | if (mdmon_running(st->container_dev) && |
1623 | st->update_tail == NULL) | |
1624 | st->update_tail = &st->updates; | |
a93ada3b AK |
1625 | } |
1626 | ||
b76b30e0 AK |
1627 | err = Grow_continue(mdfd, st, content, backup_file, |
1628 | freeze_reshape); | |
49680258 | 1629 | } else switch(content->array.level) { |
6234c63c DW |
1630 | case LEVEL_LINEAR: |
1631 | case LEVEL_MULTIPATH: | |
1632 | case 0: | |
bb50e5d3 N |
1633 | err = sysfs_set_str(content, NULL, "array_state", |
1634 | "active"); | |
6234c63c DW |
1635 | break; |
1636 | default: | |
bb50e5d3 | 1637 | err = sysfs_set_str(content, NULL, "array_state", |
6234c63c DW |
1638 | "readonly"); |
1639 | /* start mdmon if needed. */ | |
bb50e5d3 N |
1640 | if (!err) { |
1641 | if (!mdmon_running(st->container_dev)) | |
1642 | start_mdmon(st->container_dev); | |
983fff45 | 1643 | ping_monitor_by_id(st->container_dev); |
bb50e5d3 | 1644 | } |
6234c63c DW |
1645 | break; |
1646 | } | |
bb50e5d3 N |
1647 | if (!err) |
1648 | sysfs_set_safemode(content, content->safe_mode_delay); | |
6234c63c | 1649 | if (verbose >= 0) { |
bb50e5d3 N |
1650 | if (err) |
1651 | fprintf(stderr, Name | |
576d0280 N |
1652 | ": array %s now has %d device%s", |
1653 | chosen_name, working + preexist, | |
1654 | working + preexist == 1 ? "":"s"); | |
bb50e5d3 N |
1655 | else |
1656 | fprintf(stderr, Name | |
576d0280 N |
1657 | ": Started %s with %d device%s", |
1658 | chosen_name, working + preexist, | |
1659 | working + preexist == 1 ? "":"s"); | |
6234c63c DW |
1660 | if (preexist) |
1661 | fprintf(stderr, " (%d new)", working); | |
882029c8 AK |
1662 | if (expansion) |
1663 | fprintf(stderr, " ( + %d for expansion)", | |
1664 | expansion); | |
6234c63c DW |
1665 | fprintf(stderr, "\n"); |
1666 | } | |
bb50e5d3 | 1667 | if (!err) |
a7c6e3fb | 1668 | wait_for(chosen_name, mdfd); |
588bebfc | 1669 | return err; |
6234c63c | 1670 | /* FIXME should have an O_EXCL and wait for read-auto */ |
7cb2aa33 | 1671 | } else { |
88716263 | 1672 | if (verbose >= 0) { |
6234c63c | 1673 | fprintf(stderr, Name |
88716263 N |
1674 | ": %s assembled with %d device%s", |
1675 | chosen_name, preexist + working, | |
1676 | preexist + working == 1 ? "":"s"); | |
1677 | if (preexist) | |
1678 | fprintf(stderr, " (%d new)", working); | |
1679 | fprintf(stderr, " but not started\n"); | |
1680 | } | |
7cb2aa33 N |
1681 | return 1; |
1682 | } | |
6234c63c DW |
1683 | } |
1684 | #endif | |
1685 |