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