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