Skip site navigation (1)Skip section navigation (2)
Date:      Thu, 4 Apr 2013 13:47:19 -0600
From:      "Kenneth D. Merry" <ken@FreeBSD.org>
To:        fs@FreeBSD.org
Subject:   NFS File Handle Affinity ported to new NFS server
Message-ID:  <20130404194719.GA79482@nargothrond.kdm.org>

next in thread | raw e-mail | index | archive | help

--LQksG6bCIzRHxTLp
Content-Type: text/plain; charset=us-ascii
Content-Disposition: inline

Hi folks,

I have ported the old NFS server's File Handle Affinity (FHA) code so that
it works with both the old and new NFS servers.

This sped up sequential reads from ZFS very significantly in my test
scenarios.

e.g. a single stream read off of an 8-drive RAIDZ2 went from about 75MB/sec
to over 200MB/sec.

And with 7 read streams from 7 Linux clients coming off of a 36-drive
RAID-10, I went from about 700-800MB/sec to 1.7GB/sec.  (This is over
10Gb ethernet, with 2 aggregated 10Gb ports on the server end.)

The reason for the speedup is that Linux is doing a lot of prefetching, and
those read requests all wound up going to separate threads in the NFS
server.  That confused the ZFS read prefetch code, and caused it to throw
away a lot of data.

The write speed into ZFS with this version of the FHA code is similar
before and after.  One change I made was to allow multithreading writes,
since ZFS can take advantage of that.

Rick has already reviewed the patch, but any comments or testing would be 
welcome.

I have attached my internal commit messages and the patches against
FreeBSD/head as of March 28th.

Thanks!

Ken
-- 
Kenneth Merry
ken@FreeBSD.ORG

--LQksG6bCIzRHxTLp
Content-Type: text/plain; charset=us-ascii
Content-Disposition: attachment; filename="nfs_fha_commitmsg.20130329.txt"

Change 662969 by kenm@ken.spectrabsd on 2013/03/28 16:34:44

	Bring in change 662512 from //SpectraBSD/stable:
	
	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.
	
	This also improves sequential write performance in ZFS, because
	writes to a file are single-threaded.  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.
	
	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.  We may need to tweak this
	further with more testing.
	
	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_DONTWAIT) 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 ceate 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/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.
	
		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.

Affected files ...

... //depot/users/kenm/FreeBSD-test5/sys/conf/files#2 integrate
... //depot/users/kenm/FreeBSD-test5/sys/fs/nfs/nfs_commonkrpc.c#2 integrate
... //depot/users/kenm/FreeBSD-test5/sys/fs/nfs/nfs_commonport.c#2 integrate
... //depot/users/kenm/FreeBSD-test5/sys/fs/nfs/nfs_commonsubs.c#2 integrate
... //depot/users/kenm/FreeBSD-test5/sys/fs/nfs/nfs_var.h#2 integrate
... //depot/users/kenm/FreeBSD-test5/sys/fs/nfs/nfsm_subs.h#2 integrate
... //depot/users/kenm/FreeBSD-test5/sys/fs/nfs/nfsport.h#2 integrate
... //depot/users/kenm/FreeBSD-test5/sys/fs/nfsclient/nfs_clkrpc.c#2 integrate
... //depot/users/kenm/FreeBSD-test5/sys/fs/nfsserver/nfs_fha_new.c#1 branch
... //depot/users/kenm/FreeBSD-test5/sys/fs/nfsserver/nfs_fha_new.h#1 branch
... //depot/users/kenm/FreeBSD-test5/sys/fs/nfsserver/nfs_nfsdkrpc.c#2 integrate
... //depot/users/kenm/FreeBSD-test5/sys/modules/nfsd/Makefile#2 integrate
... //depot/users/kenm/FreeBSD-test5/sys/modules/nfsserver/Makefile#2 integrate
... //depot/users/kenm/FreeBSD-test5/sys/nfsserver/nfs_fha.c#2 integrate
... //depot/users/kenm/FreeBSD-test5/sys/nfsserver/nfs_fha.h#2 integrate
... //depot/users/kenm/FreeBSD-test5/sys/nfsserver/nfs_fha_old.c#1 branch
... //depot/users/kenm/FreeBSD-test5/sys/nfsserver/nfs_fha_old.h#1 branch
... //depot/users/kenm/FreeBSD-test5/sys/nfsserver/nfs_srvkrpc.c#2 integrate

