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
5f86c6ccaSdm  * Common Development and Distribution License (the "License").
6f86c6ccaSdm  * 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,
172a17ce845SMarcel Telka     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,
385a17ce845SMarcel Telka     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,
4849ea21c83Swebaker 	    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,
539a17ce845SMarcel Telka     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,
550a17ce845SMarcel Telka     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 	/*
563*0dfe541eSEvan Layton 	 * Form a reply tag by copying over the request tag.
5647c478bd9Sstevel@tonic-gate 	 */
5657c478bd9Sstevel@tonic-gate 	resp->tag.utf8string_len = args->tag.utf8string_len;
566*0dfe541eSEvan Layton 	if (args->tag.utf8string_len != 0) {
567*0dfe541eSEvan Layton 		resp->tag.utf8string_val =
568*0dfe541eSEvan Layton 		    kmem_alloc(resp->tag.utf8string_len, KM_SLEEP);
569*0dfe541eSEvan Layton 		bcopy(args->tag.utf8string_val, resp->tag.utf8string_val,
570*0dfe541eSEvan Layton 		    args->tag.utf8string_len);
571*0dfe541eSEvan Layton 	} else {
572*0dfe541eSEvan Layton 		resp->tag.utf8string_val = NULL;
573*0dfe541eSEvan Layton 	}
5747c478bd9Sstevel@tonic-gate 
5757c478bd9Sstevel@tonic-gate 	/*
5767c478bd9Sstevel@tonic-gate 	 * XXX for now, minorversion should be zero
5777c478bd9Sstevel@tonic-gate 	 */
5787c478bd9Sstevel@tonic-gate 	if (args->minorversion != CB4_MINORVERSION) {
5797c478bd9Sstevel@tonic-gate 		resp->array_len = 0;
5807c478bd9Sstevel@tonic-gate 		resp->array = NULL;
5817c478bd9Sstevel@tonic-gate 		resp->status = NFS4ERR_MINOR_VERS_MISMATCH;
5827c478bd9Sstevel@tonic-gate 		return;
5837c478bd9Sstevel@tonic-gate 	}
5847c478bd9Sstevel@tonic-gate 
5857c478bd9Sstevel@tonic-gate #ifdef DEBUG
5867c478bd9Sstevel@tonic-gate 	/*
5877c478bd9Sstevel@tonic-gate 	 * Verify callback_ident.  It doesn't really matter if it's wrong
5887c478bd9Sstevel@tonic-gate 	 * because we don't really use callback_ident -- we use prog number
5897c478bd9Sstevel@tonic-gate 	 * of the RPC request instead.  In this case, just print a DEBUG
5907c478bd9Sstevel@tonic-gate 	 * console message to reveal brokenness of cbclient (at bkoff/cthon).
5917c478bd9Sstevel@tonic-gate 	 */
5927c478bd9Sstevel@tonic-gate 	if (args->callback_ident != req->rq_prog)
5937c478bd9Sstevel@tonic-gate 		zcmn_err(getzoneid(), CE_WARN,
5947c478bd9Sstevel@tonic-gate 		    "cb_compound: cb_client using wrong "
5957c478bd9Sstevel@tonic-gate 		    "callback_ident(%d), should be %d",
5967c478bd9Sstevel@tonic-gate 		    args->callback_ident, req->rq_prog);
5977c478bd9Sstevel@tonic-gate #endif
5987c478bd9Sstevel@tonic-gate 
5997c478bd9Sstevel@tonic-gate 	resp->array_len = args->array_len;
6007c478bd9Sstevel@tonic-gate 	resp->array = kmem_zalloc(args->array_len * sizeof (nfs_cb_resop4),
6019ea21c83Swebaker 	    KM_SLEEP);
6027c478bd9Sstevel@tonic-gate 
6037c478bd9Sstevel@tonic-gate 	for (i = 0; i < args->array_len && cs.cont; i++) {
6047c478bd9Sstevel@tonic-gate 
6057c478bd9Sstevel@tonic-gate 		argop = &args->array[i];
6067c478bd9Sstevel@tonic-gate 		resop = &resp->array[i];
6077c478bd9Sstevel@tonic-gate 		resop->resop = argop->argop;
6087c478bd9Sstevel@tonic-gate 		op = (uint_t)resop->resop;
6097c478bd9Sstevel@tonic-gate 
6107c478bd9Sstevel@tonic-gate 		switch (op) {
6117c478bd9Sstevel@tonic-gate 
6127c478bd9Sstevel@tonic-gate 		case OP_CB_GETATTR:
6137c478bd9Sstevel@tonic-gate 
6147c478bd9Sstevel@tonic-gate 			cb_getattr(argop, resop, req, &cs, ncg);
6157c478bd9Sstevel@tonic-gate 			break;
6167c478bd9Sstevel@tonic-gate 
6177c478bd9Sstevel@tonic-gate 		case OP_CB_RECALL:
6187c478bd9Sstevel@tonic-gate 
6197c478bd9Sstevel@tonic-gate 			cb_recall(argop, resop, req, &cs, ncg);
6207c478bd9Sstevel@tonic-gate 			break;
6217c478bd9Sstevel@tonic-gate 
6227c478bd9Sstevel@tonic-gate 		case OP_CB_ILLEGAL:
6237c478bd9Sstevel@tonic-gate 
6247c478bd9Sstevel@tonic-gate 			/* fall through */
6257c478bd9Sstevel@tonic-gate 
6267c478bd9Sstevel@tonic-gate 		default:
6277c478bd9Sstevel@tonic-gate 			/*
6287c478bd9Sstevel@tonic-gate 			 * Handle OP_CB_ILLEGAL and any undefined opcode.
6297c478bd9Sstevel@tonic-gate 			 * Currently, the XDR code will return BADXDR
6307c478bd9Sstevel@tonic-gate 			 * if cb op doesn't decode to legal value, so
6317c478bd9Sstevel@tonic-gate 			 * it really only handles OP_CB_ILLEGAL.
6327c478bd9Sstevel@tonic-gate 			 */
6337c478bd9Sstevel@tonic-gate 			op = OP_CB_ILLEGAL;
6347c478bd9Sstevel@tonic-gate 			cb_illegal(argop, resop, req, &cs, ncg);
6357c478bd9Sstevel@tonic-gate 		}
6367c478bd9Sstevel@tonic-gate 
6377c478bd9Sstevel@tonic-gate 		if (*cs.statusp != NFS4_OK)
6387c478bd9Sstevel@tonic-gate 			cs.cont = FALSE;
6397c478bd9Sstevel@tonic-gate 
6407c478bd9Sstevel@tonic-gate 		/*
6417c478bd9Sstevel@tonic-gate 		 * If not at last op, and if we are to stop, then
6427c478bd9Sstevel@tonic-gate 		 * compact the results array.
6437c478bd9Sstevel@tonic-gate 		 */
6447c478bd9Sstevel@tonic-gate 		if ((i + 1) < args->array_len && !cs.cont) {
6457c478bd9Sstevel@tonic-gate 
6467c478bd9Sstevel@tonic-gate 			new_res = kmem_alloc(
6479ea21c83Swebaker 			    (i+1) * sizeof (nfs_cb_resop4), KM_SLEEP);
6487c478bd9Sstevel@tonic-gate 			bcopy(resp->array,
6499ea21c83Swebaker 			    new_res, (i+1) * sizeof (nfs_cb_resop4));
6507c478bd9Sstevel@tonic-gate 			kmem_free(resp->array,
6519ea21c83Swebaker 			    args->array_len * sizeof (nfs_cb_resop4));
6527c478bd9Sstevel@tonic-gate 
6537c478bd9Sstevel@tonic-gate 			resp->array_len =  i + 1;
6547c478bd9Sstevel@tonic-gate 			resp->array = new_res;
6557c478bd9Sstevel@tonic-gate 		}
6567c478bd9Sstevel@tonic-gate 	}
6577c478bd9Sstevel@tonic-gate 
6587c478bd9Sstevel@tonic-gate }
6597c478bd9Sstevel@tonic-gate 
6607c478bd9Sstevel@tonic-gate static void
cb_compound_free(CB_COMPOUND4res * resp)6617c478bd9Sstevel@tonic-gate cb_compound_free(CB_COMPOUND4res *resp)
6627c478bd9Sstevel@tonic-gate {
6637c478bd9Sstevel@tonic-gate 	uint_t i, op;
6647c478bd9Sstevel@tonic-gate 	nfs_cb_resop4 *resop;
6657c478bd9Sstevel@tonic-gate 
6667c478bd9Sstevel@tonic-gate 	if (resp->tag.utf8string_val) {
6677c478bd9Sstevel@tonic-gate 		UTF8STRING_FREE(resp->tag)
6687c478bd9Sstevel@tonic-gate 	}
6697c478bd9Sstevel@tonic-gate 
6707c478bd9Sstevel@tonic-gate 	for (i = 0; i < resp->array_len; i++) {
6717c478bd9Sstevel@tonic-gate 
6727c478bd9Sstevel@tonic-gate 		resop = &resp->array[i];
6737c478bd9Sstevel@tonic-gate 		op = (uint_t)resop->resop;
6747c478bd9Sstevel@tonic-gate 
6757c478bd9Sstevel@tonic-gate 		switch (op) {
6767c478bd9Sstevel@tonic-gate 
6777c478bd9Sstevel@tonic-gate 		case OP_CB_GETATTR:
6787c478bd9Sstevel@tonic-gate 
6797c478bd9Sstevel@tonic-gate 			cb_getattr_free(resop);
6807c478bd9Sstevel@tonic-gate 			break;
6817c478bd9Sstevel@tonic-gate 
6827c478bd9Sstevel@tonic-gate 		case OP_CB_RECALL:
6837c478bd9Sstevel@tonic-gate 
6847c478bd9Sstevel@tonic-gate 			cb_recall_free(resop);
6857c478bd9Sstevel@tonic-gate 			break;
6867c478bd9Sstevel@tonic-gate 
6877c478bd9Sstevel@tonic-gate 		default:
6887c478bd9Sstevel@tonic-gate 			break;
6897c478bd9Sstevel@tonic-gate 		}
6907c478bd9Sstevel@tonic-gate 	}
6917c478bd9Sstevel@tonic-gate 
6927c478bd9Sstevel@tonic-gate 	if (resp->array != NULL) {
6937c478bd9Sstevel@tonic-gate 		kmem_free(resp->array,
6949ea21c83Swebaker 		    resp->array_len * sizeof (nfs_cb_resop4));
6957c478bd9Sstevel@tonic-gate 	}
6967c478bd9Sstevel@tonic-gate }
6977c478bd9Sstevel@tonic-gate 
6987c478bd9Sstevel@tonic-gate static void
cb_dispatch(struct svc_req * req,SVCXPRT * xprt)6997c478bd9Sstevel@tonic-gate cb_dispatch(struct svc_req *req, SVCXPRT *xprt)
7007c478bd9Sstevel@tonic-gate {
7017c478bd9Sstevel@tonic-gate 	CB_COMPOUND4args args;
7027c478bd9Sstevel@tonic-gate 	CB_COMPOUND4res res;
7037c478bd9Sstevel@tonic-gate 	struct nfs4_callback_globals *ncg;
7047c478bd9Sstevel@tonic-gate 
7057c478bd9Sstevel@tonic-gate 	bool_t (*xdr_args)(), (*xdr_res)();
7067c478bd9Sstevel@tonic-gate 	void (*proc)(CB_COMPOUND4args *, CB_COMPOUND4res *, struct svc_req *,
7077c478bd9Sstevel@tonic-gate 	    struct nfs4_callback_globals *);
7087c478bd9Sstevel@tonic-gate 	void (*freeproc)(CB_COMPOUND4res *);
7097c478bd9Sstevel@tonic-gate 
710108322fbScarlsonj 	ncg = zone_getspecific(nfs4_callback_zone_key, nfs_zone());
7117c478bd9Sstevel@tonic-gate 	ASSERT(ncg != NULL);
7127c478bd9Sstevel@tonic-gate 
7137c478bd9Sstevel@tonic-gate 	ncg->nfs4_callback_stats.cb_dispatch.value.ui64++;
7147c478bd9Sstevel@tonic-gate 
7157c478bd9Sstevel@tonic-gate 	switch (req->rq_proc) {
7167c478bd9Sstevel@tonic-gate 	case CB_NULL:
7177c478bd9Sstevel@tonic-gate 		xdr_args = xdr_void;
7187c478bd9Sstevel@tonic-gate 		xdr_res = xdr_void;
7197c478bd9Sstevel@tonic-gate 		proc = cb_null;
7207c478bd9Sstevel@tonic-gate 		freeproc = NULL;
7217c478bd9Sstevel@tonic-gate 		break;
7227c478bd9Sstevel@tonic-gate 
7237c478bd9Sstevel@tonic-gate 	case CB_COMPOUND:
724eac3aab7Srobinson 		xdr_args = xdr_CB_COMPOUND4args_clnt;
7257c478bd9Sstevel@tonic-gate 		xdr_res = xdr_CB_COMPOUND4res;
7267c478bd9Sstevel@tonic-gate 		proc = cb_compound;
7277c478bd9Sstevel@tonic-gate 		freeproc = cb_compound_free;
7287c478bd9Sstevel@tonic-gate 		break;
7297c478bd9Sstevel@tonic-gate 
7307c478bd9Sstevel@tonic-gate 	default:
7317c478bd9Sstevel@tonic-gate 		CB_WARN("cb_dispatch: no proc\n");
7327c478bd9Sstevel@tonic-gate 		svcerr_noproc(xprt);
7337c478bd9Sstevel@tonic-gate 		return;
7347c478bd9Sstevel@tonic-gate 	}
7357c478bd9Sstevel@tonic-gate 
7367c478bd9Sstevel@tonic-gate 	args.tag.utf8string_val = NULL;
7377c478bd9Sstevel@tonic-gate 	args.array = NULL;
7387c478bd9Sstevel@tonic-gate 
7397c478bd9Sstevel@tonic-gate 	if (!SVC_GETARGS(xprt, xdr_args, (caddr_t)&args)) {
7407c478bd9Sstevel@tonic-gate 
7417c478bd9Sstevel@tonic-gate 		CB_WARN("cb_dispatch: cannot getargs\n");
7427c478bd9Sstevel@tonic-gate 		svcerr_decode(xprt);
7437c478bd9Sstevel@tonic-gate 		return;
7447c478bd9Sstevel@tonic-gate 	}
7457c478bd9Sstevel@tonic-gate 
7467c478bd9Sstevel@tonic-gate 	(*proc)(&args, &res, req, ncg);
7477c478bd9Sstevel@tonic-gate 
7487c478bd9Sstevel@tonic-gate 	if (svc_sendreply(xprt, xdr_res, (caddr_t)&res) == FALSE) {
7497c478bd9Sstevel@tonic-gate 
7507c478bd9Sstevel@tonic-gate 		CB_WARN("cb_dispatch: bad sendreply\n");
75171c6e709Srmesta 		svcerr_systemerr(xprt);
7527c478bd9Sstevel@tonic-gate 	}
7537c478bd9Sstevel@tonic-gate 
7547c478bd9Sstevel@tonic-gate 	if (freeproc)
7557c478bd9Sstevel@tonic-gate 		(*freeproc)(&res);
7567c478bd9Sstevel@tonic-gate 
7577c478bd9Sstevel@tonic-gate 	if (!SVC_FREEARGS(xprt, xdr_args, (caddr_t)&args)) {
7587c478bd9Sstevel@tonic-gate 
7597c478bd9Sstevel@tonic-gate 		CB_WARN("cb_dispatch: bad freeargs\n");
7607c478bd9Sstevel@tonic-gate 	}
7617c478bd9Sstevel@tonic-gate }
7627c478bd9Sstevel@tonic-gate 
7637c478bd9Sstevel@tonic-gate static rpcprog_t
nfs4_getnextprogram(struct nfs4_callback_globals * ncg)7647c478bd9Sstevel@tonic-gate nfs4_getnextprogram(struct nfs4_callback_globals *ncg)
7657c478bd9Sstevel@tonic-gate {
7667c478bd9Sstevel@tonic-gate 	int i, j;
7677c478bd9Sstevel@tonic-gate 
7687c478bd9Sstevel@tonic-gate 	j = ncg->nfs4_program_hint;
7697c478bd9Sstevel@tonic-gate 	for (i = 0; i < nfs4_num_prognums; i++, j++) {
7707c478bd9Sstevel@tonic-gate 
7717c478bd9Sstevel@tonic-gate 		if (j >= nfs4_num_prognums)
7727c478bd9Sstevel@tonic-gate 			j = 0;
7737c478bd9Sstevel@tonic-gate 
7747c478bd9Sstevel@tonic-gate 		if (ncg->nfs4prog2server[j] == NULL) {
7757c478bd9Sstevel@tonic-gate 			ncg->nfs4_program_hint = j+1;
7767c478bd9Sstevel@tonic-gate 			return (j+NFS4_CALLBACK);
7777c478bd9Sstevel@tonic-gate 		}
7787c478bd9Sstevel@tonic-gate 	}
7797c478bd9Sstevel@tonic-gate 
7807c478bd9Sstevel@tonic-gate 	return (0);
7817c478bd9Sstevel@tonic-gate }
7827c478bd9Sstevel@tonic-gate 
7837c478bd9Sstevel@tonic-gate void
nfs4callback_destroy(nfs4_server_t * np)7847c478bd9Sstevel@tonic-gate nfs4callback_destroy(nfs4_server_t *np)
7857c478bd9Sstevel@tonic-gate {
7867c478bd9Sstevel@tonic-gate 	struct nfs4_callback_globals *ncg;
7877c478bd9Sstevel@tonic-gate 	int i;
7887c478bd9Sstevel@tonic-gate 
7897c478bd9Sstevel@tonic-gate 	if (np->s_program == 0)
7907c478bd9Sstevel@tonic-gate 		return;
7917c478bd9Sstevel@tonic-gate 
7927c478bd9Sstevel@tonic-gate 	ncg = np->zone_globals;
7937c478bd9Sstevel@tonic-gate 	i = np->s_program - NFS4_CALLBACK;
7947c478bd9Sstevel@tonic-gate 
7957c478bd9Sstevel@tonic-gate 	mutex_enter(&ncg->nfs4_cb_lock);
7967c478bd9Sstevel@tonic-gate 
7977c478bd9Sstevel@tonic-gate 	ASSERT(ncg->nfs4prog2server[i] == np);
7987c478bd9Sstevel@tonic-gate 
7997c478bd9Sstevel@tonic-gate 	ncg->nfs4prog2server[i] = NULL;
8007c478bd9Sstevel@tonic-gate 
8017c478bd9Sstevel@tonic-gate 	if (i < ncg->nfs4_program_hint)
8027c478bd9Sstevel@tonic-gate 		ncg->nfs4_program_hint = i;
8037c478bd9Sstevel@tonic-gate 
8047c478bd9Sstevel@tonic-gate 	mutex_exit(&ncg->nfs4_cb_lock);
8057c478bd9Sstevel@tonic-gate }
8067c478bd9Sstevel@tonic-gate 
8077c478bd9Sstevel@tonic-gate /*
8087c478bd9Sstevel@tonic-gate  * nfs4_setport - This function saves a netid and univeral address for
8097c478bd9Sstevel@tonic-gate  * the callback program.  These values will be used during setclientid.
8107c478bd9Sstevel@tonic-gate  */
8117c478bd9Sstevel@tonic-gate static void
nfs4_setport(char * netid,char * uaddr,char * protofmly,char * proto,struct nfs4_callback_globals * ncg)8127c478bd9Sstevel@tonic-gate nfs4_setport(char *netid, char *uaddr, char *protofmly, char *proto,
813a17ce845SMarcel Telka     struct nfs4_callback_globals *ncg)
8147c478bd9Sstevel@tonic-gate {
8157c478bd9Sstevel@tonic-gate 	struct nfs4_cb_port *p;
8167c478bd9Sstevel@tonic-gate 	bool_t found = FALSE;
8177c478bd9Sstevel@tonic-gate 
8187c478bd9Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&ncg->nfs4_cb_lock));
8197c478bd9Sstevel@tonic-gate 
8207c478bd9Sstevel@tonic-gate 	p = list_head(&ncg->nfs4_cb_ports);
8217c478bd9Sstevel@tonic-gate 	for (; p != NULL; p = list_next(&ncg->nfs4_cb_ports, p)) {
8227c478bd9Sstevel@tonic-gate 		if (strcmp(p->netid, netid) == 0) {
8237c478bd9Sstevel@tonic-gate 			found = TRUE;
8247c478bd9Sstevel@tonic-gate 			break;
8257c478bd9Sstevel@tonic-gate 		}
8267c478bd9Sstevel@tonic-gate 	}
8277c478bd9Sstevel@tonic-gate 	if (found == TRUE)
8287c478bd9Sstevel@tonic-gate 		(void) strcpy(p->uaddr, uaddr);
8297c478bd9Sstevel@tonic-gate 	else {
8307c478bd9Sstevel@tonic-gate 		p = kmem_alloc(sizeof (*p), KM_SLEEP);
8317c478bd9Sstevel@tonic-gate 
8327c478bd9Sstevel@tonic-gate 		(void) strcpy(p->uaddr, uaddr);
8337c478bd9Sstevel@tonic-gate 		(void) strcpy(p->netid, netid);
8347c478bd9Sstevel@tonic-gate 		(void) strcpy(p->protofmly, protofmly);
8357c478bd9Sstevel@tonic-gate 		(void) strcpy(p->proto, proto);
8367c478bd9Sstevel@tonic-gate 		list_insert_head(&ncg->nfs4_cb_ports, p);
8377c478bd9Sstevel@tonic-gate 	}
8387c478bd9Sstevel@tonic-gate }
8397c478bd9Sstevel@tonic-gate 
8407c478bd9Sstevel@tonic-gate /*
8417c478bd9Sstevel@tonic-gate  * nfs4_cb_args - This function is used to construct the callback
8427c478bd9Sstevel@tonic-gate  * portion of the arguments needed for setclientid.
8437c478bd9Sstevel@tonic-gate  */
8447c478bd9Sstevel@tonic-gate 
8457c478bd9Sstevel@tonic-gate void
nfs4_cb_args(nfs4_server_t * np,struct knetconfig * knc,SETCLIENTID4args * args)8467c478bd9Sstevel@tonic-gate nfs4_cb_args(nfs4_server_t *np, struct knetconfig *knc, SETCLIENTID4args *args)
8477c478bd9Sstevel@tonic-gate {
8487c478bd9Sstevel@tonic-gate 	struct nfs4_cb_port *p;
8497c478bd9Sstevel@tonic-gate 	bool_t found = FALSE;
8507c478bd9Sstevel@tonic-gate 	rpcprog_t pgm;
8517c478bd9Sstevel@tonic-gate 	struct nfs4_callback_globals *ncg = np->zone_globals;
8527c478bd9Sstevel@tonic-gate 
8537c478bd9Sstevel@tonic-gate 	/*
8547c478bd9Sstevel@tonic-gate 	 * This server structure may already have a program number
8557c478bd9Sstevel@tonic-gate 	 * assigned to it.  This happens when the client has to
8567c478bd9Sstevel@tonic-gate 	 * re-issue SETCLIENTID.  Just re-use the information.
8577c478bd9Sstevel@tonic-gate 	 */
8587c478bd9Sstevel@tonic-gate 	if (np->s_program >= NFS4_CALLBACK &&
8597c478bd9Sstevel@tonic-gate 	    np->s_program < NFS4_CALLBACK + nfs4_num_prognums)
8607c478bd9Sstevel@tonic-gate 		nfs4callback_destroy(np);
8617c478bd9Sstevel@tonic-gate 
8627c478bd9Sstevel@tonic-gate 	mutex_enter(&ncg->nfs4_cb_lock);
8637c478bd9Sstevel@tonic-gate 
8647c478bd9Sstevel@tonic-gate 	p = list_head(&ncg->nfs4_cb_ports);
8657c478bd9Sstevel@tonic-gate 	for (; p != NULL; p = list_next(&ncg->nfs4_cb_ports, p)) {
8667c478bd9Sstevel@tonic-gate 		if (strcmp(p->protofmly, knc->knc_protofmly) == 0 &&
8677c478bd9Sstevel@tonic-gate 		    strcmp(p->proto, knc->knc_proto) == 0) {
8687c478bd9Sstevel@tonic-gate 			found = TRUE;
8697c478bd9Sstevel@tonic-gate 			break;
8707c478bd9Sstevel@tonic-gate 		}
8717c478bd9Sstevel@tonic-gate 	}
8727c478bd9Sstevel@tonic-gate 
8737c478bd9Sstevel@tonic-gate 	if (found == FALSE) {
8747c478bd9Sstevel@tonic-gate 
8757c478bd9Sstevel@tonic-gate 		NFS4_DEBUG(nfs4_callback_debug,
8769ea21c83Swebaker 		    (CE_WARN, "nfs4_cb_args: could not find netid for %s/%s\n",
8779ea21c83Swebaker 		    knc->knc_protofmly, knc->knc_proto));
8787c478bd9Sstevel@tonic-gate 
8797c478bd9Sstevel@tonic-gate 		args->callback.cb_program = 0;
8807c478bd9Sstevel@tonic-gate 		args->callback.cb_location.r_netid = NULL;
8817c478bd9Sstevel@tonic-gate 		args->callback.cb_location.r_addr = NULL;
8827c478bd9Sstevel@tonic-gate 		args->callback_ident = 0;
8837c478bd9Sstevel@tonic-gate 		mutex_exit(&ncg->nfs4_cb_lock);
8847c478bd9Sstevel@tonic-gate 		return;
8857c478bd9Sstevel@tonic-gate 	}
8867c478bd9Sstevel@tonic-gate 
8877c478bd9Sstevel@tonic-gate 	if ((pgm = nfs4_getnextprogram(ncg)) == 0) {
8887c478bd9Sstevel@tonic-gate 		CB_WARN("nfs4_cb_args: out of program numbers\n");
8897c478bd9Sstevel@tonic-gate 
8907c478bd9Sstevel@tonic-gate 		args->callback.cb_program = 0;
8917c478bd9Sstevel@tonic-gate 		args->callback.cb_location.r_netid = NULL;
8927c478bd9Sstevel@tonic-gate 		args->callback.cb_location.r_addr = NULL;
8937c478bd9Sstevel@tonic-gate 		args->callback_ident = 0;
8947c478bd9Sstevel@tonic-gate 		mutex_exit(&ncg->nfs4_cb_lock);
8957c478bd9Sstevel@tonic-gate 		return;
8967c478bd9Sstevel@tonic-gate 	}
8977c478bd9Sstevel@tonic-gate 
8987c478bd9Sstevel@tonic-gate 	ncg->nfs4prog2server[pgm-NFS4_CALLBACK] = np;
8997c478bd9Sstevel@tonic-gate 	args->callback.cb_program = pgm;
9007c478bd9Sstevel@tonic-gate 	args->callback.cb_location.r_netid = p->netid;
9017c478bd9Sstevel@tonic-gate 	args->callback.cb_location.r_addr = p->uaddr;
9027c478bd9Sstevel@tonic-gate 	args->callback_ident = pgm;
9037c478bd9Sstevel@tonic-gate 
9047c478bd9Sstevel@tonic-gate 	np->s_program = pgm;
9057c478bd9Sstevel@tonic-gate 
9067c478bd9Sstevel@tonic-gate 	mutex_exit(&ncg->nfs4_cb_lock);
9077c478bd9Sstevel@tonic-gate }
9087c478bd9Sstevel@tonic-gate 
9097c478bd9Sstevel@tonic-gate static int
nfs4_dquery(struct nfs4_svc_args * arg,model_t model)9107c478bd9Sstevel@tonic-gate nfs4_dquery(struct nfs4_svc_args *arg, model_t model)
9117c478bd9Sstevel@tonic-gate {
9127c478bd9Sstevel@tonic-gate 	file_t *fp;
9137c478bd9Sstevel@tonic-gate 	vnode_t *vp;
9147c478bd9Sstevel@tonic-gate 	rnode4_t *rp;
9157c478bd9Sstevel@tonic-gate 	int error;
9167c478bd9Sstevel@tonic-gate 	STRUCT_HANDLE(nfs4_svc_args, uap);
9177c478bd9Sstevel@tonic-gate 
9187c478bd9Sstevel@tonic-gate 	STRUCT_SET_HANDLE(uap, model, arg);
9197c478bd9Sstevel@tonic-gate 
9207c478bd9Sstevel@tonic-gate 	if ((fp = getf(STRUCT_FGET(uap, fd))) == NULL)
9217c478bd9Sstevel@tonic-gate 		return (EBADF);
9227c478bd9Sstevel@tonic-gate 
9237c478bd9Sstevel@tonic-gate 	vp = fp->f_vnode;
9247c478bd9Sstevel@tonic-gate 
9257c478bd9Sstevel@tonic-gate 	if (vp == NULL || vp->v_type != VREG ||
9267c478bd9Sstevel@tonic-gate 	    !vn_matchops(vp, nfs4_vnodeops)) {
9277c478bd9Sstevel@tonic-gate 		releasef(STRUCT_FGET(uap, fd));
9287c478bd9Sstevel@tonic-gate 		return (EBADF);
9297c478bd9Sstevel@tonic-gate 	}
9307c478bd9Sstevel@tonic-gate 
9317c478bd9Sstevel@tonic-gate 	rp = VTOR4(vp);
9327c478bd9Sstevel@tonic-gate 
9337c478bd9Sstevel@tonic-gate 	/*
9347c478bd9Sstevel@tonic-gate 	 * I can't convince myself that we need locking here.  The
9357c478bd9Sstevel@tonic-gate 	 * rnode cannot disappear and the value returned is instantly
9367c478bd9Sstevel@tonic-gate 	 * stale anway, so why bother?
9377c478bd9Sstevel@tonic-gate 	 */
9387c478bd9Sstevel@tonic-gate 
9397c478bd9Sstevel@tonic-gate 	error = suword32(STRUCT_FGETP(uap, netid), rp->r_deleg_type);
9407c478bd9Sstevel@tonic-gate 	releasef(STRUCT_FGET(uap, fd));
9417c478bd9Sstevel@tonic-gate 	return (error);
9427c478bd9Sstevel@tonic-gate }
9437c478bd9Sstevel@tonic-gate 
9447c478bd9Sstevel@tonic-gate 
9457c478bd9Sstevel@tonic-gate /*
9467c478bd9Sstevel@tonic-gate  * NFS4 client system call.  This service does the
9477c478bd9Sstevel@tonic-gate  * necessary initialization for the callback program.
9487c478bd9Sstevel@tonic-gate  * This is fashioned after the server side interaction
9497c478bd9Sstevel@tonic-gate  * between nfsd and the kernel.  On the client, the
9507c478bd9Sstevel@tonic-gate  * mount command forks and the child process does the
9517c478bd9Sstevel@tonic-gate  * necessary interaction with the kernel.
9527c478bd9Sstevel@tonic-gate  *
9537c478bd9Sstevel@tonic-gate  * uap->fd is the fd of an open transport provider
9547c478bd9Sstevel@tonic-gate  */
9557c478bd9Sstevel@tonic-gate int
nfs4_svc(struct nfs4_svc_args * arg,model_t model)9567c478bd9Sstevel@tonic-gate nfs4_svc(struct nfs4_svc_args *arg, model_t model)
9577c478bd9Sstevel@tonic-gate {
9587c478bd9Sstevel@tonic-gate 	file_t *fp;
9597c478bd9Sstevel@tonic-gate 	int error;
9607c478bd9Sstevel@tonic-gate 	int readsize;
9617c478bd9Sstevel@tonic-gate 	char buf[KNC_STRSIZE], uaddr[KNC_STRSIZE];
9627c478bd9Sstevel@tonic-gate 	char protofmly[KNC_STRSIZE], proto[KNC_STRSIZE];
9637c478bd9Sstevel@tonic-gate 	size_t len;
9647c478bd9Sstevel@tonic-gate 	STRUCT_HANDLE(nfs4_svc_args, uap);
9657c478bd9Sstevel@tonic-gate 	struct netbuf addrmask;
9667c478bd9Sstevel@tonic-gate 	int cmd;
9677c478bd9Sstevel@tonic-gate 	SVCMASTERXPRT *cb_xprt;
9687c478bd9Sstevel@tonic-gate 	struct nfs4_callback_globals *ncg;
9697c478bd9Sstevel@tonic-gate 
9707c478bd9Sstevel@tonic-gate #ifdef lint
9717c478bd9Sstevel@tonic-gate 	model = model;		/* STRUCT macros don't always refer to it */
9727c478bd9Sstevel@tonic-gate #endif
9737c478bd9Sstevel@tonic-gate 
9747c478bd9Sstevel@tonic-gate 	STRUCT_SET_HANDLE(uap, model, arg);
9757c478bd9Sstevel@tonic-gate 
9767c478bd9Sstevel@tonic-gate 	if (STRUCT_FGET(uap, cmd) == NFS4_DQUERY)
9777c478bd9Sstevel@tonic-gate 		return (nfs4_dquery(arg, model));
9787c478bd9Sstevel@tonic-gate 
9797c478bd9Sstevel@tonic-gate 	if (secpolicy_nfs(CRED()) != 0)
9807c478bd9Sstevel@tonic-gate 		return (EPERM);
9817c478bd9Sstevel@tonic-gate 
9827c478bd9Sstevel@tonic-gate 	if ((fp = getf(STRUCT_FGET(uap, fd))) == NULL)
9837c478bd9Sstevel@tonic-gate 		return (EBADF);
9847c478bd9Sstevel@tonic-gate 
9857c478bd9Sstevel@tonic-gate 	/*
9867c478bd9Sstevel@tonic-gate 	 * Set read buffer size to rsize
9877c478bd9Sstevel@tonic-gate 	 * and add room for RPC headers.
9887c478bd9Sstevel@tonic-gate 	 */
9897c478bd9Sstevel@tonic-gate 	readsize = nfs3tsize() + (RPC_MAXDATASIZE - NFS_MAXDATA);
9907c478bd9Sstevel@tonic-gate 	if (readsize < RPC_MAXDATASIZE)
9917c478bd9Sstevel@tonic-gate 		readsize = RPC_MAXDATASIZE;
9927c478bd9Sstevel@tonic-gate 
9937c478bd9Sstevel@tonic-gate 	error = copyinstr((const char *)STRUCT_FGETP(uap, netid), buf,
9947c478bd9Sstevel@tonic-gate 	    KNC_STRSIZE, &len);
9957c478bd9Sstevel@tonic-gate 	if (error) {
9967c478bd9Sstevel@tonic-gate 		releasef(STRUCT_FGET(uap, fd));
9977c478bd9Sstevel@tonic-gate 		return (error);
9987c478bd9Sstevel@tonic-gate 	}
9997c478bd9Sstevel@tonic-gate 
10007c478bd9Sstevel@tonic-gate 	cmd = STRUCT_FGET(uap, cmd);
10017c478bd9Sstevel@tonic-gate 
10027c478bd9Sstevel@tonic-gate 	if (cmd & NFS4_KRPC_START) {
10037c478bd9Sstevel@tonic-gate 		addrmask.len = STRUCT_FGET(uap, addrmask.len);
10047c478bd9Sstevel@tonic-gate 		addrmask.maxlen = STRUCT_FGET(uap, addrmask.maxlen);
10057c478bd9Sstevel@tonic-gate 		addrmask.buf = kmem_alloc(addrmask.maxlen, KM_SLEEP);
10067c478bd9Sstevel@tonic-gate 		error = copyin(STRUCT_FGETP(uap, addrmask.buf), addrmask.buf,
10077c478bd9Sstevel@tonic-gate 		    addrmask.len);
10087c478bd9Sstevel@tonic-gate 		if (error) {
10097c478bd9Sstevel@tonic-gate 			releasef(STRUCT_FGET(uap, fd));
10107c478bd9Sstevel@tonic-gate 			kmem_free(addrmask.buf, addrmask.maxlen);
10117c478bd9Sstevel@tonic-gate 			return (error);
10127c478bd9Sstevel@tonic-gate 		}
10137c478bd9Sstevel@tonic-gate 	}
10147c478bd9Sstevel@tonic-gate 	else
10157c478bd9Sstevel@tonic-gate 		addrmask.buf = NULL;
10167c478bd9Sstevel@tonic-gate 
10177c478bd9Sstevel@tonic-gate 	error = copyinstr((const char *)STRUCT_FGETP(uap, addr), uaddr,
10187c478bd9Sstevel@tonic-gate 	    sizeof (uaddr), &len);
10197c478bd9Sstevel@tonic-gate 	if (error) {
10207c478bd9Sstevel@tonic-gate 		releasef(STRUCT_FGET(uap, fd));
10217c478bd9Sstevel@tonic-gate 		if (addrmask.buf)
10227c478bd9Sstevel@tonic-gate 			kmem_free(addrmask.buf, addrmask.maxlen);
10237c478bd9Sstevel@tonic-gate 		return (error);
10247c478bd9Sstevel@tonic-gate 	}
10257c478bd9Sstevel@tonic-gate 
10267c478bd9Sstevel@tonic-gate 	error = copyinstr((const char *)STRUCT_FGETP(uap, protofmly), protofmly,
10277c478bd9Sstevel@tonic-gate 	    sizeof (protofmly), &len);
10287c478bd9Sstevel@tonic-gate 	if (error) {
10297c478bd9Sstevel@tonic-gate 		releasef(STRUCT_FGET(uap, fd));
10307c478bd9Sstevel@tonic-gate 		if (addrmask.buf)
10317c478bd9Sstevel@tonic-gate 			kmem_free(addrmask.buf, addrmask.maxlen);
10327c478bd9Sstevel@tonic-gate 		return (error);
10337c478bd9Sstevel@tonic-gate 	}
10347c478bd9Sstevel@tonic-gate 
10357c478bd9Sstevel@tonic-gate 	error = copyinstr((const char *)STRUCT_FGETP(uap, proto), proto,
10367c478bd9Sstevel@tonic-gate 	    sizeof (proto), &len);
10377c478bd9Sstevel@tonic-gate 	if (error) {
10387c478bd9Sstevel@tonic-gate 		releasef(STRUCT_FGET(uap, fd));
10397c478bd9Sstevel@tonic-gate 		if (addrmask.buf)
10407c478bd9Sstevel@tonic-gate 			kmem_free(addrmask.buf, addrmask.maxlen);
10417c478bd9Sstevel@tonic-gate 		return (error);
10427c478bd9Sstevel@tonic-gate 	}
10437c478bd9Sstevel@tonic-gate 
1044108322fbScarlsonj 	ncg = zone_getspecific(nfs4_callback_zone_key, nfs_zone());
10457c478bd9Sstevel@tonic-gate 	ASSERT(ncg != NULL);
10467c478bd9Sstevel@tonic-gate 
10477c478bd9Sstevel@tonic-gate 	mutex_enter(&ncg->nfs4_cb_lock);
10487c478bd9Sstevel@tonic-gate 	if (cmd & NFS4_SETPORT)
10497c478bd9Sstevel@tonic-gate 		nfs4_setport(buf, uaddr, protofmly, proto, ncg);
10507c478bd9Sstevel@tonic-gate 
10517c478bd9Sstevel@tonic-gate 	if (cmd & NFS4_KRPC_START) {
10527c478bd9Sstevel@tonic-gate 		error = svc_tli_kcreate(fp, readsize, buf, &addrmask, &cb_xprt,
10537c478bd9Sstevel@tonic-gate 		    &nfs4_cb_sct, NULL, NFS_CB_SVCPOOL_ID, FALSE);
10547c478bd9Sstevel@tonic-gate 		if (error) {
10557c478bd9Sstevel@tonic-gate 			CB_WARN1("nfs4_svc: svc_tli_kcreate failed %d\n",
10569ea21c83Swebaker 			    error);
10577c478bd9Sstevel@tonic-gate 			kmem_free(addrmask.buf, addrmask.maxlen);
10587c478bd9Sstevel@tonic-gate 		}
10597c478bd9Sstevel@tonic-gate 	}
10607c478bd9Sstevel@tonic-gate 
10617c478bd9Sstevel@tonic-gate 	mutex_exit(&ncg->nfs4_cb_lock);
10627c478bd9Sstevel@tonic-gate 	releasef(STRUCT_FGET(uap, fd));
10637c478bd9Sstevel@tonic-gate 	return (error);
10647c478bd9Sstevel@tonic-gate }
10657c478bd9Sstevel@tonic-gate 
10667c478bd9Sstevel@tonic-gate struct nfs4_callback_globals *
nfs4_get_callback_globals(void)10677c478bd9Sstevel@tonic-gate nfs4_get_callback_globals(void)
10687c478bd9Sstevel@tonic-gate {
1069108322fbScarlsonj 	return (zone_getspecific(nfs4_callback_zone_key, nfs_zone()));
10707c478bd9Sstevel@tonic-gate }
10717c478bd9Sstevel@tonic-gate 
10727c478bd9Sstevel@tonic-gate static void *
nfs4_callback_init_zone(zoneid_t zoneid)10737c478bd9Sstevel@tonic-gate nfs4_callback_init_zone(zoneid_t zoneid)
10747c478bd9Sstevel@tonic-gate {
10757c478bd9Sstevel@tonic-gate 	kstat_t *nfs4_callback_kstat;
10767c478bd9Sstevel@tonic-gate 	struct nfs4_callback_globals *ncg;
10777c478bd9Sstevel@tonic-gate 
10787c478bd9Sstevel@tonic-gate 	ncg = kmem_zalloc(sizeof (*ncg), KM_SLEEP);
10797c478bd9Sstevel@tonic-gate 
10807c478bd9Sstevel@tonic-gate 	ncg->nfs4prog2server = kmem_zalloc(nfs4_num_prognums *
10819ea21c83Swebaker 	    sizeof (struct nfs4_server *), KM_SLEEP);
10827c478bd9Sstevel@tonic-gate 
10837c478bd9Sstevel@tonic-gate 	/* initialize the dlist */
10847c478bd9Sstevel@tonic-gate 	mutex_init(&ncg->nfs4_dlist_lock, NULL, MUTEX_DEFAULT, NULL);
10857c478bd9Sstevel@tonic-gate 	list_create(&ncg->nfs4_dlist, sizeof (struct nfs4_dnode),
10867c478bd9Sstevel@tonic-gate 	    offsetof(struct nfs4_dnode, linkage));
10877c478bd9Sstevel@tonic-gate 
10887c478bd9Sstevel@tonic-gate 	/* initialize cb_port list */
10897c478bd9Sstevel@tonic-gate 	mutex_init(&ncg->nfs4_cb_lock, NULL, MUTEX_DEFAULT, NULL);
10907c478bd9Sstevel@tonic-gate 	list_create(&ncg->nfs4_cb_ports, sizeof (struct nfs4_cb_port),
10917c478bd9Sstevel@tonic-gate 	    offsetof(struct nfs4_cb_port, linkage));
10927c478bd9Sstevel@tonic-gate 
10937c478bd9Sstevel@tonic-gate 	/* get our own copy of the kstats */
10947c478bd9Sstevel@tonic-gate 	bcopy(&nfs4_callback_stats_tmpl, &ncg->nfs4_callback_stats,
10957c478bd9Sstevel@tonic-gate 	    sizeof (nfs4_callback_stats_tmpl));
10967c478bd9Sstevel@tonic-gate 	/* register "nfs:0:nfs4_callback_stats" for this zone */
10977c478bd9Sstevel@tonic-gate 	if ((nfs4_callback_kstat =
10989ea21c83Swebaker 	    kstat_create_zone("nfs", 0, "nfs4_callback_stats", "misc",
10999ea21c83Swebaker 	    KSTAT_TYPE_NAMED,
11009ea21c83Swebaker 	    sizeof (ncg->nfs4_callback_stats) / sizeof (kstat_named_t),
11019ea21c83Swebaker 	    KSTAT_FLAG_VIRTUAL | KSTAT_FLAG_WRITABLE,
11029ea21c83Swebaker 	    zoneid)) != NULL) {
11037c478bd9Sstevel@tonic-gate 		nfs4_callback_kstat->ks_data = &ncg->nfs4_callback_stats;
11047c478bd9Sstevel@tonic-gate 		kstat_install(nfs4_callback_kstat);
11057c478bd9Sstevel@tonic-gate 	}
11067c478bd9Sstevel@tonic-gate 	return (ncg);
11077c478bd9Sstevel@tonic-gate }
11087c478bd9Sstevel@tonic-gate 
11097c478bd9Sstevel@tonic-gate static void
nfs4_discard_delegations(struct nfs4_callback_globals * ncg)11107c478bd9Sstevel@tonic-gate nfs4_discard_delegations(struct nfs4_callback_globals *ncg)
11117c478bd9Sstevel@tonic-gate {
11127c478bd9Sstevel@tonic-gate 	nfs4_server_t *sp;
11137c478bd9Sstevel@tonic-gate 	int i, num_removed;
11147c478bd9Sstevel@tonic-gate 
11157c478bd9Sstevel@tonic-gate 	/*
11167c478bd9Sstevel@tonic-gate 	 * It's OK here to just run through the registered "programs", as
11177c478bd9Sstevel@tonic-gate 	 * servers without programs won't have any delegations to handle.
11187c478bd9Sstevel@tonic-gate 	 */
11197c478bd9Sstevel@tonic-gate 	for (i = 0; i < nfs4_num_prognums; i++) {
11207c478bd9Sstevel@tonic-gate 		rnode4_t *rp;
11217c478bd9Sstevel@tonic-gate 
11227c478bd9Sstevel@tonic-gate 		mutex_enter(&ncg->nfs4_cb_lock);
11237c478bd9Sstevel@tonic-gate 		sp = ncg->nfs4prog2server[i];
11247c478bd9Sstevel@tonic-gate 		mutex_exit(&ncg->nfs4_cb_lock);
11257c478bd9Sstevel@tonic-gate 
11267c478bd9Sstevel@tonic-gate 		if (nfs4_server_vlock(sp, 1) == FALSE)
11277c478bd9Sstevel@tonic-gate 			continue;
11287c478bd9Sstevel@tonic-gate 		num_removed = 0;
11297c478bd9Sstevel@tonic-gate 		while ((rp = list_head(&sp->s_deleg_list)) != NULL) {
11307c478bd9Sstevel@tonic-gate 			mutex_enter(&rp->r_statev4_lock);
11317c478bd9Sstevel@tonic-gate 			if (rp->r_deleg_type == OPEN_DELEGATE_NONE) {
11327c478bd9Sstevel@tonic-gate 				/*
11337c478bd9Sstevel@tonic-gate 				 * We need to take matters into our own hands,
11347c478bd9Sstevel@tonic-gate 				 * as nfs4delegreturn_cleanup_impl() won't
11357c478bd9Sstevel@tonic-gate 				 * remove this from the list.
11367c478bd9Sstevel@tonic-gate 				 */
11377c478bd9Sstevel@tonic-gate 				list_remove(&sp->s_deleg_list, rp);
11387c478bd9Sstevel@tonic-gate 				mutex_exit(&rp->r_statev4_lock);
11397c478bd9Sstevel@tonic-gate 				nfs4_dec_state_ref_count_nolock(sp,
11407c478bd9Sstevel@tonic-gate 				    VTOMI4(RTOV4(rp)));
11417c478bd9Sstevel@tonic-gate 				num_removed++;
11427c478bd9Sstevel@tonic-gate 				continue;
11437c478bd9Sstevel@tonic-gate 			}
11447c478bd9Sstevel@tonic-gate 			mutex_exit(&rp->r_statev4_lock);
11457c478bd9Sstevel@tonic-gate 			VN_HOLD(RTOV4(rp));
11467c478bd9Sstevel@tonic-gate 			mutex_exit(&sp->s_lock);
11477c478bd9Sstevel@tonic-gate 			/*
11487c478bd9Sstevel@tonic-gate 			 * The following will remove the node from the list.
11497c478bd9Sstevel@tonic-gate 			 */
11507c478bd9Sstevel@tonic-gate 			nfs4delegreturn_cleanup_impl(rp, sp, ncg);
11517c478bd9Sstevel@tonic-gate 			VN_RELE(RTOV4(rp));
11527c478bd9Sstevel@tonic-gate 			mutex_enter(&sp->s_lock);
11537c478bd9Sstevel@tonic-gate 		}
11547c478bd9Sstevel@tonic-gate 		mutex_exit(&sp->s_lock);
11557c478bd9Sstevel@tonic-gate 		/* each removed list node reles a reference */
11567c478bd9Sstevel@tonic-gate 		while (num_removed-- > 0)
11577c478bd9Sstevel@tonic-gate 			nfs4_server_rele(sp);
11587c478bd9Sstevel@tonic-gate 		/* remove our reference for nfs4_server_vlock */
11597c478bd9Sstevel@tonic-gate 		nfs4_server_rele(sp);
11607c478bd9Sstevel@tonic-gate 	}
11617c478bd9Sstevel@tonic-gate }
11627c478bd9Sstevel@tonic-gate 
11637c478bd9Sstevel@tonic-gate /* ARGSUSED */
11647c478bd9Sstevel@tonic-gate static void
nfs4_callback_shutdown_zone(zoneid_t zoneid,void * data)11657c478bd9Sstevel@tonic-gate nfs4_callback_shutdown_zone(zoneid_t zoneid, void *data)
11667c478bd9Sstevel@tonic-gate {
11677c478bd9Sstevel@tonic-gate 	struct nfs4_callback_globals *ncg = data;
11687c478bd9Sstevel@tonic-gate 
11697c478bd9Sstevel@tonic-gate 	/*
11707c478bd9Sstevel@tonic-gate 	 * Clean pending delegation return list.
11717c478bd9Sstevel@tonic-gate 	 */
11727c478bd9Sstevel@tonic-gate 	nfs4_dlistclean_impl(ncg, NFS4_DR_DISCARD);
11737c478bd9Sstevel@tonic-gate 
11747c478bd9Sstevel@tonic-gate 	/*
11757c478bd9Sstevel@tonic-gate 	 * Discard all delegations.
11767c478bd9Sstevel@tonic-gate 	 */
11777c478bd9Sstevel@tonic-gate 	nfs4_discard_delegations(ncg);
11787c478bd9Sstevel@tonic-gate }
11797c478bd9Sstevel@tonic-gate 
11807c478bd9Sstevel@tonic-gate static void
nfs4_callback_fini_zone(zoneid_t zoneid,void * data)11817c478bd9Sstevel@tonic-gate nfs4_callback_fini_zone(zoneid_t zoneid, void *data)
11827c478bd9Sstevel@tonic-gate {
11837c478bd9Sstevel@tonic-gate 	struct nfs4_callback_globals *ncg = data;
11847c478bd9Sstevel@tonic-gate 	struct nfs4_cb_port *p;
11857c478bd9Sstevel@tonic-gate 	nfs4_server_t *sp, *next;
11867c478bd9Sstevel@tonic-gate 	nfs4_server_t freelist;
11877c478bd9Sstevel@tonic-gate 	int i;
11887c478bd9Sstevel@tonic-gate 
11897c478bd9Sstevel@tonic-gate 	kstat_delete_byname_zone("nfs", 0, "nfs4_callback_stats", zoneid);
11907c478bd9Sstevel@tonic-gate 
11917c478bd9Sstevel@tonic-gate 	/*
11927c478bd9Sstevel@tonic-gate 	 * Discard all delegations that may have crept in since we did the
11937c478bd9Sstevel@tonic-gate 	 * _shutdown.
11947c478bd9Sstevel@tonic-gate 	 */
11957c478bd9Sstevel@tonic-gate 	nfs4_discard_delegations(ncg);
11967c478bd9Sstevel@tonic-gate 	/*
11977c478bd9Sstevel@tonic-gate 	 * We're completely done with this zone and all associated
11987c478bd9Sstevel@tonic-gate 	 * nfs4_server_t's.  Any remaining nfs4_server_ts should only have one
11997c478bd9Sstevel@tonic-gate 	 * more reference outstanding -- the reference we didn't release in
12007c478bd9Sstevel@tonic-gate 	 * nfs4_renew_lease_thread().
12017c478bd9Sstevel@tonic-gate 	 *
12027c478bd9Sstevel@tonic-gate 	 * Here we need to run through the global nfs4_server_lst as we need to
12037c478bd9Sstevel@tonic-gate 	 * deal with nfs4_server_ts without programs, as they also have threads
12047c478bd9Sstevel@tonic-gate 	 * created for them, and so have outstanding references that we need to
12057c478bd9Sstevel@tonic-gate 	 * release.
12067c478bd9Sstevel@tonic-gate 	 */
12077c478bd9Sstevel@tonic-gate 	freelist.forw = &freelist;
12087c478bd9Sstevel@tonic-gate 	freelist.back = &freelist;
12097c478bd9Sstevel@tonic-gate 	mutex_enter(&nfs4_server_lst_lock);
12107c478bd9Sstevel@tonic-gate 	sp = nfs4_server_lst.forw;
12117c478bd9Sstevel@tonic-gate 	while (sp != &nfs4_server_lst) {
12127c478bd9Sstevel@tonic-gate 		next = sp->forw;
12137c478bd9Sstevel@tonic-gate 		if (sp->zoneid == zoneid) {
12147c478bd9Sstevel@tonic-gate 			remque(sp);
12157c478bd9Sstevel@tonic-gate 			insque(sp, &freelist);
12167c478bd9Sstevel@tonic-gate 		}
12177c478bd9Sstevel@tonic-gate 		sp = next;
12187c478bd9Sstevel@tonic-gate 	}
12197c478bd9Sstevel@tonic-gate 	mutex_exit(&nfs4_server_lst_lock);
12207c478bd9Sstevel@tonic-gate 
12217c478bd9Sstevel@tonic-gate 	sp = freelist.forw;
12227c478bd9Sstevel@tonic-gate 	while (sp != &freelist) {
12237c478bd9Sstevel@tonic-gate 		next = sp->forw;
12247c478bd9Sstevel@tonic-gate 		nfs4_server_rele(sp);	/* free the list's reference */
12257c478bd9Sstevel@tonic-gate 		sp = next;
12267c478bd9Sstevel@tonic-gate 	}
12277c478bd9Sstevel@tonic-gate 
12287c478bd9Sstevel@tonic-gate #ifdef DEBUG
12297c478bd9Sstevel@tonic-gate 	for (i = 0; i < nfs4_num_prognums; i++) {
12307c478bd9Sstevel@tonic-gate 		ASSERT(ncg->nfs4prog2server[i] == NULL);
12317c478bd9Sstevel@tonic-gate 	}
12327c478bd9Sstevel@tonic-gate #endif
12337c478bd9Sstevel@tonic-gate 	kmem_free(ncg->nfs4prog2server, nfs4_num_prognums *
12347c478bd9Sstevel@tonic-gate 	    sizeof (struct nfs4_server *));
12357c478bd9Sstevel@tonic-gate 
12367c478bd9Sstevel@tonic-gate 	mutex_enter(&ncg->nfs4_cb_lock);
12377c478bd9Sstevel@tonic-gate 	while ((p = list_head(&ncg->nfs4_cb_ports)) != NULL) {
12387c478bd9Sstevel@tonic-gate 		list_remove(&ncg->nfs4_cb_ports, p);
12397c478bd9Sstevel@tonic-gate 		kmem_free(p, sizeof (*p));
12407c478bd9Sstevel@tonic-gate 	}
12417c478bd9Sstevel@tonic-gate 	list_destroy(&ncg->nfs4_cb_ports);
12427c478bd9Sstevel@tonic-gate 	mutex_destroy(&ncg->nfs4_cb_lock);
12437c478bd9Sstevel@tonic-gate 	list_destroy(&ncg->nfs4_dlist);
12447c478bd9Sstevel@tonic-gate 	mutex_destroy(&ncg->nfs4_dlist_lock);
12457c478bd9Sstevel@tonic-gate 	kmem_free(ncg, sizeof (*ncg));
12467c478bd9Sstevel@tonic-gate }
12477c478bd9Sstevel@tonic-gate 
12487c478bd9Sstevel@tonic-gate void
nfs4_callback_init(void)12497c478bd9Sstevel@tonic-gate nfs4_callback_init(void)
12507c478bd9Sstevel@tonic-gate {
12517c478bd9Sstevel@tonic-gate 	int i;
12527c478bd9Sstevel@tonic-gate 	SVC_CALLOUT *nfs4_cb_sc;
12537c478bd9Sstevel@tonic-gate 
12547c478bd9Sstevel@tonic-gate 	/* initialize the callback table */
12557c478bd9Sstevel@tonic-gate 	nfs4_cb_sc = kmem_alloc(nfs4_num_prognums *
12569ea21c83Swebaker 	    sizeof (SVC_CALLOUT), KM_SLEEP);
12577c478bd9Sstevel@tonic-gate 
12587c478bd9Sstevel@tonic-gate 	for (i = 0; i < nfs4_num_prognums; i++) {
12597c478bd9Sstevel@tonic-gate 		nfs4_cb_sc[i].sc_prog = NFS4_CALLBACK+i;
12607c478bd9Sstevel@tonic-gate 		nfs4_cb_sc[i].sc_versmin = NFS_CB;
12617c478bd9Sstevel@tonic-gate 		nfs4_cb_sc[i].sc_versmax = NFS_CB;
12627c478bd9Sstevel@tonic-gate 		nfs4_cb_sc[i].sc_dispatch = cb_dispatch;
12637c478bd9Sstevel@tonic-gate 	}
12647c478bd9Sstevel@tonic-gate 
12657c478bd9Sstevel@tonic-gate 	nfs4_cb_sct.sct_size = nfs4_num_prognums;
12667c478bd9Sstevel@tonic-gate 	nfs4_cb_sct.sct_free = FALSE;
12677c478bd9Sstevel@tonic-gate 	nfs4_cb_sct.sct_sc = nfs4_cb_sc;
12687c478bd9Sstevel@tonic-gate 
12697c478bd9Sstevel@tonic-gate 	/*
12707c478bd9Sstevel@tonic-gate 	 * Compute max bytes required for dyamically allocated parts
12717c478bd9Sstevel@tonic-gate 	 * of cb_getattr reply.  Only size and change are supported now.
12727c478bd9Sstevel@tonic-gate 	 * If CB_GETATTR is changed to reply with additional attrs,
12737c478bd9Sstevel@tonic-gate 	 * additional sizes must be added below.
12747c478bd9Sstevel@tonic-gate 	 *
12757c478bd9Sstevel@tonic-gate 	 * fattr4_change + fattr4_size == uint64_t + uint64_t
12767c478bd9Sstevel@tonic-gate 	 */
12777c478bd9Sstevel@tonic-gate 	cb_getattr_bytes = 2 * BYTES_PER_XDR_UNIT + 2 * BYTES_PER_XDR_UNIT;
12787c478bd9Sstevel@tonic-gate 
12797c478bd9Sstevel@tonic-gate 	zone_key_create(&nfs4_callback_zone_key, nfs4_callback_init_zone,
12807c478bd9Sstevel@tonic-gate 	    nfs4_callback_shutdown_zone, nfs4_callback_fini_zone);
12817c478bd9Sstevel@tonic-gate }
12827c478bd9Sstevel@tonic-gate 
12837c478bd9Sstevel@tonic-gate void
nfs4_callback_fini(void)12847c478bd9Sstevel@tonic-gate nfs4_callback_fini(void)
12857c478bd9Sstevel@tonic-gate {
12867c478bd9Sstevel@tonic-gate }
12877c478bd9Sstevel@tonic-gate 
12887c478bd9Sstevel@tonic-gate /*
12897c478bd9Sstevel@tonic-gate  * NB: This function can be called from the *wrong* zone (ie, the zone that
12907c478bd9Sstevel@tonic-gate  * 'rp' belongs to and the caller's zone may not be the same).  This can happen
12917c478bd9Sstevel@tonic-gate  * if the zone is going away and we get called from nfs4_async_inactive().  In
12927c478bd9Sstevel@tonic-gate  * this case the globals will be NULL and we won't update the counters, which
12937c478bd9Sstevel@tonic-gate  * doesn't matter as the zone is going away anyhow.
12947c478bd9Sstevel@tonic-gate  */
12957c478bd9Sstevel@tonic-gate static void
nfs4delegreturn_cleanup_impl(rnode4_t * rp,nfs4_server_t * np,struct nfs4_callback_globals * ncg)12967c478bd9Sstevel@tonic-gate nfs4delegreturn_cleanup_impl(rnode4_t *rp, nfs4_server_t *np,
1297a17ce845SMarcel Telka     struct nfs4_callback_globals *ncg)
12987c478bd9Sstevel@tonic-gate {
12997c478bd9Sstevel@tonic-gate 	mntinfo4_t *mi = VTOMI4(RTOV4(rp));
13007c478bd9Sstevel@tonic-gate 	boolean_t need_rele = B_FALSE;
13017c478bd9Sstevel@tonic-gate 
13028693510cSpf 	/*
13038693510cSpf 	 * Caller must be holding mi_recovlock in read mode
13048693510cSpf 	 * to call here.  This is provided by start_op.
13058693510cSpf 	 * Delegation management requires to grab s_lock
13068693510cSpf 	 * first and then r_statev4_lock.
13078693510cSpf 	 */
13088693510cSpf 
13098693510cSpf 	if (np == NULL) {
13108693510cSpf 		np = find_nfs4_server_all(mi, 1);
131122dc8f51SPavel Filipensky 		if (np == NULL)
131222dc8f51SPavel Filipensky 			return;
13138693510cSpf 		need_rele = B_TRUE;
13148693510cSpf 	} else {
13158693510cSpf 		mutex_enter(&np->s_lock);
13168693510cSpf 	}
13178693510cSpf 
13187c478bd9Sstevel@tonic-gate 	mutex_enter(&rp->r_statev4_lock);
13197c478bd9Sstevel@tonic-gate 
132022dc8f51SPavel Filipensky 	if (rp->r_deleg_type == OPEN_DELEGATE_NONE) {
132122dc8f51SPavel Filipensky 		mutex_exit(&rp->r_statev4_lock);
132222dc8f51SPavel Filipensky 		mutex_exit(&np->s_lock);
132322dc8f51SPavel Filipensky 		if (need_rele)
132422dc8f51SPavel Filipensky 			nfs4_server_rele(np);
132522dc8f51SPavel Filipensky 		return;
132622dc8f51SPavel Filipensky 	}
132722dc8f51SPavel Filipensky 
13287c478bd9Sstevel@tonic-gate 	/*
13297c478bd9Sstevel@tonic-gate 	 * Free the cred originally held when
13307c478bd9Sstevel@tonic-gate 	 * the delegation was granted.  Caller must
13317c478bd9Sstevel@tonic-gate 	 * hold this cred if it wants to use it after
13327c478bd9Sstevel@tonic-gate 	 * this call.
13337c478bd9Sstevel@tonic-gate 	 */
13347c478bd9Sstevel@tonic-gate 	crfree(rp->r_deleg_cred);
13357c478bd9Sstevel@tonic-gate 	rp->r_deleg_cred = NULL;
13367c478bd9Sstevel@tonic-gate 	rp->r_deleg_type = OPEN_DELEGATE_NONE;
13377c478bd9Sstevel@tonic-gate 	rp->r_deleg_needs_recovery = OPEN_DELEGATE_NONE;
13387c478bd9Sstevel@tonic-gate 	rp->r_deleg_needs_recall = FALSE;
13397c478bd9Sstevel@tonic-gate 	rp->r_deleg_return_pending = FALSE;
13407c478bd9Sstevel@tonic-gate 
13417c478bd9Sstevel@tonic-gate 	/*
13427c478bd9Sstevel@tonic-gate 	 * Remove the rnode from the server's list and
13437c478bd9Sstevel@tonic-gate 	 * update the ref counts.
13447c478bd9Sstevel@tonic-gate 	 */
13457c478bd9Sstevel@tonic-gate 	list_remove(&np->s_deleg_list, rp);
13468693510cSpf 	mutex_exit(&rp->r_statev4_lock);
13477c478bd9Sstevel@tonic-gate 	nfs4_dec_state_ref_count_nolock(np, mi);
13487c478bd9Sstevel@tonic-gate 	mutex_exit(&np->s_lock);
13497c478bd9Sstevel@tonic-gate 	/* removed list node removes a reference */
13507c478bd9Sstevel@tonic-gate 	nfs4_server_rele(np);
13517c478bd9Sstevel@tonic-gate 	if (need_rele)
13527c478bd9Sstevel@tonic-gate 		nfs4_server_rele(np);
13537c478bd9Sstevel@tonic-gate 	if (ncg != NULL)
13547c478bd9Sstevel@tonic-gate 		ncg->nfs4_callback_stats.delegations.value.ui64--;
13557c478bd9Sstevel@tonic-gate }
13567c478bd9Sstevel@tonic-gate 
13577c478bd9Sstevel@tonic-gate void
nfs4delegreturn_cleanup(rnode4_t * rp,nfs4_server_t * np)13587c478bd9Sstevel@tonic-gate nfs4delegreturn_cleanup(rnode4_t *rp, nfs4_server_t *np)
13597c478bd9Sstevel@tonic-gate {
13607c478bd9Sstevel@tonic-gate 	struct nfs4_callback_globals *ncg;
13617c478bd9Sstevel@tonic-gate 
13627c478bd9Sstevel@tonic-gate 	if (np != NULL) {
13637c478bd9Sstevel@tonic-gate 		ncg = np->zone_globals;
1364108322fbScarlsonj 	} else if (nfs_zone() == VTOMI4(RTOV4(rp))->mi_zone) {
1365108322fbScarlsonj 		ncg = zone_getspecific(nfs4_callback_zone_key, nfs_zone());
13667c478bd9Sstevel@tonic-gate 		ASSERT(ncg != NULL);
13677c478bd9Sstevel@tonic-gate 	} else {
13687c478bd9Sstevel@tonic-gate 		/*
13697c478bd9Sstevel@tonic-gate 		 * Request coming from the wrong zone.
13707c478bd9Sstevel@tonic-gate 		 */
13717c478bd9Sstevel@tonic-gate 		ASSERT(getzoneid() == GLOBAL_ZONEID);
13727c478bd9Sstevel@tonic-gate 		ncg = NULL;
13737c478bd9Sstevel@tonic-gate 	}
13747c478bd9Sstevel@tonic-gate 
13757c478bd9Sstevel@tonic-gate 	nfs4delegreturn_cleanup_impl(rp, np, ncg);
13767c478bd9Sstevel@tonic-gate }
13777c478bd9Sstevel@tonic-gate 
13787c478bd9Sstevel@tonic-gate static void
nfs4delegreturn_save_lost_rqst(int error,nfs4_lost_rqst_t * lost_rqstp,cred_t * cr,vnode_t * vp)13797c478bd9Sstevel@tonic-gate nfs4delegreturn_save_lost_rqst(int error, nfs4_lost_rqst_t *lost_rqstp,
1380a17ce845SMarcel Telka     cred_t *cr, vnode_t *vp)
13817c478bd9Sstevel@tonic-gate {
13827c478bd9Sstevel@tonic-gate 	if (error != ETIMEDOUT && error != EINTR &&
13837c478bd9Sstevel@tonic-gate 	    !NFS4_FRC_UNMT_ERR(error, vp->v_vfsp)) {
13847c478bd9Sstevel@tonic-gate 		lost_rqstp->lr_op = 0;
13857c478bd9Sstevel@tonic-gate 		return;
13867c478bd9Sstevel@tonic-gate 	}
13877c478bd9Sstevel@tonic-gate 
13887c478bd9Sstevel@tonic-gate 	NFS4_DEBUG(nfs4_lost_rqst_debug, (CE_NOTE,
13899ea21c83Swebaker 	    "nfs4close_save_lost_rqst: error %d", error));
13907c478bd9Sstevel@tonic-gate 
13917c478bd9Sstevel@tonic-gate 	lost_rqstp->lr_op = OP_DELEGRETURN;
13927c478bd9Sstevel@tonic-gate 	/*
13937c478bd9Sstevel@tonic-gate 	 * The vp is held and rele'd via the recovery code.
13947c478bd9Sstevel@tonic-gate 	 * See nfs4_save_lost_rqst.
13957c478bd9Sstevel@tonic-gate 	 */
13967c478bd9Sstevel@tonic-gate 	lost_rqstp->lr_vp = vp;
13977c478bd9Sstevel@tonic-gate 	lost_rqstp->lr_dvp = NULL;
13987c478bd9Sstevel@tonic-gate 	lost_rqstp->lr_oop = NULL;
13997c478bd9Sstevel@tonic-gate 	lost_rqstp->lr_osp = NULL;
14007c478bd9Sstevel@tonic-gate 	lost_rqstp->lr_lop = NULL;
14017c478bd9Sstevel@tonic-gate 	lost_rqstp->lr_cr = cr;
14027c478bd9Sstevel@tonic-gate 	lost_rqstp->lr_flk = NULL;
14037c478bd9Sstevel@tonic-gate 	lost_rqstp->lr_putfirst = FALSE;
14047c478bd9Sstevel@tonic-gate }
14057c478bd9Sstevel@tonic-gate 
14067c478bd9Sstevel@tonic-gate static void
nfs4delegreturn_otw(rnode4_t * rp,cred_t * cr,nfs4_error_t * ep)14077c478bd9Sstevel@tonic-gate nfs4delegreturn_otw(rnode4_t *rp, cred_t *cr, nfs4_error_t *ep)
14087c478bd9Sstevel@tonic-gate {
14097c478bd9Sstevel@tonic-gate 	COMPOUND4args_clnt args;
14107c478bd9Sstevel@tonic-gate 	COMPOUND4res_clnt res;
14117c478bd9Sstevel@tonic-gate 	nfs_argop4 argops[3];
14127c478bd9Sstevel@tonic-gate 	nfs4_ga_res_t *garp = NULL;
14137c478bd9Sstevel@tonic-gate 	hrtime_t t;
14147c478bd9Sstevel@tonic-gate 	int numops;
14157c478bd9Sstevel@tonic-gate 	int doqueue = 1;
14167c478bd9Sstevel@tonic-gate 
14177c478bd9Sstevel@tonic-gate 	args.ctag = TAG_DELEGRETURN;
14187c478bd9Sstevel@tonic-gate 
14197c478bd9Sstevel@tonic-gate 	numops = 3;		/* PUTFH, GETATTR, DELEGRETURN */
14207c478bd9Sstevel@tonic-gate 
14217c478bd9Sstevel@tonic-gate 	args.array = argops;
14227c478bd9Sstevel@tonic-gate 	args.array_len = numops;
14237c478bd9Sstevel@tonic-gate 
14247c478bd9Sstevel@tonic-gate 	argops[0].argop = OP_CPUTFH;
14257c478bd9Sstevel@tonic-gate 	argops[0].nfs_argop4_u.opcputfh.sfh = rp->r_fh;
14267c478bd9Sstevel@tonic-gate 
14277c478bd9Sstevel@tonic-gate 	argops[1].argop = OP_GETATTR;
14287c478bd9Sstevel@tonic-gate 	argops[1].nfs_argop4_u.opgetattr.attr_request = NFS4_VATTR_MASK;
14297c478bd9Sstevel@tonic-gate 	argops[1].nfs_argop4_u.opgetattr.mi = VTOMI4(RTOV4(rp));
14307c478bd9Sstevel@tonic-gate 
14317c478bd9Sstevel@tonic-gate 	argops[2].argop = OP_DELEGRETURN;
14327c478bd9Sstevel@tonic-gate 	argops[2].nfs_argop4_u.opdelegreturn.deleg_stateid =
14339ea21c83Swebaker 	    rp->r_deleg_stateid;
14347c478bd9Sstevel@tonic-gate 
14357c478bd9Sstevel@tonic-gate 	t = gethrtime();
14367c478bd9Sstevel@tonic-gate 	rfs4call(VTOMI4(RTOV4(rp)), &args, &res, cr, &doqueue, 0, ep);
14377c478bd9Sstevel@tonic-gate 
14387c478bd9Sstevel@tonic-gate 	if (ep->error)
14397c478bd9Sstevel@tonic-gate 		return;
14407c478bd9Sstevel@tonic-gate 
14417c478bd9Sstevel@tonic-gate 	if (res.status == NFS4_OK) {
14427c478bd9Sstevel@tonic-gate 		garp = &res.array[1].nfs_resop4_u.opgetattr.ga_res;
14437c478bd9Sstevel@tonic-gate 		nfs4_attr_cache(RTOV4(rp), garp, t, cr, TRUE, NULL);
14447c478bd9Sstevel@tonic-gate 
14457c478bd9Sstevel@tonic-gate 	}
1446a17ce845SMarcel Telka 	xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res);
14477c478bd9Sstevel@tonic-gate }
14487c478bd9Sstevel@tonic-gate 
14497c478bd9Sstevel@tonic-gate int
nfs4_do_delegreturn(rnode4_t * rp,int flags,cred_t * cr,struct nfs4_callback_globals * ncg)14507c478bd9Sstevel@tonic-gate nfs4_do_delegreturn(rnode4_t *rp, int flags, cred_t *cr,
1451a17ce845SMarcel Telka     struct nfs4_callback_globals *ncg)
14527c478bd9Sstevel@tonic-gate {
14537c478bd9Sstevel@tonic-gate 	vnode_t *vp = RTOV4(rp);
14547c478bd9Sstevel@tonic-gate 	mntinfo4_t *mi = VTOMI4(vp);
14557c478bd9Sstevel@tonic-gate 	nfs4_lost_rqst_t lost_rqst;
14567c478bd9Sstevel@tonic-gate 	nfs4_recov_state_t recov_state;
14577c478bd9Sstevel@tonic-gate 	bool_t needrecov = FALSE, recovonly, done = FALSE;
14587c478bd9Sstevel@tonic-gate 	nfs4_error_t e = { 0, NFS4_OK, RPC_SUCCESS };
14597c478bd9Sstevel@tonic-gate 
14607c478bd9Sstevel@tonic-gate 	ncg->nfs4_callback_stats.delegreturn.value.ui64++;
14617c478bd9Sstevel@tonic-gate 
14627c478bd9Sstevel@tonic-gate 	while (!done) {
14637c478bd9Sstevel@tonic-gate 		e.error = nfs4_start_fop(mi, vp, NULL, OH_DELEGRETURN,
14649ea21c83Swebaker 		    &recov_state, &recovonly);
14657c478bd9Sstevel@tonic-gate 
14667c478bd9Sstevel@tonic-gate 		if (e.error) {
14677c478bd9Sstevel@tonic-gate 			if (flags & NFS4_DR_FORCE) {
14687c478bd9Sstevel@tonic-gate 				(void) nfs_rw_enter_sig(&mi->mi_recovlock,
14697c478bd9Sstevel@tonic-gate 				    RW_READER, 0);
14707c478bd9Sstevel@tonic-gate 				nfs4delegreturn_cleanup_impl(rp, NULL, ncg);
14717c478bd9Sstevel@tonic-gate 				nfs_rw_exit(&mi->mi_recovlock);
14727c478bd9Sstevel@tonic-gate 			}
14737c478bd9Sstevel@tonic-gate 			break;
14747c478bd9Sstevel@tonic-gate 		}
14757c478bd9Sstevel@tonic-gate 
14767c478bd9Sstevel@tonic-gate 		/*
14777c478bd9Sstevel@tonic-gate 		 * Check to see if the delegation has already been
14787c478bd9Sstevel@tonic-gate 		 * returned by the recovery thread.   The state of
14797c478bd9Sstevel@tonic-gate 		 * the delegation cannot change at this point due
14807c478bd9Sstevel@tonic-gate 		 * to start_fop and the r_deleg_recall_lock.
14817c478bd9Sstevel@tonic-gate 		 */
14827c478bd9Sstevel@tonic-gate 		if (rp->r_deleg_type == OPEN_DELEGATE_NONE) {
14837c478bd9Sstevel@tonic-gate 			e.error = 0;
14847c478bd9Sstevel@tonic-gate 			nfs4_end_op(mi, vp, NULL, &recov_state, needrecov);
14857c478bd9Sstevel@tonic-gate 			break;
14867c478bd9Sstevel@tonic-gate 		}
14877c478bd9Sstevel@tonic-gate 
14887c478bd9Sstevel@tonic-gate 		if (recovonly) {
14897c478bd9Sstevel@tonic-gate 			/*
14907c478bd9Sstevel@tonic-gate 			 * Delegation will be returned via the
14917c478bd9Sstevel@tonic-gate 			 * recovery framework.  Build a lost request
14927c478bd9Sstevel@tonic-gate 			 * structure, start recovery and get out.
14937c478bd9Sstevel@tonic-gate 			 */
14947c478bd9Sstevel@tonic-gate 			nfs4_error_init(&e, EINTR);
14957c478bd9Sstevel@tonic-gate 			nfs4delegreturn_save_lost_rqst(e.error, &lost_rqst,
14969ea21c83Swebaker 			    cr, vp);
14977c478bd9Sstevel@tonic-gate 			(void) nfs4_start_recovery(&e, mi, vp,
14989ea21c83Swebaker 			    NULL, &rp->r_deleg_stateid,
14999ea21c83Swebaker 			    lost_rqst.lr_op == OP_DELEGRETURN ?
15002f172c55SRobert Thurlow 			    &lost_rqst : NULL, OP_DELEGRETURN, NULL,
15012f172c55SRobert Thurlow 			    NULL, NULL);
15027c478bd9Sstevel@tonic-gate 			nfs4_end_op(mi, vp, NULL, &recov_state, needrecov);
15037c478bd9Sstevel@tonic-gate 			break;
15047c478bd9Sstevel@tonic-gate 		}
15057c478bd9Sstevel@tonic-gate 
15067c478bd9Sstevel@tonic-gate 		nfs4delegreturn_otw(rp, cr, &e);
15077c478bd9Sstevel@tonic-gate 
15087c478bd9Sstevel@tonic-gate 		/*
15097c478bd9Sstevel@tonic-gate 		 * Ignore some errors on delegreturn; no point in marking
15107c478bd9Sstevel@tonic-gate 		 * the file dead on a state destroying operation.
15117c478bd9Sstevel@tonic-gate 		 */
15127c478bd9Sstevel@tonic-gate 		if (e.error == 0 && (nfs4_recov_marks_dead(e.stat) ||
15137c478bd9Sstevel@tonic-gate 		    e.stat == NFS4ERR_BADHANDLE ||
15147c478bd9Sstevel@tonic-gate 		    e.stat == NFS4ERR_STALE))
15157c478bd9Sstevel@tonic-gate 			needrecov = FALSE;
15167c478bd9Sstevel@tonic-gate 		else
15177c478bd9Sstevel@tonic-gate 			needrecov = nfs4_needs_recovery(&e, TRUE, vp->v_vfsp);
15187c478bd9Sstevel@tonic-gate 
15197c478bd9Sstevel@tonic-gate 		if (needrecov) {
15207c478bd9Sstevel@tonic-gate 			nfs4delegreturn_save_lost_rqst(e.error, &lost_rqst,
15219ea21c83Swebaker 			    cr, vp);
15227c478bd9Sstevel@tonic-gate 			(void) nfs4_start_recovery(&e, mi, vp,
15239ea21c83Swebaker 			    NULL, &rp->r_deleg_stateid,
15249ea21c83Swebaker 			    lost_rqst.lr_op == OP_DELEGRETURN ?
15252f172c55SRobert Thurlow 			    &lost_rqst : NULL, OP_DELEGRETURN, NULL,
15262f172c55SRobert Thurlow 			    NULL, NULL);
15277c478bd9Sstevel@tonic-gate 		} else {
15287c478bd9Sstevel@tonic-gate 			nfs4delegreturn_cleanup_impl(rp, NULL, ncg);
15297c478bd9Sstevel@tonic-gate 			done = TRUE;
15307c478bd9Sstevel@tonic-gate 		}
15317c478bd9Sstevel@tonic-gate 
15327c478bd9Sstevel@tonic-gate 		nfs4_end_op(mi, vp, NULL, &recov_state, needrecov);
15337c478bd9Sstevel@tonic-gate 	}
15347c478bd9Sstevel@tonic-gate 	return (e.error);
15357c478bd9Sstevel@tonic-gate }
15367c478bd9Sstevel@tonic-gate 
15377c478bd9Sstevel@tonic-gate /*
15387c478bd9Sstevel@tonic-gate  * nfs4_resend_delegreturn - used to drive the delegreturn
15397c478bd9Sstevel@tonic-gate  * operation via the recovery thread.
15407c478bd9Sstevel@tonic-gate  */
15417c478bd9Sstevel@tonic-gate void
nfs4_resend_delegreturn(nfs4_lost_rqst_t * lorp,nfs4_error_t * ep,nfs4_server_t * np)15427c478bd9Sstevel@tonic-gate nfs4_resend_delegreturn(nfs4_lost_rqst_t *lorp, nfs4_error_t *ep,
1543a17ce845SMarcel Telka     nfs4_server_t *np)
15447c478bd9Sstevel@tonic-gate {
15457c478bd9Sstevel@tonic-gate 	rnode4_t *rp = VTOR4(lorp->lr_vp);
15467c478bd9Sstevel@tonic-gate 
15477c478bd9Sstevel@tonic-gate 	/* If the file failed recovery, just quit. */
15487c478bd9Sstevel@tonic-gate 	mutex_enter(&rp->r_statelock);
15497c478bd9Sstevel@tonic-gate 	if (rp->r_flags & R4RECOVERR) {
15507c478bd9Sstevel@tonic-gate 		ep->error = EIO;
15517c478bd9Sstevel@tonic-gate 	}
15527c478bd9Sstevel@tonic-gate 	mutex_exit(&rp->r_statelock);
15537c478bd9Sstevel@tonic-gate 
15547c478bd9Sstevel@tonic-gate 	if (!ep->error)
15557c478bd9Sstevel@tonic-gate 		nfs4delegreturn_otw(rp, lorp->lr_cr, ep);
15567c478bd9Sstevel@tonic-gate 
15577c478bd9Sstevel@tonic-gate 	/*
15587c478bd9Sstevel@tonic-gate 	 * If recovery is now needed, then return the error
15597c478bd9Sstevel@tonic-gate 	 * and status and let the recovery thread handle it,
15607c478bd9Sstevel@tonic-gate 	 * including re-driving another delegreturn.  Otherwise,
15617c478bd9Sstevel@tonic-gate 	 * just give up and clean up the delegation.
15627c478bd9Sstevel@tonic-gate 	 */
15637c478bd9Sstevel@tonic-gate 	if (nfs4_needs_recovery(ep, TRUE, lorp->lr_vp->v_vfsp))
15647c478bd9Sstevel@tonic-gate 		return;
15657c478bd9Sstevel@tonic-gate 
15667c478bd9Sstevel@tonic-gate 	if (rp->r_deleg_type != OPEN_DELEGATE_NONE)
15677c478bd9Sstevel@tonic-gate 		nfs4delegreturn_cleanup(rp, np);
15687c478bd9Sstevel@tonic-gate 
15697c478bd9Sstevel@tonic-gate 	nfs4_error_zinit(ep);
15707c478bd9Sstevel@tonic-gate }
15717c478bd9Sstevel@tonic-gate 
15727c478bd9Sstevel@tonic-gate /*
15737c478bd9Sstevel@tonic-gate  * nfs4delegreturn - general function to return a delegation.
15747c478bd9Sstevel@tonic-gate  *
15757c478bd9Sstevel@tonic-gate  * NFS4_DR_FORCE - return the delegation even if start_op fails
15767c478bd9Sstevel@tonic-gate  * NFS4_DR_PUSH - push modified data back to the server via VOP_PUTPAGE
15777c478bd9Sstevel@tonic-gate  * NFS4_DR_DISCARD - discard the delegation w/o delegreturn
15787c478bd9Sstevel@tonic-gate  * NFS4_DR_DID_OP - calling function already did nfs4_start_op
15797c478bd9Sstevel@tonic-gate  * NFS4_DR_RECALL - delegreturned initiated via CB_RECALL
15807c478bd9Sstevel@tonic-gate  * NFS4_DR_REOPEN - do file reopens, if applicable
15817c478bd9Sstevel@tonic-gate  */
15827c478bd9Sstevel@tonic-gate static int
nfs4delegreturn_impl(rnode4_t * rp,int flags,struct nfs4_callback_globals * ncg)15837c478bd9Sstevel@tonic-gate nfs4delegreturn_impl(rnode4_t *rp, int flags, struct nfs4_callback_globals *ncg)
15847c478bd9Sstevel@tonic-gate {
15857c478bd9Sstevel@tonic-gate 	int error = 0;
15867c478bd9Sstevel@tonic-gate 	cred_t *cr = NULL;
15877c478bd9Sstevel@tonic-gate 	vnode_t *vp;
15887c478bd9Sstevel@tonic-gate 	bool_t needrecov = FALSE;
15897c478bd9Sstevel@tonic-gate 	bool_t rw_entered = FALSE;
15907c478bd9Sstevel@tonic-gate 	bool_t do_reopen;
15917c478bd9Sstevel@tonic-gate 
15927c478bd9Sstevel@tonic-gate 	vp = RTOV4(rp);
15937c478bd9Sstevel@tonic-gate 
15947c478bd9Sstevel@tonic-gate 	/*
15957c478bd9Sstevel@tonic-gate 	 * If NFS4_DR_DISCARD is set by itself, take a short-cut and
15967c478bd9Sstevel@tonic-gate 	 * discard without doing an otw DELEGRETURN.  This may only be used
15977c478bd9Sstevel@tonic-gate 	 * by the recovery thread because it bypasses the synchronization
15987c478bd9Sstevel@tonic-gate 	 * with r_deleg_recall_lock and mi->mi_recovlock.
15997c478bd9Sstevel@tonic-gate 	 */
16007c478bd9Sstevel@tonic-gate 	if (flags == NFS4_DR_DISCARD) {
16017c478bd9Sstevel@tonic-gate 		nfs4delegreturn_cleanup_impl(rp, NULL, ncg);
16027c478bd9Sstevel@tonic-gate 		return (0);
16037c478bd9Sstevel@tonic-gate 	}
16047c478bd9Sstevel@tonic-gate 
16057c478bd9Sstevel@tonic-gate 	if (flags & NFS4_DR_DID_OP) {
16067c478bd9Sstevel@tonic-gate 		/*
16077c478bd9Sstevel@tonic-gate 		 * Caller had already done start_op, which means the
16087c478bd9Sstevel@tonic-gate 		 * r_deleg_recall_lock is already held in READ mode
16097c478bd9Sstevel@tonic-gate 		 * so we cannot take it in write mode.  Return the
16107c478bd9Sstevel@tonic-gate 		 * delegation asynchronously.
16117c478bd9Sstevel@tonic-gate 		 *
16127c478bd9Sstevel@tonic-gate 		 * Remove the NFS4_DR_DID_OP flag so we don't
16137c478bd9Sstevel@tonic-gate 		 * get stuck looping through here.
16147c478bd9Sstevel@tonic-gate 		 */
16157c478bd9Sstevel@tonic-gate 		VN_HOLD(vp);
16167c478bd9Sstevel@tonic-gate 		nfs4delegreturn_async(rp, (flags & ~NFS4_DR_DID_OP), FALSE);
16177c478bd9Sstevel@tonic-gate 		return (0);
16187c478bd9Sstevel@tonic-gate 	}
16197c478bd9Sstevel@tonic-gate 
16207c478bd9Sstevel@tonic-gate 	/*
162150af34d3Sthurlow 	 * Verify we still have a delegation and crhold the credential.
16227c478bd9Sstevel@tonic-gate 	 */
162350af34d3Sthurlow 	mutex_enter(&rp->r_statev4_lock);
162450af34d3Sthurlow 	if (rp->r_deleg_type == OPEN_DELEGATE_NONE) {
162550af34d3Sthurlow 		mutex_exit(&rp->r_statev4_lock);
16267c478bd9Sstevel@tonic-gate 		goto out;
162750af34d3Sthurlow 	}
16287c478bd9Sstevel@tonic-gate 	cr = rp->r_deleg_cred;
162950af34d3Sthurlow 	ASSERT(cr != NULL);
16307c478bd9Sstevel@tonic-gate 	crhold(cr);
163150af34d3Sthurlow 	mutex_exit(&rp->r_statev4_lock);
16327c478bd9Sstevel@tonic-gate 
16337c478bd9Sstevel@tonic-gate 	/*
16347c478bd9Sstevel@tonic-gate 	 * Push the modified data back to the server synchronously
16357c478bd9Sstevel@tonic-gate 	 * before doing DELEGRETURN.
16367c478bd9Sstevel@tonic-gate 	 */
16377c478bd9Sstevel@tonic-gate 	if (flags & NFS4_DR_PUSH)
1638da6c28aaSamw 		(void) VOP_PUTPAGE(vp, 0, 0, 0, cr, NULL);
16397c478bd9Sstevel@tonic-gate 
16407c478bd9Sstevel@tonic-gate 	/*
16417c478bd9Sstevel@tonic-gate 	 * Take r_deleg_recall_lock in WRITE mode, this will prevent
16427c478bd9Sstevel@tonic-gate 	 * nfs4_is_otw_open_necessary from trying to use the delegation
16437c478bd9Sstevel@tonic-gate 	 * while the DELEGRETURN is in progress.
16447c478bd9Sstevel@tonic-gate 	 */
16457c478bd9Sstevel@tonic-gate 	(void) nfs_rw_enter_sig(&rp->r_deleg_recall_lock, RW_WRITER, FALSE);
16467c478bd9Sstevel@tonic-gate 
16477c478bd9Sstevel@tonic-gate 	rw_entered = TRUE;
16487c478bd9Sstevel@tonic-gate 
16497c478bd9Sstevel@tonic-gate 	if (rp->r_deleg_type == OPEN_DELEGATE_NONE)
16507c478bd9Sstevel@tonic-gate 		goto out;
16517c478bd9Sstevel@tonic-gate 
16527c478bd9Sstevel@tonic-gate 	if (flags & NFS4_DR_REOPEN) {
16537c478bd9Sstevel@tonic-gate 		/*
16547c478bd9Sstevel@tonic-gate 		 * If R4RECOVERRP is already set, then skip re-opening
16557c478bd9Sstevel@tonic-gate 		 * the delegation open streams and go straight to doing
16567c478bd9Sstevel@tonic-gate 		 * delegreturn.  (XXX if the file has failed recovery, then the
16577c478bd9Sstevel@tonic-gate 		 * delegreturn attempt is likely to be futile.)
16587c478bd9Sstevel@tonic-gate 		 */
16597c478bd9Sstevel@tonic-gate 		mutex_enter(&rp->r_statelock);
16607c478bd9Sstevel@tonic-gate 		do_reopen = !(rp->r_flags & R4RECOVERRP);
16617c478bd9Sstevel@tonic-gate 		mutex_exit(&rp->r_statelock);
16627c478bd9Sstevel@tonic-gate 
16637c478bd9Sstevel@tonic-gate 		if (do_reopen) {
16647c478bd9Sstevel@tonic-gate 			error = deleg_reopen(vp, &needrecov, ncg, flags);
16657c478bd9Sstevel@tonic-gate 			if (error != 0) {
16667c478bd9Sstevel@tonic-gate 				if ((flags & (NFS4_DR_FORCE | NFS4_DR_RECALL))
16679ea21c83Swebaker 				    == 0)
16687c478bd9Sstevel@tonic-gate 					goto out;
16697c478bd9Sstevel@tonic-gate 			} else if (needrecov) {
16707c478bd9Sstevel@tonic-gate 				if ((flags & NFS4_DR_FORCE) == 0)
16717c478bd9Sstevel@tonic-gate 					goto out;
16727c478bd9Sstevel@tonic-gate 			}
16737c478bd9Sstevel@tonic-gate 		}
16747c478bd9Sstevel@tonic-gate 	}
16757c478bd9Sstevel@tonic-gate 
16767c478bd9Sstevel@tonic-gate 	if (flags & NFS4_DR_DISCARD) {
16777c478bd9Sstevel@tonic-gate 		mntinfo4_t *mi = VTOMI4(RTOV4(rp));
16787c478bd9Sstevel@tonic-gate 
16797c478bd9Sstevel@tonic-gate 		mutex_enter(&rp->r_statelock);
16807c478bd9Sstevel@tonic-gate 		/*
16817c478bd9Sstevel@tonic-gate 		 * deleg_return_pending is cleared inside of delegation_accept
16827c478bd9Sstevel@tonic-gate 		 * when a delegation is accepted.  if this flag has been
16837c478bd9Sstevel@tonic-gate 		 * cleared, then a new delegation has overwritten the one we
16847c478bd9Sstevel@tonic-gate 		 * were about to throw away.
16857c478bd9Sstevel@tonic-gate 		 */
16867c478bd9Sstevel@tonic-gate 		if (!rp->r_deleg_return_pending) {
16877c478bd9Sstevel@tonic-gate 			mutex_exit(&rp->r_statelock);
16887c478bd9Sstevel@tonic-gate 			goto out;
16897c478bd9Sstevel@tonic-gate 		}
16907c478bd9Sstevel@tonic-gate 		mutex_exit(&rp->r_statelock);
16917c478bd9Sstevel@tonic-gate 		(void) nfs_rw_enter_sig(&mi->mi_recovlock, RW_READER, FALSE);
16927c478bd9Sstevel@tonic-gate 		nfs4delegreturn_cleanup_impl(rp, NULL, ncg);
16937c478bd9Sstevel@tonic-gate 		nfs_rw_exit(&mi->mi_recovlock);
16947c478bd9Sstevel@tonic-gate 	} else {
16957c478bd9Sstevel@tonic-gate 		error = nfs4_do_delegreturn(rp, flags, cr, ncg);
16967c478bd9Sstevel@tonic-gate 	}
16977c478bd9Sstevel@tonic-gate 
16987c478bd9Sstevel@tonic-gate out:
16997c478bd9Sstevel@tonic-gate 	if (cr)
17007c478bd9Sstevel@tonic-gate 		crfree(cr);
17017c478bd9Sstevel@tonic-gate 	if (rw_entered)
17027c478bd9Sstevel@tonic-gate 		nfs_rw_exit(&rp->r_deleg_recall_lock);
17037c478bd9Sstevel@tonic-gate 	return (error);
17047c478bd9Sstevel@tonic-gate }
17057c478bd9Sstevel@tonic-gate 
17067c478bd9Sstevel@tonic-gate int
nfs4delegreturn(rnode4_t * rp,int flags)17077c478bd9Sstevel@tonic-gate nfs4delegreturn(rnode4_t *rp, int flags)
17087c478bd9Sstevel@tonic-gate {
17097c478bd9Sstevel@tonic-gate 	struct nfs4_callback_globals *ncg;
17107c478bd9Sstevel@tonic-gate 
1711108322fbScarlsonj 	ncg = zone_getspecific(nfs4_callback_zone_key, nfs_zone());
17127c478bd9Sstevel@tonic-gate 	ASSERT(ncg != NULL);
17137c478bd9Sstevel@tonic-gate 
17147c478bd9Sstevel@tonic-gate 	return (nfs4delegreturn_impl(rp, flags, ncg));
17157c478bd9Sstevel@tonic-gate }
17167c478bd9Sstevel@tonic-gate 
17177c478bd9Sstevel@tonic-gate void
nfs4delegreturn_async(rnode4_t * rp,int flags,bool_t trunc)17187c478bd9Sstevel@tonic-gate nfs4delegreturn_async(rnode4_t *rp, int flags, bool_t trunc)
17197c478bd9Sstevel@tonic-gate {
17207c478bd9Sstevel@tonic-gate 	struct cb_recall_pass *pp;
17217c478bd9Sstevel@tonic-gate 
17227c478bd9Sstevel@tonic-gate 	pp = kmem_alloc(sizeof (struct cb_recall_pass), KM_SLEEP);
17237c478bd9Sstevel@tonic-gate 	pp->rp = rp;
17247c478bd9Sstevel@tonic-gate 	pp->flags = flags;
17257c478bd9Sstevel@tonic-gate 	pp->truncate = trunc;
17267c478bd9Sstevel@tonic-gate 
17277c478bd9Sstevel@tonic-gate 	/*
17287c478bd9Sstevel@tonic-gate 	 * Fire up a thread to do the actual delegreturn
17297c478bd9Sstevel@tonic-gate 	 * Caller must guarantee that the rnode doesn't
17307c478bd9Sstevel@tonic-gate 	 * vanish (by calling VN_HOLD).
17317c478bd9Sstevel@tonic-gate 	 */
17327c478bd9Sstevel@tonic-gate 
17337c478bd9Sstevel@tonic-gate 	(void) zthread_create(NULL, 0, nfs4delegreturn_thread, pp, 0,
17349ea21c83Swebaker 	    minclsyspri);
17357c478bd9Sstevel@tonic-gate }
17367c478bd9Sstevel@tonic-gate 
17377c478bd9Sstevel@tonic-gate static void
delegreturn_all_thread(rpcprog_t * pp)17387c478bd9Sstevel@tonic-gate delegreturn_all_thread(rpcprog_t *pp)
17397c478bd9Sstevel@tonic-gate {
17407c478bd9Sstevel@tonic-gate 	nfs4_server_t *np;
17417c478bd9Sstevel@tonic-gate 	bool_t found = FALSE;
17427c478bd9Sstevel@tonic-gate 	rpcprog_t prog;
17437c478bd9Sstevel@tonic-gate 	rnode4_t *rp;
17447c478bd9Sstevel@tonic-gate 	vnode_t *vp;
17457c478bd9Sstevel@tonic-gate 	zoneid_t zoneid = getzoneid();
17467c478bd9Sstevel@tonic-gate 	struct nfs4_callback_globals *ncg;
17477c478bd9Sstevel@tonic-gate 
17487c478bd9Sstevel@tonic-gate 	NFS4_DEBUG(nfs4_drat_debug,
17499ea21c83Swebaker 	    (CE_NOTE, "delereturn_all_thread: prog %d\n", *pp));
17507c478bd9Sstevel@tonic-gate 
17517c478bd9Sstevel@tonic-gate 	prog = *pp;
17527c478bd9Sstevel@tonic-gate 	kmem_free(pp, sizeof (*pp));
17537c478bd9Sstevel@tonic-gate 	pp = NULL;
17547c478bd9Sstevel@tonic-gate 
17557c478bd9Sstevel@tonic-gate 	mutex_enter(&nfs4_server_lst_lock);
17567c478bd9Sstevel@tonic-gate 	for (np = nfs4_server_lst.forw; np != &nfs4_server_lst; np = np->forw) {
17577c478bd9Sstevel@tonic-gate 		if (np->zoneid == zoneid && np->s_program == prog) {
17587c478bd9Sstevel@tonic-gate 			mutex_enter(&np->s_lock);
17597c478bd9Sstevel@tonic-gate 			found = TRUE;
17607c478bd9Sstevel@tonic-gate 			break;
17617c478bd9Sstevel@tonic-gate 		}
17627c478bd9Sstevel@tonic-gate 	}
17637c478bd9Sstevel@tonic-gate 	mutex_exit(&nfs4_server_lst_lock);
17647c478bd9Sstevel@tonic-gate 
17657c478bd9Sstevel@tonic-gate 	/*
17667c478bd9Sstevel@tonic-gate 	 * It's possible that the nfs4_server which was using this
17677c478bd9Sstevel@tonic-gate 	 * program number has vanished since this thread is async.
17687c478bd9Sstevel@tonic-gate 	 * If so, just return.  Your work here is finished, my friend.
17697c478bd9Sstevel@tonic-gate 	 */
17707c478bd9Sstevel@tonic-gate 	if (!found)
17717c478bd9Sstevel@tonic-gate 		goto out;
17727c478bd9Sstevel@tonic-gate 
17737c478bd9Sstevel@tonic-gate 	ncg = np->zone_globals;
17747c478bd9Sstevel@tonic-gate 	while ((rp = list_head(&np->s_deleg_list)) != NULL) {
17757c478bd9Sstevel@tonic-gate 		vp = RTOV4(rp);
17767c478bd9Sstevel@tonic-gate 		VN_HOLD(vp);
17777c478bd9Sstevel@tonic-gate 		mutex_exit(&np->s_lock);
17787c478bd9Sstevel@tonic-gate 		(void) nfs4delegreturn_impl(rp, NFS4_DR_PUSH|NFS4_DR_REOPEN,
17799ea21c83Swebaker 		    ncg);
17807c478bd9Sstevel@tonic-gate 		VN_RELE(vp);
17817c478bd9Sstevel@tonic-gate 
17827c478bd9Sstevel@tonic-gate 		/* retake the s_lock for next trip through the loop */
17837c478bd9Sstevel@tonic-gate 		mutex_enter(&np->s_lock);
17847c478bd9Sstevel@tonic-gate 	}
17857c478bd9Sstevel@tonic-gate 	mutex_exit(&np->s_lock);
17867c478bd9Sstevel@tonic-gate out:
17877c478bd9Sstevel@tonic-gate 	NFS4_DEBUG(nfs4_drat_debug,
17889ea21c83Swebaker 	    (CE_NOTE, "delereturn_all_thread: complete\n"));
17897c478bd9Sstevel@tonic-gate 	zthread_exit();
17907c478bd9Sstevel@tonic-gate }
17917c478bd9Sstevel@tonic-gate 
17927c478bd9Sstevel@tonic-gate void
nfs4_delegreturn_all(nfs4_server_t * sp)17937c478bd9Sstevel@tonic-gate nfs4_delegreturn_all(nfs4_server_t *sp)
17947c478bd9Sstevel@tonic-gate {
17957c478bd9Sstevel@tonic-gate 	rpcprog_t pro, *pp;
17967c478bd9Sstevel@tonic-gate 
17977c478bd9Sstevel@tonic-gate 	mutex_enter(&sp->s_lock);
17987c478bd9Sstevel@tonic-gate 
17997c478bd9Sstevel@tonic-gate 	/* Check to see if the delegation list is empty */
18007c478bd9Sstevel@tonic-gate 
18017c478bd9Sstevel@tonic-gate 	if (list_head(&sp->s_deleg_list) == NULL) {
18027c478bd9Sstevel@tonic-gate 		mutex_exit(&sp->s_lock);
18037c478bd9Sstevel@tonic-gate 		return;
18047c478bd9Sstevel@tonic-gate 	}
18057c478bd9Sstevel@tonic-gate 	/*
18067c478bd9Sstevel@tonic-gate 	 * Grab the program number; the async thread will use this
18077c478bd9Sstevel@tonic-gate 	 * to find the nfs4_server.
18087c478bd9Sstevel@tonic-gate 	 */
18097c478bd9Sstevel@tonic-gate 	pro = sp->s_program;
18107c478bd9Sstevel@tonic-gate 	mutex_exit(&sp->s_lock);
18117c478bd9Sstevel@tonic-gate 	pp = kmem_alloc(sizeof (rpcprog_t), KM_SLEEP);
18127c478bd9Sstevel@tonic-gate 	*pp = pro;
18137c478bd9Sstevel@tonic-gate 	(void) zthread_create(NULL, 0, delegreturn_all_thread, pp, 0,
18147c478bd9Sstevel@tonic-gate 	    minclsyspri);
18157c478bd9Sstevel@tonic-gate }
18167c478bd9Sstevel@tonic-gate 
18177c478bd9Sstevel@tonic-gate 
18187c478bd9Sstevel@tonic-gate /*
18197c478bd9Sstevel@tonic-gate  * Discard any delegations
18207c478bd9Sstevel@tonic-gate  *
18217c478bd9Sstevel@tonic-gate  * Iterate over the servers s_deleg_list and
18227c478bd9Sstevel@tonic-gate  * for matching mount-point rnodes discard
18237c478bd9Sstevel@tonic-gate  * the delegation.
18247c478bd9Sstevel@tonic-gate  */
18257c478bd9Sstevel@tonic-gate void
nfs4_deleg_discard(mntinfo4_t * mi,nfs4_server_t * sp)18267c478bd9Sstevel@tonic-gate nfs4_deleg_discard(mntinfo4_t *mi, nfs4_server_t *sp)
18277c478bd9Sstevel@tonic-gate {
18287c478bd9Sstevel@tonic-gate 	rnode4_t *rp, *next;
18297c478bd9Sstevel@tonic-gate 	mntinfo4_t *r_mi;
18307c478bd9Sstevel@tonic-gate 	struct nfs4_callback_globals *ncg;
18317c478bd9Sstevel@tonic-gate 
18327c478bd9Sstevel@tonic-gate 	ASSERT(mutex_owned(&sp->s_lock));
18337c478bd9Sstevel@tonic-gate 	ncg = sp->zone_globals;
18347c478bd9Sstevel@tonic-gate 
18357c478bd9Sstevel@tonic-gate 	for (rp = list_head(&sp->s_deleg_list); rp != NULL; rp = next) {
18367c478bd9Sstevel@tonic-gate 		r_mi = VTOMI4(RTOV4(rp));
18377c478bd9Sstevel@tonic-gate 		next = list_next(&sp->s_deleg_list, rp);
18387c478bd9Sstevel@tonic-gate 
18397c478bd9Sstevel@tonic-gate 		if (r_mi != mi) {
18407c478bd9Sstevel@tonic-gate 			/*
18417c478bd9Sstevel@tonic-gate 			 * Skip if this rnode is in not on the
18427c478bd9Sstevel@tonic-gate 			 * same mount-point
18437c478bd9Sstevel@tonic-gate 			 */
18447c478bd9Sstevel@tonic-gate 			continue;
18457c478bd9Sstevel@tonic-gate 		}
18467c478bd9Sstevel@tonic-gate 
18477c478bd9Sstevel@tonic-gate 		ASSERT(rp->r_deleg_type == OPEN_DELEGATE_READ);
18487c478bd9Sstevel@tonic-gate 
18497c478bd9Sstevel@tonic-gate #ifdef DEBUG
18507c478bd9Sstevel@tonic-gate 		if (nfs4_client_recov_debug) {
18517c478bd9Sstevel@tonic-gate 			zprintf(getzoneid(),
18527c478bd9Sstevel@tonic-gate 			    "nfs4_deleg_discard: matched rnode %p "
18537c478bd9Sstevel@tonic-gate 			"-- discarding delegation\n", (void *)rp);
18547c478bd9Sstevel@tonic-gate 		}
18557c478bd9Sstevel@tonic-gate #endif
18567c478bd9Sstevel@tonic-gate 		mutex_enter(&rp->r_statev4_lock);
18577c478bd9Sstevel@tonic-gate 		/*
18587c478bd9Sstevel@tonic-gate 		 * Free the cred originally held when the delegation
18597c478bd9Sstevel@tonic-gate 		 * was granted. Also need to decrement the refcnt
18607c478bd9Sstevel@tonic-gate 		 * on this server for each delegation we discard
18617c478bd9Sstevel@tonic-gate 		 */
18627c478bd9Sstevel@tonic-gate 		if (rp->r_deleg_cred)
18637c478bd9Sstevel@tonic-gate 			crfree(rp->r_deleg_cred);
18647c478bd9Sstevel@tonic-gate 		rp->r_deleg_cred = NULL;
18657c478bd9Sstevel@tonic-gate 		rp->r_deleg_type = OPEN_DELEGATE_NONE;
18667c478bd9Sstevel@tonic-gate 		rp->r_deleg_needs_recovery = OPEN_DELEGATE_NONE;
18677c478bd9Sstevel@tonic-gate 		rp->r_deleg_needs_recall = FALSE;
18687c478bd9Sstevel@tonic-gate 		ASSERT(sp->s_refcnt > 1);
18697c478bd9Sstevel@tonic-gate 		sp->s_refcnt--;
18707c478bd9Sstevel@tonic-gate 		list_remove(&sp->s_deleg_list, rp);
18717c478bd9Sstevel@tonic-gate 		mutex_exit(&rp->r_statev4_lock);
18727c478bd9Sstevel@tonic-gate 		nfs4_dec_state_ref_count_nolock(sp, mi);
18737c478bd9Sstevel@tonic-gate 		ncg->nfs4_callback_stats.delegations.value.ui64--;
18747c478bd9Sstevel@tonic-gate 	}
18757c478bd9Sstevel@tonic-gate }
18767c478bd9Sstevel@tonic-gate 
18777c478bd9Sstevel@tonic-gate /*
18787c478bd9Sstevel@tonic-gate  * Reopen any open streams that were covered by the given file's
18797c478bd9Sstevel@tonic-gate  * delegation.
18807c478bd9Sstevel@tonic-gate  * Returns zero or an errno value.  If there was no error, *recovp
18817c478bd9Sstevel@tonic-gate  * indicates whether recovery was initiated.
18827c478bd9Sstevel@tonic-gate  */
18837c478bd9Sstevel@tonic-gate 
18847c478bd9Sstevel@tonic-gate static int
deleg_reopen(vnode_t * vp,bool_t * recovp,struct nfs4_callback_globals * ncg,int flags)18857c478bd9Sstevel@tonic-gate deleg_reopen(vnode_t *vp, bool_t *recovp, struct nfs4_callback_globals *ncg,
1886a17ce845SMarcel Telka     int flags)
18877c478bd9Sstevel@tonic-gate {
18887c478bd9Sstevel@tonic-gate 	nfs4_open_stream_t *osp;
18897c478bd9Sstevel@tonic-gate 	nfs4_recov_state_t recov_state;
18907c478bd9Sstevel@tonic-gate 	bool_t needrecov = FALSE;
18917c478bd9Sstevel@tonic-gate 	mntinfo4_t *mi;
18927c478bd9Sstevel@tonic-gate 	rnode4_t *rp;
18937c478bd9Sstevel@tonic-gate 	nfs4_error_t e = { 0, NFS4_OK, RPC_SUCCESS };
18947c478bd9Sstevel@tonic-gate 	int claimnull;
18957c478bd9Sstevel@tonic-gate 
18967c478bd9Sstevel@tonic-gate 	mi = VTOMI4(vp);
18977c478bd9Sstevel@tonic-gate 	rp = VTOR4(vp);
18987c478bd9Sstevel@tonic-gate 
18997c478bd9Sstevel@tonic-gate 	recov_state.rs_flags = 0;
19007c478bd9Sstevel@tonic-gate 	recov_state.rs_num_retry_despite_err = 0;
19017c478bd9Sstevel@tonic-gate 
19027c478bd9Sstevel@tonic-gate retry:
19037c478bd9Sstevel@tonic-gate 	if ((e.error = nfs4_start_op(mi, vp, NULL, &recov_state)) != 0) {
19047c478bd9Sstevel@tonic-gate 		return (e.error);
19057c478bd9Sstevel@tonic-gate 	}
19067c478bd9Sstevel@tonic-gate 
19077c478bd9Sstevel@tonic-gate 	/*
19087c478bd9Sstevel@tonic-gate 	 * if we mean to discard the delegation, it must be BAD, so don't
19097c478bd9Sstevel@tonic-gate 	 * use it when doing the reopen or it will fail too.
19107c478bd9Sstevel@tonic-gate 	 */
19117c478bd9Sstevel@tonic-gate 	claimnull = (flags & NFS4_DR_DISCARD);
19127c478bd9Sstevel@tonic-gate 	/*
19137c478bd9Sstevel@tonic-gate 	 * Loop through the open streams for this rnode to find
19147c478bd9Sstevel@tonic-gate 	 * all of the ones created using the delegation state ID.
19157c478bd9Sstevel@tonic-gate 	 * Each of these needs to be re-opened.
19167c478bd9Sstevel@tonic-gate 	 */
19177c478bd9Sstevel@tonic-gate 
19187c478bd9Sstevel@tonic-gate 	while ((osp = get_next_deleg_stream(rp, claimnull)) != NULL) {
19197c478bd9Sstevel@tonic-gate 
19207c478bd9Sstevel@tonic-gate 		if (claimnull) {
19217c478bd9Sstevel@tonic-gate 			nfs4_reopen(vp, osp, &e, CLAIM_NULL, FALSE, FALSE);
19227c478bd9Sstevel@tonic-gate 		} else {
19237c478bd9Sstevel@tonic-gate 			ncg->nfs4_callback_stats.claim_cur.value.ui64++;
19247c478bd9Sstevel@tonic-gate 
19257c478bd9Sstevel@tonic-gate 			nfs4_reopen(vp, osp, &e, CLAIM_DELEGATE_CUR, FALSE,
19269ea21c83Swebaker 			    FALSE);
19277c478bd9Sstevel@tonic-gate 			if (e.error == 0 && e.stat == NFS4_OK)
19287c478bd9Sstevel@tonic-gate 				ncg->nfs4_callback_stats.
19299ea21c83Swebaker 				    claim_cur_ok.value.ui64++;
19307c478bd9Sstevel@tonic-gate 		}
19317c478bd9Sstevel@tonic-gate 
19327c478bd9Sstevel@tonic-gate 		if (e.error == EAGAIN) {
193349fc7055SSimon Klinkert 			open_stream_rele(osp, rp);
19347c478bd9Sstevel@tonic-gate 			nfs4_end_op(mi, vp, NULL, &recov_state, TRUE);
19357c478bd9Sstevel@tonic-gate 			goto retry;
19367c478bd9Sstevel@tonic-gate 		}
19377c478bd9Sstevel@tonic-gate 
19387c478bd9Sstevel@tonic-gate 		/*
19397c478bd9Sstevel@tonic-gate 		 * if error is EINTR, ETIMEDOUT, or NFS4_FRC_UNMT_ERR, then
19407c478bd9Sstevel@tonic-gate 		 * recovery has already been started inside of nfs4_reopen.
19417c478bd9Sstevel@tonic-gate 		 */
19427c478bd9Sstevel@tonic-gate 		if (e.error == EINTR || e.error == ETIMEDOUT ||
19437c478bd9Sstevel@tonic-gate 		    NFS4_FRC_UNMT_ERR(e.error, vp->v_vfsp)) {
19447c478bd9Sstevel@tonic-gate 			open_stream_rele(osp, rp);
19457c478bd9Sstevel@tonic-gate 			break;
19467c478bd9Sstevel@tonic-gate 		}
19477c478bd9Sstevel@tonic-gate 
19487c478bd9Sstevel@tonic-gate 		needrecov = nfs4_needs_recovery(&e, TRUE, vp->v_vfsp);
19497c478bd9Sstevel@tonic-gate 
19507c478bd9Sstevel@tonic-gate 		if (e.error != 0 && !needrecov) {
19517c478bd9Sstevel@tonic-gate 			/*
19527c478bd9Sstevel@tonic-gate 			 * Recovery is not possible, but don't give up yet;
19537c478bd9Sstevel@tonic-gate 			 * we'd still like to do delegreturn after
19547c478bd9Sstevel@tonic-gate 			 * reopening as many streams as possible.
19557c478bd9Sstevel@tonic-gate 			 * Continue processing the open streams.
19567c478bd9Sstevel@tonic-gate 			 */
19577c478bd9Sstevel@tonic-gate 
19587c478bd9Sstevel@tonic-gate 			ncg->nfs4_callback_stats.recall_failed.value.ui64++;
19597c478bd9Sstevel@tonic-gate 
19607c478bd9Sstevel@tonic-gate 		} else if (needrecov) {
19617c478bd9Sstevel@tonic-gate 			/*
19627c478bd9Sstevel@tonic-gate 			 * Start recovery and bail out.  The recovery
19637c478bd9Sstevel@tonic-gate 			 * thread will take it from here.
19647c478bd9Sstevel@tonic-gate 			 */
19657c478bd9Sstevel@tonic-gate 			(void) nfs4_start_recovery(&e, mi, vp, NULL, NULL,
19662f172c55SRobert Thurlow 			    NULL, OP_OPEN, NULL, NULL, NULL);
19677c478bd9Sstevel@tonic-gate 			open_stream_rele(osp, rp);
19687c478bd9Sstevel@tonic-gate 			*recovp = TRUE;
19697c478bd9Sstevel@tonic-gate 			break;
19707c478bd9Sstevel@tonic-gate 		}
19717c478bd9Sstevel@tonic-gate 
19727c478bd9Sstevel@tonic-gate 		open_stream_rele(osp, rp);
19737c478bd9Sstevel@tonic-gate 	}
19747c478bd9Sstevel@tonic-gate 
19757c478bd9Sstevel@tonic-gate 	nfs4_end_op(mi, vp, NULL, &recov_state, needrecov);
19767c478bd9Sstevel@tonic-gate 
19777c478bd9Sstevel@tonic-gate 	return (e.error);
19787c478bd9Sstevel@tonic-gate }
19797c478bd9Sstevel@tonic-gate 
19807c478bd9Sstevel@tonic-gate /*
19817c478bd9Sstevel@tonic-gate  * get_next_deleg_stream - returns the next open stream which
19827c478bd9Sstevel@tonic-gate  * represents a delegation for this rnode.  In order to assure
19837c478bd9Sstevel@tonic-gate  * forward progress, the caller must guarantee that each open
19847c478bd9Sstevel@tonic-gate  * stream returned is changed so that a future call won't return
19857c478bd9Sstevel@tonic-gate  * it again.
19867c478bd9Sstevel@tonic-gate  *
19877c478bd9Sstevel@tonic-gate  * There are several ways for the open stream to change.  If the open
19887c478bd9Sstevel@tonic-gate  * stream is !os_delegation, then we aren't interested in it.  Also, if
19897c478bd9Sstevel@tonic-gate  * either os_failed_reopen or !os_valid, then don't return the osp.
19907c478bd9Sstevel@tonic-gate  *
19917c478bd9Sstevel@tonic-gate  * If claimnull is false (doing reopen CLAIM_DELEGATE_CUR) then return
19927c478bd9Sstevel@tonic-gate  * the osp if it is an os_delegation open stream.  Also, if the rnode still
19937c478bd9Sstevel@tonic-gate  * has r_deleg_return_pending, then return the os_delegation osp.  Lastly,
19947c478bd9Sstevel@tonic-gate  * if the rnode's r_deleg_stateid is different from the osp's open_stateid,
19957c478bd9Sstevel@tonic-gate  * then return the osp.
19967c478bd9Sstevel@tonic-gate  *
19977c478bd9Sstevel@tonic-gate  * We have already taken the 'r_deleg_recall_lock' as WRITER, which
19987c478bd9Sstevel@tonic-gate  * prevents new OPENs from going OTW (as start_fop takes this
19997c478bd9Sstevel@tonic-gate  * lock in READ mode); thus, no new open streams can be created
2000da6c28aaSamw  * (which inherently means no new delegation open streams are
20017c478bd9Sstevel@tonic-gate  * being created).
20027c478bd9Sstevel@tonic-gate  */
20037c478bd9Sstevel@tonic-gate 
20047c478bd9Sstevel@tonic-gate static nfs4_open_stream_t *
get_next_deleg_stream(rnode4_t * rp,int claimnull)20057c478bd9Sstevel@tonic-gate get_next_deleg_stream(rnode4_t *rp, int claimnull)
20067c478bd9Sstevel@tonic-gate {
20077c478bd9Sstevel@tonic-gate 	nfs4_open_stream_t	*osp;
20087c478bd9Sstevel@tonic-gate 
20097c478bd9Sstevel@tonic-gate 	ASSERT(nfs_rw_lock_held(&rp->r_deleg_recall_lock, RW_WRITER));
20107c478bd9Sstevel@tonic-gate 
20117c478bd9Sstevel@tonic-gate 	/*
20127c478bd9Sstevel@tonic-gate 	 * Search through the list of open streams looking for
20137c478bd9Sstevel@tonic-gate 	 * one that was created while holding the delegation.
20147c478bd9Sstevel@tonic-gate 	 */
20157c478bd9Sstevel@tonic-gate 	mutex_enter(&rp->r_os_lock);
20167c478bd9Sstevel@tonic-gate 	for (osp = list_head(&rp->r_open_streams); osp != NULL;
20177c478bd9Sstevel@tonic-gate 	    osp = list_next(&rp->r_open_streams, osp)) {
20187c478bd9Sstevel@tonic-gate 		mutex_enter(&osp->os_sync_lock);
20197c478bd9Sstevel@tonic-gate 		if (!osp->os_delegation || osp->os_failed_reopen ||
20207c478bd9Sstevel@tonic-gate 		    !osp->os_valid) {
20217c478bd9Sstevel@tonic-gate 			mutex_exit(&osp->os_sync_lock);
20227c478bd9Sstevel@tonic-gate 			continue;
20237c478bd9Sstevel@tonic-gate 		}
20247c478bd9Sstevel@tonic-gate 		if (!claimnull || rp->r_deleg_return_pending ||
20257c478bd9Sstevel@tonic-gate 		    !stateid4_cmp(&osp->open_stateid, &rp->r_deleg_stateid)) {
20267c478bd9Sstevel@tonic-gate 			osp->os_ref_count++;
20277c478bd9Sstevel@tonic-gate 			mutex_exit(&osp->os_sync_lock);
20287c478bd9Sstevel@tonic-gate 			mutex_exit(&rp->r_os_lock);
20297c478bd9Sstevel@tonic-gate 			return (osp);
20307c478bd9Sstevel@tonic-gate 		}
20317c478bd9Sstevel@tonic-gate 		mutex_exit(&osp->os_sync_lock);
20327c478bd9Sstevel@tonic-gate 	}
20337c478bd9Sstevel@tonic-gate 	mutex_exit(&rp->r_os_lock);
20347c478bd9Sstevel@tonic-gate 
20357c478bd9Sstevel@tonic-gate 	return (NULL);
20367c478bd9Sstevel@tonic-gate }
20377c478bd9Sstevel@tonic-gate 
20387c478bd9Sstevel@tonic-gate static void
nfs4delegreturn_thread(struct cb_recall_pass * args)20397c478bd9Sstevel@tonic-gate nfs4delegreturn_thread(struct cb_recall_pass *args)
20407c478bd9Sstevel@tonic-gate {
20417c478bd9Sstevel@tonic-gate 	rnode4_t *rp;
20427c478bd9Sstevel@tonic-gate 	vnode_t *vp;
20437c478bd9Sstevel@tonic-gate 	cred_t *cr;
20447c478bd9Sstevel@tonic-gate 	int dtype, error, flags;
20457c478bd9Sstevel@tonic-gate 	bool_t rdirty, rip;
20467c478bd9Sstevel@tonic-gate 	kmutex_t cpr_lock;
20477c478bd9Sstevel@tonic-gate 	callb_cpr_t cpr_info;
20487c478bd9Sstevel@tonic-gate 	struct nfs4_callback_globals *ncg;
20497c478bd9Sstevel@tonic-gate 
2050108322fbScarlsonj 	ncg = zone_getspecific(nfs4_callback_zone_key, nfs_zone());
20517c478bd9Sstevel@tonic-gate 	ASSERT(ncg != NULL);
20527c478bd9Sstevel@tonic-gate 
20537c478bd9Sstevel@tonic-gate 	mutex_init(&cpr_lock, NULL, MUTEX_DEFAULT, NULL);
20547c478bd9Sstevel@tonic-gate 
20557c478bd9Sstevel@tonic-gate 	CALLB_CPR_INIT(&cpr_info, &cpr_lock, callb_generic_cpr,
20569ea21c83Swebaker 	    "nfsv4delegRtn");
20577c478bd9Sstevel@tonic-gate 
20587c478bd9Sstevel@tonic-gate 	rp = args->rp;
20597c478bd9Sstevel@tonic-gate 	vp = RTOV4(rp);
20607c478bd9Sstevel@tonic-gate 
20617c478bd9Sstevel@tonic-gate 	mutex_enter(&rp->r_statev4_lock);
20627c478bd9Sstevel@tonic-gate 	if (rp->r_deleg_type == OPEN_DELEGATE_NONE) {
20637c478bd9Sstevel@tonic-gate 		mutex_exit(&rp->r_statev4_lock);
20647c478bd9Sstevel@tonic-gate 		goto out;
20657c478bd9Sstevel@tonic-gate 	}
20667c478bd9Sstevel@tonic-gate 	mutex_exit(&rp->r_statev4_lock);
20677c478bd9Sstevel@tonic-gate 
20687c478bd9Sstevel@tonic-gate 	/*
20697c478bd9Sstevel@tonic-gate 	 * Take the read-write lock in read mode to prevent other
20707c478bd9Sstevel@tonic-gate 	 * threads from modifying the data during the recall.  This
20717c478bd9Sstevel@tonic-gate 	 * doesn't affect mmappers.
20727c478bd9Sstevel@tonic-gate 	 */
20737c478bd9Sstevel@tonic-gate 	(void) nfs_rw_enter_sig(&rp->r_rwlock, RW_READER, FALSE);
20747c478bd9Sstevel@tonic-gate 
20757c478bd9Sstevel@tonic-gate 	/* Proceed with delegreturn */
20767c478bd9Sstevel@tonic-gate 
20777c478bd9Sstevel@tonic-gate 	mutex_enter(&rp->r_statev4_lock);
20787c478bd9Sstevel@tonic-gate 	if (rp->r_deleg_type == OPEN_DELEGATE_NONE) {
20797c478bd9Sstevel@tonic-gate 		mutex_exit(&rp->r_statev4_lock);
20807c478bd9Sstevel@tonic-gate 		nfs_rw_exit(&rp->r_rwlock);
20817c478bd9Sstevel@tonic-gate 		goto out;
20827c478bd9Sstevel@tonic-gate 	}
20837c478bd9Sstevel@tonic-gate 	dtype = rp->r_deleg_type;
20847c478bd9Sstevel@tonic-gate 	cr = rp->r_deleg_cred;
20857c478bd9Sstevel@tonic-gate 	ASSERT(cr != NULL);
20867c478bd9Sstevel@tonic-gate 	crhold(cr);
20877c478bd9Sstevel@tonic-gate 	mutex_exit(&rp->r_statev4_lock);
20887c478bd9Sstevel@tonic-gate 
20897c478bd9Sstevel@tonic-gate 	flags = args->flags;
20907c478bd9Sstevel@tonic-gate 
20917c478bd9Sstevel@tonic-gate 	/*
20927c478bd9Sstevel@tonic-gate 	 * If the file is being truncated at the server, then throw
20937c478bd9Sstevel@tonic-gate 	 * away all of the pages, it doesn't matter what flavor of
20947c478bd9Sstevel@tonic-gate 	 * delegation we have.
20957c478bd9Sstevel@tonic-gate 	 */
20967c478bd9Sstevel@tonic-gate 
20977c478bd9Sstevel@tonic-gate 	if (args->truncate) {
20987c478bd9Sstevel@tonic-gate 		ncg->nfs4_callback_stats.recall_trunc.value.ui64++;
20997c478bd9Sstevel@tonic-gate 		nfs4_invalidate_pages(vp, 0, cr);
21007c478bd9Sstevel@tonic-gate 	} else if (dtype == OPEN_DELEGATE_WRITE) {
21017c478bd9Sstevel@tonic-gate 
21027c478bd9Sstevel@tonic-gate 		mutex_enter(&rp->r_statelock);
21037c478bd9Sstevel@tonic-gate 		rdirty = rp->r_flags & R4DIRTY;
21047c478bd9Sstevel@tonic-gate 		mutex_exit(&rp->r_statelock);
21057c478bd9Sstevel@tonic-gate 
21067c478bd9Sstevel@tonic-gate 		if (rdirty) {
2107da6c28aaSamw 			error = VOP_PUTPAGE(vp, 0, 0, 0, cr, NULL);
21087c478bd9Sstevel@tonic-gate 
21097c478bd9Sstevel@tonic-gate 			if (error)
21107c478bd9Sstevel@tonic-gate 				CB_WARN1("nfs4delegreturn_thread:"
21117c478bd9Sstevel@tonic-gate 				" VOP_PUTPAGE: %d\n", error);
21127c478bd9Sstevel@tonic-gate 		}
21137c478bd9Sstevel@tonic-gate 		/* turn off NFS4_DR_PUSH because we just did that above. */
21147c478bd9Sstevel@tonic-gate 		flags &= ~NFS4_DR_PUSH;
21157c478bd9Sstevel@tonic-gate 	}
21167c478bd9Sstevel@tonic-gate 
21177c478bd9Sstevel@tonic-gate 	mutex_enter(&rp->r_statelock);
21187c478bd9Sstevel@tonic-gate 	rip =  rp->r_flags & R4RECOVERRP;
21197c478bd9Sstevel@tonic-gate 	mutex_exit(&rp->r_statelock);
21207c478bd9Sstevel@tonic-gate 
21217c478bd9Sstevel@tonic-gate 	/* If a failed recovery is indicated, discard the pages */
21227c478bd9Sstevel@tonic-gate 
21237c478bd9Sstevel@tonic-gate 	if (rip) {
21247c478bd9Sstevel@tonic-gate 
2125da6c28aaSamw 		error = VOP_PUTPAGE(vp, 0, 0, B_INVAL, cr, NULL);
21267c478bd9Sstevel@tonic-gate 
21277c478bd9Sstevel@tonic-gate 		if (error)
21287c478bd9Sstevel@tonic-gate 			CB_WARN1("nfs4delegreturn_thread: VOP_PUTPAGE: %d\n",
21299ea21c83Swebaker 			    error);
21307c478bd9Sstevel@tonic-gate 	}
21317c478bd9Sstevel@tonic-gate 
21327c478bd9Sstevel@tonic-gate 	/*
21337c478bd9Sstevel@tonic-gate 	 * Pass the flags to nfs4delegreturn_impl, but be sure not to pass
21347c478bd9Sstevel@tonic-gate 	 * NFS4_DR_DID_OP, which just calls nfs4delegreturn_async again.
21357c478bd9Sstevel@tonic-gate 	 */
21367c478bd9Sstevel@tonic-gate 	flags &= ~NFS4_DR_DID_OP;
21377c478bd9Sstevel@tonic-gate 
21387c478bd9Sstevel@tonic-gate 	(void) nfs4delegreturn_impl(rp, flags, ncg);
21397c478bd9Sstevel@tonic-gate 
21407c478bd9Sstevel@tonic-gate 	nfs_rw_exit(&rp->r_rwlock);
21417c478bd9Sstevel@tonic-gate 	crfree(cr);
21427c478bd9Sstevel@tonic-gate out:
21437c478bd9Sstevel@tonic-gate 	kmem_free(args, sizeof (struct cb_recall_pass));
21447c478bd9Sstevel@tonic-gate 	VN_RELE(vp);
21457c478bd9Sstevel@tonic-gate 	mutex_enter(&cpr_lock);
21467c478bd9Sstevel@tonic-gate 	CALLB_CPR_EXIT(&cpr_info);
21477c478bd9Sstevel@tonic-gate 	mutex_destroy(&cpr_lock);
21487c478bd9Sstevel@tonic-gate 	zthread_exit();
21497c478bd9Sstevel@tonic-gate }
21507c478bd9Sstevel@tonic-gate 
21517c478bd9Sstevel@tonic-gate /*
21527c478bd9Sstevel@tonic-gate  * This function has one assumption that the caller of this function is
21537c478bd9Sstevel@tonic-gate  * either doing recovery (therefore cannot call nfs4_start_op) or has
21547c478bd9Sstevel@tonic-gate  * already called nfs4_start_op().
21557c478bd9Sstevel@tonic-gate  */
21567c478bd9Sstevel@tonic-gate void
nfs4_delegation_accept(rnode4_t * rp,open_claim_type4 claim,OPEN4res * res,nfs4_ga_res_t * garp,cred_t * cr)21579ea21c83Swebaker nfs4_delegation_accept(rnode4_t *rp, open_claim_type4 claim, OPEN4res *res,
2158a17ce845SMarcel Telka     nfs4_ga_res_t *garp, cred_t *cr)
21597c478bd9Sstevel@tonic-gate {
21607c478bd9Sstevel@tonic-gate 	open_read_delegation4 *orp;
21617c478bd9Sstevel@tonic-gate 	open_write_delegation4 *owp;
21627c478bd9Sstevel@tonic-gate 	nfs4_server_t *np;
21637c478bd9Sstevel@tonic-gate 	bool_t already = FALSE;
21647c478bd9Sstevel@tonic-gate 	bool_t recall = FALSE;
21657c478bd9Sstevel@tonic-gate 	bool_t valid_garp = TRUE;
21669ea21c83Swebaker 	bool_t delegation_granted = FALSE;
21679ea21c83Swebaker 	bool_t dr_needed = FALSE;
21689ea21c83Swebaker 	bool_t recov;
21699ea21c83Swebaker 	int dr_flags = 0;
21707c478bd9Sstevel@tonic-gate 	long mapcnt;
21717c478bd9Sstevel@tonic-gate 	uint_t rflag;
21727c478bd9Sstevel@tonic-gate 	mntinfo4_t *mi;
21737c478bd9Sstevel@tonic-gate 	struct nfs4_callback_globals *ncg;
2174ec38504dSrmesta 	open_delegation_type4 odt;
21757c478bd9Sstevel@tonic-gate 
2176108322fbScarlsonj 	ncg = zone_getspecific(nfs4_callback_zone_key, nfs_zone());
21777c478bd9Sstevel@tonic-gate 	ASSERT(ncg != NULL);
21787c478bd9Sstevel@tonic-gate 
21799ea21c83Swebaker 	mi = VTOMI4(RTOV4(rp));
21809ea21c83Swebaker 
21819ea21c83Swebaker 	/*
21829ea21c83Swebaker 	 * Accept a delegation granted to the client via an OPEN.
21839ea21c83Swebaker 	 * Set the delegation fields in the rnode and insert the
21849ea21c83Swebaker 	 * rnode onto the list anchored in the nfs4_server_t.  The
21859ea21c83Swebaker 	 * proper locking order requires the nfs4_server_t first,
21869ea21c83Swebaker 	 * even though it may not be needed in all cases.
21879ea21c83Swebaker 	 *
21889ea21c83Swebaker 	 * NB: find_nfs4_server returns with s_lock held.
21899ea21c83Swebaker 	 */
21909ea21c83Swebaker 
21919ea21c83Swebaker 	if ((np = find_nfs4_server(mi)) == NULL)
21929ea21c83Swebaker 		return;
21939ea21c83Swebaker 
21949ea21c83Swebaker 	/* grab the statelock too, for examining r_mapcnt */
21959ea21c83Swebaker 	mutex_enter(&rp->r_statelock);
21967c478bd9Sstevel@tonic-gate 	mutex_enter(&rp->r_statev4_lock);
21977c478bd9Sstevel@tonic-gate 
21987c478bd9Sstevel@tonic-gate 	if (rp->r_deleg_type == OPEN_DELEGATE_READ ||
21997c478bd9Sstevel@tonic-gate 	    rp->r_deleg_type == OPEN_DELEGATE_WRITE)
22007c478bd9Sstevel@tonic-gate 		already = TRUE;
22017c478bd9Sstevel@tonic-gate 
2202ec38504dSrmesta 	odt = res->delegation.delegation_type;
2203ec38504dSrmesta 
2204ec38504dSrmesta 	if (odt == OPEN_DELEGATE_READ) {
22057c478bd9Sstevel@tonic-gate 
22067c478bd9Sstevel@tonic-gate 		rp->r_deleg_type = res->delegation.delegation_type;
22077c478bd9Sstevel@tonic-gate 		orp = &res->delegation.open_delegation4_u.read;
22087c478bd9Sstevel@tonic-gate 		rp->r_deleg_stateid = orp->stateid;
22097c478bd9Sstevel@tonic-gate 		rp->r_deleg_perms = orp->permissions;
22109ea21c83Swebaker 		if (claim == CLAIM_PREVIOUS)
22119ea21c83Swebaker 			if ((recall = orp->recall) != 0)
22129ea21c83Swebaker 				dr_needed = TRUE;
22139ea21c83Swebaker 
22149ea21c83Swebaker 		delegation_granted = TRUE;
22157c478bd9Sstevel@tonic-gate 
22167c478bd9Sstevel@tonic-gate 		ncg->nfs4_callback_stats.delegations.value.ui64++;
22177c478bd9Sstevel@tonic-gate 		ncg->nfs4_callback_stats.delegaccept_r.value.ui64++;
22187c478bd9Sstevel@tonic-gate 
2219ec38504dSrmesta 	} else if (odt == OPEN_DELEGATE_WRITE) {
22207c478bd9Sstevel@tonic-gate 
22217c478bd9Sstevel@tonic-gate 		rp->r_deleg_type = res->delegation.delegation_type;
22227c478bd9Sstevel@tonic-gate 		owp = &res->delegation.open_delegation4_u.write;
22237c478bd9Sstevel@tonic-gate 		rp->r_deleg_stateid = owp->stateid;
22247c478bd9Sstevel@tonic-gate 		rp->r_deleg_perms = owp->permissions;
22257c478bd9Sstevel@tonic-gate 		rp->r_deleg_limit = owp->space_limit;
22269ea21c83Swebaker 		if (claim == CLAIM_PREVIOUS)
22279ea21c83Swebaker 			if ((recall = owp->recall) != 0)
22289ea21c83Swebaker 				dr_needed = TRUE;
22299ea21c83Swebaker 
22309ea21c83Swebaker 		delegation_granted = TRUE;
22317c478bd9Sstevel@tonic-gate 
22327c478bd9Sstevel@tonic-gate 		if (garp == NULL || !garp->n4g_change_valid) {
22337c478bd9Sstevel@tonic-gate 			valid_garp = FALSE;
22347c478bd9Sstevel@tonic-gate 			rp->r_deleg_change = 0;
22357c478bd9Sstevel@tonic-gate 			rp->r_deleg_change_grant = 0;
22367c478bd9Sstevel@tonic-gate 		} else {
22377c478bd9Sstevel@tonic-gate 			rp->r_deleg_change = garp->n4g_change;
22387c478bd9Sstevel@tonic-gate 			rp->r_deleg_change_grant = garp->n4g_change;
22397c478bd9Sstevel@tonic-gate 		}
22407c478bd9Sstevel@tonic-gate 		mapcnt = rp->r_mapcnt;
22417c478bd9Sstevel@tonic-gate 		rflag = rp->r_flags;
22427c478bd9Sstevel@tonic-gate 
22437c478bd9Sstevel@tonic-gate 		/*
22447c478bd9Sstevel@tonic-gate 		 * Update the delegation change attribute if
22457c478bd9Sstevel@tonic-gate 		 * there are mappers for the file is dirty.  This
22467c478bd9Sstevel@tonic-gate 		 * might be the case during recovery after server
22477c478bd9Sstevel@tonic-gate 		 * reboot.
22487c478bd9Sstevel@tonic-gate 		 */
22497c478bd9Sstevel@tonic-gate 		if (mapcnt > 0 || rflag & R4DIRTY)
22507c478bd9Sstevel@tonic-gate 			rp->r_deleg_change++;
22517c478bd9Sstevel@tonic-gate 
22527c478bd9Sstevel@tonic-gate 		NFS4_DEBUG(nfs4_callback_debug, (CE_NOTE,
22539ea21c83Swebaker 		    "nfs4_delegation_accept: r_deleg_change: 0x%x\n",
22549ea21c83Swebaker 		    (int)(rp->r_deleg_change >> 32)));
22557c478bd9Sstevel@tonic-gate 		NFS4_DEBUG(nfs4_callback_debug, (CE_NOTE,
22569ea21c83Swebaker 		    "nfs4_delegation_accept: r_delg_change_grant: 0x%x\n",
22579ea21c83Swebaker 		    (int)(rp->r_deleg_change_grant >> 32)));
22587c478bd9Sstevel@tonic-gate 
2259ec38504dSrmesta 
2260ec38504dSrmesta 		ncg->nfs4_callback_stats.delegations.value.ui64++;
2261ec38504dSrmesta 		ncg->nfs4_callback_stats.delegaccept_rw.value.ui64++;
22629ea21c83Swebaker 	} else if (already) {
22639ea21c83Swebaker 		/*
22649ea21c83Swebaker 		 * No delegation granted.  If the rnode currently has
22659ea21c83Swebaker 		 * has one, then consider it tainted and return it.
22669ea21c83Swebaker 		 */
22679ea21c83Swebaker 		dr_needed = TRUE;
22689ea21c83Swebaker 	}
22697c478bd9Sstevel@tonic-gate 
22709ea21c83Swebaker 	if (delegation_granted) {
22719ea21c83Swebaker 		/* Add the rnode to the list. */
22729ea21c83Swebaker 		if (!already) {
22739ea21c83Swebaker 			crhold(cr);
22749ea21c83Swebaker 			rp->r_deleg_cred = cr;
22757c478bd9Sstevel@tonic-gate 
22769ea21c83Swebaker 			ASSERT(mutex_owned(&np->s_lock));
22779ea21c83Swebaker 			list_insert_head(&np->s_deleg_list, rp);
22789ea21c83Swebaker 			/* added list node gets a reference */
22799ea21c83Swebaker 			np->s_refcnt++;
22809ea21c83Swebaker 			nfs4_inc_state_ref_count_nolock(np, mi);
22817c478bd9Sstevel@tonic-gate 		}
22829ea21c83Swebaker 		rp->r_deleg_needs_recovery = OPEN_DELEGATE_NONE;
22837c478bd9Sstevel@tonic-gate 	}
22847c478bd9Sstevel@tonic-gate 
22857c478bd9Sstevel@tonic-gate 	/*
22869ea21c83Swebaker 	 * We've now safely accepted the delegation, if any.  Drop the
22879ea21c83Swebaker 	 * locks and figure out what post-processing is needed.  We'd
22889ea21c83Swebaker 	 * like to retain r_statev4_lock, but nfs4_server_rele takes
22899ea21c83Swebaker 	 * s_lock which would be a lock ordering violation.
22907c478bd9Sstevel@tonic-gate 	 */
22917c478bd9Sstevel@tonic-gate 	mutex_exit(&rp->r_statev4_lock);
22929ea21c83Swebaker 	mutex_exit(&rp->r_statelock);
22939ea21c83Swebaker 	mutex_exit(&np->s_lock);
22949ea21c83Swebaker 	nfs4_server_rele(np);
22959ea21c83Swebaker 
22969ea21c83Swebaker 	/*
22979ea21c83Swebaker 	 * Check to see if we are in recovery.  Remember that
22989ea21c83Swebaker 	 * this function is protected by start_op, so a recovery
22999ea21c83Swebaker 	 * cannot begin until we are out of here.
23009ea21c83Swebaker 	 */
23019ea21c83Swebaker 	mutex_enter(&mi->mi_lock);
23029ea21c83Swebaker 	recov = mi->mi_recovflags & MI4_RECOV_ACTIV;
23039ea21c83Swebaker 	mutex_exit(&mi->mi_lock);
23047c478bd9Sstevel@tonic-gate 
23059ea21c83Swebaker 	mutex_enter(&rp->r_statev4_lock);
23067c478bd9Sstevel@tonic-gate 
23079ea21c83Swebaker 	if (nfs4_delegreturn_policy == IMMEDIATE || !valid_garp)
23089ea21c83Swebaker 		dr_needed = TRUE;
23097c478bd9Sstevel@tonic-gate 
23109ea21c83Swebaker 	if (dr_needed && rp->r_deleg_return_pending == FALSE) {
23119ea21c83Swebaker 		if (recov) {
23129ea21c83Swebaker 			/*
23139ea21c83Swebaker 			 * We cannot call delegreturn from inside
23149ea21c83Swebaker 			 * of recovery or VOP_PUTPAGE will hang
23159ea21c83Swebaker 			 * due to nfs4_start_fop call in
23169ea21c83Swebaker 			 * nfs4write.  Use dlistadd to add the
23179ea21c83Swebaker 			 * rnode to the list of rnodes needing
23189ea21c83Swebaker 			 * cleaning.  We do not need to do reopen
23199ea21c83Swebaker 			 * here because recov_openfiles will do it.
23209ea21c83Swebaker 			 * In the non-recall case, just discard the
23219ea21c83Swebaker 			 * delegation as it is no longer valid.
23229ea21c83Swebaker 			 */
23239ea21c83Swebaker 			if (recall)
23249ea21c83Swebaker 				dr_flags = NFS4_DR_PUSH;
23259ea21c83Swebaker 			else
23269ea21c83Swebaker 				dr_flags = NFS4_DR_PUSH|NFS4_DR_DISCARD;
23277c478bd9Sstevel@tonic-gate 
23289ea21c83Swebaker 			nfs4_dlistadd(rp, ncg, dr_flags);
23299ea21c83Swebaker 			dr_flags = 0;
23309ea21c83Swebaker 		} else {
23319ea21c83Swebaker 			/*
23329ea21c83Swebaker 			 * Push the modified data back to the server,
23339ea21c83Swebaker 			 * reopen any delegation open streams, and return
23349ea21c83Swebaker 			 * the delegation.  Drop the statev4_lock first!
23359ea21c83Swebaker 			 */
23369ea21c83Swebaker 			dr_flags =  NFS4_DR_PUSH|NFS4_DR_DID_OP|NFS4_DR_REOPEN;
23377c478bd9Sstevel@tonic-gate 		}
23387c478bd9Sstevel@tonic-gate 	}
23399ea21c83Swebaker 	mutex_exit(&rp->r_statev4_lock);
23409ea21c83Swebaker 	if (dr_flags)
23419ea21c83Swebaker 		(void) nfs4delegreturn_impl(rp, dr_flags, ncg);
23427c478bd9Sstevel@tonic-gate }
23437c478bd9Sstevel@tonic-gate 
23447c478bd9Sstevel@tonic-gate /*
23457c478bd9Sstevel@tonic-gate  * nfs4delegabandon - Abandon the delegation on an rnode4.  This code
23467c478bd9Sstevel@tonic-gate  * is called when the client receives EXPIRED, BAD_STATEID, OLD_STATEID
23477c478bd9Sstevel@tonic-gate  * or BADSEQID and the recovery code is unable to recover.  Push any
23487c478bd9Sstevel@tonic-gate  * dirty data back to the server and return the delegation (if any).
23497c478bd9Sstevel@tonic-gate  */
23507c478bd9Sstevel@tonic-gate 
23517c478bd9Sstevel@tonic-gate void
nfs4delegabandon(rnode4_t * rp)23527c478bd9Sstevel@tonic-gate nfs4delegabandon(rnode4_t *rp)
23537c478bd9Sstevel@tonic-gate {
23547c478bd9Sstevel@tonic-gate 	vnode_t *vp;
23557c478bd9Sstevel@tonic-gate 	struct cb_recall_pass *pp;
23567c478bd9Sstevel@tonic-gate 	open_delegation_type4 dt;
23577c478bd9Sstevel@tonic-gate 
23587c478bd9Sstevel@tonic-gate 	mutex_enter(&rp->r_statev4_lock);
23597c478bd9Sstevel@tonic-gate 	dt = rp->r_deleg_type;
23607c478bd9Sstevel@tonic-gate 	mutex_exit(&rp->r_statev4_lock);
23617c478bd9Sstevel@tonic-gate 
23627c478bd9Sstevel@tonic-gate 	if (dt == OPEN_DELEGATE_NONE)
23637c478bd9Sstevel@tonic-gate 		return;
23647c478bd9Sstevel@tonic-gate 
23657c478bd9Sstevel@tonic-gate 	vp = RTOV4(rp);
23667c478bd9Sstevel@tonic-gate 	VN_HOLD(vp);
23677c478bd9Sstevel@tonic-gate 
23687c478bd9Sstevel@tonic-gate 	pp = kmem_alloc(sizeof (struct cb_recall_pass), KM_SLEEP);
23697c478bd9Sstevel@tonic-gate 	pp->rp = rp;
23707c478bd9Sstevel@tonic-gate 	/*
23717c478bd9Sstevel@tonic-gate 	 * Recovery on the file has failed and we want to return
23727c478bd9Sstevel@tonic-gate 	 * the delegation.  We don't want to reopen files and
23737c478bd9Sstevel@tonic-gate 	 * nfs4delegreturn_thread() figures out what to do about
23747c478bd9Sstevel@tonic-gate 	 * the data.  The only thing to do is attempt to return
23757c478bd9Sstevel@tonic-gate 	 * the delegation.
23767c478bd9Sstevel@tonic-gate 	 */
23777c478bd9Sstevel@tonic-gate 	pp->flags = 0;
23787c478bd9Sstevel@tonic-gate 	pp->truncate = FALSE;
23797c478bd9Sstevel@tonic-gate 
23807c478bd9Sstevel@tonic-gate 	/*
23817c478bd9Sstevel@tonic-gate 	 * Fire up a thread to do the delegreturn; this is
23827c478bd9Sstevel@tonic-gate 	 * necessary because we could be inside a GETPAGE or
23837c478bd9Sstevel@tonic-gate 	 * PUTPAGE and we cannot do another one.
23847c478bd9Sstevel@tonic-gate 	 */
23857c478bd9Sstevel@tonic-gate 
23867c478bd9Sstevel@tonic-gate 	(void) zthread_create(NULL, 0, nfs4delegreturn_thread, pp, 0,
23879ea21c83Swebaker 	    minclsyspri);
23887c478bd9Sstevel@tonic-gate }
23897c478bd9Sstevel@tonic-gate 
23907c478bd9Sstevel@tonic-gate static int
wait_for_recall1(vnode_t * vp,nfs4_op_hint_t op,nfs4_recov_state_t * rsp,int flg)23917c478bd9Sstevel@tonic-gate wait_for_recall1(vnode_t *vp, nfs4_op_hint_t op, nfs4_recov_state_t *rsp,
2392a17ce845SMarcel Telka     int flg)
23937c478bd9Sstevel@tonic-gate {
23947c478bd9Sstevel@tonic-gate 	rnode4_t *rp;
23957c478bd9Sstevel@tonic-gate 	int error = 0;
23967c478bd9Sstevel@tonic-gate 
23977c478bd9Sstevel@tonic-gate #ifdef lint
23987c478bd9Sstevel@tonic-gate 	op = op;
23997c478bd9Sstevel@tonic-gate #endif
24007c478bd9Sstevel@tonic-gate 
24017c478bd9Sstevel@tonic-gate 	if (vp && vp->v_type == VREG) {
24027c478bd9Sstevel@tonic-gate 		rp = VTOR4(vp);
24037c478bd9Sstevel@tonic-gate 
24047c478bd9Sstevel@tonic-gate 		/*
24057c478bd9Sstevel@tonic-gate 		 * Take r_deleg_recall_lock in read mode to synchronize
24067c478bd9Sstevel@tonic-gate 		 * with delegreturn.
24077c478bd9Sstevel@tonic-gate 		 */
24087c478bd9Sstevel@tonic-gate 		error = nfs_rw_enter_sig(&rp->r_deleg_recall_lock,
24099ea21c83Swebaker 		    RW_READER, INTR4(vp));
24107c478bd9Sstevel@tonic-gate 
24117c478bd9Sstevel@tonic-gate 		if (error == 0)
24127c478bd9Sstevel@tonic-gate 			rsp->rs_flags |= flg;
24137c478bd9Sstevel@tonic-gate 
24147c478bd9Sstevel@tonic-gate 	}
24157c478bd9Sstevel@tonic-gate 	return (error);
24167c478bd9Sstevel@tonic-gate }
24177c478bd9Sstevel@tonic-gate 
24187c478bd9Sstevel@tonic-gate void
nfs4_end_op_recall(vnode_t * vp1,vnode_t * vp2,nfs4_recov_state_t * rsp)24197c478bd9Sstevel@tonic-gate nfs4_end_op_recall(vnode_t *vp1, vnode_t *vp2, nfs4_recov_state_t *rsp)
24207c478bd9Sstevel@tonic-gate {
24217c478bd9Sstevel@tonic-gate 	NFS4_DEBUG(nfs4_recall_debug,
24229ea21c83Swebaker 	    (CE_NOTE, "nfs4_end_op_recall: 0x%p, 0x%p\n",
24239ea21c83Swebaker 	    (void *)vp1, (void *)vp2));
24247c478bd9Sstevel@tonic-gate 
24257c478bd9Sstevel@tonic-gate 	if (vp2 && rsp->rs_flags & NFS4_RS_RECALL_HELD2)
24267c478bd9Sstevel@tonic-gate 		nfs_rw_exit(&VTOR4(vp2)->r_deleg_recall_lock);
24277c478bd9Sstevel@tonic-gate 	if (vp1 && rsp->rs_flags & NFS4_RS_RECALL_HELD1)
24287c478bd9Sstevel@tonic-gate 		nfs_rw_exit(&VTOR4(vp1)->r_deleg_recall_lock);
24297c478bd9Sstevel@tonic-gate }
24307c478bd9Sstevel@tonic-gate 
24317c478bd9Sstevel@tonic-gate int
wait_for_recall(vnode_t * vp1,vnode_t * vp2,nfs4_op_hint_t op,nfs4_recov_state_t * rsp)24327c478bd9Sstevel@tonic-gate wait_for_recall(vnode_t *vp1, vnode_t *vp2, nfs4_op_hint_t op,
2433a17ce845SMarcel Telka     nfs4_recov_state_t *rsp)
24347c478bd9Sstevel@tonic-gate {
24357c478bd9Sstevel@tonic-gate 	int error;
24367c478bd9Sstevel@tonic-gate 
24377c478bd9Sstevel@tonic-gate 	NFS4_DEBUG(nfs4_recall_debug,
24389ea21c83Swebaker 	    (CE_NOTE, "wait_for_recall:    0x%p, 0x%p\n",
24399ea21c83Swebaker 	    (void *)vp1, (void *) vp2));
24407c478bd9Sstevel@tonic-gate 
24417c478bd9Sstevel@tonic-gate 	rsp->rs_flags &= ~(NFS4_RS_RECALL_HELD1|NFS4_RS_RECALL_HELD2);
24427c478bd9Sstevel@tonic-gate 
24437c478bd9Sstevel@tonic-gate 	if ((error = wait_for_recall1(vp1, op, rsp, NFS4_RS_RECALL_HELD1)) != 0)
24447c478bd9Sstevel@tonic-gate 		return (error);
24457c478bd9Sstevel@tonic-gate 
24467c478bd9Sstevel@tonic-gate 	if ((error = wait_for_recall1(vp2, op, rsp, NFS4_RS_RECALL_HELD2))
24477c478bd9Sstevel@tonic-gate 	    != 0) {
24487c478bd9Sstevel@tonic-gate 		if (rsp->rs_flags & NFS4_RS_RECALL_HELD1) {
24497c478bd9Sstevel@tonic-gate 			nfs_rw_exit(&VTOR4(vp1)->r_deleg_recall_lock);
24507c478bd9Sstevel@tonic-gate 			rsp->rs_flags &= ~NFS4_RS_RECALL_HELD1;
24517c478bd9Sstevel@tonic-gate 		}
24527c478bd9Sstevel@tonic-gate 
24537c478bd9Sstevel@tonic-gate 		return (error);
24547c478bd9Sstevel@tonic-gate 	}
24557c478bd9Sstevel@tonic-gate 
24567c478bd9Sstevel@tonic-gate 	return (0);
24577c478bd9Sstevel@tonic-gate }
24587c478bd9Sstevel@tonic-gate 
24597c478bd9Sstevel@tonic-gate /*
24607c478bd9Sstevel@tonic-gate  * nfs4_dlistadd - Add this rnode to a list of rnodes to be
24617c478bd9Sstevel@tonic-gate  * DELEGRETURN'd at the end of recovery.
24627c478bd9Sstevel@tonic-gate  */
24637c478bd9Sstevel@tonic-gate 
24647c478bd9Sstevel@tonic-gate static void
nfs4_dlistadd(rnode4_t * rp,struct nfs4_callback_globals * ncg,int flags)24657c478bd9Sstevel@tonic-gate nfs4_dlistadd(rnode4_t *rp, struct nfs4_callback_globals *ncg, int flags)
24667c478bd9Sstevel@tonic-gate {
24677c478bd9Sstevel@tonic-gate 	struct nfs4_dnode *dp;
24687c478bd9Sstevel@tonic-gate 
24697c478bd9Sstevel@tonic-gate 	ASSERT(mutex_owned(&rp->r_statev4_lock));
24707c478bd9Sstevel@tonic-gate 	/*
24717c478bd9Sstevel@tonic-gate 	 * Mark the delegation as having a return pending.
24727c478bd9Sstevel@tonic-gate 	 * This will prevent the use of the delegation stateID
24737c478bd9Sstevel@tonic-gate 	 * by read, write, setattr and open.
24747c478bd9Sstevel@tonic-gate 	 */
24757c478bd9Sstevel@tonic-gate 	rp->r_deleg_return_pending = TRUE;
24767c478bd9Sstevel@tonic-gate 	dp = kmem_alloc(sizeof (*dp), KM_SLEEP);
24777c478bd9Sstevel@tonic-gate 	VN_HOLD(RTOV4(rp));
24787c478bd9Sstevel@tonic-gate 	dp->rnodep = rp;
24797c478bd9Sstevel@tonic-gate 	dp->flags = flags;
24807c478bd9Sstevel@tonic-gate 	mutex_enter(&ncg->nfs4_dlist_lock);
24817c478bd9Sstevel@tonic-gate 	list_insert_head(&ncg->nfs4_dlist, dp);
24827c478bd9Sstevel@tonic-gate #ifdef	DEBUG
24837c478bd9Sstevel@tonic-gate 	ncg->nfs4_dlistadd_c++;
24847c478bd9Sstevel@tonic-gate #endif
24857c478bd9Sstevel@tonic-gate 	mutex_exit(&ncg->nfs4_dlist_lock);
24867c478bd9Sstevel@tonic-gate }
24877c478bd9Sstevel@tonic-gate 
24887c478bd9Sstevel@tonic-gate /*
24897c478bd9Sstevel@tonic-gate  * nfs4_dlistclean_impl - Do DELEGRETURN for each rnode on the list.
24907c478bd9Sstevel@tonic-gate  * of files awaiting cleaning.  If the override_flags are non-zero
24917c478bd9Sstevel@tonic-gate  * then use them rather than the flags that were set when the rnode
24927c478bd9Sstevel@tonic-gate  * was added to the dlist.
24937c478bd9Sstevel@tonic-gate  */
24947c478bd9Sstevel@tonic-gate static void
nfs4_dlistclean_impl(struct nfs4_callback_globals * ncg,int override_flags)24957c478bd9Sstevel@tonic-gate nfs4_dlistclean_impl(struct nfs4_callback_globals *ncg, int override_flags)
24967c478bd9Sstevel@tonic-gate {
24977c478bd9Sstevel@tonic-gate 	rnode4_t *rp;
24987c478bd9Sstevel@tonic-gate 	struct nfs4_dnode *dp;
24997c478bd9Sstevel@tonic-gate 	int flags;
25007c478bd9Sstevel@tonic-gate 
25017c478bd9Sstevel@tonic-gate 	ASSERT(override_flags == 0 || override_flags == NFS4_DR_DISCARD);
25027c478bd9Sstevel@tonic-gate 
25037c478bd9Sstevel@tonic-gate 	mutex_enter(&ncg->nfs4_dlist_lock);
25047c478bd9Sstevel@tonic-gate 	while ((dp = list_head(&ncg->nfs4_dlist)) != NULL) {
25057c478bd9Sstevel@tonic-gate #ifdef	DEBUG
25067c478bd9Sstevel@tonic-gate 		ncg->nfs4_dlistclean_c++;
25077c478bd9Sstevel@tonic-gate #endif
25087c478bd9Sstevel@tonic-gate 		list_remove(&ncg->nfs4_dlist, dp);
25097c478bd9Sstevel@tonic-gate 		mutex_exit(&ncg->nfs4_dlist_lock);
25107c478bd9Sstevel@tonic-gate 		rp = dp->rnodep;
25117c478bd9Sstevel@tonic-gate 		flags = (override_flags != 0) ? override_flags : dp->flags;
25127c478bd9Sstevel@tonic-gate 		kmem_free(dp, sizeof (*dp));
25137c478bd9Sstevel@tonic-gate 		(void) nfs4delegreturn_impl(rp, flags, ncg);
25147c478bd9Sstevel@tonic-gate 		VN_RELE(RTOV4(rp));
25157c478bd9Sstevel@tonic-gate 		mutex_enter(&ncg->nfs4_dlist_lock);
25167c478bd9Sstevel@tonic-gate 	}
25177c478bd9Sstevel@tonic-gate 	mutex_exit(&ncg->nfs4_dlist_lock);
25187c478bd9Sstevel@tonic-gate }
25197c478bd9Sstevel@tonic-gate 
25207c478bd9Sstevel@tonic-gate void
nfs4_dlistclean(void)25217c478bd9Sstevel@tonic-gate nfs4_dlistclean(void)
25227c478bd9Sstevel@tonic-gate {
25237c478bd9Sstevel@tonic-gate 	struct nfs4_callback_globals *ncg;
25247c478bd9Sstevel@tonic-gate 
2525108322fbScarlsonj 	ncg = zone_getspecific(nfs4_callback_zone_key, nfs_zone());
25267c478bd9Sstevel@tonic-gate 	ASSERT(ncg != NULL);
25277c478bd9Sstevel@tonic-gate 
25287c478bd9Sstevel@tonic-gate 	nfs4_dlistclean_impl(ncg, 0);
25297c478bd9Sstevel@tonic-gate }
2530