Home | History | Annotate | Line # | Download | only in common
nfs_commonkrpc.c revision 1.1.1.2
      1      1.1  dholland /*	$NetBSD: nfs_commonkrpc.c,v 1.1.1.2 2016/11/18 07:49:12 pgoyette Exp $	*/
      2      1.1  dholland /*-
      3      1.1  dholland  * Copyright (c) 1989, 1991, 1993, 1995
      4      1.1  dholland  *	The Regents of the University of California.  All rights reserved.
      5      1.1  dholland  *
      6      1.1  dholland  * This code is derived from software contributed to Berkeley by
      7      1.1  dholland  * Rick Macklem at The University of Guelph.
      8      1.1  dholland  *
      9      1.1  dholland  * Redistribution and use in source and binary forms, with or without
     10      1.1  dholland  * modification, are permitted provided that the following conditions
     11      1.1  dholland  * are met:
     12      1.1  dholland  * 1. Redistributions of source code must retain the above copyright
     13      1.1  dholland  *    notice, this list of conditions and the following disclaimer.
     14      1.1  dholland  * 2. Redistributions in binary form must reproduce the above copyright
     15      1.1  dholland  *    notice, this list of conditions and the following disclaimer in the
     16      1.1  dholland  *    documentation and/or other materials provided with the distribution.
     17      1.1  dholland  * 4. Neither the name of the University nor the names of its contributors
     18      1.1  dholland  *    may be used to endorse or promote products derived from this software
     19      1.1  dholland  *    without specific prior written permission.
     20      1.1  dholland  *
     21      1.1  dholland  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     22      1.1  dholland  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     23      1.1  dholland  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     24      1.1  dholland  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     25      1.1  dholland  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     26      1.1  dholland  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     27      1.1  dholland  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     28      1.1  dholland  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     29      1.1  dholland  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     30      1.1  dholland  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     31      1.1  dholland  * SUCH DAMAGE.
     32      1.1  dholland  *
     33      1.1  dholland  */
     34      1.1  dholland 
     35      1.1  dholland #include <sys/cdefs.h>
     36  1.1.1.2  pgoyette /* __FBSDID("FreeBSD: head/sys/fs/nfs/nfs_commonkrpc.c 304026 2016-08-12 22:44:59Z rmacklem "); */
     37      1.1  dholland __RCSID("$NetBSD: nfs_commonkrpc.c,v 1.1.1.2 2016/11/18 07:49:12 pgoyette Exp $");
     38      1.1  dholland 
     39      1.1  dholland /*
     40      1.1  dholland  * Socket operations for use by nfs
     41      1.1  dholland  */
     42      1.1  dholland 
     43      1.1  dholland #include "opt_kgssapi.h"
     44      1.1  dholland #include "opt_nfs.h"
     45      1.1  dholland 
     46      1.1  dholland #include <sys/param.h>
     47      1.1  dholland #include <sys/systm.h>
     48      1.1  dholland #include <sys/kernel.h>
     49      1.1  dholland #include <sys/limits.h>
     50      1.1  dholland #include <sys/lock.h>
     51      1.1  dholland #include <sys/malloc.h>
     52      1.1  dholland #include <sys/mbuf.h>
     53      1.1  dholland #include <sys/mount.h>
     54      1.1  dholland #include <sys/mutex.h>
     55      1.1  dholland #include <sys/proc.h>
     56      1.1  dholland #include <sys/signalvar.h>
     57      1.1  dholland #include <sys/syscallsubr.h>
     58      1.1  dholland #include <sys/sysctl.h>
     59      1.1  dholland #include <sys/syslog.h>
     60      1.1  dholland #include <sys/vnode.h>
     61      1.1  dholland 
     62      1.1  dholland #include <rpc/rpc.h>
     63  1.1.1.2  pgoyette #include <rpc/krpc.h>
     64      1.1  dholland 
     65      1.1  dholland #include <kgssapi/krb5/kcrypto.h>
     66      1.1  dholland 
     67      1.1  dholland #include <fs/nfs/nfsport.h>
     68      1.1  dholland 
     69      1.1  dholland #ifdef KDTRACE_HOOKS
     70      1.1  dholland #include <sys/dtrace_bsd.h>
     71      1.1  dholland 
     72      1.1  dholland dtrace_nfsclient_nfs23_start_probe_func_t
     73      1.1  dholland 		dtrace_nfscl_nfs234_start_probe;
     74      1.1  dholland 
     75      1.1  dholland dtrace_nfsclient_nfs23_done_probe_func_t
     76      1.1  dholland 		dtrace_nfscl_nfs234_done_probe;
     77      1.1  dholland 
     78      1.1  dholland /*
     79      1.1  dholland  * Registered probes by RPC type.
     80      1.1  dholland  */
     81      1.1  dholland uint32_t	nfscl_nfs2_start_probes[NFSV41_NPROCS + 1];
     82      1.1  dholland uint32_t	nfscl_nfs2_done_probes[NFSV41_NPROCS + 1];
     83      1.1  dholland 
     84      1.1  dholland uint32_t	nfscl_nfs3_start_probes[NFSV41_NPROCS + 1];
     85      1.1  dholland uint32_t	nfscl_nfs3_done_probes[NFSV41_NPROCS + 1];
     86      1.1  dholland 
     87      1.1  dholland uint32_t	nfscl_nfs4_start_probes[NFSV41_NPROCS + 1];
     88      1.1  dholland uint32_t	nfscl_nfs4_done_probes[NFSV41_NPROCS + 1];
     89      1.1  dholland #endif
     90      1.1  dholland 
     91      1.1  dholland NFSSTATESPINLOCK;
     92      1.1  dholland NFSREQSPINLOCK;
     93      1.1  dholland NFSDLOCKMUTEX;
     94  1.1.1.2  pgoyette extern struct nfsstatsv1 nfsstatsv1;
     95      1.1  dholland extern struct nfsreqhead nfsd_reqq;
     96      1.1  dholland extern int nfscl_ticks;
     97      1.1  dholland extern void (*ncl_call_invalcaches)(struct vnode *);
     98      1.1  dholland extern int nfs_numnfscbd;
     99      1.1  dholland extern int nfscl_debuglevel;
    100      1.1  dholland 
    101      1.1  dholland SVCPOOL		*nfscbd_pool;
    102      1.1  dholland static int	nfsrv_gsscallbackson = 0;
    103      1.1  dholland static int	nfs_bufpackets = 4;
    104      1.1  dholland static int	nfs_reconnects;
    105      1.1  dholland static int	nfs3_jukebox_delay = 10;
    106      1.1  dholland static int	nfs_skip_wcc_data_onerr = 1;
    107      1.1  dholland 
    108      1.1  dholland SYSCTL_DECL(_vfs_nfs);
    109      1.1  dholland 
    110      1.1  dholland SYSCTL_INT(_vfs_nfs, OID_AUTO, bufpackets, CTLFLAG_RW, &nfs_bufpackets, 0,
    111      1.1  dholland     "Buffer reservation size 2 < x < 64");
    112      1.1  dholland SYSCTL_INT(_vfs_nfs, OID_AUTO, reconnects, CTLFLAG_RD, &nfs_reconnects, 0,
    113      1.1  dholland     "Number of times the nfs client has had to reconnect");
    114      1.1  dholland SYSCTL_INT(_vfs_nfs, OID_AUTO, nfs3_jukebox_delay, CTLFLAG_RW, &nfs3_jukebox_delay, 0,
    115      1.1  dholland     "Number of seconds to delay a retry after receiving EJUKEBOX");
    116      1.1  dholland SYSCTL_INT(_vfs_nfs, OID_AUTO, skip_wcc_data_onerr, CTLFLAG_RW, &nfs_skip_wcc_data_onerr, 0,
    117      1.1  dholland     "Disable weak cache consistency checking when server returns an error");
    118      1.1  dholland 
    119      1.1  dholland static void	nfs_down(struct nfsmount *, struct thread *, const char *,
    120      1.1  dholland     int, int);
    121      1.1  dholland static void	nfs_up(struct nfsmount *, struct thread *, const char *,
    122      1.1  dholland     int, int);
    123      1.1  dholland static int	nfs_msg(struct thread *, const char *, const char *, int);
    124      1.1  dholland 
    125      1.1  dholland struct nfs_cached_auth {
    126      1.1  dholland 	int		ca_refs; /* refcount, including 1 from the cache */
    127      1.1  dholland 	uid_t		ca_uid;	 /* uid that corresponds to this auth */
    128      1.1  dholland 	AUTH		*ca_auth; /* RPC auth handle */
    129      1.1  dholland };
    130      1.1  dholland 
    131      1.1  dholland static int nfsv2_procid[NFS_V3NPROCS] = {
    132      1.1  dholland 	NFSV2PROC_NULL,
    133      1.1  dholland 	NFSV2PROC_GETATTR,
    134      1.1  dholland 	NFSV2PROC_SETATTR,
    135      1.1  dholland 	NFSV2PROC_LOOKUP,
    136      1.1  dholland 	NFSV2PROC_NOOP,
    137      1.1  dholland 	NFSV2PROC_READLINK,
    138      1.1  dholland 	NFSV2PROC_READ,
    139      1.1  dholland 	NFSV2PROC_WRITE,
    140      1.1  dholland 	NFSV2PROC_CREATE,
    141      1.1  dholland 	NFSV2PROC_MKDIR,
    142      1.1  dholland 	NFSV2PROC_SYMLINK,
    143      1.1  dholland 	NFSV2PROC_CREATE,
    144      1.1  dholland 	NFSV2PROC_REMOVE,
    145      1.1  dholland 	NFSV2PROC_RMDIR,
    146      1.1  dholland 	NFSV2PROC_RENAME,
    147      1.1  dholland 	NFSV2PROC_LINK,
    148      1.1  dholland 	NFSV2PROC_READDIR,
    149      1.1  dholland 	NFSV2PROC_NOOP,
    150      1.1  dholland 	NFSV2PROC_STATFS,
    151      1.1  dholland 	NFSV2PROC_NOOP,
    152      1.1  dholland 	NFSV2PROC_NOOP,
    153      1.1  dholland 	NFSV2PROC_NOOP,
    154      1.1  dholland };
    155      1.1  dholland 
    156      1.1  dholland /*
    157      1.1  dholland  * Initialize sockets and congestion for a new NFS connection.
    158      1.1  dholland  * We do not free the sockaddr if error.
    159      1.1  dholland  */
    160      1.1  dholland int
    161      1.1  dholland newnfs_connect(struct nfsmount *nmp, struct nfssockreq *nrp,
    162      1.1  dholland     struct ucred *cred, NFSPROC_T *p, int callback_retry_mult)
    163      1.1  dholland {
    164      1.1  dholland 	int rcvreserve, sndreserve;
    165      1.1  dholland 	int pktscale;
    166      1.1  dholland 	struct sockaddr *saddr;
    167      1.1  dholland 	struct ucred *origcred;
    168      1.1  dholland 	CLIENT *client;
    169      1.1  dholland 	struct netconfig *nconf;
    170      1.1  dholland 	struct socket *so;
    171      1.1  dholland 	int one = 1, retries, error = 0;
    172      1.1  dholland 	struct thread *td = curthread;
    173      1.1  dholland 	SVCXPRT *xprt;
    174      1.1  dholland 	struct timeval timo;
    175      1.1  dholland 
    176      1.1  dholland 	/*
    177      1.1  dholland 	 * We need to establish the socket using the credentials of
    178      1.1  dholland 	 * the mountpoint.  Some parts of this process (such as
    179      1.1  dholland 	 * sobind() and soconnect()) will use the curent thread's
    180      1.1  dholland 	 * credential instead of the socket credential.  To work
    181      1.1  dholland 	 * around this, temporarily change the current thread's
    182      1.1  dholland 	 * credential to that of the mountpoint.
    183      1.1  dholland 	 *
    184      1.1  dholland 	 * XXX: It would be better to explicitly pass the correct
    185      1.1  dholland 	 * credential to sobind() and soconnect().
    186      1.1  dholland 	 */
    187      1.1  dholland 	origcred = td->td_ucred;
    188      1.1  dholland 
    189      1.1  dholland 	/*
    190      1.1  dholland 	 * Use the credential in nr_cred, if not NULL.
    191      1.1  dholland 	 */
    192      1.1  dholland 	if (nrp->nr_cred != NULL)
    193      1.1  dholland 		td->td_ucred = nrp->nr_cred;
    194      1.1  dholland 	else
    195      1.1  dholland 		td->td_ucred = cred;
    196      1.1  dholland 	saddr = nrp->nr_nam;
    197      1.1  dholland 
    198      1.1  dholland 	if (saddr->sa_family == AF_INET)
    199      1.1  dholland 		if (nrp->nr_sotype == SOCK_DGRAM)
    200      1.1  dholland 			nconf = getnetconfigent("udp");
    201      1.1  dholland 		else
    202      1.1  dholland 			nconf = getnetconfigent("tcp");
    203      1.1  dholland 	else
    204      1.1  dholland 		if (nrp->nr_sotype == SOCK_DGRAM)
    205      1.1  dholland 			nconf = getnetconfigent("udp6");
    206      1.1  dholland 		else
    207      1.1  dholland 			nconf = getnetconfigent("tcp6");
    208      1.1  dholland 
    209      1.1  dholland 	pktscale = nfs_bufpackets;
    210      1.1  dholland 	if (pktscale < 2)
    211      1.1  dholland 		pktscale = 2;
    212      1.1  dholland 	if (pktscale > 64)
    213      1.1  dholland 		pktscale = 64;
    214      1.1  dholland 	/*
    215      1.1  dholland 	 * soreserve() can fail if sb_max is too small, so shrink pktscale
    216      1.1  dholland 	 * and try again if there is an error.
    217      1.1  dholland 	 * Print a log message suggesting increasing sb_max.
    218      1.1  dholland 	 * Creating a socket and doing this is necessary since, if the
    219      1.1  dholland 	 * reservation sizes are too large and will make soreserve() fail,
    220      1.1  dholland 	 * the connection will work until a large send is attempted and
    221      1.1  dholland 	 * then it will loop in the krpc code.
    222      1.1  dholland 	 */
    223      1.1  dholland 	so = NULL;
    224      1.1  dholland 	saddr = NFSSOCKADDR(nrp->nr_nam, struct sockaddr *);
    225      1.1  dholland 	error = socreate(saddr->sa_family, &so, nrp->nr_sotype,
    226      1.1  dholland 	    nrp->nr_soproto, td->td_ucred, td);
    227      1.1  dholland 	if (error) {
    228      1.1  dholland 		td->td_ucred = origcred;
    229      1.1  dholland 		goto out;
    230      1.1  dholland 	}
    231      1.1  dholland 	do {
    232      1.1  dholland 	    if (error != 0 && pktscale > 2)
    233      1.1  dholland 		pktscale--;
    234      1.1  dholland 	    if (nrp->nr_sotype == SOCK_DGRAM) {
    235      1.1  dholland 		if (nmp != NULL) {
    236      1.1  dholland 			sndreserve = (NFS_MAXDGRAMDATA + NFS_MAXPKTHDR) *
    237      1.1  dholland 			    pktscale;
    238      1.1  dholland 			rcvreserve = (NFS_MAXDGRAMDATA + NFS_MAXPKTHDR) *
    239      1.1  dholland 			    pktscale;
    240      1.1  dholland 		} else {
    241      1.1  dholland 			sndreserve = rcvreserve = 1024 * pktscale;
    242      1.1  dholland 		}
    243      1.1  dholland 	    } else {
    244      1.1  dholland 		if (nrp->nr_sotype != SOCK_STREAM)
    245      1.1  dholland 			panic("nfscon sotype");
    246      1.1  dholland 		if (nmp != NULL) {
    247      1.1  dholland 			sndreserve = (NFS_MAXBSIZE + NFS_MAXPKTHDR +
    248      1.1  dholland 			    sizeof (u_int32_t)) * pktscale;
    249      1.1  dholland 			rcvreserve = (NFS_MAXBSIZE + NFS_MAXPKTHDR +
    250      1.1  dholland 			    sizeof (u_int32_t)) * pktscale;
    251      1.1  dholland 		} else {
    252      1.1  dholland 			sndreserve = rcvreserve = 1024 * pktscale;
    253      1.1  dholland 		}
    254      1.1  dholland 	    }
    255      1.1  dholland 	    error = soreserve(so, sndreserve, rcvreserve);
    256      1.1  dholland 	} while (error != 0 && pktscale > 2);
    257      1.1  dholland 	soclose(so);
    258      1.1  dholland 	if (error) {
    259      1.1  dholland 		td->td_ucred = origcred;
    260      1.1  dholland 		goto out;
    261      1.1  dholland 	}
    262      1.1  dholland 
    263      1.1  dholland 	client = clnt_reconnect_create(nconf, saddr, nrp->nr_prog,
    264      1.1  dholland 	    nrp->nr_vers, sndreserve, rcvreserve);
    265  1.1.1.2  pgoyette 	CLNT_CONTROL(client, CLSET_WAITCHAN, "nfsreq");
    266      1.1  dholland 	if (nmp != NULL) {
    267      1.1  dholland 		if ((nmp->nm_flag & NFSMNT_INT))
    268      1.1  dholland 			CLNT_CONTROL(client, CLSET_INTERRUPTIBLE, &one);
    269      1.1  dholland 		if ((nmp->nm_flag & NFSMNT_RESVPORT))
    270      1.1  dholland 			CLNT_CONTROL(client, CLSET_PRIVPORT, &one);
    271      1.1  dholland 		if (NFSHASSOFT(nmp)) {
    272      1.1  dholland 			if (nmp->nm_sotype == SOCK_DGRAM)
    273      1.1  dholland 				/*
    274      1.1  dholland 				 * For UDP, the large timeout for a reconnect
    275      1.1  dholland 				 * will be set to "nm_retry * nm_timeo / 2", so
    276      1.1  dholland 				 * we only want to do 2 reconnect timeout
    277      1.1  dholland 				 * retries.
    278      1.1  dholland 				 */
    279      1.1  dholland 				retries = 2;
    280      1.1  dholland 			else
    281      1.1  dholland 				retries = nmp->nm_retry;
    282      1.1  dholland 		} else
    283      1.1  dholland 			retries = INT_MAX;
    284      1.1  dholland 		if (NFSHASNFSV4N(nmp)) {
    285      1.1  dholland 			/*
    286      1.1  dholland 			 * Make sure the nfscbd_pool doesn't get destroyed
    287      1.1  dholland 			 * while doing this.
    288      1.1  dholland 			 */
    289      1.1  dholland 			NFSD_LOCK();
    290      1.1  dholland 			if (nfs_numnfscbd > 0) {
    291      1.1  dholland 				nfs_numnfscbd++;
    292      1.1  dholland 				NFSD_UNLOCK();
    293      1.1  dholland 				xprt = svc_vc_create_backchannel(nfscbd_pool);
    294      1.1  dholland 				CLNT_CONTROL(client, CLSET_BACKCHANNEL, xprt);
    295      1.1  dholland 				NFSD_LOCK();
    296      1.1  dholland 				nfs_numnfscbd--;
    297      1.1  dholland 				if (nfs_numnfscbd == 0)
    298      1.1  dholland 					wakeup(&nfs_numnfscbd);
    299      1.1  dholland 			}
    300      1.1  dholland 			NFSD_UNLOCK();
    301      1.1  dholland 		}
    302      1.1  dholland 	} else {
    303      1.1  dholland 		/*
    304      1.1  dholland 		 * Three cases:
    305      1.1  dholland 		 * - Null RPC callback to client
    306      1.1  dholland 		 * - Non-Null RPC callback to client, wait a little longer
    307      1.1  dholland 		 * - upcalls to nfsuserd and gssd (clp == NULL)
    308      1.1  dholland 		 */
    309      1.1  dholland 		if (callback_retry_mult == 0) {
    310      1.1  dholland 			retries = NFSV4_UPCALLRETRY;
    311      1.1  dholland 			CLNT_CONTROL(client, CLSET_PRIVPORT, &one);
    312      1.1  dholland 		} else {
    313      1.1  dholland 			retries = NFSV4_CALLBACKRETRY * callback_retry_mult;
    314      1.1  dholland 		}
    315      1.1  dholland 	}
    316      1.1  dholland 	CLNT_CONTROL(client, CLSET_RETRIES, &retries);
    317      1.1  dholland 
    318      1.1  dholland 	if (nmp != NULL) {
    319      1.1  dholland 		/*
    320      1.1  dholland 		 * For UDP, there are 2 timeouts:
    321      1.1  dholland 		 * - CLSET_RETRY_TIMEOUT sets the initial timeout for the timer
    322      1.1  dholland 		 *   that does a retransmit of an RPC request using the same
    323      1.1  dholland 		 *   socket and xid. This is what you normally want to do,
    324      1.1  dholland 		 *   since NFS servers depend on "same xid" for their
    325      1.1  dholland 		 *   Duplicate Request Cache.
    326      1.1  dholland 		 * - timeout specified in CLNT_CALL_MBUF(), which specifies when
    327      1.1  dholland 		 *   retransmits on the same socket should fail and a fresh
    328      1.1  dholland 		 *   socket created. Each of these timeouts counts as one
    329      1.1  dholland 		 *   CLSET_RETRIES as set above.
    330      1.1  dholland 		 * Set the initial retransmit timeout for UDP. This timeout
    331      1.1  dholland 		 * doesn't exist for TCP and the following call just fails,
    332      1.1  dholland 		 * which is ok.
    333      1.1  dholland 		 */
    334      1.1  dholland 		timo.tv_sec = nmp->nm_timeo / NFS_HZ;
    335      1.1  dholland 		timo.tv_usec = (nmp->nm_timeo % NFS_HZ) * 1000000 / NFS_HZ;
    336      1.1  dholland 		CLNT_CONTROL(client, CLSET_RETRY_TIMEOUT, &timo);
    337      1.1  dholland 	}
    338      1.1  dholland 
    339      1.1  dholland 	mtx_lock(&nrp->nr_mtx);
    340      1.1  dholland 	if (nrp->nr_client != NULL) {
    341  1.1.1.2  pgoyette 		mtx_unlock(&nrp->nr_mtx);
    342      1.1  dholland 		/*
    343      1.1  dholland 		 * Someone else already connected.
    344      1.1  dholland 		 */
    345      1.1  dholland 		CLNT_RELEASE(client);
    346      1.1  dholland 	} else {
    347      1.1  dholland 		nrp->nr_client = client;
    348  1.1.1.2  pgoyette 		/*
    349  1.1.1.2  pgoyette 		 * Protocols that do not require connections may be optionally
    350  1.1.1.2  pgoyette 		 * left unconnected for servers that reply from a port other
    351  1.1.1.2  pgoyette 		 * than NFS_PORT.
    352  1.1.1.2  pgoyette 		 */
    353  1.1.1.2  pgoyette 		if (nmp == NULL || (nmp->nm_flag & NFSMNT_NOCONN) == 0) {
    354  1.1.1.2  pgoyette 			mtx_unlock(&nrp->nr_mtx);
    355  1.1.1.2  pgoyette 			CLNT_CONTROL(client, CLSET_CONNECT, &one);
    356  1.1.1.2  pgoyette 		} else
    357  1.1.1.2  pgoyette 			mtx_unlock(&nrp->nr_mtx);
    358      1.1  dholland 	}
    359      1.1  dholland 
    360      1.1  dholland 
    361      1.1  dholland 	/* Restore current thread's credentials. */
    362      1.1  dholland 	td->td_ucred = origcred;
    363      1.1  dholland 
    364      1.1  dholland out:
    365      1.1  dholland 	NFSEXITCODE(error);
    366      1.1  dholland 	return (error);
    367      1.1  dholland }
    368      1.1  dholland 
    369      1.1  dholland /*
    370      1.1  dholland  * NFS disconnect. Clean up and unlink.
    371      1.1  dholland  */
    372      1.1  dholland void
    373      1.1  dholland newnfs_disconnect(struct nfssockreq *nrp)
    374      1.1  dholland {
    375      1.1  dholland 	CLIENT *client;
    376      1.1  dholland 
    377      1.1  dholland 	mtx_lock(&nrp->nr_mtx);
    378      1.1  dholland 	if (nrp->nr_client != NULL) {
    379      1.1  dholland 		client = nrp->nr_client;
    380      1.1  dholland 		nrp->nr_client = NULL;
    381      1.1  dholland 		mtx_unlock(&nrp->nr_mtx);
    382      1.1  dholland 		rpc_gss_secpurge_call(client);
    383      1.1  dholland 		CLNT_CLOSE(client);
    384      1.1  dholland 		CLNT_RELEASE(client);
    385      1.1  dholland 	} else {
    386      1.1  dholland 		mtx_unlock(&nrp->nr_mtx);
    387      1.1  dholland 	}
    388      1.1  dholland }
    389      1.1  dholland 
    390      1.1  dholland static AUTH *
    391      1.1  dholland nfs_getauth(struct nfssockreq *nrp, int secflavour, char *clnt_principal,
    392      1.1  dholland     char *srv_principal, gss_OID mech_oid, struct ucred *cred)
    393      1.1  dholland {
    394      1.1  dholland 	rpc_gss_service_t svc;
    395      1.1  dholland 	AUTH *auth;
    396      1.1  dholland 
    397      1.1  dholland 	switch (secflavour) {
    398      1.1  dholland 	case RPCSEC_GSS_KRB5:
    399      1.1  dholland 	case RPCSEC_GSS_KRB5I:
    400      1.1  dholland 	case RPCSEC_GSS_KRB5P:
    401      1.1  dholland 		if (!mech_oid) {
    402      1.1  dholland 			if (!rpc_gss_mech_to_oid_call("kerberosv5", &mech_oid))
    403      1.1  dholland 				return (NULL);
    404      1.1  dholland 		}
    405      1.1  dholland 		if (secflavour == RPCSEC_GSS_KRB5)
    406      1.1  dholland 			svc = rpc_gss_svc_none;
    407      1.1  dholland 		else if (secflavour == RPCSEC_GSS_KRB5I)
    408      1.1  dholland 			svc = rpc_gss_svc_integrity;
    409      1.1  dholland 		else
    410      1.1  dholland 			svc = rpc_gss_svc_privacy;
    411      1.1  dholland 
    412      1.1  dholland 		if (clnt_principal == NULL)
    413      1.1  dholland 			auth = rpc_gss_secfind_call(nrp->nr_client, cred,
    414      1.1  dholland 			    srv_principal, mech_oid, svc);
    415      1.1  dholland 		else {
    416      1.1  dholland 			auth = rpc_gss_seccreate_call(nrp->nr_client, cred,
    417      1.1  dholland 			    clnt_principal, srv_principal, "kerberosv5",
    418      1.1  dholland 			    svc, NULL, NULL, NULL);
    419      1.1  dholland 			return (auth);
    420      1.1  dholland 		}
    421      1.1  dholland 		if (auth != NULL)
    422      1.1  dholland 			return (auth);
    423      1.1  dholland 		/* fallthrough */
    424      1.1  dholland 	case AUTH_SYS:
    425      1.1  dholland 	default:
    426      1.1  dholland 		return (authunix_create(cred));
    427      1.1  dholland 
    428      1.1  dholland 	}
    429      1.1  dholland }
    430      1.1  dholland 
    431      1.1  dholland /*
    432      1.1  dholland  * Callback from the RPC code to generate up/down notifications.
    433      1.1  dholland  */
    434      1.1  dholland 
    435      1.1  dholland struct nfs_feedback_arg {
    436      1.1  dholland 	struct nfsmount *nf_mount;
    437      1.1  dholland 	int		nf_lastmsg;	/* last tprintf */
    438      1.1  dholland 	int		nf_tprintfmsg;
    439      1.1  dholland 	struct thread	*nf_td;
    440      1.1  dholland };
    441      1.1  dholland 
    442      1.1  dholland static void
    443      1.1  dholland nfs_feedback(int type, int proc, void *arg)
    444      1.1  dholland {
    445      1.1  dholland 	struct nfs_feedback_arg *nf = (struct nfs_feedback_arg *) arg;
    446      1.1  dholland 	struct nfsmount *nmp = nf->nf_mount;
    447      1.1  dholland 	time_t now;
    448      1.1  dholland 
    449      1.1  dholland 	switch (type) {
    450      1.1  dholland 	case FEEDBACK_REXMIT2:
    451      1.1  dholland 	case FEEDBACK_RECONNECT:
    452      1.1  dholland 		now = NFSD_MONOSEC;
    453      1.1  dholland 		if (nf->nf_lastmsg + nmp->nm_tprintf_delay < now) {
    454      1.1  dholland 			nfs_down(nmp, nf->nf_td,
    455      1.1  dholland 			    "not responding", 0, NFSSTA_TIMEO);
    456      1.1  dholland 			nf->nf_tprintfmsg = TRUE;
    457      1.1  dholland 			nf->nf_lastmsg = now;
    458      1.1  dholland 		}
    459      1.1  dholland 		break;
    460      1.1  dholland 
    461      1.1  dholland 	case FEEDBACK_OK:
    462      1.1  dholland 		nfs_up(nf->nf_mount, nf->nf_td,
    463      1.1  dholland 		    "is alive again", NFSSTA_TIMEO, nf->nf_tprintfmsg);
    464      1.1  dholland 		break;
    465      1.1  dholland 	}
    466      1.1  dholland }
    467      1.1  dholland 
    468      1.1  dholland /*
    469      1.1  dholland  * newnfs_request - goes something like this
    470      1.1  dholland  *	- does the rpc by calling the krpc layer
    471      1.1  dholland  *	- break down rpc header and return with nfs reply
    472      1.1  dholland  * nb: always frees up nd_mreq mbuf list
    473      1.1  dholland  */
    474      1.1  dholland int
    475      1.1  dholland newnfs_request(struct nfsrv_descript *nd, struct nfsmount *nmp,
    476      1.1  dholland     struct nfsclient *clp, struct nfssockreq *nrp, vnode_t vp,
    477      1.1  dholland     struct thread *td, struct ucred *cred, u_int32_t prog, u_int32_t vers,
    478      1.1  dholland     u_char *retsum, int toplevel, u_int64_t *xidp, struct nfsclsession *sep)
    479      1.1  dholland {
    480      1.1  dholland 	u_int32_t retseq, retval, *tl;
    481      1.1  dholland 	time_t waituntil;
    482      1.1  dholland 	int i = 0, j = 0, opcnt, set_sigset = 0, slot;
    483      1.1  dholland 	int trycnt, error = 0, usegssname = 0, secflavour = AUTH_SYS;
    484      1.1  dholland 	int freeslot, timeo;
    485      1.1  dholland 	u_int16_t procnum;
    486      1.1  dholland 	u_int trylater_delay = 1;
    487      1.1  dholland 	struct nfs_feedback_arg nf;
    488      1.1  dholland 	struct timeval timo;
    489      1.1  dholland 	AUTH *auth;
    490      1.1  dholland 	struct rpc_callextra ext;
    491      1.1  dholland 	enum clnt_stat stat;
    492      1.1  dholland 	struct nfsreq *rep = NULL;
    493      1.1  dholland 	char *srv_principal = NULL, *clnt_principal = NULL;
    494      1.1  dholland 	sigset_t oldset;
    495      1.1  dholland 	struct ucred *authcred;
    496      1.1  dholland 
    497      1.1  dholland 	if (xidp != NULL)
    498      1.1  dholland 		*xidp = 0;
    499      1.1  dholland 	/* Reject requests while attempting a forced unmount. */
    500      1.1  dholland 	if (nmp != NULL && (nmp->nm_mountp->mnt_kern_flag & MNTK_UNMOUNTF)) {
    501      1.1  dholland 		m_freem(nd->nd_mreq);
    502      1.1  dholland 		return (ESTALE);
    503      1.1  dholland 	}
    504      1.1  dholland 
    505      1.1  dholland 	/*
    506      1.1  dholland 	 * Set authcred, which is used to acquire RPC credentials to
    507      1.1  dholland 	 * the cred argument, by default. The crhold() should not be
    508      1.1  dholland 	 * necessary, but will ensure that some future code change
    509      1.1  dholland 	 * doesn't result in the credential being free'd prematurely.
    510      1.1  dholland 	 */
    511      1.1  dholland 	authcred = crhold(cred);
    512      1.1  dholland 
    513      1.1  dholland 	/* For client side interruptible mounts, mask off the signals. */
    514      1.1  dholland 	if (nmp != NULL && td != NULL && NFSHASINT(nmp)) {
    515      1.1  dholland 		newnfs_set_sigmask(td, &oldset);
    516      1.1  dholland 		set_sigset = 1;
    517      1.1  dholland 	}
    518      1.1  dholland 
    519      1.1  dholland 	/*
    520      1.1  dholland 	 * XXX if not already connected call nfs_connect now. Longer
    521      1.1  dholland 	 * term, change nfs_mount to call nfs_connect unconditionally
    522      1.1  dholland 	 * and let clnt_reconnect_create handle reconnects.
    523      1.1  dholland 	 */
    524      1.1  dholland 	if (nrp->nr_client == NULL)
    525      1.1  dholland 		newnfs_connect(nmp, nrp, cred, td, 0);
    526      1.1  dholland 
    527      1.1  dholland 	/*
    528      1.1  dholland 	 * For a client side mount, nmp is != NULL and clp == NULL. For
    529      1.1  dholland 	 * server calls (callbacks or upcalls), nmp == NULL.
    530      1.1  dholland 	 */
    531      1.1  dholland 	if (clp != NULL) {
    532      1.1  dholland 		NFSLOCKSTATE();
    533      1.1  dholland 		if ((clp->lc_flags & LCL_GSS) && nfsrv_gsscallbackson) {
    534      1.1  dholland 			secflavour = RPCSEC_GSS_KRB5;
    535      1.1  dholland 			if (nd->nd_procnum != NFSPROC_NULL) {
    536      1.1  dholland 				if (clp->lc_flags & LCL_GSSINTEGRITY)
    537      1.1  dholland 					secflavour = RPCSEC_GSS_KRB5I;
    538      1.1  dholland 				else if (clp->lc_flags & LCL_GSSPRIVACY)
    539      1.1  dholland 					secflavour = RPCSEC_GSS_KRB5P;
    540      1.1  dholland 			}
    541      1.1  dholland 		}
    542      1.1  dholland 		NFSUNLOCKSTATE();
    543      1.1  dholland 	} else if (nmp != NULL && NFSHASKERB(nmp) &&
    544      1.1  dholland 	     nd->nd_procnum != NFSPROC_NULL) {
    545      1.1  dholland 		if (NFSHASALLGSSNAME(nmp) && nmp->nm_krbnamelen > 0)
    546      1.1  dholland 			nd->nd_flag |= ND_USEGSSNAME;
    547      1.1  dholland 		if ((nd->nd_flag & ND_USEGSSNAME) != 0) {
    548      1.1  dholland 			/*
    549      1.1  dholland 			 * If there is a client side host based credential,
    550      1.1  dholland 			 * use that, otherwise use the system uid, if set.
    551      1.1  dholland 			 * The system uid is in the nmp->nm_sockreq.nr_cred
    552      1.1  dholland 			 * credentials.
    553      1.1  dholland 			 */
    554      1.1  dholland 			if (nmp->nm_krbnamelen > 0) {
    555      1.1  dholland 				usegssname = 1;
    556      1.1  dholland 				clnt_principal = nmp->nm_krbname;
    557      1.1  dholland 			} else if (nmp->nm_uid != (uid_t)-1) {
    558      1.1  dholland 				KASSERT(nmp->nm_sockreq.nr_cred != NULL,
    559      1.1  dholland 				    ("newnfs_request: NULL nr_cred"));
    560      1.1  dholland 				crfree(authcred);
    561      1.1  dholland 				authcred = crhold(nmp->nm_sockreq.nr_cred);
    562      1.1  dholland 			}
    563      1.1  dholland 		} else if (nmp->nm_krbnamelen == 0 &&
    564      1.1  dholland 		    nmp->nm_uid != (uid_t)-1 && cred->cr_uid == (uid_t)0) {
    565      1.1  dholland 			/*
    566      1.1  dholland 			 * If there is no host based principal name and
    567      1.1  dholland 			 * the system uid is set and this is root, use the
    568      1.1  dholland 			 * system uid, since root won't have user
    569      1.1  dholland 			 * credentials in a credentials cache file.
    570      1.1  dholland 			 * The system uid is in the nmp->nm_sockreq.nr_cred
    571      1.1  dholland 			 * credentials.
    572      1.1  dholland 			 */
    573      1.1  dholland 			KASSERT(nmp->nm_sockreq.nr_cred != NULL,
    574      1.1  dholland 			    ("newnfs_request: NULL nr_cred"));
    575      1.1  dholland 			crfree(authcred);
    576      1.1  dholland 			authcred = crhold(nmp->nm_sockreq.nr_cred);
    577      1.1  dholland 		}
    578      1.1  dholland 		if (NFSHASINTEGRITY(nmp))
    579      1.1  dholland 			secflavour = RPCSEC_GSS_KRB5I;
    580      1.1  dholland 		else if (NFSHASPRIVACY(nmp))
    581      1.1  dholland 			secflavour = RPCSEC_GSS_KRB5P;
    582      1.1  dholland 		else
    583      1.1  dholland 			secflavour = RPCSEC_GSS_KRB5;
    584      1.1  dholland 		srv_principal = NFSMNT_SRVKRBNAME(nmp);
    585      1.1  dholland 	} else if (nmp != NULL && !NFSHASKERB(nmp) &&
    586      1.1  dholland 	    nd->nd_procnum != NFSPROC_NULL &&
    587      1.1  dholland 	    (nd->nd_flag & ND_USEGSSNAME) != 0) {
    588      1.1  dholland 		/*
    589      1.1  dholland 		 * Use the uid that did the mount when the RPC is doing
    590      1.1  dholland 		 * NFSv4 system operations, as indicated by the
    591      1.1  dholland 		 * ND_USEGSSNAME flag, for the AUTH_SYS case.
    592      1.1  dholland 		 * The credentials in nm_sockreq.nr_cred were used for the
    593      1.1  dholland 		 * mount.
    594      1.1  dholland 		 */
    595      1.1  dholland 		KASSERT(nmp->nm_sockreq.nr_cred != NULL,
    596      1.1  dholland 		    ("newnfs_request: NULL nr_cred"));
    597      1.1  dholland 		crfree(authcred);
    598      1.1  dholland 		authcred = crhold(nmp->nm_sockreq.nr_cred);
    599      1.1  dholland 	}
    600      1.1  dholland 
    601      1.1  dholland 	if (nmp != NULL) {
    602      1.1  dholland 		bzero(&nf, sizeof(struct nfs_feedback_arg));
    603      1.1  dholland 		nf.nf_mount = nmp;
    604      1.1  dholland 		nf.nf_td = td;
    605      1.1  dholland 		nf.nf_lastmsg = NFSD_MONOSEC -
    606      1.1  dholland 		    ((nmp->nm_tprintf_delay)-(nmp->nm_tprintf_initial_delay));
    607      1.1  dholland 	}
    608      1.1  dholland 
    609      1.1  dholland 	if (nd->nd_procnum == NFSPROC_NULL)
    610      1.1  dholland 		auth = authnone_create();
    611      1.1  dholland 	else if (usegssname) {
    612      1.1  dholland 		/*
    613      1.1  dholland 		 * For this case, the authenticator is held in the
    614      1.1  dholland 		 * nfssockreq structure, so don't release the reference count
    615      1.1  dholland 		 * held on it. --> Don't AUTH_DESTROY() it in this function.
    616      1.1  dholland 		 */
    617      1.1  dholland 		if (nrp->nr_auth == NULL)
    618      1.1  dholland 			nrp->nr_auth = nfs_getauth(nrp, secflavour,
    619      1.1  dholland 			    clnt_principal, srv_principal, NULL, authcred);
    620      1.1  dholland 		else
    621      1.1  dholland 			rpc_gss_refresh_auth_call(nrp->nr_auth);
    622      1.1  dholland 		auth = nrp->nr_auth;
    623      1.1  dholland 	} else
    624      1.1  dholland 		auth = nfs_getauth(nrp, secflavour, NULL,
    625      1.1  dholland 		    srv_principal, NULL, authcred);
    626      1.1  dholland 	crfree(authcred);
    627      1.1  dholland 	if (auth == NULL) {
    628      1.1  dholland 		m_freem(nd->nd_mreq);
    629      1.1  dholland 		if (set_sigset)
    630      1.1  dholland 			newnfs_restore_sigmask(td, &oldset);
    631      1.1  dholland 		return (EACCES);
    632      1.1  dholland 	}
    633      1.1  dholland 	bzero(&ext, sizeof(ext));
    634      1.1  dholland 	ext.rc_auth = auth;
    635      1.1  dholland 	if (nmp != NULL) {
    636      1.1  dholland 		ext.rc_feedback = nfs_feedback;
    637      1.1  dholland 		ext.rc_feedback_arg = &nf;
    638      1.1  dholland 	}
    639      1.1  dholland 
    640      1.1  dholland 	procnum = nd->nd_procnum;
    641      1.1  dholland 	if ((nd->nd_flag & ND_NFSV4) &&
    642      1.1  dholland 	    nd->nd_procnum != NFSPROC_NULL &&
    643      1.1  dholland 	    nd->nd_procnum != NFSV4PROC_CBCOMPOUND)
    644      1.1  dholland 		procnum = NFSV4PROC_COMPOUND;
    645      1.1  dholland 
    646      1.1  dholland 	if (nmp != NULL) {
    647  1.1.1.2  pgoyette 		NFSINCRGLOBAL(nfsstatsv1.rpcrequests);
    648      1.1  dholland 
    649      1.1  dholland 		/* Map the procnum to the old NFSv2 one, as required. */
    650      1.1  dholland 		if ((nd->nd_flag & ND_NFSV2) != 0) {
    651      1.1  dholland 			if (nd->nd_procnum < NFS_V3NPROCS)
    652      1.1  dholland 				procnum = nfsv2_procid[nd->nd_procnum];
    653      1.1  dholland 			else
    654      1.1  dholland 				procnum = NFSV2PROC_NOOP;
    655      1.1  dholland 		}
    656      1.1  dholland 
    657      1.1  dholland 		/*
    658      1.1  dholland 		 * Now only used for the R_DONTRECOVER case, but until that is
    659      1.1  dholland 		 * supported within the krpc code, I need to keep a queue of
    660      1.1  dholland 		 * outstanding RPCs for nfsv4 client requests.
    661      1.1  dholland 		 */
    662      1.1  dholland 		if ((nd->nd_flag & ND_NFSV4) && procnum == NFSV4PROC_COMPOUND)
    663      1.1  dholland 			MALLOC(rep, struct nfsreq *, sizeof(struct nfsreq),
    664      1.1  dholland 			    M_NFSDREQ, M_WAITOK);
    665      1.1  dholland #ifdef KDTRACE_HOOKS
    666      1.1  dholland 		if (dtrace_nfscl_nfs234_start_probe != NULL) {
    667      1.1  dholland 			uint32_t probe_id;
    668      1.1  dholland 			int probe_procnum;
    669      1.1  dholland 
    670      1.1  dholland 			if (nd->nd_flag & ND_NFSV4) {
    671      1.1  dholland 				probe_id =
    672      1.1  dholland 				    nfscl_nfs4_start_probes[nd->nd_procnum];
    673      1.1  dholland 				probe_procnum = nd->nd_procnum;
    674      1.1  dholland 			} else if (nd->nd_flag & ND_NFSV3) {
    675      1.1  dholland 				probe_id = nfscl_nfs3_start_probes[procnum];
    676      1.1  dholland 				probe_procnum = procnum;
    677      1.1  dholland 			} else {
    678      1.1  dholland 				probe_id =
    679      1.1  dholland 				    nfscl_nfs2_start_probes[nd->nd_procnum];
    680      1.1  dholland 				probe_procnum = procnum;
    681      1.1  dholland 			}
    682      1.1  dholland 			if (probe_id != 0)
    683      1.1  dholland 				(dtrace_nfscl_nfs234_start_probe)
    684      1.1  dholland 				    (probe_id, vp, nd->nd_mreq, cred,
    685      1.1  dholland 				     probe_procnum);
    686      1.1  dholland 		}
    687      1.1  dholland #endif
    688      1.1  dholland 	}
    689      1.1  dholland 	trycnt = 0;
    690      1.1  dholland 	freeslot = -1;		/* Set to slot that needs to be free'd */
    691      1.1  dholland tryagain:
    692      1.1  dholland 	slot = -1;		/* Slot that needs a sequence# increment. */
    693      1.1  dholland 	/*
    694      1.1  dholland 	 * This timeout specifies when a new socket should be created,
    695      1.1  dholland 	 * along with new xid values. For UDP, this should be done
    696      1.1  dholland 	 * infrequently, since retransmits of RPC requests should normally
    697      1.1  dholland 	 * use the same xid.
    698      1.1  dholland 	 */
    699      1.1  dholland 	if (nmp == NULL) {
    700      1.1  dholland 		timo.tv_usec = 0;
    701      1.1  dholland 		if (clp == NULL)
    702      1.1  dholland 			timo.tv_sec = NFSV4_UPCALLTIMEO;
    703      1.1  dholland 		else
    704      1.1  dholland 			timo.tv_sec = NFSV4_CALLBACKTIMEO;
    705      1.1  dholland 	} else {
    706      1.1  dholland 		if (nrp->nr_sotype != SOCK_DGRAM) {
    707      1.1  dholland 			timo.tv_usec = 0;
    708      1.1  dholland 			if ((nmp->nm_flag & NFSMNT_NFSV4))
    709      1.1  dholland 				timo.tv_sec = INT_MAX;
    710      1.1  dholland 			else
    711      1.1  dholland 				timo.tv_sec = NFS_TCPTIMEO;
    712      1.1  dholland 		} else {
    713      1.1  dholland 			if (NFSHASSOFT(nmp)) {
    714      1.1  dholland 				/*
    715      1.1  dholland 				 * CLSET_RETRIES is set to 2, so this should be
    716      1.1  dholland 				 * half of the total timeout required.
    717      1.1  dholland 				 */
    718      1.1  dholland 				timeo = nmp->nm_retry * nmp->nm_timeo / 2;
    719      1.1  dholland 				if (timeo < 1)
    720      1.1  dholland 					timeo = 1;
    721      1.1  dholland 				timo.tv_sec = timeo / NFS_HZ;
    722      1.1  dholland 				timo.tv_usec = (timeo % NFS_HZ) * 1000000 /
    723      1.1  dholland 				    NFS_HZ;
    724      1.1  dholland 			} else {
    725      1.1  dholland 				/* For UDP hard mounts, use a large value. */
    726      1.1  dholland 				timo.tv_sec = NFS_MAXTIMEO / NFS_HZ;
    727      1.1  dholland 				timo.tv_usec = 0;
    728      1.1  dholland 			}
    729      1.1  dholland 		}
    730      1.1  dholland 
    731      1.1  dholland 		if (rep != NULL) {
    732      1.1  dholland 			rep->r_flags = 0;
    733      1.1  dholland 			rep->r_nmp = nmp;
    734      1.1  dholland 			/*
    735      1.1  dholland 			 * Chain request into list of outstanding requests.
    736      1.1  dholland 			 */
    737      1.1  dholland 			NFSLOCKREQ();
    738      1.1  dholland 			TAILQ_INSERT_TAIL(&nfsd_reqq, rep, r_chain);
    739      1.1  dholland 			NFSUNLOCKREQ();
    740      1.1  dholland 		}
    741      1.1  dholland 	}
    742      1.1  dholland 
    743      1.1  dholland 	nd->nd_mrep = NULL;
    744  1.1.1.2  pgoyette 	if (clp != NULL && sep != NULL)
    745  1.1.1.2  pgoyette 		stat = clnt_bck_call(nrp->nr_client, &ext, procnum,
    746  1.1.1.2  pgoyette 		    nd->nd_mreq, &nd->nd_mrep, timo, sep->nfsess_xprt);
    747  1.1.1.2  pgoyette 	else
    748  1.1.1.2  pgoyette 		stat = CLNT_CALL_MBUF(nrp->nr_client, &ext, procnum,
    749  1.1.1.2  pgoyette 		    nd->nd_mreq, &nd->nd_mrep, timo);
    750      1.1  dholland 
    751      1.1  dholland 	if (rep != NULL) {
    752      1.1  dholland 		/*
    753      1.1  dholland 		 * RPC done, unlink the request.
    754      1.1  dholland 		 */
    755      1.1  dholland 		NFSLOCKREQ();
    756      1.1  dholland 		TAILQ_REMOVE(&nfsd_reqq, rep, r_chain);
    757      1.1  dholland 		NFSUNLOCKREQ();
    758      1.1  dholland 	}
    759      1.1  dholland 
    760      1.1  dholland 	/*
    761      1.1  dholland 	 * If there was a successful reply and a tprintf msg.
    762      1.1  dholland 	 * tprintf a response.
    763      1.1  dholland 	 */
    764      1.1  dholland 	if (stat == RPC_SUCCESS) {
    765      1.1  dholland 		error = 0;
    766      1.1  dholland 	} else if (stat == RPC_TIMEDOUT) {
    767  1.1.1.2  pgoyette 		NFSINCRGLOBAL(nfsstatsv1.rpctimeouts);
    768      1.1  dholland 		error = ETIMEDOUT;
    769      1.1  dholland 	} else if (stat == RPC_VERSMISMATCH) {
    770  1.1.1.2  pgoyette 		NFSINCRGLOBAL(nfsstatsv1.rpcinvalid);
    771      1.1  dholland 		error = EOPNOTSUPP;
    772      1.1  dholland 	} else if (stat == RPC_PROGVERSMISMATCH) {
    773  1.1.1.2  pgoyette 		NFSINCRGLOBAL(nfsstatsv1.rpcinvalid);
    774      1.1  dholland 		error = EPROTONOSUPPORT;
    775      1.1  dholland 	} else if (stat == RPC_INTR) {
    776      1.1  dholland 		error = EINTR;
    777      1.1  dholland 	} else {
    778  1.1.1.2  pgoyette 		NFSINCRGLOBAL(nfsstatsv1.rpcinvalid);
    779      1.1  dholland 		error = EACCES;
    780      1.1  dholland 	}
    781      1.1  dholland 	if (error) {
    782      1.1  dholland 		m_freem(nd->nd_mreq);
    783      1.1  dholland 		if (usegssname == 0)
    784      1.1  dholland 			AUTH_DESTROY(auth);
    785      1.1  dholland 		if (rep != NULL)
    786      1.1  dholland 			FREE((caddr_t)rep, M_NFSDREQ);
    787      1.1  dholland 		if (set_sigset)
    788      1.1  dholland 			newnfs_restore_sigmask(td, &oldset);
    789      1.1  dholland 		return (error);
    790      1.1  dholland 	}
    791      1.1  dholland 
    792      1.1  dholland 	KASSERT(nd->nd_mrep != NULL, ("mrep shouldn't be NULL if no error\n"));
    793      1.1  dholland 
    794      1.1  dholland 	/*
    795      1.1  dholland 	 * Search for any mbufs that are not a multiple of 4 bytes long
    796      1.1  dholland 	 * or with m_data not longword aligned.
    797      1.1  dholland 	 * These could cause pointer alignment problems, so copy them to
    798      1.1  dholland 	 * well aligned mbufs.
    799      1.1  dholland 	 */
    800      1.1  dholland 	newnfs_realign(&nd->nd_mrep, M_WAITOK);
    801      1.1  dholland 	nd->nd_md = nd->nd_mrep;
    802      1.1  dholland 	nd->nd_dpos = NFSMTOD(nd->nd_md, caddr_t);
    803      1.1  dholland 	nd->nd_repstat = 0;
    804  1.1.1.2  pgoyette 	if (nd->nd_procnum != NFSPROC_NULL &&
    805  1.1.1.2  pgoyette 	    nd->nd_procnum != NFSV4PROC_CBNULL) {
    806      1.1  dholland 		/* If sep == NULL, set it to the default in nmp. */
    807      1.1  dholland 		if (sep == NULL && nmp != NULL)
    808      1.1  dholland 			sep = NFSMNT_MDSSESSION(nmp);
    809      1.1  dholland 		/*
    810      1.1  dholland 		 * and now the actual NFS xdr.
    811      1.1  dholland 		 */
    812      1.1  dholland 		NFSM_DISSECT(tl, u_int32_t *, NFSX_UNSIGNED);
    813      1.1  dholland 		nd->nd_repstat = fxdr_unsigned(u_int32_t, *tl);
    814      1.1  dholland 		if (nd->nd_repstat >= 10000)
    815      1.1  dholland 			NFSCL_DEBUG(1, "proc=%d reps=%d\n", (int)nd->nd_procnum,
    816      1.1  dholland 			    (int)nd->nd_repstat);
    817      1.1  dholland 
    818      1.1  dholland 		/*
    819      1.1  dholland 		 * Get rid of the tag, return count and SEQUENCE result for
    820      1.1  dholland 		 * NFSv4.
    821      1.1  dholland 		 */
    822      1.1  dholland 		if ((nd->nd_flag & ND_NFSV4) != 0) {
    823      1.1  dholland 			NFSM_DISSECT(tl, u_int32_t *, NFSX_UNSIGNED);
    824      1.1  dholland 			i = fxdr_unsigned(int, *tl);
    825      1.1  dholland 			error = nfsm_advance(nd, NFSM_RNDUP(i), -1);
    826      1.1  dholland 			if (error)
    827      1.1  dholland 				goto nfsmout;
    828      1.1  dholland 			NFSM_DISSECT(tl, u_int32_t *, 3 * NFSX_UNSIGNED);
    829      1.1  dholland 			opcnt = fxdr_unsigned(int, *tl++);
    830      1.1  dholland 			i = fxdr_unsigned(int, *tl++);
    831      1.1  dholland 			j = fxdr_unsigned(int, *tl);
    832      1.1  dholland 			if (j >= 10000)
    833      1.1  dholland 				NFSCL_DEBUG(1, "fop=%d fst=%d\n", i, j);
    834      1.1  dholland 			/*
    835      1.1  dholland 			 * If the first op is Sequence, free up the slot.
    836      1.1  dholland 			 */
    837  1.1.1.2  pgoyette 			if ((nmp != NULL && i == NFSV4OP_SEQUENCE && j != 0) ||
    838  1.1.1.2  pgoyette 			    (clp != NULL && i == NFSV4OP_CBSEQUENCE && j != 0))
    839      1.1  dholland 				NFSCL_DEBUG(1, "failed seq=%d\n", j);
    840  1.1.1.2  pgoyette 			if ((nmp != NULL && i == NFSV4OP_SEQUENCE && j == 0) ||
    841  1.1.1.2  pgoyette 			    (clp != NULL && i == NFSV4OP_CBSEQUENCE && j == 0)
    842  1.1.1.2  pgoyette 			    ) {
    843  1.1.1.2  pgoyette 				if (i == NFSV4OP_SEQUENCE)
    844  1.1.1.2  pgoyette 					NFSM_DISSECT(tl, uint32_t *,
    845  1.1.1.2  pgoyette 					    NFSX_V4SESSIONID +
    846  1.1.1.2  pgoyette 					    5 * NFSX_UNSIGNED);
    847  1.1.1.2  pgoyette 				else
    848  1.1.1.2  pgoyette 					NFSM_DISSECT(tl, uint32_t *,
    849  1.1.1.2  pgoyette 					    NFSX_V4SESSIONID +
    850  1.1.1.2  pgoyette 					    4 * NFSX_UNSIGNED);
    851      1.1  dholland 				mtx_lock(&sep->nfsess_mtx);
    852      1.1  dholland 				tl += NFSX_V4SESSIONID / NFSX_UNSIGNED;
    853      1.1  dholland 				retseq = fxdr_unsigned(uint32_t, *tl++);
    854      1.1  dholland 				slot = fxdr_unsigned(int, *tl++);
    855      1.1  dholland 				freeslot = slot;
    856      1.1  dholland 				if (retseq != sep->nfsess_slotseq[slot])
    857      1.1  dholland 					printf("retseq diff 0x%x\n", retseq);
    858      1.1  dholland 				retval = fxdr_unsigned(uint32_t, *++tl);
    859      1.1  dholland 				if ((retval + 1) < sep->nfsess_foreslots)
    860      1.1  dholland 					sep->nfsess_foreslots = (retval + 1);
    861      1.1  dholland 				else if ((retval + 1) > sep->nfsess_foreslots)
    862      1.1  dholland 					sep->nfsess_foreslots = (retval < 64) ?
    863      1.1  dholland 					    (retval + 1) : 64;
    864      1.1  dholland 				mtx_unlock(&sep->nfsess_mtx);
    865      1.1  dholland 
    866      1.1  dholland 				/* Grab the op and status for the next one. */
    867      1.1  dholland 				if (opcnt > 1) {
    868      1.1  dholland 					NFSM_DISSECT(tl, uint32_t *,
    869      1.1  dholland 					    2 * NFSX_UNSIGNED);
    870      1.1  dholland 					i = fxdr_unsigned(int, *tl++);
    871      1.1  dholland 					j = fxdr_unsigned(int, *tl);
    872      1.1  dholland 				}
    873      1.1  dholland 			}
    874      1.1  dholland 		}
    875      1.1  dholland 		if (nd->nd_repstat != 0) {
    876      1.1  dholland 			if (((nd->nd_repstat == NFSERR_DELAY ||
    877      1.1  dholland 			      nd->nd_repstat == NFSERR_GRACE) &&
    878      1.1  dholland 			     (nd->nd_flag & ND_NFSV4) &&
    879      1.1  dholland 			     nd->nd_procnum != NFSPROC_DELEGRETURN &&
    880      1.1  dholland 			     nd->nd_procnum != NFSPROC_SETATTR &&
    881      1.1  dholland 			     nd->nd_procnum != NFSPROC_READ &&
    882      1.1  dholland 			     nd->nd_procnum != NFSPROC_READDS &&
    883      1.1  dholland 			     nd->nd_procnum != NFSPROC_WRITE &&
    884      1.1  dholland 			     nd->nd_procnum != NFSPROC_WRITEDS &&
    885      1.1  dholland 			     nd->nd_procnum != NFSPROC_OPEN &&
    886      1.1  dholland 			     nd->nd_procnum != NFSPROC_CREATE &&
    887      1.1  dholland 			     nd->nd_procnum != NFSPROC_OPENCONFIRM &&
    888      1.1  dholland 			     nd->nd_procnum != NFSPROC_OPENDOWNGRADE &&
    889      1.1  dholland 			     nd->nd_procnum != NFSPROC_CLOSE &&
    890      1.1  dholland 			     nd->nd_procnum != NFSPROC_LOCK &&
    891      1.1  dholland 			     nd->nd_procnum != NFSPROC_LOCKU) ||
    892      1.1  dholland 			    (nd->nd_repstat == NFSERR_DELAY &&
    893      1.1  dholland 			     (nd->nd_flag & ND_NFSV4) == 0) ||
    894      1.1  dholland 			    nd->nd_repstat == NFSERR_RESOURCE) {
    895      1.1  dholland 				if (trylater_delay > NFS_TRYLATERDEL)
    896      1.1  dholland 					trylater_delay = NFS_TRYLATERDEL;
    897      1.1  dholland 				waituntil = NFSD_MONOSEC + trylater_delay;
    898      1.1  dholland 				while (NFSD_MONOSEC < waituntil)
    899      1.1  dholland 					(void) nfs_catnap(PZERO, 0, "nfstry");
    900      1.1  dholland 				trylater_delay *= 2;
    901      1.1  dholland 				if (slot != -1) {
    902      1.1  dholland 					mtx_lock(&sep->nfsess_mtx);
    903      1.1  dholland 					sep->nfsess_slotseq[slot]++;
    904      1.1  dholland 					*nd->nd_slotseq = txdr_unsigned(
    905      1.1  dholland 					    sep->nfsess_slotseq[slot]);
    906      1.1  dholland 					mtx_unlock(&sep->nfsess_mtx);
    907      1.1  dholland 				}
    908      1.1  dholland 				m_freem(nd->nd_mrep);
    909      1.1  dholland 				nd->nd_mrep = NULL;
    910      1.1  dholland 				goto tryagain;
    911      1.1  dholland 			}
    912      1.1  dholland 
    913      1.1  dholland 			/*
    914      1.1  dholland 			 * If the File Handle was stale, invalidate the
    915      1.1  dholland 			 * lookup cache, just in case.
    916      1.1  dholland 			 * (vp != NULL implies a client side call)
    917      1.1  dholland 			 */
    918      1.1  dholland 			if (nd->nd_repstat == ESTALE && vp != NULL) {
    919      1.1  dholland 				cache_purge(vp);
    920      1.1  dholland 				if (ncl_call_invalcaches != NULL)
    921      1.1  dholland 					(*ncl_call_invalcaches)(vp);
    922      1.1  dholland 			}
    923      1.1  dholland 		}
    924      1.1  dholland 		if ((nd->nd_flag & ND_NFSV4) != 0) {
    925      1.1  dholland 			/* Free the slot, as required. */
    926      1.1  dholland 			if (freeslot != -1)
    927      1.1  dholland 				nfsv4_freeslot(sep, freeslot);
    928      1.1  dholland 			/*
    929      1.1  dholland 			 * If this op is Putfh, throw its results away.
    930      1.1  dholland 			 */
    931      1.1  dholland 			if (j >= 10000)
    932      1.1  dholland 				NFSCL_DEBUG(1, "nop=%d nst=%d\n", i, j);
    933      1.1  dholland 			if (nmp != NULL && i == NFSV4OP_PUTFH && j == 0) {
    934      1.1  dholland 				NFSM_DISSECT(tl,u_int32_t *,2 * NFSX_UNSIGNED);
    935      1.1  dholland 				i = fxdr_unsigned(int, *tl++);
    936      1.1  dholland 				j = fxdr_unsigned(int, *tl);
    937      1.1  dholland 				if (j >= 10000)
    938      1.1  dholland 					NFSCL_DEBUG(1, "n2op=%d n2st=%d\n", i,
    939      1.1  dholland 					    j);
    940      1.1  dholland 				/*
    941      1.1  dholland 				 * All Compounds that do an Op that must
    942      1.1  dholland 				 * be in sequence consist of NFSV4OP_PUTFH
    943      1.1  dholland 				 * followed by one of these. As such, we
    944      1.1  dholland 				 * can determine if the seqid# should be
    945      1.1  dholland 				 * incremented, here.
    946      1.1  dholland 				 */
    947      1.1  dholland 				if ((i == NFSV4OP_OPEN ||
    948      1.1  dholland 				     i == NFSV4OP_OPENCONFIRM ||
    949      1.1  dholland 				     i == NFSV4OP_OPENDOWNGRADE ||
    950      1.1  dholland 				     i == NFSV4OP_CLOSE ||
    951      1.1  dholland 				     i == NFSV4OP_LOCK ||
    952      1.1  dholland 				     i == NFSV4OP_LOCKU) &&
    953      1.1  dholland 				    (j == 0 ||
    954      1.1  dholland 				     (j != NFSERR_STALECLIENTID &&
    955      1.1  dholland 				      j != NFSERR_STALESTATEID &&
    956      1.1  dholland 				      j != NFSERR_BADSTATEID &&
    957      1.1  dholland 				      j != NFSERR_BADSEQID &&
    958      1.1  dholland 				      j != NFSERR_BADXDR &&
    959      1.1  dholland 				      j != NFSERR_RESOURCE &&
    960      1.1  dholland 				      j != NFSERR_NOFILEHANDLE)))
    961      1.1  dholland 					nd->nd_flag |= ND_INCRSEQID;
    962      1.1  dholland 			}
    963      1.1  dholland 			/*
    964      1.1  dholland 			 * If this op's status is non-zero, mark
    965      1.1  dholland 			 * that there is no more data to process.
    966      1.1  dholland 			 */
    967      1.1  dholland 			if (j)
    968      1.1  dholland 				nd->nd_flag |= ND_NOMOREDATA;
    969      1.1  dholland 
    970      1.1  dholland 			/*
    971      1.1  dholland 			 * If R_DONTRECOVER is set, replace the stale error
    972      1.1  dholland 			 * reply, so that recovery isn't initiated.
    973      1.1  dholland 			 */
    974      1.1  dholland 			if ((nd->nd_repstat == NFSERR_STALECLIENTID ||
    975      1.1  dholland 			     nd->nd_repstat == NFSERR_BADSESSION ||
    976      1.1  dholland 			     nd->nd_repstat == NFSERR_STALESTATEID) &&
    977      1.1  dholland 			    rep != NULL && (rep->r_flags & R_DONTRECOVER))
    978      1.1  dholland 				nd->nd_repstat = NFSERR_STALEDONTRECOVER;
    979      1.1  dholland 		}
    980      1.1  dholland 	}
    981      1.1  dholland 
    982      1.1  dholland #ifdef KDTRACE_HOOKS
    983      1.1  dholland 	if (nmp != NULL && dtrace_nfscl_nfs234_done_probe != NULL) {
    984      1.1  dholland 		uint32_t probe_id;
    985      1.1  dholland 		int probe_procnum;
    986      1.1  dholland 
    987      1.1  dholland 		if (nd->nd_flag & ND_NFSV4) {
    988      1.1  dholland 			probe_id = nfscl_nfs4_done_probes[nd->nd_procnum];
    989      1.1  dholland 			probe_procnum = nd->nd_procnum;
    990      1.1  dholland 		} else if (nd->nd_flag & ND_NFSV3) {
    991      1.1  dholland 			probe_id = nfscl_nfs3_done_probes[procnum];
    992      1.1  dholland 			probe_procnum = procnum;
    993      1.1  dholland 		} else {
    994      1.1  dholland 			probe_id = nfscl_nfs2_done_probes[nd->nd_procnum];
    995      1.1  dholland 			probe_procnum = procnum;
    996      1.1  dholland 		}
    997      1.1  dholland 		if (probe_id != 0)
    998      1.1  dholland 			(dtrace_nfscl_nfs234_done_probe)(probe_id, vp,
    999      1.1  dholland 			    nd->nd_mreq, cred, probe_procnum, 0);
   1000      1.1  dholland 	}
   1001      1.1  dholland #endif
   1002      1.1  dholland 
   1003      1.1  dholland 	m_freem(nd->nd_mreq);
   1004      1.1  dholland 	if (usegssname == 0)
   1005      1.1  dholland 		AUTH_DESTROY(auth);
   1006      1.1  dholland 	if (rep != NULL)
   1007      1.1  dholland 		FREE((caddr_t)rep, M_NFSDREQ);
   1008      1.1  dholland 	if (set_sigset)
   1009      1.1  dholland 		newnfs_restore_sigmask(td, &oldset);
   1010      1.1  dholland 	return (0);
   1011      1.1  dholland nfsmout:
   1012      1.1  dholland 	mbuf_freem(nd->nd_mrep);
   1013      1.1  dholland 	mbuf_freem(nd->nd_mreq);
   1014      1.1  dholland 	if (usegssname == 0)
   1015      1.1  dholland 		AUTH_DESTROY(auth);
   1016      1.1  dholland 	if (rep != NULL)
   1017      1.1  dholland 		FREE((caddr_t)rep, M_NFSDREQ);
   1018      1.1  dholland 	if (set_sigset)
   1019      1.1  dholland 		newnfs_restore_sigmask(td, &oldset);
   1020      1.1  dholland 	return (error);
   1021      1.1  dholland }
   1022      1.1  dholland 
   1023      1.1  dholland /*
   1024      1.1  dholland  * Mark all of an nfs mount's outstanding requests with R_SOFTTERM and
   1025      1.1  dholland  * wait for all requests to complete. This is used by forced unmounts
   1026      1.1  dholland  * to terminate any outstanding RPCs.
   1027      1.1  dholland  */
   1028      1.1  dholland int
   1029      1.1  dholland newnfs_nmcancelreqs(struct nfsmount *nmp)
   1030      1.1  dholland {
   1031      1.1  dholland 
   1032      1.1  dholland 	if (nmp->nm_sockreq.nr_client != NULL)
   1033      1.1  dholland 		CLNT_CLOSE(nmp->nm_sockreq.nr_client);
   1034      1.1  dholland 	return (0);
   1035      1.1  dholland }
   1036      1.1  dholland 
   1037      1.1  dholland /*
   1038      1.1  dholland  * Any signal that can interrupt an NFS operation in an intr mount
   1039      1.1  dholland  * should be added to this set. SIGSTOP and SIGKILL cannot be masked.
   1040      1.1  dholland  */
   1041      1.1  dholland int newnfs_sig_set[] = {
   1042      1.1  dholland 	SIGINT,
   1043      1.1  dholland 	SIGTERM,
   1044      1.1  dholland 	SIGHUP,
   1045      1.1  dholland 	SIGKILL,
   1046      1.1  dholland 	SIGQUIT
   1047      1.1  dholland };
   1048      1.1  dholland 
   1049      1.1  dholland /*
   1050      1.1  dholland  * Check to see if one of the signals in our subset is pending on
   1051      1.1  dholland  * the process (in an intr mount).
   1052      1.1  dholland  */
   1053      1.1  dholland static int
   1054      1.1  dholland nfs_sig_pending(sigset_t set)
   1055      1.1  dholland {
   1056      1.1  dholland 	int i;
   1057      1.1  dholland 
   1058  1.1.1.2  pgoyette 	for (i = 0 ; i < nitems(newnfs_sig_set); i++)
   1059      1.1  dholland 		if (SIGISMEMBER(set, newnfs_sig_set[i]))
   1060      1.1  dholland 			return (1);
   1061      1.1  dholland 	return (0);
   1062      1.1  dholland }
   1063      1.1  dholland 
   1064      1.1  dholland /*
   1065      1.1  dholland  * The set/restore sigmask functions are used to (temporarily) overwrite
   1066      1.1  dholland  * the thread td_sigmask during an RPC call (for example). These are also
   1067      1.1  dholland  * used in other places in the NFS client that might tsleep().
   1068      1.1  dholland  */
   1069      1.1  dholland void
   1070      1.1  dholland newnfs_set_sigmask(struct thread *td, sigset_t *oldset)
   1071      1.1  dholland {
   1072      1.1  dholland 	sigset_t newset;
   1073      1.1  dholland 	int i;
   1074      1.1  dholland 	struct proc *p;
   1075      1.1  dholland 
   1076      1.1  dholland 	SIGFILLSET(newset);
   1077      1.1  dholland 	if (td == NULL)
   1078      1.1  dholland 		td = curthread; /* XXX */
   1079      1.1  dholland 	p = td->td_proc;
   1080      1.1  dholland 	/* Remove the NFS set of signals from newset */
   1081      1.1  dholland 	PROC_LOCK(p);
   1082      1.1  dholland 	mtx_lock(&p->p_sigacts->ps_mtx);
   1083  1.1.1.2  pgoyette 	for (i = 0 ; i < nitems(newnfs_sig_set); i++) {
   1084      1.1  dholland 		/*
   1085      1.1  dholland 		 * But make sure we leave the ones already masked
   1086      1.1  dholland 		 * by the process, ie. remove the signal from the
   1087      1.1  dholland 		 * temporary signalmask only if it wasn't already
   1088      1.1  dholland 		 * in p_sigmask.
   1089      1.1  dholland 		 */
   1090      1.1  dholland 		if (!SIGISMEMBER(td->td_sigmask, newnfs_sig_set[i]) &&
   1091      1.1  dholland 		    !SIGISMEMBER(p->p_sigacts->ps_sigignore, newnfs_sig_set[i]))
   1092      1.1  dholland 			SIGDELSET(newset, newnfs_sig_set[i]);
   1093      1.1  dholland 	}
   1094      1.1  dholland 	mtx_unlock(&p->p_sigacts->ps_mtx);
   1095      1.1  dholland 	kern_sigprocmask(td, SIG_SETMASK, &newset, oldset,
   1096      1.1  dholland 	    SIGPROCMASK_PROC_LOCKED);
   1097      1.1  dholland 	PROC_UNLOCK(p);
   1098      1.1  dholland }
   1099      1.1  dholland 
   1100      1.1  dholland void
   1101      1.1  dholland newnfs_restore_sigmask(struct thread *td, sigset_t *set)
   1102      1.1  dholland {
   1103      1.1  dholland 	if (td == NULL)
   1104      1.1  dholland 		td = curthread; /* XXX */
   1105      1.1  dholland 	kern_sigprocmask(td, SIG_SETMASK, set, NULL, 0);
   1106      1.1  dholland }
   1107      1.1  dholland 
   1108      1.1  dholland /*
   1109      1.1  dholland  * NFS wrapper to msleep(), that shoves a new p_sigmask and restores the
   1110      1.1  dholland  * old one after msleep() returns.
   1111      1.1  dholland  */
   1112      1.1  dholland int
   1113      1.1  dholland newnfs_msleep(struct thread *td, void *ident, struct mtx *mtx, int priority, char *wmesg, int timo)
   1114      1.1  dholland {
   1115      1.1  dholland 	sigset_t oldset;
   1116      1.1  dholland 	int error;
   1117      1.1  dholland 	struct proc *p;
   1118      1.1  dholland 
   1119      1.1  dholland 	if ((priority & PCATCH) == 0)
   1120      1.1  dholland 		return msleep(ident, mtx, priority, wmesg, timo);
   1121      1.1  dholland 	if (td == NULL)
   1122      1.1  dholland 		td = curthread; /* XXX */
   1123      1.1  dholland 	newnfs_set_sigmask(td, &oldset);
   1124      1.1  dholland 	error = msleep(ident, mtx, priority, wmesg, timo);
   1125      1.1  dholland 	newnfs_restore_sigmask(td, &oldset);
   1126      1.1  dholland 	p = td->td_proc;
   1127      1.1  dholland 	return (error);
   1128      1.1  dholland }
   1129      1.1  dholland 
   1130      1.1  dholland /*
   1131      1.1  dholland  * Test for a termination condition pending on the process.
   1132      1.1  dholland  * This is used for NFSMNT_INT mounts.
   1133      1.1  dholland  */
   1134      1.1  dholland int
   1135      1.1  dholland newnfs_sigintr(struct nfsmount *nmp, struct thread *td)
   1136      1.1  dholland {
   1137      1.1  dholland 	struct proc *p;
   1138      1.1  dholland 	sigset_t tmpset;
   1139      1.1  dholland 
   1140      1.1  dholland 	/* Terminate all requests while attempting a forced unmount. */
   1141      1.1  dholland 	if (nmp->nm_mountp->mnt_kern_flag & MNTK_UNMOUNTF)
   1142      1.1  dholland 		return (EIO);
   1143      1.1  dholland 	if (!(nmp->nm_flag & NFSMNT_INT))
   1144      1.1  dholland 		return (0);
   1145      1.1  dholland 	if (td == NULL)
   1146      1.1  dholland 		return (0);
   1147      1.1  dholland 	p = td->td_proc;
   1148      1.1  dholland 	PROC_LOCK(p);
   1149      1.1  dholland 	tmpset = p->p_siglist;
   1150      1.1  dholland 	SIGSETOR(tmpset, td->td_siglist);
   1151      1.1  dholland 	SIGSETNAND(tmpset, td->td_sigmask);
   1152      1.1  dholland 	mtx_lock(&p->p_sigacts->ps_mtx);
   1153      1.1  dholland 	SIGSETNAND(tmpset, p->p_sigacts->ps_sigignore);
   1154      1.1  dholland 	mtx_unlock(&p->p_sigacts->ps_mtx);
   1155      1.1  dholland 	if ((SIGNOTEMPTY(p->p_siglist) || SIGNOTEMPTY(td->td_siglist))
   1156      1.1  dholland 	    && nfs_sig_pending(tmpset)) {
   1157      1.1  dholland 		PROC_UNLOCK(p);
   1158      1.1  dholland 		return (EINTR);
   1159      1.1  dholland 	}
   1160      1.1  dholland 	PROC_UNLOCK(p);
   1161      1.1  dholland 	return (0);
   1162      1.1  dholland }
   1163      1.1  dholland 
   1164      1.1  dholland static int
   1165      1.1  dholland nfs_msg(struct thread *td, const char *server, const char *msg, int error)
   1166      1.1  dholland {
   1167      1.1  dholland 	struct proc *p;
   1168      1.1  dholland 
   1169      1.1  dholland 	p = td ? td->td_proc : NULL;
   1170      1.1  dholland 	if (error) {
   1171  1.1.1.2  pgoyette 		tprintf(p, LOG_INFO, "nfs server %s: %s, error %d\n",
   1172      1.1  dholland 		    server, msg, error);
   1173      1.1  dholland 	} else {
   1174  1.1.1.2  pgoyette 		tprintf(p, LOG_INFO, "nfs server %s: %s\n", server, msg);
   1175      1.1  dholland 	}
   1176      1.1  dholland 	return (0);
   1177      1.1  dholland }
   1178      1.1  dholland 
   1179      1.1  dholland static void
   1180      1.1  dholland nfs_down(struct nfsmount *nmp, struct thread *td, const char *msg,
   1181      1.1  dholland     int error, int flags)
   1182      1.1  dholland {
   1183      1.1  dholland 	if (nmp == NULL)
   1184      1.1  dholland 		return;
   1185      1.1  dholland 	mtx_lock(&nmp->nm_mtx);
   1186      1.1  dholland 	if ((flags & NFSSTA_TIMEO) && !(nmp->nm_state & NFSSTA_TIMEO)) {
   1187      1.1  dholland 		nmp->nm_state |= NFSSTA_TIMEO;
   1188      1.1  dholland 		mtx_unlock(&nmp->nm_mtx);
   1189      1.1  dholland 		vfs_event_signal(&nmp->nm_mountp->mnt_stat.f_fsid,
   1190      1.1  dholland 		    VQ_NOTRESP, 0);
   1191      1.1  dholland 	} else
   1192      1.1  dholland 		mtx_unlock(&nmp->nm_mtx);
   1193      1.1  dholland 	mtx_lock(&nmp->nm_mtx);
   1194      1.1  dholland 	if ((flags & NFSSTA_LOCKTIMEO) && !(nmp->nm_state & NFSSTA_LOCKTIMEO)) {
   1195      1.1  dholland 		nmp->nm_state |= NFSSTA_LOCKTIMEO;
   1196      1.1  dholland 		mtx_unlock(&nmp->nm_mtx);
   1197      1.1  dholland 		vfs_event_signal(&nmp->nm_mountp->mnt_stat.f_fsid,
   1198      1.1  dholland 		    VQ_NOTRESPLOCK, 0);
   1199      1.1  dholland 	} else
   1200      1.1  dholland 		mtx_unlock(&nmp->nm_mtx);
   1201      1.1  dholland 	nfs_msg(td, nmp->nm_mountp->mnt_stat.f_mntfromname, msg, error);
   1202      1.1  dholland }
   1203      1.1  dholland 
   1204      1.1  dholland static void
   1205      1.1  dholland nfs_up(struct nfsmount *nmp, struct thread *td, const char *msg,
   1206      1.1  dholland     int flags, int tprintfmsg)
   1207      1.1  dholland {
   1208      1.1  dholland 	if (nmp == NULL)
   1209      1.1  dholland 		return;
   1210      1.1  dholland 	if (tprintfmsg) {
   1211      1.1  dholland 		nfs_msg(td, nmp->nm_mountp->mnt_stat.f_mntfromname, msg, 0);
   1212      1.1  dholland 	}
   1213      1.1  dholland 
   1214      1.1  dholland 	mtx_lock(&nmp->nm_mtx);
   1215      1.1  dholland 	if ((flags & NFSSTA_TIMEO) && (nmp->nm_state & NFSSTA_TIMEO)) {
   1216      1.1  dholland 		nmp->nm_state &= ~NFSSTA_TIMEO;
   1217      1.1  dholland 		mtx_unlock(&nmp->nm_mtx);
   1218      1.1  dholland 		vfs_event_signal(&nmp->nm_mountp->mnt_stat.f_fsid,
   1219      1.1  dholland 		    VQ_NOTRESP, 1);
   1220      1.1  dholland 	} else
   1221      1.1  dholland 		mtx_unlock(&nmp->nm_mtx);
   1222      1.1  dholland 
   1223      1.1  dholland 	mtx_lock(&nmp->nm_mtx);
   1224      1.1  dholland 	if ((flags & NFSSTA_LOCKTIMEO) && (nmp->nm_state & NFSSTA_LOCKTIMEO)) {
   1225      1.1  dholland 		nmp->nm_state &= ~NFSSTA_LOCKTIMEO;
   1226      1.1  dholland 		mtx_unlock(&nmp->nm_mtx);
   1227      1.1  dholland 		vfs_event_signal(&nmp->nm_mountp->mnt_stat.f_fsid,
   1228      1.1  dholland 		    VQ_NOTRESPLOCK, 1);
   1229      1.1  dholland 	} else
   1230      1.1  dholland 		mtx_unlock(&nmp->nm_mtx);
   1231      1.1  dholland }
   1232      1.1  dholland 
   1233