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