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