]> git.ipfire.org Git - thirdparty/mdadm.git/blob - Build.c
Create version-4 bitmaps if kernel supports it.
[thirdparty/mdadm.git] / Build.c
1 /*
2 * mdadm - manage Linux "md" devices aka RAID arrays.
3 *
4 * Copyright (C) 2001-2002 Neil Brown <neilb@cse.unsw.edu.au>
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
62 /* scan all devices, make sure they really are block devices */
63 for (dv = devlist; dv; dv=dv->next) {
64 subdevs++;
65 if (strcmp("missing", dv->devname) == 0) {
66 missing_disks++;
67 continue;
68 }
69 if (stat(dv->devname, &stb)) {
70 fprintf(stderr, Name ": Cannot find %s: %s\n",
71 dv->devname, strerror(errno));
72 return 1;
73 }
74 if ((stb.st_mode & S_IFMT) != S_IFBLK) {
75 fprintf(stderr, Name ": %s is not a block device.\n",
76 dv->devname);
77 return 1;
78 }
79 }
80
81 if (raiddisks != subdevs) {
82 fprintf(stderr, Name ": requested %d devices in array but listed %d\n",
83 raiddisks, subdevs);
84 return 1;
85 }
86
87 if (layout == UnSet)
88 switch(level) {
89 default: /* no layout */
90 layout = 0;
91 break;
92 case 10:
93 layout = 0x102; /* near=2, far=1 */
94 if (verbose > 0)
95 fprintf(stderr,
96 Name ": layout defaults to n1\n");
97 break;
98 case 5:
99 case 6:
100 layout = map_name(r5layout, "default");
101 if (verbose > 0)
102 fprintf(stderr,
103 Name ": layout defaults to %s\n", map_num(r5layout, layout));
104 break;
105 case LEVEL_FAULTY:
106 layout = map_name(faultylayout, "default");
107
108 if (verbose > 0)
109 fprintf(stderr,
110 Name ": layout defaults to %s\n", map_num(faultylayout, layout));
111 break;
112 }
113
114
115 vers = md_get_version(mdfd);
116
117 /* looks Ok, go for it */
118 if (vers >= 9000) {
119 mdu_array_info_t array;
120 array.level = level;
121 array.size = 0;
122 array.nr_disks = raiddisks;
123 array.raid_disks = raiddisks;
124 array.md_minor = 0;
125 if (fstat(mdfd, &stb)==0)
126 array.md_minor = minor(stb.st_rdev);
127 array.not_persistent = 1;
128 array.state = 0; /* not clean, but no errors */
129 if (assume_clean)
130 array.state |= 1;
131 array.active_disks = raiddisks - missing_disks;
132 array.working_disks = raiddisks - missing_disks;
133 array.spare_disks = 0;
134 array.failed_disks = missing_disks;
135 if (chunk == 0 && (level==0 || level==LEVEL_LINEAR))
136 chunk = 64;
137 array.chunk_size = chunk*1024;
138 array.layout = layout;
139 if (ioctl(mdfd, SET_ARRAY_INFO, &array)) {
140 fprintf(stderr, Name ": SET_ARRAY_INFO failed for %s: %s\n",
141 mddev, strerror(errno));
142 return 1;
143 }
144 } else if (bitmap_file) {
145 fprintf(stderr, Name ": bitmaps not supported with this kernel\n");
146 return 1;
147 }
148 /* now add the devices */
149 for ((i=0), (dv = devlist) ; dv ; i++, dv=dv->next) {
150 unsigned long dsize;
151 int fd;
152 if (strcmp("missing", dv->devname) == 0)
153 continue;
154 if (stat(dv->devname, &stb)) {
155 fprintf(stderr, Name ": Weird: %s has disappeared.\n",
156 dv->devname);
157 goto abort;
158 }
159 if ((stb.st_mode & S_IFMT)!= S_IFBLK) {
160 fprintf(stderr, Name ": Wierd: %s is no longer a block device.\n",
161 dv->devname);
162 goto abort;
163 }
164 fd = open(dv->devname, O_RDONLY|O_EXCL);
165 if (fd < 0) {
166 fprintf(stderr, Name ": Cannot open %s: %s\n",
167 dv->devname, strerror(errno));
168 goto abort;
169 }
170 if (ioctl(fd, BLKGETSIZE, &dsize) == 0 && dsize > 0) {
171 unsigned long long ldsize = dsize;
172 ldsize <<= 9;
173 if (size== 0 || ldsize < size)
174 size = ldsize;
175 }
176 close(fd);
177 if (vers>= 9000) {
178 mdu_disk_info_t disk;
179 disk.number = i;
180 disk.raid_disk = i;
181 disk.state = (1<<MD_DISK_SYNC) | (1<<MD_DISK_ACTIVE);
182 if (dv->writemostly)
183 disk.state |= 1<<MD_DISK_WRITEMOSTLY;
184 disk.major = major(stb.st_rdev);
185 disk.minor = minor(stb.st_rdev);
186 if (ioctl(mdfd, ADD_NEW_DISK, &disk)) {
187 fprintf(stderr, Name ": ADD_NEW_DISK failed for %s: %s\n",
188 dv->devname, strerror(errno));
189 goto abort;
190 }
191 } else {
192 if (ioctl(mdfd, REGISTER_DEV, &stb.st_rdev)) {
193 fprintf(stderr, Name ": REGISTER_DEV failed for %s: %s.\n",
194 dv->devname, strerror(errno));
195 goto abort;
196 }
197 }
198 }
199 /* now to start it */
200 if (vers >= 9000) {
201 mdu_param_t param; /* not used by syscall */
202 if (bitmap_file) {
203 bitmap_fd = open(bitmap_file, O_RDWR);
204 if (bitmap_fd < 0) {
205 int major = BITMAP_MAJOR_HI;
206 if (bitmap_chunk == UnSet) {
207 fprintf(stderr, Name ": %s cannot be openned.",
208 bitmap_file);
209 return 1;
210 }
211 if (vers < 9003) {
212 major = BITMAP_MAJOR_HOSTENDIAN;
213 #ifdef __BIG_ENDIAN
214 fprintf(stderr, Name ": Warning - bitmaps created on this kernel are not portable\n"
215 " between different architectured. Consider upgrading the Linux kernel.\n");
216 #endif
217 }
218 if (CreateBitmap(bitmap_file, 1, NULL, bitmap_chunk,
219 delay, write_behind, size>>9, major)) {
220 return 1;
221 }
222 bitmap_fd = open(bitmap_file, O_RDWR);
223 if (bitmap_fd < 0) {
224 fprintf(stderr, Name ": %s cannot be openned.",
225 bitmap_file);
226 return 1;
227 }
228 }
229 if (bitmap_fd >= 0) {
230 if (ioctl(mdfd, SET_BITMAP_FILE, bitmap_fd) < 0) {
231 fprintf(stderr, Name ": Cannot set bitmap file for %s: %s\n",
232 mddev, strerror(errno));
233 return 1;
234 }
235 }
236 }
237 if (ioctl(mdfd, RUN_ARRAY, &param)) {
238 fprintf(stderr, Name ": RUN_ARRAY failed: %s\n",
239 strerror(errno));
240 goto abort;
241 }
242 } else {
243 unsigned long arg;
244 arg=0;
245 while (chunk > 4096) {
246 arg++;
247 chunk >>= 1;
248 }
249 if (level == 0)
250 chunk |= 0x20000;
251 else chunk |= 0x10000;
252 if (ioctl(mdfd, START_MD, arg)) {
253 fprintf(stderr, Name ": START_MD failed: %s\n",
254 strerror(errno));
255 goto abort;
256 }
257 }
258 if (verbose >= 0)
259 fprintf(stderr, Name ": array %s built and started.\n",
260 mddev);
261 return 0;
262
263 abort:
264 if (vers >= 9000)
265 ioctl(mdfd, STOP_ARRAY, 0);
266 else
267 ioctl(mdfd, STOP_MD, 0);
268 return 1;
269
270 }