]>
Commit | Line | Data |
---|---|---|
1 | /* | |
2 | * mdadm - manage Linux "md" devices aka RAID arrays. | |
3 | * | |
4 | * Copyright (C) 2001-2006 Neil Brown <neilb@suse.de> | |
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 "mdadm.h" | |
31 | #include "md_p.h" | |
32 | #include <ctype.h> | |
33 | ||
34 | void make_parts(char *dev, int cnt) | |
35 | { | |
36 | /* make 'cnt' partition devices for 'dev' | |
37 | * We use the major/minor from dev and add 1..cnt | |
38 | * If dev ends with a digit, we add "p%d" else "%d" | |
39 | * If the name exists, we use it's owner/mode, | |
40 | * else that of dev | |
41 | */ | |
42 | struct stat stb; | |
43 | int major, minor; | |
44 | int i; | |
45 | int nlen = strlen(dev) + 20; | |
46 | char *name = malloc(nlen); | |
47 | int dig = isdigit(dev[strlen(dev)-1]); | |
48 | ||
49 | if (cnt==0) cnt=4; | |
50 | if (stat(dev, &stb)!= 0) | |
51 | return; | |
52 | if (!S_ISBLK(stb.st_mode)) | |
53 | return; | |
54 | major = major(stb.st_rdev); | |
55 | minor = minor(stb.st_rdev); | |
56 | for (i=1; i <= cnt ; i++) { | |
57 | struct stat stb2; | |
58 | snprintf(name, nlen, "%s%s%d", dev, dig?"p":"", i); | |
59 | if (stat(name, &stb2)==0) { | |
60 | if (!S_ISBLK(stb2.st_mode)) | |
61 | continue; | |
62 | if (stb2.st_rdev == makedev(major, minor+i)) | |
63 | continue; | |
64 | unlink(name); | |
65 | } else { | |
66 | stb2 = stb; | |
67 | } | |
68 | if (mknod(name, S_IFBLK | 0600, makedev(major, minor+i))) | |
69 | perror("mknod"); | |
70 | if (chown(name, stb2.st_uid, stb2.st_gid)) | |
71 | perror("chown"); | |
72 | if (chmod(name, stb2.st_mode & 07777)) | |
73 | perror("chmod"); | |
74 | stat(name, &stb2); | |
75 | add_dev(name, &stb2, 0, NULL); | |
76 | } | |
77 | } | |
78 | ||
79 | /* | |
80 | * Open a given md device, and check that it really is one. | |
81 | * If 'autof' is given, then we need to create, or recreate, the md device. | |
82 | * If the name already exists, and is not a block device, we fail. | |
83 | * If it exists and is not an md device, is not the right type (partitioned or not), | |
84 | * or is currently in-use, we remove the device, but remember the owner and mode. | |
85 | * If it now doesn't exist, we find a new md array and create the device. | |
86 | * Default ownership/mode comes from config file. | |
87 | */ | |
88 | int open_mddev(char *dev, int autof) | |
89 | { | |
90 | int mdfd; | |
91 | struct stat stb; | |
92 | int major = MD_MAJOR; | |
93 | int minor = 0; | |
94 | int must_remove = 0; | |
95 | struct mdstat_ent *mdlist; | |
96 | int num; | |
97 | struct createinfo *ci = conf_get_create_info(); | |
98 | int parts; | |
99 | ||
100 | if (autof == 0) | |
101 | autof = ci->autof; | |
102 | ||
103 | parts = autof >> 3; | |
104 | autof &= 7; | |
105 | ||
106 | if (autof && autof != 1) { | |
107 | /* autof is set, so we need to check that the name is ok, | |
108 | * and possibly create one if not | |
109 | */ | |
110 | int std; | |
111 | stb.st_mode = 0; | |
112 | if (stat(dev, &stb)==0 && ! S_ISBLK(stb.st_mode)) { | |
113 | fprintf(stderr, Name ": %s is not a block device.\n", | |
114 | dev); | |
115 | return -1; | |
116 | } | |
117 | if (autof == 2 && stb.st_mode == 0 && !is_standard(dev, NULL)) { | |
118 | fprintf(stderr, Name ": --auto=yes requires a 'standard' md device name, not %s\n", dev); | |
119 | return -1; | |
120 | } | |
121 | /* check major number is correct */ | |
122 | num = -1; | |
123 | std = is_standard(dev, &num); | |
124 | if (std>0) major = get_mdp_major(); | |
125 | switch(autof) { | |
126 | case 2: /* only create is_standard names */ | |
127 | if (!std && !stb.st_mode) { | |
128 | fprintf(stderr, Name ": --auto=yes requires a 'standard' md device name, not %s\n", dev); | |
129 | return -1; | |
130 | } | |
131 | break; | |
132 | case 3: /* create md, reject std>0 */ | |
133 | if (std > 0) { | |
134 | fprintf(stderr, Name ": that --auto option not compatable with device named %s\n", dev); | |
135 | return -1; | |
136 | } | |
137 | break; | |
138 | case 4: /* create mdp, reject std<0 */ | |
139 | if (std < 0) { | |
140 | fprintf(stderr, Name ": that --auto option not compatable with device named %s\n", dev); | |
141 | return -1; | |
142 | } | |
143 | break; | |
144 | case 5: /* default to md if not standard */ | |
145 | break; | |
146 | case 6: /* default to mdp if not standard */ | |
147 | if (std == 0) major = get_mdp_major(); | |
148 | break; | |
149 | } | |
150 | /* major is final. num is -1 if not standard */ | |
151 | if (stb.st_mode && major(stb.st_rdev) != major) | |
152 | must_remove = 1; | |
153 | if (stb.st_mode && !must_remove) { | |
154 | mdu_array_info_t array; | |
155 | /* looks ok, see if it is available */ | |
156 | mdfd = open(dev, O_RDWR, 0); | |
157 | if (mdfd < 0) { | |
158 | fprintf(stderr, Name ": error opening %s: %s\n", | |
159 | dev, strerror(errno)); | |
160 | return -1; | |
161 | } else if (md_get_version(mdfd) <= 0) { | |
162 | fprintf(stderr, Name ": %s does not appear to be an md device\n", | |
163 | dev); | |
164 | close(mdfd); | |
165 | return -1; | |
166 | } | |
167 | if (ioctl(mdfd, GET_ARRAY_INFO, &array)==0) { | |
168 | /* already active */ | |
169 | close(mdfd); | |
170 | fprintf(stderr, Name ": %s is already active.\n", | |
171 | dev); | |
172 | return -1; | |
173 | } else { | |
174 | if (major != MD_MAJOR && parts > 0) | |
175 | make_parts(dev, parts); | |
176 | return mdfd; | |
177 | } | |
178 | } | |
179 | /* Ok, need to find a minor that is not in use. | |
180 | * If the device name is in a 'standard' format, | |
181 | * intuit the minor from that, else | |
182 | * easiest to read /proc/mdstat, and hunt through for | |
183 | * an unused number | |
184 | */ | |
185 | if (num < 0) { | |
186 | /* need to pick an unused number */ | |
187 | mdlist = mdstat_read(0, 0); | |
188 | /* Choose a large number. Start from 127 and search down, | |
189 | * but if nothing is found, start really big | |
190 | */ | |
191 | for (num = 127 ; num != 128 ; num = num ? num-1 : (1<<22)-1) { | |
192 | struct mdstat_ent *me; | |
193 | int devnum = num; | |
194 | if (major != MD_MAJOR) | |
195 | devnum = -1-num; | |
196 | ||
197 | for (me=mdlist; me; me=me->next) | |
198 | if (me->devnum == devnum) | |
199 | break; | |
200 | if (!me) { | |
201 | /* doesn't exist in mdstat. | |
202 | * make sure it is new to /dev too | |
203 | */ | |
204 | char *dn; | |
205 | if (major != MD_MAJOR) | |
206 | minor = num << MdpMinorShift; | |
207 | else | |
208 | minor = num; | |
209 | dn = map_dev(major,minor, 0); | |
210 | if (dn==NULL || is_standard(dn, NULL)) { | |
211 | /* this number only used by a 'standard' name, | |
212 | * so it is safe to use | |
213 | */ | |
214 | break; | |
215 | } | |
216 | } | |
217 | } | |
218 | } else if (major == MD_MAJOR) | |
219 | minor = num; | |
220 | else | |
221 | minor = num << MdpMinorShift; | |
222 | /* major and minor have been chosen */ | |
223 | ||
224 | /* If it was a 'standard' name and it is in-use, then | |
225 | * the device could already be correct | |
226 | */ | |
227 | if (stb.st_mode && major(stb.st_rdev) == major && | |
228 | minor(stb.st_rdev) == minor) | |
229 | ; | |
230 | else { | |
231 | if (major(makedev(major,minor)) != major || | |
232 | minor(makedev(major,minor)) != minor) { | |
233 | fprintf(stderr, Name ": Need newer C library to use more than 4 partitionable md devices, sorry\n"); | |
234 | return -1; | |
235 | } | |
236 | if (must_remove) | |
237 | unlink(dev); | |
238 | ||
239 | if (mknod(dev, S_IFBLK|0600, makedev(major, minor))!= 0) { | |
240 | fprintf(stderr, Name ": failed to create %s\n", dev); | |
241 | return -1; | |
242 | } | |
243 | if (must_remove) { | |
244 | if (chown(dev, stb.st_uid, stb.st_gid)) | |
245 | perror("chown"); | |
246 | if (chmod(dev, stb.st_mode & 07777)) | |
247 | perror("chmod"); | |
248 | } else { | |
249 | if (chown(dev, ci->uid, ci->gid)) | |
250 | perror("chown"); | |
251 | if (chmod(dev, ci->mode)) | |
252 | perror("chmod"); | |
253 | } | |
254 | stat(dev, &stb); | |
255 | add_dev(dev, &stb, 0, NULL); | |
256 | if (major != MD_MAJOR) | |
257 | make_parts(dev,parts); | |
258 | } | |
259 | } | |
260 | mdfd = open(dev, O_RDWR, 0); | |
261 | if (mdfd < 0) | |
262 | fprintf(stderr, Name ": error opening %s: %s\n", | |
263 | dev, strerror(errno)); | |
264 | else if (md_get_version(mdfd) <= 0) { | |
265 | fprintf(stderr, Name ": %s does not appear to be an md device\n", | |
266 | dev); | |
267 | close(mdfd); | |
268 | mdfd = -1; | |
269 | } | |
270 | return mdfd; | |
271 | } | |
272 |