]>
git.ipfire.org Git - thirdparty/mdadm.git/blob - mdopen.c
2 * mdadm - manage Linux "md" devices aka RAID arrays.
4 * Copyright (C) 2001-2009 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>
30 void make_parts(char *dev
, int cnt
)
32 /* make 'cnt' partition devices for 'dev'
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.
36 * If dev ends with a digit, we add "p%d" else "%d"
37 * If the name exists, we use it's owner/mode,
41 int major_num
= major_num
; /* quiet gcc -Os unitialized warning */
42 int minor_num
= minor_num
; /* quiet gcc -Os unitialized warning */
43 int odig
= odig
; /* quiet gcc -Os unitialized warning */
45 int nlen
= strlen(dev
) + 20;
46 char *name
= malloc(nlen
);
47 int dig
= isdigit(dev
[strlen(dev
)-1]);
53 if (lstat(dev
, &stb
)!= 0)
55 if (S_ISLNK(stb
.st_mode
)) {
56 int len
= readlink(dev
, orig
, sizeof(orig
));
57 if (len
< 0 || len
> 1000)
60 odig
= isdigit(orig
[len
-1]);
61 } else if (S_ISBLK(stb
.st_mode
)) {
62 major_num
= major(stb
.st_rdev
);
63 minor_num
= minor(stb
.st_rdev
);
66 for (i
=1; i
<= cnt
; i
++) {
68 snprintf(name
, nlen
, "%s%s%d", dev
, dig
?"p":"", i
);
69 if (stat(name
, &stb2
)==0) {
70 if (!S_ISBLK(stb2
.st_mode
))
72 if (stb2
.st_rdev
== makedev(major_num
, minor_num
+i
))
78 if (S_ISBLK(stb
.st_mode
)) {
79 if (mknod(name
, S_IFBLK
| 0600,
80 makedev(major_num
, minor_num
+i
)))
82 if (chown(name
, stb2
.st_uid
, stb2
.st_gid
))
84 if (chmod(name
, stb2
.st_mode
& 07777))
88 snprintf(sym
, sizeof(sym
), "%s%s%d", orig
, odig
?"p":"", i
);
89 err
= symlink(sym
, name
);
92 if (err
== 0 && stat(name
, &stb2
) == 0)
93 add_dev(name
, &stb2
, 0, NULL
);
99 * We need a new md device to assemble/build/create an array.
100 * 'dev' is a name given us by the user (command line or mdadm.conf)
101 * It might start with /dev or /dev/md any might end with a digit
103 * If it starts with just /dev, it must be /dev/mdX or /dev/md_dX
104 * If it ends with a digit string, then it must be as above, or
105 * 'trustworthy' must be 'METADATA' and the 'dev' must be
106 * /dev/md/'name'NN or 'name'NN
107 * If it doesn't end with a digit string, it must be /dev/md/'name'
108 * or 'name' or must be NULL.
109 * If the digit string is present, it gives the minor number to use
110 * If not, we choose a high, unused minor number.
111 * If the 'dev' is a standard name, it devices whether 'md' or 'mdp'.
112 * else if the name is 'd[0-9]+' then we use mdp
113 * else if trustworthy is 'METADATA' we use md
114 * else the choice depends on 'autof'.
115 * If name is NULL it is assumed to match whatever dev provides.
116 * If both name and dev are NULL, we choose a name 'mdXX' or 'mdpXX'
118 * If 'name' is given, and 'trustworthy' is 'foreign' and name is not
119 * supported by 'dev', we add a "_%d" suffix based on the minor number
122 * If udev is configured, we create a temporary device, open it, and
124 * If not, we create the /dev/mdXX device, and is name is usable,
126 * In any case we return /dev/md/name or (if that isn't available)
127 * /dev/mdXX in 'chosen'.
129 * When we create devices, we use uid/gid/umask from config file.
132 int create_mddev(char *dev
, char *name
, int autof
, int trustworthy
,
139 struct createinfo
*ci
= conf_get_create_info();
154 strcpy(chosen
, "/dev/md/");
155 cname
= chosen
+ strlen(chosen
);
159 if (strncmp(dev
, "/dev/md/", 8) == 0) {
160 strcpy(cname
, dev
+8);
161 } else if (strncmp(dev
, "/dev/", 5) == 0) {
162 char *e
= dev
+ strlen(dev
);
163 while (e
> dev
&& isdigit(e
[-1]))
166 num
= strtoul(e
, NULL
, 10);
167 strcpy(cname
, dev
+5);
168 cname
[e
-(dev
+5)] = 0;
169 /* name *must* be mdXX or md_dXX in this context */
171 (strcmp(cname
, "md") != 0 && strcmp(cname
, "md_d") != 0)) {
172 fprintf(stderr
, Name
": %s is an invalid name "
173 "for an md device. Try /dev/md/%s\n",
177 if (strcmp(cname
, "md") == 0)
181 /* recreate name: /dev/md/0 or /dev/md/d0 */
182 sprintf(cname
, "%s%d", use_mdp
?"d":"", num
);
186 /* 'cname' must not contain a slash, and may not be
189 if (strchr(cname
, '/') != NULL
) {
190 fprintf(stderr
, Name
": %s is an invalid name "
191 "for an md device.\n", dev
);
195 fprintf(stderr
, Name
": %s is an invalid name "
196 "for an md device (empty!).", dev
);
200 /* If cname is 'N' or 'dN', we get dev number
207 num
= strtoul(sp
, &ep
, 10);
208 if (ep
== sp
|| *ep
|| num
< 0)
210 else if (cname
[0] == 'd')
217 /* Now determine device number */
218 /* named 'METADATA' cannot use 'mdp'. */
219 if (name
&& name
[0] == 0)
221 if (name
&& trustworthy
== METADATA
&& use_mdp
== 1) {
222 fprintf(stderr
, Name
": %s is not allowed for a %s container. "
223 "Consider /dev/md%d.\n", dev
, name
, num
);
226 if (name
&& trustworthy
== METADATA
)
229 if (autof
== 4 || autof
== 6)
234 if (num
< 0 && trustworthy
== LOCAL
&& name
) {
235 /* if name is numeric, possibly prefixed by
236 * 'md' or '/dev/md', use that for num
237 * if it is not already in use */
240 if (strncmp(n2
, "/dev/", 5) == 0)
242 if (strncmp(n2
, "md", 2) == 0)
246 num
= strtoul(n2
, &ep
, 10);
249 else if (mddev_busy(use_mdp
? (-1-num
) : num
))
254 /* need to choose a free number. */
255 num
= find_free_devnum(use_mdp
);
256 if (num
== NoMdDev
) {
257 fprintf(stderr
, Name
": No avail md devices - aborting\n");
261 num
= use_mdp
? (-1-num
) : num
;
262 if (mddev_busy(num
)) {
263 fprintf(stderr
, Name
": %s is already in use.\n",
270 sprintf(devname
, "/dev/md_d%d", -1-num
);
272 sprintf(devname
, "/dev/md%d", num
);
274 if (cname
[0] == 0 && name
) {
275 /* Need to find a name if we can
276 * We don't completely trust 'name'. Truncate to
277 * reasonable length and remove '/'
280 struct map_ent
*map
= NULL
;
284 strncpy(cname
, name
, 200);
286 while ((cp
= strchr(cname
, '/')) != NULL
)
288 if (trustworthy
== LOCAL
||
289 (trustworthy
== FOREIGN
&& strchr(cname
, ':') != NULL
)) {
290 /* Only need suffix if there is a conflict */
291 if (map_by_name(&map
, cname
) == NULL
)
294 cnlen
= strlen(cname
);
296 if (trustworthy
== METADATA
&& !isdigit(cname
[cnlen
-1]))
297 sprintf(cname
+cnlen
, "%d", unum
);
299 /* add _%d to FOREIGN array that don't
302 sprintf(cname
+cnlen
, "_%d", unum
);
304 if (map_by_name(&map
, cname
) == NULL
)
309 if (dev
&& dev
[0] == '/')
311 else if (cname
[0] == 0)
312 strcpy(chosen
, devname
);
314 /* We have a device number and name.
315 * If we cannot detect udev, we need to make
316 * devices and links ourselves.
318 if (stat("/dev/.udev", &stb
) != 0 ||
319 check_env("MDADM_NO_UDEV")) {
320 /* Make sure 'devname' exists and 'chosen' is a symlink to it */
321 if (lstat(devname
, &stb
) == 0) {
322 /* Must be the correct device, else error */
323 if ((stb
.st_mode
&S_IFMT
) != S_IFBLK
||
324 stb
.st_rdev
!= makedev(dev2major(num
),dev2minor(num
))) {
325 fprintf(stderr
, Name
": %s exists but looks wrong, please fix\n",
330 if (mknod(devname
, S_IFBLK
|0600,
331 makedev(dev2major(num
),dev2minor(num
))) != 0) {
332 fprintf(stderr
, Name
": failed to create %s\n",
336 if (chown(devname
, ci
->uid
, ci
->gid
))
338 if (chmod(devname
, ci
->mode
))
341 add_dev(devname
, &stb
, 0, NULL
);
344 make_parts(devname
, parts
);
345 if (strcmp(chosen
, devname
) != 0) {
347 if (mkdir("/dev/md",0700)==0) {
348 if (chown("/dev/md", ci
->uid
, ci
->gid
))
349 perror("chown /dev/md");
350 if (chmod("/dev/md", ci
->mode
| ((ci
->mode
>>2) & 0111)))
351 perror("chmod /dev/md");
354 if (dev
&& strcmp(chosen
, dev
) == 0)
355 /* We know we are allowed to use this name */
358 if (lstat(chosen
, &stb
) == 0) {
360 if ((stb
.st_mode
& S_IFMT
) != S_IFLNK
||
361 readlink(chosen
, buf
, 300) <0 ||
362 strcmp(buf
, devname
) != 0) {
363 fprintf(stderr
, Name
": %s exists - ignoring\n",
365 strcpy(chosen
, devname
);
367 } else if (symlink(devname
, chosen
) != 0)
368 fprintf(stderr
, Name
": failed to create %s: %s\n",
369 chosen
, strerror(errno
));
370 if (use_mdp
&& strcmp(chosen
, devname
) != 0)
371 make_parts(chosen
, parts
);
374 mdfd
= open_dev_excl(num
);
376 fprintf(stderr
, Name
": unexpected failure opening %s\n",
382 /* Open this and check that it is an md device.
383 * On success, return filedescriptor.
384 * On failure, return -1 if it doesn't exist,
385 * or -2 if it exists but is not an md device.
387 int open_mddev(char *dev
, int report_errors
)
389 int mdfd
= open(dev
, O_RDWR
);
392 fprintf(stderr
, Name
": error opening %s: %s\n",
393 dev
, strerror(errno
));
396 if (md_get_version(mdfd
) <= 0) {
399 fprintf(stderr
, Name
": %s does not appear to be "
400 "an md device\n", dev
);