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 __FBSDID("$FreeBSD$");
31 #include <sys/param.h>
32 #include <sys/linker.h>
33 #include <sys/module.h>
34 #include <sys/socket.h>
36 #include <arpa/inet.h>
38 #include <netinet/in.h>
40 #include <nfs/nfssvc.h>
42 #include <fs/nfs/rpcv2.h>
43 #include <fs/nfs/nfsproto.h>
44 #include <fs/nfs/nfskpiport.h>
45 #include <fs/nfs/nfs.h>
55 #define DUMPSIZE 10000
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);
65 static struct nfsd_dumpclients dp[DUMPSIZE];
66 static struct nfsd_dumplocks lp[DUMPSIZE];
67 static char flag_string[20];
70 main(int argc, char **argv)
75 if (modfind("nfsd") < 0)
76 errx(1, "nfsd not loaded - self terminating");
79 while ((ch = getopt(argc, argv, "ol:")) != -1)
93 if (openstate == 0 && lockfile == NULL)
95 else if (openstate != 0 && lockfile != NULL)
96 errx(1, "-o and -l cannot both be specified");
99 * For -o, dump all open/lock state.
100 * For -l, dump lock state for that file.
105 dump_lockstate(lockfile);
113 errx(1, "usage: nfsdumpstate [-o] [-l]");
117 * Dump all open/lock state.
122 struct nfsd_dumplist dumplist;
124 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 for (i = 0; i < dp[cnt].ndcl_clid.nclid_idlen; i++)
165 printf("%02x", dp[cnt].ndcl_clid.nclid_id[i]);
172 * Dump the lock state for a file.
175 dump_lockstate(char *fname)
177 struct nfsd_dumplocklist dumplocklist;
180 dumplocklist.ndllck_size = DUMPSIZE;
181 dumplocklist.ndllck_list = (void *)lp;
182 dumplocklist.ndllck_fname = fname;
183 if (nfssvc(NFSSVC_DUMPLOCKS, &dumplocklist) < 0)
184 errx(1, "Can't dump locks for %s\n", fname);
186 printf("%-11s %-36s %-15s %s\n",
188 " Stateid or Lock Range",
190 "Owner and ClientID");
192 * Loop through results, printing them out.
195 while (lp[cnt].ndlck_clid.nclid_idlen > 0 && cnt < DUMPSIZE) {
196 if (lp[cnt].ndlck_flags & NFSLCK_OPEN)
197 printf("%-11s %9d %08x %08x %08x ",
198 open_flags(lp[cnt].ndlck_flags),
199 lp[cnt].ndlck_stateid.seqid,
200 lp[cnt].ndlck_stateid.other[0],
201 lp[cnt].ndlck_stateid.other[1],
202 lp[cnt].ndlck_stateid.other[2]);
203 else if (lp[cnt].ndlck_flags & (NFSLCK_DELEGREAD |
205 printf("%-11s %9d %08x %08x %08x ",
206 deleg_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]);
212 printf("%-11s %17jd %17jd ",
213 lock_flags(lp[cnt].ndlck_flags),
216 if (lp[cnt].ndlck_addrfam == AF_INET)
218 inet_ntoa(lp[cnt].ndlck_cbaddr.sin_addr));
220 printf("%-15s ", " ");
221 for (i = 0; i < lp[cnt].ndlck_owner.nclid_idlen; i++)
222 printf("%02x", lp[cnt].ndlck_owner.nclid_id[i]);
224 for (i = 0; i < lp[cnt].ndlck_clid.nclid_idlen; i++)
225 printf("%02x", lp[cnt].ndlck_clid.nclid_id[i]);
232 * Parse the Open/Lock flag bits and create a string to be printed.
235 open_flags(uint32_t flags)
239 strlcpy(flag_string, "Open ", sizeof (flag_string));
241 if (flags & NFSLCK_READACCESS)
242 flag_string[i++] = 'R';
243 if (flags & NFSLCK_WRITEACCESS)
244 flag_string[i++] = 'W';
245 flag_string[i++] = ' ';
246 flag_string[i++] = 'D';
247 flag_string[i] = 'N';
249 if (flags & NFSLCK_READDENY)
250 flag_string[i++] = 'R';
251 if (flags & NFSLCK_WRITEDENY)
252 flag_string[i++] = 'W';
255 flag_string[i] = '\0';
256 return (flag_string);
260 deleg_flags(uint32_t flags)
263 if (flags & NFSLCK_DELEGREAD)
264 strlcpy(flag_string, "Deleg R", sizeof (flag_string));
266 strlcpy(flag_string, "Deleg W", sizeof (flag_string));
267 return (flag_string);
271 lock_flags(uint32_t flags)
274 if (flags & NFSLCK_READ)
275 strlcpy(flag_string, "Lock R", sizeof (flag_string));
277 strlcpy(flag_string, "Lock W", sizeof (flag_string));
278 return (flag_string);
282 client_flags(uint32_t flags)
285 flag_string[0] = '\0';
286 if (flags & LCL_NEEDSCONFIRM)
287 strlcat(flag_string, "NC ", sizeof (flag_string));
288 if (flags & LCL_CALLBACKSON)
289 strlcat(flag_string, "CB ", sizeof (flag_string));
291 strlcat(flag_string, "GSS ", sizeof (flag_string));
292 if (flags & LCL_ADMINREVOKED)
293 strlcat(flag_string, "REV", sizeof (flag_string));
294 return (flag_string);