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