Change 662971 by kenm@ken.spectrabsd on 2013/03/28 16:36:05

	Merge change 662667 from //SpectraBSD/stable:
	
	Allow parallel writes in the FHA code and fix a bug in the way
	requests were decoded.
	
	The FHA (File Handle Affinity) code previously would execute only
	one write request at a time per file handle, but could execute
	multiple reads at one time for a particular file handle.
	
	This was done originally because of the performance characteristics
	of Isilon's filesystem.  ZFS can efficiently handle parallel read
	and write requests (within limits), and so that limitation doesn't
	apply.
	
	With a single thread per file for writes, our write performance for
	7 clients writing sequentially with FHA enabled was about half the
	speed as with FHA disabled.
	
	This change brings the two cases (with and without FHA) to the same
	level of performance (approximately 590MB/sec with a 36-drive
	mirror).
	
	There is still more performance investigation and tuning to be
	done, since write performance in my test configuration is
	significantly lower than read performance (which is in the
	1.5-1.9GB/sec range).
	
	nfs_fha.h:	Add a new callback, 'is_write()', which the FHA
			shim layers need to use.
	
			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.
	
	nfs_fha.c:	In fha_extract_info(), get the offset for reads as
			well as writes.  (We determine a write with the
			new is_write() callback.)
	
			Rename num_reads -> num_rw and num_writes ->
			num_exclusive.
	
	nfs_fha_old.c:	Add an is_write() routine, and make writes a shared
			operation, not an exclusive operation.
	
	nfs_fha_new.c:	Add an is_write() routine, and make writes a shared
			operation, not an exclusive operation.
	
			Fix the way we handle the mbuf pointer and the data
			cursor in fhanew_get_fh() and fhanew_get_offset().
	
			They weren't properly handling the case where the
			mbuf chain gets reallocated.  That wasn't happening
			when we tried to decode reads, but the way write
			requests were laid out in mbufs led to the mbuf
			getting reallocated, and exposed the bug.
	
			We also weren't properly handling data cursor
			updates.
	
			So, in both functions, rely in the dissect routine
			to update the mbuf pointer (nd->nd_md) and data
			cursor (nd->nd_dpos) and update the passed in md
			and dpos pointers with the results of the
			dissection.

Affected files ...

... //depot/users/kenm/FreeBSD-test5/sys/fs/nfsserver/nfs_fha_new.c#2 integrate
... //depot/users/kenm/FreeBSD-test5/sys/nfsserver/nfs_fha.c#3 integrate
... //depot/users/kenm/FreeBSD-test5/sys/nfsserver/nfs_fha.h#3 integrate
... //depot/users/kenm/FreeBSD-test5/sys/nfsserver/nfs_fha_old.c#2 integrate


--LQksG6bCIzRHxTLp
Content-Type: text/plain; charset=us-ascii
Content-Disposition: attachment; filename="nfs_fha.20130329.2.txt"

*** src/sys/conf/files.orig
--- src/sys/conf/files
***************
*** 2409,2414 ****
--- 2409,2415 ----
  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
***************
*** 3213,3219 ****
  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_serv.c		optional nfsserver
  nfsserver/nfs_srvkrpc.c		optional nfsserver
  nfsserver/nfs_srvsubs.c		optional nfsserver
--- 3214,3221 ----
  nfsclient/nfs_nfsiod.c		optional nfsclient
  nfsclient/nfs_vfsops.c		optional nfsclient
  nfsclient/nfs_vnops.c		optional nfsclient
! 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
*** src/sys/fs/nfs/nfs_commonkrpc.c.orig
--- src/sys/fs/nfs/nfs_commonkrpc.c
***************
*** 797,803 ****
  	 * These could cause pointer alignment problems, so copy them to
  	 * well aligned mbufs.
  	 */
! 	newnfs_realign(&nd->nd_mrep);
  	nd->nd_md = nd->nd_mrep;
  	nd->nd_dpos = NFSMTOD(nd->nd_md, caddr_t);
  	nd->nd_repstat = 0;
--- 797,803 ----
  	 * These could cause pointer alignment problems, so copy them to
  	 * well aligned mbufs.
  	 */
! 	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;
*** src/sys/fs/nfs/nfs_commonport.c.orig
--- src/sys/fs/nfs/nfs_commonport.c
***************
*** 132,142 ****
  /*
   * These architectures don't need re-alignment, so just return.
   */
