]> git.ipfire.org Git - thirdparty/mdadm.git/blob - Build.c
mdadm/util: unify fstat checking blkdev into function
[thirdparty/mdadm.git] / Build.c
1 /*
2 * mdadm - manage Linux "md" devices aka RAID arrays.
3 *
4 * Copyright (C) 2001-2009 Neil Brown <neilb@suse.de>
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@suse.de>
23 */
24
25 #include "mdadm.h"
26
27 #define REGISTER_DEV _IO (MD_MAJOR, 1)
28 #define START_MD _IO (MD_MAJOR, 2)
29 #define STOP_MD _IO (MD_MAJOR, 3)
30
31 int Build(char *mddev, struct mddev_dev *devlist,
32 struct shape *s, struct context *c)
33 {
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 int i;
44 struct stat stb;
45 dev_t rdev;
46 int subdevs = 0, missing_disks = 0;
47 struct mddev_dev *dv;
48 int bitmap_fd;
49 unsigned long long bitmapsize;
50 int mdfd;
51 char chosen_name[1024];
52 int uuid[4] = {0,0,0,0};
53 struct map_ent *map = NULL;
54 mdu_array_info_t array;
55 mdu_param_t param; /* not used by syscall */
56
57 if (s->level == UnSet) {
58 pr_err("a RAID level is needed to Build an array.\n");
59 return 1;
60 }
61 /* scan all devices, make sure they really are block devices */
62 for (dv = devlist; dv; dv=dv->next) {
63 subdevs++;
64 if (strcmp("missing", dv->devname) == 0) {
65 missing_disks++;
66 continue;
67 }
68 if (stat(dv->devname, &stb)) {
69 pr_err("Cannot find %s: %s\n",
70 dv->devname, strerror(errno));
71 return 1;
72 }
73 if ((stb.st_mode & S_IFMT) != S_IFBLK) {
74 pr_err("%s is not a block device.\n",
75 dv->devname);
76 return 1;
77 }
78 }
79
80 if (s->raiddisks != subdevs) {
81 pr_err("requested %d devices in array but listed %d\n",
82 s->raiddisks, subdevs);
83 return 1;
84 }
85
86 if (s->layout == UnSet)
87 switch(s->level) {
88 default: /* no layout */
89 s->layout = 0;
90 break;
91 case 10:
92 s->layout = 0x102; /* near=2, far=1 */
93 if (c->verbose > 0)
94 pr_err("layout defaults to n1\n");
95 break;
96 case 5:
97 case 6:
98 s->layout = map_name(r5layout, "default");
99 if (c->verbose > 0)
100 pr_err("layout defaults to %s\n", map_num(r5layout, s->layout));
101 break;
102 case LEVEL_FAULTY:
103 s->layout = map_name(faultylayout, "default");
104
105 if (c->verbose > 0)
106 pr_err("layout defaults to %s\n", map_num(faultylayout, s->layout));
107 break;
108 }
109
110 /* We need to create the device. It can have no name. */
111 map_lock(&map);
112 mdfd = create_mddev(mddev, NULL, c->autof, LOCAL,
113 chosen_name, 0);
114 if (mdfd < 0) {
115 map_unlock(&map);
116 return 1;
117 }
118 mddev = chosen_name;
119
120 map_update(&map, fd2devnm(mdfd), "none", uuid, chosen_name);
121 map_unlock(&map);
122
123 array.level = s->level;
124 if (s->size == MAX_SIZE)
125 s->size = 0;
126 array.size = s->size;
127 array.nr_disks = s->raiddisks;
128 array.raid_disks = s->raiddisks;
129 array.md_minor = 0;
130 if (fstat_is_blkdev(mdfd, mddev, &rdev))
131 array.md_minor = minor(rdev);
132 array.not_persistent = 1;
133 array.state = 0; /* not clean, but no errors */
134 if (s->assume_clean)
135 array.state |= 1;
136 array.active_disks = s->raiddisks - missing_disks;
137 array.working_disks = s->raiddisks - missing_disks;
138 array.spare_disks = 0;
139 array.failed_disks = missing_disks;
140 if (s->chunk == 0 && (s->level==0 || s->level==LEVEL_LINEAR))
141 s->chunk = 64;
142 array.chunk_size = s->chunk*1024;
143 array.layout = s->layout;
144 if (md_set_array_info(mdfd, &array)) {
145 pr_err("md_set_array_info() failed for %s: %s\n",
146 mddev, strerror(errno));
147 goto abort;
148 }
149
150 if (s->bitmap_file && strcmp(s->bitmap_file, "none") == 0)
151 s->bitmap_file = NULL;
152 if (s->bitmap_file && s->level <= 0) {
153 pr_err("bitmaps not meaningful with level %s\n",
154 map_num(pers, s->level)?:"given");
155 goto abort;
156 }
157 /* now add the devices */
158 for ((i=0), (dv = devlist) ; dv ; i++, dv=dv->next) {
159 mdu_disk_info_t disk;
160 unsigned long long dsize;
161 int fd;
162
163 if (strcmp("missing", dv->devname) == 0)
164 continue;
165 if (stat(dv->devname, &stb)) {
166 pr_err("Weird: %s has disappeared.\n",
167 dv->devname);
168 goto abort;
169 }
170 if ((stb.st_mode & S_IFMT)!= S_IFBLK) {
171 pr_err("Weird: %s is no longer a block device.\n",
172 dv->devname);
173 goto abort;
174 }
175 fd = open(dv->devname, O_RDONLY|O_EXCL);
176 if (fd < 0) {
177 pr_err("Cannot open %s: %s\n",
178 dv->devname, strerror(errno));
179 goto abort;
180 }
181 if (get_dev_size(fd, NULL, &dsize) &&
182 (s->size == 0 || s->size == MAX_SIZE || dsize < s->size))
183 s->size = dsize;
184 close(fd);
185 disk.number = i;
186 disk.raid_disk = i;
187 disk.state = (1<<MD_DISK_SYNC) | (1<<MD_DISK_ACTIVE);
188 if (dv->writemostly == FlagSet)
189 disk.state |= 1<<MD_DISK_WRITEMOSTLY;
190 disk.major = major(stb.st_rdev);
191 disk.minor = minor(stb.st_rdev);
192 if (ioctl(mdfd, ADD_NEW_DISK, &disk)) {
193 pr_err("ADD_NEW_DISK failed for %s: %s\n",
194 dv->devname, strerror(errno));
195 goto abort;
196 }
197 }
198 /* now to start it */
199 if (s->bitmap_file) {
200 bitmap_fd = open(s->bitmap_file, O_RDWR);
201 if (bitmap_fd < 0) {
202 int major = BITMAP_MAJOR_HI;
203 #if 0
204 if (s->bitmap_chunk == UnSet) {
205 pr_err("%s cannot be openned.", s->bitmap_file);
206 goto abort;
207 }
208 #endif
209 bitmapsize = s->size >> 9; /* FIXME wrong for RAID10 */
210 if (CreateBitmap(s->bitmap_file, 1, NULL,
211 s->bitmap_chunk, c->delay,
212 s->write_behind, bitmapsize, major)) {
213 goto abort;
214 }
215 bitmap_fd = open(s->bitmap_file, O_RDWR);
216 if (bitmap_fd < 0) {
217 pr_err("%s cannot be openned.", s->bitmap_file);
218 goto abort;
219 }
220 }
221 if (bitmap_fd >= 0) {
222 if (ioctl(mdfd, SET_BITMAP_FILE, bitmap_fd) < 0) {
223 pr_err("Cannot set bitmap file for %s: %s\n",
224 mddev, strerror(errno));
225 goto abort;
226 }
227 }
228 }
229 if (ioctl(mdfd, RUN_ARRAY, &param)) {
230 pr_err("RUN_ARRAY failed: %s\n", strerror(errno));
231 if (s->chunk & (s->chunk - 1)) {
232 cont_err("Problem may be that chunk size is not a power of 2\n");
233 }
234 goto abort;
235 }
236
237 if (c->verbose >= 0)
238 pr_err("array %s built and started.\n",
239 mddev);
240 wait_for(mddev, mdfd);
241 close(mdfd);
242 return 0;
243
244 abort:
245 ioctl(mdfd, STOP_ARRAY, 0);
246 close(mdfd);
247 return 1;
248 }