]>
Commit | Line | Data |
---|---|---|
64c4757e | 1 | /* |
9a9dab36 | 2 | * mdadm - manage Linux "md" devices aka RAID arrays. |
64c4757e | 3 | * |
4f589ad0 | 4 | * Copyright (C) 2001-2006 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 | |
22 | * Email: <neilb@cse.unsw.edu.au> | |
23 | * Paper: Neil Brown | |
24 | * School of Computer Science and Engineering | |
25 | * The University of New South Wales | |
26 | * Sydney, 2052 | |
27 | * Australia | |
28 | */ | |
29 | ||
9a9dab36 | 30 | #include "mdadm.h" |
41a3b72a | 31 | #include <ctype.h> |
64c4757e | 32 | |
624920bb NB |
33 | static int name_matches(char *found, char *required, char *homehost) |
34 | { | |
35 | /* See if the name found matches the required name, possibly | |
36 | * prefixed with 'homehost' | |
37 | */ | |
38 | char fnd[33]; | |
39 | ||
40 | strncpy(fnd, found, 32); | |
41 | fnd[32] = 0; | |
42 | if (strcmp(found, required)==0) | |
43 | return 1; | |
44 | if (homehost) { | |
45 | int l = strlen(homehost); | |
46 | if (l < 32 && fnd[l] == ':' && | |
47 | strcmp(fnd+l+1, required)==0) | |
48 | return 1; | |
49 | } | |
50 | return 0; | |
51 | } | |
52 | ||
2de8884f DW |
53 | /*static */ int is_member_busy(char *metadata_version) |
54 | { | |
55 | /* check if the given member array is active */ | |
56 | struct mdstat_ent *mdstat = mdstat_read(1, 0); | |
57 | struct mdstat_ent *ent; | |
58 | int busy = 0; | |
59 | ||
60 | for (ent = mdstat; ent; ent = ent->next) { | |
61 | if (ent->metadata_version == NULL) | |
62 | continue; | |
63 | if (strncmp(ent->metadata_version, "external:", 9) != 0) | |
64 | continue; | |
65 | if (!is_subarray(&ent->metadata_version[9])) | |
66 | continue; | |
67 | /* Skip first char - it can be '/' or '-' */ | |
68 | if (strcmp(&ent->metadata_version[10], metadata_version+1) == 0) { | |
69 | busy = 1; | |
70 | break; | |
71 | } | |
72 | } | |
73 | free_mdstat(mdstat); | |
74 | ||
75 | return busy; | |
76 | } | |
77 | ||
7f91af49 | 78 | int Assemble(struct supertype *st, char *mddev, |
8aec876d | 79 | mddev_ident_t ident, |
06b0d786 | 80 | mddev_dev_t devlist, char *backup_file, |
64c4757e | 81 | int readonly, int runstop, |
e5eac01f | 82 | char *update, char *homehost, |
64c4757e NB |
83 | int verbose, int force) |
84 | { | |
85 | /* | |
52826846 NB |
86 | * The task of Assemble is to find a collection of |
87 | * devices that should (according to their superblocks) | |
88 | * form an array, and to give this collection to the MD driver. | |
89 | * In Linux-2.4 and later, this involves submitting a | |
64c4757e NB |
90 | * SET_ARRAY_INFO ioctl with no arg - to prepare |
91 | * the array - and then submit a number of | |
92 | * ADD_NEW_DISK ioctls to add disks into | |
93 | * the array. Finally RUN_ARRAY might | |
94 | * be submitted to start the array. | |
95 | * | |
96 | * Much of the work of Assemble is in finding and/or | |
97 | * checking the disks to make sure they look right. | |
98 | * | |
4b1ac34b | 99 | * If mddev is not set, then scan must be set and we |
64c4757e NB |
100 | * read through the config file for dev+uuid mapping |
101 | * We recurse, setting mddev, for each device that | |
102 | * - isn't running | |
4b1ac34b | 103 | * - has a valid uuid (or any uuid if !uuidset) |
64c4757e NB |
104 | * |
105 | * If mddev is set, we try to determine state of md. | |
106 | * check version - must be at least 0.90.0 | |
107 | * check kernel version. must be at least 2.4. | |
108 | * If not, we can possibly fall back on START_ARRAY | |
109 | * Try to GET_ARRAY_INFO. | |
110 | * If possible, give up | |
111 | * If not, try to STOP_ARRAY just to make sure | |
112 | * | |
113 | * If !uuidset and scan, look in conf-file for uuid | |
114 | * If not found, give up | |
b8a8ccf9 | 115 | * If !devlist and scan and uuidset, get list of devs from conf-file |
64c4757e NB |
116 | * |
117 | * For each device: | |
118 | * Check superblock - discard if bad | |
119 | * Check uuid (set if we don't have one) - discard if no match | |
5787fa49 | 120 | * Check superblock similarity if we have a superblock - discard if different |
4b1ac34b | 121 | * Record events, devicenum |
64c4757e | 122 | * This should give us a list of devices for the array |
4b1ac34b | 123 | * We should collect the most recent event number |
64c4757e NB |
124 | * |
125 | * Count disks with recent enough event count | |
126 | * While force && !enough disks | |
127 | * Choose newest rejected disks, update event count | |
128 | * mark clean and rewrite superblock | |
129 | * If recent kernel: | |
130 | * SET_ARRAY_INFO | |
131 | * foreach device with recent events : ADD_NEW_DISK | |
132 | * if runstop == 1 || "enough" disks and runstop==0 -> RUN_ARRAY | |
133 | * If old kernel: | |
134 | * Check the device numbers in superblock are right | |
135 | * update superblock if any changes | |
136 | * START_ARRAY | |
137 | * | |
138 | */ | |
c30e5369 N |
139 | int mdfd; |
140 | int clean; | |
7f91af49 | 141 | int auto_assem = (mddev == NULL); |
64c4757e | 142 | int old_linux = 0; |
c30e5369 | 143 | int vers = vers; /* Keep gcc quite - it really is initialised */ |
64c4757e NB |
144 | struct { |
145 | char *devname; | |
213ee40b NB |
146 | int uptodate; /* set once we decide that this device is as |
147 | * recent as everything else in the array. | |
148 | */ | |
149 | struct mdinfo i; | |
5787fa49 | 150 | } *devices; |
56eedc1a | 151 | int *best = NULL; /* indexed by raid_disk */ |
98c6faba NB |
152 | unsigned int bestcnt = 0; |
153 | int devcnt = 0; | |
154 | unsigned int okcnt, sparecnt; | |
155 | unsigned int req_cnt; | |
156 | unsigned int i; | |
64c4757e | 157 | int most_recent = 0; |
cd29a5c8 | 158 | int chosen_drive; |
52826846 | 159 | int change = 0; |
cd29a5c8 | 160 | int inargv = 0; |
52437b4f | 161 | int report_missmatch; |
bf4fb153 | 162 | int bitmap_done; |
7f91af49 N |
163 | int start_partial_ok = (runstop >= 0) && |
164 | (force || devlist==NULL || auto_assem); | |
98c6faba | 165 | unsigned int num_devs; |
5787fa49 | 166 | mddev_dev_t tmpdev; |
4b1ac34b | 167 | struct mdinfo info; |
98dbd966 | 168 | struct mdinfo *content = NULL; |
c30e5369 | 169 | mdu_array_info_t tmp_inf; |
265e0f17 | 170 | char *avail; |
308e1801 | 171 | int nextspare = 0; |
197e3eb6 | 172 | char *name = NULL; |
69207ff6 | 173 | int trustworthy; |
a04d5763 | 174 | char chosen_name[1024]; |
8a46fe84 | 175 | |
682c7051 | 176 | if (get_linux_version() < 2004000) |
64c4757e NB |
177 | old_linux = 1; |
178 | ||
64c4757e | 179 | /* |
52826846 NB |
180 | * If any subdevs are listed, then any that don't |
181 | * match ident are discarded. Remainder must all match and | |
182 | * become the array. | |
183 | * If no subdevs, then we scan all devices in the config file, but | |
184 | * there must be something in the identity | |
64c4757e | 185 | */ |
64c4757e | 186 | |
cd29a5c8 | 187 | if (!devlist && |
52826846 NB |
188 | ident->uuid_set == 0 && |
189 | ident->super_minor < 0 && | |
190 | ident->devices == NULL) { | |
191 | fprintf(stderr, Name ": No identity information available for %s - cannot assemble.\n", | |
8a46fe84 | 192 | mddev ? mddev : "further assembly"); |
52826846 | 193 | return 1; |
64c4757e | 194 | } |
71d60c48 | 195 | |
cd29a5c8 | 196 | if (devlist == NULL) |
8aec876d | 197 | devlist = conf_get_devs(); |
7f91af49 | 198 | else if (mddev) |
da6b5ca9 | 199 | inargv = 1; |
64c4757e | 200 | |
52437b4f | 201 | report_missmatch = ((inargv && verbose >= 0) || verbose > 0); |
60e1bc1a | 202 | try_again: |
c30e5369 N |
203 | /* We come back here when doing auto-assembly and attempting some |
204 | * set of devices failed. Those are now marked as ->used==2 and | |
205 | * we ignore them and try again | |
206 | */ | |
60e1bc1a | 207 | |
5787fa49 NB |
208 | tmpdev = devlist; num_devs = 0; |
209 | while (tmpdev) { | |
da6b5ca9 NB |
210 | if (tmpdev->used) |
211 | tmpdev->used = 2; | |
212 | else | |
213 | num_devs++; | |
5787fa49 NB |
214 | tmpdev = tmpdev->next; |
215 | } | |
5787fa49 NB |
216 | devices = malloc(num_devs * sizeof(*devices)); |
217 | ||
82d9eba6 | 218 | if (!st && ident->st) st = ident->st; |
64c4757e | 219 | |
dab6685f | 220 | if (verbose>0) |
82b27616 | 221 | fprintf(stderr, Name ": looking for devices for %s\n", |
8a46fe84 | 222 | mddev ? mddev : "further assembly"); |
82b27616 | 223 | |
811e6cbe NB |
224 | /* first walk the list of devices to find a consistent set |
225 | * that match the criterea, if that is possible. | |
c30e5369 | 226 | * We flag the ones we like with 'used'. |
811e6cbe NB |
227 | */ |
228 | for (tmpdev = devlist; | |
229 | tmpdev; | |
230 | tmpdev = tmpdev->next) { | |
231 | char *devname = tmpdev->devname; | |
64c4757e NB |
232 | int dfd; |
233 | struct stat stb; | |
3da92f27 | 234 | struct supertype *tst = dup_super(st); |
52826846 | 235 | |
da6b5ca9 NB |
236 | if (tmpdev->used > 1) continue; |
237 | ||
52826846 | 238 | if (ident->devices && |
56eedc1a | 239 | !match_oneof(ident->devices, devname)) { |
52437b4f | 240 | if (report_missmatch) |
56eedc1a | 241 | fprintf(stderr, Name ": %s is not one of %s\n", devname, ident->devices); |
52826846 | 242 | continue; |
56eedc1a | 243 | } |
4b1ac34b | 244 | |
8b0dabea | 245 | dfd = dev_open(devname, O_RDONLY|O_EXCL); |
64c4757e | 246 | if (dfd < 0) { |
52437b4f | 247 | if (report_missmatch) |
682c7051 | 248 | fprintf(stderr, Name ": cannot open device %s: %s\n", |
64c4757e | 249 | devname, strerror(errno)); |
da6b5ca9 | 250 | tmpdev->used = 2; |
52826846 NB |
251 | } else if (fstat(dfd, &stb)< 0) { |
252 | /* Impossible! */ | |
253 | fprintf(stderr, Name ": fstat failed for %s: %s\n", | |
254 | devname, strerror(errno)); | |
da6b5ca9 | 255 | tmpdev->used = 2; |
cd29a5c8 NB |
256 | } else if ((stb.st_mode & S_IFMT) != S_IFBLK) { |
257 | fprintf(stderr, Name ": %s is not a block device.\n", | |
52826846 | 258 | devname); |
da6b5ca9 | 259 | tmpdev->used = 2; |
82d9eba6 | 260 | } else if (!tst && (tst = guess_super(dfd)) == NULL) { |
52437b4f | 261 | if (report_missmatch) |
da6b5ca9 NB |
262 | fprintf(stderr, Name ": no recogniseable superblock on %s\n", |
263 | devname); | |
264 | tmpdev->used = 2; | |
3da92f27 | 265 | } else if (tst->ss->load_super(tst,dfd, NULL)) { |
52437b4f | 266 | if (report_missmatch) |
682c7051 | 267 | fprintf( stderr, Name ": no RAID superblock on %s\n", |
64c4757e | 268 | devname); |
52826846 | 269 | } else { |
98dbd966 DW |
270 | content = &info; |
271 | memset(content, 0, sizeof(*content)); | |
272 | tst->ss->getinfo_super(tst, content); | |
64c4757e | 273 | } |
c06487ce | 274 | if (dfd >= 0) close(dfd); |
52826846 | 275 | |
838acbc2 | 276 | if (ident->uuid_set && (!update || strcmp(update, "uuid")!= 0) && |
3da92f27 | 277 | (!tst || !tst->sb || |
98dbd966 | 278 | same_uuid(content->uuid, ident->uuid, tst->ss->swapuuid)==0)) { |
52437b4f | 279 | if (report_missmatch) |
52826846 NB |
280 | fprintf(stderr, Name ": %s has wrong uuid.\n", |
281 | devname); | |
df37ffc0 | 282 | goto loop; |
82b27616 | 283 | } |
c4f12c13 | 284 | if (ident->name[0] && (!update || strcmp(update, "name")!= 0) && |
3da92f27 | 285 | (!tst || !tst->sb || |
98dbd966 | 286 | name_matches(content->name, ident->name, homehost)==0)) { |
52437b4f | 287 | if (report_missmatch) |
947fd4dd NB |
288 | fprintf(stderr, Name ": %s has wrong name.\n", |
289 | devname); | |
df37ffc0 | 290 | goto loop; |
947fd4dd | 291 | } |
98c6faba | 292 | if (ident->super_minor != UnSet && |
3da92f27 | 293 | (!tst || !tst->sb || |
98dbd966 | 294 | ident->super_minor != content->array.md_minor)) { |
52437b4f | 295 | if (report_missmatch) |
52826846 | 296 | fprintf(stderr, Name ": %s has wrong super-minor.\n", |
64c4757e | 297 | devname); |
df37ffc0 | 298 | goto loop; |
64c4757e | 299 | } |
98c6faba | 300 | if (ident->level != UnSet && |
3da92f27 | 301 | (!tst || !tst->sb || |
98dbd966 | 302 | ident->level != content->array.level)) { |
52437b4f | 303 | if (report_missmatch) |
cd29a5c8 NB |
304 | fprintf(stderr, Name ": %s has wrong raid level.\n", |
305 | devname); | |
df37ffc0 | 306 | goto loop; |
cd29a5c8 | 307 | } |
98c6faba | 308 | if (ident->raid_disks != UnSet && |
3da92f27 | 309 | (!tst || !tst->sb || |
98dbd966 | 310 | ident->raid_disks!= content->array.raid_disks)) { |
52437b4f | 311 | if (report_missmatch) |
cd29a5c8 NB |
312 | fprintf(stderr, Name ": %s requires wrong number of drives.\n", |
313 | devname); | |
df37ffc0 | 314 | goto loop; |
cd29a5c8 | 315 | } |
c30e5369 | 316 | if (auto_assem) { |
3da92f27 | 317 | if (tst == NULL || tst->sb == NULL) |
da6b5ca9 | 318 | continue; |
da6b5ca9 | 319 | } |
83b6208e | 320 | /* If we are this far, then we are nearly commited to this device. |
52826846 | 321 | * If the super_block doesn't exist, or doesn't match others, |
83b6208e NB |
322 | * then we probably cannot continue |
323 | * However if one of the arrays is for the homehost, and | |
324 | * the other isn't that can disambiguate. | |
52826846 | 325 | */ |
52826846 | 326 | |
3da92f27 | 327 | if (!tst || !tst->sb) { |
52826846 NB |
328 | fprintf(stderr, Name ": %s has no superblock - assembly aborted\n", |
329 | devname); | |
ddd1a492 NB |
330 | if (st) |
331 | st->ss->free_super(st); | |
52826846 NB |
332 | return 1; |
333 | } | |
838acbc2 | 334 | |
83b6208e | 335 | if (st == NULL) |
3da92f27 NB |
336 | st = dup_super(tst); |
337 | if (st->minor_version == -1) | |
338 | st->minor_version = tst->minor_version; | |
83b6208e NB |
339 | if (st->ss != tst->ss || |
340 | st->minor_version != tst->minor_version || | |
64557c33 | 341 | st->ss->compare_super(st, tst) != 0) { |
83b6208e | 342 | /* Some mismatch. If exactly one array matches this host, |
da6b5ca9 NB |
343 | * we can resolve on that one. |
344 | * Or, if we are auto assembling, we just ignore the second | |
345 | * for now. | |
83b6208e | 346 | */ |
c30e5369 | 347 | if (auto_assem) |
df37ffc0 | 348 | goto loop; |
83b6208e | 349 | if (homehost) { |
3da92f27 NB |
350 | int first = st->ss->match_home(st, homehost); |
351 | int last = tst->ss->match_home(tst, homehost); | |
9362c1c8 N |
352 | if (first != last && |
353 | (first == 1 || last == 1)) { | |
83b6208e NB |
354 | /* We can do something */ |
355 | if (first) {/* just ignore this one */ | |
52437b4f | 356 | if (report_missmatch) |
83b6208e NB |
357 | fprintf(stderr, Name ": %s misses out due to wrong homehost\n", |
358 | devname); | |
df37ffc0 | 359 | goto loop; |
83b6208e NB |
360 | } else { /* reject all those sofar */ |
361 | mddev_dev_t td; | |
52437b4f | 362 | if (report_missmatch) |
83b6208e NB |
363 | fprintf(stderr, Name ": %s overrides previous devices due to good homehost\n", |
364 | devname); | |
365 | for (td=devlist; td != tmpdev; td=td->next) | |
366 | if (td->used == 1) | |
367 | td->used = 0; | |
368 | tmpdev->used = 1; | |
df37ffc0 | 369 | goto loop; |
83b6208e NB |
370 | } |
371 | } | |
372 | } | |
52826846 NB |
373 | fprintf(stderr, Name ": superblock on %s doesn't match others - assembly aborted\n", |
374 | devname); | |
3da92f27 NB |
375 | tst->ss->free_super(tst); |
376 | st->ss->free_super(st); | |
52826846 | 377 | return 1; |
64c4757e NB |
378 | } |
379 | ||
811e6cbe | 380 | tmpdev->used = 1; |
df37ffc0 NB |
381 | |
382 | loop: | |
3d2b16e7 NB |
383 | if (tst) |
384 | tst->ss->free_super(tst); | |
811e6cbe NB |
385 | } |
386 | ||
98dbd966 | 387 | if (!st || !st->sb || !content) |
c30e5369 N |
388 | return 2; |
389 | ||
69207ff6 | 390 | /* Now need to open array the device. Use create_mddev */ |
98dbd966 DW |
391 | if (content == &info) |
392 | st->ss->getinfo_super(st, content); | |
3d2c4fc7 | 393 | |
69207ff6 N |
394 | trustworthy = FOREIGN; |
395 | switch (st->ss->match_home(st, homehost)) { | |
396 | case 0: | |
397 | trustworthy = FOREIGN; | |
98dbd966 | 398 | name = content->name; |
69207ff6 N |
399 | break; |
400 | case 1: | |
401 | trustworthy = LOCAL; | |
98dbd966 | 402 | name = strchr(content->name, ':'); |
69207ff6 N |
403 | if (name) |
404 | name++; | |
3d2c4fc7 | 405 | else |
98dbd966 | 406 | name = content->name; |
69207ff6 N |
407 | break; |
408 | case -1: | |
a4bc1720 | 409 | trustworthy = FOREIGN; |
69207ff6 | 410 | break; |
c30e5369 | 411 | } |
ac2ecf55 N |
412 | if (!auto_assem && trustworthy == FOREIGN) |
413 | /* If the array is listed in mdadm or on | |
414 | * command line, then we trust the name | |
415 | * even if the array doesn't look local | |
416 | */ | |
417 | trustworthy = LOCAL; | |
418 | ||
98dbd966 DW |
419 | if (content->name[0] == 0 && |
420 | content->array.level == LEVEL_CONTAINER) { | |
421 | name = content->text_version; | |
a4bc1720 N |
422 | trustworthy = METADATA; |
423 | } | |
a04d5763 N |
424 | mdfd = create_mddev(mddev, name, ident->autof, trustworthy, |
425 | chosen_name); | |
c30e5369 N |
426 | if (mdfd < 0) { |
427 | st->ss->free_super(st); | |
428 | free(devices); | |
429 | if (auto_assem) | |
60e1bc1a | 430 | goto try_again; |
c30e5369 N |
431 | return 1; |
432 | } | |
a04d5763 | 433 | mddev = chosen_name; |
c30e5369 N |
434 | vers = md_get_version(mdfd); |
435 | if (vers < 9000) { | |
436 | fprintf(stderr, Name ": Assemble requires driver version 0.90.0 or later.\n" | |
437 | " Upgrade your kernel or try --build\n"); | |
438 | close(mdfd); | |
439 | return 1; | |
440 | } | |
441 | if (ioctl(mdfd, GET_ARRAY_INFO, &tmp_inf)==0) { | |
442 | fprintf(stderr, Name ": %s already active, cannot restart it!\n", | |
443 | mddev); | |
444 | for (tmpdev = devlist ; | |
445 | tmpdev && tmpdev->used != 1; | |
446 | tmpdev = tmpdev->next) | |
447 | ; | |
448 | if (tmpdev && auto_assem) | |
449 | fprintf(stderr, Name ": %s needed for %s...\n", | |
450 | mddev, tmpdev->devname); | |
451 | close(mdfd); | |
452 | mdfd = -3; | |
453 | st->ss->free_super(st); | |
454 | free(devices); | |
455 | if (auto_assem) | |
456 | goto try_again; | |
457 | return 1; | |
8a46fe84 | 458 | } |
c30e5369 | 459 | ioctl(mdfd, STOP_ARRAY, NULL); /* just incase it was started but has no content */ |
8a46fe84 | 460 | |
811e6cbe | 461 | /* Ok, no bad inconsistancy, we can try updating etc */ |
bf4fb153 | 462 | bitmap_done = 0; |
da6b5ca9 | 463 | for (tmpdev = devlist; tmpdev; tmpdev=tmpdev->next) if (tmpdev->used == 1) { |
811e6cbe NB |
464 | char *devname = tmpdev->devname; |
465 | struct stat stb; | |
5787fa49 | 466 | /* looks like a good enough match to update the super block if needed */ |
d1f1011b | 467 | #ifndef MDASSEMBLE |
5787fa49 | 468 | if (update) { |
811e6cbe | 469 | int dfd; |
4b1ac34b NB |
470 | /* prepare useful information in info structures */ |
471 | struct stat stb2; | |
3da92f27 | 472 | struct supertype *tst; |
4b1ac34b | 473 | fstat(mdfd, &stb2); |
7d99579f NB |
474 | |
475 | if (strcmp(update, "uuid")==0 && | |
476 | !ident->uuid_set) { | |
477 | int rfd; | |
478 | if ((rfd = open("/dev/urandom", O_RDONLY)) < 0 || | |
479 | read(rfd, ident->uuid, 16) != 16) { | |
480 | *(__u32*)(ident->uuid) = random(); | |
481 | *(__u32*)(ident->uuid+1) = random(); | |
482 | *(__u32*)(ident->uuid+2) = random(); | |
483 | *(__u32*)(ident->uuid+3) = random(); | |
484 | } | |
485 | if (rfd >= 0) close(rfd); | |
7d99579f | 486 | } |
811e6cbe NB |
487 | dfd = dev_open(devname, O_RDWR|O_EXCL); |
488 | ||
0430ed48 NB |
489 | remove_partitions(dfd); |
490 | ||
3da92f27 NB |
491 | tst = dup_super(st); |
492 | tst->ss->load_super(tst, dfd, NULL); | |
98dbd966 | 493 | tst->ss->getinfo_super(tst, content); |
811e6cbe | 494 | |
98dbd966 DW |
495 | memcpy(content->uuid, ident->uuid, 16); |
496 | strcpy(content->name, ident->name); | |
497 | content->array.md_minor = minor(stb2.st_rdev); | |
811e6cbe | 498 | |
98dbd966 | 499 | tst->ss->update_super(tst, content, update, |
3da92f27 NB |
500 | devname, verbose, |
501 | ident->uuid_set, homehost); | |
e5eac01f NB |
502 | if (strcmp(update, "uuid")==0 && |
503 | !ident->uuid_set) { | |
504 | ident->uuid_set = 1; | |
98dbd966 | 505 | memcpy(ident->uuid, content->uuid, 16); |
e5eac01f | 506 | } |
b8a8ccf9 | 507 | if (dfd < 0) |
5787fa49 NB |
508 | fprintf(stderr, Name ": Cannot open %s for superblock update\n", |
509 | devname); | |
3da92f27 | 510 | else if (tst->ss->store_super(tst, dfd)) |
5787fa49 NB |
511 | fprintf(stderr, Name ": Could not re-write superblock on %s.\n", |
512 | devname); | |
513 | if (dfd >= 0) | |
514 | close(dfd); | |
8131b493 NB |
515 | |
516 | if (strcmp(update, "uuid")==0 && | |
bf4fb153 | 517 | ident->bitmap_fd >= 0 && !bitmap_done) { |
3da92f27 | 518 | if (bitmap_update_uuid(ident->bitmap_fd, |
98dbd966 | 519 | content->uuid, |
3da92f27 | 520 | tst->ss->swapuuid) != 0) |
bf4fb153 NB |
521 | fprintf(stderr, Name ": Could not update uuid on external bitmap.\n"); |
522 | else | |
523 | bitmap_done = 1; | |
524 | } | |
3da92f27 | 525 | tst->ss->free_super(tst); |
d1f1011b NB |
526 | } else |
527 | #endif | |
528 | { | |
30e1b9a5 | 529 | struct supertype *tst = dup_super(st); |
da6b5ca9 NB |
530 | int dfd; |
531 | dfd = dev_open(devname, O_RDWR|O_EXCL); | |
532 | ||
0430ed48 NB |
533 | remove_partitions(dfd); |
534 | ||
3da92f27 | 535 | tst->ss->load_super(tst, dfd, NULL); |
98dbd966 | 536 | tst->ss->getinfo_super(tst, content); |
3da92f27 | 537 | tst->ss->free_super(tst); |
da6b5ca9 | 538 | close(dfd); |
5787fa49 NB |
539 | } |
540 | ||
811e6cbe NB |
541 | stat(devname, &stb); |
542 | ||
dab6685f | 543 | if (verbose > 0) |
cd29a5c8 | 544 | fprintf(stderr, Name ": %s is identified as a member of %s, slot %d.\n", |
98dbd966 | 545 | devname, mddev, content->disk.raid_disk); |
64c4757e | 546 | devices[devcnt].devname = devname; |
64c4757e | 547 | devices[devcnt].uptodate = 0; |
98dbd966 | 548 | devices[devcnt].i = *content; |
213ee40b NB |
549 | devices[devcnt].i.disk.major = major(stb.st_rdev); |
550 | devices[devcnt].i.disk.minor = minor(stb.st_rdev); | |
64c4757e | 551 | if (most_recent < devcnt) { |
213ee40b NB |
552 | if (devices[devcnt].i.events |
553 | > devices[most_recent].i.events) | |
64c4757e NB |
554 | most_recent = devcnt; |
555 | } | |
98dbd966 | 556 | if (content->array.level == -4) |
5787fa49 NB |
557 | /* with multipath, the raid_disk from the superblock is meaningless */ |
558 | i = devcnt; | |
559 | else | |
213ee40b | 560 | i = devices[devcnt].i.disk.raid_disk; |
308e1801 | 561 | if (i+1 == 0) { |
98dbd966 DW |
562 | if (nextspare < content->array.raid_disks) |
563 | nextspare = content->array.raid_disks; | |
308e1801 | 564 | i = nextspare++; |
08110d41 | 565 | } else { |
98dbd966 | 566 | if (i >= content->array.raid_disks && |
08110d41 NB |
567 | i >= nextspare) |
568 | nextspare = i+1; | |
308e1801 | 569 | } |
98c6faba | 570 | if (i < 10000) { |
56eedc1a | 571 | if (i >= bestcnt) { |
98c6faba | 572 | unsigned int newbestcnt = i+10; |
56eedc1a | 573 | int *newbest = malloc(sizeof(int)*newbestcnt); |
98c6faba | 574 | unsigned int c; |
56eedc1a NB |
575 | for (c=0; c < newbestcnt; c++) |
576 | if (c < bestcnt) | |
577 | newbest[c] = best[c]; | |
578 | else | |
579 | newbest[c] = -1; | |
580 | if (best)free(best); | |
581 | best = newbest; | |
582 | bestcnt = newbestcnt; | |
583 | } | |
6a255b69 | 584 | if (best[i] >=0 && |
213ee40b NB |
585 | devices[best[i]].i.events |
586 | == devices[devcnt].i.events | |
587 | && (devices[best[i]].i.disk.minor | |
588 | != devices[devcnt].i.disk.minor) | |
b8ac1967 | 589 | && st->ss == &super0 |
98dbd966 | 590 | && content->array.level != LEVEL_MULTIPATH) { |
6a255b69 NB |
591 | /* two different devices with identical superblock. |
592 | * Could be a mis-detection caused by overlapping | |
593 | * partitions. fail-safe. | |
594 | */ | |
595 | fprintf(stderr, Name ": WARNING %s and %s appear" | |
596 | " to have very similar superblocks.\n" | |
597 | " If they are really different, " | |
598 | "please --zero the superblock on one\n" | |
d2df86e0 NB |
599 | " If they are the same or overlap," |
600 | " please remove one from %s.\n", | |
601 | devices[best[i]].devname, devname, | |
602 | inargv ? "the list" : | |
603 | "the\n DEVICE list in mdadm.conf" | |
604 | ); | |
7f91af49 | 605 | close(mdfd); |
6a255b69 NB |
606 | return 1; |
607 | } | |
52826846 | 608 | if (best[i] == -1 |
213ee40b NB |
609 | || (devices[best[i]].i.events |
610 | < devices[devcnt].i.events)) | |
52826846 | 611 | best[i] = devcnt; |
56eedc1a | 612 | } |
64c4757e | 613 | devcnt++; |
df37ffc0 | 614 | } |
4b1ac34b | 615 | |
64c4757e | 616 | if (devcnt == 0) { |
682c7051 | 617 | fprintf(stderr, Name ": no devices found for %s\n", |
64c4757e | 618 | mddev); |
3d2b16e7 NB |
619 | if (st) |
620 | st->ss->free_super(st); | |
7f91af49 | 621 | close(mdfd); |
64c4757e NB |
622 | return 1; |
623 | } | |
4b1ac34b | 624 | |
3d2b16e7 NB |
625 | if (update && strcmp(update, "byteorder")==0) |
626 | st->minor_version = 90; | |
627 | ||
98dbd966 DW |
628 | st->ss->getinfo_super(st, content); |
629 | clean = content->array.state & 1; | |
4b1ac34b | 630 | |
64c4757e NB |
631 | /* now we have some devices that might be suitable. |
632 | * I wonder how many | |
633 | */ | |
98dbd966 DW |
634 | avail = malloc(content->array.raid_disks); |
635 | memset(avail, 0, content->array.raid_disks); | |
64c4757e | 636 | okcnt = 0; |
52826846 | 637 | sparecnt=0; |
56eedc1a | 638 | for (i=0; i< bestcnt ;i++) { |
64c4757e | 639 | int j = best[i]; |
ee04451c NB |
640 | int event_margin = 1; /* always allow a difference of '1' |
641 | * like the kernel does | |
642 | */ | |
64c4757e | 643 | if (j < 0) continue; |
d013a55e NB |
644 | /* note: we ignore error flags in multipath arrays |
645 | * as they don't make sense | |
646 | */ | |
98dbd966 | 647 | if (content->array.level != -4) |
213ee40b NB |
648 | if (!(devices[j].i.disk.state & (1<<MD_DISK_SYNC))) { |
649 | if (!(devices[j].i.disk.state | |
650 | & (1<<MD_DISK_FAULTY))) | |
aa88f531 | 651 | sparecnt++; |
d013a55e | 652 | continue; |
aa88f531 | 653 | } |
213ee40b NB |
654 | if (devices[j].i.events+event_margin >= |
655 | devices[most_recent].i.events) { | |
64c4757e | 656 | devices[j].uptodate = 1; |
98dbd966 | 657 | if (i < content->array.raid_disks) { |
52826846 | 658 | okcnt++; |
265e0f17 NB |
659 | avail[i]=1; |
660 | } else | |
52826846 | 661 | sparecnt++; |
64c4757e NB |
662 | } |
663 | } | |
98dbd966 DW |
664 | while (force && !enough(content->array.level, content->array.raid_disks, |
665 | content->array.layout, 1, | |
265e0f17 | 666 | avail, okcnt)) { |
64c4757e NB |
667 | /* Choose the newest best drive which is |
668 | * not up-to-date, update the superblock | |
669 | * and add it. | |
670 | */ | |
52826846 | 671 | int fd; |
3da92f27 | 672 | struct supertype *tst; |
c5a6f9a6 | 673 | long long current_events; |
cd29a5c8 | 674 | chosen_drive = -1; |
98dbd966 | 675 | for (i=0; i<content->array.raid_disks && i < bestcnt; i++) { |
52826846 NB |
676 | int j = best[i]; |
677 | if (j>=0 && | |
678 | !devices[j].uptodate && | |
213ee40b | 679 | devices[j].i.events > 0 && |
52826846 | 680 | (chosen_drive < 0 || |
213ee40b NB |
681 | devices[j].i.events |
682 | > devices[chosen_drive].i.events)) | |
52826846 NB |
683 | chosen_drive = j; |
684 | } | |
685 | if (chosen_drive < 0) | |
686 | break; | |
213ee40b | 687 | current_events = devices[chosen_drive].i.events; |
c5a6f9a6 | 688 | add_another: |
dab6685f NB |
689 | if (verbose >= 0) |
690 | fprintf(stderr, Name ": forcing event count in %s(%d) from %d upto %d\n", | |
213ee40b NB |
691 | devices[chosen_drive].devname, |
692 | devices[chosen_drive].i.disk.raid_disk, | |
693 | (int)(devices[chosen_drive].i.events), | |
694 | (int)(devices[most_recent].i.events)); | |
8b0dabea | 695 | fd = dev_open(devices[chosen_drive].devname, O_RDWR|O_EXCL); |
52826846 NB |
696 | if (fd < 0) { |
697 | fprintf(stderr, Name ": Couldn't open %s for write - not updating\n", | |
698 | devices[chosen_drive].devname); | |
213ee40b | 699 | devices[chosen_drive].i.events = 0; |
52826846 NB |
700 | continue; |
701 | } | |
3da92f27 | 702 | tst = dup_super(st); |
60b435db | 703 | if (tst->ss->load_super(tst,fd, NULL)) { |
52826846 NB |
704 | close(fd); |
705 | fprintf(stderr, Name ": RAID superblock disappeared from %s - not updating.\n", | |
706 | devices[chosen_drive].devname); | |
213ee40b | 707 | devices[chosen_drive].i.events = 0; |
52826846 NB |
708 | continue; |
709 | } | |
98dbd966 DW |
710 | content->events = devices[most_recent].i.events; |
711 | tst->ss->update_super(tst, content, "force-one", | |
67a8c82d NB |
712 | devices[chosen_drive].devname, verbose, |
713 | 0, NULL); | |
4b1ac34b | 714 | |
3da92f27 | 715 | if (tst->ss->store_super(tst, fd)) { |
52826846 NB |
716 | close(fd); |
717 | fprintf(stderr, Name ": Could not re-write superblock on %s\n", | |
718 | devices[chosen_drive].devname); | |
213ee40b | 719 | devices[chosen_drive].i.events = 0; |
3da92f27 | 720 | tst->ss->free_super(tst); |
52826846 NB |
721 | continue; |
722 | } | |
723 | close(fd); | |
213ee40b | 724 | devices[chosen_drive].i.events = devices[most_recent].i.events; |
52826846 | 725 | devices[chosen_drive].uptodate = 1; |
265e0f17 | 726 | avail[chosen_drive] = 1; |
52826846 | 727 | okcnt++; |
3da92f27 | 728 | tst->ss->free_super(tst); |
c5a6f9a6 NB |
729 | |
730 | /* If there are any other drives of the same vintage, | |
731 | * add them in as well. We can't lose and we might gain | |
732 | */ | |
98dbd966 | 733 | for (i=0; i<content->array.raid_disks && i < bestcnt ; i++) { |
c5a6f9a6 NB |
734 | int j = best[i]; |
735 | if (j >= 0 && | |
736 | !devices[j].uptodate && | |
213ee40b NB |
737 | devices[j].i.events > 0 && |
738 | devices[j].i.events == current_events) { | |
c5a6f9a6 NB |
739 | chosen_drive = j; |
740 | goto add_another; | |
741 | } | |
742 | } | |
64c4757e | 743 | } |
52826846 NB |
744 | |
745 | /* Now we want to look at the superblock which the kernel will base things on | |
746 | * and compare the devices that we think are working with the devices that the | |
747 | * superblock thinks are working. | |
748 | * If there are differences and --force is given, then update this chosen | |
749 | * superblock. | |
750 | */ | |
cd29a5c8 | 751 | chosen_drive = -1; |
3da92f27 | 752 | st->ss->free_super(st); |
56eedc1a | 753 | for (i=0; chosen_drive < 0 && i<bestcnt; i++) { |
52826846 NB |
754 | int j = best[i]; |
755 | int fd; | |
4b1ac34b | 756 | |
52826846 NB |
757 | if (j<0) |
758 | continue; | |
759 | if (!devices[j].uptodate) | |
760 | continue; | |
761 | chosen_drive = j; | |
8b0dabea | 762 | if ((fd=dev_open(devices[j].devname, O_RDONLY|O_EXCL))< 0) { |
52826846 NB |
763 | fprintf(stderr, Name ": Cannot open %s: %s\n", |
764 | devices[j].devname, strerror(errno)); | |
7f91af49 | 765 | close(mdfd); |
52826846 NB |
766 | return 1; |
767 | } | |
3da92f27 | 768 | if (st->ss->load_super(st,fd, NULL)) { |
52826846 NB |
769 | close(fd); |
770 | fprintf(stderr, Name ": RAID superblock has disappeared from %s\n", | |
771 | devices[j].devname); | |
7f91af49 | 772 | close(mdfd); |
52826846 NB |
773 | return 1; |
774 | } | |
775 | close(fd); | |
776 | } | |
3da92f27 | 777 | if (st->sb == NULL) { |
4b1ac34b | 778 | fprintf(stderr, Name ": No suitable drives found for %s\n", mddev); |
7f91af49 | 779 | close(mdfd); |
4b1ac34b NB |
780 | return 1; |
781 | } | |
98dbd966 | 782 | st->ss->getinfo_super(st, content); |
f35f2525 | 783 | #ifndef MDASSEMBLE |
98dbd966 | 784 | sysfs_init(content, mdfd, 0); |
f35f2525 | 785 | #endif |
56eedc1a | 786 | for (i=0; i<bestcnt; i++) { |
52826846 | 787 | int j = best[i]; |
98c6faba | 788 | unsigned int desired_state; |
11a3e71d | 789 | |
98dbd966 | 790 | if (i < content->array.raid_disks) |
11a3e71d NB |
791 | desired_state = (1<<MD_DISK_ACTIVE) | (1<<MD_DISK_SYNC); |
792 | else | |
793 | desired_state = 0; | |
794 | ||
52826846 NB |
795 | if (j<0) |
796 | continue; | |
797 | if (!devices[j].uptodate) | |
798 | continue; | |
4b1ac34b | 799 | |
213ee40b NB |
800 | devices[j].i.disk.state = desired_state; |
801 | ||
802 | if (st->ss->update_super(st, &devices[j].i, "assemble", NULL, | |
68c7d6d7 | 803 | verbose, 0, NULL)) { |
52826846 | 804 | if (force) { |
dab6685f NB |
805 | if (verbose >= 0) |
806 | fprintf(stderr, Name ": " | |
807 | "clearing FAULTY flag for device %d in %s for %s\n", | |
808 | j, mddev, devices[j].devname); | |
4b1ac34b | 809 | change = 1; |
52826846 | 810 | } else { |
dab6685f NB |
811 | if (verbose >= -1) |
812 | fprintf(stderr, Name ": " | |
813 | "device %d in %s has wrong state in superblock, but %s seems ok\n", | |
814 | i, mddev, devices[j].devname); | |
52826846 NB |
815 | } |
816 | } | |
4b1ac34b | 817 | #if 0 |
213ee40b | 818 | if (!(super.disks[i].i.disk.state & (1 << MD_DISK_FAULTY))) { |
52826846 NB |
819 | fprintf(stderr, Name ": devices %d of %s is not marked FAULTY in superblock, but cannot be found\n", |
820 | i, mddev); | |
821 | } | |
4b1ac34b | 822 | #endif |
52826846 | 823 | } |
c5a6f9a6 | 824 | if (force && !clean && |
98dbd966 DW |
825 | !enough(content->array.level, content->array.raid_disks, |
826 | content->array.layout, clean, | |
c5a6f9a6 | 827 | avail, okcnt)) { |
98dbd966 | 828 | change += st->ss->update_super(st, content, "force-array", |
67a8c82d NB |
829 | devices[chosen_drive].devname, verbose, |
830 | 0, NULL); | |
583315d9 | 831 | clean = 1; |
d013a55e | 832 | } |
52826846 | 833 | |
4b1ac34b | 834 | if (change) { |
52826846 | 835 | int fd; |
8b0dabea | 836 | fd = dev_open(devices[chosen_drive].devname, O_RDWR|O_EXCL); |
52826846 | 837 | if (fd < 0) { |
353632d9 | 838 | fprintf(stderr, Name ": Could not open %s for write - cannot Assemble array.\n", |
52826846 | 839 | devices[chosen_drive].devname); |
7f91af49 | 840 | close(mdfd); |
52826846 NB |
841 | return 1; |
842 | } | |
3da92f27 | 843 | if (st->ss->store_super(st, fd)) { |
52826846 NB |
844 | close(fd); |
845 | fprintf(stderr, Name ": Could not re-write superblock on %s\n", | |
846 | devices[chosen_drive].devname); | |
7f91af49 | 847 | close(mdfd); |
52826846 NB |
848 | return 1; |
849 | } | |
850 | close(fd); | |
52826846 NB |
851 | } |
852 | ||
353632d9 NB |
853 | /* If we are in the middle of a reshape we may need to restore saved data |
854 | * that was moved aside due to the reshape overwriting live data | |
855 | * The code of doing this lives in Grow.c | |
856 | */ | |
39c5a909 | 857 | #ifndef MDASSEMBLE |
98dbd966 | 858 | if (content->reshape_active) { |
353632d9 NB |
859 | int err = 0; |
860 | int *fdlist = malloc(sizeof(int)* bestcnt); | |
861 | for (i=0; i<bestcnt; i++) { | |
862 | int j = best[i]; | |
863 | if (j >= 0) { | |
864 | fdlist[i] = dev_open(devices[j].devname, O_RDWR|O_EXCL); | |
865 | if (fdlist[i] < 0) { | |
866 | fprintf(stderr, Name ": Could not open %s for write - cannot Assemble array.\n", | |
867 | devices[j].devname); | |
868 | err = 1; | |
869 | break; | |
870 | } | |
871 | } else | |
872 | fdlist[i] = -1; | |
873 | } | |
874 | if (!err) | |
98dbd966 | 875 | err = Grow_restart(st, content, fdlist, bestcnt, backup_file); |
353632d9 NB |
876 | while (i>0) { |
877 | i--; | |
878 | if (fdlist[i]>=0) close(fdlist[i]); | |
879 | } | |
880 | if (err) { | |
881 | fprintf(stderr, Name ": Failed to restore critical section for reshape, sorry.\n"); | |
7f91af49 | 882 | close(mdfd); |
353632d9 NB |
883 | return err; |
884 | } | |
885 | } | |
39c5a909 | 886 | #endif |
aa88f531 NB |
887 | /* count number of in-sync devices according to the superblock. |
888 | * We must have this number to start the array without -s or -R | |
889 | */ | |
98dbd966 | 890 | req_cnt = content->array.working_disks; |
aa88f531 | 891 | |
64c4757e NB |
892 | /* Almost ready to actually *do* something */ |
893 | if (!old_linux) { | |
fbf8a0b7 | 894 | int rv; |
a19c88b8 | 895 | |
a04d5763 N |
896 | /* First, fill in the map, so that udev can find our name |
897 | * as soon as we become active. | |
898 | */ | |
98dbd966 DW |
899 | map_update(NULL, fd2devnum(mdfd), content->text_version, |
900 | content->uuid, chosen_name); | |
a04d5763 | 901 | |
98dbd966 | 902 | rv = set_array_info(mdfd, st, content); |
fbf8a0b7 | 903 | if (rv) { |
f35f2525 | 904 | fprintf(stderr, Name ": failed to set array info for %s: %s\n", |
64c4757e | 905 | mddev, strerror(errno)); |
7f91af49 | 906 | close(mdfd); |
64c4757e NB |
907 | return 1; |
908 | } | |
11484a63 | 909 | if (ident->bitmap_fd >= 0) { |
c82f047c NB |
910 | if (ioctl(mdfd, SET_BITMAP_FILE, ident->bitmap_fd) != 0) { |
911 | fprintf(stderr, Name ": SET_BITMAP_FILE failed.\n"); | |
7f91af49 | 912 | close(mdfd); |
c82f047c NB |
913 | return 1; |
914 | } | |
7ef02d01 NB |
915 | } else if (ident->bitmap_file) { |
916 | /* From config file */ | |
917 | int bmfd = open(ident->bitmap_file, O_RDWR); | |
918 | if (bmfd < 0) { | |
919 | fprintf(stderr, Name ": Could not open bitmap file %s\n", | |
920 | ident->bitmap_file); | |
7f91af49 | 921 | close(mdfd); |
7ef02d01 NB |
922 | return 1; |
923 | } | |
924 | if (ioctl(mdfd, SET_BITMAP_FILE, bmfd) != 0) { | |
925 | fprintf(stderr, Name ": Failed to set bitmapfile for %s\n", mddev); | |
926 | close(bmfd); | |
7f91af49 | 927 | close(mdfd); |
7ef02d01 NB |
928 | return 1; |
929 | } | |
930 | close(bmfd); | |
c82f047c | 931 | } |
7ef02d01 | 932 | |
52826846 | 933 | /* First, add the raid disks, but add the chosen one last */ |
56eedc1a | 934 | for (i=0; i<= bestcnt; i++) { |
52826846 | 935 | int j; |
56eedc1a | 936 | if (i < bestcnt) { |
52826846 NB |
937 | j = best[i]; |
938 | if (j == chosen_drive) | |
939 | continue; | |
940 | } else | |
941 | j = chosen_drive; | |
942 | ||
56eedc1a | 943 | if (j >= 0 /* && devices[j].uptodate */) { |
98dbd966 | 944 | rv = add_disk(mdfd, st, content, &devices[j].i); |
7801ac20 | 945 | |
a19c88b8 | 946 | if (rv) { |
213ee40b NB |
947 | fprintf(stderr, Name ": failed to add " |
948 | "%s to %s: %s\n", | |
64c4757e NB |
949 | devices[j].devname, |
950 | mddev, | |
951 | strerror(errno)); | |
98dbd966 | 952 | if (i < content->array.raid_disks |
213ee40b | 953 | || i == bestcnt) |
52826846 NB |
954 | okcnt--; |
955 | else | |
956 | sparecnt--; | |
dab6685f | 957 | } else if (verbose > 0) |
213ee40b NB |
958 | fprintf(stderr, Name ": added %s " |
959 | "to %s as %d\n", | |
960 | devices[j].devname, mddev, | |
961 | devices[j].i.disk.raid_disk); | |
98dbd966 | 962 | } else if (verbose > 0 && i < content->array.raid_disks) |
213ee40b NB |
963 | fprintf(stderr, Name ": no uptodate device for " |
964 | "slot %d of %s\n", | |
64c4757e NB |
965 | i, mddev); |
966 | } | |
aba69144 | 967 | |
98dbd966 | 968 | if (content->array.level == LEVEL_CONTAINER) { |
598f0d58 NB |
969 | if (verbose >= 0) { |
970 | fprintf(stderr, Name ": Container %s has been " | |
971 | "assembled with %d drive%s", | |
57ed8c91 | 972 | mddev, okcnt+sparecnt, okcnt+sparecnt==1?"":"s"); |
98dbd966 | 973 | if (okcnt < content->array.raid_disks) |
598f0d58 | 974 | fprintf(stderr, " (out of %d)", |
98dbd966 | 975 | content->array.raid_disks); |
598f0d58 NB |
976 | fprintf(stderr, "\n"); |
977 | } | |
98dbd966 | 978 | sysfs_uevent(content, "change"); |
7f91af49 | 979 | close(mdfd); |
598f0d58 NB |
980 | return 0; |
981 | } | |
982 | ||
64c4757e | 983 | if (runstop == 1 || |
b8a8ccf9 | 984 | (runstop <= 0 && |
98dbd966 DW |
985 | ( enough(content->array.level, content->array.raid_disks, |
986 | content->array.layout, clean, avail, okcnt) && | |
aa88f531 NB |
987 | (okcnt >= req_cnt || start_partial_ok) |
988 | ))) { | |
82b27616 | 989 | if (ioctl(mdfd, RUN_ARRAY, NULL)==0) { |
dab6685f NB |
990 | if (verbose >= 0) { |
991 | fprintf(stderr, Name ": %s has been started with %d drive%s", | |
992 | mddev, okcnt, okcnt==1?"":"s"); | |
98dbd966 DW |
993 | if (okcnt < content->array.raid_disks) |
994 | fprintf(stderr, " (out of %d)", content->array.raid_disks); | |
dab6685f NB |
995 | if (sparecnt) |
996 | fprintf(stderr, " and %d spare%s", sparecnt, sparecnt==1?"":"s"); | |
997 | fprintf(stderr, ".\n"); | |
998 | } | |
7f91af49 N |
999 | close(mdfd); |
1000 | if (auto_assem) { | |
589395d6 | 1001 | int usecs = 1; |
589395d6 NB |
1002 | /* There is a nasty race with 'mdadm --monitor'. |
1003 | * If it opens this device before we close it, | |
1004 | * it gets an incomplete open on which IO | |
598f0d58 NB |
1005 | * doesn't work and the capacity is |
1006 | * wrong. | |
589395d6 NB |
1007 | * If we reopen (to check for layered devices) |
1008 | * before --monitor closes, we loose. | |
1009 | * | |
1010 | * So: wait upto 1 second for there to be | |
1011 | * a non-zero capacity. | |
1012 | */ | |
1013 | while (usecs < 1000) { | |
1014 | mdfd = open(mddev, O_RDONLY); | |
1015 | if (mdfd >= 0) { | |
beae1dfe NB |
1016 | unsigned long long size; |
1017 | if (get_dev_size(mdfd, NULL, &size) && | |
589395d6 NB |
1018 | size > 0) |
1019 | break; | |
1020 | close(mdfd); | |
1021 | } | |
1022 | usleep(usecs); | |
1023 | usecs <<= 1; | |
1024 | } | |
1025 | } | |
64c4757e | 1026 | return 0; |
82b27616 | 1027 | } |
682c7051 | 1028 | fprintf(stderr, Name ": failed to RUN_ARRAY %s: %s\n", |
64c4757e | 1029 | mddev, strerror(errno)); |
583315d9 | 1030 | |
98dbd966 DW |
1031 | if (!enough(content->array.level, content->array.raid_disks, |
1032 | content->array.layout, 1, avail, okcnt)) | |
583315d9 NB |
1033 | fprintf(stderr, Name ": Not enough devices to " |
1034 | "start the array.\n"); | |
98dbd966 DW |
1035 | else if (!enough(content->array.level, |
1036 | content->array.raid_disks, | |
1037 | content->array.layout, clean, | |
583315d9 NB |
1038 | avail, okcnt)) |
1039 | fprintf(stderr, Name ": Not enough devices to " | |
1040 | "start the array while not clean " | |
1041 | "- consider --force.\n"); | |
1042 | ||
7f91af49 | 1043 | if (auto_assem) |
1c203a4b | 1044 | ioctl(mdfd, STOP_ARRAY, NULL); |
7f91af49 | 1045 | close(mdfd); |
64c4757e NB |
1046 | return 1; |
1047 | } | |
82b27616 | 1048 | if (runstop == -1) { |
b8a8ccf9 | 1049 | fprintf(stderr, Name ": %s assembled from %d drive%s", |
52826846 | 1050 | mddev, okcnt, okcnt==1?"":"s"); |
98dbd966 DW |
1051 | if (okcnt != content->array.raid_disks) |
1052 | fprintf(stderr, " (out of %d)", content->array.raid_disks); | |
b8a8ccf9 | 1053 | fprintf(stderr, ", but not started.\n"); |
7f91af49 | 1054 | close(mdfd); |
64c4757e | 1055 | return 0; |
82b27616 | 1056 | } |
4855f95c | 1057 | if (verbose >= -1) { |
dab6685f NB |
1058 | fprintf(stderr, Name ": %s assembled from %d drive%s", mddev, okcnt, okcnt==1?"":"s"); |
1059 | if (sparecnt) | |
1060 | fprintf(stderr, " and %d spare%s", sparecnt, sparecnt==1?"":"s"); | |
98dbd966 DW |
1061 | if (!enough(content->array.level, content->array.raid_disks, |
1062 | content->array.layout, 1, avail, okcnt)) | |
dab6685f | 1063 | fprintf(stderr, " - not enough to start the array.\n"); |
98dbd966 DW |
1064 | else if (!enough(content->array.level, |
1065 | content->array.raid_disks, | |
1066 | content->array.layout, clean, | |
583315d9 NB |
1067 | avail, okcnt)) |
1068 | fprintf(stderr, " - not enough to start the " | |
1069 | "array while not clean - consider " | |
1070 | "--force.\n"); | |
dab6685f | 1071 | else { |
98dbd966 | 1072 | if (req_cnt == content->array.raid_disks) |
dab6685f NB |
1073 | fprintf(stderr, " - need all %d to start it", req_cnt); |
1074 | else | |
98dbd966 | 1075 | fprintf(stderr, " - need %d of %d to start", req_cnt, content->array.raid_disks); |
dab6685f NB |
1076 | fprintf(stderr, " (use --run to insist).\n"); |
1077 | } | |
aa88f531 | 1078 | } |
7f91af49 | 1079 | if (auto_assem) |
1c203a4b | 1080 | ioctl(mdfd, STOP_ARRAY, NULL); |
64c4757e | 1081 | return 1; |
82b27616 | 1082 | } else { |
52826846 NB |
1083 | /* The "chosen_drive" is a good choice, and if necessary, the superblock has |
1084 | * been updated to point to the current locations of devices. | |
1085 | * so we can just start the array | |
82b27616 | 1086 | */ |
cd29a5c8 | 1087 | unsigned long dev; |
213ee40b NB |
1088 | dev = makedev(devices[chosen_drive].i.disk.major, |
1089 | devices[chosen_drive].i.disk.minor); | |
82b27616 NB |
1090 | if (ioctl(mdfd, START_ARRAY, dev)) { |
1091 | fprintf(stderr, Name ": Cannot start array: %s\n", | |
1092 | strerror(errno)); | |
1093 | } | |
aba69144 | 1094 | |
64c4757e | 1095 | } |
7f91af49 | 1096 | close(mdfd); |
e0d19036 | 1097 | return 0; |
64c4757e | 1098 | } |
6234c63c DW |
1099 | |
1100 | #ifndef MDASSEMBLE | |
1101 | int assemble_container_content(struct supertype *st, int mdfd, | |
1102 | struct mdinfo *content, int runstop, | |
1103 | char *chosen_name, int verbose) | |
1104 | { | |
1105 | struct mdinfo *dev, *sra; | |
1106 | int working = 0, preexist = 0; | |
1107 | struct map_ent *map = NULL; | |
1108 | ||
1109 | sysfs_init(content, mdfd, 0); | |
1110 | ||
1111 | sra = sysfs_read(mdfd, 0, GET_VERSION); | |
1112 | if (sra == NULL || strcmp(sra->text_version, content->text_version) != 0) | |
1113 | if (sysfs_set_array(content, md_get_version(mdfd)) != 0) | |
1114 | return 1; | |
1115 | if (sra) | |
1116 | sysfs_free(sra); | |
1117 | ||
1118 | for (dev = content->devs; dev; dev = dev->next) | |
1119 | if (sysfs_add_disk(content, dev) == 0) | |
1120 | working++; | |
1121 | else if (errno == EEXIST) | |
1122 | preexist++; | |
1123 | if (working == 0) | |
1124 | /* Nothing new, don't try to start */ ; | |
1125 | else if (runstop > 0 || | |
1126 | (working + preexist) >= content->array.working_disks) { | |
1127 | switch(content->array.level) { | |
1128 | case LEVEL_LINEAR: | |
1129 | case LEVEL_MULTIPATH: | |
1130 | case 0: | |
1131 | sysfs_set_str(content, NULL, "array_state", | |
1132 | "active"); | |
1133 | break; | |
1134 | default: | |
1135 | sysfs_set_str(content, NULL, "array_state", | |
1136 | "readonly"); | |
1137 | /* start mdmon if needed. */ | |
1138 | if (!mdmon_running(st->container_dev)) | |
1139 | start_mdmon(st->container_dev); | |
1140 | ping_monitor(devnum2devname(st->container_dev)); | |
1141 | break; | |
1142 | } | |
1143 | sysfs_set_safemode(content, content->safe_mode_delay); | |
1144 | if (verbose >= 0) { | |
1145 | fprintf(stderr, Name | |
1146 | ": Started %s with %d devices", | |
1147 | chosen_name, working + preexist); | |
1148 | if (preexist) | |
1149 | fprintf(stderr, " (%d new)", working); | |
1150 | fprintf(stderr, "\n"); | |
1151 | } | |
1152 | /* FIXME should have an O_EXCL and wait for read-auto */ | |
1153 | } else | |
1154 | if (verbose >= 0) | |
1155 | fprintf(stderr, Name | |
1156 | ": %s assembled with %d devices but " | |
1157 | "not started\n", | |
1158 | chosen_name, working); | |
1159 | map_update(&map, fd2devnum(mdfd), | |
1160 | content->text_version, | |
1161 | content->uuid, chosen_name); | |
1162 | ||
1163 | return 0; | |
1164 | } | |
1165 | #endif | |
1166 |