]> git.ipfire.org Git - thirdparty/mdadm.git/blob - lib.c
Discard devnum in favour of devnm
[thirdparty/mdadm.git] / lib.c
1 /*
2 * mdadm - manage Linux "md" devices aka RAID arrays.
3 *
4 * Copyright (C) 2011 Neil Brown <neilb@suse.de>
5 *
6 *
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.
11 *
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.
16 *
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
20 *
21 * Author: Neil Brown
22 * Email: <neilb@suse.de>
23 */
24
25 #include "mdadm.h"
26 #include <ctype.h>
27
28 /* This fill contains various 'library' style function. They
29 * have no dependency on anything outside this file.
30 */
31
32 int get_mdp_major(void)
33 {
34 static int mdp_major = -1;
35 FILE *fl;
36 char *w;
37 int have_block = 0;
38 int have_devices = 0;
39 int last_num = -1;
40
41 if (mdp_major != -1)
42 return mdp_major;
43 fl = fopen("/proc/devices", "r");
44 if (!fl)
45 return -1;
46 while ((w = conf_word(fl, 1))) {
47 if (have_block && strcmp(w, "devices:")==0)
48 have_devices = 1;
49 have_block = (strcmp(w, "Block")==0);
50 if (isdigit(w[0]))
51 last_num = atoi(w);
52 if (have_devices && strcmp(w, "mdp")==0)
53 mdp_major = last_num;
54 free(w);
55 }
56 fclose(fl);
57 return mdp_major;
58 }
59
60 char *devid2devnm(int devid)
61 {
62 char path[30];
63 char link[200];
64 static char devnm[32];
65 char *cp, *ep;
66 int n;
67
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
72 * or
73 * ...../block/md_FOO
74 */
75 sprintf(path, "/sys/dev/block/%d:%d", major(devid),
76 minor(devid));
77 n = readlink(path, link, sizeof(link)-1);
78 if (n > 0) {
79 link[n] = 0;
80 cp = strstr(link, "/block/");
81 if (cp) {
82 cp += 7;
83 ep = strchr(cp, '/');
84 if (ep)
85 *ep = 0;
86 strcpy(devnm, cp);
87 return devnm;
88 }
89 }
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));
95 else
96 return NULL;
97 return devnm;
98 }
99
100 char *stat2devnm(struct stat *st)
101 {
102 if ((S_IFMT & st->st_mode) != S_IFBLK)
103 return NULL;
104 return devid2devnm(st->st_rdev);
105 }
106
107 char *fd2devnm(int fd)
108 {
109 struct stat stb;
110 if (fstat(fd, &stb) == 0)
111 return stat2devnm(&stb);
112 return NULL;
113 }
114
115
116
117 /*
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.
121 */
122 struct devmap {
123 int major, minor;
124 char *name;
125 struct devmap *next;
126 } *devlist = NULL;
127 int devlist_ready = 0;
128
129 int add_dev(const char *name, const struct stat *stb, int flag, struct FTW *s)
130 {
131 struct stat st;
132
133 if (S_ISLNK(stb->st_mode)) {
134 if (stat(name, &st) != 0)
135 return 0;
136 stb = &st;
137 }
138
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)
143 strcpy(n+4, name+6);
144 if (dm) {
145 dm->major = major(stb->st_rdev);
146 dm->minor = minor(stb->st_rdev);
147 dm->name = n;
148 dm->next = devlist;
149 devlist = dm;
150 }
151 }
152 return 0;
153 }
154
155 #ifndef HAVE_NFTW
156 #ifdef HAVE_FTW
157 int add_dev_1(const char *name, const struct stat *stb, int flag)
158 {
159 return add_dev(name, stb, flag, NULL);
160 }
161 int nftw(const char *path, int (*han)(const char *name, const struct stat *stb, int flag, struct FTW *s), int nopenfd, int flags)
162 {
163 return ftw(path, add_dev_1, nopenfd);
164 }
165 #else
166 int nftw(const char *path, int (*han)(const char *name, const struct stat *stb, int flag, struct FTW *s), int nopenfd, int flags)
167 {
168 return 0;
169 }
170 #endif /* HAVE_FTW */
171 #endif /* HAVE_NFTW */
172
173 /*
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.
180 */
181 char *map_dev_preferred(int major, int minor, int create,
182 char *prefer)
183 {
184 struct devmap *p;
185 char *regular = NULL, *preferred=NULL;
186 int did_check = 0;
187
188 if (major == 0 && minor == 0)
189 return NULL;
190
191 retry:
192 if (!devlist_ready) {
193 char *dev = "/dev";
194 struct stat stb;
195 while(devlist) {
196 struct devmap *d = devlist;
197 devlist = d->next;
198 free(d->name);
199 free(d);
200 }
201 if (lstat(dev, &stb)==0 &&
202 S_ISLNK(stb.st_mode))
203 dev = "/dev/.";
204 nftw(dev, add_dev, 10, FTW_PHYS);
205 devlist_ready=1;
206 did_check = 1;
207 }
208
209 for (p=devlist; p; p=p->next)
210 if (p->major == major &&
211 p->minor == minor) {
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))
216 preferred = p->name;
217 } else {
218 if (regular == NULL ||
219 strlen(p->name) < strlen(regular))
220 regular = p->name;
221 }
222 }
223 if (!regular && !preferred && !did_check) {
224 devlist_ready = 0;
225 goto retry;
226 }
227 if (create && !regular && !preferred) {
228 static char buf[30];
229 snprintf(buf, sizeof(buf), "%d:%d", major, minor);
230 regular = buf;
231 }
232
233 return preferred ? preferred : regular;
234 }
235
236
237
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.
243 */
244
245 char *conf_word(FILE *file, int allow_key)
246 {
247 int wsize = 100;
248 int len = 0;
249 int c;
250 int quote;
251 int wordfound = 0;
252 char *word = xmalloc(wsize);
253
254 while (wordfound==0) {
255 /* at the end of a word.. */
256 c = getc(file);
257 if (c == '#')
258 while (c != EOF && c != '\n')
259 c = getc(file);
260 if (c == EOF) break;
261 if (c == '\n') continue;
262
263 if (c != ' ' && c != '\t' && ! allow_key) {
264 ungetc(c, file);
265 break;
266 }
267 /* looks like it is safe to get a word here, if there is one */
268 quote = 0;
269 /* first, skip any spaces */
270 while (c == ' ' || c == '\t')
271 c = getc(file);
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'))) {
275 wordfound = 1;
276 if (quote && c == quote) quote = 0;
277 else if (quote == 0 && (c == '\'' || c == '"'))
278 quote = c;
279 else {
280 if (len == wsize-1) {
281 wsize += 100;
282 word = xrealloc(word, wsize);
283 }
284 word[len++] = c;
285 }
286 c = getc(file);
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)"
291 */
292 if (c == '(' && len >= 6
293 && strncmp(word+len-6, "active", 6) == 0)
294 c = ' ';
295 }
296 }
297 if (c != EOF) ungetc(c, file);
298 }
299 word[len] = 0;
300
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)");
304
305 /* printf("word is <%s>\n", word); */
306 if (!wordfound) {
307 free(word);
308 word = NULL;
309 }
310 return word;
311 }
312
313 void print_quoted(char *str)
314 {
315 /* Printf the string with surrounding quotes
316 * iff needed.
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.
320 */
321
322 char first_quote = 0, q;
323 char *c;
324
325 for (c = str; *c; c++) {
326 switch(*c) {
327 case '\'':
328 case '"':
329 first_quote = *c;
330 break;
331 case ' ':
332 case '\t':
333 first_quote = *c;
334 continue;
335 default:
336 continue;
337 }
338 break;
339 }
340 if (!first_quote) {
341 printf("%s", str);
342 return;
343 }
344
345 if (first_quote == '"')
346 q = '\'';
347 else
348 q = '"';
349 putchar(q);
350 for (c = str; *c; c++) {
351 if (*c == q) {
352 putchar(q);
353 q ^= '"' ^ '\'';
354 putchar(q);
355 }
356 putchar(*c);
357 }
358 putchar(q);
359 }
360
361 void print_escape(char *str)
362 {
363 /* print str, but change space and tab to '_'
364 * as is suitable for device names
365 */
366 for (; *str ; str++) {
367 switch (*str) {
368 case ' ':
369 case '\t':
370 putchar('_');
371 break;
372 case '/':
373 putchar('-');
374 break;
375 default:
376 putchar(*str);
377 }
378 }
379 }
380
381 int use_udev(void)
382 {
383 static int use = -1;
384 struct stat stb;
385
386 if (use < 0) {
387 use = ((stat("/dev/.udev", &stb) == 0
388 || stat("/run/udev", &stb) == 0)
389 && check_env("MDADM_NO_UDEV") == 0);
390 }
391 return use;
392 }