1 /* 2 * Copyright (c) 1989 Jan-Simon Pendry 3 * Copyright (c) 1989 Imperial College of Science, Technology & Medicine 4 * Copyright (c) 1989, 1993 5 * The Regents of the University of California. All rights reserved. 6 * 7 * This code is derived from software contributed to Berkeley by 8 * Jan-Simon Pendry at Imperial College, London. 9 * 10 * %sccs.include.redist.c% 11 * 12 * @(#)srvr_afs.c 8.1 (Berkeley) 06/06/93 13 * 14 * $Id: srvr_afs.c,v 5.2.2.1 1992/02/09 15:09:05 jsp beta $ 15 * 16 */ 17 18 /* 19 * Automount FS server ("localhost") modeling 20 */ 21 22 #include "am.h" 23 24 extern qelem afs_srvr_list; 25 qelem afs_srvr_list = { &afs_srvr_list, &afs_srvr_list }; 26 27 static fserver *localhost; 28 29 /* 30 * Find an nfs server for the local host 31 */ 32 fserver *find_afs_srvr P((mntfs *)); 33 fserver *find_afs_srvr(mf) 34 mntfs *mf; 35 { 36 fserver *fs = localhost; 37 38 if (!fs) { 39 fs = ALLOC(fserver); 40 fs->fs_refc = 0; 41 fs->fs_host = strdup("localhost"); 42 fs->fs_ip = 0; 43 fs->fs_cid = 0; 44 fs->fs_pinger = 0; 45 fs->fs_flags = FSF_VALID; 46 fs->fs_type = "local"; 47 fs->fs_private = 0; 48 fs->fs_prfree = 0; 49 50 ins_que(&fs->fs_q, &afs_srvr_list); 51 52 srvrlog(fs, "starts up"); 53 54 localhost = fs; 55 } 56 57 fs->fs_refc++; 58 59 return fs; 60 } 61 62 /*------------------------------------------------------------------*/ 63 /* Generic routines follow */ 64 65 /* 66 * Wakeup anything waiting for this server 67 */ 68 void wakeup_srvr P((fserver *fs)); 69 void wakeup_srvr(fs) 70 fserver *fs; 71 { 72 fs->fs_flags &= ~FSF_WANT; 73 wakeup((voidp) fs); 74 } 75 76 /* 77 * Called when final ttl of server has expired 78 */ 79 static void timeout_srvr P((fserver *fs)); 80 static void timeout_srvr(fs) 81 fserver *fs; 82 { 83 /* 84 * If the reference count is still zero then 85 * we are free to remove this node 86 */ 87 if (fs->fs_refc == 0) { 88 #ifdef DEBUG 89 dlog("Deleting file server %s", fs->fs_host); 90 #endif /* DEBUG */ 91 if (fs->fs_flags & FSF_WANT) 92 wakeup_srvr(fs); 93 94 /* 95 * Remove from queue. 96 */ 97 rem_que(&fs->fs_q); 98 /* 99 * (Possibly) call the private free routine. 100 */ 101 if (fs->fs_private && fs->fs_prfree) 102 (*fs->fs_prfree)(fs->fs_private); 103 104 /* 105 * Free the net address 106 */ 107 if (fs->fs_ip) 108 free((voidp) fs->fs_ip); 109 110 /* 111 * Free the host name. 112 */ 113 free((voidp) fs->fs_host); 114 115 /* 116 * Discard the fserver object. 117 */ 118 free((voidp) fs); 119 } 120 } 121 122 /* 123 * Free a file server 124 */ 125 void free_srvr P((fserver *fs)); 126 void free_srvr(fs) 127 fserver *fs; 128 { 129 if (--fs->fs_refc == 0) { 130 /* 131 * The reference count is now zero, 132 * so arrange for this node to be 133 * removed in AM_TTL seconds if no 134 * other mntfs is referencing it. 135 */ 136 int ttl = (fs->fs_flags & (FSF_DOWN|FSF_ERROR)) ? 19 : AM_TTL; 137 #ifdef DEBUG 138 dlog("Last hard reference to file server %s - will timeout in %ds", fs->fs_host, ttl); 139 #endif /* DEBUG */ 140 if (fs->fs_cid) { 141 untimeout(fs->fs_cid); 142 /* 143 * Turn off pinging - XXX 144 */ 145 fs->fs_flags &= ~FSF_PINGING; 146 } 147 /* 148 * Keep structure lying around for a while 149 */ 150 fs->fs_cid = timeout(ttl, timeout_srvr, (voidp) fs); 151 /* 152 * Mark the fileserver down and invalid again 153 */ 154 fs->fs_flags &= ~FSF_VALID; 155 fs->fs_flags |= FSF_DOWN; 156 } 157 } 158 159 /* 160 * Make a duplicate fserver reference 161 */ 162 fserver *dup_srvr P((fserver *fs)); 163 fserver *dup_srvr(fs) 164 fserver *fs; 165 { 166 fs->fs_refc++; 167 return fs; 168 } 169 170 /* 171 * Log state change 172 */ 173 void srvrlog P((fserver *fs, char *state)); 174 void srvrlog(fs, state) 175 fserver *fs; 176 char *state; 177 { 178 plog(XLOG_INFO, "file server %s type %s %s", fs->fs_host, fs->fs_type, state); 179 } 180