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