2 * mdadm - manage Linux "md" devices aka RAID arrays.
4 * Copyright (C) 2001-2009 Neil Brown <neilb@suse.de>
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.
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.
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
22 * Email: <neilb@suse.de>
27 #define REGISTER_DEV _IO (MD_MAJOR, 1)
28 #define START_MD _IO (MD_MAJOR, 2)
29 #define STOP_MD _IO (MD_MAJOR, 3)
31 int Build(char *mddev, struct mddev_dev *devlist,
32 struct shape *s, struct context *c)
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.
43 * For md_version >= 0.90.0 we call
44 * SET_ARRAY_INFO, ADD_NEW_DISK, RUN_ARRAY
50 int subdevs = 0, missing_disks = 0;
53 unsigned long long bitmapsize;
55 char chosen_name[1024];
56 int uuid[4] = {0,0,0,0};
57 struct map_ent *map = NULL;
59 /* scan all devices, make sure they really are block devices */
60 for (dv = devlist; dv; dv=dv->next) {
62 if (strcmp("missing", dv->devname) == 0) {
66 if (stat(dv->devname, &stb)) {
67 pr_err("Cannot find %s: %s\n",
68 dv->devname, strerror(errno));
71 if ((stb.st_mode & S_IFMT) != S_IFBLK) {
72 pr_err("%s is not a block device.\n",
78 if (s->raiddisks != subdevs) {
79 pr_err("requested %d devices in array but listed %d\n",
80 s->raiddisks, subdevs);
84 if (s->layout == UnSet)
86 default: /* no layout */
90 s->layout = 0x102; /* near=2, far=1 */
92 pr_err("layout defaults to n1\n");
96 s->layout = map_name(r5layout, "default");
98 pr_err("layout defaults to %s\n", map_num(r5layout, s->layout));
101 s->layout = map_name(faultylayout, "default");
104 pr_err("layout defaults to %s\n", map_num(faultylayout, s->layout));
108 /* We need to create the device. It can have no name. */
110 mdfd = create_mddev(mddev, NULL, c->autof, LOCAL,
118 map_update(&map, fd2devnm(mdfd), "none", uuid, chosen_name);
121 vers = md_get_version(mdfd);
123 /* looks Ok, go for it */
125 mdu_array_info_t array;
126 array.level = s->level;
127 if (s->size == MAX_SIZE)
129 array.size = s->size;
130 array.nr_disks = s->raiddisks;
131 array.raid_disks = s->raiddisks;
133 if (fstat(mdfd, &stb)==0)
134 array.md_minor = minor(stb.st_rdev);
135 array.not_persistent = 1;
136 array.state = 0; /* not clean, but no errors */
139 array.active_disks = s->raiddisks - missing_disks;
140 array.working_disks = s->raiddisks - missing_disks;
141 array.spare_disks = 0;
142 array.failed_disks = missing_disks;
143 if (s->chunk == 0 && (s->level==0 || s->level==LEVEL_LINEAR))
145 array.chunk_size = s->chunk*1024;
146 array.layout = s->layout;
147 if (ioctl(mdfd, SET_ARRAY_INFO, &array)) {
148 pr_err("SET_ARRAY_INFO failed for %s: %s\n",
149 mddev, strerror(errno));
152 } else if (s->bitmap_file) {
153 pr_err("bitmaps not supported with this kernel\n");
157 if (s->bitmap_file && strcmp(s->bitmap_file, "none") == 0)
158 s->bitmap_file = NULL;
159 if (s->bitmap_file && s->level <= 0) {
160 pr_err("bitmaps not meaningful with level %s\n",
161 map_num(pers, s->level)?:"given");
164 /* now add the devices */
165 for ((i=0), (dv = devlist) ; dv ; i++, dv=dv->next) {
166 unsigned long long dsize;
168 if (strcmp("missing", dv->devname) == 0)
170 if (stat(dv->devname, &stb)) {
171 pr_err("Weird: %s has disappeared.\n",
175 if ((stb.st_mode & S_IFMT)!= S_IFBLK) {
176 pr_err("Weird: %s is no longer a block device.\n",
180 fd = open(dv->devname, O_RDONLY|O_EXCL);
182 pr_err("Cannot open %s: %s\n",
183 dv->devname, strerror(errno));
186 if (get_dev_size(fd, NULL, &dsize) &&
187 (s->size == 0 || s->size == MAX_SIZE || dsize < s->size))
191 mdu_disk_info_t disk;
194 disk.state = (1<<MD_DISK_SYNC) | (1<<MD_DISK_ACTIVE);
195 if (dv->writemostly == FlagSet)
196 disk.state |= 1<<MD_DISK_WRITEMOSTLY;
197 disk.major = major(stb.st_rdev);
198 disk.minor = minor(stb.st_rdev);
199 if (ioctl(mdfd, ADD_NEW_DISK, &disk)) {
200 pr_err("ADD_NEW_DISK failed for %s: %s\n",
201 dv->devname, strerror(errno));
205 if (ioctl(mdfd, REGISTER_DEV, &stb.st_rdev)) {
206 pr_err("REGISTER_DEV failed for %s: %s.\n",
207 dv->devname, strerror(errno));
212 /* now to start it */
214 mdu_param_t param; /* not used by syscall */
215 if (s->bitmap_file) {
216 bitmap_fd = open(s->bitmap_file, O_RDWR);
218 int major = BITMAP_MAJOR_HI;
220 if (s->bitmap_chunk == UnSet) {
221 pr_err("%s cannot be openned.",
227 major = BITMAP_MAJOR_HOSTENDIAN;
229 pr_err("Warning - bitmaps created on this kernel are not portable\n"
230 " between different architectures. Consider upgrading the Linux kernel.\n");
233 bitmapsize = s->size>>9; /* FIXME wrong for RAID10 */
234 if (CreateBitmap(s->bitmap_file, 1, NULL, s->bitmap_chunk,
235 c->delay, s->write_behind, bitmapsize, major)) {
238 bitmap_fd = open(s->bitmap_file, O_RDWR);
240 pr_err("%s cannot be openned.",
245 if (bitmap_fd >= 0) {
246 if (ioctl(mdfd, SET_BITMAP_FILE, bitmap_fd) < 0) {
247 pr_err("Cannot set bitmap file for %s: %s\n",
248 mddev, strerror(errno));
253 if (ioctl(mdfd, RUN_ARRAY, ¶m)) {
254 pr_err("RUN_ARRAY failed: %s\n",
256 if (s->chunk & (s->chunk-1)) {
257 cont_err("Problem may be that chunk size is not a power of 2\n");
264 while (s->chunk > 4096) {
272 if (ioctl(mdfd, START_MD, arg)) {
273 pr_err("START_MD failed: %s\n",
279 pr_err("array %s built and started.\n",
281 wait_for(mddev, mdfd);
287 ioctl(mdfd, STOP_ARRAY, 0);
289 ioctl(mdfd, STOP_MD, 0);