]>
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 *devid2devnm(int devid
)
65 static char devnm
[32];
69 /* Might be an extended-minor partition or a
70 * named md device. Look at the
71 * /sys/dev/block/%d:%d link which must look like
72 * ../../block/mdXXX/mdXXXpYY
76 sprintf(path
, "/sys/dev/block/%d:%d", major(devid
),
78 n
= readlink(path
, link
, sizeof(link
)-1);
81 cp
= strstr(link
, "/block/");
91 if (major(devid
) == MD_MAJOR
)
92 sprintf(devnm
,"md%d", minor(devid
));
93 else if (major(devid
) == (unsigned)get_mdp_major())
94 sprintf(devnm
,"md_d%d",
95 (minor(devid
)>>MdpMinorShift
));
101 char *stat2devnm(struct stat
*st
)
103 if ((S_IFMT
& st
->st_mode
) != S_IFBLK
)
105 return devid2devnm(st
->st_rdev
);
108 char *fd2devnm(int fd
)
111 if (fstat(fd
, &stb
) == 0)
112 return stat2devnm(&stb
);
117 * convert a major/minor pair for a block device into a name in /dev, if possible.
118 * On the first call, walk /dev collecting name.
119 * Put them in a simple linked listfor now.
126 int devlist_ready
= 0;
128 int add_dev(const char *name
, const struct stat
*stb
, int flag
, struct FTW
*s
)
132 if (S_ISLNK(stb
->st_mode
)) {
133 if (stat(name
, &st
) != 0)
138 if ((stb
->st_mode
&S_IFMT
)== S_IFBLK
) {
139 char *n
= xstrdup(name
);
140 struct devmap
*dm
= xmalloc(sizeof(*dm
));
141 if (strncmp(n
, "/dev/./", 7)==0)
144 dm
->major
= major(stb
->st_rdev
);
145 dm
->minor
= minor(stb
->st_rdev
);
156 int add_dev_1(const char *name
, const struct stat
*stb
, int flag
)
158 return add_dev(name
, stb
, flag
, NULL
);
160 int nftw(const char *path
, int (*han
)(const char *name
, const struct stat
*stb
, int flag
, struct FTW
*s
), int nopenfd
, int flags
)
162 return ftw(path
, add_dev_1
, nopenfd
);
165 int nftw(const char *path
, int (*han
)(const char *name
, const struct stat
*stb
, int flag
, struct FTW
*s
), int nopenfd
, int flags
)
169 #endif /* HAVE_FTW */
170 #endif /* HAVE_NFTW */
173 * Find a block device with the right major/minor number.
174 * If we find multiple names, choose the shortest.
175 * If we find a name in /dev/md/, we prefer that.
176 * This applies only to names for MD devices.
177 * If 'prefer' is set (normally to e.g. /by-path/)
178 * then we prefer a name which contains that string.
180 char *map_dev_preferred(int major
, int minor
, int create
,
184 char *regular
= NULL
, *preferred
=NULL
;
187 if (major
== 0 && minor
== 0)
191 if (!devlist_ready
) {
195 struct devmap
*d
= devlist
;
200 if (lstat(dev
, &stb
)==0 &&
201 S_ISLNK(stb
.st_mode
))
203 nftw(dev
, add_dev
, 10, FTW_PHYS
);
208 for (p
=devlist
; p
; p
=p
->next
)
209 if (p
->major
== major
&&
211 if (strncmp(p
->name
, "/dev/md/",8) == 0
212 || (prefer
&& strstr(p
->name
, prefer
))) {
213 if (preferred
== NULL
||
214 strlen(p
->name
) < strlen(preferred
))
217 if (regular
== NULL
||
218 strlen(p
->name
) < strlen(regular
))
222 if (!regular
&& !preferred
&& !did_check
) {
226 if (create
&& !regular
&& !preferred
) {
228 snprintf(buf
, sizeof(buf
), "%d:%d", major
, minor
);
232 return preferred
? preferred
: regular
;
235 /* conf_word gets one word from the conf file.
236 * if "allow_key", then accept words at the start of a line,
237 * otherwise stop when such a word is found.
238 * We assume that the file pointer is at the end of a word, so the
239 * next character is a space, or a newline. If not, it is the start of a line.
242 char *conf_word(FILE *file
, int allow_key
)
249 char *word
= xmalloc(wsize
);
251 while (wordfound
==0) {
252 /* at the end of a word.. */
255 while (c
!= EOF
&& c
!= '\n')
258 if (c
== '\n') continue;
260 if (c
!= ' ' && c
!= '\t' && ! allow_key
) {
264 /* looks like it is safe to get a word here, if there is one */
266 /* first, skip any spaces */
267 while (c
== ' ' || c
== '\t')
269 if (c
!= EOF
&& c
!= '\n' && c
!= '#') {
270 /* we really have a character of a word, so start saving it */
271 while (c
!= EOF
&& c
!= '\n' && (quote
|| (c
!=' ' && c
!= '\t'))) {
273 if (quote
&& c
== quote
) quote
= 0;
274 else if (quote
== 0 && (c
== '\'' || c
== '"'))
277 if (len
== wsize
-1) {
279 word
= xrealloc(word
, wsize
);
284 /* Hack for broken kernels (2.6.14-.24) that put
285 * "active(auto-read-only)"
286 * in /proc/mdstat instead of
287 * "active (auto-read-only)"
289 if (c
== '(' && len
>= 6
290 && strncmp(word
+len
-6, "active", 6) == 0)
294 if (c
!= EOF
) ungetc(c
, file
);
298 /* Further HACK for broken kernels.. 2.6.14-2.6.24 */
299 if (strcmp(word
, "auto-read-only)") == 0)
300 strcpy(word
, "(auto-read-only)");
302 /* printf("word is <%s>\n", word); */
310 void print_quoted(char *str
)
312 /* Printf the string with surrounding quotes
314 * If no space, tab, or quote - leave unchanged.
315 * Else print surrounded by " or ', swapping quotes
316 * when we find one that will cause confusion.
319 char first_quote
= 0, q
;
322 for (c
= str
; *c
; c
++) {
342 if (first_quote
== '"')
347 for (c
= str
; *c
; c
++) {
358 void print_escape(char *str
)
360 /* print str, but change space and tab to '_'
361 * as is suitable for device names
363 for (; *str
; str
++) {
378 int check_env(char *name
)
380 char *val
= getenv(name
);
382 if (val
&& atoi(val
) == 1)
394 use
= ((stat("/dev/.udev", &stb
) == 0
395 || stat("/run/udev", &stb
) == 0)
396 && check_env("MDADM_NO_UDEV") == 0);
401 unsigned long GCD(unsigned long a
, unsigned long b
)
413 * conf_line reads one logical line from the conffile or mdstat.
414 * It skips comments and continues until it finds a line that starts
415 * with a non blank/comment. This character is pushed back for the next call
416 * A doubly linked list of words is returned.
417 * the first word will be a keyword. Other words will have had quotes removed.
420 char *conf_line(FILE *file
)
425 w
= conf_word(file
, 1);
426 if (w
== NULL
) return NULL
;
432 while ((w
= conf_word(file
,0))){
433 char *w2
= dl_strdup(w
);
437 /* printf("got a line\n");*/
441 void free_line(char *line
)
444 for (w
=dl_next(line
); w
!= line
; w
=dl_next(line
)) {