Remove KTR damage.
[dragonfly.git] / sys / tools / vnode_if.awk
1 #!/usr/bin/awk -f
2
3 #
4 # Copyright (c) 1992, 1993
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. All advertising materials mentioning features or use of this software
16 #    must display the following acknowledgement:
17 #       This product includes software developed by the University of
18 #       California, Berkeley and its contributors.
19 # 4. Neither the name of the University nor the names of its contributors
20 #    may be used to endorse or promote products derived from this software
21 #    without specific prior written permission.
22 #
23 # THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
24 # ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25 # IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26 # ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
27 # FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28 # DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29 # OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30 # HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31 # LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32 # OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33 # SUCH DAMAGE.
34 #
35 #       @(#)vnode_if.sh 8.1 (Berkeley) 6/10/93
36 # $FreeBSD: src/sys/tools/vnode_if.awk,v 1.39 2003/06/22 21:20:06 truckman Exp $
37 # $DragonFly: src/sys/tools/Attic/vnode_if.awk,v 1.2 2003/11/22 21:01:50 asmodai Exp $
38 #
39 # Script to produce VFS front-end sugar.
40 #
41 # usage: vnode_if.awk <srcfile> [-c | -h]
42 #       (where <srcfile> is currently /sys/kern/vnode_if.src)
43 #
44
45 function usage()
46 {
47         print "usage: vnode_if.awk <srcfile> [-c|-h]";
48         exit 1;
49 }
50
51 function die(msg, what)
52 {
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
67 function add_debug_code(name, arg, pos)
68 {
69         if (arg == "vpp")
70                 arg = "*vpp";
71         if (lockdata[name, arg, pos] && (lockdata[name, arg, pos] != "-")) {
72                 if (arg ~ /^\*/) {
73                         printh("\tif ("substr(arg, 2)" != NULL) {");
74                 }
75                 printh("\tASSERT_VI_UNLOCKED("arg", \""uname"\");");
76                 # Add assertions for locking
77                 if (lockdata[name, arg, pos] == "L")
78                         printh("\tASSERT_VOP_LOCKED("arg", \""uname"\");");
79                 else if (lockdata[name, arg, pos] == "U")
80                         printh("\tASSERT_VOP_UNLOCKED("arg", \""uname"\");");
81                 else if (0) {
82                         # XXX More checks!
83                 }
84                 if (arg ~ /^\*/) {
85                         printh("\t}");
86                 }
87         }
88 }
89
90 function add_debug_pre(name)
91 {
92         if (lockdata[name, "pre"]) {
93                 printh("#ifdef  DEBUG_VFS_LOCKS");
94                 printh("\t"lockdata[name, "pre"]"(&a);");
95                 printh("#endif");
96         }
97 }
98
99 function add_debug_post(name)
100 {
101         if (lockdata[name, "post"]) {
102                 printh("#ifdef  DEBUG_VFS_LOCKS");
103                 printh("\t"lockdata[name, "post"]"(&a, rc);");
104                 printh("#endif");
105         }
106 }
107
108 function find_arg_with_type (type)
109 {
110         for (jj = 0; jj < numargs; jj++) {
111                 if (types[jj] == type) {
112                         return "VOPARG_OFFSETOF(struct " \
113                             name "_args,a_" args[jj] ")";
114                 }
115         }
116
117         return "VDESC_NO_OFFSET";
118 }
119
120 BEGIN{
121
122 # Process the command line
123 for (i = 1; i < ARGC; i++) {
124         arg = ARGV[i];
125         if (arg !~ /^-[ch]+$/ && arg !~ /\.src$/)
126                 usage();
127         if (arg ~ /^-.*c/)
128                 cfile = "vnode_if.c";
129         if (arg ~ /^-.*h/)
130                 hfile = "vnode_if.h";
131         if (arg ~ /\.src$/)
132                 srcfile = arg;
133 }
134 ARGC = 1;
135
136 if (!cfile && !hfile)
137         exit 0;
138
139 if (!srcfile)
140         usage();
141
142 common_head = \
143     "/*\n" \
144     " * This file is produced automatically.\n" \
145     " * Do not modify anything in here by hand.\n" \
146     " *\n" \
147     " * Created from $FreeBSD$\n" \
148     " */\n" \
149     "\n";
150
151 if (hfile)
152         printh(common_head "extern struct vnodeop_desc vop_default_desc;");
153
154 if (cfile) {
155         printc(common_head \
156             "#include <sys/param.h>\n" \
157             "#include <sys/systm.h>\n" \
158             "#include <sys/vnode.h>\n" \
159             "\n" \
160             "struct vnodeop_desc vop_default_desc = {\n" \
161             "   1,\t\t\t/* special case, vop_default => 1 */\n" \
162             "   \"default\",\n" \
163             "   0,\n" \
164             "   NULL,\n" \
165             "   VDESC_NO_OFFSET,\n" \
166             "   VDESC_NO_OFFSET,\n" \
167             "   VDESC_NO_OFFSET,\n" \
168             "   VDESC_NO_OFFSET,\n" \
169             "   NULL,\n" \
170             "};\n");
171 }
172
173 while ((getline < srcfile) > 0) {
174         if (NF == 0)
175                 continue;
176         if ($1 ~ /^#%/) {
177                 if (NF != 6  ||  $1 != "#%"  || \
178                     $2 !~ /^[a-z]+$/  ||  $3 !~ /^[a-z]+$/  || \
179                     $4 !~ /^.$/  ||  $5 !~ /^.$/  ||  $6 !~ /^.$/)
180                         continue;
181                 if ($3 == "vpp")
182                         $3 = "*vpp";
183                 lockdata["vop_" $2, $3, "Entry"] = $4;
184                 lockdata["vop_" $2, $3, "OK"]    = $5;
185                 lockdata["vop_" $2, $3, "Error"] = $6;                  
186                 continue;
187         }
188
189         if ($1 ~ /^#!/) {
190                 if (NF != 4 || $1 != "#!")
191                         continue;
192                 if ($3 != "pre" && $3 != "post")
193                         continue;
194                 lockdata["vop_" $2, $3] = $4;
195                 continue;
196         }
197         if ($1 ~ /^#/)
198                 continue;
199
200         # Get the function name.
201         name = $1;
202         uname = toupper(name);
203
204         # Get the function arguments.
205         for (numargs = 0; ; ++numargs) {
206                 if ((getline < srcfile) <= 0) {
207                         die("Unable to read through the arguments for \"%s\"",
208                             name);
209                 }
210                 if ($1 ~ /^\};/)
211                         break;
212
213                 # Delete comments, if any.
214                 gsub (/\/\*.*\*\//, "");
215
216                 # Condense whitespace and delete leading/trailing space.
217                 gsub(/[[:space:]]+/, " ");
218                 sub(/^ /, "");
219                 sub(/ $/, "");
220
221                 # Pick off direction.
222                 if ($1 != "INOUT" && $1 != "IN" && $1 != "OUT")
223                         die("No IN/OUT direction for \"%s\".", $0);
224                 dirs[numargs] = $1;
225                 sub(/^[A-Z]* /, "");
226
227                 if ((reles[numargs] = $1) == "WILLRELE")
228                         sub(/^[A-Z]* /, "");
229                 else
230                         reles[numargs] = "WONTRELE";
231
232                 # kill trailing ;
233                 if (sub(/;$/, "") < 1)
234                         die("Missing end-of-line ; in \"%s\".", $0);
235
236                 # pick off variable name
237                 if ((argp = match($0, /[A-Za-z0-9_]+$/)) < 1)
238                         die("Missing var name \"a_foo\" in \"%s\".", $0);
239                 args[numargs] = substr($0, argp);
240                 $0 = substr($0, 1, argp - 1);
241
242                 # what is left must be type
243                 # remove trailing space (if any)
244                 sub(/ $/, "");
245                 types[numargs] = $0;
246
247         }
248
249         if (hfile) {
250                 # Print out the vop_F_args structure.
251                 printh("struct "name"_args {\n\tstruct vnodeop_desc *a_desc;");
252                 for (i = 0; i < numargs; ++i)
253                         printh("\t" t_spc(types[i]) "a_" args[i] ";");
254                 printh("};");
255
256                 # Print out extern declaration.
257                 printh("extern struct vnodeop_desc " name "_desc;");
258
259                 # Print out function.
260                 printh("static __inline int " uname "(");
261                 for (i = 0; i < numargs; ++i) {
262                         printh("\t" t_spc(types[i]) args[i] \
263                             (i < numargs - 1 ? "," : ")"));
264                 }
265                 printh("{\n\tstruct " name "_args a;");
266                 printh("\tint rc;");
267                 printh("\ta.a_desc = VDESC(" name ");");
268                 for (i = 0; i < numargs; ++i)
269                         printh("\ta.a_" args[i] " = " args[i] ";");
270                 for (i = 0; i < numargs; ++i)
271                         add_debug_code(name, args[i], "Entry");
272                 add_debug_pre(name);
273                 printh("\trc = VCALL(" args[0] ", VOFFSET(" name "), &a);");
274                 printh("if (rc == 0) {");
275                 for (i = 0; i < numargs; ++i)
276                         add_debug_code(name, args[i], "OK");
277                 printh("} else {");
278                 for (i = 0; i < numargs; ++i)
279                         add_debug_code(name, args[i], "Error");
280                 printh("}");
281                 add_debug_post(name);
282                 printh("\treturn (rc);\n}");
283         }
284
285         if (cfile) {
286                 # Print out the vop_F_vp_offsets structure.  This all depends
287                 # on naming conventions and nothing else.
288                 printc("static int " name "_vp_offsets[] = {");
289                 # as a side effect, figure out the releflags
290                 releflags = "";
291                 vpnum = 0;
292                 for (i = 0; i < numargs; i++) {
293                         if (types[i] == "struct vnode *") {
294                                 printc("\tVOPARG_OFFSETOF(struct " name \
295                                     "_args,a_" args[i] "),");
296                                 if (reles[i] == "WILLRELE") {
297                                         releflags = releflags \
298                                             "|VDESC_VP" vpnum "_WILLRELE";
299                                 }
300                                 vpnum++;
301                         }
302                 }
303
304                 sub(/^\|/, "", releflags);
305                 printc("\tVDESC_NO_OFFSET");
306                 printc("};");
307
308                 # Print out the vnodeop_desc structure.
309                 printc("struct vnodeop_desc " name "_desc = {");
310                 # offset
311                 printc("\t0,");
312                 # printable name
313                 printc("\t\"" name "\",");
314                 # flags
315                 vppwillrele = "";
316                 for (i = 0; i < numargs; i++) {
317                         if (types[i] == "struct vnode **" && \
318                             reles[i] == "WILLRELE") {
319                                 vppwillrele = "|VDESC_VPP_WILLRELE";
320                         }
321                 }
322
323                 if (!releflags)
324                         releflags = "0";
325                 printc("\t" releflags vppwillrele ",");
326
327                 # vp offsets
328                 printc("\t" name "_vp_offsets,");
329                 # vpp (if any)
330                 printc("\t" find_arg_with_type("struct vnode **") ",");
331                 # cred (if any)
332                 printc("\t" find_arg_with_type("struct ucred *") ",");
333                 # thread (if any)
334                 printc("\t" find_arg_with_type("struct thread *") ",");
335                 # componentname
336                 printc("\t" find_arg_with_type("struct componentname *") ",");
337                 # transport layer information
338                 printc("\tNULL,\n};\n");
339         }
340 }
341  
342 if (hfile)
343         close(hfile);
344 if (cfile)
345         close(cfile);
346 close(srcfile);
347
348 exit 0;
349
350 }