]>
git.ipfire.org Git - thirdparty/mdadm.git/blob - mdopen.c
2 * mdadm - manage Linux "md" devices aka RAID arrays.
4 * Copyright (C) 2001-2012 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
)) {
61 int len
= readlink(dev
, orig
, sizeof(orig
));
62 if (len
< 0 || len
> 1000)
65 odig
= isdigit(orig
[len
-1]);
71 for (i
= 1; i
<= cnt
; i
++) {
73 snprintf(name
, nlen
, "%s%s%d", dev
, dig
?"p":"", i
);
74 if (stat(name
, &stb2
) == 0) {
75 if (!S_ISBLK(stb2
.st_mode
) || !S_ISBLK(stb
.st_mode
))
77 if (stb2
.st_rdev
== makedev(major_num
, minor_num
+i
))
83 if (S_ISBLK(stb
.st_mode
)) {
84 if (mknod(name
, S_IFBLK
| 0600,
85 makedev(major_num
, minor_num
+i
)))
87 if (chown(name
, stb2
.st_uid
, stb2
.st_gid
))
89 if (chmod(name
, stb2
.st_mode
& 07777))
93 snprintf(sym
, sizeof(sym
), "%s%s%d", orig
, odig
?"p":"", i
);
94 err
= symlink(sym
, name
);
97 if (err
== 0 && stat(name
, &stb2
) == 0)
98 add_dev(name
, &stb2
, 0, NULL
);
104 * We need a new md device to assemble/build/create an array.
105 * 'dev' is a name given us by the user (command line or mdadm.conf)
106 * It might start with /dev or /dev/md any might end with a digit
108 * If it starts with just /dev, it must be /dev/mdX or /dev/md_dX
109 * If it ends with a digit string, then it must be as above, or
110 * 'trustworthy' must be 'METADATA' and the 'dev' must be
111 * /dev/md/'name'NN or 'name'NN
112 * If it doesn't end with a digit string, it must be /dev/md/'name'
113 * or 'name' or must be NULL.
114 * If the digit string is present, it gives the minor number to use
115 * If not, we choose a high, unused minor number.
116 * If the 'dev' is a standard name, it devices whether 'md' or 'mdp'.
117 * else if the name is 'd[0-9]+' then we use mdp
118 * else if trustworthy is 'METADATA' we use md
119 * else the choice depends on 'autof'.
120 * If name is NULL it is assumed to match whatever dev provides.
121 * If both name and dev are NULL, we choose a name 'mdXX' or 'mdpXX'
123 * If 'name' is given, and 'trustworthy' is 'foreign' and name is not
124 * supported by 'dev', we add a "_%d" suffix based on the minor number
127 * If udev is configured, we create a temporary device, open it, and
129 * If not, we create the /dev/mdXX device, and is name is usable,
131 * In any case we return /dev/md/name or (if that isn't available)
132 * /dev/mdXX in 'chosen'.
134 * When we create devices, we use uid/gid/umask from config file.
137 int create_mddev(char *dev
, char *name
, int autof
, int trustworthy
,
144 struct createinfo
*ci
= conf_get_create_info();
159 strcpy(chosen
, "/dev/md/");
160 cname
= chosen
+ strlen(chosen
);
163 if (strncmp(dev
, "/dev/md/", 8) == 0) {
164 strcpy(cname
, dev
+8);
165 } else if (strncmp(dev
, "/dev/", 5) == 0) {
166 char *e
= dev
+ strlen(dev
);
167 while (e
> dev
&& isdigit(e
[-1]))
170 num
= strtoul(e
, NULL
, 10);
171 strcpy(cname
, dev
+5);
172 cname
[e
-(dev
+5)] = 0;
173 /* name *must* be mdXX or md_dXX in this context */
175 (strcmp(cname
, "md") != 0 && strcmp(cname
, "md_d") != 0)) {
176 pr_err("%s is an invalid name "
177 "for an md device. Try /dev/md/%s\n",
181 if (strcmp(cname
, "md") == 0)
185 /* recreate name: /dev/md/0 or /dev/md/d0 */
186 sprintf(cname
, "%s%d", use_mdp
?"d":"", num
);
190 /* 'cname' must not contain a slash, and may not be
193 if (strchr(cname
, '/') != NULL
) {
194 pr_err("%s is an invalid name "
195 "for an md device.\n", dev
);
199 pr_err("%s is an invalid name "
200 "for an md device (empty!).", dev
);
204 /* If cname is 'N' or 'dN', we get dev number
212 num
= strtoul(sp
, &ep
, 10);
215 if (ep
== sp
|| *ep
|| num
< 0)
217 else if (cname
[0] == 'd')
224 /* Now determine device number */
225 /* named 'METADATA' cannot use 'mdp'. */
226 if (name
&& name
[0] == 0)
228 if (name
&& trustworthy
== METADATA
&& use_mdp
== 1) {
229 pr_err("%s is not allowed for a %s container. "
230 "Consider /dev/md%d.\n", dev
, name
, num
);
233 if (name
&& trustworthy
== METADATA
)
236 if (autof
== 4 || autof
== 6)
241 if (num
< 0 && trustworthy
== LOCAL
&& name
) {
242 /* if name is numeric, possibly prefixed by
243 * 'md' or '/dev/md', use that for num
244 * if it is not already in use */
247 if (strncmp(n2
, "/dev/", 5) == 0)
249 if (strncmp(n2
, "md", 2) == 0)
253 num
= strtoul(n2
, &ep
, 10);
257 sprintf(devnm
, "md%s%d", use_mdp
? "_d":"", num
);
258 if (mddev_busy(devnm
))
264 /* need to choose a free number. */
265 char *_devnm
= find_free_devnm(use_mdp
);
267 pr_err("No avail md devices - aborting\n");
270 strcpy(devnm
, _devnm
);
272 sprintf(devnm
, "%s%d", use_mdp
?"md_d":"md", num
);
273 if (mddev_busy(devnm
)) {
274 pr_err("%s is already in use.\n",
280 sprintf(devname
, "/dev/%s", devnm
);
282 if (cname
[0] == 0 && name
) {
283 /* Need to find a name if we can
284 * We don't completely trust 'name'. Truncate to
285 * reasonable length and remove '/'
288 struct map_ent
*map
= NULL
;
292 strncpy(cname
, name
, 200);
294 for (cp
= cname
; *cp
; cp
++)
305 if (trustworthy
== LOCAL
||
306 (trustworthy
== FOREIGN
&& strchr(cname
, ':') != NULL
)) {
307 /* Only need suffix if there is a conflict */
308 if (map_by_name(&map
, cname
) == NULL
)
311 cnlen
= strlen(cname
);
313 if (trustworthy
== METADATA
&& !isdigit(cname
[cnlen
-1]))
314 sprintf(cname
+cnlen
, "%d", unum
);
316 /* add _%d to FOREIGN array that don't
319 sprintf(cname
+cnlen
, "_%d", unum
);
321 if (map_by_name(&map
, cname
) == NULL
)
326 if (dev
&& dev
[0] == '/')
328 else if (cname
[0] == 0)
329 strcpy(chosen
, devname
);
331 /* We have a device number and name.
332 * If we cannot detect udev, we need to make
333 * devices and links ourselves.
336 /* Make sure 'devname' exists and 'chosen' is a symlink to it */
337 if (lstat(devname
, &stb
) == 0) {
338 /* Must be the correct device, else error */
339 if ((stb
.st_mode
&S_IFMT
) != S_IFBLK
||
340 stb
.st_rdev
!= (dev_t
)devnm2devid(devnm
)) {
341 pr_err("%s exists but looks wrong, please fix\n",
346 if (mknod(devname
, S_IFBLK
|0600,
347 devnm2devid(devnm
)) != 0) {
348 pr_err("failed to create %s\n",
352 if (chown(devname
, ci
->uid
, ci
->gid
))
354 if (chmod(devname
, ci
->mode
))
357 add_dev(devname
, &stb
, 0, NULL
);
360 make_parts(devname
, parts
);
362 if (strcmp(chosen
, devname
) != 0) {
363 if (mkdir("/dev/md",0700) == 0) {
364 if (chown("/dev/md", ci
->uid
, ci
->gid
))
365 perror("chown /dev/md");
366 if (chmod("/dev/md", ci
->mode
| ((ci
->mode
>>2) & 0111)))
367 perror("chmod /dev/md");
370 if (dev
&& strcmp(chosen
, dev
) == 0)
371 /* We know we are allowed to use this name */
374 if (lstat(chosen
, &stb
) == 0) {
376 ssize_t link_len
= readlink(chosen
, buf
, sizeof(buf
)-1);
378 buf
[link_len
] = '\0';
380 if ((stb
.st_mode
& S_IFMT
) != S_IFLNK
||
382 strcmp(buf
, devname
) != 0) {
383 pr_err("%s exists - ignoring\n",
385 strcpy(chosen
, devname
);
387 } else if (symlink(devname
, chosen
) != 0)
388 pr_err("failed to create %s: %s\n",
389 chosen
, strerror(errno
));
390 if (use_mdp
&& strcmp(chosen
, devname
) != 0)
391 make_parts(chosen
, parts
);
394 mdfd
= open_dev_excl(devnm
);
396 pr_err("unexpected failure opening %s\n",
401 /* Open this and check that it is an md device.
402 * On success, return filedescriptor.
403 * On failure, return -1 if it doesn't exist,
404 * or -2 if it exists but is not an md device.
406 int open_mddev(char *dev
, int report_errors
)
408 int mdfd
= open(dev
, O_RDWR
);
409 if (mdfd
< 0 && errno
== EACCES
)
410 mdfd
= open(dev
, O_RDONLY
);
413 pr_err("error opening %s: %s\n",
414 dev
, strerror(errno
));
417 if (md_get_version(mdfd
) <= 0) {
420 pr_err("%s does not appear to be "
421 "an md device\n", dev
);