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