nfsdumpstate.c revision 347010
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: stable/10/usr.sbin/nfsdumpstate/nfsdumpstate.c 347010 2019-05-02 01:17:55Z rmacklem $"); 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 57static void dump_lockstate(char *); 58static void dump_openstate(void); 59static void usage(void); 60static char *open_flags(uint32_t); 61static char *deleg_flags(uint32_t); 62static char *lock_flags(uint32_t); 63static char *client_flags(uint32_t); 64 65static struct nfsd_dumpclients dp[DUMPSIZE]; 66static struct nfsd_dumplocks lp[DUMPSIZE]; 67static char flag_string[20]; 68 69int 70main(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 109static void 110usage(void) 111{ 112 113 errx(1, "usage: nfsdumpstate [-o] [-l]"); 114} 115 116/* 117 * Dump all open/lock state. 118 */ 119static void 120dump_openstate(void) 121{ 122 struct nfsd_dumplist dumplist; 123 int cnt, i; 124 char nbuf[INET6_ADDRSTRLEN]; 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 } 164 for (i = 0; i < dp[cnt].ndcl_clid.nclid_idlen; i++) 165 printf("%02x", dp[cnt].ndcl_clid.nclid_id[i]); 166 printf("\n"); 167 cnt++; 168 } 169} 170 171/* 172 * Dump the lock state for a file. 173 */ 174static void 175dump_lockstate(char *fname) 176{ 177 struct nfsd_dumplocklist dumplocklist; 178 int cnt, i; 179 char nbuf[INET6_ADDRSTRLEN]; 180 181 dumplocklist.ndllck_size = DUMPSIZE; 182 dumplocklist.ndllck_list = (void *)lp; 183 dumplocklist.ndllck_fname = fname; 184 if (nfssvc(NFSSVC_DUMPLOCKS, &dumplocklist) < 0) 185 errx(1, "Can't dump locks for %s\n", fname); 186 187 printf("%-11s %-36s %-45s %s\n", 188 "Open/Lock", 189 " Stateid or Lock Range", 190 "Clientaddr", 191 "Owner and ClientID"); 192 /* 193 * Loop through results, printing them out. 194 */ 195 cnt = 0; 196 while (lp[cnt].ndlck_clid.nclid_idlen > 0 && cnt < DUMPSIZE) { 197 if (lp[cnt].ndlck_flags & NFSLCK_OPEN) 198 printf("%-11s %9d %08x %08x %08x ", 199 open_flags(lp[cnt].ndlck_flags), 200 lp[cnt].ndlck_stateid.seqid, 201 lp[cnt].ndlck_stateid.other[0], 202 lp[cnt].ndlck_stateid.other[1], 203 lp[cnt].ndlck_stateid.other[2]); 204 else if (lp[cnt].ndlck_flags & (NFSLCK_DELEGREAD | 205 NFSLCK_DELEGWRITE)) 206 printf("%-11s %9d %08x %08x %08x ", 207 deleg_flags(lp[cnt].ndlck_flags), 208 lp[cnt].ndlck_stateid.seqid, 209 lp[cnt].ndlck_stateid.other[0], 210 lp[cnt].ndlck_stateid.other[1], 211 lp[cnt].ndlck_stateid.other[2]); 212 else 213 printf("%-11s %17jd %17jd ", 214 lock_flags(lp[cnt].ndlck_flags), 215 lp[cnt].ndlck_first, 216 lp[cnt].ndlck_end); 217 switch (lp[cnt].ndlck_addrfam) { 218#ifdef INET 219 case AF_INET: 220 printf("%-45s ", 221 inet_ntoa(lp[cnt].ndlck_cbaddr.sin_addr)); 222 break; 223#endif 224#ifdef INET6 225 case AF_INET6: 226 if (inet_ntop(AF_INET6, &lp[cnt].ndlck_cbaddr.sin6_addr, 227 nbuf, sizeof(nbuf)) != NULL) 228 printf("%-45s ", nbuf); 229 else 230 printf("%-45s ", " "); 231 break; 232#endif 233 default: 234 printf("%-45s ", " "); 235 break; 236 } 237 for (i = 0; i < lp[cnt].ndlck_owner.nclid_idlen; i++) 238 printf("%02x", lp[cnt].ndlck_owner.nclid_id[i]); 239 printf(" "); 240 for (i = 0; i < lp[cnt].ndlck_clid.nclid_idlen; i++) 241 printf("%02x", lp[cnt].ndlck_clid.nclid_id[i]); 242 printf("\n"); 243 cnt++; 244 } 245} 246 247/* 248 * Parse the Open/Lock flag bits and create a string to be printed. 249 */ 250static char * 251open_flags(uint32_t flags) 252{ 253 int i, j; 254 255 strlcpy(flag_string, "Open ", sizeof (flag_string)); 256 i = 5; 257 if (flags & NFSLCK_READACCESS) 258 flag_string[i++] = 'R'; 259 if (flags & NFSLCK_WRITEACCESS) 260 flag_string[i++] = 'W'; 261 flag_string[i++] = ' '; 262 flag_string[i++] = 'D'; 263 flag_string[i] = 'N'; 264 j = i; 265 if (flags & NFSLCK_READDENY) 266 flag_string[i++] = 'R'; 267 if (flags & NFSLCK_WRITEDENY) 268 flag_string[i++] = 'W'; 269 if (i == j) 270 i++; 271 flag_string[i] = '\0'; 272 return (flag_string); 273} 274 275static char * 276deleg_flags(uint32_t flags) 277{ 278 279 if (flags & NFSLCK_DELEGREAD) 280 strlcpy(flag_string, "Deleg R", sizeof (flag_string)); 281 else 282 strlcpy(flag_string, "Deleg W", sizeof (flag_string)); 283 return (flag_string); 284} 285 286static char * 287lock_flags(uint32_t flags) 288{ 289 290 if (flags & NFSLCK_READ) 291 strlcpy(flag_string, "Lock R", sizeof (flag_string)); 292 else 293 strlcpy(flag_string, "Lock W", sizeof (flag_string)); 294 return (flag_string); 295} 296 297static char * 298client_flags(uint32_t flags) 299{ 300 301 flag_string[0] = '\0'; 302 if (flags & LCL_NEEDSCONFIRM) 303 strlcat(flag_string, "NC ", sizeof (flag_string)); 304 if (flags & LCL_CALLBACKSON) 305 strlcat(flag_string, "CB ", sizeof (flag_string)); 306 if (flags & LCL_GSS) 307 strlcat(flag_string, "GSS ", sizeof (flag_string)); 308 if (flags & LCL_ADMINREVOKED) 309 strlcat(flag_string, "REV", sizeof (flag_string)); 310 return (flag_string); 311} 312