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