]> git.ipfire.org Git - thirdparty/mdadm.git/blame - lib.c
Check all member devices in enough_fd
[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"
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
32int get_mdp_major(void)
33{
34static 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
61void 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
69char *devnum2devname(int num)
70{
71 char name[100];
72 fmt_devname(name,num);
73 return strdup(name);
74}
75
76int 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
87int 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
120int 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 */
135struct devmap {
136 int major, minor;
137 char *name;
138 struct devmap *next;
139} *devlist = NULL;
140int devlist_ready = 0;
141
142int 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
170int add_dev_1(const char *name, const struct stat *stb, int flag)
171{
172 return add_dev(name, stb, flag, NULL);
173}
174int 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
179int 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 */
192char *map_dev(int major, int minor, int create)
193{
194 struct devmap *p;
195 char *regular = NULL, *preferred=NULL;
196 int did_check = 0;
197
198 if (major == 0 && minor == 0)
199 return NULL;
200
201 retry:
202 if (!devlist_ready) {
203 char *dev = "/dev";
204 struct stat stb;
205 while(devlist) {
206 struct devmap *d = devlist;
207 devlist = d->next;
208 free(d->name);
209 free(d);
210 }
211 if (lstat(dev, &stb)==0 &&
212 S_ISLNK(stb.st_mode))
213 dev = "/dev/.";
214 nftw(dev, add_dev, 10, FTW_PHYS);
215 devlist_ready=1;
216 did_check = 1;
217 }
218
219 for (p=devlist; p; p=p->next)
220 if (p->major == major &&
221 p->minor == minor) {
222 if (strncmp(p->name, "/dev/md/",8) == 0) {
223 if (preferred == NULL ||
224 strlen(p->name) < strlen(preferred))
225 preferred = p->name;
226 } else {
227 if (regular == NULL ||
228 strlen(p->name) < strlen(regular))
229 regular = p->name;
230 }
231 }
232 if (!regular && !preferred && !did_check) {
233 devlist_ready = 0;
234 goto retry;
235 }
236 if (create && !regular && !preferred) {
237 static char buf[30];
238 snprintf(buf, sizeof(buf), "%d:%d", major, minor);
239 regular = buf;
240 }
241
242 return preferred ? preferred : regular;
243}
244
245
246/* conf_word gets one word from the conf file.
247 * if "allow_key", then accept words at the start of a line,
248 * otherwise stop when such a word is found.
249 * We assume that the file pointer is at the end of a word, so the
250 * next character is a space, or a newline. If not, it is the start of a line.
251 */
252
253char *conf_word(FILE *file, int allow_key)
254{
255 int wsize = 100;
256 int len = 0;
257 int c;
258 int quote;
259 int wordfound = 0;
260 char *word = malloc(wsize);
261
262 if (!word) abort();
263
264 while (wordfound==0) {
265 /* at the end of a word.. */
266 c = getc(file);
267 if (c == '#')
268 while (c != EOF && c != '\n')
269 c = getc(file);
270 if (c == EOF) break;
271 if (c == '\n') continue;
272
273 if (c != ' ' && c != '\t' && ! allow_key) {
274 ungetc(c, file);
275 break;
276 }
277 /* looks like it is safe to get a word here, if there is one */
278 quote = 0;
279 /* first, skip any spaces */
280 while (c == ' ' || c == '\t')
281 c = getc(file);
282 if (c != EOF && c != '\n' && c != '#') {
283 /* we really have a character of a word, so start saving it */
284 while (c != EOF && c != '\n' && (quote || (c!=' ' && c != '\t'))) {
285 wordfound = 1;
286 if (quote && c == quote) quote = 0;
287 else if (quote == 0 && (c == '\'' || c == '"'))
288 quote = c;
289 else {
290 if (len == wsize-1) {
291 wsize += 100;
292 word = realloc(word, wsize);
293 if (!word) abort();
294 }
295 word[len++] = c;
296 }
297 c = getc(file);
298 /* Hack for broken kernels (2.6.14-.24) that put
299 * "active(auto-read-only)"
300 * in /proc/mdstat instead of
301 * "active (auto-read-only)"
302 */
303 if (c == '(' && len >= 6
304 && strncmp(word+len-6, "active", 6) == 0)
305 c = ' ';
306 }
307 }
308 if (c != EOF) ungetc(c, file);
309 }
310 word[len] = 0;
311
312 /* Further HACK for broken kernels.. 2.6.14-2.6.24 */
313 if (strcmp(word, "auto-read-only)") == 0)
314 strcpy(word, "(auto-read-only)");
315
316/* printf("word is <%s>\n", word); */
317 if (!wordfound) {
318 free(word);
319 word = NULL;
320 }
321 return word;
322}