]>
Commit | Line | Data |
---|---|---|
b5e64645 NB |
1 | /* |
2 | * mdadm - manage Linux "md" devices aka RAID arrays. | |
3 | * | |
6f02172d | 4 | * Copyright (C) 2001-2013 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, | |
cd6cbb08 | 138 | char *chosen, int block_udev) |
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 | 146 | char *cname; |
bd1fd72e | 147 | char devname[37]; |
4dd2df09 | 148 | char devnm[32]; |
69207ff6 | 149 | char cbuf[400]; |
cd6cbb08 N |
150 | |
151 | if (!use_udev()) | |
152 | block_udev = 0; | |
153 | ||
69207ff6 N |
154 | if (chosen == NULL) |
155 | chosen = cbuf; | |
156 | ||
5bbb4842 NB |
157 | if (autof == 0) |
158 | autof = ci->autof; | |
159 | ||
f1ae21c4 NB |
160 | parts = autof >> 3; |
161 | autof &= 7; | |
162 | ||
69207ff6 N |
163 | strcpy(chosen, "/dev/md/"); |
164 | cname = chosen + strlen(chosen); | |
165 | ||
69207ff6 | 166 | if (dev) { |
69207ff6 N |
167 | if (strncmp(dev, "/dev/md/", 8) == 0) { |
168 | strcpy(cname, dev+8); | |
169 | } else if (strncmp(dev, "/dev/", 5) == 0) { | |
170 | char *e = dev + strlen(dev); | |
171 | while (e > dev && isdigit(e[-1])) | |
172 | e--; | |
173 | if (e[0]) | |
174 | num = strtoul(e, NULL, 10); | |
175 | strcpy(cname, dev+5); | |
176 | cname[e-(dev+5)] = 0; | |
177 | /* name *must* be mdXX or md_dXX in this context */ | |
178 | if (num < 0 || | |
179 | (strcmp(cname, "md") != 0 && strcmp(cname, "md_d") != 0)) { | |
7a862a02 | 180 | pr_err("%s is an invalid name for an md device. Try /dev/md/%s\n", |
69207ff6 | 181 | dev, dev+5); |
f1ae21c4 NB |
182 | return -1; |
183 | } | |
69207ff6 N |
184 | if (strcmp(cname, "md") == 0) |
185 | use_mdp = 0; | |
186 | else | |
187 | use_mdp = 1; | |
11fb4c05 N |
188 | /* recreate name: /dev/md/0 or /dev/md/d0 */ |
189 | sprintf(cname, "%s%d", use_mdp?"d":"", num); | |
69207ff6 N |
190 | } else |
191 | strcpy(cname, dev); | |
192 | ||
11fb4c05 N |
193 | /* 'cname' must not contain a slash, and may not be |
194 | * empty. | |
69207ff6 | 195 | */ |
11fb4c05 | 196 | if (strchr(cname, '/') != NULL) { |
7a862a02 | 197 | pr_err("%s is an invalid name for an md device.\n", dev); |
69207ff6 | 198 | return -1; |
f1ae21c4 | 199 | } |
11fb4c05 | 200 | if (cname[0] == 0) { |
8e5b52cd | 201 | pr_err("%s is an invalid name for an md device (empty!).\n", dev); |
69207ff6 | 202 | return -1; |
b5e64645 | 203 | } |
11fb4c05 N |
204 | if (num < 0) { |
205 | /* If cname is 'N' or 'dN', we get dev number | |
206 | * from there. | |
207 | */ | |
208 | char *sp = cname; | |
209 | char *ep; | |
210 | if (cname[0] == 'd') | |
211 | sp++; | |
4cda8682 JM |
212 | if (isdigit(sp[0])) |
213 | num = strtoul(sp, &ep, 10); | |
214 | else | |
215 | ep = sp; | |
11fb4c05 N |
216 | if (ep == sp || *ep || num < 0) |
217 | num = -1; | |
218 | else if (cname[0] == 'd') | |
219 | use_mdp = 1; | |
220 | else | |
221 | use_mdp = 0; | |
222 | } | |
69207ff6 | 223 | } |
f1ae21c4 | 224 | |
69207ff6 N |
225 | /* Now determine device number */ |
226 | /* named 'METADATA' cannot use 'mdp'. */ | |
227 | if (name && name[0] == 0) | |
228 | name = NULL; | |
229 | if (name && trustworthy == METADATA && use_mdp == 1) { | |
7a862a02 | 230 | pr_err("%s is not allowed for a %s container. Consider /dev/md%d.\n", dev, name, num); |
69207ff6 N |
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); | |
cd6cbb08 N |
312 | if (block_udev) |
313 | udev_block(devnm); | |
eca944fa | 314 | fd = open("/sys/module/md_mod/parameters/new_array", O_WRONLY); |
fdbf7aaa N |
315 | if (fd < 0 && errno == ENOENT) { |
316 | system("modprobe md_mod"); | |
317 | fd = open("/sys/module/md_mod/parameters/new_array", O_WRONLY); | |
318 | } | |
eca944fa N |
319 | if (fd >= 0) { |
320 | n = write(fd, devnm, strlen(devnm)); | |
321 | close(fd); | |
322 | } | |
cd6cbb08 | 323 | if (n < 0) { |
eca944fa | 324 | devnm[0] = 0; |
cd6cbb08 N |
325 | udev_unblock(); |
326 | } | |
eca944fa | 327 | } |
039df362 N |
328 | if (num >= 0) { |
329 | int fd; | |
330 | int n = -1; | |
331 | sprintf(devnm, "md%d", num); | |
cd6cbb08 N |
332 | if (block_udev) |
333 | udev_block(devnm); | |
039df362 N |
334 | fd = open("/sys/module/md_mod/parameters/new_array", O_WRONLY); |
335 | if (fd >= 0) { | |
336 | n = write(fd, devnm, strlen(devnm)); | |
337 | close(fd); | |
338 | } | |
cd6cbb08 | 339 | if (n < 0) { |
039df362 | 340 | devnm[0] = 0; |
cd6cbb08 | 341 | udev_unblock(); |
eca944fa | 342 | } |
cd6cbb08 N |
343 | } |
344 | if (devnm[0] == 0) { | |
345 | if (num < 0) { | |
346 | /* need to choose a free number. */ | |
347 | char *_devnm = find_free_devnm(use_mdp); | |
348 | if (_devnm == NULL) { | |
349 | pr_err("No avail md devices - aborting\n"); | |
350 | return -1; | |
351 | } | |
352 | strcpy(devnm, _devnm); | |
353 | } else { | |
354 | sprintf(devnm, "%s%d", use_mdp?"md_d":"md", num); | |
355 | if (mddev_busy(devnm)) { | |
356 | pr_err("%s is already in use.\n", | |
357 | dev); | |
358 | return -1; | |
359 | } | |
eca944fa | 360 | } |
cd6cbb08 N |
361 | if (block_udev) |
362 | udev_block(devnm); | |
eca944fa N |
363 | } |
364 | ||
365 | sprintf(devname, "/dev/%s", devnm); | |
366 | ||
5ac6db12 | 367 | if (dev && dev[0] == '/') |
9a40c327 N |
368 | strcpy(chosen, dev); |
369 | else if (cname[0] == 0) | |
69207ff6 | 370 | strcpy(chosen, devname); |
8d80900b | 371 | |
69207ff6 | 372 | /* We have a device number and name. |
11fb4c05 N |
373 | * If we cannot detect udev, we need to make |
374 | * devices and links ourselves. | |
69207ff6 | 375 | */ |
06d2ffc3 | 376 | if (!use_udev()) { |
69207ff6 N |
377 | /* Make sure 'devname' exists and 'chosen' is a symlink to it */ |
378 | if (lstat(devname, &stb) == 0) { | |
379 | /* Must be the correct device, else error */ | |
380 | if ((stb.st_mode&S_IFMT) != S_IFBLK || | |
13db17bd | 381 | stb.st_rdev != devnm2devid(devnm)) { |
e7b84f9d | 382 | pr_err("%s exists but looks wrong, please fix\n", |
69207ff6 N |
383 | devname); |
384 | return -1; | |
0a6e1c67 | 385 | } |
69207ff6 N |
386 | } else { |
387 | if (mknod(devname, S_IFBLK|0600, | |
4dd2df09 | 388 | devnm2devid(devnm)) != 0) { |
e7b84f9d | 389 | pr_err("failed to create %s\n", |
69207ff6 | 390 | devname); |
b5e64645 NB |
391 | return -1; |
392 | } | |
69207ff6 N |
393 | if (chown(devname, ci->uid, ci->gid)) |
394 | perror("chown"); | |
395 | if (chmod(devname, ci->mode)) | |
396 | perror("chmod"); | |
397 | stat(devname, &stb); | |
398 | add_dev(devname, &stb, 0, NULL); | |
399 | } | |
c4fe2d4f N |
400 | if (use_mdp == 1) |
401 | make_parts(devname, parts); | |
69207ff6 | 402 | |
ca3b6696 N |
403 | if (strcmp(chosen, devname) != 0) { |
404 | if (mkdir("/dev/md",0700) == 0) { | |
69207ff6 N |
405 | if (chown("/dev/md", ci->uid, ci->gid)) |
406 | perror("chown /dev/md"); | |
407 | if (chmod("/dev/md", ci->mode| ((ci->mode>>2) & 0111))) | |
408 | perror("chmod /dev/md"); | |
8d80900b | 409 | } |
69207ff6 N |
410 | |
411 | if (dev && strcmp(chosen, dev) == 0) | |
412 | /* We know we are allowed to use this name */ | |
413 | unlink(chosen); | |
414 | ||
415 | if (lstat(chosen, &stb) == 0) { | |
416 | char buf[300]; | |
fca13185 TJ |
417 | ssize_t link_len = readlink(chosen, buf, sizeof(buf)-1); |
418 | if (link_len >= 0) | |
419 | buf[link_len] = '\0'; | |
420 | ||
69207ff6 | 421 | if ((stb.st_mode & S_IFMT) != S_IFLNK || |
fca13185 | 422 | link_len < 0 || |
69207ff6 | 423 | strcmp(buf, devname) != 0) { |
e7b84f9d | 424 | pr_err("%s exists - ignoring\n", |
69207ff6 N |
425 | chosen); |
426 | strcpy(chosen, devname); | |
427 | } | |
614825ea | 428 | } else if (symlink(devname, chosen) != 0) |
e7b84f9d | 429 | pr_err("failed to create %s: %s\n", |
614825ea | 430 | chosen, strerror(errno)); |
c4fe2d4f N |
431 | if (use_mdp && strcmp(chosen, devname) != 0) |
432 | make_parts(chosen, parts); | |
b5e64645 | 433 | } |
b5e64645 | 434 | } |
4dd2df09 | 435 | mdfd = open_dev_excl(devnm); |
69207ff6 | 436 | if (mdfd < 0) |
e7b84f9d | 437 | pr_err("unexpected failure opening %s\n", |
69207ff6 | 438 | devname); |
6be1d39d N |
439 | return mdfd; |
440 | } | |
441 | ||
442 | /* Open this and check that it is an md device. | |
443 | * On success, return filedescriptor. | |
444 | * On failure, return -1 if it doesn't exist, | |
445 | * or -2 if it exists but is not an md device. | |
446 | */ | |
447 | int open_mddev(char *dev, int report_errors) | |
448 | { | |
f71d2b8f | 449 | int mdfd = open(dev, O_RDONLY); |
40b054e1 | 450 | |
6be1d39d N |
451 | if (mdfd < 0) { |
452 | if (report_errors) | |
e7b84f9d | 453 | pr_err("error opening %s: %s\n", |
6be1d39d N |
454 | dev, strerror(errno)); |
455 | return -1; | |
456 | } | |
40b054e1 | 457 | |
9db2ab4e | 458 | if (md_array_valid(mdfd) == 0) { |
b5e64645 | 459 | close(mdfd); |
6be1d39d | 460 | if (report_errors) |
7a862a02 | 461 | pr_err("%s does not appear to be an md device\n", dev); |
6be1d39d | 462 | return -2; |
b5e64645 | 463 | } |
40b054e1 | 464 | |
b5e64645 NB |
465 | return mdfd; |
466 | } | |
289c74f8 N |
467 | |
468 | char *find_free_devnm(int use_partitions) | |
469 | { | |
470 | static char devnm[32]; | |
471 | int devnum; | |
472 | for (devnum = 127; devnum != 128; | |
2e466cce | 473 | devnum = devnum ? devnum-1 : (1<<9)-1) { |
289c74f8 N |
474 | |
475 | if (use_partitions) | |
476 | sprintf(devnm, "md_d%d", devnum); | |
477 | else | |
478 | sprintf(devnm, "md%d", devnum); | |
479 | if (mddev_busy(devnm)) | |
480 | continue; | |
481 | if (!conf_name_is_free(devnm)) | |
482 | continue; | |
483 | if (!use_udev()) { | |
484 | /* make sure it is new to /dev too, at least as a | |
485 | * non-standard */ | |
13db17bd | 486 | dev_t devid = devnm2devid(devnm); |
289c74f8 N |
487 | if (devid) { |
488 | char *dn = map_dev(major(devid), | |
489 | minor(devid), 0); | |
490 | if (dn && ! is_standard(dn, NULL)) | |
491 | continue; | |
492 | } | |
493 | } | |
494 | break; | |
495 | } | |
496 | if (devnum == 128) | |
497 | return NULL; | |
498 | return devnm; | |
499 | } |