Revamp the old NFS server's File Handle Affinity (FHA) code so that

it will work with either the old or new server.

The FHA code keeps a cache of currently active file handles for
NFSv2 and v3 requests, so that read and write requests for the same
file are directed to the same group of threads (reads) or thread
(writes).  It does not currently work for NFSv4 requests.  They are
more complex, and will take more work to support.

This improves read-ahead performance, especially with ZFS, if the
FHA tuning parameters are configured appropriately.  Without the
FHA code, concurrent reads that are part of a sequential read from
a file will be directed to separate NFS threads.  This has the
effect of confusing the ZFS zfetch (prefetch) code and makes
sequential reads significantly slower with clients like Linux that
do a lot of prefetching.

The FHA code has also been updated to direct write requests to nearby
file offsets to the same thread in the same way it batches reads,
and the FHA code will now also send writes to multiple threads when
needed.

This improves sequential write performance in ZFS, because writes
to a file are now more ordered.  Since NFS writes (generally
less than 64K) are smaller than the typical ZFS record size
(usually 128K), out of order NFS writes to the same block can
trigger a read in ZFS.  Sending them down the same thread increases
the odds of their being in order.

In order for multiple write threads per file in the FHA code to be
useful, writes in the NFS server have been changed to use a LK_SHARED
vnode lock, and upgrade that to LK_EXCLUSIVE if the filesystem
doesn't allow multiple writers to a file at once.  ZFS is currently
the only filesystem that allows multiple writers to a file, because
it has internal file range locking.  This change does not affect the
NFSv4 code.

This improves random write performance to a single file in ZFS, since
we can now have multiple writers inside ZFS at one time.

I have changed the default tuning parameters to a 22 bit (4MB)
window size (from 256K) and unlimited commands per thread as a
result of my benchmarking with ZFS.

The FHA code has been updated to allow configuring the tuning
parameters from loader tunable variables in addition to sysctl
variables.  The read offset window calculation has been slightly
modified as well.  Instead of having separate bins, each file
handle has a rolling window of bin_shift size.  This minimizes
glitches in throughput when shifting from one bin to another.

sys/conf/files:
	Add nfs_fha_new.c and nfs_fha_old.c.  Compile nfs_fha.c
	when either the old or the new NFS server is built.

sys/fs/nfs/nfsport.h,
sys/fs/nfs/nfs_commonport.c:
	Bring in changes from Rick Macklem to newnfs_realign that
	allow it to operate in blocking (M_WAITOK) or non-blocking
	(M_NOWAIT) mode.

sys/fs/nfs/nfs_commonsubs.c,
sys/fs/nfs/nfs_var.h:
	Bring in a change from Rick Macklem to allow telling
	nfsm_dissect() whether or not to wait for mallocs.

sys/fs/nfs/nfsm_subs.h:
	Bring in changes from Rick Macklem to create a new
	nfsm_dissect_nonblock() inline function and
	NFSM_DISSECT_NONBLOCK() macro.

sys/fs/nfs/nfs_commonkrpc.c,
sys/fs/nfsclient/nfs_clkrpc.c:
	Add the malloc wait flag to a newnfs_realign() call.

sys/fs/nfsserver/nfs_nfsdkrpc.c:
	Setup the new NFS server's RPC thread pool so that it will
	call the FHA code.

	Add the malloc flag argument to newnfs_realign().

	Unstaticize newnfs_nfsv3_procid[] so that we can use it in
	the FHA code.

sys/fs/nfsserver/nfs_nfsdsocket.c:
	In nfsrvd_dorpc(), add NFSPROC_WRITE to the list of RPC types
	that use the LK_SHARED lock type.

sys/fs/nfsserver/nfs_nfsdport.c:
	In nfsd_fhtovp(), if we're starting a write, check to see
	whether the underlying filesystem supports shared writes.
	If not, upgrade the lock type from LK_SHARED to LK_EXCLUSIVE.

sys/nfsserver/nfs_fha.c:
	Remove all code that is specific to the NFS server
	implementation.  Anything that is server-specific is now
	accessed through a callback supplied by that server's FHA
	shim in the new softc.

	There are now separate sysctls and tunables for the FHA
	implementations for the old and new NFS servers.  The new
	NFS server has its tunables under vfs.nfsd.fha, the old
	NFS server's tunables are under vfs.nfsrv.fha as before.

	In fha_extract_info(), use callouts for all server-specific
	code.  Getting file handles and offsets is now done in the
	individual server's shim module.

	In fha_hash_entry_choose_thread(), change the way we decide
	whether two reads are in proximity to each other.
	Previously, the calculation was a simple shift operation to
	see whether the offsets were in the same power of 2 bucket.
	The issue was that there would be a bucket (and therefore
	thread) transition, even if the reads were in close
	proximity.  When there is a thread transition, reads wind
	up going somewhat out of order, and ZFS gets confused.

	The new calculation simply tries to see whether the offsets
	are within 1 << bin_shift of each other.  If they are, the
	reads will be sent to the same thread.

	The effect of this change is that for sequential reads, if
	the client doesn't exceed the max_reqs_per_nfsd parameter
	and the bin_shift is set to a reasonable value (22, or
	4MB works well in my tests), the reads in any sequential
	stream will largely be confined to a single thread.

	Change fha_assign() so that it takes a softc argument.  It
	is now called from the individual server's shim code, which
	will pass in the softc.

	Change fhe_stats_sysctl() so that it takes a softc
	parameter.  It is now called from the individual server's
	shim code.  Add the current offset to the list of things
	printed out about each active thread.

	Change the num_reads and num_writes counters in the
	fha_hash_entry structure to 32-bit values, and rename them
	num_rw and num_exclusive, respectively, to reflect their
	changed usage.

	Add an enable sysctl and tunable that allows the user to
	disable the FHA code (when vfs.XXX.fha.enable = 0).  This
	is useful for before/after performance comparisons.

nfs_fha.h:
	Move most structure definitions out of nfs_fha.c and into
	the header file, so that the individual server shims can
	see them.

	Change the default bin_shift to 22 (4MB) instead of 18
	(256K).  Allow unlimited commands per thread.

sys/nfsserver/nfs_fha_old.c,
sys/nfsserver/nfs_fha_old.h,
sys/fs/nfsserver/nfs_fha_new.c,
sys/fs/nfsserver/nfs_fha_new.h:
	Add shims for the old and new NFS servers to interface with
	the FHA code, and callbacks for the

	The shims contain all of the code and definitions that are
	specific to the NFS servers.

	They setup the server-specific callbacks and set the server
	name for the sysctl and loader tunable variables.

sys/nfsserver/nfs_srvkrpc.c:
	Configure the RPC code to call fhaold_assign() instead of
	fha_assign().

sys/modules/nfsd/Makefile:
	Add nfs_fha.c and nfs_fha_new.c.

sys/modules/nfsserver/Makefile:
	Add nfs_fha_old.c.

Reviewed by:	rmacklem
Sponsored by:	Spectra Logic
MFC after:	2 weeks
This commit is contained in:
Kenneth D. Merry 2013-04-17 21:00:22 +00:00
parent 6272779b2f
commit d96b98a360
20 changed files with 874 additions and 206 deletions

View File

