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