xref: /netbsd-src/sys/kern/vnode_if.sh (revision b7b7574d3bf8eeb51a1fa3977b59142ec6434a55)
1#!/bin/sh -
2copyright="\
3/*
4 * Copyright (c) 1992, 1993, 1994, 1995
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 * 3. 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"
32SCRIPT_ID='$NetBSD: vnode_if.sh,v 1.60 2014/01/13 12:07:55 hannken Exp $'
33
34# Script to produce VFS front-end sugar.
35#
36# usage: vnode_if.sh srcfile
37#	(where srcfile is currently /sys/kern/vnode_if.src)
38#
39
40if [ $# -ne 1 ] ; then
41	echo 'usage: vnode_if.sh srcfile'
42	exit 1
43fi
44
45# Name and revision of the source file.
46src=$1
47SRC_ID=`head -1 $src | sed -e 's/.*\$\(.*\)\$.*/\1/'`
48
49# Names of the created files.
50out_c=vnode_if.c
51out_rumpc=../rump/librump/rumpvfs/rumpvnode_if.c
52out_h=../sys/vnode_if.h
53out_rumph=../rump/include/rump/rumpvnode_if.h
54
55# generate VNODE_LOCKDEBUG checks (not fully functional)
56lockdebug=0
57
58# Awk program (must support nawk extensions)
59# Use "awk" at Berkeley, "nawk" or "gawk" elsewhere.
60awk=${AWK:-awk}
61
62# Does this awk have a "toupper" function? (i.e. is it GNU awk)
63isgawk=`$awk 'BEGIN { print toupper("true"); exit; }' 2>/dev/null`
64
65# If this awk does not define "toupper" then define our own.
66if [ "$isgawk" = TRUE ] ; then
67	# GNU awk provides it.
68	toupper=
69else
70	# Provide our own toupper()
71	toupper='
72function toupper(str) {
73	_toupper_cmd = "echo "str" |tr a-z A-Z"
74	_toupper_cmd | getline _toupper_str;
75	close(_toupper_cmd);
76	return _toupper_str;
77}'
78fi
79
80#
81# This is the common part of all awk programs that read $src
82# This parses the input for one function into the arrays:
83#	argdir, argtype, argname, willrele
84# and calls "doit()" to generate output for the function.
85#
86# Input to this parser is pre-processed slightly by sed
87# so this awk parser doesn't have to work so hard.  The
88# changes done by the sed pre-processing step are:
89#	insert a space beween * and pointer name
90#	replace semicolons with spaces
91#
92sed_prep='s:\*\([^\*/]\):\* \1:g
93s/;/ /'
94awk_parser='
95# Comment line
96/^#/	{ next; }
97# First line of description
98/^vop_/	{
99	name=$1;
100	args_name=$1;
101	argc=0;
102	willmake=-1;
103	next;
104}
105# Last line of description
106/^}/	{
107	doit();
108	next;
109}
110# Middle lines of description
111{
112	if ($1 == "VERSION") {
113		args_name=args_name "_v" $2;
114		next;
115	}
116
117	argdir[argc] = $1; i=2;
118
119	if ($2 == "LOCKED=YES") {
120		lockstate[argc] = 1;
121		i++;
122	} else if ($2 == "LOCKED=NO") {
123		lockstate[argc] = 0;
124		i++;
125	} else
126		lockstate[argc] = -1;
127
128	if ($2 == "WILLRELE" ||
129	    $3 == "WILLRELE") {
130		willrele[argc] = 1;
131		i++;
132	} else if ($2 == "WILLUNLOCK" ||
133		   $3 == "WILLUNLOCK") {
134		willrele[argc] = 2;
135		i++;
136	} else if ($2 == "WILLPUT" ||
137		   $3 == "WILLPUT") {
138		willrele[argc] = 3;
139		i++;
140	} else
141		willrele[argc] = 0;
142
143	if ($2 == "WILLMAKE") {
144		willmake=argc;
145		i++;
146	}
147
148	# XXX: replace non-portable types for rump.  We should really
149	# nuke the types from the kernel, but that is a battle for
150	# another day.
151	at = $i;
152	if (rump) {
153		if (at == "vm_prot_t")
154			at = "int";
155		if (at == "voff_t")
156			at = "off_t";
157		if (at == "kauth_cred_t")
158			at = "struct kauth_cred *"
159		if (at == "daddr_t")
160			at = "int64_t"
161	}
162	argtype[argc] = at;
163	i++;
164	while (i < NF) {
165		argtype[argc] = argtype[argc]" "$i;
166		i++;
167	}
168	argname[argc] = $i;
169	argc++;
170	next;
171}
172'
173
174# This is put before the copyright on each generated file.
175warning="\
176/*	@NetBSD@	*/
177
178/*
179 * Warning: DO NOT EDIT! This file is automatically generated!
180 * (Modifications made here may easily be lost!)
181 *
182 * Created from the file:
183 *	${SRC_ID}
184 * by the script:
185 *	${SCRIPT_ID}
186 */
187"
188
189# This is to satisfy McKusick (get rid of evil spaces 8^)
190anal_retentive='s:\([^/]\*\) :\1:g'
191
192do_hfile () {
193#
194# Redirect stdout to the H file.
195#
196echo "$0: Creating $1" 1>&2
197exec > $1
198rump=$2
199
200# Begin stuff
201if [ -z "${rump}" ]; then
202	SYS='SYS_'
203else
204	SYS='RUMP_RUMP'
205fi
206echo -n "$warning" | sed -e 's/\$//g;s/@/\$/g;s/ $//'
207echo ""
208echo -n "$copyright"
209echo ''
210echo "#ifndef _${SYS}VNODE_IF_H_"
211echo "#define _${SYS}VNODE_IF_H_"
212if [ ${lockdebug} -ne 0 ] ; then
213	echo ''
214	echo '#ifdef _KERNEL_OPT'
215	echo '#include "opt_vnode_lockdebug.h"'
216	echo '#endif /* _KERNEL_OPT */'
217fi
218[ -z "${rump}" ] && echo "
219extern const struct vnodeop_desc ${rump}vop_default_desc;"
220echo
221
222# Body stuff
223# This awk program needs toupper() so define it if necessary.
224sed -e "$sed_prep" $src | $awk -v rump=${rump} "$toupper"'
225function doit() {
226	name = rump name
227	# Declare arg struct, descriptor.
228	if (!rump) {
229		printf("\n#define %s_DESCOFFSET %d\n",
230		    toupper(name), vop_offset++);
231		printf("struct %s_args {\n", args_name);
232		printf("\tconst struct vnodeop_desc * a_desc;\n");
233		for (i=0; i<argc; i++) {
234			printf("\t%s a_%s;\n", argtype[i], argname[i]);
235		}
236		printf("};\n");
237		printf("extern const struct vnodeop_desc %s_desc;\n", name);
238	}
239	# Prototype it.
240	protoarg = sprintf("int %s(", toupper(name));
241	protolen = length(protoarg);
242	printf("%s", protoarg);
243	for (i=0; i<argc; i++) {
244		protoarg = sprintf("%s", argtype[i]);
245		if (i < (argc-1)) protoarg = (protoarg ", ");
246		arglen = length(protoarg);
247		if ((protolen + arglen) > 77) {
248			protoarg = ("\n    " protoarg);
249			arglen += 4;
250			protolen = 0;
251		}
252		printf("%s", protoarg);
253		protolen += arglen;
254	}
255	printf(");\n");
256}
257BEGIN	{
258	vop_offset = 1; # start at 1, to count the 'default' op
259
260	printf("struct buf;\n");
261	if (rump) {
262		printf("struct flock;\n");
263		printf("struct knote;\n");
264		printf("struct vm_page;\n");
265	}
266	printf("\n#ifndef _KERNEL\n#include <stdbool.h>\n#endif\n");
267	if (rump)
268		printf("\n");
269}
270END	{
271	if (!rump) {
272		printf("\n#define VNODE_OPS_COUNT\t%d\n", vop_offset);
273	}
274}
275'"$awk_parser" | sed -e "$anal_retentive"
276
277# End stuff
278echo ''
279echo "#endif /* !_${SYS}VNODE_IF_H_ */"
280}
281do_hfile $out_h ''
282do_hfile $out_rumph 'rump_'
283
284do_cfile () {
285#
286# Redirect stdout to the C file.
287#
288echo "$0: Creating $1" 1>&2
289exec > $1
290rump=$2
291
292# Begin stuff
293echo -n "$warning" | sed -e 's/\$//g;s/@/\$/g;s/ $//'
294echo ""
295echo -n "$copyright"
296echo "
297#include <sys/cdefs.h>
298__KERNEL_RCSID(0, \"\$NetBSD\$\");"
299
300[ ${lockdebug} -ne 0 ] && echo && echo '#include "opt_vnode_lockdebug.h"'
301
302echo '
303#include <sys/param.h>
304#include <sys/mount.h>
305#include <sys/buf.h>
306#include <sys/vnode.h>
307#include <sys/lock.h>'
308[ ! -z "${rump}" ] && echo '#include <rump/rumpvnode_if.h>'		\
309	&& echo '#include "rump_private.h"'
310
311if [ -z "${rump}" ] ; then
312	echo "
313const struct vnodeop_desc vop_default_desc = {"
314echo '	0,
315	"default",
316	0,
317	NULL,
318	VDESC_NO_OFFSET,
319	VDESC_NO_OFFSET,
320	VDESC_NO_OFFSET,
321};
322'
323fi
324
325# Body stuff
326sed -e "$sed_prep" $src | $awk -v rump=${rump} -v lockdebug=${lockdebug} '
327function do_offset(typematch) {
328	for (i=0; i<argc; i++) {
329		if (argtype[i] == typematch) {
330			printf("\tVOPARG_OFFSETOF(struct %s_args, a_%s),\n",
331				args_name, argname[i]);
332			return i;
333		};
334	};
335	print "\tVDESC_NO_OFFSET,";
336	return -1;
337}
338
339function offsets() {
340	# Define offsets array
341	printf("const int %s_vp_offsets[] = {\n", name);
342	for (i=0; i<argc; i++) {
343		if (argtype[i] == "struct vnode *") {
344			printf ("\tVOPARG_OFFSETOF(struct %s_args,a_%s),\n",
345				args_name, argname[i]);
346		}
347	}
348	print "\tVDESC_NO_OFFSET";
349	print "};";
350	# Define F_desc
351	printf("const struct vnodeop_desc %s_desc = {\n", name);
352	# offset
353	printf ("\t%s_DESCOFFSET,\n", toupper(name));
354	# printable name
355	printf ("\t\"%s\",\n", name);
356	# flags
357	printf("\t0");
358	vpnum = 0;
359	for (i=0; i<argc; i++) {
360		if (willrele[i]) {
361			if (willrele[i] == 2) {
362				word = "UNLOCK";
363			} else if (willrele[i] == 3) {
364				word = "PUT";
365			} else {
366				word = "RELE";
367			}
368			printf(" | VDESC_VP%s_WILL%s", vpnum, word);
369			vpnum++;
370		}
371	}
372	print ",";
373	# vp offsets
374	printf ("\t%s_vp_offsets,\n", name);
375	# vpp (if any)
376	do_offset("struct vnode **");
377	# cred (if any)
378	do_offset("kauth_cred_t");
379	# componentname
380	do_offset("struct componentname *");
381	printf ("};\n");
382}
383
384function bodyrump() {
385	printf("{\n\tint error;\n\n");
386	printf("\trump_schedule();\n");
387	printf("\terror = %s(", toupper(name));
388	for (i=0; i<argc; i++) {
389		printf("%s", argname[i]);
390		if (i < (argc-1)) printf(", ");
391	}
392	printf(");\n");
393	printf("\trump_unschedule();\n\n");
394	printf("\treturn error;\n}\n");
395}
396
397function bodynorm() {
398	printf("{\n\tint error;\n\tbool mpsafe;\n\tstruct %s_args a;\n",
399		args_name);
400	if (lockdebug) {
401		printf("#ifdef VNODE_LOCKDEBUG\n");
402		for (i=0; i<argc; i++) {
403			if (lockstate[i] != -1)
404				printf("\tint islocked_%s;\n", argname[i]);
405		}
406		printf("#endif\n");
407	}
408	printf("\ta.a_desc = VDESC(%s);\n", name);
409	for (i=0; i<argc; i++) {
410		printf("\ta.a_%s = %s;\n", argname[i], argname[i]);
411		if (lockdebug && lockstate[i] != -1) {
412			printf("#ifdef VNODE_LOCKDEBUG\n");
413			printf("\tislocked_%s = (%s->v_vflag & VV_LOCKSWORK) ? (VOP_ISLOCKED(%s) == LK_EXCLUSIVE) : %d;\n",
414			    argname[i], argname[i], argname[i], lockstate[i]);
415			printf("\tif (islocked_%s != %d)\n", argname[i],
416			    lockstate[i]);
417			printf("\t\tpanic(\"%s: %s: locked %%d, expected %%d\", islocked_%s, %d);\n", name, argname[i], argname[i], lockstate[i]);
418			printf("#endif\n");
419		}
420	}
421	printf("\tmpsafe = (%s->v_vflag & VV_MPSAFE);\n", argname[0]);
422	printf("\tif (!mpsafe) { KERNEL_LOCK(1, curlwp); }\n");
423	printf("\terror = (VCALL(%s, VOFFSET(%s), &a));\n",
424		argname[0], name);
425	printf("\tif (!mpsafe) { KERNEL_UNLOCK_ONE(curlwp); }\n");
426	if (willmake != -1) {
427		printf("#ifdef DIAGNOSTIC\n");
428		printf("\tif (error == 0)\n"				\
429		    "\t\tKASSERT((*%s)->v_size != VSIZENOTSET\n"	\
430		    "\t\t    && (*%s)->v_writesize != VSIZENOTSET);\n",
431		    argname[willmake], argname[willmake]);
432		printf("#endif /* DIAGNOSTIC */\n");
433	}
434	printf("\treturn error;\n}\n");
435}
436
437function doit() {
438	printf("\n");
439	if (!rump)
440		offsets();
441
442	if (rump)
443		extname = "RUMP_" toupper(name);
444	else
445		extname = toupper(name);
446
447	# Define function.
448	printf("int\n%s(", extname);
449	for (i=0; i<argc; i++) {
450		printf("%s %s", argtype[i], argname[i]);
451		if (i < (argc-1)) printf(",\n    ");
452	}
453	printf(")\n");
454
455	if (rump)
456		bodyrump();
457	else
458		bodynorm();
459}
460BEGIN	{
461	# start from 1 (vop_default is at 0)
462	argc=1;
463}
464'"$awk_parser" | sed -e "$anal_retentive"
465
466# End stuff
467[ -n "${rump}" ] && return
468
469# Add the vfs_op_descs array to the C file.
470# Begin stuff
471echo "
472const struct vnodeop_desc * const ${rump}vfs_op_descs[] = {
473	&${rump}vop_default_desc,	/* MUST BE FIRST */
474"
475
476# Body stuff
477sed -e "$sed_prep" $src | $awk -v rump=${rump} '
478function doit() {
479	printf("\t&%s_desc,\n", name);
480}
481'"$awk_parser"
482
483# End stuff
484echo '	NULL
485};'
486}
487do_cfile $out_c ''
488do_cfile $out_rumpc 'rump_'
489
490exit 0
491
492# Local Variables:
493# tab-width: 4
494# End:
495