@ -2404,6 +2404,7 @@ fs/nfsclient/nfs_clvfsops.c optional nfscl
fs/nfsclient/nfs_clport.c optional nfscl
fs/nfsclient/nfs_clbio.c optional nfscl
fs/nfsclient/nfs_clnfsiod.c optional nfscl
fs/nfsserver/nfs_fha_new.c optional nfsd inet
fs/nfsserver/nfs_nfsdsocket.c optional nfsd inet
fs/nfsserver/nfs_nfsdsubs.c optional nfsd inet
fs/nfsserver/nfs_nfsdstate.c optional nfsd inet
@ -3210,7 +3211,8 @@ nfsclient/nfs_subs.c optional nfsclient
nfsclient/nfs_nfsiod.c optional nfsclient
nfsclient/nfs_vfsops.c optional nfsclient
nfsclient/nfs_vnops.c optional nfsclient
nfsserver/nfs_fha.c optional nfsserver
nfsserver/nfs_fha.c optional nfsserver | nfsd
nfsserver/nfs_fha_old.c optional nfsserver
nfsserver/nfs_serv.c optional nfsserver
nfsserver/nfs_srvkrpc.c optional nfsserver
nfsserver/nfs_srvsubs.c optional nfsserver

View File

@ -797,7 +797,7 @@ newnfs_request(struct nfsrv_descript *nd, struct nfsmount *nmp,
* These could cause pointer alignment problems, so copy them to
* well aligned mbufs.
*/
newnfs_realign(&nd->nd_mrep);
newnfs_realign(&nd->nd_mrep, M_WAITOK);
nd->nd_md = nd->nd_mrep;
nd->nd_dpos = NFSMTOD(nd->nd_md, caddr_t);
nd->nd_repstat = 0;

View File

@ -132,11 +132,11 @@ static int nfssvc_call(struct thread *, struct nfssvc_args *, struct ucred *);
/*
* These architectures don't need re-alignment, so just return.
*/
void
newnfs_realign(struct mbuf **pm)
int
newnfs_realign(struct mbuf **pm, int how)
{
return;
return (0);
}
#else /* !__NO_STRICT_ALIGNMENT */
/*
@ -155,8 +155,8 @@ newnfs_realign(struct mbuf **pm)
* with TCP. Use vfs.nfs.realign_count and realign_test to check this.
*
*/
void
newnfs_realign(struct mbuf **pm)
int
newnfs_realign(struct mbuf **pm, int how)
{
struct mbuf *m, *n;
int off, space;
@ -173,11 +173,11 @@ newnfs_realign(struct mbuf **pm)
space = m_length(m, NULL);
if (space >= MINCLSIZE) {
/* NB: m_copyback handles space > MCLBYTES */
n = m_getcl(M_WAITOK, MT_DATA, 0);
n = m_getcl(how, MT_DATA, 0);
} else
n = m_get(M_WAITOK, MT_DATA);
n = m_get(how, MT_DATA);
if (n == NULL)
return;
return (ENOMEM);
/*
* Align the remainder of the mbuf chain.
*/
@ -195,6 +195,8 @@ newnfs_realign(struct mbuf **pm)
}
pm = &m->m_next;
}
return (0);
}
#endif /* __NO_STRICT_ALIGNMENT */

View File

@ -271,7 +271,7 @@ nfsm_mbufuio(struct nfsrv_descript *nd, struct uio *uiop, int siz)
* cases.
*/
APPLESTATIC void *
nfsm_dissct(struct nfsrv_descript *nd, int siz)
nfsm_dissct(struct nfsrv_descript *nd, int siz, int how)
{
mbuf_t mp2;
int siz2, xfer;
@ -296,7 +296,9 @@ nfsm_dissct(struct nfsrv_descript *nd, int siz)
} else if (siz > ncl_mbuf_mhlen) {
panic("nfs S too big");
} else {
NFSMGET(mp2);
MGET(mp2, MT_DATA, how);
if (mp2 == NULL)
return (NULL);
mbuf_setnext(mp2, mbuf_next(nd->nd_md));
mbuf_setnext(nd->nd_md, mp2);
mbuf_setlen(nd->nd_md, mbuf_len(nd->nd_md) - left);

View File

@ -235,7 +235,7 @@ int nfsm_strtom(struct nfsrv_descript *, const char *, int);
int nfsm_mbufuio(struct nfsrv_descript *, struct uio *, int);
int nfsm_fhtom(struct nfsrv_descript *, u_int8_t *, int, int);
int nfsm_advance(struct nfsrv_descript *, int, int);
void *nfsm_dissct(struct nfsrv_descript *, int);
void *nfsm_dissct(struct nfsrv_descript *, int, int);
void newnfs_trimleading(struct nfsrv_descript *);
void newnfs_trimtrailing(struct nfsrv_descript *, mbuf_t,
caddr_t);

View File

@ -100,7 +100,23 @@ nfsm_dissect(struct nfsrv_descript *nd, int siz)
retp = (void *)nd->nd_dpos;
nd->nd_dpos += siz;
} else {
retp = nfsm_dissct(nd, siz);
retp = nfsm_dissct(nd, siz, M_WAITOK);
}
return (retp);
}
static __inline void *
nfsm_dissect_nonblock(struct nfsrv_descript *nd, int siz)
{
int tt1;
void *retp;
tt1 = NFSMTOD(nd->nd_md, caddr_t) + nd->nd_md->m_len - nd->nd_dpos;
if (tt1 >= siz) {
retp = (void *)nd->nd_dpos;
nd->nd_dpos += siz;
} else {
retp = nfsm_dissct(nd, siz, M_NOWAIT);
}
return (retp);
}
@ -113,6 +129,15 @@ nfsm_dissect(struct nfsrv_descript *nd, int siz)
goto nfsmout; \
} \
} while (0)
#define NFSM_DISSECT_NONBLOCK(a, c, s) \
do { \
(a) = (c)nfsm_dissect_nonblock(nd, (s)); \
if ((a) == NULL) { \
error = EBADRPC; \
goto nfsmout; \
} \
} while (0)
#endif /* !APPLE */
#define NFSM_STRSIZ(s, m) \

View File

@ -806,7 +806,7 @@ MALLOC_DECLARE(M_NEWNFSLAYRECALL);
*/
int nfscl_loadattrcache(struct vnode **, struct nfsvattr *, void *, void *,
int, int);
void newnfs_realign(struct mbuf **);
int newnfs_realign(struct mbuf **, int);
/*
* If the port runs on an SMP box that can enforce Atomic ops with low

View File

@ -83,7 +83,7 @@ nfscb_program(struct svc_req *rqst, SVCXPRT *xprt)
*/
nd.nd_mrep = rqst->rq_args;
rqst->rq_args = NULL;
newnfs_realign(&nd.nd_mrep);
newnfs_realign(&nd.nd_mrep, M_WAITOK);
nd.nd_md = nd.nd_mrep;
nd.nd_dpos = mtod(nd.nd_md, caddr_t);
nd.nd_nam = svc_getrpccaller(rqst);

View File

