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