]>
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;
44 fl
= fopen("/proc/devices", "r");
47 while ((w
= conf_word(fl
, 1))) {
48 if (have_block
&& strcmp(w
, "devices:")==0)
50 have_block
= (strcmp(w
, "Block")==0);
53 if (have_devices
&& strcmp(w
, "mdp")==0)
61 char *devid2kname(int devid
)
65 static char devnm
[32];
70 * /sys/dev/block/%d:%d link which must look like
71 * and take the last component.
73 sprintf(path
, "/sys/dev/block/%d:%d", major(devid
),
75 n
= readlink(path
, link
, sizeof(link
)-1);
78 cp
= strrchr(link
, '/');
87 char *stat2kname(struct stat
*st
)
89 if ((S_IFMT
& st
->st_mode
) != S_IFBLK
)
91 return devid2kname(st
->st_rdev
);
94 char *fd2kname(int fd
)
97 if (fstat(fd
, &stb
) == 0)
98 return stat2kname(&stb
);
102 char *devid2devnm(dev_t devid
)
106 static char devnm
[32];
110 /* Might be an extended-minor partition or a
111 * named md device. Look at the
112 * /sys/dev/block/%d:%d link which must look like
113 * ../../block/mdXXX/mdXXXpYY
117 sprintf(path
, "/sys/dev/block/%d:%d", major(devid
),
119 n
= readlink(path
, link
, sizeof(link
)-1);
122 cp
= strstr(link
, "/block/");
125 ep
= strchr(cp
, '/');
132 if (major(devid
) == MD_MAJOR
)
133 sprintf(devnm
,"md%d", minor(devid
));
134 else if (major(devid
) == (unsigned)get_mdp_major())
135 sprintf(devnm
,"md_d%d",
136 (minor(devid
)>>MdpMinorShift
));
142 char *stat2devnm(struct stat
*st
)
144 if ((S_IFMT
& st
->st_mode
) != S_IFBLK
)
146 return devid2devnm(st
->st_rdev
);
149 char *fd2devnm(int fd
)
152 if (fstat(fd
, &stb
) == 0)
153 return stat2devnm(&stb
);
158 * convert a major/minor pair for a block device into a name in /dev, if possible.
159 * On the first call, walk /dev collecting name.
160 * Put them in a simple linked listfor now.
167 int devlist_ready
= 0;
169 int add_dev(const char *name
, const struct stat
*stb
, int flag
, struct FTW
*s
)
173 if (S_ISLNK(stb
->st_mode
)) {
174 if (stat(name
, &st
) != 0)
179 if ((stb
->st_mode
&S_IFMT
)== S_IFBLK
) {
180 char *n
= xstrdup(name
);
181 struct devmap
*dm
= xmalloc(sizeof(*dm
));
182 if (strncmp(n
, "/dev/./", 7)==0)
185 dm
->major
= major(stb
->st_rdev
);
186 dm
->minor
= minor(stb
->st_rdev
);
197 int add_dev_1(const char *name
, const struct stat
*stb
, int flag
)
199 return add_dev(name
, stb
, flag
, NULL
);
201 int nftw(const char *path
, int (*han
)(const char *name
, const struct stat
*stb
, int flag
, struct FTW
*s
), int nopenfd
, int flags
)
203 return ftw(path
, add_dev_1
, nopenfd
);
206 int nftw(const char *path
, int (*han
)(const char *name
, const struct stat
*stb
, int flag
, struct FTW
*s
), int nopenfd
, int flags
)
210 #endif /* HAVE_FTW */
211 #endif /* HAVE_NFTW */
214 * Find a block device with the right major/minor number.
215 * If we find multiple names, choose the shortest.
216 * If we find a name in /dev/md/, we prefer that.
217 * This applies only to names for MD devices.
218 * If 'prefer' is set (normally to e.g. /by-path/)
219 * then we prefer a name which contains that string.
221 char *map_dev_preferred(int major
, int minor
, int create
,
225 char *regular
= NULL
, *preferred
=NULL
;
228 if (major
== 0 && minor
== 0)
232 if (!devlist_ready
) {
236 struct devmap
*d
= devlist
;
241 if (lstat(dev
, &stb
)==0 &&
242 S_ISLNK(stb
.st_mode
))
244 nftw(dev
, add_dev
, 10, FTW_PHYS
);
249 for (p
=devlist
; p
; p
=p
->next
)
250 if (p
->major
== major
&&
252 if (strncmp(p
->name
, "/dev/md/",8) == 0
253 || (prefer
&& strstr(p
->name
, prefer
))) {
254 if (preferred
== NULL
||
255 strlen(p
->name
) < strlen(preferred
))
258 if (regular
== NULL
||
259 strlen(p
->name
) < strlen(regular
))
263 if (!regular
&& !preferred
&& !did_check
) {
267 if (create
&& !regular
&& !preferred
) {
269 snprintf(buf
, sizeof(buf
), "%d:%d", major
, minor
);
273 return preferred
? preferred
: regular
;
276 /* conf_word gets one word from the conf file.
277 * if "allow_key", then accept words at the start of a line,
278 * otherwise stop when such a word is found.
279 * We assume that the file pointer is at the end of a word, so the
280 * next character is a space, or a newline. If not, it is the start of a line.
283 char *conf_word(FILE *file
, int allow_key
)
290 char *word
= xmalloc(wsize
);
292 while (wordfound
==0) {
293 /* at the end of a word.. */
296 while (c
!= EOF
&& c
!= '\n')
299 if (c
== '\n') continue;
301 if (c
!= ' ' && c
!= '\t' && ! allow_key
) {
305 /* looks like it is safe to get a word here, if there is one */
307 /* first, skip any spaces */
308 while (c
== ' ' || c
== '\t')
310 if (c
!= EOF
&& c
!= '\n' && c
!= '#') {
311 /* we really have a character of a word, so start saving it */
312 while (c
!= EOF
&& c
!= '\n' && (quote
|| (c
!=' ' && c
!= '\t'))) {
314 if (quote
&& c
== quote
) quote
= 0;
315 else if (quote
== 0 && (c
== '\'' || c
== '"'))
318 if (len
== wsize
-1) {
320 word
= xrealloc(word
, wsize
);
325 /* Hack for broken kernels (2.6.14-.24) that put
326 * "active(auto-read-only)"
327 * in /proc/mdstat instead of
328 * "active (auto-read-only)"
330 if (c
== '(' && len
>= 6
331 && strncmp(word
+len
-6, "active", 6) == 0)
335 if (c
!= EOF
) ungetc(c
, file
);
339 /* Further HACK for broken kernels.. 2.6.14-2.6.24 */
340 if (strcmp(word
, "auto-read-only)") == 0)
341 strcpy(word
, "(auto-read-only)");
343 /* printf("word is <%s>\n", word); */
351 void print_quoted(char *str
)
353 /* Printf the string with surrounding quotes
355 * If no space, tab, or quote - leave unchanged.
356 * Else print surrounded by " or ', swapping quotes
357 * when we find one that will cause confusion.
360 char first_quote
= 0, q
;
363 for (c
= str
; *c
; c
++) {
383 if (first_quote
== '"')
388 for (c
= str
; *c
; c
++) {
399 void print_escape(char *str
)
401 /* print str, but change space and tab to '_'
402 * as is suitable for device names
404 for (; *str
; str
++) {
419 int check_env(char *name
)
421 char *val
= getenv(name
);
423 if (val
&& atoi(val
) == 1)
435 use
= ((stat("/dev/.udev", &stb
) == 0
436 || stat("/run/udev", &stb
) == 0)
437 && check_env("MDADM_NO_UDEV") == 0);
442 unsigned long GCD(unsigned long a
, unsigned long b
)
454 * conf_line reads one logical line from the conffile or mdstat.
455 * It skips comments and continues until it finds a line that starts
456 * with a non blank/comment. This character is pushed back for the next call
457 * A doubly linked list of words is returned.
458 * the first word will be a keyword. Other words will have had quotes removed.
461 char *conf_line(FILE *file
)
466 w
= conf_word(file
, 1);
467 if (w
== NULL
) return NULL
;
473 while ((w
= conf_word(file
,0))){
474 char *w2
= dl_strdup(w
);
478 /* printf("got a line\n");*/
482 void free_line(char *line
)
485 for (w
=dl_next(line
); w
!= line
; w
=dl_next(line
)) {