]>
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>
30 bool is_dev_alive(char *path
)
35 if (access(path
, R_OK
) == 0)
41 /* This fill contains various 'library' style function. They
42 * have no dependency on anything outside this file.
45 int get_mdp_major(void)
47 static int mdp_major
= -1;
57 fl
= fopen("/proc/devices", "r");
61 while ((w
= conf_word(fl
, 1))) {
62 if (have_block
&& strcmp(w
, "devices:") == 0)
64 have_block
= (strcmp(w
, "Block") == 0);
67 if (have_devices
&& strcmp(w
, "mdp") == 0)
76 char *devid2kname(dev_t devid
)
80 static char devnm
[32];
85 * /sys/dev/block/%d:%d link which must look like
86 * and take the last component.
88 sprintf(path
, "/sys/dev/block/%d:%d", major(devid
), minor(devid
));
89 n
= readlink(path
, link
, sizeof(link
) - 1);
92 cp
= strrchr(link
, '/');
94 strcpy(devnm
, cp
+ 1);
101 char *stat2kname(struct stat
*st
)
103 if ((S_IFMT
& st
->st_mode
) != S_IFBLK
)
106 return devid2kname(st
->st_rdev
);
109 char *fd2kname(int fd
)
113 if (fstat(fd
, &stb
) == 0)
114 return stat2kname(&stb
);
119 char *devid2devnm(dev_t devid
)
123 static char devnm
[32];
127 /* Might be an extended-minor partition or a
128 * named md device. Look at the
129 * /sys/dev/block/%d:%d link which must look like
130 * ../../block/mdXXX/mdXXXpYY
134 sprintf(path
, "/sys/dev/block/%d:%d", major(devid
), minor(devid
));
135 n
= readlink(path
, link
, sizeof(link
) - 1);
138 cp
= strstr(link
, "/block/");
141 ep
= strchr(cp
, '/');
148 if (major(devid
) == MD_MAJOR
)
149 sprintf(devnm
,"md%d", minor(devid
));
150 else if (major(devid
) == (unsigned)get_mdp_major())
151 sprintf(devnm
,"md_d%d",
152 (minor(devid
)>>MdpMinorShift
));
159 char *stat2devnm(struct stat
*st
)
161 if ((S_IFMT
& st
->st_mode
) != S_IFBLK
)
164 return devid2devnm(st
->st_rdev
);
167 bool stat_is_md_dev(struct stat
*st
)
169 if ((S_IFMT
& st
->st_mode
) != S_IFBLK
)
171 if (major(st
->st_rdev
) == MD_MAJOR
)
173 if (major(st
->st_rdev
) == (unsigned)get_mdp_major())
179 char *fd2devnm(int fd
)
183 if (fstat(fd
, &stb
) == 0)
184 return stat2devnm(&stb
);
189 /* When we create a new array, we don't want the content to
190 * be immediately examined by udev - it is probably meaningless.
191 * So create /run/mdadm/creating-mdXXX and expect that a udev
192 * rule will noticed this and act accordingly.
194 static char block_path
[] = "/run/mdadm/creating-%s";
195 static char *unblock_path
= NULL
;
196 void udev_block(char *devnm
)
201 xasprintf(&path
, block_path
, devnm
);
202 fd
= open(path
, O_CREAT
|O_RDWR
, 0600);
210 void udev_unblock(void)
213 unlink(unblock_path
);
219 * convert a major/minor pair for a block device into a name in /dev, if possible.
220 * On the first call, walk /dev collecting name.
221 * Put them in a simple linked listfor now.
228 int devlist_ready
= 0;
230 int add_dev(const char *name
, const struct stat
*stb
, int flag
, struct FTW
*s
)
234 if (S_ISLNK(stb
->st_mode
)) {
235 if (stat(name
, &st
) != 0)
240 if ((stb
->st_mode
&S_IFMT
)== S_IFBLK
) {
241 char *n
= xstrdup(name
);
242 struct devmap
*dm
= xmalloc(sizeof(*dm
));
243 if (strncmp(n
, "/dev/./", 7) == 0)
244 strcpy(n
+ 4, name
+ 6);
246 dm
->major
= major(stb
->st_rdev
);
247 dm
->minor
= minor(stb
->st_rdev
);
259 int add_dev_1(const char *name
, const struct stat
*stb
, int flag
)
261 return add_dev(name
, stb
, flag
, NULL
);
263 int nftw(const char *path
,
264 int (*han
)(const char *name
, const struct stat
*stb
,
265 int flag
, struct FTW
*s
), int nopenfd
, int flags
)
267 return ftw(path
, add_dev_1
, nopenfd
);
270 int nftw(const char *path
,
271 int (*han
)(const char *name
, const struct stat
*stb
,
272 int flag
, struct FTW
*s
), int nopenfd
, int flags
)
276 #endif /* HAVE_FTW */
277 #endif /* HAVE_NFTW */
280 * Find a block device with the right major/minor number.
281 * If we find multiple names, choose the shortest.
282 * If we find a name in /dev/md/, we prefer that.
283 * This applies only to names for MD devices.
284 * If 'prefer' is set (normally to e.g. /by-path/)
285 * then we prefer a name which contains that string.
287 char *map_dev_preferred(int major
, int minor
, int create
,
291 char *regular
= NULL
, *preferred
=NULL
;
294 if (major
== 0 && minor
== 0)
298 if (!devlist_ready
) {
302 struct devmap
*d
= devlist
;
307 if (lstat(dev
, &stb
) == 0 && S_ISLNK(stb
.st_mode
))
309 nftw(dev
, add_dev
, 10, FTW_PHYS
);
314 for (p
= devlist
; p
; p
= p
->next
)
315 if (p
->major
== major
&& p
->minor
== minor
) {
316 if (strncmp(p
->name
, "/dev/md/",8) == 0 ||
317 (prefer
&& strstr(p
->name
, prefer
))) {
318 if (preferred
== NULL
||
319 strlen(p
->name
) < strlen(preferred
))
322 if (regular
== NULL
||
323 strlen(p
->name
) < strlen(regular
))
327 if (!regular
&& !preferred
&& !did_check
) {
331 if (create
&& !regular
&& !preferred
) {
333 snprintf(buf
, sizeof(buf
), "%d:%d", major
, minor
);
337 return preferred
? preferred
: regular
;
340 /* conf_word gets one word from the conf file.
341 * if "allow_key", then accept words at the start of a line,
342 * otherwise stop when such a word is found.
343 * We assume that the file pointer is at the end of a word, so the
344 * next character is a space, or a newline. If not, it is the start of a line.
347 char *conf_word(FILE *file
, int allow_key
)
354 char *word
= xmalloc(wsize
);
356 while (wordfound
== 0) {
357 /* at the end of a word.. */
360 while (c
!= EOF
&& c
!= '\n')
367 if (c
!= ' ' && c
!= '\t' && ! allow_key
) {
371 /* looks like it is safe to get a word here, if there is one */
373 /* first, skip any spaces */
374 while (c
== ' ' || c
== '\t')
376 if (c
!= EOF
&& c
!= '\n' && c
!= '#') {
377 /* we really have a character of a word, so start saving it */
378 while (c
!= EOF
&& c
!= '\n' &&
379 (quote
|| (c
!= ' ' && c
!= '\t'))) {
381 if (quote
&& c
== quote
)
383 else if (quote
== 0 && (c
== '\'' || c
== '"'))
386 if (len
== wsize
-1) {
388 word
= xrealloc(word
, wsize
);
393 /* Hack for broken kernels (2.6.14-.24) that put
394 * "active(auto-read-only)"
395 * in /proc/mdstat instead of
396 * "active (auto-read-only)"
398 if (c
== '(' && len
>= 6 &&
399 strncmp(word
+ len
- 6, "active", 6) == 0)
408 /* Further HACK for broken kernels.. 2.6.14-2.6.24 */
409 if (strcmp(word
, "auto-read-only)") == 0)
410 strcpy(word
, "(auto-read-only)");
412 /* printf("word is <%s>\n", word); */
420 void print_quoted(char *str
)
422 /* Printf the string with surrounding quotes
424 * If no space, tab, or quote - leave unchanged.
425 * Else print surrounded by " or ', swapping quotes
426 * when we find one that will cause confusion.
429 char first_quote
= 0, q
;
432 for (c
= str
; *c
; c
++) {
452 if (first_quote
== '"')
457 for (c
= str
; *c
; c
++) {
468 void print_escape(char *str
)
470 /* print str, but change space and tab to '_'
471 * as is suitable for device names
473 for (; *str
; str
++) {
488 int check_env(char *name
)
490 char *val
= getenv(name
);
492 if (val
&& atoi(val
) == 1)
504 use
= ((stat("/dev/.udev", &stb
) == 0 ||
505 stat("/run/udev", &stb
) == 0) &&
506 check_env("MDADM_NO_UDEV") == 0);
511 unsigned long GCD(unsigned long a
, unsigned long b
)
523 * conf_line reads one logical line from the conffile or mdstat.
524 * It skips comments and continues until it finds a line that starts
525 * with a non blank/comment. This character is pushed back for the next call
526 * A doubly linked list of words is returned.
527 * the first word will be a keyword. Other words will have had quotes removed.
530 char *conf_line(FILE *file
)
535 w
= conf_word(file
, 1);
543 while ((w
= conf_word(file
, 0))){
544 char *w2
= dl_strdup(w
);
548 /* printf("got a line\n");*/
552 void free_line(char *line
)
555 for (w
= dl_next(line
); w
!= line
; w
= dl_next(line
)) {
563 * parse_num() - Parse int from string.
564 * @dest: Pointer to destination.
565 * @num: Pointer to string that is going to be parsed.
567 * If string contains anything after a number, error code is returned.
568 * The same happens when number is bigger than INT_MAX or smaller than 0.
569 * Writes to destination only if successfully read the number.
571 * Return: 0 on success, 1 otherwise.
573 int parse_num(int *dest
, char *num
)
582 temp
= strtol(num
, &c
, 10);
583 if (temp
< 0 || temp
> INT_MAX
|| *c
|| errno
!= 0 || num
== c
)