xref: /netbsd-src/sys/fs/nfs/client/nfs_clnfsiod.c (revision 3be2222bbe56192eb75b39905cef98ac451a3d70)
1*3be2222bSpgoyette /*	$NetBSD: nfs_clnfsiod.c,v 1.2 2016/12/13 22:17:33 pgoyette Exp $	*/
26ca35587Sdholland /*-
36ca35587Sdholland  * Copyright (c) 1989, 1993
46ca35587Sdholland  *	The Regents of the University of California.  All rights reserved.
56ca35587Sdholland  *
66ca35587Sdholland  * This code is derived from software contributed to Berkeley by
76ca35587Sdholland  * Rick Macklem at The University of Guelph.
86ca35587Sdholland  *
96ca35587Sdholland  * Redistribution and use in source and binary forms, with or without
106ca35587Sdholland  * modification, are permitted provided that the following conditions
116ca35587Sdholland  * are met:
126ca35587Sdholland  * 1. Redistributions of source code must retain the above copyright
136ca35587Sdholland  *    notice, this list of conditions and the following disclaimer.
146ca35587Sdholland  * 2. Redistributions in binary form must reproduce the above copyright
156ca35587Sdholland  *    notice, this list of conditions and the following disclaimer in the
166ca35587Sdholland  *    documentation and/or other materials provided with the distribution.
176ca35587Sdholland  * 4. Neither the name of the University nor the names of its contributors
186ca35587Sdholland  *    may be used to endorse or promote products derived from this software
196ca35587Sdholland  *    without specific prior written permission.
206ca35587Sdholland  *
216ca35587Sdholland  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
226ca35587Sdholland  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
236ca35587Sdholland  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
246ca35587Sdholland  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
256ca35587Sdholland  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
266ca35587Sdholland  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
276ca35587Sdholland  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
286ca35587Sdholland  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
296ca35587Sdholland  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
306ca35587Sdholland  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
316ca35587Sdholland  * SUCH DAMAGE.
326ca35587Sdholland  *
336ca35587Sdholland  *	from nfs_syscalls.c	8.5 (Berkeley) 3/30/95
346ca35587Sdholland  */
356ca35587Sdholland 
366ca35587Sdholland #include <sys/cdefs.h>
376ca35587Sdholland /* __FBSDID("FreeBSD: head/sys/fs/nfsclient/nfs_clnfsiod.c 249630 2013-04-18 23:20:16Z rmacklem "); */
38*3be2222bSpgoyette __RCSID("$NetBSD: nfs_clnfsiod.c,v 1.2 2016/12/13 22:17:33 pgoyette Exp $");
396ca35587Sdholland 
406ca35587Sdholland #include <sys/param.h>
416ca35587Sdholland #include <sys/systm.h>
426ca35587Sdholland #include <sys/sysproto.h>
436ca35587Sdholland #include <sys/kernel.h>
446ca35587Sdholland #include <sys/sysctl.h>
456ca35587Sdholland #include <sys/file.h>
466ca35587Sdholland #include <sys/filedesc.h>
476ca35587Sdholland #include <sys/vnode.h>
486ca35587Sdholland #include <sys/malloc.h>
496ca35587Sdholland #include <sys/mount.h>
506ca35587Sdholland #include <sys/proc.h>
516ca35587Sdholland #include <sys/bio.h>
526ca35587Sdholland #include <sys/buf.h>
536ca35587Sdholland #include <sys/mbuf.h>
546ca35587Sdholland #include <sys/socket.h>
556ca35587Sdholland #include <sys/socketvar.h>
566ca35587Sdholland #include <sys/domain.h>
576ca35587Sdholland #include <sys/protosw.h>
586ca35587Sdholland #include <sys/namei.h>
596ca35587Sdholland #include <sys/unistd.h>
606ca35587Sdholland #include <sys/kthread.h>
616ca35587Sdholland #include <sys/fcntl.h>
626ca35587Sdholland #include <sys/lockf.h>
636ca35587Sdholland #include <sys/mutex.h>
646ca35587Sdholland #include <sys/taskqueue.h>
656ca35587Sdholland 
666ca35587Sdholland #include <netinet/in.h>
676ca35587Sdholland #include <netinet/tcp.h>
686ca35587Sdholland 
69*3be2222bSpgoyette #include <fs/nfs/common/nfsport.h>
70*3be2222bSpgoyette #include <fs/nfs/client/nfsmount.h>
71*3be2222bSpgoyette #include <fs/nfs/client/nfs.h>
72*3be2222bSpgoyette #include <fs/nfs/client/nfsnode.h>
736ca35587Sdholland 
746ca35587Sdholland extern struct mtx	ncl_iod_mutex;
756ca35587Sdholland extern struct task	ncl_nfsiodnew_task;
766ca35587Sdholland 
776ca35587Sdholland int ncl_numasync;
786ca35587Sdholland enum nfsiod_state ncl_iodwant[NFS_MAXASYNCDAEMON];
796ca35587Sdholland struct nfsmount *ncl_iodmount[NFS_MAXASYNCDAEMON];
806ca35587Sdholland 
816ca35587Sdholland static void	nfssvc_iod(void *);
826ca35587Sdholland 
836ca35587Sdholland static int nfs_asyncdaemon[NFS_MAXASYNCDAEMON];
846ca35587Sdholland 
856ca35587Sdholland SYSCTL_DECL(_vfs_nfs);
866ca35587Sdholland 
876ca35587Sdholland /* Maximum number of seconds a nfsiod kthread will sleep before exiting */
886ca35587Sdholland static unsigned int nfs_iodmaxidle = 120;
896ca35587Sdholland SYSCTL_UINT(_vfs_nfs, OID_AUTO, iodmaxidle, CTLFLAG_RW, &nfs_iodmaxidle, 0,
906ca35587Sdholland     "Max number of seconds an nfsiod kthread will sleep before exiting");
916ca35587Sdholland 
926ca35587Sdholland /* Maximum number of nfsiod kthreads */
936ca35587Sdholland unsigned int ncl_iodmax = 20;
946ca35587Sdholland 
956ca35587Sdholland /* Minimum number of nfsiod kthreads to keep as spares */
966ca35587Sdholland static unsigned int nfs_iodmin = 0;
976ca35587Sdholland 
986ca35587Sdholland static int nfs_nfsiodnew_sync(void);
996ca35587Sdholland 
1006ca35587Sdholland static int
sysctl_iodmin(SYSCTL_HANDLER_ARGS)1016ca35587Sdholland sysctl_iodmin(SYSCTL_HANDLER_ARGS)
1026ca35587Sdholland {
1036ca35587Sdholland 	int error, i;
1046ca35587Sdholland 	int newmin;
1056ca35587Sdholland 
1066ca35587Sdholland 	newmin = nfs_iodmin;
1076ca35587Sdholland 	error = sysctl_handle_int(oidp, &newmin, 0, req);
1086ca35587Sdholland 	if (error || (req->newptr == NULL))
1096ca35587Sdholland 		return (error);
1106ca35587Sdholland 	mtx_lock(&ncl_iod_mutex);
1116ca35587Sdholland 	if (newmin > ncl_iodmax) {
1126ca35587Sdholland 		error = EINVAL;
1136ca35587Sdholland 		goto out;
1146ca35587Sdholland 	}
1156ca35587Sdholland 	nfs_iodmin = newmin;
1166ca35587Sdholland 	if (ncl_numasync >= nfs_iodmin)
1176ca35587Sdholland 		goto out;
1186ca35587Sdholland 	/*
1196ca35587Sdholland 	 * If the current number of nfsiod is lower
1206ca35587Sdholland 	 * than the new minimum, create some more.
1216ca35587Sdholland 	 */
1226ca35587Sdholland 	for (i = nfs_iodmin - ncl_numasync; i > 0; i--)
1236ca35587Sdholland 		nfs_nfsiodnew_sync();
1246ca35587Sdholland out:
1256ca35587Sdholland 	mtx_unlock(&ncl_iod_mutex);
1266ca35587Sdholland 	return (0);
1276ca35587Sdholland }
1286ca35587Sdholland SYSCTL_PROC(_vfs_nfs, OID_AUTO, iodmin, CTLTYPE_UINT | CTLFLAG_RW, 0,
1296ca35587Sdholland     sizeof (nfs_iodmin), sysctl_iodmin, "IU",
1306ca35587Sdholland     "Min number of nfsiod kthreads to keep as spares");
1316ca35587Sdholland 
1326ca35587Sdholland static int
sysctl_iodmax(SYSCTL_HANDLER_ARGS)1336ca35587Sdholland sysctl_iodmax(SYSCTL_HANDLER_ARGS)
1346ca35587Sdholland {
1356ca35587Sdholland 	int error, i;
1366ca35587Sdholland 	int iod, newmax;
1376ca35587Sdholland 
1386ca35587Sdholland 	newmax = ncl_iodmax;
1396ca35587Sdholland 	error = sysctl_handle_int(oidp, &newmax, 0, req);
1406ca35587Sdholland 	if (error || (req->newptr == NULL))
1416ca35587Sdholland 		return (error);
1426ca35587Sdholland 	if (newmax > NFS_MAXASYNCDAEMON)
1436ca35587Sdholland 		return (EINVAL);
1446ca35587Sdholland 	mtx_lock(&ncl_iod_mutex);
1456ca35587Sdholland 	ncl_iodmax = newmax;
1466ca35587Sdholland 	if (ncl_numasync <= ncl_iodmax)
1476ca35587Sdholland 		goto out;
1486ca35587Sdholland 	/*
1496ca35587Sdholland 	 * If there are some asleep nfsiods that should
1506ca35587Sdholland 	 * exit, wakeup() them so that they check ncl_iodmax
1516ca35587Sdholland 	 * and exit.  Those who are active will exit as
1526ca35587Sdholland 	 * soon as they finish I/O.
1536ca35587Sdholland 	 */
1546ca35587Sdholland 	iod = ncl_numasync - 1;
1556ca35587Sdholland 	for (i = 0; i < ncl_numasync - ncl_iodmax; i++) {
1566ca35587Sdholland 		if (ncl_iodwant[iod] == NFSIOD_AVAILABLE)
1576ca35587Sdholland 			wakeup(&ncl_iodwant[iod]);
1586ca35587Sdholland 		iod--;
1596ca35587Sdholland 	}
1606ca35587Sdholland out:
1616ca35587Sdholland 	mtx_unlock(&ncl_iod_mutex);
1626ca35587Sdholland 	return (0);
1636ca35587Sdholland }
1646ca35587Sdholland SYSCTL_PROC(_vfs_nfs, OID_AUTO, iodmax, CTLTYPE_UINT | CTLFLAG_RW, 0,
1656ca35587Sdholland     sizeof (ncl_iodmax), sysctl_iodmax, "IU",
1666ca35587Sdholland     "Max number of nfsiod kthreads");
1676ca35587Sdholland 
1686ca35587Sdholland static int
nfs_nfsiodnew_sync(void)1696ca35587Sdholland nfs_nfsiodnew_sync(void)
1706ca35587Sdholland {
1716ca35587Sdholland 	int error, i;
1726ca35587Sdholland 
1736ca35587Sdholland 	mtx_assert(&ncl_iod_mutex, MA_OWNED);
1746ca35587Sdholland 	for (i = 0; i < ncl_iodmax; i++) {
1756ca35587Sdholland 		if (nfs_asyncdaemon[i] == 0) {
1766ca35587Sdholland 			nfs_asyncdaemon[i] = 1;
1776ca35587Sdholland 			break;
1786ca35587Sdholland 		}
1796ca35587Sdholland 	}
1806ca35587Sdholland 	if (i == ncl_iodmax)
1816ca35587Sdholland 		return (0);
1826ca35587Sdholland 	mtx_unlock(&ncl_iod_mutex);
1836ca35587Sdholland 	error = kproc_create(nfssvc_iod, nfs_asyncdaemon + i, NULL,
1846ca35587Sdholland 	    RFHIGHPID, 0, "newnfs %d", i);
1856ca35587Sdholland 	mtx_lock(&ncl_iod_mutex);
1866ca35587Sdholland 	if (error == 0) {
1876ca35587Sdholland 		ncl_numasync++;
1886ca35587Sdholland 		ncl_iodwant[i] = NFSIOD_AVAILABLE;
1896ca35587Sdholland 	} else
1906ca35587Sdholland 		nfs_asyncdaemon[i] = 0;
1916ca35587Sdholland 	return (error);
1926ca35587Sdholland }
1936ca35587Sdholland 
1946ca35587Sdholland void
ncl_nfsiodnew_tq(__unused void * arg,int pending)1956ca35587Sdholland ncl_nfsiodnew_tq(__unused void *arg, int pending)
1966ca35587Sdholland {
1976ca35587Sdholland 
1986ca35587Sdholland 	mtx_lock(&ncl_iod_mutex);
1996ca35587Sdholland 	while (pending > 0) {
2006ca35587Sdholland 		pending--;
2016ca35587Sdholland 		nfs_nfsiodnew_sync();
2026ca35587Sdholland 	}
2036ca35587Sdholland 	mtx_unlock(&ncl_iod_mutex);
2046ca35587Sdholland }
2056ca35587Sdholland 
2066ca35587Sdholland void
ncl_nfsiodnew(void)2076ca35587Sdholland ncl_nfsiodnew(void)
2086ca35587Sdholland {
2096ca35587Sdholland 
2106ca35587Sdholland 	mtx_assert(&ncl_iod_mutex, MA_OWNED);
2116ca35587Sdholland 	taskqueue_enqueue(taskqueue_thread, &ncl_nfsiodnew_task);
2126ca35587Sdholland }
2136ca35587Sdholland 
2146ca35587Sdholland static void
nfsiod_setup(void * dummy)2156ca35587Sdholland nfsiod_setup(void *dummy)
2166ca35587Sdholland {
2176ca35587Sdholland 	int error;
2186ca35587Sdholland 
2196ca35587Sdholland 	TUNABLE_INT_FETCH("vfs.nfs.iodmin", &nfs_iodmin);
2206ca35587Sdholland 	nfscl_init();
2216ca35587Sdholland 	mtx_lock(&ncl_iod_mutex);
2226ca35587Sdholland 	/* Silently limit the start number of nfsiod's */
2236ca35587Sdholland 	if (nfs_iodmin > NFS_MAXASYNCDAEMON)
2246ca35587Sdholland 		nfs_iodmin = NFS_MAXASYNCDAEMON;
2256ca35587Sdholland 
2266ca35587Sdholland 	while (ncl_numasync < nfs_iodmin) {
2276ca35587Sdholland 		error = nfs_nfsiodnew_sync();
2286ca35587Sdholland 		if (error == -1)
2296ca35587Sdholland 			panic("nfsiod_setup: nfs_nfsiodnew failed");
2306ca35587Sdholland 	}
2316ca35587Sdholland 	mtx_unlock(&ncl_iod_mutex);
2326ca35587Sdholland }
2336ca35587Sdholland SYSINIT(newnfsiod, SI_SUB_KTHREAD_IDLE, SI_ORDER_ANY, nfsiod_setup, NULL);
2346ca35587Sdholland 
2356ca35587Sdholland static int nfs_defect = 0;
2366ca35587Sdholland SYSCTL_INT(_vfs_nfs, OID_AUTO, defect, CTLFLAG_RW, &nfs_defect, 0,
2376ca35587Sdholland     "Allow nfsiods to migrate serving different mounts");
2386ca35587Sdholland 
2396ca35587Sdholland /*
2406ca35587Sdholland  * Asynchronous I/O daemons for client nfs.
2416ca35587Sdholland  * They do read-ahead and write-behind operations on the block I/O cache.
2426ca35587Sdholland  * Returns if we hit the timeout defined by the iodmaxidle sysctl.
2436ca35587Sdholland  */
2446ca35587Sdholland static void
nfssvc_iod(void * instance)2456ca35587Sdholland nfssvc_iod(void *instance)
2466ca35587Sdholland {
2476ca35587Sdholland 	struct buf *bp;
2486ca35587Sdholland 	struct nfsmount *nmp;
2496ca35587Sdholland 	int myiod, timo;
2506ca35587Sdholland 	int error = 0;
2516ca35587Sdholland 
2526ca35587Sdholland 	mtx_lock(&ncl_iod_mutex);
2536ca35587Sdholland 	myiod = (int *)instance - nfs_asyncdaemon;
2546ca35587Sdholland 	/*
2556ca35587Sdholland 	 * Main loop
2566ca35587Sdholland 	 */
2576ca35587Sdholland 	for (;;) {
2586ca35587Sdholland 	    while (((nmp = ncl_iodmount[myiod]) == NULL)
2596ca35587Sdholland 		   || !TAILQ_FIRST(&nmp->nm_bufq)) {
2606ca35587Sdholland 		if (myiod >= ncl_iodmax)
2616ca35587Sdholland 			goto finish;
2626ca35587Sdholland 		if (nmp)
2636ca35587Sdholland 			nmp->nm_bufqiods--;
2646ca35587Sdholland 		if (ncl_iodwant[myiod] == NFSIOD_NOT_AVAILABLE)
2656ca35587Sdholland 			ncl_iodwant[myiod] = NFSIOD_AVAILABLE;
2666ca35587Sdholland 		ncl_iodmount[myiod] = NULL;
2676ca35587Sdholland 		/*
2686ca35587Sdholland 		 * Always keep at least nfs_iodmin kthreads.
2696ca35587Sdholland 		 */
2706ca35587Sdholland 		timo = (myiod < nfs_iodmin) ? 0 : nfs_iodmaxidle * hz;
2716ca35587Sdholland 		error = msleep(&ncl_iodwant[myiod], &ncl_iod_mutex, PWAIT | PCATCH,
2726ca35587Sdholland 		    "-", timo);
2736ca35587Sdholland 		if (error) {
2746ca35587Sdholland 			nmp = ncl_iodmount[myiod];
2756ca35587Sdholland 			/*
2766ca35587Sdholland 			 * Rechecking the nm_bufq closes a rare race where the
2776ca35587Sdholland 			 * nfsiod is woken up at the exact time the idle timeout
2786ca35587Sdholland 			 * fires
2796ca35587Sdholland 			 */
2806ca35587Sdholland 			if (nmp && TAILQ_FIRST(&nmp->nm_bufq))
2816ca35587Sdholland 				error = 0;
2826ca35587Sdholland 			break;
2836ca35587Sdholland 		}
2846ca35587Sdholland 	    }
2856ca35587Sdholland 	    if (error)
2866ca35587Sdholland 		    break;
2876ca35587Sdholland 	    while ((bp = TAILQ_FIRST(&nmp->nm_bufq)) != NULL) {
2886ca35587Sdholland 		/* Take one off the front of the list */
2896ca35587Sdholland 		TAILQ_REMOVE(&nmp->nm_bufq, bp, b_freelist);
2906ca35587Sdholland 		nmp->nm_bufqlen--;
2916ca35587Sdholland 		if (nmp->nm_bufqwant && nmp->nm_bufqlen <= ncl_numasync) {
2926ca35587Sdholland 		    nmp->nm_bufqwant = 0;
2936ca35587Sdholland 		    wakeup(&nmp->nm_bufq);
2946ca35587Sdholland 		}
2956ca35587Sdholland 		mtx_unlock(&ncl_iod_mutex);
2966ca35587Sdholland 		if (bp->b_flags & B_DIRECT) {
2976ca35587Sdholland 			KASSERT((bp->b_iocmd == BIO_WRITE), ("nfscvs_iod: BIO_WRITE not set"));
2986ca35587Sdholland 			(void)ncl_doio_directwrite(bp);
2996ca35587Sdholland 		} else {
3006ca35587Sdholland 			if (bp->b_iocmd == BIO_READ)
3016ca35587Sdholland 				(void) ncl_doio(bp->b_vp, bp, bp->b_rcred,
3026ca35587Sdholland 				    NULL, 0);
3036ca35587Sdholland 			else
3046ca35587Sdholland 				(void) ncl_doio(bp->b_vp, bp, bp->b_wcred,
3056ca35587Sdholland 				    NULL, 0);
3066ca35587Sdholland 		}
3076ca35587Sdholland 		mtx_lock(&ncl_iod_mutex);
3086ca35587Sdholland 		/*
3096ca35587Sdholland 		 * Make sure the nmp hasn't been dismounted as soon as
3106ca35587Sdholland 		 * ncl_doio() completes for the last buffer.
3116ca35587Sdholland 		 */
3126ca35587Sdholland 		nmp = ncl_iodmount[myiod];
3136ca35587Sdholland 		if (nmp == NULL)
3146ca35587Sdholland 			break;
3156ca35587Sdholland 
3166ca35587Sdholland 		/*
3176ca35587Sdholland 		 * If there are more than one iod on this mount, then defect
3186ca35587Sdholland 		 * so that the iods can be shared out fairly between the mounts
3196ca35587Sdholland 		 */
3206ca35587Sdholland 		if (nfs_defect && nmp->nm_bufqiods > 1) {
3216ca35587Sdholland 		    NFS_DPF(ASYNCIO,
3226ca35587Sdholland 			    ("nfssvc_iod: iod %d defecting from mount %p\n",
3236ca35587Sdholland 			     myiod, nmp));
3246ca35587Sdholland 		    ncl_iodmount[myiod] = NULL;
3256ca35587Sdholland 		    nmp->nm_bufqiods--;
3266ca35587Sdholland 		    break;
3276ca35587Sdholland 		}
3286ca35587Sdholland 	    }
3296ca35587Sdholland 	}
3306ca35587Sdholland finish:
3316ca35587Sdholland 	nfs_asyncdaemon[myiod] = 0;
3326ca35587Sdholland 	if (nmp)
3336ca35587Sdholland 	    nmp->nm_bufqiods--;
3346ca35587Sdholland 	ncl_iodwant[myiod] = NFSIOD_NOT_AVAILABLE;
3356ca35587Sdholland 	ncl_iodmount[myiod] = NULL;
3366ca35587Sdholland 	/* Someone may be waiting for the last nfsiod to terminate. */
3376ca35587Sdholland 	if (--ncl_numasync == 0)
3386ca35587Sdholland 		wakeup(&ncl_numasync);
3396ca35587Sdholland 	mtx_unlock(&ncl_iod_mutex);
3406ca35587Sdholland 	if ((error == 0) || (error == EWOULDBLOCK))
3416ca35587Sdholland 		kproc_exit(0);
3426ca35587Sdholland 	/* Abnormal termination */
3436ca35587Sdholland 	kproc_exit(1);
3446ca35587Sdholland }
345