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