DragonFlyBSD Kernel Audit
sys/vfs/nfs/nfs_syscalls.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
/*
 * Copyright (c) 1989, 1993
 *	The Regents of the University of California.  All rights reserved.
 *
 * This code is derived from software contributed to Berkeley by
 * Rick Macklem at The University of Guelph.
 *
 * 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.
 *
 *	@(#)nfs_syscalls.c	8.5 (Berkeley) 3/30/95
 * $FreeBSD: src/sys/nfs/nfs_syscalls.c,v 1.58.2.1 2000/11/26 02:30:06 dillon Exp $
 */

#include <sys/param.h>
#include <sys/systm.h>
#include <sys/sysmsg.h>
#include <sys/kernel.h>
#include <sys/sysctl.h>
#include <sys/file.h>
#include <sys/filedesc.h>
#include <sys/vnode.h>
#include <sys/malloc.h>
#include <sys/mount.h>
#include <sys/proc.h>
#include <sys/caps.h>
#include <sys/buf.h>
#include <sys/mbuf.h>
#include <sys/resourcevar.h>
#include <sys/socket.h>
#include <sys/socketvar.h>
#include <sys/domain.h>
#include <sys/protosw.h>
#include <sys/nlookup.h>

#include <sys/mutex2.h>
#include <sys/thread2.h>

#include <netinet/in.h>
#include <netinet/tcp.h>
#include "xdr_subs.h"
#include "rpcv2.h"
#include "nfsproto.h"
#include "nfs.h"
#include "nfsm_subs.h"
#include "nfsrvcache.h"
#include "nfsmount.h"
#include "nfsnode.h"
#include "nfsrtt.h"

static MALLOC_DEFINE(M_NFSSVC, "NFS srvsock", "Nfs server structure");

static int nuidhash_max = NFS_MAXUIDHASH;

#ifndef NFS_NOSERVER
static void	nfsrv_zapsock (struct nfssvc_sock *slp);
#endif

#define	TRUE	1
#define	FALSE	0

SYSCTL_DECL(_vfs_nfs);

#ifndef NFS_NOSERVER
int nfsd_waiting = 0;
static struct nfsdrt nfsdrt;
static int nfs_numnfsd = 0;
static void	nfsd_rt (int sotype, struct nfsrv_descript *nd,
			     int cacherep);
static int	nfssvc_addsock (struct file *, struct sockaddr *,
				    struct thread *);
static int	nfssvc_nfsd (struct nfsd_srvargs *,caddr_t,struct thread *);

static int nfs_privport = 0;
SYSCTL_INT(_vfs_nfs, NFS_NFSPRIVPORT, nfs_privport, CTLFLAG_RW, &nfs_privport,
    0, "Enable privileged source port checks");
SYSCTL_INT(_vfs_nfs, OID_AUTO, gatherdelay, CTLFLAG_RW, &nfsrvw_procrastinate, 0,
    "Enable NFS request procrastination");
SYSCTL_INT(_vfs_nfs, OID_AUTO, gatherdelay_v3, CTLFLAG_RW, &nfsrvw_procrastinate_v3, 0,
    "Enable NFSv3 request procrastination");
int	nfs_soreserve = NFS_MAXPACKET * NFS_MAXASYNCBIO;
SYSCTL_INT(_vfs_nfs, OID_AUTO, soreserve, CTLFLAG_RW, &nfs_soreserve, 0,
    "Minimum NFS socket buffer size reservation");

/*
 * NFS server system calls
 */

#endif /* NFS_NOSERVER */
/*
 * nfssvc_args(int flag, caddr_t argp)
 *
 * Nfs server psuedo system call for the nfsd's
 * Based on the flag value it either:
 * - adds a socket to the selection list
 * - remains in the kernel as an nfsd
 * - remains in the kernel as an nfsiod
 *
 * MPALMOSTSAFE
 */
