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