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