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
57c478bd9Sstevel@tonic-gate  * Common Development and Distribution License, Version 1.0 only
67c478bd9Sstevel@tonic-gate  * (the "License").  You may not use this file except in compliance
77c478bd9Sstevel@tonic-gate  * with the License.
87c478bd9Sstevel@tonic-gate  *
97c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
107c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
117c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
127c478bd9Sstevel@tonic-gate  * and limitations under the License.
137c478bd9Sstevel@tonic-gate  *
147c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
157c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
167c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
177c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
187c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
197c478bd9Sstevel@tonic-gate  *
207c478bd9Sstevel@tonic-gate  * CDDL HEADER END
217c478bd9Sstevel@tonic-gate  */
227c478bd9Sstevel@tonic-gate /*
23*eac3aab7Srobinson  * Copyright 2006 Sun Microsystems, Inc.  All rights reserved.
247c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
257c478bd9Sstevel@tonic-gate  */
267c478bd9Sstevel@tonic-gate 
277c478bd9Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
287c478bd9Sstevel@tonic-gate 
297c478bd9Sstevel@tonic-gate #include <sys/systm.h>
307c478bd9Sstevel@tonic-gate #include <rpc/auth.h>
317c478bd9Sstevel@tonic-gate #include <rpc/clnt.h>
327c478bd9Sstevel@tonic-gate #include <nfs/nfs4_kprot.h>
337c478bd9Sstevel@tonic-gate #include <nfs/nfs4.h>
347c478bd9Sstevel@tonic-gate #include <nfs/lm.h>
357c478bd9Sstevel@tonic-gate #include <sys/cmn_err.h>
367c478bd9Sstevel@tonic-gate #include <sys/disp.h>
377c478bd9Sstevel@tonic-gate 
387c478bd9Sstevel@tonic-gate #include <sys/pathname.h>
397c478bd9Sstevel@tonic-gate 
407c478bd9Sstevel@tonic-gate #include <sys/strsubr.h>
417c478bd9Sstevel@tonic-gate #include <sys/ddi.h>
427c478bd9Sstevel@tonic-gate 
437c478bd9Sstevel@tonic-gate #include <sys/vnode.h>
447c478bd9Sstevel@tonic-gate 
457c478bd9Sstevel@tonic-gate #include <inet/common.h>
467c478bd9Sstevel@tonic-gate #include <inet/ip.h>
477c478bd9Sstevel@tonic-gate #include <inet/ip6.h>
487c478bd9Sstevel@tonic-gate 
497c478bd9Sstevel@tonic-gate #define	MAX_READ_DELEGATIONS 5
507c478bd9Sstevel@tonic-gate 
517c478bd9Sstevel@tonic-gate krwlock_t rfs4_deleg_policy_lock;
527c478bd9Sstevel@tonic-gate srv_deleg_policy_t rfs4_deleg_policy = SRV_NEVER_DELEGATE;
537c478bd9Sstevel@tonic-gate static int rfs4_deleg_wlp = 5;
547c478bd9Sstevel@tonic-gate kmutex_t rfs4_deleg_lock;
557c478bd9Sstevel@tonic-gate static int rfs4_deleg_disabled;
567c478bd9Sstevel@tonic-gate 
577c478bd9Sstevel@tonic-gate #ifdef DEBUG
587c478bd9Sstevel@tonic-gate 
597c478bd9Sstevel@tonic-gate static int rfs4_test_cbgetattr_fail = 0;
607c478bd9Sstevel@tonic-gate int rfs4_cb_null;
617c478bd9Sstevel@tonic-gate int rfs4_cb_debug;
627c478bd9Sstevel@tonic-gate int rfs4_deleg_debug;
637c478bd9Sstevel@tonic-gate 
647c478bd9Sstevel@tonic-gate #endif
657c478bd9Sstevel@tonic-gate 
667c478bd9Sstevel@tonic-gate static void rfs4_recall_file(rfs4_file_t *,
677c478bd9Sstevel@tonic-gate 			    void (*recall)(rfs4_deleg_state_t *, bool_t),
687c478bd9Sstevel@tonic-gate 			    bool_t, rfs4_client_t *);
697c478bd9Sstevel@tonic-gate static	void		rfs4_revoke_deleg(rfs4_deleg_state_t *);
707c478bd9Sstevel@tonic-gate static	void		rfs4_revoke_file(rfs4_file_t *);
717c478bd9Sstevel@tonic-gate static	void		rfs4_cb_chflush(rfs4_cbinfo_t *);
727c478bd9Sstevel@tonic-gate static	CLIENT		*rfs4_cb_getch(rfs4_cbinfo_t *);
737c478bd9Sstevel@tonic-gate static	void		rfs4_cb_freech(rfs4_cbinfo_t *, CLIENT *, bool_t);
747c478bd9Sstevel@tonic-gate static rfs4_deleg_state_t *rfs4_deleg_state(rfs4_state_t *,
757c478bd9Sstevel@tonic-gate 				open_delegation_type4, int *);
767c478bd9Sstevel@tonic-gate 
777c478bd9Sstevel@tonic-gate /*
787c478bd9Sstevel@tonic-gate  * Convert a universal address to an transport specific
797c478bd9Sstevel@tonic-gate  * address using inet_pton.
807c478bd9Sstevel@tonic-gate  */
817c478bd9Sstevel@tonic-gate static int
827c478bd9Sstevel@tonic-gate uaddr2sockaddr(int af, char *ua, void *ap, in_port_t *pp)
837c478bd9Sstevel@tonic-gate {
847c478bd9Sstevel@tonic-gate 	int dots = 0, i, j, len, k;
857c478bd9Sstevel@tonic-gate 	unsigned char c;
867c478bd9Sstevel@tonic-gate 	in_port_t port = 0;
877c478bd9Sstevel@tonic-gate 
887c478bd9Sstevel@tonic-gate 	len = strlen(ua);
897c478bd9Sstevel@tonic-gate 
907c478bd9Sstevel@tonic-gate 	for (i = len-1; i >= 0; i--) {
917c478bd9Sstevel@tonic-gate 
927c478bd9Sstevel@tonic-gate 		if (ua[i] == '.')
937c478bd9Sstevel@tonic-gate 			dots++;
947c478bd9Sstevel@tonic-gate 
957c478bd9Sstevel@tonic-gate 		if (dots == 2) {
967c478bd9Sstevel@tonic-gate 
977c478bd9Sstevel@tonic-gate 			ua[i] = '\0';
987c478bd9Sstevel@tonic-gate 			/*
997c478bd9Sstevel@tonic-gate 			 * We use k to remember were to stick '.' back, since
1007c478bd9Sstevel@tonic-gate 			 * ua was kmem_allocateded from the pool len+1.
1017c478bd9Sstevel@tonic-gate 			 */
1027c478bd9Sstevel@tonic-gate 			k = i;
1037c478bd9Sstevel@tonic-gate 			if (inet_pton(af, ua, ap) == 1) {
1047c478bd9Sstevel@tonic-gate 
1057c478bd9Sstevel@tonic-gate 				c = 0;
1067c478bd9Sstevel@tonic-gate 
1077c478bd9Sstevel@tonic-gate 				for (j = i+1; j < len; j++) {
1087c478bd9Sstevel@tonic-gate 					if (ua[j] == '.') {
1097c478bd9Sstevel@tonic-gate 						port = c << 8;
1107c478bd9Sstevel@tonic-gate 						c = 0;
1117c478bd9Sstevel@tonic-gate 					} else if (ua[j] >= '0' &&
1127c478bd9Sstevel@tonic-gate 					    ua[j] <= '9') {
1137c478bd9Sstevel@tonic-gate 						c *= 10;
1147c478bd9Sstevel@tonic-gate 						c += ua[j] - '0';
1157c478bd9Sstevel@tonic-gate 					} else {
1167c478bd9Sstevel@tonic-gate 						ua[k] = '.';
1177c478bd9Sstevel@tonic-gate 						return (EINVAL);
1187c478bd9Sstevel@tonic-gate 					}
1197c478bd9Sstevel@tonic-gate 				}
1207c478bd9Sstevel@tonic-gate 				port += c;
1217c478bd9Sstevel@tonic-gate 
1227c478bd9Sstevel@tonic-gate 
1237c478bd9Sstevel@tonic-gate 				/* reset to network order */
1247c478bd9Sstevel@tonic-gate 				if (af == AF_INET) {
1257c478bd9Sstevel@tonic-gate 					*(uint32_t *)ap =
1267c478bd9Sstevel@tonic-gate 						htonl(*(uint32_t *)ap);
1277c478bd9Sstevel@tonic-gate 					*pp = htons(port);
1287c478bd9Sstevel@tonic-gate 				} else {
1297c478bd9Sstevel@tonic-gate 					int ix;
1307c478bd9Sstevel@tonic-gate 					uint16_t *sap;
1317c478bd9Sstevel@tonic-gate 
1327c478bd9Sstevel@tonic-gate 					for (sap = ap, ix = 0; ix <
1337c478bd9Sstevel@tonic-gate 					    sizeof (struct in6_addr) /
1347c478bd9Sstevel@tonic-gate 					    sizeof (uint16_t); ix++)
1357c478bd9Sstevel@tonic-gate 						sap[ix] = htons(sap[ix]);
1367c478bd9Sstevel@tonic-gate 
1377c478bd9Sstevel@tonic-gate 					*pp = htons(port);
1387c478bd9Sstevel@tonic-gate 				}
1397c478bd9Sstevel@tonic-gate 
1407c478bd9Sstevel@tonic-gate 				ua[k] = '.';
1417c478bd9Sstevel@tonic-gate 				return (0);
1427c478bd9Sstevel@tonic-gate 			} else {
1437c478bd9Sstevel@tonic-gate 				ua[k] = '.';
1447c478bd9Sstevel@tonic-gate 				return (EINVAL);
1457c478bd9Sstevel@tonic-gate 			}
1467c478bd9Sstevel@tonic-gate 		}
1477c478bd9Sstevel@tonic-gate 	}
1487c478bd9Sstevel@tonic-gate 
1497c478bd9Sstevel@tonic-gate 	return (EINVAL);
1507c478bd9Sstevel@tonic-gate }
1517c478bd9Sstevel@tonic-gate 
1527c478bd9Sstevel@tonic-gate /*
1537c478bd9Sstevel@tonic-gate  * Update the delegation policy with the
1547c478bd9Sstevel@tonic-gate  * value of "new_policy"
1557c478bd9Sstevel@tonic-gate  */
1567c478bd9Sstevel@tonic-gate void
1577c478bd9Sstevel@tonic-gate rfs4_set_deleg_policy(srv_deleg_policy_t new_policy)
1587c478bd9Sstevel@tonic-gate {
1597c478bd9Sstevel@tonic-gate 	rw_enter(&rfs4_deleg_policy_lock, RW_WRITER);
1607c478bd9Sstevel@tonic-gate 	rfs4_deleg_policy = new_policy;
1617c478bd9Sstevel@tonic-gate 	rw_exit(&rfs4_deleg_policy_lock);
1627c478bd9Sstevel@tonic-gate }
1637c478bd9Sstevel@tonic-gate 
1647c478bd9Sstevel@tonic-gate void
1657c478bd9Sstevel@tonic-gate rfs4_hold_deleg_policy(void)
1667c478bd9Sstevel@tonic-gate {
1677c478bd9Sstevel@tonic-gate 	rw_enter(&rfs4_deleg_policy_lock, RW_READER);
1687c478bd9Sstevel@tonic-gate }
1697c478bd9Sstevel@tonic-gate 
1707c478bd9Sstevel@tonic-gate void
1717c478bd9Sstevel@tonic-gate rfs4_rele_deleg_policy(void)
1727c478bd9Sstevel@tonic-gate {
1737c478bd9Sstevel@tonic-gate 	rw_exit(&rfs4_deleg_policy_lock);
1747c478bd9Sstevel@tonic-gate }
1757c478bd9Sstevel@tonic-gate 
1767c478bd9Sstevel@tonic-gate 
1777c478bd9Sstevel@tonic-gate /*
1787c478bd9Sstevel@tonic-gate  * This free function is to be used when the client struct is being
1797c478bd9Sstevel@tonic-gate  * released and nothing at all is needed of the callback info any
1807c478bd9Sstevel@tonic-gate  * longer.
1817c478bd9Sstevel@tonic-gate  */
1827c478bd9Sstevel@tonic-gate void
1837c478bd9Sstevel@tonic-gate rfs4_cbinfo_free(rfs4_cbinfo_t *cbp)
1847c478bd9Sstevel@tonic-gate {
1857c478bd9Sstevel@tonic-gate 	char *addr = cbp->cb_callback.cb_location.r_addr;
1867c478bd9Sstevel@tonic-gate 	char *netid = cbp->cb_callback.cb_location.r_netid;
1877c478bd9Sstevel@tonic-gate 
1887c478bd9Sstevel@tonic-gate 	/* Free old address if any */
1897c478bd9Sstevel@tonic-gate 
1907c478bd9Sstevel@tonic-gate 	if (addr)
1917c478bd9Sstevel@tonic-gate 		kmem_free(addr, strlen(addr) + 1);
1927c478bd9Sstevel@tonic-gate 	if (netid)
1937c478bd9Sstevel@tonic-gate 		kmem_free(netid, strlen(netid) + 1);
1947c478bd9Sstevel@tonic-gate 
1957c478bd9Sstevel@tonic-gate 	addr = cbp->cb_newer.cb_callback.cb_location.r_addr;
1967c478bd9Sstevel@tonic-gate 	netid = cbp->cb_newer.cb_callback.cb_location.r_netid;
1977c478bd9Sstevel@tonic-gate 
1987c478bd9Sstevel@tonic-gate 	if (addr)
1997c478bd9Sstevel@tonic-gate 		kmem_free(addr, strlen(addr) + 1);
2007c478bd9Sstevel@tonic-gate 	if (netid)
2017c478bd9Sstevel@tonic-gate 		kmem_free(netid, strlen(netid) + 1);
2027c478bd9Sstevel@tonic-gate 
2037c478bd9Sstevel@tonic-gate 	if (cbp->cb_chc_free) {
2047c478bd9Sstevel@tonic-gate 		rfs4_cb_chflush(cbp);
2057c478bd9Sstevel@tonic-gate 	}
2067c478bd9Sstevel@tonic-gate }
2077c478bd9Sstevel@tonic-gate 
2087c478bd9Sstevel@tonic-gate /*
2097c478bd9Sstevel@tonic-gate  * The server uses this to check the callback path supplied by the
2107c478bd9Sstevel@tonic-gate  * client.  The callback connection is marked "in progress" while this
2117c478bd9Sstevel@tonic-gate  * work is going on and then eventually marked either OK or FAILED.
2127c478bd9Sstevel@tonic-gate  * This work can be done as part of a separate thread and at the end
2137c478bd9Sstevel@tonic-gate  * of this the thread will exit or it may be done such that the caller
2147c478bd9Sstevel@tonic-gate  * will continue with other work.
2157c478bd9Sstevel@tonic-gate  */
2167c478bd9Sstevel@tonic-gate static void
2177c478bd9Sstevel@tonic-gate rfs4_do_cb_null(rfs4_client_t *cp)
2187c478bd9Sstevel@tonic-gate {
2197c478bd9Sstevel@tonic-gate 	struct timeval tv;
2207c478bd9Sstevel@tonic-gate 	CLIENT *ch;
2217c478bd9Sstevel@tonic-gate 	rfs4_cbstate_t newstate;
2227c478bd9Sstevel@tonic-gate 	rfs4_cbinfo_t *cbp = &cp->cbinfo;
2237c478bd9Sstevel@tonic-gate 
2247c478bd9Sstevel@tonic-gate 	if (cp == NULL) {
2257c478bd9Sstevel@tonic-gate 		NFS4_DEBUG(rfs4_cb_debug, (CE_NOTE,
2267c478bd9Sstevel@tonic-gate 			"rfs4_do_cb_null: no rfs4_client specified\n"));
2277c478bd9Sstevel@tonic-gate 		return;
2287c478bd9Sstevel@tonic-gate 	}
2297c478bd9Sstevel@tonic-gate 
2307c478bd9Sstevel@tonic-gate 	mutex_enter(cbp->cb_lock);
2317c478bd9Sstevel@tonic-gate 	/* If another thread is doing CB_NULL RPC then return */
2327c478bd9Sstevel@tonic-gate 	if (cbp->cb_nullcaller == TRUE) {
2337c478bd9Sstevel@tonic-gate 		mutex_exit(cbp->cb_lock);
2347c478bd9Sstevel@tonic-gate 		rfs4_client_rele(cp);
2357c478bd9Sstevel@tonic-gate 		return;
2367c478bd9Sstevel@tonic-gate 	}
2377c478bd9Sstevel@tonic-gate 
2387c478bd9Sstevel@tonic-gate 	/* Mark the cbinfo as having a thread in the NULL callback */
2397c478bd9Sstevel@tonic-gate 	cbp->cb_nullcaller = TRUE;
2407c478bd9Sstevel@tonic-gate 
2417c478bd9Sstevel@tonic-gate 	/*
2427c478bd9Sstevel@tonic-gate 	 * Are there other threads still using the cbinfo client
2437c478bd9Sstevel@tonic-gate 	 * handles?  If so, this thread must wait before going and
2447c478bd9Sstevel@tonic-gate 	 * mucking aroiund with the callback information
2457c478bd9Sstevel@tonic-gate 	 */
246a31a5cfdSsamf 	while (cbp->cb_refcnt != 0)
2477c478bd9Sstevel@tonic-gate 		cv_wait(cbp->cb_cv_nullcaller, cbp->cb_lock);
2487c478bd9Sstevel@tonic-gate 
2497c478bd9Sstevel@tonic-gate 	/*
2507c478bd9Sstevel@tonic-gate 	 * This thread itself may find that new callback info has
2517c478bd9Sstevel@tonic-gate 	 * arrived and is set up to handle this case and redrive the
2527c478bd9Sstevel@tonic-gate 	 * call to the client's callback server.
2537c478bd9Sstevel@tonic-gate 	 */
2547c478bd9Sstevel@tonic-gate retry:
2557c478bd9Sstevel@tonic-gate 	if (cbp->cb_newer.cb_new == TRUE &&
2567c478bd9Sstevel@tonic-gate 	    cbp->cb_newer.cb_confirmed == TRUE) {
2577c478bd9Sstevel@tonic-gate 		char *addr = cbp->cb_callback.cb_location.r_addr;
2587c478bd9Sstevel@tonic-gate 		char *netid = cbp->cb_callback.cb_location.r_netid;
2597c478bd9Sstevel@tonic-gate 
2607c478bd9Sstevel@tonic-gate 		/*
2617c478bd9Sstevel@tonic-gate 		 * Free the old stuff if it exists; may be the first
2627c478bd9Sstevel@tonic-gate 		 * time through this path
2637c478bd9Sstevel@tonic-gate 		 */
2647c478bd9Sstevel@tonic-gate 		if (addr)
2657c478bd9Sstevel@tonic-gate 			kmem_free(addr, strlen(addr) + 1);
2667c478bd9Sstevel@tonic-gate 		if (netid)
2677c478bd9Sstevel@tonic-gate 			kmem_free(netid, strlen(netid) + 1);
2687c478bd9Sstevel@tonic-gate 
2697c478bd9Sstevel@tonic-gate 		/* Move over the addr/netid */
2707c478bd9Sstevel@tonic-gate 		cbp->cb_callback.cb_location.r_addr =
2717c478bd9Sstevel@tonic-gate 			cbp->cb_newer.cb_callback.cb_location.r_addr;
2727c478bd9Sstevel@tonic-gate 		cbp->cb_newer.cb_callback.cb_location.r_addr = NULL;
2737c478bd9Sstevel@tonic-gate 		cbp->cb_callback.cb_location.r_netid =
2747c478bd9Sstevel@tonic-gate 			cbp->cb_newer.cb_callback.cb_location.r_netid;
2757c478bd9Sstevel@tonic-gate 		cbp->cb_newer.cb_callback.cb_location.r_netid = NULL;
2767c478bd9Sstevel@tonic-gate 
2777c478bd9Sstevel@tonic-gate 		/* Get the program number */
2787c478bd9Sstevel@tonic-gate 		cbp->cb_callback.cb_program =
2797c478bd9Sstevel@tonic-gate 			cbp->cb_newer.cb_callback.cb_program;
2807c478bd9Sstevel@tonic-gate 		cbp->cb_newer.cb_callback.cb_program = 0;
2817c478bd9Sstevel@tonic-gate 
2827c478bd9Sstevel@tonic-gate 		/* Don't forget the protocol's "cb_ident" field */
2837c478bd9Sstevel@tonic-gate 		cbp->cb_ident = cbp->cb_newer.cb_ident;
2847c478bd9Sstevel@tonic-gate 		cbp->cb_newer.cb_ident = 0;
2857c478bd9Sstevel@tonic-gate 
2867c478bd9Sstevel@tonic-gate 		/* no longer new */
2877c478bd9Sstevel@tonic-gate 		cbp->cb_newer.cb_new = FALSE;
2887c478bd9Sstevel@tonic-gate 		cbp->cb_newer.cb_confirmed = FALSE;
2897c478bd9Sstevel@tonic-gate 
2907c478bd9Sstevel@tonic-gate 		/* get rid of the old client handles that may exist */
2917c478bd9Sstevel@tonic-gate 		rfs4_cb_chflush(cbp);
2927c478bd9Sstevel@tonic-gate 
2937c478bd9Sstevel@tonic-gate 		cbp->cb_state = CB_NONE;
2947c478bd9Sstevel@tonic-gate 		cbp->cb_timefailed = 0; /* reset the clock */
2957c478bd9Sstevel@tonic-gate 		cbp->cb_notified_of_cb_path_down = TRUE;
2967c478bd9Sstevel@tonic-gate 	}
2977c478bd9Sstevel@tonic-gate 
2987c478bd9Sstevel@tonic-gate 	if (cbp->cb_state != CB_NONE) {
2997c478bd9Sstevel@tonic-gate 		cv_broadcast(cbp->cb_cv);	/* let the others know */
3007c478bd9Sstevel@tonic-gate 		cbp->cb_nullcaller = FALSE;
3017c478bd9Sstevel@tonic-gate 		mutex_exit(cbp->cb_lock);
3027c478bd9Sstevel@tonic-gate 		rfs4_client_rele(cp);
3037c478bd9Sstevel@tonic-gate 		return;
3047c478bd9Sstevel@tonic-gate 	}
3057c478bd9Sstevel@tonic-gate 
3067c478bd9Sstevel@tonic-gate 	/* mark rfs4_client_t as CALLBACK NULL in progress */
3077c478bd9Sstevel@tonic-gate 	cbp->cb_state = CB_INPROG;
3087c478bd9Sstevel@tonic-gate 	mutex_exit(cbp->cb_lock);
3097c478bd9Sstevel@tonic-gate 
3107c478bd9Sstevel@tonic-gate 	/* get/generate a client handle */
3117c478bd9Sstevel@tonic-gate 	if ((ch = rfs4_cb_getch(cbp)) == NULL) {
3127c478bd9Sstevel@tonic-gate 		NFS4_DEBUG(rfs4_cb_debug, (CE_NOTE,
3137c478bd9Sstevel@tonic-gate 			"rfs4_do_cb_null: failed to get client handle\n"));
3147c478bd9Sstevel@tonic-gate 		mutex_enter(cbp->cb_lock);
3157c478bd9Sstevel@tonic-gate 		cbp->cb_state = CB_BAD;
3167c478bd9Sstevel@tonic-gate 		cbp->cb_timefailed = gethrestime_sec(); /* observability */
3177c478bd9Sstevel@tonic-gate 		goto retry;
3187c478bd9Sstevel@tonic-gate 	}
3197c478bd9Sstevel@tonic-gate 
3207c478bd9Sstevel@tonic-gate 
3217c478bd9Sstevel@tonic-gate 	tv.tv_sec = 30;
3227c478bd9Sstevel@tonic-gate 	tv.tv_usec = 0;
3237c478bd9Sstevel@tonic-gate 	if (clnt_call(ch, CB_NULL, xdr_void, NULL, xdr_void, NULL, tv) != 0) {
3247c478bd9Sstevel@tonic-gate 		NFS4_DEBUG(rfs4_cb_debug, (CE_NOTE,
3257c478bd9Sstevel@tonic-gate 			"rfs4_do_cb_null: clnt_call failed\n"));
3267c478bd9Sstevel@tonic-gate 
3277c478bd9Sstevel@tonic-gate 		newstate = CB_BAD;
3287c478bd9Sstevel@tonic-gate 	} else {
3297c478bd9Sstevel@tonic-gate 		newstate = CB_OK;
3307c478bd9Sstevel@tonic-gate #ifdef	DEBUG
3317c478bd9Sstevel@tonic-gate 		rfs4_cb_null++;
3327c478bd9Sstevel@tonic-gate #endif
3337c478bd9Sstevel@tonic-gate 	}
3347c478bd9Sstevel@tonic-gate 
3357c478bd9Sstevel@tonic-gate 	/* Check to see if the client has specified new callback info */
3367c478bd9Sstevel@tonic-gate 	mutex_enter(cbp->cb_lock);
3377c478bd9Sstevel@tonic-gate 	rfs4_cb_freech(cbp, ch, TRUE);
3387c478bd9Sstevel@tonic-gate 	if (cbp->cb_newer.cb_new == TRUE &&
3397c478bd9Sstevel@tonic-gate 	    cbp->cb_newer.cb_confirmed == TRUE) {
3407c478bd9Sstevel@tonic-gate 		goto retry;	/* give the CB_NULL another chance */
3417c478bd9Sstevel@tonic-gate 	}
3427c478bd9Sstevel@tonic-gate 
3437c478bd9Sstevel@tonic-gate 	cbp->cb_state = newstate;
3447c478bd9Sstevel@tonic-gate 	if (cbp->cb_state == CB_BAD)
3457c478bd9Sstevel@tonic-gate 		cbp->cb_timefailed = gethrestime_sec(); /* observability */
3467c478bd9Sstevel@tonic-gate 
3477c478bd9Sstevel@tonic-gate 	cv_broadcast(cbp->cb_cv);	/* start up the other threads */
3487c478bd9Sstevel@tonic-gate 	cbp->cb_nullcaller = FALSE;
3497c478bd9Sstevel@tonic-gate 	mutex_exit(cbp->cb_lock);
3507c478bd9Sstevel@tonic-gate 
3517c478bd9Sstevel@tonic-gate 	rfs4_client_rele(cp);
3527c478bd9Sstevel@tonic-gate }
3537c478bd9Sstevel@tonic-gate 
3547c478bd9Sstevel@tonic-gate /*
3557c478bd9Sstevel@tonic-gate  * Given a client struct, inspect the callback info to see if the
3567c478bd9Sstevel@tonic-gate  * callback path is up and available.  If it is being initialized,
3577c478bd9Sstevel@tonic-gate  * then wait for the CB_NULL RPC call to occur.
3587c478bd9Sstevel@tonic-gate  */
3597c478bd9Sstevel@tonic-gate static rfs4_cbinfo_t *
3607c478bd9Sstevel@tonic-gate rfs4_cbinfo_hold(rfs4_client_t *cp)
3617c478bd9Sstevel@tonic-gate {
3627c478bd9Sstevel@tonic-gate 	rfs4_cbinfo_t *cbp = &cp->cbinfo;
3637c478bd9Sstevel@tonic-gate 
3647c478bd9Sstevel@tonic-gate retry:
3657c478bd9Sstevel@tonic-gate 	mutex_enter(cbp->cb_lock);
3667c478bd9Sstevel@tonic-gate 
3677c478bd9Sstevel@tonic-gate 	if (cbp->cb_newer.cb_new == TRUE && cbp->cb_nullcaller == FALSE) {
3687c478bd9Sstevel@tonic-gate 		/*
3697c478bd9Sstevel@tonic-gate 		 * Looks like a new callback path may be available and
3707c478bd9Sstevel@tonic-gate 		 * noone has set it up.
3717c478bd9Sstevel@tonic-gate 		 */
3727c478bd9Sstevel@tonic-gate 		mutex_exit(cbp->cb_lock);
3737c478bd9Sstevel@tonic-gate 		rfs4_dbe_hold(cp->dbe);
3747c478bd9Sstevel@tonic-gate 		rfs4_do_cb_null(cp); /* caller will release client hold */
3757c478bd9Sstevel@tonic-gate 		goto retry;
3767c478bd9Sstevel@tonic-gate 	}
3777c478bd9Sstevel@tonic-gate 
3787c478bd9Sstevel@tonic-gate 	/* Is there a thread working on doing the CB_NULL RPC? */
3797c478bd9Sstevel@tonic-gate 	if (cbp->cb_nullcaller == TRUE)
3807c478bd9Sstevel@tonic-gate 		cv_wait(cbp->cb_cv, cbp->cb_lock);  /* if so, wait on it */
3817c478bd9Sstevel@tonic-gate 
3827c478bd9Sstevel@tonic-gate 	/* If the callback path is not okay (up and running), just quit */
3837c478bd9Sstevel@tonic-gate 	if (cbp->cb_state != CB_OK) {
3847c478bd9Sstevel@tonic-gate 		mutex_exit(cbp->cb_lock);
3857c478bd9Sstevel@tonic-gate 		return (NULL);
3867c478bd9Sstevel@tonic-gate 	}
3877c478bd9Sstevel@tonic-gate 
3887c478bd9Sstevel@tonic-gate 	/* Let someone know we are using the current callback info */
3897c478bd9Sstevel@tonic-gate 	cbp->cb_refcnt++;
3907c478bd9Sstevel@tonic-gate 	mutex_exit(cbp->cb_lock);
3917c478bd9Sstevel@tonic-gate 	return (cbp);
3927c478bd9Sstevel@tonic-gate }
3937c478bd9Sstevel@tonic-gate 
3947c478bd9Sstevel@tonic-gate /*
3957c478bd9Sstevel@tonic-gate  * The caller is done with the callback info.  It may be that the
3967c478bd9Sstevel@tonic-gate  * caller's RPC failed and the NFSv4 client has actually provided new
3977c478bd9Sstevel@tonic-gate  * callback information.  If so, let the caller know so they can
3987c478bd9Sstevel@tonic-gate  * advantage of this and maybe retry the RPC that originally failed.
3997c478bd9Sstevel@tonic-gate  */
4007c478bd9Sstevel@tonic-gate static int
4017c478bd9Sstevel@tonic-gate rfs4_cbinfo_rele(rfs4_cbinfo_t *cbp, rfs4_cbstate_t newstate)
4027c478bd9Sstevel@tonic-gate {
4037c478bd9Sstevel@tonic-gate 	int cb_new = FALSE;
4047c478bd9Sstevel@tonic-gate 
4057c478bd9Sstevel@tonic-gate 	mutex_enter(cbp->cb_lock);
4067c478bd9Sstevel@tonic-gate 
4077c478bd9Sstevel@tonic-gate 	/* The caller gets a chance to mark the callback info as bad */
4087c478bd9Sstevel@tonic-gate 	if (newstate != CB_NOCHANGE)
4097c478bd9Sstevel@tonic-gate 		cbp->cb_state = newstate;
4107c478bd9Sstevel@tonic-gate 	if (newstate == CB_FAILED) {
4117c478bd9Sstevel@tonic-gate 		cbp->cb_timefailed = gethrestime_sec(); /* observability */
4127c478bd9Sstevel@tonic-gate 		cbp->cb_notified_of_cb_path_down = FALSE;
4137c478bd9Sstevel@tonic-gate 	}
4147c478bd9Sstevel@tonic-gate 
4157c478bd9Sstevel@tonic-gate 	cbp->cb_refcnt--;	/* no longer using the information */
4167c478bd9Sstevel@tonic-gate 
4177c478bd9Sstevel@tonic-gate 	/*
4187c478bd9Sstevel@tonic-gate 	 * A thread may be waiting on this one to finish and if so,
4197c478bd9Sstevel@tonic-gate 	 * let it know that it is okay to do the CB_NULL to the
4207c478bd9Sstevel@tonic-gate 	 * client's callback server.
4217c478bd9Sstevel@tonic-gate 	 */
4227c478bd9Sstevel@tonic-gate 	if (cbp->cb_refcnt == 0 && cbp->cb_nullcaller)
4237c478bd9Sstevel@tonic-gate 		cv_broadcast(cbp->cb_cv_nullcaller);
4247c478bd9Sstevel@tonic-gate 
4257c478bd9Sstevel@tonic-gate 	/*
4267c478bd9Sstevel@tonic-gate 	 * If this is the last thread to use the callback info and
4277c478bd9Sstevel@tonic-gate 	 * there is new callback information to try and no thread is
4287c478bd9Sstevel@tonic-gate 	 * there ready to do the CB_NULL, then return true to teh
4297c478bd9Sstevel@tonic-gate 	 * caller so they can do the CB_NULL
4307c478bd9Sstevel@tonic-gate 	 */
4317c478bd9Sstevel@tonic-gate 	if (cbp->cb_refcnt == 0 &&
4327c478bd9Sstevel@tonic-gate 		cbp->cb_nullcaller == FALSE &&
4337c478bd9Sstevel@tonic-gate 		cbp->cb_newer.cb_new == TRUE &&
4347c478bd9Sstevel@tonic-gate 		cbp->cb_newer.cb_confirmed == TRUE)
4357c478bd9Sstevel@tonic-gate 		cb_new = TRUE;
4367c478bd9Sstevel@tonic-gate 
4377c478bd9Sstevel@tonic-gate 	mutex_exit(cbp->cb_lock);
4387c478bd9Sstevel@tonic-gate 
4397c478bd9Sstevel@tonic-gate 	return (cb_new);
4407c478bd9Sstevel@tonic-gate }
4417c478bd9Sstevel@tonic-gate 
4427c478bd9Sstevel@tonic-gate /*
4437c478bd9Sstevel@tonic-gate  * Given the information in the callback info struct, create a client
4447c478bd9Sstevel@tonic-gate  * handle that can be used by the server for its callback path.
4457c478bd9Sstevel@tonic-gate  */
4467c478bd9Sstevel@tonic-gate static CLIENT *
4477c478bd9Sstevel@tonic-gate rfs4_cbch_init(rfs4_cbinfo_t *cbp)
4487c478bd9Sstevel@tonic-gate {
4497c478bd9Sstevel@tonic-gate 	struct knetconfig knc;
4507c478bd9Sstevel@tonic-gate 	vnode_t *vp;
4517c478bd9Sstevel@tonic-gate 	struct sockaddr_in addr4;
4527c478bd9Sstevel@tonic-gate 	struct sockaddr_in6 addr6;
4537c478bd9Sstevel@tonic-gate 	void *addr, *taddr;
4547c478bd9Sstevel@tonic-gate 	in_port_t *pp;
4557c478bd9Sstevel@tonic-gate 	int af;
4567c478bd9Sstevel@tonic-gate 	char *devnam;
4577c478bd9Sstevel@tonic-gate 	int err = 0;
4587c478bd9Sstevel@tonic-gate 	struct netbuf nb;
4597c478bd9Sstevel@tonic-gate 	int size;
4607c478bd9Sstevel@tonic-gate 	CLIENT *ch = NULL;
4617c478bd9Sstevel@tonic-gate 	int useresvport = 0;
4627c478bd9Sstevel@tonic-gate 
4637c478bd9Sstevel@tonic-gate 	NFS4_DEBUG(rfs4_cb_debug, (CE_NOTE,
4647c478bd9Sstevel@tonic-gate 		"rfs4_cbch_init: entry cbp->%p\n", (void *)cbp));
4657c478bd9Sstevel@tonic-gate 
4667c478bd9Sstevel@tonic-gate 	mutex_enter(cbp->cb_lock);
4677c478bd9Sstevel@tonic-gate 
4687c478bd9Sstevel@tonic-gate 	if (cbp->cb_callback.cb_location.r_netid == NULL ||
4697c478bd9Sstevel@tonic-gate 	    cbp->cb_callback.cb_location.r_addr == NULL) {
4707c478bd9Sstevel@tonic-gate 		goto cb_init_out;
4717c478bd9Sstevel@tonic-gate 	}
4727c478bd9Sstevel@tonic-gate 
4737c478bd9Sstevel@tonic-gate 	if (strcmp(cbp->cb_callback.cb_location.r_netid, "tcp") == 0) {
4747c478bd9Sstevel@tonic-gate 		knc.knc_semantics = NC_TPI_COTS;
4757c478bd9Sstevel@tonic-gate 		knc.knc_protofmly = "inet";
4767c478bd9Sstevel@tonic-gate 		knc.knc_proto = "tcp";
4777c478bd9Sstevel@tonic-gate 		devnam = "/dev/tcp";
4787c478bd9Sstevel@tonic-gate 		af = AF_INET;
4797c478bd9Sstevel@tonic-gate 	} else if (strcmp(cbp->cb_callback.cb_location.r_netid, "udp")
4807c478bd9Sstevel@tonic-gate 		== 0) {
4817c478bd9Sstevel@tonic-gate 		knc.knc_semantics = NC_TPI_CLTS;
4827c478bd9Sstevel@tonic-gate 		knc.knc_protofmly = "inet";
4837c478bd9Sstevel@tonic-gate 		knc.knc_proto = "udp";
4847c478bd9Sstevel@tonic-gate 		devnam = "/dev/udp";
4857c478bd9Sstevel@tonic-gate 		af = AF_INET;
4867c478bd9Sstevel@tonic-gate 	} else if (strcmp(cbp->cb_callback.cb_location.r_netid, "tcp6")
4877c478bd9Sstevel@tonic-gate 		== 0) {
4887c478bd9Sstevel@tonic-gate 		knc.knc_semantics = NC_TPI_COTS;
4897c478bd9Sstevel@tonic-gate 		knc.knc_protofmly = "inet6";
4907c478bd9Sstevel@tonic-gate 		knc.knc_proto = "tcp";
4917c478bd9Sstevel@tonic-gate 		devnam = "/dev/tcp6";
4927c478bd9Sstevel@tonic-gate 		af = AF_INET6;
4937c478bd9Sstevel@tonic-gate 	} else if (strcmp(cbp->cb_callback.cb_location.r_netid, "udp6")
4947c478bd9Sstevel@tonic-gate 		== 0) {
4957c478bd9Sstevel@tonic-gate 		knc.knc_semantics = NC_TPI_CLTS;
4967c478bd9Sstevel@tonic-gate 		knc.knc_protofmly = "inet6";
4977c478bd9Sstevel@tonic-gate 		knc.knc_proto = "udp";
4987c478bd9Sstevel@tonic-gate 		devnam = "/dev/udp6";
4997c478bd9Sstevel@tonic-gate 		af = AF_INET6;
5007c478bd9Sstevel@tonic-gate 	} else {
5017c478bd9Sstevel@tonic-gate 		NFS4_DEBUG(rfs4_cb_debug, (CE_NOTE,
5027c478bd9Sstevel@tonic-gate 			"rfs4_cbch_init: unknown transport %s\n",
5037c478bd9Sstevel@tonic-gate 			cbp->cb_callback.cb_location.r_netid));
5047c478bd9Sstevel@tonic-gate 
5057c478bd9Sstevel@tonic-gate 		goto cb_init_out;
5067c478bd9Sstevel@tonic-gate 	}
5077c478bd9Sstevel@tonic-gate 
5087c478bd9Sstevel@tonic-gate 	if ((err = lookupname(devnam, UIO_SYSSPACE, FOLLOW,
5097c478bd9Sstevel@tonic-gate 	    NULLVPP, &vp)) != 0) {
5107c478bd9Sstevel@tonic-gate 
5117c478bd9Sstevel@tonic-gate 		NFS4_DEBUG(rfs4_cb_debug, (CE_NOTE,
5127c478bd9Sstevel@tonic-gate 			"rfs4_cbch_init: lookupname failed %d\n", err));
5137c478bd9Sstevel@tonic-gate 
5147c478bd9Sstevel@tonic-gate 		goto cb_init_out;
5157c478bd9Sstevel@tonic-gate 	}
5167c478bd9Sstevel@tonic-gate 
5177c478bd9Sstevel@tonic-gate 	if (vp->v_type != VCHR) {
5187c478bd9Sstevel@tonic-gate 
5197c478bd9Sstevel@tonic-gate 		NFS4_DEBUG(rfs4_cb_debug, (CE_NOTE,
5207c478bd9Sstevel@tonic-gate 			"rfs4_cbch_init: %s is not VCHR", devnam));
5217c478bd9Sstevel@tonic-gate 		VN_RELE(vp);
5227c478bd9Sstevel@tonic-gate 		goto cb_init_out;
5237c478bd9Sstevel@tonic-gate 	}
5247c478bd9Sstevel@tonic-gate 
5257c478bd9Sstevel@tonic-gate 	knc.knc_rdev = vp->v_rdev;
5267c478bd9Sstevel@tonic-gate 
5277c478bd9Sstevel@tonic-gate 	VN_RELE(vp);
5287c478bd9Sstevel@tonic-gate 
5297c478bd9Sstevel@tonic-gate 	if (af == AF_INET) {
5307c478bd9Sstevel@tonic-gate 		size = sizeof (addr4);
5317c478bd9Sstevel@tonic-gate 		bzero(&addr4, size);
5327c478bd9Sstevel@tonic-gate 		addr4.sin_family = (sa_family_t)af;
5337c478bd9Sstevel@tonic-gate 		addr = &addr4.sin_addr;
5347c478bd9Sstevel@tonic-gate 		pp = &addr4.sin_port;
5357c478bd9Sstevel@tonic-gate 		taddr = &addr4;
5367c478bd9Sstevel@tonic-gate 	} else /* AF_INET6 */ {
5377c478bd9Sstevel@tonic-gate 		size = sizeof (addr6);
5387c478bd9Sstevel@tonic-gate 		bzero(&addr6, size);
5397c478bd9Sstevel@tonic-gate 		addr6.sin6_family = (sa_family_t)af;
5407c478bd9Sstevel@tonic-gate 		addr = &addr6.sin6_addr;
5417c478bd9Sstevel@tonic-gate 		pp = &addr6.sin6_port;
5427c478bd9Sstevel@tonic-gate 		taddr = &addr6;
5437c478bd9Sstevel@tonic-gate 	}
5447c478bd9Sstevel@tonic-gate 
5457c478bd9Sstevel@tonic-gate 	if (uaddr2sockaddr(af,
5467c478bd9Sstevel@tonic-gate 		cbp->cb_callback.cb_location.r_addr, addr, pp)) {
5477c478bd9Sstevel@tonic-gate 
5487c478bd9Sstevel@tonic-gate 		NFS4_DEBUG(rfs4_cb_debug, (CE_NOTE,
5497c478bd9Sstevel@tonic-gate 			"rfs4_cbch_init: malformed universal addr: %s\n",
5507c478bd9Sstevel@tonic-gate 			cbp->cb_callback.cb_location.r_addr));
5517c478bd9Sstevel@tonic-gate 
5527c478bd9Sstevel@tonic-gate 		goto cb_init_out;
5537c478bd9Sstevel@tonic-gate 	}
5547c478bd9Sstevel@tonic-gate 
5557c478bd9Sstevel@tonic-gate 
5567c478bd9Sstevel@tonic-gate 	nb.maxlen = nb.len = size;
5577c478bd9Sstevel@tonic-gate 	nb.buf = (char *)taddr;
5587c478bd9Sstevel@tonic-gate 
5597c478bd9Sstevel@tonic-gate 	if (err = clnt_tli_kcreate(&knc, &nb, cbp->cb_callback.cb_program,
5607c478bd9Sstevel@tonic-gate 	    NFS_CB, 0, 0, curthread->t_cred, &ch)) {
5617c478bd9Sstevel@tonic-gate 
5627c478bd9Sstevel@tonic-gate 		NFS4_DEBUG(rfs4_cb_debug, (CE_NOTE,
5637c478bd9Sstevel@tonic-gate 			"rfs4_cbch_init: clnt_tli_kcreate failed %d\n", err));
5647c478bd9Sstevel@tonic-gate 		ch = NULL;
5657c478bd9Sstevel@tonic-gate 	}
5667c478bd9Sstevel@tonic-gate 
5677c478bd9Sstevel@tonic-gate 	/* turn off reserved port usage */
5687c478bd9Sstevel@tonic-gate 	(void) CLNT_CONTROL(ch, CLSET_BINDRESVPORT, (char *)&useresvport);
5697c478bd9Sstevel@tonic-gate 
5707c478bd9Sstevel@tonic-gate cb_init_out:
5717c478bd9Sstevel@tonic-gate 	mutex_exit(cbp->cb_lock);
5727c478bd9Sstevel@tonic-gate 	return (ch);
5737c478bd9Sstevel@tonic-gate }
5747c478bd9Sstevel@tonic-gate 
5757c478bd9Sstevel@tonic-gate /*
5767c478bd9Sstevel@tonic-gate  * Iterate over the client handle cache and
5777c478bd9Sstevel@tonic-gate  * destroy it.
5787c478bd9Sstevel@tonic-gate  */
5797c478bd9Sstevel@tonic-gate static void
5807c478bd9Sstevel@tonic-gate rfs4_cb_chflush(rfs4_cbinfo_t *cbp)
5817c478bd9Sstevel@tonic-gate {
5827c478bd9Sstevel@tonic-gate 	CLIENT *ch;
5837c478bd9Sstevel@tonic-gate 
5847c478bd9Sstevel@tonic-gate 	NFS4_DEBUG(rfs4_cb_debug, (CE_NOTE,
5857c478bd9Sstevel@tonic-gate 		"rfs4_cb_flush: enter cbp->%p, cb_chc_free=%d\n",
5867c478bd9Sstevel@tonic-gate 		(void *)cbp, cbp->cb_chc_free));
5877c478bd9Sstevel@tonic-gate 
5887c478bd9Sstevel@tonic-gate 	while (cbp->cb_chc_free) {
5897c478bd9Sstevel@tonic-gate 		cbp->cb_chc_free--;
5907c478bd9Sstevel@tonic-gate 		ch = cbp->cb_chc[cbp->cb_chc_free];
5917c478bd9Sstevel@tonic-gate 		cbp->cb_chc[cbp->cb_chc_free] = NULL;
5927c478bd9Sstevel@tonic-gate 		if (ch) {
5937c478bd9Sstevel@tonic-gate 			if (ch->cl_auth)
5947c478bd9Sstevel@tonic-gate 				auth_destroy(ch->cl_auth);
5957c478bd9Sstevel@tonic-gate 			clnt_destroy(ch);
5967c478bd9Sstevel@tonic-gate 		}
5977c478bd9Sstevel@tonic-gate 	}
5987c478bd9Sstevel@tonic-gate }
5997c478bd9Sstevel@tonic-gate 
6007c478bd9Sstevel@tonic-gate /*
6017c478bd9Sstevel@tonic-gate  * Return a client handle, either from a the small
6027c478bd9Sstevel@tonic-gate  * rfs4_client_t cache or one that we just created.
6037c478bd9Sstevel@tonic-gate  */
6047c478bd9Sstevel@tonic-gate static CLIENT *
6057c478bd9Sstevel@tonic-gate rfs4_cb_getch(rfs4_cbinfo_t *cbp)
6067c478bd9Sstevel@tonic-gate {
6077c478bd9Sstevel@tonic-gate 	CLIENT *cbch = NULL;
6087c478bd9Sstevel@tonic-gate 	uint32_t zilch = 0;
6097c478bd9Sstevel@tonic-gate 
6107c478bd9Sstevel@tonic-gate 	NFS4_DEBUG(rfs4_cb_debug, (CE_NOTE,
6117c478bd9Sstevel@tonic-gate 		"rfs4_cb_getch: enter cbp->%p, cb_chc_free=%d\n",
6127c478bd9Sstevel@tonic-gate 		(void *)cbp, cbp->cb_chc_free));
6137c478bd9Sstevel@tonic-gate 
6147c478bd9Sstevel@tonic-gate 	mutex_enter(cbp->cb_lock);
6157c478bd9Sstevel@tonic-gate 
6167c478bd9Sstevel@tonic-gate 	if (cbp->cb_chc_free) {
6177c478bd9Sstevel@tonic-gate 		cbp->cb_chc_free--;
6187c478bd9Sstevel@tonic-gate 		cbch = cbp->cb_chc[ cbp->cb_chc_free ];
6197c478bd9Sstevel@tonic-gate 		NFS4_DEBUG(rfs4_cb_debug, (CE_NOTE,
6207c478bd9Sstevel@tonic-gate 			"rfs4_cb_getch: cb_chc_free=%d ch->%p\n",
6217c478bd9Sstevel@tonic-gate 			cbp->cb_chc_free, (void *)cbch));
6227c478bd9Sstevel@tonic-gate 		mutex_exit(cbp->cb_lock);
6237c478bd9Sstevel@tonic-gate 		(void) CLNT_CONTROL(cbch, CLSET_XID, (char *)&zilch);
6247c478bd9Sstevel@tonic-gate 		return (cbch);
6257c478bd9Sstevel@tonic-gate 	}
6267c478bd9Sstevel@tonic-gate 
6277c478bd9Sstevel@tonic-gate 	mutex_exit(cbp->cb_lock);
6287c478bd9Sstevel@tonic-gate 
6297c478bd9Sstevel@tonic-gate 	NFS4_DEBUG(rfs4_cb_debug, (CE_NOTE,
6307c478bd9Sstevel@tonic-gate 		"rfs4_cb_getch: calling rfs4_cbch_init\n"));
6317c478bd9Sstevel@tonic-gate 
6327c478bd9Sstevel@tonic-gate 	/* none free so make it now */
6337c478bd9Sstevel@tonic-gate 	cbch = rfs4_cbch_init(cbp);
6347c478bd9Sstevel@tonic-gate 
6357c478bd9Sstevel@tonic-gate 	NFS4_DEBUG(rfs4_cb_debug, (CE_NOTE,
6367c478bd9Sstevel@tonic-gate 		"rfs4_cb_getch: returning cbch->%p\n", (void *)cbch));
6377c478bd9Sstevel@tonic-gate 
6387c478bd9Sstevel@tonic-gate 	return (cbch);
6397c478bd9Sstevel@tonic-gate }
6407c478bd9Sstevel@tonic-gate 
6417c478bd9Sstevel@tonic-gate /*
6427c478bd9Sstevel@tonic-gate  * Return the client handle to the small cache or
6437c478bd9Sstevel@tonic-gate  * destroy it.
6447c478bd9Sstevel@tonic-gate  */
6457c478bd9Sstevel@tonic-gate static void
6467c478bd9Sstevel@tonic-gate rfs4_cb_freech(rfs4_cbinfo_t *cbp, CLIENT *ch, bool_t lockheld)
6477c478bd9Sstevel@tonic-gate {
6487c478bd9Sstevel@tonic-gate 	if (lockheld == FALSE)
6497c478bd9Sstevel@tonic-gate 		mutex_enter(cbp->cb_lock);
6507c478bd9Sstevel@tonic-gate 
6517c478bd9Sstevel@tonic-gate 	if (cbp->cb_chc_free < RFS4_CBCH_MAX) {
6527c478bd9Sstevel@tonic-gate 		cbp->cb_chc[ cbp->cb_chc_free++ ] = ch;
6537c478bd9Sstevel@tonic-gate 		if (lockheld == FALSE)
6547c478bd9Sstevel@tonic-gate 			mutex_exit(cbp->cb_lock);
6557c478bd9Sstevel@tonic-gate 		NFS4_DEBUG(rfs4_cb_debug, (CE_NOTE,
6567c478bd9Sstevel@tonic-gate 		    "rfs4_cb_freech: caching cbp->%p, ch->%p, cb_chc_free=%d\n",
6577c478bd9Sstevel@tonic-gate 			(void *)cbp, (void *)ch, cbp->cb_chc_free));
6587c478bd9Sstevel@tonic-gate 		return;
6597c478bd9Sstevel@tonic-gate 	}
6607c478bd9Sstevel@tonic-gate 	if (lockheld == FALSE)
6617c478bd9Sstevel@tonic-gate 		mutex_exit(cbp->cb_lock);
6627c478bd9Sstevel@tonic-gate 
6637c478bd9Sstevel@tonic-gate 	NFS4_DEBUG(rfs4_cb_debug, (CE_NOTE,
6647c478bd9Sstevel@tonic-gate 		"rfs4_cb_freech: destroying cbp->%p, ch->%p, cb_chc_free=%d\n",
6657c478bd9Sstevel@tonic-gate 		(void *)cbp, (void *)ch, cbp->cb_chc_free));
6667c478bd9Sstevel@tonic-gate 
6677c478bd9Sstevel@tonic-gate 	/*
6687c478bd9Sstevel@tonic-gate 	 * cache maxed out of free entries, obliterate
6697c478bd9Sstevel@tonic-gate 	 * this client handle, destroy it, throw it away.
6707c478bd9Sstevel@tonic-gate 	 */
6717c478bd9Sstevel@tonic-gate 	if (ch->cl_auth)
6727c478bd9Sstevel@tonic-gate 		auth_destroy(ch->cl_auth);
6737c478bd9Sstevel@tonic-gate 	clnt_destroy(ch);
6747c478bd9Sstevel@tonic-gate }
6757c478bd9Sstevel@tonic-gate 
6767c478bd9Sstevel@tonic-gate /*
6777c478bd9Sstevel@tonic-gate  * With the supplied callback information - initialize the client
6787c478bd9Sstevel@tonic-gate  * callback data.  If there is a callback in progress, save the
6797c478bd9Sstevel@tonic-gate  * callback info so that a thread can pick it up in the future.
6807c478bd9Sstevel@tonic-gate  */
6817c478bd9Sstevel@tonic-gate void
6827c478bd9Sstevel@tonic-gate rfs4_client_setcb(rfs4_client_t *cp, cb_client4 *cb, uint32_t cb_ident)
6837c478bd9Sstevel@tonic-gate {
6847c478bd9Sstevel@tonic-gate 	char *addr = NULL;
6857c478bd9Sstevel@tonic-gate 	char *netid = NULL;
6867c478bd9Sstevel@tonic-gate 	rfs4_cbinfo_t *cbp = &cp->cbinfo;
6877c478bd9Sstevel@tonic-gate 	size_t len;
6887c478bd9Sstevel@tonic-gate 
6897c478bd9Sstevel@tonic-gate 	/* Set the call back for the client */
690a31a5cfdSsamf 	if (cb->cb_location.r_addr && cb->cb_location.r_addr[0] != '\0' &&
691a31a5cfdSsamf 	    cb->cb_location.r_netid && cb->cb_location.r_netid[0] != '\0') {
6927c478bd9Sstevel@tonic-gate 		len = strlen(cb->cb_location.r_addr) + 1;
6937c478bd9Sstevel@tonic-gate 		addr = kmem_alloc(len, KM_SLEEP);
6947c478bd9Sstevel@tonic-gate 		bcopy(cb->cb_location.r_addr, addr, len);
6957c478bd9Sstevel@tonic-gate 		len = strlen(cb->cb_location.r_netid) + 1;
6967c478bd9Sstevel@tonic-gate 		netid = kmem_alloc(len, KM_SLEEP);
6977c478bd9Sstevel@tonic-gate 		bcopy(cb->cb_location.r_netid, netid, len);
6987c478bd9Sstevel@tonic-gate 	}
6997c478bd9Sstevel@tonic-gate 	/* ready to save the new information but first free old, if exists */
7007c478bd9Sstevel@tonic-gate 	mutex_enter(cbp->cb_lock);
7017c478bd9Sstevel@tonic-gate 
7027c478bd9Sstevel@tonic-gate 	cbp->cb_newer.cb_callback.cb_program = cb->cb_program;
703a31a5cfdSsamf 
704a31a5cfdSsamf 	if (cbp->cb_newer.cb_callback.cb_location.r_addr != NULL)
705a31a5cfdSsamf 		kmem_free(cbp->cb_newer.cb_callback.cb_location.r_addr,
706a31a5cfdSsamf 		    strlen(cbp->cb_newer.cb_callback.cb_location.r_addr) + 1);
7077c478bd9Sstevel@tonic-gate 	cbp->cb_newer.cb_callback.cb_location.r_addr = addr;
708a31a5cfdSsamf 
709a31a5cfdSsamf 	if (cbp->cb_newer.cb_callback.cb_location.r_netid != NULL)
710a31a5cfdSsamf 		kmem_free(cbp->cb_newer.cb_callback.cb_location.r_netid,
711a31a5cfdSsamf 		    strlen(cbp->cb_newer.cb_callback.cb_location.r_netid) + 1);
7127c478bd9Sstevel@tonic-gate 	cbp->cb_newer.cb_callback.cb_location.r_netid = netid;
7137c478bd9Sstevel@tonic-gate 
7147c478bd9Sstevel@tonic-gate 	cbp->cb_newer.cb_ident = cb_ident;
7157c478bd9Sstevel@tonic-gate 
7167c478bd9Sstevel@tonic-gate 	if (addr && *addr && netid && *netid) {
7177c478bd9Sstevel@tonic-gate 		cbp->cb_newer.cb_new = TRUE;
7187c478bd9Sstevel@tonic-gate 		cbp->cb_newer.cb_confirmed = FALSE;
7197c478bd9Sstevel@tonic-gate 	} else {
7207c478bd9Sstevel@tonic-gate 		cbp->cb_newer.cb_new = FALSE;
7217c478bd9Sstevel@tonic-gate 		cbp->cb_newer.cb_confirmed = FALSE;
7227c478bd9Sstevel@tonic-gate 	}
7237c478bd9Sstevel@tonic-gate 
7247c478bd9Sstevel@tonic-gate 	mutex_exit(cbp->cb_lock);
7257c478bd9Sstevel@tonic-gate }
7267c478bd9Sstevel@tonic-gate 
7277c478bd9Sstevel@tonic-gate /*
7287c478bd9Sstevel@tonic-gate  * The server uses this when processing SETCLIENTID_CONFIRM.  Callback
7297c478bd9Sstevel@tonic-gate  * information may have been provided on SETCLIENTID and this call
7307c478bd9Sstevel@tonic-gate  * marks that information as confirmed and then starts a thread to
7317c478bd9Sstevel@tonic-gate  * test the callback path.
7327c478bd9Sstevel@tonic-gate  */
7337c478bd9Sstevel@tonic-gate void
7347c478bd9Sstevel@tonic-gate rfs4_deleg_cb_check(rfs4_client_t *cp)
7357c478bd9Sstevel@tonic-gate {
7367c478bd9Sstevel@tonic-gate 	if (cp->cbinfo.cb_newer.cb_new == FALSE)
7377c478bd9Sstevel@tonic-gate 		return;
7387c478bd9Sstevel@tonic-gate 
7397c478bd9Sstevel@tonic-gate 	cp->cbinfo.cb_newer.cb_confirmed = TRUE;
7407c478bd9Sstevel@tonic-gate 
7417c478bd9Sstevel@tonic-gate 	rfs4_dbe_hold(cp->dbe); /* hold the client struct for thread */
7427c478bd9Sstevel@tonic-gate 
7437c478bd9Sstevel@tonic-gate 	(void) thread_create(NULL, 0, rfs4_do_cb_null, cp, 0, &p0, TS_RUN,
7447c478bd9Sstevel@tonic-gate 			    minclsyspri);
7457c478bd9Sstevel@tonic-gate }
7467c478bd9Sstevel@tonic-gate 
7477c478bd9Sstevel@tonic-gate static void
7487c478bd9Sstevel@tonic-gate rfs4args_cb_recall_free(nfs_cb_argop4 *argop)
7497c478bd9Sstevel@tonic-gate {
7507c478bd9Sstevel@tonic-gate 	CB_RECALL4args	*rec_argp;
7517c478bd9Sstevel@tonic-gate 
7527c478bd9Sstevel@tonic-gate 	rec_argp = &argop->nfs_cb_argop4_u.opcbrecall;
7537c478bd9Sstevel@tonic-gate 	if (rec_argp->fh.nfs_fh4_val)
7547c478bd9Sstevel@tonic-gate 		kmem_free(rec_argp->fh.nfs_fh4_val, rec_argp->fh.nfs_fh4_len);
7557c478bd9Sstevel@tonic-gate }
7567c478bd9Sstevel@tonic-gate 
7577c478bd9Sstevel@tonic-gate /* ARGSUSED */
7587c478bd9Sstevel@tonic-gate static void
7597c478bd9Sstevel@tonic-gate rfs4args_cb_getattr_free(nfs_cb_argop4 *argop)
7607c478bd9Sstevel@tonic-gate {
7617c478bd9Sstevel@tonic-gate 	CB_GETATTR4args *argp;
7627c478bd9Sstevel@tonic-gate 
7637c478bd9Sstevel@tonic-gate 	argp = &argop->nfs_cb_argop4_u.opcbgetattr;
7647c478bd9Sstevel@tonic-gate 	if (argp->fh.nfs_fh4_val)
7657c478bd9Sstevel@tonic-gate 		kmem_free(argp->fh.nfs_fh4_val, argp->fh.nfs_fh4_len);
7667c478bd9Sstevel@tonic-gate }
7677c478bd9Sstevel@tonic-gate 
7687c478bd9Sstevel@tonic-gate static void
7697c478bd9Sstevel@tonic-gate rfs4freeargres(CB_COMPOUND4args *args, CB_COMPOUND4res *resp)
7707c478bd9Sstevel@tonic-gate {
7717c478bd9Sstevel@tonic-gate 	int i, arglen;
7727c478bd9Sstevel@tonic-gate 	nfs_cb_argop4 *argop;
7737c478bd9Sstevel@tonic-gate 
7747c478bd9Sstevel@tonic-gate 	/*
7757c478bd9Sstevel@tonic-gate 	 * First free any special args alloc'd for specific ops.
7767c478bd9Sstevel@tonic-gate 	 */
7777c478bd9Sstevel@tonic-gate 	arglen = args->array_len;
7787c478bd9Sstevel@tonic-gate 	argop = args->array;
7797c478bd9Sstevel@tonic-gate 	for (i = 0; i < arglen; i++, argop++) {
7807c478bd9Sstevel@tonic-gate 
7817c478bd9Sstevel@tonic-gate 		switch (argop->argop) {
7827c478bd9Sstevel@tonic-gate 		case OP_CB_RECALL:
7837c478bd9Sstevel@tonic-gate 			rfs4args_cb_recall_free(argop);
7847c478bd9Sstevel@tonic-gate 			break;
7857c478bd9Sstevel@tonic-gate 
7867c478bd9Sstevel@tonic-gate 		case OP_CB_GETATTR:
7877c478bd9Sstevel@tonic-gate 			rfs4args_cb_getattr_free(argop);
7887c478bd9Sstevel@tonic-gate 			break;
7897c478bd9Sstevel@tonic-gate 
7907c478bd9Sstevel@tonic-gate 		default:
7917c478bd9Sstevel@tonic-gate 			NFS4_DEBUG(rfs4_deleg_debug, (CE_NOTE,
7927c478bd9Sstevel@tonic-gate 				"rfs4freeargres: unknown op"));
7937c478bd9Sstevel@tonic-gate 			return;
7947c478bd9Sstevel@tonic-gate 		}
7957c478bd9Sstevel@tonic-gate 	}
7967c478bd9Sstevel@tonic-gate 
7977c478bd9Sstevel@tonic-gate 	if (args->tag.utf8string_len > 0)
7987c478bd9Sstevel@tonic-gate 		UTF8STRING_FREE(args->tag)
7997c478bd9Sstevel@tonic-gate 
8007c478bd9Sstevel@tonic-gate 	kmem_free(args->array, arglen * sizeof (nfs_cb_argop4));
8017c478bd9Sstevel@tonic-gate 	if (resp)
8027c478bd9Sstevel@tonic-gate 		(void) xdr_free(xdr_CB_COMPOUND4res, (caddr_t)resp);
8037c478bd9Sstevel@tonic-gate }
8047c478bd9Sstevel@tonic-gate 
8057c478bd9Sstevel@tonic-gate /*
8067c478bd9Sstevel@tonic-gate  * General callback routine for the server to the client.
8077c478bd9Sstevel@tonic-gate  */
8087c478bd9Sstevel@tonic-gate static enum clnt_stat
8097c478bd9Sstevel@tonic-gate rfs4_do_callback(rfs4_client_t	*cp, CB_COMPOUND4args *args,
8107c478bd9Sstevel@tonic-gate 		CB_COMPOUND4res *res, struct timeval timeout)
8117c478bd9Sstevel@tonic-gate {
8127c478bd9Sstevel@tonic-gate 	rfs4_cbinfo_t *cbp;
8137c478bd9Sstevel@tonic-gate 	CLIENT *ch;
8147c478bd9Sstevel@tonic-gate 	/* start with this in case cb_getch() fails */
8157c478bd9Sstevel@tonic-gate 	enum clnt_stat	stat = RPC_FAILED;
8167c478bd9Sstevel@tonic-gate 
8177c478bd9Sstevel@tonic-gate 	res->tag.utf8string_val = NULL;
8187c478bd9Sstevel@tonic-gate 	res->array = NULL;
8197c478bd9Sstevel@tonic-gate 
8207c478bd9Sstevel@tonic-gate 	NFS4_DEBUG(rfs4_cb_debug, (CE_NOTE,
8217c478bd9Sstevel@tonic-gate 		"rfs4_do_callback: enter cp->%p\n", (void *)cp));
8227c478bd9Sstevel@tonic-gate 
8237c478bd9Sstevel@tonic-gate retry:
8247c478bd9Sstevel@tonic-gate 	cbp = rfs4_cbinfo_hold(cp);
8257c478bd9Sstevel@tonic-gate 	if (cbp == NULL)
8267c478bd9Sstevel@tonic-gate 		return (stat);
8277c478bd9Sstevel@tonic-gate 
8287c478bd9Sstevel@tonic-gate 	/* get a client handle */
8297c478bd9Sstevel@tonic-gate 	if ((ch = rfs4_cb_getch(cbp)) != NULL) {
8307c478bd9Sstevel@tonic-gate 		/*
8317c478bd9Sstevel@tonic-gate 		 * reset the cb_ident since it may have changed in
8327c478bd9Sstevel@tonic-gate 		 * rfs4_cbinfo_hold()
8337c478bd9Sstevel@tonic-gate 		 */
8347c478bd9Sstevel@tonic-gate 		args->callback_ident = cbp->cb_ident;
8357c478bd9Sstevel@tonic-gate 
836*eac3aab7Srobinson 		stat = clnt_call(ch, CB_COMPOUND, xdr_CB_COMPOUND4args_srv,
8377c478bd9Sstevel@tonic-gate 			(caddr_t)args, xdr_CB_COMPOUND4res,
8387c478bd9Sstevel@tonic-gate 			(caddr_t)res, timeout);
8397c478bd9Sstevel@tonic-gate 
8407c478bd9Sstevel@tonic-gate 		/* free client handle */
8417c478bd9Sstevel@tonic-gate 		rfs4_cb_freech(cbp, ch, FALSE);
8427c478bd9Sstevel@tonic-gate 	}
8437c478bd9Sstevel@tonic-gate 
8447c478bd9Sstevel@tonic-gate 	NFS4_DEBUG(rfs4_cb_debug, (CE_NOTE,
8457c478bd9Sstevel@tonic-gate 		"rfs4_do_callback: exit with RPC status %d, %s",
8467c478bd9Sstevel@tonic-gate 		stat, clnt_sperrno(stat)));
8477c478bd9Sstevel@tonic-gate 
8487c478bd9Sstevel@tonic-gate 	/*
8497c478bd9Sstevel@tonic-gate 	 * If the rele says that there may be new callback info then
8507c478bd9Sstevel@tonic-gate 	 * retry this sequence and it may succeed as a result of the
8517c478bd9Sstevel@tonic-gate 	 * new callback path
8527c478bd9Sstevel@tonic-gate 	 */
8537c478bd9Sstevel@tonic-gate 	if (rfs4_cbinfo_rele(cbp,
8547c478bd9Sstevel@tonic-gate 		(stat == RPC_SUCCESS ? CB_NOCHANGE : CB_FAILED)) == TRUE)
8557c478bd9Sstevel@tonic-gate 		goto retry;
8567c478bd9Sstevel@tonic-gate 
8577c478bd9Sstevel@tonic-gate 	return (stat);
8587c478bd9Sstevel@tonic-gate }
8597c478bd9Sstevel@tonic-gate 
8607c478bd9Sstevel@tonic-gate /*
8617c478bd9Sstevel@tonic-gate  * Used by the NFSv4 server to get attributes for a file while
8627c478bd9Sstevel@tonic-gate  * handling the case where a file has been write delegated.  For the
8637c478bd9Sstevel@tonic-gate  * time being, VOP_GETATTR() is called and CB_GETATTR processing is
8647c478bd9Sstevel@tonic-gate  * not undertaken.  This call site is maintained in case the server is
8657c478bd9Sstevel@tonic-gate  * updated in the future to handle write delegation space guarantees.
8667c478bd9Sstevel@tonic-gate  */
8677c478bd9Sstevel@tonic-gate nfsstat4
8687c478bd9Sstevel@tonic-gate rfs4_vop_getattr(vnode_t *vp, vattr_t *vap, int flag, cred_t *cr)
8697c478bd9Sstevel@tonic-gate {
8707c478bd9Sstevel@tonic-gate 	uint_t mask;
8717c478bd9Sstevel@tonic-gate 	int error;
8727c478bd9Sstevel@tonic-gate 
8737c478bd9Sstevel@tonic-gate 	mask = vap->va_mask;
8747c478bd9Sstevel@tonic-gate 	error = VOP_GETATTR(vp, vap, flag, cr);
8757c478bd9Sstevel@tonic-gate 	/*
8767c478bd9Sstevel@tonic-gate 	 * Some file systems clobber va_mask. it is probably wrong of
8777c478bd9Sstevel@tonic-gate 	 * them to do so, nonethless we practice defensive coding.
8787c478bd9Sstevel@tonic-gate 	 * See bug id 4276830.
8797c478bd9Sstevel@tonic-gate 	 */
8807c478bd9Sstevel@tonic-gate 	vap->va_mask = mask;
8817c478bd9Sstevel@tonic-gate 	return (puterrno4(error));
8827c478bd9Sstevel@tonic-gate }
8837c478bd9Sstevel@tonic-gate 
8847c478bd9Sstevel@tonic-gate /*
8857c478bd9Sstevel@tonic-gate  * This is used everywhere in the v2/v3 server to allow the
8867c478bd9Sstevel@tonic-gate  * integration of all NFS versions and the support of delegation.  For
8877c478bd9Sstevel@tonic-gate  * now, just call the VOP_GETATTR().  If the NFSv4 server is enhanced
8887c478bd9Sstevel@tonic-gate  * in the future to provide space guarantees for write delegations
8897c478bd9Sstevel@tonic-gate  * then this call site should be expanded to interact with the client.
8907c478bd9Sstevel@tonic-gate  */
8917c478bd9Sstevel@tonic-gate int
8927c478bd9Sstevel@tonic-gate rfs4_delegated_getattr(vnode_t *vp, vattr_t *vap, int flag, cred_t *cr)
8937c478bd9Sstevel@tonic-gate {
8947c478bd9Sstevel@tonic-gate 	return (VOP_GETATTR(vp, vap, flag, cr));
8957c478bd9Sstevel@tonic-gate }
8967c478bd9Sstevel@tonic-gate 
8977c478bd9Sstevel@tonic-gate /*
8987c478bd9Sstevel@tonic-gate  * Place the actual cb_recall otw call to client.
8997c478bd9Sstevel@tonic-gate  */
9007c478bd9Sstevel@tonic-gate static void
9017c478bd9Sstevel@tonic-gate rfs4_do_cb_recall(rfs4_deleg_state_t *dsp, bool_t trunc)
9027c478bd9Sstevel@tonic-gate {
9037c478bd9Sstevel@tonic-gate 	CB_COMPOUND4args	cb4_args;
9047c478bd9Sstevel@tonic-gate 	CB_COMPOUND4res		cb4_res;
9057c478bd9Sstevel@tonic-gate 	CB_RECALL4args		*rec_argp;
9067c478bd9Sstevel@tonic-gate 	nfs_cb_argop4		*argop;
9077c478bd9Sstevel@tonic-gate 	int			numops;
9087c478bd9Sstevel@tonic-gate 	int			argoplist_size;
9097c478bd9Sstevel@tonic-gate 	struct timeval		timeout;
9107c478bd9Sstevel@tonic-gate 	nfs_fh4			*fhp;
9117c478bd9Sstevel@tonic-gate 	enum clnt_stat		call_stat;
9127c478bd9Sstevel@tonic-gate 
9137c478bd9Sstevel@tonic-gate 	NFS4_DEBUG(rfs4_deleg_debug, (CE_NOTE, "rfs4_do_cb_recall: enter"));
9147c478bd9Sstevel@tonic-gate 	/*
9157c478bd9Sstevel@tonic-gate 	 * set up the compound args
9167c478bd9Sstevel@tonic-gate 	 */
9177c478bd9Sstevel@tonic-gate 	numops = 1;	/* CB_RECALL only */
9187c478bd9Sstevel@tonic-gate 
9197c478bd9Sstevel@tonic-gate 	argoplist_size = numops * sizeof (nfs_cb_argop4);
9207c478bd9Sstevel@tonic-gate 	argop = kmem_zalloc(argoplist_size, KM_SLEEP);
9217c478bd9Sstevel@tonic-gate 	argop->argop = OP_CB_RECALL;
9227c478bd9Sstevel@tonic-gate 	rec_argp = &argop->nfs_cb_argop4_u.opcbrecall;
9237c478bd9Sstevel@tonic-gate 
9247c478bd9Sstevel@tonic-gate 	(void) str_to_utf8("cb_recall", &cb4_args.tag);
9257c478bd9Sstevel@tonic-gate 	cb4_args.minorversion = CB4_MINORVERSION;
9267c478bd9Sstevel@tonic-gate 	/* cb4_args.callback_ident is set in rfs4_do_callback() */
9277c478bd9Sstevel@tonic-gate 	cb4_args.array_len = numops;
9287c478bd9Sstevel@tonic-gate 	cb4_args.array = argop;
9297c478bd9Sstevel@tonic-gate 
9307c478bd9Sstevel@tonic-gate 	/*
9317c478bd9Sstevel@tonic-gate 	 * fill in the args struct
9327c478bd9Sstevel@tonic-gate 	 */
9337c478bd9Sstevel@tonic-gate 	bcopy(&dsp->delegid.stateid, &rec_argp->stateid, sizeof (stateid4));
9347c478bd9Sstevel@tonic-gate 	rec_argp->truncate = trunc;
9357c478bd9Sstevel@tonic-gate 
9367c478bd9Sstevel@tonic-gate 	fhp = &dsp->finfo->filehandle;
9377c478bd9Sstevel@tonic-gate 	rec_argp->fh.nfs_fh4_val = kmem_alloc(sizeof (char) *
9387c478bd9Sstevel@tonic-gate 					    fhp->nfs_fh4_len, KM_SLEEP);
9397c478bd9Sstevel@tonic-gate 	nfs_fh4_copy(fhp, &rec_argp->fh);
9407c478bd9Sstevel@tonic-gate 
9417c478bd9Sstevel@tonic-gate 	/* Keep track of when we did this for observability */
9427c478bd9Sstevel@tonic-gate 	dsp->time_recalled = gethrestime_sec();
9437c478bd9Sstevel@tonic-gate 
9447c478bd9Sstevel@tonic-gate 	/*
9457c478bd9Sstevel@tonic-gate 	 * Set up the timeout for the callback and make the actual call.
9467c478bd9Sstevel@tonic-gate 	 * Timeout will be 80% of the lease period for this server.
9477c478bd9Sstevel@tonic-gate 	 */
9487c478bd9Sstevel@tonic-gate 	timeout.tv_sec = (rfs4_lease_time * 80) / 100;
9497c478bd9Sstevel@tonic-gate 	timeout.tv_usec = 0;
9507c478bd9Sstevel@tonic-gate 
9517c478bd9Sstevel@tonic-gate 	call_stat = rfs4_do_callback(dsp->client, &cb4_args,
9527c478bd9Sstevel@tonic-gate 		&cb4_res, timeout);
9537c478bd9Sstevel@tonic-gate 
9547c478bd9Sstevel@tonic-gate 	if (call_stat != RPC_SUCCESS || cb4_res.status != NFS4_OK) {
9557c478bd9Sstevel@tonic-gate 		rfs4_revoke_deleg(dsp);
9567c478bd9Sstevel@tonic-gate 		NFS4_DEBUG(rfs4_deleg_debug, (CE_NOTE,
9577c478bd9Sstevel@tonic-gate 			"rfs4_do_cb_recall: rpcstat=%d cbstat=%d ",
9587c478bd9Sstevel@tonic-gate 			call_stat, cb4_res.status));
9597c478bd9Sstevel@tonic-gate 	}
9607c478bd9Sstevel@tonic-gate 
9617c478bd9Sstevel@tonic-gate 	rfs4freeargres(&cb4_args, &cb4_res);
9627c478bd9Sstevel@tonic-gate }
9637c478bd9Sstevel@tonic-gate 
9647c478bd9Sstevel@tonic-gate struct recall_arg {
9657c478bd9Sstevel@tonic-gate 	rfs4_deleg_state_t *dsp;
9667c478bd9Sstevel@tonic-gate 	void (*recall)(rfs4_deleg_state_t *, bool_t trunc);
9677c478bd9Sstevel@tonic-gate 	bool_t trunc;
9687c478bd9Sstevel@tonic-gate };
9697c478bd9Sstevel@tonic-gate 
9707c478bd9Sstevel@tonic-gate static void
9717c478bd9Sstevel@tonic-gate do_recall(struct recall_arg *arg)
9727c478bd9Sstevel@tonic-gate {
9737c478bd9Sstevel@tonic-gate 	rfs4_deleg_state_t *dsp = arg->dsp;
9747c478bd9Sstevel@tonic-gate 	rfs4_file_t *fp = dsp->finfo;
9757c478bd9Sstevel@tonic-gate 	callb_cpr_t cpr_info;
9767c478bd9Sstevel@tonic-gate 	kmutex_t cpr_lock;
9777c478bd9Sstevel@tonic-gate 
9787c478bd9Sstevel@tonic-gate 	mutex_init(&cpr_lock, NULL, MUTEX_DEFAULT, NULL);
9797c478bd9Sstevel@tonic-gate 	CALLB_CPR_INIT(&cpr_info, &cpr_lock, callb_generic_cpr, "nfsv4Recall");
9807c478bd9Sstevel@tonic-gate 
9817c478bd9Sstevel@tonic-gate 	/*
9827c478bd9Sstevel@tonic-gate 	 * It is possible that before this thread starts
9837c478bd9Sstevel@tonic-gate 	 * the client has send us a return_delegation, and
9847c478bd9Sstevel@tonic-gate 	 * if that is the case we do not need to send the
9857c478bd9Sstevel@tonic-gate 	 * recall callback.
9867c478bd9Sstevel@tonic-gate 	 */
9877c478bd9Sstevel@tonic-gate 	if (dsp->dtype != OPEN_DELEGATE_NONE) {
9887c478bd9Sstevel@tonic-gate 		NFS4_DEBUG(rfs4_deleg_debug,
9897c478bd9Sstevel@tonic-gate 		    (CE_NOTE, "recall = %p, state = %p, fp = %p trunc = %d",
9907c478bd9Sstevel@tonic-gate 		    (void*)arg->recall, (void*)dsp, (void*)fp, arg->trunc));
9917c478bd9Sstevel@tonic-gate 
9927c478bd9Sstevel@tonic-gate 		if (arg->recall)
9937c478bd9Sstevel@tonic-gate 			(void) (*arg->recall)(dsp, arg->trunc);
9947c478bd9Sstevel@tonic-gate 	}
9957c478bd9Sstevel@tonic-gate 
9967c478bd9Sstevel@tonic-gate 	mutex_enter(fp->dinfo->recall_lock);
9977c478bd9Sstevel@tonic-gate 	/*
9987c478bd9Sstevel@tonic-gate 	 * Recall count may go negative if the parent thread that is
9997c478bd9Sstevel@tonic-gate 	 * creating the individual callback threads does not modify
10007c478bd9Sstevel@tonic-gate 	 * the recall_count field before the callback thread actually
10017c478bd9Sstevel@tonic-gate 	 * gets a response from the CB_RECALL
10027c478bd9Sstevel@tonic-gate 	 */
10037c478bd9Sstevel@tonic-gate 	fp->dinfo->recall_count--;
10047c478bd9Sstevel@tonic-gate 	if (fp->dinfo->recall_count == 0)
10057c478bd9Sstevel@tonic-gate 		cv_signal(fp->dinfo->recall_cv);
10067c478bd9Sstevel@tonic-gate 	mutex_exit(fp->dinfo->recall_lock);
10077c478bd9Sstevel@tonic-gate 
10087c478bd9Sstevel@tonic-gate 	mutex_enter(&cpr_lock);
10097c478bd9Sstevel@tonic-gate 	CALLB_CPR_EXIT(&cpr_info);
10107c478bd9Sstevel@tonic-gate 	mutex_destroy(&cpr_lock);
10117c478bd9Sstevel@tonic-gate 
10127c478bd9Sstevel@tonic-gate 	rfs4_deleg_state_rele(dsp); /* release the hold for this thread */
10137c478bd9Sstevel@tonic-gate 
10147c478bd9Sstevel@tonic-gate 	kmem_free(arg, sizeof (struct recall_arg));
10157c478bd9Sstevel@tonic-gate }
10167c478bd9Sstevel@tonic-gate 
10177c478bd9Sstevel@tonic-gate struct master_recall_args {
10187c478bd9Sstevel@tonic-gate     rfs4_file_t *fp;
10197c478bd9Sstevel@tonic-gate     void (*recall)(rfs4_deleg_state_t *, bool_t);
10207c478bd9Sstevel@tonic-gate     bool_t trunc;
10217c478bd9Sstevel@tonic-gate };
10227c478bd9Sstevel@tonic-gate 
10237c478bd9Sstevel@tonic-gate static void
10247c478bd9Sstevel@tonic-gate do_recall_file(struct master_recall_args *map)
10257c478bd9Sstevel@tonic-gate {
10267c478bd9Sstevel@tonic-gate 	rfs4_file_t *fp = map->fp;
10277c478bd9Sstevel@tonic-gate 	rfs4_deleg_state_t *dsp;
10287c478bd9Sstevel@tonic-gate 	struct recall_arg *arg;
10297c478bd9Sstevel@tonic-gate 	callb_cpr_t cpr_info;
10307c478bd9Sstevel@tonic-gate 	kmutex_t cpr_lock;
10317c478bd9Sstevel@tonic-gate 	int32_t recall_count;
10327c478bd9Sstevel@tonic-gate 
10337c478bd9Sstevel@tonic-gate 	rfs4_dbe_lock(fp->dbe);
10347c478bd9Sstevel@tonic-gate 	/* Recall already in progress */
10357c478bd9Sstevel@tonic-gate 	if (fp->dinfo->recall_count != 0) {
10367c478bd9Sstevel@tonic-gate 		rfs4_dbe_rele_nolock(fp->dbe);
10377c478bd9Sstevel@tonic-gate 		rfs4_dbe_unlock(fp->dbe);
10387c478bd9Sstevel@tonic-gate 		kmem_free(map, sizeof (struct master_recall_args));
10397c478bd9Sstevel@tonic-gate 		return;
10407c478bd9Sstevel@tonic-gate 	}
10417c478bd9Sstevel@tonic-gate 
10427c478bd9Sstevel@tonic-gate 	mutex_init(&cpr_lock, NULL, MUTEX_DEFAULT, NULL);
10437c478bd9Sstevel@tonic-gate 	CALLB_CPR_INIT(&cpr_info, &cpr_lock, callb_generic_cpr,
10447c478bd9Sstevel@tonic-gate 			"nfsv4RecallFile");
10457c478bd9Sstevel@tonic-gate 
10467c478bd9Sstevel@tonic-gate 	recall_count = 0;
10477c478bd9Sstevel@tonic-gate 	for (dsp = fp->delegationlist.next->dsp; dsp != NULL;
10487c478bd9Sstevel@tonic-gate 		dsp = dsp->delegationlist.next->dsp) {
10497c478bd9Sstevel@tonic-gate 		arg = kmem_alloc(sizeof (struct recall_arg), KM_SLEEP);
10507c478bd9Sstevel@tonic-gate 		arg->recall = map->recall;
10517c478bd9Sstevel@tonic-gate 		arg->trunc = map->trunc;
10527c478bd9Sstevel@tonic-gate 
10537c478bd9Sstevel@tonic-gate 		rfs4_dbe_hold(dsp->dbe);	/* hold for receiving thread */
10547c478bd9Sstevel@tonic-gate 
10557c478bd9Sstevel@tonic-gate 		arg->dsp = dsp;
10567c478bd9Sstevel@tonic-gate 
10577c478bd9Sstevel@tonic-gate 		recall_count++;
10587c478bd9Sstevel@tonic-gate 
10597c478bd9Sstevel@tonic-gate 		(void) thread_create(NULL, 0, do_recall, arg, 0, &p0, TS_RUN,
10607c478bd9Sstevel@tonic-gate 				    minclsyspri);
10617c478bd9Sstevel@tonic-gate 	}
10627c478bd9Sstevel@tonic-gate 	rfs4_dbe_unlock(fp->dbe);
10637c478bd9Sstevel@tonic-gate 
10647c478bd9Sstevel@tonic-gate 	mutex_enter(fp->dinfo->recall_lock);
10657c478bd9Sstevel@tonic-gate 	/*
10667c478bd9Sstevel@tonic-gate 	 * Recall count may go negative if the parent thread that is
10677c478bd9Sstevel@tonic-gate 	 * creating the individual callback threads does not modify
10687c478bd9Sstevel@tonic-gate 	 * the recall_count field before the callback thread actually
10697c478bd9Sstevel@tonic-gate 	 * gets a response from the CB_RECALL
10707c478bd9Sstevel@tonic-gate 	 */
10717c478bd9Sstevel@tonic-gate 	fp->dinfo->recall_count += recall_count;
10727c478bd9Sstevel@tonic-gate 	while (fp->dinfo->recall_count)
10737c478bd9Sstevel@tonic-gate 		cv_wait(fp->dinfo->recall_cv, fp->dinfo->recall_lock);
10747c478bd9Sstevel@tonic-gate 
10757c478bd9Sstevel@tonic-gate 	mutex_exit(fp->dinfo->recall_lock);
10767c478bd9Sstevel@tonic-gate 
10777c478bd9Sstevel@tonic-gate 	NFS4_DEBUG(rfs4_deleg_debug, (CE_NOTE, "Recall complete for %p",
10787c478bd9Sstevel@tonic-gate 		    (void*)fp));
10797c478bd9Sstevel@tonic-gate 	rfs4_file_rele(fp);
10807c478bd9Sstevel@tonic-gate 	kmem_free(map, sizeof (struct master_recall_args));
10817c478bd9Sstevel@tonic-gate 	mutex_enter(&cpr_lock);
10827c478bd9Sstevel@tonic-gate 	CALLB_CPR_EXIT(&cpr_info);
10837c478bd9Sstevel@tonic-gate 	mutex_destroy(&cpr_lock);
10847c478bd9Sstevel@tonic-gate }
10857c478bd9Sstevel@tonic-gate 
10867c478bd9Sstevel@tonic-gate static void
10877c478bd9Sstevel@tonic-gate rfs4_recall_file(rfs4_file_t *fp,
10887c478bd9Sstevel@tonic-gate 	void (*recall)(rfs4_deleg_state_t *, bool_t trunc),
10897c478bd9Sstevel@tonic-gate 	bool_t trunc, rfs4_client_t *cp)
10907c478bd9Sstevel@tonic-gate {
10917c478bd9Sstevel@tonic-gate 	struct master_recall_args *args;
10927c478bd9Sstevel@tonic-gate 
10937c478bd9Sstevel@tonic-gate 	rfs4_dbe_lock(fp->dbe);
10947c478bd9Sstevel@tonic-gate 	if (fp->dinfo->dtype == OPEN_DELEGATE_NONE) {
10957c478bd9Sstevel@tonic-gate 		rfs4_dbe_unlock(fp->dbe);
10967c478bd9Sstevel@tonic-gate 		return;
10977c478bd9Sstevel@tonic-gate 	}
10987c478bd9Sstevel@tonic-gate 	rfs4_dbe_hold(fp->dbe);	/* hold for new thread */
10997c478bd9Sstevel@tonic-gate 
11007c478bd9Sstevel@tonic-gate 	/*
11017c478bd9Sstevel@tonic-gate 	 * Mark the time we started the recall processing.
11027c478bd9Sstevel@tonic-gate 	 * If it has been previously recalled, do not reset the
11037c478bd9Sstevel@tonic-gate 	 * timer since this is used for the revocation decision.
11047c478bd9Sstevel@tonic-gate 	 */
11057c478bd9Sstevel@tonic-gate 	if (fp->dinfo->time_recalled == 0)
11067c478bd9Sstevel@tonic-gate 		fp->dinfo->time_recalled = gethrestime_sec();
11077c478bd9Sstevel@tonic-gate 	fp->dinfo->ever_recalled = TRUE; /* used for policy decision */
11087c478bd9Sstevel@tonic-gate 	/* Client causing recall not always available */
11097c478bd9Sstevel@tonic-gate 	if (cp)
11107c478bd9Sstevel@tonic-gate 		fp->dinfo->conflicted_client = cp->clientid;
11117c478bd9Sstevel@tonic-gate 
11127c478bd9Sstevel@tonic-gate 	rfs4_dbe_unlock(fp->dbe);
11137c478bd9Sstevel@tonic-gate 
11147c478bd9Sstevel@tonic-gate 	args = kmem_alloc(sizeof (struct master_recall_args), KM_SLEEP);
11157c478bd9Sstevel@tonic-gate 	args->fp = fp;
11167c478bd9Sstevel@tonic-gate 	args->recall = recall;
11177c478bd9Sstevel@tonic-gate 	args->trunc = trunc;
11187c478bd9Sstevel@tonic-gate 
11197c478bd9Sstevel@tonic-gate 	(void) thread_create(NULL, 0, do_recall_file, args, 0, &p0, TS_RUN,
11207c478bd9Sstevel@tonic-gate 			    minclsyspri);
11217c478bd9Sstevel@tonic-gate }
11227c478bd9Sstevel@tonic-gate 
11237c478bd9Sstevel@tonic-gate void
11247c478bd9Sstevel@tonic-gate rfs4_recall_deleg(rfs4_file_t *fp, bool_t trunc, rfs4_client_t *cp)
11257c478bd9Sstevel@tonic-gate {
11267c478bd9Sstevel@tonic-gate 	time_t elapsed1, elapsed2;
11277c478bd9Sstevel@tonic-gate 
11287c478bd9Sstevel@tonic-gate 	if (fp->dinfo->time_recalled != 0) {
11297c478bd9Sstevel@tonic-gate 		elapsed1 = gethrestime_sec() - fp->dinfo->time_recalled;
11307c478bd9Sstevel@tonic-gate 		elapsed2 = gethrestime_sec() - fp->dinfo->time_lastwrite;
11317c478bd9Sstevel@tonic-gate 		/* First check to see if a revocation should occur */
11327c478bd9Sstevel@tonic-gate 		if (elapsed1 > rfs4_lease_time &&
11337c478bd9Sstevel@tonic-gate 			elapsed2 > rfs4_lease_time) {
11347c478bd9Sstevel@tonic-gate 			rfs4_revoke_file(fp);
11357c478bd9Sstevel@tonic-gate 			return;
11367c478bd9Sstevel@tonic-gate 		}
11377c478bd9Sstevel@tonic-gate 		/*
11387c478bd9Sstevel@tonic-gate 		 * Next check to see if a recall should be done again
11397c478bd9Sstevel@tonic-gate 		 * so quickly.
11407c478bd9Sstevel@tonic-gate 		 */
11417c478bd9Sstevel@tonic-gate 		if (elapsed1 <= ((rfs4_lease_time * 20) / 100))
11427c478bd9Sstevel@tonic-gate 			return;
11437c478bd9Sstevel@tonic-gate 	}
11447c478bd9Sstevel@tonic-gate 	rfs4_recall_file(fp, rfs4_do_cb_recall, trunc, cp);
11457c478bd9Sstevel@tonic-gate }
11467c478bd9Sstevel@tonic-gate 
11477c478bd9Sstevel@tonic-gate /*
11487c478bd9Sstevel@tonic-gate  * rfs4_check_recall is called from rfs4_do_open to determine if the current
11497c478bd9Sstevel@tonic-gate  * open conflicts with the delegation.
11507c478bd9Sstevel@tonic-gate  * Return true if we need recall otherwise false.
11517c478bd9Sstevel@tonic-gate  * Assumes entry locks for sp and sp->finfo are held.
11527c478bd9Sstevel@tonic-gate  */
11537c478bd9Sstevel@tonic-gate bool_t
11547c478bd9Sstevel@tonic-gate rfs4_check_recall(rfs4_state_t *sp, uint32_t access)
11557c478bd9Sstevel@tonic-gate {
11567c478bd9Sstevel@tonic-gate 	open_delegation_type4 dtype = sp->finfo->dinfo->dtype;
11577c478bd9Sstevel@tonic-gate 
11587c478bd9Sstevel@tonic-gate 	switch (dtype) {
11597c478bd9Sstevel@tonic-gate 	case OPEN_DELEGATE_NONE:
11607c478bd9Sstevel@tonic-gate 		/* Not currently delegated so there is nothing to do */
11617c478bd9Sstevel@tonic-gate 		return (FALSE);
11627c478bd9Sstevel@tonic-gate 	case OPEN_DELEGATE_READ:
11637c478bd9Sstevel@tonic-gate 		/*
11647c478bd9Sstevel@tonic-gate 		 * If the access is only asking for READ then there is
11657c478bd9Sstevel@tonic-gate 		 * no conflict and nothing to do.  If it is asking
11667c478bd9Sstevel@tonic-gate 		 * for write, then there will be conflict and the read
11677c478bd9Sstevel@tonic-gate 		 * delegation should be recalled.
11687c478bd9Sstevel@tonic-gate 		 */
11697c478bd9Sstevel@tonic-gate 		if (access == OPEN4_SHARE_ACCESS_READ)
11707c478bd9Sstevel@tonic-gate 			return (FALSE);
11717c478bd9Sstevel@tonic-gate 		else
11727c478bd9Sstevel@tonic-gate 			return (TRUE);
11737c478bd9Sstevel@tonic-gate 	case OPEN_DELEGATE_WRITE:
11747c478bd9Sstevel@tonic-gate 		/* Check to see if this client has the delegation */
11757c478bd9Sstevel@tonic-gate 		return (rfs4_is_deleg(sp));
11767c478bd9Sstevel@tonic-gate 	}
11777c478bd9Sstevel@tonic-gate 
11787c478bd9Sstevel@tonic-gate 	return (FALSE);
11797c478bd9Sstevel@tonic-gate }
11807c478bd9Sstevel@tonic-gate 
11817c478bd9Sstevel@tonic-gate /*
11827c478bd9Sstevel@tonic-gate  * Return the "best" allowable delegation available given the current
11837c478bd9Sstevel@tonic-gate  * delegation type and the desired access and deny modes on the file.
11847c478bd9Sstevel@tonic-gate  * At the point that this routine is called we know that the access and
11857c478bd9Sstevel@tonic-gate  * deny modes are consistent with the file modes.
11867c478bd9Sstevel@tonic-gate  */
11877c478bd9Sstevel@tonic-gate static open_delegation_type4
11887c478bd9Sstevel@tonic-gate rfs4_check_delegation(rfs4_state_t *sp, rfs4_file_t *fp)
11897c478bd9Sstevel@tonic-gate {
11907c478bd9Sstevel@tonic-gate 	open_delegation_type4 dtype = fp->dinfo->dtype;
11917c478bd9Sstevel@tonic-gate 	uint32_t access = sp->share_access;
11927c478bd9Sstevel@tonic-gate 	uint32_t deny = sp->share_deny;
11937c478bd9Sstevel@tonic-gate 	int readcnt = 0;
11947c478bd9Sstevel@tonic-gate 	int writecnt = 0;
11957c478bd9Sstevel@tonic-gate 
11967c478bd9Sstevel@tonic-gate 	switch (dtype) {
11977c478bd9Sstevel@tonic-gate 	case OPEN_DELEGATE_NONE:
11987c478bd9Sstevel@tonic-gate 		/*
11997c478bd9Sstevel@tonic-gate 		 * Determine if more than just this OPEN have the file
12007c478bd9Sstevel@tonic-gate 		 * open and if so, no delegation may be provided to
12017c478bd9Sstevel@tonic-gate 		 * the client.
12027c478bd9Sstevel@tonic-gate 		 */
12037c478bd9Sstevel@tonic-gate 		if (access & OPEN4_SHARE_ACCESS_WRITE)
12047c478bd9Sstevel@tonic-gate 			writecnt++;
12057c478bd9Sstevel@tonic-gate 		if (access & OPEN4_SHARE_ACCESS_READ)
12067c478bd9Sstevel@tonic-gate 			readcnt++;
12077c478bd9Sstevel@tonic-gate 
12087c478bd9Sstevel@tonic-gate 		if (fp->access_read > readcnt || fp->access_write > writecnt)
12097c478bd9Sstevel@tonic-gate 			return (OPEN_DELEGATE_NONE);
12107c478bd9Sstevel@tonic-gate 
12117c478bd9Sstevel@tonic-gate 		/*
12127c478bd9Sstevel@tonic-gate 		 * If the client is going to write, or if the client
12137c478bd9Sstevel@tonic-gate 		 * has exclusive access, return a write delegation.
12147c478bd9Sstevel@tonic-gate 		 */
12157c478bd9Sstevel@tonic-gate 		if ((access & OPEN4_SHARE_ACCESS_WRITE) ||
12167c478bd9Sstevel@tonic-gate 		    (deny & (OPEN4_SHARE_DENY_READ | OPEN4_SHARE_DENY_WRITE)))
12177c478bd9Sstevel@tonic-gate 			return (OPEN_DELEGATE_WRITE);
12187c478bd9Sstevel@tonic-gate 		/*
12197c478bd9Sstevel@tonic-gate 		 * If we don't want to write or we've haven't denied read
12207c478bd9Sstevel@tonic-gate 		 * access to others, return a read delegation.
12217c478bd9Sstevel@tonic-gate 		 */
12227c478bd9Sstevel@tonic-gate 		if ((access & ~OPEN4_SHARE_ACCESS_WRITE) ||
12237c478bd9Sstevel@tonic-gate 			(deny & ~OPEN4_SHARE_DENY_READ))
12247c478bd9Sstevel@tonic-gate 			return (OPEN_DELEGATE_READ);
12257c478bd9Sstevel@tonic-gate 
12267c478bd9Sstevel@tonic-gate 		/* Shouldn't get here */
12277c478bd9Sstevel@tonic-gate 		return (OPEN_DELEGATE_NONE);
12287c478bd9Sstevel@tonic-gate 
12297c478bd9Sstevel@tonic-gate 	case OPEN_DELEGATE_READ:
12307c478bd9Sstevel@tonic-gate 		/*
12317c478bd9Sstevel@tonic-gate 		 * If the file is delegated for read but we wan't to
12327c478bd9Sstevel@tonic-gate 		 * write or deny others to read then we can't delegate
12337c478bd9Sstevel@tonic-gate 		 * the file. We shouldn't get here since the delegation should
12347c478bd9Sstevel@tonic-gate 		 * have been recalled already.
12357c478bd9Sstevel@tonic-gate 		 */
12367c478bd9Sstevel@tonic-gate 		if ((access & OPEN4_SHARE_ACCESS_WRITE) ||
12377c478bd9Sstevel@tonic-gate 		    (deny & OPEN4_SHARE_DENY_READ))
12387c478bd9Sstevel@tonic-gate 		    return (OPEN_DELEGATE_NONE);
12397c478bd9Sstevel@tonic-gate 		return (OPEN_DELEGATE_READ);
12407c478bd9Sstevel@tonic-gate 
12417c478bd9Sstevel@tonic-gate 	case OPEN_DELEGATE_WRITE:
12427c478bd9Sstevel@tonic-gate 		return (OPEN_DELEGATE_WRITE);
12437c478bd9Sstevel@tonic-gate 	}
12447c478bd9Sstevel@tonic-gate 
12457c478bd9Sstevel@tonic-gate 	/* Shouldn't get here */
12467c478bd9Sstevel@tonic-gate 	return (OPEN_DELEGATE_NONE);
12477c478bd9Sstevel@tonic-gate }
12487c478bd9Sstevel@tonic-gate 
12497c478bd9Sstevel@tonic-gate /*
12507c478bd9Sstevel@tonic-gate  * Given the desired delegation type and the "history" of the file
12517c478bd9Sstevel@tonic-gate  * determine the actual delegation type to return.
12527c478bd9Sstevel@tonic-gate  */
12537c478bd9Sstevel@tonic-gate static open_delegation_type4
12547c478bd9Sstevel@tonic-gate rfs4_delegation_policy(open_delegation_type4 dtype,
12557c478bd9Sstevel@tonic-gate 	rfs4_dinfo_t *dinfo, clientid4 cid)
12567c478bd9Sstevel@tonic-gate {
12577c478bd9Sstevel@tonic-gate 	time_t elapsed;
12587c478bd9Sstevel@tonic-gate 
12597c478bd9Sstevel@tonic-gate 	if (rfs4_deleg_policy != SRV_NORMAL_DELEGATE)
12607c478bd9Sstevel@tonic-gate 		return (OPEN_DELEGATE_NONE);
12617c478bd9Sstevel@tonic-gate 
12627c478bd9Sstevel@tonic-gate 	/*
12637c478bd9Sstevel@tonic-gate 	 * Has this file/delegation ever been recalled?  If not then
12647c478bd9Sstevel@tonic-gate 	 * no furhter checks for a delegation race need to be done.
12657c478bd9Sstevel@tonic-gate 	 * However if a recall has occurred, then check to see if a
12667c478bd9Sstevel@tonic-gate 	 * client has caused its own delegation recall to occur.  If
12677c478bd9Sstevel@tonic-gate 	 * not, then has a delegation for this file been returned
12687c478bd9Sstevel@tonic-gate 	 * recently?  If so, then do not assign a new delegation to
12697c478bd9Sstevel@tonic-gate 	 * avoid a "delegation race" between the original client and
12707c478bd9Sstevel@tonic-gate 	 * the new/conflicting client.
12717c478bd9Sstevel@tonic-gate 	 */
12727c478bd9Sstevel@tonic-gate 	if (dinfo->ever_recalled == TRUE) {
12737c478bd9Sstevel@tonic-gate 		if (dinfo->conflicted_client != cid) {
12747c478bd9Sstevel@tonic-gate 			elapsed = gethrestime_sec() - dinfo->time_returned;
12757c478bd9Sstevel@tonic-gate 			if (elapsed < rfs4_lease_time)
12767c478bd9Sstevel@tonic-gate 				return (OPEN_DELEGATE_NONE);
12777c478bd9Sstevel@tonic-gate 		}
12787c478bd9Sstevel@tonic-gate 	}
12797c478bd9Sstevel@tonic-gate 
12807c478bd9Sstevel@tonic-gate 	/* Limit the number of read grants */
12817c478bd9Sstevel@tonic-gate 	if (dtype == OPEN_DELEGATE_READ &&
12827c478bd9Sstevel@tonic-gate 		dinfo->rdgrants > MAX_READ_DELEGATIONS)
12837c478bd9Sstevel@tonic-gate 		return (OPEN_DELEGATE_NONE);
12847c478bd9Sstevel@tonic-gate 
12857c478bd9Sstevel@tonic-gate 	/*
12867c478bd9Sstevel@tonic-gate 	 * Should consider limiting total number of read/write
12877c478bd9Sstevel@tonic-gate 	 * delegations the server will permit.
12887c478bd9Sstevel@tonic-gate 	 */
12897c478bd9Sstevel@tonic-gate 
12907c478bd9Sstevel@tonic-gate 	return (dtype);
12917c478bd9Sstevel@tonic-gate }
12927c478bd9Sstevel@tonic-gate 
12937c478bd9Sstevel@tonic-gate /*
12947c478bd9Sstevel@tonic-gate  * Try and grant a delegation for an open give the state. The routine
12957c478bd9Sstevel@tonic-gate  * returns the delegation type granted. This could be OPEN_DELEGATE_NONE.
12967c478bd9Sstevel@tonic-gate  *
12977c478bd9Sstevel@tonic-gate  * The state and associate file entry must be locked
12987c478bd9Sstevel@tonic-gate  */
12997c478bd9Sstevel@tonic-gate rfs4_deleg_state_t *
13007c478bd9Sstevel@tonic-gate rfs4_grant_delegation(delegreq_t dreq, rfs4_state_t *sp, int *recall)
13017c478bd9Sstevel@tonic-gate {
13027c478bd9Sstevel@tonic-gate 	rfs4_file_t *fp = sp->finfo;
13037c478bd9Sstevel@tonic-gate 	open_delegation_type4 dtype;
13047c478bd9Sstevel@tonic-gate 	int no_delegation;
13057c478bd9Sstevel@tonic-gate 
13067c478bd9Sstevel@tonic-gate 	ASSERT(rfs4_dbe_islocked(sp->dbe));
13077c478bd9Sstevel@tonic-gate 	ASSERT(rfs4_dbe_islocked(fp->dbe));
13087c478bd9Sstevel@tonic-gate 
13097c478bd9Sstevel@tonic-gate 	/* Is the server even providing delegations? */
13107c478bd9Sstevel@tonic-gate 	if (rfs4_deleg_policy == SRV_NEVER_DELEGATE || dreq == DELEG_NONE)
13117c478bd9Sstevel@tonic-gate 		return (NULL);
13127c478bd9Sstevel@tonic-gate 
13137c478bd9Sstevel@tonic-gate 	/* Check to see if delegations have been temporarily disabled */
13147c478bd9Sstevel@tonic-gate 	mutex_enter(&rfs4_deleg_lock);
13157c478bd9Sstevel@tonic-gate 	no_delegation = rfs4_deleg_disabled;
13167c478bd9Sstevel@tonic-gate 	mutex_exit(&rfs4_deleg_lock);
13177c478bd9Sstevel@tonic-gate 
13187c478bd9Sstevel@tonic-gate 	if (no_delegation)
13197c478bd9Sstevel@tonic-gate 		return (NULL);
13207c478bd9Sstevel@tonic-gate 
13217c478bd9Sstevel@tonic-gate 	/* Don't grant a delegation if a deletion is impending. */
13227c478bd9Sstevel@tonic-gate 	if (fp->dinfo->hold_grant > 0) {
13237c478bd9Sstevel@tonic-gate 		NFS4_DEBUG(rfs4_deleg_debug,
13247c478bd9Sstevel@tonic-gate 			(CE_NOTE, "rfs4_grant_delegation: hold_grant is set"));
13257c478bd9Sstevel@tonic-gate 		return (NULL);
13267c478bd9Sstevel@tonic-gate 	}
13277c478bd9Sstevel@tonic-gate 
13287c478bd9Sstevel@tonic-gate 	/*
13297c478bd9Sstevel@tonic-gate 	 * Don't grant a delegation if there are any lock manager
13307c478bd9Sstevel@tonic-gate 	 * (NFSv2/v3) locks for the file.  This is a bit of a hack (e.g.,
13317c478bd9Sstevel@tonic-gate 	 * if there are only read locks we should be able to grant a
13327c478bd9Sstevel@tonic-gate 	 * read-only delegation), but it's good enough for now.
13337c478bd9Sstevel@tonic-gate 	 *
13347c478bd9Sstevel@tonic-gate 	 * MT safety: the lock manager checks for conflicting delegations
13357c478bd9Sstevel@tonic-gate 	 * before processing a lock request.  That check will block until
13367c478bd9Sstevel@tonic-gate 	 * we are done here.  So if the lock manager acquires a lock after
13377c478bd9Sstevel@tonic-gate 	 * we decide to grant the delegation, the delegation will get
13387c478bd9Sstevel@tonic-gate 	 * immediately recalled (if there's a conflict), so we're safe.
13397c478bd9Sstevel@tonic-gate 	 */
13407c478bd9Sstevel@tonic-gate 	if (lm_vp_active(fp->vp)) {
13417c478bd9Sstevel@tonic-gate 		NFS4_DEBUG(rfs4_deleg_debug,
13427c478bd9Sstevel@tonic-gate 		    (CE_NOTE, "rfs4_grant_delegation: NLM lock"));
13437c478bd9Sstevel@tonic-gate 		return (NULL);
13447c478bd9Sstevel@tonic-gate 	}
13457c478bd9Sstevel@tonic-gate 
13467c478bd9Sstevel@tonic-gate 	/*
13477c478bd9Sstevel@tonic-gate 	 * Based on the type of delegation request passed in, take the
13487c478bd9Sstevel@tonic-gate 	 * appropriate action (DELEG_NONE is handled above)
13497c478bd9Sstevel@tonic-gate 	 */
13507c478bd9Sstevel@tonic-gate 	switch (dreq) {
13517c478bd9Sstevel@tonic-gate 
13527c478bd9Sstevel@tonic-gate 	case DELEG_READ:
13537c478bd9Sstevel@tonic-gate 	case DELEG_WRITE:
13547c478bd9Sstevel@tonic-gate 		/*
13557c478bd9Sstevel@tonic-gate 		 * The server "must" grant the delegation in this case.
13567c478bd9Sstevel@tonic-gate 		 * Client is using open previous
13577c478bd9Sstevel@tonic-gate 		 */
13587c478bd9Sstevel@tonic-gate 		dtype = (open_delegation_type4)dreq;
13597c478bd9Sstevel@tonic-gate 		*recall = 1;
13607c478bd9Sstevel@tonic-gate 		break;
13617c478bd9Sstevel@tonic-gate 	case DELEG_ANY:
13627c478bd9Sstevel@tonic-gate 		/*
13637c478bd9Sstevel@tonic-gate 		 * If a valid callback path does not exist, no delegation may
13647c478bd9Sstevel@tonic-gate 		 * be granted.
13657c478bd9Sstevel@tonic-gate 		 */
13667c478bd9Sstevel@tonic-gate 		if (sp->owner->client->cbinfo.cb_state != CB_OK)
13677c478bd9Sstevel@tonic-gate 			return (NULL);
13687c478bd9Sstevel@tonic-gate 
13697c478bd9Sstevel@tonic-gate 		/*
13707c478bd9Sstevel@tonic-gate 		 * If the original operation which caused time_rm_delayed
13717c478bd9Sstevel@tonic-gate 		 * to be set hasn't been retried and completed for one
13727c478bd9Sstevel@tonic-gate 		 * full lease period, clear it and allow delegations to
13737c478bd9Sstevel@tonic-gate 		 * get granted again.
13747c478bd9Sstevel@tonic-gate 		 */
13757c478bd9Sstevel@tonic-gate 		if (fp->dinfo->time_rm_delayed > 0 &&
13767c478bd9Sstevel@tonic-gate 		    gethrestime_sec() >
13777c478bd9Sstevel@tonic-gate 		    fp->dinfo->time_rm_delayed + rfs4_lease_time)
13787c478bd9Sstevel@tonic-gate 			fp->dinfo->time_rm_delayed = 0;
13797c478bd9Sstevel@tonic-gate 
13807c478bd9Sstevel@tonic-gate 		/*
13817c478bd9Sstevel@tonic-gate 		 * If we are waiting for a delegation to be returned then
13827c478bd9Sstevel@tonic-gate 		 * don't delegate this file. We do this for correctness as
13837c478bd9Sstevel@tonic-gate 		 * well as if the file is being recalled we would likely
13847c478bd9Sstevel@tonic-gate 		 * recall this file again.
13857c478bd9Sstevel@tonic-gate 		 */
13867c478bd9Sstevel@tonic-gate 
13877c478bd9Sstevel@tonic-gate 		if (fp->dinfo->time_recalled != 0 ||
13887c478bd9Sstevel@tonic-gate 		    fp->dinfo->time_rm_delayed != 0)
13897c478bd9Sstevel@tonic-gate 			return (NULL);
13907c478bd9Sstevel@tonic-gate 
13917c478bd9Sstevel@tonic-gate 		/* Get the "best" delegation candidate */
13927c478bd9Sstevel@tonic-gate 		dtype = rfs4_check_delegation(sp, fp);
13937c478bd9Sstevel@tonic-gate 
13947c478bd9Sstevel@tonic-gate 		if (dtype == OPEN_DELEGATE_NONE)
13957c478bd9Sstevel@tonic-gate 			return (NULL);
13967c478bd9Sstevel@tonic-gate 
13977c478bd9Sstevel@tonic-gate 		/*
13987c478bd9Sstevel@tonic-gate 		 * Based on policy and the history of the file get the
13997c478bd9Sstevel@tonic-gate 		 * actual delegation.
14007c478bd9Sstevel@tonic-gate 		 */
14017c478bd9Sstevel@tonic-gate 		dtype = rfs4_delegation_policy(dtype, fp->dinfo,
14027c478bd9Sstevel@tonic-gate 			sp->owner->client->clientid);
14037c478bd9Sstevel@tonic-gate 
14047c478bd9Sstevel@tonic-gate 		NFS4_DEBUG(rfs4_deleg_debug,
14057c478bd9Sstevel@tonic-gate 			(CE_NOTE, "Grant policy dtype = %d", dtype));
14067c478bd9Sstevel@tonic-gate 		if (dtype == OPEN_DELEGATE_NONE)
14077c478bd9Sstevel@tonic-gate 			return (NULL);
14087c478bd9Sstevel@tonic-gate 		break;
14097c478bd9Sstevel@tonic-gate 	default:
14107c478bd9Sstevel@tonic-gate 		return (NULL);
14117c478bd9Sstevel@tonic-gate 	}
14127c478bd9Sstevel@tonic-gate 
14137c478bd9Sstevel@tonic-gate 	/* set the delegation for the state */
14147c478bd9Sstevel@tonic-gate 	return (rfs4_deleg_state(sp, dtype, recall));
14157c478bd9Sstevel@tonic-gate }
14167c478bd9Sstevel@tonic-gate 
14177c478bd9Sstevel@tonic-gate void
14187c478bd9Sstevel@tonic-gate rfs4_set_deleg_response(rfs4_deleg_state_t *dsp, open_delegation4 *dp,
14197c478bd9Sstevel@tonic-gate 			nfsace4 *ace,  int recall)
14207c478bd9Sstevel@tonic-gate {
14217c478bd9Sstevel@tonic-gate 	open_write_delegation4 *wp;
14227c478bd9Sstevel@tonic-gate 	open_read_delegation4 *rp;
14237c478bd9Sstevel@tonic-gate 	nfs_space_limit4 *spl;
14247c478bd9Sstevel@tonic-gate 	nfsace4 nace;
14257c478bd9Sstevel@tonic-gate 
14267c478bd9Sstevel@tonic-gate 	/*
14277c478bd9Sstevel@tonic-gate 	 * We need to allocate a new copy of the who string.
14287c478bd9Sstevel@tonic-gate 	 * this string will be freed by the rfs4_op_open dis_resfree
14297c478bd9Sstevel@tonic-gate 	 * routine. We need to do this allocation since replays will
14307c478bd9Sstevel@tonic-gate 	 * be allocated and rfs4_compound can't tell the difference from
14317c478bd9Sstevel@tonic-gate 	 * a replay and an inital open. N.B. if an ace is passed in, it
14327c478bd9Sstevel@tonic-gate 	 * the caller's responsibility to free it.
14337c478bd9Sstevel@tonic-gate 	 */
14347c478bd9Sstevel@tonic-gate 
14357c478bd9Sstevel@tonic-gate 	if (ace == NULL) {
14367c478bd9Sstevel@tonic-gate 		/*
14377c478bd9Sstevel@tonic-gate 		 * Default is to deny all access, the client will have
14387c478bd9Sstevel@tonic-gate 		 * to contact the server.  XXX Do we want to actually
14397c478bd9Sstevel@tonic-gate 		 * set a deny for every one, or do we simply want to
14407c478bd9Sstevel@tonic-gate 		 * construct an entity that will match no one?
14417c478bd9Sstevel@tonic-gate 		 */
14427c478bd9Sstevel@tonic-gate 		nace.type = ACE4_ACCESS_DENIED_ACE_TYPE;
14437c478bd9Sstevel@tonic-gate 		nace.flag = 0;
14447c478bd9Sstevel@tonic-gate 		nace.access_mask = ACE4_VALID_MASK_BITS;
14457c478bd9Sstevel@tonic-gate 		(void) str_to_utf8(ACE4_WHO_EVERYONE, &nace.who);
14467c478bd9Sstevel@tonic-gate 	} else {
14477c478bd9Sstevel@tonic-gate 		nace.type = ace->type;
14487c478bd9Sstevel@tonic-gate 		nace.flag = ace->flag;
14497c478bd9Sstevel@tonic-gate 		nace.access_mask = ace->access_mask;
14507c478bd9Sstevel@tonic-gate 		(void) utf8_copy(&ace->who, &nace.who);
14517c478bd9Sstevel@tonic-gate 	}
14527c478bd9Sstevel@tonic-gate 
14537c478bd9Sstevel@tonic-gate 	dp->delegation_type = dsp->dtype;
14547c478bd9Sstevel@tonic-gate 
14557c478bd9Sstevel@tonic-gate 	switch (dsp->dtype) {
14567c478bd9Sstevel@tonic-gate 	case OPEN_DELEGATE_NONE:
14577c478bd9Sstevel@tonic-gate 		break;
14587c478bd9Sstevel@tonic-gate 	case OPEN_DELEGATE_READ:
14597c478bd9Sstevel@tonic-gate 		rp = &dp->open_delegation4_u.read;
14607c478bd9Sstevel@tonic-gate 		rp->stateid = dsp->delegid.stateid;
14617c478bd9Sstevel@tonic-gate 		rp->recall = (bool_t)recall;
14627c478bd9Sstevel@tonic-gate 		rp->permissions = nace;
14637c478bd9Sstevel@tonic-gate 		break;
14647c478bd9Sstevel@tonic-gate 	case OPEN_DELEGATE_WRITE:
14657c478bd9Sstevel@tonic-gate 		wp = &dp->open_delegation4_u.write;
14667c478bd9Sstevel@tonic-gate 		wp->stateid = dsp->delegid.stateid;
14677c478bd9Sstevel@tonic-gate 		wp->recall = (bool_t)recall;
14687c478bd9Sstevel@tonic-gate 		spl = &wp->space_limit;
14697c478bd9Sstevel@tonic-gate 		spl->limitby = NFS_LIMIT_SIZE;
14707c478bd9Sstevel@tonic-gate 		spl->nfs_space_limit4_u.filesize = 0;
14717c478bd9Sstevel@tonic-gate 		wp->permissions = nace;
14727c478bd9Sstevel@tonic-gate 		break;
14737c478bd9Sstevel@tonic-gate 	}
14747c478bd9Sstevel@tonic-gate }
14757c478bd9Sstevel@tonic-gate 
14767c478bd9Sstevel@tonic-gate /*
14777c478bd9Sstevel@tonic-gate  * Check if the file is delegated via the provided file struct.
14787c478bd9Sstevel@tonic-gate  * Return TRUE if it is delegated.  This is intended for use by
14797c478bd9Sstevel@tonic-gate  * the v4 server.  The v2/v3 server code should use rfs4_check_delegated().
14807c478bd9Sstevel@tonic-gate  *
14817c478bd9Sstevel@tonic-gate  * Note that if the file is found to have a delegation, it is
14827c478bd9Sstevel@tonic-gate  * recalled, unless the clientid of the caller matches the clientid of the
14837c478bd9Sstevel@tonic-gate  * delegation. If the caller has specified, there is a slight delay
14847c478bd9Sstevel@tonic-gate  * inserted in the hopes that the delegation will be returned quickly.
14857c478bd9Sstevel@tonic-gate  */
14867c478bd9Sstevel@tonic-gate bool_t
14877c478bd9Sstevel@tonic-gate rfs4_check_delegated_byfp(int mode, rfs4_file_t *fp,
14887c478bd9Sstevel@tonic-gate 	bool_t trunc, bool_t do_delay, bool_t is_rm, clientid4 *cp)
14897c478bd9Sstevel@tonic-gate {
14907c478bd9Sstevel@tonic-gate 	rfs4_deleg_state_t *dsp;
14917c478bd9Sstevel@tonic-gate 
14927c478bd9Sstevel@tonic-gate 	/* Is delegation enabled? */
14937c478bd9Sstevel@tonic-gate 	if (rfs4_deleg_policy == SRV_NEVER_DELEGATE)
14947c478bd9Sstevel@tonic-gate 		return (FALSE);
14957c478bd9Sstevel@tonic-gate 
14967c478bd9Sstevel@tonic-gate 	/* do we have a delegation on this file? */
14977c478bd9Sstevel@tonic-gate 	rfs4_dbe_lock(fp->dbe);
14987c478bd9Sstevel@tonic-gate 	if (fp->dinfo->dtype == OPEN_DELEGATE_NONE) {
14997c478bd9Sstevel@tonic-gate 		if (is_rm)
15007c478bd9Sstevel@tonic-gate 			fp->dinfo->hold_grant++;
15017c478bd9Sstevel@tonic-gate 		rfs4_dbe_unlock(fp->dbe);
15027c478bd9Sstevel@tonic-gate 		return (FALSE);
15037c478bd9Sstevel@tonic-gate 	}
15047c478bd9Sstevel@tonic-gate 	/*
15057c478bd9Sstevel@tonic-gate 	 * do we have a write delegation on this file or are we
15067c478bd9Sstevel@tonic-gate 	 * requesting write access to a file with any type of existing
15077c478bd9Sstevel@tonic-gate 	 * delegation?
15087c478bd9Sstevel@tonic-gate 	 */
15097c478bd9Sstevel@tonic-gate 	if (mode == FWRITE || fp->dinfo->dtype == OPEN_DELEGATE_WRITE) {
15107c478bd9Sstevel@tonic-gate 		if (cp != NULL) {
15117c478bd9Sstevel@tonic-gate 			dsp = fp->delegationlist.next->dsp;
15127c478bd9Sstevel@tonic-gate 			if (dsp == NULL) {
15137c478bd9Sstevel@tonic-gate 				rfs4_dbe_unlock(fp->dbe);
15147c478bd9Sstevel@tonic-gate 				return (FALSE);
15157c478bd9Sstevel@tonic-gate 			}
15167c478bd9Sstevel@tonic-gate 			/*
15177c478bd9Sstevel@tonic-gate 			 * Does the requestor already own the delegation?
15187c478bd9Sstevel@tonic-gate 			 */
15197c478bd9Sstevel@tonic-gate 			if (dsp->client->clientid == *(cp)) {
15207c478bd9Sstevel@tonic-gate 				rfs4_dbe_unlock(fp->dbe);
15217c478bd9Sstevel@tonic-gate 				return (FALSE);
15227c478bd9Sstevel@tonic-gate 			}
15237c478bd9Sstevel@tonic-gate 		}
15247c478bd9Sstevel@tonic-gate 
15257c478bd9Sstevel@tonic-gate 		rfs4_dbe_unlock(fp->dbe);
15267c478bd9Sstevel@tonic-gate 		rfs4_recall_deleg(fp, trunc, NULL);
15277c478bd9Sstevel@tonic-gate 
15287c478bd9Sstevel@tonic-gate 		if (!do_delay) {
15297c478bd9Sstevel@tonic-gate 			rfs4_dbe_lock(fp->dbe);
15307c478bd9Sstevel@tonic-gate 			fp->dinfo->time_rm_delayed = gethrestime_sec();
15317c478bd9Sstevel@tonic-gate 			rfs4_dbe_unlock(fp->dbe);
15327c478bd9Sstevel@tonic-gate 			return (TRUE);
15337c478bd9Sstevel@tonic-gate 		}
15347c478bd9Sstevel@tonic-gate 
15357c478bd9Sstevel@tonic-gate 		delay(NFS4_DELEGATION_CONFLICT_DELAY);
15367c478bd9Sstevel@tonic-gate 
15377c478bd9Sstevel@tonic-gate 		rfs4_dbe_lock(fp->dbe);
15387c478bd9Sstevel@tonic-gate 		if (fp->dinfo->dtype != OPEN_DELEGATE_NONE) {
15397c478bd9Sstevel@tonic-gate 			fp->dinfo->time_rm_delayed = gethrestime_sec();
15407c478bd9Sstevel@tonic-gate 			rfs4_dbe_unlock(fp->dbe);
15417c478bd9Sstevel@tonic-gate 			return (TRUE);
15427c478bd9Sstevel@tonic-gate 		}
15437c478bd9Sstevel@tonic-gate 	}
15447c478bd9Sstevel@tonic-gate 	if (is_rm)
15457c478bd9Sstevel@tonic-gate 		fp->dinfo->hold_grant++;
15467c478bd9Sstevel@tonic-gate 	rfs4_dbe_unlock(fp->dbe);
15477c478bd9Sstevel@tonic-gate 	return (FALSE);
15487c478bd9Sstevel@tonic-gate }
15497c478bd9Sstevel@tonic-gate 
15507c478bd9Sstevel@tonic-gate /*
15517c478bd9Sstevel@tonic-gate  * Check if the file is delegated in the case of a v2 or v3 access.
15527c478bd9Sstevel@tonic-gate  * Return TRUE if it is delegated which in turn means that v2 should
15537c478bd9Sstevel@tonic-gate  * drop the request and in the case of v3 JUKEBOX should be returned.
15547c478bd9Sstevel@tonic-gate  */
15557c478bd9Sstevel@tonic-gate bool_t
15567c478bd9Sstevel@tonic-gate rfs4_check_delegated(int mode, vnode_t *vp, bool_t trunc)
15577c478bd9Sstevel@tonic-gate {
15587c478bd9Sstevel@tonic-gate 	rfs4_file_t *fp;
15597c478bd9Sstevel@tonic-gate 	bool_t create = FALSE;
15607c478bd9Sstevel@tonic-gate 	bool_t rc = FALSE;
15617c478bd9Sstevel@tonic-gate 
15627c478bd9Sstevel@tonic-gate 	rfs4_hold_deleg_policy();
15637c478bd9Sstevel@tonic-gate 
15647c478bd9Sstevel@tonic-gate 	/* Is delegation enabled? */
15657c478bd9Sstevel@tonic-gate 	if (rfs4_deleg_policy != SRV_NEVER_DELEGATE) {
15667c478bd9Sstevel@tonic-gate 		fp = rfs4_findfile(vp, NULL, &create);
15677c478bd9Sstevel@tonic-gate 		if (fp != NULL) {
15687c478bd9Sstevel@tonic-gate 			if (rfs4_check_delegated_byfp(mode, fp, trunc,
15697c478bd9Sstevel@tonic-gate 						TRUE, FALSE, NULL)) {
15707c478bd9Sstevel@tonic-gate 				rc = TRUE;
15717c478bd9Sstevel@tonic-gate 			}
15727c478bd9Sstevel@tonic-gate 			rfs4_file_rele(fp);
15737c478bd9Sstevel@tonic-gate 		}
15747c478bd9Sstevel@tonic-gate 	}
15757c478bd9Sstevel@tonic-gate 	rfs4_rele_deleg_policy();
15767c478bd9Sstevel@tonic-gate 	return (rc);
15777c478bd9Sstevel@tonic-gate }
15787c478bd9Sstevel@tonic-gate 
15797c478bd9Sstevel@tonic-gate /*
15807c478bd9Sstevel@tonic-gate  * Release a hold on the hold_grant counter which
15817c478bd9Sstevel@tonic-gate  * prevents delegation from being granted while a remove
15827c478bd9Sstevel@tonic-gate  * or a rename is in progress.
15837c478bd9Sstevel@tonic-gate  */
15847c478bd9Sstevel@tonic-gate void
15857c478bd9Sstevel@tonic-gate rfs4_clear_dont_grant(rfs4_file_t *fp)
15867c478bd9Sstevel@tonic-gate {
15877c478bd9Sstevel@tonic-gate 	if (rfs4_deleg_policy == SRV_NEVER_DELEGATE)
15887c478bd9Sstevel@tonic-gate 		return;
15897c478bd9Sstevel@tonic-gate 	rfs4_dbe_lock(fp->dbe);
15907c478bd9Sstevel@tonic-gate 	ASSERT(fp->dinfo->hold_grant > 0);
15917c478bd9Sstevel@tonic-gate 	fp->dinfo->hold_grant--;
15927c478bd9Sstevel@tonic-gate 	fp->dinfo->time_rm_delayed = 0;
15937c478bd9Sstevel@tonic-gate 	rfs4_dbe_unlock(fp->dbe);
15947c478bd9Sstevel@tonic-gate }
15957c478bd9Sstevel@tonic-gate 
15967c478bd9Sstevel@tonic-gate /*
15977c478bd9Sstevel@tonic-gate  * State support for delegation.
15987c478bd9Sstevel@tonic-gate  * Set the state delegation type for this state;
15997c478bd9Sstevel@tonic-gate  * This routine is called from open via rfs4_grant_delegation and the entry
16007c478bd9Sstevel@tonic-gate  * locks on sp and sp->finfo are assumed.
16017c478bd9Sstevel@tonic-gate  */
16027c478bd9Sstevel@tonic-gate static rfs4_deleg_state_t *
16037c478bd9Sstevel@tonic-gate rfs4_deleg_state(rfs4_state_t *sp, open_delegation_type4 dtype, int *recall)
16047c478bd9Sstevel@tonic-gate {
16057c478bd9Sstevel@tonic-gate 	rfs4_file_t *fp = sp->finfo;
16067c478bd9Sstevel@tonic-gate 	bool_t create = TRUE;
16077c478bd9Sstevel@tonic-gate 	rfs4_deleg_state_t *dsp;
16087c478bd9Sstevel@tonic-gate 	vnode_t *vp;
16097c478bd9Sstevel@tonic-gate 	int open_prev = *recall;
16107c478bd9Sstevel@tonic-gate 
16117c478bd9Sstevel@tonic-gate 	ASSERT(rfs4_dbe_islocked(fp->dbe));
16127c478bd9Sstevel@tonic-gate 
16137c478bd9Sstevel@tonic-gate 	/* Shouldn't happen */
16147c478bd9Sstevel@tonic-gate 	if (fp->dinfo->recall_count != 0 ||
16157c478bd9Sstevel@tonic-gate 		(fp->dinfo->dtype == OPEN_DELEGATE_READ &&
16167c478bd9Sstevel@tonic-gate 			dtype != OPEN_DELEGATE_READ)) {
16177c478bd9Sstevel@tonic-gate 		return (NULL);
16187c478bd9Sstevel@tonic-gate 	}
16197c478bd9Sstevel@tonic-gate 
16207c478bd9Sstevel@tonic-gate 	/* Unlock to avoid deadlock */
16217c478bd9Sstevel@tonic-gate 	rfs4_dbe_unlock(fp->dbe);
16227c478bd9Sstevel@tonic-gate 
16237c478bd9Sstevel@tonic-gate 	dsp = rfs4_finddeleg(sp, &create);
16247c478bd9Sstevel@tonic-gate 
16257c478bd9Sstevel@tonic-gate 	rfs4_dbe_lock(fp->dbe);
16267c478bd9Sstevel@tonic-gate 
16277c478bd9Sstevel@tonic-gate 	if (dsp == NULL)
16287c478bd9Sstevel@tonic-gate 		return (NULL);
16297c478bd9Sstevel@tonic-gate 
16307c478bd9Sstevel@tonic-gate 	if (create == FALSE) {
16317c478bd9Sstevel@tonic-gate 		if (sp->owner->client == dsp->client &&
16327c478bd9Sstevel@tonic-gate 		    dsp->dtype == dtype) {
16337c478bd9Sstevel@tonic-gate 			return (dsp);
16347c478bd9Sstevel@tonic-gate 		} else {
16357c478bd9Sstevel@tonic-gate 			rfs4_deleg_state_rele(dsp);
16367c478bd9Sstevel@tonic-gate 			return (NULL);
16377c478bd9Sstevel@tonic-gate 		}
16387c478bd9Sstevel@tonic-gate 	}
16397c478bd9Sstevel@tonic-gate 
16407c478bd9Sstevel@tonic-gate 	/*
16417c478bd9Sstevel@tonic-gate 	 * Check that this file has not been delegated to another
16427c478bd9Sstevel@tonic-gate 	 * client
16437c478bd9Sstevel@tonic-gate 	 */
16447c478bd9Sstevel@tonic-gate 	if (fp->dinfo->recall_count != 0 ||
16457c478bd9Sstevel@tonic-gate 		fp->dinfo->dtype == OPEN_DELEGATE_WRITE ||
16467c478bd9Sstevel@tonic-gate 		(fp->dinfo->dtype == OPEN_DELEGATE_READ &&
16477c478bd9Sstevel@tonic-gate 			dtype != OPEN_DELEGATE_READ)) {
16487c478bd9Sstevel@tonic-gate 		rfs4_deleg_state_rele(dsp);
16497c478bd9Sstevel@tonic-gate 		return (NULL);
16507c478bd9Sstevel@tonic-gate 	}
16517c478bd9Sstevel@tonic-gate 
16527c478bd9Sstevel@tonic-gate 	vp = fp->vp;
16537c478bd9Sstevel@tonic-gate 	/* vnevent_support returns 0 if file system supports vnevents */
16547c478bd9Sstevel@tonic-gate 	if (vnevent_support(vp)) {
16557c478bd9Sstevel@tonic-gate 		rfs4_deleg_state_rele(dsp);
16567c478bd9Sstevel@tonic-gate 		return (NULL);
16577c478bd9Sstevel@tonic-gate 	}
16587c478bd9Sstevel@tonic-gate 
16597c478bd9Sstevel@tonic-gate 	*recall = 0;
16607c478bd9Sstevel@tonic-gate 	if (dtype == OPEN_DELEGATE_READ) {
16617c478bd9Sstevel@tonic-gate 		if (vn_is_opened(vp, V_WRITE) || vn_is_mapped(vp, V_WRITE)) {
16627c478bd9Sstevel@tonic-gate 			if (open_prev) {
16637c478bd9Sstevel@tonic-gate 				*recall = 1;
16647c478bd9Sstevel@tonic-gate 			} else {
16657c478bd9Sstevel@tonic-gate 				rfs4_deleg_state_rele(dsp);
16667c478bd9Sstevel@tonic-gate 				return (NULL);
16677c478bd9Sstevel@tonic-gate 			}
16687c478bd9Sstevel@tonic-gate 		}
16697c478bd9Sstevel@tonic-gate 		(void) fem_install(vp, deleg_rdops, (void *)fp, OPUNIQ,
16707c478bd9Sstevel@tonic-gate 				rfs4_mon_hold, rfs4_mon_rele);
16717c478bd9Sstevel@tonic-gate 		if (vn_is_opened(vp, V_WRITE) || vn_is_mapped(vp, V_WRITE)) {
16727c478bd9Sstevel@tonic-gate 			if (open_prev) {
16737c478bd9Sstevel@tonic-gate 				*recall = 1;
16747c478bd9Sstevel@tonic-gate 			} else {
16757c478bd9Sstevel@tonic-gate 				(void) fem_uninstall(vp, deleg_rdops,
16767c478bd9Sstevel@tonic-gate 						    (void *)fp);
16777c478bd9Sstevel@tonic-gate 				rfs4_deleg_state_rele(dsp);
16787c478bd9Sstevel@tonic-gate 				return (NULL);
16797c478bd9Sstevel@tonic-gate 			}
16807c478bd9Sstevel@tonic-gate 		}
16817c478bd9Sstevel@tonic-gate 	} else { /* WRITE */
16827c478bd9Sstevel@tonic-gate 		if (vn_is_opened(vp, V_RDORWR) || vn_is_mapped(vp, V_RDORWR)) {
16837c478bd9Sstevel@tonic-gate 			if (open_prev) {
16847c478bd9Sstevel@tonic-gate 				*recall = 1;
16857c478bd9Sstevel@tonic-gate 			} else {
16867c478bd9Sstevel@tonic-gate 				rfs4_deleg_state_rele(dsp);
16877c478bd9Sstevel@tonic-gate 				return (NULL);
16887c478bd9Sstevel@tonic-gate 			}
16897c478bd9Sstevel@tonic-gate 		}
16907c478bd9Sstevel@tonic-gate 		(void) fem_install(vp, deleg_wrops, (void *)fp, OPUNIQ,
16917c478bd9Sstevel@tonic-gate 				rfs4_mon_hold, rfs4_mon_rele);
16927c478bd9Sstevel@tonic-gate 		if (vn_is_opened(vp, V_RDORWR) || vn_is_mapped(vp, V_RDORWR)) {
16937c478bd9Sstevel@tonic-gate 			if (open_prev) {
16947c478bd9Sstevel@tonic-gate 				*recall = 1;
16957c478bd9Sstevel@tonic-gate 			} else {
16967c478bd9Sstevel@tonic-gate 				(void) fem_uninstall(vp, deleg_wrops,
16977c478bd9Sstevel@tonic-gate 							(void *)fp);
16987c478bd9Sstevel@tonic-gate 				rfs4_deleg_state_rele(dsp);
16997c478bd9Sstevel@tonic-gate 				return (NULL);
17007c478bd9Sstevel@tonic-gate 			}
17017c478bd9Sstevel@tonic-gate 		}
17027c478bd9Sstevel@tonic-gate 	}
17037c478bd9Sstevel@tonic-gate 	/* Place on delegation list for file */
17047c478bd9Sstevel@tonic-gate 	insque(&dsp->delegationlist, fp->delegationlist.prev);
17057c478bd9Sstevel@tonic-gate 
17067c478bd9Sstevel@tonic-gate 	dsp->dtype = fp->dinfo->dtype = dtype;
17077c478bd9Sstevel@tonic-gate 
17087c478bd9Sstevel@tonic-gate 	/* Update delegation stats for this file */
17097c478bd9Sstevel@tonic-gate 	fp->dinfo->time_lastgrant = gethrestime_sec();
17107c478bd9Sstevel@tonic-gate 
17117c478bd9Sstevel@tonic-gate 	/* reset since this is a new delegation */
17127c478bd9Sstevel@tonic-gate 	fp->dinfo->conflicted_client = 0;
17137c478bd9Sstevel@tonic-gate 	fp->dinfo->ever_recalled = FALSE;
17147c478bd9Sstevel@tonic-gate 
17157c478bd9Sstevel@tonic-gate 	if (dtype == OPEN_DELEGATE_READ)
17167c478bd9Sstevel@tonic-gate 		fp->dinfo->rdgrants++;
17177c478bd9Sstevel@tonic-gate 	else
17187c478bd9Sstevel@tonic-gate 		fp->dinfo->wrgrants++;
17197c478bd9Sstevel@tonic-gate 
17207c478bd9Sstevel@tonic-gate 	return (dsp);
17217c478bd9Sstevel@tonic-gate }
17227c478bd9Sstevel@tonic-gate 
17237c478bd9Sstevel@tonic-gate /*
17247c478bd9Sstevel@tonic-gate  * State routine for the server when a delegation is returned.
17257c478bd9Sstevel@tonic-gate  */
17267c478bd9Sstevel@tonic-gate void
17277c478bd9Sstevel@tonic-gate rfs4_return_deleg(rfs4_deleg_state_t *dsp, bool_t revoked)
17287c478bd9Sstevel@tonic-gate {
17297c478bd9Sstevel@tonic-gate 	rfs4_file_t *fp = dsp->finfo;
17307c478bd9Sstevel@tonic-gate 	open_delegation_type4 dtypewas;
17317c478bd9Sstevel@tonic-gate 
17327c478bd9Sstevel@tonic-gate 	rfs4_dbe_lock(fp->dbe);
17337c478bd9Sstevel@tonic-gate 	/* Remove state from recall list */
17347c478bd9Sstevel@tonic-gate 
17357c478bd9Sstevel@tonic-gate 	remque(&dsp->delegationlist);
17367c478bd9Sstevel@tonic-gate 	dsp->delegationlist.next = dsp->delegationlist.prev =
17377c478bd9Sstevel@tonic-gate 		&dsp->delegationlist;
17387c478bd9Sstevel@tonic-gate 
17397c478bd9Sstevel@tonic-gate 	if (&fp->delegationlist == fp->delegationlist.next) {
17407c478bd9Sstevel@tonic-gate 		dtypewas = fp->dinfo->dtype;
17417c478bd9Sstevel@tonic-gate 		fp->dinfo->dtype = OPEN_DELEGATE_NONE;
17427c478bd9Sstevel@tonic-gate 		rfs4_dbe_cv_broadcast(fp->dbe);
17437c478bd9Sstevel@tonic-gate 
17447c478bd9Sstevel@tonic-gate 		/* if file system was unshared, the vp will be NULL */
17457c478bd9Sstevel@tonic-gate 		if (fp->vp != NULL) {
17467c478bd9Sstevel@tonic-gate 			if (dtypewas == OPEN_DELEGATE_READ)
17477c478bd9Sstevel@tonic-gate 				(void) fem_uninstall(fp->vp, deleg_rdops,
17487c478bd9Sstevel@tonic-gate 						(void *)fp);
17497c478bd9Sstevel@tonic-gate 			else
17507c478bd9Sstevel@tonic-gate 				(void) fem_uninstall(fp->vp, deleg_wrops,
17517c478bd9Sstevel@tonic-gate 						(void *)fp);
17527c478bd9Sstevel@tonic-gate 		}
17537c478bd9Sstevel@tonic-gate 	}
17547c478bd9Sstevel@tonic-gate 
17557c478bd9Sstevel@tonic-gate 	switch (dsp->dtype) {
17567c478bd9Sstevel@tonic-gate 	case OPEN_DELEGATE_READ:
17577c478bd9Sstevel@tonic-gate 		fp->dinfo->rdgrants--;
17587c478bd9Sstevel@tonic-gate 		break;
17597c478bd9Sstevel@tonic-gate 	case OPEN_DELEGATE_WRITE:
17607c478bd9Sstevel@tonic-gate 		fp->dinfo->wrgrants--;
17617c478bd9Sstevel@tonic-gate 		break;
17627c478bd9Sstevel@tonic-gate 	default:
17637c478bd9Sstevel@tonic-gate 		break;
17647c478bd9Sstevel@tonic-gate 	}
17657c478bd9Sstevel@tonic-gate 
17667c478bd9Sstevel@tonic-gate 	/* used in the policy decision */
17677c478bd9Sstevel@tonic-gate 	fp->dinfo->time_returned = gethrestime_sec();
17687c478bd9Sstevel@tonic-gate 
17697c478bd9Sstevel@tonic-gate 	/*
17707c478bd9Sstevel@tonic-gate 	 * reset the time_recalled field so future delegations are not
17717c478bd9Sstevel@tonic-gate 	 * accidentally revoked
17727c478bd9Sstevel@tonic-gate 	 */
17737c478bd9Sstevel@tonic-gate 	if ((fp->dinfo->rdgrants + fp->dinfo->wrgrants) == 0)
17747c478bd9Sstevel@tonic-gate 		fp->dinfo->time_recalled = 0;
17757c478bd9Sstevel@tonic-gate 
17767c478bd9Sstevel@tonic-gate 	rfs4_dbe_unlock(fp->dbe);
17777c478bd9Sstevel@tonic-gate 
17787c478bd9Sstevel@tonic-gate 	rfs4_dbe_lock(dsp->dbe);
17797c478bd9Sstevel@tonic-gate 
17807c478bd9Sstevel@tonic-gate 	dsp->dtype = OPEN_DELEGATE_NONE;
17817c478bd9Sstevel@tonic-gate 
17827c478bd9Sstevel@tonic-gate 	if (revoked == TRUE)
17837c478bd9Sstevel@tonic-gate 		dsp->time_revoked = gethrestime_sec();
17847c478bd9Sstevel@tonic-gate 
17857c478bd9Sstevel@tonic-gate 	rfs4_dbe_invalidate(dsp->dbe);
17867c478bd9Sstevel@tonic-gate 
17877c478bd9Sstevel@tonic-gate 	rfs4_dbe_unlock(dsp->dbe);
17887c478bd9Sstevel@tonic-gate 
17897c478bd9Sstevel@tonic-gate 	if (revoked == TRUE) {
17907c478bd9Sstevel@tonic-gate 		rfs4_dbe_lock(dsp->client->dbe);
17917c478bd9Sstevel@tonic-gate 		dsp->client->deleg_revoked++;	/* observability */
17927c478bd9Sstevel@tonic-gate 		rfs4_dbe_unlock(dsp->client->dbe);
17937c478bd9Sstevel@tonic-gate 	}
17947c478bd9Sstevel@tonic-gate }
17957c478bd9Sstevel@tonic-gate 
17967c478bd9Sstevel@tonic-gate static void
17977c478bd9Sstevel@tonic-gate rfs4_revoke_deleg(rfs4_deleg_state_t *dsp)
17987c478bd9Sstevel@tonic-gate {
17997c478bd9Sstevel@tonic-gate 	rfs4_return_deleg(dsp, TRUE);
18007c478bd9Sstevel@tonic-gate }
18017c478bd9Sstevel@tonic-gate 
18027c478bd9Sstevel@tonic-gate static void
18037c478bd9Sstevel@tonic-gate rfs4_revoke_file(rfs4_file_t *fp)
18047c478bd9Sstevel@tonic-gate {
18057c478bd9Sstevel@tonic-gate 	rfs4_deleg_state_t *dsp;
18067c478bd9Sstevel@tonic-gate 
18077c478bd9Sstevel@tonic-gate 	/*
18087c478bd9Sstevel@tonic-gate 	 * The lock for rfs4_file_t must be held when traversing the
18097c478bd9Sstevel@tonic-gate 	 * delegation list but that lock needs to be released to call
18107c478bd9Sstevel@tonic-gate 	 * rfs4_revoke_deleg()
18117c478bd9Sstevel@tonic-gate 	 * This for loop is set up to check the list for being empty,
18127c478bd9Sstevel@tonic-gate 	 * and locking the rfs4_file_t struct on init and end
18137c478bd9Sstevel@tonic-gate 	 */
18147c478bd9Sstevel@tonic-gate 	for (rfs4_dbe_lock(fp->dbe);
18157c478bd9Sstevel@tonic-gate 		&fp->delegationlist != fp->delegationlist.next;
18167c478bd9Sstevel@tonic-gate 		rfs4_dbe_lock(fp->dbe)) {
18177c478bd9Sstevel@tonic-gate 
18187c478bd9Sstevel@tonic-gate 		dsp = fp->delegationlist.next->dsp;
18197c478bd9Sstevel@tonic-gate 		rfs4_dbe_hold(dsp->dbe);
18207c478bd9Sstevel@tonic-gate 		rfs4_dbe_unlock(fp->dbe);
18217c478bd9Sstevel@tonic-gate 		rfs4_revoke_deleg(dsp);
18227c478bd9Sstevel@tonic-gate 		rfs4_deleg_state_rele(dsp);
18237c478bd9Sstevel@tonic-gate 	}
18247c478bd9Sstevel@tonic-gate 	rfs4_dbe_unlock(fp->dbe);
18257c478bd9Sstevel@tonic-gate }
18267c478bd9Sstevel@tonic-gate 
18277c478bd9Sstevel@tonic-gate /*
18287c478bd9Sstevel@tonic-gate  * A delegation is assumed to be present on the file associated with
18297c478bd9Sstevel@tonic-gate  * "state".  Check to see if the delegation matches is associated with
18307c478bd9Sstevel@tonic-gate  * the same client as referenced by "state".  If it is not, TRUE is
18317c478bd9Sstevel@tonic-gate  * returned.  If the delegation DOES match the client (or no
18327c478bd9Sstevel@tonic-gate  * delegation is present), return FALSE.
18337c478bd9Sstevel@tonic-gate  * Assume the state entry and file entry are locked.
18347c478bd9Sstevel@tonic-gate  */
18357c478bd9Sstevel@tonic-gate bool_t
18367c478bd9Sstevel@tonic-gate rfs4_is_deleg(rfs4_state_t *state)
18377c478bd9Sstevel@tonic-gate {
18387c478bd9Sstevel@tonic-gate 	rfs4_deleg_state_t *dsp;
18397c478bd9Sstevel@tonic-gate 	rfs4_file_t *fp = state->finfo;
18407c478bd9Sstevel@tonic-gate 	rfs4_client_t *cp = state->owner->client;
18417c478bd9Sstevel@tonic-gate 
18427c478bd9Sstevel@tonic-gate 	NFS4_DEBUG(rfs4_deleg_debug,
18437c478bd9Sstevel@tonic-gate 		(CE_NOTE, "rfs4_is_deleg enter: cp = %p", (void*)cp));
18447c478bd9Sstevel@tonic-gate 
18457c478bd9Sstevel@tonic-gate 	ASSERT(rfs4_dbe_islocked(fp->dbe));
18467c478bd9Sstevel@tonic-gate 	for (dsp = fp->delegationlist.next->dsp; dsp != NULL;
18477c478bd9Sstevel@tonic-gate 		dsp = dsp->delegationlist.next->dsp) {
18487c478bd9Sstevel@tonic-gate 		NFS4_DEBUG(rfs4_deleg_debug,
18497c478bd9Sstevel@tonic-gate 			(CE_NOTE, "rfs4_is_deleg: client = %p",
18507c478bd9Sstevel@tonic-gate 			(void*)dsp->client));
18517c478bd9Sstevel@tonic-gate 		if (cp != dsp->client) {
18527c478bd9Sstevel@tonic-gate 			NFS4_DEBUG(rfs4_deleg_debug,
18537c478bd9Sstevel@tonic-gate 				(CE_NOTE, "rfs4_is_deleg is true"));
18547c478bd9Sstevel@tonic-gate 			return (TRUE);
18557c478bd9Sstevel@tonic-gate 		}
18567c478bd9Sstevel@tonic-gate 	}
18577c478bd9Sstevel@tonic-gate 	return (FALSE);
18587c478bd9Sstevel@tonic-gate }
18597c478bd9Sstevel@tonic-gate 
18607c478bd9Sstevel@tonic-gate void
18617c478bd9Sstevel@tonic-gate rfs4_disable_delegation(void)
18627c478bd9Sstevel@tonic-gate {
18637c478bd9Sstevel@tonic-gate 	mutex_enter(&rfs4_deleg_lock);
18647c478bd9Sstevel@tonic-gate 	rfs4_deleg_disabled++;
18657c478bd9Sstevel@tonic-gate 	mutex_exit(&rfs4_deleg_lock);
18667c478bd9Sstevel@tonic-gate }
18677c478bd9Sstevel@tonic-gate 
18687c478bd9Sstevel@tonic-gate void
18697c478bd9Sstevel@tonic-gate rfs4_enable_delegation(void)
18707c478bd9Sstevel@tonic-gate {
18717c478bd9Sstevel@tonic-gate 	mutex_enter(&rfs4_deleg_lock);
18727c478bd9Sstevel@tonic-gate 	ASSERT(rfs4_deleg_disabled > 0);
18737c478bd9Sstevel@tonic-gate 	rfs4_deleg_disabled--;
18747c478bd9Sstevel@tonic-gate 	mutex_exit(&rfs4_deleg_lock);
18757c478bd9Sstevel@tonic-gate }
18767c478bd9Sstevel@tonic-gate 
18777c478bd9Sstevel@tonic-gate void
18787c478bd9Sstevel@tonic-gate rfs4_mon_hold(void *arg)
18797c478bd9Sstevel@tonic-gate {
18807c478bd9Sstevel@tonic-gate 	rfs4_file_t *fp = arg;
18817c478bd9Sstevel@tonic-gate 
18827c478bd9Sstevel@tonic-gate 	rfs4_dbe_hold(fp->dbe);
18837c478bd9Sstevel@tonic-gate }
18847c478bd9Sstevel@tonic-gate 
18857c478bd9Sstevel@tonic-gate void
18867c478bd9Sstevel@tonic-gate rfs4_mon_rele(void *arg)
18877c478bd9Sstevel@tonic-gate {
18887c478bd9Sstevel@tonic-gate 	rfs4_file_t *fp = arg;
18897c478bd9Sstevel@tonic-gate 
18907c478bd9Sstevel@tonic-gate 	rfs4_dbe_rele_nolock(fp->dbe);
18917c478bd9Sstevel@tonic-gate }
1892