]> CyberLeo.Net >> Repos - FreeBSD/releng/9.2.git/blob - sys/tools/vnode_if.awk
- Copy stable/9 to releng/9.2 as part of the 9.2-RELEASE cycle.
[FreeBSD/releng/9.2.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 # 4. 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 #       @(#)vnode_if.sh 8.1 (Berkeley) 6/10/93
33 # $FreeBSD$
34 #
35 # Script to produce VFS front-end sugar.
36 #
37 # usage: vnode_if.awk <srcfile> [-c | -h | -p | -q]
38 #       (where <srcfile> is currently /sys/kern/vnode_if.src)
39 #       The source file must have a .src extension
40 #
41
42 function usage()
43 {
44         print "usage: vnode_if.awk <srcfile> [-c|-h|-p|-q]";
45         exit 1;
46 }
47
48 function die(msg, what)
49 {
50         printf srcfile "(" fnr "): " > "/dev/stderr";
51         printf msg "\n", what > "/dev/stderr";
52         exit 1;
53 }
54
55 function t_spc(type)
56 {
57         # Append a space if the type is not a pointer
58         return (type ~ /\*$/) ? type : type " ";
59 }
60
61 # These are just for convenience ...
62 function printc(s) {print s > cfile;}
63 function printh(s) {print s > hfile;}
64 function printp(s) {print s > pfile;}
65 function printq(s) {print s > qfile;}
66
67 function add_debug_code(name, arg, pos, ind)
68 {
69         if (arg == "vpp")
70                 star = "*";
71         else
72                 star = "";
73         if (lockdata[name, arg, pos] && (lockdata[name, arg, pos] != "-")) {
74                 printc(ind"ASSERT_VI_UNLOCKED("star"a->a_"arg", \""uname"\");");
75                 # Add assertions for locking
76                 if (lockdata[name, arg, pos] == "L")
77                         printc(ind"ASSERT_VOP_LOCKED(" star "a->a_"arg", \""uname"\");");
78                 else if (lockdata[name, arg, pos] == "U")
79                         printc(ind"ASSERT_VOP_UNLOCKED(" star "a->a_"arg", \""uname"\");");
80                 else if (lockdata[name, arg, pos] == "E")
81                         printc(ind"ASSERT_VOP_ELOCKED(" star "a->a_"arg", \""uname"\");");
82                 else if (0) {
83                         # XXX More checks!
84                 }
85         }
86 }
87
88 function add_pre(name)
89 {
90         if (lockdata[name, "pre"]) {
91                 printc("\t"lockdata[name, "pre"]"(a);");
92         }
93 }
94
95 function add_post(name)
96 {
97         if (lockdata[name, "post"]) {
98                 printc("\t"lockdata[name, "post"]"(a, rc);");
99         }
100 }
101
102 function find_arg_with_type (type)
103 {
104         for (jj = 0; jj < numargs; jj++) {
105                 if (types[jj] == type) {
106                         return "VOPARG_OFFSETOF(struct " \
107                             name "_args,a_" args[jj] ")";
108                 }
109         }
110
111         return "VDESC_NO_OFFSET";
112 }
113
114 BEGIN{
115
116 # Process the command line
117 for (i = 1; i < ARGC; i++) {
118         arg = ARGV[i];
119         if (arg !~ /^-[chpq]+$/ && arg !~ /\.src$/)
120                 usage();
121         if (arg ~ /^-.*c/)
122                 cfile = "vnode_if.c";
123         if (arg ~ /^-.*h/)
124                 hfile = "vnode_if.h";
125         if (arg ~ /^-.*p/)
126                 pfile = "vnode_if_newproto.h";
127         if (arg ~ /^-.*q/)
128                 qfile = "vnode_if_typedef.h";
129         if (arg ~ /\.src$/)
130                 srcfile = arg;
131 }
132 ARGC = 1;
133
134 if (!cfile && !hfile && !pfile && !qfile)
135         exit 0;
136
137 if (!srcfile)
138         usage();
139
140 common_head = \
141     "/*\n" \
142     " * This file is produced automatically.\n" \
143     " * Do not modify anything in here by hand.\n" \
144     " *\n" \
145     " * Created from $FreeBSD$\n" \
146     " */\n" \
147     "\n";
148
149 if (pfile) {
150         printp(common_head)
151         printp("struct vop_vector {")
152         printp("\tstruct vop_vector\t*vop_default;")
153         printp("\tvop_bypass_t\t*vop_bypass;")
154 }
155
156 if (qfile) {
157         printq(common_head)
158 }
159
160 if (hfile) {
161         printh(common_head "extern struct vnodeop_desc vop_default_desc;");
162         printh("#include \"vnode_if_typedef.h\"")
163         printh("#include \"vnode_if_newproto.h\"")
164 }
165
166 if (cfile) {
167         printc(common_head \
168             "#include \"opt_kdtrace.h\"\n" \
169             "\n" \
170             "#include <sys/param.h>\n" \
171             "#include <sys/event.h>\n" \
172             "#include <sys/kernel.h>\n" \
173             "#include <sys/mount.h>\n" \
174             "#include <sys/sdt.h>\n" \
175             "#include <sys/signalvar.h>\n" \
176             "#include <sys/systm.h>\n" \
177             "#include <sys/vnode.h>\n" \
178             "\n" \
179             "SDT_PROVIDER_DECLARE(vfs);\n" \
180             "\n" \
181             "struct vnodeop_desc vop_default_desc = {\n" \
182             "   \"default\",\n" \
183             "   0,\n" \
184             "   (vop_bypass_t *)vop_panic,\n" \
185             "   NULL,\n" \
186             "   VDESC_NO_OFFSET,\n" \
187             "   VDESC_NO_OFFSET,\n" \
188             "   VDESC_NO_OFFSET,\n" \
189             "   VDESC_NO_OFFSET,\n" \
190             "};\n");
191 }
192
193 while ((getline < srcfile) > 0) {
194         fnr++;
195         if (NF == 0)
196                 continue;
197         if ($1 ~ /^%%/) {
198                 if (NF != 6 ||
199                     $2 !~ /^[a-z_]+$/  ||  $3 !~ /^[a-z]+$/  ||
200                     $4 !~ /^.$/  ||  $5 !~ /^.$/  ||  $6 !~ /^.$/) {
201                         die("Invalid %s construction", "%%");
202                         continue;
203                 }
204                 lockdata["vop_" $2, $3, "Entry"] = $4;
205                 lockdata["vop_" $2, $3, "OK"]    = $5;
206                 lockdata["vop_" $2, $3, "Error"] = $6;                  
207                 continue;
208         }
209
210         if ($1 ~ /^%!/) {
211                 if (NF != 4 ||
212                     ($3 != "pre" && $3 != "post")) {
213                         die("Invalid %s construction", "%!");
214                         continue;
215                 }
216                 lockdata["vop_" $2, $3] = $4;
217                 continue;
218         }
219         if ($1 ~ /^#/)
220                 continue;
221
222         # Get the function name.
223         name = $1;
224         uname = toupper(name);
225
226         # Start constructing a ktrpoint string
227         ctrstr = "\"" uname;
228         # Get the function arguments.
229         for (numargs = 0; ; ++numargs) {
230                 if ((getline < srcfile) <= 0) {
231                         die("Unable to read through the arguments for \"%s\"",
232                             name);
233                 }
234                 fnr++;
235                 if ($1 ~ /^\};/)
236                         break;
237
238                 # Delete comments, if any.
239                 gsub (/\/\*.*\*\//, "");
240
241                 # Condense whitespace and delete leading/trailing space.
242                 gsub(/[[:space:]]+/, " ");
243                 sub(/^ /, "");
244                 sub(/ $/, "");
245
246                 # Pick off direction.
247                 if ($1 != "INOUT" && $1 != "IN" && $1 != "OUT")
248                         die("No IN/OUT direction for \"%s\".", $0);
249                 dirs[numargs] = $1;
250                 sub(/^[A-Z]* /, "");
251
252                 if ((reles[numargs] = $1) == "WILLRELE")
253                         sub(/^[A-Z]* /, "");
254                 else
255                         reles[numargs] = "WONTRELE";
256
257                 # kill trailing ;
258                 if (sub(/;$/, "") < 1)
259                         die("Missing end-of-line ; in \"%s\".", $0);
260
261                 # pick off variable name
262                 if ((argp = match($0, /[A-Za-z0-9_]+$/)) < 1)
263                         die("Missing var name \"a_foo\" in \"%s\".", $0);
264                 args[numargs] = substr($0, argp);
265                 $0 = substr($0, 1, argp - 1);
266
267                 # what is left must be type
268                 # remove trailing space (if any)
269                 sub(/ $/, "");
270                 types[numargs] = $0;
271
272                 # We can do a maximum of 6 arguments to CTR*
273                 if (numargs <= 6) {
274                         if (numargs == 0)
275                                 ctrstr = ctrstr "(" args[numargs];
276                         else
277                                 ctrstr = ctrstr ", " args[numargs];
278                         if (types[numargs] ~ /\*/)
279                                 ctrstr = ctrstr " 0x%lX";
280                         else
281                                 ctrstr = ctrstr " %ld";
282                 }
283         }
284         if (numargs > 6)
285                 ctrargs = 6;
286         else
287                 ctrargs = numargs;
288         ctrstr = "\tCTR" ctrargs "(KTR_VOP,\n\t    " ctrstr ")\",\n\t    ";
289         ctrstr = ctrstr "a->a_" args[0];
290         for (i = 1; i < ctrargs; ++i)
291                 ctrstr = ctrstr ", a->a_" args[i];
292         ctrstr = ctrstr ");";
293
294         if (pfile) {
295                 printp("\t"name"_t\t*"name";")
296         }
297         if (qfile) {
298                 printq("struct "name"_args;")
299                 printq("typedef int "name"_t(struct "name"_args *);\n")
300         }
301
302         if (hfile) {
303                 # Print out the vop_F_args structure.
304                 printh("struct "name"_args {\n\tstruct vop_generic_args a_gen;");
305                 for (i = 0; i < numargs; ++i)
306                         printh("\t" t_spc(types[i]) "a_" args[i] ";");
307                 printh("};");
308                 printh("");
309
310                 # Print out extern declaration.
311                 printh("extern struct vnodeop_desc " name "_desc;");
312                 printh("");
313
314                 # Print out function prototypes.
315                 printh("int " uname "_AP(struct " name "_args *);");
316                 printh("int " uname "_APV(struct vop_vector *vop, struct " name "_args *);");
317                 printh("");
318                 printh("static __inline int " uname "(");
319                 for (i = 0; i < numargs; ++i) {
320                         printh("\t" t_spc(types[i]) args[i] \
321                             (i < numargs - 1 ? "," : ")"));
322                 }
323                 printh("{");
324                 printh("\tstruct " name "_args a;");
325                 printh("");
326                 printh("\ta.a_gen.a_desc = &" name "_desc;");
327                 for (i = 0; i < numargs; ++i)
328                         printh("\ta.a_" args[i] " = " args[i] ";");
329                 printh("\treturn (" uname "_APV("args[0]"->v_op, &a));");
330                 printh("}");
331
332                 printh("");
333         }
334
335         if (cfile) {
336                 # Print out the vop_F_vp_offsets structure.  This all depends
337                 # on naming conventions and nothing else.
338                 printc("static int " name "_vp_offsets[] = {");
339                 # as a side effect, figure out the releflags
340                 releflags = "";
341                 vpnum = 0;
342                 for (i = 0; i < numargs; i++) {
343                         if (types[i] == "struct vnode *") {
344                                 printc("\tVOPARG_OFFSETOF(struct " name \
345                                     "_args,a_" args[i] "),");
346                                 if (reles[i] == "WILLRELE") {
347                                         releflags = releflags \
348                                             "|VDESC_VP" vpnum "_WILLRELE";
349                                 }
350                                 vpnum++;
351                         }
352                 }
353
354                 sub(/^\|/, "", releflags);
355                 printc("\tVDESC_NO_OFFSET");
356                 printc("};");
357
358                 printc("\n");
359                 printc("SDT_PROBE_DEFINE2(vfs, vop, " name ", entry, entry, \"struct vnode *\", \"struct " name "_args *\");\n");
360                 printc("SDT_PROBE_DEFINE3(vfs, vop, " name ", return, return, \"struct vnode *\", \"struct " name "_args *\", \"int\");\n");
361
362                 # Print out function.
363                 printc("\nint\n" uname "_AP(struct " name "_args *a)");
364                 printc("{");
365                 printc("");
366                 printc("\treturn(" uname "_APV(a->a_" args[0] "->v_op, a));");
367                 printc("}");
368                 printc("\nint\n" uname "_APV(struct vop_vector *vop, struct " name "_args *a)");
369                 printc("{");
370                 printc("\tint rc;");
371                 printc("");
372                 printc("\tVNASSERT(a->a_gen.a_desc == &" name "_desc, a->a_" args[0]",");
373                 printc("\t    (\"Wrong a_desc in " name "(%p, %p)\", a->a_" args[0]", a));");
374                 printc("\twhile(vop != NULL && \\");
375                 printc("\t    vop->"name" == NULL && vop->vop_bypass == NULL)")
376                 printc("\t\tvop = vop->vop_default;")
377                 printc("\tVNASSERT(vop != NULL, a->a_" args[0]", (\"No "name"(%p, %p)\", a->a_" args[0]", a));")
378                 printc("\tSDT_PROBE(vfs, vop, " name ", entry, a->a_" args[0] ", a, 0, 0, 0);\n");
379                 for (i = 0; i < numargs; ++i)
380                         add_debug_code(name, args[i], "Entry", "\t");
381                 add_pre(name);
382                 printc("\tVFS_PROLOGUE(a->a_" args[0]"->v_mount);")
383                 printc("\tif (vop->"name" != NULL)")
384                 printc("\t\trc = vop->"name"(a);")
385                 printc("\telse")
386                 printc("\t\trc = vop->vop_bypass(&a->a_gen);")
387                 printc("\tVFS_EPILOGUE(a->a_" args[0]"->v_mount);")
388                 printc(ctrstr);
389                 printc("\tSDT_PROBE(vfs, vop, " name ", return, a->a_" args[0] ", a, rc, 0, 0);\n");
390                 printc("\tif (rc == 0) {");
391                 for (i = 0; i < numargs; ++i)
392                         add_debug_code(name, args[i], "OK", "\t\t");
393                 printc("\t} else {");
394                 for (i = 0; i < numargs; ++i)
395                         add_debug_code(name, args[i], "Error", "\t\t");
396                 printc("\t}");
397                 add_post(name);
398                 printc("\treturn (rc);");
399                 printc("}\n");
400
401                 # Print out the vnodeop_desc structure.
402                 printc("struct vnodeop_desc " name "_desc = {");
403                 # printable name
404                 printc("\t\"" name "\",");
405                 # flags
406                 vppwillrele = "";
407                 for (i = 0; i < numargs; i++) {
408                         if (types[i] == "struct vnode **" && \
409                             reles[i] == "WILLRELE") {
410                                 vppwillrele = "|VDESC_VPP_WILLRELE";
411                         }
412                 }
413
414                 if (!releflags)
415                         releflags = "0";
416                 printc("\t" releflags vppwillrele ",");
417
418                 # function to call
419                 printc("\t(vop_bypass_t *)" uname "_AP,");
420                 # vp offsets
421                 printc("\t" name "_vp_offsets,");
422                 # vpp (if any)
423                 printc("\t" find_arg_with_type("struct vnode **") ",");
424                 # cred (if any)
425                 printc("\t" find_arg_with_type("struct ucred *") ",");
426                 # thread (if any)
427                 printc("\t" find_arg_with_type("struct thread *") ",");
428                 # componentname
429                 printc("\t" find_arg_with_type("struct componentname *") ",");
430                 # transport layer information
431                 printc("};\n");
432         }
433 }
434  
435 if (pfile)
436         printp("};")
437  
438 if (hfile)
439         close(hfile);
440 if (cfile)
441         close(cfile);
442 if (pfile)
443         close(pfile);
444 close(srcfile);
445
446 exit 0;
447
448 }