]>
git.ipfire.org Git - thirdparty/mdadm.git/blob - Build.c
2 * mdadm - manage Linux "md" devices aka RAID arrays.
4 * Copyright (C) 2001-2002 Neil Brown <neilb@cse.unsw.edu.au>
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@cse.unsw.edu.au>
24 * School of Computer Science and Engineering
25 * The University of New South Wales
32 #define REGISTER_DEV _IO (MD_MAJOR, 1)
33 #define START_MD _IO (MD_MAJOR, 2)
34 #define STOP_MD _IO (MD_MAJOR, 3)
36 int Build(char *mddev
, int mdfd
, int chunk
, int level
, int layout
,
38 mddev_dev_t devlist
, int assume_clean
,
39 char *bitmap_file
, int bitmap_chunk
, int delay
)
41 /* Build a linear or raid0 arrays without superblocks
42 * We cannot really do any checks, we just do it.
43 * For md_version < 0.90.0, we call REGISTER_DEV
44 * with the device numbers, and then
45 * START_MD giving the "geometry"
46 * geometry is 0xpp00cc
47 * where pp is personality: 1==linear, 2=raid0
48 * cc = chunk size factor: 0==4k, 1==8k etc.
50 * For md_version >= 0.90.0 we call
51 * SET_ARRAY_INFO, ADD_NEW_DISK, RUN_ARRAY
62 /* scan all devices, make sure they really are block devices */
63 for (dv
= devlist
; dv
; dv
=dv
->next
) {
64 if (stat(dv
->devname
, &stb
)) {
65 fprintf(stderr
, Name
": Cannot find %s: %s\n",
66 dv
->devname
, strerror(errno
));
69 if ((stb
.st_mode
& S_IFMT
) != S_IFBLK
) {
70 fprintf(stderr
, Name
": %s is not a block device.\n",
77 if (raiddisks
!= subdevs
) {
78 fprintf(stderr
, Name
": requested %d devices in array but listed %d\n",
85 default: /* no layout */
89 layout
= 0x102; /* near=2, far=1 */
92 Name
": layout defaults to n1\n");
96 layout
= map_name(r5layout
, "default");
99 Name
": layout defaults to %s\n", map_num(r5layout
, layout
));
102 layout
= map_name(faultylayout
, "default");
106 Name
": layout defaults to %s\n", map_num(faultylayout
, layout
));
111 vers
= md_get_version(mdfd
);
113 /* looks Ok, go for it */
115 mdu_array_info_t array
;
118 array
.nr_disks
= raiddisks
;
119 array
.raid_disks
= raiddisks
;
121 if (fstat(mdfd
, &stb
)==0)
122 array
.md_minor
= minor(stb
.st_rdev
);
123 array
.not_persistent
= 1;
124 array
.state
= 0; /* not clean, but no errors */
127 array
.active_disks
= raiddisks
;
128 array
.working_disks
= raiddisks
;
129 array
.spare_disks
= 0;
130 array
.failed_disks
= 0;
133 array
.chunk_size
= chunk
*1024;
134 array
.layout
= layout
;
135 if (ioctl(mdfd
, SET_ARRAY_INFO
, &array
)) {
136 fprintf(stderr
, Name
": SET_ARRAY_INFO failed for %s: %s\n",
137 mddev
, strerror(errno
));
140 } else if (bitmap_file
) {
141 fprintf(stderr
, Name
": bitmaps not supported with this kernel\n");
144 /* now add the devices */
145 for ((i
=0), (dv
= devlist
) ; dv
; i
++, dv
=dv
->next
) {
146 if (stat(dv
->devname
, &stb
)) {
147 fprintf(stderr
, Name
": Wierd: %s has disappeared.\n",
151 if ((stb
.st_mode
& S_IFMT
)!= S_IFBLK
) {
152 fprintf(stderr
, Name
": Wierd: %s is no longer a block device.\n",
157 mdu_disk_info_t disk
;
161 disk
.major
= major(stb
.st_rdev
);
162 disk
.minor
= minor(stb
.st_rdev
);
163 if (ioctl(mdfd
, ADD_NEW_DISK
, &disk
)) {
164 fprintf(stderr
, Name
": ADD_NEW_DISK failed for %s: %s\n",
165 dv
->devname
, strerror(errno
));
169 if (ioctl(mdfd
, REGISTER_DEV
, &stb
.st_rdev
)) {
170 fprintf(stderr
, Name
": REGISTER_DEV failed for %s: %s.\n",
171 dv
->devname
, strerror(errno
));
176 /* now to start it */
178 mdu_param_t param
; /* not used by syscall */
180 bitmap_fd
= open(bitmap_file
, O_RDWR
);
182 if (bitmap_chunk
== UnSet
) {
183 fprintf(stderr
, Name
": %s cannot be openned.",
187 if (CreateBitmap(bitmap_file
, 1, NULL
, bitmap_chunk
,
188 delay
, 0/* FIXME size */)) {
191 bitmap_fd
= open(bitmap_file
, O_RDWR
);
193 fprintf(stderr
, Name
": %s cannot be openned.",
198 if (bitmap_fd
>= 0) {
199 if (ioctl(mdfd
, SET_BITMAP_FILE
, bitmap_fd
) < 0) {
200 fprintf(stderr
, Name
": Cannot set bitmap file for %s: %s\n",
201 mddev
, strerror(errno
));
206 if (ioctl(mdfd
, RUN_ARRAY
, ¶m
)) {
207 fprintf(stderr
, Name
": RUN_ARRAY failed: %s\n",
214 while (chunk
> 4096) {
220 else chunk
|= 0x10000;
221 if (ioctl(mdfd
, START_MD
, arg
)) {
222 fprintf(stderr
, Name
": START_MD failed: %s\n",
227 fprintf(stderr
, Name
": array %s built and started.\n",
233 ioctl(mdfd
, STOP_ARRAY
, 0);
235 ioctl(mdfd
, STOP_MD
, 0);