DragonFlyBSD Kernel Audit
sys/kern/vfs_vnops.c
← back
   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
 841
 842
 843
 844
 845
 846
 847
 848
 849
 850
 851
 852
 853
 854
 855
 856
 857
 858
 859
 860
 861
 862
 863
 864
 865
 866
 867
 868
 869
 870
 871
 872
 873
 874
 875
 876
 877
 878
 879
 880
 881
 882
 883
 884
 885
 886
 887
 888
 889
 890
 891
 892
 893
 894
 895
 896
 897
 898
 899
 900
 901
 902
 903
 904
 905
 906
 907
 908
 909
 910
 911
 912
 913
 914
 915
 916
 917
 918
 919
 920
 921
 922
 923
 924
 925
 926
 927
 928
 929
 930
 931
 932
 933
 934
 935
 936
 937
 938
 939
 940
 941
 942
 943
 944
 945
 946
 947
 948
 949
 950
 951
 952
 953
 954
 955
 956
 957
 958
 959
 960
 961
 962
 963
 964
 965
 966
 967
 968
 969
 970
 971
 972
 973
 974
 975
 976
 977
 978
 979
 980
 981
 982
 983
 984
 985
 986
 987
 988
 989
 990
 991
 992
 993
 994
 995
 996
 997
 998
 999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
/*
 * Copyright (c) 1982, 1986, 1989, 1993
 *	The Regents of the University of California.  All rights reserved.
 * (c) UNIX System Laboratories, Inc.
 * All or some portions of this file are derived from material licensed
 * to the University of California by American Telephone and Telegraph
 * Co. or Unix System Laboratories, Inc. and are reproduced herein with
 * the permission of UNIX System Laboratories, Inc.
 *
 * 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.
 *
 *	@(#)vfs_vnops.c	8.2 (Berkeley) 1/21/94
 * $FreeBSD: src/sys/kern/vfs_vnops.c,v 1.87.2.13 2002/12/29 18:19:53 dillon Exp $
 */

#include <sys/param.h>
#include <sys/systm.h>
#include <sys/uio.h>
#include <sys/fcntl.h>
#include <sys/file.h>
#include <sys/file2.h>
#include <sys/stat.h>
#include <sys/proc.h>
#include <sys/caps.h>
#include <sys/mount.h>
#include <sys/nlookup.h>
#include <sys/vnode.h>
#include <sys/buf.h>
#include <sys/filio.h>
#include <sys/ttycom.h>
#include <sys/conf.h>
#include <sys/sysctl.h>
#include <sys/syslog.h>
#include <sys/spinlock.h>
#include <sys/spinlock2.h>
#include <sys/unistd.h>

#include <sys/mplock2.h>

#include <vm/vm_object.h>

#include <machine/limits.h>

static int vn_closefile (struct file *fp);
static int vn_ioctl (struct file *fp, u_long com, caddr_t data,
		struct ucred *cred, struct sysmsg *msg);
static int vn_read (struct file *fp, struct uio *uio,
		struct ucred *cred, int flags);
static int vn_kqfilter (struct file *fp, struct knote *kn);
static int vn_statfile (struct file *fp, struct stat *sb, struct ucred *cred);
static int vn_write (struct file *fp, struct uio *uio,
		struct ucred *cred, int flags);
static int vn_seek (struct file *fp, off_t offset, int whence, off_t *res);

struct fileops vnode_fileops = {
	.fo_read = vn_read,
	.fo_write = vn_write,
	.fo_ioctl = vn_ioctl,
	.fo_kqfilter = vn_kqfilter,
	.fo_stat = vn_statfile,
	.fo_close = vn_closefile,
	.fo_shutdown = nofo_shutdown,
	.fo_seek = vn_seek
};

/*
 * Common code for vnode open operations.  Check permissions, and call
 * the VOP_NOPEN or VOP_NCREATE routine.
 *
 * The caller is responsible for setting up nd with nlookup_init() and
 * for cleaning it up with nlookup_done(), whether we return an error
 * or not.
 *
 * On success nd->nl_open_vp will hold a referenced and, if requested,
 * locked vnode.  A locked vnode is requested via NLC_LOCKVP.  If fp
 * is non-NULL the vnode will be installed in the file pointer.
 *
 * NOTE: If the caller wishes the namecache entry to be operated with
 *	 a shared lock it must use NLC_SHAREDLOCK.  If NLC_LOCKVP is set
 *	 then the vnode lock will also be shared.
 *
 * NOTE: The vnode is referenced just once on return whether or not it
 *	 is also installed in the file pointer.
 */
