]> git.ipfire.org Git - thirdparty/mdadm.git/blob - mdstat.c
Show all bitmaps while examining bitmap
[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-2009 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@suse.de>
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
41 * unused devices: {dev dev ... | <none>}
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
60 * unused devices: {dev dev .. | <none>}
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"
83 #include <sys/select.h>
84 #include <ctype.h>
85
86 static 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
97 static int add_member_devname(struct dev_member **m, char *name)
98 {
99 struct dev_member *new;
100 char *t;
101
102 if ((t = strchr(name, '[')) == NULL)
103 /* not a device */
104 return 0;
105
106 new = xmalloc(sizeof(*new));
107 new->name = strndup(name, t - name);
108 new->next = *m;
109 *m = new;
110 return 1;
111 }
112
113 void free_mdstat(struct mdstat_ent *ms)
114 {
115 while (ms) {
116 struct mdstat_ent *t;
117 free(ms->dev);
118 free(ms->level);
119 free(ms->pattern);
120 free(ms->metadata_version);
121 free_member_devnames(ms->members);
122 t = ms;
123 ms = ms->next;
124 free(t);
125 }
126 }
127
128 static int mdstat_fd = -1;
129 struct mdstat_ent *mdstat_read(int hold, int start)
130 {
131 FILE *f;
132 struct mdstat_ent *all, *rv, **end, **insert_here;
133 char *line;
134 int fd;
135
136 if (hold && mdstat_fd != -1) {
137 lseek(mdstat_fd, 0L, 0);
138 fd = dup(mdstat_fd);
139 if (fd >= 0)
140 f = fdopen(fd, "r");
141 else
142 return NULL;
143 } else
144 f = fopen("/proc/mdstat", "r");
145 if (f == NULL)
146 return NULL;
147 else
148 fcntl(fileno(f), F_SETFD, FD_CLOEXEC);
149
150 all = NULL;
151 end = &all;
152 for (; (line = conf_line(f)) ; free_line(line)) {
153 struct mdstat_ent *ent;
154 char *w;
155 char devnm[32];
156 int in_devs = 0;
157
158 if (strcmp(line, "Personalities")==0)
159 continue;
160 if (strcmp(line, "read_ahead")==0)
161 continue;
162 if (strcmp(line, "unused")==0)
163 continue;
164 insert_here = NULL;
165 /* Better be an md line.. */
166 if (strncmp(line, "md", 2)!= 0 || strlen(line) >= 32
167 || (line[2] != '_' && !isdigit(line[2])))
168 continue;
169 strcpy(devnm, line);
170
171 ent = xmalloc(sizeof(*ent));
172 ent->dev = ent->level = ent->pattern= NULL;
173 ent->next = NULL;
174 ent->percent = RESYNC_NONE;
175 ent->active = -1;
176 ent->resync = 0;
177 ent->metadata_version = NULL;
178 ent->raid_disks = 0;
179 ent->devcnt = 0;
180 ent->members = NULL;
181
182 ent->dev = xstrdup(line);
183 strcpy(ent->devnm, devnm);
184
185 for (w=dl_next(line); w!= line ; w=dl_next(w)) {
186 int l = strlen(w);
187 char *eq;
188 if (strcmp(w, "active")==0)
189 ent->active = 1;
190 else if (strcmp(w, "inactive")==0) {
191 ent->active = 0;
192 in_devs = 1;
193 } else if (ent->active > 0 &&
194 ent->level == NULL &&
195 w[0] != '(' /*readonly*/) {
196 ent->level = xstrdup(w);
197 in_devs = 1;
198 } else if (in_devs && strcmp(w, "blocks")==0)
199 in_devs = 0;
200 else if (in_devs) {
201 char *ep = strchr(w, '[');
202 ent->devcnt +=
203 add_member_devname(&ent->members, w);
204 if (ep && strncmp(w, "md", 2)==0) {
205 /* This has an md device as a component.
206 * If that device is already in the
207 * list, make sure we insert before
208 * there.
209 */
210 struct mdstat_ent **ih;
211 ih = &all;
212 while (ih != insert_here && *ih &&
213 ((int)strlen((*ih)->devnm) != ep-w
214 || strncmp((*ih)->devnm, w, ep-w) != 0))
215 ih = & (*ih)->next;
216 insert_here = ih;
217 }
218 } else if (strcmp(w, "super") == 0 &&
219 dl_next(w) != line) {
220 w = dl_next(w);
221 ent->metadata_version = xstrdup(w);
222 } else if (w[0] == '[' && isdigit(w[1])) {
223 ent->raid_disks = atoi(w+1);
224 } else if (!ent->pattern &&
225 w[0] == '[' &&
226 (w[1] == 'U' || w[1] == '_')) {
227 ent->pattern = xstrdup(w+1);
228 if (ent->pattern[l-2]==']')
229 ent->pattern[l-2] = '\0';
230 } else if (ent->percent == RESYNC_NONE &&
231 strncmp(w, "re", 2)== 0 &&
232 w[l-1] == '%' &&
233 (eq=strchr(w, '=')) != NULL ) {
234 ent->percent = atoi(eq+1);
235 if (strncmp(w,"resync", 6)==0)
236 ent->resync = 1;
237 else if (strncmp(w, "reshape", 7)==0)
238 ent->resync = 2;
239 else
240 ent->resync = 0;
241 } else if (ent->percent == RESYNC_NONE &&
242 (w[0] == 'r' || w[0] == 'c')) {
243 if (strncmp(w, "resync", 4)==0)
244 ent->resync = 1;
245 if (strncmp(w, "reshape", 7)==0)
246 ent->resync = 2;
247 if (strncmp(w, "recovery", 8)==0)
248 ent->resync = 0;
249 if (strncmp(w, "check", 5)==0)
250 ent->resync = 3;
251
252 if (l > 8 && strcmp(w+l-8, "=DELAYED") == 0)
253 ent->percent = RESYNC_DELAYED;
254 if (l > 8 && strcmp(w+l-8, "=PENDING") == 0)
255 ent->percent = RESYNC_PENDING;
256 } else if (ent->percent == RESYNC_NONE &&
257 w[0] >= '0' &&
258 w[0] <= '9' &&
259 w[l-1] == '%') {
260 ent->percent = atoi(w);
261 }
262 }
263 if (insert_here && (*insert_here)) {
264 ent->next = *insert_here;
265 *insert_here = ent;
266 } else {
267 *end = ent;
268 end = &ent->next;
269 }
270 }
271 if (hold && mdstat_fd == -1) {
272 mdstat_fd = dup(fileno(f));
273 fcntl(mdstat_fd, F_SETFD, FD_CLOEXEC);
274 }
275 fclose(f);
276
277 /* If we might want to start array,
278 * reverse the order, so that components comes before composites
279 */
280 if (start) {
281 rv = NULL;
282 while (all) {
283 struct mdstat_ent *e = all;
284 all = all->next;
285 e->next = rv;
286 rv = e;
287 }
288 } else rv = all;
289 return rv;
290 }
291
292 void mdstat_close(void)
293 {
294 if (mdstat_fd >= 0)
295 close(mdstat_fd);
296 mdstat_fd = -1;
297 }
298
299 void mdstat_wait(int seconds)
300 {
301 fd_set fds;
302 struct timeval tm;
303 int maxfd = 0;
304 FD_ZERO(&fds);
305 if (mdstat_fd >= 0) {
306 FD_SET(mdstat_fd, &fds);
307 maxfd = mdstat_fd;
308 }
309 tm.tv_sec = seconds;
310 tm.tv_usec = 0;
311 select(maxfd + 1, NULL, NULL, &fds, &tm);
312 }
313
314 void mdstat_wait_fd(int fd, const sigset_t *sigmask)
315 {
316 fd_set fds, rfds;
317 int maxfd = 0;
318
319 FD_ZERO(&fds);
320 FD_ZERO(&rfds);
321 if (mdstat_fd >= 0)
322 FD_SET(mdstat_fd, &fds);
323
324 if (fd >= 0) {
325 struct stat stb;
326 fstat(fd, &stb);
327 if ((stb.st_mode & S_IFMT) == S_IFREG)
328 /* Must be a /proc or /sys fd, so expect
329 * POLLPRI
330 * i.e. an 'exceptional' event.
331 */
332 FD_SET(fd, &fds);
333 else
334 FD_SET(fd, &rfds);
335
336 if (fd > maxfd)
337 maxfd = fd;
338
339 }
340 if (mdstat_fd > maxfd)
341 maxfd = mdstat_fd;
342
343 pselect(maxfd + 1, &rfds, NULL, &fds,
344 NULL, sigmask);
345 }
346
347 int mddev_busy(char *devnm)
348 {
349 struct mdstat_ent *mdstat = mdstat_read(0, 0);
350 struct mdstat_ent *me;
351
352 for (me = mdstat ; me ; me = me->next)
353 if (strcmp(me->devnm, devnm) == 0)
354 break;
355 free_mdstat(mdstat);
356 return me != NULL;
357 }
358
359 struct mdstat_ent *mdstat_by_component(char *name)
360 {
361 struct mdstat_ent *mdstat = mdstat_read(0, 0);
362
363 while (mdstat) {
364 struct dev_member *m;
365 struct mdstat_ent *ent;
366 if (mdstat->metadata_version &&
367 strncmp(mdstat->metadata_version, "external:", 9) == 0 &&
368 is_subarray(mdstat->metadata_version+9))
369 /* don't return subarrays, only containers */
370 ;
371 else for (m = mdstat->members; m; m = m->next) {
372 if (strcmp(m->name, name) == 0) {
373 free_mdstat(mdstat->next);
374 mdstat->next = NULL;
375 return mdstat;
376 }
377 }
378 ent = mdstat;
379 mdstat = mdstat->next;
380 ent->next = NULL;
381 free_mdstat(ent);
382 }
383 return NULL;
384 }
385
386 struct mdstat_ent *mdstat_by_subdev(char *subdev, char *container)
387 {
388 struct mdstat_ent *mdstat = mdstat_read(0, 0);
389 struct mdstat_ent *ent = NULL;
390
391 while (mdstat) {
392 /* metadata version must match:
393 * external:[/-]%s/%s
394 * where first %s is 'container' and second %s is 'subdev'
395 */
396 if (ent)
397 free_mdstat(ent);
398 ent = mdstat;
399 mdstat = mdstat->next;
400 ent->next = NULL;
401
402 if (ent->metadata_version == NULL ||
403 strncmp(ent->metadata_version, "external:", 9) != 0)
404 continue;
405
406 if (!metadata_container_matches(ent->metadata_version+9,
407 container) ||
408 !metadata_subdev_matches(ent->metadata_version+9,
409 subdev))
410 continue;
411
412 free_mdstat(mdstat);
413 return ent;
414 }
415 return NULL;
416 }