]>
Commit | Line | Data |
---|---|---|
64c4757e | 1 | /* |
9a9dab36 | 2 | * mdadm - manage Linux "md" devices aka RAID arrays. |
64c4757e | 3 | * |
6f02172d | 4 | * Copyright (C) 2001-2013 Neil Brown <neilb@suse.de> |
64c4757e NB |
5 | * |
6 | * | |
7 | * This program is free software; you can redistribute it and/or modify | |
8 | * it under the terms of the GNU General Public License as published by | |
9 | * the Free Software Foundation; either version 2 of the License, or | |
10 | * (at your option) any later version. | |
11 | * | |
12 | * This program is distributed in the hope that it will be useful, | |
13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
15 | * GNU General Public License for more details. | |
16 | * | |
17 | * You should have received a copy of the GNU General Public License | |
18 | * along with this program; if not, write to the Free Software | |
19 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA | |
20 | * | |
21 | * Author: Neil Brown | |
e736b623 | 22 | * Email: <neilb@suse.de> |
64c4757e NB |
23 | */ |
24 | ||
9a9dab36 | 25 | #include "mdadm.h" |
41a3b72a | 26 | #include <ctype.h> |
64c4757e | 27 | |
624920bb NB |
28 | static int name_matches(char *found, char *required, char *homehost) |
29 | { | |
30 | /* See if the name found matches the required name, possibly | |
31 | * prefixed with 'homehost' | |
32 | */ | |
33 | char fnd[33]; | |
34 | ||
35 | strncpy(fnd, found, 32); | |
36 | fnd[32] = 0; | |
37 | if (strcmp(found, required)==0) | |
38 | return 1; | |
39 | if (homehost) { | |
40 | int l = strlen(homehost); | |
41 | if (l < 32 && fnd[l] == ':' && | |
42 | strcmp(fnd+l+1, required)==0) | |
43 | return 1; | |
44 | } | |
45 | return 0; | |
46 | } | |
47 | ||
9008ed1c | 48 | static int is_member_busy(char *metadata_version) |
2de8884f DW |
49 | { |
50 | /* check if the given member array is active */ | |
b4924f46 | 51 | struct mdstat_ent *mdstat = mdstat_read(0, 0); |
2de8884f DW |
52 | struct mdstat_ent *ent; |
53 | int busy = 0; | |
54 | ||
55 | for (ent = mdstat; ent; ent = ent->next) { | |
56 | if (ent->metadata_version == NULL) | |
57 | continue; | |
58 | if (strncmp(ent->metadata_version, "external:", 9) != 0) | |
59 | continue; | |
60 | if (!is_subarray(&ent->metadata_version[9])) | |
61 | continue; | |
62 | /* Skip first char - it can be '/' or '-' */ | |
63 | if (strcmp(&ent->metadata_version[10], metadata_version+1) == 0) { | |
64 | busy = 1; | |
65 | break; | |
66 | } | |
67 | } | |
68 | free_mdstat(mdstat); | |
69 | ||
70 | return busy; | |
71 | } | |
72 | ||
fa56eddb | 73 | static int ident_matches(struct mddev_ident *ident, |
08fb91a3 N |
74 | struct mdinfo *content, |
75 | struct supertype *tst, | |
76 | char *homehost, | |
77 | char *update, char *devname) | |
78 | { | |
79 | ||
80 | if (ident->uuid_set && (!update || strcmp(update, "uuid")!= 0) && | |
cbeeb0e5 AC |
81 | same_uuid(content->uuid, ident->uuid, tst->ss->swapuuid)==0 && |
82 | memcmp(content->uuid, uuid_zero, sizeof(int[4])) != 0) { | |
08fb91a3 | 83 | if (devname) |
e7b84f9d | 84 | pr_err("%s has wrong uuid.\n", devname); |
08fb91a3 N |
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) | |
e7b84f9d | 90 | pr_err("%s has wrong name.\n", devname); |
08fb91a3 N |
91 | return 0; |
92 | } | |
93 | if (ident->super_minor != UnSet && | |
94 | ident->super_minor != content->array.md_minor) { | |
95 | if (devname) | |
e7b84f9d N |
96 | pr_err("%s has wrong super-minor.\n", |
97 | devname); | |
08fb91a3 N |
98 | return 0; |
99 | } | |
100 | if (ident->level != UnSet && | |
101 | ident->level != content->array.level) { | |
102 | if (devname) | |
e7b84f9d N |
103 | pr_err("%s has wrong raid level.\n", |
104 | devname); | |
08fb91a3 N |
105 | return 0; |
106 | } | |
107 | if (ident->raid_disks != UnSet && | |
babb8dd4 | 108 | content->array.raid_disks != 0 && /* metadata doesn't know how many to expect */ |
08fb91a3 N |
109 | ident->raid_disks!= content->array.raid_disks) { |
110 | if (devname) | |
e7b84f9d N |
111 | pr_err("%s requires wrong number of drives.\n", |
112 | devname); | |
08fb91a3 N |
113 | return 0; |
114 | } | |
805d30b2 N |
115 | if (ident->member && ident->member[0]) { |
116 | /* content->text_version must match */ | |
117 | char *s = strchr(content->text_version+1, '/'); | |
118 | if (s == NULL) { | |
119 | if (devname) | |
e7b84f9d N |
120 | pr_err("%s is not a container and one is required.\n", |
121 | devname); | |
805d30b2 N |
122 | return 0; |
123 | } else if (strcmp(ident->member, s+1) != 0) { | |
124 | if (devname) | |
e7b84f9d N |
125 | pr_err("skipping wrong member %s is %s\n", |
126 | content->text_version, devname); | |
805d30b2 N |
127 | return 0; |
128 | } | |
129 | } | |
08fb91a3 N |
130 | return 1; |
131 | } | |
08fb91a3 | 132 | |
95425a89 N |
133 | static int select_devices(struct mddev_dev *devlist, |
134 | struct mddev_ident *ident, | |
135 | struct supertype **stp, | |
136 | struct mdinfo **contentp, | |
137 | struct context *c, | |
138 | int inargv, int auto_assem) | |
64c4757e | 139 | { |
a655e550 | 140 | struct mddev_dev *tmpdev; |
95425a89 N |
141 | int num_devs; |
142 | struct supertype *st = *stp; | |
98dbd966 | 143 | struct mdinfo *content = NULL; |
8cf2eb96 | 144 | int report_mismatch = ((inargv && c->verbose >= 0) || c->verbose > 0); |
cbeeb0e5 | 145 | struct domainlist *domains = NULL; |
60e1bc1a | 146 | |
5787fa49 NB |
147 | tmpdev = devlist; num_devs = 0; |
148 | while (tmpdev) { | |
da6b5ca9 NB |
149 | if (tmpdev->used) |
150 | tmpdev->used = 2; | |
151 | else | |
152 | num_devs++; | |
0431869c | 153 | tmpdev->disposition = 0; |
5787fa49 NB |
154 | tmpdev = tmpdev->next; |
155 | } | |
5787fa49 | 156 | |
811e6cbe NB |
157 | /* first walk the list of devices to find a consistent set |
158 | * that match the criterea, if that is possible. | |
c30e5369 | 159 | * We flag the ones we like with 'used'. |
811e6cbe NB |
160 | */ |
161 | for (tmpdev = devlist; | |
162 | tmpdev; | |
5083d66b | 163 | tmpdev = tmpdev ? tmpdev->next : NULL) { |
811e6cbe | 164 | char *devname = tmpdev->devname; |
64c4757e NB |
165 | int dfd; |
166 | struct stat stb; | |
518a60f3 | 167 | struct supertype *tst; |
4e8d9f0a | 168 | struct dev_policy *pol = NULL; |
5083d66b | 169 | int found_container = 0; |
52826846 | 170 | |
95425a89 N |
171 | if (tmpdev->used > 1) |
172 | continue; | |
da6b5ca9 | 173 | |
c39b2e63 N |
174 | if (ident->container) { |
175 | if (ident->container[0] == '/' && | |
176 | !same_dev(ident->container, devname)) { | |
177 | if (report_mismatch) | |
178 | pr_err("%s is not the container required (%s)\n", | |
179 | devname, ident->container); | |
180 | continue; | |
181 | } | |
182 | } else if (ident->devices && | |
183 | !match_oneof(ident->devices, devname)) { | |
184 | /* Note that we ignore the "device=" identifier if a | |
185 | * "container=" is given. Checking both is unnecessarily | |
186 | * complicated. | |
187 | */ | |
8cf2eb96 | 188 | if (report_mismatch) |
e7b84f9d | 189 | pr_err("%s is not one of %s\n", devname, ident->devices); |
52826846 | 190 | continue; |
56eedc1a | 191 | } |
4b1ac34b | 192 | |
518a60f3 JS |
193 | tst = dup_super(st); |
194 | ||
56d18859 | 195 | dfd = dev_open(devname, O_RDONLY); |
64c4757e | 196 | if (dfd < 0) { |
8cf2eb96 | 197 | if (report_mismatch) |
e7b84f9d N |
198 | pr_err("cannot open device %s: %s\n", |
199 | devname, strerror(errno)); | |
da6b5ca9 | 200 | tmpdev->used = 2; |
52826846 NB |
201 | } else if (fstat(dfd, &stb)< 0) { |
202 | /* Impossible! */ | |
e7b84f9d N |
203 | pr_err("fstat failed for %s: %s\n", |
204 | devname, strerror(errno)); | |
da6b5ca9 | 205 | tmpdev->used = 2; |
cd29a5c8 | 206 | } else if ((stb.st_mode & S_IFMT) != S_IFBLK) { |
e7b84f9d N |
207 | pr_err("%s is not a block device.\n", |
208 | devname); | |
da6b5ca9 | 209 | tmpdev->used = 2; |
5083d66b N |
210 | } else if (must_be_container(dfd)) { |
211 | if (st) { | |
212 | /* already found some components, this cannot | |
213 | * be another one. | |
214 | */ | |
8cf2eb96 | 215 | if (report_mismatch) |
e7b84f9d N |
216 | pr_err("%s is a container, but we are looking for components\n", |
217 | devname); | |
5083d66b | 218 | tmpdev->used = 2; |
71204a50 | 219 | #if !defined(MDASSEMBLE) || defined(MDASSEMBLE) && defined(MDASSEMBLE_AUTO) |
5083d66b | 220 | } if (!tst && (tst = super_by_fd(dfd, NULL)) == NULL) { |
8cf2eb96 | 221 | if (report_mismatch) |
e7b84f9d N |
222 | pr_err("not a recognisable container: %s\n", |
223 | devname); | |
5083d66b | 224 | tmpdev->used = 2; |
71204a50 | 225 | #endif |
417f346e HCP |
226 | } else if (!tst->ss->load_container |
227 | || tst->ss->load_container(tst, dfd, NULL)) { | |
8cf2eb96 | 228 | if (report_mismatch) |
e7b84f9d N |
229 | pr_err("no correct container type: %s\n", |
230 | devname); | |
5083d66b N |
231 | tmpdev->used = 2; |
232 | } else if (auto_assem && | |
4dd2df09 | 233 | !conf_test_metadata(tst->ss->name, (pol = devid_policy(stb.st_rdev)), |
4977146a | 234 | tst->ss->match_home(tst, c->homehost) == 1)) { |
8cf2eb96 | 235 | if (report_mismatch) |
7a862a02 | 236 | pr_err("%s has metadata type %s for which auto-assembly is disabled\n", |
e7b84f9d | 237 | devname, tst->ss->name); |
5083d66b N |
238 | tmpdev->used = 2; |
239 | } else | |
240 | found_container = 1; | |
52826846 | 241 | } else { |
5083d66b | 242 | if (!tst && (tst = guess_super(dfd)) == NULL) { |
8cf2eb96 | 243 | if (report_mismatch) |
e7b84f9d N |
244 | pr_err("no recogniseable superblock on %s\n", |
245 | devname); | |
5083d66b N |
246 | tmpdev->used = 2; |
247 | } else if (tst->ss->load_super(tst,dfd, NULL)) { | |
8cf2eb96 | 248 | if (report_mismatch) |
e7b84f9d N |
249 | pr_err("no RAID superblock on %s\n", |
250 | devname); | |
5083d66b N |
251 | tmpdev->used = 2; |
252 | } else if (tst->ss->compare_super == NULL) { | |
8cf2eb96 | 253 | if (report_mismatch) |
e7b84f9d N |
254 | pr_err("Cannot assemble %s metadata on %s\n", |
255 | tst->ss->name, devname); | |
5083d66b N |
256 | tmpdev->used = 2; |
257 | } else if (auto_assem && st == NULL && | |
4dd2df09 | 258 | !conf_test_metadata(tst->ss->name, (pol = devid_policy(stb.st_rdev)), |
4977146a | 259 | tst->ss->match_home(tst, c->homehost) == 1)) { |
8cf2eb96 | 260 | if (report_mismatch) |
7a862a02 | 261 | pr_err("%s has metadata type %s for which auto-assembly is disabled\n", |
e7b84f9d | 262 | devname, tst->ss->name); |
5083d66b N |
263 | tmpdev->used = 2; |
264 | } | |
64c4757e | 265 | } |
c06487ce | 266 | if (dfd >= 0) close(dfd); |
d68ea4d7 | 267 | if (tmpdev->used == 2) { |
d4386799 | 268 | if (auto_assem || !inargv) |
d68ea4d7 N |
269 | /* Ignore unrecognised devices during auto-assembly */ |
270 | goto loop; | |
271 | if (ident->uuid_set || ident->name[0] || | |
272 | ident->super_minor != UnSet) | |
273 | /* Ignore unrecognised device if looking for | |
274 | * specific array */ | |
275 | goto loop; | |
d68ea4d7 | 276 | |
e7b84f9d | 277 | pr_err("%s has no superblock - assembly aborted\n", |
79f9f56d | 278 | devname); |
d68ea4d7 N |
279 | if (st) |
280 | st->ss->free_super(st); | |
281 | dev_policy_free(pol); | |
cbeeb0e5 | 282 | domain_free(domains); |
95425a89 | 283 | return -1; |
d68ea4d7 | 284 | } |
52826846 | 285 | |
5083d66b | 286 | if (found_container) { |
9008ed1c N |
287 | /* tmpdev is a container. We need to be either |
288 | * looking for a member, or auto-assembling | |
289 | */ | |
56d18859 N |
290 | /* should be safe to try an exclusive open now, we |
291 | * have rejected anything that some other mdadm might | |
292 | * be looking at | |
293 | */ | |
294 | dfd = dev_open(devname, O_RDONLY | O_EXCL); | |
295 | if (dfd < 0) { | |
8cf2eb96 | 296 | if (report_mismatch) |
e7b84f9d | 297 | pr_err("%s is busy - skipping\n", devname); |
56d18859 N |
298 | goto loop; |
299 | } | |
300 | close(dfd); | |
9008ed1c | 301 | |
c39b2e63 N |
302 | if (ident->container && ident->container[0] != '/') { |
303 | /* we have a uuid */ | |
304 | int uuid[4]; | |
87477e6d | 305 | |
c39b2e63 N |
306 | content = *contentp; |
307 | tst->ss->getinfo_super(tst, content, NULL); | |
87477e6d | 308 | |
c39b2e63 N |
309 | if (!parse_uuid(ident->container, uuid) || |
310 | !same_uuid(content->uuid, uuid, tst->ss->swapuuid)) { | |
311 | if (report_mismatch) | |
312 | pr_err("%s has wrong UUID to be required container\n", | |
313 | devname); | |
314 | goto loop; | |
9008ed1c N |
315 | } |
316 | } | |
317 | /* It is worth looking inside this container. | |
318 | */ | |
4977146a | 319 | if (c->verbose > 0) |
e7b84f9d | 320 | pr_err("looking in container %s\n", |
79f9f56d | 321 | devname); |
1415fe4b | 322 | |
88cef9b3 N |
323 | for (content = tst->ss->container_content(tst, NULL); |
324 | content; | |
325 | content = content->next) { | |
1415fe4b | 326 | |
88cef9b3 | 327 | if (!ident_matches(ident, content, tst, |
4977146a | 328 | c->homehost, c->update, |
8cf2eb96 | 329 | report_mismatch ? devname : NULL)) |
88cef9b3 N |
330 | /* message already printed */; |
331 | else if (is_member_busy(content->text_version)) { | |
8cf2eb96 | 332 | if (report_mismatch) |
e7b84f9d | 333 | pr_err("member %s in %s is already assembled\n", |
79f9f56d N |
334 | content->text_version, |
335 | devname); | |
81219e70 LM |
336 | } else if (content->array.state & (1<<MD_SB_BLOCK_VOLUME)) { |
337 | /* do not assemble arrays with unsupported configurations */ | |
e7b84f9d | 338 | pr_err("Cannot activate member %s in %s.\n", |
79f9f56d N |
339 | content->text_version, |
340 | devname); | |
88cef9b3 N |
341 | } else |
342 | break; | |
343 | } | |
344 | if (!content) { | |
f7ad3ccc | 345 | tmpdev->used = 2; |
88cef9b3 | 346 | goto loop; /* empty container */ |
fa031239 | 347 | } |
88cef9b3 | 348 | |
9008ed1c | 349 | st = tst; tst = NULL; |
4c1c3ad8 | 350 | if (!auto_assem && inargv && tmpdev->next != NULL) { |
7a862a02 | 351 | pr_err("%s is a container, but is not only device given: confused and aborting\n", |
79f9f56d | 352 | devname); |
9008ed1c | 353 | st->ss->free_super(st); |
4e8d9f0a | 354 | dev_policy_free(pol); |
cbeeb0e5 | 355 | domain_free(domains); |
95425a89 | 356 | return -1; |
9008ed1c | 357 | } |
4977146a | 358 | if (c->verbose > 0) |
e7b84f9d | 359 | pr_err("found match on member %s in %s\n", |
79f9f56d | 360 | content->text_version, devname); |
bac0d92e | 361 | |
5083d66b N |
362 | /* make sure we finished the loop */ |
363 | tmpdev = NULL; | |
bac0d92e | 364 | goto loop; |
5083d66b | 365 | } else { |
95425a89 | 366 | content = *contentp; |
5083d66b N |
367 | tst->ss->getinfo_super(tst, content, NULL); |
368 | ||
369 | if (!ident_matches(ident, content, tst, | |
4977146a | 370 | c->homehost, c->update, |
8cf2eb96 | 371 | report_mismatch ? devname : NULL)) |
df37ffc0 | 372 | goto loop; |
66eb2c93 | 373 | |
5141638c N |
374 | if (auto_assem) { |
375 | /* Never auto-assemble things that conflict | |
376 | * with mdadm.conf in some way | |
1d04e275 | 377 | */ |
5141638c N |
378 | struct mddev_ident *match; |
379 | int rv = 0; | |
380 | ||
381 | match = conf_match(tst, content, devname, | |
382 | report_mismatch ? c->verbose : -1, | |
383 | &rv); | |
384 | if (!match && rv == 2) | |
385 | goto loop; | |
386 | if (match && match->devname && | |
387 | strcasecmp(match->devname, "<ignore>") == 0) { | |
388 | if (report_mismatch) | |
389 | pr_err("%s is a member of an explicitly ignored array\n", | |
390 | devname); | |
391 | goto loop; | |
392 | } | |
393 | if (match && !ident_matches(match, content, tst, | |
394 | c->homehost, c->update, | |
395 | report_mismatch ? devname : NULL)) | |
396 | /* Array exists in mdadm.conf but some | |
397 | * details don't match, so reject it | |
398 | */ | |
399 | goto loop; | |
400 | } | |
66eb2c93 | 401 | |
56d18859 N |
402 | /* should be safe to try an exclusive open now, we |
403 | * have rejected anything that some other mdadm might | |
404 | * be looking at | |
405 | */ | |
406 | dfd = dev_open(devname, O_RDONLY | O_EXCL); | |
407 | if (dfd < 0) { | |
8cf2eb96 | 408 | if (report_mismatch) |
e7b84f9d | 409 | pr_err("%s is busy - skipping\n", devname); |
56d18859 N |
410 | goto loop; |
411 | } | |
412 | close(dfd); | |
413 | ||
5083d66b N |
414 | if (st == NULL) |
415 | st = dup_super(tst); | |
416 | if (st->minor_version == -1) | |
417 | st->minor_version = tst->minor_version; | |
ed7fc6b4 AC |
418 | |
419 | if (memcmp(content->uuid, uuid_zero, | |
420 | sizeof(int[4])) == 0) { | |
421 | /* this is a floating spare. It cannot define | |
422 | * an array unless there are no more arrays of | |
423 | * this type to be found. It can be included | |
424 | * in an array of this type though. | |
425 | */ | |
426 | tmpdev->used = 3; | |
427 | goto loop; | |
428 | } | |
429 | ||
5083d66b N |
430 | if (st->ss != tst->ss || |
431 | st->minor_version != tst->minor_version || | |
432 | st->ss->compare_super(st, tst) != 0) { | |
433 | /* Some mismatch. If exactly one array matches this host, | |
434 | * we can resolve on that one. | |
435 | * Or, if we are auto assembling, we just ignore the second | |
436 | * for now. | |
437 | */ | |
438 | if (auto_assem) | |
439 | goto loop; | |
4977146a N |
440 | if (c->homehost) { |
441 | int first = st->ss->match_home(st, c->homehost); | |
442 | int last = tst->ss->match_home(tst, c->homehost); | |
5083d66b N |
443 | if (first != last && |
444 | (first == 1 || last == 1)) { | |
445 | /* We can do something */ | |
446 | if (first) {/* just ignore this one */ | |
8cf2eb96 | 447 | if (report_mismatch) |
e7b84f9d | 448 | pr_err("%s misses out due to wrong homehost\n", |
79f9f56d | 449 | devname); |
5083d66b N |
450 | goto loop; |
451 | } else { /* reject all those sofar */ | |
452 | struct mddev_dev *td; | |
8cf2eb96 | 453 | if (report_mismatch) |
e7b84f9d | 454 | pr_err("%s overrides previous devices due to good homehost\n", |
79f9f56d | 455 | devname); |
5083d66b N |
456 | for (td=devlist; td != tmpdev; td=td->next) |
457 | if (td->used == 1) | |
458 | td->used = 0; | |
459 | tmpdev->used = 1; | |
460 | goto loop; | |
461 | } | |
83b6208e NB |
462 | } |
463 | } | |
e7b84f9d | 464 | pr_err("superblock on %s doesn't match others - assembly aborted\n", |
79f9f56d | 465 | devname); |
5083d66b N |
466 | tst->ss->free_super(tst); |
467 | st->ss->free_super(st); | |
468 | dev_policy_free(pol); | |
cbeeb0e5 | 469 | domain_free(domains); |
95425a89 | 470 | return -1; |
83b6208e | 471 | } |
5083d66b | 472 | tmpdev->used = 1; |
64c4757e | 473 | } |
df37ffc0 | 474 | loop: |
cbeeb0e5 | 475 | /* Collect domain information from members only */ |
26b05aea AC |
476 | if (tmpdev && tmpdev->used == 1) { |
477 | if (!pol) | |
4dd2df09 | 478 | pol = devid_policy(stb.st_rdev); |
cbeeb0e5 | 479 | domain_merge(&domains, pol, tst?tst->ss->name:NULL); |
26b05aea | 480 | } |
4e8d9f0a N |
481 | dev_policy_free(pol); |
482 | pol = NULL; | |
3d2b16e7 NB |
483 | if (tst) |
484 | tst->ss->free_super(tst); | |
811e6cbe NB |
485 | } |
486 | ||
ed7fc6b4 | 487 | /* Check if we found some imsm spares but no members */ |
3c7b4a25 CA |
488 | if ((auto_assem || |
489 | (ident->uuid_set && | |
490 | memcmp(uuid_zero, ident->uuid,sizeof(uuid_zero)) == 0)) && | |
491 | (!st || !st->sb)) | |
ed7fc6b4 AC |
492 | for (tmpdev = devlist; tmpdev; tmpdev = tmpdev->next) { |
493 | if (tmpdev->used != 3) | |
494 | continue; | |
495 | tmpdev->used = 1; | |
95425a89 | 496 | content = *contentp; |
ed7fc6b4 AC |
497 | |
498 | if (!st->sb) { | |
499 | /* we need sb from one of the spares */ | |
500 | int dfd = dev_open(tmpdev->devname, O_RDONLY); | |
501 | if (dfd < 0 || | |
502 | st->ss->load_super(st, dfd, NULL)) | |
503 | tmpdev->used = 2; | |
504 | if (dfd > 0) | |
505 | close(dfd); | |
506 | } | |
507 | } | |
508 | ||
cbeeb0e5 AC |
509 | /* Now reject spares that don't match domains of identified members */ |
510 | for (tmpdev = devlist; tmpdev; tmpdev = tmpdev->next) { | |
511 | struct stat stb; | |
512 | if (tmpdev->used != 3) | |
513 | continue; | |
514 | if (stat(tmpdev->devname, &stb)< 0) { | |
e7b84f9d | 515 | pr_err("fstat failed for %s: %s\n", |
79f9f56d | 516 | tmpdev->devname, strerror(errno)); |
cbeeb0e5 AC |
517 | tmpdev->used = 2; |
518 | } else { | |
4dd2df09 | 519 | struct dev_policy *pol = devid_policy(stb.st_rdev); |
a5d10dce N |
520 | int dt = domain_test(domains, pol, NULL); |
521 | if (inargv && dt != 0) | |
522 | /* take this spare as domains match | |
523 | * if there are any */ | |
524 | tmpdev->used = 1; | |
525 | else if (!inargv && dt == 1) | |
526 | /* device wasn't explicitly listed, so need | |
527 | * explicit domain match - which we have */ | |
cbeeb0e5 AC |
528 | tmpdev->used = 1; |
529 | else | |
530 | /* if domains don't match mark as unused */ | |
531 | tmpdev->used = 0; | |
532 | dev_policy_free(pol); | |
533 | } | |
534 | } | |
535 | domain_free(domains); | |
95425a89 N |
536 | *stp = st; |
537 | if (st && st->sb && content == *contentp) | |
538 | st->ss->getinfo_super(st, content, NULL); | |
539 | *contentp = content; | |
540 | ||
541 | return num_devs; | |
542 | } | |
543 | ||
2c355c22 N |
544 | struct devs { |
545 | char *devname; | |
546 | int uptodate; /* set once we decide that this device is as | |
547 | * recent as everything else in the array. | |
548 | */ | |
549 | int included; /* set if the device is already in the array | |
550 | * due to a previous '-I' | |
551 | */ | |
552 | struct mdinfo i; | |
553 | }; | |
554 | ||
555 | static int load_devices(struct devs *devices, char *devmap, | |
df842e69 | 556 | struct mddev_ident *ident, struct supertype **stp, |
2c355c22 N |
557 | struct mddev_dev *devlist, struct context *c, |
558 | struct mdinfo *content, | |
559 | int mdfd, char *mddev, | |
560 | int *most_recentp, int *bestcntp, int **bestp, | |
561 | int inargv) | |
562 | { | |
563 | struct mddev_dev *tmpdev; | |
564 | int devcnt = 0; | |
565 | int nextspare = 0; | |
566 | #ifndef MDASSEMBLE | |
567 | int bitmap_done = 0; | |
568 | #endif | |
a792ece6 | 569 | int most_recent = -1; |
2c355c22 N |
570 | int bestcnt = 0; |
571 | int *best = *bestp; | |
df842e69 | 572 | struct supertype *st = *stp; |
2c355c22 N |
573 | |
574 | for (tmpdev = devlist; tmpdev; tmpdev=tmpdev->next) { | |
575 | char *devname = tmpdev->devname; | |
576 | struct stat stb; | |
9ee314da | 577 | struct supertype *tst; |
2c355c22 | 578 | int i; |
e9e6894d | 579 | int dfd; |
2c355c22 N |
580 | |
581 | if (tmpdev->used != 1) | |
582 | continue; | |
583 | /* looks like a good enough match to update the super block if needed */ | |
584 | #ifndef MDASSEMBLE | |
585 | if (c->update) { | |
2c355c22 N |
586 | /* prepare useful information in info structures */ |
587 | struct stat stb2; | |
2c355c22 N |
588 | int err; |
589 | fstat(mdfd, &stb2); | |
590 | ||
591 | if (strcmp(c->update, "uuid")==0 && | |
592 | !ident->uuid_set) { | |
593 | int rfd; | |
594 | if ((rfd = open("/dev/urandom", O_RDONLY)) < 0 || | |
595 | read(rfd, ident->uuid, 16) != 16) { | |
596 | *(__u32*)(ident->uuid) = random(); | |
597 | *(__u32*)(ident->uuid+1) = random(); | |
598 | *(__u32*)(ident->uuid+2) = random(); | |
599 | *(__u32*)(ident->uuid+3) = random(); | |
600 | } | |
601 | if (rfd >= 0) close(rfd); | |
602 | } | |
603 | dfd = dev_open(devname, | |
604 | tmpdev->disposition == 'I' | |
605 | ? O_RDWR : (O_RDWR|O_EXCL)); | |
606 | ||
607 | tst = dup_super(st); | |
608 | if (dfd < 0 || tst->ss->load_super(tst, dfd, NULL) != 0) { | |
609 | pr_err("cannot re-read metadata from %s - aborting\n", | |
610 | devname); | |
611 | if (dfd >= 0) | |
612 | close(dfd); | |
613 | close(mdfd); | |
614 | free(devices); | |
615 | free(devmap); | |
9ee314da N |
616 | tst->ss->free_super(tst); |
617 | free(tst); | |
df842e69 | 618 | *stp = st; |
2c355c22 N |
619 | return -1; |
620 | } | |
621 | tst->ss->getinfo_super(tst, content, devmap + devcnt * content->array.raid_disks); | |
622 | ||
623 | memcpy(content->uuid, ident->uuid, 16); | |
624 | strcpy(content->name, ident->name); | |
625 | content->array.md_minor = minor(stb2.st_rdev); | |
626 | ||
627 | if (strcmp(c->update, "byteorder") == 0) | |
628 | err = 0; | |
0aa2f15b GJ |
629 | else if (strcmp(c->update, "home-cluster") == 0) { |
630 | tst->cluster_name = c->homecluster; | |
631 | tst->ss->write_bitmap(tst, dfd, NameUpdate); | |
7e6e839a GJ |
632 | } else if (strcmp(c->update, "nodes") == 0) { |
633 | tst->nodes = c->nodes; | |
634 | err = tst->ss->write_bitmap(tst, dfd, NodeNumUpdate); | |
0aa2f15b | 635 | } else |
2c355c22 N |
636 | err = tst->ss->update_super(tst, content, c->update, |
637 | devname, c->verbose, | |
638 | ident->uuid_set, | |
639 | c->homehost); | |
640 | if (err < 0) { | |
afa368f4 | 641 | if (err == -1) |
7a862a02 | 642 | pr_err("--update=%s not understood for %s metadata\n", |
afa368f4 | 643 | c->update, tst->ss->name); |
2c355c22 N |
644 | tst->ss->free_super(tst); |
645 | free(tst); | |
646 | close(mdfd); | |
647 | close(dfd); | |
648 | free(devices); | |
649 | free(devmap); | |
df842e69 | 650 | *stp = st; |
2c355c22 N |
651 | return -1; |
652 | } | |
653 | if (strcmp(c->update, "uuid")==0 && | |
654 | !ident->uuid_set) { | |
655 | ident->uuid_set = 1; | |
656 | memcpy(ident->uuid, content->uuid, 16); | |
657 | } | |
658 | if (tst->ss->store_super(tst, dfd)) | |
659 | pr_err("Could not re-write superblock on %s.\n", | |
660 | devname); | |
2c355c22 N |
661 | |
662 | if (strcmp(c->update, "uuid")==0 && | |
663 | ident->bitmap_fd >= 0 && !bitmap_done) { | |
664 | if (bitmap_update_uuid(ident->bitmap_fd, | |
665 | content->uuid, | |
666 | tst->ss->swapuuid) != 0) | |
667 | pr_err("Could not update uuid on external bitmap.\n"); | |
668 | else | |
669 | bitmap_done = 1; | |
670 | } | |
2c355c22 N |
671 | } else |
672 | #endif | |
673 | { | |
e9e6894d N |
674 | dfd = dev_open(devname, |
675 | tmpdev->disposition == 'I' | |
676 | ? O_RDWR : (O_RDWR|O_EXCL)); | |
9ee314da | 677 | tst = dup_super(st); |
2c355c22 N |
678 | |
679 | if (dfd < 0 || tst->ss->load_super(tst, dfd, NULL) != 0) { | |
680 | pr_err("cannot re-read metadata from %s - aborting\n", | |
681 | devname); | |
682 | if (dfd >= 0) | |
683 | close(dfd); | |
684 | close(mdfd); | |
685 | free(devices); | |
686 | free(devmap); | |
9ee314da N |
687 | tst->ss->free_super(tst); |
688 | free(tst); | |
df842e69 | 689 | *stp = st; |
2c355c22 N |
690 | return -1; |
691 | } | |
692 | tst->ss->getinfo_super(tst, content, devmap + devcnt * content->array.raid_disks); | |
2c355c22 N |
693 | } |
694 | ||
e9e6894d N |
695 | fstat(dfd, &stb); |
696 | close(dfd); | |
2c355c22 N |
697 | |
698 | if (c->verbose > 0) | |
aacb2f81 N |
699 | pr_err("%s is identified as a member of %s, slot %d%s.\n", |
700 | devname, mddev, content->disk.raid_disk, | |
701 | (content->disk.state & (1<<MD_DISK_REPLACEMENT)) ? " replacement":""); | |
2c355c22 N |
702 | devices[devcnt].devname = devname; |
703 | devices[devcnt].uptodate = 0; | |
704 | devices[devcnt].included = (tmpdev->disposition == 'I'); | |
705 | devices[devcnt].i = *content; | |
706 | devices[devcnt].i.disk.major = major(stb.st_rdev); | |
707 | devices[devcnt].i.disk.minor = minor(stb.st_rdev); | |
f80057ae | 708 | |
a792ece6 N |
709 | if (devices[devcnt].i.disk.state == 6) { |
710 | if (most_recent < 0 || | |
711 | devices[devcnt].i.events | |
56bbc588 N |
712 | > devices[most_recent].i.events) { |
713 | struct supertype *tmp = tst; | |
714 | tst = st; | |
715 | st = tmp; | |
2c355c22 | 716 | most_recent = devcnt; |
56bbc588 | 717 | } |
a792ece6 | 718 | } |
9ee314da N |
719 | tst->ss->free_super(tst); |
720 | free(tst); | |
f80057ae | 721 | |
2c355c22 N |
722 | if (content->array.level == LEVEL_MULTIPATH) |
723 | /* with multipath, the raid_disk from the superblock is meaningless */ | |
724 | i = devcnt; | |
725 | else | |
726 | i = devices[devcnt].i.disk.raid_disk; | |
727 | if (i+1 == 0) { | |
aacb2f81 N |
728 | if (nextspare < content->array.raid_disks*2) |
729 | nextspare = content->array.raid_disks*2; | |
2c355c22 N |
730 | i = nextspare++; |
731 | } else { | |
aacb2f81 N |
732 | /* i is raid_disk - double it so there is room for |
733 | * replacements */ | |
734 | i *= 2; | |
735 | if (devices[devcnt].i.disk.state & (1<<MD_DISK_REPLACEMENT)) | |
736 | i++; | |
737 | if (i >= content->array.raid_disks*2 && | |
2c355c22 N |
738 | i >= nextspare) |
739 | nextspare = i+1; | |
740 | } | |
741 | if (i < 10000) { | |
742 | if (i >= bestcnt) { | |
743 | int newbestcnt = i+10; | |
744 | int *newbest = xmalloc(sizeof(int)*newbestcnt); | |
745 | int c; | |
746 | for (c=0; c < newbestcnt; c++) | |
747 | if (c < bestcnt) | |
748 | newbest[c] = best[c]; | |
749 | else | |
750 | newbest[c] = -1; | |
751 | if (best)free(best); | |
752 | best = newbest; | |
753 | bestcnt = newbestcnt; | |
754 | } | |
755 | if (best[i] >=0 && | |
756 | devices[best[i]].i.events | |
757 | == devices[devcnt].i.events | |
758 | && (devices[best[i]].i.disk.minor | |
759 | != devices[devcnt].i.disk.minor) | |
760 | && st->ss == &super0 | |
761 | && content->array.level != LEVEL_MULTIPATH) { | |
762 | /* two different devices with identical superblock. | |
763 | * Could be a mis-detection caused by overlapping | |
764 | * partitions. fail-safe. | |
765 | */ | |
7a862a02 N |
766 | pr_err("WARNING %s and %s appear to have very similar superblocks.\n" |
767 | " If they are really different, please --zero the superblock on one\n" | |
768 | " If they are the same or overlap, please remove one from %s.\n", | |
2c355c22 N |
769 | devices[best[i]].devname, devname, |
770 | inargv ? "the list" : | |
771 | "the\n DEVICE list in mdadm.conf" | |
772 | ); | |
773 | close(mdfd); | |
774 | free(devices); | |
775 | free(devmap); | |
df842e69 | 776 | *stp = st; |
2c355c22 N |
777 | return -1; |
778 | } | |
779 | if (best[i] == -1 | |
780 | || (devices[best[i]].i.events | |
781 | < devices[devcnt].i.events)) | |
782 | best[i] = devcnt; | |
783 | } | |
784 | devcnt++; | |
785 | } | |
a792ece6 N |
786 | if (most_recent >= 0) |
787 | *most_recentp = most_recent; | |
2c355c22 N |
788 | *bestcntp = bestcnt; |
789 | *bestp = best; | |
df842e69 | 790 | *stp = st; |
2c355c22 N |
791 | return devcnt; |
792 | } | |
793 | ||
9f5470ce N |
794 | static int force_array(struct mdinfo *content, |
795 | struct devs *devices, | |
796 | int *best, int bestcnt, char *avail, | |
797 | int most_recent, | |
798 | struct supertype *st, | |
799 | struct context *c) | |
800 | { | |
801 | int okcnt = 0; | |
802 | while (!enough(content->array.level, content->array.raid_disks, | |
803 | content->array.layout, 1, | |
804 | avail) | |
805 | || | |
806 | (content->reshape_active && content->delta_disks > 0 && | |
807 | !enough(content->array.level, (content->array.raid_disks | |
808 | - content->delta_disks), | |
809 | content->new_layout, 1, | |
810 | avail) | |
811 | )) { | |
812 | /* Choose the newest best drive which is | |
813 | * not up-to-date, update the superblock | |
814 | * and add it. | |
815 | */ | |
816 | int fd; | |
817 | struct supertype *tst; | |
818 | unsigned long long current_events; | |
819 | int chosen_drive = -1; | |
820 | int i; | |
821 | ||
f81a2b56 N |
822 | for (i = 0; |
823 | i < content->array.raid_disks * 2 && i < bestcnt; | |
824 | i += 2) { | |
9f5470ce | 825 | int j = best[i]; |
87af7267 N |
826 | if (j < 0) |
827 | continue; | |
828 | if (devices[j].uptodate) | |
829 | continue; | |
830 | if (devices[j].i.recovery_start != MaxSector) { | |
831 | int delta; | |
832 | if (!devices[j].i.reshape_active || | |
833 | devices[j].i.delta_disks <= 0) | |
834 | continue; | |
835 | /* When increasing number of devices, an | |
836 | * added device also appears to be | |
837 | * recovering. It is safe to include it | |
838 | * as long as it won't be a source of | |
839 | * data. | |
840 | * For now, just allow for last data | |
841 | * devices in RAID4 or last devices in RAID4/5/6. | |
842 | */ | |
843 | delta = devices[j].i.delta_disks; | |
844 | if (devices[j].i.array.level >= 4 && | |
845 | devices[j].i.array.level <= 6 && | |
846 | i/2 >= content->array.raid_disks - delta) | |
847 | /* OK */; | |
848 | else if (devices[j].i.array.level == 4 && | |
849 | i/2 >= content->array.raid_disks - delta - 1) | |
850 | /* OK */; | |
851 | else | |
852 | continue; | |
853 | } | |
854 | if (chosen_drive < 0 || | |
9f5470ce | 855 | devices[j].i.events |
87af7267 | 856 | > devices[chosen_drive].i.events) |
9f5470ce N |
857 | chosen_drive = j; |
858 | } | |
859 | if (chosen_drive < 0) | |
860 | break; | |
861 | current_events = devices[chosen_drive].i.events; | |
862 | add_another: | |
863 | if (c->verbose >= 0) | |
864 | pr_err("forcing event count in %s(%d) from %d upto %d\n", | |
79f9f56d N |
865 | devices[chosen_drive].devname, |
866 | devices[chosen_drive].i.disk.raid_disk, | |
867 | (int)(devices[chosen_drive].i.events), | |
868 | (int)(devices[most_recent].i.events)); | |
9f5470ce N |
869 | fd = dev_open(devices[chosen_drive].devname, |
870 | devices[chosen_drive].included ? O_RDWR | |
871 | : (O_RDWR|O_EXCL)); | |
872 | if (fd < 0) { | |
873 | pr_err("Couldn't open %s for write - not updating\n", | |
79f9f56d | 874 | devices[chosen_drive].devname); |
9f5470ce N |
875 | devices[chosen_drive].i.events = 0; |
876 | continue; | |
877 | } | |
878 | tst = dup_super(st); | |
879 | if (tst->ss->load_super(tst,fd, NULL)) { | |
880 | close(fd); | |
881 | pr_err("RAID superblock disappeared from %s - not updating.\n", | |
79f9f56d | 882 | devices[chosen_drive].devname); |
9f5470ce N |
883 | devices[chosen_drive].i.events = 0; |
884 | continue; | |
885 | } | |
886 | content->events = devices[most_recent].i.events; | |
887 | tst->ss->update_super(tst, content, "force-one", | |
79f9f56d N |
888 | devices[chosen_drive].devname, c->verbose, |
889 | 0, NULL); | |
9f5470ce N |
890 | |
891 | if (tst->ss->store_super(tst, fd)) { | |
892 | close(fd); | |
893 | pr_err("Could not re-write superblock on %s\n", | |
79f9f56d | 894 | devices[chosen_drive].devname); |
9f5470ce N |
895 | devices[chosen_drive].i.events = 0; |
896 | tst->ss->free_super(tst); | |
897 | continue; | |
898 | } | |
899 | close(fd); | |
900 | devices[chosen_drive].i.events = devices[most_recent].i.events; | |
901 | devices[chosen_drive].uptodate = 1; | |
902 | avail[chosen_drive] = 1; | |
903 | okcnt++; | |
904 | tst->ss->free_super(tst); | |
905 | ||
906 | /* If there are any other drives of the same vintage, | |
907 | * add them in as well. We can't lose and we might gain | |
908 | */ | |
f81a2b56 N |
909 | for (i = 0; |
910 | i < content->array.raid_disks * 2 && i < bestcnt ; | |
911 | i += 2) { | |
9f5470ce N |
912 | int j = best[i]; |
913 | if (j >= 0 && | |
914 | !devices[j].uptodate && | |
915 | devices[j].i.recovery_start == MaxSector && | |
916 | devices[j].i.events == current_events) { | |
917 | chosen_drive = j; | |
918 | goto add_another; | |
919 | } | |
920 | } | |
921 | } | |
922 | return okcnt; | |
923 | } | |
924 | ||
ddc1b11f N |
925 | static int start_array(int mdfd, |
926 | char *mddev, | |
927 | struct mdinfo *content, | |
928 | struct supertype *st, | |
929 | struct mddev_ident *ident, | |
930 | int *best, int bestcnt, | |
931 | int chosen_drive, | |
932 | struct devs *devices, | |
933 | unsigned int okcnt, | |
934 | unsigned int sparecnt, | |
935 | unsigned int rebuilding_cnt, | |
936 | struct context *c, | |
937 | int clean, char *avail, | |
8cde842b | 938 | int start_partial_ok, |
71417de6 | 939 | int err_ok, |
8cde842b | 940 | int was_forced |
ddc1b11f N |
941 | ) |
942 | { | |
943 | int rv; | |
944 | int i; | |
945 | unsigned int req_cnt; | |
946 | ||
947 | rv = set_array_info(mdfd, st, content); | |
71417de6 | 948 | if (rv && !err_ok) { |
ddc1b11f N |
949 | pr_err("failed to set array info for %s: %s\n", |
950 | mddev, strerror(errno)); | |
951 | return 1; | |
952 | } | |
953 | if (ident->bitmap_fd >= 0) { | |
954 | if (ioctl(mdfd, SET_BITMAP_FILE, ident->bitmap_fd) != 0) { | |
955 | pr_err("SET_BITMAP_FILE failed.\n"); | |
956 | return 1; | |
957 | } | |
958 | } else if (ident->bitmap_file) { | |
959 | /* From config file */ | |
960 | int bmfd = open(ident->bitmap_file, O_RDWR); | |
961 | if (bmfd < 0) { | |
962 | pr_err("Could not open bitmap file %s\n", | |
963 | ident->bitmap_file); | |
964 | return 1; | |
965 | } | |
966 | if (ioctl(mdfd, SET_BITMAP_FILE, bmfd) != 0) { | |
967 | pr_err("Failed to set bitmapfile for %s\n", mddev); | |
968 | close(bmfd); | |
969 | return 1; | |
970 | } | |
971 | close(bmfd); | |
972 | } | |
973 | ||
974 | /* First, add the raid disks, but add the chosen one last */ | |
975 | for (i=0; i<= bestcnt; i++) { | |
976 | int j; | |
977 | if (i < bestcnt) { | |
978 | j = best[i]; | |
979 | if (j == chosen_drive) | |
980 | continue; | |
981 | } else | |
982 | j = chosen_drive; | |
983 | ||
984 | if (j >= 0 && !devices[j].included) { | |
985 | int dfd = dev_open(devices[j].devname, | |
986 | O_RDWR|O_EXCL); | |
987 | if (dfd >= 0) { | |
988 | remove_partitions(dfd); | |
989 | close(dfd); | |
990 | } | |
991 | rv = add_disk(mdfd, st, content, &devices[j].i); | |
992 | ||
993 | if (rv) { | |
7a862a02 | 994 | pr_err("failed to add %s to %s: %s\n", |
ddc1b11f N |
995 | devices[j].devname, |
996 | mddev, | |
997 | strerror(errno)); | |
aacb2f81 | 998 | if (i < content->array.raid_disks * 2 |
ddc1b11f N |
999 | || i == bestcnt) |
1000 | okcnt--; | |
1001 | else | |
1002 | sparecnt--; | |
1003 | } else if (c->verbose > 0) | |
aacb2f81 | 1004 | pr_err("added %s to %s as %d%s%s\n", |
ddc1b11f N |
1005 | devices[j].devname, mddev, |
1006 | devices[j].i.disk.raid_disk, | |
1007 | devices[j].uptodate?"": | |
aacb2f81 N |
1008 | " (possibly out of date)", |
1009 | (devices[j].i.disk.state & (1<<MD_DISK_REPLACEMENT))?" replacement":""); | |
ddc1b11f N |
1010 | } else if (j >= 0) { |
1011 | if (c->verbose > 0) | |
1012 | pr_err("%s is already in %s as %d\n", | |
1013 | devices[j].devname, mddev, | |
1014 | devices[j].i.disk.raid_disk); | |
aacb2f81 N |
1015 | } else if (c->verbose > 0 && i < content->array.raid_disks*2 |
1016 | && (i&1) == 0) | |
ddc1b11f | 1017 | pr_err("no uptodate device for slot %d of %s\n", |
ee466574 | 1018 | i/2, mddev); |
ddc1b11f N |
1019 | } |
1020 | ||
1021 | if (content->array.level == LEVEL_CONTAINER) { | |
1022 | if (c->verbose >= 0) { | |
7a862a02 | 1023 | pr_err("Container %s has been assembled with %d drive%s", |
ddc1b11f N |
1024 | mddev, okcnt+sparecnt, okcnt+sparecnt==1?"":"s"); |
1025 | if (okcnt < (unsigned)content->array.raid_disks) | |
1026 | fprintf(stderr, " (out of %d)", | |
1027 | content->array.raid_disks); | |
1028 | fprintf(stderr, "\n"); | |
1029 | } | |
0c21b485 PB |
1030 | |
1031 | if (st->ss->validate_container) { | |
1032 | struct mdinfo *devices_list; | |
1033 | struct mdinfo *info_devices = xmalloc(sizeof(struct mdinfo)*(okcnt+sparecnt)); | |
1034 | unsigned int count; | |
1035 | devices_list = NULL; | |
1036 | for (count = 0; count < okcnt+sparecnt; count++) { | |
1037 | info_devices[count] = devices[count].i; | |
1038 | info_devices[count].next = devices_list; | |
1039 | devices_list = &info_devices[count]; | |
1040 | } | |
1041 | if (st->ss->validate_container(devices_list)) | |
1042 | pr_err("Mismatch detected!\n"); | |
1043 | free(info_devices); | |
1044 | } | |
1045 | ||
ddc1b11f N |
1046 | st->ss->free_super(st); |
1047 | sysfs_uevent(content, "change"); | |
284546ef N |
1048 | if (err_ok && okcnt < (unsigned)content->array.raid_disks) |
1049 | /* Was partial, is still partial, so signal an error | |
1050 | * to ensure we don't retry */ | |
1051 | return 1; | |
ddc1b11f N |
1052 | return 0; |
1053 | } | |
1054 | ||
1055 | /* Get number of in-sync devices according to the superblock. | |
1056 | * We must have this number to start the array without -s or -R | |
1057 | */ | |
1058 | req_cnt = content->array.working_disks; | |
1059 | ||
1060 | if (c->runstop == 1 || | |
1061 | (c->runstop <= 0 && | |
1062 | ( enough(content->array.level, content->array.raid_disks, | |
1063 | content->array.layout, clean, avail) && | |
1064 | (okcnt + rebuilding_cnt >= req_cnt || start_partial_ok) | |
1065 | ))) { | |
1066 | /* This array is good-to-go. | |
1067 | * If a reshape is in progress then we might need to | |
1068 | * continue monitoring it. In that case we start | |
1069 | * it read-only and let the grow code make it writable. | |
1070 | */ | |
1071 | int rv; | |
1072 | #ifndef MDASSEMBLE | |
1073 | if (content->reshape_active && | |
1074 | !(content->reshape_active & RESHAPE_NO_BACKUP) && | |
1075 | content->delta_disks <= 0) { | |
be7c26b4 N |
1076 | if (!c->backup_file) { |
1077 | pr_err("%s: Need a backup file to complete reshape of this array.\n", | |
1078 | mddev); | |
1079 | pr_err("Please provided one with \"--backup-file=...\"\n"); | |
1080 | if (c->update && | |
1081 | strcmp(c->update, "revert-reshape") == 0) | |
1082 | pr_err("(Don't specify --update=revert-reshape again, that part succeeded.)\n"); | |
1083 | return 1; | |
1084 | } | |
ddc1b11f N |
1085 | rv = sysfs_set_str(content, NULL, |
1086 | "array_state", "readonly"); | |
1087 | if (rv == 0) | |
1088 | rv = Grow_continue(mdfd, st, content, | |
06e293d0 | 1089 | c->backup_file, 0, |
ddc1b11f N |
1090 | c->freeze_reshape); |
1091 | } else if (c->readonly && | |
1092 | sysfs_attribute_available( | |
1093 | content, NULL, "array_state")) { | |
1094 | rv = sysfs_set_str(content, NULL, | |
1095 | "array_state", "readonly"); | |
1096 | } else | |
1097 | #endif | |
1098 | rv = ioctl(mdfd, RUN_ARRAY, NULL); | |
8832342d | 1099 | reopen_mddev(mdfd); /* drop O_EXCL */ |
ddc1b11f N |
1100 | if (rv == 0) { |
1101 | if (c->verbose >= 0) { | |
1102 | pr_err("%s has been started with %d drive%s", | |
1103 | mddev, okcnt, okcnt==1?"":"s"); | |
1104 | if (okcnt < (unsigned)content->array.raid_disks) | |
1105 | fprintf(stderr, " (out of %d)", content->array.raid_disks); | |
1106 | if (rebuilding_cnt) | |
1107 | fprintf(stderr, "%s %d rebuilding", sparecnt?",":" and", rebuilding_cnt); | |
1108 | if (sparecnt) | |
1109 | fprintf(stderr, " and %d spare%s", sparecnt, sparecnt==1?"":"s"); | |
1110 | fprintf(stderr, ".\n"); | |
1111 | } | |
1112 | if (content->reshape_active && | |
1113 | content->array.level >= 4 && | |
1114 | content->array.level <= 6) { | |
1115 | /* might need to increase the size | |
1116 | * of the stripe cache - default is 256 | |
1117 | */ | |
1118 | if (256 < 4 * (content->array.chunk_size/4096)) { | |
4dd2df09 | 1119 | struct mdinfo *sra = sysfs_read(mdfd, NULL, 0); |
ddc1b11f N |
1120 | if (sra) |
1121 | sysfs_set_num(sra, NULL, | |
1122 | "stripe_cache_size", | |
1123 | (4 * content->array.chunk_size / 4096) + 1); | |
1124 | sysfs_free(sra); | |
1125 | } | |
1126 | } | |
1127 | if (okcnt < (unsigned)content->array.raid_disks) { | |
1128 | /* If any devices did not get added | |
1129 | * because the kernel rejected them based | |
1130 | * on event count, try adding them | |
1131 | * again providing the action policy is | |
1132 | * 're-add' or greater. The bitmap | |
1133 | * might allow them to be included, or | |
1134 | * they will become spares. | |
1135 | */ | |
1136 | for (i = 0; i < bestcnt; i++) { | |
1137 | int j = best[i]; | |
1138 | if (j >= 0 && !devices[j].uptodate) { | |
1139 | if (!disk_action_allows(&devices[j].i, st->ss->name, act_re_add)) | |
1140 | continue; | |
1141 | rv = add_disk(mdfd, st, content, | |
1142 | &devices[j].i); | |
1143 | if (rv == 0 && c->verbose >= 0) | |
1144 | pr_err("%s has been re-added.\n", | |
1145 | devices[j].devname); | |
1146 | } | |
1147 | } | |
1148 | } | |
8cde842b N |
1149 | if (content->array.level == 6 && |
1150 | okcnt + 1 == (unsigned)content->array.raid_disks && | |
1151 | was_forced) { | |
1152 | struct mdinfo *sra = sysfs_read(mdfd, NULL, 0); | |
1153 | if (sra) | |
1154 | sysfs_set_str(sra, NULL, | |
1155 | "sync_action", "repair"); | |
1156 | sysfs_free(sra); | |
1157 | } | |
ddc1b11f N |
1158 | return 0; |
1159 | } | |
1160 | pr_err("failed to RUN_ARRAY %s: %s\n", | |
1161 | mddev, strerror(errno)); | |
1162 | ||
1163 | if (!enough(content->array.level, content->array.raid_disks, | |
1164 | content->array.layout, 1, avail)) | |
7a862a02 | 1165 | pr_err("Not enough devices to start the array.\n"); |
ddc1b11f N |
1166 | else if (!enough(content->array.level, |
1167 | content->array.raid_disks, | |
1168 | content->array.layout, clean, | |
1169 | avail)) | |
7a862a02 | 1170 | pr_err("Not enough devices to start the array while not clean - consider --force.\n"); |
ddc1b11f N |
1171 | |
1172 | return 1; | |
1173 | } | |
1174 | if (c->runstop == -1) { | |
1175 | pr_err("%s assembled from %d drive%s", | |
1176 | mddev, okcnt, okcnt==1?"":"s"); | |
1177 | if (okcnt != (unsigned)content->array.raid_disks) | |
1178 | fprintf(stderr, " (out of %d)", content->array.raid_disks); | |
1179 | fprintf(stderr, ", but not started.\n"); | |
1180 | return 2; | |
1181 | } | |
1182 | if (c->verbose >= -1) { | |
1183 | pr_err("%s assembled from %d drive%s", mddev, okcnt, okcnt==1?"":"s"); | |
1184 | if (rebuilding_cnt) | |
aacb2f81 | 1185 | fprintf(stderr, "%s %d rebuilding", sparecnt?",":" and", rebuilding_cnt); |
ddc1b11f N |
1186 | if (sparecnt) |
1187 | fprintf(stderr, " and %d spare%s", sparecnt, sparecnt==1?"":"s"); | |
1188 | if (!enough(content->array.level, content->array.raid_disks, | |
1189 | content->array.layout, 1, avail)) | |
1190 | fprintf(stderr, " - not enough to start the array.\n"); | |
1191 | else if (!enough(content->array.level, | |
1192 | content->array.raid_disks, | |
1193 | content->array.layout, clean, | |
1194 | avail)) | |
7a862a02 | 1195 | fprintf(stderr, " - not enough to start the array while not clean - consider --force.\n"); |
ddc1b11f N |
1196 | else { |
1197 | if (req_cnt == (unsigned)content->array.raid_disks) | |
1198 | fprintf(stderr, " - need all %d to start it", req_cnt); | |
1199 | else | |
aacb2f81 | 1200 | fprintf(stderr, " - need %d to start", req_cnt); |
ddc1b11f N |
1201 | fprintf(stderr, " (use --run to insist).\n"); |
1202 | } | |
1203 | } | |
1204 | return 1; | |
1205 | } | |
1206 | ||
95425a89 N |
1207 | int Assemble(struct supertype *st, char *mddev, |
1208 | struct mddev_ident *ident, | |
1209 | struct mddev_dev *devlist, | |
1210 | struct context *c) | |
1211 | { | |
1212 | /* | |
1213 | * The task of Assemble is to find a collection of | |
1214 | * devices that should (according to their superblocks) | |
1215 | * form an array, and to give this collection to the MD driver. | |
1216 | * In Linux-2.4 and later, this involves submitting a | |
1217 | * SET_ARRAY_INFO ioctl with no arg - to prepare | |
1218 | * the array - and then submit a number of | |
1219 | * ADD_NEW_DISK ioctls to add disks into | |
1220 | * the array. Finally RUN_ARRAY might | |
1221 | * be submitted to start the array. | |
1222 | * | |
1223 | * Much of the work of Assemble is in finding and/or | |
1224 | * checking the disks to make sure they look right. | |
1225 | * | |
1226 | * If mddev is not set, then scan must be set and we | |
1227 | * read through the config file for dev+uuid mapping | |
1228 | * We recurse, setting mddev, for each device that | |
1229 | * - isn't running | |
1230 | * - has a valid uuid (or any uuid if !uuidset) | |
1231 | * | |
1232 | * If mddev is set, we try to determine state of md. | |
1233 | * check version - must be at least 0.90.0 | |
1234 | * check kernel version. must be at least 2.4. | |
1235 | * If not, we can possibly fall back on START_ARRAY | |
1236 | * Try to GET_ARRAY_INFO. | |
1237 | * If possible, give up | |
1238 | * If not, try to STOP_ARRAY just to make sure | |
1239 | * | |
1240 | * If !uuidset and scan, look in conf-file for uuid | |
1241 | * If not found, give up | |
1242 | * If !devlist and scan and uuidset, get list of devs from conf-file | |
1243 | * | |
1244 | * For each device: | |
1245 | * Check superblock - discard if bad | |
1246 | * Check uuid (set if we don't have one) - discard if no match | |
1247 | * Check superblock similarity if we have a superblock - discard if different | |
1248 | * Record events, devicenum | |
1249 | * This should give us a list of devices for the array | |
1250 | * We should collect the most recent event number | |
1251 | * | |
1252 | * Count disks with recent enough event count | |
1253 | * While force && !enough disks | |
1254 | * Choose newest rejected disks, update event count | |
1255 | * mark clean and rewrite superblock | |
1256 | * If recent kernel: | |
1257 | * SET_ARRAY_INFO | |
1258 | * foreach device with recent events : ADD_NEW_DISK | |
1259 | * if runstop == 1 || "enough" disks and runstop==0 -> RUN_ARRAY | |
1260 | * If old kernel: | |
1261 | * Check the device numbers in superblock are right | |
1262 | * update superblock if any changes | |
1263 | * START_ARRAY | |
1264 | * | |
1265 | */ | |
6f4dbdc4 | 1266 | int rv; |
95425a89 N |
1267 | int mdfd; |
1268 | int clean; | |
1269 | int auto_assem = (mddev == NULL && !ident->uuid_set && | |
1270 | ident->super_minor == UnSet && ident->name[0] == 0 | |
1271 | && (ident->container == NULL || ident->member == NULL)); | |
2c355c22 | 1272 | struct devs *devices; |
95425a89 N |
1273 | char *devmap; |
1274 | int *best = NULL; /* indexed by raid_disk */ | |
1275 | int bestcnt = 0; | |
2c355c22 | 1276 | int devcnt; |
aacb2f81 | 1277 | unsigned int okcnt, sparecnt, rebuilding_cnt, replcnt; |
95425a89 | 1278 | int i; |
8cde842b | 1279 | int was_forced = 0; |
95425a89 N |
1280 | int most_recent = 0; |
1281 | int chosen_drive; | |
1282 | int change = 0; | |
1283 | int inargv = 0; | |
95425a89 N |
1284 | int start_partial_ok = (c->runstop >= 0) && |
1285 | (c->force || devlist==NULL || auto_assem); | |
1286 | int num_devs; | |
1287 | struct mddev_dev *tmpdev; | |
1288 | struct mdinfo info; | |
1289 | struct mdinfo *content = NULL; | |
1290 | struct mdinfo *pre_exist = NULL; | |
1291 | char *avail; | |
95425a89 N |
1292 | char *name = NULL; |
1293 | char chosen_name[1024]; | |
1294 | struct map_ent *map = NULL; | |
1295 | struct map_ent *mp; | |
1296 | ||
95425a89 N |
1297 | /* |
1298 | * If any subdevs are listed, then any that don't | |
1299 | * match ident are discarded. Remainder must all match and | |
1300 | * become the array. | |
1301 | * If no subdevs, then we scan all devices in the config file, but | |
1302 | * there must be something in the identity | |
1303 | */ | |
1304 | ||
1305 | if (!devlist && | |
1306 | ident->uuid_set == 0 && | |
1307 | (ident->super_minor < 0 || ident->super_minor == UnSet) && | |
1308 | ident->name[0] == 0 && | |
1309 | (ident->container == NULL || ident->member == NULL) && | |
1310 | ident->devices == NULL) { | |
1311 | pr_err("No identity information available for %s - cannot assemble.\n", | |
1312 | mddev ? mddev : "further assembly"); | |
1313 | return 1; | |
1314 | } | |
1315 | ||
1316 | if (devlist == NULL) | |
1317 | devlist = conf_get_devs(); | |
1318 | else if (mddev) | |
1319 | inargv = 1; | |
1320 | ||
79f9f56d | 1321 | try_again: |
95425a89 N |
1322 | /* We come back here when doing auto-assembly and attempting some |
1323 | * set of devices failed. Those are now marked as ->used==2 and | |
1324 | * we ignore them and try again | |
1325 | */ | |
1326 | if (!st && ident->st) | |
1327 | st = ident->st; | |
1328 | if (c->verbose>0) | |
1329 | pr_err("looking for devices for %s\n", | |
1330 | mddev ? mddev : "further assembly"); | |
1331 | ||
1332 | content = &info; | |
cb8f6859 N |
1333 | if (st) |
1334 | st->ignore_hw_compat = 1; | |
95425a89 N |
1335 | num_devs = select_devices(devlist, ident, &st, &content, c, |
1336 | inargv, auto_assem); | |
1337 | if (num_devs < 0) | |
1338 | return 1; | |
1011e834 | 1339 | |
98dbd966 | 1340 | if (!st || !st->sb || !content) |
c30e5369 N |
1341 | return 2; |
1342 | ||
0431869c N |
1343 | /* We have a full set of devices - we now need to find the |
1344 | * array device. | |
1345 | * However there is a risk that we are racing with "mdadm -I" | |
1346 | * and the array is already partially assembled - we will have | |
1347 | * rejected any devices already in this address. | |
1348 | * So we take a lock on the map file - to prevent further races - | |
1349 | * and look for the uuid in there. If found and the array is | |
1350 | * active, we abort. If found and the array is not active | |
1351 | * we commit to that md device and add all the contained devices | |
1352 | * to our list. We flag them so that we don't try to re-add, | |
1353 | * but can remove if they turn out to not be wanted. | |
1354 | */ | |
1355 | if (map_lock(&map)) | |
1356 | pr_err("failed to get exclusive lock on mapfile - continue anyway...\n"); | |
ec6db5ba N |
1357 | if (c->update && strcmp(c->update,"uuid") == 0) |
1358 | mp = NULL; | |
1359 | else | |
1360 | mp = map_by_uuid(&map, content->uuid); | |
0431869c N |
1361 | if (mp) { |
1362 | struct mdinfo *dv; | |
1363 | /* array already exists. */ | |
4dd2df09 | 1364 | pre_exist = sysfs_read(-1, mp->devnm, GET_LEVEL|GET_DEVS); |
0431869c N |
1365 | if (pre_exist->array.level != UnSet) { |
1366 | pr_err("Found some drive for an array that is already active: %s\n", | |
1367 | mp->path); | |
1368 | pr_err("giving up.\n"); | |
1369 | return 1; | |
1370 | } | |
1371 | for (dv = pre_exist->devs; dv; dv = dv->next) { | |
1372 | /* We want to add this device to our list, | |
1373 | * but it could already be there if "mdadm -I" | |
1374 | * started *after* we checked for O_EXCL. | |
1375 | * If we add it to the top of the list | |
1376 | * it will be preferred over later copies. | |
1377 | */ | |
1378 | struct mddev_dev *newdev; | |
1379 | char *devname = map_dev(dv->disk.major, | |
1380 | dv->disk.minor, | |
1381 | 0); | |
1382 | if (!devname) | |
1383 | continue; | |
1384 | newdev = xmalloc(sizeof(*newdev)); | |
1385 | newdev->devname = devname; | |
1386 | newdev->disposition = 'I'; | |
1387 | newdev->used = 1; | |
1388 | newdev->next = devlist; | |
1389 | devlist = newdev; | |
1390 | num_devs++; | |
1391 | } | |
1392 | strcpy(chosen_name, mp->path); | |
1393 | if (c->verbose > 0 || mddev == NULL || | |
1394 | strcmp(mddev, chosen_name) != 0) | |
1395 | pr_err("Merging with already-assembled %s\n", | |
1396 | chosen_name); | |
4dd2df09 | 1397 | mdfd = open_dev_excl(mp->devnm); |
0431869c N |
1398 | } else { |
1399 | int trustworthy = FOREIGN; | |
1400 | name = content->name; | |
1401 | switch (st->ss->match_home(st, c->homehost) | |
1402 | ?: st->ss->match_home(st, "any")) { | |
1403 | case 1: | |
1404 | trustworthy = LOCAL; | |
1405 | name = strchr(content->name, ':'); | |
1406 | if (name) | |
1407 | name++; | |
1408 | else | |
1409 | name = content->name; | |
1410 | break; | |
1411 | } | |
1412 | if (!auto_assem) | |
1413 | /* If the array is listed in mdadm.conf or on | |
1414 | * command line, then we trust the name | |
1415 | * even if the array doesn't look local | |
1416 | */ | |
1417 | trustworthy = LOCAL; | |
ac2ecf55 | 1418 | |
0431869c N |
1419 | if (name[0] == 0 && |
1420 | content->array.level == LEVEL_CONTAINER) { | |
1421 | name = content->text_version; | |
1422 | trustworthy = METADATA; | |
1423 | } | |
0ac91628 | 1424 | |
0431869c N |
1425 | if (name[0] && trustworthy != LOCAL && |
1426 | ! c->require_homehost && | |
1427 | conf_name_is_free(name)) | |
1428 | trustworthy = LOCAL; | |
0ac91628 | 1429 | |
0431869c N |
1430 | if (trustworthy == LOCAL && |
1431 | strchr(name, ':')) | |
1432 | /* Ignore 'host:' prefix of name */ | |
1433 | name = strchr(name, ':')+1; | |
7cdc0872 | 1434 | |
0431869c N |
1435 | mdfd = create_mddev(mddev, name, ident->autof, trustworthy, |
1436 | chosen_name); | |
1437 | } | |
c30e5369 N |
1438 | if (mdfd < 0) { |
1439 | st->ss->free_super(st); | |
c30e5369 | 1440 | if (auto_assem) |
60e1bc1a | 1441 | goto try_again; |
c30e5369 N |
1442 | return 1; |
1443 | } | |
a04d5763 | 1444 | mddev = chosen_name; |
6f4dbdc4 N |
1445 | if (get_linux_version() < 2004000 || |
1446 | md_get_version(mdfd) < 9000) { | |
1447 | pr_err("Assemble requires Linux 2.4 or later, and\n" | |
79f9f56d N |
1448 | " md driver version 0.90.0 or later.\n" |
1449 | " Upgrade your kernel or try --build\n"); | |
c30e5369 N |
1450 | close(mdfd); |
1451 | return 1; | |
1452 | } | |
0431869c | 1453 | if (pre_exist == NULL) { |
4dd2df09 | 1454 | if (mddev_busy(fd2devnm(mdfd))) { |
0431869c N |
1455 | pr_err("%s already active, cannot restart it!\n", |
1456 | mddev); | |
1457 | for (tmpdev = devlist ; | |
1458 | tmpdev && tmpdev->used != 1; | |
1459 | tmpdev = tmpdev->next) | |
1460 | ; | |
1461 | if (tmpdev && auto_assem) | |
1462 | pr_err("%s needed for %s...\n", | |
1463 | mddev, tmpdev->devname); | |
1464 | close(mdfd); | |
1465 | mdfd = -3; | |
1466 | st->ss->free_super(st); | |
1467 | if (auto_assem) | |
1468 | goto try_again; | |
1469 | return 1; | |
1470 | } | |
1471 | /* just incase it was started but has no content */ | |
1472 | ioctl(mdfd, STOP_ARRAY, NULL); | |
8a46fe84 NB |
1473 | } |
1474 | ||
9008ed1c N |
1475 | #ifndef MDASSEMBLE |
1476 | if (content != &info) { | |
1477 | /* This is a member of a container. Try starting the array. */ | |
588bebfc | 1478 | int err; |
11b6d91d | 1479 | err = assemble_container_content(st, mdfd, content, c, |
9ca39acb | 1480 | chosen_name, NULL); |
588bebfc | 1481 | close(mdfd); |
588bebfc | 1482 | return err; |
9008ed1c N |
1483 | } |
1484 | #endif | |
811e6cbe | 1485 | /* Ok, no bad inconsistancy, we can try updating etc */ |
503975b9 N |
1486 | devices = xcalloc(num_devs, sizeof(*devices)); |
1487 | devmap = xcalloc(num_devs, content->array.raid_disks); | |
df842e69 | 1488 | devcnt = load_devices(devices, devmap, ident, &st, devlist, |
2c355c22 N |
1489 | c, content, mdfd, mddev, |
1490 | &most_recent, &bestcnt, &best, inargv); | |
1491 | if (devcnt < 0) | |
1492 | return 1; | |
4b1ac34b | 1493 | |
64c4757e | 1494 | if (devcnt == 0) { |
e7b84f9d | 1495 | pr_err("no devices found for %s\n", |
79f9f56d | 1496 | mddev); |
3d2b16e7 NB |
1497 | if (st) |
1498 | st->ss->free_super(st); | |
7f91af49 | 1499 | close(mdfd); |
d7f7ebb7 | 1500 | free(devices); |
02e7c5b7 | 1501 | free(devmap); |
64c4757e NB |
1502 | return 1; |
1503 | } | |
4b1ac34b | 1504 | |
4977146a | 1505 | if (c->update && strcmp(c->update, "byteorder")==0) |
3d2b16e7 NB |
1506 | st->minor_version = 90; |
1507 | ||
a5d85af7 | 1508 | st->ss->getinfo_super(st, content, NULL); |
98dbd966 | 1509 | clean = content->array.state & 1; |
4b1ac34b | 1510 | |
64c4757e NB |
1511 | /* now we have some devices that might be suitable. |
1512 | * I wonder how many | |
1513 | */ | |
503975b9 | 1514 | avail = xcalloc(content->array.raid_disks, 1); |
64c4757e | 1515 | okcnt = 0; |
aacb2f81 | 1516 | replcnt = 0; |
52826846 | 1517 | sparecnt=0; |
1ff98339 | 1518 | rebuilding_cnt=0; |
f21e18ca | 1519 | for (i=0; i< bestcnt; i++) { |
64c4757e | 1520 | int j = best[i]; |
ee04451c NB |
1521 | int event_margin = 1; /* always allow a difference of '1' |
1522 | * like the kernel does | |
1523 | */ | |
64c4757e | 1524 | if (j < 0) continue; |
d013a55e NB |
1525 | /* note: we ignore error flags in multipath arrays |
1526 | * as they don't make sense | |
1527 | */ | |
df0d4ea0 | 1528 | if (content->array.level != LEVEL_MULTIPATH) |
f22385f9 | 1529 | if (!(devices[j].i.disk.state & (1<<MD_DISK_ACTIVE))) { |
213ee40b | 1530 | if (!(devices[j].i.disk.state |
ed7fc6b4 AC |
1531 | & (1<<MD_DISK_FAULTY))) { |
1532 | devices[j].uptodate = 1; | |
aa88f531 | 1533 | sparecnt++; |
ed7fc6b4 | 1534 | } |
d013a55e | 1535 | continue; |
aa88f531 | 1536 | } |
de5a472e | 1537 | /* If this device thinks that 'most_recent' has failed, then |
02e7c5b7 N |
1538 | * we must reject this device. |
1539 | */ | |
eb20ecf1 | 1540 | if (j != most_recent && !c->force && |
02e7c5b7 N |
1541 | content->array.raid_disks > 0 && |
1542 | devices[most_recent].i.disk.raid_disk >= 0 && | |
1543 | devmap[j * content->array.raid_disks + devices[most_recent].i.disk.raid_disk] == 0) { | |
4977146a | 1544 | if (c->verbose > -1) |
e7b84f9d | 1545 | pr_err("ignoring %s as it reports %s as failed\n", |
79f9f56d | 1546 | devices[j].devname, devices[most_recent].devname); |
02e7c5b7 N |
1547 | best[i] = -1; |
1548 | continue; | |
1549 | } | |
f80057ae N |
1550 | /* Require event counter to be same as, or just less than, |
1551 | * most recent. If it is bigger, it must be a stray spare and | |
1552 | * should be ignored. | |
1553 | */ | |
213ee40b | 1554 | if (devices[j].i.events+event_margin >= |
f80057ae N |
1555 | devices[most_recent].i.events && |
1556 | devices[j].i.events <= | |
1557 | devices[most_recent].i.events | |
1558 | ) { | |
64c4757e | 1559 | devices[j].uptodate = 1; |
aacb2f81 | 1560 | if (i < content->array.raid_disks * 2) { |
dcc4210f DW |
1561 | if (devices[j].i.recovery_start == MaxSector || |
1562 | (content->reshape_active && | |
d316dba7 | 1563 | i >= content->array.raid_disks - content->delta_disks)) { |
aacb2f81 N |
1564 | if (!avail[i/2]) { |
1565 | okcnt++; | |
1566 | avail[i/2]=1; | |
1567 | } else | |
1568 | replcnt++; | |
1ff98339 N |
1569 | } else |
1570 | rebuilding_cnt++; | |
265e0f17 | 1571 | } else |
52826846 | 1572 | sparecnt++; |
64c4757e NB |
1573 | } |
1574 | } | |
02e7c5b7 | 1575 | free(devmap); |
8cde842b N |
1576 | if (c->force) { |
1577 | int force_ok = force_array(content, devices, best, bestcnt, | |
1578 | avail, most_recent, st, c); | |
1579 | okcnt += force_ok; | |
1580 | if (force_ok) | |
1581 | was_forced = 1; | |
1582 | } | |
52826846 NB |
1583 | /* Now we want to look at the superblock which the kernel will base things on |
1584 | * and compare the devices that we think are working with the devices that the | |
1585 | * superblock thinks are working. | |
1586 | * If there are differences and --force is given, then update this chosen | |
1587 | * superblock. | |
1588 | */ | |
cd29a5c8 | 1589 | chosen_drive = -1; |
3da92f27 | 1590 | st->ss->free_super(st); |
f81a2b56 | 1591 | for (i=0; chosen_drive < 0 && i<bestcnt; i+=2) { |
52826846 NB |
1592 | int j = best[i]; |
1593 | int fd; | |
4b1ac34b | 1594 | |
52826846 NB |
1595 | if (j<0) |
1596 | continue; | |
1597 | if (!devices[j].uptodate) | |
1598 | continue; | |
a28232b8 N |
1599 | if (devices[j].i.events < devices[most_recent].i.events) |
1600 | continue; | |
52826846 | 1601 | chosen_drive = j; |
0431869c N |
1602 | if ((fd=dev_open(devices[j].devname, |
1603 | devices[j].included ? O_RDONLY | |
1604 | : (O_RDONLY|O_EXCL)))< 0) { | |
e7b84f9d | 1605 | pr_err("Cannot open %s: %s\n", |
79f9f56d | 1606 | devices[j].devname, strerror(errno)); |
7f91af49 | 1607 | close(mdfd); |
d7f7ebb7 | 1608 | free(devices); |
52826846 NB |
1609 | return 1; |
1610 | } | |
3da92f27 | 1611 | if (st->ss->load_super(st,fd, NULL)) { |
52826846 | 1612 | close(fd); |
e7b84f9d | 1613 | pr_err("RAID superblock has disappeared from %s\n", |
79f9f56d | 1614 | devices[j].devname); |
7f91af49 | 1615 | close(mdfd); |
d7f7ebb7 | 1616 | free(devices); |
52826846 NB |
1617 | return 1; |
1618 | } | |
1619 | close(fd); | |
1620 | } | |
3da92f27 | 1621 | if (st->sb == NULL) { |
e7b84f9d | 1622 | pr_err("No suitable drives found for %s\n", mddev); |
7f91af49 | 1623 | close(mdfd); |
d7f7ebb7 | 1624 | free(devices); |
4b1ac34b NB |
1625 | return 1; |
1626 | } | |
a5d85af7 | 1627 | st->ss->getinfo_super(st, content, NULL); |
f35f2525 | 1628 | #ifndef MDASSEMBLE |
4dd2df09 | 1629 | sysfs_init(content, mdfd, NULL); |
f35f2525 | 1630 | #endif |
56eedc1a | 1631 | for (i=0; i<bestcnt; i++) { |
52826846 | 1632 | int j = best[i]; |
98c6faba | 1633 | unsigned int desired_state; |
11a3e71d | 1634 | |
aacb2f81 | 1635 | if (i >= content->array.raid_disks * 2) |
11a3e71d | 1636 | desired_state = 0; |
aacb2f81 N |
1637 | else if (i & 1) |
1638 | desired_state = (1<<MD_DISK_ACTIVE) | (1<<MD_DISK_REPLACEMENT); | |
1639 | else | |
1640 | desired_state = (1<<MD_DISK_ACTIVE) | (1<<MD_DISK_SYNC); | |
11a3e71d | 1641 | |
52826846 NB |
1642 | if (j<0) |
1643 | continue; | |
1644 | if (!devices[j].uptodate) | |
1645 | continue; | |
4b1ac34b | 1646 | |
213ee40b | 1647 | devices[j].i.disk.state = desired_state; |
4e9a6ff7 N |
1648 | if (!(devices[j].i.array.state & 1)) |
1649 | clean = 0; | |
213ee40b NB |
1650 | |
1651 | if (st->ss->update_super(st, &devices[j].i, "assemble", NULL, | |
4977146a N |
1652 | c->verbose, 0, NULL)) { |
1653 | if (c->force) { | |
1654 | if (c->verbose >= 0) | |
e7b84f9d | 1655 | pr_err("clearing FAULTY flag for device %d in %s for %s\n", |
79f9f56d | 1656 | j, mddev, devices[j].devname); |
4b1ac34b | 1657 | change = 1; |
52826846 | 1658 | } else { |
4977146a | 1659 | if (c->verbose >= -1) |
e7b84f9d | 1660 | pr_err("device %d in %s has wrong state in superblock, but %s seems ok\n", |
79f9f56d | 1661 | i, mddev, devices[j].devname); |
52826846 NB |
1662 | } |
1663 | } | |
4b1ac34b | 1664 | #if 0 |
213ee40b | 1665 | if (!(super.disks[i].i.disk.state & (1 << MD_DISK_FAULTY))) { |
e7b84f9d | 1666 | pr_err("devices %d of %s is not marked FAULTY in superblock, but cannot be found\n", |
79f9f56d | 1667 | i, mddev); |
52826846 | 1668 | } |
4b1ac34b | 1669 | #endif |
52826846 | 1670 | } |
4977146a | 1671 | if (c->force && !clean && |
98dbd966 DW |
1672 | !enough(content->array.level, content->array.raid_disks, |
1673 | content->array.layout, clean, | |
de5a472e | 1674 | avail)) { |
98dbd966 | 1675 | change += st->ss->update_super(st, content, "force-array", |
6f4dbdc4 | 1676 | devices[chosen_drive].devname, c->verbose, |
67a8c82d | 1677 | 0, NULL); |
8cde842b | 1678 | was_forced = 1; |
583315d9 | 1679 | clean = 1; |
d013a55e | 1680 | } |
52826846 | 1681 | |
4b1ac34b | 1682 | if (change) { |
52826846 | 1683 | int fd; |
0431869c N |
1684 | fd = dev_open(devices[chosen_drive].devname, |
1685 | devices[chosen_drive].included ? | |
1686 | O_RDWR : (O_RDWR|O_EXCL)); | |
52826846 | 1687 | if (fd < 0) { |
e7b84f9d | 1688 | pr_err("Could not open %s for write - cannot Assemble array.\n", |
79f9f56d | 1689 | devices[chosen_drive].devname); |
7f91af49 | 1690 | close(mdfd); |
d7f7ebb7 | 1691 | free(devices); |
52826846 NB |
1692 | return 1; |
1693 | } | |
3da92f27 | 1694 | if (st->ss->store_super(st, fd)) { |
52826846 | 1695 | close(fd); |
e7b84f9d | 1696 | pr_err("Could not re-write superblock on %s\n", |
79f9f56d | 1697 | devices[chosen_drive].devname); |
7f91af49 | 1698 | close(mdfd); |
d7f7ebb7 | 1699 | free(devices); |
52826846 NB |
1700 | return 1; |
1701 | } | |
4977146a | 1702 | if (c->verbose >= 0) |
e7b84f9d | 1703 | pr_err("Marking array %s as 'clean'\n", |
79f9f56d | 1704 | mddev); |
52826846 | 1705 | close(fd); |
52826846 NB |
1706 | } |
1707 | ||
353632d9 NB |
1708 | /* If we are in the middle of a reshape we may need to restore saved data |
1709 | * that was moved aside due to the reshape overwriting live data | |
1710 | * The code of doing this lives in Grow.c | |
1711 | */ | |
39c5a909 | 1712 | #ifndef MDASSEMBLE |
5e88ab2e N |
1713 | if (content->reshape_active && |
1714 | !(content->reshape_active & RESHAPE_NO_BACKUP)) { | |
353632d9 | 1715 | int err = 0; |
503975b9 | 1716 | int *fdlist = xmalloc(sizeof(int)* bestcnt); |
4977146a | 1717 | if (c->verbose > 0) |
c34fef77 | 1718 | pr_err("%s has an active reshape - checking if critical section needs to be restored\n", |
79f9f56d | 1719 | chosen_name); |
54ded86f N |
1720 | if (!c->backup_file) |
1721 | c->backup_file = locate_backup(content->sys_name); | |
a7dec3fd | 1722 | enable_fds(bestcnt/2); |
5e9fd96f N |
1723 | for (i = 0; i < bestcnt/2; i++) { |
1724 | int j = best[i*2]; | |
353632d9 | 1725 | if (j >= 0) { |
0431869c N |
1726 | fdlist[i] = dev_open(devices[j].devname, |
1727 | devices[j].included | |
1728 | ? O_RDWR : (O_RDWR|O_EXCL)); | |
353632d9 | 1729 | if (fdlist[i] < 0) { |
e7b84f9d | 1730 | pr_err("Could not open %s for write - cannot Assemble array.\n", |
79f9f56d | 1731 | devices[j].devname); |
353632d9 NB |
1732 | err = 1; |
1733 | break; | |
1734 | } | |
1735 | } else | |
1736 | fdlist[i] = -1; | |
1737 | } | |
87f26d14 | 1738 | if (!err) { |
ba53ea59 AK |
1739 | if (st->ss->external && st->ss->recover_backup) |
1740 | err = st->ss->recover_backup(st, content); | |
1741 | else | |
5e9fd96f | 1742 | err = Grow_restart(st, content, fdlist, bestcnt/2, |
4977146a N |
1743 | c->backup_file, c->verbose > 0); |
1744 | if (err && c->invalid_backup) { | |
1745 | if (c->verbose > 0) | |
7a862a02 | 1746 | pr_err("continuing without restoring backup\n"); |
87f26d14 N |
1747 | err = 0; |
1748 | } | |
1749 | } | |
353632d9 NB |
1750 | while (i>0) { |
1751 | i--; | |
1752 | if (fdlist[i]>=0) close(fdlist[i]); | |
1753 | } | |
56dcaa6b | 1754 | free(fdlist); |
353632d9 | 1755 | if (err) { |
e7b84f9d | 1756 | pr_err("Failed to restore critical section for reshape, sorry.\n"); |
4977146a | 1757 | if (c->backup_file == NULL) |
e7b84f9d | 1758 | cont_err("Possibly you needed to specify the --backup-file\n"); |
7f91af49 | 1759 | close(mdfd); |
d7f7ebb7 | 1760 | free(devices); |
353632d9 NB |
1761 | return err; |
1762 | } | |
1763 | } | |
39c5a909 | 1764 | #endif |
aa88f531 | 1765 | |
64c4757e | 1766 | /* Almost ready to actually *do* something */ |
6f4dbdc4 N |
1767 | /* First, fill in the map, so that udev can find our name |
1768 | * as soon as we become active. | |
1769 | */ | |
afa368f4 N |
1770 | if (c->update && strcmp(c->update, "metadata")==0) { |
1771 | content->array.major_version = 1; | |
1772 | content->array.minor_version = 0; | |
1773 | strcpy(content->text_version, "1.0"); | |
1774 | } | |
1775 | ||
4dd2df09 | 1776 | map_update(&map, fd2devnm(mdfd), content->text_version, |
6f4dbdc4 N |
1777 | content->uuid, chosen_name); |
1778 | ||
1779 | rv = start_array(mdfd, mddev, content, | |
1780 | st, ident, best, bestcnt, | |
1781 | chosen_drive, devices, okcnt, sparecnt, | |
1782 | rebuilding_cnt, | |
1783 | c, | |
71417de6 N |
1784 | clean, avail, start_partial_ok, |
1785 | pre_exist != NULL, | |
1786 | was_forced); | |
6f4dbdc4 N |
1787 | if (rv == 1 && !pre_exist) |
1788 | ioctl(mdfd, STOP_ARRAY, NULL); | |
6f4dbdc4 N |
1789 | free(devices); |
1790 | map_unlock(&map); | |
1791 | if (rv == 0) { | |
1792 | wait_for(chosen_name, mdfd); | |
b20c8a50 | 1793 | close(mdfd); |
6f4dbdc4 N |
1794 | if (auto_assem) { |
1795 | int usecs = 1; | |
1796 | /* There is a nasty race with 'mdadm --monitor'. | |
1797 | * If it opens this device before we close it, | |
1798 | * it gets an incomplete open on which IO | |
1799 | * doesn't work and the capacity is | |
1800 | * wrong. | |
1801 | * If we reopen (to check for layered devices) | |
1802 | * before --monitor closes, we loose. | |
1803 | * | |
1804 | * So: wait upto 1 second for there to be | |
1805 | * a non-zero capacity. | |
1806 | */ | |
1807 | while (usecs < 1000) { | |
1808 | mdfd = open(mddev, O_RDONLY); | |
1809 | if (mdfd >= 0) { | |
1810 | unsigned long long size; | |
1811 | if (get_dev_size(mdfd, NULL, &size) && | |
1812 | size > 0) | |
1813 | break; | |
1814 | close(mdfd); | |
589395d6 | 1815 | } |
6f4dbdc4 N |
1816 | usleep(usecs); |
1817 | usecs <<= 1; | |
dab6685f | 1818 | } |
aa88f531 | 1819 | } |
b20c8a50 N |
1820 | } else |
1821 | close(mdfd); | |
1822 | ||
6f4dbdc4 N |
1823 | /* '2' means 'OK, but not started yet' */ |
1824 | return rv == 2 ? 0 : rv; | |
64c4757e | 1825 | } |
6234c63c DW |
1826 | |
1827 | #ifndef MDASSEMBLE | |
1828 | int assemble_container_content(struct supertype *st, int mdfd, | |
11b6d91d | 1829 | struct mdinfo *content, struct context *c, |
9ca39acb | 1830 | char *chosen_name, int *result) |
6234c63c | 1831 | { |
02b70e83 | 1832 | struct mdinfo *dev, *sra, *dev2; |
6234c63c | 1833 | int working = 0, preexist = 0; |
882029c8 | 1834 | int expansion = 0; |
6234c63c | 1835 | struct map_ent *map = NULL; |
7af03341 | 1836 | int old_raid_disks; |
4aecb54a | 1837 | int start_reshape; |
c1736844 N |
1838 | char *avail = NULL; |
1839 | int err; | |
6234c63c | 1840 | |
4dd2df09 | 1841 | sysfs_init(content, mdfd, NULL); |
6234c63c | 1842 | |
02b70e83 | 1843 | sra = sysfs_read(mdfd, NULL, GET_VERSION|GET_DEVS); |
0ea8f5b1 N |
1844 | if (sra == NULL || strcmp(sra->text_version, content->text_version) != 0) { |
1845 | if (content->array.major_version == -1 && | |
1846 | content->array.minor_version == -2 && | |
11b6d91d | 1847 | c->readonly && |
0ea8f5b1 N |
1848 | content->text_version[0] == '/') |
1849 | content->text_version[0] = '-'; | |
22472ee1 JS |
1850 | if (sysfs_set_array(content, md_get_version(mdfd)) != 0) { |
1851 | if (sra) | |
1852 | sysfs_free(sra); | |
6234c63c | 1853 | return 1; |
22472ee1 | 1854 | } |
0ea8f5b1 | 1855 | } |
588bebfc | 1856 | |
4aecb54a AK |
1857 | /* There are two types of reshape: container wide or sub-array specific |
1858 | * Check if metadata requests blocking container wide reshapes | |
1859 | */ | |
1860 | start_reshape = (content->reshape_active && | |
79f9f56d N |
1861 | !((content->reshape_active == CONTAINER_RESHAPE) && |
1862 | (content->array.state & (1<<MD_SB_BLOCK_CONTAINER_RESHAPE)))); | |
4aecb54a AK |
1863 | |
1864 | /* Block subarray here if it is under reshape now | |
1865 | * Do not allow for any changes in this array | |
1866 | */ | |
1867 | if (st->ss->external && content->recovery_blocked && start_reshape) | |
b8063f07 AK |
1868 | block_subarray(content); |
1869 | ||
02b70e83 N |
1870 | for (dev2 = sra->devs; dev2; dev2 = dev2->next) { |
1871 | for (dev = content->devs; dev; dev = dev->next) | |
1872 | if (dev2->disk.major == dev->disk.major && | |
1873 | dev2->disk.minor == dev->disk.minor) | |
1874 | break; | |
1875 | if (dev) | |
1876 | continue; | |
1877 | /* Don't want this one any more */ | |
1878 | if (sysfs_set_str(sra, dev2, "slot", "none") < 0 && | |
1879 | errno == EBUSY) { | |
1880 | pr_err("Cannot remove old device %s: not updating %s\n", dev2->sys_name, sra->sys_name); | |
1881 | sysfs_free(sra); | |
1882 | return 1; | |
1883 | } | |
1884 | sysfs_set_str(sra, dev2, "state", "remove"); | |
1885 | } | |
7af03341 | 1886 | old_raid_disks = content->array.raid_disks - content->delta_disks; |
c1736844 N |
1887 | avail = xcalloc(content->array.raid_disks, 1); |
1888 | for (dev = content->devs; dev; dev = dev->next) { | |
1889 | if (dev->disk.raid_disk >= 0) | |
1890 | avail[dev->disk.raid_disk] = 1; | |
14032016 | 1891 | if (sysfs_add_disk(content, dev, 1) == 0) { |
7af03341 | 1892 | if (dev->disk.raid_disk >= old_raid_disks && |
14032016 AK |
1893 | content->reshape_active) |
1894 | expansion++; | |
1895 | else | |
1896 | working++; | |
1897 | } else if (errno == EEXIST) | |
6234c63c | 1898 | preexist++; |
c1736844 | 1899 | } |
02b70e83 | 1900 | sysfs_free(sra); |
c1736844 N |
1901 | if (working + expansion == 0 && c->runstop <= 0) { |
1902 | free(avail); | |
7cb2aa33 | 1903 | return 1;/* Nothing new, don't try to start */ |
c1736844 | 1904 | } |
4dd2df09 | 1905 | map_update(&map, fd2devnm(mdfd), |
8b4e5ea9 N |
1906 | content->text_version, |
1907 | content->uuid, chosen_name); | |
1908 | ||
4aecb54a | 1909 | |
c1736844 N |
1910 | if (enough(content->array.level, content->array.raid_disks, |
1911 | content->array.layout, content->array.state & 1, avail) == 0) { | |
9ca39acb N |
1912 | if (c->export && result) |
1913 | *result |= INCR_NO; | |
1914 | else if (c->verbose >= 0) { | |
c1736844 N |
1915 | pr_err("%s assembled with %d device%s", |
1916 | chosen_name, preexist + working, | |
1917 | preexist + working == 1 ? "":"s"); | |
6234c63c DW |
1918 | if (preexist) |
1919 | fprintf(stderr, " (%d new)", working); | |
c1736844 | 1920 | fprintf(stderr, " but not started\n"); |
6234c63c | 1921 | } |
c1736844 N |
1922 | free(avail); |
1923 | return 1; | |
1924 | } | |
1925 | free(avail); | |
1926 | ||
1927 | if (c->runstop <= 0 && | |
1928 | (working + preexist + expansion) < | |
1929 | content->array.working_disks) { | |
9ca39acb N |
1930 | if (c->export && result) |
1931 | *result |= INCR_UNSAFE; | |
1932 | else if (c->verbose >= 0) { | |
e7b84f9d N |
1933 | pr_err("%s assembled with %d device%s", |
1934 | chosen_name, preexist + working, | |
1935 | preexist + working == 1 ? "":"s"); | |
88716263 N |
1936 | if (preexist) |
1937 | fprintf(stderr, " (%d new)", working); | |
c1736844 | 1938 | fprintf(stderr, " but not safe to start\n"); |
88716263 | 1939 | } |
7cb2aa33 N |
1940 | return 1; |
1941 | } | |
c1736844 N |
1942 | |
1943 | ||
1944 | if (start_reshape) { | |
1945 | int spare = content->array.raid_disks + expansion; | |
1946 | if (restore_backup(st, content, | |
1947 | working, | |
54ded86f | 1948 | spare, &c->backup_file, c->verbose) == 1) |
c1736844 N |
1949 | return 1; |
1950 | ||
1951 | err = sysfs_set_str(content, NULL, | |
1952 | "array_state", "readonly"); | |
1953 | if (err) | |
1954 | return 1; | |
1955 | ||
1956 | if (st->ss->external) { | |
1957 | if (!mdmon_running(st->container_devnm)) | |
1958 | start_mdmon(st->container_devnm); | |
1959 | ping_monitor(st->container_devnm); | |
1960 | if (mdmon_running(st->container_devnm) && | |
1961 | st->update_tail == NULL) | |
1962 | st->update_tail = &st->updates; | |
1963 | } | |
1964 | ||
1965 | err = Grow_continue(mdfd, st, content, c->backup_file, | |
06e293d0 | 1966 | 0, c->freeze_reshape); |
c1736844 N |
1967 | } else switch(content->array.level) { |
1968 | case LEVEL_LINEAR: | |
1969 | case LEVEL_MULTIPATH: | |
1970 | case 0: | |
1971 | err = sysfs_set_str(content, NULL, "array_state", | |
1972 | c->readonly ? "readonly" : "active"); | |
1973 | break; | |
1974 | default: | |
1975 | err = sysfs_set_str(content, NULL, "array_state", | |
1976 | "readonly"); | |
1977 | /* start mdmon if needed. */ | |
1978 | if (!err) { | |
1979 | if (!mdmon_running(st->container_devnm)) | |
1980 | start_mdmon(st->container_devnm); | |
1981 | ping_monitor(st->container_devnm); | |
1982 | } | |
1983 | break; | |
1984 | } | |
1985 | if (!err) | |
1986 | sysfs_set_safemode(content, content->safe_mode_delay); | |
1987 | ||
1988 | /* Block subarray here if it is not reshaped now | |
1989 | * It has be blocked a little later to allow mdmon to switch in | |
1990 | * in to R/W state | |
1991 | */ | |
1992 | if (st->ss->external && content->recovery_blocked && | |
1993 | !start_reshape) | |
1994 | block_subarray(content); | |
1995 | ||
9ca39acb N |
1996 | if (c->export && result) { |
1997 | if (err) | |
1998 | *result |= INCR_NO; | |
1999 | else | |
2000 | *result |= INCR_YES; | |
2001 | } else if (c->verbose >= 0) { | |
c1736844 N |
2002 | if (err) |
2003 | pr_err("array %s now has %d device%s", | |
2004 | chosen_name, working + preexist, | |
2005 | working + preexist == 1 ? "":"s"); | |
2006 | else | |
2007 | pr_err("Started %s with %d device%s", | |
2008 | chosen_name, working + preexist, | |
2009 | working + preexist == 1 ? "":"s"); | |
2010 | if (preexist) | |
2011 | fprintf(stderr, " (%d new)", working); | |
2012 | if (expansion) | |
2013 | fprintf(stderr, " ( + %d for expansion)", | |
2014 | expansion); | |
2015 | fprintf(stderr, "\n"); | |
2016 | } | |
2017 | if (!err) | |
2018 | wait_for(chosen_name, mdfd); | |
2019 | return err; | |
2020 | /* FIXME should have an O_EXCL and wait for read-auto */ | |
6234c63c DW |
2021 | } |
2022 | #endif |