Training courses

Kernel and Embedded Linux

Bootlin training courses

Embedded Linux, kernel,
Yocto Project, Buildroot, real-time,
graphics, boot time, debugging...

Bootlin logo

Elixir Cross Referencer

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
#!/bin/sh -
copyright="\
/*
 * Copyright (c) 1992, 1993, 1994, 1995
 *	The Regents of the University of California.  All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 * 3. Neither the name of the University nor the names of its contributors
 *    may be used to endorse or promote products derived from this software
 *    without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS \`\`AS IS'' AND
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
 * SUCH DAMAGE.
 */
"
SCRIPT_ID='$NetBSD: vnode_if.sh,v 1.77 2022/10/26 23:39:43 riastradh Exp $'

# Script to produce VFS front-end sugar.
#
# usage: vnode_if.sh srcfile
#	(where srcfile is currently /sys/kern/vnode_if.src)
#

if [ $# -ne 1 ] ; then
	echo 'usage: vnode_if.sh srcfile'
	exit 1
fi

# Name and revision of the source file.
src=$1
SRC_ID=`head -1 $src | sed -e 's/.*\$\(.*\)\$.*/\1/'`

# Names of the created files.
out_c=vnode_if.c
out_rumpc=../rump/librump/rumpvfs/rumpvnode_if.c
out_h=../sys/vnode_if.h
out_rumph=../rump/include/rump/rumpvnode_if.h

# generate VNODE_LOCKDEBUG checks (not fully functional)
lockdebug=1

# Awk program (must support nawk extensions)
# Use "awk" at Berkeley, "nawk" or "gawk" elsewhere.
awk=${AWK:-awk}

# Does this awk have a "toupper" function? (i.e. is it GNU awk)
isgawk=`$awk 'BEGIN { print toupper("true"); exit; }' 2>/dev/null`

# If this awk does not define "toupper" then define our own.
if [ "$isgawk" = TRUE ] ; then
	# GNU awk provides it.
	toupper=
else
	# Provide our own toupper()
	toupper='
function toupper(str) {
	_toupper_cmd = "echo "str" |tr a-z A-Z"
	_toupper_cmd | getline _toupper_str;
	close(_toupper_cmd);
	return _toupper_str;
}'
fi

#
# This is the common part of all awk programs that read $src
# This parses the input for one function into the arrays:
#	argdir, argtype, argname, willrele
# and calls "doit()" to generate output for the function.
#
# Input to this parser is pre-processed slightly by sed
# so this awk parser doesn't have to work so hard.  The
# changes done by the sed pre-processing step are:
#	insert a space between * and pointer name
#	replace semicolons with spaces
#
sed_prep='s:\*\([^\*/]\):\* \1:g
s/;/ /'
awk_parser='
# Comment line
/^#/	{ next; }
# First line of description
/^vop_/	{
	name=$1;
	args_name=$1;
	argc=0;
	have_context=0;
	is_context=0;
	ncontext=0;
	willmake=-1;
	fstrans="";
	do_pre="";
	do_post="";
	next;
}
# Last line of description
/^}/	{
	doit();
	next;
}
# Middle lines of description
{
	if ($1 == "VERSION") {
		args_name=args_name "_v" $2;
		next;
	} else if ($1 ~ "^FSTRANS=") {
		fstrans = $1;
		sub("FSTRANS=", "", fstrans);
		next;
	} else if ($1 ~ "^PRE=") {
		do_pre = $1;
		sub("PRE=", "", do_pre);
		next;
	} else if ($1 ~ "^POST=") {
		do_post = $1;
		sub("POST=", "", do_post);
		next;
	}

	if ($1 == "CONTEXT") {
		# CONTEXT require PRE and POST handlers.
		if (do_pre == "" || do_post == "")
			next;
		is_context=1;
		have_context=1;
	} else {
		if (have_context) {
			# CONTEXT members must come at the end of
			# the args structure, so everything else
			# is ignored.
			next;
		}
		argdir[argc] = $1;
	}
	i=2;

	if (is_context == 0) {
		if ($2 == "LOCKED=EXCL") {
			lockstate[argc] = "elocked";
			i++;
		} else if ($2 == "LOCKED=YES") {
			lockstate[argc] = "locked";
			i++;
		} else if ($2 == "LOCKED=NO") {
			lockstate[argc] = "unlocked";
			i++;
		} else
			lockstate[argc] = "";

		if ($2 == "WILLRELE" ||
		    $3 == "WILLRELE") {
			willrele[argc] = 1;
			i++;
		} else if ($2 == "WILLPUT" ||
			   $3 == "WILLPUT") {
			willrele[argc] = 3;
			i++;
		} else
			willrele[argc] = 0;

		if ($2 == "WILLMAKE") {
			willmake=argc;
			i++;
		}
		if (argc == 0 && fstrans == "") {
			if (lockstate[0] == "locked" ||
			     lockstate[0] == "elocked")
				fstrans = "NO";
			else
				fstrans = "YES";
		}
	}

	# XXX: replace non-portable types for rump.  We should really
	# nuke the types from the kernel, but that is a battle for
	# another day.
	at = $i;
	if (rump) {
		if (at == "vm_prot_t")
			at = "int";
		if (at == "voff_t")
			at = "off_t";
		if (at == "kauth_cred_t")
			at = "struct kauth_cred *"
		if (at == "daddr_t")
			at = "int64_t"
	}
	argtype[argc + ncontext] = at;
	i++;
	while (i < NF) {
		argtype[argc + ncontext] = argtype[argc + ncontext]" "$i;
		i++;
	}
	argname[argc + ncontext] = $i;
	if (is_context)
		ncontext++;
	else
		argc++;
	next;
}
'

# This is put before the copyright on each generated file.
warning="\
/*	@NetBSD@	*/

/*
 * Warning: DO NOT EDIT! This file is automatically generated!
 * (Modifications made here may easily be lost!)
 *
 * Created from the file:
 *	${SRC_ID}
 * by the script:
 *	${SCRIPT_ID}
 */
"

# This is to satisfy McKusick (get rid of evil spaces 8^)
anal_retentive='s:\([^/]\*\) :\1:g'

do_hfile () {
#
# Redirect stdout to the H file.
#
echo "$0: Creating $1" 1>&2
exec > $1
rump=$2

# Begin stuff
if [ -z "${rump}" ]; then
	SYS='SYS_'
else
	SYS='RUMP_RUMP'
fi
echo -n "$warning" | sed -e 's/\$//g;s/@/\$/g;s/ $//'
echo ""
echo -n "$copyright"
echo ''
echo "#ifndef _${SYS}VNODE_IF_H_"
echo "#define _${SYS}VNODE_IF_H_"
[ -z "${rump}" ] && echo "
extern const struct vnodeop_desc ${rump}vop_default_desc;"
echo

# Body stuff
# This awk program needs toupper() so define it if necessary.
sed -e "$sed_prep" $src | $awk -v rump=${rump} "$toupper"'
function doit() {
	name = rump name
	# Declare arg struct, descriptor.
	if (!rump) {
		printf("\n#define %s_DESCOFFSET %d\n",
		    toupper(name), vop_offset++);
		printf("struct %s_args {\n", args_name);
		printf("\tconst struct vnodeop_desc * a_desc;\n");
		for (i=0; i<argc; i++) {
			printf("\t%s a_%s;\n", argtype[i], argname[i]);
		}
		for (i=0; i<ncontext; i++) {
			printf("\t%s ctx_%s;\n", argtype[argc+i], \
			    argname[argc+i]);
		}
		printf("};\n");
		printf("extern const struct vnodeop_desc %s_desc;\n", name);
	}
	# Prototype it.
	protoarg = sprintf("int %s(", toupper(name));
	protolen = length(protoarg);
	printf("%s", protoarg);
	for (i=0; i<argc; i++) {
		protoarg = sprintf("%s", argtype[i]);
		if (i < (argc-1)) protoarg = (protoarg ", ");
		arglen = length(protoarg);
		if ((protolen + arglen) > 77) {
			protoarg = ("\n    " protoarg);
			arglen += 4;
			protolen = 0;
		}
		printf("%s", protoarg);
		protolen += arglen;
	}
	printf(");\n");
}
BEGIN	{
	vop_offset = 1; # start at 1, to count the 'default' op

	printf("struct buf;\n");
	if (rump) {
		printf("struct flock;\n");
		printf("struct knote;\n");
		printf("struct vm_page;\n");
		printf("struct acl;\n");
		printf("\n#include <sys/acl.h>\n");
	}
	printf("\n#ifndef _KERNEL\n#include <stdbool.h>\n#endif\n");
	if (rump)
		printf("\n");
}
END	{
	if (!rump) {
		printf("\n#define VNODE_OPS_COUNT\t%d\n", vop_offset);
	}
}
'"$awk_parser" | sed -e "$anal_retentive"

# End stuff
echo ''
echo "#endif /* !_${SYS}VNODE_IF_H_ */"
}
do_hfile $out_h ''
do_hfile $out_rumph 'rump_'

do_cfile () {
#
# Redirect stdout to the C file.
#
echo "$0: Creating $1" 1>&2
exec > $1
rump=$2

# Begin stuff
echo -n "$warning" | sed -e 's/\$//g;s/@/\$/g;s/ $//'
echo ""
echo -n "$copyright"
echo "
#include <sys/cdefs.h>
__KERNEL_RCSID(0, \"\$NetBSD\$\");"

if [ -z "${rump}" -a ${lockdebug} -ne 0 ] ; then
	echo ''
	echo '#ifdef _KERNEL_OPT'
	echo '#include "opt_vnode_lockdebug.h"'
	echo '#endif /* _KERNEL_OPT */'
fi

echo '
#include <sys/param.h>
#include <sys/mount.h>
#include <sys/buf.h>
#include <sys/fcntl.h>
#include <sys/vnode.h>
#include <sys/lock.h>'
[ -z "${rump}" ] && echo '#include <sys/fstrans.h>'
[ ! -z "${rump}" ] && echo '#include <rump/rumpvnode_if.h>'		\
	&& echo '#include <rump-sys/kern.h>'

if [ -z "${rump}" ] ; then
	echo "
#include <miscfs/deadfs/deadfs.h>

enum fst_op { FST_NO, FST_YES, FST_LAZY, FST_TRY };

static inline int
vop_pre(vnode_t *vp, struct mount **mp, bool *mpsafe, enum fst_op op)
{
	int error;

	*mpsafe = (vp->v_vflag & VV_MPSAFE);

	if (!*mpsafe) {
		KERNEL_LOCK(1, curlwp);
	}

	if (op == FST_YES || op == FST_LAZY || op == FST_TRY) {
		for (;;) {
			*mp = vp->v_mount;
			if (op == FST_TRY) {
				error = fstrans_start_nowait(*mp);
				if (error) {
					if (!*mpsafe) {
						KERNEL_UNLOCK_ONE(curlwp);
					}
					return error;
				}
			} else if (op == FST_LAZY) {
				fstrans_start_lazy(*mp);
			} else {
				fstrans_start(*mp);
			}
			if (__predict_true(*mp == vp->v_mount))
				break;
			fstrans_done(*mp);
		}
	} else {
		*mp = vp->v_mount;
	}

	return 0;
}

static inline u_quad_t
vop_pre_get_size(struct vnode *vp)
{
	mutex_enter(vp->v_interlock);
	KASSERT(vp->v_size != VSIZENOTSET);
	u_quad_t rv = (u_quad_t)vp->v_size;
	mutex_exit(vp->v_interlock);

	return rv;
}

/*
 * VOP_RMDIR(), VOP_REMOVE(), and VOP_RENAME() need special handling
 * because they each drop the caller's references on one or more of
 * their arguments.  While there must be an open file descriptor in
 * associated with a vnode in order for knotes to be attached to it,
 * that status could change during the course of the operation.  So,
 * for the vnode arguments that are WILLRELE or WILLPUT, we check
 * pre-op if there are registered knotes, take a hold count if so,
 * and post-op release the hold after activating any knotes still
 * associated with the vnode.
 */

#define	VOP_POST_KNOTE(thisvp, e, n)					\\
do {									\\
	if (__predict_true((e) == 0)) {					\\
		/*							\\
		 * VN_KNOTE() does the VN_KEVENT_INTEREST()		\\
		 * check for us.					\\
		 */							\\
		VN_KNOTE((thisvp), (n));				\\
	}								\\
} while (/*CONSTCOND*/0)

#define	VOP_POST_KNOTE_HELD(thisvp, e, n)				\\
do {									\\
	/*								\\
	 * We don't perform a VN_KEVENT_INTEREST() check here; it	\\
	 * was already performed when we did the pre-op work that	\\
	 * caused the vnode to be held in the first place.		\\
	 */								\\
	mutex_enter((thisvp)->v_interlock);				\\
	if (__predict_true((e) == 0)) {					\\
		knote(&(thisvp)->v_klist->vk_klist, (n));		\\
	}								\\
	holdrelel((thisvp));						\\
	mutex_exit((thisvp)->v_interlock);				\\
	/*								\\
	 * thisvp might be gone now!  Don't touch!			\\
	 */								\\
} while (/*CONSTCOND*/0)

#define	vop_create_post(ap, e)						\\
	VOP_POST_KNOTE((ap)->a_dvp, (e), NOTE_WRITE)

#define	vop_mknod_post(ap, e)						\\
	VOP_POST_KNOTE((ap)->a_dvp, (e), NOTE_WRITE)

#define	vop_setattr_pre(ap)						\\
	u_quad_t osize = 0;						\\
	long vp_events =						\\
	    VN_KEVENT_INTEREST((ap)->a_vp, NOTE_ATTRIB | NOTE_EXTEND)	\\
	    ? NOTE_ATTRIB : 0;						\\
	bool check_extend = false;					\\
	if (__predict_false(vp_events != 0 &&				\\
	    (ap)->a_vap->va_size != VNOVALSIZE)) {			\\
		check_extend = true;					\\
		osize = vop_pre_get_size((ap)->a_vp);			\\
	}

#define	vop_setattr_post(ap, e)						\\
do {									\\
	if (__predict_false(vp_events != 0)) {				\\
		if (__predict_false(check_extend &&			\\
		    (ap)->a_vap->va_size > osize)) {			\\
			vp_events |= NOTE_EXTEND;			\\
		}							\\
		VOP_POST_KNOTE((ap)->a_vp, (e), vp_events);		\\
	}								\\
} while (/*CONSTCOND*/0)

#define	vop_setacl_post(ap, e)						\\
	VOP_POST_KNOTE((ap)->a_vp, (e), NOTE_ATTRIB)

#define	vop_link_post(ap, e)						\\
do {									\\
	VOP_POST_KNOTE((ap)->a_dvp, (e), NOTE_WRITE);			\\
	VOP_POST_KNOTE((ap)->a_vp, (e), NOTE_LINK);			\\
} while (/*CONSTCOND*/0)

#define	vop_mkdir_post(ap, e)						\\
	VOP_POST_KNOTE((ap)->a_dvp, (e), NOTE_WRITE | NOTE_LINK)

#define	vop_remove_pre_common(ap)					\\
	bool post_event_vp =						\\
	    VN_KEVENT_INTEREST((ap)->a_vp, NOTE_DELETE | NOTE_LINK);	\\
	if (__predict_false(post_event_vp)) {				\\
		vhold((ap)->a_vp);					\\
	}

#define	vop_remove_post_common(ap, e, dn, lc)				\\
do {									\\
	VOP_POST_KNOTE((ap)->a_dvp, (e), (dn));				\\
	if (__predict_false(post_event_vp)) {				\\
		VOP_POST_KNOTE_HELD((ap)->a_vp, (e),			\\
		    (lc) ? NOTE_LINK : NOTE_DELETE);			\\
	}								\\
} while (/*CONSTCOND*/0)

/*
 * One could make the argument that VOP_REMOVE() should send NOTE_LINK
 * on vp if the resulting link count is not zero, but that's not what
 * the documentation says.
 *
 * We could change this easily by passing ap->ctx_vp_new_nlink to
 * vop_remove_post_common().
 */
#define	vop_remove_pre(ap)						\\
	vop_remove_pre_common((ap));					\\
	/*								\\
	 * We will assume that the file being removed is deleted unless	\\
	 * the file system tells us otherwise by updating vp_new_nlink.	\\
	 */								\\
	(ap)->ctx_vp_new_nlink = 0;

#define	vop_remove_post(ap, e)						\\
	vop_remove_post_common((ap), (e), NOTE_WRITE, 0)

#define	vop_rmdir_pre(ap)						\\
	vop_remove_pre_common(ap)

#define	vop_rmdir_post(ap, e)						\\
	vop_remove_post_common((ap), (e), NOTE_WRITE | NOTE_LINK, 0)

#define	vop_symlink_post(ap, e)						\\
	VOP_POST_KNOTE((ap)->a_dvp, (e), NOTE_WRITE)

#define	vop_open_post(ap, e)						\\
	VOP_POST_KNOTE((ap)->a_vp, (e), NOTE_OPEN)

#define	vop_close_post(ap, e)						\\
do {									\\
	/* See the definition of VN_KNOTE() in <sys/vnode.h>. */	\\
	if (__predict_false(VN_KEVENT_INTEREST((ap)->a_vp,		\\
	    NOTE_CLOSE_WRITE | NOTE_CLOSE) && (e) == 0)) {		\\
		struct vnode *thisvp = (ap)->a_vp;			\\
		mutex_enter(thisvp->v_interlock);			\\
		/*							\\
		 * Don't send NOTE_CLOSE when closing a vnode that's	\\
		 * been reclaimed or otherwise revoked; a NOTE_REVOKE	\\
		 * has already been sent, and this close is effectively	\\
		 * meaningless from the watcher's perspective.		\\
		 */							\\
		if (__predict_true(thisvp->v_op != dead_vnodeop_p)) {	\\
			knote(&thisvp->v_klist->vk_klist,		\\
			    ((ap)->a_fflag & FWRITE)			\\
			    ? NOTE_CLOSE_WRITE : NOTE_CLOSE);		\\
		}							\\
		mutex_exit(thisvp->v_interlock);			\\
	}								\\
} while (/*CONSTCOND*/0)

#define	vop_read_post(ap, e)						\\
	VOP_POST_KNOTE((ap)->a_vp, (e), NOTE_READ)

#define	vop_write_pre(ap)						\\
	off_t ooffset = 0, noffset = 0;					\\
	u_quad_t osize = 0;						\\
	long vp_events =						\\
	    VN_KEVENT_INTEREST((ap)->a_vp, NOTE_WRITE | NOTE_EXTEND)	\\
	    ? NOTE_WRITE : 0;						\\
	if (__predict_false(vp_events != 0)) {				\\
		ooffset = (ap)->a_uio->uio_offset;			\\
		osize = vop_pre_get_size((ap)->a_vp);			\\
	}

#define	vop_write_post(ap, e)						\\
do {									\\
	/*								\\
	 * If any data was written, we'll post an event, even if	\\
	 * there was an error.						\\
	 */								\\
	noffset = (ap)->a_uio->uio_offset;				\\
	if (__predict_false(vp_events != 0 && noffset > ooffset)) {	\\
		if (noffset > osize) {					\\
			vp_events |= NOTE_EXTEND;			\\
		}							\\
		VN_KNOTE((ap)->a_vp, vp_events);			\\
	}								\\
} while (/*CONSTCOND*/0)

static inline void
vop_post(vnode_t *vp, struct mount *mp, bool mpsafe, enum fst_op op)
{

	if (op == FST_YES || op == FST_LAZY) {
		fstrans_done(mp);
	}

	if (!mpsafe) {
		KERNEL_UNLOCK_ONE(curlwp);
	}
}

static inline void
assert_vop_unlocked(vnode_t *vp, const char *str)
{
#if defined(VNODE_LOCKDEBUG)

	if (VOP_ISLOCKED(vp) == LK_EXCLUSIVE)
		panic(\"%s: %p %d/%d is locked but should not be\",
		    str, vp, vp->v_tag, vp->v_type);
#endif
}

static inline void
assert_vop_locked(vnode_t *vp, const char *str)
{
#if defined(VNODE_LOCKDEBUG)

	if (VOP_ISLOCKED(vp) == LK_NONE)
		panic(\"%s: %p %d/%d is not locked but should be\",
		    str, vp, vp->v_tag, vp->v_type);
#endif
}

static inline void
assert_vop_elocked(vnode_t *vp, const char *str)
{
#if defined(VNODE_LOCKDEBUG)

	if (VOP_ISLOCKED(vp) != LK_EXCLUSIVE)
		panic(\"%s: %p %d/%d is not exclusive locked but should be\",
		    str, vp, vp->v_tag, vp->v_type);
#endif
}

const struct vnodeop_desc vop_default_desc = {"
echo '	0,
	"default",
	0,
	NULL,
	VDESC_NO_OFFSET,
	VDESC_NO_OFFSET,
	VDESC_NO_OFFSET,
};
'
fi

# Body stuff
sed -e "$sed_prep" $src | $awk -v rump=${rump} -v lockdebug=${lockdebug} '
function do_offset(typematch) {
	for (i=0; i<argc; i++) {
		if (argtype[i] == typematch) {
			printf("\tVOPARG_OFFSETOF(struct %s_args, a_%s),\n",
				args_name, argname[i]);
			return i;
		};
	};
	print "\tVDESC_NO_OFFSET,";
	return -1;
}

function offsets() {
	# Define offsets array
	printf("const int %s_vp_offsets[] = {\n", name);
	for (i=0; i<argc; i++) {
		if (argtype[i] == "struct vnode *") {
			printf ("\tVOPARG_OFFSETOF(struct %s_args,a_%s),\n",
				args_name, argname[i]);
		}
	}
	print "\tVDESC_NO_OFFSET";
	print "};";
	# Define F_desc
	printf("const struct vnodeop_desc %s_desc = {\n", name);
	# offset
	printf ("\t%s_DESCOFFSET,\n", toupper(name));
	# printable name
	printf ("\t\"%s\",\n", name);
	# flags
	printf("\t0");
	vpnum = 0;
	for (i=0; i<argc; i++) {
		if (willrele[i]) {
			if (willrele[i] == 3) {
				word = "PUT";
			} else {
				word = "RELE";
			}
			printf(" | VDESC_VP%s_WILL%s", vpnum, word);
		}
		if (argtype[i] == "struct vnode *")
			vpnum++;
	}
	print ",";
	# vp offsets
	printf ("\t%s_vp_offsets,\n", name);
	# vpp (if any)
	do_offset("struct vnode **");
	# cred (if any)
	do_offset("kauth_cred_t");
	# componentname
	do_offset("struct componentname *");
	printf ("};\n");
}

function bodyrump() {
	printf("{\n\tint error;\n\n");
	printf("\trump_schedule();\n");
	printf("\terror = %s(", toupper(name));
	for (i=0; i<argc; i++) {
		printf("%s", argname[i]);
		if (i < (argc-1)) printf(", ");
	}
	printf(");\n");
	printf("\trump_unschedule();\n\n");
	printf("\treturn error;\n}\n");
}

function bodynorm() {
	printf("{\n\tint error;\n\tbool mpsafe;\n\tstruct %s_args a;\n",
		args_name);
	printf("\tstruct mount *mp;\n");
	printf("\ta.a_desc = VDESC(%s);\n", name);
	for (i=0; i<argc; i++) {
		printf("\ta.a_%s = %s;\n", argname[i], argname[i]);
	}
	if (lockdebug) {
		for (i=0; i<argc; i++) {
			if (lockstate[i] == "")
				continue;
			printf("\tassert_vop_%s(%s, \"%s: %s\");\n",
			    lockstate[i], argname[i], name, argname[i]);
		}
	}
	# This is done before generic vop_pre() because we want
	# to do any setup before beginning an fstrans.
	if (do_pre != "")
		printf("\t%s(&a);\n", do_pre);
	if (fstrans == "LOCK")
		printf("\terror = vop_pre(%s, &mp, &mpsafe, %s);\n",
			argname[0], "(!(flags & (LK_SHARED|LK_EXCLUSIVE)) ? FST_NO : (flags & LK_NOWAIT ? FST_TRY : FST_YES))");
	else if (fstrans == "UNLOCK")
		printf("\terror = vop_pre(%s, &mp, &mpsafe, FST_%s);\n",
			argname[0], "NO");
	else
		printf("\terror = vop_pre(%s, &mp, &mpsafe, FST_%s);\n",
			argname[0], fstrans);
	printf("\tif (error)\n\t\treturn error;\n");
	printf("\terror = (VCALL(%s, VOFFSET(%s), &a));\n",
		argname[0], name);
	if (fstrans == "LOCK")
		printf("\tvop_post(%s, mp, mpsafe, %s);\n",
			argname[0], "(flags & (LK_UPGRADE|LK_DOWNGRADE) ? FST_NO : (error ? FST_YES : FST_NO))");
	else if (fstrans == "UNLOCK")
		printf("\tvop_post(%s, mp, mpsafe, FST_%s);\n",
			argname[0], "YES");
	else
		printf("\tvop_post(%s, mp, mpsafe, FST_%s);\n",
			argname[0], fstrans);
	# This is done after generic vop_post() in order to minimize
	# time spent with the KERNEL_LOCK held for file systems that
	# still require it.
	if (do_post != "")
		printf("\t%s(&a, error);\n", do_post);
	if (willmake != -1) {
		printf("#ifdef DIAGNOSTIC\n");
		printf("\tif (error == 0)\n"				\
		    "\t\tKASSERT((*%s)->v_size != VSIZENOTSET\n"	\
		    "\t\t    && (*%s)->v_writesize != VSIZENOTSET);\n",
		    argname[willmake], argname[willmake]);
		printf("#endif /* DIAGNOSTIC */\n");
	}
	printf("\treturn error;\n}\n");
}

function doit() {
	printf("\n");
	if (!rump)
		offsets();

	if (rump)
		extname = "RUMP_" toupper(name);
	else
		extname = toupper(name);

	# Define function.
	printf("int\n%s(", extname);
	for (i=0; i<argc; i++) {
		printf("%s %s", argtype[i], argname[i]);
		if (i < (argc-1)) printf(",\n    ");
	}
	printf(")\n");

	if (rump)
		bodyrump();
	else
		bodynorm();
}
BEGIN	{
	# start from 1 (vop_default is at 0)
	argc=1;
}
'"$awk_parser" | sed -e "$anal_retentive"

# End stuff
[ -n "${rump}" ] && return

# Add the vfs_op_descs array to the C file.
# Begin stuff
echo "
const struct vnodeop_desc * const ${rump}vfs_op_descs[] = {
	&${rump}vop_default_desc,	/* MUST BE FIRST */
"

# Body stuff
sed -e "$sed_prep" $src | $awk -v rump=${rump} '
function doit() {
	printf("\t&%s_desc,\n", name);
}
'"$awk_parser"

# End stuff
echo '	NULL
};'
}
do_cfile $out_c ''
do_cfile $out_rumpc 'rump_'

exit 0

# Local Variables:
# tab-width: 4
# End: