]>
Commit | Line | Data |
---|---|---|
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 | ||
2a0bb19e DW |
79 | static void close_aa(struct active_array *aa) |
80 | { | |
81 | struct mdinfo *d; | |
82 | ||
83 | for (d = aa->info.devs; d; d = d->next) | |
84 | close(d->state_fd); | |
85 | ||
86 | close(aa->action_fd); | |
87 | close(aa->info.state_fd); | |
88 | close(aa->resync_start_fd); | |
89 | close(aa->sync_pos_fd); | |
90 | } | |
91 | ||
549e9569 NB |
92 | static void free_aa(struct active_array *aa) |
93 | { | |
2a0bb19e DW |
94 | /* Note that this doesn't close fds if they are being used |
95 | * by a clone. ->container will be set for a clone | |
549e9569 | 96 | */ |
2a0bb19e DW |
97 | if (!aa->container) |
98 | close_aa(aa); | |
549e9569 NB |
99 | while (aa->info.devs) { |
100 | struct mdinfo *d = aa->info.devs; | |
101 | aa->info.devs = d->next; | |
102 | free(d); | |
103 | } | |
104 | free(aa); | |
105 | } | |
106 | ||
2a0bb19e DW |
107 | static void write_wakeup(struct supertype *c) |
108 | { | |
3e70c845 DW |
109 | static struct md_generic_cmd cmd = { .action = md_action_ping_monitor }; |
110 | int err; | |
111 | ||
112 | active_cmd = &cmd; | |
113 | ||
114 | /* send the monitor thread a pointer to the ping action */ | |
115 | write(c->mgr_pipe[1], &err, 1); | |
116 | read(c->mon_pipe[0], &err, 1); | |
2a0bb19e DW |
117 | } |
118 | ||
549e9569 NB |
119 | static void replace_array(struct supertype *container, |
120 | struct active_array *old, | |
121 | struct active_array *new) | |
122 | { | |
123 | /* To replace an array, we add it to the top of the list | |
124 | * marked with ->replaces to point to the original. | |
125 | * 'monitor' will take the original out of the list | |
126 | * and put it on 'discard_this'. We take it from there | |
127 | * and discard it. | |
128 | */ | |
129 | ||
130 | while (pending_discard) { | |
131 | while (discard_this == NULL) | |
132 | sleep(1); | |
133 | if (discard_this != pending_discard) | |
134 | abort(); | |
135 | discard_this->next = NULL; | |
136 | free_aa(discard_this); | |
137 | discard_this = NULL; | |
138 | pending_discard = NULL; | |
139 | } | |
140 | pending_discard = old; | |
141 | new->replaces = old; | |
142 | new->next = container->arrays; | |
143 | container->arrays = new; | |
2a0bb19e | 144 | write_wakeup(container); |
549e9569 NB |
145 | } |
146 | ||
147 | ||
148 | static void manage_container(struct mdstat_ent *mdstat, | |
149 | struct supertype *container) | |
150 | { | |
151 | /* The only thing of interest here is if a new device | |
152 | * has been added to the container. We add it to the | |
153 | * array ignoring any metadata on it. | |
154 | * FIXME should we look for compatible metadata and take hints | |
155 | * about spare assignment.... probably not. | |
156 | * | |
157 | */ | |
158 | if (mdstat->devcnt != container->devcnt) { | |
159 | /* read /sys/block/NAME/md/dev-??/block/dev to find out | |
160 | * what is there, and compare with container->info.devs | |
161 | * To see what is removed and what is added. | |
162 | * These need to be remove from, or added to, the array | |
163 | */ | |
164 | // FIXME | |
165 | container->devcnt = mdstat->devcnt; | |
166 | } | |
167 | } | |
168 | ||
169 | static void manage_member(struct mdstat_ent *mdstat, | |
170 | struct active_array *a) | |
171 | { | |
172 | /* Compare mdstat info with known state of member array. | |
173 | * We do not need to look for device state changes here, that | |
174 | * is dealt with by the monitor. | |
175 | * | |
176 | * We just look for changes which suggest that a reshape is | |
177 | * being requested. | |
178 | * Unfortunately decreases in raid_disks don't show up in | |
179 | * mdstat until the reshape completes FIXME. | |
180 | */ | |
181 | // FIXME | |
182 | a->info.array.raid_disks = mdstat->raid_disks; | |
183 | a->info.array.chunk_size = mdstat->chunk_size; | |
184 | // MORE | |
185 | ||
186 | } | |
187 | ||
549e9569 | 188 | static void manage_new(struct mdstat_ent *mdstat, |
2a0bb19e DW |
189 | struct supertype *container, |
190 | struct active_array *victim) | |
549e9569 NB |
191 | { |
192 | /* A new array has appeared in this container. | |
193 | * Hopefully it is already recorded in the metadata. | |
194 | * Check, then create the new array to report it to | |
195 | * the monitor. | |
196 | */ | |
197 | ||
198 | struct active_array *new; | |
199 | struct mdinfo *mdi, *di; | |
200 | char *n; | |
201 | int inst; | |
202 | int i; | |
203 | ||
204 | new = malloc(sizeof(*new)); | |
205 | ||
206 | new->devnum = mdstat->devnum; | |
207 | ||
208 | new->prev_state = new->curr_state = new->next_state = inactive; | |
209 | new->prev_action= new->curr_action= new->next_action= idle; | |
210 | ||
211 | new->container = container; | |
212 | ||
213 | n = &mdstat->metadata_version[10+strlen(container->devname)+1]; | |
214 | inst = atoi(n); | |
215 | if (inst < 0) | |
216 | abort();//FIXME | |
217 | ||
218 | mdi = sysfs_read(-1, new->devnum, | |
219 | GET_LEVEL|GET_CHUNK|GET_DISKS| | |
220 | GET_DEVS|GET_OFFSET|GET_SIZE|GET_STATE); | |
221 | if (!mdi) { | |
222 | /* Eeek. Cannot monitor this array. | |
223 | * Mark it to be ignored by setting container to NULL | |
224 | */ | |
225 | new->container = NULL; | |
2a0bb19e | 226 | replace_array(container, victim, new); |
549e9569 NB |
227 | return; |
228 | } | |
229 | ||
230 | new->info.array = mdi->array; | |
231 | ||
232 | for (i = 0; i < new->info.array.raid_disks; i++) { | |
233 | struct mdinfo *newd = malloc(sizeof(*newd)); | |
234 | ||
235 | for (di = mdi->devs; di; di = di->next) | |
236 | if (i == di->disk.raid_disk) | |
237 | break; | |
238 | ||
239 | if (di) { | |
240 | memcpy(newd, di, sizeof(*newd)); | |
241 | ||
242 | sprintf(newd->sys_name, "rd%d", i); | |
243 | ||
244 | newd->state_fd = sysfs_open(new->devnum, | |
245 | newd->sys_name, | |
246 | "state"); | |
247 | ||
248 | newd->prev_state = read_dev_state(newd->state_fd); | |
249 | newd->curr_state = newd->curr_state; | |
250 | } else { | |
251 | newd->state_fd = -1; | |
252 | } | |
253 | newd->next = new->info.devs; | |
254 | new->info.devs = newd; | |
255 | } | |
256 | new->action_fd = sysfs_open(new->devnum, NULL, "sync_action"); | |
257 | new->info.state_fd = sysfs_open(new->devnum, NULL, "array_state"); | |
c052ba30 | 258 | new->resync_start_fd = sysfs_open(new->devnum, NULL, "resync_start"); |
549e9569 NB |
259 | new->sync_pos_fd = sysfs_open(new->devnum, NULL, "sync_completed"); |
260 | new->sync_pos = 0; | |
261 | ||
4fa5aef9 | 262 | sysfs_free(mdi); |
549e9569 NB |
263 | // finds and compares. |
264 | if (container->ss->open_new(container, new, inst) < 0) { | |
265 | // FIXME close all those files | |
266 | new->container = NULL; | |
2a0bb19e | 267 | replace_array(container, victim, new); |
549e9569 NB |
268 | return; |
269 | } | |
2a0bb19e | 270 | replace_array(container, victim, new); |
549e9569 NB |
271 | return; |
272 | } | |
273 | ||
274 | void manage(struct mdstat_ent *mdstat, struct active_array *aa, | |
275 | struct supertype *container) | |
276 | { | |
277 | /* We have just read mdstat and need to compare it with | |
278 | * the known active arrays. | |
279 | * Arrays with the wrong metadata are ignored. | |
280 | */ | |
281 | ||
282 | for ( ; mdstat ; mdstat = mdstat->next) { | |
283 | struct active_array *a; | |
284 | if (mdstat->devnum == container->devnum) { | |
285 | manage_container(mdstat, container); | |
286 | continue; | |
287 | } | |
288 | if (mdstat->metadata_version == NULL || | |
289 | strncmp(mdstat->metadata_version, "external:/", 10) != 0 || | |
290 | strncmp(mdstat->metadata_version+10, container->devname, | |
291 | strlen(container->devname)) != 0 || | |
292 | mdstat->metadata_version[10+strlen(container->devname)] | |
293 | != '/') | |
294 | /* Not for this array */ | |
295 | continue; | |
296 | /* Looks like a member of this container */ | |
297 | for (a = aa; a; a = a->next) { | |
298 | if (mdstat->devnum == a->devnum) { | |
299 | if (a->container) | |
300 | manage_member(mdstat, a); | |
301 | break; | |
302 | } | |
303 | } | |
2a0bb19e DW |
304 | if (a == NULL || !a->container) |
305 | manage_new(mdstat, container, a); | |
549e9569 NB |
306 | } |
307 | } | |
308 | ||
3e70c845 DW |
309 | static int handle_message(struct supertype *container, struct md_message *msg) |
310 | { | |
311 | int err; | |
312 | struct md_generic_cmd *cmd = msg->buf; | |
313 | ||
314 | if (!cmd) | |
315 | return 0; | |
316 | ||
317 | switch (cmd->action) { | |
318 | case md_action_remove_device: | |
319 | ||
320 | /* forward to the monitor */ | |
321 | active_cmd = cmd; | |
322 | write(container->mgr_pipe[1], &err, 1); | |
323 | read(container->mon_pipe[0], &err, 1); | |
324 | return err; | |
325 | ||
326 | default: | |
327 | return -1; | |
328 | } | |
329 | } | |
330 | ||
331 | void read_sock(struct supertype *container) | |
549e9569 NB |
332 | { |
333 | int fd; | |
b109d928 DW |
334 | struct md_message msg; |
335 | int terminate = 0; | |
336 | long fl; | |
337 | int tmo = 3; /* 3 second timeout before hanging up the socket */ | |
549e9569 | 338 | |
3e70c845 | 339 | fd = accept(container->sock, NULL, NULL); |
549e9569 NB |
340 | if (fd < 0) |
341 | return; | |
b109d928 DW |
342 | |
343 | fl = fcntl(fd, F_GETFL, 0); | |
344 | fl |= O_NONBLOCK; | |
345 | fcntl(fd, F_SETFL, fl); | |
346 | ||
347 | do { | |
3e70c845 DW |
348 | int err; |
349 | ||
b109d928 DW |
350 | msg.buf = NULL; |
351 | ||
352 | /* read and validate the message */ | |
353 | if (receive_message(fd, &msg, tmo) == 0) { | |
3e70c845 DW |
354 | err = handle_message(container, &msg); |
355 | if (!err) | |
356 | ack(fd, msg.seq, tmo); | |
357 | else | |
358 | nack(fd, err, tmo); | |
b109d928 DW |
359 | } else { |
360 | terminate = 1; | |
361 | nack(fd, -1, tmo); | |
362 | } | |
363 | ||
364 | if (msg.buf) | |
365 | free(msg.buf); | |
366 | } while (!terminate); | |
367 | ||
549e9569 NB |
368 | close(fd); |
369 | } | |
370 | void do_manager(struct supertype *container) | |
371 | { | |
372 | struct mdstat_ent *mdstat; | |
373 | ||
374 | do { | |
375 | mdstat = mdstat_read(1, 0); | |
376 | ||
377 | manage(mdstat, array_list, container); | |
378 | ||
3e70c845 | 379 | read_sock(container); |
549e9569 | 380 | |
4fa5aef9 DW |
381 | free_mdstat(mdstat); |
382 | ||
549e9569 NB |
383 | mdstat_wait_fd(container->sock); |
384 | } while(1); | |
385 | } |