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