]> git.ipfire.org Git - thirdparty/mdadm.git/blame - Build.c
Monitor/check_array: Centralize exit path
[thirdparty/mdadm.git] / Build.c
CommitLineData
64c4757e 1/*
9a9dab36 2 * mdadm - manage Linux "md" devices aka RAID arrays.
64c4757e 3 *
e736b623 4 * Copyright (C) 2001-2009 Neil Brown <neilb@suse.de>
64c4757e
NB
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
e736b623 22 * Email: <neilb@suse.de>
64c4757e
NB
23 */
24
9a9dab36 25#include "mdadm.h"
64c4757e 26
1011e834
N
27#define REGISTER_DEV _IO (MD_MAJOR, 1)
28#define START_MD _IO (MD_MAJOR, 2)
29#define STOP_MD _IO (MD_MAJOR, 3)
82b27616 30
a4e8316a
N
31int Build(char *mddev, struct mddev_dev *devlist,
32 struct shape *s, struct context *c)
64c4757e 33{
82b27616
NB
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.
82b27616
NB
42 */
43 int i;
0a6bff09 44 dev_t rdev;
98ce3849 45 int subdevs = 0, missing_disks = 0;
a655e550 46 struct mddev_dev *dv;
c82f047c 47 int bitmap_fd;
f9c25f1d 48 unsigned long long bitmapsize;
7f91af49 49 int mdfd;
a04d5763
N
50 char chosen_name[1024];
51 int uuid[4] = {0,0,0,0};
ad5bc697 52 struct map_ent *map = NULL;
e6e5f8f1
JS
53 mdu_array_info_t array;
54 mdu_param_t param; /* not used by syscall */
82b27616 55
1b4944f3
ZL
56 if (s->level == UnSet) {
57 pr_err("a RAID level is needed to Build an array.\n");
58 return 1;
59 }
82b27616 60 /* scan all devices, make sure they really are block devices */
cd29a5c8 61 for (dv = devlist; dv; dv=dv->next) {
98ce3849
NB
62 subdevs++;
63 if (strcmp("missing", dv->devname) == 0) {
64 missing_disks++;
65 continue;
66 }
9e04ac1c 67 if (!stat_is_blkdev(dv->devname, NULL))
82b27616 68 return 1;
cd29a5c8
NB
69 }
70
a4e8316a 71 if (s->raiddisks != subdevs) {
e7b84f9d 72 pr_err("requested %d devices in array but listed %d\n",
a4e8316a 73 s->raiddisks, subdevs);
cd29a5c8 74 return 1;
82b27616
NB
75 }
76
a4e8316a
N
77 if (s->layout == UnSet)
78 switch(s->level) {
b5e64645 79 default: /* no layout */
a4e8316a 80 s->layout = 0;
b5e64645
NB
81 break;
82 case 10:
a4e8316a 83 s->layout = 0x102; /* near=2, far=1 */
0c9e4afb 84 if (c->verbose > 0)
e7b84f9d 85 pr_err("layout defaults to n1\n");
b5e64645
NB
86 break;
87 case 5:
88 case 6:
a4e8316a 89 s->layout = map_name(r5layout, "default");
0c9e4afb 90 if (c->verbose > 0)
a4e8316a 91 pr_err("layout defaults to %s\n", map_num(r5layout, s->layout));
b5e64645
NB
92 break;
93 case LEVEL_FAULTY:
a4e8316a 94 s->layout = map_name(faultylayout, "default");
b5e64645 95
0c9e4afb 96 if (c->verbose > 0)
a4e8316a 97 pr_err("layout defaults to %s\n", map_num(faultylayout, s->layout));
b5e64645
NB
98 break;
99 }
100
69207ff6 101 /* We need to create the device. It can have no name. */
ad5bc697 102 map_lock(&map);
0c9e4afb 103 mdfd = create_mddev(mddev, NULL, c->autof, LOCAL,
cd6cbb08 104 chosen_name, 0);
ad5bc697
N
105 if (mdfd < 0) {
106 map_unlock(&map);
7f91af49 107 return 1;
ad5bc697 108 }
a714580e 109 mddev = chosen_name;
b5e64645 110
4dd2df09 111 map_update(&map, fd2devnm(mdfd), "none", uuid, chosen_name);
ad5bc697 112 map_unlock(&map);
a04d5763 113
e6e5f8f1
JS
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;
0a6bff09
ZL
121 if (fstat_is_blkdev(mdfd, mddev, &rdev))
122 array.md_minor = minor(rdev);
e6e5f8f1
JS
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));
7f91af49 138 goto abort;
82b27616 139 }
5b28bd56 140
748952f7
N
141 if (s->bitmap_file && strcmp(s->bitmap_file, "none") == 0)
142 s->bitmap_file = NULL;
a4e8316a 143 if (s->bitmap_file && s->level <= 0) {
e7b84f9d 144 pr_err("bitmaps not meaningful with level %s\n",
a4e8316a 145 map_num(pers, s->level)?:"given");
7f91af49 146 goto abort;
5b28bd56 147 }
82b27616 148 /* now add the devices */
cd29a5c8 149 for ((i=0), (dv = devlist) ; dv ; i++, dv=dv->next) {
e6e5f8f1 150 mdu_disk_info_t disk;
beae1dfe 151 unsigned long long dsize;
fe80f49b 152 int fd;
e6e5f8f1 153
98ce3849
NB
154 if (strcmp("missing", dv->devname) == 0)
155 continue;
9e04ac1c 156 if (!stat_is_blkdev(dv->devname, &rdev))
82b27616 157 goto abort;
fe80f49b
NB
158 fd = open(dv->devname, O_RDONLY|O_EXCL);
159 if (fd < 0) {
e7b84f9d 160 pr_err("Cannot open %s: %s\n",
fe80f49b
NB
161 dv->devname, strerror(errno));
162 goto abort;
163 }
beae1dfe 164 if (get_dev_size(fd, NULL, &dsize) &&
a4e8316a
N
165 (s->size == 0 || s->size == MAX_SIZE || dsize < s->size))
166 s->size = dsize;
fe80f49b 167 close(fd);
e6e5f8f1
JS
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;
9e04ac1c
ZL
173 disk.major = major(rdev);
174 disk.minor = minor(rdev);
e6e5f8f1
JS
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;
82b27616
NB
179 }
180 }
181 /* now to start it */
e6e5f8f1
JS
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;
1bfdbe01 186#if 0
e6e5f8f1
JS
187 if (s->bitmap_chunk == UnSet) {
188 pr_err("%s cannot be openned.", s->bitmap_file);
189 goto abort;
190 }
dcec9ee5 191#endif
e6e5f8f1
JS
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;
aba69144 197 }
e6e5f8f1
JS
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;
c82f047c
NB
202 }
203 }
e6e5f8f1
JS
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;
a252c078 209 }
82b27616 210 }
e6e5f8f1
JS
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");
82b27616 216 }
e6e5f8f1 217 goto abort;
82b27616 218 }
e6e5f8f1 219
0c9e4afb 220 if (c->verbose >= 0)
e7b84f9d 221 pr_err("array %s built and started.\n",
dab6685f 222 mddev);
a7c6e3fb 223 wait_for(mddev, mdfd);
7f91af49 224 close(mdfd);
82b27616
NB
225 return 0;
226
227 abort:
e6e5f8f1 228 ioctl(mdfd, STOP_ARRAY, 0);
7f91af49 229 close(mdfd);
82b27616 230 return 1;
64c4757e 231}