2 * Copyright (c) 2009 Rick Macklem, University of Guelph
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
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.
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
28 #include <sys/cdefs.h>
29 #include <sys/param.h>
30 #include <sys/linker.h>
31 #include <sys/module.h>
32 #include <sys/socket.h>
34 #include <arpa/inet.h>
36 #include <netinet/in.h>
38 #include <nfs/nfssvc.h>
40 #include <fs/nfs/rpcv2.h>
41 #include <fs/nfs/nfsproto.h>
42 #include <fs/nfs/nfskpiport.h>
43 #include <fs/nfs/nfs.h>
53 #define DUMPSIZE 10000
55 static void dump_lockstate(char *);
56 static void dump_openstate(void);
57 static void usage(void);
58 static char *open_flags(uint32_t);
59 static char *deleg_flags(uint32_t);
60 static char *lock_flags(uint32_t);
61 static char *client_flags(uint32_t);
63 static struct nfsd_dumpclients dp[DUMPSIZE];
64 static struct nfsd_dumplocks lp[DUMPSIZE];
65 static char flag_string[20];
68 main(int argc, char **argv)
73 if (modfind("nfsd") < 0)
74 errx(1, "nfsd not loaded - self terminating");
77 while ((ch = getopt(argc, argv, "ol:")) != -1)
91 if (openstate == 0 && lockfile == NULL)
93 else if (openstate != 0 && lockfile != NULL)
94 errx(1, "-o and -l cannot both be specified");
97 * For -o, dump all open/lock state.
98 * For -l, dump lock state for that file.
103 dump_lockstate(lockfile);
111 errx(1, "usage: nfsdumpstate [-o] [-l]");
115 * Dump all open/lock state.
120 struct nfsd_dumplist dumplist;
123 char nbuf[INET6_ADDRSTRLEN];
126 dumplist.ndl_size = DUMPSIZE;
127 dumplist.ndl_list = (void *)dp;
128 if (nfssvc(NFSSVC_DUMPCLIENTS, &dumplist) < 0)
129 errx(1, "Can't perform dump clients syscall");
131 printf("%-13s %9.9s %9.9s %9.9s %9.9s %9.9s %9.9s %-45s %s\n",
132 "Flags", "OpenOwner", "Open", "LockOwner",
133 "Lock", "Deleg", "OldDeleg", "Clientaddr", "ClientID");
135 * Loop through results, printing them out.
138 while (dp[cnt].ndcl_clid.nclid_idlen > 0 && cnt < DUMPSIZE) {
139 printf("%-13s ", client_flags(dp[cnt].ndcl_flags));
140 printf("%9d %9d %9d %9d %9d %9d ",
141 dp[cnt].ndcl_nopenowners,
143 dp[cnt].ndcl_nlockowners,
145 dp[cnt].ndcl_ndelegs,
146 dp[cnt].ndcl_nolddelegs);
147 switch (dp[cnt].ndcl_addrfam) {
151 inet_ntoa(dp[cnt].ndcl_cbaddr.sin_addr));
156 if (inet_ntop(AF_INET6, &dp[cnt].ndcl_cbaddr.sin6_addr,
157 nbuf, sizeof(nbuf)) != NULL)
158 printf("%-45s ", nbuf);
160 printf("%-45s ", " ");
164 printf("%-45s ", " ");
167 for (i = 0; i < dp[cnt].ndcl_clid.nclid_idlen; i++)
168 printf("%02x", dp[cnt].ndcl_clid.nclid_id[i]);
175 * Dump the lock state for a file.
178 dump_lockstate(char *fname)
180 struct nfsd_dumplocklist dumplocklist;
183 char nbuf[INET6_ADDRSTRLEN];
186 dumplocklist.ndllck_size = DUMPSIZE;
187 dumplocklist.ndllck_list = (void *)lp;
188 dumplocklist.ndllck_fname = fname;
189 if (nfssvc(NFSSVC_DUMPLOCKS, &dumplocklist) < 0)
190 errx(1, "Can't dump locks for %s\n", fname);
192 printf("%-11s %-36s %-45s %s\n",
194 " Stateid or Lock Range",
196 "Owner and ClientID");
198 * Loop through results, printing them out.
201 while (lp[cnt].ndlck_clid.nclid_idlen > 0 && cnt < DUMPSIZE) {
202 if (lp[cnt].ndlck_flags & NFSLCK_OPEN)
203 printf("%-11s %9d %08x %08x %08x ",
204 open_flags(lp[cnt].ndlck_flags),
205 lp[cnt].ndlck_stateid.seqid,
206 lp[cnt].ndlck_stateid.other[0],
207 lp[cnt].ndlck_stateid.other[1],
208 lp[cnt].ndlck_stateid.other[2]);
209 else if (lp[cnt].ndlck_flags & (NFSLCK_DELEGREAD |
211 printf("%-11s %9d %08x %08x %08x ",
212 deleg_flags(lp[cnt].ndlck_flags),
213 lp[cnt].ndlck_stateid.seqid,
214 lp[cnt].ndlck_stateid.other[0],
215 lp[cnt].ndlck_stateid.other[1],
216 lp[cnt].ndlck_stateid.other[2]);
218 printf("%-11s %17jd %17jd ",
219 lock_flags(lp[cnt].ndlck_flags),
222 switch (lp[cnt].ndlck_addrfam) {
226 inet_ntoa(lp[cnt].ndlck_cbaddr.sin_addr));
231 if (inet_ntop(AF_INET6, &lp[cnt].ndlck_cbaddr.sin6_addr,
232 nbuf, sizeof(nbuf)) != NULL)
233 printf("%-45s ", nbuf);
235 printf("%-45s ", " ");
239 printf("%-45s ", " ");
242 for (i = 0; i < lp[cnt].ndlck_owner.nclid_idlen; i++)
243 printf("%02x", lp[cnt].ndlck_owner.nclid_id[i]);
245 for (i = 0; i < lp[cnt].ndlck_clid.nclid_idlen; i++)
246 printf("%02x", lp[cnt].ndlck_clid.nclid_id[i]);
253 * Parse the Open/Lock flag bits and create a string to be printed.
256 open_flags(uint32_t flags)
260 strlcpy(flag_string, "Open ", sizeof (flag_string));
262 if (flags & NFSLCK_READACCESS)
263 flag_string[i++] = 'R';
264 if (flags & NFSLCK_WRITEACCESS)
265 flag_string[i++] = 'W';
266 flag_string[i++] = ' ';
267 flag_string[i++] = 'D';
268 flag_string[i] = 'N';
270 if (flags & NFSLCK_READDENY)
271 flag_string[i++] = 'R';
272 if (flags & NFSLCK_WRITEDENY)
273 flag_string[i++] = 'W';
276 flag_string[i] = '\0';
277 return (flag_string);
281 deleg_flags(uint32_t flags)
284 if (flags & NFSLCK_DELEGREAD)
285 strlcpy(flag_string, "Deleg R", sizeof (flag_string));
287 strlcpy(flag_string, "Deleg W", sizeof (flag_string));
288 return (flag_string);
292 lock_flags(uint32_t flags)
295 if (flags & NFSLCK_READ)
296 strlcpy(flag_string, "Lock R", sizeof (flag_string));
298 strlcpy(flag_string, "Lock W", sizeof (flag_string));
299 return (flag_string);
303 client_flags(uint32_t flags)
306 flag_string[0] = '\0';
307 if (flags & LCL_NEEDSCONFIRM)
308 strlcat(flag_string, "NC ", sizeof (flag_string));
309 if (flags & LCL_CALLBACKSON)
310 strlcat(flag_string, "CB ", sizeof (flag_string));
312 strlcat(flag_string, "GSS ", sizeof (flag_string));
313 if (flags & LCL_ADMINREVOKED)
314 strlcat(flag_string, "REV", sizeof (flag_string));
315 return (flag_string);