vnode_if.awk revision 260817
1204431Sraj#!/usr/bin/awk -f 2204431Sraj 3204431Sraj#- 4204431Sraj# Copyright (c) 1992, 1993 5204431Sraj# The Regents of the University of California. All rights reserved. 6204431Sraj# 7204431Sraj# Redistribution and use in source and binary forms, with or without 8204431Sraj# modification, are permitted provided that the following conditions 9204431Sraj# are met: 10204431Sraj# 1. Redistributions of source code must retain the above copyright 11204431Sraj# notice, this list of conditions and the following disclaimer. 12204431Sraj# 2. Redistributions in binary form must reproduce the above copyright 13204431Sraj# notice, this list of conditions and the following disclaimer in the 14204431Sraj# documentation and/or other materials provided with the distribution. 15204431Sraj# 4. Neither the name of the University nor the names of its contributors 16204431Sraj# may be used to endorse or promote products derived from this software 17204431Sraj# without specific prior written permission. 18204431Sraj# 19204431Sraj# THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 20204431Sraj# ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 21204431Sraj# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 22204431Sraj# ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 23204431Sraj# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 24204431Sraj# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 25204431Sraj# OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 26204431Sraj# HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 27204431Sraj# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 28204431Sraj# OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 29204431Sraj# SUCH DAMAGE. 30204431Sraj 31204431Sraj# 32204431Sraj# @(#)vnode_if.sh 8.1 (Berkeley) 6/10/93 33204431Sraj# $FreeBSD: stable/10/sys/tools/vnode_if.awk 260817 2014-01-17 10:58:59Z avg $ 34204431Sraj# 35204431Sraj# Script to produce VFS front-end sugar. 36204431Sraj# 37204431Sraj# usage: vnode_if.awk <srcfile> [-c | -h | -p | -q] 38204431Sraj# (where <srcfile> is currently /sys/kern/vnode_if.src) 39204431Sraj# The source file must have a .src extension 40204431Sraj# 41204431Sraj 42204431Srajfunction usage() 43204431Sraj{ 44204431Sraj print "usage: vnode_if.awk <srcfile> [-c|-h|-p|-q]"; 45204431Sraj exit 1; 46204431Sraj} 47204431Sraj 48204431Srajfunction die(msg, what) 49204431Sraj{ 50204431Sraj printf srcfile "(" fnr "): " > "/dev/stderr"; 51204431Sraj printf msg "\n", what > "/dev/stderr"; 52204431Sraj exit 1; 53204431Sraj} 54204431Sraj 55204431Srajfunction t_spc(type) 56204431Sraj{ 57204431Sraj # Append a space if the type is not a pointer 58204431Sraj return (type ~ /\*$/) ? type : type " "; 59204431Sraj} 60204431Sraj 61204431Sraj# These are just for convenience ... 62204431Srajfunction printc(s) {print s > cfile;} 63204431Srajfunction printh(s) {print s > hfile;} 64204431Srajfunction printp(s) {print s > pfile;} 65204431Srajfunction printq(s) {print s > qfile;} 66204431Sraj 67204431Srajfunction add_debug_code(name, arg, pos, ind) 68204431Sraj{ 69204431Sraj if (arg == "vpp") 70204431Sraj star = "*"; 71204431Sraj else 72204431Sraj star = ""; 73204433Sraj if (lockdata[name, arg, pos] && (lockdata[name, arg, pos] != "-")) { 74204431Sraj printc(ind"ASSERT_VI_UNLOCKED("star"a->a_"arg", \""uname"\");"); 75204431Sraj # Add assertions for locking 76204431Sraj if (lockdata[name, arg, pos] == "L") 77204431Sraj printc(ind"ASSERT_VOP_LOCKED(" star "a->a_"arg", \""uname"\");"); 78204431Sraj else if (lockdata[name, arg, pos] == "U") 79204431Sraj printc(ind"ASSERT_VOP_UNLOCKED(" star "a->a_"arg", \""uname"\");"); 80204431Sraj else if (lockdata[name, arg, pos] == "E") 81204431Sraj printc(ind"ASSERT_VOP_ELOCKED(" star "a->a_"arg", \""uname"\");"); 82204431Sraj else if (0) { 83204431Sraj # XXX More checks! 84204431Sraj } 85204433Sraj } 86204431Sraj} 87204431Sraj 88204431Srajfunction add_pre(name) 89204431Sraj{ 90204431Sraj if (lockdata[name, "pre"]) { 91204431Sraj printc("\t"lockdata[name, "pre"]"(a);"); 92204431Sraj } 93204431Sraj} 94204431Sraj 95204431Srajfunction add_post(name) 96204431Sraj{ 97204431Sraj if (lockdata[name, "post"]) { 98204431Sraj printc("\t"lockdata[name, "post"]"(a, rc);"); 99204431Sraj } 100204431Sraj} 101204431Sraj 102204431Srajfunction find_arg_with_type (type) 103204431Sraj{ 104204431Sraj for (jj = 0; jj < numargs; jj++) { 105204431Sraj if (types[jj] == type) { 106204431Sraj return "VOPARG_OFFSETOF(struct " \ 107204431Sraj name "_args,a_" args[jj] ")"; 108204431Sraj } 109204431Sraj } 110204431Sraj 111204431Sraj return "VDESC_NO_OFFSET"; 112204431Sraj} 113204431Sraj 114204431SrajBEGIN{ 115204431Sraj 116204431Sraj# Process the command line 117204431Srajfor (i = 1; i < ARGC; i++) { 118204431Sraj arg = ARGV[i]; 119204431Sraj if (arg !~ /^-[chpq]+$/ && arg !~ /\.src$/) 120204431Sraj usage(); 121204431Sraj if (arg ~ /^-.*c/) 122204431Sraj cfile = "vnode_if.c"; 123204431Sraj if (arg ~ /^-.*h/) 124204431Sraj hfile = "vnode_if.h"; 125204431Sraj if (arg ~ /^-.*p/) 126204431Sraj pfile = "vnode_if_newproto.h"; 127204431Sraj if (arg ~ /^-.*q/) 128204431Sraj qfile = "vnode_if_typedef.h"; 129204431Sraj if (arg ~ /\.src$/) 130204431Sraj srcfile = arg; 131204431Sraj} 132204431SrajARGC = 1; 133204431Sraj 134204431Srajif (!cfile && !hfile && !pfile && !qfile) 135204431Sraj exit 0; 136204431Sraj 137204431Srajif (!srcfile) 138204431Sraj usage(); 139204431Sraj 140204431Srajcommon_head = \ 141204431Sraj "/*\n" \ 142204431Sraj " * This file is produced automatically.\n" \ 143204431Sraj " * Do not modify anything in here by hand.\n" \ 144204431Sraj " *\n" \ 145204431Sraj " * Created from $FreeBSD: stable/10/sys/tools/vnode_if.awk 260817 2014-01-17 10:58:59Z avg $\n" \ 146204431Sraj " */\n" \ 147204431Sraj "\n"; 148204431Sraj 149204431Srajif (pfile) { 150204431Sraj printp(common_head) 151204431Sraj printp("struct vop_vector {") 152204431Sraj printp("\tstruct vop_vector\t*vop_default;") 153204431Sraj printp("\tvop_bypass_t\t*vop_bypass;") 154204431Sraj} 155204431Sraj 156204431Srajif (qfile) { 157204431Sraj printq(common_head) 158204431Sraj} 159204431Sraj 160204431Srajif (hfile) { 161204431Sraj printh(common_head "extern struct vnodeop_desc vop_default_desc;"); 162204431Sraj printh("#include \"vnode_if_typedef.h\"") 163204431Sraj printh("#include \"vnode_if_newproto.h\"") 164204431Sraj} 165204431Sraj 166204431Srajif (cfile) { 167204431Sraj printc(common_head \ 168204431Sraj "#include \"opt_kdtrace.h\"\n" \ 169204431Sraj "\n" \ 170204431Sraj "#include <sys/param.h>\n" \ 171204431Sraj "#include <sys/event.h>\n" \ 172204431Sraj "#include <sys/kernel.h>\n" \ 173204431Sraj "#include <sys/mount.h>\n" \ 174204431Sraj "#include <sys/sdt.h>\n" \ 175204431Sraj "#include <sys/signalvar.h>\n" \ 176204431Sraj "#include <sys/systm.h>\n" \ 177204431Sraj "#include <sys/vnode.h>\n" \ 178204431Sraj "\n" \ 179204431Sraj "SDT_PROVIDER_DECLARE(vfs);\n" \ 180204431Sraj "\n" \ 181204431Sraj "struct vnodeop_desc vop_default_desc = {\n" \ 182204431Sraj " \"default\",\n" \ 183204431Sraj " 0,\n" \ 184204431Sraj " (vop_bypass_t *)vop_panic,\n" \ 185204431Sraj " NULL,\n" \ 186204431Sraj " VDESC_NO_OFFSET,\n" \ 187204431Sraj " VDESC_NO_OFFSET,\n" \ 188204431Sraj " VDESC_NO_OFFSET,\n" \ 189204431Sraj " VDESC_NO_OFFSET,\n" \ 190204431Sraj "};\n"); 191204431Sraj} 192204431Sraj 193204431Srajwhile ((getline < srcfile) > 0) { 194204431Sraj fnr++; 195204431Sraj if (NF == 0) 196204431Sraj continue; 197204431Sraj if ($1 ~ /^%%/) { 198204431Sraj if (NF != 6 || 199204431Sraj $2 !~ /^[a-z_]+$/ || $3 !~ /^[a-z]+$/ || 200204431Sraj $4 !~ /^.$/ || $5 !~ /^.$/ || $6 !~ /^.$/) { 201204431Sraj die("Invalid %s construction", "%%"); 202204431Sraj continue; 203204431Sraj } 204204431Sraj lockdata["vop_" $2, $3, "Entry"] = $4; 205204431Sraj lockdata["vop_" $2, $3, "OK"] = $5; 206204431Sraj lockdata["vop_" $2, $3, "Error"] = $6; 207204431Sraj continue; 208204431Sraj } 209204431Sraj 210204431Sraj if ($1 ~ /^%!/) { 211204431Sraj if (NF != 4 || 212204431Sraj ($3 != "pre" && $3 != "post")) { 213204431Sraj die("Invalid %s construction", "%!"); 214204431Sraj continue; 215204431Sraj } 216204431Sraj lockdata["vop_" $2, $3] = $4; 217204431Sraj continue; 218204431Sraj } 219204431Sraj if ($1 ~ /^#/) 220204431Sraj continue; 221204431Sraj 222204431Sraj # Get the function name. 223204431Sraj name = $1; 224204431Sraj uname = toupper(name); 225204431Sraj 226204431Sraj # Get the function arguments. 227204431Sraj for (numargs = 0; ; ++numargs) { 228204431Sraj if ((getline < srcfile) <= 0) { 229204431Sraj die("Unable to read through the arguments for \"%s\"", 230204431Sraj name); 231204431Sraj } 232204431Sraj fnr++; 233204431Sraj if ($1 ~ /^\};/) 234204431Sraj break; 235204431Sraj 236204431Sraj # Delete comments, if any. 237204431Sraj gsub (/\/\*.*\*\//, ""); 238204431Sraj 239204431Sraj # Condense whitespace and delete leading/trailing space. 240204433Sraj gsub(/[[:space:]]+/, " "); 241204431Sraj sub(/^ /, ""); 242204431Sraj sub(/ $/, ""); 243204431Sraj 244204431Sraj # Pick off direction. 245204431Sraj if ($1 != "INOUT" && $1 != "IN" && $1 != "OUT") 246204431Sraj die("No IN/OUT direction for \"%s\".", $0); 247204431Sraj dirs[numargs] = $1; 248204431Sraj sub(/^[A-Z]* /, ""); 249204433Sraj 250204433Sraj if ((reles[numargs] = $1) == "WILLRELE") 251204431Sraj sub(/^[A-Z]* /, ""); 252204431Sraj else 253204431Sraj reles[numargs] = "WONTRELE"; 254204431Sraj 255204431Sraj # kill trailing ; 256204431Sraj if (sub(/;$/, "") < 1) 257 die("Missing end-of-line ; in \"%s\".", $0); 258 259 # pick off variable name 260 if ((argp = match($0, /[A-Za-z0-9_]+$/)) < 1) 261 die("Missing var name \"a_foo\" in \"%s\".", $0); 262 args[numargs] = substr($0, argp); 263 $0 = substr($0, 1, argp - 1); 264 265 # what is left must be type 266 # remove trailing space (if any) 267 sub(/ $/, ""); 268 types[numargs] = $0; 269 } 270 if (numargs > 4) 271 ctrargs = 4; 272 else 273 ctrargs = numargs; 274 ctrstr = ctrargs "(KTR_VOP, \"VOP\", \"" uname "\", (uintptr_t)a,\n\t "; 275 ctrstr = ctrstr "\"" args[0] ":0x%jX\", (uintptr_t)a->a_" args[0]; 276 for (i = 1; i < ctrargs; ++i) 277 ctrstr = ctrstr ", \"" args[i] ":0x%jX\", a->a_" args[i]; 278 ctrstr = ctrstr ");"; 279 280 if (pfile) { 281 printp("\t"name"_t\t*"name";") 282 } 283 if (qfile) { 284 printq("struct "name"_args;") 285 printq("typedef int "name"_t(struct "name"_args *);\n") 286 } 287 288 if (hfile) { 289 # Print out the vop_F_args structure. 290 printh("struct "name"_args {\n\tstruct vop_generic_args a_gen;"); 291 for (i = 0; i < numargs; ++i) 292 printh("\t" t_spc(types[i]) "a_" args[i] ";"); 293 printh("};"); 294 printh(""); 295 296 # Print out extern declaration. 297 printh("extern struct vnodeop_desc " name "_desc;"); 298 printh(""); 299 300 # Print out function prototypes. 301 printh("int " uname "_AP(struct " name "_args *);"); 302 printh("int " uname "_APV(struct vop_vector *vop, struct " name "_args *);"); 303 printh(""); 304 printh("static __inline int " uname "("); 305 for (i = 0; i < numargs; ++i) { 306 printh("\t" t_spc(types[i]) args[i] \ 307 (i < numargs - 1 ? "," : ")")); 308 } 309 printh("{"); 310 printh("\tstruct " name "_args a;"); 311 printh(""); 312 printh("\ta.a_gen.a_desc = &" name "_desc;"); 313 for (i = 0; i < numargs; ++i) 314 printh("\ta.a_" args[i] " = " args[i] ";"); 315 printh("\treturn (" uname "_APV("args[0]"->v_op, &a));"); 316 printh("}"); 317 318 printh(""); 319 } 320 321 if (cfile) { 322 # Print out the vop_F_vp_offsets structure. This all depends 323 # on naming conventions and nothing else. 324 printc("static int " name "_vp_offsets[] = {"); 325 # as a side effect, figure out the releflags 326 releflags = ""; 327 vpnum = 0; 328 for (i = 0; i < numargs; i++) { 329 if (types[i] == "struct vnode *") { 330 printc("\tVOPARG_OFFSETOF(struct " name \ 331 "_args,a_" args[i] "),"); 332 if (reles[i] == "WILLRELE") { 333 releflags = releflags \ 334 "|VDESC_VP" vpnum "_WILLRELE"; 335 } 336 vpnum++; 337 } 338 } 339 340 sub(/^\|/, "", releflags); 341 printc("\tVDESC_NO_OFFSET"); 342 printc("};"); 343 344 printc("\n"); 345 printc("SDT_PROBE_DEFINE2(vfs, vop, " name ", entry, \"struct vnode *\", \"struct " name "_args *\");\n"); 346 printc("SDT_PROBE_DEFINE3(vfs, vop, " name ", return, \"struct vnode *\", \"struct " name "_args *\", \"int\");\n"); 347 348 # Print out function. 349 printc("\nint\n" uname "_AP(struct " name "_args *a)"); 350 printc("{"); 351 printc(""); 352 printc("\treturn(" uname "_APV(a->a_" args[0] "->v_op, a));"); 353 printc("}"); 354 printc("\nint\n" uname "_APV(struct vop_vector *vop, struct " name "_args *a)"); 355 printc("{"); 356 printc("\tint rc;"); 357 printc(""); 358 printc("\tVNASSERT(a->a_gen.a_desc == &" name "_desc, a->a_" args[0]","); 359 printc("\t (\"Wrong a_desc in " name "(%p, %p)\", a->a_" args[0]", a));"); 360 printc("\twhile(vop != NULL && \\"); 361 printc("\t vop->"name" == NULL && vop->vop_bypass == NULL)") 362 printc("\t\tvop = vop->vop_default;") 363 printc("\tVNASSERT(vop != NULL, a->a_" args[0]", (\"No "name"(%p, %p)\", a->a_" args[0]", a));") 364 printc("\tSDT_PROBE(vfs, vop, " name ", entry, a->a_" args[0] ", a, 0, 0, 0);\n"); 365 for (i = 0; i < numargs; ++i) 366 add_debug_code(name, args[i], "Entry", "\t"); 367 printc("\tKTR_START" ctrstr); 368 add_pre(name); 369 printc("\tVFS_PROLOGUE(a->a_" args[0]"->v_mount);") 370 printc("\tif (vop->"name" != NULL)") 371 printc("\t\trc = vop->"name"(a);") 372 printc("\telse") 373 printc("\t\trc = vop->vop_bypass(&a->a_gen);") 374 printc("\tVFS_EPILOGUE(a->a_" args[0]"->v_mount);") 375 printc("\tSDT_PROBE(vfs, vop, " name ", return, a->a_" args[0] ", a, rc, 0, 0);\n"); 376 printc("\tif (rc == 0) {"); 377 for (i = 0; i < numargs; ++i) 378 add_debug_code(name, args[i], "OK", "\t\t"); 379 printc("\t} else {"); 380 for (i = 0; i < numargs; ++i) 381 add_debug_code(name, args[i], "Error", "\t\t"); 382 printc("\t}"); 383 add_post(name); 384 printc("\tKTR_STOP" ctrstr); 385 printc("\treturn (rc);"); 386 printc("}\n"); 387 388 # Print out the vnodeop_desc structure. 389 printc("struct vnodeop_desc " name "_desc = {"); 390 # printable name 391 printc("\t\"" name "\","); 392 # flags 393 vppwillrele = ""; 394 for (i = 0; i < numargs; i++) { 395 if (types[i] == "struct vnode **" && \ 396 reles[i] == "WILLRELE") { 397 vppwillrele = "|VDESC_VPP_WILLRELE"; 398 } 399 } 400 401 if (!releflags) 402 releflags = "0"; 403 printc("\t" releflags vppwillrele ","); 404 405 # function to call 406 printc("\t(vop_bypass_t *)" uname "_AP,"); 407 # vp offsets 408 printc("\t" name "_vp_offsets,"); 409 # vpp (if any) 410 printc("\t" find_arg_with_type("struct vnode **") ","); 411 # cred (if any) 412 printc("\t" find_arg_with_type("struct ucred *") ","); 413 # thread (if any) 414 printc("\t" find_arg_with_type("struct thread *") ","); 415 # componentname 416 printc("\t" find_arg_with_type("struct componentname *") ","); 417 # transport layer information 418 printc("};\n"); 419 } 420} 421 422if (pfile) 423 printp("};") 424 425if (hfile) 426 close(hfile); 427if (cfile) 428 close(cfile); 429if (pfile) 430 close(pfile); 431close(srcfile); 432 433exit 0; 434 435} 436