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