17c478bd9Sstevel@tonic-gate /*
27c478bd9Sstevel@tonic-gate  * CDDL HEADER START
37c478bd9Sstevel@tonic-gate  *
47c478bd9Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
5da1b9cd5Sjwahlig  * Common Development and Distribution License (the "License").
6da1b9cd5Sjwahlig  * You may not use this file except in compliance with the License.
77c478bd9Sstevel@tonic-gate  *
87c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
97c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
107c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
117c478bd9Sstevel@tonic-gate  * and limitations under the License.
127c478bd9Sstevel@tonic-gate  *
137c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
147c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
157c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
167c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
177c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
187c478bd9Sstevel@tonic-gate  *
197c478bd9Sstevel@tonic-gate  * CDDL HEADER END
207c478bd9Sstevel@tonic-gate  */
2159927d31SYuri Pankov 
227c478bd9Sstevel@tonic-gate /*
23878aca8dSDai Ngo  * Copyright 2009 Sun Microsystems, Inc.  All rights reserved.
247c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
25*0dfe541eSEvan Layton  */
26*0dfe541eSEvan Layton 
27*0dfe541eSEvan Layton /*
28*0dfe541eSEvan Layton  * Copyright 2018 Nexenta Systems, Inc.
297c478bd9Sstevel@tonic-gate  */
307c478bd9Sstevel@tonic-gate 
317c478bd9Sstevel@tonic-gate #include <sys/systm.h>
327c478bd9Sstevel@tonic-gate #include <rpc/auth.h>
337c478bd9Sstevel@tonic-gate #include <rpc/clnt.h>
347c478bd9Sstevel@tonic-gate #include <nfs/nfs4_kprot.h>
357c478bd9Sstevel@tonic-gate #include <nfs/nfs4.h>
367c478bd9Sstevel@tonic-gate #include <nfs/lm.h>
377c478bd9Sstevel@tonic-gate #include <sys/cmn_err.h>
387c478bd9Sstevel@tonic-gate #include <sys/disp.h>
39f3b585ceSsamf #include <sys/sdt.h>
407c478bd9Sstevel@tonic-gate 
417c478bd9Sstevel@tonic-gate #include <sys/pathname.h>
427c478bd9Sstevel@tonic-gate 
437c478bd9Sstevel@tonic-gate #include <sys/strsubr.h>
447c478bd9Sstevel@tonic-gate #include <sys/ddi.h>
457c478bd9Sstevel@tonic-gate 
467c478bd9Sstevel@tonic-gate #include <sys/vnode.h>
47ed57cab9Srg #include <sys/sdt.h>
487c478bd9Sstevel@tonic-gate #include <inet/common.h>
497c478bd9Sstevel@tonic-gate #include <inet/ip.h>
507c478bd9Sstevel@tonic-gate #include <inet/ip6.h>
517c478bd9Sstevel@tonic-gate 
527c478bd9Sstevel@tonic-gate #define	MAX_READ_DELEGATIONS 5
537c478bd9Sstevel@tonic-gate 
547c478bd9Sstevel@tonic-gate static int rfs4_deleg_wlp = 5;
557c478bd9Sstevel@tonic-gate static int rfs4_deleg_disabled;
56878aca8dSDai Ngo static int rfs4_max_setup_cb_tries = 5;
577c478bd9Sstevel@tonic-gate 
587c478bd9Sstevel@tonic-gate #ifdef DEBUG
597c478bd9Sstevel@tonic-gate 
607c478bd9Sstevel@tonic-gate static int rfs4_test_cbgetattr_fail = 0;
617c478bd9Sstevel@tonic-gate int rfs4_cb_null;
627c478bd9Sstevel@tonic-gate int rfs4_cb_debug;
637c478bd9Sstevel@tonic-gate int rfs4_deleg_debug;
647c478bd9Sstevel@tonic-gate 
657c478bd9Sstevel@tonic-gate #endif
667c478bd9Sstevel@tonic-gate 
677c478bd9Sstevel@tonic-gate static void rfs4_recall_file(rfs4_file_t *,
68d216dff5SRobert Mastors     void (*recall)(rfs4_deleg_state_t *, bool_t),
69d216dff5SRobert Mastors     bool_t, rfs4_client_t *);
707c478bd9Sstevel@tonic-gate static	void		rfs4_revoke_file(rfs4_file_t *);
717c478bd9Sstevel@tonic-gate static	void		rfs4_cb_chflush(rfs4_cbinfo_t *);
727c478bd9Sstevel@tonic-gate static	CLIENT		*rfs4_cb_getch(rfs4_cbinfo_t *);
737c478bd9Sstevel@tonic-gate static	void		rfs4_cb_freech(rfs4_cbinfo_t *, CLIENT *, bool_t);
747c478bd9Sstevel@tonic-gate static rfs4_deleg_state_t *rfs4_deleg_state(rfs4_state_t *,
75d216dff5SRobert Mastors     open_delegation_type4, int *);
767c478bd9Sstevel@tonic-gate 
777c478bd9Sstevel@tonic-gate /*
787c478bd9Sstevel@tonic-gate  * Convert a universal address to an transport specific
797c478bd9Sstevel@tonic-gate  * address using inet_pton.
807c478bd9Sstevel@tonic-gate  */
817c478bd9Sstevel@tonic-gate static int
uaddr2sockaddr(int af,char * ua,void * ap,in_port_t * pp)827c478bd9Sstevel@tonic-gate uaddr2sockaddr(int af, char *ua, void *ap, in_port_t *pp)
837c478bd9Sstevel@tonic-gate {
847c478bd9Sstevel@tonic-gate 	int dots = 0, i, j, len, k;
857c478bd9Sstevel@tonic-gate 	unsigned char c;
867c478bd9Sstevel@tonic-gate 	in_port_t port = 0;
877c478bd9Sstevel@tonic-gate 
887c478bd9Sstevel@tonic-gate 	len = strlen(ua);
897c478bd9Sstevel@tonic-gate 
907c478bd9Sstevel@tonic-gate 	for (i = len-1; i >= 0; i--) {
917c478bd9Sstevel@tonic-gate 
927c478bd9Sstevel@tonic-gate 		if (ua[i] == '.')
937c478bd9Sstevel@tonic-gate 			dots++;
947c478bd9Sstevel@tonic-gate 
957c478bd9Sstevel@tonic-gate 		if (dots == 2) {
967c478bd9Sstevel@tonic-gate 
977c478bd9Sstevel@tonic-gate 			ua[i] = '\0';
987c478bd9Sstevel@tonic-gate 			/*
997c478bd9Sstevel@tonic-gate 			 * We use k to remember were to stick '.' back, since
1007c478bd9Sstevel@tonic-gate 			 * ua was kmem_allocateded from the pool len+1.
1017c478bd9Sstevel@tonic-gate 			 */
1027c478bd9Sstevel@tonic-gate 			k = i;
1037c478bd9Sstevel@tonic-gate 			if (inet_pton(af, ua, ap) == 1) {
1047c478bd9Sstevel@tonic-gate 
1057c478bd9Sstevel@tonic-gate 				c = 0;
1067c478bd9Sstevel@tonic-gate 
1077c478bd9Sstevel@tonic-gate 				for (j = i+1; j < len; j++) {
1087c478bd9Sstevel@tonic-gate 					if (ua[j] == '.') {
1097c478bd9Sstevel@tonic-gate 						port = c << 8;
1107c478bd9Sstevel@tonic-gate 						c = 0;
1117c478bd9Sstevel@tonic-gate 					} else if (ua[j] >= '0' &&
1127c478bd9Sstevel@tonic-gate 					    ua[j] <= '9') {
1137c478bd9Sstevel@tonic-gate 						c *= 10;
1147c478bd9Sstevel@tonic-gate 						c += ua[j] - '0';
1157c478bd9Sstevel@tonic-gate 					} else {
1167c478bd9Sstevel@tonic-gate 						ua[k] = '.';
1177c478bd9Sstevel@tonic-gate 						return (EINVAL);
1187c478bd9Sstevel@tonic-gate 					}
1197c478bd9Sstevel@tonic-gate 				}
1207c478bd9Sstevel@tonic-gate 				port += c;
1217c478bd9Sstevel@tonic-gate 
12259927d31SYuri Pankov 				*pp = htons(port);
1237c478bd9Sstevel@tonic-gate 
1247c478bd9Sstevel@tonic-gate 				ua[k] = '.';
1257c478bd9Sstevel@tonic-gate 				return (0);
1267c478bd9Sstevel@tonic-gate 			} else {
1277c478bd9Sstevel@tonic-gate 				ua[k] = '.';
1287c478bd9Sstevel@tonic-gate 				return (EINVAL);
1297c478bd9Sstevel@tonic-gate 			}
1307c478bd9Sstevel@tonic-gate 		}
1317c478bd9Sstevel@tonic-gate 	}
1327c478bd9Sstevel@tonic-gate 
1337c478bd9Sstevel@tonic-gate 	return (EINVAL);
1347c478bd9Sstevel@tonic-gate }
1357c478bd9Sstevel@tonic-gate 
1367c478bd9Sstevel@tonic-gate /*
1377c478bd9Sstevel@tonic-gate  * Update the delegation policy with the
1387c478bd9Sstevel@tonic-gate  * value of "new_policy"
1397c478bd9Sstevel@tonic-gate  */
1407c478bd9Sstevel@tonic-gate void
rfs4_set_deleg_policy(nfs4_srv_t * nsrv4,srv_deleg_policy_t new_policy)141*0dfe541eSEvan Layton rfs4_set_deleg_policy(nfs4_srv_t *nsrv4, srv_deleg_policy_t new_policy)
1427c478bd9Sstevel@tonic-gate {
143*0dfe541eSEvan Layton 	rw_enter(&nsrv4->deleg_policy_lock, RW_WRITER);
144*0dfe541eSEvan Layton 	nsrv4->nfs4_deleg_policy = new_policy;
145*0dfe541eSEvan Layton 	rw_exit(&nsrv4->deleg_policy_lock);
1467c478bd9Sstevel@tonic-gate }
1477c478bd9Sstevel@tonic-gate 
1487c478bd9Sstevel@tonic-gate void
rfs4_hold_deleg_policy(nfs4_srv_t * nsrv4)149*0dfe541eSEvan Layton rfs4_hold_deleg_policy(nfs4_srv_t *nsrv4)
1507c478bd9Sstevel@tonic-gate {
151*0dfe541eSEvan Layton 	rw_enter(&nsrv4->deleg_policy_lock, RW_READER);
1527c478bd9Sstevel@tonic-gate }
1537c478bd9Sstevel@tonic-gate 
1547c478bd9Sstevel@tonic-gate void
rfs4_rele_deleg_policy(nfs4_srv_t * nsrv4)155*0dfe541eSEvan Layton rfs4_rele_deleg_policy(nfs4_srv_t *nsrv4)
1567c478bd9Sstevel@tonic-gate {
157*0dfe541eSEvan Layton 	rw_exit(&nsrv4->deleg_policy_lock);
158*0dfe541eSEvan Layton }
159*0dfe541eSEvan Layton 
160*0dfe541eSEvan Layton srv_deleg_policy_t
nfs4_get_deleg_policy()161*0dfe541eSEvan Layton nfs4_get_deleg_policy()
162*0dfe541eSEvan Layton {
163*0dfe541eSEvan Layton 	nfs4_srv_t *nsrv4 = nfs4_get_srv();
164*0dfe541eSEvan Layton 	return (nsrv4->nfs4_deleg_policy);
1657c478bd9Sstevel@tonic-gate }
1667c478bd9Sstevel@tonic-gate 
1677c478bd9Sstevel@tonic-gate 
1687c478bd9Sstevel@tonic-gate /*
1697c478bd9Sstevel@tonic-gate  * This free function is to be used when the client struct is being
1707c478bd9Sstevel@tonic-gate  * released and nothing at all is needed of the callback info any
1717c478bd9Sstevel@tonic-gate  * longer.
1727c478bd9Sstevel@tonic-gate  */
1737c478bd9Sstevel@tonic-gate void
rfs4_cbinfo_free(rfs4_cbinfo_t * cbp)1747c478bd9Sstevel@tonic-gate rfs4_cbinfo_free(rfs4_cbinfo_t *cbp)
1757c478bd9Sstevel@tonic-gate {
1767c478bd9Sstevel@tonic-gate 	char *addr = cbp->cb_callback.cb_location.r_addr;
1777c478bd9Sstevel@tonic-gate 	char *netid = cbp->cb_callback.cb_location.r_netid;
1787c478bd9Sstevel@tonic-gate 
1797c478bd9Sstevel@tonic-gate 	/* Free old address if any */
1807c478bd9Sstevel@tonic-gate 
1817c478bd9Sstevel@tonic-gate 	if (addr)
1827c478bd9Sstevel@tonic-gate 		kmem_free(addr, strlen(addr) + 1);
1837c478bd9Sstevel@tonic-gate 	if (netid)
1847c478bd9Sstevel@tonic-gate 		kmem_free(netid, strlen(netid) + 1);
1857c478bd9Sstevel@tonic-gate 
1867c478bd9Sstevel@tonic-gate 	addr = cbp->cb_newer.cb_callback.cb_location.r_addr;
1877c478bd9Sstevel@tonic-gate 	netid = cbp->cb_newer.cb_callback.cb_location.r_netid;
1887c478bd9Sstevel@tonic-gate 
1897c478bd9Sstevel@tonic-gate 	if (addr)
1907c478bd9Sstevel@tonic-gate 		kmem_free(addr, strlen(addr) + 1);
1917c478bd9Sstevel@tonic-gate 	if (netid)
1927c478bd9Sstevel@tonic-gate 		kmem_free(netid, strlen(netid) + 1);
1937c478bd9Sstevel@tonic-gate 
1947c478bd9Sstevel@tonic-gate 	if (cbp->cb_chc_free) {
1957c478bd9Sstevel@tonic-gate 		rfs4_cb_chflush(cbp);
1967c478bd9Sstevel@tonic-gate 	}
1977c478bd9Sstevel@tonic-gate }
1987c478bd9Sstevel@tonic-gate 
1997c478bd9Sstevel@tonic-gate /*
2007c478bd9Sstevel@tonic-gate  * The server uses this to check the callback path supplied by the
2017c478bd9Sstevel@tonic-gate  * client.  The callback connection is marked "in progress" while this
2027c478bd9Sstevel@tonic-gate  * work is going on and then eventually marked either OK or FAILED.
2037c478bd9Sstevel@tonic-gate  * This work can be done as part of a separate thread and at the end
2047c478bd9Sstevel@tonic-gate  * of this the thread will exit or it may be done such that the caller
2057c478bd9Sstevel@tonic-gate  * will continue with other work.
2067c478bd9Sstevel@tonic-gate  */
2077c478bd9Sstevel@tonic-gate static void
rfs4_do_cb_null(rfs4_client_t * cp)2087c478bd9Sstevel@tonic-gate rfs4_do_cb_null(rfs4_client_t *cp)
2097c478bd9Sstevel@tonic-gate {
2107c478bd9Sstevel@tonic-gate 	struct timeval tv;
2117c478bd9Sstevel@tonic-gate 	CLIENT *ch;
2127c478bd9Sstevel@tonic-gate 	rfs4_cbstate_t newstate;
213d216dff5SRobert Mastors 	rfs4_cbinfo_t *cbp = &cp->rc_cbinfo;
2147c478bd9Sstevel@tonic-gate 
2157c478bd9Sstevel@tonic-gate 	mutex_enter(cbp->cb_lock);
2167c478bd9Sstevel@tonic-gate 	/* If another thread is doing CB_NULL RPC then return */
2177c478bd9Sstevel@tonic-gate 	if (cbp->cb_nullcaller == TRUE) {
2187c478bd9Sstevel@tonic-gate 		mutex_exit(cbp->cb_lock);
2197c478bd9Sstevel@tonic-gate 		rfs4_client_rele(cp);
220*0dfe541eSEvan Layton 		zthread_exit();
2217c478bd9Sstevel@tonic-gate 	}
2227c478bd9Sstevel@tonic-gate 
2237c478bd9Sstevel@tonic-gate 	/* Mark the cbinfo as having a thread in the NULL callback */
2247c478bd9Sstevel@tonic-gate 	cbp->cb_nullcaller = TRUE;
2257c478bd9Sstevel@tonic-gate 
2267c478bd9Sstevel@tonic-gate 	/*
2277c478bd9Sstevel@tonic-gate 	 * Are there other threads still using the cbinfo client
2287c478bd9Sstevel@tonic-gate 	 * handles?  If so, this thread must wait before going and
2297c478bd9Sstevel@tonic-gate 	 * mucking aroiund with the callback information
2307c478bd9Sstevel@tonic-gate 	 */
231a31a5cfdSsamf 	while (cbp->cb_refcnt != 0)
2327c478bd9Sstevel@tonic-gate 		cv_wait(cbp->cb_cv_nullcaller, cbp->cb_lock);
2337c478bd9Sstevel@tonic-gate 
2347c478bd9Sstevel@tonic-gate 	/*
2357c478bd9Sstevel@tonic-gate 	 * This thread itself may find that new callback info has
2367c478bd9Sstevel@tonic-gate 	 * arrived and is set up to handle this case and redrive the
2377c478bd9Sstevel@tonic-gate 	 * call to the client's callback server.
2387c478bd9Sstevel@tonic-gate 	 */
2397c478bd9Sstevel@tonic-gate retry:
2407c478bd9Sstevel@tonic-gate 	if (cbp->cb_newer.cb_new == TRUE &&
2417c478bd9Sstevel@tonic-gate 	    cbp->cb_newer.cb_confirmed == TRUE) {
2427c478bd9Sstevel@tonic-gate 		char *addr = cbp->cb_callback.cb_location.r_addr;
2437c478bd9Sstevel@tonic-gate 		char *netid = cbp->cb_callback.cb_location.r_netid;
2447c478bd9Sstevel@tonic-gate 
2457c478bd9Sstevel@tonic-gate 		/*
2467c478bd9Sstevel@tonic-gate 		 * Free the old stuff if it exists; may be the first
2477c478bd9Sstevel@tonic-gate 		 * time through this path
2487c478bd9Sstevel@tonic-gate 		 */
2497c478bd9Sstevel@tonic-gate 		if (addr)
2507c478bd9Sstevel@tonic-gate 			kmem_free(addr, strlen(addr) + 1);
2517c478bd9Sstevel@tonic-gate 		if (netid)
2527c478bd9Sstevel@tonic-gate 			kmem_free(netid, strlen(netid) + 1);
2537c478bd9Sstevel@tonic-gate 
2547c478bd9Sstevel@tonic-gate 		/* Move over the addr/netid */
2557c478bd9Sstevel@tonic-gate 		cbp->cb_callback.cb_location.r_addr =
256ed57cab9Srg 		    cbp->cb_newer.cb_callback.cb_location.r_addr;
2577c478bd9Sstevel@tonic-gate 		cbp->cb_newer.cb_callback.cb_location.r_addr = NULL;
2587c478bd9Sstevel@tonic-gate 		cbp->cb_callback.cb_location.r_netid =
259ed57cab9Srg 		    cbp->cb_newer.cb_callback.cb_location.r_netid;
2607c478bd9Sstevel@tonic-gate 		cbp->cb_newer.cb_callback.cb_location.r_netid = NULL;
2617c478bd9Sstevel@tonic-gate 
2627c478bd9Sstevel@tonic-gate 		/* Get the program number */
2637c478bd9Sstevel@tonic-gate 		cbp->cb_callback.cb_program =
264ed57cab9Srg 		    cbp->cb_newer.cb_callback.cb_program;
2657c478bd9Sstevel@tonic-gate 		cbp->cb_newer.cb_callback.cb_program = 0;
2667c478bd9Sstevel@tonic-gate 
2677c478bd9Sstevel@tonic-gate 		/* Don't forget the protocol's "cb_ident" field */
2687c478bd9Sstevel@tonic-gate 		cbp->cb_ident = cbp->cb_newer.cb_ident;
2697c478bd9Sstevel@tonic-gate 		cbp->cb_newer.cb_ident = 0;
2707c478bd9Sstevel@tonic-gate 
2717c478bd9Sstevel@tonic-gate 		/* no longer new */
2727c478bd9Sstevel@tonic-gate 		cbp->cb_newer.cb_new = FALSE;
2737c478bd9Sstevel@tonic-gate 		cbp->cb_newer.cb_confirmed = FALSE;
2747c478bd9Sstevel@tonic-gate 
2757c478bd9Sstevel@tonic-gate 		/* get rid of the old client handles that may exist */
2767c478bd9Sstevel@tonic-gate 		rfs4_cb_chflush(cbp);
2777c478bd9Sstevel@tonic-gate 
2787c478bd9Sstevel@tonic-gate 		cbp->cb_state = CB_NONE;
2797c478bd9Sstevel@tonic-gate 		cbp->cb_timefailed = 0; /* reset the clock */
2807c478bd9Sstevel@tonic-gate 		cbp->cb_notified_of_cb_path_down = TRUE;
2817c478bd9Sstevel@tonic-gate 	}
2827c478bd9Sstevel@tonic-gate 
2837c478bd9Sstevel@tonic-gate 	if (cbp->cb_state != CB_NONE) {
2847c478bd9Sstevel@tonic-gate 		cv_broadcast(cbp->cb_cv);	/* let the others know */
2857c478bd9Sstevel@tonic-gate 		cbp->cb_nullcaller = FALSE;
2867c478bd9Sstevel@tonic-gate 		mutex_exit(cbp->cb_lock);
2877c478bd9Sstevel@tonic-gate 		rfs4_client_rele(cp);
288*0dfe541eSEvan Layton 		zthread_exit();
2897c478bd9Sstevel@tonic-gate 	}
2907c478bd9Sstevel@tonic-gate 
2917c478bd9Sstevel@tonic-gate 	/* mark rfs4_client_t as CALLBACK NULL in progress */
2927c478bd9Sstevel@tonic-gate 	cbp->cb_state = CB_INPROG;
2937c478bd9Sstevel@tonic-gate 	mutex_exit(cbp->cb_lock);
2947c478bd9Sstevel@tonic-gate 
2957c478bd9Sstevel@tonic-gate 	/* get/generate a client handle */
2967c478bd9Sstevel@tonic-gate 	if ((ch = rfs4_cb_getch(cbp)) == NULL) {
2977c478bd9Sstevel@tonic-gate 		mutex_enter(cbp->cb_lock);
2987c478bd9Sstevel@tonic-gate 		cbp->cb_state = CB_BAD;
2997c478bd9Sstevel@tonic-gate 		cbp->cb_timefailed = gethrestime_sec(); /* observability */
3007c478bd9Sstevel@tonic-gate 		goto retry;
3017c478bd9Sstevel@tonic-gate 	}
3027c478bd9Sstevel@tonic-gate 
3037c478bd9Sstevel@tonic-gate 
3047c478bd9Sstevel@tonic-gate 	tv.tv_sec = 30;
3057c478bd9Sstevel@tonic-gate 	tv.tv_usec = 0;
3067c478bd9Sstevel@tonic-gate 	if (clnt_call(ch, CB_NULL, xdr_void, NULL, xdr_void, NULL, tv) != 0) {
3077c478bd9Sstevel@tonic-gate 		newstate = CB_BAD;
3087c478bd9Sstevel@tonic-gate 	} else {
3097c478bd9Sstevel@tonic-gate 		newstate = CB_OK;
3107c478bd9Sstevel@tonic-gate #ifdef	DEBUG
3117c478bd9Sstevel@tonic-gate 		rfs4_cb_null++;
3127c478bd9Sstevel@tonic-gate #endif
3137c478bd9Sstevel@tonic-gate 	}
3147c478bd9Sstevel@tonic-gate 
3157c478bd9Sstevel@tonic-gate 	/* Check to see if the client has specified new callback info */
3167c478bd9Sstevel@tonic-gate 	mutex_enter(cbp->cb_lock);
3177c478bd9Sstevel@tonic-gate 	rfs4_cb_freech(cbp, ch, TRUE);
3187c478bd9Sstevel@tonic-gate 	if (cbp->cb_newer.cb_new == TRUE &&
3197c478bd9Sstevel@tonic-gate 	    cbp->cb_newer.cb_confirmed == TRUE) {
3207c478bd9Sstevel@tonic-gate 		goto retry;	/* give the CB_NULL another chance */
3217c478bd9Sstevel@tonic-gate 	}
3227c478bd9Sstevel@tonic-gate 
3237c478bd9Sstevel@tonic-gate 	cbp->cb_state = newstate;
3247c478bd9Sstevel@tonic-gate 	if (cbp->cb_state == CB_BAD)
3257c478bd9Sstevel@tonic-gate 		cbp->cb_timefailed = gethrestime_sec(); /* observability */
3267c478bd9Sstevel@tonic-gate 
3277c478bd9Sstevel@tonic-gate 	cv_broadcast(cbp->cb_cv);	/* start up the other threads */
3287c478bd9Sstevel@tonic-gate 	cbp->cb_nullcaller = FALSE;
3297c478bd9Sstevel@tonic-gate 	mutex_exit(cbp->cb_lock);
3307c478bd9Sstevel@tonic-gate 	rfs4_client_rele(cp);
331*0dfe541eSEvan Layton 	zthread_exit();
3327c478bd9Sstevel@tonic-gate }
3337c478bd9Sstevel@tonic-gate 
3347c478bd9Sstevel@tonic-gate /*
3357c478bd9Sstevel@tonic-gate  * Given a client struct, inspect the callback info to see if the
336878aca8dSDai Ngo  * callback path is up and available.
337878aca8dSDai Ngo  *
338878aca8dSDai Ngo  * If new callback path is available and no one has set it up then
339878aca8dSDai Ngo  * try to set it up. If setup is not successful after 5 tries (5 secs)
340878aca8dSDai Ngo  * then gives up and returns NULL.
341878aca8dSDai Ngo  *
342878aca8dSDai Ngo  * If callback path is being initialized, then wait for the CB_NULL RPC
343878aca8dSDai Ngo  * call to occur.
3447c478bd9Sstevel@tonic-gate  */
3457c478bd9Sstevel@tonic-gate static rfs4_cbinfo_t *
rfs4_cbinfo_hold(rfs4_client_t * cp)3467c478bd9Sstevel@tonic-gate rfs4_cbinfo_hold(rfs4_client_t *cp)
3477c478bd9Sstevel@tonic-gate {
348d216dff5SRobert Mastors 	rfs4_cbinfo_t *cbp = &cp->rc_cbinfo;
349878aca8dSDai Ngo 	int retries = 0;
3507c478bd9Sstevel@tonic-gate 
3517c478bd9Sstevel@tonic-gate 	mutex_enter(cbp->cb_lock);
3527c478bd9Sstevel@tonic-gate 
353878aca8dSDai Ngo 	while (cbp->cb_newer.cb_new == TRUE && cbp->cb_nullcaller == FALSE) {
3547c478bd9Sstevel@tonic-gate 		/*
3557c478bd9Sstevel@tonic-gate 		 * Looks like a new callback path may be available and
3567c478bd9Sstevel@tonic-gate 		 * noone has set it up.
3577c478bd9Sstevel@tonic-gate 		 */
3587c478bd9Sstevel@tonic-gate 		mutex_exit(cbp->cb_lock);
359d216dff5SRobert Mastors 		rfs4_dbe_hold(cp->rc_dbe);
3607c478bd9Sstevel@tonic-gate 		rfs4_do_cb_null(cp); /* caller will release client hold */
361878aca8dSDai Ngo 
362878aca8dSDai Ngo 		mutex_enter(cbp->cb_lock);
363878aca8dSDai Ngo 		/*
364878aca8dSDai Ngo 		 * If callback path is no longer new, or it's being setup
365878aca8dSDai Ngo 		 * then stop and wait for it to be done.
366878aca8dSDai Ngo 		 */
367878aca8dSDai Ngo 		if (cbp->cb_newer.cb_new == FALSE || cbp->cb_nullcaller == TRUE)
368878aca8dSDai Ngo 			break;
369878aca8dSDai Ngo 		mutex_exit(cbp->cb_lock);
370878aca8dSDai Ngo 
371878aca8dSDai Ngo 		if (++retries >= rfs4_max_setup_cb_tries)
372878aca8dSDai Ngo 			return (NULL);
373878aca8dSDai Ngo 		delay(hz);
374878aca8dSDai Ngo 		mutex_enter(cbp->cb_lock);
3757c478bd9Sstevel@tonic-gate 	}
3767c478bd9Sstevel@tonic-gate 
3777c478bd9Sstevel@tonic-gate 	/* Is there a thread working on doing the CB_NULL RPC? */
3787c478bd9Sstevel@tonic-gate 	if (cbp->cb_nullcaller == TRUE)
3797c478bd9Sstevel@tonic-gate 		cv_wait(cbp->cb_cv, cbp->cb_lock);  /* if so, wait on it */
3807c478bd9Sstevel@tonic-gate 
3817c478bd9Sstevel@tonic-gate 	/* If the callback path is not okay (up and running), just quit */
3827c478bd9Sstevel@tonic-gate 	if (cbp->cb_state != CB_OK) {
3837c478bd9Sstevel@tonic-gate 		mutex_exit(cbp->cb_lock);
3847c478bd9Sstevel@tonic-gate 		return (NULL);
3857c478bd9Sstevel@tonic-gate 	}
3867c478bd9Sstevel@tonic-gate 
3877c478bd9Sstevel@tonic-gate 	/* Let someone know we are using the current callback info */
3887c478bd9Sstevel@tonic-gate 	cbp->cb_refcnt++;
3897c478bd9Sstevel@tonic-gate 	mutex_exit(cbp->cb_lock);
3907c478bd9Sstevel@tonic-gate 	return (cbp);
3917c478bd9Sstevel@tonic-gate }
3927c478bd9Sstevel@tonic-gate 
3937c478bd9Sstevel@tonic-gate /*
3947c478bd9Sstevel@tonic-gate  * The caller is done with the callback info.  It may be that the
3957c478bd9Sstevel@tonic-gate  * caller's RPC failed and the NFSv4 client has actually provided new
3967c478bd9Sstevel@tonic-gate  * callback information.  If so, let the caller know so they can
3977c478bd9Sstevel@tonic-gate  * advantage of this and maybe retry the RPC that originally failed.
3987c478bd9Sstevel@tonic-gate  */
3997c478bd9Sstevel@tonic-gate static int
rfs4_cbinfo_rele(rfs4_cbinfo_t * cbp,rfs4_cbstate_t newstate)4007c478bd9Sstevel@tonic-gate rfs4_cbinfo_rele(rfs4_cbinfo_t *cbp, rfs4_cbstate_t newstate)
4017c478bd9Sstevel@tonic-gate {
4027c478bd9Sstevel@tonic-gate 	int cb_new = FALSE;
4037c478bd9Sstevel@tonic-gate 
4047c478bd9Sstevel@tonic-gate 	mutex_enter(cbp->cb_lock);
4057c478bd9Sstevel@tonic-gate 
4067c478bd9Sstevel@tonic-gate 	/* The caller gets a chance to mark the callback info as bad */
4077c478bd9Sstevel@tonic-gate 	if (newstate != CB_NOCHANGE)
4087c478bd9Sstevel@tonic-gate 		cbp->cb_state = newstate;
4097c478bd9Sstevel@tonic-gate 	if (newstate == CB_FAILED) {
4107c478bd9Sstevel@tonic-gate 		cbp->cb_timefailed = gethrestime_sec(); /* observability */
4117c478bd9Sstevel@tonic-gate 		cbp->cb_notified_of_cb_path_down = FALSE;
4127c478bd9Sstevel@tonic-gate 	}
4137c478bd9Sstevel@tonic-gate 
4147c478bd9Sstevel@tonic-gate 	cbp->cb_refcnt--;	/* no longer using the information */
4157c478bd9Sstevel@tonic-gate 
4167c478bd9Sstevel@tonic-gate 	/*
4177c478bd9Sstevel@tonic-gate 	 * A thread may be waiting on this one to finish and if so,
4187c478bd9Sstevel@tonic-gate 	 * let it know that it is okay to do the CB_NULL to the
4197c478bd9Sstevel@tonic-gate 	 * client's callback server.
4207c478bd9Sstevel@tonic-gate 	 */
4217c478bd9Sstevel@tonic-gate 	if (cbp->cb_refcnt == 0 && cbp->cb_nullcaller)
4227c478bd9Sstevel@tonic-gate 		cv_broadcast(cbp->cb_cv_nullcaller);
4237c478bd9Sstevel@tonic-gate 
4247c478bd9Sstevel@tonic-gate 	/*
4257c478bd9Sstevel@tonic-gate 	 * If this is the last thread to use the callback info and
4267c478bd9Sstevel@tonic-gate 	 * there is new callback information to try and no thread is
4277c478bd9Sstevel@tonic-gate 	 * there ready to do the CB_NULL, then return true to teh
4287c478bd9Sstevel@tonic-gate 	 * caller so they can do the CB_NULL
4297c478bd9Sstevel@tonic-gate 	 */
4307c478bd9Sstevel@tonic-gate 	if (cbp->cb_refcnt == 0 &&
431ed57cab9Srg 	    cbp->cb_nullcaller == FALSE &&
432ed57cab9Srg 	    cbp->cb_newer.cb_new == TRUE &&
433ed57cab9Srg 	    cbp->cb_newer.cb_confirmed == TRUE)
4347c478bd9Sstevel@tonic-gate 		cb_new = TRUE;
4357c478bd9Sstevel@tonic-gate 
4367c478bd9Sstevel@tonic-gate 	mutex_exit(cbp->cb_lock);
4377c478bd9Sstevel@tonic-gate 
4387c478bd9Sstevel@tonic-gate 	return (cb_new);
4397c478bd9Sstevel@tonic-gate }
4407c478bd9Sstevel@tonic-gate 
4417c478bd9Sstevel@tonic-gate /*
4427c478bd9Sstevel@tonic-gate  * Given the information in the callback info struct, create a client
4437c478bd9Sstevel@tonic-gate  * handle that can be used by the server for its callback path.
4447c478bd9Sstevel@tonic-gate  */
4457c478bd9Sstevel@tonic-gate static CLIENT *
rfs4_cbch_init(rfs4_cbinfo_t * cbp)4467c478bd9Sstevel@tonic-gate rfs4_cbch_init(rfs4_cbinfo_t *cbp)
4477c478bd9Sstevel@tonic-gate {
4487c478bd9Sstevel@tonic-gate 	struct knetconfig knc;
4497c478bd9Sstevel@tonic-gate 	vnode_t *vp;
4507c478bd9Sstevel@tonic-gate 	struct sockaddr_in addr4;
4517c478bd9Sstevel@tonic-gate 	struct sockaddr_in6 addr6;
4527c478bd9Sstevel@tonic-gate 	void *addr, *taddr;
4537c478bd9Sstevel@tonic-gate 	in_port_t *pp;
4547c478bd9Sstevel@tonic-gate 	int af;
4557c478bd9Sstevel@tonic-gate 	char *devnam;
4567c478bd9Sstevel@tonic-gate 	struct netbuf nb;
4577c478bd9Sstevel@tonic-gate 	int size;
4587c478bd9Sstevel@tonic-gate 	CLIENT *ch = NULL;
4597c478bd9Sstevel@tonic-gate 	int useresvport = 0;
4607c478bd9Sstevel@tonic-gate 
4617c478bd9Sstevel@tonic-gate 	mutex_enter(cbp->cb_lock);
4627c478bd9Sstevel@tonic-gate 
4637c478bd9Sstevel@tonic-gate 	if (cbp->cb_callback.cb_location.r_netid == NULL ||
4647c478bd9Sstevel@tonic-gate 	    cbp->cb_callback.cb_location.r_addr == NULL) {
4657c478bd9Sstevel@tonic-gate 		goto cb_init_out;
4667c478bd9Sstevel@tonic-gate 	}
4677c478bd9Sstevel@tonic-gate 
4687c478bd9Sstevel@tonic-gate 	if (strcmp(cbp->cb_callback.cb_location.r_netid, "tcp") == 0) {
4697c478bd9Sstevel@tonic-gate 		knc.knc_semantics = NC_TPI_COTS;
4707c478bd9Sstevel@tonic-gate 		knc.knc_protofmly = "inet";
4717c478bd9Sstevel@tonic-gate 		knc.knc_proto = "tcp";
4727c478bd9Sstevel@tonic-gate 		devnam = "/dev/tcp";
4737c478bd9Sstevel@tonic-gate 		af = AF_INET;
4747c478bd9Sstevel@tonic-gate 	} else if (strcmp(cbp->cb_callback.cb_location.r_netid, "udp")
475ed57cab9Srg 	    == 0) {
4767c478bd9Sstevel@tonic-gate 		knc.knc_semantics = NC_TPI_CLTS;
4777c478bd9Sstevel@tonic-gate 		knc.knc_protofmly = "inet";
4787c478bd9Sstevel@tonic-gate 		knc.knc_proto = "udp";
4797c478bd9Sstevel@tonic-gate 		devnam = "/dev/udp";
4807c478bd9Sstevel@tonic-gate 		af = AF_INET;
4817c478bd9Sstevel@tonic-gate 	} else if (strcmp(cbp->cb_callback.cb_location.r_netid, "tcp6")
482ed57cab9Srg 	    == 0) {
4837c478bd9Sstevel@tonic-gate 		knc.knc_semantics = NC_TPI_COTS;
4847c478bd9Sstevel@tonic-gate 		knc.knc_protofmly = "inet6";
4857c478bd9Sstevel@tonic-gate 		knc.knc_proto = "tcp";
4867c478bd9Sstevel@tonic-gate 		devnam = "/dev/tcp6";
4877c478bd9Sstevel@tonic-gate 		af = AF_INET6;
4887c478bd9Sstevel@tonic-gate 	} else if (strcmp(cbp->cb_callback.cb_location.r_netid, "udp6")
489ed57cab9Srg 	    == 0) {
4907c478bd9Sstevel@tonic-gate 		knc.knc_semantics = NC_TPI_CLTS;
4917c478bd9Sstevel@tonic-gate 		knc.knc_protofmly = "inet6";
4927c478bd9Sstevel@tonic-gate 		knc.knc_proto = "udp";
4937c478bd9Sstevel@tonic-gate 		devnam = "/dev/udp6";
4947c478bd9Sstevel@tonic-gate 		af = AF_INET6;
4957c478bd9Sstevel@tonic-gate 	} else {
4967c478bd9Sstevel@tonic-gate 		goto cb_init_out;
4977c478bd9Sstevel@tonic-gate 	}
4987c478bd9Sstevel@tonic-gate 
499c52d60d4Srg 	if (lookupname(devnam, UIO_SYSSPACE, FOLLOW, NULLVPP, &vp) != 0) {
5007c478bd9Sstevel@tonic-gate 
5017c478bd9Sstevel@tonic-gate 		goto cb_init_out;
5027c478bd9Sstevel@tonic-gate 	}
5037c478bd9Sstevel@tonic-gate 
5047c478bd9Sstevel@tonic-gate 	if (vp->v_type != VCHR) {
5057c478bd9Sstevel@tonic-gate 		VN_RELE(vp);
5067c478bd9Sstevel@tonic-gate 		goto cb_init_out;
5077c478bd9Sstevel@tonic-gate 	}
5087c478bd9Sstevel@tonic-gate 
5097c478bd9Sstevel@tonic-gate 	knc.knc_rdev = vp->v_rdev;
5107c478bd9Sstevel@tonic-gate 
5117c478bd9Sstevel@tonic-gate 	VN_RELE(vp);
5127c478bd9Sstevel@tonic-gate 
5137c478bd9Sstevel@tonic-gate 	if (af == AF_INET) {
5147c478bd9Sstevel@tonic-gate 		size = sizeof (addr4);
5157c478bd9Sstevel@tonic-gate 		bzero(&addr4, size);
5167c478bd9Sstevel@tonic-gate 		addr4.sin_family = (sa_family_t)af;
5177c478bd9Sstevel@tonic-gate 		addr = &addr4.sin_addr;
5187c478bd9Sstevel@tonic-gate 		pp = &addr4.sin_port;
5197c478bd9Sstevel@tonic-gate 		taddr = &addr4;
5207c478bd9Sstevel@tonic-gate 	} else /* AF_INET6 */ {
5217c478bd9Sstevel@tonic-gate 		size = sizeof (addr6);
5227c478bd9Sstevel@tonic-gate 		bzero(&addr6, size);
5237c478bd9Sstevel@tonic-gate 		addr6.sin6_family = (sa_family_t)af;
5247c478bd9Sstevel@tonic-gate 		addr = &addr6.sin6_addr;
5257c478bd9Sstevel@tonic-gate 		pp = &addr6.sin6_port;
5267c478bd9Sstevel@tonic-gate 		taddr = &addr6;
5277c478bd9Sstevel@tonic-gate 	}
5287c478bd9Sstevel@tonic-gate 
5297c478bd9Sstevel@tonic-gate 	if (uaddr2sockaddr(af,
530ed57cab9Srg 	    cbp->cb_callback.cb_location.r_addr, addr, pp)) {
5317c478bd9Sstevel@tonic-gate 
5327c478bd9Sstevel@tonic-gate 		goto cb_init_out;
5337c478bd9Sstevel@tonic-gate 	}
5347c478bd9Sstevel@tonic-gate 
5357c478bd9Sstevel@tonic-gate 
5367c478bd9Sstevel@tonic-gate 	nb.maxlen = nb.len = size;
5377c478bd9Sstevel@tonic-gate 	nb.buf = (char *)taddr;
5387c478bd9Sstevel@tonic-gate 
539c52d60d4Srg 	if (clnt_tli_kcreate(&knc, &nb, cbp->cb_callback.cb_program,
5407c478bd9Sstevel@tonic-gate 	    NFS_CB, 0, 0, curthread->t_cred, &ch)) {
5417c478bd9Sstevel@tonic-gate 
5427c478bd9Sstevel@tonic-gate 		ch = NULL;
5437c478bd9Sstevel@tonic-gate 	}
5447c478bd9Sstevel@tonic-gate 
5457c478bd9Sstevel@tonic-gate 	/* turn off reserved port usage */
5467c478bd9Sstevel@tonic-gate 	(void) CLNT_CONTROL(ch, CLSET_BINDRESVPORT, (char *)&useresvport);
5477c478bd9Sstevel@tonic-gate 
5487c478bd9Sstevel@tonic-gate cb_init_out:
5497c478bd9Sstevel@tonic-gate 	mutex_exit(cbp->cb_lock);
5507c478bd9Sstevel@tonic-gate 	return (ch);
5517c478bd9Sstevel@tonic-gate }
5527c478bd9Sstevel@tonic-gate 
5537c478bd9Sstevel@tonic-gate /*
5547c478bd9Sstevel@tonic-gate  * Iterate over the client handle cache and
5557c478bd9Sstevel@tonic-gate  * destroy it.
5567c478bd9Sstevel@tonic-gate  */
5577c478bd9Sstevel@tonic-gate static void
rfs4_cb_chflush(rfs4_cbinfo_t * cbp)5587c478bd9Sstevel@tonic-gate rfs4_cb_chflush(rfs4_cbinfo_t *cbp)
5597c478bd9Sstevel@tonic-gate {
5607c478bd9Sstevel@tonic-gate 	CLIENT *ch;
5617c478bd9Sstevel@tonic-gate 
5627c478bd9Sstevel@tonic-gate 	while (cbp->cb_chc_free) {
5637c478bd9Sstevel@tonic-gate 		cbp->cb_chc_free--;
5647c478bd9Sstevel@tonic-gate 		ch = cbp->cb_chc[cbp->cb_chc_free];
5657c478bd9Sstevel@tonic-gate 		cbp->cb_chc[cbp->cb_chc_free] = NULL;
5667c478bd9Sstevel@tonic-gate 		if (ch) {
5677c478bd9Sstevel@tonic-gate 			if (ch->cl_auth)
5687c478bd9Sstevel@tonic-gate 				auth_destroy(ch->cl_auth);
5697c478bd9Sstevel@tonic-gate 			clnt_destroy(ch);
5707c478bd9Sstevel@tonic-gate 		}
5717c478bd9Sstevel@tonic-gate 	}
5727c478bd9Sstevel@tonic-gate }
5737c478bd9Sstevel@tonic-gate 
5747c478bd9Sstevel@tonic-gate /*
5757c478bd9Sstevel@tonic-gate  * Return a client handle, either from a the small
5767c478bd9Sstevel@tonic-gate  * rfs4_client_t cache or one that we just created.
5777c478bd9Sstevel@tonic-gate  */
5787c478bd9Sstevel@tonic-gate static CLIENT *
rfs4_cb_getch(rfs4_cbinfo_t * cbp)5797c478bd9Sstevel@tonic-gate rfs4_cb_getch(rfs4_cbinfo_t *cbp)
5807c478bd9Sstevel@tonic-gate {
5817c478bd9Sstevel@tonic-gate 	CLIENT *cbch = NULL;
5827c478bd9Sstevel@tonic-gate 	uint32_t zilch = 0;
5837c478bd9Sstevel@tonic-gate 
5847c478bd9Sstevel@tonic-gate 	mutex_enter(cbp->cb_lock);
5857c478bd9Sstevel@tonic-gate 
5867c478bd9Sstevel@tonic-gate 	if (cbp->cb_chc_free) {
5877c478bd9Sstevel@tonic-gate 		cbp->cb_chc_free--;
5887c478bd9Sstevel@tonic-gate 		cbch = cbp->cb_chc[ cbp->cb_chc_free ];
5897c478bd9Sstevel@tonic-gate 		mutex_exit(cbp->cb_lock);
5907c478bd9Sstevel@tonic-gate 		(void) CLNT_CONTROL(cbch, CLSET_XID, (char *)&zilch);
5917c478bd9Sstevel@tonic-gate 		return (cbch);
5927c478bd9Sstevel@tonic-gate 	}
5937c478bd9Sstevel@tonic-gate 
5947c478bd9Sstevel@tonic-gate 	mutex_exit(cbp->cb_lock);
5957c478bd9Sstevel@tonic-gate 
5967c478bd9Sstevel@tonic-gate 	/* none free so make it now */
5977c478bd9Sstevel@tonic-gate 	cbch = rfs4_cbch_init(cbp);
5987c478bd9Sstevel@tonic-gate 
5997c478bd9Sstevel@tonic-gate 	return (cbch);
6007c478bd9Sstevel@tonic-gate }
6017c478bd9Sstevel@tonic-gate 
6027c478bd9Sstevel@tonic-gate /*
6037c478bd9Sstevel@tonic-gate  * Return the client handle to the small cache or
6047c478bd9Sstevel@tonic-gate  * destroy it.
6057c478bd9Sstevel@tonic-gate  */
6067c478bd9Sstevel@tonic-gate static void
rfs4_cb_freech(rfs4_cbinfo_t * cbp,CLIENT * ch,bool_t lockheld)6077c478bd9Sstevel@tonic-gate rfs4_cb_freech(rfs4_cbinfo_t *cbp, CLIENT *ch, bool_t lockheld)
6087c478bd9Sstevel@tonic-gate {
6097c478bd9Sstevel@tonic-gate 	if (lockheld == FALSE)
6107c478bd9Sstevel@tonic-gate 		mutex_enter(cbp->cb_lock);
6117c478bd9Sstevel@tonic-gate 
6127c478bd9Sstevel@tonic-gate 	if (cbp->cb_chc_free < RFS4_CBCH_MAX) {
6137c478bd9Sstevel@tonic-gate 		cbp->cb_chc[ cbp->cb_chc_free++ ] = ch;
6147c478bd9Sstevel@tonic-gate 		if (lockheld == FALSE)
6157c478bd9Sstevel@tonic-gate 			mutex_exit(cbp->cb_lock);
6167c478bd9Sstevel@tonic-gate 		return;
6177c478bd9Sstevel@tonic-gate 	}
6187c478bd9Sstevel@tonic-gate 	if (lockheld == FALSE)
6197c478bd9Sstevel@tonic-gate 		mutex_exit(cbp->cb_lock);
6207c478bd9Sstevel@tonic-gate 
6217c478bd9Sstevel@tonic-gate 	/*
6227c478bd9Sstevel@tonic-gate 	 * cache maxed out of free entries, obliterate
6237c478bd9Sstevel@tonic-gate 	 * this client handle, destroy it, throw it away.
6247c478bd9Sstevel@tonic-gate 	 */
6257c478bd9Sstevel@tonic-gate 	if (ch->cl_auth)
6267c478bd9Sstevel@tonic-gate 		auth_destroy(ch->cl_auth);
6277c478bd9Sstevel@tonic-gate 	clnt_destroy(ch);
6287c478bd9Sstevel@tonic-gate }
6297c478bd9Sstevel@tonic-gate 
6307c478bd9Sstevel@tonic-gate /*
6317c478bd9Sstevel@tonic-gate  * With the supplied callback information - initialize the client
6327c478bd9Sstevel@tonic-gate  * callback data.  If there is a callback in progress, save the
6337c478bd9Sstevel@tonic-gate  * callback info so that a thread can pick it up in the future.
6347c478bd9Sstevel@tonic-gate  */
6357c478bd9Sstevel@tonic-gate void
rfs4_client_setcb(rfs4_client_t * cp,cb_client4 * cb,uint32_t cb_ident)6367c478bd9Sstevel@tonic-gate rfs4_client_setcb(rfs4_client_t *cp, cb_client4 *cb, uint32_t cb_ident)
6377c478bd9Sstevel@tonic-gate {
6387c478bd9Sstevel@tonic-gate 	char *addr = NULL;
6397c478bd9Sstevel@tonic-gate 	char *netid = NULL;
640d216dff5SRobert Mastors 	rfs4_cbinfo_t *cbp = &cp->rc_cbinfo;
6417c478bd9Sstevel@tonic-gate 	size_t len;
6427c478bd9Sstevel@tonic-gate 
6437c478bd9Sstevel@tonic-gate 	/* Set the call back for the client */
644a31a5cfdSsamf 	if (cb->cb_location.r_addr && cb->cb_location.r_addr[0] != '\0' &&
645a31a5cfdSsamf 	    cb->cb_location.r_netid && cb->cb_location.r_netid[0] != '\0') {
6467c478bd9Sstevel@tonic-gate 		len = strlen(cb->cb_location.r_addr) + 1;
6477c478bd9Sstevel@tonic-gate 		addr = kmem_alloc(len, KM_SLEEP);
6487c478bd9Sstevel@tonic-gate 		bcopy(cb->cb_location.r_addr, addr, len);
6497c478bd9Sstevel@tonic-gate 		len = strlen(cb->cb_location.r_netid) + 1;
6507c478bd9Sstevel@tonic-gate 		netid = kmem_alloc(len, KM_SLEEP);
6517c478bd9Sstevel@tonic-gate 		bcopy(cb->cb_location.r_netid, netid, len);
6527c478bd9Sstevel@tonic-gate 	}
6537c478bd9Sstevel@tonic-gate 	/* ready to save the new information but first free old, if exists */
6547c478bd9Sstevel@tonic-gate 	mutex_enter(cbp->cb_lock);
6557c478bd9Sstevel@tonic-gate 
6567c478bd9Sstevel@tonic-gate 	cbp->cb_newer.cb_callback.cb_program = cb->cb_program;
657a31a5cfdSsamf 
658a31a5cfdSsamf 	if (cbp->cb_newer.cb_callback.cb_location.r_addr != NULL)
659a31a5cfdSsamf 		kmem_free(cbp->cb_newer.cb_callback.cb_location.r_addr,
660a31a5cfdSsamf 		    strlen(cbp->cb_newer.cb_callback.cb_location.r_addr) + 1);
6617c478bd9Sstevel@tonic-gate 	cbp->cb_newer.cb_callback.cb_location.r_addr = addr;
662a31a5cfdSsamf 
663a31a5cfdSsamf 	if (cbp->cb_newer.cb_callback.cb_location.r_netid != NULL)
664a31a5cfdSsamf 		kmem_free(cbp->cb_newer.cb_callback.cb_location.r_netid,
665a31a5cfdSsamf 		    strlen(cbp->cb_newer.cb_callback.cb_location.r_netid) + 1);
6667c478bd9Sstevel@tonic-gate 	cbp->cb_newer.cb_callback.cb_location.r_netid = netid;
6677c478bd9Sstevel@tonic-gate 
6687c478bd9Sstevel@tonic-gate 	cbp->cb_newer.cb_ident = cb_ident;
6697c478bd9Sstevel@tonic-gate 
6707c478bd9Sstevel@tonic-gate 	if (addr && *addr && netid && *netid) {
6717c478bd9Sstevel@tonic-gate 		cbp->cb_newer.cb_new = TRUE;
6727c478bd9Sstevel@tonic-gate 		cbp->cb_newer.cb_confirmed = FALSE;
6737c478bd9Sstevel@tonic-gate 	} else {
6747c478bd9Sstevel@tonic-gate 		cbp->cb_newer.cb_new = FALSE;
6757c478bd9Sstevel@tonic-gate 		cbp->cb_newer.cb_confirmed = FALSE;
6767c478bd9Sstevel@tonic-gate 	}
6777c478bd9Sstevel@tonic-gate 
6787c478bd9Sstevel@tonic-gate 	mutex_exit(cbp->cb_lock);
6797c478bd9Sstevel@tonic-gate }
6807c478bd9Sstevel@tonic-gate 
6817c478bd9Sstevel@tonic-gate /*
6827c478bd9Sstevel@tonic-gate  * The server uses this when processing SETCLIENTID_CONFIRM.  Callback
6837c478bd9Sstevel@tonic-gate  * information may have been provided on SETCLIENTID and this call
6847c478bd9Sstevel@tonic-gate  * marks that information as confirmed and then starts a thread to
6857c478bd9Sstevel@tonic-gate  * test the callback path.
6867c478bd9Sstevel@tonic-gate  */
6877c478bd9Sstevel@tonic-gate void
rfs4_deleg_cb_check(rfs4_client_t * cp)6887c478bd9Sstevel@tonic-gate rfs4_deleg_cb_check(rfs4_client_t *cp)
6897c478bd9Sstevel@tonic-gate {
690d216dff5SRobert Mastors 	if (cp->rc_cbinfo.cb_newer.cb_new == FALSE)
6917c478bd9Sstevel@tonic-gate 		return;
6927c478bd9Sstevel@tonic-gate 
693d216dff5SRobert Mastors 	cp->rc_cbinfo.cb_newer.cb_confirmed = TRUE;
6947c478bd9Sstevel@tonic-gate 
695d216dff5SRobert Mastors 	rfs4_dbe_hold(cp->rc_dbe); /* hold the client struct for thread */
6967c478bd9Sstevel@tonic-gate 
697*0dfe541eSEvan Layton 	(void) zthread_create(NULL, 0, rfs4_do_cb_null, cp, 0,
698ed57cab9Srg 	    minclsyspri);
6997c478bd9Sstevel@tonic-gate }
7007c478bd9Sstevel@tonic-gate 
7017c478bd9Sstevel@tonic-gate static void
rfs4args_cb_recall_free(nfs_cb_argop4 * argop)7027c478bd9Sstevel@tonic-gate rfs4args_cb_recall_free(nfs_cb_argop4 *argop)
7037c478bd9Sstevel@tonic-gate {
7047c478bd9Sstevel@tonic-gate 	CB_RECALL4args	*rec_argp;
7057c478bd9Sstevel@tonic-gate 
7067c478bd9Sstevel@tonic-gate 	rec_argp = &argop->nfs_cb_argop4_u.opcbrecall;
7077c478bd9Sstevel@tonic-gate 	if (rec_argp->fh.nfs_fh4_val)
7087c478bd9Sstevel@tonic-gate 		kmem_free(rec_argp->fh.nfs_fh4_val, rec_argp->fh.nfs_fh4_len);
7097c478bd9Sstevel@tonic-gate }
7107c478bd9Sstevel@tonic-gate 
7117c478bd9Sstevel@tonic-gate /* ARGSUSED */
7127c478bd9Sstevel@tonic-gate static void
rfs4args_cb_getattr_free(nfs_cb_argop4 * argop)7137c478bd9Sstevel@tonic-gate rfs4args_cb_getattr_free(nfs_cb_argop4 *argop)
7147c478bd9Sstevel@tonic-gate {
7157c478bd9Sstevel@tonic-gate 	CB_GETATTR4args *argp;
7167c478bd9Sstevel@tonic-gate 
7177c478bd9Sstevel@tonic-gate 	argp = &argop->nfs_cb_argop4_u.opcbgetattr;
7187c478bd9Sstevel@tonic-gate 	if (argp->fh.nfs_fh4_val)
7197c478bd9Sstevel@tonic-gate 		kmem_free(argp->fh.nfs_fh4_val, argp->fh.nfs_fh4_len);
7207c478bd9Sstevel@tonic-gate }
7217c478bd9Sstevel@tonic-gate 
7227c478bd9Sstevel@tonic-gate static void
rfs4freeargres(CB_COMPOUND4args * args,CB_COMPOUND4res * resp)7237c478bd9Sstevel@tonic-gate rfs4freeargres(CB_COMPOUND4args *args, CB_COMPOUND4res *resp)
7247c478bd9Sstevel@tonic-gate {
7257c478bd9Sstevel@tonic-gate 	int i, arglen;
7267c478bd9Sstevel@tonic-gate 	nfs_cb_argop4 *argop;
7277c478bd9Sstevel@tonic-gate 
7287c478bd9Sstevel@tonic-gate 	/*
7297c478bd9Sstevel@tonic-gate 	 * First free any special args alloc'd for specific ops.
7307c478bd9Sstevel@tonic-gate 	 */
7317c478bd9Sstevel@tonic-gate 	arglen = args->array_len;
7327c478bd9Sstevel@tonic-gate 	argop = args->array;
7337c478bd9Sstevel@tonic-gate 	for (i = 0; i < arglen; i++, argop++) {
7347c478bd9Sstevel@tonic-gate 
7357c478bd9Sstevel@tonic-gate 		switch (argop->argop) {
7367c478bd9Sstevel@tonic-gate 		case OP_CB_RECALL:
7377c478bd9Sstevel@tonic-gate 			rfs4args_cb_recall_free(argop);
7387c478bd9Sstevel@tonic-gate 			break;
7397c478bd9Sstevel@tonic-gate 
7407c478bd9Sstevel@tonic-gate 		case OP_CB_GETATTR:
7417c478bd9Sstevel@tonic-gate 			rfs4args_cb_getattr_free(argop);
7427c478bd9Sstevel@tonic-gate 			break;
7437c478bd9Sstevel@tonic-gate 
7447c478bd9Sstevel@tonic-gate 		default:
7457c478bd9Sstevel@tonic-gate 			return;
7467c478bd9Sstevel@tonic-gate 		}
7477c478bd9Sstevel@tonic-gate 	}
7487c478bd9Sstevel@tonic-gate 
7497c478bd9Sstevel@tonic-gate 	if (args->tag.utf8string_len > 0)
7507c478bd9Sstevel@tonic-gate 		UTF8STRING_FREE(args->tag)
7517c478bd9Sstevel@tonic-gate 
7527c478bd9Sstevel@tonic-gate 	kmem_free(args->array, arglen * sizeof (nfs_cb_argop4));
7537c478bd9Sstevel@tonic-gate 	if (resp)
754a17ce845SMarcel Telka 		xdr_free(xdr_CB_COMPOUND4res, (caddr_t)resp);
7557c478bd9Sstevel@tonic-gate }
7567c478bd9Sstevel@tonic-gate 
7577c478bd9Sstevel@tonic-gate /*
7587c478bd9Sstevel@tonic-gate  * General callback routine for the server to the client.
7597c478bd9Sstevel@tonic-gate  */
7607c478bd9Sstevel@tonic-gate static enum clnt_stat
rfs4_do_callback(rfs4_client_t * cp,CB_COMPOUND4args * args,CB_COMPOUND4res * res,struct timeval timeout)761d216dff5SRobert Mastors rfs4_do_callback(rfs4_client_t *cp, CB_COMPOUND4args *args,
762d216dff5SRobert Mastors     CB_COMPOUND4res *res, struct timeval timeout)
7637c478bd9Sstevel@tonic-gate {
7647c478bd9Sstevel@tonic-gate 	rfs4_cbinfo_t *cbp;
7657c478bd9Sstevel@tonic-gate 	CLIENT *ch;
7667c478bd9Sstevel@tonic-gate 	/* start with this in case cb_getch() fails */
7677c478bd9Sstevel@tonic-gate 	enum clnt_stat	stat = RPC_FAILED;
7687c478bd9Sstevel@tonic-gate 
7697c478bd9Sstevel@tonic-gate 	res->tag.utf8string_val = NULL;
7707c478bd9Sstevel@tonic-gate 	res->array = NULL;
7717c478bd9Sstevel@tonic-gate 
7727c478bd9Sstevel@tonic-gate retry:
7737c478bd9Sstevel@tonic-gate 	cbp = rfs4_cbinfo_hold(cp);
7747c478bd9Sstevel@tonic-gate 	if (cbp == NULL)
7757c478bd9Sstevel@tonic-gate 		return (stat);
7767c478bd9Sstevel@tonic-gate 
7777c478bd9Sstevel@tonic-gate 	/* get a client handle */
7787c478bd9Sstevel@tonic-gate 	if ((ch = rfs4_cb_getch(cbp)) != NULL) {
7797c478bd9Sstevel@tonic-gate 		/*
7807c478bd9Sstevel@tonic-gate 		 * reset the cb_ident since it may have changed in
7817c478bd9Sstevel@tonic-gate 		 * rfs4_cbinfo_hold()
7827c478bd9Sstevel@tonic-gate 		 */
7837c478bd9Sstevel@tonic-gate 		args->callback_ident = cbp->cb_ident;
7847c478bd9Sstevel@tonic-gate 
785eac3aab7Srobinson 		stat = clnt_call(ch, CB_COMPOUND, xdr_CB_COMPOUND4args_srv,
786ed57cab9Srg 		    (caddr_t)args, xdr_CB_COMPOUND4res,
787ed57cab9Srg 		    (caddr_t)res, timeout);
7887c478bd9Sstevel@tonic-gate 
7897c478bd9Sstevel@tonic-gate 		/* free client handle */
7907c478bd9Sstevel@tonic-gate 		rfs4_cb_freech(cbp, ch, FALSE);
7917c478bd9Sstevel@tonic-gate 	}
7927c478bd9Sstevel@tonic-gate 
7937c478bd9Sstevel@tonic-gate 	/*
7947c478bd9Sstevel@tonic-gate 	 * If the rele says that there may be new callback info then
7957c478bd9Sstevel@tonic-gate 	 * retry this sequence and it may succeed as a result of the
7967c478bd9Sstevel@tonic-gate 	 * new callback path
7977c478bd9Sstevel@tonic-gate 	 */
7987c478bd9Sstevel@tonic-gate 	if (rfs4_cbinfo_rele(cbp,
799ed57cab9Srg 	    (stat == RPC_SUCCESS ? CB_NOCHANGE : CB_FAILED)) == TRUE)
8007c478bd9Sstevel@tonic-gate 		goto retry;
8017c478bd9Sstevel@tonic-gate 
8027c478bd9Sstevel@tonic-gate 	return (stat);
8037c478bd9Sstevel@tonic-gate }
8047c478bd9Sstevel@tonic-gate 
8057c478bd9Sstevel@tonic-gate /*
8067c478bd9Sstevel@tonic-gate  * Used by the NFSv4 server to get attributes for a file while
8077c478bd9Sstevel@tonic-gate  * handling the case where a file has been write delegated.  For the
8087c478bd9Sstevel@tonic-gate  * time being, VOP_GETATTR() is called and CB_GETATTR processing is
8097c478bd9Sstevel@tonic-gate  * not undertaken.  This call site is maintained in case the server is
8107c478bd9Sstevel@tonic-gate  * updated in the future to handle write delegation space guarantees.
8117c478bd9Sstevel@tonic-gate  */
8127c478bd9Sstevel@tonic-gate nfsstat4
rfs4_vop_getattr(vnode_t * vp,vattr_t * vap,int flag,cred_t * cr)8137c478bd9Sstevel@tonic-gate rfs4_vop_getattr(vnode_t *vp, vattr_t *vap, int flag, cred_t *cr)
8147c478bd9Sstevel@tonic-gate {
81500ba712dSGarima Tripathi 
8167c478bd9Sstevel@tonic-gate 	int error;
8177c478bd9Sstevel@tonic-gate 
818da6c28aaSamw 	error = VOP_GETATTR(vp, vap, flag, cr, NULL);
8197c478bd9Sstevel@tonic-gate 	return (puterrno4(error));
8207c478bd9Sstevel@tonic-gate }
8217c478bd9Sstevel@tonic-gate 
8227c478bd9Sstevel@tonic-gate /*
8237c478bd9Sstevel@tonic-gate  * This is used everywhere in the v2/v3 server to allow the
8247c478bd9Sstevel@tonic-gate  * integration of all NFS versions and the support of delegation.  For
8257c478bd9Sstevel@tonic-gate  * now, just call the VOP_GETATTR().  If the NFSv4 server is enhanced
8267c478bd9Sstevel@tonic-gate  * in the future to provide space guarantees for write delegations
8277c478bd9Sstevel@tonic-gate  * then this call site should be expanded to interact with the client.
8287c478bd9Sstevel@tonic-gate  */
8297c478bd9Sstevel@tonic-gate int
rfs4_delegated_getattr(vnode_t * vp,vattr_t * vap,int flag,cred_t * cr)8307c478bd9Sstevel@tonic-gate rfs4_delegated_getattr(vnode_t *vp, vattr_t *vap, int flag, cred_t *cr)
8317c478bd9Sstevel@tonic-gate {
832da6c28aaSamw 	return (VOP_GETATTR(vp, vap, flag, cr, NULL));
8337c478bd9Sstevel@tonic-gate }
8347c478bd9Sstevel@tonic-gate 
8357c478bd9Sstevel@tonic-gate /*
8367c478bd9Sstevel@tonic-gate  * Place the actual cb_recall otw call to client.
8377c478bd9Sstevel@tonic-gate  */
8387c478bd9Sstevel@tonic-gate static void
rfs4_do_cb_recall(rfs4_deleg_state_t * dsp,bool_t trunc)8397c478bd9Sstevel@tonic-gate rfs4_do_cb_recall(rfs4_deleg_state_t *dsp, bool_t trunc)
8407c478bd9Sstevel@tonic-gate {
8417c478bd9Sstevel@tonic-gate 	CB_COMPOUND4args	cb4_args;
8427c478bd9Sstevel@tonic-gate 	CB_COMPOUND4res		cb4_res;
8437c478bd9Sstevel@tonic-gate 	CB_RECALL4args		*rec_argp;
844f3b585ceSsamf 	CB_RECALL4res		*rec_resp;
8457c478bd9Sstevel@tonic-gate 	nfs_cb_argop4		*argop;
8467c478bd9Sstevel@tonic-gate 	int			numops;
8477c478bd9Sstevel@tonic-gate 	int			argoplist_size;
8487c478bd9Sstevel@tonic-gate 	struct timeval		timeout;
8497c478bd9Sstevel@tonic-gate 	nfs_fh4			*fhp;
8507c478bd9Sstevel@tonic-gate 	enum clnt_stat		call_stat;
8517c478bd9Sstevel@tonic-gate 
8527c478bd9Sstevel@tonic-gate 	/*
8537c478bd9Sstevel@tonic-gate 	 * set up the compound args
8547c478bd9Sstevel@tonic-gate 	 */
8557c478bd9Sstevel@tonic-gate 	numops = 1;	/* CB_RECALL only */
8567c478bd9Sstevel@tonic-gate 
8577c478bd9Sstevel@tonic-gate 	argoplist_size = numops * sizeof (nfs_cb_argop4);
8587c478bd9Sstevel@tonic-gate 	argop = kmem_zalloc(argoplist_size, KM_SLEEP);
8597c478bd9Sstevel@tonic-gate 	argop->argop = OP_CB_RECALL;
8607c478bd9Sstevel@tonic-gate 	rec_argp = &argop->nfs_cb_argop4_u.opcbrecall;
8617c478bd9Sstevel@tonic-gate 
8627c478bd9Sstevel@tonic-gate 	(void) str_to_utf8("cb_recall", &cb4_args.tag);
8637c478bd9Sstevel@tonic-gate 	cb4_args.minorversion = CB4_MINORVERSION;
8647c478bd9Sstevel@tonic-gate 	/* cb4_args.callback_ident is set in rfs4_do_callback() */
8657c478bd9Sstevel@tonic-gate 	cb4_args.array_len = numops;
8667c478bd9Sstevel@tonic-gate 	cb4_args.array = argop;
8677c478bd9Sstevel@tonic-gate 
8687c478bd9Sstevel@tonic-gate 	/*
8697c478bd9Sstevel@tonic-gate 	 * fill in the args struct
8707c478bd9Sstevel@tonic-gate 	 */
871d216dff5SRobert Mastors 	bcopy(&dsp->rds_delegid.stateid, &rec_argp->stateid, sizeof (stateid4));
8727c478bd9Sstevel@tonic-gate 	rec_argp->truncate = trunc;
8737c478bd9Sstevel@tonic-gate 
874d216dff5SRobert Mastors 	fhp = &dsp->rds_finfo->rf_filehandle;
8757c478bd9Sstevel@tonic-gate 	rec_argp->fh.nfs_fh4_val = kmem_alloc(sizeof (char) *
876ed57cab9Srg 	    fhp->nfs_fh4_len, KM_SLEEP);
8777c478bd9Sstevel@tonic-gate 	nfs_fh4_copy(fhp, &rec_argp->fh);
8787c478bd9Sstevel@tonic-gate 
8797c478bd9Sstevel@tonic-gate 	/* Keep track of when we did this for observability */
880d216dff5SRobert Mastors 	dsp->rds_time_recalled = gethrestime_sec();
8817c478bd9Sstevel@tonic-gate 
8827c478bd9Sstevel@tonic-gate 	/*
8837c478bd9Sstevel@tonic-gate 	 * Set up the timeout for the callback and make the actual call.
8847c478bd9Sstevel@tonic-gate 	 * Timeout will be 80% of the lease period for this server.
8857c478bd9Sstevel@tonic-gate 	 */
8867c478bd9Sstevel@tonic-gate 	timeout.tv_sec = (rfs4_lease_time * 80) / 100;
8877c478bd9Sstevel@tonic-gate 	timeout.tv_usec = 0;
8887c478bd9Sstevel@tonic-gate 
889d216dff5SRobert Mastors 	DTRACE_NFSV4_3(cb__recall__start, rfs4_client_t *, dsp->rds_client,
890f3b585ceSsamf 	    rfs4_deleg_state_t *, dsp, CB_RECALL4args *, rec_argp);
891f3b585ceSsamf 
892d216dff5SRobert Mastors 	call_stat = rfs4_do_callback(dsp->rds_client, &cb4_args, &cb4_res,
893d216dff5SRobert Mastors 	    timeout);
8947c478bd9Sstevel@tonic-gate 
895f3b585ceSsamf 	rec_resp = (cb4_res.array_len == 0) ? NULL :
896f3b585ceSsamf 	    &cb4_res.array[0].nfs_cb_resop4_u.opcbrecall;
897f3b585ceSsamf 
898d216dff5SRobert Mastors 	DTRACE_NFSV4_3(cb__recall__done, rfs4_client_t *, dsp->rds_client,
899f3b585ceSsamf 	    rfs4_deleg_state_t *, dsp, CB_RECALL4res *, rec_resp);
900f3b585ceSsamf 
9017c478bd9Sstevel@tonic-gate 	if (call_stat != RPC_SUCCESS || cb4_res.status != NFS4_OK) {
902d216dff5SRobert Mastors 		rfs4_return_deleg(dsp, TRUE);
9037c478bd9Sstevel@tonic-gate 	}
9047c478bd9Sstevel@tonic-gate 
9057c478bd9Sstevel@tonic-gate 	rfs4freeargres(&cb4_args, &cb4_res);
9067c478bd9Sstevel@tonic-gate }
9077c478bd9Sstevel@tonic-gate 
9087c478bd9Sstevel@tonic-gate struct recall_arg {
9097c478bd9Sstevel@tonic-gate 	rfs4_deleg_state_t *dsp;
9107c478bd9Sstevel@tonic-gate 	void (*recall)(rfs4_deleg_state_t *, bool_t trunc);
9117c478bd9Sstevel@tonic-gate 	bool_t trunc;
9127c478bd9Sstevel@tonic-gate };
9137c478bd9Sstevel@tonic-gate 
9147c478bd9Sstevel@tonic-gate static void
do_recall(struct recall_arg * arg)9157c478bd9Sstevel@tonic-gate do_recall(struct recall_arg *arg)
9167c478bd9Sstevel@tonic-gate {
9177c478bd9Sstevel@tonic-gate 	rfs4_deleg_state_t *dsp = arg->dsp;
918d216dff5SRobert Mastors 	rfs4_file_t *fp = dsp->rds_finfo;
9197c478bd9Sstevel@tonic-gate 	callb_cpr_t cpr_info;
9207c478bd9Sstevel@tonic-gate 	kmutex_t cpr_lock;
9217c478bd9Sstevel@tonic-gate 
9227c478bd9Sstevel@tonic-gate 	mutex_init(&cpr_lock, NULL, MUTEX_DEFAULT, NULL);
9237c478bd9Sstevel@tonic-gate 	CALLB_CPR_INIT(&cpr_info, &cpr_lock, callb_generic_cpr, "nfsv4Recall");
9247c478bd9Sstevel@tonic-gate 
9257c478bd9Sstevel@tonic-gate 	/*
9267c478bd9Sstevel@tonic-gate 	 * It is possible that before this thread starts
9277c478bd9Sstevel@tonic-gate 	 * the client has send us a return_delegation, and
9287c478bd9Sstevel@tonic-gate 	 * if that is the case we do not need to send the
9297c478bd9Sstevel@tonic-gate 	 * recall callback.
9307c478bd9Sstevel@tonic-gate 	 */
931d216dff5SRobert Mastors 	if (dsp->rds_dtype != OPEN_DELEGATE_NONE) {
932ed57cab9Srg 		DTRACE_PROBE3(nfss__i__recall,
933ed57cab9Srg 		    struct recall_arg *, arg,
934ed57cab9Srg 		    struct rfs4_deleg_state_t *, dsp,
935ed57cab9Srg 		    struct rfs4_file_t *, fp);
9367c478bd9Sstevel@tonic-gate 
9377c478bd9Sstevel@tonic-gate 		if (arg->recall)
9387c478bd9Sstevel@tonic-gate 			(void) (*arg->recall)(dsp, arg->trunc);
9397c478bd9Sstevel@tonic-gate 	}
9407c478bd9Sstevel@tonic-gate 
941d216dff5SRobert Mastors 	mutex_enter(fp->rf_dinfo.rd_recall_lock);
9427c478bd9Sstevel@tonic-gate 	/*
9437c478bd9Sstevel@tonic-gate 	 * Recall count may go negative if the parent thread that is
9447c478bd9Sstevel@tonic-gate 	 * creating the individual callback threads does not modify
9457c478bd9Sstevel@tonic-gate 	 * the recall_count field before the callback thread actually
9467c478bd9Sstevel@tonic-gate 	 * gets a response from the CB_RECALL
9477c478bd9Sstevel@tonic-gate 	 */
948d216dff5SRobert Mastors 	fp->rf_dinfo.rd_recall_count--;
949d216dff5SRobert Mastors 	if (fp->rf_dinfo.rd_recall_count == 0)
950d216dff5SRobert Mastors 		cv_signal(fp->rf_dinfo.rd_recall_cv);
951d216dff5SRobert Mastors 	mutex_exit(fp->rf_dinfo.rd_recall_lock);
9527c478bd9Sstevel@tonic-gate 
9537c478bd9Sstevel@tonic-gate 	mutex_enter(&cpr_lock);
9547c478bd9Sstevel@tonic-gate 	CALLB_CPR_EXIT(&cpr_info);
9557c478bd9Sstevel@tonic-gate 	mutex_destroy(&cpr_lock);
9567c478bd9Sstevel@tonic-gate 
9577c478bd9Sstevel@tonic-gate 	rfs4_deleg_state_rele(dsp); /* release the hold for this thread */
9587c478bd9Sstevel@tonic-gate 	kmem_free(arg, sizeof (struct recall_arg));
959*0dfe541eSEvan Layton 	zthread_exit();
9607c478bd9Sstevel@tonic-gate }
9617c478bd9Sstevel@tonic-gate 
9627c478bd9Sstevel@tonic-gate struct master_recall_args {
9637c478bd9Sstevel@tonic-gate     rfs4_file_t *fp;
9647c478bd9Sstevel@tonic-gate     void (*recall)(rfs4_deleg_state_t *, bool_t);
9657c478bd9Sstevel@tonic-gate     bool_t trunc;
9667c478bd9Sstevel@tonic-gate };
9677c478bd9Sstevel@tonic-gate 
9687c478bd9Sstevel@tonic-gate static void
do_recall_file(struct master_recall_args * map)9697c478bd9Sstevel@tonic-gate do_recall_file(struct master_recall_args *map)
9707c478bd9Sstevel@tonic-gate {
9717c478bd9Sstevel@tonic-gate 	rfs4_file_t *fp = map->fp;
9727c478bd9Sstevel@tonic-gate 	rfs4_deleg_state_t *dsp;
9737c478bd9Sstevel@tonic-gate 	struct recall_arg *arg;
9747c478bd9Sstevel@tonic-gate 	callb_cpr_t cpr_info;
9757c478bd9Sstevel@tonic-gate 	kmutex_t cpr_lock;
9767c478bd9Sstevel@tonic-gate 	int32_t recall_count;
9777c478bd9Sstevel@tonic-gate 
978d216dff5SRobert Mastors 	rfs4_dbe_lock(fp->rf_dbe);
979ed57cab9Srg 
980ed57cab9Srg 	/* Recall already in progress ? */
981d216dff5SRobert Mastors 	mutex_enter(fp->rf_dinfo.rd_recall_lock);
982d216dff5SRobert Mastors 	if (fp->rf_dinfo.rd_recall_count != 0) {
983d216dff5SRobert Mastors 		mutex_exit(fp->rf_dinfo.rd_recall_lock);
984d216dff5SRobert Mastors 		rfs4_dbe_rele_nolock(fp->rf_dbe);
985d216dff5SRobert Mastors 		rfs4_dbe_unlock(fp->rf_dbe);
9867c478bd9Sstevel@tonic-gate 		kmem_free(map, sizeof (struct master_recall_args));
987*0dfe541eSEvan Layton 		zthread_exit();
9887c478bd9Sstevel@tonic-gate 	}
9897c478bd9Sstevel@tonic-gate 
990d216dff5SRobert Mastors 	mutex_exit(fp->rf_dinfo.rd_recall_lock);
991ed57cab9Srg 
9927c478bd9Sstevel@tonic-gate 	mutex_init(&cpr_lock, NULL, MUTEX_DEFAULT, NULL);
993ed57cab9Srg 	CALLB_CPR_INIT(&cpr_info, &cpr_lock, callb_generic_cpr,	"v4RecallFile");
9947c478bd9Sstevel@tonic-gate 
9957c478bd9Sstevel@tonic-gate 	recall_count = 0;
996d216dff5SRobert Mastors 	for (dsp = list_head(&fp->rf_delegstatelist); dsp != NULL;
997d216dff5SRobert Mastors 	    dsp = list_next(&fp->rf_delegstatelist, dsp)) {
998ed57cab9Srg 
999d216dff5SRobert Mastors 		rfs4_dbe_lock(dsp->rds_dbe);
1000ed57cab9Srg 		/*
1001ed57cab9Srg 		 * if this delegation state
1002ed57cab9Srg 		 * is being reaped skip it
1003ed57cab9Srg 		 */
1004d216dff5SRobert Mastors 		if (rfs4_dbe_is_invalid(dsp->rds_dbe)) {
1005d216dff5SRobert Mastors 			rfs4_dbe_unlock(dsp->rds_dbe);
1006ed57cab9Srg 			continue;
1007ed57cab9Srg 		}
1008ed57cab9Srg 
1009ed57cab9Srg 		/* hold for receiving thread */
1010d216dff5SRobert Mastors 		rfs4_dbe_hold(dsp->rds_dbe);
1011d216dff5SRobert Mastors 		rfs4_dbe_unlock(dsp->rds_dbe);
1012ed57cab9Srg 
10137c478bd9Sstevel@tonic-gate 		arg = kmem_alloc(sizeof (struct recall_arg), KM_SLEEP);
10147c478bd9Sstevel@tonic-gate 		arg->recall = map->recall;
10157c478bd9Sstevel@tonic-gate 		arg->trunc = map->trunc;
10167c478bd9Sstevel@tonic-gate 		arg->dsp = dsp;
10177c478bd9Sstevel@tonic-gate 
10187c478bd9Sstevel@tonic-gate 		recall_count++;
10197c478bd9Sstevel@tonic-gate 
1020*0dfe541eSEvan Layton 		(void) zthread_create(NULL, 0, do_recall, arg, 0,
1021ed57cab9Srg 		    minclsyspri);
10227c478bd9Sstevel@tonic-gate 	}
1023ed57cab9Srg 
1024d216dff5SRobert Mastors 	rfs4_dbe_unlock(fp->rf_dbe);
10257c478bd9Sstevel@tonic-gate 
1026d216dff5SRobert Mastors 	mutex_enter(fp->rf_dinfo.rd_recall_lock);
10277c478bd9Sstevel@tonic-gate 	/*
10287c478bd9Sstevel@tonic-gate 	 * Recall count may go negative if the parent thread that is
10297c478bd9Sstevel@tonic-gate 	 * creating the individual callback threads does not modify
10307c478bd9Sstevel@tonic-gate 	 * the recall_count field before the callback thread actually
10317c478bd9Sstevel@tonic-gate 	 * gets a response from the CB_RECALL
10327c478bd9Sstevel@tonic-gate 	 */
1033d216dff5SRobert Mastors 	fp->rf_dinfo.rd_recall_count += recall_count;
1034d216dff5SRobert Mastors 	while (fp->rf_dinfo.rd_recall_count)
1035d216dff5SRobert Mastors 		cv_wait(fp->rf_dinfo.rd_recall_cv, fp->rf_dinfo.rd_recall_lock);
10367c478bd9Sstevel@tonic-gate 
1037d216dff5SRobert Mastors 	mutex_exit(fp->rf_dinfo.rd_recall_lock);
10387c478bd9Sstevel@tonic-gate 
1039ed57cab9Srg 	DTRACE_PROBE1(nfss__i__recall_done, rfs4_file_t *, fp);
10407c478bd9Sstevel@tonic-gate 	rfs4_file_rele(fp);
10417c478bd9Sstevel@tonic-gate 	kmem_free(map, sizeof (struct master_recall_args));
10427c478bd9Sstevel@tonic-gate 	mutex_enter(&cpr_lock);
10437c478bd9Sstevel@tonic-gate 	CALLB_CPR_EXIT(&cpr_info);
10447c478bd9Sstevel@tonic-gate 	mutex_destroy(&cpr_lock);
1045