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