int
vn_open(struct nlookupdata *nd, struct file **fpp, int fmode, int cmode)
{
	struct file *fp = fpp ? *fpp : NULL;
	struct vnode *vp;
	struct ucred *cred = nd->nl_cred;
	struct vattr vat;
	struct vattr *vap = &vat;
	int error;
	int vpexcl;
	u_int flags;
	uint64_t osize;
	struct mount *mp;

	/*
	 * Certain combinations are illegal
	 */
	if ((fmode & (FWRITE | O_TRUNC)) == O_TRUNC)
		return(EACCES);

	/*
	 * Lookup the path and create or obtain the vnode.  After a
	 * successful lookup a locked nd->nl_nch will be returned.
	 *
	 * The result of this section should be a locked vnode.
	 *
	 * XXX with only a little work we should be able to avoid locking
	 * the vnode if FWRITE, O_CREAT, and O_TRUNC are *not* set.
	 */
	nd->nl_flags |= NLC_OPEN;
	if (fmode & O_APPEND)
		nd->nl_flags |= NLC_APPEND;
	if (fmode & O_TRUNC)
		nd->nl_flags |= NLC_TRUNCATE;
	if (fmode & FREAD)
		nd->nl_flags |= NLC_READ;
	if (fmode & FWRITE)
		nd->nl_flags |= NLC_WRITE;
	if ((fmode & O_EXCL) == 0 && (fmode & O_NOFOLLOW) == 0)
		nd->nl_flags |= NLC_FOLLOW;

	if (fmode & O_CREAT) {
		/*
		 * CONDITIONAL CREATE FILE CASE
		 *
		 * Setting NLC_CREATE causes a negative hit to store
		 * the negative hit ncp and not return an error.  Then
		 * nc_error or nc_vp may be checked to see if the ncp
		 * represents a negative hit.  NLC_CREATE also requires
		 * write permission on the governing directory or EPERM
		 * is returned.
		 *
		 * If the file exists but is missing write permission,
		 * nlookup() returns EACCES. This has to be handled specially
		 * when combined with O_EXCL.
		 */
		nd->nl_flags |= NLC_CREATE;
		nd->nl_flags |= NLC_REFDVP;
		bwillinode(1);
		error = nlookup(nd);
		if (error == EACCES && nd->nl_nch.ncp->nc_vp != NULL &&
		    (fmode & O_EXCL) && !nd->nl_dir_error)
		{
			error = EEXIST;
		}

		/*
		 * If no error and nd->nl_dvp is NULL, the nlookup represents
		 * a mount-point or cross-mount situation.  e.g.
		 * open("/var/cache", O_CREAT), where /var/cache is a
		 * mount point or a null-mount point.
		 */
		if (error == 0 && nd->nl_dvp == NULL)
			error = EINVAL;
	} else {
		/*
		 * NORMAL OPEN FILE CASE
		 */
		error = nlookup(nd);
	}

	if (error)
		return (error);

	/*
	 * split case to allow us to re-resolve and retry the ncp in case
	 * we get ESTALE.
	 *
	 * (error is 0 on entry / retry)
	 */
again:
	/*
	 * Checks for (likely) filesystem-modifying cases and allows
	 * the filesystem to stall the front-end.
	 */
	if ((fmode & (FWRITE | O_TRUNC)) ||
	    ((fmode & O_CREAT) && nd->nl_nch.ncp->nc_vp == NULL)) {
		error = ncp_writechk(&nd->nl_nch);
		if (error)
			return error;
	}

	vpexcl = 1;
	if (fmode & O_CREAT) {
		if (nd->nl_nch.ncp->nc_vp == NULL) {
			VATTR_NULL(vap);
			vap->va_type = VREG;
			vap->va_mode = cmode;
			vap->va_fuseflags = fmode; /* FUSE */
			if (fmode & O_EXCL)
				vap->va_vaflags |= VA_EXCLUSIVE;
			error = VOP_NCREATE(&nd->nl_nch, nd->nl_dvp, &vp,
					    nd->nl_cred, vap);
			if (error)
				return (error);
			fmode &= ~O_TRUNC;
			/* locked vnode is returned */
		} else {
			if (fmode & O_EXCL) {
				error = EEXIST;
			} else {
				error = cache_vget(&nd->nl_nch, cred,
						    LK_EXCLUSIVE, &vp);
			}
			if (error)
				return (error);
			fmode &= ~O_CREAT;
		}
	} else {
		/*
		 * In most other cases a shared lock on the vnode is
		 * sufficient.  However, the O_RDWR case needs an
		 * exclusive lock if the vnode is executable.  The
		 * NLC_EXCLLOCK_IFEXEC and NCF_NOTX flags help resolve
		 * this.
		 *
		 * NOTE: If NCF_NOTX is not set, we do not know the
		 *	 the state of the 'x' bits and have to get
		 *	 an exclusive lock for the EXCLLOCK_IFEXEC case.
		 */
		if ((nd->nl_flags & NLC_SHAREDLOCK) &&
		    ((nd->nl_flags & NLC_EXCLLOCK_IFEXEC) == 0 ||
		     nd->nl_nch.ncp->nc_flag & NCF_NOTX)) {
			error = cache_vget(&nd->nl_nch, cred, LK_SHARED, &vp);
			vpexcl = 0;
		} else {
			error = cache_vget(&nd->nl_nch, cred,
					   LK_EXCLUSIVE, &vp);
		}
		if (error)
			return (error);
	}

	/*
	 * We have a locked vnode and ncp now.  Note that the ncp will
	 * be cleaned up by the caller if nd->nl_nch is left intact.
	 */
	if (vp->v_type == VLNK) {
		error = EMLINK;
		goto bad;
	}
	if (vp->v_type == VSOCK) {
		error = EOPNOTSUPP;
		goto bad;
	}
	if (vp->v_type != VDIR && (fmode & O_DIRECTORY)) {
		error = ENOTDIR;
		goto bad;
	}
	if ((fmode & O_CREAT) == 0) {
		if (fmode & (FWRITE | O_TRUNC)) {
			if (vp->v_type == VDIR) {
				error = EISDIR;
				goto bad;
			}

			/*
			 * Additional checks on vnode (does not substitute
			 * for ncp_writechk()).
			 */
			error = vn_writechk(vp);
			if (error) {
				/*
				 * Special stale handling, re-resolve the
				 * vnode.
				 */
				if (error == ESTALE) {
					u_int dummy_gen = 0;

					vput(vp);
					vp = NULL;
					if (vpexcl == 0) {
						cache_unlock(&nd->nl_nch);
						cache_lock(&nd->nl_nch);
					}
					cache_setunresolved(&nd->nl_nch);
					error = cache_resolve(&nd->nl_nch,
							      &dummy_gen,
							      cred);
					if (error == 0)
						goto again;
				}
				goto bad;
			}
		}
	}
	if (fmode & O_TRUNC) {
		vn_unlock(vp);				/* XXX */
		vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);	/* XXX */
		osize = vp->v_filesize;
		VATTR_NULL(vap);
		vap->va_size = 0;
		error = VOP_SETATTR_FP(vp, vap, cred, fp);
		if (error)
			goto bad;
		error = VOP_GETATTR(vp, vap);
		if (error)
			goto bad;
		mp = vq_vptomp(vp);
		VFS_ACCOUNT(mp, vap->va_uid, vap->va_gid, -osize);
	}

	/*
	 * Set or clear VNSWAPCACHE on the vp based on nd->nl_nch.ncp->nc_flag.
	 * These particular bits a tracked all the way from the root.
	 *
	 * NOTE: Might not work properly on NFS servers due to the
	 * disconnected namecache.
	 */
	flags = nd->nl_nch.ncp->nc_flag;
	if ((flags & (NCF_UF_CACHE | NCF_UF_PCACHE)) &&
	    (flags & (NCF_SF_NOCACHE | NCF_SF_PNOCACHE)) == 0) {
		vsetflags(vp, VSWAPCACHE);
	} else {
		vclrflags(vp, VSWAPCACHE);
	}

	/*
	 * Setup the fp so VOP_OPEN can override it.  No descriptor has been
	 * associated with the fp yet so we own it clean.
	 *
	 * f_nchandle inherits nl_nch.  This used to be necessary only for
	 * directories but now we do it unconditionally so f*() ops
	 * such as fchmod() can access the actual namespace that was
	 * used to open the file.
	 */
	if (fp) {
		if (nd->nl_flags & NLC_APPENDONLY)
			fmode |= FAPPENDONLY;
		fp->f_nchandle = nd->nl_nch;
		cache_zero(&nd->nl_nch);
		cache_unlock(&fp->f_nchandle);
	}

	/*
	 * Get rid of nl_nch.  vn_open does not return it (it returns the
	 * vnode or the file pointer).
	 *
	 * NOTE: We can't leave nl_nch locked through the VOP_OPEN anyway
	 *	 since the VOP_OPEN may block, e.g. on /dev/ttyd0
	 *
	 * NOTE: The VOP_OPEN() can replace the *fpp we supply with its own
	 *	 (it will fdrop/fhold), and can also set the *fpp up however
	 *	 it wants, not necessarily using DTYPE_VNODE.
	 */
	if (nd->nl_nch.ncp)
		cache_put(&nd->nl_nch);

	error = VOP_OPEN(vp, fmode, cred, fpp);
	fp = fpp ? *fpp : NULL;

	if (error) {
		/*
		 * setting f_ops to &badfileops will prevent the descriptor
		 * code from trying to close and release the vnode, since
		 * the open failed we do not want to call close.
		 */
		if (fp) {
			fp->f_data = NULL;
			fp->f_ops = &badfileops;
		}
		goto bad;
	}

