]>
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 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 | /* now add the devices */ | |
150 | for ((i=0), (dv = devlist) ; dv ; i++, dv=dv->next) { | |
151 | unsigned long dsize; | |
152 | int fd; | |
153 | if (strcmp("missing", dv->devname) == 0) | |
154 | continue; | |
155 | if (stat(dv->devname, &stb)) { | |
156 | fprintf(stderr, Name ": Weird: %s has disappeared.\n", | |
157 | dv->devname); | |
158 | goto abort; | |
159 | } | |
160 | if ((stb.st_mode & S_IFMT)!= S_IFBLK) { | |
161 | fprintf(stderr, Name ": Wierd: %s is no longer a block device.\n", | |
162 | dv->devname); | |
163 | goto abort; | |
164 | } | |
165 | fd = open(dv->devname, O_RDONLY|O_EXCL); | |
166 | if (fd < 0) { | |
167 | fprintf(stderr, Name ": Cannot open %s: %s\n", | |
168 | dv->devname, strerror(errno)); | |
169 | goto abort; | |
170 | } | |
171 | if (ioctl(fd, BLKGETSIZE, &dsize) == 0 && dsize > 0) { | |
172 | unsigned long long ldsize = dsize; | |
173 | ldsize <<= 9; | |
174 | if (size== 0 || ldsize < size) | |
175 | size = ldsize; | |
176 | } | |
177 | close(fd); | |
178 | if (vers>= 9000) { | |
179 | mdu_disk_info_t disk; | |
180 | disk.number = i; | |
181 | disk.raid_disk = i; | |
182 | disk.state = (1<<MD_DISK_SYNC) | (1<<MD_DISK_ACTIVE); | |
183 | if (dv->writemostly) | |
184 | disk.state |= 1<<MD_DISK_WRITEMOSTLY; | |
185 | disk.major = major(stb.st_rdev); | |
186 | disk.minor = minor(stb.st_rdev); | |
187 | if (ioctl(mdfd, ADD_NEW_DISK, &disk)) { | |
188 | fprintf(stderr, Name ": ADD_NEW_DISK failed for %s: %s\n", | |
189 | dv->devname, strerror(errno)); | |
190 | goto abort; | |
191 | } | |
192 | } else { | |
193 | if (ioctl(mdfd, REGISTER_DEV, &stb.st_rdev)) { | |
194 | fprintf(stderr, Name ": REGISTER_DEV failed for %s: %s.\n", | |
195 | dv->devname, strerror(errno)); | |
196 | goto abort; | |
197 | } | |
198 | } | |
199 | } | |
200 | /* now to start it */ | |
201 | if (vers >= 9000) { | |
202 | mdu_param_t param; /* not used by syscall */ | |
203 | if (bitmap_file) { | |
204 | bitmap_fd = open(bitmap_file, O_RDWR); | |
205 | if (bitmap_fd < 0) { | |
206 | int major = BITMAP_MAJOR_HI; | |
207 | #if 0 | |
208 | if (bitmap_chunk == UnSet) { | |
209 | fprintf(stderr, Name ": %s cannot be openned.", | |
210 | bitmap_file); | |
211 | return 1; | |
212 | } | |
213 | #endif | |
214 | if (vers < 9003) { | |
215 | major = BITMAP_MAJOR_HOSTENDIAN; | |
216 | #ifdef __BIG_ENDIAN | |
217 | fprintf(stderr, Name ": Warning - bitmaps created on this kernel are not portable\n" | |
218 | " between different architectures. Consider upgrading the Linux kernel.\n"); | |
219 | #endif | |
220 | } | |
221 | bitmapsize = size>>9; /* FIXME wrong for RAID10 */ | |
222 | if (CreateBitmap(bitmap_file, 1, NULL, bitmap_chunk, | |
223 | delay, write_behind, bitmapsize, major)) { | |
224 | return 1; | |
225 | } | |
226 | bitmap_fd = open(bitmap_file, O_RDWR); | |
227 | if (bitmap_fd < 0) { | |
228 | fprintf(stderr, Name ": %s cannot be openned.", | |
229 | bitmap_file); | |
230 | return 1; | |
231 | } | |
232 | } | |
233 | if (bitmap_fd >= 0) { | |
234 | if (ioctl(mdfd, SET_BITMAP_FILE, bitmap_fd) < 0) { | |
235 | fprintf(stderr, Name ": Cannot set bitmap file for %s: %s\n", | |
236 | mddev, strerror(errno)); | |
237 | return 1; | |
238 | } | |
239 | } | |
240 | } | |
241 | if (ioctl(mdfd, RUN_ARRAY, ¶m)) { | |
242 | fprintf(stderr, Name ": RUN_ARRAY failed: %s\n", | |
243 | strerror(errno)); | |
244 | goto abort; | |
245 | } | |
246 | } else { | |
247 | unsigned long arg; | |
248 | arg=0; | |
249 | while (chunk > 4096) { | |
250 | arg++; | |
251 | chunk >>= 1; | |
252 | } | |
253 | if (level == 0) | |
254 | chunk |= 0x20000; | |
255 | else chunk |= 0x10000; | |
256 | if (ioctl(mdfd, START_MD, arg)) { | |
257 | fprintf(stderr, Name ": START_MD failed: %s\n", | |
258 | strerror(errno)); | |
259 | goto abort; | |
260 | } | |
261 | } | |
262 | if (verbose >= 0) | |
263 | fprintf(stderr, Name ": array %s built and started.\n", | |
264 | mddev); | |
265 | return 0; | |
266 | ||
267 | abort: | |
268 | if (vers >= 9000) | |
269 | ioctl(mdfd, STOP_ARRAY, 0); | |
270 | else | |
271 | ioctl(mdfd, STOP_MD, 0); | |
272 | return 1; | |
273 | ||
274 | } |