]> CyberLeo.Net >> Repos - FreeBSD/releng/8.0.git/blob - sys/nfsserver/nfs_fha.c
Adjust to reflect 8.0-RELEASE.
[FreeBSD/releng/8.0.git] / sys / nfsserver / nfs_fha.c
1 /*-
2  * Copyright (c) 2008 Isilon Inc http://www.isilon.com/
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions
6  * are met:
7  * 1. Redistributions of source code must retain the above copyright
8  *    notice, this list of conditions and the following disclaimer.
9  * 2. Redistributions in binary form must reproduce the above copyright
10  *    notice, this list of conditions and the following disclaimer in the
11  *    documentation and/or other materials provided with the distribution.
12  *
13  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
14  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
15  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
16  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
17  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
18  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
19  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
20  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
21  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
22  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
23  * SUCH DAMAGE.
24  */
25
26 #include <sys/cdefs.h>
27 __FBSDID("$FreeBSD$");
28
29 #include <sys/param.h>
30 #include <sys/systm.h>
31 #include <sys/sysproto.h>
32 #include <sys/kernel.h>
33 #include <sys/sysctl.h>
34 #include <sys/vnode.h>
35 #include <sys/malloc.h>
36 #include <sys/mount.h>
37 #include <sys/mbuf.h>
38 #include <sys/sbuf.h>
39
40 #include <rpc/rpc.h>
41 #include <nfs/xdr_subs.h>
42 #include <nfs/nfsproto.h>
43 #include <nfsserver/nfs.h>
44 #include <nfsserver/nfsm_subs.h>
45 #include <nfsserver/nfs_fha.h>
46
47 static MALLOC_DEFINE(M_NFS_FHA, "NFS FHA", "NFS FHA");
48
49 /* Sysctl defaults. */
50 #define DEF_BIN_SHIFT           18 /* 256k */
51 #define DEF_MAX_NFSDS_PER_FH    8
52 #define DEF_MAX_REQS_PER_NFSD   4
53
54 struct fha_ctls {
55         u_int32_t bin_shift;
56         u_int32_t max_nfsds_per_fh;
57         u_int32_t max_reqs_per_nfsd;
58 } fha_ctls;
59
60 struct sysctl_ctx_list fha_clist;
61
62 SYSCTL_DECL(_vfs_nfsrv);
63 SYSCTL_DECL(_vfs_nfsrv_fha);
64
65 /* Static sysctl node for the fha from the top-level vfs_nfsrv node. */
66 SYSCTL_NODE(_vfs_nfsrv, OID_AUTO, fha, CTLFLAG_RD, 0, "fha node");
67
68 /* This is the global structure that represents the state of the fha system. */
69 static struct fha_global {
70         struct fha_hash_entry_list *hashtable;
71         u_long hashmask;
72 } g_fha;
73
74 /* 
75  * These are the entries in the filehandle hash. They talk about a specific 
76  * file, requests against which are being handled by one or more nfsds. We keep
77  * a chain of nfsds against the file. We only have more than one if reads are 
78  * ongoing, and then only if the reads affect disparate regions of the file.
79  *
80  * In general, we want to assign a new request to an existing nfsd if it is 
81  * going to contend with work happening already on that nfsd, or if the 
82  * operation is a read and the nfsd is already handling a proximate read. We 
83  * do this to avoid jumping around in the read stream unnecessarily, and to 
84  * avoid contention between threads over single files.
85  */
86 struct fha_hash_entry {
87         LIST_ENTRY(fha_hash_entry) link;
88         u_int64_t fh;
89         u_int16_t num_reads;
90         u_int16_t num_writes;
91         u_int8_t num_threads;
92         struct svcthread_list threads;
93 };
94 LIST_HEAD(fha_hash_entry_list, fha_hash_entry);
95
96 /* A structure used for passing around data internally. */
97 struct fha_info {
98         u_int64_t fh;
99         off_t offset;
100         int locktype;
101 };
102
103 static int fhe_stats_sysctl(SYSCTL_HANDLER_ARGS);
104  
105 static void
106 nfs_fha_init(void *foo)
107 {
108
109         /*
110          * A small hash table to map filehandles to fha_hash_entry
111          * structures.
112          */
113         g_fha.hashtable = hashinit(256, M_NFS_FHA, &g_fha.hashmask);
114
115         /*
116          * Initialize the sysctl context list for the fha module.
117          */
118         sysctl_ctx_init(&fha_clist);
119
120         fha_ctls.bin_shift = DEF_BIN_SHIFT;
121         fha_ctls.max_nfsds_per_fh = DEF_MAX_NFSDS_PER_FH;
122         fha_ctls.max_reqs_per_nfsd = DEF_MAX_REQS_PER_NFSD;
123
124         SYSCTL_ADD_UINT(&fha_clist, SYSCTL_STATIC_CHILDREN(_vfs_nfsrv_fha),
125             OID_AUTO, "bin_shift", CTLFLAG_RW,
126             &fha_ctls.bin_shift, 0, "For FHA reads, no two requests will "
127             "contend if they're 2^(bin_shift) bytes apart");
128
129         SYSCTL_ADD_UINT(&fha_clist, SYSCTL_STATIC_CHILDREN(_vfs_nfsrv_fha),
130             OID_AUTO, "max_nfsds_per_fh", CTLFLAG_RW,
131             &fha_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(&fha_clist, SYSCTL_STATIC_CHILDREN(_vfs_nfsrv_fha),
135             OID_AUTO, "max_reqs_per_nfsd", CTLFLAG_RW,
136             &fha_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(&fha_clist, SYSCTL_STATIC_CHILDREN(_vfs_nfsrv_fha), 
140             OID_AUTO, "fhe_stats", CTLTYPE_STRING | CTLFLAG_RD, 0, 0,
141             fhe_stats_sysctl, "A", "");
142 }
143
144 static void
145 nfs_fha_uninit(void *foo)
146 {
147
148         hashdestroy(g_fha.hashtable, M_NFS_FHA, g_fha.hashmask);
149 }
150
151 SYSINIT(nfs_fha, SI_SUB_ROOT_CONF, SI_ORDER_ANY, nfs_fha_init, NULL);
152 SYSUNINIT(nfs_fha, SI_SUB_ROOT_CONF, SI_ORDER_ANY, nfs_fha_uninit, NULL);
153
154 /* 
155  * This just specifies that offsets should obey affinity when within
156  * the same 1Mbyte (1<<20) chunk for the file (reads only for now).
157  */
158 static void
159 fha_extract_info(struct svc_req *req, struct fha_info *i)
160 {
161         struct mbuf *md = req->rq_args;
162         nfsfh_t fh;
163         caddr_t dpos = mtod(md, caddr_t);
164         static u_int64_t random_fh = 0;
165         int error;
166         int v3 = (req->rq_vers == 3);
167         u_int32_t *tl;
168         rpcproc_t procnum;
169
170         /* 
171          * We start off with a random fh. If we get a reasonable
172          * procnum, we set the fh. If there's a concept of offset 
173          * that we're interested in, we set that.
174          */
175         i->fh = ++random_fh;
176         i->offset = 0;
177         i->locktype = LK_EXCLUSIVE;
178         
179         /*
180          * Extract the procnum and convert to v3 form if necessary,
181          * taking care to deal with out-of-range procnums. Caller will
182          * ensure that rq_vers is either 2 or 3.
183          */
184         procnum = req->rq_proc;
185         if (!v3) {
186                 if (procnum > NFSV2PROC_STATFS)
187                         goto out;
188                 procnum = nfsrv_nfsv3_procid[procnum];
189         }
190
191         /* 
192          * We do affinity for most. However, we divide a realm of affinity 
193          * by file offset so as to allow for concurrent random access. We 
194          * only do this for reads today, but this may change when IFS supports 
195          * efficient concurrent writes.
196          */
197         if (procnum == NFSPROC_FSSTAT ||
198             procnum == NFSPROC_FSINFO ||
199             procnum == NFSPROC_PATHCONF ||
200             procnum == NFSPROC_NOOP || 
201             procnum == NFSPROC_NULL)
202                 goto out;
203         
204         /* Grab the filehandle. */
205         error = nfsm_srvmtofh_xx(&fh.fh_generic, v3, &md, &dpos);
206         if (error)
207                 goto out;
208
209         i->fh = *(const u_int64_t *)(fh.fh_generic.fh_fid.fid_data);
210
211         /* Content ourselves with zero offset for all but reads. */
212         if (procnum != NFSPROC_READ)
213                 goto out;
214
215         if (v3) {
216                 tl = nfsm_dissect_xx_nonblock(2 * NFSX_UNSIGNED, &md, &dpos);
217                 if (tl == NULL)
218                         goto out;
219                 i->offset = fxdr_hyper(tl);
220         } else {
221                 tl = nfsm_dissect_xx_nonblock(NFSX_UNSIGNED, &md, &dpos);
222                 if (tl == NULL)
223                         goto out;
224                 i->offset = fxdr_unsigned(u_int32_t, *tl);
225         }
226  out:
227         switch (procnum) {
228         case NFSPROC_NULL:
229         case NFSPROC_GETATTR:
230         case NFSPROC_LOOKUP:
231         case NFSPROC_ACCESS:
232         case NFSPROC_READLINK:
233         case NFSPROC_READ:
234         case NFSPROC_READDIR:
235         case NFSPROC_READDIRPLUS:
236                 i->locktype = LK_SHARED;
237                 break;
238         case NFSPROC_SETATTR:
239         case NFSPROC_WRITE:
240         case NFSPROC_CREATE:
241         case NFSPROC_MKDIR:
242         case NFSPROC_SYMLINK:
243         case NFSPROC_MKNOD:
244         case NFSPROC_REMOVE:
245         case NFSPROC_RMDIR:
246         case NFSPROC_RENAME:
247         case NFSPROC_LINK:
248         case NFSPROC_FSSTAT:
249         case NFSPROC_FSINFO:
250         case NFSPROC_PATHCONF:
251         case NFSPROC_COMMIT:
252         case NFSPROC_NOOP:
253                 i->locktype = LK_EXCLUSIVE;
254                 break;
255         }
256 }
257
258 static struct fha_hash_entry *
259 fha_hash_entry_new(u_int64_t fh)
260 {
261         struct fha_hash_entry *e;
262
263         e = malloc(sizeof(*e), M_NFS_FHA, M_WAITOK);
264         e->fh = fh;
265         e->num_reads = 0;
266         e->num_writes = 0;
267         e->num_threads = 0;
268         LIST_INIT(&e->threads);
269         
270         return e;
271 }
272
273 static void
274 fha_hash_entry_destroy(struct fha_hash_entry *e)
275 {
276
277         if (e->num_reads + e->num_writes)
278                 panic("nonempty fhe");
279         free(e, M_NFS_FHA);
280 }
281
282 static void
283 fha_hash_entry_remove(struct fha_hash_entry *e)
284 {
285
286         LIST_REMOVE(e, link);
287         fha_hash_entry_destroy(e);
288 }
289
290 static struct fha_hash_entry *
291 fha_hash_entry_lookup(SVCPOOL *pool, u_int64_t fh)
292 {
293         struct fha_hash_entry *fhe, *new_fhe;
294
295         LIST_FOREACH(fhe, &g_fha.hashtable[fh % g_fha.hashmask], link) {
296                 if (fhe->fh == fh)
297                         break;
298         }
299
300         if (!fhe) {
301                 /* Allocate a new entry. */
302                 mtx_unlock(&pool->sp_lock);
303                 new_fhe = fha_hash_entry_new(fh);
304                 mtx_lock(&pool->sp_lock);
305
306                 /* Double-check to make sure we still need the new entry. */
307                 LIST_FOREACH(fhe, &g_fha.hashtable[fh % g_fha.hashmask], link) {
308                         if (fhe->fh == fh)
309                                 break;
310                 }
311                 if (!fhe) {
312                         fhe = new_fhe;
313                         LIST_INSERT_HEAD(&g_fha.hashtable[fh % g_fha.hashmask],
314                             fhe, link);
315                 } else {
316                         fha_hash_entry_destroy(new_fhe);
317                 }
318         }
319
320         return fhe;
321 }
322
323 static void
324 fha_hash_entry_add_thread(struct fha_hash_entry *fhe, SVCTHREAD *thread)
325 {
326         LIST_INSERT_HEAD(&fhe->threads, thread, st_alink);
327         fhe->num_threads++;
328 }
329
330 static void
331 fha_hash_entry_remove_thread(struct fha_hash_entry *fhe, SVCTHREAD *thread)
332 {
333
334         LIST_REMOVE(thread, st_alink);
335         fhe->num_threads--;
336 }
337
338 /* 
339  * Account for an ongoing operation associated with this file.
340  */
341 static void
342 fha_hash_entry_add_op(struct fha_hash_entry *fhe, int locktype, int count)
343 {
344
345         if (LK_EXCLUSIVE == locktype)
346                 fhe->num_writes += count;
347         else
348                 fhe->num_reads += count;
349 }
350
351 static SVCTHREAD *
352 get_idle_thread(SVCPOOL *pool)
353 {
354         SVCTHREAD *st;
355
356         LIST_FOREACH(st, &pool->sp_idlethreads, st_ilink) {
357                 if (st->st_xprt == NULL && STAILQ_EMPTY(&st->st_reqs))
358                         return (st);
359         }
360         return (NULL);
361 }
362
363
364 /* 
365  * Get the service thread currently associated with the fhe that is
366  * appropriate to handle this operation.
367  */
368 SVCTHREAD *
369 fha_hash_entry_choose_thread(SVCPOOL *pool, struct fha_hash_entry *fhe,
370     struct fha_info *i, SVCTHREAD *this_thread);
371
372 SVCTHREAD *
373 fha_hash_entry_choose_thread(SVCPOOL *pool, struct fha_hash_entry *fhe,
374     struct fha_info *i, SVCTHREAD *this_thread)
375 {
376         SVCTHREAD *thread, *min_thread = NULL;
377         int req_count, min_count = 0;
378         off_t offset1, offset2;
379
380         LIST_FOREACH(thread, &fhe->threads, st_alink) {
381                 req_count = thread->st_reqcount;
382
383                 /* If there are any writes in progress, use the first thread. */
384                 if (fhe->num_writes) {
385 #if 0
386                         ITRACE_CURPROC(ITRACE_NFS, ITRACE_INFO, 
387                             "fha: %p(%d)w", thread, req_count);
388 #endif
389                         return (thread);
390                 }
391
392                 /* 
393                  * Check for read locality, making sure that we won't 
394                  * exceed our per-thread load limit in the process. 
395                  */
396                 offset1 = i->offset >> fha_ctls.bin_shift;
397                 offset2 = STAILQ_FIRST(&thread->st_reqs)->rq_p3
398                         >> fha_ctls.bin_shift;
399                 if (offset1 == offset2) {
400                         if ((fha_ctls.max_reqs_per_nfsd == 0) ||
401                             (req_count < fha_ctls.max_reqs_per_nfsd)) {
402 #if 0
403                                 ITRACE_CURPROC(ITRACE_NFS, ITRACE_INFO, 
404                                     "fha: %p(%d)r", thread, req_count);
405 #endif
406                                 return (thread);
407                         }
408                 }
409
410                 /* 
411                  * We don't have a locality match, so skip this thread,
412                  * but keep track of the most attractive thread in case 
413                  * we need to come back to it later.
414                  */
415 #if 0
416                 ITRACE_CURPROC(ITRACE_NFS, ITRACE_INFO, 
417                     "fha: %p(%d)s off1 %llu off2 %llu", thread, 
418                     req_count, offset1, offset2);
419 #endif
420                 if ((min_thread == NULL) || (req_count < min_count)) {
421                         min_count = req_count;
422                         min_thread = thread;
423                 }
424         }
425
426         /* 
427          * We didn't find a good match yet. See if we can add 
428          * a new thread to this file handle entry's thread list.
429          */
430         if ((fha_ctls.max_nfsds_per_fh == 0) || 
431             (fhe->num_threads < fha_ctls.max_nfsds_per_fh)) {
432                 /* 
433                  * We can add a new thread, so try for an idle thread 
434                  * first, and fall back to this_thread if none are idle. 
435                  */
436                 if (STAILQ_EMPTY(&this_thread->st_reqs)) {
437                         thread = this_thread;
438 #if 0
439                         ITRACE_CURPROC(ITRACE_NFS, ITRACE_INFO, 
440                             "fha: %p(%d)t", thread, thread->st_reqcount);
441 #endif
442                 } else if ((thread = get_idle_thread(pool))) {
443 #if 0
444                         ITRACE_CURPROC(ITRACE_NFS, ITRACE_INFO, 
445                             "fha: %p(%d)i", thread, thread->st_reqcount);
446 #endif
447                 } else { 
448                         thread = this_thread;
449 #if 0
450                         ITRACE_CURPROC(ITRACE_NFS, ITRACE_INFO, 
451                             "fha: %p(%d)b", thread, thread->st_reqcount);
452 #endif
453                 }
454                 fha_hash_entry_add_thread(fhe, thread);
455         } else {
456                 /* 
457                  * We don't want to use any more threads for this file, so 
458                  * go back to the most attractive nfsd we're already using.
459                  */
460                 thread = min_thread;
461         }
462
463         return (thread);
464 }
465
466 /* 
467  * After getting a request, try to assign it to some thread. Usually we
468  * handle it ourselves.
469  */
470 SVCTHREAD *
471 fha_assign(SVCTHREAD *this_thread, struct svc_req *req)
472 {
473         SVCPOOL *pool;
474         SVCTHREAD *thread;
475         struct fha_info i;
476         struct fha_hash_entry *fhe;
477
478         /*
479          * Only do placement if this is an NFS request.
480          */
481         if (req->rq_prog != NFS_PROG)
482                 return (this_thread);
483
484         if (req->rq_vers != 2 && req->rq_vers != 3)
485                 return (this_thread);
486
487         pool = req->rq_xprt->xp_pool;
488         fha_extract_info(req, &i);
489
490         /* 
491          * We save the offset associated with this request for later 
492          * nfsd matching.
493          */
494         fhe = fha_hash_entry_lookup(pool, i.fh);
495         req->rq_p1 = fhe;
496         req->rq_p2 = i.locktype;
497         req->rq_p3 = i.offset;
498         
499         /* 
500          * Choose a thread, taking into consideration locality, thread load,
501          * and the number of threads already working on this file.
502          */
503         thread = fha_hash_entry_choose_thread(pool, fhe, &i, this_thread);
504         KASSERT(thread, ("fha_assign: NULL thread!"));
505         fha_hash_entry_add_op(fhe, i.locktype, 1);
506
507         return (thread);
508 }
509
510 /* 
511  * Called when we're done with an operation. The request has already
512  * been de-queued.
513  */
514 void
515 fha_nd_complete(SVCTHREAD *thread, struct svc_req *req)
516 {
517         struct fha_hash_entry *fhe = req->rq_p1;
518
519         /*
520          * This may be called for reqs that didn't go through
521          * fha_assign (e.g. extra NULL ops used for RPCSEC_GSS.
522          */
523         if (!fhe)
524                 return;
525
526         fha_hash_entry_add_op(fhe, req->rq_p2, -1);
527
528         if (thread->st_reqcount == 0) {
529                 fha_hash_entry_remove_thread(fhe, thread);
530                 if (0 == fhe->num_reads + fhe->num_writes)
531                         fha_hash_entry_remove(fhe);
532         }
533 }
534
535 extern SVCPOOL *nfsrv_pool;
536
537 static int
538 fhe_stats_sysctl(SYSCTL_HANDLER_ARGS)
539 {
540         int error, count, i;
541         struct sbuf sb;
542         struct fha_hash_entry *fhe;
543         bool_t first = TRUE;
544         SVCTHREAD *thread;
545
546         sbuf_new(&sb, NULL, 4096, SBUF_FIXEDLEN);
547
548         if (!nfsrv_pool) {
549                 sbuf_printf(&sb, "NFSD not running\n");
550                 goto out;
551         }
552
553         mtx_lock(&nfsrv_pool->sp_lock);
554         count = 0;
555         for (i = 0; i <= g_fha.hashmask; i++)
556                 if (!LIST_EMPTY(&g_fha.hashtable[i]))
557                         count++;
558
559         if (count == 0) {
560                 sbuf_printf(&sb, "No file handle entries.\n");
561                 goto out;
562         }
563
564         for (i = 0; i <= g_fha.hashmask; i++) {
565                 LIST_FOREACH(fhe, &g_fha.hashtable[i], link) {
566                         sbuf_printf(&sb, "%sfhe %p: {\n", first ? "" : ", ", fhe);
567
568                         sbuf_printf(&sb, "    fh: %ju\n", (uintmax_t) fhe->fh);
569                         sbuf_printf(&sb, "    num_reads: %d\n", fhe->num_reads);
570                         sbuf_printf(&sb, "    num_writes: %d\n", fhe->num_writes);
571                         sbuf_printf(&sb, "    num_threads: %d\n", fhe->num_threads);
572
573                         LIST_FOREACH(thread, &fhe->threads, st_alink) {
574                                 sbuf_printf(&sb, "    thread %p (count %d)\n",
575                                     thread, thread->st_reqcount);
576                         }
577
578                         sbuf_printf(&sb, "}");
579                         first = FALSE;
580
581                         /* Limit the output. */
582                         if (++count > 128) {
583                                 sbuf_printf(&sb, "...");
584                                 break;
585                         }
586                 }
587         }
588
589  out:
590         if (nfsrv_pool)
591                 mtx_unlock(&nfsrv_pool->sp_lock);
592         sbuf_trim(&sb);
593         sbuf_finish(&sb);
594         error = sysctl_handle_string(oidp, sbuf_data(&sb), sbuf_len(&sb), req);
595         sbuf_delete(&sb);
596         return (error);
597 }