]> git.ipfire.org Git - thirdparty/mdadm.git/blob - Build.c
Create: support --readonly flag.
[thirdparty/mdadm.git] / Build.c
1 /*
2 * mdadm - manage Linux "md" devices aka RAID arrays.
3 *
4 * Copyright (C) 2001-2009 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@suse.de>
23 */
24
25 #include "mdadm.h"
26
27 #define REGISTER_DEV _IO (MD_MAJOR, 1)
28 #define START_MD _IO (MD_MAJOR, 2)
29 #define STOP_MD _IO (MD_MAJOR, 3)
30
31 int Build(char *mddev, int chunk, int level, int layout,
32 int raiddisks, struct mddev_dev *devlist, int assume_clean,
33 char *bitmap_file, int bitmap_chunk, int write_behind,
34 int delay, int verbose, int autof, unsigned long long size)
35 {
36 /* Build a linear or raid0 arrays without superblocks
37 * We cannot really do any checks, we just do it.
38 * For md_version < 0.90.0, we call REGISTER_DEV
39 * with the device numbers, and then
40 * START_MD giving the "geometry"
41 * geometry is 0xpp00cc
42 * where pp is personality: 1==linear, 2=raid0
43 * cc = chunk size factor: 0==4k, 1==8k etc.
44 *
45 * For md_version >= 0.90.0 we call
46 * SET_ARRAY_INFO, ADD_NEW_DISK, RUN_ARRAY
47 *
48 */
49 int i;
50 int vers;
51 struct stat stb;
52 int subdevs = 0, missing_disks = 0;
53 struct mddev_dev *dv;
54 int bitmap_fd;
55 unsigned long long bitmapsize;
56 int mdfd;
57 char chosen_name[1024];
58 int uuid[4] = {0,0,0,0};
59 struct map_ent *map = NULL;
60
61 /* scan all devices, make sure they really are block devices */
62 for (dv = devlist; dv; dv=dv->next) {
63 subdevs++;
64 if (strcmp("missing", dv->devname) == 0) {
65 missing_disks++;
66 continue;
67 }
68 if (stat(dv->devname, &stb)) {
69 pr_err("Cannot find %s: %s\n",
70 dv->devname, strerror(errno));
71 return 1;
72 }
73 if ((stb.st_mode & S_IFMT) != S_IFBLK) {
74 pr_err("%s is not a block device.\n",
75 dv->devname);
76 return 1;
77 }
78 }
79
80 if (raiddisks != subdevs) {
81 pr_err("requested %d devices in array but listed %d\n",
82 raiddisks, subdevs);
83 return 1;
84 }
85
86 if (layout == UnSet)
87 switch(level) {
88 default: /* no layout */
89 layout = 0;
90 break;
91 case 10:
92 layout = 0x102; /* near=2, far=1 */
93 if (verbose > 0)
94 pr_err("layout defaults to n1\n");
95 break;
96 case 5:
97 case 6:
98 layout = map_name(r5layout, "default");
99 if (verbose > 0)
100 pr_err("layout defaults to %s\n", map_num(r5layout, layout));
101 break;
102 case LEVEL_FAULTY:
103 layout = map_name(faultylayout, "default");
104
105 if (verbose > 0)
106 pr_err("layout defaults to %s\n", map_num(faultylayout, layout));
107 break;
108 }
109
110 /* We need to create the device. It can have no name. */
111 map_lock(&map);
112 mdfd = create_mddev(mddev, NULL, autof, LOCAL,
113 chosen_name);
114 if (mdfd < 0) {
115 map_unlock(&map);
116 return 1;
117 }
118 mddev = chosen_name;
119
120 map_update(&map, fd2devnum(mdfd), "none", uuid, chosen_name);
121 map_unlock(&map);
122
123 vers = md_get_version(mdfd);
124
125 /* looks Ok, go for it */
126 if (vers >= 9000) {
127 mdu_array_info_t array;
128 array.level = level;
129 array.size = size;
130 array.nr_disks = raiddisks;
131 array.raid_disks = raiddisks;
132 array.md_minor = 0;
133 if (fstat(mdfd, &stb)==0)
134 array.md_minor = minor(stb.st_rdev);
135 array.not_persistent = 1;
136 array.state = 0; /* not clean, but no errors */
137 if (assume_clean)
138 array.state |= 1;
139 array.active_disks = raiddisks - missing_disks;
140 array.working_disks = raiddisks - missing_disks;
141 array.spare_disks = 0;
142 array.failed_disks = missing_disks;
143 if (chunk == 0 && (level==0 || level==LEVEL_LINEAR))
144 chunk = 64;
145 array.chunk_size = chunk*1024;
146 array.layout = layout;
147 if (ioctl(mdfd, SET_ARRAY_INFO, &array)) {
148 pr_err("SET_ARRAY_INFO failed for %s: %s\n",
149 mddev, strerror(errno));
150 goto abort;
151 }
152 } else if (bitmap_file) {
153 pr_err("bitmaps not supported with this kernel\n");
154 goto abort;
155 }
156
157 if (bitmap_file && level <= 0) {
158 pr_err("bitmaps not meaningful with level %s\n",
159 map_num(pers, level)?:"given");
160 goto abort;
161 }
162 /* now add the devices */
163 for ((i=0), (dv = devlist) ; dv ; i++, dv=dv->next) {
164 unsigned long long dsize;
165 int fd;
166 if (strcmp("missing", dv->devname) == 0)
167 continue;
168 if (stat(dv->devname, &stb)) {
169 pr_err("Weird: %s has disappeared.\n",
170 dv->devname);
171 goto abort;
172 }
173 if ((stb.st_mode & S_IFMT)!= S_IFBLK) {
174 pr_err("Wierd: %s is no longer a block device.\n",
175 dv->devname);
176 goto abort;
177 }
178 fd = open(dv->devname, O_RDONLY|O_EXCL);
179 if (fd < 0) {
180 pr_err("Cannot open %s: %s\n",
181 dv->devname, strerror(errno));
182 goto abort;
183 }
184 if (get_dev_size(fd, NULL, &dsize) &&
185 (size == 0 || dsize < size))
186 size = dsize;
187 close(fd);
188 if (vers >= 9000) {
189 mdu_disk_info_t disk;
190 disk.number = i;
191 disk.raid_disk = i;
192 disk.state = (1<<MD_DISK_SYNC) | (1<<MD_DISK_ACTIVE);
193 if (dv->writemostly == 1)
194 disk.state |= 1<<MD_DISK_WRITEMOSTLY;
195 disk.major = major(stb.st_rdev);
196 disk.minor = minor(stb.st_rdev);
197 if (ioctl(mdfd, ADD_NEW_DISK, &disk)) {
198 pr_err("ADD_NEW_DISK failed for %s: %s\n",
199 dv->devname, strerror(errno));
200 goto abort;
201 }
202 } else {
203 if (ioctl(mdfd, REGISTER_DEV, &stb.st_rdev)) {
204 pr_err("REGISTER_DEV failed for %s: %s.\n",
205 dv->devname, strerror(errno));
206 goto abort;
207 }
208 }
209 }
210 /* now to start it */
211 if (vers >= 9000) {
212 mdu_param_t param; /* not used by syscall */
213 if (bitmap_file) {
214 bitmap_fd = open(bitmap_file, O_RDWR);
215 if (bitmap_fd < 0) {
216 int major = BITMAP_MAJOR_HI;
217 #if 0
218 if (bitmap_chunk == UnSet) {
219 pr_err("%s cannot be openned.",
220 bitmap_file);
221 goto abort;
222 }
223 #endif
224 if (vers < 9003) {
225 major = BITMAP_MAJOR_HOSTENDIAN;
226 #ifdef __BIG_ENDIAN
227 pr_err("Warning - bitmaps created on this kernel are not portable\n"
228 " between different architectures. Consider upgrading the Linux kernel.\n");
229 #endif
230 }
231 bitmapsize = size>>9; /* FIXME wrong for RAID10 */
232 if (CreateBitmap(bitmap_file, 1, NULL, bitmap_chunk,
233 delay, write_behind, bitmapsize, major)) {
234 goto abort;
235 }
236 bitmap_fd = open(bitmap_file, O_RDWR);
237 if (bitmap_fd < 0) {
238 pr_err("%s cannot be openned.",
239 bitmap_file);
240 goto abort;
241 }
242 }
243 if (bitmap_fd >= 0) {
244 if (ioctl(mdfd, SET_BITMAP_FILE, bitmap_fd) < 0) {
245 pr_err("Cannot set bitmap file for %s: %s\n",
246 mddev, strerror(errno));
247 goto abort;
248 }
249 }
250 }
251 if (ioctl(mdfd, RUN_ARRAY, &param)) {
252 pr_err("RUN_ARRAY failed: %s\n",
253 strerror(errno));
254 if (chunk & (chunk-1)) {
255 cont_err("Problem may be that chunk size"
256 " is not a power of 2\n");
257 }
258 goto abort;
259 }
260 } else {
261 unsigned long arg;
262 arg=0;
263 while (chunk > 4096) {
264 arg++;
265 chunk >>= 1;
266 }
267 if (level == 0)
268 chunk |= 0x20000;
269 else chunk |= 0x10000;
270 if (ioctl(mdfd, START_MD, arg)) {
271 pr_err("START_MD failed: %s\n",
272 strerror(errno));
273 goto abort;
274 }
275 }
276 if (verbose >= 0)
277 pr_err("array %s built and started.\n",
278 mddev);
279 wait_for(mddev, mdfd);
280 close(mdfd);
281 return 0;
282
283 abort:
284 if (vers >= 9000)
285 ioctl(mdfd, STOP_ARRAY, 0);
286 else
287 ioctl(mdfd, STOP_MD, 0);
288 close(mdfd);
289 return 1;
290 }