]> git.ipfire.org Git - thirdparty/mdadm.git/blame - managemon.c
Some fixes to make failures in ddf get handled properly.
[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 *
29 * - assist with activating spares by opening relevant sysfs file.
30 *
31 * - Pass on metadata updates from external programs such as
32 * mdadm creating a new array.
33 *
34 * This is most-messy.
35 * It might involve adding a new array or changing the status of
36 * a spare, or any reconfig that the kernel doesn't get involved in.
37 *
38 * The required updates are received via a named pipe. There will
39 * be one named pipe for each container. Each message contains a
40 * sync marker: 0x5a5aa5a5, A byte count, and the message. This is
41 * passed to the metadata handler which will interpret and process it.
42 * For 'DDF' messages are internal data blocks with the leading
43 * 'magic number' signifying what sort of data it is.
44 *
45 */
46
47/*
48 * We select on /proc/mdstat and the named pipe.
49 * We create new arrays or updated version of arrays and slip
50 * them into the head of the list, then signal 'monitor' via a pipe write.
51 * 'monitor' will notice and place the old array on a return list.
52 * Metadata updates are placed on a queue just like they arrive
53 * from the named pipe.
54 *
55 * When new arrays are found based on correct metadata string, we
56 * need to identify them with an entry in the metadata. Maybe we require
57 * the metadata to be mdX/NN when NN is the index into an appropriate table.
58 *
59 */
60
61/*
62 * List of tasks:
63 * - Watch for spares to be added to the container, and write updated
64 * metadata to them.
65 * - Watch for new arrays using this container, confirm they match metadata
66 * and if so, start monitoring them
67 * - Watch for spares being added to monitored arrays. This shouldn't
68 * happen, as we should do all the adding. Just remove them.
69 * - Watch for change in raid-disks, chunk-size, etc. Update metadata and
70 * start a reshape.
71 */
72#ifndef _GNU_SOURCE
73#define _GNU_SOURCE
74#endif
75#include "mdadm.h"
76#include "mdmon.h"
77#include <sys/socket.h>
1ed3f387 78#include <signal.h>
549e9569 79
2a0bb19e
DW
80static void close_aa(struct active_array *aa)
81{
82 struct mdinfo *d;
83
84 for (d = aa->info.devs; d; d = d->next)
85 close(d->state_fd);
86
87 close(aa->action_fd);
88 close(aa->info.state_fd);
89 close(aa->resync_start_fd);
2a0bb19e
DW
90}
91
549e9569
NB
92static void free_aa(struct active_array *aa)
93{
2a0bb19e
DW
94 /* Note that this doesn't close fds if they are being used
95 * by a clone. ->container will be set for a clone
549e9569 96 */
2a0bb19e
DW
97 if (!aa->container)
98 close_aa(aa);
549e9569
NB
99 while (aa->info.devs) {
100 struct mdinfo *d = aa->info.devs;
101 aa->info.devs = d->next;
102 free(d);
103 }
104 free(aa);
105}
106
2a0bb19e
DW
107static void write_wakeup(struct supertype *c)
108{
3e70c845
DW
109 static struct md_generic_cmd cmd = { .action = md_action_ping_monitor };
110 int err;
111
112 active_cmd = &cmd;
113
114 /* send the monitor thread a pointer to the ping action */
115 write(c->mgr_pipe[1], &err, 1);
116 read(c->mon_pipe[0], &err, 1);
2a0bb19e
DW
117}
118
1ed3f387
NB
119static void remove_old(void)
120{
121 if (discard_this) {
122 discard_this->next = NULL;
123 free_aa(discard_this);
124 if (pending_discard == discard_this)
125 pending_discard = NULL;
126 discard_this = NULL;
127 }
128}
129
549e9569
NB
130static void replace_array(struct supertype *container,
131 struct active_array *old,
132 struct active_array *new)
133{
134 /* To replace an array, we add it to the top of the list
135 * marked with ->replaces to point to the original.
136 * 'monitor' will take the original out of the list
137 * and put it on 'discard_this'. We take it from there
138 * and discard it.
139 */
1ed3f387 140 remove_old();
549e9569 141 while (pending_discard) {
1ed3f387 142 write_wakeup(container);
549e9569
NB
143 while (discard_this == NULL)
144 sleep(1);
1ed3f387 145 remove_old();
549e9569
NB
146 }
147 pending_discard = old;
148 new->replaces = old;
149 new->next = container->arrays;
150 container->arrays = new;
2a0bb19e 151 write_wakeup(container);
549e9569
NB
152}
153
2e735d19
NB
154struct metadata_update *update_queue = NULL;
155struct metadata_update *update_queue_handled = NULL;
156struct metadata_update *update_queue_pending = NULL;
157
158void check_update_queue(struct supertype *container)
159{
160 while (update_queue_handled) {
161 struct metadata_update *this = update_queue_handled;
162 update_queue_handled = this->next;
163 free(this->buf);
164 free(this);
165 }
166 if (update_queue == NULL &&
167 update_queue_pending) {
168 update_queue = update_queue_pending;
169 update_queue_pending = NULL;
170 write_wakeup(container);
171 }
172}
173
174void queue_metadata_update(struct metadata_update *mu)
175{
176 struct metadata_update **qp;
177
178 qp = &update_queue_pending;
179 while (*qp)
180 qp = & ((*qp)->next);
181 *qp = mu;
182}
183
184void wait_update_handled(void)
185{
186 /* Wait for any pending update to be handled by monitor.
187 * i.e. wait until update_queue is NULL
188 */
189 while (update_queue)
190 usleep(100 * 1000);
191}
192
549e9569
NB
193static void manage_container(struct mdstat_ent *mdstat,
194 struct supertype *container)
195{
196 /* The only thing of interest here is if a new device
197 * has been added to the container. We add it to the
198 * array ignoring any metadata on it.
199 * FIXME should we look for compatible metadata and take hints
200 * about spare assignment.... probably not.
201 *
202 */
203 if (mdstat->devcnt != container->devcnt) {
204 /* read /sys/block/NAME/md/dev-??/block/dev to find out
205 * what is there, and compare with container->info.devs
206 * To see what is removed and what is added.
207 * These need to be remove from, or added to, the array
208 */
209 // FIXME
210 container->devcnt = mdstat->devcnt;
211 }
212}
213
214static void manage_member(struct mdstat_ent *mdstat,
215 struct active_array *a)
216{
217 /* Compare mdstat info with known state of member array.
218 * We do not need to look for device state changes here, that
219 * is dealt with by the monitor.
220 *
221 * We just look for changes which suggest that a reshape is
222 * being requested.
223 * Unfortunately decreases in raid_disks don't show up in
224 * mdstat until the reshape completes FIXME.
225 */
226 // FIXME
227 a->info.array.raid_disks = mdstat->raid_disks;
228 a->info.array.chunk_size = mdstat->chunk_size;
229 // MORE
230
231}
232
549e9569 233static void manage_new(struct mdstat_ent *mdstat,
2a0bb19e
DW
234 struct supertype *container,
235 struct active_array *victim)
549e9569
NB
236{
237 /* A new array has appeared in this container.
238 * Hopefully it is already recorded in the metadata.
239 * Check, then create the new array to report it to
240 * the monitor.
241 */
242
243 struct active_array *new;
244 struct mdinfo *mdi, *di;
cba0191b 245 char *inst;
549e9569
NB
246 int i;
247
248 new = malloc(sizeof(*new));
249
d52690ac
NB
250 memset(new, 0, sizeof(*new));
251
549e9569
NB
252 new->devnum = mdstat->devnum;
253
254 new->prev_state = new->curr_state = new->next_state = inactive;
255 new->prev_action= new->curr_action= new->next_action= idle;
256
257 new->container = container;
258
cba0191b 259 inst = &mdstat->metadata_version[10+strlen(container->devname)+1];
549e9569
NB
260
261 mdi = sysfs_read(-1, new->devnum,
262 GET_LEVEL|GET_CHUNK|GET_DISKS|
263 GET_DEVS|GET_OFFSET|GET_SIZE|GET_STATE);
264 if (!mdi) {
265 /* Eeek. Cannot monitor this array.
266 * Mark it to be ignored by setting container to NULL
267 */
268 new->container = NULL;
2a0bb19e 269 replace_array(container, victim, new);
549e9569
NB
270 return;
271 }
272
273 new->info.array = mdi->array;
274
275 for (i = 0; i < new->info.array.raid_disks; i++) {
276 struct mdinfo *newd = malloc(sizeof(*newd));
277
278 for (di = mdi->devs; di; di = di->next)
279 if (i == di->disk.raid_disk)
280 break;
281
282 if (di) {
283 memcpy(newd, di, sizeof(*newd));
284
285 sprintf(newd->sys_name, "rd%d", i);
286
287 newd->state_fd = sysfs_open(new->devnum,
288 newd->sys_name,
289 "state");
290
291 newd->prev_state = read_dev_state(newd->state_fd);
292 newd->curr_state = newd->curr_state;
293 } else {
294 newd->state_fd = -1;
295 }
296 newd->next = new->info.devs;
297 new->info.devs = newd;
298 }
299 new->action_fd = sysfs_open(new->devnum, NULL, "sync_action");
300 new->info.state_fd = sysfs_open(new->devnum, NULL, "array_state");
c052ba30 301 new->resync_start_fd = sysfs_open(new->devnum, NULL, "resync_start");
77402e51 302 new->resync_start = 0;
549e9569 303
4fa5aef9 304 sysfs_free(mdi);
549e9569
NB
305 // finds and compares.
306 if (container->ss->open_new(container, new, inst) < 0) {
307 // FIXME close all those files
308 new->container = NULL;
2a0bb19e 309 replace_array(container, victim, new);
549e9569
NB
310 return;
311 }
2a0bb19e 312 replace_array(container, victim, new);
549e9569
NB
313 return;
314}
315
5d19760d 316void manage(struct mdstat_ent *mdstat, struct supertype *container)
549e9569
NB
317{
318 /* We have just read mdstat and need to compare it with
319 * the known active arrays.
320 * Arrays with the wrong metadata are ignored.
321 */
322
323 for ( ; mdstat ; mdstat = mdstat->next) {
324 struct active_array *a;
325 if (mdstat->devnum == container->devnum) {
326 manage_container(mdstat, container);
327 continue;
328 }
329 if (mdstat->metadata_version == NULL ||
330 strncmp(mdstat->metadata_version, "external:/", 10) != 0 ||
331 strncmp(mdstat->metadata_version+10, container->devname,
332 strlen(container->devname)) != 0 ||
333 mdstat->metadata_version[10+strlen(container->devname)]
334 != '/')
335 /* Not for this array */
336 continue;
337 /* Looks like a member of this container */
5d19760d 338 for (a = container->arrays; a; a = a->next) {
549e9569
NB
339 if (mdstat->devnum == a->devnum) {
340 if (a->container)
341 manage_member(mdstat, a);
342 break;
343 }
344 }
2a0bb19e
DW
345 if (a == NULL || !a->container)
346 manage_new(mdstat, container, a);
549e9569
NB
347 }
348}
349
3e70c845
DW
350static int handle_message(struct supertype *container, struct md_message *msg)
351{
352 int err;
353 struct md_generic_cmd *cmd = msg->buf;
354
355 if (!cmd)
356 return 0;
357
358 switch (cmd->action) {
359 case md_action_remove_device:
360
361 /* forward to the monitor */
362 active_cmd = cmd;
363 write(container->mgr_pipe[1], &err, 1);
364 read(container->mon_pipe[0], &err, 1);
365 return err;
366
367 default:
368 return -1;
369 }
370}
371
372void read_sock(struct supertype *container)
549e9569
NB
373{
374 int fd;
b109d928
DW
375 struct md_message msg;
376 int terminate = 0;
377 long fl;
378 int tmo = 3; /* 3 second timeout before hanging up the socket */
549e9569 379
3e70c845 380 fd = accept(container->sock, NULL, NULL);
549e9569
NB
381 if (fd < 0)
382 return;
b109d928
DW
383
384 fl = fcntl(fd, F_GETFL, 0);
385 fl |= O_NONBLOCK;
386 fcntl(fd, F_SETFL, fl);
387
388 do {
3e70c845
DW
389 int err;
390
b109d928
DW
391 msg.buf = NULL;
392
393 /* read and validate the message */
394 if (receive_message(fd, &msg, tmo) == 0) {
3e70c845
DW
395 err = handle_message(container, &msg);
396 if (!err)
397 ack(fd, msg.seq, tmo);
398 else
399 nack(fd, err, tmo);
b109d928
DW
400 } else {
401 terminate = 1;
402 nack(fd, -1, tmo);
403 }
404
405 if (msg.buf)
406 free(msg.buf);
407 } while (!terminate);
408
549e9569
NB
409 close(fd);
410}
1ed3f387
NB
411
412static int woke = 0;
413void wake_me(int sig)
414{
415 woke = 1;
416}
417
e0d6609f
NB
418int exit_now = 0;
419int manager_ready = 0;
549e9569
NB
420void do_manager(struct supertype *container)
421{
422 struct mdstat_ent *mdstat;
1ed3f387
NB
423 sigset_t block, orig;
424
425 sigemptyset(&block);
426 sigaddset(&block, SIGUSR1);
427
428 signal(SIGUSR1, wake_me);
549e9569
NB
429
430 do {
1ed3f387
NB
431 woke = 0;
432
e0d6609f
NB
433 if (exit_now)
434 exit(0);
435
549e9569
NB
436 mdstat = mdstat_read(1, 0);
437
5d19760d 438 manage(mdstat, container);
549e9569 439
3e70c845 440 read_sock(container);
549e9569 441
4fa5aef9
DW
442 free_mdstat(mdstat);
443
1ed3f387
NB
444 remove_old();
445
2e735d19
NB
446 check_update_queue(container);
447
e0d6609f 448 manager_ready = 1;
1ed3f387
NB
449 sigprocmask(SIG_SETMASK, &block, &orig);
450 if (woke == 0)
451 mdstat_wait_fd(container->sock, &orig);
452 sigprocmask(SIG_SETMASK, &orig, NULL);
549e9569
NB
453 } while(1);
454}