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