]> git.ipfire.org Git - thirdparty/mdadm.git/blob - Build.c
Don't assume that mdmon is in the current directory.
[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 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)
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
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;
69 }
70 if (stat(dv->devname, &stb)) {
71 fprintf(stderr, Name ": Cannot find %s: %s\n",
72 dv->devname, strerror(errno));
73 return 1;
74 }
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;
79 }
80 }
81
82 if (raiddisks != subdevs) {
83 fprintf(stderr, Name ": requested %d devices in array but listed %d\n",
84 raiddisks, subdevs);
85 return 1;
86 }
87
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");
108
109 if (verbose > 0)
110 fprintf(stderr,
111 Name ": layout defaults to %s\n", map_num(faultylayout, layout));
112 break;
113 }
114
115
116 vers = md_get_version(mdfd);
117
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;
144 }
145 } else if (bitmap_file) {
146 fprintf(stderr, Name ": bitmaps not supported with this kernel\n");
147 return 1;
148 }
149
150 if (bitmap_file && level <= 0) {
151 fprintf(stderr, Name ": bitmaps not meaningful with level %s\n",
152 map_num(pers, level)?:"given");
153 return 1;
154 }
155 /* now add the devices */
156 for ((i=0), (dv = devlist) ; dv ; i++, dv=dv->next) {
157 unsigned long long dsize;
158 int fd;
159 if (strcmp("missing", dv->devname) == 0)
160 continue;
161 if (stat(dv->devname, &stb)) {
162 fprintf(stderr, Name ": Weird: %s has disappeared.\n",
163 dv->devname);
164 goto abort;
165 }
166 if ((stb.st_mode & S_IFMT)!= S_IFBLK) {
167 fprintf(stderr, Name ": Wierd: %s is no longer a block device.\n",
168 dv->devname);
169 goto abort;
170 }
171 fd = open(dv->devname, O_RDONLY|O_EXCL);
172 if (fd < 0) {
173 fprintf(stderr, Name ": Cannot open %s: %s\n",
174 dv->devname, strerror(errno));
175 goto abort;
176 }
177 if (get_dev_size(fd, NULL, &dsize) &&
178 (size == 0 || dsize < size))
179 size = dsize;
180 close(fd);
181 if (vers>= 9000) {
182 mdu_disk_info_t disk;
183 disk.number = i;
184 disk.raid_disk = i;
185 disk.state = (1<<MD_DISK_SYNC) | (1<<MD_DISK_ACTIVE);
186 if (dv->writemostly)
187 disk.state |= 1<<MD_DISK_WRITEMOSTLY;
188 disk.major = major(stb.st_rdev);
189 disk.minor = minor(stb.st_rdev);
190 if (ioctl(mdfd, ADD_NEW_DISK, &disk)) {
191 fprintf(stderr, Name ": ADD_NEW_DISK failed for %s: %s\n",
192 dv->devname, strerror(errno));
193 goto abort;
194 }
195 } else {
196 if (ioctl(mdfd, REGISTER_DEV, &stb.st_rdev)) {
197 fprintf(stderr, Name ": REGISTER_DEV failed for %s: %s.\n",
198 dv->devname, strerror(errno));
199 goto abort;
200 }
201 }
202 }
203 /* now to start it */
204 if (vers >= 9000) {
205 mdu_param_t param; /* not used by syscall */
206 if (bitmap_file) {
207 bitmap_fd = open(bitmap_file, O_RDWR);
208 if (bitmap_fd < 0) {
209 int major = BITMAP_MAJOR_HI;
210 #if 0
211 if (bitmap_chunk == UnSet) {
212 fprintf(stderr, Name ": %s cannot be openned.",
213 bitmap_file);
214 return 1;
215 }
216 #endif
217 if (vers < 9003) {
218 major = BITMAP_MAJOR_HOSTENDIAN;
219 #ifdef __BIG_ENDIAN
220 fprintf(stderr, Name ": Warning - bitmaps created on this kernel are not portable\n"
221 " between different architectures. Consider upgrading the Linux kernel.\n");
222 #endif
223 }
224 bitmapsize = size>>9; /* FIXME wrong for RAID10 */
225 if (CreateBitmap(bitmap_file, 1, NULL, bitmap_chunk,
226 delay, write_behind, bitmapsize, major)) {
227 return 1;
228 }
229 bitmap_fd = open(bitmap_file, O_RDWR);
230 if (bitmap_fd < 0) {
231 fprintf(stderr, Name ": %s cannot be openned.",
232 bitmap_file);
233 return 1;
234 }
235 }
236 if (bitmap_fd >= 0) {
237 if (ioctl(mdfd, SET_BITMAP_FILE, bitmap_fd) < 0) {
238 fprintf(stderr, Name ": Cannot set bitmap file for %s: %s\n",
239 mddev, strerror(errno));
240 return 1;
241 }
242 }
243 }
244 if (ioctl(mdfd, RUN_ARRAY, &param)) {
245 fprintf(stderr, Name ": RUN_ARRAY failed: %s\n",
246 strerror(errno));
247 goto abort;
248 }
249 } else {
250 unsigned long arg;
251 arg=0;
252 while (chunk > 4096) {
253 arg++;
254 chunk >>= 1;
255 }
256 if (level == 0)
257 chunk |= 0x20000;
258 else chunk |= 0x10000;
259 if (ioctl(mdfd, START_MD, arg)) {
260 fprintf(stderr, Name ": START_MD failed: %s\n",
261 strerror(errno));
262 goto abort;
263 }
264 }
265 if (verbose >= 0)
266 fprintf(stderr, Name ": array %s built and started.\n",
267 mddev);
268 return 0;
269
270 abort:
271 if (vers >= 9000)
272 ioctl(mdfd, STOP_ARRAY, 0);
273 else
274 ioctl(mdfd, STOP_MD, 0);
275 return 1;
276 }