]>
git.ipfire.org Git - thirdparty/mdadm.git/blob - managemon.c
14e7184b0359801dd5fd9b25b3c674011635d177
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
77 #include <sys/socket.h>
80 static void free_aa(struct active_array
*aa
)
82 /* Note that this doesn't close fds, as they may be in used
83 * by a clone. Use close_aa for that.
85 while (aa
->info
.devs
) {
86 struct mdinfo
*d
= aa
->info
.devs
;
87 aa
->info
.devs
= d
->next
;
93 static void replace_array(struct supertype
*container
,
94 struct active_array
*old
,
95 struct active_array
*new)
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
104 while (pending_discard
) {
105 while (discard_this
== NULL
)
107 if (discard_this
!= pending_discard
)
109 discard_this
->next
= NULL
;
110 free_aa(discard_this
);
112 pending_discard
= NULL
;
114 pending_discard
= old
;
116 new->next
= container
->arrays
;
117 container
->arrays
= new;
121 static void manage_container(struct mdstat_ent
*mdstat
,
122 struct supertype
*container
)
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.
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
138 container
->devcnt
= mdstat
->devcnt
;
142 static void manage_member(struct mdstat_ent
*mdstat
,
143 struct active_array
*a
)
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.
149 * We just look for changes which suggest that a reshape is
151 * Unfortunately decreases in raid_disks don't show up in
152 * mdstat until the reshape completes FIXME.
155 a
->info
.array
.raid_disks
= mdstat
->raid_disks
;
156 a
->info
.array
.chunk_size
= mdstat
->chunk_size
;
161 static void write_wakeup(struct supertype
*c
)
163 write(c
->pipe
[1], "PING", 4);
166 static void manage_new(struct mdstat_ent
*mdstat
,
167 struct supertype
*container
)
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
175 struct active_array
*new;
176 struct mdinfo
*mdi
, *di
;
181 new = malloc(sizeof(*new));
183 new->devnum
= mdstat
->devnum
;
185 new->prev_state
= new->curr_state
= new->next_state
= inactive
;
186 new->prev_action
= new->curr_action
= new->next_action
= idle
;
188 new->container
= container
;
190 n
= &mdstat
->metadata_version
[10+strlen(container
->devname
)+1];
195 mdi
= sysfs_read(-1, new->devnum
,
196 GET_LEVEL
|GET_CHUNK
|GET_DISKS
|
197 GET_DEVS
|GET_OFFSET
|GET_SIZE
|GET_STATE
);
199 /* Eeek. Cannot monitor this array.
200 * Mark it to be ignored by setting container to NULL
202 new->container
= NULL
;
203 replace_array(container
, NULL
, new);
207 new->info
.array
= mdi
->array
;
209 for (i
= 0; i
< new->info
.array
.raid_disks
; i
++) {
210 struct mdinfo
*newd
= malloc(sizeof(*newd
));
212 for (di
= mdi
->devs
; di
; di
= di
->next
)
213 if (i
== di
->disk
.raid_disk
)
217 memcpy(newd
, di
, sizeof(*newd
));
219 sprintf(newd
->sys_name
, "rd%d", i
);
221 newd
->state_fd
= sysfs_open(new->devnum
,
225 newd
->prev_state
= read_dev_state(newd
->state_fd
);
226 newd
->curr_state
= newd
->curr_state
;
230 newd
->next
= new->info
.devs
;
231 new->info
.devs
= newd
;
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");
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);
246 replace_array(container
, NULL
, new);
247 write_wakeup(container
);
251 void manage(struct mdstat_ent
*mdstat
, struct active_array
*aa
,
252 struct supertype
*container
)
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.
259 for ( ; mdstat
; mdstat
= mdstat
->next
) {
260 struct active_array
*a
;
261 if (mdstat
->devnum
== container
->devnum
) {
262 manage_container(mdstat
, container
);
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
)]
271 /* Not for this array */
273 /* Looks like a member of this container */
274 for (a
= aa
; a
; a
= a
->next
) {
275 if (mdstat
->devnum
== a
->devnum
) {
277 manage_member(mdstat
, a
);
282 manage_new(mdstat
, container
);
286 void read_sock(int pfd
)
290 // FIXME set non-blocking
291 fd
= accept(pfd
, NULL
, NULL
);
294 // FIXME do something useful
297 void do_manager(struct supertype
*container
)
299 struct mdstat_ent
*mdstat
;
302 mdstat
= mdstat_read(1, 0);
304 manage(mdstat
, array_list
, container
);
306 read_sock(container
->sock
);
308 mdstat_wait_fd(container
->sock
);