! void
! newnfs_realign(struct mbuf **pm)
  {
  
! 	return;
  }
  #else	/* !__NO_STRICT_ALIGNMENT */
  /*
--- 132,142 ----
  /*
   * These architectures don't need re-alignment, so just return.
   */
! int
! newnfs_realign(struct mbuf **pm, int how)
  {
  
! 	return (0);
  }
  #else	/* !__NO_STRICT_ALIGNMENT */
  /*
***************
*** 155,162 ****
   *	with TCP.  Use vfs.nfs.realign_count and realign_test to check this.
   *
   */
! void
! newnfs_realign(struct mbuf **pm)
  {
  	struct mbuf *m, *n;
  	int off, space;
--- 155,162 ----
   *	with TCP.  Use vfs.nfs.realign_count and realign_test to check this.
   *
   */
! int
! newnfs_realign(struct mbuf **pm, int how)
  {
  	struct mbuf *m, *n;
  	int off, space;
***************
*** 173,183 ****
  			space = m_length(m, NULL);
  			if (space >= MINCLSIZE) {
  				/* NB: m_copyback handles space > MCLBYTES */
! 				n = m_getcl(M_WAITOK, MT_DATA, 0);
  			} else
! 				n = m_get(M_WAITOK, MT_DATA);
  			if (n == NULL)
! 				return;
  			/*
  			 * Align the remainder of the mbuf chain.
  			 */
--- 173,183 ----
  			space = m_length(m, NULL);
  			if (space >= MINCLSIZE) {
  				/* NB: m_copyback handles space > MCLBYTES */
! 				n = m_getcl(how, MT_DATA, 0);
  			} else
! 				n = m_get(how, MT_DATA);
  			if (n == NULL)
! 				return (ENOMEM);
  			/*
  			 * Align the remainder of the mbuf chain.
  			 */
***************
*** 195,200 ****
--- 195,202 ----
  		}
  		pm = &m->m_next;
  	}
+ 
+ 	return (0);
  }
  #endif	/* __NO_STRICT_ALIGNMENT */
  
*** src/sys/fs/nfs/nfs_commonsubs.c.orig
--- src/sys/fs/nfs/nfs_commonsubs.c
***************
*** 271,277 ****
   * cases.
   */
  APPLESTATIC void *
! nfsm_dissct(struct nfsrv_descript *nd, int siz)
  {
  	mbuf_t mp2;
  	int siz2, xfer;
--- 271,277 ----
   * cases.
   */
  APPLESTATIC void *
! nfsm_dissct(struct nfsrv_descript *nd, int siz, int how)
  {
  	mbuf_t mp2;
  	int siz2, xfer;
***************
*** 296,302 ****
  	} else if (siz > ncl_mbuf_mhlen) {
  		panic("nfs S too big");
  	} else {
! 		NFSMGET(mp2);
  		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);
--- 296,304 ----
  	} else if (siz > ncl_mbuf_mhlen) {
  		panic("nfs S too big");
  	} else {
! 		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);
*** src/sys/fs/nfs/nfs_var.h.orig
--- src/sys/fs/nfs/nfs_var.h
***************
*** 235,241 ****
  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 newnfs_trimleading(struct nfsrv_descript *);
  void newnfs_trimtrailing(struct nfsrv_descript *, mbuf_t,
      caddr_t);
--- 235,241 ----
  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, int);
  void newnfs_trimleading(struct nfsrv_descript *);
  void newnfs_trimtrailing(struct nfsrv_descript *, mbuf_t,
      caddr_t);
*** src/sys/fs/nfs/nfsm_subs.h.orig
--- src/sys/fs/nfs/nfsm_subs.h
***************
*** 100,106 ****
  		retp = (void *)nd->nd_dpos; 
  		nd->nd_dpos += siz; 
  	} else { 
! 		retp = nfsm_dissct(nd, siz); 
  	}
  	return (retp);
  }
--- 100,122 ----
  		retp = (void *)nd->nd_dpos; 
  		nd->nd_dpos += siz; 
  	} else { 
! 		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,118 ****
--- 129,143 ----
  			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)  						\
*** src/sys/fs/nfs/nfsport.h.orig
--- src/sys/fs/nfs/nfsport.h
***************
*** 806,812 ****
   */
  int nfscl_loadattrcache(struct vnode **, struct nfsvattr *, void *, void *,
      int, int);
