]>
Commit | Line | Data |
---|---|---|
b5e64645 NB |
1 | /* |
2 | * mdadm - manage Linux "md" devices aka RAID arrays. | |
3 | * | |
ca3b6696 | 4 | * Copyright (C) 2001-2012 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 | |
e736b623 | 22 | * Email: <neilb@suse.de> |
b5e64645 NB |
23 | */ |
24 | ||
25 | #include "mdadm.h" | |
26 | #include "md_p.h" | |
27 | #include <ctype.h> | |
28 | ||
c4fe2d4f | 29 | void make_parts(char *dev, int cnt) |
b5e64645 NB |
30 | { |
31 | /* make 'cnt' partition devices for 'dev' | |
c4fe2d4f N |
32 | * If dev is a device name we use the |
33 | * major/minor from dev and add 1..cnt | |
34 | * If it is a symlink, we make similar symlinks. | |
8d80900b | 35 | * If dev ends with a digit, we add "p%d" else "%d" |
b5e64645 NB |
36 | * If the name exists, we use it's owner/mode, |
37 | * else that of dev | |
38 | */ | |
39 | struct stat stb; | |
96ae5973 JS |
40 | int major_num; |
41 | int minor_num; | |
42 | int odig; | |
b5e64645 | 43 | int i; |
8f23b0b3 | 44 | int nlen = strlen(dev) + 20; |
0eb26465 | 45 | char *name; |
b5e64645 | 46 | int dig = isdigit(dev[strlen(dev)-1]); |
c4fe2d4f N |
47 | char orig[1024]; |
48 | char sym[1024]; | |
614825ea | 49 | int err; |
b5e64645 | 50 | |
ca3b6696 N |
51 | if (cnt == 0) |
52 | cnt = 4; | |
c4fe2d4f | 53 | if (lstat(dev, &stb)!= 0) |
b5e64645 | 54 | return; |
0eb26465 | 55 | |
96ae5973 JS |
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)) { | |
c4fe2d4f N |
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]); | |
96ae5973 JS |
66 | major_num = -1; |
67 | minor_num = -1; | |
c4fe2d4f | 68 | } else |
96ae5973 | 69 | return; |
503975b9 | 70 | name = xmalloc(nlen); |
ca3b6696 | 71 | for (i = 1; i <= cnt ; i++) { |
b5e64645 | 72 | struct stat stb2; |
8f23b0b3 | 73 | snprintf(name, nlen, "%s%s%d", dev, dig?"p":"", i); |
ca3b6696 | 74 | if (stat(name, &stb2) == 0) { |
96ae5973 | 75 | if (!S_ISBLK(stb2.st_mode) || !S_ISBLK(stb.st_mode)) |
b5e64645 | 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 | 99 | } |
0eb26465 | 100 | free(name); |
b5e64645 NB |
101 | } |
102 | ||
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 | * | |
ca3b6696 | 127 | * If udev is configured, we create a temporary device, open it, and |
69207ff6 | 128 | * unlink it. |
eca944fa | 129 | * If not, we create the /dev/mdXX device, and if name is usable, |
69207ff6 N |
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]; | |
4dd2df09 | 148 | char devnm[32]; |
69207ff6 N |
149 | char cbuf[400]; |
150 | if (chosen == NULL) | |
151 | chosen = cbuf; | |
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 | ||
69207ff6 | 162 | if (dev) { |
69207ff6 N |
163 | if (strncmp(dev, "/dev/md/", 8) == 0) { |
164 | strcpy(cname, dev+8); | |
165 | } else if (strncmp(dev, "/dev/", 5) == 0) { | |
166 | char *e = dev + strlen(dev); | |
167 | while (e > dev && isdigit(e[-1])) | |
168 | e--; | |
169 | if (e[0]) | |
170 | num = strtoul(e, NULL, 10); | |
171 | strcpy(cname, dev+5); | |
172 | cname[e-(dev+5)] = 0; | |
173 | /* name *must* be mdXX or md_dXX in this context */ | |
174 | if (num < 0 || | |
175 | (strcmp(cname, "md") != 0 && strcmp(cname, "md_d") != 0)) { | |
e7b84f9d | 176 | pr_err("%s is an invalid name " |
69207ff6 N |
177 | "for an md device. Try /dev/md/%s\n", |
178 | dev, dev+5); | |
f1ae21c4 NB |
179 | return -1; |
180 | } | |
69207ff6 N |
181 | if (strcmp(cname, "md") == 0) |
182 | use_mdp = 0; | |
183 | else | |
184 | use_mdp = 1; | |
11fb4c05 N |
185 | /* recreate name: /dev/md/0 or /dev/md/d0 */ |
186 | sprintf(cname, "%s%d", use_mdp?"d":"", num); | |
69207ff6 N |
187 | } else |
188 | strcpy(cname, dev); | |
189 | ||
11fb4c05 N |
190 | /* 'cname' must not contain a slash, and may not be |
191 | * empty. | |
69207ff6 | 192 | */ |
11fb4c05 | 193 | if (strchr(cname, '/') != NULL) { |
e7b84f9d | 194 | pr_err("%s is an invalid name " |
69207ff6 N |
195 | "for an md device.\n", dev); |
196 | return -1; | |
f1ae21c4 | 197 | } |
11fb4c05 | 198 | if (cname[0] == 0) { |
e7b84f9d | 199 | pr_err("%s is an invalid name " |
69207ff6 N |
200 | "for an md device (empty!).", dev); |
201 | return -1; | |
b5e64645 | 202 | } |
11fb4c05 N |
203 | if (num < 0) { |
204 | /* If cname is 'N' or 'dN', we get dev number | |
205 | * from there. | |
206 | */ | |
207 | char *sp = cname; | |
208 | char *ep; | |
209 | if (cname[0] == 'd') | |
210 | sp++; | |
4cda8682 JM |
211 | if (isdigit(sp[0])) |
212 | num = strtoul(sp, &ep, 10); | |
213 | else | |
214 | ep = sp; | |
11fb4c05 N |
215 | if (ep == sp || *ep || num < 0) |
216 | num = -1; | |
217 | else if (cname[0] == 'd') | |
218 | use_mdp = 1; | |
219 | else | |
220 | use_mdp = 0; | |
221 | } | |
69207ff6 | 222 | } |
f1ae21c4 | 223 | |
69207ff6 N |
224 | /* Now determine device number */ |
225 | /* named 'METADATA' cannot use 'mdp'. */ | |
226 | if (name && name[0] == 0) | |
227 | name = NULL; | |
228 | if (name && trustworthy == METADATA && use_mdp == 1) { | |
e7b84f9d | 229 | pr_err("%s is not allowed for a %s container. " |
69207ff6 N |
230 | "Consider /dev/md%d.\n", dev, name, num); |
231 | return -1; | |
232 | } | |
233 | if (name && trustworthy == METADATA) | |
234 | use_mdp = 0; | |
235 | if (use_mdp == -1) { | |
236 | if (autof == 4 || autof == 6) | |
237 | use_mdp = 1; | |
f1ae21c4 | 238 | else |
69207ff6 N |
239 | use_mdp = 0; |
240 | } | |
241 | if (num < 0 && trustworthy == LOCAL && name) { | |
ca3b6696 | 242 | /* if name is numeric, possibly prefixed by |
d7ba0c55 | 243 | * 'md' or '/dev/md', use that for num |
bde2c6e2 | 244 | * if it is not already in use */ |
69207ff6 | 245 | char *ep; |
d7ba0c55 N |
246 | char *n2 = name; |
247 | if (strncmp(n2, "/dev/", 5) == 0) | |
248 | n2 += 5; | |
249 | if (strncmp(n2, "md", 2) == 0) | |
250 | n2 += 2; | |
251 | if (*n2 == '/') | |
252 | n2++; | |
253 | num = strtoul(n2, &ep, 10); | |
254 | if (ep == n2 || *ep) | |
69207ff6 | 255 | num = -1; |
4dd2df09 N |
256 | else { |
257 | sprintf(devnm, "md%s%d", use_mdp ? "_d":"", num); | |
258 | if (mddev_busy(devnm)) | |
259 | num = -1; | |
260 | } | |
69207ff6 N |
261 | } |
262 | ||
69207ff6 N |
263 | if (cname[0] == 0 && name) { |
264 | /* Need to find a name if we can | |
265 | * We don't completely trust 'name'. Truncate to | |
266 | * reasonable length and remove '/' | |
8d80900b | 267 | */ |
69207ff6 | 268 | char *cp; |
f2e55ecc N |
269 | struct map_ent *map = NULL; |
270 | int conflict = 1; | |
271 | int unum = 0; | |
272 | int cnlen; | |
69207ff6 N |
273 | strncpy(cname, name, 200); |
274 | cname[200] = 0; | |
7103b9b8 N |
275 | for (cp = cname; *cp ; cp++) |
276 | switch (*cp) { | |
277 | case '/': | |
278 | *cp = '-'; | |
279 | break; | |
280 | case ' ': | |
281 | case '\t': | |
282 | *cp = '_'; | |
283 | break; | |
284 | } | |
285 | ||
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) { | |
add394f3 | 294 | if (trustworthy == METADATA && !isdigit(cname[cnlen-1])) |
f2e55ecc N |
295 | sprintf(cname+cnlen, "%d", unum); |
296 | else | |
ca3b6696 | 297 | /* add _%d to FOREIGN array that don't |
f2e55ecc N |
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 | 305 | } |
9a40c327 | 306 | |
eca944fa N |
307 | devnm[0] = 0; |
308 | if (num < 0 && cname && ci->names) { | |
309 | int fd; | |
310 | int n = -1; | |
311 | sprintf(devnm, "md_%s", cname); | |
312 | fd = open("/sys/module/md_mod/parameters/new_array", O_WRONLY); | |
313 | if (fd >= 0) { | |
314 | n = write(fd, devnm, strlen(devnm)); | |
315 | close(fd); | |
316 | } | |
317 | if (n < 0) | |
318 | devnm[0] = 0; | |
319 | } | |
320 | if (devnm[0]) | |
321 | ; | |
322 | else if (num < 0) { | |
323 | /* need to choose a free number. */ | |
324 | char *_devnm = find_free_devnm(use_mdp); | |
325 | if (devnm == NULL) { | |
326 | pr_err("No avail md devices - aborting\n"); | |
327 | return -1; | |
328 | } | |
329 | strcpy(devnm, _devnm); | |
330 | } else { | |
331 | sprintf(devnm, "%s%d", use_mdp?"md_d":"md", num); | |
332 | if (mddev_busy(devnm)) { | |
333 | pr_err("%s is already in use.\n", | |
334 | dev); | |
335 | return -1; | |
336 | } | |
337 | } | |
338 | ||
339 | sprintf(devname, "/dev/%s", devnm); | |
340 | ||
5ac6db12 | 341 | if (dev && dev[0] == '/') |
9a40c327 N |
342 | strcpy(chosen, dev); |
343 | else if (cname[0] == 0) | |
69207ff6 | 344 | strcpy(chosen, devname); |
8d80900b | 345 | |
69207ff6 | 346 | /* We have a device number and name. |
11fb4c05 N |
347 | * If we cannot detect udev, we need to make |
348 | * devices and links ourselves. | |
69207ff6 | 349 | */ |
06d2ffc3 | 350 | if (!use_udev()) { |
69207ff6 N |
351 | /* Make sure 'devname' exists and 'chosen' is a symlink to it */ |
352 | if (lstat(devname, &stb) == 0) { | |
353 | /* Must be the correct device, else error */ | |
354 | if ((stb.st_mode&S_IFMT) != S_IFBLK || | |
4dd2df09 | 355 | stb.st_rdev != (dev_t)devnm2devid(devnm)) { |
e7b84f9d | 356 | pr_err("%s exists but looks wrong, please fix\n", |
69207ff6 N |
357 | devname); |
358 | return -1; | |
0a6e1c67 | 359 | } |
69207ff6 N |
360 | } else { |
361 | if (mknod(devname, S_IFBLK|0600, | |
4dd2df09 | 362 | devnm2devid(devnm)) != 0) { |
e7b84f9d | 363 | pr_err("failed to create %s\n", |
69207ff6 | 364 | devname); |
b5e64645 NB |
365 | return -1; |
366 | } | |
69207ff6 N |
367 | if (chown(devname, ci->uid, ci->gid)) |
368 | perror("chown"); | |
369 | if (chmod(devname, ci->mode)) | |
370 | perror("chmod"); | |
371 | stat(devname, &stb); | |
372 | add_dev(devname, &stb, 0, NULL); | |
373 | } | |
c4fe2d4f N |
374 | if (use_mdp == 1) |
375 | make_parts(devname, parts); | |
69207ff6 | 376 | |
ca3b6696 N |
377 | if (strcmp(chosen, devname) != 0) { |
378 | if (mkdir("/dev/md",0700) == 0) { | |
69207ff6 N |
379 | if (chown("/dev/md", ci->uid, ci->gid)) |
380 | perror("chown /dev/md"); | |
381 | if (chmod("/dev/md", ci->mode| ((ci->mode>>2) & 0111))) | |
382 | perror("chmod /dev/md"); | |
8d80900b | 383 | } |
69207ff6 N |
384 | |
385 | if (dev && strcmp(chosen, dev) == 0) | |
386 | /* We know we are allowed to use this name */ | |
387 | unlink(chosen); | |
388 | ||
389 | if (lstat(chosen, &stb) == 0) { | |
390 | char buf[300]; | |
fca13185 TJ |
391 | ssize_t link_len = readlink(chosen, buf, sizeof(buf)-1); |
392 | if (link_len >= 0) | |
393 | buf[link_len] = '\0'; | |
394 | ||
69207ff6 | 395 | if ((stb.st_mode & S_IFMT) != S_IFLNK || |
fca13185 | 396 | link_len < 0 || |
69207ff6 | 397 | strcmp(buf, devname) != 0) { |
e7b84f9d | 398 | pr_err("%s exists - ignoring\n", |
69207ff6 N |
399 | chosen); |
400 | strcpy(chosen, devname); | |
401 | } | |
614825ea | 402 | } else if (symlink(devname, chosen) != 0) |
e7b84f9d | 403 | pr_err("failed to create %s: %s\n", |
614825ea | 404 | chosen, strerror(errno)); |
c4fe2d4f N |
405 | if (use_mdp && strcmp(chosen, devname) != 0) |
406 | make_parts(chosen, parts); | |
b5e64645 | 407 | } |
b5e64645 | 408 | } |
4dd2df09 | 409 | mdfd = open_dev_excl(devnm); |
69207ff6 | 410 | if (mdfd < 0) |
e7b84f9d | 411 | pr_err("unexpected failure opening %s\n", |
69207ff6 | 412 | devname); |
6be1d39d N |
413 | return mdfd; |
414 | } | |
415 | ||
416 | /* Open this and check that it is an md device. | |
417 | * On success, return filedescriptor. | |
418 | * On failure, return -1 if it doesn't exist, | |
419 | * or -2 if it exists but is not an md device. | |
420 | */ | |
421 | int open_mddev(char *dev, int report_errors) | |
422 | { | |
423 | int mdfd = open(dev, O_RDWR); | |
c3f26510 N |
424 | if (mdfd < 0 && errno == EACCES) |
425 | mdfd = open(dev, O_RDONLY); | |
6be1d39d N |
426 | if (mdfd < 0) { |
427 | if (report_errors) | |
e7b84f9d | 428 | pr_err("error opening %s: %s\n", |
6be1d39d N |
429 | dev, strerror(errno)); |
430 | return -1; | |
431 | } | |
432 | if (md_get_version(mdfd) <= 0) { | |
b5e64645 | 433 | close(mdfd); |
6be1d39d | 434 | if (report_errors) |
e7b84f9d | 435 | pr_err("%s does not appear to be " |
6be1d39d N |
436 | "an md device\n", dev); |
437 | return -2; | |
b5e64645 NB |
438 | } |
439 | return mdfd; | |
440 | } |