Home | History | Annotate | Line # | Download | only in kern
vnode_if.sh revision 1.70
      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.70 2020/05/16 18:31:50 christos 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="";
    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=") {
    117 		fstrans = $1;
    118 		sub("FSTRANS=", "", fstrans);
    119 		next;
    120 	}
    121 
    122 	argdir[argc] = $1; i=2;
    123 
    124 	if ($2 == "LOCKED=YES") {
    125 		lockstate[argc] = 1;
    126 		i++;
    127 	} else if ($2 == "LOCKED=NO") {
    128 		lockstate[argc] = 0;
    129 		i++;
    130 	} else
    131 		lockstate[argc] = -1;
    132 
    133 	if ($2 == "WILLRELE" ||
    134 	    $3 == "WILLRELE") {
    135 		willrele[argc] = 1;
    136 		i++;
    137 	} else if ($2 == "WILLPUT" ||
    138 		   $3 == "WILLPUT") {
    139 		willrele[argc] = 3;
    140 		i++;
    141 	} else
    142 		willrele[argc] = 0;
    143 
    144 	if ($2 == "WILLMAKE") {
    145 		willmake=argc;
    146 		i++;
    147 	}
    148 	if (argc == 0 && fstrans == "") {
    149 		if (lockstate[0] == 1)
    150 			fstrans = "NO";
    151 		else
    152 			fstrans = "YES";
    153 	}
    154 
    155 	# XXX: replace non-portable types for rump.  We should really
    156 	# nuke the types from the kernel, but that is a battle for
    157 	# another day.
    158 	at = $i;
    159 	if (rump) {
    160 		if (at == "vm_prot_t")
    161 			at = "int";
    162 		if (at == "voff_t")
    163 			at = "off_t";
    164 		if (at == "kauth_cred_t")
    165 			at = "struct kauth_cred *"
    166 		if (at == "daddr_t")
    167 			at = "int64_t"
    168 	}
    169 	argtype[argc] = at;
    170 	i++;
    171 	while (i < NF) {
    172 		argtype[argc] = argtype[argc]" "$i;
    173 		i++;
    174 	}
    175 	argname[argc] = $i;
    176 	argc++;
    177 	next;
    178 }
    179 '
    180 
    181 # This is put before the copyright on each generated file.
    182 warning="\
    183 /*	@NetBSD@	*/
    184 
    185 /*
    186  * Warning: DO NOT EDIT! This file is automatically generated!
    187  * (Modifications made here may easily be lost!)
    188  *
    189  * Created from the file:
    190  *	${SRC_ID}
    191  * by the script:
    192  *	${SCRIPT_ID}
    193  */
    194 "
    195 
    196 # This is to satisfy McKusick (get rid of evil spaces 8^)
    197 anal_retentive='s:\([^/]\*\) :\1:g'
    198 
    199 do_hfile () {
    200 #
    201 # Redirect stdout to the H file.
    202 #
    203 echo "$0: Creating $1" 1>&2
    204 exec > $1
    205 rump=$2
    206 
    207 # Begin stuff
    208 if [ -z "${rump}" ]; then
    209 	SYS='SYS_'
    210 else
    211 	SYS='RUMP_RUMP'
    212 fi
    213 echo -n "$warning" | sed -e 's/\$//g;s/@/\$/g;s/ $//'
    214 echo ""
    215 echo -n "$copyright"
    216 echo ''
    217 echo "#ifndef _${SYS}VNODE_IF_H_"
    218 echo "#define _${SYS}VNODE_IF_H_"
    219 if [ ${lockdebug} -ne 0 ] ; then
    220 	echo ''
    221 	echo '#ifdef _KERNEL_OPT'
    222 	echo '#include "opt_vnode_lockdebug.h"'
    223 	echo '#endif /* _KERNEL_OPT */'
    224 fi
    225 [ -z "${rump}" ] && echo "
    226 extern const struct vnodeop_desc ${rump}vop_default_desc;"
    227 echo
    228 
    229 # Body stuff
    230 # This awk program needs toupper() so define it if necessary.
    231 sed -e "$sed_prep" $src | $awk -v rump=${rump} "$toupper"'
    232 function doit() {
    233 	name = rump name
    234 	# Declare arg struct, descriptor.
    235 	if (!rump) {
    236 		printf("\n#define %s_DESCOFFSET %d\n",
    237 		    toupper(name), vop_offset++);
    238 		printf("struct %s_args {\n", args_name);
    239 		printf("\tconst struct vnodeop_desc * a_desc;\n");
    240 		for (i=0; i<argc; i++) {
    241 			printf("\t%s a_%s;\n", argtype[i], argname[i]);
    242 		}
    243 		printf("};\n");
    244 		printf("extern const struct vnodeop_desc %s_desc;\n", name);
    245 	}
    246 	# Prototype it.
    247 	protoarg = sprintf("int %s(", toupper(name));
    248 	protolen = length(protoarg);
    249 	printf("%s", protoarg);
    250 	for (i=0; i<argc; i++) {
    251 		protoarg = sprintf("%s", argtype[i]);
    252 		if (i < (argc-1)) protoarg = (protoarg ", ");
    253 		arglen = length(protoarg);
    254 		if ((protolen + arglen) > 77) {
    255 			protoarg = ("\n    " protoarg);
    256 			arglen += 4;
    257 			protolen = 0;
    258 		}
    259 		printf("%s", protoarg);
    260 		protolen += arglen;
    261 	}
    262 	printf(");\n");
    263 }
    264 BEGIN	{
    265 	vop_offset = 1; # start at 1, to count the 'default' op
    266 
    267 	printf("struct buf;\n");
    268 	if (rump) {
    269 		printf("struct flock;\n");
    270 		printf("struct knote;\n");
    271 		printf("struct vm_page;\n");
    272 		printf("struct acl;\n");
    273 		printf("\n#include <sys/acl.h>\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 enum fst_op { FST_NO, FST_YES, FST_LAZY, FST_TRY };
    324 
    325 static inline int
    326 vop_pre(vnode_t *vp, struct mount **mp, bool *mpsafe, enum fst_op op)
    327 {
    328 	int error;
    329 
    330 	*mpsafe = (vp->v_vflag & VV_MPSAFE);
    331 
    332 	if (!*mpsafe) {
    333 		KERNEL_LOCK(1, curlwp);
    334 	}
    335 
    336 	if (op == FST_YES || op == FST_LAZY || op == FST_TRY) {
    337 		for (;;) {
    338 			*mp = vp->v_mount;
    339 			if (op == FST_TRY) {
    340 				error = fstrans_start_nowait(*mp);
    341 				if (error) {
    342 					if (!*mpsafe) {
    343 						KERNEL_UNLOCK_ONE(curlwp);
    344 					}
    345 					return error;
    346 				}
    347 			} else if (op == FST_LAZY) {
    348 				fstrans_start_lazy(*mp);
    349 			} else {
    350 				fstrans_start(*mp);
    351 			}
    352 			if (__predict_true(*mp == vp->v_mount))
    353 				break;
    354 			fstrans_done(*mp);
    355 		}
    356 	} else {
    357 		*mp = vp->v_mount;
    358 	}
    359 
    360 	return 0;
    361 }
    362 
    363 static inline void
    364 vop_post(vnode_t *vp, struct mount *mp, bool mpsafe, enum fst_op op)
    365 {
    366 
    367 	if (op == FST_YES || op == FST_LAZY) {
    368 		fstrans_done(mp);
    369 	}
    370 
    371 	if (!mpsafe) {
    372 		KERNEL_UNLOCK_ONE(curlwp);
    373 	}
    374 }
    375 
    376 const struct vnodeop_desc vop_default_desc = {"
    377 echo '	0,
    378 	"default",
    379 	0,
    380 	NULL,
    381 	VDESC_NO_OFFSET,
    382 	VDESC_NO_OFFSET,
    383 	VDESC_NO_OFFSET,
    384 };
    385 '
    386 fi
    387 
    388 # Body stuff
    389 sed -e "$sed_prep" $src | $awk -v rump=${rump} -v lockdebug=${lockdebug} '
    390 function do_offset(typematch) {
    391 	for (i=0; i<argc; i++) {
    392 		if (argtype[i] == typematch) {
    393 			printf("\tVOPARG_OFFSETOF(struct %s_args, a_%s),\n",
    394 				args_name, argname[i]);
    395 			return i;
    396 		};
    397 	};
    398 	print "\tVDESC_NO_OFFSET,";
    399 	return -1;
    400 }
    401 
    402 function offsets() {
    403 	# Define offsets array
    404 	printf("const int %s_vp_offsets[] = {\n", name);
    405 	for (i=0; i<argc; i++) {
    406 		if (argtype[i] == "struct vnode *") {
    407 			printf ("\tVOPARG_OFFSETOF(struct %s_args,a_%s),\n",
    408 				args_name, argname[i]);
    409 		}
    410 	}
    411 	print "\tVDESC_NO_OFFSET";
    412 	print "};";
    413 	# Define F_desc
    414 	printf("const struct vnodeop_desc %s_desc = {\n", name);
    415 	# offset
    416 	printf ("\t%s_DESCOFFSET,\n", toupper(name));
    417 	# printable name
    418 	printf ("\t\"%s\",\n", name);
    419 	# flags
    420 	printf("\t0");
    421 	vpnum = 0;
    422 	for (i=0; i<argc; i++) {
    423 		if (willrele[i]) {
    424 			if (willrele[i] == 3) {
    425 				word = "PUT";
    426 			} else {
    427 				word = "RELE";
    428 			}
    429 			printf(" | VDESC_VP%s_WILL%s", vpnum, word);
    430 		}
    431 		if (argtype[i] == "struct vnode *")
    432 			vpnum++;
    433 	}
    434 	print ",";
    435 	# vp offsets
    436 	printf ("\t%s_vp_offsets,\n", name);
    437 	# vpp (if any)
    438 	do_offset("struct vnode **");
    439 	# cred (if any)
    440 	do_offset("kauth_cred_t");
    441 	# componentname
    442 	do_offset("struct componentname *");
    443 	printf ("};\n");
    444 }
    445 
    446 function bodyrump() {
    447 	printf("{\n\tint error;\n\n");
    448 	printf("\trump_schedule();\n");
    449 	printf("\terror = %s(", toupper(name));
    450 	for (i=0; i<argc; i++) {
    451 		printf("%s", argname[i]);
    452 		if (i < (argc-1)) printf(", ");
    453 	}
    454 	printf(");\n");
    455 	printf("\trump_unschedule();\n\n");
    456 	printf("\treturn error;\n}\n");
    457 }
    458 
    459 function bodynorm() {
    460 	printf("{\n\tint error;\n\tbool mpsafe;\n\tstruct %s_args a;\n",
    461 		args_name);
    462 	printf("\tstruct mount *mp;\n");
    463 	if (lockdebug) {
    464 		printf("#ifdef VNODE_LOCKDEBUG\n");
    465 		for (i=0; i<argc; i++) {
    466 			if (lockstate[i] != -1)
    467 				printf("\tint islocked_%s;\n", argname[i]);
    468 		}
    469 		printf("#endif\n");
    470 	}
    471 	printf("\ta.a_desc = VDESC(%s);\n", name);
    472 	for (i=0; i<argc; i++) {
    473 		printf("\ta.a_%s = %s;\n", argname[i], argname[i]);
    474 		if (lockdebug && lockstate[i] != -1) {
    475 			printf("#ifdef VNODE_LOCKDEBUG\n");
    476 			printf("\tislocked_%s = (%s->v_vflag & VV_LOCKSWORK) ? (VOP_ISLOCKED(%s) == LK_EXCLUSIVE) : %d;\n",
    477 			    argname[i], argname[i], argname[i], lockstate[i]);
    478 			printf("\tif (islocked_%s != %d)\n", argname[i],
    479 			    lockstate[i]);
    480 			printf("\t\tpanic(\"%s: %s: locked %%d, expected %%d\", islocked_%s, %d);\n", name, argname[i], argname[i], lockstate[i]);
    481 			printf("#endif\n");
    482 		}
    483 	}
    484 	if (fstrans == "LOCK")
    485 		printf("\terror = vop_pre(%s, &mp, &mpsafe, %s);\n",
    486 			argname[0], "(!(flags & (LK_SHARED|LK_EXCLUSIVE)) ? FST_NO : (flags & LK_NOWAIT ? FST_TRY : FST_YES))");
    487 	else if (fstrans == "UNLOCK")
    488 		printf("\terror = vop_pre(%s, &mp, &mpsafe, FST_%s);\n",
    489 			argname[0], "NO");
    490 	else
    491 		printf("\terror = vop_pre(%s, &mp, &mpsafe, FST_%s);\n",
    492 			argname[0], fstrans);
    493 	printf("\tif (error)\n\t\treturn error;\n");
    494 	printf("\terror = (VCALL(%s, VOFFSET(%s), &a));\n",
    495 		argname[0], name);
    496 	if (fstrans == "LOCK")
    497 		printf("\tvop_post(%s, mp, mpsafe, %s);\n",
    498 			argname[0], "(flags & (LK_UPGRADE|LK_DOWNGRADE) ? FST_NO : (error ? FST_YES : FST_NO))");
    499 	else if (fstrans == "UNLOCK")
    500 		printf("\tvop_post(%s, mp, mpsafe, FST_%s);\n",
    501 			argname[0], "YES");
    502 	else
    503 		printf("\tvop_post(%s, mp, mpsafe, FST_%s);\n",
    504 			argname[0], fstrans);
    505 	if (willmake != -1) {
    506 		printf("#ifdef DIAGNOSTIC\n");
    507 		printf("\tif (error == 0)\n"				\
    508 		    "\t\tKASSERT((*%s)->v_size != VSIZENOTSET\n"	\
    509 		    "\t\t    && (*%s)->v_writesize != VSIZENOTSET);\n",
    510 		    argname[willmake], argname[willmake]);
    511 		printf("#endif /* DIAGNOSTIC */\n");
    512 	}
    513 	printf("\treturn error;\n}\n");
    514 }
    515 
    516 function doit() {
    517 	printf("\n");
    518 	if (!rump)
    519 		offsets();
    520 
    521 	if (rump)
    522 		extname = "RUMP_" toupper(name);
    523 	else
    524 		extname = toupper(name);
    525 
    526 	# Define function.
    527 	printf("int\n%s(", extname);
    528 	for (i=0; i<argc; i++) {
    529 		printf("%s %s", argtype[i], argname[i]);
    530 		if (i < (argc-1)) printf(",\n    ");
    531 	}
    532 	printf(")\n");
    533 
    534 	if (rump)
    535 		bodyrump();
    536 	else
    537 		bodynorm();
    538 }
    539 BEGIN	{
    540 	# start from 1 (vop_default is at 0)
    541 	argc=1;
    542 }
    543 '"$awk_parser" | sed -e "$anal_retentive"
    544 
    545 # End stuff
    546 [ -n "${rump}" ] && return
    547 
    548 # Add the vfs_op_descs array to the C file.
    549 # Begin stuff
    550 echo "
    551 const struct vnodeop_desc * const ${rump}vfs_op_descs[] = {
    552 	&${rump}vop_default_desc,	/* MUST BE FIRST */
    553 "
    554 
    555 # Body stuff
    556 sed -e "$sed_prep" $src | $awk -v rump=${rump} '
    557 function doit() {
    558 	printf("\t&%s_desc,\n", name);
    559 }
    560 '"$awk_parser"
    561 
    562 # End stuff
    563 echo '	NULL
    564 };'
    565 }
    566 do_cfile $out_c ''
    567 do_cfile $out_rumpc 'rump_'
    568 
    569 exit 0
    570 
    571 # Local Variables:
    572 # tab-width: 4
    573 # End:
    574