xref: /freebsd/sys/tools/vnode_if.awk (revision 5e395c34402dc3fd5c786168442290a63ff54596)
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 INVARIANTS");
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 INVARIANTS");
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/inotify.h>\n" \
197	    "#include <sys/kernel.h>\n" \
198	    "#include <sys/mount.h>\n" \
199	    "#include <sys/sdt.h>\n" \
200	    "#include <sys/signalvar.h>\n" \
201	    "#include <sys/systm.h>\n" \
202	    "#include <sys/vnode.h>\n" \
203	    "\n" \
204	    "SDT_PROVIDER_DECLARE(vfs);\n" \
205	    "\n" \
206	    "struct vnodeop_desc vop_default_desc = {\n" \
207	    "	\"default\",\n" \
208	    "	0,\n" \
209	    "   0,\n" \
210	    "	(vop_bypass_t *)vop_panic,\n" \
211	    "	NULL,\n" \
212	    "	VDESC_NO_OFFSET,\n" \
213	    "	VDESC_NO_OFFSET,\n" \
214	    "	VDESC_NO_OFFSET,\n" \
215	    "	VDESC_NO_OFFSET,\n" \
216	    "};\n");
217}
218
219while ((getline < srcfile) > 0) {
220	fnr++;
221	if (NF == 0)
222		continue;
223	if ($1 ~ /^%%/) {
224		if (NF != 6 ||
225		    $2 !~ /^[a-z_]+$/  ||  $3 !~ /^[a-z]+$/  ||
226		    $4 !~ /^.$/  ||  $5 !~ /^.$/  ||  $6 !~ /^.$/) {
227			die("Invalid %s construction", "%%");
228			continue;
229		}
230		lockdata["vop_" $2, $3, "Entry"] = $4;
231		lockdata["vop_" $2, $3, "OK"]    = $5;
232		lockdata["vop_" $2, $3, "Error"] = $6;
233		continue;
234	}
235
236	if ($1 ~ /^%!/) {
237		if (NF != 4 ||
238		    ($3 != "pre" && $3 != "post" &&
239		     $3 != "debugpre" && $3 != "debugpost")) {
240			die("Invalid %s construction", "%!");
241			continue;
242		}
243		lockdata["vop_" $2, $3] = $4;
244		continue;
245	}
246	if ($1 ~ /^#/)
247		continue;
248
249	# Get the function name.
250	name = $1;
251	uname = toupper(name);
252
253	# Get the function arguments.
254	for (numargs = 0; ; ++numargs) {
255		if ((getline < srcfile) <= 0) {
256			die("Unable to read through the arguments for \"%s\"",
257			    name);
258		}
259		fnr++;
260		if ($1 ~ /^\};/)
261			break;
262
263		# Delete comments, if any.
264		gsub (/\/\*.*\*\//, "");
265
266		# Condense whitespace and delete leading/trailing space.
267		gsub(/[[:space:]]+/, " ");
268		sub(/^ /, "");
269		sub(/ $/, "");
270
271		# Pick off direction.
272		if ($1 != "INOUT" && $1 != "IN" && $1 != "OUT")
273			die("No IN/OUT direction for \"%s\".", $0);
274		dirs[numargs] = $1;
275		sub(/^[A-Z]* /, "");
276
277		if ((reles[numargs] = $1) == "WILLRELE")
278			sub(/^[A-Z]* /, "");
279		else
280			reles[numargs] = "WONTRELE";
281
282		# kill trailing ;
283		if (sub(/;$/, "") < 1)
284			die("Missing end-of-line ; in \"%s\".", $0);
285
286		# pick off variable name
287		if ((argp = match($0, /[A-Za-z0-9_]+$/)) < 1)
288			die("Missing var name \"a_foo\" in \"%s\".", $0);
289		args[numargs] = substr($0, argp);
290		$0 = substr($0, 1, argp - 1);
291
292		# what is left must be type
293		# remove trailing space (if any)
294		sub(/ $/, "");
295		types[numargs] = $0;
296	}
297	if (numargs > 4)
298		ctrargs = 4;
299	else
300		ctrargs = numargs;
301	ctrstr = ctrargs "(KTR_VOP, \"VOP\", \"" uname "\", (uintptr_t)a,\n\t    ";
302	ctrstr = ctrstr "\"" args[0] ":0x%jX\", (uintptr_t)a->a_" args[0];
303	for (i = 1; i < ctrargs; ++i)
304		ctrstr = ctrstr ", \"" args[i] ":0x%jX\", a->a_" args[i];
305	ctrstr = ctrstr ");";
306
307	if (pfile) {
308		printp("\t"name"_t\t*"name";")
309	}
310	if (qfile) {
311		printq("struct "name"_args;")
312		printq("typedef int "name"_t(struct "name"_args *);\n")
313	}
314
315	if (hfile) {
316		# Print out the vop_F_args structure.
317		printh("struct "name"_args {\n\tstruct vop_generic_args a_gen;");
318		for (i = 0; i < numargs; ++i)
319			printh("\t" t_spc(types[i]) "a_" args[i] ";");
320		printh("};");
321		printh("");
322
323		# Print out extern declaration.
324		printh("extern struct vnodeop_desc " name "_desc;");
325		printh("");
326
327		printh("SDT_PROBE_DECLARE(vfs, vop, " name ", entry);\n");
328		printh("SDT_PROBE_DECLARE(vfs, vop, " name ", return);\n");
329		printh("");
330
331		# Print out function prototypes.
332		printh("int " uname "_AP(struct " name "_args *);");
333		printh("int " uname "_APV(const struct vop_vector *vop, struct " name "_args *);");
334		printh("");
335		printh("static __inline int " uname "(");
336		for (i = 0; i < numargs; ++i) {
337			printh("\t" t_spc(types[i]) args[i] \
338			    (i < numargs - 1 ? "," : ")"));
339		}
340		printh("{");
341		printh("\tstruct " name "_args a;");
342		printh("");
343		printh("\ta.a_gen.a_desc = &" name "_desc;");
344		for (i = 0; i < numargs; ++i)
345			printh("\ta.a_" args[i] " = " args[i] ";");
346		if (can_inline(name)) {
347			printh("\n#if !defined(INVARIANTS) && !defined(KTR)");
348			printh("\tint rc;")
349			printh("\tSDT_PROBE2(vfs, vop, " name ", entry, a.a_" args[0] ", &a);");
350			printh("\trc = " args[0]"->v_op->"name"(&a);");
351			printh("\tSDT_PROBE3(vfs, vop, " name ", return, a.a_" args[0] ", &a, rc);");
352			printh("\treturn (rc);")
353			printh("#else");
354		}
355		printh("\treturn (" uname "_APV("args[0]"->v_op, &a));");
356		if (can_inline(name))
357			printh("#endif");
358
359		printh("}");
360
361		printh("");
362	}
363
364	if (cfile) {
365		funcarr[name] = 1;
366		# Print out the vop_F_vp_offsets structure.  This all depends
367		# on naming conventions and nothing else.
368		printc("static int " name "_vp_offsets[] = {");
369		# as a side effect, figure out the releflags
370		releflags = "";
371		vpnum = 0;
372		for (i = 0; i < numargs; i++) {
373			if (types[i] == "struct vnode *") {
374				printc("\tVOPARG_OFFSETOF(struct " name \
375				    "_args,a_" args[i] "),");
376				if (reles[i] == "WILLRELE") {
377					releflags = releflags \
378					    "|VDESC_VP" vpnum "_WILLRELE";
379				}
380				vpnum++;
381			}
382		}
383
384		sub(/^\|/, "", releflags);
385		printc("\tVDESC_NO_OFFSET");
386		printc("};");
387
388		printc("\n");
389		printc("SDT_PROBE_DEFINE2(vfs, vop, " name ", entry, \"struct vnode *\", \"struct " name "_args *\");\n");
390		printc("SDT_PROBE_DEFINE3(vfs, vop, " name ", return, \"struct vnode *\", \"struct " name "_args *\", \"int\");\n");
391
392		# Print out function.
393		printc("\nint\n" uname "_AP(struct " name "_args *a)");
394		printc("{");
395		printc("");
396		printc("\treturn(" uname "_APV(a->a_" args[0] "->v_op, a));");
397		printc("}");
398		printc("\nint\n" uname "_APV(const struct vop_vector *vop, struct " name "_args *a)");
399		printc("{");
400		printc("\tint rc;");
401		printc("");
402		printc("\tVNASSERT(a->a_gen.a_desc == &" name "_desc, a->a_" args[0]",");
403		printc("\t    (\"Wrong a_desc in " name "(%p, %p)\", a->a_" args[0]", a));");
404		printc("\tVNASSERT(vop != NULL, a->a_" args[0]", (\"No "name"(%p, %p)\", a->a_" args[0]", a));")
405		printc("\tKTR_START" ctrstr);
406		add_debugpre(name);
407		add_pre(name);
408		for (i = 0; i < numargs; ++i)
409			add_debug_code(name, args[i], "Entry", "\t");
410		printc("\tSDT_PROBE2(vfs, vop, " name ", entry, a->a_" args[0] ", a);");
411		printc("\trc = vop->"name"(a);")
412		printc("\tSDT_PROBE3(vfs, vop, " name ", return, a->a_" args[0] ", a, rc);");
413		printc("\tif (rc == 0) {");
414		for (i = 0; i < numargs; ++i)
415			add_debug_code(name, args[i], "OK", "\t\t");
416		printc("\t} else {");
417		for (i = 0; i < numargs; ++i)
418			add_debug_code(name, args[i], "Error", "\t\t");
419		printc("\t}");
420		add_post(name);
421		add_debugpost(name);
422		printc("\tKTR_STOP" ctrstr);
423		printc("\treturn (rc);");
424		printc("}\n");
425
426		# Print out the vnodeop_desc structure.
427		printc("struct vnodeop_desc " name "_desc = {");
428		# printable name
429		printc("\t\"" name "\",");
430		# flags
431		vppwillrele = "";
432		for (i = 0; i < numargs; i++) {
433			if (types[i] == "struct vnode **" && \
434			    reles[i] == "WILLRELE") {
435				vppwillrele = "|VDESC_VPP_WILLRELE";
436			}
437		}
438
439		if (!releflags)
440			releflags = "0";
441		printc("\t" releflags vppwillrele ",");
442
443		# index in struct vop_vector
444		printc("\t__offsetof(struct vop_vector, " name "),");
445		# function to call
446		printc("\t(vop_bypass_t *)" uname "_AP,");
447		# vp offsets
448		printc("\t" name "_vp_offsets,");
449		# vpp (if any)
450		printc("\t" find_arg_with_type("struct vnode **") ",");
451		# cred (if any)
452		printc("\t" find_arg_with_type("struct ucred *") ",");
453		# thread (if any)
454		printc("\t" find_arg_with_type("struct thread *") ",");
455		# componentname
456		printc("\t" find_arg_with_type("struct componentname *") ",");
457		# transport layer information
458		printc("};\n");
459	}
460}
461
462if (cfile) {
463	printc("void");
464	printc("vfs_vector_op_register(struct vop_vector *orig_vop)");
465	printc("{");
466	printc("\tstruct vop_vector *vop;");
467	printc("");
468	printc("\tif (orig_vop->registered)");
469	printc("\t\tpanic(\"%s: vop_vector %p already registered\",")
470	printc("\t\t    __func__, orig_vop);");
471	printc("");
472	printc("\tcache_vop_vector_register(orig_vop);");
473	printc("");
474	for (name in funcarr) {
475		printc("\tvop = orig_vop;");
476		printc("\twhile (vop != NULL && \\");
477		printc("\t    vop->"name" == NULL && vop->vop_bypass == NULL)")
478		printc("\t\tvop = vop->vop_default;")
479		printc("\tif (vop != NULL)");
480		printc("\t\torig_vop->"name" = vop->"name";");
481		printc("");
482	}
483	printc("\tvop = orig_vop;");
484	printc("\twhile (vop != NULL && vop->vop_bypass == NULL)")
485	printc("\t\tvop = vop->vop_default;")
486	printc("\tif (vop != NULL)");
487	printc("\t\torig_vop->vop_bypass = vop->vop_bypass;");
488	printc("");
489	for (name in funcarr) {
490		printc("\tif (orig_vop->"name" == NULL)");
491		printc("\t\torig_vop->"name" = (void *)orig_vop->vop_bypass;");
492	}
493	printc("");
494	printc("\torig_vop->registered = true;");
495	printc("}")
496}
497
498if (hfile) {
499	printh("void vfs_vector_op_register(struct vop_vector *orig_vop);");
500}
501
502if (pfile) {
503	printp("\tbool\tregistered;")
504	printp("};")
505}
506
507if (hfile)
508	close(hfile);
509if (cfile)
510	close(cfile);
511if (pfile)
512	close(pfile);
513close(srcfile);
514
515exit 0;
516
517}
518