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