]> git.ipfire.org Git - thirdparty/mdadm.git/blob - managemon.c
mdmon: remove devices from container
[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 * - Deal with degraded array
30 * We only do this when first noticing the array is degraded.
31 * This can be when we first see the array, when sync completes or
32 * when recovery completes.
33 *
34 * Check if number of failed devices suggests recovery is needed, and
35 * skip if not.
36 * Ask metadata to allocate a spare device
37 * Add device as not in_sync and give a role
38 * Update metadata.
39 * Open sysfs files and pass to monitor.
40 * Make sure that monitor Starts recovery....
41 *
42 * - Pass on metadata updates from external programs such as
43 * mdadm creating a new array.
44 *
45 * This is most-messy.
46 * It might involve adding a new array or changing the status of
47 * a spare, or any reconfig that the kernel doesn't get involved in.
48 *
49 * The required updates are received via a named pipe. There will
50 * be one named pipe for each container. Each message contains a
51 * sync marker: 0x5a5aa5a5, A byte count, and the message. This is
52 * passed to the metadata handler which will interpret and process it.
53 * For 'DDF' messages are internal data blocks with the leading
54 * 'magic number' signifying what sort of data it is.
55 *
56 */
57
58 /*
59 * We select on /proc/mdstat and the named pipe.
60 * We create new arrays or updated version of arrays and slip
61 * them into the head of the list, then signal 'monitor' via a pipe write.
62 * 'monitor' will notice and place the old array on a return list.
63 * Metadata updates are placed on a queue just like they arrive
64 * from the named pipe.
65 *
66 * When new arrays are found based on correct metadata string, we
67 * need to identify them with an entry in the metadata. Maybe we require
68 * the metadata to be mdX/NN when NN is the index into an appropriate table.
69 *
70 */
71
72 /*
73 * List of tasks:
74 * - Watch for spares to be added to the container, and write updated
75 * metadata to them.
76 * - Watch for new arrays using this container, confirm they match metadata
77 * and if so, start monitoring them
78 * - Watch for spares being added to monitored arrays. This shouldn't
79 * happen, as we should do all the adding. Just remove them.
80 * - Watch for change in raid-disks, chunk-size, etc. Update metadata and
81 * start a reshape.
82 */
83 #ifndef _GNU_SOURCE
84 #define _GNU_SOURCE
85 #endif
86 #include "mdadm.h"
87 #include "mdmon.h"
88 #include <sys/syscall.h>
89 #include <sys/socket.h>
90 #include <signal.h>
91
92 static void close_aa(struct active_array *aa)
93 {
94 struct mdinfo *d;
95
96 for (d = aa->info.devs; d; d = d->next)
97 close(d->state_fd);
98
99 close(aa->action_fd);
100 close(aa->info.state_fd);
101 close(aa->resync_start_fd);
102 }
103
104 static void free_aa(struct active_array *aa)
105 {
106 /* Note that this doesn't close fds if they are being used
107 * by a clone. ->container will be set for a clone
108 */
109 dprintf("%s: devnum: %d\n", __func__, aa->devnum);
110 if (!aa->container)
111 close_aa(aa);
112 while (aa->info.devs) {
113 struct mdinfo *d = aa->info.devs;
114 aa->info.devs = d->next;
115 free(d);
116 }
117 free(aa);
118 }
119
120 static struct active_array *duplicate_aa(struct active_array *aa)
121 {
122 struct active_array *newa = malloc(sizeof(*newa));
123 struct mdinfo **dp1, **dp2;
124
125 *newa = *aa;
126 newa->next = NULL;
127 newa->replaces = NULL;
128 newa->info.next = NULL;
129
130 dp2 = &newa->info.devs;
131
132 for (dp1 = &aa->info.devs; *dp1; dp1 = &(*dp1)->next) {
133 struct mdinfo *d;
134 if ((*dp1)->state_fd < 0)
135 continue;
136
137 d = malloc(sizeof(*d));
138 *d = **dp1;
139 *dp2 = d;
140 dp2 = & d->next;
141 }
142 *dp2 = NULL;
143
144 return newa;
145 }
146
147 static void wakeup_monitor(void)
148 {
149 /* tgkill(getpid(), mon_tid, SIGUSR1); */
150 int pid = getpid();
151 syscall(SYS_tgkill, pid, mon_tid, SIGUSR1);
152 }
153
154 static void remove_old(void)
155 {
156 if (discard_this) {
157 discard_this->next = NULL;
158 free_aa(discard_this);
159 if (pending_discard == discard_this)
160 pending_discard = NULL;
161 discard_this = NULL;
162 wakeup_monitor();
163 }
164 }
165
166 static void replace_array(struct supertype *container,
167 struct active_array *old,
168 struct active_array *new)
169 {
170 /* To replace an array, we add it to the top of the list
171 * marked with ->replaces to point to the original.
172 * 'monitor' will take the original out of the list
173 * and put it on 'discard_this'. We take it from there
174 * and discard it.
175 */
176 remove_old();
177 while (pending_discard) {
178 while (discard_this == NULL)
179 sleep(1);
180 remove_old();
181 }
182 pending_discard = old;
183 new->replaces = old;
184 new->next = container->arrays;
185 container->arrays = new;
186 wakeup_monitor();
187 }
188
189 struct metadata_update *update_queue = NULL;
190 struct metadata_update *update_queue_handled = NULL;
191 struct metadata_update *update_queue_pending = NULL;
192
193 void check_update_queue(struct supertype *container)
194 {
195 while (update_queue_handled) {
196 struct metadata_update *this = update_queue_handled;
197 update_queue_handled = this->next;
198 free(this->buf);
199 if (this->space)
200 free(this->space);
201 free(this);
202 }
203 if (update_queue == NULL &&
204 update_queue_pending) {
205 update_queue = update_queue_pending;
206 update_queue_pending = NULL;
207 wakeup_monitor();
208 }
209 }
210
211 static void queue_metadata_update(struct metadata_update *mu)
212 {
213 struct metadata_update **qp;
214
215 qp = &update_queue_pending;
216 while (*qp)
217 qp = & ((*qp)->next);
218 *qp = mu;
219 }
220
221 static void manage_container(struct mdstat_ent *mdstat,
222 struct supertype *container)
223 {
224 /* The only thing of interest here is if a new device
225 * has been added to the container. We add it to the
226 * array ignoring any metadata on it.
227 * FIXME should we look for compatible metadata and take hints
228 * about spare assignment.... probably not.
229 */
230 if (mdstat->devcnt != container->devcnt) {
231 struct mdinfo **cdp, *cd, *di, *mdi;
232 int found;
233
234 /* read /sys/block/NAME/md/dev-??/block/dev to find out
235 * what is there, and compare with container->info.devs
236 * To see what is removed and what is added.
237 * These need to be remove from, or added to, the array
238 */
239 mdi = sysfs_read(-1, mdstat->devnum, GET_DEVS);
240 if (!mdi)
241 return;
242
243 /* check for removals */
244 for (cdp = &container->devs; *cdp; ) {
245 found = 0;
246 for (di = mdi->devs; di; di = di->next)
247 if (di->disk.major == (*cdp)->disk.major &&
248 di->disk.minor == (*cdp)->disk.minor) {
249 found = 1;
250 break;
251 }
252 if (!found) {
253 cd = *cdp;
254 *cdp = (*cdp)->next;
255 free(cd);
256 } else
257 cdp = &(*cdp)->next;
258 }
259 sysfs_free(mdi);
260 container->devcnt = mdstat->devcnt;
261 }
262 }
263
264 static void manage_member(struct mdstat_ent *mdstat,
265 struct active_array *a)
266 {
267 /* Compare mdstat info with known state of member array.
268 * We do not need to look for device state changes here, that
269 * is dealt with by the monitor.
270 *
271 * We just look for changes which suggest that a reshape is
272 * being requested.
273 * Unfortunately decreases in raid_disks don't show up in
274 * mdstat until the reshape completes FIXME.
275 *
276 * Actually, we also want to handle degraded arrays here by
277 * trying to find and assign a spare.
278 * We do that whenever the monitor tells us too.
279 */
280 // FIXME
281 a->info.array.raid_disks = mdstat->raid_disks;
282 a->info.array.chunk_size = mdstat->chunk_size;
283 // MORE
284
285 if (a->check_degraded) {
286 struct metadata_update *updates = NULL;
287 struct mdinfo *newdev;
288 struct active_array *newa;
289
290 a->check_degraded = 0;
291
292 /* The array may not be degraded, this is just a good time
293 * to check.
294 */
295 newdev = a->container->ss->activate_spare(a, &updates);
296 if (newdev) {
297 struct mdinfo *d;
298 /* Cool, we can add a device or several. */
299 newa = duplicate_aa(a);
300 /* suspend recovery - maybe not needed */
301
302 /* Add device to array and set offset/size/slot.
303 * and open files for each newdev */
304 for (d = newdev; d ; d = d->next) {
305 struct mdinfo *newd;
306 if (sysfs_add_disk(&newa->info, d) < 0)
307 continue;
308 newd = newa->info.devs;
309 newd->state_fd = sysfs_open(a->devnum,
310 newd->sys_name,
311 "state");
312 newd->prev_state
313 = read_dev_state(newd->state_fd);
314 newd->curr_state = newd->prev_state;
315 }
316 queue_metadata_update(updates);
317 replace_array(a->container, a, newa);
318 sysfs_set_str(&a->info, NULL, "sync_action", "recover");
319 }
320 }
321 }
322
323 static int aa_ready(struct active_array *aa)
324 {
325 struct mdinfo *d;
326 int level = aa->info.array.level;
327
328 for (d = aa->info.devs; d; d = d->next)
329 if (d->state_fd < 0)
330 return 0;
331
332 if (aa->info.state_fd < 0)
333 return 0;
334
335 if (level > 0 && (aa->action_fd < 0 || aa->resync_start_fd < 0))
336 return 0;
337
338 if (!aa->container)
339 return 0;
340
341 return 1;
342 }
343
344 static void manage_new(struct mdstat_ent *mdstat,
345 struct supertype *container,
346 struct active_array *victim)
347 {
348 /* A new array has appeared in this container.
349 * Hopefully it is already recorded in the metadata.
350 * Check, then create the new array to report it to
351 * the monitor.
352 */
353
354 struct active_array *new;
355 struct mdinfo *mdi, *di;
356 char *inst;
357 int i;
358 int failed = 0;
359
360 /* check if array is ready to be monitored */
361 if (!mdstat->active)
362 return;
363
364 mdi = sysfs_read(-1, mdstat->devnum,
365 GET_LEVEL|GET_CHUNK|GET_DISKS|GET_COMPONENT|
366 GET_DEGRADED|GET_DEVS|GET_OFFSET|GET_SIZE|GET_STATE);
367
368 new = malloc(sizeof(*new));
369
370 if (!new || !mdi) {
371 if (mdi)
372 sysfs_free(mdi);
373 if (new)
374 free(new);
375 return;
376 }
377 memset(new, 0, sizeof(*new));
378
379 new->devnum = mdstat->devnum;
380 strcpy(new->info.sys_name, devnum2devname(new->devnum));
381
382 new->prev_state = new->curr_state = new->next_state = inactive;
383 new->prev_action= new->curr_action= new->next_action= idle;
384
385 new->container = container;
386
387 inst = &mdstat->metadata_version[10+strlen(container->devname)+1];
388
389 new->info.array = mdi->array;
390 new->info.component_size = mdi->component_size;
391
392 for (i = 0; i < new->info.array.raid_disks; i++) {
393 struct mdinfo *newd = malloc(sizeof(*newd));
394
395 for (di = mdi->devs; di; di = di->next)
396 if (i == di->disk.raid_disk)
397 break;
398
399 if (di) {
400 memcpy(newd, di, sizeof(*newd));
401
402 newd->state_fd = sysfs_open(new->devnum,
403 newd->sys_name,
404 "state");
405
406 newd->prev_state = read_dev_state(newd->state_fd);
407 newd->curr_state = newd->prev_state;
408 } else if (failed + 1 > new->info.array.failed_disks) {
409 /* we cannot properly monitor without all working disks */
410 new->container = NULL;
411 break;
412 } else {
413 failed++;
414 free(newd);
415 continue;
416 }
417 sprintf(newd->sys_name, "rd%d", i);
418 newd->next = new->info.devs;
419 new->info.devs = newd;
420 }
421
422 new->action_fd = sysfs_open(new->devnum, NULL, "sync_action");
423 new->info.state_fd = sysfs_open(new->devnum, NULL, "array_state");
424 new->resync_start_fd = sysfs_open(new->devnum, NULL, "resync_start");
425 get_resync_start(new);
426 dprintf("%s: inst: %d action: %d state: %d\n", __func__, atoi(inst),
427 new->action_fd, new->info.state_fd);
428
429 sysfs_free(mdi);
430
431 /* if everything checks out tell the metadata handler we want to
432 * manage this instance
433 */
434 if (!aa_ready(new) || container->ss->open_new(container, new, inst) < 0) {
435 fprintf(stderr, "mdmon: failed to monitor %s\n",
436 mdstat->metadata_version);
437 new->container = NULL;
438 free_aa(new);
439 } else
440 replace_array(container, victim, new);
441 }
442
443 void manage(struct mdstat_ent *mdstat, struct supertype *container)
444 {
445 /* We have just read mdstat and need to compare it with
446 * the known active arrays.
447 * Arrays with the wrong metadata are ignored.
448 */
449
450 for ( ; mdstat ; mdstat = mdstat->next) {
451 struct active_array *a;
452 if (mdstat->devnum == container->devnum) {
453 manage_container(mdstat, container);
454 continue;
455 }
456 if (mdstat->metadata_version == NULL ||
457 strncmp(mdstat->metadata_version, "external:/", 10) != 0 ||
458 strncmp(mdstat->metadata_version+10, container->devname,
459 strlen(container->devname)) != 0 ||
460 mdstat->metadata_version[10+strlen(container->devname)]
461 != '/')
462 /* Not for this array */
463 continue;
464 /* Looks like a member of this container */
465 for (a = container->arrays; a; a = a->next) {
466 if (mdstat->devnum == a->devnum) {
467 if (a->container)
468 manage_member(mdstat, a);
469 break;
470 }
471 }
472 if (a == NULL || !a->container)
473 manage_new(mdstat, container, a);
474 }
475 }
476
477 static void handle_message(struct supertype *container, struct metadata_update *msg)
478 {
479 /* queue this metadata update through to the monitor */
480
481 struct metadata_update *mu;
482
483 if (msg->len == 0) {
484 int cnt;
485
486 while (update_queue_pending || update_queue) {
487 check_update_queue(container);
488 usleep(15*1000);
489 }
490
491 cnt = monitor_loop_cnt;
492 if (cnt & 1)
493 cnt += 2; /* wait until next pselect */
494 else
495 cnt += 3; /* wait for 2 pselects */
496 wakeup_monitor();
497
498 while (monitor_loop_cnt - cnt < 0)
499 usleep(10 * 1000);
500 } else {
501 mu = malloc(sizeof(*mu));
502 mu->len = msg->len;
503 mu->buf = msg->buf;
504 msg->buf = NULL;
505 mu->space = NULL;
506 mu->next = NULL;
507 if (container->ss->prepare_update)
508 container->ss->prepare_update(container, mu);
509 queue_metadata_update(mu);
510 }
511 }
512
513 void read_sock(struct supertype *container)
514 {
515 int fd;
516 struct metadata_update msg;
517 int terminate = 0;
518 long fl;
519 int tmo = 3; /* 3 second timeout before hanging up the socket */
520
521 fd = accept(container->sock, NULL, NULL);
522 if (fd < 0)
523 return;
524
525 fl = fcntl(fd, F_GETFL, 0);
526 fl |= O_NONBLOCK;
527 fcntl(fd, F_SETFL, fl);
528
529 do {
530 msg.buf = NULL;
531
532 /* read and validate the message */
533 if (receive_message(fd, &msg, tmo) == 0) {
534 handle_message(container, &msg);
535 if (ack(fd, tmo) < 0)
536 terminate = 1;
537 } else
538 terminate = 1;
539
540 } while (!terminate);
541
542 close(fd);
543 }
544
545 int exit_now = 0;
546 int manager_ready = 0;
547 void do_manager(struct supertype *container)
548 {
549 struct mdstat_ent *mdstat;
550 sigset_t set;
551
552 sigprocmask(SIG_UNBLOCK, NULL, &set);
553 sigdelset(&set, SIGUSR1);
554
555 do {
556
557 if (exit_now)
558 exit(0);
559
560 /* Can only 'manage' things if 'monitor' is not making
561 * structural changes to metadata, so need to check
562 * update_queue
563 */
564 if (update_queue == NULL) {
565 mdstat = mdstat_read(1, 0);
566
567 manage(mdstat, container);
568
569 read_sock(container);
570
571 free_mdstat(mdstat);
572 }
573 remove_old();
574
575 check_update_queue(container);
576
577 manager_ready = 1;
578
579 if (update_queue == NULL)
580 mdstat_wait_fd(container->sock, &set);
581 else
582 /* If an update is happening, just wait for signal */
583 pselect(0, NULL, NULL, NULL, NULL, &set);
584 } while(1);
585 }