#if 0
	/*
	 * Assert that VREG files have been setup for vmio.
	 */
	KASSERT(vp->v_type != VREG || vp->v_object != NULL,
		("vn_open: regular file was not VMIO enabled!"));
#endif

	/*
	 * Return the vnode.  XXX needs some cleaning up.  The vnode is
	 * only returned in the fp == NULL case.
	 *
	 * NOTE: vnode stored in fp may be different
	 */
	if (fp == NULL) {
		nd->nl_open_vp = vp;
		nd->nl_vp_fmode = fmode;
		if ((nd->nl_flags & NLC_LOCKVP) == 0)
			vn_unlock(vp);
	} else {
		vput(vp);
	}
	return (0);
bad:
	if (vp)
		vput(vp);
	return (error);
}

int
vn_opendisk(const char *devname, int fmode, struct vnode **vpp)
{
	struct vnode *vp;
	int error;

	if (strncmp(devname, "/dev/", 5) == 0)
		devname += 5;
	if ((vp = getsynthvnode(devname)) == NULL) {
		error = ENODEV;
	} else {
		error = VOP_OPEN(vp, fmode, proc0.p_ucred, NULL);
		vn_unlock(vp);
		if (error) {
			vrele(vp);
			vp = NULL;
		}
	}
	*vpp = vp;
	return (error);
}

/*
 * Checks for special conditions on the vnode which might prevent writing
 * after the vnode has (likely) been locked.  The vnode might or might not
 * be locked as of this call, but will be at least referenced.
 *
 * Also re-checks the mount RDONLY flag that ncp_writechk() checked prior
 * to the vnode being locked.
 */
