]>
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 int Build(char *mddev
, struct mddev_dev
*devlist
,
28 struct shape
*s
, struct context
*c
)
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.
41 int subdevs
= 0, missing_disks
= 0;
44 unsigned long long bitmapsize
;
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 */
52 if (s
->level
== UnSet
) {
53 pr_err("a RAID level is needed to Build an array.\n");
56 /* scan all devices, make sure they really are block devices */
57 for (dv
= devlist
; dv
; dv
=dv
->next
) {
59 if (strcmp("missing", dv
->devname
) == 0) {
63 if (!stat_is_blkdev(dv
->devname
, NULL
))
67 if (s
->raiddisks
!= subdevs
) {
68 pr_err("requested %d devices in array but listed %d\n",
69 s
->raiddisks
, subdevs
);
73 if (s
->layout
== UnSet
)
74 s
->layout
= default_layout(NULL
, s
->level
, c
->verbose
);
76 /* We need to create the device. It can have no name. */
78 mdfd
= create_mddev(mddev
, NULL
, c
->autof
, LOCAL
,
86 map_update(&map
, fd2devnm(mdfd
), "none", uuid
, chosen_name
);
89 array
.level
= s
->level
;
90 if (s
->size
== MAX_SIZE
)
93 array
.nr_disks
= s
->raiddisks
;
94 array
.raid_disks
= s
->raiddisks
;
96 if (fstat_is_blkdev(mdfd
, mddev
, &rdev
))
97 array
.md_minor
= minor(rdev
);
98 array
.not_persistent
= 1;
99 array
.state
= 0; /* not clean, but no errors */
102 array
.active_disks
= s
->raiddisks
- missing_disks
;
103 array
.working_disks
= s
->raiddisks
- missing_disks
;
104 array
.spare_disks
= 0;
105 array
.failed_disks
= missing_disks
;
106 if (s
->chunk
== 0 && (s
->level
==0 || s
->level
==LEVEL_LINEAR
))
108 array
.chunk_size
= s
->chunk
*1024;
109 array
.layout
= s
->layout
;
110 if (md_set_array_info(mdfd
, &array
)) {
111 pr_err("md_set_array_info() failed for %s: %s\n",
112 mddev
, strerror(errno
));
116 if (s
->bitmap_file
&& strcmp(s
->bitmap_file
, "none") == 0)
117 s
->bitmap_file
= NULL
;
118 if (s
->bitmap_file
&& s
->level
<= 0) {
119 pr_err("bitmaps not meaningful with level %s\n",
120 map_num(pers
, s
->level
)?:"given");
123 /* now add the devices */
124 for ((i
=0), (dv
= devlist
) ; dv
; i
++, dv
=dv
->next
) {
125 mdu_disk_info_t disk
;
126 unsigned long long dsize
;
129 if (strcmp("missing", dv
->devname
) == 0)
131 if (!stat_is_blkdev(dv
->devname
, &rdev
))
133 fd
= open(dv
->devname
, O_RDONLY
|O_EXCL
);
135 pr_err("Cannot open %s: %s\n",
136 dv
->devname
, strerror(errno
));
139 if (get_dev_size(fd
, NULL
, &dsize
) &&
140 (s
->size
== 0 || s
->size
== MAX_SIZE
|| dsize
< s
->size
))
145 disk
.state
= (1<<MD_DISK_SYNC
) | (1<<MD_DISK_ACTIVE
);
146 if (dv
->writemostly
== FlagSet
)
147 disk
.state
|= 1<<MD_DISK_WRITEMOSTLY
;
148 disk
.major
= major(rdev
);
149 disk
.minor
= minor(rdev
);
150 if (ioctl(mdfd
, ADD_NEW_DISK
, &disk
)) {
151 pr_err("ADD_NEW_DISK failed for %s: %s\n",
152 dv
->devname
, strerror(errno
));
156 /* now to start it */
157 if (s
->bitmap_file
) {
158 bitmap_fd
= open(s
->bitmap_file
, O_RDWR
);
160 int major
= BITMAP_MAJOR_HI
;
162 if (s
->bitmap_chunk
== UnSet
) {
163 pr_err("%s cannot be opened.\n", s
->bitmap_file
);
167 bitmapsize
= s
->size
>> 9; /* FIXME wrong for RAID10 */
168 if (CreateBitmap(s
->bitmap_file
, 1, NULL
,
169 s
->bitmap_chunk
, c
->delay
,
170 s
->write_behind
, bitmapsize
, major
)) {
173 bitmap_fd
= open(s
->bitmap_file
, O_RDWR
);
175 pr_err("%s cannot be opened.\n", s
->bitmap_file
);
179 if (bitmap_fd
>= 0) {
180 if (ioctl(mdfd
, SET_BITMAP_FILE
, bitmap_fd
) < 0) {
181 pr_err("Cannot set bitmap file for %s: %s\n",
182 mddev
, strerror(errno
));
187 if (ioctl(mdfd
, RUN_ARRAY
, ¶m
)) {
188 pr_err("RUN_ARRAY failed: %s\n", strerror(errno
));
189 if (s
->chunk
& (s
->chunk
- 1)) {
190 cont_err("Problem may be that chunk size is not a power of 2\n");
196 pr_err("array %s built and started.\n",
198 wait_for(mddev
, mdfd
);
203 ioctl(mdfd
, STOP_ARRAY
, 0);