]> git.ipfire.org Git - thirdparty/mdadm.git/blob - Create.c
mdadm-1.3.0
[thirdparty/mdadm.git] / Create.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 #include "md_u.h"
32 #include "md_p.h"
33
34 int Create(char *mddev, int mdfd,
35 int chunk, int level, int layout, unsigned long size, int raiddisks, int sparedisks,
36 int subdevs, mddev_dev_t devlist,
37 int runstop, int verbose, int force)
38 {
39 /*
40 * Create a new raid array.
41 *
42 * First check that necessary details are available
43 * (i.e. level, raid-disks)
44 *
45 * Then check each disk to see what might be on it
46 * and report anything interesting.
47 *
48 * If anything looks odd, and runstop not set,
49 * abort.
50 *
51 * SET_ARRAY_INFO and ADD_NEW_DISK, and
52 * if runstop==run, or raiddisks diskswere used,
53 * RUN_ARRAY
54 */
55 unsigned long minsize=0, maxsize=0;
56 char *mindisc = NULL;
57 char *maxdisc = NULL;
58 int dnum;
59 mddev_dev_t dv;
60 int fail=0, warn=0;
61 struct stat stb;
62 int first_missing = MD_SB_DISKS*2;
63 int missing_disks = 0;
64 int insert_point = MD_SB_DISKS*2; /* where to insert a missing drive */
65 mddev_dev_t moved_disk = NULL; /* the disk that was moved out of the insert point */
66
67 mdu_array_info_t array;
68
69
70 if (md_get_version(mdfd) < 9000) {
71 fprintf(stderr, Name ": Create requires md driver verison 0.90.0 or later\n");
72 return 1;
73 }
74 if (level == -10) {
75 fprintf(stderr,
76 Name ": a RAID level is needed to create an array.\n");
77 return 1;
78 }
79 if (raiddisks < 1) {
80 fprintf(stderr,
81 Name ": a number of --raid-devices must be given to create an array\n");
82 return 1;
83 }
84 if (raiddisks < 2 && level >= 4) {
85 fprintf(stderr,
86 Name ": atleast 2 raid-devices needed for level 4 or 5\n");
87 return 1;
88 }
89 if (raiddisks+sparedisks > MD_SB_DISKS) {
90 fprintf(stderr,
91 Name ": too many devices requested: %d+%d > %d\n",
92 raiddisks, sparedisks, MD_SB_DISKS);
93 return 1;
94 }
95 if (subdevs > raiddisks+sparedisks) {
96 fprintf(stderr, Name ": You have listed more devices (%d) than are in the array(%d)!\n", subdevs, raiddisks+sparedisks);
97 return 1;
98 }
99 if (subdevs < raiddisks+sparedisks) {
100 fprintf(stderr, Name ": You haven't given enough devices (real or missing) to create this array\n");
101 return 1;
102 }
103
104 /* now set some defaults */
105 if (layout == -1)
106 switch(level) {
107 default: /* no layout */
108 layout = 0;
109 break;
110 case 5:
111 layout = map_name(r5layout, "default");
112 if (verbose)
113 fprintf(stderr,
114 Name ": layout defaults to %s\n", map_num(r5layout, layout));
115 break;
116 }
117
118 switch(level) {
119 case 4:
120 case 5:
121 case 0:
122 case -1: /* linear */
123 if (chunk == 0) {
124 chunk = 64;
125 if (verbose)
126 fprintf(stderr, Name ": chunk size defaults to 64K\n");
127 }
128 break;
129 default: /* raid1, multipath */
130 if (chunk) {
131 chunk = 0;
132 if (verbose)
133 fprintf(stderr, Name ": chunk size ignored for this level\n");
134 }
135 break;
136 }
137
138 /* now look at the subdevs */
139 array.active_disks = 0;
140 array.working_disks = 0;
141 dnum = 0;
142 for (dv=devlist; dv; dv=dv->next, dnum++) {
143 char *dname = dv->devname;
144 unsigned long dsize, freesize;
145 int fd;
146 if (strcasecmp(dname, "missing")==0) {
147 if (first_missing > dnum)
148 first_missing = dnum;
149 missing_disks ++;
150 continue;
151 }
152 array.working_disks++;
153 if (dnum < raiddisks)
154 array.active_disks++;
155 fd = open(dname, O_RDONLY, 0);
156 if (fd <0 ) {
157 fprintf(stderr, Name ": Cannot open %s: %s\n",
158 dname, strerror(errno));
159 fail=1;
160 continue;
161 }
162 if (ioctl(fd, BLKGETSIZE, &dsize)) {
163 fprintf(stderr, Name ": Cannot get size of %s: %s\n",
164 dname, strerror(errno));
165 fail = 1;
166 close(fd);
167 continue;
168 }
169 if (dsize < MD_RESERVED_SECTORS*2) {
170 fprintf(stderr, Name ": %s is too small: %luK\n",
171 dname, dsize/2);
172 fail = 1;
173 close(fd);
174 continue;
175 }
176 freesize = MD_NEW_SIZE_SECTORS(dsize);
177 freesize /= 2;
178
179 if (size && freesize < size) {
180 fprintf(stderr, Name ": %s is smaller that given size."
181 " %luK < %luK + superblock\n", dname, freesize, size);
182 fail = 1;
183 close(fd);
184 continue;
185 }
186 if (maxdisc == NULL || (maxdisc && freesize > maxsize)) {
187 maxdisc = dname;
188 maxsize = freesize;
189 }
190 if (mindisc ==NULL || (mindisc && freesize < minsize)) {
191 mindisc = dname;
192 minsize = freesize;
193 }
194 warn |= check_ext2(fd, dname);
195 warn |= check_reiser(fd, dname);
196 warn |= check_raid(fd, dname);
197 close(fd);
198 }
199 if (fail) {
200 fprintf(stderr, Name ": create aborted\n");
201 return 1;
202 }
203 if (size == 0) {
204 if (mindisc == NULL) {
205 fprintf(stderr, Name ": no size and no drives given - aborting create.\n");
206 return 1;
207 }
208 size = minsize;
209 if (verbose && level>0)
210 fprintf(stderr, Name ": size set to %luK\n", size);
211 }
212 if (level >= 1 && ((maxsize-size)*100 > maxsize)) {
213 fprintf(stderr, Name ": largest drive (%s) exceed size (%luK) by more than 1%%\n",
214 maxdisc, size);
215 warn = 1;
216 }
217
218 if (warn) {
219 if (runstop!= 1) {
220 if (!ask("Continue creating array? ")) {
221 fprintf(stderr, Name ": create aborted.\n");
222 return 1;
223 }
224 } else {
225 if (verbose)
226 fprintf(stderr, Name ": creation continuing despite oddities due to --run\n");
227 }
228 }
229
230 /* If this is raid5, we want to configure the last active slot
231 * as missing, so that a reconstruct happens (faster than re-parity)
232 */
233 if (force == 0 && level == 5 && first_missing >= raiddisks) {
234 insert_point = raiddisks-1;
235 sparedisks++;
236 array.active_disks--;
237 missing_disks++;
238 }
239
240 /* Ok, lets try some ioctls */
241
242 array.level = level;
243 array.size = size;
244 array.raid_disks = raiddisks;
245 /* The kernel should *know* what md_minor we are dealing
246 * with, but it chooses to trust me instead. Sigh
247 */
248 array.md_minor = 0;
249 if (fstat(mdfd, &stb)==0)
250 array.md_minor = MINOR(stb.st_rdev);
251 array.not_persistent = 0;
252 if (level == 5 && (insert_point < raiddisks || first_missing < raiddisks))
253 array.state = 1; /* clean, but one drive will be missing */
254 else
255 array.state = 0; /* not clean, but no errors */
256
257 /* There is lots of redundancy in these disk counts,
258 * raid_disks is the most meaningful value
259 * it describes the geometry of the array
260 * it is constant
261 * nr_disks is total number of used slots.
262 * it should be raid_disks+spare_disks
263 * spare_disks is the number of extra disks present
264 * see above
265 * active_disks is the number of working disks in
266 * active slots. (With raid_disks)
267 * working_disks is the total number of working disks,
268 * including spares
269 * failed_disks is the number of disks marked failed
270 *
271 * Ideally, the kernel would keep these (except raid_disks)
272 * up-to-date as we ADD_NEW_DISK, but it doesn't (yet).
273 * So for now, we assume that all raid and spare
274 * devices will be given.
275 */
276 array.spare_disks=sparedisks;
277 array.failed_disks=missing_disks;
278 array.nr_disks = array.working_disks + array.failed_disks;
279 array.layout = layout;
280 array.chunk_size = chunk*1024;
281
282 if (ioctl(mdfd, SET_ARRAY_INFO, &array)) {
283 fprintf(stderr, Name ": SET_ARRAY_INFO failed for %s: %s\n",
284 mddev, strerror(errno));
285 return 1;
286 }
287
288 for (dnum=0, dv = devlist ; dv ; dv=(dv->next)?(dv->next):moved_disk, dnum++) {
289 int fd;
290 struct stat stb;
291 mdu_disk_info_t disk;
292
293 disk.number = dnum;
294 if (dnum == insert_point) {
295 moved_disk = dv;
296 }
297 disk.raid_disk = disk.number;
298 if (disk.raid_disk < raiddisks)
299 disk.state = 6; /* active and in sync */
300 else
301 disk.state = 0;
302 if (dnum == insert_point ||
303 strcasecmp(dv->devname, "missing")==0) {
304 disk.major = 0;
305 disk.minor = 0;
306 disk.state = 1; /* faulty */
307 } else {
308 fd = open(dv->devname, O_RDONLY, 0);
309 if (fd < 0) {
310 fprintf(stderr, Name ": failed to open %s after earlier success - aborting\n",
311 dv->devname);
312 return 1;
313 }
314 fstat(fd, &stb);
315 disk.major = MAJOR(stb.st_rdev);
316 disk.minor = MINOR(stb.st_rdev);
317 close(fd);
318 }
319 if (ioctl(mdfd, ADD_NEW_DISK, &disk)) {
320 fprintf(stderr, Name ": ADD_NEW_DISK for %s failed: %s\n",
321 dv->devname, strerror(errno));
322 return 1;
323 }
324 if (dv == moved_disk && dnum != insert_point) break;
325 }
326
327 /* param is not actually used */
328 if (runstop == 1 || subdevs >= raiddisks) {
329 mdu_param_t param;
330 if (ioctl(mdfd, RUN_ARRAY, &param)) {
331 fprintf(stderr, Name ": RUN_ARRAY failed: %s\n",
332 strerror(errno));
333 return 1;
334 }
335 fprintf(stderr, Name ": array %s started.\n", mddev);
336 } else {
337 fprintf(stderr, Name ": not starting array - not enough devices.\n");
338 }
339 return 0;
340 }