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