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