]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - sys/tools/vnode_if.awk
bhnd(9): Fix a few mandoc related issues
[FreeBSD/FreeBSD.git] / sys / tools / vnode_if.awk
1 #!/usr/bin/awk -f
2
3 #-
4 # SPDX-License-Identifier: BSD-3-Clause
5 #
6 # Copyright (c) 1992, 1993
7 #       The Regents of the University of California.  All rights reserved.
8 #
9 # Redistribution and use in source and binary forms, with or without
10 # modification, are permitted provided that the following conditions
11 # are met:
12 # 1. Redistributions of source code must retain the above copyright
13 #    notice, this list of conditions and the following disclaimer.
14 # 2. Redistributions in binary form must reproduce the above copyright
15 #    notice, this list of conditions and the following disclaimer in the
16 #    documentation and/or other materials provided with the distribution.
17 # 3. Neither the name of the University nor the names of its contributors
18 #    may be used to endorse or promote products derived from this software
19 #    without specific prior written permission.
20 #
21 # THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22 # ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 # IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 # ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25 # FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 # DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27 # OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28 # HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29 # LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30 # OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31 # SUCH DAMAGE.
32
33 #
34 #       @(#)vnode_if.sh 8.1 (Berkeley) 6/10/93
35 # $FreeBSD$
36 #
37 # Script to produce VFS front-end sugar.
38 #
39 # usage: vnode_if.awk <srcfile> [-c | -h | -p | -q]
40 #       (where <srcfile> is currently /sys/kern/vnode_if.src)
41 #       The source file must have a .src extension
42 #
43
44 function usage()
45 {
46         print "usage: vnode_if.awk <srcfile> [-c|-h|-p|-q]";
47         exit 1;
48 }
49
50 function die(msg, what)
51 {
52         printf srcfile "(" fnr "): " > "/dev/stderr";
53         printf msg "\n", what > "/dev/stderr";
54         exit 1;
55 }
56
57 function t_spc(type)
58 {
59         # Append a space if the type is not a pointer
60         return (type ~ /\*$/) ? type : type " ";
61 }
62
63 # These are just for convenience ...
64 function printc(s) {print s > cfile;}
65 function printh(s) {print s > hfile;}
66 function printp(s) {print s > pfile;}
67 function printq(s) {print s > qfile;}
68
69 function add_debug_code(name, arg, pos, ind)
70 {
71         if (arg == "vpp")
72                 star = "*";
73         else
74                 star = "";
75         if (lockdata[name, arg, pos] && (lockdata[name, arg, pos] != "-")) {
76                 printc(ind"ASSERT_VI_UNLOCKED("star"a->a_"arg", \""uname"\");");
77                 # Add assertions for locking
78                 if (lockdata[name, arg, pos] == "L")
79                         printc(ind"ASSERT_VOP_LOCKED(" star "a->a_"arg", \""uname"\");");
80                 else if (lockdata[name, arg, pos] == "U")
81                         printc(ind"ASSERT_VOP_UNLOCKED(" star "a->a_"arg", \""uname"\");");
82                 else if (lockdata[name, arg, pos] == "E")
83                         printc(ind"ASSERT_VOP_ELOCKED(" star "a->a_"arg", \""uname"\");");
84                 else if (0) {
85                         # XXX More checks!
86                 }
87         }
88 }
89
90 function add_debugpre(name)
91 {
92         if (lockdata[name, "debugpre"]) {
93                 printc("#ifdef DEBUG_VFS_LOCKS");
94                 printc("\t"lockdata[name, "debugpre"]"(a);");
95                 printc("#endif");
96         }
97 }
98
99 function add_debugpost(name)
100 {
101         if (lockdata[name, "debugpost"]) {
102                 printc("#ifdef DEBUG_VFS_LOCKS");
103                 printc("\t"lockdata[name, "debugpost"]"(a, rc);");
104                 printc("#endif");
105         }
106 }
107
108 function add_pre(name)
109 {
110         if (lockdata[name, "pre"]) {
111                 printc("\t"lockdata[name, "pre"]"(a);");
112         }
113 }
114
115 function add_post(name)
116 {
117         if (lockdata[name, "post"]) {
118                 printc("\t"lockdata[name, "post"]"(a, rc);");
119         }
120 }
121
122 function can_inline(name)
123 {
124         if (lockdata[name, "pre"])
125                 return 0;
126         if (lockdata[name, "post"])
127                 return 0;
128         return 1;
129 }
130
131 function find_arg_with_type (type)
132 {
133         for (jj = 0; jj < numargs; jj++) {
134                 if (types[jj] == type) {
135                         return "VOPARG_OFFSETOF(struct " \
136                             name "_args,a_" args[jj] ")";
137                 }
138         }
139
140         return "VDESC_NO_OFFSET";
141 }
142
143 BEGIN{
144
145 # Process the command line
146 for (i = 1; i < ARGC; i++) {
147         arg = ARGV[i];
148         if (arg !~ /^-[chpq]+$/ && arg !~ /\.src$/)
149                 usage();
150         if (arg ~ /^-.*c/)
151                 cfile = "vnode_if.c";
152         if (arg ~ /^-.*h/)
153                 hfile = "vnode_if.h";
154         if (arg ~ /^-.*p/)
155                 pfile = "vnode_if_newproto.h";
156         if (arg ~ /^-.*q/)
157                 qfile = "vnode_if_typedef.h";
158         if (arg ~ /\.src$/)
159                 srcfile = arg;
160 }
161 ARGC = 1;
162
163 if (!cfile && !hfile && !pfile && !qfile)
164         exit 0;
165
166 if (!srcfile)
167         usage();
168
169 # Avoid a literal generated file tag here.
170 generated = "@" "generated";
171
172 common_head = \
173     "/*\n" \
174     " * This file is " generated " automatically.\n" \
175     " * Do not modify anything in here by hand.\n" \
176     " *\n" \
177     " * Created from $FreeBSD$\n" \
178     " */\n" \
179     "\n";
180
181 if (pfile) {
182         printp(common_head)
183         printp("struct vop_vector {")
184         printp("\tstruct vop_vector\t*vop_default;")
185         printp("\tvop_bypass_t\t*vop_bypass;")
186 }
187
188 if (qfile) {
189         printq(common_head)
190 }
191
192 if (hfile) {
193         printh(common_head "extern struct vnodeop_desc vop_default_desc;");
194         printh("#include \"vnode_if_typedef.h\"")
195         printh("#include \"vnode_if_newproto.h\"")
196 }
197
198 if (cfile) {
199         printc(common_head \
200             "#include <sys/param.h>\n" \
201             "#include <sys/event.h>\n" \
202             "#include <sys/kernel.h>\n" \
203             "#include <sys/mount.h>\n" \
204             "#include <sys/sdt.h>\n" \
205             "#include <sys/signalvar.h>\n" \
206             "#include <sys/systm.h>\n" \
207             "#include <sys/vnode.h>\n" \
208             "\n" \
209             "SDT_PROVIDER_DECLARE(vfs);\n" \
210             "\n" \
211             "struct vnodeop_desc vop_default_desc = {\n" \
212             "   \"default\",\n" \
213             "   0,\n" \
214             "   0,\n" \
215             "   (vop_bypass_t *)vop_panic,\n" \
216             "   NULL,\n" \
217             "   VDESC_NO_OFFSET,\n" \
218             "   VDESC_NO_OFFSET,\n" \
219             "   VDESC_NO_OFFSET,\n" \
220             "   VDESC_NO_OFFSET,\n" \
221             "};\n");
222 }
223
224 while ((getline < srcfile) > 0) {
225         fnr++;
226         if (NF == 0)
227                 continue;
228         if ($1 ~ /^%%/) {
229                 if (NF != 6 ||
230                     $2 !~ /^[a-z_]+$/  ||  $3 !~ /^[a-z]+$/  ||
231                     $4 !~ /^.$/  ||  $5 !~ /^.$/  ||  $6 !~ /^.$/) {
232                         die("Invalid %s construction", "%%");
233                         continue;
234                 }
235                 lockdata["vop_" $2, $3, "Entry"] = $4;
236                 lockdata["vop_" $2, $3, "OK"]    = $5;
237                 lockdata["vop_" $2, $3, "Error"] = $6;                  
238                 continue;
239         }
240
241         if ($1 ~ /^%!/) {
242                 if (NF != 4 ||
243                     ($3 != "pre" && $3 != "post" &&
244                      $3 != "debugpre" && $3 != "debugpost")) {
245                         die("Invalid %s construction", "%!");
246                         continue;
247                 }
248                 lockdata["vop_" $2, $3] = $4;
249                 continue;
250         }
251         if ($1 ~ /^#/)
252                 continue;
253
254         # Get the function name.
255         name = $1;
256         uname = toupper(name);
257
258         # Get the function arguments.
259         for (numargs = 0; ; ++numargs) {
260                 if ((getline < srcfile) <= 0) {
261                         die("Unable to read through the arguments for \"%s\"",
262                             name);
263                 }
264                 fnr++;
265                 if ($1 ~ /^\};/)
266                         break;
267
268                 # Delete comments, if any.
269                 gsub (/\/\*.*\*\//, "");
270
271                 # Condense whitespace and delete leading/trailing space.
272                 gsub(/[[:space:]]+/, " ");
273                 sub(/^ /, "");
274                 sub(/ $/, "");
275
276                 # Pick off direction.
277                 if ($1 != "INOUT" && $1 != "IN" && $1 != "OUT")
278                         die("No IN/OUT direction for \"%s\".", $0);
279                 dirs[numargs] = $1;
280                 sub(/^[A-Z]* /, "");
281
282                 if ((reles[numargs] = $1) == "WILLRELE")
283                         sub(/^[A-Z]* /, "");
284                 else
285                         reles[numargs] = "WONTRELE";
286
287                 # kill trailing ;
288                 if (sub(/;$/, "") < 1)
289                         die("Missing end-of-line ; in \"%s\".", $0);
290
291                 # pick off variable name
292                 if ((argp = match($0, /[A-Za-z0-9_]+$/)) < 1)
293                         die("Missing var name \"a_foo\" in \"%s\".", $0);
294                 args[numargs] = substr($0, argp);
295                 $0 = substr($0, 1, argp - 1);
296
297                 # what is left must be type
298                 # remove trailing space (if any)
299                 sub(/ $/, "");
300                 types[numargs] = $0;
301         }
302         if (numargs > 4)
303                 ctrargs = 4;
304         else
305                 ctrargs = numargs;
306         ctrstr = ctrargs "(KTR_VOP, \"VOP\", \"" uname "\", (uintptr_t)a,\n\t    "; 
307         ctrstr = ctrstr "\"" args[0] ":0x%jX\", (uintptr_t)a->a_" args[0];
308         for (i = 1; i < ctrargs; ++i)
309                 ctrstr = ctrstr ", \"" args[i] ":0x%jX\", a->a_" args[i];
310         ctrstr = ctrstr ");";
311
312         if (pfile) {
313                 printp("\t"name"_t\t*"name";")
314         }
315         if (qfile) {
316                 printq("struct "name"_args;")
317                 printq("typedef int "name"_t(struct "name"_args *);\n")
318         }
319
320         if (hfile) {
321                 # Print out the vop_F_args structure.
322                 printh("struct "name"_args {\n\tstruct vop_generic_args a_gen;");
323                 for (i = 0; i < numargs; ++i)
324                         printh("\t" t_spc(types[i]) "a_" args[i] ";");
325                 printh("};");
326                 printh("");
327
328                 # Print out extern declaration.
329                 printh("extern struct vnodeop_desc " name "_desc;");
330                 printh("");
331
332                 # Print out function prototypes.
333                 printh("int " uname "_AP(struct " name "_args *);");
334                 printh("int " uname "_APV(struct vop_vector *vop, struct " name "_args *);");
335                 printh("");
336                 printh("static __inline int " uname "(");
337                 for (i = 0; i < numargs; ++i) {
338                         printh("\t" t_spc(types[i]) args[i] \
339                             (i < numargs - 1 ? "," : ")"));
340                 }
341                 printh("{");
342                 printh("\tstruct " name "_args a;");
343                 printh("");
344                 printh("\ta.a_gen.a_desc = &" name "_desc;");
345                 for (i = 0; i < numargs; ++i)
346                         printh("\ta.a_" args[i] " = " args[i] ";");
347                 if (can_inline(name)) {
348                         printh("\n#if !defined(DEBUG_VFS_LOCKS) && !defined(INVARIANTS) && !defined(KTR)");
349                         printh("\tif (!SDT_PROBES_ENABLED())");
350                         printh("\t\treturn (" args[0]"->v_op->"name"(&a));");
351                         printh("\telse");
352                         printh("\t\treturn (" uname "_APV("args[0]"->v_op, &a));");
353                         printh("#else");
354                 }
355                 printh("\treturn (" uname "_APV("args[0]"->v_op, &a));");
356                 if (can_inline(name))
357                         printh("#endif");
358
359                 printh("}");
360
361                 printh("");
362         }
363
364         if (cfile) {
365                 funcarr[name] = 1;
366                 # Print out the vop_F_vp_offsets structure.  This all depends
367                 # on naming conventions and nothing else.
368                 printc("static int " name "_vp_offsets[] = {");
369                 # as a side effect, figure out the releflags
370                 releflags = "";
371                 vpnum = 0;
372                 for (i = 0; i < numargs; i++) {
373                         if (types[i] == "struct vnode *") {
374                                 printc("\tVOPARG_OFFSETOF(struct " name \
375                                     "_args,a_" args[i] "),");
376                                 if (reles[i] == "WILLRELE") {
377                                         releflags = releflags \
378                                             "|VDESC_VP" vpnum "_WILLRELE";
379                                 }
380                                 vpnum++;
381                         }
382                 }
383
384                 sub(/^\|/, "", releflags);
385                 printc("\tVDESC_NO_OFFSET");
386                 printc("};");
387
388                 printc("\n");
389                 printc("SDT_PROBE_DEFINE2(vfs, vop, " name ", entry, \"struct vnode *\", \"struct " name "_args *\");\n");
390                 printc("SDT_PROBE_DEFINE3(vfs, vop, " name ", return, \"struct vnode *\", \"struct " name "_args *\", \"int\");\n");
391
392                 # Print out function.
393                 printc("\nint\n" uname "_AP(struct " name "_args *a)");
394                 printc("{");
395                 printc("");
396                 printc("\treturn(" uname "_APV(a->a_" args[0] "->v_op, a));");
397                 printc("}");
398                 printc("\nint\n" uname "_APV(struct vop_vector *vop, struct " name "_args *a)");
399                 printc("{");
400                 printc("\tint rc;");
401                 printc("");
402                 printc("\tVNASSERT(a->a_gen.a_desc == &" name "_desc, a->a_" args[0]",");
403                 printc("\t    (\"Wrong a_desc in " name "(%p, %p)\", a->a_" args[0]", a));");
404                 printc("\tVNASSERT(vop != NULL, a->a_" args[0]", (\"No "name"(%p, %p)\", a->a_" args[0]", a));")
405                 printc("\tKTR_START" ctrstr);
406                 add_debugpre(name);
407                 add_pre(name);
408                 for (i = 0; i < numargs; ++i)
409                         add_debug_code(name, args[i], "Entry", "\t");
410                 printc("\tif (!SDT_PROBES_ENABLED()) {");
411                 printc("\t\trc = vop->"name"(a);")
412                 printc("\t} else {")
413                 printc("\t\tSDT_PROBE2(vfs, vop, " name ", entry, a->a_" args[0] ", a);");
414                 printc("\t\trc = vop->"name"(a);")
415                 printc("\t\tSDT_PROBE3(vfs, vop, " name ", return, a->a_" args[0] ", a, rc);");
416                 printc("\t}")
417                 printc("\tif (rc == 0) {");
418                 for (i = 0; i < numargs; ++i)
419                         add_debug_code(name, args[i], "OK", "\t\t");
420                 printc("\t} else {");
421                 for (i = 0; i < numargs; ++i)
422                         add_debug_code(name, args[i], "Error", "\t\t");
423                 printc("\t}");
424                 add_post(name);
425                 add_debugpost(name);
426                 printc("\tKTR_STOP" ctrstr);
427                 printc("\treturn (rc);");
428                 printc("}\n");
429
430                 # Print out the vnodeop_desc structure.
431                 printc("struct vnodeop_desc " name "_desc = {");
432                 # printable name
433                 printc("\t\"" name "\",");
434                 # flags
435                 vppwillrele = "";
436                 for (i = 0; i < numargs; i++) {
437                         if (types[i] == "struct vnode **" && \
438                             reles[i] == "WILLRELE") {
439                                 vppwillrele = "|VDESC_VPP_WILLRELE";
440                         }
441                 }
442
443                 if (!releflags)
444                         releflags = "0";
445                 printc("\t" releflags vppwillrele ",");
446
447                 # index in struct vop_vector
448                 printc("\t__offsetof(struct vop_vector, " name "),");
449                 # function to call
450                 printc("\t(vop_bypass_t *)" uname "_AP,");
451                 # vp offsets
452                 printc("\t" name "_vp_offsets,");
453                 # vpp (if any)
454                 printc("\t" find_arg_with_type("struct vnode **") ",");
455                 # cred (if any)
456                 printc("\t" find_arg_with_type("struct ucred *") ",");
457                 # thread (if any)
458                 printc("\t" find_arg_with_type("struct thread *") ",");
459                 # componentname
460                 printc("\t" find_arg_with_type("struct componentname *") ",");
461                 # transport layer information
462                 printc("};\n");
463         }
464 }
465
466 if (cfile) {
467         printc("void");
468         printc("vfs_vector_op_register(struct vop_vector *orig_vop)");
469         printc("{");
470         printc("\tstruct vop_vector *vop;");
471         printc("");
472         printc("\tif (orig_vop->registered)");
473         printc("\t\tpanic(\"%s: vop_vector %p already registered\",")
474         printc("\t\t    __func__, orig_vop);");
475         printc("");
476         for (name in funcarr) {
477                 printc("\tvop = orig_vop;");
478                 printc("\twhile (vop != NULL && \\");
479                 printc("\t    vop->"name" == NULL && vop->vop_bypass == NULL)")
480                 printc("\t\tvop = vop->vop_default;")
481                 printc("\tif (vop != NULL)");
482                 printc("\t\torig_vop->"name" = vop->"name";");
483                 printc("");
484         }
485         printc("\tvop = orig_vop;");
486         printc("\twhile (vop != NULL && vop->vop_bypass == NULL)")
487         printc("\t\tvop = vop->vop_default;")
488         printc("\tif (vop != NULL)");
489         printc("\t\torig_vop->vop_bypass = vop->vop_bypass;");
490         printc("");
491         for (name in funcarr) {
492                 printc("\tif (orig_vop->"name" == NULL)");
493                 printc("\t\torig_vop->"name" = (void *)orig_vop->vop_bypass;");
494         }
495         printc("");
496         printc("\torig_vop->registered = true;");
497         printc("}")
498 }
499
500 if (hfile) {
501         printh("void vfs_vector_op_register(struct vop_vector *orig_vop);");
502 }
503
504 if (pfile) {
505         printp("\tbool\tregistered;")
506         printp("};")
507 }
508  
509 if (hfile)
510         close(hfile);
511 if (cfile)
512         close(cfile);
513 if (pfile)
514         close(pfile);
515 close(srcfile);
516
517 exit 0;
518
519 }