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