]> git.ipfire.org Git - thirdparty/mdadm.git/blame - Build.c
Create.c: fix uclibc build
[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
330c07f8
MT
27int Build(struct mddev_ident *ident, struct mddev_dev *devlist, struct shape *s,
28 struct context *c)
64c4757e 29{
82b27616
NB
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.
82b27616
NB
38 */
39 int i;
0a6bff09 40 dev_t rdev;
98ce3849 41 int subdevs = 0, missing_disks = 0;
a655e550 42 struct mddev_dev *dv;
c82f047c 43 int bitmap_fd;
f9c25f1d 44 unsigned long long bitmapsize;
7f91af49 45 int mdfd;
a04d5763
N
46 char chosen_name[1024];
47 int uuid[4] = {0,0,0,0};
ad5bc697 48 struct map_ent *map = NULL;
e6e5f8f1
JS
49 mdu_array_info_t array;
50 mdu_param_t param; /* not used by syscall */
82b27616 51
1b4944f3
ZL
52 if (s->level == UnSet) {
53 pr_err("a RAID level is needed to Build an array.\n");
54 return 1;
55 }
82b27616 56 /* scan all devices, make sure they really are block devices */
cd29a5c8 57 for (dv = devlist; dv; dv=dv->next) {
98ce3849
NB
58 subdevs++;
59 if (strcmp("missing", dv->devname) == 0) {
60 missing_disks++;
61 continue;
62 }
9e04ac1c 63 if (!stat_is_blkdev(dv->devname, NULL))
82b27616 64 return 1;
cd29a5c8
NB
65 }
66
a4e8316a 67 if (s->raiddisks != subdevs) {
e7b84f9d 68 pr_err("requested %d devices in array but listed %d\n",
a4e8316a 69 s->raiddisks, subdevs);
cd29a5c8 70 return 1;
82b27616
NB
71 }
72
a4e8316a 73 if (s->layout == UnSet)
913f07d1 74 s->layout = default_layout(NULL, s->level, c->verbose);
b5e64645 75
69207ff6 76 /* We need to create the device. It can have no name. */
ad5bc697 77 map_lock(&map);
330c07f8 78 mdfd = create_mddev(ident->devname, NULL, c->autof, LOCAL,
cd6cbb08 79 chosen_name, 0);
ad5bc697
N
80 if (mdfd < 0) {
81 map_unlock(&map);
7f91af49 82 return 1;
ad5bc697 83 }
b5e64645 84
b823c8f9 85 map_update(&map, fd2devnm(mdfd), STR_COMMON_NONE, uuid, chosen_name);
ad5bc697 86 map_unlock(&map);
a04d5763 87
e6e5f8f1
JS
88 array.level = s->level;
89 if (s->size == MAX_SIZE)
90 s->size = 0;
91 array.size = s->size;
92 array.nr_disks = s->raiddisks;
93 array.raid_disks = s->raiddisks;
94 array.md_minor = 0;
330c07f8 95 if (fstat_is_blkdev(mdfd, chosen_name, &rdev))
0a6bff09 96 array.md_minor = minor(rdev);
e6e5f8f1
JS
97 array.not_persistent = 1;
98 array.state = 0; /* not clean, but no errors */
99 if (s->assume_clean)
100 array.state |= 1;
101 array.active_disks = s->raiddisks - missing_disks;
102 array.working_disks = s->raiddisks - missing_disks;
103 array.spare_disks = 0;
104 array.failed_disks = missing_disks;
105 if (s->chunk == 0 && (s->level==0 || s->level==LEVEL_LINEAR))
106 s->chunk = 64;
107 array.chunk_size = s->chunk*1024;
108 array.layout = s->layout;
109 if (md_set_array_info(mdfd, &array)) {
330c07f8 110 pr_err("md_set_array_info() failed for %s: %s\n", chosen_name, strerror(errno));
7f91af49 111 goto abort;
82b27616 112 }
5b28bd56 113
b823c8f9 114 if (s->bitmap_file && str_is_none(s->bitmap_file) == true)
748952f7 115 s->bitmap_file = NULL;
a4e8316a 116 if (s->bitmap_file && s->level <= 0) {
e7b84f9d 117 pr_err("bitmaps not meaningful with level %s\n",
a4e8316a 118 map_num(pers, s->level)?:"given");
7f91af49 119 goto abort;
5b28bd56 120 }
82b27616 121 /* now add the devices */
cd29a5c8 122 for ((i=0), (dv = devlist) ; dv ; i++, dv=dv->next) {
e6e5f8f1 123 mdu_disk_info_t disk;
beae1dfe 124 unsigned long long dsize;
fe80f49b 125 int fd;
e6e5f8f1 126
98ce3849
NB
127 if (strcmp("missing", dv->devname) == 0)
128 continue;
9e04ac1c 129 if (!stat_is_blkdev(dv->devname, &rdev))
82b27616 130 goto abort;
fe80f49b
NB
131 fd = open(dv->devname, O_RDONLY|O_EXCL);
132 if (fd < 0) {
e7b84f9d 133 pr_err("Cannot open %s: %s\n",
fe80f49b
NB
134 dv->devname, strerror(errno));
135 goto abort;
136 }
beae1dfe 137 if (get_dev_size(fd, NULL, &dsize) &&
a4e8316a
N
138 (s->size == 0 || s->size == MAX_SIZE || dsize < s->size))
139 s->size = dsize;
fe80f49b 140 close(fd);
e6e5f8f1
JS
141 disk.number = i;
142 disk.raid_disk = i;
143 disk.state = (1<<MD_DISK_SYNC) | (1<<MD_DISK_ACTIVE);
144 if (dv->writemostly == FlagSet)
145 disk.state |= 1<<MD_DISK_WRITEMOSTLY;
9e04ac1c
ZL
146 disk.major = major(rdev);
147 disk.minor = minor(rdev);
e6e5f8f1
JS
148 if (ioctl(mdfd, ADD_NEW_DISK, &disk)) {
149 pr_err("ADD_NEW_DISK failed for %s: %s\n",
150 dv->devname, strerror(errno));
151 goto abort;
82b27616
NB
152 }
153 }
154 /* now to start it */
e6e5f8f1
JS
155 if (s->bitmap_file) {
156 bitmap_fd = open(s->bitmap_file, O_RDWR);
157 if (bitmap_fd < 0) {
158 int major = BITMAP_MAJOR_HI;
e6e5f8f1
JS
159 bitmapsize = s->size >> 9; /* FIXME wrong for RAID10 */
160 if (CreateBitmap(s->bitmap_file, 1, NULL,
161 s->bitmap_chunk, c->delay,
162 s->write_behind, bitmapsize, major)) {
163 goto abort;
aba69144 164 }
e6e5f8f1
JS
165 bitmap_fd = open(s->bitmap_file, O_RDWR);
166 if (bitmap_fd < 0) {
8e5b52cd 167 pr_err("%s cannot be opened.\n", s->bitmap_file);
e6e5f8f1 168 goto abort;
c82f047c
NB
169 }
170 }
e6e5f8f1
JS
171 if (bitmap_fd >= 0) {
172 if (ioctl(mdfd, SET_BITMAP_FILE, bitmap_fd) < 0) {
330c07f8
MT
173 pr_err("Cannot set bitmap file for %s: %s\n", chosen_name,
174 strerror(errno));
e6e5f8f1 175 goto abort;
a252c078 176 }
82b27616 177 }
e6e5f8f1
JS
178 }
179 if (ioctl(mdfd, RUN_ARRAY, &param)) {
180 pr_err("RUN_ARRAY failed: %s\n", strerror(errno));
181 if (s->chunk & (s->chunk - 1)) {
182 cont_err("Problem may be that chunk size is not a power of 2\n");
82b27616 183 }
e6e5f8f1 184 goto abort;
82b27616 185 }
e6e5f8f1 186
0c9e4afb 187 if (c->verbose >= 0)
330c07f8
MT
188 pr_err("array %s built and started.\n", chosen_name);
189 wait_for(chosen_name, mdfd);
7f91af49 190 close(mdfd);
82b27616
NB
191 return 0;
192
193 abort:
e6e5f8f1 194 ioctl(mdfd, STOP_ARRAY, 0);
7f91af49 195 close(mdfd);
82b27616 196 return 1;
64c4757e 197}