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