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