xref: /freebsd/sys/tools/vnode_if.awk (revision 6af83ee0d2941d18880b6aaa2b4facd1d30c6106)
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 (0) {
82			# XXX More checks!
83		}
84		if (arg ~ /^\*/) {
85			printc("ind}");
86		}
87	}
88}
89
90function add_debug_pre(name)
91{
92	if (lockdata[name, "pre"]) {
93		printc("#ifdef	DEBUG_VFS_LOCKS");
94		printc("\t"lockdata[name, "pre"]"(a);");
95		printc("#endif");
96	}
97}
98
99function add_debug_post(name)
100{
101	if (lockdata[name, "post"]) {
102		printc("#ifdef	DEBUG_VFS_LOCKS");
103		printc("\t"lockdata[name, "post"]"(a, rc);");
104		printc("#endif");
105	}
106}
107
108function find_arg_with_type (type)
109{
110	for (jj = 0; jj < numargs; jj++) {
111		if (types[jj] == type) {
112			return "VOPARG_OFFSETOF(struct " \
113			    name "_args,a_" args[jj] ")";
114		}
115	}
116
117	return "VDESC_NO_OFFSET";
118}
119
120BEGIN{
121
122# Process the command line
123for (i = 1; i < ARGC; i++) {
124	arg = ARGV[i];
125	if (arg !~ /^-[chpq]+$/ && arg !~ /\.src$/)
126		usage();
127	if (arg ~ /^-.*c/)
128		cfile = "vnode_if.c";
129	if (arg ~ /^-.*h/)
130		hfile = "vnode_if.h";
131	if (arg ~ /^-.*p/)
132		pfile = "vnode_if_newproto.h";
133	if (arg ~ /^-.*q/)
134		qfile = "vnode_if_typedef.h";
135	if (arg ~ /\.src$/)
136		srcfile = arg;
137}
138ARGC = 1;
139
140if (!cfile && !hfile && !pfile && !qfile)
141	exit 0;
142
143if (!srcfile)
144	usage();
145
146common_head = \
147    "/*\n" \
148    " * This file is produced automatically.\n" \
149    " * Do not modify anything in here by hand.\n" \
150    " *\n" \
151    " * Created from $FreeBSD$\n" \
152    " */\n" \
153    "\n";
154
155if (pfile) {
156	printp(common_head)
157	printp("struct vop_vector {")
158	printp("\tstruct vop_vector\t*vop_default;")
159	printp("\tvop_bypass_t\t*vop_bypass;")
160}
161
162if (qfile) {
163	printq(common_head)
164}
165
166if (hfile) {
167	printh(common_head "extern struct vnodeop_desc vop_default_desc;");
168	printh("#include \"vnode_if_typedef.h\"")
169	printh("#include \"vnode_if_newproto.h\"")
170}
171
172if (cfile) {
173	printc(common_head \
174	    "#include <sys/param.h>\n" \
175	    "#include <sys/systm.h>\n" \
176	    "#include <sys/vnode.h>\n" \
177	    "#include <sys/systm.h>\n" \
178	    "\n" \
179	    "struct vnodeop_desc vop_default_desc = {\n" \
180	    "	\"default\",\n" \
181	    "	0,\n" \
182	    "	(void *)(uintptr_t)vop_panic,\n" \
183	    "	NULL,\n" \
184	    "	VDESC_NO_OFFSET,\n" \
185	    "	VDESC_NO_OFFSET,\n" \
186	    "	VDESC_NO_OFFSET,\n" \
187	    "	VDESC_NO_OFFSET,\n" \
188	    "	NULL,\n" \
189	    "};\n");
190}
191
192while ((getline < srcfile) > 0) {
193	if (NF == 0)
194		continue;
195	if ($1 ~ /^#%/) {
196		if (NF != 6  ||  $1 != "#%"  || \
197		    $2 !~ /^[a-z]+$/  ||  $3 !~ /^[a-z]+$/  || \
198		    $4 !~ /^.$/  ||  $5 !~ /^.$/  ||  $6 !~ /^.$/)
199			continue;
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,\n\t    " ctrstr ")\",\n\t    ";
283	ctrstr = ctrstr "a->a_" args[0];
284	for (i = 1; i < ctrargs; ++i)
285		ctrstr = ctrstr ", a->a_" args[i];
286	ctrstr = ctrstr ");";
287
288	if (pfile) {
289		printp("\t"name"_t\t*"name";")
290	}
291	if (qfile) {
292		printq("struct "name"_args;")
293		printq("typedef int "name"_t(struct "name"_args *);\n")
294	}
295
296	if (hfile) {
297		# Print out the vop_F_args structure.
298		printh("struct "name"_args {\n\tstruct vop_generic_args a_gen;");
299		for (i = 0; i < numargs; ++i)
300			printh("\t" t_spc(types[i]) "a_" args[i] ";");
301		printh("};");
302		printh("");
303
304		# Print out extern declaration.
305		printh("extern struct vnodeop_desc " name "_desc;");
306		printh("");
307
308		# Print out function prototypes.
309		printh("int " uname "_AP(struct " name "_args *);");
310		printh("int " uname "_APV(struct vop_vector *vop, struct " name "_args *);");
311		printh("");
312		printh("static __inline int " uname "(");
313		for (i = 0; i < numargs; ++i) {
314			printh("\t" t_spc(types[i]) args[i] \
315			    (i < numargs - 1 ? "," : ")"));
316		}
317		printh("{");
318		printh("\tstruct " name "_args a;");
319		printh("");
320		printh("\ta.a_gen.a_desc = &" name "_desc;");
321		for (i = 0; i < numargs; ++i)
322			printh("\ta.a_" args[i] " = " args[i] ";");
323		printh("\treturn (" uname "_APV("args[0]"->v_op, &a));");
324		printh("}");
325
326		printh("");
327	}
328
329	if (cfile) {
330		# Print out the vop_F_vp_offsets structure.  This all depends
331		# on naming conventions and nothing else.
332		printc("static int " name "_vp_offsets[] = {");
333		# as a side effect, figure out the releflags
334		releflags = "";
335		vpnum = 0;
336		for (i = 0; i < numargs; i++) {
337			if (types[i] == "struct vnode *") {
338				printc("\tVOPARG_OFFSETOF(struct " name \
339				    "_args,a_" args[i] "),");
340				if (reles[i] == "WILLRELE") {
341					releflags = releflags \
342					    "|VDESC_VP" vpnum "_WILLRELE";
343				}
344				vpnum++;
345			}
346		}
347
348		sub(/^\|/, "", releflags);
349		printc("\tVDESC_NO_OFFSET");
350		printc("};");
351
352		# Print out function.
353		printc("\nint\n" uname "_AP(struct " name "_args *a)");
354		printc("{");
355		printc("");
356		printc("\treturn(" uname "_APV(a->a_" args[0] "->v_op, a));");
357		printc("}");
358		printc("\nint\n" uname "_APV(struct vop_vector *vop, struct " name "_args *a)");
359		printc("{");
360		printc("\tint rc;");
361		printc("");
362		printc("\tVNASSERT(a->a_gen.a_desc == &" name "_desc, a->a_" args[0]",");
363		printc("\t    (\"Wrong a_desc in " name "(%p, %p)\", a->a_" args[0]", a));");
364		printc("\twhile(vop != NULL && \\");
365		printc("\t    vop->"name" == NULL && vop->vop_bypass == NULL)")
366		printc("\t\tvop = vop->vop_default;")
367		printc("\tVNASSERT(vop != NULL, a->a_" args[0]", (\"No "name"(%p, %p)\", a->a_" args[0]", a));")
368		for (i = 0; i < numargs; ++i)
369			add_debug_code(name, args[i], "Entry", "\t");
370		add_debug_pre(name);
371		printc("\tif (vop->"name" != NULL)")
372		printc("\t\trc = vop->"name"(a);")
373		printc("\telse")
374		printc("\t\trc = vop->vop_bypass(&a->a_gen);")
375		printc(ctrstr);
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_debug_post(name);
384		printc("\treturn (rc);");
385		printc("}\n");
386
387		# Print out the vnodeop_desc structure.
388		printc("struct vnodeop_desc " name "_desc = {");
389		# printable name
390		printc("\t\"" name "\",");
391		# flags
392		vppwillrele = "";
393		for (i = 0; i < numargs; i++) {
394			if (types[i] == "struct vnode **" && \
395			    reles[i] == "WILLRELE") {
396				vppwillrele = "|VDESC_VPP_WILLRELE";
397			}
398		}
399
400		if (!releflags)
401			releflags = "0";
402		printc("\t" releflags vppwillrele ",");
403
404		# function to call
405		printc("\t(void*)(uintptr_t)" uname "_AP,");
406		# vp offsets
407		printc("\t" name "_vp_offsets,");
408		# vpp (if any)
409		printc("\t" find_arg_with_type("struct vnode **") ",");
410		# cred (if any)
411		printc("\t" find_arg_with_type("struct ucred *") ",");
412		# thread (if any)
413		printc("\t" find_arg_with_type("struct thread *") ",");
414		# componentname
415		printc("\t" find_arg_with_type("struct componentname *") ",");
416		# transport layer information
417		printc("\tNULL,\n};\n");
418	}
419}
420
421if (pfile)
422	printp("};")
423
424if (hfile)
425	close(hfile);
426if (cfile)
427	close(cfile);
428if (pfile)
429	close(pfile);
430close(srcfile);
431
432exit 0;
433
434}
435