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
5852c68eeSjwahlig  * Common Development and Distribution License (the "License").
6852c68eeSjwahlig  * You may not use this file except in compliance with the License.
77c478bd9Sstevel@tonic-gate  *
87c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
97c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
107c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
117c478bd9Sstevel@tonic-gate  * and limitations under the License.
127c478bd9Sstevel@tonic-gate  *
137c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
147c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
157c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
167c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
177c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
187c478bd9Sstevel@tonic-gate  *
197c478bd9Sstevel@tonic-gate  * CDDL HEADER END
207c478bd9Sstevel@tonic-gate  */
217c478bd9Sstevel@tonic-gate /*
22bbf2a467SNagakiran Rajashekar  * Copyright 2008 Sun Microsystems, Inc.  All rights reserved.
237c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
247c478bd9Sstevel@tonic-gate  */
257c478bd9Sstevel@tonic-gate 
267c478bd9Sstevel@tonic-gate /* Copyright (c) 1983, 1984, 1985, 1986, 1987, 1988, 1989 AT&T */
277c478bd9Sstevel@tonic-gate /* All Rights Reserved */
287c478bd9Sstevel@tonic-gate 
297c478bd9Sstevel@tonic-gate 
307c478bd9Sstevel@tonic-gate #include <nfs/nfs4_clnt.h>
317c478bd9Sstevel@tonic-gate #include <nfs/rnode4.h>
327c478bd9Sstevel@tonic-gate #include <sys/systm.h>
337c478bd9Sstevel@tonic-gate #include <sys/cmn_err.h>
347c478bd9Sstevel@tonic-gate #include <sys/atomic.h>
357c478bd9Sstevel@tonic-gate 
367c478bd9Sstevel@tonic-gate static void	nfs4_free_open_owner(nfs4_open_owner_t *, mntinfo4_t *);
377c478bd9Sstevel@tonic-gate static nfs4_open_owner_t *find_freed_open_owner(cred_t *,
387c478bd9Sstevel@tonic-gate 				nfs4_oo_hash_bucket_t *, mntinfo4_t *);
397c478bd9Sstevel@tonic-gate static open_delegation_type4 get_dtype(rnode4_t *);
407c478bd9Sstevel@tonic-gate 
417c478bd9Sstevel@tonic-gate #ifdef DEBUG
427c478bd9Sstevel@tonic-gate int nfs4_client_foo_debug = 0x0;
437c478bd9Sstevel@tonic-gate int nfs4_client_open_dg = 0x0;
447c478bd9Sstevel@tonic-gate /*
457c478bd9Sstevel@tonic-gate  * If this is non-zero, the lockowner and openowner seqid sync primitives
467c478bd9Sstevel@tonic-gate  * will intermittently return errors.
477c478bd9Sstevel@tonic-gate  */
487c478bd9Sstevel@tonic-gate static int seqid_sync_faults = 0;
497c478bd9Sstevel@tonic-gate #endif
507c478bd9Sstevel@tonic-gate 
517c478bd9Sstevel@tonic-gate stateid4 clnt_special0 = {
527c478bd9Sstevel@tonic-gate 	0,
537c478bd9Sstevel@tonic-gate 	{ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }
547c478bd9Sstevel@tonic-gate };
557c478bd9Sstevel@tonic-gate 
567c478bd9Sstevel@tonic-gate stateid4 clnt_special1 = {
577c478bd9Sstevel@tonic-gate 	0xffffffff,
587c478bd9Sstevel@tonic-gate 	{
597c478bd9Sstevel@tonic-gate 		(char)0xff, (char)0xff, (char)0xff, (char)0xff,
607c478bd9Sstevel@tonic-gate 		(char)0xff, (char)0xff, (char)0xff, (char)0xff,
617c478bd9Sstevel@tonic-gate 		(char)0xff, (char)0xff, (char)0xff, (char)0xff
627c478bd9Sstevel@tonic-gate 	}
637c478bd9Sstevel@tonic-gate };
647c478bd9Sstevel@tonic-gate 
657c478bd9Sstevel@tonic-gate /* finds hash bucket and locks it */
667c478bd9Sstevel@tonic-gate static nfs4_oo_hash_bucket_t *
lock_bucket(cred_t * cr,mntinfo4_t * mi)677c478bd9Sstevel@tonic-gate lock_bucket(cred_t *cr, mntinfo4_t *mi)
687c478bd9Sstevel@tonic-gate {
697c478bd9Sstevel@tonic-gate 	nfs4_oo_hash_bucket_t *bucketp;
707c478bd9Sstevel@tonic-gate 	uint32_t hash_key;
717c478bd9Sstevel@tonic-gate 
727c478bd9Sstevel@tonic-gate 	hash_key = (uint32_t)(crgetuid(cr) + crgetruid(cr))
73bbf2a467SNagakiran Rajashekar 	    % NFS4_NUM_OO_BUCKETS;
747c478bd9Sstevel@tonic-gate 	NFS4_DEBUG(nfs4_client_state_debug, (CE_NOTE, "lock_bucket: "
75bbf2a467SNagakiran Rajashekar 	    "hash_key %d for cred %p", hash_key, (void*)cr));
767c478bd9Sstevel@tonic-gate 
777c478bd9Sstevel@tonic-gate 	ASSERT(hash_key >= 0 && hash_key < NFS4_NUM_OO_BUCKETS);
787c478bd9Sstevel@tonic-gate 	ASSERT(mi != NULL);
797c478bd9Sstevel@tonic-gate 	ASSERT(mutex_owned(&mi->mi_lock));
807c478bd9Sstevel@tonic-gate 
817c478bd9Sstevel@tonic-gate 	bucketp = &(mi->mi_oo_list[hash_key]);
827c478bd9Sstevel@tonic-gate 	mutex_enter(&bucketp->b_lock);
837c478bd9Sstevel@tonic-gate 	return (bucketp);
847c478bd9Sstevel@tonic-gate }
857c478bd9Sstevel@tonic-gate 
867c478bd9Sstevel@tonic-gate /* unlocks hash bucket pointed by bucket_ptr */
877c478bd9Sstevel@tonic-gate static void
unlock_bucket(nfs4_oo_hash_bucket_t * bucketp)887c478bd9Sstevel@tonic-gate unlock_bucket(nfs4_oo_hash_bucket_t *bucketp)
897c478bd9Sstevel@tonic-gate {
907c478bd9Sstevel@tonic-gate 	mutex_exit(&bucketp->b_lock);
917c478bd9Sstevel@tonic-gate }
927c478bd9Sstevel@tonic-gate 
937c478bd9Sstevel@tonic-gate /*
947c478bd9Sstevel@tonic-gate  * Removes the lock owner from the rnode's lock_owners list and frees the
957c478bd9Sstevel@tonic-gate  * corresponding reference.
967c478bd9Sstevel@tonic-gate  */
977c478bd9Sstevel@tonic-gate void
nfs4_rnode_remove_lock_owner(rnode4_t * rp,nfs4_lock_owner_t * lop)987c478bd9Sstevel@tonic-gate nfs4_rnode_remove_lock_owner(rnode4_t *rp, nfs4_lock_owner_t *lop)
997c478bd9Sstevel@tonic-gate {
1007c478bd9Sstevel@tonic-gate 	NFS4_DEBUG(nfs4_client_state_debug, (CE_NOTE,
101bbf2a467SNagakiran Rajashekar 	    "nfs4_rnode_remove_lock_owner"));
1027c478bd9Sstevel@tonic-gate 
1037c478bd9Sstevel@tonic-gate 	mutex_enter(&rp->r_statev4_lock);
1047c478bd9Sstevel@tonic-gate 
1057c478bd9Sstevel@tonic-gate 	if (lop->lo_next_rnode == NULL) {
1067c478bd9Sstevel@tonic-gate 		/* already removed from list */
1077c478bd9Sstevel@tonic-gate 		mutex_exit(&rp->r_statev4_lock);
1087c478bd9Sstevel@tonic-gate 		return;
1097c478bd9Sstevel@tonic-gate 	}
1107c478bd9Sstevel@tonic-gate 
1117c478bd9Sstevel@tonic-gate 	ASSERT(lop->lo_prev_rnode != NULL);
1127c478bd9Sstevel@tonic-gate 
1137c478bd9Sstevel@tonic-gate 	lop->lo_prev_rnode->lo_next_rnode = lop->lo_next_rnode;
1147c478bd9Sstevel@tonic-gate 	lop->lo_next_rnode->lo_prev_rnode = lop->lo_prev_rnode;
1157c478bd9Sstevel@tonic-gate 
1167c478bd9Sstevel@tonic-gate 	lop->lo_next_rnode = lop->lo_prev_rnode = NULL;
1177c478bd9Sstevel@tonic-gate 
1187c478bd9Sstevel@tonic-gate 	mutex_exit(&rp->r_statev4_lock);
1197c478bd9Sstevel@tonic-gate 
1207c478bd9Sstevel@tonic-gate 	/*
1217c478bd9Sstevel@tonic-gate 	 * This would be an appropriate place for
1227c478bd9Sstevel@tonic-gate 	 * RELEASE_LOCKOWNER.  For now, this is overkill
1237c478bd9Sstevel@tonic-gate 	 * because in the common case, close is going to
1247c478bd9Sstevel@tonic-gate 	 * release any lockowners anyway.
1257c478bd9Sstevel@tonic-gate 	 */
1267c478bd9Sstevel@tonic-gate 	lock_owner_rele(lop);
1277c478bd9Sstevel@tonic-gate }
1287c478bd9Sstevel@tonic-gate 
1297c478bd9Sstevel@tonic-gate /*
1307c478bd9Sstevel@tonic-gate  * Remove all lock owners from the rnode's lock_owners list.  Frees up
1317c478bd9Sstevel@tonic-gate  * their references from the list.
1327c478bd9Sstevel@tonic-gate  */
1337c478bd9Sstevel@tonic-gate 
1347c478bd9Sstevel@tonic-gate void
nfs4_flush_lock_owners(rnode4_t * rp)1357c478bd9Sstevel@tonic-gate nfs4_flush_lock_owners(rnode4_t *rp)
1367c478bd9Sstevel@tonic-gate {
1377c478bd9Sstevel@tonic-gate 	nfs4_lock_owner_t *lop;
1387c478bd9Sstevel@tonic-gate 
1397c478bd9Sstevel@tonic-gate 	mutex_enter(&rp->r_statev4_lock);
1407c478bd9Sstevel@tonic-gate 	while (rp->r_lo_head.lo_next_rnode != &rp->r_lo_head) {
1417c478bd9Sstevel@tonic-gate 		lop = rp->r_lo_head.lo_next_rnode;
1427c478bd9Sstevel@tonic-gate 		lop->lo_prev_rnode->lo_next_rnode = lop->lo_next_rnode;
1437c478bd9Sstevel@tonic-gate 		lop->lo_next_rnode->lo_prev_rnode = lop->lo_prev_rnode;
1447c478bd9Sstevel@tonic-gate 		lop->lo_next_rnode = lop->lo_prev_rnode = NULL;
1457c478bd9Sstevel@tonic-gate 		lock_owner_rele(lop);
1467c478bd9Sstevel@tonic-gate 	}
1477c478bd9Sstevel@tonic-gate 	mutex_exit(&rp->r_statev4_lock);
1487c478bd9Sstevel@tonic-gate }
1497c478bd9Sstevel@tonic-gate 
1507c478bd9Sstevel@tonic-gate void
nfs4_clear_open_streams(rnode4_t * rp)1517c478bd9Sstevel@tonic-gate nfs4_clear_open_streams(rnode4_t *rp)
1527c478bd9Sstevel@tonic-gate {
1537c478bd9Sstevel@tonic-gate 	nfs4_open_stream_t *osp;
1547c478bd9Sstevel@tonic-gate 
1557c478bd9Sstevel@tonic-gate 	mutex_enter(&rp->r_os_lock);
1567c478bd9Sstevel@tonic-gate 	while ((osp = list_head(&rp->r_open_streams)) != NULL) {
1577c478bd9Sstevel@tonic-gate 		open_owner_rele(osp->os_open_owner);
1587c478bd9Sstevel@tonic-gate 		list_remove(&rp->r_open_streams, osp);
1597c478bd9Sstevel@tonic-gate 		mutex_destroy(&osp->os_sync_lock);
1607c478bd9Sstevel@tonic-gate 		osp->os_open_owner = NULL;
1617c478bd9Sstevel@tonic-gate 		kmem_free(osp, sizeof (*osp));
1627c478bd9Sstevel@tonic-gate 	}
1637c478bd9Sstevel@tonic-gate 	mutex_exit(&rp->r_os_lock);
1647c478bd9Sstevel@tonic-gate }
1657c478bd9Sstevel@tonic-gate 
1667c478bd9Sstevel@tonic-gate void
open_owner_hold(nfs4_open_owner_t * oop)1677c478bd9Sstevel@tonic-gate open_owner_hold(nfs4_open_owner_t *oop)
1687c478bd9Sstevel@tonic-gate {
1697c478bd9Sstevel@tonic-gate 	mutex_enter(&oop->oo_lock);
1707c478bd9Sstevel@tonic-gate 	oop->oo_ref_count++;
1717c478bd9Sstevel@tonic-gate 	mutex_exit(&oop->oo_lock);
1727c478bd9Sstevel@tonic-gate }
1737c478bd9Sstevel@tonic-gate 
1747c478bd9Sstevel@tonic-gate /*
1757c478bd9Sstevel@tonic-gate  * Frees the open owner if the ref count hits zero.
1767c478bd9Sstevel@tonic-gate  */
1777c478bd9Sstevel@tonic-gate void
open_owner_rele(nfs4_open_owner_t * oop)1787c478bd9Sstevel@tonic-gate open_owner_rele(nfs4_open_owner_t *oop)
1797c478bd9Sstevel@tonic-gate {
1807c478bd9Sstevel@tonic-gate 	NFS4_DEBUG(nfs4_client_state_debug, (CE_NOTE,
181bbf2a467SNagakiran Rajashekar 	    "open_owner_rele"));
1827c478bd9Sstevel@tonic-gate 
1837c478bd9Sstevel@tonic-gate 	mutex_enter(&oop->oo_lock);
1847c478bd9Sstevel@tonic-gate 	oop->oo_ref_count--;
1857c478bd9Sstevel@tonic-gate 	if (oop->oo_ref_count == 0) {
1867c478bd9Sstevel@tonic-gate 		NFS4_DEBUG(nfs4_client_state_debug, (CE_NOTE,
187bbf2a467SNagakiran Rajashekar 		    "open_owner_rele: freeing open owner"));
1887c478bd9Sstevel@tonic-gate 		oop->oo_valid = 0;
1897c478bd9Sstevel@tonic-gate 		mutex_exit(&oop->oo_lock);
1907c478bd9Sstevel@tonic-gate 		/*
1917c478bd9Sstevel@tonic-gate 		 * Ok, we don't destroy the open owner, nor do we put it on
1927c478bd9Sstevel@tonic-gate 		 * the mntinfo4's free list just yet.  We are lazy about it
1937c478bd9Sstevel@tonic-gate 		 * and let callers to find_open_owner() do that to keep locking
1947c478bd9Sstevel@tonic-gate 		 * simple.
1957c478bd9Sstevel@tonic-gate 		 */
1967c478bd9Sstevel@tonic-gate 	} else {
1977c478bd9Sstevel@tonic-gate 		mutex_exit(&oop->oo_lock);
1987c478bd9Sstevel@tonic-gate 	}
1997c478bd9Sstevel@tonic-gate }
2007c478bd9Sstevel@tonic-gate 
2017c478bd9Sstevel@tonic-gate void
open_stream_hold(nfs4_open_stream_t * osp)2027c478bd9Sstevel@tonic-gate open_stream_hold(nfs4_open_stream_t *osp)
2037c478bd9Sstevel@tonic-gate {
2047c478bd9Sstevel@tonic-gate 	mutex_enter(&osp->os_sync_lock);
2057c478bd9Sstevel@tonic-gate 	osp->os_ref_count++;
2067c478bd9Sstevel@tonic-gate 	mutex_exit(&osp->os_sync_lock);
2077c478bd9Sstevel@tonic-gate }
2087c478bd9Sstevel@tonic-gate 
2097c478bd9Sstevel@tonic-gate /*
2107c478bd9Sstevel@tonic-gate  * Frees the open stream and removes it from the rnode4's open streams list if
2117c478bd9Sstevel@tonic-gate  * the ref count drops to zero.
2127c478bd9Sstevel@tonic-gate  */
2137c478bd9Sstevel@tonic-gate void
open_stream_rele(nfs4_open_stream_t * osp,rnode4_t * rp)2147c478bd9Sstevel@tonic-gate open_stream_rele(nfs4_open_stream_t *osp, rnode4_t *rp)
2157c478bd9Sstevel@tonic-gate {
2167c478bd9Sstevel@tonic-gate 	NFS4_DEBUG(nfs4_client_state_debug, (CE_NOTE,
217bbf2a467SNagakiran Rajashekar 	    "open_stream_rele"));
2187c478bd9Sstevel@tonic-gate 
2197c478bd9Sstevel@tonic-gate 	ASSERT(!mutex_owned(&rp->r_os_lock));
2207c478bd9Sstevel@tonic-gate 
2217c478bd9Sstevel@tonic-gate 	mutex_enter(&osp->os_sync_lock);
2227c478bd9Sstevel@tonic-gate 	ASSERT(osp->os_ref_count > 0);
2237c478bd9Sstevel@tonic-gate 	osp->os_ref_count--;
2247c478bd9Sstevel@tonic-gate 	if (osp->os_ref_count == 0) {
2257c478bd9Sstevel@tonic-gate 		nfs4_open_owner_t *tmp_oop;
2267c478bd9Sstevel@tonic-gate 
2277c478bd9Sstevel@tonic-gate 		NFS4_DEBUG(nfs4_client_state_debug, (CE_NOTE,
228bbf2a467SNagakiran Rajashekar 		    "open_stream_rele: freeing open stream"));
2297c478bd9Sstevel@tonic-gate 		osp->os_valid = 0;
2307c478bd9Sstevel@tonic-gate 		tmp_oop = osp->os_open_owner;
2317c478bd9Sstevel@tonic-gate 		mutex_exit(&osp->os_sync_lock);
2327c478bd9Sstevel@tonic-gate 
2337c478bd9Sstevel@tonic-gate 		/* now see if we need to destroy the open owner */
2347c478bd9Sstevel@tonic-gate 		open_owner_rele(tmp_oop);
2357c478bd9Sstevel@tonic-gate 
2367c478bd9Sstevel@tonic-gate 		mutex_enter(&rp->r_os_lock);
2377c478bd9Sstevel@tonic-gate 		list_remove(&rp->r_open_streams, osp);
2387c478bd9Sstevel@tonic-gate 		mutex_exit(&rp->r_os_lock);
2397c478bd9Sstevel@tonic-gate 
2407c478bd9Sstevel@tonic-gate 		/* free up osp */
2417c478bd9Sstevel@tonic-gate 		mutex_destroy(&osp->os_sync_lock);
2427c478bd9Sstevel@tonic-gate 		osp->os_open_owner = NULL;
2437c478bd9Sstevel@tonic-gate 		kmem_free(osp, sizeof (*osp));
2447c478bd9Sstevel@tonic-gate 	} else {
2457c478bd9Sstevel@tonic-gate 		mutex_exit(&osp->os_sync_lock);
2467c478bd9Sstevel@tonic-gate 	}
2477c478bd9Sstevel@tonic-gate }
2487c478bd9Sstevel@tonic-gate 
2497c478bd9Sstevel@tonic-gate void
lock_owner_hold(nfs4_lock_owner_t * lop)2507c478bd9Sstevel@tonic-gate lock_owner_hold(nfs4_lock_owner_t *lop)
2517c478bd9Sstevel@tonic-gate {
2527c478bd9Sstevel@tonic-gate 	mutex_enter(&lop->lo_lock);
2537c478bd9Sstevel@tonic-gate 	lop->lo_ref_count++;
2547c478bd9Sstevel@tonic-gate 	mutex_exit(&lop->lo_lock);
2557c478bd9Sstevel@tonic-gate }
2567c478bd9Sstevel@tonic-gate 
2577c478bd9Sstevel@tonic-gate /*
2587c478bd9Sstevel@tonic-gate  * Frees the lock owner if the ref count hits zero and
2597c478bd9Sstevel@tonic-gate  * the structure no longer has no locks.
2607c478bd9Sstevel@tonic-gate  */
2617c478bd9Sstevel@tonic-gate void
lock_owner_rele(nfs4_lock_owner_t * lop)2627c478bd9Sstevel@tonic-gate lock_owner_rele(nfs4_lock_owner_t *lop)
2637c478bd9Sstevel@tonic-gate {
2647c478bd9Sstevel@tonic-gate 	NFS4_DEBUG(nfs4_client_state_debug, (CE_NOTE,
265bbf2a467SNagakiran Rajashekar 	    "lock_owner_rele"));
2667c478bd9Sstevel@tonic-gate 
2677c478bd9Sstevel@tonic-gate 	mutex_enter(&lop->lo_lock);
2687c478bd9Sstevel@tonic-gate 	lop->lo_ref_count--;
2697c478bd9Sstevel@tonic-gate 	if (lop->lo_ref_count == 0) {
2707c478bd9Sstevel@tonic-gate 		NFS4_DEBUG(nfs4_client_state_debug, (CE_NOTE,
271bbf2a467SNagakiran Rajashekar 		    "lock_owner_rele: freeing lock owner: "
272bbf2a467SNagakiran Rajashekar 		    "%x", lop->lo_pid));
2737c478bd9Sstevel@tonic-gate 		lop->lo_valid = 0;
2747c478bd9Sstevel@tonic-gate 		/*
2757c478bd9Sstevel@tonic-gate 		 * If there are no references, the lock_owner should
2767c478bd9Sstevel@tonic-gate 		 * already be off the rnode's list.
2777c478bd9Sstevel@tonic-gate 		 */
2787c478bd9Sstevel@tonic-gate 		ASSERT(lop->lo_next_rnode == NULL);
2797c478bd9Sstevel@tonic-gate 		ASSERT(lop->lo_prev_rnode == NULL);
2807c478bd9Sstevel@tonic-gate 		ASSERT(!(lop->lo_flags & NFS4_LOCK_SEQID_INUSE));
2817c478bd9Sstevel@tonic-gate 		ASSERT(lop->lo_seqid_holder == NULL);
2827c478bd9Sstevel@tonic-gate 		mutex_exit(&lop->lo_lock);
2837c478bd9Sstevel@tonic-gate 
2847c478bd9Sstevel@tonic-gate 		/* free up lop */
2857c478bd9Sstevel@tonic-gate 		cv_destroy(&lop->lo_cv_seqid_sync);
2867c478bd9Sstevel@tonic-gate 		mutex_destroy(&lop->lo_lock);
2877c478bd9Sstevel@tonic-gate 		kmem_free(lop, sizeof (*lop));
2887c478bd9Sstevel@tonic-gate 	} else {
2897c478bd9Sstevel@tonic-gate 		mutex_exit(&lop->lo_lock);
2907c478bd9Sstevel@tonic-gate 	}
2917c478bd9Sstevel@tonic-gate }
2927c478bd9Sstevel@tonic-gate 
2937c478bd9Sstevel@tonic-gate /*
2947c478bd9Sstevel@tonic-gate  * This increments the open owner ref count if found.
2957c478bd9Sstevel@tonic-gate  * The argument 'just_created' determines whether we are looking for open
2967c478bd9Sstevel@tonic-gate  * owners with the 'oo_just_created' flag set or not.
2977c478bd9Sstevel@tonic-gate  */
2987c478bd9Sstevel@tonic-gate nfs4_open_owner_t *
find_open_owner_nolock(cred_t * cr,int just_created,mntinfo4_t * mi)2997c478bd9Sstevel@tonic-gate find_open_owner_nolock(cred_t *cr, int just_created, mntinfo4_t *mi)
3007c478bd9Sstevel@tonic-gate {
3017c478bd9Sstevel@tonic-gate 	nfs4_open_owner_t	*oop = NULL, *next_oop;
3027c478bd9Sstevel@tonic-gate 	nfs4_oo_hash_bucket_t	*bucketp;
3037c478bd9Sstevel@tonic-gate 
3047c478bd9Sstevel@tonic-gate 	NFS4_DEBUG(nfs4_client_state_debug, (CE_NOTE,
3057c478bd9Sstevel@tonic-gate 	    "find_open_owner: cred %p, just_created %d",
3067c478bd9Sstevel@tonic-gate 	    (void*)cr, just_created));
3077c478bd9Sstevel@tonic-gate 
3087c478bd9Sstevel@tonic-gate 	ASSERT(mi != NULL);
3097c478bd9Sstevel@tonic-gate 	ASSERT(mutex_owned(&mi->mi_lock));
3107c478bd9Sstevel@tonic-gate 
3117c478bd9Sstevel@tonic-gate 	bucketp = lock_bucket(cr, mi);
3127c478bd9Sstevel@tonic-gate 
3137c478bd9Sstevel@tonic-gate 	/* got hash bucket, search through open owners */
3147c478bd9Sstevel@tonic-gate 	for (oop = list_head(&bucketp->b_oo_hash_list); oop != NULL; ) {
3157c478bd9Sstevel@tonic-gate 		mutex_enter(&oop->oo_lock);
3167c478bd9Sstevel@tonic-gate 		if (!crcmp(oop->oo_cred, cr) &&
3177c478bd9Sstevel@tonic-gate 		    (oop->oo_just_created == just_created ||
3187c478bd9Sstevel@tonic-gate 		    just_created == NFS4_JUST_CREATED)) {
3197c478bd9Sstevel@tonic-gate 			/* match */
3207c478bd9Sstevel@tonic-gate 			if (oop->oo_valid == 0) {
3217c478bd9Sstevel@tonic-gate 				/* reactivate the open owner */
3227c478bd9Sstevel@tonic-gate 				oop->oo_valid = 1;
3237c478bd9Sstevel@tonic-gate 				ASSERT(oop->oo_ref_count == 0);
3247c478bd9Sstevel@tonic-gate 			}
3257c478bd9Sstevel@tonic-gate 			oop->oo_ref_count++;
3267c478bd9Sstevel@tonic-gate 			mutex_exit(&oop->oo_lock);
3277c478bd9Sstevel@tonic-gate 			unlock_bucket(bucketp);
3287c478bd9Sstevel@tonic-gate 			return (oop);
3297c478bd9Sstevel@tonic-gate 		}
3307c478bd9Sstevel@tonic-gate 		next_oop = list_next(&bucketp->b_oo_hash_list, oop);
3317c478bd9Sstevel@tonic-gate 		if (oop->oo_valid == 0) {
3327c478bd9Sstevel@tonic-gate 			list_remove(&bucketp->b_oo_hash_list, oop);
3337c478bd9Sstevel@tonic-gate 
3347c478bd9Sstevel@tonic-gate 			/*
3357c478bd9Sstevel@tonic-gate 			 * Now we go ahead and put this open owner
3367c478bd9Sstevel@tonic-gate 			 * on the freed list.  This is our lazy method.
3377c478bd9Sstevel@tonic-gate 			 */
3387c478bd9Sstevel@tonic-gate 			nfs4_free_open_owner(oop, mi);
3397c478bd9Sstevel@tonic-gate 		}
3407c478bd9Sstevel@tonic-gate 
3417c478bd9Sstevel@tonic-gate 		mutex_exit(&oop->oo_lock);
3427c478bd9Sstevel@tonic-gate 		oop = next_oop;
3437c478bd9Sstevel@tonic-gate 	}
3447c478bd9Sstevel@tonic-gate 
3457c478bd9Sstevel@tonic-gate 	/* search through recently freed open owners */
3467c478bd9Sstevel@tonic-gate 	oop = find_freed_open_owner(cr, bucketp, mi);
3477c478bd9Sstevel@tonic-gate 
3487c478bd9Sstevel@tonic-gate 	unlock_bucket(bucketp);
3497c478bd9Sstevel@tonic-gate 
3507c478bd9Sstevel@tonic-gate 	return (oop);
3517c478bd9Sstevel@tonic-gate }
3527c478bd9Sstevel@tonic-gate 
3537c478bd9Sstevel@tonic-gate nfs4_open_owner_t *
find_open_owner(cred_t * cr,int just_created,mntinfo4_t * mi)3547c478bd9Sstevel@tonic-gate find_open_owner(cred_t *cr, int just_created, mntinfo4_t *mi)
3557c478bd9Sstevel@tonic-gate {
3567c478bd9Sstevel@tonic-gate 	nfs4_open_owner_t *oop;
3577c478bd9Sstevel@tonic-gate 
3587c478bd9Sstevel@tonic-gate 	mutex_enter(&mi->mi_lock);
3597c478bd9Sstevel@tonic-gate 	oop = find_open_owner_nolock(cr, just_created, mi);
3607c478bd9Sstevel@tonic-gate 	mutex_exit(&mi->mi_lock);
3617c478bd9Sstevel@tonic-gate 
3627c478bd9Sstevel@tonic-gate 	return (oop);
3637c478bd9Sstevel@tonic-gate }
3647c478bd9Sstevel@tonic-gate 
3657c478bd9Sstevel@tonic-gate /*
3667c478bd9Sstevel@tonic-gate  * This increments osp's ref count if found.
3677c478bd9Sstevel@tonic-gate  * Returns with 'os_sync_lock' held.
3687c478bd9Sstevel@tonic-gate  */
3697c478bd9Sstevel@tonic-gate nfs4_open_stream_t *
find_open_stream(nfs4_open_owner_t * oop,rnode4_t * rp)3707c478bd9Sstevel@tonic-gate find_open_stream(nfs4_open_owner_t *oop, rnode4_t *rp)
3717c478bd9Sstevel@tonic-gate {
3727c478bd9Sstevel@tonic-gate 	nfs4_open_stream_t	*osp;
3737c478bd9Sstevel@tonic-gate 
3747c478bd9Sstevel@tonic-gate 	NFS4_DEBUG(nfs4_client_state_debug, (CE_NOTE,
375bbf2a467SNagakiran Rajashekar 	    "find_open_stream"));
3767c478bd9Sstevel@tonic-gate 
3777c478bd9Sstevel@tonic-gate 	mutex_enter(&rp->r_os_lock);
3787c478bd9Sstevel@tonic-gate 	/* Now, no one can add or delete to rp's open streams list */
3797c478bd9Sstevel@tonic-gate 	for (osp = list_head(&rp->r_open_streams); osp != NULL;
3807c478bd9Sstevel@tonic-gate 	    osp = list_next(&rp->r_open_streams, osp)) {
3817c478bd9Sstevel@tonic-gate 		mutex_enter(&osp->os_sync_lock);
3827c478bd9Sstevel@tonic-gate 		if (osp->os_open_owner == oop && osp->os_valid != 0) {
3837c478bd9Sstevel@tonic-gate 			/* match */
3847c478bd9Sstevel@tonic-gate 			NFS4_DEBUG(nfs4_client_state_debug,
385bbf2a467SNagakiran Rajashekar 			    (CE_NOTE, "find_open_stream "
386bbf2a467SNagakiran Rajashekar 			    "got a match"));
3877c478bd9Sstevel@tonic-gate 
3887c478bd9Sstevel@tonic-gate 			osp->os_ref_count++;
3897c478bd9Sstevel@tonic-gate 			mutex_exit(&rp->r_os_lock);
3907c478bd9Sstevel@tonic-gate 			return (osp);
3917c478bd9Sstevel@tonic-gate 		}
3927c478bd9Sstevel@tonic-gate 		mutex_exit(&osp->os_sync_lock);
3937c478bd9Sstevel@tonic-gate 	}
3947c478bd9Sstevel@tonic-gate 
3957c478bd9Sstevel@tonic-gate 	mutex_exit(&rp->r_os_lock);
3967c478bd9Sstevel@tonic-gate 	return (NULL);
3977c478bd9Sstevel@tonic-gate }
3987c478bd9Sstevel@tonic-gate 
3997c478bd9Sstevel@tonic-gate /*
4007c478bd9Sstevel@tonic-gate  * Find the lock owner for the given file and process ID.  If "which" is
4017c478bd9Sstevel@tonic-gate  * LOWN_VALID_STATEID, require that the lock owner contain a valid stateid
4027c478bd9Sstevel@tonic-gate  * from the server.
4037c478bd9Sstevel@tonic-gate  *
4047c478bd9Sstevel@tonic-gate  * This increments the lock owner's ref count if found.  Returns NULL if
4057c478bd9Sstevel@tonic-gate  * there was no match.
4067c478bd9Sstevel@tonic-gate  */
4077c478bd9Sstevel@tonic-gate nfs4_lock_owner_t *
find_lock_owner(rnode4_t * rp,pid_t pid,lown_which_t which)4087c478bd9Sstevel@tonic-gate find_lock_owner(rnode4_t *rp, pid_t pid, lown_which_t which)
4097c478bd9Sstevel@tonic-gate {
4107c478bd9Sstevel@tonic-gate 	nfs4_lock_owner_t	*lop, *next_lop;
4117c478bd9Sstevel@tonic-gate 
4127c478bd9Sstevel@tonic-gate 	NFS4_DEBUG(nfs4_client_state_debug, (CE_NOTE,
413bbf2a467SNagakiran Rajashekar 	    "find_lock_owner: pid %x, which %d", pid, which));
4147c478bd9Sstevel@tonic-gate 
4157c478bd9Sstevel@tonic-gate 	ASSERT(which == LOWN_ANY || which == LOWN_VALID_STATEID);
4167c478bd9Sstevel@tonic-gate 
4177c478bd9Sstevel@tonic-gate 	/* search by pid */
4187c478bd9Sstevel@tonic-gate 	mutex_enter(&rp->r_statev4_lock);
4197c478bd9Sstevel@tonic-gate 
4207c478bd9Sstevel@tonic-gate 	lop = rp->r_lo_head.lo_next_rnode;
4217c478bd9Sstevel@tonic-gate 	while (lop != &rp->r_lo_head) {
4227c478bd9Sstevel@tonic-gate 		mutex_enter(&lop->lo_lock);
4237c478bd9Sstevel@tonic-gate 		if (lop->lo_pid == pid && lop->lo_valid != 0 &&
4247c478bd9Sstevel@tonic-gate 		    !(lop->lo_flags & NFS4_BAD_SEQID_LOCK)) {
4257c478bd9Sstevel@tonic-gate 			if (which == LOWN_ANY ||
4267c478bd9Sstevel@tonic-gate 			    lop->lo_just_created != NFS4_JUST_CREATED) {
4277c478bd9Sstevel@tonic-gate 				/* Found a matching lock owner */
4287c478bd9Sstevel@tonic-gate 				NFS4_DEBUG(nfs4_client_state_debug,
429bbf2a467SNagakiran Rajashekar 				    (CE_NOTE, "find_lock_owner: "
430bbf2a467SNagakiran Rajashekar 				    "got a match"));
4317c478bd9Sstevel@tonic-gate 
4327c478bd9Sstevel@tonic-gate 				lop->lo_ref_count++;
4337c478bd9Sstevel@tonic-gate 				mutex_exit(&lop->lo_lock);
4347c478bd9Sstevel@tonic-gate 				mutex_exit(&rp->r_statev4_lock);
4357c478bd9Sstevel@tonic-gate 				return (lop);
4367c478bd9Sstevel@tonic-gate 			}
4377c478bd9Sstevel@tonic-gate 		}
4387c478bd9Sstevel@tonic-gate 		next_lop = lop->lo_next_rnode;
4397c478bd9Sstevel@tonic-gate 		mutex_exit(&lop->lo_lock);
4407c478bd9Sstevel@tonic-gate 		lop = next_lop;
4417c478bd9Sstevel@tonic-gate 	}
4427c478bd9Sstevel@tonic-gate 
4437c478bd9Sstevel@tonic-gate 	mutex_exit(&rp->r_statev4_lock);
4447c478bd9Sstevel@tonic-gate 	return (NULL);
4457c478bd9Sstevel@tonic-gate }
4467c478bd9Sstevel@tonic-gate 
4477c478bd9Sstevel@tonic-gate /*
4487c478bd9Sstevel@tonic-gate  * This returns the delegation stateid as 'sid'. Returns 1 if a successful
4497c478bd9Sstevel@tonic-gate  * delegation stateid was found, otherwise returns 0.
4507c478bd9Sstevel@tonic-gate  */
4517c478bd9Sstevel@tonic-gate 
4527c478bd9Sstevel@tonic-gate static int
nfs4_get_deleg_stateid(rnode4_t * rp,nfs_opnum4 op,stateid4 * sid)4537c478bd9Sstevel@tonic-gate nfs4_get_deleg_stateid(rnode4_t *rp, nfs_opnum4 op, stateid4 *sid)
4547c478bd9Sstevel@tonic-gate {
4557c478bd9Sstevel@tonic-gate 	ASSERT(!mutex_owned(&rp->r_statev4_lock));
4567c478bd9Sstevel@tonic-gate 
4577c478bd9Sstevel@tonic-gate 	mutex_enter(&rp->r_statev4_lock);
4587c478bd9Sstevel@tonic-gate 	if (((rp->r_deleg_type == OPEN_DELEGATE_WRITE && op == OP_WRITE) ||
4597c478bd9Sstevel@tonic-gate 	    (rp->r_deleg_type != OPEN_DELEGATE_NONE && op != OP_WRITE)) &&
4607c478bd9Sstevel@tonic-gate 	    !rp->r_deleg_return_pending) {
4617c478bd9Sstevel@tonic-gate 
4627c478bd9Sstevel@tonic-gate 		*sid = rp->r_deleg_stateid;
4637c478bd9Sstevel@tonic-gate 		mutex_exit(&rp->r_statev4_lock);
4647c478bd9Sstevel@tonic-gate 		return (1);
4657c478bd9Sstevel@tonic-gate 	}
4667c478bd9Sstevel@tonic-gate 	mutex_exit(&rp->r_statev4_lock);
4677c478bd9Sstevel@tonic-gate 	return (0);
4687c478bd9Sstevel@tonic-gate }
4697c478bd9Sstevel@tonic-gate 
4707c478bd9Sstevel@tonic-gate /*
4717c478bd9Sstevel@tonic-gate  * This returns the lock stateid as 'sid'. Returns 1 if a successful lock
4727c478bd9Sstevel@tonic-gate  * stateid was found, otherwise returns 0.
4737c478bd9Sstevel@tonic-gate  */
4747c478bd9Sstevel@tonic-gate static int
nfs4_get_lock_stateid(rnode4_t * rp,pid_t pid,stateid4 * sid)4757c478bd9Sstevel@tonic-gate nfs4_get_lock_stateid(rnode4_t *rp, pid_t pid, stateid4 *sid)
4767c478bd9Sstevel@tonic-gate {
4777c478bd9Sstevel@tonic-gate 	nfs4_lock_owner_t *lop;
4787c478bd9Sstevel@tonic-gate 
4797c478bd9Sstevel@tonic-gate 	lop = find_lock_owner(rp, pid, LOWN_VALID_STATEID);
4807c478bd9Sstevel@tonic-gate 
4817c478bd9Sstevel@tonic-gate 	if (lop) {
4827c478bd9Sstevel@tonic-gate 		/*
4837c478bd9Sstevel@tonic-gate 		 * Found a matching lock owner, so use a lock
4847c478bd9Sstevel@tonic-gate 		 * stateid rather than an open stateid.
4857c478bd9Sstevel@tonic-gate 		 */
4867c478bd9Sstevel@tonic-gate 		mutex_enter(&lop->lo_lock);
4877c478bd9Sstevel@tonic-gate 		*sid = lop->lock_stateid;
4887c478bd9Sstevel@tonic-gate 		mutex_exit(&lop->lo_lock);
4897c478bd9Sstevel@tonic-gate 		lock_owner_rele(lop);
4907c478bd9Sstevel@tonic-gate 		return (1);
4917c478bd9Sstevel@tonic-gate 	}
4927c478bd9Sstevel@tonic-gate 
4937c478bd9Sstevel@tonic-gate 	NFS4_DEBUG(nfs4_client_state_debug, (CE_NOTE,
4947c478bd9Sstevel@tonic-gate 	    "nfs4_get_lock_stateid: no lop"));
4957c478bd9Sstevel@tonic-gate 	return (0);
4967c478bd9Sstevel@tonic-gate }
4977c478bd9Sstevel@tonic-gate 
4987c478bd9Sstevel@tonic-gate /*
4997c478bd9Sstevel@tonic-gate  * This returns the open stateid as 'sid'. Returns 1 if a successful open
5007c478bd9Sstevel@tonic-gate  * stateid was found, otherwise returns 0.
5017c478bd9Sstevel@tonic-gate  *
5027c478bd9Sstevel@tonic-gate  * Once the stateid is returned to the caller, it is no longer protected;
5037c478bd9Sstevel@tonic-gate  * so the caller must be prepared to handle OLD/BAD_STATEID where
5047c478bd9Sstevel@tonic-gate  * appropiate.
5057c478bd9Sstevel@tonic-gate  */
5067c478bd9Sstevel@tonic-gate static int
nfs4_get_open_stateid(rnode4_t * rp,cred_t * cr,mntinfo4_t * mi,stateid4 * sid)5077c478bd9Sstevel@tonic-gate nfs4_get_open_stateid(rnode4_t *rp, cred_t *cr, mntinfo4_t *mi, stateid4 *sid)
5087c478bd9Sstevel@tonic-gate {
5097c478bd9Sstevel@tonic-gate 	nfs4_open_owner_t *oop;
5107c478bd9Sstevel@tonic-gate 	nfs4_open_stream_t *osp;
5117c478bd9Sstevel@tonic-gate 
5127c478bd9Sstevel@tonic-gate 	ASSERT(mi != NULL);
5137c478bd9Sstevel@tonic-gate 
5147c478bd9Sstevel@tonic-gate 	oop = find_open_owner(cr, NFS4_PERM_CREATED, mi);
5157c478bd9Sstevel@tonic-gate 	if (!oop) {
5167c478bd9Sstevel@tonic-gate 		NFS4_DEBUG(nfs4_client_state_debug, (CE_NOTE,
5177c478bd9Sstevel@tonic-gate 		    "nfs4_get_open_stateid: no oop"));
5187c478bd9Sstevel@tonic-gate 		return (0);
5197c478bd9Sstevel@tonic-gate 	}
5207c478bd9Sstevel@tonic-gate 
5217c478bd9Sstevel@tonic-gate 	osp = find_open_stream(oop, rp);
5227c478bd9Sstevel@tonic-gate 	open_owner_rele(oop);
5237c478bd9Sstevel@tonic-gate 	if (!osp) {
5247c478bd9Sstevel@tonic-gate 		NFS4_DEBUG(nfs4_client_state_debug, (CE_NOTE,
5257c478bd9Sstevel@tonic-gate 		    "nfs4_get_open_stateid: no osp"));
5267c478bd9Sstevel@tonic-gate 		return (0);
5277c478bd9Sstevel@tonic-gate 	}
5287c478bd9Sstevel@tonic-gate 
5297c478bd9Sstevel@tonic-gate 	if (osp->os_failed_reopen) {
5307c478bd9Sstevel@tonic-gate 		NFS4_DEBUG(nfs4_client_state_debug, (CE_NOTE,
5317c478bd9Sstevel@tonic-gate 		    "nfs4_get_open_stateid: osp %p failed reopen",
5327c478bd9Sstevel@tonic-gate 		    (void *)osp));
5337c478bd9Sstevel@tonic-gate 		mutex_exit(&osp->os_sync_lock);
5347c478bd9Sstevel@tonic-gate 		open_stream_rele(osp, rp);
5357c478bd9Sstevel@tonic-gate 		return (0);
5367c478bd9Sstevel@tonic-gate 	}
5377c478bd9Sstevel@tonic-gate 	*sid = osp->open_stateid;
5387c478bd9Sstevel@tonic-gate 	mutex_exit(&osp->os_sync_lock);
5397c478bd9Sstevel@tonic-gate 	open_stream_rele(osp, rp);
5407c478bd9Sstevel@tonic-gate 	return (1);
5417c478bd9Sstevel@tonic-gate }
5427c478bd9Sstevel@tonic-gate 
5437c478bd9Sstevel@tonic-gate /*
5447c478bd9Sstevel@tonic-gate  * Returns the delegation stateid if this 'op' is OP_WRITE and the
5457c478bd9Sstevel@tonic-gate  * delegation we hold is a write delegation, OR this 'op' is not
5467c478bd9Sstevel@tonic-gate  * OP_WRITE and we have a delegation held (read or write), otherwise
5477c478bd9Sstevel@tonic-gate  * returns the lock stateid if there is a lock owner, otherwise
5487c478bd9Sstevel@tonic-gate  * returns the open stateid if there is a open stream, otherwise
5497c478bd9Sstevel@tonic-gate  * returns special stateid <seqid = 0, other = 0>.
5507c478bd9Sstevel@tonic-gate  *
5517c478bd9Sstevel@tonic-gate  * Used for WRITE operations.
5527c478bd9Sstevel@tonic-gate  */
5537c478bd9Sstevel@tonic-gate stateid4
nfs4_get_w_stateid(cred_t * cr,rnode4_t * rp,pid_t pid,mntinfo4_t * mi,nfs_opnum4 op,nfs4_stateid_types_t * sid_tp)5547c478bd9Sstevel@tonic-gate nfs4_get_w_stateid(cred_t *cr, rnode4_t *rp, pid_t pid, mntinfo4_t *mi,
555957f6e72SMarcel Telka     nfs_opnum4 op, nfs4_stateid_types_t *sid_tp)
5567c478bd9Sstevel@tonic-gate {
5577c478bd9Sstevel@tonic-gate 	stateid4 sid;
5587c478bd9Sstevel@tonic-gate 
5597c478bd9Sstevel@tonic-gate 	if (nfs4_get_deleg_stateid(rp, op, &sid)) {
5607c478bd9Sstevel@tonic-gate 		if (!stateid4_cmp(&sid, &sid_tp->d_sid)) {
5617c478bd9Sstevel@tonic-gate 			sid_tp->cur_sid_type = DEL_SID;
5627c478bd9Sstevel@tonic-gate 			return (sid);
5637c478bd9Sstevel@tonic-gate 		}
5647c478bd9Sstevel@tonic-gate 	}
5657c478bd9Sstevel@tonic-gate 	if (nfs4_get_lock_stateid(rp, pid, &sid)) {
5667c478bd9Sstevel@tonic-gate 		if (!stateid4_cmp(&sid, &sid_tp->l_sid)) {
5677c478bd9Sstevel@tonic-gate 			sid_tp->cur_sid_type = LOCK_SID;
5687c478bd9Sstevel@tonic-gate 			return (sid);
5697c478bd9Sstevel@tonic-gate 		}
5707c478bd9Sstevel@tonic-gate 	}
5717c478bd9Sstevel@tonic-gate 	if (nfs4_get_open_stateid(rp, cr, mi, &sid)) {
5727c478bd9Sstevel@tonic-gate 		if (!stateid4_cmp(&sid, &sid_tp->o_sid)) {
5737c478bd9Sstevel@tonic-gate 			sid_tp->cur_sid_type = OPEN_SID;
5747c478bd9Sstevel@tonic-gate 			return (sid);
5757c478bd9Sstevel@tonic-gate 		}
5767c478bd9Sstevel@tonic-gate 	}
5777c478bd9Sstevel@tonic-gate 	bzero(&sid, sizeof (stateid4));
5787c478bd9Sstevel@tonic-gate 	sid_tp->cur_sid_type = SPEC_SID;
5797c478bd9Sstevel@tonic-gate 	return (sid);
5807c478bd9Sstevel@tonic-gate }
5817c478bd9Sstevel@tonic-gate 
5827c478bd9Sstevel@tonic-gate /*
5837c478bd9Sstevel@tonic-gate  * Returns the delegation stateid if this 'op' is OP_WRITE and the
5847c478bd9Sstevel@tonic-gate  * delegation we hold is a write delegation, OR this 'op' is not
5857c478bd9Sstevel@tonic-gate  * OP_WRITE and we have a delegation held (read or write), otherwise
5867c478bd9Sstevel@tonic-gate  * returns the lock stateid if there is a lock owner, otherwise
5877c478bd9Sstevel@tonic-gate  * returns the open stateid if there is a open stream, otherwise
5887c478bd9Sstevel@tonic-gate  * returns special stateid <seqid = 0, other = 0>.
5897c478bd9Sstevel@tonic-gate  *
5907c478bd9Sstevel@tonic-gate  * This also updates which stateid we are using in 'sid_tp', skips
5917c478bd9Sstevel@tonic-gate  * previously attempted stateids, and skips checking higher priority
5927c478bd9Sstevel@tonic-gate  * stateids than the current level as dictated by 'sid_tp->cur_sid_type'
5937c478bd9Sstevel@tonic-gate  * for async reads.
5947c478bd9Sstevel@tonic-gate  *
5957c478bd9Sstevel@tonic-gate  * Used for READ and SETATTR operations.
5967c478bd9Sstevel@tonic-gate  */
5977c478bd9Sstevel@tonic-gate stateid4
nfs4_get_stateid(cred_t * cr,rnode4_t * rp,pid_t pid,mntinfo4_t * mi,nfs_opnum4 op,nfs4_stateid_types_t * sid_tp,bool_t async_read)5987c478bd9Sstevel@tonic-gate nfs4_get_stateid(cred_t *cr, rnode4_t *rp, pid_t pid, mntinfo4_t *mi,
599957f6e72SMarcel Telka     nfs_opnum4 op, nfs4_stateid_types_t *sid_tp, bool_t async_read)
6007c478bd9Sstevel@tonic-gate {
6017c478bd9Sstevel@tonic-gate 	stateid4 sid;
6027c478bd9Sstevel@tonic-gate 
6037c478bd9Sstevel@tonic-gate 	/*
6047c478bd9Sstevel@tonic-gate 	 * For asynchronous READs, do not attempt to retry from the start of
6057c478bd9Sstevel@tonic-gate 	 * the stateid priority list, just continue from where you last left
6067c478bd9Sstevel@tonic-gate 	 * off.
6077c478bd9Sstevel@tonic-gate 	 */
6087c478bd9Sstevel@tonic-gate 	if (async_read) {
6097c478bd9Sstevel@tonic-gate 		switch (sid_tp->cur_sid_type) {
6107c478bd9Sstevel@tonic-gate 		case NO_SID:
6117c478bd9Sstevel@tonic-gate 			break;
6127c478bd9Sstevel@tonic-gate 		case DEL_SID:
6137c478bd9Sstevel@tonic-gate 			goto lock_stateid;
6147c478bd9Sstevel@tonic-gate 		case LOCK_SID:
6157c478bd9Sstevel@tonic-gate 			goto open_stateid;
6167c478bd9Sstevel@tonic-gate 		case OPEN_SID:
6177c478bd9Sstevel@tonic-gate 			goto special_stateid;
6187c478bd9Sstevel@tonic-gate 		case SPEC_SID:
6197c478bd9Sstevel@tonic-gate 		default:
6207c478bd9Sstevel@tonic-gate 			cmn_err(CE_PANIC, "nfs4_get_stateid: illegal current "
6217c478bd9Sstevel@tonic-gate 			    "stateid type %d", sid_tp->cur_sid_type);
6227c478bd9Sstevel@tonic-gate 		}
6237c478bd9Sstevel@tonic-gate 	}
6247c478bd9Sstevel@tonic-gate 
6257c478bd9Sstevel@tonic-gate 	if (nfs4_get_deleg_stateid(rp, op, &sid)) {
6267c478bd9Sstevel@tonic-gate 		if (!stateid4_cmp(&sid, &sid_tp->d_sid)) {
6277c478bd9Sstevel@tonic-gate 			sid_tp->cur_sid_type = DEL_SID;
6287c478bd9Sstevel@tonic-gate 			return (sid);
6297c478bd9Sstevel@tonic-gate 		}
6307c478bd9Sstevel@tonic-gate 	}
6317c478bd9Sstevel@tonic-gate lock_stateid:
6327c478bd9Sstevel@tonic-gate 	if (nfs4_get_lock_stateid(rp, pid, &sid)) {
6337c478bd9Sstevel@tonic-gate 		if (!stateid4_cmp(&sid, &sid_tp->l_sid)) {
6347c478bd9Sstevel@tonic-gate 			sid_tp->cur_sid_type = LOCK_SID;
6357c478bd9Sstevel@tonic-gate 			return (sid);
6367c478bd9Sstevel@tonic-gate 		}
6377c478bd9Sstevel@tonic-gate 	}
6387c478bd9Sstevel@tonic-gate open_stateid:
6397c478bd9Sstevel@tonic-gate 	if (nfs4_get_open_stateid(rp, cr, mi, &sid)) {
6407c478bd9Sstevel@tonic-gate 		if (!stateid4_cmp(&sid, &sid_tp->o_sid)) {
6417c478bd9Sstevel@tonic-gate 			sid_tp->cur_sid_type = OPEN_SID;
6427c478bd9Sstevel@tonic-gate 			return (sid);
6437c478bd9Sstevel@tonic-gate 		}
6447c478bd9Sstevel@tonic-gate 	}
6457c478bd9Sstevel@tonic-gate special_stateid:
6467c478bd9Sstevel@tonic-gate 	bzero(&sid, sizeof (stateid4));
6477c478bd9Sstevel@tonic-gate 	sid_tp->cur_sid_type = SPEC_SID;
6487c478bd9Sstevel@tonic-gate 	return	(sid);
6497c478bd9Sstevel@tonic-gate }
6507c478bd9Sstevel@tonic-gate 
6517c478bd9Sstevel@tonic-gate void
nfs4_set_lock_stateid(nfs4_lock_owner_t * lop,stateid4 stateid)6527c478bd9Sstevel@tonic-gate nfs4_set_lock_stateid(nfs4_lock_owner_t *lop, stateid4 stateid)
6537c478bd9Sstevel@tonic-gate {
6547c478bd9Sstevel@tonic-gate 	NFS4_DEBUG(nfs4_client_state_debug, (CE_NOTE,
655bbf2a467SNagakiran Rajashekar 	    "nfs4_set_lock_stateid"));
6567c478bd9Sstevel@tonic-gate 
6577c478bd9Sstevel@tonic-gate 	ASSERT(lop);
6587c478bd9Sstevel@tonic-gate 	ASSERT(lop->lo_flags & NFS4_LOCK_SEQID_INUSE);
6597c478bd9Sstevel@tonic-gate 
6607c478bd9Sstevel@tonic-gate 	mutex_enter(&lop->lo_lock);
6617c478bd9Sstevel@tonic-gate 	lop->lock_stateid = stateid;
6627c478bd9Sstevel@tonic-gate 	mutex_exit(&lop->lo_lock);
6637c478bd9Sstevel@tonic-gate }
6647c478bd9Sstevel@tonic-gate 
6657c478bd9Sstevel@tonic-gate /*
6667c478bd9Sstevel@tonic-gate  * Sequence number used when a new open owner is needed.
6677c478bd9Sstevel@tonic-gate  * This is used so as to not confuse the server.  Since a open owner
6687c478bd9Sstevel@tonic-gate  * is based off of cred, a cred could be re-used quickly, and the server
6697c478bd9Sstevel@tonic-gate  * may not release all state for a cred.
6707c478bd9Sstevel@tonic-gate  */
6717c478bd9Sstevel@tonic-gate static uint64_t open_owner_seq_num = 0;
6727c478bd9Sstevel@tonic-gate 
6737c478bd9Sstevel@tonic-gate uint64_t
nfs4_get_new_oo_name(void)6747c478bd9Sstevel@tonic-gate nfs4_get_new_oo_name(void)
6757c478bd9Sstevel@tonic-gate {
6761a5e258fSJosef 'Jeff' Sipek 	return (atomic_inc_64_nv(&open_owner_seq_num));
6777c478bd9Sstevel@tonic-gate }
6787c478bd9Sstevel@tonic-gate 
6797c478bd9Sstevel@tonic-gate /*
6807c478bd9Sstevel@tonic-gate  * Create a new open owner and add it to the open owner hash table.
6817c478bd9Sstevel@tonic-gate  */
6827c478bd9Sstevel@tonic-gate nfs4_open_owner_t *
create_open_owner(cred_t * cr,mntinfo4_t * mi)6837c478bd9Sstevel@tonic-gate create_open_owner(cred_t *cr, mntinfo4_t *mi)
6847c478bd9Sstevel@tonic-gate {
6857c478bd9Sstevel@tonic-gate 	nfs4_open_owner_t	*oop;
6867c478bd9Sstevel@tonic-gate 	nfs4_oo_hash_bucket_t	*bucketp;
6877c478bd9Sstevel@tonic-gate 
6887c478bd9Sstevel@tonic-gate 	oop = kmem_alloc(sizeof (nfs4_open_owner_t), KM_SLEEP);
6897c478bd9Sstevel@tonic-gate 	/*
6907c478bd9Sstevel@tonic-gate 	 * Make sure the cred doesn't go away when we put this open owner
6917c478bd9Sstevel@tonic-gate 	 * on the free list, as well as make crcmp() a valid check.
6927c478bd9Sstevel@tonic-gate 	 */
6937c478bd9Sstevel@tonic-gate 	crhold(cr);
6947c478bd9Sstevel@tonic-gate 	oop->oo_cred = cr;
6957c478bd9Sstevel@tonic-gate 	mutex_init(&oop->oo_lock, NULL, MUTEX_DEFAULT, NULL);
6967c478bd9Sstevel@tonic-gate 	oop->oo_ref_count = 1;
6977c478bd9Sstevel@tonic-gate 	oop->oo_valid = 1;
6987c478bd9Sstevel@tonic-gate 	oop->oo_just_created = NFS4_JUST_CREATED;
6997c478bd9Sstevel@tonic-gate 	oop->oo_seqid = 0;
7007c478bd9Sstevel@tonic-gate 	oop->oo_seqid_inuse = 0;
7017c478bd9Sstevel@tonic-gate 	oop->oo_last_good_seqid = 0;
7027c478bd9Sstevel@tonic-gate 	oop->oo_last_good_op = TAG_NONE;
7037c478bd9Sstevel@tonic-gate 	oop->oo_cred_otw = NULL;
7047c478bd9Sstevel@tonic-gate 	cv_init(&oop->oo_cv_seqid_sync, NULL, CV_DEFAULT, NULL);
7057c478bd9Sstevel@tonic-gate 
7067c478bd9Sstevel@tonic-gate 	/*
7077c478bd9Sstevel@tonic-gate 	 * A Solaris open_owner is <oo_seq_num>
7087c478bd9Sstevel@tonic-gate 	 */
7097c478bd9Sstevel@tonic-gate 	oop->oo_name = nfs4_get_new_oo_name();
7107c478bd9Sstevel@tonic-gate 
7117c478bd9Sstevel@tonic-gate 	/* now add the struct into the cred hash table */
7127c478bd9Sstevel@tonic-gate 	ASSERT(mutex_owned(&mi->mi_lock));
7137c478bd9Sstevel@tonic-gate 	bucketp = lock_bucket(cr, mi);
7147c478bd9Sstevel@tonic-gate 	list_insert_head(&bucketp->b_oo_hash_list, oop);
7157c478bd9Sstevel@tonic-gate 	unlock_bucket(bucketp);
7167c478bd9Sstevel@tonic-gate 
7177c478bd9Sstevel@tonic-gate 	return (oop);
7187c478bd9Sstevel@tonic-gate }
7197c478bd9Sstevel@tonic-gate 
7207c478bd9Sstevel@tonic-gate /*
7217c478bd9Sstevel@tonic-gate  * Create a new open stream and it to the rnode's list.
7227c478bd9Sstevel@tonic-gate  * Increments the ref count on oop.
7237c478bd9Sstevel@tonic-gate  * Returns with 'os_sync_lock' held.
7247c478bd9Sstevel@tonic-gate  */
7257c478bd9Sstevel@tonic-gate nfs4_open_stream_t *
create_open_stream(nfs4_open_owner_t * oop,rnode4_t * rp)7267c478bd9Sstevel@tonic-gate create_open_stream(nfs4_open_owner_t *oop, rnode4_t *rp)
7277c478bd9Sstevel@tonic-gate {
7287c478bd9Sstevel@tonic-gate 	nfs4_open_stream_t	*osp;
7297c478bd9Sstevel@tonic-gate 
7307c478bd9Sstevel@tonic-gate #ifdef DEBUG
7317c478bd9Sstevel@tonic-gate 	mutex_enter(&oop->oo_lock);
7327c478bd9Sstevel@tonic-gate 	ASSERT(oop->oo_seqid_inuse);
7337c478bd9Sstevel@tonic-gate 	mutex_exit(&oop->oo_lock);
7347c478bd9Sstevel@tonic-gate #endif
7357c478bd9Sstevel@tonic-gate 
7367c478bd9Sstevel@tonic-gate 	osp = kmem_alloc(sizeof (nfs4_open_stream_t), KM_SLEEP);
7377c478bd9Sstevel@tonic-gate 	osp->os_open_ref_count = 1;
7387c478bd9Sstevel@tonic-gate 	osp->os_mapcnt = 0;
7397c478bd9Sstevel@tonic-gate 	osp->os_ref_count = 2;
7407c478bd9Sstevel@tonic-gate 	osp->os_valid = 1;
7417c478bd9Sstevel@tonic-gate 	osp->os_open_owner = oop;
7427c478bd9Sstevel@tonic-gate 	osp->os_orig_oo_name = oop->oo_name;
7437c478bd9Sstevel@tonic-gate 	bzero(&osp->open_stateid, sizeof (stateid4));
7447c478bd9Sstevel@tonic-gate 	osp->os_share_acc_read = 0;
7457c478bd9Sstevel@tonic-gate 	osp->os_share_acc_write = 0;
7467c478bd9Sstevel@tonic-gate 	osp->os_mmap_read = 0;
7477c478bd9Sstevel@tonic-gate 	osp->os_mmap_write = 0;
7487c478bd9Sstevel@tonic-gate 	osp->os_share_deny_none = 0;
7497c478bd9Sstevel@tonic-gate 	osp->os_share_deny_read = 0;
7507c478bd9Sstevel@tonic-gate 	osp->os_share_deny_write = 0;
7517c478bd9Sstevel@tonic-gate 	osp->os_delegation = 0;
7527c478bd9Sstevel@tonic-gate 	osp->os_dc_openacc = 0;
7537c478bd9Sstevel@tonic-gate 	osp->os_final_close = 0;
7547c478bd9Sstevel@tonic-gate 	osp->os_pending_close = 0;
7557c478bd9Sstevel@tonic-gate 	osp->os_failed_reopen = 0;
7567c478bd9Sstevel@tonic-gate 	osp->os_force_close = 0;
7577c478bd9Sstevel@tonic-gate 	mutex_init(&osp->os_sync_lock, NULL, MUTEX_DEFAULT, NULL);
7587c478bd9Sstevel@tonic-gate 
7597c478bd9Sstevel@tonic-gate 	/* open owner gets a reference */
7607c478bd9Sstevel@tonic-gate 	open_owner_hold(oop);
7617c478bd9Sstevel@tonic-gate 
7627c478bd9Sstevel@tonic-gate 	/* now add the open stream to rp */
7637c478bd9Sstevel@tonic-gate 	mutex_enter(&rp->r_os_lock);
7647c478bd9Sstevel@tonic-gate 	mutex_enter(&osp->os_sync_lock);
7657c478bd9Sstevel@tonic-gate 	list_insert_head(&rp->r_open_streams, osp);
7667c478bd9Sstevel@tonic-gate 	mutex_exit(&rp->r_os_lock);
7677c478bd9Sstevel@tonic-gate 
7687c478bd9Sstevel@tonic-gate 	return (osp);
7697c478bd9Sstevel@tonic-gate }
7707c478bd9Sstevel@tonic-gate 
7717c478bd9Sstevel@tonic-gate /*
7727c478bd9Sstevel@tonic-gate  * Returns an open stream with 'os_sync_lock' held.
7737c478bd9Sstevel@tonic-gate  * If the open stream is found (rather than created), its
7747c478bd9Sstevel@tonic-gate  * 'os_open_ref_count' is bumped.
7757c478bd9Sstevel@tonic-gate  *
7767c478bd9Sstevel@tonic-gate  * There is no race with two threads entering this function
7777c478bd9Sstevel@tonic-gate  * and creating two open streams for the same <oop, rp> pair.
7787c478bd9Sstevel@tonic-gate  * This is because the open seqid sync must be acquired, thus
7797c478bd9Sstevel@tonic-gate  * only allowing one thread in at a time.
7807c478bd9Sstevel@tonic-gate  */
7817c478bd9Sstevel@tonic-gate nfs4_open_stream_t *
find_or_create_open_stream(nfs4_open_owner_t * oop,rnode4_t * rp,int * created_osp)7827c478bd9Sstevel@tonic-gate find_or_create_open_stream(nfs4_open_owner_t *oop, rnode4_t *rp,
783957f6e72SMarcel Telka     int *created_osp)
7847c478bd9Sstevel@tonic-gate {
7857c478bd9Sstevel@tonic-gate 	nfs4_open_stream_t *osp;
7867c478bd9Sstevel@tonic-gate 
7877c478bd9Sstevel@tonic-gate #ifdef DEBUG
7887c478bd9Sstevel@tonic-gate 	mutex_enter(&oop->oo_lock);
7897c478bd9Sstevel@tonic-gate 	ASSERT(oop->oo_seqid_inuse);
7907c478bd9Sstevel@tonic-gate 	mutex_exit(&oop->oo_lock);
7917c478bd9Sstevel@tonic-gate #endif
7927c478bd9Sstevel@tonic-gate 
7937c478bd9Sstevel@tonic-gate 	osp = find_open_stream(oop, rp);
7947c478bd9Sstevel@tonic-gate 	if (!osp) {
7957c478bd9Sstevel@tonic-gate 		osp = create_open_stream(oop, rp);
7967c478bd9Sstevel@tonic-gate 		if (osp)
7977c478bd9Sstevel@tonic-gate 			*created_osp = 1;
7987c478bd9Sstevel@tonic-gate 	} else {
7997c478bd9Sstevel@tonic-gate 		*created_osp = 0;
8007c478bd9Sstevel@tonic-gate 		osp->os_open_ref_count++;
8017c478bd9Sstevel@tonic-gate 	}
8027c478bd9Sstevel@tonic-gate 
8037c478bd9Sstevel@tonic-gate 	return (osp);
8047c478bd9Sstevel@tonic-gate }
8057c478bd9Sstevel@tonic-gate 
8067c478bd9Sstevel@tonic-gate static uint64_t lock_owner_seq_num = 0;
8077c478bd9Sstevel@tonic-gate 
8087c478bd9Sstevel@tonic-gate /*
8097c478bd9Sstevel@tonic-gate  * Create a new lock owner and add it to the rnode's list.
8107c478bd9Sstevel@tonic-gate  * Assumes the rnode's r_statev4_lock is held.
8117c478bd9Sstevel@tonic-gate  * The created lock owner has a reference count of 2: one for the list and
8127c478bd9Sstevel@tonic-gate  * one for the caller to use.  Returns the lock owner locked down.
8137c478bd9Sstevel@tonic-gate  */
8147c478bd9Sstevel@tonic-gate nfs4_lock_owner_t *
create_lock_owner(rnode4_t * rp,pid_t pid)8157c478bd9Sstevel@tonic-gate create_lock_owner(rnode4_t *rp, pid_t pid)
8167c478bd9Sstevel@tonic-gate {
8177c478bd9Sstevel@tonic-gate 	nfs4_lock_owner_t	*lop;
8187c478bd9Sstevel@tonic-gate 
8197c478bd9Sstevel@tonic-gate 	NFS4_DEBUG(nfs4_client_state_debug, (CE_NOTE,
820bbf2a467SNagakiran Rajashekar 	    "create_lock_owner: pid %x", pid));
8217c478bd9Sstevel@tonic-gate 
8227c478bd9Sstevel@tonic-gate 	ASSERT(mutex_owned(&rp->r_statev4_lock));
8237c478bd9Sstevel@tonic-gate 
8247c478bd9Sstevel@tonic-gate 	lop = kmem_alloc(sizeof (nfs4_lock_owner_t), KM_SLEEP);
8257c478bd9Sstevel@tonic-gate 	lop->lo_ref_count = 2;
8267c478bd9Sstevel@tonic-gate 	lop->lo_valid = 1;
8277c478bd9Sstevel@tonic-gate 	bzero(&lop->lock_stateid, sizeof (stateid4));
8287c478bd9Sstevel@tonic-gate 	lop->lo_pid = pid;
8297c478bd9Sstevel@tonic-gate 	lop->lock_seqid = 0;
8307c478bd9Sstevel@tonic-gate 	lop->lo_pending_rqsts = 0;
8317c478bd9Sstevel@tonic-gate 	lop->lo_just_created = NFS4_JUST_CREATED;
8327c478bd9Sstevel@tonic-gate 	lop->lo_flags = 0;
8337c478bd9Sstevel@tonic-gate 	lop->lo_seqid_holder = NULL;
8347c478bd9Sstevel@tonic-gate 
8357c478bd9Sstevel@tonic-gate 	/*
8367c478bd9Sstevel@tonic-gate 	 * A Solaris lock_owner is <seq_num><pid>
8377c478bd9Sstevel@tonic-gate 	 */
8387c478bd9Sstevel@tonic-gate 	lop->lock_owner_name.ln_seq_num =
8391a5e258fSJosef 'Jeff' Sipek 	    atomic_inc_64_nv(&lock_owner_seq_num);
8407c478bd9Sstevel@tonic-gate 	lop->lock_owner_name.ln_pid = pid;
8417c478bd9Sstevel@tonic-gate 
8427c478bd9Sstevel@tonic-gate 	cv_init(&lop->lo_cv_seqid_sync, NULL, CV_DEFAULT, NULL);
8437c478bd9Sstevel@tonic-gate 	mutex_init(&lop->lo_lock, NULL, MUTEX_DEFAULT, NULL);
8447c478bd9Sstevel@tonic-gate 
8457c478bd9Sstevel@tonic-gate 	mutex_enter(&lop->lo_lock);
8467c478bd9Sstevel@tonic-gate 
8477c478bd9Sstevel@tonic-gate 	/* now add the lock owner to rp */
8487c478bd9Sstevel@tonic-gate 	lop->lo_prev_rnode = &rp->r_lo_head;
8497c478bd9Sstevel@tonic-gate 	lop->lo_next_rnode = rp->r_lo_head.lo_next_rnode;
8507c478bd9Sstevel@tonic-gate 	rp->r_lo_head.lo_next_rnode->lo_prev_rnode = lop;
8517c478bd9Sstevel@tonic-gate 	rp->r_lo_head.lo_next_rnode = lop;
8527c478bd9Sstevel@tonic-gate 
8537c478bd9Sstevel@tonic-gate 	return (lop);
8547c478bd9Sstevel@tonic-gate 
8557c478bd9Sstevel@tonic-gate }
8567c478bd9Sstevel@tonic-gate 
8577c478bd9Sstevel@tonic-gate /*
8587c478bd9Sstevel@tonic-gate  * This sets the lock seqid of a lock owner.
8597c478bd9Sstevel@tonic-gate  */
8607c478bd9Sstevel@tonic-gate void
nfs4_set_lock_seqid(seqid4 seqid,nfs4_lock_owner_t * lop)8617c478bd9Sstevel@tonic-gate nfs4_set_lock_seqid(seqid4 seqid, nfs4_lock_owner_t *lop)
8627c478bd9Sstevel@tonic-gate {
8637c478bd9Sstevel@tonic-gate 	NFS4_DEBUG(nfs4_client_state_debug, (CE_NOTE,
864bbf2a467SNagakiran Rajashekar 	    "nfs4_set_lock_seqid"));
8657c478bd9Sstevel@tonic-gate 
8667c478bd9Sstevel@tonic-gate 	ASSERT(lop != NULL);
8677c478bd9Sstevel@tonic-gate 	ASSERT(lop->lo_flags & NFS4_LOCK_SEQID_INUSE);
8687c478bd9Sstevel@tonic-gate 
8697c478bd9Sstevel@tonic-gate 	lop->lock_seqid = seqid;
8707c478bd9Sstevel@tonic-gate }
8717c478bd9Sstevel@tonic-gate 
8727c478bd9Sstevel@tonic-gate static void
nfs4_set_new_lock_owner_args(lock_owner4 * owner,pid_t pid)8737c478bd9Sstevel@tonic-gate nfs4_set_new_lock_owner_args(lock_owner4 *owner, pid_t pid)
8747c478bd9Sstevel@tonic-gate {
8757c478bd9Sstevel@tonic-gate 	nfs4_lo_name_t *cast_namep;
8767c478bd9Sstevel@tonic-gate 
8777c478bd9Sstevel@tonic-gate 	NFS4_DEBUG(nfs4_client_state_debug, (CE_NOTE,
878bbf2a467SNagakiran Rajashekar 	    "nfs4_set_new_lock_owner_args"));
8797c478bd9Sstevel@tonic-gate 
8807c478bd9Sstevel@tonic-gate 	owner->owner_len = sizeof (*cast_namep);
8817c478bd9Sstevel@tonic-gate 	owner->owner_val = kmem_alloc(owner->owner_len, KM_SLEEP);
8827c478bd9Sstevel@tonic-gate 	/*
8837c478bd9Sstevel@tonic-gate 	 * A Solaris lock_owner is <seq_num><pid>
8847c478bd9Sstevel@tonic-gate 	 */
8857c478bd9Sstevel@tonic-gate 	cast_namep = (nfs4_lo_name_t *)owner->owner_val;
8861a5e258fSJosef 'Jeff' Sipek 	cast_namep->ln_seq_num = atomic_inc_64_nv(&lock_owner_seq_num);
8877c478bd9Sstevel@tonic-gate 	cast_namep->ln_pid = pid;
8887c478bd9Sstevel@tonic-gate }
8897c478bd9Sstevel@tonic-gate 
8907c478bd9Sstevel@tonic-gate /*
8917c478bd9Sstevel@tonic-gate  * Fill in the lock owner args.
8927c478bd9Sstevel@tonic-gate  */
8937c478bd9Sstevel@tonic-gate void
nfs4_setlockowner_args(lock_owner4 * owner,rnode4_t * rp,pid_t pid)8947c478bd9Sstevel@tonic-gate nfs4_setlockowner_args(lock_owner4 *owner, rnode4_t *rp, pid_t pid)
8957c478bd9Sstevel@tonic-gate {
8967c478bd9Sstevel@tonic-gate 	nfs4_lock_owner_t *lop;
8977c478bd9Sstevel@tonic-gate 
8987c478bd9Sstevel@tonic-gate 	NFS4_DEBUG(nfs4_client_state_debug, (CE_NOTE,
899bbf2a467SNagakiran Rajashekar 	    "nfs4_setlockowner_args"));
9007c478bd9Sstevel@tonic-gate 
9017c478bd9Sstevel@tonic-gate 	/* This increments lop's ref count */
9027c478bd9Sstevel@tonic-gate 	lop = find_lock_owner(rp, pid, LOWN_VALID_STATEID);
9037c478bd9Sstevel@tonic-gate 
9047c478bd9Sstevel@tonic-gate 	if (!lop)
9057c478bd9Sstevel@tonic-gate 		goto make_up_args;
9067c478bd9Sstevel@tonic-gate 
9077c478bd9Sstevel@tonic-gate 	mutex_enter(&lop->lo_lock);
9087c478bd9Sstevel@tonic-gate 	owner->owner_len = sizeof (lop->lock_owner_name);
9097c478bd9Sstevel@tonic-gate 	owner->owner_val = kmem_alloc(owner->owner_len, KM_SLEEP);
9107c478bd9Sstevel@tonic-gate 	bcopy(&lop->lock_owner_name, owner->owner_val,
911bbf2a467SNagakiran Rajashekar 	    owner->owner_len);
9127c478bd9Sstevel@tonic-gate 	mutex_exit(&lop->lo_lock);
9137c478bd9Sstevel@tonic-gate 	lock_owner_rele(lop);
9147c478bd9Sstevel@tonic-gate 	return;
9157c478bd9Sstevel@tonic-gate 
9167c478bd9Sstevel@tonic-gate make_up_args:
9177c478bd9Sstevel@tonic-gate 	nfs4_set_new_lock_owner_args(owner, pid);
9187c478bd9Sstevel@tonic-gate }
9197c478bd9Sstevel@tonic-gate 
9207c478bd9Sstevel@tonic-gate /*
9217c478bd9Sstevel@tonic-gate  * This ends our use of the open owner's open seqid by setting
9227c478bd9Sstevel@tonic-gate  * the appropiate flags and issuing a cv_signal to wake up another
9237c478bd9Sstevel@tonic-gate  * thread waiting to use the open seqid.
9247c478bd9Sstevel@tonic-gate  */
9257c478bd9Sstevel@tonic-gate 
9267c478bd9Sstevel@tonic-gate void
nfs4_end_open_seqid_sync(nfs4_open_owner_t * oop)9277c478bd9Sstevel@tonic-gate nfs4_end_open_seqid_sync(nfs4_open_owner_t *oop)
9287c478bd9Sstevel@tonic-gate {
9297c478bd9Sstevel@tonic-gate 	mutex_enter(&oop->oo_lock);
9307c478bd9Sstevel@tonic-gate 	ASSERT(oop->oo_seqid_inuse);
9317c478bd9Sstevel@tonic-gate 	oop->oo_seqid_inuse = 0;
932*f2211ffeSMarcel Telka 	cv_signal(&oop->oo_cv_seqid_sync);
9337c478bd9Sstevel@tonic-gate 	mutex_exit(&oop->oo_lock);
9347c478bd9Sstevel@tonic-gate }
9357c478bd9Sstevel@tonic-gate 
9367c478bd9Sstevel@tonic-gate /*
9377c478bd9Sstevel@tonic-gate  * This starts our use of the open owner's open seqid by setting
9387c478bd9Sstevel@tonic-gate  * the oo_seqid_inuse to true.  We will wait (forever) with a
9397c478bd9Sstevel@tonic-gate  * cv_wait() until we are woken up.
9407c478bd9Sstevel@tonic-gate  *
9417c478bd9Sstevel@tonic-gate  * Return values:
9427c478bd9Sstevel@tonic-gate  * 0		no problems
9437c478bd9Sstevel@tonic-gate  * EAGAIN	caller should retry (like a recovery retry)
9447c478bd9Sstevel@tonic-gate  */
9457c478bd9Sstevel@tonic-gate int
nfs4_start_open_seqid_sync(nfs4_open_owner_t * oop,mntinfo4_t * mi)9467c478bd9Sstevel@tonic-gate nfs4_start_open_seqid_sync(nfs4_open_owner_t *oop, mntinfo4_t *mi)
9477c478bd9Sstevel@tonic-gate {
9487c478bd9Sstevel@tonic-gate 	int error = 0;
9497c478bd9Sstevel@tonic-gate #ifdef DEBUG
9507c478bd9Sstevel@tonic-gate 	static int ops = 0;		/* fault injection */
9517c478bd9Sstevel@tonic-gate #endif
9527c478bd9Sstevel@tonic-gate 
9537c478bd9Sstevel@tonic-gate #ifdef DEBUG
9547c478bd9Sstevel@tonic-gate 	if (seqid_sync_faults && curthread != mi->mi_recovthread &&
9557c478bd9Sstevel@tonic-gate 	    ++ops % 5 == 0)
9567c478bd9Sstevel@tonic-gate 		return (EAGAIN);
9577c478bd9Sstevel@tonic-gate #endif
9587c478bd9Sstevel@tonic-gate 
9597c478bd9Sstevel@tonic-gate 	mutex_enter(&mi->mi_lock);
9607c478bd9Sstevel@tonic-gate 	if ((mi->mi_flags & MI4_RECOV_ACTIV) &&
9617c478bd9Sstevel@tonic-gate 	    curthread != mi->mi_recovthread)
9627c478bd9Sstevel@tonic-gate 		error = EAGAIN;
9637c478bd9Sstevel@tonic-gate 	mutex_exit(&mi->mi_lock);
9647c478bd9Sstevel@tonic-gate 	if (error != 0)
9657c478bd9Sstevel@tonic-gate 		goto done;
9667c478bd9Sstevel@tonic-gate 
9677c478bd9Sstevel@tonic-gate 	mutex_enter(&oop->oo_lock);
9687c478bd9Sstevel@tonic-gate 
9697c478bd9Sstevel@tonic-gate 	while (oop->oo_seqid_inuse) {
9707c478bd9Sstevel@tonic-gate 		NFS4_DEBUG(nfs4_seqid_sync, (CE_NOTE,
971bbf2a467SNagakiran Rajashekar 		    "nfs4_start_open_seqid_sync waiting on cv"));
9727c478bd9Sstevel@tonic-gate 
9737c478bd9Sstevel@tonic-gate 		cv_wait(&oop->oo_cv_seqid_sync, &oop->oo_lock);
9747c478bd9Sstevel@tonic-gate 	}
9757c478bd9Sstevel@tonic-gate 
9767c478bd9Sstevel@tonic-gate 	oop->oo_seqid_inuse = 1;
9777c478bd9Sstevel@tonic-gate 
9787c478bd9Sstevel@tonic-gate 	mutex_exit(&oop->oo_lock);
9797c478bd9Sstevel@tonic-gate 
9807c478bd9Sstevel@tonic-gate 	mutex_enter(&mi->mi_lock);
9817c478bd9Sstevel@tonic-gate 	if ((mi->mi_flags & MI4_RECOV_ACTIV) &&
9827c478bd9Sstevel@tonic-gate 	    curthread != mi->mi_recovthread)
9837c478bd9Sstevel@tonic-gate 		error = EAGAIN;
9847c478bd9Sstevel@tonic-gate 	mutex_exit(&mi->mi_lock);
9857c478bd9Sstevel@tonic-gate 
9867c478bd9Sstevel@tonic-gate 	if (error == EAGAIN)
9877c478bd9Sstevel@tonic-gate 		nfs4_end_open_seqid_sync(oop);
9887c478bd9Sstevel@tonic-gate 
9897c478bd9Sstevel@tonic-gate 	NFS4_DEBUG(nfs4_seqid_sync, (CE_NOTE,
990bbf2a467SNagakiran Rajashekar 	    "nfs4_start_open_seqid_sync: error=%d", error));
9917c478bd9Sstevel@tonic-gate 
9927c478bd9Sstevel@tonic-gate done:
9937c478bd9Sstevel@tonic-gate 	return (error);
9947c478bd9Sstevel@tonic-gate }
9957c478bd9Sstevel@tonic-gate 
9967c478bd9Sstevel@tonic-gate #ifdef	DEBUG
9977c478bd9Sstevel@tonic-gate int bypass_otw[2];
9987c478bd9Sstevel@tonic-gate #endif
9997c478bd9Sstevel@tonic-gate 
10007c478bd9Sstevel@tonic-gate /*
10017c478bd9Sstevel@tonic-gate  * Checks to see if the OPEN OTW is necessary that is, if it's already
10027c478bd9Sstevel@tonic-gate  * been opened with the same access and deny bits we are now asking for.
1003957f6e72SMarcel Telka  * Note, this assumes that *vp is a rnode.
10047c478bd9Sstevel@tonic-gate  */
10057c478bd9Sstevel@tonic-gate int
nfs4_is_otw_open_necessary(nfs4_open_owner_t * oop,int flag,vnode_t * vp,int just_been_created,int * errorp,int acc,nfs4_recov_state_t * rsp)10067c478bd9Sstevel@tonic-gate nfs4_is_otw_open_necessary(nfs4_open_owner_t *oop, int flag, vnode_t *vp,
1007957f6e72SMarcel Telka     int just_been_created, int *errorp, int acc, nfs4_recov_state_t *rsp)
10087c478bd9Sstevel@tonic-gate {
10097c478bd9Sstevel@tonic-gate 	rnode4_t *rp;
10107c478bd9Sstevel@tonic-gate 	nfs4_open_stream_t *osp;
10117c478bd9Sstevel@tonic-gate 	open_delegation_type4 dt;
10127c478bd9Sstevel@tonic-gate 
10137c478bd9Sstevel@tonic-gate 	rp = VTOR4(vp);
10147c478bd9Sstevel@tonic-gate 
10157c478bd9Sstevel@tonic-gate 	/*
10167c478bd9Sstevel@tonic-gate 	 * Grab the delegation type.  This function is protected against
10177c478bd9Sstevel@tonic-gate 	 * the delegation being returned by virtue of start_op (called
10187c478bd9Sstevel@tonic-gate 	 * by nfs4open_otw) taking the r_deleg_recall_lock in read mode,
10197c478bd9Sstevel@tonic-gate 	 * delegreturn requires this lock in write mode to proceed.
10207c478bd9Sstevel@tonic-gate 	 */
10217c478bd9Sstevel@tonic-gate 	ASSERT(nfs_rw_lock_held(&rp->r_deleg_recall_lock, RW_READER));
10227c478bd9Sstevel@tonic-gate 	dt = get_dtype(rp);
10237c478bd9Sstevel@tonic-gate 
10247c478bd9Sstevel@tonic-gate 	/* returns with 'os_sync_lock' held */
10257c478bd9Sstevel@tonic-gate 	osp = find_open_stream(oop, rp);
10267c478bd9Sstevel@tonic-gate 
10277c478bd9Sstevel@tonic-gate 	if (osp) {
10287c478bd9Sstevel@tonic-gate 		uint32_t	do_otw = 0;
10297c478bd9Sstevel@tonic-gate 
10307c478bd9Sstevel@tonic-gate 		if (osp->os_failed_reopen) {
10317c478bd9Sstevel@tonic-gate 			NFS4_DEBUG(nfs4_open_stream_debug, (CE_NOTE,
10327c478bd9Sstevel@tonic-gate 			    "nfs4_is_otw_open_necessary: os_failed_reopen "
10337c478bd9Sstevel@tonic-gate 			    "set on osp %p, cr %p, rp %s", (void *)osp,
10347c478bd9Sstevel@tonic-gate 			    (void *)osp->os_open_owner->oo_cred,
10357c478bd9Sstevel@tonic-gate 			    rnode4info(rp)));
10367c478bd9Sstevel@tonic-gate 			do_otw = 1;
10377c478bd9Sstevel@tonic-gate 		}
10387c478bd9Sstevel@tonic-gate 
10397c478bd9Sstevel@tonic-gate 		/*
10407c478bd9Sstevel@tonic-gate 		 * check access/deny bits
10417c478bd9Sstevel@tonic-gate 		 */
10427c478bd9Sstevel@tonic-gate 		if (!do_otw && (flag & FREAD))
10437c478bd9Sstevel@tonic-gate 			if (osp->os_share_acc_read == 0 &&
10447c478bd9Sstevel@tonic-gate 			    dt == OPEN_DELEGATE_NONE)
10457c478bd9Sstevel@tonic-gate 				do_otw = 1;
10467c478bd9Sstevel@tonic-gate 
10477c478bd9Sstevel@tonic-gate 		if (!do_otw && (flag & FWRITE))
10487c478bd9Sstevel@tonic-gate 			if (osp->os_share_acc_write == 0 &&
10497c478bd9Sstevel@tonic-gate 			    dt != OPEN_DELEGATE_WRITE)
10507c478bd9Sstevel@tonic-gate 				do_otw = 1;
10517c478bd9Sstevel@tonic-gate 
10527c478bd9Sstevel@tonic-gate 		if (!do_otw) {
10537c478bd9Sstevel@tonic-gate 			NFS4_DEBUG(nfs4_client_state_debug, (CE_NOTE,
1054bbf2a467SNagakiran Rajashekar 			    "nfs4_is_otw_open_necessary: can skip this "
1055bbf2a467SNagakiran Rajashekar 			    "open OTW"));
10567c478bd9Sstevel@tonic-gate 			if (!just_been_created) {
10577c478bd9Sstevel@tonic-gate 				osp->os_open_ref_count++;
10587c478bd9Sstevel@tonic-gate 				if (flag & FREAD)
10597c478bd9Sstevel@tonic-gate 					osp->os_share_acc_read++;
10607c478bd9Sstevel@tonic-gate 				if (flag & FWRITE)
10617c478bd9Sstevel@tonic-gate 					osp->os_share_acc_write++;
10627c478bd9Sstevel@tonic-gate 				osp->os_share_deny_none++;
10637c478bd9Sstevel@tonic-gate 			}
10647c478bd9Sstevel@tonic-gate 
10657c478bd9Sstevel@tonic-gate 			/*
10667c478bd9Sstevel@tonic-gate 			 * Need to reset this bitfield for the possible case
10677c478bd9Sstevel@tonic-gate 			 * where we were going to OTW CLOSE the file, got a
10687c478bd9Sstevel@tonic-gate 			 * non-recoverable error, and before we could retry
10697c478bd9Sstevel@tonic-gate 			 * the CLOSE, OPENed the file again.
10707c478bd9Sstevel@tonic-gate 			 */
10717c478bd9Sstevel@tonic-gate 			ASSERT(osp->os_open_owner->oo_seqid_inuse);
10727c478bd9Sstevel@tonic-gate 			osp->os_final_close = 0;
10737c478bd9Sstevel@tonic-gate 			osp->os_force_close = 0;
10747c478bd9Sstevel@tonic-gate 
10757c478bd9Sstevel@tonic-gate 			mutex_exit(&osp->os_sync_lock);
10767c478bd9Sstevel@tonic-gate 			open_stream_rele(osp, rp);
10777c478bd9Sstevel@tonic-gate 
10787c478bd9Sstevel@tonic-gate #ifdef	DEBUG
10797c478bd9Sstevel@tonic-gate 			bypass_otw[0]++;
10807c478bd9Sstevel@tonic-gate #endif
10817c478bd9Sstevel@tonic-gate 
10827c478bd9Sstevel@tonic-gate 			*errorp = 0;
10837c478bd9Sstevel@tonic-gate 			return (0);
10847c478bd9Sstevel@tonic-gate 		}
10857c478bd9Sstevel@tonic-gate 		mutex_exit(&osp->os_sync_lock);
10867c478bd9Sstevel@tonic-gate 		open_stream_rele(osp, rp);
10877c478bd9Sstevel@tonic-gate 
10887c478bd9Sstevel@tonic-gate 	} else if (dt != OPEN_DELEGATE_NONE) {
10897c478bd9Sstevel@tonic-gate 		/*
10907c478bd9Sstevel@tonic-gate 		 * Even if there isn't an open_stream yet, we may still be
10917c478bd9Sstevel@tonic-gate 		 * able to bypass the otw open if the client owns a delegation.
10927c478bd9Sstevel@tonic-gate 		 *
10937c478bd9Sstevel@tonic-gate 		 * If you are asking for for WRITE, but I only have
10947c478bd9Sstevel@tonic-gate 		 * a read delegation, then you still have to go otw.
10957c478bd9Sstevel@tonic-gate 		 */
10967c478bd9Sstevel@tonic-gate 
10977c478bd9Sstevel@tonic-gate 		if (flag & FWRITE && dt == OPEN_DELEGATE_READ)
10987c478bd9Sstevel@tonic-gate 			return (1);
10997c478bd9Sstevel@tonic-gate 
11007c478bd9Sstevel@tonic-gate 		/*
11017c478bd9Sstevel@tonic-gate 		 * TODO - evaluate the nfsace4
11027c478bd9Sstevel@tonic-gate 		 */
11037c478bd9Sstevel@tonic-gate 
11047c478bd9Sstevel@tonic-gate 		/*
11057c478bd9Sstevel@tonic-gate 		 * Check the access flags to make sure the caller
11067c478bd9Sstevel@tonic-gate 		 * had permission.
11077c478bd9Sstevel@tonic-gate 		 */
11087c478bd9Sstevel@tonic-gate 		if (flag & FREAD && !(acc & VREAD))
11097c478bd9Sstevel@tonic-gate 			return (1);
11107c478bd9Sstevel@tonic-gate 
11117c478bd9Sstevel@tonic-gate 		if (flag & FWRITE && !(acc & VWRITE))
11127c478bd9Sstevel@tonic-gate 			return (1);
11137c478bd9Sstevel@tonic-gate 
11147c478bd9Sstevel@tonic-gate 		/*
11157c478bd9Sstevel@tonic-gate 		 * create_open_stream will add a reference to oop,
11167c478bd9Sstevel@tonic-gate 		 * this will prevent the open_owner_rele done in
11177c478bd9Sstevel@tonic-gate 		 * nfs4open_otw from destroying the open_owner.
11187c478bd9Sstevel@tonic-gate 		 */
11197c478bd9Sstevel@tonic-gate 
11207c478bd9Sstevel@tonic-gate 		/* returns with 'os_sync_lock' held */
11217c478bd9Sstevel@tonic-gate 		osp = create_open_stream(oop, rp);
11227c478bd9Sstevel@tonic-gate 		if (osp == NULL)
11237c478bd9Sstevel@tonic-gate 			return (1);
11247c478bd9Sstevel@tonic-gate 
11257c478bd9Sstevel@tonic-gate 		osp->open_stateid = rp->r_deleg_stateid;
11267c478bd9Sstevel@tonic-gate 		osp->os_delegation = 1;
11277c478bd9Sstevel@tonic-gate 
11287c478bd9Sstevel@tonic-gate 		if (flag & FREAD)
11297c478bd9Sstevel@tonic-gate 			osp->os_share_acc_read++;
11307c478bd9Sstevel@tonic-gate 		if (flag & FWRITE)
11317c478bd9Sstevel@tonic-gate 			osp->os_share_acc_write++;
11327c478bd9Sstevel@tonic-gate 
11337c478bd9Sstevel@tonic-gate 		osp->os_share_deny_none++;
11347c478bd9Sstevel@tonic-gate 		mutex_exit(&osp->os_sync_lock);
11357c478bd9Sstevel@tonic-gate 
11367c478bd9Sstevel@tonic-gate 		open_stream_rele(osp, rp);
11377c478bd9Sstevel@tonic-gate 
11387c478bd9Sstevel@tonic-gate 		mutex_enter(&oop->oo_lock);
11397c478bd9Sstevel@tonic-gate 		oop->oo_just_created = NFS4_PERM_CREATED;
11407c478bd9Sstevel@tonic-gate 		mutex_exit(&oop->oo_lock);
11417c478bd9Sstevel@tonic-gate 
11427c478bd9Sstevel@tonic-gate 		ASSERT(rsp != NULL);
11437c478bd9Sstevel@tonic-gate 		if (rsp->rs_sp != NULL) {
11447c478bd9Sstevel@tonic-gate 			mutex_enter(&rsp->rs_sp->s_lock);
11457c478bd9Sstevel@tonic-gate 			nfs4_inc_state_ref_count_nolock(rsp->rs_sp,
1146bbf2a467SNagakiran Rajashekar 			    VTOMI4(vp));
11477c478bd9Sstevel@tonic-gate 			mutex_exit(&rsp->rs_sp->s_lock);
11487c478bd9Sstevel@tonic-gate 		}
11497c478bd9Sstevel@tonic-gate #ifdef	DEBUG
11507c478bd9Sstevel@tonic-gate 		bypass_otw[1]++;
11517c478bd9Sstevel@tonic-gate #endif
11527c478bd9Sstevel@tonic-gate 
11537c478bd9Sstevel@tonic-gate 		*errorp = 0;
11547c478bd9Sstevel@tonic-gate 		return (0);
11557c478bd9Sstevel@tonic-gate 	}
11567c478bd9Sstevel@tonic-gate 
11577c478bd9Sstevel@tonic-gate 	return (1);
11587c478bd9Sstevel@tonic-gate }
11597c478bd9Sstevel@tonic-gate 
11607c478bd9Sstevel@tonic-gate static open_delegation_type4
get_dtype(rnode4_t * rp)11617c478bd9Sstevel@tonic-gate get_dtype(rnode4_t *rp)
11627c478bd9Sstevel@tonic-gate {
11637c478bd9Sstevel@tonic-gate 	open_delegation_type4 dt;
11647c478bd9Sstevel@tonic-gate 
11657c478bd9Sstevel@tonic-gate 	mutex_enter(&rp->r_statev4_lock);
11667c478bd9Sstevel@tonic-gate 	ASSERT(!rp->r_deleg_return_inprog);
11677c478bd9Sstevel@tonic-gate 	if (rp->r_deleg_return_pending)
11687c478bd9Sstevel@tonic-gate 		dt = OPEN_DELEGATE_NONE;
11697c478bd9Sstevel@tonic-gate 	else
11707c478bd9Sstevel@tonic-gate 		dt = rp->r_deleg_type;
11717c478bd9Sstevel@tonic-gate 	mutex_exit(&rp->r_statev4_lock);
11727c478bd9Sstevel@tonic-gate 
11737c478bd9Sstevel@tonic-gate 	return (dt);
11747c478bd9Sstevel@tonic-gate }
11757c478bd9Sstevel@tonic-gate 
11767c478bd9Sstevel@tonic-gate /*
11777c478bd9Sstevel@tonic-gate  * Fill in *locker with the lock state arguments for a LOCK call.  If
11787c478bd9Sstevel@tonic-gate  * lop->lo_just_created == NFS4_JUST_CREATED, oop and osp must be non-NULL.
11797c478bd9Sstevel@tonic-gate  * Caller must already hold the necessary seqid sync lock(s).
11807c478bd9Sstevel@tonic-gate  */
11817c478bd9Sstevel@tonic-gate 
11827c478bd9Sstevel@tonic-gate void
nfs4_setup_lock_args(nfs4_lock_owner_t * lop,nfs4_open_owner_t * oop,nfs4_open_stream_t * osp,clientid4 clientid,locker4 * locker)11837c478bd9Sstevel@tonic-gate nfs4_setup_lock_args(nfs4_lock_owner_t *lop, nfs4_open_owner_t *oop,
1184957f6e72SMarcel Telka     nfs4_open_stream_t *osp, clientid4 clientid, locker4 *locker)
11857c478bd9Sstevel@tonic-gate {
11867c478bd9Sstevel@tonic-gate 	ASSERT(lop->lo_flags & NFS4_LOCK_SEQID_INUSE);
11877c478bd9Sstevel@tonic-gate 	if (lop->lo_just_created == NFS4_JUST_CREATED) {
11887c478bd9Sstevel@tonic-gate 		/* this is a new lock request */
11897c478bd9Sstevel@tonic-gate 		open_to_lock_owner4 *nown;
11907c478bd9Sstevel@tonic-gate 
11917c478bd9Sstevel@tonic-gate 		ASSERT(oop != NULL);
11927c478bd9Sstevel@tonic-gate 		ASSERT(osp != NULL);
11937c478bd9Sstevel@tonic-gate 
11947c478bd9Sstevel@tonic-gate 		locker->new_lock_owner = TRUE;
11957c478bd9Sstevel@tonic-gate 		nown = &locker->locker4_u.open_owner;
11967c478bd9Sstevel@tonic-gate 		nown->open_seqid = nfs4_get_open_seqid(oop) + 1;
11977c478bd9Sstevel@tonic-gate 		mutex_enter(&osp->os_sync_lock);
11987c478bd9Sstevel@tonic-gate 		nown->open_stateid = osp->open_stateid;
11997c478bd9Sstevel@tonic-gate 		mutex_exit(&osp->os_sync_lock);
12007c478bd9Sstevel@tonic-gate 		nown->lock_seqid = lop->lock_seqid; /* initial, so no +1 */
12017c478bd9Sstevel@tonic-gate 
12027c478bd9Sstevel@tonic-gate 		nown->lock_owner.clientid = clientid;
12037c478bd9Sstevel@tonic-gate 		nown->lock_owner.owner_len = sizeof (lop->lock_owner_name);
12047c478bd9Sstevel@tonic-gate 		nown->lock_owner.owner_val =
1205bbf2a467SNagakiran Rajashekar 		    kmem_alloc(nown->lock_owner.owner_len, KM_SLEEP);
12067c478bd9Sstevel@tonic-gate 		bcopy(&lop->lock_owner_name, nown->lock_owner.owner_val,
1207bbf2a467SNagakiran Rajashekar 		    nown->lock_owner.owner_len);
12087c478bd9Sstevel@tonic-gate 	} else {
12097c478bd9Sstevel@tonic-gate 		exist_lock_owner4 *eown;
12107c478bd9Sstevel@tonic-gate 		/* have an existing lock owner */
12117c478bd9Sstevel@tonic-gate 
12127c478bd9Sstevel@tonic-gate 		locker->new_lock_owner = FALSE;
12137c478bd9Sstevel@tonic-gate 		eown = &locker->locker4_u.lock_owner;
12147c478bd9Sstevel@tonic-gate 		mutex_enter(&lop->lo_lock);
12157c478bd9Sstevel@tonic-gate 		eown->lock_stateid = lop->lock_stateid;
12167c478bd9Sstevel@tonic-gate 		mutex_exit(&lop->lo_lock);
12177c478bd9Sstevel@tonic-gate 		eown->lock_seqid = lop->lock_seqid + 1;
12187c478bd9Sstevel@tonic-gate 	}
12197c478bd9Sstevel@tonic-gate }
12207c478bd9Sstevel@tonic-gate 
12217c478bd9Sstevel@tonic-gate /*
12227c478bd9Sstevel@tonic-gate  * This starts our use of the lock owner's lock seqid by setting
12237c478bd9Sstevel@tonic-gate  * the lo_flags to NFS4_LOCK_SEQID_INUSE.  We will wait (forever)
12247c478bd9Sstevel@tonic-gate  * with a cv_wait() until we are woken up.
12257c478bd9Sstevel@tonic-gate  *
12267c478bd9Sstevel@tonic-gate  * Return values:
12277c478bd9Sstevel@tonic-gate  * 0		no problems
12287c478bd9Sstevel@tonic-gate  * EAGAIN	caller should retry (like a recovery retry)
12297c478bd9Sstevel@tonic-gate  */
12307c478bd9Sstevel@tonic-gate int
nfs4_start_lock_seqid_sync(nfs4_lock_owner_t * lop,mntinfo4_t * mi)12317c478bd9Sstevel@tonic-gate nfs4_start_lock_seqid_sync(nfs4_lock_owner_t *lop, mntinfo4_t *mi)
12327c478bd9Sstevel@tonic-gate {
12337c478bd9Sstevel@tonic-gate 	int error = 0;
12347c478bd9Sstevel@tonic-gate #ifdef DEBUG
12357c478bd9Sstevel@tonic-gate 	static int ops = 0;		/* fault injection */
12367c478bd9Sstevel@tonic-gate #endif
12377c478bd9Sstevel@tonic-gate 
12387c478bd9Sstevel@tonic-gate #ifdef DEBUG
12397c478bd9Sstevel@tonic-gate 	if (seqid_sync_faults && curthread != mi->mi_recovthread &&
12407c478bd9Sstevel@tonic-gate 	    ++ops % 7 == 0)
12417c478bd9Sstevel@tonic-gate 		return (EAGAIN);
12427c478bd9Sstevel@tonic-gate #endif
12437c478bd9Sstevel@tonic-gate 
12447c478bd9Sstevel@tonic-gate 	mutex_enter(&mi->mi_lock);
12457c478bd9Sstevel@tonic-gate 	if ((mi->mi_flags & MI4_RECOV_ACTIV) &&
12467c478bd9Sstevel@tonic-gate 	    curthread != mi->mi_recovthread)
12477c478bd9Sstevel@tonic-gate 		error = EAGAIN;
12487c478bd9Sstevel@tonic-gate 	mutex_exit(&mi->mi_lock);
12497c478bd9Sstevel@tonic-gate 	if (error != 0)
12507c478bd9Sstevel@tonic-gate 		goto done;
12517c478bd9Sstevel@tonic-gate 
12527c478bd9Sstevel@tonic-gate 	mutex_enter(&lop->lo_lock);
12537c478bd9Sstevel@tonic-gate 
12547c478bd9Sstevel@tonic-gate 	ASSERT(lop->lo_seqid_holder != curthread);
12557c478bd9Sstevel@tonic-gate 	while (lop->lo_flags & NFS4_LOCK_SEQID_INUSE) {
12567c478bd9Sstevel@tonic-gate 		NFS4_DEBUG(nfs4_seqid_sync, (CE_NOTE,
1257bbf2a467SNagakiran Rajashekar 		    "nfs4_start_lock_seqid_sync: waiting on cv"));
12587c478bd9Sstevel@tonic-gate 
12597c478bd9Sstevel@tonic-gate 		cv_wait(&lop->lo_cv_seqid_sync, &lop->lo_lock);
12607c478bd9Sstevel@tonic-gate 	}
12617c478bd9Sstevel@tonic-gate 	NFS4_DEBUG(nfs4_seqid_sync, (CE_NOTE, "nfs4_start_lock_seqid_sync: "
1262bbf2a467SNagakiran Rajashekar 	    "NFS4_LOCK_SEQID_INUSE"));
12637c478bd9Sstevel@tonic-gate 
12647c478bd9Sstevel@tonic-gate 	lop->lo_flags |= NFS4_LOCK_SEQID_INUSE;
12657c478bd9Sstevel@tonic-gate 	lop->lo_seqid_holder = curthread;
12667c478bd9Sstevel@tonic-gate 	mutex_exit(&lop->lo_lock);
12677c478bd9Sstevel@tonic-gate 
12687c478bd9Sstevel@tonic-gate 	mutex_enter(&mi->mi_lock);
12697c478bd9Sstevel@tonic-gate 	if ((mi->mi_flags & MI4_RECOV_ACTIV) &&
12707c478bd9Sstevel@tonic-gate 	    curthread != mi->mi_recovthread)
12717c478bd9Sstevel@tonic-gate 		error = EAGAIN;
12727c478bd9Sstevel@tonic-gate 	mutex_exit(&mi->mi_lock);
12737c478bd9Sstevel@tonic-gate 
12747c478bd9Sstevel@tonic-gate 	if (error == EAGAIN)
12757c478bd9Sstevel@tonic-gate 		nfs4_end_lock_seqid_sync(lop);
12767c478bd9Sstevel@tonic-gate 
12777c478bd9Sstevel@tonic-gate 	NFS4_DEBUG(nfs4_seqid_sync, (CE_NOTE,
1278bbf2a467SNagakiran Rajashekar 	    "nfs4_start_lock_seqid_sync: error=%d", error));
12797c478bd9Sstevel@tonic-gate 
12807c478bd9Sstevel@tonic-gate done:
12817c478bd9Sstevel@tonic-gate 	return (error);
12827c478bd9Sstevel@tonic-gate }
12837c478bd9Sstevel@tonic-gate 
12847c478bd9Sstevel@tonic-gate /*
12857c478bd9Sstevel@tonic-gate  * This ends our use of the lock owner's lock seqid by setting
12867c478bd9Sstevel@tonic-gate  * the appropiate flags and issuing a cv_signal to wake up another
12877c478bd9Sstevel@tonic-gate  * thread waiting to use the lock seqid.
12887c478bd9Sstevel@tonic-gate  */
12897c478bd9Sstevel@tonic-gate void
nfs4_end_lock_seqid_sync(nfs4_lock_owner_t * lop)12907c478bd9Sstevel@tonic-gate nfs4_end_lock_seqid_sync(nfs4_lock_owner_t *lop)
12917c478bd9Sstevel@tonic-gate {
12927c478bd9Sstevel@tonic-gate 	mutex_enter(&lop->lo_lock);
12937c478bd9Sstevel@tonic-gate 	ASSERT(lop->lo_flags & NFS4_LOCK_SEQID_INUSE);
12947c478bd9Sstevel@tonic-gate 	ASSERT(lop->lo_seqid_holder == curthread);
12957c478bd9Sstevel@tonic-gate 	lop->lo_flags &= ~NFS4_LOCK_SEQID_INUSE;
12967c478bd9Sstevel@tonic-gate 	lop->lo_seqid_holder = NULL;
1297*f2211ffeSMarcel Telka 	cv_signal(&lop->lo_cv_seqid_sync);
12987c478bd9Sstevel@tonic-gate 	mutex_exit(&lop->lo_lock);
12997c478bd9Sstevel@tonic-gate }
13007c478bd9Sstevel@tonic-gate 
13017c478bd9Sstevel@tonic-gate /*
13027c478bd9Sstevel@tonic-gate  * Returns a reference to a lock owner via lopp, which has its lock seqid
13037c478bd9Sstevel@tonic-gate  * synchronization started.
13047c478bd9Sstevel@tonic-gate  * If the lock owner is in the 'just_created' state, then we return its open
13057c478bd9Sstevel@tonic-gate  * owner and open stream and start the open seqid synchronization.
13067c478bd9Sstevel@tonic-gate  *
13077c478bd9Sstevel@tonic-gate  * Return value:
13087c478bd9Sstevel@tonic-gate  * NFS4_OK		no problems
13097c478bd9Sstevel@tonic-gate  * NFS4ERR_DELAY	there is lost state to recover; caller should retry
13107c478bd9Sstevel@tonic-gate  * NFS4ERR_IO		no open stream
13117c478bd9Sstevel@tonic-gate  */
13127c478bd9Sstevel@tonic-gate nfsstat4
nfs4_find_or_create_lock_owner(pid_t pid,rnode4_t * rp,cred_t * cr,nfs4_open_owner_t ** oopp,nfs4_open_stream_t ** ospp,nfs4_lock_owner_t ** lopp)13137c478bd9Sstevel@tonic-gate nfs4_find_or_create_lock_owner(pid_t pid, rnode4_t *rp, cred_t *cr,
1314957f6e72SMarcel Telka     nfs4_open_owner_t **oopp, nfs4_open_stream_t **ospp,
1315957f6e72SMarcel Telka     nfs4_lock_owner_t **lopp)
13167c478bd9Sstevel@tonic-gate {
13177c478bd9Sstevel@tonic-gate 	nfs4_lock_owner_t *lop, *next_lop;
13187c478bd9Sstevel@tonic-gate 	mntinfo4_t *mi;
13197c478bd9Sstevel@tonic-gate 	int error = 0;
13207c478bd9Sstevel@tonic-gate 	nfsstat4 stat;
13217c478bd9Sstevel@tonic-gate 
13227c478bd9Sstevel@tonic-gate 	mi = VTOMI4(RTOV4(rp));
13237c478bd9Sstevel@tonic-gate 
13247c478bd9Sstevel@tonic-gate 	mutex_enter(&rp->r_statev4_lock);
13257c478bd9Sstevel@tonic-gate 
13267c478bd9Sstevel@tonic-gate 	lop = rp->r_lo_head.lo_next_rnode;
13277c478bd9Sstevel@tonic-gate 	while (lop != &rp->r_lo_head) {
13287c478bd9Sstevel@tonic-gate 		mutex_enter(&lop->lo_lock);
13297c478bd9Sstevel@tonic-gate 		if (lop->lo_pid == pid && lop->lo_valid != 0) {
13307c478bd9Sstevel@tonic-gate 			/* Found a matching lock owner */
13317c478bd9Sstevel@tonic-gate 			NFS4_DEBUG(nfs4_client_state_debug,
1332bbf2a467SNagakiran Rajashekar 			    (CE_NOTE, "nfs4_find_or_create_lock_owner: "
1333bbf2a467SNagakiran Rajashekar 			    "got a match"));
13347c478bd9Sstevel@tonic-gate 			lop->lo_ref_count++;
13357c478bd9Sstevel@tonic-gate 			break;
13367c478bd9Sstevel@tonic-gate 		}
13377c478bd9Sstevel@tonic-gate 		next_lop = lop->lo_next_rnode;
13387c478bd9Sstevel@tonic-gate 		mutex_exit(&lop->lo_lock);
13397c478bd9Sstevel@tonic-gate 		lop = next_lop;
13407c478bd9Sstevel@tonic-gate 	}
13417c478bd9Sstevel@tonic-gate 
13427c478bd9Sstevel@tonic-gate 	if (lop == &rp->r_lo_head) {
13437c478bd9Sstevel@tonic-gate 		/* create temporary lock owner */
13447c478bd9Sstevel@tonic-gate 		lop = create_lock_owner(rp, pid);
13457c478bd9Sstevel@tonic-gate 	}
13467c478bd9Sstevel@tonic-gate 	mutex_exit(&rp->r_statev4_lock);
13477c478bd9Sstevel@tonic-gate 
13487c478bd9Sstevel@tonic-gate 	/* Have a locked down lock owner struct now */
13497c478bd9Sstevel@tonic-gate 	if (lop->lo_just_created != NFS4_JUST_CREATED) {
13507c478bd9Sstevel@tonic-gate 		/* This is an existing lock owner */
13517c478bd9Sstevel@tonic-gate 		*oopp = NULL;
13527c478bd9Sstevel@tonic-gate 		*ospp = NULL;
13537c478bd9Sstevel@tonic-gate 	} else {
13547c478bd9Sstevel@tonic-gate 		/* Lock owner doesn't exist yet */
13557c478bd9Sstevel@tonic-gate 
13567c478bd9Sstevel@tonic-gate 		/* First grab open owner seqid synchronization */
13577c478bd9Sstevel@tonic-gate 		mutex_exit(&lop->lo_lock);
13587c478bd9Sstevel@tonic-gate 		*oopp = find_open_owner(cr, NFS4_PERM_CREATED, mi);
13597c478bd9Sstevel@tonic-gate 		if (*oopp == NULL)
13607c478bd9Sstevel@tonic-gate 			goto kill_new_lop;
13617c478bd9Sstevel@tonic-gate 		error = nfs4_start_open_seqid_sync(*oopp, mi);
13627c478bd9Sstevel@tonic-gate 		if (error == EAGAIN) {
13637c478bd9Sstevel@tonic-gate 			stat = NFS4ERR_DELAY;
13647c478bd9Sstevel@tonic-gate 			goto failed;
13657c478bd9Sstevel@tonic-gate 		}
13667c478bd9Sstevel@tonic-gate 		*ospp = find_open_stream(*oopp, rp);
13677c478bd9Sstevel@tonic-gate 		if (*ospp == NULL) {
13687c478bd9Sstevel@tonic-gate 			nfs4_end_open_seqid_sync(*oopp);
13697c478bd9Sstevel@tonic-gate 			goto kill_new_lop;
13707c478bd9Sstevel@tonic-gate 		}
13717c478bd9Sstevel@tonic-gate 		if ((*ospp)->os_failed_reopen) {
13727c478bd9Sstevel@tonic-gate 			mutex_exit(&(*ospp)->os_sync_lock);
13737c478bd9Sstevel@tonic-gate 			NFS4_DEBUG((nfs4_open_stream_debug ||
1374bbf2a467SNagakiran Rajashekar 			    nfs4_client_lock_debug), (CE_NOTE,
13757c478bd9Sstevel@tonic-gate 			    "nfs4_find_or_create_lock_owner: os_failed_reopen;"
13767c478bd9Sstevel@tonic-gate 			    "osp %p, cr %p, rp %s", (void *)(*ospp),
13777c478bd9Sstevel@tonic-gate 			    (void *)cr, rnode4info(rp)));
13787c478bd9Sstevel@tonic-gate 			nfs4_end_open_seqid_sync(*oopp);
13797c478bd9Sstevel@tonic-gate 			stat = NFS4ERR_IO;
13807c478bd9Sstevel@tonic-gate 			goto failed;
13817c478bd9Sstevel@tonic-gate 		}
13827c478bd9Sstevel@tonic-gate 		mutex_exit(&(*ospp)->os_sync_lock);
13837c478bd9Sstevel@tonic-gate 
13847c478bd9Sstevel@tonic-gate 		/*
13857c478bd9Sstevel@tonic-gate 		 * Now see if the lock owner has become permanent while we
13867c478bd9Sstevel@tonic-gate 		 * had released our lock.
13877c478bd9Sstevel@tonic-gate 		 */
13887c478bd9Sstevel@tonic-gate 		mutex_enter(&lop->lo_lock);
13897c478bd9Sstevel@tonic-gate 		if (lop->lo_just_created != NFS4_JUST_CREATED) {
13907c478bd9Sstevel@tonic-gate 			nfs4_end_open_seqid_sync(*oopp);
13917c478bd9Sstevel@tonic-gate 			open_stream_rele(*ospp, rp);
13927c478bd9Sstevel@tonic-gate 			open_owner_rele(*oopp);
13937c478bd9Sstevel@tonic-gate 			*oopp = NULL;
13947c478bd9Sstevel@tonic-gate 			*ospp = NULL;
13957c478bd9Sstevel@tonic-gate 		}
13967c478bd9Sstevel@tonic-gate 	}
13977c478bd9Sstevel@tonic-gate 	mutex_exit(&lop->lo_lock);
13987c478bd9Sstevel@tonic-gate 
13997c478bd9Sstevel@tonic-gate 	error = nfs4_start_lock_seqid_sync(lop, mi);
14007c478bd9Sstevel@tonic-gate 	if (error == EAGAIN) {
14017c478bd9Sstevel@tonic-gate 		if (*oopp != NULL)
14027c478bd9Sstevel@tonic-gate 			nfs4_end_open_seqid_sync(*oopp);
14037c478bd9Sstevel@tonic-gate 		stat = NFS4ERR_DELAY;
14047c478bd9Sstevel@tonic-gate 		goto failed;
14057c478bd9Sstevel@tonic-gate 	}
14067c478bd9Sstevel@tonic-gate 	ASSERT(error == 0);
14077c478bd9Sstevel@tonic-gate 
14087c478bd9Sstevel@tonic-gate 	*lopp = lop;
14097c478bd9Sstevel@tonic-gate 	return (NFS4_OK);
14107c478bd9Sstevel@tonic-gate 
14117c478bd9Sstevel@tonic-gate kill_new_lop:
14127c478bd9Sstevel@tonic-gate 	/*
14137c478bd9Sstevel@tonic-gate 	 * A previous CLOSE was attempted but got EINTR, but the application
14147c478bd9Sstevel@tonic-gate 	 * continued to use the unspecified state file descriptor.  But now the
14157c478bd9Sstevel@tonic-gate 	 * open stream is gone (which could also destroy the open owner), hence
14167c478bd9Sstevel@tonic-gate 	 * we can no longer continue.  The calling function should return EIO
14177c478bd9Sstevel@tonic-gate 	 * to the application.
14187c478bd9Sstevel@tonic-gate 	 */
14197c478bd9Sstevel@tonic-gate 	NFS4_DEBUG(nfs4_lost_rqst_debug || nfs4_client_lock_debug,
14207c478bd9Sstevel@tonic-gate 	    (CE_NOTE, "nfs4_find_or_create_lock_owner: destroy newly created "
14217c478bd9Sstevel@tonic-gate 	    "lop %p, oop %p, osp %p", (void *)lop, (void *)(*oopp),
14227c478bd9Sstevel@tonic-gate 	    (void *)(*ospp)));
14237c478bd9Sstevel@tonic-gate 
14247c478bd9Sstevel@tonic-gate 	nfs4_rnode_remove_lock_owner(rp, lop);
14257c478bd9Sstevel@tonic-gate 	stat = NFS4ERR_IO;
14267c478bd9Sstevel@tonic-gate 
14277c478bd9Sstevel@tonic-gate failed:
14287c478bd9Sstevel@tonic-gate 	lock_owner_rele(lop);
14297c478bd9Sstevel@tonic-gate 	if (*oopp) {
14307c478bd9Sstevel@tonic-gate 		open_owner_rele(*oopp);
14317c478bd9Sstevel@tonic-gate 		*oopp = NULL;
14327c478bd9Sstevel@tonic-gate 	}
14337c478bd9Sstevel@tonic-gate 	if (*ospp) {
14347c478bd9Sstevel@tonic-gate 		open_stream_rele(*ospp, rp);
14357c478bd9Sstevel@tonic-gate 		*ospp = NULL;
14367c478bd9Sstevel@tonic-gate 	}
14377c478bd9Sstevel@tonic-gate 	return (stat);
14387c478bd9Sstevel@tonic-gate }
14397c478bd9Sstevel@tonic-gate 
14407c478bd9Sstevel@tonic-gate /*
14417c478bd9Sstevel@tonic-gate  * This function grabs a recently freed open owner off of the freed open
14427c478bd9Sstevel@tonic-gate  * owner list if there is a match on the cred 'cr'.  It returns NULL if no
14437c478bd9Sstevel@tonic-gate  * such match is found.  It will set the 'oo_ref_count' and 'oo_valid' back
14447c478bd9Sstevel@tonic-gate  * to both 1 (sane values) in the case a match is found.
14457c478bd9Sstevel@tonic-gate  */
14467c478bd9Sstevel@tonic-gate static nfs4_open_owner_t *
find_freed_open_owner(cred_t * cr,nfs4_oo_hash_bucket_t * bucketp,mntinfo4_t * mi)14477c478bd9Sstevel@tonic-gate find_freed_open_owner(cred_t *cr, nfs4_oo_hash_bucket_t *bucketp,
1448957f6e72SMarcel Telka     mntinfo4_t *mi)
14497c478bd9Sstevel@tonic-gate {
14507c478bd9Sstevel@tonic-gate 	nfs4_open_owner_t		*foop;
14517c478bd9Sstevel@tonic-gate 
14527c478bd9Sstevel@tonic-gate 	NFS4_DEBUG(nfs4_client_state_debug, (CE_NOTE,
1453bbf2a467SNagakiran Rajashekar 	    "find_freed_open_owner: cred %p", (void*)cr));
14547c478bd9Sstevel@tonic-gate 
14557c478bd9Sstevel@tonic-gate 	ASSERT(mutex_owned(&mi->mi_lock));
14567c478bd9Sstevel@tonic-gate 	ASSERT(mutex_owned(&bucketp->b_lock));
14577c478bd9Sstevel@tonic-gate 
14587c478bd9Sstevel@tonic-gate 	/* got hash bucket, search through freed open owners */
14597c478bd9Sstevel@tonic-gate 	for (foop = list_head(&mi->mi_foo_list); foop != NULL;
14607c478bd9Sstevel@tonic-gate 	    foop = list_next(&mi->mi_foo_list, foop)) {
14617c478bd9Sstevel@tonic-gate 		if (!crcmp(foop->oo_cred, cr)) {
14627c478bd9Sstevel@tonic-gate 			NFS4_DEBUG(nfs4_client_foo_debug, (CE_NOTE,
1463bbf2a467SNagakiran Rajashekar 			    "find_freed_open_owner: got a match open owner "
1464bbf2a467SNagakiran Rajashekar 			    "%p", (void *)foop));
14657c478bd9Sstevel@tonic-gate 			foop->oo_ref_count = 1;
14667c478bd9Sstevel@tonic-gate 			foop->oo_valid = 1;
14677c478bd9Sstevel@tonic-gate 			list_remove(&mi->mi_foo_list, foop);
14687c478bd9Sstevel@tonic-gate 			mi->mi_foo_num--;
14697c478bd9Sstevel@tonic-gate 
14707c478bd9Sstevel@tonic-gate 			/* now add the struct into the cred hash table */
14717c478bd9Sstevel@tonic-gate 			list_insert_head(&bucketp->b_oo_hash_list, foop);
14727c478bd9Sstevel@tonic-gate 			return (foop);
14737c478bd9Sstevel@tonic-gate 		}
14747c478bd9Sstevel@tonic-gate 	}
14757c478bd9Sstevel@tonic-gate 
14767c478bd9Sstevel@tonic-gate 	return (NULL);
14777c478bd9Sstevel@tonic-gate }
14787c478bd9Sstevel@tonic-gate 
14797c478bd9Sstevel@tonic-gate /*
14807c478bd9Sstevel@tonic-gate  * Insert the newly freed 'oop' into the mi's freed oop list,
14817c478bd9Sstevel@tonic-gate  * always at the head of the list.  If we've already reached
14827c478bd9Sstevel@tonic-gate  * our maximum allowed number of freed open owners (mi_foo_max),
14837c478bd9Sstevel@tonic-gate  * then remove the LRU open owner on the list (namely the tail).
14847c478bd9Sstevel@tonic-gate  */
14857c478bd9Sstevel@tonic-gate static void
nfs4_free_open_owner(nfs4_open_owner_t * oop,mntinfo4_t * mi)14867c478bd9Sstevel@tonic-gate nfs4_free_open_owner(nfs4_open_owner_t *oop, mntinfo4_t *mi)
14877c478bd9Sstevel@tonic-gate {
14887c478bd9Sstevel@tonic-gate 	nfs4_open_owner_t *lru_foop;
14897c478bd9Sstevel@tonic-gate 
14907c478bd9Sstevel@tonic-gate 	if (mi->mi_foo_num < mi->mi_foo_max) {
14917c478bd9Sstevel@tonic-gate 		NFS4_DEBUG(nfs4_client_foo_debug, (CE_NOTE,
1492bbf2a467SNagakiran Rajashekar 		    "nfs4_free_open_owner: num free %d, max free %d, "
1493bbf2a467SNagakiran Rajashekar 		    "insert open owner %p for mntinfo4 %p",
1494bbf2a467SNagakiran Rajashekar 		    mi->mi_foo_num, mi->mi_foo_max, (void *)oop,
1495bbf2a467SNagakiran Rajashekar 		    (void *)mi));
14967c478bd9Sstevel@tonic-gate 		list_insert_head(&mi->mi_foo_list, oop);
14977c478bd9Sstevel@tonic-gate 		mi->mi_foo_num++;
14987c478bd9Sstevel@tonic-gate 		return;
14997c478bd9Sstevel@tonic-gate 	}
15007c478bd9Sstevel@tonic-gate 
15017c478bd9Sstevel@tonic-gate 	/* need to replace a freed open owner */
15027c478bd9Sstevel@tonic-gate 
15037c478bd9Sstevel@tonic-gate 	lru_foop = list_tail(&mi->mi_foo_list);
15047c478bd9Sstevel@tonic-gate 
15057c478bd9Sstevel@tonic-gate 	NFS4_DEBUG(nfs4_client_foo_debug, (CE_NOTE,
15067c478bd9Sstevel@tonic-gate 	    "nfs4_free_open_owner: destroy %p, insert %p",
15077c478bd9Sstevel@tonic-gate 	    (void *)lru_foop, (void *)oop));
15087c478bd9Sstevel@tonic-gate 
15097c478bd9Sstevel@tonic-gate 	list_remove(&mi->mi_foo_list, lru_foop);
15107c478bd9Sstevel@tonic-gate 	nfs4_destroy_open_owner(lru_foop);
15117c478bd9Sstevel@tonic-gate 
15127c478bd9Sstevel@tonic-gate 	/* head always has latest freed oop */
15137c478bd9Sstevel@tonic-gate 	list_insert_head(&mi->mi_foo_list, oop);
15147c478bd9Sstevel@tonic-gate }
15157c478bd9Sstevel@tonic-gate 
15167c478bd9Sstevel@tonic-gate void
nfs4_destroy_open_owner(nfs4_open_owner_t * oop)15177c478bd9Sstevel@tonic-gate nfs4_destroy_open_owner(nfs4_open_owner_t *oop)
15187c478bd9Sstevel@tonic-gate {
15197c478bd9Sstevel@tonic-gate 	ASSERT(oop != NULL);
15207c478bd9Sstevel@tonic-gate 
15217c478bd9Sstevel@tonic-gate 	crfree(oop->oo_cred);
15227c478bd9Sstevel@tonic-gate 	if (oop->oo_cred_otw)
15237c478bd9Sstevel@tonic-gate 		crfree(oop->oo_cred_otw);
15247c478bd9Sstevel@tonic-gate 	mutex_destroy(&oop->oo_lock);
15257c478bd9Sstevel@tonic-gate 	cv_destroy(&oop->oo_cv_seqid_sync);
15267c478bd9Sstevel@tonic-gate 	kmem_free(oop, sizeof (*oop));
15277c478bd9Sstevel@tonic-gate }
15287c478bd9Sstevel@tonic-gate 
15297c478bd9Sstevel@tonic-gate seqid4
nfs4_get_open_seqid(nfs4_open_owner_t * oop)15307c478bd9Sstevel@tonic-gate nfs4_get_open_seqid(nfs4_open_owner_t *oop)
15317c478bd9Sstevel@tonic-gate {
15327c478bd9Sstevel@tonic-gate 	ASSERT(oop->oo_seqid_inuse);
15337c478bd9Sstevel@tonic-gate 	return (oop->oo_seqid);
15347c478bd9Sstevel@tonic-gate }
15357c478bd9Sstevel@tonic-gate 
15367c478bd9Sstevel@tonic-gate /*
15377c478bd9Sstevel@tonic-gate  * This set's the open seqid for a <open owner/ mntinfo4> pair.
15387c478bd9Sstevel@tonic-gate  */
15397c478bd9Sstevel@tonic-gate void
nfs4_set_open_seqid(seqid4 seqid,nfs4_open_owner_t * oop,nfs4_tag_type_t tag_type)15407c478bd9Sstevel@tonic-gate nfs4_set_open_seqid(seqid4 seqid, nfs4_open_owner_t *oop,
1541957f6e72SMarcel Telka     nfs4_tag_type_t tag_type)
15427c478bd9Sstevel@tonic-gate {
15437c478bd9Sstevel@tonic-gate 	ASSERT(oop->oo_seqid_inuse);
15447c478bd9Sstevel@tonic-gate 	oop->oo_seqid = seqid;
15457c478bd9Sstevel@tonic-gate 	oop->oo_last_good_seqid = seqid;
15467c478bd9Sstevel@tonic-gate 	oop->oo_last_good_op = tag_type;
15477c478bd9Sstevel@tonic-gate }
15487c478bd9Sstevel@tonic-gate 
15497c478bd9Sstevel@tonic-gate /*
15507c478bd9Sstevel@tonic-gate  * This bumps the current open seqid for the open owner 'oop'.
15517c478bd9Sstevel@tonic-gate  */
15527c478bd9Sstevel@tonic-gate void
nfs4_get_and_set_next_open_seqid(nfs4_open_owner_t * oop,nfs4_tag_type_t tag_type)15537c478bd9Sstevel@tonic-gate nfs4_get_and_set_next_open_seqid(nfs4_open_owner_t *oop,
15547c478bd9Sstevel@tonic-gate     nfs4_tag_type_t tag_type)
15557c478bd9Sstevel@tonic-gate {
15567c478bd9Sstevel@tonic-gate 	ASSERT(oop->oo_seqid_inuse);
15577c478bd9Sstevel@tonic-gate 	oop->oo_seqid++;
15587c478bd9Sstevel@tonic-gate 	oop->oo_last_good_seqid = oop->oo_seqid;
15597c478bd9Sstevel@tonic-gate 	oop->oo_last_good_op = tag_type;
15607c478bd9Sstevel@tonic-gate }
15617c478bd9Sstevel@tonic-gate 
15627c478bd9Sstevel@tonic-gate /*
15637c478bd9Sstevel@tonic-gate  * If no open owner was provided, this function takes the cred to find an
15647c478bd9Sstevel@tonic-gate  * open owner within the given mntinfo4_t.  Either way we return the
15657c478bd9Sstevel@tonic-gate  * open owner's OTW credential if it exists; otherwise returns the
15667c478bd9Sstevel@tonic-gate  * supplied 'cr'.
15677c478bd9Sstevel@tonic-gate  *
15687c478bd9Sstevel@tonic-gate  * A hold is put on the returned credential, and it is up to the caller
15697c478bd9Sstevel@tonic-gate  * to free the cred.
15707c478bd9Sstevel@tonic-gate  */
15717c478bd9Sstevel@tonic-gate cred_t *
nfs4_get_otw_cred(cred_t * cr,mntinfo4_t * mi,nfs4_open_owner_t * provided_oop)15727c478bd9Sstevel@tonic-gate nfs4_get_otw_cred(cred_t *cr, mntinfo4_t *mi, nfs4_open_owner_t *provided_oop)
15737c478bd9Sstevel@tonic-gate {
15747c478bd9Sstevel@tonic-gate 	cred_t *ret_cr;
15757c478bd9Sstevel@tonic-gate 	nfs4_open_owner_t *oop = provided_oop;
15767c478bd9Sstevel@tonic-gate 
15777c478bd9Sstevel@tonic-gate 	if (oop == NULL)
15787c478bd9Sstevel@tonic-gate 		oop = find_open_owner(cr, NFS4_PERM_CREATED, mi);
15797c478bd9Sstevel@tonic-gate 	if (oop != NULL) {
15807c478bd9Sstevel@tonic-gate 		mutex_enter(&oop->oo_lock);
15817c478bd9Sstevel@tonic-gate 		if (oop->oo_cred_otw)
15827c478bd9Sstevel@tonic-gate 			ret_cr = oop->oo_cred_otw;
15837c478bd9Sstevel@tonic-gate 		else
15847c478bd9Sstevel@tonic-gate 			ret_cr = cr;
15857c478bd9Sstevel@tonic-gate 		crhold(ret_cr);
15867c478bd9Sstevel@tonic-gate 		mutex_exit(&oop->oo_lock);
15877c478bd9Sstevel@tonic-gate 		if (provided_oop == NULL)
15887c478bd9Sstevel@tonic-gate 			open_owner_rele(oop);
15897c478bd9Sstevel@tonic-gate 	} else {
15907c478bd9Sstevel@tonic-gate 		ret_cr = cr;
15917c478bd9Sstevel@tonic-gate 		crhold(ret_cr);
15927c478bd9Sstevel@tonic-gate 	}
15937c478bd9Sstevel@tonic-gate 	return (ret_cr);
15947c478bd9Sstevel@tonic-gate }
15957c478bd9Sstevel@tonic-gate 
15967c478bd9Sstevel@tonic-gate /*
15977c478bd9Sstevel@tonic-gate  * Retrieves the next open stream in the rnode's list if an open stream
15987c478bd9Sstevel@tonic-gate  * is provided; otherwise gets the first open stream in the list.
15997c478bd9Sstevel@tonic-gate  * The open owner for that open stream is then retrieved, and if its
16007c478bd9Sstevel@tonic-gate  * oo_cred_otw exists then it is returned; otherwise the provided 'cr'
16017c478bd9Sstevel@tonic-gate  * is returned.  *osp is set to the 'found' open stream.
16027c478bd9Sstevel@tonic-gate  *
16037c478bd9Sstevel@tonic-gate  * Note: we don't set *osp to the open stream retrieved via the
16047c478bd9Sstevel@tonic-gate  * optimized check since that won't necessarily be at the beginning
16057c478bd9Sstevel@tonic-gate  * of the rnode list, and if that osp doesn't work we'd like to
16067c478bd9Sstevel@tonic-gate  * check _all_ open streams (starting from the beginning of the
16077c478bd9Sstevel@tonic-gate  * rnode list).
16087c478bd9Sstevel@tonic-gate  */
16097c478bd9Sstevel@tonic-gate cred_t *
nfs4_get_otw_cred_by_osp(rnode4_t * rp,cred_t * cr,nfs4_open_stream_t ** osp,bool_t * first_time,bool_t * last_time)16107c478bd9Sstevel@tonic-gate nfs4_get_otw_cred_by_osp(rnode4_t *rp, cred_t *cr,
1611957f6e72SMarcel Telka     nfs4_open_stream_t **osp, bool_t *first_time, bool_t *last_time)
16127c478bd9Sstevel@tonic-gate {
16137c478bd9Sstevel@tonic-gate 	nfs4_open_stream_t *next_osp = NULL;
16147c478bd9Sstevel@tonic-gate 	cred_t *ret_cr;
16157c478bd9Sstevel@tonic-gate 
16167c478bd9Sstevel@tonic-gate 	ASSERT(cr != NULL);
16177c478bd9Sstevel@tonic-gate 	/*
16187c478bd9Sstevel@tonic-gate 	 * As an optimization, try to find the open owner
16197c478bd9Sstevel@tonic-gate 	 * for the cred provided since that's most likely
16207c478bd9Sstevel@tonic-gate 	 * to work.
16217c478bd9Sstevel@tonic-gate 	 */
16227c478bd9Sstevel@tonic-gate 	if (*first_time) {
16237c478bd9Sstevel@tonic-gate 		nfs4_open_owner_t *oop;
16247c478bd9Sstevel@tonic-gate 
16257c478bd9Sstevel@tonic-gate 		oop = find_open_owner(cr, NFS4_PERM_CREATED, VTOMI4(RTOV4(rp)));
16267c478bd9Sstevel@tonic-gate 		if (oop) {
16277c478bd9Sstevel@tonic-gate 			next_osp = find_open_stream(oop, rp);
16287c478bd9Sstevel@tonic-gate 			if (next_osp)
16297c478bd9Sstevel@tonic-gate 				mutex_exit(&next_osp->os_sync_lock);
16307c478bd9Sstevel@tonic-gate 			open_owner_rele(oop);
16317c478bd9Sstevel@tonic-gate 		}
1632479626d4SGerald Thornbrugh 	}
1633479626d4SGerald Thornbrugh 	if (next_osp == NULL) {
16347c478bd9Sstevel@tonic-gate 		int delay_rele = 0;
1635479626d4SGerald Thornbrugh 		*first_time = FALSE;
16367c478bd9Sstevel@tonic-gate 
16377c478bd9Sstevel@tonic-gate 		/* return the next open stream for this rnode */
16387c478bd9Sstevel@tonic-gate 		mutex_enter(&rp->r_os_lock);
16397c478bd9Sstevel@tonic-gate 		/* Now, no one can add or delete to rp's open streams list */
16407c478bd9Sstevel@tonic-gate 
16417c478bd9Sstevel@tonic-gate 		if (*osp) {
16427c478bd9Sstevel@tonic-gate 			next_osp = list_next(&rp->r_open_streams, *osp);
16437c478bd9Sstevel@tonic-gate 			/*
16447c478bd9Sstevel@tonic-gate 			 * Delay the rele of *osp until after we drop
16457c478bd9Sstevel@tonic-gate 			 * r_os_lock to not deadlock with oo_lock
16467c478bd9Sstevel@tonic-gate 			 * via an open_stream_rele()->open_owner_rele().
16477c478bd9Sstevel@tonic-gate 			 */
16487c478bd9Sstevel@tonic-gate 			delay_rele = 1;
16497c478bd9Sstevel@tonic-gate 		} else {
16507c478bd9Sstevel@tonic-gate 			next_osp = list_head(&rp->r_open_streams);
16517c478bd9Sstevel@tonic-gate 		}
16527c478bd9Sstevel@tonic-gate 		if (next_osp) {
16537c478bd9Sstevel@tonic-gate 			nfs4_open_stream_t *tmp_osp;
16547c478bd9Sstevel@tonic-gate 
16557c478bd9Sstevel@tonic-gate 			/* find the next valid open stream */
16567c478bd9Sstevel@tonic-gate 			mutex_enter(&next_osp->os_sync_lock);
16577c478bd9Sstevel@tonic-gate 			while (next_osp && !next_osp->os_valid) {
16587c478bd9Sstevel@tonic-gate 				tmp_osp =
16597c478bd9Sstevel@tonic-gate 				    list_next(&rp->r_open_streams, next_osp);
16607c478bd9Sstevel@tonic-gate 				mutex_exit(&next_osp->os_sync_lock);
16617c478bd9Sstevel@tonic-gate 				next_osp = tmp_osp;
16627c478bd9Sstevel@tonic-gate 				if (next_osp)
16637c478bd9Sstevel@tonic-gate 					mutex_enter(&next_osp->os_sync_lock);
16647c478bd9Sstevel@tonic-gate 			}
16657c478bd9Sstevel@tonic-gate 			if (next_osp) {
16667c478bd9Sstevel@tonic-gate 				next_osp->os_ref_count++;
16677c478bd9Sstevel@tonic-gate 				mutex_exit(&next_osp->os_sync_lock);
16687c478bd9Sstevel@tonic-gate 			}
16697c478bd9Sstevel@tonic-gate 		}
16707c478bd9Sstevel@tonic-gate 		mutex_exit(&rp->r_os_lock);
16717c478bd9Sstevel@tonic-gate 		if (delay_rele)
16727c478bd9Sstevel@tonic-gate 			open_stream_rele(*osp, rp);
16737c478bd9Sstevel@tonic-gate 	}
16747c478bd9Sstevel@tonic-gate 
16757c478bd9Sstevel@tonic-gate 	if (next_osp) {
16767c478bd9Sstevel@tonic-gate 		nfs4_open_owner_t *oop;
16777c478bd9Sstevel@tonic-gate 
16787c478bd9Sstevel@tonic-gate 		oop = next_osp->os_open_owner;
16797c478bd9Sstevel@tonic-gate 		mutex_enter(&oop->oo_lock);
16807c478bd9Sstevel@tonic-gate 		if (oop->oo_cred_otw)
16817c478bd9Sstevel@tonic-gate 			ret_cr = oop->oo_cred_otw;
16827c478bd9Sstevel@tonic-gate 		else
16837c478bd9Sstevel@tonic-gate 			ret_cr = cr;
16847c478bd9Sstevel@tonic-gate 		crhold(ret_cr);
16857c478bd9Sstevel@tonic-gate 		mutex_exit(&oop->oo_lock);
16867c478bd9Sstevel@tonic-gate 		if (*first_time) {
16877c478bd9Sstevel@tonic-gate 			open_stream_rele(next_osp, rp);
16887c478bd9Sstevel@tonic-gate 			*osp = NULL;
16897c478bd9Sstevel@tonic-gate 		} else
16907c478bd9Sstevel@tonic-gate 			*osp = next_osp;
16917c478bd9Sstevel@tonic-gate 	} else {
16927c478bd9Sstevel@tonic-gate 		/* just return the cred provided to us */
1693479626d4SGerald Thornbrugh 		*last_time = TRUE;
16947c478bd9Sstevel@tonic-gate 		*osp = NULL;
16957c478bd9Sstevel@tonic-gate 		ret_cr = cr;
16967c478bd9Sstevel@tonic-gate 		crhold(ret_cr);
16977c478bd9Sstevel@tonic-gate 	}
16987c478bd9Sstevel@tonic-gate 
1699479626d4SGerald Thornbrugh 	*first_time = FALSE;
17007c478bd9Sstevel@tonic-gate 	return (ret_cr);
17017c478bd9Sstevel@tonic-gate }
17027c478bd9Sstevel@tonic-gate 
17037c478bd9Sstevel@tonic-gate void
nfs4_init_stateid_types(nfs4_stateid_types_t * sid_tp)17047c478bd9Sstevel@tonic-gate nfs4_init_stateid_types(nfs4_stateid_types_t *sid_tp)
17057c478bd9Sstevel@tonic-gate {
17067c478bd9Sstevel@tonic-gate 	bzero(&sid_tp->d_sid, sizeof (stateid4));
17077c478bd9Sstevel@tonic-gate 	bzero(&sid_tp->l_sid, sizeof (stateid4));
17087c478bd9Sstevel@tonic-gate 	bzero(&sid_tp->o_sid, sizeof (stateid4));
17097c478bd9Sstevel@tonic-gate 	sid_tp->cur_sid_type = NO_SID;
17107c478bd9Sstevel@tonic-gate }
17117c478bd9Sstevel@tonic-gate 
17127c478bd9Sstevel@tonic-gate void
nfs4_save_stateid(stateid4 * s1,nfs4_stateid_types_t * sid_tp)17137c478bd9Sstevel@tonic-gate nfs4_save_stateid(stateid4 *s1, nfs4_stateid_types_t *sid_tp)
17147c478bd9Sstevel@tonic-gate {
17157c478bd9Sstevel@tonic-gate 	NFS4_DEBUG(nfs4_client_recov_debug, (CE_NOTE,
17167c478bd9Sstevel@tonic-gate 	    "nfs4_save_stateid: saved %s stateid",
17177c478bd9Sstevel@tonic-gate 	    sid_tp->cur_sid_type == DEL_SID ? "delegation" :
17187c478bd9Sstevel@tonic-gate 	    sid_tp->cur_sid_type == LOCK_SID ? "lock" :
17197c478bd9Sstevel@tonic-gate 	    sid_tp->cur_sid_type == OPEN_SID ? "open" : "special"));
17207c478bd9Sstevel@tonic-gate 
17217c478bd9Sstevel@tonic-gate 	switch (sid_tp->cur_sid_type) {
17227c478bd9Sstevel@tonic-gate 	case DEL_SID:
17237c478bd9Sstevel@tonic-gate 		sid_tp->d_sid = *s1;
17247c478bd9Sstevel@tonic-gate 		break;
17257c478bd9Sstevel@tonic-gate 	case LOCK_SID:
17267c478bd9Sstevel@tonic-gate 		sid_tp->l_sid = *s1;
17277c478bd9Sstevel@tonic-gate 		break;
17287c478bd9Sstevel@tonic-gate 	case OPEN_SID:
17297c478bd9Sstevel@tonic-gate 		sid_tp->o_sid = *s1;
17307c478bd9Sstevel@tonic-gate 		break;
17317c478bd9Sstevel@tonic-gate 	case SPEC_SID:
17327c478bd9Sstevel@tonic-gate 	default:
17337c478bd9Sstevel@tonic-gate 		cmn_err(CE_PANIC, "nfs4_save_stateid: illegal "
17347c478bd9Sstevel@tonic-gate 		    "stateid type %d", sid_tp->cur_sid_type);
17357c478bd9Sstevel@tonic-gate 	}
17367c478bd9Sstevel@tonic-gate }
17377c478bd9Sstevel@tonic-gate 
17387c478bd9Sstevel@tonic-gate /*
17397c478bd9Sstevel@tonic-gate  * We got NFS4ERR_BAD_SEQID.  Setup some arguments to pass to recovery.
17407c478bd9Sstevel@tonic-gate  * Caller is responsible for freeing.
17417c478bd9Sstevel@tonic-gate  */
17427c478bd9Sstevel@tonic-gate nfs4_bseqid_entry_t *
nfs4_create_bseqid_entry(nfs4_open_owner_t * oop,nfs4_lock_owner_t * lop,vnode_t * vp,pid_t pid,nfs4_tag_type_t tag,seqid4 seqid)17437c478bd9Sstevel@tonic-gate nfs4_create_bseqid_entry(nfs4_open_owner_t *oop, nfs4_lock_owner_t *lop,
17447c478bd9Sstevel@tonic-gate     vnode_t *vp, pid_t pid, nfs4_tag_type_t tag, seqid4 seqid)
17457c478bd9Sstevel@tonic-gate {
17467c478bd9Sstevel@tonic-gate 	nfs4_bseqid_entry_t	*bsep;
17477c478bd9Sstevel@tonic-gate 
17487c478bd9Sstevel@tonic-gate 	bsep = kmem_alloc(sizeof (*bsep), KM_SLEEP);
17497c478bd9Sstevel@tonic-gate 	bsep->bs_oop = oop;
17507c478bd9Sstevel@tonic-gate 	bsep->bs_lop = lop;
17517c478bd9Sstevel@tonic-gate 	bsep->bs_vp = vp;
17527c478bd9Sstevel@tonic-gate 	bsep->bs_pid = pid;
17537c478bd9Sstevel@tonic-gate 	bsep->bs_tag = tag;
17547c478bd9Sstevel@tonic-gate 	bsep->bs_seqid = seqid;
17557c478bd9Sstevel@tonic-gate 
17567c478bd9Sstevel@tonic-gate 	return (bsep);
17577c478bd9Sstevel@tonic-gate }
17587c478bd9Sstevel@tonic-gate 
17597c478bd9Sstevel@tonic-gate void
nfs4open_dg_save_lost_rqst(int error,nfs4_lost_rqst_t * lost_rqstp,nfs4_open_owner_t * oop,nfs4_open_stream_t * osp,cred_t * cr,vnode_t * vp,int access_close,int deny_close)17607c478bd9Sstevel@tonic-gate nfs4open_dg_save_lost_rqst(int error, nfs4_lost_rqst_t *lost_rqstp,
1761957f6e72SMarcel Telka     nfs4_open_owner_t *oop, nfs4_open_stream_t *osp, cred_t *cr,
1762957f6e72SMarcel Telka     vnode_t *vp, int access_close, int deny_close)
17637c478bd9Sstevel@tonic-gate {
17647c478bd9Sstevel@tonic-gate 	lost_rqstp->lr_putfirst = FALSE;
17657c478bd9Sstevel@tonic-gate 
17667c478bd9Sstevel@tonic-gate 	ASSERT(vp != NULL);
17677c478bd9Sstevel@tonic-gate 	if (error == ETIMEDOUT || error == EINTR ||
17687c478bd9Sstevel@tonic-gate 	    NFS4_FRC_UNMT_ERR(error, vp->v_vfsp)) {
17697c478bd9Sstevel@tonic-gate 		NFS4_DEBUG(nfs4_lost_rqst_debug, (CE_NOTE,
1770bbf2a467SNagakiran Rajashekar 		    "nfs4open_dg_save_lost_rqst: error %d", error));
17717c478bd9Sstevel@tonic-gate 
17727c478bd9Sstevel@tonic-gate 		lost_rqstp->lr_op = OP_OPEN_DOWNGRADE;
17737c478bd9Sstevel@tonic-gate 		/*
17747c478bd9Sstevel@tonic-gate 		 * The vp is held and rele'd via the recovery code.
17757c478bd9Sstevel@tonic-gate 		 * See nfs4_save_lost_rqst.
17767c478bd9Sstevel@tonic-gate 		 */
17777c478bd9Sstevel@tonic-gate 		lost_rqstp->lr_vp = vp;
17787c478bd9Sstevel@tonic-gate 		lost_rqstp->lr_dvp = NULL;
17797c478bd9Sstevel@tonic-gate 		lost_rqstp->lr_oop = oop;
17807c478bd9Sstevel@tonic-gate 		lost_rqstp->lr_osp = osp;
17817c478bd9Sstevel@tonic-gate 		lost_rqstp->lr_lop = NULL;
17827c478bd9Sstevel@tonic-gate 		lost_rqstp->lr_cr = cr;
17837c478bd9Sstevel@tonic-gate 		lost_rqstp->lr_flk = NULL;
17847c478bd9Sstevel@tonic-gate 		lost_rqstp->lr_dg_acc = access_close;
17857c478bd9Sstevel@tonic-gate 		lost_rqstp->lr_dg_deny = deny_close;
17867c478bd9Sstevel@tonic-gate 		lost_rqstp->lr_putfirst = FALSE;
17877c478bd9Sstevel@tonic-gate 	} else {
17887c478bd9Sstevel@tonic-gate 		lost_rqstp->lr_op = 0;
17897c478bd9Sstevel@tonic-gate 	}
17907c478bd9Sstevel@tonic-gate }
17917c478bd9Sstevel@tonic-gate 
17927c478bd9Sstevel@tonic-gate /*
17937c478bd9Sstevel@tonic-gate  * Change the access and deny bits of an OPEN.
17947c478bd9Sstevel@tonic-gate  * If recovery is needed, *recov_credpp is set to the cred used OTW,
17957c478bd9Sstevel@tonic-gate  * a hold is placed on it, and *recov_seqidp is set to the seqid used OTW.
17967c478bd9Sstevel@tonic-gate  */
17977c478bd9Sstevel@tonic-gate void
nfs4_open_downgrade(int access_close,int deny_close,nfs4_open_owner_t * oop,nfs4_open_stream_t * osp,vnode_t * vp,cred_t * cr,nfs4_lost_rqst_t * lrp,nfs4_error_t * ep,cred_t ** recov_credpp,seqid4 * recov_seqidp)17987c478bd9Sstevel@tonic-gate nfs4_open_downgrade(int access_close, int deny_close, nfs4_open_owner_t *oop,
1799957f6e72SMarcel Telka     nfs4_open_stream_t *osp, vnode_t *vp, cred_t *cr, nfs4_lost_rqst_t *lrp,
1800957f6e72SMarcel Telka     nfs4_error_t *ep, cred_t **recov_credpp, seqid4 *recov_seqidp)
18017c478bd9Sstevel@tonic-gate {
18027c478bd9Sstevel@tonic-gate 	mntinfo4_t		*mi;
18037c478bd9Sstevel@tonic-gate 	int			downgrade_acc, downgrade_deny;
18047c478bd9Sstevel@tonic-gate 	int			new_acc, new_deny;
18057c478bd9Sstevel@tonic-gate 	COMPOUND4args_clnt	args;
18067c478bd9Sstevel@tonic-gate 	COMPOUND4res_clnt	res;
18077c478bd9Sstevel@tonic-gate 	OPEN_DOWNGRADE4res	*odg_res;
18087c478bd9Sstevel@tonic-gate 	nfs_argop4		argop[3];
18097c478bd9Sstevel@tonic-gate 	nfs_resop4		*resop;
18107c478bd9Sstevel@tonic-gate 	rnode4_t		*rp;
18117c478bd9Sstevel@tonic-gate 	bool_t			needrecov = FALSE;
18127c478bd9Sstevel@tonic-gate 	int			doqueue = 1;
18137c478bd9Sstevel@tonic-gate 	seqid4			seqid = 0;
18147c478bd9Sstevel@tonic-gate 	cred_t			*cred_otw;
18157c478bd9Sstevel@tonic-gate 	hrtime_t		t;
18167c478bd9Sstevel@tonic-gate 
18177c478bd9Sstevel@tonic-gate 	ASSERT(mutex_owned(&osp->os_sync_lock));
18187c478bd9Sstevel@tonic-gate #if DEBUG
18197c478bd9Sstevel@tonic-gate 	mutex_enter(&oop->oo_lock);
18207c478bd9Sstevel@tonic-gate 	ASSERT(oop->oo_seqid_inuse);
18217c478bd9Sstevel@tonic-gate 	mutex_exit(&oop->oo_lock);
18227c478bd9Sstevel@tonic-gate #endif
18237c478bd9Sstevel@tonic-gate 
18247c478bd9Sstevel@tonic-gate 
18257c478bd9Sstevel@tonic-gate 	if (access_close == 0 && deny_close == 0) {
18267c478bd9Sstevel@tonic-gate 		nfs4_error_zinit(ep);
18277c478bd9Sstevel@tonic-gate 		return;
18287c478bd9Sstevel@tonic-gate 	}
18297c478bd9Sstevel@tonic-gate 
18307c478bd9Sstevel@tonic-gate 	cred_otw = nfs4_get_otw_cred(cr, VTOMI4(vp), oop);
18317c478bd9Sstevel@tonic-gate 
18327c478bd9Sstevel@tonic-gate cred_retry:
18337c478bd9Sstevel@tonic-gate 	nfs4_error_zinit(ep);
18347c478bd9Sstevel@tonic-gate 	downgrade_acc = 0;
18357c478bd9Sstevel@tonic-gate 	downgrade_deny = 0;
18367c478bd9Sstevel@tonic-gate 	mi = VTOMI4(vp);
18377c478bd9Sstevel@tonic-gate 	rp = VTOR4(vp);
18387c478bd9Sstevel@tonic-gate 
18397c478bd9Sstevel@tonic-gate 	/*
18407c478bd9Sstevel@tonic-gate 	 * Check to see if the open stream got closed before we go OTW,
18417c478bd9Sstevel@tonic-gate 	 * now that we have acquired the 'os_sync_lock'.
18427c478bd9Sstevel@tonic-gate 	 */
18437c478bd9Sstevel@tonic-gate 	if (!osp->os_valid) {
18447c478bd9Sstevel@tonic-gate 		NFS4_DEBUG(nfs4_client_open_dg, (CE_NOTE, "nfs4_open_downgrade:"
18457c478bd9Sstevel@tonic-gate 		    " open stream has already been closed, return success"));
18467c478bd9Sstevel@tonic-gate 		/* error has already been set */
18477c478bd9Sstevel@tonic-gate 		goto no_args_out;
18487c478bd9Sstevel@tonic-gate 	}
18497c478bd9Sstevel@tonic-gate 
18507c478bd9Sstevel@tonic-gate 	/* If the file failed recovery, just quit. */
18517c478bd9Sstevel@tonic-gate 	mutex_enter(&rp->r_statelock);
18527c478bd9Sstevel@tonic-gate 	if (rp->r_flags & R4RECOVERR) {
18537c478bd9Sstevel@tonic-gate 		mutex_exit(&rp->r_statelock);
18547c478bd9Sstevel@tonic-gate 		ep->error = EIO;
18557c478bd9Sstevel@tonic-gate 		goto no_args_out;
18567c478bd9Sstevel@tonic-gate 	}
18577c478bd9Sstevel@tonic-gate 	mutex_exit(&rp->r_statelock);
18587c478bd9Sstevel@tonic-gate 
18597c478bd9Sstevel@tonic-gate 	seqid = nfs4_get_open_seqid(oop) + 1;
18607c478bd9Sstevel@tonic-gate 
18617c478bd9Sstevel@tonic-gate 	NFS4_DEBUG(nfs4_client_open_dg, (CE_NOTE, "nfs4_open_downgrade:"
18627c478bd9Sstevel@tonic-gate 	    "access_close %d, acc_read %"PRIu64" acc_write %"PRIu64"",
18637c478bd9Sstevel@tonic-gate 	    access_close, osp->os_share_acc_read, osp->os_share_acc_write));
18647c478bd9Sstevel@tonic-gate 
18657c478bd9Sstevel@tonic-gate 	/* If we're closing the last READ, need to downgrade */
18667c478bd9Sstevel@tonic-gate 	if ((access_close & FREAD) && (osp->os_share_acc_read == 1))
18677c478bd9Sstevel@tonic-gate 		downgrade_acc |= OPEN4_SHARE_ACCESS_READ;
18687c478bd9Sstevel@tonic-gate 
18697c478bd9Sstevel@tonic-gate 	/* if we're closing the last WRITE, need to downgrade */
18707c478bd9Sstevel@tonic-gate 	if ((access_close & FWRITE) && (osp->os_share_acc_write == 1))
18717c478bd9Sstevel@tonic-gate 		downgrade_acc |= OPEN4_SHARE_ACCESS_WRITE;
18727c478bd9Sstevel@tonic-gate 
18737c478bd9Sstevel@tonic-gate 	downgrade_deny = OPEN4_SHARE_DENY_NONE;
18747c478bd9Sstevel@tonic-gate 
18757c478bd9Sstevel@tonic-gate 	new_acc = 0;
18767c478bd9Sstevel@tonic-gate 	new_deny = 0;
18777c478bd9Sstevel@tonic-gate 
18787c478bd9Sstevel@tonic-gate 	/* set our new access and deny share bits */
18797c478bd9Sstevel@tonic-gate 	if ((osp->os_share_acc_read > 0) &&
18807c478bd9Sstevel@tonic-gate 	    !(downgrade_acc & OPEN4_SHARE_ACCESS_READ))
18817c478bd9Sstevel@tonic-gate 		new_acc |= OPEN4_SHARE_ACCESS_READ;
18827c478bd9Sstevel@tonic-gate 	if ((osp->os_share_acc_write > 0) &&
18837c478bd9Sstevel@tonic-gate 	    !(downgrade_acc & OPEN4_SHARE_ACCESS_WRITE))
18847c478bd9Sstevel@tonic-gate 		new_acc |= OPEN4_SHARE_ACCESS_WRITE;
18857c478bd9Sstevel@tonic-gate 
18867c478bd9Sstevel@tonic-gate 	new_deny = OPEN4_SHARE_DENY_NONE;
18877c478bd9Sstevel@tonic-gate 
18887c478bd9Sstevel@tonic-gate 	NFS4_DEBUG(nfs4_client_open_dg, (CE_NOTE, "nfs4_open_downgrade:"
18897c478bd9Sstevel@tonic-gate 	    "downgrade acc 0x%x deny 0x%x", downgrade_acc, downgrade_deny));
18907c478bd9Sstevel@tonic-gate 	NFS4_DEBUG(nfs4_client_open_dg, (CE_NOTE, "nfs4_open_downgrade:"
18917c478bd9Sstevel@tonic-gate 	    "new acc 0x%x deny 0x%x", new_acc, new_deny));
18927c478bd9Sstevel@tonic-gate 
18937c478bd9Sstevel@tonic-gate 	/*
18947c478bd9Sstevel@tonic-gate 	 * Check to see if we aren't actually doing any downgrade or
18957c478bd9Sstevel@tonic-gate 	 * if this is the last 'close' but the file is still mmapped.
18967c478bd9Sstevel@tonic-gate 	 * Skip this if this a lost request resend so we don't decrement
18977c478bd9Sstevel@tonic-gate 	 * the osp's share counts more than once.
18987c478bd9Sstevel@tonic-gate 	 */
18997c478bd9Sstevel@tonic-gate 	if (!lrp &&
19007c478bd9Sstevel@tonic-gate 	    ((downgrade_acc == 0 && downgrade_deny == 0) ||
19017c478bd9Sstevel@tonic-gate 	    (new_acc == 0 && new_deny == 0))) {
19027c478bd9Sstevel@tonic-gate 		/*
19037c478bd9Sstevel@tonic-gate 		 * No downgrade to do, but still need to
19047c478bd9Sstevel@tonic-gate 		 * update osp's os_share_* counts.
19057c478bd9Sstevel@tonic-gate 		 */
19067c478bd9Sstevel@tonic-gate 		NFS4_DEBUG(nfs4_client_open_dg, (CE_NOTE,
19077c478bd9Sstevel@tonic-gate 		    "nfs4_open_downgrade: just lower the osp's count by %s",
19087c478bd9Sstevel@tonic-gate 		    (access_close & FREAD) && (access_close & FWRITE) ?
19097c478bd9Sstevel@tonic-gate 		    "read and write" : (access_close & FREAD) ? "read" :
19107c478bd9Sstevel@tonic-gate 		    (access_close & FWRITE) ? "write" : "bogus"));
19117c478bd9Sstevel@tonic-gate 		if (access_close & FREAD)
19127c478bd9Sstevel@tonic-gate 			osp->os_share_acc_read--;
19137c478bd9Sstevel@tonic-gate 		if (access_close & FWRITE)
19147c478bd9Sstevel@tonic-gate 			osp->os_share_acc_write--;
19157c478bd9Sstevel@tonic-gate 		osp->os_share_deny_none--;
19167c478bd9Sstevel@tonic-gate 		nfs4_error_zinit(ep);
19177c478bd9Sstevel@tonic-gate 
19187c478bd9Sstevel@tonic-gate 		goto no_args_out;
19197c478bd9Sstevel@tonic-gate 	}
19207c478bd9Sstevel@tonic-gate 
19217c478bd9Sstevel@tonic-gate 	if (osp->os_orig_oo_name != oop->oo_name) {
19227c478bd9Sstevel@tonic-gate 		ep->error = EIO;
19237c478bd9Sstevel@tonic-gate 		goto no_args_out;
19247c478bd9Sstevel@tonic-gate 	}
19257c478bd9Sstevel@tonic-gate 
19267c478bd9Sstevel@tonic-gate 	/* setup the COMPOUND args */
19277c478bd9Sstevel@tonic-gate 	if (lrp)
19287c478bd9Sstevel@tonic-gate 		args.ctag = TAG_OPEN_DG_LOST;
19297c478bd9Sstevel@tonic-gate 	else
19307c478bd9Sstevel@tonic-gate 		args.ctag = TAG_OPEN_DG;
19317c478bd9Sstevel@tonic-gate 
19327c478bd9Sstevel@tonic-gate 	args.array_len = 3;
19337c478bd9Sstevel@tonic-gate 	args.array = argop;
19347c478bd9Sstevel@tonic-gate 
19357c478bd9Sstevel@tonic-gate 	/* putfh */
19367c478bd9Sstevel@tonic-gate 	argop[0].argop = OP_CPUTFH;
19377c478bd9Sstevel@tonic-gate 	argop[0].nfs_argop4_u.opcputfh.sfh = rp->r_fh;
19387c478bd9Sstevel@tonic-gate 
19397c478bd9Sstevel@tonic-gate 	argop[1].argop = OP_GETATTR;
19407c478bd9Sstevel@tonic-gate 	argop[1].nfs_argop4_u.opgetattr.attr_request = NFS4_VATTR_MASK;
19417c478bd9Sstevel@tonic-gate 	argop[1].nfs_argop4_u.opgetattr.mi = mi;
19427c478bd9Sstevel@tonic-gate 
19437c478bd9Sstevel@tonic-gate 	ASSERT(mutex_owned(&osp->os_sync_lock));
19447c478bd9Sstevel@tonic-gate 	ASSERT(osp->os_delegation == FALSE);
19457c478bd9Sstevel@tonic-gate 
19467c478bd9Sstevel@tonic-gate 	/* open downgrade */
19477c478bd9Sstevel@tonic-gate 	argop[2].argop = OP_OPEN_DOWNGRADE;
19487c478bd9Sstevel@tonic-gate 	argop[2].nfs_argop4_u.opopen_downgrade.open_stateid = osp->open_stateid;
19497c478bd9Sstevel@tonic-gate 	argop[2].nfs_argop4_u.opopen_downgrade.share_access = new_acc;
19507c478bd9Sstevel@tonic-gate 	argop[2].nfs_argop4_u.opopen_downgrade.share_deny = new_deny;
19517c478bd9Sstevel@tonic-gate 	argop[2].nfs_argop4_u.opopen_downgrade.seqid = seqid;
19527c478bd9Sstevel@tonic-gate 
19537c478bd9Sstevel@tonic-gate 	t = gethrtime();
19547c478bd9Sstevel@tonic-gate 
19557c478bd9Sstevel@tonic-gate 	rfs4call(mi, &args, &res, cred_otw, &doqueue, 0, ep);
19567c478bd9Sstevel@tonic-gate 
19577c478bd9Sstevel@tonic-gate 	if (ep->error == 0 && nfs4_need_to_bump_seqid(&res))
19587c478bd9Sstevel@tonic-gate 		nfs4_set_open_seqid(seqid, oop, args.ctag);
19597c478bd9Sstevel@tonic-gate 
19607c478bd9Sstevel@tonic-gate 	if ((ep->error == EACCES ||
19617c478bd9Sstevel@tonic-gate 	    (ep->error == 0 && res.status == NFS4ERR_ACCESS)) &&
19627c478bd9Sstevel@tonic-gate 	    cred_otw != cr) {
19637c478bd9Sstevel@tonic-gate 		crfree(cred_otw);
19647c478bd9Sstevel@tonic-gate 		cred_otw = cr;
19657c478bd9Sstevel@tonic-gate 		crhold(cred_otw);
19667c478bd9Sstevel@tonic-gate 		if (!ep->error)
1967a17ce845SMarcel Telka 			xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res);
19687c478bd9Sstevel@tonic-gate 		goto cred_retry;
19697c478bd9Sstevel@tonic-gate 	}
19707c478bd9Sstevel@tonic-gate 
19717c478bd9Sstevel@tonic-gate 	needrecov = nfs4_needs_recovery(ep, TRUE, mi->mi_vfsp);
19727c478bd9Sstevel@tonic-gate 
19737c478bd9Sstevel@tonic-gate 	if (needrecov && recov_credpp) {
19747c478bd9Sstevel@tonic-gate 		*recov_credpp = cred_otw;
19757c478bd9Sstevel@tonic-gate 		crhold(*recov_credpp);
19767c478bd9Sstevel@tonic-gate 		if (recov_seqidp)
19777c478bd9Sstevel@tonic-gate 			*recov_seqidp = seqid;
19787c478bd9Sstevel@tonic-gate 	}
19797c478bd9Sstevel@tonic-gate 
19807c478bd9Sstevel@tonic-gate 	if (!ep->error && !res.status) {
19817c478bd9Sstevel@tonic-gate 		/* get the open downgrade results */
19827c478bd9Sstevel@tonic-gate 		resop = &res.array[2];
19837c478bd9Sstevel@tonic-gate 		odg_res = &resop->nfs_resop4_u.opopen_downgrade;
19847c478bd9Sstevel@tonic-gate 
19857c478bd9Sstevel@tonic-gate 		osp->open_stateid = odg_res->open_stateid;
19867c478bd9Sstevel@tonic-gate 
19877c478bd9Sstevel@tonic-gate 		/* set the open streams new access/deny bits */
19887c478bd9Sstevel@tonic-gate 		if (access_close & FREAD)
19897c478bd9Sstevel@tonic-gate 			osp->os_share_acc_read--;
19907c478bd9Sstevel@tonic-gate 		if (access_close & FWRITE)
19917c478bd9Sstevel@tonic-gate 			osp->os_share_acc_write--;
19927c478bd9Sstevel@tonic-gate 		osp->os_share_deny_none--;
19937c478bd9Sstevel@tonic-gate 		osp->os_dc_openacc = new_acc;
19947c478bd9Sstevel@tonic-gate 
19957c478bd9Sstevel@tonic-gate 		nfs4_attr_cache(vp,
1996bbf2a467SNagakiran Rajashekar 		    &res.array[1].nfs_resop4_u.opgetattr.ga_res,
1997bbf2a467SNagakiran Rajashekar 		    t, cred_otw, TRUE, NULL);
19987c478bd9Sstevel@tonic-gate 	}
19997c478bd9Sstevel@tonic-gate 
20007c478bd9Sstevel@tonic-gate 	if (!ep->error)
2001a17ce845SMarcel Telka 		xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res);
20027c478bd9Sstevel@tonic-gate 
20037c478bd9Sstevel@tonic-gate no_args_out:
20047c478bd9Sstevel@tonic-gate 	crfree(cred_otw);
20057c478bd9Sstevel@tonic-gate }
20067c478bd9Sstevel@tonic-gate 
20077c478bd9Sstevel@tonic-gate /*
20087c478bd9Sstevel@tonic-gate  * If an OPEN request gets ETIMEDOUT or EINTR (that includes bailing out
20097c478bd9Sstevel@tonic-gate  * because the filesystem was forcibly unmounted) then we don't know if we
20107c478bd9Sstevel@tonic-gate  * potentially left state dangling on the server, therefore the recovery
20117c478bd9Sstevel@tonic-gate  * framework makes this call to resend the OPEN request and then undo it.
20127c478bd9Sstevel@tonic-gate  */
20137c478bd9Sstevel@tonic-gate void
nfs4_resend_open_otw(vnode_t ** vpp,nfs4_lost_rqst_t * resend_rqstp,nfs4_error_t * ep)20147c478bd9Sstevel@tonic-gate nfs4_resend_open_otw(vnode_t **vpp, nfs4_lost_rqst_t *resend_rqstp,
2015957f6e72SMarcel Telka     nfs4_error_t *ep)
20167c478bd9Sstevel@tonic-gate {
20177c478bd9Sstevel@tonic-gate 	COMPOUND4args_clnt	args;
20187c478bd9Sstevel@tonic-gate 	COMPOUND4res_clnt	res;
20197c478bd9Sstevel@tonic-gate 	nfs_argop4		argop[4];
20207c478bd9Sstevel@tonic-gate 	GETFH4res		*gf_res = NULL;
20217c478bd9Sstevel@tonic-gate 	OPEN4cargs		*open_args;
20227c478bd9Sstevel@tonic-gate 	OPEN4res		*op_res;
20237c478bd9Sstevel@tonic-gate 	char			*destcfp;
20247c478bd9Sstevel@tonic-gate 	int			destclen;
20257c478bd9Sstevel@tonic-gate 	nfs4_ga_res_t		*garp;
20267c478bd9Sstevel@tonic-gate 	vnode_t			*dvp = NULL, *vp = NULL;
20277c478bd9Sstevel@tonic-gate 	rnode4_t		*rp = NULL, *drp = NULL;
20287c478bd9Sstevel@tonic-gate 	cred_t			*cr = NULL;
20297c478bd9Sstevel@tonic-gate 	seqid4			seqid;
20307c478bd9Sstevel@tonic-gate 	nfs4_open_owner_t	*oop = NULL;
20317c478bd9Sstevel@tonic-gate 	nfs4_open_stream_t	*osp = NULL;
20327c478bd9Sstevel@tonic-gate 	component4		*srcfp;
20337c478bd9Sstevel@tonic-gate 	open_claim_type4	claim;
20347c478bd9Sstevel@tonic-gate 	mntinfo4_t		*mi;
20357c478bd9Sstevel@tonic-gate 	int			doqueue = 1;
20367c478bd9Sstevel@tonic-gate 	bool_t			retry_open = FALSE;
20377c478bd9Sstevel@tonic-gate 	int			created_osp = 0;
20387c478bd9Sstevel@tonic-gate 	hrtime_t		t;
2039*f2211ffeSMarcel Telka 	char			*failed_msg = "";
20407c478bd9Sstevel@tonic-gate 	int			fh_different;
2041852c68eeSjwahlig 	int			reopen = 0;
20427c478bd9Sstevel@tonic-gate 
20437c478bd9Sstevel@tonic-gate 	nfs4_error_zinit(ep);
20447c478bd9Sstevel@tonic-gate 
20457c478bd9Sstevel@tonic-gate 	cr = resend_rqstp->lr_cr;
20467c478bd9Sstevel@tonic-gate 	dvp = resend_rqstp->lr_dvp;
20477c478bd9Sstevel@tonic-gate 
20487c478bd9Sstevel@tonic-gate 	vp = *vpp;
20497c478bd9Sstevel@tonic-gate 	if (vp) {
20507c478bd9Sstevel@tonic-gate 		ASSERT(nfs4_consistent_type(vp));
20517c478bd9Sstevel@tonic-gate 		rp = VTOR4(vp);
20527c478bd9Sstevel@tonic-gate 	}
20537c478bd9Sstevel@tonic-gate 
20547c478bd9Sstevel@tonic-gate 	if (rp) {
20557c478bd9Sstevel@tonic-gate 		/* If the file failed recovery, just quit. */
20567c478bd9Sstevel@tonic-gate 		mutex_enter(&rp->r_statelock);
20577c478bd9Sstevel@tonic-gate 		if (rp->r_flags & R4RECOVERR) {
20587c478bd9Sstevel@tonic-gate 			mutex_exit(&rp->r_statelock);
20597c478bd9Sstevel@tonic-gate 			ep->error = EIO;
20607c478bd9Sstevel@tonic-gate 			return;
20617c478bd9Sstevel@tonic-gate 		}
20627c478bd9Sstevel@tonic-gate 		mutex_exit(&rp->r_statelock);
20637c478bd9Sstevel@tonic-gate 	}
20647c478bd9Sstevel@tonic-gate 
20657c478bd9Sstevel@tonic-gate 	if (dvp) {
20667c478bd9Sstevel@tonic-gate 		drp = VTOR4(dvp);
20677c478bd9Sstevel@tonic-gate 		/* If the parent directory failed recovery, just quit. */
20687c478bd9Sstevel@tonic-gate 		mutex_enter(&drp->r_statelock);
20697c478bd9Sstevel@tonic-gate 		if (drp->r_flags & R4RECOVERR) {
20707c478bd9Sstevel@tonic-gate 			mutex_exit(&drp->r_statelock);
20717c478bd9Sstevel@tonic-gate 			ep->error = EIO;
20727c478bd9Sstevel@tonic-gate 			return;
20737c478bd9Sstevel@tonic-gate 		}
20747c478bd9Sstevel@tonic-gate 		mutex_exit(&drp->r_statelock);
2075852c68eeSjwahlig 	} else
2076852c68eeSjwahlig 		reopen = 1;	/* NULL dvp means this is a reopen */
20777c478bd9Sstevel@tonic-gate 
20787c478bd9Sstevel@tonic-gate 	claim = resend_rqstp->lr_oclaim;
20797c478bd9Sstevel@tonic-gate 	ASSERT(claim == CLAIM_NULL || claim == CLAIM_DELEGATE_CUR);
20807c478bd9Sstevel@tonic-gate 
20817c478bd9Sstevel@tonic-gate 	args.ctag = TAG_OPEN_LOST;
20827c478bd9Sstevel@tonic-gate 	args.array_len = 4;
20837c478bd9Sstevel@tonic-gate 	args.array = argop;
20847c478bd9Sstevel@tonic-gate 
20857c478bd9Sstevel@tonic-gate 	argop[0].argop = OP_CPUTFH;
2086852c68eeSjwahlig 	if (reopen) {
20877c478bd9Sstevel@tonic-gate 		ASSERT(vp != NULL);
20887c478bd9Sstevel@tonic-gate 
20897c478bd9Sstevel@tonic-gate 		mi = VTOMI4(vp);
20907c478bd9Sstevel@tonic-gate 		/*
20917c478bd9Sstevel@tonic-gate 		 * if this is a file mount then
20927c478bd9Sstevel@tonic-gate 		 * use the mntinfo parentfh
20937c478bd9Sstevel@tonic-gate 		 */
20947c478bd9Sstevel@tonic-gate 		argop[0].nfs_argop4_u.opcputfh.sfh =
2095bbf2a467SNagakiran Rajashekar 		    (vp->v_flag & VROOT) ? mi->mi_srvparentfh :
2096bbf2a467SNagakiran Rajashekar 		    VTOSV(vp)->sv_dfh;
20977c478bd9Sstevel@tonic-gate 		args.ctag = TAG_REOPEN_LOST;
20987c478bd9Sstevel@tonic-gate 	} else {
20997c478bd9Sstevel@tonic-gate 		argop[0].nfs_argop4_u.opcputfh.sfh = VTOR4(dvp)->r_fh;
21007c478bd9Sstevel@tonic-gate 		mi = VTOMI4(dvp);
21017c478bd9Sstevel@tonic-gate 	}
21027c478bd9Sstevel@tonic-gate 
21037c478bd9Sstevel@tonic-gate 	argop[1].argop = OP_COPEN;
21047c478bd9Sstevel@tonic-gate 	open_args = &argop[1].nfs_argop4_u.opcopen;
21057c478bd9Sstevel@tonic-gate 	open_args->claim = claim;
21067c478bd9Sstevel@tonic-gate 
21077c478bd9Sstevel@tonic-gate 	/*
21087c478bd9Sstevel@tonic-gate 	 * If we sent over a OPEN with CREATE then the only
21097c478bd9Sstevel@tonic-gate 	 * thing we care about is to not leave dangling state
21107c478bd9Sstevel@tonic-gate 	 * on the server, not whether the file we potentially
21117c478bd9Sstevel@tonic-gate 	 * created remains on the server.  So even though the
21127c478bd9Sstevel@tonic-gate 	 * lost open request specified a CREATE, we only wish
21137c478bd9Sstevel@tonic-gate 	 * to do a non-CREATE OPEN.
21147c478bd9Sstevel@tonic-gate 	 */
21157c478bd9Sstevel@tonic-gate 	open_args->opentype = OPEN4_NOCREATE;
21167c478bd9Sstevel@tonic-gate 
21177c478bd9Sstevel@tonic-gate 	srcfp = &resend_rqstp->lr_ofile;
21187c478bd9Sstevel@tonic-gate 	destclen = srcfp->utf8string_len;
21197c478bd9Sstevel@tonic-gate 	destcfp = kmem_alloc(destclen + 1, KM_SLEEP);
21207c478bd9Sstevel@tonic-gate 	bcopy(srcfp->utf8string_val, destcfp, destclen);
21217c478bd9Sstevel@tonic-gate 	destcfp[destclen] = '\0';
21227c478bd9Sstevel@tonic-gate 	if (claim == CLAIM_DELEGATE_CUR) {
21237c478bd9Sstevel@tonic-gate 		open_args->open_claim4_u.delegate_cur_info.delegate_stateid =
2124bbf2a467SNagakiran Rajashekar 		    resend_rqstp->lr_ostateid;
21257c478bd9Sstevel@tonic-gate 		open_args->open_claim4_u.delegate_cur_info.cfile = destcfp;
21267c478bd9Sstevel@tonic-gate 	} else {
21277c478bd9Sstevel@tonic-gate 		open_args->open_claim4_u.cfile = destcfp;
21287c478bd9Sstevel@tonic-gate 	}
21297c478bd9Sstevel@tonic-gate 
21307c478bd9Sstevel@tonic-gate 	open_args->share_access = resend_rqstp->lr_oacc;
21317c478bd9Sstevel@tonic-gate 	open_args->share_deny = resend_rqstp->lr_odeny;
21327c478bd9Sstevel@tonic-gate 	oop = resend_rqstp->lr_oop;
21337c478bd9Sstevel@tonic-gate 	ASSERT(oop != NULL);
21347c478bd9Sstevel@tonic-gate 
21357c478bd9Sstevel@tonic-gate 	open_args->owner.clientid = mi2clientid(mi);
21367c478bd9Sstevel@tonic-gate 	/* this length never changes */
21377c478bd9Sstevel@tonic-gate 	open_args->owner.owner_len = sizeof (oop->oo_name);
21387c478bd9Sstevel@tonic-gate 	open_args->owner.owner_val =
21397c478bd9Sstevel@tonic-gate 	    kmem_alloc(open_args->owner.owner_len, KM_SLEEP);
21407c478bd9Sstevel@tonic-gate 
21417c478bd9Sstevel@tonic-gate 	ep->error = nfs4_start_open_seqid_sync(oop, mi);
21427c478bd9Sstevel@tonic-gate 	ASSERT(ep->error == 0);		/* recov thread always succeeds */
21437c478bd9Sstevel@tonic-gate 	/*
21447c478bd9Sstevel@tonic-gate 	 * We can get away with not saving the seqid upon detection
21457c478bd9Sstevel@tonic-gate 	 * of a lost request, and now just use the open owner's current
21467c478bd9Sstevel@tonic-gate 	 * seqid since we only allow one op OTW per seqid and lost
21477c478bd9Sstevel@tonic-gate 	 * requests are saved FIFO.
21487c478bd9Sstevel@tonic-gate 	 */
21497c478bd9Sstevel@tonic-gate 	seqid = nfs4_get_open_seqid(oop) + 1;
21507c478bd9Sstevel@tonic-gate 	open_args->seqid = seqid;
21517c478bd9Sstevel@tonic-gate 
21527c478bd9Sstevel@tonic-gate 	bcopy(&oop->oo_name, open_args->owner.owner_val,
21537c478bd9Sstevel@tonic-gate 	    open_args->owner.owner_len);
21547c478bd9Sstevel@tonic-gate 
21557c478bd9Sstevel@tonic-gate 	/* getfh */
21567c478bd9Sstevel@tonic-gate 	argop[2].argop = OP_GETFH;
21577c478bd9Sstevel@tonic-gate 
21587c478bd9Sstevel@tonic-gate 	/* Construct the getattr part of the compound */
21597c478bd9Sstevel@tonic-gate 	argop[3].argop = OP_GETATTR;
21607c478bd9Sstevel@tonic-gate 	argop[3].nfs_argop4_u.opgetattr.attr_request = NFS4_VATTR_MASK;
21617c478bd9Sstevel@tonic-gate 	argop[3].nfs_argop4_u.opgetattr.mi = mi;
21627c478bd9Sstevel@tonic-gate 
21637c478bd9Sstevel@tonic-gate 	res.array = NULL;
21647c478bd9Sstevel@tonic-gate 
21657c478bd9Sstevel@tonic-gate 	t = gethrtime();
21667c478bd9Sstevel@tonic-gate 
21677c478bd9Sstevel@tonic-gate 	rfs4call(mi, &args, &res, cr, &doqueue, 0, ep);
21687c478bd9Sstevel@tonic-gate 
21697c478bd9Sstevel@tonic-gate 	if (ep->error == 0 && nfs4_need_to_bump_seqid(&res))
21707c478bd9Sstevel@tonic-gate 		nfs4_set_open_seqid(seqid, oop, args.ctag);
21717c478bd9Sstevel@tonic-gate 
21727c478bd9Sstevel@tonic-gate 	NFS4_DEBUG(nfs4_lost_rqst_debug, (CE_NOTE,
21737c478bd9Sstevel@tonic-gate 	    "nfs4_resend_open_otw: error %d stat %d", ep->error, res.status));
21747c478bd9Sstevel@tonic-gate 
21757c478bd9Sstevel@tonic-gate 	if (ep->error || res.status)
21767c478bd9Sstevel@tonic-gate 		goto err_out;
21777c478bd9Sstevel@tonic-gate 
21787c478bd9Sstevel@tonic-gate 	op_res = &res.array[1].nfs_resop4_u.opopen;
21797c478bd9Sstevel@tonic-gate 	gf_res = &res.array[2].nfs_resop4_u.opgetfh;
21807c478bd9Sstevel@tonic-gate 	garp = &res.array[3].nfs_resop4_u.opgetattr.ga_res;
21817c478bd9Sstevel@tonic-gate 
21827c478bd9Sstevel@tonic-gate 	if (!vp) {
21837c478bd9Sstevel@tonic-gate 		int rnode_err = 0;
21847c478bd9Sstevel@tonic-gate 		nfs4_sharedfh_t *sfh;
21857c478bd9Sstevel@tonic-gate 
21867c478bd9Sstevel@tonic-gate 		/*
21877c478bd9Sstevel@tonic-gate 		 * If we can't decode all the attributes they are not usable,
21887c478bd9Sstevel@tonic-gate 		 * just make the vnode.
21897c478bd9Sstevel@tonic-gate 		 */
21907c478bd9Sstevel@tonic-gate 
21917c478bd9Sstevel@tonic-gate 		sfh = sfh4_get(&gf_res->object, VTOMI4(dvp));
21927c478bd9Sstevel@tonic-gate 		*vpp = makenfs4node(sfh, garp, dvp->v_vfsp, t, cr, dvp,
2193bbf2a467SNagakiran Rajashekar 		    fn_get(VTOSV(dvp)->sv_name,
2194bbf2a467SNagakiran Rajashekar 		    open_args->open_claim4_u.cfile, sfh));
21957c478bd9Sstevel@tonic-gate 		sfh4_rele(&sfh);
21967c478bd9Sstevel@tonic-gate 		NFS4_DEBUG(nfs4_lost_rqst_debug, (CE_NOTE,
21977c478bd9Sstevel@tonic-gate 		    "nfs4_resend_open_otw: made vp %p for file %s",
21987c478bd9Sstevel@tonic-gate 		    (void *)(*vpp), open_args->open_claim4_u.cfile));
21997c478bd9Sstevel@tonic-gate 
22007c478bd9Sstevel@tonic-gate 		if (ep->error)
22017c478bd9Sstevel@tonic-gate 			PURGE_ATTRCACHE4(*vpp);
22027c478bd9Sstevel@tonic-gate 
22037c478bd9Sstevel@tonic-gate 		/*
22047c478bd9Sstevel@tonic-gate 		 * For the newly created *vpp case, make sure the rnode
22057c478bd9Sstevel@tonic-gate 		 * isn't bad before using it.
22067c478bd9Sstevel@tonic-gate 		 */
22077c478bd9Sstevel@tonic-gate 		mutex_enter(&(VTOR4(*vpp))->r_statelock);
22087c478bd9Sstevel@tonic-gate 		if (VTOR4(*vpp)->r_flags & R4RECOVERR)
22097c478bd9Sstevel@tonic-gate 			rnode_err = EIO;
22107c478bd9Sstevel@tonic-gate 		mutex_exit(&(VTOR4(*vpp))->r_statelock);
22117c478bd9Sstevel@tonic-gate 
22127c478bd9Sstevel@tonic-gate 		if (rnode_err) {
22137c478bd9Sstevel@tonic-gate 			NFS4_DEBUG(nfs4_lost_rqst_debug, (CE_NOTE,
22147c478bd9Sstevel@tonic-gate 			    "nfs4_resend_open_otw: rp %p is bad",
22157c478bd9Sstevel@tonic-gate 			    (void *)VTOR4(*vpp)));
22167c478bd9Sstevel@tonic-gate 			ep->error = rnode_err;
22177c478bd9Sstevel@tonic-gate 			goto err_out;
22187c478bd9Sstevel@tonic-gate 		}
22197c478bd9Sstevel@tonic-gate 
22207c478bd9Sstevel@tonic-gate 		vp = *vpp;
22217c478bd9Sstevel@tonic-gate 		rp = VTOR4(vp);
22227c478bd9Sstevel@tonic-gate 	}
22237c478bd9Sstevel@tonic-gate 
2224852c68eeSjwahlig 	if (reopen) {
22257c478bd9Sstevel@tonic-gate 		/*
22267c478bd9Sstevel@tonic-gate 		 * Check if the path we reopened really is the same
22277c478bd9Sstevel@tonic-gate 		 * file. We could end up in a situation were the file
22287c478bd9Sstevel@tonic-gate 		 * was removed and a new file created with the same name.
22297c478bd9Sstevel@tonic-gate 		 */
22307c478bd9Sstevel@tonic-gate 		(void) nfs_rw_enter_sig(&mi->mi_fh_lock, RW_READER, 0);
22317c478bd9Sstevel@tonic-gate 		fh_different =
2232bbf2a467SNagakiran Rajashekar 		    (nfs4cmpfh(&rp->r_fh->sfh_fh, &gf_res->object) != 0);
22337c478bd9Sstevel@tonic-gate 		if (fh_different) {
22347c478bd9Sstevel@tonic-gate 			if (mi->mi_fh_expire_type == FH4_PERSISTENT ||
22357c478bd9Sstevel@tonic-gate 			    mi->mi_fh_expire_type & FH4_NOEXPIRE_WITH_OPEN) {
22367c478bd9Sstevel@tonic-gate 				/* Oops, we don't have the same file */
22377c478bd9Sstevel@tonic-gate 				if (mi->mi_fh_expire_type == FH4_PERSISTENT)
22387c478bd9Sstevel@tonic-gate 					failed_msg =
22397c478bd9Sstevel@tonic-gate 					    "Couldn't reopen: Persistant "
22407c478bd9Sstevel@tonic-gate 					    "file handle changed";
22417c478bd9Sstevel@tonic-gate 				else
22427c478bd9Sstevel@tonic-gate 					failed_msg =
22437c478bd9Sstevel@tonic-gate 					    "Couldn't reopen: Volatile "
22447c478bd9Sstevel@tonic-gate 					    "(no expire on open) file handle "
22457c478bd9Sstevel@tonic-gate 					    "changed";
22467c478bd9Sstevel@tonic-gate 
22477c478bd9Sstevel@tonic-gate 				nfs4_end_open_seqid_sync(oop);
22487c478bd9Sstevel@tonic-gate 				kmem_free(destcfp, destclen + 1);
22497c478bd9Sstevel@tonic-gate 				nfs4args_copen_free(open_args);
2250a17ce845SMarcel Telka 				xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res);
22517c478bd9Sstevel@tonic-gate 				nfs_rw_exit(&mi->mi_fh_lock);
22527c478bd9Sstevel@tonic-gate 				nfs4_fail_recov(vp, failed_msg, ep->error,
2253bbf2a467SNagakiran Rajashekar 				    ep->stat);
22547c478bd9Sstevel@tonic-gate 				return;
22557c478bd9Sstevel@tonic-gate 			} else {
22567c478bd9Sstevel@tonic-gate 				/*
22577c478bd9Sstevel@tonic-gate 				 * We have volatile file handles that don't
22587c478bd9Sstevel@tonic-gate 				 * compare.  If the fids are the same then we
22597c478bd9Sstevel@tonic-gate 				 * assume that the file handle expired but the
22607c478bd9Sstevel@tonic-gate 				 * renode still refers to the same file object.
22617c478bd9Sstevel@tonic-gate 				 *
22627c478bd9Sstevel@tonic-gate 				 * First check that we have fids or not.
22637c478bd9Sstevel@tonic-gate 				 * If we don't we have a dumb server so we will
22647c478bd9Sstevel@tonic-gate 				 * just assume every thing is ok for now.
22657c478bd9Sstevel@tonic-gate 				 */
22667c478bd9Sstevel@tonic-gate 				if (!ep->error &&
22677c478bd9Sstevel@tonic-gate 				    garp->n4g_va.va_mask & AT_NODEID &&
22687c478bd9Sstevel@tonic-gate 				    rp->r_attr.va_mask & AT_NODEID &&
22697c478bd9Sstevel@tonic-gate 				    rp->r_attr.va_nodeid !=
2270bbf2a467SNagakiran Rajashekar 				    garp->n4g_va.va_nodeid) {
22717c478bd9Sstevel@tonic-gate 					/*
22727c478bd9Sstevel@tonic-gate 					 * We have fids, but they don't
22737c478bd9Sstevel@tonic-gate 					 * compare. So kill the file.
22747c478bd9Sstevel@tonic-gate 					 */
22757c478bd9Sstevel@tonic-gate 					failed_msg =
22767c478bd9Sstevel@tonic-gate 					    "Couldn't reopen: file handle "
22777c478bd9Sstevel@tonic-gate 					    "changed due to mismatched fids";
22787c478bd9Sstevel@tonic-gate 					nfs4_end_open_seqid_sync(oop);
22797c478bd9Sstevel@tonic-gate 					kmem_free(destcfp, destclen + 1);
22807c478bd9Sstevel@tonic-gate 					nfs4args_copen_free(open_args);
2281a17ce845SMarcel Telka 					xdr_free(xdr_COMPOUND4res_clnt,
2282bbf2a467SNagakiran Rajashekar 					    (caddr_t)&res);
22837c478bd9Sstevel@tonic-gate 					nfs_rw_exit(&mi->mi_fh_lock);
22847c478bd9Sstevel@tonic-gate 					nfs4_fail_recov(vp, failed_msg,
2285bbf2a467SNagakiran Rajashekar 					    ep->error, ep->stat);
22867c478bd9Sstevel@tonic-gate 					return;
22877c478bd9Sstevel@tonic-gate 				} else {
22887c478bd9Sstevel@tonic-gate 					/*
22897c478bd9Sstevel@tonic-gate 					 * We have volatile file handles that
22907c478bd9Sstevel@tonic-gate 					 * refers to the same file (at least
22917c478bd9Sstevel@tonic-gate 					 * they have the same fid) or we don't
22927c478bd9Sstevel@tonic-gate 					 * have fids so we can't tell. :(. We'll
22937c478bd9Sstevel@tonic-gate 					 * be a kind and accepting client so
22947c478bd9Sstevel@tonic-gate 					 * we'll update the rnode's file
22957c478bd9Sstevel@tonic-gate 					 * handle with the otw handle.
22967c478bd9Sstevel@tonic-gate 					 *
22977c478bd9Sstevel@tonic-gate 					 * We need to drop mi->mi_fh_lock since
22987c478bd9Sstevel@tonic-gate 					 * sh4_update acquires it. Since there
22997c478bd9Sstevel@tonic-gate 					 * is only one recovery thread there is
23007c478bd9Sstevel@tonic-gate 					 * no race.
23017c478bd9Sstevel@tonic-gate 					 */
23027c478bd9Sstevel@tonic-gate 					nfs_rw_exit(&mi->mi_fh_lock);
23037c478bd9Sstevel@tonic-gate 					sfh4_update(rp->r_fh, &gf_res->object);
23047c478bd9Sstevel@tonic-gate 				}
23057c478bd9Sstevel@tonic-gate 			}
23067c478bd9Sstevel@tonic-gate 		} else {
23077c478bd9Sstevel@tonic-gate 			nfs_rw_exit(&mi->mi_fh_lock);
23087c478bd9Sstevel@tonic-gate 		}
23097c478bd9Sstevel@tonic-gate 	}
23107c478bd9Sstevel@tonic-gate 
23117c478bd9Sstevel@tonic-gate 	ASSERT(nfs4_consistent_type(vp));
23127c478bd9Sstevel@tonic-gate 
23137c478bd9Sstevel@tonic-gate 	if (op_res->rflags & OPEN4_RESULT_CONFIRM)
23147c478bd9Sstevel@tonic-gate 		nfs4open_confirm(vp, &seqid, &op_res->stateid, cr, TRUE,
23157c478bd9Sstevel@tonic-gate 		    &retry_open, oop, TRUE, ep, NULL);
23167c478bd9Sstevel@tonic-gate 	if (ep->error || ep->stat) {
23177c478bd9Sstevel@tonic-gate 		nfs4_end_open_seqid_sync(oop);
23187c478bd9Sstevel@tonic-gate 		kmem_free(destcfp, destclen + 1);
23197c478bd9Sstevel@tonic-gate 		nfs4args_copen_free(open_args);
23207c478bd9Sstevel@tonic-gate 		if (!ep->error)
2321a17ce845SMarcel Telka 			xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res);
23227c478bd9Sstevel@tonic-gate 		return;
23237c478bd9Sstevel@tonic-gate 	}
23247c478bd9Sstevel@tonic-gate 
2325852c68eeSjwahlig 	if (reopen) {
23267c478bd9Sstevel@tonic-gate 		/*
23277c478bd9Sstevel@tonic-gate 		 * Doing a reopen here so the osp should already exist.
23287c478bd9Sstevel@tonic-gate 		 * If not, something changed or went very wrong.
23297c478bd9Sstevel@tonic-gate 		 *
23307c478bd9Sstevel@tonic-gate 		 * returns with 'os_sync_lock' held
23317c478bd9Sstevel@tonic-gate 		 */
23327c478bd9Sstevel@tonic-gate 		osp = find_open_stream(oop, rp);
23337c478bd9Sstevel@tonic-gate 		if (!osp) {
23347c478bd9Sstevel@tonic-gate 			NFS4_DEBUG(nfs4_lost_rqst_debug, (CE_NOTE,
23357c478bd9Sstevel@tonic-gate 			    "nfs4_resend_open_otw: couldn't find osp"));
23367c478bd9Sstevel@tonic-gate 			ep->error = EINVAL;
23377c478bd9Sstevel@tonic-gate 			goto err_out;
23387c478bd9Sstevel@tonic-gate 		}
23397c478bd9Sstevel@tonic-gate 		osp->os_open_ref_count++;
23407c478bd9Sstevel@tonic-gate 	} else {
23417c478bd9Sstevel@tonic-gate 		mutex_enter(&oop->oo_lock);
23427c478bd9Sstevel@tonic-gate 		oop->oo_just_created = NFS4_PERM_CREATED;
23437c478bd9Sstevel@tonic-gate 		mutex_exit(&oop->oo_lock);
23447c478bd9Sstevel@tonic-gate 
23457c478bd9Sstevel@tonic-gate 		/* returns with 'os_sync_lock' held */
23467c478bd9Sstevel@tonic-gate 		osp = find_or_create_open_stream(oop, rp, &created_osp);
23477c478bd9Sstevel@tonic-gate 		if (!osp) {
23487c478bd9Sstevel@tonic-gate 			NFS4_DEBUG(nfs4_lost_rqst_debug, (CE_NOTE,
23497c478bd9Sstevel@tonic-gate 			    "nfs4_resend_open_otw: couldn't create osp"));
23507c478bd9Sstevel@tonic-gate 			ep->error = EINVAL;
23517c478bd9Sstevel@tonic-gate 			goto err_out;
23527c478bd9Sstevel@tonic-gate 		}
23537c478bd9Sstevel@tonic-gate 	}
23547c478bd9Sstevel@tonic-gate 
23557c478bd9Sstevel@tonic-gate 	osp->open_stateid = op_res->stateid;
23567c478bd9Sstevel@tonic-gate 	osp->os_delegation = FALSE;
23577c478bd9Sstevel@tonic-gate 	/*
23587c478bd9Sstevel@tonic-gate 	 * Need to reset this bitfield for the possible case where we were
23597c478bd9Sstevel@tonic-gate 	 * going to OTW CLOSE the file, got a non-recoverable error, and before
23607c478bd9Sstevel@tonic-gate 	 * we could retry the CLOSE, OPENed the file again.
23617c478bd9Sstevel@tonic-gate 	 */
23627c478bd9Sstevel@tonic-gate 	ASSERT(osp->os_open_owner->oo_seqid_inuse);
23637c478bd9Sstevel@tonic-gate 	osp->os_final_close = 0;
23647c478bd9Sstevel@tonic-gate 	osp->os_force_close = 0;
23657c478bd9Sstevel@tonic-gate 
2366852c68eeSjwahlig 	if (!reopen) {
23677c478bd9Sstevel@tonic-gate 		if (open_args->share_access & OPEN4_SHARE_ACCESS_READ)
23687c478bd9Sstevel@tonic-gate 			osp->os_share_acc_read++;
23697c478bd9Sstevel@tonic-gate 		if (open_args->share_access & OPEN4_SHARE_ACCESS_WRITE)
23707c478bd9Sstevel@tonic-gate 			osp->os_share_acc_write++;
23717c478bd9Sstevel@tonic-gate 		osp->os_share_deny_none++;
23727c478bd9Sstevel@tonic-gate 	}
23737c478bd9Sstevel@tonic-gate 
23747c478bd9Sstevel@tonic-gate 	mutex_exit(&osp->os_sync_lock);
23757c478bd9Sstevel@tonic-gate 	if (created_osp)
23767c478bd9Sstevel@tonic-gate 		nfs4_inc_state_ref_count(mi);
23777c478bd9Sstevel@tonic-gate 	open_stream_rele(osp, rp);
23787c478bd9Sstevel@tonic-gate 
23797c478bd9Sstevel@tonic-gate 	nfs4_end_open_seqid_sync(oop);
23807c478bd9Sstevel@tonic-gate 
23817c478bd9Sstevel@tonic-gate 	/* accept delegation, if any */
23827c478bd9Sstevel@tonic-gate 	nfs4_delegation_accept(rp, claim, op_res, garp, cr);
23837c478bd9Sstevel@tonic-gate 
23847c478bd9Sstevel@tonic-gate 	kmem_free(destcfp, destclen + 1);
23857c478bd9Sstevel@tonic-gate 	nfs4args_copen_free(open_args);
23867c478bd9Sstevel@tonic-gate 
23877c478bd9Sstevel@tonic-gate 	if (claim == CLAIM_DELEGATE_CUR)
23887c478bd9Sstevel@tonic-gate 		nfs4_attr_cache(vp, garp, t, cr, TRUE, NULL);
23897c478bd9Sstevel@tonic-gate 	else
23907c478bd9Sstevel@tonic-gate 		PURGE_ATTRCACHE4(vp);
23917c478bd9Sstevel@tonic-gate 
2392a17ce845SMarcel Telka 	xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res);
23937c478bd9Sstevel@tonic-gate 
23947c478bd9Sstevel@tonic-gate 	ASSERT(nfs4_consistent_type(vp));
23957c478bd9Sstevel@tonic-gate 
23967c478bd9Sstevel@tonic-gate 	return;
23977c478bd9Sstevel@tonic-gate 
23987c478bd9Sstevel@tonic-gate err_out:
23997c478bd9Sstevel@tonic-gate 	nfs4_end_open_seqid_sync(oop);
24007c478bd9Sstevel@tonic-gate 	kmem_free(destcfp, destclen + 1);
24017c478bd9Sstevel@tonic-gate 	nfs4args_copen_free(open_args);
24027c478bd9Sstevel@tonic-gate 	if (!ep->error)
2403a17ce845SMarcel Telka 		xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res);
24047c478bd9Sstevel@tonic-gate }
2405