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