]>
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" | |
88 | #include <sys/socket.h> | |
1ed3f387 | 89 | #include <signal.h> |
549e9569 | 90 | |
2a0bb19e DW |
91 | static void close_aa(struct active_array *aa) |
92 | { | |
93 | struct mdinfo *d; | |
94 | ||
95 | for (d = aa->info.devs; d; d = d->next) | |
96 | close(d->state_fd); | |
97 | ||
98 | close(aa->action_fd); | |
99 | close(aa->info.state_fd); | |
100 | close(aa->resync_start_fd); | |
2a0bb19e DW |
101 | } |
102 | ||
549e9569 NB |
103 | static void free_aa(struct active_array *aa) |
104 | { | |
2a0bb19e DW |
105 | /* Note that this doesn't close fds if they are being used |
106 | * by a clone. ->container will be set for a clone | |
549e9569 | 107 | */ |
4e6e574a | 108 | dprintf("%s: devnum: %d\n", __func__, aa->devnum); |
2a0bb19e DW |
109 | if (!aa->container) |
110 | close_aa(aa); | |
549e9569 NB |
111 | while (aa->info.devs) { |
112 | struct mdinfo *d = aa->info.devs; | |
113 | aa->info.devs = d->next; | |
114 | free(d); | |
115 | } | |
116 | free(aa); | |
117 | } | |
118 | ||
6c3fb95c NB |
119 | static struct active_array *duplicate_aa(struct active_array *aa) |
120 | { | |
121 | struct active_array *newa = malloc(sizeof(*newa)); | |
122 | struct mdinfo **dp1, **dp2; | |
123 | ||
124 | *newa = *aa; | |
125 | newa->next = NULL; | |
126 | newa->replaces = NULL; | |
127 | newa->info.next = NULL; | |
128 | ||
129 | dp2 = &newa->info.devs; | |
130 | ||
131 | for (dp1 = &aa->info.devs; *dp1; dp1 = &(*dp1)->next) { | |
132 | struct mdinfo *d; | |
133 | if ((*dp1)->state_fd < 0) | |
134 | continue; | |
135 | ||
136 | d = malloc(sizeof(*d)); | |
137 | *d = **dp1; | |
138 | *dp2 = d; | |
139 | dp2 = & d->next; | |
140 | } | |
7e1432fb | 141 | *dp2 = NULL; |
6c3fb95c NB |
142 | |
143 | return newa; | |
144 | } | |
145 | ||
2a0bb19e DW |
146 | static void write_wakeup(struct supertype *c) |
147 | { | |
3e70c845 DW |
148 | static struct md_generic_cmd cmd = { .action = md_action_ping_monitor }; |
149 | int err; | |
150 | ||
151 | active_cmd = &cmd; | |
152 | ||
153 | /* send the monitor thread a pointer to the ping action */ | |
154 | write(c->mgr_pipe[1], &err, 1); | |
2a0bb19e DW |
155 | } |
156 | ||
1ed3f387 NB |
157 | static void remove_old(void) |
158 | { | |
159 | if (discard_this) { | |
160 | discard_this->next = NULL; | |
161 | free_aa(discard_this); | |
162 | if (pending_discard == discard_this) | |
163 | pending_discard = NULL; | |
164 | discard_this = NULL; | |
165 | } | |
166 | } | |
167 | ||
549e9569 NB |
168 | static void replace_array(struct supertype *container, |
169 | struct active_array *old, | |
170 | struct active_array *new) | |
171 | { | |
172 | /* To replace an array, we add it to the top of the list | |
173 | * marked with ->replaces to point to the original. | |
174 | * 'monitor' will take the original out of the list | |
175 | * and put it on 'discard_this'. We take it from there | |
176 | * and discard it. | |
177 | */ | |
1ed3f387 | 178 | remove_old(); |
549e9569 | 179 | while (pending_discard) { |
1ed3f387 | 180 | write_wakeup(container); |
549e9569 NB |
181 | while (discard_this == NULL) |
182 | sleep(1); | |
1ed3f387 | 183 | remove_old(); |
549e9569 NB |
184 | } |
185 | pending_discard = old; | |
186 | new->replaces = old; | |
187 | new->next = container->arrays; | |
188 | container->arrays = new; | |
2a0bb19e | 189 | write_wakeup(container); |
549e9569 NB |
190 | } |
191 | ||
2e735d19 NB |
192 | struct metadata_update *update_queue = NULL; |
193 | struct metadata_update *update_queue_handled = NULL; | |
194 | struct metadata_update *update_queue_pending = NULL; | |
195 | ||
196 | void check_update_queue(struct supertype *container) | |
197 | { | |
198 | while (update_queue_handled) { | |
199 | struct metadata_update *this = update_queue_handled; | |
200 | update_queue_handled = this->next; | |
904c1ef7 NB |
201 | free(this->buf); |
202 | if (this->space) | |
203 | free(this->space); | |
2e735d19 NB |
204 | free(this); |
205 | } | |
206 | if (update_queue == NULL && | |
207 | update_queue_pending) { | |
208 | update_queue = update_queue_pending; | |
209 | update_queue_pending = NULL; | |
210 | write_wakeup(container); | |
211 | } | |
212 | } | |
213 | ||
6c3fb95c | 214 | static void queue_metadata_update(struct metadata_update *mu) |
2e735d19 NB |
215 | { |
216 | struct metadata_update **qp; | |
217 | ||
218 | qp = &update_queue_pending; | |
219 | while (*qp) | |
220 | qp = & ((*qp)->next); | |
221 | *qp = mu; | |
222 | } | |
223 | ||
224 | void wait_update_handled(void) | |
225 | { | |
226 | /* Wait for any pending update to be handled by monitor. | |
227 | * i.e. wait until update_queue is NULL | |
228 | */ | |
229 | while (update_queue) | |
230 | usleep(100 * 1000); | |
231 | } | |
232 | ||
549e9569 NB |
233 | static void manage_container(struct mdstat_ent *mdstat, |
234 | struct supertype *container) | |
235 | { | |
236 | /* The only thing of interest here is if a new device | |
237 | * has been added to the container. We add it to the | |
238 | * array ignoring any metadata on it. | |
239 | * FIXME should we look for compatible metadata and take hints | |
240 | * about spare assignment.... probably not. | |
549e9569 NB |
241 | */ |
242 | if (mdstat->devcnt != container->devcnt) { | |
243 | /* read /sys/block/NAME/md/dev-??/block/dev to find out | |
244 | * what is there, and compare with container->info.devs | |
245 | * To see what is removed and what is added. | |
246 | * These need to be remove from, or added to, the array | |
247 | */ | |
248 | // FIXME | |
249 | container->devcnt = mdstat->devcnt; | |
250 | } | |
251 | } | |
252 | ||
253 | static void manage_member(struct mdstat_ent *mdstat, | |
254 | struct active_array *a) | |
255 | { | |
256 | /* Compare mdstat info with known state of member array. | |
257 | * We do not need to look for device state changes here, that | |
258 | * is dealt with by the monitor. | |
259 | * | |
260 | * We just look for changes which suggest that a reshape is | |
261 | * being requested. | |
262 | * Unfortunately decreases in raid_disks don't show up in | |
263 | * mdstat until the reshape completes FIXME. | |
6c3fb95c NB |
264 | * |
265 | * Actually, we also want to handle degraded arrays here by | |
266 | * trying to find and assign a spare. | |
267 | * We do that whenever the monitor tells us too. | |
549e9569 NB |
268 | */ |
269 | // FIXME | |
270 | a->info.array.raid_disks = mdstat->raid_disks; | |
271 | a->info.array.chunk_size = mdstat->chunk_size; | |
272 | // MORE | |
273 | ||
6c3fb95c NB |
274 | if (a->check_degraded) { |
275 | struct metadata_update *updates = NULL; | |
276 | struct mdinfo *newdev; | |
277 | struct active_array *newa; | |
278 | wait_update_handled(); | |
279 | a->check_degraded = 0; | |
280 | ||
281 | /* The array may not be degraded, this is just a good time | |
282 | * to check. | |
283 | */ | |
284 | newdev = a->container->ss->activate_spare(a, &updates); | |
285 | if (newdev) { | |
286 | struct mdinfo *d; | |
287 | /* Cool, we can add a device or several. */ | |
288 | newa = duplicate_aa(a); | |
289 | /* suspend recovery - maybe not needed */ | |
290 | ||
291 | /* Add device to array and set offset/size/slot. | |
292 | * and open files for each newdev */ | |
293 | for (d = newdev; d ; d = d->next) { | |
294 | struct mdinfo *newd; | |
295 | if (sysfs_add_disk(&newa->info, d)) | |
296 | continue; | |
297 | newd = newa->info.devs; | |
298 | newd->state_fd = sysfs_open(a->devnum, | |
299 | newd->sys_name, | |
300 | "state"); | |
301 | newd->prev_state | |
302 | = read_dev_state(newd->state_fd); | |
303 | newd->curr_state = newd->prev_state; | |
304 | } | |
305 | queue_metadata_update(updates); | |
306 | replace_array(a->container, a, newa); | |
307 | sysfs_set_str(&a->info, NULL, "sync_action", "repair"); | |
308 | } | |
309 | } | |
549e9569 NB |
310 | } |
311 | ||
549e9569 | 312 | static void manage_new(struct mdstat_ent *mdstat, |
2a0bb19e DW |
313 | struct supertype *container, |
314 | struct active_array *victim) | |
549e9569 NB |
315 | { |
316 | /* A new array has appeared in this container. | |
317 | * Hopefully it is already recorded in the metadata. | |
318 | * Check, then create the new array to report it to | |
319 | * the monitor. | |
320 | */ | |
321 | ||
322 | struct active_array *new; | |
323 | struct mdinfo *mdi, *di; | |
cba0191b | 324 | char *inst; |
549e9569 NB |
325 | int i; |
326 | ||
327 | new = malloc(sizeof(*new)); | |
328 | ||
d52690ac NB |
329 | memset(new, 0, sizeof(*new)); |
330 | ||
549e9569 | 331 | new->devnum = mdstat->devnum; |
7e1432fb | 332 | strcpy(new->info.sys_name, devnum2devname(new->devnum)); |
549e9569 NB |
333 | |
334 | new->prev_state = new->curr_state = new->next_state = inactive; | |
335 | new->prev_action= new->curr_action= new->next_action= idle; | |
336 | ||
337 | new->container = container; | |
338 | ||
cba0191b | 339 | inst = &mdstat->metadata_version[10+strlen(container->devname)+1]; |
549e9569 NB |
340 | |
341 | mdi = sysfs_read(-1, new->devnum, | |
342 | GET_LEVEL|GET_CHUNK|GET_DISKS| | |
343 | GET_DEVS|GET_OFFSET|GET_SIZE|GET_STATE); | |
344 | if (!mdi) { | |
345 | /* Eeek. Cannot monitor this array. | |
346 | * Mark it to be ignored by setting container to NULL | |
347 | */ | |
348 | new->container = NULL; | |
2a0bb19e | 349 | replace_array(container, victim, new); |
549e9569 NB |
350 | return; |
351 | } | |
352 | ||
353 | new->info.array = mdi->array; | |
354 | ||
355 | for (i = 0; i < new->info.array.raid_disks; i++) { | |
356 | struct mdinfo *newd = malloc(sizeof(*newd)); | |
357 | ||
358 | for (di = mdi->devs; di; di = di->next) | |
359 | if (i == di->disk.raid_disk) | |
360 | break; | |
361 | ||
362 | if (di) { | |
363 | memcpy(newd, di, sizeof(*newd)); | |
364 | ||
549e9569 NB |
365 | newd->state_fd = sysfs_open(new->devnum, |
366 | newd->sys_name, | |
367 | "state"); | |
368 | ||
369 | newd->prev_state = read_dev_state(newd->state_fd); | |
6c3fb95c | 370 | newd->curr_state = newd->prev_state; |
549e9569 NB |
371 | } else { |
372 | newd->state_fd = -1; | |
7e1432fb NB |
373 | newd->disk.raid_disk = i; |
374 | newd->prev_state = DS_REMOVE; | |
375 | newd->curr_state = DS_REMOVE; | |
549e9569 | 376 | } |
7e1432fb | 377 | sprintf(newd->sys_name, "rd%d", i); |
549e9569 NB |
378 | newd->next = new->info.devs; |
379 | new->info.devs = newd; | |
380 | } | |
381 | new->action_fd = sysfs_open(new->devnum, NULL, "sync_action"); | |
382 | new->info.state_fd = sysfs_open(new->devnum, NULL, "array_state"); | |
c052ba30 | 383 | new->resync_start_fd = sysfs_open(new->devnum, NULL, "resync_start"); |
77402e51 | 384 | new->resync_start = 0; |
4e6e574a DW |
385 | dprintf("%s: inst: %d action: %d state: %d\n", __func__, atoi(inst), |
386 | new->action_fd, new->info.state_fd); | |
549e9569 | 387 | |
4fa5aef9 | 388 | sysfs_free(mdi); |
549e9569 NB |
389 | // finds and compares. |
390 | if (container->ss->open_new(container, new, inst) < 0) { | |
391 | // FIXME close all those files | |
392 | new->container = NULL; | |
2a0bb19e | 393 | replace_array(container, victim, new); |
549e9569 NB |
394 | return; |
395 | } | |
2a0bb19e | 396 | replace_array(container, victim, new); |
549e9569 NB |
397 | return; |
398 | } | |
399 | ||
5d19760d | 400 | void manage(struct mdstat_ent *mdstat, struct supertype *container) |
549e9569 NB |
401 | { |
402 | /* We have just read mdstat and need to compare it with | |
403 | * the known active arrays. | |
404 | * Arrays with the wrong metadata are ignored. | |
405 | */ | |
406 | ||
407 | for ( ; mdstat ; mdstat = mdstat->next) { | |
408 | struct active_array *a; | |
409 | if (mdstat->devnum == container->devnum) { | |
410 | manage_container(mdstat, container); | |
411 | continue; | |
412 | } | |
413 | if (mdstat->metadata_version == NULL || | |
414 | strncmp(mdstat->metadata_version, "external:/", 10) != 0 || | |
415 | strncmp(mdstat->metadata_version+10, container->devname, | |
416 | strlen(container->devname)) != 0 || | |
417 | mdstat->metadata_version[10+strlen(container->devname)] | |
418 | != '/') | |
419 | /* Not for this array */ | |
420 | continue; | |
421 | /* Looks like a member of this container */ | |
5d19760d | 422 | for (a = container->arrays; a; a = a->next) { |
549e9569 NB |
423 | if (mdstat->devnum == a->devnum) { |
424 | if (a->container) | |
425 | manage_member(mdstat, a); | |
426 | break; | |
427 | } | |
428 | } | |
2a0bb19e DW |
429 | if (a == NULL || !a->container) |
430 | manage_new(mdstat, container, a); | |
549e9569 NB |
431 | } |
432 | } | |
433 | ||
3e70c845 DW |
434 | static int handle_message(struct supertype *container, struct md_message *msg) |
435 | { | |
3e70c845 DW |
436 | struct md_generic_cmd *cmd = msg->buf; |
437 | ||
438 | if (!cmd) | |
439 | return 0; | |
440 | ||
441 | switch (cmd->action) { | |
3e70c845 DW |
442 | |
443 | default: | |
444 | return -1; | |
445 | } | |
446 | } | |
447 | ||
448 | void read_sock(struct supertype *container) | |
549e9569 NB |
449 | { |
450 | int fd; | |
b109d928 DW |
451 | struct md_message msg; |
452 | int terminate = 0; | |
453 | long fl; | |
454 | int tmo = 3; /* 3 second timeout before hanging up the socket */ | |
549e9569 | 455 | |
3e70c845 | 456 | fd = accept(container->sock, NULL, NULL); |
549e9569 NB |
457 | if (fd < 0) |
458 | return; | |
b109d928 DW |
459 | |
460 | fl = fcntl(fd, F_GETFL, 0); | |
461 | fl |= O_NONBLOCK; | |
462 | fcntl(fd, F_SETFL, fl); | |
463 | ||
464 | do { | |
3e70c845 DW |
465 | int err; |
466 | ||
b109d928 DW |
467 | msg.buf = NULL; |
468 | ||
469 | /* read and validate the message */ | |
470 | if (receive_message(fd, &msg, tmo) == 0) { | |
3e70c845 DW |
471 | err = handle_message(container, &msg); |
472 | if (!err) | |
473 | ack(fd, msg.seq, tmo); | |
474 | else | |
475 | nack(fd, err, tmo); | |
b109d928 DW |
476 | } else { |
477 | terminate = 1; | |
478 | nack(fd, -1, tmo); | |
479 | } | |
480 | ||
481 | if (msg.buf) | |
482 | free(msg.buf); | |
483 | } while (!terminate); | |
484 | ||
549e9569 NB |
485 | close(fd); |
486 | } | |
1ed3f387 NB |
487 | |
488 | static int woke = 0; | |
489 | void wake_me(int sig) | |
490 | { | |
491 | woke = 1; | |
492 | } | |
493 | ||
e0d6609f NB |
494 | int exit_now = 0; |
495 | int manager_ready = 0; | |
549e9569 NB |
496 | void do_manager(struct supertype *container) |
497 | { | |
498 | struct mdstat_ent *mdstat; | |
1ed3f387 NB |
499 | sigset_t block, orig; |
500 | ||
501 | sigemptyset(&block); | |
502 | sigaddset(&block, SIGUSR1); | |
503 | ||
504 | signal(SIGUSR1, wake_me); | |
549e9569 NB |
505 | |
506 | do { | |
1ed3f387 NB |
507 | woke = 0; |
508 | ||
e0d6609f NB |
509 | if (exit_now) |
510 | exit(0); | |
511 | ||
549e9569 NB |
512 | mdstat = mdstat_read(1, 0); |
513 | ||
5d19760d | 514 | manage(mdstat, container); |
549e9569 | 515 | |
3e70c845 | 516 | read_sock(container); |
549e9569 | 517 | |
4fa5aef9 DW |
518 | free_mdstat(mdstat); |
519 | ||
1ed3f387 NB |
520 | remove_old(); |
521 | ||
2e735d19 NB |
522 | check_update_queue(container); |
523 | ||
e0d6609f | 524 | manager_ready = 1; |
1ed3f387 NB |
525 | sigprocmask(SIG_SETMASK, &block, &orig); |
526 | if (woke == 0) | |
527 | mdstat_wait_fd(container->sock, &orig); | |
528 | sigprocmask(SIG_SETMASK, &orig, NULL); | |
549e9569 NB |
529 | } while(1); |
530 | } |