1#!/usr/bin/awk -f 2 3#- 4# SPDX-License-Identifier: BSD-3-Clause 5# 6# Copyright (c) 1992, 1993 7# The Regents of the University of California. All rights reserved. 8# 9# Redistribution and use in source and binary forms, with or without 10# modification, are permitted provided that the following conditions 11# are met: 12# 1. Redistributions of source code must retain the above copyright 13# notice, this list of conditions and the following disclaimer. 14# 2. Redistributions in binary form must reproduce the above copyright 15# notice, this list of conditions and the following disclaimer in the 16# documentation and/or other materials provided with the distribution. 17# 3. Neither the name of the University nor the names of its contributors 18# may be used to endorse or promote products derived from this software 19# without specific prior written permission. 20# 21# THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 22# ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 23# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 24# ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 25# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 26# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 27# OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 28# HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 29# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 30# OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 31# SUCH DAMAGE. 32 33# Script to produce VFS front-end sugar. 34# 35# usage: vnode_if.awk <srcfile> [-c | -h | -p | -q] 36# (where <srcfile> is currently /sys/kern/vnode_if.src) 37# The source file must have a .src extension 38# 39 40function usage() 41{ 42 print "usage: vnode_if.awk <srcfile> [-c|-h|-p|-q]"; 43 exit 1; 44} 45 46function die(msg, what) 47{ 48 printf srcfile "(" fnr "): " > "/dev/stderr"; 49 printf msg "\n", what > "/dev/stderr"; 50 exit 1; 51} 52 53function t_spc(type) 54{ 55 # Append a space if the type is not a pointer 56 return (type ~ /\*$/) ? type : type " "; 57} 58 59# These are just for convenience ... 60function printc(s) {print s > cfile;} 61function printh(s) {print s > hfile;} 62function printp(s) {print s > pfile;} 63function printq(s) {print s > qfile;} 64 65function add_debug_code(name, arg, pos, ind) 66{ 67 if (arg == "vpp") 68 star = "*"; 69 else 70 star = ""; 71 if (lockdata[name, arg, pos] && (lockdata[name, arg, pos] != "-")) { 72 printc(ind"ASSERT_VI_UNLOCKED("star"a->a_"arg", \""uname" "pos" ("arg")\");"); 73 # Add assertions for locking 74 if (lockdata[name, arg, pos] == "L") 75 printc(ind"ASSERT_VOP_LOCKED(" star "a->a_"arg", \""uname" "pos" ("arg")\");"); 76 else if (lockdata[name, arg, pos] == "U") 77 printc(ind"ASSERT_VOP_UNLOCKED(" star "a->a_"arg", \""uname" "pos" ("arg")\");"); 78 else if (lockdata[name, arg, pos] == "E") 79 printc(ind"ASSERT_VOP_ELOCKED(" star "a->a_"arg", \""uname" "pos" ("arg")\");"); 80 else if (0) { 81 # XXX More checks! 82 } 83 } 84} 85 86function add_debugpre(name) 87{ 88 if (lockdata[name, "debugpre"]) { 89 printc("#ifdef DEBUG_VFS_LOCKS"); 90 printc("\t"lockdata[name, "debugpre"]"(a);"); 91 printc("#endif"); 92 } 93} 94 95function add_debugpost(name) 96{ 97 if (lockdata[name, "debugpost"]) { 98 printc("#ifdef DEBUG_VFS_LOCKS"); 99 printc("\t"lockdata[name, "debugpost"]"(a, rc);"); 100 printc("#endif"); 101 } 102} 103 104function add_pre(name) 105{ 106 if (lockdata[name, "pre"]) { 107 printc("\t"lockdata[name, "pre"]"(a);"); 108 } 109} 110 111function add_post(name) 112{ 113 if (lockdata[name, "post"]) { 114 printc("\t"lockdata[name, "post"]"(a, rc);"); 115 } 116} 117 118function can_inline(name) 119{ 120 if (lockdata[name, "pre"]) 121 return 0; 122 if (lockdata[name, "post"]) 123 return 0; 124 return 1; 125} 126 127function find_arg_with_type (type) 128{ 129 for (jj = 0; jj < numargs; jj++) { 130 if (types[jj] == type) { 131 return "VOPARG_OFFSETOF(struct " \ 132 name "_args,a_" args[jj] ")"; 133 } 134 } 135 136 return "VDESC_NO_OFFSET"; 137} 138 139BEGIN{ 140 141# Process the command line 142for (i = 1; i < ARGC; i++) { 143 arg = ARGV[i]; 144 if (arg !~ /^-[chpq]+$/ && arg !~ /\.src$/) 145 usage(); 146 if (arg ~ /^-.*c/) 147 cfile = "vnode_if.c"; 148 if (arg ~ /^-.*h/) 149 hfile = "vnode_if.h"; 150 if (arg ~ /^-.*p/) 151 pfile = "vnode_if_newproto.h"; 152 if (arg ~ /^-.*q/) 153 qfile = "vnode_if_typedef.h"; 154 if (arg ~ /\.src$/) 155 srcfile = arg; 156} 157ARGC = 1; 158 159if (!cfile && !hfile && !pfile && !qfile) 160 exit 0; 161 162if (!srcfile) 163 usage(); 164 165# Avoid a literal generated file tag here. 166generated = "@" "generated"; 167 168common_head = \ 169 "/*\n" \ 170 " * This file is " generated " automatically.\n" \ 171 " * Do not modify anything in here by hand.\n" \ 172 " */\n" \ 173 "\n"; 174 175if (pfile) { 176 printp(common_head) 177 printp("struct vop_vector {") 178 printp("\tstruct vop_vector\t*vop_default;") 179 printp("\tvop_bypass_t\t*vop_bypass;") 180} 181 182if (qfile) { 183 printq(common_head) 184} 185 186if (hfile) { 187 printh(common_head "extern struct vnodeop_desc vop_default_desc;"); 188 printh("#include \"vnode_if_typedef.h\"") 189 printh("#include \"vnode_if_newproto.h\"") 190} 191 192if (cfile) { 193 printc(common_head \ 194 "#include <sys/param.h>\n" \ 195 "#include <sys/event.h>\n" \ 196 "#include <sys/inotify.h>\n" \ 197 "#include <sys/kernel.h>\n" \ 198 "#include <sys/mount.h>\n" \ 199 "#include <sys/sdt.h>\n" \ 200 "#include <sys/signalvar.h>\n" \ 201 "#include <sys/systm.h>\n" \ 202 "#include <sys/vnode.h>\n" \ 203 "\n" \ 204 "SDT_PROVIDER_DECLARE(vfs);\n" \ 205 "\n" \ 206 "struct vnodeop_desc vop_default_desc = {\n" \ 207 " \"default\",\n" \ 208 " 0,\n" \ 209 " 0,\n" \ 210 " (vop_bypass_t *)vop_panic,\n" \ 211 " NULL,\n" \ 212 " VDESC_NO_OFFSET,\n" \ 213 " VDESC_NO_OFFSET,\n" \ 214 " VDESC_NO_OFFSET,\n" \ 215 " VDESC_NO_OFFSET,\n" \ 216 "};\n"); 217} 218 219while ((getline < srcfile) > 0) { 220 fnr++; 221 if (NF == 0) 222 continue; 223 if ($1 ~ /^%%/) { 224 if (NF != 6 || 225 $2 !~ /^[a-z_]+$/ || $3 !~ /^[a-z]+$/ || 226 $4 !~ /^.$/ || $5 !~ /^.$/ || $6 !~ /^.$/) { 227 die("Invalid %s construction", "%%"); 228 continue; 229 } 230 lockdata["vop_" $2, $3, "Entry"] = $4; 231 lockdata["vop_" $2, $3, "OK"] = $5; 232 lockdata["vop_" $2, $3, "Error"] = $6; 233 continue; 234 } 235 236 if ($1 ~ /^%!/) { 237 if (NF != 4 || 238 ($3 != "pre" && $3 != "post" && 239 $3 != "debugpre" && $3 != "debugpost")) { 240 die("Invalid %s construction", "%!"); 241 continue; 242 } 243 lockdata["vop_" $2, $3] = $4; 244 continue; 245 } 246 if ($1 ~ /^#/) 247 continue; 248 249 # Get the function name. 250 name = $1; 251 uname = toupper(name); 252 253 # Get the function arguments. 254 for (numargs = 0; ; ++numargs) { 255 if ((getline < srcfile) <= 0) { 256 die("Unable to read through the arguments for \"%s\"", 257 name); 258 } 259 fnr++; 260 if ($1 ~ /^\};/) 261 break; 262 263 # Delete comments, if any. 264 gsub (/\/\*.*\*\//, ""); 265 266 # Condense whitespace and delete leading/trailing space. 267 gsub(/[[:space:]]+/, " "); 268 sub(/^ /, ""); 269 sub(/ $/, ""); 270 271 # Pick off direction. 272 if ($1 != "INOUT" && $1 != "IN" && $1 != "OUT") 273 die("No IN/OUT direction for \"%s\".", $0); 274 dirs[numargs] = $1; 275 sub(/^[A-Z]* /, ""); 276 277 if ((reles[numargs] = $1) == "WILLRELE") 278 sub(/^[A-Z]* /, ""); 279 else 280 reles[numargs] = "WONTRELE"; 281 282 # kill trailing ; 283 if (sub(/;$/, "") < 1) 284 die("Missing end-of-line ; in \"%s\".", $0); 285 286 # pick off variable name 287 if ((argp = match($0, /[A-Za-z0-9_]+$/)) < 1) 288 die("Missing var name \"a_foo\" in \"%s\".", $0); 289 args[numargs] = substr($0, argp); 290 $0 = substr($0, 1, argp - 1); 291 292 # what is left must be type 293 # remove trailing space (if any) 294 sub(/ $/, ""); 295 types[numargs] = $0; 296 } 297 if (numargs > 4) 298 ctrargs = 4; 299 else 300 ctrargs = numargs; 301 ctrstr = ctrargs "(KTR_VOP, \"VOP\", \"" uname "\", (uintptr_t)a,\n\t "; 302 ctrstr = ctrstr "\"" args[0] ":0x%jX\", (uintptr_t)a->a_" args[0]; 303 for (i = 1; i < ctrargs; ++i) 304 ctrstr = ctrstr ", \"" args[i] ":0x%jX\", a->a_" args[i]; 305 ctrstr = ctrstr ");"; 306 307 if (pfile) { 308 printp("\t"name"_t\t*"name";") 309 } 310 if (qfile) { 311 printq("struct "name"_args;") 312 printq("typedef int "name"_t(struct "name"_args *);\n") 313 } 314 315 if (hfile) { 316 # Print out the vop_F_args structure. 317 printh("struct "name"_args {\n\tstruct vop_generic_args a_gen;"); 318 for (i = 0; i < numargs; ++i) 319 printh("\t" t_spc(types[i]) "a_" args[i] ";"); 320 printh("};"); 321 printh(""); 322 323 # Print out extern declaration. 324 printh("extern struct vnodeop_desc " name "_desc;"); 325 printh(""); 326 327 # Print out function prototypes. 328 printh("int " uname "_AP(struct " name "_args *);"); 329 printh("int " uname "_APV(const struct vop_vector *vop, struct " name "_args *);"); 330 printh(""); 331 printh("static __inline int " uname "("); 332 for (i = 0; i < numargs; ++i) { 333 printh("\t" t_spc(types[i]) args[i] \ 334 (i < numargs - 1 ? "," : ")")); 335 } 336 printh("{"); 337 printh("\tstruct " name "_args a;"); 338 printh(""); 339 printh("\ta.a_gen.a_desc = &" name "_desc;"); 340 for (i = 0; i < numargs; ++i) 341 printh("\ta.a_" args[i] " = " args[i] ";"); 342 if (can_inline(name)) { 343 printh("\n#if !defined(DEBUG_VFS_LOCKS) && !defined(INVARIANTS) && !defined(KTR)"); 344 printh("\tif (!SDT_PROBES_ENABLED())"); 345 printh("\t\treturn (" args[0]"->v_op->"name"(&a));"); 346 printh("\telse"); 347 printh("\t\treturn (" uname "_APV("args[0]"->v_op, &a));"); 348 printh("#else"); 349 } 350 printh("\treturn (" uname "_APV("args[0]"->v_op, &a));"); 351 if (can_inline(name)) 352 printh("#endif"); 353 354 printh("}"); 355 356 printh(""); 357 } 358 359 if (cfile) { 360 funcarr[name] = 1; 361 # Print out the vop_F_vp_offsets structure. This all depends 362 # on naming conventions and nothing else. 363 printc("static int " name "_vp_offsets[] = {"); 364 # as a side effect, figure out the releflags 365 releflags = ""; 366 vpnum = 0; 367 for (i = 0; i < numargs; i++) { 368 if (types[i] == "struct vnode *") { 369 printc("\tVOPARG_OFFSETOF(struct " name \ 370 "_args,a_" args[i] "),"); 371 if (reles[i] == "WILLRELE") { 372 releflags = releflags \ 373 "|VDESC_VP" vpnum "_WILLRELE"; 374 } 375 vpnum++; 376 } 377 } 378 379 sub(/^\|/, "", releflags); 380 printc("\tVDESC_NO_OFFSET"); 381 printc("};"); 382 383 printc("\n"); 384 printc("SDT_PROBE_DEFINE2(vfs, vop, " name ", entry, \"struct vnode *\", \"struct " name "_args *\");\n"); 385 printc("SDT_PROBE_DEFINE3(vfs, vop, " name ", return, \"struct vnode *\", \"struct " name "_args *\", \"int\");\n"); 386 387 # Print out function. 388 printc("\nint\n" uname "_AP(struct " name "_args *a)"); 389 printc("{"); 390 printc(""); 391 printc("\treturn(" uname "_APV(a->a_" args[0] "->v_op, a));"); 392 printc("}"); 393 printc("\nint\n" uname "_APV(const struct vop_vector *vop, struct " name "_args *a)"); 394 printc("{"); 395 printc("\tint rc;"); 396 printc(""); 397 printc("\tVNASSERT(a->a_gen.a_desc == &" name "_desc, a->a_" args[0]","); 398 printc("\t (\"Wrong a_desc in " name "(%p, %p)\", a->a_" args[0]", a));"); 399 printc("\tVNASSERT(vop != NULL, a->a_" args[0]", (\"No "name"(%p, %p)\", a->a_" args[0]", a));") 400 printc("\tKTR_START" ctrstr); 401 add_debugpre(name); 402 add_pre(name); 403 for (i = 0; i < numargs; ++i) 404 add_debug_code(name, args[i], "Entry", "\t"); 405 printc("\tSDT_PROBE2(vfs, vop, " name ", entry, a->a_" args[0] ", a);"); 406 printc("\trc = vop->"name"(a);") 407 printc("\tSDT_PROBE3(vfs, vop, " name ", return, a->a_" args[0] ", a, rc);"); 408 printc("\tif (rc == 0) {"); 409 for (i = 0; i < numargs; ++i) 410 add_debug_code(name, args[i], "OK", "\t\t"); 411 printc("\t} else {"); 412 for (i = 0; i < numargs; ++i) 413 add_debug_code(name, args[i], "Error", "\t\t"); 414 printc("\t}"); 415 add_post(name); 416 add_debugpost(name); 417 printc("\tKTR_STOP" ctrstr); 418 printc("\treturn (rc);"); 419 printc("}\n"); 420 421 # Print out the vnodeop_desc structure. 422 printc("struct vnodeop_desc " name "_desc = {"); 423 # printable name 424 printc("\t\"" name "\","); 425 # flags 426 vppwillrele = ""; 427 for (i = 0; i < numargs; i++) { 428 if (types[i] == "struct vnode **" && \ 429 reles[i] == "WILLRELE") { 430 vppwillrele = "|VDESC_VPP_WILLRELE"; 431 } 432 } 433 434 if (!releflags) 435 releflags = "0"; 436 printc("\t" releflags vppwillrele ","); 437 438 # index in struct vop_vector 439 printc("\t__offsetof(struct vop_vector, " name "),"); 440 # function to call 441 printc("\t(vop_bypass_t *)" uname "_AP,"); 442 # vp offsets 443 printc("\t" name "_vp_offsets,"); 444 # vpp (if any) 445 printc("\t" find_arg_with_type("struct vnode **") ","); 446 # cred (if any) 447 printc("\t" find_arg_with_type("struct ucred *") ","); 448 # thread (if any) 449 printc("\t" find_arg_with_type("struct thread *") ","); 450 # componentname 451 printc("\t" find_arg_with_type("struct componentname *") ","); 452 # transport layer information 453 printc("};\n"); 454 } 455} 456 457if (cfile) { 458 printc("void"); 459 printc("vfs_vector_op_register(struct vop_vector *orig_vop)"); 460 printc("{"); 461 printc("\tstruct vop_vector *vop;"); 462 printc(""); 463 printc("\tif (orig_vop->registered)"); 464 printc("\t\tpanic(\"%s: vop_vector %p already registered\",") 465 printc("\t\t __func__, orig_vop);"); 466 printc(""); 467 printc("\tcache_vop_vector_register(orig_vop);"); 468 printc(""); 469 for (name in funcarr) { 470 printc("\tvop = orig_vop;"); 471 printc("\twhile (vop != NULL && \\"); 472 printc("\t vop->"name" == NULL && vop->vop_bypass == NULL)") 473 printc("\t\tvop = vop->vop_default;") 474 printc("\tif (vop != NULL)"); 475 printc("\t\torig_vop->"name" = vop->"name";"); 476 printc(""); 477 } 478 printc("\tvop = orig_vop;"); 479 printc("\twhile (vop != NULL && vop->vop_bypass == NULL)") 480 printc("\t\tvop = vop->vop_default;") 481 printc("\tif (vop != NULL)"); 482 printc("\t\torig_vop->vop_bypass = vop->vop_bypass;"); 483 printc(""); 484 for (name in funcarr) { 485 printc("\tif (orig_vop->"name" == NULL)"); 486 printc("\t\torig_vop->"name" = (void *)orig_vop->vop_bypass;"); 487 } 488 printc(""); 489 printc("\torig_vop->registered = true;"); 490 printc("}") 491} 492 493if (hfile) { 494 printh("void vfs_vector_op_register(struct vop_vector *orig_vop);"); 495} 496 497if (pfile) { 498 printp("\tbool\tregistered;") 499 printp("};") 500} 501 502if (hfile) 503 close(hfile); 504if (cfile) 505 close(cfile); 506if (pfile) 507 close(pfile); 508close(srcfile); 509 510exit 0; 511 512} 513