xref: /freebsd/sys/tools/vnode_if.awk (revision 0b57cec536236d46e3dba9bd041533462f33dbb7)
1#!/usr/bin/awk -f
2
3#-
4# SPDX-License-Identifier: BSD-3-Clause
5#
6# Copyright (c) 1992, 1993
7#	The Regents of the University of California.  All rights reserved.
8#
9# Redistribution and use in source and binary forms, with or without
10# modification, are permitted provided that the following conditions
11# are met:
12# 1. Redistributions of source code must retain the above copyright
13#    notice, this list of conditions and the following disclaimer.
14# 2. Redistributions in binary form must reproduce the above copyright
15#    notice, this list of conditions and the following disclaimer in the
16#    documentation and/or other materials provided with the distribution.
17# 3. Neither the name of the University nor the names of its contributors
18#    may be used to endorse or promote products derived from this software
19#    without specific prior written permission.
20#
21# THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22# ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24# ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27# OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28# HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30# OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31# SUCH DAMAGE.
32
33#
34#	@(#)vnode_if.sh	8.1 (Berkeley) 6/10/93
35# $FreeBSD$
36#
37# Script to produce VFS front-end sugar.
38#
39# usage: vnode_if.awk <srcfile> [-c | -h | -p | -q]
40#	(where <srcfile> is currently /sys/kern/vnode_if.src)
41#	The source file must have a .src extension
42#
43
44function usage()
45{
46	print "usage: vnode_if.awk <srcfile> [-c|-h|-p|-q]";
47	exit 1;
48}
49
50function die(msg, what)
51{
52	printf srcfile "(" fnr "): " > "/dev/stderr";
53	printf msg "\n", what > "/dev/stderr";
54	exit 1;
55}
56
57function t_spc(type)
58{
59	# Append a space if the type is not a pointer
60	return (type ~ /\*$/) ? type : type " ";
61}
62
63# These are just for convenience ...
64function printc(s) {print s > cfile;}
65function printh(s) {print s > hfile;}
66function printp(s) {print s > pfile;}
67function printq(s) {print s > qfile;}
68
69function add_debug_code(name, arg, pos, ind)
70{
71	if (arg == "vpp")
72		star = "*";
73	else
74		star = "";
75	if (lockdata[name, arg, pos] && (lockdata[name, arg, pos] != "-")) {
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	}
88}
89
90function add_pre(name)
91{
92	if (lockdata[name, "pre"]) {
93		printc("\t"lockdata[name, "pre"]"(a);");
94	}
95}
96
97function add_post(name)
98{
99	if (lockdata[name, "post"]) {
100		printc("\t"lockdata[name, "post"]"(a, rc);");
101	}
102}
103
104function find_arg_with_type (type)
105{
106	for (jj = 0; jj < numargs; jj++) {
107		if (types[jj] == type) {
108			return "VOPARG_OFFSETOF(struct " \
109			    name "_args,a_" args[jj] ")";
110		}
111	}
112
113	return "VDESC_NO_OFFSET";
114}
115
116BEGIN{
117
118# Process the command line
119for (i = 1; i < ARGC; i++) {
120	arg = ARGV[i];
121	if (arg !~ /^-[chpq]+$/ && arg !~ /\.src$/)
122		usage();
123	if (arg ~ /^-.*c/)
124		cfile = "vnode_if.c";
125	if (arg ~ /^-.*h/)
126		hfile = "vnode_if.h";
127	if (arg ~ /^-.*p/)
128		pfile = "vnode_if_newproto.h";
129	if (arg ~ /^-.*q/)
130		qfile = "vnode_if_typedef.h";
131	if (arg ~ /\.src$/)
132		srcfile = arg;
133}
134ARGC = 1;
135
136if (!cfile && !hfile && !pfile && !qfile)
137	exit 0;
138
139if (!srcfile)
140	usage();
141
142# Avoid a literal generated file tag here.
143generated = "@" "generated";
144
145common_head = \
146    "/*\n" \
147    " * This file is " generated " automatically.\n" \
148    " * Do not modify anything in here by hand.\n" \
149    " *\n" \
150    " * Created from $FreeBSD$\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/kernel.h>\n" \
176	    "#include <sys/mount.h>\n" \
177	    "#include <sys/sdt.h>\n" \
178	    "#include <sys/signalvar.h>\n" \
179	    "#include <sys/systm.h>\n" \
180	    "#include <sys/vnode.h>\n" \
181	    "\n" \
182	    "SDT_PROVIDER_DECLARE(vfs);\n" \
183	    "\n" \
184	    "struct vnodeop_desc vop_default_desc = {\n" \
185	    "	\"default\",\n" \
186	    "	0,\n" \
187	    "   0,\n" \
188	    "	(vop_bypass_t *)vop_panic,\n" \
189	    "	NULL,\n" \
190	    "	VDESC_NO_OFFSET,\n" \
191	    "	VDESC_NO_OFFSET,\n" \
192	    "	VDESC_NO_OFFSET,\n" \
193	    "	VDESC_NO_OFFSET,\n" \
194	    "};\n");
195}
196
197while ((getline < srcfile) > 0) {
198	fnr++;
199	if (NF == 0)
200		continue;
201	if ($1 ~ /^%%/) {
202		if (NF != 6 ||
203		    $2 !~ /^[a-z_]+$/  ||  $3 !~ /^[a-z]+$/  ||
204		    $4 !~ /^.$/  ||  $5 !~ /^.$/  ||  $6 !~ /^.$/) {
205			die("Invalid %s construction", "%%");
206			continue;
207		}
208		lockdata["vop_" $2, $3, "Entry"] = $4;
209		lockdata["vop_" $2, $3, "OK"]    = $5;
210		lockdata["vop_" $2, $3, "Error"] = $6;
211		continue;
212	}
213
214	if ($1 ~ /^%!/) {
215		if (NF != 4 ||
216		    ($3 != "pre" && $3 != "post")) {
217			die("Invalid %s construction", "%!");
218			continue;
219		}
220		lockdata["vop_" $2, $3] = $4;
221		continue;
222	}
223	if ($1 ~ /^#/)
224		continue;
225
226	# Get the function name.
227	name = $1;
228	uname = toupper(name);
229
230	# Get the function arguments.
231	for (numargs = 0; ; ++numargs) {
232		if ((getline < srcfile) <= 0) {
233			die("Unable to read through the arguments for \"%s\"",
234			    name);
235		}
236		fnr++;
237		if ($1 ~ /^\};/)
238			break;
239
240		# Delete comments, if any.
241		gsub (/\/\*.*\*\//, "");
242
243		# Condense whitespace and delete leading/trailing space.
244		gsub(/[[:space:]]+/, " ");
245		sub(/^ /, "");
246		sub(/ $/, "");
247
248		# Pick off direction.
249		if ($1 != "INOUT" && $1 != "IN" && $1 != "OUT")
250			die("No IN/OUT direction for \"%s\".", $0);
251		dirs[numargs] = $1;
252		sub(/^[A-Z]* /, "");
253
254		if ((reles[numargs] = $1) == "WILLRELE")
255			sub(/^[A-Z]* /, "");
256		else
257			reles[numargs] = "WONTRELE";
258
259		# kill trailing ;
260		if (sub(/;$/, "") < 1)
261			die("Missing end-of-line ; in \"%s\".", $0);
262
263		# pick off variable name
264		if ((argp = match($0, /[A-Za-z0-9_]+$/)) < 1)
265			die("Missing var name \"a_foo\" in \"%s\".", $0);
266		args[numargs] = substr($0, argp);
267		$0 = substr($0, 1, argp - 1);
268
269		# what is left must be type
270		# remove trailing space (if any)
271		sub(/ $/, "");
272		types[numargs] = $0;
273	}
274	if (numargs > 4)
275		ctrargs = 4;
276	else
277		ctrargs = numargs;
278	ctrstr = ctrargs "(KTR_VOP, \"VOP\", \"" uname "\", (uintptr_t)a,\n\t    ";
279	ctrstr = ctrstr "\"" args[0] ":0x%jX\", (uintptr_t)a->a_" args[0];
280	for (i = 1; i < ctrargs; ++i)
281		ctrstr = ctrstr ", \"" args[i] ":0x%jX\", a->a_" args[i];
282	ctrstr = ctrstr ");";
283
284	if (pfile) {
285		printp("\t"name"_t\t*"name";")
286	}
287	if (qfile) {
288		printq("struct "name"_args;")
289		printq("typedef int "name"_t(struct "name"_args *);\n")
290	}
291
292	if (hfile) {
293		# Print out the vop_F_args structure.
294		printh("struct "name"_args {\n\tstruct vop_generic_args a_gen;");
295		for (i = 0; i < numargs; ++i)
296			printh("\t" t_spc(types[i]) "a_" args[i] ";");
297		printh("};");
298		printh("");
299
300		# Print out extern declaration.
301		printh("extern struct vnodeop_desc " name "_desc;");
302		printh("");
303
304		# Print out function prototypes.
305		printh("int " uname "_AP(struct " name "_args *);");
306		printh("int " uname "_APV(struct vop_vector *vop, struct " name "_args *);");
307		printh("");
308		printh("static __inline int " uname "(");
309		for (i = 0; i < numargs; ++i) {
310			printh("\t" t_spc(types[i]) args[i] \
311			    (i < numargs - 1 ? "," : ")"));
312		}
313		printh("{");
314		printh("\tstruct " name "_args a;");
315		printh("");
316		printh("\ta.a_gen.a_desc = &" name "_desc;");
317		for (i = 0; i < numargs; ++i)
318			printh("\ta.a_" args[i] " = " args[i] ";");
319		printh("\treturn (" uname "_APV("args[0]"->v_op, &a));");
320		printh("}");
321
322		printh("");
323	}
324
325	if (cfile) {
326		funcarr[name] = 1;
327		# Print out the vop_F_vp_offsets structure.  This all depends
328		# on naming conventions and nothing else.
329		printc("static int " name "_vp_offsets[] = {");
330		# as a side effect, figure out the releflags
331		releflags = "";
332		vpnum = 0;
333		for (i = 0; i < numargs; i++) {
334			if (types[i] == "struct vnode *") {
335				printc("\tVOPARG_OFFSETOF(struct " name \
336				    "_args,a_" args[i] "),");
337				if (reles[i] == "WILLRELE") {
338					releflags = releflags \
339					    "|VDESC_VP" vpnum "_WILLRELE";
340				}
341				vpnum++;
342			}
343		}
344
345		sub(/^\|/, "", releflags);
346		printc("\tVDESC_NO_OFFSET");
347		printc("};");
348
349		printc("\n");
350		printc("SDT_PROBE_DEFINE2(vfs, vop, " name ", entry, \"struct vnode *\", \"struct " name "_args *\");\n");
351		printc("SDT_PROBE_DEFINE3(vfs, vop, " name ", return, \"struct vnode *\", \"struct " name "_args *\", \"int\");\n");
352
353		# Print out function.
354		printc("\nint\n" uname "_AP(struct " name "_args *a)");
355		printc("{");
356		printc("");
357		printc("\treturn(" uname "_APV(a->a_" args[0] "->v_op, a));");
358		printc("}");
359		printc("\nint\n" uname "_APV(struct vop_vector *vop, struct " name "_args *a)");
360		printc("{");
361		printc("\tint rc;");
362		printc("");
363		printc("\tVNASSERT(a->a_gen.a_desc == &" name "_desc, a->a_" args[0]",");
364		printc("\t    (\"Wrong a_desc in " name "(%p, %p)\", a->a_" args[0]", a));");
365		printc("\tVNASSERT(vop != NULL, a->a_" args[0]", (\"No "name"(%p, %p)\", a->a_" args[0]", a));")
366		printc("\tKTR_START" ctrstr);
367		add_pre(name);
368		for (i = 0; i < numargs; ++i)
369			add_debug_code(name, args[i], "Entry", "\t");
370		printc("\tif (__predict_true(!SDT_PROBES_ENABLED() && vop->"name" != NULL)) {");
371		printc("\t\trc = vop->"name"(a);")
372		printc("\t} else {")
373		printc("\t\tSDT_PROBE2(vfs, vop, " name ", entry, a->a_" args[0] ", a);");
374		printc("\t\tif (vop->"name" != NULL)")
375		printc("\t\t\trc = vop->"name"(a);")
376		printc("\t\telse")
377		printc("\t\t\trc = vop->vop_bypass(&a->a_gen);")
378		printc("\t\tSDT_PROBE3(vfs, vop, " name ", return, a->a_" args[0] ", a, rc);");
379		printc("\t}")
380		printc("\tif (rc == 0) {");
381		for (i = 0; i < numargs; ++i)
382			add_debug_code(name, args[i], "OK", "\t\t");
383		printc("\t} else {");
384		for (i = 0; i < numargs; ++i)
385			add_debug_code(name, args[i], "Error", "\t\t");
386		printc("\t}");
387		add_post(name);
388		printc("\tKTR_STOP" ctrstr);
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		# index in struct vop_vector
410		printc("\t__offsetof(struct vop_vector, " name "),");
411		# function to call
412		printc("\t(vop_bypass_t *)" uname "_AP,");
413		# vp offsets
414		printc("\t" name "_vp_offsets,");
415		# vpp (if any)
416		printc("\t" find_arg_with_type("struct vnode **") ",");
417		# cred (if any)
418		printc("\t" find_arg_with_type("struct ucred *") ",");
419		# thread (if any)
420		printc("\t" find_arg_with_type("struct thread *") ",");
421		# componentname
422		printc("\t" find_arg_with_type("struct componentname *") ",");
423		# transport layer information
424		printc("};\n");
425	}
426}
427
428if (cfile) {
429	printc("void");
430	printc("vfs_vector_op_register(struct vop_vector *orig_vop)");
431	printc("{");
432	printc("\tstruct vop_vector *vop;");
433	printc("");
434	printc("\tif (orig_vop->registered)");
435	printc("\t\tpanic(\"%s: vop_vector %p already registered\",")
436	printc("\t\t    __func__, orig_vop);");
437	printc("");
438	for (name in funcarr) {
439		printc("\tvop = orig_vop;");
440		printc("\twhile (vop != NULL && \\");
441		printc("\t    vop->"name" == NULL && vop->vop_bypass == NULL)")
442		printc("\t\tvop = vop->vop_default;")
443		printc("\tif (vop != NULL)");
444		printc("\t\torig_vop->"name" = vop->"name";");
445		printc("");
446	}
447	printc("\tvop = orig_vop;");
448	printc("\twhile (vop != NULL && vop->vop_bypass == NULL)")
449	printc("\t\tvop = vop->vop_default;")
450	printc("\tif (vop != NULL)");
451	printc("\t\torig_vop->vop_bypass = vop->vop_bypass;");
452	printc("");
453	printc("\torig_vop->registered = true;");
454	printc("}")
455}
456
457if (hfile) {
458	printh("void vfs_vector_op_register(struct vop_vector *orig_vop);");
459}
460
461if (pfile) {
462	printp("\tbool\tregistered;")
463	printp("};")
464}
465
466if (hfile)
467	close(hfile);
468if (cfile)
469	close(cfile);
470if (pfile)
471	close(pfile);
472close(srcfile);
473
474exit 0;
475
476}
477