]>
Commit | Line | Data |
---|---|---|
1 | ||
2 | #include "mdadm.h" | |
3 | #include "mdmon.h" | |
4 | ||
5 | #include <sys/select.h> | |
6 | ||
7 | ||
8 | static char *array_states[] = { | |
9 | "clear", "inactive", "suspended", "readonly", "read-auto", | |
10 | "clean", "active", "write-pending", "active-idle", NULL }; | |
11 | static char *sync_actions[] = { | |
12 | "idle", "reshape", "resync", "recover", "check", "repair", NULL | |
13 | }; | |
14 | ||
15 | static int write_attr(char *attr, int fd) | |
16 | { | |
17 | return write(fd, attr, strlen(attr)); | |
18 | } | |
19 | ||
20 | static void add_fd(fd_set *fds, int *maxfd, int fd) | |
21 | { | |
22 | if (fd < 0) | |
23 | return; | |
24 | if (fd > *maxfd) | |
25 | *maxfd = fd; | |
26 | FD_SET(fd, fds); | |
27 | } | |
28 | ||
29 | static int read_attr(char *buf, int len, int fd) | |
30 | { | |
31 | int n; | |
32 | ||
33 | if (fd < 0) { | |
34 | buf[0] = 0; | |
35 | return 0; | |
36 | } | |
37 | lseek(fd, 0, 0); | |
38 | n = read(fd, buf, len - 1); | |
39 | ||
40 | if (n <= 0) { | |
41 | buf[0] = 0; | |
42 | return 0; | |
43 | } | |
44 | buf[n] = 0; | |
45 | if (buf[n-1] == '\n') | |
46 | buf[n-1] = 0; | |
47 | return n; | |
48 | } | |
49 | ||
50 | static int get_sync_pos(struct active_array *a) | |
51 | { | |
52 | char buf[30]; | |
53 | int n; | |
54 | ||
55 | n = read_attr(buf, 30, a->sync_pos_fd); | |
56 | if (n <= 0) | |
57 | return n; | |
58 | ||
59 | if (strncmp(buf, "max", 3) == 0) { | |
60 | a->sync_pos = ~(unsigned long long)0; | |
61 | return 1; | |
62 | } | |
63 | a->sync_pos = strtoull(buf, NULL, 10); | |
64 | return 1; | |
65 | } | |
66 | ||
67 | ||
68 | static int attr_match(const char *attr, const char *str) | |
69 | { | |
70 | /* See if attr, read from a sysfs file, matches | |
71 | * str. They must either be the same, or attr can | |
72 | * have a trailing newline or comma | |
73 | */ | |
74 | while (*attr && *str && *attr == *str) { | |
75 | attr++; | |
76 | str++; | |
77 | } | |
78 | ||
79 | if (*str || (*attr && *attr != ',' && *attr != '\n')) | |
80 | return 0; | |
81 | return 1; | |
82 | } | |
83 | ||
84 | static int match_word(const char *word, char **list) | |
85 | { | |
86 | int n; | |
87 | for (n=0; list[n]; n++) | |
88 | if (attr_match(word, list[n])) | |
89 | break; | |
90 | return n; | |
91 | } | |
92 | ||
93 | static enum array_state read_state(int fd) | |
94 | { | |
95 | char buf[20]; | |
96 | int n = read_attr(buf, 20, fd); | |
97 | ||
98 | if (n <= 0) | |
99 | return bad_word; | |
100 | return (enum array_state) match_word(buf, array_states); | |
101 | } | |
102 | ||
103 | static enum sync_action read_action( int fd) | |
104 | { | |
105 | char buf[20]; | |
106 | int n = read_attr(buf, 20, fd); | |
107 | ||
108 | if (n <= 0) | |
109 | return bad_action; | |
110 | return (enum sync_action) match_word(buf, sync_actions); | |
111 | } | |
112 | ||
113 | #define DS_FAULTY 1 | |
114 | #define DS_INSYNC 2 | |
115 | #define DS_WRITE_MOSTLY 4 | |
116 | #define DS_SPARE 8 | |
117 | #define DS_REMOVE 1024 | |
118 | ||
119 | int read_dev_state(int fd) | |
120 | { | |
121 | char buf[60]; | |
122 | int n = read_attr(buf, 60, fd); | |
123 | char *cp; | |
124 | int rv = 0; | |
125 | ||
126 | if (n <= 0) | |
127 | return 0; | |
128 | ||
129 | cp = buf; | |
130 | while (cp) { | |
131 | if (attr_match("faulty", cp)) | |
132 | rv |= DS_FAULTY; | |
133 | if (attr_match("in_sync", cp)) | |
134 | rv |= DS_INSYNC; | |
135 | if (attr_match("write_mostly", cp)) | |
136 | rv |= DS_WRITE_MOSTLY; | |
137 | if (attr_match("spare", cp)) | |
138 | rv |= DS_SPARE; | |
139 | cp = strchr(cp, ','); | |
140 | if (cp) | |
141 | cp++; | |
142 | } | |
143 | return rv; | |
144 | } | |
145 | ||
146 | ||
147 | /* Monitor a set of active md arrays - all of which share the | |
148 | * same metadata - and respond to events that require | |
149 | * metadata update. | |
150 | * | |
151 | * New arrays are detected by another thread which allocates | |
152 | * required memory and attaches the data structure to our list. | |
153 | * | |
154 | * Events: | |
155 | * Array stops. | |
156 | * This is detected by array_state going to 'clear' or 'inactive'. | |
157 | * while we thought it was active. | |
158 | * Response is to mark metadata as clean and 'clear' the array(??) | |
159 | * write-pending | |
160 | * array_state if 'write-pending' | |
161 | * We mark metadata as 'dirty' then set array to 'active'. | |
162 | * active_idle | |
163 | * Either ignore, or mark clean, then mark metadata as clean. | |
164 | * | |
165 | * device fails | |
166 | * detected by rd-N/state reporting "faulty" | |
167 | * mark device as 'failed' in metadata, the remove device | |
168 | * by writing 'remove' to rd/state. | |
169 | * | |
170 | * sync completes | |
171 | * sync_action was 'resync' and becomes 'idle' and resync_start becomes | |
172 | * MaxSector | |
173 | * Notify metadata that sync is complete. | |
174 | * "Deal with Degraded" | |
175 | * | |
176 | * recovery completes | |
177 | * sync_action changes from 'recover' to 'idle' | |
178 | * Check each device state and mark metadata if 'faulty' or 'in_sync'. | |
179 | * "Deal with Degraded" | |
180 | * | |
181 | * deal with degraded array | |
182 | * We only do this when first noticing the array is degraded. | |
183 | * This can be when we first see the array, when sync completes or | |
184 | * when recovery completes. | |
185 | * | |
186 | * Check if number of failed devices suggests recovery is needed, and | |
187 | * skip if not. | |
188 | * Ask metadata for a spare device | |
189 | * Add device as not in_sync and give a role | |
190 | * Update metadata. | |
191 | * Start recovery. | |
192 | * | |
193 | * deal with resync | |
194 | * This only happens on finding a new array.... | |
195 | * Maybe this is done by mdadm before passing the array to us? | |
196 | * | |
197 | * If array is 'clean' but metadata is 'dirty', start a resync | |
198 | * and mark array as 'dirty'. | |
199 | * | |
200 | * | |
201 | * | |
202 | * | |
203 | * We wait for a change (poll/select) on array_state, sync_action, and | |
204 | * each rd-X/state file. | |
205 | * When we get any change, we check everything. So read each state file, | |
206 | * then decide what to do. | |
207 | * | |
208 | * The core action is to write new metadata to all devices in the array. | |
209 | * This is done at most once on any wakeup. | |
210 | * After that we might: | |
211 | * - update the array_state | |
212 | * - set the role of some devices. | |
213 | * - request a sync_action | |
214 | * | |
215 | */ | |
216 | ||
217 | static int read_and_act(struct active_array *a) | |
218 | { | |
219 | int check_degraded; | |
220 | struct mdinfo *mdi; | |
221 | ||
222 | a->next_state = bad_word; | |
223 | a->next_action = bad_action; | |
224 | ||
225 | a->curr_state = read_state(a->info.state_fd); | |
226 | a->curr_action = read_action(a->action_fd); | |
227 | for (mdi = a->info.devs; mdi ; mdi = mdi->next) { | |
228 | mdi->next_state = 0; | |
229 | mdi->curr_state = read_dev_state(mdi->state_fd); | |
230 | } | |
231 | ||
232 | if (a->curr_state <= inactive && | |
233 | a->prev_state > inactive) { | |
234 | /* array has been stopped */ | |
235 | get_sync_pos(a); | |
236 | a->container->ss->mark_clean(a, a->sync_pos); | |
237 | a->next_state = clear; | |
238 | } | |
239 | if (a->curr_state == write_pending) { | |
240 | a->container->ss->mark_dirty(a); | |
241 | a->next_state = active; | |
242 | } | |
243 | if (a->curr_state == active_idle) { | |
244 | /* Set array to 'clean' FIRST, then | |
245 | * a->ss->mark_clean(a); | |
246 | * just ignore for now. | |
247 | */ | |
248 | } | |
249 | ||
250 | if (a->curr_state == readonly) { | |
251 | /* Well, I'm ready to handle things, so | |
252 | * read-auto is OK. FIXME what if we really want | |
253 | * readonly ??? | |
254 | */ | |
255 | a->next_state = read_auto; | |
256 | } | |
257 | ||
258 | if (a->curr_action == idle && | |
259 | a->prev_action == resync) { | |
260 | /* check resync_start to see if it is 'max'. | |
261 | * Do I open here, or have it open the whole time? | |
262 | */ | |
263 | get_sync_pos(a); | |
264 | check_degraded = 1; | |
265 | } | |
266 | ||
267 | if (a->curr_action == idle && | |
268 | a->prev_action == recover) { | |
269 | for (mdi = a->info.devs ; mdi ; mdi = mdi->next) { | |
270 | a->container->ss->set_disk(a, mdi->disk.raid_disk); | |
271 | if (! (mdi->curr_state & DS_INSYNC)) | |
272 | check_degraded = 1; | |
273 | } | |
274 | } | |
275 | ||
276 | ||
277 | for (mdi = a->info.devs ; mdi ; mdi = mdi->next) { | |
278 | if (mdi->curr_state & DS_FAULTY) { | |
279 | a->container->ss->set_disk(a, mdi->disk.raid_disk); | |
280 | check_degraded = 1; | |
281 | mdi->next_state = DS_REMOVE; | |
282 | } | |
283 | } | |
284 | ||
285 | if (check_degraded) { | |
286 | // FIXME; | |
287 | } | |
288 | ||
289 | a->container->ss->sync_metadata(a); | |
290 | ||
291 | /* Effect state changes in the array */ | |
292 | if (a->next_state != bad_word) | |
293 | write_attr(array_states[a->next_state], a->info.state_fd); | |
294 | if (a->next_action != bad_action) | |
295 | write_attr(sync_actions[a->next_action], a->action_fd); | |
296 | for (mdi = a->info.devs; mdi ; mdi = mdi->next) { | |
297 | if (mdi->next_state == DS_REMOVE) | |
298 | write_attr("remove", mdi->state_fd); | |
299 | if (mdi->next_state & DS_INSYNC) | |
300 | write_attr("+in_sync", mdi->state_fd); | |
301 | } | |
302 | ||
303 | /* move curr_ to prev_ */ | |
304 | a->prev_state = a->curr_state; | |
305 | ||
306 | a->prev_action = a->curr_action; | |
307 | ||
308 | for (mdi = a->info.devs; mdi ; mdi = mdi->next) { | |
309 | mdi->prev_state = mdi->curr_state; | |
310 | mdi->next_state = 0; | |
311 | } | |
312 | ||
313 | return 1; | |
314 | } | |
315 | ||
316 | static int wait_and_act(struct active_array *aa, int pfd, int nowait) | |
317 | { | |
318 | fd_set rfds; | |
319 | int maxfd = 0; | |
320 | struct active_array *a; | |
321 | int rv; | |
322 | ||
323 | FD_ZERO(&rfds); | |
324 | ||
325 | add_fd(&rfds, &maxfd, pfd); | |
326 | for (a = aa ; a ; a = a->next) { | |
327 | struct mdinfo *mdi; | |
328 | ||
329 | add_fd(&rfds, &maxfd, a->info.state_fd); | |
330 | add_fd(&rfds, &maxfd, a->action_fd); | |
331 | for (mdi = a->info.devs ; mdi ; mdi = mdi->next) | |
332 | add_fd(&rfds, &maxfd, mdi->state_fd); | |
333 | } | |
334 | ||
335 | if (!nowait) { | |
336 | rv = select(maxfd+1, &rfds, NULL, NULL, NULL); | |
337 | ||
338 | if (rv <= 0) | |
339 | return rv; | |
340 | ||
341 | if (FD_ISSET(pfd, &rfds)) { | |
342 | char buf[4]; | |
343 | read(pfd, buf, 4); | |
344 | ; // FIXME read from the pipe | |
345 | } | |
346 | } | |
347 | ||
348 | for (a = aa; a ; a = a->next) { | |
349 | if (a->replaces) { | |
350 | struct active_array **ap; | |
351 | for (ap = &a->next; *ap && *ap != a->replaces; | |
352 | ap = & (*ap)->next) | |
353 | ; | |
354 | if (*ap) | |
355 | *ap = (*ap)->next; | |
356 | discard_this = a->replaces; | |
357 | a->replaces = NULL; | |
358 | } | |
359 | rv += read_and_act(a); | |
360 | } | |
361 | return rv; | |
362 | } | |
363 | ||
364 | void do_monitor(struct supertype *container) | |
365 | { | |
366 | int rv; | |
367 | int first = 1; | |
368 | do { | |
369 | rv = wait_and_act(container->arrays, container->pipe[0], first); | |
370 | first = 0; | |
371 | } while (rv >= 0); | |
372 | } |