xref: /freebsd/usr.sbin/nfsdumpstate/nfsdumpstate.c (revision 3ff01b231dfa83d518854c63e7c9cd1debd1139e)
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/cdefs.h>
29 __FBSDID("$FreeBSD$");
30 
31 #include <sys/param.h>
32 #include <sys/linker.h>
33 #include <sys/module.h>
34 #include <sys/socket.h>
35 
36 #include <arpa/inet.h>
37 
38 #include <netinet/in.h>
39 
40 #include <nfs/nfssvc.h>
41 
42 #include <fs/nfs/rpcv2.h>
43 #include <fs/nfs/nfsproto.h>
44 #include <fs/nfs/nfskpiport.h>
45 #include <fs/nfs/nfs.h>
46 
47 #include <ctype.h>
48 #include <err.h>
49 #include <errno.h>
50 #include <stdio.h>
51 #include <stdlib.h>
52 #include <string.h>
53 #include <unistd.h>
54 
55 #define	DUMPSIZE	10000
56 
57 static void dump_lockstate(char *);
58 static void dump_openstate(void);
59 static void usage(void);
60 static char *open_flags(uint32_t);
61 static char *deleg_flags(uint32_t);
62 static char *lock_flags(uint32_t);
63 static char *client_flags(uint32_t);
64 
65 static struct nfsd_dumpclients dp[DUMPSIZE];
66 static struct nfsd_dumplocks lp[DUMPSIZE];
67 static char flag_string[20];
68 
69 int
70 main(int argc, char **argv)
71 {
72 	int ch, openstate;
73 	char *lockfile;
74 
75 	if (modfind("nfsd") < 0)
76 		errx(1, "nfsd not loaded - self terminating");
77 	openstate = 0;
78 	lockfile = NULL;
79 	while ((ch = getopt(argc, argv, "ol:")) != -1)
80 		switch (ch) {
81 		case 'o':
82 			openstate = 1;
83 			break;
84 		case 'l':
85 			lockfile = optarg;
86 			break;
87 		default:
88 			usage();
89 		}
90 	argc -= optind;
91 	argv += optind;
92 
93 	if (openstate == 0 && lockfile == NULL)
94 		openstate = 1;
95 	else if (openstate != 0 && lockfile != NULL)
96 		errx(1, "-o and -l cannot both be specified");
97 
98 	/*
99 	 * For -o, dump all open/lock state.
100 	 * For -l, dump lock state for that file.
101 	 */
102 	if (openstate != 0)
103 		dump_openstate();
104 	else
105 		dump_lockstate(lockfile);
106 	exit(0);
107 }
108 
109 static void
110 usage(void)
111 {
112 
113 	errx(1, "usage: nfsdumpstate [-o] [-l]");
114 }
115 
116 /*
117  * Dump all open/lock state.
118  */
119 static void
120 dump_openstate(void)
121 {
122 	struct nfsd_dumplist dumplist;
123 	int cnt, i;
124 #ifdef INET6
125 	char nbuf[INET6_ADDRSTRLEN];
126 #endif
127 
128 	dumplist.ndl_size = DUMPSIZE;
129 	dumplist.ndl_list = (void *)dp;
130 	if (nfssvc(NFSSVC_DUMPCLIENTS, &dumplist) < 0)
131 		errx(1, "Can't perform dump clients syscall");
132 
133 	printf("%-13s %9.9s %9.9s %9.9s %9.9s %9.9s %9.9s %-45s %s\n",
134 	    "Flags", "OpenOwner", "Open", "LockOwner",
135 	    "Lock", "Deleg", "OldDeleg", "Clientaddr", "ClientID");
136 	/*
137 	 * Loop through results, printing them out.
138 	 */
139 	cnt = 0;
140 	while (dp[cnt].ndcl_clid.nclid_idlen > 0 && cnt < DUMPSIZE) {
141 		printf("%-13s ", client_flags(dp[cnt].ndcl_flags));
142 		printf("%9d %9d %9d %9d %9d %9d ",
143 		    dp[cnt].ndcl_nopenowners,
144 		    dp[cnt].ndcl_nopens,
145 		    dp[cnt].ndcl_nlockowners,
146 		    dp[cnt].ndcl_nlocks,
147 		    dp[cnt].ndcl_ndelegs,
148 		    dp[cnt].ndcl_nolddelegs);
149 		switch (dp[cnt].ndcl_addrfam) {
150 #ifdef INET
151 		case AF_INET:
152 			printf("%-45s ",
153 			    inet_ntoa(dp[cnt].ndcl_cbaddr.sin_addr));
154 			break;
155 #endif
156 #ifdef INET6
157 		case AF_INET6:
158 			if (inet_ntop(AF_INET6, &dp[cnt].ndcl_cbaddr.sin6_addr,
159 			    nbuf, sizeof(nbuf)) != NULL)
160 				printf("%-45s ", nbuf);
161 			else
162 				printf("%-45s ", " ");
163 			break;
164 #endif
165 		default:
166 			printf("%-45s ", " ");
167 			break;
168 		}
169 		for (i = 0; i < dp[cnt].ndcl_clid.nclid_idlen; i++)
170 			printf("%02x", dp[cnt].ndcl_clid.nclid_id[i]);
171 		printf("\n");
172 		cnt++;
173 	}
174 }
175 
176 /*
177  * Dump the lock state for a file.
178  */
179 static void
180 dump_lockstate(char *fname)
181 {
182 	struct nfsd_dumplocklist dumplocklist;
183 	int cnt, i;
184 #ifdef INET6
185 	char nbuf[INET6_ADDRSTRLEN];
186 #endif
187 
188 	dumplocklist.ndllck_size = DUMPSIZE;
189 	dumplocklist.ndllck_list = (void *)lp;
190 	dumplocklist.ndllck_fname = fname;
191 	if (nfssvc(NFSSVC_DUMPLOCKS, &dumplocklist) < 0)
192 		errx(1, "Can't dump locks for %s\n", fname);
193 
194 	printf("%-11s %-36s %-45s %s\n",
195 	    "Open/Lock",
196 	    "          Stateid or Lock Range",
197 	    "Clientaddr",
198 	    "Owner and ClientID");
199 	/*
200 	 * Loop through results, printing them out.
201 	 */
202 	cnt = 0;
203 	while (lp[cnt].ndlck_clid.nclid_idlen > 0 && cnt < DUMPSIZE) {
204 		if (lp[cnt].ndlck_flags & NFSLCK_OPEN)
205 			printf("%-11s %9d %08x %08x %08x ",
206 			    open_flags(lp[cnt].ndlck_flags),
207 			    lp[cnt].ndlck_stateid.seqid,
208 			    lp[cnt].ndlck_stateid.other[0],
209 			    lp[cnt].ndlck_stateid.other[1],
210 			    lp[cnt].ndlck_stateid.other[2]);
211 		else if (lp[cnt].ndlck_flags & (NFSLCK_DELEGREAD |
212 		    NFSLCK_DELEGWRITE))
213 			printf("%-11s %9d %08x %08x %08x ",
214 			    deleg_flags(lp[cnt].ndlck_flags),
215 			    lp[cnt].ndlck_stateid.seqid,
216 			    lp[cnt].ndlck_stateid.other[0],
217 			    lp[cnt].ndlck_stateid.other[1],
218 			    lp[cnt].ndlck_stateid.other[2]);
219 		else
220 			printf("%-11s  %17jd %17jd ",
221 			    lock_flags(lp[cnt].ndlck_flags),
222 			    lp[cnt].ndlck_first,
223 			    lp[cnt].ndlck_end);
224 		switch (lp[cnt].ndlck_addrfam) {
225 #ifdef INET
226 		case AF_INET:
227 			printf("%-45s ",
228 			    inet_ntoa(lp[cnt].ndlck_cbaddr.sin_addr));
229 			break;
230 #endif
231 #ifdef INET6
232 		case AF_INET6:
233 			if (inet_ntop(AF_INET6, &lp[cnt].ndlck_cbaddr.sin6_addr,
234 			    nbuf, sizeof(nbuf)) != NULL)
235 				printf("%-45s ", nbuf);
236 			else
237 				printf("%-45s ", " ");
238 			break;
239 #endif
240 		default:
241 			printf("%-45s ", "  ");
242 			break;
243 		}
244 		for (i = 0; i < lp[cnt].ndlck_owner.nclid_idlen; i++)
245 			printf("%02x", lp[cnt].ndlck_owner.nclid_id[i]);
246 		printf(" ");
247 		for (i = 0; i < lp[cnt].ndlck_clid.nclid_idlen; i++)
248 			printf("%02x", lp[cnt].ndlck_clid.nclid_id[i]);
249 		printf("\n");
250 		cnt++;
251 	}
252 }
253 
254 /*
255  * Parse the Open/Lock flag bits and create a string to be printed.
256  */
257 static char *
258 open_flags(uint32_t flags)
259 {
260 	int i, j;
261 
262 	strlcpy(flag_string, "Open ", sizeof (flag_string));
263 	i = 5;
264 	if (flags & NFSLCK_READACCESS)
265 		flag_string[i++] = 'R';
266 	if (flags & NFSLCK_WRITEACCESS)
267 		flag_string[i++] = 'W';
268 	flag_string[i++] = ' ';
269 	flag_string[i++] = 'D';
270 	flag_string[i] = 'N';
271 	j = i;
272 	if (flags & NFSLCK_READDENY)
273 		flag_string[i++] = 'R';
274 	if (flags & NFSLCK_WRITEDENY)
275 		flag_string[i++] = 'W';
276 	if (i == j)
277 		i++;
278 	flag_string[i] = '\0';
279 	return (flag_string);
280 }
281 
282 static char *
283 deleg_flags(uint32_t flags)
284 {
285 
286 	if (flags & NFSLCK_DELEGREAD)
287 		strlcpy(flag_string, "Deleg R", sizeof (flag_string));
288 	else
289 		strlcpy(flag_string, "Deleg W", sizeof (flag_string));
290 	return (flag_string);
291 }
292 
293 static char *
294 lock_flags(uint32_t flags)
295 {
296 
297 	if (flags & NFSLCK_READ)
298 		strlcpy(flag_string, "Lock R", sizeof (flag_string));
299 	else
300 		strlcpy(flag_string, "Lock W", sizeof (flag_string));
301 	return (flag_string);
302 }
303 
304 static char *
305 client_flags(uint32_t flags)
306 {
307 
308 	flag_string[0] = '\0';
309 	if (flags & LCL_NEEDSCONFIRM)
310 		strlcat(flag_string, "NC ", sizeof (flag_string));
311 	if (flags & LCL_CALLBACKSON)
312 		strlcat(flag_string, "CB ", sizeof (flag_string));
313 	if (flags & LCL_GSS)
314 		strlcat(flag_string, "GSS ", sizeof (flag_string));
315 	if (flags & LCL_ADMINREVOKED)
316 		strlcat(flag_string, "REV", sizeof (flag_string));
317 	return (flag_string);
318 }
319