! void newnfs_realign(struct mbuf **);
  
  /*
   * If the port runs on an SMP box that can enforce Atomic ops with low
--- 806,812 ----
   */
  int nfscl_loadattrcache(struct vnode **, struct nfsvattr *, void *, void *,
      int, int);
! int newnfs_realign(struct mbuf **, int);
  
  /*
   * If the port runs on an SMP box that can enforce Atomic ops with low
*** src/sys/fs/nfsclient/nfs_clkrpc.c.orig
--- src/sys/fs/nfsclient/nfs_clkrpc.c
***************
*** 83,89 ****
  	 */
  	nd.nd_mrep = rqst->rq_args;
  	rqst->rq_args = NULL;
! 	newnfs_realign(&nd.nd_mrep);
  	nd.nd_md = nd.nd_mrep;
  	nd.nd_dpos = mtod(nd.nd_md, caddr_t);
  	nd.nd_nam = svc_getrpccaller(rqst);
--- 83,89 ----
  	 */
  	nd.nd_mrep = rqst->rq_args;
  	rqst->rq_args = NULL;
! 	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);
*** /dev/null	Fri Mar 29 13:26:00 2013
--- src/sys/fs/nfsserver/nfs_fha_new.c	Fri Mar 29 13:26:45 2013
***************
*** 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));
+ }
==== <none> - //depot/users/kenm/FreeBSD-test5/sys/fs/nfsserver/nfs_fha_new.c#2 ====
*** /dev/null	Fri Mar 29 13:26:00 2013
--- src/sys/fs/nfsserver/nfs_fha_new.h	Fri Mar 29 13:26:45 2013
***************
*** 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 */
==== <none> - //depot/users/kenm/FreeBSD-test5/sys/fs/nfsserver/nfs_fha_new.h#1 ====
*** src/sys/fs/nfsserver/nfs_nfsdkrpc.c.orig
--- src/sys/fs/nfsserver/nfs_nfsdkrpc.c
***************
*** 42,47 ****
--- 42,50 ----
  #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,57 ****
  /*
   * Mapping of old NFS Version 2 RPC numbers to generic numbers.
   */
! static int newnfs_nfsv3_procid[NFS_V3NPROCS] = {
  	NFSPROC_NULL,
  	NFSPROC_GETATTR,
  	NFSPROC_SETATTR,
--- 54,60 ----
  /*
   * Mapping of old NFS Version 2 RPC numbers to generic numbers.
   */
! int newnfs_nfsv3_procid[NFS_V3NPROCS] = {
  	NFSPROC_NULL,
  	NFSPROC_GETATTR,
  	NFSPROC_SETATTR,
***************
*** 147,153 ****
  	 */
  	nd.nd_mrep = rqst->rq_args;
  	rqst->rq_args = NULL;
! 	newnfs_realign(&nd.nd_mrep);
  	nd.nd_md = nd.nd_mrep;
  	nd.nd_dpos = mtod(nd.nd_md, caddr_t);
  	nd.nd_nam = svc_getrpccaller(rqst);
--- 150,156 ----
  	 */
  	nd.nd_mrep = rqst->rq_args;
  	rqst->rq_args = NULL;
! 	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,498 ****
  
  	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;
  
  	NFSD_LOCK();
  }
--- 494,501 ----
  
  	nfsrvd_pool = svcpool_create("nfsd", SYSCTL_STATIC_CHILDREN(_vfs_nfsd));
  	nfsrvd_pool->sp_rcache = NULL;
! 	nfsrvd_pool->sp_assign = fhanew_assign;
! 	nfsrvd_pool->sp_done = fha_nd_complete;
  
  	NFSD_LOCK();
  }
*** src/sys/modules/nfsd/Makefile.orig
--- src/sys/modules/nfsd/Makefile
***************
*** 1,8 ****
  # $FreeBSD: head/sys/modules/nfsd/Makefile 192991 2009-05-28 19:45:11Z rmacklem $
  
! .PATH: ${.CURDIR}/../../fs/nfsserver
  KMOD=	nfsd
  SRCS=	vnode_if.h \
  	nfs_nfsdserv.c \
  	nfs_nfsdcache.c \
  	nfs_nfsdkrpc.c \
--- 1,10 ----
  # $FreeBSD: head/sys/modules/nfsd/Makefile 192991 2009-05-28 19:45:11Z rmacklem $
  
