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