int
sys_nfssvc(struct sysmsg *sysmsg, const struct nfssvc_args *uap)
{
#ifndef NFS_NOSERVER
	struct nlookupdata nd;
	struct file *fp;
	struct sockaddr *nam;
	struct nfsd_args nfsdarg;
	struct nfsd_srvargs nfsd_srvargs, *nsd = &nfsd_srvargs;
	struct nfsd_cargs ncd;
	struct nfsd *nfsd;
	struct nfssvc_sock *slp;
	struct nfsuid *nuidp;
	struct nfsmount *nmp;
	struct vnode *vp;
#endif /* NFS_NOSERVER */
	int error;
	struct thread *td = curthread;

	/*
	 * Must be super user
	 */
	error = caps_priv_check_td(td, SYSCAP_RESTRICTEDROOT);
	if (error)
		return (error);

	lwkt_gettoken(&nfs_token);

	while (nfssvc_sockhead_flag & SLP_INIT) {
		nfssvc_sockhead_flag |= SLP_WANTINIT;
		tsleep((caddr_t)&nfssvc_sockhead, 0, "nfsd init", 0);
	}
	if (uap->flag & NFSSVC_BIOD)
		error = ENXIO;		/* no longer need nfsiod's */
#ifdef NFS_NOSERVER
	else
		error = ENXIO;
#else /* !NFS_NOSERVER */
	else if (uap->flag & NFSSVC_MNTD) {
		error = copyin(uap->argp, (caddr_t)&ncd, sizeof (ncd));
		if (error)
			goto done;
		vp = NULL;
		error = nlookup_init(&nd, ncd.ncd_dirp, UIO_USERSPACE,
					NLC_FOLLOW);
		if (error == 0)
			error = nlookup(&nd);
		if (error == 0)
			error = cache_vget(&nd.nl_nch, nd.nl_cred, LK_EXCLUSIVE, &vp);
		nlookup_done(&nd);
		if (error)
			goto done;

		if ((vp->v_flag & VROOT) == 0)
			error = EINVAL;
		nmp = VFSTONFS(vp->v_mount);
		vput(vp);
		if (error)
			goto done;
		if ((nmp->nm_state & NFSSTA_MNTD) &&
			(uap->flag & NFSSVC_GOTAUTH) == 0) {
			error = 0;
			goto done;
		}
		nmp->nm_state |= NFSSTA_MNTD;
		error = nfs_clientd(nmp, td->td_ucred, &ncd, uap->flag,
				    uap->argp, td);
	} else if (uap->flag & NFSSVC_ADDSOCK) {
		error = copyin(uap->argp, (caddr_t)&nfsdarg, sizeof(nfsdarg));
		if (error)
			goto done;
		error = holdsock(td, nfsdarg.sock, &fp);
		if (error)
			goto done;
		/*
		 * Get the client address for connected sockets.
		 */
		if (nfsdarg.name == NULL || nfsdarg.namelen == 0)
			nam = NULL;
		else {
			error = getsockaddr(&nam, nfsdarg.name,
					    nfsdarg.namelen);
			if (error) {
				fdrop(fp);
				goto done;
			}
		}
		error = nfssvc_addsock(fp, nam, td);
		fdrop(fp);
	} else {
		error = copyin(uap->argp, (caddr_t)nsd, sizeof (*nsd));
		if (error)
			goto done;
		if ((uap->flag & NFSSVC_AUTHIN) &&
		    ((nfsd = nsd->nsd_nfsd)) != NULL &&
		    (nfsd->nfsd_slp->ns_flag & SLP_VALID)) {
			slp = nfsd->nfsd_slp;

			/*
			 * First check to see if another nfsd has already
			 * added this credential.
			 */
			for (nuidp = NUIDHASH(slp,nsd->nsd_cr.cr_uid)->lh_first;
			    nuidp != 0; nuidp = nuidp->nu_hash.le_next) {
				if (nuidp->nu_cr.cr_uid == nsd->nsd_cr.cr_uid &&
				    (!nfsd->nfsd_nd->nd_nam2 ||
				     netaddr_match(AF_INET,
				     &nuidp->nu_haddr, nfsd->nfsd_nd->nd_nam2)))
					break;
			}
			if (nuidp) {
			    nfsrv_setcred(&nuidp->nu_cr,&nfsd->nfsd_nd->nd_cr);
			    nfsd->nfsd_nd->nd_flag |= ND_KERBFULL;
			} else {
			    /*
			     * Nope, so we will.
			     */
			    if (slp->ns_numuids < nuidhash_max) {
				slp->ns_numuids++;
				nuidp = (struct nfsuid *)
				   kmalloc(sizeof (struct nfsuid), M_NFSUID,
					M_WAITOK);
			    } else
				nuidp = NULL;
			    if ((slp->ns_flag & SLP_VALID) == 0) {
				if (nuidp)
				    kfree((caddr_t)nuidp, M_NFSUID);
			    } else {
				if (nuidp == NULL) {
				    nuidp = TAILQ_FIRST(&slp->ns_uidlruhead);
				    LIST_REMOVE(nuidp, nu_hash);
				    TAILQ_REMOVE(&slp->ns_uidlruhead, nuidp,
					nu_lru);
				    if (nuidp->nu_flag & NU_NAM)
					kfree(nuidp->nu_nam, M_SONAME);
			        }
				nuidp->nu_flag = 0;
				nuidp->nu_cr = nsd->nsd_cr;
				if (nuidp->nu_cr.cr_ngroups > NGROUPS)
				    nuidp->nu_cr.cr_ngroups = NGROUPS;
				nuidp->nu_cr.cr_ref = 1;
				nuidp->nu_timestamp = nsd->nsd_timestamp;
				nuidp->nu_expire = time_uptime + nsd->nsd_ttl;
				/*
				 * and save the session key in nu_key.
				 */
				bcopy(nsd->nsd_key, nuidp->nu_key,
				    sizeof (nsd->nsd_key));
				if (nfsd->nfsd_nd->nd_nam2) {
				    struct sockaddr_in *saddr;

				    saddr = (struct sockaddr_in *)
					    nfsd->nfsd_nd->nd_nam2;
				    switch (saddr->sin_family) {
				    case AF_INET:
					nuidp->nu_flag |= NU_INETADDR;
					nuidp->nu_inetaddr =
					     saddr->sin_addr.s_addr;
					break;
				    default:
					nuidp->nu_flag |= NU_NAM;
					nuidp->nu_nam =
					  dup_sockaddr(nfsd->nfsd_nd->nd_nam2);
					break;
				    };
				}
				TAILQ_INSERT_TAIL(&slp->ns_uidlruhead, nuidp,
					nu_lru);
				LIST_INSERT_HEAD(NUIDHASH(slp, nsd->nsd_uid),
					nuidp, nu_hash);
				nfsrv_setcred(&nuidp->nu_cr,
				    &nfsd->nfsd_nd->nd_cr);
				nfsd->nfsd_nd->nd_flag |= ND_KERBFULL;
			    }
			}
		}
		if ((uap->flag & NFSSVC_AUTHINFAIL) && (nfsd = nsd->nsd_nfsd))
			nfsd->nfsd_flag |= NFSD_AUTHFAIL;
		error = nfssvc_nfsd(nsd, uap->argp, td);
	}
#endif /* NFS_NOSERVER */
	if (error == EINTR || error == ERESTART)
		error = 0;
done:
	lwkt_reltoken(&nfs_token);
	return (error);
}

#ifndef NFS_NOSERVER
/*
 * Adds a socket to the list for servicing by nfsds.
 */
