]>
Commit | Line | Data |
---|---|---|
78c0a3b1 N |
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" | |
d0c017a6 | 26 | #include "dlink.h" |
78c0a3b1 N |
27 | #include <ctype.h> |
28 | ||
29 | /* This fill contains various 'library' style function. They | |
30 | * have no dependency on anything outside this file. | |
31 | */ | |
32 | ||
33 | int get_mdp_major(void) | |
34 | { | |
ad7ac9ac | 35 | static int mdp_major = -1; |
78c0a3b1 N |
36 | FILE *fl; |
37 | char *w; | |
38 | int have_block = 0; | |
39 | int have_devices = 0; | |
40 | int last_num = -1; | |
41 | ||
42 | if (mdp_major != -1) | |
43 | return mdp_major; | |
ad7ac9ac | 44 | |
78c0a3b1 N |
45 | fl = fopen("/proc/devices", "r"); |
46 | if (!fl) | |
47 | return -1; | |
ad7ac9ac | 48 | |
78c0a3b1 | 49 | while ((w = conf_word(fl, 1))) { |
ad7ac9ac | 50 | if (have_block && strcmp(w, "devices:") == 0) |
78c0a3b1 | 51 | have_devices = 1; |
ad7ac9ac | 52 | have_block = (strcmp(w, "Block") == 0); |
78c0a3b1 N |
53 | if (isdigit(w[0])) |
54 | last_num = atoi(w); | |
ad7ac9ac | 55 | if (have_devices && strcmp(w, "mdp") == 0) |
78c0a3b1 N |
56 | mdp_major = last_num; |
57 | free(w); | |
58 | } | |
59 | fclose(fl); | |
ad7ac9ac | 60 | |
78c0a3b1 N |
61 | return mdp_major; |
62 | } | |
63 | ||
d3c40fab | 64 | char *devid2kname(dev_t devid) |
bc17158d N |
65 | { |
66 | char path[30]; | |
52a94085 | 67 | char link[PATH_MAX]; |
bc17158d N |
68 | static char devnm[32]; |
69 | char *cp; | |
70 | int n; | |
71 | ||
72 | /* Look at the | |
73 | * /sys/dev/block/%d:%d link which must look like | |
74 | * and take the last component. | |
75 | */ | |
d3c40fab | 76 | sprintf(path, "/sys/dev/block/%d:%d", major(devid), minor(devid)); |
ad7ac9ac | 77 | n = readlink(path, link, sizeof(link) - 1); |
bc17158d N |
78 | if (n > 0) { |
79 | link[n] = 0; | |
80 | cp = strrchr(link, '/'); | |
81 | if (cp) { | |
ad7ac9ac | 82 | strcpy(devnm, cp + 1); |
bc17158d N |
83 | return devnm; |
84 | } | |
85 | } | |
86 | return NULL; | |
87 | } | |
88 | ||
f96b1302 AP |
89 | char *stat2kname(struct stat *st) |
90 | { | |
91 | if ((S_IFMT & st->st_mode) != S_IFBLK) | |
92 | return NULL; | |
ad7ac9ac | 93 | |
f96b1302 AP |
94 | return devid2kname(st->st_rdev); |
95 | } | |
96 | ||
97 | char *fd2kname(int fd) | |
98 | { | |
99 | struct stat stb; | |
ad7ac9ac | 100 | |
f96b1302 AP |
101 | if (fstat(fd, &stb) == 0) |
102 | return stat2kname(&stb); | |
ad7ac9ac | 103 | |
f96b1302 AP |
104 | return NULL; |
105 | } | |
106 | ||
13db17bd | 107 | char *devid2devnm(dev_t devid) |
78c0a3b1 N |
108 | { |
109 | char path[30]; | |
110 | char link[200]; | |
4dd2df09 N |
111 | static char devnm[32]; |
112 | char *cp, *ep; | |
78c0a3b1 N |
113 | int n; |
114 | ||
4dd2df09 N |
115 | /* Might be an extended-minor partition or a |
116 | * named md device. Look at the | |
117 | * /sys/dev/block/%d:%d link which must look like | |
118 | * ../../block/mdXXX/mdXXXpYY | |
119 | * or | |
120 | * ...../block/md_FOO | |
121 | */ | |
ad7ac9ac JS |
122 | sprintf(path, "/sys/dev/block/%d:%d", major(devid), minor(devid)); |
123 | n = readlink(path, link, sizeof(link) - 1); | |
4dd2df09 | 124 | if (n > 0) { |
78c0a3b1 | 125 | link[n] = 0; |
4dd2df09 N |
126 | cp = strstr(link, "/block/"); |
127 | if (cp) { | |
128 | cp += 7; | |
129 | ep = strchr(cp, '/'); | |
130 | if (ep) | |
131 | *ep = 0; | |
132 | strcpy(devnm, cp); | |
133 | return devnm; | |
134 | } | |
78c0a3b1 | 135 | } |
4dd2df09 N |
136 | if (major(devid) == MD_MAJOR) |
137 | sprintf(devnm,"md%d", minor(devid)); | |
138 | else if (major(devid) == (unsigned)get_mdp_major()) | |
139 | sprintf(devnm,"md_d%d", | |
140 | (minor(devid)>>MdpMinorShift)); | |
141 | else | |
142 | return NULL; | |
ad7ac9ac | 143 | |
4dd2df09 N |
144 | return devnm; |
145 | } | |
78c0a3b1 | 146 | |
4dd2df09 N |
147 | char *stat2devnm(struct stat *st) |
148 | { | |
149 | if ((S_IFMT & st->st_mode) != S_IFBLK) | |
150 | return NULL; | |
ad7ac9ac | 151 | |
4dd2df09 | 152 | return devid2devnm(st->st_rdev); |
78c0a3b1 N |
153 | } |
154 | ||
4dd2df09 | 155 | char *fd2devnm(int fd) |
78c0a3b1 N |
156 | { |
157 | struct stat stb; | |
ad7ac9ac | 158 | |
78c0a3b1 | 159 | if (fstat(fd, &stb) == 0) |
4dd2df09 | 160 | return stat2devnm(&stb); |
ad7ac9ac | 161 | |
4dd2df09 | 162 | return NULL; |
78c0a3b1 N |
163 | } |
164 | ||
cd6cbb08 N |
165 | /* When we create a new array, we don't want the content to |
166 | * be immediately examined by udev - it is probably meaningless. | |
dd180cb1 | 167 | * So create /run/mdadm/creating-mdXXX and expect that a udev |
cd6cbb08 N |
168 | * rule will noticed this and act accordingly. |
169 | */ | |
170 | static char block_path[] = "/run/mdadm/creating-%s"; | |
171 | static char *unblock_path = NULL; | |
172 | void udev_block(char *devnm) | |
173 | { | |
174 | int fd; | |
175 | char *path = NULL; | |
176 | ||
177 | xasprintf(&path, block_path, devnm); | |
178 | fd = open(path, O_CREAT|O_RDWR, 0600); | |
179 | if (fd >= 0) { | |
180 | close(fd); | |
181 | unblock_path = path; | |
182 | } else | |
183 | free(path); | |
184 | } | |
185 | ||
186 | void udev_unblock(void) | |
187 | { | |
188 | if (unblock_path) | |
189 | unlink(unblock_path); | |
190 | free(unblock_path); | |
191 | unblock_path = NULL; | |
192 | } | |
193 | ||
78c0a3b1 N |
194 | /* |
195 | * convert a major/minor pair for a block device into a name in /dev, if possible. | |
196 | * On the first call, walk /dev collecting name. | |
197 | * Put them in a simple linked listfor now. | |
198 | */ | |
199 | struct devmap { | |
5d500228 N |
200 | int major, minor; |
201 | char *name; | |
202 | struct devmap *next; | |
78c0a3b1 N |
203 | } *devlist = NULL; |
204 | int devlist_ready = 0; | |
205 | ||
206 | int add_dev(const char *name, const struct stat *stb, int flag, struct FTW *s) | |
207 | { | |
208 | struct stat st; | |
209 | ||
210 | if (S_ISLNK(stb->st_mode)) { | |
211 | if (stat(name, &st) != 0) | |
212 | return 0; | |
213 | stb = &st; | |
214 | } | |
215 | ||
216 | if ((stb->st_mode&S_IFMT)== S_IFBLK) { | |
503975b9 N |
217 | char *n = xstrdup(name); |
218 | struct devmap *dm = xmalloc(sizeof(*dm)); | |
ad7ac9ac JS |
219 | if (strncmp(n, "/dev/./", 7) == 0) |
220 | strcpy(n + 4, name + 6); | |
78c0a3b1 N |
221 | if (dm) { |
222 | dm->major = major(stb->st_rdev); | |
223 | dm->minor = minor(stb->st_rdev); | |
224 | dm->name = n; | |
225 | dm->next = devlist; | |
226 | devlist = dm; | |
227 | } | |
228 | } | |
ad7ac9ac | 229 | |
78c0a3b1 N |
230 | return 0; |
231 | } | |
232 | ||
233 | #ifndef HAVE_NFTW | |
234 | #ifdef HAVE_FTW | |
235 | int add_dev_1(const char *name, const struct stat *stb, int flag) | |
236 | { | |
237 | return add_dev(name, stb, flag, NULL); | |
238 | } | |
ad7ac9ac JS |
239 | int nftw(const char *path, |
240 | int (*han)(const char *name, const struct stat *stb, | |
241 | int flag, struct FTW *s), int nopenfd, int flags) | |
78c0a3b1 N |
242 | { |
243 | return ftw(path, add_dev_1, nopenfd); | |
244 | } | |
245 | #else | |
ad7ac9ac JS |
246 | int nftw(const char *path, |
247 | int (*han)(const char *name, const struct stat *stb, | |
248 | int flag, struct FTW *s), int nopenfd, int flags) | |
78c0a3b1 N |
249 | { |
250 | return 0; | |
251 | } | |
252 | #endif /* HAVE_FTW */ | |
253 | #endif /* HAVE_NFTW */ | |
254 | ||
255 | /* | |
256 | * Find a block device with the right major/minor number. | |
257 | * If we find multiple names, choose the shortest. | |
258 | * If we find a name in /dev/md/, we prefer that. | |
259 | * This applies only to names for MD devices. | |
c2ecf5f6 N |
260 | * If 'prefer' is set (normally to e.g. /by-path/) |
261 | * then we prefer a name which contains that string. | |
78c0a3b1 | 262 | */ |
c2ecf5f6 N |
263 | char *map_dev_preferred(int major, int minor, int create, |
264 | char *prefer) | |
78c0a3b1 N |
265 | { |
266 | struct devmap *p; | |
267 | char *regular = NULL, *preferred=NULL; | |
268 | int did_check = 0; | |
269 | ||
270 | if (major == 0 && minor == 0) | |
781f7efb | 271 | return NULL; |
78c0a3b1 N |
272 | |
273 | retry: | |
274 | if (!devlist_ready) { | |
275 | char *dev = "/dev"; | |
276 | struct stat stb; | |
277 | while(devlist) { | |
278 | struct devmap *d = devlist; | |
279 | devlist = d->next; | |
280 | free(d->name); | |
281 | free(d); | |
282 | } | |
ad7ac9ac | 283 | if (lstat(dev, &stb) == 0 && S_ISLNK(stb.st_mode)) |
78c0a3b1 N |
284 | dev = "/dev/."; |
285 | nftw(dev, add_dev, 10, FTW_PHYS); | |
286 | devlist_ready=1; | |
287 | did_check = 1; | |
288 | } | |
289 | ||
ad7ac9ac JS |
290 | for (p = devlist; p; p = p->next) |
291 | if (p->major == major && p->minor == minor) { | |
292 | if (strncmp(p->name, "/dev/md/",8) == 0 || | |
293 | (prefer && strstr(p->name, prefer))) { | |
78c0a3b1 N |
294 | if (preferred == NULL || |
295 | strlen(p->name) < strlen(preferred)) | |
296 | preferred = p->name; | |
297 | } else { | |
298 | if (regular == NULL || | |
299 | strlen(p->name) < strlen(regular)) | |
300 | regular = p->name; | |
301 | } | |
302 | } | |
303 | if (!regular && !preferred && !did_check) { | |
304 | devlist_ready = 0; | |
305 | goto retry; | |
306 | } | |
307 | if (create && !regular && !preferred) { | |
308 | static char buf[30]; | |
309 | snprintf(buf, sizeof(buf), "%d:%d", major, minor); | |
310 | regular = buf; | |
311 | } | |
312 | ||
313 | return preferred ? preferred : regular; | |
314 | } | |
315 | ||
78c0a3b1 N |
316 | /* conf_word gets one word from the conf file. |
317 | * if "allow_key", then accept words at the start of a line, | |
318 | * otherwise stop when such a word is found. | |
319 | * We assume that the file pointer is at the end of a word, so the | |
320 | * next character is a space, or a newline. If not, it is the start of a line. | |
321 | */ | |
322 | ||
323 | char *conf_word(FILE *file, int allow_key) | |
324 | { | |
325 | int wsize = 100; | |
326 | int len = 0; | |
327 | int c; | |
328 | int quote; | |
329 | int wordfound = 0; | |
503975b9 | 330 | char *word = xmalloc(wsize); |
78c0a3b1 | 331 | |
ad7ac9ac | 332 | while (wordfound == 0) { |
78c0a3b1 N |
333 | /* at the end of a word.. */ |
334 | c = getc(file); | |
335 | if (c == '#') | |
336 | while (c != EOF && c != '\n') | |
337 | c = getc(file); | |
ad7ac9ac JS |
338 | if (c == EOF) |
339 | break; | |
340 | if (c == '\n') | |
341 | continue; | |
78c0a3b1 N |
342 | |
343 | if (c != ' ' && c != '\t' && ! allow_key) { | |
344 | ungetc(c, file); | |
345 | break; | |
346 | } | |
347 | /* looks like it is safe to get a word here, if there is one */ | |
348 | quote = 0; | |
349 | /* first, skip any spaces */ | |
350 | while (c == ' ' || c == '\t') | |
351 | c = getc(file); | |
352 | if (c != EOF && c != '\n' && c != '#') { | |
353 | /* we really have a character of a word, so start saving it */ | |
ad7ac9ac JS |
354 | while (c != EOF && c != '\n' && |
355 | (quote || (c != ' ' && c != '\t'))) { | |
78c0a3b1 | 356 | wordfound = 1; |
ad7ac9ac JS |
357 | if (quote && c == quote) |
358 | quote = 0; | |
78c0a3b1 N |
359 | else if (quote == 0 && (c == '\'' || c == '"')) |
360 | quote = c; | |
361 | else { | |
362 | if (len == wsize-1) { | |
363 | wsize += 100; | |
503975b9 | 364 | word = xrealloc(word, wsize); |
78c0a3b1 N |
365 | } |
366 | word[len++] = c; | |
367 | } | |
368 | c = getc(file); | |
369 | /* Hack for broken kernels (2.6.14-.24) that put | |
370 | * "active(auto-read-only)" | |
371 | * in /proc/mdstat instead of | |
372 | * "active (auto-read-only)" | |
373 | */ | |
ad7ac9ac JS |
374 | if (c == '(' && len >= 6 && |
375 | strncmp(word + len - 6, "active", 6) == 0) | |
78c0a3b1 N |
376 | c = ' '; |
377 | } | |
378 | } | |
ad7ac9ac JS |
379 | if (c != EOF) |
380 | ungetc(c, file); | |
78c0a3b1 N |
381 | } |
382 | word[len] = 0; | |
383 | ||
384 | /* Further HACK for broken kernels.. 2.6.14-2.6.24 */ | |
385 | if (strcmp(word, "auto-read-only)") == 0) | |
386 | strcpy(word, "(auto-read-only)"); | |
387 | ||
388 | /* printf("word is <%s>\n", word); */ | |
389 | if (!wordfound) { | |
390 | free(word); | |
391 | word = NULL; | |
392 | } | |
393 | return word; | |
394 | } | |
7103b9b8 N |
395 | |
396 | void print_quoted(char *str) | |
397 | { | |
398 | /* Printf the string with surrounding quotes | |
399 | * iff needed. | |
400 | * If no space, tab, or quote - leave unchanged. | |
401 | * Else print surrounded by " or ', swapping quotes | |
402 | * when we find one that will cause confusion. | |
403 | */ | |
404 | ||
405 | char first_quote = 0, q; | |
406 | char *c; | |
407 | ||
408 | for (c = str; *c; c++) { | |
409 | switch(*c) { | |
410 | case '\'': | |
411 | case '"': | |
412 | first_quote = *c; | |
413 | break; | |
414 | case ' ': | |
415 | case '\t': | |
416 | first_quote = *c; | |
417 | continue; | |
418 | default: | |
419 | continue; | |
420 | } | |
421 | break; | |
422 | } | |
423 | if (!first_quote) { | |
424 | printf("%s", str); | |
425 | return; | |
426 | } | |
427 | ||
428 | if (first_quote == '"') | |
429 | q = '\''; | |
430 | else | |
431 | q = '"'; | |
432 | putchar(q); | |
433 | for (c = str; *c; c++) { | |
434 | if (*c == q) { | |
435 | putchar(q); | |
436 | q ^= '"' ^ '\''; | |
437 | putchar(q); | |
438 | } | |
439 | putchar(*c); | |
440 | } | |
441 | putchar(q); | |
442 | } | |
443 | ||
444 | void print_escape(char *str) | |
445 | { | |
446 | /* print str, but change space and tab to '_' | |
447 | * as is suitable for device names | |
448 | */ | |
ad7ac9ac | 449 | for (; *str; str++) { |
7103b9b8 N |
450 | switch (*str) { |
451 | case ' ': | |
452 | case '\t': | |
453 | putchar('_'); | |
454 | break; | |
455 | case '/': | |
456 | putchar('-'); | |
457 | break; | |
458 | default: | |
459 | putchar(*str); | |
460 | } | |
461 | } | |
462 | } | |
06d2ffc3 | 463 | |
f8fcf7a1 BS |
464 | int check_env(char *name) |
465 | { | |
466 | char *val = getenv(name); | |
467 | ||
468 | if (val && atoi(val) == 1) | |
469 | return 1; | |
470 | ||
471 | return 0; | |
472 | } | |
473 | ||
06d2ffc3 N |
474 | int use_udev(void) |
475 | { | |
476 | static int use = -1; | |
477 | struct stat stb; | |
478 | ||
479 | if (use < 0) { | |
ad7ac9ac JS |
480 | use = ((stat("/dev/.udev", &stb) == 0 || |
481 | stat("/run/udev", &stb) == 0) && | |
482 | check_env("MDADM_NO_UDEV") == 0); | |
06d2ffc3 N |
483 | } |
484 | return use; | |
485 | } | |
2eba8496 N |
486 | |
487 | unsigned long GCD(unsigned long a, unsigned long b) | |
488 | { | |
489 | while (a != b) { | |
490 | if (a < b) | |
491 | b -= a; | |
492 | if (b < a) | |
493 | a -= b; | |
494 | } | |
495 | return a; | |
496 | } | |
d0c017a6 N |
497 | |
498 | /* | |
499 | * conf_line reads one logical line from the conffile or mdstat. | |
500 | * It skips comments and continues until it finds a line that starts | |
501 | * with a non blank/comment. This character is pushed back for the next call | |
502 | * A doubly linked list of words is returned. | |
503 | * the first word will be a keyword. Other words will have had quotes removed. | |
504 | */ | |
505 | ||
506 | char *conf_line(FILE *file) | |
507 | { | |
508 | char *w; | |
509 | char *list; | |
510 | ||
511 | w = conf_word(file, 1); | |
781f7efb JS |
512 | if (w == NULL) |
513 | return NULL; | |
d0c017a6 N |
514 | |
515 | list = dl_strdup(w); | |
516 | free(w); | |
517 | dl_init(list); | |
518 | ||
ad7ac9ac | 519 | while ((w = conf_word(file, 0))){ |
d0c017a6 N |
520 | char *w2 = dl_strdup(w); |
521 | free(w); | |
522 | dl_add(list, w2); | |
523 | } | |
524 | /* printf("got a line\n");*/ | |
525 | return list; | |
526 | } | |
527 | ||
528 | void free_line(char *line) | |
529 | { | |
530 | char *w; | |
ad7ac9ac | 531 | for (w = dl_next(line); w != line; w = dl_next(line)) { |
d0c017a6 N |
532 | dl_del(w); |
533 | dl_free(w); | |
534 | } | |
535 | dl_free(line); | |
536 | } |