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