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