xref: /freebsd/usr.sbin/nfsdumpstate/nfsdumpstate.c (revision 1db64f89363c97858961c4df0b7d02f3223723cf)
1 /*-
2  * Copyright (c) 2009 Rick Macklem, University of Guelph
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  *
14  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24  * SUCH DAMAGE.
25  *
26  */
27 
28 #include <sys/param.h>
29 #include <sys/linker.h>
30 #include <sys/module.h>
31 #include <sys/socket.h>
32 
33 #include <arpa/inet.h>
34 
35 #include <netinet/in.h>
36 
37 #include <nfs/nfssvc.h>
38 
39 #include <fs/nfs/rpcv2.h>
40 #include <fs/nfs/nfsproto.h>
41 #include <fs/nfs/nfskpiport.h>
42 #include <fs/nfs/nfs.h>
43 
44 #include <ctype.h>
45 #include <err.h>
46 #include <errno.h>
47 #include <stdio.h>
48 #include <stdlib.h>
49 #include <string.h>
50 #include <unistd.h>
51 
52 #define	DUMPSIZE	10000
53 
54 static void dump_lockstate(char *);
55 static void dump_openstate(void);
56 static void usage(void) __dead2;
57 static char *open_flags(uint32_t);
58 static char *deleg_flags(uint32_t);
59 static char *lock_flags(uint32_t);
60 static char *client_flags(uint32_t);
61 
62 static struct nfsd_dumpclients dp[DUMPSIZE];
63 static struct nfsd_dumplocks lp[DUMPSIZE];
64 static char flag_string[20];
65 
66 int
67 main(int argc, char **argv)
68 {
69 	int ch, openstate;
70 	char *lockfile;
71 
72 	if (modfind("nfsd") < 0)
73 		errx(1, "nfsd not loaded - self terminating");
74 	openstate = 0;
75 	lockfile = NULL;
76 	while ((ch = getopt(argc, argv, "ol:")) != -1)
77 		switch (ch) {
78 		case 'o':
79 			openstate = 1;
80 			break;
81 		case 'l':
82 			lockfile = optarg;
83 			break;
84 		default:
85 			usage();
86 		}
87 	argc -= optind;
88 	argv += optind;
89 
90 	if (openstate == 0 && lockfile == NULL)
91 		openstate = 1;
92 	else if (openstate != 0 && lockfile != NULL)
93 		errx(1, "-o and -l cannot both be specified");
94 
95 	/*
96 	 * For -o, dump all open/lock state.
97 	 * For -l, dump lock state for that file.
98 	 */
99 	if (openstate != 0)
100 		dump_openstate();
101 	else
102 		dump_lockstate(lockfile);
103 	exit(0);
104 }
105 
106 static void
107 usage(void)
108 {
109 
110 	errx(1, "usage: nfsdumpstate [-o] [-l]");
111 }
112 
113 /*
114  * Dump all open/lock state.
115  */
116 static void
117 dump_openstate(void)
118 {
119 	struct nfsd_dumplist dumplist;
120 	int cnt, i;
121 #ifdef INET6
122 	char nbuf[INET6_ADDRSTRLEN];
123 #endif
124 
125 	dumplist.ndl_size = DUMPSIZE;
126 	dumplist.ndl_list = (void *)dp;
127 	if (nfssvc(NFSSVC_DUMPCLIENTS, &dumplist) < 0)
128 		errx(1, "Can't perform dump clients syscall");
129 
130 	printf("%-13s %9.9s %9.9s %9.9s %9.9s %9.9s %9.9s %-45s %s\n",
131 	    "Flags", "OpenOwner", "Open", "LockOwner",
132 	    "Lock", "Deleg", "OldDeleg", "Clientaddr", "ClientID");
133 	/*
134 	 * Loop through results, printing them out.
135 	 */
136 	cnt = 0;
137 	while (dp[cnt].ndcl_clid.nclid_idlen > 0 && cnt < DUMPSIZE) {
138 		printf("%-13s ", client_flags(dp[cnt].ndcl_flags));
139 		printf("%9d %9d %9d %9d %9d %9d ",
140 		    dp[cnt].ndcl_nopenowners,
141 		    dp[cnt].ndcl_nopens,
142 		    dp[cnt].ndcl_nlockowners,
143 		    dp[cnt].ndcl_nlocks,
144 		    dp[cnt].ndcl_ndelegs,
145 		    dp[cnt].ndcl_nolddelegs);
146 		switch (dp[cnt].ndcl_addrfam) {
147 #ifdef INET
148 		case AF_INET:
149 			printf("%-45s ",
150 			    inet_ntoa(dp[cnt].ndcl_cbaddr.sin_addr));
151 			break;
152 #endif
153 #ifdef INET6
154 		case AF_INET6:
155 			if (inet_ntop(AF_INET6, &dp[cnt].ndcl_cbaddr.sin6_addr,
156 			    nbuf, sizeof(nbuf)) != NULL)
157 				printf("%-45s ", nbuf);
158 			else
159 				printf("%-45s ", " ");
160 			break;
161 #endif
162 		default:
163 			printf("%-45s ", " ");
164 			break;
165 		}
166 		for (i = 0; i < dp[cnt].ndcl_clid.nclid_idlen; i++)
167 			printf("%02x", dp[cnt].ndcl_clid.nclid_id[i]);
168 		printf("\n");
169 		cnt++;
170 	}
171 }
172 
173 /*
174  * Dump the lock state for a file.
175  */
176 static void
177 dump_lockstate(char *fname)
178 {
179 	struct nfsd_dumplocklist dumplocklist;
180 	int cnt, i;
181 #ifdef INET6
182 	char nbuf[INET6_ADDRSTRLEN];
183 #endif
184 
185 	dumplocklist.ndllck_size = DUMPSIZE;
186 	dumplocklist.ndllck_list = (void *)lp;
187 	dumplocklist.ndllck_fname = fname;
188 	if (nfssvc(NFSSVC_DUMPLOCKS, &dumplocklist) < 0)
189 		errx(1, "Can't dump locks for %s\n", fname);
190 
191 	printf("%-11s %-36s %-45s %s\n",
192 	    "Open/Lock",
193 	    "          Stateid or Lock Range",
194 	    "Clientaddr",
195 	    "Owner and ClientID");
196 	/*
197 	 * Loop through results, printing them out.
198 	 */
199 	cnt = 0;
200 	while (lp[cnt].ndlck_clid.nclid_idlen > 0 && cnt < DUMPSIZE) {
201 		if (lp[cnt].ndlck_flags & NFSLCK_OPEN)
202 			printf("%-11s %9d %08x %08x %08x ",
203 			    open_flags(lp[cnt].ndlck_flags),
204 			    lp[cnt].ndlck_stateid.seqid,
205 			    lp[cnt].ndlck_stateid.other[0],
206 			    lp[cnt].ndlck_stateid.other[1],
207 			    lp[cnt].ndlck_stateid.other[2]);
208 		else if (lp[cnt].ndlck_flags & (NFSLCK_DELEGREAD |
209 		    NFSLCK_DELEGWRITE))
210 			printf("%-11s %9d %08x %08x %08x ",
211 			    deleg_flags(lp[cnt].ndlck_flags),
212 			    lp[cnt].ndlck_stateid.seqid,
213 			    lp[cnt].ndlck_stateid.other[0],
214 			    lp[cnt].ndlck_stateid.other[1],
215 			    lp[cnt].ndlck_stateid.other[2]);
216 		else
217 			printf("%-11s  %17jd %17jd ",
218 			    lock_flags(lp[cnt].ndlck_flags),
219 			    lp[cnt].ndlck_first,
220 			    lp[cnt].ndlck_end);
221 		switch (lp[cnt].ndlck_addrfam) {
222 #ifdef INET
223 		case AF_INET:
224 			printf("%-45s ",
225 			    inet_ntoa(lp[cnt].ndlck_cbaddr.sin_addr));
226 			break;
227 #endif
228 #ifdef INET6
229 		case AF_INET6:
230 			if (inet_ntop(AF_INET6, &lp[cnt].ndlck_cbaddr.sin6_addr,
231 			    nbuf, sizeof(nbuf)) != NULL)
232 				printf("%-45s ", nbuf);
233 			else
234 				printf("%-45s ", " ");
235 			break;
236 #endif
237 		default:
238 			printf("%-45s ", "  ");
239 			break;
240 		}
241 		for (i = 0; i < lp[cnt].ndlck_owner.nclid_idlen; i++)
242 			printf("%02x", lp[cnt].ndlck_owner.nclid_id[i]);
243 		printf(" ");
244 		for (i = 0; i < lp[cnt].ndlck_clid.nclid_idlen; i++)
245 			printf("%02x", lp[cnt].ndlck_clid.nclid_id[i]);
246 		printf("\n");
247 		cnt++;
248 	}
249 }
250 
251 /*
252  * Parse the Open/Lock flag bits and create a string to be printed.
253  */
254 static char *
255 open_flags(uint32_t flags)
256 {
257 	int i, j;
258 
259 	strlcpy(flag_string, "Open ", sizeof (flag_string));
260 	i = 5;
261 	if (flags & NFSLCK_READACCESS)
262 		flag_string[i++] = 'R';
263 	if (flags & NFSLCK_WRITEACCESS)
264 		flag_string[i++] = 'W';
265 	flag_string[i++] = ' ';
266 	flag_string[i++] = 'D';
267 	flag_string[i] = 'N';
268 	j = i;
269 	if (flags & NFSLCK_READDENY)
270 		flag_string[i++] = 'R';
271 	if (flags & NFSLCK_WRITEDENY)
272 		flag_string[i++] = 'W';
273 	if (i == j)
274 		i++;
275 	flag_string[i] = '\0';
276 	return (flag_string);
277 }
278 
279 static char *
280 deleg_flags(uint32_t flags)
281 {
282 
283 	if (flags & NFSLCK_DELEGREAD)
284 		strlcpy(flag_string, "Deleg R", sizeof (flag_string));
285 	else
286 		strlcpy(flag_string, "Deleg W", sizeof (flag_string));
287 	return (flag_string);
288 }
289 
290 static char *
291 lock_flags(uint32_t flags)
292 {
293 
294 	if (flags & NFSLCK_READ)
295 		strlcpy(flag_string, "Lock R", sizeof (flag_string));
296 	else
297 		strlcpy(flag_string, "Lock W", sizeof (flag_string));
298 	return (flag_string);
299 }
300 
301 static char *
302 client_flags(uint32_t flags)
303 {
304 
305 	flag_string[0] = '\0';
306 	if (flags & LCL_NEEDSCONFIRM)
307 		strlcat(flag_string, "NC ", sizeof (flag_string));
308 	if (flags & LCL_CALLBACKSON)
309 		strlcat(flag_string, "CB ", sizeof (flag_string));
310 	if (flags & LCL_GSS)
311 		strlcat(flag_string, "GSS ", sizeof (flag_string));
312 	if (flags & LCL_ADMINREVOKED)
313 		strlcat(flag_string, "REV", sizeof (flag_string));
314 	return (flag_string);
315 }
316