static int
nfssvc_addsock(struct file *fp, struct sockaddr *mynam, struct thread *td)
{
	int siz;
	struct nfssvc_sock *slp;
	struct socket *so;
	int error;

	so = (struct socket *)fp->f_data;
#if 0
	tslp = NULL;
	/*
	 * Add it to the list, as required.
	 */
	if (so->so_proto->pr_protocol == IPPROTO_UDP) {
		tslp = nfs_udpsock;
		if (tslp->ns_flag & SLP_VALID) {
			if (mynam != NULL)
				kfree(mynam, M_SONAME);
			return (EPERM);
		}
	}
#endif
	/*
	 * Reserve buffer space in the socket.  Note that due to bugs in
	 * Linux's delayed-ack code, serious performance degredation may
	 * occur with linux hosts if the minimum is used.
	 *
	 * NFS sockets are not limited to the standard sb_max or by
	 * resource limits.
	 */
	if (so->so_type == SOCK_STREAM)
		siz = NFS_MAXPACKET + sizeof (u_long);
	else
		siz = NFS_MAXPACKET;
	if (siz < nfs_soreserve)
	    siz = nfs_soreserve;

	error = soreserve(so, siz, siz, NULL);
	if (error) {
		if (mynam != NULL)
			kfree(mynam, M_SONAME);
		return (error);
	}

	/*
	 * Set protocol specific options { for now TCP only } and
	 * reserve some space. For datagram sockets, this can get called
	 * repeatedly for the same socket, but that isn't harmful.
	 */
	if (so->so_type == SOCK_STREAM) {
		struct sockopt sopt;
		int val;

		bzero(&sopt, sizeof sopt);
		sopt.sopt_level = SOL_SOCKET;
		sopt.sopt_name = SO_KEEPALIVE;
		sopt.sopt_val = &val;
		sopt.sopt_valsize = sizeof val;
		val = 1;
		sosetopt(so, &sopt);
	}
	if (so->so_proto->pr_domain->dom_family == AF_INET &&
	    so->so_proto->pr_protocol == IPPROTO_TCP) {
		struct sockopt sopt;
		int val;

		bzero(&sopt, sizeof sopt);
		sopt.sopt_level = IPPROTO_TCP;
		sopt.sopt_name = TCP_NODELAY;
		sopt.sopt_val = &val;
		sopt.sopt_valsize = sizeof val;
		val = 1;
		sosetopt(so, &sopt);

		bzero(&sopt, sizeof sopt);
		sopt.sopt_level = IPPROTO_TCP;
		sopt.sopt_name = TCP_FASTKEEP;
		sopt.sopt_val = &val;
		sopt.sopt_valsize = sizeof val;
		val = 1;
		sosetopt(so, &sopt);
	}
	atomic_clear_int(&so->so_rcv.ssb_flags, SSB_NOINTR);
	so->so_rcv.ssb_timeo = 0;
	atomic_clear_int(&so->so_snd.ssb_flags, SSB_NOINTR);
	so->so_snd.ssb_timeo = 0;

	/*
	 * Clear AUTOSIZE, otherwise the socket buffer could be reduced
	 * to the point where rpc's cannot be queued using the mbuf
	 * interface.
	 */
	atomic_clear_int(&so->so_rcv.ssb_flags, SSB_AUTOSIZE);
	atomic_clear_int(&so->so_snd.ssb_flags, SSB_AUTOSIZE);

	slp = kmalloc(sizeof (struct nfssvc_sock), M_NFSSVC, M_WAITOK | M_ZERO);
	mtx_init(&slp->ns_solock, "nfsvc");
	STAILQ_INIT(&slp->ns_rec);
	TAILQ_INIT(&slp->ns_uidlruhead);
	lwkt_token_init(&slp->ns_token, "nfssrv_token");

	lwkt_gettoken(&nfs_token);
	nfsrv_slpref(slp);
	TAILQ_INSERT_TAIL(&nfssvc_sockhead, slp, ns_chain);
	lwkt_gettoken(&slp->ns_token);

	slp->ns_so = so;
	slp->ns_nam = mynam;
	fhold(fp);
	slp->ns_fp = fp;

	so->so_upcallarg = (caddr_t)slp;
	so->so_upcall = nfsrv_rcv_upcall;
	atomic_set_int(&so->so_rcv.ssb_flags, SSB_UPCALL);
	slp->ns_flag = (SLP_VALID | SLP_NEEDQ);
	nfsrv_wakenfsd(slp, 1);

	lwkt_reltoken(&slp->ns_token);
	lwkt_reltoken(&nfs_token);

	return (0);
}

/*
 * Called by nfssvc() for nfsds. Just loops around servicing rpc requests
 * until it is killed by a signal.
 */
