1139823Simp/*-
21541Srgrimes * Copyright (c) 1989, 1993
31541Srgrimes *	The Regents of the University of California.  All rights reserved.
41541Srgrimes *
51541Srgrimes * This code is derived from software contributed to Berkeley by
61541Srgrimes * Rick Macklem at The University of Guelph.
71541Srgrimes *
81541Srgrimes * Redistribution and use in source and binary forms, with or without
91541Srgrimes * modification, are permitted provided that the following conditions
101541Srgrimes * are met:
111541Srgrimes * 1. Redistributions of source code must retain the above copyright
121541Srgrimes *    notice, this list of conditions and the following disclaimer.
131541Srgrimes * 2. Redistributions in binary form must reproduce the above copyright
141541Srgrimes *    notice, this list of conditions and the following disclaimer in the
151541Srgrimes *    documentation and/or other materials provided with the distribution.
161541Srgrimes * 4. Neither the name of the University nor the names of its contributors
171541Srgrimes *    may be used to endorse or promote products derived from this software
181541Srgrimes *    without specific prior written permission.
191541Srgrimes *
201541Srgrimes * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
211541Srgrimes * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
221541Srgrimes * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
231541Srgrimes * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
241541Srgrimes * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
251541Srgrimes * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
261541Srgrimes * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
271541Srgrimes * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
281541Srgrimes * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
291541Srgrimes * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
301541Srgrimes * SUCH DAMAGE.
311541Srgrimes *
3222521Sdyson *	@(#)nfs_syscalls.c	8.5 (Berkeley) 3/30/95
331541Srgrimes */
341541Srgrimes
3583651Speter#include <sys/cdefs.h>
3683651Speter__FBSDID("$FreeBSD$");
3783651Speter
381541Srgrimes#include <sys/param.h>
391541Srgrimes#include <sys/systm.h>
4012274Sbde#include <sys/sysproto.h>
411541Srgrimes#include <sys/kernel.h>
4219449Sdfr#include <sys/sysctl.h>
431541Srgrimes#include <sys/file.h>
4415480Sbde#include <sys/filedesc.h>
451541Srgrimes#include <sys/vnode.h>
4630354Sphk#include <sys/malloc.h>
471541Srgrimes#include <sys/mount.h>
481541Srgrimes#include <sys/proc.h>
4960041Sphk#include <sys/bio.h>
501541Srgrimes#include <sys/buf.h>
511541Srgrimes#include <sys/mbuf.h>
521541Srgrimes#include <sys/socket.h>
531541Srgrimes#include <sys/socketvar.h>
541541Srgrimes#include <sys/domain.h>
551541Srgrimes#include <sys/protosw.h>
561541Srgrimes#include <sys/namei.h>
5783651Speter#include <sys/unistd.h>
5883651Speter#include <sys/kthread.h>
5975631Salfred#include <sys/fcntl.h>
6075631Salfred#include <sys/lockf.h>
6183651Speter#include <sys/mutex.h>
62212506Skib#include <sys/taskqueue.h>
631541Srgrimes
641541Srgrimes#include <netinet/in.h>
651541Srgrimes#include <netinet/tcp.h>
66122698Salfred
679336Sdfr#include <nfs/xdr_subs.h>
689336Sdfr#include <nfs/nfsproto.h>
6983651Speter#include <nfsclient/nfs.h>
7083651Speter#include <nfsclient/nfsm_subs.h>
7183651Speter#include <nfsclient/nfsmount.h>
7283651Speter#include <nfsclient/nfsnode.h>
73210455Srmacklem#include <nfs/nfs_lock.h>
741541Srgrimes
75151897Srwatsonstatic MALLOC_DEFINE(M_NFSSVC, "nfsclient_srvsock", "Nfs server structure");
7630309Sphk
7783651Speterstatic void	nfssvc_iod(void *);
781541Srgrimes
791541Srgrimesstatic int nfs_asyncdaemon[NFS_MAXASYNCDAEMON];
8012457Sbde
81221973SrmacklemSYSCTL_DECL(_vfs_oldnfs);
8244112Sdfr
8389407Speter/* Maximum number of seconds a nfsiod kthread will sleep before exiting */
8489407Speterstatic unsigned int nfs_iodmaxidle = 120;
85221973SrmacklemSYSCTL_UINT(_vfs_oldnfs, OID_AUTO, iodmaxidle, CTLFLAG_RW, &nfs_iodmaxidle, 0,
86184561Strhodes    "Max number of seconds an nfsiod kthread will sleep before exiting");
8789407Speter
8889407Speter/* Maximum number of nfsiod kthreads */
89128111Speadarunsigned int nfs_iodmax = 20;
9089407Speter
9189324Speter/* Minimum number of nfsiod kthreads to keep as spares */
92158903Srwatsonstatic unsigned int nfs_iodmin = 0;
9389324Speter
94214026Skibstatic int nfs_nfsiodnew_sync(void);
95214026Skib
9689407Speterstatic int
9789407Spetersysctl_iodmin(SYSCTL_HANDLER_ARGS)
9889407Speter{
9989407Speter	int error, i;
10089407Speter	int newmin;
10189324Speter
10289407Speter	newmin = nfs_iodmin;
10389407Speter	error = sysctl_handle_int(oidp, &newmin, 0, req);
10489407Speter	if (error || (req->newptr == NULL))
10589407Speter		return (error);
106158739Smohans	mtx_lock(&nfs_iod_mtx);
107158739Smohans	if (newmin > nfs_iodmax) {
108158739Smohans		error = EINVAL;
109158739Smohans		goto out;
110158739Smohans	}
11189407Speter	nfs_iodmin = newmin;
11289407Speter	if (nfs_numasync >= nfs_iodmin)
113158739Smohans		goto out;
11489407Speter	/*
11589407Speter	 * If the current number of nfsiod is lower
11689407Speter	 * than the new minimum, create some more.
11789407Speter	 */
11889407Speter	for (i = nfs_iodmin - nfs_numasync; i > 0; i--)
119214026Skib		nfs_nfsiodnew_sync();
120158739Smohansout:
121158739Smohans	mtx_unlock(&nfs_iod_mtx);
12289407Speter	return (0);
12389407Speter}
124221973SrmacklemSYSCTL_PROC(_vfs_oldnfs, OID_AUTO, iodmin, CTLTYPE_UINT | CTLFLAG_RW, 0,
125184561Strhodes    sizeof (nfs_iodmin), sysctl_iodmin, "IU",
126184561Strhodes    "Min number of nfsiod kthreads to keep as spares");
12789407Speter
12889407Speter
12989407Speterstatic int
13089407Spetersysctl_iodmax(SYSCTL_HANDLER_ARGS)
13189407Speter{
13289407Speter	int error, i;
13389407Speter	int iod, newmax;
13489407Speter
13589407Speter	newmax = nfs_iodmax;
13689407Speter	error = sysctl_handle_int(oidp, &newmax, 0, req);
13789407Speter	if (error || (req->newptr == NULL))
13889407Speter		return (error);
13989407Speter	if (newmax > NFS_MAXASYNCDAEMON)
14089407Speter		return (EINVAL);
141158739Smohans	mtx_lock(&nfs_iod_mtx);
14289407Speter	nfs_iodmax = newmax;
14389407Speter	if (nfs_numasync <= nfs_iodmax)
144158739Smohans		goto out;
14589407Speter	/*
14689407Speter	 * If there are some asleep nfsiods that should
14789407Speter	 * exit, wakeup() them so that they check nfs_iodmax
14889407Speter	 * and exit.  Those who are active will exit as
14989407Speter	 * soon as they finish I/O.
15089407Speter	 */
15189407Speter	iod = nfs_numasync - 1;
15289407Speter	for (i = 0; i < nfs_numasync - nfs_iodmax; i++) {
153203072Srmacklem		if (nfs_iodwant[iod] == NFSIOD_AVAILABLE)
154111748Sdes			wakeup(&nfs_iodwant[iod]);
15589407Speter		iod--;
15689407Speter	}
157158739Smohansout:
158158739Smohans	mtx_unlock(&nfs_iod_mtx);
15989407Speter	return (0);
16089407Speter}
161221973SrmacklemSYSCTL_PROC(_vfs_oldnfs, OID_AUTO, iodmax, CTLTYPE_UINT | CTLFLAG_RW, 0,
162184561Strhodes    sizeof (nfs_iodmax), sysctl_iodmax, "IU",
163184561Strhodes    "Max number of nfsiod kthreads");
16489407Speter
165214026Skibstatic int
166214026Skibnfs_nfsiodnew_sync(void)
167214026Skib{
168214026Skib	int error, i;
169214026Skib
170214026Skib	mtx_assert(&nfs_iod_mtx, MA_OWNED);
171214026Skib	for (i = 0; i < nfs_iodmax; i++) {
172214026Skib		if (nfs_asyncdaemon[i] == 0) {
173214026Skib			nfs_asyncdaemon[i] = 1;
174214026Skib			break;
175214026Skib		}
176214026Skib	}
177214026Skib	if (i == nfs_iodmax)
178214026Skib		return (0);
179214026Skib	mtx_unlock(&nfs_iod_mtx);
180214026Skib	error = kproc_create(nfssvc_iod, nfs_asyncdaemon + i, NULL,
181214026Skib	    RFHIGHPID, 0, "nfsiod %d", i);
182214026Skib	mtx_lock(&nfs_iod_mtx);
183214026Skib	if (error == 0) {
184214026Skib		nfs_numasync++;
185214026Skib		nfs_iodwant[i] = NFSIOD_AVAILABLE;
186214026Skib	} else
187214026Skib		nfs_asyncdaemon[i] = 0;
188214026Skib	return (error);
189214026Skib}
190214026Skib
191212506Skibvoid
192212506Skibnfs_nfsiodnew_tq(__unused void *arg, int pending)
193212506Skib{
194212506Skib
195212506Skib	mtx_lock(&nfs_iod_mtx);
196214026Skib	while (pending > 0) {
197214026Skib		pending--;
198214026Skib		nfs_nfsiodnew_sync();
199212506Skib	}
200212506Skib	mtx_unlock(&nfs_iod_mtx);
201212506Skib}
202212506Skib
203214026Skibvoid
204214026Skibnfs_nfsiodnew(void)
20589324Speter{
20689324Speter
207214026Skib	mtx_assert(&nfs_iod_mtx, MA_OWNED);
208212506Skib	taskqueue_enqueue(taskqueue_thread, &nfs_nfsiodnew_task);
20989324Speter}
21089324Speter
21183651Speterstatic void
21283651Speternfsiod_setup(void *dummy)
2131541Srgrimes{
2141541Srgrimes	int error;
2151541Srgrimes
216221973Srmacklem	TUNABLE_INT_FETCH("vfs.oldnfs.iodmin", &nfs_iodmin);
217158739Smohans	mtx_lock(&nfs_iod_mtx);
21889324Speter	/* Silently limit the start number of nfsiod's */
21989324Speter	if (nfs_iodmin > NFS_MAXASYNCDAEMON)
22089324Speter		nfs_iodmin = NFS_MAXASYNCDAEMON;
22189324Speter
222214026Skib	while (nfs_numasync < nfs_iodmin) {
223214026Skib		error = nfs_nfsiodnew_sync();
22489324Speter		if (error == -1)
22589324Speter			panic("nfsiod_setup: nfs_nfsiodnew failed");
2261541Srgrimes	}
227158739Smohans	mtx_unlock(&nfs_iod_mtx);
2281541Srgrimes}
22983651SpeterSYSINIT(nfsiod, SI_SUB_KTHREAD_IDLE, SI_ORDER_ANY, nfsiod_setup, NULL);
2301541Srgrimes
23183651Speterstatic int nfs_defect = 0;
232221973SrmacklemSYSCTL_INT(_vfs_oldnfs, OID_AUTO, defect, CTLFLAG_RW, &nfs_defect, 0,
233184561Strhodes    "Allow nfsiods to migrate serving different mounts");
2341541Srgrimes
23544246Speter/*
2361541Srgrimes * Asynchronous I/O daemons for client nfs.
2371541Srgrimes * They do read-ahead and write-behind operations on the block I/O cache.
23889324Speter * Returns if we hit the timeout defined by the iodmaxidle sysctl.
2391541Srgrimes */
24083651Speterstatic void
24189324Speternfssvc_iod(void *instance)
2421541Srgrimes{
24383651Speter	struct buf *bp;
24419449Sdfr	struct nfsmount *nmp;
24589324Speter	int myiod, timo;
24631016Sphk	int error = 0;
2471541Srgrimes
248158739Smohans	mtx_lock(&nfs_iod_mtx);
24989324Speter	myiod = (int *)instance - nfs_asyncdaemon;
2501541Srgrimes	/*
25189324Speter	 * Main loop
2521541Srgrimes	 */
2531541Srgrimes	for (;;) {
254172324Smohans	    while (((nmp = nfs_iodmount[myiod]) == NULL)
255172324Smohans		   || !TAILQ_FIRST(&nmp->nm_bufq)) {
25689407Speter		if (myiod >= nfs_iodmax)
25789407Speter			goto finish;
25819449Sdfr		if (nmp)
25989324Speter			nmp->nm_bufqiods--;
260203072Srmacklem		if (nfs_iodwant[myiod] == NFSIOD_NOT_AVAILABLE)
261203072Srmacklem			nfs_iodwant[myiod] = NFSIOD_AVAILABLE;
26219449Sdfr		nfs_iodmount[myiod] = NULL;
26389324Speter		/*
26489324Speter		 * Always keep at least nfs_iodmin kthreads.
26589324Speter		 */
26689324Speter		timo = (myiod < nfs_iodmin) ? 0 : nfs_iodmaxidle * hz;
267158739Smohans		error = msleep(&nfs_iodwant[myiod], &nfs_iod_mtx, PWAIT | PCATCH,
268117152Sphk		    "-", timo);
269172324Smohans		if (error) {
270172324Smohans			nmp = nfs_iodmount[myiod];
271172324Smohans			/*
272172324Smohans			 * Rechecking the nm_bufq closes a rare race where the
273172324Smohans			 * nfsiod is woken up at the exact time the idle timeout
274172324Smohans			 * fires
275172324Smohans			 */
276172324Smohans			if (nmp && TAILQ_FIRST(&nmp->nm_bufq))
277172324Smohans				error = 0;
278172324Smohans			break;
279172324Smohans		}
2809336Sdfr	    }
28189324Speter	    if (error)
28289324Speter		    break;
28383651Speter	    while ((bp = TAILQ_FIRST(&nmp->nm_bufq)) != NULL) {
284158905Smohans	        int giant_locked = 0;
285158905Smohans
2869336Sdfr		/* Take one off the front of the list */
28719449Sdfr		TAILQ_REMOVE(&nmp->nm_bufq, bp, b_freelist);
28819449Sdfr		nmp->nm_bufqlen--;
28955431Sdillon		if (nmp->nm_bufqwant && nmp->nm_bufqlen <= nfs_numasync) {
29089324Speter		    nmp->nm_bufqwant = 0;
29119449Sdfr		    wakeup(&nmp->nm_bufq);
29219449Sdfr		}
293158739Smohans		mtx_unlock(&nfs_iod_mtx);
294158905Smohans		if (NFS_ISV4(bp->b_vp)) {
295158905Smohans			giant_locked = 1;
296158905Smohans			mtx_lock(&Giant);
297158905Smohans		}
298138899Sps		if (bp->b_flags & B_DIRECT) {
299138899Sps			KASSERT((bp->b_iocmd == BIO_WRITE), ("nfscvs_iod: BIO_WRITE not set"));
300138899Sps			(void)nfs_doio_directwrite(bp);
301138899Sps		} else {
302138899Sps			if (bp->b_iocmd == BIO_READ)
303138899Sps				(void) nfs_doio(bp->b_vp, bp, bp->b_rcred, NULL);
304138899Sps			else
305138899Sps				(void) nfs_doio(bp->b_vp, bp, bp->b_wcred, NULL);
306138899Sps		}
307158905Smohans		if (giant_locked)
308158905Smohans			mtx_unlock(&Giant);
309158739Smohans		mtx_lock(&nfs_iod_mtx);
31019449Sdfr		/*
311249630Srmacklem		 * Make sure the nmp hasn't been dismounted as soon as
312249630Srmacklem		 * nfs_doio() completes for the last buffer.
313249630Srmacklem		 */
314249630Srmacklem		nmp = nfs_iodmount[myiod];
315249630Srmacklem		if (nmp == NULL)
316249630Srmacklem			break;
317249630Srmacklem
318249630Srmacklem		/*
31919449Sdfr		 * If there are more than one iod on this mount, then defect
32019449Sdfr		 * so that the iods can be shared out fairly between the mounts
32119449Sdfr		 */
32219449Sdfr		if (nfs_defect && nmp->nm_bufqiods > 1) {
32319449Sdfr		    NFS_DPF(ASYNCIO,
32419449Sdfr			    ("nfssvc_iod: iod %d defecting from mount %p\n",
32519449Sdfr			     myiod, nmp));
32619449Sdfr		    nfs_iodmount[myiod] = NULL;
32719449Sdfr		    nmp->nm_bufqiods--;
32819449Sdfr		    break;
32919449Sdfr		}
3309336Sdfr	    }
3311541Srgrimes	}
33289407Speterfinish:
33389324Speter	nfs_asyncdaemon[myiod] = 0;
33489324Speter	if (nmp)
33589324Speter	    nmp->nm_bufqiods--;
336203072Srmacklem	nfs_iodwant[myiod] = NFSIOD_NOT_AVAILABLE;
33789324Speter	nfs_iodmount[myiod] = NULL;
338128111Speadar	/* Someone may be waiting for the last nfsiod to terminate. */
339128111Speadar	if (--nfs_numasync == 0)
340128111Speadar		wakeup(&nfs_numasync);
341158739Smohans	mtx_unlock(&nfs_iod_mtx);
34289407Speter	if ((error == 0) || (error == EWOULDBLOCK))
343172836Sjulian		kproc_exit(0);
34489324Speter	/* Abnormal termination */
345172836Sjulian	kproc_exit(1);
3461541Srgrimes}
347