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