]>
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 | #include "md_u.h" | |
32 | #include "md_p.h" | |
33 | #include <ctype.h> | |
34 | ||
35 | static int default_layout(struct supertype *st, int level, int verbose) | |
36 | { | |
37 | int layout = UnSet; | |
38 | ||
39 | if (st && st->ss->default_layout) | |
40 | layout = st->ss->default_layout(level); | |
41 | ||
42 | if (layout == UnSet) | |
43 | switch(level) { | |
44 | default: /* no layout */ | |
45 | layout = 0; | |
46 | break; | |
47 | case 10: | |
48 | layout = 0x102; /* near=2, far=1 */ | |
49 | if (verbose > 0) | |
50 | fprintf(stderr, | |
51 | Name ": layout defaults to n1\n"); | |
52 | break; | |
53 | case 5: | |
54 | case 6: | |
55 | layout = map_name(r5layout, "default"); | |
56 | if (verbose > 0) | |
57 | fprintf(stderr, | |
58 | Name ": layout defaults to %s\n", map_num(r5layout, layout)); | |
59 | break; | |
60 | case LEVEL_FAULTY: | |
61 | layout = map_name(faultylayout, "default"); | |
62 | ||
63 | if (verbose > 0) | |
64 | fprintf(stderr, | |
65 | Name ": layout defaults to %s\n", map_num(faultylayout, layout)); | |
66 | break; | |
67 | } | |
68 | ||
69 | return layout; | |
70 | } | |
71 | ||
72 | ||
73 | int Create(struct supertype *st, char *mddev, | |
74 | int chunk, int level, int layout, unsigned long long size, int raiddisks, int sparedisks, | |
75 | char *name, char *homehost, int *uuid, | |
76 | int subdevs, mddev_dev_t devlist, | |
77 | int runstop, int verbose, int force, int assume_clean, | |
78 | char *bitmap_file, int bitmap_chunk, int write_behind, int delay, int autof) | |
79 | { | |
80 | /* | |
81 | * Create a new raid array. | |
82 | * | |
83 | * First check that necessary details are available | |
84 | * (i.e. level, raid-disks) | |
85 | * | |
86 | * Then check each disk to see what might be on it | |
87 | * and report anything interesting. | |
88 | * | |
89 | * If anything looks odd, and runstop not set, | |
90 | * abort. | |
91 | * | |
92 | * SET_ARRAY_INFO and ADD_NEW_DISK, and | |
93 | * if runstop==run, or raiddisks disks were used, | |
94 | * RUN_ARRAY | |
95 | */ | |
96 | int mdfd; | |
97 | unsigned long long minsize=0, maxsize=0; | |
98 | char *mindisc = NULL; | |
99 | char *maxdisc = NULL; | |
100 | int dnum; | |
101 | mddev_dev_t dv; | |
102 | int fail=0, warn=0; | |
103 | struct stat stb; | |
104 | int first_missing = subdevs * 2; | |
105 | int second_missing = subdevs * 2; | |
106 | int missing_disks = 0; | |
107 | int insert_point = subdevs * 2; /* where to insert a missing drive */ | |
108 | int total_slots; | |
109 | int pass; | |
110 | int vers; | |
111 | int rv; | |
112 | int bitmap_fd; | |
113 | int have_container = 0; | |
114 | int container_fd = -1; | |
115 | int need_mdmon = 0; | |
116 | unsigned long long bitmapsize; | |
117 | struct mdinfo info, *infos; | |
118 | int did_default = 0; | |
119 | int do_default_layout = 0; | |
120 | unsigned long safe_mode_delay = 0; | |
121 | char chosen_name[1024]; | |
122 | struct map_ent *map = NULL; | |
123 | unsigned long long newsize; | |
124 | ||
125 | int major_num = BITMAP_MAJOR_HI; | |
126 | ||
127 | memset(&info, 0, sizeof(info)); | |
128 | ||
129 | if (level == UnSet) { | |
130 | /* "ddf" and "imsm" metadata only supports one level - should possibly | |
131 | * push this into metadata handler?? | |
132 | */ | |
133 | if (st && (st->ss == &super_ddf || st->ss == &super_imsm)) | |
134 | level = LEVEL_CONTAINER; | |
135 | } | |
136 | ||
137 | if (level == UnSet) { | |
138 | fprintf(stderr, | |
139 | Name ": a RAID level is needed to create an array.\n"); | |
140 | return 1; | |
141 | } | |
142 | if (raiddisks < 4 && level == 6) { | |
143 | fprintf(stderr, | |
144 | Name ": at least 4 raid-devices needed for level 6\n"); | |
145 | return 1; | |
146 | } | |
147 | if (raiddisks > 256 && level == 6) { | |
148 | fprintf(stderr, | |
149 | Name ": no more than 256 raid-devices supported for level 6\n"); | |
150 | return 1; | |
151 | } | |
152 | if (raiddisks < 2 && level >= 4) { | |
153 | fprintf(stderr, | |
154 | Name ": at least 2 raid-devices needed for level 4 or 5\n"); | |
155 | return 1; | |
156 | } | |
157 | if (level <= 0 && sparedisks) { | |
158 | fprintf(stderr, | |
159 | Name ": This level does not support spare devices\n"); | |
160 | return 1; | |
161 | } | |
162 | ||
163 | if (subdevs == 1 && strcmp(devlist->devname, "missing") != 0) { | |
164 | /* If given a single device, it might be a container, and we can | |
165 | * extract a device list from there | |
166 | */ | |
167 | mdu_array_info_t inf; | |
168 | int fd; | |
169 | ||
170 | memset(&inf, 0, sizeof(inf)); | |
171 | fd = open(devlist->devname, O_RDONLY); | |
172 | if (fd >= 0 && | |
173 | ioctl(fd, GET_ARRAY_INFO, &inf) == 0 && | |
174 | inf.raid_disks == 0) { | |
175 | /* yep, looks like a container */ | |
176 | if (st) { | |
177 | rv = st->ss->load_super(st, fd, | |
178 | devlist->devname); | |
179 | if (rv == 0) | |
180 | have_container = 1; | |
181 | } else { | |
182 | st = guess_super(fd); | |
183 | if (st && !(rv = st->ss-> | |
184 | load_super(st, fd, | |
185 | devlist->devname))) | |
186 | have_container = 1; | |
187 | else | |
188 | st = NULL; | |
189 | } | |
190 | if (have_container) { | |
191 | subdevs = raiddisks; | |
192 | first_missing = subdevs * 2; | |
193 | second_missing = subdevs * 2; | |
194 | insert_point = subdevs * 2; | |
195 | } | |
196 | } | |
197 | if (fd >= 0) | |
198 | close(fd); | |
199 | } | |
200 | if (st && st->ss->external && sparedisks) { | |
201 | fprintf(stderr, | |
202 | Name ": This metadata type does not support " | |
203 | "spare disks are create time\n"); | |
204 | return 1; | |
205 | } | |
206 | if (subdevs > raiddisks+sparedisks) { | |
207 | fprintf(stderr, Name ": You have listed more devices (%d) than are in the array(%d)!\n", subdevs, raiddisks+sparedisks); | |
208 | return 1; | |
209 | } | |
210 | if (!have_container && subdevs < raiddisks+sparedisks) { | |
211 | fprintf(stderr, Name ": You haven't given enough devices (real or missing) to create this array\n"); | |
212 | return 1; | |
213 | } | |
214 | if (bitmap_file && level <= 0) { | |
215 | fprintf(stderr, Name ": bitmaps not meaningful with level %s\n", | |
216 | map_num(pers, level)?:"given"); | |
217 | return 1; | |
218 | } | |
219 | ||
220 | /* now set some defaults */ | |
221 | ||
222 | ||
223 | if (layout == UnSet) { | |
224 | do_default_layout = 1; | |
225 | layout = default_layout(st, level, verbose); | |
226 | } | |
227 | ||
228 | if (level == 10) | |
229 | /* check layout fits in array*/ | |
230 | if ((layout&255) * ((layout>>8)&255) > raiddisks) { | |
231 | fprintf(stderr, Name ": that layout requires at least %d devices\n", | |
232 | (layout&255) * ((layout>>8)&255)); | |
233 | return 1; | |
234 | } | |
235 | ||
236 | switch(level) { | |
237 | case 4: | |
238 | case 5: | |
239 | case 10: | |
240 | case 6: | |
241 | case 0: | |
242 | case LEVEL_LINEAR: /* linear */ | |
243 | if (chunk == 0) { | |
244 | chunk = 64; | |
245 | if (verbose > 0) | |
246 | fprintf(stderr, Name ": chunk size defaults to 64K\n"); | |
247 | } | |
248 | break; | |
249 | case 1: | |
250 | case LEVEL_FAULTY: | |
251 | case LEVEL_MULTIPATH: | |
252 | case LEVEL_CONTAINER: | |
253 | if (chunk) { | |
254 | chunk = 0; | |
255 | if (verbose > 0) | |
256 | fprintf(stderr, Name ": chunk size ignored for this level\n"); | |
257 | } | |
258 | break; | |
259 | default: | |
260 | fprintf(stderr, Name ": unknown level %d\n", level); | |
261 | return 1; | |
262 | } | |
263 | ||
264 | if (size && chunk) | |
265 | size &= ~(unsigned long long)(chunk - 1); | |
266 | newsize = size * 2; | |
267 | if (st && ! st->ss->validate_geometry(st, level, layout, raiddisks, | |
268 | chunk, size*2, NULL, &newsize, verbose>=0)) | |
269 | return 1; | |
270 | if (size == 0) { | |
271 | size = newsize / 2; | |
272 | if (size && verbose > 0) | |
273 | fprintf(stderr, Name ": setting size to %lluK\n", | |
274 | (unsigned long long)size); | |
275 | } | |
276 | ||
277 | /* now look at the subdevs */ | |
278 | info.array.active_disks = 0; | |
279 | info.array.working_disks = 0; | |
280 | dnum = 0; | |
281 | for (dv=devlist; dv && !have_container; dv=dv->next, dnum++) { | |
282 | char *dname = dv->devname; | |
283 | unsigned long long freesize; | |
284 | if (strcasecmp(dname, "missing")==0) { | |
285 | if (first_missing > dnum) | |
286 | first_missing = dnum; | |
287 | if (second_missing > dnum && dnum > first_missing) | |
288 | second_missing = dnum; | |
289 | missing_disks ++; | |
290 | continue; | |
291 | } | |
292 | info.array.working_disks++; | |
293 | if (dnum < raiddisks) | |
294 | info.array.active_disks++; | |
295 | if (st == NULL) { | |
296 | struct createinfo *ci = conf_get_create_info(); | |
297 | if (ci) | |
298 | st = ci->supertype; | |
299 | } | |
300 | if (st == NULL) { | |
301 | /* Need to choose a default metadata, which is different | |
302 | * depending on geometry of array. | |
303 | */ | |
304 | int i; | |
305 | char *name = "default"; | |
306 | for(i=0; !st && superlist[i]; i++) { | |
307 | st = superlist[i]->match_metadata_desc(name); | |
308 | if (do_default_layout) | |
309 | layout = default_layout(st, level, verbose); | |
310 | if (st && !st->ss->validate_geometry | |
311 | (st, level, layout, raiddisks, | |
312 | chunk, size*2, dname, &freesize, | |
313 | verbose > 0)) | |
314 | st = NULL; | |
315 | } | |
316 | ||
317 | if (!st) { | |
318 | fprintf(stderr, Name ": device %s not suitable " | |
319 | "for any style of array\n", | |
320 | dname); | |
321 | exit(2); | |
322 | } | |
323 | if (st->ss != &super0 || | |
324 | st->minor_version != 90) | |
325 | did_default = 1; | |
326 | } else { | |
327 | if (do_default_layout) | |
328 | layout = default_layout(st, level, verbose); | |
329 | if (!st->ss->validate_geometry(st, level, layout, | |
330 | raiddisks, | |
331 | chunk, size*2, dname, | |
332 | &freesize, | |
333 | verbose > 0)) { | |
334 | ||
335 | fprintf(stderr, | |
336 | Name ": %s is not suitable for " | |
337 | "this array.\n", | |
338 | dname); | |
339 | fail = 1; | |
340 | continue; | |
341 | } | |
342 | } | |
343 | ||
344 | freesize /= 2; /* convert to K */ | |
345 | if (chunk) { | |
346 | /* round to chunk size */ | |
347 | freesize = freesize & ~(chunk-1); | |
348 | } | |
349 | ||
350 | if (size && freesize < size) { | |
351 | fprintf(stderr, Name ": %s is smaller than given size." | |
352 | " %lluK < %lluK + metadata\n", | |
353 | dname, freesize, size); | |
354 | fail = 1; | |
355 | continue; | |
356 | } | |
357 | if (maxdisc == NULL || (maxdisc && freesize > maxsize)) { | |
358 | maxdisc = dname; | |
359 | maxsize = freesize; | |
360 | } | |
361 | if (mindisc ==NULL || (mindisc && freesize < minsize)) { | |
362 | mindisc = dname; | |
363 | minsize = freesize; | |
364 | } | |
365 | if (runstop != 1 || verbose >= 0) { | |
366 | int fd = open(dname, O_RDONLY); | |
367 | if (fd <0 ) { | |
368 | fprintf(stderr, Name ": Cannot open %s: %s\n", | |
369 | dname, strerror(errno)); | |
370 | fail=1; | |
371 | continue; | |
372 | } | |
373 | warn |= check_ext2(fd, dname); | |
374 | warn |= check_reiser(fd, dname); | |
375 | warn |= check_raid(fd, dname); | |
376 | close(fd); | |
377 | } | |
378 | } | |
379 | if (have_container) | |
380 | info.array.working_disks = raiddisks; | |
381 | if (fail) { | |
382 | fprintf(stderr, Name ": create aborted\n"); | |
383 | return 1; | |
384 | } | |
385 | if (size == 0) { | |
386 | if (mindisc == NULL && !have_container) { | |
387 | fprintf(stderr, Name ": no size and no drives given - aborting create.\n"); | |
388 | return 1; | |
389 | } | |
390 | if (level > 0 || level == LEVEL_MULTIPATH | |
391 | || level == LEVEL_FAULTY | |
392 | || st->ss->external ) { | |
393 | /* size is meaningful */ | |
394 | if (!st->ss->validate_geometry(st, level, layout, | |
395 | raiddisks, | |
396 | chunk, minsize*2, | |
397 | NULL, NULL, 0)) { | |
398 | fprintf(stderr, Name ": devices too large for RAID level %d\n", level); | |
399 | return 1; | |
400 | } | |
401 | size = minsize; | |
402 | if (verbose > 0) | |
403 | fprintf(stderr, Name ": size set to %lluK\n", size); | |
404 | } | |
405 | } | |
406 | if (!have_container && level > 0 && ((maxsize-size)*100 > maxsize)) { | |
407 | if (runstop != 1 || verbose >= 0) | |
408 | fprintf(stderr, Name ": largest drive (%s) exceeds size (%lluK) by more than 1%%\n", | |
409 | maxdisc, size); | |
410 | warn = 1; | |
411 | } | |
412 | ||
413 | if (st->ss->detail_platform && st->ss->detail_platform(0, 1) != 0) { | |
414 | if (runstop != 1 || verbose >= 0) | |
415 | fprintf(stderr, Name ": %s unable to enumerate platform support\n" | |
416 | " array may not be compatible with hardware/firmware\n", | |
417 | st->ss->name); | |
418 | warn = 1; | |
419 | } | |
420 | ||
421 | if (warn) { | |
422 | if (runstop!= 1) { | |
423 | if (!ask("Continue creating array? ")) { | |
424 | fprintf(stderr, Name ": create aborted.\n"); | |
425 | return 1; | |
426 | } | |
427 | } else { | |
428 | if (verbose > 0) | |
429 | fprintf(stderr, Name ": creation continuing despite oddities due to --run\n"); | |
430 | } | |
431 | } | |
432 | ||
433 | /* If this is raid4/5, we want to configure the last active slot | |
434 | * as missing, so that a reconstruct happens (faster than re-parity) | |
435 | * FIX: Can we do this for raid6 as well? | |
436 | */ | |
437 | if (st->ss->external == 0 && | |
438 | assume_clean==0 && force == 0 && first_missing >= raiddisks) { | |
439 | switch ( level ) { | |
440 | case 4: | |
441 | case 5: | |
442 | insert_point = raiddisks-1; | |
443 | sparedisks++; | |
444 | info.array.active_disks--; | |
445 | missing_disks++; | |
446 | break; | |
447 | default: | |
448 | break; | |
449 | } | |
450 | } | |
451 | /* For raid6, if creating with 1 missing drive, make a good drive | |
452 | * into a spare, else the create will fail | |
453 | */ | |
454 | if (assume_clean == 0 && force == 0 && first_missing < raiddisks && | |
455 | st->ss->external == 0 && | |
456 | second_missing >= raiddisks && level == 6) { | |
457 | insert_point = raiddisks - 1; | |
458 | if (insert_point == first_missing) | |
459 | insert_point--; | |
460 | sparedisks ++; | |
461 | info.array.active_disks--; | |
462 | missing_disks++; | |
463 | } | |
464 | ||
465 | if (level <= 0 && first_missing < subdevs * 2) { | |
466 | fprintf(stderr, | |
467 | Name ": This level does not support missing devices\n"); | |
468 | return 1; | |
469 | } | |
470 | ||
471 | /* We need to create the device */ | |
472 | map_lock(&map); | |
473 | mdfd = create_mddev(mddev, name, autof, LOCAL, chosen_name); | |
474 | if (mdfd < 0) | |
475 | return 1; | |
476 | mddev = chosen_name; | |
477 | ||
478 | vers = md_get_version(mdfd); | |
479 | if (vers < 9000) { | |
480 | fprintf(stderr, Name ": Create requires md driver version 0.90.0 or later\n"); | |
481 | goto abort; | |
482 | } else { | |
483 | mdu_array_info_t inf; | |
484 | memset(&inf, 0, sizeof(inf)); | |
485 | ioctl(mdfd, GET_ARRAY_INFO, &inf); | |
486 | if (inf.working_disks != 0) { | |
487 | fprintf(stderr, Name ": another array by this name" | |
488 | " is already running.\n"); | |
489 | goto abort; | |
490 | } | |
491 | } | |
492 | ||
493 | /* Ok, lets try some ioctls */ | |
494 | ||
495 | info.array.level = level; | |
496 | info.array.size = size; | |
497 | info.array.raid_disks = raiddisks; | |
498 | /* The kernel should *know* what md_minor we are dealing | |
499 | * with, but it chooses to trust me instead. Sigh | |
500 | */ | |
501 | info.array.md_minor = 0; | |
502 | if (fstat(mdfd, &stb)==0) | |
503 | info.array.md_minor = minor(stb.st_rdev); | |
504 | info.array.not_persistent = 0; | |
505 | ||
506 | if ( ( (level == 4 || level == 5) && | |
507 | (insert_point < raiddisks || first_missing < raiddisks) ) | |
508 | || | |
509 | ( level == 6 && (insert_point < raiddisks | |
510 | || second_missing < raiddisks)) | |
511 | || | |
512 | ( level <= 0 ) | |
513 | || | |
514 | assume_clean | |
515 | ) { | |
516 | info.array.state = 1; /* clean, but one+ drive will be missing*/ | |
517 | info.resync_start = ~0ULL; | |
518 | } else { | |
519 | info.array.state = 0; /* not clean, but no errors */ | |
520 | info.resync_start = 0; | |
521 | } | |
522 | if (level == 10) { | |
523 | /* for raid10, the bitmap size is the capacity of the array, | |
524 | * which is array.size * raid_disks / ncopies; | |
525 | * .. but convert to sectors. | |
526 | */ | |
527 | int ncopies = ((layout>>8) & 255) * (layout & 255); | |
528 | bitmapsize = (unsigned long long)size * raiddisks / ncopies * 2; | |
529 | /* printf("bms=%llu as=%d rd=%d nc=%d\n", bitmapsize, size, raiddisks, ncopies);*/ | |
530 | } else | |
531 | bitmapsize = (unsigned long long)size * 2; | |
532 | ||
533 | /* There is lots of redundancy in these disk counts, | |
534 | * raid_disks is the most meaningful value | |
535 | * it describes the geometry of the array | |
536 | * it is constant | |
537 | * nr_disks is total number of used slots. | |
538 | * it should be raid_disks+spare_disks | |
539 | * spare_disks is the number of extra disks present | |
540 | * see above | |
541 | * active_disks is the number of working disks in | |
542 | * active slots. (With raid_disks) | |
543 | * working_disks is the total number of working disks, | |
544 | * including spares | |
545 | * failed_disks is the number of disks marked failed | |
546 | * | |
547 | * Ideally, the kernel would keep these (except raid_disks) | |
548 | * up-to-date as we ADD_NEW_DISK, but it doesn't (yet). | |
549 | * So for now, we assume that all raid and spare | |
550 | * devices will be given. | |
551 | */ | |
552 | info.array.spare_disks=sparedisks; | |
553 | info.array.failed_disks=missing_disks; | |
554 | info.array.nr_disks = info.array.working_disks | |
555 | + info.array.failed_disks; | |
556 | info.array.layout = layout; | |
557 | info.array.chunk_size = chunk*1024; | |
558 | ||
559 | if (name == NULL || *name == 0) { | |
560 | /* base name on mddev */ | |
561 | /* /dev/md0 -> 0 | |
562 | * /dev/md_d0 -> d0 | |
563 | * /dev/md/1 -> 1 | |
564 | * /dev/md/d1 -> d1 | |
565 | * /dev/md/home -> home | |
566 | * /dev/mdhome -> home | |
567 | */ | |
568 | /* FIXME compare this with rules in create_mddev */ | |
569 | name = strrchr(mddev, '/'); | |
570 | if (name) { | |
571 | name++; | |
572 | if (strncmp(name, "md_d", 4)==0 && | |
573 | strlen(name) > 4 && | |
574 | isdigit(name[4]) && | |
575 | (name-mddev) == 5 /* /dev/ */) | |
576 | name += 3; | |
577 | else if (strncmp(name, "md", 2)==0 && | |
578 | strlen(name) > 2 && | |
579 | isdigit(name[2]) && | |
580 | (name-mddev) == 5 /* /dev/ */) | |
581 | name += 2; | |
582 | } | |
583 | } | |
584 | if (!st->ss->init_super(st, &info.array, size, name, homehost, uuid)) | |
585 | goto abort; | |
586 | ||
587 | total_slots = info.array.nr_disks; | |
588 | sysfs_init(&info, mdfd, 0); | |
589 | st->ss->getinfo_super(st, &info); | |
590 | ||
591 | if (did_default && verbose >= 0) { | |
592 | if (is_subarray(info.text_version)) { | |
593 | int dnum = devname2devnum(info.text_version+1); | |
594 | char *path; | |
595 | int mdp = get_mdp_major(); | |
596 | struct mdinfo *mdi; | |
597 | if (dnum > 0) | |
598 | path = map_dev(MD_MAJOR, dnum, 1); | |
599 | else | |
600 | path = map_dev(mdp, (-1-dnum)<< 6, 1); | |
601 | ||
602 | mdi = sysfs_read(-1, dnum, GET_VERSION); | |
603 | ||
604 | fprintf(stderr, Name ": Creating array inside " | |
605 | "%s container %s\n", | |
606 | mdi?mdi->text_version:"managed", path); | |
607 | sysfs_free(mdi); | |
608 | } else | |
609 | fprintf(stderr, Name ": Defaulting to version" | |
610 | " %s metadata\n", info.text_version); | |
611 | } | |
612 | ||
613 | map_update(&map, fd2devnum(mdfd), info.text_version, | |
614 | info.uuid, chosen_name); | |
615 | map_unlock(&map); | |
616 | ||
617 | if (bitmap_file && vers < 9003) { | |
618 | major_num = BITMAP_MAJOR_HOSTENDIAN; | |
619 | #ifdef __BIG_ENDIAN | |
620 | fprintf(stderr, Name ": Warning - bitmaps created on this kernel are not portable\n" | |
621 | " between different architectured. Consider upgrading the Linux kernel.\n"); | |
622 | #endif | |
623 | } | |
624 | ||
625 | if (bitmap_file && strcmp(bitmap_file, "internal")==0) { | |
626 | if ((vers%100) < 2) { | |
627 | fprintf(stderr, Name ": internal bitmaps not supported by this kernel.\n"); | |
628 | goto abort; | |
629 | } | |
630 | if (!st->ss->add_internal_bitmap(st, &bitmap_chunk, | |
631 | delay, write_behind, | |
632 | bitmapsize, 1, major_num)) { | |
633 | fprintf(stderr, Name ": Given bitmap chunk size not supported.\n"); | |
634 | goto abort; | |
635 | } | |
636 | bitmap_file = NULL; | |
637 | } | |
638 | ||
639 | ||
640 | sysfs_init(&info, mdfd, 0); | |
641 | ||
642 | if (st->ss->external && st->subarray[0]) { | |
643 | /* member */ | |
644 | ||
645 | /* When creating a member, we need to be careful | |
646 | * to negotiate with mdmon properly. | |
647 | * If it is already running, we cannot write to | |
648 | * the devices and must ask it to do that part. | |
649 | * If it isn't running, we write to the devices, | |
650 | * and then start it. | |
651 | * We hold an exclusive open on the container | |
652 | * device to make sure mdmon doesn't exit after | |
653 | * we checked that it is running. | |
654 | * | |
655 | * For now, fail if it is already running. | |
656 | */ | |
657 | container_fd = open_dev_excl(st->container_dev); | |
658 | if (container_fd < 0) { | |
659 | fprintf(stderr, Name ": Cannot get exclusive " | |
660 | "open on container - weird.\n"); | |
661 | goto abort; | |
662 | } | |
663 | if (mdmon_running(st->container_dev)) { | |
664 | if (verbose) | |
665 | fprintf(stderr, Name ": reusing mdmon " | |
666 | "for %s.\n", | |
667 | devnum2devname(st->container_dev)); | |
668 | st->update_tail = &st->updates; | |
669 | } else | |
670 | need_mdmon = 1; | |
671 | } | |
672 | rv = set_array_info(mdfd, st, &info); | |
673 | if (rv) { | |
674 | fprintf(stderr, Name ": failed to set array info for %s: %s\n", | |
675 | mddev, strerror(errno)); | |
676 | goto abort; | |
677 | } | |
678 | ||
679 | if (bitmap_file) { | |
680 | int uuid[4]; | |
681 | ||
682 | st->ss->uuid_from_super(st, uuid); | |
683 | if (CreateBitmap(bitmap_file, force, (char*)uuid, bitmap_chunk, | |
684 | delay, write_behind, | |
685 | bitmapsize, | |
686 | major_num)) { | |
687 | goto abort; | |
688 | } | |
689 | bitmap_fd = open(bitmap_file, O_RDWR); | |
690 | if (bitmap_fd < 0) { | |
691 | fprintf(stderr, Name ": weird: %s cannot be openned\n", | |
692 | bitmap_file); | |
693 | goto abort; | |
694 | } | |
695 | if (ioctl(mdfd, SET_BITMAP_FILE, bitmap_fd) < 0) { | |
696 | fprintf(stderr, Name ": Cannot set bitmap file for %s: %s\n", | |
697 | mddev, strerror(errno)); | |
698 | goto abort; | |
699 | } | |
700 | } | |
701 | ||
702 | infos = malloc(sizeof(*infos) * total_slots); | |
703 | ||
704 | for (pass=1; pass <=2 ; pass++) { | |
705 | mddev_dev_t moved_disk = NULL; /* the disk that was moved out of the insert point */ | |
706 | ||
707 | for (dnum=0, dv = devlist ; dv ; | |
708 | dv=(dv->next)?(dv->next):moved_disk, dnum++) { | |
709 | int fd; | |
710 | struct stat stb; | |
711 | struct mdinfo *inf = &infos[dnum]; | |
712 | ||
713 | if (dnum >= total_slots) | |
714 | abort(); | |
715 | if (dnum == insert_point) { | |
716 | moved_disk = dv; | |
717 | continue; | |
718 | } | |
719 | if (strcasecmp(dv->devname, "missing")==0) | |
720 | continue; | |
721 | if (have_container) | |
722 | moved_disk = NULL; | |
723 | if (have_container && dnum < info.array.raid_disks - 1) | |
724 | /* repeatedly use the container */ | |
725 | moved_disk = dv; | |
726 | ||
727 | switch(pass) { | |
728 | case 1: | |
729 | *inf = info; | |
730 | ||
731 | inf->disk.number = dnum; | |
732 | inf->disk.raid_disk = dnum; | |
733 | if (inf->disk.raid_disk < raiddisks) | |
734 | inf->disk.state = (1<<MD_DISK_ACTIVE) | | |
735 | (1<<MD_DISK_SYNC); | |
736 | else | |
737 | inf->disk.state = 0; | |
738 | ||
739 | if (dv->writemostly == 1) | |
740 | inf->disk.state |= (1<<MD_DISK_WRITEMOSTLY); | |
741 | ||
742 | if (have_container) | |
743 | fd = -1; | |
744 | else { | |
745 | if (st->ss->external && st->subarray[0]) | |
746 | fd = open(dv->devname, O_RDWR); | |
747 | else | |
748 | fd = open(dv->devname, O_RDWR|O_EXCL); | |
749 | ||
750 | if (fd < 0) { | |
751 | fprintf(stderr, Name ": failed to open %s " | |
752 | "after earlier success - aborting\n", | |
753 | dv->devname); | |
754 | goto abort; | |
755 | } | |
756 | fstat(fd, &stb); | |
757 | inf->disk.major = major(stb.st_rdev); | |
758 | inf->disk.minor = minor(stb.st_rdev); | |
759 | } | |
760 | if (fd >= 0) | |
761 | remove_partitions(fd); | |
762 | if (st->ss->add_to_super(st, &inf->disk, | |
763 | fd, dv->devname)) | |
764 | goto abort; | |
765 | st->ss->getinfo_super(st, inf); | |
766 | safe_mode_delay = inf->safe_mode_delay; | |
767 | ||
768 | if (have_container && verbose > 0) | |
769 | fprintf(stderr, Name ": Using %s for device %d\n", | |
770 | map_dev(inf->disk.major, | |
771 | inf->disk.minor, | |
772 | 0), dnum); | |
773 | ||
774 | if (!have_container) { | |
775 | /* getinfo_super might have lost these ... */ | |
776 | inf->disk.major = major(stb.st_rdev); | |
777 | inf->disk.minor = minor(stb.st_rdev); | |
778 | } | |
779 | break; | |
780 | case 2: | |
781 | inf->errors = 0; | |
782 | rv = 0; | |
783 | ||
784 | rv = add_disk(mdfd, st, &info, inf); | |
785 | ||
786 | if (rv) { | |
787 | fprintf(stderr, | |
788 | Name ": ADD_NEW_DISK for %s " | |
789 | "failed: %s\n", | |
790 | dv->devname, strerror(errno)); | |
791 | st->ss->free_super(st); | |
792 | goto abort; | |
793 | } | |
794 | break; | |
795 | } | |
796 | if (!have_container && | |
797 | dv == moved_disk && dnum != insert_point) break; | |
798 | } | |
799 | if (pass == 1) { | |
800 | st->ss->write_init_super(st); | |
801 | flush_metadata_updates(st); | |
802 | } | |
803 | } | |
804 | free(infos); | |
805 | st->ss->free_super(st); | |
806 | ||
807 | if (level == LEVEL_CONTAINER) { | |
808 | /* No need to start. But we should signal udev to | |
809 | * create links */ | |
810 | sysfs_uevent(&info, "change"); | |
811 | if (verbose >= 0) | |
812 | fprintf(stderr, Name ": container %s prepared.\n", mddev); | |
813 | wait_for(chosen_name, mdfd); | |
814 | } else if (runstop == 1 || subdevs >= raiddisks) { | |
815 | if (st->ss->external) { | |
816 | switch(level) { | |
817 | case LEVEL_LINEAR: | |
818 | case LEVEL_MULTIPATH: | |
819 | case 0: | |
820 | sysfs_set_str(&info, NULL, "array_state", | |
821 | "active"); | |
822 | need_mdmon = 0; | |
823 | break; | |
824 | default: | |
825 | sysfs_set_str(&info, NULL, "array_state", | |
826 | "readonly"); | |
827 | break; | |
828 | } | |
829 | sysfs_set_safemode(&info, safe_mode_delay); | |
830 | } else { | |
831 | /* param is not actually used */ | |
832 | mdu_param_t param; | |
833 | if (ioctl(mdfd, RUN_ARRAY, ¶m)) { | |
834 | fprintf(stderr, Name ": RUN_ARRAY failed: %s\n", | |
835 | strerror(errno)); | |
836 | Manage_runstop(mddev, mdfd, -1, 0); | |
837 | goto abort; | |
838 | } | |
839 | } | |
840 | if (verbose >= 0) | |
841 | fprintf(stderr, Name ": array %s started.\n", mddev); | |
842 | if (st->ss->external && st->subarray[0]) { | |
843 | if (need_mdmon) | |
844 | start_mdmon(st->container_dev); | |
845 | ||
846 | ping_monitor(devnum2devname(st->container_dev)); | |
847 | close(container_fd); | |
848 | } | |
849 | wait_for(chosen_name, mdfd); | |
850 | } else { | |
851 | fprintf(stderr, Name ": not starting array - not enough devices.\n"); | |
852 | } | |
853 | close(mdfd); | |
854 | return 0; | |
855 | ||
856 | abort: | |
857 | if (mdfd >= 0) | |
858 | close(mdfd); | |
859 | return 1; | |
860 | } |