@ -0,0 +1,272 @@
/*-
* Copyright (c) 2008 Isilon Inc http://www.isilon.com/
* Copyright (c) 2013 Spectra Logic Corporation
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*/
#include <sys/cdefs.h>
__FBSDID("$FreeBSD$");
#include <fs/nfs/nfsport.h>
#include <rpc/rpc.h>
#include <fs/nfs/xdr_subs.h>
#include <fs/nfs/nfs.h>
#include <fs/nfs/nfsproto.h>
#include <fs/nfs/nfsm_subs.h>
#include <nfsserver/nfs_fha.h>
#include <fs/nfsserver/nfs_fha_new.h>
static void fhanew_init(void *foo);
static void fhanew_uninit(void *foo);
rpcproc_t fhanew_get_procnum(rpcproc_t procnum);
int fhanew_realign(struct mbuf **mb, int malloc_flags);
int fhanew_get_fh(fhandle_t *fh, int v3, struct mbuf **md, caddr_t *dpos);
int fhanew_is_read(rpcproc_t procnum);
int fhanew_is_write(rpcproc_t procnum);
int fhanew_get_offset(struct mbuf **md, caddr_t *dpos, int v3,
struct fha_info *info);
int fhanew_no_offset(rpcproc_t procnum);
void fhanew_set_locktype(rpcproc_t procnum, struct fha_info *info);
static int fhenew_stats_sysctl(SYSCTL_HANDLER_ARGS);
static struct fha_params fhanew_softc;
SYSCTL_DECL(_vfs_nfsd);
extern int newnfs_nfsv3_procid[];
extern SVCPOOL *nfsrvd_pool;
SYSINIT(nfs_fhanew, SI_SUB_ROOT_CONF, SI_ORDER_ANY, fhanew_init, NULL);
SYSUNINIT(nfs_fhanew, SI_SUB_ROOT_CONF, SI_ORDER_ANY, fhanew_uninit, NULL);
static void
fhanew_init(void *foo)
{
struct fha_params *softc;
softc = &fhanew_softc;
bzero(softc, sizeof(*softc));
/*
* Setup the callbacks for this FHA personality.
*/
softc->callbacks.get_procnum = fhanew_get_procnum;
softc->callbacks.realign = fhanew_realign;
softc->callbacks.get_fh = fhanew_get_fh;
softc->callbacks.is_read = fhanew_is_read;
softc->callbacks.is_write = fhanew_is_write;
softc->callbacks.get_offset = fhanew_get_offset;
softc->callbacks.no_offset = fhanew_no_offset;
softc->callbacks.set_locktype = fhanew_set_locktype;
softc->callbacks.fhe_stats_sysctl = fhenew_stats_sysctl;
snprintf(softc->server_name, sizeof(softc->server_name),
FHANEW_SERVER_NAME);
softc->pool = &nfsrvd_pool;
/*
* Initialize the sysctl context list for the fha module.
*/
sysctl_ctx_init(&softc->sysctl_ctx);
softc->sysctl_tree = SYSCTL_ADD_NODE(&softc->sysctl_ctx,
SYSCTL_STATIC_CHILDREN(_vfs_nfsd), OID_AUTO, "fha", CTLFLAG_RD,
0, "fha node");
if (softc->sysctl_tree == NULL) {
printf("%s: unable to allocate sysctl tree\n", __func__);
return;
}
fha_init(softc);
}
static void
fhanew_uninit(void *foo)
{
struct fha_params *softc;
softc = &fhanew_softc;
fha_uninit(softc);
}
rpcproc_t
fhanew_get_procnum(rpcproc_t procnum)
{
if (procnum > NFSV2PROC_STATFS)
return (-1);
return (newnfs_nfsv3_procid[procnum]);
}
int
fhanew_realign(struct mbuf **mb, int malloc_flags)
{
return (newnfs_realign(mb, malloc_flags));
}
int
fhanew_get_fh(fhandle_t *fh, int v3, struct mbuf **md, caddr_t *dpos)
{
struct nfsrv_descript lnd, *nd;
uint32_t *tl;
int error, len;
error = 0;
len = 0;
nd = &lnd;
nd->nd_md = *md;
nd->nd_dpos = *dpos;
if (v3) {
NFSM_DISSECT_NONBLOCK(tl, uint32_t *, NFSX_UNSIGNED);
if ((len = fxdr_unsigned(int, *tl)) <= 0 || len > NFSX_FHMAX) {
error = EBADRPC;
goto nfsmout;
}
} else {
len = NFSX_V2FH;
}
if (len != 0) {
NFSM_DISSECT_NONBLOCK(tl, uint32_t *, len);
bcopy(tl, fh, len);
} else
bzero(fh, sizeof(*fh));
nfsmout:
*md = nd->nd_md;
*dpos = nd->nd_dpos;
return (error);
}
int
fhanew_is_read(rpcproc_t procnum)
{
if (procnum == NFSPROC_READ)
return (1);
else
return (0);
}
int
fhanew_is_write(rpcproc_t procnum)
{
if (procnum == NFSPROC_WRITE)
return (1);
else
return (0);
}
int
fhanew_get_offset(struct mbuf **md, caddr_t *dpos, int v3,
struct fha_info *info)
{
struct nfsrv_descript lnd, *nd;
uint32_t *tl;
int error;
error = 0;
nd = &lnd;
nd->nd_md = *md;
nd->nd_dpos = *dpos;
if (v3) {
NFSM_DISSECT_NONBLOCK(tl, uint32_t *, 2 * NFSX_UNSIGNED);
info->offset = fxdr_hyper(tl);
} else {
NFSM_DISSECT_NONBLOCK(tl, uint32_t *, NFSX_UNSIGNED);
info->offset = fxdr_unsigned(uint32_t, *tl);
}
nfsmout:
*md = nd->nd_md;
*dpos = nd->nd_dpos;
return (error);
}
int
fhanew_no_offset(rpcproc_t procnum)
{
if (procnum == NFSPROC_FSSTAT ||
procnum == NFSPROC_FSINFO ||
procnum == NFSPROC_PATHCONF ||
procnum == NFSPROC_NOOP ||
procnum == NFSPROC_NULL)
return (1);
else
return (0);
}
void
fhanew_set_locktype(rpcproc_t procnum, struct fha_info *info)
{
switch (procnum) {
case NFSPROC_NULL:
case NFSPROC_GETATTR:
case NFSPROC_LOOKUP:
case NFSPROC_ACCESS:
case NFSPROC_READLINK:
case NFSPROC_READ:
case NFSPROC_READDIR:
case NFSPROC_READDIRPLUS:
case NFSPROC_WRITE:
info->locktype = LK_SHARED;
break;
case NFSPROC_SETATTR:
case NFSPROC_CREATE:
case NFSPROC_MKDIR:
case NFSPROC_SYMLINK:
case NFSPROC_MKNOD:
case NFSPROC_REMOVE:
case NFSPROC_RMDIR:
case NFSPROC_RENAME:
case NFSPROC_LINK:
case NFSPROC_FSSTAT:
case NFSPROC_FSINFO:
case NFSPROC_PATHCONF:
case NFSPROC_COMMIT:
case NFSPROC_NOOP:
info->locktype = LK_EXCLUSIVE;
break;
}
}
static int
fhenew_stats_sysctl(SYSCTL_HANDLER_ARGS)
{
return (fhe_stats_sysctl(oidp, arg1, arg2, req, &fhanew_softc));
}
SVCTHREAD *
fhanew_assign(SVCTHREAD *this_thread, struct svc_req *req)
{
return (fha_assign(this_thread, req, &fhanew_softc));
}

View File

@ -0,0 +1,39 @@
/*-
* Copyright (c) 2008 Isilon Inc http://www.isilon.com/
* Copyright (c) 2013 Spectra Logic Corporation
*
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*/
/* $FreeBSD$ */
#ifndef _NFS_FHA_NEW_H
#define _NFS_FHA_NEW_H 1
#ifdef _KERNEL
#define FHANEW_SERVER_NAME "nfsd"
SVCTHREAD *fhanew_assign(SVCTHREAD *this_thread, struct svc_req *req);
#endif /* _KERNEL */
#endif /* _NFS_FHA_NEW_H */

View File

