2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
4 * Copyright (c) 2008 Isilon Inc http://www.isilon.com/
5 * Copyright (c) 2013 Spectra Logic Corporation
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
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.
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
29 #include <sys/cdefs.h>
30 __FBSDID("$FreeBSD$");
32 #include <sys/types.h>
36 #include <fs/nfs/nfsport.h>
37 #include <fs/nfsserver/nfs_fha_new.h>
41 static MALLOC_DEFINE(M_NFS_FHA, "NFS FHA", "NFS FHA");
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,
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,
59 NFSD_VNET_DEFINE_STATIC(struct fha_params *, fhanew_softc);
61 SYSCTL_DECL(_vfs_nfsd);
63 extern int newnfs_nfsv3_procid[];
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);
69 fhanew_init(void *foo)
71 struct fha_params *softc;
74 NFSD_VNET(fhanew_softc) = malloc(sizeof(struct fha_params), M_TEMP,
76 softc = NFSD_VNET(fhanew_softc);
78 snprintf(softc->server_name, sizeof(softc->server_name),
82 * Initialize the sysctl context list for the fha module.
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__);
95 for (i = 0; i < FHA_HASH_SIZE; i++)
96 mtx_init(&softc->fha_hash[i].mtx, "fhalock", NULL, MTX_DEF);
99 * Set the default tuning parameters.
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;
109 * Add sysctls so the user can change the tuning parameters.
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)");
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");
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");
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");
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");
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");
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", "");
146 fhanew_uninit(void *foo)
148 struct fha_params *softc;
151 softc = NFSD_VNET(fhanew_softc);
153 sysctl_ctx_free(&softc->sysctl_ctx);
154 for (i = 0; i < FHA_HASH_SIZE; i++)
155 mtx_destroy(&softc->fha_hash[i].mtx);
160 fhanew_get_procnum(rpcproc_t procnum)
162 if (procnum > NFSV2PROC_STATFS)
165 return (newnfs_nfsv3_procid[procnum]);
169 fhanew_get_fh(uint64_t *fh, int v3, struct mbuf **md, caddr_t *dpos)
171 struct nfsrv_descript lnd, *nd;
185 NFSM_DISSECT_NONBLOCK(tl, uint32_t *, NFSX_UNSIGNED);
186 if ((len = fxdr_unsigned(int, *tl)) <= 0 || len > NFSX_FHMAX) {
196 NFSM_DISSECT_NONBLOCK(buf, uint8_t *, len);
197 for (i = 0; i < len; i++)
198 t ^= ((uint64_t)buf[i] << (i & 7) * 8);
210 fhanew_is_read(rpcproc_t procnum)
212 if (procnum == NFSPROC_READ)
219 fhanew_is_write(rpcproc_t procnum)
221 if (procnum == NFSPROC_WRITE)
228 fhanew_get_offset(struct mbuf **md, caddr_t *dpos, int v3,
229 struct fha_info *info)
231 struct nfsrv_descript lnd, *nd;
242 NFSM_DISSECT_NONBLOCK(tl, uint32_t *, 2 * NFSX_UNSIGNED);
243 info->offset = fxdr_hyper(tl);
245 NFSM_DISSECT_NONBLOCK(tl, uint32_t *, NFSX_UNSIGNED);
246 info->offset = fxdr_unsigned(uint32_t, *tl);
257 fhanew_no_offset(rpcproc_t procnum)
259 if (procnum == NFSPROC_FSSTAT ||
260 procnum == NFSPROC_FSINFO ||
261 procnum == NFSPROC_PATHCONF ||
262 procnum == NFSPROC_NOOP ||
263 procnum == NFSPROC_NULL)
270 fhanew_set_locktype(rpcproc_t procnum, struct fha_info *info)
274 case NFSPROC_GETATTR:
277 case NFSPROC_READLINK:
279 case NFSPROC_READDIR:
280 case NFSPROC_READDIRPLUS:
282 info->locktype = LK_SHARED;
284 case NFSPROC_SETATTR:
287 case NFSPROC_SYMLINK:
295 case NFSPROC_PATHCONF:
298 info->locktype = LK_EXCLUSIVE;
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).
308 fha_extract_info(struct svc_req *req, struct fha_info *i)
312 static u_int64_t random_fh = 0;
314 int v3 = (req->rq_vers == 3);
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.
324 i->locktype = LK_EXCLUSIVE;
325 i->read = i->write = 0;
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.
332 procnum = req->rq_proc;
334 rpcproc_t tmp_procnum;
336 tmp_procnum = fhanew_get_procnum(procnum);
337 if (tmp_procnum == -1)
339 procnum = tmp_procnum;
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.
348 if (fhanew_no_offset(procnum))
351 i->read = fhanew_is_read(procnum);
352 i->write = fhanew_is_write(procnum);
354 error = newnfs_realign(&req->rq_args, M_NOWAIT);
358 dpos = mtod(md, caddr_t);
360 /* Grab the filehandle. */
361 error = fhanew_get_fh(&i->fh, v3, &md, &dpos);
365 /* Content ourselves with zero offset for all but reads. */
366 if (i->read || i->write)
367 fhanew_get_offset(&md, &dpos, v3, i);
370 fhanew_set_locktype(procnum, i);
373 static struct fha_hash_entry *
374 fha_hash_entry_new(u_int64_t fh)
376 struct fha_hash_entry *e;
378 e = malloc(sizeof(*e), M_NFS_FHA, M_WAITOK);
381 e->num_exclusive = 0;
383 LIST_INIT(&e->threads);
389 fha_hash_entry_destroy(struct fha_hash_entry *e)
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));
403 fha_hash_entry_remove(struct fha_hash_entry *e)
406 mtx_assert(e->mtx, MA_OWNED);
407 LIST_REMOVE(e, link);
408 fha_hash_entry_destroy(e);
411 static struct fha_hash_entry *
412 fha_hash_entry_lookup(struct fha_params *softc, u_int64_t fh)
414 struct fha_hash_slot *fhs;
415 struct fha_hash_entry *fhe, *new_fhe;
417 fhs = &softc->fha_hash[fh % FHA_HASH_SIZE];
418 new_fhe = fha_hash_entry_new(fh);
419 new_fhe->mtx = &fhs->mtx;
421 LIST_FOREACH(fhe, &fhs->list, link)
426 LIST_INSERT_HEAD(&fhs->list, fhe, link);
428 fha_hash_entry_destroy(new_fhe);
433 fha_hash_entry_add_thread(struct fha_hash_entry *fhe, SVCTHREAD *thread)
436 mtx_assert(fhe->mtx, MA_OWNED);
438 LIST_INSERT_HEAD(&fhe->threads, thread, st_alink);
443 fha_hash_entry_remove_thread(struct fha_hash_entry *fhe, SVCTHREAD *thread)
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);
454 * Account for an ongoing operation associated with this file.
457 fha_hash_entry_add_op(struct fha_hash_entry *fhe, int locktype, int count)
460 mtx_assert(fhe->mtx, MA_OWNED);
461 if (LK_EXCLUSIVE == locktype)
462 fhe->num_exclusive += count;
464 fhe->num_rw += count;
468 * Get the service thread currently associated with the fhe that is
469 * appropriate to handle this operation.
472 fha_hash_entry_choose_thread(struct fha_params *softc,
473 struct fha_hash_entry *fhe, struct fha_info *i, SVCTHREAD *this_thread)
475 SVCTHREAD *thread, *min_thread = NULL;
476 int req_count, min_count = 0;
477 off_t offset1, offset2;
479 LIST_FOREACH(thread, &fhe->threads, st_alink) {
480 req_count = thread->st_p2;
482 /* If there are any writes in progress, use the first thread. */
483 if (fhe->num_exclusive) {
485 ITRACE_CURPROC(ITRACE_NFS, ITRACE_INFO,
486 "fha: %p(%d)w", thread, req_count);
491 /* Check whether we should consider locality. */
492 if ((i->read && !softc->ctls.read) ||
493 (i->write && !softc->ctls.write))
497 * Check for locality, making sure that we won't
498 * exceed our per-thread load limit in the process.
501 offset2 = thread->st_p3;
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)) {
510 ITRACE_CURPROC(ITRACE_NFS, ITRACE_INFO,
511 "fha: %p(%d)r", thread, req_count);
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.
524 ITRACE_CURPROC(ITRACE_NFS, ITRACE_INFO,
525 "fha: %p(%d)s off1 %llu off2 %llu", thread,
526 req_count, offset1, offset2);
528 if ((min_thread == NULL) || (req_count < min_count)) {
529 min_count = req_count;
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.
538 if ((softc->ctls.max_nfsds_per_fh == 0) ||
539 (fhe->num_threads < softc->ctls.max_nfsds_per_fh)) {
540 thread = this_thread;
542 ITRACE_CURPROC(ITRACE_NFS, ITRACE_INFO,
543 "fha: %p(%d)t", thread, thread->st_p2);
545 fha_hash_entry_add_thread(fhe, thread);
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.
558 * After getting a request, try to assign it to some thread. Usually we
559 * handle it ourselves.
562 fhanew_assign(SVCTHREAD *this_thread, struct svc_req *req)
564 struct fha_params *softc;
567 struct fha_hash_entry *fhe;
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)
576 * Only do placement if this is an NFS request.
578 if (req->rq_prog != NFS_PROG)
581 if (req->rq_vers != 2 && req->rq_vers != 3)
584 fha_extract_info(req, &i);
587 * We save the offset associated with this request for later
590 fhe = fha_hash_entry_lookup(softc, i.fh);
592 req->rq_p2 = i.locktype;
593 req->rq_p3 = i.offset;
596 * Choose a thread, taking into consideration locality, thread load,
597 * and the number of threads already working on this file.
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);
603 thread->st_p3 = i.offset;
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.
609 mtx_lock(&thread->st_lock);
610 mtx_unlock(fhe->mtx);
612 NFSD_CURVNET_RESTORE();
616 NFSD_CURVNET_RESTORE();
617 mtx_lock(&this_thread->st_lock);
618 return (this_thread);
622 * Called when we're done with an operation. The request has already
626 fhanew_nd_complete(SVCTHREAD *thread, struct svc_req *req)
628 struct fha_hash_entry *fhe = req->rq_p1;
631 NFSD_CURVNET_SET(NFSD_TD_TO_VNET(curthread));
633 * This may be called for reqs that didn't go through
634 * fha_assign (e.g. extra NULL ops used for RPCSEC_GSS.
637 NFSD_CURVNET_RESTORE();
643 fha_hash_entry_add_op(fhe, req->rq_p2, -1);
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);
653 NFSD_CURVNET_RESTORE();
657 fhenew_stats_sysctl(SYSCTL_HANDLER_ARGS)
659 struct fha_params *softc;
662 struct fha_hash_entry *fhe;
663 bool_t first, hfirst;
666 sbuf_new(&sb, NULL, 65536, SBUF_FIXEDLEN);
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))
674 if (i == FHA_HASH_SIZE) {
675 sbuf_printf(&sb, "No file handle entries.\n");
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);
686 sbuf_printf(&sb, "%shash %d: {\n", hfirst ? "" : ", ", i);
688 LIST_FOREACH(fhe, &softc->fha_hash[i].list, link) {
689 sbuf_printf(&sb, "%sfhe %p: {\n", first ? " " : ", ",
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",
697 LIST_FOREACH(thread, &fhe->threads, st_alink) {
698 sbuf_printf(&sb, " thread %p offset %ju "
700 thread->st_p3, thread->st_p2);
703 sbuf_printf(&sb, " }");
706 sbuf_printf(&sb, "\n}");
707 mtx_unlock(&softc->fha_hash[i].mtx);
712 NFSD_CURVNET_RESTORE();
715 error = sysctl_handle_string(oidp, sbuf_data(&sb), sbuf_len(&sb), req);