xref: /freebsd/sys/tools/vnode_if.awk (revision 4f29da19bd44f0e99f021510460a81bf754c21d2)
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#
32#	@(#)vnode_if.sh	8.1 (Berkeley) 6/10/93
33# $FreeBSD$
34#
35# Script to produce VFS front-end sugar.
36#
37# usage: vnode_if.awk <srcfile> [-c | -h]
38#	(where <srcfile> is currently /sys/kern/vnode_if.src)
39#
40
41function usage()
42{
43	print "usage: vnode_if.awk <srcfile> [-c|-h|-p|-q]";
44	exit 1;
45}
46
47function die(msg, what)
48{
49	printf msg "\n", what > "/dev/stderr";
50	exit 1;
51}
52
53function t_spc(type)
54{
55	# Append a space if the type is not a pointer
56	return (type ~ /\*$/) ? type : type " ";
57}
58
59# These are just for convenience ...
60function printc(s) {print s > cfile;}
61function printh(s) {print s > hfile;}
62function printp(s) {print s > pfile;}
63function printq(s) {print s > qfile;}
64
65function add_debug_code(name, arg, pos, ind)
66{
67	if (arg == "vpp")
68		star = "*";
69	else
70		star = "";
71	if (lockdata[name, arg, pos] && (lockdata[name, arg, pos] != "-")) {
72		if (arg ~ /^\*/) {
73			printc(ind"if ("substr(arg, 2)" != NULL) {");
74		}
75		printc(ind"ASSERT_VI_UNLOCKED("star"a->a_"arg", \""uname"\");");
76		# Add assertions for locking
77		if (lockdata[name, arg, pos] == "L")
78			printc(ind"ASSERT_VOP_LOCKED(" star "a->a_"arg", \""uname"\");");
79		else if (lockdata[name, arg, pos] == "U")
80			printc(ind"ASSERT_VOP_UNLOCKED(" star "a->a_"arg", \""uname"\");");
81		else if (lockdata[name, arg, pos] == "E")
82			printc(ind"ASSERT_VOP_ELOCKED(" star "a->a_"arg", \""uname"\");");
83		else if (0) {
84			# XXX More checks!
85		}
86		if (arg ~ /^\*/) {
87			printc("ind}");
88		}
89	}
90}
91
92function add_pre(name)
93{
94	if (lockdata[name, "pre"]) {
95		printc("\t"lockdata[name, "pre"]"(a);");
96	}
97}
98
99function add_post(name)
100{
101	if (lockdata[name, "post"]) {
102		printc("\t"lockdata[name, "post"]"(a, rc);");
103	}
104}
105
106function find_arg_with_type (type)
107{
108	for (jj = 0; jj < numargs; jj++) {
109		if (types[jj] == type) {
110			return "VOPARG_OFFSETOF(struct " \
111			    name "_args,a_" args[jj] ")";
112		}
113	}
114
115	return "VDESC_NO_OFFSET";
116}
117
118BEGIN{
119
120# Process the command line
121for (i = 1; i < ARGC; i++) {
122	arg = ARGV[i];
123	if (arg !~ /^-[chpq]+$/ && arg !~ /\.src$/)
124		usage();
125	if (arg ~ /^-.*c/)
126		cfile = "vnode_if.c";
127	if (arg ~ /^-.*h/)
128		hfile = "vnode_if.h";
129	if (arg ~ /^-.*p/)
130		pfile = "vnode_if_newproto.h";
131	if (arg ~ /^-.*q/)
132		qfile = "vnode_if_typedef.h";
133	if (arg ~ /\.src$/)
134		srcfile = arg;
135}
136ARGC = 1;
137
138if (!cfile && !hfile && !pfile && !qfile)
139	exit 0;
140
141if (!srcfile)
142	usage();
143
144common_head = \
145    "/*\n" \
146    " * This file is produced automatically.\n" \
147    " * Do not modify anything in here by hand.\n" \
148    " *\n" \
149    " * Created from $FreeBSD$\n" \
150    " */\n" \
151    "\n";
152
153if (pfile) {
154	printp(common_head)
155	printp("struct vop_vector {")
156	printp("\tstruct vop_vector\t*vop_default;")
157	printp("\tvop_bypass_t\t*vop_bypass;")
158}
159
160if (qfile) {
161	printq(common_head)
162}
163
164if (hfile) {
165	printh(common_head "extern struct vnodeop_desc vop_default_desc;");
166	printh("#include \"vnode_if_typedef.h\"")
167	printh("#include \"vnode_if_newproto.h\"")
168}
169
170if (cfile) {
171	printc(common_head \
172	    "#include <sys/param.h>\n" \
173	    "#include <sys/event.h>\n" \
174	    "#include <sys/mount.h>\n" \
175	    "#include <sys/systm.h>\n" \
176	    "#include <sys/vnode.h>\n" \
177	    "\n" \
178	    "struct vnodeop_desc vop_default_desc = {\n" \
179	    "	\"default\",\n" \
180	    "	0,\n" \
181	    "	(vop_bypass_t *)vop_panic,\n" \
182	    "	NULL,\n" \
183	    "	VDESC_NO_OFFSET,\n" \
184	    "	VDESC_NO_OFFSET,\n" \
185	    "	VDESC_NO_OFFSET,\n" \
186	    "	VDESC_NO_OFFSET,\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		lockdata["vop_" $2, $3, "Entry"] = $4;
199		lockdata["vop_" $2, $3, "OK"]    = $5;
200		lockdata["vop_" $2, $3, "Error"] = $6;
201		continue;
202	}
203
204	if ($1 ~ /^#!/) {
205		if (NF != 4 || $1 != "#!")
206			continue;
207		if ($3 != "pre" && $3 != "post")
208			continue;
209		lockdata["vop_" $2, $3] = $4;
210		continue;
211	}
212	if ($1 ~ /^#/)
213		continue;
214
215	# Get the function name.
216	name = $1;
217	uname = toupper(name);
218
219	# Start constructing a ktrpoint string
220	ctrstr = "\"" uname;
221	# Get the function arguments.
222	for (numargs = 0; ; ++numargs) {
223		if ((getline < srcfile) <= 0) {
224			die("Unable to read through the arguments for \"%s\"",
225			    name);
226		}
227		if ($1 ~ /^\};/)
228			break;
229
230		# Delete comments, if any.
231		gsub (/\/\*.*\*\//, "");
232
233		# Condense whitespace and delete leading/trailing space.
234		gsub(/[[:space:]]+/, " ");
235		sub(/^ /, "");
236		sub(/ $/, "");
237
238		# Pick off direction.
239		if ($1 != "INOUT" && $1 != "IN" && $1 != "OUT")
240			die("No IN/OUT direction for \"%s\".", $0);
241		dirs[numargs] = $1;
242		sub(/^[A-Z]* /, "");
243
244		if ((reles[numargs] = $1) == "WILLRELE")
245			sub(/^[A-Z]* /, "");
246		else
247			reles[numargs] = "WONTRELE";
248
249		# kill trailing ;
250		if (sub(/;$/, "") < 1)
251			die("Missing end-of-line ; in \"%s\".", $0);
252
253		# pick off variable name
254		if ((argp = match($0, /[A-Za-z0-9_]+$/)) < 1)
255			die("Missing var name \"a_foo\" in \"%s\".", $0);
256		args[numargs] = substr($0, argp);
257		$0 = substr($0, 1, argp - 1);
258
259		# what is left must be type
260		# remove trailing space (if any)
261		sub(/ $/, "");
262		types[numargs] = $0;
263
264		# We can do a maximum of 6 arguments to CTR*
265		if (numargs <= 6) {
266			if (numargs == 0)
267				ctrstr = ctrstr "(" args[numargs];
268			else
269				ctrstr = ctrstr ", " args[numargs];
270			if (types[numargs] ~ /\*/)
271				ctrstr = ctrstr " 0x%lX";
272			else
273				ctrstr = ctrstr " %ld";
274		}
275	}
276	if (numargs > 6)
277		ctrargs = 6;
278	else
279		ctrargs = numargs;
280	ctrstr = "\tCTR" ctrargs "(KTR_VOP,\n\t    " ctrstr ")\",\n\t    ";
281	ctrstr = ctrstr "a->a_" args[0];
282	for (i = 1; i < ctrargs; ++i)
283		ctrstr = ctrstr ", a->a_" args[i];
284	ctrstr = ctrstr ");";
285
286	if (pfile) {
287		printp("\t"name"_t\t*"name";")
288	}
289	if (qfile) {
290		printq("struct "name"_args;")
291		printq("typedef int "name"_t(struct "name"_args *);\n")
292	}
293
294	if (hfile) {
295		# Print out the vop_F_args structure.
296		printh("struct "name"_args {\n\tstruct vop_generic_args a_gen;");
297		for (i = 0; i < numargs; ++i)
298			printh("\t" t_spc(types[i]) "a_" args[i] ";");
299		printh("};");
300		printh("");
301
302		# Print out extern declaration.
303		printh("extern struct vnodeop_desc " name "_desc;");
304		printh("");
305
306		# Print out function prototypes.
307		printh("int " uname "_AP(struct " name "_args *);");
308		printh("int " uname "_APV(struct vop_vector *vop, struct " name "_args *);");
309		printh("");
310		printh("static __inline int " uname "(");
311		for (i = 0; i < numargs; ++i) {
312			printh("\t" t_spc(types[i]) args[i] \
313			    (i < numargs - 1 ? "," : ")"));
314		}
315		printh("{");
316		printh("\tstruct " name "_args a;");
317		printh("");
318		printh("\ta.a_gen.a_desc = &" name "_desc;");
319		for (i = 0; i < numargs; ++i)
320			printh("\ta.a_" args[i] " = " args[i] ";");
321		printh("\treturn (" uname "_APV("args[0]"->v_op, &a));");
322		printh("}");
323
324		printh("");
325	}
326
327	if (cfile) {
328		# Print out the vop_F_vp_offsets structure.  This all depends
329		# on naming conventions and nothing else.
330		printc("static int " name "_vp_offsets[] = {");
331		# as a side effect, figure out the releflags
332		releflags = "";
333		vpnum = 0;
334		for (i = 0; i < numargs; i++) {
335			if (types[i] == "struct vnode *") {
336				printc("\tVOPARG_OFFSETOF(struct " name \
337				    "_args,a_" args[i] "),");
338				if (reles[i] == "WILLRELE") {
339					releflags = releflags \
340					    "|VDESC_VP" vpnum "_WILLRELE";
341				}
342				vpnum++;
343			}
344		}
345
346		sub(/^\|/, "", releflags);
347		printc("\tVDESC_NO_OFFSET");
348		printc("};");
349
350		# Print out function.
351		printc("\nint\n" uname "_AP(struct " name "_args *a)");
352		printc("{");
353		printc("");
354		printc("\treturn(" uname "_APV(a->a_" args[0] "->v_op, a));");
355		printc("}");
356		printc("\nint\n" uname "_APV(struct vop_vector *vop, struct " name "_args *a)");
357		printc("{");
358		printc("\tint rc;");
359		printc("");
360		printc("\tVNASSERT(a->a_gen.a_desc == &" name "_desc, a->a_" args[0]",");
361		printc("\t    (\"Wrong a_desc in " name "(%p, %p)\", a->a_" args[0]", a));");
362		printc("\twhile(vop != NULL && \\");
363		printc("\t    vop->"name" == NULL && vop->vop_bypass == NULL)")
364		printc("\t\tvop = vop->vop_default;")
365		printc("\tVNASSERT(vop != NULL, a->a_" args[0]", (\"No "name"(%p, %p)\", a->a_" args[0]", a));")
366		for (i = 0; i < numargs; ++i)
367			add_debug_code(name, args[i], "Entry", "\t");
368		add_pre(name);
369		printc("\tif (vop->"name" != NULL)")
370		printc("\t\trc = vop->"name"(a);")
371		printc("\telse")
372		printc("\t\trc = vop->vop_bypass(&a->a_gen);")
373		printc(ctrstr);
374		printc("\tif (rc == 0) {");
375		for (i = 0; i < numargs; ++i)
376			add_debug_code(name, args[i], "OK", "\t\t");
377		printc("\t} else {");
378		for (i = 0; i < numargs; ++i)
379			add_debug_code(name, args[i], "Error", "\t\t");
380		printc("\t}");
381		add_post(name);
382		printc("\treturn (rc);");
383		printc("}\n");
384
385		# Print out the vnodeop_desc structure.
386		printc("struct vnodeop_desc " name "_desc = {");
387		# printable name
388		printc("\t\"" name "\",");
389		# flags
390		vppwillrele = "";
391		for (i = 0; i < numargs; i++) {
392			if (types[i] == "struct vnode **" && \
393			    reles[i] == "WILLRELE") {
394				vppwillrele = "|VDESC_VPP_WILLRELE";
395			}
396		}
397
398		if (!releflags)
399			releflags = "0";
400		printc("\t" releflags vppwillrele ",");
401
402		# function to call
403		printc("\t(vop_bypass_t *)" uname "_AP,");
404		# vp offsets
405		printc("\t" name "_vp_offsets,");
406		# vpp (if any)
407		printc("\t" find_arg_with_type("struct vnode **") ",");
408		# cred (if any)
409		printc("\t" find_arg_with_type("struct ucred *") ",");
410		# thread (if any)
411		printc("\t" find_arg_with_type("struct thread *") ",");
412		# componentname
413		printc("\t" find_arg_with_type("struct componentname *") ",");
414		# transport layer information
415		printc("};\n");
416	}
417}
418
419if (pfile)
420	printp("};")
421
422if (hfile)
423	close(hfile);
424if (cfile)
425	close(cfile);
426if (pfile)
427	close(pfile);
428close(srcfile);
429
430exit 0;
431
432}
433