]>
Commit | Line | Data |
---|---|---|
1 | /* | |
2 | * mdadm - manage Linux "md" devices aka RAID arrays. | |
3 | * | |
4 | * Copyright (C) 2001-2009 Neil Brown <neilb@suse.de> | |
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@suse.de> | |
23 | */ | |
24 | ||
25 | #include "mdadm.h" | |
26 | #include "md_u.h" | |
27 | #include "md_p.h" | |
28 | #include <ctype.h> | |
29 | ||
30 | static int default_layout(struct supertype *st, int level, int verbose) | |
31 | { | |
32 | int layout = UnSet; | |
33 | ||
34 | if (st && st->ss->default_layout) | |
35 | layout = st->ss->default_layout(level); | |
36 | ||
37 | if (layout == UnSet) | |
38 | switch(level) { | |
39 | default: /* no layout */ | |
40 | layout = 0; | |
41 | break; | |
42 | case 10: | |
43 | layout = 0x102; /* near=2, far=1 */ | |
44 | if (verbose > 0) | |
45 | fprintf(stderr, | |
46 | Name ": layout defaults to n2\n"); | |
47 | break; | |
48 | case 5: | |
49 | case 6: | |
50 | layout = map_name(r5layout, "default"); | |
51 | if (verbose > 0) | |
52 | fprintf(stderr, | |
53 | Name ": layout defaults to %s\n", map_num(r5layout, layout)); | |
54 | break; | |
55 | case LEVEL_FAULTY: | |
56 | layout = map_name(faultylayout, "default"); | |
57 | ||
58 | if (verbose > 0) | |
59 | fprintf(stderr, | |
60 | Name ": layout defaults to %s\n", map_num(faultylayout, layout)); | |
61 | break; | |
62 | } | |
63 | ||
64 | return layout; | |
65 | } | |
66 | ||
67 | ||
68 | int Create(struct supertype *st, char *mddev, | |
69 | int chunk, int level, int layout, unsigned long long size, int raiddisks, int sparedisks, | |
70 | char *name, char *homehost, int *uuid, | |
71 | int subdevs, mddev_dev_t devlist, | |
72 | int runstop, int verbose, int force, int assume_clean, | |
73 | char *bitmap_file, int bitmap_chunk, int write_behind, int delay, int autof) | |
74 | { | |
75 | /* | |
76 | * Create a new raid array. | |
77 | * | |
78 | * First check that necessary details are available | |
79 | * (i.e. level, raid-disks) | |
80 | * | |
81 | * Then check each disk to see what might be on it | |
82 | * and report anything interesting. | |
83 | * | |
84 | * If anything looks odd, and runstop not set, | |
85 | * abort. | |
86 | * | |
87 | * SET_ARRAY_INFO and ADD_NEW_DISK, and | |
88 | * if runstop==run, or raiddisks disks were used, | |
89 | * RUN_ARRAY | |
90 | */ | |
91 | int mdfd; | |
92 | unsigned long long minsize=0, maxsize=0; | |
93 | char *mindisc = NULL; | |
94 | char *maxdisc = NULL; | |
95 | int dnum; | |
96 | mddev_dev_t dv; | |
97 | int fail=0, warn=0; | |
98 | struct stat stb; | |
99 | int first_missing = subdevs * 2; | |
100 | int second_missing = subdevs * 2; | |
101 | int missing_disks = 0; | |
102 | int insert_point = subdevs * 2; /* where to insert a missing drive */ | |
103 | int total_slots; | |
104 | int pass; | |
105 | int vers; | |
106 | int rv; | |
107 | int bitmap_fd; | |
108 | int have_container = 0; | |
109 | int container_fd = -1; | |
110 | int need_mdmon = 0; | |
111 | unsigned long long bitmapsize; | |
112 | struct mdinfo info, *infos; | |
113 | int did_default = 0; | |
114 | int do_default_layout = 0; | |
115 | unsigned long safe_mode_delay = 0; | |
116 | char chosen_name[1024]; | |
117 | struct map_ent *map = NULL; | |
118 | unsigned long long newsize; | |
119 | ||
120 | int major_num = BITMAP_MAJOR_HI; | |
121 | ||
122 | memset(&info, 0, sizeof(info)); | |
123 | ||
124 | if (level == UnSet) { | |
125 | /* "ddf" and "imsm" metadata only supports one level - should possibly | |
126 | * push this into metadata handler?? | |
127 | */ | |
128 | if (st && (st->ss == &super_ddf || st->ss == &super_imsm)) | |
129 | level = LEVEL_CONTAINER; | |
130 | } | |
131 | ||
132 | if (level == UnSet) { | |
133 | fprintf(stderr, | |
134 | Name ": a RAID level is needed to create an array.\n"); | |
135 | return 1; | |
136 | } | |
137 | if (raiddisks < 4 && level == 6) { | |
138 | fprintf(stderr, | |
139 | Name ": at least 4 raid-devices needed for level 6\n"); | |
140 | return 1; | |
141 | } | |
142 | if (raiddisks > 256 && level == 6) { | |
143 | fprintf(stderr, | |
144 | Name ": no more than 256 raid-devices supported for level 6\n"); | |
145 | return 1; | |
146 | } | |
147 | if (raiddisks < 2 && level >= 4) { | |
148 | fprintf(stderr, | |
149 | Name ": at least 2 raid-devices needed for level 4 or 5\n"); | |
150 | return 1; | |
151 | } | |
152 | if (level <= 0 && sparedisks) { | |
153 | fprintf(stderr, | |
154 | Name ": This level does not support spare devices\n"); | |
155 | return 1; | |
156 | } | |
157 | ||
158 | if (subdevs == 1 && strcmp(devlist->devname, "missing") != 0) { | |
159 | /* If given a single device, it might be a container, and we can | |
160 | * extract a device list from there | |
161 | */ | |
162 | mdu_array_info_t inf; | |
163 | int fd; | |
164 | ||
165 | memset(&inf, 0, sizeof(inf)); | |
166 | fd = open(devlist->devname, O_RDONLY); | |
167 | if (fd >= 0 && | |
168 | ioctl(fd, GET_ARRAY_INFO, &inf) == 0 && | |
169 | inf.raid_disks == 0) { | |
170 | /* yep, looks like a container */ | |
171 | if (st) { | |
172 | rv = st->ss->load_super(st, fd, | |
173 | devlist->devname); | |
174 | if (rv == 0) | |
175 | have_container = 1; | |
176 | } else { | |
177 | st = guess_super(fd); | |
178 | if (st && !(rv = st->ss-> | |
179 | load_super(st, fd, | |
180 | devlist->devname))) | |
181 | have_container = 1; | |
182 | else | |
183 | st = NULL; | |
184 | } | |
185 | if (have_container) { | |
186 | subdevs = raiddisks; | |
187 | first_missing = subdevs * 2; | |
188 | second_missing = subdevs * 2; | |
189 | insert_point = subdevs * 2; | |
190 | } | |
191 | } | |
192 | if (fd >= 0) | |
193 | close(fd); | |
194 | } | |
195 | if (st && st->ss->external && sparedisks) { | |
196 | fprintf(stderr, | |
197 | Name ": This metadata type does not support " | |
198 | "spare disks at create time\n"); | |
199 | return 1; | |
200 | } | |
201 | if (subdevs > raiddisks+sparedisks) { | |
202 | fprintf(stderr, Name ": You have listed more devices (%d) than are in the array(%d)!\n", subdevs, raiddisks+sparedisks); | |
203 | return 1; | |
204 | } | |
205 | if (!have_container && subdevs < raiddisks+sparedisks) { | |
206 | fprintf(stderr, Name ": You haven't given enough devices (real or missing) to create this array\n"); | |
207 | return 1; | |
208 | } | |
209 | if (bitmap_file && level <= 0) { | |
210 | fprintf(stderr, Name ": bitmaps not meaningful with level %s\n", | |
211 | map_num(pers, level)?:"given"); | |
212 | return 1; | |
213 | } | |
214 | ||
215 | /* now set some defaults */ | |
216 | ||
217 | ||
218 | if (layout == UnSet) { | |
219 | do_default_layout = 1; | |
220 | layout = default_layout(st, level, verbose); | |
221 | } | |
222 | ||
223 | if (level == 10) | |
224 | /* check layout fits in array*/ | |
225 | if ((layout&255) * ((layout>>8)&255) > raiddisks) { | |
226 | fprintf(stderr, Name ": that layout requires at least %d devices\n", | |
227 | (layout&255) * ((layout>>8)&255)); | |
228 | return 1; | |
229 | } | |
230 | ||
231 | switch(level) { | |
232 | case 4: | |
233 | case 5: | |
234 | case 10: | |
235 | case 6: | |
236 | case 0: | |
237 | if (chunk == 0) { | |
238 | if (st && st->ss->default_chunk) | |
239 | chunk = st->ss->default_chunk(st); | |
240 | ||
241 | chunk = chunk ? : 512; | |
242 | ||
243 | if (verbose > 0) | |
244 | fprintf(stderr, Name ": chunk size defaults to %dK\n", chunk); | |
245 | } | |
246 | break; | |
247 | case LEVEL_LINEAR: | |
248 | /* a chunksize of zero 0s perfectly valid (and preferred) since 2.6.16 */ | |
249 | if (get_linux_version() < 2006016 && chunk == 0) { | |
250 | chunk = 64; | |
251 | if (verbose > 0) | |
252 | fprintf(stderr, Name ": chunk size defaults to 64K\n"); | |
253 | } | |
254 | break; | |
255 | case 1: | |
256 | case LEVEL_FAULTY: | |
257 | case LEVEL_MULTIPATH: | |
258 | case LEVEL_CONTAINER: | |
259 | if (chunk) { | |
260 | chunk = 0; | |
261 | if (verbose > 0) | |
262 | fprintf(stderr, Name ": chunk size ignored for this level\n"); | |
263 | } | |
264 | break; | |
265 | default: | |
266 | fprintf(stderr, Name ": unknown level %d\n", level); | |
267 | return 1; | |
268 | } | |
269 | ||
270 | if (size && chunk) | |
271 | size &= ~(unsigned long long)(chunk - 1); | |
272 | newsize = size * 2; | |
273 | if (st && ! st->ss->validate_geometry(st, level, layout, raiddisks, | |
274 | chunk, size*2, NULL, &newsize, verbose>=0)) | |
275 | return 1; | |
276 | if (size == 0) { | |
277 | size = newsize / 2; | |
278 | if (size && verbose > 0) | |
279 | fprintf(stderr, Name ": setting size to %lluK\n", | |
280 | (unsigned long long)size); | |
281 | } | |
282 | ||
283 | /* now look at the subdevs */ | |
284 | info.array.active_disks = 0; | |
285 | info.array.working_disks = 0; | |
286 | dnum = 0; | |
287 | for (dv=devlist; dv && !have_container; dv=dv->next, dnum++) { | |
288 | char *dname = dv->devname; | |
289 | unsigned long long freesize; | |
290 | if (strcasecmp(dname, "missing")==0) { | |
291 | if (first_missing > dnum) | |
292 | first_missing = dnum; | |
293 | if (second_missing > dnum && dnum > first_missing) | |
294 | second_missing = dnum; | |
295 | missing_disks ++; | |
296 | continue; | |
297 | } | |
298 | info.array.working_disks++; | |
299 | if (dnum < raiddisks) | |
300 | info.array.active_disks++; | |
301 | if (st == NULL) { | |
302 | struct createinfo *ci = conf_get_create_info(); | |
303 | if (ci) | |
304 | st = ci->supertype; | |
305 | } | |
306 | if (st == NULL) { | |
307 | /* Need to choose a default metadata, which is different | |
308 | * depending on geometry of array. | |
309 | */ | |
310 | int i; | |
311 | char *name = "default"; | |
312 | for(i=0; !st && superlist[i]; i++) { | |
313 | st = superlist[i]->match_metadata_desc(name); | |
314 | if (do_default_layout) | |
315 | layout = default_layout(st, level, verbose); | |
316 | if (st && !st->ss->validate_geometry | |
317 | (st, level, layout, raiddisks, | |
318 | chunk, size*2, dname, &freesize, | |
319 | verbose > 0)) | |
320 | st = NULL; | |
321 | } | |
322 | ||
323 | if (!st) { | |
324 | fprintf(stderr, Name ": device %s not suitable " | |
325 | "for any style of array\n", | |
326 | dname); | |
327 | exit(2); | |
328 | } | |
329 | if (st->ss != &super0 || | |
330 | st->minor_version != 90) | |
331 | did_default = 1; | |
332 | } else { | |
333 | if (do_default_layout) | |
334 | layout = default_layout(st, level, verbose); | |
335 | if (!st->ss->validate_geometry(st, level, layout, | |
336 | raiddisks, | |
337 | chunk, size*2, dname, | |
338 | &freesize, | |
339 | verbose >= 0)) { | |
340 | ||
341 | fprintf(stderr, | |
342 | Name ": %s is not suitable for " | |
343 | "this array.\n", | |
344 | dname); | |
345 | fail = 1; | |
346 | continue; | |
347 | } | |
348 | } | |
349 | ||
350 | freesize /= 2; /* convert to K */ | |
351 | if (chunk) { | |
352 | /* round to chunk size */ | |
353 | freesize = freesize & ~(chunk-1); | |
354 | } | |
355 | ||
356 | if (size && freesize < size) { | |
357 | fprintf(stderr, Name ": %s is smaller than given size." | |
358 | " %lluK < %lluK + metadata\n", | |
359 | dname, freesize, size); | |
360 | fail = 1; | |
361 | continue; | |
362 | } | |
363 | if (maxdisc == NULL || (maxdisc && freesize > maxsize)) { | |
364 | maxdisc = dname; | |
365 | maxsize = freesize; | |
366 | } | |
367 | if (mindisc ==NULL || (mindisc && freesize < minsize)) { | |
368 | mindisc = dname; | |
369 | minsize = freesize; | |
370 | } | |
371 | if (runstop != 1 || verbose >= 0) { | |
372 | int fd = open(dname, O_RDONLY); | |
373 | if (fd <0 ) { | |
374 | fprintf(stderr, Name ": Cannot open %s: %s\n", | |
375 | dname, strerror(errno)); | |
376 | fail=1; | |
377 | continue; | |
378 | } | |
379 | warn |= check_ext2(fd, dname); | |
380 | warn |= check_reiser(fd, dname); | |
381 | warn |= check_raid(fd, dname); | |
382 | if (strcmp(st->ss->name, "1.x") == 0 && | |
383 | st->minor_version >= 1) | |
384 | /* metadata at front */ | |
385 | warn |= check_partitions(fd, dname, 0); | |
386 | else if (level == 1 || level == LEVEL_CONTAINER) | |
387 | /* partitions could be meaningful */ | |
388 | warn |= check_partitions(fd, dname, freesize*2); | |
389 | else | |
390 | /* partitions cannot be meaningful */ | |
391 | warn |= check_partitions(fd, dname, 0); | |
392 | if (strcmp(st->ss->name, "1.x") == 0 && | |
393 | st->minor_version >= 1 && | |
394 | did_default && | |
395 | level == 1 && | |
396 | (warn & 1024) == 0) { | |
397 | warn |= 1024; | |
398 | fprintf(stderr, Name ": Note: this array has metadata at the start and\n" | |
399 | " may not be suitable as a boot device. If you plan to\n" | |
400 | " store '/boot' on this device please ensure that\n" | |
401 | " your boot-loader understands md/v1.x metadata, or use\n" | |
402 | " --metadata=0.90\n"); | |
403 | } | |
404 | close(fd); | |
405 | } | |
406 | } | |
407 | if (have_container) | |
408 | info.array.working_disks = raiddisks; | |
409 | if (fail) { | |
410 | fprintf(stderr, Name ": create aborted\n"); | |
411 | return 1; | |
412 | } | |
413 | if (size == 0) { | |
414 | if (mindisc == NULL && !have_container) { | |
415 | fprintf(stderr, Name ": no size and no drives given - aborting create.\n"); | |
416 | return 1; | |
417 | } | |
418 | if (level > 0 || level == LEVEL_MULTIPATH | |
419 | || level == LEVEL_FAULTY | |
420 | || st->ss->external ) { | |
421 | /* size is meaningful */ | |
422 | if (!st->ss->validate_geometry(st, level, layout, | |
423 | raiddisks, | |
424 | chunk, minsize*2, | |
425 | NULL, NULL, 0)) { | |
426 | fprintf(stderr, Name ": devices too large for RAID level %d\n", level); | |
427 | return 1; | |
428 | } | |
429 | size = minsize; | |
430 | if (verbose > 0) | |
431 | fprintf(stderr, Name ": size set to %lluK\n", size); | |
432 | } | |
433 | } | |
434 | if (!have_container && level > 0 && ((maxsize-size)*100 > maxsize)) { | |
435 | if (runstop != 1 || verbose >= 0) | |
436 | fprintf(stderr, Name ": largest drive (%s) exceeds size (%lluK) by more than 1%%\n", | |
437 | maxdisc, size); | |
438 | warn = 1; | |
439 | } | |
440 | ||
441 | if (st->ss->detail_platform && st->ss->detail_platform(0, 1) != 0) { | |
442 | if (runstop != 1 || verbose >= 0) | |
443 | fprintf(stderr, Name ": %s unable to enumerate platform support\n" | |
444 | " array may not be compatible with hardware/firmware\n", | |
445 | st->ss->name); | |
446 | warn = 1; | |
447 | } | |
448 | ||
449 | if (warn) { | |
450 | if (runstop!= 1) { | |
451 | if (!ask("Continue creating array? ")) { | |
452 | fprintf(stderr, Name ": create aborted.\n"); | |
453 | return 1; | |
454 | } | |
455 | } else { | |
456 | if (verbose > 0) | |
457 | fprintf(stderr, Name ": creation continuing despite oddities due to --run\n"); | |
458 | } | |
459 | } | |
460 | ||
461 | /* If this is raid4/5, we want to configure the last active slot | |
462 | * as missing, so that a reconstruct happens (faster than re-parity) | |
463 | * FIX: Can we do this for raid6 as well? | |
464 | */ | |
465 | if (st->ss->external == 0 && | |
466 | assume_clean==0 && force == 0 && first_missing >= raiddisks) { | |
467 | switch ( level ) { | |
468 | case 4: | |
469 | case 5: | |
470 | insert_point = raiddisks-1; | |
471 | sparedisks++; | |
472 | info.array.active_disks--; | |
473 | missing_disks++; | |
474 | break; | |
475 | default: | |
476 | break; | |
477 | } | |
478 | } | |
479 | /* For raid6, if creating with 1 missing drive, make a good drive | |
480 | * into a spare, else the create will fail | |
481 | */ | |
482 | if (assume_clean == 0 && force == 0 && first_missing < raiddisks && | |
483 | st->ss->external == 0 && | |
484 | second_missing >= raiddisks && level == 6) { | |
485 | insert_point = raiddisks - 1; | |
486 | if (insert_point == first_missing) | |
487 | insert_point--; | |
488 | sparedisks ++; | |
489 | info.array.active_disks--; | |
490 | missing_disks++; | |
491 | } | |
492 | ||
493 | if (level <= 0 && first_missing < subdevs * 2) { | |
494 | fprintf(stderr, | |
495 | Name ": This level does not support missing devices\n"); | |
496 | return 1; | |
497 | } | |
498 | ||
499 | /* We need to create the device */ | |
500 | map_lock(&map); | |
501 | mdfd = create_mddev(mddev, name, autof, LOCAL, chosen_name); | |
502 | if (mdfd < 0) | |
503 | return 1; | |
504 | mddev = chosen_name; | |
505 | ||
506 | vers = md_get_version(mdfd); | |
507 | if (vers < 9000) { | |
508 | fprintf(stderr, Name ": Create requires md driver version 0.90.0 or later\n"); | |
509 | goto abort; | |
510 | } else { | |
511 | mdu_array_info_t inf; | |
512 | memset(&inf, 0, sizeof(inf)); | |
513 | ioctl(mdfd, GET_ARRAY_INFO, &inf); | |
514 | if (inf.working_disks != 0) { | |
515 | fprintf(stderr, Name ": another array by this name" | |
516 | " is already running.\n"); | |
517 | goto abort; | |
518 | } | |
519 | } | |
520 | ||
521 | /* Ok, lets try some ioctls */ | |
522 | ||
523 | info.array.level = level; | |
524 | info.array.size = size; | |
525 | info.array.raid_disks = raiddisks; | |
526 | /* The kernel should *know* what md_minor we are dealing | |
527 | * with, but it chooses to trust me instead. Sigh | |
528 | */ | |
529 | info.array.md_minor = 0; | |
530 | if (fstat(mdfd, &stb)==0) | |
531 | info.array.md_minor = minor(stb.st_rdev); | |
532 | info.array.not_persistent = 0; | |
533 | ||
534 | if ( ( (level == 4 || level == 5) && | |
535 | (insert_point < raiddisks || first_missing < raiddisks) ) | |
536 | || | |
537 | ( level == 6 && (insert_point < raiddisks | |
538 | || second_missing < raiddisks)) | |
539 | || | |
540 | ( level <= 0 ) | |
541 | || | |
542 | assume_clean | |
543 | ) { | |
544 | info.array.state = 1; /* clean, but one+ drive will be missing*/ | |
545 | info.resync_start = MaxSector; | |
546 | } else { | |
547 | info.array.state = 0; /* not clean, but no errors */ | |
548 | info.resync_start = 0; | |
549 | } | |
550 | if (level == 10) { | |
551 | /* for raid10, the bitmap size is the capacity of the array, | |
552 | * which is array.size * raid_disks / ncopies; | |
553 | * .. but convert to sectors. | |
554 | */ | |
555 | int ncopies = ((layout>>8) & 255) * (layout & 255); | |
556 | bitmapsize = (unsigned long long)size * raiddisks / ncopies * 2; | |
557 | /* printf("bms=%llu as=%d rd=%d nc=%d\n", bitmapsize, size, raiddisks, ncopies);*/ | |
558 | } else | |
559 | bitmapsize = (unsigned long long)size * 2; | |
560 | ||
561 | /* There is lots of redundancy in these disk counts, | |
562 | * raid_disks is the most meaningful value | |
563 | * it describes the geometry of the array | |
564 | * it is constant | |
565 | * nr_disks is total number of used slots. | |
566 | * it should be raid_disks+spare_disks | |
567 | * spare_disks is the number of extra disks present | |
568 | * see above | |
569 | * active_disks is the number of working disks in | |
570 | * active slots. (With raid_disks) | |
571 | * working_disks is the total number of working disks, | |
572 | * including spares | |
573 | * failed_disks is the number of disks marked failed | |
574 | * | |
575 | * Ideally, the kernel would keep these (except raid_disks) | |
576 | * up-to-date as we ADD_NEW_DISK, but it doesn't (yet). | |
577 | * So for now, we assume that all raid and spare | |
578 | * devices will be given. | |
579 | */ | |
580 | info.array.spare_disks=sparedisks; | |
581 | info.array.failed_disks=missing_disks; | |
582 | info.array.nr_disks = info.array.working_disks | |
583 | + info.array.failed_disks; | |
584 | info.array.layout = layout; | |
585 | info.array.chunk_size = chunk*1024; | |
586 | ||
587 | if (name == NULL || *name == 0) { | |
588 | /* base name on mddev */ | |
589 | /* /dev/md0 -> 0 | |
590 | * /dev/md_d0 -> d0 | |
591 | * /dev/md/1 -> 1 | |
592 | * /dev/md/d1 -> d1 | |
593 | * /dev/md/home -> home | |
594 | * /dev/mdhome -> home | |
595 | */ | |
596 | /* FIXME compare this with rules in create_mddev */ | |
597 | name = strrchr(mddev, '/'); | |
598 | if (name) { | |
599 | name++; | |
600 | if (strncmp(name, "md_d", 4)==0 && | |
601 | strlen(name) > 4 && | |
602 | isdigit(name[4]) && | |
603 | (name-mddev) == 5 /* /dev/ */) | |
604 | name += 3; | |
605 | else if (strncmp(name, "md", 2)==0 && | |
606 | strlen(name) > 2 && | |
607 | isdigit(name[2]) && | |
608 | (name-mddev) == 5 /* /dev/ */) | |
609 | name += 2; | |
610 | } | |
611 | } | |
612 | if (!st->ss->init_super(st, &info.array, size, name, homehost, uuid)) | |
613 | goto abort; | |
614 | ||
615 | total_slots = info.array.nr_disks; | |
616 | sysfs_init(&info, mdfd, 0); | |
617 | st->ss->getinfo_super(st, &info, NULL); | |
618 | ||
619 | if (did_default && verbose >= 0) { | |
620 | if (is_subarray(info.text_version)) { | |
621 | int dnum = devname2devnum(info.text_version+1); | |
622 | char *path; | |
623 | int mdp = get_mdp_major(); | |
624 | struct mdinfo *mdi; | |
625 | if (dnum > 0) | |
626 | path = map_dev(MD_MAJOR, dnum, 1); | |
627 | else | |
628 | path = map_dev(mdp, (-1-dnum)<< 6, 1); | |
629 | ||
630 | mdi = sysfs_read(-1, dnum, GET_VERSION); | |
631 | ||
632 | fprintf(stderr, Name ": Creating array inside " | |
633 | "%s container %s\n", | |
634 | mdi?mdi->text_version:"managed", path); | |
635 | sysfs_free(mdi); | |
636 | } else | |
637 | fprintf(stderr, Name ": Defaulting to version" | |
638 | " %s metadata\n", info.text_version); | |
639 | } | |
640 | ||
641 | map_update(&map, fd2devnum(mdfd), info.text_version, | |
642 | info.uuid, chosen_name); | |
643 | map_unlock(&map); | |
644 | ||
645 | if (bitmap_file && vers < 9003) { | |
646 | major_num = BITMAP_MAJOR_HOSTENDIAN; | |
647 | #ifdef __BIG_ENDIAN | |
648 | fprintf(stderr, Name ": Warning - bitmaps created on this kernel are not portable\n" | |
649 | " between different architectured. Consider upgrading the Linux kernel.\n"); | |
650 | #endif | |
651 | } | |
652 | ||
653 | if (bitmap_file && strcmp(bitmap_file, "internal")==0) { | |
654 | if ((vers%100) < 2) { | |
655 | fprintf(stderr, Name ": internal bitmaps not supported by this kernel.\n"); | |
656 | goto abort; | |
657 | } | |
658 | if (!st->ss->add_internal_bitmap) { | |
659 | fprintf(stderr, Name ": internal bitmaps not supported with %s metadata\n", | |
660 | st->ss->name); | |
661 | goto abort; | |
662 | } | |
663 | if (!st->ss->add_internal_bitmap(st, &bitmap_chunk, | |
664 | delay, write_behind, | |
665 | bitmapsize, 1, major_num)) { | |
666 | fprintf(stderr, Name ": Given bitmap chunk size not supported.\n"); | |
667 | goto abort; | |
668 | } | |
669 | bitmap_file = NULL; | |
670 | } | |
671 | ||
672 | ||
673 | sysfs_init(&info, mdfd, 0); | |
674 | ||
675 | if (st->ss->external && st->container_dev != NoMdDev) { | |
676 | /* member */ | |
677 | ||
678 | /* When creating a member, we need to be careful | |
679 | * to negotiate with mdmon properly. | |
680 | * If it is already running, we cannot write to | |
681 | * the devices and must ask it to do that part. | |
682 | * If it isn't running, we write to the devices, | |
683 | * and then start it. | |
684 | * We hold an exclusive open on the container | |
685 | * device to make sure mdmon doesn't exit after | |
686 | * we checked that it is running. | |
687 | * | |
688 | * For now, fail if it is already running. | |
689 | */ | |
690 | container_fd = open_dev_excl(st->container_dev); | |
691 | if (container_fd < 0) { | |
692 | fprintf(stderr, Name ": Cannot get exclusive " | |
693 | "open on container - weird.\n"); | |
694 | goto abort; | |
695 | } | |
696 | if (mdmon_running(st->container_dev)) { | |
697 | if (verbose) | |
698 | fprintf(stderr, Name ": reusing mdmon " | |
699 | "for %s.\n", | |
700 | devnum2devname(st->container_dev)); | |
701 | st->update_tail = &st->updates; | |
702 | } else | |
703 | need_mdmon = 1; | |
704 | } | |
705 | rv = set_array_info(mdfd, st, &info); | |
706 | if (rv) { | |
707 | fprintf(stderr, Name ": failed to set array info for %s: %s\n", | |
708 | mddev, strerror(errno)); | |
709 | goto abort; | |
710 | } | |
711 | ||
712 | if (bitmap_file) { | |
713 | int uuid[4]; | |
714 | ||
715 | st->ss->uuid_from_super(st, uuid); | |
716 | if (CreateBitmap(bitmap_file, force, (char*)uuid, bitmap_chunk, | |
717 | delay, write_behind, | |
718 | bitmapsize, | |
719 | major_num)) { | |
720 | goto abort; | |
721 | } | |
722 | bitmap_fd = open(bitmap_file, O_RDWR); | |
723 | if (bitmap_fd < 0) { | |
724 | fprintf(stderr, Name ": weird: %s cannot be openned\n", | |
725 | bitmap_file); | |
726 | goto abort; | |
727 | } | |
728 | if (ioctl(mdfd, SET_BITMAP_FILE, bitmap_fd) < 0) { | |
729 | fprintf(stderr, Name ": Cannot set bitmap file for %s: %s\n", | |
730 | mddev, strerror(errno)); | |
731 | goto abort; | |
732 | } | |
733 | } | |
734 | ||
735 | infos = malloc(sizeof(*infos) * total_slots); | |
736 | ||
737 | for (pass=1; pass <=2 ; pass++) { | |
738 | mddev_dev_t moved_disk = NULL; /* the disk that was moved out of the insert point */ | |
739 | ||
740 | for (dnum=0, dv = devlist ; dv ; | |
741 | dv=(dv->next)?(dv->next):moved_disk, dnum++) { | |
742 | int fd; | |
743 | struct stat stb; | |
744 | struct mdinfo *inf = &infos[dnum]; | |
745 | ||
746 | if (dnum >= total_slots) | |
747 | abort(); | |
748 | if (dnum == insert_point) { | |
749 | moved_disk = dv; | |
750 | continue; | |
751 | } | |
752 | if (strcasecmp(dv->devname, "missing")==0) | |
753 | continue; | |
754 | if (have_container) | |
755 | moved_disk = NULL; | |
756 | if (have_container && dnum < info.array.raid_disks - 1) | |
757 | /* repeatedly use the container */ | |
758 | moved_disk = dv; | |
759 | ||
760 | switch(pass) { | |
761 | case 1: | |
762 | *inf = info; | |
763 | ||
764 | inf->disk.number = dnum; | |
765 | inf->disk.raid_disk = dnum; | |
766 | if (inf->disk.raid_disk < raiddisks) | |
767 | inf->disk.state = (1<<MD_DISK_ACTIVE) | | |
768 | (1<<MD_DISK_SYNC); | |
769 | else | |
770 | inf->disk.state = 0; | |
771 | ||
772 | if (dv->writemostly == 1) | |
773 | inf->disk.state |= (1<<MD_DISK_WRITEMOSTLY); | |
774 | ||
775 | if (have_container) | |
776 | fd = -1; | |
777 | else { | |
778 | if (st->ss->external && | |
779 | st->container_dev != NoMdDev) | |
780 | fd = open(dv->devname, O_RDWR); | |
781 | else | |
782 | fd = open(dv->devname, O_RDWR|O_EXCL); | |
783 | ||
784 | if (fd < 0) { | |
785 | fprintf(stderr, Name ": failed to open %s " | |
786 | "after earlier success - aborting\n", | |
787 | dv->devname); | |
788 | goto abort; | |
789 | } | |
790 | fstat(fd, &stb); | |
791 | inf->disk.major = major(stb.st_rdev); | |
792 | inf->disk.minor = minor(stb.st_rdev); | |
793 | } | |
794 | if (fd >= 0) | |
795 | remove_partitions(fd); | |
796 | if (st->ss->add_to_super(st, &inf->disk, | |
797 | fd, dv->devname)) { | |
798 | ioctl(mdfd, STOP_ARRAY, NULL); | |
799 | goto abort; | |
800 | } | |
801 | st->ss->getinfo_super(st, inf, NULL); | |
802 | safe_mode_delay = inf->safe_mode_delay; | |
803 | ||
804 | if (have_container && verbose > 0) | |
805 | fprintf(stderr, Name ": Using %s for device %d\n", | |
806 | map_dev(inf->disk.major, | |
807 | inf->disk.minor, | |
808 | 0), dnum); | |
809 | ||
810 | if (!have_container) { | |
811 | /* getinfo_super might have lost these ... */ | |
812 | inf->disk.major = major(stb.st_rdev); | |
813 | inf->disk.minor = minor(stb.st_rdev); | |
814 | } | |
815 | break; | |
816 | case 2: | |
817 | inf->errors = 0; | |
818 | rv = 0; | |
819 | ||
820 | rv = add_disk(mdfd, st, &info, inf); | |
821 | ||
822 | if (rv) { | |
823 | fprintf(stderr, | |
824 | Name ": ADD_NEW_DISK for %s " | |
825 | "failed: %s\n", | |
826 | dv->devname, strerror(errno)); | |
827 | st->ss->free_super(st); | |
828 | goto abort; | |
829 | } | |
830 | break; | |
831 | } | |
832 | if (!have_container && | |
833 | dv == moved_disk && dnum != insert_point) break; | |
834 | } | |
835 | if (pass == 1) { | |
836 | struct mdinfo info_new; | |
837 | struct map_ent *me = NULL; | |
838 | ||
839 | /* check to see if the uuid has changed due to these | |
840 | * metadata changes, and if so update the member array | |
841 | * and container uuid. Note ->write_init_super clears | |
842 | * the subarray cursor such that ->getinfo_super once | |
843 | * again returns container info. | |
844 | */ | |
845 | map_lock(&map); | |
846 | st->ss->getinfo_super(st, &info_new, NULL); | |
847 | if (st->ss->external && level != LEVEL_CONTAINER && | |
848 | !same_uuid(info_new.uuid, info.uuid, 0)) { | |
849 | map_update(&map, fd2devnum(mdfd), | |
850 | info_new.text_version, | |
851 | info_new.uuid, chosen_name); | |
852 | me = map_by_devnum(&map, st->container_dev); | |
853 | } | |
854 | ||
855 | st->ss->write_init_super(st); | |
856 | ||
857 | /* update parent container uuid */ | |
858 | if (me) { | |
859 | char *path = strdup(me->path); | |
860 | ||
861 | st->ss->getinfo_super(st, &info_new, NULL); | |
862 | map_update(&map, st->container_dev, | |
863 | info_new.text_version, | |
864 | info_new.uuid, path); | |
865 | free(path); | |
866 | } | |
867 | map_unlock(&map); | |
868 | ||
869 | flush_metadata_updates(st); | |
870 | } | |
871 | } | |
872 | free(infos); | |
873 | st->ss->free_super(st); | |
874 | ||
875 | if (level == LEVEL_CONTAINER) { | |
876 | /* No need to start. But we should signal udev to | |
877 | * create links */ | |
878 | sysfs_uevent(&info, "change"); | |
879 | if (verbose >= 0) | |
880 | fprintf(stderr, Name ": container %s prepared.\n", mddev); | |
881 | wait_for(chosen_name, mdfd); | |
882 | } else if (runstop == 1 || subdevs >= raiddisks) { | |
883 | if (st->ss->external) { | |
884 | switch(level) { | |
885 | case LEVEL_LINEAR: | |
886 | case LEVEL_MULTIPATH: | |
887 | case 0: | |
888 | sysfs_set_str(&info, NULL, "array_state", | |
889 | "active"); | |
890 | need_mdmon = 0; | |
891 | break; | |
892 | default: | |
893 | sysfs_set_str(&info, NULL, "array_state", | |
894 | "readonly"); | |
895 | break; | |
896 | } | |
897 | sysfs_set_safemode(&info, safe_mode_delay); | |
898 | } else { | |
899 | /* param is not actually used */ | |
900 | mdu_param_t param; | |
901 | if (ioctl(mdfd, RUN_ARRAY, ¶m)) { | |
902 | fprintf(stderr, Name ": RUN_ARRAY failed: %s\n", | |
903 | strerror(errno)); | |
904 | ioctl(mdfd, STOP_ARRAY, NULL); | |
905 | goto abort; | |
906 | } | |
907 | } | |
908 | if (verbose >= 0) | |
909 | fprintf(stderr, Name ": array %s started.\n", mddev); | |
910 | if (st->ss->external && st->container_dev != NoMdDev) { | |
911 | if (need_mdmon) | |
912 | start_mdmon(st->container_dev); | |
913 | ||
914 | ping_monitor(devnum2devname(st->container_dev)); | |
915 | close(container_fd); | |
916 | } | |
917 | wait_for(chosen_name, mdfd); | |
918 | } else { | |
919 | fprintf(stderr, Name ": not starting array - not enough devices.\n"); | |
920 | } | |
921 | close(mdfd); | |
922 | return 0; | |
923 | ||
924 | abort: | |
925 | map_lock(&map); | |
926 | map_remove(&map, fd2devnum(mdfd)); | |
927 | map_unlock(&map); | |
928 | ||
929 | if (mdfd >= 0) | |
930 | close(mdfd); | |
931 | return 1; | |
932 | } |