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