Home | History | Annotate | Line # | Download | only in common
nfs_fha.c revision 1.2
      1  1.2  pgoyette /*	$NetBSD: nfs_fha.c,v 1.2 2016/12/13 22:41:46 pgoyette Exp $	*/
      2  1.1  dholland /*-
      3  1.1  dholland  * Copyright (c) 2008 Isilon Inc http://www.isilon.com/
      4  1.1  dholland  *
      5  1.1  dholland  * Redistribution and use in source and binary forms, with or without
      6  1.1  dholland  * modification, are permitted provided that the following conditions
      7  1.1  dholland  * are met:
      8  1.1  dholland  * 1. Redistributions of source code must retain the above copyright
      9  1.1  dholland  *    notice, this list of conditions and the following disclaimer.
     10  1.1  dholland  * 2. Redistributions in binary form must reproduce the above copyright
     11  1.1  dholland  *    notice, this list of conditions and the following disclaimer in the
     12  1.1  dholland  *    documentation and/or other materials provided with the distribution.
     13  1.1  dholland  *
     14  1.1  dholland  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
     15  1.1  dholland  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     16  1.1  dholland  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     17  1.1  dholland  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
     18  1.1  dholland  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     19  1.1  dholland  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     20  1.1  dholland  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     21  1.1  dholland  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     22  1.1  dholland  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     23  1.1  dholland  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     24  1.1  dholland  * SUCH DAMAGE.
     25  1.1  dholland  */
     26  1.1  dholland 
     27  1.1  dholland #include <sys/cdefs.h>
     28  1.2  pgoyette /* __FBSDID("FreeBSD: head/sys/nfs/nfs_fha.c 267479 2014-06-14 12:26:12Z mav "); */
     29  1.2  pgoyette __RCSID("$NetBSD: nfs_fha.c,v 1.2 2016/12/13 22:41:46 pgoyette Exp $");
     30  1.1  dholland 
     31  1.1  dholland #include <sys/param.h>
     32  1.1  dholland #include <sys/systm.h>
     33  1.1  dholland #include <sys/sysproto.h>
     34  1.1  dholland #include <sys/kernel.h>
     35  1.1  dholland #include <sys/sysctl.h>
     36  1.1  dholland #include <sys/vnode.h>
     37  1.1  dholland #include <sys/malloc.h>
     38  1.1  dholland #include <sys/mount.h>
     39  1.1  dholland #include <sys/mbuf.h>
     40  1.1  dholland #include <sys/sbuf.h>
     41  1.1  dholland 
     42  1.1  dholland #include <rpc/rpc.h>
     43  1.2  pgoyette #include <fs/nfs/common/nfs_fha.h>
     44  1.1  dholland 
     45  1.1  dholland static MALLOC_DEFINE(M_NFS_FHA, "NFS FHA", "NFS FHA");
     46  1.1  dholland 
     47  1.1  dholland /*
     48  1.1  dholland  * XXX need to commonize definitions between old and new NFS code.  Define
     49  1.1  dholland  * this here so we don't include one nfsproto.h over the other.
     50  1.1  dholland  */
     51  1.1  dholland #define	NFS_PROG		100003
     52  1.1  dholland 
     53  1.1  dholland void
     54  1.1  dholland fha_init(struct fha_params *softc)
     55  1.1  dholland {
     56  1.1  dholland 	char tmpstr[128];
     57  1.2  pgoyette 	int i;
     58  1.1  dholland 
     59  1.2  pgoyette 	for (i = 0; i < FHA_HASH_SIZE; i++)
     60  1.2  pgoyette 		mtx_init(&softc->fha_hash[i].mtx, "fhalock", NULL, MTX_DEF);
     61  1.1  dholland 
     62  1.1  dholland 	/*
     63  1.1  dholland 	 * Set the default tuning parameters.
     64  1.1  dholland 	 */
     65  1.1  dholland 	softc->ctls.enable = FHA_DEF_ENABLE;
     66  1.1  dholland 	softc->ctls.bin_shift = FHA_DEF_BIN_SHIFT;
     67  1.1  dholland 	softc->ctls.max_nfsds_per_fh = FHA_DEF_MAX_NFSDS_PER_FH;
     68  1.1  dholland 	softc->ctls.max_reqs_per_nfsd = FHA_DEF_MAX_REQS_PER_NFSD;
     69  1.1  dholland 
     70  1.1  dholland 	/*
     71  1.1  dholland 	 * Allow the user to override the defaults at boot time with
     72  1.1  dholland 	 * tunables.
     73  1.1  dholland 	 */
     74  1.1  dholland 	snprintf(tmpstr, sizeof(tmpstr), "vfs.%s.fha.enable",
     75  1.1  dholland 	    softc->server_name);
     76  1.1  dholland 	TUNABLE_INT_FETCH(tmpstr, &softc->ctls.enable);
     77  1.1  dholland 	snprintf(tmpstr, sizeof(tmpstr), "vfs.%s.fha.bin_shift",
     78  1.1  dholland 	    softc->server_name);
     79  1.1  dholland 	TUNABLE_INT_FETCH(tmpstr, &softc->ctls.bin_shift);
     80  1.1  dholland 	snprintf(tmpstr, sizeof(tmpstr), "vfs.%s.fha.max_nfsds_per_fh",
     81  1.1  dholland 	    softc->server_name);
     82  1.1  dholland 	TUNABLE_INT_FETCH(tmpstr, &softc->ctls.max_nfsds_per_fh);
     83  1.1  dholland 	snprintf(tmpstr, sizeof(tmpstr), "vfs.%s.fha.max_reqs_per_nfsd",
     84  1.1  dholland 	    softc->server_name);
     85  1.1  dholland 	TUNABLE_INT_FETCH(tmpstr, &softc->ctls.max_reqs_per_nfsd);
     86  1.1  dholland 
     87  1.1  dholland 	/*
     88  1.1  dholland 	 * Add sysctls so the user can change the tuning parameters at
     89  1.1  dholland 	 * runtime.
     90  1.1  dholland 	 */
     91  1.1  dholland 	SYSCTL_ADD_UINT(&softc->sysctl_ctx, SYSCTL_CHILDREN(softc->sysctl_tree),
     92  1.1  dholland 	    OID_AUTO, "enable", CTLFLAG_RW,
     93  1.1  dholland 	    &softc->ctls.enable, 0, "Enable NFS File Handle Affinity (FHA)");
     94  1.1  dholland 
     95  1.1  dholland 	SYSCTL_ADD_UINT(&softc->sysctl_ctx, SYSCTL_CHILDREN(softc->sysctl_tree),
     96  1.1  dholland 	    OID_AUTO, "bin_shift", CTLFLAG_RW,
     97  1.1  dholland 	    &softc->ctls.bin_shift, 0, "For FHA reads, no two requests will "
     98  1.1  dholland 	    "contend if they're 2^(bin_shift) bytes apart");
     99  1.1  dholland 
    100  1.1  dholland 	SYSCTL_ADD_UINT(&softc->sysctl_ctx, SYSCTL_CHILDREN(softc->sysctl_tree),
    101  1.1  dholland 	    OID_AUTO, "max_nfsds_per_fh", CTLFLAG_RW,
    102  1.1  dholland 	    &softc->ctls.max_nfsds_per_fh, 0, "Maximum nfsd threads that "
    103  1.1  dholland 	    "should be working on requests for the same file handle");
    104  1.1  dholland 
    105  1.1  dholland 	SYSCTL_ADD_UINT(&softc->sysctl_ctx, SYSCTL_CHILDREN(softc->sysctl_tree),
    106  1.1  dholland 	    OID_AUTO, "max_reqs_per_nfsd", CTLFLAG_RW,
    107  1.1  dholland 	    &softc->ctls.max_reqs_per_nfsd, 0, "Maximum requests that "
    108  1.1  dholland 	    "single nfsd thread should be working on at any time");
    109  1.1  dholland 
    110  1.1  dholland 	SYSCTL_ADD_OID(&softc->sysctl_ctx, SYSCTL_CHILDREN(softc->sysctl_tree),
    111  1.1  dholland 	    OID_AUTO, "fhe_stats", CTLTYPE_STRING | CTLFLAG_RD, 0, 0,
    112  1.1  dholland 	    softc->callbacks.fhe_stats_sysctl, "A", "");
    113  1.1  dholland 
    114  1.1  dholland }
    115  1.1  dholland 
    116  1.1  dholland void
    117  1.1  dholland fha_uninit(struct fha_params *softc)
    118  1.1  dholland {
    119  1.2  pgoyette 	int i;
    120  1.2  pgoyette 
    121  1.1  dholland 	sysctl_ctx_free(&softc->sysctl_ctx);
    122  1.2  pgoyette 	for (i = 0; i < FHA_HASH_SIZE; i++)
    123  1.2  pgoyette 		mtx_destroy(&softc->fha_hash[i].mtx);
    124  1.1  dholland }
    125  1.1  dholland 
    126  1.1  dholland /*
    127  1.1  dholland  * This just specifies that offsets should obey affinity when within
    128  1.1  dholland  * the same 1Mbyte (1<<20) chunk for the file (reads only for now).
    129  1.1  dholland  */
    130  1.1  dholland static void
    131  1.1  dholland fha_extract_info(struct svc_req *req, struct fha_info *i,
    132  1.1  dholland     struct fha_callbacks *cb)
    133  1.1  dholland {
    134  1.1  dholland 	struct mbuf *md;
    135  1.1  dholland 	caddr_t dpos;
    136  1.1  dholland 	static u_int64_t random_fh = 0;
    137  1.1  dholland 	int error;
    138  1.1  dholland 	int v3 = (req->rq_vers == 3);
    139  1.1  dholland 	rpcproc_t procnum;
    140  1.1  dholland 
    141  1.1  dholland 	/*
    142  1.1  dholland 	 * We start off with a random fh.  If we get a reasonable
    143  1.1  dholland 	 * procnum, we set the fh.  If there's a concept of offset
    144  1.1  dholland 	 * that we're interested in, we set that.
    145  1.1  dholland 	 */
    146  1.1  dholland 	i->fh = ++random_fh;
    147  1.1  dholland 	i->offset = 0;
    148  1.1  dholland 	i->locktype = LK_EXCLUSIVE;
    149  1.1  dholland 
    150  1.1  dholland 	/*
    151  1.1  dholland 	 * Extract the procnum and convert to v3 form if necessary,
    152  1.1  dholland 	 * taking care to deal with out-of-range procnums.  Caller will
    153  1.1  dholland 	 * ensure that rq_vers is either 2 or 3.
    154  1.1  dholland 	 */
    155  1.1  dholland 	procnum = req->rq_proc;
    156  1.1  dholland 	if (!v3) {
    157  1.1  dholland 		rpcproc_t tmp_procnum;
    158  1.1  dholland 
    159  1.1  dholland 		tmp_procnum = cb->get_procnum(procnum);
    160  1.1  dholland 		if (tmp_procnum == -1)
    161  1.1  dholland 			goto out;
    162  1.1  dholland 		procnum = tmp_procnum;
    163  1.1  dholland 	}
    164  1.1  dholland 
    165  1.1  dholland 	/*
    166  1.1  dholland 	 * We do affinity for most.  However, we divide a realm of affinity
    167  1.1  dholland 	 * by file offset so as to allow for concurrent random access.  We
    168  1.1  dholland 	 * only do this for reads today, but this may change when IFS supports
    169  1.1  dholland 	 * efficient concurrent writes.
    170  1.1  dholland 	 */
    171  1.1  dholland 	if (cb->no_offset(procnum))
    172  1.1  dholland 		goto out;
    173  1.1  dholland 
    174  1.1  dholland 	error = cb->realign(&req->rq_args, M_NOWAIT);
    175  1.1  dholland 	if (error)
    176  1.1  dholland 		goto out;
    177  1.1  dholland 	md = req->rq_args;
    178  1.1  dholland 	dpos = mtod(md, caddr_t);
    179  1.1  dholland 
    180  1.1  dholland 	/* Grab the filehandle. */
    181  1.2  pgoyette 	error = cb->get_fh(&i->fh, v3, &md, &dpos);
    182  1.1  dholland 	if (error)
    183  1.1  dholland 		goto out;
    184  1.1  dholland 
    185  1.1  dholland 	/* Content ourselves with zero offset for all but reads. */
    186  1.1  dholland 	if (cb->is_read(procnum) || cb->is_write(procnum))
    187  1.1  dholland 		cb->get_offset(&md, &dpos, v3, i);
    188  1.1  dholland 
    189  1.1  dholland out:
    190  1.1  dholland 	cb->set_locktype(procnum, i);
    191  1.1  dholland }
    192  1.1  dholland 
    193  1.1  dholland static struct fha_hash_entry *
    194  1.1  dholland fha_hash_entry_new(u_int64_t fh)
    195  1.1  dholland {
    196  1.1  dholland 	struct fha_hash_entry *e;
    197  1.1  dholland 
    198  1.1  dholland 	e = malloc(sizeof(*e), M_NFS_FHA, M_WAITOK);
    199  1.1  dholland 	e->fh = fh;
    200  1.1  dholland 	e->num_rw = 0;
    201  1.1  dholland 	e->num_exclusive = 0;
    202  1.1  dholland 	e->num_threads = 0;
    203  1.1  dholland 	LIST_INIT(&e->threads);
    204  1.1  dholland 
    205  1.1  dholland 	return (e);
    206  1.1  dholland }
    207  1.1  dholland 
    208  1.1  dholland static void
    209  1.1  dholland fha_hash_entry_destroy(struct fha_hash_entry *e)
    210  1.1  dholland {
    211  1.1  dholland 
    212  1.2  pgoyette 	mtx_assert(e->mtx, MA_OWNED);
    213  1.2  pgoyette 	KASSERT(e->num_rw == 0,
    214  1.2  pgoyette 	    ("%d reqs on destroyed fhe %p", e->num_rw, e));
    215  1.2  pgoyette 	KASSERT(e->num_exclusive == 0,
    216  1.2  pgoyette 	    ("%d exclusive reqs on destroyed fhe %p", e->num_exclusive, e));
    217  1.2  pgoyette 	KASSERT(e->num_threads == 0,
    218  1.2  pgoyette 	    ("%d threads on destroyed fhe %p", e->num_threads, e));
    219  1.1  dholland 	free(e, M_NFS_FHA);
    220  1.1  dholland }
    221  1.1  dholland 
    222  1.1  dholland static void
    223  1.1  dholland fha_hash_entry_remove(struct fha_hash_entry *e)
    224  1.1  dholland {
    225  1.1  dholland 
    226  1.2  pgoyette 	mtx_assert(e->mtx, MA_OWNED);
    227  1.1  dholland 	LIST_REMOVE(e, link);
    228  1.1  dholland 	fha_hash_entry_destroy(e);
    229  1.1  dholland }
    230  1.1  dholland 
    231  1.1  dholland static struct fha_hash_entry *
    232  1.1  dholland fha_hash_entry_lookup(struct fha_params *softc, u_int64_t fh)
    233  1.1  dholland {
    234  1.1  dholland 	SVCPOOL *pool;
    235  1.2  pgoyette 	struct fha_hash_slot *fhs;
    236  1.2  pgoyette 	struct fha_hash_entry *fhe, *new_fhe;
    237  1.1  dholland 
    238  1.1  dholland 	pool = *softc->pool;
    239  1.2  pgoyette 	fhs = &softc->fha_hash[fh % FHA_HASH_SIZE];
    240  1.2  pgoyette 	new_fhe = fha_hash_entry_new(fh);
    241  1.2  pgoyette 	new_fhe->mtx = &fhs->mtx;
    242  1.2  pgoyette 	mtx_lock(&fhs->mtx);
    243  1.2  pgoyette 	LIST_FOREACH(fhe, &fhs->list, link)
    244  1.1  dholland 		if (fhe->fh == fh)
    245  1.1  dholland 			break;
    246  1.1  dholland 	if (!fhe) {
    247  1.2  pgoyette 		fhe = new_fhe;
    248  1.2  pgoyette 		LIST_INSERT_HEAD(&fhs->list, fhe, link);
    249  1.2  pgoyette 	} else
    250  1.2  pgoyette 		fha_hash_entry_destroy(new_fhe);
    251  1.1  dholland 	return (fhe);
    252  1.1  dholland }
    253  1.1  dholland 
    254  1.1  dholland static void
    255  1.1  dholland fha_hash_entry_add_thread(struct fha_hash_entry *fhe, SVCTHREAD *thread)
    256  1.1  dholland {
    257  1.1  dholland 
    258  1.2  pgoyette 	mtx_assert(fhe->mtx, MA_OWNED);
    259  1.2  pgoyette 	thread->st_p2 = 0;
    260  1.1  dholland 	LIST_INSERT_HEAD(&fhe->threads, thread, st_alink);
    261  1.1  dholland 	fhe->num_threads++;
    262  1.1  dholland }
    263  1.1  dholland 
    264  1.1  dholland static void
    265  1.1  dholland fha_hash_entry_remove_thread(struct fha_hash_entry *fhe, SVCTHREAD *thread)
    266  1.1  dholland {
    267  1.1  dholland 
    268  1.2  pgoyette 	mtx_assert(fhe->mtx, MA_OWNED);
    269  1.2  pgoyette 	KASSERT(thread->st_p2 == 0,
    270  1.2  pgoyette 	    ("%d reqs on removed thread %p", thread->st_p2, thread));
    271  1.1  dholland 	LIST_REMOVE(thread, st_alink);
    272  1.1  dholland 	fhe->num_threads--;
    273  1.1  dholland }
    274  1.1  dholland 
    275  1.1  dholland /*
    276  1.1  dholland  * Account for an ongoing operation associated with this file.
    277  1.1  dholland  */
    278  1.1  dholland static void
    279  1.1  dholland fha_hash_entry_add_op(struct fha_hash_entry *fhe, int locktype, int count)
    280  1.1  dholland {
    281  1.1  dholland 
    282  1.2  pgoyette 	mtx_assert(fhe->mtx, MA_OWNED);
    283  1.1  dholland 	if (LK_EXCLUSIVE == locktype)
    284  1.1  dholland 		fhe->num_exclusive += count;
    285  1.1  dholland 	else
    286  1.1  dholland 		fhe->num_rw += count;
    287  1.1  dholland }
    288  1.1  dholland 
    289  1.1  dholland /*
    290  1.1  dholland  * Get the service thread currently associated with the fhe that is
    291  1.1  dholland  * appropriate to handle this operation.
    292  1.1  dholland  */
    293  1.2  pgoyette static SVCTHREAD *
    294  1.1  dholland fha_hash_entry_choose_thread(struct fha_params *softc,
    295  1.1  dholland     struct fha_hash_entry *fhe, struct fha_info *i, SVCTHREAD *this_thread)
    296  1.1  dholland {
    297  1.1  dholland 	SVCTHREAD *thread, *min_thread = NULL;
    298  1.1  dholland 	SVCPOOL *pool;
    299  1.1  dholland 	int req_count, min_count = 0;
    300  1.1  dholland 	off_t offset1, offset2;
    301  1.1  dholland 
    302  1.1  dholland 	pool = *softc->pool;
    303  1.1  dholland 
    304  1.1  dholland 	LIST_FOREACH(thread, &fhe->threads, st_alink) {
    305  1.2  pgoyette 		req_count = thread->st_p2;
    306  1.1  dholland 
    307  1.1  dholland 		/* If there are any writes in progress, use the first thread. */
    308  1.1  dholland 		if (fhe->num_exclusive) {
    309  1.1  dholland #if 0
    310  1.1  dholland 			ITRACE_CURPROC(ITRACE_NFS, ITRACE_INFO,
    311  1.1  dholland 			    "fha: %p(%d)w", thread, req_count);
    312  1.1  dholland #endif
    313  1.1  dholland 			return (thread);
    314  1.1  dholland 		}
    315  1.1  dholland 
    316  1.1  dholland 		/*
    317  1.1  dholland 		 * Check for read locality, making sure that we won't
    318  1.1  dholland 		 * exceed our per-thread load limit in the process.
    319  1.1  dholland 		 */
    320  1.1  dholland 		offset1 = i->offset;
    321  1.2  pgoyette 		offset2 = thread->st_p3;
    322  1.1  dholland 
    323  1.1  dholland 		if (((offset1 >= offset2)
    324  1.1  dholland 		  && ((offset1 - offset2) < (1 << softc->ctls.bin_shift)))
    325  1.1  dholland 		 || ((offset2 > offset1)
    326  1.1  dholland 		  && ((offset2 - offset1) < (1 << softc->ctls.bin_shift)))) {
    327  1.1  dholland 			if ((softc->ctls.max_reqs_per_nfsd == 0) ||
    328  1.1  dholland 			    (req_count < softc->ctls.max_reqs_per_nfsd)) {
    329  1.1  dholland #if 0
    330  1.1  dholland 				ITRACE_CURPROC(ITRACE_NFS, ITRACE_INFO,
    331  1.1  dholland 				    "fha: %p(%d)r", thread, req_count);
    332  1.1  dholland #endif
    333  1.1  dholland 				return (thread);
    334  1.1  dholland 			}
    335  1.1  dholland 		}
    336  1.1  dholland 
    337  1.1  dholland 		/*
    338  1.1  dholland 		 * We don't have a locality match, so skip this thread,
    339  1.1  dholland 		 * but keep track of the most attractive thread in case
    340  1.1  dholland 		 * we need to come back to it later.
    341  1.1  dholland 		 */
    342  1.1  dholland #if 0
    343  1.1  dholland 		ITRACE_CURPROC(ITRACE_NFS, ITRACE_INFO,
    344  1.1  dholland 		    "fha: %p(%d)s off1 %llu off2 %llu", thread,
    345  1.1  dholland 		    req_count, offset1, offset2);
    346  1.1  dholland #endif
    347  1.1  dholland 		if ((min_thread == NULL) || (req_count < min_count)) {
    348  1.1  dholland 			min_count = req_count;
    349  1.1  dholland 			min_thread = thread;
    350  1.1  dholland 		}
    351  1.1  dholland 	}
    352  1.1  dholland 
    353  1.1  dholland 	/*
    354  1.1  dholland 	 * We didn't find a good match yet.  See if we can add
    355  1.1  dholland 	 * a new thread to this file handle entry's thread list.
    356  1.1  dholland 	 */
    357  1.1  dholland 	if ((softc->ctls.max_nfsds_per_fh == 0) ||
    358  1.1  dholland 	    (fhe->num_threads < softc->ctls.max_nfsds_per_fh)) {
    359  1.2  pgoyette 		thread = this_thread;
    360  1.1  dholland #if 0
    361  1.2  pgoyette 		ITRACE_CURPROC(ITRACE_NFS, ITRACE_INFO,
    362  1.2  pgoyette 		    "fha: %p(%d)t", thread, thread->st_p2);
    363  1.1  dholland #endif
    364  1.1  dholland 		fha_hash_entry_add_thread(fhe, thread);
    365  1.1  dholland 	} else {
    366  1.1  dholland 		/*
    367  1.1  dholland 		 * We don't want to use any more threads for this file, so
    368  1.1  dholland 		 * go back to the most attractive nfsd we're already using.
    369  1.1  dholland 		 */
    370  1.1  dholland 		thread = min_thread;
    371  1.1  dholland 	}
    372  1.1  dholland 
    373  1.1  dholland 	return (thread);
    374  1.1  dholland }
    375  1.1  dholland 
    376  1.1  dholland /*
    377  1.1  dholland  * After getting a request, try to assign it to some thread.  Usually we
    378  1.1  dholland  * handle it ourselves.
    379  1.1  dholland  */
    380  1.1  dholland SVCTHREAD *
    381  1.1  dholland fha_assign(SVCTHREAD *this_thread, struct svc_req *req,
    382  1.1  dholland     struct fha_params *softc)
    383  1.1  dholland {
    384  1.1  dholland 	SVCTHREAD *thread;
    385  1.1  dholland 	struct fha_info i;
    386  1.1  dholland 	struct fha_hash_entry *fhe;
    387  1.1  dholland 	struct fha_callbacks *cb;
    388  1.1  dholland 
    389  1.1  dholland 	cb = &softc->callbacks;
    390  1.1  dholland 
    391  1.1  dholland 	/* Check to see whether we're enabled. */
    392  1.1  dholland 	if (softc->ctls.enable == 0)
    393  1.2  pgoyette 		goto thist;
    394  1.1  dholland 
    395  1.1  dholland 	/*
    396  1.1  dholland 	 * Only do placement if this is an NFS request.
    397  1.1  dholland 	 */
    398  1.1  dholland 	if (req->rq_prog != NFS_PROG)
    399  1.2  pgoyette 		goto thist;
    400  1.1  dholland 
    401  1.1  dholland 	if (req->rq_vers != 2 && req->rq_vers != 3)
    402  1.2  pgoyette 		goto thist;
    403  1.1  dholland 
    404  1.1  dholland 	fha_extract_info(req, &i, cb);
    405  1.1  dholland 
    406  1.1  dholland 	/*
    407  1.1  dholland 	 * We save the offset associated with this request for later
    408  1.1  dholland 	 * nfsd matching.
    409  1.1  dholland 	 */
    410  1.1  dholland 	fhe = fha_hash_entry_lookup(softc, i.fh);
    411  1.1  dholland 	req->rq_p1 = fhe;
    412  1.1  dholland 	req->rq_p2 = i.locktype;
    413  1.1  dholland 	req->rq_p3 = i.offset;
    414  1.1  dholland 
    415  1.1  dholland 	/*
    416  1.1  dholland 	 * Choose a thread, taking into consideration locality, thread load,
    417  1.1  dholland 	 * and the number of threads already working on this file.
    418  1.1  dholland 	 */
    419  1.1  dholland 	thread = fha_hash_entry_choose_thread(softc, fhe, &i, this_thread);
    420  1.1  dholland 	KASSERT(thread, ("fha_assign: NULL thread!"));
    421  1.1  dholland 	fha_hash_entry_add_op(fhe, i.locktype, 1);
    422  1.2  pgoyette 	thread->st_p2++;
    423  1.2  pgoyette 	thread->st_p3 = i.offset;
    424  1.2  pgoyette 
    425  1.2  pgoyette 	/*
    426  1.2  pgoyette 	 * Grab the pool lock here to not let chosen thread go away before
    427  1.2  pgoyette 	 * the new request inserted to its queue while we drop fhe lock.
    428  1.2  pgoyette 	 */
    429  1.2  pgoyette 	mtx_lock(&thread->st_lock);
    430  1.2  pgoyette 	mtx_unlock(fhe->mtx);
    431  1.1  dholland 
    432  1.1  dholland 	return (thread);
    433  1.2  pgoyette thist:
    434  1.2  pgoyette 	req->rq_p1 = NULL;
    435  1.2  pgoyette 	mtx_lock(&this_thread->st_lock);
    436  1.2  pgoyette 	return (this_thread);
    437  1.1  dholland }
    438  1.1  dholland 
    439  1.1  dholland /*
    440  1.1  dholland  * Called when we're done with an operation.  The request has already
    441  1.1  dholland  * been de-queued.
    442  1.1  dholland  */
    443  1.1  dholland void
    444  1.1  dholland fha_nd_complete(SVCTHREAD *thread, struct svc_req *req)
    445  1.1  dholland {
    446  1.1  dholland 	struct fha_hash_entry *fhe = req->rq_p1;
    447  1.2  pgoyette 	struct mtx *mtx;
    448  1.1  dholland 
    449  1.1  dholland 	/*
    450  1.1  dholland 	 * This may be called for reqs that didn't go through
    451  1.1  dholland 	 * fha_assign (e.g. extra NULL ops used for RPCSEC_GSS.
    452  1.1  dholland 	 */
    453  1.1  dholland 	if (!fhe)
    454  1.1  dholland 		return;
    455  1.1  dholland 
    456  1.2  pgoyette 	mtx = fhe->mtx;
    457  1.2  pgoyette 	mtx_lock(mtx);
    458  1.1  dholland 	fha_hash_entry_add_op(fhe, req->rq_p2, -1);
    459  1.2  pgoyette 	thread->st_p2--;
    460  1.2  pgoyette 	KASSERT(thread->st_p2 >= 0, ("Negative request count %d on %p",
    461  1.2  pgoyette 	    thread->st_p2, thread));
    462  1.2  pgoyette 	if (thread->st_p2 == 0) {
    463  1.1  dholland 		fha_hash_entry_remove_thread(fhe, thread);
    464  1.1  dholland 		if (0 == fhe->num_rw + fhe->num_exclusive)
    465  1.1  dholland 			fha_hash_entry_remove(fhe);
    466  1.1  dholland 	}
    467  1.2  pgoyette 	mtx_unlock(mtx);
    468  1.1  dholland }
    469  1.1  dholland 
    470  1.1  dholland int
    471  1.1  dholland fhe_stats_sysctl(SYSCTL_HANDLER_ARGS, struct fha_params *softc)
    472  1.1  dholland {
    473  1.2  pgoyette 	int error, i;
    474  1.1  dholland 	struct sbuf sb;
    475  1.1  dholland 	struct fha_hash_entry *fhe;
    476  1.2  pgoyette 	bool_t first, hfirst;
    477  1.1  dholland 	SVCTHREAD *thread;
    478  1.1  dholland 	SVCPOOL *pool;
    479  1.1  dholland 
    480  1.2  pgoyette 	sbuf_new(&sb, NULL, 65536, SBUF_FIXEDLEN);
    481  1.1  dholland 
    482  1.1  dholland 	pool = NULL;
    483  1.1  dholland 
    484  1.1  dholland 	if (!*softc->pool) {
    485  1.1  dholland 		sbuf_printf(&sb, "NFSD not running\n");
    486  1.1  dholland 		goto out;
    487  1.1  dholland 	}
    488  1.1  dholland 	pool = *softc->pool;
    489  1.1  dholland 
    490  1.2  pgoyette 	for (i = 0; i < FHA_HASH_SIZE; i++)
    491  1.2  pgoyette 		if (!LIST_EMPTY(&softc->fha_hash[i].list))
    492  1.2  pgoyette 			break;
    493  1.1  dholland 
    494  1.2  pgoyette 	if (i == FHA_HASH_SIZE) {
    495  1.1  dholland 		sbuf_printf(&sb, "No file handle entries.\n");
    496  1.1  dholland 		goto out;
    497  1.1  dholland 	}
    498  1.1  dholland 
    499  1.2  pgoyette 	hfirst = TRUE;
    500  1.2  pgoyette 	for (; i < FHA_HASH_SIZE; i++) {
    501  1.2  pgoyette 		mtx_lock(&softc->fha_hash[i].mtx);
    502  1.2  pgoyette 		if (LIST_EMPTY(&softc->fha_hash[i].list)) {
    503  1.2  pgoyette 			mtx_unlock(&softc->fha_hash[i].mtx);
    504  1.2  pgoyette 			continue;
    505  1.2  pgoyette 		}
    506  1.2  pgoyette 		sbuf_printf(&sb, "%shash %d: {\n", hfirst ? "" : ", ", i);
    507  1.2  pgoyette 		first = TRUE;
    508  1.2  pgoyette 		LIST_FOREACH(fhe, &softc->fha_hash[i].list, link) {
    509  1.2  pgoyette 			sbuf_printf(&sb, "%sfhe %p: {\n", first ? "  " : ", ", fhe);
    510  1.1  dholland 
    511  1.1  dholland 			sbuf_printf(&sb, "    fh: %ju\n", (uintmax_t) fhe->fh);
    512  1.2  pgoyette 			sbuf_printf(&sb, "    num_rw/exclusive: %d/%d\n",
    513  1.2  pgoyette 			    fhe->num_rw, fhe->num_exclusive);
    514  1.1  dholland 			sbuf_printf(&sb, "    num_threads: %d\n", fhe->num_threads);
    515  1.1  dholland 
    516  1.1  dholland 			LIST_FOREACH(thread, &fhe->threads, st_alink) {
    517  1.2  pgoyette 				sbuf_printf(&sb, "      thread %p offset %ju "
    518  1.2  pgoyette 				    "reqs %d\n", thread,
    519  1.2  pgoyette 				    thread->st_p3, thread->st_p2);
    520  1.1  dholland 			}
    521  1.1  dholland 
    522  1.2  pgoyette 			sbuf_printf(&sb, "  }");
    523  1.1  dholland 			first = FALSE;
    524  1.1  dholland 		}
    525  1.2  pgoyette 		sbuf_printf(&sb, "\n}");
    526  1.2  pgoyette 		mtx_unlock(&softc->fha_hash[i].mtx);
    527  1.2  pgoyette 		hfirst = FALSE;
    528  1.1  dholland 	}
    529  1.1  dholland 
    530  1.1  dholland  out:
    531  1.1  dholland 	sbuf_trim(&sb);
    532  1.1  dholland 	sbuf_finish(&sb);
    533  1.1  dholland 	error = sysctl_handle_string(oidp, sbuf_data(&sb), sbuf_len(&sb), req);
    534  1.1  dholland 	sbuf_delete(&sb);
    535  1.1  dholland 	return (error);
    536  1.1  dholland }
    537