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