]> git.ipfire.org Git - thirdparty/mdadm.git/blame - lib.c
Create: support --readonly flag.
[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);
503975b9 73 return xstrdup(name);
78c0a3b1
N
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) {
503975b9
N
153 char *n = xstrdup(name);
154 struct devmap *dm = xmalloc(sizeof(*dm));
78c0a3b1
N
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.
c2ecf5f6
N
191 * If 'prefer' is set (normally to e.g. /by-path/)
192 * then we prefer a name which contains that string.
78c0a3b1 193 */
c2ecf5f6
N
194char *map_dev_preferred(int major, int minor, int create,
195 char *prefer)
78c0a3b1
N
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) {
c2ecf5f6
N
225 if (strncmp(p->name, "/dev/md/",8) == 0
226 || (prefer && strstr(p->name, prefer))) {
78c0a3b1
N
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
c2ecf5f6 250
78c0a3b1
N
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
258char *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;
503975b9 265 char *word = xmalloc(wsize);
78c0a3b1
N
266
267 while (wordfound==0) {
268 /* at the end of a word.. */
269 c = getc(file);
270 if (c == '#')
271 while (c != EOF && c != '\n')
272 c = getc(file);
273 if (c == EOF) break;
274 if (c == '\n') continue;
275
276 if (c != ' ' && c != '\t' && ! allow_key) {
277 ungetc(c, file);
278 break;
279 }
280 /* looks like it is safe to get a word here, if there is one */
281 quote = 0;
282 /* first, skip any spaces */
283 while (c == ' ' || c == '\t')
284 c = getc(file);
285 if (c != EOF && c != '\n' && c != '#') {
286 /* we really have a character of a word, so start saving it */
287 while (c != EOF && c != '\n' && (quote || (c!=' ' && c != '\t'))) {
288 wordfound = 1;
289 if (quote && c == quote) quote = 0;
290 else if (quote == 0 && (c == '\'' || c == '"'))
291 quote = c;
292 else {
293 if (len == wsize-1) {
294 wsize += 100;
503975b9 295 word = xrealloc(word, wsize);
78c0a3b1
N
296 }
297 word[len++] = c;
298 }
299 c = getc(file);
300 /* Hack for broken kernels (2.6.14-.24) that put
301 * "active(auto-read-only)"
302 * in /proc/mdstat instead of
303 * "active (auto-read-only)"
304 */
305 if (c == '(' && len >= 6
306 && strncmp(word+len-6, "active", 6) == 0)
307 c = ' ';
308 }
309 }
310 if (c != EOF) ungetc(c, file);
311 }
312 word[len] = 0;
313
314 /* Further HACK for broken kernels.. 2.6.14-2.6.24 */
315 if (strcmp(word, "auto-read-only)") == 0)
316 strcpy(word, "(auto-read-only)");
317
318/* printf("word is <%s>\n", word); */
319 if (!wordfound) {
320 free(word);
321 word = NULL;
322 }
323 return word;
324}