From e3aece4cabaa46a683b14597e6b6d556169e0a74 Mon Sep 17 00:00:00 2001
From: Joyce McIntosh <joyce.mcintosh@nexenta.com>
Date: Tue, 28 Aug 2018 11:56:19 -0600
Subject: [PATCH 15/30] NEX-18369 SMB & NFS dtrace providers do not support
 per-zone filtering Reviewed by: Evan Layton <evan.layton@nexenta.com>
 Reviewed by: Gordon Ross <gordon.ross@nexenta.com>

---
 usr/src/cmd/fs.d/nfs/dtrace/nfs-time.d  | 38 ++++++++++-------
 usr/src/cmd/fs.d/nfs/dtrace/nfs-trace.d | 56 +++++++++++++++----------
 usr/src/cmd/smbsrv/dtrace/smb-trace.d   | 39 ++++++++++++++++-
 usr/src/cmd/smbsrv/dtrace/smb2-trace.d  | 39 ++++++++++++++++-
 usr/src/lib/libdtrace/common/nfs.d      |  5 +++
 usr/src/lib/libdtrace/common/smb.d      |  6 ++-
 6 files changed, 142 insertions(+), 41 deletions(-)

diff --git a/usr/src/cmd/fs.d/nfs/dtrace/nfs-time.d b/usr/src/cmd/fs.d/nfs/dtrace/nfs-time.d
index cb030d10e8..d8302cabc1 100755
--- a/usr/src/cmd/fs.d/nfs/dtrace/nfs-time.d
+++ b/usr/src/cmd/fs.d/nfs/dtrace/nfs-time.d
@@ -17,17 +17,15 @@
 
 /*
  * Quanitize the time spent in each NFSv3 andf NFSv4 operation,
- * optionally for a specified client and share.
+ * optionally for a specified client, share and zone.
  *
- * usage:   nfs_time.d
- * usage:   nfs_time.d <client ip>   <share path>
- * example: nfs_time.d 192.168.123.1 /mypool/fs1
+ * Usage: nfs-time.d [<client ip>|all [<share path>|all] [<zone id>]]]
  *
- * It is valid to specify <client ip> or <share path> as "all" to
- * quantize data for all clients and/or all shares.
- * example: nfs_time.d 192.168.123.1 all
- * example: nfs_time.d all /mypool/fs1
- * example: nfs_time.d all all
+ * example: nfs_time.d 192.168.123.1 /mypool/fs1  0
+ *
+ * It is valid to specify <client ip> or <share path> as "all"
+ * to quantize data for all clients and/or all shares.
+ * Ommitting <zone id> will quantize data for all zones.
  */
 
 #pragma D option flowindent
@@ -35,23 +33,31 @@
 
 dtrace:::BEGIN
 {
-	client = ($$1 == NULL) ? "all" : $$1;
-	share = ($$2 == NULL) ? "all" : $$2;
-	printf("%Y - client=%s share=%s\n", walltimestamp, client, share);
+	all_clients = (($$1 == NULL) || ($$1 == "all")) ? 1 : 0;
+	all_shares = (($$2 == NULL) || ($$2 == "all")) ? 1 : 0;
+	all_zones = ($$3 == NULL) ? 1 : 0;
+
+	client = $$1;
+	share = $$2;
+	zoneid = $3;
+
+	printf("%Y - client=%s share=%s zone=%s)\n", walltimestamp,
+	    (all_clients) ? "all" : client,
+	    (all_shares) ? "all" : share,
+	    (all_zones) ? "all" : $$3);
 }
 
 nfsv3:::op-*-start,
 nfsv4:::op-*-start
-/ ((client == "all") || (args[0]->ci_remote == client)) &&
-   ((share == "all") || (args[1]->noi_shrpath == share)) /
 {
 	self->ts[probefunc] = timestamp;
 }
 
 nfsv3:::op-*-done,
 nfsv4:::op-*-done
