]>
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 void free_mdstat(struct mdstat_ent
*ms
)
90 if (ms
->dev
) free(ms
->dev
);
91 if (ms
->level
) free(ms
->level
);
92 if (ms
->pattern
) free(ms
->pattern
);
93 if (ms
->metadata_version
) free(ms
->metadata_version
);
100 static int mdstat_fd
= -1;
101 struct mdstat_ent
*mdstat_read(int hold
, int start
)
104 struct mdstat_ent
*all
, *rv
, **end
, **insert_here
;
107 if (hold
&& mdstat_fd
!= -1) {
108 lseek(mdstat_fd
, 0L, 0);
109 f
= fdopen(dup(mdstat_fd
), "r");
111 f
= fopen("/proc/mdstat", "r");
115 fcntl(fileno(f
), F_SETFD
, FD_CLOEXEC
);
119 for (; (line
= conf_line(f
)) ; free_line(line
)) {
120 struct mdstat_ent
*ent
;
126 if (strcmp(line
, "Personalities")==0)
128 if (strcmp(line
, "read_ahead")==0)
130 if (strcmp(line
, "unused")==0)
133 /* Better be an md line.. */
134 if (strncmp(line
, "md", 2)!= 0)
136 if (strncmp(line
, "md_d", 4) == 0)
137 devnum
= -1-strtoul(line
+4, &ep
, 10);
138 else if (strncmp(line
, "md", 2) == 0)
139 devnum
= strtoul(line
+2, &ep
, 10);
142 if (ep
== NULL
|| *ep
) {
143 /* fprintf(stderr, Name ": bad /proc/mdstat line starts: %s\n", line); */
147 ent
= malloc(sizeof(*ent
));
149 fprintf(stderr
, Name
": malloc failed reading /proc/mdstat.\n");
153 ent
->dev
= ent
->level
= ent
->pattern
= NULL
;
158 ent
->metadata_version
= NULL
;
163 ent
->dev
= strdup(line
);
164 ent
->devnum
= devnum
;
166 for (w
=dl_next(line
); w
!= line
; w
=dl_next(w
)) {
169 if (strcmp(w
, "active")==0)
171 else if (strcmp(w
, "inactive")==0)
173 else if (ent
->active
>=0 &&
174 ent
->level
== NULL
&&
175 w
[0] != '(' /*readonly*/) {
176 ent
->level
= strdup(w
);
178 } else if (in_devs
&& strcmp(w
, "blocks")==0)
182 if (strncmp(w
, "md", 2)==0) {
183 /* This has an md device as a component.
184 * If that device is already in the
185 * list, make sure we insert before
188 struct mdstat_ent
**ih
;
189 int dn2
= devname2devnum(w
);
191 while (ih
!= insert_here
&& *ih
&&
192 (*ih
)->devnum
!= dn2
)
196 } else if (strcmp(w
, "super") == 0 &&
197 dl_next(w
) != line
) {
199 ent
->metadata_version
= strdup(w
);
200 } else if (w
[0] == '[' && isdigit(w
[1])) {
201 ent
->raid_disks
= atoi(w
+1);
202 } else if (!ent
->pattern
&&
204 (w
[1] == 'U' || w
[1] == '_')) {
205 ent
->pattern
= strdup(w
+1);
206 if (ent
->pattern
[l
-2]==']')
207 ent
->pattern
[l
-2] = '\0';
208 } else if (ent
->percent
== -1 &&
209 strncmp(w
, "re", 2)== 0 &&
211 (eq
=strchr(w
, '=')) != NULL
) {
212 ent
->percent
= atoi(eq
+1);
213 if (strncmp(w
,"resync", 4)==0)
215 } else if (ent
->percent
== -1 &&
216 strncmp(w
, "resync", 4)==0) {
218 } else if (ent
->percent
== -1 &&
222 ent
->percent
= atoi(w
);
225 if (insert_here
&& (*insert_here
)) {
226 ent
->next
= *insert_here
;
233 if (hold
&& mdstat_fd
== -1) {
234 mdstat_fd
= dup(fileno(f
));
235 fcntl(mdstat_fd
, F_SETFD
, FD_CLOEXEC
);
239 /* If we might want to start array,
240 * reverse the order, so that components comes before composites
245 struct mdstat_ent
*e
= all
;
254 void mdstat_wait(int seconds
)
260 if (mdstat_fd
>= 0) {
261 FD_SET(mdstat_fd
, &fds
);
266 select(maxfd
+ 1, NULL
, NULL
, &fds
, &tm
);
269 void mdstat_wait_fd(int fd
, const sigset_t
*sigmask
)
277 FD_SET(mdstat_fd
, &fds
);
281 if ((stb
.st_mode
& S_IFMT
) == S_IFREG
)
282 /* Must be a /proc or /sys fd, so expect
284 * i.e. an 'exceptional' event.
290 if (mdstat_fd
> maxfd
)
293 pselect(maxfd
+ 1, &rfds
, NULL
, &fds
,
297 int mddev_busy(int devnum
)
299 struct mdstat_ent
*mdstat
= mdstat_read(0, 0);
300 struct mdstat_ent
*me
;
302 for (me
= mdstat
; me
; me
= me
->next
)
303 if (me
->devnum
== devnum
)