]> git.ipfire.org Git - thirdparty/mdadm.git/blob - Build.c
mdctl-v0.4.2
[thirdparty/mdadm.git] / Build.c
1 /*
2 * mdctl - manage Linux "md" devices aka RAID arrays.
3 *
4 * Copyright (C) 2001 Neil Brown <neilb@cse.unsw.edu.au>
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
22 * Email: <neilb@cse.unsw.edu.au>
23 * Paper: Neil Brown
24 * School of Computer Science and Engineering
25 * The University of New South Wales
26 * Sydney, 2052
27 * Australia
28 */
29
30 #include "mdctl.h"
31
32 #define REGISTER_DEV _IO (MD_MAJOR, 1)
33 #define START_MD _IO (MD_MAJOR, 2)
34 #define STOP_MD _IO (MD_MAJOR, 3)
35
36 int Build(char *mddev, int mdfd, int chunk, int level,
37 int raiddisks,
38 int subdevs, char *subdev[])
39 {
40 /* Build a linear or raid0 arrays without superblocks
41 * We cannot really do any checks, we just do it.
42 * For md_version < 0.90.0, we call REGISTER_DEV
43 * with the device numbers, and then
44 * START_MD giving the "geometry"
45 * geometry is 0xpp00cc
46 * where pp is personality: 1==linear, 2=raid0
47 * cc = chunk size factor: 0==4k, 1==8k etc.
48 *
49 * For md_version >= 0.90.0 we call
50 * SET_ARRAY_INFO, ADD_NEW_DISK, RUN_ARRAY
51 *
52 */
53 int i;
54 int vers;
55 struct stat stb;
56 if (raiddisks != subdevs) {
57 fprintf(stderr, Name ": requested %d devices in array but listed %d\n",
58 raiddisks, subdevs);
59 return 1;
60 }
61
62 /* scan all devices, make sure they really are block devices */
63 for (i=0; i<subdevs; i++) {
64 if (stat(subdev[i], &stb)) {
65 fprintf(stderr, Name ": Cannot find %s: %s\n",
66 subdev[i], strerror(errno));
67 return 1;
68 }
69 if ((stb.st_mode & S_IFMT) != S_IFBLK) {
70 fprintf(stderr, Name ": %s is not a block device.\n",
71 subdev[i]);
72 return 1;
73 }
74 }
75
76 vers = md_get_version(mdfd);
77
78 /* looks Ok, go for it */
79 if (vers >= 90000) {
80 mdu_array_info_t array;
81 array.level = level;
82 array.size = 0;
83 array.nr_disks = raiddisks;
84 array.raid_disks = raiddisks;
85 array.md_minor = 0;
86 if (fstat(mdfd, &stb)==0)
87 array.md_minor = MINOR(stb.st_rdev);
88 array.not_persistent = 0;
89 array.state = 0; /* not clean, but no errors */
90 array.active_disks = raiddisks;
91 array.working_disks = raiddisks;
92 array.spare_disks = 0;
93 array.failed_disks = 0;
94 array.chunk_size = chunk*1024;
95 if (ioctl(mdfd, SET_ARRAY_INFO, &array)) {
96 fprintf(stderr, Name ": SET_ARRAY_INFO failed for %s: %s\n",
97 mddev, strerror(errno));
98 return 1;
99 }
100 }
101 /* now add the devices */
102 for (i=0; i<subdevs; i++) {
103 if (stat(subdev[i], &stb)) {
104 fprintf(stderr, Name ": Wierd: %s has disappeared.\n",
105 subdev[i]);
106 goto abort;
107 }
108 if ((stb.st_rdev & S_IFMT)!= S_IFBLK) {
109 fprintf(stderr, Name ": Wierd: %s is no longer a block device.\n",
110 subdev[i]);
111 goto abort;
112 }
113 if (vers> 90000) {
114 mdu_disk_info_t disk;
115 disk.number = i;
116 disk.raid_disk = i;
117 disk.state = 6;
118 disk.major = MAJOR(stb.st_rdev);
119 disk.minor = MINOR(stb.st_rdev);
120 if (ioctl(mdfd, ADD_NEW_DISK, &disk)) {
121 fprintf(stderr, Name ": ADD_NEW_DISK failed for %s: %s\n",
122 subdev[i], strerror(errno));
123 goto abort;
124 }
125 } else {
126 if (ioctl(mdfd, REGISTER_DEV, &stb.st_rdev)) {
127 fprintf(stderr, Name ": REGISTER_DEV failed for %s.\n",
128 subdev[i], strerror(errno));
129 goto abort;
130 }
131 }
132 }
133 /* now to start it */
134 if (vers > 90000) {
135 mdu_param_t param; /* not used by syscall */
136 if (ioctl(mdfd, RUN_ARRAY, param)) {
137 fprintf(stderr, Name ": RUN_ARRAY failed: %s\n",
138 strerror(errno));
139 goto abort;
140 }
141 } else {
142 int arg;
143 arg=0;
144 while (chunk > 4096) {
145 arg++;
146 chunk >>= 1;
147 }
148 if (level == 0)
149 chunk |= 0x20000;
150 else chunk |= 0x10000;
151 if (ioctl(mdfd, START_MD, arg)) {
152 fprintf(stderr, Name ": START_MD failed: %s\n",
153 strerror(errno));
154 goto abort;
155 }
156 }
157 fprintf(stderr, Name ": array %s built and started.\n",
158 mddev);
159 return 0;
160
161 abort:
162 if (vers > 900000)
163 ioctl(mdfd, STOP_ARRAY, 0);
164 else
165 ioctl(mdfd, STOP_MD, 0);
166 return 1;
167
168 }
169