]> git.ipfire.org Git - thirdparty/mdadm.git/blame - managemon.c
start fleshing out socket code, ping monitor to see if it is alive
[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"
b109d928 77#include "msg.h"
549e9569
NB
78#include <sys/socket.h>
79
80
2a0bb19e
DW
81static void close_aa(struct active_array *aa)
82{
83 struct mdinfo *d;
84
85 for (d = aa->info.devs; d; d = d->next)
86 close(d->state_fd);
87
88 close(aa->action_fd);
89 close(aa->info.state_fd);
90 close(aa->resync_start_fd);
91 close(aa->sync_pos_fd);
92}
93
549e9569
NB
94static void free_aa(struct active_array *aa)
95{
2a0bb19e
DW
96 /* Note that this doesn't close fds if they are being used
97 * by a clone. ->container will be set for a clone
549e9569 98 */
2a0bb19e
DW
99 if (!aa->container)
100 close_aa(aa);
549e9569
NB
101 while (aa->info.devs) {
102 struct mdinfo *d = aa->info.devs;
103 aa->info.devs = d->next;
104 free(d);
105 }
106 free(aa);
107}
108
2a0bb19e
DW
109static void write_wakeup(struct supertype *c)
110{
111 write(c->pipe[1], "PING", 4);
112}
113
549e9569
NB
114static void replace_array(struct supertype *container,
115 struct active_array *old,
116 struct active_array *new)
117{
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
122 * and discard it.
123 */
124
125 while (pending_discard) {
126 while (discard_this == NULL)
127 sleep(1);
128 if (discard_this != pending_discard)
129 abort();
130 discard_this->next = NULL;
131 free_aa(discard_this);
132 discard_this = NULL;
133 pending_discard = NULL;
134 }
135 pending_discard = old;
136 new->replaces = old;
137 new->next = container->arrays;
138 container->arrays = new;
2a0bb19e 139 write_wakeup(container);
549e9569
NB
140}
141
142
143static void manage_container(struct mdstat_ent *mdstat,
144 struct supertype *container)
145{
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.
151 *
152 */
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
158 */
159 // FIXME
160 container->devcnt = mdstat->devcnt;
161 }
162}
163
164static void manage_member(struct mdstat_ent *mdstat,
165 struct active_array *a)
166{
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.
170 *
171 * We just look for changes which suggest that a reshape is
172 * being requested.
173 * Unfortunately decreases in raid_disks don't show up in
174 * mdstat until the reshape completes FIXME.
175 */
176 // FIXME
177 a->info.array.raid_disks = mdstat->raid_disks;
178 a->info.array.chunk_size = mdstat->chunk_size;
179 // MORE
180
181}
182
549e9569 183static void manage_new(struct mdstat_ent *mdstat,
2a0bb19e
DW
184 struct supertype *container,
185 struct active_array *victim)
549e9569
NB
186{
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
190 * the monitor.
191 */
192
193 struct active_array *new;
194 struct mdinfo *mdi, *di;
195 char *n;
196 int inst;
197 int i;
198
199 new = malloc(sizeof(*new));
200
201 new->devnum = mdstat->devnum;
202
203 new->prev_state = new->curr_state = new->next_state = inactive;
204 new->prev_action= new->curr_action= new->next_action= idle;
205
206 new->container = container;
207
208 n = &mdstat->metadata_version[10+strlen(container->devname)+1];
209 inst = atoi(n);
210 if (inst < 0)
211 abort();//FIXME
212
213 mdi = sysfs_read(-1, new->devnum,
214 GET_LEVEL|GET_CHUNK|GET_DISKS|
215 GET_DEVS|GET_OFFSET|GET_SIZE|GET_STATE);
216 if (!mdi) {
217 /* Eeek. Cannot monitor this array.
218 * Mark it to be ignored by setting container to NULL
219 */
220 new->container = NULL;
2a0bb19e 221 replace_array(container, victim, new);
549e9569
NB
222 return;
223 }
224
225 new->info.array = mdi->array;
226
227 for (i = 0; i < new->info.array.raid_disks; i++) {
228 struct mdinfo *newd = malloc(sizeof(*newd));
229
230 for (di = mdi->devs; di; di = di->next)
231 if (i == di->disk.raid_disk)
232 break;
233
234 if (di) {
235 memcpy(newd, di, sizeof(*newd));
236
237 sprintf(newd->sys_name, "rd%d", i);
238
239 newd->state_fd = sysfs_open(new->devnum,
240 newd->sys_name,
241 "state");
242
243 newd->prev_state = read_dev_state(newd->state_fd);
244 newd->curr_state = newd->curr_state;
245 } else {
246 newd->state_fd = -1;
247 }
248 newd->next = new->info.devs;
249 new->info.devs = newd;
250 }
251 new->action_fd = sysfs_open(new->devnum, NULL, "sync_action");
252 new->info.state_fd = sysfs_open(new->devnum, NULL, "array_state");
c052ba30 253 new->resync_start_fd = sysfs_open(new->devnum, NULL, "resync_start");
549e9569
NB
254 new->sync_pos_fd = sysfs_open(new->devnum, NULL, "sync_completed");
255 new->sync_pos = 0;
256
257 // finds and compares.
258 if (container->ss->open_new(container, new, inst) < 0) {
259 // FIXME close all those files
260 new->container = NULL;
2a0bb19e 261 replace_array(container, victim, new);
549e9569
NB
262 return;
263 }
2a0bb19e 264 replace_array(container, victim, new);
549e9569
NB
265 return;
266}
267
268void manage(struct mdstat_ent *mdstat, struct active_array *aa,
269 struct supertype *container)
270{
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.
274 */
275
276 for ( ; mdstat ; mdstat = mdstat->next) {
277 struct active_array *a;
278 if (mdstat->devnum == container->devnum) {
279 manage_container(mdstat, container);
280 continue;
281 }
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)]
287 != '/')
288 /* Not for this array */
289 continue;
290 /* Looks like a member of this container */
291 for (a = aa; a; a = a->next) {
292 if (mdstat->devnum == a->devnum) {
293 if (a->container)
294 manage_member(mdstat, a);
295 break;
296 }
297 }
2a0bb19e
DW
298 if (a == NULL || !a->container)
299 manage_new(mdstat, container, a);
549e9569
NB
300 }
301}
302
303void read_sock(int pfd)
304{
305 int fd;
b109d928
DW
306 struct md_message msg;
307 int terminate = 0;
308 long fl;
309 int tmo = 3; /* 3 second timeout before hanging up the socket */
549e9569 310
549e9569
NB
311 fd = accept(pfd, NULL, NULL);
312 if (fd < 0)
313 return;
b109d928
DW
314
315 fl = fcntl(fd, F_GETFL, 0);
316 fl |= O_NONBLOCK;
317 fcntl(fd, F_SETFL, fl);
318
319 do {
320 msg.buf = NULL;
321
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);
326 } else {
327 terminate = 1;
328 nack(fd, -1, tmo);
329 }
330
331 if (msg.buf)
332 free(msg.buf);
333 } while (!terminate);
334
549e9569
NB
335 close(fd);
336}
337void do_manager(struct supertype *container)
338{
339 struct mdstat_ent *mdstat;
340
341 do {
342 mdstat = mdstat_read(1, 0);
343
344 manage(mdstat, array_list, container);
345
346 read_sock(container->sock);
347
348 mdstat_wait_fd(container->sock);
349 } while(1);
350}