]> git.ipfire.org Git - thirdparty/mdadm.git/blame - mdstat.c
Add mbr pseudo metadata handler.
[thirdparty/mdadm.git] / mdstat.c
CommitLineData
e0d19036
NB
1/*
2 * mdstat - parse /proc/mdstat file. Part of:
3 * mdadm - manage Linux "md" devices aka RAID arrays.
4 *
e736b623 5 * Copyright (C) 2002-2009 Neil Brown <neilb@suse.de>
e0d19036
NB
6 *
7 *
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
21 *
22 * Author: Neil Brown
e736b623 23 * Email: <neilb@suse.de>
e0d19036
NB
24 */
25
26/*
27 * The /proc/mdstat file comes in at least 3 flavours:
28 * In an unpatched 2.2 kernel (md 0.36.6):
29 * Personalities : [n raidx] ...
30 * read_ahead {not set|%d sectors}
31 * md0 : {in}active{ raidX /dev/hda... %d blocks{ maxfault=%d}}
32 * md1 : .....
33 *
34 * Normally only 4 md lines, but all are listed.
35 *
36 * In a patched 2.2 kernel (md 0.90.0)
37 * Personalities : [raidx] ...
38 * read_ahead {not set|%d sectors}
39 * mdN : {in}active {(readonly)} raidX dev[%d]{(F)} ... %d blocks STATUS RESYNC
40 * ... Only initialised arrays listed
8f21823f 41 * unused devices: {dev dev ... | <none>}
e0d19036
NB
42 *
43 * STATUS is personality dependant:
44 * linear: %dk rounding
45 * raid0: %dk chunks
46 * raid1: [%d/%d] [U_U] ( raid/working. operational or not)
47 * raid5: level 4/5, %dk chunk, algorithm %d [%d/%d] [U_U]
48 *
49 * RESYNC is empty or:
50 * {resync|recovery}=%u%% finish=%u.%umin
51 * or
52 * resync=DELAYED
53 *
54 * In a 2.4 kernel (md 0.90.0/2.4)
55 * Personalities : [raidX] ...
56 * read_ahead {not set|%d sectors}
57 * mdN : {in}active {(read-only)} raidX dev[%d]{(F)} ...
58 * %d blocks STATUS
59 * RESYNC
8f21823f 60 * unused devices: {dev dev .. | <none>}
e0d19036
NB
61 *
62 * STATUS matches 0.90.0/2.2
63 * RESYNC includes [===>....],
64 * adds a space after {resync|recovery} and before and after '='
65 * adds a decimal to the recovery percent.
66 * adds (%d/%d) resync amount and max_blocks, before finish.
67 * adds speed=%dK/sec after finish
68 *
69 *
70 *
71 * Out of this we want to extract:
72 * list of devices, active or not
73 * pattern of failed drives (so need number of drives)
74 * percent resync complete
75 *
76 * As continuation is indicated by leading space, we use
77 * conf_line from config.c to read logical lines
78 *
79 */
80
81#include "mdadm.h"
82#include "dlink.h"
dd0781e5 83#include <sys/select.h>
549e9569 84#include <ctype.h>
e0d19036 85
3b57c466
N
86static void free_member_devnames(struct dev_member *m)
87{
88 while(m) {
89 struct dev_member *t = m;
90
91 m = m->next;
92 free(t->name);
93 free(t);
94 }
95}
96
0ad6835c 97static int add_member_devname(struct dev_member **m, char *name)
3b57c466
N
98{
99 struct dev_member *new;
100 char *t;
101
102 if ((t = strchr(name, '[')) == NULL)
103 /* not a device */
0ad6835c 104 return 0;
3b57c466
N
105
106 new = malloc(sizeof(*new));
107 new->name = strndup(name, t - name);
108 new->next = *m;
109 *m = new;
0ad6835c 110 return 1;
3b57c466
N
111}
112
e0d19036
NB
113void free_mdstat(struct mdstat_ent *ms)
114{
115 while (ms) {
116 struct mdstat_ent *t;
3b57c466
N
117 free(ms->dev);
118 free(ms->level);
119 free(ms->pattern);
120 free(ms->metadata_version);
121 free_member_devnames(ms->members);
e0d19036
NB
122 t = ms;
123 ms = ms->next;
124 free(t);
125 }
126}
127
dd0781e5 128static int mdstat_fd = -1;
22a88995 129struct mdstat_ent *mdstat_read(int hold, int start)
e0d19036
NB
130{
131 FILE *f;
22a88995 132 struct mdstat_ent *all, *rv, **end, **insert_here;
e0d19036
NB
133 char *line;
134
dd0781e5
NB
135 if (hold && mdstat_fd != -1) {
136 lseek(mdstat_fd, 0L, 0);
137 f = fdopen(dup(mdstat_fd), "r");
138 } else
139 f = fopen("/proc/mdstat", "r");
e0d19036
NB
140 if (f == NULL)
141 return NULL;
e4dc5106
DL
142 else
143 fcntl(fileno(f), F_SETFD, FD_CLOEXEC);
e0d19036
NB
144
145 all = NULL;
146 end = &all;
147 for (; (line = conf_line(f)) ; free_line(line)) {
148 struct mdstat_ent *ent;
149 char *w;
98c6faba 150 int devnum;
22a88995 151 int in_devs = 0;
98c6faba 152 char *ep;
e0d19036
NB
153
154 if (strcmp(line, "Personalities")==0)
155 continue;
156 if (strcmp(line, "read_ahead")==0)
157 continue;
158 if (strcmp(line, "unused")==0)
159 continue;
22a88995 160 insert_here = NULL;
e0d19036 161 /* Better be an md line.. */
98c6faba
NB
162 if (strncmp(line, "md", 2)!= 0)
163 continue;
164 if (strncmp(line, "md_d", 4) == 0)
165 devnum = -1-strtoul(line+4, &ep, 10);
166 else if (strncmp(line, "md", 2) == 0)
167 devnum = strtoul(line+2, &ep, 10);
168 else
169 continue;
170 if (ep == NULL || *ep ) {
171 /* fprintf(stderr, Name ": bad /proc/mdstat line starts: %s\n", line); */
e0d19036
NB
172 continue;
173 }
174
175 ent = malloc(sizeof(*ent));
176 if (!ent) {
177 fprintf(stderr, Name ": malloc failed reading /proc/mdstat.\n");
178 free_line(line);
dd0781e5 179 break;
e0d19036
NB
180 }
181 ent->dev = ent->level = ent->pattern= NULL;
182 ent->next = NULL;
183 ent->percent = -1;
184 ent->active = -1;
e5329c37 185 ent->resync = 0;
549e9569
NB
186 ent->metadata_version = NULL;
187 ent->raid_disks = 0;
188 ent->chunk_size = 0;
189 ent->devcnt = 0;
3b57c466 190 ent->members = NULL;
e0d19036
NB
191
192 ent->dev = strdup(line);
98c6faba 193 ent->devnum = devnum;
aba69144 194
e0d19036
NB
195 for (w=dl_next(line); w!= line ; w=dl_next(w)) {
196 int l = strlen(w);
197 char *eq;
198 if (strcmp(w, "active")==0)
199 ent->active = 1;
b6d7a7fb 200 else if (strcmp(w, "inactive")==0) {
e0d19036 201 ent->active = 0;
b6d7a7fb
JD
202 in_devs = 1;
203 } else if (ent->active > 0 &&
e0d19036 204 ent->level == NULL &&
22a88995 205 w[0] != '(' /*readonly*/) {
e0d19036 206 ent->level = strdup(w);
22a88995
NB
207 in_devs = 1;
208 } else if (in_devs && strcmp(w, "blocks")==0)
209 in_devs = 0;
549e9569 210 else if (in_devs) {
0ad6835c
ML
211 ent->devcnt +=
212 add_member_devname(&ent->members, w);
549e9569
NB
213 if (strncmp(w, "md", 2)==0) {
214 /* This has an md device as a component.
215 * If that device is already in the
216 * list, make sure we insert before
217 * there.
218 */
219 struct mdstat_ent **ih;
77472ff8 220 int dn2 = devname2devnum(w);
549e9569
NB
221 ih = &all;
222 while (ih != insert_here && *ih &&
223 (*ih)->devnum != dn2)
224 ih = & (*ih)->next;
225 insert_here = ih;
226 }
227 } else if (strcmp(w, "super") == 0 &&
228 dl_next(w) != line) {
229 w = dl_next(w);
230 ent->metadata_version = strdup(w);
231 } else if (w[0] == '[' && isdigit(w[1])) {
232 ent->raid_disks = atoi(w+1);
22a88995 233 } else if (!ent->pattern &&
e0d19036
NB
234 w[0] == '[' &&
235 (w[1] == 'U' || w[1] == '_')) {
236 ent->pattern = strdup(w+1);
237 if (ent->pattern[l-2]==']')
238 ent->pattern[l-2] = '\0';
239 } else if (ent->percent == -1 &&
240 strncmp(w, "re", 2)== 0 &&
241 w[l-1] == '%' &&
242 (eq=strchr(w, '=')) != NULL ) {
243 ent->percent = atoi(eq+1);
e5329c37
NB
244 if (strncmp(w,"resync", 4)==0)
245 ent->resync = 1;
246 } else if (ent->percent == -1 &&
247 strncmp(w, "resync", 4)==0) {
248 ent->resync = 1;
e0d19036 249 } else if (ent->percent == -1 &&
aba69144 250 w[0] >= '0' &&
e0d19036
NB
251 w[0] <= '9' &&
252 w[l-1] == '%') {
253 ent->percent = atoi(w);
254 }
255 }
22a88995
NB
256 if (insert_here && (*insert_here)) {
257 ent->next = *insert_here;
258 *insert_here = ent;
259 } else {
260 *end = ent;
261 end = &ent->next;
262 }
e0d19036 263 }
e4dc5106 264 if (hold && mdstat_fd == -1) {
dd0781e5 265 mdstat_fd = dup(fileno(f));
e4dc5106
DL
266 fcntl(mdstat_fd, F_SETFD, FD_CLOEXEC);
267 }
e0d19036 268 fclose(f);
22a88995
NB
269
270 /* If we might want to start array,
271 * reverse the order, so that components comes before composites
272 */
273 if (start) {
274 rv = NULL;
275 while (all) {
276 struct mdstat_ent *e = all;
277 all = all->next;
278 e->next = rv;
279 rv = e;
280 }
281 } else rv = all;
282 return rv;
e0d19036 283}
dd0781e5
NB
284
285void mdstat_wait(int seconds)
286{
287 fd_set fds;
288 struct timeval tm;
0b5ec75e 289 int maxfd = 0;
dd0781e5 290 FD_ZERO(&fds);
0b5ec75e 291 if (mdstat_fd >= 0) {
dd0781e5 292 FD_SET(mdstat_fd, &fds);
0b5ec75e
N
293 maxfd = mdstat_fd;
294 }
dd0781e5
NB
295 tm.tv_sec = seconds;
296 tm.tv_usec = 0;
0b5ec75e 297 select(maxfd + 1, NULL, NULL, &fds, &tm);
dd0781e5 298}
8382f19b 299
58a4ba2a 300void mdstat_wait_fd(int fd, const sigset_t *sigmask)
549e9569
NB
301{
302 fd_set fds, rfds;
5d4d1b26 303 int maxfd = 0;
549e9569
NB
304
305 FD_ZERO(&fds);
306 FD_ZERO(&rfds);
307 if (mdstat_fd >= 0)
308 FD_SET(mdstat_fd, &fds);
5d4d1b26 309
58a4ba2a 310 if (fd >= 0) {
28005287
N
311 struct stat stb;
312 fstat(fd, &stb);
313 if ((stb.st_mode & S_IFMT) == S_IFREG)
314 /* Must be a /proc or /sys fd, so expect
315 * POLLPRI
316 * i.e. an 'exceptional' event.
317 */
318 FD_SET(fd, &fds);
319 else
320 FD_SET(fd, &rfds);
5d4d1b26
N
321
322 if (fd > maxfd)
323 maxfd = fd;
324
28005287 325 }
0b5ec75e
N
326 if (mdstat_fd > maxfd)
327 maxfd = mdstat_fd;
549e9569 328
0b5ec75e 329 pselect(maxfd + 1, &rfds, NULL, &fds,
1ed3f387 330 NULL, sigmask);
549e9569
NB
331}
332
8382f19b
NB
333int mddev_busy(int devnum)
334{
335 struct mdstat_ent *mdstat = mdstat_read(0, 0);
336 struct mdstat_ent *me;
337
338 for (me = mdstat ; me ; me = me->next)
339 if (me->devnum == devnum)
340 break;
341 free_mdstat(mdstat);
342 return me != NULL;
343}
3b57c466
N
344
345struct mdstat_ent *mdstat_by_component(char *name)
346{
347 struct mdstat_ent *mdstat = mdstat_read(0, 0);
348
349 while (mdstat) {
350 struct dev_member *m;
351 struct mdstat_ent *ent;
352 if (mdstat->metadata_version &&
353 strncmp(mdstat->metadata_version, "external:", 9) == 0 &&
354 is_subarray(mdstat->metadata_version+9))
355 /* don't return subarrays, only containers */
356 ;
357 else for (m = mdstat->members; m; m = m->next) {
358 if (strcmp(m->name, name) == 0) {
359 free_mdstat(mdstat->next);
360 mdstat->next = NULL;
361 return mdstat;
362 }
363 }
364 ent = mdstat;
365 mdstat = mdstat->next;
366 ent->next = NULL;
367 free_mdstat(ent);
368 }
369 return NULL;
370}