]>
git.ipfire.org Git - thirdparty/mdadm.git/blob - lib.c
2 * mdadm - manage Linux "md" devices aka RAID arrays.
4 * Copyright (C) 2011 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 /* This fill contains various 'library' style function. They
30 * have no dependency on anything outside this file.
33 int get_mdp_major(void)
35 static int mdp_major
= -1;
45 fl
= fopen("/proc/devices", "r");
49 while ((w
= conf_word(fl
, 1))) {
50 if (have_block
&& strcmp(w
, "devices:") == 0)
52 have_block
= (strcmp(w
, "Block") == 0);
55 if (have_devices
&& strcmp(w
, "mdp") == 0)
64 char *devid2kname(dev_t devid
)
68 static char devnm
[32];
73 * /sys/dev/block/%d:%d link which must look like
74 * and take the last component.
76 sprintf(path
, "/sys/dev/block/%d:%d", major(devid
), minor(devid
));
77 n
= readlink(path
, link
, sizeof(link
) - 1);
80 cp
= strrchr(link
, '/');
82 strcpy(devnm
, cp
+ 1);
89 char *stat2kname(struct stat
*st
)
91 if ((S_IFMT
& st
->st_mode
) != S_IFBLK
)
94 return devid2kname(st
->st_rdev
);
97 char *fd2kname(int fd
)
101 if (fstat(fd
, &stb
) == 0)
102 return stat2kname(&stb
);
107 char *devid2devnm(dev_t devid
)
111 static char devnm
[32];
115 /* Might be an extended-minor partition or a
116 * named md device. Look at the
117 * /sys/dev/block/%d:%d link which must look like
118 * ../../block/mdXXX/mdXXXpYY
122 sprintf(path
, "/sys/dev/block/%d:%d", major(devid
), minor(devid
));
123 n
= readlink(path
, link
, sizeof(link
) - 1);
126 cp
= strstr(link
, "/block/");
129 ep
= strchr(cp
, '/');
136 if (major(devid
) == MD_MAJOR
)
137 sprintf(devnm
,"md%d", minor(devid
));
138 else if (major(devid
) == (unsigned)get_mdp_major())
139 sprintf(devnm
,"md_d%d",
140 (minor(devid
)>>MdpMinorShift
));
147 char *stat2devnm(struct stat
*st
)
149 if ((S_IFMT
& st
->st_mode
) != S_IFBLK
)
152 return devid2devnm(st
->st_rdev
);
155 char *fd2devnm(int fd
)
159 if (fstat(fd
, &stb
) == 0)
160 return stat2devnm(&stb
);
165 /* When we create a new array, we don't want the content to
166 * be immediately examined by udev - it is probably meaningless.
167 * So create /run/mdadm/creating-mdXXX and expect that a udev
168 * rule will noticed this and act accordingly.
170 static char block_path
[] = "/run/mdadm/creating-%s";
171 static char *unblock_path
= NULL
;
172 void udev_block(char *devnm
)
177 xasprintf(&path
, block_path
, devnm
);
178 fd
= open(path
, O_CREAT
|O_RDWR
, 0600);
186 void udev_unblock(void)
189 unlink(unblock_path
);
195 * convert a major/minor pair for a block device into a name in /dev, if possible.
196 * On the first call, walk /dev collecting name.
197 * Put them in a simple linked listfor now.
204 int devlist_ready
= 0;
206 int add_dev(const char *name
, const struct stat
*stb
, int flag
, struct FTW
*s
)
210 if (S_ISLNK(stb
->st_mode
)) {
211 if (stat(name
, &st
) != 0)
216 if ((stb
->st_mode
&S_IFMT
)== S_IFBLK
) {
217 char *n
= xstrdup(name
);
218 struct devmap
*dm
= xmalloc(sizeof(*dm
));
219 if (strncmp(n
, "/dev/./", 7) == 0)
220 strcpy(n
+ 4, name
+ 6);
222 dm
->major
= major(stb
->st_rdev
);
223 dm
->minor
= minor(stb
->st_rdev
);
235 int add_dev_1(const char *name
, const struct stat
*stb
, int flag
)
237 return add_dev(name
, stb
, flag
, NULL
);
239 int nftw(const char *path
,
240 int (*han
)(const char *name
, const struct stat
*stb
,
241 int flag
, struct FTW
*s
), int nopenfd
, int flags
)
243 return ftw(path
, add_dev_1
, nopenfd
);
246 int nftw(const char *path
,
247 int (*han
)(const char *name
, const struct stat
*stb
,
248 int flag
, struct FTW
*s
), int nopenfd
, int flags
)
252 #endif /* HAVE_FTW */
253 #endif /* HAVE_NFTW */
256 * Find a block device with the right major/minor number.
257 * If we find multiple names, choose the shortest.
258 * If we find a name in /dev/md/, we prefer that.
259 * This applies only to names for MD devices.
260 * If 'prefer' is set (normally to e.g. /by-path/)
261 * then we prefer a name which contains that string.
263 char *map_dev_preferred(int major
, int minor
, int create
,
267 char *regular
= NULL
, *preferred
=NULL
;
270 if (major
== 0 && minor
== 0)
274 if (!devlist_ready
) {
278 struct devmap
*d
= devlist
;
283 if (lstat(dev
, &stb
) == 0 && S_ISLNK(stb
.st_mode
))
285 nftw(dev
, add_dev
, 10, FTW_PHYS
);
290 for (p
= devlist
; p
; p
= p
->next
)
291 if (p
->major
== major
&& p
->minor
== minor
) {
292 if (strncmp(p
->name
, "/dev/md/",8) == 0 ||
293 (prefer
&& strstr(p
->name
, prefer
))) {
294 if (preferred
== NULL
||
295 strlen(p
->name
) < strlen(preferred
))
298 if (regular
== NULL
||
299 strlen(p
->name
) < strlen(regular
))
303 if (!regular
&& !preferred
&& !did_check
) {
307 if (create
&& !regular
&& !preferred
) {
309 snprintf(buf
, sizeof(buf
), "%d:%d", major
, minor
);
313 return preferred
? preferred
: regular
;
316 /* conf_word gets one word from the conf file.
317 * if "allow_key", then accept words at the start of a line,
318 * otherwise stop when such a word is found.
319 * We assume that the file pointer is at the end of a word, so the
320 * next character is a space, or a newline. If not, it is the start of a line.
323 char *conf_word(FILE *file
, int allow_key
)
330 char *word
= xmalloc(wsize
);
332 while (wordfound
== 0) {
333 /* at the end of a word.. */
336 while (c
!= EOF
&& c
!= '\n')
343 if (c
!= ' ' && c
!= '\t' && ! allow_key
) {
347 /* looks like it is safe to get a word here, if there is one */
349 /* first, skip any spaces */
350 while (c
== ' ' || c
== '\t')
352 if (c
!= EOF
&& c
!= '\n' && c
!= '#') {
353 /* we really have a character of a word, so start saving it */
354 while (c
!= EOF
&& c
!= '\n' &&
355 (quote
|| (c
!= ' ' && c
!= '\t'))) {
357 if (quote
&& c
== quote
)
359 else if (quote
== 0 && (c
== '\'' || c
== '"'))
362 if (len
== wsize
-1) {
364 word
= xrealloc(word
, wsize
);
369 /* Hack for broken kernels (2.6.14-.24) that put
370 * "active(auto-read-only)"
371 * in /proc/mdstat instead of
372 * "active (auto-read-only)"
374 if (c
== '(' && len
>= 6 &&
375 strncmp(word
+ len
- 6, "active", 6) == 0)
384 /* Further HACK for broken kernels.. 2.6.14-2.6.24 */
385 if (strcmp(word
, "auto-read-only)") == 0)
386 strcpy(word
, "(auto-read-only)");
388 /* printf("word is <%s>\n", word); */
396 void print_quoted(char *str
)
398 /* Printf the string with surrounding quotes
400 * If no space, tab, or quote - leave unchanged.
401 * Else print surrounded by " or ', swapping quotes
402 * when we find one that will cause confusion.
405 char first_quote
= 0, q
;
408 for (c
= str
; *c
; c
++) {
428 if (first_quote
== '"')
433 for (c
= str
; *c
; c
++) {
444 void print_escape(char *str
)
446 /* print str, but change space and tab to '_'
447 * as is suitable for device names
449 for (; *str
; str
++) {
464 int check_env(char *name
)
466 char *val
= getenv(name
);
468 if (val
&& atoi(val
) == 1)
480 use
= ((stat("/dev/.udev", &stb
) == 0 ||
481 stat("/run/udev", &stb
) == 0) &&
482 check_env("MDADM_NO_UDEV") == 0);
487 unsigned long GCD(unsigned long a
, unsigned long b
)
499 * conf_line reads one logical line from the conffile or mdstat.
500 * It skips comments and continues until it finds a line that starts
501 * with a non blank/comment. This character is pushed back for the next call
502 * A doubly linked list of words is returned.
503 * the first word will be a keyword. Other words will have had quotes removed.
506 char *conf_line(FILE *file
)
511 w
= conf_word(file
, 1);
519 while ((w
= conf_word(file
, 0))){
520 char *w2
= dl_strdup(w
);
524 /* printf("got a line\n");*/
528 void free_line(char *line
)
531 for (w
= dl_next(line
); w
!= line
; w
= dl_next(line
)) {