static int
nfssvc_nfsd(struct nfsd_srvargs *nsd, caddr_t argp, struct thread *td)
{
	int siz;
	struct nfssvc_sock *slp;
	struct nfsd *nfsd = nsd->nsd_nfsd;
	struct nfsrv_descript *nd = NULL;
	struct mbuf *m, *mreq;
	int error, cacherep, sotype, writes_todo;
	int procrastinate;
	int slplocked;
	u_quad_t cur_usec;

#ifndef nolint
	cacherep = RC_DOIT;
	writes_todo = 0;
#endif
	lwkt_gettoken(&nfs_token);

	if (nfsd == NULL) {
		nsd->nsd_nfsd = nfsd = (struct nfsd *)
			kmalloc(sizeof (struct nfsd), M_NFSD, M_WAITOK|M_ZERO);
		nfsd->nfsd_td = td;
		TAILQ_INSERT_TAIL(&nfsd_head, nfsd, nfsd_chain);
		nfs_numnfsd++;
	}

	/*
	 * Loop getting rpc requests until SIGKILL.
	 */
	for (;;) {
		if ((nfsd->nfsd_flag & NFSD_REQINPROG) == 0) {
			while (nfsd->nfsd_slp == NULL &&
			    (nfsd_head_flag & NFSD_CHECKSLP) == 0) {
				nfsd->nfsd_flag |= NFSD_WAITING;
				nfsd_waiting++;
				error = tsleep(nfsd, PCATCH, "nfsd", 0);
				nfsd_waiting--;
				if (error && nfsd->nfsd_slp == NULL)
					goto done;
			}
			if (nfsd->nfsd_slp == NULL &&
			    (nfsd_head_flag & NFSD_CHECKSLP)) {
				TAILQ_FOREACH(slp, &nfssvc_sockhead, ns_chain) {
				    if ((slp->ns_flag & SLP_ACTION_MASK) ||
					slp->ns_needq_upcall) {
					    nfsrv_slpref(slp);
					    nfsd->nfsd_slp = slp;
					    break;
				    }
				}
				if (slp == NULL)
					nfsd_head_flag &= ~NFSD_CHECKSLP;
			}
			if ((slp = nfsd->nfsd_slp) == NULL)
				continue;

			lwkt_reltoken(&nfs_token);
			lwkt_gettoken(&slp->ns_token);

			if (slp->ns_needq_upcall) {
				slp->ns_needq_upcall = 0;
				slp->ns_flag |= SLP_NEEDQ;
			}

			if (slp->ns_flag & SLP_VALID) {
				/*
				 * We can both process additional received
				 * data into new records and process existing
				 * records.  This keeps the pipeline hot by
				 * allowing the tcp socket to continue to
				 * drain while we are processing records.
				 */
				while (slp->ns_flag & (SLP_DISCONN|SLP_NEEDQ)) {
					if (slp->ns_flag & SLP_DISCONN) {
						nfsrv_zapsock(slp);
						break;
					}
					if (slp->ns_flag & SLP_NEEDQ) {
						nfs_slplock(slp, 1);
						if ((slp->ns_flag &
						     SLP_NEEDQ) == 0) {
							nfs_slpunlock(slp);
							continue;
						}
						nfsrv_rcv(slp->ns_so,
							  (caddr_t)slp,
							  M_WAITOK);
						nfs_slpunlock(slp);
						break;
					}
				}
				error = nfsrv_dorec(slp, nfsd, &nd);
				cur_usec = nfs_curusec();
				if (error && slp->ns_tq.lh_first &&
				    slp->ns_tq.lh_first->nd_time <= cur_usec) {
					error = 0;
					cacherep = RC_DOIT;
					writes_todo = 1;
				} else {
					writes_todo = 0;
				}
				nfsd->nfsd_flag |= NFSD_REQINPROG;
			} else {
				slp->ns_flag &= ~SLP_ACTION_MASK;
				error = 0;
			}
		} else {
			error = 0;
			slp = nfsd->nfsd_slp;

			lwkt_reltoken(&nfs_token);
			lwkt_gettoken(&slp->ns_token);

			if (slp->ns_needq_upcall) {
				slp->ns_needq_upcall = 0;
				slp->ns_flag |= SLP_NEEDQ;
			}
			if (NFSRV_RECLIMIT(slp) == 0 &&
			    (slp->ns_flag & SLP_NEEDQ)) {
				nfs_slplock(slp, 1);
				if (NFSRV_RECLIMIT(slp) == 0 &&
				    (slp->ns_flag & SLP_NEEDQ)) {
					nfsrv_rcv(slp->ns_so, (caddr_t)slp,
						  M_WAITOK);
				}
				nfs_slpunlock(slp);
			}
		}

		/*
		 * nfs_token not held here.  slp token is held.
		 */
		if (error || (slp->ns_flag & SLP_VALID) == 0) {
			if (nd) {
				kfree((caddr_t)nd, M_NFSRVDESC);
				nd = NULL;
			}
			nfsd->nfsd_flag &= ~NFSD_REQINPROG;
			if (slp->ns_flag & SLP_ACTION_MASK) {
				lwkt_reltoken(&slp->ns_token);
				lwkt_gettoken(&nfs_token);
			} else {
				nfsd->nfsd_slp = NULL;
				lwkt_reltoken(&slp->ns_token);
				lwkt_gettoken(&nfs_token);
				nfsrv_slpderef(slp);
			}
			continue;
		}
		sotype = slp->ns_so->so_type;

		/*
		 * Execute the NFS request - handle the server side cache
		 *
		 * nfs_token not held here.  slp token is held.
		 */
		if (nd) {
		    getmicrotime(&nd->nd_starttime);
		    if (nd->nd_nam2)
			nd->nd_nam = nd->nd_nam2;
		    else
			nd->nd_nam = slp->ns_nam;

		    /*
		     * Check to see if authorization is needed.
		     */
		    if (nfsd->nfsd_flag & NFSD_NEEDAUTH) {
			nfsd->nfsd_flag &= ~NFSD_NEEDAUTH;
			nsd->nsd_haddr =
				((struct sockaddr_in *)
				 nd->nd_nam)->sin_addr.s_addr;
			nsd->nsd_authlen = nfsd->nfsd_authlen;
			nsd->nsd_verflen = nfsd->nfsd_verflen;
			if (!copyout(nfsd->nfsd_authstr,nsd->nsd_authstr,
				nfsd->nfsd_authlen) &&
			    !copyout(nfsd->nfsd_verfstr, nsd->nsd_verfstr,
				nfsd->nfsd_verflen) &&
			    !copyout((caddr_t)nsd, argp, sizeof (*nsd)))
			{
			    lwkt_reltoken(&slp->ns_token);
			    return (ENEEDAUTH);
			}
			cacherep = RC_DROPIT;
		    } else {
			cacherep = nfsrv_getcache(nd, slp, &mreq);
		    }

		    if (nfsd->nfsd_flag & NFSD_AUTHFAIL) {
			nfsd->nfsd_flag &= ~NFSD_AUTHFAIL;
			nd->nd_procnum = NFSPROC_NOOP;
			nd->nd_repstat = (NFSERR_AUTHERR | AUTH_TOOWEAK);
			cacherep = RC_DOIT;
		    } else if (nfs_privport) {
			/* Check if source port is privileged */
			u_short port;
			struct sockaddr *nam = nd->nd_nam;
			struct sockaddr_in *sin;

			sin = (struct sockaddr_in *)nam;
			port = ntohs(sin->sin_port);
			if (port >= IPPORT_RESERVED &&
			    nd->nd_procnum != NFSPROC_NULL) {
			    char addr[INET_ADDRSTRLEN];

			    nd->nd_procnum = NFSPROC_NOOP;
			    nd->nd_repstat = (NFSERR_AUTHERR | AUTH_TOOWEAK);
			    cacherep = RC_DOIT;
			    kprintf("NFS request from unprivileged port (%s:%d)\n",
				    kinet_ntoa(sin->sin_addr, addr), port);
			}
		    }
		}

		/*
		 * Execute the NFS request - direct execution
		 *
		 * Loop to get all the write rpc replies that have been
		 * gathered together.
		 *
		 * nfs_token not held here.  slp token is held.
		 */
		do {
		    switch (cacherep) {
		    case RC_DOIT:
			if (nd && (nd->nd_flag & ND_NFSV3))
			    procrastinate = nfsrvw_procrastinate_v3;
			else
			    procrastinate = nfsrvw_procrastinate;
			if (writes_todo || (nd->nd_procnum == NFSPROC_WRITE &&
			    procrastinate > 0)
			) {
			    error = nfsrv_writegather(&nd, slp,
						      nfsd->nfsd_td, &mreq);
			} else {
			    /* NOT YET lwkt_reltoken(&slp->ns_token); */
			    error = (*(nfsrv3_procs[nd->nd_procnum]))(nd,
						slp, nfsd->nfsd_td, &mreq);
			    /* NOT YET lwkt_gettoken(&slp->ns_token); */
			    lwpkthreaddeferred();	/* vnlru issues */
			}
			if (mreq == NULL)
				break;
			if (error != 0 && error != NFSERR_RETVOID) {
				if (nd->nd_procnum != NQNFSPROC_VACATED)
					nfsstats.srv_errs++;
				nfsrv_updatecache(nd, FALSE, mreq);
				if (nd->nd_nam2)
					kfree(nd->nd_nam2, M_SONAME);
				break;
			}
			nfsstats.srvrpccnt[nd->nd_procnum]++;
			nfsrv_updatecache(nd, TRUE, mreq);
			nd->nd_mrep = NULL;
			/* FALL THROUGH */
		    case RC_REPLY:
			m = mreq;
			siz = 0;
			while (m) {
				siz += m->m_len;
				m = m->m_next;
			}
			if (siz <= 0 || siz > NFS_MAXPACKET) {
				kprintf("mbuf siz=%d\n",siz);
				panic("Bad nfs svc reply");
			}
			m = mreq;
			m->m_pkthdr.len = siz;
			m->m_pkthdr.rcvif = NULL;
			/*
			 * For stream protocols, prepend a Sun RPC
			 * Record Mark.
			 */
			if (sotype == SOCK_STREAM) {
				M_PREPEND(m, NFSX_UNSIGNED, M_WAITOK);
				*mtod(m, u_int32_t *) = htonl(0x80000000 | siz);
			}
			if ((slp->ns_flag & SLP_VALID) &&
			    (slp->ns_so->so_proto->pr_flags & PR_CONNREQUIRED)){
				nfs_slplock(slp, 1);
				slplocked = 1;
			} else {
				slplocked = 0;
			}
			if (slp->ns_flag & SLP_VALID) {
			    error = nfs_send(slp->ns_so, nd->nd_nam2, m, NULL);
			} else {
			    error = EPIPE;
			    m_freem(m);
			}
			if (nfsrtton)
				nfsd_rt(sotype, nd, cacherep);
			if (nd->nd_nam2)
				kfree(nd->nd_nam2, M_SONAME);
			if (nd->nd_mrep)
				m_freem(nd->nd_mrep);
			if (error == EPIPE || error == ENOBUFS)
				nfsrv_zapsock(slp);
			if (slplocked)
				nfs_slpunlock(slp);
			if (error == EINTR || error == ERESTART) {
				kfree((caddr_t)nd, M_NFSRVDESC);
				lwkt_reltoken(&slp->ns_token);
				lwkt_gettoken(&nfs_token);
				nfsd->nfsd_slp = NULL;
				nfsrv_slpderef(slp);
				goto done;
			}
			break;
		    case RC_DROPIT:
			if (nfsrtton)
				nfsd_rt(sotype, nd, cacherep);
			m_freem(nd->nd_mrep);
			if (nd->nd_nam2)
				kfree(nd->nd_nam2, M_SONAME);
			break;
		    }
		    if (nd) {
			kfree((caddr_t)nd, M_NFSRVDESC);
			nd = NULL;
		    }

		    /*
		     * Check to see if there are outstanding writes that
		     * need to be serviced.
		     */
		    cur_usec = nfs_curusec();
		    if (slp->ns_tq.lh_first &&
			slp->ns_tq.lh_first->nd_time <= cur_usec) {
			cacherep = RC_DOIT;
			writes_todo = 1;
		    } else {
			writes_todo = 0;
		    }
		} while (writes_todo);

		/*
		 * nfs_token not held here.  slp token is held.
		 */
		if (nfsrv_dorec(slp, nfsd, &nd)) {
			nfsd->nfsd_flag &= ~NFSD_REQINPROG;
			if (slp->ns_flag & SLP_ACTION_MASK) {
				lwkt_reltoken(&slp->ns_token);
				lwkt_gettoken(&nfs_token);
			} else {
				nfsd->nfsd_slp = NULL;
				lwkt_reltoken(&slp->ns_token);
				lwkt_gettoken(&nfs_token);
				nfsrv_slpderef(slp);
			}
		} else {
			lwkt_reltoken(&slp->ns_token);
			lwkt_gettoken(&nfs_token);
		}
	}
done:
	TAILQ_REMOVE(&nfsd_head, nfsd, nfsd_chain);
	kfree((caddr_t)nfsd, M_NFSD);
	nsd->nsd_nfsd = NULL;
	if (--nfs_numnfsd == 0)
		nfsrv_init(TRUE);	/* Reinitialize everything */

	lwkt_reltoken(&nfs_token);
	return (error);
}

