xref: /illumos-gate/usr/src/uts/common/fs/nfs/nfs4_srv.c (revision aa59c4cb)
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
545916cd2Sjpk  * Common Development and Distribution License (the "License").
645916cd2Sjpk  * 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 /*
22*aa59c4cbSrsb  * Copyright 2007 Sun Microsystems, Inc.  All rights reserved.
237c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
247c478bd9Sstevel@tonic-gate  */
257c478bd9Sstevel@tonic-gate 
267c478bd9Sstevel@tonic-gate /*
277c478bd9Sstevel@tonic-gate  *	Copyright (c) 1983,1984,1985,1986,1987,1988,1989  AT&T.
287c478bd9Sstevel@tonic-gate  *	All Rights Reserved
297c478bd9Sstevel@tonic-gate  */
307c478bd9Sstevel@tonic-gate 
317c478bd9Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
327c478bd9Sstevel@tonic-gate 
337c478bd9Sstevel@tonic-gate #include <sys/param.h>
347c478bd9Sstevel@tonic-gate #include <sys/types.h>
357c478bd9Sstevel@tonic-gate #include <sys/systm.h>
367c478bd9Sstevel@tonic-gate #include <sys/cred.h>
377c478bd9Sstevel@tonic-gate #include <sys/buf.h>
387c478bd9Sstevel@tonic-gate #include <sys/vfs.h>
39*aa59c4cbSrsb #include <sys/vfs_opreg.h>
407c478bd9Sstevel@tonic-gate #include <sys/vnode.h>
417c478bd9Sstevel@tonic-gate #include <sys/uio.h>
427c478bd9Sstevel@tonic-gate #include <sys/errno.h>
437c478bd9Sstevel@tonic-gate #include <sys/sysmacros.h>
447c478bd9Sstevel@tonic-gate #include <sys/statvfs.h>
457c478bd9Sstevel@tonic-gate #include <sys/kmem.h>
467c478bd9Sstevel@tonic-gate #include <sys/dirent.h>
477c478bd9Sstevel@tonic-gate #include <sys/cmn_err.h>
487c478bd9Sstevel@tonic-gate #include <sys/debug.h>
497c478bd9Sstevel@tonic-gate #include <sys/systeminfo.h>
507c478bd9Sstevel@tonic-gate #include <sys/flock.h>
517c478bd9Sstevel@tonic-gate #include <sys/pathname.h>
527c478bd9Sstevel@tonic-gate #include <sys/nbmlock.h>
537c478bd9Sstevel@tonic-gate #include <sys/share.h>
547c478bd9Sstevel@tonic-gate #include <sys/atomic.h>
557c478bd9Sstevel@tonic-gate #include <sys/policy.h>
567c478bd9Sstevel@tonic-gate #include <sys/fem.h>
5745916cd2Sjpk #include <sys/sdt.h>
58cee86682Scalum #include <sys/ddi.h>
597c478bd9Sstevel@tonic-gate 
607c478bd9Sstevel@tonic-gate #include <rpc/types.h>
617c478bd9Sstevel@tonic-gate #include <rpc/auth.h>
627c478bd9Sstevel@tonic-gate #include <rpc/rpcsec_gss.h>
637c478bd9Sstevel@tonic-gate #include <rpc/svc.h>
647c478bd9Sstevel@tonic-gate 
657c478bd9Sstevel@tonic-gate #include <nfs/nfs.h>
667c478bd9Sstevel@tonic-gate #include <nfs/export.h>
677c478bd9Sstevel@tonic-gate #include <nfs/lm.h>
687c478bd9Sstevel@tonic-gate #include <nfs/nfs4.h>
697c478bd9Sstevel@tonic-gate 
707c478bd9Sstevel@tonic-gate #include <sys/strsubr.h>
717c478bd9Sstevel@tonic-gate #include <sys/strsun.h>
727c478bd9Sstevel@tonic-gate 
737c478bd9Sstevel@tonic-gate #include <inet/common.h>
747c478bd9Sstevel@tonic-gate #include <inet/ip.h>
757c478bd9Sstevel@tonic-gate #include <inet/ip6.h>
767c478bd9Sstevel@tonic-gate 
7745916cd2Sjpk #include <sys/tsol/label.h>
7845916cd2Sjpk #include <sys/tsol/tndb.h>
7945916cd2Sjpk 
807c478bd9Sstevel@tonic-gate #define	RFS4_MAXLOCK_TRIES 4	/* Try to get the lock this many times */
817c478bd9Sstevel@tonic-gate static int rfs4_maxlock_tries = RFS4_MAXLOCK_TRIES;
827c478bd9Sstevel@tonic-gate #define	RFS4_LOCK_DELAY 10	/* Milliseconds */
837c478bd9Sstevel@tonic-gate static clock_t rfs4_lock_delay = RFS4_LOCK_DELAY;
847c478bd9Sstevel@tonic-gate 
857c478bd9Sstevel@tonic-gate /* End of Tunables */
867c478bd9Sstevel@tonic-gate 
877c478bd9Sstevel@tonic-gate /*
887c478bd9Sstevel@tonic-gate  * Used to bump the stateid4.seqid value and show changes in the stateid
897c478bd9Sstevel@tonic-gate  */
907c478bd9Sstevel@tonic-gate #define	next_stateid(sp) (++(sp)->bits.chgseq)
917c478bd9Sstevel@tonic-gate 
927c478bd9Sstevel@tonic-gate /*
937c478bd9Sstevel@tonic-gate  * RFS4_MINLEN_ENTRY4: XDR-encoded size of smallest possible dirent.
947c478bd9Sstevel@tonic-gate  *	This is used to return NFS4ERR_TOOSMALL when clients specify
957c478bd9Sstevel@tonic-gate  *	maxcount that isn't large enough to hold the smallest possible
967c478bd9Sstevel@tonic-gate  *	XDR encoded dirent.
977c478bd9Sstevel@tonic-gate  *
987c478bd9Sstevel@tonic-gate  *	    sizeof cookie (8 bytes) +
997c478bd9Sstevel@tonic-gate  *	    sizeof name_len (4 bytes) +
1007c478bd9Sstevel@tonic-gate  *	    sizeof smallest (padded) name (4 bytes) +
1017c478bd9Sstevel@tonic-gate  *	    sizeof bitmap4_len (12 bytes) +   NOTE: we always encode len=2 bm4
1027c478bd9Sstevel@tonic-gate  *	    sizeof attrlist4_len (4 bytes) +
1037c478bd9Sstevel@tonic-gate  *	    sizeof next boolean (4 bytes)
1047c478bd9Sstevel@tonic-gate  *
1057c478bd9Sstevel@tonic-gate  * RFS4_MINLEN_RDDIR4: XDR-encoded size of READDIR op reply containing
1067c478bd9Sstevel@tonic-gate  * the smallest possible entry4 (assumes no attrs requested).
1077c478bd9Sstevel@tonic-gate  *	sizeof nfsstat4 (4 bytes) +
1087c478bd9Sstevel@tonic-gate  *	sizeof verifier4 (8 bytes) +
1097c478bd9Sstevel@tonic-gate  *	sizeof entry4list bool (4 bytes) +
1107c478bd9Sstevel@tonic-gate  *	sizeof entry4 	(36 bytes) +
1117c478bd9Sstevel@tonic-gate  *	sizeof eof bool  (4 bytes)
1127c478bd9Sstevel@tonic-gate  *
1137c478bd9Sstevel@tonic-gate  * RFS4_MINLEN_RDDIR_BUF: minimum length of buffer server will provide to
1147c478bd9Sstevel@tonic-gate  *	VOP_READDIR.  Its value is the size of the maximum possible dirent
1157c478bd9Sstevel@tonic-gate  *	for solaris.  The DIRENT64_RECLEN macro returns	the size of dirent
1167c478bd9Sstevel@tonic-gate  *	required for a given name length.  MAXNAMELEN is the maximum
1177c478bd9Sstevel@tonic-gate  *	filename length allowed in Solaris.  The first two DIRENT64_RECLEN()
1187c478bd9Sstevel@tonic-gate  *	macros are to allow for . and .. entries -- just a minor tweak to try
1197c478bd9Sstevel@tonic-gate  *	and guarantee that buffer we give to VOP_READDIR will be large enough
1207c478bd9Sstevel@tonic-gate  *	to hold ., .., and the largest possible solaris dirent64.
1217c478bd9Sstevel@tonic-gate  */
1227c478bd9Sstevel@tonic-gate #define	RFS4_MINLEN_ENTRY4 36
1237c478bd9Sstevel@tonic-gate #define	RFS4_MINLEN_RDDIR4 (4 + NFS4_VERIFIER_SIZE + 4 + RFS4_MINLEN_ENTRY4 + 4)
1247c478bd9Sstevel@tonic-gate #define	RFS4_MINLEN_RDDIR_BUF \
1257c478bd9Sstevel@tonic-gate 	(DIRENT64_RECLEN(1) + DIRENT64_RECLEN(2) + DIRENT64_RECLEN(MAXNAMELEN))
1267c478bd9Sstevel@tonic-gate 
1277c478bd9Sstevel@tonic-gate /*
1287c478bd9Sstevel@tonic-gate  * It would be better to pad to 4 bytes since that's what XDR would do,
1297c478bd9Sstevel@tonic-gate  * but the dirents UFS gives us are already padded to 8, so just take
1307c478bd9Sstevel@tonic-gate  * what we're given.  Dircount is only a hint anyway.  Currently the
1317c478bd9Sstevel@tonic-gate  * solaris kernel is ASCII only, so there's no point in calling the
1327c478bd9Sstevel@tonic-gate  * UTF8 functions.
1337c478bd9Sstevel@tonic-gate  *
1347c478bd9Sstevel@tonic-gate  * dirent64: named padded to provide 8 byte struct alignment
1357c478bd9Sstevel@tonic-gate  *	d_ino(8) + d_off(8) + d_reclen(2) + d_name(namelen + null(1) + pad)
1367c478bd9Sstevel@tonic-gate  *
1377c478bd9Sstevel@tonic-gate  * cookie: uint64_t   +  utf8namelen: uint_t  +   utf8name padded to 8 bytes
1387c478bd9Sstevel@tonic-gate  *
1397c478bd9Sstevel@tonic-gate  */
1407c478bd9Sstevel@tonic-gate #define	DIRENT64_TO_DIRCOUNT(dp) \
1417c478bd9Sstevel@tonic-gate 	(3 * BYTES_PER_XDR_UNIT + DIRENT64_NAMELEN((dp)->d_reclen))
1427c478bd9Sstevel@tonic-gate 
14345916cd2Sjpk /*
14445916cd2Sjpk  * types of label comparison
14545916cd2Sjpk  */
14645916cd2Sjpk #define	EQUALITY_CHECK	0
14745916cd2Sjpk #define	DOMINANCE_CHECK	1
14845916cd2Sjpk 
1497c478bd9Sstevel@tonic-gate time_t rfs4_start_time;			/* Initialized in rfs4_srvrinit */
1507c478bd9Sstevel@tonic-gate 
1517c478bd9Sstevel@tonic-gate static sysid_t lockt_sysid;		/* dummy sysid for all LOCKT calls */
1527c478bd9Sstevel@tonic-gate 
1537c478bd9Sstevel@tonic-gate u_longlong_t nfs4_srv_caller_id;
1547c478bd9Sstevel@tonic-gate 
1557c478bd9Sstevel@tonic-gate verifier4	Write4verf;
1567c478bd9Sstevel@tonic-gate verifier4	Readdir4verf;
1577c478bd9Sstevel@tonic-gate 
1587c478bd9Sstevel@tonic-gate void		rfs4_init_compound_state(struct compound_state *);
1597c478bd9Sstevel@tonic-gate 
1607c478bd9Sstevel@tonic-gate static void	nullfree(caddr_t);
1617c478bd9Sstevel@tonic-gate static void	rfs4_op_inval(nfs_argop4 *, nfs_resop4 *, struct svc_req *,
1627c478bd9Sstevel@tonic-gate 			struct compound_state *);
1637c478bd9Sstevel@tonic-gate static void	rfs4_op_access(nfs_argop4 *, nfs_resop4 *, struct svc_req *,
1647c478bd9Sstevel@tonic-gate 			struct compound_state *);
1657c478bd9Sstevel@tonic-gate static void	rfs4_op_close(nfs_argop4 *, nfs_resop4 *, struct svc_req *,
1667c478bd9Sstevel@tonic-gate 			struct compound_state *);
1677c478bd9Sstevel@tonic-gate static void	rfs4_op_commit(nfs_argop4 *, nfs_resop4 *, struct svc_req *,
1687c478bd9Sstevel@tonic-gate 			struct compound_state *);
1697c478bd9Sstevel@tonic-gate static void	rfs4_op_create(nfs_argop4 *, nfs_resop4 *, struct svc_req *,
1707c478bd9Sstevel@tonic-gate 			struct compound_state *);
1717c478bd9Sstevel@tonic-gate static void	rfs4_op_create_free(nfs_resop4 *resop);
1727c478bd9Sstevel@tonic-gate static void	rfs4_op_delegreturn(nfs_argop4 *, nfs_resop4 *,
1737c478bd9Sstevel@tonic-gate 				    struct svc_req *, struct compound_state *);
1747c478bd9Sstevel@tonic-gate static void	rfs4_op_getattr(nfs_argop4 *, nfs_resop4 *, struct svc_req *,
1757c478bd9Sstevel@tonic-gate 			struct compound_state *);
1767c478bd9Sstevel@tonic-gate static void	rfs4_op_getattr_free(nfs_resop4 *);
1777c478bd9Sstevel@tonic-gate static void	rfs4_op_getfh(nfs_argop4 *, nfs_resop4 *, struct svc_req *,
1787c478bd9Sstevel@tonic-gate 			struct compound_state *);
1797c478bd9Sstevel@tonic-gate static void	rfs4_op_getfh_free(nfs_resop4 *);
1807c478bd9Sstevel@tonic-gate static void	rfs4_op_illegal(nfs_argop4 *, nfs_resop4 *, struct svc_req *,
1817c478bd9Sstevel@tonic-gate 			struct compound_state *);
1827c478bd9Sstevel@tonic-gate static void	rfs4_op_link(nfs_argop4 *, nfs_resop4 *, struct svc_req *,
1837c478bd9Sstevel@tonic-gate 			struct compound_state *);
1847c478bd9Sstevel@tonic-gate static void	rfs4_op_lock(nfs_argop4 *, nfs_resop4 *, struct svc_req *,
1857c478bd9Sstevel@tonic-gate 			struct compound_state *);
1867c478bd9Sstevel@tonic-gate static void	lock_denied_free(nfs_resop4 *);
1877c478bd9Sstevel@tonic-gate static void	rfs4_op_locku(nfs_argop4 *, nfs_resop4 *, struct svc_req *,
1887c478bd9Sstevel@tonic-gate 			struct compound_state *);
1897c478bd9Sstevel@tonic-gate static void	rfs4_op_lockt(nfs_argop4 *, nfs_resop4 *, struct svc_req *,
1907c478bd9Sstevel@tonic-gate 			struct compound_state *);
1917c478bd9Sstevel@tonic-gate static void	rfs4_op_lookup(nfs_argop4 *, nfs_resop4 *, struct svc_req *,
1927c478bd9Sstevel@tonic-gate 			struct compound_state *);
1937c478bd9Sstevel@tonic-gate static void	rfs4_op_lookupp(nfs_argop4 *, nfs_resop4 *, struct svc_req *,
1947c478bd9Sstevel@tonic-gate 			struct compound_state *);
1957c478bd9Sstevel@tonic-gate static void	rfs4_op_openattr(nfs_argop4 *argop, nfs_resop4 *resop,
1967c478bd9Sstevel@tonic-gate 				struct svc_req *req, struct compound_state *cs);
1977c478bd9Sstevel@tonic-gate static void	rfs4_op_nverify(nfs_argop4 *, nfs_resop4 *, struct svc_req *,
1987c478bd9Sstevel@tonic-gate 			struct compound_state *);
1997c478bd9Sstevel@tonic-gate static void	rfs4_op_open(nfs_argop4 *, nfs_resop4 *, struct svc_req *,
2007c478bd9Sstevel@tonic-gate 			struct compound_state *);
2017c478bd9Sstevel@tonic-gate static void	rfs4_op_open_confirm(nfs_argop4 *, nfs_resop4 *,
2027c478bd9Sstevel@tonic-gate 			struct svc_req *, struct compound_state *);
2037c478bd9Sstevel@tonic-gate static void	rfs4_op_open_downgrade(nfs_argop4 *, nfs_resop4 *,
2047c478bd9Sstevel@tonic-gate 			struct svc_req *, struct compound_state *);
2057c478bd9Sstevel@tonic-gate static void	rfs4_op_putfh(nfs_argop4 *, nfs_resop4 *, struct svc_req *,
2067c478bd9Sstevel@tonic-gate 			struct compound_state *);
2077c478bd9Sstevel@tonic-gate static void	rfs4_op_putpubfh(nfs_argop4 *, nfs_resop4 *, struct svc_req *,
2087c478bd9Sstevel@tonic-gate 			struct compound_state *);
2097c478bd9Sstevel@tonic-gate static void	rfs4_op_putrootfh(nfs_argop4 *, nfs_resop4 *, struct svc_req *,
2107c478bd9Sstevel@tonic-gate 			struct compound_state *);
2117c478bd9Sstevel@tonic-gate static void	rfs4_op_read(nfs_argop4 *, nfs_resop4 *, struct svc_req *,
2127c478bd9Sstevel@tonic-gate 			struct compound_state *);
2137c478bd9Sstevel@tonic-gate static void	rfs4_op_read_free(nfs_resop4 *);
2147c478bd9Sstevel@tonic-gate static void	rfs4_op_readdir_free(nfs_resop4 *resop);
2157c478bd9Sstevel@tonic-gate static void	rfs4_op_readlink(nfs_argop4 *, nfs_resop4 *, struct svc_req *,
2167c478bd9Sstevel@tonic-gate 			struct compound_state *);
2177c478bd9Sstevel@tonic-gate static void	rfs4_op_readlink_free(nfs_resop4 *);
2187c478bd9Sstevel@tonic-gate static void	rfs4_op_release_lockowner(nfs_argop4 *, nfs_resop4 *,
2197c478bd9Sstevel@tonic-gate 			struct svc_req *, struct compound_state *);
2207c478bd9Sstevel@tonic-gate static void	rfs4_op_remove(nfs_argop4 *, nfs_resop4 *, struct svc_req *,
2217c478bd9Sstevel@tonic-gate 			struct compound_state *);
2227c478bd9Sstevel@tonic-gate static void	rfs4_op_rename(nfs_argop4 *, nfs_resop4 *, struct svc_req *,
2237c478bd9Sstevel@tonic-gate 			struct compound_state *);
2247c478bd9Sstevel@tonic-gate static void	rfs4_op_renew(nfs_argop4 *, nfs_resop4 *, struct svc_req *,
2257c478bd9Sstevel@tonic-gate 			struct compound_state *);
2267c478bd9Sstevel@tonic-gate static void	rfs4_op_restorefh(nfs_argop4 *, nfs_resop4 *, struct svc_req *,
2277c478bd9Sstevel@tonic-gate 			struct compound_state *);
2287c478bd9Sstevel@tonic-gate static void	rfs4_op_savefh(nfs_argop4 *, nfs_resop4 *, struct svc_req *,
2297c478bd9Sstevel@tonic-gate 			struct compound_state *);
2307c478bd9Sstevel@tonic-gate static void	rfs4_op_setattr(nfs_argop4 *, nfs_resop4 *, struct svc_req *,
2317c478bd9Sstevel@tonic-gate 			struct compound_state *);
2327c478bd9Sstevel@tonic-gate static void	rfs4_op_verify(nfs_argop4 *, nfs_resop4 *, struct svc_req *,
2337c478bd9Sstevel@tonic-gate 			struct compound_state *);
2347c478bd9Sstevel@tonic-gate static void	rfs4_op_write(nfs_argop4 *, nfs_resop4 *, struct svc_req *,
2357c478bd9Sstevel@tonic-gate 			struct compound_state *);
2367c478bd9Sstevel@tonic-gate static void	rfs4_op_setclientid(nfs_argop4 *, nfs_resop4 *,
2377c478bd9Sstevel@tonic-gate 			struct svc_req *, struct compound_state *);
2387c478bd9Sstevel@tonic-gate static void	rfs4_op_setclientid_confirm(nfs_argop4 *, nfs_resop4 *,
2397c478bd9Sstevel@tonic-gate 			struct svc_req *req, struct compound_state *);
2407c478bd9Sstevel@tonic-gate static void	rfs4_op_secinfo(nfs_argop4 *, nfs_resop4 *, struct svc_req *,
2417c478bd9Sstevel@tonic-gate 			struct compound_state *);
2427c478bd9Sstevel@tonic-gate static void	rfs4_op_secinfo_free(nfs_resop4 *);
2437c478bd9Sstevel@tonic-gate 
2447c478bd9Sstevel@tonic-gate static nfsstat4 check_open_access(uint32_t,
2457c478bd9Sstevel@tonic-gate 				struct compound_state *, struct svc_req *);
2467c478bd9Sstevel@tonic-gate nfsstat4 rfs4_client_sysid(rfs4_client_t *, sysid_t *);
2477c478bd9Sstevel@tonic-gate static int	vop_shrlock(vnode_t *, int, struct shrlock *, int);
2487c478bd9Sstevel@tonic-gate static int 	rfs4_shrlock(rfs4_state_t *, int);
2497c478bd9Sstevel@tonic-gate static int	rfs4_share(rfs4_state_t *);
2507c478bd9Sstevel@tonic-gate void rfs4_ss_clid(rfs4_client_t *, struct svc_req *);
2517c478bd9Sstevel@tonic-gate 
2527c478bd9Sstevel@tonic-gate /*
2537c478bd9Sstevel@tonic-gate  * translation table for attrs
2547c478bd9Sstevel@tonic-gate  */
2557c478bd9Sstevel@tonic-gate struct nfs4_ntov_table {
2567c478bd9Sstevel@tonic-gate 	union nfs4_attr_u *na;
2577c478bd9Sstevel@tonic-gate 	uint8_t amap[NFS4_MAXNUM_ATTRS];
2587c478bd9Sstevel@tonic-gate 	int attrcnt;
2597c478bd9Sstevel@tonic-gate 	bool_t vfsstat;
2607c478bd9Sstevel@tonic-gate };
2617c478bd9Sstevel@tonic-gate 
2627c478bd9Sstevel@tonic-gate static void	nfs4_ntov_table_init(struct nfs4_ntov_table *ntovp);
2637c478bd9Sstevel@tonic-gate static void	nfs4_ntov_table_free(struct nfs4_ntov_table *ntovp,
2647c478bd9Sstevel@tonic-gate 				    struct nfs4_svgetit_arg *sargp);
2657c478bd9Sstevel@tonic-gate 
2667c478bd9Sstevel@tonic-gate static nfsstat4	do_rfs4_set_attrs(bitmap4 *resp, fattr4 *fattrp,
2677c478bd9Sstevel@tonic-gate 		    struct compound_state *cs, struct nfs4_svgetit_arg *sargp,
2687c478bd9Sstevel@tonic-gate 		    struct nfs4_ntov_table *ntovp, nfs4_attr_cmd_t cmd);
2697c478bd9Sstevel@tonic-gate 
2707c478bd9Sstevel@tonic-gate fem_t	*deleg_rdops;
2717c478bd9Sstevel@tonic-gate fem_t	*deleg_wrops;
2727c478bd9Sstevel@tonic-gate 
2737c478bd9Sstevel@tonic-gate rfs4_servinst_t	*rfs4_cur_servinst = NULL;	/* current server instance */
2747c478bd9Sstevel@tonic-gate kmutex_t	rfs4_servinst_lock;		/* protects linked list */
2757c478bd9Sstevel@tonic-gate int		rfs4_seen_first_compound;	/* set first time we see one */
2767c478bd9Sstevel@tonic-gate 
2777c478bd9Sstevel@tonic-gate /*
2787c478bd9Sstevel@tonic-gate  * NFS4 op dispatch table
2797c478bd9Sstevel@tonic-gate  */
2807c478bd9Sstevel@tonic-gate 
2817c478bd9Sstevel@tonic-gate struct rfsv4disp {
2827c478bd9Sstevel@tonic-gate 	void	(*dis_proc)();		/* proc to call */
2837c478bd9Sstevel@tonic-gate 	void	(*dis_resfree)();	/* frees space allocated by proc */
2847c478bd9Sstevel@tonic-gate 	int	dis_flags;		/* RPC_IDEMPOTENT, etc... */
2857c478bd9Sstevel@tonic-gate };
2867c478bd9Sstevel@tonic-gate 
2877c478bd9Sstevel@tonic-gate static struct rfsv4disp rfsv4disptab[] = {
2887c478bd9Sstevel@tonic-gate 	/*
2897c478bd9Sstevel@tonic-gate 	 * NFS VERSION 4
2907c478bd9Sstevel@tonic-gate 	 */
2917c478bd9Sstevel@tonic-gate 
2927c478bd9Sstevel@tonic-gate 	/* RFS_NULL = 0 */
2937c478bd9Sstevel@tonic-gate 	{rfs4_op_illegal, nullfree, 0},
2947c478bd9Sstevel@tonic-gate 
2957c478bd9Sstevel@tonic-gate 	/* UNUSED = 1 */
2967c478bd9Sstevel@tonic-gate 	{rfs4_op_illegal, nullfree, 0},
2977c478bd9Sstevel@tonic-gate 
2987c478bd9Sstevel@tonic-gate 	/* UNUSED = 2 */
2997c478bd9Sstevel@tonic-gate 	{rfs4_op_illegal, nullfree, 0},
3007c478bd9Sstevel@tonic-gate 
3017c478bd9Sstevel@tonic-gate 	/* OP_ACCESS = 3 */
3027c478bd9Sstevel@tonic-gate 	{rfs4_op_access, nullfree, RPC_IDEMPOTENT},
3037c478bd9Sstevel@tonic-gate 
3047c478bd9Sstevel@tonic-gate 	/* OP_CLOSE = 4 */
3057c478bd9Sstevel@tonic-gate 	{rfs4_op_close, nullfree, 0},
3067c478bd9Sstevel@tonic-gate 
3077c478bd9Sstevel@tonic-gate 	/* OP_COMMIT = 5 */
3087c478bd9Sstevel@tonic-gate 	{rfs4_op_commit, nullfree, RPC_IDEMPOTENT},
3097c478bd9Sstevel@tonic-gate 
3107c478bd9Sstevel@tonic-gate 	/* OP_CREATE = 6 */
3117c478bd9Sstevel@tonic-gate 	{rfs4_op_create, nullfree, 0},
3127c478bd9Sstevel@tonic-gate 
3137c478bd9Sstevel@tonic-gate 	/* OP_DELEGPURGE = 7 */
3147c478bd9Sstevel@tonic-gate 	{rfs4_op_inval, nullfree, 0},
3157c478bd9Sstevel@tonic-gate 
3167c478bd9Sstevel@tonic-gate 	/* OP_DELEGRETURN = 8 */
3177c478bd9Sstevel@tonic-gate 	{rfs4_op_delegreturn, nullfree, 0},
3187c478bd9Sstevel@tonic-gate 
3197c478bd9Sstevel@tonic-gate 	/* OP_GETATTR = 9 */
3207c478bd9Sstevel@tonic-gate 	{rfs4_op_getattr, rfs4_op_getattr_free, RPC_IDEMPOTENT},
3217c478bd9Sstevel@tonic-gate 
3227c478bd9Sstevel@tonic-gate 	/* OP_GETFH = 10 */
3237c478bd9Sstevel@tonic-gate 	{rfs4_op_getfh, rfs4_op_getfh_free, RPC_ALL},
3247c478bd9Sstevel@tonic-gate 
3257c478bd9Sstevel@tonic-gate 	/* OP_LINK = 11 */
3267c478bd9Sstevel@tonic-gate 	{rfs4_op_link, nullfree, 0},
3277c478bd9Sstevel@tonic-gate 
3287c478bd9Sstevel@tonic-gate 	/* OP_LOCK = 12 */
3297c478bd9Sstevel@tonic-gate 	{rfs4_op_lock, lock_denied_free, 0},
3307c478bd9Sstevel@tonic-gate 
3317c478bd9Sstevel@tonic-gate 	/* OP_LOCKT = 13 */
3327c478bd9Sstevel@tonic-gate 	{rfs4_op_lockt, lock_denied_free, 0},
3337c478bd9Sstevel@tonic-gate 
3347c478bd9Sstevel@tonic-gate 	/* OP_LOCKU = 14 */
3357c478bd9Sstevel@tonic-gate 	{rfs4_op_locku, nullfree, 0},
3367c478bd9Sstevel@tonic-gate 
3377c478bd9Sstevel@tonic-gate 	/* OP_LOOKUP = 15 */
3387c478bd9Sstevel@tonic-gate 	{rfs4_op_lookup, nullfree, (RPC_IDEMPOTENT|RPC_PUBLICFH_OK)},
3397c478bd9Sstevel@tonic-gate 
3407c478bd9Sstevel@tonic-gate 	/* OP_LOOKUPP = 16 */
3417c478bd9Sstevel@tonic-gate 	{rfs4_op_lookupp, nullfree, (RPC_IDEMPOTENT|RPC_PUBLICFH_OK)},
3427c478bd9Sstevel@tonic-gate 
3437c478bd9Sstevel@tonic-gate 	/* OP_NVERIFY = 17 */
3447c478bd9Sstevel@tonic-gate 	{rfs4_op_nverify, nullfree, RPC_IDEMPOTENT},
3457c478bd9Sstevel@tonic-gate 
3467c478bd9Sstevel@tonic-gate 	/* OP_OPEN = 18 */
3477c478bd9Sstevel@tonic-gate 	{rfs4_op_open, rfs4_free_reply, 0},
3487c478bd9Sstevel@tonic-gate 
3497c478bd9Sstevel@tonic-gate 	/* OP_OPENATTR = 19 */
3507c478bd9Sstevel@tonic-gate 	{rfs4_op_openattr, nullfree, 0},
3517c478bd9Sstevel@tonic-gate 
3527c478bd9Sstevel@tonic-gate 	/* OP_OPEN_CONFIRM = 20 */
3537c478bd9Sstevel@tonic-gate 	{rfs4_op_open_confirm, nullfree, 0},
3547c478bd9Sstevel@tonic-gate 
3557c478bd9Sstevel@tonic-gate 	/* OP_OPEN_DOWNGRADE = 21 */
3567c478bd9Sstevel@tonic-gate 	{rfs4_op_open_downgrade, nullfree, 0},
3577c478bd9Sstevel@tonic-gate 
3587c478bd9Sstevel@tonic-gate 	/* OP_OPEN_PUTFH = 22 */
3597c478bd9Sstevel@tonic-gate 	{rfs4_op_putfh, nullfree, RPC_ALL},
3607c478bd9Sstevel@tonic-gate 
3617c478bd9Sstevel@tonic-gate 	/* OP_PUTPUBFH = 23 */
3627c478bd9Sstevel@tonic-gate 	{rfs4_op_putpubfh, nullfree, RPC_ALL},
3637c478bd9Sstevel@tonic-gate 
3647c478bd9Sstevel@tonic-gate 	/* OP_PUTROOTFH = 24 */
3657c478bd9Sstevel@tonic-gate 	{rfs4_op_putrootfh, nullfree, RPC_ALL},
3667c478bd9Sstevel@tonic-gate 
3677c478bd9Sstevel@tonic-gate 	/* OP_READ = 25 */
3687c478bd9Sstevel@tonic-gate 	{rfs4_op_read, rfs4_op_read_free, RPC_IDEMPOTENT},
3697c478bd9Sstevel@tonic-gate 
3707c478bd9Sstevel@tonic-gate 	/* OP_READDIR = 26 */
3717c478bd9Sstevel@tonic-gate 	{rfs4_op_readdir, rfs4_op_readdir_free, RPC_IDEMPOTENT},
3727c478bd9Sstevel@tonic-gate 
3737c478bd9Sstevel@tonic-gate 	/* OP_READLINK = 27 */
3747c478bd9Sstevel@tonic-gate 	{rfs4_op_readlink, rfs4_op_readlink_free, RPC_IDEMPOTENT},
3757c478bd9Sstevel@tonic-gate 
3767c478bd9Sstevel@tonic-gate 	/* OP_REMOVE = 28 */
3777c478bd9Sstevel@tonic-gate 	{rfs4_op_remove, nullfree, 0},
3787c478bd9Sstevel@tonic-gate 
3797c478bd9Sstevel@tonic-gate 	/* OP_RENAME = 29 */
3807c478bd9Sstevel@tonic-gate 	{rfs4_op_rename, nullfree, 0},
3817c478bd9Sstevel@tonic-gate 
3827c478bd9Sstevel@tonic-gate 	/* OP_RENEW = 30 */
3837c478bd9Sstevel@tonic-gate 	{rfs4_op_renew, nullfree, 0},
3847c478bd9Sstevel@tonic-gate 
3857c478bd9Sstevel@tonic-gate 	/* OP_RESTOREFH = 31 */
3867c478bd9Sstevel@tonic-gate 	{rfs4_op_restorefh, nullfree, RPC_ALL},
3877c478bd9Sstevel@tonic-gate 
3887c478bd9Sstevel@tonic-gate 	/* OP_SAVEFH = 32 */
3897c478bd9Sstevel@tonic-gate 	{rfs4_op_savefh, nullfree, RPC_ALL},
3907c478bd9Sstevel@tonic-gate 
3917c478bd9Sstevel@tonic-gate 	/* OP_SECINFO = 33 */
3927c478bd9Sstevel@tonic-gate 	{rfs4_op_secinfo, rfs4_op_secinfo_free, 0},
3937c478bd9Sstevel@tonic-gate 
3947c478bd9Sstevel@tonic-gate 	/* OP_SETATTR = 34 */
3957c478bd9Sstevel@tonic-gate 	{rfs4_op_setattr, nullfree, 0},
3967c478bd9Sstevel@tonic-gate 
3977c478bd9Sstevel@tonic-gate 	/* OP_SETCLIENTID = 35 */
3987c478bd9Sstevel@tonic-gate 	{rfs4_op_setclientid, nullfree, 0},
3997c478bd9Sstevel@tonic-gate 
4007c478bd9Sstevel@tonic-gate 	/* OP_SETCLIENTID_CONFIRM = 36 */
4017c478bd9Sstevel@tonic-gate 	{rfs4_op_setclientid_confirm, nullfree, 0},
4027c478bd9Sstevel@tonic-gate 
4037c478bd9Sstevel@tonic-gate 	/* OP_VERIFY = 37 */
4047c478bd9Sstevel@tonic-gate 	{rfs4_op_verify, nullfree, RPC_IDEMPOTENT},
4057c478bd9Sstevel@tonic-gate 
4067c478bd9Sstevel@tonic-gate 	/* OP_WRITE = 38 */
4077c478bd9Sstevel@tonic-gate 	{rfs4_op_write, nullfree, 0},
4087c478bd9Sstevel@tonic-gate 
4097c478bd9Sstevel@tonic-gate 	/* OP_RELEASE_LOCKOWNER = 39 */
4107c478bd9Sstevel@tonic-gate 	{rfs4_op_release_lockowner, nullfree, 0},
4117c478bd9Sstevel@tonic-gate };
4127c478bd9Sstevel@tonic-gate 
4137c478bd9Sstevel@tonic-gate static uint_t rfsv4disp_cnt = sizeof (rfsv4disptab) / sizeof (rfsv4disptab[0]);
4147c478bd9Sstevel@tonic-gate 
4157c478bd9Sstevel@tonic-gate #define	OP_ILLEGAL_IDX (rfsv4disp_cnt)
4167c478bd9Sstevel@tonic-gate 
4177c478bd9Sstevel@tonic-gate #ifdef DEBUG
4187c478bd9Sstevel@tonic-gate 
4197c478bd9Sstevel@tonic-gate int rfs4_fillone_debug = 0;
4207c478bd9Sstevel@tonic-gate int rfs4_shrlock_debug = 0;
4217c478bd9Sstevel@tonic-gate int rfs4_no_stub_access = 1;
4227c478bd9Sstevel@tonic-gate int rfs4_rddir_debug = 0;
4237c478bd9Sstevel@tonic-gate 
4247c478bd9Sstevel@tonic-gate static char *rfs4_op_string[] = {
4257c478bd9Sstevel@tonic-gate 	"rfs4_op_null",
4267c478bd9Sstevel@tonic-gate 	"rfs4_op_1 unused",
4277c478bd9Sstevel@tonic-gate 	"rfs4_op_2 unused",
4287c478bd9Sstevel@tonic-gate 	"rfs4_op_access",
4297c478bd9Sstevel@tonic-gate 	"rfs4_op_close",
4307c478bd9Sstevel@tonic-gate 	"rfs4_op_commit",
4317c478bd9Sstevel@tonic-gate 	"rfs4_op_create",
4327c478bd9Sstevel@tonic-gate 	"rfs4_op_delegpurge",
4337c478bd9Sstevel@tonic-gate 	"rfs4_op_delegreturn",
4347c478bd9Sstevel@tonic-gate 	"rfs4_op_getattr",
4357c478bd9Sstevel@tonic-gate 	"rfs4_op_getfh",
4367c478bd9Sstevel@tonic-gate 	"rfs4_op_link",
4377c478bd9Sstevel@tonic-gate 	"rfs4_op_lock",
4387c478bd9Sstevel@tonic-gate 	"rfs4_op_lockt",
4397c478bd9Sstevel@tonic-gate 	"rfs4_op_locku",
4407c478bd9Sstevel@tonic-gate 	"rfs4_op_lookup",
4417c478bd9Sstevel@tonic-gate 	"rfs4_op_lookupp",
4427c478bd9Sstevel@tonic-gate 	"rfs4_op_nverify",
4437c478bd9Sstevel@tonic-gate 	"rfs4_op_open",
4447c478bd9Sstevel@tonic-gate 	"rfs4_op_openattr",
4457c478bd9Sstevel@tonic-gate 	"rfs4_op_open_confirm",
4467c478bd9Sstevel@tonic-gate 	"rfs4_op_open_downgrade",
4477c478bd9Sstevel@tonic-gate 	"rfs4_op_putfh",
4487c478bd9Sstevel@tonic-gate 	"rfs4_op_putpubfh",
4497c478bd9Sstevel@tonic-gate 	"rfs4_op_putrootfh",
4507c478bd9Sstevel@tonic-gate 	"rfs4_op_read",
4517c478bd9Sstevel@tonic-gate 	"rfs4_op_readdir",
4527c478bd9Sstevel@tonic-gate 	"rfs4_op_readlink",
4537c478bd9Sstevel@tonic-gate 	"rfs4_op_remove",
4547c478bd9Sstevel@tonic-gate 	"rfs4_op_rename",
4557c478bd9Sstevel@tonic-gate 	"rfs4_op_renew",
4567c478bd9Sstevel@tonic-gate 	"rfs4_op_restorefh",
4577c478bd9Sstevel@tonic-gate 	"rfs4_op_savefh",
4587c478bd9Sstevel@tonic-gate 	"rfs4_op_secinfo",
4597c478bd9Sstevel@tonic-gate 	"rfs4_op_setattr",
4607c478bd9Sstevel@tonic-gate 	"rfs4_op_setclientid",
4617c478bd9Sstevel@tonic-gate 	"rfs4_op_setclient_confirm",
4627c478bd9Sstevel@tonic-gate 	"rfs4_op_verify",
4637c478bd9Sstevel@tonic-gate 	"rfs4_op_write",
4647c478bd9Sstevel@tonic-gate 	"rfs4_op_release_lockowner",
4657c478bd9Sstevel@tonic-gate 	"rfs4_op_illegal"
4667c478bd9Sstevel@tonic-gate };
4677c478bd9Sstevel@tonic-gate #endif
4687c478bd9Sstevel@tonic-gate 
4697c478bd9Sstevel@tonic-gate void rfs4_ss_chkclid(rfs4_client_t *);
4707c478bd9Sstevel@tonic-gate 
471cee86682Scalum extern size_t strlcpy(char *dst, const char *src, size_t dstsize);
472cee86682Scalum 
4737c478bd9Sstevel@tonic-gate #ifdef	nextdp
4747c478bd9Sstevel@tonic-gate #undef nextdp
4757c478bd9Sstevel@tonic-gate #endif
4767c478bd9Sstevel@tonic-gate #define	nextdp(dp)	((struct dirent64 *)((char *)(dp) + (dp)->d_reclen))
4777c478bd9Sstevel@tonic-gate 
4787c478bd9Sstevel@tonic-gate static const fs_operation_def_t nfs4_rd_deleg_tmpl[] = {
479*aa59c4cbSrsb 	VOPNAME_OPEN,		{ .femop_open = deleg_rdopen },
480*aa59c4cbSrsb 	VOPNAME_WRITE,		{ .femop_write = deleg_write },
481*aa59c4cbSrsb 	VOPNAME_SETATTR,	{ .femop_setattr = deleg_setattr },
482*aa59c4cbSrsb 	VOPNAME_RWLOCK,		{ .femop_rwlock = deleg_rd_rwlock },
483*aa59c4cbSrsb 	VOPNAME_SPACE,		{ .femop_space = deleg_space },
484*aa59c4cbSrsb 	VOPNAME_SETSECATTR,	{ .femop_setsecattr = deleg_setsecattr },
485*aa59c4cbSrsb 	VOPNAME_VNEVENT,	{ .femop_vnevent = deleg_vnevent },
486*aa59c4cbSrsb 	NULL,			NULL
4877c478bd9Sstevel@tonic-gate };
4887c478bd9Sstevel@tonic-gate static const fs_operation_def_t nfs4_wr_deleg_tmpl[] = {
489*aa59c4cbSrsb 	VOPNAME_OPEN,		{ .femop_open = deleg_wropen },
490*aa59c4cbSrsb 	VOPNAME_READ,		{ .femop_read = deleg_read },
491*aa59c4cbSrsb 	VOPNAME_WRITE,		{ .femop_write = deleg_write },
492*aa59c4cbSrsb 	VOPNAME_SETATTR,	{ .femop_setattr = deleg_setattr },
493*aa59c4cbSrsb 	VOPNAME_RWLOCK,		{ .femop_rwlock = deleg_wr_rwlock },
494*aa59c4cbSrsb 	VOPNAME_SPACE,		{ .femop_space = deleg_space },
495*aa59c4cbSrsb 	VOPNAME_SETSECATTR,	{ .femop_setsecattr = deleg_setsecattr },
496*aa59c4cbSrsb 	VOPNAME_VNEVENT,	{ .femop_vnevent = deleg_vnevent },
497*aa59c4cbSrsb 	NULL,			NULL
4987c478bd9Sstevel@tonic-gate };
4997c478bd9Sstevel@tonic-gate 
5007c478bd9Sstevel@tonic-gate int
5017c478bd9Sstevel@tonic-gate rfs4_srvrinit(void)
5027c478bd9Sstevel@tonic-gate {
5037c478bd9Sstevel@tonic-gate 	timespec32_t verf;
5047c478bd9Sstevel@tonic-gate 	int error;
5057c478bd9Sstevel@tonic-gate 	extern void rfs4_attr_init();
5067c478bd9Sstevel@tonic-gate 	extern krwlock_t rfs4_deleg_policy_lock;
5077c478bd9Sstevel@tonic-gate 
5087c478bd9Sstevel@tonic-gate 	/*
5097c478bd9Sstevel@tonic-gate 	 * The following algorithm attempts to find a unique verifier
5107c478bd9Sstevel@tonic-gate 	 * to be used as the write verifier returned from the server
5117c478bd9Sstevel@tonic-gate 	 * to the client.  It is important that this verifier change
5127c478bd9Sstevel@tonic-gate 	 * whenever the server reboots.  Of secondary importance, it
5137c478bd9Sstevel@tonic-gate 	 * is important for the verifier to be unique between two
5147c478bd9Sstevel@tonic-gate 	 * different servers.
5157c478bd9Sstevel@tonic-gate 	 *
5167c478bd9Sstevel@tonic-gate 	 * Thus, an attempt is made to use the system hostid and the
5177c478bd9Sstevel@tonic-gate 	 * current time in seconds when the nfssrv kernel module is
5187c478bd9Sstevel@tonic-gate 	 * loaded.  It is assumed that an NFS server will not be able
5197c478bd9Sstevel@tonic-gate 	 * to boot and then to reboot in less than a second.  If the
5207c478bd9Sstevel@tonic-gate 	 * hostid has not been set, then the current high resolution
5217c478bd9Sstevel@tonic-gate 	 * time is used.  This will ensure different verifiers each
5227c478bd9Sstevel@tonic-gate 	 * time the server reboots and minimize the chances that two
5237c478bd9Sstevel@tonic-gate 	 * different servers will have the same verifier.
5247c478bd9Sstevel@tonic-gate 	 * XXX - this is broken on LP64 kernels.
5257c478bd9Sstevel@tonic-gate 	 */
5267c478bd9Sstevel@tonic-gate 	verf.tv_sec = (time_t)nfs_atoi(hw_serial);
5277c478bd9Sstevel@tonic-gate 	if (verf.tv_sec != 0) {
5287c478bd9Sstevel@tonic-gate 		verf.tv_nsec = gethrestime_sec();
5297c478bd9Sstevel@tonic-gate 	} else {
5307c478bd9Sstevel@tonic-gate 		timespec_t tverf;
5317c478bd9Sstevel@tonic-gate 
5327c478bd9Sstevel@tonic-gate 		gethrestime(&tverf);
5337c478bd9Sstevel@tonic-gate 		verf.tv_sec = (time_t)tverf.tv_sec;
5347c478bd9Sstevel@tonic-gate 		verf.tv_nsec = tverf.tv_nsec;
5357c478bd9Sstevel@tonic-gate 	}
5367c478bd9Sstevel@tonic-gate 
5377c478bd9Sstevel@tonic-gate 	Write4verf = *(uint64_t *)&verf;
5387c478bd9Sstevel@tonic-gate 
5397c478bd9Sstevel@tonic-gate 	rfs4_attr_init();
5407c478bd9Sstevel@tonic-gate 	mutex_init(&rfs4_deleg_lock, NULL, MUTEX_DEFAULT, NULL);
5417c478bd9Sstevel@tonic-gate 
5427c478bd9Sstevel@tonic-gate 	/* Used to manage create/destroy of server state */
5437c478bd9Sstevel@tonic-gate 	mutex_init(&rfs4_state_lock, NULL, MUTEX_DEFAULT, NULL);
5447c478bd9Sstevel@tonic-gate 
5457c478bd9Sstevel@tonic-gate 	/* Used to manage access to server instance linked list */
5467c478bd9Sstevel@tonic-gate 	mutex_init(&rfs4_servinst_lock, NULL, MUTEX_DEFAULT, NULL);
5477c478bd9Sstevel@tonic-gate 
5487c478bd9Sstevel@tonic-gate 	/* Used to manage access to rfs4_deleg_policy */
5497c478bd9Sstevel@tonic-gate 	rw_init(&rfs4_deleg_policy_lock, NULL, RW_DEFAULT, NULL);
5507c478bd9Sstevel@tonic-gate 
5517c478bd9Sstevel@tonic-gate 	error = fem_create("deleg_rdops", nfs4_rd_deleg_tmpl, &deleg_rdops);
5527c478bd9Sstevel@tonic-gate 	if (error != 0) {
5537c478bd9Sstevel@tonic-gate 		rfs4_disable_delegation();
5547c478bd9Sstevel@tonic-gate 	} else {
5557c478bd9Sstevel@tonic-gate 		error = fem_create("deleg_wrops", nfs4_wr_deleg_tmpl,
5567c478bd9Sstevel@tonic-gate 				&deleg_wrops);
5577c478bd9Sstevel@tonic-gate 		if (error != 0) {
5587c478bd9Sstevel@tonic-gate 			rfs4_disable_delegation();
5597c478bd9Sstevel@tonic-gate 			fem_free(deleg_rdops);
5607c478bd9Sstevel@tonic-gate 		}
5617c478bd9Sstevel@tonic-gate 	}
5627c478bd9Sstevel@tonic-gate 
5637c478bd9Sstevel@tonic-gate 	nfs4_srv_caller_id = fs_new_caller_id();
5647c478bd9Sstevel@tonic-gate 
5657c478bd9Sstevel@tonic-gate 	lockt_sysid = lm_alloc_sysidt();
5667c478bd9Sstevel@tonic-gate 
5677c478bd9Sstevel@tonic-gate 	return (0);
5687c478bd9Sstevel@tonic-gate }
5697c478bd9Sstevel@tonic-gate 
5707c478bd9Sstevel@tonic-gate void
5717c478bd9Sstevel@tonic-gate rfs4_srvrfini(void)
5727c478bd9Sstevel@tonic-gate {
5737c478bd9Sstevel@tonic-gate 	extern krwlock_t rfs4_deleg_policy_lock;
5747c478bd9Sstevel@tonic-gate 
5757c478bd9Sstevel@tonic-gate 	if (lockt_sysid != LM_NOSYSID) {
5767c478bd9Sstevel@tonic-gate 		lm_free_sysidt(lockt_sysid);
5777c478bd9Sstevel@tonic-gate 		lockt_sysid = LM_NOSYSID;
5787c478bd9Sstevel@tonic-gate 	}
5797c478bd9Sstevel@tonic-gate 
5807c478bd9Sstevel@tonic-gate 	mutex_destroy(&rfs4_deleg_lock);
5817c478bd9Sstevel@tonic-gate 	mutex_destroy(&rfs4_state_lock);
5827c478bd9Sstevel@tonic-gate 	rw_destroy(&rfs4_deleg_policy_lock);
5837c478bd9Sstevel@tonic-gate 
5847c478bd9Sstevel@tonic-gate 	fem_free(deleg_rdops);
5857c478bd9Sstevel@tonic-gate 	fem_free(deleg_wrops);
5867c478bd9Sstevel@tonic-gate }
5877c478bd9Sstevel@tonic-gate 
5887c478bd9Sstevel@tonic-gate void
5897c478bd9Sstevel@tonic-gate rfs4_init_compound_state(struct compound_state *cs)
5907c478bd9Sstevel@tonic-gate {
5917c478bd9Sstevel@tonic-gate 	bzero(cs, sizeof (*cs));
5927c478bd9Sstevel@tonic-gate 	cs->cont = TRUE;
5937c478bd9Sstevel@tonic-gate 	cs->access = CS_ACCESS_DENIED;
5947c478bd9Sstevel@tonic-gate 	cs->deleg = FALSE;
5957c478bd9Sstevel@tonic-gate 	cs->mandlock = FALSE;
5967c478bd9Sstevel@tonic-gate 	cs->fh.nfs_fh4_val = cs->fhbuf;
5977c478bd9Sstevel@tonic-gate }
5987c478bd9Sstevel@tonic-gate 
5997c478bd9Sstevel@tonic-gate void
6007c478bd9Sstevel@tonic-gate rfs4_grace_start(rfs4_servinst_t *sip)
6017c478bd9Sstevel@tonic-gate {
6027c478bd9Sstevel@tonic-gate 	time_t now = gethrestime_sec();
6037c478bd9Sstevel@tonic-gate 
6047c478bd9Sstevel@tonic-gate 	rw_enter(&sip->rwlock, RW_WRITER);
6057c478bd9Sstevel@tonic-gate 	sip->start_time = now;
6067c478bd9Sstevel@tonic-gate 	sip->grace_period = rfs4_grace_period;
6077c478bd9Sstevel@tonic-gate 	rw_exit(&sip->rwlock);
6087c478bd9Sstevel@tonic-gate }
6097c478bd9Sstevel@tonic-gate 
6107c478bd9Sstevel@tonic-gate /*
6117c478bd9Sstevel@tonic-gate  * returns true if the instance's grace period has never been started
6127c478bd9Sstevel@tonic-gate  */
6137c478bd9Sstevel@tonic-gate int
6147c478bd9Sstevel@tonic-gate rfs4_servinst_grace_new(rfs4_servinst_t *sip)
6157c478bd9Sstevel@tonic-gate {
6167c478bd9Sstevel@tonic-gate 	time_t start_time;
6177c478bd9Sstevel@tonic-gate 
6187c478bd9Sstevel@tonic-gate 	rw_enter(&sip->rwlock, RW_READER);
6197c478bd9Sstevel@tonic-gate 	start_time = sip->start_time;
6207c478bd9Sstevel@tonic-gate 	rw_exit(&sip->rwlock);
6217c478bd9Sstevel@tonic-gate 
6227c478bd9Sstevel@tonic-gate 	return (start_time == 0);
6237c478bd9Sstevel@tonic-gate }
6247c478bd9Sstevel@tonic-gate 
6257c478bd9Sstevel@tonic-gate /*
6267c478bd9Sstevel@tonic-gate  * Indicates if server instance is within the
6277c478bd9Sstevel@tonic-gate  * grace period.
6287c478bd9Sstevel@tonic-gate  */
6297c478bd9Sstevel@tonic-gate int
6307c478bd9Sstevel@tonic-gate rfs4_servinst_in_grace(rfs4_servinst_t *sip)
6317c478bd9Sstevel@tonic-gate {
6327c478bd9Sstevel@tonic-gate 	time_t grace_expiry;
6337c478bd9Sstevel@tonic-gate 
6347c478bd9Sstevel@tonic-gate 	rw_enter(&sip->rwlock, RW_READER);
6357c478bd9Sstevel@tonic-gate 	grace_expiry = sip->start_time + sip->grace_period;
6367c478bd9Sstevel@tonic-gate 	rw_exit(&sip->rwlock);
6377c478bd9Sstevel@tonic-gate 
6387c478bd9Sstevel@tonic-gate 	return (gethrestime_sec() < grace_expiry);
6397c478bd9Sstevel@tonic-gate }
6407c478bd9Sstevel@tonic-gate 
6417c478bd9Sstevel@tonic-gate int
6427c478bd9Sstevel@tonic-gate rfs4_clnt_in_grace(rfs4_client_t *cp)
6437c478bd9Sstevel@tonic-gate {
6447c478bd9Sstevel@tonic-gate 	ASSERT(rfs4_dbe_refcnt(cp->dbe) > 0);
6457c478bd9Sstevel@tonic-gate 
6467c478bd9Sstevel@tonic-gate 	return (rfs4_servinst_in_grace(cp->server_instance));
6477c478bd9Sstevel@tonic-gate }
6487c478bd9Sstevel@tonic-gate 
6497c478bd9Sstevel@tonic-gate /*
6507c478bd9Sstevel@tonic-gate  * reset all currently active grace periods
6517c478bd9Sstevel@tonic-gate  */
6527c478bd9Sstevel@tonic-gate void
6537c478bd9Sstevel@tonic-gate rfs4_grace_reset_all(void)
6547c478bd9Sstevel@tonic-gate {
6557c478bd9Sstevel@tonic-gate 	rfs4_servinst_t *sip;
6567c478bd9Sstevel@tonic-gate 
6577c478bd9Sstevel@tonic-gate 	mutex_enter(&rfs4_servinst_lock);
658cee86682Scalum 	for (sip = rfs4_cur_servinst; sip != NULL; sip = sip->prev)
659cee86682Scalum 		if (rfs4_servinst_in_grace(sip))
6607c478bd9Sstevel@tonic-gate 			rfs4_grace_start(sip);
6617c478bd9Sstevel@tonic-gate 	mutex_exit(&rfs4_servinst_lock);
6627c478bd9Sstevel@tonic-gate }
6637c478bd9Sstevel@tonic-gate 
6647c478bd9Sstevel@tonic-gate /*
6657c478bd9Sstevel@tonic-gate  * start any new instances' grace periods
6667c478bd9Sstevel@tonic-gate  */
6677c478bd9Sstevel@tonic-gate void
6687c478bd9Sstevel@tonic-gate rfs4_grace_start_new(void)
6697c478bd9Sstevel@tonic-gate {
6707c478bd9Sstevel@tonic-gate 	rfs4_servinst_t *sip;
6717c478bd9Sstevel@tonic-gate 
6727c478bd9Sstevel@tonic-gate 	mutex_enter(&rfs4_servinst_lock);
673cee86682Scalum 	for (sip = rfs4_cur_servinst; sip != NULL; sip = sip->prev)
6747c478bd9Sstevel@tonic-gate 		if (rfs4_servinst_grace_new(sip))
6757c478bd9Sstevel@tonic-gate 			rfs4_grace_start(sip);
6767c478bd9Sstevel@tonic-gate 	mutex_exit(&rfs4_servinst_lock);
677cee86682Scalum }
678cee86682Scalum 
679cee86682Scalum static rfs4_dss_path_t *
680cee86682Scalum rfs4_dss_newpath(rfs4_servinst_t *sip, char *path, unsigned index)
681cee86682Scalum {
682cee86682Scalum 	size_t len;
683cee86682Scalum 	rfs4_dss_path_t *dss_path;
684cee86682Scalum 
685cee86682Scalum 	dss_path = kmem_alloc(sizeof (rfs4_dss_path_t), KM_SLEEP);
686cee86682Scalum 
687cee86682Scalum 	/*
688cee86682Scalum 	 * Take a copy of the string, since the original may be overwritten.
689cee86682Scalum 	 * Sadly, no strdup() in the kernel.
690cee86682Scalum 	 */
691cee86682Scalum 	/* allow for NUL */
692cee86682Scalum 	len = strlen(path) + 1;
693cee86682Scalum 	dss_path->path = kmem_alloc(len, KM_SLEEP);
694cee86682Scalum 	(void) strlcpy(dss_path->path, path, len);
695cee86682Scalum 
696cee86682Scalum 	/* associate with servinst */
697cee86682Scalum 	dss_path->sip = sip;
698cee86682Scalum 	dss_path->index = index;
699cee86682Scalum 
700cee86682Scalum 	/*
701cee86682Scalum 	 * Add to list of served paths.
702cee86682Scalum 	 * No locking required, as we're only ever called at startup.
703cee86682Scalum 	 */
704cee86682Scalum 	if (rfs4_dss_pathlist == NULL) {
705cee86682Scalum 		/* this is the first dss_path_t */
7067c478bd9Sstevel@tonic-gate 
707cee86682Scalum 		/* needed for insque/remque */
708cee86682Scalum 		dss_path->next = dss_path->prev = dss_path;
709cee86682Scalum 
710cee86682Scalum 		rfs4_dss_pathlist = dss_path;
711cee86682Scalum 	} else {
712cee86682Scalum 		insque(dss_path, rfs4_dss_pathlist);
713cee86682Scalum 	}
714cee86682Scalum 
715cee86682Scalum 	return (dss_path);
7167c478bd9Sstevel@tonic-gate }
7177c478bd9Sstevel@tonic-gate 
7187c478bd9Sstevel@tonic-gate /*
7197c478bd9Sstevel@tonic-gate  * Create a new server instance, and make it the currently active instance.
7207c478bd9Sstevel@tonic-gate  * Note that starting the grace period too early will reduce the clients'
7217c478bd9Sstevel@tonic-gate  * recovery window.
7227c478bd9Sstevel@tonic-gate  */
7237c478bd9Sstevel@tonic-gate void
724cee86682Scalum rfs4_servinst_create(int start_grace, int dss_npaths, char **dss_paths)
7257c478bd9Sstevel@tonic-gate {
726cee86682Scalum 	unsigned i;
7277c478bd9Sstevel@tonic-gate 	rfs4_servinst_t *sip;
728cee86682Scalum 	rfs4_oldstate_t *oldstate;
7297c478bd9Sstevel@tonic-gate 
7307c478bd9Sstevel@tonic-gate 	sip = kmem_alloc(sizeof (rfs4_servinst_t), KM_SLEEP);
7317c478bd9Sstevel@tonic-gate 	rw_init(&sip->rwlock, NULL, RW_DEFAULT, NULL);
7327c478bd9Sstevel@tonic-gate 
7337c478bd9Sstevel@tonic-gate 	sip->start_time = (time_t)0;
7347c478bd9Sstevel@tonic-gate 	sip->grace_period = (time_t)0;
7357c478bd9Sstevel@tonic-gate 	sip->next = NULL;
7367c478bd9Sstevel@tonic-gate 	sip->prev = NULL;
7377c478bd9Sstevel@tonic-gate 
738cee86682Scalum 	rw_init(&sip->oldstate_lock, NULL, RW_DEFAULT, NULL);
739cee86682Scalum 	/*
740cee86682Scalum 	 * This initial dummy entry is required to setup for insque/remque.
741cee86682Scalum 	 * It must be skipped over whenever the list is traversed.
742cee86682Scalum 	 */
743cee86682Scalum 	oldstate = kmem_alloc(sizeof (rfs4_oldstate_t), KM_SLEEP);
744cee86682Scalum 	/* insque/remque require initial list entry to be self-terminated */
745cee86682Scalum 	oldstate->next = oldstate;
746cee86682Scalum 	oldstate->prev = oldstate;
747cee86682Scalum 	sip->oldstate = oldstate;
748cee86682Scalum 
749cee86682Scalum 
750cee86682Scalum 	sip->dss_npaths = dss_npaths;
751cee86682Scalum 	sip->dss_paths = kmem_alloc(dss_npaths *
752cee86682Scalum 	    sizeof (rfs4_dss_path_t *), KM_SLEEP);
753cee86682Scalum 
754cee86682Scalum 	for (i = 0; i < dss_npaths; i++) {
755cee86682Scalum 		sip->dss_paths[i] = rfs4_dss_newpath(sip, dss_paths[i], i);
756cee86682Scalum 	}
757cee86682Scalum 
7587c478bd9Sstevel@tonic-gate 	mutex_enter(&rfs4_servinst_lock);
759cee86682Scalum 	if (rfs4_cur_servinst != NULL) {
7607c478bd9Sstevel@tonic-gate 		/* add to linked list */
7617c478bd9Sstevel@tonic-gate 		sip->prev = rfs4_cur_servinst;
7627c478bd9Sstevel@tonic-gate 		rfs4_cur_servinst->next = sip;
7637c478bd9Sstevel@tonic-gate 	}
7647c478bd9Sstevel@tonic-gate 	if (start_grace)
7657c478bd9Sstevel@tonic-gate 		rfs4_grace_start(sip);
7667c478bd9Sstevel@tonic-gate 	/* make the new instance "current" */
7677c478bd9Sstevel@tonic-gate 	rfs4_cur_servinst = sip;
7687c478bd9Sstevel@tonic-gate 
769cee86682Scalum 	mutex_exit(&rfs4_servinst_lock);
7707c478bd9Sstevel@tonic-gate }
7717c478bd9Sstevel@tonic-gate 
7727c478bd9Sstevel@tonic-gate /*
7737c478bd9Sstevel@tonic-gate  * In future, we might add a rfs4_servinst_destroy(sip) but, for now, destroy
7747c478bd9Sstevel@tonic-gate  * all instances directly.
7757c478bd9Sstevel@tonic-gate  */
7767c478bd9Sstevel@tonic-gate void
7777c478bd9Sstevel@tonic-gate rfs4_servinst_destroy_all(void)
7787c478bd9Sstevel@tonic-gate {
7797c478bd9Sstevel@tonic-gate 	rfs4_servinst_t *sip, *prev, *current;
7807c478bd9Sstevel@tonic-gate #ifdef DEBUG
7817c478bd9Sstevel@tonic-gate 	int n = 0;
7827c478bd9Sstevel@tonic-gate #endif
7837c478bd9Sstevel@tonic-gate 
7847c478bd9Sstevel@tonic-gate 	mutex_enter(&rfs4_servinst_lock);
7857c478bd9Sstevel@tonic-gate 	ASSERT(rfs4_cur_servinst != NULL);
7867c478bd9Sstevel@tonic-gate 	current = rfs4_cur_servinst;
7877c478bd9Sstevel@tonic-gate 	rfs4_cur_servinst = NULL;
7887c478bd9Sstevel@tonic-gate 	for (sip = current; sip != NULL; sip = prev) {
7897c478bd9Sstevel@tonic-gate 		prev = sip->prev;
7907c478bd9Sstevel@tonic-gate 		rw_destroy(&sip->rwlock);
791cee86682Scalum 		if (sip->oldstate)
792cee86682Scalum 			kmem_free(sip->oldstate, sizeof (rfs4_oldstate_t));
793cee86682Scalum 		if (sip->dss_paths)
794cee86682Scalum 			kmem_free(sip->dss_paths,
795cee86682Scalum 			    sip->dss_npaths * sizeof (rfs4_dss_path_t *));
7967c478bd9Sstevel@tonic-gate 		kmem_free(sip, sizeof (rfs4_servinst_t));
7977c478bd9Sstevel@tonic-gate #ifdef DEBUG
7987c478bd9Sstevel@tonic-gate 		n++;
7997c478bd9Sstevel@tonic-gate #endif
8007c478bd9Sstevel@tonic-gate 	}
8017c478bd9Sstevel@tonic-gate 	mutex_exit(&rfs4_servinst_lock);
8027c478bd9Sstevel@tonic-gate }
8037c478bd9Sstevel@tonic-gate 
8047c478bd9Sstevel@tonic-gate /*
8057c478bd9Sstevel@tonic-gate  * Assign the current server instance to a client_t.
8067c478bd9Sstevel@tonic-gate  * Should be called with cp->dbe held.
8077c478bd9Sstevel@tonic-gate  */
8087c478bd9Sstevel@tonic-gate void
8097c478bd9Sstevel@tonic-gate rfs4_servinst_assign(rfs4_client_t *cp, rfs4_servinst_t *sip)
8107c478bd9Sstevel@tonic-gate {
8117c478bd9Sstevel@tonic-gate 	ASSERT(rfs4_dbe_refcnt(cp->dbe) > 0);
8127c478bd9Sstevel@tonic-gate 
8137c478bd9Sstevel@tonic-gate 	/*
8147c478bd9Sstevel@tonic-gate 	 * The lock ensures that if the current instance is in the process
8157c478bd9Sstevel@tonic-gate 	 * of changing, we will see the new one.
8167c478bd9Sstevel@tonic-gate 	 */
8177c478bd9Sstevel@tonic-gate 	mutex_enter(&rfs4_servinst_lock);
8187c478bd9Sstevel@tonic-gate 	cp->server_instance = sip;
8197c478bd9Sstevel@tonic-gate 	mutex_exit(&rfs4_servinst_lock);
8207c478bd9Sstevel@tonic-gate }
8217c478bd9Sstevel@tonic-gate 
8227c478bd9Sstevel@tonic-gate rfs4_servinst_t *
8237c478bd9Sstevel@tonic-gate rfs4_servinst(rfs4_client_t *cp)
8247c478bd9Sstevel@tonic-gate {
8257c478bd9Sstevel@tonic-gate 	ASSERT(rfs4_dbe_refcnt(cp->dbe) > 0);
8267c478bd9Sstevel@tonic-gate 
8277c478bd9Sstevel@tonic-gate 	return (cp->server_instance);
8287c478bd9Sstevel@tonic-gate }
8297c478bd9Sstevel@tonic-gate 
8307c478bd9Sstevel@tonic-gate /* ARGSUSED */
8317c478bd9Sstevel@tonic-gate static void
8327c478bd9Sstevel@tonic-gate nullfree(caddr_t resop)
8337c478bd9Sstevel@tonic-gate {
8347c478bd9Sstevel@tonic-gate }
8357c478bd9Sstevel@tonic-gate 
8367c478bd9Sstevel@tonic-gate /*
8377c478bd9Sstevel@tonic-gate  * This is a fall-through for invalid or not implemented (yet) ops
8387c478bd9Sstevel@tonic-gate  */
8397c478bd9Sstevel@tonic-gate /* ARGSUSED */
8407c478bd9Sstevel@tonic-gate static void
8417c478bd9Sstevel@tonic-gate rfs4_op_inval(nfs_argop4 *argop, nfs_resop4 *resop, struct svc_req *req,
8427c478bd9Sstevel@tonic-gate 	struct compound_state *cs)
8437c478bd9Sstevel@tonic-gate {
8447c478bd9Sstevel@tonic-gate 	*cs->statusp = *((nfsstat4 *)&(resop)->nfs_resop4_u) = NFS4ERR_INVAL;
8457c478bd9Sstevel@tonic-gate }
8467c478bd9Sstevel@tonic-gate 
8477c478bd9Sstevel@tonic-gate /*
8487c478bd9Sstevel@tonic-gate  * Check if the security flavor, nfsnum, is in the flavor_list.
8497c478bd9Sstevel@tonic-gate  */
8507c478bd9Sstevel@tonic-gate bool_t
8517c478bd9Sstevel@tonic-gate in_flavor_list(int nfsnum, int *flavor_list, int count)
8527c478bd9Sstevel@tonic-gate {
8537c478bd9Sstevel@tonic-gate 	int i;
8547c478bd9Sstevel@tonic-gate 
8557c478bd9Sstevel@tonic-gate 	for (i = 0; i < count; i++) {
8567c478bd9Sstevel@tonic-gate 		if (nfsnum == flavor_list[i])
8577c478bd9Sstevel@tonic-gate 			return (TRUE);
8587c478bd9Sstevel@tonic-gate 	}
8597c478bd9Sstevel@tonic-gate 	return (FALSE);
8607c478bd9Sstevel@tonic-gate }
8617c478bd9Sstevel@tonic-gate 
8627c478bd9Sstevel@tonic-gate /*
8637c478bd9Sstevel@tonic-gate  * Used by rfs4_op_secinfo to get the security information from the
8647c478bd9Sstevel@tonic-gate  * export structure associated with the component.
8657c478bd9Sstevel@tonic-gate  */
8667c478bd9Sstevel@tonic-gate /* ARGSUSED */
8677c478bd9Sstevel@tonic-gate static nfsstat4
8687c478bd9Sstevel@tonic-gate do_rfs4_op_secinfo(struct compound_state *cs, char *nm, SECINFO4res *resp)
8697c478bd9Sstevel@tonic-gate {
8707c478bd9Sstevel@tonic-gate 	int error, different_export = 0;
8717c478bd9Sstevel@tonic-gate 	vnode_t *dvp, *vp, *tvp;
8727c478bd9Sstevel@tonic-gate 	struct exportinfo *exi = NULL;
8737c478bd9Sstevel@tonic-gate 	fid_t fid;
8747c478bd9Sstevel@tonic-gate 	uint_t count, i;
8757c478bd9Sstevel@tonic-gate 	secinfo4 *resok_val;
8767c478bd9Sstevel@tonic-gate 	struct secinfo *secp;
8777c478bd9Sstevel@tonic-gate 	bool_t did_traverse;
8787c478bd9Sstevel@tonic-gate 	int dotdot, walk;
8797c478bd9Sstevel@tonic-gate 
8807c478bd9Sstevel@tonic-gate 	dvp = cs->vp;
8817c478bd9Sstevel@tonic-gate 	dotdot = (nm[0] == '.' && nm[1] == '.' && nm[2] == '\0');
8827c478bd9Sstevel@tonic-gate 
8837c478bd9Sstevel@tonic-gate 	/*
8847c478bd9Sstevel@tonic-gate 	 * If dotdotting, then need to check whether it's above the
8857c478bd9Sstevel@tonic-gate 	 * root of a filesystem, or above an export point.
8867c478bd9Sstevel@tonic-gate 	 */
8877c478bd9Sstevel@tonic-gate 	if (dotdot) {
8887c478bd9Sstevel@tonic-gate 
8897c478bd9Sstevel@tonic-gate 		/*
8907c478bd9Sstevel@tonic-gate 		 * If dotdotting at the root of a filesystem, then
8917c478bd9Sstevel@tonic-gate 		 * need to traverse back to the mounted-on filesystem
8927c478bd9Sstevel@tonic-gate 		 * and do the dotdot lookup there.
8937c478bd9Sstevel@tonic-gate 		 */
8947c478bd9Sstevel@tonic-gate 		if (cs->vp->v_flag & VROOT) {
8957c478bd9Sstevel@tonic-gate 
8967c478bd9Sstevel@tonic-gate 			/*
8977c478bd9Sstevel@tonic-gate 			 * If at the system root, then can
8987c478bd9Sstevel@tonic-gate 			 * go up no further.
8997c478bd9Sstevel@tonic-gate 			 */
9007c478bd9Sstevel@tonic-gate 			if (VN_CMP(dvp, rootdir))
9017c478bd9Sstevel@tonic-gate 				return (puterrno4(ENOENT));
9027c478bd9Sstevel@tonic-gate 
9037c478bd9Sstevel@tonic-gate 			/*
9047c478bd9Sstevel@tonic-gate 			 * Traverse back to the mounted-on filesystem
9057c478bd9Sstevel@tonic-gate 			 */
9067c478bd9Sstevel@tonic-gate 			dvp = untraverse(cs->vp);
9077c478bd9Sstevel@tonic-gate 
9087c478bd9Sstevel@tonic-gate 			/*
9097c478bd9Sstevel@tonic-gate 			 * Set the different_export flag so we remember
9107c478bd9Sstevel@tonic-gate 			 * to pick up a new exportinfo entry for
9117c478bd9Sstevel@tonic-gate 			 * this new filesystem.
9127c478bd9Sstevel@tonic-gate 			 */
9137c478bd9Sstevel@tonic-gate 			different_export = 1;
9147c478bd9Sstevel@tonic-gate 		} else {
9157c478bd9Sstevel@tonic-gate 
9167c478bd9Sstevel@tonic-gate 			/*
9177c478bd9Sstevel@tonic-gate 			 * If dotdotting above an export point then set
9187c478bd9Sstevel@tonic-gate 			 * the different_export to get new export info.
9197c478bd9Sstevel@tonic-gate 			 */
9207c478bd9Sstevel@tonic-gate 			different_export = nfs_exported(cs->exi, cs->vp);
9217c478bd9Sstevel@tonic-gate 		}
9227c478bd9Sstevel@tonic-gate 	}
9237c478bd9Sstevel@tonic-gate 
9247c478bd9Sstevel@tonic-gate 	/*
9257c478bd9Sstevel@tonic-gate 	 * Get the vnode for the component "nm".
9267c478bd9Sstevel@tonic-gate 	 */
9277c478bd9Sstevel@tonic-gate 	error = VOP_LOOKUP(dvp, nm, &vp, NULL, 0, NULL, cs->cr);
9287c478bd9Sstevel@tonic-gate 	if (error)
9297c478bd9Sstevel@tonic-gate 		return (puterrno4(error));
9307c478bd9Sstevel@tonic-gate 
9317c478bd9Sstevel@tonic-gate 	/*
9327c478bd9Sstevel@tonic-gate 	 * If the vnode is in a pseudo filesystem, or if the security flavor
9337c478bd9Sstevel@tonic-gate 	 * used in the request is valid but not an explicitly shared flavor,
9347c478bd9Sstevel@tonic-gate 	 * or the access bit indicates that this is a limited access,
9357c478bd9Sstevel@tonic-gate 	 * check whether this vnode is visible.
9367c478bd9Sstevel@tonic-gate 	 */
9377c478bd9Sstevel@tonic-gate 	if (!different_export &&
9387c478bd9Sstevel@tonic-gate 	    (PSEUDO(cs->exi) || ! is_exported_sec(cs->nfsflavor, cs->exi) ||
9397c478bd9Sstevel@tonic-gate 	    cs->access & CS_ACCESS_LIMITED)) {
9407c478bd9Sstevel@tonic-gate 		if (! nfs_visible(cs->exi, vp, &different_export)) {
9417c478bd9Sstevel@tonic-gate 			VN_RELE(vp);
9427c478bd9Sstevel@tonic-gate 			return (puterrno4(ENOENT));
9437c478bd9Sstevel@tonic-gate 		}
9447c478bd9Sstevel@tonic-gate 	}
9457c478bd9Sstevel@tonic-gate 
9467c478bd9Sstevel@tonic-gate 	/*
9477c478bd9Sstevel@tonic-gate 	 * If it's a mountpoint, then traverse it.
9487c478bd9Sstevel@tonic-gate 	 */
9497c478bd9Sstevel@tonic-gate 	if (vn_ismntpt(vp)) {
9507c478bd9Sstevel@tonic-gate 		tvp = vp;
9517c478bd9Sstevel@tonic-gate 		if ((error = traverse(&tvp)) != 0) {
9527c478bd9Sstevel@tonic-gate 			VN_RELE(vp);
9537c478bd9Sstevel@tonic-gate 			return (puterrno4(error));
9547c478bd9Sstevel@tonic-gate 		}
9557c478bd9Sstevel@tonic-gate 		/* remember that we had to traverse mountpoint */
9567c478bd9Sstevel@tonic-gate 		did_traverse = TRUE;
9577c478bd9Sstevel@tonic-gate 		vp = tvp;
9587c478bd9Sstevel@tonic-gate 		different_export = 1;
9597c478bd9Sstevel@tonic-gate 	} else if (vp->v_vfsp != dvp->v_vfsp) {
9607c478bd9Sstevel@tonic-gate 		/*
9617c478bd9Sstevel@tonic-gate 		 * If vp isn't a mountpoint and the vfs ptrs aren't the same,
9627c478bd9Sstevel@tonic-gate 		 * then vp is probably an LOFS object.  We don't need the
9637c478bd9Sstevel@tonic-gate 		 * realvp, we just need to know that we might have crossed
9647c478bd9Sstevel@tonic-gate 		 * a server fs boundary and need to call checkexport4.
9657c478bd9Sstevel@tonic-gate 		 * (LOFS lookup hides server fs mountpoints, and actually calls
9667c478bd9Sstevel@tonic-gate 		 * traverse)
9677c478bd9Sstevel@tonic-gate 		 */
9687c478bd9Sstevel@tonic-gate 		different_export = 1;
9697c478bd9Sstevel@tonic-gate 		did_traverse = FALSE;
9707c478bd9Sstevel@tonic-gate 	}
9717c478bd9Sstevel@tonic-gate 
9727c478bd9Sstevel@tonic-gate 	/*
9737c478bd9Sstevel@tonic-gate 	 * Get the export information for it.
9747c478bd9Sstevel@tonic-gate 	 */
9757c478bd9Sstevel@tonic-gate 	if (different_export) {
9767c478bd9Sstevel@tonic-gate 
9777c478bd9Sstevel@tonic-gate 		bzero(&fid, sizeof (fid));
9787c478bd9Sstevel@tonic-gate 		fid.fid_len = MAXFIDSZ;
9797c478bd9Sstevel@tonic-gate 		error = vop_fid_pseudo(vp, &fid);
9807c478bd9Sstevel@tonic-gate 		if (error) {
9817c478bd9Sstevel@tonic-gate 			VN_RELE(vp);
9827c478bd9Sstevel@tonic-gate 			return (puterrno4(error));
9837c478bd9Sstevel@tonic-gate 		}
9847c478bd9Sstevel@tonic-gate 
9857c478bd9Sstevel@tonic-gate 		if (dotdot)
9867c478bd9Sstevel@tonic-gate 			exi = nfs_vptoexi(NULL, vp, cs->cr, &walk, NULL, TRUE);
9877c478bd9Sstevel@tonic-gate 		else
9887c478bd9Sstevel@tonic-gate 			exi = checkexport4(&vp->v_vfsp->vfs_fsid, &fid, vp);
9897c478bd9Sstevel@tonic-gate 
9907c478bd9Sstevel@tonic-gate 		if (exi == NULL) {
9917c478bd9Sstevel@tonic-gate 			if (did_traverse == TRUE) {
9927c478bd9Sstevel@tonic-gate 				/*
9937c478bd9Sstevel@tonic-gate 				 * If this vnode is a mounted-on vnode,
9947c478bd9Sstevel@tonic-gate 				 * but the mounted-on file system is not
9957c478bd9Sstevel@tonic-gate 				 * exported, send back the secinfo for
9967c478bd9Sstevel@tonic-gate 				 * the exported node that the mounted-on
9977c478bd9Sstevel@tonic-gate 				 * vnode lives in.
9987c478bd9Sstevel@tonic-gate 				 */
9997c478bd9Sstevel@tonic-gate 				exi = cs->exi;
10007c478bd9Sstevel@tonic-gate 			} else {
10017c478bd9Sstevel@tonic-gate 				VN_RELE(vp);
10027c478bd9Sstevel@tonic-gate 				return (puterrno4(EACCES));
10037c478bd9Sstevel@tonic-gate 			}
10047c478bd9Sstevel@tonic-gate 		}
10057c478bd9Sstevel@tonic-gate 	} else {
10067c478bd9Sstevel@tonic-gate 		exi = cs->exi;
10077c478bd9Sstevel@tonic-gate 	}
10087c478bd9Sstevel@tonic-gate 	ASSERT(exi != NULL);
10097c478bd9Sstevel@tonic-gate 
10107c478bd9Sstevel@tonic-gate 
10117c478bd9Sstevel@tonic-gate 	/*
10127c478bd9Sstevel@tonic-gate 	 * Create the secinfo result based on the security information
10137c478bd9Sstevel@tonic-gate 	 * from the exportinfo structure (exi).
10147c478bd9Sstevel@tonic-gate 	 *
10157c478bd9Sstevel@tonic-gate 	 * Return all flavors for a pseudo node.
10167c478bd9Sstevel@tonic-gate 	 * For a real export node, return the flavor that the client
10177c478bd9Sstevel@tonic-gate 	 * has access with.
10187c478bd9Sstevel@tonic-gate 	 */
10197c478bd9Sstevel@tonic-gate 	ASSERT(RW_LOCK_HELD(&exported_lock));
10207c478bd9Sstevel@tonic-gate 	if (PSEUDO(exi)) {
10217c478bd9Sstevel@tonic-gate 		count = exi->exi_export.ex_seccnt; /* total sec count */
10227c478bd9Sstevel@tonic-gate 		resok_val = kmem_alloc(count * sizeof (secinfo4), KM_SLEEP);
10237c478bd9Sstevel@tonic-gate 		secp = exi->exi_export.ex_secinfo;
10247c478bd9Sstevel@tonic-gate 
10257c478bd9Sstevel@tonic-gate 		for (i = 0; i < count; i++) {
10267c478bd9Sstevel@tonic-gate 		    resok_val[i].flavor = secp[i].s_secinfo.sc_rpcnum;
10277c478bd9Sstevel@tonic-gate 		    if (resok_val[i].flavor == RPCSEC_GSS) {
10287c478bd9Sstevel@tonic-gate 			rpcsec_gss_info *info;
10297c478bd9Sstevel@tonic-gate 
10307c478bd9Sstevel@tonic-gate 			info = &resok_val[i].flavor_info;
10317c478bd9Sstevel@tonic-gate 			info->qop = secp[i].s_secinfo.sc_qop;
10327c478bd9Sstevel@tonic-gate 			info->service =
10337c478bd9Sstevel@tonic-gate 				(rpc_gss_svc_t)secp[i].s_secinfo.sc_service;
10347c478bd9Sstevel@tonic-gate 
10357c478bd9Sstevel@tonic-gate 			/* get oid opaque data */
10367c478bd9Sstevel@tonic-gate 			info->oid.sec_oid4_len =
10377c478bd9Sstevel@tonic-gate 				secp[i].s_secinfo.sc_gss_mech_type->length;
10387c478bd9Sstevel@tonic-gate 			info->oid.sec_oid4_val =
10397c478bd9Sstevel@tonic-gate 				kmem_alloc(
10407c478bd9Sstevel@tonic-gate 				    secp[i].s_secinfo.sc_gss_mech_type->length,
10417c478bd9Sstevel@tonic-gate 				    KM_SLEEP);
10427c478bd9Sstevel@tonic-gate 			bcopy(secp[i].s_secinfo.sc_gss_mech_type->elements,
10437c478bd9Sstevel@tonic-gate 				info->oid.sec_oid4_val, info->oid.sec_oid4_len);
10447c478bd9Sstevel@tonic-gate 		    }
10457c478bd9Sstevel@tonic-gate 		}
10467c478bd9Sstevel@tonic-gate 		resp->SECINFO4resok_len = count;
10477c478bd9Sstevel@tonic-gate 		resp->SECINFO4resok_val = resok_val;
10487c478bd9Sstevel@tonic-gate 	} else {
10497c478bd9Sstevel@tonic-gate 		int ret_cnt = 0, k = 0;
10507c478bd9Sstevel@tonic-gate 		int *flavor_list;
10517c478bd9Sstevel@tonic-gate 
10527c478bd9Sstevel@tonic-gate 		count = exi->exi_export.ex_seccnt; /* total sec count */
10537c478bd9Sstevel@tonic-gate 		secp = exi->exi_export.ex_secinfo;
10547c478bd9Sstevel@tonic-gate 
10557c478bd9Sstevel@tonic-gate 		flavor_list = kmem_alloc(count * sizeof (int), KM_SLEEP);
10567c478bd9Sstevel@tonic-gate 		/* find out which flavors to return */
10577c478bd9Sstevel@tonic-gate 		for (i = 0; i < count; i ++) {
10587c478bd9Sstevel@tonic-gate 			int access, flavor, perm;
10597c478bd9Sstevel@tonic-gate 
10607c478bd9Sstevel@tonic-gate 			flavor = secp[i].s_secinfo.sc_nfsnum;
10617c478bd9Sstevel@tonic-gate 			perm = secp[i].s_flags;
10627c478bd9Sstevel@tonic-gate 
10637c478bd9Sstevel@tonic-gate 			access = nfsauth4_secinfo_access(exi, cs->req,
10647c478bd9Sstevel@tonic-gate 						flavor, perm);
10657c478bd9Sstevel@tonic-gate 
10667c478bd9Sstevel@tonic-gate 			if (! (access & NFSAUTH_DENIED) &&
10677c478bd9Sstevel@tonic-gate 			    ! (access & NFSAUTH_WRONGSEC)) {
10687c478bd9Sstevel@tonic-gate 				flavor_list[ret_cnt] = flavor;
10697c478bd9Sstevel@tonic-gate 				ret_cnt++;
10707c478bd9Sstevel@tonic-gate 			}
10717c478bd9Sstevel@tonic-gate 		}
10727c478bd9Sstevel@tonic-gate 
10737c478bd9Sstevel@tonic-gate 		/* Create the returning SECINFO value */
10747c478bd9Sstevel@tonic-gate 		resok_val = kmem_alloc(ret_cnt * sizeof (secinfo4), KM_SLEEP);
10757c478bd9Sstevel@tonic-gate 
10767c478bd9Sstevel@tonic-gate 		for (i = 0; i < count; i++) {
10777c478bd9Sstevel@tonic-gate 		/* If the flavor is in the flavor list, fill in resok_val. */
10787c478bd9Sstevel@tonic-gate 		    if (in_flavor_list(secp[i].s_secinfo.sc_nfsnum,
10797c478bd9Sstevel@tonic-gate 						flavor_list, ret_cnt)) {
10807c478bd9Sstevel@tonic-gate 			resok_val[k].flavor = secp[i].s_secinfo.sc_rpcnum;
10817c478bd9Sstevel@tonic-gate 			if (resok_val[k].flavor == RPCSEC_GSS) {
10827c478bd9Sstevel@tonic-gate 			    rpcsec_gss_info *info;
10837c478bd9Sstevel@tonic-gate 
10847c478bd9Sstevel@tonic-gate 			    info = &resok_val[k].flavor_info;
10857c478bd9Sstevel@tonic-gate 			    info->qop = secp[i].s_secinfo.sc_qop;
10867c478bd9Sstevel@tonic-gate 			    info->service =
10877c478bd9Sstevel@tonic-gate 				(rpc_gss_svc_t)secp[i].s_secinfo.sc_service;
10887c478bd9Sstevel@tonic-gate 
10897c478bd9Sstevel@tonic-gate 			    /* get oid opaque data */
10907c478bd9Sstevel@tonic-gate 			    info->oid.sec_oid4_len =
10917c478bd9Sstevel@tonic-gate 				secp[i].s_secinfo.sc_gss_mech_type->length;
10927c478bd9Sstevel@tonic-gate 			    info->oid.sec_oid4_val =
10937c478bd9Sstevel@tonic-gate 				kmem_alloc(
10947c478bd9Sstevel@tonic-gate 				    secp[i].s_secinfo.sc_gss_mech_type->length,
10957c478bd9Sstevel@tonic-gate 				    KM_SLEEP);
10967c478bd9Sstevel@tonic-gate 			    bcopy(secp[i].s_secinfo.sc_gss_mech_type->elements,
10977c478bd9Sstevel@tonic-gate 				info->oid.sec_oid4_val, info->oid.sec_oid4_len);
10987c478bd9Sstevel@tonic-gate 			}
10997c478bd9Sstevel@tonic-gate 			k++;
11007c478bd9Sstevel@tonic-gate 		    }
11017c478bd9Sstevel@tonic-gate 		    if (k >= ret_cnt)
11027c478bd9Sstevel@tonic-gate 			break;
11037c478bd9Sstevel@tonic-gate 		}
11047c478bd9Sstevel@tonic-gate 		resp->SECINFO4resok_len = ret_cnt;
11057c478bd9Sstevel@tonic-gate 		resp->SECINFO4resok_val = resok_val;
11067c478bd9Sstevel@tonic-gate 		kmem_free(flavor_list, count * sizeof (int));
11077c478bd9Sstevel@tonic-gate 	}
11087c478bd9Sstevel@tonic-gate 
11097c478bd9Sstevel@tonic-gate 	VN_RELE(vp);
11107c478bd9Sstevel@tonic-gate 	return (NFS4_OK);
11117c478bd9Sstevel@tonic-gate }
11127c478bd9Sstevel@tonic-gate 
11137c478bd9Sstevel@tonic-gate /*
11147c478bd9Sstevel@tonic-gate  * SECINFO (Operation 33): Obtain required security information on
11157c478bd9Sstevel@tonic-gate  * the component name in the format of (security-mechanism-oid, qop, service)
11167c478bd9Sstevel@tonic-gate  * triplets.
11177c478bd9Sstevel@tonic-gate  */
11187c478bd9Sstevel@tonic-gate /* ARGSUSED */
11197c478bd9Sstevel@tonic-gate static void
11207c478bd9Sstevel@tonic-gate rfs4_op_secinfo(nfs_argop4 *argop, nfs_resop4 *resop, struct svc_req *req,
11217c478bd9Sstevel@tonic-gate 	struct compound_state *cs)
11227c478bd9Sstevel@tonic-gate {
11237c478bd9Sstevel@tonic-gate 	SECINFO4res *resp = &resop->nfs_resop4_u.opsecinfo;
11247c478bd9Sstevel@tonic-gate 	utf8string *utfnm = &argop->nfs_argop4_u.opsecinfo.name;
11257c478bd9Sstevel@tonic-gate 	uint_t len;
11267c478bd9Sstevel@tonic-gate 	char *nm;
11277c478bd9Sstevel@tonic-gate 
11287c478bd9Sstevel@tonic-gate 	/*
11297c478bd9Sstevel@tonic-gate 	 * Current file handle (cfh) should have been set before getting
11307c478bd9Sstevel@tonic-gate 	 * into this function. If not, return error.
11317c478bd9Sstevel@tonic-gate 	 */
11327c478bd9Sstevel@tonic-gate 	if (cs->vp == NULL) {
11337c478bd9Sstevel@tonic-gate 		*cs->statusp = resp->status = NFS4ERR_NOFILEHANDLE;
11347c478bd9Sstevel@tonic-gate 		return;
11357c478bd9Sstevel@tonic-gate 	}
11367c478bd9Sstevel@tonic-gate 
11377c478bd9Sstevel@tonic-gate 	if (cs->vp->v_type != VDIR) {
11387c478bd9Sstevel@tonic-gate 		*cs->statusp = resp->status = NFS4ERR_NOTDIR;
11397c478bd9Sstevel@tonic-gate 		return;
11407c478bd9Sstevel@tonic-gate 	}
11417c478bd9Sstevel@tonic-gate 
11427c478bd9Sstevel@tonic-gate 	/*
11437c478bd9Sstevel@tonic-gate 	 * Verify the component name. If failed, error out, but
11447c478bd9Sstevel@tonic-gate 	 * do not error out if the component name is a "..".
11457c478bd9Sstevel@tonic-gate 	 * SECINFO will return its parents secinfo data for SECINFO "..".
11467c478bd9Sstevel@tonic-gate 	 */
11477c478bd9Sstevel@tonic-gate 	if (!utf8_dir_verify(utfnm)) {
11487c478bd9Sstevel@tonic-gate 		if (utfnm->utf8string_len != 2 ||
11497c478bd9Sstevel@tonic-gate 				utfnm->utf8string_val[0] != '.' ||
11507c478bd9Sstevel@tonic-gate 				utfnm->utf8string_val[1] != '.') {
11517c478bd9Sstevel@tonic-gate 			*cs->statusp = resp->status = NFS4ERR_INVAL;
11527c478bd9Sstevel@tonic-gate 			return;
11537c478bd9Sstevel@tonic-gate 		}
11547c478bd9Sstevel@tonic-gate 	}
11557c478bd9Sstevel@tonic-gate 
11567c478bd9Sstevel@tonic-gate 	nm = utf8_to_str(utfnm, &len, NULL);
11577c478bd9Sstevel@tonic-gate 	if (nm == NULL) {
11587c478bd9Sstevel@tonic-gate 		*cs->statusp = resp->status = NFS4ERR_INVAL;
11597c478bd9Sstevel@tonic-gate 		return;
11607c478bd9Sstevel@tonic-gate 	}
11617c478bd9Sstevel@tonic-gate 
11627c478bd9Sstevel@tonic-gate 	if (len > MAXNAMELEN) {
11637c478bd9Sstevel@tonic-gate 		*cs->statusp = resp->status = NFS4ERR_NAMETOOLONG;
11647c478bd9Sstevel@tonic-gate 		kmem_free(nm, len);
11657c478bd9Sstevel@tonic-gate 		return;
11667c478bd9Sstevel@tonic-gate 	}
11677c478bd9Sstevel@tonic-gate 
11687c478bd9Sstevel@tonic-gate 	*cs->statusp = resp->status = do_rfs4_op_secinfo(cs, nm, resp);
11697c478bd9Sstevel@tonic-gate 
11707c478bd9Sstevel@tonic-gate 	kmem_free(nm, len);
11717c478bd9Sstevel@tonic-gate }
11727c478bd9Sstevel@tonic-gate 
11737c478bd9Sstevel@tonic-gate /*
11747c478bd9Sstevel@tonic-gate  * Free SECINFO result.
11757c478bd9Sstevel@tonic-gate  */
11767c478bd9Sstevel@tonic-gate /* ARGSUSED */
11777c478bd9Sstevel@tonic-gate static void
11787c478bd9Sstevel@tonic-gate rfs4_op_secinfo_free(nfs_resop4 *resop)
11797c478bd9Sstevel@tonic-gate {
11807c478bd9Sstevel@tonic-gate 	SECINFO4res *resp = &resop->nfs_resop4_u.opsecinfo;
11817c478bd9Sstevel@tonic-gate 	int count, i;
11827c478bd9Sstevel@tonic-gate 	secinfo4 *resok_val;
11837c478bd9Sstevel@tonic-gate 
11847c478bd9Sstevel@tonic-gate 	/* If this is not an Ok result, nothing to free. */
11857c478bd9Sstevel@tonic-gate 	if (resp->status != NFS4_OK) {
11867c478bd9Sstevel@tonic-gate 		return;
11877c478bd9Sstevel@tonic-gate 	}
11887c478bd9Sstevel@tonic-gate 
11897c478bd9Sstevel@tonic-gate 	count = resp->SECINFO4resok_len;
11907c478bd9Sstevel@tonic-gate 	resok_val = resp->SECINFO4resok_val;
11917c478bd9Sstevel@tonic-gate 
11927c478bd9Sstevel@tonic-gate 	for (i = 0; i < count; i++) {
11937c478bd9Sstevel@tonic-gate 	    if (resok_val[i].flavor == RPCSEC_GSS) {
11947c478bd9Sstevel@tonic-gate 		rpcsec_gss_info *info;
11957c478bd9Sstevel@tonic-gate 
11967c478bd9Sstevel@tonic-gate 		info = &resok_val[i].flavor_info;
11977c478bd9Sstevel@tonic-gate 		kmem_free(info->oid.sec_oid4_val, info->oid.sec_oid4_len);
11987c478bd9Sstevel@tonic-gate 	    }
11997c478bd9Sstevel@tonic-gate 	}
12007c478bd9Sstevel@tonic-gate 	kmem_free(resok_val, count * sizeof (secinfo4));
12017c478bd9Sstevel@tonic-gate 	resp->SECINFO4resok_len = 0;
12027c478bd9Sstevel@tonic-gate 	resp->SECINFO4resok_val = NULL;
12037c478bd9Sstevel@tonic-gate }
12047c478bd9Sstevel@tonic-gate 
120545916cd2Sjpk /*
120645916cd2Sjpk  * do label check on client label and server's file lable.
120745916cd2Sjpk  */
120845916cd2Sjpk static boolean_t
120945916cd2Sjpk do_rfs4_label_check(bslabel_t *clabel, vnode_t *vp, int flag)
121045916cd2Sjpk {
121145916cd2Sjpk 	bslabel_t *slabel;
121245916cd2Sjpk 	ts_label_t *tslabel;
121345916cd2Sjpk 	boolean_t result;
121445916cd2Sjpk 
121545916cd2Sjpk 	if ((tslabel = nfs4_getflabel(vp)) == NULL) {
121645916cd2Sjpk 		return (B_FALSE);
121745916cd2Sjpk 	}
121845916cd2Sjpk 	slabel = label2bslabel(tslabel);
121945916cd2Sjpk 	DTRACE_PROBE4(tx__rfs4__log__info__labelcheck, char *,
122045916cd2Sjpk 	    "comparing server's file label(1) with client label(2) (vp(3))",
122145916cd2Sjpk 	    bslabel_t *, slabel, bslabel_t *, clabel, vnode_t *, vp);
122245916cd2Sjpk 
122345916cd2Sjpk 	if (flag == EQUALITY_CHECK)
122445916cd2Sjpk 		result = blequal(clabel, slabel);
122545916cd2Sjpk 	else
122645916cd2Sjpk 		result = bldominates(clabel, slabel);
122745916cd2Sjpk 	label_rele(tslabel);
122845916cd2Sjpk 	return (result);
122945916cd2Sjpk }
123045916cd2Sjpk 
12317c478bd9Sstevel@tonic-gate /* ARGSUSED */
12327c478bd9Sstevel@tonic-gate static void
12337c478bd9Sstevel@tonic-gate rfs4_op_access(nfs_argop4 *argop, nfs_resop4 *resop, struct svc_req *req,
12347c478bd9Sstevel@tonic-gate 	struct compound_state *cs)
12357c478bd9Sstevel@tonic-gate {
12367c478bd9Sstevel@tonic-gate 	ACCESS4args *args = &argop->nfs_argop4_u.opaccess;
12377c478bd9Sstevel@tonic-gate 	ACCESS4res *resp = &resop->nfs_resop4_u.opaccess;
12387c478bd9Sstevel@tonic-gate 	int error;
12397c478bd9Sstevel@tonic-gate 	vnode_t *vp;
12407c478bd9Sstevel@tonic-gate 	struct vattr va;
12417c478bd9Sstevel@tonic-gate 	int checkwriteperm;
12427c478bd9Sstevel@tonic-gate 	cred_t *cr = cs->cr;
124345916cd2Sjpk 	bslabel_t *clabel, *slabel;
124445916cd2Sjpk 	ts_label_t *tslabel;
124545916cd2Sjpk 	boolean_t admin_low_client;
12467c478bd9Sstevel@tonic-gate 
12477c478bd9Sstevel@tonic-gate #if 0	/* XXX allow access even if !cs->access. Eventually only pseudo fs */
12487c478bd9Sstevel@tonic-gate 	if (cs->access == CS_ACCESS_DENIED) {
12497c478bd9Sstevel@tonic-gate 		*cs->statusp = resp->status = NFS4ERR_ACCESS;
12507c478bd9Sstevel@tonic-gate 		return;
12517c478bd9Sstevel@tonic-gate 	}
12527c478bd9Sstevel@tonic-gate #endif
12537c478bd9Sstevel@tonic-gate 	if (cs->vp == NULL) {
12547c478bd9Sstevel@tonic-gate 		*cs->statusp = resp->status = NFS4ERR_NOFILEHANDLE;
12557c478bd9Sstevel@tonic-gate 		return;
12567c478bd9Sstevel@tonic-gate 	}
12577c478bd9Sstevel@tonic-gate 
12587c478bd9Sstevel@tonic-gate 	ASSERT(cr != NULL);
12597c478bd9Sstevel@tonic-gate 
12607c478bd9Sstevel@tonic-gate 	vp = cs->vp;
12617c478bd9Sstevel@tonic-gate 
12627c478bd9Sstevel@tonic-gate 	/*
12637c478bd9Sstevel@tonic-gate 	 * If the file system is exported read only, it is not appropriate
12647c478bd9Sstevel@tonic-gate 	 * to check write permissions for regular files and directories.
12657c478bd9Sstevel@tonic-gate 	 * Special files are interpreted by the client, so the underlying
12667c478bd9Sstevel@tonic-gate 	 * permissions are sent back to the client for interpretation.
12677c478bd9Sstevel@tonic-gate 	 */
12687c478bd9Sstevel@tonic-gate 	if (rdonly4(cs->exi, cs->vp, req) &&
12697c478bd9Sstevel@tonic-gate 		(vp->v_type == VREG || vp->v_type == VDIR))
12707c478bd9Sstevel@tonic-gate 		checkwriteperm = 0;
12717c478bd9Sstevel@tonic-gate 	else
12727c478bd9Sstevel@tonic-gate 		checkwriteperm = 1;
12737c478bd9Sstevel@tonic-gate 
12747c478bd9Sstevel@tonic-gate 	/*
12757c478bd9Sstevel@tonic-gate 	 * XXX
12767c478bd9Sstevel@tonic-gate 	 * We need the mode so that we can correctly determine access
12777c478bd9Sstevel@tonic-gate 	 * permissions relative to a mandatory lock file.  Access to
12787c478bd9Sstevel@tonic-gate 	 * mandatory lock files is denied on the server, so it might
12797c478bd9Sstevel@tonic-gate 	 * as well be reflected to the server during the open.
12807c478bd9Sstevel@tonic-gate 	 */
12817c478bd9Sstevel@tonic-gate 	va.va_mask = AT_MODE;
12827c478bd9Sstevel@tonic-gate 	error = VOP_GETATTR(vp, &va, 0, cr);
12837c478bd9Sstevel@tonic-gate 	if (error) {
12847c478bd9Sstevel@tonic-gate 		*cs->statusp = resp->status = puterrno4(error);
12857c478bd9Sstevel@tonic-gate 		return;
12867c478bd9Sstevel@tonic-gate 	}
12877c478bd9Sstevel@tonic-gate 	resp->access = 0;
12887c478bd9Sstevel@tonic-gate 	resp->supported = 0;
12897c478bd9Sstevel@tonic-gate 
129045916cd2Sjpk 	if (is_system_labeled()) {
129145916cd2Sjpk 		ASSERT(req->rq_label != NULL);
129245916cd2Sjpk 		clabel = req->rq_label;
129345916cd2Sjpk 		DTRACE_PROBE2(tx__rfs4__log__info__opaccess__clabel, char *,
129445916cd2Sjpk 		    "got client label from request(1)",
129545916cd2Sjpk 		    struct svc_req *, req);
129645916cd2Sjpk 		if (!blequal(&l_admin_low->tsl_label, clabel)) {
129745916cd2Sjpk 			if ((tslabel = nfs4_getflabel(vp)) == NULL) {
129845916cd2Sjpk 				*cs->statusp = resp->status = puterrno4(EACCES);
129945916cd2Sjpk 				return;
130045916cd2Sjpk 			}
130145916cd2Sjpk 			slabel = label2bslabel(tslabel);
130245916cd2Sjpk 			DTRACE_PROBE3(tx__rfs4__log__info__opaccess__slabel,
130345916cd2Sjpk 			    char *, "got server label(1) for vp(2)",
130445916cd2Sjpk 			    bslabel_t *, slabel, vnode_t *, vp);
130545916cd2Sjpk 
130645916cd2Sjpk 			admin_low_client = B_FALSE;
130745916cd2Sjpk 		} else
130845916cd2Sjpk 			admin_low_client = B_TRUE;
130945916cd2Sjpk 	}
131045916cd2Sjpk 
13117c478bd9Sstevel@tonic-gate 	if (args->access & ACCESS4_READ) {
13127c478bd9Sstevel@tonic-gate 		error = VOP_ACCESS(vp, VREAD, 0, cr);
131345916cd2Sjpk 		if (!error && !MANDLOCK(vp, va.va_mode) &&
131445916cd2Sjpk 		    (!is_system_labeled() || admin_low_client ||
131545916cd2Sjpk 		    bldominates(clabel, slabel)))
13167c478bd9Sstevel@tonic-gate 			resp->access |= ACCESS4_READ;
13177c478bd9Sstevel@tonic-gate 		resp->supported |= ACCESS4_READ;
13187c478bd9Sstevel@tonic-gate 	}
13197c478bd9Sstevel@tonic-gate 	if ((args->access & ACCESS4_LOOKUP) && vp->v_type == VDIR) {
13207c478bd9Sstevel@tonic-gate 		error = VOP_ACCESS(vp, VEXEC, 0, cr);
132145916cd2Sjpk 		if (!error && (!is_system_labeled() || admin_low_client ||
132245916cd2Sjpk 		    bldominates(clabel, slabel)))
13237c478bd9Sstevel@tonic-gate 			resp->access |= ACCESS4_LOOKUP;
13247c478bd9Sstevel@tonic-gate 		resp->supported |= ACCESS4_LOOKUP;
13257c478bd9Sstevel@tonic-gate 	}
13267c478bd9Sstevel@tonic-gate 	if (checkwriteperm &&
13277c478bd9Sstevel@tonic-gate 	    (args->access & (ACCESS4_MODIFY|ACCESS4_EXTEND))) {
13287c478bd9Sstevel@tonic-gate 		error = VOP_ACCESS(vp, VWRITE, 0, cr);
132945916cd2Sjpk 		if (!error && !MANDLOCK(vp, va.va_mode) &&
133045916cd2Sjpk 		    (!is_system_labeled() || admin_low_client ||
133145916cd2Sjpk 		    blequal(clabel, slabel)))
13327c478bd9Sstevel@tonic-gate 			resp->access |=
13337c478bd9Sstevel@tonic-gate 			    (args->access & (ACCESS4_MODIFY|ACCESS4_EXTEND));
13347c478bd9Sstevel@tonic-gate 		resp->supported |= (ACCESS4_MODIFY|ACCESS4_EXTEND);
13357c478bd9Sstevel@tonic-gate 	}
13367c478bd9Sstevel@tonic-gate 
13377c478bd9Sstevel@tonic-gate 	if (checkwriteperm &&
13387c478bd9Sstevel@tonic-gate 	    (args->access & ACCESS4_DELETE) && vp->v_type == VDIR) {
13397c478bd9Sstevel@tonic-gate 		error = VOP_ACCESS(vp, VWRITE, 0, cr);
134045916cd2Sjpk 		if (!error && (!is_system_labeled() || admin_low_client ||
134145916cd2Sjpk 		    blequal(clabel, slabel)))
13427c478bd9Sstevel@tonic-gate 			resp->access |= ACCESS4_DELETE;
13437c478bd9Sstevel@tonic-gate 		resp->supported |= ACCESS4_DELETE;
13447c478bd9Sstevel@tonic-gate 	}
13457c478bd9Sstevel@tonic-gate 	if (args->access & ACCESS4_EXECUTE && vp->v_type != VDIR) {
13467c478bd9Sstevel@tonic-gate 		error = VOP_ACCESS(vp, VEXEC, 0, cr);
134745916cd2Sjpk 		if (!error && !MANDLOCK(vp, va.va_mode) &&
134845916cd2Sjpk 		    (!is_system_labeled() || admin_low_client ||
134945916cd2Sjpk 		    bldominates(clabel, slabel)))
13507c478bd9Sstevel@tonic-gate 			resp->access |= ACCESS4_EXECUTE;
13517c478bd9Sstevel@tonic-gate 		resp->supported |= ACCESS4_EXECUTE;
13527c478bd9Sstevel@tonic-gate 	}
13537c478bd9Sstevel@tonic-gate 
135445916cd2Sjpk 	if (is_system_labeled() && !admin_low_client)
135545916cd2Sjpk 		label_rele(tslabel);
135645916cd2Sjpk 
13577c478bd9Sstevel@tonic-gate 	*cs->statusp = resp->status = NFS4_OK;
13587c478bd9Sstevel@tonic-gate }
13597c478bd9Sstevel@tonic-gate 
13607c478bd9Sstevel@tonic-gate /* ARGSUSED */
13617c478bd9Sstevel@tonic-gate static void
13627c478bd9Sstevel@tonic-gate rfs4_op_commit(nfs_argop4 *argop, nfs_resop4 *resop, struct svc_req *req,
13637c478bd9Sstevel@tonic-gate 	struct compound_state *cs)
13647c478bd9Sstevel@tonic-gate {
13657c478bd9Sstevel@tonic-gate 	COMMIT4args *args = &argop->nfs_argop4_u.opcommit;
13667c478bd9Sstevel@tonic-gate 	COMMIT4res *resp = &resop->nfs_resop4_u.opcommit;
13677c478bd9Sstevel@tonic-gate 	int error;
13687c478bd9Sstevel@tonic-gate 	vnode_t *vp = cs->vp;
13697c478bd9Sstevel@tonic-gate 	cred_t *cr = cs->cr;
13707c478bd9Sstevel@tonic-gate 	vattr_t va;
13717c478bd9Sstevel@tonic-gate 
13727c478bd9Sstevel@tonic-gate 	if (vp == NULL) {
13737c478bd9Sstevel@tonic-gate 		*cs->statusp = resp->status = NFS4ERR_NOFILEHANDLE;
13747c478bd9Sstevel@tonic-gate 		return;
13757c478bd9Sstevel@tonic-gate 	}
13767c478bd9Sstevel@tonic-gate 	if (cs->access == CS_ACCESS_DENIED) {
13777c478bd9Sstevel@tonic-gate 		*cs->statusp = resp->status = NFS4ERR_ACCESS;
13787c478bd9Sstevel@tonic-gate 		return;
13797c478bd9Sstevel@tonic-gate 	}
13807c478bd9Sstevel@tonic-gate 
13817c478bd9Sstevel@tonic-gate 	if (args->offset + args->count < args->offset) {
13827c478bd9Sstevel@tonic-gate 		*cs->statusp = resp->status = NFS4ERR_INVAL;
13837c478bd9Sstevel@tonic-gate 		return;
13847c478bd9Sstevel@tonic-gate 	}
13857c478bd9Sstevel@tonic-gate 
13867c478bd9Sstevel@tonic-gate 	va.va_mask = AT_UID;
13877c478bd9Sstevel@tonic-gate 	error = VOP_GETATTR(vp, &va, 0, cr);
13887c478bd9Sstevel@tonic-gate 
13897c478bd9Sstevel@tonic-gate 	/*
13907c478bd9Sstevel@tonic-gate 	 * If we can't get the attributes, then we can't do the
13917c478bd9Sstevel@tonic-gate 	 * right access checking.  So, we'll fail the request.
13927c478bd9Sstevel@tonic-gate 	 */
13937c478bd9Sstevel@tonic-gate 	if (error) {
13947c478bd9Sstevel@tonic-gate 		*cs->statusp = resp->status = puterrno4(error);
13957c478bd9Sstevel@tonic-gate 		return;
13967c478bd9Sstevel@tonic-gate 	}
13977c478bd9Sstevel@tonic-gate 	if (rdonly4(cs->exi, cs->vp, req)) {
13987c478bd9Sstevel@tonic-gate 		*cs->statusp = resp->status = NFS4ERR_ROFS;
13997c478bd9Sstevel@tonic-gate 		return;
14007c478bd9Sstevel@tonic-gate 	}
14017c478bd9Sstevel@tonic-gate 
14027c478bd9Sstevel@tonic-gate 	if (vp->v_type != VREG) {
14037c478bd9Sstevel@tonic-gate 		if (vp->v_type == VDIR)
14047c478bd9Sstevel@tonic-gate 			resp->status = NFS4ERR_ISDIR;
14057c478bd9Sstevel@tonic-gate 		else
14067c478bd9Sstevel@tonic-gate 			resp->status = NFS4ERR_INVAL;
14077c478bd9Sstevel@tonic-gate 		*cs->statusp = resp->status;
14087c478bd9Sstevel@tonic-gate 		return;
14097c478bd9Sstevel@tonic-gate 	}
14107c478bd9Sstevel@tonic-gate 
14117c478bd9Sstevel@tonic-gate 	if (crgetuid(cr) != va.va_uid &&
14127c478bd9Sstevel@tonic-gate 	    (error = VOP_ACCESS(vp, VWRITE, 0, cs->cr))) {
14137c478bd9Sstevel@tonic-gate 		*cs->statusp = resp->status = puterrno4(error);
14147c478bd9Sstevel@tonic-gate 		return;
14157c478bd9Sstevel@tonic-gate 	}
14167c478bd9Sstevel@tonic-gate 
14177c478bd9Sstevel@tonic-gate 	error = VOP_PUTPAGE(vp, args->offset, args->count, 0, cr);
14187c478bd9Sstevel@tonic-gate 	if (!error)
14197c478bd9Sstevel@tonic-gate 		error = VOP_FSYNC(vp, FNODSYNC, cr);
14207c478bd9Sstevel@tonic-gate 
14217c478bd9Sstevel@tonic-gate 	if (error) {
14227c478bd9Sstevel@tonic-gate 		*cs->statusp = resp->status = puterrno4(error);
14237c478bd9Sstevel@tonic-gate 		return;
14247c478bd9Sstevel@tonic-gate 	}
14257c478bd9Sstevel@tonic-gate 
14267c478bd9Sstevel@tonic-gate 	*cs->statusp = resp->status = NFS4_OK;
14277c478bd9Sstevel@tonic-gate 	resp->writeverf = Write4verf;
14287c478bd9Sstevel@tonic-gate }
14297c478bd9Sstevel@tonic-gate 
14307c478bd9Sstevel@tonic-gate /*
14317c478bd9Sstevel@tonic-gate  * rfs4_op_mknod is called from rfs4_op_create after all initial verification
14327c478bd9Sstevel@tonic-gate  * was completed. It does the nfsv4 create for special files.
14337c478bd9Sstevel@tonic-gate  */
14347c478bd9Sstevel@tonic-gate /* ARGSUSED */
14357c478bd9Sstevel@tonic-gate static vnode_t *
14367c478bd9Sstevel@tonic-gate do_rfs4_op_mknod(CREATE4args *args, CREATE4res *resp, struct svc_req *req,
14377c478bd9Sstevel@tonic-gate 	struct compound_state *cs, vattr_t *vap, char *nm)
14387c478bd9Sstevel@tonic-gate {
14397c478bd9Sstevel@tonic-gate 	int error;
14407c478bd9Sstevel@tonic-gate 	cred_t *cr = cs->cr;
14417c478bd9Sstevel@tonic-gate 	vnode_t *dvp = cs->vp;
14427c478bd9Sstevel@tonic-gate 	vnode_t *vp = NULL;
14437c478bd9Sstevel@tonic-gate 	int mode;
14447c478bd9Sstevel@tonic-gate 	enum vcexcl excl;
14457c478bd9Sstevel@tonic-gate 
14467c478bd9Sstevel@tonic-gate 	switch (args->type) {
14477c478bd9Sstevel@tonic-gate 	case NF4CHR:
14487c478bd9Sstevel@tonic-gate 	case NF4BLK:
14497c478bd9Sstevel@tonic-gate 		if (secpolicy_sys_devices(cr) != 0) {
14507c478bd9Sstevel@tonic-gate 			*cs->statusp = resp->status = NFS4ERR_PERM;
14517c478bd9Sstevel@tonic-gate 			return (NULL);
14527c478bd9Sstevel@tonic-gate 		}
14537c478bd9Sstevel@tonic-gate 		if (args->type == NF4CHR)
14547c478bd9Sstevel@tonic-gate 			vap->va_type = VCHR;
14557c478bd9Sstevel@tonic-gate 		else
14567c478bd9Sstevel@tonic-gate 			vap->va_type = VBLK;
14577c478bd9Sstevel@tonic-gate 		vap->va_rdev = makedevice(args->ftype4_u.devdata.specdata1,
14587c478bd9Sstevel@tonic-gate 					args->ftype4_u.devdata.specdata2);
14597c478bd9Sstevel@tonic-gate 		vap->va_mask |= AT_RDEV;
14607c478bd9Sstevel@tonic-gate 		break;
14617c478bd9Sstevel@tonic-gate 	case NF4SOCK:
14627c478bd9Sstevel@tonic-gate 		vap->va_type = VSOCK;
14637c478bd9Sstevel@tonic-gate 		break;
14647c478bd9Sstevel@tonic-gate 	case NF4FIFO:
14657c478bd9Sstevel@tonic-gate 		vap->va_type = VFIFO;
14667c478bd9Sstevel@tonic-gate 		break;
14677c478bd9Sstevel@tonic-gate 	default:
14687c478bd9Sstevel@tonic-gate 		*cs->statusp = resp->status = NFS4ERR_BADTYPE;
14697c478bd9Sstevel@tonic-gate 		return (NULL);
14707c478bd9Sstevel@tonic-gate 	}
14717c478bd9Sstevel@tonic-gate 
14727c478bd9Sstevel@tonic-gate 	/*
14737c478bd9Sstevel@tonic-gate 	 * Must specify the mode.
14747c478bd9Sstevel@tonic-gate 	 */
14757c478bd9Sstevel@tonic-gate 	if (!(vap->va_mask & AT_MODE)) {
14767c478bd9Sstevel@tonic-gate 		*cs->statusp = resp->status = NFS4ERR_INVAL;
14777c478bd9Sstevel@tonic-gate 		return (NULL);
14787c478bd9Sstevel@tonic-gate 	}
14797c478bd9Sstevel@tonic-gate 
14807c478bd9Sstevel@tonic-gate 	excl = EXCL;
14817c478bd9Sstevel@tonic-gate 
14827c478bd9Sstevel@tonic-gate 	mode = 0;
14837c478bd9Sstevel@tonic-gate 
14847c478bd9Sstevel@tonic-gate 	error = VOP_CREATE(dvp, nm, vap, excl, mode, &vp, cr, 0);
14857c478bd9Sstevel@tonic-gate 	if (error) {
14867c478bd9Sstevel@tonic-gate 		*cs->statusp = resp->status = puterrno4(error);
14877c478bd9Sstevel@tonic-gate 		return (NULL);
14887c478bd9Sstevel@tonic-gate 	}
14897c478bd9Sstevel@tonic-gate 	return (vp);
14907c478bd9Sstevel@tonic-gate }
14917c478bd9Sstevel@tonic-gate 
14927c478bd9Sstevel@tonic-gate /*
14937c478bd9Sstevel@tonic-gate  * nfsv4 create is used to create non-regular files. For regular files,
14947c478bd9Sstevel@tonic-gate  * use nfsv4 open.
14957c478bd9Sstevel@tonic-gate  */
14967c478bd9Sstevel@tonic-gate /* ARGSUSED */
14977c478bd9Sstevel@tonic-gate static void
14987c478bd9Sstevel@tonic-gate rfs4_op_create(nfs_argop4 *argop, nfs_resop4 *resop, struct svc_req *req,
14997c478bd9Sstevel@tonic-gate 	struct compound_state *cs)
15007c478bd9Sstevel@tonic-gate {
15017c478bd9Sstevel@tonic-gate 	CREATE4args *args = &argop->nfs_argop4_u.opcreate;
15027c478bd9Sstevel@tonic-gate 	CREATE4res *resp = &resop->nfs_resop4_u.opcreate;
15037c478bd9Sstevel@tonic-gate 	int error;
15047c478bd9Sstevel@tonic-gate 	struct vattr bva, iva, iva2, ava, *vap;
15057c478bd9Sstevel@tonic-gate 	cred_t *cr = cs->cr;
15067c478bd9Sstevel@tonic-gate 	vnode_t *dvp = cs->vp;
15077c478bd9Sstevel@tonic-gate 	vnode_t *vp = NULL;
15087c478bd9Sstevel@tonic-gate 	char *nm, *lnm;
15097c478bd9Sstevel@tonic-gate 	uint_t len, llen;
15107c478bd9Sstevel@tonic-gate 	int syncval = 0;
15117c478bd9Sstevel@tonic-gate 	struct nfs4_svgetit_arg sarg;
15127c478bd9Sstevel@tonic-gate 	struct nfs4_ntov_table ntov;
15137c478bd9Sstevel@tonic-gate 	struct statvfs64 sb;
15147c478bd9Sstevel@tonic-gate 	nfsstat4 status;
15157c478bd9Sstevel@tonic-gate 
15167c478bd9Sstevel@tonic-gate 	resp->attrset = 0;
15177c478bd9Sstevel@tonic-gate 
15187c478bd9Sstevel@tonic-gate 	if (dvp == NULL) {
15197c478bd9Sstevel@tonic-gate 		*cs->statusp = resp->status = NFS4ERR_NOFILEHANDLE;
15207c478bd9Sstevel@tonic-gate 		return;
15217c478bd9Sstevel@tonic-gate 	}
15227c478bd9Sstevel@tonic-gate 
15237c478bd9Sstevel@tonic-gate 	/*
15247c478bd9Sstevel@tonic-gate 	 * If there is an unshared filesystem mounted on this vnode,
15257c478bd9Sstevel@tonic-gate 	 * do not allow to create an object in this directory.
15267c478bd9Sstevel@tonic-gate 	 */
15277c478bd9Sstevel@tonic-gate 	if (vn_ismntpt(dvp)) {
15287c478bd9Sstevel@tonic-gate 		*cs->statusp = resp->status = NFS4ERR_ACCESS;
15297c478bd9Sstevel@tonic-gate 		return;
15307c478bd9Sstevel@tonic-gate 	}
15317c478bd9Sstevel@tonic-gate 
15327c478bd9Sstevel@tonic-gate 	/* Verify that type is correct */
15337c478bd9Sstevel@tonic-gate 	switch (args->type) {
15347c478bd9Sstevel@tonic-gate 	case NF4LNK:
15357c478bd9Sstevel@tonic-gate 	case NF4BLK:
15367c478bd9Sstevel@tonic-gate 	case NF4CHR:
15377c478bd9Sstevel@tonic-gate 	case NF4SOCK:
15387c478bd9Sstevel@tonic-gate 	case NF4FIFO:
15397c478bd9Sstevel@tonic-gate 	case NF4DIR:
15407c478bd9Sstevel@tonic-gate 		break;
15417c478bd9Sstevel@tonic-gate 	default:
15427c478bd9Sstevel@tonic-gate 		*cs->statusp = resp->status = NFS4ERR_BADTYPE;
15437c478bd9Sstevel@tonic-gate 		return;
15447c478bd9Sstevel@tonic-gate 	};
15457c478bd9Sstevel@tonic-gate 
15467c478bd9Sstevel@tonic-gate 	if (cs->access == CS_ACCESS_DENIED) {
15477c478bd9Sstevel@tonic-gate 		*cs->statusp = resp->status = NFS4ERR_ACCESS;
15487c478bd9Sstevel@tonic-gate 		return;
15497c478bd9Sstevel@tonic-gate 	}
15507c478bd9Sstevel@tonic-gate 	if (dvp->v_type != VDIR) {
15517c478bd9Sstevel@tonic-gate 		*cs->statusp = resp->status = NFS4ERR_NOTDIR;
15527c478bd9Sstevel@tonic-gate 		return;
15537c478bd9Sstevel@tonic-gate 	}
15547c478bd9Sstevel@tonic-gate 	if (!utf8_dir_verify(&args->objname)) {
15557c478bd9Sstevel@tonic-gate 		*cs->statusp = resp->status = NFS4ERR_INVAL;
15567c478bd9Sstevel@tonic-gate 		return;
15577c478bd9Sstevel@tonic-gate 	}
15587c478bd9Sstevel@tonic-gate 
15597c478bd9Sstevel@tonic-gate 	if (rdonly4(cs->exi, cs->vp, req)) {
15607c478bd9Sstevel@tonic-gate 		*cs->statusp = resp->status = NFS4ERR_ROFS;
15617c478bd9Sstevel@tonic-gate 		return;
15627c478bd9Sstevel@tonic-gate 	}
15637c478bd9Sstevel@tonic-gate 
15647c478bd9Sstevel@tonic-gate 	/*
15657c478bd9Sstevel@tonic-gate 	 * Name of newly created object
15667c478bd9Sstevel@tonic-gate 	 */
15677c478bd9Sstevel@tonic-gate 	nm = utf8_to_fn(&args->objname, &len, NULL);
15687c478bd9Sstevel@tonic-gate 	if (nm == NULL) {
15697c478bd9Sstevel@tonic-gate 		*cs->statusp = resp->status = NFS4ERR_INVAL;
15707c478bd9Sstevel@tonic-gate 		return;
15717c478bd9Sstevel@tonic-gate 	}
15727c478bd9Sstevel@tonic-gate 
15737c478bd9Sstevel@tonic-gate 	if (len > MAXNAMELEN) {
15747c478bd9Sstevel@tonic-gate 		*cs->statusp = resp->status = NFS4ERR_NAMETOOLONG;
15757c478bd9Sstevel@tonic-gate 		kmem_free(nm, len);
15767c478bd9Sstevel@tonic-gate 		return;
15777c478bd9Sstevel@tonic-gate 	}
15787c478bd9Sstevel@tonic-gate 
15797c478bd9Sstevel@tonic-gate 	resp->attrset = 0;
15807c478bd9Sstevel@tonic-gate 
15817c478bd9Sstevel@tonic-gate 	sarg.sbp = &sb;
15827c478bd9Sstevel@tonic-gate 	nfs4_ntov_table_init(&ntov);
15837c478bd9Sstevel@tonic-gate 
15847c478bd9Sstevel@tonic-gate 	status = do_rfs4_set_attrs(&resp->attrset,
15857c478bd9Sstevel@tonic-gate 					&args->createattrs, cs, &sarg,
15867c478bd9Sstevel@tonic-gate 					&ntov, NFS4ATTR_SETIT);
15877c478bd9Sstevel@tonic-gate 
15887c478bd9Sstevel@tonic-gate 	if (sarg.vap->va_mask == 0 && status == NFS4_OK)
15897c478bd9Sstevel@tonic-gate 		status = NFS4ERR_INVAL;
15907c478bd9Sstevel@tonic-gate 
15917c478bd9Sstevel@tonic-gate 	if (status != NFS4_OK) {
15927c478bd9Sstevel@tonic-gate 		*cs->statusp = resp->status = status;
15937c478bd9Sstevel@tonic-gate 		kmem_free(nm, len);
15947c478bd9Sstevel@tonic-gate 		nfs4_ntov_table_free(&ntov, &sarg);
15957c478bd9Sstevel@tonic-gate 		resp->attrset = 0;
15967c478bd9Sstevel@tonic-gate 		return;
15977c478bd9Sstevel@tonic-gate 	}
15987c478bd9Sstevel@tonic-gate 
15997c478bd9Sstevel@tonic-gate 	/* Get "before" change value */
16007c478bd9Sstevel@tonic-gate 	bva.va_mask = AT_CTIME|AT_SEQ;
16017c478bd9Sstevel@tonic-gate 	error = VOP_GETATTR(dvp, &bva, 0, cr);
16027c478bd9Sstevel@tonic-gate 	if (error) {
16037c478bd9Sstevel@tonic-gate 		*cs->statusp = resp->status = puterrno4(error);
16047c478bd9Sstevel@tonic-gate 		kmem_free(nm, len);
16057c478bd9Sstevel@tonic-gate 		nfs4_ntov_table_free(&ntov, &sarg);
16067c478bd9Sstevel@tonic-gate 		resp->attrset = 0;
16077c478bd9Sstevel@tonic-gate 		return;
16087c478bd9Sstevel@tonic-gate 	}
16097c478bd9Sstevel@tonic-gate 	NFS4_SET_FATTR4_CHANGE(resp->cinfo.before, bva.va_ctime)
16107c478bd9Sstevel@tonic-gate 
16117c478bd9Sstevel@tonic-gate 	vap = sarg.vap;
16127c478bd9Sstevel@tonic-gate 
16137c478bd9Sstevel@tonic-gate 	/*
16147c478bd9Sstevel@tonic-gate 	 * Set default initial values for attributes when not specified
16157c478bd9Sstevel@tonic-gate 	 * in createattrs.
16167c478bd9Sstevel@tonic-gate 	 */
16177c478bd9Sstevel@tonic-gate 	if ((vap->va_mask & AT_UID) == 0) {
16187c478bd9Sstevel@tonic-gate 		vap->va_uid = crgetuid(cr);
16197c478bd9Sstevel@tonic-gate 		vap->va_mask |= AT_UID;
16207c478bd9Sstevel@tonic-gate 	}
16217c478bd9Sstevel@tonic-gate 	if ((vap->va_mask & AT_GID) == 0) {
16227c478bd9Sstevel@tonic-gate 		vap->va_gid = crgetgid(cr);
16237c478bd9Sstevel@tonic-gate 		vap->va_mask |= AT_GID;
16247c478bd9Sstevel@tonic-gate 	}
16257c478bd9Sstevel@tonic-gate 
16267c478bd9Sstevel@tonic-gate 	vap->va_mask |= AT_TYPE;
16277c478bd9Sstevel@tonic-gate 	switch (args->type) {
16287c478bd9Sstevel@tonic-gate 	case NF4DIR:
16297c478bd9Sstevel@tonic-gate 		vap->va_type = VDIR;
16307c478bd9Sstevel@tonic-gate 		if ((vap->va_mask & AT_MODE) == 0) {
16317c478bd9Sstevel@tonic-gate 			vap->va_mode = 0700;	/* default: owner rwx only */
16327c478bd9Sstevel@tonic-gate 			vap->va_mask |= AT_MODE;
16337c478bd9Sstevel@tonic-gate 		}
16347c478bd9Sstevel@tonic-gate 		error = VOP_MKDIR(dvp, nm, vap, &vp, cr);
16357c478bd9Sstevel@tonic-gate 		if (error)
16367c478bd9Sstevel@tonic-gate 			break;
16377c478bd9Sstevel@tonic-gate 
16387c478bd9Sstevel@tonic-gate 		/*
16397c478bd9Sstevel@tonic-gate 		 * Get the initial "after" sequence number, if it fails,
16407c478bd9Sstevel@tonic-gate 		 * set to zero
16417c478bd9Sstevel@tonic-gate 		 */
16427c478bd9Sstevel@tonic-gate 		iva.va_mask = AT_SEQ;
16437c478bd9Sstevel@tonic-gate 		if (VOP_GETATTR(dvp, &iva, 0, cs->cr))
16447c478bd9Sstevel@tonic-gate 			iva.va_seq = 0;
16457c478bd9Sstevel@tonic-gate 		break;
16467c478bd9Sstevel@tonic-gate 	case NF4LNK:
16477c478bd9Sstevel@tonic-gate 		vap->va_type = VLNK;
16487c478bd9Sstevel@tonic-gate 		if ((vap->va_mask & AT_MODE) == 0) {
16497c478bd9Sstevel@tonic-gate 			vap->va_mode = 0700;	/* default: owner rwx only */
16507c478bd9Sstevel@tonic-gate 			vap->va_mask |= AT_MODE;
16517c478bd9Sstevel@tonic-gate 		}
16527c478bd9Sstevel@tonic-gate 
16537c478bd9Sstevel@tonic-gate 		/*
16547c478bd9Sstevel@tonic-gate 		 * symlink names must be treated as data
16557c478bd9Sstevel@tonic-gate 		 */
16567c478bd9Sstevel@tonic-gate 		lnm = utf8_to_str(&args->ftype4_u.linkdata, &llen, NULL);
16577c478bd9Sstevel@tonic-gate 
16587c478bd9Sstevel@tonic-gate 		if (lnm == NULL) {
16597c478bd9Sstevel@tonic-gate 			*cs->statusp = resp->status = NFS4ERR_INVAL;
16607c478bd9Sstevel@tonic-gate 			kmem_free(nm, len);
16617c478bd9Sstevel@tonic-gate 			nfs4_ntov_table_free(&ntov, &sarg);
16627c478bd9Sstevel@tonic-gate 			resp->attrset = 0;
16637c478bd9Sstevel@tonic-gate 			return;
16647c478bd9Sstevel@tonic-gate 		}
16657c478bd9Sstevel@tonic-gate 
16667c478bd9Sstevel@tonic-gate 		if (llen > MAXPATHLEN) {
16677c478bd9Sstevel@tonic-gate 			*cs->statusp = resp->status = NFS4ERR_NAMETOOLONG;
16687c478bd9Sstevel@tonic-gate 			kmem_free(nm, len);
16697c478bd9Sstevel@tonic-gate 			kmem_free(lnm, llen);
16707c478bd9Sstevel@tonic-gate 			nfs4_ntov_table_free(&ntov, &sarg);
16717c478bd9Sstevel@tonic-gate 			resp->attrset = 0;
16727c478bd9Sstevel@tonic-gate 			return;
16737c478bd9Sstevel@tonic-gate 		}
16747c478bd9Sstevel@tonic-gate 
16757c478bd9Sstevel@tonic-gate 		error = VOP_SYMLINK(dvp, nm, vap, lnm, cr);
16767c478bd9Sstevel@tonic-gate 		if (lnm != NULL)
16777c478bd9Sstevel@tonic-gate 			kmem_free(lnm, llen);
16787c478bd9Sstevel@tonic-gate 		if (error)
16797c478bd9Sstevel@tonic-gate 			break;
16807c478bd9Sstevel@tonic-gate 
16817c478bd9Sstevel@tonic-gate 		/*
16827c478bd9Sstevel@tonic-gate 		 * Get the initial "after" sequence number, if it fails,
16837c478bd9Sstevel@tonic-gate 		 * set to zero
16847c478bd9Sstevel@tonic-gate 		 */
16857c478bd9Sstevel@tonic-gate 		iva.va_mask = AT_SEQ;
16867c478bd9Sstevel@tonic-gate 		if (VOP_GETATTR(dvp, &iva, 0, cs->cr))
16877c478bd9Sstevel@tonic-gate 			iva.va_seq = 0;
16887c478bd9Sstevel@tonic-gate 
16897c478bd9Sstevel@tonic-gate 		error = VOP_LOOKUP(dvp, nm, &vp, NULL, 0, NULL, cr);
16907c478bd9Sstevel@tonic-gate 		if (error)
16917c478bd9Sstevel@tonic-gate 			break;
16927c478bd9Sstevel@tonic-gate 
16937c478bd9Sstevel@tonic-gate 		/*
16947c478bd9Sstevel@tonic-gate 		 * va_seq is not safe over VOP calls, check it again
16957c478bd9Sstevel@tonic-gate 		 * if it has changed zero out iva to force atomic = FALSE.
16967c478bd9Sstevel@tonic-gate 		 */
16977c478bd9Sstevel@tonic-gate 		iva2.va_mask = AT_SEQ;
16987c478bd9Sstevel@tonic-gate 		if (VOP_GETATTR(dvp, &iva2, 0, cs->cr) ||
16997c478bd9Sstevel@tonic-gate 						iva2.va_seq != iva.va_seq)
17007c478bd9Sstevel@tonic-gate 			iva.va_seq = 0;
17017c478bd9Sstevel@tonic-gate 		break;
17027c478bd9Sstevel@tonic-gate 	default:
17037c478bd9Sstevel@tonic-gate 		/*
17047c478bd9Sstevel@tonic-gate 		 * probably a special file.
17057c478bd9Sstevel@tonic-gate 		 */
17067c478bd9Sstevel@tonic-gate 		if ((vap->va_mask & AT_MODE) == 0) {
17077c478bd9Sstevel@tonic-gate 			vap->va_mode = 0600;	/* default: owner rw only */
17087c478bd9Sstevel@tonic-gate 			vap->va_mask |= AT_MODE;
17097c478bd9Sstevel@tonic-gate 		}
17107c478bd9Sstevel@tonic-gate 		syncval = FNODSYNC;
17117c478bd9Sstevel@tonic-gate 		/*
17127c478bd9Sstevel@tonic-gate 		 * We know this will only generate one VOP call
17137c478bd9Sstevel@tonic-gate 		 */
17147c478bd9Sstevel@tonic-gate 		vp = do_rfs4_op_mknod(args, resp, req, cs, vap, nm);
17157c478bd9Sstevel@tonic-gate 
17167c478bd9Sstevel@tonic-gate 		if (vp == NULL) {
17177c478bd9Sstevel@tonic-gate 			kmem_free(nm, len);
17187c478bd9Sstevel@tonic-gate 			nfs4_ntov_table_free(&ntov, &sarg);
17197c478bd9Sstevel@tonic-gate 			resp->attrset = 0;
17207c478bd9Sstevel@tonic-gate 			return;
17217c478bd9Sstevel@tonic-gate 		}
17227c478bd9Sstevel@tonic-gate 
17237c478bd9Sstevel@tonic-gate 		/*
17247c478bd9Sstevel@tonic-gate 		 * Get the initial "after" sequence number, if it fails,
17257c478bd9Sstevel@tonic-gate 		 * set to zero
17267c478bd9Sstevel@tonic-gate 		 */
17277c478bd9Sstevel@tonic-gate 		iva.va_mask = AT_SEQ;
17287c478bd9Sstevel@tonic-gate 		if (VOP_GETATTR(dvp, &iva, 0, cs->cr))
17297c478bd9Sstevel@tonic-gate 			iva.va_seq = 0;
17307c478bd9Sstevel@tonic-gate 
17317c478bd9Sstevel@tonic-gate 		break;
17327c478bd9Sstevel@tonic-gate 	}
17337c478bd9Sstevel@tonic-gate 	kmem_free(nm, len);
17347c478bd9Sstevel@tonic-gate 
17357c478bd9Sstevel@tonic-gate 	if (error) {
17367c478bd9Sstevel@tonic-gate 		*cs->statusp = resp->status = puterrno4(error);
17377c478bd9Sstevel@tonic-gate 	}
17387c478bd9Sstevel@tonic-gate 
17397c478bd9Sstevel@tonic-gate 	/*
17407c478bd9Sstevel@tonic-gate 	 * Force modified data and metadata out to stable storage.
17417c478bd9Sstevel@tonic-gate 	 */
17427c478bd9Sstevel@tonic-gate 	(void) VOP_FSYNC(dvp, 0, cr);
17437c478bd9Sstevel@tonic-gate 
17447c478bd9Sstevel@tonic-gate 	if (resp->status != NFS4_OK) {
17457c478bd9Sstevel@tonic-gate 		if (vp != NULL)
17467c478bd9Sstevel@tonic-gate 			VN_RELE(vp);
17477c478bd9Sstevel@tonic-gate 		nfs4_ntov_table_free(&ntov, &sarg);
17487c478bd9Sstevel@tonic-gate 		resp->attrset = 0;
17497c478bd9Sstevel@tonic-gate 		return;
17507c478bd9Sstevel@tonic-gate 	}
17517c478bd9Sstevel@tonic-gate 
17527c478bd9Sstevel@tonic-gate 	/*
17537c478bd9Sstevel@tonic-gate 	 * Finish setup of cinfo response, "before" value already set.
17547c478bd9Sstevel@tonic-gate 	 * Get "after" change value, if it fails, simply return the
17557c478bd9Sstevel@tonic-gate 	 * before value.
17567c478bd9Sstevel@tonic-gate 	 */
17577c478bd9Sstevel@tonic-gate 	ava.va_mask = AT_CTIME|AT_SEQ;
17587c478bd9Sstevel@tonic-gate 	if (VOP_GETATTR(dvp, &ava, 0, cr)) {
17597c478bd9Sstevel@tonic-gate 		ava.va_ctime = bva.va_ctime;
17607c478bd9Sstevel@tonic-gate 		ava.va_seq = 0;
17617c478bd9Sstevel@tonic-gate 	}
17627c478bd9Sstevel@tonic-gate 	NFS4_SET_FATTR4_CHANGE(resp->cinfo.after, ava.va_ctime);
17637c478bd9Sstevel@tonic-gate 
17647c478bd9Sstevel@tonic-gate 	/*
17657c478bd9Sstevel@tonic-gate 	 * True verification that object was created with correct
17667c478bd9Sstevel@tonic-gate 	 * attrs is impossible.  The attrs could have been changed
17677c478bd9Sstevel@tonic-gate 	 * immediately after object creation.  If attributes did
17687c478bd9Sstevel@tonic-gate 	 * not verify, the only recourse for the server is to
17697c478bd9Sstevel@tonic-gate 	 * destroy the object.  Maybe if some attrs (like gid)
17707c478bd9Sstevel@tonic-gate 	 * are set incorrectly, the object should be destroyed;
17717c478bd9Sstevel@tonic-gate 	 * however, seems bad as a default policy.  Do we really
17727c478bd9Sstevel@tonic-gate 	 * want to destroy an object over one of the times not
17737c478bd9Sstevel@tonic-gate 	 * verifying correctly?  For these reasons, the server
17747c478bd9Sstevel@tonic-gate 	 * currently sets bits in attrset for createattrs
17757c478bd9Sstevel@tonic-gate 	 * that were set; however, no verification is done.
17767c478bd9Sstevel@tonic-gate 	 *
17777c478bd9Sstevel@tonic-gate 	 * vmask_to_nmask accounts for vattr bits set on create
17787c478bd9Sstevel@tonic-gate 	 *	[do_rfs4_set_attrs() only sets resp bits for
17797c478bd9Sstevel@tonic-gate 	 *	 non-vattr/vfs bits.]
17807c478bd9Sstevel@tonic-gate 	 * Mask off any bits set by default so as not to return
17817c478bd9Sstevel@tonic-gate 	 * more attrset bits than were requested in createattrs
17827c478bd9Sstevel@tonic-gate 	 */
17837c478bd9Sstevel@tonic-gate 	nfs4_vmask_to_nmask(sarg.vap->va_mask, &resp->attrset);
17847c478bd9Sstevel@tonic-gate 	resp->attrset &= args->createattrs.attrmask;
17857c478bd9Sstevel@tonic-gate 	nfs4_ntov_table_free(&ntov, &sarg);
17867c478bd9Sstevel@tonic-gate 
17877c478bd9Sstevel@tonic-gate 	error = makefh4(&cs->fh, vp, cs->exi);
17887c478bd9Sstevel@tonic-gate 	if (error) {
17897c478bd9Sstevel@tonic-gate 		*cs->statusp = resp->status = puterrno4(error);
17907c478bd9Sstevel@tonic-gate 	}
17917c478bd9Sstevel@tonic-gate 
17927c478bd9Sstevel@tonic-gate 	/*
17937c478bd9Sstevel@tonic-gate 	 * The cinfo.atomic = TRUE only if we got no errors, we have
17947c478bd9Sstevel@tonic-gate 	 * non-zero va_seq's, and it has incremented by exactly one
17957c478bd9Sstevel@tonic-gate 	 * during the creation and it didn't change during the VOP_LOOKUP
17967c478bd9Sstevel@tonic-gate 	 * or VOP_FSYNC.
17977c478bd9Sstevel@tonic-gate 	 */
17987c478bd9Sstevel@tonic-gate 	if (!error && bva.va_seq && iva.va_seq && ava.va_seq &&
17997c478bd9Sstevel@tonic-gate 			iva.va_seq == (bva.va_seq + 1) &&
18007c478bd9Sstevel@tonic-gate 			iva.va_seq == ava.va_seq)
18017c478bd9Sstevel@tonic-gate 		resp->cinfo.atomic = TRUE;
18027c478bd9Sstevel@tonic-gate 	else
18037c478bd9Sstevel@tonic-gate 		resp->cinfo.atomic = FALSE;
18047c478bd9Sstevel@tonic-gate 
18057c478bd9Sstevel@tonic-gate 	(void) VOP_FSYNC(vp, syncval, cr);
18067c478bd9Sstevel@tonic-gate 
18077c478bd9Sstevel@tonic-gate 	if (resp->status != NFS4_OK) {
18087c478bd9Sstevel@tonic-gate 		VN_RELE(vp);
18097c478bd9Sstevel@tonic-gate 		return;
18107c478bd9Sstevel@tonic-gate 	}
18117c478bd9Sstevel@tonic-gate 	if (cs->vp)
18127c478bd9Sstevel@tonic-gate 		VN_RELE(cs->vp);
18137c478bd9Sstevel@tonic-gate 
18147c478bd9Sstevel@tonic-gate 	cs->vp = vp;
18157c478bd9Sstevel@tonic-gate 	*cs->statusp = resp->status = NFS4_OK;
18167c478bd9Sstevel@tonic-gate }
18177c478bd9Sstevel@tonic-gate 
18187c478bd9Sstevel@tonic-gate 
18197c478bd9Sstevel@tonic-gate /*ARGSUSED*/
18207c478bd9Sstevel@tonic-gate static void
18217c478bd9Sstevel@tonic-gate rfs4_op_delegreturn(nfs_argop4 *argop, nfs_resop4 *resop, struct svc_req *req,
18227c478bd9Sstevel@tonic-gate 	struct compound_state *cs)
18237c478bd9Sstevel@tonic-gate {
18247c478bd9Sstevel@tonic-gate 	DELEGRETURN4args *args = &argop->nfs_argop4_u.opdelegreturn;
18257c478bd9Sstevel@tonic-gate 	DELEGRETURN4res *resp = &resop->nfs_resop4_u.opdelegreturn;
18267c478bd9Sstevel@tonic-gate 	rfs4_deleg_state_t *dsp;
18277c478bd9Sstevel@tonic-gate 	nfsstat4 status;
18287c478bd9Sstevel@tonic-gate 
18297c478bd9Sstevel@tonic-gate 	status = rfs4_get_deleg_state(&args->deleg_stateid, &dsp);
18307c478bd9Sstevel@tonic-gate 	resp->status = *cs->statusp = status;
18317c478bd9Sstevel@tonic-gate 	if (status != NFS4_OK)
18327c478bd9Sstevel@tonic-gate 		return;
18337c478bd9Sstevel@tonic-gate 
18347c478bd9Sstevel@tonic-gate 	/* Ensure specified filehandle matches */
18357c478bd9Sstevel@tonic-gate 	if (cs->vp != dsp->finfo->vp) {
18367c478bd9Sstevel@tonic-gate 		resp->status = *cs->statusp = NFS4ERR_BAD_STATEID;
18377c478bd9Sstevel@tonic-gate 	} else
18387c478bd9Sstevel@tonic-gate 		rfs4_return_deleg(dsp, FALSE);
18397c478bd9Sstevel@tonic-gate 
18407c478bd9Sstevel@tonic-gate 	rfs4_update_lease(dsp->client);
18417c478bd9Sstevel@tonic-gate 
18427c478bd9Sstevel@tonic-gate 	rfs4_deleg_state_rele(dsp);
18437c478bd9Sstevel@tonic-gate }
18447c478bd9Sstevel@tonic-gate 
18457c478bd9Sstevel@tonic-gate /*
18467c478bd9Sstevel@tonic-gate  * Check to see if a given "flavor" is an explicitly shared flavor.
18477c478bd9Sstevel@tonic-gate  * The assumption of this routine is the "flavor" is already a valid
18487c478bd9Sstevel@tonic-gate  * flavor in the secinfo list of "exi".
18497c478bd9Sstevel@tonic-gate  *
18507c478bd9Sstevel@tonic-gate  *	e.g.
18517c478bd9Sstevel@tonic-gate  *		# share -o sec=flavor1 /export
18527c478bd9Sstevel@tonic-gate  *		# share -o sec=flavor2 /export/home
18537c478bd9Sstevel@tonic-gate  *
18547c478bd9Sstevel@tonic-gate  *		flavor2 is not an explicitly shared flavor for /export,
18557c478bd9Sstevel@tonic-gate  *		however it is in the secinfo list for /export thru the
18567c478bd9Sstevel@tonic-gate  *		server namespace setup.
18577c478bd9Sstevel@tonic-gate  */
18587c478bd9Sstevel@tonic-gate int
18597c478bd9Sstevel@tonic-gate is_exported_sec(int flavor, struct exportinfo *exi)
18607c478bd9Sstevel@tonic-gate {
18617c478bd9Sstevel@tonic-gate 	int	i;
18627c478bd9Sstevel@tonic-gate 	struct secinfo *sp;
18637c478bd9Sstevel@tonic-gate 
18647c478bd9Sstevel@tonic-gate 	sp = exi->exi_export.ex_secinfo;
18657c478bd9Sstevel@tonic-gate 	for (i = 0; i < exi->exi_export.ex_seccnt; i++) {
18667c478bd9Sstevel@tonic-gate 		if (flavor == sp[i].s_secinfo.sc_nfsnum ||
18677c478bd9Sstevel@tonic-gate 		    sp[i].s_secinfo.sc_nfsnum == AUTH_NONE) {
18687c478bd9Sstevel@tonic-gate 			return (SEC_REF_EXPORTED(&sp[i]));
18697c478bd9Sstevel@tonic-gate 		}
18707c478bd9Sstevel@tonic-gate 	}
18717c478bd9Sstevel@tonic-gate 
18727c478bd9Sstevel@tonic-gate 	/* Should not reach this point based on the assumption */
18737c478bd9Sstevel@tonic-gate 	return (0);
18747c478bd9Sstevel@tonic-gate }
18757c478bd9Sstevel@tonic-gate 
18767c478bd9Sstevel@tonic-gate /*
18777c478bd9Sstevel@tonic-gate  * Check if the security flavor used in the request matches what is
18787c478bd9Sstevel@tonic-gate  * required at the export point or at the root pseudo node (exi_root).
18797c478bd9Sstevel@tonic-gate  *
18807c478bd9Sstevel@tonic-gate  * returns 1 if there's a match or if exported with AUTH_NONE; 0 otherwise.
18817c478bd9Sstevel@tonic-gate  *
18827c478bd9Sstevel@tonic-gate  */
18837c478bd9Sstevel@tonic-gate static int
18847c478bd9Sstevel@tonic-gate secinfo_match_or_authnone(struct compound_state *cs)
18857c478bd9Sstevel@tonic-gate {
18867c478bd9Sstevel@tonic-gate 	int	i;
18877c478bd9Sstevel@tonic-gate 	struct secinfo *sp;
18887c478bd9Sstevel@tonic-gate 
18897c478bd9Sstevel@tonic-gate 	/*
18907c478bd9Sstevel@tonic-gate 	 * Check cs->nfsflavor (from the request) against
18917c478bd9Sstevel@tonic-gate 	 * the current export data in cs->exi.
18927c478bd9Sstevel@tonic-gate 	 */
18937c478bd9Sstevel@tonic-gate 	sp = cs->exi->exi_export.ex_secinfo;
18947c478bd9Sstevel@tonic-gate 	for (i = 0; i < cs->exi->exi_export.ex_seccnt; i++) {
18957c478bd9Sstevel@tonic-gate 		if (cs->nfsflavor == sp[i].s_secinfo.sc_nfsnum ||
18967c478bd9Sstevel@tonic-gate 		    sp[i].s_secinfo.sc_nfsnum == AUTH_NONE)
18977c478bd9Sstevel@tonic-gate 			return (1);
18987c478bd9Sstevel@tonic-gate 	}
18997c478bd9Sstevel@tonic-gate 
19007c478bd9Sstevel@tonic-gate 	return (0);
19017c478bd9Sstevel@tonic-gate }
19027c478bd9Sstevel@tonic-gate 
19037c478bd9Sstevel@tonic-gate /*
19047c478bd9Sstevel@tonic-gate  * Check the access authority for the client and return the correct error.
19057c478bd9Sstevel@tonic-gate  */
19067c478bd9Sstevel@tonic-gate nfsstat4
19077c478bd9Sstevel@tonic-gate call_checkauth4(struct compound_state *cs, struct svc_req *req)
19087c478bd9Sstevel@tonic-gate {
19097c478bd9Sstevel@tonic-gate 	int	authres;
19107c478bd9Sstevel@tonic-gate 
19117c478bd9Sstevel@tonic-gate 	/*
19127c478bd9Sstevel@tonic-gate 	 * First, check if the security flavor used in the request
19137c478bd9Sstevel@tonic-gate 	 * are among the flavors set in the server namespace.
19147c478bd9Sstevel@tonic-gate 	 */
19157c478bd9Sstevel@tonic-gate 	if (!secinfo_match_or_authnone(cs)) {
19167c478bd9Sstevel@tonic-gate 		*cs->statusp = NFS4ERR_WRONGSEC;
19177c478bd9Sstevel@tonic-gate 		return (*cs->statusp);
19187c478bd9Sstevel@tonic-gate 	}
19197c478bd9Sstevel@tonic-gate 
19207c478bd9Sstevel@tonic-gate 	authres = checkauth4(cs, req);
19217c478bd9Sstevel@tonic-gate 
19227c478bd9Sstevel@tonic-gate 	if (authres > 0) {
19237c478bd9Sstevel@tonic-gate 		*cs->statusp = NFS4_OK;
19247c478bd9Sstevel@tonic-gate 		if (! (cs->access & CS_ACCESS_LIMITED))
19257c478bd9Sstevel@tonic-gate 			cs->access = CS_ACCESS_OK;
19267c478bd9Sstevel@tonic-gate 	} else if (authres == 0) {
19277c478bd9Sstevel@tonic-gate 		*cs->statusp = NFS4ERR_ACCESS;
19287c478bd9Sstevel@tonic-gate 	} else if (authres == -2) {
19297c478bd9Sstevel@tonic-gate 		*cs->statusp = NFS4ERR_WRONGSEC;
19307c478bd9Sstevel@tonic-gate 	} else {
19317c478bd9Sstevel@tonic-gate 		*cs->statusp = NFS4ERR_DELAY;
19327c478bd9Sstevel@tonic-gate 	}
19337c478bd9Sstevel@tonic-gate 	return (*cs->statusp);
19347c478bd9Sstevel@tonic-gate }
19357c478bd9Sstevel@tonic-gate 
19367c478bd9Sstevel@tonic-gate /*
19377c478bd9Sstevel@tonic-gate  * bitmap4_to_attrmask is called by getattr and readdir.
19387c478bd9Sstevel@tonic-gate  * It sets up the vattr mask and determines whether vfsstat call is needed
19397c478bd9Sstevel@tonic-gate  * based on the input bitmap.
19407c478bd9Sstevel@tonic-gate  * Returns nfsv4 status.
19417c478bd9Sstevel@tonic-gate  */
19427c478bd9Sstevel@tonic-gate static nfsstat4
19437c478bd9Sstevel@tonic-gate bitmap4_to_attrmask(bitmap4 breq, struct nfs4_svgetit_arg *sargp)
19447c478bd9Sstevel@tonic-gate {
19457c478bd9Sstevel@tonic-gate 	int i;
19467c478bd9Sstevel@tonic-gate 	uint_t	va_mask;
19477c478bd9Sstevel@tonic-gate 	struct statvfs64 *sbp = sargp->sbp;
19487c478bd9Sstevel@tonic-gate 
19497c478bd9Sstevel@tonic-gate 	sargp->sbp = NULL;
19507c478bd9Sstevel@tonic-gate 	sargp->flag = 0;
19517c478bd9Sstevel@tonic-gate 	sargp->rdattr_error = NFS4_OK;
19527c478bd9Sstevel@tonic-gate 	sargp->mntdfid_set = FALSE;
19537c478bd9Sstevel@tonic-gate 	if (sargp->cs->vp)
19547c478bd9Sstevel@tonic-gate 		sargp->xattr = get_fh4_flag(&sargp->cs->fh,
19557c478bd9Sstevel@tonic-gate 					    FH4_ATTRDIR | FH4_NAMEDATTR);
19567c478bd9Sstevel@tonic-gate 	else
19577c478bd9Sstevel@tonic-gate 		sargp->xattr = 0;
19587c478bd9Sstevel@tonic-gate 
19597c478bd9Sstevel@tonic-gate 	/*
19607c478bd9Sstevel@tonic-gate 	 * Set rdattr_error_req to true if return error per
19617c478bd9Sstevel@tonic-gate 	 * failed entry rather than fail the readdir.
19627c478bd9Sstevel@tonic-gate 	 */
19637c478bd9Sstevel@tonic-gate 	if (breq & FATTR4_RDATTR_ERROR_MASK)
19647c478bd9Sstevel@tonic-gate 		sargp->rdattr_error_req = 1;
19657c478bd9Sstevel@tonic-gate 	else
19667c478bd9Sstevel@tonic-gate 		sargp->rdattr_error_req = 0;
19677c478bd9Sstevel@tonic-gate 
19687c478bd9Sstevel@tonic-gate 	/*
19697c478bd9Sstevel@tonic-gate 	 * generate the va_mask
19707c478bd9Sstevel@tonic-gate 	 * Handle the easy cases first
19717c478bd9Sstevel@tonic-gate 	 */
19727c478bd9Sstevel@tonic-gate 	switch (breq) {
19737c478bd9Sstevel@tonic-gate 	case NFS4_NTOV_ATTR_MASK:
19747c478bd9Sstevel@tonic-gate 		sargp->vap->va_mask = NFS4_NTOV_ATTR_AT_MASK;
19757c478bd9Sstevel@tonic-gate 		return (NFS4_OK);
19767c478bd9Sstevel@tonic-gate 
19777c478bd9Sstevel@tonic-gate 	case NFS4_FS_ATTR_MASK:
19787c478bd9Sstevel@tonic-gate 		sargp->vap->va_mask = NFS4_FS_ATTR_AT_MASK;
19797c478bd9Sstevel@tonic-gate 		sargp->sbp = sbp;
19807c478bd9Sstevel@tonic-gate 		return (NFS4_OK);
19817c478bd9Sstevel@tonic-gate 
19827c478bd9Sstevel@tonic-gate 	case NFS4_NTOV_ATTR_CACHE_MASK:
19837c478bd9Sstevel@tonic-gate 		sargp->vap->va_mask = NFS4_NTOV_ATTR_CACHE_AT_MASK;
19847c478bd9Sstevel@tonic-gate 		return (NFS4_OK);
19857c478bd9Sstevel@tonic-gate 
19867c478bd9Sstevel@tonic-gate 	case FATTR4_LEASE_TIME_MASK:
19877c478bd9Sstevel@tonic-gate 		sargp->vap->va_mask = 0;
19887c478bd9Sstevel@tonic-gate 		return (NFS4_OK);
19897c478bd9Sstevel@tonic-gate 
19907c478bd9Sstevel@tonic-gate 	default:
19917c478bd9Sstevel@tonic-gate 		va_mask = 0;
19927c478bd9Sstevel@tonic-gate 		for (i = 0; i < nfs4_ntov_map_size; i++) {
19937c478bd9Sstevel@tonic-gate 			if ((breq & nfs4_ntov_map[i].fbit) &&
19947c478bd9Sstevel@tonic-gate 							nfs4_ntov_map[i].vbit)
19957c478bd9Sstevel@tonic-gate 				va_mask |= nfs4_ntov_map[i].vbit;
19967c478bd9Sstevel@tonic-gate 		}
19977c478bd9Sstevel@tonic-gate 
19987c478bd9Sstevel@tonic-gate 		/*
19997c478bd9Sstevel@tonic-gate 		 * Check is vfsstat is needed
20007c478bd9Sstevel@tonic-gate 		 */
20017c478bd9Sstevel@tonic-gate 		if (breq & NFS4_FS_ATTR_MASK)
20027c478bd9Sstevel@tonic-gate 			sargp->sbp = sbp;
20037c478bd9Sstevel@tonic-gate 
20047c478bd9Sstevel@tonic-gate 		sargp->vap->va_mask = va_mask;
20057c478bd9Sstevel@tonic-gate 		return (NFS4_OK);
20067c478bd9Sstevel@tonic-gate 	}
20077c478bd9Sstevel@tonic-gate 	/* NOTREACHED */
20087c478bd9Sstevel@tonic-gate }
20097c478bd9Sstevel@tonic-gate 
20107c478bd9Sstevel@tonic-gate /*
20117c478bd9Sstevel@tonic-gate  * bitmap4_get_sysattrs is called by getattr and readdir.
20127c478bd9Sstevel@tonic-gate  * It calls both VOP_GETATTR and VFS_STATVFS calls to get the attrs.
20137c478bd9Sstevel@tonic-gate  * Returns nfsv4 status.
20147c478bd9Sstevel@tonic-gate  */
20157c478bd9Sstevel@tonic-gate static nfsstat4
20167c478bd9Sstevel@tonic-gate bitmap4_get_sysattrs(struct nfs4_svgetit_arg *sargp)
20177c478bd9Sstevel@tonic-gate {
20187c478bd9Sstevel@tonic-gate 	int error;
20197c478bd9Sstevel@tonic-gate 	struct compound_state *cs = sargp->cs;
20207c478bd9Sstevel@tonic-gate 	vnode_t *vp = cs->vp;
20217c478bd9Sstevel@tonic-gate 
20227c478bd9Sstevel@tonic-gate 	if (sargp->sbp != NULL) {
20237c478bd9Sstevel@tonic-gate 		if (error = VFS_STATVFS(vp->v_vfsp, sargp->sbp)) {
20247c478bd9Sstevel@tonic-gate 			sargp->sbp = NULL;	/* to identify error */
20257c478bd9Sstevel@tonic-gate 			return (puterrno4(error));
20267c478bd9Sstevel@tonic-gate 		}
20277c478bd9Sstevel@tonic-gate 	}
20287c478bd9Sstevel@tonic-gate 
20297c478bd9Sstevel@tonic-gate 	return (rfs4_vop_getattr(vp, sargp->vap, 0, cs->cr));
20307c478bd9Sstevel@tonic-gate }
20317c478bd9Sstevel@tonic-gate 
20327c478bd9Sstevel@tonic-gate static void
20337c478bd9Sstevel@tonic-gate nfs4_ntov_table_init(struct nfs4_ntov_table *ntovp)
20347c478bd9Sstevel@tonic-gate {
20357c478bd9Sstevel@tonic-gate 	ntovp->na = kmem_zalloc(sizeof (union nfs4_attr_u) * nfs4_ntov_map_size,
20367c478bd9Sstevel@tonic-gate 			KM_SLEEP);
20377c478bd9Sstevel@tonic-gate 	ntovp->attrcnt = 0;
20387c478bd9Sstevel@tonic-gate 	ntovp->vfsstat = FALSE;
20397c478bd9Sstevel@tonic-gate }
20407c478bd9Sstevel@tonic-gate 
20417c478bd9Sstevel@tonic-gate static void
20427c478bd9Sstevel@tonic-gate nfs4_ntov_table_free(struct nfs4_ntov_table *ntovp,
20437c478bd9Sstevel@tonic-gate 	struct nfs4_svgetit_arg *sargp)
20447c478bd9Sstevel@tonic-gate {
20457c478bd9Sstevel@tonic-gate 	int i;
20467c478bd9Sstevel@tonic-gate 	union nfs4_attr_u *na;
20477c478bd9Sstevel@tonic-gate 	uint8_t *amap;
20487c478bd9Sstevel@tonic-gate 
20497c478bd9Sstevel@tonic-gate 	/*
20507c478bd9Sstevel@tonic-gate 	 * XXX Should do the same checks for whether the bit is set
20517c478bd9Sstevel@tonic-gate 	 */
20527c478bd9Sstevel@tonic-gate 	for (i = 0, na = ntovp->na, amap = ntovp->amap;
20537c478bd9Sstevel@tonic-gate 		i < ntovp->attrcnt; i++, na++, amap++) {
20547c478bd9Sstevel@tonic-gate 		(void) (*nfs4_ntov_map[*amap].sv_getit)(
20557c478bd9Sstevel@tonic-gate 			NFS4ATTR_FREEIT, sargp, na);
20567c478bd9Sstevel@tonic-gate 	}
20577c478bd9Sstevel@tonic-gate 	if ((sargp->op == NFS4ATTR_SETIT) || (sargp->op == NFS4ATTR_VERIT)) {
20587c478bd9Sstevel@tonic-gate 		/*
20597c478bd9Sstevel@tonic-gate 		 * xdr_free for getattr will be done later
20607c478bd9Sstevel@tonic-gate 		 */
20617c478bd9Sstevel@tonic-gate 		for (i = 0, na = ntovp->na, amap = ntovp->amap;
20627c478bd9Sstevel@tonic-gate 			i < ntovp->attrcnt; i++, na++, amap++) {
20637c478bd9Sstevel@tonic-gate 			xdr_free(nfs4_ntov_map[*amap].xfunc, (caddr_t)na);
20647c478bd9Sstevel@tonic-gate 		}
20657c478bd9Sstevel@tonic-gate 	}
20667c478bd9Sstevel@tonic-gate 	kmem_free(ntovp->na, sizeof (union nfs4_attr_u) * nfs4_ntov_map_size);
20677c478bd9Sstevel@tonic-gate }
20687c478bd9Sstevel@tonic-gate 
20697c478bd9Sstevel@tonic-gate /*
20707c478bd9Sstevel@tonic-gate  * do_rfs4_op_getattr gets the system attrs and converts into fattr4.
20717c478bd9Sstevel@tonic-gate  */
20727c478bd9Sstevel@tonic-gate static nfsstat4
20737c478bd9Sstevel@tonic-gate do_rfs4_op_getattr(bitmap4 breq, fattr4 *fattrp,
20747c478bd9Sstevel@tonic-gate 	struct nfs4_svgetit_arg *sargp)
20757c478bd9Sstevel@tonic-gate {
20767c478bd9Sstevel@tonic-gate 	int error = 0;
20777c478bd9Sstevel@tonic-gate 	int i, k;
20787c478bd9Sstevel@tonic-gate 	struct nfs4_ntov_table ntov;
20797c478bd9Sstevel@tonic-gate 	XDR xdr;
20807c478bd9Sstevel@tonic-gate 	ulong_t xdr_size;
20817c478bd9Sstevel@tonic-gate 	char *xdr_attrs;
20827c478bd9Sstevel@tonic-gate 	nfsstat4 status = NFS4_OK;
20837c478bd9Sstevel@tonic-gate 	nfsstat4 prev_rdattr_error = sargp->rdattr_error;
20847c478bd9Sstevel@tonic-gate 	union nfs4_attr_u *na;
20857c478bd9Sstevel@tonic-gate 	uint8_t *amap;
20867c478bd9Sstevel@tonic-gate 
20877c478bd9Sstevel@tonic-gate 	sargp->op = NFS4ATTR_GETIT;
20887c478bd9Sstevel@tonic-gate 	sargp->flag = 0;
20897c478bd9Sstevel@tonic-gate 
20907c478bd9Sstevel@tonic-gate 	fattrp->attrmask = 0;
20917c478bd9Sstevel@tonic-gate 	/* if no bits requested, then return empty fattr4 */
20927c478bd9Sstevel@tonic-gate 	if (breq == 0) {
20937c478bd9Sstevel@tonic-gate 		fattrp->attrlist4_len = 0;
20947c478bd9Sstevel@tonic-gate 		fattrp->attrlist4 = NULL;
20957c478bd9Sstevel@tonic-gate 		return (NFS4_OK);
20967c478bd9Sstevel@tonic-gate 	}
20977c478bd9Sstevel@tonic-gate 
20987c478bd9Sstevel@tonic-gate 	/*
20997c478bd9Sstevel@tonic-gate 	 * return NFS4ERR_INVAL when client requests write-only attrs
21007c478bd9Sstevel@tonic-gate 	 */
21017c478bd9Sstevel@tonic-gate 	if (breq & (FATTR4_TIME_ACCESS_SET_MASK | FATTR4_TIME_MODIFY_SET_MASK))
21027c478bd9Sstevel@tonic-gate 		return (NFS4ERR_INVAL);
21037c478bd9Sstevel@tonic-gate 
21047c478bd9Sstevel@tonic-gate 	nfs4_ntov_table_init(&ntov);
21057c478bd9Sstevel@tonic-gate 	na = ntov.na;
21067c478bd9Sstevel@tonic-gate 	amap = ntov.amap;
21077c478bd9Sstevel@tonic-gate 
21087c478bd9Sstevel@tonic-gate 	/*
21097c478bd9Sstevel@tonic-gate 	 * Now loop to get or verify the attrs
21107c478bd9Sstevel@tonic-gate 	 */
21117c478bd9Sstevel@tonic-gate 	for (i = 0; i < nfs4_ntov_map_size; i++) {
21127c478bd9Sstevel@tonic-gate 		if (breq & nfs4_ntov_map[i].fbit) {
21137c478bd9Sstevel@tonic-gate 			if ((*nfs4_ntov_map[i].sv_getit)(
21147c478bd9Sstevel@tonic-gate 				    NFS4ATTR_SUPPORTED, sargp, NULL) == 0) {
21157c478bd9Sstevel@tonic-gate 
21167c478bd9Sstevel@tonic-gate 				error = (*nfs4_ntov_map[i].sv_getit)(
21177c478bd9Sstevel@tonic-gate 						NFS4ATTR_GETIT, sargp, na);
21187c478bd9Sstevel@tonic-gate 
21197c478bd9Sstevel@tonic-gate 				/*
21207c478bd9Sstevel@tonic-gate 				 * Possible error values:
21217c478bd9Sstevel@tonic-gate 				 * >0 if sv_getit failed to
21227c478bd9Sstevel@tonic-gate 				 * get the attr; 0 if succeeded;
21237c478bd9Sstevel@tonic-gate 				 * <0 if rdattr_error and the
21247c478bd9Sstevel@tonic-gate 				 * attribute cannot be returned.
21257c478bd9Sstevel@tonic-gate 				 */
21267c478bd9Sstevel@tonic-gate 				if (error && !(sargp->rdattr_error_req))
21277c478bd9Sstevel@tonic-gate 					goto done;
21287c478bd9Sstevel@tonic-gate 				/*
21297c478bd9Sstevel@tonic-gate 				 * If error then just for entry
21307c478bd9Sstevel@tonic-gate 				 */
21317c478bd9Sstevel@tonic-gate 				if (error == 0) {
21327c478bd9Sstevel@tonic-gate 					fattrp->attrmask |=
21337c478bd9Sstevel@tonic-gate 						nfs4_ntov_map[i].fbit;
21347c478bd9Sstevel@tonic-gate 					*amap++ =
21357c478bd9Sstevel@tonic-gate 						(uint8_t)nfs4_ntov_map[i].nval;
21367c478bd9Sstevel@tonic-gate 					na++;
21377c478bd9Sstevel@tonic-gate 					(ntov.attrcnt)++;
21387c478bd9Sstevel@tonic-gate 				} else if ((error > 0) &&
21397c478bd9Sstevel@tonic-gate 					(sargp->rdattr_error == NFS4_OK)) {
21407c478bd9Sstevel@tonic-gate 					sargp->rdattr_error = puterrno4(error);
21417c478bd9Sstevel@tonic-gate 				}
21427c478bd9Sstevel@tonic-gate 				error = 0;
21437c478bd9Sstevel@tonic-gate 			}
21447c478bd9Sstevel@tonic-gate 		}
21457c478bd9Sstevel@tonic-gate 	}
21467c478bd9Sstevel@tonic-gate 
21477c478bd9Sstevel@tonic-gate 	/*
21487c478bd9Sstevel@tonic-gate 	 * If rdattr_error was set after the return value for it was assigned,
21497c478bd9Sstevel@tonic-gate 	 * update it.
21507c478bd9Sstevel@tonic-gate 	 */
21517c478bd9Sstevel@tonic-gate 	if (prev_rdattr_error != sargp->rdattr_error) {
21527c478bd9Sstevel@tonic-gate 		na = ntov.na;
21537c478bd9Sstevel@tonic-gate 		amap = ntov.amap;
21547c478bd9Sstevel@tonic-gate 		for (i = 0; i < ntov.attrcnt; i++, na++, amap++) {
21557c478bd9Sstevel@tonic-gate 			k = *amap;
21567c478bd9Sstevel@tonic-gate 			if (k < FATTR4_RDATTR_ERROR) {
21577c478bd9Sstevel@tonic-gate 				continue;
21587c478bd9Sstevel@tonic-gate 			}
21597c478bd9Sstevel@tonic-gate 			if ((k == FATTR4_RDATTR_ERROR) &&
21607c478bd9Sstevel@tonic-gate 			    ((*nfs4_ntov_map[k].sv_getit)(
21617c478bd9Sstevel@tonic-gate 				NFS4ATTR_SUPPORTED, sargp, NULL) == 0)) {
21627c478bd9Sstevel@tonic-gate 
21637c478bd9Sstevel@tonic-gate 				(void) (*nfs4_ntov_map[k].sv_getit)(
21647c478bd9Sstevel@tonic-gate 						NFS4ATTR_GETIT, sargp, na);
21657c478bd9Sstevel@tonic-gate 			}
21667c478bd9Sstevel@tonic-gate 			break;
21677c478bd9Sstevel@tonic-gate 		}
21687c478bd9Sstevel@tonic-gate 	}
21697c478bd9Sstevel@tonic-gate 
21707c478bd9Sstevel@tonic-gate 	xdr_size = 0;
21717c478bd9Sstevel@tonic-gate 	na = ntov.na;
21727c478bd9Sstevel@tonic-gate 	amap = ntov.amap;
21737c478bd9Sstevel@tonic-gate 	for (i = 0; i < ntov.attrcnt; i++, na++, amap++) {
21747c478bd9Sstevel@tonic-gate 		xdr_size += xdr_sizeof(nfs4_ntov_map[*amap].xfunc, na);
21757c478bd9Sstevel@tonic-gate 	}
21767c478bd9Sstevel@tonic-gate 
21777c478bd9Sstevel@tonic-gate 	fattrp->attrlist4_len = xdr_size;
21787c478bd9Sstevel@tonic-gate 	if (xdr_size) {
21797c478bd9Sstevel@tonic-gate 		/* freed by rfs4_op_getattr_free() */
21807c478bd9Sstevel@tonic-gate 		fattrp->attrlist4 = xdr_attrs = kmem_zalloc(xdr_size, KM_SLEEP);
21817c478bd9Sstevel@tonic-gate 
21827c478bd9Sstevel@tonic-gate 		xdrmem_create(&xdr, xdr_attrs, xdr_size, XDR_ENCODE);
21837c478bd9Sstevel@tonic-gate 
21847c478bd9Sstevel@tonic-gate 		na = ntov.na;
21857c478bd9Sstevel@tonic-gate 		amap = ntov.amap;
21867c478bd9Sstevel@tonic-gate 		for (i = 0; i < ntov.attrcnt; i++, na++, amap++) {
21877c478bd9Sstevel@tonic-gate 			if (!(*nfs4_ntov_map[*amap].xfunc)(&xdr, na)) {
21887c478bd9Sstevel@tonic-gate 				cmn_err(CE_WARN, "do_rfs4_op_getattr: xdr "
21897c478bd9Sstevel@tonic-gate 					"encode of attribute %d failed\n",
21907c478bd9Sstevel@tonic-gate 					*amap);
21917c478bd9Sstevel@tonic-gate 				status = NFS4ERR_SERVERFAULT;
21927c478bd9Sstevel@tonic-gate 				break;
21937c478bd9Sstevel@tonic-gate 			}
21947c478bd9Sstevel@tonic-gate 		}
21957c478bd9Sstevel@tonic-gate 		/* xdrmem_destroy(&xdrs); */	/* NO-OP */
21967c478bd9Sstevel@tonic-gate 	} else {
21977c478bd9Sstevel@tonic-gate 		fattrp->attrlist4 = NULL;
21987c478bd9Sstevel@tonic-gate 	}
21997c478bd9Sstevel@tonic-gate done:
22007c478bd9Sstevel@tonic-gate 
22017c478bd9Sstevel@tonic-gate 	nfs4_ntov_table_free(&ntov, sargp);
22027c478bd9Sstevel@tonic-gate 
22037c478bd9Sstevel@tonic-gate 	if (error != 0)
22047c478bd9Sstevel@tonic-gate 		status = puterrno4(error);
22057c478bd9Sstevel@tonic-gate 
22067c478bd9Sstevel@tonic-gate 	return (status);
22077c478bd9Sstevel@tonic-gate }
22087c478bd9Sstevel@tonic-gate 
22097c478bd9Sstevel@tonic-gate /* ARGSUSED */
22107c478bd9Sstevel@tonic-gate static void
22117c478bd9Sstevel@tonic-gate rfs4_op_getattr(nfs_argop4 *argop, nfs_resop4 *resop, struct svc_req *req,
22127c478bd9Sstevel@tonic-gate 	struct compound_state *cs)
22137c478bd9Sstevel@tonic-gate {
22147c478bd9Sstevel@tonic-gate 	GETATTR4args *args = &argop->nfs_argop4_u.opgetattr;
22157c478bd9Sstevel@tonic-gate 	GETATTR4res *resp = &resop->nfs_resop4_u.opgetattr;
22167c478bd9Sstevel@tonic-gate 	struct nfs4_svgetit_arg sarg;
22177c478bd9Sstevel@tonic-gate 	struct statvfs64 sb;
22187c478bd9Sstevel@tonic-gate 	nfsstat4 status;
22197c478bd9Sstevel@tonic-gate 
22207c478bd9Sstevel@tonic-gate 	if (cs->vp == NULL) {
22217c478bd9Sstevel@tonic-gate 		*cs->statusp = resp->status = NFS4ERR_NOFILEHANDLE;
22227c478bd9Sstevel@tonic-gate 		return;
22237c478bd9Sstevel@tonic-gate 	}
22247c478bd9Sstevel@tonic-gate 
22257c478bd9Sstevel@tonic-gate 	if (cs->access == CS_ACCESS_DENIED) {
22267c478bd9Sstevel@tonic-gate 		*cs->statusp = resp->status = NFS4ERR_ACCESS;
22277c478bd9Sstevel@tonic-gate 		return;
22287c478bd9Sstevel@tonic-gate 	}
22297c478bd9Sstevel@tonic-gate 
22307c478bd9Sstevel@tonic-gate 	sarg.sbp = &sb;
22317c478bd9Sstevel@tonic-gate 	sarg.cs = cs;
22327c478bd9Sstevel@tonic-gate 
22337c478bd9Sstevel@tonic-gate 	status = bitmap4_to_attrmask(args->attr_request, &sarg);
22347c478bd9Sstevel@tonic-gate 	if (status == NFS4_OK) {
22357c478bd9Sstevel@tonic-gate 		status = bitmap4_get_sysattrs(&sarg);
22367c478bd9Sstevel@tonic-gate 		if (status == NFS4_OK)
22377c478bd9Sstevel@tonic-gate 			status = do_rfs4_op_getattr(args->attr_request,
22387c478bd9Sstevel@tonic-gate 				&resp->obj_attributes, &sarg);
22397c478bd9Sstevel@tonic-gate 	}
22407c478bd9Sstevel@tonic-gate 	*cs->statusp = resp->status = status;
22417c478bd9Sstevel@tonic-gate }
22427c478bd9Sstevel@tonic-gate 
22437c478bd9Sstevel@tonic-gate static void
22447c478bd9Sstevel@tonic-gate rfs4_op_getattr_free(nfs_resop4 *resop)
22457c478bd9Sstevel@tonic-gate {
22467c478bd9Sstevel@tonic-gate 	GETATTR4res *resp = &resop->nfs_resop4_u.opgetattr;
22477c478bd9Sstevel@tonic-gate 
22487c478bd9Sstevel@tonic-gate 	nfs4_fattr4_free(&resp->obj_attributes);
22497c478bd9Sstevel@tonic-gate }
22507c478bd9Sstevel@tonic-gate 
22517c478bd9Sstevel@tonic-gate /* ARGSUSED */
22527c478bd9Sstevel@tonic-gate static void
22537c478bd9Sstevel@tonic-gate rfs4_op_getfh(nfs_argop4 *argop, nfs_resop4 *resop, struct svc_req *req,
22547c478bd9Sstevel@tonic-gate 	struct compound_state *cs)
22557c478bd9Sstevel@tonic-gate {
22567c478bd9Sstevel@tonic-gate 	GETFH4res *resp = &resop->nfs_resop4_u.opgetfh;
22577c478bd9Sstevel@tonic-gate 
22587c478bd9Sstevel@tonic-gate 	if (cs->vp == NULL) {
22597c478bd9Sstevel@tonic-gate 		*cs->statusp = resp->status = NFS4ERR_NOFILEHANDLE;
22607c478bd9Sstevel@tonic-gate 		return;
22617c478bd9Sstevel@tonic-gate 	}
22627c478bd9Sstevel@tonic-gate 	if (cs->access == CS_ACCESS_DENIED) {
22637c478bd9Sstevel@tonic-gate 		*cs->statusp = resp->status = NFS4ERR_ACCESS;
22647c478bd9Sstevel@tonic-gate 		return;
22657c478bd9Sstevel@tonic-gate 	}
22667c478bd9Sstevel@tonic-gate 
22677c478bd9Sstevel@tonic-gate 	resp->object.nfs_fh4_val =
22687c478bd9Sstevel@tonic-gate 		kmem_alloc(cs->fh.nfs_fh4_len, KM_SLEEP);
22697c478bd9Sstevel@tonic-gate 	nfs_fh4_copy(&cs->fh, &resp->object);
22707c478bd9Sstevel@tonic-gate 	*cs->statusp = resp->status = NFS4_OK;
22717c478bd9Sstevel@tonic-gate }
22727c478bd9Sstevel@tonic-gate 
22737c478bd9Sstevel@tonic-gate static void
22747c478bd9Sstevel@tonic-gate rfs4_op_getfh_free(nfs_resop4 *resop)
22757c478bd9Sstevel@tonic-gate {
22767c478bd9Sstevel@tonic-gate 	GETFH4res *resp = &resop->nfs_resop4_u.opgetfh;
22777c478bd9Sstevel@tonic-gate 
22787c478bd9Sstevel@tonic-gate 	if (resp->status == NFS4_OK &&
22797c478bd9Sstevel@tonic-gate 	    resp->object.nfs_fh4_val != NULL) {
22807c478bd9Sstevel@tonic-gate 		kmem_free(resp->object.nfs_fh4_val, resp->object.nfs_fh4_len);
22817c478bd9Sstevel@tonic-gate 		resp->object.nfs_fh4_val = NULL;
22827c478bd9Sstevel@tonic-gate 		resp->object.nfs_fh4_len = 0;
22837c478bd9Sstevel@tonic-gate 	}
22847c478bd9Sstevel@tonic-gate }
22857c478bd9Sstevel@tonic-gate 
22867c478bd9Sstevel@tonic-gate /*
22877c478bd9Sstevel@tonic-gate  * illegal: args: void
22887c478bd9Sstevel@tonic-gate  *	    res : status (NFS4ERR_OP_ILLEGAL)
22897c478bd9Sstevel@tonic-gate  */
22907c478bd9Sstevel@tonic-gate /* ARGSUSED */
22917c478bd9Sstevel@tonic-gate static void
22927c478bd9Sstevel@tonic-gate rfs4_op_illegal(nfs_argop4 *argop, nfs_resop4 *resop,
22937c478bd9Sstevel@tonic-gate 	struct svc_req *req, struct compound_state *cs)
22947c478bd9Sstevel@tonic-gate {
22957c478bd9Sstevel@tonic-gate 	ILLEGAL4res *resp = &resop->nfs_resop4_u.opillegal;
22967c478bd9Sstevel@tonic-gate 
22977c478bd9Sstevel@tonic-gate 	resop->resop = OP_ILLEGAL;
22987c478bd9Sstevel@tonic-gate 	*cs->statusp = resp->status = NFS4ERR_OP_ILLEGAL;
22997c478bd9Sstevel@tonic-gate }
23007c478bd9Sstevel@tonic-gate 
23017c478bd9Sstevel@tonic-gate /*
23027c478bd9Sstevel@tonic-gate  * link: args: SAVED_FH: file, CURRENT_FH: target directory
23037c478bd9Sstevel@tonic-gate  *	 res: status. If success - CURRENT_FH unchanged, return change_info
23047c478bd9Sstevel@tonic-gate  */
23057c478bd9Sstevel@tonic-gate /* ARGSUSED */
23067c478bd9Sstevel@tonic-gate static void
23077c478bd9Sstevel@tonic-gate rfs4_op_link(nfs_argop4 *argop, nfs_resop4 *resop, struct svc_req *req,
23087c478bd9Sstevel@tonic-gate 	struct compound_state *cs)
23097c478bd9Sstevel@tonic-gate {
23107c478bd9Sstevel@tonic-gate 	LINK4args *args = &argop->nfs_argop4_u.oplink;
23117c478bd9Sstevel@tonic-gate 	LINK4res *resp = &resop->nfs_resop4_u.oplink;
23127c478bd9Sstevel@tonic-gate 	int error;
23137c478bd9Sstevel@tonic-gate 	vnode_t *vp;
23147c478bd9Sstevel@tonic-gate 	vnode_t *dvp;
23157c478bd9Sstevel@tonic-gate 	struct vattr bdva, idva, adva;
23167c478bd9Sstevel@tonic-gate 	char *nm;
23177c478bd9Sstevel@tonic-gate 	uint_t  len;
23187c478bd9Sstevel@tonic-gate 
23197c478bd9Sstevel@tonic-gate 	/* SAVED_FH: source object */
23207c478bd9Sstevel@tonic-gate 	vp = cs->saved_vp;
23217c478bd9Sstevel@tonic-gate 	if (vp == NULL) {
23227c478bd9Sstevel@tonic-gate 		*cs->statusp = resp->status = NFS4ERR_NOFILEHANDLE;
23237c478bd9Sstevel@tonic-gate 		return;
23247c478bd9Sstevel@tonic-gate 	}
23257c478bd9Sstevel@tonic-gate 
23267c478bd9Sstevel@tonic-gate 	/* CURRENT_FH: target directory */
23277c478bd9Sstevel@tonic-gate 	dvp = cs->vp;
23287c478bd9Sstevel@tonic-gate 	if (dvp == NULL) {
23297c478bd9Sstevel@tonic-gate 		*cs->statusp = resp->status = NFS4ERR_NOFILEHANDLE;
23307c478bd9Sstevel@tonic-gate 		return;
23317c478bd9Sstevel@tonic-gate 	}
23327c478bd9Sstevel@tonic-gate 
23337c478bd9Sstevel@tonic-gate 	/*
23347c478bd9Sstevel@tonic-gate 	 * If there is a non-shared filesystem mounted on this vnode,
23357c478bd9Sstevel@tonic-gate 	 * do not allow to link any file in this directory.
23367c478bd9Sstevel@tonic-gate 	 */
23377c478bd9Sstevel@tonic-gate 	if (vn_ismntpt(dvp)) {
23387c478bd9Sstevel@tonic-gate 		*cs->statusp = resp->status = NFS4ERR_ACCESS;
23397c478bd9Sstevel@tonic-gate 		return;
23407c478bd9Sstevel@tonic-gate 	}
23417c478bd9Sstevel@tonic-gate 
23427c478bd9Sstevel@tonic-gate 	if (cs->access == CS_ACCESS_DENIED) {
23437c478bd9Sstevel@tonic-gate 		*cs->statusp = resp->status = NFS4ERR_ACCESS;
23447c478bd9Sstevel@tonic-gate 		return;
23457c478bd9Sstevel@tonic-gate 	}
23467c478bd9Sstevel@tonic-gate 
23477c478bd9Sstevel@tonic-gate 	/* Check source object's type validity */
23487c478bd9Sstevel@tonic-gate 	if (vp->v_type == VDIR) {
23497c478bd9Sstevel@tonic-gate 		*cs->statusp = resp->status = NFS4ERR_ISDIR;
23507c478bd9Sstevel@tonic-gate 		return;
23517c478bd9Sstevel@tonic-gate 	}
23527c478bd9Sstevel@tonic-gate 
23537c478bd9Sstevel@tonic-gate 	/* Check target directory's type */
23547c478bd9Sstevel@tonic-gate 	if (dvp->v_type != VDIR) {
23557c478bd9Sstevel@tonic-gate 		*cs->statusp = resp->status = NFS4ERR_NOTDIR;
23567c478bd9Sstevel@tonic-gate 		return;
23577c478bd9Sstevel@tonic-gate 	}
23587c478bd9Sstevel@tonic-gate 
23597c478bd9Sstevel@tonic-gate 	if (cs->saved_exi != cs->exi) {
23607c478bd9Sstevel@tonic-gate 		*cs->statusp = resp->status = NFS4ERR_XDEV;
23617c478bd9Sstevel@tonic-gate 		return;
23627c478bd9Sstevel@tonic-gate 	}
23637c478bd9Sstevel@tonic-gate 
23647c478bd9Sstevel@tonic-gate 	if (!utf8_dir_verify(&args->newname)) {
23657c478bd9Sstevel@tonic-gate 		*cs->statusp = resp->status = NFS4ERR_INVAL;
23667c478bd9Sstevel@tonic-gate 		return;
23677c478bd9Sstevel@tonic-gate 	}
23687c478bd9Sstevel@tonic-gate 
23697c478bd9Sstevel@tonic-gate 	nm = utf8_to_fn(&args->newname, &len, NULL);
23707c478bd9Sstevel@tonic-gate 	if (nm == NULL) {
23717c478bd9Sstevel@tonic-gate 		*cs->statusp = resp->status = NFS4ERR_INVAL;
23727c478bd9Sstevel@tonic-gate 		return;
23737c478bd9Sstevel@tonic-gate 	}
23747c478bd9Sstevel@tonic-gate 
23757c478bd9Sstevel@tonic-gate 	if (len > MAXNAMELEN) {
23767c478bd9Sstevel@tonic-gate 		*cs->statusp = resp->status = NFS4ERR_NAMETOOLONG;
23777c478bd9Sstevel@tonic-gate 		kmem_free(nm, len);
23787c478bd9Sstevel@tonic-gate 		return;
23797c478bd9Sstevel@tonic-gate 	}
23807c478bd9Sstevel@tonic-gate 
23817c478bd9Sstevel@tonic-gate 	if (rdonly4(cs->exi, cs->vp, req)) {
23827c478bd9Sstevel@tonic-gate 		*cs->statusp = resp->status = NFS4ERR_ROFS;
23837c478bd9Sstevel@tonic-gate 		kmem_free(nm, len);
23847c478bd9Sstevel@tonic-gate 		return;
23857c478bd9Sstevel@tonic-gate 	}
23867c478bd9Sstevel@tonic-gate 
23877c478bd9Sstevel@tonic-gate 	/* Get "before" change value */
23887c478bd9Sstevel@tonic-gate 	bdva.va_mask = AT_CTIME|AT_SEQ;
23897c478bd9Sstevel@tonic-gate 	error = VOP_GETATTR(dvp, &bdva, 0, cs->cr);
23907c478bd9Sstevel@tonic-gate 	if (error) {
23917c478bd9Sstevel@tonic-gate 		*cs->statusp = resp->status = puterrno4(error);
23927c478bd9Sstevel@tonic-gate 		kmem_free(nm, len);
23937c478bd9Sstevel@tonic-gate 		return;
23947c478bd9Sstevel@tonic-gate 	}
23957c478bd9Sstevel@tonic-gate 
23967c478bd9Sstevel@tonic-gate 	NFS4_SET_FATTR4_CHANGE(resp->cinfo.before, bdva.va_ctime)
23977c478bd9Sstevel@tonic-gate 
23987c478bd9Sstevel@tonic-gate 	error = VOP_LINK(dvp, vp, nm, cs->cr);
23997c478bd9Sstevel@tonic-gate 
24007c478bd9Sstevel@tonic-gate 	kmem_free(nm, len);
24017c478bd9Sstevel@tonic-gate 
24027c478bd9Sstevel@tonic-gate 	/*
24037c478bd9Sstevel@tonic-gate 	 * Get the initial "after" sequence number, if it fails, set to zero
24047c478bd9Sstevel@tonic-gate 	 */
24057c478bd9Sstevel@tonic-gate 	idva.va_mask = AT_SEQ;
24067c478bd9Sstevel@tonic-gate 	if (VOP_GETATTR(dvp, &idva, 0, cs->cr))
24077c478bd9Sstevel@tonic-gate 		idva.va_seq = 0;
24087c478bd9Sstevel@tonic-gate 
24097c478bd9Sstevel@tonic-gate 	/*
24107c478bd9Sstevel@tonic-gate 	 * Force modified data and metadata out to stable storage.
24117c478bd9Sstevel@tonic-gate 	 */
24127c478bd9Sstevel@tonic-gate 	(void) VOP_FSYNC(vp, FNODSYNC, cs->cr);
24137c478bd9Sstevel@tonic-gate 	(void) VOP_FSYNC(dvp, 0, cs->cr);
24147c478bd9Sstevel@tonic-gate 
24157c478bd9Sstevel@tonic-gate 	if (error) {
24167c478bd9Sstevel@tonic-gate 		*cs->statusp = resp->status = puterrno4(error);
24177c478bd9Sstevel@tonic-gate 		return;
24187c478bd9Sstevel@tonic-gate 	}
24197c478bd9Sstevel@tonic-gate 
24207c478bd9Sstevel@tonic-gate 	/*
24217c478bd9Sstevel@tonic-gate 	 * Get "after" change value, if it fails, simply return the
24227c478bd9Sstevel@tonic-gate 	 * before value.
24237c478bd9Sstevel@tonic-gate 	 */
24247c478bd9Sstevel@tonic-gate 	adva.va_mask = AT_CTIME|AT_SEQ;
24257c478bd9Sstevel@tonic-gate 	if (VOP_GETATTR(dvp, &adva, 0, cs->cr)) {
24267c478bd9Sstevel@tonic-gate 		adva.va_ctime = bdva.va_ctime;
24277c478bd9Sstevel@tonic-gate 		adva.va_seq = 0;
24287c478bd9Sstevel@tonic-gate 	}
24297c478bd9Sstevel@tonic-gate 
24307c478bd9Sstevel@tonic-gate 	NFS4_SET_FATTR4_CHANGE(resp->cinfo.after, adva.va_ctime)
24317c478bd9Sstevel@tonic-gate 
24327c478bd9Sstevel@tonic-gate 	/*
24337c478bd9Sstevel@tonic-gate 	 * The cinfo.atomic = TRUE only if we have
24347c478bd9Sstevel@tonic-gate 	 * non-zero va_seq's, and it has incremented by exactly one
24357c478bd9Sstevel@tonic-gate 	 * during the VOP_LINK and it didn't change during the VOP_FSYNC.
24367c478bd9Sstevel@tonic-gate 	 */
24377c478bd9Sstevel@tonic-gate 	if (bdva.va_seq && idva.va_seq && adva.va_seq &&
24387c478bd9Sstevel@tonic-gate 			idva.va_seq == (bdva.va_seq + 1) &&
24397c478bd9Sstevel@tonic-gate 			idva.va_seq == adva.va_seq)
24407c478bd9Sstevel@tonic-gate 		resp->cinfo.atomic = TRUE;
24417c478bd9Sstevel@tonic-gate 	else
24427c478bd9Sstevel@tonic-gate 		resp->cinfo.atomic = FALSE;
24437c478bd9Sstevel@tonic-gate 
24447c478bd9Sstevel@tonic-gate 	*cs->statusp = resp->status = NFS4_OK;
24457c478bd9Sstevel@tonic-gate }
24467c478bd9Sstevel@tonic-gate 
24477c478bd9Sstevel@tonic-gate /*
24487c478bd9Sstevel@tonic-gate  * Used by rfs4_op_lookup and rfs4_op_lookupp to do the actual work.
24497c478bd9Sstevel@tonic-gate  */
24507c478bd9Sstevel@tonic-gate 
24517c478bd9Sstevel@tonic-gate /* ARGSUSED */
24527c478bd9Sstevel@tonic-gate static nfsstat4
24537c478bd9Sstevel@tonic-gate do_rfs4_op_lookup(char *nm, uint_t buflen, struct svc_req *req,
24547c478bd9Sstevel@tonic-gate 	struct compound_state *cs)
24557c478bd9Sstevel@tonic-gate {
24567c478bd9Sstevel@tonic-gate 	int error;
24577c478bd9Sstevel@tonic-gate 	int different_export = 0;
24587c478bd9Sstevel@tonic-gate 	vnode_t *vp, *tvp, *pre_tvp = NULL, *oldvp = NULL;
24597c478bd9Sstevel@tonic-gate 	struct exportinfo *exi = NULL, *pre_exi = NULL;
24607c478bd9Sstevel@tonic-gate 	nfsstat4 stat;
24617c478bd9Sstevel@tonic-gate 	fid_t fid;
24627c478bd9Sstevel@tonic-gate 	int attrdir, dotdot, walk;
24637c478bd9Sstevel@tonic-gate 	bool_t is_newvp = FALSE;
24647c478bd9Sstevel@tonic-gate 
24657c478bd9Sstevel@tonic-gate 	if (cs->vp->v_flag & V_XATTRDIR) {
24667c478bd9Sstevel@tonic-gate 		attrdir = 1;
24677c478bd9Sstevel@tonic-gate 		ASSERT(get_fh4_flag(&cs->fh, FH4_ATTRDIR));
24687c478bd9Sstevel@tonic-gate 	} else {
24697c478bd9Sstevel@tonic-gate 		attrdir = 0;
24707c478bd9Sstevel@tonic-gate 		ASSERT(! get_fh4_flag(&cs->fh, FH4_ATTRDIR));
24717c478bd9Sstevel@tonic-gate 	}
24727c478bd9Sstevel@tonic-gate 
24737c478bd9Sstevel@tonic-gate 	dotdot = (nm[0] == '.' && nm[1] == '.' && nm[2] == '\0');
24747c478bd9Sstevel@tonic-gate 
24757c478bd9Sstevel@tonic-gate 	/*
24767c478bd9Sstevel@tonic-gate 	 * If dotdotting, then need to check whether it's
24777c478bd9Sstevel@tonic-gate 	 * above the root of a filesystem, or above an
24787c478bd9Sstevel@tonic-gate 	 * export point.
24797c478bd9Sstevel@tonic-gate 	 */
24807c478bd9Sstevel@tonic-gate 	if (dotdot) {
24817c478bd9Sstevel@tonic-gate 
24827c478bd9Sstevel@tonic-gate 		/*
24837c478bd9Sstevel@tonic-gate 		 * If dotdotting at the root of a filesystem, then
24847c478bd9Sstevel@tonic-gate 		 * need to traverse back to the mounted-on filesystem
24857c478bd9Sstevel@tonic-gate 		 * and do the dotdot lookup there.
24867c478bd9Sstevel@tonic-gate 		 */
24877c478bd9Sstevel@tonic-gate 		if (cs->vp->v_flag & VROOT) {
24887c478bd9Sstevel@tonic-gate 
24897c478bd9Sstevel@tonic-gate 			/*
24907c478bd9Sstevel@tonic-gate 			 * If at the system root, then can
24917c478bd9Sstevel@tonic-gate 			 * go up no further.
24927c478bd9Sstevel@tonic-gate 			 */
24937c478bd9Sstevel@tonic-gate 			if (VN_CMP(cs->vp, rootdir))
24947c478bd9Sstevel@tonic-gate 				return (puterrno4(ENOENT));
24957c478bd9Sstevel@tonic-gate 
24967c478bd9Sstevel@tonic-gate 			/*
24977c478bd9Sstevel@tonic-gate 			 * Traverse back to the mounted-on filesystem
24987c478bd9Sstevel@tonic-gate 			 */
24997c478bd9Sstevel@tonic-gate 			cs->vp = untraverse(cs->vp);
25007c478bd9Sstevel@tonic-gate 
25017c478bd9Sstevel@tonic-gate 			/*
25027c478bd9Sstevel@tonic-gate 			 * Set the different_export flag so we remember
25037c478bd9Sstevel@tonic-gate 			 * to pick up a new exportinfo entry for
25047c478bd9Sstevel@tonic-gate 			 * this new filesystem.
25057c478bd9Sstevel@tonic-gate 			 */
25067c478bd9Sstevel@tonic-gate 			different_export = 1;
25077c478bd9Sstevel@tonic-gate 		} else {
25087c478bd9Sstevel@tonic-gate 
25097c478bd9Sstevel@tonic-gate 			/*
25107c478bd9Sstevel@tonic-gate 			 * If dotdotting above an export point then set
25117c478bd9Sstevel@tonic-gate 			 * the different_export to get new export info.
25127c478bd9Sstevel@tonic-gate 			 */
25137c478bd9Sstevel@tonic-gate 			different_export = nfs_exported(cs->exi, cs->vp);
25147c478bd9Sstevel@tonic-gate 		}
25157c478bd9Sstevel@tonic-gate 	}
25167c478bd9Sstevel@tonic-gate 
25177c478bd9Sstevel@tonic-gate 	error = VOP_LOOKUP(cs->vp, nm, &vp, NULL, 0, NULL, cs->cr);
25187c478bd9Sstevel@tonic-gate 	if (error)
25197c478bd9Sstevel@tonic-gate 		return (puterrno4(error));
25207c478bd9Sstevel@tonic-gate 
25217c478bd9Sstevel@tonic-gate 	/*
25227c478bd9Sstevel@tonic-gate 	 * If the vnode is in a pseudo filesystem, check whether it is visible.
25237c478bd9Sstevel@tonic-gate 	 *
25247c478bd9Sstevel@tonic-gate 	 * XXX if the vnode is a symlink and it is not visible in
25257c478bd9Sstevel@tonic-gate 	 * a pseudo filesystem, return ENOENT (not following symlink).
25267c478bd9Sstevel@tonic-gate 	 * V4 client can not mount such symlink. This is a regression
25277c478bd9Sstevel@tonic-gate 	 * from V2/V3.
25287c478bd9Sstevel@tonic-gate 	 *
25297c478bd9Sstevel@tonic-gate 	 * In the same exported filesystem, if the security flavor used
25307c478bd9Sstevel@tonic-gate 	 * is not an explicitly shared flavor, limit the view to the visible
25317c478bd9Sstevel@tonic-gate 	 * list entries only. This is not a WRONGSEC case because it's already
25327c478bd9Sstevel@tonic-gate 	 * checked via PUTROOTFH/PUTPUBFH or PUTFH.
25337c478bd9Sstevel@tonic-gate 	 */
25347c478bd9Sstevel@tonic-gate 	if (!different_export &&
25357c478bd9Sstevel@tonic-gate 	    (PSEUDO(cs->exi) || ! is_exported_sec(cs->nfsflavor, cs->exi) ||
25367c478bd9Sstevel@tonic-gate 	    cs->access & CS_ACCESS_LIMITED)) {
25377c478bd9Sstevel@tonic-gate 		if (! nfs_visible(cs->exi, vp, &different_export)) {
25387c478bd9Sstevel@tonic-gate 			VN_RELE(vp);
25397c478bd9Sstevel@tonic-gate 			return (puterrno4(ENOENT));
25407c478bd9Sstevel@tonic-gate 		}
25417c478bd9Sstevel@tonic-gate 	}
25427c478bd9Sstevel@tonic-gate 
25437c478bd9Sstevel@tonic-gate 	/*
25447c478bd9Sstevel@tonic-gate 	 * If it's a mountpoint, then traverse it.
25457c478bd9Sstevel@tonic-gate 	 */
25467c478bd9Sstevel@tonic-gate 	if (vn_ismntpt(vp)) {
25477c478bd9Sstevel@tonic-gate 		pre_exi = cs->exi;	/* save pre-traversed exportinfo */
25487c478bd9Sstevel@tonic-gate 		pre_tvp = vp;		/* save pre-traversed vnode	*/
25497c478bd9Sstevel@tonic-gate 
25507c478bd9Sstevel@tonic-gate 		/*
25517c478bd9Sstevel@tonic-gate 		 * hold pre_tvp to counteract rele by traverse.  We will
25527c478bd9Sstevel@tonic-gate 		 * need pre_tvp below if checkexport4 fails
25537c478bd9Sstevel@tonic-gate 		 */
25547c478bd9Sstevel@tonic-gate 		VN_HOLD(pre_tvp);
25557c478bd9Sstevel@tonic-gate 		tvp = vp;
25567c478bd9Sstevel@tonic-gate 		if ((error = traverse(&tvp)) != 0) {
25577c478bd9Sstevel@tonic-gate 			VN_RELE(vp);
25587c478bd9Sstevel@tonic-gate 			VN_RELE(pre_tvp);
25597c478bd9Sstevel@tonic-gate 			return (puterrno4(error));
25607c478bd9Sstevel@tonic-gate 		}
25617c478bd9Sstevel@tonic-gate 		vp = tvp;
25627c478bd9Sstevel@tonic-gate 		different_export = 1;
25637c478bd9Sstevel@tonic-gate 	} else if (vp->v_vfsp != cs->vp->v_vfsp) {
25647c478bd9Sstevel@tonic-gate 		/*
25657c478bd9Sstevel@tonic-gate 		 * The vfsp comparison is to handle the case where
25667c478bd9Sstevel@tonic-gate 		 * a LOFS mount is shared.  lo_lookup traverses mount points,
25677c478bd9Sstevel@tonic-gate 		 * and NFS is unaware of local fs transistions because
25687c478bd9Sstevel@tonic-gate 		 * v_vfsmountedhere isn't set.  For this special LOFS case,
25697c478bd9Sstevel@tonic-gate 		 * the dir and the obj returned by lookup will have different
25707c478bd9Sstevel@tonic-gate 		 * vfs ptrs.
25717c478bd9Sstevel@tonic-gate 		 */
25727c478bd9Sstevel@tonic-gate 		different_export = 1;
25737c478bd9Sstevel@tonic-gate 	}
25747c478bd9Sstevel@tonic-gate 
25757c478bd9Sstevel@tonic-gate 	if (different_export) {
25767c478bd9Sstevel@tonic-gate 
25777c478bd9Sstevel@tonic-gate 		bzero(&fid, sizeof (fid));
25787c478bd9Sstevel@tonic-gate 		fid.fid_len = MAXFIDSZ;
25797c478bd9Sstevel@tonic-gate 		error = vop_fid_pseudo(vp, &fid);
25807c478bd9Sstevel@tonic-gate 		if (error) {
25817c478bd9Sstevel@tonic-gate 			VN_RELE(vp);
25827c478bd9Sstevel@tonic-gate 			if (pre_tvp)
25837c478bd9Sstevel@tonic-gate 				VN_RELE(pre_tvp);
25847c478bd9Sstevel@tonic-gate 			return (puterrno4(error));
25857c478bd9Sstevel@tonic-gate 		}
25867c478bd9Sstevel@tonic-gate 
25877c478bd9Sstevel@tonic-gate 		if (dotdot)
25887c478bd9Sstevel@tonic-gate 			exi = nfs_vptoexi(NULL, vp, cs->cr, &walk, NULL, TRUE);
25897c478bd9Sstevel@tonic-gate 		else
25907c478bd9Sstevel@tonic-gate 			exi = checkexport4(&vp->v_vfsp->vfs_fsid, &fid, vp);
25917c478bd9Sstevel@tonic-gate 
25927c478bd9Sstevel@tonic-gate 		if (exi == NULL) {
25937c478bd9Sstevel@tonic-gate 			if (pre_tvp) {
25947c478bd9Sstevel@tonic-gate 				/*
25957c478bd9Sstevel@tonic-gate 				 * If this vnode is a mounted-on vnode,
25967c478bd9Sstevel@tonic-gate 				 * but the mounted-on file system is not
25977c478bd9Sstevel@tonic-gate 				 * exported, send back the filehandle for
25987c478bd9Sstevel@tonic-gate 				 * the mounted-on vnode, not the root of
25997c478bd9Sstevel@tonic-gate 				 * the mounted-on file system.
26007c478bd9Sstevel@tonic-gate 				 */
26017c478bd9Sstevel@tonic-gate 				VN_RELE(vp);
26027c478bd9Sstevel@tonic-gate 				vp = pre_tvp;
26037c478bd9Sstevel@tonic-gate 				exi = pre_exi;
26047c478bd9Sstevel@tonic-gate 			} else {
26057c478bd9Sstevel@tonic-gate 				VN_RELE(vp);
26067c478bd9Sstevel@tonic-gate 				return (puterrno4(EACCES));
26077c478bd9Sstevel@tonic-gate 			}
26087c478bd9Sstevel@tonic-gate 		} else if (pre_tvp) {
26097c478bd9Sstevel@tonic-gate 			/* we're done with pre_tvp now. release extra hold */
26107c478bd9Sstevel@tonic-gate 			VN_RELE(pre_tvp);
26117c478bd9Sstevel@tonic-gate 		}
26127c478bd9Sstevel@tonic-gate 
26137c478bd9Sstevel@tonic-gate 		cs->exi = exi;
26147c478bd9Sstevel@tonic-gate 
26157c478bd9Sstevel@tonic-gate 		/*
26167c478bd9Sstevel@tonic-gate 		 * Now we do a checkauth4. The reason is that
26177c478bd9Sstevel@tonic-gate 		 * this client/user may not have access to the new
26187c478bd9Sstevel@tonic-gate 		 * exported file system, and if he does,
26197c478bd9Sstevel@tonic-gate 		 * the client/user may be mapped to a different uid.
26207c478bd9Sstevel@tonic-gate 		 *
26217c478bd9Sstevel@tonic-gate 		 * We start with a new cr, because the checkauth4 done
26227c478bd9Sstevel@tonic-gate 		 * in the PUT*FH operation over wrote the cred's uid,
26237c478bd9Sstevel@tonic-gate 		 * gid, etc, and we want the real thing before calling
26247c478bd9Sstevel@tonic-gate 		 * checkauth4()
26257c478bd9Sstevel@tonic-gate 		 */
26267c478bd9Sstevel@tonic-gate 		crfree(cs->cr);
26277c478bd9Sstevel@tonic-gate 		cs->cr = crdup(cs->basecr);
26287c478bd9Sstevel@tonic-gate 
26297c478bd9Sstevel@tonic-gate 		if (cs->vp)
26307c478bd9Sstevel@tonic-gate 			oldvp = cs->vp;
26317c478bd9Sstevel@tonic-gate 		cs->vp = vp;
26327c478bd9Sstevel@tonic-gate 		is_newvp = TRUE;
26337c478bd9Sstevel@tonic-gate 
26347c478bd9Sstevel@tonic-gate 		stat = call_checkauth4(cs, req);
26357c478bd9Sstevel@tonic-gate 		if (stat != NFS4_OK) {
26367c478bd9Sstevel@tonic-gate 			VN_RELE(cs->vp);
26377c478bd9Sstevel@tonic-gate 			cs->vp = oldvp;
26387c478bd9Sstevel@tonic-gate 			return (stat);
26397c478bd9Sstevel@tonic-gate 		}
26407c478bd9Sstevel@tonic-gate 	}
26417c478bd9Sstevel@tonic-gate 
264245916cd2Sjpk 	/*
264345916cd2Sjpk 	 * After various NFS checks, do a label check on the path
264445916cd2Sjpk 	 * component. The label on this path should either be the
264545916cd2Sjpk 	 * global zone's label or a zone's label. We are only
264645916cd2Sjpk 	 * interested in the zone's label because exported files
264745916cd2Sjpk 	 * in global zone is accessible (though read-only) to
264845916cd2Sjpk 	 * clients. The exportability/visibility check is already
264945916cd2Sjpk 	 * done before reaching this code.
265045916cd2Sjpk 	 */
265145916cd2Sjpk 	if (is_system_labeled()) {
265245916cd2Sjpk 		bslabel_t *clabel;
265345916cd2Sjpk 
265445916cd2Sjpk 		ASSERT(req->rq_label != NULL);
265545916cd2Sjpk 		clabel = req->rq_label;
265645916cd2Sjpk 		DTRACE_PROBE2(tx__rfs4__log__info__oplookup__clabel, char *,
265745916cd2Sjpk 		    "got client label from request(1)", struct svc_req *, req);
265845916cd2Sjpk 
265945916cd2Sjpk 		if (!blequal(&l_admin_low->tsl_label, clabel)) {
266045916cd2Sjpk 			if (!do_rfs4_label_check(clabel, vp, DOMINANCE_CHECK)) {
266145916cd2Sjpk 				error = EACCES;
266245916cd2Sjpk 				goto err_out;
266345916cd2Sjpk 			}
266445916cd2Sjpk 		} else {
266545916cd2Sjpk 			/*
266645916cd2Sjpk 			 * We grant access to admin_low label clients
266745916cd2Sjpk 			 * only if the client is trusted, i.e. also
266845916cd2Sjpk 			 * running Solaris Trusted Extension.
266945916cd2Sjpk 			 */
267045916cd2Sjpk 			struct sockaddr	*ca;
267145916cd2Sjpk 			int		addr_type;
267245916cd2Sjpk 			void		*ipaddr;
267345916cd2Sjpk 			tsol_tpc_t	*tp;
267445916cd2Sjpk 
267545916cd2Sjpk 			ca = (struct sockaddr *)svc_getrpccaller(
267645916cd2Sjpk 			    req->rq_xprt)->buf;
267745916cd2Sjpk 			if (ca->sa_family == AF_INET) {
267845916cd2Sjpk 				addr_type = IPV4_VERSION;
267945916cd2Sjpk 				ipaddr = &((struct sockaddr_in *)ca)->sin_addr;
268045916cd2Sjpk 			} else if (ca->sa_family == AF_INET6) {
268145916cd2Sjpk 				addr_type = IPV6_VERSION;
268245916cd2Sjpk 				ipaddr = &((struct sockaddr_in6 *)
268345916cd2Sjpk 				    ca)->sin6_addr;
268445916cd2Sjpk 			}
268545916cd2Sjpk 			tp = find_tpc(ipaddr, addr_type, B_FALSE);
268645916cd2Sjpk 			if (tp == NULL || tp->tpc_tp.tp_doi !=
268745916cd2Sjpk 			    l_admin_low->tsl_doi || tp->tpc_tp.host_type !=
268845916cd2Sjpk 			    SUN_CIPSO) {
268945916cd2Sjpk 				error = EACCES;
269045916cd2Sjpk 				goto err_out;
269145916cd2Sjpk 			}
269245916cd2Sjpk 		}
269345916cd2Sjpk 	}
269445916cd2Sjpk 
26957c478bd9Sstevel@tonic-gate 	error = makefh4(&cs->fh, vp, cs->exi);
26967c478bd9Sstevel@tonic-gate 
269745916cd2Sjpk err_out:
26987c478bd9Sstevel@tonic-gate 	if (error) {
26997c478bd9Sstevel@tonic-gate 		if (is_newvp) {
27007c478bd9Sstevel@tonic-gate 			VN_RELE(cs->vp);
27017c478bd9Sstevel@tonic-gate 			cs->vp = oldvp;
27027c478bd9Sstevel@tonic-gate 		} else
27037c478bd9Sstevel@tonic-gate 			VN_RELE(vp);
27047c478bd9Sstevel@tonic-gate 		return (puterrno4(error));
27057c478bd9Sstevel@tonic-gate 	}
27067c478bd9Sstevel@tonic-gate 
27077c478bd9Sstevel@tonic-gate 	if (!is_newvp) {
27087c478bd9Sstevel@tonic-gate 		if (cs->vp)
27097c478bd9Sstevel@tonic-gate 			VN_RELE(cs->vp);
27107c478bd9Sstevel@tonic-gate 		cs->vp = vp;
27117c478bd9Sstevel@tonic-gate 	} else if (oldvp)
27127c478bd9Sstevel@tonic-gate 		VN_RELE(oldvp);
27137c478bd9Sstevel@tonic-gate 
27147c478bd9Sstevel@tonic-gate 	/*
27157c478bd9Sstevel@tonic-gate 	 * if did lookup on attrdir and didn't lookup .., set named
27167c478bd9Sstevel@tonic-gate 	 * attr fh flag
27177c478bd9Sstevel@tonic-gate 	 */
27187c478bd9Sstevel@tonic-gate 	if (attrdir && ! dotdot)
27197c478bd9Sstevel@tonic-gate 		set_fh4_flag(&cs->fh, FH4_NAMEDATTR);
27207c478bd9Sstevel@tonic-gate 
27217c478bd9Sstevel@tonic-gate 	/* Assume false for now, open proc will set this */
27227c478bd9Sstevel@tonic-gate 	cs->mandlock = FALSE;
27237c478bd9Sstevel@tonic-gate 
27247c478bd9Sstevel@tonic-gate 	return (NFS4_OK);
27257c478bd9Sstevel@tonic-gate }
27267c478bd9Sstevel@tonic-gate 
27277c478bd9Sstevel@tonic-gate /* ARGSUSED */
27287c478bd9Sstevel@tonic-gate static void
27297c478bd9Sstevel@tonic-gate rfs4_op_lookup(nfs_argop4 *argop, nfs_resop4 *resop, struct svc_req *req,
27307c478bd9Sstevel@tonic-gate 	struct compound_state *cs)
27317c478bd9Sstevel@tonic-gate {
27327c478bd9Sstevel@tonic-gate 	LOOKUP4args *args = &argop->nfs_argop4_u.oplookup;
27337c478bd9Sstevel@tonic-gate 	LOOKUP4res *resp = &resop->nfs_resop4_u.oplookup;
27347c478bd9Sstevel@tonic-gate 	char *nm;
27357c478bd9Sstevel@tonic-gate 	uint_t len;
27367c478bd9Sstevel@tonic-gate 
27377c478bd9Sstevel@tonic-gate 	if (cs->vp == NULL) {
27387c478bd9Sstevel@tonic-gate 		*cs->statusp = resp->status = NFS4ERR_NOFILEHANDLE;
27397c478bd9Sstevel@tonic-gate 		return;
27407c478bd9Sstevel@tonic-gate 	}
27417c478bd9Sstevel@tonic-gate 
27427c478bd9Sstevel@tonic-gate 	if (cs->vp->v_type == VLNK) {
27437c478bd9Sstevel@tonic-gate 		*cs->statusp = resp->status = NFS4ERR_SYMLINK;
27447c478bd9Sstevel@tonic-gate 		return;
27457c478bd9Sstevel@tonic-gate 	}
27467c478bd9Sstevel@tonic-gate 
27477c478bd9Sstevel@tonic-gate 	if (cs->vp->v_type != VDIR) {
27487c478bd9Sstevel@tonic-gate 		*cs->statusp = resp->status = NFS4ERR_NOTDIR;
27497c478bd9Sstevel@tonic-gate 		return;
27507c478bd9Sstevel@tonic-gate 	}
27517c478bd9Sstevel@tonic-gate 
27527c478bd9Sstevel@tonic-gate 	if (!utf8_dir_verify(&args->objname)) {
27537c478bd9Sstevel@tonic-gate 		*cs->statusp = resp->status = NFS4ERR_INVAL;
27547c478bd9Sstevel@tonic-gate 		return;
27557c478bd9Sstevel@tonic-gate 	}
27567c478bd9Sstevel@tonic-gate 
27577c478bd9Sstevel@tonic-gate 	nm = utf8_to_str(&args->objname, &len, NULL);
27587c478bd9Sstevel@tonic-gate 	if (nm == NULL) {
27597c478bd9Sstevel@tonic-gate 		*cs->statusp = resp->status = NFS4ERR_INVAL;
27607c478bd9Sstevel@tonic-gate 		return;
27617c478bd9Sstevel@tonic-gate 	}
27627c478bd9Sstevel@tonic-gate 
27637c478bd9Sstevel@tonic-gate 	if (len > MAXNAMELEN) {
27647c478bd9Sstevel@tonic-gate 		*cs->statusp = resp->status = NFS4ERR_NAMETOOLONG;
27657c478bd9Sstevel@tonic-gate 		kmem_free(nm, len);
27667c478bd9Sstevel@tonic-gate 		return;
27677c478bd9Sstevel@tonic-gate 	}
27687c478bd9Sstevel@tonic-gate 
27697c478bd9Sstevel@tonic-gate 	*cs->statusp = resp->status = do_rfs4_op_lookup(nm, len, req, cs);
27707c478bd9Sstevel@tonic-gate 
27717c478bd9Sstevel@tonic-gate 	kmem_free(nm, len);
27727c478bd9Sstevel@tonic-gate }
27737c478bd9Sstevel@tonic-gate 
27747c478bd9Sstevel@tonic-gate /* ARGSUSED */
27757c478bd9Sstevel@tonic-gate static void
27767c478bd9Sstevel@tonic-gate rfs4_op_lookupp(nfs_argop4 *args, nfs_resop4 *resop, struct svc_req *req,
27777c478bd9Sstevel@tonic-gate 	struct compound_state *cs)
27787c478bd9Sstevel@tonic-gate {
27797c478bd9Sstevel@tonic-gate 	LOOKUPP4res *resp = &resop->nfs_resop4_u.oplookupp;
27807c478bd9Sstevel@tonic-gate 
27817c478bd9Sstevel@tonic-gate 	if (cs->vp == NULL) {
27827c478bd9Sstevel@tonic-gate 		*cs->statusp = resp->status = NFS4ERR_NOFILEHANDLE;
27837c478bd9Sstevel@tonic-gate 		return;
27847c478bd9Sstevel@tonic-gate 	}
27857c478bd9Sstevel@tonic-gate 
27867c478bd9Sstevel@tonic-gate 	if (cs->vp->v_type != VDIR) {
27877c478bd9Sstevel@tonic-gate 		*cs->statusp = resp->status = NFS4ERR_NOTDIR;
27887c478bd9Sstevel@tonic-gate 		return;
27897c478bd9Sstevel@tonic-gate 	}
27907c478bd9Sstevel@tonic-gate 
27917c478bd9Sstevel@tonic-gate 	*cs->statusp = resp->status = do_rfs4_op_lookup("..", 3, req, cs);
27927c478bd9Sstevel@tonic-gate 
27937c478bd9Sstevel@tonic-gate 	/*
27947c478bd9Sstevel@tonic-gate 	 * From NFSV4 Specification, LOOKUPP should not check for
27957c478bd9Sstevel@tonic-gate 	 * NFS4ERR_WRONGSEC. Retrun NFS4_OK instead.
27967c478bd9Sstevel@tonic-gate 	 */
27977c478bd9Sstevel@tonic-gate 	if (resp->status == NFS4ERR_WRONGSEC) {
27987c478bd9Sstevel@tonic-gate 		*cs->statusp = resp->status = NFS4_OK;
27997c478bd9Sstevel@tonic-gate 	}
28007c478bd9Sstevel@tonic-gate }
28017c478bd9Sstevel@tonic-gate 
28027c478bd9Sstevel@tonic-gate 
28037c478bd9Sstevel@tonic-gate /*ARGSUSED2*/
28047c478bd9Sstevel@tonic-gate static void
28057c478bd9Sstevel@tonic-gate rfs4_op_openattr(nfs_argop4 *argop, nfs_resop4 *resop, struct svc_req *req,
28067c478bd9Sstevel@tonic-gate 	struct compound_state *cs)
28077c478bd9Sstevel@tonic-gate {
28087c478bd9Sstevel@tonic-gate 	OPENATTR4args	*args = &argop->nfs_argop4_u.opopenattr;
28097c478bd9Sstevel@tonic-gate 	OPENATTR4res	*resp = &resop->nfs_resop4_u.opopenattr;
28107c478bd9Sstevel@tonic-gate 	vnode_t		*avp = NULL;
28117c478bd9Sstevel@tonic-gate 	int		lookup_flags = LOOKUP_XATTR, error;
28127c478bd9Sstevel@tonic-gate 	int		exp_ro = 0;
28137c478bd9Sstevel@tonic-gate 
28147c478bd9Sstevel@tonic-gate 	if (cs->vp == NULL) {
28157c478bd9Sstevel@tonic-gate 		*cs->statusp = resp->status = NFS4ERR_NOFILEHANDLE;
28167c478bd9Sstevel@tonic-gate 		return;
28177c478bd9Sstevel@tonic-gate 	}
28187c478bd9Sstevel@tonic-gate 
28197c478bd9Sstevel@tonic-gate 	/*
28207c478bd9Sstevel@tonic-gate 	 * Make a couple of checks made by copen()
28217c478bd9Sstevel@tonic-gate 	 *
28227c478bd9Sstevel@tonic-gate 	 * Check to make sure underlying fs supports xattrs.  This
28237c478bd9Sstevel@tonic-gate 	 * is required because solaris filesystem implementations
28247c478bd9Sstevel@tonic-gate 	 * (UFS/TMPFS) don't enforce the noxattr mount option
28257c478bd9Sstevel@tonic-gate 	 * in VOP_LOOKUP(LOOKUP_XATTR).  If fs doesn't support this
28267c478bd9Sstevel@tonic-gate 	 * pathconf cmd or if fs supports cmd but doesn't claim
28277c478bd9Sstevel@tonic-gate 	 * support for xattr, return NOTSUPP.  It would be better
28287c478bd9Sstevel@tonic-gate 	 * to use VOP_PATHCONF( _PC_XATTR_ENABLED) for this; however,
28297c478bd9Sstevel@tonic-gate 	 * that cmd is not available to VOP_PATHCONF interface
28307c478bd9Sstevel@tonic-gate 	 * (it's only implemented inside pathconf syscall)...
28317c478bd9Sstevel@tonic-gate 	 *
28327c478bd9Sstevel@tonic-gate 	 * Verify permission to put attributes on files (access
28337c478bd9Sstevel@tonic-gate 	 * checks from copen).
28347c478bd9Sstevel@tonic-gate 	 */
28357c478bd9Sstevel@tonic-gate 
28367c478bd9Sstevel@tonic-gate 	if ((cs->vp->v_vfsp->vfs_flag & VFS_XATTR) == 0) {
28377c478bd9Sstevel@tonic-gate 		error = ENOTSUP;
28387c478bd9Sstevel@tonic-gate 		goto error_out;
28397c478bd9Sstevel@tonic-gate 	}
28407c478bd9Sstevel@tonic-gate 
28417c478bd9Sstevel@tonic-gate 	if ((VOP_ACCESS(cs->vp, VREAD, 0, cs->cr) != 0) &&
28427c478bd9Sstevel@tonic-gate 	    (VOP_ACCESS(cs->vp, VWRITE, 0, cs->cr) != 0) &&
28437c478bd9Sstevel@tonic-gate 	    (VOP_ACCESS(cs->vp, VEXEC, 0, cs->cr) != 0)) {
28447c478bd9Sstevel@tonic-gate 		error = EACCES;
28457c478bd9Sstevel@tonic-gate 		goto error_out;
28467c478bd9Sstevel@tonic-gate 	}
28477c478bd9Sstevel@tonic-gate 
28487c478bd9Sstevel@tonic-gate 	/*
28497c478bd9Sstevel@tonic-gate 	 * The CREATE_XATTR_DIR VOP flag cannot be specified if
28507c478bd9Sstevel@tonic-gate 	 * the file system is exported read-only -- regardless of
28517c478bd9Sstevel@tonic-gate 	 * createdir flag.  Otherwise the attrdir would be created
28527c478bd9Sstevel@tonic-gate 	 * (assuming server fs isn't mounted readonly locally).  If
28537c478bd9Sstevel@tonic-gate 	 * VOP_LOOKUP returns ENOENT in this case, the error will
28547c478bd9Sstevel@tonic-gate 	 * be translated into EROFS.  ENOSYS is mapped to ENOTSUP
28557c478bd9Sstevel@tonic-gate 	 * because specfs has no VOP_LOOKUP op, so the macro would
28567c478bd9Sstevel@tonic-gate 	 * return ENOSYS.  EINVAL is returned by all (current)
28577c478bd9Sstevel@tonic-gate 	 * Solaris file system implementations when any of their
28587c478bd9Sstevel@tonic-gate 	 * restrictions are violated (xattr(dir) can't have xattrdir).
28597c478bd9Sstevel@tonic-gate 	 * Returning NOTSUPP is more appropriate in this case
28607c478bd9Sstevel@tonic-gate 	 * because the object will never be able to have an attrdir.
28617c478bd9Sstevel@tonic-gate 	 */
28627c478bd9Sstevel@tonic-gate 	if (args->createdir && ! (exp_ro = rdonly4(cs->exi, cs->vp, req)))
28637c478bd9Sstevel@tonic-gate 		lookup_flags |= CREATE_XATTR_DIR;
28647c478bd9Sstevel@tonic-gate 
28657c478bd9Sstevel@tonic-gate 	error = VOP_LOOKUP(cs->vp, "", &avp, NULL, lookup_flags, NULL, cs->cr);
28667c478bd9Sstevel@tonic-gate 
28677c478bd9Sstevel@tonic-gate 	if (error) {
28687c478bd9Sstevel@tonic-gate 		if (error == ENOENT && args->createdir && exp_ro)
28697c478bd9Sstevel@tonic-gate 			error = EROFS;
28707c478bd9Sstevel@tonic-gate 		else if (error == EINVAL || error == ENOSYS)
28717c478bd9Sstevel@tonic-gate 			error = ENOTSUP;
28727c478bd9Sstevel@tonic-gate 		goto error_out;
28737c478bd9Sstevel@tonic-gate 	}
28747c478bd9Sstevel@tonic-gate 
28757c478bd9Sstevel@tonic-gate 	ASSERT(avp->v_flag & V_XATTRDIR);
28767c478bd9Sstevel@tonic-gate 
28777c478bd9Sstevel@tonic-gate 	error = makefh4(&cs->fh, avp, cs->exi);
28787c478bd9Sstevel@tonic-gate 
28797c478bd9Sstevel@tonic-gate 	if (error) {
28807c478bd9Sstevel@tonic-gate 		VN_RELE(avp);
28817c478bd9Sstevel@tonic-gate 		goto error_out;
28827c478bd9Sstevel@tonic-gate 	}
28837c478bd9Sstevel@tonic-gate 
28847c478bd9Sstevel@tonic-gate 	VN_RELE(cs->vp);
28857c478bd9Sstevel@tonic-gate 	cs->vp = avp;
28867c478bd9Sstevel@tonic-gate 
28877c478bd9Sstevel@tonic-gate 	/*
28887c478bd9Sstevel@tonic-gate 	 * There is no requirement for an attrdir fh flag
28897c478bd9Sstevel@tonic-gate 	 * because the attrdir has a vnode flag to distinguish
28907c478bd9Sstevel@tonic-gate 	 * it from regular (non-xattr) directories.  The
28917c478bd9Sstevel@tonic-gate 	 * FH4_ATTRDIR flag is set for future sanity checks.
28927c478bd9Sstevel@tonic-gate 	 */
28937c478bd9Sstevel@tonic-gate 	set_fh4_flag(&cs->fh, FH4_ATTRDIR);
28947c478bd9Sstevel@tonic-gate 	*cs->statusp = resp->status = NFS4_OK;
28957c478bd9Sstevel@tonic-gate 	return;
28967c478bd9Sstevel@tonic-gate 
28977c478bd9Sstevel@tonic-gate error_out:
28987c478bd9Sstevel@tonic-gate 
28997c478bd9Sstevel@tonic-gate 	*cs->statusp = resp->status = puterrno4(error);
29007c478bd9Sstevel@tonic-gate }
29017c478bd9Sstevel@tonic-gate 
29027c478bd9Sstevel@tonic-gate static int
29037c478bd9Sstevel@tonic-gate do_io(int direction, vnode_t *vp, struct uio *uio, int ioflag, cred_t *cred)
29047c478bd9Sstevel@tonic-gate {
29057c478bd9Sstevel@tonic-gate 	int error;
29067c478bd9Sstevel@tonic-gate 	int i;
29077c478bd9Sstevel@tonic-gate 	clock_t delaytime;
29087c478bd9Sstevel@tonic-gate 	caller_context_t ct;
29097c478bd9Sstevel@tonic-gate 
29107c478bd9Sstevel@tonic-gate 	delaytime = MSEC_TO_TICK_ROUNDUP(rfs4_lock_delay);
29117c478bd9Sstevel@tonic-gate 
29127c478bd9Sstevel@tonic-gate 	/*
29137c478bd9Sstevel@tonic-gate 	 * Don't block on mandatory locks. If this routine returns
29147c478bd9Sstevel@tonic-gate 	 * EAGAIN, the caller should return NFS4ERR_LOCKED.
29157c478bd9Sstevel@tonic-gate 	 */
29167c478bd9Sstevel@tonic-gate 	uio->uio_fmode = FNONBLOCK;
29177c478bd9Sstevel@tonic-gate 
29187c478bd9Sstevel@tonic-gate 	ct.cc_sysid = 0;
29197c478bd9Sstevel@tonic-gate 	ct.cc_pid = 0;
29207c478bd9Sstevel@tonic-gate 	ct.cc_caller_id = nfs4_srv_caller_id;
29217c478bd9Sstevel@tonic-gate 
29227c478bd9Sstevel@tonic-gate 	for (i = 0; i < rfs4_maxlock_tries; i++) {
29237c478bd9Sstevel@tonic-gate 
29247c478bd9Sstevel@tonic-gate 
29257c478bd9Sstevel@tonic-gate 		if (direction == FREAD) {
29267c478bd9Sstevel@tonic-gate 			(void) VOP_RWLOCK(vp, V_WRITELOCK_FALSE, &ct);
29277c478bd9Sstevel@tonic-gate 			error = VOP_READ(vp, uio, ioflag, cred, &ct);
29287c478bd9Sstevel@tonic-gate 			VOP_RWUNLOCK(vp, V_WRITELOCK_FALSE, &ct);
29297c478bd9Sstevel@tonic-gate 		} else {
29307c478bd9Sstevel@tonic-gate 			(void) VOP_RWLOCK(vp, V_WRITELOCK_TRUE, &ct);
29317c478bd9Sstevel@tonic-gate 			error = VOP_WRITE(vp, uio, ioflag, cred, &ct);
29327c478bd9Sstevel@tonic-gate 			VOP_RWUNLOCK(vp, V_WRITELOCK_TRUE, &ct);
29337c478bd9Sstevel@tonic-gate 		}
29347c478bd9Sstevel@tonic-gate 
29357c478bd9Sstevel@tonic-gate 		if (error != EAGAIN)
29367c478bd9Sstevel@tonic-gate 			break;
29377c478bd9Sstevel@tonic-gate 
29387c478bd9Sstevel@tonic-gate 		if (i < rfs4_maxlock_tries - 1) {
29397c478bd9Sstevel@tonic-gate 			delay(delaytime);
29407c478bd9Sstevel@tonic-gate 			delaytime *= 2;
29417c478bd9Sstevel@tonic-gate 		}
29427c478bd9Sstevel@tonic-gate 	}
29437c478bd9Sstevel@tonic-gate 
29447c478bd9Sstevel@tonic-gate 	return (error);
29457c478bd9Sstevel@tonic-gate }
29467c478bd9Sstevel@tonic-gate 
29477c478bd9Sstevel@tonic-gate /* ARGSUSED */
29487c478bd9Sstevel@tonic-gate static void
29497c478bd9Sstevel@tonic-gate rfs4_op_read(nfs_argop4 *argop, nfs_resop4 *resop, struct svc_req *req,
29507c478bd9Sstevel@tonic-gate 	struct compound_state *cs)
29517c478bd9Sstevel@tonic-gate {
29527c478bd9Sstevel@tonic-gate 	READ4args *args = &argop->nfs_argop4_u.opread;
29537c478bd9Sstevel@tonic-gate 	READ4res *resp = &resop->nfs_resop4_u.opread;
29547c478bd9Sstevel@tonic-gate 	int error;
29557c478bd9Sstevel@tonic-gate 	int verror;
29567c478bd9Sstevel@tonic-gate 	vnode_t *vp;
29577c478bd9Sstevel@tonic-gate 	struct vattr va;
29587c478bd9Sstevel@tonic-gate 	struct iovec iov;
29597c478bd9Sstevel@tonic-gate 	struct uio uio;
29607c478bd9Sstevel@tonic-gate 	u_offset_t offset;
29617c478bd9Sstevel@tonic-gate 	bool_t *deleg = &cs->deleg;
29627c478bd9Sstevel@tonic-gate 	nfsstat4 stat;
29637c478bd9Sstevel@tonic-gate 	int in_crit = 0;
29647c478bd9Sstevel@tonic-gate 	mblk_t *mp;
29657c478bd9Sstevel@tonic-gate 	int alloc_err = 0;
29667c478bd9Sstevel@tonic-gate 
29677c478bd9Sstevel@tonic-gate 	vp = cs->vp;
29687c478bd9Sstevel@tonic-gate 	if (vp == NULL) {
29697c478bd9Sstevel@tonic-gate 		*cs->statusp = resp->status = NFS4ERR_NOFILEHANDLE;
29707c478bd9Sstevel@tonic-gate 		return;
29717c478bd9Sstevel@tonic-gate 	}
29727c478bd9Sstevel@tonic-gate 	if (cs->access == CS_ACCESS_DENIED) {
29737c478bd9Sstevel@tonic-gate 		*cs->statusp = resp->status = NFS4ERR_ACCESS;
29747c478bd9Sstevel@tonic-gate 		return;
29757c478bd9Sstevel@tonic-gate 	}
29767c478bd9Sstevel@tonic-gate 
29777c478bd9Sstevel@tonic-gate 	/*
29787c478bd9Sstevel@tonic-gate 	 * Enter the critical region before calling VOP_RWLOCK
29797c478bd9Sstevel@tonic-gate 	 * to avoid a deadlock with write requests.
29807c478bd9Sstevel@tonic-gate 	 */
29817c478bd9Sstevel@tonic-gate 	if (nbl_need_check(vp)) {
29827c478bd9Sstevel@tonic-gate 		nbl_start_crit(vp, RW_READER);
29837c478bd9Sstevel@tonic-gate 		in_crit = 1;
29847c478bd9Sstevel@tonic-gate 		if (nbl_conflict(vp, NBL_READ, args->offset, args->count, 0)) {
29857c478bd9Sstevel@tonic-gate 			*cs->statusp = resp->status = NFS4ERR_LOCKED;
29867c478bd9Sstevel@tonic-gate 			goto out;
29877c478bd9Sstevel@tonic-gate 		}
29887c478bd9Sstevel@tonic-gate 	}
29897c478bd9Sstevel@tonic-gate 
29907c478bd9Sstevel@tonic-gate 	if ((stat = rfs4_check_stateid(FREAD, vp, &args->stateid, FALSE,
29917c478bd9Sstevel@tonic-gate 					deleg, TRUE)) != NFS4_OK) {
29927c478bd9Sstevel@tonic-gate 		*cs->statusp = resp->status = stat;
29937c478bd9Sstevel@tonic-gate 		goto out;
29947c478bd9Sstevel@tonic-gate 	}
29957c478bd9Sstevel@tonic-gate 
29967c478bd9Sstevel@tonic-gate 	va.va_mask = AT_MODE|AT_SIZE|AT_UID;
29977c478bd9Sstevel@tonic-gate 	verror = VOP_GETATTR(vp, &va, 0, cs->cr);
29987c478bd9Sstevel@tonic-gate 
29997c478bd9Sstevel@tonic-gate 	/*
30007c478bd9Sstevel@tonic-gate 	 * If we can't get the attributes, then we can't do the
30017c478bd9Sstevel@tonic-gate 	 * right access checking.  So, we'll fail the request.
30027c478bd9Sstevel@tonic-gate 	 */
30037c478bd9Sstevel@tonic-gate 	if (verror) {
30047c478bd9Sstevel@tonic-gate 		*cs->statusp = resp->status = puterrno4(verror);
30057c478bd9Sstevel@tonic-gate 		goto out;
30067c478bd9Sstevel@tonic-gate 	}
30077c478bd9Sstevel@tonic-gate 
30087c478bd9Sstevel@tonic-gate 	if (vp->v_type != VREG) {
30097c478bd9Sstevel@tonic-gate 		*cs->statusp = resp->status =
30107c478bd9Sstevel@tonic-gate 			((vp->v_type == VDIR) ? NFS4ERR_ISDIR : NFS4ERR_INVAL);
30117c478bd9Sstevel@tonic-gate 		goto out;
30127c478bd9Sstevel@tonic-gate 	}
30137c478bd9Sstevel@tonic-gate 
30147c478bd9Sstevel@tonic-gate 	if (crgetuid(cs->cr) != va.va_uid &&
30157c478bd9Sstevel@tonic-gate 	    (error = VOP_ACCESS(vp, VREAD, 0, cs->cr)) &&
30167c478bd9Sstevel@tonic-gate 	    (error = VOP_ACCESS(vp, VEXEC, 0, cs->cr))) {
30177c478bd9Sstevel@tonic-gate 		*cs->statusp = resp->status = puterrno4(error);
30187c478bd9Sstevel@tonic-gate 		goto out;
30197c478bd9Sstevel@tonic-gate 	}
30207c478bd9Sstevel@tonic-gate 
30217c478bd9Sstevel@tonic-gate 	if (MANDLOCK(vp, va.va_mode)) { /* XXX - V4 supports mand locking */
30227c478bd9Sstevel@tonic-gate 		*cs->statusp = resp->status = NFS4ERR_ACCESS;
30237c478bd9Sstevel@tonic-gate 		goto out;
30247c478bd9Sstevel@tonic-gate 	}
30257c478bd9Sstevel@tonic-gate 
30267c478bd9Sstevel@tonic-gate 	offset = args->offset;
30277c478bd9Sstevel@tonic-gate 	if (offset >= va.va_size) {
30287c478bd9Sstevel@tonic-gate 		*cs->statusp = resp->status = NFS4_OK;
30297c478bd9Sstevel@tonic-gate 		resp->eof = TRUE;
30307c478bd9Sstevel@tonic-gate 		resp->data_len = 0;
30317c478bd9Sstevel@tonic-gate 		resp->data_val = NULL;
30327c478bd9Sstevel@tonic-gate 		resp->mblk = NULL;
30337c478bd9Sstevel@tonic-gate 		*cs->statusp = resp->status = NFS4_OK;
30347c478bd9Sstevel@tonic-gate 		goto out;
30357c478bd9Sstevel@tonic-gate 	}
30367c478bd9Sstevel@tonic-gate 
30377c478bd9Sstevel@tonic-gate 	if (args->count == 0) {
30387c478bd9Sstevel@tonic-gate 		*cs->statusp = resp->status = NFS4_OK;
30397c478bd9Sstevel@tonic-gate 		resp->eof = FALSE;
30407c478bd9Sstevel@tonic-gate 		resp->data_len = 0;
30417c478bd9Sstevel@tonic-gate 		resp->data_val = NULL;
30427c478bd9Sstevel@tonic-gate 		resp->mblk = NULL;
30437c478bd9Sstevel@tonic-gate 		goto out;
30447c478bd9Sstevel@tonic-gate 	}
30457c478bd9Sstevel@tonic-gate 
30467c478bd9Sstevel@tonic-gate 	/*
30477c478bd9Sstevel@tonic-gate 	 * Do not allocate memory more than maximum allowed
30487c478bd9Sstevel@tonic-gate 	 * transfer size
30497c478bd9Sstevel@tonic-gate 	 */
30507c478bd9Sstevel@tonic-gate 	if (args->count > rfs4_tsize(req))
30517c478bd9Sstevel@tonic-gate 		args->count = rfs4_tsize(req);
30527c478bd9Sstevel@tonic-gate 
30537c478bd9Sstevel@tonic-gate 	/*
30547c478bd9Sstevel@tonic-gate 	 * mp will contain the data to be sent out in the read reply.
30557c478bd9Sstevel@tonic-gate 	 * It will be freed after the reply has been sent.
30567c478bd9Sstevel@tonic-gate 	 * Let's roundup the data to a BYTES_PER_XDR_UNIT multiple,
30577c478bd9Sstevel@tonic-gate 	 * so that the call to xdrmblk_putmblk() never fails.
30587c478bd9Sstevel@tonic-gate 	 * If the first alloc of the requested size fails, then
30597c478bd9Sstevel@tonic-gate 	 * decrease the size to something more reasonable and wait
30607c478bd9Sstevel@tonic-gate 	 * for the allocation to occur.
30617c478bd9Sstevel@tonic-gate 	 */
30627c478bd9Sstevel@tonic-gate 	mp = allocb(RNDUP(args->count), BPRI_MED);
30637c478bd9Sstevel@tonic-gate 	if (mp == NULL) {
30647c478bd9Sstevel@tonic-gate 		if (args->count > MAXBSIZE)
30657c478bd9Sstevel@tonic-gate 			args->count = MAXBSIZE;
30667c478bd9Sstevel@tonic-gate 		mp = allocb_wait(RNDUP(args->count), BPRI_MED,
30677c478bd9Sstevel@tonic-gate 				STR_NOSIG, &alloc_err);
30687c478bd9Sstevel@tonic-gate 	}
30697c478bd9Sstevel@tonic-gate 	ASSERT(mp != NULL);
30707c478bd9Sstevel@tonic-gate 	ASSERT(alloc_err == 0);
30717c478bd9Sstevel@tonic-gate 
30727c478bd9Sstevel@tonic-gate 	iov.iov_base = (caddr_t)mp->b_datap->db_base;
30737c478bd9Sstevel@tonic-gate 	iov.iov_len = args->count;
30747c478bd9Sstevel@tonic-gate 	uio.uio_iov = &iov;
30757c478bd9Sstevel@tonic-gate 	uio.uio_iovcnt = 1;
30767c478bd9Sstevel@tonic-gate 	uio.uio_segflg = UIO_SYSSPACE;
30777c478bd9Sstevel@tonic-gate 	uio.uio_extflg = UIO_COPY_CACHED;
30787c478bd9Sstevel@tonic-gate 	uio.uio_loffset = args->offset;
30797c478bd9Sstevel@tonic-gate 	uio.uio_resid = args->count;
30807c478bd9Sstevel@tonic-gate 
30817c478bd9Sstevel@tonic-gate 	error = do_io(FREAD, vp, &uio, 0, cs->cr);
30827c478bd9Sstevel@tonic-gate 
30837c478bd9Sstevel@tonic-gate 	va.va_mask = AT_SIZE;
30847c478bd9Sstevel@tonic-gate 	verror = VOP_GETATTR(vp, &va, 0, cs->cr);
30857c478bd9Sstevel@tonic-gate 
30867c478bd9Sstevel@tonic-gate 	if (error) {
30877c478bd9Sstevel@tonic-gate 		freeb(mp);
30887c478bd9Sstevel@tonic-gate 		*cs->statusp = resp->status = puterrno4(error);
30897c478bd9Sstevel@tonic-gate 		goto out;
30907c478bd9Sstevel@tonic-gate 	}
30917c478bd9Sstevel@tonic-gate 
30927c478bd9Sstevel@tonic-gate 	*cs->statusp = resp->status = NFS4_OK;
30937c478bd9Sstevel@tonic-gate 
30947c478bd9Sstevel@tonic-gate 	ASSERT(uio.uio_resid >= 0);
30957c478bd9Sstevel@tonic-gate 	resp->data_len = args->count - uio.uio_resid;
30967c478bd9Sstevel@tonic-gate 	resp->data_val = (char *)mp->b_datap->db_base;
30977c478bd9Sstevel@tonic-gate 	resp->mblk = mp;
30987c478bd9Sstevel@tonic-gate 
30997c478bd9Sstevel@tonic-gate 	if (!verror && offset + resp->data_len == va.va_size)
31007c478bd9Sstevel@tonic-gate 		resp->eof = TRUE;
31017c478bd9Sstevel@tonic-gate 	else
31027c478bd9Sstevel@tonic-gate 		resp->eof = FALSE;
31037c478bd9Sstevel@tonic-gate 
31047c478bd9Sstevel@tonic-gate out:
31057c478bd9Sstevel@tonic-gate 	if (in_crit)
31067c478bd9Sstevel@tonic-gate 		nbl_end_crit(vp);
31077c478bd9Sstevel@tonic-gate }
31087c478bd9Sstevel@tonic-gate 
31097c478bd9Sstevel@tonic-gate static void
31107c478bd9Sstevel@tonic-gate rfs4_op_read_free(nfs_resop4 *resop)
31117c478bd9Sstevel@tonic-gate {
31127c478bd9Sstevel@tonic-gate 	READ4res *resp = &resop->nfs_resop4_u.opread;
31137c478bd9Sstevel@tonic-gate 
31147c478bd9Sstevel@tonic-gate 	if (resp->status == NFS4_OK && resp->mblk != NULL) {
31157c478bd9Sstevel@tonic-gate 		freeb(resp->mblk);
31167c478bd9Sstevel@tonic-gate 		resp->mblk = NULL;
31177c478bd9Sstevel@tonic-gate 		resp->data_val = NULL;
31187c478bd9Sstevel@tonic-gate 		resp->data_len = 0;
31197c478bd9Sstevel@tonic-gate 	}
31207c478bd9Sstevel@tonic-gate }
31217c478bd9Sstevel@tonic-gate 
31227c478bd9Sstevel@tonic-gate static void
31237c478bd9Sstevel@tonic-gate rfs4_op_readdir_free(nfs_resop4 *resop)
31247c478bd9Sstevel@tonic-gate {
31257c478bd9Sstevel@tonic-gate 	READDIR4res *resp = &resop->nfs_resop4_u.opreaddir;
31267c478bd9Sstevel@tonic-gate 
31277c478bd9Sstevel@tonic-gate 	if (resp->status == NFS4_OK && resp->mblk != NULL) {
31287c478bd9Sstevel@tonic-gate 		freeb(resp->mblk);
31297c478bd9Sstevel@tonic-gate 		resp->mblk = NULL;
31307c478bd9Sstevel@tonic-gate 		resp->data_len = 0;
31317c478bd9Sstevel@tonic-gate 	}
31327c478bd9Sstevel@tonic-gate }
31337c478bd9Sstevel@tonic-gate 
31347c478bd9Sstevel@tonic-gate 
31357c478bd9Sstevel@tonic-gate /* ARGSUSED */
31367c478bd9Sstevel@tonic-gate static void
31377c478bd9Sstevel@tonic-gate rfs4_op_putpubfh(nfs_argop4 *args, nfs_resop4 *resop, struct svc_req *req,
31387c478bd9Sstevel@tonic-gate 	struct compound_state *cs)
31397c478bd9Sstevel@tonic-gate {
31407c478bd9Sstevel@tonic-gate 	PUTPUBFH4res *resp = &resop->nfs_resop4_u.opputpubfh;
31417c478bd9Sstevel@tonic-gate 	int error;
31427c478bd9Sstevel@tonic-gate 	vnode_t *vp;
31437c478bd9Sstevel@tonic-gate 	struct exportinfo *exi, *sav_exi;
31447c478bd9Sstevel@tonic-gate 	nfs_fh4_fmt_t *fh_fmtp;
31457c478bd9Sstevel@tonic-gate 
31467c478bd9Sstevel@tonic-gate 	if (cs->vp) {
31477c478bd9Sstevel@tonic-gate 		VN_RELE(cs->vp);
31487c478bd9Sstevel@tonic-gate 		cs->vp = NULL;
31497c478bd9Sstevel@tonic-gate 	}
31507c478bd9Sstevel@tonic-gate 
31517c478bd9Sstevel@tonic-gate 	if (cs->cr)
31527c478bd9Sstevel@tonic-gate 		crfree(cs->cr);
31537c478bd9Sstevel@tonic-gate 
31547c478bd9Sstevel@tonic-gate 	cs->cr = crdup(cs->basecr);
31557c478bd9Sstevel@tonic-gate 
31567c478bd9Sstevel@tonic-gate 	vp = exi_public->exi_vp;
31577c478bd9Sstevel@tonic-gate 	if (vp == NULL) {
31587c478bd9Sstevel@tonic-gate 		*cs->statusp = resp->status = NFS4ERR_SERVERFAULT;
31597c478bd9Sstevel@tonic-gate 		return;
31607c478bd9Sstevel@tonic-gate 	}
31617c478bd9Sstevel@tonic-gate 
31627c478bd9Sstevel@tonic-gate 	error = makefh4(&cs->fh, vp, exi_public);
31637c478bd9Sstevel@tonic-gate 	if (error != 0) {
31647c478bd9Sstevel@tonic-gate 		*cs->statusp = resp->status = puterrno4(error);
31657c478bd9Sstevel@tonic-gate 		return;
31667c478bd9Sstevel@tonic-gate 	}
31677c478bd9Sstevel@tonic-gate 	sav_exi = cs->exi;
31687c478bd9Sstevel@tonic-gate 	if (exi_public == exi_root) {
31697c478bd9Sstevel@tonic-gate 		/*
31707c478bd9Sstevel@tonic-gate 		 * No filesystem is actually shared public, so we default
31717c478bd9Sstevel@tonic-gate 		 * to exi_root. In this case, we must check whether root
31727c478bd9Sstevel@tonic-gate 		 * is exported.
31737c478bd9Sstevel@tonic-gate 		 */
31747c478bd9Sstevel@tonic-gate 		fh_fmtp = (nfs_fh4_fmt_t *)cs->fh.nfs_fh4_val;
31757c478bd9Sstevel@tonic-gate 
31767c478bd9Sstevel@tonic-gate 		/*
31777c478bd9Sstevel@tonic-gate 		 * if root filesystem is exported, the exportinfo struct that we
31787c478bd9Sstevel@tonic-gate 		 * should use is what checkexport4 returns, because root_exi is
31797c478bd9Sstevel@tonic-gate 		 * actually a mostly empty struct.
31807c478bd9Sstevel@tonic-gate 		 */
31817c478bd9Sstevel@tonic-gate 		exi = checkexport4(&fh_fmtp->fh4_fsid,
31827c478bd9Sstevel@tonic-gate 			(fid_t *)&fh_fmtp->fh4_xlen, NULL);
31837c478bd9Sstevel@tonic-gate 		cs->exi = ((exi != NULL) ? exi : exi_public);
31847c478bd9Sstevel@tonic-gate 	} else {
31857c478bd9Sstevel@tonic-gate 		/*
31867c478bd9Sstevel@tonic-gate 		 * it's a properly shared filesystem
31877c478bd9Sstevel@tonic-gate 		 */
31887c478bd9Sstevel@tonic-gate 		cs->exi = exi_public;
31897c478bd9Sstevel@tonic-gate 	}
31907c478bd9Sstevel@tonic-gate 
31917c478bd9Sstevel@tonic-gate 	VN_HOLD(vp);
31927c478bd9Sstevel@tonic-gate 	cs->vp = vp;
31937c478bd9Sstevel@tonic-gate 
31947c478bd9Sstevel@tonic-gate 	if ((resp->status = call_checkauth4(cs, req)) != NFS4_OK) {
31957c478bd9Sstevel@tonic-gate 		VN_RELE(cs->vp);
31967c478bd9Sstevel@tonic-gate 		cs->vp = NULL;
31977c478bd9Sstevel@tonic-gate 		cs->exi = sav_exi;
31987c478bd9Sstevel@tonic-gate 		return;
31997c478bd9Sstevel@tonic-gate 	}
32007c478bd9Sstevel@tonic-gate 
32017c478bd9Sstevel@tonic-gate 	*cs->statusp = resp->status = NFS4_OK;
32027c478bd9Sstevel@tonic-gate }
32037c478bd9Sstevel@tonic-gate 
32047c478bd9Sstevel@tonic-gate /*
32057c478bd9Sstevel@tonic-gate  * XXX - issue with put*fh operations. Suppose /export/home is exported.
32067c478bd9Sstevel@tonic-gate  * Suppose an NFS client goes to mount /export/home/joe. If /export, home,
32077c478bd9Sstevel@tonic-gate  * or joe have restrictive search permissions, then we shouldn't let
32087c478bd9Sstevel@tonic-gate  * the client get a file handle. This is easy to enforce. However, we
32097c478bd9Sstevel@tonic-gate  * don't know what security flavor should be used until we resolve the
32107c478bd9Sstevel@tonic-gate  * path name. Another complication is uid mapping. If root is
32117c478bd9Sstevel@tonic-gate  * the user, then it will be mapped to the anonymous user by default,
32127c478bd9Sstevel@tonic-gate  * but we won't know that till we've resolved the path name. And we won't
32137c478bd9Sstevel@tonic-gate  * know what the anonymous user is.
32147c478bd9Sstevel@tonic-gate  * Luckily, SECINFO is specified to take a full filename.
32157c478bd9Sstevel@tonic-gate  * So what we will have to in rfs4_op_lookup is check that flavor of
32167c478bd9Sstevel@tonic-gate  * the target object matches that of the request, and if root was the
32177c478bd9Sstevel@tonic-gate  * caller, check for the root= and anon= options, and if necessary,
32187c478bd9Sstevel@tonic-gate  * repeat the lookup using the right cred_t. But that's not done yet.
32197c478bd9Sstevel@tonic-gate  */
32207c478bd9Sstevel@tonic-gate /* ARGSUSED */
32217c478bd9Sstevel@tonic-gate static void
32227c478bd9Sstevel@tonic-gate rfs4_op_putfh(nfs_argop4 *argop, nfs_resop4 *resop, struct svc_req *req,
32237c478bd9Sstevel@tonic-gate 	struct compound_state *cs)
32247c478bd9Sstevel@tonic-gate {
32257c478bd9Sstevel@tonic-gate 	PUTFH4args *args = &argop->nfs_argop4_u.opputfh;
32267c478bd9Sstevel@tonic-gate 	PUTFH4res *resp = &resop->nfs_resop4_u.opputfh;
32277c478bd9Sstevel@tonic-gate 	nfs_fh4_fmt_t *fh_fmtp;
32287c478bd9Sstevel@tonic-gate 
32297c478bd9Sstevel@tonic-gate 	if (cs->vp) {
32307c478bd9Sstevel@tonic-gate 		VN_RELE(cs->vp);
32317c478bd9Sstevel@tonic-gate 		cs->vp = NULL;
32327c478bd9Sstevel@tonic-gate 	}
32337c478bd9Sstevel@tonic-gate 
32347c478bd9Sstevel@tonic-gate 	if (cs->cr) {
32357c478bd9Sstevel@tonic-gate 		crfree(cs->cr);
32367c478bd9Sstevel@tonic-gate 		cs->cr = NULL;
32377c478bd9Sstevel@tonic-gate 	}
32387c478bd9Sstevel@tonic-gate 
32397c478bd9Sstevel@tonic-gate 
32407c478bd9Sstevel@tonic-gate 	if (args->object.nfs_fh4_len < NFS_FH4_LEN) {
32417c478bd9Sstevel@tonic-gate 		*cs->statusp = resp->status = NFS4ERR_BADHANDLE;
32427c478bd9Sstevel@tonic-gate 		return;
32437c478bd9Sstevel@tonic-gate 	}
32447c478bd9Sstevel@tonic-gate 
32457c478bd9Sstevel@tonic-gate 	fh_fmtp = (nfs_fh4_fmt_t *)args->object.nfs_fh4_val;
32467c478bd9Sstevel@tonic-gate 	cs->exi = checkexport4(&fh_fmtp->fh4_fsid, (fid_t *)&fh_fmtp->fh4_xlen,
32477c478bd9Sstevel@tonic-gate 				NULL);
32487c478bd9Sstevel@tonic-gate 
32497c478bd9Sstevel@tonic-gate 	if (cs->exi == NULL) {
32507c478bd9Sstevel@tonic-gate 		*cs->statusp = resp->status = NFS4ERR_STALE;
32517c478bd9Sstevel@tonic-gate 		return;
32527c478bd9Sstevel@tonic-gate 	}
32537c478bd9Sstevel@tonic-gate 
32547c478bd9Sstevel@tonic-gate 	cs->cr = crdup(cs->basecr);
32557c478bd9Sstevel@tonic-gate 
32567c478bd9Sstevel@tonic-gate 	ASSERT(cs->cr != NULL);
32577c478bd9Sstevel@tonic-gate 
32587c478bd9Sstevel@tonic-gate 	if (! (cs->vp = nfs4_fhtovp(&args->object, cs->exi, &resp->status))) {
32597c478bd9Sstevel@tonic-gate 		*cs->statusp = resp->status;
32607c478bd9Sstevel@tonic-gate 		return;
32617c478bd9Sstevel@tonic-gate 	}
32627c478bd9Sstevel@tonic-gate 
32637c478bd9Sstevel@tonic-gate 	if ((resp->status = call_checkauth4(cs, req)) != NFS4_OK) {
32647c478bd9Sstevel@tonic-gate 		VN_RELE(cs->vp);
32657c478bd9Sstevel@tonic-gate 		cs->vp = NULL;
32667c478bd9Sstevel@tonic-gate 		return;
32677c478bd9Sstevel@tonic-gate 	}
32687c478bd9Sstevel@tonic-gate 
32697c478bd9Sstevel@tonic-gate 	nfs_fh4_copy(&args->object, &cs->fh);
32707c478bd9Sstevel@tonic-gate 	*cs->statusp = resp->status = NFS4_OK;
32717c478bd9Sstevel@tonic-gate 	cs->deleg = FALSE;
32727c478bd9Sstevel@tonic-gate }
32737c478bd9Sstevel@tonic-gate 
32747c478bd9Sstevel@tonic-gate /* ARGSUSED */
32757c478bd9Sstevel@tonic-gate static void
32767c478bd9Sstevel@tonic-gate rfs4_op_putrootfh(nfs_argop4 *argop, nfs_resop4 *resop, struct svc_req *req,
32777c478bd9Sstevel@tonic-gate 	struct compound_state *cs)
32787c478bd9Sstevel@tonic-gate 
32797c478bd9Sstevel@tonic-gate {
32807c478bd9Sstevel@tonic-gate 	PUTROOTFH4res *resp = &resop->nfs_resop4_u.opputrootfh;
32817c478bd9Sstevel@tonic-gate 	int error;
32827c478bd9Sstevel@tonic-gate 	fid_t fid;
32837c478bd9Sstevel@tonic-gate 	struct exportinfo *exi, *sav_exi;
32847c478bd9Sstevel@tonic-gate 
32857c478bd9Sstevel@tonic-gate 	if (cs->vp) {
32867c478bd9Sstevel@tonic-gate 		VN_RELE(cs->vp);
32877c478bd9Sstevel@tonic-gate 		cs->vp = NULL;
32887c478bd9Sstevel@tonic-gate 	}
32897c478bd9Sstevel@tonic-gate 
32907c478bd9Sstevel@tonic-gate 	if (cs->cr)
32917c478bd9Sstevel@tonic-gate 		crfree(cs->cr);
32927c478bd9Sstevel@tonic-gate 
32937c478bd9Sstevel@tonic-gate 	cs->cr = crdup(cs->basecr);
32947c478bd9Sstevel@tonic-gate 
32957c478bd9Sstevel@tonic-gate 	/*
32967c478bd9Sstevel@tonic-gate 	 * Using rootdir, the system root vnode,
32977c478bd9Sstevel@tonic-gate 	 * get its fid.
32987c478bd9Sstevel@tonic-gate 	 */
32997c478bd9Sstevel@tonic-gate 	bzero(&fid, sizeof (fid));
33007c478bd9Sstevel@tonic-gate 	fid.fid_len = MAXFIDSZ;
33017c478bd9Sstevel@tonic-gate 	error = vop_fid_pseudo(rootdir, &fid);
33027c478bd9Sstevel@tonic-gate 	if (error != 0) {
33037c478bd9Sstevel@tonic-gate 		*cs->statusp = resp->status = puterrno4(error);
33047c478bd9Sstevel@tonic-gate 		return;
33057c478bd9Sstevel@tonic-gate 	}
33067c478bd9Sstevel@tonic-gate 
33077c478bd9Sstevel@tonic-gate 	/*
33087c478bd9Sstevel@tonic-gate 	 * Then use the root fsid & fid it to find out if it's exported
33097c478bd9Sstevel@tonic-gate 	 *
33107c478bd9Sstevel@tonic-gate 	 * If the server root isn't exported directly, then
33117c478bd9Sstevel@tonic-gate 	 * it should at least be a pseudo export based on
33127c478bd9Sstevel@tonic-gate 	 * one or more exports further down in the server's
33137c478bd9Sstevel@tonic-gate 	 * file tree.
33147c478bd9Sstevel@tonic-gate 	 */
33157c478bd9Sstevel@tonic-gate 	exi = checkexport4(&rootdir->v_vfsp->vfs_fsid, &fid, NULL);
33167c478bd9Sstevel@tonic-gate 	if (exi == NULL || exi->exi_export.ex_flags & EX_PUBLIC) {
33177c478bd9Sstevel@tonic-gate 		NFS4_DEBUG(rfs4_debug,
33187c478bd9Sstevel@tonic-gate 			(CE_WARN, "rfs4_op_putrootfh: export check failure"));
33197c478bd9Sstevel@tonic-gate 		*cs->statusp = resp->status = NFS4ERR_SERVERFAULT;
33207c478bd9Sstevel@tonic-gate 		return;
33217c478bd9Sstevel@tonic-gate 	}
33227c478bd9Sstevel@tonic-gate 
33237c478bd9Sstevel@tonic-gate 	/*
33247c478bd9Sstevel@tonic-gate 	 * Now make a filehandle based on the root
33257c478bd9Sstevel@tonic-gate 	 * export and root vnode.
33267c478bd9Sstevel@tonic-gate 	 */
33277c478bd9Sstevel@tonic-gate 	error = makefh4(&cs->fh, rootdir, exi);
33287c478bd9Sstevel@tonic-gate 	if (error != 0) {
33297c478bd9Sstevel@tonic-gate 		*cs->statusp = resp->status = puterrno4(error);
33307c478bd9Sstevel@tonic-gate 		return;
33317c478bd9Sstevel@tonic-gate 	}
33327c478bd9Sstevel@tonic-gate 
33337c478bd9Sstevel@tonic-gate 	sav_exi = cs->exi;
33347c478bd9Sstevel@tonic-gate 	cs->exi = exi;
33357c478bd9Sstevel@tonic-gate 
33367c478bd9Sstevel@tonic-gate 	VN_HOLD(rootdir);
33377c478bd9Sstevel@tonic-gate 	cs->vp = rootdir;
33387c478bd9Sstevel@tonic-gate 
33397c478bd9Sstevel@tonic-gate 	if ((resp->status = call_checkauth4(cs, req)) != NFS4_OK) {
33407c478bd9Sstevel@tonic-gate 		VN_RELE(rootdir);
33417c478bd9Sstevel@tonic-gate 		cs->vp = NULL;
33427c478bd9Sstevel@tonic-gate 		cs->exi = sav_exi;
33437c478bd9Sstevel@tonic-gate 		return;
33447c478bd9Sstevel@tonic-gate 	}
33457c478bd9Sstevel@tonic-gate 
33467c478bd9Sstevel@tonic-gate 	*cs->statusp = resp->status = NFS4_OK;
33477c478bd9Sstevel@tonic-gate 	cs->deleg = FALSE;
33487c478bd9Sstevel@tonic-gate }
33497c478bd9Sstevel@tonic-gate 
33507c478bd9Sstevel@tonic-gate /*
33517c478bd9Sstevel@tonic-gate  * A directory entry is a valid nfsv4 entry if
33527c478bd9Sstevel@tonic-gate  * - it has a non-zero ino
33537c478bd9Sstevel@tonic-gate  * - it is not a dot or dotdot name
33547c478bd9Sstevel@tonic-gate  * - it is visible in a pseudo export or in a real export that can
33557c478bd9Sstevel@tonic-gate  *   only have a limited view.
33567c478bd9Sstevel@tonic-gate  */
33577c478bd9Sstevel@tonic-gate static bool_t
33587c478bd9Sstevel@tonic-gate valid_nfs4_entry(struct exportinfo *exi, struct dirent64 *dp,
33597c478bd9Sstevel@tonic-gate 		int *expseudo, int check_visible)
33607c478bd9Sstevel@tonic-gate {
33617c478bd9Sstevel@tonic-gate 	if (dp->d_ino == 0 || NFS_IS_DOTNAME(dp->d_name)) {
33627c478bd9Sstevel@tonic-gate 		*expseudo = 0;
33637c478bd9Sstevel@tonic-gate 		return (FALSE);
33647c478bd9Sstevel@tonic-gate 	}
33657c478bd9Sstevel@tonic-gate 
33667c478bd9Sstevel@tonic-gate 	if (! check_visible) {
33677c478bd9Sstevel@tonic-gate 		*expseudo = 0;
33687c478bd9Sstevel@tonic-gate 		return (TRUE);
33697c478bd9Sstevel@tonic-gate 	}
33707c478bd9Sstevel@tonic-gate 
33717c478bd9Sstevel@tonic-gate 	return (nfs_visible_inode(exi, dp->d_ino, expseudo));
33727c478bd9Sstevel@tonic-gate }
33737c478bd9Sstevel@tonic-gate 
33747c478bd9Sstevel@tonic-gate /*
33757c478bd9Sstevel@tonic-gate  * set_rdattr_params sets up the variables used to manage what information
33767c478bd9Sstevel@tonic-gate  * to get for each directory entry.
33777c478bd9Sstevel@tonic-gate  */
33787c478bd9Sstevel@tonic-gate static nfsstat4
33797c478bd9Sstevel@tonic-gate set_rdattr_params(struct nfs4_svgetit_arg *sargp,
33807c478bd9Sstevel@tonic-gate 		bitmap4 attrs, bool_t *need_to_lookup)
33817c478bd9Sstevel@tonic-gate {
33827c478bd9Sstevel@tonic-gate 	uint_t	va_mask;
33837c478bd9Sstevel@tonic-gate 	nfsstat4 status;
33847c478bd9Sstevel@tonic-gate 	bitmap4 objbits;
33857c478bd9Sstevel@tonic-gate 
33867c478bd9Sstevel@tonic-gate 	status = bitmap4_to_attrmask(attrs, sargp);
33877c478bd9Sstevel@tonic-gate 	if (status != NFS4_OK) {
33887c478bd9Sstevel@tonic-gate 		/*
33897c478bd9Sstevel@tonic-gate 		 * could not even figure attr mask
33907c478bd9Sstevel@tonic-gate 		 */
33917c478bd9Sstevel@tonic-gate 		return (status);
33927c478bd9Sstevel@tonic-gate 	}
33937c478bd9Sstevel@tonic-gate 	va_mask = sargp->vap->va_mask;
33947c478bd9Sstevel@tonic-gate 
33957c478bd9Sstevel@tonic-gate 	/*
33967c478bd9Sstevel@tonic-gate 	 * dirent's d_ino is always correct value for mounted_on_fileid.
33977c478bd9Sstevel@tonic-gate 	 * mntdfid_set is set once here, but mounted_on_fileid is
33987c478bd9Sstevel@tonic-gate 	 * set in main dirent processing loop for each dirent.
33997c478bd9Sstevel@tonic-gate 	 * The mntdfid_set is a simple optimization that lets the
34007c478bd9Sstevel@tonic-gate 	 * server attr code avoid work when caller is readdir.
34017c478bd9Sstevel@tonic-gate 	 */
34027c478bd9Sstevel@tonic-gate 	sargp->mntdfid_set = TRUE;
34037c478bd9Sstevel@tonic-gate 
34047c478bd9Sstevel@tonic-gate 	/*
34057c478bd9Sstevel@tonic-gate 	 * Lookup entry only if client asked for any of the following:
34067c478bd9Sstevel@tonic-gate 	 * a) vattr attrs
34077c478bd9Sstevel@tonic-gate 	 * b) vfs attrs
34087c478bd9Sstevel@tonic-gate 	 * c) attrs w/per-object scope requested (change, filehandle, etc)
34097c478bd9Sstevel@tonic-gate 	 *    other than mounted_on_fileid (which we can take from dirent)
34107c478bd9Sstevel@tonic-gate 	 */
34117c478bd9Sstevel@tonic-gate 	objbits = attrs ? attrs & NFS4_VP_ATTR_MASK : 0;
34127c478bd9Sstevel@tonic-gate 
34137c478bd9Sstevel@tonic-gate 	if (va_mask || sargp->sbp || (objbits & ~FATTR4_MOUNTED_ON_FILEID_MASK))
34147c478bd9Sstevel@tonic-gate 		*need_to_lookup = TRUE;
34157c478bd9Sstevel@tonic-gate 	else
34167c478bd9Sstevel@tonic-gate 		*need_to_lookup = FALSE;
34177c478bd9Sstevel@tonic-gate 
34187c478bd9Sstevel@tonic-gate 	if (sargp->sbp == NULL)
34197c478bd9Sstevel@tonic-gate 		return (NFS4_OK);
34207c478bd9Sstevel@tonic-gate 
34217c478bd9Sstevel@tonic-gate 	/*
34227c478bd9Sstevel@tonic-gate 	 * If filesystem attrs are requested, get them now from the
34237c478bd9Sstevel@tonic-gate 	 * directory vp, as most entries will have same filesystem. The only
34247c478bd9Sstevel@tonic-gate 	 * exception are mounted over entries but we handle
34257c478bd9Sstevel@tonic-gate 	 * those as we go (XXX mounted over detection not yet implemented).
34267c478bd9Sstevel@tonic-gate 	 */
34277c478bd9Sstevel@tonic-gate 	sargp->vap->va_mask = 0;	/* to avoid VOP_GETATTR */
34287c478bd9Sstevel@tonic-gate 	status = bitmap4_get_sysattrs(sargp);
34297c478bd9Sstevel@tonic-gate 	sargp->vap->va_mask = va_mask;
34307c478bd9Sstevel@tonic-gate 
34317c478bd9Sstevel@tonic-gate 	if ((status != NFS4_OK) && sargp->rdattr_error_req) {
34327c478bd9Sstevel@tonic-gate 		/*
34337c478bd9Sstevel@tonic-gate 		 * Failed to get filesystem attributes.
34347c478bd9Sstevel@tonic-gate 		 * Return a rdattr_error for each entry, but don't fail.
34357c478bd9Sstevel@tonic-gate 		 * However, don't get any obj-dependent attrs.
34367c478bd9Sstevel@tonic-gate 		 */
34377c478bd9Sstevel@tonic-gate 		sargp->rdattr_error = status;	/* for rdattr_error */
34387c478bd9Sstevel@tonic-gate 		*need_to_lookup = FALSE;
34397c478bd9Sstevel@tonic-gate 		/*
34407c478bd9Sstevel@tonic-gate 		 * At least get fileid for regular readdir output
34417c478bd9Sstevel@tonic-gate 		 */
34427c478bd9Sstevel@tonic-gate 		sargp->vap->va_mask &= AT_NODEID;
34437c478bd9Sstevel@tonic-gate 		status = NFS4_OK;
34447c478bd9Sstevel@tonic-gate 	}
34457c478bd9Sstevel@tonic-gate 
34467c478bd9Sstevel@tonic-gate 	return (status);
34477c478bd9Sstevel@tonic-gate }
34487c478bd9Sstevel@tonic-gate 
34497c478bd9Sstevel@tonic-gate /*
34507c478bd9Sstevel@tonic-gate  * readlink: args: CURRENT_FH.
34517c478bd9Sstevel@tonic-gate  *	res: status. If success - CURRENT_FH unchanged, return linktext.
34527c478bd9Sstevel@tonic-gate  */
34537c478bd9Sstevel@tonic-gate 
34547c478bd9Sstevel@tonic-gate /* ARGSUSED */
34557c478bd9Sstevel@tonic-gate static void
34567c478bd9Sstevel@tonic-gate rfs4_op_readlink(nfs_argop4 *argop, nfs_resop4 *resop, struct svc_req *req,
34577c478bd9Sstevel@tonic-gate 	struct compound_state *cs)
34587c478bd9Sstevel@tonic-gate {
34597c478bd9Sstevel@tonic-gate 	READLINK4res *resp = &resop->nfs_resop4_u.opreadlink;
34607c478bd9Sstevel@tonic-gate 	int error;
34617c478bd9Sstevel@tonic-gate 	vnode_t *vp;
34627c478bd9Sstevel@tonic-gate 	struct iovec iov;
34637c478bd9Sstevel@tonic-gate 	struct vattr va;
34647c478bd9Sstevel@tonic-gate 	struct uio uio;
34657c478bd9Sstevel@tonic-gate 	char *data;
34667c478bd9Sstevel@tonic-gate 
34677c478bd9Sstevel@tonic-gate 	/* CURRENT_FH: directory */
34687c478bd9Sstevel@tonic-gate 	vp = cs->vp;
34697c478bd9Sstevel@tonic-gate 	if (vp == NULL) {
34707c478bd9Sstevel@tonic-gate 		*cs->statusp = resp->status = NFS4ERR_NOFILEHANDLE;
34717c478bd9Sstevel@tonic-gate 		return;
34727c478bd9Sstevel@tonic-gate 	}
34737c478bd9Sstevel@tonic-gate 
34747c478bd9Sstevel@tonic-gate 	if (cs->access == CS_ACCESS_DENIED) {
34757c478bd9Sstevel@tonic-gate 		*cs->statusp = resp->status = NFS4ERR_ACCESS;
34767c478bd9Sstevel@tonic-gate 		return;
34777c478bd9Sstevel@tonic-gate 	}
34787c478bd9Sstevel@tonic-gate 
34797c478bd9Sstevel@tonic-gate 	if (vp->v_type == VDIR) {
34807c478bd9Sstevel@tonic-gate 		*cs->statusp = resp->status = NFS4ERR_ISDIR;
34817c478bd9Sstevel@tonic-gate 		return;
34827c478bd9Sstevel@tonic-gate 	}
34837c478bd9Sstevel@tonic-gate 
34847c478bd9Sstevel@tonic-gate 	if (vp->v_type != VLNK) {
34857c478bd9Sstevel@tonic-gate 		*cs->statusp = resp->status = NFS4ERR_INVAL;
34867c478bd9Sstevel@tonic-gate 		return;
34877c478bd9Sstevel@tonic-gate 	}
34887c478bd9Sstevel@tonic-gate 
34897c478bd9Sstevel@tonic-gate 	va.va_mask = AT_MODE;
34907c478bd9Sstevel@tonic-gate 	error = VOP_GETATTR(vp, &va, 0, cs->cr);
34917c478bd9Sstevel@tonic-gate 	if (error) {
34927c478bd9Sstevel@tonic-gate 		*cs->statusp = resp->status = puterrno4(error);
34937c478bd9Sstevel@tonic-gate 		return;
34947c478bd9Sstevel@tonic-gate 	}
34957c478bd9Sstevel@tonic-gate 
34967c478bd9Sstevel@tonic-gate 	if (MANDLOCK(vp, va.va_mode)) {
34977c478bd9Sstevel@tonic-gate 		*cs->statusp = resp->status = NFS4ERR_ACCESS;
34987c478bd9Sstevel@tonic-gate 		return;
34997c478bd9Sstevel@tonic-gate 	}
35007c478bd9Sstevel@tonic-gate 
35017c478bd9Sstevel@tonic-gate 	data = kmem_alloc(MAXPATHLEN + 1, KM_SLEEP);
35027c478bd9Sstevel@tonic-gate 
35037c478bd9Sstevel@tonic-gate 	iov.iov_base = data;
35047c478bd9Sstevel@tonic-gate 	iov.iov_len = MAXPATHLEN;
35057c478bd9Sstevel@tonic-gate 	uio.uio_iov = &iov;
35067c478bd9Sstevel@tonic-gate 	uio.uio_iovcnt = 1;
35077c478bd9Sstevel@tonic-gate 	uio.uio_segflg = UIO_SYSSPACE;
35087c478bd9Sstevel@tonic-gate 	uio.uio_extflg = UIO_COPY_CACHED;
35097c478bd9Sstevel@tonic-gate 	uio.uio_loffset = 0;
35107c478bd9Sstevel@tonic-gate 	uio.uio_resid = MAXPATHLEN;
35117c478bd9Sstevel@tonic-gate 
35127c478bd9Sstevel@tonic-gate 	error = VOP_READLINK(vp, &uio, cs->cr);
35137c478bd9Sstevel@tonic-gate 
35147c478bd9Sstevel@tonic-gate 	if (error) {
35157c478bd9Sstevel@tonic-gate 		kmem_free((caddr_t)data, (uint_t)MAXPATHLEN + 1);
35167c478bd9Sstevel@tonic-gate 		*cs->statusp = resp->status = puterrno4(error);
35177c478bd9Sstevel@tonic-gate 		return;
35187c478bd9Sstevel@tonic-gate 	}
35197c478bd9Sstevel@tonic-gate 
35207c478bd9Sstevel@tonic-gate 	*(data + MAXPATHLEN - uio.uio_resid) = '\0';
35217c478bd9Sstevel@tonic-gate 
35227c478bd9Sstevel@tonic-gate 	/*
35237c478bd9Sstevel@tonic-gate 	 * treat link name as data
35247c478bd9Sstevel@tonic-gate 	 */
35257c478bd9Sstevel@tonic-gate 	(void) str_to_utf8(data, &resp->link);
35267c478bd9Sstevel@tonic-gate 
35277c478bd9Sstevel@tonic-gate 	kmem_free((caddr_t)data, (uint_t)MAXPATHLEN + 1);
35287c478bd9Sstevel@tonic-gate 	*cs->statusp = resp->status = NFS4_OK;
35297c478bd9Sstevel@tonic-gate }
35307c478bd9Sstevel@tonic-gate 
35317c478bd9Sstevel@tonic-gate static void
35327c478bd9Sstevel@tonic-gate rfs4_op_readlink_free(nfs_resop4 *resop)
35337c478bd9Sstevel@tonic-gate {
35347c478bd9Sstevel@tonic-gate 	READLINK4res *resp = &resop->nfs_resop4_u.opreadlink;
35357c478bd9Sstevel@tonic-gate 	utf8string *symlink = &resp->link;
35367c478bd9Sstevel@tonic-gate 
35377c478bd9Sstevel@tonic-gate 	if (symlink->utf8string_val) {
35387c478bd9Sstevel@tonic-gate 		UTF8STRING_FREE(*symlink)
35397c478bd9Sstevel@tonic-gate 	}
35407c478bd9Sstevel@tonic-gate }
35417c478bd9Sstevel@tonic-gate 
35427c478bd9Sstevel@tonic-gate /*
35437c478bd9Sstevel@tonic-gate  * release_lockowner:
35447c478bd9Sstevel@tonic-gate  *	Release any state associated with the supplied
35457c478bd9Sstevel@tonic-gate  *	lockowner. Note if any lo_state is holding locks we will not
35467c478bd9Sstevel@tonic-gate  *	rele that lo_state and thus the lockowner will not be destroyed.
35477c478bd9Sstevel@tonic-gate  *	A client using lock after the lock owner stateid has been released
35487c478bd9Sstevel@tonic-gate  *	will suffer the consequence of NFS4ERR_BAD_STATEID and would have
35497c478bd9Sstevel@tonic-gate  *	to reissue the lock with new_lock_owner set to TRUE.
35507c478bd9Sstevel@tonic-gate  *	args: lock_owner
35517c478bd9Sstevel@tonic-gate  *	res:  status
35527c478bd9Sstevel@tonic-gate  */
35537c478bd9Sstevel@tonic-gate /* ARGSUSED */
35547c478bd9Sstevel@tonic-gate static void
35557c478bd9Sstevel@tonic-gate rfs4_op_release_lockowner(nfs_argop4 *argop, nfs_resop4 *resop,
35567c478bd9Sstevel@tonic-gate 	struct svc_req *req, struct compound_state *cs)
35577c478bd9Sstevel@tonic-gate {
35587c478bd9Sstevel@tonic-gate 	RELEASE_LOCKOWNER4args *ap = &argop->nfs_argop4_u.oprelease_lockowner;
35597c478bd9Sstevel@tonic-gate 	RELEASE_LOCKOWNER4res *resp = &resop->nfs_resop4_u.oprelease_lockowner;
35607c478bd9Sstevel@tonic-gate 	rfs4_lockowner_t *lo;
35617c478bd9Sstevel@tonic-gate 	rfs4_openowner_t *oop;
35627c478bd9Sstevel@tonic-gate 	rfs4_state_t *sp;
35637c478bd9Sstevel@tonic-gate 	rfs4_lo_state_t *lsp;
35647c478bd9Sstevel@tonic-gate 	rfs4_client_t *cp;
35657c478bd9Sstevel@tonic-gate 	bool_t create = FALSE;
35667c478bd9Sstevel@tonic-gate 	locklist_t *llist;
35677c478bd9Sstevel@tonic-gate 	sysid_t sysid;
35687c478bd9Sstevel@tonic-gate 
35697c478bd9Sstevel@tonic-gate 	/* Make sure there is a clientid around for this request */
35707c478bd9Sstevel@tonic-gate 	cp = rfs4_findclient_by_id(ap->lock_owner.clientid, FALSE);
35717c478bd9Sstevel@tonic-gate 
35727c478bd9Sstevel@tonic-gate 	if (cp == NULL) {
35737c478bd9Sstevel@tonic-gate 		*cs->statusp = resp->status =
35747c478bd9Sstevel@tonic-gate 			rfs4_check_clientid(&ap->lock_owner.clientid, 0);
35757c478bd9Sstevel@tonic-gate 		return;
35767c478bd9Sstevel@tonic-gate 	}
35777c478bd9Sstevel@tonic-gate 	rfs4_client_rele(cp);
35787c478bd9Sstevel@tonic-gate 
35797c478bd9Sstevel@tonic-gate 	lo = rfs4_findlockowner(&ap->lock_owner, &create);
35807c478bd9Sstevel@tonic-gate 	if (lo == NULL) {
35817c478bd9Sstevel@tonic-gate 		*cs->statusp = resp->status = NFS4_OK;
35827c478bd9Sstevel@tonic-gate 		return;
35837c478bd9Sstevel@tonic-gate 	}
35847c478bd9Sstevel@tonic-gate 	ASSERT(lo->client != NULL);
35857c478bd9Sstevel@tonic-gate 
35867c478bd9Sstevel@tonic-gate 	/*
35877c478bd9Sstevel@tonic-gate 	 * Check for EXPIRED client. If so will reap state with in a lease
35887c478bd9Sstevel@tonic-gate 	 * period or on next set_clientid_confirm step
35897c478bd9Sstevel@tonic-gate 	 */
35907c478bd9Sstevel@tonic-gate 	if (rfs4_lease_expired(lo->client)) {
35917c478bd9Sstevel@tonic-gate 		rfs4_lockowner_rele(lo);
35927c478bd9Sstevel@tonic-gate 		*cs->statusp = resp->status = NFS4ERR_EXPIRED;
35937c478bd9Sstevel@tonic-gate 		return;
35947c478bd9Sstevel@tonic-gate 	}
35957c478bd9Sstevel@tonic-gate 
35967c478bd9Sstevel@tonic-gate 	/*
35977c478bd9Sstevel@tonic-gate 	 * If no sysid has been assigned, then no locks exist; just return.
35987c478bd9Sstevel@tonic-gate 	 */
35997c478bd9Sstevel@tonic-gate 	rfs4_dbe_lock(lo->client->dbe);
36007c478bd9Sstevel@tonic-gate 	if (lo->client->sysidt == LM_NOSYSID) {
36017c478bd9Sstevel@tonic-gate 		rfs4_lockowner_rele(lo);
36027c478bd9Sstevel@tonic-gate 		rfs4_dbe_unlock(lo->client->dbe);
36037c478bd9Sstevel@tonic-gate 		return;
36047c478bd9Sstevel@tonic-gate 	}
36057c478bd9Sstevel@tonic-gate 
36067c478bd9Sstevel@tonic-gate 	sysid = lo->client->sysidt;
36077c478bd9Sstevel@tonic-gate 	rfs4_dbe_unlock(lo->client->dbe);
36087c478bd9Sstevel@tonic-gate 
36097c478bd9Sstevel@tonic-gate 	/*
36107c478bd9Sstevel@tonic-gate 	 * Mark the lockowner invalid.
36117c478bd9Sstevel@tonic-gate 	 */
36127c478bd9Sstevel@tonic-gate 	rfs4_dbe_hide(lo->dbe);
36137c478bd9Sstevel@tonic-gate 
36147c478bd9Sstevel@tonic-gate 	/*
36157c478bd9Sstevel@tonic-gate 	 * sysid-pid pair should now not be used since the lockowner is
36167c478bd9Sstevel@tonic-gate 	 * invalid. If the client were to instantiate the lockowner again
36177c478bd9Sstevel@tonic-gate 	 * it would be assigned a new pid. Thus we can get the list of
36187c478bd9Sstevel@tonic-gate 	 * current locks.
36197c478bd9Sstevel@tonic-gate 	 */
36207c478bd9Sstevel@tonic-gate 
36217c478bd9Sstevel@tonic-gate 	llist = flk_get_active_locks(sysid, lo->pid);
36227c478bd9Sstevel@tonic-gate 	/* If we are still holding locks fail */
36237c478bd9Sstevel@tonic-gate 	if (llist != NULL) {
36247c478bd9Sstevel@tonic-gate 
36257c478bd9Sstevel@tonic-gate 		*cs->statusp = resp->status = NFS4ERR_LOCKS_HELD;
36267c478bd9Sstevel@tonic-gate 
36277c478bd9Sstevel@tonic-gate 		flk_free_locklist(llist);
36287c478bd9Sstevel@tonic-gate 		/*
36297c478bd9Sstevel@tonic-gate 		 * We need to unhide the lockowner so the client can
36307c478bd9Sstevel@tonic-gate 		 * try it again. The bad thing here is if the client
36317c478bd9Sstevel@tonic-gate 		 * has a logic error that took it here in the first place
36327c478bd9Sstevel@tonic-gate 		 * he probably has lost accounting of the locks that it
36337c478bd9Sstevel@tonic-gate 		 * is holding. So we may have dangling state until the
36347c478bd9Sstevel@tonic-gate 		 * open owner state is reaped via close. One scenario
36357c478bd9Sstevel@tonic-gate 		 * that could possibly occur is that the client has
36367c478bd9Sstevel@tonic-gate 		 * sent the unlock request(s) in separate threads
36377c478bd9Sstevel@tonic-gate 		 * and has not waited for the replies before sending the
36387c478bd9Sstevel@tonic-gate 		 * RELEASE_LOCKOWNER request. Presumably, it would expect
36397c478bd9Sstevel@tonic-gate 		 * and deal appropriately with NFS4ERR_LOCKS_HELD, by
36407c478bd9Sstevel@tonic-gate 		 * reissuing the request.
36417c478bd9Sstevel@tonic-gate 		 */
36427c478bd9Sstevel@tonic-gate 		rfs4_dbe_unhide(lo->dbe);
36437c478bd9Sstevel@tonic-gate 		rfs4_lockowner_rele(lo);
36447c478bd9Sstevel@tonic-gate 		return;
36457c478bd9Sstevel@tonic-gate 	}
36467c478bd9Sstevel@tonic-gate 
36477c478bd9Sstevel@tonic-gate 	/*
36487c478bd9Sstevel@tonic-gate 	 * For the corresponding client we need to check each open
36497c478bd9Sstevel@tonic-gate 	 * owner for any opens that have lockowner state associated
36507c478bd9Sstevel@tonic-gate 	 * with this lockowner.
36517c478bd9Sstevel@tonic-gate 	 */
36527c478bd9Sstevel@tonic-gate 
36537c478bd9Sstevel@tonic-gate 	rfs4_dbe_lock(lo->client->dbe);
36547c478bd9Sstevel@tonic-gate 	for (oop = lo->client->openownerlist.next->oop; oop != NULL;
36557c478bd9Sstevel@tonic-gate 	    oop = oop->openownerlist.next->oop) {
36567c478bd9Sstevel@tonic-gate 
36577c478bd9Sstevel@tonic-gate 		rfs4_dbe_lock(oop->dbe);
36587c478bd9Sstevel@tonic-gate 		for (sp = oop->ownerstateids.next->sp; sp != NULL;
36597c478bd9Sstevel@tonic-gate 		    sp = sp->ownerstateids.next->sp) {
36607c478bd9Sstevel@tonic-gate 
36617c478bd9Sstevel@tonic-gate 			rfs4_dbe_lock(sp->dbe);
36627c478bd9Sstevel@tonic-gate 			for (lsp = sp->lockownerlist.next->lsp;
36637c478bd9Sstevel@tonic-gate 			    lsp != NULL; lsp = lsp->lockownerlist.next->lsp) {
36647c478bd9Sstevel@tonic-gate 				if (lsp->locker == lo) {
36657c478bd9Sstevel@tonic-gate 					rfs4_dbe_lock(lsp->dbe);
36667c478bd9Sstevel@tonic-gate 					rfs4_dbe_invalidate(lsp->dbe);
36677c478bd9Sstevel@tonic-gate 					rfs4_dbe_unlock(lsp->dbe);
36687c478bd9Sstevel@tonic-gate 				}
36697c478bd9Sstevel@tonic-gate 			}
36707c478bd9Sstevel@tonic-gate 			rfs4_dbe_unlock(sp->dbe);
36717c478bd9Sstevel@tonic-gate 		}
36727c478bd9Sstevel@tonic-gate 		rfs4_dbe_unlock(oop->dbe);
36737c478bd9Sstevel@tonic-gate 	}
36747c478bd9Sstevel@tonic-gate 	rfs4_dbe_unlock(lo->client->dbe);
36757c478bd9Sstevel@tonic-gate 
36767c478bd9Sstevel@tonic-gate 	rfs4_lockowner_rele(lo);
36777c478bd9Sstevel@tonic-gate 
36787c478bd9Sstevel@tonic-gate 	*cs->statusp = resp->status = NFS4_OK;
36797c478bd9Sstevel@tonic-gate }
36807c478bd9Sstevel@tonic-gate 
36817c478bd9Sstevel@tonic-gate /*
36827c478bd9Sstevel@tonic-gate  * short utility function to lookup a file and recall the delegation
36837c478bd9Sstevel@tonic-gate  */
36847c478bd9Sstevel@tonic-gate static rfs4_file_t *
36857c478bd9Sstevel@tonic-gate rfs4_lookup_and_findfile(vnode_t *dvp, char *nm, vnode_t **vpp,
36867c478bd9Sstevel@tonic-gate 	int *lkup_error, cred_t *cr)
36877c478bd9Sstevel@tonic-gate {
36887c478bd9Sstevel@tonic-gate 	vnode_t *vp;
36897c478bd9Sstevel@tonic-gate 	rfs4_file_t *fp = NULL;
36907c478bd9Sstevel@tonic-gate 	bool_t fcreate = FALSE;
36917c478bd9Sstevel@tonic-gate 	int error;
36927c478bd9Sstevel@tonic-gate 
36937c478bd9Sstevel@tonic-gate 	if (vpp)
36947c478bd9Sstevel@tonic-gate 		*vpp = NULL;
36957c478bd9Sstevel@tonic-gate 
36967c478bd9Sstevel@tonic-gate 	if ((error = VOP_LOOKUP(dvp, nm, &vp, NULL, 0, NULL, cr)) == 0) {
36977c478bd9Sstevel@tonic-gate 		if (vp->v_type == VREG)
36987c478bd9Sstevel@tonic-gate 			fp = rfs4_findfile(vp, NULL, &fcreate);
36997c478bd9Sstevel@tonic-gate 		if (vpp)
37007c478bd9Sstevel@tonic-gate 			*vpp = vp;
37017c478bd9Sstevel@tonic-gate 		else
37027c478bd9Sstevel@tonic-gate 			VN_RELE(vp);
37037c478bd9Sstevel@tonic-gate 	}
37047c478bd9Sstevel@tonic-gate 
37057c478bd9Sstevel@tonic-gate 	if (lkup_error)
37067c478bd9Sstevel@tonic-gate 		*lkup_error = error;
37077c478bd9Sstevel@tonic-gate 
37087c478bd9Sstevel@tonic-gate 	return (fp);
37097c478bd9Sstevel@tonic-gate }
37107c478bd9Sstevel@tonic-gate 
37117c478bd9Sstevel@tonic-gate /*
37127c478bd9Sstevel@tonic-gate  * remove: args: CURRENT_FH: directory; name.
37137c478bd9Sstevel@tonic-gate  *	res: status. If success - CURRENT_FH unchanged, return change_info
37147c478bd9Sstevel@tonic-gate  *		for directory.
37157c478bd9Sstevel@tonic-gate  */
37167c478bd9Sstevel@tonic-gate /* ARGSUSED */
37177c478bd9Sstevel@tonic-gate static void
37187c478bd9Sstevel@tonic-gate rfs4_op_remove(nfs_argop4 *argop, nfs_resop4 *resop, struct svc_req *req,
37197c478bd9Sstevel@tonic-gate 	struct compound_state *cs)
37207c478bd9Sstevel@tonic-gate {
37217c478bd9Sstevel@tonic-gate 	REMOVE4args *args = &argop->nfs_argop4_u.opremove;
37227c478bd9Sstevel@tonic-gate 	REMOVE4res *resp = &resop->nfs_resop4_u.opremove;
37237c478bd9Sstevel@tonic-gate 	int error;
37247c478bd9Sstevel@tonic-gate 	vnode_t *dvp, *vp;
37257c478bd9Sstevel@tonic-gate 	struct vattr bdva, idva, adva;
37267c478bd9Sstevel@tonic-gate 	char *nm;
37277c478bd9Sstevel@tonic-gate 	uint_t len;
37287c478bd9Sstevel@tonic-gate 	rfs4_file_t *fp;
37297c478bd9Sstevel@tonic-gate 	int in_crit = 0;
373045916cd2Sjpk 	bslabel_t *clabel;
37317c478bd9Sstevel@tonic-gate 
37327c478bd9Sstevel@tonic-gate 	/* CURRENT_FH: directory */
37337c478bd9Sstevel@tonic-gate 	dvp = cs->vp;
37347c478bd9Sstevel@tonic-gate 	if (dvp == NULL) {
37357c478bd9Sstevel@tonic-gate 		*cs->statusp = resp->status = NFS4ERR_NOFILEHANDLE;
37367c478bd9Sstevel@tonic-gate 		return;
37377c478bd9Sstevel@tonic-gate 	}
37387c478bd9Sstevel@tonic-gate 
37397c478bd9Sstevel@tonic-gate 	if (cs->access == CS_ACCESS_DENIED) {
37407c478bd9Sstevel@tonic-gate 		*cs->statusp = resp->status = NFS4ERR_ACCESS;
37417c478bd9Sstevel@tonic-gate 		return;
37427c478bd9Sstevel@tonic-gate 	}
37437c478bd9Sstevel@tonic-gate 
37447c478bd9Sstevel@tonic-gate 	/*
37457c478bd9Sstevel@tonic-gate 	 * If there is an unshared filesystem mounted on this vnode,
37467c478bd9Sstevel@tonic-gate 	 * Do not allow to remove anything in this directory.
37477c478bd9Sstevel@tonic-gate 	 */
37487c478bd9Sstevel@tonic-gate 	if (vn_ismntpt(dvp)) {
37497c478bd9Sstevel@tonic-gate 		*cs->statusp = resp->status = NFS4ERR_ACCESS;
37507c478bd9Sstevel@tonic-gate 		return;
37517c478bd9Sstevel@tonic-gate 	}
37527c478bd9Sstevel@tonic-gate 
37537c478bd9Sstevel@tonic-gate 	if (dvp->v_type != VDIR) {
37547c478bd9Sstevel@tonic-gate 		*cs->statusp = resp->status = NFS4ERR_NOTDIR;
37557c478bd9Sstevel@tonic-gate 		return;
37567c478bd9Sstevel@tonic-gate 	}
37577c478bd9Sstevel@tonic-gate 
37587c478bd9Sstevel@tonic-gate 	if (!utf8_dir_verify(&args->target)) {
37597c478bd9Sstevel@tonic-gate 		*cs->statusp = resp->status = NFS4ERR_INVAL;
37607c478bd9Sstevel@tonic-gate 		return;
37617c478bd9Sstevel@tonic-gate 	}
37627c478bd9Sstevel@tonic-gate 
37637c478bd9Sstevel@tonic-gate 	/*
37647c478bd9Sstevel@tonic-gate 	 * Lookup the file so that we can check if it's a directory
37657c478bd9Sstevel@tonic-gate 	 */
37667c478bd9Sstevel@tonic-gate 	nm = utf8_to_fn(&args->target, &len, NULL);
37677c478bd9Sstevel@tonic-gate 	if (nm == NULL) {
37687c478bd9Sstevel@tonic-gate 		*cs->statusp = resp->status = NFS4ERR_INVAL;
37697c478bd9Sstevel@tonic-gate 		return;
37707c478bd9Sstevel@tonic-gate 	}
37717c478bd9Sstevel@tonic-gate 
37727c478bd9Sstevel@tonic-gate 	if (len > MAXNAMELEN) {
37737c478bd9Sstevel@tonic-gate 		*cs->statusp = resp->status = NFS4ERR_NAMETOOLONG;
37747c478bd9Sstevel@tonic-gate 		kmem_free(nm, len);
37757c478bd9Sstevel@tonic-gate 		return;
37767c478bd9Sstevel@tonic-gate 	}
37777c478bd9Sstevel@tonic-gate 
37787c478bd9Sstevel@tonic-gate 	if (rdonly4(cs->exi, cs->vp, req)) {
37797c478bd9Sstevel@tonic-gate 		*cs->statusp = resp->status = NFS4ERR_ROFS;
37807c478bd9Sstevel@tonic-gate 		kmem_free(nm, len);
37817c478bd9Sstevel@tonic-gate 		return;
37827c478bd9Sstevel@tonic-gate 	}
37837c478bd9Sstevel@tonic-gate 
37847c478bd9Sstevel@tonic-gate 	/*
37857c478bd9Sstevel@tonic-gate 	 * Lookup the file to determine type and while we are see if
37867c478bd9Sstevel@tonic-gate 	 * there is a file struct around and check for delegation.
37877c478bd9Sstevel@tonic-gate 	 * We don't need to acquire va_seq before this lookup, if
37887c478bd9Sstevel@tonic-gate 	 * it causes an update, cinfo.before will not match, which will
37897c478bd9Sstevel@tonic-gate 	 * trigger a cache flush even if atomic is TRUE.
37907c478bd9Sstevel@tonic-gate 	 */
37917c478bd9Sstevel@tonic-gate 	if (fp = rfs4_lookup_and_findfile(dvp, nm, &vp, &error, cs->cr)) {
37927c478bd9Sstevel@tonic-gate 		if (rfs4_check_delegated_byfp(FWRITE, fp, TRUE, TRUE, TRUE,
37937c478bd9Sstevel@tonic-gate 						NULL)) {
37947c478bd9Sstevel@tonic-gate 			VN_RELE(vp);
37957c478bd9Sstevel@tonic-gate 			rfs4_file_rele(fp);
37967c478bd9Sstevel@tonic-gate 			*cs->statusp = resp->status = NFS4ERR_DELAY;
37977c478bd9Sstevel@tonic-gate 			kmem_free(nm, len);
37987c478bd9Sstevel@tonic-gate 			return;
37997c478bd9Sstevel@tonic-gate 		}
38007c478bd9Sstevel@tonic-gate 	}
38017c478bd9Sstevel@tonic-gate 
38027c478bd9Sstevel@tonic-gate 	/* Didn't find anything to remove */
38037c478bd9Sstevel@tonic-gate 	if (vp == NULL) {
38047c478bd9Sstevel@tonic-gate 		*cs->statusp = resp->status = error;
38057c478bd9Sstevel@tonic-gate 		kmem_free(nm, len);
38067c478bd9Sstevel@tonic-gate 		return;
38077c478bd9Sstevel@tonic-gate 	}
38087c478bd9Sstevel@tonic-gate 
38097c478bd9Sstevel@tonic-gate 	if (nbl_need_check(vp)) {
38107c478bd9Sstevel@tonic-gate 		nbl_start_crit(vp, RW_READER);
38117c478bd9Sstevel@tonic-gate 		in_crit = 1;
38127c478bd9Sstevel@tonic-gate 		if (nbl_conflict(vp, NBL_REMOVE, 0, 0, 0)) {
38137c478bd9Sstevel@tonic-gate 			*cs->statusp = resp->status = NFS4ERR_FILE_OPEN;
38147c478bd9Sstevel@tonic-gate 			kmem_free(nm, len);
38157c478bd9Sstevel@tonic-gate 			nbl_end_crit(vp);
38167c478bd9Sstevel@tonic-gate 			VN_RELE(vp);
38177c478bd9Sstevel@tonic-gate 			if (fp) {
38187c478bd9Sstevel@tonic-gate 				rfs4_clear_dont_grant(fp);
38197c478bd9Sstevel@tonic-gate 				rfs4_file_rele(fp);
38207c478bd9Sstevel@tonic-gate 			}
38217c478bd9Sstevel@tonic-gate 			return;
38227c478bd9Sstevel@tonic-gate 		}
38237c478bd9Sstevel@tonic-gate 	}
38247c478bd9Sstevel@tonic-gate 
382545916cd2Sjpk 	/* check label before allowing removal */
382645916cd2Sjpk 	if (is_system_labeled()) {
382745916cd2Sjpk 		ASSERT(req->rq_label != NULL);
382845916cd2Sjpk 		clabel = req->rq_label;
382945916cd2Sjpk 		DTRACE_PROBE2(tx__rfs4__log__info__opremove__clabel, char *,
383045916cd2Sjpk 		    "got client label from request(1)",
383145916cd2Sjpk 		    struct svc_req *, req);
383245916cd2Sjpk 		if (!blequal(&l_admin_low->tsl_label, clabel)) {
383345916cd2Sjpk 			if (!do_rfs4_label_check(clabel, vp, EQUALITY_CHECK)) {
383445916cd2Sjpk 				*cs->statusp = resp->status = NFS4ERR_ACCESS;
383545916cd2Sjpk 				kmem_free(nm, len);
383645916cd2Sjpk 				if (in_crit)
383745916cd2Sjpk 					nbl_end_crit(vp);
383845916cd2Sjpk 				VN_RELE(vp);
383945916cd2Sjpk 				if (fp) {
384045916cd2Sjpk 					rfs4_clear_dont_grant(fp);
384145916cd2Sjpk 					rfs4_file_rele(fp);
384245916cd2Sjpk 				}
384345916cd2Sjpk 				return;
384445916cd2Sjpk 			}
384545916cd2Sjpk 		}
384645916cd2Sjpk 	}
384745916cd2Sjpk 
38487c478bd9Sstevel@tonic-gate 	/* Get dir "before" change value */
38497c478bd9Sstevel@tonic-gate 	bdva.va_mask = AT_CTIME|AT_SEQ;
38507c478bd9Sstevel@tonic-gate 	error = VOP_GETATTR(dvp, &bdva, 0, cs->cr);
38517c478bd9Sstevel@tonic-gate 	if (error) {
38527c478bd9Sstevel@tonic-gate 		*cs->statusp = resp->status = puterrno4(error);
38537c478bd9Sstevel@tonic-gate 		kmem_free(nm, len);
38547c478bd9Sstevel@tonic-gate 		return;
38557c478bd9Sstevel@tonic-gate 	}
38567c478bd9Sstevel@tonic-gate 	NFS4_SET_FATTR4_CHANGE(resp->cinfo.before, bdva.va_ctime)
38577c478bd9Sstevel@tonic-gate 
38587c478bd9Sstevel@tonic-gate 	/* Actually do the REMOVE operation */
38597c478bd9Sstevel@tonic-gate 	if (vp->v_type == VDIR) {
38607c478bd9Sstevel@tonic-gate 		/*
38617c478bd9Sstevel@tonic-gate 		 * Can't remove a directory that has a mounted-on filesystem.
38627c478bd9Sstevel@tonic-gate 		 */
38637c478bd9Sstevel@tonic-gate 		if (vn_ismntpt(vp)) {
38647c478bd9Sstevel@tonic-gate 			error = EACCES;
38657c478bd9Sstevel@tonic-gate 		} else {
38667c478bd9Sstevel@tonic-gate 			/*
38677c478bd9Sstevel@tonic-gate 			 * System V defines rmdir to return EEXIST,
38687c478bd9Sstevel@tonic-gate 			 * not * ENOTEMPTY, if the directory is not
38697c478bd9Sstevel@tonic-gate 			 * empty.  A System V NFS server needs to map
38707c478bd9Sstevel@tonic-gate 			 * NFS4ERR_EXIST to NFS4ERR_NOTEMPTY to
38717c478bd9Sstevel@tonic-gate 			 * transmit over the wire.
38727c478bd9Sstevel@tonic-gate 			 */
38737c478bd9Sstevel@tonic-gate 			if ((error = VOP_RMDIR(dvp, nm, rootdir, cs->cr))
38747c478bd9Sstevel@tonic-gate 				== EEXIST)
38757c478bd9Sstevel@tonic-gate 				error = ENOTEMPTY;
38767c478bd9Sstevel@tonic-gate 		}
38777c478bd9Sstevel@tonic-gate 	} else {
38787c478bd9Sstevel@tonic-gate 		if ((error = VOP_REMOVE(dvp, nm, cs->cr)) == 0 &&
38797c478bd9Sstevel@tonic-gate 			fp != NULL) {
38807c478bd9Sstevel@tonic-gate 			struct vattr va;
3881418d27f3Sshepler 			vnode_t *tvp;
38827c478bd9Sstevel@tonic-gate 
3883418d27f3Sshepler 			rfs4_dbe_lock(fp->dbe);
3884418d27f3Sshepler 			tvp = fp->vp;
3885418d27f3Sshepler 			if (tvp)
3886418d27f3Sshepler 				VN_HOLD(tvp);
3887418d27f3Sshepler 			rfs4_dbe_unlock(fp->dbe);
3888418d27f3Sshepler 
3889418d27f3Sshepler 			if (tvp) {
3890418d27f3Sshepler 				/*
3891418d27f3Sshepler 				 * This is va_seq safe because we are not
3892418d27f3Sshepler 				 * manipulating dvp.
3893418d27f3Sshepler 				 */
3894418d27f3Sshepler 				va.va_mask = AT_NLINK;
3895418d27f3Sshepler 				if (!VOP_GETATTR(tvp, &va, 0, cs->cr) &&
3896418d27f3Sshepler 					va.va_nlink == 0) {
3897418d27f3Sshepler 					/* Remove state on file remove */
3898418d27f3Sshepler 					if (in_crit) {
3899418d27f3Sshepler 						nbl_end_crit(vp);
3900418d27f3Sshepler 						in_crit = 0;
3901418d27f3Sshepler 					}
3902418d27f3Sshepler 					rfs4_close_all_state(fp);
39037c478bd9Sstevel@tonic-gate 				}
3904418d27f3Sshepler 				VN_RELE(tvp);
39057c478bd9Sstevel@tonic-gate 			}
39067c478bd9Sstevel@tonic-gate 		}
39077c478bd9Sstevel@tonic-gate 	}
39087c478bd9Sstevel@tonic-gate 
39097c478bd9Sstevel@tonic-gate 	if (in_crit)
39107c478bd9Sstevel@tonic-gate 		nbl_end_crit(vp);
39117c478bd9Sstevel@tonic-gate 	VN_RELE(vp);
39127c478bd9Sstevel@tonic-gate 
39137c478bd9Sstevel@tonic-gate 	if (fp) {
39147c478bd9Sstevel@tonic-gate 		rfs4_clear_dont_grant(fp);
39157c478bd9Sstevel@tonic-gate 		rfs4_file_rele(fp);
39167c478bd9Sstevel@tonic-gate 	}
39177c478bd9Sstevel@tonic-gate 	kmem_free(nm, len);
39187c478bd9Sstevel@tonic-gate 
39197c478bd9Sstevel@tonic-gate 	if (error) {
39207c478bd9Sstevel@tonic-gate 		*cs->statusp = resp->status = puterrno4(error);
39217c478bd9Sstevel@tonic-gate 		return;
39227c478bd9Sstevel@tonic-gate 	}
39237c478bd9Sstevel@tonic-gate 
39247c478bd9Sstevel@tonic-gate 	/*
39257c478bd9Sstevel@tonic-gate 	 * Get the initial "after" sequence number, if it fails, set to zero
39267c478bd9Sstevel@tonic-gate 	 */
39277c478bd9Sstevel@tonic-gate 	idva.va_mask = AT_SEQ;
39287c478bd9Sstevel@tonic-gate 	if (VOP_GETATTR(dvp, &idva, 0, cs->cr))
39297c478bd9Sstevel@tonic-gate 		idva.va_seq = 0;
39307c478bd9Sstevel@tonic-gate 
39317c478bd9Sstevel@tonic-gate 	/*
39327c478bd9Sstevel@tonic-gate 	 * Force modified data and metadata out to stable storage.
39337c478bd9Sstevel@tonic-gate 	 */
39347c478bd9Sstevel@tonic-gate 	(void) VOP_FSYNC(dvp, 0, cs->cr);
39357c478bd9Sstevel@tonic-gate 
39367c478bd9Sstevel@tonic-gate 	/*
39377c478bd9Sstevel@tonic-gate 	 * Get "after" change value, if it fails, simply return the
39387c478bd9Sstevel@tonic-gate 	 * before value.
39397c478bd9Sstevel@tonic-gate 	 */
39407c478bd9Sstevel@tonic-gate 	adva.va_mask = AT_CTIME|AT_SEQ;
39417c478bd9Sstevel@tonic-gate 	if (VOP_GETATTR(dvp, &adva, 0, cs->cr)) {
39427c478bd9Sstevel@tonic-gate 		adva.va_ctime = bdva.va_ctime;
39437c478bd9Sstevel@tonic-gate 		adva.va_seq = 0;
39447c478bd9Sstevel@tonic-gate 	}
39457c478bd9Sstevel@tonic-gate 
39467c478bd9Sstevel@tonic-gate 	NFS4_SET_FATTR4_CHANGE(resp->cinfo.after, adva.va_ctime)
39477c478bd9Sstevel@tonic-gate 
39487c478bd9Sstevel@tonic-gate 	/*
39497c478bd9Sstevel@tonic-gate 	 * The cinfo.atomic = TRUE only if we have
39507c478bd9Sstevel@tonic-gate 	 * non-zero va_seq's, and it has incremented by exactly one
39517c478bd9Sstevel@tonic-gate 	 * during the VOP_REMOVE/RMDIR and it didn't change during
39527c478bd9Sstevel@tonic-gate 	 * the VOP_FSYNC.
39537c478bd9Sstevel@tonic-gate 	 */
39547c478bd9Sstevel@tonic-gate 	if (bdva.va_seq && idva.va_seq && adva.va_seq &&
39557c478bd9Sstevel@tonic-gate 			idva.va_seq == (bdva.va_seq + 1) &&
39567c478bd9Sstevel@tonic-gate 			idva.va_seq == adva.va_seq)
39577c478bd9Sstevel@tonic-gate 		resp->cinfo.atomic = TRUE;
39587c478bd9Sstevel@tonic-gate 	else
39597c478bd9Sstevel@tonic-gate 		resp->cinfo.atomic = FALSE;
39607c478bd9Sstevel@tonic-gate 
39617c478bd9Sstevel@tonic-gate 	*cs->statusp = resp->status = NFS4_OK;
39627c478bd9Sstevel@tonic-gate }
39637c478bd9Sstevel@tonic-gate 
39647c478bd9Sstevel@tonic-gate /*
39657c478bd9Sstevel@tonic-gate  * rename: args: SAVED_FH: from directory, CURRENT_FH: target directory,
39667c478bd9Sstevel@tonic-gate  *		oldname and newname.
39677c478bd9Sstevel@tonic-gate  *	res: status. If success - CURRENT_FH unchanged, return change_info
39687c478bd9Sstevel@tonic-gate  *		for both from and target directories.
39697c478bd9Sstevel@tonic-gate  */
39707c478bd9Sstevel@tonic-gate /* ARGSUSED */
39717c478bd9Sstevel@tonic-gate static void
39727c478bd9Sstevel@tonic-gate rfs4_op_rename(nfs_argop4 *argop, nfs_resop4 *resop, struct svc_req *req,
39737c478bd9Sstevel@tonic-gate 	struct compound_state *cs)
39747c478bd9Sstevel@tonic-gate {
39757c478bd9Sstevel@tonic-gate 	RENAME4args *args = &argop->nfs_argop4_u.oprename;
39767c478bd9Sstevel@tonic-gate 	RENAME4res *resp = &resop->nfs_resop4_u.oprename;
39777c478bd9Sstevel@tonic-gate 	int error;
39787c478bd9Sstevel@tonic-gate 	vnode_t *odvp;
39797c478bd9Sstevel@tonic-gate 	vnode_t *ndvp;
39807c478bd9Sstevel@tonic-gate 	vnode_t *srcvp, *targvp;
39817c478bd9Sstevel@tonic-gate 	struct vattr obdva, oidva, oadva;
39827c478bd9Sstevel@tonic-gate 	struct vattr nbdva, nidva, nadva;
39837c478bd9Sstevel@tonic-gate 	char *onm, *nnm;
39847c478bd9Sstevel@tonic-gate 	uint_t olen, nlen;
39857c478bd9Sstevel@tonic-gate 	rfs4_file_t *fp, *sfp;
39867c478bd9Sstevel@tonic-gate 	int in_crit_src, in_crit_targ;
39877c478bd9Sstevel@tonic-gate 	int fp_rele_grant_hold, sfp_rele_grant_hold;
398845916cd2Sjpk 	bslabel_t *clabel;
39897c478bd9Sstevel@tonic-gate 
39907c478bd9Sstevel@tonic-gate 	fp = sfp = NULL;
39917c478bd9Sstevel@tonic-gate 	srcvp = targvp = NULL;
39927c478bd9Sstevel@tonic-gate 	in_crit_src = in_crit_targ = 0;
39937c478bd9Sstevel@tonic-gate 	fp_rele_grant_hold = sfp_rele_grant_hold = 0;
39947c478bd9Sstevel@tonic-gate 
39957c478bd9Sstevel@tonic-gate 	/* CURRENT_FH: target directory */
39967c478bd9Sstevel@tonic-gate 	ndvp = cs->vp;
39977c478bd9Sstevel@tonic-gate 	if (ndvp == NULL) {
39987c478bd9Sstevel@tonic-gate 		*cs->statusp = resp->status = NFS4ERR_NOFILEHANDLE;
39997c478bd9Sstevel@tonic-gate 		return;
40007c478bd9Sstevel@tonic-gate 	}
40017c478bd9Sstevel@tonic-gate 
40027c478bd9Sstevel@tonic-gate 	/* SAVED_FH: from directory */
40037c478bd9Sstevel@tonic-gate 	odvp = cs->saved_vp;
40047c478bd9Sstevel@tonic-gate 	if (odvp == NULL) {
40057c478bd9Sstevel@tonic-gate 		*cs->statusp = resp->status = NFS4ERR_NOFILEHANDLE;
40067c478bd9Sstevel@tonic-gate 		return;
40077c478bd9Sstevel@tonic-gate 	}
40087c478bd9Sstevel@tonic-gate 
40097c478bd9Sstevel@tonic-gate 	if (cs->access == CS_ACCESS_DENIED) {
40107c478bd9Sstevel@tonic-gate 		*cs->statusp = resp->status = NFS4ERR_ACCESS;
40117c478bd9Sstevel@tonic-gate 		return;
40127c478bd9Sstevel@tonic-gate 	}
40137c478bd9Sstevel@tonic-gate 
40147c478bd9Sstevel@tonic-gate 	/*
40157c478bd9Sstevel@tonic-gate 	 * If there is an unshared filesystem mounted on this vnode,
40167c478bd9Sstevel@tonic-gate 	 * do not allow to rename objects in this directory.
40177c478bd9Sstevel@tonic-gate 	 */
40187c478bd9Sstevel@tonic-gate 	if (vn_ismntpt(odvp)) {
40197c478bd9Sstevel@tonic-gate 		*cs->statusp = resp->status = NFS4ERR_ACCESS;
40207c478bd9Sstevel@tonic-gate 		return;
40217c478bd9Sstevel@tonic-gate 	}
40227c478bd9Sstevel@tonic-gate 
40237c478bd9Sstevel@tonic-gate 	/*
40247c478bd9Sstevel@tonic-gate 	 * If there is an unshared filesystem mounted on this vnode,
40257c478bd9Sstevel@tonic-gate 	 * do not allow to rename to this directory.
40267c478bd9Sstevel@tonic-gate 	 */
40277c478bd9Sstevel@tonic-gate 	if (vn_ismntpt(ndvp)) {
40287c478bd9Sstevel@tonic-gate 		*cs->statusp = resp->status = NFS4ERR_ACCESS;
40297c478bd9Sstevel@tonic-gate 		return;
40307c478bd9Sstevel@tonic-gate 	}
40317c478bd9Sstevel@tonic-gate 
40327c478bd9Sstevel@tonic-gate 	if (odvp->v_type != VDIR || ndvp->v_type != VDIR) {
40337c478bd9Sstevel@tonic-gate 		*cs->statusp = resp->status = NFS4ERR_NOTDIR;
40347c478bd9Sstevel@tonic-gate 		return;
40357c478bd9Sstevel@tonic-gate 	}
40367c478bd9Sstevel@tonic-gate 
40377c478bd9Sstevel@tonic-gate 	if (cs->saved_exi != cs->exi) {
40387c478bd9Sstevel@tonic-gate 		*cs->statusp = resp->status = NFS4ERR_XDEV;
40397c478bd9Sstevel@tonic-gate 		return;
40407c478bd9Sstevel@tonic-gate 	}
40417c478bd9Sstevel@tonic-gate 
40427c478bd9Sstevel@tonic-gate 	if (!utf8_dir_verify(&args->oldname)) {
40437c478bd9Sstevel@tonic-gate 		*cs->statusp = resp->status = NFS4ERR_INVAL;
40447c478bd9Sstevel@tonic-gate 		return;
40457c478bd9Sstevel@tonic-gate 	}
40467c478bd9Sstevel@tonic-gate 
40477c478bd9Sstevel@tonic-gate 	if (!utf8_dir_verify(&args->newname)) {
40487c478bd9Sstevel@tonic-gate 		*cs->statusp = resp->status = NFS4ERR_INVAL;
40497c478bd9Sstevel@tonic-gate 		return;
40507c478bd9Sstevel@tonic-gate 	}
40517c478bd9Sstevel@tonic-gate 
40527c478bd9Sstevel@tonic-gate 	onm = utf8_to_fn(&args->oldname, &olen, NULL);
40537c478bd9Sstevel@tonic-gate 	if (onm == NULL) {
40547c478bd9Sstevel@tonic-gate 		*cs->statusp = resp->status = NFS4ERR_INVAL;
40557c478bd9Sstevel@tonic-gate 		return;
40567c478bd9Sstevel@tonic-gate 	}
40577c478bd9Sstevel@tonic-gate 
40587c478bd9Sstevel@tonic-gate 	nnm = utf8_to_fn(&args->newname, &nlen, NULL);
40597c478bd9Sstevel@tonic-gate 	if (nnm == NULL) {
40607c478bd9Sstevel@tonic-gate 		*cs->statusp = resp->status = NFS4ERR_INVAL;
40617c478bd9Sstevel@tonic-gate 		kmem_free(onm, olen);
40627c478bd9Sstevel@tonic-gate 		return;
40637c478bd9Sstevel@tonic-gate 	}
40647c478bd9Sstevel@tonic-gate 
40657c478bd9Sstevel@tonic-gate 	if (olen > MAXNAMELEN || nlen > MAXNAMELEN) {
40667c478bd9Sstevel@tonic-gate 		*cs->statusp = resp->status = NFS4ERR_NAMETOOLONG;
40677c478bd9Sstevel@tonic-gate 		kmem_free(onm, olen);
40687c478bd9Sstevel@tonic-gate 		kmem_free(nnm, nlen);
40697c478bd9Sstevel@tonic-gate 		return;
40707c478bd9Sstevel@tonic-gate 	}
40717c478bd9Sstevel@tonic-gate 
40727c478bd9Sstevel@tonic-gate 
40737c478bd9Sstevel@tonic-gate 	if (rdonly4(cs->exi, cs->vp, req)) {
40747c478bd9Sstevel@tonic-gate 		*cs->statusp = resp->status = NFS4ERR_ROFS;
40757c478bd9Sstevel@tonic-gate 		kmem_free(onm, olen);
40767c478bd9Sstevel@tonic-gate 		kmem_free(nnm, nlen);
40777c478bd9Sstevel@tonic-gate 		return;
40787c478bd9Sstevel@tonic-gate 	}
40797c478bd9Sstevel@tonic-gate 
408045916cd2Sjpk 	/* check label of the target dir */
408145916cd2Sjpk 	if (is_system_labeled()) {
408245916cd2Sjpk 		ASSERT(req->rq_label != NULL);
408345916cd2Sjpk 		clabel = req->rq_label;
408445916cd2Sjpk 		DTRACE_PROBE2(tx__rfs4__log__info__oprename__clabel, char *,
408545916cd2Sjpk 		    "got client label from request(1)",
408645916cd2Sjpk 		    struct svc_req *, req);
408745916cd2Sjpk 		if (!blequal(&l_admin_low->tsl_label, clabel)) {
408845916cd2Sjpk 			if (!do_rfs4_label_check(clabel, ndvp,
408945916cd2Sjpk 			    EQUALITY_CHECK)) {
409045916cd2Sjpk 				*cs->statusp = resp->status = NFS4ERR_ACCESS;
409145916cd2Sjpk 				return;
409245916cd2Sjpk 			}
409345916cd2Sjpk 		}
409445916cd2Sjpk 	}
409545916cd2Sjpk 
40967c478bd9Sstevel@tonic-gate 	/*
40977c478bd9Sstevel@tonic-gate 	 * Is the source a file and have a delegation?
40987c478bd9Sstevel@tonic-gate 	 * We don't need to acquire va_seq before these lookups, if
40997c478bd9Sstevel@tonic-gate 	 * it causes an update, cinfo.before will not match, which will
41007c478bd9Sstevel@tonic-gate 	 * trigger a cache flush even if atomic is TRUE.
41017c478bd9Sstevel@tonic-gate 	 */
41027c478bd9Sstevel@tonic-gate 	if (sfp = rfs4_lookup_and_findfile(odvp, onm, &srcvp, &error, cs->cr)) {
41037c478bd9Sstevel@tonic-gate 		if (rfs4_check_delegated_byfp(FWRITE, sfp, TRUE, TRUE, TRUE,
41047c478bd9Sstevel@tonic-gate 						NULL)) {
41057c478bd9Sstevel@tonic-gate 			*cs->statusp = resp->status = NFS4ERR_DELAY;
41067c478bd9Sstevel@tonic-gate 			goto err_out;
41077c478bd9Sstevel@tonic-gate 		}
41087c478bd9Sstevel@tonic-gate 	}
41097c478bd9Sstevel@tonic-gate 
41107c478bd9Sstevel@tonic-gate 	if (srcvp == NULL) {
41117c478bd9Sstevel@tonic-gate 		*cs->statusp = resp->status = puterrno4(error);
41127c478bd9Sstevel@tonic-gate 		kmem_free(onm, olen);
41137c478bd9Sstevel@tonic-gate 		kmem_free(nnm, nlen);
41147c478bd9Sstevel@tonic-gate 		return;
41157c478bd9Sstevel@tonic-gate 	}
41167c478bd9Sstevel@tonic-gate 
41177c478bd9Sstevel@tonic-gate 	sfp_rele_grant_hold = 1;
41187c478bd9Sstevel@tonic-gate 
41197c478bd9Sstevel@tonic-gate 	/* Does the destination exist and a file and have a delegation? */
41207c478bd9Sstevel@tonic-gate 	if (fp = rfs4_lookup_and_findfile(ndvp, nnm, &targvp, NULL, cs->cr)) {
41217c478bd9Sstevel@tonic-gate 		if (rfs4_check_delegated_byfp(FWRITE, fp, TRUE, TRUE, TRUE,
41227c478bd9Sstevel@tonic-gate 						NULL)) {
41237c478bd9Sstevel@tonic-gate 			*cs->statusp = resp->status = NFS4ERR_DELAY;
41247c478bd9Sstevel@tonic-gate 			goto err_out;
41257c478bd9Sstevel@tonic-gate 		}
41267c478bd9Sstevel@tonic-gate 	}
41277c478bd9Sstevel@tonic-gate 	fp_rele_grant_hold = 1;
41287c478bd9Sstevel@tonic-gate 
41297c478bd9Sstevel@tonic-gate 
41307c478bd9Sstevel@tonic-gate 	/* Check for NBMAND lock on both source and target */
41317c478bd9Sstevel@tonic-gate 	if (nbl_need_check(srcvp)) {
41327c478bd9Sstevel@tonic-gate 		nbl_start_crit(srcvp, RW_READER);
41337c478bd9Sstevel@tonic-gate 		in_crit_src = 1;
41347c478bd9Sstevel@tonic-gate 		if (nbl_conflict(srcvp, NBL_RENAME, 0, 0, 0)) {
41357c478bd9Sstevel@tonic-gate 			*cs->statusp = resp->status = NFS4ERR_FILE_OPEN;
41367c478bd9Sstevel@tonic-gate 			goto err_out;
41377c478bd9Sstevel@tonic-gate 		}
41387c478bd9Sstevel@tonic-gate 	}
41397c478bd9Sstevel@tonic-gate 
41407c478bd9Sstevel@tonic-gate 	if (targvp && nbl_need_check(targvp)) {
41417c478bd9Sstevel@tonic-gate 		nbl_start_crit(targvp, RW_READER);
41427c478bd9Sstevel@tonic-gate 		in_crit_targ = 1;
41437c478bd9Sstevel@tonic-gate 		if (nbl_conflict(targvp, NBL_REMOVE, 0, 0, 0)) {
41447c478bd9Sstevel@tonic-gate 			*cs->statusp = resp->status = NFS4ERR_FILE_OPEN;
41457c478bd9Sstevel@tonic-gate 			goto err_out;
41467c478bd9Sstevel@tonic-gate 		}
41477c478bd9Sstevel@tonic-gate 	}
41487c478bd9Sstevel@tonic-gate 
41497c478bd9Sstevel@tonic-gate 	/* Get source "before" change value */
41507c478bd9Sstevel@tonic-gate 	obdva.va_mask = AT_CTIME|AT_SEQ;
41517c478bd9Sstevel@tonic-gate 	error = VOP_GETATTR(odvp, &obdva, 0, cs->cr);
41527c478bd9Sstevel@tonic-gate 	if (!error) {
41537c478bd9Sstevel@tonic-gate 		nbdva.va_mask = AT_CTIME|AT_SEQ;
41547c478bd9Sstevel@tonic-gate 		error = VOP_GETATTR(ndvp, &nbdva, 0, cs->cr);
41557c478bd9Sstevel@tonic-gate 	}
41567c478bd9Sstevel@tonic-gate 	if (error) {
41577c478bd9Sstevel@tonic-gate 		*cs->statusp = resp->status = puterrno4(error);
41587c478bd9Sstevel@tonic-gate 		goto err_out;
41597c478bd9Sstevel@tonic-gate 	}
41607c478bd9Sstevel@tonic-gate 
41617c478bd9Sstevel@tonic-gate 	NFS4_SET_FATTR4_CHANGE(resp->source_cinfo.before, obdva.va_ctime)
41627c478bd9Sstevel@tonic-gate 	NFS4_SET_FATTR4_CHANGE(resp->target_cinfo.before, nbdva.va_ctime)
41637c478bd9Sstevel@tonic-gate 
41647c478bd9Sstevel@tonic-gate 	if ((error = VOP_RENAME(odvp, onm, ndvp, nnm, cs->cr)) == 0 &&
41657c478bd9Sstevel@tonic-gate 		fp != NULL) {
41667c478bd9Sstevel@tonic-gate 		struct vattr va;
4167418d27f3Sshepler 		vnode_t *tvp;
4168418d27f3Sshepler 
4169418d27f3Sshepler 		rfs4_dbe_lock(fp->dbe);
4170418d27f3Sshepler 		tvp = fp->vp;
4171418d27f3Sshepler 		if (tvp)
4172418d27f3Sshepler 			VN_HOLD(tvp);
4173418d27f3Sshepler 		rfs4_dbe_unlock(fp->dbe);
41747c478bd9Sstevel@tonic-gate 
4175418d27f3Sshepler 		if (tvp) {
4176418d27f3Sshepler 			va.va_mask = AT_NLINK;
4177418d27f3Sshepler 			if (!VOP_GETATTR(tvp, &va, 0, cs->cr) &&
4178418d27f3Sshepler 				va.va_nlink == 0) {
4179418d27f3Sshepler 				/* The file is gone and so should the state */
4180418d27f3Sshepler 				if (in_crit_targ) {
4181418d27f3Sshepler 					nbl_end_crit(targvp);
4182418d27f3Sshepler 					in_crit_targ = 0;
4183418d27f3Sshepler 				}
4184418d27f3Sshepler 				rfs4_close_all_state(fp);
41857c478bd9Sstevel@tonic-gate 			}
4186418d27f3Sshepler 			VN_RELE(tvp);
41877c478bd9Sstevel@tonic-gate 		}
41887c478bd9Sstevel@tonic-gate 	}
4189c9264041Sjwahlig 	if (error == 0) {
4190c9264041Sjwahlig 		char *tmp;
4191c9264041Sjwahlig 
4192c9264041Sjwahlig 		/* fix the path name for the renamed file */
4193c9264041Sjwahlig 		mutex_enter(&srcvp->v_lock);
4194c9264041Sjwahlig 		tmp = srcvp->v_path;
4195c9264041Sjwahlig 		srcvp->v_path = NULL;
4196c9264041Sjwahlig 		mutex_exit(&srcvp->v_lock);
4197c9264041Sjwahlig 		vn_setpath(rootdir, ndvp, srcvp, nnm, nlen - 1);
4198c9264041Sjwahlig 		if (tmp != NULL)
4199c9264041Sjwahlig 			kmem_free(tmp, strlen(tmp) + 1);
4200c9264041Sjwahlig 	}
42017c478bd9Sstevel@tonic-gate 
42027c478bd9Sstevel@tonic-gate 	if (in_crit_src)
42037c478bd9Sstevel@tonic-gate 		nbl_end_crit(srcvp);
42047c478bd9Sstevel@tonic-gate 	if (srcvp)
42057c478bd9Sstevel@tonic-gate 		VN_RELE(srcvp);
42067c478bd9Sstevel@tonic-gate 	if (in_crit_targ)
42077c478bd9Sstevel@tonic-gate 		nbl_end_crit(targvp);
42087c478bd9Sstevel@tonic-gate 	if (targvp)
42097c478bd9Sstevel@tonic-gate 		VN_RELE(targvp);
42107c478bd9Sstevel@tonic-gate 
42117c478bd9Sstevel@tonic-gate 	if (sfp) {
42127c478bd9Sstevel@tonic-gate 		rfs4_clear_dont_grant(sfp);
42137c478bd9Sstevel@tonic-gate 		rfs4_file_rele(sfp);
42147c478bd9Sstevel@tonic-gate 	}
42157c478bd9Sstevel@tonic-gate 	if (fp) {
42167c478bd9Sstevel@tonic-gate 		rfs4_clear_dont_grant(fp);
42177c478bd9Sstevel@tonic-gate 		rfs4_file_rele(fp);
42187c478bd9Sstevel@tonic-gate 	}
42197c478bd9Sstevel@tonic-gate 
42207c478bd9Sstevel@tonic-gate 	kmem_free(onm, olen);
42217c478bd9Sstevel@tonic-gate 	kmem_free(nnm, nlen);
42227c478bd9Sstevel@tonic-gate 
42237c478bd9Sstevel@tonic-gate 	/*
42247c478bd9Sstevel@tonic-gate 	 * Get the initial "after" sequence number, if it fails, set to zero
42257c478bd9Sstevel@tonic-gate 	 */
42267c478bd9Sstevel@tonic-gate 	oidva.va_mask = AT_SEQ;
42277c478bd9Sstevel@tonic-gate 	if (VOP_GETATTR(odvp, &oidva, 0, cs->cr))
42287c478bd9Sstevel@tonic-gate 		oidva.va_seq = 0;
42297c478bd9Sstevel@tonic-gate 
42307c478bd9Sstevel@tonic-gate 	nidva.va_mask = AT_SEQ;
42317c478bd9Sstevel@tonic-gate 	if (VOP_GETATTR(ndvp, &nidva, 0, cs->cr))
42327c478bd9Sstevel@tonic-gate 		nidva.va_seq = 0;
42337c478bd9Sstevel@tonic-gate 
42347c478bd9Sstevel@tonic-gate 	/*
42357c478bd9Sstevel@tonic-gate 	 * Force modified data and metadata out to stable storage.
42367c478bd9Sstevel@tonic-gate 	 */
42377c478bd9Sstevel@tonic-gate 	(void) VOP_FSYNC(odvp, 0, cs->cr);
42387c478bd9Sstevel@tonic-gate 	(void) VOP_FSYNC(ndvp, 0, cs->cr);
42397c478bd9Sstevel@tonic-gate 
42407c478bd9Sstevel@tonic-gate 	if (error) {
42417c478bd9Sstevel@tonic-gate 		*cs->statusp = resp->status = puterrno4(error);
42427c478bd9Sstevel@tonic-gate 		return;
42437c478bd9Sstevel@tonic-gate 	}
42447c478bd9Sstevel@tonic-gate 
42457c478bd9Sstevel@tonic-gate 	/*
42467c478bd9Sstevel@tonic-gate 	 * Get "after" change values, if it fails, simply return the
42477c478bd9Sstevel@tonic-gate 	 * before value.
42487c478bd9Sstevel@tonic-gate 	 */
42497c478bd9Sstevel@tonic-gate 	oadva.va_mask = AT_CTIME|AT_SEQ;
42507c478bd9Sstevel@tonic-gate 	if (VOP_GETATTR(odvp, &oadva, 0, cs->cr)) {
42517c478bd9Sstevel@tonic-gate 		oadva.va_ctime = obdva.va_ctime;
42527c478bd9Sstevel@tonic-gate 		oadva.va_seq = 0;
42537c478bd9Sstevel@tonic-gate 	}
42547c478bd9Sstevel@tonic-gate 
42557c478bd9Sstevel@tonic-gate 	nadva.va_mask = AT_CTIME|AT_SEQ;
42567c478bd9Sstevel@tonic-gate 	if (VOP_GETATTR(odvp, &nadva, 0, cs->cr)) {
42577c478bd9Sstevel@tonic-gate 		nadva.va_ctime = nbdva.va_ctime;
42587c478bd9Sstevel@tonic-gate 		nadva.va_seq = 0;
42597c478bd9Sstevel@tonic-gate 	}
42607c478bd9Sstevel@tonic-gate 
42617c478bd9Sstevel@tonic-gate 	NFS4_SET_FATTR4_CHANGE(resp->source_cinfo.after, oadva.va_ctime)
42627c478bd9Sstevel@tonic-gate 	NFS4_SET_FATTR4_CHANGE(resp->target_cinfo.after, nadva.va_ctime)
42637c478bd9Sstevel@tonic-gate 
42647c478bd9Sstevel@tonic-gate 	/*
42657c478bd9Sstevel@tonic-gate 	 * The cinfo.atomic = TRUE only if we have
42667c478bd9Sstevel@tonic-gate 	 * non-zero va_seq's, and it has incremented by exactly one
42677c478bd9Sstevel@tonic-gate 	 * during the VOP_RENAME and it didn't change during the VOP_FSYNC.
42687c478bd9Sstevel@tonic-gate 	 */
42697c478bd9Sstevel@tonic-gate 	if (obdva.va_seq && oidva.va_seq && oadva.va_seq &&
42707c478bd9Sstevel@tonic-gate 			oidva.va_seq == (obdva.va_seq + 1) &&
42717c478bd9Sstevel@tonic-gate 			oidva.va_seq == oadva.va_seq)
42727c478bd9Sstevel@tonic-gate 		resp->source_cinfo.atomic = TRUE;
42737c478bd9Sstevel@tonic-gate 	else
42747c478bd9Sstevel@tonic-gate 		resp->source_cinfo.atomic = FALSE;
42757c478bd9Sstevel@tonic-gate 
42767c478bd9Sstevel@tonic-gate 	if (nbdva.va_seq && nidva.va_seq && nadva.va_seq &&
42777c478bd9Sstevel@tonic-gate 			nidva.va_seq == (nbdva.va_seq + 1) &&
42787c478bd9Sstevel@tonic-gate 			nidva.va_seq == nadva.va_seq)
42797c478bd9Sstevel@tonic-gate 		resp->target_cinfo.atomic = TRUE;
42807c478bd9Sstevel@tonic-gate 	else
42817c478bd9Sstevel@tonic-gate 		resp->target_cinfo.atomic = FALSE;
42827c478bd9Sstevel@tonic-gate 
42837c478bd9Sstevel@tonic-gate #ifdef	VOLATILE_FH_TEST
42847c478bd9Sstevel@tonic-gate 	{
42857c478bd9Sstevel@tonic-gate 	extern void add_volrnm_fh(struct exportinfo *, vnode_t *);
42867c478bd9Sstevel@tonic-gate 
42877c478bd9Sstevel@tonic-gate 	/*
42887c478bd9Sstevel@tonic-gate 	 * Add the renamed file handle to the volatile rename list
42897c478bd9Sstevel@tonic-gate 	 */
42907c478bd9Sstevel@tonic-gate 	if (cs->exi->exi_export.ex_flags & EX_VOLRNM) {
42917c478bd9Sstevel@tonic-gate 		/* file handles may expire on rename */
42927c478bd9Sstevel@tonic-gate 		vnode_t *vp;
42937c478bd9Sstevel@tonic-gate 
42947c478bd9Sstevel@tonic-gate 		nnm = utf8_to_fn(&args->newname, &nlen, NULL);
42957c478bd9Sstevel@tonic-gate 		/*
42967c478bd9Sstevel@tonic-gate 		 * Already know that nnm will be a valid string
42977c478bd9Sstevel@tonic-gate 		 */
42987c478bd9Sstevel@tonic-gate 		error = VOP_LOOKUP(ndvp, nnm, &vp, NULL, 0, NULL, cs->cr);
42997c478bd9Sstevel@tonic-gate 		kmem_free(nnm, nlen);
43007c478bd9Sstevel@tonic-gate 		if (!error) {
43017c478bd9Sstevel@tonic-gate 			add_volrnm_fh(cs->exi, vp);
43027c478bd9Sstevel@tonic-gate 			VN_RELE(vp);
43037c478bd9Sstevel@tonic-gate 		}
43047c478bd9Sstevel@tonic-gate 	}
43057c478bd9Sstevel@tonic-gate 	}
43067c478bd9Sstevel@tonic-gate #endif	/* VOLATILE_FH_TEST */
43077c478bd9Sstevel@tonic-gate 
43087c478bd9Sstevel@tonic-gate 	*cs->statusp = resp->status = NFS4_OK;
43097c478bd9Sstevel@tonic-gate 	return;
43107c478bd9Sstevel@tonic-gate 
43117c478bd9Sstevel@tonic-gate err_out:
43127c478bd9Sstevel@tonic-gate 	kmem_free(onm, olen);
43137c478bd9Sstevel@tonic-gate 	kmem_free(nnm, nlen);
43147c478bd9Sstevel@tonic-gate 
43157c478bd9Sstevel@tonic-gate 	if (in_crit_src) nbl_end_crit(srcvp);
43167c478bd9Sstevel@tonic-gate 	if (in_crit_targ) nbl_end_crit(targvp);
43177c478bd9Sstevel@tonic-gate 	if (targvp) VN_RELE(targvp);
43187c478bd9Sstevel@tonic-gate 	if (srcvp) VN_RELE(srcvp);
43197c478bd9Sstevel@tonic-gate 	if (sfp) {
43207c478bd9Sstevel@tonic-gate 		if (sfp_rele_grant_hold) rfs4_clear_dont_grant(sfp);
43217c478bd9Sstevel@tonic-gate 		rfs4_file_rele(sfp);
43227c478bd9Sstevel@tonic-gate 	}
43237c478bd9Sstevel@tonic-gate 	if (fp) {
43247c478bd9Sstevel@tonic-gate 		if (fp_rele_grant_hold) rfs4_clear_dont_grant(fp);
43257c478bd9Sstevel@tonic-gate 		rfs4_file_rele(fp);
43267c478bd9Sstevel@tonic-gate 	}
43277c478bd9Sstevel@tonic-gate }
43287c478bd9Sstevel@tonic-gate 
43297c478bd9Sstevel@tonic-gate /* ARGSUSED */
43307c478bd9Sstevel@tonic-gate static void
43317c478bd9Sstevel@tonic-gate rfs4_op_renew(nfs_argop4 *argop, nfs_resop4 *resop, struct svc_req *req,
43327c478bd9Sstevel@tonic-gate 	struct compound_state *cs)
43337c478bd9Sstevel@tonic-gate {
43347c478bd9Sstevel@tonic-gate 	RENEW4args *args = &argop->nfs_argop4_u.oprenew;
43357c478bd9Sstevel@tonic-gate 	RENEW4res *resp = &resop->nfs_resop4_u.oprenew;
43367c478bd9Sstevel@tonic-gate 	rfs4_client_t *cp;
43377c478bd9Sstevel@tonic-gate 
43387c478bd9Sstevel@tonic-gate 	if ((cp = rfs4_findclient_by_id(args->clientid, FALSE)) == NULL) {
43397c478bd9Sstevel@tonic-gate 		*cs->statusp = resp->status =
43407c478bd9Sstevel@tonic-gate 			rfs4_check_clientid(&args->clientid, 0);
43417c478bd9Sstevel@tonic-gate 		return;
43427c478bd9Sstevel@tonic-gate 	}
43437c478bd9Sstevel@tonic-gate 
43447c478bd9Sstevel@tonic-gate 	if (rfs4_lease_expired(cp)) {
43457c478bd9Sstevel@tonic-gate 		rfs4_client_rele(cp);
43467c478bd9Sstevel@tonic-gate 		*cs->statusp = resp->status = NFS4ERR_EXPIRED;
43477c478bd9Sstevel@tonic-gate 		return;
43487c478bd9Sstevel@tonic-gate 	}
43497c478bd9Sstevel@tonic-gate 
43507c478bd9Sstevel@tonic-gate 	rfs4_update_lease(cp);
43517c478bd9Sstevel@tonic-gate 
43527c478bd9Sstevel@tonic-gate 	mutex_enter(cp->cbinfo.cb_lock);
43537c478bd9Sstevel@tonic-gate 	if (cp->cbinfo.cb_notified_of_cb_path_down == FALSE) {
43547c478bd9Sstevel@tonic-gate 		cp->cbinfo.cb_notified_of_cb_path_down = TRUE;
43557c478bd9Sstevel@tonic-gate 		*cs->statusp = resp->status = NFS4ERR_CB_PATH_DOWN;
43567c478bd9Sstevel@tonic-gate 	} else {
43577c478bd9Sstevel@tonic-gate 		*cs->statusp = resp->status = NFS4_OK;
43587c478bd9Sstevel@tonic-gate 	}
43597c478bd9Sstevel@tonic-gate 	mutex_exit(cp->cbinfo.cb_lock);
43607c478bd9Sstevel@tonic-gate 
43617c478bd9Sstevel@tonic-gate 	rfs4_client_rele(cp);
43627c478bd9Sstevel@tonic-gate 
43637c478bd9Sstevel@tonic-gate }
43647c478bd9Sstevel@tonic-gate 
43657c478bd9Sstevel@tonic-gate /* ARGSUSED */
43667c478bd9Sstevel@tonic-gate static void
43677c478bd9Sstevel@tonic-gate rfs4_op_restorefh(nfs_argop4 *args, nfs_resop4 *resop, struct svc_req *req,
43687c478bd9Sstevel@tonic-gate 	struct compound_state *cs)
43697c478bd9Sstevel@tonic-gate {
43707c478bd9Sstevel@tonic-gate 	RESTOREFH4res *resp = &resop->nfs_resop4_u.oprestorefh;
43717c478bd9Sstevel@tonic-gate 
43727c478bd9Sstevel@tonic-gate 	/* No need to check cs->access - we are not accessing any object */
43737c478bd9Sstevel@tonic-gate 	if ((cs->saved_vp == NULL) || (cs->saved_fh.nfs_fh4_val == NULL)) {
43747c478bd9Sstevel@tonic-gate 		*cs->statusp = resp->status = NFS4ERR_RESTOREFH;
43757c478bd9Sstevel@tonic-gate 		return;
43767c478bd9Sstevel@tonic-gate 	}
43777c478bd9Sstevel@tonic-gate 	if (cs->vp != NULL) {
43787c478bd9Sstevel@tonic-gate 		VN_RELE(cs->vp);
43797c478bd9Sstevel@tonic-gate 	}
43807c478bd9Sstevel@tonic-gate 	cs->vp = cs->saved_vp;
43817c478bd9Sstevel@tonic-gate 	cs->saved_vp = NULL;
43827c478bd9Sstevel@tonic-gate 	cs->exi = cs->saved_exi;
43837c478bd9Sstevel@tonic-gate 	nfs_fh4_copy(&cs->saved_fh, &cs->fh);
43847c478bd9Sstevel@tonic-gate 	*cs->statusp = resp->status = NFS4_OK;
43857c478bd9Sstevel@tonic-gate 	cs->deleg = FALSE;
43867c478bd9Sstevel@tonic-gate }
43877c478bd9Sstevel@tonic-gate 
43887c478bd9Sstevel@tonic-gate /* ARGSUSED */
43897c478bd9Sstevel@tonic-gate static void
43907c478bd9Sstevel@tonic-gate rfs4_op_savefh(nfs_argop4 *argop, nfs_resop4 *resop, struct svc_req *req,
43917c478bd9Sstevel@tonic-gate 	struct compound_state *cs)
43927c478bd9Sstevel@tonic-gate {
43937c478bd9Sstevel@tonic-gate 	SAVEFH4res *resp = &resop->nfs_resop4_u.opsavefh;
43947c478bd9Sstevel@tonic-gate 
43957c478bd9Sstevel@tonic-gate 	/* No need to check cs->access - we are not accessing any object */
43967c478bd9Sstevel@tonic-gate 	if (cs->vp == NULL) {
43977c478bd9Sstevel@tonic-gate 		*cs->statusp = resp->status = NFS4ERR_NOFILEHANDLE;
43987c478bd9Sstevel@tonic-gate 		return;
43997c478bd9Sstevel@tonic-gate 	}
44007c478bd9Sstevel@tonic-gate 	if (cs->saved_vp != NULL) {
44017c478bd9Sstevel@tonic-gate 		VN_RELE(cs->saved_vp);
44027c478bd9Sstevel@tonic-gate 	}
44037c478bd9Sstevel@tonic-gate 	cs->saved_vp = cs->vp;
44047c478bd9Sstevel@tonic-gate 	VN_HOLD(cs->saved_vp);
44057c478bd9Sstevel@tonic-gate 	cs->saved_exi = cs->exi;
44067c478bd9Sstevel@tonic-gate 	/*
44077c478bd9Sstevel@tonic-gate 	 * since SAVEFH is fairly rare, don't alloc space for its fh
44087c478bd9Sstevel@tonic-gate 	 * unless necessary.
44097c478bd9Sstevel@tonic-gate 	 */
44107c478bd9Sstevel@tonic-gate 	if (cs->saved_fh.nfs_fh4_val == NULL) {
44117c478bd9Sstevel@tonic-gate 		cs->saved_fh.nfs_fh4_val = kmem_alloc(NFS4_FHSIZE, KM_SLEEP);
44127c478bd9Sstevel@tonic-gate 	}
44137c478bd9Sstevel@tonic-gate 	nfs_fh4_copy(&cs->fh, &cs->saved_fh);
44147c478bd9Sstevel@tonic-gate 	*cs->statusp = resp->status = NFS4_OK;
44157c478bd9Sstevel@tonic-gate }
44167c478bd9Sstevel@tonic-gate 
44177c478bd9Sstevel@tonic-gate /*
44187c478bd9Sstevel@tonic-gate  * rfs4_verify_attr is called when nfsv4 Setattr failed, but we wish to
44197c478bd9Sstevel@tonic-gate  * return the bitmap of attrs that were set successfully. It is also
44207c478bd9Sstevel@tonic-gate  * called by Verify/Nverify to test the vattr/vfsstat attrs. It should
44217c478bd9Sstevel@tonic-gate  * always be called only after rfs4_do_set_attrs().
44227c478bd9Sstevel@tonic-gate  *
44237c478bd9Sstevel@tonic-gate  * Verify that the attributes are same as the expected ones. sargp->vap
44247c478bd9Sstevel@tonic-gate  * and sargp->sbp contain the input attributes as translated from fattr4.
44257c478bd9Sstevel@tonic-gate  *
44267c478bd9Sstevel@tonic-gate  * This function verifies only the attrs that correspond to a vattr or
44277c478bd9Sstevel@tonic-gate  * vfsstat struct. That is because of the extra step needed to get the
44287c478bd9Sstevel@tonic-gate  * corresponding system structs. Other attributes have already been set or
44297c478bd9Sstevel@tonic-gate  * verified by do_rfs4_set_attrs.
44307c478bd9Sstevel@tonic-gate  *
44317c478bd9Sstevel@tonic-gate  * Return 0 if all attrs match, -1 if some don't, error if error processing.
44327c478bd9Sstevel@tonic-gate  */
44337c478bd9Sstevel@tonic-gate static int
44347c478bd9Sstevel@tonic-gate rfs4_verify_attr(struct nfs4_svgetit_arg *sargp,
44357c478bd9Sstevel@tonic-gate 	bitmap4 *resp, struct nfs4_ntov_table *ntovp)
44367c478bd9Sstevel@tonic-gate {
44377c478bd9Sstevel@tonic-gate 	int error, ret_error = 0;
44387c478bd9Sstevel@tonic-gate 	int i, k;
44397c478bd9Sstevel@tonic-gate 	uint_t sva_mask = sargp->vap->va_mask;
44407c478bd9Sstevel@tonic-gate 	uint_t vbit;
44417c478bd9Sstevel@tonic-gate 	union nfs4_attr_u *na;
44427c478bd9Sstevel@tonic-gate 	uint8_t *amap;
44437c478bd9Sstevel@tonic-gate 	bool_t getsb = ntovp->vfsstat;
44447c478bd9Sstevel@tonic-gate 
44457c478bd9Sstevel@tonic-gate 	if (sva_mask != 0) {
44467c478bd9Sstevel@tonic-gate 		/*
44477c478bd9Sstevel@tonic-gate 		 * Okay to overwrite sargp->vap because we verify based
44487c478bd9Sstevel@tonic-gate 		 * on the incoming values.
44497c478bd9Sstevel@tonic-gate 		 */
44507c478bd9Sstevel@tonic-gate 		ret_error = VOP_GETATTR(sargp->cs->vp, sargp->vap, 0,
44517c478bd9Sstevel@tonic-gate 				sargp->cs->cr);
44527c478bd9Sstevel@tonic-gate 		if (ret_error) {
44537c478bd9Sstevel@tonic-gate 			if (resp == NULL)
44547c478bd9Sstevel@tonic-gate 				return (ret_error);
44557c478bd9Sstevel@tonic-gate 			/*
44567c478bd9Sstevel@tonic-gate 			 * Must return bitmap of successful attrs
44577c478bd9Sstevel@tonic-gate 			 */
44587c478bd9Sstevel@tonic-gate 			sva_mask = 0;	/* to prevent checking vap later */
44597c478bd9Sstevel@tonic-gate 		} else {
44607c478bd9Sstevel@tonic-gate 			/*
44617c478bd9Sstevel@tonic-gate 			 * Some file systems clobber va_mask. it is probably
44627c478bd9Sstevel@tonic-gate 			 * wrong of them to do so, nonethless we practice
44637c478bd9Sstevel@tonic-gate 			 * defensive coding.
44647c478bd9Sstevel@tonic-gate 			 * See bug id 4276830.
44657c478bd9Sstevel@tonic-gate 			 */
44667c478bd9Sstevel@tonic-gate 			sargp->vap->va_mask = sva_mask;
44677c478bd9Sstevel@tonic-gate 		}
44687c478bd9Sstevel@tonic-gate 	}
44697c478bd9Sstevel@tonic-gate 
44707c478bd9Sstevel@tonic-gate 	if (getsb) {
44717c478bd9Sstevel@tonic-gate 		/*
44727c478bd9Sstevel@tonic-gate 		 * Now get the superblock and loop on the bitmap, as there is
44737c478bd9Sstevel@tonic-gate 		 * no simple way of translating from superblock to bitmap4.
44747c478bd9Sstevel@tonic-gate 		 */
44757c478bd9Sstevel@tonic-gate 		ret_error = VFS_STATVFS(sargp->cs->vp->v_vfsp, sargp->sbp);
44767c478bd9Sstevel@tonic-gate 		if (ret_error) {
44777c478bd9Sstevel@tonic-gate 			if (resp == NULL)
44787c478bd9Sstevel@tonic-gate 				goto errout;
44797c478bd9Sstevel@tonic-gate 			getsb = FALSE;
44807c478bd9Sstevel@tonic-gate 		}
44817c478bd9Sstevel@tonic-gate 	}
44827c478bd9Sstevel@tonic-gate 
44837c478bd9Sstevel@tonic-gate 	/*
44847c478bd9Sstevel@tonic-gate 	 * Now loop and verify each attribute which getattr returned
44857c478bd9Sstevel@tonic-gate 	 * whether it's the same as the input.
44867c478bd9Sstevel@tonic-gate 	 */
44877c478bd9Sstevel@tonic-gate 	if (resp == NULL && !getsb && (sva_mask == 0))
44887c478bd9Sstevel@tonic-gate 		goto errout;
44897c478bd9Sstevel@tonic-gate 
44907c478bd9Sstevel@tonic-gate 	na = ntovp->na;
44917c478bd9Sstevel@tonic-gate 	amap = ntovp->amap;
44927c478bd9Sstevel@tonic-gate 	k = 0;
44937c478bd9Sstevel@tonic-gate 	for (i = 0; i < ntovp->attrcnt; i++, na++, amap++) {
44947c478bd9Sstevel@tonic-gate 		k = *amap;
44957c478bd9Sstevel@tonic-gate 		ASSERT(nfs4_ntov_map[k].nval == k);
44967c478bd9Sstevel@tonic-gate 		vbit = nfs4_ntov_map[k].vbit;
44977c478bd9Sstevel@tonic-gate 
44987c478bd9Sstevel@tonic-gate 		/*
44997c478bd9Sstevel@tonic-gate 		 * If vattr attribute but VOP_GETATTR failed, or it's
45007c478bd9Sstevel@tonic-gate 		 * superblock attribute but VFS_STATVFS failed, skip
45017c478bd9Sstevel@tonic-gate 		 */
45027c478bd9Sstevel@tonic-gate 		if (vbit) {
45037c478bd9Sstevel@tonic-gate 			if ((vbit & sva_mask) == 0)
45047c478bd9Sstevel@tonic-gate 				continue;
45057c478bd9Sstevel@tonic-gate 		} else if (!(getsb && nfs4_ntov_map[k].vfsstat)) {
45067c478bd9Sstevel@tonic-gate 			continue;
45077c478bd9Sstevel@tonic-gate 		}
45087c478bd9Sstevel@tonic-gate 		error = (*nfs4_ntov_map[k].sv_getit)(
45097c478bd9Sstevel@tonic-gate 				NFS4ATTR_VERIT, sargp, na);
45107c478bd9Sstevel@tonic-gate 		if (resp != NULL) {
45117c478bd9Sstevel@tonic-gate 			if (error)
45127c478bd9Sstevel@tonic-gate 				ret_error = -1;	/* not all match */
45137c478bd9Sstevel@tonic-gate 			else	/* update response bitmap */
45147c478bd9Sstevel@tonic-gate 				*resp |= nfs4_ntov_map[k].fbit;
45157c478bd9Sstevel@tonic-gate 			continue;
45167c478bd9Sstevel@tonic-gate 		}
45177c478bd9Sstevel@tonic-gate 		if (error) {
45187c478bd9Sstevel@tonic-gate 			ret_error = -1;	/* not all match */
45197c478bd9Sstevel@tonic-gate 			break;
45207c478bd9Sstevel@tonic-gate 		}
45217c478bd9Sstevel@tonic-gate 	}
45227c478bd9Sstevel@tonic-gate errout:
45237c478bd9Sstevel@tonic-gate 	return (ret_error);
45247c478bd9Sstevel@tonic-gate }
45257c478bd9Sstevel@tonic-gate 
45267c478bd9Sstevel@tonic-gate /*
45277c478bd9Sstevel@tonic-gate  * Decode the attribute to be set/verified. If the attr requires a sys op
45287c478bd9Sstevel@tonic-gate  * (VOP_GETATTR, VFS_VFSSTAT), and the request is to verify, then don't
45297c478bd9Sstevel@tonic-gate  * call the sv_getit function for it, because the sys op hasn't yet been done.
45307c478bd9Sstevel@tonic-gate  * Return 0 for success, error code if failed.
45317c478bd9Sstevel@tonic-gate  *
45327c478bd9Sstevel@tonic-gate  * Note: the decoded arg is not freed here but in nfs4_ntov_table_free.
45337c478bd9Sstevel@tonic-gate  */
45347c478bd9Sstevel@tonic-gate static int
45357c478bd9Sstevel@tonic-gate decode_fattr4_attr(nfs4_attr_cmd_t cmd, struct nfs4_svgetit_arg *sargp,
45367c478bd9Sstevel@tonic-gate 	int k, XDR *xdrp, bitmap4 *resp_bval, union nfs4_attr_u *nap)
45377c478bd9Sstevel@tonic-gate {
45387c478bd9Sstevel@tonic-gate 	int error = 0;
45397c478bd9Sstevel@tonic-gate 	bool_t set_later;
45407c478bd9Sstevel@tonic-gate 
45417c478bd9Sstevel@tonic-gate 	sargp->vap->va_mask |= nfs4_ntov_map[k].vbit;
45427c478bd9Sstevel@tonic-gate 
45437c478bd9Sstevel@tonic-gate 	if ((*nfs4_ntov_map[k].xfunc)(xdrp, nap)) {
45447c478bd9Sstevel@tonic-gate 		set_later = nfs4_ntov_map[k].vbit || nfs4_ntov_map[k].vfsstat;
45457c478bd9Sstevel@tonic-gate 		/*
45467c478bd9Sstevel@tonic-gate 		 * don't verify yet if a vattr or sb dependent attr,
45477c478bd9Sstevel@tonic-gate 		 * because we don't have their sys values yet.
45487c478bd9Sstevel@tonic-gate 		 * Will be done later.
45497c478bd9Sstevel@tonic-gate 		 */
45507c478bd9Sstevel@tonic-gate 		if (! (set_later && (cmd == NFS4ATTR_VERIT))) {
45517c478bd9Sstevel@tonic-gate 			/*
45527c478bd9Sstevel@tonic-gate 			 * ACLs are a special case, since setting the MODE
45537c478bd9Sstevel@tonic-gate 			 * conflicts with setting the ACL.  We delay setting
45547c478bd9Sstevel@tonic-gate 			 * the ACL until all other attributes have been set.
45557c478bd9Sstevel@tonic-gate 			 * The ACL gets set in do_rfs4_op_setattr().
45567c478bd9Sstevel@tonic-gate 			 */
45577c478bd9Sstevel@tonic-gate 			if (nfs4_ntov_map[k].fbit != FATTR4_ACL_MASK) {
45587c478bd9Sstevel@tonic-gate 				error = (*nfs4_ntov_map[k].sv_getit)(cmd,
45597c478bd9Sstevel@tonic-gate 				    sargp, nap);
45607c478bd9Sstevel@tonic-gate 				if (error) {
45617c478bd9Sstevel@tonic-gate 					xdr_free(nfs4_ntov_map[k].xfunc,
45627c478bd9Sstevel@tonic-gate 					    (caddr_t)nap);
45637c478bd9Sstevel@tonic-gate 				}
45647c478bd9Sstevel@tonic-gate 			}
45657c478bd9Sstevel@tonic-gate 		}
45667c478bd9Sstevel@tonic-gate 	} else {
45677c478bd9Sstevel@tonic-gate #ifdef  DEBUG
45687c478bd9Sstevel@tonic-gate 		cmn_err(CE_NOTE, "decode_fattr4_attr: error "
45697c478bd9Sstevel@tonic-gate 			"decoding attribute %d\n", k);
45707c478bd9Sstevel@tonic-gate #endif
45717c478bd9Sstevel@tonic-gate 		error = EINVAL;
45727c478bd9Sstevel@tonic-gate 	}
45737c478bd9Sstevel@tonic-gate 	if (!error && resp_bval && !set_later) {
45747c478bd9Sstevel@tonic-gate 		*resp_bval |= nfs4_ntov_map[k].fbit;
45757c478bd9Sstevel@tonic-gate 	}
45767c478bd9Sstevel@tonic-gate 
45777c478bd9Sstevel@tonic-gate 	return (error);
45787c478bd9Sstevel@tonic-gate }
45797c478bd9Sstevel@tonic-gate 
45807c478bd9Sstevel@tonic-gate /*
45817c478bd9Sstevel@tonic-gate  * Set vattr based on incoming fattr4 attrs - used by setattr.
45827c478bd9Sstevel@tonic-gate  * Set response mask. Ignore any values that are not writable vattr attrs.
45837c478bd9Sstevel@tonic-gate  */
45847c478bd9Sstevel@tonic-gate static nfsstat4
45857c478bd9Sstevel@tonic-gate do_rfs4_set_attrs(bitmap4 *resp, fattr4 *fattrp, struct compound_state *cs,
45867c478bd9Sstevel@tonic-gate 		struct nfs4_svgetit_arg *sargp, struct nfs4_ntov_table *ntovp,
45877c478bd9Sstevel@tonic-gate 		nfs4_attr_cmd_t cmd)
45887c478bd9Sstevel@tonic-gate {
45897c478bd9Sstevel@tonic-gate 	int error = 0;
45907c478bd9Sstevel@tonic-gate 	int i;
45917c478bd9Sstevel@tonic-gate 	char *attrs = fattrp->attrlist4;
45927c478bd9Sstevel@tonic-gate 	uint32_t attrslen = fattrp->attrlist4_len;
45937c478bd9Sstevel@tonic-gate 	XDR xdr;
45947c478bd9Sstevel@tonic-gate 	nfsstat4 status = NFS4_OK;
45957c478bd9Sstevel@tonic-gate 	vnode_t *vp = cs->vp;
45967c478bd9Sstevel@tonic-gate 	union nfs4_attr_u *na;
45977c478bd9Sstevel@tonic-gate 	uint8_t *amap;
45987c478bd9Sstevel@tonic-gate 
45997c478bd9Sstevel@tonic-gate #ifndef lint
46007c478bd9Sstevel@tonic-gate 	/*
46017c478bd9Sstevel@tonic-gate 	 * Make sure that maximum attribute number can be expressed as an
46027c478bd9Sstevel@tonic-gate 	 * 8 bit quantity.
46037c478bd9Sstevel@tonic-gate 	 */
46047c478bd9Sstevel@tonic-gate 	ASSERT(NFS4_MAXNUM_ATTRS <= (UINT8_MAX + 1));
46057c478bd9Sstevel@tonic-gate #endif
46067c478bd9Sstevel@tonic-gate 
46077c478bd9Sstevel@tonic-gate 	if (vp == NULL) {
46087c478bd9Sstevel@tonic-gate 		if (resp)
46097c478bd9Sstevel@tonic-gate 			*resp = 0;
46107c478bd9Sstevel@tonic-gate 		return (NFS4ERR_NOFILEHANDLE);
46117c478bd9Sstevel@tonic-gate 	}
46127c478bd9Sstevel@tonic-gate 	if (cs->access == CS_ACCESS_DENIED) {
46137c478bd9Sstevel@tonic-gate 		if (resp)
46147c478bd9Sstevel@tonic-gate 			*resp = 0;
46157c478bd9Sstevel@tonic-gate 		return (NFS4ERR_ACCESS);
46167c478bd9Sstevel@tonic-gate 	}
46177c478bd9Sstevel@tonic-gate 
46187c478bd9Sstevel@tonic-gate 	sargp->op = cmd;
46197c478bd9Sstevel@tonic-gate 	sargp->cs = cs;
46207c478bd9Sstevel@tonic-gate 	sargp->flag = 0;	/* may be set later */
46217c478bd9Sstevel@tonic-gate 	sargp->vap->va_mask = 0;
46227c478bd9Sstevel@tonic-gate 	sargp->rdattr_error = NFS4_OK;
46237c478bd9Sstevel@tonic-gate 	sargp->rdattr_error_req = FALSE;
46247c478bd9Sstevel@tonic-gate 	/* sargp->sbp is set by the caller */
46257c478bd9Sstevel@tonic-gate 
46267c478bd9Sstevel@tonic-gate 	xdrmem_create(&xdr, attrs, attrslen, XDR_DECODE);
46277c478bd9Sstevel@tonic-gate 
46287c478bd9Sstevel@tonic-gate 	na = ntovp->na;
46297c478bd9Sstevel@tonic-gate 	amap = ntovp->amap;
46307c478bd9Sstevel@tonic-gate 
46317c478bd9Sstevel@tonic-gate 	/*
46327c478bd9Sstevel@tonic-gate 	 * The following loop iterates on the nfs4_ntov_map checking
46337c478bd9Sstevel@tonic-gate 	 * if the fbit is set in the requested bitmap.
46347c478bd9Sstevel@tonic-gate 	 * If set then we process the arguments using the
46357c478bd9Sstevel@tonic-gate 	 * rfs4_fattr4 conversion functions to populate the setattr
46367c478bd9Sstevel@tonic-gate 	 * vattr and va_mask. Any settable attrs that are not using vattr
46377c478bd9Sstevel@tonic-gate 	 * will be set in this loop.
46387c478bd9Sstevel@tonic-gate 	 */
46397c478bd9Sstevel@tonic-gate 	for (i = 0; i < nfs4_ntov_map_size; i++) {
46407c478bd9Sstevel@tonic-gate 		if (!(fattrp->attrmask & nfs4_ntov_map[i].fbit)) {
46417c478bd9Sstevel@tonic-gate 			continue;
46427c478bd9Sstevel@tonic-gate 		}
46437c478bd9Sstevel@tonic-gate 		/*
46447c478bd9Sstevel@tonic-gate 		 * If setattr, must be a writable attr.
46457c478bd9Sstevel@tonic-gate 		 * If verify/nverify, must be a readable attr.
46467c478bd9Sstevel@tonic-gate 		 */
46477c478bd9Sstevel@tonic-gate 		if ((error = (*nfs4_ntov_map[i].sv_getit)(
46487c478bd9Sstevel@tonic-gate 				    NFS4ATTR_SUPPORTED, sargp, NULL)) != 0) {
46497c478bd9Sstevel@tonic-gate 			/*
46507c478bd9Sstevel@tonic-gate 			 * Client tries to set/verify an
46517c478bd9Sstevel@tonic-gate 			 * unsupported attribute, tries to set
46527c478bd9Sstevel@tonic-gate 			 * a read only attr or verify a write
46537c478bd9Sstevel@tonic-gate 			 * only one - error!
46547c478bd9Sstevel@tonic-gate 			 */
46557c478bd9Sstevel@tonic-gate 			break;
46567c478bd9Sstevel@tonic-gate 		}
46577c478bd9Sstevel@tonic-gate 		/*
46587c478bd9Sstevel@tonic-gate 		 * Decode the attribute to set/verify
46597c478bd9Sstevel@tonic-gate 		 */
46607c478bd9Sstevel@tonic-gate 		error = decode_fattr4_attr(cmd, sargp, nfs4_ntov_map[i].nval,
46617c478bd9Sstevel@tonic-gate 					&xdr, resp ? resp : NULL, na);
46627c478bd9Sstevel@tonic-gate 		if (error)
46637c478bd9Sstevel@tonic-gate 			break;
46647c478bd9Sstevel@tonic-gate 		*amap++ = (uint8_t)nfs4_ntov_map[i].nval;
46657c478bd9Sstevel@tonic-gate 		na++;
46667c478bd9Sstevel@tonic-gate 		(ntovp->attrcnt)++;
46677c478bd9Sstevel@tonic-gate 		if (nfs4_ntov_map[i].vfsstat)
46687c478bd9Sstevel@tonic-gate 			ntovp->vfsstat = TRUE;
46697c478bd9Sstevel@tonic-gate 	}
46707c478bd9Sstevel@tonic-gate 
46717c478bd9Sstevel@tonic-gate 	if (error != 0)
46727c478bd9Sstevel@tonic-gate 		status = (error == ENOTSUP ?	NFS4ERR_ATTRNOTSUPP :
46737c478bd9Sstevel@tonic-gate 						puterrno4(error));
46747c478bd9Sstevel@tonic-gate 	/* xdrmem_destroy(&xdrs); */	/* NO-OP */
46757c478bd9Sstevel@tonic-gate 	return (status);
46767c478bd9Sstevel@tonic-gate }
46777c478bd9Sstevel@tonic-gate 
46787c478bd9Sstevel@tonic-gate static nfsstat4
46797c478bd9Sstevel@tonic-gate do_rfs4_op_setattr(bitmap4 *resp, fattr4 *fattrp, struct compound_state *cs,
46807c478bd9Sstevel@tonic-gate 		stateid4 *stateid)
46817c478bd9Sstevel@tonic-gate {
46827c478bd9Sstevel@tonic-gate 	int error = 0;
46837c478bd9Sstevel@tonic-gate 	struct nfs4_svgetit_arg sarg;
46847c478bd9Sstevel@tonic-gate 	bool_t trunc;
46857c478bd9Sstevel@tonic-gate 
46867c478bd9Sstevel@tonic-gate 	nfsstat4 status = NFS4_OK;
46877c478bd9Sstevel@tonic-gate 	cred_t *cr = cs->cr;
46887c478bd9Sstevel@tonic-gate 	vnode_t *vp = cs->vp;
46897c478bd9Sstevel@tonic-gate 	struct nfs4_ntov_table ntov;
46907c478bd9Sstevel@tonic-gate 	struct statvfs64 sb;
46917c478bd9Sstevel@tonic-gate 	struct vattr bva;
46927c478bd9Sstevel@tonic-gate 	struct flock64 bf;
46937c478bd9Sstevel@tonic-gate 	int in_crit = 0;
46947c478bd9Sstevel@tonic-gate 	uint_t saved_mask = 0;
46957c478bd9Sstevel@tonic-gate 	caller_context_t ct;
46967c478bd9Sstevel@tonic-gate 
46977c478bd9Sstevel@tonic-gate 	*resp = 0;
46987c478bd9Sstevel@tonic-gate 	sarg.sbp = &sb;
46997c478bd9Sstevel@tonic-gate 	nfs4_ntov_table_init(&ntov);
47007c478bd9Sstevel@tonic-gate 	status = do_rfs4_set_attrs(resp, fattrp, cs, &sarg, &ntov,
47017c478bd9Sstevel@tonic-gate 			NFS4ATTR_SETIT);
47027c478bd9Sstevel@tonic-gate 	if (status != NFS4_OK) {
47037c478bd9Sstevel@tonic-gate 		/*
47047c478bd9Sstevel@tonic-gate 		 * failed set attrs
47057c478bd9Sstevel@tonic-gate 		 */
47067c478bd9Sstevel@tonic-gate 		goto done;
47077c478bd9Sstevel@tonic-gate 	}
47087c478bd9Sstevel@tonic-gate 	if ((sarg.vap->va_mask == 0) &&
47097c478bd9Sstevel@tonic-gate 	    (! (fattrp->attrmask & FATTR4_ACL_MASK))) {
47107c478bd9Sstevel@tonic-gate 		/*
47117c478bd9Sstevel@tonic-gate 		 * no further work to be done
47127c478bd9Sstevel@tonic-gate 		 */
47137c478bd9Sstevel@tonic-gate 		goto done;
47147c478bd9Sstevel@tonic-gate 	}
47157c478bd9Sstevel@tonic-gate 
47167c478bd9Sstevel@tonic-gate 	/*
47177c478bd9Sstevel@tonic-gate 	 * If we got a request to set the ACL and the MODE, only
47187c478bd9Sstevel@tonic-gate 	 * allow changing VSUID, VSGID, and VSVTX.  Attempting
47197c478bd9Sstevel@tonic-gate 	 * to change any other bits, along with setting an ACL,
47207c478bd9Sstevel@tonic-gate 	 * gives NFS4ERR_INVAL.
47217c478bd9Sstevel@tonic-gate 	 */
47227c478bd9Sstevel@tonic-gate 	if ((fattrp->attrmask & FATTR4_ACL_MASK) &&
47237c478bd9Sstevel@tonic-gate 	    (fattrp->attrmask & FATTR4_MODE_MASK)) {
47247c478bd9Sstevel@tonic-gate 		vattr_t va;
47257c478bd9Sstevel@tonic-gate 
47267c478bd9Sstevel@tonic-gate 		va.va_mask = AT_MODE;
47277c478bd9Sstevel@tonic-gate 		error = VOP_GETATTR(vp, &va, 0, cs->cr);
47287c478bd9Sstevel@tonic-gate 		if (error) {
47297c478bd9Sstevel@tonic-gate 			status = puterrno4(error);
47307c478bd9Sstevel@tonic-gate 			goto done;
47317c478bd9Sstevel@tonic-gate 		}
47327c478bd9Sstevel@tonic-gate 		if ((sarg.vap->va_mode ^ va.va_mode) &
47337c478bd9Sstevel@tonic-gate 		    ~(VSUID | VSGID | VSVTX)) {
47347c478bd9Sstevel@tonic-gate 			status = NFS4ERR_INVAL;
47357c478bd9Sstevel@tonic-gate 			goto done;
47367c478bd9Sstevel@tonic-gate 		}
47377c478bd9Sstevel@tonic-gate 	}
47387c478bd9Sstevel@tonic-gate 
47397c478bd9Sstevel@tonic-gate 	/* Check stateid only if size has been set */
47407c478bd9Sstevel@tonic-gate 	if (sarg.vap->va_mask & AT_SIZE) {
47417c478bd9Sstevel@tonic-gate 		trunc = (sarg.vap->va_size == 0);
47427c478bd9Sstevel@tonic-gate 		status = rfs4_check_stateid(FWRITE, cs->vp, stateid,
47437c478bd9Sstevel@tonic-gate 			trunc, &cs->deleg, sarg.vap->va_mask & AT_SIZE);
47447c478bd9Sstevel@tonic-gate 		if (status != NFS4_OK)
47457c478bd9Sstevel@tonic-gate 			goto done;
47467c478bd9Sstevel@tonic-gate 	}
47477c478bd9Sstevel@tonic-gate 
47487c478bd9Sstevel@tonic-gate 	ct.cc_sysid = 0;
47497c478bd9Sstevel@tonic-gate 	ct.cc_pid = 0;
47507c478bd9Sstevel@tonic-gate 	ct.cc_caller_id = nfs4_srv_caller_id;
47517c478bd9Sstevel@tonic-gate 
47527c478bd9Sstevel@tonic-gate 	/* XXX start of possible race with delegations */
47537c478bd9Sstevel@tonic-gate 
47547c478bd9Sstevel@tonic-gate 	/*
47557c478bd9Sstevel@tonic-gate 	 * We need to specially handle size changes because it is
47567c478bd9Sstevel@tonic-gate 	 * possible for the client to create a file with read-only
47577c478bd9Sstevel@tonic-gate 	 * modes, but with the file opened for writing. If the client
47587c478bd9Sstevel@tonic-gate 	 * then tries to set the file size, e.g. ftruncate(3C),
47597c478bd9Sstevel@tonic-gate 	 * fcntl(F_FREESP), the normal access checking done in
47607c478bd9Sstevel@tonic-gate 	 * VOP_SETATTR would prevent the client from doing it even though
47617c478bd9Sstevel@tonic-gate 	 * it should be allowed to do so.  To get around this, we do the
47627c478bd9Sstevel@tonic-gate 	 * access checking for ourselves and use VOP_SPACE which doesn't
47637c478bd9Sstevel@tonic-gate 	 * do the access checking.
47647c478bd9Sstevel@tonic-gate 	 * Also the client should not be allowed to change the file
47657c478bd9Sstevel@tonic-gate 	 * size if there is a conflicting non-blocking mandatory lock in
47667c478bd9Sstevel@tonic-gate 	 * the region of the change.
47677c478bd9Sstevel@tonic-gate 	 */
47687c478bd9Sstevel@tonic-gate 	if (vp->v_type == VREG && (sarg.vap->va_mask & AT_SIZE)) {
47697c478bd9Sstevel@tonic-gate 		u_offset_t offset;
47707c478bd9Sstevel@tonic-gate 		ssize_t length;
47717c478bd9Sstevel@tonic-gate 
47729720e166Sjasmith 		/*
47739720e166Sjasmith 		 * ufs_setattr clears AT_SIZE from vap->va_mask, but
47749720e166Sjasmith 		 * before returning, sarg.vap->va_mask is used to
47759720e166Sjasmith 		 * generate the setattr reply bitmap.  We also clear
47769720e166Sjasmith 		 * AT_SIZE below before calling VOP_SPACE.  For both
47779720e166Sjasmith 		 * of these cases, the va_mask needs to be saved here
47789720e166Sjasmith 		 * and restored after calling VOP_SETATTR.
47799720e166Sjasmith 		 */
47809720e166Sjasmith 		saved_mask = sarg.vap->va_mask;
47819720e166Sjasmith 
47827c478bd9Sstevel@tonic-gate 		/*
47837c478bd9Sstevel@tonic-gate 		 * Check any possible conflict due to NBMAND locks.
47847c478bd9Sstevel@tonic-gate 		 * Get into critical region before VOP_GETATTR, so the
47857c478bd9Sstevel@tonic-gate 		 * size attribute is valid when checking conflicts.
47867c478bd9Sstevel@tonic-gate 		 */
47877c478bd9Sstevel@tonic-gate 		if (nbl_need_check(vp)) {
47887c478bd9Sstevel@tonic-gate 			nbl_start_crit(vp, RW_READER);
47897c478bd9Sstevel@tonic-gate 			in_crit = 1;
47907c478bd9Sstevel@tonic-gate 		}
47917c478bd9Sstevel@tonic-gate 
47927c478bd9Sstevel@tonic-gate 		bva.va_mask = AT_UID|AT_SIZE;
47937c478bd9Sstevel@tonic-gate 		if (error = VOP_GETATTR(vp, &bva, 0, cr)) {
47947c478bd9Sstevel@tonic-gate 			status = puterrno4(error);
47957c478bd9Sstevel@tonic-gate 			goto done;
47967c478bd9Sstevel@tonic-gate 		}
47977c478bd9Sstevel@tonic-gate 
47987c478bd9Sstevel@tonic-gate 		if (in_crit) {
47997c478bd9Sstevel@tonic-gate 			if (sarg.vap->va_size < bva.va_size) {
48007c478bd9Sstevel@tonic-gate 				offset = sarg.vap->va_size;
48017c478bd9Sstevel@tonic-gate 				length = bva.va_size - sarg.vap->va_size;
48027c478bd9Sstevel@tonic-gate 			} else {
48037c478bd9Sstevel@tonic-gate 				offset = bva.va_size;
48047c478bd9Sstevel@tonic-gate 				length = sarg.vap->va_size - bva.va_size;
48057c478bd9Sstevel@tonic-gate 			}
48067c478bd9Sstevel@tonic-gate 			if (nbl_conflict(vp, NBL_WRITE, offset, length, 0)) {
48077c478bd9Sstevel@tonic-gate 				status = NFS4ERR_LOCKED;
48087c478bd9Sstevel@tonic-gate 				goto done;
48097c478bd9Sstevel@tonic-gate 			}
48107c478bd9Sstevel@tonic-gate 		}
48117c478bd9Sstevel@tonic-gate 
48127c478bd9Sstevel@tonic-gate 		if (crgetuid(cr) == bva.va_uid) {
48137c478bd9Sstevel@tonic-gate 			sarg.vap->va_mask &= ~AT_SIZE;
48147c478bd9Sstevel@tonic-gate 			bf.l_type = F_WRLCK;
48157c478bd9Sstevel@tonic-gate 			bf.l_whence = 0;
48167c478bd9Sstevel@tonic-gate 			bf.l_start = (off64_t)sarg.vap->va_size;
48177c478bd9Sstevel@tonic-gate 			bf.l_len = 0;
48187c478bd9Sstevel@tonic-gate 			bf.l_sysid = 0;
48197c478bd9Sstevel@tonic-gate 			bf.l_pid = 0;
48207c478bd9Sstevel@tonic-gate 			error = VOP_SPACE(vp, F_FREESP, &bf, FWRITE,
48217c478bd9Sstevel@tonic-gate 					(offset_t)sarg.vap->va_size, cr, &ct);
48227c478bd9Sstevel@tonic-gate 		}
48237c478bd9Sstevel@tonic-gate 	}
48247c478bd9Sstevel@tonic-gate 
48257c478bd9Sstevel@tonic-gate 	if (!error && sarg.vap->va_mask != 0)
48267c478bd9Sstevel@tonic-gate 		error = VOP_SETATTR(vp, sarg.vap, sarg.flag, cr, &ct);
48277c478bd9Sstevel@tonic-gate 
48289720e166Sjasmith 	/* restore va_mask -- ufs_setattr clears AT_SIZE */
48297c478bd9Sstevel@tonic-gate 	if (saved_mask & AT_SIZE)
48307c478bd9Sstevel@tonic-gate 		sarg.vap->va_mask |= AT_SIZE;
48317c478bd9Sstevel@tonic-gate 
48327c478bd9Sstevel@tonic-gate 	/*
48337c478bd9Sstevel@tonic-gate 	 * If an ACL was being set, it has been delayed until now,
48347c478bd9Sstevel@tonic-gate 	 * in order to set the mode (via the VOP_SETATTR() above) first.
48357c478bd9Sstevel@tonic-gate 	 */
48367c478bd9Sstevel@tonic-gate 	if ((! error) && (fattrp->attrmask & FATTR4_ACL_MASK)) {
48377c478bd9Sstevel@tonic-gate 		int i;
48387c478bd9Sstevel@tonic-gate 
48397c478bd9Sstevel@tonic-gate 		for (i = 0; i < NFS4_MAXNUM_ATTRS; i++)
48407c478bd9Sstevel@tonic-gate 			if (ntov.amap[i] == FATTR4_ACL)
48417c478bd9Sstevel@tonic-gate 				break;
48427c478bd9Sstevel@tonic-gate 		if (i < NFS4_MAXNUM_ATTRS) {
48437c478bd9Sstevel@tonic-gate 			error = (*nfs4_ntov_map[FATTR4_ACL].sv_getit)(
48447c478bd9Sstevel@tonic-gate 			    NFS4ATTR_SETIT, &sarg, &ntov.na[i]);
48457c478bd9Sstevel@tonic-gate 			if (error == 0) {
48467c478bd9Sstevel@tonic-gate 				*resp |= FATTR4_ACL_MASK;
48477c478bd9Sstevel@tonic-gate 			} else if (error == ENOTSUP) {
48487c478bd9Sstevel@tonic-gate 				(void) rfs4_verify_attr(&sarg, resp, &ntov);
48497c478bd9Sstevel@tonic-gate 				status = NFS4ERR_ATTRNOTSUPP;
48507c478bd9Sstevel@tonic-gate 				goto done;
48517c478bd9Sstevel@tonic-gate 			}
48527c478bd9Sstevel@tonic-gate 		} else {
48537c478bd9Sstevel@tonic-gate 			NFS4_DEBUG(rfs4_debug,
48547c478bd9Sstevel@tonic-gate 			    (CE_NOTE, "do_rfs4_op_setattr: "
48557c478bd9Sstevel@tonic-gate 			    "unable to find ACL in fattr4"));
48567c478bd9Sstevel@tonic-gate 			error = EINVAL;
48577c478bd9Sstevel@tonic-gate 		}
48587c478bd9Sstevel@tonic-gate 	}
48597c478bd9Sstevel@tonic-gate 
48607c478bd9Sstevel@tonic-gate 	if (error) {
48617c478bd9Sstevel@tonic-gate 		status = puterrno4(error);
48627c478bd9Sstevel@tonic-gate 
48637c478bd9Sstevel@tonic-gate 		/*
48647c478bd9Sstevel@tonic-gate 		 * Set the response bitmap when setattr failed.
48657c478bd9Sstevel@tonic-gate 		 * If VOP_SETATTR partially succeeded, test by doing a
48667c478bd9Sstevel@tonic-gate 		 * VOP_GETATTR on the object and comparing the data
48677c478bd9Sstevel@tonic-gate 		 * to the setattr arguments.
48687c478bd9Sstevel@tonic-gate 		 */
48697c478bd9Sstevel@tonic-gate 		(void) rfs4_verify_attr(&sarg, resp, &ntov);
48707c478bd9Sstevel@tonic-gate 	} else {
48717c478bd9Sstevel@tonic-gate 		/*
48727c478bd9Sstevel@tonic-gate 		 * Force modified metadata out to stable storage.
48737c478bd9Sstevel@tonic-gate 		 */
48747c478bd9Sstevel@tonic-gate 		(void) VOP_FSYNC(vp, FNODSYNC, cr);
48757c478bd9Sstevel@tonic-gate 		/*
48767c478bd9Sstevel@tonic-gate 		 * Set response bitmap
48777c478bd9Sstevel@tonic-gate 		 */
48789720e166Sjasmith 		nfs4_vmask_to_nmask_set(sarg.vap->va_mask, resp);
48797c478bd9Sstevel@tonic-gate 	}
48807c478bd9Sstevel@tonic-gate 
48817c478bd9Sstevel@tonic-gate /* Return early and already have a NFSv4 error */
48827c478bd9Sstevel@tonic-gate done:
48839720e166Sjasmith 	/*
48849720e166Sjasmith 	 * Except for nfs4_vmask_to_nmask_set(), vattr --> fattr
48859720e166Sjasmith 	 * conversion sets both readable and writeable NFS4 attrs
48869720e166Sjasmith 	 * for AT_MTIME and AT_ATIME.  The line below masks out
48879720e166Sjasmith 	 * unrequested attrs from the setattr result bitmap.  This
48889720e166Sjasmith 	 * is placed after the done: label to catch the ATTRNOTSUP
48899720e166Sjasmith 	 * case.
48909720e166Sjasmith 	 */
48919720e166Sjasmith 	*resp &= fattrp->attrmask;
48929720e166Sjasmith 
48937c478bd9Sstevel@tonic-gate 	if (in_crit)
48947c478bd9Sstevel@tonic-gate 		nbl_end_crit(vp);
48957c478bd9Sstevel@tonic-gate 
48967c478bd9Sstevel@tonic-gate 	nfs4_ntov_table_free(&ntov, &sarg);
48977c478bd9Sstevel@tonic-gate 
48987c478bd9Sstevel@tonic-gate 	return (status);
48997c478bd9Sstevel@tonic-gate }
49007c478bd9Sstevel@tonic-gate 
49017c478bd9Sstevel@tonic-gate /* ARGSUSED */
49027c478bd9Sstevel@tonic-gate static void
49037c478bd9Sstevel@tonic-gate rfs4_op_setattr(nfs_argop4 *argop, nfs_resop4 *resop, struct svc_req *req,
49047c478bd9Sstevel@tonic-gate 	struct compound_state *cs)
49057c478bd9Sstevel@tonic-gate {
49067c478bd9Sstevel@tonic-gate 	SETATTR4args *args = &argop->nfs_argop4_u.opsetattr;
49077c478bd9Sstevel@tonic-gate 	SETATTR4res *resp = &resop->nfs_resop4_u.opsetattr;
490845916cd2Sjpk 	bslabel_t *clabel;
49097c478bd9Sstevel@tonic-gate 
49107c478bd9Sstevel@tonic-gate 	if (cs->vp == NULL) {
49117c478bd9Sstevel@tonic-gate 		*cs->statusp = resp->status = NFS4ERR_NOFILEHANDLE;
49127c478bd9Sstevel@tonic-gate 		return;
49137c478bd9Sstevel@tonic-gate 	}
49147c478bd9Sstevel@tonic-gate 
49157c478bd9Sstevel@tonic-gate 	/*
49167c478bd9Sstevel@tonic-gate 	 * If there is an unshared filesystem mounted on this vnode,
49177c478bd9Sstevel@tonic-gate 	 * do not allow to setattr on this vnode.
49187c478bd9Sstevel@tonic-gate 	 */
49197c478bd9Sstevel@tonic-gate 	if (vn_ismntpt(cs->vp)) {
49207c478bd9Sstevel@tonic-gate 		*cs->statusp = resp->status = NFS4ERR_ACCESS;
49217c478bd9Sstevel@tonic-gate 		return;
49227c478bd9Sstevel@tonic-gate 	}
49237c478bd9Sstevel@tonic-gate 
49247c478bd9Sstevel@tonic-gate 	resp->attrsset = 0;
49257c478bd9Sstevel@tonic-gate 
49267c478bd9Sstevel@tonic-gate 	if (rdonly4(cs->exi, cs->vp, req)) {
49277c478bd9Sstevel@tonic-gate 		*cs->statusp = resp->status = NFS4ERR_ROFS;
49287c478bd9Sstevel@tonic-gate 		return;
49297c478bd9Sstevel@tonic-gate 	}
49307c478bd9Sstevel@tonic-gate 
493145916cd2Sjpk 	/* check label before setting attributes */
493245916cd2Sjpk 	if (is_system_labeled()) {
493345916cd2Sjpk 		ASSERT(req->rq_label != NULL);
493445916cd2Sjpk 		clabel = req->rq_label;
493545916cd2Sjpk 		DTRACE_PROBE2(tx__rfs4__log__info__opsetattr__clabel, char *,
493645916cd2Sjpk 		    "got client label from request(1)",
493745916cd2Sjpk 		    struct svc_req *, req);
493845916cd2Sjpk 		if (!blequal(&l_admin_low->tsl_label, clabel)) {
493945916cd2Sjpk 			if (!do_rfs4_label_check(clabel, cs->vp,
494045916cd2Sjpk 			    EQUALITY_CHECK)) {
494145916cd2Sjpk 				*cs->statusp = resp->status = NFS4ERR_ACCESS;
494245916cd2Sjpk 				return;
494345916cd2Sjpk 			}
494445916cd2Sjpk 		}
494545916cd2Sjpk 	}
494645916cd2Sjpk 
49477c478bd9Sstevel@tonic-gate 	*cs->statusp = resp->status =
49487c478bd9Sstevel@tonic-gate 		do_rfs4_op_setattr(&resp->attrsset, &args->obj_attributes, cs,
49497c478bd9Sstevel@tonic-gate 			&args->stateid);
49507c478bd9Sstevel@tonic-gate }
49517c478bd9Sstevel@tonic-gate 
49527c478bd9Sstevel@tonic-gate /* ARGSUSED */
49537c478bd9Sstevel@tonic-gate static void
49547c478bd9Sstevel@tonic-gate rfs4_op_verify(nfs_argop4 *argop, nfs_resop4 *resop, struct svc_req *req,
49557c478bd9Sstevel@tonic-gate 	struct compound_state *cs)
49567c478bd9Sstevel@tonic-gate {
49577c478bd9Sstevel@tonic-gate 	/*
49587c478bd9Sstevel@tonic-gate 	 * verify and nverify are exactly the same, except that nverify
49597c478bd9Sstevel@tonic-gate 	 * succeeds when some argument changed, and verify succeeds when
49607c478bd9Sstevel@tonic-gate 	 * when none changed.
49617c478bd9Sstevel@tonic-gate 	 */
49627c478bd9Sstevel@tonic-gate 
49637c478bd9Sstevel@tonic-gate 	VERIFY4args  *args = &argop->nfs_argop4_u.opverify;
49647c478bd9Sstevel@tonic-gate 	VERIFY4res *resp = &resop->nfs_resop4_u.opverify;
49657c478bd9Sstevel@tonic-gate 
49667c478bd9Sstevel@tonic-gate 	int error;
49677c478bd9Sstevel@tonic-gate 	struct nfs4_svgetit_arg sarg;
49687c478bd9Sstevel@tonic-gate 	struct statvfs64 sb;
49697c478bd9Sstevel@tonic-gate 	struct nfs4_ntov_table ntov;
49707c478bd9Sstevel@tonic-gate 
49717c478bd9Sstevel@tonic-gate 	if (cs->vp == NULL) {
49727c478bd9Sstevel@tonic-gate 		*cs->statusp = resp->status = NFS4ERR_NOFILEHANDLE;
49737c478bd9Sstevel@tonic-gate 		return;
49747c478bd9Sstevel@tonic-gate 	}
49757c478bd9Sstevel@tonic-gate 
49767c478bd9Sstevel@tonic-gate 	sarg.sbp = &sb;
49777c478bd9Sstevel@tonic-gate 	nfs4_ntov_table_init(&ntov);
49787c478bd9Sstevel@tonic-gate 	resp->status = do_rfs4_set_attrs(NULL, &args->obj_attributes, cs,
49797c478bd9Sstevel@tonic-gate 				&sarg, &ntov, NFS4ATTR_VERIT);
49807c478bd9Sstevel@tonic-gate 	if (resp->status != NFS4_OK) {
49817c478bd9Sstevel@tonic-gate 		/*
49827c478bd9Sstevel@tonic-gate 		 * do_rfs4_set_attrs will try to verify systemwide attrs,
49837c478bd9Sstevel@tonic-gate 		 * so could return -1 for "no match".
49847c478bd9Sstevel@tonic-gate 		 */
49857c478bd9Sstevel@tonic-gate 		if (resp->status == -1)
49867c478bd9Sstevel@tonic-gate 			resp->status = NFS4ERR_NOT_SAME;
49877c478bd9Sstevel@tonic-gate 		goto done;
49887c478bd9Sstevel@tonic-gate 	}
49897c478bd9Sstevel@tonic-gate 	error = rfs4_verify_attr(&sarg, NULL, &ntov);
49907c478bd9Sstevel@tonic-gate 	switch (error) {
49917c478bd9Sstevel@tonic-gate 	case 0:
49927c478bd9Sstevel@tonic-gate 		resp->status = NFS4_OK;
49937c478bd9Sstevel@tonic-gate 		break;
49947c478bd9Sstevel@tonic-gate 	case -1:
49957c478bd9Sstevel@tonic-gate 		resp->status = NFS4ERR_NOT_SAME;
49967c478bd9Sstevel@tonic-gate 		break;
49977c478bd9Sstevel@tonic-gate 	default:
49987c478bd9Sstevel@tonic-gate 		resp->status = puterrno4(error);
49997c478bd9Sstevel@tonic-gate 		break;
50007c478bd9Sstevel@tonic-gate 	}
50017c478bd9Sstevel@tonic-gate done:
50027c478bd9Sstevel@tonic-gate 	*cs->statusp = resp->status;
50037c478bd9Sstevel@tonic-gate 	nfs4_ntov_table_free(&ntov, &sarg);
50047c478bd9Sstevel@tonic-gate }
50057c478bd9Sstevel@tonic-gate 
50067c478bd9Sstevel@tonic-gate /* ARGSUSED */
50077c478bd9Sstevel@tonic-gate static void
50087c478bd9Sstevel@tonic-gate rfs4_op_nverify(nfs_argop4 *argop, nfs_resop4 *resop, struct svc_req *req,
50097c478bd9Sstevel@tonic-gate 	struct compound_state *cs)
50107c478bd9Sstevel@tonic-gate {
50117c478bd9Sstevel@tonic-gate 	/*
50127c478bd9Sstevel@tonic-gate 	 * verify and nverify are exactly the same, except that nverify
50137c478bd9Sstevel@tonic-gate 	 * succeeds when some argument changed, and verify succeeds when
50147c478bd9Sstevel@tonic-gate 	 * when none changed.
50157c478bd9Sstevel@tonic-gate 	 */
50167c478bd9Sstevel@tonic-gate 
50177c478bd9Sstevel@tonic-gate 	NVERIFY4args  *args = &argop->nfs_argop4_u.opnverify;
50187c478bd9Sstevel@tonic-gate 	NVERIFY4res *resp = &resop->nfs_resop4_u.opnverify;
50197c478bd9Sstevel@tonic-gate 
50207c478bd9Sstevel@tonic-gate 	int error;
50217c478bd9Sstevel@tonic-gate 	struct nfs4_svgetit_arg sarg;
50227c478bd9Sstevel@tonic-gate 	struct statvfs64 sb;
50237c478bd9Sstevel@tonic-gate 	struct nfs4_ntov_table ntov;
50247c478bd9Sstevel@tonic-gate 
50257c478bd9Sstevel@tonic-gate 	if (cs->vp == NULL) {
50267c478bd9Sstevel@tonic-gate 		*cs->statusp = resp->status = NFS4ERR_NOFILEHANDLE;
50277c478bd9Sstevel@tonic-gate 		return;
50287c478bd9Sstevel@tonic-gate 	}
50297c478bd9Sstevel@tonic-gate 	sarg.sbp = &sb;
50307c478bd9Sstevel@tonic-gate 	nfs4_ntov_table_init(&ntov);
50317c478bd9Sstevel@tonic-gate 	resp->status = do_rfs4_set_attrs(NULL, &args->obj_attributes, cs,
50327c478bd9Sstevel@tonic-gate 				&sarg, &ntov, NFS4ATTR_VERIT);
50337c478bd9Sstevel@tonic-gate 	if (resp->status != NFS4_OK) {
50347c478bd9Sstevel@tonic-gate 		/*
50357c478bd9Sstevel@tonic-gate 		 * do_rfs4_set_attrs will try to verify systemwide attrs,
50367c478bd9Sstevel@tonic-gate 		 * so could return -1 for "no match".
50377c478bd9Sstevel@tonic-gate 		 */
50387c478bd9Sstevel@tonic-gate 		if (resp->status == -1)
50397c478bd9Sstevel@tonic-gate 			resp->status = NFS4_OK;
50407c478bd9Sstevel@tonic-gate 		goto done;
50417c478bd9Sstevel@tonic-gate 	}
50427c478bd9Sstevel@tonic-gate 	error = rfs4_verify_attr(&sarg, NULL, &ntov);
50437c478bd9Sstevel@tonic-gate 	switch (error) {
50447c478bd9Sstevel@tonic-gate 	case 0:
50457c478bd9Sstevel@tonic-gate 		resp->status = NFS4ERR_SAME;
50467c478bd9Sstevel@tonic-gate 		break;
50477c478bd9Sstevel@tonic-gate 	case -1:
50487c478bd9Sstevel@tonic-gate 		resp->status = NFS4_OK;
50497c478bd9Sstevel@tonic-gate 		break;
50507c478bd9Sstevel@tonic-gate 	default:
50517c478bd9Sstevel@tonic-gate 		resp->status = puterrno4(error);
50527c478bd9Sstevel@tonic-gate 		break;
50537c478bd9Sstevel@tonic-gate 	}
50547c478bd9Sstevel@tonic-gate done:
50557c478bd9Sstevel@tonic-gate 	*cs->statusp = resp->status;
50567c478bd9Sstevel@tonic-gate 	nfs4_ntov_table_free(&ntov, &sarg);
50577c478bd9Sstevel@tonic-gate }
50587c478bd9Sstevel@tonic-gate 
50597c478bd9Sstevel@tonic-gate /*
50607c478bd9Sstevel@tonic-gate  * XXX - This should live in an NFS header file.
50617c478bd9Sstevel@tonic-gate  */
50627c478bd9Sstevel@tonic-gate #define	MAX_IOVECS	12
50637c478bd9Sstevel@tonic-gate 
50647c478bd9Sstevel@tonic-gate /* ARGSUSED */
50657c478bd9Sstevel@tonic-gate static void
50667c478bd9Sstevel@tonic-gate rfs4_op_write(nfs_argop4 *argop, nfs_resop4 *resop, struct svc_req *req,
50677c478bd9Sstevel@tonic-gate 	struct compound_state *cs)
50687c478bd9Sstevel@tonic-gate {
50697c478bd9Sstevel@tonic-gate 	WRITE4args  *args = &argop->nfs_argop4_u.opwrite;
50707c478bd9Sstevel@tonic-gate 	WRITE4res *resp = &resop->nfs_resop4_u.opwrite;
50717c478bd9Sstevel@tonic-gate 	int error;
50727c478bd9Sstevel@tonic-gate 	vnode_t *vp;
50737c478bd9Sstevel@tonic-gate 	struct vattr bva;
50747c478bd9Sstevel@tonic-gate 	u_offset_t rlimit;
50757c478bd9Sstevel@tonic-gate 	struct uio uio;
50767c478bd9Sstevel@tonic-gate 	struct iovec iov[MAX_IOVECS];
50777c478bd9Sstevel@tonic-gate 	struct iovec *iovp;
50787c478bd9Sstevel@tonic-gate 	int iovcnt;
50797c478bd9Sstevel@tonic-gate 	int ioflag;
50807c478bd9Sstevel@tonic-gate 	cred_t *savecred, *cr;
50817c478bd9Sstevel@tonic-gate 	bool_t *deleg = &cs->deleg;
50827c478bd9Sstevel@tonic-gate 	nfsstat4 stat;
50837c478bd9Sstevel@tonic-gate 	int in_crit = 0;
50847c478bd9Sstevel@tonic-gate 
50857c478bd9Sstevel@tonic-gate 	vp = cs->vp;
50867c478bd9Sstevel@tonic-gate 	if (vp == NULL) {
50877c478bd9Sstevel@tonic-gate 		*cs->statusp = resp->status = NFS4ERR_NOFILEHANDLE;
50887c478bd9Sstevel@tonic-gate 		return;
50897c478bd9Sstevel@tonic-gate 	}
50907c478bd9Sstevel@tonic-gate 	if (cs->access == CS_ACCESS_DENIED) {
50917c478bd9Sstevel@tonic-gate 		*cs->statusp = resp->status = NFS4ERR_ACCESS;
50927c478bd9Sstevel@tonic-gate 		return;
50937c478bd9Sstevel@tonic-gate 	}
50947c478bd9Sstevel@tonic-gate 
50957c478bd9Sstevel@tonic-gate 	cr = cs->cr;
50967c478bd9Sstevel@tonic-gate 
50977c478bd9Sstevel@tonic-gate 	/*
50987c478bd9Sstevel@tonic-gate 	 * We have to enter the critical region before calling VOP_RWLOCK
50997c478bd9Sstevel@tonic-gate 	 * to avoid a deadlock with ufs.
51007c478bd9Sstevel@tonic-gate 	 */
51017c478bd9Sstevel@tonic-gate 	if (nbl_need_check(vp)) {
51027c478bd9Sstevel@tonic-gate 		nbl_start_crit(vp, RW_READER);
51037c478bd9Sstevel@tonic-gate 		in_crit = 1;
51047c478bd9Sstevel@tonic-gate 		if (nbl_conflict(vp, NBL_WRITE,
51057c478bd9Sstevel@tonic-gate 				args->offset, args->data_len, 0)) {
51067c478bd9Sstevel@tonic-gate 			*cs->statusp = resp->status = NFS4ERR_LOCKED;
51077c478bd9Sstevel@tonic-gate 			goto out;
51087c478bd9Sstevel@tonic-gate 		}
51097c478bd9Sstevel@tonic-gate 	}
51107c478bd9Sstevel@tonic-gate 
51117c478bd9Sstevel@tonic-gate 	if ((stat = rfs4_check_stateid(FWRITE, vp, &args->stateid, FALSE,
51127c478bd9Sstevel@tonic-gate 					deleg, TRUE)) != NFS4_OK) {
51137c478bd9Sstevel@tonic-gate 		*cs->statusp = resp->status = stat;
51147c478bd9Sstevel@tonic-gate 		goto out;
51157c478bd9Sstevel@tonic-gate 	}
51167c478bd9Sstevel@tonic-gate 
51177c478bd9Sstevel@tonic-gate 	bva.va_mask = AT_MODE | AT_UID;
51187c478bd9Sstevel@tonic-gate 	error = VOP_GETATTR(vp, &bva, 0, cr);
51197c478bd9Sstevel@tonic-gate 
51207c478bd9Sstevel@tonic-gate 	/*
51217c478bd9Sstevel@tonic-gate 	 * If we can't get the attributes, then we can't do the
51227c478bd9Sstevel@tonic-gate 	 * right access checking.  So, we'll fail the request.
51237c478bd9Sstevel@tonic-gate 	 */
51247c478bd9Sstevel@tonic-gate 	if (error) {
51257c478bd9Sstevel@tonic-gate 		*cs->statusp = resp->status = puterrno4(error);
51267c478bd9Sstevel@tonic-gate 		goto out;
51277c478bd9Sstevel@tonic-gate 	}
51287c478bd9Sstevel@tonic-gate 
51297c478bd9Sstevel@tonic-gate 	if (rdonly4(cs->exi, cs->vp, req)) {
51307c478bd9Sstevel@tonic-gate 		*cs->statusp = resp->status = NFS4ERR_ROFS;
51317c478bd9Sstevel@tonic-gate 		goto out;
51327c478bd9Sstevel@tonic-gate 	}
51337c478bd9Sstevel@tonic-gate 
51347c478bd9Sstevel@tonic-gate 	if (vp->v_type != VREG) {
51357c478bd9Sstevel@tonic-gate 		*cs->statusp = resp->status =
51367c478bd9Sstevel@tonic-gate 			((vp->v_type == VDIR) ? NFS4ERR_ISDIR : NFS4ERR_INVAL);
51377c478bd9Sstevel@tonic-gate 		goto out;
51387c478bd9Sstevel@tonic-gate 	}
51397c478bd9Sstevel@tonic-gate 
51407c478bd9Sstevel@tonic-gate 	if (crgetuid(cr) != bva.va_uid &&
51417c478bd9Sstevel@tonic-gate 	    (error = VOP_ACCESS(vp, VWRITE, 0, cr))) {
51427c478bd9Sstevel@tonic-gate 		*cs->statusp = resp->status = puterrno4(error);
51437c478bd9Sstevel@tonic-gate 		goto out;
51447c478bd9Sstevel@tonic-gate 	}
51457c478bd9Sstevel@tonic-gate 
51467c478bd9Sstevel@tonic-gate 	if (MANDLOCK(vp, bva.va_mode)) {
51477c478bd9Sstevel@tonic-gate 		*cs->statusp = resp->status = NFS4ERR_ACCESS;
51487c478bd9Sstevel@tonic-gate 		goto out;
51497c478bd9Sstevel@tonic-gate 	}
51507c478bd9Sstevel@tonic-gate 
51517c478bd9Sstevel@tonic-gate 	if (args->data_len == 0) {
51527c478bd9Sstevel@tonic-gate 		*cs->statusp = resp->status = NFS4_OK;
51537c478bd9Sstevel@tonic-gate 		resp->count = 0;
51547c478bd9Sstevel@tonic-gate 		resp->committed = args->stable;
51557c478bd9Sstevel@tonic-gate 		resp->writeverf = Write4verf;
51567c478bd9Sstevel@tonic-gate 		goto out;
51577c478bd9Sstevel@tonic-gate 	}
51587c478bd9Sstevel@tonic-gate 
51597c478bd9Sstevel@tonic-gate 	if (args->mblk != NULL) {
51607c478bd9Sstevel@tonic-gate 		mblk_t *m;
51617c478bd9Sstevel@tonic-gate 		uint_t bytes, round_len;
51627c478bd9Sstevel@tonic-gate 
51637c478bd9Sstevel@tonic-gate 		iovcnt = 0;
51647c478bd9Sstevel@tonic-gate 		bytes = 0;
51657c478bd9Sstevel@tonic-gate 		round_len = roundup(args->data_len, BYTES_PER_XDR_UNIT);
51667c478bd9Sstevel@tonic-gate 		for (m = args->mblk;
51677c478bd9Sstevel@tonic-gate 		    m != NULL && bytes < round_len;
51687c478bd9Sstevel@tonic-gate 		    m = m->b_cont) {
51697c478bd9Sstevel@tonic-gate 			iovcnt++;
51707c478bd9Sstevel@tonic-gate 			bytes += MBLKL(m);
51717c478bd9Sstevel@tonic-gate 		}
51727c478bd9Sstevel@tonic-gate #ifdef DEBUG
51737c478bd9Sstevel@tonic-gate 		/* should have ended on an mblk boundary */
51747c478bd9Sstevel@tonic-gate 		if (bytes != round_len) {
51757c478bd9Sstevel@tonic-gate 			printf("bytes=0x%x, round_len=0x%x, req len=0x%x\n",
51767c478bd9Sstevel@tonic-gate 			    bytes, round_len, args->data_len);
51777c478bd9Sstevel@tonic-gate 			printf("args=%p, args->mblk=%p, m=%p", (void *)args,
51787c478bd9Sstevel@tonic-gate 			    (void *)args->mblk, (void *)m);
51797c478bd9Sstevel@tonic-gate 			ASSERT(bytes == round_len);
51807c478bd9Sstevel@tonic-gate 		}
51817c478bd9Sstevel@tonic-gate #endif
51827c478bd9Sstevel@tonic-gate 		if (iovcnt <= MAX_IOVECS) {
51837c478bd9Sstevel@tonic-gate 			iovp = iov;
51847c478bd9Sstevel@tonic-gate 		} else {
51857c478bd9Sstevel@tonic-gate 			iovp = kmem_alloc(sizeof (*iovp) * iovcnt, KM_SLEEP);
51867c478bd9Sstevel@tonic-gate 		}
51877c478bd9Sstevel@tonic-gate 		mblk_to_iov(args->mblk, iovcnt, iovp);
51887c478bd9Sstevel@tonic-gate 	} else {
51897c478bd9Sstevel@tonic-gate 		iovcnt = 1;
51907c478bd9Sstevel@tonic-gate 		iovp = iov;
51917c478bd9Sstevel@tonic-gate 		iovp->iov_base = args->data_val;
51927c478bd9Sstevel@tonic-gate 		iovp->iov_len = args->data_len;
51937c478bd9Sstevel@tonic-gate 	}
51947c478bd9Sstevel@tonic-gate 
51957c478bd9Sstevel@tonic-gate 	uio.uio_iov = iovp;
51967c478bd9Sstevel@tonic-gate 	uio.uio_iovcnt = iovcnt;
51977c478bd9Sstevel@tonic-gate 
51987c478bd9Sstevel@tonic-gate 	uio.uio_segflg = UIO_SYSSPACE;
51997c478bd9Sstevel@tonic-gate 	uio.uio_extflg = UIO_COPY_DEFAULT;
52007c478bd9Sstevel@tonic-gate 	uio.uio_loffset = args->offset;
52017c478bd9Sstevel@tonic-gate 	uio.uio_resid = args->data_len;
52027c478bd9Sstevel@tonic-gate 	uio.uio_llimit = curproc->p_fsz_ctl;
52037c478bd9Sstevel@tonic-gate 	rlimit = uio.uio_llimit - args->offset;
52047c478bd9Sstevel@tonic-gate 	if (rlimit < (u_offset_t)uio.uio_resid)
52057c478bd9Sstevel@tonic-gate 		uio.uio_resid = (int)rlimit;
52067c478bd9Sstevel@tonic-gate 
52077c478bd9Sstevel@tonic-gate 	if (args->stable == UNSTABLE4)
52087c478bd9Sstevel@tonic-gate 		ioflag = 0;
52097c478bd9Sstevel@tonic-gate 	else if (args->stable == FILE_SYNC4)
52107c478bd9Sstevel@tonic-gate 		ioflag = FSYNC;
52117c478bd9Sstevel@tonic-gate 	else if (args->stable == DATA_SYNC4)
52127c478bd9Sstevel@tonic-gate 		ioflag = FDSYNC;
52137c478bd9Sstevel@tonic-gate 	else {
52147c478bd9Sstevel@tonic-gate 		if (iovp != iov)
52157c478bd9Sstevel@tonic-gate 			kmem_free(iovp, sizeof (*iovp) * iovcnt);
52167c478bd9Sstevel@tonic-gate 		*cs->statusp = resp->status = NFS4ERR_INVAL;
52177c478bd9Sstevel@tonic-gate 		goto out;
52187c478bd9Sstevel@tonic-gate 	}
52197c478bd9Sstevel@tonic-gate 
52207c478bd9Sstevel@tonic-gate 	/*
52217c478bd9Sstevel@tonic-gate 	 * We're changing creds because VM may fault and we need
52227c478bd9Sstevel@tonic-gate 	 * the cred of the current thread to be used if quota
52237c478bd9Sstevel@tonic-gate 	 * checking is enabled.
52247c478bd9Sstevel@tonic-gate 	 */
52257c478bd9Sstevel@tonic-gate 	savecred = curthread->t_cred;
52267c478bd9Sstevel@tonic-gate 	curthread->t_cred = cr;
52277c478bd9Sstevel@tonic-gate 	error = do_io(FWRITE, vp, &uio, ioflag, cr);
52287c478bd9Sstevel@tonic-gate 	curthread->t_cred = savecred;
52297c478bd9Sstevel@tonic-gate 
52307c478bd9Sstevel@tonic-gate 	if (iovp != iov)
52317c478bd9Sstevel@tonic-gate 		kmem_free(iovp, sizeof (*iovp) * iovcnt);
52327c478bd9Sstevel@tonic-gate 
52337c478bd9Sstevel@tonic-gate 	if (error) {
52347c478bd9Sstevel@tonic-gate 		*cs->statusp = resp->status = puterrno4(error);
52357c478bd9Sstevel@tonic-gate 		goto out;
52367c478bd9Sstevel@tonic-gate 	}
52377c478bd9Sstevel@tonic-gate 
52387c478bd9Sstevel@tonic-gate 	*cs->statusp = resp->status = NFS4_OK;
52397c478bd9Sstevel@tonic-gate 	resp->count = args->data_len - uio.uio_resid;
52407c478bd9Sstevel@tonic-gate 
52417c478bd9Sstevel@tonic-gate 	if (ioflag == 0)
52427c478bd9Sstevel@tonic-gate 		resp->committed = UNSTABLE4;
52437c478bd9Sstevel@tonic-gate 	else
52447c478bd9Sstevel@tonic-gate 		resp->committed = FILE_SYNC4;
52457c478bd9Sstevel@tonic-gate 
52467c478bd9Sstevel@tonic-gate 	resp->writeverf = Write4verf;
52477c478bd9Sstevel@tonic-gate 
52487c478bd9Sstevel@tonic-gate out:
52497c478bd9Sstevel@tonic-gate 	if (in_crit)
52507c478bd9Sstevel@tonic-gate 		nbl_end_crit(vp);
52517c478bd9Sstevel@tonic-gate }
52527c478bd9Sstevel@tonic-gate 
52537c478bd9Sstevel@tonic-gate 
52547c478bd9Sstevel@tonic-gate /* XXX put in a header file */
52557c478bd9Sstevel@tonic-gate extern int	sec_svc_getcred(struct svc_req *, cred_t *,  caddr_t *, int *);
52567c478bd9Sstevel@tonic-gate 
52577c478bd9Sstevel@tonic-gate void
52587c478bd9Sstevel@tonic-gate rfs4_compound(COMPOUND4args *args, COMPOUND4res *resp, struct exportinfo *exi,
52597c478bd9Sstevel@tonic-gate 	struct svc_req *req, cred_t *cr)
52607c478bd9Sstevel@tonic-gate {
52617c478bd9Sstevel@tonic-gate 	uint_t i;
52627c478bd9Sstevel@tonic-gate 	struct compound_state cs;
52637c478bd9Sstevel@tonic-gate 
52647c478bd9Sstevel@tonic-gate 	rfs4_init_compound_state(&cs);
52657c478bd9Sstevel@tonic-gate 	/*
52667c478bd9Sstevel@tonic-gate 	 * Form a reply tag by copying over the reqeuest tag.
52677c478bd9Sstevel@tonic-gate 	 */
52687c478bd9Sstevel@tonic-gate 	resp->tag.utf8string_val =
52697c478bd9Sstevel@tonic-gate 				kmem_alloc(args->tag.utf8string_len, KM_SLEEP);
52707c478bd9Sstevel@tonic-gate 	resp->tag.utf8string_len = args->tag.utf8string_len;
52717c478bd9Sstevel@tonic-gate 	bcopy(args->tag.utf8string_val, resp->tag.utf8string_val,
52727c478bd9Sstevel@tonic-gate 					resp->tag.utf8string_len);
52737c478bd9Sstevel@tonic-gate 
52747c478bd9Sstevel@tonic-gate 	cs.statusp = &resp->status;
52757c478bd9Sstevel@tonic-gate 
52767c478bd9Sstevel@tonic-gate 	/*
52777c478bd9Sstevel@tonic-gate 	 * XXX for now, minorversion should be zero
52787c478bd9Sstevel@tonic-gate 	 */
52797c478bd9Sstevel@tonic-gate 	if (args->minorversion != NFS4_MINORVERSION) {
52807c478bd9Sstevel@tonic-gate 		resp->array_len = 0;
52817c478bd9Sstevel@tonic-gate 		resp->array = NULL;
52827c478bd9Sstevel@tonic-gate 		resp->status = NFS4ERR_MINOR_VERS_MISMATCH;
52837c478bd9Sstevel@tonic-gate 		return;
52847c478bd9Sstevel@tonic-gate 	}
52857c478bd9Sstevel@tonic-gate 
52867c478bd9Sstevel@tonic-gate 	resp->array_len = args->array_len;
52877c478bd9Sstevel@tonic-gate 	resp->array = kmem_zalloc(args->array_len * sizeof (nfs_resop4),
52887c478bd9Sstevel@tonic-gate 		KM_SLEEP);
52897c478bd9Sstevel@tonic-gate 
52907c478bd9Sstevel@tonic-gate 	ASSERT(exi == NULL);
52917c478bd9Sstevel@tonic-gate 	ASSERT(cr == NULL);
52927c478bd9Sstevel@tonic-gate 
52937c478bd9Sstevel@tonic-gate 	cr = crget();
52947c478bd9Sstevel@tonic-gate 	ASSERT(cr != NULL);
52957c478bd9Sstevel@tonic-gate 
52967c478bd9Sstevel@tonic-gate 	if (sec_svc_getcred(req, cr, &cs.principal, &cs.nfsflavor) == 0) {
52977c478bd9Sstevel@tonic-gate 		crfree(cr);
52987c478bd9Sstevel@tonic-gate 		return;
52997c478bd9Sstevel@tonic-gate 	}
53007c478bd9Sstevel@tonic-gate 
53017c478bd9Sstevel@tonic-gate 	cs.basecr = cr;
53027c478bd9Sstevel@tonic-gate 
53037c478bd9Sstevel@tonic-gate 	cs.req = req;
53047c478bd9Sstevel@tonic-gate 
53057c478bd9Sstevel@tonic-gate 	/*
53067c478bd9Sstevel@tonic-gate 	 * For now, NFS4 compound processing must be protected by
53077c478bd9Sstevel@tonic-gate 	 * exported_lock because it can access more than one exportinfo
53087c478bd9Sstevel@tonic-gate 	 * per compound and share/unshare can now change multiple
53097c478bd9Sstevel@tonic-gate 	 * exinfo structs.  The NFS2/3 code only refs 1 exportinfo
53107c478bd9Sstevel@tonic-gate 	 * per proc (excluding public exinfo), and exi_count design
53117c478bd9Sstevel@tonic-gate 	 * is sufficient to protect concurrent execution of NFS2/3
53127c478bd9Sstevel@tonic-gate 	 * ops along with unexport.  This lock will be removed as
53137c478bd9Sstevel@tonic-gate 	 * part of the NFSv4 phase 2 namespace redesign work.
53147c478bd9Sstevel@tonic-gate 	 */
53157c478bd9Sstevel@tonic-gate 	rw_enter(&exported_lock, RW_READER);
53167c478bd9Sstevel@tonic-gate 
53177c478bd9Sstevel@tonic-gate 	/*
53187c478bd9Sstevel@tonic-gate 	 * If this is the first compound we've seen, we need to start all
53197c478bd9Sstevel@tonic-gate 	 * new instances' grace periods.
53207c478bd9Sstevel@tonic-gate 	 */
53217c478bd9Sstevel@tonic-gate 	if (rfs4_seen_first_compound == 0) {
53227c478bd9Sstevel@tonic-gate 		rfs4_grace_start_new();
53237c478bd9Sstevel@tonic-gate 		/*
53247c478bd9Sstevel@tonic-gate 		 * This must be set after rfs4_grace_start_new(), otherwise
53257c478bd9Sstevel@tonic-gate 		 * another thread could proceed past here before the former
53267c478bd9Sstevel@tonic-gate 		 * is finished.
53277c478bd9Sstevel@tonic-gate 		 */
53287c478bd9Sstevel@tonic-gate 		rfs4_seen_first_compound = 1;
53297c478bd9Sstevel@tonic-gate 	}
53307c478bd9Sstevel@tonic-gate 
53317c478bd9Sstevel@tonic-gate 	for (i = 0; i < args->array_len && cs.cont; i++) {
53327c478bd9Sstevel@tonic-gate 		nfs_argop4 *argop;
53337c478bd9Sstevel@tonic-gate 		nfs_resop4 *resop;
53347c478bd9Sstevel@tonic-gate 		uint_t op;
53357c478bd9Sstevel@tonic-gate 
53367c478bd9Sstevel@tonic-gate 		argop = &args->array[i];
53377c478bd9Sstevel@tonic-gate 		resop = &resp->array[i];
53387c478bd9Sstevel@tonic-gate 		resop->resop = argop->argop;
53397c478bd9Sstevel@tonic-gate 		op = (uint_t)resop->resop;
53407c478bd9Sstevel@tonic-gate 
53417c478bd9Sstevel@tonic-gate 		if (op < rfsv4disp_cnt) {
53427c478bd9Sstevel@tonic-gate 			/*
53437c478bd9Sstevel@tonic-gate 			 * Count the individual ops here; NULL and COMPOUND
53447c478bd9Sstevel@tonic-gate 			 * are counted in common_dispatch()
53457c478bd9Sstevel@tonic-gate 			 */
53467c478bd9Sstevel@tonic-gate 			rfsproccnt_v4_ptr[op].value.ui64++;
53477c478bd9Sstevel@tonic-gate 
53487c478bd9Sstevel@tonic-gate 			NFS4_DEBUG(rfs4_debug > 1,
53497c478bd9Sstevel@tonic-gate 				(CE_NOTE, "Executing %s", rfs4_op_string[op]));
53507c478bd9Sstevel@tonic-gate 			(*rfsv4disptab[op].dis_proc)(argop, resop, req, &cs);
53517c478bd9Sstevel@tonic-gate 			NFS4_DEBUG(rfs4_debug > 1,
53527c478bd9Sstevel@tonic-gate 				(CE_NOTE, "%s returned %d",
53537c478bd9Sstevel@tonic-gate 				rfs4_op_string[op], *cs.statusp));
53547c478bd9Sstevel@tonic-gate 			if (*cs.statusp != NFS4_OK)
53557c478bd9Sstevel@tonic-gate 				cs.cont = FALSE;
53567c478bd9Sstevel@tonic-gate 		} else {
53577c478bd9Sstevel@tonic-gate 			/*
53587c478bd9Sstevel@tonic-gate 			 * This is effectively dead code since XDR code
53597c478bd9Sstevel@tonic-gate 			 * will have already returned BADXDR if op doesn't
53607c478bd9Sstevel@tonic-gate 			 * decode to legal value.  This only done for a
53617c478bd9Sstevel@tonic-gate 			 * day when XDR code doesn't verify v4 opcodes.
53627c478bd9Sstevel@tonic-gate 			 */
53637c478bd9Sstevel@tonic-gate 			op = OP_ILLEGAL;
53647c478bd9Sstevel@tonic-gate 			rfsproccnt_v4_ptr[OP_ILLEGAL_IDX].value.ui64++;
53657c478bd9Sstevel@tonic-gate 
53667c478bd9Sstevel@tonic-gate 			rfs4_op_illegal(argop, resop, req, &cs);
53677c478bd9Sstevel@tonic-gate 			cs.cont = FALSE;
53687c478bd9Sstevel@tonic-gate 		}
53697c478bd9Sstevel@tonic-gate 
53707c478bd9Sstevel@tonic-gate 		/*
53717c478bd9Sstevel@tonic-gate 		 * If not at last op, and if we are to stop, then
53727c478bd9Sstevel@tonic-gate 		 * compact the results array.
53737c478bd9Sstevel@tonic-gate 		 */
53747c478bd9Sstevel@tonic-gate 		if ((i + 1) < args->array_len && !cs.cont) {
53757c478bd9Sstevel@tonic-gate 			nfs_resop4 *new_res = kmem_alloc(
53767c478bd9Sstevel@tonic-gate 				(i+1) * sizeof (nfs_resop4), KM_SLEEP);
53777c478bd9Sstevel@tonic-gate 			bcopy(resp->array,
53787c478bd9Sstevel@tonic-gate 				new_res, (i+1) * sizeof (nfs_resop4));
53797c478bd9Sstevel@tonic-gate 			kmem_free(resp->array,
53807c478bd9Sstevel@tonic-gate 				args->array_len * sizeof (nfs_resop4));
53817c478bd9Sstevel@tonic-gate 
53827c478bd9Sstevel@tonic-gate 			resp->array_len =  i + 1;
53837c478bd9Sstevel@tonic-gate 			resp->array = new_res;
53847c478bd9Sstevel@tonic-gate 		}
53857c478bd9Sstevel@tonic-gate 	}
53867c478bd9Sstevel@tonic-gate 
53877c478bd9Sstevel@tonic-gate 	rw_exit(&exported_lock);
53887c478bd9Sstevel@tonic-gate 
53897c478bd9Sstevel@tonic-gate 	if (cs.vp)
53907c478bd9Sstevel@tonic-gate 		VN_RELE(cs.vp);
53917c478bd9Sstevel@tonic-gate 	if (cs.saved_vp)
53927c478bd9Sstevel@tonic-gate 		VN_RELE(cs.saved_vp);
53937c478bd9Sstevel@tonic-gate 	if (cs.saved_fh.nfs_fh4_val)
53947c478bd9Sstevel@tonic-gate 		kmem_free(cs.saved_fh.nfs_fh4_val, NFS4_FHSIZE);
53957c478bd9Sstevel@tonic-gate 
53967c478bd9Sstevel@tonic-gate 	if (cs.basecr)
53977c478bd9Sstevel@tonic-gate 		crfree(cs.basecr);
53987c478bd9Sstevel@tonic-gate 	if (cs.cr)
53997c478bd9Sstevel@tonic-gate 		crfree(cs.cr);
540045916cd2Sjpk 	/*
540145916cd2Sjpk 	 * done with this compound request, free the label
540245916cd2Sjpk 	 */
540345916cd2Sjpk 
540445916cd2Sjpk 	if (req->rq_label != NULL) {
540545916cd2Sjpk 		kmem_free(req->rq_label, sizeof (bslabel_t));
540645916cd2Sjpk 		req->rq_label = NULL;
540745916cd2Sjpk 	}
54087c478bd9Sstevel@tonic-gate }
54097c478bd9Sstevel@tonic-gate 
54107c478bd9Sstevel@tonic-gate /*
54117c478bd9Sstevel@tonic-gate  * XXX because of what appears to be duplicate calls to rfs4_compound_free
54127c478bd9Sstevel@tonic-gate  * XXX zero out the tag and array values. Need to investigate why the
54137c478bd9Sstevel@tonic-gate  * XXX calls occur, but at least prevent the panic for now.
54147c478bd9Sstevel@tonic-gate  */
54157c478bd9Sstevel@tonic-gate void
54167c478bd9Sstevel@tonic-gate rfs4_compound_free(COMPOUND4res *resp)
54177c478bd9Sstevel@tonic-gate {
54187c478bd9Sstevel@tonic-gate 	uint_t i;
54197c478bd9Sstevel@tonic-gate 
54207c478bd9Sstevel@tonic-gate 	if (resp->tag.utf8string_val) {
54217c478bd9Sstevel@tonic-gate 		UTF8STRING_FREE(resp->tag)
54227c478bd9Sstevel@tonic-gate 	}
54237c478bd9Sstevel@tonic-gate 
54247c478bd9Sstevel@tonic-gate 	for (i = 0; i < resp->array_len; i++) {
54257c478bd9Sstevel@tonic-gate 		nfs_resop4 *resop;
54267c478bd9Sstevel@tonic-gate 		uint_t op;
54277c478bd9Sstevel@tonic-gate 
54287c478bd9Sstevel@tonic-gate 		resop = &resp->array[i];
54297c478bd9Sstevel@tonic-gate 		op = (uint_t)resop->resop;
54307c478bd9Sstevel@tonic-gate 		if (op < rfsv4disp_cnt) {
54317c478bd9Sstevel@tonic-gate 			(*rfsv4disptab[op].dis_resfree)(resop);
54327c478bd9Sstevel@tonic-gate 		}
54337c478bd9Sstevel@tonic-gate 	}
54347c478bd9Sstevel@tonic-gate 	if (resp->array != NULL) {
54357c478bd9Sstevel@tonic-gate 		kmem_free(resp->array, resp->array_len * sizeof (nfs_resop4));
54367c478bd9Sstevel@tonic-gate 	}
54377c478bd9Sstevel@tonic-gate }
54387c478bd9Sstevel@tonic-gate 
54397c478bd9Sstevel@tonic-gate /*
54407c478bd9Sstevel@tonic-gate  * Process the value of the compound request rpc flags, as a bit-AND
54417c478bd9Sstevel@tonic-gate  * of the individual per-op flags (idempotent, allowork, publicfh_ok)
54427c478bd9Sstevel@tonic-gate  */
54437c478bd9Sstevel@tonic-gate void
54447c478bd9Sstevel@tonic-gate rfs4_compound_flagproc(COMPOUND4args *args, int *flagp)
54457c478bd9Sstevel@tonic-gate {
54467c478bd9Sstevel@tonic-gate 	int i;
54477c478bd9Sstevel@tonic-gate 	int flag = RPC_ALL;
54487c478bd9Sstevel@tonic-gate 
54497c478bd9Sstevel@tonic-gate 	for (i = 0; flag && i < args->array_len; i++) {
54507c478bd9Sstevel@tonic-gate 		uint_t op;
54517c478bd9Sstevel@tonic-gate 
54527c478bd9Sstevel@tonic-gate 		op = (uint_t)args->array[i].argop;
54537c478bd9Sstevel@tonic-gate 
54547c478bd9Sstevel@tonic-gate 		if (op < rfsv4disp_cnt)
54557c478bd9Sstevel@tonic-gate 			flag &= rfsv4disptab[op].dis_flags;
54567c478bd9Sstevel@tonic-gate 		else
54577c478bd9Sstevel@tonic-gate 			flag = 0;
54587c478bd9Sstevel@tonic-gate 	}
54597c478bd9Sstevel@tonic-gate 	*flagp = flag;
54607c478bd9Sstevel@tonic-gate }
54617c478bd9Sstevel@tonic-gate 
54627c478bd9Sstevel@tonic-gate nfsstat4
54637c478bd9Sstevel@tonic-gate rfs4_client_sysid(rfs4_client_t *cp, sysid_t *sp)
54647c478bd9Sstevel@tonic-gate {
54657c478bd9Sstevel@tonic-gate 	nfsstat4 e;
54667c478bd9Sstevel@tonic-gate 
54677c478bd9Sstevel@tonic-gate 	rfs4_dbe_lock(cp->dbe);
54687c478bd9Sstevel@tonic-gate 
54697c478bd9Sstevel@tonic-gate 	if (cp->sysidt != LM_NOSYSID) {
54707c478bd9Sstevel@tonic-gate 		*sp = cp->sysidt;
54717c478bd9Sstevel@tonic-gate 		e = NFS4_OK;
54727c478bd9Sstevel@tonic-gate 
54737c478bd9Sstevel@tonic-gate 	} else if ((cp->sysidt = lm_alloc_sysidt()) != LM_NOSYSID) {
54747c478bd9Sstevel@tonic-gate 		*sp = cp->sysidt;
54757c478bd9Sstevel@tonic-gate 		e = NFS4_OK;
54767c478bd9Sstevel@tonic-gate 
54777c478bd9Sstevel@tonic-gate 		NFS4_DEBUG(rfs4_debug, (CE_NOTE,
54787c478bd9Sstevel@tonic-gate 			"rfs4_client_sysid: allocated 0x%x\n", *sp));
54797c478bd9Sstevel@tonic-gate 	} else
54807c478bd9Sstevel@tonic-gate 		e = NFS4ERR_DELAY;
54817c478bd9Sstevel@tonic-gate 
54827c478bd9Sstevel@tonic-gate 	rfs4_dbe_unlock(cp->dbe);
54837c478bd9Sstevel@tonic-gate 	return (e);
54847c478bd9Sstevel@tonic-gate }
54857c478bd9Sstevel@tonic-gate 
54867c478bd9Sstevel@tonic-gate #if defined(DEBUG) && ! defined(lint)
54877c478bd9Sstevel@tonic-gate static void lock_print(char *str, int operation, struct flock64 *flk)
54887c478bd9Sstevel@tonic-gate {
54897c478bd9Sstevel@tonic-gate 	char *op, *type;
54907c478bd9Sstevel@tonic-gate 
54917c478bd9Sstevel@tonic-gate 	switch (operation) {
54927c478bd9Sstevel@tonic-gate 	case F_GETLK: op = "F_GETLK";
54937c478bd9Sstevel@tonic-gate 		break;
54947c478bd9Sstevel@tonic-gate 	case F_SETLK: op = "F_SETLK";
54957c478bd9Sstevel@tonic-gate 		break;
54967c478bd9Sstevel@tonic-gate 	default: op = "F_UNKNOWN";
54977c478bd9Sstevel@tonic-gate 		break;
54987c478bd9Sstevel@tonic-gate 	}
54997c478bd9Sstevel@tonic-gate 	switch (flk->l_type) {
55007c478bd9Sstevel@tonic-gate 	case F_UNLCK: type = "F_UNLCK";
55017c478bd9Sstevel@tonic-gate 		break;
55027c478bd9Sstevel@tonic-gate 	case F_RDLCK: type = "F_RDLCK";
55037c478bd9Sstevel@tonic-gate 		break;
55047c478bd9Sstevel@tonic-gate 	case F_WRLCK: type = "F_WRLCK";
55057c478bd9Sstevel@tonic-gate 		break;
55067c478bd9Sstevel@tonic-gate 	default: type = "F_UNKNOWN";
55077c478bd9Sstevel@tonic-gate 		break;
55087c478bd9Sstevel@tonic-gate 	}
55097c478bd9Sstevel@tonic-gate 
55107c478bd9Sstevel@tonic-gate 	ASSERT(flk->l_whence == 0);
55117c478bd9Sstevel@tonic-gate 	cmn_err(CE_NOTE, "%s:  %s, type = %s, off = %llx len = %llx pid = %d",
55127c478bd9Sstevel@tonic-gate 		str, op, type,
55137c478bd9Sstevel@tonic-gate 		(longlong_t)flk->l_start,
55147c478bd9Sstevel@tonic-gate 		flk->l_len ? (longlong_t)flk->l_len : ~0LL,
55157c478bd9Sstevel@tonic-gate 		flk->l_pid);
55167c478bd9Sstevel@tonic-gate }
55177c478bd9Sstevel@tonic-gate 
55187c478bd9Sstevel@tonic-gate #define	LOCK_PRINT(d, s, t, f) if (d) lock_print(s, t, f)
55197c478bd9Sstevel@tonic-gate #else
55207c478bd9Sstevel@tonic-gate #define	LOCK_PRINT(d, s, t, f)
55217c478bd9Sstevel@tonic-gate #endif
55227c478bd9Sstevel@tonic-gate 
55237c478bd9Sstevel@tonic-gate /*ARGSUSED*/
55247c478bd9Sstevel@tonic-gate static bool_t
55257c478bd9Sstevel@tonic-gate creds_ok(cred_set_t cr_set, struct svc_req *req, struct compound_state *cs)
55267c478bd9Sstevel@tonic-gate {
55277c478bd9Sstevel@tonic-gate 	return (TRUE);
55287c478bd9Sstevel@tonic-gate }
55297c478bd9Sstevel@tonic-gate 
55307c478bd9Sstevel@tonic-gate /*
55317c478bd9Sstevel@tonic-gate  * Look up the pathname using the vp in cs as the directory vnode.
55327c478bd9Sstevel@tonic-gate  * cs->vp will be the vnode for the file on success
55337c478bd9Sstevel@tonic-gate  */
55347c478bd9Sstevel@tonic-gate 
55357c478bd9Sstevel@tonic-gate static nfsstat4
55367c478bd9Sstevel@tonic-gate rfs4_lookup(component4 *component, struct svc_req *req,
55377c478bd9Sstevel@tonic-gate 	    struct compound_state *cs)
55387c478bd9Sstevel@tonic-gate {
55397c478bd9Sstevel@tonic-gate 	char *nm;
55407c478bd9Sstevel@tonic-gate 	uint32_t len;
55417c478bd9Sstevel@tonic-gate 	nfsstat4 status;
55427c478bd9Sstevel@tonic-gate 
55437c478bd9Sstevel@tonic-gate 	if (cs->vp == NULL) {
55447c478bd9Sstevel@tonic-gate 		return (NFS4ERR_NOFILEHANDLE);
55457c478bd9Sstevel@tonic-gate 	}
55467c478bd9Sstevel@tonic-gate 	if (cs->vp->v_type != VDIR) {
55477c478bd9Sstevel@tonic-gate 		return (NFS4ERR_NOTDIR);
55487c478bd9Sstevel@tonic-gate 	}
55497c478bd9Sstevel@tonic-gate 
55507c478bd9Sstevel@tonic-gate 	if (!utf8_dir_verify(component))
55517c478bd9Sstevel@tonic-gate 		return (NFS4ERR_INVAL);
55527c478bd9Sstevel@tonic-gate 
55537c478bd9Sstevel@tonic-gate 	nm = utf8_to_fn(component, &len, NULL);
55547c478bd9Sstevel@tonic-gate 	if (nm == NULL) {
55557c478bd9Sstevel@tonic-gate 		return (NFS4ERR_INVAL);
55567c478bd9Sstevel@tonic-gate 	}
55577c478bd9Sstevel@tonic-gate 
55587c478bd9Sstevel@tonic-gate 	if (len > MAXNAMELEN) {
55597c478bd9Sstevel@tonic-gate 		kmem_free(nm, len);
55607c478bd9Sstevel@tonic-gate 		return (NFS4ERR_NAMETOOLONG);
55617c478bd9Sstevel@tonic-gate 	}
55627c478bd9Sstevel@tonic-gate 
55637c478bd9Sstevel@tonic-gate 	status = do_rfs4_op_lookup(nm, len, req, cs);
55647c478bd9Sstevel@tonic-gate 
55657c478bd9Sstevel@tonic-gate 	kmem_free(nm, len);
55667c478bd9Sstevel@tonic-gate 
55677c478bd9Sstevel@tonic-gate 	return (status);
55687c478bd9Sstevel@tonic-gate }
55697c478bd9Sstevel@tonic-gate 
55707c478bd9Sstevel@tonic-gate static nfsstat4
55717c478bd9Sstevel@tonic-gate rfs4_lookupfile(component4 *component, struct svc_req *req,
55727c478bd9Sstevel@tonic-gate 		struct compound_state *cs, uint32_t access,
55737c478bd9Sstevel@tonic-gate 		change_info4 *cinfo)
55747c478bd9Sstevel@tonic-gate {
55757c478bd9Sstevel@tonic-gate 	nfsstat4 status;
55767c478bd9Sstevel@tonic-gate 	vnode_t *dvp = cs->vp;
55777c478bd9Sstevel@tonic-gate 	vattr_t bva, ava, fva;
55787c478bd9Sstevel@tonic-gate 	int error;
55797c478bd9Sstevel@tonic-gate 
55807c478bd9Sstevel@tonic-gate 	/* Get "before" change value */
55817c478bd9Sstevel@tonic-gate 	bva.va_mask = AT_CTIME|AT_SEQ;
55827c478bd9Sstevel@tonic-gate 	error = VOP_GETATTR(dvp, &bva, 0, cs->cr);
55837c478bd9Sstevel@tonic-gate 	if (error)
55847c478bd9Sstevel@tonic-gate 		return (puterrno4(error));
55857c478bd9Sstevel@tonic-gate 
55867c478bd9Sstevel@tonic-gate 	/* rfs4_lookup may VN_RELE directory */
55877c478bd9Sstevel@tonic-gate 	VN_HOLD(dvp);
55887c478bd9Sstevel@tonic-gate 
55897c478bd9Sstevel@tonic-gate 	status = rfs4_lookup(component, req, cs);
55907c478bd9Sstevel@tonic-gate 	if (status != NFS4_OK) {
55917c478bd9Sstevel@tonic-gate 		VN_RELE(dvp);
55927c478bd9Sstevel@tonic-gate 		return (status);
55937c478bd9Sstevel@tonic-gate 	}
55947c478bd9Sstevel@tonic-gate 
55957c478bd9Sstevel@tonic-gate 	/*
55967c478bd9Sstevel@tonic-gate 	 * Get "after" change value, if it fails, simply return the
55977c478bd9Sstevel@tonic-gate 	 * before value.
55987c478bd9Sstevel@tonic-gate 	 */
55997c478bd9Sstevel@tonic-gate 	ava.va_mask = AT_CTIME|AT_SEQ;
56007c478bd9Sstevel@tonic-gate 	if (VOP_GETATTR(dvp, &ava, 0, cs->cr)) {
56017c478bd9Sstevel@tonic-gate 		ava.va_ctime = bva.va_ctime;
56027c478bd9Sstevel@tonic-gate 		ava.va_seq = 0;
56037c478bd9Sstevel@tonic-gate 	}
56047c478bd9Sstevel@tonic-gate 	VN_RELE(dvp);
56057c478bd9Sstevel@tonic-gate 
56067c478bd9Sstevel@tonic-gate 	/*
56077c478bd9Sstevel@tonic-gate 	 * Validate the file is a file
56087c478bd9Sstevel@tonic-gate 	 */
56097c478bd9Sstevel@tonic-gate 	fva.va_mask = AT_TYPE|AT_MODE;
56107c478bd9Sstevel@tonic-gate 	error = VOP_GETATTR(cs->vp, &fva, 0, cs->cr);
56117c478bd9Sstevel@tonic-gate 	if (error)
56127c478bd9Sstevel@tonic-gate 		return (puterrno4(error));
56137c478bd9Sstevel@tonic-gate 
56147c478bd9Sstevel@tonic-gate 	if (fva.va_type != VREG) {
56157c478bd9Sstevel@tonic-gate 		if (fva.va_type == VDIR)
56167c478bd9Sstevel@tonic-gate 			return (NFS4ERR_ISDIR);
56177c478bd9Sstevel@tonic-gate 		if (fva.va_type == VLNK)
56187c478bd9Sstevel@tonic-gate 			return (NFS4ERR_SYMLINK);
56197c478bd9Sstevel@tonic-gate 		return (NFS4ERR_INVAL);
56207c478bd9Sstevel@tonic-gate 	}
56217c478bd9Sstevel@tonic-gate 
56227c478bd9Sstevel@tonic-gate 	NFS4_SET_FATTR4_CHANGE(cinfo->before, bva.va_ctime);
56237c478bd9Sstevel@tonic-gate 	NFS4_SET_FATTR4_CHANGE(cinfo->after, ava.va_ctime);
56247c478bd9Sstevel@tonic-gate 
56257c478bd9Sstevel@tonic-gate 	/*
56267c478bd9Sstevel@tonic-gate 	 * It is undefined if VOP_LOOKUP will change va_seq, so
56277c478bd9Sstevel@tonic-gate 	 * cinfo.atomic = TRUE only if we have
56287c478bd9Sstevel@tonic-gate 	 * non-zero va_seq's, and they have not changed.
56297c478bd9Sstevel@tonic-gate 	 */
56307c478bd9Sstevel@tonic-gate 	if (bva.va_seq && ava.va_seq && ava.va_seq == bva.va_seq)
56317c478bd9Sstevel@tonic-gate 		cinfo->atomic = TRUE;
56327c478bd9Sstevel@tonic-gate 	else
56337c478bd9Sstevel@tonic-gate 		cinfo->atomic = FALSE;
56347c478bd9Sstevel@tonic-gate 
56357c478bd9Sstevel@tonic-gate 	/* Check for mandatory locking */
56367c478bd9Sstevel@tonic-gate 	cs->mandlock = MANDLOCK(cs->vp, fva.va_mode);
56377c478bd9Sstevel@tonic-gate 	return (check_open_access(access, cs, req));
56387c478bd9Sstevel@tonic-gate }
56397c478bd9Sstevel@tonic-gate 
56407c478bd9Sstevel@tonic-gate static nfsstat4
56417c478bd9Sstevel@tonic-gate create_vnode(vnode_t *dvp, char *nm,  vattr_t *vap, createmode4 mode,
56427c478bd9Sstevel@tonic-gate 	    timespec32_t *mtime, cred_t *cr, vnode_t **vpp, bool_t *created)
56437c478bd9Sstevel@tonic-gate {
56447c478bd9Sstevel@tonic-gate 	int error;
56457c478bd9Sstevel@tonic-gate 	nfsstat4 status = NFS4_OK;
56467c478bd9Sstevel@tonic-gate 	vattr_t va;
56477c478bd9Sstevel@tonic-gate 
56487c478bd9Sstevel@tonic-gate tryagain:
56497c478bd9Sstevel@tonic-gate 
56507c478bd9Sstevel@tonic-gate 	/*
56517c478bd9Sstevel@tonic-gate 	 * The file open mode used is VWRITE.  If the client needs
56527c478bd9Sstevel@tonic-gate 	 * some other semantic, then it should do the access checking
56537c478bd9Sstevel@tonic-gate 	 * itself.  It would have been nice to have the file open mode
56547c478bd9Sstevel@tonic-gate 	 * passed as part of the arguments.
56557c478bd9Sstevel@tonic-gate 	 */
56567c478bd9Sstevel@tonic-gate 
56577c478bd9Sstevel@tonic-gate 	*created = TRUE;
56587c478bd9Sstevel@tonic-gate 	error = VOP_CREATE(dvp, nm, vap, EXCL, VWRITE, vpp, cr, 0);
56597c478bd9Sstevel@tonic-gate 
56607c478bd9Sstevel@tonic-gate 	if (error) {
56617c478bd9Sstevel@tonic-gate 		*created = FALSE;
56627c478bd9Sstevel@tonic-gate 
56637c478bd9Sstevel@tonic-gate 		/*
56647c478bd9Sstevel@tonic-gate 		 * If we got something other than file already exists
56657c478bd9Sstevel@tonic-gate 		 * then just return this error.  Otherwise, we got
56667c478bd9Sstevel@tonic-gate 		 * EEXIST.  If we were doing a GUARDED create, then
56677c478bd9Sstevel@tonic-gate 		 * just return this error.  Otherwise, we need to
56687c478bd9Sstevel@tonic-gate 		 * make sure that this wasn't a duplicate of an
56697c478bd9Sstevel@tonic-gate 		 * exclusive create request.
56707c478bd9Sstevel@tonic-gate 		 *
56717c478bd9Sstevel@tonic-gate 		 * The assumption is made that a non-exclusive create
56727c478bd9Sstevel@tonic-gate 		 * request will never return EEXIST.
56737c478bd9Sstevel@tonic-gate 		 */
56747c478bd9Sstevel@tonic-gate 
56757c478bd9Sstevel@tonic-gate 		if (error != EEXIST || mode == GUARDED4) {
56767c478bd9Sstevel@tonic-gate 			status = puterrno4(error);
56777c478bd9Sstevel@tonic-gate 			return (status);
56787c478bd9Sstevel@tonic-gate 		}
56797c478bd9Sstevel@tonic-gate 		error = VOP_LOOKUP(dvp, nm, vpp, NULL, 0, NULL, cr);
56807c478bd9Sstevel@tonic-gate 
56817c478bd9Sstevel@tonic-gate 		if (error) {
56827c478bd9Sstevel@tonic-gate 			/*
56837c478bd9Sstevel@tonic-gate 			 * We couldn't find the file that we thought that
56847c478bd9Sstevel@tonic-gate 			 * we just created.  So, we'll just try creating
56857c478bd9Sstevel@tonic-gate 			 * it again.
56867c478bd9Sstevel@tonic-gate 			 */
56877c478bd9Sstevel@tonic-gate 			if (error == ENOENT)
56887c478bd9Sstevel@tonic-gate 				goto tryagain;
56897c478bd9Sstevel@tonic-gate 
56907c478bd9Sstevel@tonic-gate 			status = puterrno4(error);
56917c478bd9Sstevel@tonic-gate 			return (status);
56927c478bd9Sstevel@tonic-gate 		}
56937c478bd9Sstevel@tonic-gate 
56947c478bd9Sstevel@tonic-gate 		if (mode == UNCHECKED4) {
56957c478bd9Sstevel@tonic-gate 			/* existing object must be regular file */
56967c478bd9Sstevel@tonic-gate 			if ((*vpp)->v_type != VREG) {
56977c478bd9Sstevel@tonic-gate 				if ((*vpp)->v_type == VDIR)
56987c478bd9Sstevel@tonic-gate 					status = NFS4ERR_ISDIR;
56997c478bd9Sstevel@tonic-gate 				else if ((*vpp)->v_type == VLNK)
57007c478bd9Sstevel@tonic-gate 					status = NFS4ERR_SYMLINK;
57017c478bd9Sstevel@tonic-gate 				else
57027c478bd9Sstevel@tonic-gate 					status = NFS4ERR_INVAL;
57037c478bd9Sstevel@tonic-gate 				VN_RELE(*vpp);
57047c478bd9Sstevel@tonic-gate 				return (status);
57057c478bd9Sstevel@tonic-gate 			}
57067c478bd9Sstevel@tonic-gate 
57077c478bd9Sstevel@tonic-gate 			return (NFS4_OK);
57087c478bd9Sstevel@tonic-gate 		}
57097c478bd9Sstevel@tonic-gate 
57107c478bd9Sstevel@tonic-gate 		/* Check for duplicate request */
57117c478bd9Sstevel@tonic-gate 		ASSERT(mtime != 0);
57127c478bd9Sstevel@tonic-gate 		va.va_mask = AT_MTIME;
57137c478bd9Sstevel@tonic-gate 		error = VOP_GETATTR(*vpp, &va, 0, cr);
57147c478bd9Sstevel@tonic-gate 		if (!error) {
57157c478bd9Sstevel@tonic-gate 			/* We found the file */
57167c478bd9Sstevel@tonic-gate 			if (va.va_mtime.tv_sec != mtime->tv_sec ||
57177c478bd9Sstevel@tonic-gate 			    va.va_mtime.tv_nsec != mtime->tv_nsec) {
57187c478bd9Sstevel@tonic-gate 				/* but its not our creation */
57197c478bd9Sstevel@tonic-gate 				VN_RELE(*vpp);
57207c478bd9Sstevel@tonic-gate 				return (NFS4ERR_EXIST);
57217c478bd9Sstevel@tonic-gate 			}
57227c478bd9Sstevel@tonic-gate 			*created = TRUE; /* retrans of create == created */
57237c478bd9Sstevel@tonic-gate 			return (NFS4_OK);
57247c478bd9Sstevel@tonic-gate 		}
57257c478bd9Sstevel@tonic-gate 		VN_RELE(*vpp);
57267c478bd9Sstevel@tonic-gate 		return (NFS4ERR_EXIST);
57277c478bd9Sstevel@tonic-gate 	}
57287c478bd9Sstevel@tonic-gate 
57297c478bd9Sstevel@tonic-gate 	return (NFS4_OK);
57307c478bd9Sstevel@tonic-gate }
57317c478bd9Sstevel@tonic-gate 
57327c478bd9Sstevel@tonic-gate static nfsstat4
57337c478bd9Sstevel@tonic-gate check_open_access(uint32_t access,
57347c478bd9Sstevel@tonic-gate 		struct compound_state *cs, struct svc_req *req)
57357c478bd9Sstevel@tonic-gate {
57367c478bd9Sstevel@tonic-gate 	int error;
57377c478bd9Sstevel@tonic-gate 	vnode_t *vp;
57387c478bd9Sstevel@tonic-gate 	bool_t readonly;
57397c478bd9Sstevel@tonic-gate 	cred_t *cr = cs->cr;
57407c478bd9Sstevel@tonic-gate 
57417c478bd9Sstevel@tonic-gate 	/* For now we don't allow mandatory locking as per V2/V3 */
57427c478bd9Sstevel@tonic-gate 	if (cs->access == CS_ACCESS_DENIED || cs->mandlock) {
57437c478bd9Sstevel@tonic-gate 		return (NFS4ERR_ACCESS);
57447c478bd9Sstevel@tonic-gate 	}
57457c478bd9Sstevel@tonic-gate 
57467c478bd9Sstevel@tonic-gate 	vp = cs->vp;
57477c478bd9Sstevel@tonic-gate 	ASSERT(cr != NULL && vp->v_type == VREG);
57487c478bd9Sstevel@tonic-gate 
57497c478bd9Sstevel@tonic-gate 	/*
57507c478bd9Sstevel@tonic-gate 	 * If the file system is exported read only and we are trying
57517c478bd9Sstevel@tonic-gate 	 * to open for write, then return NFS4ERR_ROFS
57527c478bd9Sstevel@tonic-gate 	 */
57537c478bd9Sstevel@tonic-gate 
57547c478bd9Sstevel@tonic-gate 	readonly = rdonly4(cs->exi, cs->vp, req);
57557c478bd9Sstevel@tonic-gate 
57567c478bd9Sstevel@tonic-gate 	if ((access & OPEN4_SHARE_ACCESS_WRITE) && readonly)
57577c478bd9Sstevel@tonic-gate 		return (NFS4ERR_ROFS);
57587c478bd9Sstevel@tonic-gate 
57597c478bd9Sstevel@tonic-gate 	if (access & OPEN4_SHARE_ACCESS_READ) {
57607c478bd9Sstevel@tonic-gate 		if ((VOP_ACCESS(vp, VREAD, 0, cr) != 0) &&
57617c478bd9Sstevel@tonic-gate 		    (VOP_ACCESS(vp, VEXEC, 0, cr) != 0)) {
57627c478bd9Sstevel@tonic-gate 			return (NFS4ERR_ACCESS);
57637c478bd9Sstevel@tonic-gate 		}
57647c478bd9Sstevel@tonic-gate 	}
57657c478bd9Sstevel@tonic-gate 
57667c478bd9Sstevel@tonic-gate 	if (access & OPEN4_SHARE_ACCESS_WRITE) {
57677c478bd9Sstevel@tonic-gate 		error = VOP_ACCESS(vp, VWRITE, 0, cr);
57687c478bd9Sstevel@tonic-gate 		if (error)
57697c478bd9Sstevel@tonic-gate 			return (NFS4ERR_ACCESS);
57707c478bd9Sstevel@tonic-gate 	}
57717c478bd9Sstevel@tonic-gate 
57727c478bd9Sstevel@tonic-gate 	return (NFS4_OK);
57737c478bd9Sstevel@tonic-gate }
57747c478bd9Sstevel@tonic-gate 
57757c478bd9Sstevel@tonic-gate static nfsstat4
57767c478bd9Sstevel@tonic-gate rfs4_createfile(OPEN4args *args, struct svc_req *req, struct compound_state *cs,
57777c478bd9Sstevel@tonic-gate 		change_info4 *cinfo, bitmap4 *attrset, clientid4 clientid)
57787c478bd9Sstevel@tonic-gate {
57797c478bd9Sstevel@tonic-gate 	struct nfs4_svgetit_arg sarg;
57807c478bd9Sstevel@tonic-gate 	struct nfs4_ntov_table ntov;
57817c478bd9Sstevel@tonic-gate 
57827c478bd9Sstevel@tonic-gate 	bool_t ntov_table_init = FALSE;
57837c478bd9Sstevel@tonic-gate 	struct statvfs64 sb;
57847c478bd9Sstevel@tonic-gate 	nfsstat4 status;
57857c478bd9Sstevel@tonic-gate 	vnode_t *vp;
57867c478bd9Sstevel@tonic-gate 	vattr_t bva, ava, iva, cva, *vap;
57877c478bd9Sstevel@tonic-gate 	vnode_t *dvp;
57887c478bd9Sstevel@tonic-gate 	timespec32_t *mtime;
57897c478bd9Sstevel@tonic-gate 	char *nm = NULL;
57907c478bd9Sstevel@tonic-gate 	uint_t buflen;
57917c478bd9Sstevel@tonic-gate 	bool_t created;
57927c478bd9Sstevel@tonic-gate 	bool_t setsize = FALSE;
57937c478bd9Sstevel@tonic-gate 	len_t reqsize;
57947c478bd9Sstevel@tonic-gate 	int error;
57957c478bd9Sstevel@tonic-gate 	bool_t trunc;
57967c478bd9Sstevel@tonic-gate 	caller_context_t ct;
57977c478bd9Sstevel@tonic-gate 	component4 *component;
579845916cd2Sjpk 	bslabel_t *clabel;
57997c478bd9Sstevel@tonic-gate 
58007c478bd9Sstevel@tonic-gate 	sarg.sbp = &sb;
58017c478bd9Sstevel@tonic-gate 
58027c478bd9Sstevel@tonic-gate 	dvp = cs->vp;
58037c478bd9Sstevel@tonic-gate 
58047c478bd9Sstevel@tonic-gate 	/* Check if the file system is read only */
58057c478bd9Sstevel@tonic-gate 	if (rdonly4(cs->exi, dvp, req))
58067c478bd9Sstevel@tonic-gate 		return (NFS4ERR_ROFS);
58077c478bd9Sstevel@tonic-gate 
580845916cd2Sjpk 	/* check the label of including directory */
580945916cd2Sjpk 	if (is_system_labeled()) {
581045916cd2Sjpk 		ASSERT(req->rq_label != NULL);
581145916cd2Sjpk 		clabel = req->rq_label;
581245916cd2Sjpk 		DTRACE_PROBE2(tx__rfs4__log__info__opremove__clabel, char *,
581345916cd2Sjpk 		    "got client label from request(1)",
581445916cd2Sjpk 		    struct svc_req *, req);
581545916cd2Sjpk 		if (!blequal(&l_admin_low->tsl_label, clabel)) {
581645916cd2Sjpk 			if (!do_rfs4_label_check(clabel, dvp, EQUALITY_CHECK)) {
581745916cd2Sjpk 				return (NFS4ERR_ACCESS);
581845916cd2Sjpk 			}
581945916cd2Sjpk 		}
582045916cd2Sjpk 	}
582145916cd2Sjpk 
58227c478bd9Sstevel@tonic-gate 	/*
58237c478bd9Sstevel@tonic-gate 	 * Get the last component of path name in nm. cs will reference
58247c478bd9Sstevel@tonic-gate 	 * the including directory on success.
58257c478bd9Sstevel@tonic-gate 	 */
58267c478bd9Sstevel@tonic-gate 	component = &args->open_claim4_u.file;
58277c478bd9Sstevel@tonic-gate 	if (!utf8_dir_verify(component))
58287c478bd9Sstevel@tonic-gate 		return (NFS4ERR_INVAL);
58297c478bd9Sstevel@tonic-gate 
58307c478bd9Sstevel@tonic-gate 	nm = utf8_to_fn(component, &buflen, NULL);
58317c478bd9Sstevel@tonic-gate 
58327c478bd9Sstevel@tonic-gate 	if (nm == NULL)
58337c478bd9Sstevel@tonic-gate 		return (NFS4ERR_RESOURCE);
58347c478bd9Sstevel@tonic-gate 
58357c478bd9Sstevel@tonic-gate 	if (buflen > MAXNAMELEN) {
58367c478bd9Sstevel@tonic-gate 		kmem_free(nm, buflen);
58377c478bd9Sstevel@tonic-gate 		return (NFS4ERR_NAMETOOLONG);
58387c478bd9Sstevel@tonic-gate 	}
58397c478bd9Sstevel@tonic-gate 
58407c478bd9Sstevel@tonic-gate 	bva.va_mask = AT_TYPE|AT_CTIME|AT_SEQ;
58417c478bd9Sstevel@tonic-gate 	error = VOP_GETATTR(dvp, &bva, 0, cs->cr);
58427c478bd9Sstevel@tonic-gate 	if (error) {
58437c478bd9Sstevel@tonic-gate 		kmem_free(nm, buflen);
58447c478bd9Sstevel@tonic-gate 		return (puterrno4(error));
58457c478bd9Sstevel@tonic-gate 	}
58467c478bd9Sstevel@tonic-gate 
58477c478bd9Sstevel@tonic-gate 	if (bva.va_type != VDIR) {
58487c478bd9Sstevel@tonic-gate 		kmem_free(nm, buflen);
58497c478bd9Sstevel@tonic-gate 		return (NFS4ERR_NOTDIR);
58507c478bd9Sstevel@tonic-gate 	}
58517c478bd9Sstevel@tonic-gate 
58527c478bd9Sstevel@tonic-gate 	NFS4_SET_FATTR4_CHANGE(cinfo->before, bva.va_ctime)
58537c478bd9Sstevel@tonic-gate 
58547c478bd9Sstevel@tonic-gate 	switch (args->mode) {
58557c478bd9Sstevel@tonic-gate 	case GUARDED4:
58567c478bd9Sstevel@tonic-gate 		/*FALLTHROUGH*/
58577c478bd9Sstevel@tonic-gate 	case UNCHECKED4:
58587c478bd9Sstevel@tonic-gate 		nfs4_ntov_table_init(&ntov);
58597c478bd9Sstevel@tonic-gate 		ntov_table_init = TRUE;
58607c478bd9Sstevel@tonic-gate 
58617c478bd9Sstevel@tonic-gate 		*attrset = 0;
58627c478bd9Sstevel@tonic-gate 		status = do_rfs4_set_attrs(attrset,
58637c478bd9Sstevel@tonic-gate 					&args->createhow4_u.createattrs,
58647c478bd9Sstevel@tonic-gate 					cs, &sarg, &ntov, NFS4ATTR_SETIT);
58657c478bd9Sstevel@tonic-gate 
58667c478bd9Sstevel@tonic-gate 		if (status == NFS4_OK && (sarg.vap->va_mask & AT_TYPE) &&
58677c478bd9Sstevel@tonic-gate 		    sarg.vap->va_type != VREG) {
58687c478bd9Sstevel@tonic-gate 			if (sarg.vap->va_type == VDIR)
58697c478bd9Sstevel@tonic-gate 				status = NFS4ERR_ISDIR;
58707c478bd9Sstevel@tonic-gate 			else if (sarg.vap->va_type == VLNK)
58717c478bd9Sstevel@tonic-gate 				status = NFS4ERR_SYMLINK;
58727c478bd9Sstevel@tonic-gate 			else
58737c478bd9Sstevel@tonic-gate 				status = NFS4ERR_INVAL;
58747c478bd9Sstevel@tonic-gate 		}
58757c478bd9Sstevel@tonic-gate 
58767c478bd9Sstevel@tonic-gate 		if (status != NFS4_OK) {
58777c478bd9Sstevel@tonic-gate 			kmem_free(nm, buflen);
58787c478bd9Sstevel@tonic-gate 			nfs4_ntov_table_free(&ntov, &sarg);
58797c478bd9Sstevel@tonic-gate 			*attrset = 0;
58807c478bd9Sstevel@tonic-gate 			return (status);
58817c478bd9Sstevel@tonic-gate 		}
58827c478bd9Sstevel@tonic-gate 
58837c478bd9Sstevel@tonic-gate 		vap = sarg.vap;
58847c478bd9Sstevel@tonic-gate 		vap->va_type = VREG;
58857c478bd9Sstevel@tonic-gate 		vap->va_mask |= AT_TYPE;
58867c478bd9Sstevel@tonic-gate 
58877c478bd9Sstevel@tonic-gate 		if ((vap->va_mask & AT_MODE) == 0) {
58887c478bd9Sstevel@tonic-gate 			vap->va_mask |= AT_MODE;
58897c478bd9Sstevel@tonic-gate 			vap->va_mode = (mode_t)0600;
58907c478bd9Sstevel@tonic-gate 		}
58917c478bd9Sstevel@tonic-gate 
58927c478bd9Sstevel@tonic-gate 		if (vap->va_mask & AT_SIZE) {
58937c478bd9Sstevel@tonic-gate 
58947c478bd9Sstevel@tonic-gate 			/* Disallow create with a non-zero size */
58957c478bd9Sstevel@tonic-gate 
58967c478bd9Sstevel@tonic-gate 			if ((reqsize = sarg.vap->va_size) != 0) {
58977c478bd9Sstevel@tonic-gate 				kmem_free(nm, buflen);
58987c478bd9Sstevel@tonic-gate 				nfs4_ntov_table_free(&ntov, &sarg);
58997c478bd9Sstevel@tonic-gate 				*attrset = 0;
59007c478bd9Sstevel@tonic-gate 				return (NFS4ERR_INVAL);
59017c478bd9Sstevel@tonic-gate 			}
59027c478bd9Sstevel@tonic-gate 			setsize = TRUE;
59037c478bd9Sstevel@tonic-gate 		}
59047c478bd9Sstevel@tonic-gate 		break;
59057c478bd9Sstevel@tonic-gate 
59067c478bd9Sstevel@tonic-gate 	case EXCLUSIVE4:
59077c478bd9Sstevel@tonic-gate 		/* prohibit EXCL create of named attributes */
59087c478bd9Sstevel@tonic-gate 		if (dvp->v_flag & V_XATTRDIR) {
59097c478bd9Sstevel@tonic-gate 			kmem_free(nm, buflen);
59107c478bd9Sstevel@tonic-gate 			*attrset = 0;
59117c478bd9Sstevel@tonic-gate 			return (NFS4ERR_INVAL);
59127c478bd9Sstevel@tonic-gate 		}
59137c478bd9Sstevel@tonic-gate 
59147c478bd9Sstevel@tonic-gate 		cva.va_mask = AT_TYPE | AT_MTIME | AT_MODE;
59157c478bd9Sstevel@tonic-gate 		cva.va_type = VREG;
59167c478bd9Sstevel@tonic-gate 		/*
59177c478bd9Sstevel@tonic-gate 		 * Ensure no time overflows. Assumes underlying
59187c478bd9Sstevel@tonic-gate 		 * filesystem supports at least 32 bits.
59197c478bd9Sstevel@tonic-gate 		 * Truncate nsec to usec resolution to allow valid
59207c478bd9Sstevel@tonic-gate 		 * compares even if the underlying filesystem truncates.
59217c478bd9Sstevel@tonic-gate 		 */
59227c478bd9Sstevel@tonic-gate 		mtime = (timespec32_t *)&args->createhow4_u.createverf;
59237c478bd9Sstevel@tonic-gate 		cva.va_mtime.tv_sec = mtime->tv_sec % TIME32_MAX;
59247c478bd9Sstevel@tonic-gate 		cva.va_mtime.tv_nsec = (mtime->tv_nsec / 1000) * 1000;
59257c478bd9Sstevel@tonic-gate 		cva.va_mode = (mode_t)0;
59267c478bd9Sstevel@tonic-gate 		vap = &cva;
59277c478bd9Sstevel@tonic-gate 		break;
59287c478bd9Sstevel@tonic-gate 	}
59297c478bd9Sstevel@tonic-gate 
59307c478bd9Sstevel@tonic-gate 	status = create_vnode(dvp, nm, vap, args->mode, mtime,
59317c478bd9Sstevel@tonic-gate 						cs->cr, &vp, &created);
59327c478bd9Sstevel@tonic-gate 	kmem_free(nm, buflen);
59337c478bd9Sstevel@tonic-gate 
59347c478bd9Sstevel@tonic-gate 	if (status != NFS4_OK) {
59357c478bd9Sstevel@tonic-gate 		if (ntov_table_init)
59367c478bd9Sstevel@tonic-gate 			nfs4_ntov_table_free(&ntov, &sarg);
59377c478bd9Sstevel@tonic-gate 		*attrset = 0;
59387c478bd9Sstevel@tonic-gate 		return (status);
59397c478bd9Sstevel@tonic-gate 	}
59407c478bd9Sstevel@tonic-gate 
59417c478bd9Sstevel@tonic-gate 	trunc = (setsize && !created);
59427c478bd9Sstevel@tonic-gate 
59437c478bd9Sstevel@tonic-gate 	if (args->mode != EXCLUSIVE4) {
59447c478bd9Sstevel@tonic-gate 		bitmap4 createmask = args->createhow4_u.createattrs.attrmask;
59457c478bd9Sstevel@tonic-gate 
59467c478bd9Sstevel@tonic-gate 		/*
59477c478bd9Sstevel@tonic-gate 		 * True verification that object was created with correct
59487c478bd9Sstevel@tonic-gate 		 * attrs is impossible.  The attrs could have been changed
59497c478bd9Sstevel@tonic-gate 		 * immediately after object creation.  If attributes did
59507c478bd9Sstevel@tonic-gate 		 * not verify, the only recourse for the server is to
59517c478bd9Sstevel@tonic-gate 		 * destroy the object.  Maybe if some attrs (like gid)
59527c478bd9Sstevel@tonic-gate 		 * are set incorrectly, the object should be destroyed;
59537c478bd9Sstevel@tonic-gate 		 * however, seems bad as a default policy.  Do we really
59547c478bd9Sstevel@tonic-gate 		 * want to destroy an object over one of the times not
59557c478bd9Sstevel@tonic-gate 		 * verifying correctly?  For these reasons, the server
59567c478bd9Sstevel@tonic-gate 		 * currently sets bits in attrset for createattrs
59577c478bd9Sstevel@tonic-gate 		 * that were set; however, no verification is done.
59587c478bd9Sstevel@tonic-gate 		 *
59597c478bd9Sstevel@tonic-gate 		 * vmask_to_nmask accounts for vattr bits set on create
59607c478bd9Sstevel@tonic-gate 		 *	[do_rfs4_set_attrs() only sets resp bits for
59617c478bd9Sstevel@tonic-gate 		 *	 non-vattr/vfs bits.]
59627c478bd9Sstevel@tonic-gate 		 * Mask off any bits we set by default so as not to return
59637c478bd9Sstevel@tonic-gate 		 * more attrset bits than were requested in createattrs
59647c478bd9Sstevel@tonic-gate 		 */
59657c478bd9Sstevel@tonic-gate 		if (created) {
59667c478bd9Sstevel@tonic-gate 			nfs4_vmask_to_nmask(sarg.vap->va_mask, attrset);
59677c478bd9Sstevel@tonic-gate 			*attrset &= createmask;
59687c478bd9Sstevel@tonic-gate 		} else {
59697c478bd9Sstevel@tonic-gate 			/*
59707c478bd9Sstevel@tonic-gate 			 * We did not create the vnode (we tried but it
59717c478bd9Sstevel@tonic-gate 			 * already existed).  In this case, the only createattr
59727c478bd9Sstevel@tonic-gate 			 * that the spec allows the server to set is size,
59737c478bd9Sstevel@tonic-gate 			 * and even then, it can only be set if it is 0.
59747c478bd9Sstevel@tonic-gate 			 */
59757c478bd9Sstevel@tonic-gate 			*attrset = 0;
59767c478bd9Sstevel@tonic-gate 			if (trunc)
59777c478bd9Sstevel@tonic-gate 				*attrset = FATTR4_SIZE_MASK;
59787c478bd9Sstevel@tonic-gate 		}
59797c478bd9Sstevel@tonic-gate 	}
59807c478bd9Sstevel@tonic-gate 	if (ntov_table_init)
59817c478bd9Sstevel@tonic-gate 		nfs4_ntov_table_free(&ntov, &sarg);
59827c478bd9Sstevel@tonic-gate 
59837c478bd9Sstevel@tonic-gate 	/*
59847c478bd9Sstevel@tonic-gate 	 * Get the initial "after" sequence number, if it fails,
59857c478bd9Sstevel@tonic-gate 	 * set to zero, time to before.
59867c478bd9Sstevel@tonic-gate 	 */
59877c478bd9Sstevel@tonic-gate 	iva.va_mask = AT_CTIME|AT_SEQ;
59887c478bd9Sstevel@tonic-gate 	if (VOP_GETATTR(dvp, &iva, 0, cs->cr)) {
59897c478bd9Sstevel@tonic-gate 		iva.va_seq = 0;
59907c478bd9Sstevel@tonic-gate 		iva.va_ctime = bva.va_ctime;
59917c478bd9Sstevel@tonic-gate 	}
59927c478bd9Sstevel@tonic-gate 
59937c478bd9Sstevel@tonic-gate 	/*
59947c478bd9Sstevel@tonic-gate 	 * create_vnode attempts to create the file exclusive,
59957c478bd9Sstevel@tonic-gate 	 * if it already exists the VOP_CREATE will fail and
59967c478bd9Sstevel@tonic-gate 	 * may not increase va_seq. It is atomic if
59977c478bd9Sstevel@tonic-gate 	 * we haven't changed the directory, but if it has changed
59987c478bd9Sstevel@tonic-gate 	 * we don't know what changed it.
59997c478bd9Sstevel@tonic-gate 	 */
60007c478bd9Sstevel@tonic-gate 	if (!created) {
60017c478bd9Sstevel@tonic-gate 		if (bva.va_seq && iva.va_seq &&
60027c478bd9Sstevel@tonic-gate 			bva.va_seq == iva.va_seq)
60037c478bd9Sstevel@tonic-gate 			cinfo->atomic = TRUE;
60047c478bd9Sstevel@tonic-gate 		else
60057c478bd9Sstevel@tonic-gate 			cinfo->atomic = FALSE;
60067c478bd9Sstevel@tonic-gate 		NFS4_SET_FATTR4_CHANGE(cinfo->after, iva.va_ctime);
60077c478bd9Sstevel@tonic-gate 	} else {
60087c478bd9Sstevel@tonic-gate 		/*
60097c478bd9Sstevel@tonic-gate 		 * The entry was created, we need to sync the
60107c478bd9Sstevel@tonic-gate 		 * directory metadata.
60117c478bd9Sstevel@tonic-gate 		 */
60127c478bd9Sstevel@tonic-gate 		(void) VOP_FSYNC(dvp, 0, cs->cr);
60137c478bd9Sstevel@tonic-gate 
60147c478bd9Sstevel@tonic-gate 		/*
60157c478bd9Sstevel@tonic-gate 		 * Get "after" change value, if it fails, simply return the
60167c478bd9Sstevel@tonic-gate 		 * before value.
60177c478bd9Sstevel@tonic-gate 		 */
60187c478bd9Sstevel@tonic-gate 		ava.va_mask = AT_CTIME|AT_SEQ;
60197c478bd9Sstevel@tonic-gate 		if (VOP_GETATTR(dvp, &ava, 0, cs->cr)) {
60207c478bd9Sstevel@tonic-gate 			ava.va_ctime = bva.va_ctime;
60217c478bd9Sstevel@tonic-gate 			ava.va_seq = 0;
60227c478bd9Sstevel@tonic-gate 		}
60237c478bd9Sstevel@tonic-gate 
60247c478bd9Sstevel@tonic-gate 		NFS4_SET_FATTR4_CHANGE(cinfo->after, ava.va_ctime);
60257c478bd9Sstevel@tonic-gate 
60267c478bd9Sstevel@tonic-gate 		/*
60277c478bd9Sstevel@tonic-gate 		 * The cinfo->atomic = TRUE only if we have
60287c478bd9Sstevel@tonic-gate 		 * non-zero va_seq's, and it has incremented by exactly one
60297c478bd9Sstevel@tonic-gate 		 * during the create_vnode and it didn't
60307c478bd9Sstevel@tonic-gate 		 * change during the VOP_FSYNC.
60317c478bd9Sstevel@tonic-gate 		 */
60327c478bd9Sstevel@tonic-gate 		if (bva.va_seq && iva.va_seq && ava.va_seq &&
60337c478bd9Sstevel@tonic-gate 				iva.va_seq == (bva.va_seq + 1) &&
60347c478bd9Sstevel@tonic-gate 				iva.va_seq == ava.va_seq)
60357c478bd9Sstevel@tonic-gate 			cinfo->atomic = TRUE;
60367c478bd9Sstevel@tonic-gate 		else
60377c478bd9Sstevel@tonic-gate 			cinfo->atomic = FALSE;
60387c478bd9Sstevel@tonic-gate 	}
60397c478bd9Sstevel@tonic-gate 
60407c478bd9Sstevel@tonic-gate 	/* Check for mandatory locking and that the size gets set. */
60417c478bd9Sstevel@tonic-gate 	cva.va_mask = AT_MODE;
60427c478bd9Sstevel@tonic-gate 	if (setsize)
60437c478bd9Sstevel@tonic-gate 		cva.va_mask |= AT_SIZE;
60447c478bd9Sstevel@tonic-gate 
60457c478bd9Sstevel@tonic-gate 	/* Assume the worst */
60467c478bd9Sstevel@tonic-gate 	cs->mandlock = TRUE;
60477c478bd9Sstevel@tonic-gate 
60487c478bd9Sstevel@tonic-gate 	if (VOP_GETATTR(vp, &cva, 0, cs->cr) == 0) {
60497c478bd9Sstevel@tonic-gate 		cs->mandlock = MANDLOCK(cs->vp, cva.va_mode);
60507c478bd9Sstevel@tonic-gate 
60517c478bd9Sstevel@tonic-gate 		/*
60527c478bd9Sstevel@tonic-gate 		 * Truncate the file if necessary; this would be
60537c478bd9Sstevel@tonic-gate 		 * the case for create over an existing file.
60547c478bd9Sstevel@tonic-gate 		 */
60557c478bd9Sstevel@tonic-gate 
60567c478bd9Sstevel@tonic-gate 		if (trunc) {
60577c478bd9Sstevel@tonic-gate 			int in_crit = 0;
60587c478bd9Sstevel@tonic-gate 			rfs4_file_t *fp;
60597c478bd9Sstevel@tonic-gate 			bool_t create = FALSE;
60607c478bd9Sstevel@tonic-gate 
60617c478bd9Sstevel@tonic-gate 			/*
60627c478bd9Sstevel@tonic-gate 			 * We are writing over an existing file.
60637c478bd9Sstevel@tonic-gate 			 * Check to see if we need to recall a delegation.
60647c478bd9Sstevel@tonic-gate 			 */
60657c478bd9Sstevel@tonic-gate 			rfs4_hold_deleg_policy();
60667c478bd9Sstevel@tonic-gate 			if ((fp = rfs4_findfile(vp, NULL, &create)) != NULL) {
60677c478bd9Sstevel@tonic-gate 				if (rfs4_check_delegated_byfp(FWRITE, fp,
60687c478bd9Sstevel@tonic-gate 					(reqsize == 0), FALSE, FALSE,
60697c478bd9Sstevel@tonic-gate 							&clientid)) {
60707c478bd9Sstevel@tonic-gate 
60717c478bd9Sstevel@tonic-gate 					rfs4_file_rele(fp);
60727c478bd9Sstevel@tonic-gate 					rfs4_rele_deleg_policy();
60737c478bd9Sstevel@tonic-gate 					VN_RELE(vp);
60747c478bd9Sstevel@tonic-gate 					*attrset = 0;
60757c478bd9Sstevel@tonic-gate 					return (NFS4ERR_DELAY);
60767c478bd9Sstevel@tonic-gate 				}
60777c478bd9Sstevel@tonic-gate 				rfs4_file_rele(fp);
60787c478bd9Sstevel@tonic-gate 			}
60797c478bd9Sstevel@tonic-gate 			rfs4_rele_deleg_policy();
60807c478bd9Sstevel@tonic-gate 
60817c478bd9Sstevel@tonic-gate 			if (nbl_need_check(vp)) {
60827c478bd9Sstevel@tonic-gate 				in_crit = 1;
60837c478bd9Sstevel@tonic-gate 
60847c478bd9Sstevel@tonic-gate 				ASSERT(reqsize == 0);
60857c478bd9Sstevel@tonic-gate 
60867c478bd9Sstevel@tonic-gate 				nbl_start_crit(vp, RW_READER);
60877c478bd9Sstevel@tonic-gate 				if (nbl_conflict(vp, NBL_WRITE, 0,
60887c478bd9Sstevel@tonic-gate 						cva.va_size, 0)) {
60897c478bd9Sstevel@tonic-gate 					in_crit = 0;
60907c478bd9Sstevel@tonic-gate 					nbl_end_crit(vp);
60917c478bd9Sstevel@tonic-gate 					VN_RELE(vp);
60927c478bd9Sstevel@tonic-gate 					*attrset = 0;
60937c478bd9Sstevel@tonic-gate 					return (NFS4ERR_ACCESS);
60947c478bd9Sstevel@tonic-gate 				}
60957c478bd9Sstevel@tonic-gate 			}
60967c478bd9Sstevel@tonic-gate 			ct.cc_sysid = 0;
60977c478bd9Sstevel@tonic-gate 			ct.cc_pid = 0;
60987c478bd9Sstevel@tonic-gate 			ct.cc_caller_id = nfs4_srv_caller_id;
60997c478bd9Sstevel@tonic-gate 
61007c478bd9Sstevel@tonic-gate 			cva.va_mask = AT_SIZE;
61017c478bd9Sstevel@tonic-gate 			cva.va_size = reqsize;
61027c478bd9Sstevel@tonic-gate 			(void) VOP_SETATTR(vp, &cva, 0, cs->cr, &ct);
61037c478bd9Sstevel@tonic-gate 			if (in_crit)
61047c478bd9Sstevel@tonic-gate 				nbl_end_crit(vp);
61057c478bd9Sstevel@tonic-gate 		}
61067c478bd9Sstevel@tonic-gate 	}
61077c478bd9Sstevel@tonic-gate 
61087c478bd9Sstevel@tonic-gate 	error = makefh4(&cs->fh, vp, cs->exi);
61097c478bd9Sstevel@tonic-gate 
61107c478bd9Sstevel@tonic-gate 	/*
61117c478bd9Sstevel@tonic-gate 	 * Force modified data and metadata out to stable storage.
61127c478bd9Sstevel@tonic-gate 	 */
61137c478bd9Sstevel@tonic-gate 	(void) VOP_FSYNC(vp, FNODSYNC, cs->cr);
61147c478bd9Sstevel@tonic-gate 
61157c478bd9Sstevel@tonic-gate 	if (error) {
61167c478bd9Sstevel@tonic-gate 		VN_RELE(vp);
61177c478bd9Sstevel@tonic-gate 		*attrset = 0;
61187c478bd9Sstevel@tonic-gate 		return (puterrno4(error));
61197c478bd9Sstevel@tonic-gate 	}
61207c478bd9Sstevel@tonic-gate 
61217c478bd9Sstevel@tonic-gate 	/* if parent dir is attrdir, set namedattr fh flag */
61227c478bd9Sstevel@tonic-gate 	if (dvp->v_flag & V_XATTRDIR)
61237c478bd9Sstevel@tonic-gate 		set_fh4_flag(&cs->fh, FH4_NAMEDATTR);
61247c478bd9Sstevel@tonic-gate 
61257c478bd9Sstevel@tonic-gate 	if (cs->vp)
61267c478bd9Sstevel@tonic-gate 		VN_RELE(cs->vp);
61277c478bd9Sstevel@tonic-gate 
61287c478bd9Sstevel@tonic-gate 	cs->vp = vp;
61297c478bd9Sstevel@tonic-gate 
61307c478bd9Sstevel@tonic-gate 	/*
61317c478bd9Sstevel@tonic-gate 	 * if we did not create the file, we will need to check
61327c478bd9Sstevel@tonic-gate 	 * the access bits on the file
61337c478bd9Sstevel@tonic-gate 	 */
61347c478bd9Sstevel@tonic-gate 
61357c478bd9Sstevel@tonic-gate 	if (!created) {
61367c478bd9Sstevel@tonic-gate 		if (setsize)
61377c478bd9Sstevel@tonic-gate 			args->share_access |= OPEN4_SHARE_ACCESS_WRITE;
61387c478bd9Sstevel@tonic-gate 		status = check_open_access(args->share_access, cs, req);
61397c478bd9Sstevel@tonic-gate 		if (status != NFS4_OK)
61407c478bd9Sstevel@tonic-gate 			*attrset = 0;
61417c478bd9Sstevel@tonic-gate 	}
61427c478bd9Sstevel@tonic-gate 	return (status);
61437c478bd9Sstevel@tonic-gate }
61447c478bd9Sstevel@tonic-gate 
61457c478bd9Sstevel@tonic-gate /*ARGSUSED*/
61467c478bd9Sstevel@tonic-gate static void
61477c478bd9Sstevel@tonic-gate rfs4_do_open(struct compound_state *cs, struct svc_req *req,
61487c478bd9Sstevel@tonic-gate 		rfs4_openowner_t *oo, delegreq_t deleg,
61497c478bd9Sstevel@tonic-gate 		uint32_t access, uint32_t deny,
61507c478bd9Sstevel@tonic-gate 		OPEN4res *resp)
61517c478bd9Sstevel@tonic-gate {
61527c478bd9Sstevel@tonic-gate 	/* XXX Currently not using req  */
61537c478bd9Sstevel@tonic-gate 	rfs4_state_t *state;
61547c478bd9Sstevel@tonic-gate 	rfs4_file_t *file;
61557c478bd9Sstevel@tonic-gate 	bool_t screate = TRUE;
61567c478bd9Sstevel@tonic-gate 	bool_t fcreate = TRUE;
61577c478bd9Sstevel@tonic-gate 	uint32_t amodes;
61587c478bd9Sstevel@tonic-gate 	uint32_t dmodes;
61597c478bd9Sstevel@tonic-gate 	rfs4_deleg_state_t *dsp;
61607c478bd9Sstevel@tonic-gate 	struct shrlock shr;
61617c478bd9Sstevel@tonic-gate 	struct shr_locowner shr_loco;
61627c478bd9Sstevel@tonic-gate 	sysid_t sysid;
61637c478bd9Sstevel@tonic-gate 	nfsstat4 status;
61647c478bd9Sstevel@tonic-gate 	int fflags = 0;
61657c478bd9Sstevel@tonic-gate 	int recall = 0;
61667c478bd9Sstevel@tonic-gate 	int err;
61677c478bd9Sstevel@tonic-gate 
61687c478bd9Sstevel@tonic-gate 	/* get the file struct and hold a lock on it during initial open */
61697c478bd9Sstevel@tonic-gate 	file = rfs4_findfile_withlock(cs->vp, &cs->fh, &fcreate);
61707c478bd9Sstevel@tonic-gate 	if (file == NULL) {
61717c478bd9Sstevel@tonic-gate 		NFS4_DEBUG(rfs4_debug,
61727c478bd9Sstevel@tonic-gate 			(CE_NOTE, "rfs4_do_open: can't find file"));
61737c478bd9Sstevel@tonic-gate 		resp->status = NFS4ERR_SERVERFAULT;
61747c478bd9Sstevel@tonic-gate 		return;
61757c478bd9Sstevel@tonic-gate 	}
61767c478bd9Sstevel@tonic-gate 
61777c478bd9Sstevel@tonic-gate 	state = rfs4_findstate_by_owner_file(oo, file, &screate);
61787c478bd9Sstevel@tonic-gate 	if (state == NULL) {
61797c478bd9Sstevel@tonic-gate 		NFS4_DEBUG(rfs4_debug,
61807c478bd9Sstevel@tonic-gate 			(CE_NOTE, "rfs4_do_open: can't find state"));
61817c478bd9Sstevel@tonic-gate 		resp->status = NFS4ERR_RESOURCE;
61827c478bd9Sstevel@tonic-gate 		/* No need to keep any reference */
61837c478bd9Sstevel@tonic-gate 		rfs4_file_rele_withunlock(file);
61847c478bd9Sstevel@tonic-gate 		return;
61857c478bd9Sstevel@tonic-gate 	}
61867c478bd9Sstevel@tonic-gate 
6187fd93bfc3Sjwahlig 	/* try to get the sysid before continuing */
61887c478bd9Sstevel@tonic-gate 	if ((status = rfs4_client_sysid(oo->client, &sysid)) != NFS4_OK) {
61897c478bd9Sstevel@tonic-gate 		resp->status = status;
61907c478bd9Sstevel@tonic-gate 		rfs4_file_rele(file);
61917c478bd9Sstevel@tonic-gate 		/* Not a fully formed open; "close" it */
61927c478bd9Sstevel@tonic-gate 		if (screate == TRUE)
61937c478bd9Sstevel@tonic-gate 			rfs4_state_close(state, FALSE, FALSE, cs->cr);
61947c478bd9Sstevel@tonic-gate 		rfs4_state_rele(state);
61957c478bd9Sstevel@tonic-gate 		return;
61967c478bd9Sstevel@tonic-gate 	}
61977c478bd9Sstevel@tonic-gate 
6198fd93bfc3Sjwahlig 	/*
6199fd93bfc3Sjwahlig 	 * Calculate the new deny and access mode that this open is adding to
6200fd93bfc3Sjwahlig 	 * the file for this open owner;
6201fd93bfc3Sjwahlig 	 */
6202fd93bfc3Sjwahlig 	dmodes = (deny & ~state->share_deny);
6203fd93bfc3Sjwahlig 	amodes = (access & ~state->share_access);
62047c478bd9Sstevel@tonic-gate 
6205fd93bfc3Sjwahlig 	/*
6206fd93bfc3Sjwahlig 	 * Check to see the client has already sent an open for this
6207fd93bfc3Sjwahlig 	 * open owner on this file with the same share/deny modes.
6208fd93bfc3Sjwahlig 	 * If so, we don't need to check for a conflict and we don't
6209fd93bfc3Sjwahlig 	 * need to add another shrlock.  If not, then we need to
6210fd93bfc3Sjwahlig 	 * check for conflicts in deny and access before checking for
6211fd93bfc3Sjwahlig 	 * conflicts in delegation.  We don't want to recall a
6212fd93bfc3Sjwahlig 	 * delegation based on an open that will eventually fail based
6213fd93bfc3Sjwahlig 	 * on shares modes.
6214fd93bfc3Sjwahlig 	 */
62157c478bd9Sstevel@tonic-gate 
6216fd93bfc3Sjwahlig 	if (dmodes || amodes) {
6217fd93bfc3Sjwahlig 		shr.s_access = (short)access;
6218fd93bfc3Sjwahlig 		shr.s_deny = (short)deny;
6219fd93bfc3Sjwahlig 		shr.s_pid = rfs4_dbe_getid(oo->dbe);
6220fd93bfc3Sjwahlig 		shr.s_sysid = sysid;
6221fd93bfc3Sjwahlig 		shr_loco.sl_pid = shr.s_pid;
6222fd93bfc3Sjwahlig 		shr_loco.sl_id = shr.s_sysid;
6223fd93bfc3Sjwahlig 		shr.s_owner = (caddr_t)&shr_loco;
6224fd93bfc3Sjwahlig 		shr.s_own_len = sizeof (shr_loco);
62257c478bd9Sstevel@tonic-gate 
6226fd93bfc3Sjwahlig 		fflags = 0;
6227fd93bfc3Sjwahlig 		if (access & OPEN4_SHARE_ACCESS_READ)
6228fd93bfc3Sjwahlig 			fflags |= FREAD;
6229fd93bfc3Sjwahlig 		if (access & OPEN4_SHARE_ACCESS_WRITE)
6230fd93bfc3Sjwahlig 			fflags |= FWRITE;
6231fd93bfc3Sjwahlig 
6232fd93bfc3Sjwahlig 		if ((err = vop_shrlock(cs->vp, F_SHARE, &shr, fflags)) != 0) {
6233fd93bfc3Sjwahlig 
6234fd93bfc3Sjwahlig 			resp->status = err == EAGAIN ?
6235fd93bfc3Sjwahlig 				NFS4ERR_SHARE_DENIED : puterrno4(err);
6236fd93bfc3Sjwahlig 
6237fd93bfc3Sjwahlig 			rfs4_file_rele(file);
6238fd93bfc3Sjwahlig 			/* Not a fully formed open; "close" it */
6239fd93bfc3Sjwahlig 			if (screate == TRUE)
6240fd93bfc3Sjwahlig 				rfs4_state_close(state, FALSE, FALSE, cs->cr);
6241fd93bfc3Sjwahlig 			rfs4_state_rele(state);
6242fd93bfc3Sjwahlig 			return;
6243fd93bfc3Sjwahlig 		}
62447c478bd9Sstevel@tonic-gate 	}
62457c478bd9Sstevel@tonic-gate 
62467c478bd9Sstevel@tonic-gate 	rfs4_dbe_lock(state->dbe);
62477c478bd9Sstevel@tonic-gate 	rfs4_dbe_lock(file->dbe);
62487c478bd9Sstevel@tonic-gate 
62497c478bd9Sstevel@tonic-gate 	/*
62507c478bd9Sstevel@tonic-gate 	 * Check to see if this file is delegated and if so, if a
62517c478bd9Sstevel@tonic-gate 	 * recall needs to be done.
62527c478bd9Sstevel@tonic-gate 	 */
62537c478bd9Sstevel@tonic-gate 	if (rfs4_check_recall(state, access)) {
62547c478bd9Sstevel@tonic-gate 		rfs4_dbe_unlock(file->dbe);
62557c478bd9Sstevel@tonic-gate 		rfs4_dbe_unlock(state->dbe);
62567c478bd9Sstevel@tonic-gate 		rfs4_recall_deleg(file, FALSE, state->owner->client);
62577c478bd9Sstevel@tonic-gate 		delay(NFS4_DELEGATION_CONFLICT_DELAY);
62587c478bd9Sstevel@tonic-gate 		rfs4_dbe_lock(state->dbe);
62597c478bd9Sstevel@tonic-gate 		rfs4_dbe_lock(file->dbe);
62607c478bd9Sstevel@tonic-gate 		/* Let's see if the delegation was returned */
62617c478bd9Sstevel@tonic-gate 		if (rfs4_check_recall(state, access)) {
62627c478bd9Sstevel@tonic-gate 			rfs4_dbe_unlock(file->dbe);
62637c478bd9Sstevel@tonic-gate 			rfs4_dbe_unlock(state->dbe);
62647c478bd9Sstevel@tonic-gate 			rfs4_file_rele(file);
62657c478bd9Sstevel@tonic-gate 			rfs4_update_lease(state->owner->client);
62667c478bd9Sstevel@tonic-gate 			/* recalculate flags to match what was added */
62677c478bd9Sstevel@tonic-gate 			fflags = 0;
62687c478bd9Sstevel@tonic-gate 			if (amodes & OPEN4_SHARE_ACCESS_READ)
62697c478bd9Sstevel@tonic-gate 				fflags |= FREAD;
62707c478bd9Sstevel@tonic-gate 			if (amodes & OPEN4_SHARE_ACCESS_WRITE)
62717c478bd9Sstevel@tonic-gate 				fflags |= FWRITE;
62727c478bd9Sstevel@tonic-gate 			(void) vop_shrlock(cs->vp, F_UNSHARE, &shr, fflags);
62737c478bd9Sstevel@tonic-gate 			/* Not a fully formed open; "close" it */
62747c478bd9Sstevel@tonic-gate 			if (screate == TRUE)
62757c478bd9Sstevel@tonic-gate 				rfs4_state_close(state, FALSE, FALSE, cs->cr);
62767c478bd9Sstevel@tonic-gate 			rfs4_state_rele(state);
62777c478bd9Sstevel@tonic-gate 			resp->status = NFS4ERR_DELAY;
62787c478bd9Sstevel@tonic-gate 			return;
62797c478bd9Sstevel@tonic-gate 		}
62807c478bd9Sstevel@tonic-gate 	}
62817c478bd9Sstevel@tonic-gate 
62827c478bd9Sstevel@tonic-gate 	if (dmodes & OPEN4_SHARE_DENY_READ)
62837c478bd9Sstevel@tonic-gate 		file->deny_read++;
62847c478bd9Sstevel@tonic-gate 	if (dmodes & OPEN4_SHARE_DENY_WRITE)
62857c478bd9Sstevel@tonic-gate 		file->deny_write++;
62867c478bd9Sstevel@tonic-gate 	file->share_deny |= deny;
62877c478bd9Sstevel@tonic-gate 	state->share_deny |= deny;
62887c478bd9Sstevel@tonic-gate 
62897c478bd9Sstevel@tonic-gate 	if (amodes & OPEN4_SHARE_ACCESS_READ)
62907c478bd9Sstevel@tonic-gate 		file->access_read++;
62917c478bd9Sstevel@tonic-gate 	if (amodes & OPEN4_SHARE_ACCESS_WRITE)
62927c478bd9Sstevel@tonic-gate 		file->access_write++;
62937c478bd9Sstevel@tonic-gate 	file->share_access |= access;
62947c478bd9Sstevel@tonic-gate 	state->share_access |= access;
62957c478bd9Sstevel@tonic-gate 
62967c478bd9Sstevel@tonic-gate 	/*
62977c478bd9Sstevel@tonic-gate 	 * Check for delegation here. if the deleg argument is not
62987c478bd9Sstevel@tonic-gate 	 * DELEG_ANY, then this is a reclaim from a client and
62997c478bd9Sstevel@tonic-gate 	 * we must honor the delegation requested. If necessary we can
63007c478bd9Sstevel@tonic-gate 	 * set the recall flag.
63017c478bd9Sstevel@tonic-gate 	 */
63027c478bd9Sstevel@tonic-gate 
63037c478bd9Sstevel@tonic-gate 	dsp = rfs4_grant_delegation(deleg, state, &recall);
63047c478bd9Sstevel@tonic-gate 
63057c478bd9Sstevel@tonic-gate 	cs->deleg = (file->dinfo->dtype == OPEN_DELEGATE_WRITE);
63067c478bd9Sstevel@tonic-gate 
63077c478bd9Sstevel@tonic-gate 	next_stateid(&state->stateid);
63087c478bd9Sstevel@tonic-gate 
63097c478bd9Sstevel@tonic-gate 	resp->stateid = state->stateid.stateid;
63107c478bd9Sstevel@tonic-gate 
63117c478bd9Sstevel@tonic-gate 	rfs4_dbe_unlock(file->dbe);
63127c478bd9Sstevel@tonic-gate 	rfs4_dbe_unlock(state->dbe);
63137c478bd9Sstevel@tonic-gate 
63147c478bd9Sstevel@tonic-gate 	if (dsp) {
63157c478bd9Sstevel@tonic-gate 		rfs4_set_deleg_response(dsp, &resp->delegation, NULL, recall);
63167c478bd9Sstevel@tonic-gate 		rfs4_deleg_state_rele(dsp);
63177c478bd9Sstevel@tonic-gate 	}
63187c478bd9Sstevel@tonic-gate 
63197c478bd9Sstevel@tonic-gate 	rfs4_file_rele(file);
63207c478bd9Sstevel@tonic-gate 	rfs4_state_rele(state);
63217c478bd9Sstevel@tonic-gate 
63227c478bd9Sstevel@tonic-gate 	resp->status = NFS4_OK;
63237c478bd9Sstevel@tonic-gate }
63247c478bd9Sstevel@tonic-gate 
63257c478bd9Sstevel@tonic-gate /*ARGSUSED*/
63267c478bd9Sstevel@tonic-gate static void
63277c478bd9Sstevel@tonic-gate rfs4_do_opennull(struct compound_state *cs, struct svc_req *req,
63287c478bd9Sstevel@tonic-gate 		OPEN4args *args, rfs4_openowner_t *oo, OPEN4res *resp)
63297c478bd9Sstevel@tonic-gate {
63307c478bd9Sstevel@tonic-gate 	change_info4 *cinfo = &resp->cinfo;
63317c478bd9Sstevel@tonic-gate 	bitmap4 *attrset = &resp->attrset;
63327c478bd9Sstevel@tonic-gate 
63337c478bd9Sstevel@tonic-gate 	if (args->opentype == OPEN4_NOCREATE)
63347c478bd9Sstevel@tonic-gate 		resp->status = rfs4_lookupfile(&args->open_claim4_u.file,
63357c478bd9Sstevel@tonic-gate 					req, cs, args->share_access, cinfo);
63367c478bd9Sstevel@tonic-gate 	else {
63377c478bd9Sstevel@tonic-gate 		/* inhibit delegation grants during exclusive create */
63387c478bd9Sstevel@tonic-gate 
63397c478bd9Sstevel@tonic-gate 		if (args->mode == EXCLUSIVE4)
63407c478bd9Sstevel@tonic-gate 			rfs4_disable_delegation();
63417c478bd9Sstevel@tonic-gate 
63427c478bd9Sstevel@tonic-gate 		resp->status = rfs4_createfile(args, req, cs, cinfo, attrset,
63437c478bd9Sstevel@tonic-gate 					oo->client->clientid);
63447c478bd9Sstevel@tonic-gate 	}
63457c478bd9Sstevel@tonic-gate 
63467c478bd9Sstevel@tonic-gate 	if (resp->status == NFS4_OK) {
63477c478bd9Sstevel@tonic-gate 
63487c478bd9Sstevel@tonic-gate 		/* cs->vp cs->fh now reference the desired file */
63497c478bd9Sstevel@tonic-gate 
63507c478bd9Sstevel@tonic-gate 		rfs4_do_open(cs, req, oo, DELEG_ANY, args->share_access,
63517c478bd9Sstevel@tonic-gate 						args->share_deny, resp);
63527c478bd9Sstevel@tonic-gate 
63537c478bd9Sstevel@tonic-gate 		/*
63547c478bd9Sstevel@tonic-gate 		 * If rfs4_createfile set attrset, we must
63557c478bd9Sstevel@tonic-gate 		 * clear this attrset before the response is copied.
63567c478bd9Sstevel@tonic-gate 		 */
63577c478bd9Sstevel@tonic-gate 		if (resp->status != NFS4_OK && resp->attrset) {
63587c478bd9Sstevel@tonic-gate 			resp->attrset = 0;
63597c478bd9Sstevel@tonic-gate 		}
63607c478bd9Sstevel@tonic-gate 	}
63617c478bd9Sstevel@tonic-gate 	else
63627c478bd9Sstevel@tonic-gate 		*cs->statusp = resp->status;
63637c478bd9Sstevel@tonic-gate 
63647c478bd9Sstevel@tonic-gate 	if (args->mode == EXCLUSIVE4)
63657c478bd9Sstevel@tonic-gate 		rfs4_enable_delegation();
63667c478bd9Sstevel@tonic-gate }
63677c478bd9Sstevel@tonic-gate 
63687c478bd9Sstevel@tonic-gate /*ARGSUSED*/
63697c478bd9Sstevel@tonic-gate static void
63707c478bd9Sstevel@tonic-gate rfs4_do_openprev(struct compound_state *cs, struct svc_req *req,
63717c478bd9Sstevel@tonic-gate 		OPEN4args *args, rfs4_openowner_t *oo, OPEN4res *resp)
63727c478bd9Sstevel@tonic-gate {
63737c478bd9Sstevel@tonic-gate 	change_info4 *cinfo = &resp->cinfo;
63747c478bd9Sstevel@tonic-gate 	vattr_t va;
63757c478bd9Sstevel@tonic-gate 	vtype_t v_type = cs->vp->v_type;
63767c478bd9Sstevel@tonic-gate 	int error = 0;
63777c478bd9Sstevel@tonic-gate 
63787c478bd9Sstevel@tonic-gate 	/* Verify that we have a regular file */
63797c478bd9Sstevel@tonic-gate 	if (v_type != VREG) {
63807c478bd9Sstevel@tonic-gate 		if (v_type == VDIR)
63817c478bd9Sstevel@tonic-gate 			resp->status = NFS4ERR_ISDIR;
63827c478bd9Sstevel@tonic-gate 		else if (v_type == VLNK)
63837c478bd9Sstevel@tonic-gate 			resp->status = NFS4ERR_SYMLINK;
63847c478bd9Sstevel@tonic-gate 		else
63857c478bd9Sstevel@tonic-gate 			resp->status = NFS4ERR_INVAL;
63867c478bd9Sstevel@tonic-gate 		return;
63877c478bd9Sstevel@tonic-gate 	}
63887c478bd9Sstevel@tonic-gate 
63897c478bd9Sstevel@tonic-gate 	va.va_mask = AT_MODE|AT_UID;
63907c478bd9Sstevel@tonic-gate 	error = VOP_GETATTR(cs->vp, &va, 0, cs->cr);
63917c478bd9Sstevel@tonic-gate 	if (error) {
63927c478bd9Sstevel@tonic-gate 		resp->status = puterrno4(error);
63937c478bd9Sstevel@tonic-gate 		return;
63947c478bd9Sstevel@tonic-gate 	}
63957c478bd9Sstevel@tonic-gate 
63967c478bd9Sstevel@tonic-gate 	cs->mandlock = MANDLOCK(cs->vp, va.va_mode);
63977c478bd9Sstevel@tonic-gate 
63987c478bd9Sstevel@tonic-gate 	/*
63997c478bd9Sstevel@tonic-gate 	 * Check if we have access to the file, Note the the file
64007c478bd9Sstevel@tonic-gate 	 * could have originally been open UNCHECKED or GUARDED
64017c478bd9Sstevel@tonic-gate 	 * with mode bits that will now fail, but there is nothing
64027c478bd9Sstevel@tonic-gate 	 * we can really do about that except in the case that the
64037c478bd9Sstevel@tonic-gate 	 * owner of the file is the one requesting the open.
64047c478bd9Sstevel@tonic-gate 	 */
64057c478bd9Sstevel@tonic-gate 	if (crgetuid(cs->cr) != va.va_uid) {
64067c478bd9Sstevel@tonic-gate 		resp->status = check_open_access(args->share_access, cs, req);
64077c478bd9Sstevel@tonic-gate 		if (resp->status != NFS4_OK) {
64087c478bd9Sstevel@tonic-gate 			return;
64097c478bd9Sstevel@tonic-gate 		}
64107c478bd9Sstevel@tonic-gate 	}
64117c478bd9Sstevel@tonic-gate 
64127c478bd9Sstevel@tonic-gate 	/*
64137c478bd9Sstevel@tonic-gate 	 * cinfo on a CLAIM_PREVIOUS is undefined, initialize to zero
64147c478bd9Sstevel@tonic-gate 	 */
64157c478bd9Sstevel@tonic-gate 	cinfo->before = 0;
64167c478bd9Sstevel@tonic-gate 	cinfo->after = 0;
64177c478bd9Sstevel@tonic-gate 	cinfo->atomic = FALSE;
64187c478bd9Sstevel@tonic-gate 
64197c478bd9Sstevel@tonic-gate 	rfs4_do_open(cs, req, oo,
64207c478bd9Sstevel@tonic-gate 		NFS4_DELEG4TYPE2REQTYPE(args->open_claim4_u.delegate_type),
64217c478bd9Sstevel@tonic-gate 		args->share_access, args->share_deny, resp);
64227c478bd9Sstevel@tonic-gate }
64237c478bd9Sstevel@tonic-gate 
64247c478bd9Sstevel@tonic-gate static void
64257c478bd9Sstevel@tonic-gate rfs4_do_opendelcur(struct compound_state *cs, struct svc_req *req,
64267c478bd9Sstevel@tonic-gate 		OPEN4args *args, rfs4_openowner_t *oo, OPEN4res *resp)
64277c478bd9Sstevel@tonic-gate {
64287c478bd9Sstevel@tonic-gate 	int error;
64297c478bd9Sstevel@tonic-gate 	nfsstat4 status;
64307c478bd9Sstevel@tonic-gate 	stateid4 stateid =
64317c478bd9Sstevel@tonic-gate 			args->open_claim4_u.delegate_cur_info.delegate_stateid;
64327c478bd9Sstevel@tonic-gate 	rfs4_deleg_state_t *dsp;
64337c478bd9Sstevel@tonic-gate 
64347c478bd9Sstevel@tonic-gate 	/*
64357c478bd9Sstevel@tonic-gate 	 * Find the state info from the stateid and confirm that the
64367c478bd9Sstevel@tonic-gate 	 * file is delegated.  If the state openowner is the same as
64377c478bd9Sstevel@tonic-gate 	 * the supplied openowner we're done. If not, get the file
64387c478bd9Sstevel@tonic-gate 	 * info from the found state info. Use that file info to
64397c478bd9Sstevel@tonic-gate 	 * create the state for this lock owner. Note solaris doen't
64407c478bd9Sstevel@tonic-gate 	 * really need the pathname to find the file. We may want to
64417c478bd9Sstevel@tonic-gate 	 * lookup the pathname and make sure that the vp exist and
64427c478bd9Sstevel@tonic-gate 	 * matches the vp in the file structure. However it is
64437c478bd9Sstevel@tonic-gate 	 * possible that the pathname nolonger exists (local process
64447c478bd9Sstevel@tonic-gate 	 * unlinks the file), so this may not be that useful.
64457c478bd9Sstevel@tonic-gate 	 */
64467c478bd9Sstevel@tonic-gate 
64477c478bd9Sstevel@tonic-gate 	status = rfs4_get_deleg_state(&stateid, &dsp);
64487c478bd9Sstevel@tonic-gate 	if (status != NFS4_OK) {
64497c478bd9Sstevel@tonic-gate 		resp->status = status;
64507c478bd9Sstevel@tonic-gate 		return;
64517c478bd9Sstevel@tonic-gate 	}
64527c478bd9Sstevel@tonic-gate 
64537c478bd9Sstevel@tonic-gate 	ASSERT(dsp->finfo->dinfo->dtype != OPEN_DELEGATE_NONE);
64547c478bd9Sstevel@tonic-gate 
64557c478bd9Sstevel@tonic-gate 	/*
64567c478bd9Sstevel@tonic-gate 	 * New lock owner, create state. Since this was probably called
64577c478bd9Sstevel@tonic-gate 	 * in response to a CB_RECALL we set deleg to DELEG_NONE
64587c478bd9Sstevel@tonic-gate 	 */
64597c478bd9Sstevel@tonic-gate 
64607c478bd9Sstevel@tonic-gate 	ASSERT(cs->vp != NULL);
64617c478bd9Sstevel@tonic-gate 	VN_RELE(cs->vp);
64627c478bd9Sstevel@tonic-gate 	VN_HOLD(dsp->finfo->vp);
64637c478bd9Sstevel@tonic-gate 	cs->vp = dsp->finfo->vp;
64647c478bd9Sstevel@tonic-gate 
64657c478bd9Sstevel@tonic-gate 	if (error = makefh4(&cs->fh, cs->vp, cs->exi)) {
64667c478bd9Sstevel@tonic-gate 		rfs4_deleg_state_rele(dsp);
64677c478bd9Sstevel@tonic-gate 		*cs->statusp = resp->status = puterrno4(error);
64687c478bd9Sstevel@tonic-gate 		return;
64697c478bd9Sstevel@tonic-gate 	}
64707c478bd9Sstevel@tonic-gate 
64717c478bd9Sstevel@tonic-gate 	/* Mark progress for delegation returns */
64727c478bd9Sstevel@tonic-gate 	dsp->finfo->dinfo->time_lastwrite = gethrestime_sec();
64737c478bd9Sstevel@tonic-gate 	rfs4_deleg_state_rele(dsp);
64747c478bd9Sstevel@tonic-gate 	rfs4_do_open(cs, req, oo, DELEG_NONE,
64757c478bd9Sstevel@tonic-gate 				args->share_access, args->share_deny, resp);
64767c478bd9Sstevel@tonic-gate }
64777c478bd9Sstevel@tonic-gate 
64787c478bd9Sstevel@tonic-gate /*ARGSUSED*/
64797c478bd9Sstevel@tonic-gate static void
64807c478bd9Sstevel@tonic-gate rfs4_do_opendelprev(struct compound_state *cs, struct svc_req *req,
64817c478bd9Sstevel@tonic-gate 			OPEN4args *args, rfs4_openowner_t *oo, OPEN4res *resp)
64827c478bd9Sstevel@tonic-gate {
64837c478bd9Sstevel@tonic-gate 	/*
64847c478bd9Sstevel@tonic-gate 	 * Lookup the pathname, it must already exist since this file
64857c478bd9Sstevel@tonic-gate 	 * was delegated.
64867c478bd9Sstevel@tonic-gate 	 *
64877c478bd9Sstevel@tonic-gate 	 * Find the file and state info for this vp and open owner pair.
64887c478bd9Sstevel@tonic-gate 	 *	check that they are in fact delegated.
64897c478bd9Sstevel@tonic-gate 	 *	check that the state access and deny modes are the same.
64907c478bd9Sstevel@tonic-gate 	 *
64917c478bd9Sstevel@tonic-gate 	 * Return the delgation possibly seting the recall flag.
64927c478bd9Sstevel@tonic-gate 	 */
64937c478bd9Sstevel@tonic-gate 	rfs4_file_t *file;
64947c478bd9Sstevel@tonic-gate 	rfs4_state_t *state;
64957c478bd9Sstevel@tonic-gate 	bool_t create = FALSE;
64967c478bd9Sstevel@tonic-gate 	bool_t dcreate = FALSE;
64977c478bd9Sstevel@tonic-gate 	rfs4_deleg_state_t *dsp;
64987c478bd9Sstevel@tonic-gate 	nfsace4 *ace;
64997c478bd9Sstevel@tonic-gate 
65007c478bd9Sstevel@tonic-gate 
65017c478bd9Sstevel@tonic-gate 	/* Note we ignore oflags */
65027c478bd9Sstevel@tonic-gate 	resp->status = rfs4_lookupfile(&args->open_claim4_u.file_delegate_prev,
65037c478bd9Sstevel@tonic-gate 				req, cs, args->share_access, &resp->cinfo);
65047c478bd9Sstevel@tonic-gate 
65057c478bd9Sstevel@tonic-gate 	if (resp->status != NFS4_OK) {
65067c478bd9Sstevel@tonic-gate 		return;
65077c478bd9Sstevel@tonic-gate 	}
65087c478bd9Sstevel@tonic-gate 
65097c478bd9Sstevel@tonic-gate 	/* get the file struct and hold a lock on it during initial open */
65107c478bd9Sstevel@tonic-gate 	file = rfs4_findfile_withlock(cs->vp, NULL, &create);
65117c478bd9Sstevel@tonic-gate 	if (file == NULL) {
65127c478bd9Sstevel@tonic-gate 		NFS4_DEBUG(rfs4_debug,
65137c478bd9Sstevel@tonic-gate 			(CE_NOTE, "rfs4_do_opendelprev: can't find file"));
65147c478bd9Sstevel@tonic-gate 		resp->status = NFS4ERR_SERVERFAULT;
65157c478bd9Sstevel@tonic-gate 		return;
65167c478bd9Sstevel@tonic-gate 	}
65177c478bd9Sstevel@tonic-gate 
65187c478bd9Sstevel@tonic-gate 	state = rfs4_findstate_by_owner_file(oo, file, &create);
65197c478bd9Sstevel@tonic-gate 	if (state == NULL) {
65207c478bd9Sstevel@tonic-gate 		NFS4_DEBUG(rfs4_debug,
65217c478bd9Sstevel@tonic-gate 			(CE_NOTE, "rfs4_do_opendelprev: can't find state"));
65227c478bd9Sstevel@tonic-gate 		resp->status = NFS4ERR_SERVERFAULT;
65237c478bd9Sstevel@tonic-gate 		rfs4_file_rele_withunlock(file);
65247c478bd9Sstevel@tonic-gate 		return;
65257c478bd9Sstevel@tonic-gate 	}
65267c478bd9Sstevel@tonic-gate 
65277c478bd9Sstevel@tonic-gate 	rfs4_dbe_lock(state->dbe);
65287c478bd9Sstevel@tonic-gate 	rfs4_dbe_lock(file->dbe);
65297c478bd9Sstevel@tonic-gate 	if (args->share_access != state->share_access ||
65307c478bd9Sstevel@tonic-gate 			args->share_deny != state->share_deny ||
65317c478bd9Sstevel@tonic-gate 			state->finfo->dinfo->dtype == OPEN_DELEGATE_NONE) {
65327c478bd9Sstevel@tonic-gate 		NFS4_DEBUG(rfs4_debug,
65337c478bd9Sstevel@tonic-gate 			(CE_NOTE, "rfs4_do_opendelprev: state mixup"));
65347c478bd9Sstevel@tonic-gate 		rfs4_dbe_unlock(file->dbe);
65357c478bd9Sstevel@tonic-gate 		rfs4_dbe_unlock(state->dbe);
65367c478bd9Sstevel@tonic-gate 		rfs4_file_rele(file);
65377c478bd9Sstevel@tonic-gate 		rfs4_state_rele(state);
65387c478bd9Sstevel@tonic-gate 		resp->status = NFS4ERR_SERVERFAULT;
65397c478bd9Sstevel@tonic-gate 		return;
65407c478bd9Sstevel@tonic-gate 	}
65417c478bd9Sstevel@tonic-gate 	rfs4_dbe_unlock(file->dbe);
65427c478bd9Sstevel@tonic-gate 	rfs4_dbe_unlock(state->dbe);
65437c478bd9Sstevel@tonic-gate 
65447c478bd9Sstevel@tonic-gate 	dsp = rfs4_finddeleg(state, &dcreate);
65457c478bd9Sstevel@tonic-gate 	if (dsp == NULL) {
65467c478bd9Sstevel@tonic-gate 		rfs4_state_rele(state);
65477c478bd9Sstevel@tonic-gate 		rfs4_file_rele(file);
65487c478bd9Sstevel@tonic-gate 		resp->status = NFS4ERR_SERVERFAULT;
65497c478bd9Sstevel@tonic-gate 		return;
65507c478bd9Sstevel@tonic-gate 	}
65517c478bd9Sstevel@tonic-gate 
65527c478bd9Sstevel@tonic-gate 	next_stateid(&state->stateid);
65537c478bd9Sstevel@tonic-gate 
65547c478bd9Sstevel@tonic-gate 	resp->stateid = state->stateid.stateid;
65557c478bd9Sstevel@tonic-gate 
65567c478bd9Sstevel@tonic-gate 	resp->delegation.delegation_type = dsp->dtype;
65577c478bd9Sstevel@tonic-gate 
65587c478bd9Sstevel@tonic-gate 	if (dsp->dtype == OPEN_DELEGATE_READ) {
65597c478bd9Sstevel@tonic-gate 		open_read_delegation4 *rv =
65607c478bd9Sstevel@tonic-gate 			&resp->delegation.open_delegation4_u.read;
65617c478bd9Sstevel@tonic-gate 
65627c478bd9Sstevel@tonic-gate 		rv->stateid = dsp->delegid.stateid;
65637c478bd9Sstevel@tonic-gate 		rv->recall = FALSE; /* no policy in place to set to TRUE */
65647c478bd9Sstevel@tonic-gate 		ace = &rv->permissions;
65657c478bd9Sstevel@tonic-gate 	} else {
65667c478bd9Sstevel@tonic-gate 		open_write_delegation4 *rv =
65677c478bd9Sstevel@tonic-gate 			&resp->delegation.open_delegation4_u.write;
65687c478bd9Sstevel@tonic-gate 
65697c478bd9Sstevel@tonic-gate 		rv->stateid = dsp->delegid.stateid;
65707c478bd9Sstevel@tonic-gate 		rv->recall = FALSE;  /* no policy in place to set to TRUE */
65717c478bd9Sstevel@tonic-gate 		ace = &rv->permissions;
65727c478bd9Sstevel@tonic-gate 		rv->space_limit.limitby = NFS_LIMIT_SIZE;
65737c478bd9Sstevel@tonic-gate 		rv->space_limit.nfs_space_limit4_u.filesize = UINT64_MAX;
65747c478bd9Sstevel@tonic-gate 	}
65757c478bd9Sstevel@tonic-gate 
65767c478bd9Sstevel@tonic-gate 	/* XXX For now */
65777c478bd9Sstevel@tonic-gate 	ace->type = ACE4_ACCESS_ALLOWED_ACE_TYPE;
65787c478bd9Sstevel@tonic-gate 	ace->flag = 0;
65797c478bd9Sstevel@tonic-gate 	ace->access_mask = 0;
65807c478bd9Sstevel@tonic-gate 	ace->who.utf8string_len = 0;
65817c478bd9Sstevel@tonic-gate 	ace->who.utf8string_val = 0;
65827c478bd9Sstevel@tonic-gate 
65837c478bd9Sstevel@tonic-gate 	rfs4_deleg_state_rele(dsp);
65847c478bd9Sstevel@tonic-gate 	rfs4_state_rele(state);
65857c478bd9Sstevel@tonic-gate 	rfs4_file_rele(file);
65867c478bd9Sstevel@tonic-gate }
65877c478bd9Sstevel@tonic-gate 
65887c478bd9Sstevel@tonic-gate typedef enum {
65897c478bd9Sstevel@tonic-gate 	NFS4_CHKSEQ_OKAY = 0,
65907c478bd9Sstevel@tonic-gate 	NFS4_CHKSEQ_REPLAY = 1,
65917c478bd9Sstevel@tonic-gate 	NFS4_CHKSEQ_BAD = 2
65927c478bd9Sstevel@tonic-gate } rfs4_chkseq_t;
65937c478bd9Sstevel@tonic-gate 
65947c478bd9Sstevel@tonic-gate /*
65957c478bd9Sstevel@tonic-gate  * Generic function for sequence number checks.
65967c478bd9Sstevel@tonic-gate  */
65977c478bd9Sstevel@tonic-gate static rfs4_chkseq_t
65987c478bd9Sstevel@tonic-gate rfs4_check_seqid(seqid4 seqid, nfs_resop4 *lastop,
65997c478bd9Sstevel@tonic-gate 		seqid4 rqst_seq, nfs_resop4 *resop, bool_t copyres)
66007c478bd9Sstevel@tonic-gate {
66017c478bd9Sstevel@tonic-gate 	/* Same sequence ids and matching operations? */
66027c478bd9Sstevel@tonic-gate 	if (seqid == rqst_seq && resop->resop == lastop->resop) {
66037c478bd9Sstevel@tonic-gate 		if (copyres == TRUE) {
66047c478bd9Sstevel@tonic-gate 			rfs4_free_reply(resop);
66057c478bd9Sstevel@tonic-gate 			rfs4_copy_reply(resop, lastop);
66067c478bd9Sstevel@tonic-gate 		}
66077c478bd9Sstevel@tonic-gate 		NFS4_DEBUG(rfs4_debug, (CE_NOTE,
66087c478bd9Sstevel@tonic-gate 			"Replayed SEQID %d\n", seqid));
66097c478bd9Sstevel@tonic-gate 		return (NFS4_CHKSEQ_REPLAY);
66107c478bd9Sstevel@tonic-gate 	}
66117c478bd9Sstevel@tonic-gate 
66127c478bd9Sstevel@tonic-gate 	/* If the incoming sequence is not the next expected then it is bad */
66137c478bd9Sstevel@tonic-gate 	if (rqst_seq != seqid + 1) {
66147c478bd9Sstevel@tonic-gate 		if (rqst_seq == seqid) {
66157c478bd9Sstevel@tonic-gate 			NFS4_DEBUG(rfs4_debug,
66167c478bd9Sstevel@tonic-gate 				(CE_NOTE, "BAD SEQID: Replayed sequence id "
66177c478bd9Sstevel@tonic-gate 				"but last op was %d current op is %d\n",
66187c478bd9Sstevel@tonic-gate 				lastop->resop, resop->resop));
66197c478bd9Sstevel@tonic-gate 			return (NFS4_CHKSEQ_BAD);
66207c478bd9Sstevel@tonic-gate 		}
66217c478bd9Sstevel@tonic-gate 		NFS4_DEBUG(rfs4_debug,
66227c478bd9Sstevel@tonic-gate 			(CE_NOTE, "BAD SEQID: got %u expecting %u\n",
66237c478bd9Sstevel@tonic-gate 				rqst_seq, seqid));
66247c478bd9Sstevel@tonic-gate 		return (NFS4_CHKSEQ_BAD);
66257c478bd9Sstevel@tonic-gate 	}
66267c478bd9Sstevel@tonic-gate 
66277c478bd9Sstevel@tonic-gate 	/* Everything okay -- next expected */
66287c478bd9Sstevel@tonic-gate 	return (NFS4_CHKSEQ_OKAY);
66297c478bd9Sstevel@tonic-gate }
66307c478bd9Sstevel@tonic-gate 
66317c478bd9Sstevel@tonic-gate 
66327c478bd9Sstevel@tonic-gate static rfs4_chkseq_t
66337c478bd9Sstevel@tonic-gate rfs4_check_open_seqid(seqid4 seqid, rfs4_openowner_t *op, nfs_resop4 *resop)
66347c478bd9Sstevel@tonic-gate {
66357c478bd9Sstevel@tonic-gate 	rfs4_chkseq_t rc;
66367c478bd9Sstevel@tonic-gate 
66377c478bd9Sstevel@tonic-gate 	rfs4_dbe_lock(op->dbe);
66387c478bd9Sstevel@tonic-gate 	rc = rfs4_check_seqid(op->open_seqid, op->reply, seqid, resop, TRUE);
66397c478bd9Sstevel@tonic-gate 	rfs4_dbe_unlock(op->dbe);
66407c478bd9Sstevel@tonic-gate 
66417c478bd9Sstevel@tonic-gate 	if (rc == NFS4_CHKSEQ_OKAY)
66427c478bd9Sstevel@tonic-gate 		rfs4_update_lease(op->client);
66437c478bd9Sstevel@tonic-gate 
66447c478bd9Sstevel@tonic-gate 	return (rc);
66457c478bd9Sstevel@tonic-gate }
66467c478bd9Sstevel@tonic-gate 
66477c478bd9Sstevel@tonic-gate static rfs4_chkseq_t
66487c478bd9Sstevel@tonic-gate rfs4_check_olo_seqid(seqid4 olo_seqid, rfs4_openowner_t *op,
66497c478bd9Sstevel@tonic-gate 	nfs_resop4 *resop)
66507c478bd9Sstevel@tonic-gate {
66517c478bd9Sstevel@tonic-gate 	rfs4_chkseq_t rc;
66527c478bd9Sstevel@tonic-gate 
66537c478bd9Sstevel@tonic-gate 	rfs4_dbe_lock(op->dbe);
66547c478bd9Sstevel@tonic-gate 	rc = rfs4_check_seqid(op->open_seqid, op->reply,
66557c478bd9Sstevel@tonic-gate 		olo_seqid, resop, FALSE);
66567c478bd9Sstevel@tonic-gate 	rfs4_dbe_unlock(op->dbe);
66577c478bd9Sstevel@tonic-gate 
66587c478bd9Sstevel@tonic-gate 	return (rc);
66597c478bd9Sstevel@tonic-gate }
66607c478bd9Sstevel@tonic-gate 
66617c478bd9Sstevel@tonic-gate static rfs4_chkseq_t
66627c478bd9Sstevel@tonic-gate rfs4_check_lock_seqid(seqid4 seqid, rfs4_lo_state_t *lp, nfs_resop4 *resop)
66637c478bd9Sstevel@tonic-gate {
66647c478bd9Sstevel@tonic-gate 	rfs4_chkseq_t rc = NFS4_CHKSEQ_OKAY;
66657c478bd9Sstevel@tonic-gate 
66667c478bd9Sstevel@tonic-gate 	rfs4_dbe_lock(lp->dbe);
66677c478bd9Sstevel@tonic-gate 	if (!lp->skip_seqid_check)
66687c478bd9Sstevel@tonic-gate 		rc = rfs4_check_seqid(lp->seqid, lp->reply,
66697c478bd9Sstevel@tonic-gate 			seqid, resop, TRUE);
66707c478bd9Sstevel@tonic-gate 	rfs4_dbe_unlock(lp->dbe);
66717c478bd9Sstevel@tonic-gate 
66727c478bd9Sstevel@tonic-gate 	return (rc);
66737c478bd9Sstevel@tonic-gate }
66747c478bd9Sstevel@tonic-gate 
66757c478bd9Sstevel@tonic-gate static void
66767c478bd9Sstevel@tonic-gate rfs4_op_open(nfs_argop4 *argop, nfs_resop4 *resop,
66777c478bd9Sstevel@tonic-gate 	    struct svc_req *req, struct compound_state *cs)
66787c478bd9Sstevel@tonic-gate {
66797c478bd9Sstevel@tonic-gate 	OPEN4args *args = &argop->nfs_argop4_u.opopen;
66807c478bd9Sstevel@tonic-gate 	OPEN4res *resp = &resop->nfs_resop4_u.opopen;
66817c478bd9Sstevel@tonic-gate 	open_owner4 *owner = &args->owner;
66827c478bd9Sstevel@tonic-gate 	open_claim_type4 claim = args->claim;
66837c478bd9Sstevel@tonic-gate 	rfs4_client_t *cp;
66847c478bd9Sstevel@tonic-gate 	rfs4_openowner_t *oo;
66857c478bd9Sstevel@tonic-gate 	bool_t create;
66867c478bd9Sstevel@tonic-gate 	bool_t replay = FALSE;
66877c478bd9Sstevel@tonic-gate 	int can_reclaim;
66887c478bd9Sstevel@tonic-gate 
66897c478bd9Sstevel@tonic-gate 
66907c478bd9Sstevel@tonic-gate 	if (cs->vp == NULL) {
66917c478bd9Sstevel@tonic-gate 		*cs->statusp = resp->status = NFS4ERR_NOFILEHANDLE;
66927c478bd9Sstevel@tonic-gate 		return;
66937c478bd9Sstevel@tonic-gate 	}
66947c478bd9Sstevel@tonic-gate 
66957c478bd9Sstevel@tonic-gate 	/*
66967c478bd9Sstevel@tonic-gate 	 * Need to check clientid and lease expiration first based on
66977c478bd9Sstevel@tonic-gate 	 * error ordering and incrementing sequence id.
66987c478bd9Sstevel@tonic-gate 	 */
66997c478bd9Sstevel@tonic-gate 	cp = rfs4_findclient_by_id(owner->clientid, FALSE);
67007c478bd9Sstevel@tonic-gate 	if (cp == NULL) {
67017c478bd9Sstevel@tonic-gate 		*cs->statusp = resp->status =
67027c478bd9Sstevel@tonic-gate 			rfs4_check_clientid(&owner->clientid, 0);
67037c478bd9Sstevel@tonic-gate 		return;
67047c478bd9Sstevel@tonic-gate 	}
67057c478bd9Sstevel@tonic-gate 
67067c478bd9Sstevel@tonic-gate 	if (rfs4_lease_expired(cp)) {
67077c478bd9Sstevel@tonic-gate 		rfs4_client_close(cp);
67087c478bd9Sstevel@tonic-gate 		*cs->statusp = resp->status = NFS4ERR_EXPIRED;
67097c478bd9Sstevel@tonic-gate 		return;
67107c478bd9Sstevel@tonic-gate 	}
67117c478bd9Sstevel@tonic-gate 	can_reclaim = cp->can_reclaim;
67127c478bd9Sstevel@tonic-gate 
67137c478bd9Sstevel@tonic-gate 	/*
67147c478bd9Sstevel@tonic-gate 	 * Find the open_owner for use from this point forward.  Take
67157c478bd9Sstevel@tonic-gate 	 * care in updating the sequence id based on the type of error
67167c478bd9Sstevel@tonic-gate 	 * being returned.
67177c478bd9Sstevel@tonic-gate 	 */
67187c478bd9Sstevel@tonic-gate retry:
67197c478bd9Sstevel@tonic-gate 	create = TRUE;
67207c478bd9Sstevel@tonic-gate 	oo = rfs4_findopenowner(owner, &create, args->seqid);
67217c478bd9Sstevel@tonic-gate 	if (oo == NULL) {
67227c478bd9Sstevel@tonic-gate 		*cs->statusp = resp->status = NFS4ERR_STALE_CLIENTID;
67237c478bd9Sstevel@tonic-gate 		rfs4_client_rele(cp);
67247c478bd9Sstevel@tonic-gate 		return;
67257c478bd9Sstevel@tonic-gate 	}
67267c478bd9Sstevel@tonic-gate 
67277c478bd9Sstevel@tonic-gate 	/* Hold off access to the sequence space while the open is done */
67287c478bd9Sstevel@tonic-gate 	rfs4_sw_enter(&oo->oo_sw);
67297c478bd9Sstevel@tonic-gate 
67307c478bd9Sstevel@tonic-gate 	/*
67317c478bd9Sstevel@tonic-gate 	 * If the open_owner existed before at the server, then check
67327c478bd9Sstevel@tonic-gate 	 * the sequence id.
67337c478bd9Sstevel@tonic-gate 	 */
67347c478bd9Sstevel@tonic-gate 	if (!create && !oo->postpone_confirm) {
67357c478bd9Sstevel@tonic-gate 		switch (rfs4_check_open_seqid(args->seqid, oo, resop)) {
67367c478bd9Sstevel@tonic-gate 		case NFS4_CHKSEQ_BAD:
67377c478bd9Sstevel@tonic-gate 			if ((args->seqid > oo->open_seqid) &&
67387c478bd9Sstevel@tonic-gate 				oo->need_confirm) {
67397c478bd9Sstevel@tonic-gate 				rfs4_free_opens(oo, TRUE, FALSE);
67407c478bd9Sstevel@tonic-gate 				rfs4_sw_exit(&oo->oo_sw);
67417c478bd9Sstevel@tonic-gate 				rfs4_openowner_rele(oo);
67427c478bd9Sstevel@tonic-gate 				goto retry;
67437c478bd9Sstevel@tonic-gate 			}
67447c478bd9Sstevel@tonic-gate 			resp->status = NFS4ERR_BAD_SEQID;
67457c478bd9Sstevel@tonic-gate 			goto out;
67467c478bd9Sstevel@tonic-gate 		case NFS4_CHKSEQ_REPLAY: /* replay of previous request */
67477c478bd9Sstevel@tonic-gate 			replay = TRUE;
67487c478bd9Sstevel@tonic-gate 			goto out;
67497c478bd9Sstevel@tonic-gate 		default:
67507c478bd9Sstevel@tonic-gate 			break;
67517c478bd9Sstevel@tonic-gate 		}
67527c478bd9Sstevel@tonic-gate 
67537c478bd9Sstevel@tonic-gate 		/*
67547c478bd9Sstevel@tonic-gate 		 * Sequence was ok and open owner exists
67557c478bd9Sstevel@tonic-gate 		 * check to see if we have yet to see an
67567c478bd9Sstevel@tonic-gate 		 * open_confirm.
67577c478bd9Sstevel@tonic-gate 		 */
67587c478bd9Sstevel@tonic-gate 		if (oo->need_confirm) {
67597c478bd9Sstevel@tonic-gate 			rfs4_free_opens(oo, TRUE, FALSE);
67607c478bd9Sstevel@tonic-gate 			rfs4_sw_exit(&oo->oo_sw);
67617c478bd9Sstevel@tonic-gate 			rfs4_openowner_rele(oo);
67627c478bd9Sstevel@tonic-gate 			goto retry;
67637c478bd9Sstevel@tonic-gate 		}
67647c478bd9Sstevel@tonic-gate 	}
67657c478bd9Sstevel@tonic-gate 	/* Grace only applies to regular-type OPENs */
67667c478bd9Sstevel@tonic-gate 	if (rfs4_clnt_in_grace(cp) &&
67677c478bd9Sstevel@tonic-gate 	    (claim == CLAIM_NULL || claim == CLAIM_DELEGATE_CUR)) {
67687c478bd9Sstevel@tonic-gate 		*cs->statusp = resp->status = NFS4ERR_GRACE;
67697c478bd9Sstevel@tonic-gate 		goto out;
67707c478bd9Sstevel@tonic-gate 	}
67717c478bd9Sstevel@tonic-gate 
67727c478bd9Sstevel@tonic-gate 	/*
67737c478bd9Sstevel@tonic-gate 	 * If previous state at the server existed then can_reclaim
67747c478bd9Sstevel@tonic-gate 	 * will be set. If not reply NFS4ERR_NO_GRACE to the
67757c478bd9Sstevel@tonic-gate 	 * client.
67767c478bd9Sstevel@tonic-gate 	 */
67777c478bd9Sstevel@tonic-gate 	if (rfs4_clnt_in_grace(cp) && claim == CLAIM_PREVIOUS && !can_reclaim) {
67787c478bd9Sstevel@tonic-gate 		*cs->statusp = resp->status = NFS4ERR_NO_GRACE;
67797c478bd9Sstevel@tonic-gate 		goto out;
67807c478bd9Sstevel@tonic-gate 	}
67817c478bd9Sstevel@tonic-gate 
67827c478bd9Sstevel@tonic-gate 
67837c478bd9Sstevel@tonic-gate 	/*
67847c478bd9Sstevel@tonic-gate 	 * Reject the open if the client has missed the grace period
67857c478bd9Sstevel@tonic-gate 	 */
67867c478bd9Sstevel@tonic-gate 	if (!rfs4_clnt_in_grace(cp) && claim == CLAIM_PREVIOUS) {
67877c478bd9Sstevel@tonic-gate 		*cs->statusp = resp->status = NFS4ERR_NO_GRACE;
67887c478bd9Sstevel@tonic-gate 		goto out;
67897c478bd9Sstevel@tonic-gate 	}
67907c478bd9Sstevel@tonic-gate 
67917c478bd9Sstevel@tonic-gate 	/* Couple of up-front bookkeeping items */
67927c478bd9Sstevel@tonic-gate 	if (oo->need_confirm) {
67937c478bd9Sstevel@tonic-gate 		/*
67947c478bd9Sstevel@tonic-gate 		 * If this is a reclaim OPEN then we should not ask
67957c478bd9Sstevel@tonic-gate 		 * for a confirmation of the open_owner per the
67967c478bd9Sstevel@tonic-gate 		 * protocol specification.
67977c478bd9Sstevel@tonic-gate 		 */
67987c478bd9Sstevel@tonic-gate 		if (claim == CLAIM_PREVIOUS)
67997c478bd9Sstevel@tonic-gate 			oo->need_confirm = FALSE;
68007c478bd9Sstevel@tonic-gate 		else
68017c478bd9Sstevel@tonic-gate 			resp->rflags |= OPEN4_RESULT_CONFIRM;
68027c478bd9Sstevel@tonic-gate 	}
68037c478bd9Sstevel@tonic-gate 	resp->rflags |= OPEN4_RESULT_LOCKTYPE_POSIX;
68047c478bd9Sstevel@tonic-gate 
68057c478bd9Sstevel@tonic-gate 	/*
68067c478bd9Sstevel@tonic-gate 	 * If there is an unshared filesystem mounted on this vnode,
68077c478bd9Sstevel@tonic-gate 	 * do not allow to open/create in this directory.
68087c478bd9Sstevel@tonic-gate 	 */
68097c478bd9Sstevel@tonic-gate 	if (vn_ismntpt(cs->vp)) {
68107c478bd9Sstevel@tonic-gate 		*cs->statusp = resp->status = NFS4ERR_ACCESS;
68117c478bd9Sstevel@tonic-gate 		goto out;
68127c478bd9Sstevel@tonic-gate 	}
68137c478bd9Sstevel@tonic-gate 
68147c478bd9Sstevel@tonic-gate 	/*
68157c478bd9Sstevel@tonic-gate 	 * access must READ, WRITE, or BOTH.  No access is invalid.
68167c478bd9Sstevel@tonic-gate 	 * deny can be READ, WRITE, BOTH, or NONE.
68177c478bd9Sstevel@tonic-gate 	 * bits not defined for access/deny are invalid.
68187c478bd9Sstevel@tonic-gate 	 */
68197c478bd9Sstevel@tonic-gate 	if (! (args->share_access & OPEN4_SHARE_ACCESS_BOTH) ||
68207c478bd9Sstevel@tonic-gate 	    (args->share_access & ~OPEN4_SHARE_ACCESS_BOTH) ||
68217c478bd9Sstevel@tonic-gate 	    (args->share_deny & ~OPEN4_SHARE_DENY_BOTH)) {
68227c478bd9Sstevel@tonic-gate 		*cs->statusp = resp->status = NFS4ERR_INVAL;
68237c478bd9Sstevel@tonic-gate 		goto out;
68247c478bd9Sstevel@tonic-gate 	}
68257c478bd9Sstevel@tonic-gate 
68267c478bd9Sstevel@tonic-gate 
68277c478bd9Sstevel@tonic-gate 	/*
68287c478bd9Sstevel@tonic-gate 	 * make sure attrset is zero before response is built.
68297c478bd9Sstevel@tonic-gate 	 */
68307c478bd9Sstevel@tonic-gate 	resp->attrset = 0;
68317c478bd9Sstevel@tonic-gate 
68327c478bd9Sstevel@tonic-gate 	switch (claim) {
68337c478bd9Sstevel@tonic-gate 	case CLAIM_NULL:
68347c478bd9Sstevel@tonic-gate 		rfs4_do_opennull(cs, req, args, oo, resp);
68357c478bd9Sstevel@tonic-gate 	    break;
68367c478bd9Sstevel@tonic-gate 	case CLAIM_PREVIOUS:
68377c478bd9Sstevel@tonic-gate 		rfs4_do_openprev(cs, req, args, oo, resp);
68387c478bd9Sstevel@tonic-gate 	    break;
68397c478bd9Sstevel@tonic-gate 	case CLAIM_DELEGATE_CUR:
68407c478bd9Sstevel@tonic-gate 		rfs4_do_opendelcur(cs, req, args, oo, resp);
68417c478bd9Sstevel@tonic-gate 	    break;
68427c478bd9Sstevel@tonic-gate 	case CLAIM_DELEGATE_PREV:
68437c478bd9Sstevel@tonic-gate 		rfs4_do_opendelprev(cs, req, args, oo, resp);
68447c478bd9Sstevel@tonic-gate 	    break;
68457c478bd9Sstevel@tonic-gate 	default:
68467c478bd9Sstevel@tonic-gate 		resp->status = NFS4ERR_INVAL;
68477c478bd9Sstevel@tonic-gate 		break;
68487c478bd9Sstevel@tonic-gate 	}
68497c478bd9Sstevel@tonic-gate 
68507c478bd9Sstevel@tonic-gate out:
68517c478bd9Sstevel@tonic-gate 	rfs4_client_rele(cp);
68527c478bd9Sstevel@tonic-gate 
68537c478bd9Sstevel@tonic-gate 	/* Catch sequence id handling here to make it a little easier */
68547c478bd9Sstevel@tonic-gate 	switch (resp->status) {
68557c478bd9Sstevel@tonic-gate 	case NFS4ERR_BADXDR:
68567c478bd9Sstevel@tonic-gate 	case NFS4ERR_BAD_SEQID:
68577c478bd9Sstevel@tonic-gate 	case NFS4ERR_BAD_STATEID:
68587c478bd9Sstevel@tonic-gate 	case NFS4ERR_NOFILEHANDLE:
68597c478bd9Sstevel@tonic-gate 	case NFS4ERR_RESOURCE:
68607c478bd9Sstevel@tonic-gate 	case NFS4ERR_STALE_CLIENTID:
68617c478bd9Sstevel@tonic-gate 	case NFS4ERR_STALE_STATEID:
68627c478bd9Sstevel@tonic-gate 		/*
68637c478bd9Sstevel@tonic-gate 		 * The protocol states that if any of these errors are
68647c478bd9Sstevel@tonic-gate 		 * being returned, the sequence id should not be
68657c478bd9Sstevel@tonic-gate 		 * incremented.  Any other return requires an
68667c478bd9Sstevel@tonic-gate 		 * increment.
68677c478bd9Sstevel@tonic-gate 		 */
68687c478bd9Sstevel@tonic-gate 		break;
68697c478bd9Sstevel@tonic-gate 	default:
68707c478bd9Sstevel@tonic-gate 		/* Always update the lease in this case */
68717c478bd9Sstevel@tonic-gate 		rfs4_update_lease(oo->client);
68727c478bd9Sstevel@tonic-gate 
68737c478bd9Sstevel@tonic-gate 		/* Regular response - copy the result */
68747c478bd9Sstevel@tonic-gate 		if (!replay)
68757c478bd9Sstevel@tonic-gate 			rfs4_update_open_resp(oo, resop, &cs->fh);
68767c478bd9Sstevel@tonic-gate 
68777c478bd9Sstevel@tonic-gate 		/*
68787c478bd9Sstevel@tonic-gate 		 * REPLAY case: Only if the previous response was OK
68797c478bd9Sstevel@tonic-gate 		 * do we copy the filehandle.  If not OK, no
68807c478bd9Sstevel@tonic-gate 		 * filehandle to copy.
68817c478bd9Sstevel@tonic-gate 		 */
68827c478bd9Sstevel@tonic-gate 		if (replay == TRUE &&
68837c478bd9Sstevel@tonic-gate 		    resp->status == NFS4_OK &&
68847c478bd9Sstevel@tonic-gate 		    oo->reply_fh.nfs_fh4_val) {
68857c478bd9Sstevel@tonic-gate 			/*
68867c478bd9Sstevel@tonic-gate 			 * If this is a replay, we must restore the
68877c478bd9Sstevel@tonic-gate 			 * current filehandle/vp to that of what was
68887c478bd9Sstevel@tonic-gate 			 * returned originally.  Try our best to do
68897c478bd9Sstevel@tonic-gate 			 * it.
68907c478bd9Sstevel@tonic-gate 			 */
68917c478bd9Sstevel@tonic-gate 			nfs_fh4_fmt_t *fh_fmtp =
68927c478bd9Sstevel@tonic-gate 				(nfs_fh4_fmt_t *)oo->reply_fh.nfs_fh4_val;
68937c478bd9Sstevel@tonic-gate 
68947c478bd9Sstevel@tonic-gate 			cs->exi = checkexport4(&fh_fmtp->fh4_fsid,
68957c478bd9Sstevel@tonic-gate 				(fid_t *)&fh_fmtp->fh4_xlen, NULL);
68967c478bd9Sstevel@tonic-gate 
68977c478bd9Sstevel@tonic-gate 			if (cs->exi == NULL) {
68987c478bd9Sstevel@tonic-gate 				resp->status = NFS4ERR_STALE;
68997c478bd9Sstevel@tonic-gate 				goto finish;
69007c478bd9Sstevel@tonic-gate 			}
69017c478bd9Sstevel@tonic-gate 
69027c478bd9Sstevel@tonic-gate 			VN_RELE(cs->vp);
69037c478bd9Sstevel@tonic-gate 
69047c478bd9Sstevel@tonic-gate 			cs->vp = nfs4_fhtovp(&oo->reply_fh, cs->exi,
69057c478bd9Sstevel@tonic-gate 				&resp->status);
69067c478bd9Sstevel@tonic-gate 
69077c478bd9Sstevel@tonic-gate 			if (cs->vp == NULL)
69087c478bd9Sstevel@tonic-gate 				goto finish;
69097c478bd9Sstevel@tonic-gate 
69107c478bd9Sstevel@tonic-gate 			nfs_fh4_copy(&oo->reply_fh, &cs->fh);
69117c478bd9Sstevel@tonic-gate 		}
69127c478bd9Sstevel@tonic-gate 
69137c478bd9Sstevel@tonic-gate 		/*
69147c478bd9Sstevel@tonic-gate 		 * If this was a replay, no need to update the
69157c478bd9Sstevel@tonic-gate 		 * sequence id. If the open_owner was not created on
69167c478bd9Sstevel@tonic-gate 		 * this pass, then update.  The first use of an
69177c478bd9Sstevel@tonic-gate 		 * open_owner will not bump the sequence id.
69187c478bd9Sstevel@tonic-gate 		 */
69197c478bd9Sstevel@tonic-gate 		if (replay == FALSE && !create)
69207c478bd9Sstevel@tonic-gate 			rfs4_update_open_sequence(oo);
69217c478bd9Sstevel@tonic-gate 		/*
69227c478bd9Sstevel@tonic-gate 		 * If the client is receiving an error and the
69237c478bd9Sstevel@tonic-gate 		 * open_owner needs to be confirmed, there is no way
69247c478bd9Sstevel@tonic-gate 		 * to notify the client of this fact ignoring the fact
69257c478bd9Sstevel@tonic-gate 		 * that the server has no method of returning a
69267c478bd9Sstevel@tonic-gate 		 * stateid to confirm.  Therefore, the server needs to
69277c478bd9Sstevel@tonic-gate 		 * mark this open_owner in a way as to avoid the
69287c478bd9Sstevel@tonic-gate 		 * sequence id checking the next time the client uses
69297c478bd9Sstevel@tonic-gate 		 * this open_owner.
69307c478bd9Sstevel@tonic-gate 		 */
69317c478bd9Sstevel@tonic-gate 		if (resp->status != NFS4_OK && oo->need_confirm)
69327c478bd9Sstevel@tonic-gate 			oo->postpone_confirm = TRUE;
69337c478bd9Sstevel@tonic-gate 		/*
69347c478bd9Sstevel@tonic-gate 		 * If OK response then clear the postpone flag and
69357c478bd9Sstevel@tonic-gate 		 * reset the sequence id to keep in sync with the
69367c478bd9Sstevel@tonic-gate 		 * client.
69377c478bd9Sstevel@tonic-gate 		 */
69387c478bd9Sstevel@tonic-gate 		if (resp->status == NFS4_OK && oo->postpone_confirm) {
69397c478bd9Sstevel@tonic-gate 			oo->postpone_confirm = FALSE;
69407c478bd9Sstevel@tonic-gate 			oo->open_seqid = args->seqid;
69417c478bd9Sstevel@tonic-gate 		}
69427c478bd9Sstevel@tonic-gate 		break;
69437c478bd9Sstevel@tonic-gate 	}
69447c478bd9Sstevel@tonic-gate 
69457c478bd9Sstevel@tonic-gate finish:
69467c478bd9Sstevel@tonic-gate 	*cs->statusp = resp->status;
69477c478bd9Sstevel@tonic-gate 
69487c478bd9Sstevel@tonic-gate 	rfs4_sw_exit(&oo->oo_sw);
69497c478bd9Sstevel@tonic-gate 	rfs4_openowner_rele(oo);
69507c478bd9Sstevel@tonic-gate }
69517c478bd9Sstevel@tonic-gate 
69527c478bd9Sstevel@tonic-gate /*ARGSUSED*/
69537c478bd9Sstevel@tonic-gate void
69547c478bd9Sstevel@tonic-gate rfs4_op_open_confirm(nfs_argop4 *argop, nfs_resop4 *resop,
69557c478bd9Sstevel@tonic-gate 		    struct svc_req *req, struct compound_state *cs)
69567c478bd9Sstevel@tonic-gate {
69577c478bd9Sstevel@tonic-gate 	OPEN_CONFIRM4args *args = &argop->nfs_argop4_u.opopen_confirm;
69587c478bd9Sstevel@tonic-gate 	OPEN_CONFIRM4res *resp = &resop->nfs_resop4_u.opopen_confirm;
69597c478bd9Sstevel@tonic-gate 	rfs4_state_t *sp;
69607c478bd9Sstevel@tonic-gate 	nfsstat4 status;
69617c478bd9Sstevel@tonic-gate 
69627c478bd9Sstevel@tonic-gate 	if (cs->vp == NULL) {
69637c478bd9Sstevel@tonic-gate 		*cs->statusp = resp->status = NFS4ERR_NOFILEHANDLE;
69647c478bd9Sstevel@tonic-gate 		return;
69657c478bd9Sstevel@tonic-gate 	}
69667c478bd9Sstevel@tonic-gate 
69677c478bd9Sstevel@tonic-gate 	status = rfs4_get_state(&args->open_stateid, &sp, RFS4_DBS_VALID);
69687c478bd9Sstevel@tonic-gate 	if (status != NFS4_OK) {
69697c478bd9Sstevel@tonic-gate 		*cs->statusp = resp->status = status;
69707c478bd9Sstevel@tonic-gate 		return;
69717c478bd9Sstevel@tonic-gate 	}
69727c478bd9Sstevel@tonic-gate 
69737c478bd9Sstevel@tonic-gate 	/* Ensure specified filehandle matches */
69747c478bd9Sstevel@tonic-gate 	if (cs->vp != sp->finfo->vp) {
69757c478bd9Sstevel@tonic-gate 		rfs4_state_rele(sp);
69767c478bd9Sstevel@tonic-gate 		*cs->statusp = resp->status = NFS4ERR_BAD_STATEID;
69777c478bd9Sstevel@tonic-gate 		return;
69787c478bd9Sstevel@tonic-gate 	}
69797c478bd9Sstevel@tonic-gate 
69807c478bd9Sstevel@tonic-gate 	/* hold off other access to open_owner while we tinker */
69817c478bd9Sstevel@tonic-gate 	rfs4_sw_enter(&sp->owner->oo_sw);
69827c478bd9Sstevel@tonic-gate 
69837c478bd9Sstevel@tonic-gate 	switch (rfs4_check_stateid_seqid(sp, &args->open_stateid)) {
69847c478bd9Sstevel@tonic-gate 	case NFS4_CHECK_STATEID_OKAY:
69857c478bd9Sstevel@tonic-gate 		if (rfs4_check_open_seqid(args->seqid, sp->owner,
69867c478bd9Sstevel@tonic-gate 			resop) != 0) {
69877c478bd9Sstevel@tonic-gate 			*cs->statusp = resp->status = NFS4ERR_BAD_SEQID;
69887c478bd9Sstevel@tonic-gate 			break;
69897c478bd9Sstevel@tonic-gate 		}
69907c478bd9Sstevel@tonic-gate 		/*
69917c478bd9Sstevel@tonic-gate 		 * If it is the appropriate stateid and determined to
69927c478bd9Sstevel@tonic-gate 		 * be "OKAY" then this means that the stateid does not
69937c478bd9Sstevel@tonic-gate 		 * need to be confirmed and the client is in error for
69947c478bd9Sstevel@tonic-gate 		 * sending an OPEN_CONFIRM.
69957c478bd9Sstevel@tonic-gate 		 */
69967c478bd9Sstevel@tonic-gate 		*cs->statusp = resp->status = NFS4ERR_BAD_STATEID;
69977c478bd9Sstevel@tonic-gate 		break;
69987c478bd9Sstevel@tonic-gate 	case NFS4_CHECK_STATEID_OLD:
69997c478bd9Sstevel@tonic-gate 		*cs->statusp = resp->status = NFS4ERR_OLD_STATEID;
70007c478bd9Sstevel@tonic-gate 		break;
70017c478bd9Sstevel@tonic-gate 	case NFS4_CHECK_STATEID_BAD:
70027c478bd9Sstevel@tonic-gate 		*cs->statusp = resp->status = NFS4ERR_BAD_STATEID;
70037c478bd9Sstevel@tonic-gate 		break;
70047c478bd9Sstevel@tonic-gate 	case NFS4_CHECK_STATEID_EXPIRED:
70057c478bd9Sstevel@tonic-gate 		*cs->statusp = resp->status = NFS4ERR_EXPIRED;
70067c478bd9Sstevel@tonic-gate 		break;
70077c478bd9Sstevel@tonic-gate 	case NFS4_CHECK_STATEID_CLOSED:
70087c478bd9Sstevel@tonic-gate 		*cs->statusp = resp->status = NFS4ERR_OLD_STATEID;
70097c478bd9Sstevel@tonic-gate 		break;
70107c478bd9Sstevel@tonic-gate 	case NFS4_CHECK_STATEID_REPLAY:
70117c478bd9Sstevel@tonic-gate 		switch (rfs4_check_open_seqid(args->seqid, sp->owner, resop)) {
70127c478bd9Sstevel@tonic-gate 		case NFS4_CHKSEQ_OKAY:
70137c478bd9Sstevel@tonic-gate 			/*
70147c478bd9Sstevel@tonic-gate 			 * This is replayed stateid; if seqid matches
70157c478bd9Sstevel@tonic-gate 			 * next expected, then client is using wrong seqid.
70167c478bd9Sstevel@tonic-gate 			 */
70177c478bd9Sstevel@tonic-gate 			/* fall through */
70187c478bd9Sstevel@tonic-gate 		case NFS4_CHKSEQ_BAD:
70197c478bd9Sstevel@tonic-gate 			*cs->statusp = resp->status = NFS4ERR_BAD_SEQID;
70207c478bd9Sstevel@tonic-gate 			break;
70217c478bd9Sstevel@tonic-gate 		case NFS4_CHKSEQ_REPLAY:
70227c478bd9Sstevel@tonic-gate 			/*
70237c478bd9Sstevel@tonic-gate 			 * Note this case is the duplicate case so
70247c478bd9Sstevel@tonic-gate 			 * resp->status is already set.
70257c478bd9Sstevel@tonic-gate 			 */
70267c478bd9Sstevel@tonic-gate 			*cs->statusp = resp->status;
70277c478bd9Sstevel@tonic-gate 			rfs4_update_lease(sp->owner->client);
70287c478bd9Sstevel@tonic-gate 			break;
70297c478bd9Sstevel@tonic-gate 		}
70307c478bd9Sstevel@tonic-gate 		break;
70317c478bd9Sstevel@tonic-gate 	case NFS4_CHECK_STATEID_UNCONFIRMED:
70327c478bd9Sstevel@tonic-gate 		if (rfs4_check_open_seqid(args->seqid, sp->owner,
70337c478bd9Sstevel@tonic-gate 			resop) != NFS4_CHKSEQ_OKAY) {
70347c478bd9Sstevel@tonic-gate 			*cs->statusp = resp->status = NFS4ERR_BAD_SEQID;
70357c478bd9Sstevel@tonic-gate 			break;
70367c478bd9Sstevel@tonic-gate 		}
70377c478bd9Sstevel@tonic-gate 		*cs->statusp = resp->status = NFS4_OK;
70387c478bd9Sstevel@tonic-gate 
70397c478bd9Sstevel@tonic-gate 		next_stateid(&sp->stateid);
70407c478bd9Sstevel@tonic-gate 		resp->open_stateid = sp->stateid.stateid;
70417c478bd9Sstevel@tonic-gate 		sp->owner->need_confirm = FALSE;
70427c478bd9Sstevel@tonic-gate 		rfs4_update_lease(sp->owner->client);
70437c478bd9Sstevel@tonic-gate 		rfs4_update_open_sequence(sp->owner);
70447c478bd9Sstevel@tonic-gate 		rfs4_update_open_resp(sp->owner, resop, NULL);
70457c478bd9Sstevel@tonic-gate 		break;
70467c478bd9Sstevel@tonic-gate 	default:
70477c478bd9Sstevel@tonic-gate 		ASSERT(FALSE);
70487c478bd9Sstevel@tonic-gate 		*cs->statusp = resp->status = NFS4ERR_SERVERFAULT;
70497c478bd9Sstevel@tonic-gate 		break;
70507c478bd9Sstevel@tonic-gate 	}
70517c478bd9Sstevel@tonic-gate 	rfs4_sw_exit(&sp->owner->oo_sw);
70527c478bd9Sstevel@tonic-gate 	rfs4_state_rele(sp);
70537c478bd9Sstevel@tonic-gate }
70547c478bd9Sstevel@tonic-gate 
70557c478bd9Sstevel@tonic-gate /*ARGSUSED*/
70567c478bd9Sstevel@tonic-gate void
70577c478bd9Sstevel@tonic-gate rfs4_op_open_downgrade(nfs_argop4 *argop, nfs_resop4 *resop,
70587c478bd9Sstevel@tonic-gate 		    struct svc_req *req, struct compound_state *cs)
70597c478bd9Sstevel@tonic-gate {
70607c478bd9Sstevel@tonic-gate 	OPEN_DOWNGRADE4args *args = &argop->nfs_argop4_u.opopen_downgrade;
70617c478bd9Sstevel@tonic-gate 	OPEN_DOWNGRADE4res *resp = &resop->nfs_resop4_u.opopen_downgrade;
70627c478bd9Sstevel@tonic-gate 	uint32_t access = args->share_access;
70637c478bd9Sstevel@tonic-gate 	uint32_t deny = args->share_deny;
70647c478bd9Sstevel@tonic-gate 	nfsstat4 status;
70657c478bd9Sstevel@tonic-gate 	rfs4_state_t *sp;
70667c478bd9Sstevel@tonic-gate 	rfs4_file_t *fp;
70677c478bd9Sstevel@tonic-gate 
70687c478bd9Sstevel@tonic-gate 	if (cs->vp == NULL) {
70697c478bd9Sstevel@tonic-gate 		*cs->statusp = resp->status = NFS4ERR_NOFILEHANDLE;
70707c478bd9Sstevel@tonic-gate 		return;
70717c478bd9Sstevel@tonic-gate 	}
70727c478bd9Sstevel@tonic-gate 
70737c478bd9Sstevel@tonic-gate 	status = rfs4_get_state(&args->open_stateid, &sp, RFS4_DBS_VALID);
70747c478bd9Sstevel@tonic-gate 	if (status != NFS4_OK) {
70757c478bd9Sstevel@tonic-gate 		*cs->statusp = resp->status = status;
70767c478bd9Sstevel@tonic-gate 		return;
70777c478bd9Sstevel@tonic-gate 	}
70787c478bd9Sstevel@tonic-gate 
70797c478bd9Sstevel@tonic-gate 	/* Ensure specified filehandle matches */
70807c478bd9Sstevel@tonic-gate 	if (cs->vp != sp->finfo->vp) {
70817c478bd9Sstevel@tonic-gate 		rfs4_state_rele(sp);
70827c478bd9Sstevel@tonic-gate 		*cs->statusp = resp->status = NFS4ERR_BAD_STATEID;
70837c478bd9Sstevel@tonic-gate 		return;
70847c478bd9Sstevel@tonic-gate 	}
70857c478bd9Sstevel@tonic-gate 
70867c478bd9Sstevel@tonic-gate 	/* hold off other access to open_owner while we tinker */
70877c478bd9Sstevel@tonic-gate 	rfs4_sw_enter(&sp->owner->oo_sw);
70887c478bd9Sstevel@tonic-gate 
70897c478bd9Sstevel@tonic-gate 	switch (rfs4_check_stateid_seqid(sp, &args->open_stateid)) {
70907c478bd9Sstevel@tonic-gate 	case NFS4_CHECK_STATEID_OKAY:
70917c478bd9Sstevel@tonic-gate 		if (rfs4_check_open_seqid(args->seqid, sp->owner,
70927c478bd9Sstevel@tonic-gate 			resop) != NFS4_CHKSEQ_OKAY) {
70937c478bd9Sstevel@tonic-gate 			*cs->statusp = resp->status = NFS4ERR_BAD_SEQID;
70947c478bd9Sstevel@tonic-gate 			goto end;
70957c478bd9Sstevel@tonic-gate 		}
70967c478bd9Sstevel@tonic-gate 		break;
70977c478bd9Sstevel@tonic-gate 	case NFS4_CHECK_STATEID_OLD:
70987c478bd9Sstevel@tonic-gate 		*cs->statusp = resp->status = NFS4ERR_OLD_STATEID;
70997c478bd9Sstevel@tonic-gate 		goto end;
71007c478bd9Sstevel@tonic-gate 	case NFS4_CHECK_STATEID_BAD:
71017c478bd9Sstevel@tonic-gate 		*cs->statusp = resp->status = NFS4ERR_BAD_STATEID;
71027c478bd9Sstevel@tonic-gate 		goto end;
71037c478bd9Sstevel@tonic-gate 	case NFS4_CHECK_STATEID_EXPIRED:
71047c478bd9Sstevel@tonic-gate 		*cs->statusp = resp->status = NFS4ERR_EXPIRED;
71057c478bd9Sstevel@tonic-gate 		goto end;
71067c478bd9Sstevel@tonic-gate 	case NFS4_CHECK_STATEID_CLOSED:
71077c478bd9Sstevel@tonic-gate 		*cs->statusp = resp->status = NFS4ERR_OLD_STATEID;
71087c478bd9Sstevel@tonic-gate 		goto end;
71097c478bd9Sstevel@tonic-gate 	case NFS4_CHECK_STATEID_UNCONFIRMED:
71107c478bd9Sstevel@tonic-gate 		*cs->statusp = resp->status = NFS4ERR_BAD_STATEID;
71117c478bd9Sstevel@tonic-gate 		goto end;
71127c478bd9Sstevel@tonic-gate 	case NFS4_CHECK_STATEID_REPLAY:
71137c478bd9Sstevel@tonic-gate 		/* Check the sequence id for the open owner */
71147c478bd9Sstevel@tonic-gate 		switch (rfs4_check_open_seqid(args->seqid, sp->owner, resop)) {
71157c478bd9Sstevel@tonic-gate 		case NFS4_CHKSEQ_OKAY:
71167c478bd9Sstevel@tonic-gate 			/*
71177c478bd9Sstevel@tonic-gate 			 * This is replayed stateid; if seqid matches
71187c478bd9Sstevel@tonic-gate 			 * next expected, then client is using wrong seqid.
71197c478bd9Sstevel@tonic-gate 			 */
71207c478bd9Sstevel@tonic-gate 			/* fall through */
71217c478bd9Sstevel@tonic-gate 		case NFS4_CHKSEQ_BAD:
71227c478bd9Sstevel@tonic-gate 			*cs->statusp = resp->status = NFS4ERR_BAD_SEQID;
71237c478bd9Sstevel@tonic-gate 			goto end;
71247c478bd9Sstevel@tonic-gate 		case NFS4_CHKSEQ_REPLAY:
71257c478bd9Sstevel@tonic-gate 			/*
71267c478bd9Sstevel@tonic-gate 			 * Note this case is the duplicate case so
71277c478bd9Sstevel@tonic-gate 			 * resp->status is already set.
71287c478bd9Sstevel@tonic-gate 			 */
71297c478bd9Sstevel@tonic-gate 			*cs->statusp = resp->status;
71307c478bd9Sstevel@tonic-gate 			rfs4_update_lease(sp->owner->client);
71317c478bd9Sstevel@tonic-gate 			goto end;
71327c478bd9Sstevel@tonic-gate 		}
71337c478bd9Sstevel@tonic-gate 		break;
71347c478bd9Sstevel@tonic-gate 	default:
71357c478bd9Sstevel@tonic-gate 		ASSERT(FALSE);
71367c478bd9Sstevel@tonic-gate 		break;
71377c478bd9Sstevel@tonic-gate 	}
71387c478bd9Sstevel@tonic-gate 
71397c478bd9Sstevel@tonic-gate 	rfs4_dbe_lock(sp->dbe);
71407c478bd9Sstevel@tonic-gate 	/*
71417c478bd9Sstevel@tonic-gate 	 * Check that the new access modes and deny modes are valid.
71427c478bd9Sstevel@tonic-gate 	 * Check that no invalid bits are set.
71437c478bd9Sstevel@tonic-gate 	 */
71447c478bd9Sstevel@tonic-gate 	if ((access & ~(OPEN4_SHARE_ACCESS_READ | OPEN4_SHARE_ACCESS_WRITE)) ||
71457c478bd9Sstevel@tonic-gate 	    (deny & ~(OPEN4_SHARE_DENY_READ | OPEN4_SHARE_DENY_READ))) {
71467c478bd9Sstevel@tonic-gate 		*cs->statusp = resp->status = NFS4ERR_INVAL;
71477c478bd9Sstevel@tonic-gate 		rfs4_update_open_sequence(sp->owner);
71487c478bd9Sstevel@tonic-gate 		rfs4_dbe_unlock(sp->dbe);
71497c478bd9Sstevel@tonic-gate 		goto end;
71507c478bd9Sstevel@tonic-gate 	}
71517c478bd9Sstevel@tonic-gate 
71527c478bd9Sstevel@tonic-gate 	/*
71537c478bd9Sstevel@tonic-gate 	 * The new modes must be a subset of the current modes and
71547c478bd9Sstevel@tonic-gate 	 * the access must specify at least one mode. To test that
71557c478bd9Sstevel@tonic-gate 	 * the new mode is a subset of the current modes we bitwise
71567c478bd9Sstevel@tonic-gate 	 * AND them together and check that the result equals the new
71577c478bd9Sstevel@tonic-gate 	 * mode. For example:
71587c478bd9Sstevel@tonic-gate 	 * New mode, access == R and current mode, sp->share_access  == RW
71597c478bd9Sstevel@tonic-gate 	 * access & sp->share_access == R == access, so the new access mode
71607c478bd9Sstevel@tonic-gate 	 * is valid. Consider access == RW, sp->share_access = R
71617c478bd9Sstevel@tonic-gate 	 * access & sp->share_access == R != access, so the new access mode
71627c478bd9Sstevel@tonic-gate 	 * is invalid.
71637c478bd9Sstevel@tonic-gate 	 */
71647c478bd9Sstevel@tonic-gate 	if ((access & sp->share_access) != access ||
71657c478bd9Sstevel@tonic-gate 	    (deny & sp->share_deny) != deny ||
71667c478bd9Sstevel@tonic-gate 	    (access &
71677c478bd9Sstevel@tonic-gate 	    (OPEN4_SHARE_ACCESS_READ | OPEN4_SHARE_ACCESS_WRITE)) == 0) {
71687c478bd9Sstevel@tonic-gate 		*cs->statusp = resp->status = NFS4ERR_INVAL;
71697c478bd9Sstevel@tonic-gate 		rfs4_update_open_sequence(sp->owner);
71707c478bd9Sstevel@tonic-gate 		rfs4_dbe_unlock(sp->dbe);
71717c478bd9Sstevel@tonic-gate 		goto end;
71727c478bd9Sstevel@tonic-gate 	}
71737c478bd9Sstevel@tonic-gate 
71747c478bd9Sstevel@tonic-gate 	/*
71757c478bd9Sstevel@tonic-gate 	 * Release any share locks associated with this stateID.
71767c478bd9Sstevel@tonic-gate 	 * Strictly speaking, this violates the spec because the
71777c478bd9Sstevel@tonic-gate 	 * spec effectively requires that open downgrade be atomic.
71787c478bd9Sstevel@tonic-gate 	 * At present, fs_shrlock does not have this capability.
71797c478bd9Sstevel@tonic-gate 	 */
71807c478bd9Sstevel@tonic-gate 	rfs4_dbe_unlock(sp->dbe);
71817c478bd9Sstevel@tonic-gate 	rfs4_unshare(sp);
71827c478bd9Sstevel@tonic-gate 	rfs4_dbe_lock(sp->dbe);
71837c478bd9Sstevel@tonic-gate 
71847c478bd9Sstevel@tonic-gate 	fp = sp->finfo;
71857c478bd9Sstevel@tonic-gate 	rfs4_dbe_lock(fp->dbe);
71867c478bd9Sstevel@tonic-gate 
71877c478bd9Sstevel@tonic-gate 	/*
71887c478bd9Sstevel@tonic-gate 	 * If the current mode has deny read and the new mode
71897c478bd9Sstevel@tonic-gate 	 * does not, decrement the number of deny read mode bits
71907c478bd9Sstevel@tonic-gate 	 * and if it goes to zero turn off the deny read bit
71917c478bd9Sstevel@tonic-gate 	 * on the file.
71927c478bd9Sstevel@tonic-gate 	 */
71937c478bd9Sstevel@tonic-gate 	if ((sp->share_deny & OPEN4_SHARE_DENY_READ) &&
71947c478bd9Sstevel@tonic-gate 	    (deny & OPEN4_SHARE_DENY_READ) == 0) {
71957c478bd9Sstevel@tonic-gate 		fp->deny_read--;
71967c478bd9Sstevel@tonic-gate 		if (fp->deny_read == 0)
71977c478bd9Sstevel@tonic-gate 			fp->share_deny &= ~OPEN4_SHARE_DENY_READ;
71987c478bd9Sstevel@tonic-gate 	}
71997c478bd9Sstevel@tonic-gate 
72007c478bd9Sstevel@tonic-gate 	/*
72017c478bd9Sstevel@tonic-gate 	 * If the current mode has deny write and the new mode
72027c478bd9Sstevel@tonic-gate 	 * does not, decrement the number of deny write mode bits
72037c478bd9Sstevel@tonic-gate 	 * and if it goes to zero turn off the deny write bit
72047c478bd9Sstevel@tonic-gate 	 * on the file.
72057c478bd9Sstevel@tonic-gate 	 */
72067c478bd9Sstevel@tonic-gate 	if ((sp->share_deny & OPEN4_SHARE_DENY_WRITE) &&
72077c478bd9Sstevel@tonic-gate 	    (deny & OPEN4_SHARE_DENY_WRITE) == 0) {
72087c478bd9Sstevel@tonic-gate 		fp->deny_write--;
72097c478bd9Sstevel@tonic-gate 		if (fp->deny_write == 0)
72107c478bd9Sstevel@tonic-gate 			fp->share_deny &= ~OPEN4_SHARE_DENY_WRITE;
72117c478bd9Sstevel@tonic-gate 	}
72127c478bd9Sstevel@tonic-gate 
72137c478bd9Sstevel@tonic-gate 	/*
72147c478bd9Sstevel@tonic-gate 	 * If the current mode has access read and the new mode
72157c478bd9Sstevel@tonic-gate 	 * does not, decrement the number of access read mode bits
72167c478bd9Sstevel@tonic-gate 	 * and if it goes to zero turn off the access read bit
72177c478bd9Sstevel@tonic-gate 	 * on the file.
72187c478bd9Sstevel@tonic-gate 	 */
72197c478bd9Sstevel@tonic-gate 	if ((sp->share_access & OPEN4_SHARE_ACCESS_READ) &&
72207c478bd9Sstevel@tonic-gate 	    (access & OPEN4_SHARE_ACCESS_READ) == 0) {
72217c478bd9Sstevel@tonic-gate 		fp->access_read--;
72227c478bd9Sstevel@tonic-gate 		if (fp->access_read == 0)
72237c478bd9Sstevel@tonic-gate 			fp->share_access &= ~OPEN4_SHARE_ACCESS_READ;
72247c478bd9Sstevel@tonic-gate 	}
72257c478bd9Sstevel@tonic-gate 
72267c478bd9Sstevel@tonic-gate 	/*
72277c478bd9Sstevel@tonic-gate 	 * If the current mode has access write and the new mode
72287c478bd9Sstevel@tonic-gate 	 * does not, decrement the number of access write mode bits
72297c478bd9Sstevel@tonic-gate 	 * and if it goes to zero turn off the access write bit
72307c478bd9Sstevel@tonic-gate 	 * on the file.
72317c478bd9Sstevel@tonic-gate 	 */
72327c478bd9Sstevel@tonic-gate 	if ((sp->share_access & OPEN4_SHARE_ACCESS_WRITE) &&
72337c478bd9Sstevel@tonic-gate 	    (access & OPEN4_SHARE_ACCESS_WRITE) == 0) {
72347c478bd9Sstevel@tonic-gate 		fp->access_write--;
72357c478bd9Sstevel@tonic-gate 		if (fp->access_write == 0)
72367c478bd9Sstevel@tonic-gate 			fp->share_deny &= ~OPEN4_SHARE_ACCESS_WRITE;
72377c478bd9Sstevel@tonic-gate 	}
72387c478bd9Sstevel@tonic-gate 
72397c478bd9Sstevel@tonic-gate 	/* Set the new access and deny modes */
72407c478bd9Sstevel@tonic-gate 	sp->share_access = access;
72417c478bd9Sstevel@tonic-gate 	sp->share_deny = deny;
72427c478bd9Sstevel@tonic-gate 	/* Check that the file is still accessible */
72437c478bd9Sstevel@tonic-gate 	ASSERT(fp->share_access);
72447c478bd9Sstevel@tonic-gate 
72457c478bd9Sstevel@tonic-gate 	rfs4_dbe_unlock(fp->dbe);
72467c478bd9Sstevel@tonic-gate 
72477c478bd9Sstevel@tonic-gate 	rfs4_dbe_unlock(sp->dbe);
72487c478bd9Sstevel@tonic-gate 	if ((status = rfs4_share(sp)) != NFS4_OK) {
72497c478bd9Sstevel@tonic-gate 		*cs->statusp = resp->status = NFS4ERR_SERVERFAULT;
72507c478bd9Sstevel@tonic-gate 		rfs4_update_open_sequence(sp->owner);
72517c478bd9Sstevel@tonic-gate 		goto end;
72527c478bd9Sstevel@tonic-gate 	}
72537c478bd9Sstevel@tonic-gate 
72547c478bd9Sstevel@tonic-gate 	rfs4_dbe_lock(sp->dbe);
72557c478bd9Sstevel@tonic-gate 
72567c478bd9Sstevel@tonic-gate 	/* Update the stateid */
72577c478bd9Sstevel@tonic-gate 	next_stateid(&sp->stateid);
72587c478bd9Sstevel@tonic-gate 	resp->open_stateid = sp->stateid.stateid;
72597c478bd9Sstevel@tonic-gate 
72607c478bd9Sstevel@tonic-gate 	rfs4_dbe_unlock(sp->dbe);
72617c478bd9Sstevel@tonic-gate 
72627c478bd9Sstevel@tonic-gate 	*cs->statusp = resp->status = NFS4_OK;
72637c478bd9Sstevel@tonic-gate 	/* Update the lease */
72647c478bd9Sstevel@tonic-gate 	rfs4_update_lease(sp->owner->client);
72657c478bd9Sstevel@tonic-gate 	/* And the sequence */
72667c478bd9Sstevel@tonic-gate 	rfs4_update_open_sequence(sp->owner);
72677c478bd9Sstevel@tonic-gate 	rfs4_update_open_resp(sp->owner, resop, NULL);
72687c478bd9Sstevel@tonic-gate 
72697c478bd9Sstevel@tonic-gate end:
72707c478bd9Sstevel@tonic-gate 	rfs4_sw_exit(&sp->owner->oo_sw);
72717c478bd9Sstevel@tonic-gate 	rfs4_state_rele(sp);
72727c478bd9Sstevel@tonic-gate }
72737c478bd9Sstevel@tonic-gate 
72747c478bd9Sstevel@tonic-gate /*
72757c478bd9Sstevel@tonic-gate  * The logic behind this function is detailed in the NFSv4 RFC in the
72767c478bd9Sstevel@tonic-gate  * SETCLIENTID operation description under IMPLEMENTATION.  Refer to
72777c478bd9Sstevel@tonic-gate  * that section for explicit guidance to server behavior for
72787c478bd9Sstevel@tonic-gate  * SETCLIENTID.
72797c478bd9Sstevel@tonic-gate  */
72807c478bd9Sstevel@tonic-gate void
72817c478bd9Sstevel@tonic-gate rfs4_op_setclientid(nfs_argop4 *argop, nfs_resop4 *resop,
72827c478bd9Sstevel@tonic-gate 		    struct svc_req *req, struct compound_state *cs)
72837c478bd9Sstevel@tonic-gate {
72847c478bd9Sstevel@tonic-gate 	SETCLIENTID4args *args = &argop->nfs_argop4_u.opsetclientid;
72857c478bd9Sstevel@tonic-gate 	SETCLIENTID4res *res = &resop->nfs_resop4_u.opsetclientid;
72867c478bd9Sstevel@tonic-gate 	rfs4_client_t *cp, *newcp, *cp_confirmed, *cp_unconfirmed;
72877c478bd9Sstevel@tonic-gate 	bool_t create = TRUE;
72887c478bd9Sstevel@tonic-gate 	char *addr, *netid;
72897c478bd9Sstevel@tonic-gate 	int len;
72907c478bd9Sstevel@tonic-gate 
72917c478bd9Sstevel@tonic-gate retry:
72927c478bd9Sstevel@tonic-gate 	newcp = cp_confirmed = cp_unconfirmed = NULL;
72937c478bd9Sstevel@tonic-gate 
72947c478bd9Sstevel@tonic-gate 	/*
72957c478bd9Sstevel@tonic-gate 	 * In search of an EXISTING client matching the incoming
72967c478bd9Sstevel@tonic-gate 	 * request to establish a new client identifier at the server
72977c478bd9Sstevel@tonic-gate 	 */
72987c478bd9Sstevel@tonic-gate 	create = TRUE;
72997c478bd9Sstevel@tonic-gate 	cp = rfs4_findclient(&args->client, &create, NULL);
73007c478bd9Sstevel@tonic-gate 
73017c478bd9Sstevel@tonic-gate 	/* Should never happen */
73027c478bd9Sstevel@tonic-gate 	ASSERT(cp != NULL);
73037c478bd9Sstevel@tonic-gate 
73047c478bd9Sstevel@tonic-gate 	if (cp == NULL) {
73057c478bd9Sstevel@tonic-gate 		*cs->statusp = res->status = NFS4ERR_SERVERFAULT;
73067c478bd9Sstevel@tonic-gate 		return;
73077c478bd9Sstevel@tonic-gate 	}
73087c478bd9Sstevel@tonic-gate 
73097c478bd9Sstevel@tonic-gate 	/*
73107c478bd9Sstevel@tonic-gate 	 * Easiest case. Client identifier is newly created and is
73117c478bd9Sstevel@tonic-gate 	 * unconfirmed.  Also note that for this case, no other
73127c478bd9Sstevel@tonic-gate 	 * entries exist for the client identifier.  Nothing else to
73137c478bd9Sstevel@tonic-gate 	 * check.  Just setup the response and respond.
73147c478bd9Sstevel@tonic-gate 	 */
73157c478bd9Sstevel@tonic-gate 	if (create) {
73167c478bd9Sstevel@tonic-gate 		*cs->statusp = res->status = NFS4_OK;
73177c478bd9Sstevel@tonic-gate 		res->SETCLIENTID4res_u.resok4.clientid = cp->clientid;
73187c478bd9Sstevel@tonic-gate 		res->SETCLIENTID4res_u.resok4.setclientid_confirm =
73197c478bd9Sstevel@tonic-gate 							cp->confirm_verf;
73207c478bd9Sstevel@tonic-gate 		/* Setup callback information; CB_NULL confirmation later */
73217c478bd9Sstevel@tonic-gate 		rfs4_client_setcb(cp, &args->callback, args->callback_ident);
73227c478bd9Sstevel@tonic-gate 
73237c478bd9Sstevel@tonic-gate 		rfs4_client_rele(cp);
73247c478bd9Sstevel@tonic-gate 		return;
73257c478bd9Sstevel@tonic-gate 	}
73267c478bd9Sstevel@tonic-gate 
73277c478bd9Sstevel@tonic-gate 	/*
73287c478bd9Sstevel@tonic-gate 	 * An existing, confirmed client may exist but it may not have
73297c478bd9Sstevel@tonic-gate 	 * been active for at least one lease period.  If so, then
73307c478bd9Sstevel@tonic-gate 	 * "close" the client and create a new client identifier
73317c478bd9Sstevel@tonic-gate 	 */
73327c478bd9Sstevel@tonic-gate 	if (rfs4_lease_expired(cp)) {
73337c478bd9Sstevel@tonic-gate 		rfs4_client_close(cp);
73347c478bd9Sstevel@tonic-gate 		goto retry;
73357c478bd9Sstevel@tonic-gate 	}
73367c478bd9Sstevel@tonic-gate 
73377c478bd9Sstevel@tonic-gate 	if (cp->need_confirm == TRUE)
73387c478bd9Sstevel@tonic-gate 		cp_unconfirmed = cp;
73397c478bd9Sstevel@tonic-gate 	else
73407c478bd9Sstevel@tonic-gate 		cp_confirmed = cp;
73417c478bd9Sstevel@tonic-gate 
73427c478bd9Sstevel@tonic-gate 	cp = NULL;
73437c478bd9Sstevel@tonic-gate 
73447c478bd9Sstevel@tonic-gate 	/*
73457c478bd9Sstevel@tonic-gate 	 * We have a confirmed client, now check for an
73467c478bd9Sstevel@tonic-gate 	 * unconfimred entry
73477c478bd9Sstevel@tonic-gate 	 */
73487c478bd9Sstevel@tonic-gate 	if (cp_confirmed) {
73497c478bd9Sstevel@tonic-gate 		/* If creds don't match then client identifier is inuse */
73507c478bd9Sstevel@tonic-gate 		if (!creds_ok(cp_confirmed->cr_set, req, cs)) {
73517c478bd9Sstevel@tonic-gate 			rfs4_cbinfo_t *cbp;
73527c478bd9Sstevel@tonic-gate 			/*
73537c478bd9Sstevel@tonic-gate 			 * Some one else has established this client
73547c478bd9Sstevel@tonic-gate 			 * id. Try and say * who they are. We will use
73557c478bd9Sstevel@tonic-gate 			 * the call back address supplied by * the
73567c478bd9Sstevel@tonic-gate 			 * first client.
73577c478bd9Sstevel@tonic-gate 			 */
73587c478bd9Sstevel@tonic-gate 			*cs->statusp = res->status = NFS4ERR_CLID_INUSE;
73597c478bd9Sstevel@tonic-gate 
73607c478bd9Sstevel@tonic-gate 			addr = netid = NULL;
73617c478bd9Sstevel@tonic-gate 
73627c478bd9Sstevel@tonic-gate 			cbp = &cp_confirmed->cbinfo;
73637c478bd9Sstevel@tonic-gate 			if (cbp->cb_callback.cb_location.r_addr &&
73647c478bd9Sstevel@tonic-gate 			    cbp->cb_callback.cb_location.r_netid) {
73657c478bd9Sstevel@tonic-gate 				cb_client4 *cbcp = &cbp->cb_callback;
73667c478bd9Sstevel@tonic-gate 
73677c478bd9Sstevel@tonic-gate 				len = strlen(cbcp->cb_location.r_addr)+1;
73687c478bd9Sstevel@tonic-gate 				addr = kmem_alloc(len, KM_SLEEP);
73697c478bd9Sstevel@tonic-gate 				bcopy(cbcp->cb_location.r_addr, addr, len);
73707c478bd9Sstevel@tonic-gate 				len = strlen(cbcp->cb_location.r_netid)+1;
73717c478bd9Sstevel@tonic-gate 				netid = kmem_alloc(len, KM_SLEEP);
73727c478bd9Sstevel@tonic-gate 				bcopy(cbcp->cb_location.r_netid, netid, len);
73737c478bd9Sstevel@tonic-gate 			}
73747c478bd9Sstevel@tonic-gate 
73757c478bd9Sstevel@tonic-gate 			res->SETCLIENTID4res_u.client_using.r_addr = addr;
73767c478bd9Sstevel@tonic-gate 			res->SETCLIENTID4res_u.client_using.r_netid = netid;
73777c478bd9Sstevel@tonic-gate 
73787c478bd9Sstevel@tonic-gate 			rfs4_client_rele(cp_confirmed);
73797c478bd9Sstevel@tonic-gate 		}
73807c478bd9Sstevel@tonic-gate 
73817c478bd9Sstevel@tonic-gate 		/*
73827c478bd9Sstevel@tonic-gate 		 * Confirmed, creds match, and verifier matches; must
73837c478bd9Sstevel@tonic-gate 		 * be an update of the callback info
73847c478bd9Sstevel@tonic-gate 		 */
73857c478bd9Sstevel@tonic-gate 		if (cp_confirmed->nfs_client.verifier ==
73867c478bd9Sstevel@tonic-gate 						args->client.verifier) {
73877c478bd9Sstevel@tonic-gate 			/* Setup callback information */
73887c478bd9Sstevel@tonic-gate 			rfs4_client_setcb(cp_confirmed, &args->callback,
73897c478bd9Sstevel@tonic-gate 						args->callback_ident);
73907c478bd9Sstevel@tonic-gate 
73917c478bd9Sstevel@tonic-gate 			/* everything okay -- move ahead */
73927c478bd9Sstevel@tonic-gate 			*cs->statusp = res->status = NFS4_OK;
73937c478bd9Sstevel@tonic-gate 			res->SETCLIENTID4res_u.resok4.clientid =
73947c478bd9Sstevel@tonic-gate 				cp_confirmed->clientid;
73957c478bd9Sstevel@tonic-gate 
73967c478bd9Sstevel@tonic-gate 			/* update the confirm_verifier and return it */
73977c478bd9Sstevel@tonic-gate 			rfs4_client_scv_next(cp_confirmed);
73987c478bd9Sstevel@tonic-gate 			res->SETCLIENTID4res_u.resok4.setclientid_confirm =
73997c478bd9Sstevel@tonic-gate 						cp_confirmed->confirm_verf;
74007c478bd9Sstevel@tonic-gate 
74017c478bd9Sstevel@tonic-gate 			rfs4_client_rele(cp_confirmed);
74027c478bd9Sstevel@tonic-gate 			return;
74037c478bd9Sstevel@tonic-gate 		}
74047c478bd9Sstevel@tonic-gate 
74057c478bd9Sstevel@tonic-gate 		/*
74067c478bd9Sstevel@tonic-gate 		 * Creds match but the verifier doesn't.  Must search
74077c478bd9Sstevel@tonic-gate 		 * for an unconfirmed client that would be replaced by
74087c478bd9Sstevel@tonic-gate 		 * this request.
74097c478bd9Sstevel@tonic-gate 		 */
74107c478bd9Sstevel@tonic-gate 		create = FALSE;
74117c478bd9Sstevel@tonic-gate 		cp_unconfirmed = rfs4_findclient(&args->client, &create,
74127c478bd9Sstevel@tonic-gate 						cp_confirmed);
74137c478bd9Sstevel@tonic-gate 	}
74147c478bd9Sstevel@tonic-gate 
74157c478bd9Sstevel@tonic-gate 	/*
74167c478bd9Sstevel@tonic-gate 	 * At this point, we have taken care of the brand new client
74177c478bd9Sstevel@tonic-gate 	 * struct, INUSE case, update of an existing, and confirmed
74187c478bd9Sstevel@tonic-gate 	 * client struct.
74197c478bd9Sstevel@tonic-gate 	 */
74207c478bd9Sstevel@tonic-gate 
74217c478bd9Sstevel@tonic-gate 	/*
74227c478bd9Sstevel@tonic-gate 	 * check to see if things have changed while we originally
74237c478bd9Sstevel@tonic-gate 	 * picked up the client struct.  If they have, then return and
74247c478bd9Sstevel@tonic-gate 	 * retry the processing of this SETCLIENTID request.
74257c478bd9Sstevel@tonic-gate 	 */
74267c478bd9Sstevel@tonic-gate 	if (cp_unconfirmed) {
74277c478bd9Sstevel@tonic-gate 		rfs4_dbe_lock(cp_unconfirmed->dbe);
74287c478bd9Sstevel@tonic-gate 		if (!cp_unconfirmed->need_confirm) {
74297c478bd9Sstevel@tonic-gate 			rfs4_dbe_unlock(cp_unconfirmed->dbe);
74307c478bd9Sstevel@tonic-gate 			rfs4_client_rele(cp_unconfirmed);
74317c478bd9Sstevel@tonic-gate 			if (cp_confirmed)
74327c478bd9Sstevel@tonic-gate 				rfs4_client_rele(cp_confirmed);
74337c478bd9Sstevel@tonic-gate 			goto retry;
74347c478bd9Sstevel@tonic-gate 		}
74357c478bd9Sstevel@tonic-gate 		/* do away with the old unconfirmed one */
74367c478bd9Sstevel@tonic-gate 		rfs4_dbe_invalidate(cp_unconfirmed->dbe);
74377c478bd9Sstevel@tonic-gate 		rfs4_dbe_unlock(cp_unconfirmed->dbe);
74387c478bd9Sstevel@tonic-gate 		rfs4_client_rele(cp_unconfirmed);
74397c478bd9Sstevel@tonic-gate 		cp_unconfirmed = NULL;
74407c478bd9Sstevel@tonic-gate 	}
74417c478bd9Sstevel@tonic-gate 
74427c478bd9Sstevel@tonic-gate 	/*
74437c478bd9Sstevel@tonic-gate 	 * This search will temporarily hide the confirmed client
74447c478bd9Sstevel@tonic-gate 	 * struct while a new client struct is created as the
74457c478bd9Sstevel@tonic-gate 	 * unconfirmed one.
74467c478bd9Sstevel@tonic-gate 	 */
74477c478bd9Sstevel@tonic-gate 	create = TRUE;
74487c478bd9Sstevel@tonic-gate 	newcp = rfs4_findclient(&args->client, &create, cp_confirmed);
74497c478bd9Sstevel@tonic-gate 
74507c478bd9Sstevel@tonic-gate 	ASSERT(newcp != NULL);
74517c478bd9Sstevel@tonic-gate 
74527c478bd9Sstevel@tonic-gate 	if (newcp == NULL) {
74537c478bd9Sstevel@tonic-gate 		*cs->statusp = res->status = NFS4ERR_SERVERFAULT;
74547c478bd9Sstevel@tonic-gate 		rfs4_client_rele(cp_confirmed);
74557c478bd9Sstevel@tonic-gate 		return;
74567c478bd9Sstevel@tonic-gate 	}
74577c478bd9Sstevel@tonic-gate 
74587c478bd9Sstevel@tonic-gate 	/*
74597c478bd9Sstevel@tonic-gate 	 * If one was not created, then a similar request must be in
74607c478bd9Sstevel@tonic-gate 	 * process so release and start over with this one
74617c478bd9Sstevel@tonic-gate 	 */
74627c478bd9Sstevel@tonic-gate 	if (create != TRUE) {
74637c478bd9Sstevel@tonic-gate 		rfs4_client_rele(newcp);
74647c478bd9Sstevel@tonic-gate 		if (cp_confirmed)
74657c478bd9Sstevel@tonic-gate 			rfs4_client_rele(cp_confirmed);
74667c478bd9Sstevel@tonic-gate 		goto retry;
74677c478bd9Sstevel@tonic-gate 	}
74687c478bd9Sstevel@tonic-gate 
74697c478bd9Sstevel@tonic-gate 	*cs->statusp = res->status = NFS4_OK;
74707c478bd9Sstevel@tonic-gate 	res->SETCLIENTID4res_u.resok4.clientid = newcp->clientid;
74717c478bd9Sstevel@tonic-gate 	res->SETCLIENTID4res_u.resok4.setclientid_confirm =
74727c478bd9Sstevel@tonic-gate 							newcp->confirm_verf;
74737c478bd9Sstevel@tonic-gate 	/* Setup callback information; CB_NULL confirmation later */
74747c478bd9Sstevel@tonic-gate 	rfs4_client_setcb(newcp, &args->callback,
74757c478bd9Sstevel@tonic-gate 				args->callback_ident);
74767c478bd9Sstevel@tonic-gate 
74777c478bd9Sstevel@tonic-gate 	newcp->cp_confirmed = cp_confirmed;
74787c478bd9Sstevel@tonic-gate 
74797c478bd9Sstevel@tonic-gate 	rfs4_client_rele(newcp);
74807c478bd9Sstevel@tonic-gate }
74817c478bd9Sstevel@tonic-gate 
74827c478bd9Sstevel@tonic-gate /*ARGSUSED*/
74837c478bd9Sstevel@tonic-gate void
74847c478bd9Sstevel@tonic-gate rfs4_op_setclientid_confirm(nfs_argop4 *argop, nfs_resop4 *resop,
74857c478bd9Sstevel@tonic-gate 			    struct svc_req *req, struct compound_state *cs)
74867c478bd9Sstevel@tonic-gate {
74877c478bd9Sstevel@tonic-gate 	SETCLIENTID_CONFIRM4args *args =
74887c478bd9Sstevel@tonic-gate 		&argop->nfs_argop4_u.opsetclientid_confirm;
74897c478bd9Sstevel@tonic-gate 	SETCLIENTID_CONFIRM4res *res =
74907c478bd9Sstevel@tonic-gate 		&resop->nfs_resop4_u.opsetclientid_confirm;
74917c478bd9Sstevel@tonic-gate 	rfs4_client_t *cp, *cptoclose = NULL;
74927c478bd9Sstevel@tonic-gate 
74937c478bd9Sstevel@tonic-gate 	*cs->statusp = res->status = NFS4_OK;
74947c478bd9Sstevel@tonic-gate 
74957c478bd9Sstevel@tonic-gate 	cp = rfs4_findclient_by_id(args->clientid, TRUE);
74967c478bd9Sstevel@tonic-gate 
74977c478bd9Sstevel@tonic-gate 	if (cp == NULL) {
74987c478bd9Sstevel@tonic-gate 		*cs->statusp = res->status =
74997c478bd9Sstevel@tonic-gate 			rfs4_check_clientid(&args->clientid, 1);
75007c478bd9Sstevel@tonic-gate 		return;
75017c478bd9Sstevel@tonic-gate 	}
75027c478bd9Sstevel@tonic-gate 
75037c478bd9Sstevel@tonic-gate 	if (!creds_ok(cp, req, cs)) {
75047c478bd9Sstevel@tonic-gate 		*cs->statusp = res->status = NFS4ERR_CLID_INUSE;
75057c478bd9Sstevel@tonic-gate 		rfs4_client_rele(cp);
75067c478bd9Sstevel@tonic-gate 		return;
75077c478bd9Sstevel@tonic-gate 	}
75087c478bd9Sstevel@tonic-gate 
75097c478bd9Sstevel@tonic-gate 	/* If the verifier doesn't match, the record doesn't match */
75107c478bd9Sstevel@tonic-gate 	if (cp->confirm_verf != args->setclientid_confirm) {
75117c478bd9Sstevel@tonic-gate 		*cs->statusp = res->status = NFS4ERR_STALE_CLIENTID;
75127c478bd9Sstevel@tonic-gate 		rfs4_client_rele(cp);
75137c478bd9Sstevel@tonic-gate 		return;
75147c478bd9Sstevel@tonic-gate 	}
75157c478bd9Sstevel@tonic-gate 
75167c478bd9Sstevel@tonic-gate 	rfs4_dbe_lock(cp->dbe);
75177c478bd9Sstevel@tonic-gate 	cp->need_confirm = FALSE;
75187c478bd9Sstevel@tonic-gate 	if (cp->cp_confirmed) {
75197c478bd9Sstevel@tonic-gate 		cptoclose = cp->cp_confirmed;
75207c478bd9Sstevel@tonic-gate 		cptoclose->ss_remove = 1;
75217c478bd9Sstevel@tonic-gate 		cp->cp_confirmed = NULL;
75227c478bd9Sstevel@tonic-gate 	}
75237c478bd9Sstevel@tonic-gate 
75247c478bd9Sstevel@tonic-gate 	/*
7525cee86682Scalum 	 * Update the client's associated server instance, if it's changed
7526cee86682Scalum 	 * since the client was created.
7527cee86682Scalum 	 */
7528cee86682Scalum 	if (rfs4_servinst(cp) != rfs4_cur_servinst)
7529cee86682Scalum 		rfs4_servinst_assign(cp, rfs4_cur_servinst);
7530cee86682Scalum 
7531cee86682Scalum 	/*
7532cee86682Scalum 	 * Record clientid in stable storage.
7533cee86682Scalum 	 * Must be done after server instance has been assigned.
75347c478bd9Sstevel@tonic-gate 	 */
75357c478bd9Sstevel@tonic-gate 	rfs4_ss_clid(cp, req);
75367c478bd9Sstevel@tonic-gate 
75377c478bd9Sstevel@tonic-gate 	rfs4_dbe_unlock(cp->dbe);
75387c478bd9Sstevel@tonic-gate 
75397c478bd9Sstevel@tonic-gate 	if (cptoclose)
75407c478bd9Sstevel@tonic-gate 		/* don't need to rele, client_close does it */
75417c478bd9Sstevel@tonic-gate 		rfs4_client_close(cptoclose);
75427c478bd9Sstevel@tonic-gate 
75437c478bd9Sstevel@tonic-gate 	/* If needed, initiate CB_NULL call for callback path */
75447c478bd9Sstevel@tonic-gate 	rfs4_deleg_cb_check(cp);
75457c478bd9Sstevel@tonic-gate 	rfs4_update_lease(cp);
75467c478bd9Sstevel@tonic-gate 
75477c478bd9Sstevel@tonic-gate 	/*
75487c478bd9Sstevel@tonic-gate 	 * Check to see if client can perform reclaims
75497c478bd9Sstevel@tonic-gate 	 */
75507c478bd9Sstevel@tonic-gate 	rfs4_ss_chkclid(cp);
75517c478bd9Sstevel@tonic-gate 
75527c478bd9Sstevel@tonic-gate 	rfs4_client_rele(cp);
75537c478bd9Sstevel@tonic-gate }
75547c478bd9Sstevel@tonic-gate 
75557c478bd9Sstevel@tonic-gate 
75567c478bd9Sstevel@tonic-gate /*ARGSUSED*/
75577c478bd9Sstevel@tonic-gate void
75587c478bd9Sstevel@tonic-gate rfs4_op_close(nfs_argop4 *argop, nfs_resop4 *resop,
75597c478bd9Sstevel@tonic-gate 	    struct svc_req *req, struct compound_state *cs)
75607c478bd9Sstevel@tonic-gate {
75617c478bd9Sstevel@tonic-gate 	/* XXX Currently not using req arg */
75627c478bd9Sstevel@tonic-gate 	CLOSE4args *args = &argop->nfs_argop4_u.opclose;
75637c478bd9Sstevel@tonic-gate 	CLOSE4res *resp = &resop->nfs_resop4_u.opclose;
75647c478bd9Sstevel@tonic-gate 	rfs4_state_t *sp;
75657c478bd9Sstevel@tonic-gate 	nfsstat4 status;
75667c478bd9Sstevel@tonic-gate 
75677c478bd9Sstevel@tonic-gate 	if (cs->vp == NULL) {
75687c478bd9Sstevel@tonic-gate 		*cs->statusp = resp->status = NFS4ERR_NOFILEHANDLE;
75697c478bd9Sstevel@tonic-gate 		return;
75707c478bd9Sstevel@tonic-gate 	}
75717c478bd9Sstevel@tonic-gate 
75727c478bd9Sstevel@tonic-gate 	status = rfs4_get_state(&args->open_stateid, &sp, RFS4_DBS_INVALID);
75737c478bd9Sstevel@tonic-gate 	if (status != NFS4_OK) {
75747c478bd9Sstevel@tonic-gate 		*cs->statusp = resp->status = status;
75757c478bd9Sstevel@tonic-gate 		return;
75767c478bd9Sstevel@tonic-gate 	}
75777c478bd9Sstevel@tonic-gate 
75787c478bd9Sstevel@tonic-gate 	/* Ensure specified filehandle matches */
75797c478bd9Sstevel@tonic-gate 	if (cs->vp != sp->finfo->vp) {
75807c478bd9Sstevel@tonic-gate 		rfs4_state_rele(sp);
75817c478bd9Sstevel@tonic-gate 		*cs->statusp = resp->status = NFS4ERR_BAD_STATEID;
75827c478bd9Sstevel@tonic-gate 		return;
75837c478bd9Sstevel@tonic-gate 	}
75847c478bd9Sstevel@tonic-gate 
75857c478bd9Sstevel@tonic-gate 	/* hold off other access to open_owner while we tinker */
75867c478bd9Sstevel@tonic-gate 	rfs4_sw_enter(&sp->owner->oo_sw);
75877c478bd9Sstevel@tonic-gate 
75887c478bd9Sstevel@tonic-gate 	switch (rfs4_check_stateid_seqid(sp, &args->open_stateid)) {
75897c478bd9Sstevel@tonic-gate 	case NFS4_CHECK_STATEID_OKAY:
75907c478bd9Sstevel@tonic-gate 		if (rfs4_check_open_seqid(args->seqid, sp->owner,
75917c478bd9Sstevel@tonic-gate 			resop) != NFS4_CHKSEQ_OKAY) {
75927c478bd9Sstevel@tonic-gate 			*cs->statusp = resp->status = NFS4ERR_BAD_SEQID;
75937c478bd9Sstevel@tonic-gate 			goto end;
75947c478bd9Sstevel@tonic-gate 		}
75957c478bd9Sstevel@tonic-gate 		break;
75967c478bd9Sstevel@tonic-gate 	case NFS4_CHECK_STATEID_OLD:
75977c478bd9Sstevel@tonic-gate 		*cs->statusp = resp->status = NFS4ERR_OLD_STATEID;
75987c478bd9Sstevel@tonic-gate 		goto end;
75997c478bd9Sstevel@tonic-gate 	case NFS4_CHECK_STATEID_BAD:
76007c478bd9Sstevel@tonic-gate 		*cs->statusp = resp->status = NFS4ERR_BAD_STATEID;
76017c478bd9Sstevel@tonic-gate 		goto end;
76027c478bd9Sstevel@tonic-gate 	case NFS4_CHECK_STATEID_EXPIRED:
76037c478bd9Sstevel@tonic-gate 		*cs->statusp = resp->status = NFS4ERR_EXPIRED;
76047c478bd9Sstevel@tonic-gate 		goto end;
76057c478bd9Sstevel@tonic-gate 	case NFS4_CHECK_STATEID_CLOSED:
76067c478bd9Sstevel@tonic-gate 		*cs->statusp = resp->status = NFS4ERR_OLD_STATEID;
76077c478bd9Sstevel@tonic-gate 		goto end;
76087c478bd9Sstevel@tonic-gate 	case NFS4_CHECK_STATEID_UNCONFIRMED:
76097c478bd9Sstevel@tonic-gate 		*cs->statusp = resp->status = NFS4ERR_BAD_STATEID;
76107c478bd9Sstevel@tonic-gate 		goto end;
76117c478bd9Sstevel@tonic-gate 	case NFS4_CHECK_STATEID_REPLAY:
76127c478bd9Sstevel@tonic-gate 		/* Check the sequence id for the open owner */
76137c478bd9Sstevel@tonic-gate 		switch (rfs4_check_open_seqid(args->seqid, sp->owner, resop)) {
76147c478bd9Sstevel@tonic-gate 		case NFS4_CHKSEQ_OKAY:
76157c478bd9Sstevel@tonic-gate 			/*
76167c478bd9Sstevel@tonic-gate 			 * This is replayed stateid; if seqid matches
76177c478bd9Sstevel@tonic-gate 			 * next expected, then client is using wrong seqid.
76187c478bd9Sstevel@tonic-gate 			 */
76197c478bd9Sstevel@tonic-gate 			/* FALL THROUGH */
76207c478bd9Sstevel@tonic-gate 		case NFS4_CHKSEQ_BAD:
76217c478bd9Sstevel@tonic-gate 			*cs->statusp = resp->status = NFS4ERR_BAD_SEQID;
76227c478bd9Sstevel@tonic-gate 			goto end;
76237c478bd9Sstevel@tonic-gate 		case NFS4_CHKSEQ_REPLAY:
76247c478bd9Sstevel@tonic-gate 			/*
76257c478bd9Sstevel@tonic-gate 			 * Note this case is the duplicate case so
76267c478bd9Sstevel@tonic-gate 			 * resp->status is already set.
76277c478bd9Sstevel@tonic-gate 			 */
76287c478bd9Sstevel@tonic-gate 			*cs->statusp = resp->status;
76297c478bd9Sstevel@tonic-gate 			rfs4_update_lease(sp->owner->client);
76307c478bd9Sstevel@tonic-gate 			goto end;
76317c478bd9Sstevel@tonic-gate 		}
76327c478bd9Sstevel@tonic-gate 		break;
76337c478bd9Sstevel@tonic-gate 	default:
76347c478bd9Sstevel@tonic-gate 		ASSERT(FALSE);
76357c478bd9Sstevel@tonic-gate 		break;
76367c478bd9Sstevel@tonic-gate 	}
76377c478bd9Sstevel@tonic-gate 
76387c478bd9Sstevel@tonic-gate 	rfs4_dbe_lock(sp->dbe);
76397c478bd9Sstevel@tonic-gate 
76407c478bd9Sstevel@tonic-gate 	/* Update the stateid. */
76417c478bd9Sstevel@tonic-gate 	next_stateid(&sp->stateid);
76427c478bd9Sstevel@tonic-gate 	resp->open_stateid = sp->stateid.stateid;
76437c478bd9Sstevel@tonic-gate 
76447c478bd9Sstevel@tonic-gate 	rfs4_dbe_unlock(sp->dbe);
76457c478bd9Sstevel@tonic-gate 
76467c478bd9Sstevel@tonic-gate 	rfs4_update_lease(sp->owner->client);
76477c478bd9Sstevel@tonic-gate 	rfs4_update_open_sequence(sp->owner);
76487c478bd9Sstevel@tonic-gate 	rfs4_update_open_resp(sp->owner, resop, NULL);
76497c478bd9Sstevel@tonic-gate 
76507c478bd9Sstevel@tonic-gate 	rfs4_state_close(sp, FALSE, FALSE, cs->cr);
76517c478bd9Sstevel@tonic-gate 
76527c478bd9Sstevel@tonic-gate 	*cs->statusp = resp->status = status;
76537c478bd9Sstevel@tonic-gate 
76547c478bd9Sstevel@tonic-gate end:
76557c478bd9Sstevel@tonic-gate 	rfs4_sw_exit(&sp->owner->oo_sw);
76567c478bd9Sstevel@tonic-gate 	rfs4_state_rele(sp);
76577c478bd9Sstevel@tonic-gate }
76587c478bd9Sstevel@tonic-gate 
76597c478bd9Sstevel@tonic-gate /*
76607c478bd9Sstevel@tonic-gate  * Manage the counts on the file struct and close all file locks
76617c478bd9Sstevel@tonic-gate  */
76627c478bd9Sstevel@tonic-gate /*ARGSUSED*/
76637c478bd9Sstevel@tonic-gate void
76647c478bd9Sstevel@tonic-gate rfs4_release_share_lock_state(rfs4_state_t *sp, cred_t *cr,
76657c478bd9Sstevel@tonic-gate 	bool_t close_of_client)
76667c478bd9Sstevel@tonic-gate {
76677c478bd9Sstevel@tonic-gate 	rfs4_file_t *fp = sp->finfo;
76687c478bd9Sstevel@tonic-gate 	rfs4_lo_state_t *lsp;
76697c478bd9Sstevel@tonic-gate 	struct shrlock shr;
76707c478bd9Sstevel@tonic-gate 	struct shr_locowner shr_loco;
76717c478bd9Sstevel@tonic-gate 	int fflags, s_access, s_deny;
76727c478bd9Sstevel@tonic-gate 
76737c478bd9Sstevel@tonic-gate 	fflags = s_access = s_deny = 0;
76747c478bd9Sstevel@tonic-gate 	/*
76757c478bd9Sstevel@tonic-gate 	 * Decrement the count for each access and deny bit that this
76767c478bd9Sstevel@tonic-gate 	 * state has contributed to the file. If the file counts go to zero
76777c478bd9Sstevel@tonic-gate 	 * clear the appropriate bit in the appropriate mask.
76787c478bd9Sstevel@tonic-gate 	 */
76797c478bd9Sstevel@tonic-gate 
76807c478bd9Sstevel@tonic-gate 	if (sp->share_access & OPEN4_SHARE_ACCESS_READ) {
76817c478bd9Sstevel@tonic-gate 		fp->access_read--;
76827c478bd9Sstevel@tonic-gate 		fflags |= FREAD;
76837c478bd9Sstevel@tonic-gate 		s_access |= F_RDACC;
76847c478bd9Sstevel@tonic-gate 		if (fp->access_read == 0)
76857c478bd9Sstevel@tonic-gate 			fp->share_access &= ~OPEN4_SHARE_ACCESS_READ;
76867c478bd9Sstevel@tonic-gate 	}
76877c478bd9Sstevel@tonic-gate 	if (sp->share_access & OPEN4_SHARE_ACCESS_WRITE) {
76887c478bd9Sstevel@tonic-gate 		fp->access_write--;
76897c478bd9Sstevel@tonic-gate 		fflags |= FWRITE;
76907c478bd9Sstevel@tonic-gate 		s_access |= F_WRACC;
76917c478bd9Sstevel@tonic-gate 		if (fp->access_write == 0)
76927c478bd9Sstevel@tonic-gate 			fp->share_access &= ~OPEN4_SHARE_ACCESS_WRITE;
76937c478bd9Sstevel@tonic-gate 	}
76947c478bd9Sstevel@tonic-gate 	if (sp->share_deny & OPEN4_SHARE_DENY_READ) {
76957c478bd9Sstevel@tonic-gate 		fp->deny_read--;
76967c478bd9Sstevel@tonic-gate 		s_deny |= F_RDDNY;
76977c478bd9Sstevel@tonic-gate 		if (fp->deny_read == 0)
76987c478bd9Sstevel@tonic-gate 			fp->share_deny &= ~OPEN4_SHARE_DENY_READ;
76997c478bd9Sstevel@tonic-gate 	}
77007c478bd9Sstevel@tonic-gate 	if (sp->share_deny & OPEN4_SHARE_DENY_WRITE) {
77017c478bd9Sstevel@tonic-gate 		fp->deny_write--;
77027c478bd9Sstevel@tonic-gate 		s_deny |= F_WRDNY;
77037c478bd9Sstevel@tonic-gate 		if (fp->deny_write == 0)
77047c478bd9Sstevel@tonic-gate 			fp->share_deny &= ~OPEN4_SHARE_DENY_WRITE;
77057c478bd9Sstevel@tonic-gate 	}
77067c478bd9Sstevel@tonic-gate 
77077c478bd9Sstevel@tonic-gate 	/*
77087c478bd9Sstevel@tonic-gate 	 * If this call is part of the larger closing down of client
77097c478bd9Sstevel@tonic-gate 	 * state then it is just easier to release all locks
77107c478bd9Sstevel@tonic-gate 	 * associated with this client instead of going through each
77117c478bd9Sstevel@tonic-gate 	 * individual file and cleaning locks there.
77127c478bd9Sstevel@tonic-gate 	 */
77137c478bd9Sstevel@tonic-gate 	if (close_of_client) {
77147c478bd9Sstevel@tonic-gate 		if (sp->owner->client->unlksys_completed == FALSE &&
77157c478bd9Sstevel@tonic-gate 		    sp->lockownerlist.next->lsp != NULL &&
77167c478bd9Sstevel@tonic-gate 			sp->owner->client->sysidt != LM_NOSYSID) {
77177c478bd9Sstevel@tonic-gate 			/* Is the PxFS kernel module loaded? */
77187c478bd9Sstevel@tonic-gate 			if (lm_remove_file_locks != NULL) {
77197c478bd9Sstevel@tonic-gate 				int new_sysid;
77207c478bd9Sstevel@tonic-gate 
77217c478bd9Sstevel@tonic-gate 				/* Encode the cluster nodeid in new sysid */
77227c478bd9Sstevel@tonic-gate 				new_sysid = sp->owner->client->sysidt;
77237c478bd9Sstevel@tonic-gate 				lm_set_nlmid_flk(&new_sysid);
77247c478bd9Sstevel@tonic-gate 
77257c478bd9Sstevel@tonic-gate 				/*
77267c478bd9Sstevel@tonic-gate 				 * This PxFS routine removes file locks for a
77277c478bd9Sstevel@tonic-gate 				 * client over all nodes of a cluster.
77287c478bd9Sstevel@tonic-gate 				 */
77297c478bd9Sstevel@tonic-gate 				NFS4_DEBUG(rfs4_debug, (CE_NOTE,
77307c478bd9Sstevel@tonic-gate 				    "lm_remove_file_locks(sysid=0x%x)\n",
77317c478bd9Sstevel@tonic-gate 				    new_sysid));
77327c478bd9Sstevel@tonic-gate 				(*lm_remove_file_locks)(new_sysid);
77337c478bd9Sstevel@tonic-gate 			} else {
77347c478bd9Sstevel@tonic-gate 				struct flock64 flk;
77357c478bd9Sstevel@tonic-gate 
77367c478bd9Sstevel@tonic-gate 				/* Release all locks for this client */
77377c478bd9Sstevel@tonic-gate 				flk.l_type = F_UNLKSYS;
77387c478bd9Sstevel@tonic-gate 				flk.l_whence = 0;
77397c478bd9Sstevel@tonic-gate 				flk.l_start = 0;
77407c478bd9Sstevel@tonic-gate 				flk.l_len = 0;
77417c478bd9Sstevel@tonic-gate 				flk.l_sysid = sp->owner->client->sysidt;
77427c478bd9Sstevel@tonic-gate 				flk.l_pid = 0;
77437c478bd9Sstevel@tonic-gate 				(void) VOP_FRLOCK(sp->finfo->vp, F_SETLK, &flk,
77447c478bd9Sstevel@tonic-gate 				    F_REMOTELOCK | FREAD | FWRITE,
77457c478bd9Sstevel@tonic-gate 				    (u_offset_t)0, NULL, CRED());
77467c478bd9Sstevel@tonic-gate 			}
77477c478bd9Sstevel@tonic-gate 
77487c478bd9Sstevel@tonic-gate 			sp->owner->client->unlksys_completed = TRUE;
77497c478bd9Sstevel@tonic-gate 		}
77507c478bd9Sstevel@tonic-gate 	}
77517c478bd9Sstevel@tonic-gate 
77527c478bd9Sstevel@tonic-gate 	/*
77537c478bd9Sstevel@tonic-gate 	 * Release all locks on this file by this lock owner or at
77547c478bd9Sstevel@tonic-gate 	 * least mark the locks as having been released
77557c478bd9Sstevel@tonic-gate 	 */
77567c478bd9Sstevel@tonic-gate 	for (lsp = sp->lockownerlist.next->lsp; lsp != NULL;
77577c478bd9Sstevel@tonic-gate 		lsp = lsp->lockownerlist.next->lsp) {
77587c478bd9Sstevel@tonic-gate 
77597c478bd9Sstevel@tonic-gate 		lsp->locks_cleaned = TRUE;
77607c478bd9Sstevel@tonic-gate 
77617c478bd9Sstevel@tonic-gate 		/* Was this already taken care of above? */
77627c478bd9Sstevel@tonic-gate 		if (!close_of_client &&
77637c478bd9Sstevel@tonic-gate 		    sp->owner->client->sysidt != LM_NOSYSID)
77647c478bd9Sstevel@tonic-gate 			(void) cleanlocks(sp->finfo->vp, lsp->locker->pid,
77657c478bd9Sstevel@tonic-gate 				lsp->locker->client->sysidt);
77667c478bd9Sstevel@tonic-gate 	}
77677c478bd9Sstevel@tonic-gate 
77687c478bd9Sstevel@tonic-gate 	/*
77697c478bd9Sstevel@tonic-gate 	 * Release any shrlocks associated with this open state ID.
77707c478bd9Sstevel@tonic-gate 	 * This must be done before the rfs4_state gets marked closed.
77717c478bd9Sstevel@tonic-gate 	 */
77727c478bd9Sstevel@tonic-gate 	if (sp->owner->client->sysidt != LM_NOSYSID) {
77737c478bd9Sstevel@tonic-gate 		shr.s_access = s_access;
77747c478bd9Sstevel@tonic-gate 		shr.s_deny = s_deny;
77757c478bd9Sstevel@tonic-gate 		shr.s_pid = rfs4_dbe_getid(sp->owner->dbe);
77767c478bd9Sstevel@tonic-gate 		shr.s_sysid = sp->owner->client->sysidt;
77777c478bd9Sstevel@tonic-gate 		shr_loco.sl_pid = shr.s_pid;
77787c478bd9Sstevel@tonic-gate 		shr_loco.sl_id = shr.s_sysid;
77797c478bd9Sstevel@tonic-gate 		shr.s_owner = (caddr_t)&shr_loco;
77807c478bd9Sstevel@tonic-gate 		shr.s_own_len = sizeof (shr_loco);
77817c478bd9Sstevel@tonic-gate 		(void) vop_shrlock(sp->finfo->vp, F_UNSHARE, &shr, fflags);
77827c478bd9Sstevel@tonic-gate 	}
77837c478bd9Sstevel@tonic-gate }
77847c478bd9Sstevel@tonic-gate 
77857c478bd9Sstevel@tonic-gate /*
77867c478bd9Sstevel@tonic-gate  * lock_denied: Fill in a LOCK4deneid structure given an flock64 structure.
77877c478bd9Sstevel@tonic-gate  */
77887c478bd9Sstevel@tonic-gate static nfsstat4
77897c478bd9Sstevel@tonic-gate lock_denied(LOCK4denied *dp, struct flock64 *flk)
77907c478bd9Sstevel@tonic-gate {
77917c478bd9Sstevel@tonic-gate 	rfs4_lockowner_t *lo;
77927c478bd9Sstevel@tonic-gate 	rfs4_client_t *cp;
77937c478bd9Sstevel@tonic-gate 	uint32_t len;
77947c478bd9Sstevel@tonic-gate 
77957c478bd9Sstevel@tonic-gate 	lo = rfs4_findlockowner_by_pid(flk->l_pid);
77967c478bd9Sstevel@tonic-gate 	if (lo != NULL) {
77977c478bd9Sstevel@tonic-gate 		cp = lo->client;
77987c478bd9Sstevel@tonic-gate 		if (rfs4_lease_expired(cp)) {
77997c478bd9Sstevel@tonic-gate 			rfs4_lockowner_rele(lo);
78007c478bd9Sstevel@tonic-gate 			rfs4_dbe_hold(cp->dbe);
78017c478bd9Sstevel@tonic-gate 			rfs4_client_close(cp);
78027c478bd9Sstevel@tonic-gate 			return (NFS4ERR_EXPIRED);
78037c478bd9Sstevel@tonic-gate 		}
78047c478bd9Sstevel@tonic-gate 		dp->owner.clientid = lo->owner.clientid;
78057c478bd9Sstevel@tonic-gate 		len = lo->owner.owner_len;
78067c478bd9Sstevel@tonic-gate 		dp->owner.owner_val = kmem_alloc(len, KM_SLEEP);
78077c478bd9Sstevel@tonic-gate 		bcopy(lo->owner.owner_val, dp->owner.owner_val, len);
78087c478bd9Sstevel@tonic-gate 		dp->owner.owner_len = len;
78097c478bd9Sstevel@tonic-gate 		rfs4_lockowner_rele(lo);
78107c478bd9Sstevel@tonic-gate 		goto finish;
78117c478bd9Sstevel@tonic-gate 	}
78127c478bd9Sstevel@tonic-gate 
78137c478bd9Sstevel@tonic-gate 	/*
78147c478bd9Sstevel@tonic-gate 	 * Its not a NFS4 lock. We take advantage that the upper 32 bits
78157c478bd9Sstevel@tonic-gate 	 * of the client id contain the boot time for a NFS4 lock. So we
78167c478bd9Sstevel@tonic-gate 	 * fabricate and identity by setting clientid to the sysid, and
78177c478bd9Sstevel@tonic-gate 	 * the lock owner to the pid.
78187c478bd9Sstevel@tonic-gate 	 */
78197c478bd9Sstevel@tonic-gate 	dp->owner.clientid = flk->l_sysid;
78207c478bd9Sstevel@tonic-gate 	len = sizeof (pid_t);
78217c478bd9Sstevel@tonic-gate 	dp->owner.owner_len = len;
78227c478bd9Sstevel@tonic-gate 	dp->owner.owner_val = kmem_alloc(len, KM_SLEEP);
78237c478bd9Sstevel@tonic-gate 	bcopy(&flk->l_pid, dp->owner.owner_val, len);
78247c478bd9Sstevel@tonic-gate finish:
78257c478bd9Sstevel@tonic-gate 	dp->offset = flk->l_start;
78267c478bd9Sstevel@tonic-gate 	dp->length = flk->l_len;
78277c478bd9Sstevel@tonic-gate 
78287c478bd9Sstevel@tonic-gate 	if (flk->l_type == F_RDLCK)
78297c478bd9Sstevel@tonic-gate 		dp->locktype = READ_LT;
78307c478bd9Sstevel@tonic-gate 	else if (flk->l_type == F_WRLCK)
78317c478bd9Sstevel@tonic-gate 		dp->locktype = WRITE_LT;
78327c478bd9Sstevel@tonic-gate 	else
78337c478bd9Sstevel@tonic-gate 		return (NFS4ERR_INVAL);	/* no mapping from POSIX ltype to v4 */
78347c478bd9Sstevel@tonic-gate 
78357c478bd9Sstevel@tonic-gate 	return (NFS4_OK);
78367c478bd9Sstevel@tonic-gate }
78377c478bd9Sstevel@tonic-gate 
78387c478bd9Sstevel@tonic-gate static int
78397c478bd9Sstevel@tonic-gate setlock(vnode_t *vp, struct flock64 *flock, int flag, cred_t *cred)
78407c478bd9Sstevel@tonic-gate {
78417c478bd9Sstevel@tonic-gate 	int error;
78427c478bd9Sstevel@tonic-gate 	struct flock64 flk;
78437c478bd9Sstevel@tonic-gate 	int i;
78447c478bd9Sstevel@tonic-gate 	clock_t delaytime;
78457c478bd9Sstevel@tonic-gate 
78467c478bd9Sstevel@tonic-gate retry:
78477c478bd9Sstevel@tonic-gate 	delaytime = MSEC_TO_TICK_ROUNDUP(rfs4_lock_delay);
78487c478bd9Sstevel@tonic-gate 
78497c478bd9Sstevel@tonic-gate 	for (i = 0; i < rfs4_maxlock_tries; i++) {
78507c478bd9Sstevel@tonic-gate 		LOCK_PRINT(rfs4_debug, "setlock", F_SETLK, flock);
78517c478bd9Sstevel@tonic-gate 		error = VOP_FRLOCK(vp, F_SETLK,
78527c478bd9Sstevel@tonic-gate 				flock, flag, (u_offset_t)0, NULL, cred);
78537c478bd9Sstevel@tonic-gate 
78547c478bd9Sstevel@tonic-gate 		if (error != EAGAIN && error != EACCES)
78557c478bd9Sstevel@tonic-gate 			break;
78567c478bd9Sstevel@tonic-gate 
78577c478bd9Sstevel@tonic-gate 		if (i < rfs4_maxlock_tries - 1) {
78587c478bd9Sstevel@tonic-gate 			delay(delaytime);
78597c478bd9Sstevel@tonic-gate 			delaytime *= 2;
78607c478bd9Sstevel@tonic-gate 		}
78617c478bd9Sstevel@tonic-gate 	}
78627c478bd9Sstevel@tonic-gate 
78637c478bd9Sstevel@tonic-gate 	if (error == EAGAIN || error == EACCES) {
78647c478bd9Sstevel@tonic-gate 		/* Get the owner of the lock */
78657c478bd9Sstevel@tonic-gate 		flk = *flock;
78667c478bd9Sstevel@tonic-gate 		LOCK_PRINT(rfs4_debug, "setlock", F_GETLK, &flk);
78677c478bd9Sstevel@tonic-gate 		if (VOP_FRLOCK(vp, F_GETLK,
78687c478bd9Sstevel@tonic-gate 			    &flk,  flag, (u_offset_t)0, NULL, cred) == 0) {
78697c478bd9Sstevel@tonic-gate 			if (flk.l_type == F_UNLCK) {
78707c478bd9Sstevel@tonic-gate 				/* No longer locked, retry */
78717c478bd9Sstevel@tonic-gate 				goto retry;
78727c478bd9Sstevel@tonic-gate 			}
78737c478bd9Sstevel@tonic-gate 			*flock = flk;
78747c478bd9Sstevel@tonic-gate 			LOCK_PRINT(rfs4_debug, "setlock(blocking lock)",
78757c478bd9Sstevel@tonic-gate 				F_GETLK, &flk);
78767c478bd9Sstevel@tonic-gate 		}
78777c478bd9Sstevel@tonic-gate 	}
78787c478bd9Sstevel@tonic-gate 
78797c478bd9Sstevel@tonic-gate 	return (error);
78807c478bd9Sstevel@tonic-gate }
78817c478bd9Sstevel@tonic-gate 
78827c478bd9Sstevel@tonic-gate /*ARGSUSED*/
78837c478bd9Sstevel@tonic-gate static nfsstat4
78847c478bd9Sstevel@tonic-gate rfs4_do_lock(rfs4_lo_state_t *lp, nfs_lock_type4 locktype,
78857c478bd9Sstevel@tonic-gate 	    seqid4 seqid, offset4 offset,
78867c478bd9Sstevel@tonic-gate 	    length4 length, cred_t *cred, nfs_resop4 *resop)
78877c478bd9Sstevel@tonic-gate {
78887c478bd9Sstevel@tonic-gate 	nfsstat4 status;
78897c478bd9Sstevel@tonic-gate 	rfs4_lockowner_t *lo = lp->locker;
78907c478bd9Sstevel@tonic-gate 	rfs4_state_t *sp = lp->state;
78917c478bd9Sstevel@tonic-gate 	struct flock64 flock;
78927c478bd9Sstevel@tonic-gate 	int16_t ltype;
78937c478bd9Sstevel@tonic-gate 	int flag;
78947c478bd9Sstevel@tonic-gate 	int error;
78957c478bd9Sstevel@tonic-gate 	sysid_t sysid;
78967c478bd9Sstevel@tonic-gate 	LOCK4res *lres;
78977c478bd9Sstevel@tonic-gate 
78987c478bd9Sstevel@tonic-gate 	if (rfs4_lease_expired(lo->client)) {
78997c478bd9Sstevel@tonic-gate 		return (NFS4ERR_EXPIRED);
79007c478bd9Sstevel@tonic-gate 	}
79017c478bd9Sstevel@tonic-gate 
79027c478bd9Sstevel@tonic-gate 	if ((status = rfs4_client_sysid(lo->client, &sysid)) != NFS4_OK)
79037c478bd9Sstevel@tonic-gate 		return (status);
79047c478bd9Sstevel@tonic-gate 
79057c478bd9Sstevel@tonic-gate 	/* Check for zero length. To lock to end of file use all ones for V4 */
79067c478bd9Sstevel@tonic-gate 	if (length == 0)
79077c478bd9Sstevel@tonic-gate 		return (NFS4ERR_INVAL);
79087c478bd9Sstevel@tonic-gate 	else if (length == (length4)(~0))
79097c478bd9Sstevel@tonic-gate 		length = 0;		/* Posix to end of file  */
79107c478bd9Sstevel@tonic-gate 
79117c478bd9Sstevel@tonic-gate retry:
79127c478bd9Sstevel@tonic-gate 	rfs4_dbe_lock(sp->dbe);
79137c478bd9Sstevel@tonic-gate 
79147c478bd9Sstevel@tonic-gate 
79157c478bd9Sstevel@tonic-gate 	if (resop->resop != OP_LOCKU) {
79167c478bd9Sstevel@tonic-gate 		switch (locktype) {
79177c478bd9Sstevel@tonic-gate 		case READ_LT:
79187c478bd9Sstevel@tonic-gate 		case READW_LT:
79197c478bd9Sstevel@tonic-gate 			if ((sp->share_access
79207c478bd9Sstevel@tonic-gate 			    & OPEN4_SHARE_ACCESS_READ) == 0) {
79217c478bd9Sstevel@tonic-gate 				rfs4_dbe_unlock(sp->dbe);
79227c478bd9Sstevel@tonic-gate 
79237c478bd9Sstevel@tonic-gate 				return (NFS4ERR_OPENMODE);
79247c478bd9Sstevel@tonic-gate 			}
79257c478bd9Sstevel@tonic-gate 			ltype = F_RDLCK;
79267c478bd9Sstevel@tonic-gate 			break;
79277c478bd9Sstevel@tonic-gate 		case WRITE_LT:
79287c478bd9Sstevel@tonic-gate 		case WRITEW_LT:
79297c478bd9Sstevel@tonic-gate 			if ((sp->share_access
79307c478bd9Sstevel@tonic-gate 			    & OPEN4_SHARE_ACCESS_WRITE) == 0) {
79317c478bd9Sstevel@tonic-gate 				rfs4_dbe_unlock(sp->dbe);
79327c478bd9Sstevel@tonic-gate 
79337c478bd9Sstevel@tonic-gate 				return (NFS4ERR_OPENMODE);
79347c478bd9Sstevel@tonic-gate 			}
79357c478bd9Sstevel@tonic-gate 			ltype = F_WRLCK;
79367c478bd9Sstevel@tonic-gate 			break;
79377c478bd9Sstevel@tonic-gate 		}
79387c478bd9Sstevel@tonic-gate 	} else
79397c478bd9Sstevel@tonic-gate 		ltype = F_UNLCK;
79407c478bd9Sstevel@tonic-gate 
79417c478bd9Sstevel@tonic-gate 	flock.l_type = ltype;
79427c478bd9Sstevel@tonic-gate 	flock.l_whence = 0;		/* SEEK_SET */
79437c478bd9Sstevel@tonic-gate 	flock.l_start = offset;
79447c478bd9Sstevel@tonic-gate 	flock.l_len = length;
79457c478bd9Sstevel@tonic-gate 	flock.l_sysid = sysid;
79467c478bd9Sstevel@tonic-gate 	flock.l_pid = lp->locker->pid;
79477c478bd9Sstevel@tonic-gate 
79487c478bd9Sstevel@tonic-gate 	/* Note that length4 is uint64_t but l_len and l_start are off64_t */
79497c478bd9Sstevel@tonic-gate 	if (flock.l_len < 0 || flock.l_start < 0) {
79507c478bd9Sstevel@tonic-gate 		rfs4_dbe_unlock(sp->dbe);
79517c478bd9Sstevel@tonic-gate 		return (NFS4ERR_INVAL);
79527c478bd9Sstevel@tonic-gate 	}
79537c478bd9Sstevel@tonic-gate 
79547c478bd9Sstevel@tonic-gate 	/*
79557c478bd9Sstevel@tonic-gate 	 * N.B. FREAD has the same value as OPEN4_SHARE_ACCESS_READ and
79567c478bd9Sstevel@tonic-gate 	 * FWRITE has the same value as OPEN4_SHARE_ACCESS_WRITE.
79577c478bd9Sstevel@tonic-gate 	 */
79587c478bd9Sstevel@tonic-gate 	flag = (int)sp->share_access | F_REMOTELOCK;
79597c478bd9Sstevel@tonic-gate 
79607c478bd9Sstevel@tonic-gate 	error = setlock(sp->finfo->vp, &flock, flag, cred);
79617c478bd9Sstevel@tonic-gate 	if (error == 0) {
79627c478bd9Sstevel@tonic-gate 		rfs4_dbe_lock(lp->dbe);
79637c478bd9Sstevel@tonic-gate 		next_stateid(&lp->lockid);
79647c478bd9Sstevel@tonic-gate 		rfs4_dbe_unlock(lp->dbe);
79657c478bd9Sstevel@tonic-gate 	}
79667c478bd9Sstevel@tonic-gate 
79677c478bd9Sstevel@tonic-gate 	rfs4_dbe_unlock(sp->dbe);
79687c478bd9Sstevel@tonic-gate 
79697c478bd9Sstevel@tonic-gate 	/*
79707c478bd9Sstevel@tonic-gate 	 * N.B. We map error values to nfsv4 errors. This is differrent
79717c478bd9Sstevel@tonic-gate 	 * than puterrno4 routine.
79727c478bd9Sstevel@tonic-gate 	 */
79737c478bd9Sstevel@tonic-gate 	switch (error) {
79747c478bd9Sstevel@tonic-gate 	case 0:
79757c478bd9Sstevel@tonic-gate 		status = NFS4_OK;
79767c478bd9Sstevel@tonic-gate 		break;
79777c478bd9Sstevel@tonic-gate 	case EAGAIN:
79787c478bd9Sstevel@tonic-gate 	case EACCES:		/* Old value */
79797c478bd9Sstevel@tonic-gate 		/* Can only get here if op is OP_LOCK */
79807c478bd9Sstevel@tonic-gate 		ASSERT(resop->resop == OP_LOCK);
79817c478bd9Sstevel@tonic-gate 		lres = &resop->nfs_resop4_u.oplock;
79827c478bd9Sstevel@tonic-gate 		status = NFS4ERR_DENIED;
79837c478bd9Sstevel@tonic-gate 		if (lock_denied(&lres->LOCK4res_u.denied, &flock)
79847c478bd9Sstevel@tonic-gate 			== NFS4ERR_EXPIRED)
79857c478bd9Sstevel@tonic-gate 			goto retry;
79867c478bd9Sstevel@tonic-gate 		break;
79877c478bd9Sstevel@tonic-gate 	case ENOLCK:
79887c478bd9Sstevel@tonic-gate 		status = NFS4ERR_DELAY;
79897c478bd9Sstevel@tonic-gate 		break;
79907c478bd9Sstevel@tonic-gate 	case EOVERFLOW:
79917c478bd9Sstevel@tonic-gate 		status = NFS4ERR_INVAL;
79927c478bd9Sstevel@tonic-gate 		break;
79937c478bd9Sstevel@tonic-gate 	case EINVAL:
79947c478bd9Sstevel@tonic-gate 		status = NFS4ERR_NOTSUPP;
79957c478bd9Sstevel@tonic-gate 		break;
79967c478bd9Sstevel@tonic-gate 	default:
79977c478bd9Sstevel@tonic-gate 		cmn_err(CE_WARN, "rfs4_do_lock: unexpected errno (%d)",
79987c478bd9Sstevel@tonic-gate 			error);
79997c478bd9Sstevel@tonic-gate 		status = NFS4ERR_SERVERFAULT;
80007c478bd9Sstevel@tonic-gate 		break;
80017c478bd9Sstevel@tonic-gate 	}
80027c478bd9Sstevel@tonic-gate 
80037c478bd9Sstevel@tonic-gate 	return (status);
80047c478bd9Sstevel@tonic-gate }
80057c478bd9Sstevel@tonic-gate 
80067c478bd9Sstevel@tonic-gate /*ARGSUSED*/
80077c478bd9Sstevel@tonic-gate void
80087c478bd9Sstevel@tonic-gate rfs4_op_lock(nfs_argop4 *argop, nfs_resop4 *resop,
80097c478bd9Sstevel@tonic-gate 	    struct svc_req *req, struct compound_state *cs)
80107c478bd9Sstevel@tonic-gate {
80117c478bd9Sstevel@tonic-gate 	/* XXX Currently not using req arg */
80127c478bd9Sstevel@tonic-gate 	LOCK4args *args = &argop->nfs_argop4_u.oplock;
80137c478bd9Sstevel@tonic-gate 	LOCK4res *resp = &resop->nfs_resop4_u.oplock;
80147c478bd9Sstevel@tonic-gate 	nfsstat4 status;
80157c478bd9Sstevel@tonic-gate 	stateid4 *stateid;
80167c478bd9Sstevel@tonic-gate 	rfs4_lockowner_t *lo;
80177c478bd9Sstevel@tonic-gate 	rfs4_client_t *cp;
80187c478bd9Sstevel@tonic-gate 	rfs4_state_t *sp = NULL;
80197c478bd9Sstevel@tonic-gate 	rfs4_lo_state_t *lsp = NULL;
80207c478bd9Sstevel@tonic-gate 	bool_t ls_sw_held = FALSE;
80217c478bd9Sstevel@tonic-gate 	bool_t create = TRUE;
80227c478bd9Sstevel@tonic-gate 	bool_t lcreate = TRUE;
80237c478bd9Sstevel@tonic-gate 	bool_t dup_lock = FALSE;
80247c478bd9Sstevel@tonic-gate 	int rc;
80257c478bd9Sstevel@tonic-gate 
80267c478bd9Sstevel@tonic-gate 	if (cs->vp == NULL) {
80277c478bd9Sstevel@tonic-gate 		*cs->statusp = resp->status = NFS4ERR_NOFILEHANDLE;
80287c478bd9Sstevel@tonic-gate 		return;
80297c478bd9Sstevel@tonic-gate 	}
80307c478bd9Sstevel@tonic-gate 
80317c478bd9Sstevel@tonic-gate 	if (args->locker.new_lock_owner) {
80327c478bd9Sstevel@tonic-gate 		/* Create a new lockowner for this instance */
80337c478bd9Sstevel@tonic-gate 		open_to_lock_owner4 *olo = &args->locker.locker4_u.open_owner;
80347c478bd9Sstevel@tonic-gate 
80357c478bd9Sstevel@tonic-gate 		NFS4_DEBUG(rfs4_debug, (CE_NOTE, "Creating new lock owner"));
80367c478bd9Sstevel@tonic-gate 
80377c478bd9Sstevel@tonic-gate 		stateid = &olo->open_stateid;
80387c478bd9Sstevel@tonic-gate 		status = rfs4_get_state(stateid, &sp, RFS4_DBS_VALID);
80397c478bd9Sstevel@tonic-gate 		if (status != NFS4_OK) {
80407c478bd9Sstevel@tonic-gate 			NFS4_DEBUG(rfs4_debug,
80417c478bd9Sstevel@tonic-gate 				(CE_NOTE, "Get state failed in lock %d",
80427c478bd9Sstevel@tonic-gate 				status));
80437c478bd9Sstevel@tonic-gate 			*cs->statusp = resp->status = status;
80447c478bd9Sstevel@tonic-gate 			return;
80457c478bd9Sstevel@tonic-gate 		}
80467c478bd9Sstevel@tonic-gate 
80477c478bd9Sstevel@tonic-gate 		/* Ensure specified filehandle matches */
80487c478bd9Sstevel@tonic-gate 		if (cs->vp != sp->finfo->vp) {
80497c478bd9Sstevel@tonic-gate 			rfs4_state_rele(sp);
80507c478bd9Sstevel@tonic-gate 			*cs->statusp = resp->status = NFS4ERR_BAD_STATEID;
80517c478bd9Sstevel@tonic-gate 			return;
80527c478bd9Sstevel@tonic-gate 		}
80537c478bd9Sstevel@tonic-gate 
80547c478bd9Sstevel@tonic-gate 		/* hold off other access to open_owner while we tinker */
80557c478bd9Sstevel@tonic-gate 		rfs4_sw_enter(&sp->owner->oo_sw);
80567c478bd9Sstevel@tonic-gate 
80577c478bd9Sstevel@tonic-gate 		switch (rc = rfs4_check_stateid_seqid(sp, stateid)) {
80587c478bd9Sstevel@tonic-gate 		case NFS4_CHECK_STATEID_OLD:
80597c478bd9Sstevel@tonic-gate 			*cs->statusp = resp->status = NFS4ERR_OLD_STATEID;
80607c478bd9Sstevel@tonic-gate 			goto end;
80617c478bd9Sstevel@tonic-gate 		case NFS4_CHECK_STATEID_BAD:
80627c478bd9Sstevel@tonic-gate 			*cs->statusp = resp->status = NFS4ERR_BAD_STATEID;
80637c478bd9Sstevel@tonic-gate 			goto end;
80647c478bd9Sstevel@tonic-gate 		case NFS4_CHECK_STATEID_EXPIRED:
80657c478bd9Sstevel@tonic-gate 			*cs->statusp = resp->status = NFS4ERR_EXPIRED;
80667c478bd9Sstevel@tonic-gate 			goto end;
80677c478bd9Sstevel@tonic-gate 		case NFS4_CHECK_STATEID_UNCONFIRMED:
80687c478bd9Sstevel@tonic-gate 			*cs->statusp = resp->status = NFS4ERR_BAD_STATEID;
80697c478bd9Sstevel@tonic-gate 			goto end;
80707c478bd9Sstevel@tonic-gate 		case NFS4_CHECK_STATEID_CLOSED:
80717c478bd9Sstevel@tonic-gate 			*cs->statusp = resp->status = NFS4ERR_OLD_STATEID;
80727c478bd9Sstevel@tonic-gate 			goto end;
80737c478bd9Sstevel@tonic-gate 		case NFS4_CHECK_STATEID_OKAY:
80747c478bd9Sstevel@tonic-gate 		case NFS4_CHECK_STATEID_REPLAY:
80757c478bd9Sstevel@tonic-gate 			switch (rfs4_check_olo_seqid(olo->open_seqid,
80767c478bd9Sstevel@tonic-gate 				sp->owner, resop)) {
80777c478bd9Sstevel@tonic-gate 			case NFS4_CHKSEQ_OKAY:
80787c478bd9Sstevel@tonic-gate 				if (rc == NFS4_CHECK_STATEID_OKAY)
80797c478bd9Sstevel@tonic-gate 					break;
80807c478bd9Sstevel@tonic-gate 				/*
80817c478bd9Sstevel@tonic-gate 				 * This is replayed stateid; if seqid
80827c478bd9Sstevel@tonic-gate 				 * matches next expected, then client
80837c478bd9Sstevel@tonic-gate 				 * is using wrong seqid.
80847c478bd9Sstevel@tonic-gate 				 */
80857c478bd9Sstevel@tonic-gate 				/* FALLTHROUGH */
80867c478bd9Sstevel@tonic-gate 			case NFS4_CHKSEQ_BAD:
80877c478bd9Sstevel@tonic-gate 				*cs->statusp = resp->status =
80887c478bd9Sstevel@tonic-gate 					NFS4ERR_BAD_SEQID;
80897c478bd9Sstevel@tonic-gate 				goto end;
80907c478bd9Sstevel@tonic-gate 			case NFS4_CHKSEQ_REPLAY:
80917c478bd9Sstevel@tonic-gate 				/* This is a duplicate LOCK request */
80927c478bd9Sstevel@tonic-gate 				dup_lock = TRUE;
80937c478bd9Sstevel@tonic-gate 
80947c478bd9Sstevel@tonic-gate 				/*
80957c478bd9Sstevel@tonic-gate 				 * For a duplicate we do not want to
80967c478bd9Sstevel@tonic-gate 				 * create a new lockowner as it should
80977c478bd9Sstevel@tonic-gate 				 * already exist.
80987c478bd9Sstevel@tonic-gate 				 * Turn off the lockowner create flag.
80997c478bd9Sstevel@tonic-gate 				 */
81007c478bd9Sstevel@tonic-gate 				lcreate = FALSE;
81017c478bd9Sstevel@tonic-gate 			}
81027c478bd9Sstevel@tonic-gate 			break;
81037c478bd9Sstevel@tonic-gate 		}
81047c478bd9Sstevel@tonic-gate 
81057c478bd9Sstevel@tonic-gate 		lo = rfs4_findlockowner(&olo->lock_owner, &lcreate);
81067c478bd9Sstevel@tonic-gate 		if (lo == NULL) {
81077c478bd9Sstevel@tonic-gate 			NFS4_DEBUG(rfs4_debug,
81087c478bd9Sstevel@tonic-gate 				(CE_NOTE, "rfs4_op_lock: no lock owner"));
81097c478bd9Sstevel@tonic-gate 			*cs->statusp = resp->status = NFS4ERR_RESOURCE;
81107c478bd9Sstevel@tonic-gate 			goto end;
81117c478bd9Sstevel@tonic-gate 		}
81127c478bd9Sstevel@tonic-gate 
81137c478bd9Sstevel@tonic-gate 		lsp = rfs4_findlo_state_by_owner(lo, sp, &create);
81147c478bd9Sstevel@tonic-gate 		if (lsp == NULL) {
81157c478bd9Sstevel@tonic-gate 			rfs4_update_lease(sp->owner->client);
81167c478bd9Sstevel@tonic-gate 			/*
81177c478bd9Sstevel@tonic-gate 			 * Only update theh open_seqid if this is not
81187c478bd9Sstevel@tonic-gate 			 * a duplicate request
81197c478bd9Sstevel@tonic-gate 			 */
81207c478bd9Sstevel@tonic-gate 			if (dup_lock == FALSE) {
81217c478bd9Sstevel@tonic-gate 				rfs4_update_open_sequence(sp->owner);
81227c478bd9Sstevel@tonic-gate 			}
81237c478bd9Sstevel@tonic-gate 
81247c478bd9Sstevel@tonic-gate 			NFS4_DEBUG(rfs4_debug,
81257c478bd9Sstevel@tonic-gate 				(CE_NOTE, "rfs4_op_lock: no state"));
81267c478bd9Sstevel@tonic-gate 			*cs->statusp = resp->status = NFS4ERR_SERVERFAULT;
81277c478bd9Sstevel@tonic-gate 			rfs4_update_open_resp(sp->owner, resop, NULL);
81287c478bd9Sstevel@tonic-gate 			rfs4_lockowner_rele(lo);
81297c478bd9Sstevel@tonic-gate 			goto end;
81307c478bd9Sstevel@tonic-gate 		}
81317c478bd9Sstevel@tonic-gate 
81327c478bd9Sstevel@tonic-gate 		/*
81337c478bd9Sstevel@tonic-gate 		 * This is the new_lock_owner branch and the client is
81347c478bd9Sstevel@tonic-gate 		 * supposed to be associating a new lock_owner with
81357c478bd9Sstevel@tonic-gate 		 * the open file at this point.  If we find that a
81367c478bd9Sstevel@tonic-gate 		 * lock_owner/state association already exists and a
81377c478bd9Sstevel@tonic-gate 		 * successful LOCK request was returned to the client,
81387c478bd9Sstevel@tonic-gate 		 * an error is returned to the client since this is
81397c478bd9Sstevel@tonic-gate 		 * not appropriate.  The client should be using the
81407c478bd9Sstevel@tonic-gate 		 * existing lock_owner branch.
81417c478bd9Sstevel@tonic-gate 		 */
81427c478bd9Sstevel@tonic-gate 		if (dup_lock == FALSE && create == FALSE) {
81437c478bd9Sstevel@tonic-gate 			if (lsp->lock_completed == TRUE) {
81447c478bd9Sstevel@tonic-gate 				*cs->statusp =
81457c478bd9Sstevel@tonic-gate 					resp->status = NFS4ERR_BAD_SEQID;
81467c478bd9Sstevel@tonic-gate 				rfs4_lockowner_rele(lo);
81477c478bd9Sstevel@tonic-gate 				goto end;
81487c478bd9Sstevel@tonic-gate 			}
81497c478bd9Sstevel@tonic-gate 		}
81507c478bd9Sstevel@tonic-gate 
81517c478bd9Sstevel@tonic-gate 		rfs4_update_lease(sp->owner->client);
81527c478bd9Sstevel@tonic-gate 
81537c478bd9Sstevel@tonic-gate 		/*
81547c478bd9Sstevel@tonic-gate 		 * Only update theh open_seqid if this is not
81557c478bd9Sstevel@tonic-gate 		 * a duplicate request
81567c478bd9Sstevel@tonic-gate 		 */
81577c478bd9Sstevel@tonic-gate 		if (dup_lock == FALSE) {
81587c478bd9Sstevel@tonic-gate 			rfs4_update_open_sequence(sp->owner);
81597c478bd9Sstevel@tonic-gate 		}
81607c478bd9Sstevel@tonic-gate 
81617c478bd9Sstevel@tonic-gate 		/*
81627c478bd9Sstevel@tonic-gate 		 * If this is a duplicate lock request, just copy the
81637c478bd9Sstevel@tonic-gate 		 * previously saved reply and return.
81647c478bd9Sstevel@tonic-gate 		 */
81657c478bd9Sstevel@tonic-gate 		if (dup_lock == TRUE) {
81667c478bd9Sstevel@tonic-gate 			/* verify that lock_seqid's match */
81677c478bd9Sstevel@tonic-gate 			if (lsp->seqid != olo->lock_seqid) {
81687c478bd9Sstevel@tonic-gate 				NFS4_DEBUG(rfs4_debug,
81697c478bd9Sstevel@tonic-gate 				(CE_NOTE, "rfs4_op_lock: Dup-Lock seqid bad"
81707c478bd9Sstevel@tonic-gate 				"lsp->seqid=%d old->seqid=%d",
81717c478bd9Sstevel@tonic-gate 				lsp->seqid, olo->lock_seqid));
81727c478bd9Sstevel@tonic-gate 				*cs->statusp = resp->status = NFS4ERR_BAD_SEQID;
81737c478bd9Sstevel@tonic-gate 			} else {
81747c478bd9Sstevel@tonic-gate 				rfs4_copy_reply(resop, lsp->reply);
81757c478bd9Sstevel@tonic-gate 				/*
81767c478bd9Sstevel@tonic-gate 				 * Make sure to copy the just
81777c478bd9Sstevel@tonic-gate 				 * retrieved reply status into the
81787c478bd9Sstevel@tonic-gate 				 * overall compound status
81797c478bd9Sstevel@tonic-gate 				 */
81807c478bd9Sstevel@tonic-gate 				*cs->statusp = resp->status;
81817c478bd9Sstevel@tonic-gate 			}
81827c478bd9Sstevel@tonic-gate 			rfs4_lockowner_rele(lo);
81837c478bd9Sstevel@tonic-gate 			goto end;
81847c478bd9Sstevel@tonic-gate 		}
81857c478bd9Sstevel@tonic-gate 
81867c478bd9Sstevel@tonic-gate 		rfs4_dbe_lock(lsp->dbe);
81877c478bd9Sstevel@tonic-gate 
81887c478bd9Sstevel@tonic-gate 		/* Make sure to update the lock sequence id */
81897c478bd9Sstevel@tonic-gate 		lsp->seqid = olo->lock_seqid;
81907c478bd9Sstevel@tonic-gate 
81917c478bd9Sstevel@tonic-gate 		NFS4_DEBUG(rfs4_debug,
81927c478bd9Sstevel@tonic-gate 			(CE_NOTE, "Lock seqid established as %d", lsp->seqid));
81937c478bd9Sstevel@tonic-gate 
81947c478bd9Sstevel@tonic-gate 		/*
81957c478bd9Sstevel@tonic-gate 		 * This is used to signify the newly created lockowner
81967c478bd9Sstevel@tonic-gate 		 * stateid and its sequence number.  The checks for
81977c478bd9Sstevel@tonic-gate 		 * sequence number and increment don't occur on the
81987c478bd9Sstevel@tonic-gate 		 * very first lock request for a lockowner.
81997c478bd9Sstevel@tonic-gate 		 */
82007c478bd9Sstevel@tonic-gate 		lsp->skip_seqid_check = TRUE;
82017c478bd9Sstevel@tonic-gate 
82027c478bd9Sstevel@tonic-gate 		/* hold off other access to lsp while we tinker */
82037c478bd9Sstevel@tonic-gate 		rfs4_sw_enter(&lsp->ls_sw);
82047c478bd9Sstevel@tonic-gate 		ls_sw_held = TRUE;
82057c478bd9Sstevel@tonic-gate 
82067c478bd9Sstevel@tonic-gate 		rfs4_dbe_unlock(lsp->dbe);
82077c478bd9Sstevel@tonic-gate 
82087c478bd9Sstevel@tonic-gate 		rfs4_lockowner_rele(lo);
82097c478bd9Sstevel@tonic-gate 	} else {
82107c478bd9Sstevel@tonic-gate 		stateid = &args->locker.locker4_u.lock_owner.lock_stateid;
82117c478bd9Sstevel@tonic-gate 		/* get lsp and hold the lock on the underlying file struct */
82127c478bd9Sstevel@tonic-gate 		if ((status = rfs4_get_lo_state(stateid, &lsp, TRUE))
82137c478bd9Sstevel@tonic-gate 		    != NFS4_OK) {
82147c478bd9Sstevel@tonic-gate 			*cs->statusp = resp->status = status;
82157c478bd9Sstevel@tonic-gate 			return;
82167c478bd9Sstevel@tonic-gate 		}
82177c478bd9Sstevel@tonic-gate 		create = FALSE;	/* We didn't create lsp */
82187c478bd9Sstevel@tonic-gate 
82197c478bd9Sstevel@tonic-gate 		/* Ensure specified filehandle matches */
82207c478bd9Sstevel@tonic-gate 		if (cs->vp != lsp->state->finfo->vp) {
82217c478bd9Sstevel@tonic-gate 			rfs4_lo_state_rele(lsp, TRUE);
82227c478bd9Sstevel@tonic-gate 			*cs->statusp = resp->status = NFS4ERR_BAD_STATEID;
82237c478bd9Sstevel@tonic-gate 			return;
82247c478bd9Sstevel@tonic-gate 		}
82257c478bd9Sstevel@tonic-gate 
82267c478bd9Sstevel@tonic-gate 		/* hold off other access to lsp while we tinker */
82277c478bd9Sstevel@tonic-gate 		rfs4_sw_enter(&lsp->ls_sw);
82287c478bd9Sstevel@tonic-gate 		ls_sw_held = TRUE;
82297c478bd9Sstevel@tonic-gate 
82307c478bd9Sstevel@tonic-gate 		switch (rfs4_check_lo_stateid_seqid(lsp, stateid)) {
82317c478bd9Sstevel@tonic-gate 		/*
82327c478bd9Sstevel@tonic-gate 		 * The stateid looks like it was okay (expected to be
82337c478bd9Sstevel@tonic-gate 		 * the next one)
82347c478bd9Sstevel@tonic-gate 		 */
82357c478bd9Sstevel@tonic-gate 		case NFS4_CHECK_STATEID_OKAY:
82367c478bd9Sstevel@tonic-gate 			/*
82377c478bd9Sstevel@tonic-gate 			 * The sequence id is now checked.  Determine
82387c478bd9Sstevel@tonic-gate 			 * if this is a replay or if it is in the
82397c478bd9Sstevel@tonic-gate 			 * expected (next) sequence.  In the case of a
82407c478bd9Sstevel@tonic-gate 			 * replay, there are two replay conditions
82417c478bd9Sstevel@tonic-gate 			 * that may occur.  The first is the normal
82427c478bd9Sstevel@tonic-gate 			 * condition where a LOCK is done with a
82437c478bd9Sstevel@tonic-gate 			 * NFS4_OK response and the stateid is
82447c478bd9Sstevel@tonic-gate 			 * updated.  That case is handled below when
82457c478bd9Sstevel@tonic-gate 			 * the stateid is identified as a REPLAY.  The
82467c478bd9Sstevel@tonic-gate 			 * second is the case where an error is
82477c478bd9Sstevel@tonic-gate 			 * returned, like NFS4ERR_DENIED, and the
82487c478bd9Sstevel@tonic-gate 			 * sequence number is updated but the stateid
82497c478bd9Sstevel@tonic-gate 			 * is not updated.  This second case is dealt
82507c478bd9Sstevel@tonic-gate 			 * with here.  So it may seem odd that the
82517c478bd9Sstevel@tonic-gate 			 * stateid is okay but the sequence id is a
82527c478bd9Sstevel@tonic-gate 			 * replay but it is okay.
82537c478bd9Sstevel@tonic-gate 			 */
82547c478bd9Sstevel@tonic-gate 			switch (rfs4_check_lock_seqid(
82557c478bd9Sstevel@tonic-gate 				args->locker.locker4_u.lock_owner.lock_seqid,
82567c478bd9Sstevel@tonic-gate 				lsp, resop)) {
82577c478bd9Sstevel@tonic-gate 			case NFS4_CHKSEQ_REPLAY:
82587c478bd9Sstevel@tonic-gate 				if (resp->status != NFS4_OK) {
82597c478bd9Sstevel@tonic-gate 					/*
82607c478bd9Sstevel@tonic-gate 					 * Here is our replay and need
82617c478bd9Sstevel@tonic-gate 					 * to verify that the last
82627c478bd9Sstevel@tonic-gate 					 * response was an error.
82637c478bd9Sstevel@tonic-gate 					 */
82647c478bd9Sstevel@tonic-gate 					*cs->statusp = resp->status;
82657c478bd9Sstevel@tonic-gate 					goto end;
82667c478bd9Sstevel@tonic-gate 				}
82677c478bd9Sstevel@tonic-gate 				/*
82687c478bd9Sstevel@tonic-gate 				 * This is done since the sequence id
82697c478bd9Sstevel@tonic-gate 				 * looked like a replay but it didn't
82707c478bd9Sstevel@tonic-gate 				 * pass our check so a BAD_SEQID is
82717c478bd9Sstevel@tonic-gate 				 * returned as a result.
82727c478bd9Sstevel@tonic-gate 				 */
82737c478bd9Sstevel@tonic-gate 				/*FALLTHROUGH*/
82747c478bd9Sstevel@tonic-gate 			case NFS4_CHKSEQ_BAD:
82757c478bd9Sstevel@tonic-gate 				*cs->statusp = resp->status =
82767c478bd9Sstevel@tonic-gate 					NFS4ERR_BAD_SEQID;
82777c478bd9Sstevel@tonic-gate 				goto end;
82787c478bd9Sstevel@tonic-gate 			case NFS4_CHKSEQ_OKAY:
82797c478bd9Sstevel@tonic-gate 				/* Everything looks okay move ahead */
82807c478bd9Sstevel@tonic-gate 				break;
82817c478bd9Sstevel@tonic-gate 			}
82827c478bd9Sstevel@tonic-gate 			break;
82837c478bd9Sstevel@tonic-gate 		case NFS4_CHECK_STATEID_OLD:
82847c478bd9Sstevel@tonic-gate 			*cs->statusp = resp->status = NFS4ERR_OLD_STATEID;
82857c478bd9Sstevel@tonic-gate 			goto end;
82867c478bd9Sstevel@tonic-gate 		case NFS4_CHECK_STATEID_BAD:
82877c478bd9Sstevel@tonic-gate 			*cs->statusp = resp->status = NFS4ERR_BAD_STATEID;
82887c478bd9Sstevel@tonic-gate 			goto end;
82897c478bd9Sstevel@tonic-gate 		case NFS4_CHECK_STATEID_EXPIRED:
82907c478bd9Sstevel@tonic-gate 			*cs->statusp = resp->status = NFS4ERR_EXPIRED;
82917c478bd9Sstevel@tonic-gate 			goto end;
82927c478bd9Sstevel@tonic-gate 		case NFS4_CHECK_STATEID_CLOSED:
82937c478bd9Sstevel@tonic-gate 			*cs->statusp = resp->status = NFS4ERR_OLD_STATEID;
82947c478bd9Sstevel@tonic-gate 			goto end;
82957c478bd9Sstevel@tonic-gate 		case NFS4_CHECK_STATEID_REPLAY:
82967c478bd9Sstevel@tonic-gate 			switch (rfs4_check_lock_seqid(
82977c478bd9Sstevel@tonic-gate 				args->locker.locker4_u.lock_owner.lock_seqid,
82987c478bd9Sstevel@tonic-gate 				lsp, resop)) {
82997c478bd9Sstevel@tonic-gate 			case NFS4_CHKSEQ_OKAY:
83007c478bd9Sstevel@tonic-gate 				/*
83017c478bd9Sstevel@tonic-gate 				 * This is a replayed stateid; if
83027c478bd9Sstevel@tonic-gate 				 * seqid matches the next expected,
83037c478bd9Sstevel@tonic-gate 				 * then client is using wrong seqid.
83047c478bd9Sstevel@tonic-gate 				 */
83057c478bd9Sstevel@tonic-gate 			case NFS4_CHKSEQ_BAD:
83067c478bd9Sstevel@tonic-gate 				*cs->statusp = resp->status =
83077c478bd9Sstevel@tonic-gate 					NFS4ERR_BAD_SEQID;
83087c478bd9Sstevel@tonic-gate 				goto end;
83097c478bd9Sstevel@tonic-gate 			case NFS4_CHKSEQ_REPLAY:
83107c478bd9Sstevel@tonic-gate 				rfs4_update_lease(lsp->locker->client);
83117c478bd9Sstevel@tonic-gate 				*cs->statusp = status = resp->status;
83127c478bd9Sstevel@tonic-gate 				goto end;
83137c478bd9Sstevel@tonic-gate 			}
83147c478bd9Sstevel@tonic-gate 			break;
83157c478bd9Sstevel@tonic-gate 		default:
83167c478bd9Sstevel@tonic-gate 			ASSERT(FALSE);
83177c478bd9Sstevel@tonic-gate 			break;
83187c478bd9Sstevel@tonic-gate 		}
83197c478bd9Sstevel@tonic-gate 
83207c478bd9Sstevel@tonic-gate 		rfs4_update_lock_sequence(lsp);
83217c478bd9Sstevel@tonic-gate 		rfs4_update_lease(lsp->locker->client);
83227c478bd9Sstevel@tonic-gate 	}
83237c478bd9Sstevel@tonic-gate 
83247c478bd9Sstevel@tonic-gate 	/*
83257c478bd9Sstevel@tonic-gate 	 * NFS4 only allows locking on regular files, so
83267c478bd9Sstevel@tonic-gate 	 * verify type of object.
83277c478bd9Sstevel@tonic-gate 	 */
83287c478bd9Sstevel@tonic-gate 	if (cs->vp->v_type != VREG) {
83297c478bd9Sstevel@tonic-gate 		if (cs->vp->v_type == VDIR)
83307c478bd9Sstevel@tonic-gate 			status = NFS4ERR_ISDIR;
83317c478bd9Sstevel@tonic-gate 		else
83327c478bd9Sstevel@tonic-gate 			status = NFS4ERR_INVAL;
83337c478bd9Sstevel@tonic-gate 		goto out;
83347c478bd9Sstevel@tonic-gate 	}
83357c478bd9Sstevel@tonic-gate 
83367c478bd9Sstevel@tonic-gate 	cp = lsp->state->owner->client;
83377c478bd9Sstevel@tonic-gate 
83387c478bd9Sstevel@tonic-gate 	if (rfs4_clnt_in_grace(cp) && !args->reclaim) {
83397c478bd9Sstevel@tonic-gate 		status = NFS4ERR_GRACE;
83407c478bd9Sstevel@tonic-gate 		goto out;
83417c478bd9Sstevel@tonic-gate 	}
83427c478bd9Sstevel@tonic-gate 
83437c478bd9Sstevel@tonic-gate 	if (rfs4_clnt_in_grace(cp) && args->reclaim && !cp->can_reclaim) {
83447c478bd9Sstevel@tonic-gate 		status = NFS4ERR_NO_GRACE;
83457c478bd9Sstevel@tonic-gate 		goto out;
83467c478bd9Sstevel@tonic-gate 	}
83477c478bd9Sstevel@tonic-gate 
83487c478bd9Sstevel@tonic-gate 	if (!rfs4_clnt_in_grace(cp) && args->reclaim) {
83497c478bd9Sstevel@tonic-gate 		status = NFS4ERR_NO_GRACE;
83507c478bd9Sstevel@tonic-gate 		goto out;
83517c478bd9Sstevel@tonic-gate 	}
83527c478bd9Sstevel@tonic-gate 
83537c478bd9Sstevel@tonic-gate 	if (lsp->state->finfo->dinfo->dtype == OPEN_DELEGATE_WRITE)
83547c478bd9Sstevel@tonic-gate 		cs->deleg = TRUE;
83557c478bd9Sstevel@tonic-gate 
83567c478bd9Sstevel@tonic-gate 	status = rfs4_do_lock(lsp, args->locktype,
83577c478bd9Sstevel@tonic-gate 				args->locker.locker4_u.lock_owner.lock_seqid,
83587c478bd9Sstevel@tonic-gate 				args->offset,
83597c478bd9Sstevel@tonic-gate 				args->length, cs->cr, resop);
83607c478bd9Sstevel@tonic-gate 
83617c478bd9Sstevel@tonic-gate out:
83627c478bd9Sstevel@tonic-gate 	lsp->skip_seqid_check = FALSE;
83637c478bd9Sstevel@tonic-gate 
83647c478bd9Sstevel@tonic-gate 	*cs->statusp = resp->status = status;
83657c478bd9Sstevel@tonic-gate 
83667c478bd9Sstevel@tonic-gate 	if (status == NFS4_OK) {
83677c478bd9Sstevel@tonic-gate 		resp->LOCK4res_u.lock_stateid = lsp->lockid.stateid;
83687c478bd9Sstevel@tonic-gate 		lsp->lock_completed = TRUE;
83697c478bd9Sstevel@tonic-gate 	}
83707c478bd9Sstevel@tonic-gate 	/*
83717c478bd9Sstevel@tonic-gate 	 * Only update the "OPEN" response here if this was a new
83727c478bd9Sstevel@tonic-gate 	 * lock_owner
83737c478bd9Sstevel@tonic-gate 	 */
83747c478bd9Sstevel@tonic-gate 	if (sp)
83757c478bd9Sstevel@tonic-gate 		rfs4_update_open_resp(sp->owner, resop, NULL);
83767c478bd9Sstevel@tonic-gate 
83777c478bd9Sstevel@tonic-gate 	rfs4_update_lock_resp(lsp, resop);
83787c478bd9Sstevel@tonic-gate 
83797c478bd9Sstevel@tonic-gate end:
83807c478bd9Sstevel@tonic-gate 	if (lsp) {
83817c478bd9Sstevel@tonic-gate 		if (ls_sw_held)
83827c478bd9Sstevel@tonic-gate 			rfs4_sw_exit(&lsp->ls_sw);
83837c478bd9Sstevel@tonic-gate 		/*
83847c478bd9Sstevel@tonic-gate 		 * If an sp obtained, then the lsp does not represent
83857c478bd9Sstevel@tonic-gate 		 * a lock on the file struct.
83867c478bd9Sstevel@tonic-gate 		 */
83877c478bd9Sstevel@tonic-gate 		if (sp != NULL)
83887c478bd9Sstevel@tonic-gate 			rfs4_lo_state_rele(lsp, FALSE);
83897c478bd9Sstevel@tonic-gate 		else
83907c478bd9Sstevel@tonic-gate 			rfs4_lo_state_rele(lsp, TRUE);
83917c478bd9Sstevel@tonic-gate 	}
83927c478bd9Sstevel@tonic-gate 	if (sp) {
83937c478bd9Sstevel@tonic-gate 		rfs4_sw_exit(&sp->owner->oo_sw);
83947c478bd9Sstevel@tonic-gate 		rfs4_state_rele(sp);
83957c478bd9Sstevel@tonic-gate 	}
83967c478bd9Sstevel@tonic-gate }
83977c478bd9Sstevel@tonic-gate 
83987c478bd9Sstevel@tonic-gate /* free function for LOCK/LOCKT */
83997c478bd9Sstevel@tonic-gate static void
84007c478bd9Sstevel@tonic-gate lock_denied_free(nfs_resop4 *resop)
84017c478bd9Sstevel@tonic-gate {
84027c478bd9Sstevel@tonic-gate 	LOCK4denied *dp = NULL;
84037c478bd9Sstevel@tonic-gate 
84047c478bd9Sstevel@tonic-gate 	switch (resop->resop) {
84057c478bd9Sstevel@tonic-gate 	case OP_LOCK:
84067c478bd9Sstevel@tonic-gate 		if (resop->nfs_resop4_u.oplock.status == NFS4ERR_DENIED)
84077c478bd9Sstevel@tonic-gate 			dp = &resop->nfs_resop4_u.oplock.LOCK4res_u.denied;
84087c478bd9Sstevel@tonic-gate 		break;
84097c478bd9Sstevel@tonic-gate 	case OP_LOCKT:
84107c478bd9Sstevel@tonic-gate 		if (resop->nfs_resop4_u.oplockt.status == NFS4ERR_DENIED)
84117c478bd9Sstevel@tonic-gate 			dp = &resop->nfs_resop4_u.oplockt.denied;
84127c478bd9Sstevel@tonic-gate 		break;
84137c478bd9Sstevel@tonic-gate 	default:
84147c478bd9Sstevel@tonic-gate 		break;
84157c478bd9Sstevel@tonic-gate 	}
84167c478bd9Sstevel@tonic-gate 
84177c478bd9Sstevel@tonic-gate 	if (dp)
84187c478bd9Sstevel@tonic-gate 		kmem_free(dp->owner.owner_val, dp->owner.owner_len);
84197c478bd9Sstevel@tonic-gate }
84207c478bd9Sstevel@tonic-gate 
84217c478bd9Sstevel@tonic-gate /*ARGSUSED*/
84227c478bd9Sstevel@tonic-gate void
84237c478bd9Sstevel@tonic-gate rfs4_op_locku(nfs_argop4 *argop, nfs_resop4 *resop,
84247c478bd9Sstevel@tonic-gate 	    struct svc_req *req, struct compound_state *cs)
84257c478bd9Sstevel@tonic-gate {
84267c478bd9Sstevel@tonic-gate 	/* XXX Currently not using req arg */
84277c478bd9Sstevel@tonic-gate 	LOCKU4args *args = &argop->nfs_argop4_u.oplocku;
84287c478bd9Sstevel@tonic-gate 	LOCKU4res *resp = &resop->nfs_resop4_u.oplocku;
84297c478bd9Sstevel@tonic-gate 	nfsstat4 status;
84307c478bd9Sstevel@tonic-gate 	stateid4 *stateid = &args->lock_stateid;
84317c478bd9Sstevel@tonic-gate 	rfs4_lo_state_t *lsp;
84327c478bd9Sstevel@tonic-gate 
84337c478bd9Sstevel@tonic-gate 	if (cs->vp == NULL) {
84347c478bd9Sstevel@tonic-gate 		*cs->statusp = resp->status = NFS4ERR_NOFILEHANDLE;
84357c478bd9Sstevel@tonic-gate 		return;
84367c478bd9Sstevel@tonic-gate 	}
84377c478bd9Sstevel@tonic-gate 
84387c478bd9Sstevel@tonic-gate 	if ((status = rfs4_get_lo_state(stateid, &lsp, TRUE)) != NFS4_OK) {
84397c478bd9Sstevel@tonic-gate 		*cs->statusp = resp->status = status;
84407c478bd9Sstevel@tonic-gate 		return;
84417c478bd9Sstevel@tonic-gate 	}
84427c478bd9Sstevel@tonic-gate 
84437c478bd9Sstevel@tonic-gate 	/* Ensure specified filehandle matches */
84447c478bd9Sstevel@tonic-gate 	if (cs->vp != lsp->state->finfo->vp) {
84457c478bd9Sstevel@tonic-gate 		rfs4_lo_state_rele(lsp, TRUE);
84467c478bd9Sstevel@tonic-gate 		*cs->statusp = resp->status = NFS4ERR_BAD_STATEID;
84477c478bd9Sstevel@tonic-gate 		return;
84487c478bd9Sstevel@tonic-gate 	}
84497c478bd9Sstevel@tonic-gate 
84507c478bd9Sstevel@tonic-gate 	/* hold off other access to lsp while we tinker */
84517c478bd9Sstevel@tonic-gate 	rfs4_sw_enter(&lsp->ls_sw);
84527c478bd9Sstevel@tonic-gate 
84537c478bd9Sstevel@tonic-gate 	switch (rfs4_check_lo_stateid_seqid(lsp, stateid)) {
84547c478bd9Sstevel@tonic-gate 	case NFS4_CHECK_STATEID_OKAY:
84557c478bd9Sstevel@tonic-gate 		if (rfs4_check_lock_seqid(args->seqid, lsp, resop)
84567c478bd9Sstevel@tonic-gate 		    != NFS4_CHKSEQ_OKAY) {
84577c478bd9Sstevel@tonic-gate 			*cs->statusp = resp->status = NFS4ERR_BAD_SEQID;
84587c478bd9Sstevel@tonic-gate 			goto end;
84597c478bd9Sstevel@tonic-gate 		}
84607c478bd9Sstevel@tonic-gate 		break;
84617c478bd9Sstevel@tonic-gate 	case NFS4_CHECK_STATEID_OLD:
84627c478bd9Sstevel@tonic-gate 		*cs->statusp = resp->status = NFS4ERR_OLD_STATEID;
84637c478bd9Sstevel@tonic-gate 		goto end;
84647c478bd9Sstevel@tonic-gate 	case NFS4_CHECK_STATEID_BAD:
84657c478bd9Sstevel@tonic-gate 		*cs->statusp = resp->status = NFS4ERR_BAD_STATEID;
84667c478bd9Sstevel@tonic-gate 		goto end;
84677c478bd9Sstevel@tonic-gate 	case NFS4_CHECK_STATEID_EXPIRED:
84687c478bd9Sstevel@tonic-gate 		*cs->statusp = resp->status = NFS4ERR_EXPIRED;
84697c478bd9Sstevel@tonic-gate 		goto end;
84707c478bd9Sstevel@tonic-gate 	case NFS4_CHECK_STATEID_CLOSED:
84717c478bd9Sstevel@tonic-gate 		*cs->statusp = resp->status = NFS4ERR_OLD_STATEID;
84727c478bd9Sstevel@tonic-gate 		goto end;
84737c478bd9Sstevel@tonic-gate 	case NFS4_CHECK_STATEID_REPLAY:
84747c478bd9Sstevel@tonic-gate 		switch (rfs4_check_lock_seqid(args->seqid, lsp, resop)) {
84757c478bd9Sstevel@tonic-gate 		case NFS4_CHKSEQ_OKAY:
84767c478bd9Sstevel@tonic-gate 				/*
84777c478bd9Sstevel@tonic-gate 				 * This is a replayed stateid; if
84787c478bd9Sstevel@tonic-gate 				 * seqid matches the next expected,
84797c478bd9Sstevel@tonic-gate 				 * then client is using wrong seqid.
84807c478bd9Sstevel@tonic-gate 				 */
84817c478bd9Sstevel@tonic-gate 		case NFS4_CHKSEQ_BAD:
84827c478bd9Sstevel@tonic-gate 			*cs->statusp = resp->status =
84837c478bd9Sstevel@tonic-gate 				NFS4ERR_BAD_SEQID;
84847c478bd9Sstevel@tonic-gate 			goto end;
84857c478bd9Sstevel@tonic-gate 		case NFS4_CHKSEQ_REPLAY:
84867c478bd9Sstevel@tonic-gate 			rfs4_update_lease(lsp->locker->client);
84877c478bd9Sstevel@tonic-gate 			*cs->statusp = status = resp->status;
84887c478bd9Sstevel@tonic-gate 			goto end;
84897c478bd9Sstevel@tonic-gate 		}
84907c478bd9Sstevel@tonic-gate 		break;
84917c478bd9Sstevel@tonic-gate 	default:
84927c478bd9Sstevel@tonic-gate 		ASSERT(FALSE);
84937c478bd9Sstevel@tonic-gate 		break;
84947c478bd9Sstevel@tonic-gate 	}
84957c478bd9Sstevel@tonic-gate 
84967c478bd9Sstevel@tonic-gate 	rfs4_update_lock_sequence(lsp);
84977c478bd9Sstevel@tonic-gate 	rfs4_update_lease(lsp->locker->client);
84987c478bd9Sstevel@tonic-gate 
84997c478bd9Sstevel@tonic-gate 	/*
85007c478bd9Sstevel@tonic-gate 	 * NFS4 only allows locking on regular files, so
85017c478bd9Sstevel@tonic-gate 	 * verify type of object.
85027c478bd9Sstevel@tonic-gate 	 */
85037c478bd9Sstevel@tonic-gate 	if (cs->vp->v_type != VREG) {
85047c478bd9Sstevel@tonic-gate 		if (cs->vp->v_type == VDIR)
85057c478bd9Sstevel@tonic-gate 			status = NFS4ERR_ISDIR;
85067c478bd9Sstevel@tonic-gate 		else
85077c478bd9Sstevel@tonic-gate 			status = NFS4ERR_INVAL;
85087c478bd9Sstevel@tonic-gate 		goto out;
85097c478bd9Sstevel@tonic-gate 	}
85107c478bd9Sstevel@tonic-gate 
85117c478bd9Sstevel@tonic-gate 	if (rfs4_clnt_in_grace(lsp->state->owner->client)) {
85127c478bd9Sstevel@tonic-gate 		status = NFS4ERR_GRACE;
85137c478bd9Sstevel@tonic-gate 		goto out;
85147c478bd9Sstevel@tonic-gate 	}
85157c478bd9Sstevel@tonic-gate 
85167c478bd9Sstevel@tonic-gate 	status = rfs4_do_lock(lsp, args->locktype,
85177c478bd9Sstevel@tonic-gate 			    args->seqid, args->offset,
85187c478bd9Sstevel@tonic-gate 			    args->length, cs->cr, resop);
85197c478bd9Sstevel@tonic-gate 
85207c478bd9Sstevel@tonic-gate out:
85217c478bd9Sstevel@tonic-gate 	*cs->statusp = resp->status = status;
85227c478bd9Sstevel@tonic-gate 
85237c478bd9Sstevel@tonic-gate 	if (status == NFS4_OK)
85247c478bd9Sstevel@tonic-gate 		resp->lock_stateid = lsp->lockid.stateid;
85257c478bd9Sstevel@tonic-gate 
85267c478bd9Sstevel@tonic-gate 	rfs4_update_lock_resp(lsp, resop);
85277c478bd9Sstevel@tonic-gate 
85287c478bd9Sstevel@tonic-gate end:
85297c478bd9Sstevel@tonic-gate 	rfs4_sw_exit(&lsp->ls_sw);
85307c478bd9Sstevel@tonic-gate 	rfs4_lo_state_rele(lsp, TRUE);
85317c478bd9Sstevel@tonic-gate }
85327c478bd9Sstevel@tonic-gate 
85337c478bd9Sstevel@tonic-gate /*
85347c478bd9Sstevel@tonic-gate  * LOCKT is a best effort routine, the client can not be guaranteed that
85357c478bd9Sstevel@tonic-gate  * the status return is still in effect by the time the reply is received.
85367c478bd9Sstevel@tonic-gate  * They are numerous race conditions in this routine, but we are not required
85377c478bd9Sstevel@tonic-gate  * and can not be accurate.
85387c478bd9Sstevel@tonic-gate  */
85397c478bd9Sstevel@tonic-gate /*ARGSUSED*/
85407c478bd9Sstevel@tonic-gate void
85417c478bd9Sstevel@tonic-gate rfs4_op_lockt(nfs_argop4 *argop, nfs_resop4 *resop,
85427c478bd9Sstevel@tonic-gate 	    struct svc_req *req, struct compound_state *cs)
85437c478bd9Sstevel@tonic-gate {
85447c478bd9Sstevel@tonic-gate 	LOCKT4args *args = &argop->nfs_argop4_u.oplockt;
85457c478bd9Sstevel@tonic-gate 	LOCKT4res *resp = &resop->nfs_resop4_u.oplockt;
85467c478bd9Sstevel@tonic-gate 	rfs4_lockowner_t *lo;
85477c478bd9Sstevel@tonic-gate 	rfs4_client_t *cp;
85487c478bd9Sstevel@tonic-gate 	bool_t create = FALSE;
85497c478bd9Sstevel@tonic-gate 	struct flock64 flk;
85507c478bd9Sstevel@tonic-gate 	int error;
85517c478bd9Sstevel@tonic-gate 	int flag = FREAD | FWRITE;
85527c478bd9Sstevel@tonic-gate 	int ltype;
85537c478bd9Sstevel@tonic-gate 	length4 posix_length;
85547c478bd9Sstevel@tonic-gate 	sysid_t sysid;
85557c478bd9Sstevel@tonic-gate 	pid_t pid;
85567c478bd9Sstevel@tonic-gate 
85577c478bd9Sstevel@tonic-gate 	if (cs->vp == NULL) {
85587c478bd9Sstevel@tonic-gate 		*cs->statusp = resp->status = NFS4ERR_NOFILEHANDLE;
85597c478bd9Sstevel@tonic-gate 		return;
85607c478bd9Sstevel@tonic-gate 	}
85617c478bd9Sstevel@tonic-gate 
85627c478bd9Sstevel@tonic-gate 	/*
85637c478bd9Sstevel@tonic-gate 	 * NFS4 only allows locking on regular files, so
85647c478bd9Sstevel@tonic-gate 	 * verify type of object.
85657c478bd9Sstevel@tonic-gate 	 */
85667c478bd9Sstevel@tonic-gate 	if (cs->vp->v_type != VREG) {
85677c478bd9Sstevel@tonic-gate 		if (cs->vp->v_type == VDIR)
85687c478bd9Sstevel@tonic-gate 			*cs->statusp = resp->status = NFS4ERR_ISDIR;
85697c478bd9Sstevel@tonic-gate 		else
85707c478bd9Sstevel@tonic-gate 			*cs->statusp = resp->status =  NFS4ERR_INVAL;
85717c478bd9Sstevel@tonic-gate 		return;
85727c478bd9Sstevel@tonic-gate 	}
85737c478bd9Sstevel@tonic-gate 
85747c478bd9Sstevel@tonic-gate 	/*
85757c478bd9Sstevel@tonic-gate 	 * Check out the clientid to ensure the server knows about it
85767c478bd9Sstevel@tonic-gate 	 * so that we correctly inform the client of a server reboot.
85777c478bd9Sstevel@tonic-gate 	 */
85787c478bd9Sstevel@tonic-gate 	if ((cp = rfs4_findclient_by_id(args->owner.clientid, FALSE))
85797c478bd9Sstevel@tonic-gate 	    == NULL) {
85807c478bd9Sstevel@tonic-gate 		*cs->statusp = resp->status =
85817c478bd9Sstevel@tonic-gate 			rfs4_check_clientid(&args->owner.clientid, 0);
85827c478bd9Sstevel@tonic-gate 		return;
85837c478bd9Sstevel@tonic-gate 	}
85847c478bd9Sstevel@tonic-gate 	if (rfs4_lease_expired(cp)) {
85857c478bd9Sstevel@tonic-gate 		rfs4_client_close(cp);
85867c478bd9Sstevel@tonic-gate 		/*
85877c478bd9Sstevel@tonic-gate 		 * Protocol doesn't allow returning NFS4ERR_STALE as
85887c478bd9Sstevel@tonic-gate 		 * other operations do on this check so STALE_CLIENTID
85897c478bd9Sstevel@tonic-gate 		 * is returned instead
85907c478bd9Sstevel@tonic-gate 		 */
85917c478bd9Sstevel@tonic-gate 		*cs->statusp = resp->status = NFS4ERR_STALE_CLIENTID;
85927c478bd9Sstevel@tonic-gate 		return;
85937c478bd9Sstevel@tonic-gate 	}
85947c478bd9Sstevel@tonic-gate 
85957c478bd9Sstevel@tonic-gate 	if (rfs4_clnt_in_grace(cp)) {
85967c478bd9Sstevel@tonic-gate 		*cs->statusp = resp->status = NFS4ERR_GRACE;
85977c478bd9Sstevel@tonic-gate 		return;
85987c478bd9Sstevel@tonic-gate 	}
85997c478bd9Sstevel@tonic-gate 	rfs4_client_rele(cp);
86007c478bd9Sstevel@tonic-gate 
86017c478bd9Sstevel@tonic-gate 	resp->status = NFS4_OK;
86027c478bd9Sstevel@tonic-gate 
86037c478bd9Sstevel@tonic-gate 	switch (args->locktype) {
86047c478bd9Sstevel@tonic-gate 	case READ_LT:
86057c478bd9Sstevel@tonic-gate 	case READW_LT:
86067c478bd9Sstevel@tonic-gate 		ltype = F_RDLCK;
86077c478bd9Sstevel@tonic-gate 		break;
86087c478bd9Sstevel@tonic-gate 	case WRITE_LT:
86097c478bd9Sstevel@tonic-gate 	case WRITEW_LT:
86107c478bd9Sstevel@tonic-gate 		ltype = F_WRLCK;
86117c478bd9Sstevel@tonic-gate 		break;
86127c478bd9Sstevel@tonic-gate 	}
86137c478bd9Sstevel@tonic-gate 
86147c478bd9Sstevel@tonic-gate 	posix_length = args->length;
86157c478bd9Sstevel@tonic-gate 	/* Check for zero length. To lock to end of file use all ones for V4 */
86167c478bd9Sstevel@tonic-gate 	if (posix_length == 0) {
86177c478bd9Sstevel@tonic-gate 		*cs->statusp = resp->status = NFS4ERR_INVAL;
86187c478bd9Sstevel@tonic-gate 		return;
86197c478bd9Sstevel@tonic-gate 	} else if (posix_length == (length4)(~0)) {
86207c478bd9Sstevel@tonic-gate 		posix_length = 0;	/* Posix to end of file  */
86217c478bd9Sstevel@tonic-gate 	}
86227c478bd9Sstevel@tonic-gate 
86237c478bd9Sstevel@tonic-gate 	/* Find or create a lockowner */
86247c478bd9Sstevel@tonic-gate 	lo = rfs4_findlockowner(&args->owner, &create);
86257c478bd9Sstevel@tonic-gate 
86267c478bd9Sstevel@tonic-gate 	if (lo) {
86277c478bd9Sstevel@tonic-gate 		pid = lo->pid;
86287c478bd9Sstevel@tonic-gate 		if ((resp->status =
86297c478bd9Sstevel@tonic-gate 			rfs4_client_sysid(lo->client, &sysid)) != NFS4_OK)
86307c478bd9Sstevel@tonic-gate 		goto out;
86317c478bd9Sstevel@tonic-gate 	} else {
86327c478bd9Sstevel@tonic-gate 		pid = 0;
86337c478bd9Sstevel@tonic-gate 		sysid = lockt_sysid;
86347c478bd9Sstevel@tonic-gate 	}
86357c478bd9Sstevel@tonic-gate retry:
86367c478bd9Sstevel@tonic-gate 	flk.l_type = ltype;
86377c478bd9Sstevel@tonic-gate 	flk.l_whence = 0;		/* SEEK_SET */
86387c478bd9Sstevel@tonic-gate 	flk.l_start = args->offset;
86397c478bd9Sstevel@tonic-gate 	flk.l_len = posix_length;
86407c478bd9Sstevel@tonic-gate 	flk.l_sysid = sysid;
86417c478bd9Sstevel@tonic-gate 	flk.l_pid = pid;
86427c478bd9Sstevel@tonic-gate 	flag |= F_REMOTELOCK;
86437c478bd9Sstevel@tonic-gate 
86447c478bd9Sstevel@tonic-gate 	LOCK_PRINT(rfs4_debug, "rfs4_op_lockt", F_GETLK, &flk);
86457c478bd9Sstevel@tonic-gate 
86467c478bd9Sstevel@tonic-gate 	/* Note that length4 is uint64_t but l_len and l_start are off64_t */
86477c478bd9Sstevel@tonic-gate 	if (flk.l_len < 0 || flk.l_start < 0) {
86487c478bd9Sstevel@tonic-gate 		resp->status = NFS4ERR_INVAL;
86497c478bd9Sstevel@tonic-gate 		goto out;
86507c478bd9Sstevel@tonic-gate 	}
86517c478bd9Sstevel@tonic-gate 	error = VOP_FRLOCK(cs->vp, F_GETLK, &flk, flag, (u_offset_t)0,
86527c478bd9Sstevel@tonic-gate 	    NULL, cs->cr);
86537c478bd9Sstevel@tonic-gate 
86547c478bd9Sstevel@tonic-gate 	/*
86557c478bd9Sstevel@tonic-gate 	 * N.B. We map error values to nfsv4 errors. This is differrent
86567c478bd9Sstevel@tonic-gate 	 * than puterrno4 routine.
86577c478bd9Sstevel@tonic-gate 	 */
86587c478bd9Sstevel@tonic-gate 	switch (error) {
86597c478bd9Sstevel@tonic-gate 	case 0:
86607c478bd9Sstevel@tonic-gate 		if (flk.l_type == F_UNLCK)
86617c478bd9Sstevel@tonic-gate 			resp->status = NFS4_OK;
86627c478bd9Sstevel@tonic-gate 		else {
86637c478bd9Sstevel@tonic-gate 			if (lock_denied(&resp->denied, &flk) == NFS4ERR_EXPIRED)
86647c478bd9Sstevel@tonic-gate 				goto retry;
86657c478bd9Sstevel@tonic-gate 			resp->status = NFS4ERR_DENIED;
86667c478bd9Sstevel@tonic-gate 		}
86677c478bd9Sstevel@tonic-gate 		break;
86687c478bd9Sstevel@tonic-gate 	case EOVERFLOW:
86697c478bd9Sstevel@tonic-gate 		resp->status = NFS4ERR_INVAL;
86707c478bd9Sstevel@tonic-gate 		break;
86717c478bd9Sstevel@tonic-gate 	case EINVAL:
86727c478bd9Sstevel@tonic-gate 		resp->status = NFS4ERR_NOTSUPP;
86737c478bd9Sstevel@tonic-gate 		break;
86747c478bd9Sstevel@tonic-gate 	default:
86757c478bd9Sstevel@tonic-gate 		cmn_err(CE_WARN, "rfs4_op_lockt: unexpected errno (%d)",
86767c478bd9Sstevel@tonic-gate 			error);
86777c478bd9Sstevel@tonic-gate 		resp->status = NFS4ERR_SERVERFAULT;
86787c478bd9Sstevel@tonic-gate 		break;
86797c478bd9Sstevel@tonic-gate 	}
86807c478bd9Sstevel@tonic-gate 
86817c478bd9Sstevel@tonic-gate out:
86827c478bd9Sstevel@tonic-gate 	if (lo)
86837c478bd9Sstevel@tonic-gate 		rfs4_lockowner_rele(lo);
86847c478bd9Sstevel@tonic-gate 	*cs->statusp = resp->status;
86857c478bd9Sstevel@tonic-gate }
86867c478bd9Sstevel@tonic-gate 
86877c478bd9Sstevel@tonic-gate static int
86887c478bd9Sstevel@tonic-gate vop_shrlock(vnode_t *vp, int cmd, struct shrlock *sp, int fflags)
86897c478bd9Sstevel@tonic-gate {
86907c478bd9Sstevel@tonic-gate 	int err;
86917c478bd9Sstevel@tonic-gate 
86927c478bd9Sstevel@tonic-gate 	if (cmd == F_UNSHARE && sp->s_deny == 0 && sp->s_access == 0)
86937c478bd9Sstevel@tonic-gate 		return (0);
86947c478bd9Sstevel@tonic-gate 
86957c478bd9Sstevel@tonic-gate 	err = VOP_SHRLOCK(vp, cmd, sp, fflags, CRED());
86967c478bd9Sstevel@tonic-gate 
86977c478bd9Sstevel@tonic-gate 	NFS4_DEBUG(rfs4_shrlock_debug,
86987c478bd9Sstevel@tonic-gate 		(CE_NOTE, "rfs4_shrlock %s vp=%p acc=%d dny=%d sysid=%d "
86997c478bd9Sstevel@tonic-gate 		"pid=%d err=%d\n", cmd == F_SHARE ? "SHARE" : "UNSHR",
87007c478bd9Sstevel@tonic-gate 		(void *) vp, sp->s_access, sp->s_deny, sp->s_sysid, sp->s_pid,
87017c478bd9Sstevel@tonic-gate 		err));
87027c478bd9Sstevel@tonic-gate 
87037c478bd9Sstevel@tonic-gate 	return (err);
87047c478bd9Sstevel@tonic-gate }
87057c478bd9Sstevel@tonic-gate 
87067c478bd9Sstevel@tonic-gate static int
87077c478bd9Sstevel@tonic-gate rfs4_shrlock(rfs4_state_t *sp, int cmd)
87087c478bd9Sstevel@tonic-gate {
87097c478bd9Sstevel@tonic-gate 	struct shrlock shr;
87107c478bd9Sstevel@tonic-gate 	struct shr_locowner shr_loco;
87117c478bd9Sstevel@tonic-gate 	int fflags;
87127c478bd9Sstevel@tonic-gate 
87137c478bd9Sstevel@tonic-gate 	fflags = shr.s_access = shr.s_deny = 0;
87147c478bd9Sstevel@tonic-gate 
87157c478bd9Sstevel@tonic-gate 	if (sp->share_access & OPEN4_SHARE_ACCESS_READ) {
87167c478bd9Sstevel@tonic-gate 		fflags |= FREAD;
87177c478bd9Sstevel@tonic-gate 		shr.s_access |= F_RDACC;
87187c478bd9Sstevel@tonic-gate 	}
87197c478bd9Sstevel@tonic-gate 	if (sp->share_access & OPEN4_SHARE_ACCESS_WRITE) {
87207c478bd9Sstevel@tonic-gate 		fflags |= FWRITE;
87217c478bd9Sstevel@tonic-gate 		shr.s_access |= F_WRACC;
87227c478bd9Sstevel@tonic-gate 	}
87237c478bd9Sstevel@tonic-gate 	if (sp->share_deny & OPEN4_SHARE_DENY_READ)
87247c478bd9Sstevel@tonic-gate 		shr.s_deny |= F_RDDNY;
87257c478bd9Sstevel@tonic-gate 	if (sp->share_deny & OPEN4_SHARE_DENY_WRITE)
87267c478bd9Sstevel@tonic-gate 		shr.s_deny |= F_WRDNY;
87277c478bd9Sstevel@tonic-gate 
87287c478bd9Sstevel@tonic-gate 	shr.s_pid = rfs4_dbe_getid(sp->owner->dbe);
87297c478bd9Sstevel@tonic-gate 	shr.s_sysid = sp->owner->client->sysidt;
87307c478bd9Sstevel@tonic-gate 	shr_loco.sl_pid = shr.s_pid;
87317c478bd9Sstevel@tonic-gate 	shr_loco.sl_id = shr.s_sysid;
87327c478bd9Sstevel@tonic-gate 	shr.s_owner = (caddr_t)&shr_loco;
87337c478bd9Sstevel@tonic-gate 	shr.s_own_len = sizeof (shr_loco);
87347c478bd9Sstevel@tonic-gate 	return (vop_shrlock(sp->finfo->vp, cmd, &shr, fflags));
87357c478bd9Sstevel@tonic-gate }
87367c478bd9Sstevel@tonic-gate 
87377c478bd9Sstevel@tonic-gate static int
87387c478bd9Sstevel@tonic-gate rfs4_share(rfs4_state_t *sp)
87397c478bd9Sstevel@tonic-gate {
87407c478bd9Sstevel@tonic-gate 	return (rfs4_shrlock(sp, F_SHARE));
87417c478bd9Sstevel@tonic-gate }
87427c478bd9Sstevel@tonic-gate 
87437c478bd9Sstevel@tonic-gate void
87447c478bd9Sstevel@tonic-gate rfs4_unshare(rfs4_state_t *sp)
87457c478bd9Sstevel@tonic-gate {
87467c478bd9Sstevel@tonic-gate 	(void) rfs4_shrlock(sp, F_UNSHARE);
87477c478bd9Sstevel@tonic-gate }
8748