]>
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 | ||
35 | void make_dev_symlink(char *dev) | |
36 | { | |
37 | char *new = strdup(dev); | |
38 | ||
39 | if (!new) return; | |
40 | /* /dev/md/0 -> /dev/md0 | |
41 | * /dev/md/d0 -> /dev/md_d0 | |
42 | */ | |
43 | if (isdigit(new[8])) | |
44 | strcpy(new+7, new+8); | |
45 | else | |
46 | new[7] = '_'; | |
47 | if (symlink(dev+5, new)) | |
48 | perror(new); | |
49 | } | |
50 | ||
51 | ||
52 | void make_parts(char *dev, int cnt, int symlinks) | |
53 | { | |
54 | /* make 'cnt' partition devices for 'dev' | |
55 | * We use the major/minor from dev and add 1..cnt | |
56 | * If dev ends with a digit, we add "p%d" else "%d" | |
57 | * If the name exists, we use it's owner/mode, | |
58 | * else that of dev | |
59 | */ | |
60 | struct stat stb; | |
61 | int major_num, minor_num; | |
62 | int i; | |
63 | int nlen = strlen(dev) + 20; | |
64 | char *name = malloc(nlen); | |
65 | int dig = isdigit(dev[strlen(dev)-1]); | |
66 | ||
67 | if (cnt==0) cnt=4; | |
68 | if (stat(dev, &stb)!= 0) | |
69 | return; | |
70 | if (!S_ISBLK(stb.st_mode)) | |
71 | return; | |
72 | major_num = major(stb.st_rdev); | |
73 | minor_num = minor(stb.st_rdev); | |
74 | for (i=1; i <= cnt ; i++) { | |
75 | struct stat stb2; | |
76 | snprintf(name, nlen, "%s%s%d", dev, dig?"p":"", i); | |
77 | if (stat(name, &stb2)==0) { | |
78 | if (!S_ISBLK(stb2.st_mode)) | |
79 | continue; | |
80 | if (stb2.st_rdev == makedev(major_num, minor_num+i)) | |
81 | continue; | |
82 | unlink(name); | |
83 | } else { | |
84 | stb2 = stb; | |
85 | } | |
86 | if (mknod(name, S_IFBLK | 0600, makedev(major_num, minor_num+i))) | |
87 | perror("mknod"); | |
88 | if (chown(name, stb2.st_uid, stb2.st_gid)) | |
89 | perror("chown"); | |
90 | if (chmod(name, stb2.st_mode & 07777)) | |
91 | perror("chmod"); | |
92 | if (symlinks && strncmp(name, "/dev/md/", 8) == 0) | |
93 | make_dev_symlink(name); | |
94 | stat(name, &stb2); | |
95 | add_dev(name, &stb2, 0, NULL); | |
96 | } | |
97 | } | |
98 | ||
99 | ||
100 | /* | |
101 | * Open a given md device, and check that it really is one. | |
102 | * If 'autof' is given, then we need to create, or recreate, the md device. | |
103 | * If the name already exists, and is not a block device, we fail. | |
104 | * If it exists and is not an md device, is not the right type (partitioned or not), | |
105 | * or is currently in-use, we remove the device, but remember the owner and mode. | |
106 | * If it now doesn't exist, we find a new md array and create the device. | |
107 | * Default ownership/mode comes from config file. | |
108 | */ | |
109 | int open_mddev(char *dev, int autof) | |
110 | { | |
111 | int mdfd; | |
112 | struct stat stb; | |
113 | int major_num = MD_MAJOR; | |
114 | int minor_num = 0; | |
115 | int must_remove = 0; | |
116 | int num; | |
117 | struct createinfo *ci = conf_get_create_info(); | |
118 | int parts; | |
119 | ||
120 | if (autof == 0) | |
121 | autof = ci->autof; | |
122 | ||
123 | parts = autof >> 3; | |
124 | autof &= 7; | |
125 | ||
126 | if (autof && autof != 1) { | |
127 | /* autof is set, so we need to check that the name is ok, | |
128 | * and possibly create one if not | |
129 | */ | |
130 | int std; | |
131 | stb.st_mode = 0; | |
132 | if (stat(dev, &stb)==0 && ! S_ISBLK(stb.st_mode)) { | |
133 | fprintf(stderr, Name ": %s is not a block device.\n", | |
134 | dev); | |
135 | return -1; | |
136 | } | |
137 | /* check major number is correct */ | |
138 | num = -1; | |
139 | std = is_standard(dev, &num); | |
140 | if (std>0) major_num = get_mdp_major(); | |
141 | switch(autof) { | |
142 | case 2: /* only create is_standard names */ | |
143 | if (!std && !stb.st_mode) { | |
144 | fprintf(stderr, Name | |
145 | ": %s does not exist and is not a 'standard' name " | |
146 | "so it cannot be created\n", dev); | |
147 | return -1; | |
148 | } | |
149 | break; | |
150 | case 3: /* create md, reject std>0 */ | |
151 | if (std > 0) { | |
152 | fprintf(stderr, Name ": that --auto option " | |
153 | "not compatable with device named %s\n", dev); | |
154 | return -1; | |
155 | } | |
156 | break; | |
157 | case 4: /* create mdp, reject std<0 */ | |
158 | if (std < 0) { | |
159 | fprintf(stderr, Name ": that --auto option " | |
160 | "not compatable with device named %s\n", dev); | |
161 | return -1; | |
162 | } | |
163 | major_num = get_mdp_major(); | |
164 | break; | |
165 | case 5: /* default to md if not standard */ | |
166 | break; | |
167 | case 6: /* default to mdp if not standard */ | |
168 | if (std == 0) major_num = get_mdp_major(); | |
169 | break; | |
170 | } | |
171 | /* major is final. num is -1 if not standard */ | |
172 | if (stb.st_mode && major(stb.st_rdev) != major_num) | |
173 | must_remove = 1; | |
174 | if (stb.st_mode && !must_remove) { | |
175 | /* looks ok, see if it is available */ | |
176 | mdfd = open(dev, O_RDWR, 0); | |
177 | if (mdfd < 0) { | |
178 | fprintf(stderr, Name ": error opening %s: %s\n", | |
179 | dev, strerror(errno)); | |
180 | return -1; | |
181 | } else if (md_get_version(mdfd) <= 0) { | |
182 | fprintf(stderr, Name ": %s does not appear to be an md device\n", | |
183 | dev); | |
184 | close(mdfd); | |
185 | return -1; | |
186 | } | |
187 | if (major_num != MD_MAJOR && parts > 0) | |
188 | make_parts(dev, parts, ci->symlinks); | |
189 | return mdfd; | |
190 | } | |
191 | /* Ok, need to find a minor that is not in use. | |
192 | * If the device name is in a 'standard' format, | |
193 | * intuit the minor from that, else | |
194 | * easiest to read /proc/mdstat, and hunt through for | |
195 | * an unused number | |
196 | */ | |
197 | if (num < 0) { | |
198 | /* need to pick an unused number */ | |
199 | int num = find_free_devnum(major_num != MD_MAJOR); | |
200 | ||
201 | if (major_num == MD_MAJOR) | |
202 | minor_num = num; | |
203 | else | |
204 | minor_num = (-1-num) << MdpMinorShift; | |
205 | } else if (major_num == MD_MAJOR) | |
206 | minor_num = num; | |
207 | else | |
208 | minor_num = num << MdpMinorShift; | |
209 | /* major and minor have been chosen */ | |
210 | ||
211 | /* If it was a 'standard' name and it is in-use, then | |
212 | * the device could already be correct | |
213 | */ | |
214 | if (stb.st_mode && major(stb.st_rdev) == major_num && | |
215 | minor(stb.st_rdev) == minor_num) | |
216 | ; | |
217 | else { | |
218 | if (major(makedev(major_num,minor_num)) != major_num || | |
219 | minor(makedev(major_num,minor_num)) != minor_num) { | |
220 | fprintf(stderr, Name ": Need newer C library to use more than 4 partitionable md devices, sorry\n"); | |
221 | return -1; | |
222 | } | |
223 | if (must_remove) | |
224 | unlink(dev); | |
225 | ||
226 | if (strncmp(dev, "/dev/md/", 8) == 0) { | |
227 | if (mkdir("/dev/md",0700)==0) { | |
228 | if (chown("/dev/md", ci->uid, ci->gid)) | |
229 | perror("chown /dev/md"); | |
230 | if (chmod("/dev/md", ci->mode| ((ci->mode>>2) & 0111))) | |
231 | perror("chmod /dev/md"); | |
232 | } | |
233 | } | |
234 | if (mknod(dev, S_IFBLK|0600, makedev(major_num, minor_num))!= 0) { | |
235 | fprintf(stderr, Name ": failed to create %s\n", dev); | |
236 | return -1; | |
237 | } | |
238 | if (must_remove) { | |
239 | if (chown(dev, stb.st_uid, stb.st_gid)) | |
240 | perror("chown"); | |
241 | if (chmod(dev, stb.st_mode & 07777)) | |
242 | perror("chmod"); | |
243 | } else { | |
244 | if (chown(dev, ci->uid, ci->gid)) | |
245 | perror("chown"); | |
246 | if (chmod(dev, ci->mode)) | |
247 | perror("chmod"); | |
248 | } | |
249 | stat(dev, &stb); | |
250 | add_dev(dev, &stb, 0, NULL); | |
251 | if (ci->symlinks && strncmp(dev, "/dev/md/", 8) == 0) | |
252 | make_dev_symlink(dev); | |
253 | if (major_num != MD_MAJOR) | |
254 | make_parts(dev,parts, ci->symlinks); | |
255 | } | |
256 | } | |
257 | mdfd = open(dev, O_RDWR, 0); | |
258 | if (mdfd < 0) | |
259 | fprintf(stderr, Name ": error opening %s: %s\n", | |
260 | dev, strerror(errno)); | |
261 | else if (md_get_version(mdfd) <= 0) { | |
262 | fprintf(stderr, Name ": %s does not appear to be an md device\n", | |
263 | dev); | |
264 | close(mdfd); | |
265 | mdfd = -1; | |
266 | } | |
267 | return mdfd; | |
268 | } | |
269 | ||
270 | ||
271 | int open_mddev_devnum(char *devname, int devnum, char *name, | |
272 | char *chosen_name, int parts) | |
273 | { | |
274 | /* Open the md device with number 'devnum', possibly using 'devname', | |
275 | * possibly constructing a name with 'name', but in any case, copying | |
276 | * the name into 'chosen_name' | |
277 | */ | |
278 | int major_num, minor_num; | |
279 | struct stat stb; | |
280 | int i; | |
281 | struct createinfo *ci = conf_get_create_info(); | |
282 | ||
283 | if (devname) | |
284 | strcpy(chosen_name, devname); | |
285 | else if (name && strchr(name,'/') == NULL) { | |
286 | char *n = strchr(name, ':'); | |
287 | if (n) n++; else n = name; | |
288 | if (isdigit(*n) && devnum < 0) | |
289 | sprintf(chosen_name, "/dev/md/d%s", n); | |
290 | else | |
291 | sprintf(chosen_name, "/dev/md/%s", n); | |
292 | } else { | |
293 | if (devnum >= 0) | |
294 | sprintf(chosen_name, "/dev/md%d", devnum); | |
295 | else | |
296 | sprintf(chosen_name, "/dev/md/d%d", -1-devnum); | |
297 | } | |
298 | if (devnum >= 0) { | |
299 | major_num = MD_MAJOR; | |
300 | minor_num = devnum; | |
301 | } else { | |
302 | major_num = get_mdp_major(); | |
303 | minor_num = (-1-devnum) << 6; | |
304 | } | |
305 | if (stat(chosen_name, &stb) == 0) { | |
306 | /* It already exists. Check it is right. */ | |
307 | if ( ! S_ISBLK(stb.st_mode) || | |
308 | stb.st_rdev != makedev(major_num, minor_num)) { | |
309 | errno = EEXIST; | |
310 | return -1; | |
311 | } | |
312 | } else { | |
313 | /* special case: if --incremental is suggesting a name | |
314 | * in /dev/md/, we make sure the directory exists. | |
315 | */ | |
316 | if (strncmp(chosen_name, "/dev/md/", 8) == 0) { | |
317 | if (mkdir("/dev/md",0700)==0) { | |
318 | if (chown("/dev/md", ci->uid, ci->gid)) | |
319 | perror("chown /dev/md"); | |
320 | if (chmod("/dev/md", ci->mode| | |
321 | ((ci->mode>>2) & 0111))) | |
322 | perror("chmod /dev/md"); | |
323 | } | |
324 | } | |
325 | ||
326 | if (mknod(chosen_name, S_IFBLK | 0600, | |
327 | makedev(major_num, minor_num)) != 0) { | |
328 | return -1; | |
329 | } | |
330 | /* FIXME chown/chmod ?? */ | |
331 | } | |
332 | ||
333 | /* Simple locking to avoid --incr being called for the same | |
334 | * array multiple times in parallel. | |
335 | */ | |
336 | for (i = 0; i < 25 ; i++) { | |
337 | int fd; | |
338 | ||
339 | fd = open(chosen_name, O_RDWR|O_EXCL); | |
340 | if (fd >= 0 || errno != EBUSY) { | |
341 | if (devnum < 0) | |
342 | make_parts(chosen_name, parts, ci->symlinks); | |
343 | return fd; | |
344 | } | |
345 | usleep(200000); | |
346 | } | |
347 | return -1; | |
348 | } |