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