int
vn_writechk(struct vnode *vp)
{
	/*
	 * If there's shared text associated with
	 * the vnode, try to free it up once.  If
	 * we fail, we can't allow writing.
	 */
	if (vp->v_flag & VTEXT)
		return (ETXTBSY);
	if (vp->v_mount && (vp->v_mount->mnt_flag & MNT_RDONLY))
		return (EROFS);
	return 0;
}

/*
 * Check whether the underlying mount is read-only.  The mount point
 * referenced by the namecache may be different from the mount point
 * used by the underlying vnode in the case of NULLFS, so a separate
 * check is needed.
 *
 * Must be called PRIOR to any vnodes being locked.
 */
int
ncp_writechk(struct nchandle *nch)
{
	struct mount *mp;

	if ((mp = nch->mount) != NULL) {
		if (mp->mnt_flag & MNT_RDONLY)
			return (EROFS);
		if (mp->mnt_op->vfs_modifying != vfs_stdmodifying)
			VFS_MODIFYING(mp);
	}
	return(0);
}

/*
 * Vnode close call
 *
 * MPSAFE
 */
int
vn_close(struct vnode *vp, int flags, struct file *fp)
{
	int error;

	error = vn_lock(vp, LK_SHARED | LK_RETRY | LK_FAILRECLAIM);
	if (error == 0) {
		error = VOP_CLOSE(vp, flags, fp);
		vn_unlock(vp);
	}
	vrele(vp);
	return (error);
}

/*
 * Sequential heuristic.
 *
 * MPSAFE (f_seqcount and f_nextoff are allowed to race)
 */
static __inline
int
sequential_heuristic(struct uio *uio, struct file *fp)
{
	/*
	 * Sequential heuristic - detect sequential operation
	 *
	 * NOTE: SMP: We allow f_seqcount updates to race.
	 */
	if ((uio->uio_offset == 0 && fp->f_seqcount > 0) ||
	    uio->uio_offset == fp->f_nextoff) {
		int tmpseq = fp->f_seqcount;

		tmpseq += howmany(uio->uio_resid, MAXBSIZE);
		if (tmpseq > IO_SEQMAX)
			tmpseq = IO_SEQMAX;
		fp->f_seqcount = tmpseq;
		return(fp->f_seqcount << IO_SEQSHIFT);
	}

	/*
	 * Not sequential, quick draw-down of seqcount
	 *
	 * NOTE: SMP: We allow f_seqcount updates to race.
	 */
	if (fp->f_seqcount > 1)
		fp->f_seqcount = 1;
	else
		fp->f_seqcount = 0;
	return(0);
}

/*
 * get - lock and return the f_offset field.
 * set - set and unlock the f_offset field.
 *
 * These routines serve the dual purpose of serializing access to the
 * f_offset field (at least on x86) and guaranteeing operational integrity
 * when multiple read()ers and write()ers are present on the same fp.
 *
 * MPSAFE
 */
static __inline off_t
vn_get_fpf_offset(struct file *fp)
{
	u_int	flags;
	u_int	nflags;

	/*
	 * Shortcut critical path.
	 */
	flags = fp->f_flag & ~FOFFSETLOCK;
	if (atomic_cmpset_int(&fp->f_flag, flags, flags | FOFFSETLOCK))
		return(fp->f_offset);

	/*
	 * The hard way
	 */
	for (;;) {
		flags = fp->f_flag;
		if (flags & FOFFSETLOCK) {
			nflags = flags | FOFFSETWAKE;
			tsleep_interlock(&fp->f_flag, 0);
			if (atomic_cmpset_int(&fp->f_flag, flags, nflags))
				tsleep(&fp->f_flag, PINTERLOCKED, "fpoff", 0);
		} else {
			nflags = flags | FOFFSETLOCK;
			if (atomic_cmpset_int(&fp->f_flag, flags, nflags))
				break;
		}
	}
	return(fp->f_offset);
}

/*
 * MPSAFE
 */
static __inline void
vn_set_fpf_offset(struct file *fp, off_t offset)
{
	u_int	flags;
	u_int	nflags;

	/*
	 * We hold the lock so we can set the offset without interference.
	 */
	fp->f_offset = offset;

	/*
	 * Normal release is already a reasonably critical path.
	 */
	for (;;) {
		flags = fp->f_flag;
		nflags = flags & ~(FOFFSETLOCK | FOFFSETWAKE);
		if (atomic_cmpset_int(&fp->f_flag, flags, nflags)) {
			if (flags & FOFFSETWAKE)
				wakeup(&fp->f_flag);
			break;
		}
	}
}

/*
 * MPSAFE
 */
static __inline off_t
vn_poll_fpf_offset(struct file *fp)
{
#if defined(__x86_64__)
	return(fp->f_offset);
#else
	off_t off = vn_get_fpf_offset(fp);
	vn_set_fpf_offset(fp, off);
	return(off);
#endif
}

/*
 * Package up an I/O request on a vnode into a uio and do it.
 *
 * MPSAFE
 */
int
vn_rdwr(enum uio_rw rw, struct vnode *vp, caddr_t base, int len,
	off_t offset, enum uio_seg segflg, int ioflg,
	struct ucred *cred, int *aresid)
{
	struct uio auio;
	struct iovec aiov;
	int error;

