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