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