xref: /freebsd-14.2/sys/tools/vnode_if.awk (revision 1fd2cdcb)
1#!/usr/bin/awk -f
2
3#
4# Copyright (c) 1992, 1993
5#	The Regents of the University of California.  All rights reserved.
6#
7# Redistribution and use in source and binary forms, with or without
8# modification, are permitted provided that the following conditions
9# are met:
10# 1. Redistributions of source code must retain the above copyright
11#    notice, this list of conditions and the following disclaimer.
12# 2. Redistributions in binary form must reproduce the above copyright
13#    notice, this list of conditions and the following disclaimer in the
14#    documentation and/or other materials provided with the distribution.
15# 3. All advertising materials mentioning features or use of this software
16#    must display the following acknowledgement:
17#	This product includes software developed by the University of
18#	California, Berkeley and its contributors.
19# 4. Neither the name of the University nor the names of its contributors
20#    may be used to endorse or promote products derived from this software
21#    without specific prior written permission.
22#
23# THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
24# ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26# ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
27# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29# OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30# HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32# OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33# SUCH DAMAGE.
34#
35#	@(#)vnode_if.sh	8.1 (Berkeley) 6/10/93
36# $FreeBSD$
37#
38# Script to produce VFS front-end sugar.
39#
40# usage: vnode_if.awk <srcfile> [-c | -h]
41#	(where <srcfile> is currently /sys/kern/vnode_if.src)
42#
43
44function usage()
45{
46	print "usage: vnode_if.awk <srcfile> [-c|-h]";
47	exit 1;
48}
49
50function die(msg, what)
51{
52	printf msg "\n", what > "/dev/stderr";
53	exit 1;
54}
55
56function t_spc(type)
57{
58	# Append a space if the type is not a pointer
59	return (type ~ /\*$/) ? type : type " ";
60}
61
62# These are just for convenience ...
63function printc(s) {print s > cfile;}
64function printh(s) {print s > hfile;}
65
66function add_debug_code(name, arg, pos)
67{
68	if (lockdata[name, arg, pos]) {
69		printh("\tASSERT_VI_UNLOCKED("arg");");
70		# Add assertions for locking
71		if (lockdata[name, arg, pos] == "L")
72			printh("\tASSERT_VOP_LOCKED("arg", \""uname"\");");
73		else if (lockdata[name, arg, pos] == "U")
74			printh("\tASSERT_VOP_UNLOCKED("arg", \""uname"\");");
75		else if (0) {
76			# XXX More checks!
77		}
78	}
79}
80
81function add_debug_pre(name)
82{
83	if (lockdata[name, "pre"]) {
84		printh("#ifdef	DEBUG_VFS_LOCKS");
85		printh("\t"lockdata[name, "pre"]"(&a);");
86		printh("#endif");
87	}
88}
89
90function add_debug_post(name)
91{
92	if (lockdata[name, "post"]) {
93		printh("#ifdef	DEBUG_VFS_LOCKS");
94		printh("\t"lockdata[name, "post"]"(&a, rc);");
95		printh("#endif");
96	}
97}
98
99function find_arg_with_type (type)
100{
101	for (jj = 0; jj < numargs; jj++) {
102		if (types[jj] == type) {
103			return "VOPARG_OFFSETOF(struct " \
104			    name "_args,a_" args[jj] ")";
105		}
106	}
107
108	return "VDESC_NO_OFFSET";
109}
110
111BEGIN{
112
113# Process the command line
114for (i = 1; i < ARGC; i++) {
115	arg = ARGV[i];
116	if (arg !~ /^-[ch]+$/ && arg !~ /\.src$/)
117		usage();
118	if (arg ~ /^-.*c/)
119		cfile = "vnode_if.c";
120	if (arg ~ /^-.*h/)
121		hfile = "vnode_if.h";
122	if (arg ~ /\.src$/)
123		srcfile = arg;
124}
125ARGC = 1;
126
127if (!cfile && !hfile)
128	exit 0;
129
130if (!srcfile)
131	usage();
132
133common_head = \
134    "/*\n" \
135    " * This file is produced automatically.\n" \
136    " * Do not modify anything in here by hand.\n" \
137    " *\n" \
138    " * Created from $FreeBSD$\n" \
139    " */\n" \
140    "\n";
141
142if (hfile)
143	printh(common_head "extern struct vnodeop_desc vop_default_desc;");
144
145if (cfile) {
146	printc(common_head \
147	    "#include <sys/param.h>\n" \
148	    "#include <sys/systm.h>\n" \
149	    "#include <sys/vnode.h>\n" \
150	    "\n" \
151	    "struct vnodeop_desc vop_default_desc = {\n" \
152	    "	1,\t\t\t/* special case, vop_default => 1 */\n" \
153	    "	\"default\",\n" \
154	    "	0,\n" \
155	    "	NULL,\n" \
156	    "	VDESC_NO_OFFSET,\n" \
157	    "	VDESC_NO_OFFSET,\n" \
158	    "	VDESC_NO_OFFSET,\n" \
159	    "	VDESC_NO_OFFSET,\n" \
160	    "	NULL,\n" \
161	    "};\n");
162}
163
164while ((getline < srcfile) > 0) {
165	if (NF == 0)
166		continue;
167	if ($1 ~ /^#%/) {
168		if (NF != 6  ||  $1 != "#%"  || \
169		    $2 !~ /^[a-z]+$/  ||  $3 !~ /^[a-z]+$/  || \
170		    $4 !~ /^.$/  ||  $5 !~ /^.$/  ||  $6 !~ /^.$/)
171			continue;
172		if ($3 == "vpp")
173			$3 = "*vpp";
174		lockdata["vop_" $2, $3, "Entry"] = $4;
175		lockdata["vop_" $2, $3, "OK"]    = $5;
176		lockdata["vop_" $2, $3, "Error"] = $6;
177		continue;
178	}
179
180	if ($1 ~ /^#!/) {
181		if (NF != 4 || $1 != "#!")
182			continue;
183		if ($3 != "pre" && $3 != "post")
184			continue;
185		lockdata["vop_" $2, $3] = $4;
186		continue;
187	}
188	if ($1 ~ /^#/)
189		continue;
190
191	# Get the function name.
192	name = $1;
193	uname = toupper(name);
194	# Get the function arguments.
195	for (numargs = 0; ; ++numargs) {
196		if ((getline < srcfile) <= 0) {
197			die("Unable to read through the arguments for \"%s\"",
198			    name);
199		}
200		if ($1 ~ /^\};/)
201			break;
202
203		# Delete comments, if any.
204		gsub (/\/\*.*\*\//, "");
205
206		# Condense whitespace and delete leading/trailing space.
207		gsub(/[[:space:]]+/, " ");
208		sub(/^ /, "");
209		sub(/ $/, "");
210
211		# Pick off direction.
212		if ($1 != "INOUT" && $1 != "IN" && $1 != "OUT")
213			die("No IN/OUT direction for \"%s\".", $0);
214		dirs[numargs] = $1;
215		sub(/^[A-Z]* /, "");
216
217		if ((reles[numargs] = $1) == "WILLRELE")
218			sub(/^[A-Z]* /, "");
219		else
220			reles[numargs] = "WONTRELE";
221
222		# kill trailing ;
223		if (sub(/;$/, "") < 1)
224			die("Missing end-of-line ; in \"%s\".", $0);
225
226		# pick off variable name
227		if ((argp = match($0, /[A-Za-z0-9_]+$/)) < 1)
228			die("Missing var name \"a_foo\" in \"%s\".", $0);
229		args[numargs] = substr($0, argp);
230		$0 = substr($0, 1, argp - 1);
231
232		# what is left must be type
233		# remove trailing space (if any)
234		sub(/ $/, "");
235		types[numargs] = $0;
236	}
237
238	if (hfile) {
239		# Print out the vop_F_args structure.
240		printh("struct "name"_args {\n\tstruct vnodeop_desc *a_desc;");
241		for (i = 0; i < numargs; ++i)
242			printh("\t" t_spc(types[i]) "a_" args[i] ";");
243		printh("};");
244
245		# Print out extern declaration.
246		printh("extern struct vnodeop_desc " name "_desc;");
247
248		# Print out function.
249		printh("static __inline int " uname "(");
250		for (i = 0; i < numargs; ++i) {
251			printh("\t" t_spc(types[i]) args[i] \
252			    (i < numargs - 1 ? "," : ")"));
253		}
254		printh("{\n\tstruct " name "_args a;");
255		printh("\tint rc;");
256		printh("\ta.a_desc = VDESC(" name ");");
257		for (i = 0; i < numargs; ++i)
258			printh("\ta.a_" args[i] " = " args[i] ";");
259		for (i = 0; i < numargs; ++i)
260			add_debug_code(name, args[i], "Entry");
261		add_debug_pre(name);
262		printh("\trc = VCALL(" args[0] ", VOFFSET(" name "), &a);");
263		printh("if (rc == 0) {");
264		for (i = 0; i < numargs; ++i)
265			add_debug_code(name, args[i], "OK");
266		printh("} else {");
267		for (i = 0; i < numargs; ++i)
268			add_debug_code(name, args[i], "Error");
269		printh("}");
270		add_debug_post(name);
271		printh("\treturn (rc);\n}");
272	}
273
274	if (cfile) {
275		# Print out the vop_F_vp_offsets structure.  This all depends
276		# on naming conventions and nothing else.
277		printc("static int " name "_vp_offsets[] = {");
278		# as a side effect, figure out the releflags
279		releflags = "";
280		vpnum = 0;
281		for (i = 0; i < numargs; i++) {
282			if (types[i] == "struct vnode *") {
283				printc("\tVOPARG_OFFSETOF(struct " name \
284				    "_args,a_" args[i] "),");
285				if (reles[i] == "WILLRELE") {
286					releflags = releflags \
287					    "|VDESC_VP" vpnum "_WILLRELE";
288				}
289				vpnum++;
290			}
291		}
292
293		sub(/^\|/, "", releflags);
294		printc("\tVDESC_NO_OFFSET");
295		printc("};");
296
297		# Print out the vnodeop_desc structure.
298		printc("struct vnodeop_desc " name "_desc = {");
299		# offset
300		printc("\t0,");
301		# printable name
302		printc("\t\"" name "\",");
303		# flags
304		vppwillrele = "";
305		for (i = 0; i < numargs; i++) {
306			if (types[i] == "struct vnode **" && \
307			    reles[i] == "WILLRELE") {
308				vppwillrele = "|VDESC_VPP_WILLRELE";
309			}
310		}
311
312		if (!releflags)
313			releflags = "0";
314		printc("\t" releflags vppwillrele ",");
315
316		# vp offsets
317		printc("\t" name "_vp_offsets,");
318		# vpp (if any)
319		printc("\t" find_arg_with_type("struct vnode **") ",");
320		# cred (if any)
321		printc("\t" find_arg_with_type("struct ucred *") ",");
322		# thread (if any)
323		printc("\t" find_arg_with_type("struct thread *") ",");
324		# componentname
325		printc("\t" find_arg_with_type("struct componentname *") ",");
326		# transport layer information
327		printc("\tNULL,\n};\n");
328	}
329}
330
331if (hfile)
332	close(hfile);
333if (cfile)
334	close(cfile);
335close(srcfile);
336
337exit 0;
338
339}
340