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