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