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