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