]>
git.ipfire.org Git - thirdparty/mdadm.git/blob - managemon.c
3 * The management thread for monitoring active md arrays.
4 * This thread does things which might block such as memory
8 * - Find out about new arrays in this container.
9 * Allocate the data structures and open the files.
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.
16 * - Respond to reshape requests by allocating new data structures
17 * and opening new files.
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.
26 * - When a device is added to the container, we add it to the metadata
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.
34 * Check if number of failed devices suggests recovery is needed, and
36 * Ask metadata to allocate a spare device
37 * Add device as not in_sync and give a role
39 * Open sysfs files and pass to monitor.
40 * Make sure that monitor Starts recovery....
42 * - Pass on metadata updates from external programs such as
43 * mdadm creating a new array.
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.
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.
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.
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.
74 * - Watch for spares to be added to the container, and write updated
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
88 #include <sys/syscall.h>
89 #include <sys/socket.h>
92 static void close_aa(struct active_array
*aa
)
96 for (d
= aa
->info
.devs
; d
; d
= d
->next
)
100 close(aa
->info
.state_fd
);
101 close(aa
->resync_start_fd
);
104 static void free_aa(struct active_array
*aa
)
106 /* Note that this doesn't close fds if they are being used
107 * by a clone. ->container will be set for a clone
109 dprintf("%s: devnum: %d\n", __func__
, aa
->devnum
);
112 while (aa
->info
.devs
) {
113 struct mdinfo
*d
= aa
->info
.devs
;
114 aa
->info
.devs
= d
->next
;
120 static struct active_array
*duplicate_aa(struct active_array
*aa
)
122 struct active_array
*newa
= malloc(sizeof(*newa
));
123 struct mdinfo
**dp1
, **dp2
;
127 newa
->replaces
= NULL
;
128 newa
->info
.next
= NULL
;
130 dp2
= &newa
->info
.devs
;
132 for (dp1
= &aa
->info
.devs
; *dp1
; dp1
= &(*dp1
)->next
) {
134 if ((*dp1
)->state_fd
< 0)
137 d
= malloc(sizeof(*d
));
147 static void wakeup_monitor(void)
149 /* tgkill(getpid(), mon_tid, SIGUSR1); */
151 syscall(SYS_tgkill
, pid
, mon_tid
, SIGUSR1
);
154 static void remove_old(void)
157 discard_this
->next
= NULL
;
158 free_aa(discard_this
);
159 if (pending_discard
== discard_this
)
160 pending_discard
= NULL
;
166 static void replace_array(struct supertype
*container
,
167 struct active_array
*old
,
168 struct active_array
*new)
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
177 while (pending_discard
) {
178 while (discard_this
== NULL
)
182 pending_discard
= old
;
184 new->next
= container
->arrays
;
185 container
->arrays
= new;
189 struct metadata_update
*update_queue
= NULL
;
190 struct metadata_update
*update_queue_handled
= NULL
;
191 struct metadata_update
*update_queue_pending
= NULL
;
193 void check_update_queue(struct supertype
*container
)
195 while (update_queue_handled
) {
196 struct metadata_update
*this = update_queue_handled
;
197 update_queue_handled
= this->next
;
203 if (update_queue
== NULL
&&
204 update_queue_pending
) {
205 update_queue
= update_queue_pending
;
206 update_queue_pending
= NULL
;
211 static void queue_metadata_update(struct metadata_update
*mu
)
213 struct metadata_update
**qp
;
215 qp
= &update_queue_pending
;
217 qp
= & ((*qp
)->next
);
221 static void add_disk_to_container(struct supertype
*st
, struct mdinfo
*sd
)
225 struct metadata_update
*update
= NULL
;
226 mdu_disk_info_t dk
= {
228 .major
= sd
->disk
.major
,
229 .minor
= sd
->disk
.minor
,
234 dprintf("%s: add %d:%d to container\n",
235 __func__
, sd
->disk
.major
, sd
->disk
.minor
);
237 sprintf(nm
, "%d:%d", sd
->disk
.major
, sd
->disk
.minor
);
238 dfd
= dev_open(nm
, O_RDWR
);
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
;
249 static void manage_container(struct mdstat_ent
*mdstat
,
250 struct supertype
*container
)
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.
258 if (mdstat
->devcnt
!= container
->devcnt
) {
259 struct mdinfo
**cdp
, *cd
, *di
, *mdi
;
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
267 mdi
= sysfs_read(-1, mdstat
->devnum
, GET_DEVS
);
269 /* invalidate the current count so we can try again */
270 container
->devcnt
= -1;
274 /* check for removals */
275 for (cdp
= &container
->devs
; *cdp
; ) {
277 for (di
= mdi
->devs
; di
; di
= di
->next
)
278 if (di
->disk
.major
== (*cdp
)->disk
.major
&&
279 di
->disk
.minor
== (*cdp
)->disk
.minor
) {
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
)
298 add_disk_to_container(container
, di
);
301 container
->devcnt
= mdstat
->devcnt
;
305 static void manage_member(struct mdstat_ent
*mdstat
,
306 struct active_array
*a
)
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.
312 * We just look for changes which suggest that a reshape is
314 * Unfortunately decreases in raid_disks don't show up in
315 * mdstat until the reshape completes FIXME.
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.
322 a
->info
.array
.raid_disks
= mdstat
->raid_disks
;
323 a
->info
.array
.chunk_size
= mdstat
->chunk_size
;
326 if (a
->check_degraded
) {
327 struct metadata_update
*updates
= NULL
;
328 struct mdinfo
*newdev
;
329 struct active_array
*newa
;
331 a
->check_degraded
= 0;
333 /* The array may not be degraded, this is just a good time
336 newdev
= a
->container
->ss
->activate_spare(a
, &updates
);
339 /* Cool, we can add a device or several. */
340 newa
= duplicate_aa(a
);
341 /* suspend recovery - maybe not needed */
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
) {
347 if (sysfs_add_disk(&newa
->info
, d
) < 0)
349 newd
= malloc(sizeof(*newd
));
351 newd
->next
= newa
->info
.devs
;
352 newa
->info
.devs
= newd
;
354 newd
->state_fd
= sysfs_open(a
->devnum
,
358 = read_dev_state(newd
->state_fd
);
359 newd
->curr_state
= newd
->prev_state
;
361 queue_metadata_update(updates
);
362 replace_array(a
->container
, a
, newa
);
363 sysfs_set_str(&a
->info
, NULL
, "sync_action", "recover");
368 static int aa_ready(struct active_array
*aa
)
371 int level
= aa
->info
.array
.level
;
373 for (d
= aa
->info
.devs
; d
; d
= d
->next
)
377 if (aa
->info
.state_fd
< 0)
380 if (level
> 0 && (aa
->action_fd
< 0 || aa
->resync_start_fd
< 0))
389 static void manage_new(struct mdstat_ent
*mdstat
,
390 struct supertype
*container
,
391 struct active_array
*victim
)
393 /* A new array has appeared in this container.
394 * Hopefully it is already recorded in the metadata.
395 * Check, then create the new array to report it to
399 struct active_array
*new;
400 struct mdinfo
*mdi
, *di
;
405 /* check if array is ready to be monitored */
409 mdi
= sysfs_read(-1, mdstat
->devnum
,
410 GET_LEVEL
|GET_CHUNK
|GET_DISKS
|GET_COMPONENT
|
411 GET_DEGRADED
|GET_DEVS
|GET_OFFSET
|GET_SIZE
|GET_STATE
);
413 new = malloc(sizeof(*new));
422 memset(new, 0, sizeof(*new));
424 new->devnum
= mdstat
->devnum
;
425 strcpy(new->info
.sys_name
, devnum2devname(new->devnum
));
427 new->prev_state
= new->curr_state
= new->next_state
= inactive
;
428 new->prev_action
= new->curr_action
= new->next_action
= idle
;
430 new->container
= container
;
432 inst
= &mdstat
->metadata_version
[10+strlen(container
->devname
)+1];
434 new->info
.array
= mdi
->array
;
435 new->info
.component_size
= mdi
->component_size
;
437 for (i
= 0; i
< new->info
.array
.raid_disks
; i
++) {
438 struct mdinfo
*newd
= malloc(sizeof(*newd
));
440 for (di
= mdi
->devs
; di
; di
= di
->next
)
441 if (i
== di
->disk
.raid_disk
)
445 memcpy(newd
, di
, sizeof(*newd
));
447 newd
->state_fd
= sysfs_open(new->devnum
,
451 newd
->prev_state
= read_dev_state(newd
->state_fd
);
452 newd
->curr_state
= newd
->prev_state
;
453 } else if (failed
+ 1 > new->info
.array
.failed_disks
) {
454 /* we cannot properly monitor without all working disks */
455 new->container
= NULL
;
462 sprintf(newd
->sys_name
, "rd%d", i
);
463 newd
->next
= new->info
.devs
;
464 new->info
.devs
= newd
;
467 new->action_fd
= sysfs_open(new->devnum
, NULL
, "sync_action");
468 new->info
.state_fd
= sysfs_open(new->devnum
, NULL
, "array_state");
469 new->resync_start_fd
= sysfs_open(new->devnum
, NULL
, "resync_start");
470 new->metadata_fd
= sysfs_open(new->devnum
, NULL
, "metadata_version");
471 get_resync_start(new);
472 dprintf("%s: inst: %d action: %d state: %d\n", __func__
, atoi(inst
),
473 new->action_fd
, new->info
.state_fd
);
477 /* if everything checks out tell the metadata handler we want to
478 * manage this instance
480 if (!aa_ready(new) || container
->ss
->open_new(container
, new, inst
) < 0) {
481 fprintf(stderr
, "mdmon: failed to monitor %s\n",
482 mdstat
->metadata_version
);
483 new->container
= NULL
;
486 replace_array(container
, victim
, new);
488 new->check_degraded
= 1;
489 manage_member(mdstat
, new);
494 void manage(struct mdstat_ent
*mdstat
, struct supertype
*container
)
496 /* We have just read mdstat and need to compare it with
497 * the known active arrays.
498 * Arrays with the wrong metadata are ignored.
501 for ( ; mdstat
; mdstat
= mdstat
->next
) {
502 struct active_array
*a
;
503 if (mdstat
->devnum
== container
->devnum
) {
504 manage_container(mdstat
, container
);
507 if (mdstat
->metadata_version
== NULL
||
508 strncmp(mdstat
->metadata_version
, "external:", 9) != 0 ||
509 !is_subarray(mdstat
->metadata_version
+9) ||
510 strncmp(mdstat
->metadata_version
+10, container
->devname
,
511 strlen(container
->devname
)) != 0 ||
512 mdstat
->metadata_version
[10+strlen(container
->devname
)]
514 /* Not for this array */
516 /* Looks like a member of this container */
517 for (a
= container
->arrays
; a
; a
= a
->next
) {
518 if (mdstat
->devnum
== a
->devnum
) {
520 manage_member(mdstat
, a
);
524 if (a
== NULL
|| !a
->container
)
525 manage_new(mdstat
, container
, a
);
529 static void handle_message(struct supertype
*container
, struct metadata_update
*msg
)
531 /* queue this metadata update through to the monitor */
533 struct metadata_update
*mu
;
536 while (update_queue_pending
|| update_queue
) {
537 check_update_queue(container
);
541 if (msg
->len
== 0) { /* ping_monitor */
544 cnt
= monitor_loop_cnt
;
546 cnt
+= 2; /* wait until next pselect */
548 cnt
+= 3; /* wait for 2 pselects */
551 while (monitor_loop_cnt
- cnt
< 0)
553 } else if (msg
->len
== -1) { /* ping_manager */
554 struct mdstat_ent
*mdstat
= mdstat_read(1, 0);
556 manage(mdstat
, container
);
559 mu
= malloc(sizeof(*mu
));
565 if (container
->ss
->prepare_update
)
566 container
->ss
->prepare_update(container
, mu
);
567 queue_metadata_update(mu
);
571 void read_sock(struct supertype
*container
)
574 struct metadata_update msg
;
577 int tmo
= 3; /* 3 second timeout before hanging up the socket */
579 fd
= accept(container
->sock
, NULL
, NULL
);
583 fl
= fcntl(fd
, F_GETFL
, 0);
585 fcntl(fd
, F_SETFL
, fl
);
590 /* read and validate the message */
591 if (receive_message(fd
, &msg
, tmo
) == 0) {
592 handle_message(container
, &msg
);
593 if (ack(fd
, tmo
) < 0)
598 } while (!terminate
);
604 int manager_ready
= 0;
605 void do_manager(struct supertype
*container
)
607 struct mdstat_ent
*mdstat
;
610 sigprocmask(SIG_UNBLOCK
, NULL
, &set
);
611 sigdelset(&set
, SIGUSR1
);
612 sigdelset(&set
, SIGHUP
);
619 /* Can only 'manage' things if 'monitor' is not making
620 * structural changes to metadata, so need to check
623 if (update_queue
== NULL
) {
624 mdstat
= mdstat_read(1, 0);
626 manage(mdstat
, container
);
628 read_sock(container
);
630 if (socket_hup_requested
) {
631 close(container
->sock
);
632 container
->sock
= make_control_sock(container
->devname
);
633 make_pidfile(container
->devname
, 0);
634 socket_hup_requested
= 0;
641 check_update_queue(container
);
645 if (update_queue
== NULL
)
646 mdstat_wait_fd(container
->sock
, &set
);
648 /* If an update is happening, just wait for signal */
649 pselect(0, NULL
, NULL
, NULL
, NULL
, &set
);