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
77 * keep a chain of nfsds against the file. We only have more than one if reads
78 * are ongoing, and then only if the reads affect disparate regions of the
79 * file.
80 *
81 * In general, we want to assign a new request to an existing nfsd if it is
82 * going to contend with work happening already on that nfsd, or if the
83 * operation is a read and the nfsd is already handling a proximate read. We
84 * do this to avoid jumping around in the read stream unnecessarily, and to
85 * avoid contention between threads over single files.
86 */
87 struct fha_hash_entry {
88 LIST_ENTRY(fha_hash_entry) link;
89 u_int64_t fh;
90 u_int16_t num_reads;
91 u_int16_t num_writes;
92 u_int8_t num_threads;
93 struct svcthread_list threads;
94 };
95 LIST_HEAD(fha_hash_entry_list, fha_hash_entry);
96
97 /* A structure used for passing around data internally. */
98 struct fha_info {
99 u_int64_t fh;
100 off_t offset;
101 int locktype;
102 };
103
104 static int fhe_stats_sysctl(SYSCTL_HANDLER_ARGS);
105
106 static void
107 nfs_fha_init(void *foo)
108 {
109
110 /*
111 * A small hash table to map filehandles to fha_hash_entry
112 * structures.
113 */
114 g_fha.hashtable = hashinit(256, M_NFS_FHA, &g_fha.hashmask);
115
116 /*
117 * Initialize the sysctl context list for the fha module.
118 */
119 sysctl_ctx_init(&fha_clist);
120
121 fha_ctls.bin_shift = DEF_BIN_SHIFT;
122 fha_ctls.max_nfsds_per_fh = DEF_MAX_NFSDS_PER_FH;
123 fha_ctls.max_reqs_per_nfsd = DEF_MAX_REQS_PER_NFSD;
124
125 SYSCTL_ADD_UINT(&fha_clist, SYSCTL_STATIC_CHILDREN(_vfs_nfsrv_fha),
126 OID_AUTO, "bin_shift", CTLFLAG_RW,
127 &fha_ctls.bin_shift, 0, "For FHA reads, no two requests will "
128 "contend if they're 2^(bin_shift) bytes apart");
129
130 SYSCTL_ADD_UINT(&fha_clist, SYSCTL_STATIC_CHILDREN(_vfs_nfsrv_fha),
131 OID_AUTO, "max_nfsds_per_fh", CTLFLAG_RW,
132 &fha_ctls.max_nfsds_per_fh, 0, "Maximum nfsd threads that "
133 "should be working on requests for the same file handle");
134
135 SYSCTL_ADD_UINT(&fha_clist, SYSCTL_STATIC_CHILDREN(_vfs_nfsrv_fha),
136 OID_AUTO, "max_reqs_per_nfsd", CTLFLAG_RW,
137 &fha_ctls.max_reqs_per_nfsd, 0, "Maximum requests that "
138 "single nfsd thread should be working on at any time");
139
140 SYSCTL_ADD_OID(&fha_clist, SYSCTL_STATIC_CHILDREN(_vfs_nfsrv_fha),
141 OID_AUTO, "fhe_stats", CTLTYPE_STRING | CTLFLAG_RD, 0, 0,
142 fhe_stats_sysctl, "A", "");
143 }
144
145 static void
146 nfs_fha_uninit(void *foo)
147 {
148
149 hashdestroy(g_fha.hashtable, M_NFS_FHA, g_fha.hashmask);
150 }
151
152 SYSINIT(nfs_fha, SI_SUB_ROOT_CONF, SI_ORDER_ANY, nfs_fha_init, NULL);
153 SYSUNINIT(nfs_fha, SI_SUB_ROOT_CONF, SI_ORDER_ANY, nfs_fha_uninit, NULL);
154
155 /*
156 * This just specifies that offsets should obey affinity when within
157 * the same 1Mbyte (1<<20) chunk for the file (reads only for now).
158 */
159 static void
160 fha_extract_info(struct svc_req *req, struct fha_info *i)
161 {
162 struct mbuf *md;
163 nfsfh_t fh;
164 caddr_t dpos;
165 static u_int64_t random_fh = 0;
166 int error;
167 int v3 = (req->rq_vers == 3);
168 u_int32_t *tl;
169 rpcproc_t procnum;
170
171 /*
172 * We start off with a random fh. If we get a reasonable
173 * procnum, we set the fh. If there's a concept of offset
174 * that we're interested in, we set that.
175 */
176 i->fh = ++random_fh;
177 i->offset = 0;
178 i->locktype = LK_EXCLUSIVE;
179
180 /*
181 * Extract the procnum and convert to v3 form if necessary,
182 * taking care to deal with out-of-range procnums. Caller will
183 * ensure that rq_vers is either 2 or 3.
184 */
185 procnum = req->rq_proc;
186 if (!v3) {
187 if (procnum > NFSV2PROC_STATFS)
188 goto out;
189 procnum = nfsrv_nfsv3_procid[procnum];
190 }
191
192 /*
193 * We do affinity for most. However, we divide a realm of affinity
194 * by file offset so as to allow for concurrent random access. We
195 * only do this for reads today, but this may change when IFS supports
196 * efficient concurrent writes.
197 */
198 if (procnum == NFSPROC_FSSTAT ||
199 procnum == NFSPROC_FSINFO ||
200 procnum == NFSPROC_PATHCONF ||
201 procnum == NFSPROC_NOOP ||
202 procnum == NFSPROC_NULL)
203 goto out;
204
205 error = nfs_realign(&req->rq_args, M_DONTWAIT);
206 if (error)
207 goto out;
208 md = req->rq_args;
209 dpos = mtod(md, caddr_t);
210
211 /* Grab the filehandle. */
212 error = nfsm_srvmtofh_xx(&fh.fh_generic, v3, &md, &dpos);
213 if (error)
214 goto out;
215
216 bcopy(fh.fh_generic.fh_fid.fid_data, &i->fh, sizeof(i->fh));
217
218 /* Content ourselves with zero offset for all but reads. */
219 if (procnum != NFSPROC_READ)
220 goto out;
221
222 if (v3) {
223 tl = nfsm_dissect_xx_nonblock(2 * NFSX_UNSIGNED, &md, &dpos);
224 if (tl == NULL)
225 goto out;
226 i->offset = fxdr_hyper(tl);
227 } else {
228 tl = nfsm_dissect_xx_nonblock(NFSX_UNSIGNED, &md, &dpos);
229 if (tl == NULL)
230 goto out;
231 i->offset = fxdr_unsigned(u_int32_t, *tl);
232 }
233 out:
234 switch (procnum) {
235 case NFSPROC_NULL:
236 case NFSPROC_GETATTR:
237 case NFSPROC_LOOKUP:
238 case NFSPROC_ACCESS:
239 case NFSPROC_READLINK:
240 case NFSPROC_READ:
241 case NFSPROC_READDIR:
242 case NFSPROC_READDIRPLUS:
243 i->locktype = LK_SHARED;
244 break;
245 case NFSPROC_SETATTR:
246 case NFSPROC_WRITE:
247 case NFSPROC_CREATE:
248 case NFSPROC_MKDIR:
249 case NFSPROC_SYMLINK:
250 case NFSPROC_MKNOD:
251 case NFSPROC_REMOVE:
252 case NFSPROC_RMDIR:
253 case NFSPROC_RENAME:
254 case NFSPROC_LINK:
255 case NFSPROC_FSSTAT:
256 case NFSPROC_FSINFO:
257 case NFSPROC_PATHCONF:
258 case NFSPROC_COMMIT:
259 case NFSPROC_NOOP:
260 i->locktype = LK_EXCLUSIVE;
261 break;
262 }
263 }
264
265 static struct fha_hash_entry *
266 fha_hash_entry_new(u_int64_t fh)
267 {
268 struct fha_hash_entry *e;
269
270 e = malloc(sizeof(*e), M_NFS_FHA, M_WAITOK);
271 e->fh = fh;
272 e->num_reads = 0;
273 e->num_writes = 0;
274 e->num_threads = 0;
275 LIST_INIT(&e->threads);
276
277 return (e);
278 }
279
280 static void
281 fha_hash_entry_destroy(struct fha_hash_entry *e)
282 {
283
284 if (e->num_reads + e->num_writes)
285 panic("nonempty fhe");
286 free(e, M_NFS_FHA);
287 }
288
289 static void
290 fha_hash_entry_remove(struct fha_hash_entry *e)
291 {
292
293 LIST_REMOVE(e, link);
294 fha_hash_entry_destroy(e);
295 }
296
297 static struct fha_hash_entry *
298 fha_hash_entry_lookup(SVCPOOL *pool, u_int64_t fh)
299 {
300 struct fha_hash_entry *fhe, *new_fhe;
301
302 LIST_FOREACH(fhe, &g_fha.hashtable[fh % g_fha.hashmask], link)
303 if (fhe->fh == fh)
304 break;
305
306 if (!fhe) {
307 /* Allocate a new entry. */
308 mtx_unlock(&pool->sp_lock);
309 new_fhe = fha_hash_entry_new(fh);
310 mtx_lock(&pool->sp_lock);
311
312 /* Double-check to make sure we still need the new entry. */
313 LIST_FOREACH(fhe, &g_fha.hashtable[fh % g_fha.hashmask], link)
314 if (fhe->fh == fh)
315 break;
316 if (!fhe) {
317 fhe = new_fhe;
318 LIST_INSERT_HEAD(&g_fha.hashtable[fh % g_fha.hashmask],
319 fhe, link);
320 } else
321 fha_hash_entry_destroy(new_fhe);
322 }
323
324 return (fhe);
325 }
326
327 static void
328 fha_hash_entry_add_thread(struct fha_hash_entry *fhe, SVCTHREAD *thread)
329 {
330
331 LIST_INSERT_HEAD(&fhe->threads, thread, st_alink);
332 fhe->num_threads++;
333 }
334
335 static void
336 fha_hash_entry_remove_thread(struct fha_hash_entry *fhe, SVCTHREAD *thread)
337 {
338
339 LIST_REMOVE(thread, st_alink);
340 fhe->num_threads--;
341 }
342
343 /*
344 * Account for an ongoing operation associated with this file.
345 */
346 static void
347 fha_hash_entry_add_op(struct fha_hash_entry *fhe, int locktype, int count)
348 {
349
350 if (LK_EXCLUSIVE == locktype)
351 fhe->num_writes += count;
352 else
353 fhe->num_reads += count;
354 }
355
356 static SVCTHREAD *
357 get_idle_thread(SVCPOOL *pool)
358 {
359 SVCTHREAD *st;
360
361 LIST_FOREACH(st, &pool->sp_idlethreads, st_ilink) {
362 if (st->st_xprt == NULL && STAILQ_EMPTY(&st->st_reqs))
363 return (st);
364 }
365 return (NULL);
366 }
367
368
369 /*
370 * Get the service thread currently associated with the fhe that is
371 * appropriate to handle this operation.
372 */
373 SVCTHREAD *
374 fha_hash_entry_choose_thread(SVCPOOL *pool, struct fha_hash_entry *fhe,
375 struct fha_info *i, SVCTHREAD *this_thread);
376
377 SVCTHREAD *
378 fha_hash_entry_choose_thread(SVCPOOL *pool, struct fha_hash_entry *fhe,
379 struct fha_info *i, SVCTHREAD *this_thread)
380 {
381 SVCTHREAD *thread, *min_thread = NULL;
382 int req_count, min_count = 0;
383 off_t offset1, offset2;
384
385 LIST_FOREACH(thread, &fhe->threads, st_alink) {
386 req_count = thread->st_reqcount;
387
388 /* If there are any writes in progress, use the first thread. */
389 if (fhe->num_writes) {
390 #if 0
391 ITRACE_CURPROC(ITRACE_NFS, ITRACE_INFO,
392 "fha: %p(%d)w", thread, req_count);
393 #endif
394 return (thread);
395 }
396
397 /*
398 * Check for read locality, making sure that we won't
399 * exceed our per-thread load limit in the process.
400 */
401 offset1 = i->offset >> fha_ctls.bin_shift;
402 offset2 = STAILQ_FIRST(&thread->st_reqs)->rq_p3
403 >> fha_ctls.bin_shift;
404 if (offset1 == offset2) {
405 if ((fha_ctls.max_reqs_per_nfsd == 0) ||
406 (req_count < fha_ctls.max_reqs_per_nfsd)) {
407 #if 0
408 ITRACE_CURPROC(ITRACE_NFS, ITRACE_INFO,
409 "fha: %p(%d)r", thread, req_count);
410 #endif
411 return (thread);
412 }
413 }
414
415 /*
416 * We don't have a locality match, so skip this thread,
417 * but keep track of the most attractive thread in case
418 * we need to come back to it later.
419 */
420 #if 0
421 ITRACE_CURPROC(ITRACE_NFS, ITRACE_INFO,
422 "fha: %p(%d)s off1 %llu off2 %llu", thread,
423 req_count, offset1, offset2);
424 #endif
425 if ((min_thread == NULL) || (req_count < min_count)) {
426 min_count = req_count;
427 min_thread = thread;
428 }
429 }
430
431 /*
432 * We didn't find a good match yet. See if we can add
433 * a new thread to this file handle entry's thread list.
434 */
435 if ((fha_ctls.max_nfsds_per_fh == 0) ||
436 (fhe->num_threads < fha_ctls.max_nfsds_per_fh)) {
437 /*
438 * We can add a new thread, so try for an idle thread
439 * first, and fall back to this_thread if none are idle.
440 */
441 if (STAILQ_EMPTY(&this_thread->st_reqs)) {
442 thread = this_thread;
443 #if 0
444 ITRACE_CURPROC(ITRACE_NFS, ITRACE_INFO,
445 "fha: %p(%d)t", thread, thread->st_reqcount);
446 #endif
447 } else if ((thread = get_idle_thread(pool))) {
448 #if 0
449 ITRACE_CURPROC(ITRACE_NFS, ITRACE_INFO,
450 "fha: %p(%d)i", thread, thread->st_reqcount);
451 #endif
452 } else {
453 thread = this_thread;
454 #if 0
455 ITRACE_CURPROC(ITRACE_NFS, ITRACE_INFO,
456 "fha: %p(%d)b", thread, thread->st_reqcount);
457 #endif
458 }
459 fha_hash_entry_add_thread(fhe, thread);
460 } else {
461 /*
462 * We don't want to use any more threads for this file, so
463 * go back to the most attractive nfsd we're already using.
464 */
465 thread = min_thread;
466 }
467
468 return (thread);
469 }
470
471 /*
472 * After getting a request, try to assign it to some thread. Usually we
473 * handle it ourselves.
474 */
475 SVCTHREAD *
476 fha_assign(SVCTHREAD *this_thread, struct svc_req *req)
477 {
478 SVCPOOL *pool;
479 SVCTHREAD *thread;
480 struct fha_info i;
481 struct fha_hash_entry *fhe;
482
483 /*
484 * Only do placement if this is an NFS request.
485 */
486 if (req->rq_prog != NFS_PROG)
487 return (this_thread);
488
489 if (req->rq_vers != 2 && req->rq_vers != 3)
490 return (this_thread);
491
492 pool = req->rq_xprt->xp_pool;
493 fha_extract_info(req, &i);
494
495 /*
496 * We save the offset associated with this request for later
497 * nfsd matching.
498 */
499 fhe = fha_hash_entry_lookup(pool, i.fh);
500 req->rq_p1 = fhe;
501 req->rq_p2 = i.locktype;
502 req->rq_p3 = i.offset;
503
504 /*
505 * Choose a thread, taking into consideration locality, thread load,
506 * and the number of threads already working on this file.
507 */
508 thread = fha_hash_entry_choose_thread(pool, fhe, &i, this_thread);
509 KASSERT(thread, ("fha_assign: NULL thread!"));
510 fha_hash_entry_add_op(fhe, i.locktype, 1);
511
512 return (thread);
513 }
514
515 /*
516 * Called when we're done with an operation. The request has already
517 * been de-queued.
518 */
519 void
520 fha_nd_complete(SVCTHREAD *thread, struct svc_req *req)
521 {
522 struct fha_hash_entry *fhe = req->rq_p1;
523
524 /*
525 * This may be called for reqs that didn't go through
526 * fha_assign (e.g. extra NULL ops used for RPCSEC_GSS.
527 */
528 if (!fhe)
529 return;
530
531 fha_hash_entry_add_op(fhe, req->rq_p2, -1);
532
533 if (thread->st_reqcount == 0) {
534 fha_hash_entry_remove_thread(fhe, thread);
535 if (0 == fhe->num_reads + fhe->num_writes)
536 fha_hash_entry_remove(fhe);
537 }
538 }
539
540 extern SVCPOOL *nfsrv_pool;
541
542 static int
543 fhe_stats_sysctl(SYSCTL_HANDLER_ARGS)
544 {
545 int error, count, i;
546 struct sbuf sb;
547 struct fha_hash_entry *fhe;
548 bool_t first = TRUE;
549 SVCTHREAD *thread;
550
551 sbuf_new(&sb, NULL, 4096, SBUF_FIXEDLEN);
552
553 if (!nfsrv_pool) {
554 sbuf_printf(&sb, "NFSD not running\n");
555 goto out;
556 }
557
558 mtx_lock(&nfsrv_pool->sp_lock);
559 count = 0;
560 for (i = 0; i <= g_fha.hashmask; i++)
561 if (!LIST_EMPTY(&g_fha.hashtable[i]))
562 count++;
563
564 if (count == 0) {
565 sbuf_printf(&sb, "No file handle entries.\n");
566 goto out;
567 }
568
569 for (i = 0; i <= g_fha.hashmask; i++) {
570 LIST_FOREACH(fhe, &g_fha.hashtable[i], link) {
571 sbuf_printf(&sb, "%sfhe %p: {\n", first ? "" : ", ", fhe);
572
573 sbuf_printf(&sb, " fh: %ju\n", (uintmax_t) fhe->fh);
574 sbuf_printf(&sb, " num_reads: %d\n", fhe->num_reads);
575 sbuf_printf(&sb, " num_writes: %d\n", fhe->num_writes);
576 sbuf_printf(&sb, " num_threads: %d\n", fhe->num_threads);
577
578 LIST_FOREACH(thread, &fhe->threads, st_alink) {
579 sbuf_printf(&sb, " thread %p (count %d)\n",
580 thread, thread->st_reqcount);
581 }
582
583 sbuf_printf(&sb, "}");
584 first = FALSE;
585
586 /* Limit the output. */
587 if (++count > 128) {
588 sbuf_printf(&sb, "...");
589 break;
590 }
591 }
592 }
593
594 out:
595 if (nfsrv_pool)
596 mtx_unlock(&nfsrv_pool->sp_lock);
597 sbuf_trim(&sb);
598 sbuf_finish(&sb);
599 error = sysctl_handle_string(oidp, sbuf_data(&sb), sbuf_len(&sb), req);
600 sbuf_delete(&sb);
601 return (error);
602 }
Cache object: 6070c515feb399d09f9f21cef2a16809
|