! .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 \
*** src/sys/modules/nfsserver/Makefile.orig
--- src/sys/modules/nfsserver/Makefile
***************
*** 3,9 ****
  .PATH: ${.CURDIR}/../../nfsserver
  KMOD=	nfsserver
  SRCS=	vnode_if.h \
! 	nfs_fha.c nfs_serv.c nfs_srvkrpc.c nfs_srvsubs.c \
  	opt_mac.h \
  	opt_kgssapi.h \
  	opt_nfs.h
--- 3,9 ----
  .PATH: ${.CURDIR}/../../nfsserver
  KMOD=	nfsserver
  SRCS=	vnode_if.h \
! 	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
*** src/sys/nfsserver/nfs_fha.c.orig
--- src/sys/nfsserver/nfs_fha.c
***************
*** 38,171 ****
  #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.
   */
! 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);
  
! /* 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)
  {
  
  	/*
  	 * A small hash table to map filehandles to fha_hash_entry
  	 * structures.
  	 */
! 	g_fha.hashtable = hashinit(256, M_NFS_FHA, &g_fha.hashmask);
  
  	/*
! 	 * Initialize the sysctl context list for the fha module.
  	 */
! 	sysctl_ctx_init(&fha_clist);
  
! 	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;
  
! 	SYSCTL_ADD_UINT(&fha_clist, SYSCTL_STATIC_CHILDREN(_vfs_nfsrv_fha),
  	    OID_AUTO, "bin_shift", CTLFLAG_RW,
! 	    &fha_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),
  	    OID_AUTO, "max_nfsds_per_fh", CTLFLAG_RW,
! 	    &fha_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),
  	    OID_AUTO, "max_reqs_per_nfsd", CTLFLAG_RW,
! 	    &fha_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),
  	    OID_AUTO, "fhe_stats", CTLTYPE_STRING | CTLFLAG_RD, 0, 0,
! 	    fhe_stats_sysctl, "A", "");
  }
  