/*
 * Shut down a socket associated with an nfssvc_sock structure.
 * Should be called with the send lock set, if required.
 *
 * The trick here is to increment the sref at the start, so that the nfsds
 * will stop using it and clear ns_flag at the end so that it will not be
 * reassigned during cleanup.
 *
 * That said, while we shutdown() the socket here, we don't actually destroy
 * it until the final deref as there might be other code in the middle of
 * using it.
 */
static void
nfsrv_zapsock(struct nfssvc_sock *slp)
{
	struct nfsuid *nuidp, *nnuidp;
	struct nfsrv_descript *nwp, *nnwp;
	struct socket *so;
	struct nfsrv_rec *rec;
	int wasvalid;

	wasvalid = slp->ns_flag & SLP_VALID;
	slp->ns_flag &= ~SLP_ALLFLAGS;
	if (wasvalid && slp->ns_fp) {
		so = slp->ns_so;
		atomic_clear_int(&so->so_rcv.ssb_flags, SSB_UPCALL);
		so->so_upcall = NULL;
		so->so_upcallarg = NULL;
		soshutdown(so, SHUT_RDWR);
		if (slp->ns_nam) {
			kfree(slp->ns_nam, M_SONAME);
			slp->ns_nam = NULL;
		}

		m_freem(slp->ns_raw);
		slp->ns_raw = NULL;

		while ((rec = STAILQ_FIRST(&slp->ns_rec)) != NULL) {
			--slp->ns_numrec;
			STAILQ_REMOVE_HEAD(&slp->ns_rec, nr_link);
			if (rec->nr_address)
				kfree(rec->nr_address, M_SONAME);
			m_freem(rec->nr_packet);
			kfree(rec, M_NFSRVDESC);
		}
		KKASSERT(slp->ns_numrec == 0);

		TAILQ_FOREACH_MUTABLE(nuidp, &slp->ns_uidlruhead, nu_lru,
				      nnuidp) {
			LIST_REMOVE(nuidp, nu_hash);
			TAILQ_REMOVE(&slp->ns_uidlruhead, nuidp, nu_lru);
			if (nuidp->nu_flag & NU_NAM)
				kfree(nuidp->nu_nam, M_SONAME);
			kfree((caddr_t)nuidp, M_NFSUID);
		}
		crit_enter();	/* XXX doesn't do anything any more */
		for (nwp = slp->ns_tq.lh_first; nwp; nwp = nnwp) {
			nnwp = nwp->nd_tq.le_next;
			LIST_REMOVE(nwp, nd_tq);
			kfree((caddr_t)nwp, M_NFSRVDESC);
		}
		LIST_INIT(&slp->ns_tq);
		crit_exit();

		nfsrv_slpderef(slp);
	}
}