	if ((ioflg & IO_NODELOCKED) == 0)
		vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
	auio.uio_iov = &aiov;
	auio.uio_iovcnt = 1;
	aiov.iov_base = base;
	aiov.iov_len = len;
	auio.uio_resid = len;
	auio.uio_offset = offset;
	auio.uio_segflg = segflg;
	auio.uio_rw = rw;
	auio.uio_td = curthread;
	if (rw == UIO_READ) {
		error = VOP_READ(vp, &auio, ioflg, cred);
	} else {
		error = VOP_WRITE(vp, &auio, ioflg, cred);
	}
	if (aresid)
		*aresid = auio.uio_resid;
	else
		if (auio.uio_resid && error == 0)
			error = EIO;
	if ((ioflg & IO_NODELOCKED) == 0)
		vn_unlock(vp);
	return (error);
}

/*
 * Package up an I/O request on a vnode into a uio and do it.  The I/O
 * request is split up into smaller chunks and we try to avoid saturating
 * the buffer cache while potentially holding a vnode locked, so we
 * check bwillwrite() before calling vn_rdwr().  We also call lwkt_user_yield()
 * to give other processes a chance to lock the vnode (either other processes
 * core'ing the same binary, or unrelated processes scanning the directory).
 *
 * MPSAFE
 */
int
vn_rdwr_inchunks(enum uio_rw rw, struct vnode *vp, caddr_t base, int len,
		 off_t offset, enum uio_seg segflg, int ioflg,
		 struct ucred *cred, int *aresid)
{
	int error = 0;

	do {
		int chunk;

		/*
		 * Force `offset' to a multiple of MAXBSIZE except possibly
		 * for the first chunk, so that filesystems only need to
		 * write full blocks except possibly for the first and last
		 * chunks.
		 */
		chunk = MAXBSIZE - (uoff_t)offset % MAXBSIZE;

		if (chunk > len)
			chunk = len;
		if (vp->v_type == VREG && (ioflg & IO_RECURSE) == 0) {
			switch(rw) {
			case UIO_READ:
				bwillread(chunk);
				break;
			case UIO_WRITE:
				bwillwrite(chunk);
				break;
			}
		}
		error = vn_rdwr(rw, vp, base, chunk, offset, segflg,
				ioflg, cred, aresid);
		len -= chunk;	/* aresid calc already includes length */
		if (error)
			break;
		offset += chunk;
		base += chunk;
		lwkt_user_yield();
	} while (len);
	if (aresid)
		*aresid += len;
	return (error);
}

/*
 * File pointers can no longer get ripped up by revoke so
 * we don't need to lock access to the vp.
 *
 * f_offset updates are not guaranteed against multiple readers
 */
static int
vn_read(struct file *fp, struct uio *uio, struct ucred *cred, int flags)
{
	struct vnode *vp;
	int error, ioflag;

	KASSERT(uio->uio_td == curthread,
		("uio_td %p is not td %p", uio->uio_td, curthread));
	vp = (struct vnode *)fp->f_data;

	ioflag = 0;
	if (flags & O_FBLOCKING) {
		/* ioflag &= ~IO_NDELAY; */
	} else if (flags & O_FNONBLOCKING) {
		ioflag |= IO_NDELAY;
	} else if (fp->f_flag & FNONBLOCK) {
		ioflag |= IO_NDELAY;
	}
	if (fp->f_flag & O_DIRECT) {
		ioflag |= IO_DIRECT;
	}
	if ((flags & O_FOFFSET) == 0 && (vp->v_flag & VNOTSEEKABLE) == 0)
		uio->uio_offset = vn_get_fpf_offset(fp);
	vn_lock(vp, LK_SHARED | LK_RETRY);
	ioflag |= sequential_heuristic(uio, fp);

	error = VOP_READ_FP(vp, uio, ioflag, cred, fp);
	fp->f_nextoff = uio->uio_offset;
	vn_unlock(vp);
	if ((flags & O_FOFFSET) == 0 && (vp->v_flag & VNOTSEEKABLE) == 0)
		vn_set_fpf_offset(fp, uio->uio_offset);
	return (error);
}

/*
 * MPSAFE
 */
static int
vn_write(struct file *fp, struct uio *uio, struct ucred *cred, int flags)
{
	struct vnode *vp;
	int error, ioflag;

	KASSERT(uio->uio_td == curthread,
		("uio_td %p is not p %p", uio->uio_td, curthread));
	vp = (struct vnode *)fp->f_data;

	ioflag = IO_UNIT;
	if (vp->v_type == VREG &&
	   ((fp->f_flag & O_APPEND) || (flags & O_FAPPEND))) {
		ioflag |= IO_APPEND;
	}

	if (flags & O_FBLOCKING) {
		/* ioflag &= ~IO_NDELAY; */
	} else if (flags & O_FNONBLOCKING) {
		ioflag |= IO_NDELAY;
	} else if (fp->f_flag & FNONBLOCK) {
		ioflag |= IO_NDELAY;
	}
	if (fp->f_flag & O_DIRECT) {
		ioflag |= IO_DIRECT;
	}
	if (flags & O_FASYNCWRITE) {
		/* ioflag &= ~IO_SYNC; */
	} else if (flags & O_FSYNCWRITE) {
		ioflag |= IO_SYNC;
	} else if (fp->f_flag & O_FSYNC) {
		ioflag |= IO_SYNC;
	}

	if (vp->v_mount && (vp->v_mount->mnt_flag & MNT_SYNCHRONOUS))
		ioflag |= IO_SYNC;
	if ((flags & O_FOFFSET) == 0)
		uio->uio_offset = vn_get_fpf_offset(fp);
	if (vp->v_mount)
		VFS_MODIFYING(vp->v_mount);
	vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
	ioflag |= sequential_heuristic(uio, fp);
	error = VOP_WRITE_FP(vp, uio, ioflag, cred, fp);
	fp->f_nextoff = uio->uio_offset;
	vn_unlock(vp);
	if ((flags & O_FOFFSET) == 0)
		vn_set_fpf_offset(fp, uio->uio_offset);
	return (error);
}

