]>
Commit | Line | Data |
---|---|---|
64c4757e | 1 | /* |
9a9dab36 | 2 | * mdadm - manage Linux "md" devices aka RAID arrays. |
64c4757e | 3 | * |
e736b623 | 4 | * Copyright (C) 2001-2009 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 | ||
9a9dab36 | 25 | #include "mdadm.h" |
64c4757e | 26 | |
82b27616 NB |
27 | #define REGISTER_DEV _IO (MD_MAJOR, 1) |
28 | #define START_MD _IO (MD_MAJOR, 2) | |
29 | #define STOP_MD _IO (MD_MAJOR, 3) | |
30 | ||
7f91af49 N |
31 | int Build(char *mddev, int chunk, int level, int layout, |
32 | int raiddisks, mddev_dev_t devlist, int assume_clean, | |
33 | char *bitmap_file, int bitmap_chunk, int write_behind, | |
83208785 | 34 | int delay, int verbose, int autof, unsigned long long size) |
64c4757e | 35 | { |
82b27616 NB |
36 | /* Build a linear or raid0 arrays without superblocks |
37 | * We cannot really do any checks, we just do it. | |
38 | * For md_version < 0.90.0, we call REGISTER_DEV | |
39 | * with the device numbers, and then | |
40 | * START_MD giving the "geometry" | |
41 | * geometry is 0xpp00cc | |
42 | * where pp is personality: 1==linear, 2=raid0 | |
43 | * cc = chunk size factor: 0==4k, 1==8k etc. | |
44 | * | |
45 | * For md_version >= 0.90.0 we call | |
46 | * SET_ARRAY_INFO, ADD_NEW_DISK, RUN_ARRAY | |
47 | * | |
48 | */ | |
49 | int i; | |
50 | int vers; | |
51 | struct stat stb; | |
98ce3849 | 52 | int subdevs = 0, missing_disks = 0; |
cd29a5c8 | 53 | mddev_dev_t dv; |
c82f047c | 54 | int bitmap_fd; |
f9c25f1d | 55 | unsigned long long bitmapsize; |
7f91af49 | 56 | int mdfd; |
a04d5763 N |
57 | char chosen_name[1024]; |
58 | int uuid[4] = {0,0,0,0}; | |
ad5bc697 | 59 | struct map_ent *map = NULL; |
82b27616 NB |
60 | |
61 | /* scan all devices, make sure they really are block devices */ | |
cd29a5c8 | 62 | for (dv = devlist; dv; dv=dv->next) { |
98ce3849 NB |
63 | subdevs++; |
64 | if (strcmp("missing", dv->devname) == 0) { | |
65 | missing_disks++; | |
66 | continue; | |
67 | } | |
cd29a5c8 | 68 | if (stat(dv->devname, &stb)) { |
82b27616 | 69 | fprintf(stderr, Name ": Cannot find %s: %s\n", |
cd29a5c8 | 70 | dv->devname, strerror(errno)); |
82b27616 NB |
71 | return 1; |
72 | } | |
73 | if ((stb.st_mode & S_IFMT) != S_IFBLK) { | |
74 | fprintf(stderr, Name ": %s is not a block device.\n", | |
cd29a5c8 | 75 | dv->devname); |
82b27616 NB |
76 | return 1; |
77 | } | |
cd29a5c8 NB |
78 | } |
79 | ||
80 | if (raiddisks != subdevs) { | |
81 | fprintf(stderr, Name ": requested %d devices in array but listed %d\n", | |
82 | raiddisks, subdevs); | |
83 | return 1; | |
82b27616 NB |
84 | } |
85 | ||
b5e64645 NB |
86 | if (layout == UnSet) |
87 | switch(level) { | |
88 | default: /* no layout */ | |
89 | layout = 0; | |
90 | break; | |
91 | case 10: | |
92 | layout = 0x102; /* near=2, far=1 */ | |
dab6685f | 93 | if (verbose > 0) |
b5e64645 NB |
94 | fprintf(stderr, |
95 | Name ": layout defaults to n1\n"); | |
96 | break; | |
97 | case 5: | |
98 | case 6: | |
99 | layout = map_name(r5layout, "default"); | |
dab6685f | 100 | if (verbose > 0) |
b5e64645 NB |
101 | fprintf(stderr, |
102 | Name ": layout defaults to %s\n", map_num(r5layout, layout)); | |
103 | break; | |
104 | case LEVEL_FAULTY: | |
105 | layout = map_name(faultylayout, "default"); | |
106 | ||
dab6685f | 107 | if (verbose > 0) |
b5e64645 NB |
108 | fprintf(stderr, |
109 | Name ": layout defaults to %s\n", map_num(faultylayout, layout)); | |
110 | break; | |
111 | } | |
112 | ||
69207ff6 | 113 | /* We need to create the device. It can have no name. */ |
ad5bc697 | 114 | map_lock(&map); |
a04d5763 N |
115 | mdfd = create_mddev(mddev, NULL, autof, LOCAL, |
116 | chosen_name); | |
ad5bc697 N |
117 | if (mdfd < 0) { |
118 | map_unlock(&map); | |
7f91af49 | 119 | return 1; |
ad5bc697 | 120 | } |
a714580e | 121 | mddev = chosen_name; |
b5e64645 | 122 | |
ad5bc697 N |
123 | map_update(&map, fd2devnum(mdfd), "none", uuid, chosen_name); |
124 | map_unlock(&map); | |
a04d5763 | 125 | |
82b27616 | 126 | vers = md_get_version(mdfd); |
aba69144 | 127 | |
82b27616 | 128 | /* looks Ok, go for it */ |
cd29a5c8 | 129 | if (vers >= 9000) { |
82b27616 NB |
130 | mdu_array_info_t array; |
131 | array.level = level; | |
25affb56 | 132 | array.size = size; |
82b27616 NB |
133 | array.nr_disks = raiddisks; |
134 | array.raid_disks = raiddisks; | |
135 | array.md_minor = 0; | |
136 | if (fstat(mdfd, &stb)==0) | |
0df46c2a | 137 | array.md_minor = minor(stb.st_rdev); |
cd29a5c8 | 138 | array.not_persistent = 1; |
82b27616 | 139 | array.state = 0; /* not clean, but no errors */ |
dd0781e5 NB |
140 | if (assume_clean) |
141 | array.state |= 1; | |
98ce3849 NB |
142 | array.active_disks = raiddisks - missing_disks; |
143 | array.working_disks = raiddisks - missing_disks; | |
82b27616 | 144 | array.spare_disks = 0; |
98ce3849 | 145 | array.failed_disks = missing_disks; |
699f9899 | 146 | if (chunk == 0 && (level==0 || level==LEVEL_LINEAR)) |
cd29a5c8 | 147 | chunk = 64; |
82b27616 | 148 | array.chunk_size = chunk*1024; |
b5e64645 | 149 | array.layout = layout; |
82b27616 NB |
150 | if (ioctl(mdfd, SET_ARRAY_INFO, &array)) { |
151 | fprintf(stderr, Name ": SET_ARRAY_INFO failed for %s: %s\n", | |
152 | mddev, strerror(errno)); | |
7f91af49 | 153 | goto abort; |
82b27616 | 154 | } |
c82f047c NB |
155 | } else if (bitmap_file) { |
156 | fprintf(stderr, Name ": bitmaps not supported with this kernel\n"); | |
7f91af49 | 157 | goto abort; |
82b27616 | 158 | } |
5b28bd56 NB |
159 | |
160 | if (bitmap_file && level <= 0) { | |
161 | fprintf(stderr, Name ": bitmaps not meaningful with level %s\n", | |
162 | map_num(pers, level)?:"given"); | |
7f91af49 | 163 | goto abort; |
5b28bd56 | 164 | } |
82b27616 | 165 | /* now add the devices */ |
cd29a5c8 | 166 | for ((i=0), (dv = devlist) ; dv ; i++, dv=dv->next) { |
beae1dfe | 167 | unsigned long long dsize; |
fe80f49b | 168 | int fd; |
98ce3849 NB |
169 | if (strcmp("missing", dv->devname) == 0) |
170 | continue; | |
cd29a5c8 | 171 | if (stat(dv->devname, &stb)) { |
98ce3849 | 172 | fprintf(stderr, Name ": Weird: %s has disappeared.\n", |
cd29a5c8 | 173 | dv->devname); |
82b27616 NB |
174 | goto abort; |
175 | } | |
cd29a5c8 | 176 | if ((stb.st_mode & S_IFMT)!= S_IFBLK) { |
82b27616 | 177 | fprintf(stderr, Name ": Wierd: %s is no longer a block device.\n", |
cd29a5c8 | 178 | dv->devname); |
82b27616 NB |
179 | goto abort; |
180 | } | |
fe80f49b NB |
181 | fd = open(dv->devname, O_RDONLY|O_EXCL); |
182 | if (fd < 0) { | |
aba69144 | 183 | fprintf(stderr, Name ": Cannot open %s: %s\n", |
fe80f49b NB |
184 | dv->devname, strerror(errno)); |
185 | goto abort; | |
186 | } | |
beae1dfe NB |
187 | if (get_dev_size(fd, NULL, &dsize) && |
188 | (size == 0 || dsize < size)) | |
189 | size = dsize; | |
fe80f49b | 190 | close(fd); |
25affb56 | 191 | if (vers >= 9000) { |
82b27616 NB |
192 | mdu_disk_info_t disk; |
193 | disk.number = i; | |
194 | disk.raid_disk = i; | |
dfd4d8ee | 195 | disk.state = (1<<MD_DISK_SYNC) | (1<<MD_DISK_ACTIVE); |
b3d31955 | 196 | if (dv->writemostly == 1) |
dfd4d8ee | 197 | disk.state |= 1<<MD_DISK_WRITEMOSTLY; |
0df46c2a NB |
198 | disk.major = major(stb.st_rdev); |
199 | disk.minor = minor(stb.st_rdev); | |
82b27616 NB |
200 | if (ioctl(mdfd, ADD_NEW_DISK, &disk)) { |
201 | fprintf(stderr, Name ": ADD_NEW_DISK failed for %s: %s\n", | |
cd29a5c8 | 202 | dv->devname, strerror(errno)); |
82b27616 NB |
203 | goto abort; |
204 | } | |
205 | } else { | |
206 | if (ioctl(mdfd, REGISTER_DEV, &stb.st_rdev)) { | |
e0d19036 | 207 | fprintf(stderr, Name ": REGISTER_DEV failed for %s: %s.\n", |
cd29a5c8 | 208 | dv->devname, strerror(errno)); |
82b27616 NB |
209 | goto abort; |
210 | } | |
211 | } | |
212 | } | |
213 | /* now to start it */ | |
cd29a5c8 | 214 | if (vers >= 9000) { |
82b27616 | 215 | mdu_param_t param; /* not used by syscall */ |
c82f047c NB |
216 | if (bitmap_file) { |
217 | bitmap_fd = open(bitmap_file, O_RDWR); | |
218 | if (bitmap_fd < 0) { | |
dcec9ee5 | 219 | int major = BITMAP_MAJOR_HI; |
1bfdbe01 | 220 | #if 0 |
c82f047c NB |
221 | if (bitmap_chunk == UnSet) { |
222 | fprintf(stderr, Name ": %s cannot be openned.", | |
223 | bitmap_file); | |
7f91af49 | 224 | goto abort; |
c82f047c | 225 | } |
1bfdbe01 | 226 | #endif |
dcec9ee5 NB |
227 | if (vers < 9003) { |
228 | major = BITMAP_MAJOR_HOSTENDIAN; | |
229 | #ifdef __BIG_ENDIAN | |
230 | fprintf(stderr, Name ": Warning - bitmaps created on this kernel are not portable\n" | |
1bfdbe01 | 231 | " between different architectures. Consider upgrading the Linux kernel.\n"); |
dcec9ee5 NB |
232 | #endif |
233 | } | |
f9c25f1d | 234 | bitmapsize = size>>9; /* FIXME wrong for RAID10 */ |
c82f047c | 235 | if (CreateBitmap(bitmap_file, 1, NULL, bitmap_chunk, |
f9c25f1d | 236 | delay, write_behind, bitmapsize, major)) { |
7f91af49 | 237 | goto abort; |
c82f047c NB |
238 | } |
239 | bitmap_fd = open(bitmap_file, O_RDWR); | |
240 | if (bitmap_fd < 0) { | |
241 | fprintf(stderr, Name ": %s cannot be openned.", | |
242 | bitmap_file); | |
7f91af49 | 243 | goto abort; |
c82f047c | 244 | } |
aba69144 | 245 | } |
c82f047c NB |
246 | if (bitmap_fd >= 0) { |
247 | if (ioctl(mdfd, SET_BITMAP_FILE, bitmap_fd) < 0) { | |
248 | fprintf(stderr, Name ": Cannot set bitmap file for %s: %s\n", | |
249 | mddev, strerror(errno)); | |
7f91af49 | 250 | goto abort; |
c82f047c NB |
251 | } |
252 | } | |
253 | } | |
cd29a5c8 | 254 | if (ioctl(mdfd, RUN_ARRAY, ¶m)) { |
82b27616 NB |
255 | fprintf(stderr, Name ": RUN_ARRAY failed: %s\n", |
256 | strerror(errno)); | |
257 | goto abort; | |
258 | } | |
259 | } else { | |
cd29a5c8 | 260 | unsigned long arg; |
82b27616 NB |
261 | arg=0; |
262 | while (chunk > 4096) { | |
263 | arg++; | |
264 | chunk >>= 1; | |
265 | } | |
266 | if (level == 0) | |
267 | chunk |= 0x20000; | |
268 | else chunk |= 0x10000; | |
269 | if (ioctl(mdfd, START_MD, arg)) { | |
270 | fprintf(stderr, Name ": START_MD failed: %s\n", | |
271 | strerror(errno)); | |
272 | goto abort; | |
273 | } | |
274 | } | |
dab6685f NB |
275 | if (verbose >= 0) |
276 | fprintf(stderr, Name ": array %s built and started.\n", | |
277 | mddev); | |
a7c6e3fb | 278 | wait_for(mddev, mdfd); |
7f91af49 | 279 | close(mdfd); |
82b27616 NB |
280 | return 0; |
281 | ||
282 | abort: | |
cd29a5c8 | 283 | if (vers >= 9000) |
82b27616 NB |
284 | ioctl(mdfd, STOP_ARRAY, 0); |
285 | else | |
286 | ioctl(mdfd, STOP_MD, 0); | |
7f91af49 | 287 | close(mdfd); |
82b27616 | 288 | return 1; |
64c4757e | 289 | } |