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