2 * Copyright (c) 2009 Robert N. M. Watson
5 * This software was developed at the University of Cambridge Computer
6 * Laboratory with support from a grant from Google, Inc.
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30 #include <sys/cdefs.h>
31 __FBSDID("$FreeBSD$");
33 #include <sys/param.h>
34 #include <sys/systm.h>
36 #include <sys/kernel.h>
37 #include <sys/malloc.h>
38 #include <sys/module.h>
40 #include <sys/dtrace.h>
41 #include <sys/dtrace_bsd.h>
43 #include <nfs/nfsproto.h>
46 * dtnfsclient is a DTrace provider that tracks the intent to perform RPCs
47 * in the NFS client, as well as acess to and maintenance of the access and
48 * attribute caches. This is not quite the same as RPCs, because NFS may
49 * issue multiple RPC transactions in the event that authentication fails,
50 * there's a jukebox error, or none at all if the access or attribute cache
51 * hits. However, it cleanly represents the logical layer between RPC
52 * transmission and vnode/vfs operations, providing access to state linking
56 static int dtnfsclient_unload(void);
57 static void dtnfsclient_getargdesc(void *, dtrace_id_t, void *,
59 static void dtnfsclient_provide(void *, dtrace_probedesc_t *);
60 static void dtnfsclient_destroy(void *, dtrace_id_t, void *);
61 static void dtnfsclient_enable(void *, dtrace_id_t, void *);
62 static void dtnfsclient_disable(void *, dtrace_id_t, void *);
63 static void dtnfsclient_load(void *);
65 static dtrace_pattr_t dtnfsclient_attr = {
66 { DTRACE_STABILITY_STABLE, DTRACE_STABILITY_STABLE, DTRACE_CLASS_COMMON },
67 { DTRACE_STABILITY_PRIVATE, DTRACE_STABILITY_PRIVATE, DTRACE_CLASS_UNKNOWN },
68 { DTRACE_STABILITY_PRIVATE, DTRACE_STABILITY_PRIVATE, DTRACE_CLASS_UNKNOWN },
69 { DTRACE_STABILITY_STABLE, DTRACE_STABILITY_STABLE, DTRACE_CLASS_COMMON },
70 { DTRACE_STABILITY_STABLE, DTRACE_STABILITY_STABLE, DTRACE_CLASS_COMMON },
74 * Description of NFSv3 and (optional) NFSv2 probes for a procedure.
76 struct dtnfsclient_rpc {
78 char *nr_v2_name; /* Or NULL if none. */
81 * IDs for the start and done cases, for both NFSv2 and NFSv3.
83 uint32_t nr_v2_id_start, nr_v2_id_done;
84 uint32_t nr_v3_id_start, nr_v3_id_done;
88 * This table is indexed by NFSv3 procedure number, but also used for NFSv2
91 static struct dtnfsclient_rpc dtnfsclient_rpcs[NFS_NPROCS] = {
93 { "getattr", "getattr" },
94 { "setattr", "setattr" },
95 { "lookup", "lookup" },
97 { "readlink", "readlink" },
100 { "create", "create" },
101 { "mkdir", "mkdir" },
102 { "symlink", "symlink" },
104 { "remove", "remove" },
105 { "rmdir", "rmdir" },
106 { "rename", "rename" },
108 { "readdir", "readdir" },
110 { "fsstat", "statfs" },
118 * Module name strings.
120 static char *dtnfsclient_accesscache_str = "accesscache";
121 static char *dtnfsclient_attrcache_str = "attrcache";
122 static char *dtnfsclient_nfs2_str = "nfs2";
123 static char *dtnfsclient_nfs3_str = "nfs3";
126 * Function name strings.
128 static char *dtnfsclient_flush_str = "flush";
129 static char *dtnfsclient_load_str = "load";
130 static char *dtnfsclient_get_str = "get";
135 static char *dtnfsclient_done_str = "done";
136 static char *dtnfsclient_hit_str = "hit";
137 static char *dtnfsclient_miss_str = "miss";
138 static char *dtnfsclient_start_str = "start";
140 static dtrace_pops_t dtnfsclient_pops = {
147 dtnfsclient_getargdesc,
153 static dtrace_provider_id_t dtnfsclient_id;
156 * Most probes are generated from the above RPC table, but for access and
157 * attribute caches, we have specific IDs we recognize and handle specially
160 extern uint32_t nfsclient_accesscache_flush_done_id;
161 extern uint32_t nfsclient_accesscache_get_hit_id;
162 extern uint32_t nfsclient_accesscache_get_miss_id;
163 extern uint32_t nfsclient_accesscache_load_done_id;
165 extern uint32_t nfsclient_attrcache_flush_done_id;
166 extern uint32_t nfsclient_attrcache_get_hit_id;
167 extern uint32_t nfsclient_attrcache_get_miss_id;
168 extern uint32_t nfsclient_attrcache_load_done_id;
171 * When tracing on a procedure is enabled, the DTrace ID for an RPC event is
172 * stored in one of these two NFS client-allocated arrays; 0 indicates that
173 * the event is not being traced so probes should not be called.
175 * For simplicity, we allocate both v2 and v3 arrays as NFS_NPROCS, and the
176 * v2 array is simply sparse.
178 extern uint32_t nfsclient_nfs2_start_probes[NFS_NPROCS];
179 extern uint32_t nfsclient_nfs2_done_probes[NFS_NPROCS];
181 extern uint32_t nfsclient_nfs3_start_probes[NFS_NPROCS];
182 extern uint32_t nfsclient_nfs3_done_probes[NFS_NPROCS];
185 * Look up a DTrace probe ID to see if it's associated with a "done" event --
186 * if so, we will return a fourth argument type of "int".
189 dtnfs23_isdoneprobe(dtrace_id_t id)
193 for (i = 0; i < NFS_NPROCS; i++) {
194 if (dtnfsclient_rpcs[i].nr_v3_id_done == id ||
195 dtnfsclient_rpcs[i].nr_v2_id_done == id)
202 dtnfsclient_getargdesc(void *arg, dtrace_id_t id, void *parg,
203 dtrace_argdesc_t *desc)
205 const char *p = NULL;
207 if (id == nfsclient_accesscache_flush_done_id ||
208 id == nfsclient_attrcache_flush_done_id ||
209 id == nfsclient_attrcache_get_miss_id) {
210 switch (desc->dtargd_ndx) {
212 p = "struct vnode *";
215 desc->dtargd_ndx = DTRACE_ARGNONE;
218 } else if (id == nfsclient_accesscache_get_hit_id ||
219 id == nfsclient_accesscache_get_miss_id) {
220 switch (desc->dtargd_ndx) {
222 p = "struct vnode *";
231 desc->dtargd_ndx = DTRACE_ARGNONE;
234 } else if (id == nfsclient_accesscache_load_done_id) {
235 switch (desc->dtargd_ndx) {
237 p = "struct vnode *";
249 desc->dtargd_ndx = DTRACE_ARGNONE;
252 } else if (id == nfsclient_attrcache_get_hit_id) {
253 switch (desc->dtargd_ndx) {
255 p = "struct vnode *";
258 p = "struct vattr *";
261 desc->dtargd_ndx = DTRACE_ARGNONE;
264 } else if (id == nfsclient_attrcache_load_done_id) {
265 switch (desc->dtargd_ndx) {
267 p = "struct vnode *";
270 p = "struct vattr *";
276 desc->dtargd_ndx = DTRACE_ARGNONE;
280 switch (desc->dtargd_ndx) {
282 p = "struct vnode *";
288 p = "struct ucred *";
294 if (dtnfs23_isdoneprobe(id)) {
300 desc->dtargd_ndx = DTRACE_ARGNONE;
305 strlcpy(desc->dtargd_native, p, sizeof(desc->dtargd_native));
309 dtnfsclient_provide(void *arg, dtrace_probedesc_t *desc)
317 * Register access cache probes.
319 if (dtrace_probe_lookup(dtnfsclient_id, dtnfsclient_accesscache_str,
320 dtnfsclient_flush_str, dtnfsclient_done_str) == 0) {
321 nfsclient_accesscache_flush_done_id = dtrace_probe_create(
322 dtnfsclient_id, dtnfsclient_accesscache_str,
323 dtnfsclient_flush_str, dtnfsclient_done_str, 0, NULL);
325 if (dtrace_probe_lookup(dtnfsclient_id, dtnfsclient_accesscache_str,
326 dtnfsclient_get_str, dtnfsclient_hit_str) == 0) {
327 nfsclient_accesscache_get_hit_id = dtrace_probe_create(
328 dtnfsclient_id, dtnfsclient_accesscache_str,
329 dtnfsclient_get_str, dtnfsclient_hit_str, 0, NULL);
331 if (dtrace_probe_lookup(dtnfsclient_id, dtnfsclient_accesscache_str,
332 dtnfsclient_get_str, dtnfsclient_miss_str) == 0) {
333 nfsclient_accesscache_get_miss_id = dtrace_probe_create(
334 dtnfsclient_id, dtnfsclient_accesscache_str,
335 dtnfsclient_get_str, dtnfsclient_miss_str, 0, NULL);
337 if (dtrace_probe_lookup(dtnfsclient_id, dtnfsclient_accesscache_str,
338 dtnfsclient_load_str, dtnfsclient_done_str) == 0) {
339 nfsclient_accesscache_load_done_id = dtrace_probe_create(
340 dtnfsclient_id, dtnfsclient_accesscache_str,
341 dtnfsclient_load_str, dtnfsclient_done_str, 0, NULL);
345 * Register attribute cache probes.
347 if (dtrace_probe_lookup(dtnfsclient_id, dtnfsclient_attrcache_str,
348 dtnfsclient_flush_str, dtnfsclient_done_str) == 0) {
349 nfsclient_attrcache_flush_done_id = dtrace_probe_create(
350 dtnfsclient_id, dtnfsclient_attrcache_str,
351 dtnfsclient_flush_str, dtnfsclient_done_str, 0, NULL);
353 if (dtrace_probe_lookup(dtnfsclient_id, dtnfsclient_attrcache_str,
354 dtnfsclient_get_str, dtnfsclient_hit_str) == 0) {
355 nfsclient_attrcache_get_hit_id = dtrace_probe_create(
356 dtnfsclient_id, dtnfsclient_attrcache_str,
357 dtnfsclient_get_str, dtnfsclient_hit_str, 0, NULL);
359 if (dtrace_probe_lookup(dtnfsclient_id, dtnfsclient_attrcache_str,
360 dtnfsclient_get_str, dtnfsclient_miss_str) == 0) {
361 nfsclient_attrcache_get_miss_id = dtrace_probe_create(
362 dtnfsclient_id, dtnfsclient_attrcache_str,
363 dtnfsclient_get_str, dtnfsclient_miss_str, 0, NULL);
365 if (dtrace_probe_lookup(dtnfsclient_id, dtnfsclient_attrcache_str,
366 dtnfsclient_load_str, dtnfsclient_done_str) == 0) {
367 nfsclient_attrcache_load_done_id = dtrace_probe_create(
368 dtnfsclient_id, dtnfsclient_attrcache_str,
369 dtnfsclient_load_str, dtnfsclient_done_str, 0, NULL);
373 * Register NFSv2 RPC procedures; note sparseness check for each slot
374 * in the NFSv3 procnum-indexed array.
376 for (i = 0; i < NFS_NPROCS; i++) {
377 if (dtnfsclient_rpcs[i].nr_v2_name != NULL &&
378 dtrace_probe_lookup(dtnfsclient_id, dtnfsclient_nfs2_str,
379 dtnfsclient_rpcs[i].nr_v2_name, dtnfsclient_start_str) ==
381 dtnfsclient_rpcs[i].nr_v2_id_start =
382 dtrace_probe_create(dtnfsclient_id,
383 dtnfsclient_nfs2_str,
384 dtnfsclient_rpcs[i].nr_v2_name,
385 dtnfsclient_start_str, 0,
386 &nfsclient_nfs2_start_probes[i]);
388 if (dtnfsclient_rpcs[i].nr_v2_name != NULL &&
389 dtrace_probe_lookup(dtnfsclient_id, dtnfsclient_nfs2_str,
390 dtnfsclient_rpcs[i].nr_v2_name, dtnfsclient_done_str) ==
392 dtnfsclient_rpcs[i].nr_v2_id_done =
393 dtrace_probe_create(dtnfsclient_id,
394 dtnfsclient_nfs2_str,
395 dtnfsclient_rpcs[i].nr_v2_name,
396 dtnfsclient_done_str, 0,
397 &nfsclient_nfs2_done_probes[i]);
402 * Register NFSv3 RPC procedures.
404 for (i = 0; i < NFS_NPROCS; i++) {
405 if (dtrace_probe_lookup(dtnfsclient_id, dtnfsclient_nfs3_str,
406 dtnfsclient_rpcs[i].nr_v3_name, dtnfsclient_start_str) ==
408 dtnfsclient_rpcs[i].nr_v3_id_start =
409 dtrace_probe_create(dtnfsclient_id,
410 dtnfsclient_nfs3_str,
411 dtnfsclient_rpcs[i].nr_v3_name,
412 dtnfsclient_start_str, 0,
413 &nfsclient_nfs3_start_probes[i]);
415 if (dtrace_probe_lookup(dtnfsclient_id, dtnfsclient_nfs3_str,
416 dtnfsclient_rpcs[i].nr_v3_name, dtnfsclient_done_str) ==
418 dtnfsclient_rpcs[i].nr_v3_id_done =
419 dtrace_probe_create(dtnfsclient_id,
420 dtnfsclient_nfs3_str,
421 dtnfsclient_rpcs[i].nr_v3_name,
422 dtnfsclient_done_str, 0,
423 &nfsclient_nfs3_done_probes[i]);
429 dtnfsclient_destroy(void *arg, dtrace_id_t id, void *parg)
434 dtnfsclient_enable(void *arg, dtrace_id_t id, void *parg)
437 void *f = dtrace_probe;
439 if (id == nfsclient_accesscache_flush_done_id)
440 dtrace_nfsclient_accesscache_flush_done_probe = f;
441 else if (id == nfsclient_accesscache_get_hit_id)
442 dtrace_nfsclient_accesscache_get_hit_probe = f;
443 else if (id == nfsclient_accesscache_get_miss_id)
444 dtrace_nfsclient_accesscache_get_miss_probe = f;
445 else if (id == nfsclient_accesscache_load_done_id)
446 dtrace_nfsclient_accesscache_load_done_probe = f;
447 else if (id == nfsclient_attrcache_flush_done_id)
448 dtrace_nfsclient_attrcache_flush_done_probe = f;
449 else if (id == nfsclient_attrcache_get_hit_id)
450 dtrace_nfsclient_attrcache_get_hit_probe = f;
451 else if (id == nfsclient_attrcache_get_miss_id)
452 dtrace_nfsclient_attrcache_get_miss_probe = f;
453 else if (id == nfsclient_attrcache_load_done_id)
454 dtrace_nfsclient_attrcache_load_done_probe = f;
460 dtnfsclient_disable(void *arg, dtrace_id_t id, void *parg)
464 if (id == nfsclient_accesscache_flush_done_id)
465 dtrace_nfsclient_accesscache_flush_done_probe = NULL;
466 else if (id == nfsclient_accesscache_get_hit_id)
467 dtrace_nfsclient_accesscache_get_hit_probe = NULL;
468 else if (id == nfsclient_accesscache_get_miss_id)
469 dtrace_nfsclient_accesscache_get_miss_probe = NULL;
470 else if (id == nfsclient_accesscache_load_done_id)
471 dtrace_nfsclient_accesscache_load_done_probe = NULL;
472 else if (id == nfsclient_attrcache_flush_done_id)
473 dtrace_nfsclient_attrcache_flush_done_probe = NULL;
474 else if (id == nfsclient_attrcache_get_hit_id)
475 dtrace_nfsclient_attrcache_get_hit_probe = NULL;
476 else if (id == nfsclient_attrcache_get_miss_id)
477 dtrace_nfsclient_attrcache_get_miss_probe = NULL;
478 else if (id == nfsclient_attrcache_load_done_id)
479 dtrace_nfsclient_attrcache_load_done_probe = NULL;
485 dtnfsclient_load(void *dummy)
488 if (dtrace_register("nfsclient", &dtnfsclient_attr,
489 DTRACE_PRIV_USER, NULL, &dtnfsclient_pops, NULL,
490 &dtnfsclient_id) != 0)
493 dtrace_nfsclient_nfs23_start_probe =
494 (dtrace_nfsclient_nfs23_start_probe_func_t)dtrace_probe;
495 dtrace_nfsclient_nfs23_done_probe =
496 (dtrace_nfsclient_nfs23_done_probe_func_t)dtrace_probe;
505 dtrace_nfsclient_nfs23_start_probe = NULL;
506 dtrace_nfsclient_nfs23_done_probe = NULL;
508 if ((error = dtrace_unregister(dtnfsclient_id)) != 0)
515 dtnfsclient_modevent(module_t mod __unused, int type, void *data __unused)
537 SYSINIT(dtnfsclient_load, SI_SUB_DTRACE_PROVIDER, SI_ORDER_ANY,
538 dtnfsclient_load, NULL);
539 SYSUNINIT(dtnfsclient_unload, SI_SUB_DTRACE_PROVIDER, SI_ORDER_ANY,
540 dtnfsclient_unload, NULL);
542 DEV_MODULE(dtnfsclient, dtnfsclient_modevent, NULL);
543 MODULE_VERSION(dtnfsclient, 1);
544 MODULE_DEPEND(dtnfsclient, dtrace, 1, 1, 1);
545 MODULE_DEPEND(dtnfsclient, opensolaris, 1, 1, 1);
546 MODULE_DEPEND(dtnfsclient, nfs, 1, 1, 1);