vnode_if.awk revision 289798
1204917Sdes#!/usr/bin/awk -f
257429Smarkm
392555Sdes#-
457429Smarkm# Copyright (c) 1992, 1993
557429Smarkm#	The Regents of the University of California.  All rights reserved.
657429Smarkm#
757429Smarkm# Redistribution and use in source and binary forms, with or without
857429Smarkm# modification, are permitted provided that the following conditions
957429Smarkm# are met:
1057429Smarkm# 1. Redistributions of source code must retain the above copyright
1157429Smarkm#    notice, this list of conditions and the following disclaimer.
1257429Smarkm# 2. Redistributions in binary form must reproduce the above copyright
1357429Smarkm#    notice, this list of conditions and the following disclaimer in the
1457429Smarkm#    documentation and/or other materials provided with the distribution.
1557429Smarkm# 4. Neither the name of the University nor the names of its contributors
1657429Smarkm#    may be used to endorse or promote products derived from this software
1757429Smarkm#    without specific prior written permission.
1857429Smarkm#
1957429Smarkm# THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
2057429Smarkm# ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
2157429Smarkm# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
2257429Smarkm# ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
2357429Smarkm# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
2457429Smarkm# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
2557429Smarkm# OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
2657429Smarkm# HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
2757429Smarkm# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28162852Sdes# OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29162852Sdes# SUCH DAMAGE.
30162852Sdes
31162852Sdes#
32162852Sdes#	@(#)vnode_if.sh	8.1 (Berkeley) 6/10/93
33162852Sdes# $FreeBSD: stable/10/sys/tools/vnode_if.awk 289798 2015-10-23 07:40:43Z avg $
34162852Sdes#
35181111Sdes# Script to produce VFS front-end sugar.
3676259Sgreen#
3776259Sgreen# usage: vnode_if.awk <srcfile> [-c | -h | -p | -q]
3857429Smarkm#	(where <srcfile> is currently /sys/kern/vnode_if.src)
3957429Smarkm#	The source file must have a .src extension
4057429Smarkm#
4160573Skris
4276259Sgreenfunction usage()
4357429Smarkm{
4492555Sdes	print "usage: vnode_if.awk <srcfile> [-c|-h|-p|-q]";
4592555Sdes	exit 1;
4692555Sdes}
4792555Sdes
4892555Sdesfunction die(msg, what)
4992555Sdes{
5092555Sdes	printf srcfile "(" fnr "): " > "/dev/stderr";
5192555Sdes	printf msg "\n", what > "/dev/stderr";
5292555Sdes	exit 1;
53137015Sdes}
54137015Sdes
5592555Sdesfunction t_spc(type)
5692555Sdes{
57137015Sdes	# Append a space if the type is not a pointer
58137015Sdes	return (type ~ /\*$/) ? type : type " ";
59137015Sdes}
60137015Sdes
61137015Sdes# These are just for convenience ...
6292555Sdesfunction printc(s) {print s > cfile;}
6392555Sdesfunction printh(s) {print s > hfile;}
6492555Sdesfunction printp(s) {print s > pfile;}
6592555Sdesfunction printq(s) {print s > qfile;}
6692555Sdes
6792555Sdesfunction add_debug_code(name, arg, pos, ind)
6892555Sdes{
6992555Sdes	if (arg == "vpp")
7060573Skris		star = "*";
7157429Smarkm	else
7257429Smarkm		star = "";
7357429Smarkm	if (lockdata[name, arg, pos] && (lockdata[name, arg, pos] != "-")) {
7460573Skris		printc(ind"ASSERT_VI_UNLOCKED("star"a->a_"arg", \""uname"\");");
7560573Skris		# Add assertions for locking
7660573Skris		if (lockdata[name, arg, pos] == "L")
7792555Sdes			printc(ind"ASSERT_VOP_LOCKED(" star "a->a_"arg", \""uname"\");");
7892555Sdes		else if (lockdata[name, arg, pos] == "U")
7992555Sdes			printc(ind"ASSERT_VOP_UNLOCKED(" star "a->a_"arg", \""uname"\");");
8092555Sdes		else if (lockdata[name, arg, pos] == "E")
81181111Sdes			printc(ind"ASSERT_VOP_ELOCKED(" star "a->a_"arg", \""uname"\");");
8257429Smarkm		else if (0) {
8360573Skris			# XXX More checks!
8492555Sdes		}
8592555Sdes	}
8660573Skris}
8792555Sdes
8892555Sdesfunction add_pre(name)
8992555Sdes{
9092555Sdes	if (lockdata[name, "pre"]) {
9192555Sdes		printc("\t"lockdata[name, "pre"]"(a);");
9292555Sdes	}
9392555Sdes}
9492555Sdes
95124208Sdesfunction add_post(name)
9692555Sdes{
9792555Sdes	if (lockdata[name, "post"]) {
9892555Sdes		printc("\t"lockdata[name, "post"]"(a, rc);");
9992555Sdes	}
10092555Sdes}
10192555Sdes
10292555Sdesfunction find_arg_with_type (type)
10392555Sdes{
104124208Sdes	for (jj = 0; jj < numargs; jj++) {
10592555Sdes		if (types[jj] == type) {
10692555Sdes			return "VOPARG_OFFSETOF(struct " \
10792555Sdes			    name "_args,a_" args[jj] ")";
10892555Sdes		}
10960573Skris	}
11060573Skris
11160573Skris	return "VDESC_NO_OFFSET";
11260573Skris}
11360573Skris
11460573SkrisBEGIN{
11557429Smarkm
116124208Sdes# Process the command line
11757429Smarkmfor (i = 1; i < ARGC; i++) {
11857429Smarkm	arg = ARGV[i];
11992555Sdes	if (arg !~ /^-[chpq]+$/ && arg !~ /\.src$/)
12057429Smarkm		usage();
12157429Smarkm	if (arg ~ /^-.*c/)
12257429Smarkm		cfile = "vnode_if.c";
12360573Skris	if (arg ~ /^-.*h/)
12492555Sdes		hfile = "vnode_if.h";
12557429Smarkm	if (arg ~ /^-.*p/)
12657429Smarkm		pfile = "vnode_if_newproto.h";
12757429Smarkm	if (arg ~ /^-.*q/)
12860573Skris		qfile = "vnode_if_typedef.h";
12992555Sdes	if (arg ~ /\.src$/)
13057429Smarkm		srcfile = arg;
13157429Smarkm}
13292555SdesARGC = 1;
13360573Skris
13457429Smarkmif (!cfile && !hfile && !pfile && !qfile)
13557429Smarkm	exit 0;
13657429Smarkm
13792555Sdesif (!srcfile)
13892555Sdes	usage();
13957429Smarkm
140124208Sdescommon_head = \
14157429Smarkm    "/*\n" \
14257429Smarkm    " * This file is produced automatically.\n" \
14357429Smarkm    " * Do not modify anything in here by hand.\n" \
14492555Sdes    " *\n" \
14557429Smarkm    " * Created from $FreeBSD: stable/10/sys/tools/vnode_if.awk 289798 2015-10-23 07:40:43Z avg $\n" \
14657429Smarkm    " */\n" \
14792555Sdes    "\n";
14860573Skris
14957429Smarkmif (pfile) {
15057429Smarkm	printp(common_head)
15157429Smarkm	printp("struct vop_vector {")
15292555Sdes	printp("\tstruct vop_vector\t*vop_default;")
15392555Sdes	printp("\tvop_bypass_t\t*vop_bypass;")
15457429Smarkm}
155124208Sdes
15657429Smarkmif (qfile) {
15792555Sdes	printq(common_head)
15860573Skris}
15957429Smarkm
16057429Smarkmif (hfile) {
16157429Smarkm	printh(common_head "extern struct vnodeop_desc vop_default_desc;");
16257429Smarkm	printh("#include \"vnode_if_typedef.h\"")
16392555Sdes	printh("#include \"vnode_if_newproto.h\"")
164204917Sdes}
16592555Sdes
16692555Sdesif (cfile) {
16792555Sdes	printc(common_head \
16892555Sdes	    "#include \"opt_kdtrace.h\"\n" \
16992555Sdes	    "\n" \
17092555Sdes	    "#include <sys/param.h>\n" \
17157429Smarkm	    "#include <sys/event.h>\n" \
17257429Smarkm	    "#include <sys/kernel.h>\n" \
17392555Sdes	    "#include <sys/mount.h>\n" \
17460573Skris	    "#include <sys/sdt.h>\n" \
17557429Smarkm	    "#include <sys/signalvar.h>\n" \
17657429Smarkm	    "#include <sys/systm.h>\n" \
17757429Smarkm	    "#include <sys/vnode.h>\n" \
17860573Skris	    "\n" \
17960573Skris	    "SDT_PROVIDER_DECLARE(vfs);\n" \
18057429Smarkm	    "\n" \
181124208Sdes	    "struct vnodeop_desc vop_default_desc = {\n" \
18257429Smarkm	    "	\"default\",\n" \
18357429Smarkm	    "	0,\n" \
18492555Sdes	    "	(vop_bypass_t *)vop_panic,\n" \
18557429Smarkm	    "	NULL,\n" \
18657429Smarkm	    "	VDESC_NO_OFFSET,\n" \
18792555Sdes	    "	VDESC_NO_OFFSET,\n" \
18857429Smarkm	    "	VDESC_NO_OFFSET,\n" \
18957429Smarkm	    "	VDESC_NO_OFFSET,\n" \
19092555Sdes	    "};\n");
19160573Skris}
19257429Smarkm
19357429Smarkmwhile ((getline < srcfile) > 0) {
19457429Smarkm	fnr++;
19560573Skris	if (NF == 0)
19660573Skris		continue;
19757429Smarkm	if ($1 ~ /^%%/) {
198124208Sdes		if (NF != 6 ||
19957429Smarkm		    $2 !~ /^[a-z_]+$/  ||  $3 !~ /^[a-z]+$/  ||
20057429Smarkm		    $4 !~ /^.$/  ||  $5 !~ /^.$/  ||  $6 !~ /^.$/) {
20192555Sdes			die("Invalid %s construction", "%%");
20260573Skris			continue;
20392555Sdes		}
20457429Smarkm		lockdata["vop_" $2, $3, "Entry"] = $4;
20557429Smarkm		lockdata["vop_" $2, $3, "OK"]    = $5;
20692555Sdes		lockdata["vop_" $2, $3, "Error"] = $6;			
20760573Skris		continue;
20892555Sdes	}
20957429Smarkm
21057429Smarkm	if ($1 ~ /^%!/) {
21192555Sdes		if (NF != 4 ||
21260573Skris		    ($3 != "pre" && $3 != "post")) {
21357429Smarkm			die("Invalid %s construction", "%!");
21457429Smarkm			continue;
21557429Smarkm		}
21692555Sdes		lockdata["vop_" $2, $3] = $4;
21792555Sdes		continue;
21857429Smarkm	}
219124208Sdes	if ($1 ~ /^#/)
22057429Smarkm		continue;
22192555Sdes
22260573Skris	# Get the function name.
22357429Smarkm	name = $1;
22457429Smarkm	uname = toupper(name);
22557429Smarkm
22657429Smarkm	# Get the function arguments.
22792555Sdes	for (numargs = 0; ; ++numargs) {
22892555Sdes		if ((getline < srcfile) <= 0) {
22992555Sdes			die("Unable to read through the arguments for \"%s\"",
23092555Sdes			    name);
23157429Smarkm		}
23257429Smarkm		fnr++;
23392555Sdes		if ($1 ~ /^\};/)
23460573Skris			break;
23557429Smarkm
23657429Smarkm		# Delete comments, if any.
23757429Smarkm		gsub (/\/\*.*\*\//, "");
23857429Smarkm
23960573Skris		# Condense whitespace and delete leading/trailing space.
24057429Smarkm		gsub(/[[:space:]]+/, " ");
241124208Sdes		sub(/^ /, "");
24257429Smarkm		sub(/ $/, "");
24357429Smarkm
24457429Smarkm		# Pick off direction.
24557429Smarkm		if ($1 != "INOUT" && $1 != "IN" && $1 != "OUT")
24657429Smarkm			die("No IN/OUT direction for \"%s\".", $0);
24757429Smarkm		dirs[numargs] = $1;
24857429Smarkm		sub(/^[A-Z]* /, "");
24957429Smarkm
25092555Sdes		if ((reles[numargs] = $1) == "WILLRELE")
25160573Skris			sub(/^[A-Z]* /, "");
25257429Smarkm		else
25357429Smarkm			reles[numargs] = "WONTRELE";
25457429Smarkm
25557429Smarkm		# kill trailing ;
25660573Skris		if (sub(/;$/, "") < 1)
25757429Smarkm			die("Missing end-of-line ; in \"%s\".", $0);
258124208Sdes
25957429Smarkm		# pick off variable name
26057429Smarkm		if ((argp = match($0, /[A-Za-z0-9_]+$/)) < 1)
26157429Smarkm			die("Missing var name \"a_foo\" in \"%s\".", $0);
26292555Sdes		args[numargs] = substr($0, argp);
26357429Smarkm		$0 = substr($0, 1, argp - 1);
26457429Smarkm
26557429Smarkm		# what is left must be type
26657429Smarkm		# remove trailing space (if any)
26757429Smarkm		sub(/ $/, "");
26892555Sdes		types[numargs] = $0;
26992555Sdes	}
27057429Smarkm	if (numargs > 4)
27157429Smarkm		ctrargs = 4;
27257429Smarkm	else
27357429Smarkm		ctrargs = numargs;
27460573Skris	ctrstr = ctrargs "(KTR_VOP, \"VOP\", \"" uname "\", (uintptr_t)a,\n\t    "; 
27560573Skris	ctrstr = ctrstr "\"" args[0] ":0x%jX\", (uintptr_t)a->a_" args[0];
27660573Skris	for (i = 1; i < ctrargs; ++i)
27757429Smarkm		ctrstr = ctrstr ", \"" args[i] ":0x%jX\", a->a_" args[i];
27892555Sdes	ctrstr = ctrstr ");";
27957429Smarkm
280124208Sdes	if (pfile) {
281204917Sdes		printp("\t"name"_t\t*"name";")
282204917Sdes	}
283204917Sdes	if (qfile) {
284204917Sdes		printq("struct "name"_args;")
285204917Sdes		printq("typedef int "name"_t(struct "name"_args *);\n")
286204917Sdes	}
28760573Skris
28860573Skris	if (hfile) {
28992555Sdes		# Print out the vop_F_args structure.
29092555Sdes		printh("struct "name"_args {\n\tstruct vop_generic_args a_gen;");
29160573Skris		for (i = 0; i < numargs; ++i)
29260573Skris			printh("\t" t_spc(types[i]) "a_" args[i] ";");
29360573Skris		printh("};");
29460573Skris		printh("");
29592555Sdes
29692555Sdes		# Print out extern declaration.
29792555Sdes		printh("extern struct vnodeop_desc " name "_desc;");
29892555Sdes		printh("");
29992555Sdes
30060573Skris		# Print out function prototypes.
30160573Skris		printh("int " uname "_AP(struct " name "_args *);");
30260573Skris		printh("int " uname "_APV(struct vop_vector *vop, struct " name "_args *);");
30360573Skris		printh("");
30460573Skris		printh("static __inline int " uname "(");
30592555Sdes		for (i = 0; i < numargs; ++i) {
30660573Skris			printh("\t" t_spc(types[i]) args[i] \
30760573Skris			    (i < numargs - 1 ? "," : ")"));
308204917Sdes		}
309204917Sdes		printh("{");
31092555Sdes		printh("\tstruct " name "_args a;");
31160573Skris		printh("");
31260573Skris		printh("\ta.a_gen.a_desc = &" name "_desc;");
31357429Smarkm		for (i = 0; i < numargs; ++i)
314204917Sdes			printh("\ta.a_" args[i] " = " args[i] ";");
315181111Sdes		printh("\treturn (" uname "_APV("args[0]"->v_op, &a));");
316181111Sdes		printh("}");
317181111Sdes
318181111Sdes		printh("");
319181111Sdes	}
320181111Sdes
321181111Sdes	if (cfile) {
322181111Sdes		# Print out the vop_F_vp_offsets structure.  This all depends
323181111Sdes		# on naming conventions and nothing else.
324181111Sdes		printc("static int " name "_vp_offsets[] = {");
325181111Sdes		# as a side effect, figure out the releflags
32657429Smarkm		releflags = "";
32792555Sdes		vpnum = 0;
32857429Smarkm		for (i = 0; i < numargs; i++) {
329124208Sdes			if (types[i] == "struct vnode *") {
33098675Sdes				printc("\tVOPARG_OFFSETOF(struct " name \
33192555Sdes				    "_args,a_" args[i] "),");
33292555Sdes				if (reles[i] == "WILLRELE") {
33360573Skris					releflags = releflags \
33460573Skris					    "|VDESC_VP" vpnum "_WILLRELE";
33560573Skris				}
33660573Skris				vpnum++;
337124208Sdes			}
33860573Skris		}
33960573Skris
34060573Skris		sub(/^\|/, "", releflags);
34160573Skris		printc("\tVDESC_NO_OFFSET");
342181111Sdes		printc("};");
343181111Sdes
34492555Sdes		printc("\n");
34560573Skris		printc("SDT_PROBE_DEFINE2(vfs, vop, " name ", entry, \"struct vnode *\", \"struct " name "_args *\");\n");
34660573Skris		printc("SDT_PROBE_DEFINE3(vfs, vop, " name ", return, \"struct vnode *\", \"struct " name "_args *\", \"int\");\n");
34792555Sdes
34860573Skris		# Print out function.
34960573Skris		printc("\nint\n" uname "_AP(struct " name "_args *a)");
35060573Skris		printc("{");
35160573Skris		printc("");
35260573Skris		printc("\treturn(" uname "_APV(a->a_" args[0] "->v_op, a));");
35360573Skris		printc("}");
35460573Skris		printc("\nint\n" uname "_APV(struct vop_vector *vop, struct " name "_args *a)");
355124208Sdes		printc("{");
35660573Skris		printc("\tint rc;");
35760573Skris		printc("");
35860573Skris		printc("\tVNASSERT(a->a_gen.a_desc == &" name "_desc, a->a_" args[0]",");
35960573Skris		printc("\t    (\"Wrong a_desc in " name "(%p, %p)\", a->a_" args[0]", a));");
36060573Skris		printc("\twhile(vop != NULL && \\");
36198675Sdes		printc("\t    vop->"name" == NULL && vop->vop_bypass == NULL)")
36260573Skris		printc("\t\tvop = vop->vop_default;")
36360573Skris		printc("\tVNASSERT(vop != NULL, a->a_" args[0]", (\"No "name"(%p, %p)\", a->a_" args[0]", a));")
36492555Sdes		printc("\tSDT_PROBE2(vfs, vop, " name ", entry, a->a_" args[0] ", a);\n");
36560573Skris		for (i = 0; i < numargs; ++i)
36660573Skris			add_debug_code(name, args[i], "Entry", "\t");
36760573Skris		printc("\tKTR_START" ctrstr);
36860573Skris		add_pre(name);
36960573Skris		printc("\tVFS_PROLOGUE(a->a_" args[0]"->v_mount);")
37060573Skris		printc("\tif (vop->"name" != NULL)")
37160573Skris		printc("\t\trc = vop->"name"(a);")
372124208Sdes		printc("\telse")
37360573Skris		printc("\t\trc = vop->vop_bypass(&a->a_gen);")
37460573Skris		printc("\tVFS_EPILOGUE(a->a_" args[0]"->v_mount);")
37592555Sdes		printc("\tSDT_PROBE3(vfs, vop, " name ", return, a->a_" args[0] ", a, rc);\n");
37660573Skris		printc("\tif (rc == 0) {");
37760573Skris		for (i = 0; i < numargs; ++i)
37892555Sdes			add_debug_code(name, args[i], "OK", "\t\t");
37960573Skris		printc("\t} else {");
38060573Skris		for (i = 0; i < numargs; ++i)
38160573Skris			add_debug_code(name, args[i], "Error", "\t\t");
38260573Skris		printc("\t}");
38360573Skris		add_post(name);
38460573Skris		printc("\tKTR_STOP" ctrstr);
38560573Skris		printc("\treturn (rc);");
386181111Sdes		printc("}\n");
387181111Sdes
388181111Sdes		# Print out the vnodeop_desc structure.
389181111Sdes		printc("struct vnodeop_desc " name "_desc = {");
390181111Sdes		# printable name
391181111Sdes		printc("\t\"" name "\",");
392181111Sdes		# flags
393181111Sdes		vppwillrele = "";
394181111Sdes		for (i = 0; i < numargs; i++) {
395192595Sdes			if (types[i] == "struct vnode **" && \
396192595Sdes			    reles[i] == "WILLRELE") {
397181111Sdes				vppwillrele = "|VDESC_VPP_WILLRELE";
398181111Sdes			}
399181111Sdes		}
400181111Sdes
401181111Sdes		if (!releflags)
402181111Sdes			releflags = "0";
40376259Sgreen		printc("\t" releflags vppwillrele ",");
40476259Sgreen
40576259Sgreen		# function to call
40692555Sdes		printc("\t(vop_bypass_t *)" uname "_AP,");
40792555Sdes		# vp offsets
40892555Sdes		printc("\t" name "_vp_offsets,");
40992555Sdes		# vpp (if any)
41092555Sdes		printc("\t" find_arg_with_type("struct vnode **") ",");
41192555Sdes		# cred (if any)
41292555Sdes		printc("\t" find_arg_with_type("struct ucred *") ",");
41392555Sdes		# thread (if any)
41498675Sdes		printc("\t" find_arg_with_type("struct thread *") ",");
41598675Sdes		# componentname
41692555Sdes		printc("\t" find_arg_with_type("struct componentname *") ",");
41792555Sdes		# transport layer information
41892555Sdes		printc("};\n");
41992555Sdes	}
42092555Sdes}
42192555Sdes 
42292555Sdesif (pfile)
42392555Sdes	printp("};")
42492555Sdes 
42592555Sdesif (hfile)
42692555Sdes	close(hfile);
42792555Sdesif (cfile)
42892555Sdes	close(cfile);
42992555Sdesif (pfile)
43092555Sdes	close(pfile);
43192555Sdesclose(srcfile);
43292555Sdes
43392555Sdesexit 0;
43492555Sdes
43592555Sdes}
43692555Sdes