Home | History | Annotate | Line # | Download | only in nfs
      1  1.9  christos /*	$NetBSD: nfs_iod.c,v 1.9 2023/03/21 15:47:46 christos Exp $	*/
      2  1.1        ad 
      3  1.1        ad /*
      4  1.1        ad  * Copyright (c) 1989, 1993
      5  1.1        ad  *	The Regents of the University of California.  All rights reserved.
      6  1.1        ad  *
      7  1.1        ad  * This code is derived from software contributed to Berkeley by
      8  1.1        ad  * Rick Macklem at The University of Guelph.
      9  1.1        ad  *
     10  1.1        ad  * Redistribution and use in source and binary forms, with or without
     11  1.1        ad  * modification, are permitted provided that the following conditions
     12  1.1        ad  * are met:
     13  1.1        ad  * 1. Redistributions of source code must retain the above copyright
     14  1.1        ad  *    notice, this list of conditions and the following disclaimer.
     15  1.1        ad  * 2. Redistributions in binary form must reproduce the above copyright
     16  1.1        ad  *    notice, this list of conditions and the following disclaimer in the
     17  1.1        ad  *    documentation and/or other materials provided with the distribution.
     18  1.1        ad  * 3. Neither the name of the University nor the names of its contributors
     19  1.1        ad  *    may be used to endorse or promote products derived from this software
     20  1.1        ad  *    without specific prior written permission.
     21  1.1        ad  *
     22  1.1        ad  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     23  1.1        ad  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     24  1.1        ad  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     25  1.1        ad  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     26  1.1        ad  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     27  1.1        ad  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     28  1.1        ad  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     29  1.1        ad  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     30  1.1        ad  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     31  1.1        ad  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     32  1.1        ad  * SUCH DAMAGE.
     33  1.1        ad  *
     34  1.1        ad  *	@(#)nfs_syscalls.c	8.5 (Berkeley) 3/30/95
     35  1.1        ad  */
     36  1.1        ad 
     37  1.1        ad #include <sys/cdefs.h>
     38  1.9  christos __KERNEL_RCSID(0, "$NetBSD: nfs_iod.c,v 1.9 2023/03/21 15:47:46 christos Exp $");
     39  1.1        ad 
     40  1.1        ad #include <sys/param.h>
     41  1.1        ad #include <sys/systm.h>
     42  1.1        ad #include <sys/kernel.h>
     43  1.1        ad #include <sys/file.h>
     44  1.1        ad #include <sys/stat.h>
     45  1.1        ad #include <sys/vnode.h>
     46  1.1        ad #include <sys/mount.h>
     47  1.1        ad #include <sys/proc.h>
     48  1.1        ad #include <sys/uio.h>
     49  1.1        ad #include <sys/malloc.h>
     50  1.1        ad #include <sys/kmem.h>
     51  1.1        ad #include <sys/buf.h>
     52  1.1        ad #include <sys/mbuf.h>
     53  1.1        ad #include <sys/socket.h>
     54  1.1        ad #include <sys/socketvar.h>
     55  1.1        ad #include <sys/signalvar.h>
     56  1.1        ad #include <sys/domain.h>
     57  1.1        ad #include <sys/protosw.h>
     58  1.1        ad #include <sys/namei.h>
     59  1.1        ad #include <sys/syslog.h>
     60  1.1        ad #include <sys/filedesc.h>
     61  1.1        ad #include <sys/kthread.h>
     62  1.1        ad #include <sys/kauth.h>
     63  1.1        ad #include <sys/syscallargs.h>
     64  1.1        ad 
     65  1.1        ad #include <netinet/in.h>
     66  1.1        ad #include <netinet/tcp.h>
     67  1.1        ad #include <nfs/xdr_subs.h>
     68  1.1        ad #include <nfs/rpcv2.h>
     69  1.1        ad #include <nfs/nfsproto.h>
     70  1.1        ad #include <nfs/nfs.h>
     71  1.1        ad #include <nfs/nfsm_subs.h>
     72  1.1        ad #include <nfs/nfsrvcache.h>
     73  1.1        ad #include <nfs/nfsmount.h>
     74  1.1        ad #include <nfs/nfsnode.h>
     75  1.1        ad #include <nfs/nfsrtt.h>
     76  1.1        ad #include <nfs/nfs_var.h>
     77  1.1        ad 
     78  1.4  christos extern int nuidhash_max;
     79  1.1        ad 
     80  1.1        ad /*
     81  1.1        ad  * locking order:
     82  1.1        ad  *	nfs_iodlist_lock -> nid_lock -> nm_lock
     83  1.1        ad  */
     84  1.1        ad kmutex_t nfs_iodlist_lock;
     85  1.1        ad struct nfs_iodlist nfs_iodlist_idle;
     86  1.1        ad struct nfs_iodlist nfs_iodlist_all;
     87  1.1        ad int nfs_niothreads = -1; /* == "0, and has never been set" */
     88  1.1        ad int nfs_defect = 0;
     89  1.1        ad 
     90  1.1        ad /*
     91  1.1        ad  * Asynchronous I/O threads for client nfs.
     92  1.1        ad  * They do read-ahead and write-behind operations on the block I/O cache.
     93  1.1        ad  * Never returns unless it fails or gets killed.
     94  1.1        ad  */
     95  1.1        ad 
     96  1.1        ad static void
     97  1.1        ad nfssvc_iod(void *arg)
     98  1.1        ad {
     99  1.1        ad 	struct buf *bp;
    100  1.1        ad 	struct nfs_iod *myiod;
    101  1.1        ad 	struct nfsmount *nmp;
    102  1.1        ad 
    103  1.1        ad 	myiod = kmem_alloc(sizeof(*myiod), KM_SLEEP);
    104  1.1        ad 	mutex_init(&myiod->nid_lock, MUTEX_DEFAULT, IPL_NONE);
    105  1.1        ad 	cv_init(&myiod->nid_cv, "nfsiod");
    106  1.1        ad 	myiod->nid_exiting = false;
    107  1.1        ad 	myiod->nid_mount = NULL;
    108  1.1        ad 	mutex_enter(&nfs_iodlist_lock);
    109  1.1        ad 	LIST_INSERT_HEAD(&nfs_iodlist_all, myiod, nid_all);
    110  1.1        ad 	mutex_exit(&nfs_iodlist_lock);
    111  1.1        ad 
    112  1.1        ad 	for (;;) {
    113  1.1        ad 		mutex_enter(&nfs_iodlist_lock);
    114  1.1        ad 		LIST_INSERT_HEAD(&nfs_iodlist_idle, myiod, nid_idle);
    115  1.1        ad 		mutex_exit(&nfs_iodlist_lock);
    116  1.1        ad 
    117  1.1        ad 		mutex_enter(&myiod->nid_lock);
    118  1.1        ad 		while (/*CONSTCOND*/ true) {
    119  1.1        ad 			nmp = myiod->nid_mount;
    120  1.1        ad 			if (nmp) {
    121  1.1        ad 				myiod->nid_mount = NULL;
    122  1.1        ad 				break;
    123  1.1        ad 			}
    124  1.1        ad 			if (__predict_false(myiod->nid_exiting)) {
    125  1.1        ad 				/*
    126  1.1        ad 				 * drop nid_lock to preserve locking order.
    127  1.1        ad 				 */
    128  1.1        ad 				mutex_exit(&myiod->nid_lock);
    129  1.1        ad 				mutex_enter(&nfs_iodlist_lock);
    130  1.1        ad 				mutex_enter(&myiod->nid_lock);
    131  1.1        ad 				/*
    132  1.1        ad 				 * recheck nid_mount because nfs_asyncio can
    133  1.1        ad 				 * pick us in the meantime as we are still on
    134  1.1        ad 				 * nfs_iodlist_lock.
    135  1.1        ad 				 */
    136  1.1        ad 				if (myiod->nid_mount != NULL) {
    137  1.1        ad 					mutex_exit(&nfs_iodlist_lock);
    138  1.1        ad 					continue;
    139  1.1        ad 				}
    140  1.1        ad 				LIST_REMOVE(myiod, nid_idle);
    141  1.1        ad 				mutex_exit(&nfs_iodlist_lock);
    142  1.1        ad 				goto quit;
    143  1.1        ad 			}
    144  1.1        ad 			cv_wait(&myiod->nid_cv, &myiod->nid_lock);
    145  1.1        ad 		}
    146  1.1        ad 		mutex_exit(&myiod->nid_lock);
    147  1.1        ad 
    148  1.1        ad 		mutex_enter(&nmp->nm_lock);
    149  1.1        ad 		while ((bp = TAILQ_FIRST(&nmp->nm_bufq)) != NULL) {
    150  1.1        ad 			/* Take one off the front of the list */
    151  1.1        ad 			TAILQ_REMOVE(&nmp->nm_bufq, bp, b_freelist);
    152  1.1        ad 			nmp->nm_bufqlen--;
    153  1.1        ad 			if (nmp->nm_bufqlen < 2 * nmp->nm_bufqiods) {
    154  1.1        ad 				cv_broadcast(&nmp->nm_aiocv);
    155  1.1        ad 			}
    156  1.1        ad 			mutex_exit(&nmp->nm_lock);
    157  1.1        ad 			KERNEL_LOCK(1, curlwp);
    158  1.1        ad 			(void)nfs_doio(bp);
    159  1.1        ad 			KERNEL_UNLOCK_LAST(curlwp);
    160  1.1        ad 			mutex_enter(&nmp->nm_lock);
    161  1.1        ad 			/*
    162  1.1        ad 			 * If there are more than one iod on this mount,
    163  1.1        ad 			 * then defect so that the iods can be shared out
    164  1.1        ad 			 * fairly between the mounts
    165  1.1        ad 			 */
    166  1.1        ad 			if (nfs_defect && nmp->nm_bufqiods > 1) {
    167  1.1        ad 				break;
    168  1.1        ad 			}
    169  1.1        ad 		}
    170  1.1        ad 		KASSERT(nmp->nm_bufqiods > 0);
    171  1.1        ad 		nmp->nm_bufqiods--;
    172  1.1        ad 		mutex_exit(&nmp->nm_lock);
    173  1.1        ad 	}
    174  1.1        ad quit:
    175  1.1        ad 	KASSERT(myiod->nid_mount == NULL);
    176  1.1        ad 	mutex_exit(&myiod->nid_lock);
    177  1.1        ad 
    178  1.1        ad 	cv_destroy(&myiod->nid_cv);
    179  1.1        ad 	mutex_destroy(&myiod->nid_lock);
    180  1.1        ad 	kmem_free(myiod, sizeof(*myiod));
    181  1.1        ad 
    182  1.1        ad 	kthread_exit(0);
    183  1.1        ad }
    184  1.1        ad 
    185  1.1        ad void
    186  1.1        ad nfs_iodinit(void)
    187  1.1        ad {
    188  1.1        ad 
    189  1.1        ad 	mutex_init(&nfs_iodlist_lock, MUTEX_DEFAULT, IPL_NONE);
    190  1.1        ad 	LIST_INIT(&nfs_iodlist_all);
    191  1.1        ad 	LIST_INIT(&nfs_iodlist_idle);
    192  1.1        ad }
    193  1.1        ad 
    194  1.1        ad void
    195  1.1        ad nfs_iodfini(void)
    196  1.1        ad {
    197  1.6    martin 	int error __diagused;
    198  1.1        ad 
    199  1.1        ad 	error = nfs_set_niothreads(0);
    200  1.1        ad 	KASSERT(error == 0);
    201  1.1        ad 	mutex_destroy(&nfs_iodlist_lock);
    202  1.1        ad }
    203  1.1        ad 
    204  1.1        ad int
    205  1.7      manu nfs_iodbusy(struct nfsmount *nmp)
    206  1.7      manu {
    207  1.7      manu 	struct nfs_iod *iod;
    208  1.7      manu 	int ret = 0;
    209  1.7      manu 
    210  1.7      manu 	mutex_enter(&nfs_iodlist_lock);
    211  1.7      manu 	LIST_FOREACH(iod, &nfs_iodlist_all, nid_all) {
    212  1.7      manu 		if (iod->nid_mount == nmp)
    213  1.7      manu 			ret++;
    214  1.7      manu 	}
    215  1.7      manu 	mutex_exit(&nfs_iodlist_lock);
    216  1.7      manu 
    217  1.7      manu 	return ret;
    218  1.7      manu }
    219  1.7      manu 
    220  1.7      manu int
    221  1.1        ad nfs_set_niothreads(int newval)
    222  1.1        ad {
    223  1.1        ad 	struct nfs_iod *nid;
    224  1.1        ad 	int error = 0;
    225  1.1        ad         int hold_count;
    226  1.1        ad 
    227  1.1        ad 	KERNEL_UNLOCK_ALL(curlwp, &hold_count);
    228  1.1        ad 
    229  1.1        ad 	mutex_enter(&nfs_iodlist_lock);
    230  1.1        ad 	/* clamp to sane range */
    231  1.8  riastrad 	nfs_niothreads = uimax(0, uimin(newval, NFS_MAXASYNCDAEMON));
    232  1.1        ad 
    233  1.1        ad 	while (nfs_numasync != nfs_niothreads && error == 0) {
    234  1.1        ad 		while (nfs_numasync < nfs_niothreads) {
    235  1.1        ad 
    236  1.1        ad 			/*
    237  1.1        ad 			 * kthread_create can wait for pagedaemon and
    238  1.1        ad 			 * pagedaemon can wait for nfsiod which needs to acquire
    239  1.1        ad 			 * nfs_iodlist_lock.
    240  1.1        ad 			 */
    241  1.1        ad 
    242  1.1        ad 			mutex_exit(&nfs_iodlist_lock);
    243  1.1        ad 			error = kthread_create(PRI_NONE, KTHREAD_MPSAFE, NULL,
    244  1.1        ad 			    nfssvc_iod, NULL, NULL, "nfsio");
    245  1.1        ad 			mutex_enter(&nfs_iodlist_lock);
    246  1.1        ad 			if (error) {
    247  1.1        ad 				/* give up */
    248  1.1        ad 				nfs_niothreads = nfs_numasync;
    249  1.1        ad 				break;
    250  1.1        ad 			}
    251  1.1        ad 			nfs_numasync++;
    252  1.1        ad 		}
    253  1.1        ad 		while (nfs_numasync > nfs_niothreads) {
    254  1.1        ad 			nid = LIST_FIRST(&nfs_iodlist_all);
    255  1.1        ad 			if (nid == NULL) {
    256  1.1        ad 				/* iod has not started yet. */
    257  1.1        ad 				kpause("nfsiorm", false, hz, &nfs_iodlist_lock);
    258  1.1        ad 				continue;
    259  1.1        ad 			}
    260  1.1        ad 			LIST_REMOVE(nid, nid_all);
    261  1.1        ad 			mutex_enter(&nid->nid_lock);
    262  1.1        ad 			KASSERT(!nid->nid_exiting);
    263  1.1        ad 			nid->nid_exiting = true;
    264  1.1        ad 			cv_signal(&nid->nid_cv);
    265  1.1        ad 			mutex_exit(&nid->nid_lock);
    266  1.1        ad 			nfs_numasync--;
    267  1.1        ad 		}
    268  1.1        ad 	}
    269  1.1        ad 	mutex_exit(&nfs_iodlist_lock);
    270  1.1        ad 
    271  1.1        ad 	KERNEL_LOCK(hold_count, curlwp);
    272  1.1        ad 	return error;
    273  1.1        ad }
    274  1.1        ad 
    275  1.1        ad /*
    276  1.1        ad  * Get an authorization string for the uid by having the mount_nfs sitting
    277  1.1        ad  * on this mount point porpous out of the kernel and do it.
    278  1.1        ad  */
    279  1.1        ad int
    280  1.3       dsl nfs_getauth(struct nfsmount *nmp, struct nfsreq *rep, kauth_cred_t cred, char **auth_str, int *auth_len, char *verf_str, int *verf_len, NFSKERBKEY_T key)
    281  1.3       dsl 	/* key:		 return session key */
    282  1.1        ad {
    283  1.1        ad 	int error = 0;
    284  1.1        ad 
    285  1.1        ad 	while ((nmp->nm_iflag & NFSMNT_WAITAUTH) == 0) {
    286  1.1        ad 		nmp->nm_iflag |= NFSMNT_WANTAUTH;
    287  1.1        ad 		(void) tsleep((void *)&nmp->nm_authtype, PSOCK,
    288  1.1        ad 			"nfsauth1", 2 * hz);
    289  1.1        ad 		error = nfs_sigintr(nmp, rep, rep->r_lwp);
    290  1.1        ad 		if (error) {
    291  1.1        ad 			nmp->nm_iflag &= ~NFSMNT_WANTAUTH;
    292  1.1        ad 			return (error);
    293  1.1        ad 		}
    294  1.1        ad 	}
    295  1.1        ad 	nmp->nm_iflag &= ~(NFSMNT_WAITAUTH | NFSMNT_WANTAUTH);
    296  1.1        ad 	nmp->nm_authstr = *auth_str = (char *)malloc(RPCAUTH_MAXSIZ, M_TEMP, M_WAITOK);
    297  1.1        ad 	nmp->nm_authlen = RPCAUTH_MAXSIZ;
    298  1.1        ad 	nmp->nm_verfstr = verf_str;
    299  1.1        ad 	nmp->nm_verflen = *verf_len;
    300  1.1        ad 	nmp->nm_authuid = kauth_cred_geteuid(cred);
    301  1.1        ad 	wakeup((void *)&nmp->nm_authstr);
    302  1.1        ad 
    303  1.1        ad 	/*
    304  1.1        ad 	 * And wait for mount_nfs to do its stuff.
    305  1.1        ad 	 */
    306  1.1        ad 	while ((nmp->nm_iflag & NFSMNT_HASAUTH) == 0 && error == 0) {
    307  1.1        ad 		(void) tsleep((void *)&nmp->nm_authlen, PSOCK,
    308  1.1        ad 			"nfsauth2", 2 * hz);
    309  1.1        ad 		error = nfs_sigintr(nmp, rep, rep->r_lwp);
    310  1.1        ad 	}
    311  1.1        ad 	if (nmp->nm_iflag & NFSMNT_AUTHERR) {
    312  1.1        ad 		nmp->nm_iflag &= ~NFSMNT_AUTHERR;
    313  1.1        ad 		error = EAUTH;
    314  1.1        ad 	}
    315  1.1        ad 	if (error)
    316  1.1        ad 		free((void *)*auth_str, M_TEMP);
    317  1.1        ad 	else {
    318  1.1        ad 		*auth_len = nmp->nm_authlen;
    319  1.1        ad 		*verf_len = nmp->nm_verflen;
    320  1.1        ad 		memcpy(key, nmp->nm_key, sizeof (NFSKERBKEY_T));
    321  1.1        ad 	}
    322  1.1        ad 	nmp->nm_iflag &= ~NFSMNT_HASAUTH;
    323  1.1        ad 	nmp->nm_iflag |= NFSMNT_WAITAUTH;
    324  1.1        ad 	if (nmp->nm_iflag & NFSMNT_WANTAUTH) {
    325  1.1        ad 		nmp->nm_iflag &= ~NFSMNT_WANTAUTH;
    326  1.1        ad 		wakeup((void *)&nmp->nm_authtype);
    327  1.1        ad 	}
    328  1.1        ad 	return (error);
    329  1.1        ad }
    330  1.1        ad 
    331  1.1        ad /*
    332  1.1        ad  * Get a nickname authenticator and verifier.
    333  1.1        ad  */
    334  1.1        ad int
    335  1.1        ad nfs_getnickauth(struct nfsmount *nmp, kauth_cred_t cred, char **auth_str,
    336  1.1        ad     int *auth_len, char *verf_str, int verf_len)
    337  1.1        ad {
    338  1.5    martin #ifdef NFSKERB
    339  1.5    martin 	struct timeval ktvin;
    340  1.5    martin #endif
    341  1.5    martin 	struct timeval ktvout, tv;
    342  1.1        ad 	struct nfsuid *nuidp;
    343  1.1        ad 	u_int32_t *nickp, *verfp;
    344  1.1        ad 
    345  1.1        ad 	memset(&ktvout, 0, sizeof ktvout);	/* XXX gcc */
    346  1.1        ad 
    347  1.1        ad #ifdef DIAGNOSTIC
    348  1.1        ad 	if (verf_len < (4 * NFSX_UNSIGNED))
    349  1.1        ad 		panic("nfs_getnickauth verf too small");
    350  1.1        ad #endif
    351  1.1        ad 	LIST_FOREACH(nuidp, NMUIDHASH(nmp, kauth_cred_geteuid(cred)), nu_hash) {
    352  1.1        ad 		if (kauth_cred_geteuid(nuidp->nu_cr) == kauth_cred_geteuid(cred))
    353  1.1        ad 			break;
    354  1.1        ad 	}
    355  1.1        ad 	if (!nuidp || nuidp->nu_expire < time_second)
    356  1.1        ad 		return (EACCES);
    357  1.1        ad 
    358  1.1        ad 	/*
    359  1.1        ad 	 * Move to the end of the lru list (end of lru == most recently used).
    360  1.1        ad 	 */
    361  1.1        ad 	TAILQ_REMOVE(&nmp->nm_uidlruhead, nuidp, nu_lru);
    362  1.1        ad 	TAILQ_INSERT_TAIL(&nmp->nm_uidlruhead, nuidp, nu_lru);
    363  1.1        ad 
    364  1.1        ad 	nickp = (u_int32_t *)malloc(2 * NFSX_UNSIGNED, M_TEMP, M_WAITOK);
    365  1.1        ad 	*nickp++ = txdr_unsigned(RPCAKN_NICKNAME);
    366  1.1        ad 	*nickp = txdr_unsigned(nuidp->nu_nickname);
    367  1.1        ad 	*auth_str = (char *)nickp;
    368  1.1        ad 	*auth_len = 2 * NFSX_UNSIGNED;
    369  1.1        ad 
    370  1.1        ad 	/*
    371  1.1        ad 	 * Now we must encrypt the verifier and package it up.
    372  1.1        ad 	 */
    373  1.1        ad 	verfp = (u_int32_t *)verf_str;
    374  1.1        ad 	*verfp++ = txdr_unsigned(RPCAKN_NICKNAME);
    375  1.1        ad 	getmicrotime(&tv);
    376  1.1        ad 	if (tv.tv_sec > nuidp->nu_timestamp.tv_sec ||
    377  1.1        ad 	    (tv.tv_sec == nuidp->nu_timestamp.tv_sec &&
    378  1.1        ad 	     tv.tv_usec > nuidp->nu_timestamp.tv_usec))
    379  1.1        ad 		nuidp->nu_timestamp = tv;
    380  1.1        ad 	else
    381  1.1        ad 		nuidp->nu_timestamp.tv_usec++;
    382  1.5    martin #ifdef NFSKERB
    383  1.1        ad 	ktvin.tv_sec = txdr_unsigned(nuidp->nu_timestamp.tv_sec);
    384  1.1        ad 	ktvin.tv_usec = txdr_unsigned(nuidp->nu_timestamp.tv_usec);
    385  1.1        ad 
    386  1.1        ad 	/*
    387  1.1        ad 	 * Now encrypt the timestamp verifier in ecb mode using the session
    388  1.1        ad 	 * key.
    389  1.1        ad 	 */
    390  1.1        ad 	XXX
    391  1.1        ad #endif
    392  1.1        ad 
    393  1.1        ad 	*verfp++ = ktvout.tv_sec;
    394  1.1        ad 	*verfp++ = ktvout.tv_usec;
    395  1.1        ad 	*verfp = 0;
    396  1.1        ad 	return (0);
    397  1.1        ad }
    398  1.1        ad 
    399  1.1        ad /*
    400  1.1        ad  * Save the current nickname in a hash list entry on the mount point.
    401  1.1        ad  */
    402  1.1        ad int
    403  1.2       dsl nfs_savenickauth(struct nfsmount *nmp, kauth_cred_t cred, int len, NFSKERBKEY_T key, struct mbuf **mdp, char **dposp, struct mbuf *mrep)
    404  1.1        ad {
    405  1.1        ad 	struct nfsuid *nuidp;
    406  1.1        ad 	u_int32_t *tl;
    407  1.1        ad 	int32_t t1;
    408  1.1        ad 	struct mbuf *md = *mdp;
    409  1.1        ad 	struct timeval ktvin, ktvout;
    410  1.1        ad 	u_int32_t nick;
    411  1.1        ad 	char *dpos = *dposp, *cp2;
    412  1.9  christos 	time_t deltasec;
    413  1.9  christos 	int error = 0;
    414  1.1        ad 
    415  1.1        ad 	memset(&ktvout, 0, sizeof ktvout);	 /* XXX gcc */
    416  1.1        ad 
    417  1.1        ad 	if (len == (3 * NFSX_UNSIGNED)) {
    418  1.1        ad 		nfsm_dissect(tl, u_int32_t *, 3 * NFSX_UNSIGNED);
    419  1.1        ad 		ktvin.tv_sec = *tl++;
    420  1.1        ad 		ktvin.tv_usec = *tl++;
    421  1.1        ad 		nick = fxdr_unsigned(u_int32_t, *tl);
    422  1.1        ad 
    423  1.1        ad 		/*
    424  1.1        ad 		 * Decrypt the timestamp in ecb mode.
    425  1.1        ad 		 */
    426  1.1        ad #ifdef NFSKERB
    427  1.1        ad 		XXX
    428  1.5    martin #else
    429  1.5    martin 		(void)ktvin.tv_sec;
    430  1.1        ad #endif
    431  1.1        ad 		ktvout.tv_sec = fxdr_unsigned(long, ktvout.tv_sec);
    432  1.1        ad 		ktvout.tv_usec = fxdr_unsigned(long, ktvout.tv_usec);
    433  1.1        ad 		deltasec = time_second - ktvout.tv_sec;
    434  1.1        ad 		if (deltasec < 0)
    435  1.1        ad 			deltasec = -deltasec;
    436  1.1        ad 		/*
    437  1.1        ad 		 * If ok, add it to the hash list for the mount point.
    438  1.1        ad 		 */
    439  1.1        ad 		if (deltasec <= NFS_KERBCLOCKSKEW) {
    440  1.1        ad 			if (nmp->nm_numuids < nuidhash_max) {
    441  1.1        ad 				nmp->nm_numuids++;
    442  1.1        ad 				nuidp = kmem_alloc(sizeof(*nuidp), KM_SLEEP);
    443  1.1        ad 			} else {
    444  1.1        ad 				nuidp = TAILQ_FIRST(&nmp->nm_uidlruhead);
    445  1.1        ad 				LIST_REMOVE(nuidp, nu_hash);
    446  1.1        ad 				TAILQ_REMOVE(&nmp->nm_uidlruhead, nuidp,
    447  1.1        ad 					nu_lru);
    448  1.1        ad 			}
    449  1.1        ad 			nuidp->nu_flag = 0;
    450  1.1        ad 			kauth_cred_seteuid(nuidp->nu_cr, kauth_cred_geteuid(cred));
    451  1.1        ad 			nuidp->nu_expire = time_second + NFS_KERBTTL;
    452  1.1        ad 			nuidp->nu_timestamp = ktvout;
    453  1.1        ad 			nuidp->nu_nickname = nick;
    454  1.1        ad 			memcpy(nuidp->nu_key, key, sizeof (NFSKERBKEY_T));
    455  1.1        ad 			TAILQ_INSERT_TAIL(&nmp->nm_uidlruhead, nuidp,
    456  1.1        ad 				nu_lru);
    457  1.1        ad 			LIST_INSERT_HEAD(NMUIDHASH(nmp, kauth_cred_geteuid(cred)),
    458  1.1        ad 				nuidp, nu_hash);
    459  1.1        ad 		}
    460  1.1        ad 	} else
    461  1.1        ad 		nfsm_adv(nfsm_rndup(len));
    462  1.1        ad nfsmout:
    463  1.1        ad 	*mdp = md;
    464  1.1        ad 	*dposp = dpos;
    465  1.1        ad 	return (error);
    466  1.1        ad }
    467