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