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