! static void
! nfs_fha_uninit(void *foo)
  {
! 
! 	hashdestroy(g_fha.hashtable, M_NFS_FHA, 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)
  {
  	struct mbuf *md;
! 	nfsfh_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;
  
  	/*
--- 38,140 ----
  #include <sys/sbuf.h>
  
  #include <rpc/rpc.h>
  #include <nfsserver/nfs_fha.h>
  
  static MALLOC_DEFINE(M_NFS_FHA, "NFS FHA", "NFS FHA");
  
  /*
!  * 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.
   */
! #define	NFS_PROG		100003
  
! void
! fha_init(struct fha_params *softc)
  {
+ 	char tmpstr[128];
  
  	/*
  	 * A small hash table to map filehandles to fha_hash_entry
  	 * structures.
  	 */
! 	softc->g_fha.hashtable = hashinit(256, M_NFS_FHA,
! 	    &softc->g_fha.hashmask);
! 
! 	/*
! 	 * Set the default tuning parameters.
! 	 */
! 	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;
  
  	/*
! 	 * 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);
  
! 	/*
! 	 * 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,
! 	    &softc->ctls.bin_shift, 0, "For FHA reads, no two requests will "
  	    "contend if they're 2^(bin_shift) bytes apart");
  
! 	SYSCTL_ADD_UINT(&softc->sysctl_ctx, SYSCTL_CHILDREN(softc->sysctl_tree),
  	    OID_AUTO, "max_nfsds_per_fh", CTLFLAG_RW,
! 	    &softc->ctls.max_nfsds_per_fh, 0, "Maximum nfsd threads that "
  	    "should be working on requests for the same file handle");
  
! 	SYSCTL_ADD_UINT(&softc->sysctl_ctx, SYSCTL_CHILDREN(softc->sysctl_tree),
  	    OID_AUTO, "max_reqs_per_nfsd", CTLFLAG_RW,
! 	    &softc->ctls.max_reqs_per_nfsd, 0, "Maximum requests that "
  	    "single nfsd thread should be working on at any time");
  
! 	SYSCTL_ADD_OID(&softc->sysctl_ctx, SYSCTL_CHILDREN(softc->sysctl_tree),
  	    OID_AUTO, "fhe_stats", CTLTYPE_STRING | CTLFLAG_RD, 0, 0,
! 	    softc->callbacks.fhe_stats_sysctl, "A", "");
! 
  }
  
! void
! fha_uninit(struct fha_params *softc)
  {
! 	sysctl_ctx_free(&softc->sysctl_ctx);
! 	hashdestroy(softc->g_fha.hashtable, M_NFS_FHA, softc->g_fha.hashmask);
  }
  
  /*
   * 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,
!     struct fha_callbacks *cb)
  {
  	struct mbuf *md;
! 	fhandle_t fh;
  	caddr_t dpos;
  	static u_int64_t random_fh = 0;
  	int error;
  	int v3 = (req->rq_vers == 3);
  	rpcproc_t procnum;
  
  	/*
***************
*** 184,192 ****
  	 */
  	procnum = req->rq_proc;
  	if (!v3) {
! 		if (procnum > NFSV2PROC_STATFS)
  			goto out;
! 		procnum = nfsrv_nfsv3_procid[procnum];
  	}
  
  	/*
--- 153,164 ----
  	 */
  	procnum = req->rq_proc;
  	if (!v3) {
! 		rpcproc_t tmp_procnum;
! 
! 		tmp_procnum = cb->get_procnum(procnum);
! 		if (tmp_procnum == -1)
  			goto out;
! 		procnum = tmp_procnum;
  	}
  
  	/*
***************
*** 195,265 ****
  	 * 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)
  		goto out;
  
! 	error = nfs_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);
  	if (error)
  		goto out;
  
! 	bcopy(fh.fh_generic.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 (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;
! 	}
  }
  
  static struct fha_hash_entry *
--- 167,194 ----
  	 * only do this for reads today, but this may change when IFS supports
  	 * efficient concurrent writes.
  	 */
! 	if (cb->no_offset(procnum))
  		goto out;
  
! 	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 = cb->get_fh(&fh, v3, &md, &dpos);
  	if (error)
  		goto out;
  
! 	bcopy(fh.fh_fid.fid_data, &i->fh, sizeof(i->fh));
  
  	/* Content ourselves with zero offset for all but reads. */
! 	if (cb->is_read(procnum) || cb->is_write(procnum))
! 		cb->get_offset(&md, &dpos, v3, i);
  
! out:
! 	cb->set_locktype(procnum, i);
  }
  
  static struct fha_hash_entry *
***************
*** 269,276 ****
  
  	e = malloc(sizeof(*e), M_NFS_FHA, M_WAITOK);
  	e->fh = fh;
! 	e->num_reads = 0;
! 	e->num_writes = 0;
  	e->num_threads = 0;
  	LIST_INIT(&e->threads);
  
--- 198,205 ----
  
  	e = malloc(sizeof(*e), M_NFS_FHA, M_WAITOK);
  	e->fh = fh;
! 	e->num_rw = 0;
! 	e->num_exclusive = 0;
  	e->num_threads = 0;
  	LIST_INIT(&e->threads);
  
***************
*** 281,287 ****
  fha_hash_entry_destroy(struct fha_hash_entry *e)
  {
  
! 	if (e->num_reads + e->num_writes)
  		panic("nonempty fhe");
  	free(e, M_NFS_FHA);
  }
--- 210,216 ----
  fha_hash_entry_destroy(struct fha_hash_entry *e)
  {
  
! 	if (e->num_rw + e->num_exclusive)
  		panic("nonempty fhe");
  	free(e, M_NFS_FHA);
  }
***************
*** 295,305 ****
  }
  
  static struct fha_hash_entry *
! fha_hash_entry_lookup(SVCPOOL *pool, u_int64_t fh)
  {
  	struct fha_hash_entry *fhe, *new_fhe;
  
! 	LIST_FOREACH(fhe, &g_fha.hashtable[fh % g_fha.hashmask], link)
  		if (fhe->fh == fh)
  			break;
  
--- 224,239 ----
  }
  
  static struct fha_hash_entry *
! 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, &softc->g_fha.hashtable[fh % softc->g_fha.hashmask],
! 	    link)
  		if (fhe->fh == fh)
  			break;
  
***************
*** 310,321 ****
  		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)
  			if (fhe->fh == fh)
  				break;
  		if (!fhe) {
  			fhe = new_fhe;
! 			LIST_INSERT_HEAD(&g_fha.hashtable[fh % g_fha.hashmask],
  			    fhe, link);
  		} else
  			fha_hash_entry_destroy(new_fhe);
--- 244,257 ----
  		mtx_lock(&pool->sp_lock);
  
  		/* Double-check to make sure we still need the new entry. */
! 		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(
! 			    &softc->g_fha.hashtable[fh % softc->g_fha.hashmask],
  			    fhe, link);
  		} else
  			fha_hash_entry_destroy(new_fhe);