/*
 * MPSAFE
 */
static int
vn_statfile(struct file *fp, struct stat *sb, struct ucred *cred)
{
	struct vnode *vp;
	int error;

	vp = (struct vnode *)fp->f_data;
	error = vn_stat(vp, sb, cred);
	return (error);
}

/*
 * MPSAFE
 */
int
vn_stat(struct vnode *vp, struct stat *sb, struct ucred *cred)
{
	struct vattr vattr;
	struct vattr *vap;
	int error;
	u_short mode;
	cdev_t dev;

	/*
	 * vp already has a ref and is validated, can call unlocked.
	 */
	vap = &vattr;
	error = VOP_GETATTR(vp, vap);
	if (error)
		return (error);

	/*
	 * Zero the spare stat fields
	 */
	sb->st_lspare = 0;
	sb->st_qspare2 = 0;

	/*
	 * Copy from vattr table
	 */
	if (vap->va_fsid != VNOVAL)
		sb->st_dev = vap->va_fsid;
	else
		sb->st_dev = vp->v_mount->mnt_stat.f_fsid.val[0];
	sb->st_ino = vap->va_fileid;
	mode = vap->va_mode;
	switch (vap->va_type) {
	case VREG:
		mode |= S_IFREG;
		break;
	case VDATABASE:
		mode |= S_IFDB;
		break;
	case VDIR:
		mode |= S_IFDIR;
		break;
	case VBLK:
		mode |= S_IFBLK;
		break;
	case VCHR:
		mode |= S_IFCHR;
		break;
	case VLNK:
		mode |= S_IFLNK;
		/* This is a cosmetic change, symlinks do not have a mode. */
		if (vp->v_mount->mnt_flag & MNT_NOSYMFOLLOW)
			sb->st_mode &= ~ACCESSPERMS;	/* 0000 */
		else
			sb->st_mode |= ACCESSPERMS;	/* 0777 */
		break;
	case VSOCK:
		mode |= S_IFSOCK;
		break;
	case VFIFO:
		mode |= S_IFIFO;
		break;
	default:
		return (EBADF);
	}
	sb->st_mode = mode;
	if (vap->va_nlink > (nlink_t)-1)
		sb->st_nlink = (nlink_t)-1;
	else
		sb->st_nlink = vap->va_nlink;
	sb->st_uid = vap->va_uid;
	sb->st_gid = vap->va_gid;
	sb->st_rdev = devid_from_dev(vp->v_rdev);
	sb->st_size = vap->va_size;
	sb->st_atimespec = vap->va_atime;
	sb->st_mtimespec = vap->va_mtime;
	sb->st_ctimespec = vap->va_ctime;

	/*
	 * A VCHR and VBLK device may track the last access and last modified
	 * time independantly of the filesystem.  This is particularly true
	 * because device read and write calls may bypass the filesystem.
	 */
	if (vp->v_type == VCHR || vp->v_type == VBLK) {
		dev = vp->v_rdev;
		if (dev != NULL) {
			if (dev->si_lastread) {
				sb->st_atimespec.tv_sec = time_second +
							  (dev->si_lastread -
							   time_uptime);
				sb->st_atimespec.tv_nsec = 0;
			}
			if (dev->si_lastwrite) {
				sb->st_mtimespec.tv_sec = time_second +
							  (dev->si_lastwrite -
							   time_uptime);
				sb->st_mtimespec.tv_nsec = 0;
			}
		}
	}

        /*
	 * According to www.opengroup.org, the meaning of st_blksize is
	 *   "a filesystem-specific preferred I/O block size for this
	 *    object.  In some filesystem types, this may vary from file
	 *    to file"
	 * Default to PAGE_SIZE after much discussion.
	 */

	if (vap->va_type == VREG) {
		sb->st_blksize = vap->va_blocksize;
	} else if (vn_isdisk(vp, NULL)) {
		/*
		 * XXX this is broken.  If the device is not yet open (aka
		 * stat() call, aka v_rdev == NULL), how are we supposed
		 * to get a valid block size out of it?
		 */
		dev = vp->v_rdev;

		sb->st_blksize = dev->si_bsize_best;
		if (sb->st_blksize < dev->si_bsize_phys)
			sb->st_blksize = dev->si_bsize_phys;
		if (sb->st_blksize < BLKDEV_IOSIZE)
			sb->st_blksize = BLKDEV_IOSIZE;
	} else {
		sb->st_blksize = PAGE_SIZE;
	}

	sb->st_flags = vap->va_flags;

	error = caps_priv_check(cred, SYSCAP_NOVFS_GENERATION);
	if (error)
		sb->st_gen = 0;
	else
		sb->st_gen = (u_int32_t)vap->va_gen;

	sb->st_blocks = vap->va_bytes / S_BLKSIZE;

	/*
	 * This is for ABI compatibility <= 5.7 (for ABI change made in
	 * 5.7 master).
	 */
	sb->__old_st_blksize = sb->st_blksize;

	return (0);
}

