xref: /freebsd/sys/tools/vnode_if.awk (revision 1e66f787c838b5af7de716e266caf4e5d190d54b)
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#
34#	@(#)vnode_if.sh	8.1 (Berkeley) 6/10/93
35# $FreeBSD$
36#
37# Script to produce VFS front-end sugar.
38#
39# usage: vnode_if.awk <srcfile> [-c | -h | -p | -q]
40#	(where <srcfile> is currently /sys/kern/vnode_if.src)
41#	The source file must have a .src extension
42#
43
44function usage()
45{
46	print "usage: vnode_if.awk <srcfile> [-c|-h|-p|-q]";
47	exit 1;
48}
49
50function die(msg, what)
51{
52	printf srcfile "(" fnr "): " > "/dev/stderr";
53	printf msg "\n", what > "/dev/stderr";
54	exit 1;
55}
56
57function t_spc(type)
58{
59	# Append a space if the type is not a pointer
60	return (type ~ /\*$/) ? type : type " ";
61}
62
63# These are just for convenience ...
64function printc(s) {print s > cfile;}
65function printh(s) {print s > hfile;}
66function printp(s) {print s > pfile;}
67function printq(s) {print s > qfile;}
68
69function add_debug_code(name, arg, pos, ind)
70{
71	if (arg == "vpp")
72		star = "*";
73	else
74		star = "";
75	if (lockdata[name, arg, pos] && (lockdata[name, arg, pos] != "-")) {
76		printc(ind"ASSERT_VI_UNLOCKED("star"a->a_"arg", \""uname"\");");
77		# Add assertions for locking
78		if (lockdata[name, arg, pos] == "L")
79			printc(ind"ASSERT_VOP_LOCKED(" star "a->a_"arg", \""uname"\");");
80		else if (lockdata[name, arg, pos] == "U")
81			printc(ind"ASSERT_VOP_UNLOCKED(" star "a->a_"arg", \""uname"\");");
82		else if (lockdata[name, arg, pos] == "E")
83			printc(ind"ASSERT_VOP_ELOCKED(" star "a->a_"arg", \""uname"\");");
84		else if (0) {
85			# XXX More checks!
86		}
87	}
88}
89
90function add_pre(name)
91{
92	if (lockdata[name, "pre"]) {
93		printc("\t"lockdata[name, "pre"]"(a);");
94	}
95}
96
97function add_post(name)
98{
99	if (lockdata[name, "post"]) {
100		printc("\t"lockdata[name, "post"]"(a, rc);");
101	}
102}
103
104function find_arg_with_type (type)
105{
106	for (jj = 0; jj < numargs; jj++) {
107		if (types[jj] == type) {
108			return "VOPARG_OFFSETOF(struct " \
109			    name "_args,a_" args[jj] ")";
110		}
111	}
112
113	return "VDESC_NO_OFFSET";
114}
115
116BEGIN{
117
118# Process the command line
119for (i = 1; i < ARGC; i++) {
120	arg = ARGV[i];
121	if (arg !~ /^-[chpq]+$/ && arg !~ /\.src$/)
122		usage();
123	if (arg ~ /^-.*c/)
124		cfile = "vnode_if.c";
125	if (arg ~ /^-.*h/)
126		hfile = "vnode_if.h";
127	if (arg ~ /^-.*p/)
128		pfile = "vnode_if_newproto.h";
129	if (arg ~ /^-.*q/)
130		qfile = "vnode_if_typedef.h";
131	if (arg ~ /\.src$/)
132		srcfile = arg;
133}
134ARGC = 1;
135
136if (!cfile && !hfile && !pfile && !qfile)
137	exit 0;
138
139if (!srcfile)
140	usage();
141
142common_head = \
143    "/*\n" \
144    " * This file is produced automatically.\n" \
145    " * Do not modify anything in here by hand.\n" \
146    " *\n" \
147    " * Created from $FreeBSD$\n" \
148    " */\n" \
149    "\n";
150
151if (pfile) {
152	printp(common_head)
153	printp("struct vop_vector {")
154	printp("\tstruct vop_vector\t*vop_default;")
155	printp("\tvop_bypass_t\t*vop_bypass;")
156}
157
158if (qfile) {
159	printq(common_head)
160}
161
162if (hfile) {
163	printh(common_head "extern struct vnodeop_desc vop_default_desc;");
164	printh("#include \"vnode_if_typedef.h\"")
165	printh("#include \"vnode_if_newproto.h\"")
166}
167
168if (cfile) {
169	printc(common_head \
170	    "#include <sys/param.h>\n" \
171	    "#include <sys/event.h>\n" \
172	    "#include <sys/kernel.h>\n" \
173	    "#include <sys/mount.h>\n" \
174	    "#include <sys/sdt.h>\n" \
175	    "#include <sys/signalvar.h>\n" \
176	    "#include <sys/systm.h>\n" \
177	    "#include <sys/vnode.h>\n" \
178	    "\n" \
179	    "SDT_PROVIDER_DECLARE(vfs);\n" \
180	    "\n" \
181	    "struct vnodeop_desc vop_default_desc = {\n" \
182	    "	\"default\",\n" \
183	    "	0,\n" \
184	    "	(vop_bypass_t *)vop_panic,\n" \
185	    "	NULL,\n" \
186	    "	VDESC_NO_OFFSET,\n" \
187	    "	VDESC_NO_OFFSET,\n" \
188	    "	VDESC_NO_OFFSET,\n" \
189	    "	VDESC_NO_OFFSET,\n" \
190	    "};\n");
191}
192
193while ((getline < srcfile) > 0) {
194	fnr++;
195	if (NF == 0)
196		continue;
197	if ($1 ~ /^%%/) {
198		if (NF != 6 ||
199		    $2 !~ /^[a-z_]+$/  ||  $3 !~ /^[a-z]+$/  ||
200		    $4 !~ /^.$/  ||  $5 !~ /^.$/  ||  $6 !~ /^.$/) {
201			die("Invalid %s construction", "%%");
202			continue;
203		}
204		lockdata["vop_" $2, $3, "Entry"] = $4;
205		lockdata["vop_" $2, $3, "OK"]    = $5;
206		lockdata["vop_" $2, $3, "Error"] = $6;
207		continue;
208	}
209
210	if ($1 ~ /^%!/) {
211		if (NF != 4 ||
212		    ($3 != "pre" && $3 != "post")) {
213			die("Invalid %s construction", "%!");
214			continue;
215		}
216		lockdata["vop_" $2, $3] = $4;
217		continue;
218	}
219	if ($1 ~ /^#/)
220		continue;
221
222	# Get the function name.
223	name = $1;
224	uname = toupper(name);
225
226	# Get the function arguments.
227	for (numargs = 0; ; ++numargs) {
228		if ((getline < srcfile) <= 0) {
229			die("Unable to read through the arguments for \"%s\"",
230			    name);
231		}
232		fnr++;
233		if ($1 ~ /^\};/)
234			break;
235
236		# Delete comments, if any.
237		gsub (/\/\*.*\*\//, "");
238
239		# Condense whitespace and delete leading/trailing space.
240		gsub(/[[:space:]]+/, " ");
241		sub(/^ /, "");
242		sub(/ $/, "");
243
244		# Pick off direction.
245		if ($1 != "INOUT" && $1 != "IN" && $1 != "OUT")
246			die("No IN/OUT direction for \"%s\".", $0);
247		dirs[numargs] = $1;
248		sub(/^[A-Z]* /, "");
249
250		if ((reles[numargs] = $1) == "WILLRELE")
251			sub(/^[A-Z]* /, "");
252		else
253			reles[numargs] = "WONTRELE";
254
255		# kill trailing ;
256		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_PROBE2(vfs, vop, " name ", entry, a->a_" args[0] ", a);\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_PROBE3(vfs, vop, " name ", return, a->a_" args[0] ", a, rc);\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