]>
git.ipfire.org Git - thirdparty/mdadm.git/blob - mdstat.c
2 * mdstat - parse /proc/mdstat file. Part of:
3 * mdadm - manage Linux "md" devices aka RAID arrays.
5 * Copyright (C) 2002-2009 Neil Brown <neilb@suse.de>
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.
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.
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
23 * Email: <neilb@suse.de>
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}}
34 * Normally only 4 md lines, but all are listed.
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>}
43 * STATUS is personality dependant:
44 * linear: %dk rounding
46 * raid1: [%d/%d] [U_U] ( raid/working. operational or not)
47 * raid5: level 4/5, %dk chunk, algorithm %d [%d/%d] [U_U]
50 * {resync|recovery}=%u%% finish=%u.%umin
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)} ...
60 * unused devices: {dev dev .. | <none>}
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
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
76 * As continuation is indicated by leading space, we use
77 * conf_line from config.c to read logical lines
83 #include <sys/select.h>
86 static void free_member_devnames(struct dev_member
*m
)
89 struct dev_member
*t
= m
;
97 static int add_member_devname(struct dev_member
**m
, char *name
)
99 struct dev_member
*new;
102 if ((t
= strchr(name
, '[')) == NULL
)
106 new = xmalloc(sizeof(*new));
107 new->name
= strndup(name
, t
- name
);
113 void free_mdstat(struct mdstat_ent
*ms
)
116 struct mdstat_ent
*t
;
119 free(ms
->metadata_version
);
120 free_member_devnames(ms
->members
);
127 static int mdstat_fd
= -1;
128 struct mdstat_ent
*mdstat_read(int hold
, int start
)
131 struct mdstat_ent
*all
, *rv
, **end
, **insert_here
;
135 if (hold
&& mdstat_fd
!= -1) {
136 lseek(mdstat_fd
, 0L, 0);
143 f
= fopen("/proc/mdstat", "r");
147 fcntl(fileno(f
), F_SETFD
, FD_CLOEXEC
);
151 for (; (line
= conf_line(f
)) ; free_line(line
)) {
152 struct mdstat_ent
*ent
;
157 if (strcmp(line
, "Personalities")==0)
159 if (strcmp(line
, "read_ahead")==0)
161 if (strcmp(line
, "unused")==0)
164 /* Better be an md line.. */
165 if (strncmp(line
, "md", 2)!= 0 || strlen(line
) >= 32
166 || (line
[2] != '_' && !isdigit(line
[2])))
170 ent
= xmalloc(sizeof(*ent
));
171 ent
->level
= ent
->pattern
= NULL
;
173 ent
->percent
= RESYNC_NONE
;
176 ent
->metadata_version
= NULL
;
181 strcpy(ent
->devnm
, devnm
);
183 for (w
=dl_next(line
); w
!= line
; w
=dl_next(w
)) {
186 if (strcmp(w
, "active")==0)
188 else if (strcmp(w
, "inactive")==0) {
191 } else if (ent
->active
> 0 &&
192 ent
->level
== NULL
&&
193 w
[0] != '(' /*readonly*/) {
194 ent
->level
= xstrdup(w
);
196 } else if (in_devs
&& strcmp(w
, "blocks")==0)
199 char *ep
= strchr(w
, '[');
201 add_member_devname(&ent
->members
, w
);
202 if (ep
&& strncmp(w
, "md", 2)==0) {
203 /* This has an md device as a component.
204 * If that device is already in the
205 * list, make sure we insert before
208 struct mdstat_ent
**ih
;
210 while (ih
!= insert_here
&& *ih
&&
211 ((int)strlen((*ih
)->devnm
) != ep
-w
212 || strncmp((*ih
)->devnm
, w
, ep
-w
) != 0))
216 } else if (strcmp(w
, "super") == 0 &&
217 dl_next(w
) != line
) {
219 ent
->metadata_version
= xstrdup(w
);
220 } else if (w
[0] == '[' && isdigit(w
[1])) {
221 ent
->raid_disks
= atoi(w
+1);
222 } else if (!ent
->pattern
&&
224 (w
[1] == 'U' || w
[1] == '_')) {
225 ent
->pattern
= xstrdup(w
+1);
226 if (ent
->pattern
[l
-2]==']')
227 ent
->pattern
[l
-2] = '\0';
228 } else if (ent
->percent
== RESYNC_NONE
&&
229 strncmp(w
, "re", 2)== 0 &&
231 (eq
=strchr(w
, '=')) != NULL
) {
232 ent
->percent
= atoi(eq
+1);
233 if (strncmp(w
,"resync", 6)==0)
235 else if (strncmp(w
, "reshape", 7)==0)
239 } else if (ent
->percent
== RESYNC_NONE
&&
240 (w
[0] == 'r' || w
[0] == 'c')) {
241 if (strncmp(w
, "resync", 4)==0)
243 if (strncmp(w
, "reshape", 7)==0)
245 if (strncmp(w
, "recovery", 8)==0)
247 if (strncmp(w
, "check", 5)==0)
250 if (l
> 8 && strcmp(w
+l
-8, "=DELAYED") == 0)
251 ent
->percent
= RESYNC_DELAYED
;
252 if (l
> 8 && strcmp(w
+l
-8, "=PENDING") == 0)
253 ent
->percent
= RESYNC_PENDING
;
254 } else if (ent
->percent
== RESYNC_NONE
&&
258 ent
->percent
= atoi(w
);
261 if (insert_here
&& (*insert_here
)) {
262 ent
->next
= *insert_here
;
269 if (hold
&& mdstat_fd
== -1) {
270 mdstat_fd
= dup(fileno(f
));
271 fcntl(mdstat_fd
, F_SETFD
, FD_CLOEXEC
);
275 /* If we might want to start array,
276 * reverse the order, so that components comes before composites
281 struct mdstat_ent
*e
= all
;
290 void mdstat_close(void)
297 void mdstat_wait(int seconds
)
303 if (mdstat_fd
>= 0) {
304 FD_SET(mdstat_fd
, &fds
);
309 select(maxfd
+ 1, NULL
, NULL
, &fds
, &tm
);
312 void mdstat_wait_fd(int fd
, const sigset_t
*sigmask
)
320 FD_SET(mdstat_fd
, &fds
);
325 if ((stb
.st_mode
& S_IFMT
) == S_IFREG
)
326 /* Must be a /proc or /sys fd, so expect
328 * i.e. an 'exceptional' event.
338 if (mdstat_fd
> maxfd
)
341 pselect(maxfd
+ 1, &rfds
, NULL
, &fds
,
345 int mddev_busy(char *devnm
)
347 struct mdstat_ent
*mdstat
= mdstat_read(0, 0);
348 struct mdstat_ent
*me
;
350 for (me
= mdstat
; me
; me
= me
->next
)
351 if (strcmp(me
->devnm
, devnm
) == 0)
357 struct mdstat_ent
*mdstat_by_component(char *name
)
359 struct mdstat_ent
*mdstat
= mdstat_read(0, 0);
362 struct dev_member
*m
;
363 struct mdstat_ent
*ent
;
364 if (mdstat
->metadata_version
&&
365 strncmp(mdstat
->metadata_version
, "external:", 9) == 0 &&
366 is_subarray(mdstat
->metadata_version
+9))
367 /* don't return subarrays, only containers */
369 else for (m
= mdstat
->members
; m
; m
= m
->next
) {
370 if (strcmp(m
->name
, name
) == 0) {
371 free_mdstat(mdstat
->next
);
377 mdstat
= mdstat
->next
;
384 struct mdstat_ent
*mdstat_by_subdev(char *subdev
, char *container
)
386 struct mdstat_ent
*mdstat
= mdstat_read(0, 0);
387 struct mdstat_ent
*ent
= NULL
;
390 /* metadata version must match:
392 * where first %s is 'container' and second %s is 'subdev'
397 mdstat
= mdstat
->next
;
400 if (ent
->metadata_version
== NULL
||
401 strncmp(ent
->metadata_version
, "external:", 9) != 0)
404 if (!metadata_container_matches(ent
->metadata_version
+9,
406 !metadata_subdev_matches(ent
->metadata_version
+9,