]>
git.ipfire.org Git - thirdparty/mdadm.git/blob - mdopen.c
2 * mdadm - manage Linux "md" devices aka RAID arrays.
4 * Copyright (C) 2001-2013 Neil Brown <neilb@suse.de>
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.
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.
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
22 * Email: <neilb@suse.de>
29 void make_parts(char *dev
, int cnt
)
31 /* make 'cnt' partition devices for 'dev'
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.
35 * If dev ends with a digit, we add "p%d" else "%d"
36 * If the name exists, we use it's owner/mode,
44 int nlen
= strlen(dev
) + 20;
46 int dig
= isdigit(dev
[strlen(dev
)-1]);
53 if (lstat(dev
, &stb
)!= 0)
56 if (S_ISBLK(stb
.st_mode
)) {
57 major_num
= major(stb
.st_rdev
);
58 minor_num
= minor(stb
.st_rdev
);
60 } else if (S_ISLNK(stb
.st_mode
)) {
63 len
= readlink(dev
, orig
, sizeof(orig
));
64 if (len
< 0 || len
>= (int)sizeof(orig
))
67 odig
= isdigit(orig
[len
-1]);
73 for (i
= 1; i
<= cnt
; i
++) {
75 snprintf(name
, nlen
, "%s%s%d", dev
, dig
?"p":"", i
);
76 if (stat(name
, &stb2
) == 0) {
77 if (!S_ISBLK(stb2
.st_mode
) || !S_ISBLK(stb
.st_mode
))
79 if (stb2
.st_rdev
== makedev(major_num
, minor_num
+i
))
85 if (S_ISBLK(stb
.st_mode
)) {
86 if (mknod(name
, S_IFBLK
| 0600,
87 makedev(major_num
, minor_num
+i
)))
89 if (chown(name
, stb2
.st_uid
, stb2
.st_gid
))
91 if (chmod(name
, stb2
.st_mode
& 07777))
95 snprintf(sym
, sizeof(sym
), "%s%s%d", orig
, odig
?"p":"", i
);
96 err
= symlink(sym
, name
);
99 if (err
== 0 && stat(name
, &stb2
) == 0)
100 add_dev(name
, &stb2
, 0, NULL
);
105 int create_named_array(char *devnm
)
109 static const char new_array_file
[] = {
110 "/sys/module/md_mod/parameters/new_array"
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
);
119 n
= write(fd
, devnm
, strlen(devnm
));
122 if (fd
< 0 || n
!= (int)strlen(devnm
)) {
123 pr_err("Fail to create %s when using %s, fallback to creation via node\n",
124 devnm
, new_array_file
);
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
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'
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
155 * If udev is configured, we create a temporary device, open it, and
157 * If not, we create the /dev/mdXX device, and if name is usable,
159 * In any case we return /dev/md/name or (if that isn't available)
160 * /dev/mdXX in 'chosen'.
162 * When we create devices, we use uid/gid/umask from config file.
165 int create_mddev(char *dev
, char *name
, int autof
, int trustworthy
,
166 char *chosen
, int block_udev
)
172 struct createinfo
*ci
= conf_get_create_info();
191 strcpy(chosen
, "/dev/md/");
192 cname
= chosen
+ strlen(chosen
);
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]))
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 */
207 (strcmp(cname
, "md") != 0 && strcmp(cname
, "md_d") != 0)) {
208 pr_err("%s is an invalid name for an md device. Try /dev/md/%s\n",
212 if (strcmp(cname
, "md") == 0)
216 /* recreate name: /dev/md/0 or /dev/md/d0 */
217 sprintf(cname
, "%s%d", use_mdp
?"d":"", num
);
221 /* 'cname' must not contain a slash, and may not be
224 if (strchr(cname
, '/') != NULL
) {
225 pr_err("%s is an invalid name for an md device.\n", dev
);
229 pr_err("%s is an invalid name for an md device (empty!).\n", dev
);
233 /* If cname is 'N' or 'dN', we get dev number
241 num
= strtoul(sp
, &ep
, 10);
244 if (ep
== sp
|| *ep
|| num
< 0)
246 else if (cname
[0] == 'd')
253 /* Now determine device number */
254 /* named 'METADATA' cannot use 'mdp'. */
255 if (name
&& name
[0] == 0)
257 if (name
&& trustworthy
== METADATA
&& use_mdp
== 1) {
258 pr_err("%s is not allowed for a %s container. Consider /dev/md%d.\n", dev
, name
, num
);
261 if (name
&& trustworthy
== METADATA
)
264 if (autof
== 4 || autof
== 6)
269 if (num
< 0 && trustworthy
== LOCAL
&& name
) {
270 /* if name is numeric, possibly prefixed by
271 * 'md' or '/dev/md', use that for num
272 * if it is not already in use */
275 if (strncmp(n2
, "/dev/", 5) == 0)
277 if (strncmp(n2
, "md", 2) == 0)
281 num
= strtoul(n2
, &ep
, 10);
285 sprintf(devnm
, "md%s%d", use_mdp
? "_d":"", num
);
286 if (mddev_busy(devnm
))
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 '/'
297 struct map_ent
*map
= NULL
;
301 strncpy(cname
, name
, 200);
303 for (cp
= cname
; *cp
; cp
++)
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
)
320 cnlen
= strlen(cname
);
322 if (trustworthy
== METADATA
&& !isdigit(cname
[cnlen
-1]))
323 sprintf(cname
+cnlen
, "%d", unum
);
325 /* add _%d to FOREIGN array that don't
328 sprintf(cname
+cnlen
, "_%d", unum
);
330 if (map_by_name(&map
, cname
) == NULL
)
336 if (num
< 0 && cname
&& ci
->names
) {
337 sprintf(devnm
, "md_%s", cname
);
340 if (!create_named_array(devnm
)) {
346 sprintf(devnm
, "md%d", num
);
349 if (!create_named_array(devnm
)) {
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");
362 strcpy(devnm
, _devnm
);
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",
375 sprintf(devname
, "/dev/%s", devnm
);
377 if (dev
&& dev
[0] == '/')
379 else if (cname
[0] == 0)
380 strcpy(chosen
, devname
);
382 /* We have a device number and name.
383 * If we cannot detect udev, we need to make
384 * devices and links ourselves.
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
||
391 stb
.st_rdev
!= devnm2devid(devnm
)) {
392 pr_err("%s exists but looks wrong, please fix\n",
397 if (mknod(devname
, S_IFBLK
|0600,
398 devnm2devid(devnm
)) != 0) {
399 pr_err("failed to create %s\n",
403 if (chown(devname
, ci
->uid
, ci
->gid
))
405 if (chmod(devname
, ci
->mode
))
408 add_dev(devname
, &stb
, 0, NULL
);
411 make_parts(devname
, parts
);
413 if (strcmp(chosen
, devname
) != 0) {
414 if (mkdir("/dev/md",0700) == 0) {
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");
421 if (dev
&& strcmp(chosen
, dev
) == 0)
422 /* We know we are allowed to use this name */
425 if (lstat(chosen
, &stb
) == 0) {
427 ssize_t link_len
= readlink(chosen
, buf
, sizeof(buf
)-1);
429 buf
[link_len
] = '\0';
431 if ((stb
.st_mode
& S_IFMT
) != S_IFLNK
||
433 strcmp(buf
, devname
) != 0) {
434 pr_err("%s exists - ignoring\n",
436 strcpy(chosen
, devname
);
438 } else if (symlink(devname
, chosen
) != 0)
439 pr_err("failed to create %s: %s\n",
440 chosen
, strerror(errno
));
441 if (use_mdp
&& strcmp(chosen
, devname
) != 0)
442 make_parts(chosen
, parts
);
445 mdfd
= open_dev_excl(devnm
);
447 pr_err("unexpected failure opening %s\n",
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.
457 int open_mddev(char *dev
, int report_errors
)
459 int mdfd
= open(dev
, O_RDONLY
);
463 pr_err("error opening %s: %s\n",
464 dev
, strerror(errno
));
468 if (md_array_valid(mdfd
) == 0) {
471 pr_err("%s does not appear to be an md device\n", dev
);
478 char *find_free_devnm(int use_partitions
)
480 static char devnm
[32];
482 for (devnum
= 127; devnum
!= 128;
483 devnum
= devnum
? devnum
-1 : (1<<9)-1) {
486 sprintf(devnm
, "md_d%d", devnum
);
488 sprintf(devnm
, "md%d", devnum
);
489 if (mddev_busy(devnm
))
491 if (!conf_name_is_free(devnm
))
494 /* make sure it is new to /dev too, at least as a
496 dev_t devid
= devnm2devid(devnm
);
498 char *dn
= map_dev(major(devid
),
500 if (dn
&& ! is_standard(dn
, NULL
))