]> git.ipfire.org Git - thirdparty/mdadm.git/blob - Build.c
Always update mdadm/map when starting an array.
[thirdparty/mdadm.git] / Build.c
1 /*
2 * mdadm - manage Linux "md" devices aka RAID arrays.
3 *
4 * Copyright (C) 2001-2006 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@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
28 */
29
30 #include "mdadm.h"
31
32 #define REGISTER_DEV _IO (MD_MAJOR, 1)
33 #define START_MD _IO (MD_MAJOR, 2)
34 #define STOP_MD _IO (MD_MAJOR, 3)
35
36 int Build(char *mddev, int chunk, int level, int layout,
37 int raiddisks, mddev_dev_t devlist, int assume_clean,
38 char *bitmap_file, int bitmap_chunk, int write_behind,
39 int delay, int verbose, int autof)
40 {
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.
49 *
50 * For md_version >= 0.90.0 we call
51 * SET_ARRAY_INFO, ADD_NEW_DISK, RUN_ARRAY
52 *
53 */
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;
62 int mdfd;
63 char chosen_name[1024];
64 int uuid[4] = {0,0,0,0};
65
66 /* scan all devices, make sure they really are block devices */
67 for (dv = devlist; dv; dv=dv->next) {
68 subdevs++;
69 if (strcmp("missing", dv->devname) == 0) {
70 missing_disks++;
71 continue;
72 }
73 if (stat(dv->devname, &stb)) {
74 fprintf(stderr, Name ": Cannot find %s: %s\n",
75 dv->devname, strerror(errno));
76 return 1;
77 }
78 if ((stb.st_mode & S_IFMT) != S_IFBLK) {
79 fprintf(stderr, Name ": %s is not a block device.\n",
80 dv->devname);
81 return 1;
82 }
83 }
84
85 if (raiddisks != subdevs) {
86 fprintf(stderr, Name ": requested %d devices in array but listed %d\n",
87 raiddisks, subdevs);
88 return 1;
89 }
90
91 if (layout == UnSet)
92 switch(level) {
93 default: /* no layout */
94 layout = 0;
95 break;
96 case 10:
97 layout = 0x102; /* near=2, far=1 */
98 if (verbose > 0)
99 fprintf(stderr,
100 Name ": layout defaults to n1\n");
101 break;
102 case 5:
103 case 6:
104 layout = map_name(r5layout, "default");
105 if (verbose > 0)
106 fprintf(stderr,
107 Name ": layout defaults to %s\n", map_num(r5layout, layout));
108 break;
109 case LEVEL_FAULTY:
110 layout = map_name(faultylayout, "default");
111
112 if (verbose > 0)
113 fprintf(stderr,
114 Name ": layout defaults to %s\n", map_num(faultylayout, layout));
115 break;
116 }
117
118 /* We need to create the device. It can have no name. */
119 mdfd = create_mddev(mddev, NULL, autof, LOCAL,
120 chosen_name);
121 if (mdfd < 0)
122 return 1;
123
124 map_update(NULL, fd2devnum(mdfd), "none", uuid, chosen_name);
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 = 0;
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 close(mdfd);
279 return 0;
280
281 abort:
282 if (vers >= 9000)
283 ioctl(mdfd, STOP_ARRAY, 0);
284 else
285 ioctl(mdfd, STOP_MD, 0);
286 close(mdfd);
287 return 1;
288 }