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