***************
*** 348,356 ****
  {
  
  	if (LK_EXCLUSIVE == locktype)
! 		fhe->num_writes += count;
  	else
! 		fhe->num_reads += count;
  }
  
  static SVCTHREAD *
--- 284,292 ----
  {
  
  	if (LK_EXCLUSIVE == locktype)
! 		fhe->num_exclusive += count;
  	else
! 		fhe->num_rw += count;
  }
  
  static SVCTHREAD *
***************
*** 371,392 ****
   * 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);
  
  SVCTHREAD *
! fha_hash_entry_choose_thread(SVCPOOL *pool, struct fha_hash_entry *fhe,
!     struct fha_info *i, SVCTHREAD *this_thread)
  {
  	SVCTHREAD *thread, *min_thread = NULL;
  	int req_count, min_count = 0;
  	off_t offset1, offset2;
  
  	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 0
  			ITRACE_CURPROC(ITRACE_NFS, ITRACE_INFO,
  			    "fha: %p(%d)w", thread, req_count);
--- 307,331 ----
   * appropriate to handle this operation.
   */
  SVCTHREAD *
! 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(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_exclusive) {
  #if 0
  			ITRACE_CURPROC(ITRACE_NFS, ITRACE_INFO,
  			    "fha: %p(%d)w", thread, req_count);
***************
*** 398,409 ****
  		 * 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)) {
  #if 0
  				ITRACE_CURPROC(ITRACE_NFS, ITRACE_INFO,
  				    "fha: %p(%d)r", thread, req_count);
--- 337,351 ----
  		 * Check for read locality, making sure that we won't
  		 * exceed our per-thread load limit in the process.
  		 */
! 		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,439 ****
  	 * 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)) {
  		/*
  		 * We can add a new thread, so try for an idle thread
  		 * first, and fall back to this_thread if none are idle.
--- 374,381 ----
  	 * 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 ((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,485 ****
   * handle it ourselves.
   */
  SVCTHREAD *
! fha_assign(SVCTHREAD *this_thread, struct svc_req *req)
  {
  	SVCPOOL *pool;
  	SVCTHREAD *thread;
  	struct fha_info i;
  	struct fha_hash_entry *fhe;
  
  	/*
  	 * Only do placement if this is an NFS request.
  	 */
--- 415,435 ----
   * handle it ourselves.
   */
  SVCTHREAD *
! 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,502 ****
  		return (this_thread);
  
  	pool = req->rq_xprt->xp_pool;
! 	fha_extract_info(req, &i);
  
  	/*
  	 * We save the offset associated with this request for later
  	 * nfsd matching.
  	 */
! 	fhe = fha_hash_entry_lookup(pool, i.fh);
  	req->rq_p1 = fhe;
  	req->rq_p2 = i.locktype;
  	req->rq_p3 = i.offset;
--- 440,452 ----
  		return (this_thread);
  
  	pool = req->rq_xprt->xp_pool;
! 	fha_extract_info(req, &i, cb);
  
  	/*
  	 * We save the offset associated with this request for later
  	 * nfsd matching.
  	 */
! 	fhe = fha_hash_entry_lookup(softc, i.fh);
  	req->rq_p1 = fhe;
  	req->rq_p2 = i.locktype;
  	req->rq_p3 = i.offset;
***************
*** 505,511 ****
  	 * 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);
  	KASSERT(thread, ("fha_assign: NULL thread!"));
  	fha_hash_entry_add_op(fhe, i.locktype, 1);
  
--- 455,461 ----
  	 * 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(softc, fhe, &i, this_thread);
  	KASSERT(thread, ("fha_assign: NULL thread!"));
  	fha_hash_entry_add_op(fhe, i.locktype, 1);
  
***************
*** 532,564 ****
  
  	if (thread->st_reqcount == 0) {
  		fha_hash_entry_remove_thread(fhe, thread);
! 		if (0 == fhe->num_reads + fhe->num_writes)
  			fha_hash_entry_remove(fhe);
  	}
  }
  
