]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - sys/fs/nfsserver/nfs_fha_new.c
sqlite3: Vendor import of sqlite3 3.41.0
[FreeBSD/FreeBSD.git] / sys / fs / nfsserver / nfs_fha_new.c
1 /*-
2  * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
3  *
4  * Copyright (c) 2008 Isilon Inc http://www.isilon.com/
5  * Copyright (c) 2013 Spectra Logic Corporation
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions and the following disclaimer.
12  * 2. Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following disclaimer in the
14  *    documentation and/or other materials provided with the distribution.
15  *
16  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26  * SUCH DAMAGE.
27  */
28
29 #include <sys/cdefs.h>
30 __FBSDID("$FreeBSD$");
31
32 #include <sys/types.h>
33 #include <sys/mbuf.h>
34 #include <sys/sbuf.h>
35
36 #include <fs/nfs/nfsport.h>
37 #include <fs/nfsserver/nfs_fha_new.h>
38
39 #include <rpc/rpc.h>
40
41 static MALLOC_DEFINE(M_NFS_FHA, "NFS FHA", "NFS FHA");
42
43 static void             fhanew_init(void *foo);
44 static void             fhanew_uninit(void *foo);
45 static rpcproc_t        fhanew_get_procnum(rpcproc_t procnum);
46 static int              fhanew_get_fh(uint64_t *fh, int v3, struct mbuf **md,
47                             caddr_t *dpos);
48 static int              fhanew_is_read(rpcproc_t procnum);
49 static int              fhanew_is_write(rpcproc_t procnum);
50 static int              fhanew_get_offset(struct mbuf **md, caddr_t *dpos,
51                             int v3, struct fha_info *info);
52 static int              fhanew_no_offset(rpcproc_t procnum);
53 static void             fhanew_set_locktype(rpcproc_t procnum,
54                             struct fha_info *info);
55 static int              fhenew_stats_sysctl(SYSCTL_HANDLER_ARGS);
56 static void             fha_extract_info(struct svc_req *req,
57                             struct fha_info *i);
58
59 NFSD_VNET_DEFINE_STATIC(struct fha_params *, fhanew_softc);
60
61 SYSCTL_DECL(_vfs_nfsd);
62
63 extern int newnfs_nfsv3_procid[];
64
65 VNET_SYSINIT(nfs_fhanew, SI_SUB_VNET_DONE, SI_ORDER_ANY, fhanew_init, NULL);
66 VNET_SYSUNINIT(nfs_fhanew, SI_SUB_VNET_DONE, SI_ORDER_ANY, fhanew_uninit, NULL);
67
68 static void
69 fhanew_init(void *foo)
70 {
71         struct fha_params *softc;
72         int i;
73
74         NFSD_VNET(fhanew_softc) = malloc(sizeof(struct fha_params), M_TEMP,
75             M_WAITOK | M_ZERO);
76         softc = NFSD_VNET(fhanew_softc);
77
78         snprintf(softc->server_name, sizeof(softc->server_name),
79             FHANEW_SERVER_NAME);
80
81         /*
82          * Initialize the sysctl context list for the fha module.
83          */
84         sysctl_ctx_init(&softc->sysctl_ctx);
85         if (IS_DEFAULT_VNET(curvnet)) {
86                 softc->sysctl_tree = SYSCTL_ADD_NODE(&softc->sysctl_ctx,
87                     SYSCTL_STATIC_CHILDREN(_vfs_nfsd), OID_AUTO, "fha",
88                     CTLFLAG_RD | CTLFLAG_MPSAFE, 0, "NFS File Handle Affinity (FHA)");
89                 if (softc->sysctl_tree == NULL) {
90                         printf("%s: unable to allocate sysctl tree\n", __func__);
91                         return;
92                 }
93         }
94
95         for (i = 0; i < FHA_HASH_SIZE; i++)
96                 mtx_init(&softc->fha_hash[i].mtx, "fhalock", NULL, MTX_DEF);
97
98         /*
99          * Set the default tuning parameters.
100          */
101         softc->ctls.enable = FHA_DEF_ENABLE;
102         softc->ctls.read = FHA_DEF_READ;
103         softc->ctls.write = FHA_DEF_WRITE;
104         softc->ctls.bin_shift = FHA_DEF_BIN_SHIFT;
105         softc->ctls.max_nfsds_per_fh = FHA_DEF_MAX_NFSDS_PER_FH;
106         softc->ctls.max_reqs_per_nfsd = FHA_DEF_MAX_REQS_PER_NFSD;
107
108         /*
109          * Add sysctls so the user can change the tuning parameters.
110          */
111         if (IS_DEFAULT_VNET(curvnet)) {
112                 SYSCTL_ADD_UINT(&softc->sysctl_ctx, SYSCTL_CHILDREN(softc->sysctl_tree),
113                     OID_AUTO, "enable", CTLFLAG_RWTUN,
114                     &softc->ctls.enable, 0, "Enable NFS File Handle Affinity (FHA)");
115
116                 SYSCTL_ADD_UINT(&softc->sysctl_ctx, SYSCTL_CHILDREN(softc->sysctl_tree),
117                     OID_AUTO, "read", CTLFLAG_RWTUN,
118                     &softc->ctls.read, 0, "Enable NFS FHA read locality");
119
120                 SYSCTL_ADD_UINT(&softc->sysctl_ctx, SYSCTL_CHILDREN(softc->sysctl_tree),
121                     OID_AUTO, "write", CTLFLAG_RWTUN,
122                     &softc->ctls.write, 0, "Enable NFS FHA write locality");
123
124                 SYSCTL_ADD_UINT(&softc->sysctl_ctx, SYSCTL_CHILDREN(softc->sysctl_tree),
125                     OID_AUTO, "bin_shift", CTLFLAG_RWTUN,
126                     &softc->ctls.bin_shift, 0,
127                     "Maximum locality distance 2^(bin_shift) bytes");
128
129                 SYSCTL_ADD_UINT(&softc->sysctl_ctx, SYSCTL_CHILDREN(softc->sysctl_tree),
130                     OID_AUTO, "max_nfsds_per_fh", CTLFLAG_RWTUN,
131                     &softc->ctls.max_nfsds_per_fh, 0, "Maximum nfsd threads that "
132                     "should be working on requests for the same file handle");
133
134                 SYSCTL_ADD_UINT(&softc->sysctl_ctx, SYSCTL_CHILDREN(softc->sysctl_tree),
135                     OID_AUTO, "max_reqs_per_nfsd", CTLFLAG_RWTUN,
136                     &softc->ctls.max_reqs_per_nfsd, 0, "Maximum requests that "
137                     "single nfsd thread should be working on at any time");
138
139                 SYSCTL_ADD_OID(&softc->sysctl_ctx, SYSCTL_CHILDREN(softc->sysctl_tree),
140                     OID_AUTO, "fhe_stats", CTLTYPE_STRING | CTLFLAG_RD | CTLFLAG_MPSAFE,
141                     0, 0, fhenew_stats_sysctl, "A", "");
142         }
143 }
144
145 static void
146 fhanew_uninit(void *foo)
147 {
148         struct fha_params *softc;
149         int i;
150
151         softc = NFSD_VNET(fhanew_softc);
152
153         sysctl_ctx_free(&softc->sysctl_ctx);
154         for (i = 0; i < FHA_HASH_SIZE; i++)
155                 mtx_destroy(&softc->fha_hash[i].mtx);
156         free(softc, M_TEMP);
157 }
158
159 static rpcproc_t
160 fhanew_get_procnum(rpcproc_t procnum)
161 {
162         if (procnum > NFSV2PROC_STATFS)
163                 return (-1);
164
165         return (newnfs_nfsv3_procid[procnum]);
166 }
167
168 static int
169 fhanew_get_fh(uint64_t *fh, int v3, struct mbuf **md, caddr_t *dpos)
170 {
171         struct nfsrv_descript lnd, *nd;
172         uint32_t *tl;
173         uint8_t *buf;
174         uint64_t t;
175         int error, len, i;
176
177         error = 0;
178         len = 0;
179         nd = &lnd;
180
181         nd->nd_md = *md;
182         nd->nd_dpos = *dpos;
183
184         if (v3) {
185                 NFSM_DISSECT_NONBLOCK(tl, uint32_t *, NFSX_UNSIGNED);
186                 if ((len = fxdr_unsigned(int, *tl)) <= 0 || len > NFSX_FHMAX) {
187                         error = EBADRPC;
188                         goto nfsmout;
189                 }
190         } else {
191                 len = NFSX_V2FH;
192         }
193
194         t = 0;
195         if (len != 0) {
196                 NFSM_DISSECT_NONBLOCK(buf, uint8_t *, len);
197                 for (i = 0; i < len; i++)
198                         t ^= ((uint64_t)buf[i] << (i & 7) * 8);
199         }
200         *fh = t;
201
202 nfsmout:
203         *md = nd->nd_md;
204         *dpos = nd->nd_dpos;
205
206         return (error);
207 }
208
209 static int
210 fhanew_is_read(rpcproc_t procnum)
211 {
212         if (procnum == NFSPROC_READ)
213                 return (1);
214         else
215                 return (0);
216 }
217
218 static int
219 fhanew_is_write(rpcproc_t procnum)
220 {
221         if (procnum == NFSPROC_WRITE)
222                 return (1);
223         else
224                 return (0);
225 }
226
227 static int
228 fhanew_get_offset(struct mbuf **md, caddr_t *dpos, int v3,
229     struct fha_info *info)
230 {
231         struct nfsrv_descript lnd, *nd;
232         uint32_t *tl;
233         int error;
234
235         error = 0;
236
237         nd = &lnd;
238         nd->nd_md = *md;
239         nd->nd_dpos = *dpos;
240
241         if (v3) {
242                 NFSM_DISSECT_NONBLOCK(tl, uint32_t *, 2 * NFSX_UNSIGNED);
243                 info->offset = fxdr_hyper(tl);
244         } else {
245                 NFSM_DISSECT_NONBLOCK(tl, uint32_t *, NFSX_UNSIGNED);
246                 info->offset = fxdr_unsigned(uint32_t, *tl);
247         }
248
249 nfsmout:
250         *md = nd->nd_md;
251         *dpos = nd->nd_dpos;
252
253         return (error);
254 }
255
256 static int
257 fhanew_no_offset(rpcproc_t procnum)
258 {
259         if (procnum == NFSPROC_FSSTAT ||
260             procnum == NFSPROC_FSINFO ||
261             procnum == NFSPROC_PATHCONF ||
262             procnum == NFSPROC_NOOP ||
263             procnum == NFSPROC_NULL)
264                 return (1);
265         else
266                 return (0);
267 }
268
269 static void
270 fhanew_set_locktype(rpcproc_t procnum, struct fha_info *info)
271 {
272         switch (procnum) {
273         case NFSPROC_NULL:
274         case NFSPROC_GETATTR:
275         case NFSPROC_LOOKUP:
276         case NFSPROC_ACCESS:
277         case NFSPROC_READLINK:
278         case NFSPROC_READ:
279         case NFSPROC_READDIR:
280         case NFSPROC_READDIRPLUS:
281         case NFSPROC_WRITE:
282                 info->locktype = LK_SHARED;
283                 break;
284         case NFSPROC_SETATTR:
285         case NFSPROC_CREATE:
286         case NFSPROC_MKDIR:
287         case NFSPROC_SYMLINK:
288         case NFSPROC_MKNOD:
289         case NFSPROC_REMOVE:
290         case NFSPROC_RMDIR:
291         case NFSPROC_RENAME:
292         case NFSPROC_LINK:
293         case NFSPROC_FSSTAT:
294         case NFSPROC_FSINFO:
295         case NFSPROC_PATHCONF:
296         case NFSPROC_COMMIT:
297         case NFSPROC_NOOP:
298                 info->locktype = LK_EXCLUSIVE;
299                 break;
300         }
301 }
302
303 /*
304  * This just specifies that offsets should obey affinity when within
305  * the same 1Mbyte (1<<20) chunk for the file (reads only for now).
306  */
307 static void
308 fha_extract_info(struct svc_req *req, struct fha_info *i)
309 {
310         struct mbuf *md;
311         caddr_t dpos;
312         static u_int64_t random_fh = 0;
313         int error;
314         int v3 = (req->rq_vers == 3);
315         rpcproc_t procnum;
316
317         /*
318          * We start off with a random fh.  If we get a reasonable
319          * procnum, we set the fh.  If there's a concept of offset
320          * that we're interested in, we set that.
321          */
322         i->fh = ++random_fh;
323         i->offset = 0;
324         i->locktype = LK_EXCLUSIVE;
325         i->read = i->write = 0;
326
327         /*
328          * Extract the procnum and convert to v3 form if necessary,
329          * taking care to deal with out-of-range procnums.  Caller will
330          * ensure that rq_vers is either 2 or 3.
331          */
332         procnum = req->rq_proc;
333         if (!v3) {
334                 rpcproc_t tmp_procnum;
335
336                 tmp_procnum = fhanew_get_procnum(procnum);
337                 if (tmp_procnum == -1)
338                         goto out;
339                 procnum = tmp_procnum;
340         }
341
342         /*
343          * We do affinity for most.  However, we divide a realm of affinity
344          * by file offset so as to allow for concurrent random access.  We
345          * only do this for reads today, but this may change when IFS supports
346          * efficient concurrent writes.
347          */
348         if (fhanew_no_offset(procnum))
349                 goto out;
350
351         i->read = fhanew_is_read(procnum);
352         i->write = fhanew_is_write(procnum);
353
354         error = newnfs_realign(&req->rq_args, M_NOWAIT);
355         if (error)
356                 goto out;
357         md = req->rq_args;
358         dpos = mtod(md, caddr_t);
359
360         /* Grab the filehandle. */
361         error = fhanew_get_fh(&i->fh, v3, &md, &dpos);
362         if (error)
363                 goto out;
364
365         /* Content ourselves with zero offset for all but reads. */
366         if (i->read || i->write)
367                 fhanew_get_offset(&md, &dpos, v3, i);
368
369 out:
370         fhanew_set_locktype(procnum, i);
371 }
372
373 static struct fha_hash_entry *
374 fha_hash_entry_new(u_int64_t fh)
375 {
376         struct fha_hash_entry *e;
377
378         e = malloc(sizeof(*e), M_NFS_FHA, M_WAITOK);
379         e->fh = fh;
380         e->num_rw = 0;
381         e->num_exclusive = 0;
382         e->num_threads = 0;
383         LIST_INIT(&e->threads);
384
385         return (e);
386 }
387
388 static void
389 fha_hash_entry_destroy(struct fha_hash_entry *e)
390 {
391
392         mtx_assert(e->mtx, MA_OWNED);
393         KASSERT(e->num_rw == 0,
394             ("%d reqs on destroyed fhe %p", e->num_rw, e));
395         KASSERT(e->num_exclusive == 0,
396             ("%d exclusive reqs on destroyed fhe %p", e->num_exclusive, e));
397         KASSERT(e->num_threads == 0,
398             ("%d threads on destroyed fhe %p", e->num_threads, e));
399         free(e, M_NFS_FHA);
400 }
401
402 static void
403 fha_hash_entry_remove(struct fha_hash_entry *e)
404 {
405
406         mtx_assert(e->mtx, MA_OWNED);
407         LIST_REMOVE(e, link);
408         fha_hash_entry_destroy(e);
409 }
410
411 static struct fha_hash_entry *
412 fha_hash_entry_lookup(struct fha_params *softc, u_int64_t fh)
413 {
414         struct fha_hash_slot *fhs;
415         struct fha_hash_entry *fhe, *new_fhe;
416
417         fhs = &softc->fha_hash[fh % FHA_HASH_SIZE];
418         new_fhe = fha_hash_entry_new(fh);
419         new_fhe->mtx = &fhs->mtx;
420         mtx_lock(&fhs->mtx);
421         LIST_FOREACH(fhe, &fhs->list, link)
422                 if (fhe->fh == fh)
423                         break;
424         if (!fhe) {
425                 fhe = new_fhe;
426                 LIST_INSERT_HEAD(&fhs->list, fhe, link);
427         } else
428                 fha_hash_entry_destroy(new_fhe);
429         return (fhe);
430 }
431
432 static void
433 fha_hash_entry_add_thread(struct fha_hash_entry *fhe, SVCTHREAD *thread)
434 {
435
436         mtx_assert(fhe->mtx, MA_OWNED);
437         thread->st_p2 = 0;
438         LIST_INSERT_HEAD(&fhe->threads, thread, st_alink);
439         fhe->num_threads++;
440 }
441
442 static void
443 fha_hash_entry_remove_thread(struct fha_hash_entry *fhe, SVCTHREAD *thread)
444 {
445
446         mtx_assert(fhe->mtx, MA_OWNED);
447         KASSERT(thread->st_p2 == 0,
448             ("%d reqs on removed thread %p", thread->st_p2, thread));
449         LIST_REMOVE(thread, st_alink);
450         fhe->num_threads--;
451 }
452
453 /*
454  * Account for an ongoing operation associated with this file.
455  */
456 static void
457 fha_hash_entry_add_op(struct fha_hash_entry *fhe, int locktype, int count)
458 {
459
460         mtx_assert(fhe->mtx, MA_OWNED);
461         if (LK_EXCLUSIVE == locktype)
462                 fhe->num_exclusive += count;
463         else
464                 fhe->num_rw += count;
465 }
466
467 /*
468  * Get the service thread currently associated with the fhe that is
469  * appropriate to handle this operation.
470  */
471 static SVCTHREAD *
472 fha_hash_entry_choose_thread(struct fha_params *softc,
473     struct fha_hash_entry *fhe, struct fha_info *i, SVCTHREAD *this_thread)
474 {
475         SVCTHREAD *thread, *min_thread = NULL;
476         int req_count, min_count = 0;
477         off_t offset1, offset2;
478
479         LIST_FOREACH(thread, &fhe->threads, st_alink) {
480                 req_count = thread->st_p2;
481
482                 /* If there are any writes in progress, use the first thread. */
483                 if (fhe->num_exclusive) {
484 #if 0
485                         ITRACE_CURPROC(ITRACE_NFS, ITRACE_INFO,
486                             "fha: %p(%d)w", thread, req_count);
487 #endif
488                         return (thread);
489                 }
490
491                 /* Check whether we should consider locality. */
492                 if ((i->read && !softc->ctls.read) ||
493                     (i->write && !softc->ctls.write))
494                         goto noloc;
495
496                 /*
497                  * Check for locality, making sure that we won't
498                  * exceed our per-thread load limit in the process.
499                  */
500                 offset1 = i->offset;
501                 offset2 = thread->st_p3;
502
503                 if (((offset1 >= offset2)
504                   && ((offset1 - offset2) < (1 << softc->ctls.bin_shift)))
505                  || ((offset2 > offset1)
506                   && ((offset2 - offset1) < (1 << softc->ctls.bin_shift)))) {
507                         if ((softc->ctls.max_reqs_per_nfsd == 0) ||
508                             (req_count < softc->ctls.max_reqs_per_nfsd)) {
509 #if 0
510                                 ITRACE_CURPROC(ITRACE_NFS, ITRACE_INFO,
511                                     "fha: %p(%d)r", thread, req_count);
512 #endif
513                                 return (thread);
514                         }
515                 }
516
517 noloc:
518                 /*
519                  * We don't have a locality match, so skip this thread,
520                  * but keep track of the most attractive thread in case
521                  * we need to come back to it later.
522                  */
523 #if 0
524                 ITRACE_CURPROC(ITRACE_NFS, ITRACE_INFO,
525                     "fha: %p(%d)s off1 %llu off2 %llu", thread,
526                     req_count, offset1, offset2);
527 #endif
528                 if ((min_thread == NULL) || (req_count < min_count)) {
529                         min_count = req_count;
530                         min_thread = thread;
531                 }
532         }
533
534         /*
535          * We didn't find a good match yet.  See if we can add
536          * a new thread to this file handle entry's thread list.
537          */
538         if ((softc->ctls.max_nfsds_per_fh == 0) ||
539             (fhe->num_threads < softc->ctls.max_nfsds_per_fh)) {
540                 thread = this_thread;
541 #if 0
542                 ITRACE_CURPROC(ITRACE_NFS, ITRACE_INFO,
543                     "fha: %p(%d)t", thread, thread->st_p2);
544 #endif
545                 fha_hash_entry_add_thread(fhe, thread);
546         } else {
547                 /*
548                  * We don't want to use any more threads for this file, so
549                  * go back to the most attractive nfsd we're already using.
550                  */
551                 thread = min_thread;
552         }
553
554         return (thread);
555 }
556
557 /*
558  * After getting a request, try to assign it to some thread.  Usually we
559  * handle it ourselves.
560  */
561 SVCTHREAD *
562 fhanew_assign(SVCTHREAD *this_thread, struct svc_req *req)
563 {
564         struct fha_params *softc;
565         SVCTHREAD *thread;
566         struct fha_info i;
567         struct fha_hash_entry *fhe;
568
569         NFSD_CURVNET_SET(NFSD_TD_TO_VNET(curthread));
570         softc = NFSD_VNET(fhanew_softc);
571         /* Check to see whether we're enabled. */
572         if (softc->ctls.enable == 0)
573                 goto thist;
574
575         /*
576          * Only do placement if this is an NFS request.
577          */
578         if (req->rq_prog != NFS_PROG)
579                 goto thist;
580
581         if (req->rq_vers != 2 && req->rq_vers != 3)
582                 goto thist;
583
584         fha_extract_info(req, &i);
585
586         /*
587          * We save the offset associated with this request for later
588          * nfsd matching.
589          */
590         fhe = fha_hash_entry_lookup(softc, i.fh);
591         req->rq_p1 = fhe;
592         req->rq_p2 = i.locktype;
593         req->rq_p3 = i.offset;
594
595         /*
596          * Choose a thread, taking into consideration locality, thread load,
597          * and the number of threads already working on this file.
598          */
599         thread = fha_hash_entry_choose_thread(softc, fhe, &i, this_thread);
600         KASSERT(thread, ("fha_assign: NULL thread!"));
601         fha_hash_entry_add_op(fhe, i.locktype, 1);
602         thread->st_p2++;
603         thread->st_p3 = i.offset;
604
605         /*
606          * Grab the pool lock here to not let chosen thread go away before
607          * the new request inserted to its queue while we drop fhe lock.
608          */
609         mtx_lock(&thread->st_lock);
610         mtx_unlock(fhe->mtx);
611
612         NFSD_CURVNET_RESTORE();
613         return (thread);
614 thist:
615         req->rq_p1 = NULL;
616         NFSD_CURVNET_RESTORE();
617         mtx_lock(&this_thread->st_lock);
618         return (this_thread);
619 }
620
621 /*
622  * Called when we're done with an operation.  The request has already
623  * been de-queued.
624  */
625 void
626 fhanew_nd_complete(SVCTHREAD *thread, struct svc_req *req)
627 {
628         struct fha_hash_entry *fhe = req->rq_p1;
629         struct mtx *mtx;
630
631         NFSD_CURVNET_SET(NFSD_TD_TO_VNET(curthread));
632         /*
633          * This may be called for reqs that didn't go through
634          * fha_assign (e.g. extra NULL ops used for RPCSEC_GSS.
635          */
636         if (!fhe) {
637                 NFSD_CURVNET_RESTORE();
638                 return;
639         }
640
641         mtx = fhe->mtx;
642         mtx_lock(mtx);
643         fha_hash_entry_add_op(fhe, req->rq_p2, -1);
644         thread->st_p2--;
645         KASSERT(thread->st_p2 >= 0, ("Negative request count %d on %p",
646             thread->st_p2, thread));
647         if (thread->st_p2 == 0) {
648                 fha_hash_entry_remove_thread(fhe, thread);
649                 if (0 == fhe->num_rw + fhe->num_exclusive)
650                         fha_hash_entry_remove(fhe);
651         }
652         mtx_unlock(mtx);
653         NFSD_CURVNET_RESTORE();
654 }
655
656 static int
657 fhenew_stats_sysctl(SYSCTL_HANDLER_ARGS)
658 {
659         struct fha_params *softc;
660         int error, i;
661         struct sbuf sb;
662         struct fha_hash_entry *fhe;
663         bool_t first, hfirst;
664         SVCTHREAD *thread;
665
666         sbuf_new(&sb, NULL, 65536, SBUF_FIXEDLEN);
667
668         NFSD_CURVNET_SET(NFSD_TD_TO_VNET(curthread));
669         softc = NFSD_VNET(fhanew_softc);
670         for (i = 0; i < FHA_HASH_SIZE; i++)
671                 if (!LIST_EMPTY(&softc->fha_hash[i].list))
672                         break;
673
674         if (i == FHA_HASH_SIZE) {
675                 sbuf_printf(&sb, "No file handle entries.\n");
676                 goto out;
677         }
678
679         hfirst = TRUE;
680         for (; i < FHA_HASH_SIZE; i++) {
681                 mtx_lock(&softc->fha_hash[i].mtx);
682                 if (LIST_EMPTY(&softc->fha_hash[i].list)) {
683                         mtx_unlock(&softc->fha_hash[i].mtx);
684                         continue;
685                 }
686                 sbuf_printf(&sb, "%shash %d: {\n", hfirst ? "" : ", ", i);
687                 first = TRUE;
688                 LIST_FOREACH(fhe, &softc->fha_hash[i].list, link) {
689                         sbuf_printf(&sb, "%sfhe %p: {\n", first ? "  " : ", ",
690                             fhe);
691                         sbuf_printf(&sb, "    fh: %ju\n", (uintmax_t) fhe->fh);
692                         sbuf_printf(&sb, "    num_rw/exclusive: %d/%d\n",
693                             fhe->num_rw, fhe->num_exclusive);
694                         sbuf_printf(&sb, "    num_threads: %d\n",
695                             fhe->num_threads);
696
697                         LIST_FOREACH(thread, &fhe->threads, st_alink) {
698                                 sbuf_printf(&sb, "      thread %p offset %ju "
699                                     "reqs %d\n", thread,
700                                     thread->st_p3, thread->st_p2);
701                         }
702
703                         sbuf_printf(&sb, "  }");
704                         first = FALSE;
705                 }
706                 sbuf_printf(&sb, "\n}");
707                 mtx_unlock(&softc->fha_hash[i].mtx);
708                 hfirst = FALSE;
709         }
710
711  out:
712         NFSD_CURVNET_RESTORE();
713         sbuf_trim(&sb);
714         sbuf_finish(&sb);
715         error = sysctl_handle_string(oidp, sbuf_data(&sb), sbuf_len(&sb), req);
716         sbuf_delete(&sb);
717         return (error);
718 }