]> git.ipfire.org Git - thirdparty/mdadm.git/blob - lib.c
Release 3.2.6 - stability release
[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 <ctype.h>
27
28 /* This fill contains various 'library' style function. They
29 * have no dependency on anything outside this file.
30 */
31
32 int get_mdp_major(void)
33 {
34 static int mdp_major = -1;
35 FILE *fl;
36 char *w;
37 int have_block = 0;
38 int have_devices = 0;
39 int last_num = -1;
40
41 if (mdp_major != -1)
42 return mdp_major;
43 fl = fopen("/proc/devices", "r");
44 if (!fl)
45 return -1;
46 while ((w = conf_word(fl, 1))) {
47 if (have_block && strcmp(w, "devices:")==0)
48 have_devices = 1;
49 have_block = (strcmp(w, "Block")==0);
50 if (isdigit(w[0]))
51 last_num = atoi(w);
52 if (have_devices && strcmp(w, "mdp")==0)
53 mdp_major = last_num;
54 free(w);
55 }
56 fclose(fl);
57 return mdp_major;
58 }
59
60
61 void fmt_devname(char *name, int num)
62 {
63 if (num >= 0)
64 sprintf(name, "md%d", num);
65 else
66 sprintf(name, "md_d%d", -1-num);
67 }
68
69 char *devnum2devname(int num)
70 {
71 char name[100];
72 fmt_devname(name,num);
73 return strdup(name);
74 }
75
76 int devname2devnum(char *name)
77 {
78 char *ep;
79 int num;
80 if (strncmp(name, "md_d", 4)==0)
81 num = -1-strtoul(name+4, &ep, 10);
82 else
83 num = strtoul(name+2, &ep, 10);
84 return num;
85 }
86
87 int stat2devnum(struct stat *st)
88 {
89 char path[30];
90 char link[200];
91 char *cp;
92 int n;
93
94 if ((S_IFMT & st->st_mode) == S_IFBLK) {
95 if (major(st->st_rdev) == MD_MAJOR)
96 return minor(st->st_rdev);
97 else if (major(st->st_rdev) == (unsigned)get_mdp_major())
98 return -1- (minor(st->st_rdev)>>MdpMinorShift);
99
100 /* must be an extended-minor partition. Look at the
101 * /sys/dev/block/%d:%d link which must look like
102 * ../../block/mdXXX/mdXXXpYY
103 */
104 sprintf(path, "/sys/dev/block/%d:%d", major(st->st_rdev),
105 minor(st->st_rdev));
106 n = readlink(path, link, sizeof(link)-1);
107 if (n <= 0)
108 return NoMdDev;
109 link[n] = 0;
110 cp = strrchr(link, '/');
111 if (cp) *cp = 0;
112 cp = strrchr(link, '/');
113 if (cp && strncmp(cp, "/md", 3) == 0)
114 return devname2devnum(cp+1);
115 }
116 return NoMdDev;
117
118 }
119
120 int fd2devnum(int fd)
121 {
122 struct stat stb;
123 if (fstat(fd, &stb) == 0)
124 return stat2devnum(&stb);
125 return NoMdDev;
126 }
127
128
129
130 /*
131 * convert a major/minor pair for a block device into a name in /dev, if possible.
132 * On the first call, walk /dev collecting name.
133 * Put them in a simple linked listfor now.
134 */
135 struct devmap {
136 int major, minor;
137 char *name;
138 struct devmap *next;
139 } *devlist = NULL;
140 int devlist_ready = 0;
141
142 int add_dev(const char *name, const struct stat *stb, int flag, struct FTW *s)
143 {
144 struct stat st;
145
146 if (S_ISLNK(stb->st_mode)) {
147 if (stat(name, &st) != 0)
148 return 0;
149 stb = &st;
150 }
151
152 if ((stb->st_mode&S_IFMT)== S_IFBLK) {
153 char *n = strdup(name);
154 struct devmap *dm = malloc(sizeof(*dm));
155 if (strncmp(n, "/dev/./", 7)==0)
156 strcpy(n+4, name+6);
157 if (dm) {
158 dm->major = major(stb->st_rdev);
159 dm->minor = minor(stb->st_rdev);
160 dm->name = n;
161 dm->next = devlist;
162 devlist = dm;
163 }
164 }
165 return 0;
166 }
167
168 #ifndef HAVE_NFTW
169 #ifdef HAVE_FTW
170 int add_dev_1(const char *name, const struct stat *stb, int flag)
171 {
172 return add_dev(name, stb, flag, NULL);
173 }
174 int nftw(const char *path, int (*han)(const char *name, const struct stat *stb, int flag, struct FTW *s), int nopenfd, int flags)
175 {
176 return ftw(path, add_dev_1, nopenfd);
177 }
178 #else
179 int nftw(const char *path, int (*han)(const char *name, const struct stat *stb, int flag, struct FTW *s), int nopenfd, int flags)
180 {
181 return 0;
182 }
183 #endif /* HAVE_FTW */
184 #endif /* HAVE_NFTW */
185
186 /*
187 * Find a block device with the right major/minor number.
188 * If we find multiple names, choose the shortest.
189 * If we find a name in /dev/md/, we prefer that.
190 * This applies only to names for MD devices.
191 * If 'prefer' is set (normally to e.g. /by-path/)
192 * then we prefer a name which contains that string.
193 */
194 char *map_dev_preferred(int major, int minor, int create,
195 char *prefer)
196 {
197 struct devmap *p;
198 char *regular = NULL, *preferred=NULL;
199 int did_check = 0;
200
201 if (major == 0 && minor == 0)
202 return NULL;
203
204 retry:
205 if (!devlist_ready) {
206 char *dev = "/dev";
207 struct stat stb;
208 while(devlist) {
209 struct devmap *d = devlist;
210 devlist = d->next;
211 free(d->name);
212 free(d);
213 }
214 if (lstat(dev, &stb)==0 &&
215 S_ISLNK(stb.st_mode))
216 dev = "/dev/.";
217 nftw(dev, add_dev, 10, FTW_PHYS);
218 devlist_ready=1;
219 did_check = 1;
220 }
221
222 for (p=devlist; p; p=p->next)
223 if (p->major == major &&
224 p->minor == minor) {
225 if (strncmp(p->name, "/dev/md/",8) == 0
226 || (prefer && strstr(p->name, prefer))) {
227 if (preferred == NULL ||
228 strlen(p->name) < strlen(preferred))
229 preferred = p->name;
230 } else {
231 if (regular == NULL ||
232 strlen(p->name) < strlen(regular))
233 regular = p->name;
234 }
235 }
236 if (!regular && !preferred && !did_check) {
237 devlist_ready = 0;
238 goto retry;
239 }
240 if (create && !regular && !preferred) {
241 static char buf[30];
242 snprintf(buf, sizeof(buf), "%d:%d", major, minor);
243 regular = buf;
244 }
245
246 return preferred ? preferred : regular;
247 }
248
249
250
251 /* conf_word gets one word from the conf file.
252 * if "allow_key", then accept words at the start of a line,
253 * otherwise stop when such a word is found.
254 * We assume that the file pointer is at the end of a word, so the
255 * next character is a space, or a newline. If not, it is the start of a line.
256 */
257
258 char *conf_word(FILE *file, int allow_key)
259 {
260 int wsize = 100;
261 int len = 0;
262 int c;
263 int quote;
264 int wordfound = 0;
265 char *word = malloc(wsize);
266
267 if (!word) abort();
268
269 while (wordfound==0) {
270 /* at the end of a word.. */
271 c = getc(file);
272 if (c == '#')
273 while (c != EOF && c != '\n')
274 c = getc(file);
275 if (c == EOF) break;
276 if (c == '\n') continue;
277
278 if (c != ' ' && c != '\t' && ! allow_key) {
279 ungetc(c, file);
280 break;
281 }
282 /* looks like it is safe to get a word here, if there is one */
283 quote = 0;
284 /* first, skip any spaces */
285 while (c == ' ' || c == '\t')
286 c = getc(file);
287 if (c != EOF && c != '\n' && c != '#') {
288 /* we really have a character of a word, so start saving it */
289 while (c != EOF && c != '\n' && (quote || (c!=' ' && c != '\t'))) {
290 wordfound = 1;
291 if (quote && c == quote) quote = 0;
292 else if (quote == 0 && (c == '\'' || c == '"'))
293 quote = c;
294 else {
295 if (len == wsize-1) {
296 wsize += 100;
297 word = realloc(word, wsize);
298 if (!word) abort();
299 }
300 word[len++] = c;
301 }
302 c = getc(file);
303 /* Hack for broken kernels (2.6.14-.24) that put
304 * "active(auto-read-only)"
305 * in /proc/mdstat instead of
306 * "active (auto-read-only)"
307 */
308 if (c == '(' && len >= 6
309 && strncmp(word+len-6, "active", 6) == 0)
310 c = ' ';
311 }
312 }
313 if (c != EOF) ungetc(c, file);
314 }
315 word[len] = 0;
316
317 /* Further HACK for broken kernels.. 2.6.14-2.6.24 */
318 if (strcmp(word, "auto-read-only)") == 0)
319 strcpy(word, "(auto-read-only)");
320
321 /* printf("word is <%s>\n", word); */
322 if (!wordfound) {
323 free(word);
324 word = NULL;
325 }
326 return word;
327 }
328
329 void print_quoted(char *str)
330 {
331 /* Printf the string with surrounding quotes
332 * iff needed.
333 * If no space, tab, or quote - leave unchanged.
334 * Else print surrounded by " or ', swapping quotes
335 * when we find one that will cause confusion.
336 */
337
338 char first_quote = 0, q;
339 char *c;
340
341 for (c = str; *c; c++) {
342 switch(*c) {
343 case '\'':
344 case '"':
345 first_quote = *c;
346 break;
347 case ' ':
348 case '\t':
349 first_quote = *c;
350 continue;
351 default:
352 continue;
353 }
354 break;
355 }
356 if (!first_quote) {
357 printf("%s", str);
358 return;
359 }
360
361 if (first_quote == '"')
362 q = '\'';
363 else
364 q = '"';
365 putchar(q);
366 for (c = str; *c; c++) {
367 if (*c == q) {
368 putchar(q);
369 q ^= '"' ^ '\'';
370 putchar(q);
371 }
372 putchar(*c);
373 }
374 putchar(q);
375 }
376
377 void print_escape(char *str)
378 {
379 /* print str, but change space and tab to '_'
380 * as is suitable for device names
381 */
382 for (; *str ; str++) {
383 switch (*str) {
384 case ' ':
385 case '\t':
386 putchar('_');
387 break;
388 case '/':
389 putchar('-');
390 break;
391 default:
392 putchar(*str);
393 }
394 }
395 }