]>
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>
28 /* This fill contains various 'library' style function. They
29 * have no dependency on anything outside this file.
32 int get_mdp_major(void)
34 static int mdp_major
= -1;
43 fl
= fopen("/proc/devices", "r");
46 while ((w
= conf_word(fl
, 1))) {
47 if (have_block
&& strcmp(w
, "devices:")==0)
49 have_block
= (strcmp(w
, "Block")==0);
52 if (have_devices
&& strcmp(w
, "mdp")==0)
60 char *devid2devnm(int devid
)
64 static char devnm
[32];
68 /* Might be an extended-minor partition or a
69 * named md device. Look at the
70 * /sys/dev/block/%d:%d link which must look like
71 * ../../block/mdXXX/mdXXXpYY
75 sprintf(path
, "/sys/dev/block/%d:%d", major(devid
),
77 n
= readlink(path
, link
, sizeof(link
)-1);
80 cp
= strstr(link
, "/block/");
90 if (major(devid
) == MD_MAJOR
)
91 sprintf(devnm
,"md%d", minor(devid
));
92 else if (major(devid
) == (unsigned)get_mdp_major())
93 sprintf(devnm
,"md_d%d",
94 (minor(devid
)>>MdpMinorShift
));
100 char *stat2devnm(struct stat
*st
)
102 if ((S_IFMT
& st
->st_mode
) != S_IFBLK
)
104 return devid2devnm(st
->st_rdev
);
107 char *fd2devnm(int fd
)
110 if (fstat(fd
, &stb
) == 0)
111 return stat2devnm(&stb
);
118 * convert a major/minor pair for a block device into a name in /dev, if possible.
119 * On the first call, walk /dev collecting name.
120 * Put them in a simple linked listfor now.
127 int devlist_ready
= 0;
129 int add_dev(const char *name
, const struct stat
*stb
, int flag
, struct FTW
*s
)
133 if (S_ISLNK(stb
->st_mode
)) {
134 if (stat(name
, &st
) != 0)
139 if ((stb
->st_mode
&S_IFMT
)== S_IFBLK
) {
140 char *n
= xstrdup(name
);
141 struct devmap
*dm
= xmalloc(sizeof(*dm
));
142 if (strncmp(n
, "/dev/./", 7)==0)
145 dm
->major
= major(stb
->st_rdev
);
146 dm
->minor
= minor(stb
->st_rdev
);
157 int add_dev_1(const char *name
, const struct stat
*stb
, int flag
)
159 return add_dev(name
, stb
, flag
, NULL
);
161 int nftw(const char *path
, int (*han
)(const char *name
, const struct stat
*stb
, int flag
, struct FTW
*s
), int nopenfd
, int flags
)
163 return ftw(path
, add_dev_1
, nopenfd
);
166 int nftw(const char *path
, int (*han
)(const char *name
, const struct stat
*stb
, int flag
, struct FTW
*s
), int nopenfd
, int flags
)
170 #endif /* HAVE_FTW */
171 #endif /* HAVE_NFTW */
174 * Find a block device with the right major/minor number.
175 * If we find multiple names, choose the shortest.
176 * If we find a name in /dev/md/, we prefer that.
177 * This applies only to names for MD devices.
178 * If 'prefer' is set (normally to e.g. /by-path/)
179 * then we prefer a name which contains that string.
181 char *map_dev_preferred(int major
, int minor
, int create
,
185 char *regular
= NULL
, *preferred
=NULL
;
188 if (major
== 0 && minor
== 0)
192 if (!devlist_ready
) {
196 struct devmap
*d
= devlist
;
201 if (lstat(dev
, &stb
)==0 &&
202 S_ISLNK(stb
.st_mode
))
204 nftw(dev
, add_dev
, 10, FTW_PHYS
);
209 for (p
=devlist
; p
; p
=p
->next
)
210 if (p
->major
== major
&&
212 if (strncmp(p
->name
, "/dev/md/",8) == 0
213 || (prefer
&& strstr(p
->name
, prefer
))) {
214 if (preferred
== NULL
||
215 strlen(p
->name
) < strlen(preferred
))
218 if (regular
== NULL
||
219 strlen(p
->name
) < strlen(regular
))
223 if (!regular
&& !preferred
&& !did_check
) {
227 if (create
&& !regular
&& !preferred
) {
229 snprintf(buf
, sizeof(buf
), "%d:%d", major
, minor
);
233 return preferred
? preferred
: regular
;
238 /* conf_word gets one word from the conf file.
239 * if "allow_key", then accept words at the start of a line,
240 * otherwise stop when such a word is found.
241 * We assume that the file pointer is at the end of a word, so the
242 * next character is a space, or a newline. If not, it is the start of a line.
245 char *conf_word(FILE *file
, int allow_key
)
252 char *word
= xmalloc(wsize
);
254 while (wordfound
==0) {
255 /* at the end of a word.. */
258 while (c
!= EOF
&& c
!= '\n')
261 if (c
== '\n') continue;
263 if (c
!= ' ' && c
!= '\t' && ! allow_key
) {
267 /* looks like it is safe to get a word here, if there is one */
269 /* first, skip any spaces */
270 while (c
== ' ' || c
== '\t')
272 if (c
!= EOF
&& c
!= '\n' && c
!= '#') {
273 /* we really have a character of a word, so start saving it */
274 while (c
!= EOF
&& c
!= '\n' && (quote
|| (c
!=' ' && c
!= '\t'))) {
276 if (quote
&& c
== quote
) quote
= 0;
277 else if (quote
== 0 && (c
== '\'' || c
== '"'))
280 if (len
== wsize
-1) {
282 word
= xrealloc(word
, wsize
);
287 /* Hack for broken kernels (2.6.14-.24) that put
288 * "active(auto-read-only)"
289 * in /proc/mdstat instead of
290 * "active (auto-read-only)"
292 if (c
== '(' && len
>= 6
293 && strncmp(word
+len
-6, "active", 6) == 0)
297 if (c
!= EOF
) ungetc(c
, file
);
301 /* Further HACK for broken kernels.. 2.6.14-2.6.24 */
302 if (strcmp(word
, "auto-read-only)") == 0)
303 strcpy(word
, "(auto-read-only)");
305 /* printf("word is <%s>\n", word); */
313 void print_quoted(char *str
)
315 /* Printf the string with surrounding quotes
317 * If no space, tab, or quote - leave unchanged.
318 * Else print surrounded by " or ', swapping quotes
319 * when we find one that will cause confusion.
322 char first_quote
= 0, q
;
325 for (c
= str
; *c
; c
++) {
345 if (first_quote
== '"')
350 for (c
= str
; *c
; c
++) {
361 void print_escape(char *str
)
363 /* print str, but change space and tab to '_'
364 * as is suitable for device names
366 for (; *str
; str
++) {
387 use
= ((stat("/dev/.udev", &stb
) == 0
388 || stat("/run/udev", &stb
) == 0)
389 && check_env("MDADM_NO_UDEV") == 0);