]> git.ipfire.org Git - thirdparty/mdadm.git/blame - Build.c
Add a test.
[thirdparty/mdadm.git] / Build.c
CommitLineData
64c4757e 1/*
9a9dab36 2 * mdadm - manage Linux "md" devices aka RAID arrays.
64c4757e 3 *
cd29a5c8 4 * Copyright (C) 2001-2002 Neil Brown <neilb@cse.unsw.edu.au>
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
b5e64645 36int Build(char *mddev, int mdfd, int chunk, int level, int layout,
64c4757e 37 int raiddisks,
c82f047c 38 mddev_dev_t devlist, int assume_clean,
dfd4d8ee 39 char *bitmap_file, int bitmap_chunk, int write_behind, int delay)
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 */
b5e64645 54 int verbose = 0;
82b27616
NB
55 int i;
56 int vers;
57 struct stat stb;
98ce3849 58 int subdevs = 0, missing_disks = 0;
cd29a5c8 59 mddev_dev_t dv;
c82f047c 60 int bitmap_fd;
98ce3849 61/* unsigned long long size = ~0ULL; / * needed for bitmap only */
82b27616
NB
62
63 /* scan all devices, make sure they really are block devices */
cd29a5c8 64 for (dv = devlist; dv; dv=dv->next) {
98ce3849
NB
65 subdevs++;
66 if (strcmp("missing", dv->devname) == 0) {
67 missing_disks++;
68 continue;
69 }
cd29a5c8 70 if (stat(dv->devname, &stb)) {
82b27616 71 fprintf(stderr, Name ": Cannot find %s: %s\n",
cd29a5c8 72 dv->devname, strerror(errno));
82b27616
NB
73 return 1;
74 }
75 if ((stb.st_mode & S_IFMT) != S_IFBLK) {
76 fprintf(stderr, Name ": %s is not a block device.\n",
cd29a5c8 77 dv->devname);
82b27616
NB
78 return 1;
79 }
cd29a5c8
NB
80 }
81
82 if (raiddisks != subdevs) {
83 fprintf(stderr, Name ": requested %d devices in array but listed %d\n",
84 raiddisks, subdevs);
85 return 1;
82b27616
NB
86 }
87
b5e64645
NB
88 if (layout == UnSet)
89 switch(level) {
90 default: /* no layout */
91 layout = 0;
92 break;
93 case 10:
94 layout = 0x102; /* near=2, far=1 */
95 if (verbose)
96 fprintf(stderr,
97 Name ": layout defaults to n1\n");
98 break;
99 case 5:
100 case 6:
101 layout = map_name(r5layout, "default");
102 if (verbose)
103 fprintf(stderr,
104 Name ": layout defaults to %s\n", map_num(r5layout, layout));
105 break;
106 case LEVEL_FAULTY:
107 layout = map_name(faultylayout, "default");
108
109 if (verbose)
110 fprintf(stderr,
111 Name ": layout defaults to %s\n", map_num(faultylayout, layout));
112 break;
113 }
114
115
82b27616
NB
116 vers = md_get_version(mdfd);
117
118 /* looks Ok, go for it */
cd29a5c8 119 if (vers >= 9000) {
82b27616
NB
120 mdu_array_info_t array;
121 array.level = level;
122 array.size = 0;
123 array.nr_disks = raiddisks;
124 array.raid_disks = raiddisks;
125 array.md_minor = 0;
126 if (fstat(mdfd, &stb)==0)
0df46c2a 127 array.md_minor = minor(stb.st_rdev);
cd29a5c8 128 array.not_persistent = 1;
82b27616 129 array.state = 0; /* not clean, but no errors */
dd0781e5
NB
130 if (assume_clean)
131 array.state |= 1;
98ce3849
NB
132 array.active_disks = raiddisks - missing_disks;
133 array.working_disks = raiddisks - missing_disks;
82b27616 134 array.spare_disks = 0;
98ce3849 135 array.failed_disks = missing_disks;
699f9899 136 if (chunk == 0 && (level==0 || level==LEVEL_LINEAR))
cd29a5c8 137 chunk = 64;
82b27616 138 array.chunk_size = chunk*1024;
b5e64645 139 array.layout = layout;
82b27616
NB
140 if (ioctl(mdfd, SET_ARRAY_INFO, &array)) {
141 fprintf(stderr, Name ": SET_ARRAY_INFO failed for %s: %s\n",
142 mddev, strerror(errno));
143 return 1;
144 }
c82f047c
NB
145 } else if (bitmap_file) {
146 fprintf(stderr, Name ": bitmaps not supported with this kernel\n");
147 return 1;
82b27616
NB
148 }
149 /* now add the devices */
cd29a5c8 150 for ((i=0), (dv = devlist) ; dv ; i++, dv=dv->next) {
98ce3849
NB
151 if (strcmp("missing", dv->devname) == 0)
152 continue;
cd29a5c8 153 if (stat(dv->devname, &stb)) {
98ce3849 154 fprintf(stderr, Name ": Weird: %s has disappeared.\n",
cd29a5c8 155 dv->devname);
82b27616
NB
156 goto abort;
157 }
cd29a5c8 158 if ((stb.st_mode & S_IFMT)!= S_IFBLK) {
82b27616 159 fprintf(stderr, Name ": Wierd: %s is no longer a block device.\n",
cd29a5c8 160 dv->devname);
82b27616
NB
161 goto abort;
162 }
cd29a5c8 163 if (vers>= 9000) {
82b27616
NB
164 mdu_disk_info_t disk;
165 disk.number = i;
166 disk.raid_disk = i;
dfd4d8ee
NB
167 disk.state = (1<<MD_DISK_SYNC) | (1<<MD_DISK_ACTIVE);
168 if (dv->writemostly)
169 disk.state |= 1<<MD_DISK_WRITEMOSTLY;
0df46c2a
NB
170 disk.major = major(stb.st_rdev);
171 disk.minor = minor(stb.st_rdev);
82b27616
NB
172 if (ioctl(mdfd, ADD_NEW_DISK, &disk)) {
173 fprintf(stderr, Name ": ADD_NEW_DISK failed for %s: %s\n",
cd29a5c8 174 dv->devname, strerror(errno));
82b27616
NB
175 goto abort;
176 }
177 } else {
178 if (ioctl(mdfd, REGISTER_DEV, &stb.st_rdev)) {
e0d19036 179 fprintf(stderr, Name ": REGISTER_DEV failed for %s: %s.\n",
cd29a5c8 180 dv->devname, strerror(errno));
82b27616
NB
181 goto abort;
182 }
183 }
184 }
185 /* now to start it */
cd29a5c8 186 if (vers >= 9000) {
82b27616 187 mdu_param_t param; /* not used by syscall */
c82f047c
NB
188 if (bitmap_file) {
189 bitmap_fd = open(bitmap_file, O_RDWR);
190 if (bitmap_fd < 0) {
191 if (bitmap_chunk == UnSet) {
192 fprintf(stderr, Name ": %s cannot be openned.",
193 bitmap_file);
194 return 1;
195 }
196 if (CreateBitmap(bitmap_file, 1, NULL, bitmap_chunk,
dfd4d8ee 197 delay, write_behind, 0/* FIXME size */)) {
c82f047c
NB
198 return 1;
199 }
200 bitmap_fd = open(bitmap_file, O_RDWR);
201 if (bitmap_fd < 0) {
202 fprintf(stderr, Name ": %s cannot be openned.",
203 bitmap_file);
204 return 1;
205 }
206 }
207 if (bitmap_fd >= 0) {
208 if (ioctl(mdfd, SET_BITMAP_FILE, bitmap_fd) < 0) {
209 fprintf(stderr, Name ": Cannot set bitmap file for %s: %s\n",
210 mddev, strerror(errno));
211 return 1;
212 }
213 }
214 }
cd29a5c8 215 if (ioctl(mdfd, RUN_ARRAY, &param)) {
82b27616
NB
216 fprintf(stderr, Name ": RUN_ARRAY failed: %s\n",
217 strerror(errno));
218 goto abort;
219 }
220 } else {
cd29a5c8 221 unsigned long arg;
82b27616
NB
222 arg=0;
223 while (chunk > 4096) {
224 arg++;
225 chunk >>= 1;
226 }
227 if (level == 0)
228 chunk |= 0x20000;
229 else chunk |= 0x10000;
230 if (ioctl(mdfd, START_MD, arg)) {
231 fprintf(stderr, Name ": START_MD failed: %s\n",
232 strerror(errno));
233 goto abort;
234 }
235 }
236 fprintf(stderr, Name ": array %s built and started.\n",
237 mddev);
238 return 0;
239
240 abort:
cd29a5c8 241 if (vers >= 9000)
82b27616
NB
242 ioctl(mdfd, STOP_ARRAY, 0);
243 else
244 ioctl(mdfd, STOP_MD, 0);
245 return 1;
246
64c4757e 247}