/*
 * MPALMOSTSAFE - acquires mplock
 */
static int
vn_ioctl(struct file *fp, u_long com, caddr_t data, struct ucred *ucred,
	 struct sysmsg *msg)
{
	struct vnode *vp = ((struct vnode *)fp->f_data);
	struct vnode *ovp;
	struct vattr vattr;
	int error;
	off_t size;

	switch (vp->v_type) {
	case VREG:
	case VDIR:
		if (com == FIONREAD) {
			error = VOP_GETATTR(vp, &vattr);
			if (error)
				break;
			size = vattr.va_size;
			if ((vp->v_flag & VNOTSEEKABLE) == 0)
				size -= vn_poll_fpf_offset(fp);
			if (size > 0x7FFFFFFF)
				size = 0x7FFFFFFF;
			*(int *)data = size;
			error = 0;
			break;
		}
		if (com == FIOASYNC) {				/* XXX */
			error = 0;				/* XXX */
			break;
		}
		/* fall into ... */
	default:
#if 0
		return (ENOTTY);
#endif
	case VFIFO:
	case VCHR:
	case VBLK:
		if (com == FIODTYPE) {
			if (vp->v_type != VCHR && vp->v_type != VBLK) {
				error = ENOTTY;
				break;
			}
			*(int *)data = dev_dflags(vp->v_rdev) & D_TYPEMASK;
			error = 0;
			break;
		}
		error = VOP_IOCTL(vp, com, data, fp->f_flag, ucred, msg);
		if (error == 0 && com == TIOCSCTTY) {
			struct proc *p = curthread->td_proc;
			struct session *sess;

			if (p == NULL) {
				error = ENOTTY;
				break;
			}

			get_mplock();
			sess = p->p_session;
			/* Do nothing if reassigning same control tty */
			if (sess->s_ttyvp == vp) {
				error = 0;
				rel_mplock();
				break;
			}

			/* Get rid of reference to old control tty */
			ovp = sess->s_ttyvp;
			vref(vp);
			sess->s_ttyvp = vp;
			if (ovp)
				vrele(ovp);
			rel_mplock();
		}
		break;
	}
	return (error);
}

/*
 * Obtain the requested vnode lock
 *
 *	LK_RETRY	Automatically retry on timeout
 *	LK_FAILRECLAIM	Fail if the vnode is being reclaimed
 *
 * Failures will occur if the vnode is undergoing recyclement, but not
 * all callers expect that the function will fail so the caller must pass
 * LK_FAILOK if it wants to process an error code.
 *
 * Errors can occur for other reasons if you pass in other LK_ flags,
 * regardless of whether you pass in LK_FAILRECLAIM
 */
int
vn_lock(struct vnode *vp, int flags)
{
	int error;

	do {
		error = lockmgr(&vp->v_lock, flags);
		if (error == 0)
			break;
	} while (flags & LK_RETRY);

	/*
	 * Because we (had better!) have a ref on the vnode, once it
	 * goes to VRECLAIMED state it will not be recycled until all
	 * refs go away.  So we can just check the flag.
	 */
	if (error == 0 && (vp->v_flag & VRECLAIMED)) {
		if (flags & LK_FAILRECLAIM) {
			lockmgr(&vp->v_lock, LK_RELEASE);
			error = ENOENT;
		}
	}
	return (error);
}

int
vn_relock(struct vnode *vp, int flags)
{
	int error;

	do {
		error = lockmgr(&vp->v_lock, flags);
		if (error == 0)
			break;
	} while (flags & LK_RETRY);

	return error;
}

#ifdef DEBUG_VN_UNLOCK

void
debug_vn_unlock(struct vnode *vp, const char *filename, int line)
{
	kprintf("vn_unlock from %s:%d\n", filename, line);
	lockmgr(&vp->v_lock, LK_RELEASE);
}

#else

void
vn_unlock(struct vnode *vp)
{
	lockmgr(&vp->v_lock, LK_RELEASE);
}

#endif

/*
 * MPSAFE
 */
int
vn_islocked(struct vnode *vp)
{
	return (lockstatus(&vp->v_lock, curthread));
}

/*
 * Return the lock status of a vnode and unlock the vnode
 * if we owned the lock.  This is not a boolean, if the
 * caller cares what the lock status is the caller must
 * check the various possible values.
 *
 * This only unlocks exclusive locks held by the caller,
 * it will NOT unlock shared locks (there is no way to
 * tell who the shared lock belongs to).
 *
 * MPSAFE
 */
int
vn_islocked_unlock(struct vnode *vp)
{
	int vpls;

	vpls = lockstatus(&vp->v_lock, curthread);
	if (vpls == LK_EXCLUSIVE)
		lockmgr(&vp->v_lock, LK_RELEASE);
	return(vpls);
}

