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