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  */
217c478bd9Sstevel@tonic-gate /*
22878aca8dSDai Ngo  * Copyright 2009 Sun Microsystems, Inc.  All rights reserved.
237c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
247c478bd9Sstevel@tonic-gate  */
257c478bd9Sstevel@tonic-gate 
267c478bd9Sstevel@tonic-gate #include <sys/systm.h>
277c478bd9Sstevel@tonic-gate #include <rpc/auth.h>
287c478bd9Sstevel@tonic-gate #include <rpc/clnt.h>
297c478bd9Sstevel@tonic-gate #include <nfs/nfs4_kprot.h>
307c478bd9Sstevel@tonic-gate #include <nfs/nfs4.h>
317c478bd9Sstevel@tonic-gate #include <nfs/lm.h>
327c478bd9Sstevel@tonic-gate #include <sys/cmn_err.h>
337c478bd9Sstevel@tonic-gate #include <sys/disp.h>
34f3b585ceSsamf #include <sys/sdt.h>
357c478bd9Sstevel@tonic-gate 
367c478bd9Sstevel@tonic-gate #include <sys/pathname.h>
377c478bd9Sstevel@tonic-gate 
387c478bd9Sstevel@tonic-gate #include <sys/strsubr.h>
397c478bd9Sstevel@tonic-gate #include <sys/ddi.h>
407c478bd9Sstevel@tonic-gate 
417c478bd9Sstevel@tonic-gate #include <sys/vnode.h>
42ed57cab9Srg #include <sys/sdt.h>
437c478bd9Sstevel@tonic-gate #include <inet/common.h>
447c478bd9Sstevel@tonic-gate #include <inet/ip.h>
457c478bd9Sstevel@tonic-gate #include <inet/ip6.h>
467c478bd9Sstevel@tonic-gate 
477c478bd9Sstevel@tonic-gate #define	MAX_READ_DELEGATIONS 5
487c478bd9Sstevel@tonic-gate 
497c478bd9Sstevel@tonic-gate krwlock_t rfs4_deleg_policy_lock;
507c478bd9Sstevel@tonic-gate srv_deleg_policy_t rfs4_deleg_policy = SRV_NEVER_DELEGATE;
517c478bd9Sstevel@tonic-gate static int rfs4_deleg_wlp = 5;
527c478bd9Sstevel@tonic-gate kmutex_t rfs4_deleg_lock;
537c478bd9Sstevel@tonic-gate static int rfs4_deleg_disabled;
54878aca8dSDai Ngo static int rfs4_max_setup_cb_tries = 5;
557c478bd9Sstevel@tonic-gate 
567c478bd9Sstevel@tonic-gate #ifdef DEBUG
577c478bd9Sstevel@tonic-gate 
587c478bd9Sstevel@tonic-gate static int rfs4_test_cbgetattr_fail = 0;
597c478bd9Sstevel@tonic-gate int rfs4_cb_null;
607c478bd9Sstevel@tonic-gate int rfs4_cb_debug;
617c478bd9Sstevel@tonic-gate int rfs4_deleg_debug;
627c478bd9Sstevel@tonic-gate 
637c478bd9Sstevel@tonic-gate #endif
647c478bd9Sstevel@tonic-gate 
657c478bd9Sstevel@tonic-gate static void rfs4_recall_file(rfs4_file_t *,
66*d216dff5SRobert Mastors     void (*recall)(rfs4_deleg_state_t *, bool_t),
67*d216dff5SRobert Mastors     bool_t, rfs4_client_t *);
687c478bd9Sstevel@tonic-gate static	void		rfs4_revoke_file(rfs4_file_t *);
697c478bd9Sstevel@tonic-gate static	void		rfs4_cb_chflush(rfs4_cbinfo_t *);
707c478bd9Sstevel@tonic-gate static	CLIENT		*rfs4_cb_getch(rfs4_cbinfo_t *);
717c478bd9Sstevel@tonic-gate static	void		rfs4_cb_freech(rfs4_cbinfo_t *, CLIENT *, bool_t);
727c478bd9Sstevel@tonic-gate static rfs4_deleg_state_t *rfs4_deleg_state(rfs4_state_t *,
73*d216dff5SRobert Mastors     open_delegation_type4, int *);
747c478bd9Sstevel@tonic-gate 
757c478bd9Sstevel@tonic-gate /*
767c478bd9Sstevel@tonic-gate  * Convert a universal address to an transport specific
777c478bd9Sstevel@tonic-gate  * address using inet_pton.
787c478bd9Sstevel@tonic-gate  */
797c478bd9Sstevel@tonic-gate static int
807c478bd9Sstevel@tonic-gate uaddr2sockaddr(int af, char *ua, void *ap, in_port_t *pp)
817c478bd9Sstevel@tonic-gate {
827c478bd9Sstevel@tonic-gate 	int dots = 0, i, j, len, k;
837c478bd9Sstevel@tonic-gate 	unsigned char c;
847c478bd9Sstevel@tonic-gate 	in_port_t port = 0;
857c478bd9Sstevel@tonic-gate 
867c478bd9Sstevel@tonic-gate 	len = strlen(ua);
877c478bd9Sstevel@tonic-gate 
887c478bd9Sstevel@tonic-gate 	for (i = len-1; i >= 0; i--) {
897c478bd9Sstevel@tonic-gate 
907c478bd9Sstevel@tonic-gate 		if (ua[i] == '.')
917c478bd9Sstevel@tonic-gate 			dots++;
927c478bd9Sstevel@tonic-gate 
937c478bd9Sstevel@tonic-gate 		if (dots == 2) {
947c478bd9Sstevel@tonic-gate 
957c478bd9Sstevel@tonic-gate 			ua[i] = '\0';
967c478bd9Sstevel@tonic-gate 			/*
977c478bd9Sstevel@tonic-gate 			 * We use k to remember were to stick '.' back, since
987c478bd9Sstevel@tonic-gate 			 * ua was kmem_allocateded from the pool len+1.
997c478bd9Sstevel@tonic-gate 			 */
1007c478bd9Sstevel@tonic-gate 			k = i;
1017c478bd9Sstevel@tonic-gate 			if (inet_pton(af, ua, ap) == 1) {
1027c478bd9Sstevel@tonic-gate 
1037c478bd9Sstevel@tonic-gate 				c = 0;
1047c478bd9Sstevel@tonic-gate 
1057c478bd9Sstevel@tonic-gate 				for (j = i+1; j < len; j++) {
1067c478bd9Sstevel@tonic-gate 					if (ua[j] == '.') {
1077c478bd9Sstevel@tonic-gate 						port = c << 8;
1087c478bd9Sstevel@tonic-gate 						c = 0;
1097c478bd9Sstevel@tonic-gate 					} else if (ua[j] >= '0' &&
1107c478bd9Sstevel@tonic-gate 					    ua[j] <= '9') {
1117c478bd9Sstevel@tonic-gate 						c *= 10;
1127c478bd9Sstevel@tonic-gate 						c += ua[j] - '0';
1137c478bd9Sstevel@tonic-gate 					} else {
1147c478bd9Sstevel@tonic-gate 						ua[k] = '.';
1157c478bd9Sstevel@tonic-gate 						return (EINVAL);
1167c478bd9Sstevel@tonic-gate 					}
1177c478bd9Sstevel@tonic-gate 				}
1187c478bd9Sstevel@tonic-gate 				port += c;
1197c478bd9Sstevel@tonic-gate 
1207c478bd9Sstevel@tonic-gate 
1217c478bd9Sstevel@tonic-gate 				/* reset to network order */
1227c478bd9Sstevel@tonic-gate 				if (af == AF_INET) {
1237c478bd9Sstevel@tonic-gate 					*(uint32_t *)ap =
124ed57cab9Srg 					    htonl(*(uint32_t *)ap);
1257c478bd9Sstevel@tonic-gate 					*pp = htons(port);
1267c478bd9Sstevel@tonic-gate 				} else {
1277c478bd9Sstevel@tonic-gate 					int ix;
1287c478bd9Sstevel@tonic-gate 					uint16_t *sap;
1297c478bd9Sstevel@tonic-gate 
1307c478bd9Sstevel@tonic-gate 					for (sap = ap, ix = 0; ix <
1317c478bd9Sstevel@tonic-gate 					    sizeof (struct in6_addr) /
1327c478bd9Sstevel@tonic-gate 					    sizeof (uint16_t); ix++)
1337c478bd9Sstevel@tonic-gate 						sap[ix] = htons(sap[ix]);
1347c478bd9Sstevel@tonic-gate 
1357c478bd9Sstevel@tonic-gate 					*pp = htons(port);
1367c478bd9Sstevel@tonic-gate 				}
1377c478bd9Sstevel@tonic-gate 
1387c478bd9Sstevel@tonic-gate 				ua[k] = '.';
1397c478bd9Sstevel@tonic-gate 				return (0);
1407c478bd9Sstevel@tonic-gate 			} else {
1417c478bd9Sstevel@tonic-gate 				ua[k] = '.';
1427c478bd9Sstevel@tonic-gate 				return (EINVAL);
1437c478bd9Sstevel@tonic-gate 			}
1447c478bd9Sstevel@tonic-gate 		}
1457c478bd9Sstevel@tonic-gate 	}
1467c478bd9Sstevel@tonic-gate 
1477c478bd9Sstevel@tonic-gate 	return (EINVAL);
1487c478bd9Sstevel@tonic-gate }
1497c478bd9Sstevel@tonic-gate 
1507c478bd9Sstevel@tonic-gate /*
1517c478bd9Sstevel@tonic-gate  * Update the delegation policy with the
1527c478bd9Sstevel@tonic-gate  * value of "new_policy"
1537c478bd9Sstevel@tonic-gate  */
1547c478bd9Sstevel@tonic-gate void
1557c478bd9Sstevel@tonic-gate rfs4_set_deleg_policy(srv_deleg_policy_t new_policy)
1567c478bd9Sstevel@tonic-gate {
1577c478bd9Sstevel@tonic-gate 	rw_enter(&rfs4_deleg_policy_lock, RW_WRITER);
1587c478bd9Sstevel@tonic-gate 	rfs4_deleg_policy = new_policy;
1597c478bd9Sstevel@tonic-gate 	rw_exit(&rfs4_deleg_policy_lock);
1607c478bd9Sstevel@tonic-gate }
1617c478bd9Sstevel@tonic-gate 
1627c478bd9Sstevel@tonic-gate void
1637c478bd9Sstevel@tonic-gate rfs4_hold_deleg_policy(void)
1647c478bd9Sstevel@tonic-gate {
1657c478bd9Sstevel@tonic-gate 	rw_enter(&rfs4_deleg_policy_lock, RW_READER);
1667c478bd9Sstevel@tonic-gate }
1677c478bd9Sstevel@tonic-gate 
1687c478bd9Sstevel@tonic-gate void
1697c478bd9Sstevel@tonic-gate rfs4_rele_deleg_policy(void)
1707c478bd9Sstevel@tonic-gate {
1717c478bd9Sstevel@tonic-gate 	rw_exit(&rfs4_deleg_policy_lock);
1727c478bd9Sstevel@tonic-gate }
1737c478bd9Sstevel@tonic-gate 
1747c478bd9Sstevel@tonic-gate 
1757c478bd9Sstevel@tonic-gate /*
1767c478bd9Sstevel@tonic-gate  * This free function is to be used when the client struct is being
1777c478bd9Sstevel@tonic-gate  * released and nothing at all is needed of the callback info any
1787c478bd9Sstevel@tonic-gate  * longer.
1797c478bd9Sstevel@tonic-gate  */
1807c478bd9Sstevel@tonic-gate void
1817c478bd9Sstevel@tonic-gate rfs4_cbinfo_free(rfs4_cbinfo_t *cbp)
1827c478bd9Sstevel@tonic-gate {
1837c478bd9Sstevel@tonic-gate 	char *addr = cbp->cb_callback.cb_location.r_addr;
1847c478bd9Sstevel@tonic-gate 	char *netid = cbp->cb_callback.cb_location.r_netid;
1857c478bd9Sstevel@tonic-gate 
1867c478bd9Sstevel@tonic-gate 	/* Free old address if any */
1877c478bd9Sstevel@tonic-gate 
1887c478bd9Sstevel@tonic-gate 	if (addr)
1897c478bd9Sstevel@tonic-gate 		kmem_free(addr, strlen(addr) + 1);
1907c478bd9Sstevel@tonic-gate 	if (netid)
1917c478bd9Sstevel@tonic-gate 		kmem_free(netid, strlen(netid) + 1);
1927c478bd9Sstevel@tonic-gate 
1937c478bd9Sstevel@tonic-gate 	addr = cbp->cb_newer.cb_callback.cb_location.r_addr;
1947c478bd9Sstevel@tonic-gate 	netid = cbp->cb_newer.cb_callback.cb_location.r_netid;
1957c478bd9Sstevel@tonic-gate 
1967c478bd9Sstevel@tonic-gate 	if (addr)
1977c478bd9Sstevel@tonic-gate 		kmem_free(addr, strlen(addr) + 1);
1987c478bd9Sstevel@tonic-gate 	if (netid)
1997c478bd9Sstevel@tonic-gate 		kmem_free(netid, strlen(netid) + 1);
2007c478bd9Sstevel@tonic-gate 
2017c478bd9Sstevel@tonic-gate 	if (cbp->cb_chc_free) {
2027c478bd9Sstevel@tonic-gate 		rfs4_cb_chflush(cbp);
2037c478bd9Sstevel@tonic-gate 	}
2047c478bd9Sstevel@tonic-gate }
2057c478bd9Sstevel@tonic-gate 
2067c478bd9Sstevel@tonic-gate /*
2077c478bd9Sstevel@tonic-gate  * The server uses this to check the callback path supplied by the
2087c478bd9Sstevel@tonic-gate  * client.  The callback connection is marked "in progress" while this
2097c478bd9Sstevel@tonic-gate  * work is going on and then eventually marked either OK or FAILED.
2107c478bd9Sstevel@tonic-gate  * This work can be done as part of a separate thread and at the end
2117c478bd9Sstevel@tonic-gate  * of this the thread will exit or it may be done such that the caller
2127c478bd9Sstevel@tonic-gate  * will continue with other work.
2137c478bd9Sstevel@tonic-gate  */
2147c478bd9Sstevel@tonic-gate static void
2157c478bd9Sstevel@tonic-gate rfs4_do_cb_null(rfs4_client_t *cp)
2167c478bd9Sstevel@tonic-gate {
2177c478bd9Sstevel@tonic-gate 	struct timeval tv;
2187c478bd9Sstevel@tonic-gate 	CLIENT *ch;
2197c478bd9Sstevel@tonic-gate 	rfs4_cbstate_t newstate;
220*d216dff5SRobert Mastors 	rfs4_cbinfo_t *cbp = &cp->rc_cbinfo;
2217c478bd9Sstevel@tonic-gate 
2227c478bd9Sstevel@tonic-gate 	mutex_enter(cbp->cb_lock);
2237c478bd9Sstevel@tonic-gate 	/* If another thread is doing CB_NULL RPC then return */
2247c478bd9Sstevel@tonic-gate 	if (cbp->cb_nullcaller == TRUE) {
2257c478bd9Sstevel@tonic-gate 		mutex_exit(cbp->cb_lock);
2267c478bd9Sstevel@tonic-gate 		rfs4_client_rele(cp);
2277c478bd9Sstevel@tonic-gate 		return;
2287c478bd9Sstevel@tonic-gate 	}
2297c478bd9Sstevel@tonic-gate 
2307c478bd9Sstevel@tonic-gate 	/* Mark the cbinfo as having a thread in the NULL callback */
2317c478bd9Sstevel@tonic-gate 	cbp->cb_nullcaller = TRUE;
2327c478bd9Sstevel@tonic-gate 
2337c478bd9Sstevel@tonic-gate 	/*
2347c478bd9Sstevel@tonic-gate 	 * Are there other threads still using the cbinfo client
2357c478bd9Sstevel@tonic-gate 	 * handles?  If so, this thread must wait before going and
2367c478bd9Sstevel@tonic-gate 	 * mucking aroiund with the callback information
2377c478bd9Sstevel@tonic-gate 	 */
238a31a5cfdSsamf 	while (cbp->cb_refcnt != 0)
2397c478bd9Sstevel@tonic-gate 		cv_wait(cbp->cb_cv_nullcaller, cbp->cb_lock);
2407c478bd9Sstevel@tonic-gate 
2417c478bd9Sstevel@tonic-gate 	/*
2427c478bd9Sstevel@tonic-gate 	 * This thread itself may find that new callback info has
2437c478bd9Sstevel@tonic-gate 	 * arrived and is set up to handle this case and redrive the
2447c478bd9Sstevel@tonic-gate 	 * call to the client's callback server.
2457c478bd9Sstevel@tonic-gate 	 */
2467c478bd9Sstevel@tonic-gate retry:
2477c478bd9Sstevel@tonic-gate 	if (cbp->cb_newer.cb_new == TRUE &&
2487c478bd9Sstevel@tonic-gate 	    cbp->cb_newer.cb_confirmed == TRUE) {
2497c478bd9Sstevel@tonic-gate 		char *addr = cbp->cb_callback.cb_location.r_addr;
2507c478bd9Sstevel@tonic-gate 		char *netid = cbp->cb_callback.cb_location.r_netid;
2517c478bd9Sstevel@tonic-gate 
2527c478bd9Sstevel@tonic-gate 		/*
2537c478bd9Sstevel@tonic-gate 		 * Free the old stuff if it exists; may be the first
2547c478bd9Sstevel@tonic-gate 		 * time through this path
2557c478bd9Sstevel@tonic-gate 		 */
2567c478bd9Sstevel@tonic-gate 		if (addr)
2577c478bd9Sstevel@tonic-gate 			kmem_free(addr, strlen(addr) + 1);
2587c478bd9Sstevel@tonic-gate 		if (netid)
2597c478bd9Sstevel@tonic-gate 			kmem_free(netid, strlen(netid) + 1);
2607c478bd9Sstevel@tonic-gate 
2617c478bd9Sstevel@tonic-gate 		/* Move over the addr/netid */
2627c478bd9Sstevel@tonic-gate 		cbp->cb_callback.cb_location.r_addr =
263ed57cab9Srg 		    cbp->cb_newer.cb_callback.cb_location.r_addr;
2647c478bd9Sstevel@tonic-gate 		cbp->cb_newer.cb_callback.cb_location.r_addr = NULL;
2657c478bd9Sstevel@tonic-gate 		cbp->cb_callback.cb_location.r_netid =
266ed57cab9Srg 		    cbp->cb_newer.cb_callback.cb_location.r_netid;
2677c478bd9Sstevel@tonic-gate 		cbp->cb_newer.cb_callback.cb_location.r_netid = NULL;
2687c478bd9Sstevel@tonic-gate 
2697c478bd9Sstevel@tonic-gate 		/* Get the program number */
2707c478bd9Sstevel@tonic-gate 		cbp->cb_callback.cb_program =
271ed57cab9Srg 		    cbp->cb_newer.cb_callback.cb_program;
2727c478bd9Sstevel@tonic-gate 		cbp->cb_newer.cb_callback.cb_program = 0;
2737c478bd9Sstevel@tonic-gate 
2747c478bd9Sstevel@tonic-gate 		/* Don't forget the protocol's "cb_ident" field */
2757c478bd9Sstevel@tonic-gate 		cbp->cb_ident = cbp->cb_newer.cb_ident;
2767c478bd9Sstevel@tonic-gate 		cbp->cb_newer.cb_ident = 0;
2777c478bd9Sstevel@tonic-gate 
2787c478bd9Sstevel@tonic-gate 		/* no longer new */
2797c478bd9Sstevel@tonic-gate 		cbp->cb_newer.cb_new = FALSE;
2807c478bd9Sstevel@tonic-gate 		cbp->cb_newer.cb_confirmed = FALSE;
2817c478bd9Sstevel@tonic-gate 
2827c478bd9Sstevel@tonic-gate 		/* get rid of the old client handles that may exist */
2837c478bd9Sstevel@tonic-gate 		rfs4_cb_chflush(cbp);
2847c478bd9Sstevel@tonic-gate 
2857c478bd9Sstevel@tonic-gate 		cbp->cb_state = CB_NONE;
2867c478bd9Sstevel@tonic-gate 		cbp->cb_timefailed = 0; /* reset the clock */
2877c478bd9Sstevel@tonic-gate 		cbp->cb_notified_of_cb_path_down = TRUE;
2887c478bd9Sstevel@tonic-gate 	}
2897c478bd9Sstevel@tonic-gate 
2907c478bd9Sstevel@tonic-gate 	if (cbp->cb_state != CB_NONE) {
2917c478bd9Sstevel@tonic-gate 		cv_broadcast(cbp->cb_cv);	/* let the others know */
2927c478bd9Sstevel@tonic-gate 		cbp->cb_nullcaller = FALSE;
2937c478bd9Sstevel@tonic-gate 		mutex_exit(cbp->cb_lock);
2947c478bd9Sstevel@tonic-gate 		rfs4_client_rele(cp);
2957c478bd9Sstevel@tonic-gate 		return;
2967c478bd9Sstevel@tonic-gate 	}
2977c478bd9Sstevel@tonic-gate 
2987c478bd9Sstevel@tonic-gate 	/* mark rfs4_client_t as CALLBACK NULL in progress */
2997c478bd9Sstevel@tonic-gate 	cbp->cb_state = CB_INPROG;
3007c478bd9Sstevel@tonic-gate 	mutex_exit(cbp->cb_lock);
3017c478bd9Sstevel@tonic-gate 
3027c478bd9Sstevel@tonic-gate 	/* get/generate a client handle */
3037c478bd9Sstevel@tonic-gate 	if ((ch = rfs4_cb_getch(cbp)) == NULL) {
3047c478bd9Sstevel@tonic-gate 		mutex_enter(cbp->cb_lock);
3057c478bd9Sstevel@tonic-gate 		cbp->cb_state = CB_BAD;
3067c478bd9Sstevel@tonic-gate 		cbp->cb_timefailed = gethrestime_sec(); /* observability */
3077c478bd9Sstevel@tonic-gate 		goto retry;
3087c478bd9Sstevel@tonic-gate 	}
3097c478bd9Sstevel@tonic-gate 
3107c478bd9Sstevel@tonic-gate 
3117c478bd9Sstevel@tonic-gate 	tv.tv_sec = 30;
3127c478bd9Sstevel@tonic-gate 	tv.tv_usec = 0;
3137c478bd9Sstevel@tonic-gate 	if (clnt_call(ch, CB_NULL, xdr_void, NULL, xdr_void, NULL, tv) != 0) {
3147c478bd9Sstevel@tonic-gate 		newstate = CB_BAD;
3157c478bd9Sstevel@tonic-gate 	} else {
3167c478bd9Sstevel@tonic-gate 		newstate = CB_OK;
3177c478bd9Sstevel@tonic-gate #ifdef	DEBUG
3187c478bd9Sstevel@tonic-gate 		rfs4_cb_null++;
3197c478bd9Sstevel@tonic-gate #endif
3207c478bd9Sstevel@tonic-gate 	}
3217c478bd9Sstevel@tonic-gate 
3227c478bd9Sstevel@tonic-gate 	/* Check to see if the client has specified new callback info */
3237c478bd9Sstevel@tonic-gate 	mutex_enter(cbp->cb_lock);
3247c478bd9Sstevel@tonic-gate 	rfs4_cb_freech(cbp, ch, TRUE);
3257c478bd9Sstevel@tonic-gate 	if (cbp->cb_newer.cb_new == TRUE &&
3267c478bd9Sstevel@tonic-gate 	    cbp->cb_newer.cb_confirmed == TRUE) {
3277c478bd9Sstevel@tonic-gate 		goto retry;	/* give the CB_NULL another chance */
3287c478bd9Sstevel@tonic-gate 	}
3297c478bd9Sstevel@tonic-gate 
3307c478bd9Sstevel@tonic-gate 	cbp->cb_state = newstate;
3317c478bd9Sstevel@tonic-gate 	if (cbp->cb_state == CB_BAD)
3327c478bd9Sstevel@tonic-gate 		cbp->cb_timefailed = gethrestime_sec(); /* observability */
3337c478bd9Sstevel@tonic-gate 
3347c478bd9Sstevel@tonic-gate 	cv_broadcast(cbp->cb_cv);	/* start up the other threads */
3357c478bd9Sstevel@tonic-gate 	cbp->cb_nullcaller = FALSE;
3367c478bd9Sstevel@tonic-gate 	mutex_exit(cbp->cb_lock);
3377c478bd9Sstevel@tonic-gate 
3387c478bd9Sstevel@tonic-gate 	rfs4_client_rele(cp);
3397c478bd9Sstevel@tonic-gate }
3407c478bd9Sstevel@tonic-gate 
3417c478bd9Sstevel@tonic-gate /*
3427c478bd9Sstevel@tonic-gate  * Given a client struct, inspect the callback info to see if the
343878aca8dSDai Ngo  * callback path is up and available.
344878aca8dSDai Ngo  *
345878aca8dSDai Ngo  * If new callback path is available and no one has set it up then
346878aca8dSDai Ngo  * try to set it up. If setup is not successful after 5 tries (5 secs)
347878aca8dSDai Ngo  * then gives up and returns NULL.
348878aca8dSDai Ngo  *
349878aca8dSDai Ngo  * If callback path is being initialized, then wait for the CB_NULL RPC
350878aca8dSDai Ngo  * call to occur.
3517c478bd9Sstevel@tonic-gate  */
3527c478bd9Sstevel@tonic-gate static rfs4_cbinfo_t *
3537c478bd9Sstevel@tonic-gate rfs4_cbinfo_hold(rfs4_client_t *cp)
3547c478bd9Sstevel@tonic-gate {
355*d216dff5SRobert Mastors 	rfs4_cbinfo_t *cbp = &cp->rc_cbinfo;
356878aca8dSDai Ngo 	int retries = 0;
3577c478bd9Sstevel@tonic-gate 
3587c478bd9Sstevel@tonic-gate 	mutex_enter(cbp->cb_lock);
3597c478bd9Sstevel@tonic-gate 
360878aca8dSDai Ngo 	while (cbp->cb_newer.cb_new == TRUE && cbp->cb_nullcaller == FALSE) {
3617c478bd9Sstevel@tonic-gate 		/*
3627c478bd9Sstevel@tonic-gate 		 * Looks like a new callback path may be available and
3637c478bd9Sstevel@tonic-gate 		 * noone has set it up.
3647c478bd9Sstevel@tonic-gate 		 */
3657c478bd9Sstevel@tonic-gate 		mutex_exit(cbp->cb_lock);
366*d216dff5SRobert Mastors 		rfs4_dbe_hold(cp->rc_dbe);
3677c478bd9Sstevel@tonic-gate 		rfs4_do_cb_null(cp); /* caller will release client hold */
368878aca8dSDai Ngo 
369878aca8dSDai Ngo 		mutex_enter(cbp->cb_lock);
370878aca8dSDai Ngo 		/*
371878aca8dSDai Ngo 		 * If callback path is no longer new, or it's being setup
372878aca8dSDai Ngo 		 * then stop and wait for it to be done.
373878aca8dSDai Ngo 		 */
374878aca8dSDai Ngo 		if (cbp->cb_newer.cb_new == FALSE || cbp->cb_nullcaller == TRUE)
375878aca8dSDai Ngo 			break;
376878aca8dSDai Ngo 		mutex_exit(cbp->cb_lock);
377878aca8dSDai Ngo 
378878aca8dSDai Ngo 		if (++retries >= rfs4_max_setup_cb_tries)
379878aca8dSDai Ngo 			return (NULL);
380878aca8dSDai Ngo 		delay(hz);
381878aca8dSDai Ngo 		mutex_enter(cbp->cb_lock);
3827c478bd9Sstevel@tonic-gate 	}
3837c478bd9Sstevel@tonic-gate 
3847c478bd9Sstevel@tonic-gate 	/* Is there a thread working on doing the CB_NULL RPC? */
3857c478bd9Sstevel@tonic-gate 	if (cbp->cb_nullcaller == TRUE)
3867c478bd9Sstevel@tonic-gate 		cv_wait(cbp->cb_cv, cbp->cb_lock);  /* if so, wait on it */
3877c478bd9Sstevel@tonic-gate 
3887c478bd9Sstevel@tonic-gate 	/* If the callback path is not okay (up and running), just quit */
3897c478bd9Sstevel@tonic-gate 	if (cbp->cb_state != CB_OK) {
3907c478bd9Sstevel@tonic-gate 		mutex_exit(cbp->cb_lock);
3917c478bd9Sstevel@tonic-gate 		return (NULL);
3927c478bd9Sstevel@tonic-gate 	}
3937c478bd9Sstevel@tonic-gate 
3947c478bd9Sstevel@tonic-gate 	/* Let someone know we are using the current callback info */
3957c478bd9Sstevel@tonic-gate 	cbp->cb_refcnt++;
3967c478bd9Sstevel@tonic-gate 	mutex_exit(cbp->cb_lock);
3977c478bd9Sstevel@tonic-gate 	return (cbp);
3987c478bd9Sstevel@tonic-gate }
3997c478bd9Sstevel@tonic-gate 
4007c478bd9Sstevel@tonic-gate /*
4017c478bd9Sstevel@tonic-gate  * The caller is done with the callback info.  It may be that the
4027c478bd9Sstevel@tonic-gate  * caller's RPC failed and the NFSv4 client has actually provided new
4037c478bd9Sstevel@tonic-gate  * callback information.  If so, let the caller know so they can
4047c478bd9Sstevel@tonic-gate  * advantage of this and maybe retry the RPC that originally failed.
4057c478bd9Sstevel@tonic-gate  */
4067c478bd9Sstevel@tonic-gate static int
4077c478bd9Sstevel@tonic-gate rfs4_cbinfo_rele(rfs4_cbinfo_t *cbp, rfs4_cbstate_t newstate)
4087c478bd9Sstevel@tonic-gate {
4097c478bd9Sstevel@tonic-gate 	int cb_new = FALSE;
4107c478bd9Sstevel@tonic-gate 
4117c478bd9Sstevel@tonic-gate 	mutex_enter(cbp->cb_lock);
4127c478bd9Sstevel@tonic-gate 
4137c478bd9Sstevel@tonic-gate 	/* The caller gets a chance to mark the callback info as bad */
4147c478bd9Sstevel@tonic-gate 	if (newstate != CB_NOCHANGE)
4157c478bd9Sstevel@tonic-gate 		cbp->cb_state = newstate;
4167c478bd9Sstevel@tonic-gate 	if (newstate == CB_FAILED) {
4177c478bd9Sstevel@tonic-gate 		cbp->cb_timefailed = gethrestime_sec(); /* observability */
4187c478bd9Sstevel@tonic-gate 		cbp->cb_notified_of_cb_path_down = FALSE;
4197c478bd9Sstevel@tonic-gate 	}
4207c478bd9Sstevel@tonic-gate 
4217c478bd9Sstevel@tonic-gate 	cbp->cb_refcnt--;	/* no longer using the information */
4227c478bd9Sstevel@tonic-gate 
4237c478bd9Sstevel@tonic-gate 	/*
4247c478bd9Sstevel@tonic-gate 	 * A thread may be waiting on this one to finish and if so,
4257c478bd9Sstevel@tonic-gate 	 * let it know that it is okay to do the CB_NULL to the
4267c478bd9Sstevel@tonic-gate 	 * client's callback server.
4277c478bd9Sstevel@tonic-gate 	 */
4287c478bd9Sstevel@tonic-gate 	if (cbp->cb_refcnt == 0 && cbp->cb_nullcaller)
4297c478bd9Sstevel@tonic-gate 		cv_broadcast(cbp->cb_cv_nullcaller);
4307c478bd9Sstevel@tonic-gate 
4317c478bd9Sstevel@tonic-gate 	/*
4327c478bd9Sstevel@tonic-gate 	 * If this is the last thread to use the callback info and
4337c478bd9Sstevel@tonic-gate 	 * there is new callback information to try and no thread is
4347c478bd9Sstevel@tonic-gate 	 * there ready to do the CB_NULL, then return true to teh
4357c478bd9Sstevel@tonic-gate 	 * caller so they can do the CB_NULL
4367c478bd9Sstevel@tonic-gate 	 */
4377c478bd9Sstevel@tonic-gate 	if (cbp->cb_refcnt == 0 &&
438ed57cab9Srg 	    cbp->cb_nullcaller == FALSE &&
439ed57cab9Srg 	    cbp->cb_newer.cb_new == TRUE &&
440ed57cab9Srg 	    cbp->cb_newer.cb_confirmed == TRUE)
4417c478bd9Sstevel@tonic-gate 		cb_new = TRUE;
4427c478bd9Sstevel@tonic-gate 
4437c478bd9Sstevel@tonic-gate 	mutex_exit(cbp->cb_lock);
4447c478bd9Sstevel@tonic-gate 
4457c478bd9Sstevel@tonic-gate 	return (cb_new);
4467c478bd9Sstevel@tonic-gate }
4477c478bd9Sstevel@tonic-gate 
4487c478bd9Sstevel@tonic-gate /*
4497c478bd9Sstevel@tonic-gate  * Given the information in the callback info struct, create a client
4507c478bd9Sstevel@tonic-gate  * handle that can be used by the server for its callback path.
4517c478bd9Sstevel@tonic-gate  */
4527c478bd9Sstevel@tonic-gate static CLIENT *
4537c478bd9Sstevel@tonic-gate rfs4_cbch_init(rfs4_cbinfo_t *cbp)
4547c478bd9Sstevel@tonic-gate {
4557c478bd9Sstevel@tonic-gate 	struct knetconfig knc;
4567c478bd9Sstevel@tonic-gate 	vnode_t *vp;
4577c478bd9Sstevel@tonic-gate 	struct sockaddr_in addr4;
4587c478bd9Sstevel@tonic-gate 	struct sockaddr_in6 addr6;
4597c478bd9Sstevel@tonic-gate 	void *addr, *taddr;
4607c478bd9Sstevel@tonic-gate 	in_port_t *pp;
4617c478bd9Sstevel@tonic-gate 	int af;
4627c478bd9Sstevel@tonic-gate 	char *devnam;
4637c478bd9Sstevel@tonic-gate 	struct netbuf nb;
4647c478bd9Sstevel@tonic-gate 	int size;
4657c478bd9Sstevel@tonic-gate 	CLIENT *ch = NULL;
4667c478bd9Sstevel@tonic-gate 	int useresvport = 0;
4677c478bd9Sstevel@tonic-gate 
4687c478bd9Sstevel@tonic-gate 	mutex_enter(cbp->cb_lock);
4697c478bd9Sstevel@tonic-gate 
4707c478bd9Sstevel@tonic-gate 	if (cbp->cb_callback.cb_location.r_netid == NULL ||
4717c478bd9Sstevel@tonic-gate 	    cbp->cb_callback.cb_location.r_addr == NULL) {
4727c478bd9Sstevel@tonic-gate 		goto cb_init_out;
4737c478bd9Sstevel@tonic-gate 	}
4747c478bd9Sstevel@tonic-gate 
4757c478bd9Sstevel@tonic-gate 	if (strcmp(cbp->cb_callback.cb_location.r_netid, "tcp") == 0) {
4767c478bd9Sstevel@tonic-gate 		knc.knc_semantics = NC_TPI_COTS;
4777c478bd9Sstevel@tonic-gate 		knc.knc_protofmly = "inet";
4787c478bd9Sstevel@tonic-gate 		knc.knc_proto = "tcp";
4797c478bd9Sstevel@tonic-gate 		devnam = "/dev/tcp";
4807c478bd9Sstevel@tonic-gate 		af = AF_INET;
4817c478bd9Sstevel@tonic-gate 	} else if (strcmp(cbp->cb_callback.cb_location.r_netid, "udp")
482ed57cab9Srg 	    == 0) {
4837c478bd9Sstevel@tonic-gate 		knc.knc_semantics = NC_TPI_CLTS;
4847c478bd9Sstevel@tonic-gate 		knc.knc_protofmly = "inet";
4857c478bd9Sstevel@tonic-gate 		knc.knc_proto = "udp";
4867c478bd9Sstevel@tonic-gate 		devnam = "/dev/udp";
4877c478bd9Sstevel@tonic-gate 		af = AF_INET;
4887c478bd9Sstevel@tonic-gate 	} else if (strcmp(cbp->cb_callback.cb_location.r_netid, "tcp6")
489ed57cab9Srg 	    == 0) {
4907c478bd9Sstevel@tonic-gate 		knc.knc_semantics = NC_TPI_COTS;
4917c478bd9Sstevel@tonic-gate 		knc.knc_protofmly = "inet6";
4927c478bd9Sstevel@tonic-gate 		knc.knc_proto = "tcp";
4937c478bd9Sstevel@tonic-gate 		devnam = "/dev/tcp6";
4947c478bd9Sstevel@tonic-gate 		af = AF_INET6;
4957c478bd9Sstevel@tonic-gate 	} else if (strcmp(cbp->cb_callback.cb_location.r_netid, "udp6")
496ed57cab9Srg 	    == 0) {
4977c478bd9Sstevel@tonic-gate 		knc.knc_semantics = NC_TPI_CLTS;
4987c478bd9Sstevel@tonic-gate 		knc.knc_protofmly = "inet6";
4997c478bd9Sstevel@tonic-gate 		knc.knc_proto = "udp";
5007c478bd9Sstevel@tonic-gate 		devnam = "/dev/udp6";
5017c478bd9Sstevel@tonic-gate 		af = AF_INET6;
5027c478bd9Sstevel@tonic-gate 	} else {
5037c478bd9Sstevel@tonic-gate 		goto cb_init_out;
5047c478bd9Sstevel@tonic-gate 	}
5057c478bd9Sstevel@tonic-gate 
506c52d60d4Srg 	if (lookupname(devnam, UIO_SYSSPACE, FOLLOW, NULLVPP, &vp) != 0) {
5077c478bd9Sstevel@tonic-gate 
5087c478bd9Sstevel@tonic-gate 		goto cb_init_out;
5097c478bd9Sstevel@tonic-gate 	}
5107c478bd9Sstevel@tonic-gate 
5117c478bd9Sstevel@tonic-gate 	if (vp->v_type != VCHR) {
5127c478bd9Sstevel@tonic-gate 		VN_RELE(vp);
5137c478bd9Sstevel@tonic-gate 		goto cb_init_out;
5147c478bd9Sstevel@tonic-gate 	}
5157c478bd9Sstevel@tonic-gate 
5167c478bd9Sstevel@tonic-gate 	knc.knc_rdev = vp->v_rdev;
5177c478bd9Sstevel@tonic-gate 
5187c478bd9Sstevel@tonic-gate 	VN_RELE(vp);
5197c478bd9Sstevel@tonic-gate 
5207c478bd9Sstevel@tonic-gate 	if (af == AF_INET) {
5217c478bd9Sstevel@tonic-gate 		size = sizeof (addr4);
5227c478bd9Sstevel@tonic-gate 		bzero(&addr4, size);
5237c478bd9Sstevel@tonic-gate 		addr4.sin_family = (sa_family_t)af;
5247c478bd9Sstevel@tonic-gate 		addr = &addr4.sin_addr;
5257c478bd9Sstevel@tonic-gate 		pp = &addr4.sin_port;
5267c478bd9Sstevel@tonic-gate 		taddr = &addr4;
5277c478bd9Sstevel@tonic-gate 	} else /* AF_INET6 */ {
5287c478bd9Sstevel@tonic-gate 		size = sizeof (addr6);
5297c478bd9Sstevel@tonic-gate 		bzero(&addr6, size);
5307c478bd9Sstevel@tonic-gate 		addr6.sin6_family = (sa_family_t)af;
5317c478bd9Sstevel@tonic-gate 		addr = &addr6.sin6_addr;
5327c478bd9Sstevel@tonic-gate 		pp = &addr6.sin6_port;
5337c478bd9Sstevel@tonic-gate 		taddr = &addr6;
5347c478bd9Sstevel@tonic-gate 	}
5357c478bd9Sstevel@tonic-gate 
5367c478bd9Sstevel@tonic-gate 	if (uaddr2sockaddr(af,
537ed57cab9Srg 	    cbp->cb_callback.cb_location.r_addr, addr, pp)) {
5387c478bd9Sstevel@tonic-gate 
5397c478bd9Sstevel@tonic-gate 		goto cb_init_out;
5407c478bd9Sstevel@tonic-gate 	}
5417c478bd9Sstevel@tonic-gate 
5427c478bd9Sstevel@tonic-gate 
5437c478bd9Sstevel@tonic-gate 	nb.maxlen = nb.len = size;
5447c478bd9Sstevel@tonic-gate 	nb.buf = (char *)taddr;
5457c478bd9Sstevel@tonic-gate 
546c52d60d4Srg 	if (clnt_tli_kcreate(&knc, &nb, cbp->cb_callback.cb_program,
5477c478bd9Sstevel@tonic-gate 	    NFS_CB, 0, 0, curthread->t_cred, &ch)) {
5487c478bd9Sstevel@tonic-gate 
5497c478bd9Sstevel@tonic-gate 		ch = NULL;
5507c478bd9Sstevel@tonic-gate 	}
5517c478bd9Sstevel@tonic-gate 
5527c478bd9Sstevel@tonic-gate 	/* turn off reserved port usage */
5537c478bd9Sstevel@tonic-gate 	(void) CLNT_CONTROL(ch, CLSET_BINDRESVPORT, (char *)&useresvport);
5547c478bd9Sstevel@tonic-gate 
5557c478bd9Sstevel@tonic-gate cb_init_out:
5567c478bd9Sstevel@tonic-gate 	mutex_exit(cbp->cb_lock);
5577c478bd9Sstevel@tonic-gate 	return (ch);
5587c478bd9Sstevel@tonic-gate }
5597c478bd9Sstevel@tonic-gate 
5607c478bd9Sstevel@tonic-gate /*
5617c478bd9Sstevel@tonic-gate  * Iterate over the client handle cache and
5627c478bd9Sstevel@tonic-gate  * destroy it.
5637c478bd9Sstevel@tonic-gate  */
5647c478bd9Sstevel@tonic-gate static void
5657c478bd9Sstevel@tonic-gate rfs4_cb_chflush(rfs4_cbinfo_t *cbp)
5667c478bd9Sstevel@tonic-gate {
5677c478bd9Sstevel@tonic-gate 	CLIENT *ch;
5687c478bd9Sstevel@tonic-gate 
5697c478bd9Sstevel@tonic-gate 	while (cbp->cb_chc_free) {
5707c478bd9Sstevel@tonic-gate 		cbp->cb_chc_free--;
5717c478bd9Sstevel@tonic-gate 		ch = cbp->cb_chc[cbp->cb_chc_free];
5727c478bd9Sstevel@tonic-gate 		cbp->cb_chc[cbp->cb_chc_free] = NULL;
5737c478bd9Sstevel@tonic-gate 		if (ch) {
5747c478bd9Sstevel@tonic-gate 			if (ch->cl_auth)
5757c478bd9Sstevel@tonic-gate 				auth_destroy(ch->cl_auth);
5767c478bd9Sstevel@tonic-gate 			clnt_destroy(ch);
5777c478bd9Sstevel@tonic-gate 		}
5787c478bd9Sstevel@tonic-gate 	}
5797c478bd9Sstevel@tonic-gate }
5807c478bd9Sstevel@tonic-gate 
5817c478bd9Sstevel@tonic-gate /*
5827c478bd9Sstevel@tonic-gate  * Return a client handle, either from a the small
5837c478bd9Sstevel@tonic-gate  * rfs4_client_t cache or one that we just created.
5847c478bd9Sstevel@tonic-gate  */
5857c478bd9Sstevel@tonic-gate static CLIENT *
5867c478bd9Sstevel@tonic-gate rfs4_cb_getch(rfs4_cbinfo_t *cbp)
5877c478bd9Sstevel@tonic-gate {
5887c478bd9Sstevel@tonic-gate 	CLIENT *cbch = NULL;
5897c478bd9Sstevel@tonic-gate 	uint32_t zilch = 0;
5907c478bd9Sstevel@tonic-gate 
5917c478bd9Sstevel@tonic-gate 	mutex_enter(cbp->cb_lock);
5927c478bd9Sstevel@tonic-gate 
5937c478bd9Sstevel@tonic-gate 	if (cbp->cb_chc_free) {
5947c478bd9Sstevel@tonic-gate 		cbp->cb_chc_free--;
5957c478bd9Sstevel@tonic-gate 		cbch = cbp->cb_chc[ cbp->cb_chc_free ];
5967c478bd9Sstevel@tonic-gate 		mutex_exit(cbp->cb_lock);
5977c478bd9Sstevel@tonic-gate 		(void) CLNT_CONTROL(cbch, CLSET_XID, (char *)&zilch);
5987c478bd9Sstevel@tonic-gate 		return (cbch);
5997c478bd9Sstevel@tonic-gate 	}
6007c478bd9Sstevel@tonic-gate 
6017c478bd9Sstevel@tonic-gate 	mutex_exit(cbp->cb_lock);
6027c478bd9Sstevel@tonic-gate 
6037c478bd9Sstevel@tonic-gate 	/* none free so make it now */
6047c478bd9Sstevel@tonic-gate 	cbch = rfs4_cbch_init(cbp);
6057c478bd9Sstevel@tonic-gate 
6067c478bd9Sstevel@tonic-gate 	return (cbch);
6077c478bd9Sstevel@tonic-gate }
6087c478bd9Sstevel@tonic-gate 
6097c478bd9Sstevel@tonic-gate /*
6107c478bd9Sstevel@tonic-gate  * Return the client handle to the small cache or
6117c478bd9Sstevel@tonic-gate  * destroy it.
6127c478bd9Sstevel@tonic-gate  */
6137c478bd9Sstevel@tonic-gate static void
6147c478bd9Sstevel@tonic-gate rfs4_cb_freech(rfs4_cbinfo_t *cbp, CLIENT *ch, bool_t lockheld)
6157c478bd9Sstevel@tonic-gate {
6167c478bd9Sstevel@tonic-gate 	if (lockheld == FALSE)
6177c478bd9Sstevel@tonic-gate 		mutex_enter(cbp->cb_lock);
6187c478bd9Sstevel@tonic-gate 
6197c478bd9Sstevel@tonic-gate 	if (cbp->cb_chc_free < RFS4_CBCH_MAX) {
6207c478bd9Sstevel@tonic-gate 		cbp->cb_chc[ cbp->cb_chc_free++ ] = ch;
6217c478bd9Sstevel@tonic-gate 		if (lockheld == FALSE)
6227c478bd9Sstevel@tonic-gate 			mutex_exit(cbp->cb_lock);
6237c478bd9Sstevel@tonic-gate 		return;
6247c478bd9Sstevel@tonic-gate 	}
6257c478bd9Sstevel@tonic-gate 	if (lockheld == FALSE)
6267c478bd9Sstevel@tonic-gate 		mutex_exit(cbp->cb_lock);
6277c478bd9Sstevel@tonic-gate 
6287c478bd9Sstevel@tonic-gate 	/*
6297c478bd9Sstevel@tonic-gate 	 * cache maxed out of free entries, obliterate
6307c478bd9Sstevel@tonic-gate 	 * this client handle, destroy it, throw it away.
6317c478bd9Sstevel@tonic-gate 	 */
6327c478bd9Sstevel@tonic-gate 	if (ch->cl_auth)
6337c478bd9Sstevel@tonic-gate 		auth_destroy(ch->cl_auth);
6347c478bd9Sstevel@tonic-gate 	clnt_destroy(ch);
6357c478bd9Sstevel@tonic-gate }
6367c478bd9Sstevel@tonic-gate 
6377c478bd9Sstevel@tonic-gate /*
6387c478bd9Sstevel@tonic-gate  * With the supplied callback information - initialize the client
6397c478bd9Sstevel@tonic-gate  * callback data.  If there is a callback in progress, save the
6407c478bd9Sstevel@tonic-gate  * callback info so that a thread can pick it up in the future.
6417c478bd9Sstevel@tonic-gate  */
6427c478bd9Sstevel@tonic-gate void
6437c478bd9Sstevel@tonic-gate rfs4_client_setcb(rfs4_client_t *cp, cb_client4 *cb, uint32_t cb_ident)
6447c478bd9Sstevel@tonic-gate {
6457c478bd9Sstevel@tonic-gate 	char *addr = NULL;
6467c478bd9Sstevel@tonic-gate 	char *netid = NULL;
647*d216dff5SRobert Mastors 	rfs4_cbinfo_t *cbp = &cp->rc_cbinfo;
6487c478bd9Sstevel@tonic-gate 	size_t len;
6497c478bd9Sstevel@tonic-gate 
6507c478bd9Sstevel@tonic-gate 	/* Set the call back for the client */
651a31a5cfdSsamf 	if (cb->cb_location.r_addr && cb->cb_location.r_addr[0] != '\0' &&
652a31a5cfdSsamf 	    cb->cb_location.r_netid && cb->cb_location.r_netid[0] != '\0') {
6537c478bd9Sstevel@tonic-gate 		len = strlen(cb->cb_location.r_addr) + 1;
6547c478bd9Sstevel@tonic-gate 		addr = kmem_alloc(len, KM_SLEEP);
6557c478bd9Sstevel@tonic-gate 		bcopy(cb->cb_location.r_addr, addr, len);
6567c478bd9Sstevel@tonic-gate 		len = strlen(cb->cb_location.r_netid) + 1;
6577c478bd9Sstevel@tonic-gate 		netid = kmem_alloc(len, KM_SLEEP);
6587c478bd9Sstevel@tonic-gate 		bcopy(cb->cb_location.r_netid, netid, len);
6597c478bd9Sstevel@tonic-gate 	}
6607c478bd9Sstevel@tonic-gate 	/* ready to save the new information but first free old, if exists */
6617c478bd9Sstevel@tonic-gate 	mutex_enter(cbp->cb_lock);
6627c478bd9Sstevel@tonic-gate 
6637c478bd9Sstevel@tonic-gate 	cbp->cb_newer.cb_callback.cb_program = cb->cb_program;
664a31a5cfdSsamf 
665a31a5cfdSsamf 	if (cbp->cb_newer.cb_callback.cb_location.r_addr != NULL)
666a31a5cfdSsamf 		kmem_free(cbp->cb_newer.cb_callback.cb_location.r_addr,
667a31a5cfdSsamf 		    strlen(cbp->cb_newer.cb_callback.cb_location.r_addr) + 1);
6687c478bd9Sstevel@tonic-gate 	cbp->cb_newer.cb_callback.cb_location.r_addr = addr;
669a31a5cfdSsamf 
670a31a5cfdSsamf 	if (cbp->cb_newer.cb_callback.cb_location.r_netid != NULL)
671a31a5cfdSsamf 		kmem_free(cbp->cb_newer.cb_callback.cb_location.r_netid,
672a31a5cfdSsamf 		    strlen(cbp->cb_newer.cb_callback.cb_location.r_netid) + 1);
6737c478bd9Sstevel@tonic-gate 	cbp->cb_newer.cb_callback.cb_location.r_netid = netid;
6747c478bd9Sstevel@tonic-gate 
6757c478bd9Sstevel@tonic-gate 	cbp->cb_newer.cb_ident = cb_ident;
6767c478bd9Sstevel@tonic-gate 
6777c478bd9Sstevel@tonic-gate 	if (addr && *addr && netid && *netid) {
6787c478bd9Sstevel@tonic-gate 		cbp->cb_newer.cb_new = TRUE;
6797c478bd9Sstevel@tonic-gate 		cbp->cb_newer.cb_confirmed = FALSE;
6807c478bd9Sstevel@tonic-gate 	} else {
6817c478bd9Sstevel@tonic-gate 		cbp->cb_newer.cb_new = FALSE;
6827c478bd9Sstevel@tonic-gate 		cbp->cb_newer.cb_confirmed = FALSE;
6837c478bd9Sstevel@tonic-gate 	}
6847c478bd9Sstevel@tonic-gate 
6857c478bd9Sstevel@tonic-gate 	mutex_exit(cbp->cb_lock);
6867c478bd9Sstevel@tonic-gate }
6877c478bd9Sstevel@tonic-gate 
6887c478bd9Sstevel@tonic-gate /*
6897c478bd9Sstevel@tonic-gate  * The server uses this when processing SETCLIENTID_CONFIRM.  Callback
6907c478bd9Sstevel@tonic-gate  * information may have been provided on SETCLIENTID and this call
6917c478bd9Sstevel@tonic-gate  * marks that information as confirmed and then starts a thread to
6927c478bd9Sstevel@tonic-gate  * test the callback path.
6937c478bd9Sstevel@tonic-gate  */
6947c478bd9Sstevel@tonic-gate void
6957c478bd9Sstevel@tonic-gate rfs4_deleg_cb_check(rfs4_client_t *cp)
6967c478bd9Sstevel@tonic-gate {
697*d216dff5SRobert Mastors 	if (cp->rc_cbinfo.cb_newer.cb_new == FALSE)
6987c478bd9Sstevel@tonic-gate 		return;
6997c478bd9Sstevel@tonic-gate 
700*d216dff5SRobert Mastors 	cp->rc_cbinfo.cb_newer.cb_confirmed = TRUE;
7017c478bd9Sstevel@tonic-gate 
702*d216dff5SRobert Mastors 	rfs4_dbe_hold(cp->rc_dbe); /* hold the client struct for thread */
7037c478bd9Sstevel@tonic-gate 
7047c478bd9Sstevel@tonic-gate 	(void) thread_create(NULL, 0, rfs4_do_cb_null, cp, 0, &p0, TS_RUN,
705ed57cab9Srg 	    minclsyspri);
7067c478bd9Sstevel@tonic-gate }
7077c478bd9Sstevel@tonic-gate 
7087c478bd9Sstevel@tonic-gate static void
7097c478bd9Sstevel@tonic-gate rfs4args_cb_recall_free(nfs_cb_argop4 *argop)
7107c478bd9Sstevel@tonic-gate {
7117c478bd9Sstevel@tonic-gate 	CB_RECALL4args	*rec_argp;
7127c478bd9Sstevel@tonic-gate 
7137c478bd9Sstevel@tonic-gate 	rec_argp = &argop->nfs_cb_argop4_u.opcbrecall;
7147c478bd9Sstevel@tonic-gate 	if (rec_argp->fh.nfs_fh4_val)
7157c478bd9Sstevel@tonic-gate 		kmem_free(rec_argp->fh.nfs_fh4_val, rec_argp->fh.nfs_fh4_len);
7167c478bd9Sstevel@tonic-gate }
7177c478bd9Sstevel@tonic-gate 
7187c478bd9Sstevel@tonic-gate /* ARGSUSED */
7197c478bd9Sstevel@tonic-gate static void
7207c478bd9Sstevel@tonic-gate rfs4args_cb_getattr_free(nfs_cb_argop4 *argop)
7217c478bd9Sstevel@tonic-gate {
7227c478bd9Sstevel@tonic-gate 	CB_GETATTR4args *argp;
7237c478bd9Sstevel@tonic-gate 
7247c478bd9Sstevel@tonic-gate 	argp = &argop->nfs_cb_argop4_u.opcbgetattr;
7257c478bd9Sstevel@tonic-gate 	if (argp->fh.nfs_fh4_val)
7267c478bd9Sstevel@tonic-gate 		kmem_free(argp->fh.nfs_fh4_val, argp->fh.nfs_fh4_len);
7277c478bd9Sstevel@tonic-gate }
7287c478bd9Sstevel@tonic-gate 
7297c478bd9Sstevel@tonic-gate static void
7307c478bd9Sstevel@tonic-gate rfs4freeargres(CB_COMPOUND4args *args, CB_COMPOUND4res *resp)
7317c478bd9Sstevel@tonic-gate {
7327c478bd9Sstevel@tonic-gate 	int i, arglen;
7337c478bd9Sstevel@tonic-gate 	nfs_cb_argop4 *argop;
7347c478bd9Sstevel@tonic-gate 
7357c478bd9Sstevel@tonic-gate 	/*
7367c478bd9Sstevel@tonic-gate 	 * First free any special args alloc'd for specific ops.
7377c478bd9Sstevel@tonic-gate 	 */
7387c478bd9Sstevel@tonic-gate 	arglen = args->array_len;
7397c478bd9Sstevel@tonic-gate 	argop = args->array;
7407c478bd9Sstevel@tonic-gate 	for (i = 0; i < arglen; i++, argop++) {
7417c478bd9Sstevel@tonic-gate 
7427c478bd9Sstevel@tonic-gate 		switch (argop->argop) {
7437c478bd9Sstevel@tonic-gate 		case OP_CB_RECALL:
7447c478bd9Sstevel@tonic-gate 			rfs4args_cb_recall_free(argop);
7457c478bd9Sstevel@tonic-gate 			break;
7467c478bd9Sstevel@tonic-gate 
7477c478bd9Sstevel@tonic-gate 		case OP_CB_GETATTR:
7487c478bd9Sstevel@tonic-gate 			rfs4args_cb_getattr_free(argop);
7497c478bd9Sstevel@tonic-gate 			break;
7507c478bd9Sstevel@tonic-gate 
7517c478bd9Sstevel@tonic-gate 		default:
7527c478bd9Sstevel@tonic-gate 			return;
7537c478bd9Sstevel@tonic-gate 		}
7547c478bd9Sstevel@tonic-gate 	}
7557c478bd9Sstevel@tonic-gate 
7567c478bd9Sstevel@tonic-gate 	if (args->tag.utf8string_len > 0)
7577c478bd9Sstevel@tonic-gate 		UTF8STRING_FREE(args->tag)
7587c478bd9Sstevel@tonic-gate 
7597c478bd9Sstevel@tonic-gate 	kmem_free(args->array, arglen * sizeof (nfs_cb_argop4));
7607c478bd9Sstevel@tonic-gate 	if (resp)
7617c478bd9Sstevel@tonic-gate 		(void) xdr_free(xdr_CB_COMPOUND4res, (caddr_t)resp);
7627c478bd9Sstevel@tonic-gate }
7637c478bd9Sstevel@tonic-gate 
7647c478bd9Sstevel@tonic-gate /*
7657c478bd9Sstevel@tonic-gate  * General callback routine for the server to the client.
7667c478bd9Sstevel@tonic-gate  */
7677c478bd9Sstevel@tonic-gate static enum clnt_stat
768*d216dff5SRobert Mastors rfs4_do_callback(rfs4_client_t *cp, CB_COMPOUND4args *args,
769*d216dff5SRobert Mastors     CB_COMPOUND4res *res, struct timeval timeout)
7707c478bd9Sstevel@tonic-gate {
7717c478bd9Sstevel@tonic-gate 	rfs4_cbinfo_t *cbp;
7727c478bd9Sstevel@tonic-gate 	CLIENT *ch;
7737c478bd9Sstevel@tonic-gate 	/* start with this in case cb_getch() fails */
7747c478bd9Sstevel@tonic-gate 	enum clnt_stat	stat = RPC_FAILED;
7757c478bd9Sstevel@tonic-gate 
7767c478bd9Sstevel@tonic-gate 	res->tag.utf8string_val = NULL;
7777c478bd9Sstevel@tonic-gate 	res->array = NULL;
7787c478bd9Sstevel@tonic-gate 
7797c478bd9Sstevel@tonic-gate retry:
7807c478bd9Sstevel@tonic-gate 	cbp = rfs4_cbinfo_hold(cp);
7817c478bd9Sstevel@tonic-gate 	if (cbp == NULL)
7827c478bd9Sstevel@tonic-gate 		return (stat);
7837c478bd9Sstevel@tonic-gate 
7847c478bd9Sstevel@tonic-gate 	/* get a client handle */
7857c478bd9Sstevel@tonic-gate 	if ((ch = rfs4_cb_getch(cbp)) != NULL) {
7867c478bd9Sstevel@tonic-gate 		/*
7877c478bd9Sstevel@tonic-gate 		 * reset the cb_ident since it may have changed in
7887c478bd9Sstevel@tonic-gate 		 * rfs4_cbinfo_hold()
7897c478bd9Sstevel@tonic-gate 		 */
7907c478bd9Sstevel@tonic-gate 		args->callback_ident = cbp->cb_ident;
7917c478bd9Sstevel@tonic-gate 
792eac3aab7Srobinson 		stat = clnt_call(ch, CB_COMPOUND, xdr_CB_COMPOUND4args_srv,
793ed57cab9Srg 		    (caddr_t)args, xdr_CB_COMPOUND4res,
794ed57cab9Srg 		    (caddr_t)res, timeout);
7957c478bd9Sstevel@tonic-gate 
7967c478bd9Sstevel@tonic-gate 		/* free client handle */
7977c478bd9Sstevel@tonic-gate 		rfs4_cb_freech(cbp, ch, FALSE);
7987c478bd9Sstevel@tonic-gate 	}
7997c478bd9Sstevel@tonic-gate 
8007c478bd9Sstevel@tonic-gate 	/*
8017c478bd9Sstevel@tonic-gate 	 * If the rele says that there may be new callback info then
8027c478bd9Sstevel@tonic-gate 	 * retry this sequence and it may succeed as a result of the
8037c478bd9Sstevel@tonic-gate 	 * new callback path
8047c478bd9Sstevel@tonic-gate 	 */
8057c478bd9Sstevel@tonic-gate 	if (rfs4_cbinfo_rele(cbp,
806ed57cab9Srg 	    (stat == RPC_SUCCESS ? CB_NOCHANGE : CB_FAILED)) == TRUE)
8077c478bd9Sstevel@tonic-gate 		goto retry;
8087c478bd9Sstevel@tonic-gate 
8097c478bd9Sstevel@tonic-gate 	return (stat);
8107c478bd9Sstevel@tonic-gate }
8117c478bd9Sstevel@tonic-gate 
8127c478bd9Sstevel@tonic-gate /*
8137c478bd9Sstevel@tonic-gate  * Used by the NFSv4 server to get attributes for a file while
8147c478bd9Sstevel@tonic-gate  * handling the case where a file has been write delegated.  For the
8157c478bd9Sstevel@tonic-gate  * time being, VOP_GETATTR() is called and CB_GETATTR processing is
8167c478bd9Sstevel@tonic-gate  * not undertaken.  This call site is maintained in case the server is
8177c478bd9Sstevel@tonic-gate  * updated in the future to handle write delegation space guarantees.
8187c478bd9Sstevel@tonic-gate  */
8197c478bd9Sstevel@tonic-gate nfsstat4
8207c478bd9Sstevel@tonic-gate rfs4_vop_getattr(vnode_t *vp, vattr_t *vap, int flag, cred_t *cr)
8217c478bd9Sstevel@tonic-gate {
82200ba712dSGarima Tripathi 
8237c478bd9Sstevel@tonic-gate 	int error;
8247c478bd9Sstevel@tonic-gate 
825da6c28aaSamw 	error = VOP_GETATTR(vp, vap, flag, cr, NULL);
8267c478bd9Sstevel@tonic-gate 	return (puterrno4(error));
8277c478bd9Sstevel@tonic-gate }
8287c478bd9Sstevel@tonic-gate 
8297c478bd9Sstevel@tonic-gate /*
8307c478bd9Sstevel@tonic-gate  * This is used everywhere in the v2/v3 server to allow the
8317c478bd9Sstevel@tonic-gate  * integration of all NFS versions and the support of delegation.  For
8327c478bd9Sstevel@tonic-gate  * now, just call the VOP_GETATTR().  If the NFSv4 server is enhanced
8337c478bd9Sstevel@tonic-gate  * in the future to provide space guarantees for write delegations
8347c478bd9Sstevel@tonic-gate  * then this call site should be expanded to interact with the client.
8357c478bd9Sstevel@tonic-gate  */
8367c478bd9Sstevel@tonic-gate int
8377c478bd9Sstevel@tonic-gate rfs4_delegated_getattr(vnode_t *vp, vattr_t *vap, int flag, cred_t *cr)
8387c478bd9Sstevel@tonic-gate {
839da6c28aaSamw 	return (VOP_GETATTR(vp, vap, flag, cr, NULL));
8407c478bd9Sstevel@tonic-gate }
8417c478bd9Sstevel@tonic-gate 
8427c478bd9Sstevel@tonic-gate /*
8437c478bd9Sstevel@tonic-gate  * Place the actual cb_recall otw call to client.
8447c478bd9Sstevel@tonic-gate  */
8457c478bd9Sstevel@tonic-gate static void
8467c478bd9Sstevel@tonic-gate rfs4_do_cb_recall(rfs4_deleg_state_t *dsp, bool_t trunc)
8477c478bd9Sstevel@tonic-gate {
8487c478bd9Sstevel@tonic-gate 	CB_COMPOUND4args	cb4_args;
8497c478bd9Sstevel@tonic-gate 	CB_COMPOUND4res		cb4_res;
8507c478bd9Sstevel@tonic-gate 	CB_RECALL4args		*rec_argp;
851f3b585ceSsamf 	CB_RECALL4res		*rec_resp;
8527c478bd9Sstevel@tonic-gate 	nfs_cb_argop4		*argop;
8537c478bd9Sstevel@tonic-gate 	int			numops;
8547c478bd9Sstevel@tonic-gate 	int			argoplist_size;
8557c478bd9Sstevel@tonic-gate 	struct timeval		timeout;
8567c478bd9Sstevel@tonic-gate 	nfs_fh4			*fhp;
8577c478bd9Sstevel@tonic-gate 	enum clnt_stat		call_stat;
8587c478bd9Sstevel@tonic-gate 
8597c478bd9Sstevel@tonic-gate 	/*
8607c478bd9Sstevel@tonic-gate 	 * set up the compound args
8617c478bd9Sstevel@tonic-gate 	 */
8627c478bd9Sstevel@tonic-gate 	numops = 1;	/* CB_RECALL only */
8637c478bd9Sstevel@tonic-gate 
8647c478bd9Sstevel@tonic-gate 	argoplist_size = numops * sizeof (nfs_cb_argop4);
8657c478bd9Sstevel@tonic-gate 	argop = kmem_zalloc(argoplist_size, KM_SLEEP);
8667c478bd9Sstevel@tonic-gate 	argop->argop = OP_CB_RECALL;
8677c478bd9Sstevel@tonic-gate 	rec_argp = &argop->nfs_cb_argop4_u.opcbrecall;
8687c478bd9Sstevel@tonic-gate 
8697c478bd9Sstevel@tonic-gate 	(void) str_to_utf8("cb_recall", &cb4_args.tag);
8707c478bd9Sstevel@tonic-gate 	cb4_args.minorversion = CB4_MINORVERSION;
8717c478bd9Sstevel@tonic-gate 	/* cb4_args.callback_ident is set in rfs4_do_callback() */
8727c478bd9Sstevel@tonic-gate 	cb4_args.array_len = numops;
8737c478bd9Sstevel@tonic-gate 	cb4_args.array = argop;
8747c478bd9Sstevel@tonic-gate 
8757c478bd9Sstevel@tonic-gate 	/*
8767c478bd9Sstevel@tonic-gate 	 * fill in the args struct
8777c478bd9Sstevel@tonic-gate 	 */
878*d216dff5SRobert Mastors 	bcopy(&dsp->rds_delegid.stateid, &rec_argp->stateid, sizeof (stateid4));
8797c478bd9Sstevel@tonic-gate 	rec_argp->truncate = trunc;
8807c478bd9Sstevel@tonic-gate 
881*d216dff5SRobert Mastors 	fhp = &dsp->rds_finfo->rf_filehandle;
8827c478bd9Sstevel@tonic-gate 	rec_argp->fh.nfs_fh4_val = kmem_alloc(sizeof (char) *
883ed57cab9Srg 	    fhp->nfs_fh4_len, KM_SLEEP);
8847c478bd9Sstevel@tonic-gate 	nfs_fh4_copy(fhp, &rec_argp->fh);
8857c478bd9Sstevel@tonic-gate 
8867c478bd9Sstevel@tonic-gate 	/* Keep track of when we did this for observability */
887*d216dff5SRobert Mastors 	dsp->rds_time_recalled = gethrestime_sec();
8887c478bd9Sstevel@tonic-gate 
8897c478bd9Sstevel@tonic-gate 	/*
8907c478bd9Sstevel@tonic-gate 	 * Set up the timeout for the callback and make the actual call.
8917c478bd9Sstevel@tonic-gate 	 * Timeout will be 80% of the lease period for this server.
8927c478bd9Sstevel@tonic-gate 	 */
8937c478bd9Sstevel@tonic-gate 	timeout.tv_sec = (rfs4_lease_time * 80) / 100;
8947c478bd9Sstevel@tonic-gate 	timeout.tv_usec = 0;
8957c478bd9Sstevel@tonic-gate 
896*d216dff5SRobert Mastors 	DTRACE_NFSV4_3(cb__recall__start, rfs4_client_t *, dsp->rds_client,
897f3b585ceSsamf 	    rfs4_deleg_state_t *, dsp, CB_RECALL4args *, rec_argp);
898f3b585ceSsamf 
899*d216dff5SRobert Mastors 	call_stat = rfs4_do_callback(dsp->rds_client, &cb4_args, &cb4_res,
900*d216dff5SRobert Mastors 	    timeout);
9017c478bd9Sstevel@tonic-gate 
902f3b585ceSsamf 	rec_resp = (cb4_res.array_len == 0) ? NULL :
903f3b585ceSsamf 	    &cb4_res.array[0].nfs_cb_resop4_u.opcbrecall;
904f3b585ceSsamf 
905*d216dff5SRobert Mastors 	DTRACE_NFSV4_3(cb__recall__done, rfs4_client_t *, dsp->rds_client,
906f3b585ceSsamf 	    rfs4_deleg_state_t *, dsp, CB_RECALL4res *, rec_resp);
907f3b585ceSsamf 
9087c478bd9Sstevel@tonic-gate 	if (call_stat != RPC_SUCCESS || cb4_res.status != NFS4_OK) {
909*d216dff5SRobert Mastors 		rfs4_return_deleg(dsp, TRUE);
9107c478bd9Sstevel@tonic-gate 	}
9117c478bd9Sstevel@tonic-gate 
9127c478bd9Sstevel@tonic-gate 	rfs4freeargres(&cb4_args, &cb4_res);
9137c478bd9Sstevel@tonic-gate }
9147c478bd9Sstevel@tonic-gate 
9157c478bd9Sstevel@tonic-gate struct recall_arg {
9167c478bd9Sstevel@tonic-gate 	rfs4_deleg_state_t *dsp;
9177c478bd9Sstevel@tonic-gate 	void (*recall)(rfs4_deleg_state_t *, bool_t trunc);
9187c478bd9Sstevel@tonic-gate 	bool_t trunc;
9197c478bd9Sstevel@tonic-gate };
9207c478bd9Sstevel@tonic-gate 
9217c478bd9Sstevel@tonic-gate static void
9227c478bd9Sstevel@tonic-gate do_recall(struct recall_arg *arg)
9237c478bd9Sstevel@tonic-gate {
9247c478bd9Sstevel@tonic-gate 	rfs4_deleg_state_t *dsp = arg->dsp;
925*d216dff5SRobert Mastors 	rfs4_file_t *fp = dsp->rds_finfo;
9267c478bd9Sstevel@tonic-gate 	callb_cpr_t cpr_info;
9277c478bd9Sstevel@tonic-gate 	kmutex_t cpr_lock;
9287c478bd9Sstevel@tonic-gate 
9297c478bd9Sstevel@tonic-gate 	mutex_init(&cpr_lock, NULL, MUTEX_DEFAULT, NULL);
9307c478bd9Sstevel@tonic-gate 	CALLB_CPR_INIT(&cpr_info, &cpr_lock, callb_generic_cpr, "nfsv4Recall");
9317c478bd9Sstevel@tonic-gate 
9327c478bd9Sstevel@tonic-gate 	/*
9337c478bd9Sstevel@tonic-gate 	 * It is possible that before this thread starts
9347c478bd9Sstevel@tonic-gate 	 * the client has send us a return_delegation, and
9357c478bd9Sstevel@tonic-gate 	 * if that is the case we do not need to send the
9367c478bd9Sstevel@tonic-gate 	 * recall callback.
9377c478bd9Sstevel@tonic-gate 	 */
938*d216dff5SRobert Mastors 	if (dsp->rds_dtype != OPEN_DELEGATE_NONE) {
939ed57cab9Srg 		DTRACE_PROBE3(nfss__i__recall,
940ed57cab9Srg 		    struct recall_arg *, arg,
941ed57cab9Srg 		    struct rfs4_deleg_state_t *, dsp,
942ed57cab9Srg 		    struct rfs4_file_t *, fp);
9437c478bd9Sstevel@tonic-gate 
9447c478bd9Sstevel@tonic-gate 		if (arg->recall)
9457c478bd9Sstevel@tonic-gate 			(void) (*arg->recall)(dsp, arg->trunc);
9467c478bd9Sstevel@tonic-gate 	}
9477c478bd9Sstevel@tonic-gate 
948*d216dff5SRobert Mastors 	mutex_enter(fp->rf_dinfo.rd_recall_lock);
9497c478bd9Sstevel@tonic-gate 	/*
9507c478bd9Sstevel@tonic-gate 	 * Recall count may go negative if the parent thread that is
9517c478bd9Sstevel@tonic-gate 	 * creating the individual callback threads does not modify
9527c478bd9Sstevel@tonic-gate 	 * the recall_count field before the callback thread actually
9537c478bd9Sstevel@tonic-gate 	 * gets a response from the CB_RECALL
9547c478bd9Sstevel@tonic-gate 	 */
955*d216dff5SRobert Mastors 	fp->rf_dinfo.rd_recall_count--;
956*d216dff5SRobert Mastors 	if (fp->rf_dinfo.rd_recall_count == 0)
957*d216dff5SRobert Mastors 		cv_signal(fp->rf_dinfo.rd_recall_cv);
958*d216dff5SRobert Mastors 	mutex_exit(fp->rf_dinfo.rd_recall_lock);
9597c478bd9Sstevel@tonic-gate 
9607c478bd9Sstevel@tonic-gate 	mutex_enter(&cpr_lock);
9617c478bd9Sstevel@tonic-gate 	CALLB_CPR_EXIT(&cpr_info);
9627c478bd9Sstevel@tonic-gate 	mutex_destroy(&cpr_lock);
9637c478bd9Sstevel@tonic-gate 
9647c478bd9Sstevel@tonic-gate 	rfs4_deleg_state_rele(dsp); /* release the hold for this thread */
9657c478bd9Sstevel@tonic-gate 
9667c478bd9Sstevel@tonic-gate 	kmem_free(arg, sizeof (struct recall_arg));
9677c478bd9Sstevel@tonic-gate }
9687c478bd9Sstevel@tonic-gate 
9697c478bd9Sstevel@tonic-gate struct master_recall_args {
9707c478bd9Sstevel@tonic-gate     rfs4_file_t *fp;
9717c478bd9Sstevel@tonic-gate     void (*recall)(rfs4_deleg_state_t *, bool_t);
9727c478bd9Sstevel@tonic-gate     bool_t trunc;
9737c478bd9Sstevel@tonic-gate };
9747c478bd9Sstevel@tonic-gate 
9757c478bd9Sstevel@tonic-gate static void
9767c478bd9Sstevel@tonic-gate do_recall_file(struct master_recall_args *map)
9777c478bd9Sstevel@tonic-gate {
9787c478bd9Sstevel@tonic-gate 	rfs4_file_t *fp = map->fp;
9797c478bd9Sstevel@tonic-gate 	rfs4_deleg_state_t *dsp;
9807c478bd9Sstevel@tonic-gate 	struct recall_arg *arg;
9817c478bd9Sstevel@tonic-gate 	callb_cpr_t cpr_info;
9827c478bd9Sstevel@tonic-gate 	kmutex_t cpr_lock;
9837c478bd9Sstevel@tonic-gate 	int32_t recall_count;
9847c478bd9Sstevel@tonic-gate 
985*d216dff5SRobert Mastors 	rfs4_dbe_lock(fp->rf_dbe);
986ed57cab9Srg 
987ed57cab9Srg 	/* Recall already in progress ? */
988*d216dff5SRobert Mastors 	mutex_enter(fp->rf_dinfo.rd_recall_lock);
989*d216dff5SRobert Mastors 	if (fp->rf_dinfo.rd_recall_count != 0) {
990*d216dff5SRobert Mastors 		mutex_exit(fp->rf_dinfo.rd_recall_lock);
991*d216dff5SRobert Mastors 		rfs4_dbe_rele_nolock(fp->rf_dbe);
992*d216dff5SRobert Mastors 		rfs4_dbe_unlock(fp->rf_dbe);
9937c478bd9Sstevel@tonic-gate 		kmem_free(map, sizeof (struct master_recall_args));
9947c478bd9Sstevel@tonic-gate 		return;
9957c478bd9Sstevel@tonic-gate 	}
9967c478bd9Sstevel@tonic-gate 
997*d216dff5SRobert Mastors 	mutex_exit(fp->rf_dinfo.rd_recall_lock);
998ed57cab9Srg 
9997c478bd9Sstevel@tonic-gate 	mutex_init(&cpr_lock, NULL, MUTEX_DEFAULT, NULL);
1000ed57cab9Srg 	CALLB_CPR_INIT(&cpr_info, &cpr_lock, callb_generic_cpr,	"v4RecallFile");
10017c478bd9Sstevel@tonic-gate 
10027c478bd9Sstevel@tonic-gate 	recall_count = 0;
1003*d216dff5SRobert Mastors 	for (dsp = list_head(&fp->rf_delegstatelist); dsp != NULL;
1004*d216dff5SRobert Mastors 	    dsp = list_next(&fp->rf_delegstatelist, dsp)) {
1005ed57cab9Srg 
1006*d216dff5SRobert Mastors 		rfs4_dbe_lock(dsp->rds_dbe);
1007ed57cab9Srg 		/*
1008ed57cab9Srg 		 * if this delegation state
1009ed57cab9Srg 		 * is being reaped skip it
1010ed57cab9Srg 		 */
1011*d216dff5SRobert Mastors 		if (rfs4_dbe_is_invalid(dsp->rds_dbe)) {
1012*d216dff5SRobert Mastors 			rfs4_dbe_unlock(dsp->rds_dbe);
1013ed57cab9Srg 			continue;
1014ed57cab9Srg 		}
1015ed57cab9Srg 
1016ed57cab9Srg 		/* hold for receiving thread */
1017*d216dff5SRobert Mastors 		rfs4_dbe_hold(dsp->rds_dbe);
1018*d216dff5SRobert Mastors 		rfs4_dbe_unlock(dsp->rds_dbe);
1019ed57cab9Srg 
10207c478bd9Sstevel@tonic-gate 		arg = kmem_alloc(sizeof (struct recall_arg), KM_SLEEP);
10217c478bd9Sstevel@tonic-gate 		arg->recall = map->recall;
10227c478bd9Sstevel@tonic-gate 		arg->trunc = map->trunc;
10237c478bd9Sstevel@tonic-gate 		arg->dsp = dsp;
10247c478bd9Sstevel@tonic-gate 
10257c478bd9Sstevel@tonic-gate 		recall_count++;
10267c478bd9Sstevel@tonic-gate 
10277c478bd9Sstevel@tonic-gate 		(void) thread_create(NULL, 0, do_recall, arg, 0, &p0, TS_RUN,
1028ed57cab9Srg 		    minclsyspri);
10297c478bd9Sstevel@tonic-gate 	}
1030ed57cab9Srg 
1031*d216dff5SRobert Mastors 	rfs4_dbe_unlock(fp->rf_dbe);
10327c478bd9Sstevel@tonic-gate 
1033*d216dff5SRobert Mastors 	mutex_enter(fp->rf_dinfo.rd_recall_lock);
10347c478bd9Sstevel@tonic-gate 	/*
10357c478bd9Sstevel@tonic-gate 	 * Recall count may go negative if the parent thread that is
10367c478bd9Sstevel@tonic-gate 	 * creating the individual callback threads does not modify
10377c478bd9Sstevel@tonic-gate 	 * the recall_count field before the callback thread actually
10387c478bd9Sstevel@tonic-gate 	 * gets a response from the CB_RECALL
10397c478bd9Sstevel@tonic-gate 	 */
1040*d216dff5SRobert Mastors 	fp->rf_dinfo.rd_recall_count += recall_count;
1041*d216dff5SRobert Mastors 	while (fp->rf_dinfo.rd_recall_count)
1042*d216dff5SRobert Mastors 		cv_wait(fp->rf_dinfo.rd_recall_cv, fp->rf_dinfo.rd_recall_lock);
10437c478bd9Sstevel@tonic-gate 
1044*d216dff5SRobert Mastors 	mutex_exit(fp->rf_dinfo.rd_recall_lock);
10457c478bd9Sstevel@tonic-gate 
1046ed57cab9Srg 	DTRACE_PROBE1(nfss__i__recall_done, rfs4_file_t *, fp);
10477c478bd9Sstevel@tonic-gate 	rfs4_file_rele(fp);
10487c478bd9Sstevel@tonic-gate 	kmem_free(map, sizeof (struct master_recall_args));
10497c478bd9Sstevel@tonic-gate 	mutex_enter(&cpr_lock);
10507c478bd9Sstevel@tonic-gate 	CALLB_CPR_EXIT(&cpr_info);
10517c478bd9Sstevel@tonic-gate 	mutex_destroy(&cpr_lock);
10527c478bd9Sstevel@tonic-gate }
10537c478bd9Sstevel@tonic-gate 
10547c478bd9Sstevel@tonic-gate static void
10557c478bd9Sstevel@tonic-gate rfs4_recall_file(rfs4_file_t *fp,
1056*d216dff5SRobert Mastors     void (*recall)(rfs4_deleg_state_t *, bool_t trunc),
1057*d216dff5SRobert Mastors     bool_t trunc, rfs4_client_t *cp)
10587c478bd9Sstevel@tonic-gate {
10597c478bd9Sstevel@tonic-gate 	struct master_recall_args *args;
10607c478bd9Sstevel@tonic-gate 
1061*d216dff5SRobert Mastors 	rfs4_dbe_lock(fp->rf_dbe);
1062*d216dff5SRobert Mastors 	if (fp->rf_dinfo.rd_dtype == OPEN_DELEGATE_NONE) {
1063*d216dff5SRobert Mastors 		rfs4_dbe_unlock(fp->rf_dbe);
10647c478bd9Sstevel@tonic-gate 		return;
10657c478bd9Sstevel@tonic-gate 	}
1066*d216dff5SRobert Mastors 	rfs4_dbe_hold(fp->rf_dbe);	/* hold for new thread */
10677c478bd9Sstevel@tonic-gate 
10687c478bd9Sstevel@tonic-gate 	/*
10697c478bd9Sstevel@tonic-gate 	 * Mark the time we started the recall processing.
10707c478bd9Sstevel@tonic-gate 	 * If it has been previously recalled, do not reset the
10717c478bd9Sstevel@tonic-gate 	 * timer since this is used for the revocation decision.
10727c478bd9Sstevel@tonic-gate 	 */
1073*d216dff5SRobert Mastors 	if (fp->rf_dinfo.rd_time_recalled == 0)
1074*d216dff5SRobert Mastors 		fp->rf_dinfo.rd_time_recalled = gethrestime_sec();
1075*d216dff5SRobert Mastors 	fp->rf_dinfo.rd_ever_recalled = TRUE; /* used for policy decision */
10767c478bd9Sstevel@tonic-gate 	/* Client causing recall not always available */
10777c478bd9Sstevel@tonic-gate 	if (cp)
1078*d216dff5SRobert Mastors 		fp->rf_dinfo.rd_conflicted_client = cp->rc_clientid;
10797c478bd9Sstevel@tonic-gate 
1080*d216dff5SRobert Mastors 	rfs4_dbe_unlock(fp->rf_dbe);
10817c478bd9Sstevel@tonic-gate 
10827c478bd9Sstevel@tonic-gate 	args = kmem_alloc(sizeof (struct master_recall_args), KM_SLEEP);
10837c478bd9Sstevel@tonic-gate 	args->fp = fp;
10847c478bd9Sstevel@tonic-gate 	args->recall = recall;
10857c478bd9Sstevel@tonic-gate 	args->trunc = trunc;
10867c478bd9Sstevel@tonic-gate 
10877c478bd9Sstevel@tonic-gate 	(void) thread_create(NULL, 0, do_recall_file, args, 0, &p0, TS_RUN,
1088ed57cab9Srg 	    minclsyspri);
10897c478bd9Sstevel@tonic-gate }
10907c478bd9Sstevel@tonic-gate 
10917c478bd9Sstevel@tonic-gate void
10927c478bd9Sstevel@tonic-gate rfs4_recall_deleg(rfs4_file_t *fp, bool_t trunc, rfs4_client_t *cp)
10937c478bd9Sstevel@tonic-gate {
10947c478bd9Sstevel@tonic-gate 	time_t elapsed1, elapsed2;
10957c478bd9Sstevel@tonic-gate 
1096*d216dff5SRobert Mastors 	if (fp->rf_dinfo.rd_time_recalled != 0) {
1097*d216dff5SRobert Mastors 		elapsed1 = gethrestime_sec() - fp->rf_dinfo.rd_time_recalled;
1098*d216dff5SRobert Mastors 		elapsed2 = gethrestime_sec() - fp->rf_dinfo.rd_time_lastwrite;
10997c478bd9Sstevel@tonic-gate 		/* First check to see if a revocation should occur */
11007c478bd9Sstevel@tonic-gate 		if (elapsed1 > rfs4_lease_time &&
1101ed57cab9Srg 		    elapsed2 > rfs4_lease_time) {
11027c478bd9Sstevel@tonic-gate 			rfs4_revoke_file(fp);
11037c478bd9Sstevel@tonic-gate 			return;
11047c478bd9Sstevel@tonic-gate 		}
11057c478bd9Sstevel@tonic-gate 		/*
11067c478bd9Sstevel@tonic-gate 		 * Next check to see if a recall should be done again
11077c478bd9Sstevel@tonic-gate 		 * so quickly.
11087c478bd9Sstevel@tonic-gate 		 */
11097c478bd9Sstevel@tonic-gate 		if (elapsed1 <= ((rfs4_lease_time * 20) / 100))
11107c478bd9Sstevel@tonic-gate 			return;
11117c478bd9Sstevel@tonic-gate 	}
11127c478bd9Sstevel@tonic-gate 	rfs4_recall_file(fp, rfs4_do_cb_recall, trunc, cp);
11137c478bd9Sstevel@tonic-gate }
11147c478bd9Sstevel@tonic-gate 
11157c478bd9Sstevel@tonic-gate /*
11167c478bd9Sstevel@tonic-gate  * rfs4_check_recall is called from rfs4_do_open to determine if the current
11177c478bd9Sstevel@tonic-gate  * open conflicts with the delegation.
11187c478bd9Sstevel@tonic-gate  * Return true if we need recall otherwise false.
1119*d216dff5SRobert Mastors  * Assumes entry locks for sp and sp->rs_finfo are held.
11207c478bd9Sstevel@tonic-gate  */
11217c478bd9Sstevel@tonic-gate bool_t
11227c478bd9Sstevel@tonic-gate rfs4_check_recall(rfs4_state_t *sp, uint32_t access)
11237c478bd9Sstevel@tonic-gate {
1124*d216dff5SRobert Mastors 	open_delegation_type4 dtype = sp->rs_finfo->rf_dinfo.rd_dtype;
11257c478bd9Sstevel@tonic-gate 
11267c478bd9Sstevel@tonic-gate 	switch (dtype) {
11277c478bd9Sstevel@tonic-gate 	case OPEN_DELEGATE_NONE:
11287c478bd9Sstevel@tonic-gate 		/* Not currently delegated so there is nothing to do */
11297c478bd9Sstevel@tonic-gate 		return (FALSE);
11307c478bd9Sstevel@tonic-gate 	case OPEN_DELEGATE_READ:
11317c478bd9Sstevel@tonic-gate 		/*
11327c478bd9Sstevel@tonic-gate 		 * If the access is only asking for READ then there is
11337c478bd9Sstevel@tonic-gate 		 * no conflict and nothing to do.  If it is asking
11347c478bd9Sstevel@tonic-gate 		 * for write, then there will be conflict and the read
11357c478bd9Sstevel@tonic-gate 		 * delegation should be recalled.
11367c478bd9Sstevel@tonic-gate 		 */
11377c478bd9Sstevel@tonic-gate 		if (access == OPEN4_SHARE_ACCESS_READ)
11387c478bd9Sstevel@tonic-gate 			return (FALSE);
11397c478bd9Sstevel@tonic-gate 		else
11407c478bd9Sstevel@tonic-gate 			return (TRUE);
11417c478bd9Sstevel@tonic-gate 	case OPEN_DELEGATE_WRITE:
11427c478bd9Sstevel@tonic-gate 		/* Check to see if this client has the delegation */
11437c478bd9Sstevel@tonic-gate 		return (rfs4_is_deleg(sp));
11447c478bd9Sstevel@tonic-gate 	}
11457c478bd9Sstevel@tonic-gate 
11467c478bd9Sstevel@tonic-gate 	return (FALSE);
11477c478bd9Sstevel@tonic-gate }
11487c478bd9Sstevel@tonic-gate 
11497c478bd9Sstevel@tonic-gate /*
11507c478bd9Sstevel@tonic-gate  * Return the "best" allowable delegation available given the current
11517c478bd9Sstevel@tonic-gate  * delegation type and the desired access and deny modes on the file.
11527c478bd9Sstevel@tonic-gate  * At the point that this routine is called we know that the access and
11537c478bd9Sstevel@tonic-gate  * deny modes are consistent with the file modes.
11547c478bd9Sstevel@tonic-gate  */
11557c478bd9Sstevel@tonic-gate static open_delegation_type4
11567c478bd9Sstevel@tonic-gate rfs4_check_delegation(rfs4_state_t *sp, rfs4_file_t *fp)
11577c478bd9Sstevel@tonic-gate {
1158*d216dff5SRobert Mastors 	open_delegation_type4 dtype = fp->rf_dinfo.rd_dtype;
1159*d216dff5SRobert Mastors 	uint32_t access = sp->rs_share_access;
1160*d216dff5SRobert Mastors 	uint32_t deny = sp->rs_share_deny;
11617c478bd9Sstevel@tonic-gate 	int readcnt = 0;
11627c478bd9Sstevel@tonic-gate 	int writecnt = 0;
11637c478bd9Sstevel@tonic-gate 
11647c478bd9Sstevel@tonic-gate 	switch (dtype) {
11657c478bd9Sstevel@tonic-gate 	case OPEN_DELEGATE_NONE:
11667c478bd9Sstevel@tonic-gate 		/*
11677c478bd9Sstevel@tonic-gate 		 * Determine if more than just this OPEN have the file
11687c478bd9Sstevel@tonic-gate 		 * open and if so, no delegation may be provided to
11697c478bd9Sstevel@tonic-gate 		 * the client.
11707c478bd9Sstevel@tonic-gate 		 */
11717c478bd9Sstevel@tonic-gate 		if (access & OPEN4_SHARE_ACCESS_WRITE)
11727c478bd9Sstevel@tonic-gate 			writecnt++;
11737c478bd9Sstevel@tonic-gate 		if (access & OPEN4_SHARE_ACCESS_READ)
11747c478bd9Sstevel@tonic-gate 			readcnt++;
11757c478bd9Sstevel@tonic-gate 
1176*d216dff5SRobert Mastors 		if (fp->rf_access_read > readcnt ||
1177*d216dff5SRobert Mastors 		    fp->rf_access_write > writecnt)
11787c478bd9Sstevel@tonic-gate 			return (OPEN_DELEGATE_NONE);
11797c478bd9Sstevel@tonic-gate 
11807c478bd9Sstevel@tonic-gate 		/*
11817c478bd9Sstevel@tonic-gate 		 * If the client is going to write, or if the client
11827c478bd9Sstevel@tonic-gate 		 * has exclusive access, return a write delegation.
11837c478bd9Sstevel@tonic-gate 		 */
11847c478bd9Sstevel@tonic-gate 		if ((access & OPEN4_SHARE_ACCESS_WRITE) ||
11857c478bd9Sstevel@tonic-gate 		    (deny & (OPEN4_SHARE_DENY_READ | OPEN4_SHARE_DENY_WRITE)))
11867c478bd9Sstevel@tonic-gate 			return (OPEN_DELEGATE_WRITE);
11877c478bd9Sstevel@tonic-gate 		/*
11887c478bd9Sstevel@tonic-gate 		 * If we don't want to write or we've haven't denied read
11897c478bd9Sstevel@tonic-gate 		 * access to others, return a read delegation.
11907c478bd9Sstevel@tonic-gate 		 */
11917c478bd9Sstevel@tonic-gate 		if ((access & ~OPEN4_SHARE_ACCESS_WRITE) ||
1192ed57cab9Srg 		    (deny & ~OPEN4_SHARE_DENY_READ))
11937c478bd9Sstevel@tonic-gate 			return (OPEN_DELEGATE_READ);
11947c478bd9Sstevel@tonic-gate 
11957c478bd9Sstevel@tonic-gate 		/* Shouldn't get here */
11967c478bd9Sstevel@tonic-gate 		return (OPEN_DELEGATE_NONE);
11977c478bd9Sstevel@tonic-gate 
11987c478bd9Sstevel@tonic-gate 	case OPEN_DELEGATE_READ:
11997c478bd9Sstevel@tonic-gate 		/*
12007c478bd9Sstevel@tonic-gate 		 * If the file is delegated for read but we wan't to
12017c478bd9Sstevel@tonic-gate 		 * write or deny others to read then we can't delegate
12027c478bd9Sstevel@tonic-gate 		 * the file. We shouldn't get here since the delegation should
12037c478bd9Sstevel@tonic-gate 		 * have been recalled already.
12047c478bd9Sstevel@tonic-gate 		 */
12057c478bd9Sstevel@tonic-gate 		if ((access & OPEN4_SHARE_ACCESS_WRITE) ||
12067c478bd9Sstevel@tonic-gate 		    (deny & OPEN4_SHARE_DENY_READ))
1207ed57cab9Srg 			return (OPEN_DELEGATE_NONE);
12087c478bd9Sstevel@tonic-gate 		return (OPEN_DELEGATE_READ);
12097c478bd9Sstevel@tonic-gate 
12107c478bd9Sstevel@tonic-gate 	case OPEN_DELEGATE_WRITE:
12117c478bd9Sstevel@tonic-gate 		return (OPEN_DELEGATE_WRITE);
12127c478bd9Sstevel@tonic-gate 	}
12137c478bd9Sstevel@tonic-gate 
12147c478bd9Sstevel@tonic-gate 	/* Shouldn't get here */
12157c478bd9Sstevel@tonic-gate 	return (OPEN_DELEGATE_NONE);
12167c478bd9Sstevel@tonic-gate }
12177c478bd9Sstevel@tonic-gate 
12187c478bd9Sstevel@tonic-gate /*
12197c478bd9Sstevel@tonic-gate  * Given the desired delegation type and the "history" of the file
12207c478bd9Sstevel@tonic-gate  * determine the actual delegation type to return.
12217c478bd9Sstevel@tonic-gate  */
12227c478bd9Sstevel@tonic-gate static open_delegation_type4
12237c478bd9Sstevel@tonic-gate rfs4_delegation_policy(open_delegation_type4 dtype,
1224*d216dff5SRobert Mastors     rfs4_dinfo_t *dinfo, clientid4 cid)
12257c478bd9Sstevel@tonic-gate {
12267c478bd9Sstevel@tonic-gate 	time_t elapsed;
12277c478bd9Sstevel@tonic-gate 
12287c478bd9Sstevel@tonic-gate 	if (rfs4_deleg_policy != SRV_NORMAL_DELEGATE)
12297c478bd9Sstevel@tonic-gate 		return (OPEN_DELEGATE_NONE);
12307c478bd9Sstevel@tonic-gate 
12317c478bd9Sstevel@tonic-gate 	/*
12327c478bd9Sstevel@tonic-gate 	 * Has this file/delegation ever been recalled?  If not then
1233*d216dff5SRobert Mastors 	 * no further checks for a delegation race need to be done.
12347c478bd9Sstevel@tonic-gate 	 * However if a recall has occurred, then check to see if a
12357c478bd9Sstevel@tonic-gate 	 * client has caused its own delegation recall to occur.  If
12367c478bd9Sstevel@tonic-gate 	 * not, then has a delegation for this file been returned
12377c478bd9Sstevel@tonic-gate 	 * recently?  If so, then do not assign a new delegation to
12387c478bd9Sstevel@tonic-gate 	 * avoid a "delegation race" between the original client and
12397c478bd9Sstevel@tonic-gate 	 * the new/conflicting client.
12407c478bd9Sstevel@tonic-gate 	 */
1241*d216dff5SRobert Mastors 	if (dinfo->rd_ever_recalled == TRUE) {
1242*d216dff5SRobert Mastors 		if (dinfo->rd_conflicted_client != cid) {
1243*d216dff5SRobert Mastors 			elapsed = gethrestime_sec() - dinfo->rd_time_returned;
12447c478bd9Sstevel@tonic-gate 			if (elapsed < rfs4_lease_time)
12457c478bd9Sstevel@tonic-gate 				return (OPEN_DELEGATE_NONE);
12467c478bd9Sstevel@tonic-gate 		}
12477c478bd9Sstevel@tonic-gate 	}
12487c478bd9Sstevel@tonic-gate 
12497c478bd9Sstevel@tonic-gate 	/* Limit the number of read grants */
12507c478bd9Sstevel@tonic-gate 	if (dtype == OPEN_DELEGATE_READ &&
1251*d216dff5SRobert Mastors 	    dinfo->rd_rdgrants > MAX_READ_DELEGATIONS)
12527c478bd9Sstevel@tonic-gate 		return (OPEN_DELEGATE_NONE);
12537c478bd9Sstevel@tonic-gate 
12547c478bd9Sstevel@tonic-gate 	/*
12557c478bd9Sstevel@tonic-gate 	 * Should consider limiting total number of read/write
12567c478bd9Sstevel@tonic-gate 	 * delegations the server will permit.
12577c478bd9Sstevel@tonic-gate 	 */
12587c478bd9Sstevel@tonic-gate 
12597c478bd9Sstevel@tonic-gate 	return (dtype);
12607c478bd9Sstevel@tonic-gate }
12617c478bd9Sstevel@tonic-gate 
12627c478bd9Sstevel@tonic-gate /*
12637c478bd9Sstevel@tonic-gate  * Try and grant a delegation for an open give the state. The routine
12647c478bd9Sstevel@tonic-gate  * returns the delegation type granted. This could be OPEN_DELEGATE_NONE.
12657c478bd9Sstevel@tonic-gate  *
12667c478bd9Sstevel@tonic-gate  * The state and associate file entry must be locked
12677c478bd9Sstevel@tonic-gate  */
12687c478bd9Sstevel@tonic-gate rfs4_deleg_state_t *
12697c478bd9Sstevel@tonic-gate rfs4_grant_delegation(delegreq_t dreq, rfs4_state_t *sp, int *recall)
12707c478bd9Sstevel@tonic-gate {
1271*d216dff5SRobert Mastors 	rfs4_file_t *fp = sp->rs_finfo;
12727c478bd9Sstevel@tonic-gate 	open_delegation_type4 dtype;
12737c478bd9Sstevel@tonic-gate 	int no_delegation;
12747c478bd9Sstevel@tonic-gate 
1275*d216dff5SRobert Mastors 	ASSERT(rfs4_dbe_islocked(sp->rs_dbe));
1276*d216dff5SRobert Mastors 	ASSERT(rfs4_dbe_islocked(fp->rf_dbe));
12777c478bd9Sstevel@tonic-gate 
12787c478bd9Sstevel@tonic-gate 	/* Is the server even providing delegations? */
12797c478bd9Sstevel@tonic-gate 	if (rfs4_deleg_policy == SRV_NEVER_DELEGATE || dreq == DELEG_NONE)
12807c478bd9Sstevel@tonic-gate 		return (NULL);
12817c478bd9Sstevel@tonic-gate 
12827c478bd9Sstevel@tonic-gate 	/* Check to see if delegations have been temporarily disabled */
12837c478bd9Sstevel@tonic-gate 	mutex_enter(&rfs4_deleg_lock);
12847c478bd9Sstevel@tonic-gate 	no_delegation = rfs4_deleg_disabled;
12857c478bd9Sstevel@tonic-gate 	mutex_exit(&rfs4_deleg_lock);
12867c478bd9Sstevel@tonic-gate 
12877c478bd9Sstevel@tonic-gate 	if (no_delegation)
12887c478bd9Sstevel@tonic-gate 		return (NULL);
12897c478bd9Sstevel@tonic-gate 
12907c478bd9Sstevel@tonic-gate 	/* Don't grant a delegation if a deletion is impending. */
1291*d216dff5SRobert Mastors 	if (fp->rf_dinfo.rd_hold_grant > 0) {
12927c478bd9Sstevel@tonic-gate 		return (NULL);
12937c478bd9Sstevel@tonic-gate 	}
12947c478bd9Sstevel@tonic-gate 
12957c478bd9Sstevel@tonic-gate 	/*
12967c478bd9Sstevel@tonic-gate 	 * Don't grant a delegation if there are any lock manager
12977c478bd9Sstevel@tonic-gate 	 * (NFSv2/v3) locks for the file.  This is a bit of a hack (e.g.,
12987c478bd9Sstevel@tonic-gate 	 * if there are only read locks we should be able to grant a
12997c478bd9Sstevel@tonic-gate 	 * read-only delegation), but it's good enough for now.
13007c478bd9Sstevel@tonic-gate 	 *
13017c478bd9Sstevel@tonic-gate 	 * MT safety: the lock manager checks for conflicting delegations
13027c478bd9Sstevel@tonic-gate 	 * before processing a lock request.  That check will block until
13037c478bd9Sstevel@tonic-gate 	 * we are done here.  So if the lock manager acquires a lock after
13047c478bd9Sstevel@tonic-gate 	 * we decide to grant the delegation, the delegation will get
13057c478bd9Sstevel@tonic-gate 	 * immediately recalled (if there's a conflict), so we're safe.
13067c478bd9Sstevel@tonic-gate 	 */
1307*d216dff5SRobert Mastors 	if (lm_vp_active(fp->rf_vp)) {
13087c478bd9Sstevel@tonic-gate 		return (NULL);
13097c478bd9Sstevel@tonic-gate 	}
13107c478bd9Sstevel@tonic-gate 
13117c478bd9Sstevel@tonic-gate 	/*
13127c478bd9Sstevel@tonic-gate 	 * Based on the type of delegation request passed in, take the
13137c478bd9Sstevel@tonic-gate 	 * appropriate action (DELEG_NONE is handled above)
13147c478bd9Sstevel@tonic-gate 	 */
13157c478bd9Sstevel@tonic-gate 	switch (dreq) {
13167c478bd9Sstevel@tonic-gate 
13177c478bd9Sstevel@tonic-gate 	case DELEG_READ:
13187c478bd9Sstevel@tonic-gate 	case DELEG_WRITE:
13197c478bd9Sstevel@tonic-gate 		/*
13207c478bd9Sstevel@tonic-gate 		 * The server "must" grant the delegation in this case.
13217c478bd9Sstevel@tonic-gate 		 * Client is using open previous
13227c478bd9Sstevel@tonic-gate 		 */
13237c478bd9Sstevel@tonic-gate 		dtype = (open_delegation_type4)dreq;
13247c478bd9Sstevel@tonic-gate 		*recall = 1;
13257c478bd9Sstevel@tonic-gate 		break;
13267c478bd9Sstevel@tonic-gate 	case DELEG_ANY:
13277c478bd9Sstevel@tonic-gate 		/*
13287c478bd9Sstevel@tonic-gate 		 * If a valid callback path does not exist, no delegation may
13297c478bd9Sstevel@tonic-gate 		 * be granted.
13307c478bd9Sstevel@tonic-gate 		 */
1331*d216dff5SRobert Mastors 		if (sp->rs_owner->ro_client->rc_cbinfo.cb_state != CB_OK)
13327c478bd9Sstevel@tonic-gate 			return (NULL);
13337c478bd9Sstevel@tonic-gate 
13347c478bd9Sstevel@tonic-gate 		/*
13357c478bd9Sstevel@tonic-gate 		 * If the original operation which caused time_rm_delayed
13367c478bd9Sstevel@tonic-gate 		 * to be set hasn't been retried and completed for one
13377c478bd9Sstevel@tonic-gate 		 * full lease period, clear it and allow delegations to
13387c478bd9Sstevel@tonic-gate 		 * get granted again.
13397c478bd9Sstevel@tonic-gate 		 */
1340*d216dff5SRobert Mastors 		if (fp->rf_dinfo.rd_time_rm_delayed > 0 &&
13417c478bd9Sstevel@tonic-gate 		    gethrestime_sec() >
1342*d216dff5SRobert Mastors 		    fp->rf_dinfo.rd_time_rm_delayed + rfs4_lease_time)
1343*d216dff5SRobert Mastors 			fp->rf_dinfo.rd_time_rm_delayed = 0;
13447c478bd9Sstevel@tonic-gate 
13457c478bd9Sstevel@tonic-gate 		/*
13467c478bd9Sstevel@tonic-gate 		 * If we are waiting for a delegation to be returned then
13477c478bd9Sstevel@tonic-gate 		 * don't delegate this file. We do this for correctness as
13487c478bd9Sstevel@tonic-gate 		 * well as if the file is being recalled we would likely
13497c478bd9Sstevel@tonic-gate 		 * recall this file again.
13507c478bd9Sstevel@tonic-gate 		 */
13517c478bd9Sstevel@tonic-gate 
1352*d216dff5SRobert Mastors 		if (fp->rf_dinfo.rd_time_recalled != 0 ||
1353*d216dff5SRobert Mastors 		    fp->rf_dinfo.rd_time_rm_delayed != 0)
13547c478bd9Sstevel@tonic-gate 			return (NULL);
13557c478bd9Sstevel@tonic-gate 
13567c478bd9Sstevel@tonic-gate 		/* Get the "best" delegation candidate */
13577c478bd9Sstevel@tonic-gate 		dtype = rfs4_check_delegation(sp, fp);
13587c478bd9Sstevel@tonic-gate 
13597c478bd9Sstevel@tonic-gate 		if (dtype == OPEN_DELEGATE_NONE)
13607c478bd9Sstevel@tonic-gate 			return (NULL);
13617c478bd9Sstevel@tonic-gate 
13627c478bd9Sstevel@tonic-gate 		/*
13637c478bd9Sstevel@tonic-gate 		 * Based on policy and the history of the file get the
13647c478bd9Sstevel@tonic-gate 		 * actual delegation.
13657c478bd9Sstevel@tonic-gate 		 */
1366*d216dff5SRobert Mastors 		dtype = rfs4_delegation_policy(dtype, &fp->rf_dinfo,
1367*d216dff5SRobert Mastors 		    sp->rs_owner->ro_client->rc_clientid);
13687c478bd9Sstevel@tonic-gate 
13697c478bd9Sstevel@tonic-gate 		if (dtype == OPEN_DELEGATE_NONE)
13707c478bd9Sstevel@tonic-gate 			return (NULL);
13717c478bd9Sstevel@tonic-gate 		break;
13727c478bd9Sstevel@tonic-gate 	default:
13737c478bd9Sstevel@tonic-gate 		return (NULL);
13747c478bd9Sstevel@tonic-gate 	}
13757c478bd9Sstevel@tonic-gate 
13767c478bd9Sstevel@tonic-gate 	/* set the delegation for the state */
13777c478bd9Sstevel@tonic-gate 	return (rfs4_deleg_state(sp, dtype, recall));
13787c478bd9Sstevel@tonic-gate }
13797c478bd9Sstevel@tonic-gate 
13807c478bd9Sstevel@tonic-gate void
13817c478bd9Sstevel@tonic-gate rfs4_set_deleg_response(rfs4_deleg_state_t *dsp, open_delegation4 *dp,
1382*d216dff5SRobert Mastors     nfsace4 *ace,  int recall)
13837c478bd9Sstevel@tonic-gate {
13847c478bd9Sstevel@tonic-gate 	open_write_delegation4 *wp;
13857c478bd9Sstevel@tonic-gate 	open_read_delegation4 *rp;
13867c478bd9Sstevel@tonic-gate 	nfs_space_limit4 *spl;
13877c478bd9Sstevel@tonic-gate 	nfsace4 nace;
13887c478bd9Sstevel@tonic-gate 
13897c478bd9Sstevel@tonic-gate 	/*
13907c478bd9Sstevel@tonic-gate 	 * We need to allocate a new copy of the who string.
13917c478bd9Sstevel@tonic-gate 	 * this string will be freed by the rfs4_op_open dis_resfree
13927c478bd9Sstevel@tonic-gate 	 * routine. We need to do this allocation since replays will
13937c478bd9Sstevel@tonic-gate 	 * be allocated and rfs4_compound can't tell the difference from
13947c478bd9Sstevel@tonic-gate 	 * a replay and an inital open. N.B. if an ace is passed in, it
13957c478bd9Sstevel@tonic-gate 	 * the caller's responsibility to free it.
13967c478bd9Sstevel@tonic-gate 	 */
13977c478bd9Sstevel@tonic-gate 
13987c478bd9Sstevel@tonic-gate 	if (ace == NULL) {
13997c478bd9Sstevel@tonic-gate 		/*
14007c478bd9Sstevel@tonic-gate 		 * Default is to deny all access, the client will have
14017c478bd9Sstevel@tonic-gate 		 * to contact the server.  XXX Do we want to actually
14027c478bd9Sstevel@tonic-gate 		 * set a deny for every one, or do we simply want to
14037c478bd9Sstevel@tonic-gate 		 * construct an entity that will match no one?
14047c478bd9Sstevel@tonic-gate 		 */
14057c478bd9Sstevel@tonic-gate 		nace.type = ACE4_ACCESS_DENIED_ACE_TYPE;
14067c478bd9Sstevel@tonic-gate 		nace.flag = 0;
14077c478bd9Sstevel@tonic-gate 		nace.access_mask = ACE4_VALID_MASK_BITS;
14087c478bd9Sstevel@tonic-gate 		(void) str_to_utf8(ACE4_WHO_EVERYONE, &nace.who);
14097c478bd9Sstevel@tonic-gate 	} else {
14107c478bd9Sstevel@tonic-gate 		nace.type = ace->type;
14117c478bd9Sstevel@tonic-gate 		nace.flag = ace->flag;
14127c478bd9Sstevel@tonic-gate 		nace.access_mask = ace->access_mask;
14137c478bd9Sstevel@tonic-gate 		(void) utf8_copy(&ace->who, &nace.who);
14147c478bd9Sstevel@tonic-gate 	}
14157c478bd9Sstevel@tonic-gate 
1416*d216dff5SRobert Mastors 	dp->delegation_type = dsp->rds_dtype;
14177c478bd9Sstevel@tonic-gate 
1418*d216dff5SRobert Mastors 	switch (dsp->rds_dtype) {
14197c478bd9Sstevel@tonic-gate 	case OPEN_DELEGATE_NONE:
14207c478bd9Sstevel@tonic-gate 		break;
14217c478bd9Sstevel@tonic-gate 	case OPEN_DELEGATE_READ:
14227c478bd9Sstevel@tonic-gate 		rp = &dp->open_delegation4_u.read;
1423*d216dff5SRobert Mastors 		rp->stateid = dsp->rds_delegid.stateid;
14247c478bd9Sstevel@tonic-gate 		rp->recall = (bool_t)recall;
14257c478bd9Sstevel@tonic-gate 		rp->permissions = nace;
14267c478bd9Sstevel@tonic-gate 		break;
14277c478bd9Sstevel@tonic-gate 	case OPEN_DELEGATE_WRITE:
14287c478bd9Sstevel@tonic-gate 		wp = &dp->open_delegation4_u.write;
1429*d216dff5SRobert Mastors 		wp->stateid = dsp->rds_delegid.stateid;
14307c478bd9Sstevel@tonic-gate 		wp->recall = (bool_t)recall;
14317c478bd9Sstevel@tonic-gate 		spl = &wp->space_limit;
14327c478bd9Sstevel@tonic-gate 		spl->limitby = NFS_LIMIT_SIZE;
14337c478bd9Sstevel@tonic-gate 		spl->nfs_space_limit4_u.filesize = 0;
14347c478bd9Sstevel@tonic-gate 		wp->permissions = nace;
14357c478bd9Sstevel@tonic-gate 		break;
14367c478bd9Sstevel@tonic-gate 	}
14377c478bd9Sstevel@tonic-gate }
14387c478bd9Sstevel@tonic-gate 
14397c478bd9Sstevel@tonic-gate /*
14407c478bd9Sstevel@tonic-gate  * Check if the file is delegated via the provided file struct.
14417c478bd9Sstevel@tonic-gate  * Return TRUE if it is delegated.  This is intended for use by
14427c478bd9Sstevel@tonic-gate  * the v4 server.  The v2/v3 server code should use rfs4_check_delegated().
14437c478bd9Sstevel@tonic-gate  *
14447c478bd9Sstevel@tonic-gate  * Note that if the file is found to have a delegation, it is
14457c478bd9Sstevel@tonic-gate  * recalled, unless the clientid of the caller matches the clientid of the
14467c478bd9Sstevel@tonic-gate  * delegation. If the caller has specified, there is a slight delay
14477c478bd9Sstevel@tonic-gate  * inserted in the hopes that the delegation will be returned quickly.
14487c478bd9Sstevel@tonic-gate  */
14497c478bd9Sstevel@tonic-gate bool_t
14507c478bd9Sstevel@tonic-gate rfs4_check_delegated_byfp(int mode, rfs4_file_t *fp,
1451*d216dff5SRobert Mastors     bool_t trunc, bool_t do_delay, bool_t is_rm, clientid4 *cp)
14527c478bd9Sstevel@tonic-gate {
14537c478bd9Sstevel@tonic-gate 	rfs4_deleg_state_t *dsp;
14547c478bd9Sstevel@tonic-gate 
14557c478bd9Sstevel@tonic-gate 	/* Is delegation enabled? */
14567c478bd9Sstevel@tonic-gate 	if (rfs4_deleg_policy == SRV_NEVER_DELEGATE)
14577c478bd9Sstevel@tonic-gate 		return (FALSE);
14587c478bd9Sstevel@tonic-gate 
14597c478bd9Sstevel@tonic-gate 	/* do we have a delegation on this file? */
1460*d216dff5SRobert Mastors 	rfs4_dbe_lock(fp->rf_dbe);
1461*d216dff5SRobert Mastors 	if (fp->rf_dinfo.rd_dtype == OPEN_DELEGATE_NONE) {
14627c478bd9Sstevel@tonic-gate 		if (is_rm)
1463*d216dff5SRobert Mastors 			fp->rf_dinfo.rd_hold_grant++;
1464*d216dff5SRobert Mastors 		rfs4_dbe_unlock(fp->rf_dbe);
14657c478bd9Sstevel@tonic-gate 		return (FALSE);
14667c478bd9Sstevel@tonic-gate 	}
14677c478bd9Sstevel@tonic-gate 	/*
14687c478bd9Sstevel@tonic-gate 	 * do we have a write delegation on this file or are we
14697c478bd9Sstevel@tonic-gate 	 * requesting write access to a file with any type of existing
14707c478bd9Sstevel@tonic-gate 	 * delegation?
14717c478bd9Sstevel@tonic-gate 	 */
1472*d216dff5SRobert Mastors 	if (mode == FWRITE || fp->rf_dinfo.rd_dtype == OPEN_DELEGATE_WRITE) {
14737c478bd9Sstevel@tonic-gate 		if (cp != NULL) {
1474*d216dff5SRobert Mastors 			dsp = list_head(&fp->rf_delegstatelist);
14757c478bd9Sstevel@tonic-gate 			if (dsp == NULL) {
1476*d216dff5SRobert Mastors 				rfs4_dbe_unlock(fp->rf_dbe);
14777c478bd9Sstevel@tonic-gate 				return (FALSE);
14787c478bd9Sstevel@tonic-gate 			}
14797c478bd9Sstevel@tonic-gate 			/*
14807c478bd9Sstevel@tonic-gate 			 * Does the requestor already own the delegation?
14817c478bd9Sstevel@tonic-gate 			 */
1482*d216dff5SRobert Mastors 			if (dsp->rds_client->rc_clientid == *(cp)) {
1483*d216dff5SRobert Mastors 				rfs4_dbe_unlock(fp->rf_dbe);
14847c478bd9Sstevel@tonic-gate 				return (FALSE);
14857c478bd9Sstevel@tonic-gate 			}
14867c478bd9Sstevel@tonic-gate 		}
14877c478bd9Sstevel@tonic-gate 
1488*d216dff5SRobert Mastors 		rfs4_dbe_unlock(fp->rf_dbe);
14897c478bd9Sstevel@tonic-gate 		rfs4_recall_deleg(fp, trunc, NULL);
14907c478bd9Sstevel@tonic-gate 
14917c478bd9Sstevel@tonic-gate 		if (!do_delay) {
1492*d216dff5SRobert Mastors 			rfs4_dbe_lock(fp->rf_dbe);
1493*d216dff5SRobert Mastors 			fp->rf_dinfo.rd_time_rm_delayed = gethrestime_sec();
1494*d216dff5SRobert Mastors 			rfs4_dbe_unlock(fp->rf_dbe);
14957c478bd9Sstevel@tonic-gate 			return (TRUE);
14967c478bd9Sstevel@tonic-gate 		}
14977c478bd9Sstevel@tonic-gate 
14987c478bd9Sstevel@tonic-gate 		delay(NFS4_DELEGATION_CONFLICT_DELAY);
14997c478bd9Sstevel@tonic-gate 
1500*d216dff5SRobert Mastors 		rfs4_dbe_lock(fp->rf_dbe);
1501*d216dff5SRobert Mastors 		if (fp->rf_dinfo.rd_dtype != OPEN_DELEGATE_NONE) {
1502*d216dff5SRobert Mastors 			fp->rf_dinfo.rd_time_rm_delayed = gethrestime_sec();
1503*d216dff5SRobert Mastors 			rfs4_dbe_unlock(fp->rf_dbe);
15047c478bd9Sstevel@tonic-gate 			return (TRUE);
15057c478bd9Sstevel@tonic-gate 		}
15067c478bd9Sstevel@tonic-gate 	}
15077c478bd9Sstevel@tonic-gate 	if (is_rm)
1508*d216dff5SRobert Mastors 		fp->rf_dinfo.rd_hold_grant++;
1509*d216dff5SRobert Mastors 	rfs4_dbe_unlock(fp->rf_dbe);
15107c478bd9Sstevel@tonic-gate 	return (FALSE);
15117c478bd9Sstevel@tonic-gate }
15127c478bd9Sstevel@tonic-gate 
15137c478bd9Sstevel@tonic-gate /*
15147c478bd9Sstevel@tonic-gate  * Check if the file is delegated in the case of a v2 or v3 access.
15157c478bd9Sstevel@tonic-gate  * Return TRUE if it is delegated which in turn means that v2 should
15167c478bd9Sstevel@tonic-gate  * drop the request and in the case of v3 JUKEBOX should be returned.
15177c478bd9Sstevel@tonic-gate  */
15187c478bd9Sstevel@tonic-gate bool_t
15197c478bd9Sstevel@tonic-gate rfs4_check_delegated(int mode, vnode_t *vp, bool_t trunc)
15207c478bd9Sstevel@tonic-gate {
15217c478bd9Sstevel@tonic-gate 	rfs4_file_t *fp;
15227c478bd9Sstevel@tonic-gate 	bool_t create = FALSE;
15237c478bd9Sstevel@tonic-gate 	bool_t rc = FALSE;
15247c478bd9Sstevel@tonic-gate 
15257c478bd9Sstevel@tonic-gate 	rfs4_hold_deleg_policy();
15267c478bd9Sstevel@tonic-gate 
15277c478bd9Sstevel@tonic-gate 	/* Is delegation enabled? */
15287c478bd9Sstevel@tonic-gate 	if (rfs4_deleg_policy != SRV_NEVER_DELEGATE) {
15297c478bd9Sstevel@tonic-gate 		fp = rfs4_findfile(vp, NULL, &create);
15307c478bd9Sstevel@tonic-gate 		if (fp != NULL) {
15317c478bd9Sstevel@tonic-gate 			if (rfs4_check_delegated_byfp(mode, fp, trunc,
1532ed57cab9Srg 			    TRUE, FALSE, NULL)) {
15337c478bd9Sstevel@tonic-gate 				rc = TRUE;
15347c478bd9Sstevel@tonic-gate 			}
15357c478bd9Sstevel@tonic-gate 			rfs4_file_rele(fp);
15367c478bd9Sstevel@tonic-gate 		}
15377c478bd9Sstevel@tonic-gate 	}
15387c478bd9Sstevel@tonic-gate 	rfs4_rele_deleg_policy();
15397c478bd9Sstevel@tonic-gate 	return (rc);
15407c478bd9Sstevel@tonic-gate }
15417c478bd9Sstevel@tonic-gate 
15427c478bd9Sstevel@tonic-gate /*
15437c478bd9Sstevel@tonic-gate  * Release a hold on the hold_grant counter which
15447c478bd9Sstevel@tonic-gate  * prevents delegation from being granted while a remove
15457c478bd9Sstevel@tonic-gate  * or a rename is in progress.
15467c478bd9Sstevel@tonic-gate  */
15477c478bd9Sstevel@tonic-gate void
15487c478bd9Sstevel@tonic-gate rfs4_clear_dont_grant(rfs4_file_t *fp)
15497c478bd9Sstevel@tonic-gate {
15507c478bd9Sstevel@tonic-gate 	if (rfs4_deleg_policy == SRV_NEVER_DELEGATE)
15517c478bd9Sstevel@tonic-gate 		return;
1552*d216dff5SRobert Mastors 	rfs4_dbe_lock(fp->rf_dbe);
1553*d216dff5SRobert Mastors 	ASSERT(fp->rf_dinfo.rd_hold_grant > 0);
1554*d216dff5SRobert Mastors 	fp->rf_dinfo.rd_hold_grant--;
1555*d216dff5SRobert Mastors 	fp->rf_dinfo.rd_time_rm_delayed = 0;
1556*d216dff5SRobert Mastors 	rfs4_dbe_unlock(fp->rf_dbe);
15577c478bd9Sstevel@tonic-gate }
15587c478bd9Sstevel@tonic-gate 
15597c478bd9Sstevel@tonic-gate /*
15607c478bd9Sstevel@tonic-gate  * State support for delegation.
15617c478bd9Sstevel@tonic-gate  * Set the state delegation type for this state;
15627c478bd9Sstevel@tonic-gate  * This routine is called from open via rfs4_grant_delegation and the entry
1563*d216dff5SRobert Mastors  * locks on sp and sp->rs_finfo are assumed.
15647c478bd9Sstevel@tonic-gate  */
15657c478bd9Sstevel@tonic-gate static rfs4_deleg_state_t *
15667c478bd9Sstevel@tonic-gate rfs4_deleg_state(rfs4_state_t *sp, open_delegation_type4 dtype, int *recall)
15677c478bd9Sstevel@tonic-gate {
1568*d216dff5SRobert Mastors 	rfs4_file_t *fp = sp->rs_finfo;
15697c478bd9Sstevel@tonic-gate 	bool_t create = TRUE;
15707c478bd9Sstevel@tonic-gate 	rfs4_deleg_state_t *dsp;
15717c478bd9Sstevel@tonic-gate 	vnode_t *vp;
15727c478bd9Sstevel@tonic-gate 	int open_prev = *recall;
1573da6c28aaSamw 	int ret;
1574da6c28aaSamw 	int fflags = 0;
15757c478bd9Sstevel@tonic-gate 
1576*d216dff5SRobert Mastors 	ASSERT(rfs4_dbe_islocked(sp->rs_dbe));
1577*d216dff5SRobert Mastors 	ASSERT(rfs4_dbe_islocked(fp->rf_dbe));
15787c478bd9Sstevel@tonic-gate 
15797c478bd9Sstevel@tonic-gate 	/* Shouldn't happen */
1580*d216dff5SRobert Mastors 	if (fp->rf_dinfo.rd_recall_count != 0 ||
1581*d216dff5SRobert Mastors 	    (fp->rf_dinfo.rd_dtype == OPEN_DELEGATE_READ &&
1582ed57cab9Srg 	    dtype != OPEN_DELEGATE_READ)) {
15837c478bd9Sstevel@tonic-gate 		return (NULL);
15847c478bd9Sstevel@tonic-gate 	}
15857c478bd9Sstevel@tonic-gate 
15867c478bd9Sstevel@tonic-gate 	/* Unlock to avoid deadlock */
1587*d216dff5SRobert Mastors 	rfs4_dbe_unlock(fp->rf_dbe);
1588*d216dff5SRobert Mastors 	rfs4_dbe_unlock(sp->rs_dbe);
15897c478bd9Sstevel@tonic-gate 
15907c478bd9Sstevel@tonic-gate 	dsp = rfs4_finddeleg(sp, &create);
15917c478bd9Sstevel@tonic-gate 
1592*d216dff5SRobert Mastors 	rfs4_dbe_lock(sp->rs_dbe);
1593*d216dff5SRobert Mastors 	rfs4_dbe_lock(fp->rf_dbe);
15947c478bd9Sstevel@tonic-gate 
15957c478bd9Sstevel@tonic-gate 	if (dsp == NULL)
15967c478bd9Sstevel@tonic-gate 		return (NULL);
15977c478bd9Sstevel@tonic-gate 
1598da1b9cd5Sjwahlig 	/*
1599da1b9cd5Sjwahlig 	 * It is possible that since we dropped the lock
1600da1b9cd5Sjwahlig 	 * in order to call finddeleg, the rfs4_file_t
1601da1b9cd5Sjwahlig 	 * was marked such that we should not grant a
1602da1b9cd5Sjwahlig 	 * delegation, if so bail out.
1603da1b9cd5Sjwahlig 	 */
1604*d216dff5SRobert Mastors 	if (fp->rf_dinfo.rd_hold_grant > 0) {
1605da1b9cd5Sjwahlig 		rfs4_deleg_state_rele(dsp);
1606da1b9cd5Sjwahlig 		return (NULL);
1607da1b9cd5Sjwahlig 	}
1608da1b9cd5Sjwahlig 
16097c478bd9Sstevel@tonic-gate 	if (create == FALSE) {
1610*d216dff5SRobert Mastors 		if (sp->rs_owner->ro_client == dsp->rds_client &&
1611*d216dff5SRobert Mastors 		    dsp->rds_dtype == dtype) {
16127c478bd9Sstevel@tonic-gate 			return (dsp);
16137c478bd9Sstevel@tonic-gate 		} else {
16147c478bd9Sstevel@tonic-gate 			rfs4_deleg_state_rele(dsp);
16157c478bd9Sstevel@tonic-gate 			return (NULL);
16167c478bd9Sstevel@tonic-gate 		}
16177c478bd9Sstevel@tonic-gate 	}
16187c478bd9Sstevel@tonic-gate 
16197c478bd9Sstevel@tonic-gate 	/*
16207c478bd9Sstevel@tonic-gate 	 * Check that this file has not been delegated to another
16217c478bd9Sstevel@tonic-gate 	 * client
16227c478bd9Sstevel@tonic-gate 	 */
1623*d216dff5SRobert Mastors 	if (fp->rf_dinfo.rd_recall_count != 0 ||
1624*d216dff5SRobert Mastors 	    fp->rf_dinfo.rd_dtype == OPEN_DELEGATE_WRITE ||
1625*d216dff5SRobert Mastors 	    (fp->rf_dinfo.rd_dtype == OPEN_DELEGATE_READ &&
1626ed57cab9Srg 	    dtype != OPEN_DELEGATE_READ)) {
16277c478bd9Sstevel@tonic-gate 		rfs4_deleg_state_rele(dsp);
16287c478bd9Sstevel@tonic-gate 		return (NULL);
16297c478bd9Sstevel@tonic-gate 	}
16307c478bd9Sstevel@tonic-gate 
1631*d216dff5SRobert Mastors 	vp = fp->rf_vp;
16327c478bd9Sstevel@tonic-gate 	/* vnevent_support returns 0 if file system supports vnevents */
1633da6c28aaSamw 	if (vnevent_support(vp, NULL)) {
16347c478bd9Sstevel@tonic-gate 		rfs4_deleg_state_rele(dsp);
16357c478bd9Sstevel@tonic-gate 		return (NULL);
16367c478bd9Sstevel@tonic-gate 	}
16377c478bd9Sstevel@tonic-gate 
1638da6c28aaSamw 	/* Calculate the fflags for this OPEN. */
1639*d216dff5SRobert Mastors 	if (sp->rs_share_access & OPEN4_SHARE_ACCESS_READ)
1640da6c28aaSamw 		fflags |= FREAD;
1641*d216dff5SRobert Mastors 	if (sp->rs_share_access & OPEN4_SHARE_ACCESS_WRITE)
1642da6c28aaSamw 		fflags |= FWRITE;
1643da6c28aaSamw 
16447c478bd9Sstevel@tonic-gate 	*recall = 0;
1645da6c28aaSamw 	/*
1646da6c28aaSamw 	 * Before granting a delegation we need to know if anyone else has
1647da6c28aaSamw 	 * opened the file in a conflicting mode.  However, first we need to
1648da6c28aaSamw 	 * know how we opened the file to check the counts properly.
1649da6c28aaSamw 	 */
16507c478bd9Sstevel@tonic-gate 	if (dtype == OPEN_DELEGATE_READ) {
1651da6c28aaSamw 		if (((fflags & FWRITE) && vn_has_other_opens(vp, V_WRITE)) ||
1652da6c28aaSamw 		    (((fflags & FWRITE) == 0) && vn_is_opened(vp, V_WRITE)) ||
1653da6c28aaSamw 		    vn_is_mapped(vp, V_WRITE)) {
16547c478bd9Sstevel@tonic-gate 			if (open_prev) {
16557c478bd9Sstevel@tonic-gate 				*recall = 1;
16567c478bd9Sstevel@tonic-gate 			} else {
16577c478bd9Sstevel@tonic-gate 				rfs4_deleg_state_rele(dsp);
16587c478bd9Sstevel@tonic-gate 				return (NULL);
16597c478bd9Sstevel@tonic-gate 			}
16607c478bd9Sstevel@tonic-gate 		}
1661da6c28aaSamw 		ret = fem_install(vp, deleg_rdops, (void *)fp, OPUNIQ,
1662ed57cab9Srg 		    rfs4_mon_hold, rfs4_mon_rele);
1663da6c28aaSamw 		if (((fflags & FWRITE) && vn_has_other_opens(vp, V_WRITE)) ||
1664da6c28aaSamw 		    (((fflags & FWRITE) == 0) && vn_is_opened(vp, V_WRITE)) ||
1665da6c28aaSamw 		    vn_is_mapped(vp, V_WRITE)) {
16667c478bd9Sstevel@tonic-gate 			if (open_prev) {
16677c478bd9Sstevel@tonic-gate 				*recall = 1;
16687c478bd9Sstevel@tonic-gate 			} else {
16697c478bd9Sstevel@tonic-gate 				(void) fem_uninstall(vp, deleg_rdops,
1670ed57cab9Srg 				    (void *)fp);
16717c478bd9Sstevel@tonic-gate 				rfs4_deleg_state_rele(dsp);
16727c478bd9Sstevel@tonic-gate 				return (NULL);
16737c478bd9Sstevel@tonic-gate 			}
16747c478bd9Sstevel@tonic-gate 		}
1675da6c28aaSamw 		/*
1676da6c28aaSamw 		 * Because a client can hold onto a delegation after the
1677da6c28aaSamw 		 * file has been closed, we need to keep track of the
1678da6c28aaSamw 		 * access to this file.  Otherwise the CIFS server would
1679da6c28aaSamw 		 * not know about the client accessing the file and could
1680da6c28aaSamw 		 * inappropriately grant an OPLOCK.
1681da6c28aaSamw 		 * fem_install() returns EBUSY when asked to install a
1682da6c28aaSamw 		 * OPUNIQ monitor more than once.  Therefore, check the
1683da6c28aaSamw 		 * return code because we only want this done once.
1684da6c28aaSamw 		 */
1685da6c28aaSamw 		if (ret == 0)
1686da6c28aaSamw 			vn_open_upgrade(vp, FREAD);
16877c478bd9Sstevel@tonic-gate 	} else { /* WRITE */
1688da6c28aaSamw 		if (((fflags & FWRITE) && vn_has_other_opens(vp, V_WRITE)) ||
1689da6c28aaSamw 		    (((fflags & FWRITE) == 0) && vn_is_opened(vp, V_WRITE)) ||
1690da6c28aaSamw 		    ((fflags & FREAD) && vn_has_other_opens(vp, V_READ)) ||
1691da6c28aaSamw 		    (((fflags & FREAD) == 0) && vn_is_opened(vp, V_READ)) ||
1692da6c28aaSamw 		    vn_is_mapped(vp, V_RDORWR)) {
16937c478bd9Sstevel@tonic-gate 			if (open_prev) {
16947c478bd9Sstevel@tonic-gate 				*recall = 1;
16957c478bd9Sstevel@tonic-gate 			} else {
16967c478bd9Sstevel@tonic-gate 				rfs4_deleg_state_rele(dsp);
16977c478bd9Sstevel@tonic-gate 				return (NULL);
16987c478bd9Sstevel@tonic-gate 			}
16997c478bd9Sstevel@tonic-gate 		}
1700da6c28aaSamw 		ret = fem_install(vp, deleg_wrops, (void *)fp, OPUNIQ,
1701ed57cab9Srg 		    rfs4_mon_hold, rfs4_mon_rele);
1702da6c28aaSamw 		if (((fflags & FWRITE) && vn_has_other_opens(vp, V_WRITE)) ||
1703da6c28aaSamw 		    (((fflags & FWRITE) == 0) && vn_is_opened(vp, V_WRITE)) ||
1704da6c28aaSamw 		    ((fflags & FREAD) && vn_has_other_opens(vp, V_READ)) ||
1705da6c28aaSamw 		    (((fflags & FREAD) == 0) && vn_is_opened(vp, V_READ)) ||
1706da6c28aaSamw 		    vn_is_mapped(vp, V_RDORWR)) {
17077c478bd9Sstevel@tonic-gate 			if (open_prev) {
17087c478bd9Sstevel@tonic-gate 				*recall = 1;
17097c478bd9Sstevel@tonic-gate 			} else {
17107c478bd9Sstevel@tonic-gate 				(void) fem_uninstall(vp, deleg_wrops,
1711ed57cab9Srg 				    (void *)fp);
17127c478bd9Sstevel@tonic-gate 				rfs4_deleg_state_rele(dsp);
17137c478bd9Sstevel@tonic-gate 				return (NULL);
17147c478bd9Sstevel@tonic-gate 			}
17157c478bd9Sstevel@tonic-gate 		}
1716da6c28aaSamw 		/*
1717da6c28aaSamw 		 * Because a client can hold onto a delegation after the
1718da6c28aaSamw 		 * file has been closed, we need to keep track of the
1719da6c28aaSamw 		 * access to this file.  Otherwise the CIFS server would
1720da6c28aaSamw 		 * not know about the client accessing the file and could
1721da6c28aaSamw 		 * inappropriately grant an OPLOCK.
1722da6c28aaSamw 		 * fem_install() returns EBUSY when asked to install a
1723da6c28aaSamw 		 * OPUNIQ monitor more than once.  Therefore, check the
1724da6c28aaSamw 		 * return code because we only want this done once.
1725da6c28aaSamw 		 */
1726da6c28aaSamw 		if (ret == 0)
1727da6c28aaSamw 			vn_open_upgrade(vp, FREAD|FWRITE);
17287c478bd9Sstevel@tonic-gate 	}
17297c478bd9Sstevel@tonic-gate 	/* Place on delegation list for file */
1730*d216dff5SRobert Mastors 	ASSERT(!list_link_active(&dsp->rds_node));
1731*d216dff5SRobert Mastors 	list_insert_tail(&fp->rf_delegstatelist, dsp);
17327c478bd9Sstevel@tonic-gate 
1733*d216dff5SRobert Mastors 	dsp->rds_dtype = fp->rf_dinfo.rd_dtype = dtype;
17347c478bd9Sstevel@tonic-gate 
17357c478bd9Sstevel@tonic-gate 	/* Update delegation stats for this file */
1736*d216dff5SRobert Mastors 	fp->rf_dinfo.rd_time_lastgrant = gethrestime_sec();
17377c478bd9Sstevel@tonic-gate 
17387c478bd9Sstevel@tonic-gate 	/* reset since this is a new delegation */
1739*d216dff5SRobert Mastors 	fp->rf_dinfo.rd_conflicted_client = 0;
1740*d216dff5SRobert Mastors 	fp->rf_dinfo.rd_ever_recalled = FALSE;
17417c478bd9Sstevel@tonic-gate 
17427c478bd9Sstevel@tonic-gate 	if (dtype == OPEN_DELEGATE_READ)
1743*d216dff5SRobert Mastors 		fp->rf_dinfo.rd_rdgrants++;
17447c478bd9Sstevel@tonic-gate 	else
1745*d216dff5SRobert Mastors 		fp->rf_dinfo.rd_wrgrants++;
17467c478bd9Sstevel@tonic-gate 
17477c478bd9Sstevel@tonic-gate 	return (dsp);
17487c478bd9Sstevel@tonic-gate }
17497c478bd9Sstevel@tonic-gate 
17507c478bd9Sstevel@tonic-gate /*
17517c478bd9Sstevel@tonic-gate  * State routine for the server when a delegation is returned.
17527c478bd9Sstevel@tonic-gate  */
17537c478bd9Sstevel@tonic-gate void
17547c478bd9Sstevel@tonic-gate rfs4_return_deleg(rfs4_deleg_state_t *dsp, bool_t revoked)
17557c478bd9Sstevel@tonic-gate {
1756*d216dff5SRobert Mastors 	rfs4_file_t *fp = dsp->rds_finfo;
17577c478bd9Sstevel@tonic-gate 	open_delegation_type4 dtypewas;
17587c478bd9Sstevel@tonic-gate 
1759*d216dff5SRobert Mastors 	rfs4_dbe_lock(fp->rf_dbe);
17607c478bd9Sstevel@tonic-gate 
1761*d216dff5SRobert Mastors 	/* nothing to do if no longer on list */
1762*d216dff5SRobert Mastors 	if (!list_link_active(&dsp->rds_node)) {
1763*d216dff5SRobert Mastors 		rfs4_dbe_unlock(fp->rf_dbe);
1764*d216dff5SRobert Mastors 		return;
1765*d216dff5SRobert Mastors 	}
1766*d216dff5SRobert Mastors 
1767*d216dff5SRobert Mastors 	/* Remove state from recall list */
1768*d216dff5SRobert Mastors 	list_remove(&fp->rf_delegstatelist, dsp);
17697c478bd9Sstevel@tonic-gate 
1770*d216dff5SRobert Mastors 	if (list_is_empty(&fp->rf_delegstatelist)) {
1771*d216dff5SRobert Mastors 		dtypewas = fp->rf_dinfo.rd_dtype;
1772*d216dff5SRobert Mastors 		fp->rf_dinfo.rd_dtype = OPEN_DELEGATE_NONE;
1773*d216dff5SRobert Mastors 		rfs4_dbe_cv_broadcast(fp->rf_dbe);
17747c478bd9Sstevel@tonic-gate 
17757c478bd9Sstevel@tonic-gate 		/* if file system was unshared, the vp will be NULL */
1776*d216dff5SRobert Mastors 		if (fp->rf_vp != NULL) {
1777da6c28aaSamw 			/*
1778da6c28aaSamw 			 * Once a delegation is no longer held by any client,
1779da6c28aaSamw 			 * the monitor is uninstalled.  At this point, the
1780da6c28aaSamw 			 * client must send OPEN otw, so we don't need the
1781da6c28aaSamw 			 * reference on the vnode anymore.  The open
1782da6c28aaSamw 			 * downgrade removes the reference put on earlier.
1783da6c28aaSamw 			 */
1784da6c28aaSamw 			if (dtypewas == OPEN_DELEGATE_READ) {
1785*d216dff5SRobert Mastors 				(void) fem_uninstall(fp->rf_vp, deleg_rdops,
1786ed57cab9Srg 				    (void *)fp);
1787*d216dff5SRobert Mastors 				vn_open_downgrade(fp->rf_vp, FREAD);
1788da6c28aaSamw 			} else if (dtypewas == OPEN_DELEGATE_WRITE) {
1789*d216dff5SRobert Mastors 				(void) fem_uninstall(fp->rf_vp, deleg_wrops,
1790ed57cab9Srg 				    (void *)fp);
1791*d216dff5SRobert Mastors 				vn_open_downgrade(fp->rf_vp, FREAD|FWRITE);
1792da6c28aaSamw 			}
17937c478bd9Sstevel@tonic-gate 		}
17947c478bd9Sstevel@tonic-gate 	}
17957c478bd9Sstevel@tonic-gate 
1796*d216dff5SRobert Mastors 	switch (dsp->rds_dtype) {
17977c478bd9Sstevel@tonic-gate 	case OPEN_DELEGATE_READ:
1798*d216dff5SRobert Mastors 		fp->rf_dinfo.rd_rdgrants--;
17997c478bd9Sstevel@tonic-gate 		break;
18007c478bd9Sstevel@tonic-gate 	case OPEN_DELEGATE_WRITE:
1801*d216dff5SRobert Mastors 		fp->rf_dinfo.rd_wrgrants--;
18027c478bd9Sstevel@tonic-gate 		break;
18037c478bd9Sstevel@tonic-gate 	default:
18047c478bd9Sstevel@tonic-gate 		break;
18057c478bd9Sstevel@tonic-gate 	}
18067c478bd9Sstevel@tonic-gate 
18077c478bd9Sstevel@tonic-gate 	/* used in the policy decision */
1808*d216dff5SRobert Mastors 	fp->rf_dinfo.rd_time_returned = gethrestime_sec();
18097c478bd9Sstevel@tonic-gate 
18107c478bd9Sstevel@tonic-gate 	/*
18117c478bd9Sstevel@tonic-gate 	 * reset the time_recalled field so future delegations are not
18127c478bd9Sstevel@tonic-gate 	 * accidentally revoked
18137c478bd9Sstevel@tonic-gate 	 */
1814*d216dff5SRobert Mastors 	if ((fp->rf_dinfo.rd_rdgrants + fp->rf_dinfo.rd_wrgrants) == 0)
1815*d216dff5SRobert Mastors 		fp->rf_dinfo.rd_time_recalled = 0;
18167c478bd9Sstevel@tonic-gate 
1817*d216dff5SRobert Mastors 	rfs4_dbe_unlock(fp->rf_dbe);
18187c478bd9Sstevel@tonic-gate 
1819*d216dff5SRobert Mastors 	rfs4_dbe_lock(dsp->rds_dbe);
18207c478bd9Sstevel@tonic-gate 
1821*d216dff5SRobert Mastors 	dsp->rds_dtype = OPEN_DELEGATE_NONE;
18227c478bd9Sstevel@tonic-gate 
18237c478bd9Sstevel@tonic-gate 	if (revoked == TRUE)
1824*d216dff5SRobert Mastors 		dsp->rds_time_revoked = gethrestime_sec();
18257c478bd9Sstevel@tonic-gate 
1826*d216dff5SRobert Mastors 	rfs4_dbe_invalidate(dsp->rds_dbe);
18277c478bd9Sstevel@tonic-gate 
1828*d216dff5SRobert Mastors 	rfs4_dbe_unlock(dsp->rds_dbe);
18297c478bd9Sstevel@tonic-gate 
18307c478bd9Sstevel@tonic-gate 	if (revoked == TRUE) {
1831*d216dff5SRobert Mastors 		rfs4_dbe_lock(dsp->rds_client->rc_dbe);
1832*d216dff5SRobert Mastors 		dsp->rds_client->rc_deleg_revoked++;	/* observability */
1833*d216dff5SRobert Mastors 		rfs4_dbe_unlock(dsp->rds_client->rc_dbe);
18347c478bd9Sstevel@tonic-gate 	}
18357c478bd9Sstevel@tonic-gate }
18367c478bd9Sstevel@tonic-gate 
18377c478bd9Sstevel@tonic-gate static void
18387c478bd9Sstevel@tonic-gate rfs4_revoke_file(rfs4_file_t *fp)
18397c478bd9Sstevel@tonic-gate {
18407c478bd9Sstevel@tonic-gate 	rfs4_deleg_state_t *dsp;
18417c478bd9Sstevel@tonic-gate 
18427c478bd9Sstevel@tonic-gate 	/*
18437c478bd9Sstevel@tonic-gate 	 * The lock for rfs4_file_t must be held when traversing the
18447c478bd9Sstevel@tonic-gate 	 * delegation list but that lock needs to be released to call
1845*d216dff5SRobert Mastors 	 * rfs4_return_deleg()
18467c478bd9Sstevel@tonic-gate 	 */
1847*d216dff5SRobert Mastors 	rfs4_dbe_lock(fp->rf_dbe);
1848*d216dff5SRobert Mastors 	while (dsp = list_head(&fp->rf_delegstatelist)) {
1849*d216dff5SRobert Mastors 		rfs4_dbe_hold(dsp->rds_dbe);
1850*d216dff5SRobert Mastors 		rfs4_dbe_unlock(fp->rf_dbe);
1851*d216dff5SRobert Mastors 		rfs4_return_deleg(dsp, TRUE);
18527c478bd9Sstevel@tonic-gate 		rfs4_deleg_state_rele(dsp);
1853*d216dff5SRobert Mastors 		rfs4_dbe_lock(fp->rf_dbe);
18547c478bd9Sstevel@tonic-gate 	}
1855*d216dff5SRobert Mastors 	rfs4_dbe_unlock(fp->rf_dbe);
18567c478bd9Sstevel@tonic-gate }
18577c478bd9Sstevel@tonic-gate 
18587c478bd9Sstevel@tonic-gate /*
18597c478bd9Sstevel@tonic-gate  * A delegation is assumed to be present on the file associated with
1860*d216dff5SRobert Mastors  * "sp".  Check to see if the delegation matches is associated with
1861*d216dff5SRobert Mastors  * the same client as referenced by "sp".  If it is not, TRUE is
18627c478bd9Sstevel@tonic-gate  * returned.  If the delegation DOES match the client (or no
18637c478bd9Sstevel@tonic-gate  * delegation is present), return FALSE.
18647c478bd9Sstevel@tonic-gate  * Assume the state entry and file entry are locked.
18657c478bd9Sstevel@tonic-gate  */
18667c478bd9Sstevel@tonic-gate bool_t
1867*d216dff5SRobert Mastors rfs4_is_deleg(rfs4_state_t *sp)
18687c478bd9Sstevel@tonic-gate {
18697c478bd9Sstevel@tonic-gate 	rfs4_deleg_state_t *dsp;
1870*d216dff5SRobert Mastors 	rfs4_file_t *fp = sp->rs_finfo;
1871*d216dff5SRobert Mastors 	rfs4_client_t *cp = sp->rs_owner->ro_client;
18727c478bd9Sstevel@tonic-gate 
1873*d216dff5SRobert Mastors 	ASSERT(rfs4_dbe_islocked(fp->rf_dbe));
1874*d216dff5SRobert Mastors 	for (dsp = list_head(&fp->rf_delegstatelist); dsp != NULL;
1875*d216dff5SRobert Mastors 	    dsp = list_next(&fp->rf_delegstatelist, dsp)) {
1876*d216dff5SRobert Mastors 		if (cp != dsp->rds_client) {
18777c478bd9Sstevel@tonic-gate 			return (TRUE);
18787c478bd9Sstevel@tonic-gate 		}
18797c478bd9Sstevel@tonic-gate 	}
18807c478bd9Sstevel@tonic-gate 	return (FALSE);
18817c478bd9Sstevel@tonic-gate }
18827c478bd9Sstevel@tonic-gate 
18837c478bd9Sstevel@tonic-gate void
18847c478bd9Sstevel@tonic-gate rfs4_disable_delegation(void)
18857c478bd9Sstevel@tonic-gate {
18867c478bd9Sstevel@tonic-gate 	mutex_enter(&rfs4_deleg_lock);
18877c478bd9Sstevel@tonic-gate 	rfs4_deleg_disabled++;
18887c478bd9Sstevel@tonic-gate 	mutex_exit(&rfs4_deleg_lock);
18897c478bd9Sstevel@tonic-gate }
18907c478bd9Sstevel@tonic-gate 
18917c478bd9Sstevel@tonic-gate void
18927c478bd9Sstevel@tonic-gate rfs4_enable_delegation(void)
18937c478bd9Sstevel@tonic-gate {
18947c478bd9Sstevel@tonic-gate 	mutex_enter(&rfs4_deleg_lock);
18957c478bd9Sstevel@tonic-gate 	ASSERT(rfs4_deleg_disabled > 0);
18967c478bd9Sstevel@tonic-gate 	rfs4_deleg_disabled--;
18977c478bd9Sstevel@tonic-gate 	mutex_exit(&rfs4_deleg_lock);
18987c478bd9Sstevel@tonic-gate }
18997c478bd9Sstevel@tonic-gate 
19007c478bd9Sstevel@tonic-gate void
19017c478bd9Sstevel@tonic-gate rfs4_mon_hold(void *arg)
19027c478bd9Sstevel@tonic-gate {
19037c478bd9Sstevel@tonic-gate 	rfs4_file_t *fp = arg;
19047c478bd9Sstevel@tonic-gate 
1905*d216dff5SRobert Mastors 	rfs4_dbe_hold(fp->rf_dbe);
19067c478bd9Sstevel@tonic-gate }
19077c478bd9Sstevel@tonic-gate 
19087c478bd9Sstevel@tonic-gate void
19097c478bd9Sstevel@tonic-gate rfs4_mon_rele(void *arg)
19107c478bd9Sstevel@tonic-gate {
19117c478bd9Sstevel@tonic-gate 	rfs4_file_t *fp = arg;
19127c478bd9Sstevel@tonic-gate 
1913*d216dff5SRobert Mastors 	rfs4_dbe_rele_nolock(fp->rf_dbe);
19147c478bd9Sstevel@tonic-gate }
1915