xref: /netbsd-src/sys/fs/nfs/common/nfs_fha.c (revision 88b1d6a671d2140f122be1da1cac398023579045)
1*88b1d6a6Spgoyette /*	$NetBSD: nfs_fha.c,v 1.2 2016/12/13 22:41:46 pgoyette Exp $	*/
26ca35587Sdholland /*-
36ca35587Sdholland  * Copyright (c) 2008 Isilon Inc http://www.isilon.com/
46ca35587Sdholland  *
56ca35587Sdholland  * Redistribution and use in source and binary forms, with or without
66ca35587Sdholland  * modification, are permitted provided that the following conditions
76ca35587Sdholland  * are met:
86ca35587Sdholland  * 1. Redistributions of source code must retain the above copyright
96ca35587Sdholland  *    notice, this list of conditions and the following disclaimer.
106ca35587Sdholland  * 2. Redistributions in binary form must reproduce the above copyright
116ca35587Sdholland  *    notice, this list of conditions and the following disclaimer in the
126ca35587Sdholland  *    documentation and/or other materials provided with the distribution.
136ca35587Sdholland  *
146ca35587Sdholland  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
156ca35587Sdholland  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
166ca35587Sdholland  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
176ca35587Sdholland  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
186ca35587Sdholland  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
196ca35587Sdholland  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
206ca35587Sdholland  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
216ca35587Sdholland  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
226ca35587Sdholland  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
236ca35587Sdholland  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
246ca35587Sdholland  * SUCH DAMAGE.
256ca35587Sdholland  */
266ca35587Sdholland 
276ca35587Sdholland #include <sys/cdefs.h>
28e81f0ea2Spgoyette /* __FBSDID("FreeBSD: head/sys/nfs/nfs_fha.c 267479 2014-06-14 12:26:12Z mav "); */
29*88b1d6a6Spgoyette __RCSID("$NetBSD: nfs_fha.c,v 1.2 2016/12/13 22:41:46 pgoyette Exp $");
306ca35587Sdholland 
316ca35587Sdholland #include <sys/param.h>
326ca35587Sdholland #include <sys/systm.h>
336ca35587Sdholland #include <sys/sysproto.h>
346ca35587Sdholland #include <sys/kernel.h>
356ca35587Sdholland #include <sys/sysctl.h>
366ca35587Sdholland #include <sys/vnode.h>
376ca35587Sdholland #include <sys/malloc.h>
386ca35587Sdholland #include <sys/mount.h>
396ca35587Sdholland #include <sys/mbuf.h>
406ca35587Sdholland #include <sys/sbuf.h>
416ca35587Sdholland 
426ca35587Sdholland #include <rpc/rpc.h>
43*88b1d6a6Spgoyette #include <fs/nfs/common/nfs_fha.h>
446ca35587Sdholland 
456ca35587Sdholland static MALLOC_DEFINE(M_NFS_FHA, "NFS FHA", "NFS FHA");
466ca35587Sdholland 
476ca35587Sdholland /*
486ca35587Sdholland  * XXX need to commonize definitions between old and new NFS code.  Define
496ca35587Sdholland  * this here so we don't include one nfsproto.h over the other.
506ca35587Sdholland  */
516ca35587Sdholland #define	NFS_PROG		100003
526ca35587Sdholland 
536ca35587Sdholland void
fha_init(struct fha_params * softc)546ca35587Sdholland fha_init(struct fha_params *softc)
556ca35587Sdholland {
566ca35587Sdholland 	char tmpstr[128];
57e81f0ea2Spgoyette 	int i;
586ca35587Sdholland 
59e81f0ea2Spgoyette 	for (i = 0; i < FHA_HASH_SIZE; i++)
60e81f0ea2Spgoyette 		mtx_init(&softc->fha_hash[i].mtx, "fhalock", NULL, MTX_DEF);
616ca35587Sdholland 
626ca35587Sdholland 	/*
636ca35587Sdholland 	 * Set the default tuning parameters.
646ca35587Sdholland 	 */
656ca35587Sdholland 	softc->ctls.enable = FHA_DEF_ENABLE;
666ca35587Sdholland 	softc->ctls.bin_shift = FHA_DEF_BIN_SHIFT;
676ca35587Sdholland 	softc->ctls.max_nfsds_per_fh = FHA_DEF_MAX_NFSDS_PER_FH;
686ca35587Sdholland 	softc->ctls.max_reqs_per_nfsd = FHA_DEF_MAX_REQS_PER_NFSD;
696ca35587Sdholland 
706ca35587Sdholland 	/*
716ca35587Sdholland 	 * Allow the user to override the defaults at boot time with
726ca35587Sdholland 	 * tunables.
736ca35587Sdholland 	 */
746ca35587Sdholland 	snprintf(tmpstr, sizeof(tmpstr), "vfs.%s.fha.enable",
756ca35587Sdholland 	    softc->server_name);
766ca35587Sdholland 	TUNABLE_INT_FETCH(tmpstr, &softc->ctls.enable);
776ca35587Sdholland 	snprintf(tmpstr, sizeof(tmpstr), "vfs.%s.fha.bin_shift",
786ca35587Sdholland 	    softc->server_name);
796ca35587Sdholland 	TUNABLE_INT_FETCH(tmpstr, &softc->ctls.bin_shift);
806ca35587Sdholland 	snprintf(tmpstr, sizeof(tmpstr), "vfs.%s.fha.max_nfsds_per_fh",
816ca35587Sdholland 	    softc->server_name);
826ca35587Sdholland 	TUNABLE_INT_FETCH(tmpstr, &softc->ctls.max_nfsds_per_fh);
836ca35587Sdholland 	snprintf(tmpstr, sizeof(tmpstr), "vfs.%s.fha.max_reqs_per_nfsd",
846ca35587Sdholland 	    softc->server_name);
856ca35587Sdholland 	TUNABLE_INT_FETCH(tmpstr, &softc->ctls.max_reqs_per_nfsd);
866ca35587Sdholland 
876ca35587Sdholland 	/*
886ca35587Sdholland 	 * Add sysctls so the user can change the tuning parameters at
896ca35587Sdholland 	 * runtime.
906ca35587Sdholland 	 */
916ca35587Sdholland 	SYSCTL_ADD_UINT(&softc->sysctl_ctx, SYSCTL_CHILDREN(softc->sysctl_tree),
926ca35587Sdholland 	    OID_AUTO, "enable", CTLFLAG_RW,
936ca35587Sdholland 	    &softc->ctls.enable, 0, "Enable NFS File Handle Affinity (FHA)");
946ca35587Sdholland 
956ca35587Sdholland 	SYSCTL_ADD_UINT(&softc->sysctl_ctx, SYSCTL_CHILDREN(softc->sysctl_tree),
966ca35587Sdholland 	    OID_AUTO, "bin_shift", CTLFLAG_RW,
976ca35587Sdholland 	    &softc->ctls.bin_shift, 0, "For FHA reads, no two requests will "
986ca35587Sdholland 	    "contend if they're 2^(bin_shift) bytes apart");
996ca35587Sdholland 
1006ca35587Sdholland 	SYSCTL_ADD_UINT(&softc->sysctl_ctx, SYSCTL_CHILDREN(softc->sysctl_tree),
1016ca35587Sdholland 	    OID_AUTO, "max_nfsds_per_fh", CTLFLAG_RW,
1026ca35587Sdholland 	    &softc->ctls.max_nfsds_per_fh, 0, "Maximum nfsd threads that "
1036ca35587Sdholland 	    "should be working on requests for the same file handle");
1046ca35587Sdholland 
1056ca35587Sdholland 	SYSCTL_ADD_UINT(&softc->sysctl_ctx, SYSCTL_CHILDREN(softc->sysctl_tree),
1066ca35587Sdholland 	    OID_AUTO, "max_reqs_per_nfsd", CTLFLAG_RW,
1076ca35587Sdholland 	    &softc->ctls.max_reqs_per_nfsd, 0, "Maximum requests that "
1086ca35587Sdholland 	    "single nfsd thread should be working on at any time");
1096ca35587Sdholland 
1106ca35587Sdholland 	SYSCTL_ADD_OID(&softc->sysctl_ctx, SYSCTL_CHILDREN(softc->sysctl_tree),
1116ca35587Sdholland 	    OID_AUTO, "fhe_stats", CTLTYPE_STRING | CTLFLAG_RD, 0, 0,
1126ca35587Sdholland 	    softc->callbacks.fhe_stats_sysctl, "A", "");
1136ca35587Sdholland 
1146ca35587Sdholland }
1156ca35587Sdholland 
1166ca35587Sdholland void
fha_uninit(struct fha_params * softc)1176ca35587Sdholland fha_uninit(struct fha_params *softc)
1186ca35587Sdholland {
119e81f0ea2Spgoyette 	int i;
120e81f0ea2Spgoyette 
1216ca35587Sdholland 	sysctl_ctx_free(&softc->sysctl_ctx);
122e81f0ea2Spgoyette 	for (i = 0; i < FHA_HASH_SIZE; i++)
123e81f0ea2Spgoyette 		mtx_destroy(&softc->fha_hash[i].mtx);
1246ca35587Sdholland }
1256ca35587Sdholland 
1266ca35587Sdholland /*
1276ca35587Sdholland  * This just specifies that offsets should obey affinity when within
1286ca35587Sdholland  * the same 1Mbyte (1<<20) chunk for the file (reads only for now).
1296ca35587Sdholland  */
1306ca35587Sdholland static void
fha_extract_info(struct svc_req * req,struct fha_info * i,struct fha_callbacks * cb)1316ca35587Sdholland fha_extract_info(struct svc_req *req, struct fha_info *i,
1326ca35587Sdholland     struct fha_callbacks *cb)
1336ca35587Sdholland {
1346ca35587Sdholland 	struct mbuf *md;
1356ca35587Sdholland 	caddr_t dpos;
1366ca35587Sdholland 	static u_int64_t random_fh = 0;
1376ca35587Sdholland 	int error;
1386ca35587Sdholland 	int v3 = (req->rq_vers == 3);
1396ca35587Sdholland 	rpcproc_t procnum;
1406ca35587Sdholland 
1416ca35587Sdholland 	/*
1426ca35587Sdholland 	 * We start off with a random fh.  If we get a reasonable
1436ca35587Sdholland 	 * procnum, we set the fh.  If there's a concept of offset
1446ca35587Sdholland 	 * that we're interested in, we set that.
1456ca35587Sdholland 	 */
1466ca35587Sdholland 	i->fh = ++random_fh;
1476ca35587Sdholland 	i->offset = 0;
1486ca35587Sdholland 	i->locktype = LK_EXCLUSIVE;
1496ca35587Sdholland 
1506ca35587Sdholland 	/*
1516ca35587Sdholland 	 * Extract the procnum and convert to v3 form if necessary,
1526ca35587Sdholland 	 * taking care to deal with out-of-range procnums.  Caller will
1536ca35587Sdholland 	 * ensure that rq_vers is either 2 or 3.
1546ca35587Sdholland 	 */
1556ca35587Sdholland 	procnum = req->rq_proc;
1566ca35587Sdholland 	if (!v3) {
1576ca35587Sdholland 		rpcproc_t tmp_procnum;
1586ca35587Sdholland 
1596ca35587Sdholland 		tmp_procnum = cb->get_procnum(procnum);
1606ca35587Sdholland 		if (tmp_procnum == -1)
1616ca35587Sdholland 			goto out;
1626ca35587Sdholland 		procnum = tmp_procnum;
1636ca35587Sdholland 	}
1646ca35587Sdholland 
1656ca35587Sdholland 	/*
1666ca35587Sdholland 	 * We do affinity for most.  However, we divide a realm of affinity
1676ca35587Sdholland 	 * by file offset so as to allow for concurrent random access.  We
1686ca35587Sdholland 	 * only do this for reads today, but this may change when IFS supports
1696ca35587Sdholland 	 * efficient concurrent writes.
1706ca35587Sdholland 	 */
1716ca35587Sdholland 	if (cb->no_offset(procnum))
1726ca35587Sdholland 		goto out;
1736ca35587Sdholland 
1746ca35587Sdholland 	error = cb->realign(&req->rq_args, M_NOWAIT);
1756ca35587Sdholland 	if (error)
1766ca35587Sdholland 		goto out;
1776ca35587Sdholland 	md = req->rq_args;
1786ca35587Sdholland 	dpos = mtod(md, caddr_t);
1796ca35587Sdholland 
1806ca35587Sdholland 	/* Grab the filehandle. */
181e81f0ea2Spgoyette 	error = cb->get_fh(&i->fh, v3, &md, &dpos);
1826ca35587Sdholland 	if (error)
1836ca35587Sdholland 		goto out;
1846ca35587Sdholland 
1856ca35587Sdholland 	/* Content ourselves with zero offset for all but reads. */
1866ca35587Sdholland 	if (cb->is_read(procnum) || cb->is_write(procnum))
1876ca35587Sdholland 		cb->get_offset(&md, &dpos, v3, i);
1886ca35587Sdholland 
1896ca35587Sdholland out:
1906ca35587Sdholland 	cb->set_locktype(procnum, i);
1916ca35587Sdholland }
1926ca35587Sdholland 
1936ca35587Sdholland static struct fha_hash_entry *
fha_hash_entry_new(u_int64_t fh)1946ca35587Sdholland fha_hash_entry_new(u_int64_t fh)
1956ca35587Sdholland {
1966ca35587Sdholland 	struct fha_hash_entry *e;
1976ca35587Sdholland 
1986ca35587Sdholland 	e = malloc(sizeof(*e), M_NFS_FHA, M_WAITOK);
1996ca35587Sdholland 	e->fh = fh;
2006ca35587Sdholland 	e->num_rw = 0;
2016ca35587Sdholland 	e->num_exclusive = 0;
2026ca35587Sdholland 	e->num_threads = 0;
2036ca35587Sdholland 	LIST_INIT(&e->threads);
2046ca35587Sdholland 
2056ca35587Sdholland 	return (e);
2066ca35587Sdholland }
2076ca35587Sdholland 
2086ca35587Sdholland static void
fha_hash_entry_destroy(struct fha_hash_entry * e)2096ca35587Sdholland fha_hash_entry_destroy(struct fha_hash_entry *e)
2106ca35587Sdholland {
2116ca35587Sdholland 
212e81f0ea2Spgoyette 	mtx_assert(e->mtx, MA_OWNED);
213e81f0ea2Spgoyette 	KASSERT(e->num_rw == 0,
214e81f0ea2Spgoyette 	    ("%d reqs on destroyed fhe %p", e->num_rw, e));
215e81f0ea2Spgoyette 	KASSERT(e->num_exclusive == 0,
216e81f0ea2Spgoyette 	    ("%d exclusive reqs on destroyed fhe %p", e->num_exclusive, e));
217e81f0ea2Spgoyette 	KASSERT(e->num_threads == 0,
218e81f0ea2Spgoyette 	    ("%d threads on destroyed fhe %p", e->num_threads, e));
2196ca35587Sdholland 	free(e, M_NFS_FHA);
2206ca35587Sdholland }
2216ca35587Sdholland 
2226ca35587Sdholland static void
fha_hash_entry_remove(struct fha_hash_entry * e)2236ca35587Sdholland fha_hash_entry_remove(struct fha_hash_entry *e)
2246ca35587Sdholland {
2256ca35587Sdholland 
226e81f0ea2Spgoyette 	mtx_assert(e->mtx, MA_OWNED);
2276ca35587Sdholland 	LIST_REMOVE(e, link);
2286ca35587Sdholland 	fha_hash_entry_destroy(e);
2296ca35587Sdholland }
2306ca35587Sdholland 
2316ca35587Sdholland static struct fha_hash_entry *
fha_hash_entry_lookup(struct fha_params * softc,u_int64_t fh)2326ca35587Sdholland fha_hash_entry_lookup(struct fha_params *softc, u_int64_t fh)
2336ca35587Sdholland {
2346ca35587Sdholland 	SVCPOOL *pool;
235e81f0ea2Spgoyette 	struct fha_hash_slot *fhs;
2366ca35587Sdholland 	struct fha_hash_entry *fhe, *new_fhe;
2376ca35587Sdholland 
238e81f0ea2Spgoyette 	pool = *softc->pool;
239e81f0ea2Spgoyette 	fhs = &softc->fha_hash[fh % FHA_HASH_SIZE];
2406ca35587Sdholland 	new_fhe = fha_hash_entry_new(fh);
241e81f0ea2Spgoyette 	new_fhe->mtx = &fhs->mtx;
242e81f0ea2Spgoyette 	mtx_lock(&fhs->mtx);
243e81f0ea2Spgoyette 	LIST_FOREACH(fhe, &fhs->list, link)
2446ca35587Sdholland 		if (fhe->fh == fh)
2456ca35587Sdholland 			break;
2466ca35587Sdholland 	if (!fhe) {
2476ca35587Sdholland 		fhe = new_fhe;
248e81f0ea2Spgoyette 		LIST_INSERT_HEAD(&fhs->list, fhe, link);
2496ca35587Sdholland 	} else
2506ca35587Sdholland 		fha_hash_entry_destroy(new_fhe);
2516ca35587Sdholland 	return (fhe);
2526ca35587Sdholland }
2536ca35587Sdholland 
2546ca35587Sdholland static void
fha_hash_entry_add_thread(struct fha_hash_entry * fhe,SVCTHREAD * thread)2556ca35587Sdholland fha_hash_entry_add_thread(struct fha_hash_entry *fhe, SVCTHREAD *thread)
2566ca35587Sdholland {
2576ca35587Sdholland 
258e81f0ea2Spgoyette 	mtx_assert(fhe->mtx, MA_OWNED);
259e81f0ea2Spgoyette 	thread->st_p2 = 0;
2606ca35587Sdholland 	LIST_INSERT_HEAD(&fhe->threads, thread, st_alink);
2616ca35587Sdholland 	fhe->num_threads++;
2626ca35587Sdholland }
2636ca35587Sdholland 
2646ca35587Sdholland static void
fha_hash_entry_remove_thread(struct fha_hash_entry * fhe,SVCTHREAD * thread)2656ca35587Sdholland fha_hash_entry_remove_thread(struct fha_hash_entry *fhe, SVCTHREAD *thread)
2666ca35587Sdholland {
2676ca35587Sdholland 
268e81f0ea2Spgoyette 	mtx_assert(fhe->mtx, MA_OWNED);
269e81f0ea2Spgoyette 	KASSERT(thread->st_p2 == 0,
270e81f0ea2Spgoyette 	    ("%d reqs on removed thread %p", thread->st_p2, thread));
2716ca35587Sdholland 	LIST_REMOVE(thread, st_alink);
2726ca35587Sdholland 	fhe->num_threads--;
2736ca35587Sdholland }
2746ca35587Sdholland 
2756ca35587Sdholland /*
2766ca35587Sdholland  * Account for an ongoing operation associated with this file.
2776ca35587Sdholland  */
2786ca35587Sdholland static void
fha_hash_entry_add_op(struct fha_hash_entry * fhe,int locktype,int count)2796ca35587Sdholland fha_hash_entry_add_op(struct fha_hash_entry *fhe, int locktype, int count)
2806ca35587Sdholland {
2816ca35587Sdholland 
282e81f0ea2Spgoyette 	mtx_assert(fhe->mtx, MA_OWNED);
2836ca35587Sdholland 	if (LK_EXCLUSIVE == locktype)
2846ca35587Sdholland 		fhe->num_exclusive += count;
2856ca35587Sdholland 	else
2866ca35587Sdholland 		fhe->num_rw += count;
2876ca35587Sdholland }
2886ca35587Sdholland 
2896ca35587Sdholland /*
2906ca35587Sdholland  * Get the service thread currently associated with the fhe that is
2916ca35587Sdholland  * appropriate to handle this operation.
2926ca35587Sdholland  */
293e81f0ea2Spgoyette static SVCTHREAD *
fha_hash_entry_choose_thread(struct fha_params * softc,struct fha_hash_entry * fhe,struct fha_info * i,SVCTHREAD * this_thread)2946ca35587Sdholland fha_hash_entry_choose_thread(struct fha_params *softc,
2956ca35587Sdholland     struct fha_hash_entry *fhe, struct fha_info *i, SVCTHREAD *this_thread)
2966ca35587Sdholland {
2976ca35587Sdholland 	SVCTHREAD *thread, *min_thread = NULL;
2986ca35587Sdholland 	SVCPOOL *pool;
2996ca35587Sdholland 	int req_count, min_count = 0;
3006ca35587Sdholland 	off_t offset1, offset2;
3016ca35587Sdholland 
3026ca35587Sdholland 	pool = *softc->pool;
3036ca35587Sdholland 
3046ca35587Sdholland 	LIST_FOREACH(thread, &fhe->threads, st_alink) {
305e81f0ea2Spgoyette 		req_count = thread->st_p2;
3066ca35587Sdholland 
3076ca35587Sdholland 		/* If there are any writes in progress, use the first thread. */
3086ca35587Sdholland 		if (fhe->num_exclusive) {
3096ca35587Sdholland #if 0
3106ca35587Sdholland 			ITRACE_CURPROC(ITRACE_NFS, ITRACE_INFO,
3116ca35587Sdholland 			    "fha: %p(%d)w", thread, req_count);
3126ca35587Sdholland #endif
3136ca35587Sdholland 			return (thread);
3146ca35587Sdholland 		}
3156ca35587Sdholland 
3166ca35587Sdholland 		/*
3176ca35587Sdholland 		 * Check for read locality, making sure that we won't
3186ca35587Sdholland 		 * exceed our per-thread load limit in the process.
3196ca35587Sdholland 		 */
3206ca35587Sdholland 		offset1 = i->offset;
321e81f0ea2Spgoyette 		offset2 = thread->st_p3;
3226ca35587Sdholland 
3236ca35587Sdholland 		if (((offset1 >= offset2)
3246ca35587Sdholland 		  && ((offset1 - offset2) < (1 << softc->ctls.bin_shift)))
3256ca35587Sdholland 		 || ((offset2 > offset1)
3266ca35587Sdholland 		  && ((offset2 - offset1) < (1 << softc->ctls.bin_shift)))) {
3276ca35587Sdholland 			if ((softc->ctls.max_reqs_per_nfsd == 0) ||
3286ca35587Sdholland 			    (req_count < softc->ctls.max_reqs_per_nfsd)) {
3296ca35587Sdholland #if 0
3306ca35587Sdholland 				ITRACE_CURPROC(ITRACE_NFS, ITRACE_INFO,
3316ca35587Sdholland 				    "fha: %p(%d)r", thread, req_count);
3326ca35587Sdholland #endif
3336ca35587Sdholland 				return (thread);
3346ca35587Sdholland 			}
3356ca35587Sdholland 		}
3366ca35587Sdholland 
3376ca35587Sdholland 		/*
3386ca35587Sdholland 		 * We don't have a locality match, so skip this thread,
3396ca35587Sdholland 		 * but keep track of the most attractive thread in case
3406ca35587Sdholland 		 * we need to come back to it later.
3416ca35587Sdholland 		 */
3426ca35587Sdholland #if 0
3436ca35587Sdholland 		ITRACE_CURPROC(ITRACE_NFS, ITRACE_INFO,
3446ca35587Sdholland 		    "fha: %p(%d)s off1 %llu off2 %llu", thread,
3456ca35587Sdholland 		    req_count, offset1, offset2);
3466ca35587Sdholland #endif
3476ca35587Sdholland 		if ((min_thread == NULL) || (req_count < min_count)) {
3486ca35587Sdholland 			min_count = req_count;
3496ca35587Sdholland 			min_thread = thread;
3506ca35587Sdholland 		}
3516ca35587Sdholland 	}
3526ca35587Sdholland 
3536ca35587Sdholland 	/*
3546ca35587Sdholland 	 * We didn't find a good match yet.  See if we can add
3556ca35587Sdholland 	 * a new thread to this file handle entry's thread list.
3566ca35587Sdholland 	 */
3576ca35587Sdholland 	if ((softc->ctls.max_nfsds_per_fh == 0) ||
3586ca35587Sdholland 	    (fhe->num_threads < softc->ctls.max_nfsds_per_fh)) {
3596ca35587Sdholland 		thread = this_thread;
3606ca35587Sdholland #if 0
3616ca35587Sdholland 		ITRACE_CURPROC(ITRACE_NFS, ITRACE_INFO,
362e81f0ea2Spgoyette 		    "fha: %p(%d)t", thread, thread->st_p2);
3636ca35587Sdholland #endif
3646ca35587Sdholland 		fha_hash_entry_add_thread(fhe, thread);
3656ca35587Sdholland 	} else {
3666ca35587Sdholland 		/*
3676ca35587Sdholland 		 * We don't want to use any more threads for this file, so
3686ca35587Sdholland 		 * go back to the most attractive nfsd we're already using.
3696ca35587Sdholland 		 */
3706ca35587Sdholland 		thread = min_thread;
3716ca35587Sdholland 	}
3726ca35587Sdholland 
3736ca35587Sdholland 	return (thread);
3746ca35587Sdholland }
3756ca35587Sdholland 
3766ca35587Sdholland /*
3776ca35587Sdholland  * After getting a request, try to assign it to some thread.  Usually we
3786ca35587Sdholland  * handle it ourselves.
3796ca35587Sdholland  */
3806ca35587Sdholland SVCTHREAD *
fha_assign(SVCTHREAD * this_thread,struct svc_req * req,struct fha_params * softc)3816ca35587Sdholland fha_assign(SVCTHREAD *this_thread, struct svc_req *req,
3826ca35587Sdholland     struct fha_params *softc)
3836ca35587Sdholland {
3846ca35587Sdholland 	SVCTHREAD *thread;
3856ca35587Sdholland 	struct fha_info i;
3866ca35587Sdholland 	struct fha_hash_entry *fhe;
3876ca35587Sdholland 	struct fha_callbacks *cb;
3886ca35587Sdholland 
3896ca35587Sdholland 	cb = &softc->callbacks;
3906ca35587Sdholland 
3916ca35587Sdholland 	/* Check to see whether we're enabled. */
3926ca35587Sdholland 	if (softc->ctls.enable == 0)
393e81f0ea2Spgoyette 		goto thist;
3946ca35587Sdholland 
3956ca35587Sdholland 	/*
3966ca35587Sdholland 	 * Only do placement if this is an NFS request.
3976ca35587Sdholland 	 */
3986ca35587Sdholland 	if (req->rq_prog != NFS_PROG)
399e81f0ea2Spgoyette 		goto thist;
4006ca35587Sdholland 
4016ca35587Sdholland 	if (req->rq_vers != 2 && req->rq_vers != 3)
402e81f0ea2Spgoyette 		goto thist;
4036ca35587Sdholland 
4046ca35587Sdholland 	fha_extract_info(req, &i, cb);
4056ca35587Sdholland 
4066ca35587Sdholland 	/*
4076ca35587Sdholland 	 * We save the offset associated with this request for later
4086ca35587Sdholland 	 * nfsd matching.
4096ca35587Sdholland 	 */
4106ca35587Sdholland 	fhe = fha_hash_entry_lookup(softc, i.fh);
4116ca35587Sdholland 	req->rq_p1 = fhe;
4126ca35587Sdholland 	req->rq_p2 = i.locktype;
4136ca35587Sdholland 	req->rq_p3 = i.offset;
4146ca35587Sdholland 
4156ca35587Sdholland 	/*
4166ca35587Sdholland 	 * Choose a thread, taking into consideration locality, thread load,
4176ca35587Sdholland 	 * and the number of threads already working on this file.
4186ca35587Sdholland 	 */
4196ca35587Sdholland 	thread = fha_hash_entry_choose_thread(softc, fhe, &i, this_thread);
4206ca35587Sdholland 	KASSERT(thread, ("fha_assign: NULL thread!"));
4216ca35587Sdholland 	fha_hash_entry_add_op(fhe, i.locktype, 1);
422e81f0ea2Spgoyette 	thread->st_p2++;
423e81f0ea2Spgoyette 	thread->st_p3 = i.offset;
424e81f0ea2Spgoyette 
425e81f0ea2Spgoyette 	/*
426e81f0ea2Spgoyette 	 * Grab the pool lock here to not let chosen thread go away before
427e81f0ea2Spgoyette 	 * the new request inserted to its queue while we drop fhe lock.
428e81f0ea2Spgoyette 	 */
429e81f0ea2Spgoyette 	mtx_lock(&thread->st_lock);
430e81f0ea2Spgoyette 	mtx_unlock(fhe->mtx);
4316ca35587Sdholland 
4326ca35587Sdholland 	return (thread);
433e81f0ea2Spgoyette thist:
434e81f0ea2Spgoyette 	req->rq_p1 = NULL;
435e81f0ea2Spgoyette 	mtx_lock(&this_thread->st_lock);
436e81f0ea2Spgoyette 	return (this_thread);
4376ca35587Sdholland }
4386ca35587Sdholland 
4396ca35587Sdholland /*
4406ca35587Sdholland  * Called when we're done with an operation.  The request has already
4416ca35587Sdholland  * been de-queued.
4426ca35587Sdholland  */
4436ca35587Sdholland void
fha_nd_complete(SVCTHREAD * thread,struct svc_req * req)4446ca35587Sdholland fha_nd_complete(SVCTHREAD *thread, struct svc_req *req)
4456ca35587Sdholland {
4466ca35587Sdholland 	struct fha_hash_entry *fhe = req->rq_p1;
447e81f0ea2Spgoyette 	struct mtx *mtx;
4486ca35587Sdholland 
4496ca35587Sdholland 	/*
4506ca35587Sdholland 	 * This may be called for reqs that didn't go through
4516ca35587Sdholland 	 * fha_assign (e.g. extra NULL ops used for RPCSEC_GSS.
4526ca35587Sdholland 	 */
4536ca35587Sdholland 	if (!fhe)
4546ca35587Sdholland 		return;
4556ca35587Sdholland 
456e81f0ea2Spgoyette 	mtx = fhe->mtx;
457e81f0ea2Spgoyette 	mtx_lock(mtx);
4586ca35587Sdholland 	fha_hash_entry_add_op(fhe, req->rq_p2, -1);
459e81f0ea2Spgoyette 	thread->st_p2--;
460e81f0ea2Spgoyette 	KASSERT(thread->st_p2 >= 0, ("Negative request count %d on %p",
461e81f0ea2Spgoyette 	    thread->st_p2, thread));
462e81f0ea2Spgoyette 	if (thread->st_p2 == 0) {
4636ca35587Sdholland 		fha_hash_entry_remove_thread(fhe, thread);
4646ca35587Sdholland 		if (0 == fhe->num_rw + fhe->num_exclusive)
4656ca35587Sdholland 			fha_hash_entry_remove(fhe);
4666ca35587Sdholland 	}
467e81f0ea2Spgoyette 	mtx_unlock(mtx);
4686ca35587Sdholland }
4696ca35587Sdholland 
4706ca35587Sdholland int
fhe_stats_sysctl(SYSCTL_HANDLER_ARGS,struct fha_params * softc)4716ca35587Sdholland fhe_stats_sysctl(SYSCTL_HANDLER_ARGS, struct fha_params *softc)
4726ca35587Sdholland {
473e81f0ea2Spgoyette 	int error, i;
4746ca35587Sdholland 	struct sbuf sb;
4756ca35587Sdholland 	struct fha_hash_entry *fhe;
476e81f0ea2Spgoyette 	bool_t first, hfirst;
4776ca35587Sdholland 	SVCTHREAD *thread;
4786ca35587Sdholland 	SVCPOOL *pool;
4796ca35587Sdholland 
480e81f0ea2Spgoyette 	sbuf_new(&sb, NULL, 65536, SBUF_FIXEDLEN);
4816ca35587Sdholland 
4826ca35587Sdholland 	pool = NULL;
4836ca35587Sdholland 
4846ca35587Sdholland 	if (!*softc->pool) {
4856ca35587Sdholland 		sbuf_printf(&sb, "NFSD not running\n");
4866ca35587Sdholland 		goto out;
4876ca35587Sdholland 	}
4886ca35587Sdholland 	pool = *softc->pool;
4896ca35587Sdholland 
490e81f0ea2Spgoyette 	for (i = 0; i < FHA_HASH_SIZE; i++)
491e81f0ea2Spgoyette 		if (!LIST_EMPTY(&softc->fha_hash[i].list))
492e81f0ea2Spgoyette 			break;
4936ca35587Sdholland 
494e81f0ea2Spgoyette 	if (i == FHA_HASH_SIZE) {
4956ca35587Sdholland 		sbuf_printf(&sb, "No file handle entries.\n");
4966ca35587Sdholland 		goto out;
4976ca35587Sdholland 	}
4986ca35587Sdholland 
499e81f0ea2Spgoyette 	hfirst = TRUE;
500e81f0ea2Spgoyette 	for (; i < FHA_HASH_SIZE; i++) {
501e81f0ea2Spgoyette 		mtx_lock(&softc->fha_hash[i].mtx);
502e81f0ea2Spgoyette 		if (LIST_EMPTY(&softc->fha_hash[i].list)) {
503e81f0ea2Spgoyette 			mtx_unlock(&softc->fha_hash[i].mtx);
504e81f0ea2Spgoyette 			continue;
505e81f0ea2Spgoyette 		}
506e81f0ea2Spgoyette 		sbuf_printf(&sb, "%shash %d: {\n", hfirst ? "" : ", ", i);
507e81f0ea2Spgoyette 		first = TRUE;
508e81f0ea2Spgoyette 		LIST_FOREACH(fhe, &softc->fha_hash[i].list, link) {
5096ca35587Sdholland 			sbuf_printf(&sb, "%sfhe %p: {\n", first ? "  " : ", ", fhe);
5106ca35587Sdholland 
5116ca35587Sdholland 			sbuf_printf(&sb, "    fh: %ju\n", (uintmax_t) fhe->fh);
512e81f0ea2Spgoyette 			sbuf_printf(&sb, "    num_rw/exclusive: %d/%d\n",
513e81f0ea2Spgoyette 			    fhe->num_rw, fhe->num_exclusive);
5146ca35587Sdholland 			sbuf_printf(&sb, "    num_threads: %d\n", fhe->num_threads);
5156ca35587Sdholland 
5166ca35587Sdholland 			LIST_FOREACH(thread, &fhe->threads, st_alink) {
5176ca35587Sdholland 				sbuf_printf(&sb, "      thread %p offset %ju "
518e81f0ea2Spgoyette 				    "reqs %d\n", thread,
519e81f0ea2Spgoyette 				    thread->st_p3, thread->st_p2);
5206ca35587Sdholland 			}
5216ca35587Sdholland 
5226ca35587Sdholland 			sbuf_printf(&sb, "  }");
5236ca35587Sdholland 			first = FALSE;
5246ca35587Sdholland 		}
525e81f0ea2Spgoyette 		sbuf_printf(&sb, "\n}");
526e81f0ea2Spgoyette 		mtx_unlock(&softc->fha_hash[i].mtx);
527e81f0ea2Spgoyette 		hfirst = FALSE;
5286ca35587Sdholland 	}
5296ca35587Sdholland 
5306ca35587Sdholland  out:
5316ca35587Sdholland 	sbuf_trim(&sb);
5326ca35587Sdholland 	sbuf_finish(&sb);
5336ca35587Sdholland 	error = sysctl_handle_string(oidp, sbuf_data(&sb), sbuf_len(&sb), req);
5346ca35587Sdholland 	sbuf_delete(&sb);
5356ca35587Sdholland 	return (error);
5366ca35587Sdholland }
537