]>
Commit | Line | Data |
---|---|---|
1 | /* | |
2 | * mdadm - manage Linux "md" devices aka RAID arrays. | |
3 | * | |
4 | * Copyright (C) 2001-2009 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@suse.de> | |
23 | */ | |
24 | ||
25 | #include "mdadm.h" | |
26 | #include "md_p.h" | |
27 | #include <ctype.h> | |
28 | ||
29 | ||
30 | void make_parts(char *dev, int cnt) | |
31 | { | |
32 | /* make 'cnt' partition devices for 'dev' | |
33 | * If dev is a device name we use the | |
34 | * major/minor from dev and add 1..cnt | |
35 | * If it is a symlink, we make similar symlinks. | |
36 | * If dev ends with a digit, we add "p%d" else "%d" | |
37 | * If the name exists, we use it's owner/mode, | |
38 | * else that of dev | |
39 | */ | |
40 | struct stat stb; | |
41 | int major_num; | |
42 | int minor_num; | |
43 | int odig; | |
44 | int i; | |
45 | int nlen = strlen(dev) + 20; | |
46 | char *name; | |
47 | int dig = isdigit(dev[strlen(dev)-1]); | |
48 | char orig[1024]; | |
49 | char sym[1024]; | |
50 | int err; | |
51 | ||
52 | if (cnt==0) cnt=4; | |
53 | if (lstat(dev, &stb)!= 0) | |
54 | return; | |
55 | ||
56 | if (S_ISBLK(stb.st_mode)) { | |
57 | major_num = major(stb.st_rdev); | |
58 | minor_num = minor(stb.st_rdev); | |
59 | odig = -1; | |
60 | } else if (S_ISLNK(stb.st_mode)) { | |
61 | int len = readlink(dev, orig, sizeof(orig)); | |
62 | if (len < 0 || len > 1000) | |
63 | return; | |
64 | orig[len] = 0; | |
65 | odig = isdigit(orig[len-1]); | |
66 | major_num = -1; | |
67 | minor_num = -1; | |
68 | } else | |
69 | return; | |
70 | name = malloc(nlen); | |
71 | for (i=1; i <= cnt ; i++) { | |
72 | struct stat stb2; | |
73 | snprintf(name, nlen, "%s%s%d", dev, dig?"p":"", i); | |
74 | if (stat(name, &stb2)==0) { | |
75 | if (!S_ISBLK(stb2.st_mode) || !S_ISBLK(stb.st_mode)) | |
76 | continue; | |
77 | if (stb2.st_rdev == makedev(major_num, minor_num+i)) | |
78 | continue; | |
79 | unlink(name); | |
80 | } else { | |
81 | stb2 = stb; | |
82 | } | |
83 | if (S_ISBLK(stb.st_mode)) { | |
84 | if (mknod(name, S_IFBLK | 0600, | |
85 | makedev(major_num, minor_num+i))) | |
86 | perror("mknod"); | |
87 | if (chown(name, stb2.st_uid, stb2.st_gid)) | |
88 | perror("chown"); | |
89 | if (chmod(name, stb2.st_mode & 07777)) | |
90 | perror("chmod"); | |
91 | err = 0; | |
92 | } else { | |
93 | snprintf(sym, sizeof(sym), "%s%s%d", orig, odig?"p":"", i); | |
94 | err = symlink(sym, name); | |
95 | } | |
96 | ||
97 | if (err == 0 && stat(name, &stb2) == 0) | |
98 | add_dev(name, &stb2, 0, NULL); | |
99 | } | |
100 | free(name); | |
101 | } | |
102 | ||
103 | ||
104 | /* | |
105 | * We need a new md device to assemble/build/create an array. | |
106 | * 'dev' is a name given us by the user (command line or mdadm.conf) | |
107 | * It might start with /dev or /dev/md any might end with a digit | |
108 | * string. | |
109 | * If it starts with just /dev, it must be /dev/mdX or /dev/md_dX | |
110 | * If it ends with a digit string, then it must be as above, or | |
111 | * 'trustworthy' must be 'METADATA' and the 'dev' must be | |
112 | * /dev/md/'name'NN or 'name'NN | |
113 | * If it doesn't end with a digit string, it must be /dev/md/'name' | |
114 | * or 'name' or must be NULL. | |
115 | * If the digit string is present, it gives the minor number to use | |
116 | * If not, we choose a high, unused minor number. | |
117 | * If the 'dev' is a standard name, it devices whether 'md' or 'mdp'. | |
118 | * else if the name is 'd[0-9]+' then we use mdp | |
119 | * else if trustworthy is 'METADATA' we use md | |
120 | * else the choice depends on 'autof'. | |
121 | * If name is NULL it is assumed to match whatever dev provides. | |
122 | * If both name and dev are NULL, we choose a name 'mdXX' or 'mdpXX' | |
123 | * | |
124 | * If 'name' is given, and 'trustworthy' is 'foreign' and name is not | |
125 | * supported by 'dev', we add a "_%d" suffix based on the minor number | |
126 | * use that. | |
127 | * | |
128 | * If udev is configured, we create a temporary device, open it, and | |
129 | * unlink it. | |
130 | * If not, we create the /dev/mdXX device, and is name is usable, | |
131 | * /dev/md/name | |
132 | * In any case we return /dev/md/name or (if that isn't available) | |
133 | * /dev/mdXX in 'chosen'. | |
134 | * | |
135 | * When we create devices, we use uid/gid/umask from config file. | |
136 | */ | |
137 | ||
138 | int create_mddev(char *dev, char *name, int autof, int trustworthy, | |
139 | char *chosen) | |
140 | { | |
141 | int mdfd; | |
142 | struct stat stb; | |
143 | int num = -1; | |
144 | int use_mdp = -1; | |
145 | struct createinfo *ci = conf_get_create_info(); | |
146 | int parts; | |
147 | char *cname; | |
148 | char devname[20]; | |
149 | char cbuf[400]; | |
150 | if (chosen == NULL) | |
151 | chosen = cbuf; | |
152 | ||
153 | ||
154 | if (autof == 0) | |
155 | autof = ci->autof; | |
156 | ||
157 | parts = autof >> 3; | |
158 | autof &= 7; | |
159 | ||
160 | strcpy(chosen, "/dev/md/"); | |
161 | cname = chosen + strlen(chosen); | |
162 | ||
163 | ||
164 | if (dev) { | |
165 | if (strncmp(dev, "/dev/md/", 8) == 0) { | |
166 | strcpy(cname, dev+8); | |
167 | } else if (strncmp(dev, "/dev/", 5) == 0) { | |
168 | char *e = dev + strlen(dev); | |
169 | while (e > dev && isdigit(e[-1])) | |
170 | e--; | |
171 | if (e[0]) | |
172 | num = strtoul(e, NULL, 10); | |
173 | strcpy(cname, dev+5); | |
174 | cname[e-(dev+5)] = 0; | |
175 | /* name *must* be mdXX or md_dXX in this context */ | |
176 | if (num < 0 || | |
177 | (strcmp(cname, "md") != 0 && strcmp(cname, "md_d") != 0)) { | |
178 | fprintf(stderr, Name ": %s is an invalid name " | |
179 | "for an md device. Try /dev/md/%s\n", | |
180 | dev, dev+5); | |
181 | return -1; | |
182 | } | |
183 | if (strcmp(cname, "md") == 0) | |
184 | use_mdp = 0; | |
185 | else | |
186 | use_mdp = 1; | |
187 | /* recreate name: /dev/md/0 or /dev/md/d0 */ | |
188 | sprintf(cname, "%s%d", use_mdp?"d":"", num); | |
189 | } else | |
190 | strcpy(cname, dev); | |
191 | ||
192 | /* 'cname' must not contain a slash, and may not be | |
193 | * empty. | |
194 | */ | |
195 | if (strchr(cname, '/') != NULL) { | |
196 | fprintf(stderr, Name ": %s is an invalid name " | |
197 | "for an md device.\n", dev); | |
198 | return -1; | |
199 | } | |
200 | if (cname[0] == 0) { | |
201 | fprintf(stderr, Name ": %s is an invalid name " | |
202 | "for an md device (empty!).", dev); | |
203 | return -1; | |
204 | } | |
205 | if (num < 0) { | |
206 | /* If cname is 'N' or 'dN', we get dev number | |
207 | * from there. | |
208 | */ | |
209 | char *sp = cname; | |
210 | char *ep; | |
211 | if (cname[0] == 'd') | |
212 | sp++; | |
213 | if (isdigit(sp[0])) | |
214 | num = strtoul(sp, &ep, 10); | |
215 | else | |
216 | ep = sp; | |
217 | if (ep == sp || *ep || num < 0) | |
218 | num = -1; | |
219 | else if (cname[0] == 'd') | |
220 | use_mdp = 1; | |
221 | else | |
222 | use_mdp = 0; | |
223 | } | |
224 | } | |
225 | ||
226 | /* Now determine device number */ | |
227 | /* named 'METADATA' cannot use 'mdp'. */ | |
228 | if (name && name[0] == 0) | |
229 | name = NULL; | |
230 | if (name && trustworthy == METADATA && use_mdp == 1) { | |
231 | fprintf(stderr, Name ": %s is not allowed for a %s container. " | |
232 | "Consider /dev/md%d.\n", dev, name, num); | |
233 | return -1; | |
234 | } | |
235 | if (name && trustworthy == METADATA) | |
236 | use_mdp = 0; | |
237 | if (use_mdp == -1) { | |
238 | if (autof == 4 || autof == 6) | |
239 | use_mdp = 1; | |
240 | else | |
241 | use_mdp = 0; | |
242 | } | |
243 | if (num < 0 && trustworthy == LOCAL && name) { | |
244 | /* if name is numeric, possibly prefixed by | |
245 | * 'md' or '/dev/md', use that for num | |
246 | * if it is not already in use */ | |
247 | char *ep; | |
248 | char *n2 = name; | |
249 | if (strncmp(n2, "/dev/", 5) == 0) | |
250 | n2 += 5; | |
251 | if (strncmp(n2, "md", 2) == 0) | |
252 | n2 += 2; | |
253 | if (*n2 == '/') | |
254 | n2++; | |
255 | num = strtoul(n2, &ep, 10); | |
256 | if (ep == n2 || *ep) | |
257 | num = -1; | |
258 | else if (mddev_busy(use_mdp ? (-1-num) : num)) | |
259 | num = -1; | |
260 | } | |
261 | ||
262 | if (num < 0) { | |
263 | /* need to choose a free number. */ | |
264 | num = find_free_devnum(use_mdp); | |
265 | if (num == NoMdDev) { | |
266 | fprintf(stderr, Name ": No avail md devices - aborting\n"); | |
267 | return -1; | |
268 | } | |
269 | } else { | |
270 | num = use_mdp ? (-1-num) : num; | |
271 | if (mddev_busy(num)) { | |
272 | fprintf(stderr, Name ": %s is already in use.\n", | |
273 | dev); | |
274 | return -1; | |
275 | } | |
276 | } | |
277 | ||
278 | if (num < 0) | |
279 | sprintf(devname, "/dev/md_d%d", -1-num); | |
280 | else | |
281 | sprintf(devname, "/dev/md%d", num); | |
282 | ||
283 | if (cname[0] == 0 && name) { | |
284 | /* Need to find a name if we can | |
285 | * We don't completely trust 'name'. Truncate to | |
286 | * reasonable length and remove '/' | |
287 | */ | |
288 | char *cp; | |
289 | struct map_ent *map = NULL; | |
290 | int conflict = 1; | |
291 | int unum = 0; | |
292 | int cnlen; | |
293 | strncpy(cname, name, 200); | |
294 | cname[200] = 0; | |
295 | for (cp = cname; *cp ; cp++) | |
296 | switch (*cp) { | |
297 | case '/': | |
298 | *cp = '-'; | |
299 | break; | |
300 | case ' ': | |
301 | case '\t': | |
302 | *cp = '_'; | |
303 | break; | |
304 | } | |
305 | ||
306 | if (trustworthy == LOCAL || | |
307 | (trustworthy == FOREIGN && strchr(cname, ':') != NULL)) { | |
308 | /* Only need suffix if there is a conflict */ | |
309 | if (map_by_name(&map, cname) == NULL) | |
310 | conflict = 0; | |
311 | } | |
312 | cnlen = strlen(cname); | |
313 | while (conflict) { | |
314 | if (trustworthy == METADATA && !isdigit(cname[cnlen-1])) | |
315 | sprintf(cname+cnlen, "%d", unum); | |
316 | else | |
317 | /* add _%d to FOREIGN array that don't | |
318 | * a 'host:' prefix | |
319 | */ | |
320 | sprintf(cname+cnlen, "_%d", unum); | |
321 | unum++; | |
322 | if (map_by_name(&map, cname) == NULL) | |
323 | conflict = 0; | |
324 | } | |
325 | } | |
326 | ||
327 | if (dev && dev[0] == '/') | |
328 | strcpy(chosen, dev); | |
329 | else if (cname[0] == 0) | |
330 | strcpy(chosen, devname); | |
331 | ||
332 | /* We have a device number and name. | |
333 | * If we cannot detect udev, we need to make | |
334 | * devices and links ourselves. | |
335 | */ | |
336 | if ((stat("/dev/.udev", &stb) != 0 && stat("/run/udev", &stb) != 0) || | |
337 | check_env("MDADM_NO_UDEV")) { | |
338 | /* Make sure 'devname' exists and 'chosen' is a symlink to it */ | |
339 | if (lstat(devname, &stb) == 0) { | |
340 | /* Must be the correct device, else error */ | |
341 | if ((stb.st_mode&S_IFMT) != S_IFBLK || | |
342 | stb.st_rdev != makedev(dev2major(num),dev2minor(num))) { | |
343 | fprintf(stderr, Name ": %s exists but looks wrong, please fix\n", | |
344 | devname); | |
345 | return -1; | |
346 | } | |
347 | } else { | |
348 | if (mknod(devname, S_IFBLK|0600, | |
349 | makedev(dev2major(num),dev2minor(num))) != 0) { | |
350 | fprintf(stderr, Name ": failed to create %s\n", | |
351 | devname); | |
352 | return -1; | |
353 | } | |
354 | if (chown(devname, ci->uid, ci->gid)) | |
355 | perror("chown"); | |
356 | if (chmod(devname, ci->mode)) | |
357 | perror("chmod"); | |
358 | stat(devname, &stb); | |
359 | add_dev(devname, &stb, 0, NULL); | |
360 | } | |
361 | if (use_mdp == 1) | |
362 | make_parts(devname, parts); | |
363 | if (strcmp(chosen, devname) != 0) { | |
364 | ||
365 | if (mkdir("/dev/md",0700)==0) { | |
366 | if (chown("/dev/md", ci->uid, ci->gid)) | |
367 | perror("chown /dev/md"); | |
368 | if (chmod("/dev/md", ci->mode| ((ci->mode>>2) & 0111))) | |
369 | perror("chmod /dev/md"); | |
370 | } | |
371 | ||
372 | if (dev && strcmp(chosen, dev) == 0) | |
373 | /* We know we are allowed to use this name */ | |
374 | unlink(chosen); | |
375 | ||
376 | if (lstat(chosen, &stb) == 0) { | |
377 | char buf[300]; | |
378 | ssize_t link_len = readlink(chosen, buf, sizeof(buf)-1); | |
379 | if (link_len >= 0) | |
380 | buf[link_len] = '\0'; | |
381 | ||
382 | if ((stb.st_mode & S_IFMT) != S_IFLNK || | |
383 | link_len < 0 || | |
384 | strcmp(buf, devname) != 0) { | |
385 | fprintf(stderr, Name ": %s exists - ignoring\n", | |
386 | chosen); | |
387 | strcpy(chosen, devname); | |
388 | } | |
389 | } else if (symlink(devname, chosen) != 0) | |
390 | fprintf(stderr, Name ": failed to create %s: %s\n", | |
391 | chosen, strerror(errno)); | |
392 | if (use_mdp && strcmp(chosen, devname) != 0) | |
393 | make_parts(chosen, parts); | |
394 | } | |
395 | } | |
396 | mdfd = open_dev_excl(num); | |
397 | if (mdfd < 0) | |
398 | fprintf(stderr, Name ": unexpected failure opening %s\n", | |
399 | devname); | |
400 | return mdfd; | |
401 | } | |
402 | ||
403 | ||
404 | /* Open this and check that it is an md device. | |
405 | * On success, return filedescriptor. | |
406 | * On failure, return -1 if it doesn't exist, | |
407 | * or -2 if it exists but is not an md device. | |
408 | */ | |
409 | int open_mddev(char *dev, int report_errors) | |
410 | { | |
411 | int mdfd = open(dev, O_RDWR); | |
412 | if (mdfd < 0 && errno == EACCES) | |
413 | mdfd = open(dev, O_RDONLY); | |
414 | if (mdfd < 0) { | |
415 | if (report_errors) | |
416 | fprintf(stderr, Name ": error opening %s: %s\n", | |
417 | dev, strerror(errno)); | |
418 | return -1; | |
419 | } | |
420 | if (md_get_version(mdfd) <= 0) { | |
421 | close(mdfd); | |
422 | if (report_errors) | |
423 | fprintf(stderr, Name ": %s does not appear to be " | |
424 | "an md device\n", dev); | |
425 | return -2; | |
426 | } | |
427 | return mdfd; | |
428 | } |