]>
git.ipfire.org Git - thirdparty/mdadm.git/blob - managemon.c
f8123d0efd974210889ca6bcac8f2be890638e42
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 * - assist with activating spares by opening relevant sysfs file.
31 * - Pass on metadata updates from external programs such as
32 * mdadm creating a new array.
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.
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.
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.
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.
63 * - Watch for spares to be added to the container, and write updated
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
78 #include <sys/socket.h>
81 static void close_aa(struct active_array
*aa
)
85 for (d
= aa
->info
.devs
; d
; d
= d
->next
)
89 close(aa
->info
.state_fd
);
90 close(aa
->resync_start_fd
);
91 close(aa
->sync_pos_fd
);
94 static void free_aa(struct active_array
*aa
)
96 /* Note that this doesn't close fds if they are being used
97 * by a clone. ->container will be set for a clone
101 while (aa
->info
.devs
) {
102 struct mdinfo
*d
= aa
->info
.devs
;
103 aa
->info
.devs
= d
->next
;
109 static void write_wakeup(struct supertype
*c
)
111 write(c
->pipe
[1], "PING", 4);
114 static void replace_array(struct supertype
*container
,
115 struct active_array
*old
,
116 struct active_array
*new)
118 /* To replace an array, we add it to the top of the list
119 * marked with ->replaces to point to the original.
120 * 'monitor' will take the original out of the list
121 * and put it on 'discard_this'. We take it from there
125 while (pending_discard
) {
126 while (discard_this
== NULL
)
128 if (discard_this
!= pending_discard
)
130 discard_this
->next
= NULL
;
131 free_aa(discard_this
);
133 pending_discard
= NULL
;
135 pending_discard
= old
;
137 new->next
= container
->arrays
;
138 container
->arrays
= new;
139 write_wakeup(container
);
143 static void manage_container(struct mdstat_ent
*mdstat
,
144 struct supertype
*container
)
146 /* The only thing of interest here is if a new device
147 * has been added to the container. We add it to the
148 * array ignoring any metadata on it.
149 * FIXME should we look for compatible metadata and take hints
150 * about spare assignment.... probably not.
153 if (mdstat
->devcnt
!= container
->devcnt
) {
154 /* read /sys/block/NAME/md/dev-??/block/dev to find out
155 * what is there, and compare with container->info.devs
156 * To see what is removed and what is added.
157 * These need to be remove from, or added to, the array
160 container
->devcnt
= mdstat
->devcnt
;
164 static void manage_member(struct mdstat_ent
*mdstat
,
165 struct active_array
*a
)
167 /* Compare mdstat info with known state of member array.
168 * We do not need to look for device state changes here, that
169 * is dealt with by the monitor.
171 * We just look for changes which suggest that a reshape is
173 * Unfortunately decreases in raid_disks don't show up in
174 * mdstat until the reshape completes FIXME.
177 a
->info
.array
.raid_disks
= mdstat
->raid_disks
;
178 a
->info
.array
.chunk_size
= mdstat
->chunk_size
;
183 static void manage_new(struct mdstat_ent
*mdstat
,
184 struct supertype
*container
,
185 struct active_array
*victim
)
187 /* A new array has appeared in this container.
188 * Hopefully it is already recorded in the metadata.
189 * Check, then create the new array to report it to
193 struct active_array
*new;
194 struct mdinfo
*mdi
, *di
;
199 new = malloc(sizeof(*new));
201 new->devnum
= mdstat
->devnum
;
203 new->prev_state
= new->curr_state
= new->next_state
= inactive
;
204 new->prev_action
= new->curr_action
= new->next_action
= idle
;
206 new->container
= container
;
208 n
= &mdstat
->metadata_version
[10+strlen(container
->devname
)+1];
213 mdi
= sysfs_read(-1, new->devnum
,
214 GET_LEVEL
|GET_CHUNK
|GET_DISKS
|
215 GET_DEVS
|GET_OFFSET
|GET_SIZE
|GET_STATE
);
217 /* Eeek. Cannot monitor this array.
218 * Mark it to be ignored by setting container to NULL
220 new->container
= NULL
;
221 replace_array(container
, victim
, new);
225 new->info
.array
= mdi
->array
;
227 for (i
= 0; i
< new->info
.array
.raid_disks
; i
++) {
228 struct mdinfo
*newd
= malloc(sizeof(*newd
));
230 for (di
= mdi
->devs
; di
; di
= di
->next
)
231 if (i
== di
->disk
.raid_disk
)
235 memcpy(newd
, di
, sizeof(*newd
));
237 sprintf(newd
->sys_name
, "rd%d", i
);
239 newd
->state_fd
= sysfs_open(new->devnum
,
243 newd
->prev_state
= read_dev_state(newd
->state_fd
);
244 newd
->curr_state
= newd
->curr_state
;
248 newd
->next
= new->info
.devs
;
249 new->info
.devs
= newd
;
251 new->action_fd
= sysfs_open(new->devnum
, NULL
, "sync_action");
252 new->info
.state_fd
= sysfs_open(new->devnum
, NULL
, "array_state");
253 new->resync_start_fd
= sysfs_open(new->devnum
, NULL
, "resync_start");
254 new->sync_pos_fd
= sysfs_open(new->devnum
, NULL
, "sync_completed");
257 // finds and compares.
258 if (container
->ss
->open_new(container
, new, inst
) < 0) {
259 // FIXME close all those files
260 new->container
= NULL
;
261 replace_array(container
, victim
, new);
264 replace_array(container
, victim
, new);
268 void manage(struct mdstat_ent
*mdstat
, struct active_array
*aa
,
269 struct supertype
*container
)
271 /* We have just read mdstat and need to compare it with
272 * the known active arrays.
273 * Arrays with the wrong metadata are ignored.
276 for ( ; mdstat
; mdstat
= mdstat
->next
) {
277 struct active_array
*a
;
278 if (mdstat
->devnum
== container
->devnum
) {
279 manage_container(mdstat
, container
);
282 if (mdstat
->metadata_version
== NULL
||
283 strncmp(mdstat
->metadata_version
, "external:/", 10) != 0 ||
284 strncmp(mdstat
->metadata_version
+10, container
->devname
,
285 strlen(container
->devname
)) != 0 ||
286 mdstat
->metadata_version
[10+strlen(container
->devname
)]
288 /* Not for this array */
290 /* Looks like a member of this container */
291 for (a
= aa
; a
; a
= a
->next
) {
292 if (mdstat
->devnum
== a
->devnum
) {
294 manage_member(mdstat
, a
);
298 if (a
== NULL
|| !a
->container
)
299 manage_new(mdstat
, container
, a
);
303 void read_sock(int pfd
)
306 struct md_message msg
;
309 int tmo
= 3; /* 3 second timeout before hanging up the socket */
311 fd
= accept(pfd
, NULL
, NULL
);
315 fl
= fcntl(fd
, F_GETFL
, 0);
317 fcntl(fd
, F_SETFL
, fl
);
322 /* read and validate the message */
323 if (receive_message(fd
, &msg
, tmo
) == 0) {
324 // FIXME: handle message contents
325 ack(fd
, msg
.seq
, tmo
);
333 } while (!terminate
);
337 void do_manager(struct supertype
*container
)
339 struct mdstat_ent
*mdstat
;
342 mdstat
= mdstat_read(1, 0);
344 manage(mdstat
, array_list
, container
);
346 read_sock(container
->sock
);
348 mdstat_wait_fd(container
->sock
);