xref: /titanic_51/usr/src/uts/common/fs/nfs/nfs4_callback.c (revision 8e46f7b42b9c17653472e2d1becd2cd061346b0a)
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
5f86c6ccaSdm120769  * Common Development and Distribution License (the "License").
6f86c6ccaSdm120769  * 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 /*
223b895386SPavel Filipensky  * Copyright 2009 Sun Microsystems, Inc.  All rights reserved.
237c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
247c478bd9Sstevel@tonic-gate  */
257c478bd9Sstevel@tonic-gate 
267c478bd9Sstevel@tonic-gate /* Copyright (c) 1983, 1984, 1985, 1986, 1987, 1988, 1989 AT&T */
277c478bd9Sstevel@tonic-gate /* All Rights Reserved */
287c478bd9Sstevel@tonic-gate 
297c478bd9Sstevel@tonic-gate #include <sys/param.h>
307c478bd9Sstevel@tonic-gate #include <sys/types.h>
317c478bd9Sstevel@tonic-gate #include <sys/systm.h>
327c478bd9Sstevel@tonic-gate #include <sys/cred.h>
337c478bd9Sstevel@tonic-gate #include <sys/vfs.h>
347c478bd9Sstevel@tonic-gate #include <sys/vnode.h>
357c478bd9Sstevel@tonic-gate #include <sys/pathname.h>
367c478bd9Sstevel@tonic-gate #include <sys/sysmacros.h>
377c478bd9Sstevel@tonic-gate #include <sys/kmem.h>
387c478bd9Sstevel@tonic-gate #include <sys/kstat.h>
397c478bd9Sstevel@tonic-gate #include <sys/mkdev.h>
407c478bd9Sstevel@tonic-gate #include <sys/mount.h>
417c478bd9Sstevel@tonic-gate #include <sys/statvfs.h>
427c478bd9Sstevel@tonic-gate #include <sys/errno.h>
437c478bd9Sstevel@tonic-gate #include <sys/debug.h>
447c478bd9Sstevel@tonic-gate #include <sys/cmn_err.h>
457c478bd9Sstevel@tonic-gate #include <sys/utsname.h>
467c478bd9Sstevel@tonic-gate #include <sys/bootconf.h>
477c478bd9Sstevel@tonic-gate #include <sys/modctl.h>
487c478bd9Sstevel@tonic-gate #include <sys/acl.h>
497c478bd9Sstevel@tonic-gate #include <sys/flock.h>
507c478bd9Sstevel@tonic-gate #include <sys/kstr.h>
517c478bd9Sstevel@tonic-gate #include <sys/stropts.h>
527c478bd9Sstevel@tonic-gate #include <sys/strsubr.h>
537c478bd9Sstevel@tonic-gate #include <sys/atomic.h>
547c478bd9Sstevel@tonic-gate #include <sys/disp.h>
557c478bd9Sstevel@tonic-gate #include <sys/policy.h>
567c478bd9Sstevel@tonic-gate #include <sys/list.h>
577c478bd9Sstevel@tonic-gate #include <sys/zone.h>
587c478bd9Sstevel@tonic-gate 
597c478bd9Sstevel@tonic-gate #include <rpc/types.h>
607c478bd9Sstevel@tonic-gate #include <rpc/auth.h>
617c478bd9Sstevel@tonic-gate #include <rpc/rpcsec_gss.h>
627c478bd9Sstevel@tonic-gate #include <rpc/clnt.h>
637c478bd9Sstevel@tonic-gate #include <rpc/xdr.h>
647c478bd9Sstevel@tonic-gate 
657c478bd9Sstevel@tonic-gate #include <nfs/nfs.h>
667c478bd9Sstevel@tonic-gate #include <nfs/nfs_clnt.h>
677c478bd9Sstevel@tonic-gate #include <nfs/mount.h>
687c478bd9Sstevel@tonic-gate #include <nfs/nfs_acl.h>
697c478bd9Sstevel@tonic-gate 
707c478bd9Sstevel@tonic-gate #include <fs/fs_subr.h>
717c478bd9Sstevel@tonic-gate 
727c478bd9Sstevel@tonic-gate #include <nfs/nfs4.h>
737c478bd9Sstevel@tonic-gate #include <nfs/rnode4.h>
747c478bd9Sstevel@tonic-gate #include <nfs/nfs4_clnt.h>
757c478bd9Sstevel@tonic-gate #include <nfs/nfssys.h>
767c478bd9Sstevel@tonic-gate 
777c478bd9Sstevel@tonic-gate #ifdef	DEBUG
787c478bd9Sstevel@tonic-gate /*
797c478bd9Sstevel@tonic-gate  * These are "special" state IDs and file handles that
807c478bd9Sstevel@tonic-gate  * match any delegation state ID or file handled.  This
817c478bd9Sstevel@tonic-gate  * is for testing purposes only.
827c478bd9Sstevel@tonic-gate  */
837c478bd9Sstevel@tonic-gate 
847c478bd9Sstevel@tonic-gate stateid4 nfs4_deleg_any = { 0x7FFFFFF0 };
857c478bd9Sstevel@tonic-gate char nfs4_deleg_fh[] = "\0377\0376\0375\0374";
867c478bd9Sstevel@tonic-gate nfs_fh4 nfs4_deleg_anyfh = { sizeof (nfs4_deleg_fh)-1, nfs4_deleg_fh };
877c478bd9Sstevel@tonic-gate nfsstat4 cb4_getattr_fail = NFS4_OK;
887c478bd9Sstevel@tonic-gate nfsstat4 cb4_recall_fail = NFS4_OK;
897c478bd9Sstevel@tonic-gate 
907c478bd9Sstevel@tonic-gate int nfs4_callback_debug;
917c478bd9Sstevel@tonic-gate int nfs4_recall_debug;
927c478bd9Sstevel@tonic-gate int nfs4_drat_debug;
937c478bd9Sstevel@tonic-gate 
947c478bd9Sstevel@tonic-gate #endif
957c478bd9Sstevel@tonic-gate 
967c478bd9Sstevel@tonic-gate #define	CB_NOTE(x)	NFS4_DEBUG(nfs4_callback_debug, (CE_NOTE, x))
977c478bd9Sstevel@tonic-gate #define	CB_WARN(x)	NFS4_DEBUG(nfs4_callback_debug, (CE_WARN, x))
987c478bd9Sstevel@tonic-gate #define	CB_WARN1(x, y)	NFS4_DEBUG(nfs4_callback_debug, (CE_WARN, x, y))
997c478bd9Sstevel@tonic-gate 
1007c478bd9Sstevel@tonic-gate enum nfs4_delegreturn_policy nfs4_delegreturn_policy = INACTIVE;
1017c478bd9Sstevel@tonic-gate 
1027c478bd9Sstevel@tonic-gate static zone_key_t nfs4_callback_zone_key;
1037c478bd9Sstevel@tonic-gate 
1047c478bd9Sstevel@tonic-gate /*
1057c478bd9Sstevel@tonic-gate  * NFS4_MAPSIZE is the number of bytes we are willing to consume
1067c478bd9Sstevel@tonic-gate  * for the block allocation map when the server grants a NFS_LIMIT_BLOCK
1077c478bd9Sstevel@tonic-gate  * style delegation.
1087c478bd9Sstevel@tonic-gate  */
1097c478bd9Sstevel@tonic-gate 
1107c478bd9Sstevel@tonic-gate #define	NFS4_MAPSIZE	8192
1117c478bd9Sstevel@tonic-gate #define	NFS4_MAPWORDS	NFS4_MAPSIZE/sizeof (uint_t)
1127c478bd9Sstevel@tonic-gate #define	NbPW		(NBBY*sizeof (uint_t))
1137c478bd9Sstevel@tonic-gate 
1147c478bd9Sstevel@tonic-gate static int nfs4_num_prognums = 1024;
1157c478bd9Sstevel@tonic-gate static SVC_CALLOUT_TABLE nfs4_cb_sct;
1167c478bd9Sstevel@tonic-gate 
1177c478bd9Sstevel@tonic-gate struct nfs4_dnode {
1187c478bd9Sstevel@tonic-gate 	list_node_t	linkage;
1197c478bd9Sstevel@tonic-gate 	rnode4_t	*rnodep;
1207c478bd9Sstevel@tonic-gate 	int		flags;		/* Flags for nfs4delegreturn_impl() */
1217c478bd9Sstevel@tonic-gate };
1227c478bd9Sstevel@tonic-gate 
1237c478bd9Sstevel@tonic-gate static const struct nfs4_callback_stats nfs4_callback_stats_tmpl = {
1247c478bd9Sstevel@tonic-gate 	{ "delegations",	KSTAT_DATA_UINT64 },
1257c478bd9Sstevel@tonic-gate 	{ "cb_getattr",		KSTAT_DATA_UINT64 },
1267c478bd9Sstevel@tonic-gate 	{ "cb_recall",		KSTAT_DATA_UINT64 },
1277c478bd9Sstevel@tonic-gate 	{ "cb_null",		KSTAT_DATA_UINT64 },
1287c478bd9Sstevel@tonic-gate 	{ "cb_dispatch",	KSTAT_DATA_UINT64 },
1297c478bd9Sstevel@tonic-gate 	{ "delegaccept_r",	KSTAT_DATA_UINT64 },
1307c478bd9Sstevel@tonic-gate 	{ "delegaccept_rw",	KSTAT_DATA_UINT64 },
1317c478bd9Sstevel@tonic-gate 	{ "delegreturn",	KSTAT_DATA_UINT64 },
1327c478bd9Sstevel@tonic-gate 	{ "callbacks",		KSTAT_DATA_UINT64 },
1337c478bd9Sstevel@tonic-gate 	{ "claim_cur",		KSTAT_DATA_UINT64 },
1347c478bd9Sstevel@tonic-gate 	{ "claim_cur_ok",	KSTAT_DATA_UINT64 },
1357c478bd9Sstevel@tonic-gate 	{ "recall_trunc",	KSTAT_DATA_UINT64 },
1367c478bd9Sstevel@tonic-gate 	{ "recall_failed",	KSTAT_DATA_UINT64 },
1377c478bd9Sstevel@tonic-gate 	{ "return_limit_write",	KSTAT_DATA_UINT64 },
1387c478bd9Sstevel@tonic-gate 	{ "return_limit_addmap", KSTAT_DATA_UINT64 },
1397c478bd9Sstevel@tonic-gate 	{ "deleg_recover",	KSTAT_DATA_UINT64 },
1407c478bd9Sstevel@tonic-gate 	{ "cb_illegal",		KSTAT_DATA_UINT64 }
1417c478bd9Sstevel@tonic-gate };
1427c478bd9Sstevel@tonic-gate 
1437c478bd9Sstevel@tonic-gate struct nfs4_cb_port {
1447c478bd9Sstevel@tonic-gate 	list_node_t		linkage; /* linkage into per-zone port list */
1457c478bd9Sstevel@tonic-gate 	char			netid[KNC_STRSIZE];
1467c478bd9Sstevel@tonic-gate 	char			uaddr[KNC_STRSIZE];
1477c478bd9Sstevel@tonic-gate 	char			protofmly[KNC_STRSIZE];
1487c478bd9Sstevel@tonic-gate 	char			proto[KNC_STRSIZE];
1497c478bd9Sstevel@tonic-gate };
1507c478bd9Sstevel@tonic-gate 
1517c478bd9Sstevel@tonic-gate static int cb_getattr_bytes;
1527c478bd9Sstevel@tonic-gate 
1537c478bd9Sstevel@tonic-gate struct cb_recall_pass {
1547c478bd9Sstevel@tonic-gate 	rnode4_t	*rp;
1557c478bd9Sstevel@tonic-gate 	int		flags;		/* Flags for nfs4delegreturn_impl() */
1567c478bd9Sstevel@tonic-gate 	bool_t		truncate;
1577c478bd9Sstevel@tonic-gate };
1587c478bd9Sstevel@tonic-gate 
1597c478bd9Sstevel@tonic-gate static nfs4_open_stream_t *get_next_deleg_stream(rnode4_t *, int);
1607c478bd9Sstevel@tonic-gate static void nfs4delegreturn_thread(struct cb_recall_pass *);
1617c478bd9Sstevel@tonic-gate static int deleg_reopen(vnode_t *, bool_t *, struct nfs4_callback_globals *,
1627c478bd9Sstevel@tonic-gate     int);
1637c478bd9Sstevel@tonic-gate static void nfs4_dlistadd(rnode4_t *, struct nfs4_callback_globals *, int);
1647c478bd9Sstevel@tonic-gate static void nfs4_dlistclean_impl(struct nfs4_callback_globals *, int);
1657c478bd9Sstevel@tonic-gate static int nfs4delegreturn_impl(rnode4_t *, int,
1667c478bd9Sstevel@tonic-gate     struct nfs4_callback_globals *);
1677c478bd9Sstevel@tonic-gate static void nfs4delegreturn_cleanup_impl(rnode4_t *, nfs4_server_t *,
1687c478bd9Sstevel@tonic-gate     struct nfs4_callback_globals *);
169*8e46f7b4SArne Jansen static void nfs4_recall_sync_wait(nfs4_server_t *);
1707c478bd9Sstevel@tonic-gate 
1717c478bd9Sstevel@tonic-gate static void
1727c478bd9Sstevel@tonic-gate cb_getattr(nfs_cb_argop4 *argop, nfs_cb_resop4 *resop, struct svc_req *req,
1737c478bd9Sstevel@tonic-gate 	struct compound_state *cs, struct nfs4_callback_globals *ncg)
1747c478bd9Sstevel@tonic-gate {
1757c478bd9Sstevel@tonic-gate 	CB_GETATTR4args *args = &argop->nfs_cb_argop4_u.opcbgetattr;
1767c478bd9Sstevel@tonic-gate 	CB_GETATTR4res *resp = &resop->nfs_cb_resop4_u.opcbgetattr;
1777c478bd9Sstevel@tonic-gate 	rnode4_t *rp;
1787c478bd9Sstevel@tonic-gate 	vnode_t *vp;
1797c478bd9Sstevel@tonic-gate 	bool_t found = FALSE;
1807c478bd9Sstevel@tonic-gate 	struct nfs4_server *sp;
1817c478bd9Sstevel@tonic-gate 	struct fattr4 *fap;
182eac3aab7Srobinson 	rpc_inline_t *fdata;
1837c478bd9Sstevel@tonic-gate 	long mapcnt;
1847c478bd9Sstevel@tonic-gate 	fattr4_change change;
1857c478bd9Sstevel@tonic-gate 	fattr4_size size;
1867c478bd9Sstevel@tonic-gate 	uint_t rflag;
1877c478bd9Sstevel@tonic-gate 
1887c478bd9Sstevel@tonic-gate 	ncg->nfs4_callback_stats.cb_getattr.value.ui64++;
1897c478bd9Sstevel@tonic-gate 
1907c478bd9Sstevel@tonic-gate #ifdef DEBUG
1917c478bd9Sstevel@tonic-gate 	/*
1927c478bd9Sstevel@tonic-gate 	 * error injection hook: set cb_getattr_fail global to
1937c478bd9Sstevel@tonic-gate 	 * NFS4 pcol error to be returned
1947c478bd9Sstevel@tonic-gate 	 */
1957c478bd9Sstevel@tonic-gate 	if (cb4_getattr_fail != NFS4_OK) {
1967c478bd9Sstevel@tonic-gate 		*cs->statusp = resp->status = cb4_getattr_fail;
1977c478bd9Sstevel@tonic-gate 		return;
1987c478bd9Sstevel@tonic-gate 	}
1997c478bd9Sstevel@tonic-gate #endif
2007c478bd9Sstevel@tonic-gate 
2017c478bd9Sstevel@tonic-gate 	resp->obj_attributes.attrmask = 0;
2027c478bd9Sstevel@tonic-gate 
2037c478bd9Sstevel@tonic-gate 	mutex_enter(&ncg->nfs4_cb_lock);
2047c478bd9Sstevel@tonic-gate 	sp = ncg->nfs4prog2server[req->rq_prog - NFS4_CALLBACK];
2057c478bd9Sstevel@tonic-gate 	mutex_exit(&ncg->nfs4_cb_lock);
2067c478bd9Sstevel@tonic-gate 
2077c478bd9Sstevel@tonic-gate 	if (nfs4_server_vlock(sp, 0) == FALSE) {
2087c478bd9Sstevel@tonic-gate 
2097c478bd9Sstevel@tonic-gate 		CB_WARN("cb_getattr: cannot find server\n");
2107c478bd9Sstevel@tonic-gate 
2117c478bd9Sstevel@tonic-gate 		*cs->statusp = resp->status = NFS4ERR_BADHANDLE;
2127c478bd9Sstevel@tonic-gate 		return;
2137c478bd9Sstevel@tonic-gate 	}
2147c478bd9Sstevel@tonic-gate 
2157c478bd9Sstevel@tonic-gate 	/*
2167c478bd9Sstevel@tonic-gate 	 * In cb_compound, callback_ident was validated against rq_prog,
2177c478bd9Sstevel@tonic-gate 	 * but we couldn't verify that it was set to the value we provided
2187c478bd9Sstevel@tonic-gate 	 * at setclientid time (because we didn't have server struct yet).
2197c478bd9Sstevel@tonic-gate 	 * Now we have the server struct, but don't have callback_ident
2207c478bd9Sstevel@tonic-gate 	 * handy.  So, validate server struct program number against req
2217c478bd9Sstevel@tonic-gate 	 * RPC's prog number.  At this point, we know the RPC prog num
2227c478bd9Sstevel@tonic-gate 	 * is valid (else we wouldn't be here); however, we don't know
2237c478bd9Sstevel@tonic-gate 	 * that it was the prog number we supplied to this server at
2247c478bd9Sstevel@tonic-gate 	 * setclientid time.  If the prog numbers aren't equivalent, then
2257c478bd9Sstevel@tonic-gate 	 * log the problem and fail the request because either cbserv
2267c478bd9Sstevel@tonic-gate 	 * and/or cbclient are confused.  This will probably never happen.
2277c478bd9Sstevel@tonic-gate 	 */
2287c478bd9Sstevel@tonic-gate 	if (sp->s_program != req->rq_prog) {
2297c478bd9Sstevel@tonic-gate #ifdef DEBUG
2307c478bd9Sstevel@tonic-gate 		zcmn_err(getzoneid(), CE_WARN,
2317c478bd9Sstevel@tonic-gate 		    "cb_getattr: wrong server program number srv=%d req=%d\n",
2327c478bd9Sstevel@tonic-gate 		    sp->s_program, req->rq_prog);
2337c478bd9Sstevel@tonic-gate #else
2347c478bd9Sstevel@tonic-gate 		zcmn_err(getzoneid(), CE_WARN,
2357c478bd9Sstevel@tonic-gate 		    "cb_getattr: wrong server program number\n");
2367c478bd9Sstevel@tonic-gate #endif
2377c478bd9Sstevel@tonic-gate 		mutex_exit(&sp->s_lock);
2387c478bd9Sstevel@tonic-gate 		nfs4_server_rele(sp);
2397c478bd9Sstevel@tonic-gate 		*cs->statusp = resp->status = NFS4ERR_BADHANDLE;
2407c478bd9Sstevel@tonic-gate 		return;
2417c478bd9Sstevel@tonic-gate 	}
2427c478bd9Sstevel@tonic-gate 
2437c478bd9Sstevel@tonic-gate 	/*
2447c478bd9Sstevel@tonic-gate 	 * Search the delegation list for a matching file handle;
2457c478bd9Sstevel@tonic-gate 	 * mutex on sp prevents the list from changing.
2467c478bd9Sstevel@tonic-gate 	 */
2477c478bd9Sstevel@tonic-gate 
2487c478bd9Sstevel@tonic-gate 	rp = list_head(&sp->s_deleg_list);
2497c478bd9Sstevel@tonic-gate 	for (; rp != NULL; rp = list_next(&sp->s_deleg_list, rp)) {
2507c478bd9Sstevel@tonic-gate 		nfs4_fhandle_t fhandle;
2517c478bd9Sstevel@tonic-gate 
2527c478bd9Sstevel@tonic-gate 		sfh4_copyval(rp->r_fh, &fhandle);
2537c478bd9Sstevel@tonic-gate 
2547c478bd9Sstevel@tonic-gate 		if ((fhandle.fh_len == args->fh.nfs_fh4_len &&
2557c478bd9Sstevel@tonic-gate 		    bcmp(fhandle.fh_buf, args->fh.nfs_fh4_val,
2567c478bd9Sstevel@tonic-gate 		    fhandle.fh_len) == 0)) {
2577c478bd9Sstevel@tonic-gate 
2587c478bd9Sstevel@tonic-gate 			found = TRUE;
2597c478bd9Sstevel@tonic-gate 			break;
2607c478bd9Sstevel@tonic-gate 		}
2617c478bd9Sstevel@tonic-gate #ifdef	DEBUG
2627c478bd9Sstevel@tonic-gate 		if (nfs4_deleg_anyfh.nfs_fh4_len == args->fh.nfs_fh4_len &&
2637c478bd9Sstevel@tonic-gate 		    bcmp(nfs4_deleg_anyfh.nfs_fh4_val, args->fh.nfs_fh4_val,
2647c478bd9Sstevel@tonic-gate 		    args->fh.nfs_fh4_len) == 0) {
2657c478bd9Sstevel@tonic-gate 
2667c478bd9Sstevel@tonic-gate 			found = TRUE;
2677c478bd9Sstevel@tonic-gate 			break;
2687c478bd9Sstevel@tonic-gate 		}
2697c478bd9Sstevel@tonic-gate #endif
2707c478bd9Sstevel@tonic-gate 	}
2717c478bd9Sstevel@tonic-gate 
2727c478bd9Sstevel@tonic-gate 	/*
2737c478bd9Sstevel@tonic-gate 	 * VN_HOLD the vnode before releasing s_lock to guarantee
2747c478bd9Sstevel@tonic-gate 	 * we have a valid vnode reference.
2757c478bd9Sstevel@tonic-gate 	 */
2767c478bd9Sstevel@tonic-gate 	if (found == TRUE) {
2777c478bd9Sstevel@tonic-gate 		vp = RTOV4(rp);
2787c478bd9Sstevel@tonic-gate 		VN_HOLD(vp);
2797c478bd9Sstevel@tonic-gate 	}
2807c478bd9Sstevel@tonic-gate 
2817c478bd9Sstevel@tonic-gate 	mutex_exit(&sp->s_lock);
2827c478bd9Sstevel@tonic-gate 	nfs4_server_rele(sp);
2837c478bd9Sstevel@tonic-gate 
2847c478bd9Sstevel@tonic-gate 	if (found == FALSE) {
2857c478bd9Sstevel@tonic-gate 
2867c478bd9Sstevel@tonic-gate 		CB_WARN("cb_getattr: bad fhandle\n");
2877c478bd9Sstevel@tonic-gate 
2887c478bd9Sstevel@tonic-gate 		*cs->statusp = resp->status = NFS4ERR_BADHANDLE;
2897c478bd9Sstevel@tonic-gate 		return;
2907c478bd9Sstevel@tonic-gate 	}
2917c478bd9Sstevel@tonic-gate 
2927c478bd9Sstevel@tonic-gate 	/*
2937c478bd9Sstevel@tonic-gate 	 * Figure out which attributes the server wants.  We only
2947c478bd9Sstevel@tonic-gate 	 * offer FATTR4_CHANGE & FATTR4_SIZE; ignore the rest.
2957c478bd9Sstevel@tonic-gate 	 */
2967c478bd9Sstevel@tonic-gate 	fdata = kmem_alloc(cb_getattr_bytes, KM_SLEEP);
2977c478bd9Sstevel@tonic-gate 
2987c478bd9Sstevel@tonic-gate 	/*
2997c478bd9Sstevel@tonic-gate 	 * Don't actually need to create XDR to encode these
3007c478bd9Sstevel@tonic-gate 	 * simple data structures.
3017c478bd9Sstevel@tonic-gate 	 * xdrmem_create(&xdr, fdata, cb_getattr_bytes, XDR_ENCODE);
3027c478bd9Sstevel@tonic-gate 	 */
3037c478bd9Sstevel@tonic-gate 	fap = &resp->obj_attributes;
3047c478bd9Sstevel@tonic-gate 
3057c478bd9Sstevel@tonic-gate 	fap->attrmask = 0;
3067c478bd9Sstevel@tonic-gate 	/* attrlist4_len starts at 0 and increases as attrs are processed */
307eac3aab7Srobinson 	fap->attrlist4 = (char *)fdata;
3087c478bd9Sstevel@tonic-gate 	fap->attrlist4_len = 0;
3097c478bd9Sstevel@tonic-gate 
3107c478bd9Sstevel@tonic-gate 	/* don't supply attrs if request was zero */
3117c478bd9Sstevel@tonic-gate 	if (args->attr_request != 0) {
3127c478bd9Sstevel@tonic-gate 		if (args->attr_request & FATTR4_CHANGE_MASK) {
3137c478bd9Sstevel@tonic-gate 			/*
3147c478bd9Sstevel@tonic-gate 			 * If the file is mmapped, then increment the change
3157c478bd9Sstevel@tonic-gate 			 * attribute and return it.  This will guarantee that
3167c478bd9Sstevel@tonic-gate 			 * the server will perceive that the file has changed
3177c478bd9Sstevel@tonic-gate 			 * if there is any chance that the client application
3187c478bd9Sstevel@tonic-gate 			 * has changed it.  Otherwise, just return the change
3197c478bd9Sstevel@tonic-gate 			 * attribute as it has been updated by nfs4write_deleg.
3207c478bd9Sstevel@tonic-gate 			 */
3217c478bd9Sstevel@tonic-gate 
3227c478bd9Sstevel@tonic-gate 			mutex_enter(&rp->r_statelock);
3237c478bd9Sstevel@tonic-gate 			mapcnt = rp->r_mapcnt;
3247c478bd9Sstevel@tonic-gate 			rflag = rp->r_flags;
3257c478bd9Sstevel@tonic-gate 			mutex_exit(&rp->r_statelock);
3267c478bd9Sstevel@tonic-gate 
3277c478bd9Sstevel@tonic-gate 			mutex_enter(&rp->r_statev4_lock);
3287c478bd9Sstevel@tonic-gate 			/*
3297c478bd9Sstevel@tonic-gate 			 * If object mapped, then always return new change.
3307c478bd9Sstevel@tonic-gate 			 * Otherwise, return change if object has dirty
3317c478bd9Sstevel@tonic-gate 			 * pages.  If object doesn't have any dirty pages,
3327c478bd9Sstevel@tonic-gate 			 * then all changes have been pushed to server, so
3337c478bd9Sstevel@tonic-gate 			 * reset change to grant change.
3347c478bd9Sstevel@tonic-gate 			 */
3357c478bd9Sstevel@tonic-gate 			if (mapcnt)
3367c478bd9Sstevel@tonic-gate 				rp->r_deleg_change++;
3377c478bd9Sstevel@tonic-gate 			else if (! (rflag & R4DIRTY))
3387c478bd9Sstevel@tonic-gate 				rp->r_deleg_change = rp->r_deleg_change_grant;
3397c478bd9Sstevel@tonic-gate 			change = rp->r_deleg_change;
3407c478bd9Sstevel@tonic-gate 			mutex_exit(&rp->r_statev4_lock);
3417c478bd9Sstevel@tonic-gate 
3427c478bd9Sstevel@tonic-gate 			/*
3437c478bd9Sstevel@tonic-gate 			 * Use inline XDR code directly, we know that we
3447c478bd9Sstevel@tonic-gate 			 * going to a memory buffer and it has enough
3457c478bd9Sstevel@tonic-gate 			 * space so it cannot fail.
3467c478bd9Sstevel@tonic-gate 			 */
3477c478bd9Sstevel@tonic-gate 			IXDR_PUT_U_HYPER(fdata, change);
3487c478bd9Sstevel@tonic-gate 			fap->attrlist4_len += 2 * BYTES_PER_XDR_UNIT;
349eac3aab7Srobinson 			fap->attrmask |= FATTR4_CHANGE_MASK;
3507c478bd9Sstevel@tonic-gate 		}
3517c478bd9Sstevel@tonic-gate 
3527c478bd9Sstevel@tonic-gate 		if (args->attr_request & FATTR4_SIZE_MASK) {
3537c478bd9Sstevel@tonic-gate 			/*
3547c478bd9Sstevel@tonic-gate 			 * Use an atomic add of 0 to fetch a consistent view
3557c478bd9Sstevel@tonic-gate 			 * of r_size; this avoids having to take rw_lock
3567c478bd9Sstevel@tonic-gate 			 * which could cause a deadlock.
3577c478bd9Sstevel@tonic-gate 			 */
3587c478bd9Sstevel@tonic-gate 			size = atomic_add_64_nv((uint64_t *)&rp->r_size, 0);
3597c478bd9Sstevel@tonic-gate 
3607c478bd9Sstevel@tonic-gate 			/*
3617c478bd9Sstevel@tonic-gate 			 * Use inline XDR code directly, we know that we
3627c478bd9Sstevel@tonic-gate 			 * going to a memory buffer and it has enough
3637c478bd9Sstevel@tonic-gate 			 * space so it cannot fail.
3647c478bd9Sstevel@tonic-gate 			 */
3657c478bd9Sstevel@tonic-gate 			IXDR_PUT_U_HYPER(fdata, size);
3667c478bd9Sstevel@tonic-gate 			fap->attrlist4_len += 2 * BYTES_PER_XDR_UNIT;
367eac3aab7Srobinson 			fap->attrmask |= FATTR4_SIZE_MASK;
3687c478bd9Sstevel@tonic-gate 		}
3697c478bd9Sstevel@tonic-gate 	}
3707c478bd9Sstevel@tonic-gate 
3717c478bd9Sstevel@tonic-gate 	VN_RELE(vp);
3727c478bd9Sstevel@tonic-gate 
3737c478bd9Sstevel@tonic-gate 	*cs->statusp = resp->status = NFS4_OK;
3747c478bd9Sstevel@tonic-gate }
3757c478bd9Sstevel@tonic-gate 
3767c478bd9Sstevel@tonic-gate static void
3777c478bd9Sstevel@tonic-gate cb_getattr_free(nfs_cb_resop4 *resop)
3787c478bd9Sstevel@tonic-gate {
3797c478bd9Sstevel@tonic-gate 	if (resop->nfs_cb_resop4_u.opcbgetattr.obj_attributes.attrlist4)
3807c478bd9Sstevel@tonic-gate 		kmem_free(resop->nfs_cb_resop4_u.opcbgetattr.
3819ea21c83Swebaker 		    obj_attributes.attrlist4, cb_getattr_bytes);
3827c478bd9Sstevel@tonic-gate }
3837c478bd9Sstevel@tonic-gate 
3847c478bd9Sstevel@tonic-gate static void
3857c478bd9Sstevel@tonic-gate cb_recall(nfs_cb_argop4 *argop, nfs_cb_resop4 *resop, struct svc_req *req,
3867c478bd9Sstevel@tonic-gate 	struct compound_state *cs, struct nfs4_callback_globals *ncg)
3877c478bd9Sstevel@tonic-gate {
3887c478bd9Sstevel@tonic-gate 	CB_RECALL4args * args = &argop->nfs_cb_argop4_u.opcbrecall;
3897c478bd9Sstevel@tonic-gate 	CB_RECALL4res *resp = &resop->nfs_cb_resop4_u.opcbrecall;
3907c478bd9Sstevel@tonic-gate 	rnode4_t *rp;
3917c478bd9Sstevel@tonic-gate 	vnode_t *vp;
3927c478bd9Sstevel@tonic-gate 	struct nfs4_server *sp;
3937c478bd9Sstevel@tonic-gate 	bool_t found = FALSE;
394*8e46f7b4SArne Jansen 	bool_t retried = FALSE;
3957c478bd9Sstevel@tonic-gate 
3967c478bd9Sstevel@tonic-gate 	ncg->nfs4_callback_stats.cb_recall.value.ui64++;
3977c478bd9Sstevel@tonic-gate 
3987c478bd9Sstevel@tonic-gate 	ASSERT(req->rq_prog >= NFS4_CALLBACK);
3997c478bd9Sstevel@tonic-gate 	ASSERT(req->rq_prog < NFS4_CALLBACK+nfs4_num_prognums);
4007c478bd9Sstevel@tonic-gate 
4017c478bd9Sstevel@tonic-gate #ifdef DEBUG
4027c478bd9Sstevel@tonic-gate 	/*
4037c478bd9Sstevel@tonic-gate 	 * error injection hook: set cb_recall_fail global to
4047c478bd9Sstevel@tonic-gate 	 * NFS4 pcol error to be returned
4057c478bd9Sstevel@tonic-gate 	 */
4067c478bd9Sstevel@tonic-gate 	if (cb4_recall_fail != NFS4_OK) {
4077c478bd9Sstevel@tonic-gate 		*cs->statusp = resp->status = cb4_recall_fail;
4087c478bd9Sstevel@tonic-gate 		return;
4097c478bd9Sstevel@tonic-gate 	}
4107c478bd9Sstevel@tonic-gate #endif
4117c478bd9Sstevel@tonic-gate 
4127c478bd9Sstevel@tonic-gate 	mutex_enter(&ncg->nfs4_cb_lock);
4137c478bd9Sstevel@tonic-gate 	sp = ncg->nfs4prog2server[req->rq_prog - NFS4_CALLBACK];
4147c478bd9Sstevel@tonic-gate 	mutex_exit(&ncg->nfs4_cb_lock);
4157c478bd9Sstevel@tonic-gate 
4167c478bd9Sstevel@tonic-gate 	if (nfs4_server_vlock(sp, 0) == FALSE) {
4177c478bd9Sstevel@tonic-gate 
4187c478bd9Sstevel@tonic-gate 		CB_WARN("cb_recall: cannot find server\n");
4197c478bd9Sstevel@tonic-gate 
4207c478bd9Sstevel@tonic-gate 		*cs->statusp = resp->status = NFS4ERR_BADHANDLE;
4217c478bd9Sstevel@tonic-gate 		return;
4227c478bd9Sstevel@tonic-gate 	}
4237c478bd9Sstevel@tonic-gate 
424*8e46f7b4SArne Jansen retry:
4257c478bd9Sstevel@tonic-gate 	/*
4267c478bd9Sstevel@tonic-gate 	 * Search the delegation list for a matching file handle
4277c478bd9Sstevel@tonic-gate 	 * AND stateid; mutex on sp prevents the list from changing.
4287c478bd9Sstevel@tonic-gate 	 */
4297c478bd9Sstevel@tonic-gate 
4307c478bd9Sstevel@tonic-gate 	rp = list_head(&sp->s_deleg_list);
4317c478bd9Sstevel@tonic-gate 	for (; rp != NULL; rp = list_next(&sp->s_deleg_list, rp)) {
4327c478bd9Sstevel@tonic-gate 		mutex_enter(&rp->r_statev4_lock);
4337c478bd9Sstevel@tonic-gate 
4347c478bd9Sstevel@tonic-gate 		/* check both state id and file handle! */
4357c478bd9Sstevel@tonic-gate 
4367c478bd9Sstevel@tonic-gate 		if ((bcmp(&rp->r_deleg_stateid, &args->stateid,
4377c478bd9Sstevel@tonic-gate 		    sizeof (stateid4)) == 0)) {
4387c478bd9Sstevel@tonic-gate 			nfs4_fhandle_t fhandle;
4397c478bd9Sstevel@tonic-gate 
4407c478bd9Sstevel@tonic-gate 			sfh4_copyval(rp->r_fh, &fhandle);
4417c478bd9Sstevel@tonic-gate 			if ((fhandle.fh_len == args->fh.nfs_fh4_len &&
4427c478bd9Sstevel@tonic-gate 			    bcmp(fhandle.fh_buf, args->fh.nfs_fh4_val,
4437c478bd9Sstevel@tonic-gate 			    fhandle.fh_len) == 0)) {
4447c478bd9Sstevel@tonic-gate 
4457c478bd9Sstevel@tonic-gate 				found = TRUE;
4467c478bd9Sstevel@tonic-gate 				break;
4477c478bd9Sstevel@tonic-gate 			} else {
4487c478bd9Sstevel@tonic-gate #ifdef	DEBUG
4497c478bd9Sstevel@tonic-gate 				CB_WARN("cb_recall: stateid OK, bad fh");
4507c478bd9Sstevel@tonic-gate #endif
4517c478bd9Sstevel@tonic-gate 			}
4527c478bd9Sstevel@tonic-gate 		}
4537c478bd9Sstevel@tonic-gate #ifdef	DEBUG
4547c478bd9Sstevel@tonic-gate 		if (bcmp(&args->stateid, &nfs4_deleg_any,
4557c478bd9Sstevel@tonic-gate 		    sizeof (stateid4)) == 0) {
4567c478bd9Sstevel@tonic-gate 
4577c478bd9Sstevel@tonic-gate 			found = TRUE;
4587c478bd9Sstevel@tonic-gate 			break;
4597c478bd9Sstevel@tonic-gate 		}
4607c478bd9Sstevel@tonic-gate #endif
4617c478bd9Sstevel@tonic-gate 		mutex_exit(&rp->r_statev4_lock);
4627c478bd9Sstevel@tonic-gate 	}
4637c478bd9Sstevel@tonic-gate 
4647c478bd9Sstevel@tonic-gate 	/*
4657c478bd9Sstevel@tonic-gate 	 * VN_HOLD the vnode before releasing s_lock to guarantee
4667c478bd9Sstevel@tonic-gate 	 * we have a valid vnode reference.  The async thread will
4677c478bd9Sstevel@tonic-gate 	 * release the hold when it's done.
4687c478bd9Sstevel@tonic-gate 	 */
4697c478bd9Sstevel@tonic-gate 	if (found == TRUE) {
4707c478bd9Sstevel@tonic-gate 		mutex_exit(&rp->r_statev4_lock);
4717c478bd9Sstevel@tonic-gate 		vp = RTOV4(rp);
4727c478bd9Sstevel@tonic-gate 		VN_HOLD(vp);
4737c478bd9Sstevel@tonic-gate 	}
4747c478bd9Sstevel@tonic-gate 	mutex_exit(&sp->s_lock);
475*8e46f7b4SArne Jansen 
476*8e46f7b4SArne Jansen 	if (found == FALSE && retried == FALSE) {
477*8e46f7b4SArne Jansen 		nfs4_recall_sync_wait(sp);
478*8e46f7b4SArne Jansen 		mutex_enter(&sp->s_lock);
479*8e46f7b4SArne Jansen 		retried = TRUE;
480*8e46f7b4SArne Jansen 		goto retry;
481*8e46f7b4SArne Jansen 	}
482*8e46f7b4SArne Jansen 
4837c478bd9Sstevel@tonic-gate 	nfs4_server_rele(sp);
4847c478bd9Sstevel@tonic-gate 
4857c478bd9Sstevel@tonic-gate 	if (found == FALSE) {
4867c478bd9Sstevel@tonic-gate 
4877c478bd9Sstevel@tonic-gate 		CB_WARN("cb_recall: bad stateid\n");
4887c478bd9Sstevel@tonic-gate 
4897c478bd9Sstevel@tonic-gate 		*cs->statusp = resp->status = NFS4ERR_BAD_STATEID;
4907c478bd9Sstevel@tonic-gate 		return;
4917c478bd9Sstevel@tonic-gate 	}
4927c478bd9Sstevel@tonic-gate 
4937c478bd9Sstevel@tonic-gate 	/* Fire up a thread to do the delegreturn */
4947c478bd9Sstevel@tonic-gate 	nfs4delegreturn_async(rp, NFS4_DR_RECALL|NFS4_DR_REOPEN,
4957c478bd9Sstevel@tonic-gate 	    args->truncate);
4967c478bd9Sstevel@tonic-gate 
4977c478bd9Sstevel@tonic-gate 	*cs->statusp = resp->status = 0;
4987c478bd9Sstevel@tonic-gate }
4997c478bd9Sstevel@tonic-gate 
5007c478bd9Sstevel@tonic-gate /* ARGSUSED */
5017c478bd9Sstevel@tonic-gate static void
5027c478bd9Sstevel@tonic-gate cb_recall_free(nfs_cb_resop4 *resop)
5037c478bd9Sstevel@tonic-gate {
5047c478bd9Sstevel@tonic-gate 	/* nothing to do here, cb_recall doesn't kmem_alloc */
5057c478bd9Sstevel@tonic-gate }
5067c478bd9Sstevel@tonic-gate 
5077c478bd9Sstevel@tonic-gate /*
5087c478bd9Sstevel@tonic-gate  * This function handles the CB_NULL proc call from an NFSv4 Server.
5097c478bd9Sstevel@tonic-gate  *
5107c478bd9Sstevel@tonic-gate  * We take note that the server has sent a CB_NULL for later processing
5117c478bd9Sstevel@tonic-gate  * in the recovery logic. It is noted so we may pause slightly after the
5127c478bd9Sstevel@tonic-gate  * setclientid and before reopening files. The pause is to allow the
5137c478bd9Sstevel@tonic-gate  * NFSv4 Server time to receive the CB_NULL reply and adjust any of
5147c478bd9Sstevel@tonic-gate  * its internal structures such that it has the opportunity to grant
5157c478bd9Sstevel@tonic-gate  * delegations to reopened files.
5167c478bd9Sstevel@tonic-gate  *
5177c478bd9Sstevel@tonic-gate  */
5187c478bd9Sstevel@tonic-gate 
5197c478bd9Sstevel@tonic-gate /* ARGSUSED */
5207c478bd9Sstevel@tonic-gate static void
5217c478bd9Sstevel@tonic-gate cb_null(CB_COMPOUND4args *args, CB_COMPOUND4res *resp, struct svc_req *req,
5227c478bd9Sstevel@tonic-gate     struct nfs4_callback_globals *ncg)
5237c478bd9Sstevel@tonic-gate {
5247c478bd9Sstevel@tonic-gate 	struct nfs4_server *sp;
5257c478bd9Sstevel@tonic-gate 
5267c478bd9Sstevel@tonic-gate 	ncg->nfs4_callback_stats.cb_null.value.ui64++;
5277c478bd9Sstevel@tonic-gate 
5287c478bd9Sstevel@tonic-gate 	ASSERT(req->rq_prog >= NFS4_CALLBACK);
5297c478bd9Sstevel@tonic-gate 	ASSERT(req->rq_prog < NFS4_CALLBACK+nfs4_num_prognums);
5307c478bd9Sstevel@tonic-gate 
5317c478bd9Sstevel@tonic-gate 	mutex_enter(&ncg->nfs4_cb_lock);
5327c478bd9Sstevel@tonic-gate 	sp = ncg->nfs4prog2server[req->rq_prog - NFS4_CALLBACK];
5337c478bd9Sstevel@tonic-gate 	mutex_exit(&ncg->nfs4_cb_lock);
5347c478bd9Sstevel@tonic-gate 
5357c478bd9Sstevel@tonic-gate 	if (nfs4_server_vlock(sp, 0) != FALSE) {
5367c478bd9Sstevel@tonic-gate 		sp->s_flags |= N4S_CB_PINGED;
5377c478bd9Sstevel@tonic-gate 		cv_broadcast(&sp->wait_cb_null);
5387c478bd9Sstevel@tonic-gate 		mutex_exit(&sp->s_lock);
5397c478bd9Sstevel@tonic-gate 		nfs4_server_rele(sp);
5407c478bd9Sstevel@tonic-gate 	}
5417c478bd9Sstevel@tonic-gate }
5427c478bd9Sstevel@tonic-gate 
5437c478bd9Sstevel@tonic-gate /*
5447c478bd9Sstevel@tonic-gate  * cb_illegal	args: void
5457c478bd9Sstevel@tonic-gate  *		res : status (NFS4ERR_OP_CB_ILLEGAL)
5467c478bd9Sstevel@tonic-gate  */
5477c478bd9Sstevel@tonic-gate /* ARGSUSED */
5487c478bd9Sstevel@tonic-gate static void
5497c478bd9Sstevel@tonic-gate cb_illegal(nfs_cb_argop4 *argop, nfs_cb_resop4 *resop, struct svc_req *req,
5507c478bd9Sstevel@tonic-gate 	struct compound_state *cs, struct nfs4_callback_globals *ncg)
5517c478bd9Sstevel@tonic-gate {
5527c478bd9Sstevel@tonic-gate 	CB_ILLEGAL4res *resp = &resop->nfs_cb_resop4_u.opcbillegal;
5537c478bd9Sstevel@tonic-gate 
5547c478bd9Sstevel@tonic-gate 	ncg->nfs4_callback_stats.cb_illegal.value.ui64++;
5557c478bd9Sstevel@tonic-gate 	resop->resop = OP_CB_ILLEGAL;
5567c478bd9Sstevel@tonic-gate 	*cs->statusp = resp->status = NFS4ERR_OP_ILLEGAL;
5577c478bd9Sstevel@tonic-gate }
5587c478bd9Sstevel@tonic-gate 
5597c478bd9Sstevel@tonic-gate static void
5607c478bd9Sstevel@tonic-gate cb_compound(CB_COMPOUND4args *args, CB_COMPOUND4res *resp, struct svc_req *req,
5617c478bd9Sstevel@tonic-gate 	struct nfs4_callback_globals *ncg)
5627c478bd9Sstevel@tonic-gate {
5637c478bd9Sstevel@tonic-gate 	uint_t i;
5647c478bd9Sstevel@tonic-gate 	struct compound_state cs;
5657c478bd9Sstevel@tonic-gate 	nfs_cb_argop4 *argop;
5667c478bd9Sstevel@tonic-gate 	nfs_cb_resop4 *resop, *new_res;
5677c478bd9Sstevel@tonic-gate 	uint_t op;
5687c478bd9Sstevel@tonic-gate 
5697c478bd9Sstevel@tonic-gate 	bzero(&cs, sizeof (cs));
5707c478bd9Sstevel@tonic-gate 	cs.statusp = &resp->status;
5717c478bd9Sstevel@tonic-gate 	cs.cont = TRUE;
5727c478bd9Sstevel@tonic-gate 
5737c478bd9Sstevel@tonic-gate 	/*
5747c478bd9Sstevel@tonic-gate 	 * Form a reply tag by copying over the reqeuest tag.
5757c478bd9Sstevel@tonic-gate 	 */
5767c478bd9Sstevel@tonic-gate 	resp->tag.utf8string_len = args->tag.utf8string_len;
5777c478bd9Sstevel@tonic-gate 	resp->tag.utf8string_val = kmem_alloc(resp->tag.utf8string_len,
5787c478bd9Sstevel@tonic-gate 	    KM_SLEEP);
5797c478bd9Sstevel@tonic-gate 	bcopy(args->tag.utf8string_val, resp->tag.utf8string_val,
5807c478bd9Sstevel@tonic-gate 	    args->tag.utf8string_len);
5817c478bd9Sstevel@tonic-gate 
5827c478bd9Sstevel@tonic-gate 	/*
5837c478bd9Sstevel@tonic-gate 	 * XXX for now, minorversion should be zero
5847c478bd9Sstevel@tonic-gate 	 */
5857c478bd9Sstevel@tonic-gate 	if (args->minorversion != CB4_MINORVERSION) {
5867c478bd9Sstevel@tonic-gate 		resp->array_len = 0;
5877c478bd9Sstevel@tonic-gate 		resp->array = NULL;
5887c478bd9Sstevel@tonic-gate 		resp->status = NFS4ERR_MINOR_VERS_MISMATCH;
5897c478bd9Sstevel@tonic-gate 		return;
5907c478bd9Sstevel@tonic-gate 	}
5917c478bd9Sstevel@tonic-gate 
5927c478bd9Sstevel@tonic-gate #ifdef DEBUG
5937c478bd9Sstevel@tonic-gate 	/*
5947c478bd9Sstevel@tonic-gate 	 * Verify callback_ident.  It doesn't really matter if it's wrong
5957c478bd9Sstevel@tonic-gate 	 * because we don't really use callback_ident -- we use prog number
5967c478bd9Sstevel@tonic-gate 	 * of the RPC request instead.  In this case, just print a DEBUG
5977c478bd9Sstevel@tonic-gate 	 * console message to reveal brokenness of cbclient (at bkoff/cthon).
5987c478bd9Sstevel@tonic-gate 	 */
5997c478bd9Sstevel@tonic-gate 	if (args->callback_ident != req->rq_prog)
6007c478bd9Sstevel@tonic-gate 		zcmn_err(getzoneid(), CE_WARN,
6017c478bd9Sstevel@tonic-gate 		    "cb_compound: cb_client using wrong "
6027c478bd9Sstevel@tonic-gate 		    "callback_ident(%d), should be %d",
6037c478bd9Sstevel@tonic-gate 		    args->callback_ident, req->rq_prog);
6047c478bd9Sstevel@tonic-gate #endif
6057c478bd9Sstevel@tonic-gate 
6067c478bd9Sstevel@tonic-gate 	resp->array_len = args->array_len;
6077c478bd9Sstevel@tonic-gate 	resp->array = kmem_zalloc(args->array_len * sizeof (nfs_cb_resop4),
6087c478bd9Sstevel@tonic-gate 	    KM_SLEEP);
6097c478bd9Sstevel@tonic-gate 
6107c478bd9Sstevel@tonic-gate 	for (i = 0; i < args->array_len && cs.cont; i++) {
6117c478bd9Sstevel@tonic-gate 
6127c478bd9Sstevel@tonic-gate 		argop = &args->array[i];
6137c478bd9Sstevel@tonic-gate 		resop = &resp->array[i];
6147c478bd9Sstevel@tonic-gate 		resop->resop = argop->argop;
6157c478bd9Sstevel@tonic-gate 		op = (uint_t)resop->resop;
6167c478bd9Sstevel@tonic-gate 
6177c478bd9Sstevel@tonic-gate 		switch (op) {
6187c478bd9Sstevel@tonic-gate 
6197c478bd9Sstevel@tonic-gate 		case OP_CB_GETATTR:
6207c478bd9Sstevel@tonic-gate 
6217c478bd9Sstevel@tonic-gate 			cb_getattr(argop, resop, req, &cs, ncg);
6227c478bd9Sstevel@tonic-gate 			break;
6237c478bd9Sstevel@tonic-gate 
6247c478bd9Sstevel@tonic-gate 		case OP_CB_RECALL:
6257c478bd9Sstevel@tonic-gate 
6267c478bd9Sstevel@tonic-gate 			cb_recall(argop, resop, req, &cs, ncg);
6277c478bd9Sstevel@tonic-gate 			break;
6287c478bd9Sstevel@tonic-gate 
6297c478bd9Sstevel@tonic-gate 		case OP_CB_ILLEGAL:
6307c478bd9Sstevel@tonic-gate 
6317c478bd9Sstevel@tonic-gate 			/* fall through */
6327c478bd9Sstevel@tonic-gate 
6337c478bd9Sstevel@tonic-gate 		default:
6347c478bd9Sstevel@tonic-gate 			/*
6357c478bd9Sstevel@tonic-gate 			 * Handle OP_CB_ILLEGAL and any undefined opcode.
6367c478bd9Sstevel@tonic-gate 			 * Currently, the XDR code will return BADXDR
6377c478bd9Sstevel@tonic-gate 			 * if cb op doesn't decode to legal value, so
6387c478bd9Sstevel@tonic-gate 			 * it really only handles OP_CB_ILLEGAL.
6397c478bd9Sstevel@tonic-gate 			 */
6407c478bd9Sstevel@tonic-gate 			op = OP_CB_ILLEGAL;
6417c478bd9Sstevel@tonic-gate 			cb_illegal(argop, resop, req, &cs, ncg);
6427c478bd9Sstevel@tonic-gate 		}
6437c478bd9Sstevel@tonic-gate 
6447c478bd9Sstevel@tonic-gate 		if (*cs.statusp != NFS4_OK)
6457c478bd9Sstevel@tonic-gate 			cs.cont = FALSE;
6467c478bd9Sstevel@tonic-gate 
6477c478bd9Sstevel@tonic-gate 		/*
6487c478bd9Sstevel@tonic-gate 		 * If not at last op, and if we are to stop, then
6497c478bd9Sstevel@tonic-gate 		 * compact the results array.
6507c478bd9Sstevel@tonic-gate 		 */
6517c478bd9Sstevel@tonic-gate 		if ((i + 1) < args->array_len && !cs.cont) {
6527c478bd9Sstevel@tonic-gate 
6537c478bd9Sstevel@tonic-gate 			new_res = kmem_alloc(
6547c478bd9Sstevel@tonic-gate 			    (i+1) * sizeof (nfs_cb_resop4), KM_SLEEP);
6557c478bd9Sstevel@tonic-gate 			bcopy(resp->array,
6567c478bd9Sstevel@tonic-gate 			    new_res, (i+1) * sizeof (nfs_cb_resop4));
6577c478bd9Sstevel@tonic-gate 			kmem_free(resp->array,
6587c478bd9Sstevel@tonic-gate 			    args->array_len * sizeof (nfs_cb_resop4));
6597c478bd9Sstevel@tonic-gate 
6607c478bd9Sstevel@tonic-gate 			resp->array_len =  i + 1;
6617c478bd9Sstevel@tonic-gate 			resp->array = new_res;
6627c478bd9Sstevel@tonic-gate 		}
6637c478bd9Sstevel@tonic-gate 	}
6647c478bd9Sstevel@tonic-gate 
6657c478bd9Sstevel@tonic-gate }
6667c478bd9Sstevel@tonic-gate 
6677c478bd9Sstevel@tonic-gate static void
6687c478bd9Sstevel@tonic-gate cb_compound_free(CB_COMPOUND4res *resp)
6697c478bd9Sstevel@tonic-gate {
6707c478bd9Sstevel@tonic-gate 	uint_t i, op;
6717c478bd9Sstevel@tonic-gate 	nfs_cb_resop4 *resop;
6727c478bd9Sstevel@tonic-gate 
6737c478bd9Sstevel@tonic-gate 	if (resp->tag.utf8string_val) {
6747c478bd9Sstevel@tonic-gate 		UTF8STRING_FREE(resp->tag)
6757c478bd9Sstevel@tonic-gate 	}
6767c478bd9Sstevel@tonic-gate 
6777c478bd9Sstevel@tonic-gate 	for (i = 0; i < resp->array_len; i++) {
6787c478bd9Sstevel@tonic-gate 
6797c478bd9Sstevel@tonic-gate 		resop = &resp->array[i];
6807c478bd9Sstevel@tonic-gate 		op = (uint_t)resop->resop;
6817c478bd9Sstevel@tonic-gate 
6827c478bd9Sstevel@tonic-gate 		switch (op) {
6837c478bd9Sstevel@tonic-gate 
6847c478bd9Sstevel@tonic-gate 		case OP_CB_GETATTR:
6857c478bd9Sstevel@tonic-gate 
6867c478bd9Sstevel@tonic-gate 			cb_getattr_free(resop);
6877c478bd9Sstevel@tonic-gate 			break;
6887c478bd9Sstevel@tonic-gate 
6897c478bd9Sstevel@tonic-gate 		case OP_CB_RECALL:
6907c478bd9Sstevel@tonic-gate 
6917c478bd9Sstevel@tonic-gate 			cb_recall_free(resop);
6927c478bd9Sstevel@tonic-gate 			break;
6937c478bd9Sstevel@tonic-gate 
6947c478bd9Sstevel@tonic-gate 		default:
6957c478bd9Sstevel@tonic-gate 			break;
6967c478bd9Sstevel@tonic-gate 		}
6977c478bd9Sstevel@tonic-gate 	}
6987c478bd9Sstevel@tonic-gate 
6997c478bd9Sstevel@tonic-gate 	if (resp->array != NULL) {
7007c478bd9Sstevel@tonic-gate 		kmem_free(resp->array,
7017c478bd9Sstevel@tonic-gate 		    resp->array_len * sizeof (nfs_cb_resop4));
7027c478bd9Sstevel@tonic-gate 	}
7037c478bd9Sstevel@tonic-gate }
7047c478bd9Sstevel@tonic-gate 
7057c478bd9Sstevel@tonic-gate static void
7067c478bd9Sstevel@tonic-gate cb_dispatch(struct svc_req *req, SVCXPRT *xprt)
7077c478bd9Sstevel@tonic-gate {
7087c478bd9Sstevel@tonic-gate 	CB_COMPOUND4args args;
7097c478bd9Sstevel@tonic-gate 	CB_COMPOUND4res res;
7107c478bd9Sstevel@tonic-gate 	struct nfs4_callback_globals *ncg;
7117c478bd9Sstevel@tonic-gate 
7127c478bd9Sstevel@tonic-gate 	bool_t (*xdr_args)(), (*xdr_res)();
7137c478bd9Sstevel@tonic-gate 	void (*proc)(CB_COMPOUND4args *, CB_COMPOUND4res *, struct svc_req *,
7147c478bd9Sstevel@tonic-gate 	    struct nfs4_callback_globals *);
7157c478bd9Sstevel@tonic-gate 	void (*freeproc)(CB_COMPOUND4res *);
7167c478bd9Sstevel@tonic-gate 
717108322fbScarlsonj 	ncg = zone_getspecific(nfs4_callback_zone_key, nfs_zone());
7187c478bd9Sstevel@tonic-gate 	ASSERT(ncg != NULL);
7197c478bd9Sstevel@tonic-gate 
7207c478bd9Sstevel@tonic-gate 	ncg->nfs4_callback_stats.cb_dispatch.value.ui64++;
7217c478bd9Sstevel@tonic-gate 
7227c478bd9Sstevel@tonic-gate 	switch (req->rq_proc) {
7237c478bd9Sstevel@tonic-gate 	case CB_NULL:
7247c478bd9Sstevel@tonic-gate 		xdr_args = xdr_void;
7257c478bd9Sstevel@tonic-gate 		xdr_res = xdr_void;
7267c478bd9Sstevel@tonic-gate 		proc = cb_null;
7277c478bd9Sstevel@tonic-gate 		freeproc = NULL;
7287c478bd9Sstevel@tonic-gate 		break;
7297c478bd9Sstevel@tonic-gate 
7307c478bd9Sstevel@tonic-gate 	case CB_COMPOUND:
731eac3aab7Srobinson 		xdr_args = xdr_CB_COMPOUND4args_clnt;
7327c478bd9Sstevel@tonic-gate 		xdr_res = xdr_CB_COMPOUND4res;
7337c478bd9Sstevel@tonic-gate 		proc = cb_compound;
7347c478bd9Sstevel@tonic-gate 		freeproc = cb_compound_free;
7357c478bd9Sstevel@tonic-gate 		break;
7367c478bd9Sstevel@tonic-gate 
7377c478bd9Sstevel@tonic-gate 	default:
7387c478bd9Sstevel@tonic-gate 		CB_WARN("cb_dispatch: no proc\n");
7397c478bd9Sstevel@tonic-gate 		svcerr_noproc(xprt);
7407c478bd9Sstevel@tonic-gate 		return;
7417c478bd9Sstevel@tonic-gate 	}
7427c478bd9Sstevel@tonic-gate 
7437c478bd9Sstevel@tonic-gate 	args.tag.utf8string_val = NULL;
7447c478bd9Sstevel@tonic-gate 	args.array = NULL;
7457c478bd9Sstevel@tonic-gate 
7467c478bd9Sstevel@tonic-gate 	if (!SVC_GETARGS(xprt, xdr_args, (caddr_t)&args)) {
7477c478bd9Sstevel@tonic-gate 
7487c478bd9Sstevel@tonic-gate 		CB_WARN("cb_dispatch: cannot getargs\n");
7497c478bd9Sstevel@tonic-gate 		svcerr_decode(xprt);
7507c478bd9Sstevel@tonic-gate 		return;
7517c478bd9Sstevel@tonic-gate 	}
7527c478bd9Sstevel@tonic-gate 
7537c478bd9Sstevel@tonic-gate 	(*proc)(&args, &res, req, ncg);
7547c478bd9Sstevel@tonic-gate 
7557c478bd9Sstevel@tonic-gate 	if (svc_sendreply(xprt, xdr_res, (caddr_t)&res) == FALSE) {
7567c478bd9Sstevel@tonic-gate 
7577c478bd9Sstevel@tonic-gate 		CB_WARN("cb_dispatch: bad sendreply\n");
75871c6e709Srmesta 		svcerr_systemerr(xprt);
7597c478bd9Sstevel@tonic-gate 	}
7607c478bd9Sstevel@tonic-gate 
7617c478bd9Sstevel@tonic-gate 	if (freeproc)
7627c478bd9Sstevel@tonic-gate 		(*freeproc)(&res);
7637c478bd9Sstevel@tonic-gate 
7647c478bd9Sstevel@tonic-gate 	if (!SVC_FREEARGS(xprt, xdr_args, (caddr_t)&args)) {
7657c478bd9Sstevel@tonic-gate 
7667c478bd9Sstevel@tonic-gate 		CB_WARN("cb_dispatch: bad freeargs\n");
7677c478bd9Sstevel@tonic-gate 	}
7687c478bd9Sstevel@tonic-gate }
7697c478bd9Sstevel@tonic-gate 
7707c478bd9Sstevel@tonic-gate static rpcprog_t
7717c478bd9Sstevel@tonic-gate nfs4_getnextprogram(struct nfs4_callback_globals *ncg)
7727c478bd9Sstevel@tonic-gate {
7737c478bd9Sstevel@tonic-gate 	int i, j;
7747c478bd9Sstevel@tonic-gate 
7757c478bd9Sstevel@tonic-gate 	j = ncg->nfs4_program_hint;
7767c478bd9Sstevel@tonic-gate 	for (i = 0; i < nfs4_num_prognums; i++, j++) {
7777c478bd9Sstevel@tonic-gate 
7787c478bd9Sstevel@tonic-gate 		if (j >= nfs4_num_prognums)
7797c478bd9Sstevel@tonic-gate 			j = 0;
7807c478bd9Sstevel@tonic-gate 
7817c478bd9Sstevel@tonic-gate 		if (ncg->nfs4prog2server[j] == NULL) {
7827c478bd9Sstevel@tonic-gate 			ncg->nfs4_program_hint = j+1;
7837c478bd9Sstevel@tonic-gate 			return (j+NFS4_CALLBACK);
7847c478bd9Sstevel@tonic-gate 		}
7857c478bd9Sstevel@tonic-gate 	}
7867c478bd9Sstevel@tonic-gate 
7877c478bd9Sstevel@tonic-gate 	return (0);
7887c478bd9Sstevel@tonic-gate }
7897c478bd9Sstevel@tonic-gate 
7907c478bd9Sstevel@tonic-gate void
7917c478bd9Sstevel@tonic-gate nfs4callback_destroy(nfs4_server_t *np)
7927c478bd9Sstevel@tonic-gate {
7937c478bd9Sstevel@tonic-gate 	struct nfs4_callback_globals *ncg;
7947c478bd9Sstevel@tonic-gate 	int i;
7957c478bd9Sstevel@tonic-gate 
7967c478bd9Sstevel@tonic-gate 	if (np->s_program == 0)
7977c478bd9Sstevel@tonic-gate 		return;
7987c478bd9Sstevel@tonic-gate 
7997c478bd9Sstevel@tonic-gate 	ncg = np->zone_globals;
8007c478bd9Sstevel@tonic-gate 	i = np->s_program - NFS4_CALLBACK;
8017c478bd9Sstevel@tonic-gate 
8027c478bd9Sstevel@tonic-gate 	mutex_enter(&ncg->nfs4_cb_lock);
8037c478bd9Sstevel@tonic-gate 
8047c478bd9Sstevel@tonic-gate 	ASSERT(ncg->nfs4prog2server[i] == np);
8057c478bd9Sstevel@tonic-gate 
8067c478bd9Sstevel@tonic-gate 	ncg->nfs4prog2server[i] = NULL;
8077c478bd9Sstevel@tonic-gate 
8087c478bd9Sstevel@tonic-gate 	if (i < ncg->nfs4_program_hint)
8097c478bd9Sstevel@tonic-gate 		ncg->nfs4_program_hint = i;
8107c478bd9Sstevel@tonic-gate 
8117c478bd9Sstevel@tonic-gate 	mutex_exit(&ncg->nfs4_cb_lock);
8127c478bd9Sstevel@tonic-gate }
8137c478bd9Sstevel@tonic-gate 
8147c478bd9Sstevel@tonic-gate /*
8157c478bd9Sstevel@tonic-gate  * nfs4_setport - This function saves a netid and univeral address for
8167c478bd9Sstevel@tonic-gate  * the callback program.  These values will be used during setclientid.
8177c478bd9Sstevel@tonic-gate  */
8187c478bd9Sstevel@tonic-gate static void
8197c478bd9Sstevel@tonic-gate nfs4_setport(char *netid, char *uaddr, char *protofmly, char *proto,
8207c478bd9Sstevel@tonic-gate 	struct nfs4_callback_globals *ncg)
8217c478bd9Sstevel@tonic-gate {
8227c478bd9Sstevel@tonic-gate 	struct nfs4_cb_port *p;
8237c478bd9Sstevel@tonic-gate 	bool_t found = FALSE;
8247c478bd9Sstevel@tonic-gate 
8257c478bd9Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&ncg->nfs4_cb_lock));
8267c478bd9Sstevel@tonic-gate 
8277c478bd9Sstevel@tonic-gate 	p = list_head(&ncg->nfs4_cb_ports);
8287c478bd9Sstevel@tonic-gate 	for (; p != NULL; p = list_next(&ncg->nfs4_cb_ports, p)) {
8297c478bd9Sstevel@tonic-gate 		if (strcmp(p->netid, netid) == 0) {
8307c478bd9Sstevel@tonic-gate 			found = TRUE;
8317c478bd9Sstevel@tonic-gate 			break;
8327c478bd9Sstevel@tonic-gate 		}
8337c478bd9Sstevel@tonic-gate 	}
8347c478bd9Sstevel@tonic-gate 	if (found == TRUE)
8357c478bd9Sstevel@tonic-gate 		(void) strcpy(p->uaddr, uaddr);
8367c478bd9Sstevel@tonic-gate 	else {
8377c478bd9Sstevel@tonic-gate 		p = kmem_alloc(sizeof (*p), KM_SLEEP);
8387c478bd9Sstevel@tonic-gate 
8397c478bd9Sstevel@tonic-gate 		(void) strcpy(p->uaddr, uaddr);
8407c478bd9Sstevel@tonic-gate 		(void) strcpy(p->netid, netid);
8417c478bd9Sstevel@tonic-gate 		(void) strcpy(p->protofmly, protofmly);
8427c478bd9Sstevel@tonic-gate 		(void) strcpy(p->proto, proto);
8437c478bd9Sstevel@tonic-gate 		list_insert_head(&ncg->nfs4_cb_ports, p);
8447c478bd9Sstevel@tonic-gate 	}
8457c478bd9Sstevel@tonic-gate }
8467c478bd9Sstevel@tonic-gate 
8477c478bd9Sstevel@tonic-gate /*
8487c478bd9Sstevel@tonic-gate  * nfs4_cb_args - This function is used to construct the callback
8497c478bd9Sstevel@tonic-gate  * portion of the arguments needed for setclientid.
8507c478bd9Sstevel@tonic-gate  */
8517c478bd9Sstevel@tonic-gate 
8527c478bd9Sstevel@tonic-gate void
8537c478bd9Sstevel@tonic-gate nfs4_cb_args(nfs4_server_t *np, struct knetconfig *knc, SETCLIENTID4args *args)
8547c478bd9Sstevel@tonic-gate {
8557c478bd9Sstevel@tonic-gate 	struct nfs4_cb_port *p;
8567c478bd9Sstevel@tonic-gate 	bool_t found = FALSE;
8577c478bd9Sstevel@tonic-gate 	rpcprog_t pgm;
8587c478bd9Sstevel@tonic-gate 	struct nfs4_callback_globals *ncg = np->zone_globals;
8597c478bd9Sstevel@tonic-gate 
8607c478bd9Sstevel@tonic-gate 	/*
8617c478bd9Sstevel@tonic-gate 	 * This server structure may already have a program number
8627c478bd9Sstevel@tonic-gate 	 * assigned to it.  This happens when the client has to
8637c478bd9Sstevel@tonic-gate 	 * re-issue SETCLIENTID.  Just re-use the information.
8647c478bd9Sstevel@tonic-gate 	 */
8657c478bd9Sstevel@tonic-gate 	if (np->s_program >= NFS4_CALLBACK &&
8667c478bd9Sstevel@tonic-gate 	    np->s_program < NFS4_CALLBACK + nfs4_num_prognums)
8677c478bd9Sstevel@tonic-gate 		nfs4callback_destroy(np);
8687c478bd9Sstevel@tonic-gate 
8697c478bd9Sstevel@tonic-gate 	mutex_enter(&ncg->nfs4_cb_lock);
8707c478bd9Sstevel@tonic-gate 
8717c478bd9Sstevel@tonic-gate 	p = list_head(&ncg->nfs4_cb_ports);
8727c478bd9Sstevel@tonic-gate 	for (; p != NULL; p = list_next(&ncg->nfs4_cb_ports, p)) {
8737c478bd9Sstevel@tonic-gate 		if (strcmp(p->protofmly, knc->knc_protofmly) == 0 &&
8747c478bd9Sstevel@tonic-gate 		    strcmp(p->proto, knc->knc_proto) == 0) {
8757c478bd9Sstevel@tonic-gate 			found = TRUE;
8767c478bd9Sstevel@tonic-gate 			break;
8777c478bd9Sstevel@tonic-gate 		}
8787c478bd9Sstevel@tonic-gate 	}
8797c478bd9Sstevel@tonic-gate 
8807c478bd9Sstevel@tonic-gate 	if (found == FALSE) {
8817c478bd9Sstevel@tonic-gate 
8827c478bd9Sstevel@tonic-gate 		NFS4_DEBUG(nfs4_callback_debug,
8837c478bd9Sstevel@tonic-gate 		    (CE_WARN, "nfs4_cb_args: could not find netid for %s/%s\n",
8847c478bd9Sstevel@tonic-gate 		    knc->knc_protofmly, knc->knc_proto));
8857c478bd9Sstevel@tonic-gate 
8867c478bd9Sstevel@tonic-gate 		args->callback.cb_program = 0;
8877c478bd9Sstevel@tonic-gate 		args->callback.cb_location.r_netid = NULL;
8887c478bd9Sstevel@tonic-gate 		args->callback.cb_location.r_addr = NULL;
8897c478bd9Sstevel@tonic-gate 		args->callback_ident = 0;
8907c478bd9Sstevel@tonic-gate 		mutex_exit(&ncg->nfs4_cb_lock);
8917c478bd9Sstevel@tonic-gate 		return;
8927c478bd9Sstevel@tonic-gate 	}
8937c478bd9Sstevel@tonic-gate 
8947c478bd9Sstevel@tonic-gate 	if ((pgm = nfs4_getnextprogram(ncg)) == 0) {
8957c478bd9Sstevel@tonic-gate 		CB_WARN("nfs4_cb_args: out of program numbers\n");
8967c478bd9Sstevel@tonic-gate 
8977c478bd9Sstevel@tonic-gate 		args->callback.cb_program = 0;
8987c478bd9Sstevel@tonic-gate 		args->callback.cb_location.r_netid = NULL;
8997c478bd9Sstevel@tonic-gate 		args->callback.cb_location.r_addr = NULL;
9007c478bd9Sstevel@tonic-gate 		args->callback_ident = 0;
9017c478bd9Sstevel@tonic-gate 		mutex_exit(&ncg->nfs4_cb_lock);
9027c478bd9Sstevel@tonic-gate 		return;
9037c478bd9Sstevel@tonic-gate 	}
9047c478bd9Sstevel@tonic-gate 
9057c478bd9Sstevel@tonic-gate 	ncg->nfs4prog2server[pgm-NFS4_CALLBACK] = np;
9067c478bd9Sstevel@tonic-gate 	args->callback.cb_program = pgm;
9077c478bd9Sstevel@tonic-gate 	args->callback.cb_location.r_netid = p->netid;
9087c478bd9Sstevel@tonic-gate 	args->callback.cb_location.r_addr = p->uaddr;
9097c478bd9Sstevel@tonic-gate 	args->callback_ident = pgm;
9107c478bd9Sstevel@tonic-gate 
9117c478bd9Sstevel@tonic-gate 	np->s_program = pgm;
9127c478bd9Sstevel@tonic-gate 
9137c478bd9Sstevel@tonic-gate 	mutex_exit(&ncg->nfs4_cb_lock);
9147c478bd9Sstevel@tonic-gate }
9157c478bd9Sstevel@tonic-gate 
9167c478bd9Sstevel@tonic-gate static int
9177c478bd9Sstevel@tonic-gate nfs4_dquery(struct nfs4_svc_args *arg, model_t model)
9187c478bd9Sstevel@tonic-gate {
9197c478bd9Sstevel@tonic-gate 	file_t *fp;
9207c478bd9Sstevel@tonic-gate 	vnode_t *vp;
9217c478bd9Sstevel@tonic-gate 	rnode4_t *rp;
9227c478bd9Sstevel@tonic-gate 	int error;
9237c478bd9Sstevel@tonic-gate 	STRUCT_HANDLE(nfs4_svc_args, uap);
9247c478bd9Sstevel@tonic-gate 
9257c478bd9Sstevel@tonic-gate 	STRUCT_SET_HANDLE(uap, model, arg);
9267c478bd9Sstevel@tonic-gate 
9277c478bd9Sstevel@tonic-gate 	if ((fp = getf(STRUCT_FGET(uap, fd))) == NULL)
9287c478bd9Sstevel@tonic-gate 		return (EBADF);
9297c478bd9Sstevel@tonic-gate 
9307c478bd9Sstevel@tonic-gate 	vp = fp->f_vnode;
9317c478bd9Sstevel@tonic-gate 
9327c478bd9Sstevel@tonic-gate 	if (vp == NULL || vp->v_type != VREG ||
9337c478bd9Sstevel@tonic-gate 	    !vn_matchops(vp, nfs4_vnodeops)) {
9347c478bd9Sstevel@tonic-gate 		releasef(STRUCT_FGET(uap, fd));
9357c478bd9Sstevel@tonic-gate 		return (EBADF);
9367c478bd9Sstevel@tonic-gate 	}
9377c478bd9Sstevel@tonic-gate 
9387c478bd9Sstevel@tonic-gate 	rp = VTOR4(vp);
9397c478bd9Sstevel@tonic-gate 
9407c478bd9Sstevel@tonic-gate 	/*
9417c478bd9Sstevel@tonic-gate 	 * I can't convince myself that we need locking here.  The
9427c478bd9Sstevel@tonic-gate 	 * rnode cannot disappear and the value returned is instantly
9437c478bd9Sstevel@tonic-gate 	 * stale anway, so why bother?
9447c478bd9Sstevel@tonic-gate 	 */
9457c478bd9Sstevel@tonic-gate 
9467c478bd9Sstevel@tonic-gate 	error = suword32(STRUCT_FGETP(uap, netid), rp->r_deleg_type);
9477c478bd9Sstevel@tonic-gate 	releasef(STRUCT_FGET(uap, fd));
9487c478bd9Sstevel@tonic-gate 	return (error);
9497c478bd9Sstevel@tonic-gate }
9507c478bd9Sstevel@tonic-gate 
9517c478bd9Sstevel@tonic-gate 
9527c478bd9Sstevel@tonic-gate /*
9537c478bd9Sstevel@tonic-gate  * NFS4 client system call.  This service does the
9547c478bd9Sstevel@tonic-gate  * necessary initialization for the callback program.
9557c478bd9Sstevel@tonic-gate  * This is fashioned after the server side interaction
9567c478bd9Sstevel@tonic-gate  * between nfsd and the kernel.  On the client, the
9577c478bd9Sstevel@tonic-gate  * mount command forks and the child process does the
9587c478bd9Sstevel@tonic-gate  * necessary interaction with the kernel.
9597c478bd9Sstevel@tonic-gate  *
9607c478bd9Sstevel@tonic-gate  * uap->fd is the fd of an open transport provider
9617c478bd9Sstevel@tonic-gate  */
9627c478bd9Sstevel@tonic-gate int
9637c478bd9Sstevel@tonic-gate nfs4_svc(struct nfs4_svc_args *arg, model_t model)
9647c478bd9Sstevel@tonic-gate {
9657c478bd9Sstevel@tonic-gate 	file_t *fp;
9667c478bd9Sstevel@tonic-gate 	int error;
9677c478bd9Sstevel@tonic-gate 	int readsize;
9687c478bd9Sstevel@tonic-gate 	char buf[KNC_STRSIZE], uaddr[KNC_STRSIZE];
9697c478bd9Sstevel@tonic-gate 	char protofmly[KNC_STRSIZE], proto[KNC_STRSIZE];
9707c478bd9Sstevel@tonic-gate 	size_t len;
9717c478bd9Sstevel@tonic-gate 	STRUCT_HANDLE(nfs4_svc_args, uap);
9727c478bd9Sstevel@tonic-gate 	struct netbuf addrmask;
9737c478bd9Sstevel@tonic-gate 	int cmd;
9747c478bd9Sstevel@tonic-gate 	SVCMASTERXPRT *cb_xprt;
9757c478bd9Sstevel@tonic-gate 	struct nfs4_callback_globals *ncg;
9767c478bd9Sstevel@tonic-gate 
9777c478bd9Sstevel@tonic-gate #ifdef lint
9787c478bd9Sstevel@tonic-gate 	model = model;		/* STRUCT macros don't always refer to it */
9797c478bd9Sstevel@tonic-gate #endif
9807c478bd9Sstevel@tonic-gate 
9817c478bd9Sstevel@tonic-gate 	STRUCT_SET_HANDLE(uap, model, arg);
9827c478bd9Sstevel@tonic-gate 
9837c478bd9Sstevel@tonic-gate 	if (STRUCT_FGET(uap, cmd) == NFS4_DQUERY)
9847c478bd9Sstevel@tonic-gate 		return (nfs4_dquery(arg, model));
9857c478bd9Sstevel@tonic-gate 
9867c478bd9Sstevel@tonic-gate 	if (secpolicy_nfs(CRED()) != 0)
9877c478bd9Sstevel@tonic-gate 		return (EPERM);
9887c478bd9Sstevel@tonic-gate 
9897c478bd9Sstevel@tonic-gate 	if ((fp = getf(STRUCT_FGET(uap, fd))) == NULL)
9907c478bd9Sstevel@tonic-gate 		return (EBADF);
9917c478bd9Sstevel@tonic-gate 
9927c478bd9Sstevel@tonic-gate 	/*
9937c478bd9Sstevel@tonic-gate 	 * Set read buffer size to rsize
9947c478bd9Sstevel@tonic-gate 	 * and add room for RPC headers.
9957c478bd9Sstevel@tonic-gate 	 */
9967c478bd9Sstevel@tonic-gate 	readsize = nfs3tsize() + (RPC_MAXDATASIZE - NFS_MAXDATA);
9977c478bd9Sstevel@tonic-gate 	if (readsize < RPC_MAXDATASIZE)
9987c478bd9Sstevel@tonic-gate 		readsize = RPC_MAXDATASIZE;
9997c478bd9Sstevel@tonic-gate 
10007c478bd9Sstevel@tonic-gate 	error = copyinstr((const char *)STRUCT_FGETP(uap, netid), buf,
10017c478bd9Sstevel@tonic-gate 	    KNC_STRSIZE, &len);
10027c478bd9Sstevel@tonic-gate 	if (error) {
10037c478bd9Sstevel@tonic-gate 		releasef(STRUCT_FGET(uap, fd));
10047c478bd9Sstevel@tonic-gate 		return (error);
10057c478bd9Sstevel@tonic-gate 	}
10067c478bd9Sstevel@tonic-gate 
10077c478bd9Sstevel@tonic-gate 	cmd = STRUCT_FGET(uap, cmd);
10087c478bd9Sstevel@tonic-gate 
10097c478bd9Sstevel@tonic-gate 	if (cmd & NFS4_KRPC_START) {
10107c478bd9Sstevel@tonic-gate 		addrmask.len = STRUCT_FGET(uap, addrmask.len);
10117c478bd9Sstevel@tonic-gate 		addrmask.maxlen = STRUCT_FGET(uap, addrmask.maxlen);
10127c478bd9Sstevel@tonic-gate 		addrmask.buf = kmem_alloc(addrmask.maxlen, KM_SLEEP);
10137c478bd9Sstevel@tonic-gate 		error = copyin(STRUCT_FGETP(uap, addrmask.buf), addrmask.buf,
10147c478bd9Sstevel@tonic-gate 		    addrmask.len);
10157c478bd9Sstevel@tonic-gate 		if (error) {
10167c478bd9Sstevel@tonic-gate 			releasef(STRUCT_FGET(uap, fd));
10177c478bd9Sstevel@tonic-gate 			kmem_free(addrmask.buf, addrmask.maxlen);
10187c478bd9Sstevel@tonic-gate 			return (error);
10197c478bd9Sstevel@tonic-gate 		}
10207c478bd9Sstevel@tonic-gate 	}
10217c478bd9Sstevel@tonic-gate 	else
10227c478bd9Sstevel@tonic-gate 		addrmask.buf = NULL;
10237c478bd9Sstevel@tonic-gate 
10247c478bd9Sstevel@tonic-gate 	error = copyinstr((const char *)STRUCT_FGETP(uap, addr), uaddr,
10257c478bd9Sstevel@tonic-gate 	    sizeof (uaddr), &len);
10267c478bd9Sstevel@tonic-gate 	if (error) {
10277c478bd9Sstevel@tonic-gate 		releasef(STRUCT_FGET(uap, fd));
10287c478bd9Sstevel@tonic-gate 		if (addrmask.buf)
10297c478bd9Sstevel@tonic-gate 			kmem_free(addrmask.buf, addrmask.maxlen);
10307c478bd9Sstevel@tonic-gate 		return (error);
10317c478bd9Sstevel@tonic-gate 	}
10327c478bd9Sstevel@tonic-gate 
10337c478bd9Sstevel@tonic-gate 	error = copyinstr((const char *)STRUCT_FGETP(uap, protofmly), protofmly,
10347c478bd9Sstevel@tonic-gate 	    sizeof (protofmly), &len);
10357c478bd9Sstevel@tonic-gate 	if (error) {
10367c478bd9Sstevel@tonic-gate 		releasef(STRUCT_FGET(uap, fd));
10377c478bd9Sstevel@tonic-gate 		if (addrmask.buf)
10387c478bd9Sstevel@tonic-gate 			kmem_free(addrmask.buf, addrmask.maxlen);
10397c478bd9Sstevel@tonic-gate 		return (error);
10407c478bd9Sstevel@tonic-gate 	}
10417c478bd9Sstevel@tonic-gate 
10427c478bd9Sstevel@tonic-gate 	error = copyinstr((const char *)STRUCT_FGETP(uap, proto), proto,
10437c478bd9Sstevel@tonic-gate 	    sizeof (proto), &len);
10447c478bd9Sstevel@tonic-gate 	if (error) {
10457c478bd9Sstevel@tonic-gate 		releasef(STRUCT_FGET(uap, fd));
10467c478bd9Sstevel@tonic-gate 		if (addrmask.buf)
10477c478bd9Sstevel@tonic-gate 			kmem_free(addrmask.buf, addrmask.maxlen);
10487c478bd9Sstevel@tonic-gate 		return (error);
10497c478bd9Sstevel@tonic-gate 	}
10507c478bd9Sstevel@tonic-gate 
1051108322fbScarlsonj 	ncg = zone_getspecific(nfs4_callback_zone_key, nfs_zone());
10527c478bd9Sstevel@tonic-gate 	ASSERT(ncg != NULL);
10537c478bd9Sstevel@tonic-gate 
10547c478bd9Sstevel@tonic-gate 	mutex_enter(&ncg->nfs4_cb_lock);
10557c478bd9Sstevel@tonic-gate 	if (cmd & NFS4_SETPORT)
10567c478bd9Sstevel@tonic-gate 		nfs4_setport(buf, uaddr, protofmly, proto, ncg);
10577c478bd9Sstevel@tonic-gate 
10587c478bd9Sstevel@tonic-gate 	if (cmd & NFS4_KRPC_START) {
10597c478bd9Sstevel@tonic-gate 		error = svc_tli_kcreate(fp, readsize, buf, &addrmask, &cb_xprt,
10607c478bd9Sstevel@tonic-gate 		    &nfs4_cb_sct, NULL, NFS_CB_SVCPOOL_ID, FALSE);
10617c478bd9Sstevel@tonic-gate 		if (error) {
10627c478bd9Sstevel@tonic-gate 			CB_WARN1("nfs4_svc: svc_tli_kcreate failed %d\n",
10637c478bd9Sstevel@tonic-gate 			    error);
10647c478bd9Sstevel@tonic-gate 			kmem_free(addrmask.buf, addrmask.maxlen);
10657c478bd9Sstevel@tonic-gate 		}
10667c478bd9Sstevel@tonic-gate 	}
10677c478bd9Sstevel@tonic-gate 
10687c478bd9Sstevel@tonic-gate 	mutex_exit(&ncg->nfs4_cb_lock);
10697c478bd9Sstevel@tonic-gate 	releasef(STRUCT_FGET(uap, fd));
10707c478bd9Sstevel@tonic-gate 	return (error);
10717c478bd9Sstevel@tonic-gate }
10727c478bd9Sstevel@tonic-gate 
10737c478bd9Sstevel@tonic-gate struct nfs4_callback_globals *
10747c478bd9Sstevel@tonic-gate nfs4_get_callback_globals(void)
10757c478bd9Sstevel@tonic-gate {
1076108322fbScarlsonj 	return (zone_getspecific(nfs4_callback_zone_key, nfs_zone()));
10777c478bd9Sstevel@tonic-gate }
10787c478bd9Sstevel@tonic-gate 
10797c478bd9Sstevel@tonic-gate static void *
10807c478bd9Sstevel@tonic-gate nfs4_callback_init_zone(zoneid_t zoneid)
10817c478bd9Sstevel@tonic-gate {
10827c478bd9Sstevel@tonic-gate 	kstat_t *nfs4_callback_kstat;
10837c478bd9Sstevel@tonic-gate 	struct nfs4_callback_globals *ncg;
10847c478bd9Sstevel@tonic-gate 
10857c478bd9Sstevel@tonic-gate 	ncg = kmem_zalloc(sizeof (*ncg), KM_SLEEP);
10867c478bd9Sstevel@tonic-gate 
10877c478bd9Sstevel@tonic-gate 	ncg->nfs4prog2server = kmem_zalloc(nfs4_num_prognums *
10887c478bd9Sstevel@tonic-gate 	    sizeof (struct nfs4_server *), KM_SLEEP);
10897c478bd9Sstevel@tonic-gate 
10907c478bd9Sstevel@tonic-gate 	/* initialize the dlist */
10917c478bd9Sstevel@tonic-gate 	mutex_init(&ncg->nfs4_dlist_lock, NULL, MUTEX_DEFAULT, NULL);
10927c478bd9Sstevel@tonic-gate 	list_create(&ncg->nfs4_dlist, sizeof (struct nfs4_dnode),
10937c478bd9Sstevel@tonic-gate 	    offsetof(struct nfs4_dnode, linkage));
10947c478bd9Sstevel@tonic-gate 
10957c478bd9Sstevel@tonic-gate 	/* initialize cb_port list */
10967c478bd9Sstevel@tonic-gate 	mutex_init(&ncg->nfs4_cb_lock, NULL, MUTEX_DEFAULT, NULL);
10977c478bd9Sstevel@tonic-gate 	list_create(&ncg->nfs4_cb_ports, sizeof (struct nfs4_cb_port),
10987c478bd9Sstevel@tonic-gate 	    offsetof(struct nfs4_cb_port, linkage));
10997c478bd9Sstevel@tonic-gate 
11007c478bd9Sstevel@tonic-gate 	/* get our own copy of the kstats */
11017c478bd9Sstevel@tonic-gate 	bcopy(&nfs4_callback_stats_tmpl, &ncg->nfs4_callback_stats,
11027c478bd9Sstevel@tonic-gate 	    sizeof (nfs4_callback_stats_tmpl));
11037c478bd9Sstevel@tonic-gate 	/* register "nfs:0:nfs4_callback_stats" for this zone */
11047c478bd9Sstevel@tonic-gate 	if ((nfs4_callback_kstat =
11057c478bd9Sstevel@tonic-gate 	    kstat_create_zone("nfs", 0, "nfs4_callback_stats", "misc",
11067c478bd9Sstevel@tonic-gate 	    KSTAT_TYPE_NAMED,
11077c478bd9Sstevel@tonic-gate 	    sizeof (ncg->nfs4_callback_stats) / sizeof (kstat_named_t),
11087c478bd9Sstevel@tonic-gate 	    KSTAT_FLAG_VIRTUAL | KSTAT_FLAG_WRITABLE,
11097c478bd9Sstevel@tonic-gate 	    zoneid)) != NULL) {
11107c478bd9Sstevel@tonic-gate 		nfs4_callback_kstat->ks_data = &ncg->nfs4_callback_stats;
11117c478bd9Sstevel@tonic-gate 		kstat_install(nfs4_callback_kstat);
11127c478bd9Sstevel@tonic-gate 	}
11137c478bd9Sstevel@tonic-gate 	return (ncg);
11147c478bd9Sstevel@tonic-gate }
11157c478bd9Sstevel@tonic-gate 
11167c478bd9Sstevel@tonic-gate static void
11177c478bd9Sstevel@tonic-gate nfs4_discard_delegations(struct nfs4_callback_globals *ncg)
11187c478bd9Sstevel@tonic-gate {
11197c478bd9Sstevel@tonic-gate 	nfs4_server_t *sp;
11207c478bd9Sstevel@tonic-gate 	int i, num_removed;
11217c478bd9Sstevel@tonic-gate 
11227c478bd9Sstevel@tonic-gate 	/*
11237c478bd9Sstevel@tonic-gate 	 * It's OK here to just run through the registered "programs", as
11247c478bd9Sstevel@tonic-gate 	 * servers without programs won't have any delegations to handle.
11257c478bd9Sstevel@tonic-gate 	 */
11267c478bd9Sstevel@tonic-gate 	for (i = 0; i < nfs4_num_prognums; i++) {
11277c478bd9Sstevel@tonic-gate 		rnode4_t *rp;
11287c478bd9Sstevel@tonic-gate 
11297c478bd9Sstevel@tonic-gate 		mutex_enter(&ncg->nfs4_cb_lock);
11307c478bd9Sstevel@tonic-gate 		sp = ncg->nfs4prog2server[i];
11317c478bd9Sstevel@tonic-gate 		mutex_exit(&ncg->nfs4_cb_lock);
11327c478bd9Sstevel@tonic-gate 
11337c478bd9Sstevel@tonic-gate 		if (nfs4_server_vlock(sp, 1) == FALSE)
11347c478bd9Sstevel@tonic-gate 			continue;
11357c478bd9Sstevel@tonic-gate 		num_removed = 0;
11367c478bd9Sstevel@tonic-gate 		while ((rp = list_head(&sp->s_deleg_list)) != NULL) {
11377c478bd9Sstevel@tonic-gate 			mutex_enter(&rp->r_statev4_lock);
11387c478bd9Sstevel@tonic-gate 			if (rp->r_deleg_type == OPEN_DELEGATE_NONE) {
11397c478bd9Sstevel@tonic-gate 				/*
11407c478bd9Sstevel@tonic-gate 				 * We need to take matters into our own hands,
11417c478bd9Sstevel@tonic-gate 				 * as nfs4delegreturn_cleanup_impl() won't
11427c478bd9Sstevel@tonic-gate 				 * remove this from the list.
11437c478bd9Sstevel@tonic-gate 				 */
11447c478bd9Sstevel@tonic-gate 				list_remove(&sp->s_deleg_list, rp);
11457c478bd9Sstevel@tonic-gate 				mutex_exit(&rp->r_statev4_lock);
11467c478bd9Sstevel@tonic-gate 				nfs4_dec_state_ref_count_nolock(sp,
11477c478bd9Sstevel@tonic-gate 				    VTOMI4(RTOV4(rp)));
11487c478bd9Sstevel@tonic-gate 				num_removed++;
11497c478bd9Sstevel@tonic-gate 				continue;
11507c478bd9Sstevel@tonic-gate 			}
11517c478bd9Sstevel@tonic-gate 			mutex_exit(&rp->r_statev4_lock);
11527c478bd9Sstevel@tonic-gate 			VN_HOLD(RTOV4(rp));
11537c478bd9Sstevel@tonic-gate 			mutex_exit(&sp->s_lock);
11547c478bd9Sstevel@tonic-gate 			/*
11557c478bd9Sstevel@tonic-gate 			 * The following will remove the node from the list.
11567c478bd9Sstevel@tonic-gate 			 */
11577c478bd9Sstevel@tonic-gate 			nfs4delegreturn_cleanup_impl(rp, sp, ncg);
11587c478bd9Sstevel@tonic-gate 			VN_RELE(RTOV4(rp));
11597c478bd9Sstevel@tonic-gate 			mutex_enter(&sp->s_lock);
11607c478bd9Sstevel@tonic-gate 		}
11617c478bd9Sstevel@tonic-gate 		mutex_exit(&sp->s_lock);
11627c478bd9Sstevel@tonic-gate 		/* each removed list node reles a reference */
11637c478bd9Sstevel@tonic-gate 		while (num_removed-- > 0)
11647c478bd9Sstevel@tonic-gate 			nfs4_server_rele(sp);
11657c478bd9Sstevel@tonic-gate 		/* remove our reference for nfs4_server_vlock */
11667c478bd9Sstevel@tonic-gate 		nfs4_server_rele(sp);
11677c478bd9Sstevel@tonic-gate 	}
11687c478bd9Sstevel@tonic-gate }
11697c478bd9Sstevel@tonic-gate 
11707c478bd9Sstevel@tonic-gate /* ARGSUSED */
11717c478bd9Sstevel@tonic-gate static void
11727c478bd9Sstevel@tonic-gate nfs4_callback_shutdown_zone(zoneid_t zoneid, void *data)
11737c478bd9Sstevel@tonic-gate {
11747c478bd9Sstevel@tonic-gate 	struct nfs4_callback_globals *ncg = data;
11757c478bd9Sstevel@tonic-gate 
11767c478bd9Sstevel@tonic-gate 	/*
11777c478bd9Sstevel@tonic-gate 	 * Clean pending delegation return list.
11787c478bd9Sstevel@tonic-gate 	 */
11797c478bd9Sstevel@tonic-gate 	nfs4_dlistclean_impl(ncg, NFS4_DR_DISCARD);
11807c478bd9Sstevel@tonic-gate 
11817c478bd9Sstevel@tonic-gate 	/*
11827c478bd9Sstevel@tonic-gate 	 * Discard all delegations.
11837c478bd9Sstevel@tonic-gate 	 */
11847c478bd9Sstevel@tonic-gate 	nfs4_discard_delegations(ncg);
11857c478bd9Sstevel@tonic-gate }
11867c478bd9Sstevel@tonic-gate 
11877c478bd9Sstevel@tonic-gate static void
11887c478bd9Sstevel@tonic-gate nfs4_callback_fini_zone(zoneid_t zoneid, void *data)
11897c478bd9Sstevel@tonic-gate {
11907c478bd9Sstevel@tonic-gate 	struct nfs4_callback_globals *ncg = data;
11917c478bd9Sstevel@tonic-gate 	struct nfs4_cb_port *p;
11927c478bd9Sstevel@tonic-gate 	nfs4_server_t *sp, *next;
11937c478bd9Sstevel@tonic-gate 	nfs4_server_t freelist;
11947c478bd9Sstevel@tonic-gate 	int i;
11957c478bd9Sstevel@tonic-gate 
11967c478bd9Sstevel@tonic-gate 	kstat_delete_byname_zone("nfs", 0, "nfs4_callback_stats", zoneid);
11977c478bd9Sstevel@tonic-gate 
11987c478bd9Sstevel@tonic-gate 	/*
11997c478bd9Sstevel@tonic-gate 	 * Discard all delegations that may have crept in since we did the
12007c478bd9Sstevel@tonic-gate 	 * _shutdown.
12017c478bd9Sstevel@tonic-gate 	 */
12027c478bd9Sstevel@tonic-gate 	nfs4_discard_delegations(ncg);
12037c478bd9Sstevel@tonic-gate 	/*
12047c478bd9Sstevel@tonic-gate 	 * We're completely done with this zone and all associated
12057c478bd9Sstevel@tonic-gate 	 * nfs4_server_t's.  Any remaining nfs4_server_ts should only have one
12067c478bd9Sstevel@tonic-gate 	 * more reference outstanding -- the reference we didn't release in
12077c478bd9Sstevel@tonic-gate 	 * nfs4_renew_lease_thread().
12087c478bd9Sstevel@tonic-gate 	 *
12097c478bd9Sstevel@tonic-gate 	 * Here we need to run through the global nfs4_server_lst as we need to
12107c478bd9Sstevel@tonic-gate 	 * deal with nfs4_server_ts without programs, as they also have threads
12117c478bd9Sstevel@tonic-gate 	 * created for them, and so have outstanding references that we need to
12127c478bd9Sstevel@tonic-gate 	 * release.
12137c478bd9Sstevel@tonic-gate 	 */
12147c478bd9Sstevel@tonic-gate 	freelist.forw = &freelist;
12157c478bd9Sstevel@tonic-gate 	freelist.back = &freelist;
12167c478bd9Sstevel@tonic-gate 	mutex_enter(&nfs4_server_lst_lock);
12177c478bd9Sstevel@tonic-gate 	sp = nfs4_server_lst.forw;
12187c478bd9Sstevel@tonic-gate 	while (sp != &nfs4_server_lst) {
12197c478bd9Sstevel@tonic-gate 		next = sp->forw;
12207c478bd9Sstevel@tonic-gate 		if (sp->zoneid == zoneid) {
12217c478bd9Sstevel@tonic-gate 			remque(sp);
12227c478bd9Sstevel@tonic-gate 			insque(sp, &freelist);
12237c478bd9Sstevel@tonic-gate 		}
12247c478bd9Sstevel@tonic-gate 		sp = next;
12257c478bd9Sstevel@tonic-gate 	}
12267c478bd9Sstevel@tonic-gate 	mutex_exit(&nfs4_server_lst_lock);
12277c478bd9Sstevel@tonic-gate 
12287c478bd9Sstevel@tonic-gate 	sp = freelist.forw;
12297c478bd9Sstevel@tonic-gate 	while (sp != &freelist) {
12307c478bd9Sstevel@tonic-gate 		next = sp->forw;
12317c478bd9Sstevel@tonic-gate 		nfs4_server_rele(sp);	/* free the list's reference */
12327c478bd9Sstevel@tonic-gate 		sp = next;
12337c478bd9Sstevel@tonic-gate 	}
12347c478bd9Sstevel@tonic-gate 
12357c478bd9Sstevel@tonic-gate #ifdef DEBUG
12367c478bd9Sstevel@tonic-gate 	for (i = 0; i < nfs4_num_prognums; i++) {
12377c478bd9Sstevel@tonic-gate 		ASSERT(ncg->nfs4prog2server[i] == NULL);
12387c478bd9Sstevel@tonic-gate 	}
12397c478bd9Sstevel@tonic-gate #endif
12407c478bd9Sstevel@tonic-gate 	kmem_free(ncg->nfs4prog2server, nfs4_num_prognums *
12417c478bd9Sstevel@tonic-gate 	    sizeof (struct nfs4_server *));
12427c478bd9Sstevel@tonic-gate 
12437c478bd9Sstevel@tonic-gate 	mutex_enter(&ncg->nfs4_cb_lock);
12447c478bd9Sstevel@tonic-gate 	while ((p = list_head(&ncg->nfs4_cb_ports)) != NULL) {
12457c478bd9Sstevel@tonic-gate 		list_remove(&ncg->nfs4_cb_ports, p);
12467c478bd9Sstevel@tonic-gate 		kmem_free(p, sizeof (*p));
12477c478bd9Sstevel@tonic-gate 	}
12487c478bd9Sstevel@tonic-gate 	list_destroy(&ncg->nfs4_cb_ports);
12497c478bd9Sstevel@tonic-gate 	mutex_destroy(&ncg->nfs4_cb_lock);
12507c478bd9Sstevel@tonic-gate 	list_destroy(&ncg->nfs4_dlist);
12517c478bd9Sstevel@tonic-gate 	mutex_destroy(&ncg->nfs4_dlist_lock);
12527c478bd9Sstevel@tonic-gate 	kmem_free(ncg, sizeof (*ncg));
12537c478bd9Sstevel@tonic-gate }
12547c478bd9Sstevel@tonic-gate 
12557c478bd9Sstevel@tonic-gate void
12567c478bd9Sstevel@tonic-gate nfs4_callback_init(void)
12577c478bd9Sstevel@tonic-gate {
12587c478bd9Sstevel@tonic-gate 	int i;
12597c478bd9Sstevel@tonic-gate 	SVC_CALLOUT *nfs4_cb_sc;
12607c478bd9Sstevel@tonic-gate 
12617c478bd9Sstevel@tonic-gate 	/* initialize the callback table */
12627c478bd9Sstevel@tonic-gate 	nfs4_cb_sc = kmem_alloc(nfs4_num_prognums *
12637c478bd9Sstevel@tonic-gate 	    sizeof (SVC_CALLOUT), KM_SLEEP);
12647c478bd9Sstevel@tonic-gate 
12657c478bd9Sstevel@tonic-gate 	for (i = 0; i < nfs4_num_prognums; i++) {
12667c478bd9Sstevel@tonic-gate 		nfs4_cb_sc[i].sc_prog = NFS4_CALLBACK+i;
12677c478bd9Sstevel@tonic-gate 		nfs4_cb_sc[i].sc_versmin = NFS_CB;
12687c478bd9Sstevel@tonic-gate 		nfs4_cb_sc[i].sc_versmax = NFS_CB;
12697c478bd9Sstevel@tonic-gate 		nfs4_cb_sc[i].sc_dispatch = cb_dispatch;
12707c478bd9Sstevel@tonic-gate 	}
12717c478bd9Sstevel@tonic-gate 
12727c478bd9Sstevel@tonic-gate 	nfs4_cb_sct.sct_size = nfs4_num_prognums;
12737c478bd9Sstevel@tonic-gate 	nfs4_cb_sct.sct_free = FALSE;
12747c478bd9Sstevel@tonic-gate 	nfs4_cb_sct.sct_sc = nfs4_cb_sc;
12757c478bd9Sstevel@tonic-gate 
12767c478bd9Sstevel@tonic-gate 	/*
12777c478bd9Sstevel@tonic-gate 	 * Compute max bytes required for dyamically allocated parts
12787c478bd9Sstevel@tonic-gate 	 * of cb_getattr reply.  Only size and change are supported now.
12797c478bd9Sstevel@tonic-gate 	 * If CB_GETATTR is changed to reply with additional attrs,
12807c478bd9Sstevel@tonic-gate 	 * additional sizes must be added below.
12817c478bd9Sstevel@tonic-gate 	 *
12827c478bd9Sstevel@tonic-gate 	 * fattr4_change + fattr4_size == uint64_t + uint64_t
12837c478bd9Sstevel@tonic-gate 	 */
12847c478bd9Sstevel@tonic-gate 	cb_getattr_bytes = 2 * BYTES_PER_XDR_UNIT + 2 * BYTES_PER_XDR_UNIT;
12857c478bd9Sstevel@tonic-gate 
12867c478bd9Sstevel@tonic-gate 	zone_key_create(&nfs4_callback_zone_key, nfs4_callback_init_zone,
12877c478bd9Sstevel@tonic-gate 	    nfs4_callback_shutdown_zone, nfs4_callback_fini_zone);
12887c478bd9Sstevel@tonic-gate }
12897c478bd9Sstevel@tonic-gate 
12907c478bd9Sstevel@tonic-gate void
12917c478bd9Sstevel@tonic-gate nfs4_callback_fini(void)
12927c478bd9Sstevel@tonic-gate {
12937c478bd9Sstevel@tonic-gate }
12947c478bd9Sstevel@tonic-gate 
12957c478bd9Sstevel@tonic-gate /*
12967c478bd9Sstevel@tonic-gate  * NB: This function can be called from the *wrong* zone (ie, the zone that
12977c478bd9Sstevel@tonic-gate  * 'rp' belongs to and the caller's zone may not be the same).  This can happen
12987c478bd9Sstevel@tonic-gate  * if the zone is going away and we get called from nfs4_async_inactive().  In
12997c478bd9Sstevel@tonic-gate  * this case the globals will be NULL and we won't update the counters, which
13007c478bd9Sstevel@tonic-gate  * doesn't matter as the zone is going away anyhow.
13017c478bd9Sstevel@tonic-gate  */
13027c478bd9Sstevel@tonic-gate static void
13037c478bd9Sstevel@tonic-gate nfs4delegreturn_cleanup_impl(rnode4_t *rp, nfs4_server_t *np,
13047c478bd9Sstevel@tonic-gate 	struct nfs4_callback_globals *ncg)
13057c478bd9Sstevel@tonic-gate {
13067c478bd9Sstevel@tonic-gate 	mntinfo4_t *mi = VTOMI4(RTOV4(rp));
13077c478bd9Sstevel@tonic-gate 	boolean_t need_rele = B_FALSE;
13087c478bd9Sstevel@tonic-gate 
13098693510cSpf199842 	/*
13108693510cSpf199842 	 * Caller must be holding mi_recovlock in read mode
13118693510cSpf199842 	 * to call here.  This is provided by start_op.
13128693510cSpf199842 	 * Delegation management requires to grab s_lock
13138693510cSpf199842 	 * first and then r_statev4_lock.
13148693510cSpf199842 	 */
13158693510cSpf199842 
13168693510cSpf199842 	if (np == NULL) {
13178693510cSpf199842 		np = find_nfs4_server_all(mi, 1);
131822dc8f51SPavel Filipensky 		if (np == NULL)
131922dc8f51SPavel Filipensky 			return;
13208693510cSpf199842 		need_rele = B_TRUE;
13218693510cSpf199842 	} else {
13228693510cSpf199842 		mutex_enter(&np->s_lock);
13238693510cSpf199842 	}
13248693510cSpf199842 
13257c478bd9Sstevel@tonic-gate 	mutex_enter(&rp->r_statev4_lock);
13267c478bd9Sstevel@tonic-gate 
132722dc8f51SPavel Filipensky 	if (rp->r_deleg_type == OPEN_DELEGATE_NONE) {
132822dc8f51SPavel Filipensky 		mutex_exit(&rp->r_statev4_lock);
132922dc8f51SPavel Filipensky 		mutex_exit(&np->s_lock);
133022dc8f51SPavel Filipensky 		if (need_rele)
133122dc8f51SPavel Filipensky 			nfs4_server_rele(np);
133222dc8f51SPavel Filipensky 		return;
133322dc8f51SPavel Filipensky 	}
133422dc8f51SPavel Filipensky 
13357c478bd9Sstevel@tonic-gate 	/*
13367c478bd9Sstevel@tonic-gate 	 * Free the cred originally held when
13377c478bd9Sstevel@tonic-gate 	 * the delegation was granted.  Caller must
13387c478bd9Sstevel@tonic-gate 	 * hold this cred if it wants to use it after
13397c478bd9Sstevel@tonic-gate 	 * this call.
13407c478bd9Sstevel@tonic-gate 	 */
13417c478bd9Sstevel@tonic-gate 	crfree(rp->r_deleg_cred);
13427c478bd9Sstevel@tonic-gate 	rp->r_deleg_cred = NULL;
13437c478bd9Sstevel@tonic-gate 	rp->r_deleg_type = OPEN_DELEGATE_NONE;
13447c478bd9Sstevel@tonic-gate 	rp->r_deleg_needs_recovery = OPEN_DELEGATE_NONE;
13457c478bd9Sstevel@tonic-gate 	rp->r_deleg_needs_recall = FALSE;
13467c478bd9Sstevel@tonic-gate 	rp->r_deleg_return_pending = FALSE;
13477c478bd9Sstevel@tonic-gate 
13487c478bd9Sstevel@tonic-gate 	/*
13497c478bd9Sstevel@tonic-gate 	 * Remove the rnode from the server's list and
13507c478bd9Sstevel@tonic-gate 	 * update the ref counts.
13517c478bd9Sstevel@tonic-gate 	 */
13527c478bd9Sstevel@tonic-gate 	list_remove(&np->s_deleg_list, rp);
13538693510cSpf199842 	mutex_exit(&rp->r_statev4_lock);
13547c478bd9Sstevel@tonic-gate 	nfs4_dec_state_ref_count_nolock(np, mi);
13557c478bd9Sstevel@tonic-gate 	mutex_exit(&np->s_lock);
13567c478bd9Sstevel@tonic-gate 	/* removed list node removes a reference */
13577c478bd9Sstevel@tonic-gate 	nfs4_server_rele(np);
13587c478bd9Sstevel@tonic-gate 	if (need_rele)
13597c478bd9Sstevel@tonic-gate 		nfs4_server_rele(np);
13607c478bd9Sstevel@tonic-gate 	if (ncg != NULL)
13617c478bd9Sstevel@tonic-gate 		ncg->nfs4_callback_stats.delegations.value.ui64--;
13627c478bd9Sstevel@tonic-gate }
13637c478bd9Sstevel@tonic-gate 
13647c478bd9Sstevel@tonic-gate void
13657c478bd9Sstevel@tonic-gate nfs4delegreturn_cleanup(rnode4_t *rp, nfs4_server_t *np)
13667c478bd9Sstevel@tonic-gate {
13677c478bd9Sstevel@tonic-gate 	struct nfs4_callback_globals *ncg;
13687c478bd9Sstevel@tonic-gate 
13697c478bd9Sstevel@tonic-gate 	if (np != NULL) {
13707c478bd9Sstevel@tonic-gate 		ncg = np->zone_globals;
1371108322fbScarlsonj 	} else if (nfs_zone() == VTOMI4(RTOV4(rp))->mi_zone) {
1372108322fbScarlsonj 		ncg = zone_getspecific(nfs4_callback_zone_key, nfs_zone());
13737c478bd9Sstevel@tonic-gate 		ASSERT(ncg != NULL);
13747c478bd9Sstevel@tonic-gate 	} else {
13757c478bd9Sstevel@tonic-gate 		/*
13767c478bd9Sstevel@tonic-gate 		 * Request coming from the wrong zone.
13777c478bd9Sstevel@tonic-gate 		 */
13787c478bd9Sstevel@tonic-gate 		ASSERT(getzoneid() == GLOBAL_ZONEID);
13797c478bd9Sstevel@tonic-gate 		ncg = NULL;
13807c478bd9Sstevel@tonic-gate 	}
13817c478bd9Sstevel@tonic-gate 
13827c478bd9Sstevel@tonic-gate 	nfs4delegreturn_cleanup_impl(rp, np, ncg);
13837c478bd9Sstevel@tonic-gate }
13847c478bd9Sstevel@tonic-gate 
13857c478bd9Sstevel@tonic-gate static void
13867c478bd9Sstevel@tonic-gate nfs4delegreturn_save_lost_rqst(int error, nfs4_lost_rqst_t *lost_rqstp,
13877c478bd9Sstevel@tonic-gate 	cred_t *cr, vnode_t *vp)
13887c478bd9Sstevel@tonic-gate {
13897c478bd9Sstevel@tonic-gate 	if (error != ETIMEDOUT && error != EINTR &&
13907c478bd9Sstevel@tonic-gate 	    !NFS4_FRC_UNMT_ERR(error, vp->v_vfsp)) {
13917c478bd9Sstevel@tonic-gate 		lost_rqstp->lr_op = 0;
13927c478bd9Sstevel@tonic-gate 		return;
13937c478bd9Sstevel@tonic-gate 	}
13947c478bd9Sstevel@tonic-gate 
13957c478bd9Sstevel@tonic-gate 	NFS4_DEBUG(nfs4_lost_rqst_debug, (CE_NOTE,
13967c478bd9Sstevel@tonic-gate 	    "nfs4close_save_lost_rqst: error %d", error));
13977c478bd9Sstevel@tonic-gate 
13987c478bd9Sstevel@tonic-gate 	lost_rqstp->lr_op = OP_DELEGRETURN;
13997c478bd9Sstevel@tonic-gate 	/*
14007c478bd9Sstevel@tonic-gate 	 * The vp is held and rele'd via the recovery code.
14017c478bd9Sstevel@tonic-gate 	 * See nfs4_save_lost_rqst.
14027c478bd9Sstevel@tonic-gate 	 */
14037c478bd9Sstevel@tonic-gate 	lost_rqstp->lr_vp = vp;
14047c478bd9Sstevel@tonic-gate 	lost_rqstp->lr_dvp = NULL;
14057c478bd9Sstevel@tonic-gate 	lost_rqstp->lr_oop = NULL;
14067c478bd9Sstevel@tonic-gate 	lost_rqstp->lr_osp = NULL;
14077c478bd9Sstevel@tonic-gate 	lost_rqstp->lr_lop = NULL;
14087c478bd9Sstevel@tonic-gate 	lost_rqstp->lr_cr = cr;
14097c478bd9Sstevel@tonic-gate 	lost_rqstp->lr_flk = NULL;
14107c478bd9Sstevel@tonic-gate 	lost_rqstp->lr_putfirst = FALSE;
14117c478bd9Sstevel@tonic-gate }
14127c478bd9Sstevel@tonic-gate 
14137c478bd9Sstevel@tonic-gate static void
14147c478bd9Sstevel@tonic-gate nfs4delegreturn_otw(rnode4_t *rp, cred_t *cr, nfs4_error_t *ep)
14157c478bd9Sstevel@tonic-gate {
14167c478bd9Sstevel@tonic-gate 	COMPOUND4args_clnt args;
14177c478bd9Sstevel@tonic-gate 	COMPOUND4res_clnt res;
14187c478bd9Sstevel@tonic-gate 	nfs_argop4 argops[3];
14197c478bd9Sstevel@tonic-gate 	nfs4_ga_res_t *garp = NULL;
14207c478bd9Sstevel@tonic-gate 	hrtime_t t;
14217c478bd9Sstevel@tonic-gate 	int numops;
14227c478bd9Sstevel@tonic-gate 	int doqueue = 1;
14237c478bd9Sstevel@tonic-gate 
14247c478bd9Sstevel@tonic-gate 	args.ctag = TAG_DELEGRETURN;
14257c478bd9Sstevel@tonic-gate 
14267c478bd9Sstevel@tonic-gate 	numops = 3;		/* PUTFH, GETATTR, DELEGRETURN */
14277c478bd9Sstevel@tonic-gate 
14287c478bd9Sstevel@tonic-gate 	args.array = argops;
14297c478bd9Sstevel@tonic-gate 	args.array_len = numops;
14307c478bd9Sstevel@tonic-gate 
14317c478bd9Sstevel@tonic-gate 	argops[0].argop = OP_CPUTFH;
14327c478bd9Sstevel@tonic-gate 	argops[0].nfs_argop4_u.opcputfh.sfh = rp->r_fh;
14337c478bd9Sstevel@tonic-gate 
14347c478bd9Sstevel@tonic-gate 	argops[1].argop = OP_GETATTR;
14357c478bd9Sstevel@tonic-gate 	argops[1].nfs_argop4_u.opgetattr.attr_request = NFS4_VATTR_MASK;
14367c478bd9Sstevel@tonic-gate 	argops[1].nfs_argop4_u.opgetattr.mi = VTOMI4(RTOV4(rp));
14377c478bd9Sstevel@tonic-gate 
14387c478bd9Sstevel@tonic-gate 	argops[2].argop = OP_DELEGRETURN;
14397c478bd9Sstevel@tonic-gate 	argops[2].nfs_argop4_u.opdelegreturn.deleg_stateid =
14407c478bd9Sstevel@tonic-gate 	    rp->r_deleg_stateid;
14417c478bd9Sstevel@tonic-gate 
14427c478bd9Sstevel@tonic-gate 	t = gethrtime();
14437c478bd9Sstevel@tonic-gate 	rfs4call(VTOMI4(RTOV4(rp)), &args, &res, cr, &doqueue, 0, ep);
14447c478bd9Sstevel@tonic-gate 
14457c478bd9Sstevel@tonic-gate 	if (ep->error)
14467c478bd9Sstevel@tonic-gate 		return;
14477c478bd9Sstevel@tonic-gate 
14487c478bd9Sstevel@tonic-gate 	if (res.status == NFS4_OK) {
14497c478bd9Sstevel@tonic-gate 		garp = &res.array[1].nfs_resop4_u.opgetattr.ga_res;
14507c478bd9Sstevel@tonic-gate 		nfs4_attr_cache(RTOV4(rp), garp, t, cr, TRUE, NULL);
14517c478bd9Sstevel@tonic-gate 
14527c478bd9Sstevel@tonic-gate 	}
14537c478bd9Sstevel@tonic-gate 	(void) xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res);
14547c478bd9Sstevel@tonic-gate }
14557c478bd9Sstevel@tonic-gate 
14567c478bd9Sstevel@tonic-gate int
14577c478bd9Sstevel@tonic-gate nfs4_do_delegreturn(rnode4_t *rp, int flags, cred_t *cr,
14587c478bd9Sstevel@tonic-gate 	struct nfs4_callback_globals *ncg)
14597c478bd9Sstevel@tonic-gate {
14607c478bd9Sstevel@tonic-gate 	vnode_t *vp = RTOV4(rp);
14617c478bd9Sstevel@tonic-gate 	mntinfo4_t *mi = VTOMI4(vp);
14627c478bd9Sstevel@tonic-gate 	nfs4_lost_rqst_t lost_rqst;
14637c478bd9Sstevel@tonic-gate 	nfs4_recov_state_t recov_state;
14647c478bd9Sstevel@tonic-gate 	bool_t needrecov = FALSE, recovonly, done = FALSE;
14657c478bd9Sstevel@tonic-gate 	nfs4_error_t e = { 0, NFS4_OK, RPC_SUCCESS };
14667c478bd9Sstevel@tonic-gate 
14677c478bd9Sstevel@tonic-gate 	ncg->nfs4_callback_stats.delegreturn.value.ui64++;
14687c478bd9Sstevel@tonic-gate 
14697c478bd9Sstevel@tonic-gate 	while (!done) {
14707c478bd9Sstevel@tonic-gate 		e.error = nfs4_start_fop(mi, vp, NULL, OH_DELEGRETURN,
14717c478bd9Sstevel@tonic-gate 		    &recov_state, &recovonly);
14727c478bd9Sstevel@tonic-gate 
14737c478bd9Sstevel@tonic-gate 		if (e.error) {
14747c478bd9Sstevel@tonic-gate 			if (flags & NFS4_DR_FORCE) {
14757c478bd9Sstevel@tonic-gate 				(void) nfs_rw_enter_sig(&mi->mi_recovlock,
14767c478bd9Sstevel@tonic-gate 				    RW_READER, 0);
14777c478bd9Sstevel@tonic-gate 				nfs4delegreturn_cleanup_impl(rp, NULL, ncg);
14787c478bd9Sstevel@tonic-gate 				nfs_rw_exit(&mi->mi_recovlock);
14797c478bd9Sstevel@tonic-gate 			}
14807c478bd9Sstevel@tonic-gate 			break;
14817c478bd9Sstevel@tonic-gate 		}
14827c478bd9Sstevel@tonic-gate 
14837c478bd9Sstevel@tonic-gate 		/*
14847c478bd9Sstevel@tonic-gate 		 * Check to see if the delegation has already been
14857c478bd9Sstevel@tonic-gate 		 * returned by the recovery thread.   The state of
14867c478bd9Sstevel@tonic-gate 		 * the delegation cannot change at this point due
14877c478bd9Sstevel@tonic-gate 		 * to start_fop and the r_deleg_recall_lock.
14887c478bd9Sstevel@tonic-gate 		 */
14897c478bd9Sstevel@tonic-gate 		if (rp->r_deleg_type == OPEN_DELEGATE_NONE) {
14907c478bd9Sstevel@tonic-gate 			e.error = 0;
14917c478bd9Sstevel@tonic-gate 			nfs4_end_op(mi, vp, NULL, &recov_state, needrecov);
14927c478bd9Sstevel@tonic-gate 			break;
14937c478bd9Sstevel@tonic-gate 		}
14947c478bd9Sstevel@tonic-gate 
14957c478bd9Sstevel@tonic-gate 		if (recovonly) {
14967c478bd9Sstevel@tonic-gate 			/*
14977c478bd9Sstevel@tonic-gate 			 * Delegation will be returned via the
14987c478bd9Sstevel@tonic-gate 			 * recovery framework.  Build a lost request
14997c478bd9Sstevel@tonic-gate 			 * structure, start recovery and get out.
15007c478bd9Sstevel@tonic-gate 			 */
15017c478bd9Sstevel@tonic-gate 			nfs4_error_init(&e, EINTR);
15027c478bd9Sstevel@tonic-gate 			nfs4delegreturn_save_lost_rqst(e.error, &lost_rqst,
15037c478bd9Sstevel@tonic-gate 			    cr, vp);
15047c478bd9Sstevel@tonic-gate 			(void) nfs4_start_recovery(&e, mi, vp,
15057c478bd9Sstevel@tonic-gate 			    NULL, &rp->r_deleg_stateid,
15067c478bd9Sstevel@tonic-gate 			    lost_rqst.lr_op == OP_DELEGRETURN ?
15072f172c55SRobert Thurlow 			    &lost_rqst : NULL, OP_DELEGRETURN, NULL,
15082f172c55SRobert Thurlow 			    NULL, NULL);
15097c478bd9Sstevel@tonic-gate 			nfs4_end_op(mi, vp, NULL, &recov_state, needrecov);
15107c478bd9Sstevel@tonic-gate 			break;
15117c478bd9Sstevel@tonic-gate 		}
15127c478bd9Sstevel@tonic-gate 
15137c478bd9Sstevel@tonic-gate 		nfs4delegreturn_otw(rp, cr, &e);
15147c478bd9Sstevel@tonic-gate 
15157c478bd9Sstevel@tonic-gate 		/*
15167c478bd9Sstevel@tonic-gate 		 * Ignore some errors on delegreturn; no point in marking
15177c478bd9Sstevel@tonic-gate 		 * the file dead on a state destroying operation.
15187c478bd9Sstevel@tonic-gate 		 */
15197c478bd9Sstevel@tonic-gate 		if (e.error == 0 && (nfs4_recov_marks_dead(e.stat) ||
15207c478bd9Sstevel@tonic-gate 		    e.stat == NFS4ERR_BADHANDLE ||
15210550d313SSimon Klinkert 		    e.stat == NFS4ERR_STALE ||
15220550d313SSimon Klinkert 		    (e.stat == NFS4ERR_STALE_STATEID &&
15230550d313SSimon Klinkert 		     !(rp->r_flags & R4HASHED))))
15247c478bd9Sstevel@tonic-gate 			needrecov = FALSE;
15257c478bd9Sstevel@tonic-gate 		else
15267c478bd9Sstevel@tonic-gate 			needrecov = nfs4_needs_recovery(&e, TRUE, vp->v_vfsp);
15277c478bd9Sstevel@tonic-gate 
15287c478bd9Sstevel@tonic-gate 		if (needrecov) {
15297c478bd9Sstevel@tonic-gate 			nfs4delegreturn_save_lost_rqst(e.error, &lost_rqst,
15307c478bd9Sstevel@tonic-gate 			    cr, vp);
15317c478bd9Sstevel@tonic-gate 			(void) nfs4_start_recovery(&e, mi, vp,
15327c478bd9Sstevel@tonic-gate 			    NULL, &rp->r_deleg_stateid,
15337c478bd9Sstevel@tonic-gate 			    lost_rqst.lr_op == OP_DELEGRETURN ?
15342f172c55SRobert Thurlow 			    &lost_rqst : NULL, OP_DELEGRETURN, NULL,
15352f172c55SRobert Thurlow 			    NULL, NULL);
15367c478bd9Sstevel@tonic-gate 		} else {
15377c478bd9Sstevel@tonic-gate 			nfs4delegreturn_cleanup_impl(rp, NULL, ncg);
15387c478bd9Sstevel@tonic-gate 			done = TRUE;
15397c478bd9Sstevel@tonic-gate 		}
15407c478bd9Sstevel@tonic-gate 
15417c478bd9Sstevel@tonic-gate 		nfs4_end_op(mi, vp, NULL, &recov_state, needrecov);
15427c478bd9Sstevel@tonic-gate 	}
15437c478bd9Sstevel@tonic-gate 	return (e.error);
15447c478bd9Sstevel@tonic-gate }
15457c478bd9Sstevel@tonic-gate 
15467c478bd9Sstevel@tonic-gate /*
15477c478bd9Sstevel@tonic-gate  * nfs4_resend_delegreturn - used to drive the delegreturn
15487c478bd9Sstevel@tonic-gate  * operation via the recovery thread.
15497c478bd9Sstevel@tonic-gate  */
15507c478bd9Sstevel@tonic-gate void
15517c478bd9Sstevel@tonic-gate nfs4_resend_delegreturn(nfs4_lost_rqst_t *lorp, nfs4_error_t *ep,
15527c478bd9Sstevel@tonic-gate 	nfs4_server_t *np)
15537c478bd9Sstevel@tonic-gate {
15547c478bd9Sstevel@tonic-gate 	rnode4_t *rp = VTOR4(lorp->lr_vp);
15557c478bd9Sstevel@tonic-gate 
15567c478bd9Sstevel@tonic-gate 	/* If the file failed recovery, just quit. */
15577c478bd9Sstevel@tonic-gate 	mutex_enter(&rp->r_statelock);
15587c478bd9Sstevel@tonic-gate 	if (rp->r_flags & R4RECOVERR) {
15597c478bd9Sstevel@tonic-gate 		ep->error = EIO;
15607c478bd9Sstevel@tonic-gate 	}
15617c478bd9Sstevel@tonic-gate 	mutex_exit(&rp->r_statelock);
15627c478bd9Sstevel@tonic-gate 
15637c478bd9Sstevel@tonic-gate 	if (!ep->error)
15647c478bd9Sstevel@tonic-gate 		nfs4delegreturn_otw(rp, lorp->lr_cr, ep);
15657c478bd9Sstevel@tonic-gate 
15667c478bd9Sstevel@tonic-gate 	/*
15677c478bd9Sstevel@tonic-gate 	 * If recovery is now needed, then return the error
15687c478bd9Sstevel@tonic-gate 	 * and status and let the recovery thread handle it,
15697c478bd9Sstevel@tonic-gate 	 * including re-driving another delegreturn.  Otherwise,
15707c478bd9Sstevel@tonic-gate 	 * just give up and clean up the delegation.
15717c478bd9Sstevel@tonic-gate 	 */
15727c478bd9Sstevel@tonic-gate 	if (nfs4_needs_recovery(ep, TRUE, lorp->lr_vp->v_vfsp))
15737c478bd9Sstevel@tonic-gate 		return;
15747c478bd9Sstevel@tonic-gate 
15757c478bd9Sstevel@tonic-gate 	if (rp->r_deleg_type != OPEN_DELEGATE_NONE)
15767c478bd9Sstevel@tonic-gate 		nfs4delegreturn_cleanup(rp, np);
15777c478bd9Sstevel@tonic-gate 
15787c478bd9Sstevel@tonic-gate 	nfs4_error_zinit(ep);
15797c478bd9Sstevel@tonic-gate }
15807c478bd9Sstevel@tonic-gate 
15817c478bd9Sstevel@tonic-gate /*
15827c478bd9Sstevel@tonic-gate  * nfs4delegreturn - general function to return a delegation.
15837c478bd9Sstevel@tonic-gate  *
15847c478bd9Sstevel@tonic-gate  * NFS4_DR_FORCE - return the delegation even if start_op fails
15857c478bd9Sstevel@tonic-gate  * NFS4_DR_PUSH - push modified data back to the server via VOP_PUTPAGE
15867c478bd9Sstevel@tonic-gate  * NFS4_DR_DISCARD - discard the delegation w/o delegreturn
15877c478bd9Sstevel@tonic-gate  * NFS4_DR_DID_OP - calling function already did nfs4_start_op
15887c478bd9Sstevel@tonic-gate  * NFS4_DR_RECALL - delegreturned initiated via CB_RECALL
15897c478bd9Sstevel@tonic-gate  * NFS4_DR_REOPEN - do file reopens, if applicable
15907c478bd9Sstevel@tonic-gate  */
15917c478bd9Sstevel@tonic-gate static int
15927c478bd9Sstevel@tonic-gate nfs4delegreturn_impl(rnode4_t *rp, int flags, struct nfs4_callback_globals *ncg)
15937c478bd9Sstevel@tonic-gate {
15947c478bd9Sstevel@tonic-gate 	int error = 0;
15957c478bd9Sstevel@tonic-gate 	cred_t *cr = NULL;
15967c478bd9Sstevel@tonic-gate 	vnode_t *vp;
15977c478bd9Sstevel@tonic-gate 	bool_t needrecov = FALSE;
15987c478bd9Sstevel@tonic-gate 	bool_t rw_entered = FALSE;
15997c478bd9Sstevel@tonic-gate 	bool_t do_reopen;
16007c478bd9Sstevel@tonic-gate 
16017c478bd9Sstevel@tonic-gate 	vp = RTOV4(rp);
16027c478bd9Sstevel@tonic-gate 
16037c478bd9Sstevel@tonic-gate 	/*
16047c478bd9Sstevel@tonic-gate 	 * If NFS4_DR_DISCARD is set by itself, take a short-cut and
16057c478bd9Sstevel@tonic-gate 	 * discard without doing an otw DELEGRETURN.  This may only be used
16067c478bd9Sstevel@tonic-gate 	 * by the recovery thread because it bypasses the synchronization
16077c478bd9Sstevel@tonic-gate 	 * with r_deleg_recall_lock and mi->mi_recovlock.
16087c478bd9Sstevel@tonic-gate 	 */
16097c478bd9Sstevel@tonic-gate 	if (flags == NFS4_DR_DISCARD) {
16107c478bd9Sstevel@tonic-gate 		nfs4delegreturn_cleanup_impl(rp, NULL, ncg);
16117c478bd9Sstevel@tonic-gate 		return (0);
16127c478bd9Sstevel@tonic-gate 	}
16137c478bd9Sstevel@tonic-gate 
16147c478bd9Sstevel@tonic-gate 	if (flags & NFS4_DR_DID_OP) {
16157c478bd9Sstevel@tonic-gate 		/*
16167c478bd9Sstevel@tonic-gate 		 * Caller had already done start_op, which means the
16177c478bd9Sstevel@tonic-gate 		 * r_deleg_recall_lock is already held in READ mode
16187c478bd9Sstevel@tonic-gate 		 * so we cannot take it in write mode.  Return the
16197c478bd9Sstevel@tonic-gate 		 * delegation asynchronously.
16207c478bd9Sstevel@tonic-gate 		 *
16217c478bd9Sstevel@tonic-gate 		 * Remove the NFS4_DR_DID_OP flag so we don't
16227c478bd9Sstevel@tonic-gate 		 * get stuck looping through here.
16237c478bd9Sstevel@tonic-gate 		 */
16247c478bd9Sstevel@tonic-gate 		VN_HOLD(vp);
16257c478bd9Sstevel@tonic-gate 		nfs4delegreturn_async(rp, (flags & ~NFS4_DR_DID_OP), FALSE);
16267c478bd9Sstevel@tonic-gate 		return (0);
16277c478bd9Sstevel@tonic-gate 	}
16287c478bd9Sstevel@tonic-gate 
16297c478bd9Sstevel@tonic-gate 	/*
163050af34d3Sthurlow 	 * Verify we still have a delegation and crhold the credential.
16317c478bd9Sstevel@tonic-gate 	 */
163250af34d3Sthurlow 	mutex_enter(&rp->r_statev4_lock);
163350af34d3Sthurlow 	if (rp->r_deleg_type == OPEN_DELEGATE_NONE) {
163450af34d3Sthurlow 		mutex_exit(&rp->r_statev4_lock);
16357c478bd9Sstevel@tonic-gate 		goto out;
163650af34d3Sthurlow 	}
16377c478bd9Sstevel@tonic-gate 	cr = rp->r_deleg_cred;
163850af34d3Sthurlow 	ASSERT(cr != NULL);
16397c478bd9Sstevel@tonic-gate 	crhold(cr);
164050af34d3Sthurlow 	mutex_exit(&rp->r_statev4_lock);
16417c478bd9Sstevel@tonic-gate 
16427c478bd9Sstevel@tonic-gate 	/*
16437c478bd9Sstevel@tonic-gate 	 * Push the modified data back to the server synchronously
16447c478bd9Sstevel@tonic-gate 	 * before doing DELEGRETURN.
16457c478bd9Sstevel@tonic-gate 	 */
16467c478bd9Sstevel@tonic-gate 	if (flags & NFS4_DR_PUSH)
1647da6c28aaSamw 		(void) VOP_PUTPAGE(vp, 0, 0, 0, cr, NULL);
16487c478bd9Sstevel@tonic-gate 
16497c478bd9Sstevel@tonic-gate 	/*
16507c478bd9Sstevel@tonic-gate 	 * Take r_deleg_recall_lock in WRITE mode, this will prevent
16517c478bd9Sstevel@tonic-gate 	 * nfs4_is_otw_open_necessary from trying to use the delegation
16527c478bd9Sstevel@tonic-gate 	 * while the DELEGRETURN is in progress.
16537c478bd9Sstevel@tonic-gate 	 */
16547c478bd9Sstevel@tonic-gate 	(void) nfs_rw_enter_sig(&rp->r_deleg_recall_lock, RW_WRITER, FALSE);
16557c478bd9Sstevel@tonic-gate 
16567c478bd9Sstevel@tonic-gate 	rw_entered = TRUE;
16577c478bd9Sstevel@tonic-gate 
16587c478bd9Sstevel@tonic-gate 	if (rp->r_deleg_type == OPEN_DELEGATE_NONE)
16597c478bd9Sstevel@tonic-gate 		goto out;
16607c478bd9Sstevel@tonic-gate 
16617c478bd9Sstevel@tonic-gate 	if (flags & NFS4_DR_REOPEN) {
16627c478bd9Sstevel@tonic-gate 		/*
16637c478bd9Sstevel@tonic-gate 		 * If R4RECOVERRP is already set, then skip re-opening
16647c478bd9Sstevel@tonic-gate 		 * the delegation open streams and go straight to doing
16657c478bd9Sstevel@tonic-gate 		 * delegreturn.  (XXX if the file has failed recovery, then the
16667c478bd9Sstevel@tonic-gate 		 * delegreturn attempt is likely to be futile.)
16677c478bd9Sstevel@tonic-gate 		 */
16687c478bd9Sstevel@tonic-gate 		mutex_enter(&rp->r_statelock);
16697c478bd9Sstevel@tonic-gate 		do_reopen = !(rp->r_flags & R4RECOVERRP);
16707c478bd9Sstevel@tonic-gate 		mutex_exit(&rp->r_statelock);
16717c478bd9Sstevel@tonic-gate 
16727c478bd9Sstevel@tonic-gate 		if (do_reopen) {
16737c478bd9Sstevel@tonic-gate 			error = deleg_reopen(vp, &needrecov, ncg, flags);
16747c478bd9Sstevel@tonic-gate 			if (error != 0) {
16757c478bd9Sstevel@tonic-gate 				if ((flags & (NFS4_DR_FORCE | NFS4_DR_RECALL))
16767c478bd9Sstevel@tonic-gate 				    == 0)
16777c478bd9Sstevel@tonic-gate 					goto out;
16787c478bd9Sstevel@tonic-gate 			} else if (needrecov) {
16797c478bd9Sstevel@tonic-gate 				if ((flags & NFS4_DR_FORCE) == 0)
16807c478bd9Sstevel@tonic-gate 					goto out;
16817c478bd9Sstevel@tonic-gate 			}
16827c478bd9Sstevel@tonic-gate 		}
16837c478bd9Sstevel@tonic-gate 	}
16847c478bd9Sstevel@tonic-gate 
16857c478bd9Sstevel@tonic-gate 	if (flags & NFS4_DR_DISCARD) {
16867c478bd9Sstevel@tonic-gate 		mntinfo4_t *mi = VTOMI4(RTOV4(rp));
16877c478bd9Sstevel@tonic-gate 
16887c478bd9Sstevel@tonic-gate 		mutex_enter(&rp->r_statelock);
16897c478bd9Sstevel@tonic-gate 		/*
16907c478bd9Sstevel@tonic-gate 		 * deleg_return_pending is cleared inside of delegation_accept
16917c478bd9Sstevel@tonic-gate 		 * when a delegation is accepted.  if this flag has been
16927c478bd9Sstevel@tonic-gate 		 * cleared, then a new delegation has overwritten the one we
16937c478bd9Sstevel@tonic-gate 		 * were about to throw away.
16947c478bd9Sstevel@tonic-gate 		 */
16957c478bd9Sstevel@tonic-gate 		if (!rp->r_deleg_return_pending) {
16967c478bd9Sstevel@tonic-gate 			mutex_exit(&rp->r_statelock);
16977c478bd9Sstevel@tonic-gate 			goto out;
16987c478bd9Sstevel@tonic-gate 		}
16997c478bd9Sstevel@tonic-gate 		mutex_exit(&rp->r_statelock);
17007c478bd9Sstevel@tonic-gate 		(void) nfs_rw_enter_sig(&mi->mi_recovlock, RW_READER, FALSE);
17017c478bd9Sstevel@tonic-gate 		nfs4delegreturn_cleanup_impl(rp, NULL, ncg);
17027c478bd9Sstevel@tonic-gate 		nfs_rw_exit(&mi->mi_recovlock);
17037c478bd9Sstevel@tonic-gate 	} else {
17047c478bd9Sstevel@tonic-gate 		error = nfs4_do_delegreturn(rp, flags, cr, ncg);
17057c478bd9Sstevel@tonic-gate 	}
17067c478bd9Sstevel@tonic-gate 
17077c478bd9Sstevel@tonic-gate out:
17087c478bd9Sstevel@tonic-gate 	if (cr)
17097c478bd9Sstevel@tonic-gate 		crfree(cr);
17107c478bd9Sstevel@tonic-gate 	if (rw_entered)
17117c478bd9Sstevel@tonic-gate 		nfs_rw_exit(&rp->r_deleg_recall_lock);
17127c478bd9Sstevel@tonic-gate 	return (error);
17137c478bd9Sstevel@tonic-gate }
17147c478bd9Sstevel@tonic-gate 
17157c478bd9Sstevel@tonic-gate int
17167c478bd9Sstevel@tonic-gate nfs4delegreturn(rnode4_t *rp, int flags)
17177c478bd9Sstevel@tonic-gate {
17187c478bd9Sstevel@tonic-gate 	struct nfs4_callback_globals *ncg;
17197c478bd9Sstevel@tonic-gate 
1720108322fbScarlsonj 	ncg = zone_getspecific(nfs4_callback_zone_key, nfs_zone());
17217c478bd9Sstevel@tonic-gate 	ASSERT(ncg != NULL);
17227c478bd9Sstevel@tonic-gate 
17237c478bd9Sstevel@tonic-gate 	return (nfs4delegreturn_impl(rp, flags, ncg));
17247c478bd9Sstevel@tonic-gate }
17257c478bd9Sstevel@tonic-gate 
17267c478bd9Sstevel@tonic-gate void
17277c478bd9Sstevel@tonic-gate nfs4delegreturn_async(rnode4_t *rp, int flags, bool_t trunc)
17287c478bd9Sstevel@tonic-gate {
17297c478bd9Sstevel@tonic-gate 	struct cb_recall_pass *pp;
17307c478bd9Sstevel@tonic-gate 
17317c478bd9Sstevel@tonic-gate 	pp = kmem_alloc(sizeof (struct cb_recall_pass), KM_SLEEP);
17327c478bd9Sstevel@tonic-gate 	pp->rp = rp;
17337c478bd9Sstevel@tonic-gate 	pp->flags = flags;
17347c478bd9Sstevel@tonic-gate 	pp->truncate = trunc;
17357c478bd9Sstevel@tonic-gate 
17367c478bd9Sstevel@tonic-gate 	/*
17377c478bd9Sstevel@tonic-gate 	 * Fire up a thread to do the actual delegreturn
17387c478bd9Sstevel@tonic-gate 	 * Caller must guarantee that the rnode doesn't
17397c478bd9Sstevel@tonic-gate 	 * vanish (by calling VN_HOLD).
17407c478bd9Sstevel@tonic-gate 	 */
17417c478bd9Sstevel@tonic-gate 
17427c478bd9Sstevel@tonic-gate 	(void) zthread_create(NULL, 0, nfs4delegreturn_thread, pp, 0,
17437c478bd9Sstevel@tonic-gate 	    minclsyspri);
17447c478bd9Sstevel@tonic-gate }
17457c478bd9Sstevel@tonic-gate 
17467c478bd9Sstevel@tonic-gate static void
17477c478bd9Sstevel@tonic-gate delegreturn_all_thread(rpcprog_t *pp)
17487c478bd9Sstevel@tonic-gate {
17497c478bd9Sstevel@tonic-gate 	nfs4_server_t *np;
17507c478bd9Sstevel@tonic-gate 	bool_t found = FALSE;
17517c478bd9Sstevel@tonic-gate 	rpcprog_t prog;
17527c478bd9Sstevel@tonic-gate 	rnode4_t *rp;
17537c478bd9Sstevel@tonic-gate 	vnode_t *vp;
17547c478bd9Sstevel@tonic-gate 	zoneid_t zoneid = getzoneid();
17557c478bd9Sstevel@tonic-gate 	struct nfs4_callback_globals *ncg;
17567c478bd9Sstevel@tonic-gate 
17577c478bd9Sstevel@tonic-gate 	NFS4_DEBUG(nfs4_drat_debug,
17587c478bd9Sstevel@tonic-gate 	    (CE_NOTE, "delereturn_all_thread: prog %d\n", *pp));
17597c478bd9Sstevel@tonic-gate 
17607c478bd9Sstevel@tonic-gate 	prog = *pp;
17617c478bd9Sstevel@tonic-gate 	kmem_free(pp, sizeof (*pp));
17627c478bd9Sstevel@tonic-gate 	pp = NULL;
17637c478bd9Sstevel@tonic-gate 
17647c478bd9Sstevel@tonic-gate 	mutex_enter(&nfs4_server_lst_lock);
17657c478bd9Sstevel@tonic-gate 	for (np = nfs4_server_lst.forw; np != &nfs4_server_lst; np = np->forw) {
17667c478bd9Sstevel@tonic-gate 		if (np->zoneid == zoneid && np->s_program == prog) {
17677c478bd9Sstevel@tonic-gate 			mutex_enter(&np->s_lock);
17687c478bd9Sstevel@tonic-gate 			found = TRUE;
17697c478bd9Sstevel@tonic-gate 			break;
17707c478bd9Sstevel@tonic-gate 		}
17717c478bd9Sstevel@tonic-gate 	}
17727c478bd9Sstevel@tonic-gate 	mutex_exit(&nfs4_server_lst_lock);
17737c478bd9Sstevel@tonic-gate 
17747c478bd9Sstevel@tonic-gate 	/*
17757c478bd9Sstevel@tonic-gate 	 * It's possible that the nfs4_server which was using this
17767c478bd9Sstevel@tonic-gate 	 * program number has vanished since this thread is async.
17777c478bd9Sstevel@tonic-gate 	 * If so, just return.  Your work here is finished, my friend.
17787c478bd9Sstevel@tonic-gate 	 */
17797c478bd9Sstevel@tonic-gate 	if (!found)
17807c478bd9Sstevel@tonic-gate 		goto out;
17817c478bd9Sstevel@tonic-gate 
17827c478bd9Sstevel@tonic-gate 	ncg = np->zone_globals;
17837c478bd9Sstevel@tonic-gate 	while ((rp = list_head(&np->s_deleg_list)) != NULL) {
17847c478bd9Sstevel@tonic-gate 		vp = RTOV4(rp);
17857c478bd9Sstevel@tonic-gate 		VN_HOLD(vp);
17867c478bd9Sstevel@tonic-gate 		mutex_exit(&np->s_lock);
17877c478bd9Sstevel@tonic-gate 		(void) nfs4delegreturn_impl(rp, NFS4_DR_PUSH|NFS4_DR_REOPEN,
17887c478bd9Sstevel@tonic-gate 		    ncg);
17897c478bd9Sstevel@tonic-gate 		VN_RELE(vp);
17907c478bd9Sstevel@tonic-gate 
17917c478bd9Sstevel@tonic-gate 		/* retake the s_lock for next trip through the loop */
17927c478bd9Sstevel@tonic-gate 		mutex_enter(&np->s_lock);
17937c478bd9Sstevel@tonic-gate 	}
17947c478bd9Sstevel@tonic-gate 	mutex_exit(&np->s_lock);
17957c478bd9Sstevel@tonic-gate out:
17967c478bd9Sstevel@tonic-gate 	NFS4_DEBUG(nfs4_drat_debug,
17977c478bd9Sstevel@tonic-gate 	    (CE_NOTE, "delereturn_all_thread: complete\n"));
17987c478bd9Sstevel@tonic-gate 	zthread_exit();
17997c478bd9Sstevel@tonic-gate }
18007c478bd9Sstevel@tonic-gate 
18017c478bd9Sstevel@tonic-gate void
18027c478bd9Sstevel@tonic-gate nfs4_delegreturn_all(nfs4_server_t *sp)
18037c478bd9Sstevel@tonic-gate {
18047c478bd9Sstevel@tonic-gate 	rpcprog_t pro, *pp;
18057c478bd9Sstevel@tonic-gate 
18067c478bd9Sstevel@tonic-gate 	mutex_enter(&sp->s_lock);
18077c478bd9Sstevel@tonic-gate 
18087c478bd9Sstevel@tonic-gate 	/* Check to see if the delegation list is empty */
18097c478bd9Sstevel@tonic-gate 
18107c478bd9Sstevel@tonic-gate 	if (list_head(&sp->s_deleg_list) == NULL) {
18117c478bd9Sstevel@tonic-gate 		mutex_exit(&sp->s_lock);
18127c478bd9Sstevel@tonic-gate 		return;
18137c478bd9Sstevel@tonic-gate 	}
18147c478bd9Sstevel@tonic-gate 	/*
18157c478bd9Sstevel@tonic-gate 	 * Grab the program number; the async thread will use this
18167c478bd9Sstevel@tonic-gate 	 * to find the nfs4_server.
18177c478bd9Sstevel@tonic-gate 	 */
18187c478bd9Sstevel@tonic-gate 	pro = sp->s_program;
18197c478bd9Sstevel@tonic-gate 	mutex_exit(&sp->s_lock);
18207c478bd9Sstevel@tonic-gate 	pp = kmem_alloc(sizeof (rpcprog_t), KM_SLEEP);
18217c478bd9Sstevel@tonic-gate 	*pp = pro;
18227c478bd9Sstevel@tonic-gate 	(void) zthread_create(NULL, 0, delegreturn_all_thread, pp, 0,
18237c478bd9Sstevel@tonic-gate 	    minclsyspri);
18247c478bd9Sstevel@tonic-gate }
18257c478bd9Sstevel@tonic-gate 
18267c478bd9Sstevel@tonic-gate 
18277c478bd9Sstevel@tonic-gate /*
18287c478bd9Sstevel@tonic-gate  * Discard any delegations
18297c478bd9Sstevel@tonic-gate  *
18307c478bd9Sstevel@tonic-gate  * Iterate over the servers s_deleg_list and
18317c478bd9Sstevel@tonic-gate  * for matching mount-point rnodes discard
18327c478bd9Sstevel@tonic-gate  * the delegation.
18337c478bd9Sstevel@tonic-gate  */
18347c478bd9Sstevel@tonic-gate void
18357c478bd9Sstevel@tonic-gate nfs4_deleg_discard(mntinfo4_t *mi, nfs4_server_t *sp)
18367c478bd9Sstevel@tonic-gate {
18377c478bd9Sstevel@tonic-gate 	rnode4_t *rp, *next;
18387c478bd9Sstevel@tonic-gate 	mntinfo4_t *r_mi;
18397c478bd9Sstevel@tonic-gate 	struct nfs4_callback_globals *ncg;
18407c478bd9Sstevel@tonic-gate 
18417c478bd9Sstevel@tonic-gate 	ASSERT(mutex_owned(&sp->s_lock));
18427c478bd9Sstevel@tonic-gate 	ncg = sp->zone_globals;
18437c478bd9Sstevel@tonic-gate 
18447c478bd9Sstevel@tonic-gate 	for (rp = list_head(&sp->s_deleg_list); rp != NULL; rp = next) {
18457c478bd9Sstevel@tonic-gate 		r_mi = VTOMI4(RTOV4(rp));
18467c478bd9Sstevel@tonic-gate 		next = list_next(&sp->s_deleg_list, rp);
18477c478bd9Sstevel@tonic-gate 
18487c478bd9Sstevel@tonic-gate 		if (r_mi != mi) {
18497c478bd9Sstevel@tonic-gate 			/*
18507c478bd9Sstevel@tonic-gate 			 * Skip if this rnode is in not on the
18517c478bd9Sstevel@tonic-gate 			 * same mount-point
18527c478bd9Sstevel@tonic-gate 			 */
18537c478bd9Sstevel@tonic-gate 			continue;
18547c478bd9Sstevel@tonic-gate 		}
18557c478bd9Sstevel@tonic-gate 
18567c478bd9Sstevel@tonic-gate 		ASSERT(rp->r_deleg_type == OPEN_DELEGATE_READ);
18577c478bd9Sstevel@tonic-gate 
18587c478bd9Sstevel@tonic-gate #ifdef DEBUG
18597c478bd9Sstevel@tonic-gate 		if (nfs4_client_recov_debug) {
18607c478bd9Sstevel@tonic-gate 			zprintf(getzoneid(),
18617c478bd9Sstevel@tonic-gate 			    "nfs4_deleg_discard: matched rnode %p "
18627c478bd9Sstevel@tonic-gate 			"-- discarding delegation\n", (void *)rp);
18637c478bd9Sstevel@tonic-gate 		}
18647c478bd9Sstevel@tonic-gate #endif
18657c478bd9Sstevel@tonic-gate 		mutex_enter(&rp->r_statev4_lock);
18667c478bd9Sstevel@tonic-gate 		/*
18677c478bd9Sstevel@tonic-gate 		 * Free the cred originally held when the delegation
18687c478bd9Sstevel@tonic-gate 		 * was granted. Also need to decrement the refcnt
18697c478bd9Sstevel@tonic-gate 		 * on this server for each delegation we discard
18707c478bd9Sstevel@tonic-gate 		 */
18717c478bd9Sstevel@tonic-gate 		if (rp->r_deleg_cred)
18727c478bd9Sstevel@tonic-gate 			crfree(rp->r_deleg_cred);
18737c478bd9Sstevel@tonic-gate 		rp->r_deleg_cred = NULL;
18747c478bd9Sstevel@tonic-gate 		rp->r_deleg_type = OPEN_DELEGATE_NONE;
18757c478bd9Sstevel@tonic-gate 		rp->r_deleg_needs_recovery = OPEN_DELEGATE_NONE;
18767c478bd9Sstevel@tonic-gate 		rp->r_deleg_needs_recall = FALSE;
18777c478bd9Sstevel@tonic-gate 		ASSERT(sp->s_refcnt > 1);
18787c478bd9Sstevel@tonic-gate 		sp->s_refcnt--;
18797c478bd9Sstevel@tonic-gate 		list_remove(&sp->s_deleg_list, rp);
18807c478bd9Sstevel@tonic-gate 		mutex_exit(&rp->r_statev4_lock);
18817c478bd9Sstevel@tonic-gate 		nfs4_dec_state_ref_count_nolock(sp, mi);
18827c478bd9Sstevel@tonic-gate 		ncg->nfs4_callback_stats.delegations.value.ui64--;
18837c478bd9Sstevel@tonic-gate 	}
18847c478bd9Sstevel@tonic-gate }
18857c478bd9Sstevel@tonic-gate 
18867c478bd9Sstevel@tonic-gate /*
18877c478bd9Sstevel@tonic-gate  * Reopen any open streams that were covered by the given file's
18887c478bd9Sstevel@tonic-gate  * delegation.
18897c478bd9Sstevel@tonic-gate  * Returns zero or an errno value.  If there was no error, *recovp
18907c478bd9Sstevel@tonic-gate  * indicates whether recovery was initiated.
18917c478bd9Sstevel@tonic-gate  */
18927c478bd9Sstevel@tonic-gate 
18937c478bd9Sstevel@tonic-gate static int
18947c478bd9Sstevel@tonic-gate deleg_reopen(vnode_t *vp, bool_t *recovp, struct nfs4_callback_globals *ncg,
18957c478bd9Sstevel@tonic-gate 	int flags)
18967c478bd9Sstevel@tonic-gate {
18977c478bd9Sstevel@tonic-gate 	nfs4_open_stream_t *osp;
18987c478bd9Sstevel@tonic-gate 	nfs4_recov_state_t recov_state;
18997c478bd9Sstevel@tonic-gate 	bool_t needrecov = FALSE;
19007c478bd9Sstevel@tonic-gate 	mntinfo4_t *mi;
19017c478bd9Sstevel@tonic-gate 	rnode4_t *rp;
19027c478bd9Sstevel@tonic-gate 	nfs4_error_t e = { 0, NFS4_OK, RPC_SUCCESS };
19037c478bd9Sstevel@tonic-gate 	int claimnull;
19047c478bd9Sstevel@tonic-gate 
19057c478bd9Sstevel@tonic-gate 	mi = VTOMI4(vp);
19067c478bd9Sstevel@tonic-gate 	rp = VTOR4(vp);
19077c478bd9Sstevel@tonic-gate 
19087c478bd9Sstevel@tonic-gate 	recov_state.rs_flags = 0;
19097c478bd9Sstevel@tonic-gate 	recov_state.rs_num_retry_despite_err = 0;
19107c478bd9Sstevel@tonic-gate 
19117c478bd9Sstevel@tonic-gate retry:
19127c478bd9Sstevel@tonic-gate 	if ((e.error = nfs4_start_op(mi, vp, NULL, &recov_state)) != 0) {
19137c478bd9Sstevel@tonic-gate 		return (e.error);
19147c478bd9Sstevel@tonic-gate 	}
19157c478bd9Sstevel@tonic-gate 
19167c478bd9Sstevel@tonic-gate 	/*
19177c478bd9Sstevel@tonic-gate 	 * if we mean to discard the delegation, it must be BAD, so don't
19187c478bd9Sstevel@tonic-gate 	 * use it when doing the reopen or it will fail too.
19197c478bd9Sstevel@tonic-gate 	 */
19207c478bd9Sstevel@tonic-gate 	claimnull = (flags & NFS4_DR_DISCARD);
19217c478bd9Sstevel@tonic-gate 	/*
19227c478bd9Sstevel@tonic-gate 	 * Loop through the open streams for this rnode to find
19237c478bd9Sstevel@tonic-gate 	 * all of the ones created using the delegation state ID.
19247c478bd9Sstevel@tonic-gate 	 * Each of these needs to be re-opened.
19257c478bd9Sstevel@tonic-gate 	 */
19267c478bd9Sstevel@tonic-gate 
19277c478bd9Sstevel@tonic-gate 	while ((osp = get_next_deleg_stream(rp, claimnull)) != NULL) {
19287c478bd9Sstevel@tonic-gate 
19297c478bd9Sstevel@tonic-gate 		if (claimnull) {
19307c478bd9Sstevel@tonic-gate 			nfs4_reopen(vp, osp, &e, CLAIM_NULL, FALSE, FALSE);
19317c478bd9Sstevel@tonic-gate 		} else {
19327c478bd9Sstevel@tonic-gate 			ncg->nfs4_callback_stats.claim_cur.value.ui64++;
19337c478bd9Sstevel@tonic-gate 
19347c478bd9Sstevel@tonic-gate 			nfs4_reopen(vp, osp, &e, CLAIM_DELEGATE_CUR, FALSE,
19357c478bd9Sstevel@tonic-gate 			    FALSE);
19367c478bd9Sstevel@tonic-gate 			if (e.error == 0 && e.stat == NFS4_OK)
19377c478bd9Sstevel@tonic-gate 				ncg->nfs4_callback_stats.
19387c478bd9Sstevel@tonic-gate 				    claim_cur_ok.value.ui64++;
19397c478bd9Sstevel@tonic-gate 		}
19407c478bd9Sstevel@tonic-gate 
19417c478bd9Sstevel@tonic-gate 		if (e.error == EAGAIN) {
194249fc7055SSimon Klinkert 			open_stream_rele(osp, rp);
19437c478bd9Sstevel@tonic-gate 			nfs4_end_op(mi, vp, NULL, &recov_state, TRUE);
19447c478bd9Sstevel@tonic-gate 			goto retry;
19457c478bd9Sstevel@tonic-gate 		}
19467c478bd9Sstevel@tonic-gate 
19477c478bd9Sstevel@tonic-gate 		/*
19487c478bd9Sstevel@tonic-gate 		 * if error is EINTR, ETIMEDOUT, or NFS4_FRC_UNMT_ERR, then
19497c478bd9Sstevel@tonic-gate 		 * recovery has already been started inside of nfs4_reopen.
19507c478bd9Sstevel@tonic-gate 		 */
19517c478bd9Sstevel@tonic-gate 		if (e.error == EINTR || e.error == ETIMEDOUT ||
19527c478bd9Sstevel@tonic-gate 		    NFS4_FRC_UNMT_ERR(e.error, vp->v_vfsp)) {
19537c478bd9Sstevel@tonic-gate 			open_stream_rele(osp, rp);
19547c478bd9Sstevel@tonic-gate 			break;
19557c478bd9Sstevel@tonic-gate 		}
19567c478bd9Sstevel@tonic-gate 
19577c478bd9Sstevel@tonic-gate 		needrecov = nfs4_needs_recovery(&e, TRUE, vp->v_vfsp);
19587c478bd9Sstevel@tonic-gate 
19597c478bd9Sstevel@tonic-gate 		if (e.error != 0 && !needrecov) {
19607c478bd9Sstevel@tonic-gate 			/*
19617c478bd9Sstevel@tonic-gate 			 * Recovery is not possible, but don't give up yet;
19627c478bd9Sstevel@tonic-gate 			 * we'd still like to do delegreturn after
19637c478bd9Sstevel@tonic-gate 			 * reopening as many streams as possible.
19647c478bd9Sstevel@tonic-gate 			 * Continue processing the open streams.
19657c478bd9Sstevel@tonic-gate 			 */
19667c478bd9Sstevel@tonic-gate 
19677c478bd9Sstevel@tonic-gate 			ncg->nfs4_callback_stats.recall_failed.value.ui64++;
19687c478bd9Sstevel@tonic-gate 
19697c478bd9Sstevel@tonic-gate 		} else if (needrecov) {
19707c478bd9Sstevel@tonic-gate 			/*
19717c478bd9Sstevel@tonic-gate 			 * Start recovery and bail out.  The recovery
19727c478bd9Sstevel@tonic-gate 			 * thread will take it from here.
19737c478bd9Sstevel@tonic-gate 			 */
19747c478bd9Sstevel@tonic-gate 			(void) nfs4_start_recovery(&e, mi, vp, NULL, NULL,
19752f172c55SRobert Thurlow 			    NULL, OP_OPEN, NULL, NULL, NULL);
19767c478bd9Sstevel@tonic-gate 			open_stream_rele(osp, rp);
19777c478bd9Sstevel@tonic-gate 			*recovp = TRUE;
19787c478bd9Sstevel@tonic-gate 			break;
19797c478bd9Sstevel@tonic-gate 		}
19807c478bd9Sstevel@tonic-gate 
19817c478bd9Sstevel@tonic-gate 		open_stream_rele(osp, rp);
19827c478bd9Sstevel@tonic-gate 	}
19837c478bd9Sstevel@tonic-gate 
19847c478bd9Sstevel@tonic-gate 	nfs4_end_op(mi, vp, NULL, &recov_state, needrecov);
19857c478bd9Sstevel@tonic-gate 
19867c478bd9Sstevel@tonic-gate 	return (e.error);
19877c478bd9Sstevel@tonic-gate }
19887c478bd9Sstevel@tonic-gate 
19897c478bd9Sstevel@tonic-gate /*
19907c478bd9Sstevel@tonic-gate  * get_next_deleg_stream - returns the next open stream which
19917c478bd9Sstevel@tonic-gate  * represents a delegation for this rnode.  In order to assure
19927c478bd9Sstevel@tonic-gate  * forward progress, the caller must guarantee that each open
19937c478bd9Sstevel@tonic-gate  * stream returned is changed so that a future call won't return
19947c478bd9Sstevel@tonic-gate  * it again.
19957c478bd9Sstevel@tonic-gate  *
19967c478bd9Sstevel@tonic-gate  * There are several ways for the open stream to change.  If the open
19977c478bd9Sstevel@tonic-gate  * stream is !os_delegation, then we aren't interested in it.  Also, if
19987c478bd9Sstevel@tonic-gate  * either os_failed_reopen or !os_valid, then don't return the osp.
19997c478bd9Sstevel@tonic-gate  *
20007c478bd9Sstevel@tonic-gate  * If claimnull is false (doing reopen CLAIM_DELEGATE_CUR) then return
20017c478bd9Sstevel@tonic-gate  * the osp if it is an os_delegation open stream.  Also, if the rnode still
20027c478bd9Sstevel@tonic-gate  * has r_deleg_return_pending, then return the os_delegation osp.  Lastly,
20037c478bd9Sstevel@tonic-gate  * if the rnode's r_deleg_stateid is different from the osp's open_stateid,
20047c478bd9Sstevel@tonic-gate  * then return the osp.
20057c478bd9Sstevel@tonic-gate  *
20067c478bd9Sstevel@tonic-gate  * We have already taken the 'r_deleg_recall_lock' as WRITER, which
20077c478bd9Sstevel@tonic-gate  * prevents new OPENs from going OTW (as start_fop takes this
20087c478bd9Sstevel@tonic-gate  * lock in READ mode); thus, no new open streams can be created
2009da6c28aaSamw  * (which inherently means no new delegation open streams are
20107c478bd9Sstevel@tonic-gate  * being created).
20117c478bd9Sstevel@tonic-gate  */
20127c478bd9Sstevel@tonic-gate 
20137c478bd9Sstevel@tonic-gate static nfs4_open_stream_t *
20147c478bd9Sstevel@tonic-gate get_next_deleg_stream(rnode4_t *rp, int claimnull)
20157c478bd9Sstevel@tonic-gate {
20167c478bd9Sstevel@tonic-gate 	nfs4_open_stream_t	*osp;
20177c478bd9Sstevel@tonic-gate 
20187c478bd9Sstevel@tonic-gate 	ASSERT(nfs_rw_lock_held(&rp->r_deleg_recall_lock, RW_WRITER));
20197c478bd9Sstevel@tonic-gate 
20207c478bd9Sstevel@tonic-gate 	/*
20217c478bd9Sstevel@tonic-gate 	 * Search through the list of open streams looking for
20227c478bd9Sstevel@tonic-gate 	 * one that was created while holding the delegation.
20237c478bd9Sstevel@tonic-gate 	 */
20247c478bd9Sstevel@tonic-gate 	mutex_enter(&rp->r_os_lock);
20257c478bd9Sstevel@tonic-gate 	for (osp = list_head(&rp->r_open_streams); osp != NULL;
20267c478bd9Sstevel@tonic-gate 	    osp = list_next(&rp->r_open_streams, osp)) {
20277c478bd9Sstevel@tonic-gate 		mutex_enter(&osp->os_sync_lock);
20287c478bd9Sstevel@tonic-gate 		if (!osp->os_delegation || osp->os_failed_reopen ||
20297c478bd9Sstevel@tonic-gate 		    !osp->os_valid) {
20307c478bd9Sstevel@tonic-gate 			mutex_exit(&osp->os_sync_lock);
20317c478bd9Sstevel@tonic-gate 			continue;
20327c478bd9Sstevel@tonic-gate 		}
20337c478bd9Sstevel@tonic-gate 		if (!claimnull || rp->r_deleg_return_pending ||
20347c478bd9Sstevel@tonic-gate 		    !stateid4_cmp(&osp->open_stateid, &rp->r_deleg_stateid)) {
20357c478bd9Sstevel@tonic-gate 			osp->os_ref_count++;
20367c478bd9Sstevel@tonic-gate 			mutex_exit(&osp->os_sync_lock);
20377c478bd9Sstevel@tonic-gate 			mutex_exit(&rp->r_os_lock);
20387c478bd9Sstevel@tonic-gate 			return (osp);
20397c478bd9Sstevel@tonic-gate 		}
20407c478bd9Sstevel@tonic-gate 		mutex_exit(&osp->os_sync_lock);
20417c478bd9Sstevel@tonic-gate 	}
20427c478bd9Sstevel@tonic-gate 	mutex_exit(&rp->r_os_lock);
20437c478bd9Sstevel@tonic-gate 
20447c478bd9Sstevel@tonic-gate 	return (NULL);
20457c478bd9Sstevel@tonic-gate }
20467c478bd9Sstevel@tonic-gate 
20477c478bd9Sstevel@tonic-gate static void
20487c478bd9Sstevel@tonic-gate nfs4delegreturn_thread(struct cb_recall_pass *args)
20497c478bd9Sstevel@tonic-gate {
20507c478bd9Sstevel@tonic-gate 	rnode4_t *rp;
20517c478bd9Sstevel@tonic-gate 	vnode_t *vp;
20527c478bd9Sstevel@tonic-gate 	cred_t *cr;
20537c478bd9Sstevel@tonic-gate 	int dtype, error, flags;
20547c478bd9Sstevel@tonic-gate 	bool_t rdirty, rip;
20557c478bd9Sstevel@tonic-gate 	kmutex_t cpr_lock;
20567c478bd9Sstevel@tonic-gate 	callb_cpr_t cpr_info;
20577c478bd9Sstevel@tonic-gate 	struct nfs4_callback_globals *ncg;
20587c478bd9Sstevel@tonic-gate 
2059108322fbScarlsonj 	ncg = zone_getspecific(nfs4_callback_zone_key, nfs_zone());
20607c478bd9Sstevel@tonic-gate 	ASSERT(ncg != NULL);
20617c478bd9Sstevel@tonic-gate 
20627c478bd9Sstevel@tonic-gate 	mutex_init(&cpr_lock, NULL, MUTEX_DEFAULT, NULL);
20637c478bd9Sstevel@tonic-gate 
20647c478bd9Sstevel@tonic-gate 	CALLB_CPR_INIT(&cpr_info, &cpr_lock, callb_generic_cpr,
20657c478bd9Sstevel@tonic-gate 	    "nfsv4delegRtn");
20667c478bd9Sstevel@tonic-gate 
20677c478bd9Sstevel@tonic-gate 	rp = args->rp;
20687c478bd9Sstevel@tonic-gate 	vp = RTOV4(rp);
20697c478bd9Sstevel@tonic-gate 
20707c478bd9Sstevel@tonic-gate 	mutex_enter(&rp->r_statev4_lock);
20717c478bd9Sstevel@tonic-gate 	if (rp->r_deleg_type == OPEN_DELEGATE_NONE) {
20727c478bd9Sstevel@tonic-gate 		mutex_exit(&rp->r_statev4_lock);
20737c478bd9Sstevel@tonic-gate 		goto out;
20747c478bd9Sstevel@tonic-gate 	}
20757c478bd9Sstevel@tonic-gate 	mutex_exit(&rp->r_statev4_lock);
20767c478bd9Sstevel@tonic-gate 
20777c478bd9Sstevel@tonic-gate 	/*
20787c478bd9Sstevel@tonic-gate 	 * Take the read-write lock in read mode to prevent other
20797c478bd9Sstevel@tonic-gate 	 * threads from modifying the data during the recall.  This
20807c478bd9Sstevel@tonic-gate 	 * doesn't affect mmappers.
20817c478bd9Sstevel@tonic-gate 	 */
20827c478bd9Sstevel@tonic-gate 	(void) nfs_rw_enter_sig(&rp->r_rwlock, RW_READER, FALSE);
20837c478bd9Sstevel@tonic-gate 
20847c478bd9Sstevel@tonic-gate 	/* Proceed with delegreturn */
20857c478bd9Sstevel@tonic-gate 
20867c478bd9Sstevel@tonic-gate 	mutex_enter(&rp->r_statev4_lock);
20877c478bd9Sstevel@tonic-gate 	if (rp->r_deleg_type == OPEN_DELEGATE_NONE) {
20887c478bd9Sstevel@tonic-gate 		mutex_exit(&rp->r_statev4_lock);
20897c478bd9Sstevel@tonic-gate 		nfs_rw_exit(&rp->r_rwlock);
20907c478bd9Sstevel@tonic-gate 		goto out;
20917c478bd9Sstevel@tonic-gate 	}
20927c478bd9Sstevel@tonic-gate 	dtype = rp->r_deleg_type;
20937c478bd9Sstevel@tonic-gate 	cr = rp->r_deleg_cred;
20947c478bd9Sstevel@tonic-gate 	ASSERT(cr != NULL);
20957c478bd9Sstevel@tonic-gate 	crhold(cr);
20967c478bd9Sstevel@tonic-gate 	mutex_exit(&rp->r_statev4_lock);
20977c478bd9Sstevel@tonic-gate 
20987c478bd9Sstevel@tonic-gate 	flags = args->flags;
20997c478bd9Sstevel@tonic-gate 
21007c478bd9Sstevel@tonic-gate 	/*
21017c478bd9Sstevel@tonic-gate 	 * If the file is being truncated at the server, then throw
21027c478bd9Sstevel@tonic-gate 	 * away all of the pages, it doesn't matter what flavor of
21037c478bd9Sstevel@tonic-gate 	 * delegation we have.
21047c478bd9Sstevel@tonic-gate 	 */
21057c478bd9Sstevel@tonic-gate 
21067c478bd9Sstevel@tonic-gate 	if (args->truncate) {
21077c478bd9Sstevel@tonic-gate 		ncg->nfs4_callback_stats.recall_trunc.value.ui64++;
21087c478bd9Sstevel@tonic-gate 		nfs4_invalidate_pages(vp, 0, cr);
21097c478bd9Sstevel@tonic-gate 	} else if (dtype == OPEN_DELEGATE_WRITE) {
21107c478bd9Sstevel@tonic-gate 
21117c478bd9Sstevel@tonic-gate 		mutex_enter(&rp->r_statelock);
21127c478bd9Sstevel@tonic-gate 		rdirty = rp->r_flags & R4DIRTY;
21137c478bd9Sstevel@tonic-gate 		mutex_exit(&rp->r_statelock);
21147c478bd9Sstevel@tonic-gate 
21157c478bd9Sstevel@tonic-gate 		if (rdirty) {
2116da6c28aaSamw 			error = VOP_PUTPAGE(vp, 0, 0, 0, cr, NULL);
21177c478bd9Sstevel@tonic-gate 
21187c478bd9Sstevel@tonic-gate 			if (error)
21197c478bd9Sstevel@tonic-gate 				CB_WARN1("nfs4delegreturn_thread:"
21207c478bd9Sstevel@tonic-gate 				" VOP_PUTPAGE: %d\n", error);
21217c478bd9Sstevel@tonic-gate 		}
21227c478bd9Sstevel@tonic-gate 		/* turn off NFS4_DR_PUSH because we just did that above. */
21237c478bd9Sstevel@tonic-gate 		flags &= ~NFS4_DR_PUSH;
21247c478bd9Sstevel@tonic-gate 	}
21257c478bd9Sstevel@tonic-gate 
21267c478bd9Sstevel@tonic-gate 	mutex_enter(&rp->r_statelock);
21277c478bd9Sstevel@tonic-gate 	rip =  rp->r_flags & R4RECOVERRP;
21287c478bd9Sstevel@tonic-gate 	mutex_exit(&rp->r_statelock);
21297c478bd9Sstevel@tonic-gate 
21307c478bd9Sstevel@tonic-gate 	/* If a failed recovery is indicated, discard the pages */
21317c478bd9Sstevel@tonic-gate 
21327c478bd9Sstevel@tonic-gate 	if (rip) {
21337c478bd9Sstevel@tonic-gate 
2134da6c28aaSamw 		error = VOP_PUTPAGE(vp, 0, 0, B_INVAL, cr, NULL);
21357c478bd9Sstevel@tonic-gate 
21367c478bd9Sstevel@tonic-gate 		if (error)
21377c478bd9Sstevel@tonic-gate 			CB_WARN1("nfs4delegreturn_thread: VOP_PUTPAGE: %d\n",
21387c478bd9Sstevel@tonic-gate 			    error);
21397c478bd9Sstevel@tonic-gate 	}
21407c478bd9Sstevel@tonic-gate 
21417c478bd9Sstevel@tonic-gate 	/*
21427c478bd9Sstevel@tonic-gate 	 * Pass the flags to nfs4delegreturn_impl, but be sure not to pass
21437c478bd9Sstevel@tonic-gate 	 * NFS4_DR_DID_OP, which just calls nfs4delegreturn_async again.
21447c478bd9Sstevel@tonic-gate 	 */
21457c478bd9Sstevel@tonic-gate 	flags &= ~NFS4_DR_DID_OP;
21467c478bd9Sstevel@tonic-gate 
21477c478bd9Sstevel@tonic-gate 	(void) nfs4delegreturn_impl(rp, flags, ncg);
21487c478bd9Sstevel@tonic-gate 
21497c478bd9Sstevel@tonic-gate 	nfs_rw_exit(&rp->r_rwlock);
21507c478bd9Sstevel@tonic-gate 	crfree(cr);
21517c478bd9Sstevel@tonic-gate out:
21527c478bd9Sstevel@tonic-gate 	kmem_free(args, sizeof (struct cb_recall_pass));
21537c478bd9Sstevel@tonic-gate 	VN_RELE(vp);
21547c478bd9Sstevel@tonic-gate 	mutex_enter(&cpr_lock);
21557c478bd9Sstevel@tonic-gate 	CALLB_CPR_EXIT(&cpr_info);
21567c478bd9Sstevel@tonic-gate 	mutex_destroy(&cpr_lock);
21577c478bd9Sstevel@tonic-gate 	zthread_exit();
21587c478bd9Sstevel@tonic-gate }
21597c478bd9Sstevel@tonic-gate 
21607c478bd9Sstevel@tonic-gate /*
21617c478bd9Sstevel@tonic-gate  * This function has one assumption that the caller of this function is
21627c478bd9Sstevel@tonic-gate  * either doing recovery (therefore cannot call nfs4_start_op) or has
21637c478bd9Sstevel@tonic-gate  * already called nfs4_start_op().
21647c478bd9Sstevel@tonic-gate  */
21657c478bd9Sstevel@tonic-gate void
21667c478bd9Sstevel@tonic-gate nfs4_delegation_accept(rnode4_t *rp, open_claim_type4 claim, OPEN4res *res,
21677c478bd9Sstevel@tonic-gate 	nfs4_ga_res_t *garp, cred_t *cr)
21687c478bd9Sstevel@tonic-gate {
21697c478bd9Sstevel@tonic-gate 	open_read_delegation4 *orp;
21707c478bd9Sstevel@tonic-gate 	open_write_delegation4 *owp;
21717c478bd9Sstevel@tonic-gate 	nfs4_server_t *np;
21727c478bd9Sstevel@tonic-gate 	bool_t already = FALSE;
21737c478bd9Sstevel@tonic-gate 	bool_t recall = FALSE;
21747c478bd9Sstevel@tonic-gate 	bool_t valid_garp = TRUE;
21759ea21c83Swebaker 	bool_t delegation_granted = FALSE;
21769ea21c83Swebaker 	bool_t dr_needed = FALSE;
21779ea21c83Swebaker 	bool_t recov;
21789ea21c83Swebaker 	int dr_flags = 0;
21797c478bd9Sstevel@tonic-gate 	long mapcnt;
21807c478bd9Sstevel@tonic-gate 	uint_t rflag;
21817c478bd9Sstevel@tonic-gate 	mntinfo4_t *mi;
21827c478bd9Sstevel@tonic-gate 	struct nfs4_callback_globals *ncg;
2183ec38504dSrmesta 	open_delegation_type4 odt;
21847c478bd9Sstevel@tonic-gate 
2185108322fbScarlsonj 	ncg = zone_getspecific(nfs4_callback_zone_key, nfs_zone());
21867c478bd9Sstevel@tonic-gate 	ASSERT(ncg != NULL);
21877c478bd9Sstevel@tonic-gate 
21889ea21c83Swebaker 	mi = VTOMI4(RTOV4(rp));
21899ea21c83Swebaker 
21909ea21c83Swebaker 	/*
21919ea21c83Swebaker 	 * Accept a delegation granted to the client via an OPEN.
21929ea21c83Swebaker 	 * Set the delegation fields in the rnode and insert the
21939ea21c83Swebaker 	 * rnode onto the list anchored in the nfs4_server_t.  The
21949ea21c83Swebaker 	 * proper locking order requires the nfs4_server_t first,
21959ea21c83Swebaker 	 * even though it may not be needed in all cases.
21969ea21c83Swebaker 	 *
21979ea21c83Swebaker 	 * NB: find_nfs4_server returns with s_lock held.
21989ea21c83Swebaker 	 */
21999ea21c83Swebaker 
22009ea21c83Swebaker 	if ((np = find_nfs4_server(mi)) == NULL)
22019ea21c83Swebaker 		return;
22029ea21c83Swebaker 
22039ea21c83Swebaker 	/* grab the statelock too, for examining r_mapcnt */
22049ea21c83Swebaker 	mutex_enter(&rp->r_statelock);
22057c478bd9Sstevel@tonic-gate 	mutex_enter(&rp->r_statev4_lock);
22067c478bd9Sstevel@tonic-gate 
22077c478bd9Sstevel@tonic-gate 	if (rp->r_deleg_type == OPEN_DELEGATE_READ ||
22087c478bd9Sstevel@tonic-gate 	    rp->r_deleg_type == OPEN_DELEGATE_WRITE)
22097c478bd9Sstevel@tonic-gate 		already = TRUE;
22107c478bd9Sstevel@tonic-gate 
2211ec38504dSrmesta 	odt = res->delegation.delegation_type;
22127c478bd9Sstevel@tonic-gate 
2213ec38504dSrmesta 	if (odt == OPEN_DELEGATE_READ) {
2214ec38504dSrmesta 
22157c478bd9Sstevel@tonic-gate 		rp->r_deleg_type = res->delegation.delegation_type;
22167c478bd9Sstevel@tonic-gate 		orp = &res->delegation.open_delegation4_u.read;
22177c478bd9Sstevel@tonic-gate 		rp->r_deleg_stateid = orp->stateid;
22187c478bd9Sstevel@tonic-gate 		rp->r_deleg_perms = orp->permissions;
22199ea21c83Swebaker 		if (claim == CLAIM_PREVIOUS)
22209ea21c83Swebaker 			if ((recall = orp->recall) != 0)
22219ea21c83Swebaker 				dr_needed = TRUE;
22229ea21c83Swebaker 
22239ea21c83Swebaker 		delegation_granted = TRUE;
22247c478bd9Sstevel@tonic-gate 
22257c478bd9Sstevel@tonic-gate 		ncg->nfs4_callback_stats.delegations.value.ui64++;
22267c478bd9Sstevel@tonic-gate 		ncg->nfs4_callback_stats.delegaccept_r.value.ui64++;
22277c478bd9Sstevel@tonic-gate 
2228ec38504dSrmesta 	} else if (odt == OPEN_DELEGATE_WRITE) {
22297c478bd9Sstevel@tonic-gate 
22307c478bd9Sstevel@tonic-gate 		rp->r_deleg_type = res->delegation.delegation_type;
22317c478bd9Sstevel@tonic-gate 		owp = &res->delegation.open_delegation4_u.write;
22327c478bd9Sstevel@tonic-gate 		rp->r_deleg_stateid = owp->stateid;
22337c478bd9Sstevel@tonic-gate 		rp->r_deleg_perms = owp->permissions;
22347c478bd9Sstevel@tonic-gate 		rp->r_deleg_limit = owp->space_limit;
22359ea21c83Swebaker 		if (claim == CLAIM_PREVIOUS)
22369ea21c83Swebaker 			if ((recall = owp->recall) != 0)
22379ea21c83Swebaker 				dr_needed = TRUE;
22389ea21c83Swebaker 
22399ea21c83Swebaker 		delegation_granted = TRUE;
22407c478bd9Sstevel@tonic-gate 
22417c478bd9Sstevel@tonic-gate 		if (garp == NULL || !garp->n4g_change_valid) {
22427c478bd9Sstevel@tonic-gate 			valid_garp = FALSE;
22437c478bd9Sstevel@tonic-gate 			rp->r_deleg_change = 0;
22447c478bd9Sstevel@tonic-gate 			rp->r_deleg_change_grant = 0;
22457c478bd9Sstevel@tonic-gate 		} else {
22467c478bd9Sstevel@tonic-gate 			rp->r_deleg_change = garp->n4g_change;
22477c478bd9Sstevel@tonic-gate 			rp->r_deleg_change_grant = garp->n4g_change;
22487c478bd9Sstevel@tonic-gate 		}
22497c478bd9Sstevel@tonic-gate 		mapcnt = rp->r_mapcnt;
22507c478bd9Sstevel@tonic-gate 		rflag = rp->r_flags;
22517c478bd9Sstevel@tonic-gate 
22527c478bd9Sstevel@tonic-gate 		/*
22537c478bd9Sstevel@tonic-gate 		 * Update the delegation change attribute if
22547c478bd9Sstevel@tonic-gate 		 * there are mappers for the file is dirty.  This
22557c478bd9Sstevel@tonic-gate 		 * might be the case during recovery after server
22567c478bd9Sstevel@tonic-gate 		 * reboot.
22577c478bd9Sstevel@tonic-gate 		 */
22587c478bd9Sstevel@tonic-gate 		if (mapcnt > 0 || rflag & R4DIRTY)
22597c478bd9Sstevel@tonic-gate 			rp->r_deleg_change++;
22607c478bd9Sstevel@tonic-gate 
22617c478bd9Sstevel@tonic-gate 		NFS4_DEBUG(nfs4_callback_debug, (CE_NOTE,
22627c478bd9Sstevel@tonic-gate 		    "nfs4_delegation_accept: r_deleg_change: 0x%x\n",
22637c478bd9Sstevel@tonic-gate 		    (int)(rp->r_deleg_change >> 32)));
22647c478bd9Sstevel@tonic-gate 		NFS4_DEBUG(nfs4_callback_debug, (CE_NOTE,
22657c478bd9Sstevel@tonic-gate 		    "nfs4_delegation_accept: r_delg_change_grant: 0x%x\n",
22667c478bd9Sstevel@tonic-gate 		    (int)(rp->r_deleg_change_grant >> 32)));
22677c478bd9Sstevel@tonic-gate 
2268ec38504dSrmesta 
2269ec38504dSrmesta 		ncg->nfs4_callback_stats.delegations.value.ui64++;
2270ec38504dSrmesta 		ncg->nfs4_callback_stats.delegaccept_rw.value.ui64++;
22719ea21c83Swebaker 	} else if (already) {
22727c478bd9Sstevel@tonic-gate 		/*
22739ea21c83Swebaker 		 * No delegation granted.  If the rnode currently has
22749ea21c83Swebaker 		 * has one, then consider it tainted and return it.
22757c478bd9Sstevel@tonic-gate 		 */
22769ea21c83Swebaker 		dr_needed = TRUE;
22779ea21c83Swebaker 	}
22789ea21c83Swebaker 
22799ea21c83Swebaker 	if (delegation_granted) {
22809ea21c83Swebaker 		/* Add the rnode to the list. */
22819ea21c83Swebaker 		if (!already) {
22829ea21c83Swebaker 			crhold(cr);
22839ea21c83Swebaker 			rp->r_deleg_cred = cr;
22849ea21c83Swebaker 
22859ea21c83Swebaker 			ASSERT(mutex_owned(&np->s_lock));
22869ea21c83Swebaker 			list_insert_head(&np->s_deleg_list, rp);
22879ea21c83Swebaker 			/* added list node gets a reference */
22889ea21c83Swebaker 			np->s_refcnt++;
22899ea21c83Swebaker 			nfs4_inc_state_ref_count_nolock(np, mi);
22909ea21c83Swebaker 		}
22919ea21c83Swebaker 		rp->r_deleg_needs_recovery = OPEN_DELEGATE_NONE;
22929ea21c83Swebaker 	}
22939ea21c83Swebaker 
22949ea21c83Swebaker 	/*
22959ea21c83Swebaker 	 * We've now safely accepted the delegation, if any.  Drop the
22969ea21c83Swebaker 	 * locks and figure out what post-processing is needed.  We'd
22979ea21c83Swebaker 	 * like to retain r_statev4_lock, but nfs4_server_rele takes
22989ea21c83Swebaker 	 * s_lock which would be a lock ordering violation.
22999ea21c83Swebaker 	 */
23009ea21c83Swebaker 	mutex_exit(&rp->r_statev4_lock);
23019ea21c83Swebaker 	mutex_exit(&rp->r_statelock);
23029ea21c83Swebaker 	mutex_exit(&np->s_lock);
23039ea21c83Swebaker 	nfs4_server_rele(np);
23049ea21c83Swebaker 
23059ea21c83Swebaker 	/*
23069ea21c83Swebaker 	 * Check to see if we are in recovery.  Remember that
23079ea21c83Swebaker 	 * this function is protected by start_op, so a recovery
23089ea21c83Swebaker 	 * cannot begin until we are out of here.
23099ea21c83Swebaker 	 */
23107c478bd9Sstevel@tonic-gate 	mutex_enter(&mi->mi_lock);
23119ea21c83Swebaker 	recov = mi->mi_recovflags & MI4_RECOV_ACTIV;
23127c478bd9Sstevel@tonic-gate 	mutex_exit(&mi->mi_lock);
23137c478bd9Sstevel@tonic-gate 
23147c478bd9Sstevel@tonic-gate 	mutex_enter(&rp->r_statev4_lock);
23159ea21c83Swebaker 
23169ea21c83Swebaker 	if (nfs4_delegreturn_policy == IMMEDIATE || !valid_garp)
23179ea21c83Swebaker 		dr_needed = TRUE;
23189ea21c83Swebaker 
23199ea21c83Swebaker 	if (dr_needed && rp->r_deleg_return_pending == FALSE) {
23209ea21c83Swebaker 		if (recov) {
23217c478bd9Sstevel@tonic-gate 			/*
23229ea21c83Swebaker 			 * We cannot call delegreturn from inside
23237c478bd9Sstevel@tonic-gate 			 * of recovery or VOP_PUTPAGE will hang
23247c478bd9Sstevel@tonic-gate 			 * due to nfs4_start_fop call in
23257c478bd9Sstevel@tonic-gate 			 * nfs4write.  Use dlistadd to add the
23267c478bd9Sstevel@tonic-gate 			 * rnode to the list of rnodes needing
23279ea21c83Swebaker 			 * cleaning.  We do not need to do reopen
23289ea21c83Swebaker 			 * here because recov_openfiles will do it.
23299ea21c83Swebaker 			 * In the non-recall case, just discard the
23309ea21c83Swebaker 			 * delegation as it is no longer valid.
23317c478bd9Sstevel@tonic-gate 			 */
23329ea21c83Swebaker 			if (recall)
23339ea21c83Swebaker 				dr_flags = NFS4_DR_PUSH;
23349ea21c83Swebaker 			else
23359ea21c83Swebaker 				dr_flags = NFS4_DR_PUSH|NFS4_DR_DISCARD;
23369ea21c83Swebaker 
23379ea21c83Swebaker 			nfs4_dlistadd(rp, ncg, dr_flags);
23389ea21c83Swebaker 			dr_flags = 0;
23397c478bd9Sstevel@tonic-gate 		} else {
23407c478bd9Sstevel@tonic-gate 			/*
23419ea21c83Swebaker 			 * Push the modified data back to the server,
23429ea21c83Swebaker 			 * reopen any delegation open streams, and return
23439ea21c83Swebaker 			 * the delegation.  Drop the statev4_lock first!
23447c478bd9Sstevel@tonic-gate 			 */
23459ea21c83Swebaker 			dr_flags =  NFS4_DR_PUSH|NFS4_DR_DID_OP|NFS4_DR_REOPEN;
23467c478bd9Sstevel@tonic-gate 		}
23477c478bd9Sstevel@tonic-gate 	}
23487c478bd9Sstevel@tonic-gate 	mutex_exit(&rp->r_statev4_lock);
23499ea21c83Swebaker 	if (dr_flags)
23509ea21c83Swebaker 		(void) nfs4delegreturn_impl(rp, dr_flags, ncg);
23517c478bd9Sstevel@tonic-gate }
23527c478bd9Sstevel@tonic-gate 
23537c478bd9Sstevel@tonic-gate /*
23547c478bd9Sstevel@tonic-gate  * nfs4delegabandon - Abandon the delegation on an rnode4.  This code
23557c478bd9Sstevel@tonic-gate  * is called when the client receives EXPIRED, BAD_STATEID, OLD_STATEID
23567c478bd9Sstevel@tonic-gate  * or BADSEQID and the recovery code is unable to recover.  Push any
23577c478bd9Sstevel@tonic-gate  * dirty data back to the server and return the delegation (if any).
23587c478bd9Sstevel@tonic-gate  */
23597c478bd9Sstevel@tonic-gate 
23607c478bd9Sstevel@tonic-gate void
23617c478bd9Sstevel@tonic-gate nfs4delegabandon(rnode4_t *rp)
23627c478bd9Sstevel@tonic-gate {
23637c478bd9Sstevel@tonic-gate 	vnode_t *vp;
23647c478bd9Sstevel@tonic-gate 	struct cb_recall_pass *pp;
23657c478bd9Sstevel@tonic-gate 	open_delegation_type4 dt;
23667c478bd9Sstevel@tonic-gate 
23677c478bd9Sstevel@tonic-gate 	mutex_enter(&rp->r_statev4_lock);
23687c478bd9Sstevel@tonic-gate 	dt = rp->r_deleg_type;
23697c478bd9Sstevel@tonic-gate 	mutex_exit(&rp->r_statev4_lock);
23707c478bd9Sstevel@tonic-gate 
23717c478bd9Sstevel@tonic-gate 	if (dt == OPEN_DELEGATE_NONE)
23727c478bd9Sstevel@tonic-gate 		return;
23737c478bd9Sstevel@tonic-gate 
23747c478bd9Sstevel@tonic-gate 	vp = RTOV4(rp);
23757c478bd9Sstevel@tonic-gate 	VN_HOLD(vp);
23767c478bd9Sstevel@tonic-gate 
23777c478bd9Sstevel@tonic-gate 	pp = kmem_alloc(sizeof (struct cb_recall_pass), KM_SLEEP);
23787c478bd9Sstevel@tonic-gate 	pp->rp = rp;
23797c478bd9Sstevel@tonic-gate 	/*
23807c478bd9Sstevel@tonic-gate 	 * Recovery on the file has failed and we want to return
23817c478bd9Sstevel@tonic-gate 	 * the delegation.  We don't want to reopen files and
23827c478bd9Sstevel@tonic-gate 	 * nfs4delegreturn_thread() figures out what to do about
23837c478bd9Sstevel@tonic-gate 	 * the data.  The only thing to do is attempt to return
23847c478bd9Sstevel@tonic-gate 	 * the delegation.
23857c478bd9Sstevel@tonic-gate 	 */
23867c478bd9Sstevel@tonic-gate 	pp->flags = 0;
23877c478bd9Sstevel@tonic-gate 	pp->truncate = FALSE;
23887c478bd9Sstevel@tonic-gate 
23897c478bd9Sstevel@tonic-gate 	/*
23907c478bd9Sstevel@tonic-gate 	 * Fire up a thread to do the delegreturn; this is
23917c478bd9Sstevel@tonic-gate 	 * necessary because we could be inside a GETPAGE or
23927c478bd9Sstevel@tonic-gate 	 * PUTPAGE and we cannot do another one.
23937c478bd9Sstevel@tonic-gate 	 */
23947c478bd9Sstevel@tonic-gate 
23957c478bd9Sstevel@tonic-gate 	(void) zthread_create(NULL, 0, nfs4delegreturn_thread, pp, 0,
23967c478bd9Sstevel@tonic-gate 	    minclsyspri);
23977c478bd9Sstevel@tonic-gate }
23987c478bd9Sstevel@tonic-gate 
23997c478bd9Sstevel@tonic-gate static int
24007c478bd9Sstevel@tonic-gate wait_for_recall1(vnode_t *vp, nfs4_op_hint_t op, nfs4_recov_state_t *rsp,
24017c478bd9Sstevel@tonic-gate 	int flg)
24027c478bd9Sstevel@tonic-gate {
24037c478bd9Sstevel@tonic-gate 	rnode4_t *rp;
24047c478bd9Sstevel@tonic-gate 	int error = 0;
24057c478bd9Sstevel@tonic-gate 
24067c478bd9Sstevel@tonic-gate #ifdef lint
24077c478bd9Sstevel@tonic-gate 	op = op;
24087c478bd9Sstevel@tonic-gate #endif
24097c478bd9Sstevel@tonic-gate 
24107c478bd9Sstevel@tonic-gate 	if (vp && vp->v_type == VREG) {
24117c478bd9Sstevel@tonic-gate 		rp = VTOR4(vp);
24127c478bd9Sstevel@tonic-gate 
24137c478bd9Sstevel@tonic-gate 		/*
24147c478bd9Sstevel@tonic-gate 		 * Take r_deleg_recall_lock in read mode to synchronize
24157c478bd9Sstevel@tonic-gate 		 * with delegreturn.
24167c478bd9Sstevel@tonic-gate 		 */
24177c478bd9Sstevel@tonic-gate 		error = nfs_rw_enter_sig(&rp->r_deleg_recall_lock,
24187c478bd9Sstevel@tonic-gate 		    RW_READER, INTR4(vp));
24197c478bd9Sstevel@tonic-gate 
24207c478bd9Sstevel@tonic-gate 		if (error == 0)
24217c478bd9Sstevel@tonic-gate 			rsp->rs_flags |= flg;
24227c478bd9Sstevel@tonic-gate 
24237c478bd9Sstevel@tonic-gate 	}
24247c478bd9Sstevel@tonic-gate 	return (error);
24257c478bd9Sstevel@tonic-gate }
24267c478bd9Sstevel@tonic-gate 
24277c478bd9Sstevel@tonic-gate void
24287c478bd9Sstevel@tonic-gate nfs4_end_op_recall(vnode_t *vp1, vnode_t *vp2, nfs4_recov_state_t *rsp)
24297c478bd9Sstevel@tonic-gate {
24307c478bd9Sstevel@tonic-gate 	NFS4_DEBUG(nfs4_recall_debug,
24317c478bd9Sstevel@tonic-gate 	    (CE_NOTE, "nfs4_end_op_recall: 0x%p, 0x%p\n",
24327c478bd9Sstevel@tonic-gate 	    (void *)vp1, (void *)vp2));
24337c478bd9Sstevel@tonic-gate 
24347c478bd9Sstevel@tonic-gate 	if (vp2 && rsp->rs_flags & NFS4_RS_RECALL_HELD2)
24357c478bd9Sstevel@tonic-gate 		nfs_rw_exit(&VTOR4(vp2)->r_deleg_recall_lock);
24367c478bd9Sstevel@tonic-gate 	if (vp1 && rsp->rs_flags & NFS4_RS_RECALL_HELD1)
24377c478bd9Sstevel@tonic-gate 		nfs_rw_exit(&VTOR4(vp1)->r_deleg_recall_lock);
24387c478bd9Sstevel@tonic-gate }
24397c478bd9Sstevel@tonic-gate 
24407c478bd9Sstevel@tonic-gate int
24417c478bd9Sstevel@tonic-gate wait_for_recall(vnode_t *vp1, vnode_t *vp2, nfs4_op_hint_t op,
24427c478bd9Sstevel@tonic-gate 	nfs4_recov_state_t *rsp)
24437c478bd9Sstevel@tonic-gate {
24447c478bd9Sstevel@tonic-gate 	int error;
24457c478bd9Sstevel@tonic-gate 
24467c478bd9Sstevel@tonic-gate 	NFS4_DEBUG(nfs4_recall_debug,
24477c478bd9Sstevel@tonic-gate 	    (CE_NOTE, "wait_for_recall:    0x%p, 0x%p\n",
24487c478bd9Sstevel@tonic-gate 	    (void *)vp1, (void *) vp2));
24497c478bd9Sstevel@tonic-gate 
24507c478bd9Sstevel@tonic-gate 	rsp->rs_flags &= ~(NFS4_RS_RECALL_HELD1|NFS4_RS_RECALL_HELD2);
24517c478bd9Sstevel@tonic-gate 
24527c478bd9Sstevel@tonic-gate 	if ((error = wait_for_recall1(vp1, op, rsp, NFS4_RS_RECALL_HELD1)) != 0)
24537c478bd9Sstevel@tonic-gate 		return (error);
24547c478bd9Sstevel@tonic-gate 
24557c478bd9Sstevel@tonic-gate 	if ((error = wait_for_recall1(vp2, op, rsp, NFS4_RS_RECALL_HELD2))
24567c478bd9Sstevel@tonic-gate 	    != 0) {
24577c478bd9Sstevel@tonic-gate 		if (rsp->rs_flags & NFS4_RS_RECALL_HELD1) {
24587c478bd9Sstevel@tonic-gate 			nfs_rw_exit(&VTOR4(vp1)->r_deleg_recall_lock);
24597c478bd9Sstevel@tonic-gate 			rsp->rs_flags &= ~NFS4_RS_RECALL_HELD1;
24607c478bd9Sstevel@tonic-gate 		}
24617c478bd9Sstevel@tonic-gate 
24627c478bd9Sstevel@tonic-gate 		return (error);
24637c478bd9Sstevel@tonic-gate 	}
24647c478bd9Sstevel@tonic-gate 
24657c478bd9Sstevel@tonic-gate 	return (0);
24667c478bd9Sstevel@tonic-gate }
24677c478bd9Sstevel@tonic-gate 
24687c478bd9Sstevel@tonic-gate /*
24697c478bd9Sstevel@tonic-gate  * nfs4_dlistadd - Add this rnode to a list of rnodes to be
24707c478bd9Sstevel@tonic-gate  * DELEGRETURN'd at the end of recovery.
24717c478bd9Sstevel@tonic-gate  */
24727c478bd9Sstevel@tonic-gate 
24737c478bd9Sstevel@tonic-gate static void
24747c478bd9Sstevel@tonic-gate nfs4_dlistadd(rnode4_t *rp, struct nfs4_callback_globals *ncg, int flags)
24757c478bd9Sstevel@tonic-gate {
24767c478bd9Sstevel@tonic-gate 	struct nfs4_dnode *dp;
24777c478bd9Sstevel@tonic-gate 
24787c478bd9Sstevel@tonic-gate 	ASSERT(mutex_owned(&rp->r_statev4_lock));
24797c478bd9Sstevel@tonic-gate 	/*
24807c478bd9Sstevel@tonic-gate 	 * Mark the delegation as having a return pending.
24817c478bd9Sstevel@tonic-gate 	 * This will prevent the use of the delegation stateID
24827c478bd9Sstevel@tonic-gate 	 * by read, write, setattr and open.
24837c478bd9Sstevel@tonic-gate 	 */
24847c478bd9Sstevel@tonic-gate 	rp->r_deleg_return_pending = TRUE;
24857c478bd9Sstevel@tonic-gate 	dp = kmem_alloc(sizeof (*dp), KM_SLEEP);
24867c478bd9Sstevel@tonic-gate 	VN_HOLD(RTOV4(rp));
24877c478bd9Sstevel@tonic-gate 	dp->rnodep = rp;
24887c478bd9Sstevel@tonic-gate 	dp->flags = flags;
24897c478bd9Sstevel@tonic-gate 	mutex_enter(&ncg->nfs4_dlist_lock);
24907c478bd9Sstevel@tonic-gate 	list_insert_head(&ncg->nfs4_dlist, dp);
24917c478bd9Sstevel@tonic-gate #ifdef	DEBUG
24927c478bd9Sstevel@tonic-gate 	ncg->nfs4_dlistadd_c++;
24937c478bd9Sstevel@tonic-gate #endif
24947c478bd9Sstevel@tonic-gate 	mutex_exit(&ncg->nfs4_dlist_lock);
24957c478bd9Sstevel@tonic-gate }
24967c478bd9Sstevel@tonic-gate 
24977c478bd9Sstevel@tonic-gate /*
24987c478bd9Sstevel@tonic-gate  * nfs4_dlistclean_impl - Do DELEGRETURN for each rnode on the list.
24997c478bd9Sstevel@tonic-gate  * of files awaiting cleaning.  If the override_flags are non-zero
25007c478bd9Sstevel@tonic-gate  * then use them rather than the flags that were set when the rnode
25017c478bd9Sstevel@tonic-gate  * was added to the dlist.
25027c478bd9Sstevel@tonic-gate  */
25037c478bd9Sstevel@tonic-gate static void
25047c478bd9Sstevel@tonic-gate nfs4_dlistclean_impl(struct nfs4_callback_globals *ncg, int override_flags)
25057c478bd9Sstevel@tonic-gate {
25067c478bd9Sstevel@tonic-gate 	rnode4_t *rp;
25077c478bd9Sstevel@tonic-gate 	struct nfs4_dnode *dp;
25087c478bd9Sstevel@tonic-gate 	int flags;
25097c478bd9Sstevel@tonic-gate 
25107c478bd9Sstevel@tonic-gate 	ASSERT(override_flags == 0 || override_flags == NFS4_DR_DISCARD);
25117c478bd9Sstevel@tonic-gate 
25127c478bd9Sstevel@tonic-gate 	mutex_enter(&ncg->nfs4_dlist_lock);
25137c478bd9Sstevel@tonic-gate 	while ((dp = list_head(&ncg->nfs4_dlist)) != NULL) {
25147c478bd9Sstevel@tonic-gate #ifdef	DEBUG
25157c478bd9Sstevel@tonic-gate 		ncg->nfs4_dlistclean_c++;
25167c478bd9Sstevel@tonic-gate #endif
25177c478bd9Sstevel@tonic-gate 		list_remove(&ncg->nfs4_dlist, dp);
25187c478bd9Sstevel@tonic-gate 		mutex_exit(&ncg->nfs4_dlist_lock);
25197c478bd9Sstevel@tonic-gate 		rp = dp->rnodep;
25207c478bd9Sstevel@tonic-gate 		flags = (override_flags != 0) ? override_flags : dp->flags;
25217c478bd9Sstevel@tonic-gate 		kmem_free(dp, sizeof (*dp));
25227c478bd9Sstevel@tonic-gate 		(void) nfs4delegreturn_impl(rp, flags, ncg);
25237c478bd9Sstevel@tonic-gate 		VN_RELE(RTOV4(rp));
25247c478bd9Sstevel@tonic-gate 		mutex_enter(&ncg->nfs4_dlist_lock);
25257c478bd9Sstevel@tonic-gate 	}
25267c478bd9Sstevel@tonic-gate 	mutex_exit(&ncg->nfs4_dlist_lock);
25277c478bd9Sstevel@tonic-gate }
25287c478bd9Sstevel@tonic-gate 
25297c478bd9Sstevel@tonic-gate void
25307c478bd9Sstevel@tonic-gate nfs4_dlistclean(void)
25317c478bd9Sstevel@tonic-gate {
25327c478bd9Sstevel@tonic-gate 	struct nfs4_callback_globals *ncg;
25337c478bd9Sstevel@tonic-gate 
2534108322fbScarlsonj 	ncg = zone_getspecific(nfs4_callback_zone_key, nfs_zone());
25357c478bd9Sstevel@tonic-gate 	ASSERT(ncg != NULL);
25367c478bd9Sstevel@tonic-gate 
25377c478bd9Sstevel@tonic-gate 	nfs4_dlistclean_impl(ncg, 0);
25387c478bd9Sstevel@tonic-gate }
2539*8e46f7b4SArne Jansen 
2540*8e46f7b4SArne Jansen nfs4_rcsync_t *
2541*8e46f7b4SArne Jansen nfs4_recall_sync_start(mntinfo4_t *mi)
2542*8e46f7b4SArne Jansen {
2543*8e46f7b4SArne Jansen 	nfs4_server_t *sp = mi->mi_srv;
2544*8e46f7b4SArne Jansen 	nfs4_rcsync_t *rcp = kmem_zalloc(sizeof (*rcp), KM_SLEEP);
2545*8e46f7b4SArne Jansen 
2546*8e46f7b4SArne Jansen 	if (nfs4_server_vlock(sp, 0) == FALSE) {
2547*8e46f7b4SArne Jansen 		rcp->rs_flags = RS_SERVER_GONE;
2548*8e46f7b4SArne Jansen 		return rcp;
2549*8e46f7b4SArne Jansen 	}
2550*8e46f7b4SArne Jansen 	rcp->rs_mi = mi;
2551*8e46f7b4SArne Jansen 
2552*8e46f7b4SArne Jansen 	mutex_enter(&sp->s_rcsync_lock);
2553*8e46f7b4SArne Jansen 	rcp->rs_seq = sp->s_rcsync_seq++;
2554*8e46f7b4SArne Jansen 	list_insert_tail(&sp->s_rcsync_list, rcp);
2555*8e46f7b4SArne Jansen 	mutex_exit(&sp->s_rcsync_lock);
2556*8e46f7b4SArne Jansen 
2557*8e46f7b4SArne Jansen 	mutex_exit(&sp->s_lock);
2558*8e46f7b4SArne Jansen 	nfs4_server_rele(sp);
2559*8e46f7b4SArne Jansen 
2560*8e46f7b4SArne Jansen 	return rcp;
2561*8e46f7b4SArne Jansen }
2562*8e46f7b4SArne Jansen 
2563*8e46f7b4SArne Jansen void
2564*8e46f7b4SArne Jansen nfs4_recall_sync_end(mntinfo4_t *mi, nfs4_rcsync_t *rcp)
2565*8e46f7b4SArne Jansen {
2566*8e46f7b4SArne Jansen 	nfs4_server_t *sp = mi->mi_srv;
2567*8e46f7b4SArne Jansen 
2568*8e46f7b4SArne Jansen 	if (nfs4_server_vlock(sp, 1) == FALSE) {
2569*8e46f7b4SArne Jansen 		ASSERT((rcp->rs_flags & RS_SERVER_GONE) != 0);
2570*8e46f7b4SArne Jansen 		kmem_free(rcp, sizeof (*rcp));
2571*8e46f7b4SArne Jansen 		return;
2572*8e46f7b4SArne Jansen 	}
2573*8e46f7b4SArne Jansen 
2574*8e46f7b4SArne Jansen 	mutex_enter(&sp->s_rcsync_lock);
2575*8e46f7b4SArne Jansen 	if ((rcp->rs_flags & RS_SERVER_GONE) == 0) {
2576*8e46f7b4SArne Jansen 		list_remove(&sp->s_rcsync_list, rcp);
2577*8e46f7b4SArne Jansen 		cv_broadcast(&sp->s_rcsync_cv);
2578*8e46f7b4SArne Jansen 	}
2579*8e46f7b4SArne Jansen 	mutex_exit(&sp->s_rcsync_lock);
2580*8e46f7b4SArne Jansen 	mutex_exit(&sp->s_lock);
2581*8e46f7b4SArne Jansen 	nfs4_server_rele(sp);
2582*8e46f7b4SArne Jansen 	kmem_free(rcp, sizeof (*rcp));
2583*8e46f7b4SArne Jansen }
2584*8e46f7b4SArne Jansen 
2585*8e46f7b4SArne Jansen static void
2586*8e46f7b4SArne Jansen nfs4_recall_sync_wait(nfs4_server_t *sp)
2587*8e46f7b4SArne Jansen {
2588*8e46f7b4SArne Jansen 	uint64_t seq;
2589*8e46f7b4SArne Jansen 
2590*8e46f7b4SArne Jansen 	mutex_enter(&sp->s_rcsync_lock);
2591*8e46f7b4SArne Jansen 	seq = sp->s_rcsync_seq;
2592*8e46f7b4SArne Jansen 
2593*8e46f7b4SArne Jansen 	while (!list_is_empty(&sp->s_rcsync_list)) {
2594*8e46f7b4SArne Jansen 		nfs4_rcsync_t *rcp = list_head(&sp->s_rcsync_list);
2595*8e46f7b4SArne Jansen 
2596*8e46f7b4SArne Jansen 		if (rcp->rs_seq >= seq)
2597*8e46f7b4SArne Jansen 			break;
2598*8e46f7b4SArne Jansen 
2599*8e46f7b4SArne Jansen 		cv_wait(&sp->s_rcsync_cv, &sp->s_rcsync_lock);
2600*8e46f7b4SArne Jansen 	}
2601*8e46f7b4SArne Jansen 
2602*8e46f7b4SArne Jansen 	mutex_exit(&sp->s_rcsync_lock);
2603*8e46f7b4SArne Jansen }
2604