]>
Commit | Line | Data |
---|---|---|
549e9569 NB |
1 | |
2 | /* | |
3 | * The management thread for monitoring active md arrays. | |
4 | * This thread does things which might block such as memory | |
5 | * allocation. | |
6 | * In particular: | |
7 | * | |
8 | * - Find out about new arrays in this container. | |
9 | * Allocate the data structures and open the files. | |
10 | * | |
11 | * For this we watch /proc/mdstat and find new arrays with | |
12 | * metadata type that confirms sharing. e.g. "md4" | |
13 | * When we find a new array we slip it into the list of | |
14 | * arrays and signal 'monitor' by writing to a pipe. | |
15 | * | |
16 | * - Respond to reshape requests by allocating new data structures | |
17 | * and opening new files. | |
18 | * | |
19 | * These come as a change to raid_disks. We allocate a new | |
20 | * version of the data structures and slip it into the list. | |
21 | * 'monitor' will notice and release the old version. | |
22 | * Changes to level, chunksize, layout.. do not need re-allocation. | |
23 | * Reductions in raid_disks don't really either, but we handle | |
24 | * them the same way for consistency. | |
25 | * | |
26 | * - When a device is added to the container, we add it to the metadata | |
27 | * as a spare. | |
28 | * | |
6c3fb95c NB |
29 | * - Deal with degraded array |
30 | * We only do this when first noticing the array is degraded. | |
31 | * This can be when we first see the array, when sync completes or | |
32 | * when recovery completes. | |
33 | * | |
34 | * Check if number of failed devices suggests recovery is needed, and | |
35 | * skip if not. | |
36 | * Ask metadata to allocate a spare device | |
37 | * Add device as not in_sync and give a role | |
38 | * Update metadata. | |
39 | * Open sysfs files and pass to monitor. | |
40 | * Make sure that monitor Starts recovery.... | |
549e9569 NB |
41 | * |
42 | * - Pass on metadata updates from external programs such as | |
43 | * mdadm creating a new array. | |
44 | * | |
45 | * This is most-messy. | |
46 | * It might involve adding a new array or changing the status of | |
47 | * a spare, or any reconfig that the kernel doesn't get involved in. | |
48 | * | |
49 | * The required updates are received via a named pipe. There will | |
50 | * be one named pipe for each container. Each message contains a | |
51 | * sync marker: 0x5a5aa5a5, A byte count, and the message. This is | |
52 | * passed to the metadata handler which will interpret and process it. | |
53 | * For 'DDF' messages are internal data blocks with the leading | |
54 | * 'magic number' signifying what sort of data it is. | |
55 | * | |
56 | */ | |
57 | ||
58 | /* | |
59 | * We select on /proc/mdstat and the named pipe. | |
60 | * We create new arrays or updated version of arrays and slip | |
61 | * them into the head of the list, then signal 'monitor' via a pipe write. | |
62 | * 'monitor' will notice and place the old array on a return list. | |
63 | * Metadata updates are placed on a queue just like they arrive | |
64 | * from the named pipe. | |
65 | * | |
66 | * When new arrays are found based on correct metadata string, we | |
67 | * need to identify them with an entry in the metadata. Maybe we require | |
68 | * the metadata to be mdX/NN when NN is the index into an appropriate table. | |
69 | * | |
70 | */ | |
71 | ||
72 | /* | |
73 | * List of tasks: | |
74 | * - Watch for spares to be added to the container, and write updated | |
75 | * metadata to them. | |
76 | * - Watch for new arrays using this container, confirm they match metadata | |
77 | * and if so, start monitoring them | |
78 | * - Watch for spares being added to monitored arrays. This shouldn't | |
79 | * happen, as we should do all the adding. Just remove them. | |
80 | * - Watch for change in raid-disks, chunk-size, etc. Update metadata and | |
81 | * start a reshape. | |
82 | */ | |
83 | #ifndef _GNU_SOURCE | |
84 | #define _GNU_SOURCE | |
85 | #endif | |
86 | #include "mdadm.h" | |
87 | #include "mdmon.h" | |
4d43913c | 88 | #include <sys/syscall.h> |
549e9569 | 89 | #include <sys/socket.h> |
1ed3f387 | 90 | #include <signal.h> |
549e9569 | 91 | |
2a0bb19e DW |
92 | static void close_aa(struct active_array *aa) |
93 | { | |
94 | struct mdinfo *d; | |
95 | ||
96 | for (d = aa->info.devs; d; d = d->next) | |
97 | close(d->state_fd); | |
98 | ||
99 | close(aa->action_fd); | |
100 | close(aa->info.state_fd); | |
101 | close(aa->resync_start_fd); | |
2a0bb19e DW |
102 | } |
103 | ||
549e9569 NB |
104 | static void free_aa(struct active_array *aa) |
105 | { | |
2a0bb19e DW |
106 | /* Note that this doesn't close fds if they are being used |
107 | * by a clone. ->container will be set for a clone | |
549e9569 | 108 | */ |
4e6e574a | 109 | dprintf("%s: devnum: %d\n", __func__, aa->devnum); |
2a0bb19e DW |
110 | if (!aa->container) |
111 | close_aa(aa); | |
549e9569 NB |
112 | while (aa->info.devs) { |
113 | struct mdinfo *d = aa->info.devs; | |
114 | aa->info.devs = d->next; | |
115 | free(d); | |
116 | } | |
117 | free(aa); | |
118 | } | |
119 | ||
6c3fb95c NB |
120 | static struct active_array *duplicate_aa(struct active_array *aa) |
121 | { | |
122 | struct active_array *newa = malloc(sizeof(*newa)); | |
123 | struct mdinfo **dp1, **dp2; | |
124 | ||
125 | *newa = *aa; | |
126 | newa->next = NULL; | |
127 | newa->replaces = NULL; | |
128 | newa->info.next = NULL; | |
129 | ||
130 | dp2 = &newa->info.devs; | |
131 | ||
132 | for (dp1 = &aa->info.devs; *dp1; dp1 = &(*dp1)->next) { | |
133 | struct mdinfo *d; | |
134 | if ((*dp1)->state_fd < 0) | |
135 | continue; | |
136 | ||
137 | d = malloc(sizeof(*d)); | |
138 | *d = **dp1; | |
139 | *dp2 = d; | |
140 | dp2 = & d->next; | |
141 | } | |
7e1432fb | 142 | *dp2 = NULL; |
6c3fb95c NB |
143 | |
144 | return newa; | |
145 | } | |
146 | ||
4d43913c | 147 | static void wakeup_monitor(void) |
2a0bb19e | 148 | { |
4d43913c NB |
149 | /* tgkill(getpid(), mon_tid, SIGUSR1); */ |
150 | int pid = getpid(); | |
151 | syscall(SYS_tgkill, pid, mon_tid, SIGUSR1); | |
2a0bb19e DW |
152 | } |
153 | ||
1ed3f387 NB |
154 | static void remove_old(void) |
155 | { | |
156 | if (discard_this) { | |
157 | discard_this->next = NULL; | |
158 | free_aa(discard_this); | |
159 | if (pending_discard == discard_this) | |
160 | pending_discard = NULL; | |
161 | discard_this = NULL; | |
48561b01 | 162 | wakeup_monitor(); |
1ed3f387 NB |
163 | } |
164 | } | |
165 | ||
549e9569 NB |
166 | static void replace_array(struct supertype *container, |
167 | struct active_array *old, | |
168 | struct active_array *new) | |
169 | { | |
170 | /* To replace an array, we add it to the top of the list | |
171 | * marked with ->replaces to point to the original. | |
172 | * 'monitor' will take the original out of the list | |
173 | * and put it on 'discard_this'. We take it from there | |
174 | * and discard it. | |
175 | */ | |
1ed3f387 | 176 | remove_old(); |
549e9569 NB |
177 | while (pending_discard) { |
178 | while (discard_this == NULL) | |
179 | sleep(1); | |
1ed3f387 | 180 | remove_old(); |
549e9569 NB |
181 | } |
182 | pending_discard = old; | |
183 | new->replaces = old; | |
184 | new->next = container->arrays; | |
185 | container->arrays = new; | |
4d43913c | 186 | wakeup_monitor(); |
549e9569 NB |
187 | } |
188 | ||
2e735d19 NB |
189 | struct metadata_update *update_queue = NULL; |
190 | struct metadata_update *update_queue_handled = NULL; | |
191 | struct metadata_update *update_queue_pending = NULL; | |
192 | ||
193 | void check_update_queue(struct supertype *container) | |
194 | { | |
195 | while (update_queue_handled) { | |
196 | struct metadata_update *this = update_queue_handled; | |
197 | update_queue_handled = this->next; | |
904c1ef7 NB |
198 | free(this->buf); |
199 | if (this->space) | |
200 | free(this->space); | |
2e735d19 NB |
201 | free(this); |
202 | } | |
203 | if (update_queue == NULL && | |
204 | update_queue_pending) { | |
205 | update_queue = update_queue_pending; | |
206 | update_queue_pending = NULL; | |
4d43913c | 207 | wakeup_monitor(); |
2e735d19 NB |
208 | } |
209 | } | |
210 | ||
6c3fb95c | 211 | static void queue_metadata_update(struct metadata_update *mu) |
2e735d19 NB |
212 | { |
213 | struct metadata_update **qp; | |
214 | ||
215 | qp = &update_queue_pending; | |
216 | while (*qp) | |
217 | qp = & ((*qp)->next); | |
218 | *qp = mu; | |
219 | } | |
220 | ||
43dad3d6 DW |
221 | static void add_disk_to_container(struct supertype *st, struct mdinfo *sd) |
222 | { | |
223 | int dfd; | |
224 | char nm[20]; | |
225 | struct metadata_update *update = NULL; | |
226 | mdu_disk_info_t dk = { | |
227 | .number = -1, | |
228 | .major = sd->disk.major, | |
229 | .minor = sd->disk.minor, | |
230 | .raid_disk = -1, | |
231 | .state = 0, | |
232 | }; | |
233 | ||
234 | dprintf("%s: add %d:%d to container\n", | |
235 | __func__, sd->disk.major, sd->disk.minor); | |
236 | ||
237 | sprintf(nm, "%d:%d", sd->disk.major, sd->disk.minor); | |
238 | dfd = dev_open(nm, O_RDWR); | |
239 | if (dfd < 0) | |
240 | return; | |
241 | ||
242 | st->update_tail = &update; | |
243 | st->ss->add_to_super(st, &dk, dfd, NULL); | |
244 | st->ss->write_init_super(st); | |
245 | queue_metadata_update(update); | |
246 | st->update_tail = NULL; | |
247 | } | |
248 | ||
549e9569 NB |
249 | static void manage_container(struct mdstat_ent *mdstat, |
250 | struct supertype *container) | |
251 | { | |
252 | /* The only thing of interest here is if a new device | |
253 | * has been added to the container. We add it to the | |
254 | * array ignoring any metadata on it. | |
255 | * FIXME should we look for compatible metadata and take hints | |
256 | * about spare assignment.... probably not. | |
549e9569 NB |
257 | */ |
258 | if (mdstat->devcnt != container->devcnt) { | |
7bc1962f DW |
259 | struct mdinfo **cdp, *cd, *di, *mdi; |
260 | int found; | |
261 | ||
549e9569 NB |
262 | /* read /sys/block/NAME/md/dev-??/block/dev to find out |
263 | * what is there, and compare with container->info.devs | |
264 | * To see what is removed and what is added. | |
265 | * These need to be remove from, or added to, the array | |
266 | */ | |
7bc1962f | 267 | mdi = sysfs_read(-1, mdstat->devnum, GET_DEVS); |
313a4a82 DW |
268 | if (!mdi) { |
269 | /* invalidate the current count so we can try again */ | |
270 | container->devcnt = -1; | |
7bc1962f | 271 | return; |
313a4a82 | 272 | } |
7bc1962f DW |
273 | |
274 | /* check for removals */ | |
275 | for (cdp = &container->devs; *cdp; ) { | |
276 | found = 0; | |
277 | for (di = mdi->devs; di; di = di->next) | |
278 | if (di->disk.major == (*cdp)->disk.major && | |
279 | di->disk.minor == (*cdp)->disk.minor) { | |
280 | found = 1; | |
281 | break; | |
282 | } | |
283 | if (!found) { | |
284 | cd = *cdp; | |
285 | *cdp = (*cdp)->next; | |
286 | free(cd); | |
287 | } else | |
288 | cdp = &(*cdp)->next; | |
289 | } | |
43dad3d6 DW |
290 | |
291 | /* check for additions */ | |
292 | for (di = mdi->devs; di; di = di->next) { | |
293 | for (cd = container->devs; cd; cd = cd->next) | |
294 | if (di->disk.major == cd->disk.major && | |
295 | di->disk.minor == cd->disk.minor) | |
296 | break; | |
297 | if (!cd) | |
298 | add_disk_to_container(container, di); | |
299 | } | |
7bc1962f | 300 | sysfs_free(mdi); |
549e9569 NB |
301 | container->devcnt = mdstat->devcnt; |
302 | } | |
303 | } | |
304 | ||
305 | static void manage_member(struct mdstat_ent *mdstat, | |
306 | struct active_array *a) | |
307 | { | |
308 | /* Compare mdstat info with known state of member array. | |
309 | * We do not need to look for device state changes here, that | |
310 | * is dealt with by the monitor. | |
311 | * | |
312 | * We just look for changes which suggest that a reshape is | |
313 | * being requested. | |
314 | * Unfortunately decreases in raid_disks don't show up in | |
315 | * mdstat until the reshape completes FIXME. | |
6c3fb95c NB |
316 | * |
317 | * Actually, we also want to handle degraded arrays here by | |
318 | * trying to find and assign a spare. | |
319 | * We do that whenever the monitor tells us too. | |
549e9569 NB |
320 | */ |
321 | // FIXME | |
322 | a->info.array.raid_disks = mdstat->raid_disks; | |
323 | a->info.array.chunk_size = mdstat->chunk_size; | |
324 | // MORE | |
325 | ||
6c3fb95c NB |
326 | if (a->check_degraded) { |
327 | struct metadata_update *updates = NULL; | |
328 | struct mdinfo *newdev; | |
329 | struct active_array *newa; | |
3c00ffbe | 330 | |
6c3fb95c NB |
331 | a->check_degraded = 0; |
332 | ||
333 | /* The array may not be degraded, this is just a good time | |
334 | * to check. | |
335 | */ | |
336 | newdev = a->container->ss->activate_spare(a, &updates); | |
337 | if (newdev) { | |
338 | struct mdinfo *d; | |
339 | /* Cool, we can add a device or several. */ | |
340 | newa = duplicate_aa(a); | |
341 | /* suspend recovery - maybe not needed */ | |
342 | ||
343 | /* Add device to array and set offset/size/slot. | |
344 | * and open files for each newdev */ | |
345 | for (d = newdev; d ; d = d->next) { | |
346 | struct mdinfo *newd; | |
00451d98 | 347 | if (sysfs_add_disk(&newa->info, d) < 0) |
6c3fb95c NB |
348 | continue; |
349 | newd = newa->info.devs; | |
350 | newd->state_fd = sysfs_open(a->devnum, | |
351 | newd->sys_name, | |
352 | "state"); | |
353 | newd->prev_state | |
354 | = read_dev_state(newd->state_fd); | |
355 | newd->curr_state = newd->prev_state; | |
356 | } | |
357 | queue_metadata_update(updates); | |
358 | replace_array(a->container, a, newa); | |
63d7cc78 | 359 | sysfs_set_str(&a->info, NULL, "sync_action", "recover"); |
6c3fb95c NB |
360 | } |
361 | } | |
549e9569 NB |
362 | } |
363 | ||
836759d5 DW |
364 | static int aa_ready(struct active_array *aa) |
365 | { | |
366 | struct mdinfo *d; | |
367 | int level = aa->info.array.level; | |
368 | ||
369 | for (d = aa->info.devs; d; d = d->next) | |
370 | if (d->state_fd < 0) | |
371 | return 0; | |
372 | ||
373 | if (aa->info.state_fd < 0) | |
374 | return 0; | |
375 | ||
376 | if (level > 0 && (aa->action_fd < 0 || aa->resync_start_fd < 0)) | |
377 | return 0; | |
378 | ||
379 | if (!aa->container) | |
380 | return 0; | |
381 | ||
382 | return 1; | |
383 | } | |
384 | ||
549e9569 | 385 | static void manage_new(struct mdstat_ent *mdstat, |
2a0bb19e DW |
386 | struct supertype *container, |
387 | struct active_array *victim) | |
549e9569 NB |
388 | { |
389 | /* A new array has appeared in this container. | |
390 | * Hopefully it is already recorded in the metadata. | |
391 | * Check, then create the new array to report it to | |
392 | * the monitor. | |
393 | */ | |
394 | ||
395 | struct active_array *new; | |
396 | struct mdinfo *mdi, *di; | |
cba0191b | 397 | char *inst; |
549e9569 | 398 | int i; |
f1d26766 | 399 | int failed = 0; |
549e9569 | 400 | |
836759d5 DW |
401 | /* check if array is ready to be monitored */ |
402 | if (!mdstat->active) | |
403 | return; | |
404 | ||
405 | mdi = sysfs_read(-1, mdstat->devnum, | |
406 | GET_LEVEL|GET_CHUNK|GET_DISKS|GET_COMPONENT| | |
f1d26766 | 407 | GET_DEGRADED|GET_DEVS|GET_OFFSET|GET_SIZE|GET_STATE); |
836759d5 | 408 | |
549e9569 NB |
409 | new = malloc(sizeof(*new)); |
410 | ||
836759d5 DW |
411 | if (!new || !mdi) { |
412 | if (mdi) | |
413 | sysfs_free(mdi); | |
414 | if (new) | |
415 | free(new); | |
416 | return; | |
417 | } | |
d52690ac NB |
418 | memset(new, 0, sizeof(*new)); |
419 | ||
549e9569 | 420 | new->devnum = mdstat->devnum; |
7e1432fb | 421 | strcpy(new->info.sys_name, devnum2devname(new->devnum)); |
549e9569 NB |
422 | |
423 | new->prev_state = new->curr_state = new->next_state = inactive; | |
424 | new->prev_action= new->curr_action= new->next_action= idle; | |
425 | ||
426 | new->container = container; | |
427 | ||
cba0191b | 428 | inst = &mdstat->metadata_version[10+strlen(container->devname)+1]; |
549e9569 | 429 | |
549e9569 | 430 | new->info.array = mdi->array; |
272bcc48 | 431 | new->info.component_size = mdi->component_size; |
549e9569 NB |
432 | |
433 | for (i = 0; i < new->info.array.raid_disks; i++) { | |
434 | struct mdinfo *newd = malloc(sizeof(*newd)); | |
435 | ||
436 | for (di = mdi->devs; di; di = di->next) | |
437 | if (i == di->disk.raid_disk) | |
438 | break; | |
439 | ||
440 | if (di) { | |
441 | memcpy(newd, di, sizeof(*newd)); | |
442 | ||
549e9569 NB |
443 | newd->state_fd = sysfs_open(new->devnum, |
444 | newd->sys_name, | |
445 | "state"); | |
446 | ||
447 | newd->prev_state = read_dev_state(newd->state_fd); | |
6c3fb95c | 448 | newd->curr_state = newd->prev_state; |
f1d26766 DW |
449 | } else if (failed + 1 > new->info.array.failed_disks) { |
450 | /* we cannot properly monitor without all working disks */ | |
836759d5 DW |
451 | new->container = NULL; |
452 | break; | |
f1d26766 DW |
453 | } else { |
454 | failed++; | |
455 | free(newd); | |
456 | continue; | |
549e9569 | 457 | } |
7e1432fb | 458 | sprintf(newd->sys_name, "rd%d", i); |
549e9569 NB |
459 | newd->next = new->info.devs; |
460 | new->info.devs = newd; | |
461 | } | |
836759d5 | 462 | |
549e9569 NB |
463 | new->action_fd = sysfs_open(new->devnum, NULL, "sync_action"); |
464 | new->info.state_fd = sysfs_open(new->devnum, NULL, "array_state"); | |
c052ba30 | 465 | new->resync_start_fd = sysfs_open(new->devnum, NULL, "resync_start"); |
e9dd1598 | 466 | new->metadata_fd = sysfs_open(new->devnum, NULL, "metadata_version"); |
103f2410 | 467 | get_resync_start(new); |
4e6e574a DW |
468 | dprintf("%s: inst: %d action: %d state: %d\n", __func__, atoi(inst), |
469 | new->action_fd, new->info.state_fd); | |
549e9569 | 470 | |
4fa5aef9 | 471 | sysfs_free(mdi); |
836759d5 DW |
472 | |
473 | /* if everything checks out tell the metadata handler we want to | |
474 | * manage this instance | |
475 | */ | |
476 | if (!aa_ready(new) || container->ss->open_new(container, new, inst) < 0) { | |
477 | fprintf(stderr, "mdmon: failed to monitor %s\n", | |
478 | mdstat->metadata_version); | |
549e9569 | 479 | new->container = NULL; |
836759d5 | 480 | free_aa(new); |
93f7caca | 481 | } else { |
2a0bb19e | 482 | replace_array(container, victim, new); |
93f7caca DW |
483 | if (failed) { |
484 | new->check_degraded = 1; | |
485 | manage_member(mdstat, new); | |
486 | } | |
487 | } | |
549e9569 NB |
488 | } |
489 | ||
5d19760d | 490 | void manage(struct mdstat_ent *mdstat, struct supertype *container) |
549e9569 NB |
491 | { |
492 | /* We have just read mdstat and need to compare it with | |
493 | * the known active arrays. | |
494 | * Arrays with the wrong metadata are ignored. | |
495 | */ | |
496 | ||
497 | for ( ; mdstat ; mdstat = mdstat->next) { | |
498 | struct active_array *a; | |
499 | if (mdstat->devnum == container->devnum) { | |
500 | manage_container(mdstat, container); | |
501 | continue; | |
502 | } | |
503 | if (mdstat->metadata_version == NULL || | |
3c558363 N |
504 | strncmp(mdstat->metadata_version, "external:", 9) != 0 || |
505 | !is_subarray(mdstat->metadata_version+9) || | |
549e9569 NB |
506 | strncmp(mdstat->metadata_version+10, container->devname, |
507 | strlen(container->devname)) != 0 || | |
508 | mdstat->metadata_version[10+strlen(container->devname)] | |
509 | != '/') | |
510 | /* Not for this array */ | |
511 | continue; | |
512 | /* Looks like a member of this container */ | |
5d19760d | 513 | for (a = container->arrays; a; a = a->next) { |
549e9569 NB |
514 | if (mdstat->devnum == a->devnum) { |
515 | if (a->container) | |
516 | manage_member(mdstat, a); | |
517 | break; | |
518 | } | |
519 | } | |
2a0bb19e DW |
520 | if (a == NULL || !a->container) |
521 | manage_new(mdstat, container, a); | |
549e9569 NB |
522 | } |
523 | } | |
524 | ||
edd8d13c | 525 | static void handle_message(struct supertype *container, struct metadata_update *msg) |
3e70c845 | 526 | { |
edd8d13c NB |
527 | /* queue this metadata update through to the monitor */ |
528 | ||
529 | struct metadata_update *mu; | |
530 | ||
313a4a82 | 531 | if (msg->len <= 0) |
3c00ffbe N |
532 | while (update_queue_pending || update_queue) { |
533 | check_update_queue(container); | |
534 | usleep(15*1000); | |
535 | } | |
536 | ||
313a4a82 DW |
537 | if (msg->len == 0) { /* ping_monitor */ |
538 | int cnt; | |
539 | ||
3c00ffbe | 540 | cnt = monitor_loop_cnt; |
1eb252b8 N |
541 | if (cnt & 1) |
542 | cnt += 2; /* wait until next pselect */ | |
543 | else | |
544 | cnt += 3; /* wait for 2 pselects */ | |
545 | wakeup_monitor(); | |
3c00ffbe | 546 | |
1eb252b8 N |
547 | while (monitor_loop_cnt - cnt < 0) |
548 | usleep(10 * 1000); | |
313a4a82 DW |
549 | } else if (msg->len == -1) { /* ping_manager */ |
550 | struct mdstat_ent *mdstat = mdstat_read(1, 0); | |
551 | ||
552 | manage(mdstat, container); | |
553 | free_mdstat(mdstat); | |
edd8d13c NB |
554 | } else { |
555 | mu = malloc(sizeof(*mu)); | |
556 | mu->len = msg->len; | |
557 | mu->buf = msg->buf; | |
558 | msg->buf = NULL; | |
559 | mu->space = NULL; | |
560 | mu->next = NULL; | |
561 | if (container->ss->prepare_update) | |
562 | container->ss->prepare_update(container, mu); | |
563 | queue_metadata_update(mu); | |
564 | } | |
3e70c845 DW |
565 | } |
566 | ||
567 | void read_sock(struct supertype *container) | |
549e9569 NB |
568 | { |
569 | int fd; | |
bfa44e2e | 570 | struct metadata_update msg; |
b109d928 DW |
571 | int terminate = 0; |
572 | long fl; | |
573 | int tmo = 3; /* 3 second timeout before hanging up the socket */ | |
549e9569 | 574 | |
3e70c845 | 575 | fd = accept(container->sock, NULL, NULL); |
549e9569 NB |
576 | if (fd < 0) |
577 | return; | |
b109d928 DW |
578 | |
579 | fl = fcntl(fd, F_GETFL, 0); | |
580 | fl |= O_NONBLOCK; | |
581 | fcntl(fd, F_SETFL, fl); | |
582 | ||
583 | do { | |
584 | msg.buf = NULL; | |
585 | ||
586 | /* read and validate the message */ | |
587 | if (receive_message(fd, &msg, tmo) == 0) { | |
bfa44e2e NB |
588 | handle_message(container, &msg); |
589 | if (ack(fd, tmo) < 0) | |
590 | terminate = 1; | |
591 | } else | |
b109d928 | 592 | terminate = 1; |
b109d928 | 593 | |
b109d928 DW |
594 | } while (!terminate); |
595 | ||
549e9569 NB |
596 | close(fd); |
597 | } | |
1ed3f387 | 598 | |
e0d6609f NB |
599 | int exit_now = 0; |
600 | int manager_ready = 0; | |
549e9569 NB |
601 | void do_manager(struct supertype *container) |
602 | { | |
603 | struct mdstat_ent *mdstat; | |
4d43913c | 604 | sigset_t set; |
1ed3f387 | 605 | |
4d43913c NB |
606 | sigprocmask(SIG_UNBLOCK, NULL, &set); |
607 | sigdelset(&set, SIGUSR1); | |
549e9569 NB |
608 | |
609 | do { | |
1ed3f387 | 610 | |
e0d6609f NB |
611 | if (exit_now) |
612 | exit(0); | |
613 | ||
3c00ffbe N |
614 | /* Can only 'manage' things if 'monitor' is not making |
615 | * structural changes to metadata, so need to check | |
616 | * update_queue | |
617 | */ | |
618 | if (update_queue == NULL) { | |
619 | mdstat = mdstat_read(1, 0); | |
549e9569 | 620 | |
3c00ffbe | 621 | manage(mdstat, container); |
549e9569 | 622 | |
3c00ffbe | 623 | read_sock(container); |
4fa5aef9 | 624 | |
3c00ffbe N |
625 | free_mdstat(mdstat); |
626 | } | |
1ed3f387 NB |
627 | remove_old(); |
628 | ||
2e735d19 NB |
629 | check_update_queue(container); |
630 | ||
e0d6609f | 631 | manager_ready = 1; |
4d43913c | 632 | |
3c00ffbe N |
633 | if (update_queue == NULL) |
634 | mdstat_wait_fd(container->sock, &set); | |
635 | else | |
636 | /* If an update is happening, just wait for signal */ | |
637 | pselect(0, NULL, NULL, NULL, NULL, &set); | |
549e9569 NB |
638 | } while(1); |
639 | } |