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