]>
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(int 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
),
78 n
= readlink(path
, link
, sizeof(link
) - 1);
81 cp
= strrchr(link
, '/');
83 strcpy(devnm
, cp
+ 1);
90 char *stat2kname(struct stat
*st
)
92 if ((S_IFMT
& st
->st_mode
) != S_IFBLK
)
95 return devid2kname(st
->st_rdev
);
98 char *fd2kname(int fd
)
102 if (fstat(fd
, &stb
) == 0)
103 return stat2kname(&stb
);
108 char *devid2devnm(dev_t devid
)
112 static char devnm
[32];
116 /* Might be an extended-minor partition or a
117 * named md device. Look at the
118 * /sys/dev/block/%d:%d link which must look like
119 * ../../block/mdXXX/mdXXXpYY
123 sprintf(path
, "/sys/dev/block/%d:%d", major(devid
), minor(devid
));
124 n
= readlink(path
, link
, sizeof(link
) - 1);
127 cp
= strstr(link
, "/block/");
130 ep
= strchr(cp
, '/');
137 if (major(devid
) == MD_MAJOR
)
138 sprintf(devnm
,"md%d", minor(devid
));
139 else if (major(devid
) == (unsigned)get_mdp_major())
140 sprintf(devnm
,"md_d%d",
141 (minor(devid
)>>MdpMinorShift
));
148 char *stat2devnm(struct stat
*st
)
150 if ((S_IFMT
& st
->st_mode
) != S_IFBLK
)
153 return devid2devnm(st
->st_rdev
);
156 char *fd2devnm(int fd
)
160 if (fstat(fd
, &stb
) == 0)
161 return stat2devnm(&stb
);
167 * convert a major/minor pair for a block device into a name in /dev, if possible.
168 * On the first call, walk /dev collecting name.
169 * Put them in a simple linked listfor now.
176 int devlist_ready
= 0;
178 int add_dev(const char *name
, const struct stat
*stb
, int flag
, struct FTW
*s
)
182 if (S_ISLNK(stb
->st_mode
)) {
183 if (stat(name
, &st
) != 0)
188 if ((stb
->st_mode
&S_IFMT
)== S_IFBLK
) {
189 char *n
= xstrdup(name
);
190 struct devmap
*dm
= xmalloc(sizeof(*dm
));
191 if (strncmp(n
, "/dev/./", 7) == 0)
192 strcpy(n
+ 4, name
+ 6);
194 dm
->major
= major(stb
->st_rdev
);
195 dm
->minor
= minor(stb
->st_rdev
);
207 int add_dev_1(const char *name
, const struct stat
*stb
, int flag
)
209 return add_dev(name
, stb
, flag
, NULL
);
211 int nftw(const char *path
,
212 int (*han
)(const char *name
, const struct stat
*stb
,
213 int flag
, struct FTW
*s
), int nopenfd
, int flags
)
215 return ftw(path
, add_dev_1
, nopenfd
);
218 int nftw(const char *path
,
219 int (*han
)(const char *name
, const struct stat
*stb
,
220 int flag
, struct FTW
*s
), int nopenfd
, int flags
)
224 #endif /* HAVE_FTW */
225 #endif /* HAVE_NFTW */
228 * Find a block device with the right major/minor number.
229 * If we find multiple names, choose the shortest.
230 * If we find a name in /dev/md/, we prefer that.
231 * This applies only to names for MD devices.
232 * If 'prefer' is set (normally to e.g. /by-path/)
233 * then we prefer a name which contains that string.
235 char *map_dev_preferred(int major
, int minor
, int create
,
239 char *regular
= NULL
, *preferred
=NULL
;
242 if (major
== 0 && minor
== 0)
246 if (!devlist_ready
) {
250 struct devmap
*d
= devlist
;
255 if (lstat(dev
, &stb
) == 0 && S_ISLNK(stb
.st_mode
))
257 nftw(dev
, add_dev
, 10, FTW_PHYS
);
262 for (p
= devlist
; p
; p
= p
->next
)
263 if (p
->major
== major
&& p
->minor
== minor
) {
264 if (strncmp(p
->name
, "/dev/md/",8) == 0 ||
265 (prefer
&& strstr(p
->name
, prefer
))) {
266 if (preferred
== NULL
||
267 strlen(p
->name
) < strlen(preferred
))
270 if (regular
== NULL
||
271 strlen(p
->name
) < strlen(regular
))
275 if (!regular
&& !preferred
&& !did_check
) {
279 if (create
&& !regular
&& !preferred
) {
281 snprintf(buf
, sizeof(buf
), "%d:%d", major
, minor
);
285 return preferred
? preferred
: regular
;
288 /* conf_word gets one word from the conf file.
289 * if "allow_key", then accept words at the start of a line,
290 * otherwise stop when such a word is found.
291 * We assume that the file pointer is at the end of a word, so the
292 * next character is a space, or a newline. If not, it is the start of a line.
295 char *conf_word(FILE *file
, int allow_key
)
302 char *word
= xmalloc(wsize
);
304 while (wordfound
== 0) {
305 /* at the end of a word.. */
308 while (c
!= EOF
&& c
!= '\n')
315 if (c
!= ' ' && c
!= '\t' && ! allow_key
) {
319 /* looks like it is safe to get a word here, if there is one */
321 /* first, skip any spaces */
322 while (c
== ' ' || c
== '\t')
324 if (c
!= EOF
&& c
!= '\n' && c
!= '#') {
325 /* we really have a character of a word, so start saving it */
326 while (c
!= EOF
&& c
!= '\n' &&
327 (quote
|| (c
!= ' ' && c
!= '\t'))) {
329 if (quote
&& c
== quote
)
331 else if (quote
== 0 && (c
== '\'' || c
== '"'))
334 if (len
== wsize
-1) {
336 word
= xrealloc(word
, wsize
);
341 /* Hack for broken kernels (2.6.14-.24) that put
342 * "active(auto-read-only)"
343 * in /proc/mdstat instead of
344 * "active (auto-read-only)"
346 if (c
== '(' && len
>= 6 &&
347 strncmp(word
+ len
- 6, "active", 6) == 0)
356 /* Further HACK for broken kernels.. 2.6.14-2.6.24 */
357 if (strcmp(word
, "auto-read-only)") == 0)
358 strcpy(word
, "(auto-read-only)");
360 /* printf("word is <%s>\n", word); */
368 void print_quoted(char *str
)
370 /* Printf the string with surrounding quotes
372 * If no space, tab, or quote - leave unchanged.
373 * Else print surrounded by " or ', swapping quotes
374 * when we find one that will cause confusion.
377 char first_quote
= 0, q
;
380 for (c
= str
; *c
; c
++) {
400 if (first_quote
== '"')
405 for (c
= str
; *c
; c
++) {
416 void print_escape(char *str
)
418 /* print str, but change space and tab to '_'
419 * as is suitable for device names
421 for (; *str
; str
++) {
436 int check_env(char *name
)
438 char *val
= getenv(name
);
440 if (val
&& atoi(val
) == 1)
452 use
= ((stat("/dev/.udev", &stb
) == 0 ||
453 stat("/run/udev", &stb
) == 0) &&
454 check_env("MDADM_NO_UDEV") == 0);
459 unsigned long GCD(unsigned long a
, unsigned long b
)
471 * conf_line reads one logical line from the conffile or mdstat.
472 * It skips comments and continues until it finds a line that starts
473 * with a non blank/comment. This character is pushed back for the next call
474 * A doubly linked list of words is returned.
475 * the first word will be a keyword. Other words will have had quotes removed.
478 char *conf_line(FILE *file
)
483 w
= conf_word(file
, 1);
491 while ((w
= conf_word(file
, 0))){
492 char *w2
= dl_strdup(w
);
496 /* printf("got a line\n");*/
500 void free_line(char *line
)
503 for (w
= dl_next(line
); w
!= line
; w
= dl_next(line
)) {