/*
 * Restore a vnode lock that we previously released via
 * vn_islocked_unlock().  This is a NOP if we did not
 * own the original lock.
 *
 * MPSAFE
 */
void
vn_islocked_relock(struct vnode *vp, int vpls)
{
	int error;

	if (vpls == LK_EXCLUSIVE)
		error = lockmgr(&vp->v_lock, vpls);
}

/*
 * MPSAFE
 */
static int
vn_closefile(struct file *fp)
{
	int error;

	fp->f_ops = &badfileops;
	error = vn_close(((struct vnode *)fp->f_data), fp->f_flag, fp);
	return (error);
}

/*
 * MPSAFE
 */
static int
vn_kqfilter(struct file *fp, struct knote *kn)
{
	int error;

	error = VOP_KQFILTER(((struct vnode *)fp->f_data), kn);
	return (error);
}

int
vn_bmap_seekhole_locked(struct vnode *vp, u_long cmd, off_t *off,
    struct ucred *cred)
{
	struct vattr vattr;
	off_t size;
	uint64_t bsize;
	off_t noff, doff;
	int error;

	KASSERT(cmd == FIOSEEKHOLE || cmd == FIOSEEKDATA,
	    ("%s: Wrong command %lu", __func__, cmd));
	KKASSERT(vn_islocked(vp) == LK_EXCLUSIVE);

	if (vp->v_type != VREG) {
		error = ENOTTY;
		goto out;
	}
	error = VOP_GETATTR(vp, &vattr);
	if (vattr.va_size <= OFF_MAX)
		size = vattr.va_size;
	else
		error = EFBIG;
	if (error != 0)
		goto out;
	noff = *off;
	if (noff < 0 || noff >= size) {
		error = ENXIO;
		goto out;
	}

	vm_object_page_clean(vp->v_object, 0, 0, OBJPC_SYNC);

	bsize = vp->v_mount->mnt_stat.f_iosize;
	for (; noff < size; noff += bsize - noff % bsize) {
		error = VOP_BMAP(vp, noff, &doff, NULL, NULL, BUF_CMD_SEEK);
		if (error == EOPNOTSUPP) {
			error = ENOTTY;
			goto out;
		}
		if ((doff == NOOFFSET && cmd == FIOSEEKHOLE) ||
		    (doff != NOOFFSET && cmd == FIOSEEKDATA)) {
			if (noff < *off)
				noff = *off;
			goto out;
		}
	}
	if (noff > size)
		noff = size;
	/* noff == size. There is an implicit hole at the end of file. */
	if (cmd == FIOSEEKDATA)
		error = ENXIO;
out:
	if (error == 0)
		*off = noff;
	return (error);
}

int
vn_bmap_seekhole(struct vnode *vp, u_long cmd, off_t *off, struct ucred *cred)
{
	int error;

	KASSERT(cmd == FIOSEEKHOLE || cmd == FIOSEEKDATA,
	    ("%s: Wrong command %lu", __func__, cmd));

	if (vn_lock(vp, LK_EXCLUSIVE) != 0)
		return (EBADF);
	error = vn_bmap_seekhole_locked(vp, cmd, off, cred);
	vn_unlock(vp);
	return (error);
}

int
vn_seek(struct file *fp, off_t offset, int whence, off_t *res)
{
	/*
	 * NOTE: devfs_dev_fileops uses exact same code
	 */
	struct thread *td = curthread;
	struct vnode *vp;
	struct vattr_lite lva;
	off_t new_offset;
	int error;

	vp = (struct vnode *)fp->f_data;

	switch (whence) {
	case L_INCR:
		spin_lock(&fp->f_spin);
		new_offset = fp->f_offset + offset;
		error = 0;
		break;
	case L_XTND:
		error = VOP_GETATTR_LITE(vp, &lva);
		spin_lock(&fp->f_spin);
		new_offset = offset + lva.va_size;
		break;
	case L_SET:
		new_offset = offset;
		error = 0;
		spin_lock(&fp->f_spin);
		break;
	case SEEK_DATA:
		error = fo_ioctl(fp, FIOSEEKDATA, (caddr_t)&offset,
		    td->td_ucred, NULL);
		if (error == ENOTTY)
			error = EINVAL;
		new_offset = offset;
		spin_lock(&fp->f_spin);
		break;
	case SEEK_HOLE:
		error = fo_ioctl(fp, FIOSEEKHOLE, (caddr_t)&offset,
		    td->td_ucred, NULL);
		if (error == ENOTTY)
			error = EINVAL;
		new_offset = offset;
		spin_lock(&fp->f_spin);
		break;
	default:
		new_offset = 0;
		error = EINVAL;
		spin_lock(&fp->f_spin);
		break;
	}

	/*
	 * Validate the seek position.  Negative offsets are not allowed
	 * for regular files or directories.
	 *
	 * Normally we would also not want to allow negative offsets for
	 * character and block-special devices.  However kvm addresses
	 * on 64 bit architectures might appear to be negative and must
	 * be allowed.
	 */
	if (error == 0) {
		if (new_offset < 0 &&
		    (vp->v_type == VREG || vp->v_type == VDIR)) {
			error = EINVAL;
		} else {
			fp->f_offset = new_offset;
		}
	}
	*res = fp->f_offset;
	spin_unlock(&fp->f_spin);

	return (error);
}