-/ ((client == "all") || (args[0]->ci_remote == client)) &&
-   ((share == "all") || (args[1]->noi_shrpath == share)) /
+/ ((all_clients) || (args[0]->ci_remote == client)) &&
+   ((all_shares) || (args[1]->noi_shrpath == share)) &&
+   ((all_zones) || (args[1]->noi_zoneid == zoneid)) /
 {
 	elapsed = (timestamp - self->ts[probefunc]);
 	@q[probefunc]=quantize(elapsed);
diff --git a/usr/src/cmd/fs.d/nfs/dtrace/nfs-trace.d b/usr/src/cmd/fs.d/nfs/dtrace/nfs-trace.d
index e26737a9ec..46bc3a0a80 100755
--- a/usr/src/cmd/fs.d/nfs/dtrace/nfs-trace.d
+++ b/usr/src/cmd/fs.d/nfs/dtrace/nfs-trace.d
@@ -17,17 +17,15 @@
 
 /*
  * Print input and output values for each NFSv3 andf NFSv4 operation,
- * optionally for a specified client and share.
+ * optionally for a specified client, share and zone.
  *
- * usage:   nfs_test.d
- * usage:   nfs_test.d <client ip>   <share path>
- * example: nfs_test.d 192.168.123.1 /mypool/fs1
+ * Usage: nfs-trace.d [<client ip>|all [<share path>|all] [<zone id>]]]
  *
- * It is valid to specify <client ip> or <share path> as "all" to
- * print data for all clients and/or all shares.
- * example: nfs_test.d 192.168.123.1 all
- * example: nfs_test.d all /mypool/fs1
- * example: nfs_test.d all all
+ * example: nfs_trace.d 192.168.123.1 /mypool/fs1  0
+ *
+ * It is valid to specify <client ip> or <share path> as "all"
+ * to quantize data for all clients and/or all shares.
+ * Ommitting <zone id> will quantize data for all zones.
  */
 
 /*
@@ -46,9 +44,18 @@
 
 dtrace:::BEGIN
 {
-	client = ($$1 == NULL) ? "all" : $$1;
-	share = ($$2 == NULL) ? "all" : $$2;
-	printf("%Y - client=%s share=%s\n", walltimestamp, client, share);
+	all_clients = (($$1 == NULL) || ($$1 == "all")) ? 1 : 0;
+	all_shares = (($$2 == NULL) || ($$2 == "all")) ? 1 : 0;
+	all_zones = ($$3 == NULL) ? 1 : 0;
+
+	client = $$1;
+	share = $$2;
+	zoneid = $3;
+
+	printf("%Y - client=%s share=%s zone=%s)\n", walltimestamp,
+	    (all_clients) ? "all" : client,
+	    (all_shares) ? "all" : share,
+	    (all_zones) ? "all" : $$3);
 }
 
 nfsv3:::op-getattr-start,
@@ -72,8 +79,9 @@ nfsv3:::op-rename-start,
 nfsv3:::op-rmdir-start,
 nfsv3:::op-symlink-start,
 nfsv3:::op-write-start
-/ ((client == "all") || (args[0]->ci_remote == client)) &&
-   ((share == "all") || (args[1]->noi_shrpath == share)) /
+/ ((all_clients) || (args[0]->ci_remote == client)) &&
+   ((all_shares) || (args[1]->noi_shrpath == share)) &&
+   ((all_zones) || (args[1]->noi_zoneid == zoneid)) /
 {
 	printf("\n");
 	print(*args[0]);
@@ -105,8 +113,9 @@ nfsv3:::op-rename-done,
 nfsv3:::op-rmdir-done,
 nfsv3:::op-symlink-done,
 nfsv3:::op-write-done
-/ ((client == "all") || (args[0]->ci_remote == client)) &&
-   ((share == "all") || (args[1]->noi_shrpath == share)) /
+/ ((all_clients) || (args[0]->ci_remote == client)) &&
+   ((all_shares) || (args[1]->noi_shrpath == share)) &&
+   ((all_zones) || (args[1]->noi_zoneid == zoneid)) /
 {
 	/*
 	printf("\n");
@@ -149,8 +158,9 @@ nfsv4:::op-setclientid-start,
 nfsv4:::op-setclientid-confirm-start,
 nfsv4:::op-verify-start,
 nfsv4:::op-write-start
-/ ((client == "all") || (args[0]->ci_remote == client)) &&
-   ((share == "all") || (args[1]->noi_shrpath == share)) /
+/ ((all_clients) || (args[0]->ci_remote == client)) &&
+   ((all_shares) || (args[1]->noi_shrpath == share)) &&
+   ((all_zones) || (args[1]->noi_zoneid == zoneid)) /
 {
 	printf("\n");
 	print(*args[0]);
@@ -169,8 +179,9 @@ nfsv4:::op-putrootfh-start,
 nfsv4:::op-readlink-start,
 nfsv4:::op-restorefh-start,
 nfsv4:::op-savefh-start
-/ ((client == "all") || (args[0]->ci_remote == client)) &&
-   ((share == "all") || (args[1]->noi_shrpath == share)) /
+/ ((all_clients) || (args[0]->ci_remote == client)) &&
+   ((all_shares) || (args[1]->noi_shrpath == share)) &&
+   ((all_zones) || (args[1]->noi_zoneid == zoneid)) /
 {
 	printf("\n");
 	print(*args[0]);
@@ -217,8 +228,9 @@ nfsv4:::op-setclientid-done,
 nfsv4:::op-setclientid-confirm-done,
 nfsv4:::op-verify-done,
 nfsv4:::op-write-done
-/ ((client == "all") || (args[0]->ci_remote == client)) &&
-   ((share == "all") || (args[1]->noi_shrpath == share)) /
+/ ((all_clients) || (args[0]->ci_remote == client)) &&
+   ((all_shares) || (args[1]->noi_shrpath == share)) &&
+   ((all_zones) || (args[1]->noi_zoneid == zoneid)) /
 {
 	/*
 	printf("\n");
diff --git a/usr/src/cmd/smbsrv/dtrace/smb-trace.d b/usr/src/cmd/smbsrv/dtrace/smb-trace.d
index 32162dca2b..1ed48eaf2d 100644
--- a/usr/src/cmd/smbsrv/dtrace/smb-trace.d
+++ b/usr/src/cmd/smbsrv/dtrace/smb-trace.d
@@ -1,3 +1,4 @@
+#!/usr/sbin/dtrace -s
 /*
  * This file and its contents are supplied under the terms of the
  * Common Development and Distribution License ("CDDL"), version 1.0.
@@ -10,7 +11,7 @@
  */
 
 /*
- * Copyright 2017 Nexenta Systems, Inc.  All rights reserved.
+ * Copyright 2018 Nexenta Systems, Inc.  All rights reserved.
  */
 
 /*
@@ -24,9 +25,38 @@
  *	args[2]  smb_name_args_t
  *	args[2]  smb_open_args_t
  *	args[2]  smb_rw_args_t
+ *
+ * Usage: smb-trace.d [<client ip>|all [<share path>|all] [<zone id>]]]
+ *
+ * example: smb_trace.d 192.168.012.001 mypool_fs1  0
+ *
+ * It is valid to specify <client ip> or <share path> as "all" to
+ * print data for all clients and/or all shares.
+ * Ommitting <zone id> will print data for all zones.
  */
 
+#pragma D option defaultargs
+
+dtrace:::BEGIN
+{
+	all_clients = (($$1 == NULL) || ($$1 == "all")) ? 1 : 0;
+	all_shares = (($$2 == NULL) || ($$2 == "all")) ? 1 : 0;
+	all_zones = ($$3 == NULL) ? 1 : 0;
+
+	client = $$1;
+	share = $$2;
+	zoneid = $3;
+
+	printf("%Y - client=%s share=%s zone=%s)\n", walltimestamp,
+	    (all_clients) ? "all" : client,
+	    (all_shares) ? "all" : share,
+	    (all_zones) ? "all" : $$3);
+}
+
 smb:::op-*-start
+/ ((all_clients) || (args[0]->ci_remote == client)) &&
+   ((all_shares) || (args[1]->soi_share == share)) &&
+   ((all_zones) || (args[1]->soi_zoneid == zoneid)) /
 {
 	printf("clnt=%s mid=0x%x uid=0x%x tid=0x%x\n",
 	       args[0]->ci_remote,
@@ -36,9 +66,16 @@ smb:::op-*-start
 }
 
 smb:::op-*-done
+/ ((all_clients) || (args[0]->ci_remote == client)) &&
+   ((all_shares) || (args[1]->soi_share == share)) &&
+   ((all_zones) || (args[1]->soi_zoneid == zoneid)) /
 {
 	printf("clnt=%s mid=0x%x status=0x%x\n",
 	       args[0]->ci_remote,
 	       args[1]->soi_mid,
 	       args[1]->soi_status);
 }
+
+dtrace:::END
+{
+}
diff --git a/usr/src/cmd/smbsrv/dtrace/smb2-trace.d b/usr/src/cmd/smbsrv/dtrace/smb2-trace.d
index 4e83216785..572b09f31b 100644
--- a/usr/src/cmd/smbsrv/dtrace/smb2-trace.d
+++ b/usr/src/cmd/smbsrv/dtrace/smb2-trace.d
@@ -1,3 +1,4 @@
+#!/usr/sbin/dtrace -s
 /*
  * This file and its contents are supplied under the terms of the
  * Common Development and Distribution License ("CDDL"), version 1.0.
@@ -10,7 +11,7 @@
  */
 
 /*
- * Copyright 2017 Nexenta Systems, Inc.  All rights reserved.
+ * Copyright 2018 Nexenta Systems, Inc.  All rights reserved.
  */
 
 /*
@@ -23,9 +24,38 @@
  * Some also provide one of: (not used here)
  *	args[2]  smb_open_args_t
  *	args[2]  smb_rw_args_t
+ *
+ * Usage: smb2-trace.d [<client ip>|all [<share path>|all] [<zone id>]]]
+ *
+ * example: smb2_trace.d 192.168.012.001 mypool_fs1  0
+ *
+ * It is valid to specify <client ip> or <share path> as "all" to
+ * print data for all clients and/or all shares.
+ * Ommitting <zone id> will print data for all zones.
  */
 
+#pragma D option defaultargs
+
+dtrace:::BEGIN
+{
+	all_clients = (($$1 == NULL) || ($$1 == "all")) ? 1 : 0;
+	all_shares = (($$2 == NULL) || ($$2 == "all")) ? 1 : 0;
+	all_zones = ($$3 == NULL) ? 1 : 0;
+
+	client = $$1;
+	share = $$2;
+	zoneid = $3;
+
+	printf("%Y - client=%s share=%s zone=%s)\n", walltimestamp,
+	    (all_clients) ? "all" : client,
+	    (all_shares) ? "all" : share,
+	    (all_zones) ? "all" : $$3);
+}
+
 smb2:::op-*-start
+/ ((all_clients == 1) || (args[0]->ci_remote == client)) &&
+   ((all_shares == 1) || (args[1]->soi_share == share)) &&
+   ((all_zones == 1) || (args[1]->soi_zoneid == zoneid)) /
 {
 	printf("clnt=%s mid=0x%x uid=0x%x tid=0x%x\n",
 	       args[0]->ci_remote,
@@ -35,9 +65,16 @@ smb2:::op-*-start
 }
 
 smb2:::op-*-done
+/ ((all_clients == 1) || (args[0]->ci_remote == client)) &&
+   ((all_shares == 1) || (args[1]->soi_share == share)) &&
+   ((all_zones == 1) || (args[1]->soi_zoneid == zoneid)) /
 {
 	printf("clnt=%s mid=0x%x status=0x%x\n",
 	       args[0]->ci_remote,
 	       args[1]->soi_mid,
 	       args[1]->soi_status);
 }
+
+dtrace:::END
+{
+}
diff --git a/usr/src/lib/libdtrace/common/nfs.d b/usr/src/lib/libdtrace/common/nfs.d
index 81f37de83b..7a97cd5e6d 100644
--- a/usr/src/lib/libdtrace/common/nfs.d
+++ b/usr/src/lib/libdtrace/common/nfs.d
@@ -39,6 +39,7 @@ typedef struct nfsv4opinfo {
 	cred_t *noi_cred;	/* credentials for operation */
 	string noi_curpath;	/* current file handle path (if any) */
 	string noi_shrpath;     /* current share path */
+	zoneid_t noi_zoneid;	/* zone identifier */
 } nfsv4opinfo_t;
 
 typedef struct nfsv4cbinfo {
@@ -107,6 +108,7 @@ translator nfsv4opinfo_t < struct compound_state *P > {
 	noi_curpath = (P->vp == NULL) ? "<unknown>" : P->vp->v_path;
 	noi_shrpath = (P->exi == NULL || P->exi->exi_export.ex_path == NULL) ?
 	    "<unknown>" : P->exi->exi_export.ex_path;
+	noi_zoneid = (P->exi == NULL) ? -1 : P->exi->exi_zoneid;
 };
 
 typedef struct nfsv3opinfo {
@@ -114,6 +116,7 @@ typedef struct nfsv3opinfo {
 	cred_t *noi_cred;	/* credentials for operation */
 	string noi_curpath;	/* current file handle path (if any) */
 	string noi_shrpath;     /* current share path */
+	zoneid_t noi_zoneid;	/* zone identifier */
 } nfsv3opinfo_t;
 
 typedef struct nfsv3oparg nfsv3oparg_t;
@@ -127,4 +130,6 @@ translator nfsv3opinfo_t < nfsv3oparg_t *P > {
 	noi_shrpath =
 	    (arg3 == 0 || ((exportinfo_t *)arg3)->exi_export.ex_path == NULL) ?
 	    "<unknown>" : ((exportinfo_t *)arg3)->exi_export.ex_path;
+	noi_zoneid =
+	    (arg3 == 0) ? -1 : ((exportinfo_t *)arg3)->exi_zoneid;
 };
diff --git a/usr/src/lib/libdtrace/common/smb.d b/usr/src/lib/libdtrace/common/smb.d
index c58cb4bf1c..9b2f8bdc1f 100644
--- a/usr/src/lib/libdtrace/common/smb.d
+++ b/usr/src/lib/libdtrace/common/smb.d
@@ -23,7 +23,7 @@
  * Copyright 2009 Sun Microsystems, Inc.  All rights reserved.
  * Use is subject to license terms.
  *
- * Copyright 2017 Nexenta Systems, Inc.  All rights reserved.
+ * Copyright 2018 Nexenta Systems, Inc.  All rights reserved.
  */
 
 #pragma	D depends_on library ip.d
@@ -58,6 +58,7 @@ typedef struct smbopinfo {
 	uint16_t soi_fid;		/* file id */
 	uint16_t soi_flags2;		/* flags2 */
 	uint8_t  soi_flags;		/* flags */
+	zoneid_t soi_zoneid;		/* zone identifier */
 } smbopinfo_t;
 
 #pragma D binding "1.5" translator
@@ -72,6 +73,7 @@ translator smbopinfo_t < struct smb_request *P > {
 	soi_fid		= P->smb_fid;
 	soi_flags2	= P->smb_flg2;
 	soi_flags	= P->smb_flg;
+	soi_zoneid	= P->sr_server->sv_zid;
 
 	soi_share = (P->tid_tree == NULL) ? "<NULL>" :
 	    P->tid_tree->t_sharename;
@@ -137,6 +139,7 @@ typedef struct smb2opinfo {
 	uint32_t soi_tid;		/* tree ID */
 	uint32_t soi_status;
 	uint32_t soi_flags;
+	zoneid_t soi_zoneid;		/* zone identifier */
 } smb2opinfo_t;
 
 #pragma D binding "1.5" translator
@@ -149,6 +152,7 @@ translator smb2opinfo_t < struct smb_request *P > {
 	soi_tid		= P->smb_tid;
 	soi_status	= P->smb2_status;
 	soi_flags	= P->smb2_hdr_flags;
+	soi_zoneid	= P->sr_server->sv_zid;
 
 	soi_share = (P->tid_tree == NULL) ? "<NULL>" :
 	    P->tid_tree->t_sharename;
-- 
2.21.0

