]> git.ipfire.org Git - thirdparty/mdadm.git/blame - managemon.c
Remove mgr_pipe for communicating from manage to monitor.
[thirdparty/mdadm.git] / managemon.c
CommitLineData
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
92static 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
104static 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
120static 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 147static 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
154static 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;
162 }
163}
164
549e9569
NB
165static void replace_array(struct supertype *container,
166 struct active_array *old,
167 struct active_array *new)
168{
169 /* To replace an array, we add it to the top of the list
170 * marked with ->replaces to point to the original.
171 * 'monitor' will take the original out of the list
172 * and put it on 'discard_this'. We take it from there
173 * and discard it.
174 */
1ed3f387 175 remove_old();
549e9569 176 while (pending_discard) {
4d43913c 177 wakeup_monitor();
549e9569
NB
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
189struct metadata_update *update_queue = NULL;
190struct metadata_update *update_queue_handled = NULL;
191struct metadata_update *update_queue_pending = NULL;
192
193void 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 211static 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
221void wait_update_handled(void)
222{
223 /* Wait for any pending update to be handled by monitor.
224 * i.e. wait until update_queue is NULL
225 */
226 while (update_queue)
227 usleep(100 * 1000);
228}
229
549e9569
NB
230static void manage_container(struct mdstat_ent *mdstat,
231 struct supertype *container)
232{
233 /* The only thing of interest here is if a new device
234 * has been added to the container. We add it to the
235 * array ignoring any metadata on it.
236 * FIXME should we look for compatible metadata and take hints
237 * about spare assignment.... probably not.
549e9569
NB
238 */
239 if (mdstat->devcnt != container->devcnt) {
240 /* read /sys/block/NAME/md/dev-??/block/dev to find out
241 * what is there, and compare with container->info.devs
242 * To see what is removed and what is added.
243 * These need to be remove from, or added to, the array
244 */
245 // FIXME
246 container->devcnt = mdstat->devcnt;
247 }
248}
249
250static void manage_member(struct mdstat_ent *mdstat,
251 struct active_array *a)
252{
253 /* Compare mdstat info with known state of member array.
254 * We do not need to look for device state changes here, that
255 * is dealt with by the monitor.
256 *
257 * We just look for changes which suggest that a reshape is
258 * being requested.
259 * Unfortunately decreases in raid_disks don't show up in
260 * mdstat until the reshape completes FIXME.
6c3fb95c
NB
261 *
262 * Actually, we also want to handle degraded arrays here by
263 * trying to find and assign a spare.
264 * We do that whenever the monitor tells us too.
549e9569
NB
265 */
266 // FIXME
267 a->info.array.raid_disks = mdstat->raid_disks;
268 a->info.array.chunk_size = mdstat->chunk_size;
269 // MORE
270
6c3fb95c
NB
271 if (a->check_degraded) {
272 struct metadata_update *updates = NULL;
273 struct mdinfo *newdev;
274 struct active_array *newa;
275 wait_update_handled();
276 a->check_degraded = 0;
277
278 /* The array may not be degraded, this is just a good time
279 * to check.
280 */
281 newdev = a->container->ss->activate_spare(a, &updates);
282 if (newdev) {
283 struct mdinfo *d;
284 /* Cool, we can add a device or several. */
285 newa = duplicate_aa(a);
286 /* suspend recovery - maybe not needed */
287
288 /* Add device to array and set offset/size/slot.
289 * and open files for each newdev */
290 for (d = newdev; d ; d = d->next) {
291 struct mdinfo *newd;
292 if (sysfs_add_disk(&newa->info, d))
293 continue;
294 newd = newa->info.devs;
295 newd->state_fd = sysfs_open(a->devnum,
296 newd->sys_name,
297 "state");
298 newd->prev_state
299 = read_dev_state(newd->state_fd);
300 newd->curr_state = newd->prev_state;
301 }
302 queue_metadata_update(updates);
303 replace_array(a->container, a, newa);
304 sysfs_set_str(&a->info, NULL, "sync_action", "repair");
305 }
306 }
549e9569
NB
307}
308
549e9569 309static void manage_new(struct mdstat_ent *mdstat,
2a0bb19e
DW
310 struct supertype *container,
311 struct active_array *victim)
549e9569
NB
312{
313 /* A new array has appeared in this container.
314 * Hopefully it is already recorded in the metadata.
315 * Check, then create the new array to report it to
316 * the monitor.
317 */
318
319 struct active_array *new;
320 struct mdinfo *mdi, *di;
cba0191b 321 char *inst;
549e9569
NB
322 int i;
323
324 new = malloc(sizeof(*new));
325
d52690ac
NB
326 memset(new, 0, sizeof(*new));
327
549e9569 328 new->devnum = mdstat->devnum;
7e1432fb 329 strcpy(new->info.sys_name, devnum2devname(new->devnum));
549e9569
NB
330
331 new->prev_state = new->curr_state = new->next_state = inactive;
332 new->prev_action= new->curr_action= new->next_action= idle;
333
334 new->container = container;
335
cba0191b 336 inst = &mdstat->metadata_version[10+strlen(container->devname)+1];
549e9569
NB
337
338 mdi = sysfs_read(-1, new->devnum,
339 GET_LEVEL|GET_CHUNK|GET_DISKS|
340 GET_DEVS|GET_OFFSET|GET_SIZE|GET_STATE);
341 if (!mdi) {
342 /* Eeek. Cannot monitor this array.
343 * Mark it to be ignored by setting container to NULL
344 */
345 new->container = NULL;
2a0bb19e 346 replace_array(container, victim, new);
549e9569
NB
347 return;
348 }
349
350 new->info.array = mdi->array;
351
352 for (i = 0; i < new->info.array.raid_disks; i++) {
353 struct mdinfo *newd = malloc(sizeof(*newd));
354
355 for (di = mdi->devs; di; di = di->next)
356 if (i == di->disk.raid_disk)
357 break;
358
359 if (di) {
360 memcpy(newd, di, sizeof(*newd));
361
549e9569
NB
362 newd->state_fd = sysfs_open(new->devnum,
363 newd->sys_name,
364 "state");
365
366 newd->prev_state = read_dev_state(newd->state_fd);
6c3fb95c 367 newd->curr_state = newd->prev_state;
549e9569
NB
368 } else {
369 newd->state_fd = -1;
7e1432fb
NB
370 newd->disk.raid_disk = i;
371 newd->prev_state = DS_REMOVE;
372 newd->curr_state = DS_REMOVE;
549e9569 373 }
7e1432fb 374 sprintf(newd->sys_name, "rd%d", i);
549e9569
NB
375 newd->next = new->info.devs;
376 new->info.devs = newd;
377 }
378 new->action_fd = sysfs_open(new->devnum, NULL, "sync_action");
379 new->info.state_fd = sysfs_open(new->devnum, NULL, "array_state");
c052ba30 380 new->resync_start_fd = sysfs_open(new->devnum, NULL, "resync_start");
77402e51 381 new->resync_start = 0;
4e6e574a
DW
382 dprintf("%s: inst: %d action: %d state: %d\n", __func__, atoi(inst),
383 new->action_fd, new->info.state_fd);
549e9569 384
4fa5aef9 385 sysfs_free(mdi);
549e9569
NB
386 // finds and compares.
387 if (container->ss->open_new(container, new, inst) < 0) {
388 // FIXME close all those files
389 new->container = NULL;
2a0bb19e 390 replace_array(container, victim, new);
549e9569
NB
391 return;
392 }
2a0bb19e 393 replace_array(container, victim, new);
549e9569
NB
394 return;
395}
396
5d19760d 397void manage(struct mdstat_ent *mdstat, struct supertype *container)
549e9569
NB
398{
399 /* We have just read mdstat and need to compare it with
400 * the known active arrays.
401 * Arrays with the wrong metadata are ignored.
402 */
403
404 for ( ; mdstat ; mdstat = mdstat->next) {
405 struct active_array *a;
406 if (mdstat->devnum == container->devnum) {
407 manage_container(mdstat, container);
408 continue;
409 }
410 if (mdstat->metadata_version == NULL ||
411 strncmp(mdstat->metadata_version, "external:/", 10) != 0 ||
412 strncmp(mdstat->metadata_version+10, container->devname,
413 strlen(container->devname)) != 0 ||
414 mdstat->metadata_version[10+strlen(container->devname)]
415 != '/')
416 /* Not for this array */
417 continue;
418 /* Looks like a member of this container */
5d19760d 419 for (a = container->arrays; a; a = a->next) {
549e9569
NB
420 if (mdstat->devnum == a->devnum) {
421 if (a->container)
422 manage_member(mdstat, a);
423 break;
424 }
425 }
2a0bb19e
DW
426 if (a == NULL || !a->container)
427 manage_new(mdstat, container, a);
549e9569
NB
428 }
429}
430
3e70c845
DW
431static int handle_message(struct supertype *container, struct md_message *msg)
432{
4d43913c 433 return -1;
3e70c845
DW
434}
435
436void read_sock(struct supertype *container)
549e9569
NB
437{
438 int fd;
b109d928
DW
439 struct md_message msg;
440 int terminate = 0;
441 long fl;
442 int tmo = 3; /* 3 second timeout before hanging up the socket */
549e9569 443
3e70c845 444 fd = accept(container->sock, NULL, NULL);
549e9569
NB
445 if (fd < 0)
446 return;
b109d928
DW
447
448 fl = fcntl(fd, F_GETFL, 0);
449 fl |= O_NONBLOCK;
450 fcntl(fd, F_SETFL, fl);
451
452 do {
3e70c845
DW
453 int err;
454
b109d928
DW
455 msg.buf = NULL;
456
457 /* read and validate the message */
458 if (receive_message(fd, &msg, tmo) == 0) {
3e70c845
DW
459 err = handle_message(container, &msg);
460 if (!err)
461 ack(fd, msg.seq, tmo);
462 else
463 nack(fd, err, tmo);
b109d928
DW
464 } else {
465 terminate = 1;
466 nack(fd, -1, tmo);
467 }
468
469 if (msg.buf)
470 free(msg.buf);
471 } while (!terminate);
472
549e9569
NB
473 close(fd);
474}
1ed3f387 475
e0d6609f
NB
476int exit_now = 0;
477int manager_ready = 0;
549e9569
NB
478void do_manager(struct supertype *container)
479{
480 struct mdstat_ent *mdstat;
4d43913c 481 sigset_t set;
1ed3f387 482
4d43913c
NB
483 sigprocmask(SIG_UNBLOCK, NULL, &set);
484 sigdelset(&set, SIGUSR1);
549e9569
NB
485
486 do {
1ed3f387 487
e0d6609f
NB
488 if (exit_now)
489 exit(0);
490
549e9569
NB
491 mdstat = mdstat_read(1, 0);
492
5d19760d 493 manage(mdstat, container);
549e9569 494
3e70c845 495 read_sock(container);
549e9569 496
4fa5aef9
DW
497 free_mdstat(mdstat);
498
1ed3f387
NB
499 remove_old();
500
2e735d19
NB
501 check_update_queue(container);
502
e0d6609f 503 manager_ready = 1;
4d43913c
NB
504
505 mdstat_wait_fd(container->sock, &set);
549e9569
NB
506 } while(1);
507}