/*
 * Derefence a server socket structure. If it has no more references and
 * is no longer valid, you can throw it away.
 *
 * Must be holding nfs_token!
 */
void
nfsrv_slpderef(struct nfssvc_sock *slp)
{
	struct file *fp;

	ASSERT_LWKT_TOKEN_HELD(&nfs_token);
	if (slp->ns_sref == 1) {
		KKASSERT((slp->ns_flag & SLP_VALID) == 0);
		TAILQ_REMOVE(&nfssvc_sockhead, slp, ns_chain);
		slp->ns_sref = 0;
		fp = slp->ns_fp;
		slp->ns_fp = NULL;
		slp->ns_so = NULL;
		if (fp)
			closef(fp, NULL);
		kfree((caddr_t)slp, M_NFSSVC);
	} else {
		--slp->ns_sref;
	}
}

void
nfsrv_slpref(struct nfssvc_sock *slp)
{
	ASSERT_LWKT_TOKEN_HELD(&nfs_token);
	++slp->ns_sref;
}

/*
 * Lock a socket against others.
 *
 * Returns 0 on failure, 1 on success.
 */
int
nfs_slplock(struct nfssvc_sock *slp, int wait)
{
	mtx_t *mtx = &slp->ns_solock;

	if (wait) {
		mtx_lock_ex(mtx, 0, 0);
		return(1);
	} else if (mtx_lock_ex_try(mtx) == 0) {
		return(1);
	} else {
		return(0);
	}
}

/*
 * Unlock the stream socket for others.
 */
void
nfs_slpunlock(struct nfssvc_sock *slp)
{
	mtx_t *mtx = &slp->ns_solock;

	mtx_unlock(mtx);
}

/*
 * Initialize the data structures for the server.
 * Handshake with any new nfsds starting up to avoid any chance of
 * corruption.
 */