! extern SVCPOOL *nfsrv_pool;
! 
! static int
! fhe_stats_sysctl(SYSCTL_HANDLER_ARGS)
  {
  	int error, count, i;
  	struct sbuf sb;
  	struct fha_hash_entry *fhe;
  	bool_t first = TRUE;
  	SVCTHREAD *thread;
  
  	sbuf_new(&sb, NULL, 4096, SBUF_FIXEDLEN);
  
! 	if (!nfsrv_pool) {
  		sbuf_printf(&sb, "NFSD not running\n");
  		goto out;
  	}
  
! 	mtx_lock(&nfsrv_pool->sp_lock);
  	count = 0;
! 	for (i = 0; i <= g_fha.hashmask; i++)
! 		if (!LIST_EMPTY(&g_fha.hashtable[i]))
  			count++;
  
  	if (count == 0) {
--- 482,516 ----
  
  	if (thread->st_reqcount == 0) {
  		fha_hash_entry_remove_thread(fhe, thread);
! 		if (0 == fhe->num_rw + fhe->num_exclusive)
  			fha_hash_entry_remove(fhe);
  	}
  }
  
! 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);
  
! 	pool = NULL;
! 
! 	if (!*softc->pool) {
  		sbuf_printf(&sb, "NFSD not running\n");
  		goto out;
  	}
+ 	pool = *softc->pool;
  
! 	mtx_lock(&pool->sp_lock);
  	count = 0;
! 	for (i = 0; i <= softc->g_fha.hashmask; i++)
! 		if (!LIST_EMPTY(&softc->g_fha.hashtable[i]))
  			count++;
  
  	if (count == 0) {
***************
*** 566,583 ****
  		goto out;
  	}
  
! 	for (i = 0; i <= g_fha.hashmask; i++) {
! 		LIST_FOREACH(fhe, &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_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, "}");
--- 518,537 ----
  		goto out;
  	}
  
! 	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_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 offset %ju "
! 				    "(count %d)\n", thread,
! 				    STAILQ_FIRST(&thread->st_reqs)->rq_p3,
! 				    thread->st_reqcount);
  			}
  
  			sbuf_printf(&sb, "}");
***************
*** 592,599 ****
  	}
  
   out:
! 	if (nfsrv_pool)
! 		mtx_unlock(&nfsrv_pool->sp_lock);
  	sbuf_trim(&sb);
  	sbuf_finish(&sb);
  	error = sysctl_handle_string(oidp, sbuf_data(&sb), sbuf_len(&sb), req);
--- 546,553 ----
  	}
  
   out:
! 	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);
*** src/sys/nfsserver/nfs_fha.h.orig
--- src/sys/nfsserver/nfs_fha.h
***************
*** 24,28 ****
   */
  /* $FreeBSD: head/sys/nfsserver/nfs_fha.h 184588 2008-11-03 10:38:00Z dfr $ */
  
  void fha_nd_complete(SVCTHREAD *, struct svc_req *);
! SVCTHREAD *fha_assign(SVCTHREAD *, struct svc_req *);
--- 24,112 ----
   */
  /* $FreeBSD: head/sys/nfsserver/nfs_fha.h 184588 2008-11-03 10:38:00Z dfr $ */
  
+ #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 *, 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_ */
*** /dev/null	Fri Mar 29 13:26:00 2013
--- src/sys/nfsserver/nfs_fha_old.c	Fri Mar 29 13:26:45 2013
***************
*** 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));
+ }
==== <none> - //depot/users/kenm/FreeBSD-test5/sys/nfsserver/nfs_fha_old.c#2 ====
*** /dev/null	Fri Mar 29 13:26:00 2013
--- src/sys/nfsserver/nfs_fha_old.h	Fri Mar 29 13:26:45 2013
***************
*** 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 */
==== <none> - //depot/users/kenm/FreeBSD-test5/sys/nfsserver/nfs_fha_old.h#1 ====
*** src/sys/nfsserver/nfs_srvkrpc.c.orig
--- src/sys/nfsserver/nfs_srvkrpc.c
***************
*** 81,86 ****
--- 81,87 ----
  #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,538 ****
  
  	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_done = fha_nd_complete;
  	nfsrv_nmbclusters_tag = EVENTHANDLER_REGISTER(nmbclusters_change,
  	    nfsrv_nmbclusters_change, NULL, EVENTHANDLER_PRI_FIRST);
--- 533,539 ----
  
  	nfsrv_pool = svcpool_create("nfsd", SYSCTL_STATIC_CHILDREN(_vfs_nfsrv));
  	nfsrv_pool->sp_rcache = replay_newcache(nfsrv_replay_size());
! 	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);

--LQksG6bCIzRHxTLp--



Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?20130404194719.GA79482>