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