]> git.ipfire.org Git - thirdparty/mdadm.git/blob - mdstat.c
Merge branch 'master' into devel-3.0
[thirdparty/mdadm.git] / mdstat.c
1 /*
2 * mdstat - parse /proc/mdstat file. Part of:
3 * mdadm - manage Linux "md" devices aka RAID arrays.
4 *
5 * Copyright (C) 2002-2006 Neil Brown <neilb@suse.de>
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
23 * Email: <neilb@cse.unsw.edu.au>
24 * Paper: Neil Brown
25 * School of Computer Science and Engineering
26 * The University of New South Wales
27 * Sydney, 2052
28 * Australia
29 */
30
31 /*
32 * The /proc/mdstat file comes in at least 3 flavours:
33 * In an unpatched 2.2 kernel (md 0.36.6):
34 * Personalities : [n raidx] ...
35 * read_ahead {not set|%d sectors}
36 * md0 : {in}active{ raidX /dev/hda... %d blocks{ maxfault=%d}}
37 * md1 : .....
38 *
39 * Normally only 4 md lines, but all are listed.
40 *
41 * In a patched 2.2 kernel (md 0.90.0)
42 * Personalities : [raidx] ...
43 * read_ahead {not set|%d sectors}
44 * mdN : {in}active {(readonly)} raidX dev[%d]{(F)} ... %d blocks STATUS RESYNC
45 * ... Only initialised arrays listed
46 * unused devices: {dev dev ... | <none>}
47 *
48 * STATUS is personality dependant:
49 * linear: %dk rounding
50 * raid0: %dk chunks
51 * raid1: [%d/%d] [U_U] ( raid/working. operational or not)
52 * raid5: level 4/5, %dk chunk, algorithm %d [%d/%d] [U_U]
53 *
54 * RESYNC is empty or:
55 * {resync|recovery}=%u%% finish=%u.%umin
56 * or
57 * resync=DELAYED
58 *
59 * In a 2.4 kernel (md 0.90.0/2.4)
60 * Personalities : [raidX] ...
61 * read_ahead {not set|%d sectors}
62 * mdN : {in}active {(read-only)} raidX dev[%d]{(F)} ...
63 * %d blocks STATUS
64 * RESYNC
65 * unused devices: {dev dev .. | <none>}
66 *
67 * STATUS matches 0.90.0/2.2
68 * RESYNC includes [===>....],
69 * adds a space after {resync|recovery} and before and after '='
70 * adds a decimal to the recovery percent.
71 * adds (%d/%d) resync amount and max_blocks, before finish.
72 * adds speed=%dK/sec after finish
73 *
74 *
75 *
76 * Out of this we want to extract:
77 * list of devices, active or not
78 * pattern of failed drives (so need number of drives)
79 * percent resync complete
80 *
81 * As continuation is indicated by leading space, we use
82 * conf_line from config.c to read logical lines
83 *
84 */
85
86 #include "mdadm.h"
87 #include "dlink.h"
88 #include <sys/select.h>
89 #include <ctype.h>
90
91 void free_mdstat(struct mdstat_ent *ms)
92 {
93 while (ms) {
94 struct mdstat_ent *t;
95 if (ms->dev) free(ms->dev);
96 if (ms->level) free(ms->level);
97 if (ms->pattern) free(ms->pattern);
98 if (ms->metadata_version) free(ms->metadata_version);
99 t = ms;
100 ms = ms->next;
101 free(t);
102 }
103 }
104
105 static int mdstat_fd = -1;
106 struct mdstat_ent *mdstat_read(int hold, int start)
107 {
108 FILE *f;
109 struct mdstat_ent *all, *rv, **end, **insert_here;
110 char *line;
111
112 if (hold && mdstat_fd != -1) {
113 lseek(mdstat_fd, 0L, 0);
114 f = fdopen(dup(mdstat_fd), "r");
115 } else
116 f = fopen("/proc/mdstat", "r");
117 if (f == NULL)
118 return NULL;
119 else
120 fcntl(fileno(f), F_SETFD, FD_CLOEXEC);
121
122 all = NULL;
123 end = &all;
124 for (; (line = conf_line(f)) ; free_line(line)) {
125 struct mdstat_ent *ent;
126 char *w;
127 int devnum;
128 int in_devs = 0;
129 char *ep;
130
131 if (strcmp(line, "Personalities")==0)
132 continue;
133 if (strcmp(line, "read_ahead")==0)
134 continue;
135 if (strcmp(line, "unused")==0)
136 continue;
137 insert_here = NULL;
138 /* Better be an md line.. */
139 if (strncmp(line, "md", 2)!= 0)
140 continue;
141 if (strncmp(line, "md_d", 4) == 0)
142 devnum = -1-strtoul(line+4, &ep, 10);
143 else if (strncmp(line, "md", 2) == 0)
144 devnum = strtoul(line+2, &ep, 10);
145 else
146 continue;
147 if (ep == NULL || *ep ) {
148 /* fprintf(stderr, Name ": bad /proc/mdstat line starts: %s\n", line); */
149 continue;
150 }
151
152 ent = malloc(sizeof(*ent));
153 if (!ent) {
154 fprintf(stderr, Name ": malloc failed reading /proc/mdstat.\n");
155 free_line(line);
156 break;
157 }
158 ent->dev = ent->level = ent->pattern= NULL;
159 ent->next = NULL;
160 ent->percent = -1;
161 ent->active = -1;
162 ent->resync = 0;
163 ent->metadata_version = NULL;
164 ent->raid_disks = 0;
165 ent->chunk_size = 0;
166 ent->devcnt = 0;
167
168 ent->dev = strdup(line);
169 ent->devnum = devnum;
170
171 for (w=dl_next(line); w!= line ; w=dl_next(w)) {
172 int l = strlen(w);
173 char *eq;
174 if (strcmp(w, "active")==0)
175 ent->active = 1;
176 else if (strcmp(w, "inactive")==0)
177 ent->active = 0;
178 else if (ent->active >=0 &&
179 ent->level == NULL &&
180 w[0] != '(' /*readonly*/) {
181 ent->level = strdup(w);
182 in_devs = 1;
183 } else if (in_devs && strcmp(w, "blocks")==0)
184 in_devs = 0;
185 else if (in_devs) {
186 ent->devcnt++;
187 if (strncmp(w, "md", 2)==0) {
188 /* This has an md device as a component.
189 * If that device is already in the
190 * list, make sure we insert before
191 * there.
192 */
193 struct mdstat_ent **ih;
194 int dn2 = devname2devnum(w);
195 ih = &all;
196 while (ih != insert_here && *ih &&
197 (*ih)->devnum != dn2)
198 ih = & (*ih)->next;
199 insert_here = ih;
200 }
201 } else if (strcmp(w, "super") == 0 &&
202 dl_next(w) != line) {
203 w = dl_next(w);
204 ent->metadata_version = strdup(w);
205 } else if (w[0] == '[' && isdigit(w[1])) {
206 ent->raid_disks = atoi(w+1);
207 } else if (!ent->pattern &&
208 w[0] == '[' &&
209 (w[1] == 'U' || w[1] == '_')) {
210 ent->pattern = strdup(w+1);
211 if (ent->pattern[l-2]==']')
212 ent->pattern[l-2] = '\0';
213 } else if (ent->percent == -1 &&
214 strncmp(w, "re", 2)== 0 &&
215 w[l-1] == '%' &&
216 (eq=strchr(w, '=')) != NULL ) {
217 ent->percent = atoi(eq+1);
218 if (strncmp(w,"resync", 4)==0)
219 ent->resync = 1;
220 } else if (ent->percent == -1 &&
221 strncmp(w, "resync", 4)==0) {
222 ent->resync = 1;
223 } else if (ent->percent == -1 &&
224 w[0] >= '0' &&
225 w[0] <= '9' &&
226 w[l-1] == '%') {
227 ent->percent = atoi(w);
228 }
229 }
230 if (insert_here && (*insert_here)) {
231 ent->next = *insert_here;
232 *insert_here = ent;
233 } else {
234 *end = ent;
235 end = &ent->next;
236 }
237 }
238 if (hold && mdstat_fd == -1) {
239 mdstat_fd = dup(fileno(f));
240 fcntl(mdstat_fd, F_SETFD, FD_CLOEXEC);
241 }
242 fclose(f);
243
244 /* If we might want to start array,
245 * reverse the order, so that components comes before composites
246 */
247 if (start) {
248 rv = NULL;
249 while (all) {
250 struct mdstat_ent *e = all;
251 all = all->next;
252 e->next = rv;
253 rv = e;
254 }
255 } else rv = all;
256 return rv;
257 }
258
259 void mdstat_wait(int seconds)
260 {
261 fd_set fds;
262 struct timeval tm;
263 int maxfd = 0;
264 FD_ZERO(&fds);
265 if (mdstat_fd >= 0) {
266 FD_SET(mdstat_fd, &fds);
267 maxfd = mdstat_fd;
268 }
269 tm.tv_sec = seconds;
270 tm.tv_usec = 0;
271 select(maxfd + 1, NULL, NULL, &fds, &tm);
272 }
273
274 void mdstat_wait_fd(int fd, const sigset_t *sigmask)
275 {
276 fd_set fds, rfds;
277 int maxfd = fd;
278
279 FD_ZERO(&fds);
280 FD_ZERO(&rfds);
281 if (mdstat_fd >= 0)
282 FD_SET(mdstat_fd, &fds);
283 if (fd >= 0)
284 FD_SET(fd, &rfds);
285 if (mdstat_fd > maxfd)
286 maxfd = mdstat_fd;
287
288 pselect(maxfd + 1, &rfds, NULL, &fds,
289 NULL, sigmask);
290 }
291
292 int mddev_busy(int devnum)
293 {
294 struct mdstat_ent *mdstat = mdstat_read(0, 0);
295 struct mdstat_ent *me;
296
297 for (me = mdstat ; me ; me = me->next)
298 if (me->devnum == devnum)
299 break;
300 free_mdstat(mdstat);
301 return me != NULL;
302 }