vnode_if.awk revision 99516
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: head/sys/tools/vnode_if.awk 99516 2002-07-07 06:39:36Z jeff $
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)
67{
68	if (lockdata[name, arg, "Entry"]) {
69		# Add assertions for locking
70		if (lockdata[name, arg, "Entry"] == "L")
71			printh("\tASSERT_VOP_LOCKED("arg", \""uname"\");");
72		else if (lockdata[name, arg, "Entry"] == "U")
73			printh("\tASSERT_VOP_UNLOCKED("arg", \""uname"\");");
74		else if (0) {
75			# XXX More checks!
76		}
77	}
78}
79
80function add_debug_pre(name)
81{
82	if (lockdata[name, "pre"]) {
83		printh("#ifdef	DEBUG_VFS_LOCKS");
84		printh("\t"lockdata[name, "pre"]"(&a);");
85		printh("#endif");
86	}
87}
88
89function add_debug_post(name)
90{
91	if (lockdata[name, "post"]) {
92		printh("#ifdef	DEBUG_VFS_LOCKS");
93		printh("\t"lockdata[name, "post"]"(&a, rc);");
94		printh("#endif");
95	}
96}
97
98function find_arg_with_type (type)
99{
100	for (jj = 0; jj < numargs; jj++) {
101		if (types[jj] == type) {
102			return "VOPARG_OFFSETOF(struct " \
103			    name "_args,a_" args[jj] ")";
104		}
105	}
106
107	return "VDESC_NO_OFFSET";
108}
109
110BEGIN{
111
112# Process the command line
113for (i = 1; i < ARGC; i++) {
114	arg = ARGV[i];
115	if (arg !~ /^-[ch]+$/ && arg !~ /\.src$/)
116		usage();
117	if (arg ~ /^-.*c/)
118		cfile = "vnode_if.c";
119	if (arg ~ /^-.*h/)
120		hfile = "vnode_if.h";
121	if (arg ~ /\.src$/)
122		srcfile = arg;
123}
124ARGC = 1;
125
126if (!cfile && !hfile)
127	exit 0;
128
129if (!srcfile)
130	usage();
131
132common_head = \
133    "/*\n" \
134    " * This file is produced automatically.\n" \
135    " * Do not modify anything in here by hand.\n" \
136    " *\n" \
137    " * Created from $FreeBSD: head/sys/tools/vnode_if.awk 99516 2002-07-07 06:39:36Z jeff $\n" \
138    " */\n" \
139    "\n";
140
141if (hfile)
142	printh(common_head "extern struct vnodeop_desc vop_default_desc;");
143
144if (cfile) {
145	printc(common_head \
146	    "#include <sys/param.h>\n" \
147	    "#include <sys/systm.h>\n" \
148	    "#include <sys/vnode.h>\n" \
149	    "\n" \
150	    "struct vnodeop_desc vop_default_desc = {\n" \
151	    "	1,\t\t\t/* special case, vop_default => 1 */\n" \
152	    "	\"default\",\n" \
153	    "	0,\n" \
154	    "	NULL,\n" \
155	    "	VDESC_NO_OFFSET,\n" \
156	    "	VDESC_NO_OFFSET,\n" \
157	    "	VDESC_NO_OFFSET,\n" \
158	    "	VDESC_NO_OFFSET,\n" \
159	    "	NULL,\n" \
160	    "};\n");
161}
162
163while ((getline < srcfile) > 0) {
164	if (NF == 0)
165		continue;
166	if ($1 ~ /^#%/) {
167		if (NF != 6  ||  $1 != "#%"  || \
168		    $2 !~ /^[a-z]+$/  ||  $3 !~ /^[a-z]+$/  || \
169		    $4 !~ /^.$/  ||  $5 !~ /^.$/  ||  $6 !~ /^.$/)
170			continue;
171		lockdata["vop_" $2, $3, "Entry"] = $4;
172		lockdata["vop_" $2, $3, "OK"]    = $5;
173		lockdata["vop_" $2, $3, "Error"] = $6;			
174		continue;
175	}
176
177	if ($1 ~ /^#!/) {
178		if (NF != 4 || $1 != "#!")
179			continue;
180		if ($3 != "pre" && $3 != "post")
181			continue;
182		lockdata["vop_" $2, $3] = $4;
183		continue;
184	}
185	if ($1 ~ /^#/)
186		continue;
187
188	# Get the function name.
189	name = $1;
190	uname = toupper(name);
191	# Get the function arguments.
192	for (numargs = 0; ; ++numargs) {
193		if ((getline < srcfile) <= 0) {
194			die("Unable to read through the arguments for \"%s\"",
195			    name);
196		}
197		if ($1 ~ /^\};/)
198			break;
199
200		# Delete comments, if any.
201		gsub (/\/\*.*\*\//, "");
202
203		# Condense whitespace and delete leading/trailing space.
204		gsub(/[[:space:]]+/, " ");
205		sub(/^ /, "");
206		sub(/ $/, "");
207
208		# Pick off direction.
209		if ($1 != "INOUT" && $1 != "IN" && $1 != "OUT")
210			die("No IN/OUT direction for \"%s\".", $0);
211		dirs[numargs] = $1;
212		sub(/^[A-Z]* /, "");
213
214		if ((reles[numargs] = $1) == "WILLRELE")
215			sub(/^[A-Z]* /, "");
216		else
217			reles[numargs] = "WONTRELE";
218
219		# kill trailing ;
220		if (sub(/;$/, "") < 1)
221			die("Missing end-of-line ; in \"%s\".", $0);
222
223		# pick off variable name
224		if ((argp = match($0, /[A-Za-z0-9_]+$/)) < 1)
225			die("Missing var name \"a_foo\" in \"%s\".", $0);
226		args[numargs] = substr($0, argp);
227		$0 = substr($0, 1, argp - 1);
228
229		# what is left must be type
230		# remove trailing space (if any)
231		sub(/ $/, "");
232		types[numargs] = $0;
233	}
234
235	if (hfile) {
236		# Print out the vop_F_args structure.
237		printh("struct "name"_args {\n\tstruct vnodeop_desc *a_desc;");
238		for (i = 0; i < numargs; ++i)
239			printh("\t" t_spc(types[i]) "a_" args[i] ";");
240		printh("};");
241
242		# Print out extern declaration.
243		printh("extern struct vnodeop_desc " name "_desc;");
244
245		# Print out function.
246		printh("static __inline int " uname "(");
247		for (i = 0; i < numargs; ++i) {
248			printh("\t" t_spc(types[i]) args[i] \
249			    (i < numargs - 1 ? "," : ")"));
250		}
251		printh("{\n\tstruct " name "_args a;");
252		printh("\tint rc;");
253		printh("\ta.a_desc = VDESC(" name ");");
254		for (i = 0; i < numargs; ++i)
255			printh("\ta.a_" args[i] " = " args[i] ";");
256		for (i = 0; i < numargs; ++i)
257			add_debug_code(name, args[i]);
258		add_debug_pre(name);
259		printh("\trc = VCALL(" args[0] ", VOFFSET(" name "), &a);");
260		add_debug_post(name);
261		printh("\treturn (rc);\n}");
262	}
263
264	if (cfile) {
265		# Print out the vop_F_vp_offsets structure.  This all depends
266		# on naming conventions and nothing else.
267		printc("static int " name "_vp_offsets[] = {");
268		# as a side effect, figure out the releflags
269		releflags = "";
270		vpnum = 0;
271		for (i = 0; i < numargs; i++) {
272			if (types[i] == "struct vnode *") {
273				printc("\tVOPARG_OFFSETOF(struct " name \
274				    "_args,a_" args[i] "),");
275				if (reles[i] == "WILLRELE") {
276					releflags = releflags \
277					    "|VDESC_VP" vpnum "_WILLRELE";
278				}
279				vpnum++;
280			}
281		}
282
283		sub(/^\|/, "", releflags);
284		printc("\tVDESC_NO_OFFSET");
285		printc("};");
286
287		# Print out the vnodeop_desc structure.
288		printc("struct vnodeop_desc " name "_desc = {");
289		# offset
290		printc("\t0,");
291		# printable name
292		printc("\t\"" name "\",");
293		# flags
294		vppwillrele = "";
295		for (i = 0; i < numargs; i++) {
296			if (types[i] == "struct vnode **" && \
297			    reles[i] == "WILLRELE") {
298				vppwillrele = "|VDESC_VPP_WILLRELE";
299			}
300		}
301
302		if (!releflags)
303			releflags = "0";
304		printc("\t" releflags vppwillrele ",");
305
306		# vp offsets
307		printc("\t" name "_vp_offsets,");
308		# vpp (if any)
309		printc("\t" find_arg_with_type("struct vnode **") ",");
310		# cred (if any)
311		printc("\t" find_arg_with_type("struct ucred *") ",");
312		# thread (if any)
313		printc("\t" find_arg_with_type("struct thread *") ",");
314		# componentname
315		printc("\t" find_arg_with_type("struct componentname *") ",");
316		# transport layer information
317		printc("\tNULL,\n};\n");
318	}
319}
320 
321if (hfile)
322	close(hfile);
323if (cfile)
324	close(cfile);
325close(srcfile);
326
327exit 0;
328
329}
330