]>
git.ipfire.org Git - thirdparty/mdadm.git/blob - Build.c
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.
45 int subdevs
= 0, missing_disks
= 0;
48 unsigned long long bitmapsize
;
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 */
56 if (s
->level
== UnSet
) {
57 pr_err("a RAID level is needed to Build an array.\n");
60 /* scan all devices, make sure they really are block devices */
61 for (dv
= devlist
; dv
; dv
=dv
->next
) {
63 if (strcmp("missing", dv
->devname
) == 0) {
67 if (stat(dv
->devname
, &stb
)) {
68 pr_err("Cannot find %s: %s\n",
69 dv
->devname
, strerror(errno
));
72 if ((stb
.st_mode
& S_IFMT
) != S_IFBLK
) {
73 pr_err("%s is not a block device.\n",
79 if (s
->raiddisks
!= subdevs
) {
80 pr_err("requested %d devices in array but listed %d\n",
81 s
->raiddisks
, subdevs
);
85 if (s
->layout
== UnSet
)
87 default: /* no layout */
91 s
->layout
= 0x102; /* near=2, far=1 */
93 pr_err("layout defaults to n1\n");
97 s
->layout
= map_name(r5layout
, "default");
99 pr_err("layout defaults to %s\n", map_num(r5layout
, s
->layout
));
102 s
->layout
= map_name(faultylayout
, "default");
105 pr_err("layout defaults to %s\n", map_num(faultylayout
, s
->layout
));
109 /* We need to create the device. It can have no name. */
111 mdfd
= create_mddev(mddev
, NULL
, c
->autof
, LOCAL
,
119 map_update(&map
, fd2devnm(mdfd
), "none", uuid
, chosen_name
);
122 array
.level
= s
->level
;
123 if (s
->size
== MAX_SIZE
)
125 array
.size
= s
->size
;
126 array
.nr_disks
= s
->raiddisks
;
127 array
.raid_disks
= s
->raiddisks
;
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 */
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
))
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
));
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");
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
;
162 if (strcmp("missing", dv
->devname
) == 0)
164 if (stat(dv
->devname
, &stb
)) {
165 pr_err("Weird: %s has disappeared.\n",
169 if ((stb
.st_mode
& S_IFMT
)!= S_IFBLK
) {
170 pr_err("Weird: %s is no longer a block device.\n",
174 fd
= open(dv
->devname
, O_RDONLY
|O_EXCL
);
176 pr_err("Cannot open %s: %s\n",
177 dv
->devname
, strerror(errno
));
180 if (get_dev_size(fd
, NULL
, &dsize
) &&
181 (s
->size
== 0 || s
->size
== MAX_SIZE
|| dsize
< s
->size
))
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
));
197 /* now to start it */
198 if (s
->bitmap_file
) {
199 bitmap_fd
= open(s
->bitmap_file
, O_RDWR
);
201 int major
= BITMAP_MAJOR_HI
;
203 if (s
->bitmap_chunk
== UnSet
) {
204 pr_err("%s cannot be openned.", s
->bitmap_file
);
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
)) {
214 bitmap_fd
= open(s
->bitmap_file
, O_RDWR
);
216 pr_err("%s cannot be openned.", s
->bitmap_file
);
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
));
228 if (ioctl(mdfd
, RUN_ARRAY
, ¶m
)) {
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");
237 pr_err("array %s built and started.\n",
239 wait_for(mddev
, mdfd
);
244 ioctl(mdfd
, STOP_ARRAY
, 0);