xref: /openbsd-src/usr.sbin/amd/amd/srvr_afs.c (revision b2ea75c1b17e1a9a339660e7ed45cd24946b230e)
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  * Redistribution and use in source and binary forms, with or without
11  * modification, are permitted provided that the following conditions
12  * are met:
13  * 1. Redistributions of source code must retain the above copyright
14  *    notice, this list of conditions and the following disclaimer.
15  * 2. Redistributions in binary form must reproduce the above copyright
16  *    notice, this list of conditions and the following disclaimer in the
17  *    documentation and/or other materials provided with the distribution.
18  * 3. All advertising materials mentioning features or use of this software
19  *    must display the following acknowledgement:
20  *	This product includes software developed by the University of
21  *	California, Berkeley and its contributors.
22  * 4. Neither the name of the University nor the names of its contributors
23  *    may be used to endorse or promote products derived from this software
24  *    without specific prior written permission.
25  *
26  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
27  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
28  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
29  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
30  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
31  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
32  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
33  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
34  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
35  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
36  * SUCH DAMAGE.
37  *
38  *	from: @(#)srvr_afs.c	8.1 (Berkeley) 6/6/93
39  *	$Id: srvr_afs.c,v 1.1.1.1 1995/10/18 08:47:12 deraadt Exp $
40  */
41 
42 /*
43  * Automount FS server ("localhost") modeling
44  */
45 
46 #include "am.h"
47 
48 extern qelem afs_srvr_list;
49 qelem afs_srvr_list = { &afs_srvr_list, &afs_srvr_list };
50 
51 static fserver *localhost;
52 
53 /*
54  * Find an nfs server for the local host
55  */
56 fserver *find_afs_srvr P((mntfs *));
57 fserver *find_afs_srvr(mf)
58 mntfs *mf;
59 {
60 	fserver *fs = localhost;
61 
62 	if (!fs) {
63 		fs = ALLOC(fserver);
64 		fs->fs_refc = 0;
65 		fs->fs_host = strdup("localhost");
66 		fs->fs_ip = 0;
67 		fs->fs_cid = 0;
68 		fs->fs_pinger = 0;
69 		fs->fs_flags = FSF_VALID;
70 		fs->fs_type = "local";
71 		fs->fs_private = 0;
72 		fs->fs_prfree = 0;
73 
74 		ins_que(&fs->fs_q, &afs_srvr_list);
75 
76 		srvrlog(fs, "starts up");
77 
78 		localhost = fs;
79 	}
80 
81 	fs->fs_refc++;
82 
83 	return fs;
84 }
85 
86 /*------------------------------------------------------------------*/
87 		/* Generic routines follow */
88 
89 /*
90  * Wakeup anything waiting for this server
91  */
92 void wakeup_srvr P((fserver *fs));
93 void wakeup_srvr(fs)
94 fserver *fs;
95 {
96 	fs->fs_flags &= ~FSF_WANT;
97 	wakeup((voidp) fs);
98 }
99 
100 /*
101  * Called when final ttl of server has expired
102  */
103 static void timeout_srvr P((fserver *fs));
104 static void timeout_srvr(fs)
105 fserver *fs;
106 {
107 	/*
108 	 * If the reference count is still zero then
109 	 * we are free to remove this node
110 	 */
111 	if (fs->fs_refc == 0) {
112 #ifdef DEBUG
113 		dlog("Deleting file server %s", fs->fs_host);
114 #endif /* DEBUG */
115 		if (fs->fs_flags & FSF_WANT)
116 			wakeup_srvr(fs);
117 
118 		/*
119 		 * Remove from queue.
120 		 */
121 		rem_que(&fs->fs_q);
122 		/*
123 		 * (Possibly) call the private free routine.
124 		 */
125 		if (fs->fs_private && fs->fs_prfree)
126 			(*fs->fs_prfree)(fs->fs_private);
127 
128 		/*
129 		 * Free the net address
130 		 */
131 		if (fs->fs_ip)
132 			free((voidp) fs->fs_ip);
133 
134 		/*
135 		 * Free the host name.
136 		 */
137 		free((voidp) fs->fs_host);
138 
139 		/*
140 		 * Discard the fserver object.
141 		 */
142 		free((voidp) fs);
143 	}
144 }
145 
146 /*
147  * Free a file server
148  */
149 void free_srvr P((fserver *fs));
150 void free_srvr(fs)
151 fserver *fs;
152 {
153 	if (--fs->fs_refc == 0) {
154 		/*
155 		 * The reference count is now zero,
156 		 * so arrange for this node to be
157 		 * removed in AM_TTL seconds if no
158 		 * other mntfs is referencing it.
159 		 */
160 		int ttl = (fs->fs_flags & (FSF_DOWN|FSF_ERROR)) ? 19 : AM_TTL;
161 #ifdef DEBUG
162 		dlog("Last hard reference to file server %s - will timeout in %ds", fs->fs_host, ttl);
163 #endif /* DEBUG */
164 		if (fs->fs_cid) {
165 			untimeout(fs->fs_cid);
166 			/*
167 			 * Turn off pinging - XXX
168 			 */
169 			fs->fs_flags &= ~FSF_PINGING;
170 		}
171 		/*
172 		 * Keep structure lying around for a while
173 		 */
174 		fs->fs_cid = timeout(ttl, timeout_srvr, (voidp) fs);
175 		/*
176 		 * Mark the fileserver down and invalid again
177 		 */
178 		fs->fs_flags &= ~FSF_VALID;
179 		fs->fs_flags |= FSF_DOWN;
180 	}
181 }
182 
183 /*
184  * Make a duplicate fserver reference
185  */
186 fserver *dup_srvr P((fserver *fs));
187 fserver *dup_srvr(fs)
188 fserver *fs;
189 {
190 	fs->fs_refc++;
191 	return fs;
192 }
193 
194 /*
195  * Log state change
196  */
197 void srvrlog P((fserver *fs, char *state));
198 void srvrlog(fs, state)
199 fserver *fs;
200 char *state;
201 {
202 	plog(XLOG_INFO, "file server %s type %s %s", fs->fs_host, fs->fs_type, state);
203 }
204