xref: /freebsd/sys/tools/vnode_if.awk (revision 59c8e88e72633afbc47a4ace0d2170d00d51f7dc)
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# Script to produce VFS front-end sugar.
34#
35# usage: vnode_if.awk <srcfile> [-c | -h | -p | -q]
36#	(where <srcfile> is currently /sys/kern/vnode_if.src)
37#	The source file must have a .src extension
38#
39
40function usage()
41{
42	print "usage: vnode_if.awk <srcfile> [-c|-h|-p|-q]";
43	exit 1;
44}
45
46function die(msg, what)
47{
48	printf srcfile "(" fnr "): " > "/dev/stderr";
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		printc(ind"ASSERT_VI_UNLOCKED("star"a->a_"arg", \""uname" "pos" ("arg")\");");
73		# Add assertions for locking
74		if (lockdata[name, arg, pos] == "L")
75			printc(ind"ASSERT_VOP_LOCKED(" star "a->a_"arg", \""uname" "pos" ("arg")\");");
76		else if (lockdata[name, arg, pos] == "U")
77			printc(ind"ASSERT_VOP_UNLOCKED(" star "a->a_"arg", \""uname" "pos" ("arg")\");");
78		else if (lockdata[name, arg, pos] == "E")
79			printc(ind"ASSERT_VOP_ELOCKED(" star "a->a_"arg", \""uname" "pos" ("arg")\");");
80		else if (0) {
81			# XXX More checks!
82		}
83	}
84}
85
86function add_debugpre(name)
87{
88	if (lockdata[name, "debugpre"]) {
89		printc("#ifdef DEBUG_VFS_LOCKS");
90		printc("\t"lockdata[name, "debugpre"]"(a);");
91		printc("#endif");
92	}
93}
94
95function add_debugpost(name)
96{
97	if (lockdata[name, "debugpost"]) {
98		printc("#ifdef DEBUG_VFS_LOCKS");
99		printc("\t"lockdata[name, "debugpost"]"(a, rc);");
100		printc("#endif");
101	}
102}
103
104function add_pre(name)
105{
106	if (lockdata[name, "pre"]) {
107		printc("\t"lockdata[name, "pre"]"(a);");
108	}
109}
110
111function add_post(name)
112{
113	if (lockdata[name, "post"]) {
114		printc("\t"lockdata[name, "post"]"(a, rc);");
115	}
116}
117
118function can_inline(name)
119{
120	if (lockdata[name, "pre"])
121		return 0;
122	if (lockdata[name, "post"])
123		return 0;
124	return 1;
125}
126
127function find_arg_with_type (type)
128{
129	for (jj = 0; jj < numargs; jj++) {
130		if (types[jj] == type) {
131			return "VOPARG_OFFSETOF(struct " \
132			    name "_args,a_" args[jj] ")";
133		}
134	}
135
136	return "VDESC_NO_OFFSET";
137}
138
139BEGIN{
140
141# Process the command line
142for (i = 1; i < ARGC; i++) {
143	arg = ARGV[i];
144	if (arg !~ /^-[chpq]+$/ && arg !~ /\.src$/)
145		usage();
146	if (arg ~ /^-.*c/)
147		cfile = "vnode_if.c";
148	if (arg ~ /^-.*h/)
149		hfile = "vnode_if.h";
150	if (arg ~ /^-.*p/)
151		pfile = "vnode_if_newproto.h";
152	if (arg ~ /^-.*q/)
153		qfile = "vnode_if_typedef.h";
154	if (arg ~ /\.src$/)
155		srcfile = arg;
156}
157ARGC = 1;
158
159if (!cfile && !hfile && !pfile && !qfile)
160	exit 0;
161
162if (!srcfile)
163	usage();
164
165# Avoid a literal generated file tag here.
166generated = "@" "generated";
167
168common_head = \
169    "/*\n" \
170    " * This file is " generated " automatically.\n" \
171    " * Do not modify anything in here by hand.\n" \
172    " */\n" \
173    "\n";
174
175if (pfile) {
176	printp(common_head)
177	printp("struct vop_vector {")
178	printp("\tstruct vop_vector\t*vop_default;")
179	printp("\tvop_bypass_t\t*vop_bypass;")
180}
181
182if (qfile) {
183	printq(common_head)
184}
185
186if (hfile) {
187	printh(common_head "extern struct vnodeop_desc vop_default_desc;");
188	printh("#include \"vnode_if_typedef.h\"")
189	printh("#include \"vnode_if_newproto.h\"")
190}
191
192if (cfile) {
193	printc(common_head \
194	    "#include <sys/param.h>\n" \
195	    "#include <sys/event.h>\n" \
196	    "#include <sys/kernel.h>\n" \
197	    "#include <sys/mount.h>\n" \
198	    "#include <sys/sdt.h>\n" \
199	    "#include <sys/signalvar.h>\n" \
200	    "#include <sys/systm.h>\n" \
201	    "#include <sys/vnode.h>\n" \
202	    "\n" \
203	    "SDT_PROVIDER_DECLARE(vfs);\n" \
204	    "\n" \
205	    "struct vnodeop_desc vop_default_desc = {\n" \
206	    "	\"default\",\n" \
207	    "	0,\n" \
208	    "   0,\n" \
209	    "	(vop_bypass_t *)vop_panic,\n" \
210	    "	NULL,\n" \
211	    "	VDESC_NO_OFFSET,\n" \
212	    "	VDESC_NO_OFFSET,\n" \
213	    "	VDESC_NO_OFFSET,\n" \
214	    "	VDESC_NO_OFFSET,\n" \
215	    "};\n");
216}
217
218while ((getline < srcfile) > 0) {
219	fnr++;
220	if (NF == 0)
221		continue;
222	if ($1 ~ /^%%/) {
223		if (NF != 6 ||
224		    $2 !~ /^[a-z_]+$/  ||  $3 !~ /^[a-z]+$/  ||
225		    $4 !~ /^.$/  ||  $5 !~ /^.$/  ||  $6 !~ /^.$/) {
226			die("Invalid %s construction", "%%");
227			continue;
228		}
229		lockdata["vop_" $2, $3, "Entry"] = $4;
230		lockdata["vop_" $2, $3, "OK"]    = $5;
231		lockdata["vop_" $2, $3, "Error"] = $6;
232		continue;
233	}
234
235	if ($1 ~ /^%!/) {
236		if (NF != 4 ||
237		    ($3 != "pre" && $3 != "post" &&
238		     $3 != "debugpre" && $3 != "debugpost")) {
239			die("Invalid %s construction", "%!");
240			continue;
241		}
242		lockdata["vop_" $2, $3] = $4;
243		continue;
244	}
245	if ($1 ~ /^#/)
246		continue;
247
248	# Get the function name.
249	name = $1;
250	uname = toupper(name);
251
252	# Get the function arguments.
253	for (numargs = 0; ; ++numargs) {
254		if ((getline < srcfile) <= 0) {
255			die("Unable to read through the arguments for \"%s\"",
256			    name);
257		}
258		fnr++;
259		if ($1 ~ /^\};/)
260			break;
261
262		# Delete comments, if any.
263		gsub (/\/\*.*\*\//, "");
264
265		# Condense whitespace and delete leading/trailing space.
266		gsub(/[[:space:]]+/, " ");
267		sub(/^ /, "");
268		sub(/ $/, "");
269
270		# Pick off direction.
271		if ($1 != "INOUT" && $1 != "IN" && $1 != "OUT")
272			die("No IN/OUT direction for \"%s\".", $0);
273		dirs[numargs] = $1;
274		sub(/^[A-Z]* /, "");
275
276		if ((reles[numargs] = $1) == "WILLRELE")
277			sub(/^[A-Z]* /, "");
278		else
279			reles[numargs] = "WONTRELE";
280
281		# kill trailing ;
282		if (sub(/;$/, "") < 1)
283			die("Missing end-of-line ; in \"%s\".", $0);
284
285		# pick off variable name
286		if ((argp = match($0, /[A-Za-z0-9_]+$/)) < 1)
287			die("Missing var name \"a_foo\" in \"%s\".", $0);
288		args[numargs] = substr($0, argp);
289		$0 = substr($0, 1, argp - 1);
290
291		# what is left must be type
292		# remove trailing space (if any)
293		sub(/ $/, "");
294		types[numargs] = $0;
295	}
296	if (numargs > 4)
297		ctrargs = 4;
298	else
299		ctrargs = numargs;
300	ctrstr = ctrargs "(KTR_VOP, \"VOP\", \"" uname "\", (uintptr_t)a,\n\t    ";
301	ctrstr = ctrstr "\"" args[0] ":0x%jX\", (uintptr_t)a->a_" args[0];
302	for (i = 1; i < ctrargs; ++i)
303		ctrstr = ctrstr ", \"" args[i] ":0x%jX\", a->a_" args[i];
304	ctrstr = ctrstr ");";
305
306	if (pfile) {
307		printp("\t"name"_t\t*"name";")
308	}
309	if (qfile) {
310		printq("struct "name"_args;")
311		printq("typedef int "name"_t(struct "name"_args *);\n")
312	}
313
314	if (hfile) {
315		# Print out the vop_F_args structure.
316		printh("struct "name"_args {\n\tstruct vop_generic_args a_gen;");
317		for (i = 0; i < numargs; ++i)
318			printh("\t" t_spc(types[i]) "a_" args[i] ";");
319		printh("};");
320		printh("");
321
322		# Print out extern declaration.
323		printh("extern struct vnodeop_desc " name "_desc;");
324		printh("");
325
326		# Print out function prototypes.
327		printh("int " uname "_AP(struct " name "_args *);");
328		printh("int " uname "_APV(struct vop_vector *vop, struct " name "_args *);");
329		printh("");
330		printh("static __inline int " uname "(");
331		for (i = 0; i < numargs; ++i) {
332			printh("\t" t_spc(types[i]) args[i] \
333			    (i < numargs - 1 ? "," : ")"));
334		}
335		printh("{");
336		printh("\tstruct " name "_args a;");
337		printh("");
338		printh("\ta.a_gen.a_desc = &" name "_desc;");
339		for (i = 0; i < numargs; ++i)
340			printh("\ta.a_" args[i] " = " args[i] ";");
341		if (can_inline(name)) {
342			printh("\n#if !defined(DEBUG_VFS_LOCKS) && !defined(INVARIANTS) && !defined(KTR)");
343			printh("\tif (!SDT_PROBES_ENABLED())");
344			printh("\t\treturn (" args[0]"->v_op->"name"(&a));");
345			printh("\telse");
346			printh("\t\treturn (" uname "_APV("args[0]"->v_op, &a));");
347			printh("#else");
348		}
349		printh("\treturn (" uname "_APV("args[0]"->v_op, &a));");
350		if (can_inline(name))
351			printh("#endif");
352
353		printh("}");
354
355		printh("");
356	}
357
358	if (cfile) {
359		funcarr[name] = 1;
360		# Print out the vop_F_vp_offsets structure.  This all depends
361		# on naming conventions and nothing else.
362		printc("static int " name "_vp_offsets[] = {");
363		# as a side effect, figure out the releflags
364		releflags = "";
365		vpnum = 0;
366		for (i = 0; i < numargs; i++) {
367			if (types[i] == "struct vnode *") {
368				printc("\tVOPARG_OFFSETOF(struct " name \
369				    "_args,a_" args[i] "),");
370				if (reles[i] == "WILLRELE") {
371					releflags = releflags \
372					    "|VDESC_VP" vpnum "_WILLRELE";
373				}
374				vpnum++;
375			}
376		}
377
378		sub(/^\|/, "", releflags);
379		printc("\tVDESC_NO_OFFSET");
380		printc("};");
381
382		printc("\n");
383		printc("SDT_PROBE_DEFINE2(vfs, vop, " name ", entry, \"struct vnode *\", \"struct " name "_args *\");\n");
384		printc("SDT_PROBE_DEFINE3(vfs, vop, " name ", return, \"struct vnode *\", \"struct " name "_args *\", \"int\");\n");
385
386		# Print out function.
387		printc("\nint\n" uname "_AP(struct " name "_args *a)");
388		printc("{");
389		printc("");
390		printc("\treturn(" uname "_APV(a->a_" args[0] "->v_op, a));");
391		printc("}");
392		printc("\nint\n" uname "_APV(struct vop_vector *vop, struct " name "_args *a)");
393		printc("{");
394		printc("\tint rc;");
395		printc("");
396		printc("\tVNASSERT(a->a_gen.a_desc == &" name "_desc, a->a_" args[0]",");
397		printc("\t    (\"Wrong a_desc in " name "(%p, %p)\", a->a_" args[0]", a));");
398		printc("\tVNASSERT(vop != NULL, a->a_" args[0]", (\"No "name"(%p, %p)\", a->a_" args[0]", a));")
399		printc("\tKTR_START" ctrstr);
400		add_debugpre(name);
401		add_pre(name);
402		for (i = 0; i < numargs; ++i)
403			add_debug_code(name, args[i], "Entry", "\t");
404		printc("\tif (!SDT_PROBES_ENABLED()) {");
405		printc("\t\trc = vop->"name"(a);")
406		printc("\t} else {")
407		printc("\t\tSDT_PROBE2(vfs, vop, " name ", entry, a->a_" args[0] ", a);");
408		printc("\t\trc = vop->"name"(a);")
409		printc("\t\tSDT_PROBE3(vfs, vop, " name ", return, a->a_" args[0] ", a, rc);");
410		printc("\t}")
411		printc("\tif (rc == 0) {");
412		for (i = 0; i < numargs; ++i)
413			add_debug_code(name, args[i], "OK", "\t\t");
414		printc("\t} else {");
415		for (i = 0; i < numargs; ++i)
416			add_debug_code(name, args[i], "Error", "\t\t");
417		printc("\t}");
418		add_post(name);
419		add_debugpost(name);
420		printc("\tKTR_STOP" ctrstr);
421		printc("\treturn (rc);");
422		printc("}\n");
423
424		# Print out the vnodeop_desc structure.
425		printc("struct vnodeop_desc " name "_desc = {");
426		# printable name
427		printc("\t\"" name "\",");
428		# flags
429		vppwillrele = "";
430		for (i = 0; i < numargs; i++) {
431			if (types[i] == "struct vnode **" && \
432			    reles[i] == "WILLRELE") {
433				vppwillrele = "|VDESC_VPP_WILLRELE";
434			}
435		}
436
437		if (!releflags)
438			releflags = "0";
439		printc("\t" releflags vppwillrele ",");
440
441		# index in struct vop_vector
442		printc("\t__offsetof(struct vop_vector, " name "),");
443		# function to call
444		printc("\t(vop_bypass_t *)" uname "_AP,");
445		# vp offsets
446		printc("\t" name "_vp_offsets,");
447		# vpp (if any)
448		printc("\t" find_arg_with_type("struct vnode **") ",");
449		# cred (if any)
450		printc("\t" find_arg_with_type("struct ucred *") ",");
451		# thread (if any)
452		printc("\t" find_arg_with_type("struct thread *") ",");
453		# componentname
454		printc("\t" find_arg_with_type("struct componentname *") ",");
455		# transport layer information
456		printc("};\n");
457	}
458}
459
460if (cfile) {
461	printc("void");
462	printc("vfs_vector_op_register(struct vop_vector *orig_vop)");
463	printc("{");
464	printc("\tstruct vop_vector *vop;");
465	printc("");
466	printc("\tif (orig_vop->registered)");
467	printc("\t\tpanic(\"%s: vop_vector %p already registered\",")
468	printc("\t\t    __func__, orig_vop);");
469	printc("");
470	printc("\tcache_vop_vector_register(orig_vop);");
471	printc("");
472	for (name in funcarr) {
473		printc("\tvop = orig_vop;");
474		printc("\twhile (vop != NULL && \\");
475		printc("\t    vop->"name" == NULL && vop->vop_bypass == NULL)")
476		printc("\t\tvop = vop->vop_default;")
477		printc("\tif (vop != NULL)");
478		printc("\t\torig_vop->"name" = vop->"name";");
479		printc("");
480	}
481	printc("\tvop = orig_vop;");
482	printc("\twhile (vop != NULL && vop->vop_bypass == NULL)")
483	printc("\t\tvop = vop->vop_default;")
484	printc("\tif (vop != NULL)");
485	printc("\t\torig_vop->vop_bypass = vop->vop_bypass;");
486	printc("");
487	for (name in funcarr) {
488		printc("\tif (orig_vop->"name" == NULL)");
489		printc("\t\torig_vop->"name" = (void *)orig_vop->vop_bypass;");
490	}
491	printc("");
492	printc("\torig_vop->registered = true;");
493	printc("}")
494}
495
496if (hfile) {
497	printh("void vfs_vector_op_register(struct vop_vector *orig_vop);");
498}
499
500if (pfile) {
501	printp("\tbool\tregistered;")
502	printp("};")
503}
504
505if (hfile)
506	close(hfile);
507if (cfile)
508	close(cfile);
509if (pfile)
510	close(pfile);
511close(srcfile);
512
513exit 0;
514
515}
516