void
nfsrv_init(int terminating)
{
	struct nfssvc_sock *slp, *nslp;

	lwkt_gettoken(&nfs_token);
	if (nfssvc_sockhead_flag & SLP_INIT)
		panic("nfsd init");
	nfssvc_sockhead_flag |= SLP_INIT;

	if (terminating) {
		TAILQ_FOREACH_MUTABLE(slp, &nfssvc_sockhead, ns_chain, nslp) {
			nfsrv_slpref(slp);
			lwkt_gettoken(&slp->ns_token);
			if (slp->ns_flag & SLP_VALID)
				nfsrv_zapsock(slp);
			lwkt_reltoken(&slp->ns_token);
			nfsrv_slpderef(slp);
		}
		nfsrv_cleancache();	/* And clear out server cache */
	} else {
		nfs_pub.np_valid = 0;
	}

	TAILQ_INIT(&nfssvc_sockhead);
	nfssvc_sockhead_flag &= ~SLP_INIT;
	if (nfssvc_sockhead_flag & SLP_WANTINIT) {
		nfssvc_sockhead_flag &= ~SLP_WANTINIT;
		wakeup((caddr_t)&nfssvc_sockhead);
	}

	TAILQ_INIT(&nfsd_head);
	nfsd_head_flag &= ~NFSD_CHECKSLP;

	lwkt_reltoken(&nfs_token);

#if 0
	nfs_udpsock = (struct nfssvc_sock *)
	    kmalloc(sizeof (struct nfssvc_sock), M_NFSSVC, M_WAITOK | M_ZERO);
	mtx_init(&nfs_udpsock->ns_solock);
	STAILQ_INIT(&nfs_udpsock->ns_rec);
	TAILQ_INIT(&nfs_udpsock->ns_uidlruhead);
	TAILQ_INSERT_HEAD(&nfssvc_sockhead, nfs_udpsock, ns_chain);

	nfs_cltpsock = (struct nfssvc_sock *)
	    kmalloc(sizeof (struct nfssvc_sock), M_NFSSVC, M_WAITOK | M_ZERO);
	mtx_init(&nfs_cltpsock->ns_solock);
	STAILQ_INIT(&nfs_cltpsock->ns_rec);
	TAILQ_INIT(&nfs_cltpsock->ns_uidlruhead);
	TAILQ_INSERT_TAIL(&nfssvc_sockhead, nfs_cltpsock, ns_chain);
#endif
}

/*
 * Add entries to the server monitor log.
 */
static void
nfsd_rt(int sotype, struct nfsrv_descript *nd, int cacherep)
{
	struct drt *rt;

	rt = &nfsdrt.drt[nfsdrt.pos];
	if (cacherep == RC_DOIT)
		rt->flag = 0;
	else if (cacherep == RC_REPLY)
		rt->flag = DRT_CACHEREPLY;
	else
		rt->flag = DRT_CACHEDROP;
	if (sotype == SOCK_STREAM)
		rt->flag |= DRT_TCP;
	if (nd->nd_flag & ND_NFSV3)
		rt->flag |= DRT_NFSV3;
	rt->proc = nd->nd_procnum;
	if (nd->nd_nam->sa_family == AF_INET)
	    rt->ipadr = ((struct sockaddr_in *)nd->nd_nam)->sin_addr.s_addr;
	else
	    rt->ipadr = INADDR_ANY;
	rt->resptime = nfs_curusec() - (nd->nd_starttime.tv_sec * 1000000 + nd->nd_starttime.tv_usec);
	getmicrotime(&rt->tstamp);
	nfsdrt.pos = (nfsdrt.pos + 1) % NFSRTTLOGSIZ;
}
#endif /* NFS_NOSERVER */

/*
 * Get an authorization string for the uid by having the mount_nfs sitting
 * on this mount point porpous out of the kernel and do it.
 */
int
nfs_getauth(struct nfsmount *nmp, struct nfsreq *rep,
	    struct ucred *cred, char **auth_str, int *auth_len, char *verf_str,
	    int *verf_len, NFSKERBKEY_T key /* return session key */)
{
	int error = 0;

	while ((nmp->nm_state & NFSSTA_WAITAUTH) == 0) {
		nmp->nm_state |= NFSSTA_WANTAUTH;
		(void) tsleep((caddr_t)&nmp->nm_authtype, 0,
			"nfsauth1", 2 * hz);
		error = nfs_sigintr(nmp, rep, rep->r_td);
		if (error) {
			nmp->nm_state &= ~NFSSTA_WANTAUTH;
			return (error);
		}
	}
	nmp->nm_state &= ~(NFSSTA_WAITAUTH | NFSSTA_WANTAUTH);
	nmp->nm_authstr = *auth_str = (char *)kmalloc(RPCAUTH_MAXSIZ, M_TEMP, M_WAITOK);
	nmp->nm_authlen = RPCAUTH_MAXSIZ;
	nmp->nm_verfstr = verf_str;
	nmp->nm_verflen = *verf_len;
	nmp->nm_authuid = cred->cr_uid;
	wakeup((caddr_t)&nmp->nm_authstr);

	/*
	 * And wait for mount_nfs to do its stuff.
	 */
	while ((nmp->nm_state & NFSSTA_HASAUTH) == 0 && error == 0) {
		(void) tsleep((caddr_t)&nmp->nm_authlen, 0,
			"nfsauth2", 2 * hz);
		error = nfs_sigintr(nmp, rep, rep->r_td);
	}
	if (nmp->nm_state & NFSSTA_AUTHERR) {
		nmp->nm_state &= ~NFSSTA_AUTHERR;
		error = EAUTH;
	}
	if (error)
		kfree((caddr_t)*auth_str, M_TEMP);
	else {
		*auth_len = nmp->nm_authlen;
		*verf_len = nmp->nm_verflen;
		bcopy((caddr_t)nmp->nm_key, (caddr_t)key, sizeof (NFSKERBKEY_T));
	}
	nmp->nm_state &= ~NFSSTA_HASAUTH;
	nmp->nm_state |= NFSSTA_WAITAUTH;
	if (nmp->nm_state & NFSSTA_WANTAUTH) {
		nmp->nm_state &= ~NFSSTA_WANTAUTH;
		wakeup((caddr_t)&nmp->nm_authtype);
	}
	return (error);
}

