]> git.ipfire.org Git - thirdparty/mdadm.git/blame - Build.c
Document this...
[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
NB
38 mddev_dev_t devlist, int assume_clean,
39 char *bitmap_file, int bitmap_chunk, 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;
cd29a5c8
NB
136 if (chunk == 0)
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;
167 disk.state = 6;
0df46c2a
NB
168 disk.major = major(stb.st_rdev);
169 disk.minor = minor(stb.st_rdev);
82b27616
NB
170 if (ioctl(mdfd, ADD_NEW_DISK, &disk)) {
171 fprintf(stderr, Name ": ADD_NEW_DISK failed for %s: %s\n",
cd29a5c8 172 dv->devname, strerror(errno));
82b27616
NB
173 goto abort;
174 }
175 } else {
176 if (ioctl(mdfd, REGISTER_DEV, &stb.st_rdev)) {
e0d19036 177 fprintf(stderr, Name ": REGISTER_DEV failed for %s: %s.\n",
cd29a5c8 178 dv->devname, strerror(errno));
82b27616
NB
179 goto abort;
180 }
181 }
182 }
183 /* now to start it */
cd29a5c8 184 if (vers >= 9000) {
82b27616 185 mdu_param_t param; /* not used by syscall */
c82f047c
NB
186 if (bitmap_file) {
187 bitmap_fd = open(bitmap_file, O_RDWR);
188 if (bitmap_fd < 0) {
189 if (bitmap_chunk == UnSet) {
190 fprintf(stderr, Name ": %s cannot be openned.",
191 bitmap_file);
192 return 1;
193 }
194 if (CreateBitmap(bitmap_file, 1, NULL, bitmap_chunk,
195 delay, 0/* FIXME size */)) {
196 return 1;
197 }
198 bitmap_fd = open(bitmap_file, O_RDWR);
199 if (bitmap_fd < 0) {
200 fprintf(stderr, Name ": %s cannot be openned.",
201 bitmap_file);
202 return 1;
203 }
204 }
205 if (bitmap_fd >= 0) {
206 if (ioctl(mdfd, SET_BITMAP_FILE, bitmap_fd) < 0) {
207 fprintf(stderr, Name ": Cannot set bitmap file for %s: %s\n",
208 mddev, strerror(errno));
209 return 1;
210 }
211 }
212 }
cd29a5c8 213 if (ioctl(mdfd, RUN_ARRAY, &param)) {
82b27616
NB
214 fprintf(stderr, Name ": RUN_ARRAY failed: %s\n",
215 strerror(errno));
216 goto abort;
217 }
218 } else {
cd29a5c8 219 unsigned long arg;
82b27616
NB
220 arg=0;
221 while (chunk > 4096) {
222 arg++;
223 chunk >>= 1;
224 }
225 if (level == 0)
226 chunk |= 0x20000;
227 else chunk |= 0x10000;
228 if (ioctl(mdfd, START_MD, arg)) {
229 fprintf(stderr, Name ": START_MD failed: %s\n",
230 strerror(errno));
231 goto abort;
232 }
233 }
234 fprintf(stderr, Name ": array %s built and started.\n",
235 mddev);
236 return 0;
237
238 abort:
cd29a5c8 239 if (vers >= 9000)
82b27616
NB
240 ioctl(mdfd, STOP_ARRAY, 0);
241 else
242 ioctl(mdfd, STOP_MD, 0);
243 return 1;
244
64c4757e 245}