]> git.ipfire.org Git - thirdparty/mdadm.git/blob - Build.c
Correctly abort level change when reshape_array fails.
[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 fprintf(stderr, Name ": Cannot find %s: %s\n",
70 dv->devname, strerror(errno));
71 return 1;
72 }
73 if ((stb.st_mode & S_IFMT) != S_IFBLK) {
74 fprintf(stderr, Name ": %s is not a block device.\n",
75 dv->devname);
76 return 1;
77 }
78 }
79
80 if (raiddisks != subdevs) {
81 fprintf(stderr, Name ": 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 fprintf(stderr,
95 Name ": layout defaults to n1\n");
96 break;
97 case 5:
98 case 6:
99 layout = map_name(r5layout, "default");
100 if (verbose > 0)
101 fprintf(stderr,
102 Name ": layout defaults to %s\n", map_num(r5layout, layout));
103 break;
104 case LEVEL_FAULTY:
105 layout = map_name(faultylayout, "default");
106
107 if (verbose > 0)
108 fprintf(stderr,
109 Name ": layout defaults to %s\n", map_num(faultylayout, layout));
110 break;
111 }
112
113 /* We need to create the device. It can have no name. */
114 map_lock(&map);
115 mdfd = create_mddev(mddev, NULL, autof, LOCAL,
116 chosen_name);
117 if (mdfd < 0) {
118 map_unlock(&map);
119 return 1;
120 }
121 mddev = chosen_name;
122
123 map_update(&map, fd2devnum(mdfd), "none", uuid, chosen_name);
124 map_unlock(&map);
125
126 vers = md_get_version(mdfd);
127
128 /* looks Ok, go for it */
129 if (vers >= 9000) {
130 mdu_array_info_t array;
131 array.level = level;
132 array.size = size;
133 array.nr_disks = raiddisks;
134 array.raid_disks = raiddisks;
135 array.md_minor = 0;
136 if (fstat(mdfd, &stb)==0)
137 array.md_minor = minor(stb.st_rdev);
138 array.not_persistent = 1;
139 array.state = 0; /* not clean, but no errors */
140 if (assume_clean)
141 array.state |= 1;
142 array.active_disks = raiddisks - missing_disks;
143 array.working_disks = raiddisks - missing_disks;
144 array.spare_disks = 0;
145 array.failed_disks = missing_disks;
146 if (chunk == 0 && (level==0 || level==LEVEL_LINEAR))
147 chunk = 64;
148 array.chunk_size = chunk*1024;
149 array.layout = layout;
150 if (ioctl(mdfd, SET_ARRAY_INFO, &array)) {
151 fprintf(stderr, Name ": SET_ARRAY_INFO failed for %s: %s\n",
152 mddev, strerror(errno));
153 goto abort;
154 }
155 } else if (bitmap_file) {
156 fprintf(stderr, Name ": bitmaps not supported with this kernel\n");
157 goto abort;
158 }
159
160 if (bitmap_file && level <= 0) {
161 fprintf(stderr, Name ": bitmaps not meaningful with level %s\n",
162 map_num(pers, level)?:"given");
163 goto abort;
164 }
165 /* now add the devices */
166 for ((i=0), (dv = devlist) ; dv ; i++, dv=dv->next) {
167 unsigned long long dsize;
168 int fd;
169 if (strcmp("missing", dv->devname) == 0)
170 continue;
171 if (stat(dv->devname, &stb)) {
172 fprintf(stderr, Name ": Weird: %s has disappeared.\n",
173 dv->devname);
174 goto abort;
175 }
176 if ((stb.st_mode & S_IFMT)!= S_IFBLK) {
177 fprintf(stderr, Name ": Wierd: %s is no longer a block device.\n",
178 dv->devname);
179 goto abort;
180 }
181 fd = open(dv->devname, O_RDONLY|O_EXCL);
182 if (fd < 0) {
183 fprintf(stderr, Name ": Cannot open %s: %s\n",
184 dv->devname, strerror(errno));
185 goto abort;
186 }
187 if (get_dev_size(fd, NULL, &dsize) &&
188 (size == 0 || dsize < size))
189 size = dsize;
190 close(fd);
191 if (vers >= 9000) {
192 mdu_disk_info_t disk;
193 disk.number = i;
194 disk.raid_disk = i;
195 disk.state = (1<<MD_DISK_SYNC) | (1<<MD_DISK_ACTIVE);
196 if (dv->writemostly == 1)
197 disk.state |= 1<<MD_DISK_WRITEMOSTLY;
198 disk.major = major(stb.st_rdev);
199 disk.minor = minor(stb.st_rdev);
200 if (ioctl(mdfd, ADD_NEW_DISK, &disk)) {
201 fprintf(stderr, Name ": ADD_NEW_DISK failed for %s: %s\n",
202 dv->devname, strerror(errno));
203 goto abort;
204 }
205 } else {
206 if (ioctl(mdfd, REGISTER_DEV, &stb.st_rdev)) {
207 fprintf(stderr, Name ": REGISTER_DEV failed for %s: %s.\n",
208 dv->devname, strerror(errno));
209 goto abort;
210 }
211 }
212 }
213 /* now to start it */
214 if (vers >= 9000) {
215 mdu_param_t param; /* not used by syscall */
216 if (bitmap_file) {
217 bitmap_fd = open(bitmap_file, O_RDWR);
218 if (bitmap_fd < 0) {
219 int major = BITMAP_MAJOR_HI;
220 #if 0
221 if (bitmap_chunk == UnSet) {
222 fprintf(stderr, Name ": %s cannot be openned.",
223 bitmap_file);
224 goto abort;
225 }
226 #endif
227 if (vers < 9003) {
228 major = BITMAP_MAJOR_HOSTENDIAN;
229 #ifdef __BIG_ENDIAN
230 fprintf(stderr, Name ": Warning - bitmaps created on this kernel are not portable\n"
231 " between different architectures. Consider upgrading the Linux kernel.\n");
232 #endif
233 }
234 bitmapsize = size>>9; /* FIXME wrong for RAID10 */
235 if (CreateBitmap(bitmap_file, 1, NULL, bitmap_chunk,
236 delay, write_behind, bitmapsize, major)) {
237 goto abort;
238 }
239 bitmap_fd = open(bitmap_file, O_RDWR);
240 if (bitmap_fd < 0) {
241 fprintf(stderr, Name ": %s cannot be openned.",
242 bitmap_file);
243 goto abort;
244 }
245 }
246 if (bitmap_fd >= 0) {
247 if (ioctl(mdfd, SET_BITMAP_FILE, bitmap_fd) < 0) {
248 fprintf(stderr, Name ": Cannot set bitmap file for %s: %s\n",
249 mddev, strerror(errno));
250 goto abort;
251 }
252 }
253 }
254 if (ioctl(mdfd, RUN_ARRAY, &param)) {
255 fprintf(stderr, Name ": RUN_ARRAY failed: %s\n",
256 strerror(errno));
257 goto abort;
258 }
259 } else {
260 unsigned long arg;
261 arg=0;
262 while (chunk > 4096) {
263 arg++;
264 chunk >>= 1;
265 }
266 if (level == 0)
267 chunk |= 0x20000;
268 else chunk |= 0x10000;
269 if (ioctl(mdfd, START_MD, arg)) {
270 fprintf(stderr, Name ": START_MD failed: %s\n",
271 strerror(errno));
272 goto abort;
273 }
274 }
275 if (verbose >= 0)
276 fprintf(stderr, Name ": 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 }