]> git.ipfire.org Git - thirdparty/mdadm.git/blame - managemon.c
when failures happen they should be propagated to all member arrays
[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>
78
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);
90 close(aa->sync_pos_fd);
91}
92
549e9569
NB
93static void free_aa(struct active_array *aa)
94{
2a0bb19e
DW
95 /* Note that this doesn't close fds if they are being used
96 * by a clone. ->container will be set for a clone
549e9569 97 */
2a0bb19e
DW
98 if (!aa->container)
99 close_aa(aa);
549e9569
NB
100 while (aa->info.devs) {
101 struct mdinfo *d = aa->info.devs;
102 aa->info.devs = d->next;
103 free(d);
104 }
105 free(aa);
106}
107
2a0bb19e
DW
108static void write_wakeup(struct supertype *c)
109{
110 write(c->pipe[1], "PING", 4);
111}
112
549e9569
NB
113static void replace_array(struct supertype *container,
114 struct active_array *old,
115 struct active_array *new)
116{
117 /* To replace an array, we add it to the top of the list
118 * marked with ->replaces to point to the original.
119 * 'monitor' will take the original out of the list
120 * and put it on 'discard_this'. We take it from there
121 * and discard it.
122 */
123
124 while (pending_discard) {
125 while (discard_this == NULL)
126 sleep(1);
127 if (discard_this != pending_discard)
128 abort();
129 discard_this->next = NULL;
130 free_aa(discard_this);
131 discard_this = NULL;
132 pending_discard = NULL;
133 }
134 pending_discard = old;
135 new->replaces = old;
136 new->next = container->arrays;
137 container->arrays = new;
2a0bb19e 138 write_wakeup(container);
549e9569
NB
139}
140
141
142static void manage_container(struct mdstat_ent *mdstat,
143 struct supertype *container)
144{
145 /* The only thing of interest here is if a new device
146 * has been added to the container. We add it to the
147 * array ignoring any metadata on it.
148 * FIXME should we look for compatible metadata and take hints
149 * about spare assignment.... probably not.
150 *
151 */
152 if (mdstat->devcnt != container->devcnt) {
153 /* read /sys/block/NAME/md/dev-??/block/dev to find out
154 * what is there, and compare with container->info.devs
155 * To see what is removed and what is added.
156 * These need to be remove from, or added to, the array
157 */
158 // FIXME
159 container->devcnt = mdstat->devcnt;
160 }
161}
162
163static void manage_member(struct mdstat_ent *mdstat,
164 struct active_array *a)
165{
166 /* Compare mdstat info with known state of member array.
167 * We do not need to look for device state changes here, that
168 * is dealt with by the monitor.
169 *
170 * We just look for changes which suggest that a reshape is
171 * being requested.
172 * Unfortunately decreases in raid_disks don't show up in
173 * mdstat until the reshape completes FIXME.
174 */
175 // FIXME
176 a->info.array.raid_disks = mdstat->raid_disks;
177 a->info.array.chunk_size = mdstat->chunk_size;
178 // MORE
179
180}
181
549e9569 182static void manage_new(struct mdstat_ent *mdstat,
2a0bb19e
DW
183 struct supertype *container,
184 struct active_array *victim)
549e9569
NB
185{
186 /* A new array has appeared in this container.
187 * Hopefully it is already recorded in the metadata.
188 * Check, then create the new array to report it to
189 * the monitor.
190 */
191
192 struct active_array *new;
193 struct mdinfo *mdi, *di;
194 char *n;
195 int inst;
196 int i;
197
198 new = malloc(sizeof(*new));
199
200 new->devnum = mdstat->devnum;
201
202 new->prev_state = new->curr_state = new->next_state = inactive;
203 new->prev_action= new->curr_action= new->next_action= idle;
204
205 new->container = container;
206
207 n = &mdstat->metadata_version[10+strlen(container->devname)+1];
208 inst = atoi(n);
209 if (inst < 0)
210 abort();//FIXME
211
212 mdi = sysfs_read(-1, new->devnum,
213 GET_LEVEL|GET_CHUNK|GET_DISKS|
214 GET_DEVS|GET_OFFSET|GET_SIZE|GET_STATE);
215 if (!mdi) {
216 /* Eeek. Cannot monitor this array.
217 * Mark it to be ignored by setting container to NULL
218 */
219 new->container = NULL;
2a0bb19e 220 replace_array(container, victim, new);
549e9569
NB
221 return;
222 }
223
224 new->info.array = mdi->array;
225
226 for (i = 0; i < new->info.array.raid_disks; i++) {
227 struct mdinfo *newd = malloc(sizeof(*newd));
228
229 for (di = mdi->devs; di; di = di->next)
230 if (i == di->disk.raid_disk)
231 break;
232
233 if (di) {
234 memcpy(newd, di, sizeof(*newd));
235
236 sprintf(newd->sys_name, "rd%d", i);
237
238 newd->state_fd = sysfs_open(new->devnum,
239 newd->sys_name,
240 "state");
241
242 newd->prev_state = read_dev_state(newd->state_fd);
243 newd->curr_state = newd->curr_state;
244 } else {
245 newd->state_fd = -1;
246 }
247 newd->next = new->info.devs;
248 new->info.devs = newd;
249 }
250 new->action_fd = sysfs_open(new->devnum, NULL, "sync_action");
251 new->info.state_fd = sysfs_open(new->devnum, NULL, "array_state");
c052ba30 252 new->resync_start_fd = sysfs_open(new->devnum, NULL, "resync_start");
549e9569
NB
253 new->sync_pos_fd = sysfs_open(new->devnum, NULL, "sync_completed");
254 new->sync_pos = 0;
255
256 // finds and compares.
257 if (container->ss->open_new(container, new, inst) < 0) {
258 // FIXME close all those files
259 new->container = NULL;
2a0bb19e 260 replace_array(container, victim, new);
549e9569
NB
261 return;
262 }
2a0bb19e 263 replace_array(container, victim, new);
549e9569
NB
264 return;
265}
266
267void manage(struct mdstat_ent *mdstat, struct active_array *aa,
268 struct supertype *container)
269{
270 /* We have just read mdstat and need to compare it with
271 * the known active arrays.
272 * Arrays with the wrong metadata are ignored.
273 */
274
275 for ( ; mdstat ; mdstat = mdstat->next) {
276 struct active_array *a;
277 if (mdstat->devnum == container->devnum) {
278 manage_container(mdstat, container);
279 continue;
280 }
281 if (mdstat->metadata_version == NULL ||
282 strncmp(mdstat->metadata_version, "external:/", 10) != 0 ||
283 strncmp(mdstat->metadata_version+10, container->devname,
284 strlen(container->devname)) != 0 ||
285 mdstat->metadata_version[10+strlen(container->devname)]
286 != '/')
287 /* Not for this array */
288 continue;
289 /* Looks like a member of this container */
290 for (a = aa; a; a = a->next) {
291 if (mdstat->devnum == a->devnum) {
292 if (a->container)
293 manage_member(mdstat, a);
294 break;
295 }
296 }
2a0bb19e
DW
297 if (a == NULL || !a->container)
298 manage_new(mdstat, container, a);
549e9569
NB
299 }
300}
301
302void read_sock(int pfd)
303{
304 int fd;
305
306 // FIXME set non-blocking
307 fd = accept(pfd, NULL, NULL);
308 if (fd < 0)
309 return;
310 // FIXME do something useful
311 close(fd);
312}
313void do_manager(struct supertype *container)
314{
315 struct mdstat_ent *mdstat;
316
317 do {
318 mdstat = mdstat_read(1, 0);
319
320 manage(mdstat, array_list, container);
321
322 read_sock(container->sock);
323
324 mdstat_wait_fd(container->sock);
325 } while(1);
326}