@ -42,6 +42,9 @@ __FBSDID("$FreeBSD$");
#include <rpc/rpc.h>
#include <rpc/rpcsec_gss.h>
#include <nfsserver/nfs_fha.h>
#include <fs/nfsserver/nfs_fha_new.h>
#include <security/mac/mac_framework.h>
NFSDLOCKMUTEX;
@ -51,7 +54,7 @@ struct nfsv4lock nfsd_suspend_lock;
/*
* Mapping of old NFS Version 2 RPC numbers to generic numbers.
*/
static int newnfs_nfsv3_procid[NFS_V3NPROCS] = {
int newnfs_nfsv3_procid[NFS_V3NPROCS] = {
NFSPROC_NULL,
NFSPROC_GETATTR,
NFSPROC_SETATTR,
@ -147,7 +150,7 @@ nfssvc_program(struct svc_req *rqst, SVCXPRT *xprt)
*/
nd.nd_mrep = rqst->rq_args;
rqst->rq_args = NULL;
newnfs_realign(&nd.nd_mrep);
newnfs_realign(&nd.nd_mrep, M_WAITOK);
nd.nd_md = nd.nd_mrep;
nd.nd_dpos = mtod(nd.nd_md, caddr_t);
nd.nd_nam = svc_getrpccaller(rqst);
@ -491,8 +494,8 @@ nfsrvd_init(int terminating)
nfsrvd_pool = svcpool_create("nfsd", SYSCTL_STATIC_CHILDREN(_vfs_nfsd));
nfsrvd_pool->sp_rcache = NULL;
nfsrvd_pool->sp_assign = NULL;
nfsrvd_pool->sp_done = NULL;
nfsrvd_pool->sp_assign = fhanew_assign;
nfsrvd_pool->sp_done = fha_nd_complete;
NFSD_LOCK();
}

View File

@ -2692,9 +2692,11 @@ nfsd_fhtovp(struct nfsrv_descript *nd, struct nfsrvfh *nfp, int lktype,
goto out;
}
if (startwrite)
if (startwrite) {
vn_start_write(NULL, mpp, V_WAIT);
if (lktype == LK_SHARED && !(MNT_SHARED_WRITES(mp)))
lktype = LK_EXCLUSIVE;
}
nd->nd_repstat = nfsvno_fhtovp(mp, fhp, nd->nd_nam, lktype, vpp, exp,
&credanon);
vfs_unbusy(mp);

View File

@ -379,6 +379,7 @@ nfsrvd_dorpc(struct nfsrv_descript *nd, int isdgram,
goto out;
}
if (nd->nd_procnum == NFSPROC_READ ||
nd->nd_procnum == NFSPROC_WRITE ||
nd->nd_procnum == NFSPROC_READDIR ||
nd->nd_procnum == NFSPROC_READLINK ||
nd->nd_procnum == NFSPROC_GETATTR ||

View File

@ -1,8 +1,10 @@
# $FreeBSD$
.PATH: ${.CURDIR}/../../fs/nfsserver
.PATH: ${.CURDIR}/../../fs/nfsserver ${.CURDIR}/../../nfsserver
KMOD= nfsd
SRCS= vnode_if.h \
nfs_fha.c \
nfs_fha_new.c \
nfs_nfsdserv.c \
nfs_nfsdcache.c \
nfs_nfsdkrpc.c \

View File

@ -3,7 +3,7 @@
.PATH: ${.CURDIR}/../../nfsserver
KMOD= nfsserver
SRCS= vnode_if.h \
nfs_fha.c nfs_serv.c nfs_srvkrpc.c nfs_srvsubs.c \
nfs_fha.c nfs_fha_old.c nfs_serv.c nfs_srvkrpc.c nfs_srvsubs.c \
opt_mac.h \
opt_kgssapi.h \
opt_nfs.h

View File

@ -38,134 +38,103 @@ __FBSDID("$FreeBSD$");
#include <sys/sbuf.h>
#include <rpc/rpc.h>
#include <nfs/xdr_subs.h>
#include <nfs/nfsproto.h>
#include <nfsserver/nfs.h>
#include <nfsserver/nfsm_subs.h>
#include <nfsserver/nfs_fha.h>
static MALLOC_DEFINE(M_NFS_FHA, "NFS FHA", "NFS FHA");
/* Sysctl defaults. */
#define DEF_BIN_SHIFT 18 /* 256k */
#define DEF_MAX_NFSDS_PER_FH 8
#define DEF_MAX_REQS_PER_NFSD 4
struct fha_ctls {
u_int32_t bin_shift;
u_int32_t max_nfsds_per_fh;
u_int32_t max_reqs_per_nfsd;
} fha_ctls;
struct sysctl_ctx_list fha_clist;
SYSCTL_DECL(_vfs_nfsrv);
SYSCTL_DECL(_vfs_nfsrv_fha);
/* Static sysctl node for the fha from the top-level vfs_nfsrv node. */
SYSCTL_NODE(_vfs_nfsrv, OID_AUTO, fha, CTLFLAG_RD, 0, "fha node");
/* This is the global structure that represents the state of the fha system. */
static struct fha_global {
struct fha_hash_entry_list *hashtable;
u_long hashmask;
} g_fha;
/*
* These are the entries in the filehandle hash. They talk about a specific
* file, requests against which are being handled by one or more nfsds. We
* keep a chain of nfsds against the file. We only have more than one if reads
* are ongoing, and then only if the reads affect disparate regions of the
* file.
*
* In general, we want to assign a new request to an existing nfsd if it is
* going to contend with work happening already on that nfsd, or if the
* operation is a read and the nfsd is already handling a proximate read. We
* do this to avoid jumping around in the read stream unnecessarily, and to
* avoid contention between threads over single files.
* XXX need to commonize definitions between old and new NFS code. Define
* this here so we don't include one nfsproto.h over the other.
*/
struct fha_hash_entry {
LIST_ENTRY(fha_hash_entry) link;
u_int64_t fh;
u_int16_t num_reads;
u_int16_t num_writes;
u_int8_t num_threads;
struct svcthread_list threads;
};
LIST_HEAD(fha_hash_entry_list, fha_hash_entry);
#define NFS_PROG 100003
/* A structure used for passing around data internally. */
struct fha_info {
u_int64_t fh;
off_t offset;
int locktype;
};
static int fhe_stats_sysctl(SYSCTL_HANDLER_ARGS);
static void
nfs_fha_init(void *foo)
void
fha_init(struct fha_params *softc)
{
char tmpstr[128];
/*
* A small hash table to map filehandles to fha_hash_entry
* structures.
*/
g_fha.hashtable = hashinit(256, M_NFS_FHA, &g_fha.hashmask);
softc->g_fha.hashtable = hashinit(256, M_NFS_FHA,
&softc->g_fha.hashmask);
/*
* Initialize the sysctl context list for the fha module.
* Set the default tuning parameters.
*/
sysctl_ctx_init(&fha_clist);
softc->ctls.enable = FHA_DEF_ENABLE;
softc->ctls.bin_shift = FHA_DEF_BIN_SHIFT;
softc->ctls.max_nfsds_per_fh = FHA_DEF_MAX_NFSDS_PER_FH;
softc->ctls.max_reqs_per_nfsd = FHA_DEF_MAX_REQS_PER_NFSD;
fha_ctls.bin_shift = DEF_BIN_SHIFT;
fha_ctls.max_nfsds_per_fh = DEF_MAX_NFSDS_PER_FH;
fha_ctls.max_reqs_per_nfsd = DEF_MAX_REQS_PER_NFSD;
/*
* Allow the user to override the defaults at boot time with
* tunables.
*/
snprintf(tmpstr, sizeof(tmpstr), "vfs.%s.fha.enable",
softc->server_name);
TUNABLE_INT_FETCH(tmpstr, &softc->ctls.enable);
snprintf(tmpstr, sizeof(tmpstr), "vfs.%s.fha.bin_shift",
softc->server_name);
TUNABLE_INT_FETCH(tmpstr, &softc->ctls.bin_shift);
snprintf(tmpstr, sizeof(tmpstr), "vfs.%s.fha.max_nfsds_per_fh",
softc->server_name);
TUNABLE_INT_FETCH(tmpstr, &softc->ctls.max_nfsds_per_fh);
snprintf(tmpstr, sizeof(tmpstr), "vfs.%s.fha.max_reqs_per_nfsd",
softc->server_name);
TUNABLE_INT_FETCH(tmpstr, &softc->ctls.max_reqs_per_nfsd);
SYSCTL_ADD_UINT(&fha_clist, SYSCTL_STATIC_CHILDREN(_vfs_nfsrv_fha),
/*
* Add sysctls so the user can change the tuning parameters at
* runtime.
*/
SYSCTL_ADD_UINT(&softc->sysctl_ctx, SYSCTL_CHILDREN(softc->sysctl_tree),
OID_AUTO, "enable", CTLFLAG_RW,
&softc->ctls.enable, 0, "Enable NFS File Handle Affinity (FHA)");
SYSCTL_ADD_UINT(&softc->sysctl_ctx, SYSCTL_CHILDREN(softc->sysctl_tree),
OID_AUTO, "bin_shift", CTLFLAG_RW,
&fha_ctls.bin_shift, 0, "For FHA reads, no two requests will "
&softc->ctls.bin_shift, 0, "For FHA reads, no two requests will "
"contend if they're 2^(bin_shift) bytes apart");
SYSCTL_ADD_UINT(&fha_clist, SYSCTL_STATIC_CHILDREN(_vfs_nfsrv_fha),
SYSCTL_ADD_UINT(&softc->sysctl_ctx, SYSCTL_CHILDREN(softc->sysctl_tree),
OID_AUTO, "max_nfsds_per_fh", CTLFLAG_RW,
&fha_ctls.max_nfsds_per_fh, 0, "Maximum nfsd threads that "
&softc->ctls.max_nfsds_per_fh, 0, "Maximum nfsd threads that "
"should be working on requests for the same file handle");
SYSCTL_ADD_UINT(&fha_clist, SYSCTL_STATIC_CHILDREN(_vfs_nfsrv_fha),
SYSCTL_ADD_UINT(&softc->sysctl_ctx, SYSCTL_CHILDREN(softc->sysctl_tree),
OID_AUTO, "max_reqs_per_nfsd", CTLFLAG_RW,
&fha_ctls.max_reqs_per_nfsd, 0, "Maximum requests that "
&softc->ctls.max_reqs_per_nfsd, 0, "Maximum requests that "
"single nfsd thread should be working on at any time");
SYSCTL_ADD_OID(&fha_clist, SYSCTL_STATIC_CHILDREN(_vfs_nfsrv_fha),
SYSCTL_ADD_OID(&softc->sysctl_ctx, SYSCTL_CHILDREN(softc->sysctl_tree),
OID_AUTO, "fhe_stats", CTLTYPE_STRING | CTLFLAG_RD, 0, 0,
fhe_stats_sysctl, "A", "");
softc->callbacks.fhe_stats_sysctl, "A", "");
}
static void
nfs_fha_uninit(void *foo)
void
fha_uninit(struct fha_params *softc)
{
hashdestroy(g_fha.hashtable, M_NFS_FHA, g_fha.hashmask);
sysctl_ctx_free(&softc->sysctl_ctx);
hashdestroy(softc->g_fha.hashtable, M_NFS_FHA, softc->g_fha.hashmask);
}
SYSINIT(nfs_fha, SI_SUB_ROOT_CONF, SI_ORDER_ANY, nfs_fha_init, NULL);
SYSUNINIT(nfs_fha, SI_SUB_ROOT_CONF, SI_ORDER_ANY, nfs_fha_uninit, NULL);
/*
* This just specifies that offsets should obey affinity when within
* the same 1Mbyte (1<<20) chunk for the file (reads only for now).
*/
static void
fha_extract_info(struct svc_req *req, struct fha_info *i)
fha_extract_info(struct svc_req *req, struct fha_info *i,
struct fha_callbacks *cb)
{
struct mbuf *md;
nfsfh_t fh;
fhandle_t fh;
caddr_t dpos;
static u_int64_t random_fh = 0;
int error;
int v3 = (req->rq_vers == 3);
u_int32_t *tl;
rpcproc_t procnum;
/*
@ -184,9 +153,12 @@ fha_extract_info(struct svc_req *req, struct fha_info *i)
*/
procnum = req->rq_proc;
if (!v3) {
if (procnum > NFSV2PROC_STATFS)
rpcproc_t tmp_procnum;
tmp_procnum = cb->get_procnum(procnum);
if (tmp_procnum == -1)
goto out;
procnum = nfsrv_nfsv3_procid[procnum];
procnum = tmp_procnum;
}
/*
@ -195,71 +167,28 @@ fha_extract_info(struct svc_req *req, struct fha_info *i)
* only do this for reads today, but this may change when IFS supports
* efficient concurrent writes.
*/
if (procnum == NFSPROC_FSSTAT ||
procnum == NFSPROC_FSINFO ||
procnum == NFSPROC_PATHCONF ||
procnum == NFSPROC_NOOP ||
procnum == NFSPROC_NULL)
if (cb->no_offset(procnum))
goto out;
error = nfs_realign(&req->rq_args, M_NOWAIT);
error = cb->realign(&req->rq_args, M_NOWAIT);
if (error)
goto out;
md = req->rq_args;
dpos = mtod(md, caddr_t);
/* Grab the filehandle. */
error = nfsm_srvmtofh_xx(&fh.fh_generic, v3, &md, &dpos);
error = cb->get_fh(&fh, v3, &md, &dpos);
if (error)
goto out;
bcopy(fh.fh_generic.fh_fid.fid_data, &i->fh, sizeof(i->fh));
bcopy(fh.fh_fid.fid_data, &i->fh, sizeof(i->fh));
/* Content ourselves with zero offset for all but reads. */
if (procnum != NFSPROC_READ)
goto out;
if (cb->is_read(procnum) || cb->is_write(procnum))
cb->get_offset(&md, &dpos, v3, i);
if (v3) {
tl = nfsm_dissect_xx_nonblock(2 * NFSX_UNSIGNED, &md, &dpos);
if (tl == NULL)
goto out;
i->offset = fxdr_hyper(tl);
} else {
tl = nfsm_dissect_xx_nonblock(NFSX_UNSIGNED, &md, &dpos);
if (tl == NULL)
goto out;
i->offset = fxdr_unsigned(u_int32_t, *tl);
}
out:
switch (procnum) {
case NFSPROC_NULL:
case NFSPROC_GETATTR:
case NFSPROC_LOOKUP:
case NFSPROC_ACCESS:
case NFSPROC_READLINK:
case NFSPROC_READ:
case NFSPROC_READDIR:
case NFSPROC_READDIRPLUS:
i->locktype = LK_SHARED;
break;
case NFSPROC_SETATTR:
case NFSPROC_WRITE:
case NFSPROC_CREATE:
case NFSPROC_MKDIR:
case NFSPROC_SYMLINK:
case NFSPROC_MKNOD:
case NFSPROC_REMOVE:
case NFSPROC_RMDIR:
case NFSPROC_RENAME:
case NFSPROC_LINK:
case NFSPROC_FSSTAT:
case NFSPROC_FSINFO:
case NFSPROC_PATHCONF:
case NFSPROC_COMMIT:
case NFSPROC_NOOP:
i->locktype = LK_EXCLUSIVE;
break;
}
out:
cb->set_locktype(procnum, i);
}
static struct fha_hash_entry *
@ -269,8 +198,8 @@ fha_hash_entry_new(u_int64_t fh)
e = malloc(sizeof(*e), M_NFS_FHA, M_WAITOK);
e->fh = fh;
e->num_reads = 0;
e->num_writes = 0;
e->num_rw = 0;
e->num_exclusive = 0;
e->num_threads = 0;
LIST_INIT(&e->threads);
@ -281,7 +210,7 @@ static void
fha_hash_entry_destroy(struct fha_hash_entry *e)
{
if (e->num_reads + e->num_writes)
if (e->num_rw + e->num_exclusive)
panic("nonempty fhe");
free(e, M_NFS_FHA);
}
@ -295,11 +224,16 @@ fha_hash_entry_remove(struct fha_hash_entry *e)
}
static struct fha_hash_entry *
fha_hash_entry_lookup(SVCPOOL *pool, u_int64_t fh)
fha_hash_entry_lookup(struct fha_params *softc, u_int64_t fh)
{
SVCPOOL *pool;
pool = *softc->pool;
struct fha_hash_entry *fhe, *new_fhe;
LIST_FOREACH(fhe, &g_fha.hashtable[fh % g_fha.hashmask], link)
LIST_FOREACH(fhe, &softc->g_fha.hashtable[fh % softc->g_fha.hashmask],
link)
if (fhe->fh == fh)
break;
@ -310,12 +244,14 @@ fha_hash_entry_lookup(SVCPOOL *pool, u_int64_t fh)
mtx_lock(&pool->sp_lock);
/* Double-check to make sure we still need the new entry. */
LIST_FOREACH(fhe, &g_fha.hashtable[fh % g_fha.hashmask], link)
LIST_FOREACH(fhe,
&softc->g_fha.hashtable[fh % softc->g_fha.hashmask], link)
if (fhe->fh == fh)
break;
if (!fhe) {
fhe = new_fhe;
LIST_INSERT_HEAD(&g_fha.hashtable[fh % g_fha.hashmask],
LIST_INSERT_HEAD(
&softc->g_fha.hashtable[fh % softc->g_fha.hashmask],
fhe, link);
} else
fha_hash_entry_destroy(new_fhe);
@ -348,9 +284,9 @@ fha_hash_entry_add_op(struct fha_hash_entry *fhe, int locktype, int count)
{
if (LK_EXCLUSIVE == locktype)
fhe->num_writes += count;
fhe->num_exclusive += count;
else
fhe->num_reads += count;
fhe->num_rw += count;
}
static SVCTHREAD *
@ -371,22 +307,25 @@ get_idle_thread(SVCPOOL *pool)
* appropriate to handle this operation.
*/
SVCTHREAD *
fha_hash_entry_choose_thread(SVCPOOL *pool, struct fha_hash_entry *fhe,
struct fha_info *i, SVCTHREAD *this_thread);
fha_hash_entry_choose_thread(struct fha_params *softc,
struct fha_hash_entry *fhe, struct fha_info *i, SVCTHREAD *this_thread);
SVCTHREAD *
fha_hash_entry_choose_thread(SVCPOOL *pool, struct fha_hash_entry *fhe,
struct fha_info *i, SVCTHREAD *this_thread)
fha_hash_entry_choose_thread(struct fha_params *softc,
struct fha_hash_entry *fhe, struct fha_info *i, SVCTHREAD *this_thread)
{
SVCTHREAD *thread, *min_thread = NULL;
SVCPOOL *pool;
int req_count, min_count = 0;
off_t offset1, offset2;
pool = *softc->pool;
LIST_FOREACH(thread, &fhe->threads, st_alink) {
req_count = thread->st_reqcount;
/* If there are any writes in progress, use the first thread. */
if (fhe->num_writes) {
if (fhe->num_exclusive) {
#if 0
ITRACE_CURPROC(ITRACE_NFS, ITRACE_INFO,
"fha: %p(%d)w", thread, req_count);
@ -398,12 +337,15 @@ fha_hash_entry_choose_thread(SVCPOOL *pool, struct fha_hash_entry *fhe,
* Check for read locality, making sure that we won't
* exceed our per-thread load limit in the process.
*/
offset1 = i->offset >> fha_ctls.bin_shift;
offset2 = STAILQ_FIRST(&thread->st_reqs)->rq_p3
>> fha_ctls.bin_shift;
if (offset1 == offset2) {
if ((fha_ctls.max_reqs_per_nfsd == 0) ||
(req_count < fha_ctls.max_reqs_per_nfsd)) {
offset1 = i->offset;
offset2 = STAILQ_FIRST(&thread->st_reqs)->rq_p3;
if (((offset1 >= offset2)
&& ((offset1 - offset2) < (1 << softc->ctls.bin_shift)))
|| ((offset2 > offset1)
&& ((offset2 - offset1) < (1 << softc->ctls.bin_shift)))) {
if ((softc->ctls.max_reqs_per_nfsd == 0) ||
(req_count < softc->ctls.max_reqs_per_nfsd)) {
#if 0
ITRACE_CURPROC(ITRACE_NFS, ITRACE_INFO,
"fha: %p(%d)r", thread, req_count);
@ -432,8 +374,8 @@ fha_hash_entry_choose_thread(SVCPOOL *pool, struct fha_hash_entry *fhe,
* We didn't find a good match yet. See if we can add
* a new thread to this file handle entry's thread list.
*/
if ((fha_ctls.max_nfsds_per_fh == 0) ||
(fhe->num_threads < fha_ctls.max_nfsds_per_fh)) {
if ((softc->ctls.max_nfsds_per_fh == 0) ||
(fhe->num_threads < softc->ctls.max_nfsds_per_fh)) {
/*
* We can add a new thread, so try for an idle thread
* first, and fall back to this_thread if none are idle.
@ -473,12 +415,20 @@ fha_hash_entry_choose_thread(SVCPOOL *pool, struct fha_hash_entry *fhe,
* handle it ourselves.
*/
SVCTHREAD *
fha_assign(SVCTHREAD *this_thread, struct svc_req *req)
fha_assign(SVCTHREAD *this_thread, struct svc_req *req,
struct fha_params *softc)
{
SVCPOOL *pool;
SVCTHREAD *thread;
struct fha_info i;
struct fha_hash_entry *fhe;
struct fha_callbacks *cb;
cb = &softc->callbacks;
/* Check to see whether we're enabled. */
if (softc->ctls.enable == 0)
return (this_thread);
/*
* Only do placement if this is an NFS request.
@ -490,13 +440,13 @@ fha_assign(SVCTHREAD *this_thread, struct svc_req *req)
return (this_thread);
pool = req->rq_xprt->xp_pool;
fha_extract_info(req, &i);
fha_extract_info(req, &i, cb);
/*
* We save the offset associated with this request for later
* nfsd matching.
*/
fhe = fha_hash_entry_lookup(pool, i.fh);
fhe = fha_hash_entry_lookup(softc, i.fh);
req->rq_p1 = fhe;
req->rq_p2 = i.locktype;
req->rq_p3 = i.offset;
@ -505,7 +455,7 @@ fha_assign(SVCTHREAD *this_thread, struct svc_req *req)
* Choose a thread, taking into consideration locality, thread load,
* and the number of threads already working on this file.
*/
thread = fha_hash_entry_choose_thread(pool, fhe, &i, this_thread);
thread = fha_hash_entry_choose_thread(softc, fhe, &i, this_thread);
KASSERT(thread, ("fha_assign: NULL thread!"));
fha_hash_entry_add_op(fhe, i.locktype, 1);
@ -532,33 +482,35 @@ fha_nd_complete(SVCTHREAD *thread, struct svc_req *req)
if (thread->st_reqcount == 0) {
fha_hash_entry_remove_thread(fhe, thread);
if (0 == fhe->num_reads + fhe->num_writes)
if (0 == fhe->num_rw + fhe->num_exclusive)
fha_hash_entry_remove(fhe);
}
}
extern SVCPOOL *nfsrv_pool;
static int
fhe_stats_sysctl(SYSCTL_HANDLER_ARGS)
int
fhe_stats_sysctl(SYSCTL_HANDLER_ARGS, struct fha_params *softc)
{
int error, count, i;
struct sbuf sb;
struct fha_hash_entry *fhe;
bool_t first = TRUE;
SVCTHREAD *thread;
SVCPOOL *pool;
sbuf_new(&sb, NULL, 4096, SBUF_FIXEDLEN);
if (!nfsrv_pool) {
pool = NULL;
if (!*softc->pool) {
sbuf_printf(&sb, "NFSD not running\n");
goto out;
}
pool = *softc->pool;
mtx_lock(&nfsrv_pool->sp_lock);
mtx_lock(&pool->sp_lock);
count = 0;
for (i = 0; i <= g_fha.hashmask; i++)
if (!LIST_EMPTY(&g_fha.hashtable[i]))
for (i = 0; i <= softc->g_fha.hashmask; i++)
if (!LIST_EMPTY(&softc->g_fha.hashtable[i]))
count++;
if (count == 0) {
@ -566,18 +518,20 @@ fhe_stats_sysctl(SYSCTL_HANDLER_ARGS)
goto out;
}
for (i = 0; i <= g_fha.hashmask; i++) {
LIST_FOREACH(fhe, &g_fha.hashtable[i], link) {
for (i = 0; i <= softc->g_fha.hashmask; i++) {
LIST_FOREACH(fhe, &softc->g_fha.hashtable[i], link) {
sbuf_printf(&sb, "%sfhe %p: {\n", first ? "" : ", ", fhe);
sbuf_printf(&sb, " fh: %ju\n", (uintmax_t) fhe->fh);
sbuf_printf(&sb, " num_reads: %d\n", fhe->num_reads);
sbuf_printf(&sb, " num_writes: %d\n", fhe->num_writes);
sbuf_printf(&sb, " num_rw: %d\n", fhe->num_rw);
sbuf_printf(&sb, " num_exclusive: %d\n", fhe->num_exclusive);
sbuf_printf(&sb, " num_threads: %d\n", fhe->num_threads);
LIST_FOREACH(thread, &fhe->threads, st_alink) {
sbuf_printf(&sb, " thread %p (count %d)\n",
thread, thread->st_reqcount);
sbuf_printf(&sb, " thread %p offset %ju "
"(count %d)\n", thread,
STAILQ_FIRST(&thread->st_reqs)->rq_p3,
thread->st_reqcount);
}
sbuf_printf(&sb, "}");
@ -592,8 +546,8 @@ fhe_stats_sysctl(SYSCTL_HANDLER_ARGS)
}
out:
if (nfsrv_pool)
mtx_unlock(&nfsrv_pool->sp_lock);
if (pool)
mtx_unlock(&pool->sp_lock);
sbuf_trim(&sb);
sbuf_finish(&sb);
error = sysctl_handle_string(oidp, sbuf_data(&sb), sbuf_len(&sb), req);

View File

@ -24,5 +24,89 @@
*/
/* $FreeBSD$ */
#ifndef _NFS_FHA_H
#define _NFS_FHA_H 1
#ifdef _KERNEL
/* Sysctl defaults. */
#define FHA_DEF_ENABLE 1
#define FHA_DEF_BIN_SHIFT 22 /* 4MB */
#define FHA_DEF_MAX_NFSDS_PER_FH 8
#define FHA_DEF_MAX_REQS_PER_NFSD 0 /* Unlimited */
/* This is the global structure that represents the state of the fha system. */
struct fha_global {
struct fha_hash_entry_list *hashtable;
u_long hashmask;
};
struct fha_ctls {
int enable;
uint32_t bin_shift;
uint32_t max_nfsds_per_fh;
uint32_t max_reqs_per_nfsd;
};
/*
* These are the entries in the filehandle hash. They talk about a specific
* file, requests against which are being handled by one or more nfsds. We
* keep a chain of nfsds against the file. We only have more than one if reads
* are ongoing, and then only if the reads affect disparate regions of the
* file.
*
* In general, we want to assign a new request to an existing nfsd if it is
* going to contend with work happening already on that nfsd, or if the
* operation is a read and the nfsd is already handling a proximate read. We
* do this to avoid jumping around in the read stream unnecessarily, and to
* avoid contention between threads over single files.
*/
struct fha_hash_entry {
LIST_ENTRY(fha_hash_entry) link;
u_int64_t fh;
u_int32_t num_rw;
u_int32_t num_exclusive;
u_int8_t num_threads;
struct svcthread_list threads;
};
LIST_HEAD(fha_hash_entry_list, fha_hash_entry);
/* A structure used for passing around data internally. */
struct fha_info {
u_int64_t fh;
off_t offset;
int locktype;
};
struct fha_callbacks {
rpcproc_t (*get_procnum)(rpcproc_t procnum);
int (*realign)(struct mbuf **mb, int malloc_flags);
int (*get_fh)(fhandle_t *fh, int v3, struct mbuf **md, caddr_t *dpos);
int (*is_read)(rpcproc_t procnum);
int (*is_write)(rpcproc_t procnum);
int (*get_offset)(struct mbuf **md, caddr_t *dpos, int v3, struct
fha_info *info);
int (*no_offset)(rpcproc_t procnum);
void (*set_locktype)(rpcproc_t procnum, struct fha_info *info);
int (*fhe_stats_sysctl)(SYSCTL_HANDLER_ARGS);
};
struct fha_params {
struct fha_global g_fha;
struct sysctl_ctx_list sysctl_ctx;
struct sysctl_oid *sysctl_tree;
struct fha_ctls ctls;
struct fha_callbacks callbacks;
char server_name[32];
SVCPOOL **pool;
};
void fha_nd_complete(SVCTHREAD *, struct svc_req *);
SVCTHREAD *fha_assign(SVCTHREAD *, struct svc_req *);
SVCTHREAD *fha_assign(SVCTHREAD *, struct svc_req *, struct fha_params *);
void fha_init(struct fha_params *softc);
void fha_uninit(struct fha_params *softc);
int fhe_stats_sysctl(SYSCTL_HANDLER_ARGS, struct fha_params *softc);
#endif /* _KERNEL */
#endif /* _NFS_FHA_H_ */

241
sys/nfsserver/nfs_fha_old.c Normal file
View File

@ -0,0 +1,241 @@
/*-
* Copyright (c) 2008 Isilon Inc http://www.isilon.com/
* Copyright (c) 2013 Spectra Logic Corporation
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*/
#include <sys/cdefs.h>
__FBSDID("$FreeBSD$");
#include <sys/param.h>
#include <sys/systm.h>
#include <sys/sysproto.h>
#include <sys/kernel.h>
#include <sys/vnode.h>
#include <sys/malloc.h>
#include <sys/mount.h>
#include <sys/mbuf.h>
#include <sys/sysctl.h>
#include <rpc/rpc.h>
#include <nfs/xdr_subs.h>
#include <nfs/nfsproto.h>
#include <nfsserver/nfs.h>
#include <nfsserver/nfsm_subs.h>
#include <nfsserver/nfs_fha.h>
#include <nfsserver/nfs_fha_old.h>
static void fhaold_init(void *foo);
static void fhaold_uninit(void *foo);
rpcproc_t fhaold_get_procnum(rpcproc_t procnum);
int fhaold_realign(struct mbuf **mb, int malloc_flags);
int fhaold_get_fh(fhandle_t *fh, int v3, struct mbuf **md, caddr_t *dpos);
int fhaold_is_read(rpcproc_t procnum);
int fhaold_is_write(rpcproc_t procnum);
int fhaold_get_offset(struct mbuf **md, caddr_t *dpos, int v3,
struct fha_info *info);
int fhaold_no_offset(rpcproc_t procnum);
void fhaold_set_locktype(rpcproc_t procnum, struct fha_info *info);
static int fheold_stats_sysctl(SYSCTL_HANDLER_ARGS);
static struct fha_params fhaold_softc;
SYSCTL_DECL(_vfs_nfsrv);
extern SVCPOOL *nfsrv_pool;
SYSINIT(nfs_fhaold, SI_SUB_ROOT_CONF, SI_ORDER_ANY, fhaold_init, NULL);
SYSUNINIT(nfs_fhaold, SI_SUB_ROOT_CONF, SI_ORDER_ANY, fhaold_uninit, NULL);
static void
fhaold_init(void *foo)
{
struct fha_params *softc;
softc = &fhaold_softc;
bzero(softc, sizeof(*softc));
/*
* Setup the callbacks for this FHA personality.
*/
softc->callbacks.get_procnum = fhaold_get_procnum;
softc->callbacks.realign = fhaold_realign;
softc->callbacks.get_fh = fhaold_get_fh;
softc->callbacks.is_read = fhaold_is_read;
softc->callbacks.is_write = fhaold_is_write;
softc->callbacks.get_offset = fhaold_get_offset;
softc->callbacks.no_offset = fhaold_no_offset;
softc->callbacks.set_locktype = fhaold_set_locktype;
softc->callbacks.fhe_stats_sysctl = fheold_stats_sysctl;
snprintf(softc->server_name, sizeof(softc->server_name),
FHAOLD_SERVER_NAME);
softc->pool = &nfsrv_pool;
/*
* Initialize the sysctl context list for the fha module.
*/
sysctl_ctx_init(&softc->sysctl_ctx);
softc->sysctl_tree = SYSCTL_ADD_NODE(&softc->sysctl_ctx,
SYSCTL_STATIC_CHILDREN(_vfs_nfsrv), OID_AUTO, "fha", CTLFLAG_RD,
0, "fha node");
if (softc->sysctl_tree == NULL) {
printf("%s: unable to allocate sysctl tree\n", __func__);
return;
}
fha_init(softc);
}
static void
fhaold_uninit(void *foo)
{
struct fha_params *softc;
softc = &fhaold_softc;
fha_uninit(softc);
}
rpcproc_t
fhaold_get_procnum(rpcproc_t procnum)
{
if (procnum > NFSV2PROC_STATFS)
return (-1);
return (nfsrv_nfsv3_procid[procnum]);
}
int
fhaold_realign(struct mbuf **mb, int malloc_flags)
{
return (nfs_realign(mb, malloc_flags));
}
int
fhaold_get_fh(fhandle_t *fh, int v3, struct mbuf **md, caddr_t *dpos)
{
return (nfsm_srvmtofh_xx(fh, v3, md, dpos));
}
int
fhaold_is_read(rpcproc_t procnum)
{
if (procnum == NFSPROC_READ)
return (1);
else
return (0);
}
int
fhaold_is_write(rpcproc_t procnum)
{
if (procnum == NFSPROC_WRITE)
return (1);
else
return (0);
}
int
fhaold_get_offset(struct mbuf **md, caddr_t *dpos, int v3,
struct fha_info *info)
{
uint32_t *tl;
if (v3) {
tl = nfsm_dissect_xx_nonblock(2 * NFSX_UNSIGNED, md, dpos);
if (tl == NULL)
goto out;
info->offset = fxdr_hyper(tl);
} else {
tl = nfsm_dissect_xx_nonblock(NFSX_UNSIGNED, md, dpos);
if (tl == NULL)
goto out;
info->offset = fxdr_unsigned(uint32_t, *tl);
}
return (0);
out:
return (-1);
}
int
fhaold_no_offset(rpcproc_t procnum)
{
if (procnum == NFSPROC_FSSTAT ||
procnum == NFSPROC_FSINFO ||
procnum == NFSPROC_PATHCONF ||
procnum == NFSPROC_NOOP ||
procnum == NFSPROC_NULL)
return (1);
else
return (0);
}
void
fhaold_set_locktype(rpcproc_t procnum, struct fha_info *info)
{
switch (procnum) {
case NFSPROC_NULL:
case NFSPROC_GETATTR:
case NFSPROC_LOOKUP:
case NFSPROC_ACCESS:
case NFSPROC_READLINK:
case NFSPROC_READ:
case NFSPROC_READDIR:
case NFSPROC_READDIRPLUS:
case NFSPROC_WRITE:
info->locktype = LK_SHARED;
break;
case NFSPROC_SETATTR:
case NFSPROC_CREATE:
case NFSPROC_MKDIR:
case NFSPROC_SYMLINK:
case NFSPROC_MKNOD:
case NFSPROC_REMOVE:
case NFSPROC_RMDIR:
case NFSPROC_RENAME:
case NFSPROC_LINK:
case NFSPROC_FSSTAT:
case NFSPROC_FSINFO:
case NFSPROC_PATHCONF:
case NFSPROC_COMMIT:
case NFSPROC_NOOP:
info->locktype = LK_EXCLUSIVE;
break;
}
}
static int
fheold_stats_sysctl(SYSCTL_HANDLER_ARGS)
{
return (fhe_stats_sysctl(oidp, arg1, arg2, req, &fhaold_softc));
}
SVCTHREAD *
fhaold_assign(SVCTHREAD *this_thread, struct svc_req *req)
{
return (fha_assign(this_thread, req, &fhaold_softc));
}

View File

@ -0,0 +1,38 @@
/*-
* Copyright (c) 2008 Isilon Inc http://www.isilon.com/
* Copyright (c) 2013 Spectra Logic Corporation
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*/
/* $FreeBSD$ */
#ifndef _NFS_FHA_OLD_H
#define _NFS_FHA_OLD_H 1
#ifdef _KERNEL
#define FHAOLD_SERVER_NAME "nfsrv"
SVCTHREAD *fhaold_assign(SVCTHREAD *this_thread, struct svc_req *req);
#endif /* _KERNEL */
#endif /* _NFS_FHA_OLD_H */

View File

@ -81,6 +81,7 @@ __FBSDID("$FreeBSD$");
#include <nfsserver/nfsm_subs.h>
#include <nfsserver/nfsrvcache.h>
#include <nfsserver/nfs_fha.h>
#include <nfsserver/nfs_fha_old.h>
#include <security/mac/mac_framework.h>
@ -532,7 +533,7 @@ nfsrv_init(int terminating)
nfsrv_pool = svcpool_create("nfsd", SYSCTL_STATIC_CHILDREN(_vfs_nfsrv));
nfsrv_pool->sp_rcache = replay_newcache(nfsrv_replay_size());
nfsrv_pool->sp_assign = fha_assign;
nfsrv_pool->sp_assign = fhaold_assign;
nfsrv_pool->sp_done = fha_nd_complete;
nfsrv_nmbclusters_tag = EVENTHANDLER_REGISTER(nmbclusters_change,
nfsrv_nmbclusters_change, NULL, EVENTHANDLER_PRI_FIRST);