]>
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]); |
c1b78589 | 47 | char orig[1001]; |
c4fe2d4f | 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)) { | |
c1b78589 JS |
61 | int len; |
62 | ||
63 | len = readlink(dev, orig, sizeof(orig)); | |
64 | if (len < 0 || len >= (int)sizeof(orig)) | |
c4fe2d4f N |
65 | return; |
66 | orig[len] = 0; | |
67 | odig = isdigit(orig[len-1]); | |
96ae5973 JS |
68 | major_num = -1; |
69 | minor_num = -1; | |
c4fe2d4f | 70 | } else |
96ae5973 | 71 | return; |
503975b9 | 72 | name = xmalloc(nlen); |
ca3b6696 | 73 | for (i = 1; i <= cnt ; i++) { |
b5e64645 | 74 | struct stat stb2; |
8f23b0b3 | 75 | snprintf(name, nlen, "%s%s%d", dev, dig?"p":"", i); |
ca3b6696 | 76 | if (stat(name, &stb2) == 0) { |
96ae5973 | 77 | if (!S_ISBLK(stb2.st_mode) || !S_ISBLK(stb.st_mode)) |
b5e64645 | 78 | continue; |
b440882e | 79 | if (stb2.st_rdev == makedev(major_num, minor_num+i)) |
b5e64645 NB |
80 | continue; |
81 | unlink(name); | |
82 | } else { | |
83 | stb2 = stb; | |
84 | } | |
c4fe2d4f N |
85 | if (S_ISBLK(stb.st_mode)) { |
86 | if (mknod(name, S_IFBLK | 0600, | |
87 | makedev(major_num, minor_num+i))) | |
88 | perror("mknod"); | |
89 | if (chown(name, stb2.st_uid, stb2.st_gid)) | |
90 | perror("chown"); | |
91 | if (chmod(name, stb2.st_mode & 07777)) | |
92 | perror("chmod"); | |
614825ea | 93 | err = 0; |
c4fe2d4f | 94 | } else { |
e10a79c3 | 95 | snprintf(sym, sizeof(sym), "%s%s%d", orig, odig?"p":"", i); |
614825ea | 96 | err = symlink(sym, name); |
c4fe2d4f | 97 | } |
614825ea DW |
98 | |
99 | if (err == 0 && stat(name, &stb2) == 0) | |
100 | add_dev(name, &stb2, 0, NULL); | |
b5e64645 | 101 | } |
0eb26465 | 102 | free(name); |
b5e64645 NB |
103 | } |
104 | ||
7105228e ZL |
105 | int create_named_array(char *devnm) |
106 | { | |
107 | int fd; | |
108 | int n = -1; | |
109 | static const char new_array_file[] = { | |
110 | "/sys/module/md_mod/parameters/new_array" | |
111 | }; | |
112 | ||
113 | fd = open(new_array_file, O_WRONLY); | |
114 | if (fd < 0 && errno == ENOENT) { | |
115 | if (system("modprobe md_mod") == 0) | |
116 | fd = open(new_array_file, O_WRONLY); | |
117 | } | |
118 | if (fd >= 0) { | |
119 | n = write(fd, devnm, strlen(devnm)); | |
120 | close(fd); | |
121 | } | |
122 | if (fd < 0 || n != (int)strlen(devnm)) { | |
1a874930 KT |
123 | pr_err("Fail to create %s when using %s, fallback to creation via node\n", |
124 | devnm, new_array_file); | |
7105228e ZL |
125 | return 0; |
126 | } | |
127 | ||
128 | return 1; | |
129 | } | |
130 | ||
b5e64645 | 131 | /* |
69207ff6 N |
132 | * We need a new md device to assemble/build/create an array. |
133 | * 'dev' is a name given us by the user (command line or mdadm.conf) | |
134 | * It might start with /dev or /dev/md any might end with a digit | |
135 | * string. | |
136 | * If it starts with just /dev, it must be /dev/mdX or /dev/md_dX | |
137 | * If it ends with a digit string, then it must be as above, or | |
138 | * 'trustworthy' must be 'METADATA' and the 'dev' must be | |
139 | * /dev/md/'name'NN or 'name'NN | |
140 | * If it doesn't end with a digit string, it must be /dev/md/'name' | |
141 | * or 'name' or must be NULL. | |
142 | * If the digit string is present, it gives the minor number to use | |
143 | * If not, we choose a high, unused minor number. | |
144 | * If the 'dev' is a standard name, it devices whether 'md' or 'mdp'. | |
145 | * else if the name is 'd[0-9]+' then we use mdp | |
146 | * else if trustworthy is 'METADATA' we use md | |
147 | * else the choice depends on 'autof'. | |
148 | * If name is NULL it is assumed to match whatever dev provides. | |
149 | * If both name and dev are NULL, we choose a name 'mdXX' or 'mdpXX' | |
150 | * | |
151 | * If 'name' is given, and 'trustworthy' is 'foreign' and name is not | |
152 | * supported by 'dev', we add a "_%d" suffix based on the minor number | |
153 | * use that. | |
154 | * | |
ca3b6696 | 155 | * If udev is configured, we create a temporary device, open it, and |
69207ff6 | 156 | * unlink it. |
eca944fa | 157 | * If not, we create the /dev/mdXX device, and if name is usable, |
69207ff6 N |
158 | * /dev/md/name |
159 | * In any case we return /dev/md/name or (if that isn't available) | |
160 | * /dev/mdXX in 'chosen'. | |
161 | * | |
162 | * When we create devices, we use uid/gid/umask from config file. | |
b5e64645 | 163 | */ |
69207ff6 N |
164 | |
165 | int create_mddev(char *dev, char *name, int autof, int trustworthy, | |
cd6cbb08 | 166 | char *chosen, int block_udev) |
b5e64645 NB |
167 | { |
168 | int mdfd; | |
169 | struct stat stb; | |
69207ff6 N |
170 | int num = -1; |
171 | int use_mdp = -1; | |
8aec876d | 172 | struct createinfo *ci = conf_get_create_info(); |
f1ae21c4 | 173 | int parts; |
69207ff6 | 174 | char *cname; |
bd1fd72e | 175 | char devname[37]; |
4dd2df09 | 176 | char devnm[32]; |
69207ff6 | 177 | char cbuf[400]; |
cd6cbb08 N |
178 | |
179 | if (!use_udev()) | |
180 | block_udev = 0; | |
181 | ||
69207ff6 N |
182 | if (chosen == NULL) |
183 | chosen = cbuf; | |
184 | ||
5bbb4842 NB |
185 | if (autof == 0) |
186 | autof = ci->autof; | |
187 | ||
f1ae21c4 NB |
188 | parts = autof >> 3; |
189 | autof &= 7; | |
190 | ||
69207ff6 N |
191 | strcpy(chosen, "/dev/md/"); |
192 | cname = chosen + strlen(chosen); | |
193 | ||
69207ff6 | 194 | if (dev) { |
69207ff6 N |
195 | if (strncmp(dev, "/dev/md/", 8) == 0) { |
196 | strcpy(cname, dev+8); | |
197 | } else if (strncmp(dev, "/dev/", 5) == 0) { | |
198 | char *e = dev + strlen(dev); | |
199 | while (e > dev && isdigit(e[-1])) | |
200 | e--; | |
201 | if (e[0]) | |
202 | num = strtoul(e, NULL, 10); | |
203 | strcpy(cname, dev+5); | |
204 | cname[e-(dev+5)] = 0; | |
205 | /* name *must* be mdXX or md_dXX in this context */ | |
206 | if (num < 0 || | |
207 | (strcmp(cname, "md") != 0 && strcmp(cname, "md_d") != 0)) { | |
7a862a02 | 208 | pr_err("%s is an invalid name for an md device. Try /dev/md/%s\n", |
69207ff6 | 209 | dev, dev+5); |
f1ae21c4 NB |
210 | return -1; |
211 | } | |
69207ff6 N |
212 | if (strcmp(cname, "md") == 0) |
213 | use_mdp = 0; | |
214 | else | |
215 | use_mdp = 1; | |
11fb4c05 N |
216 | /* recreate name: /dev/md/0 or /dev/md/d0 */ |
217 | sprintf(cname, "%s%d", use_mdp?"d":"", num); | |
69207ff6 N |
218 | } else |
219 | strcpy(cname, dev); | |
220 | ||
11fb4c05 N |
221 | /* 'cname' must not contain a slash, and may not be |
222 | * empty. | |
69207ff6 | 223 | */ |
11fb4c05 | 224 | if (strchr(cname, '/') != NULL) { |
7a862a02 | 225 | pr_err("%s is an invalid name for an md device.\n", dev); |
69207ff6 | 226 | return -1; |
f1ae21c4 | 227 | } |
11fb4c05 | 228 | if (cname[0] == 0) { |
8e5b52cd | 229 | pr_err("%s is an invalid name for an md device (empty!).\n", dev); |
69207ff6 | 230 | return -1; |
b5e64645 | 231 | } |
11fb4c05 N |
232 | if (num < 0) { |
233 | /* If cname is 'N' or 'dN', we get dev number | |
234 | * from there. | |
235 | */ | |
236 | char *sp = cname; | |
237 | char *ep; | |
238 | if (cname[0] == 'd') | |
239 | sp++; | |
4cda8682 JM |
240 | if (isdigit(sp[0])) |
241 | num = strtoul(sp, &ep, 10); | |
242 | else | |
243 | ep = sp; | |
11fb4c05 N |
244 | if (ep == sp || *ep || num < 0) |
245 | num = -1; | |
246 | else if (cname[0] == 'd') | |
247 | use_mdp = 1; | |
248 | else | |
249 | use_mdp = 0; | |
250 | } | |
69207ff6 | 251 | } |
f1ae21c4 | 252 | |
69207ff6 N |
253 | /* Now determine device number */ |
254 | /* named 'METADATA' cannot use 'mdp'. */ | |
255 | if (name && name[0] == 0) | |
256 | name = NULL; | |
257 | if (name && trustworthy == METADATA && use_mdp == 1) { | |
7a862a02 | 258 | pr_err("%s is not allowed for a %s container. Consider /dev/md%d.\n", dev, name, num); |
69207ff6 N |
259 | return -1; |
260 | } | |
261 | if (name && trustworthy == METADATA) | |
262 | use_mdp = 0; | |
263 | if (use_mdp == -1) { | |
264 | if (autof == 4 || autof == 6) | |
265 | use_mdp = 1; | |
f1ae21c4 | 266 | else |
69207ff6 N |
267 | use_mdp = 0; |
268 | } | |
269 | if (num < 0 && trustworthy == LOCAL && name) { | |
ca3b6696 | 270 | /* if name is numeric, possibly prefixed by |
d7ba0c55 | 271 | * 'md' or '/dev/md', use that for num |
bde2c6e2 | 272 | * if it is not already in use */ |
69207ff6 | 273 | char *ep; |
d7ba0c55 N |
274 | char *n2 = name; |
275 | if (strncmp(n2, "/dev/", 5) == 0) | |
276 | n2 += 5; | |
277 | if (strncmp(n2, "md", 2) == 0) | |
278 | n2 += 2; | |
279 | if (*n2 == '/') | |
280 | n2++; | |
281 | num = strtoul(n2, &ep, 10); | |
282 | if (ep == n2 || *ep) | |
69207ff6 | 283 | num = -1; |
4dd2df09 N |
284 | else { |
285 | sprintf(devnm, "md%s%d", use_mdp ? "_d":"", num); | |
286 | if (mddev_busy(devnm)) | |
287 | num = -1; | |
288 | } | |
69207ff6 N |
289 | } |
290 | ||
69207ff6 N |
291 | if (cname[0] == 0 && name) { |
292 | /* Need to find a name if we can | |
293 | * We don't completely trust 'name'. Truncate to | |
294 | * reasonable length and remove '/' | |
8d80900b | 295 | */ |
69207ff6 | 296 | char *cp; |
f2e55ecc N |
297 | struct map_ent *map = NULL; |
298 | int conflict = 1; | |
299 | int unum = 0; | |
300 | int cnlen; | |
69207ff6 N |
301 | strncpy(cname, name, 200); |
302 | cname[200] = 0; | |
7103b9b8 N |
303 | for (cp = cname; *cp ; cp++) |
304 | switch (*cp) { | |
305 | case '/': | |
306 | *cp = '-'; | |
307 | break; | |
308 | case ' ': | |
309 | case '\t': | |
310 | *cp = '_'; | |
311 | break; | |
312 | } | |
313 | ||
f2e55ecc N |
314 | if (trustworthy == LOCAL || |
315 | (trustworthy == FOREIGN && strchr(cname, ':') != NULL)) { | |
316 | /* Only need suffix if there is a conflict */ | |
317 | if (map_by_name(&map, cname) == NULL) | |
318 | conflict = 0; | |
319 | } | |
320 | cnlen = strlen(cname); | |
321 | while (conflict) { | |
add394f3 | 322 | if (trustworthy == METADATA && !isdigit(cname[cnlen-1])) |
f2e55ecc N |
323 | sprintf(cname+cnlen, "%d", unum); |
324 | else | |
ca3b6696 | 325 | /* add _%d to FOREIGN array that don't |
f2e55ecc N |
326 | * a 'host:' prefix |
327 | */ | |
328 | sprintf(cname+cnlen, "_%d", unum); | |
329 | unum++; | |
330 | if (map_by_name(&map, cname) == NULL) | |
331 | conflict = 0; | |
332 | } | |
69207ff6 | 333 | } |
9a40c327 | 334 | |
eca944fa N |
335 | devnm[0] = 0; |
336 | if (num < 0 && cname && ci->names) { | |
eca944fa | 337 | sprintf(devnm, "md_%s", cname); |
cd6cbb08 N |
338 | if (block_udev) |
339 | udev_block(devnm); | |
7105228e | 340 | if (!create_named_array(devnm)) { |
eca944fa | 341 | devnm[0] = 0; |
cd6cbb08 N |
342 | udev_unblock(); |
343 | } | |
eca944fa | 344 | } |
039df362 | 345 | if (num >= 0) { |
039df362 | 346 | sprintf(devnm, "md%d", num); |
cd6cbb08 N |
347 | if (block_udev) |
348 | udev_block(devnm); | |
7105228e | 349 | if (!create_named_array(devnm)) { |
039df362 | 350 | devnm[0] = 0; |
cd6cbb08 | 351 | udev_unblock(); |
eca944fa | 352 | } |
cd6cbb08 N |
353 | } |
354 | if (devnm[0] == 0) { | |
355 | if (num < 0) { | |
356 | /* need to choose a free number. */ | |
357 | char *_devnm = find_free_devnm(use_mdp); | |
358 | if (_devnm == NULL) { | |
359 | pr_err("No avail md devices - aborting\n"); | |
360 | return -1; | |
361 | } | |
362 | strcpy(devnm, _devnm); | |
363 | } else { | |
364 | sprintf(devnm, "%s%d", use_mdp?"md_d":"md", num); | |
365 | if (mddev_busy(devnm)) { | |
366 | pr_err("%s is already in use.\n", | |
367 | dev); | |
368 | return -1; | |
369 | } | |
eca944fa | 370 | } |
cd6cbb08 N |
371 | if (block_udev) |
372 | udev_block(devnm); | |
eca944fa N |
373 | } |
374 | ||
375 | sprintf(devname, "/dev/%s", devnm); | |
376 | ||
5ac6db12 | 377 | if (dev && dev[0] == '/') |
9a40c327 N |
378 | strcpy(chosen, dev); |
379 | else if (cname[0] == 0) | |
69207ff6 | 380 | strcpy(chosen, devname); |
8d80900b | 381 | |
69207ff6 | 382 | /* We have a device number and name. |
11fb4c05 N |
383 | * If we cannot detect udev, we need to make |
384 | * devices and links ourselves. | |
69207ff6 | 385 | */ |
06d2ffc3 | 386 | if (!use_udev()) { |
69207ff6 N |
387 | /* Make sure 'devname' exists and 'chosen' is a symlink to it */ |
388 | if (lstat(devname, &stb) == 0) { | |
389 | /* Must be the correct device, else error */ | |
390 | if ((stb.st_mode&S_IFMT) != S_IFBLK || | |
13db17bd | 391 | stb.st_rdev != devnm2devid(devnm)) { |
e7b84f9d | 392 | pr_err("%s exists but looks wrong, please fix\n", |
69207ff6 N |
393 | devname); |
394 | return -1; | |
0a6e1c67 | 395 | } |
69207ff6 N |
396 | } else { |
397 | if (mknod(devname, S_IFBLK|0600, | |
4dd2df09 | 398 | devnm2devid(devnm)) != 0) { |
e7b84f9d | 399 | pr_err("failed to create %s\n", |
69207ff6 | 400 | devname); |
b5e64645 NB |
401 | return -1; |
402 | } | |
69207ff6 N |
403 | if (chown(devname, ci->uid, ci->gid)) |
404 | perror("chown"); | |
405 | if (chmod(devname, ci->mode)) | |
406 | perror("chmod"); | |
407 | stat(devname, &stb); | |
408 | add_dev(devname, &stb, 0, NULL); | |
409 | } | |
c4fe2d4f N |
410 | if (use_mdp == 1) |
411 | make_parts(devname, parts); | |
69207ff6 | 412 | |
ca3b6696 N |
413 | if (strcmp(chosen, devname) != 0) { |
414 | if (mkdir("/dev/md",0700) == 0) { | |
69207ff6 N |
415 | if (chown("/dev/md", ci->uid, ci->gid)) |
416 | perror("chown /dev/md"); | |
417 | if (chmod("/dev/md", ci->mode| ((ci->mode>>2) & 0111))) | |
418 | perror("chmod /dev/md"); | |
8d80900b | 419 | } |
69207ff6 N |
420 | |
421 | if (dev && strcmp(chosen, dev) == 0) | |
422 | /* We know we are allowed to use this name */ | |
423 | unlink(chosen); | |
424 | ||
425 | if (lstat(chosen, &stb) == 0) { | |
426 | char buf[300]; | |
fca13185 TJ |
427 | ssize_t link_len = readlink(chosen, buf, sizeof(buf)-1); |
428 | if (link_len >= 0) | |
429 | buf[link_len] = '\0'; | |
430 | ||
69207ff6 | 431 | if ((stb.st_mode & S_IFMT) != S_IFLNK || |
fca13185 | 432 | link_len < 0 || |
69207ff6 | 433 | strcmp(buf, devname) != 0) { |
e7b84f9d | 434 | pr_err("%s exists - ignoring\n", |
69207ff6 N |
435 | chosen); |
436 | strcpy(chosen, devname); | |
437 | } | |
614825ea | 438 | } else if (symlink(devname, chosen) != 0) |
e7b84f9d | 439 | pr_err("failed to create %s: %s\n", |
614825ea | 440 | chosen, strerror(errno)); |
c4fe2d4f N |
441 | if (use_mdp && strcmp(chosen, devname) != 0) |
442 | make_parts(chosen, parts); | |
b5e64645 | 443 | } |
b5e64645 | 444 | } |
4dd2df09 | 445 | mdfd = open_dev_excl(devnm); |
69207ff6 | 446 | if (mdfd < 0) |
e7b84f9d | 447 | pr_err("unexpected failure opening %s\n", |
69207ff6 | 448 | devname); |
6be1d39d N |
449 | return mdfd; |
450 | } | |
451 | ||
452 | /* Open this and check that it is an md device. | |
453 | * On success, return filedescriptor. | |
454 | * On failure, return -1 if it doesn't exist, | |
455 | * or -2 if it exists but is not an md device. | |
456 | */ | |
457 | int open_mddev(char *dev, int report_errors) | |
458 | { | |
f71d2b8f | 459 | int mdfd = open(dev, O_RDONLY); |
40b054e1 | 460 | |
6be1d39d N |
461 | if (mdfd < 0) { |
462 | if (report_errors) | |
e7b84f9d | 463 | pr_err("error opening %s: %s\n", |
6be1d39d N |
464 | dev, strerror(errno)); |
465 | return -1; | |
466 | } | |
40b054e1 | 467 | |
9db2ab4e | 468 | if (md_array_valid(mdfd) == 0) { |
b5e64645 | 469 | close(mdfd); |
6be1d39d | 470 | if (report_errors) |
7a862a02 | 471 | pr_err("%s does not appear to be an md device\n", dev); |
6be1d39d | 472 | return -2; |
b5e64645 | 473 | } |
40b054e1 | 474 | |
b5e64645 NB |
475 | return mdfd; |
476 | } | |
289c74f8 N |
477 | |
478 | char *find_free_devnm(int use_partitions) | |
479 | { | |
480 | static char devnm[32]; | |
481 | int devnum; | |
482 | for (devnum = 127; devnum != 128; | |
2e466cce | 483 | devnum = devnum ? devnum-1 : (1<<9)-1) { |
289c74f8 N |
484 | |
485 | if (use_partitions) | |
486 | sprintf(devnm, "md_d%d", devnum); | |
487 | else | |
488 | sprintf(devnm, "md%d", devnum); | |
489 | if (mddev_busy(devnm)) | |
490 | continue; | |
491 | if (!conf_name_is_free(devnm)) | |
492 | continue; | |
493 | if (!use_udev()) { | |
494 | /* make sure it is new to /dev too, at least as a | |
495 | * non-standard */ | |
13db17bd | 496 | dev_t devid = devnm2devid(devnm); |
289c74f8 N |
497 | if (devid) { |
498 | char *dn = map_dev(major(devid), | |
499 | minor(devid), 0); | |
500 | if (dn && ! is_standard(dn, NULL)) | |
501 | continue; | |
502 | } | |
503 | } | |
504 | break; | |
505 | } | |
506 | if (devnum == 128) | |
507 | return NULL; | |
508 | return devnm; | |
509 | } |