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