1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162
|
/* ----------------------------------------------------------------------- *
*
* state.c - state machine functions.
*
* Copyright 2006 Ian Kent <raven@themaw.net> - All Rights Reserved
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, Inc., 675 Mass Ave, Cambridge MA 02139,
* USA; either version 2 of the License, or (at your option) any later
* version; incorporated herein by reference.
*
* ----------------------------------------------------------------------- */
#include <sys/ioctl.h>
#include "automount.h"
/* Attribute to create detached thread */
extern pthread_attr_t th_attr_detached;
struct state_queue {
pthread_t thid;
struct list_head list;
struct list_head pending;
struct autofs_point *ap;
enum states state;
unsigned int busy;
unsigned int done;
unsigned int cancel;
};
static pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;
static pthread_cond_t cond = PTHREAD_COND_INITIALIZER;
static unsigned int signaled = 0;
static LIST_HEAD(state_queue);
static void st_set_thid(struct autofs_point *, pthread_t);
static void st_set_done(struct autofs_point *ap);
void st_mutex_lock(void)
{
int status = pthread_mutex_lock(&mutex);
if (status)
fatal(status);
}
void st_mutex_unlock(void)
{
int status = pthread_mutex_unlock(&mutex);
if (status)
fatal(status);
}
/*
* Handle expire thread cleanup and return the next state the system
* should enter as a result.
*/
void expire_cleanup(void *arg)
{
struct ioctl_ops *ops = get_ioctl_ops();
pthread_t thid = pthread_self();
struct expire_args *ec;
struct autofs_point *ap;
int success;
enum states next = ST_INVAL;
ec = (struct expire_args *) arg;
ap = ec->ap;
success = ec->status;
st_mutex_lock();
debug(ap->logopt,
"got thid %lu path %s stat %d",
(unsigned long) thid, ap->path, success);
/* Check to see if expire process finished */
if (thid == ap->exp_thread) {
unsigned int idle;
int rv;
ap->exp_thread = 0;
switch (ap->state) {
case ST_EXPIRE:
/* FALLTHROUGH */
case ST_PRUNE:
/*
* If we're a submount and we've just pruned or
* expired everything away, try to shut down.
*/
if (ap->submount && !success) {
rv = ops->askumount(ap->logopt, ap->ioctlfd, &idle);
if (!rv && idle) {
next = ST_SHUTDOWN_PENDING;
break;
}
}
if (ap->state == ST_EXPIRE)
conditional_alarm_add(ap, ap->exp_runfreq);
/* FALLTHROUGH */
case ST_READY:
next = ST_READY;
break;
case ST_SHUTDOWN_PENDING:
/*
* If we reveive a mount request while trying to
* shutdown return to ready state unless we have
* been signaled to shutdown.
*/
rv = ops->askumount(ap->logopt, ap->ioctlfd, &idle);
if (!rv && !idle && !ap->shutdown) {
next = ST_READY;
conditional_alarm_add(ap, ap->exp_runfreq);
break;
}
next = ST_SHUTDOWN;
#ifdef ENABLE_IGNORE_BUSY_MOUNTS
break;
#else
if (success == 0)
break;
/* Failed shutdown returns to ready */
warn(ap->logopt, "filesystem %s still busy", ap->path);
conditional_alarm_add(ap, ap->exp_runfreq);
next = ST_READY;
break;
#endif
case ST_SHUTDOWN_FORCE:
next = ST_SHUTDOWN;
break;
default:
error(ap->logopt, "bad state %d", ap->state);
}
if (next != ST_INVAL) {
debug(ap->logopt,
"sigchld: exp %lu finished, switching from %d to %d",
(unsigned long) thid, ap->state, next);
}
}
st_set_done(ap);
if (next != ST_INVAL)
__st_add_task(ap, next);
st_mutex_unlock();
return;
}
static unsigned int st_ready(struct autofs_point *ap)
{
debug(ap->logopt,
"st_ready(): state = %d path %s", ap->state, ap->path);
ap->shutdown = 0;
ap->state = ST_READY;
return 1;
}
enum expire {
EXP_ERROR,
EXP_STARTED,
EXP_PARTIAL
};
/*
* Generate expiry messages. If "now" is true, timeouts are ignored.
*
* Returns: ERROR - error
* STARTED - expiry process started
* DONE - nothing to expire
* PARTIAL - partial expire
*/
void expire_proc_cleanup(void *arg)
{
struct expire_args *ea;
int status;
ea = (struct expire_args *) arg;
status = pthread_mutex_unlock(&ea->mutex);
if (status)
fatal(status);
status = pthread_cond_destroy(&ea->cond);
if (status)
fatal(status);
status = pthread_mutex_destroy(&ea->mutex);
if (status)
fatal(status);
free(ea);
return;
}
static enum expire expire_proc(struct autofs_point *ap, int how)
{
pthread_t thid;
struct expire_args *ea;
void *(*expire)(void *);
int status;
assert(ap->exp_thread == 0);
ea = malloc(sizeof(struct expire_args));
if (!ea) {
error(ap->logopt, "failed to malloc expire cond struct");
return EXP_ERROR;
}
status = pthread_mutex_init(&ea->mutex, NULL);
if (status)
fatal(status);
status = pthread_cond_init(&ea->cond, NULL);
if (status)
fatal(status);
status = pthread_mutex_lock(&ea->mutex);
if (status)
fatal(status);
ea->ap = ap;
ea->how = how;
ea->status = 1;
if (ap->type == LKP_INDIRECT)
expire = expire_proc_indirect;
else
expire = expire_proc_direct;
status = pthread_create(&thid, &th_attr_detached, expire, ea);
if (status) {
error(ap->logopt,
"expire thread create for %s failed", ap->path);
expire_proc_cleanup((void *) ea);
return EXP_ERROR;
}
ap->exp_thread = thid;
st_set_thid(ap, thid);
pthread_cleanup_push(expire_proc_cleanup, ea);
debug(ap->logopt, "exp_proc = %lu path %s",
(unsigned long) ap->exp_thread, ap->path);
ea->signaled = 0;
while (!ea->signaled) {
status = pthread_cond_wait(&ea->cond, &ea->mutex);
if (status)
fatal(status);
}
pthread_cleanup_pop(1);
return EXP_STARTED;
}
static void do_readmap_cleanup(void *arg)
{
struct readmap_args *ra;
struct autofs_point *ap;
ra = (struct readmap_args *) arg;
ap = ra->ap;
st_mutex_lock();
ap->readmap_thread = 0;
st_set_done(ap);
st_ready(ap);
st_mutex_unlock();
free(ra);
return;
}
static int do_readmap_mount(struct autofs_point *ap,
struct map_source *map, struct mapent *me, time_t now)
{
struct mapent_cache *nc;
struct mapent *ne, *nested, *valid;
int ret = 0;
nc = ap->entry->master->nc;
cache_writelock(nc);
ne = cache_lookup_distinct(nc, me->key);
if (ne)
cache_unlock(nc);
else {
nested = cache_partial_match(nc, me->key);
if (!nested)
cache_unlock(nc);
else {
cache_delete(nc, nested->key);
cache_unlock(nc);
error(ap->logopt,
"removing invalid nested null entry %s",
nested->key);
}
}
if (me->age < now || (ne && map->master_line > ne->age)) {
/*
* The map instance may have changed, such as the map name or
* the mount options, but the direct map entry may still exist
* in one of the other maps. If so then update the new cache
* entry device and inode so we can find it at lookup. Later,
* the mount for the new cache entry will just update the
* timeout.
*
* TODO: how do we recognise these orphaned map instances. We
* can't just delete these instances when the cache becomes
* empty because that is a valid state for a master map entry.
* This is because of the requirement to continue running with
* an empty cache awaiting a map re-load.
*/
valid = lookup_source_valid_mapent(ap, me->key, LKP_DISTINCT);
if (valid && valid->mc == me->mc) {
/*
* We've found a map entry that has been removed from
* the current cache so there is no need to update it.
* The stale entry will be dealt with when we prune the
* cache later.
*/
cache_unlock(valid->mc);
valid = NULL;
}
if (valid) {
struct mapent_cache *vmc = valid->mc;
struct ioctl_ops *ops = get_ioctl_ops();
time_t timeout;
time_t runfreq;
cache_unlock(vmc);
debug(ap->logopt,
"updating cache entry for valid direct trigger %s",
me->key);
cache_writelock(vmc);
valid = cache_lookup_distinct(vmc, me->key);
if (!valid) {
cache_unlock(vmc);
error(ap->logopt,
"failed to find expected existing valid map entry");
return ret;
}
/* Take over the mount if there is one */
valid->ioctlfd = me->ioctlfd;
me->ioctlfd = -1;
/* Set device and inode number of the new mapent */
cache_set_ino_index(vmc, me);
cache_unlock(vmc);
/* Set timeout and calculate the expire run frequency */
timeout = get_exp_timeout(ap, map);
ops->timeout(ap->logopt, valid->ioctlfd, timeout);
if (timeout) {
runfreq = (timeout + CHECK_RATIO - 1) / CHECK_RATIO;
if (ap->exp_runfreq)
ap->exp_runfreq = min(ap->exp_runfreq, runfreq);
else
ap->exp_runfreq = runfreq;
}
} else if (!is_mounted(me->key, MNTS_REAL))
ret = do_umount_autofs_direct(ap, me);
else
debug(ap->logopt,
"%s is mounted", me->key);
} else
do_mount_autofs_direct(ap, me, get_exp_timeout(ap, map));
return ret;
}
static void *do_readmap(void *arg)
{
struct autofs_point *ap;
struct map_source *map;
struct mapent_cache *mc;
struct readmap_args *ra;
int status;
time_t now;
ra = (struct readmap_args *) arg;
status = pthread_mutex_lock(&ra->mutex);
if (status)
fatal(status);
ap = ra->ap;
now = ra->now;
ra->signaled = 1;
status = pthread_cond_signal(&ra->cond);
if (status) {
error(ap->logopt, "failed to signal expire condition");
pthread_mutex_unlock(&ra->mutex);
fatal(status);
}
status = pthread_mutex_unlock(&ra->mutex);
if (status)
fatal(status);
pthread_cleanup_push(do_readmap_cleanup, ra);
info(ap->logopt, "re-reading map for %s", ap->path);
status = lookup_nss_read_map(ap, NULL, now);
if (!status)
pthread_exit(NULL);
if (ap->type == LKP_INDIRECT) {
struct ioctl_ops *ops = get_ioctl_ops();
time_t timeout = get_exp_timeout(ap, ap->entry->maps);
ap->exp_runfreq = (timeout + CHECK_RATIO - 1) / CHECK_RATIO;
ops->timeout(ap->logopt, ap->ioctlfd, timeout);
lookup_prune_cache(ap, now);
status = lookup_ghost(ap);
} else {
struct mapent *me;
unsigned int append_alarm = !ap->exp_runfreq;
master_source_readlock(ap->entry);
pthread_cleanup_push(master_source_lock_cleanup, ap->entry);
map = ap->entry->maps;
while (map) {
/* Is map source up to date or no longer valid */
if (!map->stale && !check_stale_instances(map)) {
map = map->next;
continue;
}
mc = map->mc;
pthread_cleanup_push(cache_lock_cleanup, mc);
cache_readlock(mc);
restart:
me = cache_enumerate(mc, NULL);
while (me) {
int ret = do_readmap_mount(ap, map, me, now);
if (ret == -1)
goto restart;
me = cache_enumerate(mc, me);
}
lookup_prune_one_cache(ap, map->mc, now);
pthread_cleanup_pop(1);
clear_stale_instances(map);
map->stale = 0;
map = map->next;
}
/* If the direct mount map was empty at startup no expire
* alarm will have been added. So add it here if there are
* now map entries.
*/
if (append_alarm && ap->exp_runfreq) {
time_t seconds = ap->exp_runfreq + rand() % ap->exp_runfreq;
conditional_alarm_add(ap, seconds);
}
pthread_cleanup_pop(1);
}
pthread_cleanup_pop(1);
return NULL;
}
static void st_readmap_cleanup(void *arg)
{
struct readmap_args *ra;
int status;
ra = (struct readmap_args *) arg;
status = pthread_mutex_unlock(&ra->mutex);
if (status)
fatal(status);
status = pthread_cond_destroy(&ra->cond);
if (status)
fatal(status);
status = pthread_mutex_destroy(&ra->mutex);
if (status)
fatal(status);
return;
}
static unsigned int st_readmap(struct autofs_point *ap)
{
pthread_t thid;
struct readmap_args *ra;
int status;
int now = monotonic_time(NULL);
debug(ap->logopt, "state %d path %s", ap->state, ap->path);
assert(ap->state == ST_READY);
assert(ap->readmap_thread == 0);
ap->state = ST_READMAP;
ra = malloc(sizeof(struct readmap_args));
if (!ra) {
error(ap->logopt, "failed to malloc readmap cond struct");
/* It didn't work: return to ready */
st_ready(ap);
conditional_alarm_add(ap, ap->exp_runfreq);
return 0;
}
status = pthread_mutex_init(&ra->mutex, NULL);
if (status)
fatal(status);
status = pthread_cond_init(&ra->cond, NULL);
if (status)
fatal(status);
status = pthread_mutex_lock(&ra->mutex);
if (status)
fatal(status);
ra->ap = ap;
ra->now = now;
status = pthread_create(&thid, &th_attr_detached, do_readmap, ra);
if (status) {
error(ap->logopt, "readmap thread create failed");
st_readmap_cleanup(ra);
free(ra);
/* It didn't work: return to ready */
st_ready(ap);
conditional_alarm_add(ap, ap->exp_runfreq);
return 0;
}
ap->readmap_thread = thid;
st_set_thid(ap, thid);
pthread_cleanup_push(st_readmap_cleanup, ra);
ra->signaled = 0;
while (!ra->signaled) {
status = pthread_cond_wait(&ra->cond, &ra->mutex);
if (status)
fatal(status);
}
pthread_cleanup_pop(1);
return 1;
}
static unsigned int st_prepare_shutdown(struct autofs_point *ap)
{
int exp;
debug(ap->logopt, "state %d path %s", ap->state, ap->path);
assert(ap->state == ST_READY || ap->state == ST_EXPIRE);
ap->state = ST_SHUTDOWN_PENDING;
/* Unmount everything */
exp = expire_proc(ap, AUTOFS_EXP_IMMEDIATE);
switch (exp) {
case EXP_ERROR:
case EXP_PARTIAL:
/* It didn't work: return to ready */
conditional_alarm_add(ap, ap->exp_runfreq);
st_ready(ap);
return 0;
case EXP_STARTED:
return 1;
}
return 0;
}
static unsigned int st_force_shutdown(struct autofs_point *ap)
{
int exp;
debug(ap->logopt, "state %d path %s", ap->state, ap->path);
assert(ap->state == ST_READY || ap->state == ST_EXPIRE);
ap->state = ST_SHUTDOWN_FORCE;
/* Unmount everything */
exp = expire_proc(ap, AUTOFS_EXP_FORCE | AUTOFS_EXP_IMMEDIATE);
switch (exp) {
case EXP_ERROR:
case EXP_PARTIAL:
/* It didn't work: return to ready */
conditional_alarm_add(ap, ap->exp_runfreq);
st_ready(ap);
return 0;
case EXP_STARTED:
return 1;
}
return 0;
}
static unsigned int st_shutdown(struct autofs_point *ap)
{
int ret;
debug(ap->logopt, "state %d path %s", ap->state, ap->path);
assert(ap->state == ST_SHUTDOWN_PENDING || ap->state == ST_SHUTDOWN_FORCE);
ap->state = ST_SHUTDOWN;
ret = pthread_kill(ap->thid, SIGCONT);
if (ret)
error(LOGOPT_ANY, "error %d sending shutdown signal", ret);
return 0;
}
static unsigned int st_prune(struct autofs_point *ap)
{
debug(ap->logopt, "state %d path %s", ap->state, ap->path);
assert(ap->state == ST_READY);
ap->state = ST_PRUNE;
switch (expire_proc(ap, AUTOFS_EXP_IMMEDIATE)) {
case EXP_ERROR:
case EXP_PARTIAL:
conditional_alarm_add(ap, ap->exp_runfreq);
st_ready(ap);
return 0;
case EXP_STARTED:
return 1;
}
return 0;
}
static unsigned int st_expire(struct autofs_point *ap)
{
debug(ap->logopt, "state %d path %s", ap->state, ap->path);
assert(ap->state == ST_READY);
ap->state = ST_EXPIRE;
switch (expire_proc(ap, AUTOFS_EXP_NORMAL)) {
case EXP_ERROR:
case EXP_PARTIAL:
conditional_alarm_add(ap, ap->exp_runfreq);
st_ready(ap);
return 0;
case EXP_STARTED:
return 1;
}
return 0;
}
static struct state_queue *st_alloc_task(struct autofs_point *ap, enum states state)
{
struct state_queue *task;
task = malloc(sizeof(struct state_queue));
if (!task)
return NULL;
memset(task, 0, sizeof(struct state_queue));
task->ap = ap;
task->state = state;
INIT_LIST_HEAD(&task->list);
INIT_LIST_HEAD(&task->pending);
return task;
}
/*
* Insert alarm entry on ordered list.
* State queue mutex and ap state mutex, in that order, must be held.
*/
int __st_add_task(struct autofs_point *ap, enum states state)
{
struct list_head *head;
struct list_head *p, *q;
struct state_queue *new;
unsigned int empty = 1;
int status;
/* Task termination marker, poke state machine */
if (state == ST_READY) {
st_ready(ap);
signaled = 1;
status = pthread_cond_signal(&cond);
if (status)
fatal(status);
return 1;
}
if (ap->state == ST_SHUTDOWN)
return 1;
if (state == ST_SHUTDOWN)
return st_shutdown(ap);
head = &state_queue;
/* Add to task queue for autofs_point ? */
list_for_each(p, head) {
struct state_queue *task;
task = list_entry(p, struct state_queue, list);
if (task->ap != ap)
continue;
empty = 0;
/* Don't add duplicate tasks */
if ((task->state == state && !task->done) ||
(ap->state == ST_SHUTDOWN_PENDING ||
ap->state == ST_SHUTDOWN_FORCE))
break;
/* No pending tasks */
if (list_empty(&task->pending)) {
new = st_alloc_task(ap, state);
if (new)
list_add_tail(&new->pending, &task->pending);
goto done;
}
list_for_each(q, &task->pending) {
struct state_queue *p_task;
p_task = list_entry(q, struct state_queue, pending);
if (p_task->state == state ||
(ap->state == ST_SHUTDOWN_PENDING ||
ap->state == ST_SHUTDOWN_FORCE))
goto done;
}
new = st_alloc_task(ap, state);
if (new)
list_add_tail(&new->pending, &task->pending);
done:
break;
}
if (empty) {
new = st_alloc_task(ap, state);
if (new)
list_add(&new->list, head);
}
signaled = 1;
status = pthread_cond_signal(&cond);
if (status)
fatal(status);
return 1;
}
int st_add_task(struct autofs_point *ap, enum states state)
{
int ret;
st_mutex_lock();
ret = __st_add_task(ap, state);
st_mutex_unlock();
return ret;
}
/*
* Remove state queue tasks for ap.
* State queue mutex and ap state mutex, in that order, must be held.
*/
void st_remove_tasks(struct autofs_point *ap)
{
struct list_head *head;
struct list_head *p, *q;
struct state_queue *task, *waiting;
int status;
st_mutex_lock();
head = &state_queue;
if (list_empty(head)) {
st_mutex_unlock();
return;
}
p = head->next;
while (p != head) {
task = list_entry(p, struct state_queue, list);
p = p->next;
if (task->ap != ap)
continue;
if (task->busy) {
/* We only cancel readmap, prune and expire */
if (task->state == ST_EXPIRE ||
task->state == ST_PRUNE ||
task->state == ST_READMAP)
task->cancel = 1;
}
q = (&task->pending)->next;
while(q != &task->pending) {
waiting = list_entry(q, struct state_queue, pending);
q = q->next;
/* Don't remove existing shutdown task */
if (waiting->state != ST_SHUTDOWN_PENDING &&
waiting->state != ST_SHUTDOWN_FORCE) {
list_del(&waiting->pending);
free(waiting);
}
}
}
signaled = 1;
status = pthread_cond_signal(&cond);
if (status)
fatal(status);
st_mutex_unlock();
return;
}
static int st_task_active(struct autofs_point *ap, enum states state)
{
struct list_head *head;
struct list_head *p, *q;
struct state_queue *task, *waiting;
unsigned int active = 0;
st_mutex_lock();
head = &state_queue;
list_for_each(p, head) {
task = list_entry(p, struct state_queue, list);
if (task->ap != ap)
continue;
if (task->state == state) {
active = 1;
break;
}
if (state == ST_ANY) {
active = 1;
break;
}
list_for_each(q, &task->pending) {
waiting = list_entry(q, struct state_queue, pending);
if (waiting->state == state) {
active = 1;
break;
}
if (state == ST_ANY) {
active = 1;
break;
}
}
}
st_mutex_unlock();
return active;
}
int st_wait_task(struct autofs_point *ap, enum states state, unsigned int seconds)
{
unsigned int wait = 0;
unsigned int duration = 0;
int ret = 0;
while (1) {
struct timespec t = { 0, 200000000 };
struct timespec r;
while (nanosleep(&t, &r) == -1 && errno == EINTR)
memcpy(&t, &r, sizeof(struct timespec));
if (wait++ == 4) {
wait = 0;
duration++;
}
if (!st_task_active(ap, state)) {
ret = 1;
break;
}
if (seconds && duration >= seconds)
break;
}
return ret;
}
int st_wait_state(struct autofs_point *ap, enum states state)
{
while (1) {
struct timespec t = { 0, 200000000 };
struct timespec r;
while (nanosleep(&t, &r) == -1 && errno == EINTR)
memcpy(&t, &r, sizeof(struct timespec));
st_mutex_lock();
if (ap->state == state) {
st_mutex_unlock();
return 1;
}
st_mutex_unlock();
}
return 0;
}
static int run_state_task(struct state_queue *task)
{
struct autofs_point *ap;
enum states next_state, state;
unsigned long ret = 0;
ap = task->ap;
next_state = task->state;
state = ap->state;
if (next_state != state) {
switch (next_state) {
case ST_PRUNE:
ret = st_prune(ap);
break;
case ST_EXPIRE:
ret = st_expire(ap);
break;
case ST_READMAP:
ret = st_readmap(ap);
break;
case ST_SHUTDOWN_PENDING:
ret = st_prepare_shutdown(ap);
break;
case ST_SHUTDOWN_FORCE:
ret = st_force_shutdown(ap);
break;
default:
error(ap->logopt, "bad next state %d", next_state);
}
}
return ret;
}
static void st_set_thid(struct autofs_point *ap, pthread_t thid)
{
struct list_head *p, *head = &state_queue;
struct state_queue *task;
list_for_each(p, head) {
task = list_entry(p, struct state_queue, list);
if (task->ap == ap) {
task->thid = thid;
break;
}
}
return;
}
/* Requires state mutex to be held */
static void st_set_done(struct autofs_point *ap)
{
struct list_head *p, *head;
struct state_queue *task;
head = &state_queue;
list_for_each(p, head) {
task = list_entry(p, struct state_queue, list);
if (task->ap == ap) {
task->done = 1;
break;
}
}
return;
}
static void *st_queue_handler(void *arg)
{
struct list_head *head;
struct list_head *p;
int status, ret;
st_mutex_lock();
while (1) {
/*
* If the state queue list is empty, wait until an
* entry is added.
*/
head = &state_queue;
while (list_empty(head)) {
status = pthread_cond_wait(&cond, &mutex);
if (status)
fatal(status);
}
p = head->next;
while(p != head) {
struct state_queue *task;
task = list_entry(p, struct state_queue, list);
p = p->next;
if (task->cancel) {
list_del(&task->list);
free(task);
continue;
}
task->busy = 1;
ret = run_state_task(task);
if (!ret) {
list_del(&task->list);
free(task);
}
}
while (1) {
signaled = 0;
while (!signaled) {
status = pthread_cond_wait(&cond, &mutex);
if (status)
fatal(status);
}
head = &state_queue;
p = head->next;
while (p != head) {
struct state_queue *task, *next;
task = list_entry(p, struct state_queue, list);
p = p->next;
/* Task may have been canceled before it started */
if (!task->thid && task->cancel)
goto remove;
if (!task->busy) {
/* Start a new task */
task->busy = 1;
ret = run_state_task(task);
if (!ret)
goto remove;
continue;
}
/* Still starting up */
if (!task->thid)
continue;
if (task->cancel) {
pthread_cancel(task->thid);
task->cancel = 0;
continue;
}
/* Still busy */
if (!task->done)
continue;
remove:
/* No more tasks for this queue */
if (list_empty(&task->pending)) {
list_del(&task->list);
free(task);
continue;
}
/* Next task */
next = list_entry((&task->pending)->next,
struct state_queue, pending);
list_del(&task->list);
list_del_init(&next->pending);
free(task);
list_add_tail(&next->list, head);
if (p == head)
p = head->next;
}
if (list_empty(head))
break;
}
}
}
int st_start_handler(void)
{
pthread_t thid;
pthread_attr_t attrs;
pthread_attr_t *pattrs = &attrs;
int status;
status = pthread_attr_init(pattrs);
if (status)
pattrs = NULL;
else
pthread_attr_setdetachstate(pattrs, PTHREAD_CREATE_DETACHED);
status = pthread_create(&thid, pattrs, st_queue_handler, NULL);
if (pattrs)
pthread_attr_destroy(pattrs);
return !status;
}
|