]>
Commit | Line | Data |
---|---|---|
64c4757e | 1 | /* |
9a9dab36 | 2 | * mdadm - manage Linux "md" devices aka RAID arrays. |
64c4757e | 3 | * |
6f02172d | 4 | * Copyright (C) 2001-2013 Neil Brown <neilb@suse.de> |
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 | |
e736b623 | 22 | * Email: <neilb@suse.de> |
64c4757e NB |
23 | */ |
24 | ||
ca3b6696 | 25 | #include "mdadm.h" |
682c7051 NB |
26 | #include "md_u.h" |
27 | #include "md_p.h" | |
60248f74 | 28 | #include <ctype.h> |
64c4757e | 29 | |
22dc741f MT |
30 | static int round_size_and_verify(unsigned long long *size, int chunk) |
31 | { | |
32 | if (*size == 0) | |
33 | return 0; | |
34 | *size &= ~(unsigned long long)(chunk - 1); | |
35 | if (*size == 0) { | |
36 | pr_err("Size cannot be smaller than chunk.\n"); | |
37 | return 1; | |
38 | } | |
39 | return 0; | |
40 | } | |
41 | ||
913f07d1 MT |
42 | /** |
43 | * default_layout() - Get default layout for level. | |
44 | * @st: metadata requested, could be NULL. | |
45 | * @level: raid level requested. | |
46 | * @verbose: verbose level. | |
47 | * | |
48 | * Try to ask metadata handler first, otherwise use global defaults. | |
49 | * | |
50 | * Return: Layout or &UnSet, return value meaning depends of level used. | |
51 | */ | |
52 | int default_layout(struct supertype *st, int level, int verbose) | |
a18a888e DW |
53 | { |
54 | int layout = UnSet; | |
913f07d1 MT |
55 | mapping_t *layout_map = NULL; |
56 | char *layout_name = NULL; | |
a18a888e | 57 | |
30f58b22 DW |
58 | if (st && st->ss->default_geometry) |
59 | st->ss->default_geometry(st, &level, &layout, NULL); | |
a18a888e | 60 | |
913f07d1 MT |
61 | if (layout != UnSet) |
62 | return layout; | |
a18a888e | 63 | |
913f07d1 MT |
64 | switch (level) { |
65 | default: /* no layout */ | |
66 | layout = 0; | |
67 | break; | |
68 | case 0: | |
69 | layout = RAID0_ORIG_LAYOUT; | |
70 | break; | |
71 | case 10: | |
72 | layout = 0x102; /* near=2, far=1 */ | |
73 | layout_name = "n2"; | |
74 | break; | |
75 | case 5: | |
76 | case 6: | |
77 | layout_map = r5layout; | |
78 | break; | |
79 | case LEVEL_FAULTY: | |
80 | layout_map = faultylayout; | |
81 | break; | |
82 | } | |
83 | ||
84 | if (layout_map) { | |
85 | layout = map_name(layout_map, "default"); | |
5f21d674 | 86 | layout_name = map_num_s(layout_map, layout); |
913f07d1 MT |
87 | } |
88 | if (layout_name && verbose > 0) | |
89 | pr_err("layout defaults to %s\n", layout_name); | |
a18a888e DW |
90 | |
91 | return layout; | |
92 | } | |
93 | ||
7f91af49 | 94 | int Create(struct supertype *st, char *mddev, |
171dccc8 | 95 | char *name, int *uuid, |
a655e550 | 96 | int subdevs, struct mddev_dev *devlist, |
99cc42f4 | 97 | struct shape *s, |
ae5dfc56 | 98 | struct context *c) |
64c4757e | 99 | { |
682c7051 NB |
100 | /* |
101 | * Create a new raid array. | |
102 | * | |
103 | * First check that necessary details are available | |
104 | * (i.e. level, raid-disks) | |
105 | * | |
106 | * Then check each disk to see what might be on it | |
107 | * and report anything interesting. | |
108 | * | |
109 | * If anything looks odd, and runstop not set, | |
110 | * abort. | |
111 | * | |
112 | * SET_ARRAY_INFO and ADD_NEW_DISK, and | |
82d9eba6 | 113 | * if runstop==run, or raiddisks disks were used, |
682c7051 NB |
114 | * RUN_ARRAY |
115 | */ | |
7f91af49 | 116 | int mdfd; |
98dbf73c | 117 | unsigned long long minsize = 0, maxsize = 0; |
cd29a5c8 NB |
118 | char *mindisc = NULL; |
119 | char *maxdisc = NULL; | |
cc1799c3 | 120 | int dnum, raid_disk_num; |
a655e550 | 121 | struct mddev_dev *dv; |
0a6bff09 | 122 | dev_t rdev; |
98dbf73c | 123 | int fail = 0, warn = 0; |
82d9eba6 | 124 | int first_missing = subdevs * 2; |
6fb79233 | 125 | int second_missing = subdevs * 2; |
52826846 | 126 | int missing_disks = 0; |
82d9eba6 | 127 | int insert_point = subdevs * 2; /* where to insert a missing drive */ |
d2ca6449 | 128 | int total_slots; |
4b1ac34b | 129 | int pass; |
82d9eba6 | 130 | int rv; |
c82f047c | 131 | int bitmap_fd; |
5f8097be | 132 | int have_container = 0; |
0e600426 | 133 | int container_fd = -1; |
a931db9e | 134 | int need_mdmon = 0; |
f9c25f1d | 135 | unsigned long long bitmapsize; |
d2ca6449 | 136 | struct mdinfo info, *infos; |
b8ac1967 | 137 | int did_default = 0; |
a18a888e | 138 | int do_default_layout = 0; |
c21e737b | 139 | int do_default_chunk = 0; |
8ed3e5e1 | 140 | unsigned long safe_mode_delay = 0; |
a04d5763 | 141 | char chosen_name[1024]; |
ad5bc697 | 142 | struct map_ent *map = NULL; |
8592f29d | 143 | unsigned long long newsize; |
5f4cc239 | 144 | mdu_array_info_t inf; |
682c7051 | 145 | |
943eafef | 146 | int major_num = BITMAP_MAJOR_HI; |
82d9485e | 147 | if (s->bitmap_file && strcmp(s->bitmap_file, "clustered") == 0) { |
6d9c7c25 | 148 | major_num = BITMAP_MAJOR_CLUSTERED; |
82d9485e GJ |
149 | if (c->nodes <= 1) { |
150 | pr_err("At least 2 nodes are needed for cluster-md\n"); | |
151 | return 1; | |
152 | } | |
153 | } | |
dcec9ee5 | 154 | |
06c7f68e | 155 | memset(&info, 0, sizeof(info)); |
99cc42f4 N |
156 | if (s->level == UnSet && st && st->ss->default_geometry) |
157 | st->ss->default_geometry(st, &s->level, NULL, NULL); | |
158 | if (s->level == UnSet) { | |
e7b84f9d | 159 | pr_err("a RAID level is needed to create an array.\n"); |
682c7051 NB |
160 | return 1; |
161 | } | |
99cc42f4 | 162 | if (s->raiddisks < 4 && s->level == 6) { |
e7b84f9d | 163 | pr_err("at least 4 raid-devices needed for level 6\n"); |
98c6faba NB |
164 | return 1; |
165 | } | |
99cc42f4 | 166 | if (s->raiddisks > 256 && s->level == 6) { |
e7b84f9d | 167 | pr_err("no more than 256 raid-devices supported for level 6\n"); |
98c6faba NB |
168 | return 1; |
169 | } | |
99cc42f4 | 170 | if (s->raiddisks < 2 && s->level >= 4) { |
f7889e51 | 171 | pr_err("at least 2 raid-devices needed for level %d\n", s->level); |
52826846 NB |
172 | return 1; |
173 | } | |
99cc42f4 | 174 | if (s->level <= 0 && s->sparedisks) { |
e7b84f9d | 175 | pr_err("This level does not support spare devices\n"); |
570510ba NB |
176 | return 1; |
177 | } | |
5f8097be NB |
178 | |
179 | if (subdevs == 1 && strcmp(devlist->devname, "missing") != 0) { | |
180 | /* If given a single device, it might be a container, and we can | |
181 | * extract a device list from there | |
182 | */ | |
5f8097be NB |
183 | int fd; |
184 | ||
185 | memset(&inf, 0, sizeof(inf)); | |
37ea3936 | 186 | fd = open(devlist->devname, O_RDONLY); |
5f8097be | 187 | if (fd >= 0 && |
9cd39f01 | 188 | md_get_array_info(fd, &inf) == 0 && inf.raid_disks == 0) { |
5f8097be NB |
189 | /* yep, looks like a container */ |
190 | if (st) { | |
0fb69d1d N |
191 | rv = st->ss->load_container(st, fd, |
192 | devlist->devname); | |
5f8097be NB |
193 | if (rv == 0) |
194 | have_container = 1; | |
195 | } else { | |
0fb69d1d | 196 | st = super_by_fd(fd, NULL); |
5f8097be | 197 | if (st && !(rv = st->ss-> |
0fb69d1d N |
198 | load_container(st, fd, |
199 | devlist->devname))) | |
5f8097be NB |
200 | have_container = 1; |
201 | else | |
202 | st = NULL; | |
203 | } | |
18fde300 | 204 | if (have_container) { |
99cc42f4 | 205 | subdevs = s->raiddisks; |
18fde300 DW |
206 | first_missing = subdevs * 2; |
207 | second_missing = subdevs * 2; | |
208 | insert_point = subdevs * 2; | |
209 | } | |
5f8097be NB |
210 | } |
211 | if (fd >= 0) | |
212 | close(fd); | |
5f8097be | 213 | } |
99cc42f4 | 214 | if (st && st->ss->external && s->sparedisks) { |
7a862a02 | 215 | pr_err("This metadata type does not support spare disks at create time\n"); |
ffcfc735 N |
216 | return 1; |
217 | } | |
cc1799c3 | 218 | if (subdevs > s->raiddisks+s->sparedisks+s->journaldisks) { |
99cc42f4 | 219 | pr_err("You have listed more devices (%d) than are in the array(%d)!\n", subdevs, s->raiddisks+s->sparedisks); |
52826846 | 220 | return 1; |
682c7051 | 221 | } |
cc1799c3 | 222 | if (!have_container && subdevs < s->raiddisks+s->sparedisks+s->journaldisks) { |
e7b84f9d | 223 | pr_err("You haven't given enough devices (real or missing) to create this array\n"); |
52826846 NB |
224 | return 1; |
225 | } | |
99cc42f4 | 226 | if (s->bitmap_file && s->level <= 0) { |
e7b84f9d | 227 | pr_err("bitmaps not meaningful with level %s\n", |
99cc42f4 | 228 | map_num(pers, s->level)?:"given"); |
5b28bd56 NB |
229 | return 1; |
230 | } | |
52826846 | 231 | |
682c7051 | 232 | /* now set some defaults */ |
b5e64645 | 233 | |
99cc42f4 | 234 | if (s->layout == UnSet) { |
a18a888e | 235 | do_default_layout = 1; |
99cc42f4 | 236 | s->layout = default_layout(st, s->level, c->verbose); |
a18a888e | 237 | } |
682c7051 | 238 | |
99cc42f4 | 239 | if (s->level == 10) |
e5329c37 | 240 | /* check layout fits in array*/ |
99cc42f4 | 241 | if ((s->layout&255) * ((s->layout>>8)&255) > s->raiddisks) { |
e7b84f9d | 242 | pr_err("that layout requires at least %d devices\n", |
99cc42f4 | 243 | (s->layout&255) * ((s->layout>>8)&255)); |
e5329c37 NB |
244 | return 1; |
245 | } | |
246 | ||
99cc42f4 | 247 | switch(s->level) { |
aa88f531 NB |
248 | case 4: |
249 | case 5: | |
e5329c37 | 250 | case 10: |
98c6faba | 251 | case 6: |
aa88f531 | 252 | case 0: |
99cc42f4 N |
253 | if (s->chunk == 0 || s->chunk == UnSet) { |
254 | s->chunk = UnSet; | |
c21e737b CA |
255 | do_default_chunk = 1; |
256 | /* chunk will be set later */ | |
bb7295f1 | 257 | } |
5f175898 N |
258 | break; |
259 | case LEVEL_LINEAR: | |
260 | /* a chunksize of zero 0s perfectly valid (and preferred) since 2.6.16 */ | |
99cc42f4 N |
261 | if (get_linux_version() < 2006016 && s->chunk == 0) { |
262 | s->chunk = 64; | |
171dccc8 | 263 | if (c->verbose > 0) |
e7b84f9d | 264 | pr_err("chunk size defaults to 64K\n"); |
aa88f531 NB |
265 | } |
266 | break; | |
570510ba | 267 | case 1: |
08e43379 | 268 | case LEVEL_FAULTY: |
570510ba | 269 | case LEVEL_MULTIPATH: |
17f25ca6 | 270 | case LEVEL_CONTAINER: |
99cc42f4 | 271 | if (s->chunk) { |
5b30a34a MG |
272 | pr_err("specifying chunk size is forbidden for this level\n"); |
273 | return 1; | |
aa88f531 NB |
274 | } |
275 | break; | |
570510ba | 276 | default: |
99cc42f4 | 277 | pr_err("unknown level %d\n", s->level); |
570510ba | 278 | return 1; |
682c7051 | 279 | } |
22dc741f | 280 | |
99cc42f4 | 281 | if (s->size == MAX_SIZE) |
d04f65f4 | 282 | /* use '0' to mean 'max' now... */ |
99cc42f4 N |
283 | s->size = 0; |
284 | if (s->size && s->chunk && s->chunk != UnSet) | |
22dc741f MT |
285 | if (round_size_and_verify(&s->size, s->chunk)) |
286 | return 1; | |
287 | ||
99cc42f4 N |
288 | newsize = s->size * 2; |
289 | if (st && ! st->ss->validate_geometry(st, s->level, s->layout, s->raiddisks, | |
af4348dd | 290 | &s->chunk, s->size*2, |
ae5dfc56 | 291 | s->data_offset, NULL, |
5308f117 | 292 | &newsize, s->consistency_policy, |
98dbf73c | 293 | c->verbose >= 0)) |
17f25ca6 | 294 | return 1; |
bb7295f1 | 295 | |
99cc42f4 N |
296 | if (s->chunk && s->chunk != UnSet) { |
297 | newsize &= ~(unsigned long long)(s->chunk*2 - 1); | |
64385908 CA |
298 | if (do_default_chunk) { |
299 | /* default chunk was just set */ | |
171dccc8 | 300 | if (c->verbose > 0) |
7a862a02 | 301 | pr_err("chunk size defaults to %dK\n", s->chunk); |
22dc741f MT |
302 | if (round_size_and_verify(&s->size, s->chunk)) |
303 | return 1; | |
64385908 CA |
304 | do_default_chunk = 0; |
305 | } | |
bb7295f1 | 306 | } |
64385908 | 307 | |
99cc42f4 N |
308 | if (s->size == 0) { |
309 | s->size = newsize / 2; | |
310 | if (s->level == 1) | |
ae6c05ad N |
311 | /* If this is ever reshaped to RAID5, we will |
312 | * need a chunksize. So round it off a bit | |
313 | * now just to be safe | |
314 | */ | |
99cc42f4 | 315 | s->size &= ~(64ULL-1); |
ae6c05ad | 316 | |
99cc42f4 N |
317 | if (s->size && c->verbose > 0) |
318 | pr_err("setting size to %lluK\n", s->size); | |
8592f29d | 319 | } |
17f25ca6 | 320 | |
682c7051 | 321 | /* now look at the subdevs */ |
06c7f68e NB |
322 | info.array.active_disks = 0; |
323 | info.array.working_disks = 0; | |
c913b90e | 324 | dnum = 0; |
98dbf73c | 325 | for (dv = devlist; dv; dv = dv->next) |
ae5dfc56 | 326 | if (s->data_offset == VARIABLE_OFFSET) |
476066a3 N |
327 | dv->data_offset = INVALID_SECTORS; |
328 | else | |
ae5dfc56 | 329 | dv->data_offset = s->data_offset; |
476066a3 | 330 | |
8592f29d | 331 | for (dv=devlist; dv && !have_container; dv=dv->next, dnum++) { |
cd29a5c8 | 332 | char *dname = dv->devname; |
17f25ca6 | 333 | unsigned long long freesize; |
e9b11fee | 334 | int dfd; |
72ca9bcf | 335 | char *doff; |
e9b11fee | 336 | |
98dbf73c | 337 | if (strcasecmp(dname, "missing") == 0) { |
c913b90e NB |
338 | if (first_missing > dnum) |
339 | first_missing = dnum; | |
6fb79233 NB |
340 | if (second_missing > dnum && dnum > first_missing) |
341 | second_missing = dnum; | |
52826846 NB |
342 | missing_disks ++; |
343 | continue; | |
344 | } | |
ae5dfc56 | 345 | if (s->data_offset == VARIABLE_OFFSET) { |
72ca9bcf N |
346 | doff = strchr(dname, ':'); |
347 | if (doff) { | |
348 | *doff++ = 0; | |
349 | dv->data_offset = parse_size(doff); | |
350 | } else | |
351 | dv->data_offset = INVALID_SECTORS; | |
352 | } else | |
ae5dfc56 | 353 | dv->data_offset = s->data_offset; |
72ca9bcf | 354 | |
58b3c697 | 355 | dfd = open(dname, O_RDONLY); |
e9b11fee | 356 | if (dfd < 0) { |
e7b84f9d | 357 | pr_err("cannot open %s: %s\n", |
e9b11fee N |
358 | dname, strerror(errno)); |
359 | exit(2); | |
360 | } | |
0a6bff09 | 361 | if (!fstat_is_blkdev(dfd, dname, NULL)) { |
e9b11fee | 362 | close(dfd); |
e9b11fee N |
363 | exit(2); |
364 | } | |
365 | close(dfd); | |
06c7f68e | 366 | info.array.working_disks++; |
dfd7822c | 367 | if (dnum < s->raiddisks && dv->disposition != 'j') |
06c7f68e | 368 | info.array.active_disks++; |
058574b1 | 369 | if (st == NULL) { |
8aec876d | 370 | struct createinfo *ci = conf_get_create_info(); |
058574b1 NB |
371 | if (ci) |
372 | st = ci->supertype; | |
373 | } | |
576d6d83 NB |
374 | if (st == NULL) { |
375 | /* Need to choose a default metadata, which is different | |
17f25ca6 | 376 | * depending on geometry of array. |
576d6d83 NB |
377 | */ |
378 | int i; | |
379 | char *name = "default"; | |
98dbf73c | 380 | for(i = 0; !st && superlist[i]; i++) { |
576d6d83 | 381 | st = superlist[i]->match_metadata_desc(name); |
ecbd9e81 N |
382 | if (!st) |
383 | continue; | |
a18a888e | 384 | if (do_default_layout) |
99cc42f4 | 385 | s->layout = default_layout(st, s->level, c->verbose); |
ecbd9e81 | 386 | switch (st->ss->validate_geometry( |
99cc42f4 | 387 | st, s->level, s->layout, s->raiddisks, |
af4348dd | 388 | &s->chunk, s->size*2, |
72ca9bcf | 389 | dv->data_offset, dname, |
5308f117 AP |
390 | &freesize, s->consistency_policy, |
391 | c->verbose > 0)) { | |
ecbd9e81 N |
392 | case -1: /* Not valid, message printed, and not |
393 | * worth checking any further */ | |
394 | exit(2); | |
395 | break; | |
396 | case 0: /* Geometry not valid */ | |
55425f27 | 397 | free(st); |
17f25ca6 | 398 | st = NULL; |
99cc42f4 | 399 | s->chunk = do_default_chunk ? UnSet : s->chunk; |
ecbd9e81 N |
400 | break; |
401 | case 1: /* All happy */ | |
402 | break; | |
55425f27 | 403 | } |
17f25ca6 | 404 | } |
576d6d83 NB |
405 | |
406 | if (!st) { | |
58b3c697 N |
407 | int dfd = open(dname, O_RDONLY|O_EXCL); |
408 | if (dfd < 0) { | |
e7b84f9d | 409 | pr_err("cannot open %s: %s\n", |
58b3c697 N |
410 | dname, strerror(errno)); |
411 | exit(2); | |
412 | } | |
7a862a02 | 413 | pr_err("device %s not suitable for any style of array\n", |
17f25ca6 | 414 | dname); |
576d6d83 NB |
415 | exit(2); |
416 | } | |
b8ac1967 | 417 | if (st->ss != &super0 || |
576d6d83 | 418 | st->minor_version != 90) |
b8ac1967 | 419 | did_default = 1; |
17f25ca6 | 420 | } else { |
a18a888e | 421 | if (do_default_layout) |
99cc42f4 N |
422 | s->layout = default_layout(st, s->level, 0); |
423 | if (!st->ss->validate_geometry(st, s->level, s->layout, | |
424 | s->raiddisks, | |
af4348dd | 425 | &s->chunk, s->size*2, |
72ca9bcf | 426 | dv->data_offset, |
af4348dd | 427 | dname, &freesize, |
5308f117 | 428 | s->consistency_policy, |
171dccc8 | 429 | c->verbose >= 0)) { |
17f25ca6 | 430 | |
7a862a02 | 431 | pr_err("%s is not suitable for this array.\n", |
e7b84f9d | 432 | dname); |
17f25ca6 NB |
433 | fail = 1; |
434 | continue; | |
435 | } | |
682c7051 | 436 | } |
82d9eba6 | 437 | |
cc1799c3 | 438 | if (dv->disposition == 'j') |
dfd7822c | 439 | goto skip_size_check; /* skip write journal for size check */ |
cc1799c3 | 440 | |
82d9eba6 | 441 | freesize /= 2; /* convert to K */ |
99cc42f4 | 442 | if (s->chunk && s->chunk != UnSet) { |
d2cd3ffc | 443 | /* round to chunk size */ |
99cc42f4 | 444 | freesize = freesize & ~(s->chunk-1); |
64385908 CA |
445 | if (do_default_chunk) { |
446 | /* default chunk was just set */ | |
171dccc8 | 447 | if (c->verbose > 0) |
7a862a02 | 448 | pr_err("chunk size defaults to %dK\n", s->chunk); |
22dc741f MT |
449 | if (round_size_and_verify(&s->size, s->chunk)) |
450 | return 1; | |
64385908 CA |
451 | do_default_chunk = 0; |
452 | } | |
d2cd3ffc | 453 | } |
066e92f0 LD |
454 | if (!freesize) { |
455 | pr_err("no free space left on %s\n", dname); | |
456 | fail = 1; | |
457 | continue; | |
458 | } | |
682c7051 | 459 | |
99cc42f4 | 460 | if (s->size && freesize < s->size) { |
7a862a02 | 461 | pr_err("%s is smaller than given size. %lluK < %lluK + metadata\n", |
99cc42f4 | 462 | dname, freesize, s->size); |
52826846 | 463 | fail = 1; |
52826846 | 464 | continue; |
682c7051 | 465 | } |
cd29a5c8 NB |
466 | if (maxdisc == NULL || (maxdisc && freesize > maxsize)) { |
467 | maxdisc = dname; | |
52826846 | 468 | maxsize = freesize; |
682c7051 | 469 | } |
cd29a5c8 NB |
470 | if (mindisc ==NULL || (mindisc && freesize < minsize)) { |
471 | mindisc = dname; | |
52826846 | 472 | minsize = freesize; |
682c7051 | 473 | } |
dfd7822c | 474 | skip_size_check: |
171dccc8 | 475 | if (c->runstop != 1 || c->verbose >= 0) { |
37ea3936 | 476 | int fd = open(dname, O_RDONLY); |
98dbf73c | 477 | if (fd < 0) { |
e7b84f9d | 478 | pr_err("Cannot open %s: %s\n", |
b6e63da4 | 479 | dname, strerror(errno)); |
98dbf73c | 480 | fail = 1; |
b6e63da4 NB |
481 | continue; |
482 | } | |
dab6685f NB |
483 | warn |= check_ext2(fd, dname); |
484 | warn |= check_reiser(fd, dname); | |
485 | warn |= check_raid(fd, dname); | |
034b203a TM |
486 | if (strcmp(st->ss->name, "1.x") == 0 && |
487 | st->minor_version >= 1) | |
488 | /* metadata at front */ | |
53ed6ac3 | 489 | warn |= check_partitions(fd, dname, 0, 0); |
6f2af6a4 | 490 | else if (s->level == 1 || is_container(s->level) || |
cf622ec1 | 491 | (s->level == 0 && s->raiddisks == 1)) |
034b203a | 492 | /* partitions could be meaningful */ |
99cc42f4 | 493 | warn |= check_partitions(fd, dname, freesize*2, s->size*2); |
034b203a TM |
494 | else |
495 | /* partitions cannot be meaningful */ | |
53ed6ac3 | 496 | warn |= check_partitions(fd, dname, 0, 0); |
034b203a | 497 | if (strcmp(st->ss->name, "1.x") == 0 && |
a0962fe9 N |
498 | st->minor_version >= 1 && |
499 | did_default && | |
99cc42f4 | 500 | s->level == 1 && |
034b203a TM |
501 | (warn & 1024) == 0) { |
502 | warn |= 1024; | |
e7b84f9d | 503 | pr_err("Note: this array has metadata at the start and\n" |
a0962fe9 | 504 | " may not be suitable as a boot device. If you plan to\n" |
cc86f89c | 505 | " store '/boot' on this device please ensure that\n" |
a0962fe9 | 506 | " your boot-loader understands md/v1.x metadata, or use\n" |
cc86f89c | 507 | " --metadata=0.90\n"); |
a0962fe9 | 508 | } |
b6e63da4 | 509 | close(fd); |
dab6685f | 510 | } |
682c7051 | 511 | } |
b91ad097 | 512 | if (missing_disks == dnum && !have_container) { |
1db03765 XN |
513 | pr_err("Subdevs can't be all missing\n"); |
514 | return 1; | |
515 | } | |
99cc42f4 | 516 | if (s->raiddisks + s->sparedisks > st->max_devs) { |
7a862a02 | 517 | pr_err("Too many devices: %s metadata only supports %d\n", |
acab7bb1 N |
518 | st->ss->name, st->max_devs); |
519 | return 1; | |
520 | } | |
8592f29d | 521 | if (have_container) |
99cc42f4 | 522 | info.array.working_disks = s->raiddisks; |
682c7051 | 523 | if (fail) { |
e7b84f9d | 524 | pr_err("create aborted\n"); |
52826846 | 525 | return 1; |
682c7051 | 526 | } |
99cc42f4 | 527 | if (s->size == 0) { |
5f8097be | 528 | if (mindisc == NULL && !have_container) { |
e7b84f9d | 529 | pr_err("no size and no drives given - aborting create.\n"); |
52826846 NB |
530 | return 1; |
531 | } | |
cf622ec1 | 532 | if (s->level > 0 || s->level == LEVEL_MULTIPATH || |
98dbf73c | 533 | s->level == LEVEL_FAULTY || st->ss->external) { |
b5e64645 | 534 | /* size is meaningful */ |
99cc42f4 N |
535 | if (!st->ss->validate_geometry(st, s->level, s->layout, |
536 | s->raiddisks, | |
537 | &s->chunk, minsize*2, | |
ae5dfc56 | 538 | s->data_offset, |
5308f117 AP |
539 | NULL, NULL, |
540 | s->consistency_policy, 0)) { | |
99cc42f4 | 541 | pr_err("devices too large for RAID level %d\n", s->level); |
b5e64645 NB |
542 | return 1; |
543 | } | |
99cc42f4 N |
544 | s->size = minsize; |
545 | if (s->level == 1) | |
ae6c05ad N |
546 | /* If this is ever reshaped to RAID5, we will |
547 | * need a chunksize. So round it off a bit | |
548 | * now just to be safe | |
549 | */ | |
99cc42f4 | 550 | s->size &= ~(64ULL-1); |
171dccc8 | 551 | if (c->verbose > 0) |
99cc42f4 | 552 | pr_err("size set to %lluK\n", s->size); |
b5e64645 | 553 | } |
682c7051 | 554 | } |
748952f7 N |
555 | |
556 | if (!s->bitmap_file && | |
848d71c9 | 557 | !st->ss->external && |
748952f7 | 558 | s->level >= 1 && |
39917e56 | 559 | st->ss->add_internal_bitmap && |
2ce09172 | 560 | s->journaldisks == 0 && |
e97a7cd0 AP |
561 | (s->consistency_policy != CONSISTENCY_POLICY_RESYNC && |
562 | s->consistency_policy != CONSISTENCY_POLICY_PPL) && | |
748952f7 N |
563 | (s->write_behind || s->size > 100*1024*1024ULL)) { |
564 | if (c->verbose > 0) | |
565 | pr_err("automatically enabling write-intent bitmap on large array\n"); | |
566 | s->bitmap_file = "internal"; | |
567 | } | |
568 | if (s->bitmap_file && strcmp(s->bitmap_file, "none") == 0) | |
569 | s->bitmap_file = NULL; | |
570 | ||
5308f117 AP |
571 | if (s->consistency_policy == CONSISTENCY_POLICY_PPL && |
572 | !st->ss->write_init_ppl) { | |
573 | pr_err("%s metadata does not support PPL\n", st->ss->name); | |
574 | return 1; | |
575 | } | |
576 | ||
99cc42f4 | 577 | if (!have_container && s->level > 0 && ((maxsize-s->size)*100 > maxsize)) { |
171dccc8 | 578 | if (c->runstop != 1 || c->verbose >= 0) |
e7b84f9d | 579 | pr_err("largest drive (%s) exceeds size (%lluK) by more than 1%%\n", |
99cc42f4 | 580 | maxdisc, s->size); |
52826846 | 581 | warn = 1; |
682c7051 NB |
582 | } |
583 | ||
9eafa1de | 584 | if (st->ss->detail_platform && st->ss->detail_platform(0, 1, NULL) != 0) { |
171dccc8 | 585 | if (c->runstop != 1 || c->verbose >= 0) |
e7b84f9d | 586 | pr_err("%s unable to enumerate platform support\n" |
5615172f DW |
587 | " array may not be compatible with hardware/firmware\n", |
588 | st->ss->name); | |
589 | warn = 1; | |
590 | } | |
529e2aa5 | 591 | st->nodes = c->nodes; |
7716570e | 592 | st->cluster_name = c->homecluster; |
5615172f | 593 | |
682c7051 | 594 | if (warn) { |
171dccc8 | 595 | if (c->runstop!= 1) { |
52826846 | 596 | if (!ask("Continue creating array? ")) { |
e7b84f9d | 597 | pr_err("create aborted.\n"); |
52826846 NB |
598 | return 1; |
599 | } | |
600 | } else { | |
171dccc8 | 601 | if (c->verbose > 0) |
e7b84f9d | 602 | pr_err("creation continuing despite oddities due to --run\n"); |
682c7051 | 603 | } |
682c7051 NB |
604 | } |
605 | ||
66f8bbbe | 606 | /* If this is raid4/5, we want to configure the last active slot |
52826846 | 607 | * as missing, so that a reconstruct happens (faster than re-parity) |
98c6faba | 608 | * FIX: Can we do this for raid6 as well? |
52826846 | 609 | */ |
98dbf73c JS |
610 | if (st->ss->external == 0 && s->assume_clean == 0 && |
611 | c->force == 0 && first_missing >= s->raiddisks) { | |
612 | switch (s->level) { | |
66f8bbbe | 613 | case 4: |
98c6faba | 614 | case 5: |
99cc42f4 N |
615 | insert_point = s->raiddisks-1; |
616 | s->sparedisks++; | |
06c7f68e | 617 | info.array.active_disks--; |
98c6faba NB |
618 | missing_disks++; |
619 | break; | |
620 | default: | |
621 | break; | |
622 | } | |
52826846 | 623 | } |
6fb79233 NB |
624 | /* For raid6, if creating with 1 missing drive, make a good drive |
625 | * into a spare, else the create will fail | |
626 | */ | |
99cc42f4 | 627 | if (s->assume_clean == 0 && c->force == 0 && first_missing < s->raiddisks && |
ffcfc735 | 628 | st->ss->external == 0 && |
99cc42f4 N |
629 | second_missing >= s->raiddisks && s->level == 6) { |
630 | insert_point = s->raiddisks - 1; | |
6fb79233 NB |
631 | if (insert_point == first_missing) |
632 | insert_point--; | |
99cc42f4 | 633 | s->sparedisks ++; |
6fb79233 NB |
634 | info.array.active_disks--; |
635 | missing_disks++; | |
636 | } | |
570510ba | 637 | |
99cc42f4 | 638 | if (s->level <= 0 && first_missing < subdevs * 2) { |
e7b84f9d | 639 | pr_err("This level does not support missing devices\n"); |
570510ba NB |
640 | return 1; |
641 | } | |
aba69144 | 642 | |
7f91af49 | 643 | /* We need to create the device */ |
ad5bc697 | 644 | map_lock(&map); |
cd6cbb08 | 645 | mdfd = create_mddev(mddev, name, c->autof, LOCAL, chosen_name, 1); |
e06af9dd JS |
646 | if (mdfd < 0) { |
647 | map_unlock(&map); | |
7f91af49 | 648 | return 1; |
e06af9dd | 649 | } |
3e9df86a AK |
650 | /* verify if chosen_name is not in use, |
651 | * it could be in conflict with already existing device | |
652 | * e.g. container, array | |
653 | */ | |
cf622ec1 JS |
654 | if (strncmp(chosen_name, "/dev/md/", 8) == 0 && |
655 | map_by_name(&map, chosen_name+8) != NULL) { | |
e7b84f9d | 656 | pr_err("Array name %s is in use already.\n", |
3e9df86a AK |
657 | chosen_name); |
658 | close(mdfd); | |
659 | map_unlock(&map); | |
cd6cbb08 | 660 | udev_unblock(); |
3e9df86a AK |
661 | return 1; |
662 | } | |
f2be09f1 | 663 | mddev = chosen_name; |
7f91af49 | 664 | |
5f4cc239 JS |
665 | memset(&inf, 0, sizeof(inf)); |
666 | md_get_array_info(mdfd, &inf); | |
667 | if (inf.working_disks != 0) { | |
668 | pr_err("another array by this name is already running.\n"); | |
e06af9dd | 669 | goto abort_locked; |
7f91af49 | 670 | } |
a04d5763 | 671 | |
682c7051 NB |
672 | /* Ok, lets try some ioctls */ |
673 | ||
99cc42f4 N |
674 | info.array.level = s->level; |
675 | info.array.size = s->size; | |
676 | info.array.raid_disks = s->raiddisks; | |
82b27616 NB |
677 | /* The kernel should *know* what md_minor we are dealing |
678 | * with, but it chooses to trust me instead. Sigh | |
679 | */ | |
06c7f68e | 680 | info.array.md_minor = 0; |
0a6bff09 ZL |
681 | if (fstat_is_blkdev(mdfd, mddev, &rdev)) |
682 | info.array.md_minor = minor(rdev); | |
06c7f68e | 683 | info.array.not_persistent = 0; |
6fb79233 | 684 | |
cf622ec1 JS |
685 | if (((s->level == 4 || s->level == 5) && |
686 | (insert_point < s->raiddisks || first_missing < s->raiddisks)) || | |
687 | (s->level == 6 && (insert_point < s->raiddisks || | |
688 | second_missing < s->raiddisks)) || | |
689 | (s->level <= 0) || s->assume_clean) { | |
06c7f68e | 690 | info.array.state = 1; /* clean, but one+ drive will be missing*/ |
b7528a20 | 691 | info.resync_start = MaxSector; |
103f2410 | 692 | } else { |
06c7f68e | 693 | info.array.state = 0; /* not clean, but no errors */ |
103f2410 NB |
694 | info.resync_start = 0; |
695 | } | |
99cc42f4 | 696 | if (s->level == 10) { |
f9c25f1d NB |
697 | /* for raid10, the bitmap size is the capacity of the array, |
698 | * which is array.size * raid_disks / ncopies; | |
699 | * .. but convert to sectors. | |
700 | */ | |
99cc42f4 N |
701 | int ncopies = ((s->layout>>8) & 255) * (s->layout & 255); |
702 | bitmapsize = s->size * s->raiddisks / ncopies * 2; | |
703 | /* printf("bms=%llu as=%d rd=%d nc=%d\n", bitmapsize, s->size, s->raiddisks, ncopies);*/ | |
f9c25f1d | 704 | } else |
99cc42f4 | 705 | bitmapsize = s->size * 2; |
f9c25f1d | 706 | |
82b27616 NB |
707 | /* There is lots of redundancy in these disk counts, |
708 | * raid_disks is the most meaningful value | |
709 | * it describes the geometry of the array | |
710 | * it is constant | |
711 | * nr_disks is total number of used slots. | |
712 | * it should be raid_disks+spare_disks | |
713 | * spare_disks is the number of extra disks present | |
714 | * see above | |
715 | * active_disks is the number of working disks in | |
716 | * active slots. (With raid_disks) | |
717 | * working_disks is the total number of working disks, | |
718 | * including spares | |
719 | * failed_disks is the number of disks marked failed | |
720 | * | |
5d500228 | 721 | * Ideally, the kernel would keep these (except raid_disks) |
82b27616 NB |
722 | * up-to-date as we ADD_NEW_DISK, but it doesn't (yet). |
723 | * So for now, we assume that all raid and spare | |
724 | * devices will be given. | |
725 | */ | |
99cc42f4 | 726 | info.array.spare_disks=s->sparedisks; |
06c7f68e NB |
727 | info.array.failed_disks=missing_disks; |
728 | info.array.nr_disks = info.array.working_disks | |
729 | + info.array.failed_disks; | |
99cc42f4 N |
730 | info.array.layout = s->layout; |
731 | info.array.chunk_size = s->chunk*1024; | |
82d9eba6 | 732 | |
b3b33eb5 NB |
733 | if (name == NULL || *name == 0) { |
734 | /* base name on mddev */ | |
d1e80164 NB |
735 | /* /dev/md0 -> 0 |
736 | * /dev/md_d0 -> d0 | |
eca944fa | 737 | * /dev/md_foo -> foo |
d1e80164 NB |
738 | * /dev/md/1 -> 1 |
739 | * /dev/md/d1 -> d1 | |
740 | * /dev/md/home -> home | |
741 | * /dev/mdhome -> home | |
742 | */ | |
69207ff6 | 743 | /* FIXME compare this with rules in create_mddev */ |
b3b33eb5 NB |
744 | name = strrchr(mddev, '/'); |
745 | if (name) { | |
746 | name++; | |
98dbf73c JS |
747 | if (strncmp(name, "md_", 3) == 0 && |
748 | strlen(name) > 3 && (name-mddev) == 5 /* /dev/ */) | |
d1e80164 | 749 | name += 3; |
98dbf73c JS |
750 | else if (strncmp(name, "md", 2) == 0 && |
751 | strlen(name) > 2 && isdigit(name[2]) && | |
60248f74 | 752 | (name-mddev) == 5 /* /dev/ */) |
b3b33eb5 NB |
753 | name += 2; |
754 | } | |
755 | } | |
5308f117 | 756 | if (!st->ss->init_super(st, &info.array, s, name, c->homehost, uuid, |
ae5dfc56 | 757 | s->data_offset)) |
e06af9dd | 758 | goto abort_locked; |
682c7051 | 759 | |
d2ca6449 | 760 | total_slots = info.array.nr_disks; |
a5d85af7 | 761 | st->ss->getinfo_super(st, &info, NULL); |
dae13137 JS |
762 | if (sysfs_init(&info, mdfd, NULL)) { |
763 | pr_err("unable to initialize sysfs\n"); | |
764 | goto abort_locked; | |
765 | } | |
159c3a1a | 766 | |
171dccc8 | 767 | if (did_default && c->verbose >= 0) { |
3c558363 | 768 | if (is_subarray(info.text_version)) { |
4dd2df09 N |
769 | char devnm[32]; |
770 | char *ep; | |
f7f1b6a1 | 771 | struct mdinfo *mdi; |
f7f1b6a1 | 772 | |
4dd2df09 N |
773 | strncpy(devnm, info.text_version+1, 32); |
774 | devnm[31] = 0; | |
775 | ep = strchr(devnm, '/'); | |
776 | if (ep) | |
777 | *ep = 0; | |
778 | ||
779 | mdi = sysfs_read(-1, devnm, GET_VERSION); | |
f7f1b6a1 | 780 | |
4dd2df09 N |
781 | pr_err("Creating array inside %s container %s\n", |
782 | mdi?mdi->text_version:"managed", devnm); | |
f7f1b6a1 NB |
783 | sysfs_free(mdi); |
784 | } else | |
7a862a02 | 785 | pr_err("Defaulting to version %s metadata\n", info.text_version); |
f7f1b6a1 | 786 | } |
b8ac1967 | 787 | |
4dd2df09 | 788 | map_update(&map, fd2devnm(mdfd), info.text_version, |
a04d5763 | 789 | info.uuid, chosen_name); |
19ad4b2c AP |
790 | /* Keep map locked until devices have been added to array |
791 | * to stop another mdadm from finding and using those devices. | |
792 | */ | |
a04d5763 | 793 | |
98dbf73c JS |
794 | if (s->bitmap_file && (strcmp(s->bitmap_file, "internal") == 0 || |
795 | strcmp(s->bitmap_file, "clustered") == 0)) { | |
ebeb3663 | 796 | if (!st->ss->add_internal_bitmap) { |
e7b84f9d | 797 | pr_err("internal bitmaps not supported with %s metadata\n", |
ebeb3663 | 798 | st->ss->name); |
19ad4b2c | 799 | goto abort_locked; |
ebeb3663 | 800 | } |
2ec2b7e9 JS |
801 | if (st->ss->add_internal_bitmap(st, &s->bitmap_chunk, |
802 | c->delay, s->write_behind, | |
803 | bitmapsize, 1, major_num)) { | |
e7b84f9d | 804 | pr_err("Given bitmap chunk size not supported.\n"); |
19ad4b2c | 805 | goto abort_locked; |
55935d51 | 806 | } |
99cc42f4 | 807 | s->bitmap_file = NULL; |
55935d51 NB |
808 | } |
809 | ||
dae13137 JS |
810 | if (sysfs_init(&info, mdfd, NULL)) { |
811 | pr_err("unable to initialize sysfs\n"); | |
812 | goto abort_locked; | |
813 | } | |
55935d51 | 814 | |
4dd2df09 | 815 | if (st->ss->external && st->container_devnm[0]) { |
f35f2525 N |
816 | /* member */ |
817 | ||
818 | /* When creating a member, we need to be careful | |
819 | * to negotiate with mdmon properly. | |
820 | * If it is already running, we cannot write to | |
821 | * the devices and must ask it to do that part. | |
822 | * If it isn't running, we write to the devices, | |
823 | * and then start it. | |
824 | * We hold an exclusive open on the container | |
825 | * device to make sure mdmon doesn't exit after | |
826 | * we checked that it is running. | |
827 | * | |
828 | * For now, fail if it is already running. | |
829 | */ | |
4dd2df09 | 830 | container_fd = open_dev_excl(st->container_devnm); |
f35f2525 | 831 | if (container_fd < 0) { |
7a862a02 | 832 | pr_err("Cannot get exclusive open on container - weird.\n"); |
19ad4b2c | 833 | goto abort_locked; |
d03373f1 | 834 | } |
4dd2df09 | 835 | if (mdmon_running(st->container_devnm)) { |
171dccc8 | 836 | if (c->verbose) |
7a862a02 | 837 | pr_err("reusing mdmon for %s.\n", |
4dd2df09 | 838 | st->container_devnm); |
f35f2525 N |
839 | st->update_tail = &st->updates; |
840 | } else | |
841 | need_mdmon = 1; | |
842 | } | |
843 | rv = set_array_info(mdfd, st, &info); | |
82d9eba6 | 844 | if (rv) { |
e7b84f9d | 845 | pr_err("failed to set array info for %s: %s\n", |
52826846 | 846 | mddev, strerror(errno)); |
19ad4b2c | 847 | goto abort_locked; |
682c7051 | 848 | } |
52826846 | 849 | |
99cc42f4 | 850 | if (s->bitmap_file) { |
c82f047c | 851 | int uuid[4]; |
55935d51 | 852 | |
3da92f27 | 853 | st->ss->uuid_from_super(st, uuid); |
99cc42f4 N |
854 | if (CreateBitmap(s->bitmap_file, c->force, (char*)uuid, s->bitmap_chunk, |
855 | c->delay, s->write_behind, | |
f9c25f1d | 856 | bitmapsize, |
943eafef | 857 | major_num)) { |
19ad4b2c | 858 | goto abort_locked; |
c82f047c | 859 | } |
99cc42f4 | 860 | bitmap_fd = open(s->bitmap_file, O_RDWR); |
c82f047c | 861 | if (bitmap_fd < 0) { |
ebf3be99 | 862 | pr_err("weird: %s cannot be opened\n", |
99cc42f4 | 863 | s->bitmap_file); |
19ad4b2c | 864 | goto abort_locked; |
c82f047c NB |
865 | } |
866 | if (ioctl(mdfd, SET_BITMAP_FILE, bitmap_fd) < 0) { | |
e7b84f9d | 867 | pr_err("Cannot set bitmap file for %s: %s\n", |
c82f047c | 868 | mddev, strerror(errno)); |
19ad4b2c | 869 | goto abort_locked; |
c82f047c NB |
870 | } |
871 | } | |
872 | ||
503975b9 | 873 | infos = xmalloc(sizeof(*infos) * total_slots); |
a7dec3fd | 874 | enable_fds(total_slots); |
98dbf73c | 875 | for (pass = 1; pass <= 2; pass++) { |
a655e550 | 876 | struct mddev_dev *moved_disk = NULL; /* the disk that was moved out of the insert point */ |
4b1ac34b | 877 | |
98dbf73c JS |
878 | for (dnum = 0, raid_disk_num = 0, dv = devlist; dv; |
879 | dv = (dv->next) ? (dv->next) : moved_disk, dnum++) { | |
4b1ac34b | 880 | int fd; |
d2ca6449 | 881 | struct mdinfo *inf = &infos[dnum]; |
4b1ac34b | 882 | |
d2ca6449 NB |
883 | if (dnum >= total_slots) |
884 | abort(); | |
4b1ac34b | 885 | if (dnum == insert_point) { |
f170a5a9 | 886 | raid_disk_num += 1; |
4b1ac34b | 887 | moved_disk = dv; |
8592f29d | 888 | continue; |
52826846 | 889 | } |
98dbf73c | 890 | if (strcasecmp(dv->devname, "missing") == 0) { |
f170a5a9 | 891 | raid_disk_num += 1; |
111d01fc | 892 | continue; |
f170a5a9 | 893 | } |
8592f29d N |
894 | if (have_container) |
895 | moved_disk = NULL; | |
896 | if (have_container && dnum < info.array.raid_disks - 1) | |
897 | /* repeatedly use the container */ | |
898 | moved_disk = dv; | |
111d01fc | 899 | |
d2ca6449 NB |
900 | switch(pass) { |
901 | case 1: | |
902 | *inf = info; | |
5f8097be | 903 | |
d2ca6449 | 904 | inf->disk.number = dnum; |
cc1799c3 SL |
905 | inf->disk.raid_disk = raid_disk_num++; |
906 | ||
907 | if (dv->disposition == 'j') { | |
908 | inf->disk.raid_disk = MD_DISK_ROLE_JOURNAL; | |
909 | inf->disk.state = (1<<MD_DISK_JOURNAL); | |
910 | raid_disk_num--; | |
911 | } else if (inf->disk.raid_disk < s->raiddisks) | |
d2ca6449 NB |
912 | inf->disk.state = (1<<MD_DISK_ACTIVE) | |
913 | (1<<MD_DISK_SYNC); | |
914 | else | |
915 | inf->disk.state = 0; | |
916 | ||
a0422106 NC |
917 | if (dv->writemostly == FlagSet) { |
918 | if (major_num == BITMAP_MAJOR_CLUSTERED) { | |
919 | pr_err("Can not set %s --write-mostly with a clustered bitmap\n",dv->devname); | |
920 | goto abort_locked; | |
921 | } else | |
922 | inf->disk.state |= (1<<MD_DISK_WRITEMOSTLY); | |
923 | } | |
e22fe3ae | 924 | if (dv->failfast == FlagSet) |
71574efb | 925 | inf->disk.state |= (1<<MD_DISK_FAILFAST); |
d2ca6449 | 926 | |
8592f29d N |
927 | if (have_container) |
928 | fd = -1; | |
929 | else { | |
d1d599ea | 930 | if (st->ss->external && |
4dd2df09 | 931 | st->container_devnm[0]) |
8592f29d N |
932 | fd = open(dv->devname, O_RDWR); |
933 | else | |
934 | fd = open(dv->devname, O_RDWR|O_EXCL); | |
935 | ||
936 | if (fd < 0) { | |
7a862a02 | 937 | pr_err("failed to open %s after earlier success - aborting\n", |
8592f29d | 938 | dv->devname); |
19ad4b2c | 939 | goto abort_locked; |
8592f29d | 940 | } |
0a6bff09 ZL |
941 | if (!fstat_is_blkdev(fd, dv->devname, &rdev)) |
942 | return 1; | |
943 | inf->disk.major = major(rdev); | |
944 | inf->disk.minor = minor(rdev); | |
d2ca6449 | 945 | } |
8592f29d N |
946 | if (fd >= 0) |
947 | remove_partitions(fd); | |
f20c3968 | 948 | if (st->ss->add_to_super(st, &inf->disk, |
72ca9bcf N |
949 | fd, dv->devname, |
950 | dv->data_offset)) { | |
4eb26970 | 951 | ioctl(mdfd, STOP_ARRAY, NULL); |
19ad4b2c | 952 | goto abort_locked; |
4eb26970 | 953 | } |
a5d85af7 | 954 | st->ss->getinfo_super(st, inf, NULL); |
8ed3e5e1 | 955 | safe_mode_delay = inf->safe_mode_delay; |
d2ca6449 | 956 | |
171dccc8 | 957 | if (have_container && c->verbose > 0) |
e7b84f9d | 958 | pr_err("Using %s for device %d\n", |
8592f29d N |
959 | map_dev(inf->disk.major, |
960 | inf->disk.minor, | |
961 | 0), dnum); | |
962 | ||
963 | if (!have_container) { | |
964 | /* getinfo_super might have lost these ... */ | |
0a6bff09 ZL |
965 | inf->disk.major = major(rdev); |
966 | inf->disk.minor = minor(rdev); | |
8592f29d | 967 | } |
4b1ac34b NB |
968 | break; |
969 | case 2: | |
d2ca6449 | 970 | inf->errors = 0; |
4b1ac34b | 971 | |
f35f2525 | 972 | rv = add_disk(mdfd, st, &info, inf); |
d2ca6449 | 973 | |
2503d23b | 974 | if (rv) { |
7a862a02 | 975 | pr_err("ADD_NEW_DISK for %s failed: %s\n", |
e7b84f9d | 976 | dv->devname, strerror(errno)); |
329dfc28 N |
977 | if (errno == EINVAL && |
978 | info.array.level == 0) { | |
979 | pr_err("Possibly your kernel doesn't support RAID0 layouts.\n"); | |
980 | pr_err("Either upgrade, or use --layout=dangerous\n"); | |
981 | } | |
19ad4b2c | 982 | goto abort_locked; |
4b1ac34b | 983 | } |
4b1ac34b NB |
984 | break; |
985 | } | |
8592f29d N |
986 | if (!have_container && |
987 | dv == moved_disk && dnum != insert_point) break; | |
52826846 | 988 | } |
edd8d13c | 989 | if (pass == 1) { |
9b1fb677 DW |
990 | struct mdinfo info_new; |
991 | struct map_ent *me = NULL; | |
992 | ||
993 | /* check to see if the uuid has changed due to these | |
994 | * metadata changes, and if so update the member array | |
995 | * and container uuid. Note ->write_init_super clears | |
996 | * the subarray cursor such that ->getinfo_super once | |
997 | * again returns container info. | |
998 | */ | |
a5d85af7 | 999 | st->ss->getinfo_super(st, &info_new, NULL); |
6f2af6a4 | 1000 | if (st->ss->external && !is_container(s->level) && |
9b1fb677 | 1001 | !same_uuid(info_new.uuid, info.uuid, 0)) { |
4dd2df09 | 1002 | map_update(&map, fd2devnm(mdfd), |
9b1fb677 DW |
1003 | info_new.text_version, |
1004 | info_new.uuid, chosen_name); | |
4dd2df09 | 1005 | me = map_by_devnm(&map, st->container_devnm); |
9b1fb677 DW |
1006 | } |
1007 | ||
ab80e597 | 1008 | if (st->ss->write_init_super(st)) { |
ab80e597 | 1009 | st->ss->free_super(st); |
e06af9dd | 1010 | goto abort_locked; |
ab80e597 | 1011 | } |
b554ab5c JR |
1012 | /* |
1013 | * Before activating the array, perform extra steps | |
1014 | * required to configure the internal write-intent | |
1015 | * bitmap. | |
1016 | */ | |
1017 | if (info_new.consistency_policy == | |
1018 | CONSISTENCY_POLICY_BITMAP && | |
1019 | st->ss->set_bitmap && | |
1020 | st->ss->set_bitmap(st, &info)) { | |
1021 | st->ss->free_super(st); | |
1022 | goto abort_locked; | |
1023 | } | |
9b1fb677 DW |
1024 | |
1025 | /* update parent container uuid */ | |
1026 | if (me) { | |
503975b9 | 1027 | char *path = xstrdup(me->path); |
9b1fb677 | 1028 | |
a5d85af7 | 1029 | st->ss->getinfo_super(st, &info_new, NULL); |
4dd2df09 | 1030 | map_update(&map, st->container_devnm, |
9b1fb677 DW |
1031 | info_new.text_version, |
1032 | info_new.uuid, path); | |
1033 | free(path); | |
1034 | } | |
9b1fb677 | 1035 | |
edd8d13c | 1036 | flush_metadata_updates(st); |
6946681d | 1037 | st->ss->free_super(st); |
edd8d13c | 1038 | } |
5e52ae9e | 1039 | } |
19ad4b2c | 1040 | map_unlock(&map); |
d2ca6449 | 1041 | free(infos); |
5e52ae9e | 1042 | |
6f2af6a4 | 1043 | if (is_container(s->level)) { |
97590376 N |
1044 | /* No need to start. But we should signal udev to |
1045 | * create links */ | |
1046 | sysfs_uevent(&info, "change"); | |
171dccc8 | 1047 | if (c->verbose >= 0) |
e7b84f9d | 1048 | pr_err("container %s prepared.\n", mddev); |
a7c6e3fb | 1049 | wait_for(chosen_name, mdfd); |
99cc42f4 | 1050 | } else if (c->runstop == 1 || subdevs >= s->raiddisks) { |
598f0d58 | 1051 | if (st->ss->external) { |
4c821454 | 1052 | int err; |
99cc42f4 | 1053 | switch(s->level) { |
598f0d58 NB |
1054 | case LEVEL_LINEAR: |
1055 | case LEVEL_MULTIPATH: | |
1056 | case 0: | |
4c821454 | 1057 | err = sysfs_set_str(&info, NULL, "array_state", |
171dccc8 | 1058 | c->readonly |
72d566f6 N |
1059 | ? "readonly" |
1060 | : "active"); | |
a931db9e | 1061 | need_mdmon = 0; |
598f0d58 NB |
1062 | break; |
1063 | default: | |
4c821454 N |
1064 | err = sysfs_set_str(&info, NULL, "array_state", |
1065 | "readonly"); | |
598f0d58 NB |
1066 | break; |
1067 | } | |
f35f2525 | 1068 | sysfs_set_safemode(&info, safe_mode_delay); |
4c821454 | 1069 | if (err) { |
7a862a02 | 1070 | pr_err("failed to activate array.\n"); |
4c821454 N |
1071 | ioctl(mdfd, STOP_ARRAY, NULL); |
1072 | goto abort; | |
1073 | } | |
171dccc8 | 1074 | } else if (c->readonly && |
72d566f6 N |
1075 | sysfs_attribute_available( |
1076 | &info, NULL, "array_state")) { | |
1077 | if (sysfs_set_str(&info, NULL, | |
1078 | "array_state", "readonly") < 0) { | |
1079 | pr_err("Failed to start array: %s\n", | |
1080 | strerror(errno)); | |
1081 | ioctl(mdfd, STOP_ARRAY, NULL); | |
1082 | goto abort; | |
1083 | } | |
598f0d58 | 1084 | } else { |
97590376 | 1085 | /* param is not actually used */ |
598f0d58 NB |
1086 | mdu_param_t param; |
1087 | if (ioctl(mdfd, RUN_ARRAY, ¶m)) { | |
e7b84f9d | 1088 | pr_err("RUN_ARRAY failed: %s\n", |
72d566f6 | 1089 | strerror(errno)); |
329dfc28 N |
1090 | if (errno == 524 /* ENOTSUP */ && |
1091 | info.array.level == 0) | |
1092 | cont_err("Please use --layout=original or --layout=alternate\n"); | |
a252c078 | 1093 | if (info.array.chunk_size & (info.array.chunk_size-1)) { |
7a862a02 | 1094 | cont_err("Problem may be that chunk size is not a power of 2\n"); |
a252c078 | 1095 | } |
4eb26970 | 1096 | ioctl(mdfd, STOP_ARRAY, NULL); |
7f91af49 | 1097 | goto abort; |
598f0d58 | 1098 | } |
a21e848a N |
1099 | /* if start_ro module parameter is set, array is |
1100 | * auto-read-only, which is bad as the resync won't | |
1101 | * start. So lets make it read-write now. | |
1102 | */ | |
1103 | ioctl(mdfd, RESTART_ARRAY_RW, NULL); | |
682c7051 | 1104 | } |
171dccc8 | 1105 | if (c->verbose >= 0) |
e7b84f9d | 1106 | pr_err("array %s started.\n", mddev); |
4dd2df09 | 1107 | if (st->ss->external && st->container_devnm[0]) { |
8850ee3e | 1108 | if (need_mdmon) |
4dd2df09 | 1109 | start_mdmon(st->container_devnm); |
8850ee3e | 1110 | |
4dd2df09 | 1111 | ping_monitor(st->container_devnm); |
a931db9e NB |
1112 | close(container_fd); |
1113 | } | |
a7c6e3fb | 1114 | wait_for(chosen_name, mdfd); |
682c7051 | 1115 | } else { |
e7b84f9d | 1116 | pr_err("not starting array - not enough devices.\n"); |
682c7051 | 1117 | } |
cd6cbb08 | 1118 | udev_unblock(); |
ce559078 MT |
1119 | close(mdfd); |
1120 | sysfs_uevent(&info, "change"); | |
682c7051 | 1121 | return 0; |
7f91af49 N |
1122 | |
1123 | abort: | |
cd6cbb08 | 1124 | udev_unblock(); |
4eb26970 | 1125 | map_lock(&map); |
e06af9dd | 1126 | abort_locked: |
4dd2df09 | 1127 | map_remove(&map, fd2devnm(mdfd)); |
4eb26970 DW |
1128 | map_unlock(&map); |
1129 | ||
7f91af49 N |
1130 | if (mdfd >= 0) |
1131 | close(mdfd); | |
1132 | return 1; | |
64c4757e | 1133 | } |