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