]> git.ipfire.org Git - thirdparty/mdadm.git/blob - Create.c
Assorted fixes...
[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(struct supertype *st, char *mddev, int mdfd,
35 int chunk, int level, int layout, unsigned long size, int raiddisks, int sparedisks,
36 char *name,
37 int subdevs, mddev_dev_t devlist,
38 int runstop, int verbose, int force, int assume_clean,
39 char *bitmap_file, int bitmap_chunk, int write_behind, int delay)
40 {
41 /*
42 * Create a new raid array.
43 *
44 * First check that necessary details are available
45 * (i.e. level, raid-disks)
46 *
47 * Then check each disk to see what might be on it
48 * and report anything interesting.
49 *
50 * If anything looks odd, and runstop not set,
51 * abort.
52 *
53 * SET_ARRAY_INFO and ADD_NEW_DISK, and
54 * if runstop==run, or raiddisks disks were used,
55 * RUN_ARRAY
56 */
57 unsigned long long minsize=0, maxsize=0;
58 char *mindisc = NULL;
59 char *maxdisc = NULL;
60 int dnum;
61 mddev_dev_t dv;
62 int fail=0, warn=0;
63 struct stat stb;
64 int first_missing = subdevs * 2;
65 int missing_disks = 0;
66 int insert_point = subdevs * 2; /* where to insert a missing drive */
67 void *super;
68 int pass;
69 int vers;
70 int rv;
71 int bitmap_fd;
72 unsigned long long bitmapsize;
73
74 mdu_array_info_t array;
75 int major = BITMAP_MAJOR_HI;
76
77 vers = md_get_version(mdfd);
78 if (vers < 9000) {
79 fprintf(stderr, Name ": Create requires md driver version 0.90.0 or later\n");
80 return 1;
81 }
82 if (level == UnSet) {
83 fprintf(stderr,
84 Name ": a RAID level is needed to create an array.\n");
85 return 1;
86 }
87 if (raiddisks < 1) {
88 fprintf(stderr,
89 Name ": a number of --raid-devices must be given to create an array\n");
90 return 1;
91 }
92 if (raiddisks < 4 && level == 6) {
93 fprintf(stderr,
94 Name ": at least 4 raid-devices needed for level 6\n");
95 return 1;
96 }
97 if (raiddisks > 256 && level == 6) {
98 fprintf(stderr,
99 Name ": no more than 256 raid-devices supported for level 6\n");
100 return 1;
101 }
102 if (raiddisks < 2 && level >= 4) {
103 fprintf(stderr,
104 Name ": at least 2 raid-devices needed for level 4 or 5\n");
105 return 1;
106 }
107 if (subdevs > raiddisks+sparedisks) {
108 fprintf(stderr, Name ": You have listed more devices (%d) than are in the array(%d)!\n", subdevs, raiddisks+sparedisks);
109 return 1;
110 }
111 if (subdevs < raiddisks+sparedisks) {
112 fprintf(stderr, Name ": You haven't given enough devices (real or missing) to create this array\n");
113 return 1;
114 }
115
116 /* now set some defaults */
117 if (layout == UnSet)
118 switch(level) {
119 default: /* no layout */
120 layout = 0;
121 break;
122 case 10:
123 layout = 0x102; /* near=2, far=1 */
124 if (verbose > 0)
125 fprintf(stderr,
126 Name ": layout defaults to n1\n");
127 break;
128 case 5:
129 case 6:
130 layout = map_name(r5layout, "default");
131 if (verbose > 0)
132 fprintf(stderr,
133 Name ": layout defaults to %s\n", map_num(r5layout, layout));
134 break;
135 case LEVEL_FAULTY:
136 layout = map_name(faultylayout, "default");
137
138 if (verbose > 0)
139 fprintf(stderr,
140 Name ": layout defaults to %s\n", map_num(faultylayout, layout));
141 break;
142 }
143
144 if (level == 10)
145 /* check layout fits in array*/
146 if ((layout&255) * ((layout>>8)&255) > raiddisks) {
147 fprintf(stderr, Name ": that layout requires at least %d devices\n",
148 (layout&255) * ((layout>>8)&255));
149 return 1;
150 }
151
152 switch(level) {
153 case 4:
154 case 5:
155 case 10:
156 case 6:
157 case 0:
158 case -1: /* linear */
159 if (chunk == 0) {
160 chunk = 64;
161 if (verbose > 0)
162 fprintf(stderr, Name ": chunk size defaults to 64K\n");
163 }
164 break;
165 default: /* raid1, multipath */
166 if (chunk) {
167 chunk = 0;
168 if (verbose > 0)
169 fprintf(stderr, Name ": chunk size ignored for this level\n");
170 }
171 break;
172 }
173
174 /* now look at the subdevs */
175 array.active_disks = 0;
176 array.working_disks = 0;
177 dnum = 0;
178 for (dv=devlist; dv; dv=dv->next, dnum++) {
179 char *dname = dv->devname;
180 unsigned long dsize;
181 unsigned long long ldsize, freesize;
182 int fd;
183 if (strcasecmp(dname, "missing")==0) {
184 if (first_missing > dnum)
185 first_missing = dnum;
186 missing_disks ++;
187 continue;
188 }
189 array.working_disks++;
190 if (dnum < raiddisks)
191 array.active_disks++;
192 fd = open(dname, O_RDONLY|O_EXCL, 0);
193 if (fd <0 ) {
194 fprintf(stderr, Name ": Cannot open %s: %s\n",
195 dname, strerror(errno));
196 fail=1;
197 continue;
198 }
199 #ifdef BLKGETSIZE64
200 if (ioctl(fd, BLKGETSIZE64, &ldsize)==0)
201 ;
202 else
203 #endif
204 if (ioctl(fd, BLKGETSIZE, &dsize)) {
205 fprintf(stderr, Name ": Cannot get size of %s: %s\n",
206 dname, strerror(errno));
207 fail = 1;
208 close(fd);
209 continue;
210 }
211 else {
212 ldsize = dsize;
213 ldsize <<= 9;
214 }
215 freesize = st->ss->avail_size(st, ldsize >> 9);
216 if (freesize == 0) {
217 fprintf(stderr, Name ": %s is too small: %luK\n",
218 dname, (unsigned long)(ldsize>>10));
219 fail = 1;
220 close(fd);
221 continue;
222 }
223
224 freesize /= 2; /* convert to K */
225 if (chunk) {
226 /* round to chunk size */
227 freesize = freesize & ~(chunk-1);
228 }
229
230 if (size && freesize < size) {
231 fprintf(stderr, Name ": %s is smaller that given size."
232 " %lluK < %luK + superblock\n", dname, freesize, size);
233 fail = 1;
234 close(fd);
235 continue;
236 }
237 if (maxdisc == NULL || (maxdisc && freesize > maxsize)) {
238 maxdisc = dname;
239 maxsize = freesize;
240 }
241 if (mindisc ==NULL || (mindisc && freesize < minsize)) {
242 mindisc = dname;
243 minsize = freesize;
244 }
245 if (runstop != 1 || verbose >= 0) {
246 warn |= check_ext2(fd, dname);
247 warn |= check_reiser(fd, dname);
248 warn |= check_raid(fd, dname);
249 }
250 close(fd);
251 }
252 if (fail) {
253 fprintf(stderr, Name ": create aborted\n");
254 return 1;
255 }
256 if (size == 0) {
257 if (mindisc == NULL) {
258 fprintf(stderr, Name ": no size and no drives given - aborting create.\n");
259 return 1;
260 }
261 if (level > 0 || level == LEVEL_MULTIPATH || level == LEVEL_FAULTY) {
262 /* size is meaningful */
263 if (minsize > 0x100000000ULL) {
264 fprintf(stderr, Name ": devices too large for RAID level %d\n", level);
265 return 1;
266 }
267 size = minsize;
268 if (verbose > 0)
269 fprintf(stderr, Name ": size set to %luK\n", size);
270 }
271 }
272 if (level > 0 && ((maxsize-size)*100 > maxsize)) {
273 if (runstop != 1 || verbose >= 0)
274 fprintf(stderr, Name ": largest drive (%s) exceed size (%luK) by more than 1%%\n",
275 maxdisc, size);
276 warn = 1;
277 }
278
279 if (warn) {
280 if (runstop!= 1) {
281 if (!ask("Continue creating array? ")) {
282 fprintf(stderr, Name ": create aborted.\n");
283 return 1;
284 }
285 } else {
286 if (verbose > 0)
287 fprintf(stderr, Name ": creation continuing despite oddities due to --run\n");
288 }
289 }
290
291 /* If this is raid5, we want to configure the last active slot
292 * as missing, so that a reconstruct happens (faster than re-parity)
293 * FIX: Can we do this for raid6 as well?
294 */
295 if (assume_clean==0 && force == 0 && first_missing >= raiddisks) {
296 switch ( level ) {
297 case 5:
298 insert_point = raiddisks-1;
299 sparedisks++;
300 array.active_disks--;
301 missing_disks++;
302 break;
303 default:
304 break;
305 }
306 }
307
308 /* Ok, lets try some ioctls */
309
310 array.level = level;
311 array.size = size;
312 array.raid_disks = raiddisks;
313 /* The kernel should *know* what md_minor we are dealing
314 * with, but it chooses to trust me instead. Sigh
315 */
316 array.md_minor = 0;
317 if (fstat(mdfd, &stb)==0)
318 array.md_minor = minor(stb.st_rdev);
319 array.not_persistent = 0;
320 /*** FIX: Need to do something about RAID-6 here ***/
321 if ( ( (level == 5) &&
322 (insert_point < raiddisks || first_missing < raiddisks) )
323 ||
324 ( level == 6 && missing_disks == 2)
325 ||
326 assume_clean
327 )
328 array.state = 1; /* clean, but one+ drive will be missing */
329 else
330 array.state = 0; /* not clean, but no errors */
331
332 if (level == 10) {
333 /* for raid10, the bitmap size is the capacity of the array,
334 * which is array.size * raid_disks / ncopies;
335 * .. but convert to sectors.
336 */
337 int ncopies = (layout>>8) * (layout & 255);
338 bitmapsize = (unsigned long long)array.size * raiddisks / ncopies * 2;
339 printf("bms=%llu as=%d rd=%d nc=%d\n", bitmapsize, array.size, raiddisks, ncopies);
340 } else
341 bitmapsize = (unsigned long long)array.size * 2;
342
343 /* There is lots of redundancy in these disk counts,
344 * raid_disks is the most meaningful value
345 * it describes the geometry of the array
346 * it is constant
347 * nr_disks is total number of used slots.
348 * it should be raid_disks+spare_disks
349 * spare_disks is the number of extra disks present
350 * see above
351 * active_disks is the number of working disks in
352 * active slots. (With raid_disks)
353 * working_disks is the total number of working disks,
354 * including spares
355 * failed_disks is the number of disks marked failed
356 *
357 * Ideally, the kernel would keep these (except raid_disks)
358 * up-to-date as we ADD_NEW_DISK, but it doesn't (yet).
359 * So for now, we assume that all raid and spare
360 * devices will be given.
361 */
362 array.spare_disks=sparedisks;
363 array.failed_disks=missing_disks;
364 array.nr_disks = array.working_disks + array.failed_disks;
365 array.layout = layout;
366 array.chunk_size = chunk*1024;
367 array.major_version = st->ss->major;
368
369 if (!st->ss->init_super(st, &super, &array, name))
370 return 1;
371
372 if (bitmap_file && vers < 9003) {
373 major = BITMAP_MAJOR_HOSTENDIAN;
374 #ifdef __BIG_ENDIAN
375 fprintf(stderr, Name ": Warning - bitmaps created on this kernel are not portable\n"
376 " between different architectured. Consider upgrading the Linux kernel.\n");
377 #endif
378 }
379
380 if (bitmap_file && strcmp(bitmap_file, "internal")==0) {
381 if ((vers%100) < 2) {
382 fprintf(stderr, Name ": internal bitmaps not supported by this kernel.\n");
383 return 1;
384 }
385 if (!st->ss->add_internal_bitmap(st, super, bitmap_chunk, delay, write_behind,
386 bitmapsize, 1, major)) {
387 fprintf(stderr, Name ": Given bitmap chunk size not supported.\n");
388 return 1;
389 }
390 bitmap_file = NULL;
391 }
392
393
394
395 if ((vers % 100) >= 1) { /* can use different versions */
396 mdu_array_info_t inf;
397 memset(&inf, 0, sizeof(inf));
398 inf.major_version = st->ss->major;
399 inf.minor_version = st->minor_version;
400 rv = ioctl(mdfd, SET_ARRAY_INFO, &inf);
401 } else
402 rv = ioctl(mdfd, SET_ARRAY_INFO, NULL);
403 if (rv) {
404 fprintf(stderr, Name ": SET_ARRAY_INFO failed for %s: %s\n",
405 mddev, strerror(errno));
406 return 1;
407 }
408
409 if (bitmap_file) {
410 int uuid[4];
411
412 if (bitmap_chunk == UnSet)
413 bitmap_chunk = DEFAULT_BITMAP_CHUNK;
414
415 st->ss->uuid_from_super(uuid, super);
416 if (CreateBitmap(bitmap_file, force, (char*)uuid, bitmap_chunk,
417 delay, write_behind,
418 bitmapsize,
419 major)) {
420 return 1;
421 }
422 bitmap_fd = open(bitmap_file, O_RDWR);
423 if (bitmap_fd < 0) {
424 fprintf(stderr, Name ": weird: %s cannot be openned\n",
425 bitmap_file);
426 return 1;
427 }
428 if (ioctl(mdfd, SET_BITMAP_FILE, bitmap_fd) < 0) {
429 fprintf(stderr, Name ": Cannot set bitmap file for %s: %s\n",
430 mddev, strerror(errno));
431 return 1;
432 }
433 }
434
435
436
437 for (pass=1; pass <=2 ; pass++) {
438 mddev_dev_t moved_disk = NULL; /* the disk that was moved out of the insert point */
439
440 for (dnum=0, dv = devlist ; dv ;
441 dv=(dv->next)?(dv->next):moved_disk, dnum++) {
442 int fd;
443 struct stat stb;
444 mdu_disk_info_t disk;
445
446 disk.number = dnum;
447 if (dnum == insert_point) {
448 moved_disk = dv;
449 }
450 disk.raid_disk = disk.number;
451 if (disk.raid_disk < raiddisks)
452 disk.state = (1<<MD_DISK_ACTIVE) |
453 (1<<MD_DISK_SYNC);
454 else
455 disk.state = 0;
456 if (dv->writemostly)
457 disk.state |= (1<<MD_DISK_WRITEMOSTLY);
458
459 if (dnum == insert_point ||
460 strcasecmp(dv->devname, "missing")==0) {
461 disk.major = 0;
462 disk.minor = 0;
463 disk.state = (1<<MD_DISK_FAULTY);
464 } else {
465 fd = open(dv->devname, O_RDONLY|O_EXCL, 0);
466 if (fd < 0) {
467 fprintf(stderr, Name ": failed to open %s after earlier success - aborting\n",
468 dv->devname);
469 return 1;
470 }
471 fstat(fd, &stb);
472 disk.major = major(stb.st_rdev);
473 disk.minor = minor(stb.st_rdev);
474 close(fd);
475 }
476 switch(pass){
477 case 1:
478 st->ss->add_to_super(super, &disk);
479 break;
480 case 2:
481 if (disk.state == 1) break;
482 st->ss->write_init_super(st, super, &disk, dv->devname);
483
484 if (ioctl(mdfd, ADD_NEW_DISK, &disk)) {
485 fprintf(stderr, Name ": ADD_NEW_DISK for %s failed: %s\n",
486 dv->devname, strerror(errno));
487 free(super);
488 return 1;
489 }
490
491 break;
492 }
493 if (dv == moved_disk && dnum != insert_point) break;
494 }
495 }
496 free(super);
497
498 /* param is not actually used */
499 if (runstop == 1 || subdevs >= raiddisks) {
500 mdu_param_t param;
501 if (ioctl(mdfd, RUN_ARRAY, &param)) {
502 fprintf(stderr, Name ": RUN_ARRAY failed: %s\n",
503 strerror(errno));
504 Manage_runstop(mddev, mdfd, -1, 0);
505 return 1;
506 }
507 if (verbose >= 0)
508 fprintf(stderr, Name ": array %s started.\n", mddev);
509 } else {
510 fprintf(stderr, Name ": not starting array - not enough devices.\n");
511 }
512 return 0;
513 }