]>
Commit | Line | Data |
---|---|---|
64c4757e | 1 | /* |
9a9dab36 | 2 | * mdadm - manage Linux "md" devices aka RAID arrays. |
64c4757e | 3 | * |
cd29a5c8 | 4 | * Copyright (C) 2001-2002 Neil Brown <neilb@cse.unsw.edu.au> |
64c4757e NB |
5 | * |
6 | * | |
7 | * This program is free software; you can redistribute it and/or modify | |
8 | * it under the terms of the GNU General Public License as published by | |
9 | * the Free Software Foundation; either version 2 of the License, or | |
10 | * (at your option) any later version. | |
11 | * | |
12 | * This program is distributed in the hope that it will be useful, | |
13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
15 | * GNU General Public License for more details. | |
16 | * | |
17 | * You should have received a copy of the GNU General Public License | |
18 | * along with this program; if not, write to the Free Software | |
19 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA | |
20 | * | |
21 | * Author: Neil Brown | |
22 | * Email: <neilb@cse.unsw.edu.au> | |
23 | * Paper: Neil Brown | |
24 | * School of Computer Science and Engineering | |
25 | * The University of New South Wales | |
26 | * Sydney, 2052 | |
27 | * Australia | |
28 | */ | |
29 | ||
9a9dab36 | 30 | #include "mdadm.h" |
682c7051 NB |
31 | #include "md_u.h" |
32 | #include "md_p.h" | |
64c4757e | 33 | |
82d9eba6 | 34 | int Create(struct supertype *st, char *mddev, int mdfd, |
aa88f531 | 35 | int chunk, int level, int layout, unsigned long size, int raiddisks, int sparedisks, |
cd29a5c8 | 36 | int subdevs, mddev_dev_t devlist, |
c82f047c NB |
37 | int runstop, int verbose, int force, |
38 | char *bitmap_file, int bitmap_chunk, int delay) | |
64c4757e | 39 | { |
682c7051 NB |
40 | /* |
41 | * Create a new raid array. | |
42 | * | |
43 | * First check that necessary details are available | |
44 | * (i.e. level, raid-disks) | |
45 | * | |
46 | * Then check each disk to see what might be on it | |
47 | * and report anything interesting. | |
48 | * | |
49 | * If anything looks odd, and runstop not set, | |
50 | * abort. | |
51 | * | |
52 | * SET_ARRAY_INFO and ADD_NEW_DISK, and | |
82d9eba6 | 53 | * if runstop==run, or raiddisks disks were used, |
682c7051 NB |
54 | * RUN_ARRAY |
55 | */ | |
b5e64645 | 56 | unsigned long long minsize=0, maxsize=0; |
cd29a5c8 NB |
57 | char *mindisc = NULL; |
58 | char *maxdisc = NULL; | |
c913b90e | 59 | int dnum; |
cd29a5c8 | 60 | mddev_dev_t dv; |
682c7051 | 61 | int fail=0, warn=0; |
82b27616 | 62 | struct stat stb; |
82d9eba6 | 63 | int first_missing = subdevs * 2; |
52826846 | 64 | int missing_disks = 0; |
82d9eba6 | 65 | int insert_point = subdevs * 2; /* where to insert a missing drive */ |
4b1ac34b NB |
66 | void *super; |
67 | int pass; | |
82d9eba6 NB |
68 | int vers; |
69 | int rv; | |
c82f047c | 70 | int bitmap_fd; |
682c7051 NB |
71 | |
72 | mdu_array_info_t array; | |
682c7051 | 73 | |
82d9eba6 NB |
74 | vers = md_get_version(mdfd); |
75 | if (vers < 9000) { | |
dd0781e5 | 76 | fprintf(stderr, Name ": Create requires md driver version 0.90.0 or later\n"); |
52826846 | 77 | return 1; |
682c7051 | 78 | } |
98c6faba | 79 | if (level == UnSet) { |
682c7051 NB |
80 | fprintf(stderr, |
81 | Name ": a RAID level is needed to create an array.\n"); | |
82 | return 1; | |
83 | } | |
84 | if (raiddisks < 1) { | |
85 | fprintf(stderr, | |
b83d95f3 | 86 | Name ": a number of --raid-devices must be given to create an array\n"); |
682c7051 NB |
87 | return 1; |
88 | } | |
98c6faba NB |
89 | if (raiddisks < 4 && level == 6) { |
90 | fprintf(stderr, | |
91 | Name ": at least 4 raid-devices needed for level 6\n"); | |
92 | return 1; | |
93 | } | |
94 | if (raiddisks > 256 && level == 6) { | |
95 | fprintf(stderr, | |
96 | Name ": no more than 256 raid-devices supported for level 6\n"); | |
97 | return 1; | |
98 | } | |
52826846 NB |
99 | if (raiddisks < 2 && level >= 4) { |
100 | fprintf(stderr, | |
98c6faba | 101 | Name ": at least 2 raid-devices needed for level 4 or 5\n"); |
52826846 NB |
102 | return 1; |
103 | } | |
682c7051 | 104 | if (subdevs > raiddisks+sparedisks) { |
b83d95f3 | 105 | fprintf(stderr, Name ": You have listed more devices (%d) than are in the array(%d)!\n", subdevs, raiddisks+sparedisks); |
52826846 | 106 | return 1; |
682c7051 | 107 | } |
cd29a5c8 | 108 | if (subdevs < raiddisks+sparedisks) { |
52826846 NB |
109 | fprintf(stderr, Name ": You haven't given enough devices (real or missing) to create this array\n"); |
110 | return 1; | |
111 | } | |
112 | ||
682c7051 | 113 | /* now set some defaults */ |
98c6faba | 114 | if (layout == UnSet) |
682c7051 NB |
115 | switch(level) { |
116 | default: /* no layout */ | |
117 | layout = 0; | |
118 | break; | |
e5329c37 NB |
119 | case 10: |
120 | layout = 0x102; /* near=2, far=1 */ | |
121 | if (verbose) | |
122 | fprintf(stderr, | |
123 | Name ": layout defaults to n1\n"); | |
124 | break; | |
682c7051 | 125 | case 5: |
98c6faba | 126 | case 6: |
682c7051 NB |
127 | layout = map_name(r5layout, "default"); |
128 | if (verbose) | |
129 | fprintf(stderr, | |
130 | Name ": layout defaults to %s\n", map_num(r5layout, layout)); | |
131 | break; | |
b5e64645 NB |
132 | case LEVEL_FAULTY: |
133 | layout = map_name(faultylayout, "default"); | |
134 | ||
135 | if (verbose) | |
136 | fprintf(stderr, | |
137 | Name ": layout defaults to %s\n", map_num(faultylayout, layout)); | |
138 | break; | |
682c7051 NB |
139 | } |
140 | ||
e5329c37 NB |
141 | if (level == 10) |
142 | /* check layout fits in array*/ | |
143 | if ((layout&255) * ((layout>>8)&255) > raiddisks) { | |
144 | fprintf(stderr, Name ": that layout requires at least %d devices\n", | |
145 | (layout&255) * ((layout>>8)&255)); | |
146 | return 1; | |
147 | } | |
148 | ||
aa88f531 NB |
149 | switch(level) { |
150 | case 4: | |
151 | case 5: | |
e5329c37 | 152 | case 10: |
98c6faba | 153 | case 6: |
aa88f531 NB |
154 | case 0: |
155 | case -1: /* linear */ | |
156 | if (chunk == 0) { | |
157 | chunk = 64; | |
158 | if (verbose) | |
159 | fprintf(stderr, Name ": chunk size defaults to 64K\n"); | |
160 | } | |
161 | break; | |
162 | default: /* raid1, multipath */ | |
163 | if (chunk) { | |
164 | chunk = 0; | |
165 | if (verbose) | |
166 | fprintf(stderr, Name ": chunk size ignored for this level\n"); | |
167 | } | |
168 | break; | |
682c7051 NB |
169 | } |
170 | ||
171 | /* now look at the subdevs */ | |
52826846 NB |
172 | array.active_disks = 0; |
173 | array.working_disks = 0; | |
c913b90e NB |
174 | dnum = 0; |
175 | for (dv=devlist; dv; dv=dv->next, dnum++) { | |
cd29a5c8 | 176 | char *dname = dv->devname; |
b5e64645 NB |
177 | unsigned long dsize; |
178 | unsigned long long ldsize, freesize; | |
52826846 | 179 | int fd; |
cd29a5c8 | 180 | if (strcasecmp(dname, "missing")==0) { |
c913b90e NB |
181 | if (first_missing > dnum) |
182 | first_missing = dnum; | |
52826846 NB |
183 | missing_disks ++; |
184 | continue; | |
185 | } | |
186 | array.working_disks++; | |
c913b90e | 187 | if (dnum < raiddisks) |
52826846 | 188 | array.active_disks++; |
d7eaf49f | 189 | fd = open(dname, O_RDONLY|O_EXCL, 0); |
682c7051 NB |
190 | if (fd <0 ) { |
191 | fprintf(stderr, Name ": Cannot open %s: %s\n", | |
192 | dname, strerror(errno)); | |
193 | fail=1; | |
194 | continue; | |
195 | } | |
b5e64645 NB |
196 | #ifdef BLKGETSIZE64 |
197 | if (ioctl(fd, BLKGETSIZE64, &ldsize)==0) | |
198 | ; | |
199 | else | |
200 | #endif | |
682c7051 NB |
201 | if (ioctl(fd, BLKGETSIZE, &dsize)) { |
202 | fprintf(stderr, Name ": Cannot get size of %s: %s\n", | |
203 | dname, strerror(errno)); | |
204 | fail = 1; | |
205 | close(fd); | |
206 | continue; | |
207 | } | |
b5e64645 NB |
208 | else { |
209 | ldsize = dsize; | |
570c0542 | 210 | ldsize <<= 9; |
b5e64645 | 211 | } |
570c0542 | 212 | freesize = st->ss->avail_size(ldsize >> 9); |
82d9eba6 | 213 | if (freesize == 0) { |
aa88f531 | 214 | fprintf(stderr, Name ": %s is too small: %luK\n", |
b5e64645 | 215 | dname, (unsigned long)(ldsize>>10)); |
52826846 NB |
216 | fail = 1; |
217 | close(fd); | |
218 | continue; | |
682c7051 | 219 | } |
82d9eba6 NB |
220 | |
221 | freesize /= 2; /* convert to K */ | |
682c7051 NB |
222 | |
223 | if (size && freesize < size) { | |
52826846 | 224 | fprintf(stderr, Name ": %s is smaller that given size." |
b5e64645 | 225 | " %lluK < %luK + superblock\n", dname, freesize, size); |
52826846 NB |
226 | fail = 1; |
227 | close(fd); | |
228 | continue; | |
682c7051 | 229 | } |
cd29a5c8 NB |
230 | if (maxdisc == NULL || (maxdisc && freesize > maxsize)) { |
231 | maxdisc = dname; | |
52826846 | 232 | maxsize = freesize; |
682c7051 | 233 | } |
cd29a5c8 NB |
234 | if (mindisc ==NULL || (mindisc && freesize < minsize)) { |
235 | mindisc = dname; | |
52826846 | 236 | minsize = freesize; |
682c7051 NB |
237 | } |
238 | warn |= check_ext2(fd, dname); | |
239 | warn |= check_reiser(fd, dname); | |
240 | warn |= check_raid(fd, dname); | |
241 | close(fd); | |
242 | } | |
243 | if (fail) { | |
52826846 NB |
244 | fprintf(stderr, Name ": create aborted\n"); |
245 | return 1; | |
682c7051 NB |
246 | } |
247 | if (size == 0) { | |
cd29a5c8 | 248 | if (mindisc == NULL) { |
52826846 NB |
249 | fprintf(stderr, Name ": no size and no drives given - aborting create.\n"); |
250 | return 1; | |
251 | } | |
b5e64645 NB |
252 | if (level > 0) { |
253 | /* size is meaningful */ | |
254 | if (minsize > 0x100000000ULL) { | |
255 | fprintf(stderr, Name ": devices too large for RAID level %d\n", level); | |
256 | return 1; | |
257 | } | |
258 | size = minsize; | |
259 | if (verbose) | |
260 | fprintf(stderr, Name ": size set to %luK\n", size); | |
261 | } | |
682c7051 | 262 | } |
b5e64645 | 263 | if (level > 0 && ((maxsize-size)*100 > maxsize)) { |
aa88f531 | 264 | fprintf(stderr, Name ": largest drive (%s) exceed size (%luK) by more than 1%%\n", |
cd29a5c8 | 265 | maxdisc, size); |
52826846 | 266 | warn = 1; |
682c7051 NB |
267 | } |
268 | ||
269 | if (warn) { | |
52826846 NB |
270 | if (runstop!= 1) { |
271 | if (!ask("Continue creating array? ")) { | |
272 | fprintf(stderr, Name ": create aborted.\n"); | |
273 | return 1; | |
274 | } | |
275 | } else { | |
276 | if (verbose) | |
277 | fprintf(stderr, Name ": creation continuing despite oddities due to --run\n"); | |
682c7051 | 278 | } |
682c7051 NB |
279 | } |
280 | ||
52826846 NB |
281 | /* If this is raid5, we want to configure the last active slot |
282 | * as missing, so that a reconstruct happens (faster than re-parity) | |
98c6faba | 283 | * FIX: Can we do this for raid6 as well? |
52826846 | 284 | */ |
98c6faba NB |
285 | if (force == 0 && first_missing >= raiddisks) { |
286 | switch ( level ) { | |
287 | case 5: | |
288 | insert_point = raiddisks-1; | |
289 | sparedisks++; | |
290 | array.active_disks--; | |
291 | missing_disks++; | |
292 | break; | |
293 | default: | |
294 | break; | |
295 | } | |
52826846 NB |
296 | } |
297 | ||
682c7051 NB |
298 | /* Ok, lets try some ioctls */ |
299 | ||
300 | array.level = level; | |
301 | array.size = size; | |
682c7051 | 302 | array.raid_disks = raiddisks; |
82b27616 NB |
303 | /* The kernel should *know* what md_minor we are dealing |
304 | * with, but it chooses to trust me instead. Sigh | |
305 | */ | |
682c7051 | 306 | array.md_minor = 0; |
82b27616 | 307 | if (fstat(mdfd, &stb)==0) |
0df46c2a | 308 | array.md_minor = minor(stb.st_rdev); |
682c7051 | 309 | array.not_persistent = 0; |
98c6faba | 310 | /*** FIX: Need to do something about RAID-6 here ***/ |
b5e64645 NB |
311 | if ( ( (level == 5) && |
312 | (insert_point < raiddisks || first_missing < raiddisks) ) | |
313 | || | |
314 | ( level == 6 && missing_disks == 2) | |
315 | ) | |
98c6faba | 316 | array.state = 1; /* clean, but one+ drive will be missing */ |
82b27616 | 317 | else |
52826846 | 318 | array.state = 0; /* not clean, but no errors */ |
82b27616 NB |
319 | |
320 | /* There is lots of redundancy in these disk counts, | |
321 | * raid_disks is the most meaningful value | |
322 | * it describes the geometry of the array | |
323 | * it is constant | |
324 | * nr_disks is total number of used slots. | |
325 | * it should be raid_disks+spare_disks | |
326 | * spare_disks is the number of extra disks present | |
327 | * see above | |
328 | * active_disks is the number of working disks in | |
329 | * active slots. (With raid_disks) | |
330 | * working_disks is the total number of working disks, | |
331 | * including spares | |
332 | * failed_disks is the number of disks marked failed | |
333 | * | |
334 | * Ideally, the kernel would keep these (except raid_disks) | |
335 | * up-to-date as we ADD_NEW_DISK, but it doesn't (yet). | |
336 | * So for now, we assume that all raid and spare | |
337 | * devices will be given. | |
338 | */ | |
52826846 NB |
339 | array.spare_disks=sparedisks; |
340 | array.failed_disks=missing_disks; | |
341 | array.nr_disks = array.working_disks + array.failed_disks; | |
682c7051 NB |
342 | array.layout = layout; |
343 | array.chunk_size = chunk*1024; | |
55935d51 | 344 | |
82d9eba6 | 345 | |
96395475 | 346 | if (!st->ss->init_super(st, &super, &array)) |
82d9eba6 | 347 | return 1; |
682c7051 | 348 | |
55935d51 NB |
349 | if (bitmap_file && strcmp(bitmap_file, "internal")==0) { |
350 | if ((vers%100) < 2) { | |
351 | fprintf(stderr, Name ": internal bitmaps not supported by this kernel.\n"); | |
352 | return 1; | |
353 | } | |
354 | if (!st->ss->add_internal_bitmap(super, bitmap_chunk, delay, | |
355 | size ? size : maxsize)) { | |
356 | fprintf(stderr, Name ": Given bitmap chunk size not supported.\n"); | |
357 | return 1; | |
358 | } | |
359 | bitmap_file = NULL; | |
360 | } | |
361 | ||
362 | ||
363 | ||
82d9eba6 NB |
364 | if ((vers % 100) >= 1) { /* can use different versions */ |
365 | mdu_array_info_t inf; | |
366 | memset(&inf, 0, sizeof(inf)); | |
367 | inf.major_version = st->ss->major; | |
368 | inf.minor_version = st->minor_version; | |
369 | rv = ioctl(mdfd, SET_ARRAY_INFO, &inf); | |
370 | } else | |
371 | rv = ioctl(mdfd, SET_ARRAY_INFO, NULL); | |
372 | if (rv) { | |
52826846 NB |
373 | fprintf(stderr, Name ": SET_ARRAY_INFO failed for %s: %s\n", |
374 | mddev, strerror(errno)); | |
375 | return 1; | |
682c7051 | 376 | } |
52826846 | 377 | |
c82f047c NB |
378 | if (bitmap_file) { |
379 | int uuid[4]; | |
55935d51 NB |
380 | |
381 | if (bitmap_chunk == UnSet) | |
382 | bitmap_chunk = DEFAULT_BITMAP_CHUNK; | |
383 | ||
c82f047c NB |
384 | st->ss->uuid_from_super(uuid, super); |
385 | if (CreateBitmap(bitmap_file, force, (char*)uuid, bitmap_chunk, delay, | |
386 | array.size*2ULL /* FIXME wrong for raid10 */)) { | |
387 | return 1; | |
388 | } | |
389 | bitmap_fd = open(bitmap_file, O_RDWR); | |
390 | if (bitmap_fd < 0) { | |
391 | fprintf(stderr, Name ": weird: %s cannot be openned\n", | |
392 | bitmap_file); | |
393 | return 1; | |
394 | } | |
395 | if (ioctl(mdfd, SET_BITMAP_FILE, bitmap_fd) < 0) { | |
396 | fprintf(stderr, Name ": Cannot set bitmap file for %s: %s\n", | |
397 | mddev, strerror(errno)); | |
398 | return 1; | |
399 | } | |
400 | } | |
401 | ||
4b1ac34b NB |
402 | |
403 | ||
404 | for (pass=1; pass <=2 ; pass++) { | |
405 | mddev_dev_t moved_disk = NULL; /* the disk that was moved out of the insert point */ | |
406 | ||
407 | for (dnum=0, dv = devlist ; dv ; | |
408 | dv=(dv->next)?(dv->next):moved_disk, dnum++) { | |
409 | int fd; | |
410 | struct stat stb; | |
411 | mdu_disk_info_t disk; | |
412 | ||
413 | disk.number = dnum; | |
414 | if (dnum == insert_point) { | |
415 | moved_disk = dv; | |
52826846 | 416 | } |
4b1ac34b NB |
417 | disk.raid_disk = disk.number; |
418 | if (disk.raid_disk < raiddisks) | |
419 | disk.state = 6; /* active and in sync */ | |
420 | else | |
421 | disk.state = 0; | |
422 | if (dnum == insert_point || | |
423 | strcasecmp(dv->devname, "missing")==0) { | |
424 | disk.major = 0; | |
425 | disk.minor = 0; | |
426 | disk.state = 1; /* faulty */ | |
427 | } else { | |
428 | fd = open(dv->devname, O_RDONLY|O_EXCL, 0); | |
429 | if (fd < 0) { | |
430 | fprintf(stderr, Name ": failed to open %s after earlier success - aborting\n", | |
431 | dv->devname); | |
432 | return 1; | |
433 | } | |
434 | fstat(fd, &stb); | |
435 | disk.major = major(stb.st_rdev); | |
436 | disk.minor = minor(stb.st_rdev); | |
437 | close(fd); | |
438 | } | |
439 | switch(pass){ | |
440 | case 1: | |
82d9eba6 | 441 | st->ss->add_to_super(super, &disk); |
4b1ac34b NB |
442 | break; |
443 | case 2: | |
eaac7dde | 444 | if (disk.state == 1) break; |
82d9eba6 | 445 | st->ss->write_init_super(st, super, &disk, dv->devname); |
4b1ac34b NB |
446 | |
447 | if (ioctl(mdfd, ADD_NEW_DISK, &disk)) { | |
448 | fprintf(stderr, Name ": ADD_NEW_DISK for %s failed: %s\n", | |
449 | dv->devname, strerror(errno)); | |
450 | free(super); | |
451 | return 1; | |
452 | } | |
453 | ||
454 | break; | |
455 | } | |
456 | if (dv == moved_disk && dnum != insert_point) break; | |
52826846 | 457 | } |
5e52ae9e | 458 | } |
4b1ac34b | 459 | free(super); |
5e52ae9e | 460 | |
682c7051 NB |
461 | /* param is not actually used */ |
462 | if (runstop == 1 || subdevs >= raiddisks) { | |
82b27616 | 463 | mdu_param_t param; |
682c7051 NB |
464 | if (ioctl(mdfd, RUN_ARRAY, ¶m)) { |
465 | fprintf(stderr, Name ": RUN_ARRAY failed: %s\n", | |
466 | strerror(errno)); | |
91f068bf | 467 | Manage_runstop(mddev, mdfd, -1, 0); |
682c7051 NB |
468 | return 1; |
469 | } | |
470 | fprintf(stderr, Name ": array %s started.\n", mddev); | |
471 | } else { | |
b83d95f3 | 472 | fprintf(stderr, Name ": not starting array - not enough devices.\n"); |
682c7051 NB |
473 | } |
474 | return 0; | |
64c4757e | 475 | } |