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