xref: /freebsd/sys/tools/vnode_if.awk (revision ab7a294721dd609f58c50a79dbe9452526899d59)
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# 4. Neither the name of the University nor the names of its contributors
16#    may be used to endorse or promote products derived from this software
17#    without specific prior written permission.
18#
19# THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
20# ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22# ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
23# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25# OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26# HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28# OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29# SUCH DAMAGE.
30#
31#	@(#)vnode_if.sh	8.1 (Berkeley) 6/10/93
32# $FreeBSD$
33#
34# Script to produce VFS front-end sugar.
35#
36# usage: vnode_if.awk <srcfile> [-c | -h]
37#	(where <srcfile> is currently /sys/kern/vnode_if.src)
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 msg "\n", what > "/dev/stderr";
49	exit 1;
50}
51
52function t_spc(type)
53{
54	# Append a space if the type is not a pointer
55	return (type ~ /\*$/) ? type : type " ";
56}
57
58# These are just for convenience ...
59function printc(s) {print s > cfile;}
60function printh(s) {print s > hfile;}
61function printp(s) {print s > pfile;}
62function printq(s) {print s > qfile;}
63
64function add_debug_code(name, arg, pos)
65{
66	if (arg == "vpp")
67		arg = "*vpp";
68	if (lockdata[name, arg, pos] && (lockdata[name, arg, pos] != "-")) {
69		if (arg ~ /^\*/) {
70			printh("\tif ("substr(arg, 2)" != NULL) {");
71		}
72		printh("\tASSERT_VI_UNLOCKED("arg", \""uname"\");");
73		# Add assertions for locking
74		if (lockdata[name, arg, pos] == "L")
75			printh("\tASSERT_VOP_LOCKED("arg", \""uname"\");");
76		else if (lockdata[name, arg, pos] == "U")
77			printh("\tASSERT_VOP_UNLOCKED("arg", \""uname"\");");
78		else if (0) {
79			# XXX More checks!
80		}
81		if (arg ~ /^\*/) {
82			printh("\t}");
83		}
84	}
85}
86
87function add_debug_pre(name)
88{
89	if (lockdata[name, "pre"]) {
90		printh("#ifdef	DEBUG_VFS_LOCKS");
91		printh("\t"lockdata[name, "pre"]"(&a);");
92		printh("#endif");
93	}
94}
95
96function add_debug_post(name)
97{
98	if (lockdata[name, "post"]) {
99		printh("#ifdef	DEBUG_VFS_LOCKS");
100		printh("\t"lockdata[name, "post"]"(&a, rc);");
101		printh("#endif");
102	}
103}
104
105function find_arg_with_type (type)
106{
107	for (jj = 0; jj < numargs; jj++) {
108		if (types[jj] == type) {
109			return "VOPARG_OFFSETOF(struct " \
110			    name "_args,a_" args[jj] ")";
111		}
112	}
113
114	return "VDESC_NO_OFFSET";
115}
116
117BEGIN{
118
119# Process the command line
120for (i = 1; i < ARGC; i++) {
121	arg = ARGV[i];
122	if (arg !~ /^-[chpq]+$/ && arg !~ /\.src$/)
123		usage();
124	if (arg ~ /^-.*c/)
125		cfile = "vnode_if.c";
126	if (arg ~ /^-.*h/)
127		hfile = "vnode_if.h";
128	if (arg ~ /^-.*p/)
129		pfile = "vnode_if_newproto.h";
130	if (arg ~ /^-.*q/)
131		qfile = "vnode_if_typedef.h";
132	if (arg ~ /\.src$/)
133		srcfile = arg;
134}
135ARGC = 1;
136
137if (!cfile && !hfile && !pfile && !qfile)
138	exit 0;
139
140if (!srcfile)
141	usage();
142
143common_head = \
144    "/*\n" \
145    " * This file is produced automatically.\n" \
146    " * Do not modify anything in here by hand.\n" \
147    " *\n" \
148    " * Created from $FreeBSD$\n" \
149    " */\n" \
150    "\n";
151
152if (pfile) {
153	printp(common_head)
154	printp("struct vop_vector {")
155	printp("\tstruct vop_vector\t*vop_default;")
156	printp("\tvop_bypass_t\t*vop_bypass;")
157}
158
159if (qfile) {
160	printq(common_head)
161	printq("struct vop_generic_args;")
162	printq("typedef int vop_bypass_t(struct vop_generic_args *);\n")
163}
164
165if (hfile) {
166	printh(common_head "extern struct vnodeop_desc vop_default_desc;");
167	printh("#include \"vnode_if_typedef.h\"")
168	printh("#include \"vnode_if_newproto.h\"")
169}
170
171if (cfile) {
172	printc(common_head \
173	    "#include <sys/param.h>\n" \
174	    "#include <sys/systm.h>\n" \
175	    "#include <sys/vnode.h>\n" \
176	    "\n" \
177	    "struct vnodeop_desc vop_default_desc = {\n" \
178	    "	1,\t\t\t/* special case, vop_default => 1 */\n" \
179	    "	\"default\",\n" \
180	    "	0,\n" \
181	    "	NULL,\n" \
182	    "	VDESC_NO_OFFSET,\n" \
183	    "	VDESC_NO_OFFSET,\n" \
184	    "	VDESC_NO_OFFSET,\n" \
185	    "	VDESC_NO_OFFSET,\n" \
186	    "	NULL,\n" \
187	    "};\n");
188}
189
190while ((getline < srcfile) > 0) {
191	if (NF == 0)
192		continue;
193	if ($1 ~ /^#%/) {
194		if (NF != 6  ||  $1 != "#%"  || \
195		    $2 !~ /^[a-z]+$/  ||  $3 !~ /^[a-z]+$/  || \
196		    $4 !~ /^.$/  ||  $5 !~ /^.$/  ||  $6 !~ /^.$/)
197			continue;
198		if ($3 == "vpp")
199			$3 = "*vpp";
200		lockdata["vop_" $2, $3, "Entry"] = $4;
201		lockdata["vop_" $2, $3, "OK"]    = $5;
202		lockdata["vop_" $2, $3, "Error"] = $6;
203		continue;
204	}
205
206	if ($1 ~ /^#!/) {
207		if (NF != 4 || $1 != "#!")
208			continue;
209		if ($3 != "pre" && $3 != "post")
210			continue;
211		lockdata["vop_" $2, $3] = $4;
212		continue;
213	}
214	if ($1 ~ /^#/)
215		continue;
216
217	# Get the function name.
218	name = $1;
219	uname = toupper(name);
220
221	# Start constructing a ktrpoint string
222	ctrstr = "\"" uname;
223	# Get the function arguments.
224	for (numargs = 0; ; ++numargs) {
225		if ((getline < srcfile) <= 0) {
226			die("Unable to read through the arguments for \"%s\"",
227			    name);
228		}
229		if ($1 ~ /^\};/)
230			break;
231
232		# Delete comments, if any.
233		gsub (/\/\*.*\*\//, "");
234
235		# Condense whitespace and delete leading/trailing space.
236		gsub(/[[:space:]]+/, " ");
237		sub(/^ /, "");
238		sub(/ $/, "");
239
240		# Pick off direction.
241		if ($1 != "INOUT" && $1 != "IN" && $1 != "OUT")
242			die("No IN/OUT direction for \"%s\".", $0);
243		dirs[numargs] = $1;
244		sub(/^[A-Z]* /, "");
245
246		if ((reles[numargs] = $1) == "WILLRELE")
247			sub(/^[A-Z]* /, "");
248		else
249			reles[numargs] = "WONTRELE";
250
251		# kill trailing ;
252		if (sub(/;$/, "") < 1)
253			die("Missing end-of-line ; in \"%s\".", $0);
254
255		# pick off variable name
256		if ((argp = match($0, /[A-Za-z0-9_]+$/)) < 1)
257			die("Missing var name \"a_foo\" in \"%s\".", $0);
258		args[numargs] = substr($0, argp);
259		$0 = substr($0, 1, argp - 1);
260
261		# what is left must be type
262		# remove trailing space (if any)
263		sub(/ $/, "");
264		types[numargs] = $0;
265
266		# We can do a maximum of 6 arguments to CTR*
267		if (numargs <= 6) {
268			if (numargs == 0)
269				ctrstr = ctrstr "(" args[numargs];
270			else
271				ctrstr = ctrstr ", " args[numargs];
272			if (types[numargs] ~ /\*/)
273				ctrstr = ctrstr " 0x%lX";
274			else
275				ctrstr = ctrstr " %ld";
276		}
277	}
278	if (numargs > 6)
279		ctrargs = 6;
280	else
281		ctrargs = numargs;
282	ctrstr = "\tCTR" ctrargs "(KTR_VOP, " ctrstr ")\"";
283	for (i = 0; i < ctrargs; ++i)
284		ctrstr = ctrstr ", " args[i];
285	ctrstr = ctrstr ");";
286
287	if (pfile) {
288		printp("\t"name"_t\t*"name";")
289	}
290	if (qfile) {
291		printq("struct "name"_args;")
292		printq("typedef int "name"_t(struct "name"_args *);\n")
293	}
294
295	if (hfile) {
296		# Print out the vop_F_args structure.
297		printh("struct "name"_args {\n\tstruct vop_generic_args a_gen;");
298		for (i = 0; i < numargs; ++i)
299			printh("\t" t_spc(types[i]) "a_" args[i] ";");
300		printh("};");
301
302		# Print out extern declaration.
303		printh("extern struct vnodeop_desc " name "_desc;");
304
305		# Print out function.
306		printh("static __inline int " uname "(");
307		for (i = 0; i < numargs; ++i) {
308			printh("\t" t_spc(types[i]) args[i] \
309			    (i < numargs - 1 ? "," : ")"));
310		}
311		printh("{\n\tstruct " name "_args a;");
312		printh("\tint rc;");
313		printh("\ta.a_gen.a_desc = VDESC(" name ");");
314		for (i = 0; i < numargs; ++i)
315			printh("\ta.a_" args[i] " = " args[i] ";");
316		for (i = 0; i < numargs; ++i)
317			add_debug_code(name, args[i], "Entry");
318		add_debug_pre(name);
319		printh("\t{")
320		printh("\t\tstruct vop_vector *vop = "args[0]"->v_op;")
321		printh("\t\twhile(vop != NULL && vop->"name" == NULL && vop->vop_bypass == NULL)")
322		printh("\t\t\tvop = vop->vop_default;")
323		printh("\t\tKASSERT(vop != NULL, (\"No "name"(%p...)\", "args[0]"));")
324		printh("\t\tif (vop->"name" != NULL)")
325		printh("\t\t\trc = vop->"name"(&a);")
326		printh("\t\telse")
327		printh("\t\t\trc = vop->vop_bypass(&a.a_gen);")
328		printh("\t}")
329		printh(ctrstr);
330		printh("if (rc == 0) {");
331		for (i = 0; i < numargs; ++i)
332			add_debug_code(name, args[i], "OK");
333		printh("} else {");
334		for (i = 0; i < numargs; ++i)
335			add_debug_code(name, args[i], "Error");
336		printh("}");
337		add_debug_post(name);
338		printh("\treturn (rc);\n}");
339	}
340
341	if (cfile) {
342		# Print out the vop_F_vp_offsets structure.  This all depends
343		# on naming conventions and nothing else.
344		printc("static int " name "_vp_offsets[] = {");
345		# as a side effect, figure out the releflags
346		releflags = "";
347		vpnum = 0;
348		for (i = 0; i < numargs; i++) {
349			if (types[i] == "struct vnode *") {
350				printc("\tVOPARG_OFFSETOF(struct " name \
351				    "_args,a_" args[i] "),");
352				if (reles[i] == "WILLRELE") {
353					releflags = releflags \
354					    "|VDESC_VP" vpnum "_WILLRELE";
355				}
356				vpnum++;
357			}
358		}
359
360		sub(/^\|/, "", releflags);
361		printc("\tVDESC_NO_OFFSET");
362		printc("};");
363
364		# Print out the vnodeop_desc structure.
365		printc("struct vnodeop_desc " name "_desc = {");
366		# offset
367		printc("\toffsetof(struct vop_vector, "name"),");
368		# printable name
369		printc("\t\"" name "\",");
370		# flags
371		vppwillrele = "";
372		for (i = 0; i < numargs; i++) {
373			if (types[i] == "struct vnode **" && \
374			    reles[i] == "WILLRELE") {
375				vppwillrele = "|VDESC_VPP_WILLRELE";
376			}
377		}
378
379		if (!releflags)
380			releflags = "0";
381		printc("\t" releflags vppwillrele ",");
382
383		# vp offsets
384		printc("\t" name "_vp_offsets,");
385		# vpp (if any)
386		printc("\t" find_arg_with_type("struct vnode **") ",");
387		# cred (if any)
388		printc("\t" find_arg_with_type("struct ucred *") ",");
389		# thread (if any)
390		printc("\t" find_arg_with_type("struct thread *") ",");
391		# componentname
392		printc("\t" find_arg_with_type("struct componentname *") ",");
393		# transport layer information
394		printc("\tNULL,\n};\n");
395	}
396}
397
398if (pfile)
399	printp("};")
400
401if (hfile)
402	close(hfile);
403if (cfile)
404	close(cfile);
405if (pfile)
406	close(pfile);
407close(srcfile);
408
409exit 0;
410
411}
412