Correct PPTP server firewall rules chain.
[tomato/davidwu.git] / release / src / router / mdadm / Build.c
blobdf9f4f2f5a5585eca174625b7b17c5ff43f0ca4b
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"
32 #define REGISTER_DEV _IO (MD_MAJOR, 1)
33 #define START_MD _IO (MD_MAJOR, 2)
34 #define STOP_MD _IO (MD_MAJOR, 3)
36 int Build(char *mddev, int mdfd, int chunk, int level, int layout,
37 int raiddisks,
38 mddev_dev_t devlist, int assume_clean,
39 char *bitmap_file, int bitmap_chunk, int write_behind, int delay, int verbose)
41 /* Build a linear or raid0 arrays without superblocks
42 * We cannot really do any checks, we just do it.
43 * For md_version < 0.90.0, we call REGISTER_DEV
44 * with the device numbers, and then
45 * START_MD giving the "geometry"
46 * geometry is 0xpp00cc
47 * where pp is personality: 1==linear, 2=raid0
48 * cc = chunk size factor: 0==4k, 1==8k etc.
50 * For md_version >= 0.90.0 we call
51 * SET_ARRAY_INFO, ADD_NEW_DISK, RUN_ARRAY
54 int i;
55 int vers;
56 struct stat stb;
57 int subdevs = 0, missing_disks = 0;
58 mddev_dev_t dv;
59 int bitmap_fd;
60 unsigned long long size = ~0ULL;
61 unsigned long long bitmapsize;
63 /* scan all devices, make sure they really are block devices */
64 for (dv = devlist; dv; dv=dv->next) {
65 subdevs++;
66 if (strcmp("missing", dv->devname) == 0) {
67 missing_disks++;
68 continue;
70 if (stat(dv->devname, &stb)) {
71 fprintf(stderr, Name ": Cannot find %s: %s\n",
72 dv->devname, strerror(errno));
73 return 1;
75 if ((stb.st_mode & S_IFMT) != S_IFBLK) {
76 fprintf(stderr, Name ": %s is not a block device.\n",
77 dv->devname);
78 return 1;
82 if (raiddisks != subdevs) {
83 fprintf(stderr, Name ": requested %d devices in array but listed %d\n",
84 raiddisks, subdevs);
85 return 1;
88 if (layout == UnSet)
89 switch(level) {
90 default: /* no layout */
91 layout = 0;
92 break;
93 case 10:
94 layout = 0x102; /* near=2, far=1 */
95 if (verbose > 0)
96 fprintf(stderr,
97 Name ": layout defaults to n1\n");
98 break;
99 case 5:
100 case 6:
101 layout = map_name(r5layout, "default");
102 if (verbose > 0)
103 fprintf(stderr,
104 Name ": layout defaults to %s\n", map_num(r5layout, layout));
105 break;
106 case LEVEL_FAULTY:
107 layout = map_name(faultylayout, "default");
109 if (verbose > 0)
110 fprintf(stderr,
111 Name ": layout defaults to %s\n", map_num(faultylayout, layout));
112 break;
116 vers = md_get_version(mdfd);
118 /* looks Ok, go for it */
119 if (vers >= 9000) {
120 mdu_array_info_t array;
121 array.level = level;
122 array.size = 0;
123 array.nr_disks = raiddisks;
124 array.raid_disks = raiddisks;
125 array.md_minor = 0;
126 if (fstat(mdfd, &stb)==0)
127 array.md_minor = minor(stb.st_rdev);
128 array.not_persistent = 1;
129 array.state = 0; /* not clean, but no errors */
130 if (assume_clean)
131 array.state |= 1;
132 array.active_disks = raiddisks - missing_disks;
133 array.working_disks = raiddisks - missing_disks;
134 array.spare_disks = 0;
135 array.failed_disks = missing_disks;
136 if (chunk == 0 && (level==0 || level==LEVEL_LINEAR))
137 chunk = 64;
138 array.chunk_size = chunk*1024;
139 array.layout = layout;
140 if (ioctl(mdfd, SET_ARRAY_INFO, &array)) {
141 fprintf(stderr, Name ": SET_ARRAY_INFO failed for %s: %s\n",
142 mddev, strerror(errno));
143 return 1;
145 } else if (bitmap_file) {
146 fprintf(stderr, Name ": bitmaps not supported with this kernel\n");
147 return 1;
149 /* now add the devices */
150 for ((i=0), (dv = devlist) ; dv ; i++, dv=dv->next) {
151 unsigned long long dsize;
152 int fd;
153 if (strcmp("missing", dv->devname) == 0)
154 continue;
155 if (stat(dv->devname, &stb)) {
156 fprintf(stderr, Name ": Weird: %s has disappeared.\n",
157 dv->devname);
158 goto abort;
160 if ((stb.st_mode & S_IFMT)!= S_IFBLK) {
161 fprintf(stderr, Name ": Wierd: %s is no longer a block device.\n",
162 dv->devname);
163 goto abort;
165 fd = open(dv->devname, O_RDONLY|O_EXCL);
166 if (fd < 0) {
167 fprintf(stderr, Name ": Cannot open %s: %s\n",
168 dv->devname, strerror(errno));
169 goto abort;
171 if (get_dev_size(fd, NULL, &dsize) &&
172 (size == 0 || dsize < size))
173 size = dsize;
174 close(fd);
175 if (vers>= 9000) {
176 mdu_disk_info_t disk;
177 disk.number = i;
178 disk.raid_disk = i;
179 disk.state = (1<<MD_DISK_SYNC) | (1<<MD_DISK_ACTIVE);
180 if (dv->writemostly)
181 disk.state |= 1<<MD_DISK_WRITEMOSTLY;
182 disk.major = major(stb.st_rdev);
183 disk.minor = minor(stb.st_rdev);
184 if (ioctl(mdfd, ADD_NEW_DISK, &disk)) {
185 fprintf(stderr, Name ": ADD_NEW_DISK failed for %s: %s\n",
186 dv->devname, strerror(errno));
187 goto abort;
189 } else {
190 if (ioctl(mdfd, REGISTER_DEV, &stb.st_rdev)) {
191 fprintf(stderr, Name ": REGISTER_DEV failed for %s: %s.\n",
192 dv->devname, strerror(errno));
193 goto abort;
197 /* now to start it */
198 if (vers >= 9000) {
199 mdu_param_t param; /* not used by syscall */
200 if (bitmap_file) {
201 bitmap_fd = open(bitmap_file, O_RDWR);
202 if (bitmap_fd < 0) {
203 int major = BITMAP_MAJOR_HI;
204 #if 0
205 if (bitmap_chunk == UnSet) {
206 fprintf(stderr, Name ": %s cannot be openned.",
207 bitmap_file);
208 return 1;
210 #endif
211 if (vers < 9003) {
212 major = BITMAP_MAJOR_HOSTENDIAN;
213 #ifdef __BIG_ENDIAN
214 fprintf(stderr, Name ": Warning - bitmaps created on this kernel are not portable\n"
215 " between different architectures. Consider upgrading the Linux kernel.\n");
216 #endif
218 bitmapsize = size>>9; /* FIXME wrong for RAID10 */
219 if (CreateBitmap(bitmap_file, 1, NULL, bitmap_chunk,
220 delay, write_behind, bitmapsize, major)) {
221 return 1;
223 bitmap_fd = open(bitmap_file, O_RDWR);
224 if (bitmap_fd < 0) {
225 fprintf(stderr, Name ": %s cannot be openned.",
226 bitmap_file);
227 return 1;
230 if (bitmap_fd >= 0) {
231 if (ioctl(mdfd, SET_BITMAP_FILE, bitmap_fd) < 0) {
232 fprintf(stderr, Name ": Cannot set bitmap file for %s: %s\n",
233 mddev, strerror(errno));
234 return 1;
238 if (ioctl(mdfd, RUN_ARRAY, &param)) {
239 fprintf(stderr, Name ": RUN_ARRAY failed: %s\n",
240 strerror(errno));
241 goto abort;
243 } else {
244 unsigned long arg;
245 arg=0;
246 while (chunk > 4096) {
247 arg++;
248 chunk >>= 1;
250 if (level == 0)
251 chunk |= 0x20000;
252 else chunk |= 0x10000;
253 if (ioctl(mdfd, START_MD, arg)) {
254 fprintf(stderr, Name ": START_MD failed: %s\n",
255 strerror(errno));
256 goto abort;
259 if (verbose >= 0)
260 fprintf(stderr, Name ": array %s built and started.\n",
261 mddev);
262 return 0;
264 abort:
265 if (vers >= 9000)
266 ioctl(mdfd, STOP_ARRAY, 0);
267 else
268 ioctl(mdfd, STOP_MD, 0);
269 return 1;