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