/*
 * Get a nickname authenticator and verifier.
 */
int
nfs_getnickauth(struct nfsmount *nmp, struct ucred *cred, char **auth_str,
		int *auth_len, char *verf_str, int verf_len)
{
	struct nfsuid *nuidp;
	u_int32_t *nickp, *verfp;
	struct timeval ktvout;

#ifdef DIAGNOSTIC
	if (verf_len < (4 * NFSX_UNSIGNED))
		panic("nfs_getnickauth verf too small");
#endif
	for (nuidp = NMUIDHASH(nmp, cred->cr_uid)->lh_first;
	    nuidp != NULL; nuidp = nuidp->nu_hash.le_next) {
		if (nuidp->nu_cr.cr_uid == cred->cr_uid)
			break;
	}
	if (!nuidp || nuidp->nu_expire < time_uptime)
		return (EACCES);

	/*
	 * Move to the end of the lru list (end of lru == most recently used).
	 */
	TAILQ_REMOVE(&nmp->nm_uidlruhead, nuidp, nu_lru);
	TAILQ_INSERT_TAIL(&nmp->nm_uidlruhead, nuidp, nu_lru);

	nickp = (u_int32_t *)kmalloc(2 * NFSX_UNSIGNED, M_TEMP, M_WAITOK);
	*nickp++ = txdr_unsigned(RPCAKN_NICKNAME);
	*nickp = txdr_unsigned(nuidp->nu_nickname);
	*auth_str = (char *)nickp;
	*auth_len = 2 * NFSX_UNSIGNED;

	/*
	 * Now we must encrypt the verifier and package it up.
	 */
	verfp = (u_int32_t *)verf_str;
	*verfp++ = txdr_unsigned(RPCAKN_NICKNAME);
	if (time_second != nuidp->nu_timestamp.tv_sec ||
	    time_second > nuidp->nu_timestamp.tv_usec)		/* XXX */
		getmicrotime(&nuidp->nu_timestamp);
	else
		nuidp->nu_timestamp.tv_usec++;

	/*
	 * Now encrypt the timestamp verifier in ecb mode using the session
	 * key.
	 */
#ifdef NFSKERB
	XXX
#else
	ktvout.tv_sec = 0;
	ktvout.tv_usec = 0;
#endif

	*verfp++ = ktvout.tv_sec;
	*verfp++ = ktvout.tv_usec;
	*verfp = 0;
	return (0);
}

/*
 * Save the current nickname in a hash list entry on the mount point.
 */
int
nfs_savenickauth(struct nfsmount *nmp, struct ucred *cred, int len,
		 NFSKERBKEY_T key, struct mbuf **mdp, char **dposp,
		 struct mbuf *mrep)
{
	struct nfsuid *nuidp;
	u_int32_t *tl;
	struct timeval ktvin, ktvout;
	u_int32_t nick;
	int deltasec, error = 0;
	struct nfsm_info info;

	info.md = *mdp;
	info.dpos = *dposp;
	info.mrep = mrep;

	if (len == (3 * NFSX_UNSIGNED)) {
		NULLOUT(tl = nfsm_dissect(&info, 3 * NFSX_UNSIGNED));
		ktvin.tv_sec = *tl++;
		ktvin.tv_usec = *tl++;
		nick = fxdr_unsigned(u_int32_t, *tl);

		/*
		 * Decrypt the timestamp in ecb mode.
		 */
#ifdef NFSKERB
		XXX
#else
		ktvout.tv_sec = 0;
		ktvout.tv_usec = 0;
#endif
		ktvout.tv_sec = fxdr_unsigned(long, ktvout.tv_sec);
		ktvout.tv_usec = fxdr_unsigned(long, ktvout.tv_usec);
		deltasec = time_second - ktvout.tv_sec;
		if (deltasec < 0)
			deltasec = -deltasec;
		/*
		 * If ok, add it to the hash list for the mount point.
		 */
		if (deltasec <= NFS_KERBCLOCKSKEW) {
			if (nmp->nm_numuids < nuidhash_max) {
				nmp->nm_numuids++;
				nuidp = (struct nfsuid *)
				   kmalloc(sizeof (struct nfsuid), M_NFSUID,
					M_WAITOK);
			} else {
				nuidp = TAILQ_FIRST(&nmp->nm_uidlruhead);
				LIST_REMOVE(nuidp, nu_hash);
				TAILQ_REMOVE(&nmp->nm_uidlruhead, nuidp,
					nu_lru);
			}
			nuidp->nu_flag = 0;
			nuidp->nu_cr.cr_uid = cred->cr_uid;
			nuidp->nu_expire = time_uptime + NFS_KERBTTL;
			nuidp->nu_timestamp = ktvout;
			nuidp->nu_nickname = nick;
			bcopy(key, nuidp->nu_key, sizeof (NFSKERBKEY_T));
			TAILQ_INSERT_TAIL(&nmp->nm_uidlruhead, nuidp,
				nu_lru);
			LIST_INSERT_HEAD(NMUIDHASH(nmp, cred->cr_uid),
				nuidp, nu_hash);
		}
	} else {
		ERROROUT(nfsm_adv(&info, nfsm_rndup(len)));
	}
nfsmout:
	*mdp = info.md;
	*dposp = info.dpos;
	return (error);
}