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