]> git.ipfire.org Git - thirdparty/mdadm.git/blame - Create.c
Report device size correctly in --detail for Large devices.
[thirdparty/mdadm.git] / Create.c
CommitLineData
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 34int Create(struct supertype *st, char *mddev, int mdfd,
5dd497ee 35 int chunk, int level, int layout, unsigned long long size, int raiddisks, int sparedisks,
947fd4dd 36 char *name,
cd29a5c8 37 int subdevs, mddev_dev_t devlist,
47d79ef8 38 int runstop, int verbose, int force, int assume_clean,
dfd4d8ee 39 char *bitmap_file, int bitmap_chunk, int write_behind, int delay)
64c4757e 40{
682c7051
NB
41 /*
42 * Create a new raid array.
43 *
44 * First check that necessary details are available
45 * (i.e. level, raid-disks)
46 *
47 * Then check each disk to see what might be on it
48 * and report anything interesting.
49 *
50 * If anything looks odd, and runstop not set,
51 * abort.
52 *
53 * SET_ARRAY_INFO and ADD_NEW_DISK, and
82d9eba6 54 * if runstop==run, or raiddisks disks were used,
682c7051
NB
55 * RUN_ARRAY
56 */
b5e64645 57 unsigned long long minsize=0, maxsize=0;
cd29a5c8
NB
58 char *mindisc = NULL;
59 char *maxdisc = NULL;
c913b90e 60 int dnum;
cd29a5c8 61 mddev_dev_t dv;
682c7051 62 int fail=0, warn=0;
82b27616 63 struct stat stb;
82d9eba6 64 int first_missing = subdevs * 2;
52826846 65 int missing_disks = 0;
82d9eba6 66 int insert_point = subdevs * 2; /* where to insert a missing drive */
4b1ac34b
NB
67 void *super;
68 int pass;
82d9eba6
NB
69 int vers;
70 int rv;
c82f047c 71 int bitmap_fd;
f9c25f1d 72 unsigned long long bitmapsize;
682c7051
NB
73
74 mdu_array_info_t array;
dcec9ee5
NB
75 int major = BITMAP_MAJOR_HI;
76
5dd497ee 77 memset(&array, 0, sizeof(array));
29e766a5 78
82d9eba6
NB
79 vers = md_get_version(mdfd);
80 if (vers < 9000) {
dd0781e5 81 fprintf(stderr, Name ": Create requires md driver version 0.90.0 or later\n");
52826846 82 return 1;
682c7051 83 }
98c6faba 84 if (level == UnSet) {
682c7051
NB
85 fprintf(stderr,
86 Name ": a RAID level is needed to create an array.\n");
87 return 1;
88 }
89 if (raiddisks < 1) {
90 fprintf(stderr,
b83d95f3 91 Name ": a number of --raid-devices must be given to create an array\n");
682c7051
NB
92 return 1;
93 }
98c6faba
NB
94 if (raiddisks < 4 && level == 6) {
95 fprintf(stderr,
96 Name ": at least 4 raid-devices needed for level 6\n");
97 return 1;
98 }
99 if (raiddisks > 256 && level == 6) {
100 fprintf(stderr,
101 Name ": no more than 256 raid-devices supported for level 6\n");
102 return 1;
103 }
52826846
NB
104 if (raiddisks < 2 && level >= 4) {
105 fprintf(stderr,
98c6faba 106 Name ": at least 2 raid-devices needed for level 4 or 5\n");
52826846
NB
107 return 1;
108 }
682c7051 109 if (subdevs > raiddisks+sparedisks) {
b83d95f3 110 fprintf(stderr, Name ": You have listed more devices (%d) than are in the array(%d)!\n", subdevs, raiddisks+sparedisks);
52826846 111 return 1;
682c7051 112 }
cd29a5c8 113 if (subdevs < raiddisks+sparedisks) {
52826846
NB
114 fprintf(stderr, Name ": You haven't given enough devices (real or missing) to create this array\n");
115 return 1;
116 }
117
682c7051 118 /* now set some defaults */
98c6faba 119 if (layout == UnSet)
682c7051
NB
120 switch(level) {
121 default: /* no layout */
122 layout = 0;
123 break;
e5329c37
NB
124 case 10:
125 layout = 0x102; /* near=2, far=1 */
dab6685f 126 if (verbose > 0)
e5329c37
NB
127 fprintf(stderr,
128 Name ": layout defaults to n1\n");
129 break;
682c7051 130 case 5:
98c6faba 131 case 6:
682c7051 132 layout = map_name(r5layout, "default");
dab6685f 133 if (verbose > 0)
682c7051
NB
134 fprintf(stderr,
135 Name ": layout defaults to %s\n", map_num(r5layout, layout));
136 break;
b5e64645
NB
137 case LEVEL_FAULTY:
138 layout = map_name(faultylayout, "default");
139
dab6685f 140 if (verbose > 0)
b5e64645
NB
141 fprintf(stderr,
142 Name ": layout defaults to %s\n", map_num(faultylayout, layout));
143 break;
682c7051
NB
144 }
145
e5329c37
NB
146 if (level == 10)
147 /* check layout fits in array*/
148 if ((layout&255) * ((layout>>8)&255) > raiddisks) {
149 fprintf(stderr, Name ": that layout requires at least %d devices\n",
150 (layout&255) * ((layout>>8)&255));
151 return 1;
152 }
153
aa88f531
NB
154 switch(level) {
155 case 4:
156 case 5:
e5329c37 157 case 10:
98c6faba 158 case 6:
aa88f531
NB
159 case 0:
160 case -1: /* linear */
161 if (chunk == 0) {
162 chunk = 64;
dab6685f 163 if (verbose > 0)
aa88f531
NB
164 fprintf(stderr, Name ": chunk size defaults to 64K\n");
165 }
166 break;
167 default: /* raid1, multipath */
168 if (chunk) {
169 chunk = 0;
dab6685f 170 if (verbose > 0)
aa88f531
NB
171 fprintf(stderr, Name ": chunk size ignored for this level\n");
172 }
173 break;
682c7051
NB
174 }
175
176 /* now look at the subdevs */
52826846
NB
177 array.active_disks = 0;
178 array.working_disks = 0;
c913b90e
NB
179 dnum = 0;
180 for (dv=devlist; dv; dv=dv->next, dnum++) {
cd29a5c8 181 char *dname = dv->devname;
b5e64645
NB
182 unsigned long dsize;
183 unsigned long long ldsize, freesize;
52826846 184 int fd;
cd29a5c8 185 if (strcasecmp(dname, "missing")==0) {
c913b90e
NB
186 if (first_missing > dnum)
187 first_missing = dnum;
52826846
NB
188 missing_disks ++;
189 continue;
190 }
191 array.working_disks++;
c913b90e 192 if (dnum < raiddisks)
52826846 193 array.active_disks++;
d7eaf49f 194 fd = open(dname, O_RDONLY|O_EXCL, 0);
682c7051
NB
195 if (fd <0 ) {
196 fprintf(stderr, Name ": Cannot open %s: %s\n",
197 dname, strerror(errno));
198 fail=1;
199 continue;
200 }
b5e64645
NB
201#ifdef BLKGETSIZE64
202 if (ioctl(fd, BLKGETSIZE64, &ldsize)==0)
203 ;
204 else
205#endif
682c7051
NB
206 if (ioctl(fd, BLKGETSIZE, &dsize)) {
207 fprintf(stderr, Name ": Cannot get size of %s: %s\n",
208 dname, strerror(errno));
209 fail = 1;
210 close(fd);
211 continue;
212 }
b5e64645
NB
213 else {
214 ldsize = dsize;
570c0542 215 ldsize <<= 9;
b5e64645 216 }
1bf4e2d9 217 freesize = st->ss->avail_size(st, ldsize >> 9);
82d9eba6 218 if (freesize == 0) {
aa88f531 219 fprintf(stderr, Name ": %s is too small: %luK\n",
b5e64645 220 dname, (unsigned long)(ldsize>>10));
52826846
NB
221 fail = 1;
222 close(fd);
223 continue;
682c7051 224 }
82d9eba6
NB
225
226 freesize /= 2; /* convert to K */
d2cd3ffc
NB
227 if (chunk) {
228 /* round to chunk size */
229 freesize = freesize & ~(chunk-1);
230 }
682c7051
NB
231
232 if (size && freesize < size) {
52826846 233 fprintf(stderr, Name ": %s is smaller that given size."
5dd497ee 234 " %lluK < %lluK + superblock\n", dname, freesize, size);
52826846
NB
235 fail = 1;
236 close(fd);
237 continue;
682c7051 238 }
cd29a5c8
NB
239 if (maxdisc == NULL || (maxdisc && freesize > maxsize)) {
240 maxdisc = dname;
52826846 241 maxsize = freesize;
682c7051 242 }
cd29a5c8
NB
243 if (mindisc ==NULL || (mindisc && freesize < minsize)) {
244 mindisc = dname;
52826846 245 minsize = freesize;
682c7051 246 }
dab6685f
NB
247 if (runstop != 1 || verbose >= 0) {
248 warn |= check_ext2(fd, dname);
249 warn |= check_reiser(fd, dname);
250 warn |= check_raid(fd, dname);
251 }
682c7051
NB
252 close(fd);
253 }
254 if (fail) {
52826846
NB
255 fprintf(stderr, Name ": create aborted\n");
256 return 1;
682c7051
NB
257 }
258 if (size == 0) {
cd29a5c8 259 if (mindisc == NULL) {
52826846
NB
260 fprintf(stderr, Name ": no size and no drives given - aborting create.\n");
261 return 1;
262 }
c13c45e9 263 if (level > 0 || level == LEVEL_MULTIPATH || level == LEVEL_FAULTY) {
b5e64645 264 /* size is meaningful */
5dd497ee 265 if (minsize > 0x100000000ULL && st->ss->major == 0) {
b5e64645
NB
266 fprintf(stderr, Name ": devices too large for RAID level %d\n", level);
267 return 1;
268 }
269 size = minsize;
dab6685f 270 if (verbose > 0)
5dd497ee 271 fprintf(stderr, Name ": size set to %lluK\n", size);
b5e64645 272 }
682c7051 273 }
b5e64645 274 if (level > 0 && ((maxsize-size)*100 > maxsize)) {
dab6685f 275 if (runstop != 1 || verbose >= 0)
5dd497ee 276 fprintf(stderr, Name ": largest drive (%s) exceed size (%lluK) by more than 1%%\n",
dab6685f 277 maxdisc, size);
52826846 278 warn = 1;
682c7051
NB
279 }
280
281 if (warn) {
52826846
NB
282 if (runstop!= 1) {
283 if (!ask("Continue creating array? ")) {
284 fprintf(stderr, Name ": create aborted.\n");
285 return 1;
286 }
287 } else {
dab6685f 288 if (verbose > 0)
52826846 289 fprintf(stderr, Name ": creation continuing despite oddities due to --run\n");
682c7051 290 }
682c7051
NB
291 }
292
52826846
NB
293 /* If this is raid5, we want to configure the last active slot
294 * as missing, so that a reconstruct happens (faster than re-parity)
98c6faba 295 * FIX: Can we do this for raid6 as well?
52826846 296 */
47d79ef8 297 if (assume_clean==0 && force == 0 && first_missing >= raiddisks) {
98c6faba
NB
298 switch ( level ) {
299 case 5:
300 insert_point = raiddisks-1;
301 sparedisks++;
302 array.active_disks--;
303 missing_disks++;
304 break;
305 default:
306 break;
307 }
52826846
NB
308 }
309
682c7051
NB
310 /* Ok, lets try some ioctls */
311
312 array.level = level;
313 array.size = size;
682c7051 314 array.raid_disks = raiddisks;
82b27616
NB
315 /* The kernel should *know* what md_minor we are dealing
316 * with, but it chooses to trust me instead. Sigh
317 */
682c7051 318 array.md_minor = 0;
82b27616 319 if (fstat(mdfd, &stb)==0)
0df46c2a 320 array.md_minor = minor(stb.st_rdev);
682c7051 321 array.not_persistent = 0;
98c6faba 322 /*** FIX: Need to do something about RAID-6 here ***/
b5e64645
NB
323 if ( ( (level == 5) &&
324 (insert_point < raiddisks || first_missing < raiddisks) )
325 ||
326 ( level == 6 && missing_disks == 2)
47d79ef8
NB
327 ||
328 assume_clean
b5e64645 329 )
98c6faba 330 array.state = 1; /* clean, but one+ drive will be missing */
82b27616 331 else
52826846 332 array.state = 0; /* not clean, but no errors */
82b27616 333
f9c25f1d
NB
334 if (level == 10) {
335 /* for raid10, the bitmap size is the capacity of the array,
336 * which is array.size * raid_disks / ncopies;
337 * .. but convert to sectors.
338 */
339 int ncopies = (layout>>8) * (layout & 255);
5dd497ee
NB
340 bitmapsize = (unsigned long long)size * raiddisks / ncopies * 2;
341/* printf("bms=%llu as=%d rd=%d nc=%d\n", bitmapsize, size, raiddisks, ncopies);*/
f9c25f1d 342 } else
5dd497ee 343 bitmapsize = (unsigned long long)size * 2;
f9c25f1d 344
82b27616
NB
345 /* There is lots of redundancy in these disk counts,
346 * raid_disks is the most meaningful value
347 * it describes the geometry of the array
348 * it is constant
349 * nr_disks is total number of used slots.
350 * it should be raid_disks+spare_disks
351 * spare_disks is the number of extra disks present
352 * see above
353 * active_disks is the number of working disks in
354 * active slots. (With raid_disks)
355 * working_disks is the total number of working disks,
356 * including spares
357 * failed_disks is the number of disks marked failed
358 *
359 * Ideally, the kernel would keep these (except raid_disks)
360 * up-to-date as we ADD_NEW_DISK, but it doesn't (yet).
361 * So for now, we assume that all raid and spare
362 * devices will be given.
363 */
52826846
NB
364 array.spare_disks=sparedisks;
365 array.failed_disks=missing_disks;
366 array.nr_disks = array.working_disks + array.failed_disks;
682c7051
NB
367 array.layout = layout;
368 array.chunk_size = chunk*1024;
838acbc2 369 array.major_version = st->ss->major;
82d9eba6 370
5dd497ee 371 if (!st->ss->init_super(st, &super, &array, size, name))
82d9eba6 372 return 1;
682c7051 373
dcec9ee5
NB
374 if (bitmap_file && vers < 9003) {
375 major = BITMAP_MAJOR_HOSTENDIAN;
376#ifdef __BIG_ENDIAN
377 fprintf(stderr, Name ": Warning - bitmaps created on this kernel are not portable\n"
378 " between different architectured. Consider upgrading the Linux kernel.\n");
379#endif
380 }
381
55935d51
NB
382 if (bitmap_file && strcmp(bitmap_file, "internal")==0) {
383 if ((vers%100) < 2) {
384 fprintf(stderr, Name ": internal bitmaps not supported by this kernel.\n");
385 return 1;
386 }
34163fc7 387 if (!st->ss->add_internal_bitmap(st, super, bitmap_chunk, delay, write_behind,
f9c25f1d 388 bitmapsize, 1, major)) {
55935d51
NB
389 fprintf(stderr, Name ": Given bitmap chunk size not supported.\n");
390 return 1;
391 }
392 bitmap_file = NULL;
393 }
394
395
396
82d9eba6
NB
397 if ((vers % 100) >= 1) { /* can use different versions */
398 mdu_array_info_t inf;
399 memset(&inf, 0, sizeof(inf));
400 inf.major_version = st->ss->major;
401 inf.minor_version = st->minor_version;
402 rv = ioctl(mdfd, SET_ARRAY_INFO, &inf);
403 } else
404 rv = ioctl(mdfd, SET_ARRAY_INFO, NULL);
405 if (rv) {
52826846
NB
406 fprintf(stderr, Name ": SET_ARRAY_INFO failed for %s: %s\n",
407 mddev, strerror(errno));
408 return 1;
682c7051 409 }
52826846 410
c82f047c
NB
411 if (bitmap_file) {
412 int uuid[4];
55935d51
NB
413
414 if (bitmap_chunk == UnSet)
415 bitmap_chunk = DEFAULT_BITMAP_CHUNK;
416
c82f047c 417 st->ss->uuid_from_super(uuid, super);
dfd4d8ee 418 if (CreateBitmap(bitmap_file, force, (char*)uuid, bitmap_chunk,
dcec9ee5 419 delay, write_behind,
f9c25f1d 420 bitmapsize,
dcec9ee5 421 major)) {
c82f047c
NB
422 return 1;
423 }
424 bitmap_fd = open(bitmap_file, O_RDWR);
425 if (bitmap_fd < 0) {
426 fprintf(stderr, Name ": weird: %s cannot be openned\n",
dcec9ee5 427 bitmap_file);
c82f047c
NB
428 return 1;
429 }
430 if (ioctl(mdfd, SET_BITMAP_FILE, bitmap_fd) < 0) {
431 fprintf(stderr, Name ": Cannot set bitmap file for %s: %s\n",
432 mddev, strerror(errno));
433 return 1;
434 }
435 }
436
4b1ac34b
NB
437
438
439 for (pass=1; pass <=2 ; pass++) {
440 mddev_dev_t moved_disk = NULL; /* the disk that was moved out of the insert point */
441
442 for (dnum=0, dv = devlist ; dv ;
443 dv=(dv->next)?(dv->next):moved_disk, dnum++) {
444 int fd;
445 struct stat stb;
446 mdu_disk_info_t disk;
447
448 disk.number = dnum;
449 if (dnum == insert_point) {
450 moved_disk = dv;
52826846 451 }
4b1ac34b
NB
452 disk.raid_disk = disk.number;
453 if (disk.raid_disk < raiddisks)
dfd4d8ee
NB
454 disk.state = (1<<MD_DISK_ACTIVE) |
455 (1<<MD_DISK_SYNC);
4b1ac34b
NB
456 else
457 disk.state = 0;
dfd4d8ee
NB
458 if (dv->writemostly)
459 disk.state |= (1<<MD_DISK_WRITEMOSTLY);
460
4b1ac34b
NB
461 if (dnum == insert_point ||
462 strcasecmp(dv->devname, "missing")==0) {
463 disk.major = 0;
464 disk.minor = 0;
dfd4d8ee 465 disk.state = (1<<MD_DISK_FAULTY);
4b1ac34b
NB
466 } else {
467 fd = open(dv->devname, O_RDONLY|O_EXCL, 0);
468 if (fd < 0) {
469 fprintf(stderr, Name ": failed to open %s after earlier success - aborting\n",
470 dv->devname);
471 return 1;
472 }
473 fstat(fd, &stb);
474 disk.major = major(stb.st_rdev);
475 disk.minor = minor(stb.st_rdev);
476 close(fd);
477 }
478 switch(pass){
479 case 1:
82d9eba6 480 st->ss->add_to_super(super, &disk);
4b1ac34b
NB
481 break;
482 case 2:
eaac7dde 483 if (disk.state == 1) break;
1bf4e2d9 484 st->ss->write_init_super(st, super, &disk, dv->devname);
4b1ac34b
NB
485
486 if (ioctl(mdfd, ADD_NEW_DISK, &disk)) {
487 fprintf(stderr, Name ": ADD_NEW_DISK for %s failed: %s\n",
488 dv->devname, strerror(errno));
489 free(super);
490 return 1;
491 }
492
493 break;
494 }
495 if (dv == moved_disk && dnum != insert_point) break;
52826846 496 }
5e52ae9e 497 }
4b1ac34b 498 free(super);
5e52ae9e 499
682c7051
NB
500 /* param is not actually used */
501 if (runstop == 1 || subdevs >= raiddisks) {
82b27616 502 mdu_param_t param;
682c7051
NB
503 if (ioctl(mdfd, RUN_ARRAY, &param)) {
504 fprintf(stderr, Name ": RUN_ARRAY failed: %s\n",
505 strerror(errno));
91f068bf 506 Manage_runstop(mddev, mdfd, -1, 0);
682c7051
NB
507 return 1;
508 }
dab6685f
NB
509 if (verbose >= 0)
510 fprintf(stderr, Name ": array %s started.\n", mddev);
682c7051 511 } else {
b83d95f3 512 fprintf(stderr, Name ": not starting array - not enough devices.\n");
682c7051
NB
513 }
514 return 0;
64c4757e 515}