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