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